pinctrl-stmfx.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Driver for STMicroelectronics Multi-Function eXpander (STMFX) GPIO expander
  4. *
  5. * Copyright (C) 2019 STMicroelectronics
  6. * Author(s): Amelie Delaunay <amelie.delaunay@st.com>.
  7. */
  8. #include <linux/gpio/driver.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/mfd/stmfx.h>
  11. #include <linux/module.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/seq_file.h>
  14. #include <linux/string_choices.h>
  15. #include <linux/pinctrl/pinconf.h>
  16. #include <linux/pinctrl/pinmux.h>
  17. #include "core.h"
  18. #include "pinctrl-utils.h"
  19. /* GPIOs expander */
  20. /* GPIO_STATE1 0x10, GPIO_STATE2 0x11, GPIO_STATE3 0x12 */
  21. #define STMFX_REG_GPIO_STATE STMFX_REG_GPIO_STATE1 /* R */
  22. /* GPIO_DIR1 0x60, GPIO_DIR2 0x61, GPIO_DIR3 0x63 */
  23. #define STMFX_REG_GPIO_DIR STMFX_REG_GPIO_DIR1 /* RW */
  24. /* GPIO_TYPE1 0x64, GPIO_TYPE2 0x65, GPIO_TYPE3 0x66 */
  25. #define STMFX_REG_GPIO_TYPE STMFX_REG_GPIO_TYPE1 /* RW */
  26. /* GPIO_PUPD1 0x68, GPIO_PUPD2 0x69, GPIO_PUPD3 0x6A */
  27. #define STMFX_REG_GPIO_PUPD STMFX_REG_GPIO_PUPD1 /* RW */
  28. /* GPO_SET1 0x6C, GPO_SET2 0x6D, GPO_SET3 0x6E */
  29. #define STMFX_REG_GPO_SET STMFX_REG_GPO_SET1 /* RW */
  30. /* GPO_CLR1 0x70, GPO_CLR2 0x71, GPO_CLR3 0x72 */
  31. #define STMFX_REG_GPO_CLR STMFX_REG_GPO_CLR1 /* RW */
  32. /* IRQ_GPI_SRC1 0x48, IRQ_GPI_SRC2 0x49, IRQ_GPI_SRC3 0x4A */
  33. #define STMFX_REG_IRQ_GPI_SRC STMFX_REG_IRQ_GPI_SRC1 /* RW */
  34. /* IRQ_GPI_EVT1 0x4C, IRQ_GPI_EVT2 0x4D, IRQ_GPI_EVT3 0x4E */
  35. #define STMFX_REG_IRQ_GPI_EVT STMFX_REG_IRQ_GPI_EVT1 /* RW */
  36. /* IRQ_GPI_TYPE1 0x50, IRQ_GPI_TYPE2 0x51, IRQ_GPI_TYPE3 0x52 */
  37. #define STMFX_REG_IRQ_GPI_TYPE STMFX_REG_IRQ_GPI_TYPE1 /* RW */
  38. /* IRQ_GPI_PENDING1 0x0C, IRQ_GPI_PENDING2 0x0D, IRQ_GPI_PENDING3 0x0E*/
  39. #define STMFX_REG_IRQ_GPI_PENDING STMFX_REG_IRQ_GPI_PENDING1 /* R */
  40. /* IRQ_GPI_ACK1 0x54, IRQ_GPI_ACK2 0x55, IRQ_GPI_ACK3 0x56 */
  41. #define STMFX_REG_IRQ_GPI_ACK STMFX_REG_IRQ_GPI_ACK1 /* RW */
  42. #define NR_GPIO_REGS 3
  43. #define NR_GPIOS_PER_REG 8
  44. #define get_reg(offset) ((offset) / NR_GPIOS_PER_REG)
  45. #define get_shift(offset) ((offset) % NR_GPIOS_PER_REG)
  46. #define get_mask(offset) (BIT(get_shift(offset)))
  47. /*
  48. * STMFX pinctrl can have up to 24 pins if STMFX other functions are not used.
  49. * Pins availability is managed thanks to gpio-ranges property.
  50. */
  51. static const struct pinctrl_pin_desc stmfx_pins[] = {
  52. PINCTRL_PIN(0, "gpio0"),
  53. PINCTRL_PIN(1, "gpio1"),
  54. PINCTRL_PIN(2, "gpio2"),
  55. PINCTRL_PIN(3, "gpio3"),
  56. PINCTRL_PIN(4, "gpio4"),
  57. PINCTRL_PIN(5, "gpio5"),
  58. PINCTRL_PIN(6, "gpio6"),
  59. PINCTRL_PIN(7, "gpio7"),
  60. PINCTRL_PIN(8, "gpio8"),
  61. PINCTRL_PIN(9, "gpio9"),
  62. PINCTRL_PIN(10, "gpio10"),
  63. PINCTRL_PIN(11, "gpio11"),
  64. PINCTRL_PIN(12, "gpio12"),
  65. PINCTRL_PIN(13, "gpio13"),
  66. PINCTRL_PIN(14, "gpio14"),
  67. PINCTRL_PIN(15, "gpio15"),
  68. PINCTRL_PIN(16, "agpio0"),
  69. PINCTRL_PIN(17, "agpio1"),
  70. PINCTRL_PIN(18, "agpio2"),
  71. PINCTRL_PIN(19, "agpio3"),
  72. PINCTRL_PIN(20, "agpio4"),
  73. PINCTRL_PIN(21, "agpio5"),
  74. PINCTRL_PIN(22, "agpio6"),
  75. PINCTRL_PIN(23, "agpio7"),
  76. };
  77. struct stmfx_pinctrl {
  78. struct device *dev;
  79. struct stmfx *stmfx;
  80. struct pinctrl_dev *pctl_dev;
  81. struct pinctrl_desc pctl_desc;
  82. struct gpio_chip gpio_chip;
  83. struct mutex lock; /* IRQ bus lock */
  84. unsigned long gpio_valid_mask;
  85. /* Cache of IRQ_GPI_* registers for bus_lock */
  86. u8 irq_gpi_src[NR_GPIO_REGS];
  87. u8 irq_gpi_type[NR_GPIO_REGS];
  88. u8 irq_gpi_evt[NR_GPIO_REGS];
  89. u8 irq_toggle_edge[NR_GPIO_REGS];
  90. #ifdef CONFIG_PM
  91. /* Backup of GPIO_* registers for suspend/resume */
  92. u8 bkp_gpio_state[NR_GPIO_REGS];
  93. u8 bkp_gpio_dir[NR_GPIO_REGS];
  94. u8 bkp_gpio_type[NR_GPIO_REGS];
  95. u8 bkp_gpio_pupd[NR_GPIO_REGS];
  96. #endif
  97. };
  98. static int stmfx_gpio_get(struct gpio_chip *gc, unsigned int offset)
  99. {
  100. struct stmfx_pinctrl *pctl = gpiochip_get_data(gc);
  101. u32 reg = STMFX_REG_GPIO_STATE + get_reg(offset);
  102. u32 mask = get_mask(offset);
  103. u32 value;
  104. int ret;
  105. ret = regmap_read(pctl->stmfx->map, reg, &value);
  106. return ret ? ret : !!(value & mask);
  107. }
  108. static int stmfx_gpio_set(struct gpio_chip *gc, unsigned int offset, int value)
  109. {
  110. struct stmfx_pinctrl *pctl = gpiochip_get_data(gc);
  111. u32 reg = value ? STMFX_REG_GPO_SET : STMFX_REG_GPO_CLR;
  112. u32 mask = get_mask(offset);
  113. return regmap_write_bits(pctl->stmfx->map, reg + get_reg(offset),
  114. mask, mask);
  115. }
  116. static int stmfx_gpio_get_direction(struct gpio_chip *gc, unsigned int offset)
  117. {
  118. struct stmfx_pinctrl *pctl = gpiochip_get_data(gc);
  119. u32 reg = STMFX_REG_GPIO_DIR + get_reg(offset);
  120. u32 mask = get_mask(offset);
  121. u32 val;
  122. int ret;
  123. ret = regmap_read(pctl->stmfx->map, reg, &val);
  124. /*
  125. * On stmfx, gpio pins direction is (0)input, (1)output.
  126. */
  127. if (ret)
  128. return ret;
  129. if (val & mask)
  130. return GPIO_LINE_DIRECTION_OUT;
  131. return GPIO_LINE_DIRECTION_IN;
  132. }
  133. static int stmfx_gpio_direction_input(struct gpio_chip *gc, unsigned int offset)
  134. {
  135. struct stmfx_pinctrl *pctl = gpiochip_get_data(gc);
  136. u32 reg = STMFX_REG_GPIO_DIR + get_reg(offset);
  137. u32 mask = get_mask(offset);
  138. return regmap_write_bits(pctl->stmfx->map, reg, mask, 0);
  139. }
  140. static int stmfx_gpio_direction_output(struct gpio_chip *gc,
  141. unsigned int offset, int value)
  142. {
  143. struct stmfx_pinctrl *pctl = gpiochip_get_data(gc);
  144. u32 reg = STMFX_REG_GPIO_DIR + get_reg(offset);
  145. u32 mask = get_mask(offset);
  146. int ret;
  147. ret = stmfx_gpio_set(gc, offset, value);
  148. if (ret)
  149. return ret;
  150. return regmap_write_bits(pctl->stmfx->map, reg, mask, mask);
  151. }
  152. static int stmfx_pinconf_get_pupd(struct stmfx_pinctrl *pctl,
  153. unsigned int offset)
  154. {
  155. u32 reg = STMFX_REG_GPIO_PUPD + get_reg(offset);
  156. u32 pupd, mask = get_mask(offset);
  157. int ret;
  158. ret = regmap_read(pctl->stmfx->map, reg, &pupd);
  159. if (ret)
  160. return ret;
  161. return !!(pupd & mask);
  162. }
  163. static int stmfx_pinconf_set_pupd(struct stmfx_pinctrl *pctl,
  164. unsigned int offset, u32 pupd)
  165. {
  166. u32 reg = STMFX_REG_GPIO_PUPD + get_reg(offset);
  167. u32 mask = get_mask(offset);
  168. return regmap_write_bits(pctl->stmfx->map, reg, mask, pupd ? mask : 0);
  169. }
  170. static int stmfx_pinconf_get_type(struct stmfx_pinctrl *pctl,
  171. unsigned int offset)
  172. {
  173. u32 reg = STMFX_REG_GPIO_TYPE + get_reg(offset);
  174. u32 type, mask = get_mask(offset);
  175. int ret;
  176. ret = regmap_read(pctl->stmfx->map, reg, &type);
  177. if (ret)
  178. return ret;
  179. return !!(type & mask);
  180. }
  181. static int stmfx_pinconf_set_type(struct stmfx_pinctrl *pctl,
  182. unsigned int offset, u32 type)
  183. {
  184. u32 reg = STMFX_REG_GPIO_TYPE + get_reg(offset);
  185. u32 mask = get_mask(offset);
  186. return regmap_write_bits(pctl->stmfx->map, reg, mask, type ? mask : 0);
  187. }
  188. static int stmfx_pinconf_get(struct pinctrl_dev *pctldev,
  189. unsigned int pin, unsigned long *config)
  190. {
  191. struct stmfx_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  192. u32 param = pinconf_to_config_param(*config);
  193. struct pinctrl_gpio_range *range;
  194. u32 arg = 0;
  195. int ret, dir, type, pupd;
  196. range = pinctrl_find_gpio_range_from_pin_nolock(pctldev, pin);
  197. if (!range)
  198. return -EINVAL;
  199. dir = stmfx_gpio_get_direction(&pctl->gpio_chip, pin);
  200. if (dir < 0)
  201. return dir;
  202. /*
  203. * Currently the gpiolib IN is 1 and OUT is 0 but let's not count
  204. * on it just to be on the safe side also in the future :)
  205. */
  206. dir = (dir == GPIO_LINE_DIRECTION_IN) ? 1 : 0;
  207. type = stmfx_pinconf_get_type(pctl, pin);
  208. if (type < 0)
  209. return type;
  210. pupd = stmfx_pinconf_get_pupd(pctl, pin);
  211. if (pupd < 0)
  212. return pupd;
  213. switch (param) {
  214. case PIN_CONFIG_BIAS_DISABLE:
  215. if ((!dir && (!type || !pupd)) || (dir && !type))
  216. arg = 1;
  217. break;
  218. case PIN_CONFIG_BIAS_PULL_DOWN:
  219. if (dir && type && !pupd)
  220. arg = 1;
  221. break;
  222. case PIN_CONFIG_BIAS_PULL_UP:
  223. if (type && pupd)
  224. arg = 1;
  225. break;
  226. case PIN_CONFIG_DRIVE_OPEN_DRAIN:
  227. if ((!dir && type) || (dir && !type))
  228. arg = 1;
  229. break;
  230. case PIN_CONFIG_DRIVE_PUSH_PULL:
  231. if ((!dir && !type) || (dir && type))
  232. arg = 1;
  233. break;
  234. case PIN_CONFIG_LEVEL:
  235. if (dir)
  236. return -EINVAL;
  237. ret = stmfx_gpio_get(&pctl->gpio_chip, pin);
  238. if (ret < 0)
  239. return ret;
  240. arg = ret;
  241. break;
  242. default:
  243. return -ENOTSUPP;
  244. }
  245. *config = pinconf_to_config_packed(param, arg);
  246. return 0;
  247. }
  248. static int stmfx_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
  249. unsigned long *configs, unsigned int num_configs)
  250. {
  251. struct stmfx_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  252. struct pinctrl_gpio_range *range;
  253. enum pin_config_param param;
  254. u32 arg;
  255. int i, ret;
  256. range = pinctrl_find_gpio_range_from_pin_nolock(pctldev, pin);
  257. if (!range) {
  258. dev_err(pctldev->dev, "pin %d is not available\n", pin);
  259. return -EINVAL;
  260. }
  261. for (i = 0; i < num_configs; i++) {
  262. param = pinconf_to_config_param(configs[i]);
  263. arg = pinconf_to_config_argument(configs[i]);
  264. switch (param) {
  265. case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
  266. case PIN_CONFIG_BIAS_DISABLE:
  267. case PIN_CONFIG_DRIVE_PUSH_PULL:
  268. ret = stmfx_pinconf_set_type(pctl, pin, 0);
  269. if (ret)
  270. return ret;
  271. break;
  272. case PIN_CONFIG_BIAS_PULL_DOWN:
  273. ret = stmfx_pinconf_set_type(pctl, pin, 1);
  274. if (ret)
  275. return ret;
  276. ret = stmfx_pinconf_set_pupd(pctl, pin, 0);
  277. if (ret)
  278. return ret;
  279. break;
  280. case PIN_CONFIG_BIAS_PULL_UP:
  281. ret = stmfx_pinconf_set_type(pctl, pin, 1);
  282. if (ret)
  283. return ret;
  284. ret = stmfx_pinconf_set_pupd(pctl, pin, 1);
  285. if (ret)
  286. return ret;
  287. break;
  288. case PIN_CONFIG_DRIVE_OPEN_DRAIN:
  289. ret = stmfx_pinconf_set_type(pctl, pin, 1);
  290. if (ret)
  291. return ret;
  292. break;
  293. case PIN_CONFIG_LEVEL:
  294. ret = stmfx_gpio_direction_output(&pctl->gpio_chip,
  295. pin, arg);
  296. if (ret)
  297. return ret;
  298. break;
  299. default:
  300. return -ENOTSUPP;
  301. }
  302. }
  303. return 0;
  304. }
  305. static void stmfx_pinconf_dbg_show(struct pinctrl_dev *pctldev,
  306. struct seq_file *s, unsigned int offset)
  307. {
  308. struct stmfx_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  309. struct pinctrl_gpio_range *range;
  310. int dir, type, pupd, val;
  311. range = pinctrl_find_gpio_range_from_pin_nolock(pctldev, offset);
  312. if (!range)
  313. return;
  314. dir = stmfx_gpio_get_direction(&pctl->gpio_chip, offset);
  315. if (dir < 0)
  316. return;
  317. type = stmfx_pinconf_get_type(pctl, offset);
  318. if (type < 0)
  319. return;
  320. pupd = stmfx_pinconf_get_pupd(pctl, offset);
  321. if (pupd < 0)
  322. return;
  323. val = stmfx_gpio_get(&pctl->gpio_chip, offset);
  324. if (val < 0)
  325. return;
  326. if (dir == GPIO_LINE_DIRECTION_OUT) {
  327. seq_printf(s, "output %s ", str_high_low(val));
  328. if (type)
  329. seq_printf(s, "open drain %s internal pull-up ",
  330. pupd ? "with" : "without");
  331. else
  332. seq_puts(s, "push pull no pull ");
  333. } else {
  334. seq_printf(s, "input %s ", str_high_low(val));
  335. if (type)
  336. seq_printf(s, "with internal pull-%s ",
  337. str_up_down(pupd));
  338. else
  339. seq_printf(s, "%s ", pupd ? "floating" : "analog");
  340. }
  341. }
  342. static const struct pinconf_ops stmfx_pinconf_ops = {
  343. .pin_config_get = stmfx_pinconf_get,
  344. .pin_config_set = stmfx_pinconf_set,
  345. .pin_config_dbg_show = stmfx_pinconf_dbg_show,
  346. };
  347. static int stmfx_pinctrl_get_groups_count(struct pinctrl_dev *pctldev)
  348. {
  349. return 0;
  350. }
  351. static const char *stmfx_pinctrl_get_group_name(struct pinctrl_dev *pctldev,
  352. unsigned int selector)
  353. {
  354. return NULL;
  355. }
  356. static int stmfx_pinctrl_get_group_pins(struct pinctrl_dev *pctldev,
  357. unsigned int selector,
  358. const unsigned int **pins,
  359. unsigned int *num_pins)
  360. {
  361. return -ENOTSUPP;
  362. }
  363. static const struct pinctrl_ops stmfx_pinctrl_ops = {
  364. .get_groups_count = stmfx_pinctrl_get_groups_count,
  365. .get_group_name = stmfx_pinctrl_get_group_name,
  366. .get_group_pins = stmfx_pinctrl_get_group_pins,
  367. .dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
  368. .dt_free_map = pinctrl_utils_free_map,
  369. };
  370. static void stmfx_pinctrl_irq_mask(struct irq_data *data)
  371. {
  372. struct gpio_chip *gpio_chip = irq_data_get_irq_chip_data(data);
  373. struct stmfx_pinctrl *pctl = gpiochip_get_data(gpio_chip);
  374. u32 reg = get_reg(data->hwirq);
  375. u32 mask = get_mask(data->hwirq);
  376. pctl->irq_gpi_src[reg] &= ~mask;
  377. gpiochip_disable_irq(gpio_chip, irqd_to_hwirq(data));
  378. }
  379. static void stmfx_pinctrl_irq_unmask(struct irq_data *data)
  380. {
  381. struct gpio_chip *gpio_chip = irq_data_get_irq_chip_data(data);
  382. struct stmfx_pinctrl *pctl = gpiochip_get_data(gpio_chip);
  383. u32 reg = get_reg(data->hwirq);
  384. u32 mask = get_mask(data->hwirq);
  385. gpiochip_enable_irq(gpio_chip, irqd_to_hwirq(data));
  386. pctl->irq_gpi_src[reg] |= mask;
  387. }
  388. static int stmfx_pinctrl_irq_set_type(struct irq_data *data, unsigned int type)
  389. {
  390. struct gpio_chip *gpio_chip = irq_data_get_irq_chip_data(data);
  391. struct stmfx_pinctrl *pctl = gpiochip_get_data(gpio_chip);
  392. u32 reg = get_reg(data->hwirq);
  393. u32 mask = get_mask(data->hwirq);
  394. if (type == IRQ_TYPE_NONE)
  395. return -EINVAL;
  396. if (type & IRQ_TYPE_EDGE_BOTH) {
  397. pctl->irq_gpi_evt[reg] |= mask;
  398. irq_set_handler_locked(data, handle_edge_irq);
  399. } else {
  400. pctl->irq_gpi_evt[reg] &= ~mask;
  401. irq_set_handler_locked(data, handle_level_irq);
  402. }
  403. if ((type & IRQ_TYPE_EDGE_RISING) || (type & IRQ_TYPE_LEVEL_HIGH))
  404. pctl->irq_gpi_type[reg] |= mask;
  405. else
  406. pctl->irq_gpi_type[reg] &= ~mask;
  407. /*
  408. * In case of (type & IRQ_TYPE_EDGE_BOTH), we need to know current
  409. * GPIO value to set the right edge trigger. But in atomic context
  410. * here we can't access registers over I2C. That's why (type &
  411. * IRQ_TYPE_EDGE_BOTH) will be managed in .irq_sync_unlock.
  412. */
  413. if ((type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH)
  414. pctl->irq_toggle_edge[reg] |= mask;
  415. else
  416. pctl->irq_toggle_edge[reg] &= mask;
  417. return 0;
  418. }
  419. static void stmfx_pinctrl_irq_bus_lock(struct irq_data *data)
  420. {
  421. struct gpio_chip *gpio_chip = irq_data_get_irq_chip_data(data);
  422. struct stmfx_pinctrl *pctl = gpiochip_get_data(gpio_chip);
  423. mutex_lock(&pctl->lock);
  424. }
  425. static void stmfx_pinctrl_irq_bus_sync_unlock(struct irq_data *data)
  426. {
  427. struct gpio_chip *gpio_chip = irq_data_get_irq_chip_data(data);
  428. struct stmfx_pinctrl *pctl = gpiochip_get_data(gpio_chip);
  429. u32 reg = get_reg(data->hwirq);
  430. u32 mask = get_mask(data->hwirq);
  431. /*
  432. * In case of IRQ_TYPE_EDGE_BOTH), read the current GPIO value
  433. * (this couldn't be done in .irq_set_type because of atomic context)
  434. * to set the right irq trigger type.
  435. */
  436. if (pctl->irq_toggle_edge[reg] & mask) {
  437. if (stmfx_gpio_get(gpio_chip, data->hwirq))
  438. pctl->irq_gpi_type[reg] &= ~mask;
  439. else
  440. pctl->irq_gpi_type[reg] |= mask;
  441. }
  442. regmap_bulk_write(pctl->stmfx->map, STMFX_REG_IRQ_GPI_EVT,
  443. pctl->irq_gpi_evt, NR_GPIO_REGS);
  444. regmap_bulk_write(pctl->stmfx->map, STMFX_REG_IRQ_GPI_TYPE,
  445. pctl->irq_gpi_type, NR_GPIO_REGS);
  446. regmap_bulk_write(pctl->stmfx->map, STMFX_REG_IRQ_GPI_SRC,
  447. pctl->irq_gpi_src, NR_GPIO_REGS);
  448. mutex_unlock(&pctl->lock);
  449. }
  450. static int stmfx_gpio_irq_request_resources(struct irq_data *data)
  451. {
  452. struct gpio_chip *gpio_chip = irq_data_get_irq_chip_data(data);
  453. int ret;
  454. ret = stmfx_gpio_direction_input(gpio_chip, data->hwirq);
  455. if (ret)
  456. return ret;
  457. return gpiochip_reqres_irq(gpio_chip, data->hwirq);
  458. }
  459. static void stmfx_gpio_irq_release_resources(struct irq_data *data)
  460. {
  461. struct gpio_chip *gpio_chip = irq_data_get_irq_chip_data(data);
  462. return gpiochip_relres_irq(gpio_chip, data->hwirq);
  463. }
  464. static void stmfx_pinctrl_irq_toggle_trigger(struct stmfx_pinctrl *pctl,
  465. unsigned int offset)
  466. {
  467. u32 reg = get_reg(offset);
  468. u32 mask = get_mask(offset);
  469. int val;
  470. if (!(pctl->irq_toggle_edge[reg] & mask))
  471. return;
  472. val = stmfx_gpio_get(&pctl->gpio_chip, offset);
  473. if (val < 0)
  474. return;
  475. if (val) {
  476. pctl->irq_gpi_type[reg] &= mask;
  477. regmap_write_bits(pctl->stmfx->map,
  478. STMFX_REG_IRQ_GPI_TYPE + reg,
  479. mask, 0);
  480. } else {
  481. pctl->irq_gpi_type[reg] |= mask;
  482. regmap_write_bits(pctl->stmfx->map,
  483. STMFX_REG_IRQ_GPI_TYPE + reg,
  484. mask, mask);
  485. }
  486. }
  487. static irqreturn_t stmfx_pinctrl_irq_thread_fn(int irq, void *dev_id)
  488. {
  489. struct stmfx_pinctrl *pctl = (struct stmfx_pinctrl *)dev_id;
  490. struct gpio_chip *gc = &pctl->gpio_chip;
  491. u8 pending[NR_GPIO_REGS];
  492. u8 src[NR_GPIO_REGS] = {0, 0, 0};
  493. unsigned long n, status;
  494. int i, ret;
  495. ret = regmap_bulk_read(pctl->stmfx->map, STMFX_REG_IRQ_GPI_PENDING,
  496. &pending, NR_GPIO_REGS);
  497. if (ret)
  498. return IRQ_NONE;
  499. regmap_bulk_write(pctl->stmfx->map, STMFX_REG_IRQ_GPI_SRC,
  500. src, NR_GPIO_REGS);
  501. BUILD_BUG_ON(NR_GPIO_REGS > sizeof(status));
  502. for (i = 0, status = 0; i < NR_GPIO_REGS; i++)
  503. status |= (unsigned long)pending[i] << (i * 8);
  504. for_each_set_bit(n, &status, gc->ngpio) {
  505. handle_nested_irq(irq_find_mapping(gc->irq.domain, n));
  506. stmfx_pinctrl_irq_toggle_trigger(pctl, n);
  507. }
  508. regmap_bulk_write(pctl->stmfx->map, STMFX_REG_IRQ_GPI_SRC,
  509. pctl->irq_gpi_src, NR_GPIO_REGS);
  510. return IRQ_HANDLED;
  511. }
  512. static void stmfx_pinctrl_irq_print_chip(struct irq_data *d, struct seq_file *p)
  513. {
  514. struct gpio_chip *gpio_chip = irq_data_get_irq_chip_data(d);
  515. struct stmfx_pinctrl *pctl = gpiochip_get_data(gpio_chip);
  516. seq_puts(p, dev_name(pctl->dev));
  517. }
  518. static const struct irq_chip stmfx_pinctrl_irq_chip = {
  519. .irq_mask = stmfx_pinctrl_irq_mask,
  520. .irq_unmask = stmfx_pinctrl_irq_unmask,
  521. .irq_set_type = stmfx_pinctrl_irq_set_type,
  522. .irq_bus_lock = stmfx_pinctrl_irq_bus_lock,
  523. .irq_bus_sync_unlock = stmfx_pinctrl_irq_bus_sync_unlock,
  524. .irq_request_resources = stmfx_gpio_irq_request_resources,
  525. .irq_release_resources = stmfx_gpio_irq_release_resources,
  526. .irq_print_chip = stmfx_pinctrl_irq_print_chip,
  527. .flags = IRQCHIP_IMMUTABLE,
  528. };
  529. static int stmfx_pinctrl_gpio_function_enable(struct stmfx_pinctrl *pctl)
  530. {
  531. struct pinctrl_gpio_range *gpio_range;
  532. struct pinctrl_dev *pctl_dev = pctl->pctl_dev;
  533. u32 func = STMFX_FUNC_GPIO;
  534. pctl->gpio_valid_mask = GENMASK(15, 0);
  535. gpio_range = pinctrl_find_gpio_range_from_pin(pctl_dev, 16);
  536. if (gpio_range) {
  537. func |= STMFX_FUNC_ALTGPIO_LOW;
  538. pctl->gpio_valid_mask |= GENMASK(19, 16);
  539. }
  540. gpio_range = pinctrl_find_gpio_range_from_pin(pctl_dev, 20);
  541. if (gpio_range) {
  542. func |= STMFX_FUNC_ALTGPIO_HIGH;
  543. pctl->gpio_valid_mask |= GENMASK(23, 20);
  544. }
  545. return stmfx_function_enable(pctl->stmfx, func);
  546. }
  547. static int stmfx_pinctrl_probe(struct platform_device *pdev)
  548. {
  549. struct stmfx *stmfx = dev_get_drvdata(pdev->dev.parent);
  550. struct device_node *np = pdev->dev.of_node;
  551. struct stmfx_pinctrl *pctl;
  552. struct gpio_irq_chip *girq;
  553. int irq, ret;
  554. pctl = devm_kzalloc(stmfx->dev, sizeof(*pctl), GFP_KERNEL);
  555. if (!pctl)
  556. return -ENOMEM;
  557. platform_set_drvdata(pdev, pctl);
  558. pctl->dev = &pdev->dev;
  559. pctl->stmfx = stmfx;
  560. if (!of_property_present(np, "gpio-ranges")) {
  561. dev_err(pctl->dev, "missing required gpio-ranges property\n");
  562. return -EINVAL;
  563. }
  564. irq = platform_get_irq(pdev, 0);
  565. if (irq < 0)
  566. return irq;
  567. mutex_init(&pctl->lock);
  568. /* Register pin controller */
  569. pctl->pctl_desc.name = "stmfx-pinctrl";
  570. pctl->pctl_desc.pctlops = &stmfx_pinctrl_ops;
  571. pctl->pctl_desc.confops = &stmfx_pinconf_ops;
  572. pctl->pctl_desc.pins = stmfx_pins;
  573. pctl->pctl_desc.npins = ARRAY_SIZE(stmfx_pins);
  574. pctl->pctl_desc.owner = THIS_MODULE;
  575. pctl->pctl_desc.link_consumers = true;
  576. ret = devm_pinctrl_register_and_init(pctl->dev, &pctl->pctl_desc,
  577. pctl, &pctl->pctl_dev);
  578. if (ret) {
  579. dev_err(pctl->dev, "pinctrl registration failed\n");
  580. return ret;
  581. }
  582. ret = pinctrl_enable(pctl->pctl_dev);
  583. if (ret) {
  584. dev_err(pctl->dev, "pinctrl enable failed\n");
  585. return ret;
  586. }
  587. /* Register gpio controller */
  588. pctl->gpio_chip.label = "stmfx-gpio";
  589. pctl->gpio_chip.parent = pctl->dev;
  590. pctl->gpio_chip.get_direction = stmfx_gpio_get_direction;
  591. pctl->gpio_chip.direction_input = stmfx_gpio_direction_input;
  592. pctl->gpio_chip.direction_output = stmfx_gpio_direction_output;
  593. pctl->gpio_chip.get = stmfx_gpio_get;
  594. pctl->gpio_chip.set = stmfx_gpio_set;
  595. pctl->gpio_chip.set_config = gpiochip_generic_config;
  596. pctl->gpio_chip.base = -1;
  597. pctl->gpio_chip.ngpio = pctl->pctl_desc.npins;
  598. pctl->gpio_chip.can_sleep = true;
  599. girq = &pctl->gpio_chip.irq;
  600. gpio_irq_chip_set_chip(girq, &stmfx_pinctrl_irq_chip);
  601. /* This will let us handle the parent IRQ in the driver */
  602. girq->parent_handler = NULL;
  603. girq->num_parents = 0;
  604. girq->parents = NULL;
  605. girq->default_type = IRQ_TYPE_NONE;
  606. girq->handler = handle_bad_irq;
  607. girq->threaded = true;
  608. ret = devm_gpiochip_add_data(pctl->dev, &pctl->gpio_chip, pctl);
  609. if (ret) {
  610. dev_err(pctl->dev, "gpio_chip registration failed\n");
  611. return ret;
  612. }
  613. ret = stmfx_pinctrl_gpio_function_enable(pctl);
  614. if (ret)
  615. return ret;
  616. ret = devm_request_threaded_irq(pctl->dev, irq, NULL,
  617. stmfx_pinctrl_irq_thread_fn,
  618. IRQF_ONESHOT,
  619. dev_name(pctl->dev), pctl);
  620. if (ret) {
  621. dev_err(pctl->dev, "cannot request irq%d\n", irq);
  622. return ret;
  623. }
  624. dev_info(pctl->dev,
  625. "%ld GPIOs available\n", hweight_long(pctl->gpio_valid_mask));
  626. return 0;
  627. }
  628. static void stmfx_pinctrl_remove(struct platform_device *pdev)
  629. {
  630. struct stmfx *stmfx = dev_get_drvdata(pdev->dev.parent);
  631. int ret;
  632. ret = stmfx_function_disable(stmfx,
  633. STMFX_FUNC_GPIO |
  634. STMFX_FUNC_ALTGPIO_LOW |
  635. STMFX_FUNC_ALTGPIO_HIGH);
  636. if (ret)
  637. dev_err(&pdev->dev, "Failed to disable pins (%pe)\n",
  638. ERR_PTR(ret));
  639. }
  640. #ifdef CONFIG_PM_SLEEP
  641. static int stmfx_pinctrl_backup_regs(struct stmfx_pinctrl *pctl)
  642. {
  643. int ret;
  644. ret = regmap_bulk_read(pctl->stmfx->map, STMFX_REG_GPIO_STATE,
  645. &pctl->bkp_gpio_state, NR_GPIO_REGS);
  646. if (ret)
  647. return ret;
  648. ret = regmap_bulk_read(pctl->stmfx->map, STMFX_REG_GPIO_DIR,
  649. &pctl->bkp_gpio_dir, NR_GPIO_REGS);
  650. if (ret)
  651. return ret;
  652. ret = regmap_bulk_read(pctl->stmfx->map, STMFX_REG_GPIO_TYPE,
  653. &pctl->bkp_gpio_type, NR_GPIO_REGS);
  654. if (ret)
  655. return ret;
  656. ret = regmap_bulk_read(pctl->stmfx->map, STMFX_REG_GPIO_PUPD,
  657. &pctl->bkp_gpio_pupd, NR_GPIO_REGS);
  658. if (ret)
  659. return ret;
  660. return 0;
  661. }
  662. static int stmfx_pinctrl_restore_regs(struct stmfx_pinctrl *pctl)
  663. {
  664. int ret;
  665. ret = regmap_bulk_write(pctl->stmfx->map, STMFX_REG_GPIO_DIR,
  666. pctl->bkp_gpio_dir, NR_GPIO_REGS);
  667. if (ret)
  668. return ret;
  669. ret = regmap_bulk_write(pctl->stmfx->map, STMFX_REG_GPIO_TYPE,
  670. pctl->bkp_gpio_type, NR_GPIO_REGS);
  671. if (ret)
  672. return ret;
  673. ret = regmap_bulk_write(pctl->stmfx->map, STMFX_REG_GPIO_PUPD,
  674. pctl->bkp_gpio_pupd, NR_GPIO_REGS);
  675. if (ret)
  676. return ret;
  677. ret = regmap_bulk_write(pctl->stmfx->map, STMFX_REG_GPO_SET,
  678. pctl->bkp_gpio_state, NR_GPIO_REGS);
  679. if (ret)
  680. return ret;
  681. ret = regmap_bulk_write(pctl->stmfx->map, STMFX_REG_IRQ_GPI_EVT,
  682. pctl->irq_gpi_evt, NR_GPIO_REGS);
  683. if (ret)
  684. return ret;
  685. ret = regmap_bulk_write(pctl->stmfx->map, STMFX_REG_IRQ_GPI_TYPE,
  686. pctl->irq_gpi_type, NR_GPIO_REGS);
  687. if (ret)
  688. return ret;
  689. ret = regmap_bulk_write(pctl->stmfx->map, STMFX_REG_IRQ_GPI_SRC,
  690. pctl->irq_gpi_src, NR_GPIO_REGS);
  691. if (ret)
  692. return ret;
  693. return 0;
  694. }
  695. static int stmfx_pinctrl_suspend(struct device *dev)
  696. {
  697. struct stmfx_pinctrl *pctl = dev_get_drvdata(dev);
  698. int ret;
  699. ret = stmfx_pinctrl_backup_regs(pctl);
  700. if (ret) {
  701. dev_err(pctl->dev, "registers backup failure\n");
  702. return ret;
  703. }
  704. return 0;
  705. }
  706. static int stmfx_pinctrl_resume(struct device *dev)
  707. {
  708. struct stmfx_pinctrl *pctl = dev_get_drvdata(dev);
  709. int ret;
  710. ret = stmfx_pinctrl_restore_regs(pctl);
  711. if (ret) {
  712. dev_err(pctl->dev, "registers restoration failure\n");
  713. return ret;
  714. }
  715. return 0;
  716. }
  717. #endif
  718. static SIMPLE_DEV_PM_OPS(stmfx_pinctrl_dev_pm_ops,
  719. stmfx_pinctrl_suspend, stmfx_pinctrl_resume);
  720. static const struct of_device_id stmfx_pinctrl_of_match[] = {
  721. { .compatible = "st,stmfx-0300-pinctrl", },
  722. {},
  723. };
  724. MODULE_DEVICE_TABLE(of, stmfx_pinctrl_of_match);
  725. static struct platform_driver stmfx_pinctrl_driver = {
  726. .driver = {
  727. .name = "stmfx-pinctrl",
  728. .of_match_table = stmfx_pinctrl_of_match,
  729. .pm = &stmfx_pinctrl_dev_pm_ops,
  730. },
  731. .probe = stmfx_pinctrl_probe,
  732. .remove = stmfx_pinctrl_remove,
  733. };
  734. module_platform_driver(stmfx_pinctrl_driver);
  735. MODULE_DESCRIPTION("STMFX pinctrl/GPIO driver");
  736. MODULE_AUTHOR("Amelie Delaunay <amelie.delaunay@st.com>");
  737. MODULE_LICENSE("GPL v2");