pinctrl-stmfx.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  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 void 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. 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. stmfx_gpio_set(gc, offset, value);
  147. return regmap_write_bits(pctl->stmfx->map, reg, mask, mask);
  148. }
  149. static int stmfx_pinconf_get_pupd(struct stmfx_pinctrl *pctl,
  150. unsigned int offset)
  151. {
  152. u32 reg = STMFX_REG_GPIO_PUPD + get_reg(offset);
  153. u32 pupd, mask = get_mask(offset);
  154. int ret;
  155. ret = regmap_read(pctl->stmfx->map, reg, &pupd);
  156. if (ret)
  157. return ret;
  158. return !!(pupd & mask);
  159. }
  160. static int stmfx_pinconf_set_pupd(struct stmfx_pinctrl *pctl,
  161. unsigned int offset, u32 pupd)
  162. {
  163. u32 reg = STMFX_REG_GPIO_PUPD + get_reg(offset);
  164. u32 mask = get_mask(offset);
  165. return regmap_write_bits(pctl->stmfx->map, reg, mask, pupd ? mask : 0);
  166. }
  167. static int stmfx_pinconf_get_type(struct stmfx_pinctrl *pctl,
  168. unsigned int offset)
  169. {
  170. u32 reg = STMFX_REG_GPIO_TYPE + get_reg(offset);
  171. u32 type, mask = get_mask(offset);
  172. int ret;
  173. ret = regmap_read(pctl->stmfx->map, reg, &type);
  174. if (ret)
  175. return ret;
  176. return !!(type & mask);
  177. }
  178. static int stmfx_pinconf_set_type(struct stmfx_pinctrl *pctl,
  179. unsigned int offset, u32 type)
  180. {
  181. u32 reg = STMFX_REG_GPIO_TYPE + get_reg(offset);
  182. u32 mask = get_mask(offset);
  183. return regmap_write_bits(pctl->stmfx->map, reg, mask, type ? mask : 0);
  184. }
  185. static int stmfx_pinconf_get(struct pinctrl_dev *pctldev,
  186. unsigned int pin, unsigned long *config)
  187. {
  188. struct stmfx_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  189. u32 param = pinconf_to_config_param(*config);
  190. struct pinctrl_gpio_range *range;
  191. u32 arg = 0;
  192. int ret, dir, type, pupd;
  193. range = pinctrl_find_gpio_range_from_pin_nolock(pctldev, pin);
  194. if (!range)
  195. return -EINVAL;
  196. dir = stmfx_gpio_get_direction(&pctl->gpio_chip, pin);
  197. if (dir < 0)
  198. return dir;
  199. /*
  200. * Currently the gpiolib IN is 1 and OUT is 0 but let's not count
  201. * on it just to be on the safe side also in the future :)
  202. */
  203. dir = (dir == GPIO_LINE_DIRECTION_IN) ? 1 : 0;
  204. type = stmfx_pinconf_get_type(pctl, pin);
  205. if (type < 0)
  206. return type;
  207. pupd = stmfx_pinconf_get_pupd(pctl, pin);
  208. if (pupd < 0)
  209. return pupd;
  210. switch (param) {
  211. case PIN_CONFIG_BIAS_DISABLE:
  212. if ((!dir && (!type || !pupd)) || (dir && !type))
  213. arg = 1;
  214. break;
  215. case PIN_CONFIG_BIAS_PULL_DOWN:
  216. if (dir && type && !pupd)
  217. arg = 1;
  218. break;
  219. case PIN_CONFIG_BIAS_PULL_UP:
  220. if (type && pupd)
  221. arg = 1;
  222. break;
  223. case PIN_CONFIG_DRIVE_OPEN_DRAIN:
  224. if ((!dir && type) || (dir && !type))
  225. arg = 1;
  226. break;
  227. case PIN_CONFIG_DRIVE_PUSH_PULL:
  228. if ((!dir && !type) || (dir && type))
  229. arg = 1;
  230. break;
  231. case PIN_CONFIG_OUTPUT:
  232. if (dir)
  233. return -EINVAL;
  234. ret = stmfx_gpio_get(&pctl->gpio_chip, pin);
  235. if (ret < 0)
  236. return ret;
  237. arg = ret;
  238. break;
  239. default:
  240. return -ENOTSUPP;
  241. }
  242. *config = pinconf_to_config_packed(param, arg);
  243. return 0;
  244. }
  245. static int stmfx_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
  246. unsigned long *configs, unsigned int num_configs)
  247. {
  248. struct stmfx_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  249. struct pinctrl_gpio_range *range;
  250. enum pin_config_param param;
  251. u32 arg;
  252. int i, ret;
  253. range = pinctrl_find_gpio_range_from_pin_nolock(pctldev, pin);
  254. if (!range) {
  255. dev_err(pctldev->dev, "pin %d is not available\n", pin);
  256. return -EINVAL;
  257. }
  258. for (i = 0; i < num_configs; i++) {
  259. param = pinconf_to_config_param(configs[i]);
  260. arg = pinconf_to_config_argument(configs[i]);
  261. switch (param) {
  262. case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
  263. case PIN_CONFIG_BIAS_DISABLE:
  264. case PIN_CONFIG_DRIVE_PUSH_PULL:
  265. ret = stmfx_pinconf_set_type(pctl, pin, 0);
  266. if (ret)
  267. return ret;
  268. break;
  269. case PIN_CONFIG_BIAS_PULL_DOWN:
  270. ret = stmfx_pinconf_set_type(pctl, pin, 1);
  271. if (ret)
  272. return ret;
  273. ret = stmfx_pinconf_set_pupd(pctl, pin, 0);
  274. if (ret)
  275. return ret;
  276. break;
  277. case PIN_CONFIG_BIAS_PULL_UP:
  278. ret = stmfx_pinconf_set_type(pctl, pin, 1);
  279. if (ret)
  280. return ret;
  281. ret = stmfx_pinconf_set_pupd(pctl, pin, 1);
  282. if (ret)
  283. return ret;
  284. break;
  285. case PIN_CONFIG_DRIVE_OPEN_DRAIN:
  286. ret = stmfx_pinconf_set_type(pctl, pin, 1);
  287. if (ret)
  288. return ret;
  289. break;
  290. case PIN_CONFIG_OUTPUT:
  291. ret = stmfx_gpio_direction_output(&pctl->gpio_chip,
  292. pin, arg);
  293. if (ret)
  294. return ret;
  295. break;
  296. default:
  297. return -ENOTSUPP;
  298. }
  299. }
  300. return 0;
  301. }
  302. static void stmfx_pinconf_dbg_show(struct pinctrl_dev *pctldev,
  303. struct seq_file *s, unsigned int offset)
  304. {
  305. struct stmfx_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  306. struct pinctrl_gpio_range *range;
  307. int dir, type, pupd, val;
  308. range = pinctrl_find_gpio_range_from_pin_nolock(pctldev, offset);
  309. if (!range)
  310. return;
  311. dir = stmfx_gpio_get_direction(&pctl->gpio_chip, offset);
  312. if (dir < 0)
  313. return;
  314. type = stmfx_pinconf_get_type(pctl, offset);
  315. if (type < 0)
  316. return;
  317. pupd = stmfx_pinconf_get_pupd(pctl, offset);
  318. if (pupd < 0)
  319. return;
  320. val = stmfx_gpio_get(&pctl->gpio_chip, offset);
  321. if (val < 0)
  322. return;
  323. if (dir == GPIO_LINE_DIRECTION_OUT) {
  324. seq_printf(s, "output %s ", str_high_low(val));
  325. if (type)
  326. seq_printf(s, "open drain %s internal pull-up ",
  327. pupd ? "with" : "without");
  328. else
  329. seq_puts(s, "push pull no pull ");
  330. } else {
  331. seq_printf(s, "input %s ", str_high_low(val));
  332. if (type)
  333. seq_printf(s, "with internal pull-%s ",
  334. pupd ? "up" : "down");
  335. else
  336. seq_printf(s, "%s ", pupd ? "floating" : "analog");
  337. }
  338. }
  339. static const struct pinconf_ops stmfx_pinconf_ops = {
  340. .pin_config_get = stmfx_pinconf_get,
  341. .pin_config_set = stmfx_pinconf_set,
  342. .pin_config_dbg_show = stmfx_pinconf_dbg_show,
  343. };
  344. static int stmfx_pinctrl_get_groups_count(struct pinctrl_dev *pctldev)
  345. {
  346. return 0;
  347. }
  348. static const char *stmfx_pinctrl_get_group_name(struct pinctrl_dev *pctldev,
  349. unsigned int selector)
  350. {
  351. return NULL;
  352. }
  353. static int stmfx_pinctrl_get_group_pins(struct pinctrl_dev *pctldev,
  354. unsigned int selector,
  355. const unsigned int **pins,
  356. unsigned int *num_pins)
  357. {
  358. return -ENOTSUPP;
  359. }
  360. static const struct pinctrl_ops stmfx_pinctrl_ops = {
  361. .get_groups_count = stmfx_pinctrl_get_groups_count,
  362. .get_group_name = stmfx_pinctrl_get_group_name,
  363. .get_group_pins = stmfx_pinctrl_get_group_pins,
  364. .dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
  365. .dt_free_map = pinctrl_utils_free_map,
  366. };
  367. static void stmfx_pinctrl_irq_mask(struct irq_data *data)
  368. {
  369. struct gpio_chip *gpio_chip = irq_data_get_irq_chip_data(data);
  370. struct stmfx_pinctrl *pctl = gpiochip_get_data(gpio_chip);
  371. u32 reg = get_reg(data->hwirq);
  372. u32 mask = get_mask(data->hwirq);
  373. pctl->irq_gpi_src[reg] &= ~mask;
  374. gpiochip_disable_irq(gpio_chip, irqd_to_hwirq(data));
  375. }
  376. static void stmfx_pinctrl_irq_unmask(struct irq_data *data)
  377. {
  378. struct gpio_chip *gpio_chip = irq_data_get_irq_chip_data(data);
  379. struct stmfx_pinctrl *pctl = gpiochip_get_data(gpio_chip);
  380. u32 reg = get_reg(data->hwirq);
  381. u32 mask = get_mask(data->hwirq);
  382. gpiochip_enable_irq(gpio_chip, irqd_to_hwirq(data));
  383. pctl->irq_gpi_src[reg] |= mask;
  384. }
  385. static int stmfx_pinctrl_irq_set_type(struct irq_data *data, unsigned int type)
  386. {
  387. struct gpio_chip *gpio_chip = irq_data_get_irq_chip_data(data);
  388. struct stmfx_pinctrl *pctl = gpiochip_get_data(gpio_chip);
  389. u32 reg = get_reg(data->hwirq);
  390. u32 mask = get_mask(data->hwirq);
  391. if (type == IRQ_TYPE_NONE)
  392. return -EINVAL;
  393. if (type & IRQ_TYPE_EDGE_BOTH) {
  394. pctl->irq_gpi_evt[reg] |= mask;
  395. irq_set_handler_locked(data, handle_edge_irq);
  396. } else {
  397. pctl->irq_gpi_evt[reg] &= ~mask;
  398. irq_set_handler_locked(data, handle_level_irq);
  399. }
  400. if ((type & IRQ_TYPE_EDGE_RISING) || (type & IRQ_TYPE_LEVEL_HIGH))
  401. pctl->irq_gpi_type[reg] |= mask;
  402. else
  403. pctl->irq_gpi_type[reg] &= ~mask;
  404. /*
  405. * In case of (type & IRQ_TYPE_EDGE_BOTH), we need to know current
  406. * GPIO value to set the right edge trigger. But in atomic context
  407. * here we can't access registers over I2C. That's why (type &
  408. * IRQ_TYPE_EDGE_BOTH) will be managed in .irq_sync_unlock.
  409. */
  410. if ((type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH)
  411. pctl->irq_toggle_edge[reg] |= mask;
  412. else
  413. pctl->irq_toggle_edge[reg] &= mask;
  414. return 0;
  415. }
  416. static void stmfx_pinctrl_irq_bus_lock(struct irq_data *data)
  417. {
  418. struct gpio_chip *gpio_chip = irq_data_get_irq_chip_data(data);
  419. struct stmfx_pinctrl *pctl = gpiochip_get_data(gpio_chip);
  420. mutex_lock(&pctl->lock);
  421. }
  422. static void stmfx_pinctrl_irq_bus_sync_unlock(struct irq_data *data)
  423. {
  424. struct gpio_chip *gpio_chip = irq_data_get_irq_chip_data(data);
  425. struct stmfx_pinctrl *pctl = gpiochip_get_data(gpio_chip);
  426. u32 reg = get_reg(data->hwirq);
  427. u32 mask = get_mask(data->hwirq);
  428. /*
  429. * In case of IRQ_TYPE_EDGE_BOTH), read the current GPIO value
  430. * (this couldn't be done in .irq_set_type because of atomic context)
  431. * to set the right irq trigger type.
  432. */
  433. if (pctl->irq_toggle_edge[reg] & mask) {
  434. if (stmfx_gpio_get(gpio_chip, data->hwirq))
  435. pctl->irq_gpi_type[reg] &= ~mask;
  436. else
  437. pctl->irq_gpi_type[reg] |= mask;
  438. }
  439. regmap_bulk_write(pctl->stmfx->map, STMFX_REG_IRQ_GPI_EVT,
  440. pctl->irq_gpi_evt, NR_GPIO_REGS);
  441. regmap_bulk_write(pctl->stmfx->map, STMFX_REG_IRQ_GPI_TYPE,
  442. pctl->irq_gpi_type, NR_GPIO_REGS);
  443. regmap_bulk_write(pctl->stmfx->map, STMFX_REG_IRQ_GPI_SRC,
  444. pctl->irq_gpi_src, NR_GPIO_REGS);
  445. mutex_unlock(&pctl->lock);
  446. }
  447. static int stmfx_gpio_irq_request_resources(struct irq_data *data)
  448. {
  449. struct gpio_chip *gpio_chip = irq_data_get_irq_chip_data(data);
  450. int ret;
  451. ret = stmfx_gpio_direction_input(gpio_chip, data->hwirq);
  452. if (ret)
  453. return ret;
  454. return gpiochip_reqres_irq(gpio_chip, data->hwirq);
  455. }
  456. static void stmfx_gpio_irq_release_resources(struct irq_data *data)
  457. {
  458. struct gpio_chip *gpio_chip = irq_data_get_irq_chip_data(data);
  459. return gpiochip_relres_irq(gpio_chip, data->hwirq);
  460. }
  461. static void stmfx_pinctrl_irq_toggle_trigger(struct stmfx_pinctrl *pctl,
  462. unsigned int offset)
  463. {
  464. u32 reg = get_reg(offset);
  465. u32 mask = get_mask(offset);
  466. int val;
  467. if (!(pctl->irq_toggle_edge[reg] & mask))
  468. return;
  469. val = stmfx_gpio_get(&pctl->gpio_chip, offset);
  470. if (val < 0)
  471. return;
  472. if (val) {
  473. pctl->irq_gpi_type[reg] &= mask;
  474. regmap_write_bits(pctl->stmfx->map,
  475. STMFX_REG_IRQ_GPI_TYPE + reg,
  476. mask, 0);
  477. } else {
  478. pctl->irq_gpi_type[reg] |= mask;
  479. regmap_write_bits(pctl->stmfx->map,
  480. STMFX_REG_IRQ_GPI_TYPE + reg,
  481. mask, mask);
  482. }
  483. }
  484. static irqreturn_t stmfx_pinctrl_irq_thread_fn(int irq, void *dev_id)
  485. {
  486. struct stmfx_pinctrl *pctl = (struct stmfx_pinctrl *)dev_id;
  487. struct gpio_chip *gc = &pctl->gpio_chip;
  488. u8 pending[NR_GPIO_REGS];
  489. u8 src[NR_GPIO_REGS] = {0, 0, 0};
  490. unsigned long n, status;
  491. int i, ret;
  492. ret = regmap_bulk_read(pctl->stmfx->map, STMFX_REG_IRQ_GPI_PENDING,
  493. &pending, NR_GPIO_REGS);
  494. if (ret)
  495. return IRQ_NONE;
  496. regmap_bulk_write(pctl->stmfx->map, STMFX_REG_IRQ_GPI_SRC,
  497. src, NR_GPIO_REGS);
  498. BUILD_BUG_ON(NR_GPIO_REGS > sizeof(status));
  499. for (i = 0, status = 0; i < NR_GPIO_REGS; i++)
  500. status |= (unsigned long)pending[i] << (i * 8);
  501. for_each_set_bit(n, &status, gc->ngpio) {
  502. handle_nested_irq(irq_find_mapping(gc->irq.domain, n));
  503. stmfx_pinctrl_irq_toggle_trigger(pctl, n);
  504. }
  505. regmap_bulk_write(pctl->stmfx->map, STMFX_REG_IRQ_GPI_SRC,
  506. pctl->irq_gpi_src, NR_GPIO_REGS);
  507. return IRQ_HANDLED;
  508. }
  509. static void stmfx_pinctrl_irq_print_chip(struct irq_data *d, struct seq_file *p)
  510. {
  511. struct gpio_chip *gpio_chip = irq_data_get_irq_chip_data(d);
  512. struct stmfx_pinctrl *pctl = gpiochip_get_data(gpio_chip);
  513. seq_printf(p, dev_name(pctl->dev));
  514. }
  515. static const struct irq_chip stmfx_pinctrl_irq_chip = {
  516. .irq_mask = stmfx_pinctrl_irq_mask,
  517. .irq_unmask = stmfx_pinctrl_irq_unmask,
  518. .irq_set_type = stmfx_pinctrl_irq_set_type,
  519. .irq_bus_lock = stmfx_pinctrl_irq_bus_lock,
  520. .irq_bus_sync_unlock = stmfx_pinctrl_irq_bus_sync_unlock,
  521. .irq_request_resources = stmfx_gpio_irq_request_resources,
  522. .irq_release_resources = stmfx_gpio_irq_release_resources,
  523. .irq_print_chip = stmfx_pinctrl_irq_print_chip,
  524. .flags = IRQCHIP_IMMUTABLE,
  525. };
  526. static int stmfx_pinctrl_gpio_function_enable(struct stmfx_pinctrl *pctl)
  527. {
  528. struct pinctrl_gpio_range *gpio_range;
  529. struct pinctrl_dev *pctl_dev = pctl->pctl_dev;
  530. u32 func = STMFX_FUNC_GPIO;
  531. pctl->gpio_valid_mask = GENMASK(15, 0);
  532. gpio_range = pinctrl_find_gpio_range_from_pin(pctl_dev, 16);
  533. if (gpio_range) {
  534. func |= STMFX_FUNC_ALTGPIO_LOW;
  535. pctl->gpio_valid_mask |= GENMASK(19, 16);
  536. }
  537. gpio_range = pinctrl_find_gpio_range_from_pin(pctl_dev, 20);
  538. if (gpio_range) {
  539. func |= STMFX_FUNC_ALTGPIO_HIGH;
  540. pctl->gpio_valid_mask |= GENMASK(23, 20);
  541. }
  542. return stmfx_function_enable(pctl->stmfx, func);
  543. }
  544. static int stmfx_pinctrl_probe(struct platform_device *pdev)
  545. {
  546. struct stmfx *stmfx = dev_get_drvdata(pdev->dev.parent);
  547. struct device_node *np = pdev->dev.of_node;
  548. struct stmfx_pinctrl *pctl;
  549. struct gpio_irq_chip *girq;
  550. int irq, ret;
  551. pctl = devm_kzalloc(stmfx->dev, sizeof(*pctl), GFP_KERNEL);
  552. if (!pctl)
  553. return -ENOMEM;
  554. platform_set_drvdata(pdev, pctl);
  555. pctl->dev = &pdev->dev;
  556. pctl->stmfx = stmfx;
  557. if (!of_property_present(np, "gpio-ranges")) {
  558. dev_err(pctl->dev, "missing required gpio-ranges property\n");
  559. return -EINVAL;
  560. }
  561. irq = platform_get_irq(pdev, 0);
  562. if (irq < 0)
  563. return irq;
  564. mutex_init(&pctl->lock);
  565. /* Register pin controller */
  566. pctl->pctl_desc.name = "stmfx-pinctrl";
  567. pctl->pctl_desc.pctlops = &stmfx_pinctrl_ops;
  568. pctl->pctl_desc.confops = &stmfx_pinconf_ops;
  569. pctl->pctl_desc.pins = stmfx_pins;
  570. pctl->pctl_desc.npins = ARRAY_SIZE(stmfx_pins);
  571. pctl->pctl_desc.owner = THIS_MODULE;
  572. pctl->pctl_desc.link_consumers = true;
  573. ret = devm_pinctrl_register_and_init(pctl->dev, &pctl->pctl_desc,
  574. pctl, &pctl->pctl_dev);
  575. if (ret) {
  576. dev_err(pctl->dev, "pinctrl registration failed\n");
  577. return ret;
  578. }
  579. ret = pinctrl_enable(pctl->pctl_dev);
  580. if (ret) {
  581. dev_err(pctl->dev, "pinctrl enable failed\n");
  582. return ret;
  583. }
  584. /* Register gpio controller */
  585. pctl->gpio_chip.label = "stmfx-gpio";
  586. pctl->gpio_chip.parent = pctl->dev;
  587. pctl->gpio_chip.get_direction = stmfx_gpio_get_direction;
  588. pctl->gpio_chip.direction_input = stmfx_gpio_direction_input;
  589. pctl->gpio_chip.direction_output = stmfx_gpio_direction_output;
  590. pctl->gpio_chip.get = stmfx_gpio_get;
  591. pctl->gpio_chip.set = stmfx_gpio_set;
  592. pctl->gpio_chip.set_config = gpiochip_generic_config;
  593. pctl->gpio_chip.base = -1;
  594. pctl->gpio_chip.ngpio = pctl->pctl_desc.npins;
  595. pctl->gpio_chip.can_sleep = true;
  596. girq = &pctl->gpio_chip.irq;
  597. gpio_irq_chip_set_chip(girq, &stmfx_pinctrl_irq_chip);
  598. /* This will let us handle the parent IRQ in the driver */
  599. girq->parent_handler = NULL;
  600. girq->num_parents = 0;
  601. girq->parents = NULL;
  602. girq->default_type = IRQ_TYPE_NONE;
  603. girq->handler = handle_bad_irq;
  604. girq->threaded = true;
  605. ret = devm_gpiochip_add_data(pctl->dev, &pctl->gpio_chip, pctl);
  606. if (ret) {
  607. dev_err(pctl->dev, "gpio_chip registration failed\n");
  608. return ret;
  609. }
  610. ret = stmfx_pinctrl_gpio_function_enable(pctl);
  611. if (ret)
  612. return ret;
  613. ret = devm_request_threaded_irq(pctl->dev, irq, NULL,
  614. stmfx_pinctrl_irq_thread_fn,
  615. IRQF_ONESHOT,
  616. dev_name(pctl->dev), pctl);
  617. if (ret) {
  618. dev_err(pctl->dev, "cannot request irq%d\n", irq);
  619. return ret;
  620. }
  621. dev_info(pctl->dev,
  622. "%ld GPIOs available\n", hweight_long(pctl->gpio_valid_mask));
  623. return 0;
  624. }
  625. static void stmfx_pinctrl_remove(struct platform_device *pdev)
  626. {
  627. struct stmfx *stmfx = dev_get_drvdata(pdev->dev.parent);
  628. int ret;
  629. ret = stmfx_function_disable(stmfx,
  630. STMFX_FUNC_GPIO |
  631. STMFX_FUNC_ALTGPIO_LOW |
  632. STMFX_FUNC_ALTGPIO_HIGH);
  633. if (ret)
  634. dev_err(&pdev->dev, "Failed to disable pins (%pe)\n",
  635. ERR_PTR(ret));
  636. }
  637. #ifdef CONFIG_PM_SLEEP
  638. static int stmfx_pinctrl_backup_regs(struct stmfx_pinctrl *pctl)
  639. {
  640. int ret;
  641. ret = regmap_bulk_read(pctl->stmfx->map, STMFX_REG_GPIO_STATE,
  642. &pctl->bkp_gpio_state, NR_GPIO_REGS);
  643. if (ret)
  644. return ret;
  645. ret = regmap_bulk_read(pctl->stmfx->map, STMFX_REG_GPIO_DIR,
  646. &pctl->bkp_gpio_dir, NR_GPIO_REGS);
  647. if (ret)
  648. return ret;
  649. ret = regmap_bulk_read(pctl->stmfx->map, STMFX_REG_GPIO_TYPE,
  650. &pctl->bkp_gpio_type, NR_GPIO_REGS);
  651. if (ret)
  652. return ret;
  653. ret = regmap_bulk_read(pctl->stmfx->map, STMFX_REG_GPIO_PUPD,
  654. &pctl->bkp_gpio_pupd, NR_GPIO_REGS);
  655. if (ret)
  656. return ret;
  657. return 0;
  658. }
  659. static int stmfx_pinctrl_restore_regs(struct stmfx_pinctrl *pctl)
  660. {
  661. int ret;
  662. ret = regmap_bulk_write(pctl->stmfx->map, STMFX_REG_GPIO_DIR,
  663. pctl->bkp_gpio_dir, NR_GPIO_REGS);
  664. if (ret)
  665. return ret;
  666. ret = regmap_bulk_write(pctl->stmfx->map, STMFX_REG_GPIO_TYPE,
  667. pctl->bkp_gpio_type, NR_GPIO_REGS);
  668. if (ret)
  669. return ret;
  670. ret = regmap_bulk_write(pctl->stmfx->map, STMFX_REG_GPIO_PUPD,
  671. pctl->bkp_gpio_pupd, NR_GPIO_REGS);
  672. if (ret)
  673. return ret;
  674. ret = regmap_bulk_write(pctl->stmfx->map, STMFX_REG_GPO_SET,
  675. pctl->bkp_gpio_state, NR_GPIO_REGS);
  676. if (ret)
  677. return ret;
  678. ret = regmap_bulk_write(pctl->stmfx->map, STMFX_REG_IRQ_GPI_EVT,
  679. pctl->irq_gpi_evt, NR_GPIO_REGS);
  680. if (ret)
  681. return ret;
  682. ret = regmap_bulk_write(pctl->stmfx->map, STMFX_REG_IRQ_GPI_TYPE,
  683. pctl->irq_gpi_type, NR_GPIO_REGS);
  684. if (ret)
  685. return ret;
  686. ret = regmap_bulk_write(pctl->stmfx->map, STMFX_REG_IRQ_GPI_SRC,
  687. pctl->irq_gpi_src, NR_GPIO_REGS);
  688. if (ret)
  689. return ret;
  690. return 0;
  691. }
  692. static int stmfx_pinctrl_suspend(struct device *dev)
  693. {
  694. struct stmfx_pinctrl *pctl = dev_get_drvdata(dev);
  695. int ret;
  696. ret = stmfx_pinctrl_backup_regs(pctl);
  697. if (ret) {
  698. dev_err(pctl->dev, "registers backup failure\n");
  699. return ret;
  700. }
  701. return 0;
  702. }
  703. static int stmfx_pinctrl_resume(struct device *dev)
  704. {
  705. struct stmfx_pinctrl *pctl = dev_get_drvdata(dev);
  706. int ret;
  707. ret = stmfx_pinctrl_restore_regs(pctl);
  708. if (ret) {
  709. dev_err(pctl->dev, "registers restoration failure\n");
  710. return ret;
  711. }
  712. return 0;
  713. }
  714. #endif
  715. static SIMPLE_DEV_PM_OPS(stmfx_pinctrl_dev_pm_ops,
  716. stmfx_pinctrl_suspend, stmfx_pinctrl_resume);
  717. static const struct of_device_id stmfx_pinctrl_of_match[] = {
  718. { .compatible = "st,stmfx-0300-pinctrl", },
  719. {},
  720. };
  721. MODULE_DEVICE_TABLE(of, stmfx_pinctrl_of_match);
  722. static struct platform_driver stmfx_pinctrl_driver = {
  723. .driver = {
  724. .name = "stmfx-pinctrl",
  725. .of_match_table = stmfx_pinctrl_of_match,
  726. .pm = &stmfx_pinctrl_dev_pm_ops,
  727. },
  728. .probe = stmfx_pinctrl_probe,
  729. .remove_new = stmfx_pinctrl_remove,
  730. };
  731. module_platform_driver(stmfx_pinctrl_driver);
  732. MODULE_DESCRIPTION("STMFX pinctrl/GPIO driver");
  733. MODULE_AUTHOR("Amelie Delaunay <amelie.delaunay@st.com>");
  734. MODULE_LICENSE("GPL v2");