pinctrl-microchip-sgpio.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Microsemi/Microchip SoCs serial gpio driver
  4. *
  5. * Author: Lars Povlsen <lars.povlsen@microchip.com>
  6. *
  7. * Copyright (c) 2020 Microchip Technology Inc. and its subsidiaries.
  8. */
  9. #include <linux/bitfield.h>
  10. #include <linux/bits.h>
  11. #include <linux/clk.h>
  12. #include <linux/gpio/driver.h>
  13. #include <linux/io.h>
  14. #include <linux/mfd/ocelot.h>
  15. #include <linux/mod_devicetable.h>
  16. #include <linux/module.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/property.h>
  19. #include <linux/regmap.h>
  20. #include <linux/reset.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/pinctrl/pinconf.h>
  23. #include <linux/pinctrl/pinmux.h>
  24. #include "core.h"
  25. #include "pinconf.h"
  26. #define SGPIO_BITS_PER_WORD 32
  27. #define SGPIO_MAX_BITS 4
  28. #define SGPIO_SRC_BITS 3 /* 3 bit wide field per pin */
  29. enum {
  30. REG_INPUT_DATA,
  31. REG_PORT_CONFIG,
  32. REG_PORT_ENABLE,
  33. REG_SIO_CONFIG,
  34. REG_SIO_CLOCK,
  35. REG_INT_POLARITY,
  36. REG_INT_TRIGGER,
  37. REG_INT_ACK,
  38. REG_INT_ENABLE,
  39. REG_INT_IDENT,
  40. MAXREG
  41. };
  42. enum {
  43. SGPIO_ARCH_LUTON,
  44. SGPIO_ARCH_OCELOT,
  45. SGPIO_ARCH_SPARX5,
  46. };
  47. enum {
  48. SGPIO_FLAGS_HAS_IRQ = BIT(0),
  49. };
  50. struct sgpio_properties {
  51. int arch;
  52. int flags;
  53. u8 regoff[MAXREG];
  54. };
  55. #define SGPIO_LUTON_AUTO_REPEAT BIT(5)
  56. #define SGPIO_LUTON_PORT_WIDTH GENMASK(3, 2)
  57. #define SGPIO_LUTON_CLK_FREQ GENMASK(11, 0)
  58. #define SGPIO_LUTON_BIT_SOURCE GENMASK(11, 0)
  59. #define SGPIO_OCELOT_AUTO_REPEAT BIT(10)
  60. #define SGPIO_OCELOT_SINGLE_SHOT BIT(11)
  61. #define SGPIO_OCELOT_PORT_WIDTH GENMASK(8, 7)
  62. #define SGPIO_OCELOT_CLK_FREQ GENMASK(19, 8)
  63. #define SGPIO_OCELOT_BIT_SOURCE GENMASK(23, 12)
  64. #define SGPIO_SPARX5_AUTO_REPEAT BIT(6)
  65. #define SGPIO_SPARX5_SINGLE_SHOT BIT(7)
  66. #define SGPIO_SPARX5_PORT_WIDTH GENMASK(4, 3)
  67. #define SGPIO_SPARX5_CLK_FREQ GENMASK(19, 8)
  68. #define SGPIO_SPARX5_BIT_SOURCE GENMASK(23, 12)
  69. #define SGPIO_MASTER_INTR_ENA BIT(0)
  70. #define SGPIO_INT_TRG_LEVEL 0
  71. #define SGPIO_INT_TRG_EDGE 1
  72. #define SGPIO_INT_TRG_EDGE_FALL 2
  73. #define SGPIO_INT_TRG_EDGE_RISE 3
  74. #define SGPIO_TRG_LEVEL_HIGH 0
  75. #define SGPIO_TRG_LEVEL_LOW 1
  76. static const struct sgpio_properties properties_luton = {
  77. .arch = SGPIO_ARCH_LUTON,
  78. .regoff = { 0x00, 0x09, 0x29, 0x2a, 0x2b },
  79. };
  80. static const struct sgpio_properties properties_ocelot = {
  81. .arch = SGPIO_ARCH_OCELOT,
  82. .regoff = { 0x00, 0x06, 0x26, 0x04, 0x05 },
  83. };
  84. static const struct sgpio_properties properties_sparx5 = {
  85. .arch = SGPIO_ARCH_SPARX5,
  86. .flags = SGPIO_FLAGS_HAS_IRQ,
  87. .regoff = { 0x00, 0x06, 0x26, 0x04, 0x05, 0x2a, 0x32, 0x3a, 0x3e, 0x42 },
  88. };
  89. static const char * const functions[] = { "gpio" };
  90. struct sgpio_bank {
  91. struct sgpio_priv *priv;
  92. bool is_input;
  93. struct gpio_chip gpio;
  94. struct pinctrl_desc pctl_desc;
  95. };
  96. struct sgpio_priv {
  97. struct device *dev;
  98. struct sgpio_bank in;
  99. struct sgpio_bank out;
  100. u32 bitcount;
  101. u32 ports;
  102. u32 clock;
  103. struct regmap *regs;
  104. const struct sgpio_properties *properties;
  105. spinlock_t lock;
  106. /* protects the config register and single shot mode */
  107. struct mutex poll_lock;
  108. };
  109. struct sgpio_port_addr {
  110. u8 port;
  111. u8 bit;
  112. };
  113. static inline void sgpio_pin_to_addr(struct sgpio_priv *priv, int pin,
  114. struct sgpio_port_addr *addr)
  115. {
  116. addr->port = pin / priv->bitcount;
  117. addr->bit = pin % priv->bitcount;
  118. }
  119. static inline int sgpio_addr_to_pin(struct sgpio_priv *priv, int port, int bit)
  120. {
  121. return bit + port * priv->bitcount;
  122. }
  123. static inline u32 sgpio_get_addr(struct sgpio_priv *priv, u32 rno, u32 off)
  124. {
  125. return (priv->properties->regoff[rno] + off) *
  126. regmap_get_reg_stride(priv->regs);
  127. }
  128. static u32 sgpio_readl(struct sgpio_priv *priv, u32 rno, u32 off)
  129. {
  130. u32 addr = sgpio_get_addr(priv, rno, off);
  131. u32 val = 0;
  132. int ret;
  133. ret = regmap_read(priv->regs, addr, &val);
  134. WARN_ONCE(ret, "error reading sgpio reg %d\n", ret);
  135. return val;
  136. }
  137. static void sgpio_writel(struct sgpio_priv *priv,
  138. u32 val, u32 rno, u32 off)
  139. {
  140. u32 addr = sgpio_get_addr(priv, rno, off);
  141. int ret;
  142. ret = regmap_write(priv->regs, addr, val);
  143. WARN_ONCE(ret, "error writing sgpio reg %d\n", ret);
  144. }
  145. static inline void sgpio_clrsetbits(struct sgpio_priv *priv,
  146. u32 rno, u32 off, u32 clear, u32 set)
  147. {
  148. u32 addr = sgpio_get_addr(priv, rno, off);
  149. int ret;
  150. ret = regmap_update_bits(priv->regs, addr, clear | set, set);
  151. WARN_ONCE(ret, "error updating sgpio reg %d\n", ret);
  152. }
  153. static inline void sgpio_configure_bitstream(struct sgpio_priv *priv)
  154. {
  155. int width = priv->bitcount - 1;
  156. u32 clr, set;
  157. switch (priv->properties->arch) {
  158. case SGPIO_ARCH_LUTON:
  159. clr = SGPIO_LUTON_PORT_WIDTH;
  160. set = SGPIO_LUTON_AUTO_REPEAT |
  161. FIELD_PREP(SGPIO_LUTON_PORT_WIDTH, width);
  162. break;
  163. case SGPIO_ARCH_OCELOT:
  164. clr = SGPIO_OCELOT_PORT_WIDTH;
  165. set = SGPIO_OCELOT_AUTO_REPEAT |
  166. FIELD_PREP(SGPIO_OCELOT_PORT_WIDTH, width);
  167. break;
  168. case SGPIO_ARCH_SPARX5:
  169. clr = SGPIO_SPARX5_PORT_WIDTH;
  170. set = SGPIO_SPARX5_AUTO_REPEAT |
  171. FIELD_PREP(SGPIO_SPARX5_PORT_WIDTH, width);
  172. break;
  173. default:
  174. return;
  175. }
  176. sgpio_clrsetbits(priv, REG_SIO_CONFIG, 0, clr, set);
  177. }
  178. static inline void sgpio_configure_clock(struct sgpio_priv *priv, u32 clkfrq)
  179. {
  180. u32 clr, set;
  181. switch (priv->properties->arch) {
  182. case SGPIO_ARCH_LUTON:
  183. clr = SGPIO_LUTON_CLK_FREQ;
  184. set = FIELD_PREP(SGPIO_LUTON_CLK_FREQ, clkfrq);
  185. break;
  186. case SGPIO_ARCH_OCELOT:
  187. clr = SGPIO_OCELOT_CLK_FREQ;
  188. set = FIELD_PREP(SGPIO_OCELOT_CLK_FREQ, clkfrq);
  189. break;
  190. case SGPIO_ARCH_SPARX5:
  191. clr = SGPIO_SPARX5_CLK_FREQ;
  192. set = FIELD_PREP(SGPIO_SPARX5_CLK_FREQ, clkfrq);
  193. break;
  194. default:
  195. return;
  196. }
  197. sgpio_clrsetbits(priv, REG_SIO_CLOCK, 0, clr, set);
  198. }
  199. static int sgpio_single_shot(struct sgpio_priv *priv)
  200. {
  201. u32 addr = sgpio_get_addr(priv, REG_SIO_CONFIG, 0);
  202. int ret, ret2;
  203. u32 ctrl;
  204. unsigned int single_shot;
  205. unsigned int auto_repeat;
  206. switch (priv->properties->arch) {
  207. case SGPIO_ARCH_LUTON:
  208. /* not supported for now */
  209. return 0;
  210. case SGPIO_ARCH_OCELOT:
  211. single_shot = SGPIO_OCELOT_SINGLE_SHOT;
  212. auto_repeat = SGPIO_OCELOT_AUTO_REPEAT;
  213. break;
  214. case SGPIO_ARCH_SPARX5:
  215. single_shot = SGPIO_SPARX5_SINGLE_SHOT;
  216. auto_repeat = SGPIO_SPARX5_AUTO_REPEAT;
  217. break;
  218. default:
  219. return -EINVAL;
  220. }
  221. /*
  222. * Trigger immediate burst. This only works when auto repeat is turned
  223. * off. Otherwise, the single shot bit will never be cleared by the
  224. * hardware. Measurements showed that an update might take as long as
  225. * the burst gap. On a LAN9668 this is about 50ms for the largest
  226. * setting.
  227. * After the manual burst, reenable the auto repeat mode again.
  228. */
  229. mutex_lock(&priv->poll_lock);
  230. ret = regmap_update_bits(priv->regs, addr, single_shot | auto_repeat,
  231. single_shot);
  232. if (ret)
  233. goto out;
  234. ret = regmap_read_poll_timeout(priv->regs, addr, ctrl,
  235. !(ctrl & single_shot), 100, 60000);
  236. /* reenable auto repeat mode even if there was an error */
  237. ret2 = regmap_update_bits(priv->regs, addr, auto_repeat, auto_repeat);
  238. out:
  239. mutex_unlock(&priv->poll_lock);
  240. return ret ?: ret2;
  241. }
  242. static int sgpio_output_set(struct sgpio_priv *priv,
  243. struct sgpio_port_addr *addr,
  244. int value)
  245. {
  246. unsigned int bit = SGPIO_SRC_BITS * addr->bit;
  247. u32 reg = sgpio_get_addr(priv, REG_PORT_CONFIG, addr->port);
  248. bool changed;
  249. u32 clr, set;
  250. int ret;
  251. switch (priv->properties->arch) {
  252. case SGPIO_ARCH_LUTON:
  253. clr = FIELD_PREP(SGPIO_LUTON_BIT_SOURCE, BIT(bit));
  254. set = FIELD_PREP(SGPIO_LUTON_BIT_SOURCE, value << bit);
  255. break;
  256. case SGPIO_ARCH_OCELOT:
  257. clr = FIELD_PREP(SGPIO_OCELOT_BIT_SOURCE, BIT(bit));
  258. set = FIELD_PREP(SGPIO_OCELOT_BIT_SOURCE, value << bit);
  259. break;
  260. case SGPIO_ARCH_SPARX5:
  261. clr = FIELD_PREP(SGPIO_SPARX5_BIT_SOURCE, BIT(bit));
  262. set = FIELD_PREP(SGPIO_SPARX5_BIT_SOURCE, value << bit);
  263. break;
  264. default:
  265. return -EINVAL;
  266. }
  267. ret = regmap_update_bits_check(priv->regs, reg, clr | set, set,
  268. &changed);
  269. if (ret)
  270. return ret;
  271. if (changed) {
  272. ret = sgpio_single_shot(priv);
  273. if (ret)
  274. return ret;
  275. }
  276. return 0;
  277. }
  278. static int sgpio_output_get(struct sgpio_priv *priv,
  279. struct sgpio_port_addr *addr)
  280. {
  281. u32 val, portval = sgpio_readl(priv, REG_PORT_CONFIG, addr->port);
  282. unsigned int bit = SGPIO_SRC_BITS * addr->bit;
  283. switch (priv->properties->arch) {
  284. case SGPIO_ARCH_LUTON:
  285. val = FIELD_GET(SGPIO_LUTON_BIT_SOURCE, portval);
  286. break;
  287. case SGPIO_ARCH_OCELOT:
  288. val = FIELD_GET(SGPIO_OCELOT_BIT_SOURCE, portval);
  289. break;
  290. case SGPIO_ARCH_SPARX5:
  291. val = FIELD_GET(SGPIO_SPARX5_BIT_SOURCE, portval);
  292. break;
  293. default:
  294. val = 0;
  295. break;
  296. }
  297. return !!(val & BIT(bit));
  298. }
  299. static int sgpio_input_get(struct sgpio_priv *priv,
  300. struct sgpio_port_addr *addr)
  301. {
  302. return !!(sgpio_readl(priv, REG_INPUT_DATA, addr->bit) & BIT(addr->port));
  303. }
  304. static int sgpio_pinconf_get(struct pinctrl_dev *pctldev,
  305. unsigned int pin, unsigned long *config)
  306. {
  307. struct sgpio_bank *bank = pinctrl_dev_get_drvdata(pctldev);
  308. u32 param = pinconf_to_config_param(*config);
  309. struct sgpio_priv *priv = bank->priv;
  310. struct sgpio_port_addr addr;
  311. int val;
  312. sgpio_pin_to_addr(priv, pin, &addr);
  313. switch (param) {
  314. case PIN_CONFIG_INPUT_ENABLE:
  315. val = bank->is_input;
  316. break;
  317. case PIN_CONFIG_OUTPUT_ENABLE:
  318. val = !bank->is_input;
  319. break;
  320. case PIN_CONFIG_OUTPUT:
  321. if (bank->is_input)
  322. return -EINVAL;
  323. val = sgpio_output_get(priv, &addr);
  324. break;
  325. default:
  326. return -ENOTSUPP;
  327. }
  328. *config = pinconf_to_config_packed(param, val);
  329. return 0;
  330. }
  331. static int sgpio_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
  332. unsigned long *configs, unsigned int num_configs)
  333. {
  334. struct sgpio_bank *bank = pinctrl_dev_get_drvdata(pctldev);
  335. struct sgpio_priv *priv = bank->priv;
  336. struct sgpio_port_addr addr;
  337. int cfg, err = 0;
  338. u32 param, arg;
  339. sgpio_pin_to_addr(priv, pin, &addr);
  340. for (cfg = 0; cfg < num_configs; cfg++) {
  341. param = pinconf_to_config_param(configs[cfg]);
  342. arg = pinconf_to_config_argument(configs[cfg]);
  343. switch (param) {
  344. case PIN_CONFIG_OUTPUT:
  345. if (bank->is_input)
  346. return -EINVAL;
  347. err = sgpio_output_set(priv, &addr, arg);
  348. break;
  349. default:
  350. err = -ENOTSUPP;
  351. }
  352. }
  353. return err;
  354. }
  355. static const struct pinconf_ops sgpio_confops = {
  356. .is_generic = true,
  357. .pin_config_get = sgpio_pinconf_get,
  358. .pin_config_set = sgpio_pinconf_set,
  359. .pin_config_config_dbg_show = pinconf_generic_dump_config,
  360. };
  361. static int sgpio_get_functions_count(struct pinctrl_dev *pctldev)
  362. {
  363. return 1;
  364. }
  365. static const char *sgpio_get_function_name(struct pinctrl_dev *pctldev,
  366. unsigned int function)
  367. {
  368. return functions[0];
  369. }
  370. static int sgpio_get_function_groups(struct pinctrl_dev *pctldev,
  371. unsigned int function,
  372. const char *const **groups,
  373. unsigned *const num_groups)
  374. {
  375. *groups = functions;
  376. *num_groups = ARRAY_SIZE(functions);
  377. return 0;
  378. }
  379. static int sgpio_pinmux_set_mux(struct pinctrl_dev *pctldev,
  380. unsigned int selector, unsigned int group)
  381. {
  382. return 0;
  383. }
  384. static int sgpio_gpio_set_direction(struct pinctrl_dev *pctldev,
  385. struct pinctrl_gpio_range *range,
  386. unsigned int pin, bool input)
  387. {
  388. struct sgpio_bank *bank = pinctrl_dev_get_drvdata(pctldev);
  389. return (input == bank->is_input) ? 0 : -EINVAL;
  390. }
  391. static int sgpio_gpio_request_enable(struct pinctrl_dev *pctldev,
  392. struct pinctrl_gpio_range *range,
  393. unsigned int offset)
  394. {
  395. struct sgpio_bank *bank = pinctrl_dev_get_drvdata(pctldev);
  396. struct sgpio_priv *priv = bank->priv;
  397. struct sgpio_port_addr addr;
  398. sgpio_pin_to_addr(priv, offset, &addr);
  399. if ((priv->ports & BIT(addr.port)) == 0) {
  400. dev_warn(priv->dev, "Request port %d.%d: Port is not enabled\n",
  401. addr.port, addr.bit);
  402. return -EINVAL;
  403. }
  404. return 0;
  405. }
  406. static const struct pinmux_ops sgpio_pmx_ops = {
  407. .get_functions_count = sgpio_get_functions_count,
  408. .get_function_name = sgpio_get_function_name,
  409. .get_function_groups = sgpio_get_function_groups,
  410. .set_mux = sgpio_pinmux_set_mux,
  411. .gpio_set_direction = sgpio_gpio_set_direction,
  412. .gpio_request_enable = sgpio_gpio_request_enable,
  413. };
  414. static int sgpio_pctl_get_groups_count(struct pinctrl_dev *pctldev)
  415. {
  416. struct sgpio_bank *bank = pinctrl_dev_get_drvdata(pctldev);
  417. return bank->pctl_desc.npins;
  418. }
  419. static const char *sgpio_pctl_get_group_name(struct pinctrl_dev *pctldev,
  420. unsigned int group)
  421. {
  422. struct sgpio_bank *bank = pinctrl_dev_get_drvdata(pctldev);
  423. return bank->pctl_desc.pins[group].name;
  424. }
  425. static int sgpio_pctl_get_group_pins(struct pinctrl_dev *pctldev,
  426. unsigned int group,
  427. const unsigned int **pins,
  428. unsigned int *num_pins)
  429. {
  430. struct sgpio_bank *bank = pinctrl_dev_get_drvdata(pctldev);
  431. *pins = &bank->pctl_desc.pins[group].number;
  432. *num_pins = 1;
  433. return 0;
  434. }
  435. static const struct pinctrl_ops sgpio_pctl_ops = {
  436. .get_groups_count = sgpio_pctl_get_groups_count,
  437. .get_group_name = sgpio_pctl_get_group_name,
  438. .get_group_pins = sgpio_pctl_get_group_pins,
  439. .dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
  440. .dt_free_map = pinconf_generic_dt_free_map,
  441. };
  442. static int microchip_sgpio_direction_input(struct gpio_chip *gc, unsigned int gpio)
  443. {
  444. struct sgpio_bank *bank = gpiochip_get_data(gc);
  445. /* Fixed-position function */
  446. return bank->is_input ? 0 : -EINVAL;
  447. }
  448. static int microchip_sgpio_direction_output(struct gpio_chip *gc,
  449. unsigned int gpio, int value)
  450. {
  451. struct sgpio_bank *bank = gpiochip_get_data(gc);
  452. struct sgpio_priv *priv = bank->priv;
  453. struct sgpio_port_addr addr;
  454. /* Fixed-position function */
  455. if (bank->is_input)
  456. return -EINVAL;
  457. sgpio_pin_to_addr(priv, gpio, &addr);
  458. return sgpio_output_set(priv, &addr, value);
  459. }
  460. static int microchip_sgpio_get_direction(struct gpio_chip *gc, unsigned int gpio)
  461. {
  462. struct sgpio_bank *bank = gpiochip_get_data(gc);
  463. return bank->is_input ? GPIO_LINE_DIRECTION_IN : GPIO_LINE_DIRECTION_OUT;
  464. }
  465. static void microchip_sgpio_set_value(struct gpio_chip *gc,
  466. unsigned int gpio, int value)
  467. {
  468. microchip_sgpio_direction_output(gc, gpio, value);
  469. }
  470. static int microchip_sgpio_get_value(struct gpio_chip *gc, unsigned int gpio)
  471. {
  472. struct sgpio_bank *bank = gpiochip_get_data(gc);
  473. struct sgpio_priv *priv = bank->priv;
  474. struct sgpio_port_addr addr;
  475. sgpio_pin_to_addr(priv, gpio, &addr);
  476. return bank->is_input ? sgpio_input_get(priv, &addr) : sgpio_output_get(priv, &addr);
  477. }
  478. static int microchip_sgpio_of_xlate(struct gpio_chip *gc,
  479. const struct of_phandle_args *gpiospec,
  480. u32 *flags)
  481. {
  482. struct sgpio_bank *bank = gpiochip_get_data(gc);
  483. struct sgpio_priv *priv = bank->priv;
  484. int pin;
  485. /*
  486. * Note that the SGIO pin is defined by *2* numbers, a port
  487. * number between 0 and 31, and a bit index, 0 to 3.
  488. */
  489. if (gpiospec->args[0] > SGPIO_BITS_PER_WORD ||
  490. gpiospec->args[1] > priv->bitcount)
  491. return -EINVAL;
  492. pin = sgpio_addr_to_pin(priv, gpiospec->args[0], gpiospec->args[1]);
  493. if (pin > gc->ngpio)
  494. return -EINVAL;
  495. if (flags)
  496. *flags = gpiospec->args[2];
  497. return pin;
  498. }
  499. static int microchip_sgpio_get_ports(struct sgpio_priv *priv)
  500. {
  501. const char *range_property_name = "microchip,sgpio-port-ranges";
  502. struct device *dev = priv->dev;
  503. u32 range_params[64];
  504. int i, nranges, ret;
  505. /* Calculate port mask */
  506. nranges = device_property_count_u32(dev, range_property_name);
  507. if (nranges < 2 || nranges % 2 || nranges > ARRAY_SIZE(range_params)) {
  508. dev_err(dev, "%s port range: '%s' property\n",
  509. nranges == -EINVAL ? "Missing" : "Invalid",
  510. range_property_name);
  511. return -EINVAL;
  512. }
  513. ret = device_property_read_u32_array(dev, range_property_name,
  514. range_params, nranges);
  515. if (ret) {
  516. dev_err(dev, "failed to parse '%s' property: %d\n",
  517. range_property_name, ret);
  518. return ret;
  519. }
  520. for (i = 0; i < nranges; i += 2) {
  521. int start, end;
  522. start = range_params[i];
  523. end = range_params[i + 1];
  524. if (start > end || end >= SGPIO_BITS_PER_WORD) {
  525. dev_err(dev, "Ill-formed port-range [%d:%d]\n",
  526. start, end);
  527. }
  528. priv->ports |= GENMASK(end, start);
  529. }
  530. return 0;
  531. }
  532. static void microchip_sgpio_irq_settype(struct irq_data *data,
  533. int type,
  534. int polarity)
  535. {
  536. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  537. struct sgpio_bank *bank = gpiochip_get_data(chip);
  538. unsigned int gpio = irqd_to_hwirq(data);
  539. struct sgpio_port_addr addr;
  540. unsigned long flags;
  541. u32 ena;
  542. sgpio_pin_to_addr(bank->priv, gpio, &addr);
  543. spin_lock_irqsave(&bank->priv->lock, flags);
  544. /* Disable interrupt while changing type */
  545. ena = sgpio_readl(bank->priv, REG_INT_ENABLE, addr.bit);
  546. sgpio_writel(bank->priv, ena & ~BIT(addr.port), REG_INT_ENABLE, addr.bit);
  547. /* Type value spread over 2 registers sets: low, high bit */
  548. sgpio_clrsetbits(bank->priv, REG_INT_TRIGGER, addr.bit,
  549. BIT(addr.port), (!!(type & 0x1)) << addr.port);
  550. sgpio_clrsetbits(bank->priv, REG_INT_TRIGGER, SGPIO_MAX_BITS + addr.bit,
  551. BIT(addr.port), (!!(type & 0x2)) << addr.port);
  552. if (type == SGPIO_INT_TRG_LEVEL)
  553. sgpio_clrsetbits(bank->priv, REG_INT_POLARITY, addr.bit,
  554. BIT(addr.port), polarity << addr.port);
  555. /* Possibly re-enable interrupts */
  556. sgpio_writel(bank->priv, ena, REG_INT_ENABLE, addr.bit);
  557. spin_unlock_irqrestore(&bank->priv->lock, flags);
  558. }
  559. static void microchip_sgpio_irq_setreg(struct irq_data *data,
  560. int reg,
  561. bool clear)
  562. {
  563. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  564. struct sgpio_bank *bank = gpiochip_get_data(chip);
  565. unsigned int gpio = irqd_to_hwirq(data);
  566. struct sgpio_port_addr addr;
  567. sgpio_pin_to_addr(bank->priv, gpio, &addr);
  568. if (clear)
  569. sgpio_clrsetbits(bank->priv, reg, addr.bit, BIT(addr.port), 0);
  570. else
  571. sgpio_clrsetbits(bank->priv, reg, addr.bit, 0, BIT(addr.port));
  572. }
  573. static void microchip_sgpio_irq_mask(struct irq_data *data)
  574. {
  575. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  576. microchip_sgpio_irq_setreg(data, REG_INT_ENABLE, true);
  577. gpiochip_disable_irq(chip, data->hwirq);
  578. }
  579. static void microchip_sgpio_irq_unmask(struct irq_data *data)
  580. {
  581. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  582. gpiochip_enable_irq(chip, data->hwirq);
  583. microchip_sgpio_irq_setreg(data, REG_INT_ENABLE, false);
  584. }
  585. static void microchip_sgpio_irq_ack(struct irq_data *data)
  586. {
  587. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  588. struct sgpio_bank *bank = gpiochip_get_data(chip);
  589. unsigned int gpio = irqd_to_hwirq(data);
  590. struct sgpio_port_addr addr;
  591. sgpio_pin_to_addr(bank->priv, gpio, &addr);
  592. sgpio_writel(bank->priv, BIT(addr.port), REG_INT_ACK, addr.bit);
  593. }
  594. static int microchip_sgpio_irq_set_type(struct irq_data *data, unsigned int type)
  595. {
  596. switch (type) {
  597. case IRQ_TYPE_EDGE_BOTH:
  598. irq_set_handler_locked(data, handle_edge_irq);
  599. microchip_sgpio_irq_settype(data, SGPIO_INT_TRG_EDGE, 0);
  600. break;
  601. case IRQ_TYPE_EDGE_RISING:
  602. irq_set_handler_locked(data, handle_edge_irq);
  603. microchip_sgpio_irq_settype(data, SGPIO_INT_TRG_EDGE_RISE, 0);
  604. break;
  605. case IRQ_TYPE_EDGE_FALLING:
  606. irq_set_handler_locked(data, handle_edge_irq);
  607. microchip_sgpio_irq_settype(data, SGPIO_INT_TRG_EDGE_FALL, 0);
  608. break;
  609. case IRQ_TYPE_LEVEL_HIGH:
  610. irq_set_handler_locked(data, handle_level_irq);
  611. microchip_sgpio_irq_settype(data, SGPIO_INT_TRG_LEVEL, SGPIO_TRG_LEVEL_HIGH);
  612. break;
  613. case IRQ_TYPE_LEVEL_LOW:
  614. irq_set_handler_locked(data, handle_level_irq);
  615. microchip_sgpio_irq_settype(data, SGPIO_INT_TRG_LEVEL, SGPIO_TRG_LEVEL_LOW);
  616. break;
  617. default:
  618. return -EINVAL;
  619. }
  620. return 0;
  621. }
  622. static const struct irq_chip microchip_sgpio_irqchip = {
  623. .name = "gpio",
  624. .irq_mask = microchip_sgpio_irq_mask,
  625. .irq_ack = microchip_sgpio_irq_ack,
  626. .irq_unmask = microchip_sgpio_irq_unmask,
  627. .irq_set_type = microchip_sgpio_irq_set_type,
  628. .flags = IRQCHIP_IMMUTABLE,
  629. GPIOCHIP_IRQ_RESOURCE_HELPERS,
  630. };
  631. static void sgpio_irq_handler(struct irq_desc *desc)
  632. {
  633. struct irq_chip *parent_chip = irq_desc_get_chip(desc);
  634. struct gpio_chip *chip = irq_desc_get_handler_data(desc);
  635. struct sgpio_bank *bank = gpiochip_get_data(chip);
  636. struct sgpio_priv *priv = bank->priv;
  637. int bit, port, gpio;
  638. long val;
  639. for (bit = 0; bit < priv->bitcount; bit++) {
  640. val = sgpio_readl(priv, REG_INT_IDENT, bit);
  641. if (!val)
  642. continue;
  643. chained_irq_enter(parent_chip, desc);
  644. for_each_set_bit(port, &val, SGPIO_BITS_PER_WORD) {
  645. gpio = sgpio_addr_to_pin(priv, port, bit);
  646. generic_handle_domain_irq(chip->irq.domain, gpio);
  647. }
  648. chained_irq_exit(parent_chip, desc);
  649. }
  650. }
  651. static int microchip_sgpio_register_bank(struct device *dev,
  652. struct sgpio_priv *priv,
  653. struct fwnode_handle *fwnode,
  654. int bankno)
  655. {
  656. struct pinctrl_pin_desc *pins;
  657. struct pinctrl_desc *pctl_desc;
  658. struct pinctrl_dev *pctldev;
  659. struct sgpio_bank *bank;
  660. struct gpio_chip *gc;
  661. u32 ngpios;
  662. int i, ret;
  663. /* Get overall bank struct */
  664. bank = (bankno == 0) ? &priv->in : &priv->out;
  665. bank->priv = priv;
  666. if (fwnode_property_read_u32(fwnode, "ngpios", &ngpios)) {
  667. dev_info(dev, "failed to get number of gpios for bank%d\n",
  668. bankno);
  669. ngpios = 64;
  670. }
  671. priv->bitcount = ngpios / SGPIO_BITS_PER_WORD;
  672. if (priv->bitcount > SGPIO_MAX_BITS) {
  673. dev_err(dev, "Bit width exceeds maximum (%d)\n",
  674. SGPIO_MAX_BITS);
  675. return -EINVAL;
  676. }
  677. pctl_desc = &bank->pctl_desc;
  678. pctl_desc->name = devm_kasprintf(dev, GFP_KERNEL, "%s-%sput",
  679. dev_name(dev),
  680. bank->is_input ? "in" : "out");
  681. if (!pctl_desc->name)
  682. return -ENOMEM;
  683. pctl_desc->pctlops = &sgpio_pctl_ops;
  684. pctl_desc->pmxops = &sgpio_pmx_ops;
  685. pctl_desc->confops = &sgpio_confops;
  686. pctl_desc->owner = THIS_MODULE;
  687. pins = devm_kzalloc(dev, sizeof(*pins)*ngpios, GFP_KERNEL);
  688. if (!pins)
  689. return -ENOMEM;
  690. pctl_desc->npins = ngpios;
  691. pctl_desc->pins = pins;
  692. for (i = 0; i < ngpios; i++) {
  693. struct sgpio_port_addr addr;
  694. sgpio_pin_to_addr(priv, i, &addr);
  695. pins[i].number = i;
  696. pins[i].name = devm_kasprintf(dev, GFP_KERNEL,
  697. "SGPIO_%c_p%db%d",
  698. bank->is_input ? 'I' : 'O',
  699. addr.port, addr.bit);
  700. if (!pins[i].name)
  701. return -ENOMEM;
  702. }
  703. pctldev = devm_pinctrl_register(dev, pctl_desc, bank);
  704. if (IS_ERR(pctldev))
  705. return dev_err_probe(dev, PTR_ERR(pctldev), "Failed to register pinctrl\n");
  706. gc = &bank->gpio;
  707. gc->label = pctl_desc->name;
  708. gc->parent = dev;
  709. gc->fwnode = fwnode;
  710. gc->owner = THIS_MODULE;
  711. gc->get_direction = microchip_sgpio_get_direction;
  712. gc->direction_input = microchip_sgpio_direction_input;
  713. gc->direction_output = microchip_sgpio_direction_output;
  714. gc->get = microchip_sgpio_get_value;
  715. gc->set = microchip_sgpio_set_value;
  716. gc->request = gpiochip_generic_request;
  717. gc->free = gpiochip_generic_free;
  718. gc->of_xlate = microchip_sgpio_of_xlate;
  719. gc->of_gpio_n_cells = 3;
  720. gc->base = -1;
  721. gc->ngpio = ngpios;
  722. gc->can_sleep = !bank->is_input;
  723. if (bank->is_input && priv->properties->flags & SGPIO_FLAGS_HAS_IRQ) {
  724. int irq;
  725. irq = fwnode_irq_get(fwnode, 0);
  726. if (irq > 0) {
  727. struct gpio_irq_chip *girq = &gc->irq;
  728. gpio_irq_chip_set_chip(girq, &microchip_sgpio_irqchip);
  729. girq->parent_handler = sgpio_irq_handler;
  730. girq->num_parents = 1;
  731. girq->parents = devm_kcalloc(dev, 1,
  732. sizeof(*girq->parents),
  733. GFP_KERNEL);
  734. if (!girq->parents)
  735. return -ENOMEM;
  736. girq->parents[0] = irq;
  737. girq->default_type = IRQ_TYPE_NONE;
  738. girq->handler = handle_bad_irq;
  739. /* Disable all individual pins */
  740. for (i = 0; i < SGPIO_MAX_BITS; i++)
  741. sgpio_writel(priv, 0, REG_INT_ENABLE, i);
  742. /* Master enable */
  743. sgpio_clrsetbits(priv, REG_SIO_CONFIG, 0, 0, SGPIO_MASTER_INTR_ENA);
  744. }
  745. }
  746. ret = devm_gpiochip_add_data(dev, gc, bank);
  747. if (ret)
  748. dev_err(dev, "Failed to register: ret %d\n", ret);
  749. return ret;
  750. }
  751. static int microchip_sgpio_probe(struct platform_device *pdev)
  752. {
  753. int div_clock = 0, ret, port, i, nbanks;
  754. struct device *dev = &pdev->dev;
  755. struct fwnode_handle *fwnode;
  756. struct reset_control *reset;
  757. struct sgpio_priv *priv;
  758. struct clk *clk;
  759. u32 val;
  760. struct regmap_config regmap_config = {
  761. .reg_bits = 32,
  762. .val_bits = 32,
  763. .reg_stride = 4,
  764. };
  765. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  766. if (!priv)
  767. return -ENOMEM;
  768. priv->dev = dev;
  769. spin_lock_init(&priv->lock);
  770. mutex_init(&priv->poll_lock);
  771. reset = devm_reset_control_get_optional_shared(&pdev->dev, "switch");
  772. if (IS_ERR(reset))
  773. return dev_err_probe(dev, PTR_ERR(reset), "Failed to get reset\n");
  774. reset_control_reset(reset);
  775. clk = devm_clk_get(dev, NULL);
  776. if (IS_ERR(clk))
  777. return dev_err_probe(dev, PTR_ERR(clk), "Failed to get clock\n");
  778. div_clock = clk_get_rate(clk);
  779. if (device_property_read_u32(dev, "bus-frequency", &priv->clock))
  780. priv->clock = 12500000;
  781. if (priv->clock == 0 || priv->clock > (div_clock / 2)) {
  782. dev_err(dev, "Invalid frequency %d\n", priv->clock);
  783. return -EINVAL;
  784. }
  785. priv->regs = ocelot_regmap_from_resource(pdev, 0, &regmap_config);
  786. if (IS_ERR(priv->regs))
  787. return PTR_ERR(priv->regs);
  788. priv->properties = device_get_match_data(dev);
  789. priv->in.is_input = true;
  790. /* Get rest of device properties */
  791. ret = microchip_sgpio_get_ports(priv);
  792. if (ret)
  793. return ret;
  794. nbanks = device_get_child_node_count(dev);
  795. if (nbanks != 2) {
  796. dev_err(dev, "Must have 2 banks (have %d)\n", nbanks);
  797. return -EINVAL;
  798. }
  799. i = 0;
  800. device_for_each_child_node(dev, fwnode) {
  801. ret = microchip_sgpio_register_bank(dev, priv, fwnode, i++);
  802. if (ret) {
  803. fwnode_handle_put(fwnode);
  804. return ret;
  805. }
  806. }
  807. if (priv->in.gpio.ngpio != priv->out.gpio.ngpio) {
  808. dev_err(dev, "Banks must have same GPIO count\n");
  809. return -ERANGE;
  810. }
  811. sgpio_configure_bitstream(priv);
  812. val = max(2U, div_clock / priv->clock);
  813. sgpio_configure_clock(priv, val);
  814. for (port = 0; port < SGPIO_BITS_PER_WORD; port++)
  815. sgpio_writel(priv, 0, REG_PORT_CONFIG, port);
  816. sgpio_writel(priv, priv->ports, REG_PORT_ENABLE, 0);
  817. return 0;
  818. }
  819. static const struct of_device_id microchip_sgpio_gpio_of_match[] = {
  820. {
  821. .compatible = "microchip,sparx5-sgpio",
  822. .data = &properties_sparx5,
  823. }, {
  824. .compatible = "mscc,luton-sgpio",
  825. .data = &properties_luton,
  826. }, {
  827. .compatible = "mscc,ocelot-sgpio",
  828. .data = &properties_ocelot,
  829. }, {
  830. /* sentinel */
  831. }
  832. };
  833. MODULE_DEVICE_TABLE(of, microchip_sgpio_gpio_of_match);
  834. static struct platform_driver microchip_sgpio_pinctrl_driver = {
  835. .driver = {
  836. .name = "pinctrl-microchip-sgpio",
  837. .of_match_table = microchip_sgpio_gpio_of_match,
  838. .suppress_bind_attrs = true,
  839. },
  840. .probe = microchip_sgpio_probe,
  841. };
  842. module_platform_driver(microchip_sgpio_pinctrl_driver);
  843. MODULE_DESCRIPTION("Microchip SGPIO Pinctrl Driver");
  844. MODULE_LICENSE("GPL");