pinctrl-armada-37xx.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174
  1. /*
  2. * Marvell 37xx SoC pinctrl driver
  3. *
  4. * Copyright (C) 2017 Marvell
  5. *
  6. * Gregory CLEMENT <gregory.clement@free-electrons.com>
  7. *
  8. * This file is licensed under the terms of the GNU General Public
  9. * License version 2 or later. This program is licensed "as is"
  10. * without any warranty of any kind, whether express or implied.
  11. */
  12. #include <linux/gpio/driver.h>
  13. #include <linux/mfd/syscon.h>
  14. #include <linux/of.h>
  15. #include <linux/of_address.h>
  16. #include <linux/of_device.h>
  17. #include <linux/of_irq.h>
  18. #include <linux/pinctrl/pinconf-generic.h>
  19. #include <linux/pinctrl/pinconf.h>
  20. #include <linux/pinctrl/pinctrl.h>
  21. #include <linux/pinctrl/pinmux.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/regmap.h>
  24. #include <linux/slab.h>
  25. #include "../pinctrl-utils.h"
  26. #define OUTPUT_EN 0x0
  27. #define INPUT_VAL 0x10
  28. #define OUTPUT_VAL 0x18
  29. #define OUTPUT_CTL 0x20
  30. #define SELECTION 0x30
  31. #define IRQ_EN 0x0
  32. #define IRQ_POL 0x08
  33. #define IRQ_STATUS 0x10
  34. #define IRQ_WKUP 0x18
  35. #define NB_FUNCS 3
  36. #define GPIO_PER_REG 32
  37. /**
  38. * struct armada_37xx_pin_group: represents group of pins of a pinmux function.
  39. * The pins of a pinmux groups are composed of one or two groups of contiguous
  40. * pins.
  41. * @name: Name of the pin group, used to lookup the group.
  42. * @start_pins: Index of the first pin of the main range of pins belonging to
  43. * the group
  44. * @npins: Number of pins included in the first range
  45. * @reg_mask: Bit mask matching the group in the selection register
  46. * @extra_pins: Index of the first pin of the optional second range of pins
  47. * belonging to the group
  48. * @npins: Number of pins included in the second optional range
  49. * @funcs: A list of pinmux functions that can be selected for this group.
  50. * @pins: List of the pins included in the group
  51. */
  52. struct armada_37xx_pin_group {
  53. const char *name;
  54. unsigned int start_pin;
  55. unsigned int npins;
  56. u32 reg_mask;
  57. u32 val[NB_FUNCS];
  58. unsigned int extra_pin;
  59. unsigned int extra_npins;
  60. const char *funcs[NB_FUNCS];
  61. unsigned int *pins;
  62. };
  63. struct armada_37xx_pin_data {
  64. u8 nr_pins;
  65. char *name;
  66. struct armada_37xx_pin_group *groups;
  67. int ngroups;
  68. };
  69. struct armada_37xx_pmx_func {
  70. const char *name;
  71. const char **groups;
  72. unsigned int ngroups;
  73. };
  74. struct armada_37xx_pm_state {
  75. u32 out_en_l;
  76. u32 out_en_h;
  77. u32 out_val_l;
  78. u32 out_val_h;
  79. u32 irq_en_l;
  80. u32 irq_en_h;
  81. u32 irq_pol_l;
  82. u32 irq_pol_h;
  83. u32 selection;
  84. };
  85. struct armada_37xx_pinctrl {
  86. struct regmap *regmap;
  87. void __iomem *base;
  88. const struct armada_37xx_pin_data *data;
  89. struct device *dev;
  90. struct gpio_chip gpio_chip;
  91. struct irq_chip irq_chip;
  92. spinlock_t irq_lock;
  93. struct pinctrl_desc pctl;
  94. struct pinctrl_dev *pctl_dev;
  95. struct armada_37xx_pin_group *groups;
  96. unsigned int ngroups;
  97. struct armada_37xx_pmx_func *funcs;
  98. unsigned int nfuncs;
  99. struct armada_37xx_pm_state pm;
  100. };
  101. #define PIN_GRP(_name, _start, _nr, _mask, _func1, _func2) \
  102. { \
  103. .name = _name, \
  104. .start_pin = _start, \
  105. .npins = _nr, \
  106. .reg_mask = _mask, \
  107. .val = {0, _mask}, \
  108. .funcs = {_func1, _func2} \
  109. }
  110. #define PIN_GRP_GPIO(_name, _start, _nr, _mask, _func1) \
  111. { \
  112. .name = _name, \
  113. .start_pin = _start, \
  114. .npins = _nr, \
  115. .reg_mask = _mask, \
  116. .val = {0, _mask}, \
  117. .funcs = {_func1, "gpio"} \
  118. }
  119. #define PIN_GRP_GPIO_2(_name, _start, _nr, _mask, _val1, _val2, _func1) \
  120. { \
  121. .name = _name, \
  122. .start_pin = _start, \
  123. .npins = _nr, \
  124. .reg_mask = _mask, \
  125. .val = {_val1, _val2}, \
  126. .funcs = {_func1, "gpio"} \
  127. }
  128. #define PIN_GRP_GPIO_3(_name, _start, _nr, _mask, _v1, _v2, _v3, _f1, _f2) \
  129. { \
  130. .name = _name, \
  131. .start_pin = _start, \
  132. .npins = _nr, \
  133. .reg_mask = _mask, \
  134. .val = {_v1, _v2, _v3}, \
  135. .funcs = {_f1, _f2, "gpio"} \
  136. }
  137. #define PIN_GRP_EXTRA(_name, _start, _nr, _mask, _v1, _v2, _start2, _nr2, \
  138. _f1, _f2) \
  139. { \
  140. .name = _name, \
  141. .start_pin = _start, \
  142. .npins = _nr, \
  143. .reg_mask = _mask, \
  144. .val = {_v1, _v2}, \
  145. .extra_pin = _start2, \
  146. .extra_npins = _nr2, \
  147. .funcs = {_f1, _f2} \
  148. }
  149. static struct armada_37xx_pin_group armada_37xx_nb_groups[] = {
  150. PIN_GRP_GPIO("jtag", 20, 5, BIT(0), "jtag"),
  151. PIN_GRP_GPIO("sdio0", 8, 3, BIT(1), "sdio"),
  152. PIN_GRP_GPIO("emmc_nb", 27, 9, BIT(2), "emmc"),
  153. PIN_GRP_GPIO("pwm0", 11, 1, BIT(3), "pwm"),
  154. PIN_GRP_GPIO("pwm1", 12, 1, BIT(4), "pwm"),
  155. PIN_GRP_GPIO("pwm2", 13, 1, BIT(5), "pwm"),
  156. PIN_GRP_GPIO("pwm3", 14, 1, BIT(6), "pwm"),
  157. PIN_GRP_GPIO("pmic1", 17, 1, BIT(7), "pmic"),
  158. PIN_GRP_GPIO("pmic0", 16, 1, BIT(8), "pmic"),
  159. PIN_GRP_GPIO("i2c2", 2, 2, BIT(9), "i2c"),
  160. PIN_GRP_GPIO("i2c1", 0, 2, BIT(10), "i2c"),
  161. PIN_GRP_GPIO("spi_cs1", 17, 1, BIT(12), "spi"),
  162. PIN_GRP_GPIO_2("spi_cs2", 18, 1, BIT(13) | BIT(19), 0, BIT(13), "spi"),
  163. PIN_GRP_GPIO_2("spi_cs3", 19, 1, BIT(14) | BIT(19), 0, BIT(14), "spi"),
  164. PIN_GRP_GPIO("onewire", 4, 1, BIT(16), "onewire"),
  165. PIN_GRP_GPIO("uart1", 25, 2, BIT(17), "uart"),
  166. PIN_GRP_GPIO("spi_quad", 15, 2, BIT(18), "spi"),
  167. PIN_GRP_EXTRA("uart2", 9, 2, BIT(1) | BIT(13) | BIT(14) | BIT(19),
  168. BIT(1) | BIT(13) | BIT(14), BIT(1) | BIT(19),
  169. 18, 2, "gpio", "uart"),
  170. PIN_GRP_GPIO_2("led0_od", 11, 1, BIT(20), BIT(20), 0, "led"),
  171. PIN_GRP_GPIO_2("led1_od", 12, 1, BIT(21), BIT(21), 0, "led"),
  172. PIN_GRP_GPIO_2("led2_od", 13, 1, BIT(22), BIT(22), 0, "led"),
  173. PIN_GRP_GPIO_2("led3_od", 14, 1, BIT(23), BIT(23), 0, "led"),
  174. };
  175. static struct armada_37xx_pin_group armada_37xx_sb_groups[] = {
  176. PIN_GRP_GPIO("usb32_drvvbus0", 0, 1, BIT(0), "drvbus"),
  177. PIN_GRP_GPIO("usb2_drvvbus1", 1, 1, BIT(1), "drvbus"),
  178. PIN_GRP_GPIO("sdio_sb", 24, 6, BIT(2), "sdio"),
  179. PIN_GRP_GPIO("rgmii", 6, 12, BIT(3), "mii"),
  180. PIN_GRP_GPIO("pcie1", 3, 2, BIT(4), "pcie"),
  181. PIN_GRP_GPIO("ptp", 20, 3, BIT(5), "ptp"),
  182. PIN_GRP("ptp_clk", 21, 1, BIT(6), "ptp", "mii"),
  183. PIN_GRP("ptp_trig", 22, 1, BIT(7), "ptp", "mii"),
  184. PIN_GRP_GPIO_3("mii_col", 23, 1, BIT(8) | BIT(14), 0, BIT(8), BIT(14),
  185. "mii", "mii_err"),
  186. };
  187. static const struct armada_37xx_pin_data armada_37xx_pin_nb = {
  188. .nr_pins = 36,
  189. .name = "GPIO1",
  190. .groups = armada_37xx_nb_groups,
  191. .ngroups = ARRAY_SIZE(armada_37xx_nb_groups),
  192. };
  193. static const struct armada_37xx_pin_data armada_37xx_pin_sb = {
  194. .nr_pins = 30,
  195. .name = "GPIO2",
  196. .groups = armada_37xx_sb_groups,
  197. .ngroups = ARRAY_SIZE(armada_37xx_sb_groups),
  198. };
  199. static inline void armada_37xx_update_reg(unsigned int *reg,
  200. unsigned int *offset)
  201. {
  202. /* We never have more than 2 registers */
  203. if (*offset >= GPIO_PER_REG) {
  204. *offset -= GPIO_PER_REG;
  205. *reg += sizeof(u32);
  206. }
  207. }
  208. static struct armada_37xx_pin_group *armada_37xx_find_next_grp_by_pin(
  209. struct armada_37xx_pinctrl *info, int pin, int *grp)
  210. {
  211. while (*grp < info->ngroups) {
  212. struct armada_37xx_pin_group *group = &info->groups[*grp];
  213. int j;
  214. *grp = *grp + 1;
  215. for (j = 0; j < (group->npins + group->extra_npins); j++)
  216. if (group->pins[j] == pin)
  217. return group;
  218. }
  219. return NULL;
  220. }
  221. static int armada_37xx_pin_config_group_get(struct pinctrl_dev *pctldev,
  222. unsigned int selector, unsigned long *config)
  223. {
  224. return -ENOTSUPP;
  225. }
  226. static int armada_37xx_pin_config_group_set(struct pinctrl_dev *pctldev,
  227. unsigned int selector, unsigned long *configs,
  228. unsigned int num_configs)
  229. {
  230. return -ENOTSUPP;
  231. }
  232. static const struct pinconf_ops armada_37xx_pinconf_ops = {
  233. .is_generic = true,
  234. .pin_config_group_get = armada_37xx_pin_config_group_get,
  235. .pin_config_group_set = armada_37xx_pin_config_group_set,
  236. };
  237. static int armada_37xx_get_groups_count(struct pinctrl_dev *pctldev)
  238. {
  239. struct armada_37xx_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  240. return info->ngroups;
  241. }
  242. static const char *armada_37xx_get_group_name(struct pinctrl_dev *pctldev,
  243. unsigned int group)
  244. {
  245. struct armada_37xx_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  246. return info->groups[group].name;
  247. }
  248. static int armada_37xx_get_group_pins(struct pinctrl_dev *pctldev,
  249. unsigned int selector,
  250. const unsigned int **pins,
  251. unsigned int *npins)
  252. {
  253. struct armada_37xx_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  254. if (selector >= info->ngroups)
  255. return -EINVAL;
  256. *pins = info->groups[selector].pins;
  257. *npins = info->groups[selector].npins +
  258. info->groups[selector].extra_npins;
  259. return 0;
  260. }
  261. static const struct pinctrl_ops armada_37xx_pctrl_ops = {
  262. .get_groups_count = armada_37xx_get_groups_count,
  263. .get_group_name = armada_37xx_get_group_name,
  264. .get_group_pins = armada_37xx_get_group_pins,
  265. .dt_node_to_map = pinconf_generic_dt_node_to_map_group,
  266. .dt_free_map = pinctrl_utils_free_map,
  267. };
  268. /*
  269. * Pinmux_ops handling
  270. */
  271. static int armada_37xx_pmx_get_funcs_count(struct pinctrl_dev *pctldev)
  272. {
  273. struct armada_37xx_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  274. return info->nfuncs;
  275. }
  276. static const char *armada_37xx_pmx_get_func_name(struct pinctrl_dev *pctldev,
  277. unsigned int selector)
  278. {
  279. struct armada_37xx_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  280. return info->funcs[selector].name;
  281. }
  282. static int armada_37xx_pmx_get_groups(struct pinctrl_dev *pctldev,
  283. unsigned int selector,
  284. const char * const **groups,
  285. unsigned int * const num_groups)
  286. {
  287. struct armada_37xx_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  288. *groups = info->funcs[selector].groups;
  289. *num_groups = info->funcs[selector].ngroups;
  290. return 0;
  291. }
  292. static int armada_37xx_pmx_set_by_name(struct pinctrl_dev *pctldev,
  293. const char *name,
  294. struct armada_37xx_pin_group *grp)
  295. {
  296. struct armada_37xx_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  297. unsigned int reg = SELECTION;
  298. unsigned int mask = grp->reg_mask;
  299. int func, val;
  300. dev_dbg(info->dev, "enable function %s group %s\n",
  301. name, grp->name);
  302. func = match_string(grp->funcs, NB_FUNCS, name);
  303. if (func < 0)
  304. return -ENOTSUPP;
  305. val = grp->val[func];
  306. regmap_update_bits(info->regmap, reg, mask, val);
  307. return 0;
  308. }
  309. static int armada_37xx_pmx_set(struct pinctrl_dev *pctldev,
  310. unsigned int selector,
  311. unsigned int group)
  312. {
  313. struct armada_37xx_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  314. struct armada_37xx_pin_group *grp = &info->groups[group];
  315. const char *name = info->funcs[selector].name;
  316. return armada_37xx_pmx_set_by_name(pctldev, name, grp);
  317. }
  318. static inline void armada_37xx_irq_update_reg(unsigned int *reg,
  319. struct irq_data *d)
  320. {
  321. int offset = irqd_to_hwirq(d);
  322. armada_37xx_update_reg(reg, &offset);
  323. }
  324. static int armada_37xx_gpio_direction_input(struct gpio_chip *chip,
  325. unsigned int offset)
  326. {
  327. struct armada_37xx_pinctrl *info = gpiochip_get_data(chip);
  328. unsigned int reg = OUTPUT_EN;
  329. unsigned int mask;
  330. armada_37xx_update_reg(&reg, &offset);
  331. mask = BIT(offset);
  332. return regmap_update_bits(info->regmap, reg, mask, 0);
  333. }
  334. static int armada_37xx_gpio_get_direction(struct gpio_chip *chip,
  335. unsigned int offset)
  336. {
  337. struct armada_37xx_pinctrl *info = gpiochip_get_data(chip);
  338. unsigned int reg = OUTPUT_EN;
  339. unsigned int val, mask;
  340. armada_37xx_update_reg(&reg, &offset);
  341. mask = BIT(offset);
  342. regmap_read(info->regmap, reg, &val);
  343. return !(val & mask);
  344. }
  345. static int armada_37xx_gpio_direction_output(struct gpio_chip *chip,
  346. unsigned int offset, int value)
  347. {
  348. struct armada_37xx_pinctrl *info = gpiochip_get_data(chip);
  349. unsigned int reg = OUTPUT_EN;
  350. unsigned int mask, val, ret;
  351. armada_37xx_update_reg(&reg, &offset);
  352. mask = BIT(offset);
  353. ret = regmap_update_bits(info->regmap, reg, mask, mask);
  354. if (ret)
  355. return ret;
  356. reg = OUTPUT_VAL;
  357. val = value ? mask : 0;
  358. regmap_update_bits(info->regmap, reg, mask, val);
  359. return 0;
  360. }
  361. static int armada_37xx_gpio_get(struct gpio_chip *chip, unsigned int offset)
  362. {
  363. struct armada_37xx_pinctrl *info = gpiochip_get_data(chip);
  364. unsigned int reg = INPUT_VAL;
  365. unsigned int val, mask;
  366. armada_37xx_update_reg(&reg, &offset);
  367. mask = BIT(offset);
  368. regmap_read(info->regmap, reg, &val);
  369. return (val & mask) != 0;
  370. }
  371. static void armada_37xx_gpio_set(struct gpio_chip *chip, unsigned int offset,
  372. int value)
  373. {
  374. struct armada_37xx_pinctrl *info = gpiochip_get_data(chip);
  375. unsigned int reg = OUTPUT_VAL;
  376. unsigned int mask, val;
  377. armada_37xx_update_reg(&reg, &offset);
  378. mask = BIT(offset);
  379. val = value ? mask : 0;
  380. regmap_update_bits(info->regmap, reg, mask, val);
  381. }
  382. static int armada_37xx_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
  383. struct pinctrl_gpio_range *range,
  384. unsigned int offset, bool input)
  385. {
  386. struct armada_37xx_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  387. struct gpio_chip *chip = range->gc;
  388. dev_dbg(info->dev, "gpio_direction for pin %u as %s-%d to %s\n",
  389. offset, range->name, offset, input ? "input" : "output");
  390. if (input)
  391. armada_37xx_gpio_direction_input(chip, offset);
  392. else
  393. armada_37xx_gpio_direction_output(chip, offset, 0);
  394. return 0;
  395. }
  396. static int armada_37xx_gpio_request_enable(struct pinctrl_dev *pctldev,
  397. struct pinctrl_gpio_range *range,
  398. unsigned int offset)
  399. {
  400. struct armada_37xx_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  401. struct armada_37xx_pin_group *group;
  402. int grp = 0;
  403. dev_dbg(info->dev, "requesting gpio %d\n", offset);
  404. while ((group = armada_37xx_find_next_grp_by_pin(info, offset, &grp)))
  405. armada_37xx_pmx_set_by_name(pctldev, "gpio", group);
  406. return 0;
  407. }
  408. static const struct pinmux_ops armada_37xx_pmx_ops = {
  409. .get_functions_count = armada_37xx_pmx_get_funcs_count,
  410. .get_function_name = armada_37xx_pmx_get_func_name,
  411. .get_function_groups = armada_37xx_pmx_get_groups,
  412. .set_mux = armada_37xx_pmx_set,
  413. .gpio_request_enable = armada_37xx_gpio_request_enable,
  414. .gpio_set_direction = armada_37xx_pmx_gpio_set_direction,
  415. };
  416. static const struct gpio_chip armada_37xx_gpiolib_chip = {
  417. .request = gpiochip_generic_request,
  418. .free = gpiochip_generic_free,
  419. .set = armada_37xx_gpio_set,
  420. .get = armada_37xx_gpio_get,
  421. .get_direction = armada_37xx_gpio_get_direction,
  422. .direction_input = armada_37xx_gpio_direction_input,
  423. .direction_output = armada_37xx_gpio_direction_output,
  424. .owner = THIS_MODULE,
  425. };
  426. static void armada_37xx_irq_ack(struct irq_data *d)
  427. {
  428. struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
  429. struct armada_37xx_pinctrl *info = gpiochip_get_data(chip);
  430. u32 reg = IRQ_STATUS;
  431. unsigned long flags;
  432. armada_37xx_irq_update_reg(&reg, d);
  433. spin_lock_irqsave(&info->irq_lock, flags);
  434. writel(d->mask, info->base + reg);
  435. spin_unlock_irqrestore(&info->irq_lock, flags);
  436. }
  437. static void armada_37xx_irq_mask(struct irq_data *d)
  438. {
  439. struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
  440. struct armada_37xx_pinctrl *info = gpiochip_get_data(chip);
  441. u32 val, reg = IRQ_EN;
  442. unsigned long flags;
  443. armada_37xx_irq_update_reg(&reg, d);
  444. spin_lock_irqsave(&info->irq_lock, flags);
  445. val = readl(info->base + reg);
  446. writel(val & ~d->mask, info->base + reg);
  447. spin_unlock_irqrestore(&info->irq_lock, flags);
  448. }
  449. static void armada_37xx_irq_unmask(struct irq_data *d)
  450. {
  451. struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
  452. struct armada_37xx_pinctrl *info = gpiochip_get_data(chip);
  453. u32 val, reg = IRQ_EN;
  454. unsigned long flags;
  455. armada_37xx_irq_update_reg(&reg, d);
  456. spin_lock_irqsave(&info->irq_lock, flags);
  457. val = readl(info->base + reg);
  458. writel(val | d->mask, info->base + reg);
  459. spin_unlock_irqrestore(&info->irq_lock, flags);
  460. }
  461. static int armada_37xx_irq_set_wake(struct irq_data *d, unsigned int on)
  462. {
  463. struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
  464. struct armada_37xx_pinctrl *info = gpiochip_get_data(chip);
  465. u32 val, reg = IRQ_WKUP;
  466. unsigned long flags;
  467. armada_37xx_irq_update_reg(&reg, d);
  468. spin_lock_irqsave(&info->irq_lock, flags);
  469. val = readl(info->base + reg);
  470. if (on)
  471. val |= (BIT(d->hwirq % GPIO_PER_REG));
  472. else
  473. val &= ~(BIT(d->hwirq % GPIO_PER_REG));
  474. writel(val, info->base + reg);
  475. spin_unlock_irqrestore(&info->irq_lock, flags);
  476. return 0;
  477. }
  478. static int armada_37xx_irq_set_type(struct irq_data *d, unsigned int type)
  479. {
  480. struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
  481. struct armada_37xx_pinctrl *info = gpiochip_get_data(chip);
  482. u32 val, reg = IRQ_POL;
  483. unsigned long flags;
  484. spin_lock_irqsave(&info->irq_lock, flags);
  485. armada_37xx_irq_update_reg(&reg, d);
  486. val = readl(info->base + reg);
  487. switch (type) {
  488. case IRQ_TYPE_EDGE_RISING:
  489. val &= ~(BIT(d->hwirq % GPIO_PER_REG));
  490. break;
  491. case IRQ_TYPE_EDGE_FALLING:
  492. val |= (BIT(d->hwirq % GPIO_PER_REG));
  493. break;
  494. case IRQ_TYPE_EDGE_BOTH: {
  495. u32 in_val, in_reg = INPUT_VAL;
  496. armada_37xx_irq_update_reg(&in_reg, d);
  497. regmap_read(info->regmap, in_reg, &in_val);
  498. /* Set initial polarity based on current input level. */
  499. if (in_val & BIT(d->hwirq % GPIO_PER_REG))
  500. val |= BIT(d->hwirq % GPIO_PER_REG); /* falling */
  501. else
  502. val &= ~(BIT(d->hwirq % GPIO_PER_REG)); /* rising */
  503. break;
  504. }
  505. default:
  506. spin_unlock_irqrestore(&info->irq_lock, flags);
  507. return -EINVAL;
  508. }
  509. writel(val, info->base + reg);
  510. spin_unlock_irqrestore(&info->irq_lock, flags);
  511. return 0;
  512. }
  513. static int armada_37xx_edge_both_irq_swap_pol(struct armada_37xx_pinctrl *info,
  514. u32 pin_idx)
  515. {
  516. u32 reg_idx = pin_idx / GPIO_PER_REG;
  517. u32 bit_num = pin_idx % GPIO_PER_REG;
  518. u32 p, l, ret;
  519. unsigned long flags;
  520. regmap_read(info->regmap, INPUT_VAL + 4*reg_idx, &l);
  521. spin_lock_irqsave(&info->irq_lock, flags);
  522. p = readl(info->base + IRQ_POL + 4 * reg_idx);
  523. if ((p ^ l) & (1 << bit_num)) {
  524. /*
  525. * For the gpios which are used for both-edge irqs, when their
  526. * interrupts happen, their input levels are changed,
  527. * yet their interrupt polarities are kept in old values, we
  528. * should synchronize their interrupt polarities; for example,
  529. * at first a gpio's input level is low and its interrupt
  530. * polarity control is "Detect rising edge", then the gpio has
  531. * a interrupt , its level turns to high, we should change its
  532. * polarity control to "Detect falling edge" correspondingly.
  533. */
  534. p ^= 1 << bit_num;
  535. writel(p, info->base + IRQ_POL + 4 * reg_idx);
  536. ret = 0;
  537. } else {
  538. /* Spurious irq */
  539. ret = -1;
  540. }
  541. spin_unlock_irqrestore(&info->irq_lock, flags);
  542. return ret;
  543. }
  544. static void armada_37xx_irq_handler(struct irq_desc *desc)
  545. {
  546. struct gpio_chip *gc = irq_desc_get_handler_data(desc);
  547. struct irq_chip *chip = irq_desc_get_chip(desc);
  548. struct armada_37xx_pinctrl *info = gpiochip_get_data(gc);
  549. struct irq_domain *d = gc->irq.domain;
  550. int i;
  551. chained_irq_enter(chip, desc);
  552. for (i = 0; i <= d->revmap_size / GPIO_PER_REG; i++) {
  553. u32 status;
  554. unsigned long flags;
  555. spin_lock_irqsave(&info->irq_lock, flags);
  556. status = readl_relaxed(info->base + IRQ_STATUS + 4 * i);
  557. /* Manage only the interrupt that was enabled */
  558. status &= readl_relaxed(info->base + IRQ_EN + 4 * i);
  559. spin_unlock_irqrestore(&info->irq_lock, flags);
  560. while (status) {
  561. u32 hwirq = ffs(status) - 1;
  562. u32 virq = irq_find_mapping(d, hwirq +
  563. i * GPIO_PER_REG);
  564. u32 t = irq_get_trigger_type(virq);
  565. if ((t & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH) {
  566. /* Swap polarity (race with GPIO line) */
  567. if (armada_37xx_edge_both_irq_swap_pol(info,
  568. hwirq + i * GPIO_PER_REG)) {
  569. /*
  570. * For spurious irq, which gpio level
  571. * is not as expected after incoming
  572. * edge, just ack the gpio irq.
  573. */
  574. writel(1 << hwirq,
  575. info->base +
  576. IRQ_STATUS + 4 * i);
  577. goto update_status;
  578. }
  579. }
  580. generic_handle_irq(virq);
  581. update_status:
  582. /* Update status in case a new IRQ appears */
  583. spin_lock_irqsave(&info->irq_lock, flags);
  584. status = readl_relaxed(info->base +
  585. IRQ_STATUS + 4 * i);
  586. /* Manage only the interrupt that was enabled */
  587. status &= readl_relaxed(info->base + IRQ_EN + 4 * i);
  588. spin_unlock_irqrestore(&info->irq_lock, flags);
  589. }
  590. }
  591. chained_irq_exit(chip, desc);
  592. }
  593. static unsigned int armada_37xx_irq_startup(struct irq_data *d)
  594. {
  595. /*
  596. * The mask field is a "precomputed bitmask for accessing the
  597. * chip registers" which was introduced for the generic
  598. * irqchip framework. As we don't use this framework, we can
  599. * reuse this field for our own usage.
  600. */
  601. d->mask = BIT(d->hwirq % GPIO_PER_REG);
  602. armada_37xx_irq_unmask(d);
  603. return 0;
  604. }
  605. static int armada_37xx_irqchip_register(struct platform_device *pdev,
  606. struct armada_37xx_pinctrl *info)
  607. {
  608. struct device_node *np = info->dev->of_node;
  609. struct gpio_chip *gc = &info->gpio_chip;
  610. struct irq_chip *irqchip = &info->irq_chip;
  611. struct resource res;
  612. int ret = -ENODEV, i, nr_irq_parent;
  613. /* Check if we have at least one gpio-controller child node */
  614. for_each_child_of_node(info->dev->of_node, np) {
  615. if (of_property_read_bool(np, "gpio-controller")) {
  616. ret = 0;
  617. break;
  618. }
  619. };
  620. if (ret)
  621. return ret;
  622. nr_irq_parent = of_irq_count(np);
  623. spin_lock_init(&info->irq_lock);
  624. if (!nr_irq_parent) {
  625. dev_err(&pdev->dev, "Invalid or no IRQ\n");
  626. return 0;
  627. }
  628. if (of_address_to_resource(info->dev->of_node, 1, &res)) {
  629. dev_err(info->dev, "cannot find IO resource\n");
  630. return -ENOENT;
  631. }
  632. info->base = devm_ioremap_resource(info->dev, &res);
  633. if (IS_ERR(info->base))
  634. return PTR_ERR(info->base);
  635. irqchip->irq_ack = armada_37xx_irq_ack;
  636. irqchip->irq_mask = armada_37xx_irq_mask;
  637. irqchip->irq_unmask = armada_37xx_irq_unmask;
  638. irqchip->irq_set_wake = armada_37xx_irq_set_wake;
  639. irqchip->irq_set_type = armada_37xx_irq_set_type;
  640. irqchip->irq_startup = armada_37xx_irq_startup;
  641. irqchip->name = info->data->name;
  642. ret = gpiochip_irqchip_add(gc, irqchip, 0,
  643. handle_edge_irq, IRQ_TYPE_NONE);
  644. if (ret) {
  645. dev_info(&pdev->dev, "could not add irqchip\n");
  646. return ret;
  647. }
  648. /*
  649. * Many interrupts are connected to the parent interrupt
  650. * controller. But we do not take advantage of this and use
  651. * the chained irq with all of them.
  652. */
  653. for (i = 0; i < nr_irq_parent; i++) {
  654. int irq = irq_of_parse_and_map(np, i);
  655. if (irq < 0)
  656. continue;
  657. gpiochip_set_chained_irqchip(gc, irqchip, irq,
  658. armada_37xx_irq_handler);
  659. }
  660. return 0;
  661. }
  662. static int armada_37xx_gpiochip_register(struct platform_device *pdev,
  663. struct armada_37xx_pinctrl *info)
  664. {
  665. struct device_node *np;
  666. struct gpio_chip *gc;
  667. int ret = -ENODEV;
  668. for_each_child_of_node(info->dev->of_node, np) {
  669. if (of_find_property(np, "gpio-controller", NULL)) {
  670. ret = 0;
  671. break;
  672. }
  673. };
  674. if (ret)
  675. return ret;
  676. info->gpio_chip = armada_37xx_gpiolib_chip;
  677. gc = &info->gpio_chip;
  678. gc->ngpio = info->data->nr_pins;
  679. gc->parent = &pdev->dev;
  680. gc->base = -1;
  681. gc->of_node = np;
  682. gc->label = info->data->name;
  683. ret = devm_gpiochip_add_data(&pdev->dev, gc, info);
  684. if (ret)
  685. return ret;
  686. ret = armada_37xx_irqchip_register(pdev, info);
  687. if (ret)
  688. return ret;
  689. return 0;
  690. }
  691. /**
  692. * armada_37xx_add_function() - Add a new function to the list
  693. * @funcs: array of function to add the new one
  694. * @funcsize: size of the remaining space for the function
  695. * @name: name of the function to add
  696. *
  697. * If it is a new function then create it by adding its name else
  698. * increment the number of group associated to this function.
  699. */
  700. static int armada_37xx_add_function(struct armada_37xx_pmx_func *funcs,
  701. int *funcsize, const char *name)
  702. {
  703. int i = 0;
  704. if (*funcsize <= 0)
  705. return -EOVERFLOW;
  706. while (funcs->ngroups) {
  707. /* function already there */
  708. if (strcmp(funcs->name, name) == 0) {
  709. funcs->ngroups++;
  710. return -EEXIST;
  711. }
  712. funcs++;
  713. i++;
  714. }
  715. /* append new unique function */
  716. funcs->name = name;
  717. funcs->ngroups = 1;
  718. (*funcsize)--;
  719. return 0;
  720. }
  721. /**
  722. * armada_37xx_fill_group() - complete the group array
  723. * @info: info driver instance
  724. *
  725. * Based on the data available from the armada_37xx_pin_group array
  726. * completes the last member of the struct for each function: the list
  727. * of the groups associated to this function.
  728. *
  729. */
  730. static int armada_37xx_fill_group(struct armada_37xx_pinctrl *info)
  731. {
  732. int n, num = 0, funcsize = info->data->nr_pins;
  733. for (n = 0; n < info->ngroups; n++) {
  734. struct armada_37xx_pin_group *grp = &info->groups[n];
  735. int i, j, f;
  736. grp->pins = devm_kcalloc(info->dev,
  737. grp->npins + grp->extra_npins,
  738. sizeof(*grp->pins),
  739. GFP_KERNEL);
  740. if (!grp->pins)
  741. return -ENOMEM;
  742. for (i = 0; i < grp->npins; i++)
  743. grp->pins[i] = grp->start_pin + i;
  744. for (j = 0; j < grp->extra_npins; j++)
  745. grp->pins[i+j] = grp->extra_pin + j;
  746. for (f = 0; (f < NB_FUNCS) && grp->funcs[f]; f++) {
  747. int ret;
  748. /* check for unique functions and count groups */
  749. ret = armada_37xx_add_function(info->funcs, &funcsize,
  750. grp->funcs[f]);
  751. if (ret == -EOVERFLOW)
  752. dev_err(info->dev,
  753. "More functions than pins(%d)\n",
  754. info->data->nr_pins);
  755. if (ret < 0)
  756. continue;
  757. num++;
  758. }
  759. }
  760. info->nfuncs = num;
  761. return 0;
  762. }
  763. /**
  764. * armada_37xx_fill_funcs() - complete the funcs array
  765. * @info: info driver instance
  766. *
  767. * Based on the data available from the armada_37xx_pin_group array
  768. * completes the last two member of the struct for each group:
  769. * - the list of the pins included in the group
  770. * - the list of pinmux functions that can be selected for this group
  771. *
  772. */
  773. static int armada_37xx_fill_func(struct armada_37xx_pinctrl *info)
  774. {
  775. struct armada_37xx_pmx_func *funcs = info->funcs;
  776. int n;
  777. for (n = 0; n < info->nfuncs; n++) {
  778. const char *name = funcs[n].name;
  779. const char **groups;
  780. int g;
  781. funcs[n].groups = devm_kcalloc(info->dev,
  782. funcs[n].ngroups,
  783. sizeof(*(funcs[n].groups)),
  784. GFP_KERNEL);
  785. if (!funcs[n].groups)
  786. return -ENOMEM;
  787. groups = funcs[n].groups;
  788. for (g = 0; g < info->ngroups; g++) {
  789. struct armada_37xx_pin_group *gp = &info->groups[g];
  790. int f;
  791. f = match_string(gp->funcs, NB_FUNCS, name);
  792. if (f < 0)
  793. continue;
  794. *groups = gp->name;
  795. groups++;
  796. }
  797. }
  798. return 0;
  799. }
  800. static int armada_37xx_pinctrl_register(struct platform_device *pdev,
  801. struct armada_37xx_pinctrl *info)
  802. {
  803. const struct armada_37xx_pin_data *pin_data = info->data;
  804. struct pinctrl_desc *ctrldesc = &info->pctl;
  805. struct pinctrl_pin_desc *pindesc, *pdesc;
  806. int pin, ret;
  807. info->groups = pin_data->groups;
  808. info->ngroups = pin_data->ngroups;
  809. ctrldesc->name = "armada_37xx-pinctrl";
  810. ctrldesc->owner = THIS_MODULE;
  811. ctrldesc->pctlops = &armada_37xx_pctrl_ops;
  812. ctrldesc->pmxops = &armada_37xx_pmx_ops;
  813. ctrldesc->confops = &armada_37xx_pinconf_ops;
  814. pindesc = devm_kcalloc(&pdev->dev,
  815. pin_data->nr_pins, sizeof(*pindesc),
  816. GFP_KERNEL);
  817. if (!pindesc)
  818. return -ENOMEM;
  819. ctrldesc->pins = pindesc;
  820. ctrldesc->npins = pin_data->nr_pins;
  821. pdesc = pindesc;
  822. for (pin = 0; pin < pin_data->nr_pins; pin++) {
  823. pdesc->number = pin;
  824. pdesc->name = kasprintf(GFP_KERNEL, "%s-%d",
  825. pin_data->name, pin);
  826. pdesc++;
  827. }
  828. /*
  829. * we allocate functions for number of pins and hope there are
  830. * fewer unique functions than pins available
  831. */
  832. info->funcs = devm_kcalloc(&pdev->dev,
  833. pin_data->nr_pins,
  834. sizeof(struct armada_37xx_pmx_func),
  835. GFP_KERNEL);
  836. if (!info->funcs)
  837. return -ENOMEM;
  838. ret = armada_37xx_fill_group(info);
  839. if (ret)
  840. return ret;
  841. ret = armada_37xx_fill_func(info);
  842. if (ret)
  843. return ret;
  844. info->pctl_dev = devm_pinctrl_register(&pdev->dev, ctrldesc, info);
  845. if (IS_ERR(info->pctl_dev)) {
  846. dev_err(&pdev->dev, "could not register pinctrl driver\n");
  847. return PTR_ERR(info->pctl_dev);
  848. }
  849. return 0;
  850. }
  851. #if defined(CONFIG_PM)
  852. static int armada_3700_pinctrl_suspend(struct device *dev)
  853. {
  854. struct armada_37xx_pinctrl *info = dev_get_drvdata(dev);
  855. /* Save GPIO state */
  856. regmap_read(info->regmap, OUTPUT_EN, &info->pm.out_en_l);
  857. regmap_read(info->regmap, OUTPUT_EN + sizeof(u32), &info->pm.out_en_h);
  858. regmap_read(info->regmap, OUTPUT_VAL, &info->pm.out_val_l);
  859. regmap_read(info->regmap, OUTPUT_VAL + sizeof(u32),
  860. &info->pm.out_val_h);
  861. info->pm.irq_en_l = readl(info->base + IRQ_EN);
  862. info->pm.irq_en_h = readl(info->base + IRQ_EN + sizeof(u32));
  863. info->pm.irq_pol_l = readl(info->base + IRQ_POL);
  864. info->pm.irq_pol_h = readl(info->base + IRQ_POL + sizeof(u32));
  865. /* Save pinctrl state */
  866. regmap_read(info->regmap, SELECTION, &info->pm.selection);
  867. return 0;
  868. }
  869. static int armada_3700_pinctrl_resume(struct device *dev)
  870. {
  871. struct armada_37xx_pinctrl *info = dev_get_drvdata(dev);
  872. struct gpio_chip *gc;
  873. struct irq_domain *d;
  874. int i;
  875. /* Restore GPIO state */
  876. regmap_write(info->regmap, OUTPUT_EN, info->pm.out_en_l);
  877. regmap_write(info->regmap, OUTPUT_EN + sizeof(u32),
  878. info->pm.out_en_h);
  879. regmap_write(info->regmap, OUTPUT_VAL, info->pm.out_val_l);
  880. regmap_write(info->regmap, OUTPUT_VAL + sizeof(u32),
  881. info->pm.out_val_h);
  882. /*
  883. * Input levels may change during suspend, which is not monitored at
  884. * that time. GPIOs used for both-edge IRQs may not be synchronized
  885. * anymore with their polarities (rising/falling edge) and must be
  886. * re-configured manually.
  887. */
  888. gc = &info->gpio_chip;
  889. d = gc->irq.domain;
  890. for (i = 0; i < gc->ngpio; i++) {
  891. u32 irq_bit = BIT(i % GPIO_PER_REG);
  892. u32 mask, *irq_pol, input_reg, virq, type, level;
  893. if (i < GPIO_PER_REG) {
  894. mask = info->pm.irq_en_l;
  895. irq_pol = &info->pm.irq_pol_l;
  896. input_reg = INPUT_VAL;
  897. } else {
  898. mask = info->pm.irq_en_h;
  899. irq_pol = &info->pm.irq_pol_h;
  900. input_reg = INPUT_VAL + sizeof(u32);
  901. }
  902. if (!(mask & irq_bit))
  903. continue;
  904. virq = irq_find_mapping(d, i);
  905. type = irq_get_trigger_type(virq);
  906. /*
  907. * Synchronize level and polarity for both-edge irqs:
  908. * - a high input level expects a falling edge,
  909. * - a low input level exepects a rising edge.
  910. */
  911. if ((type & IRQ_TYPE_SENSE_MASK) ==
  912. IRQ_TYPE_EDGE_BOTH) {
  913. regmap_read(info->regmap, input_reg, &level);
  914. if ((*irq_pol ^ level) & irq_bit)
  915. *irq_pol ^= irq_bit;
  916. }
  917. }
  918. writel(info->pm.irq_en_l, info->base + IRQ_EN);
  919. writel(info->pm.irq_en_h, info->base + IRQ_EN + sizeof(u32));
  920. writel(info->pm.irq_pol_l, info->base + IRQ_POL);
  921. writel(info->pm.irq_pol_h, info->base + IRQ_POL + sizeof(u32));
  922. /* Restore pinctrl state */
  923. regmap_write(info->regmap, SELECTION, info->pm.selection);
  924. return 0;
  925. }
  926. /*
  927. * Since pinctrl is an infrastructure module, its resume should be issued prior
  928. * to other IO drivers.
  929. */
  930. static const struct dev_pm_ops armada_3700_pinctrl_pm_ops = {
  931. .suspend_late = armada_3700_pinctrl_suspend,
  932. .resume_early = armada_3700_pinctrl_resume,
  933. };
  934. #define PINCTRL_ARMADA_37XX_DEV_PM_OPS (&armada_3700_pinctrl_pm_ops)
  935. #else
  936. #define PINCTRL_ARMADA_37XX_DEV_PM_OPS NULL
  937. #endif /* CONFIG_PM */
  938. static const struct of_device_id armada_37xx_pinctrl_of_match[] = {
  939. {
  940. .compatible = "marvell,armada3710-sb-pinctrl",
  941. .data = &armada_37xx_pin_sb,
  942. },
  943. {
  944. .compatible = "marvell,armada3710-nb-pinctrl",
  945. .data = &armada_37xx_pin_nb,
  946. },
  947. { },
  948. };
  949. static int __init armada_37xx_pinctrl_probe(struct platform_device *pdev)
  950. {
  951. struct armada_37xx_pinctrl *info;
  952. struct device *dev = &pdev->dev;
  953. struct device_node *np = dev->of_node;
  954. struct regmap *regmap;
  955. int ret;
  956. info = devm_kzalloc(dev, sizeof(struct armada_37xx_pinctrl),
  957. GFP_KERNEL);
  958. if (!info)
  959. return -ENOMEM;
  960. info->dev = dev;
  961. regmap = syscon_node_to_regmap(np);
  962. if (IS_ERR(regmap)) {
  963. dev_err(&pdev->dev, "cannot get regmap\n");
  964. return PTR_ERR(regmap);
  965. }
  966. info->regmap = regmap;
  967. info->data = of_device_get_match_data(dev);
  968. ret = armada_37xx_pinctrl_register(pdev, info);
  969. if (ret)
  970. return ret;
  971. ret = armada_37xx_gpiochip_register(pdev, info);
  972. if (ret)
  973. return ret;
  974. platform_set_drvdata(pdev, info);
  975. return 0;
  976. }
  977. static struct platform_driver armada_37xx_pinctrl_driver = {
  978. .driver = {
  979. .name = "armada-37xx-pinctrl",
  980. .of_match_table = armada_37xx_pinctrl_of_match,
  981. .pm = PINCTRL_ARMADA_37XX_DEV_PM_OPS,
  982. },
  983. };
  984. builtin_platform_driver_probe(armada_37xx_pinctrl_driver,
  985. armada_37xx_pinctrl_probe);