pinctrl-armada-37xx.c 32 KB

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