gpio-stmpe.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /*
  2. * Copyright (C) ST-Ericsson SA 2010
  3. *
  4. * License Terms: GNU General Public License, version 2
  5. * Author: Rabin Vincent <rabin.vincent@stericsson.com> for ST-Ericsson
  6. */
  7. #include <linux/module.h>
  8. #include <linux/init.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/slab.h>
  11. #include <linux/gpio.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/of.h>
  14. #include <linux/mfd/stmpe.h>
  15. #include <linux/seq_file.h>
  16. /*
  17. * These registers are modified under the irq bus lock and cached to avoid
  18. * unnecessary writes in bus_sync_unlock.
  19. */
  20. enum { REG_RE, REG_FE, REG_IE };
  21. #define CACHE_NR_REGS 3
  22. /* No variant has more than 24 GPIOs */
  23. #define CACHE_NR_BANKS (24 / 8)
  24. struct stmpe_gpio {
  25. struct gpio_chip chip;
  26. struct stmpe *stmpe;
  27. struct device *dev;
  28. struct mutex irq_lock;
  29. u32 norequest_mask;
  30. /* Caches of interrupt control registers for bus_lock */
  31. u8 regs[CACHE_NR_REGS][CACHE_NR_BANKS];
  32. u8 oldregs[CACHE_NR_REGS][CACHE_NR_BANKS];
  33. };
  34. static inline struct stmpe_gpio *to_stmpe_gpio(struct gpio_chip *chip)
  35. {
  36. return container_of(chip, struct stmpe_gpio, chip);
  37. }
  38. static int stmpe_gpio_get(struct gpio_chip *chip, unsigned offset)
  39. {
  40. struct stmpe_gpio *stmpe_gpio = to_stmpe_gpio(chip);
  41. struct stmpe *stmpe = stmpe_gpio->stmpe;
  42. u8 reg = stmpe->regs[STMPE_IDX_GPMR_LSB] - (offset / 8);
  43. u8 mask = 1 << (offset % 8);
  44. int ret;
  45. ret = stmpe_reg_read(stmpe, reg);
  46. if (ret < 0)
  47. return ret;
  48. return !!(ret & mask);
  49. }
  50. static void stmpe_gpio_set(struct gpio_chip *chip, unsigned offset, int val)
  51. {
  52. struct stmpe_gpio *stmpe_gpio = to_stmpe_gpio(chip);
  53. struct stmpe *stmpe = stmpe_gpio->stmpe;
  54. int which = val ? STMPE_IDX_GPSR_LSB : STMPE_IDX_GPCR_LSB;
  55. u8 reg = stmpe->regs[which] - (offset / 8);
  56. u8 mask = 1 << (offset % 8);
  57. /*
  58. * Some variants have single register for gpio set/clear functionality.
  59. * For them we need to write 0 to clear and 1 to set.
  60. */
  61. if (stmpe->regs[STMPE_IDX_GPSR_LSB] == stmpe->regs[STMPE_IDX_GPCR_LSB])
  62. stmpe_set_bits(stmpe, reg, mask, val ? mask : 0);
  63. else
  64. stmpe_reg_write(stmpe, reg, mask);
  65. }
  66. static int stmpe_gpio_direction_output(struct gpio_chip *chip,
  67. unsigned offset, int val)
  68. {
  69. struct stmpe_gpio *stmpe_gpio = to_stmpe_gpio(chip);
  70. struct stmpe *stmpe = stmpe_gpio->stmpe;
  71. u8 reg = stmpe->regs[STMPE_IDX_GPDR_LSB] - (offset / 8);
  72. u8 mask = 1 << (offset % 8);
  73. stmpe_gpio_set(chip, offset, val);
  74. return stmpe_set_bits(stmpe, reg, mask, mask);
  75. }
  76. static int stmpe_gpio_direction_input(struct gpio_chip *chip,
  77. unsigned offset)
  78. {
  79. struct stmpe_gpio *stmpe_gpio = to_stmpe_gpio(chip);
  80. struct stmpe *stmpe = stmpe_gpio->stmpe;
  81. u8 reg = stmpe->regs[STMPE_IDX_GPDR_LSB] - (offset / 8);
  82. u8 mask = 1 << (offset % 8);
  83. return stmpe_set_bits(stmpe, reg, mask, 0);
  84. }
  85. static int stmpe_gpio_request(struct gpio_chip *chip, unsigned offset)
  86. {
  87. struct stmpe_gpio *stmpe_gpio = to_stmpe_gpio(chip);
  88. struct stmpe *stmpe = stmpe_gpio->stmpe;
  89. if (stmpe_gpio->norequest_mask & (1 << offset))
  90. return -EINVAL;
  91. return stmpe_set_altfunc(stmpe, 1 << offset, STMPE_BLOCK_GPIO);
  92. }
  93. static struct gpio_chip template_chip = {
  94. .label = "stmpe",
  95. .owner = THIS_MODULE,
  96. .direction_input = stmpe_gpio_direction_input,
  97. .get = stmpe_gpio_get,
  98. .direction_output = stmpe_gpio_direction_output,
  99. .set = stmpe_gpio_set,
  100. .request = stmpe_gpio_request,
  101. .can_sleep = true,
  102. };
  103. static int stmpe_gpio_irq_set_type(struct irq_data *d, unsigned int type)
  104. {
  105. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  106. struct stmpe_gpio *stmpe_gpio = to_stmpe_gpio(gc);
  107. int offset = d->hwirq;
  108. int regoffset = offset / 8;
  109. int mask = 1 << (offset % 8);
  110. if (type & IRQ_TYPE_LEVEL_LOW || type & IRQ_TYPE_LEVEL_HIGH)
  111. return -EINVAL;
  112. /* STMPE801 doesn't have RE and FE registers */
  113. if (stmpe_gpio->stmpe->partnum == STMPE801)
  114. return 0;
  115. if (type & IRQ_TYPE_EDGE_RISING)
  116. stmpe_gpio->regs[REG_RE][regoffset] |= mask;
  117. else
  118. stmpe_gpio->regs[REG_RE][regoffset] &= ~mask;
  119. if (type & IRQ_TYPE_EDGE_FALLING)
  120. stmpe_gpio->regs[REG_FE][regoffset] |= mask;
  121. else
  122. stmpe_gpio->regs[REG_FE][regoffset] &= ~mask;
  123. return 0;
  124. }
  125. static void stmpe_gpio_irq_lock(struct irq_data *d)
  126. {
  127. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  128. struct stmpe_gpio *stmpe_gpio = to_stmpe_gpio(gc);
  129. mutex_lock(&stmpe_gpio->irq_lock);
  130. }
  131. static void stmpe_gpio_irq_sync_unlock(struct irq_data *d)
  132. {
  133. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  134. struct stmpe_gpio *stmpe_gpio = to_stmpe_gpio(gc);
  135. struct stmpe *stmpe = stmpe_gpio->stmpe;
  136. int num_banks = DIV_ROUND_UP(stmpe->num_gpios, 8);
  137. static const u8 regmap[] = {
  138. [REG_RE] = STMPE_IDX_GPRER_LSB,
  139. [REG_FE] = STMPE_IDX_GPFER_LSB,
  140. [REG_IE] = STMPE_IDX_IEGPIOR_LSB,
  141. };
  142. int i, j;
  143. for (i = 0; i < CACHE_NR_REGS; i++) {
  144. /* STMPE801 doesn't have RE and FE registers */
  145. if ((stmpe->partnum == STMPE801) &&
  146. (i != REG_IE))
  147. continue;
  148. for (j = 0; j < num_banks; j++) {
  149. u8 old = stmpe_gpio->oldregs[i][j];
  150. u8 new = stmpe_gpio->regs[i][j];
  151. if (new == old)
  152. continue;
  153. stmpe_gpio->oldregs[i][j] = new;
  154. stmpe_reg_write(stmpe, stmpe->regs[regmap[i]] - j, new);
  155. }
  156. }
  157. mutex_unlock(&stmpe_gpio->irq_lock);
  158. }
  159. static void stmpe_gpio_irq_mask(struct irq_data *d)
  160. {
  161. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  162. struct stmpe_gpio *stmpe_gpio = to_stmpe_gpio(gc);
  163. int offset = d->hwirq;
  164. int regoffset = offset / 8;
  165. int mask = 1 << (offset % 8);
  166. stmpe_gpio->regs[REG_IE][regoffset] &= ~mask;
  167. }
  168. static void stmpe_gpio_irq_unmask(struct irq_data *d)
  169. {
  170. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  171. struct stmpe_gpio *stmpe_gpio = to_stmpe_gpio(gc);
  172. int offset = d->hwirq;
  173. int regoffset = offset / 8;
  174. int mask = 1 << (offset % 8);
  175. stmpe_gpio->regs[REG_IE][regoffset] |= mask;
  176. }
  177. static void stmpe_dbg_show_one(struct seq_file *s,
  178. struct gpio_chip *gc,
  179. unsigned offset, unsigned gpio)
  180. {
  181. struct stmpe_gpio *stmpe_gpio = to_stmpe_gpio(gc);
  182. struct stmpe *stmpe = stmpe_gpio->stmpe;
  183. const char *label = gpiochip_is_requested(gc, offset);
  184. int num_banks = DIV_ROUND_UP(stmpe->num_gpios, 8);
  185. bool val = !!stmpe_gpio_get(gc, offset);
  186. u8 dir_reg = stmpe->regs[STMPE_IDX_GPDR_LSB] - (offset / 8);
  187. u8 mask = 1 << (offset % 8);
  188. int ret;
  189. u8 dir;
  190. ret = stmpe_reg_read(stmpe, dir_reg);
  191. if (ret < 0)
  192. return;
  193. dir = !!(ret & mask);
  194. if (dir) {
  195. seq_printf(s, " gpio-%-3d (%-20.20s) out %s",
  196. gpio, label ?: "(none)",
  197. val ? "hi" : "lo");
  198. } else {
  199. u8 edge_det_reg = stmpe->regs[STMPE_IDX_GPEDR_MSB] + num_banks - 1 - (offset / 8);
  200. u8 rise_reg = stmpe->regs[STMPE_IDX_GPRER_LSB] - (offset / 8);
  201. u8 fall_reg = stmpe->regs[STMPE_IDX_GPFER_LSB] - (offset / 8);
  202. u8 irqen_reg = stmpe->regs[STMPE_IDX_IEGPIOR_LSB] - (offset / 8);
  203. bool edge_det;
  204. bool rise;
  205. bool fall;
  206. bool irqen;
  207. ret = stmpe_reg_read(stmpe, edge_det_reg);
  208. if (ret < 0)
  209. return;
  210. edge_det = !!(ret & mask);
  211. ret = stmpe_reg_read(stmpe, rise_reg);
  212. if (ret < 0)
  213. return;
  214. rise = !!(ret & mask);
  215. ret = stmpe_reg_read(stmpe, fall_reg);
  216. if (ret < 0)
  217. return;
  218. fall = !!(ret & mask);
  219. ret = stmpe_reg_read(stmpe, irqen_reg);
  220. if (ret < 0)
  221. return;
  222. irqen = !!(ret & mask);
  223. seq_printf(s, " gpio-%-3d (%-20.20s) in %s %s %s%s%s",
  224. gpio, label ?: "(none)",
  225. val ? "hi" : "lo",
  226. edge_det ? "edge-asserted" : "edge-inactive",
  227. irqen ? "IRQ-enabled" : "",
  228. rise ? " rising-edge-detection" : "",
  229. fall ? " falling-edge-detection" : "");
  230. }
  231. }
  232. static void stmpe_dbg_show(struct seq_file *s, struct gpio_chip *gc)
  233. {
  234. unsigned i;
  235. unsigned gpio = gc->base;
  236. for (i = 0; i < gc->ngpio; i++, gpio++) {
  237. stmpe_dbg_show_one(s, gc, i, gpio);
  238. seq_printf(s, "\n");
  239. }
  240. }
  241. static struct irq_chip stmpe_gpio_irq_chip = {
  242. .name = "stmpe-gpio",
  243. .irq_bus_lock = stmpe_gpio_irq_lock,
  244. .irq_bus_sync_unlock = stmpe_gpio_irq_sync_unlock,
  245. .irq_mask = stmpe_gpio_irq_mask,
  246. .irq_unmask = stmpe_gpio_irq_unmask,
  247. .irq_set_type = stmpe_gpio_irq_set_type,
  248. };
  249. static irqreturn_t stmpe_gpio_irq(int irq, void *dev)
  250. {
  251. struct stmpe_gpio *stmpe_gpio = dev;
  252. struct stmpe *stmpe = stmpe_gpio->stmpe;
  253. u8 statmsbreg = stmpe->regs[STMPE_IDX_ISGPIOR_MSB];
  254. int num_banks = DIV_ROUND_UP(stmpe->num_gpios, 8);
  255. u8 status[num_banks];
  256. int ret;
  257. int i;
  258. ret = stmpe_block_read(stmpe, statmsbreg, num_banks, status);
  259. if (ret < 0)
  260. return IRQ_NONE;
  261. for (i = 0; i < num_banks; i++) {
  262. int bank = num_banks - i - 1;
  263. unsigned int enabled = stmpe_gpio->regs[REG_IE][bank];
  264. unsigned int stat = status[i];
  265. stat &= enabled;
  266. if (!stat)
  267. continue;
  268. while (stat) {
  269. int bit = __ffs(stat);
  270. int line = bank * 8 + bit;
  271. int child_irq = irq_find_mapping(stmpe_gpio->chip.irqdomain,
  272. line);
  273. handle_nested_irq(child_irq);
  274. stat &= ~(1 << bit);
  275. }
  276. stmpe_reg_write(stmpe, statmsbreg + i, status[i]);
  277. /* Edge detect register is not present on 801 */
  278. if (stmpe->partnum != STMPE801)
  279. stmpe_reg_write(stmpe, stmpe->regs[STMPE_IDX_GPEDR_MSB]
  280. + i, status[i]);
  281. }
  282. return IRQ_HANDLED;
  283. }
  284. static int stmpe_gpio_probe(struct platform_device *pdev)
  285. {
  286. struct stmpe *stmpe = dev_get_drvdata(pdev->dev.parent);
  287. struct device_node *np = pdev->dev.of_node;
  288. struct stmpe_gpio *stmpe_gpio;
  289. int ret;
  290. int irq = 0;
  291. irq = platform_get_irq(pdev, 0);
  292. stmpe_gpio = kzalloc(sizeof(struct stmpe_gpio), GFP_KERNEL);
  293. if (!stmpe_gpio)
  294. return -ENOMEM;
  295. mutex_init(&stmpe_gpio->irq_lock);
  296. stmpe_gpio->dev = &pdev->dev;
  297. stmpe_gpio->stmpe = stmpe;
  298. stmpe_gpio->chip = template_chip;
  299. stmpe_gpio->chip.ngpio = stmpe->num_gpios;
  300. stmpe_gpio->chip.dev = &pdev->dev;
  301. stmpe_gpio->chip.of_node = np;
  302. stmpe_gpio->chip.base = -1;
  303. if (IS_ENABLED(CONFIG_DEBUG_FS))
  304. stmpe_gpio->chip.dbg_show = stmpe_dbg_show;
  305. of_property_read_u32(np, "st,norequest-mask",
  306. &stmpe_gpio->norequest_mask);
  307. if (irq < 0)
  308. dev_info(&pdev->dev,
  309. "device configured in no-irq mode: "
  310. "irqs are not available\n");
  311. ret = stmpe_enable(stmpe, STMPE_BLOCK_GPIO);
  312. if (ret)
  313. goto out_free;
  314. ret = gpiochip_add(&stmpe_gpio->chip);
  315. if (ret) {
  316. dev_err(&pdev->dev, "unable to add gpiochip: %d\n", ret);
  317. goto out_disable;
  318. }
  319. if (irq > 0) {
  320. ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
  321. stmpe_gpio_irq, IRQF_ONESHOT,
  322. "stmpe-gpio", stmpe_gpio);
  323. if (ret) {
  324. dev_err(&pdev->dev, "unable to get irq: %d\n", ret);
  325. goto out_disable;
  326. }
  327. ret = gpiochip_irqchip_add(&stmpe_gpio->chip,
  328. &stmpe_gpio_irq_chip,
  329. 0,
  330. handle_simple_irq,
  331. IRQ_TYPE_NONE);
  332. if (ret) {
  333. dev_err(&pdev->dev,
  334. "could not connect irqchip to gpiochip\n");
  335. goto out_disable;
  336. }
  337. gpiochip_set_chained_irqchip(&stmpe_gpio->chip,
  338. &stmpe_gpio_irq_chip,
  339. irq,
  340. NULL);
  341. }
  342. platform_set_drvdata(pdev, stmpe_gpio);
  343. return 0;
  344. out_disable:
  345. stmpe_disable(stmpe, STMPE_BLOCK_GPIO);
  346. gpiochip_remove(&stmpe_gpio->chip);
  347. out_free:
  348. kfree(stmpe_gpio);
  349. return ret;
  350. }
  351. static int stmpe_gpio_remove(struct platform_device *pdev)
  352. {
  353. struct stmpe_gpio *stmpe_gpio = platform_get_drvdata(pdev);
  354. struct stmpe *stmpe = stmpe_gpio->stmpe;
  355. gpiochip_remove(&stmpe_gpio->chip);
  356. stmpe_disable(stmpe, STMPE_BLOCK_GPIO);
  357. kfree(stmpe_gpio);
  358. return 0;
  359. }
  360. static struct platform_driver stmpe_gpio_driver = {
  361. .driver.name = "stmpe-gpio",
  362. .driver.owner = THIS_MODULE,
  363. .probe = stmpe_gpio_probe,
  364. .remove = stmpe_gpio_remove,
  365. };
  366. static int __init stmpe_gpio_init(void)
  367. {
  368. return platform_driver_register(&stmpe_gpio_driver);
  369. }
  370. subsys_initcall(stmpe_gpio_init);
  371. static void __exit stmpe_gpio_exit(void)
  372. {
  373. platform_driver_unregister(&stmpe_gpio_driver);
  374. }
  375. module_exit(stmpe_gpio_exit);
  376. MODULE_LICENSE("GPL v2");
  377. MODULE_DESCRIPTION("STMPExxxx GPIO driver");
  378. MODULE_AUTHOR("Rabin Vincent <rabin.vincent@stericsson.com>");