gpio-stmpe.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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/init.h>
  8. #include <linux/platform_device.h>
  9. #include <linux/slab.h>
  10. #include <linux/gpio.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/of.h>
  13. #include <linux/mfd/stmpe.h>
  14. #include <linux/seq_file.h>
  15. #include <linux/bitops.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. enum { LSB, CSB, MSB };
  22. #define CACHE_NR_REGS 3
  23. /* No variant has more than 24 GPIOs */
  24. #define CACHE_NR_BANKS (24 / 8)
  25. struct stmpe_gpio {
  26. struct gpio_chip chip;
  27. struct stmpe *stmpe;
  28. struct device *dev;
  29. struct mutex irq_lock;
  30. u32 norequest_mask;
  31. /* Caches of interrupt control registers for bus_lock */
  32. u8 regs[CACHE_NR_REGS][CACHE_NR_BANKS];
  33. u8 oldregs[CACHE_NR_REGS][CACHE_NR_BANKS];
  34. };
  35. static int stmpe_gpio_get(struct gpio_chip *chip, unsigned offset)
  36. {
  37. struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(chip);
  38. struct stmpe *stmpe = stmpe_gpio->stmpe;
  39. u8 reg = stmpe->regs[STMPE_IDX_GPMR_LSB + (offset / 8)];
  40. u8 mask = BIT(offset % 8);
  41. int ret;
  42. ret = stmpe_reg_read(stmpe, reg);
  43. if (ret < 0)
  44. return ret;
  45. return !!(ret & mask);
  46. }
  47. static void stmpe_gpio_set(struct gpio_chip *chip, unsigned offset, int val)
  48. {
  49. struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(chip);
  50. struct stmpe *stmpe = stmpe_gpio->stmpe;
  51. int which = val ? STMPE_IDX_GPSR_LSB : STMPE_IDX_GPCR_LSB;
  52. u8 reg = stmpe->regs[which + (offset / 8)];
  53. u8 mask = BIT(offset % 8);
  54. /*
  55. * Some variants have single register for gpio set/clear functionality.
  56. * For them we need to write 0 to clear and 1 to set.
  57. */
  58. if (stmpe->regs[STMPE_IDX_GPSR_LSB] == stmpe->regs[STMPE_IDX_GPCR_LSB])
  59. stmpe_set_bits(stmpe, reg, mask, val ? mask : 0);
  60. else
  61. stmpe_reg_write(stmpe, reg, mask);
  62. }
  63. static int stmpe_gpio_get_direction(struct gpio_chip *chip,
  64. unsigned offset)
  65. {
  66. struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(chip);
  67. struct stmpe *stmpe = stmpe_gpio->stmpe;
  68. u8 reg = stmpe->regs[STMPE_IDX_GPDR_LSB] - (offset / 8);
  69. u8 mask = BIT(offset % 8);
  70. int ret;
  71. ret = stmpe_reg_read(stmpe, reg);
  72. if (ret < 0)
  73. return ret;
  74. return !(ret & mask);
  75. }
  76. static int stmpe_gpio_direction_output(struct gpio_chip *chip,
  77. unsigned offset, int val)
  78. {
  79. struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(chip);
  80. struct stmpe *stmpe = stmpe_gpio->stmpe;
  81. u8 reg = stmpe->regs[STMPE_IDX_GPDR_LSB + (offset / 8)];
  82. u8 mask = BIT(offset % 8);
  83. stmpe_gpio_set(chip, offset, val);
  84. return stmpe_set_bits(stmpe, reg, mask, mask);
  85. }
  86. static int stmpe_gpio_direction_input(struct gpio_chip *chip,
  87. unsigned offset)
  88. {
  89. struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(chip);
  90. struct stmpe *stmpe = stmpe_gpio->stmpe;
  91. u8 reg = stmpe->regs[STMPE_IDX_GPDR_LSB + (offset / 8)];
  92. u8 mask = BIT(offset % 8);
  93. return stmpe_set_bits(stmpe, reg, mask, 0);
  94. }
  95. static int stmpe_gpio_request(struct gpio_chip *chip, unsigned offset)
  96. {
  97. struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(chip);
  98. struct stmpe *stmpe = stmpe_gpio->stmpe;
  99. if (stmpe_gpio->norequest_mask & BIT(offset))
  100. return -EINVAL;
  101. return stmpe_set_altfunc(stmpe, BIT(offset), STMPE_BLOCK_GPIO);
  102. }
  103. static const struct gpio_chip template_chip = {
  104. .label = "stmpe",
  105. .owner = THIS_MODULE,
  106. .get_direction = stmpe_gpio_get_direction,
  107. .direction_input = stmpe_gpio_direction_input,
  108. .get = stmpe_gpio_get,
  109. .direction_output = stmpe_gpio_direction_output,
  110. .set = stmpe_gpio_set,
  111. .request = stmpe_gpio_request,
  112. .can_sleep = true,
  113. };
  114. static int stmpe_gpio_irq_set_type(struct irq_data *d, unsigned int type)
  115. {
  116. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  117. struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(gc);
  118. int offset = d->hwirq;
  119. int regoffset = offset / 8;
  120. int mask = BIT(offset % 8);
  121. if (type & IRQ_TYPE_LEVEL_LOW || type & IRQ_TYPE_LEVEL_HIGH)
  122. return -EINVAL;
  123. /* STMPE801 and STMPE 1600 don't have RE and FE registers */
  124. if (stmpe_gpio->stmpe->partnum == STMPE801 ||
  125. stmpe_gpio->stmpe->partnum == STMPE1600)
  126. return 0;
  127. if (type & IRQ_TYPE_EDGE_RISING)
  128. stmpe_gpio->regs[REG_RE][regoffset] |= mask;
  129. else
  130. stmpe_gpio->regs[REG_RE][regoffset] &= ~mask;
  131. if (type & IRQ_TYPE_EDGE_FALLING)
  132. stmpe_gpio->regs[REG_FE][regoffset] |= mask;
  133. else
  134. stmpe_gpio->regs[REG_FE][regoffset] &= ~mask;
  135. return 0;
  136. }
  137. static void stmpe_gpio_irq_lock(struct irq_data *d)
  138. {
  139. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  140. struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(gc);
  141. mutex_lock(&stmpe_gpio->irq_lock);
  142. }
  143. static void stmpe_gpio_irq_sync_unlock(struct irq_data *d)
  144. {
  145. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  146. struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(gc);
  147. struct stmpe *stmpe = stmpe_gpio->stmpe;
  148. int num_banks = DIV_ROUND_UP(stmpe->num_gpios, 8);
  149. static const u8 regmap[CACHE_NR_REGS][CACHE_NR_BANKS] = {
  150. [REG_RE][LSB] = STMPE_IDX_GPRER_LSB,
  151. [REG_RE][CSB] = STMPE_IDX_GPRER_CSB,
  152. [REG_RE][MSB] = STMPE_IDX_GPRER_MSB,
  153. [REG_FE][LSB] = STMPE_IDX_GPFER_LSB,
  154. [REG_FE][CSB] = STMPE_IDX_GPFER_CSB,
  155. [REG_FE][MSB] = STMPE_IDX_GPFER_MSB,
  156. [REG_IE][LSB] = STMPE_IDX_IEGPIOR_LSB,
  157. [REG_IE][CSB] = STMPE_IDX_IEGPIOR_CSB,
  158. [REG_IE][MSB] = STMPE_IDX_IEGPIOR_MSB,
  159. };
  160. int i, j;
  161. /*
  162. * STMPE1600: to be able to get IRQ from pins,
  163. * a read must be done on GPMR register, or a write in
  164. * GPSR or GPCR registers
  165. */
  166. if (stmpe->partnum == STMPE1600) {
  167. stmpe_reg_read(stmpe, stmpe->regs[STMPE_IDX_GPMR_LSB]);
  168. stmpe_reg_read(stmpe, stmpe->regs[STMPE_IDX_GPMR_CSB]);
  169. }
  170. for (i = 0; i < CACHE_NR_REGS; i++) {
  171. /* STMPE801 and STMPE1600 don't have RE and FE registers */
  172. if ((stmpe->partnum == STMPE801 ||
  173. stmpe->partnum == STMPE1600) &&
  174. (i != REG_IE))
  175. continue;
  176. for (j = 0; j < num_banks; j++) {
  177. u8 old = stmpe_gpio->oldregs[i][j];
  178. u8 new = stmpe_gpio->regs[i][j];
  179. if (new == old)
  180. continue;
  181. stmpe_gpio->oldregs[i][j] = new;
  182. stmpe_reg_write(stmpe, stmpe->regs[regmap[i][j]], new);
  183. }
  184. }
  185. mutex_unlock(&stmpe_gpio->irq_lock);
  186. }
  187. static void stmpe_gpio_irq_mask(struct irq_data *d)
  188. {
  189. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  190. struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(gc);
  191. int offset = d->hwirq;
  192. int regoffset = offset / 8;
  193. int mask = BIT(offset % 8);
  194. stmpe_gpio->regs[REG_IE][regoffset] &= ~mask;
  195. }
  196. static void stmpe_gpio_irq_unmask(struct irq_data *d)
  197. {
  198. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  199. struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(gc);
  200. int offset = d->hwirq;
  201. int regoffset = offset / 8;
  202. int mask = BIT(offset % 8);
  203. stmpe_gpio->regs[REG_IE][regoffset] |= mask;
  204. }
  205. static void stmpe_dbg_show_one(struct seq_file *s,
  206. struct gpio_chip *gc,
  207. unsigned offset, unsigned gpio)
  208. {
  209. struct stmpe_gpio *stmpe_gpio = gpiochip_get_data(gc);
  210. struct stmpe *stmpe = stmpe_gpio->stmpe;
  211. const char *label = gpiochip_is_requested(gc, offset);
  212. bool val = !!stmpe_gpio_get(gc, offset);
  213. u8 bank = offset / 8;
  214. u8 dir_reg = stmpe->regs[STMPE_IDX_GPDR_LSB + bank];
  215. u8 mask = BIT(offset % 8);
  216. int ret;
  217. u8 dir;
  218. ret = stmpe_reg_read(stmpe, dir_reg);
  219. if (ret < 0)
  220. return;
  221. dir = !!(ret & mask);
  222. if (dir) {
  223. seq_printf(s, " gpio-%-3d (%-20.20s) out %s",
  224. gpio, label ?: "(none)",
  225. val ? "hi" : "lo");
  226. } else {
  227. u8 edge_det_reg;
  228. u8 rise_reg;
  229. u8 fall_reg;
  230. u8 irqen_reg;
  231. char *edge_det_values[] = {"edge-inactive",
  232. "edge-asserted",
  233. "not-supported"};
  234. char *rise_values[] = {"no-rising-edge-detection",
  235. "rising-edge-detection",
  236. "not-supported"};
  237. char *fall_values[] = {"no-falling-edge-detection",
  238. "falling-edge-detection",
  239. "not-supported"};
  240. #define NOT_SUPPORTED_IDX 2
  241. u8 edge_det = NOT_SUPPORTED_IDX;
  242. u8 rise = NOT_SUPPORTED_IDX;
  243. u8 fall = NOT_SUPPORTED_IDX;
  244. bool irqen;
  245. switch (stmpe->partnum) {
  246. case STMPE610:
  247. case STMPE811:
  248. case STMPE1601:
  249. case STMPE2401:
  250. case STMPE2403:
  251. edge_det_reg = stmpe->regs[STMPE_IDX_GPEDR_LSB + bank];
  252. ret = stmpe_reg_read(stmpe, edge_det_reg);
  253. if (ret < 0)
  254. return;
  255. edge_det = !!(ret & mask);
  256. case STMPE1801:
  257. rise_reg = stmpe->regs[STMPE_IDX_GPRER_LSB + bank];
  258. fall_reg = stmpe->regs[STMPE_IDX_GPFER_LSB + bank];
  259. ret = stmpe_reg_read(stmpe, rise_reg);
  260. if (ret < 0)
  261. return;
  262. rise = !!(ret & mask);
  263. ret = stmpe_reg_read(stmpe, fall_reg);
  264. if (ret < 0)
  265. return;
  266. fall = !!(ret & mask);
  267. case STMPE801:
  268. case STMPE1600:
  269. irqen_reg = stmpe->regs[STMPE_IDX_IEGPIOR_LSB + bank];
  270. break;
  271. default:
  272. return;
  273. }
  274. ret = stmpe_reg_read(stmpe, irqen_reg);
  275. if (ret < 0)
  276. return;
  277. irqen = !!(ret & mask);
  278. seq_printf(s, " gpio-%-3d (%-20.20s) in %s %13s %13s %25s %25s",
  279. gpio, label ?: "(none)",
  280. val ? "hi" : "lo",
  281. edge_det_values[edge_det],
  282. irqen ? "IRQ-enabled" : "IRQ-disabled",
  283. rise_values[rise],
  284. fall_values[fall]);
  285. }
  286. }
  287. static void stmpe_dbg_show(struct seq_file *s, struct gpio_chip *gc)
  288. {
  289. unsigned i;
  290. unsigned gpio = gc->base;
  291. for (i = 0; i < gc->ngpio; i++, gpio++) {
  292. stmpe_dbg_show_one(s, gc, i, gpio);
  293. seq_printf(s, "\n");
  294. }
  295. }
  296. static struct irq_chip stmpe_gpio_irq_chip = {
  297. .name = "stmpe-gpio",
  298. .irq_bus_lock = stmpe_gpio_irq_lock,
  299. .irq_bus_sync_unlock = stmpe_gpio_irq_sync_unlock,
  300. .irq_mask = stmpe_gpio_irq_mask,
  301. .irq_unmask = stmpe_gpio_irq_unmask,
  302. .irq_set_type = stmpe_gpio_irq_set_type,
  303. };
  304. static irqreturn_t stmpe_gpio_irq(int irq, void *dev)
  305. {
  306. struct stmpe_gpio *stmpe_gpio = dev;
  307. struct stmpe *stmpe = stmpe_gpio->stmpe;
  308. u8 statmsbreg;
  309. int num_banks = DIV_ROUND_UP(stmpe->num_gpios, 8);
  310. u8 status[num_banks];
  311. int ret;
  312. int i;
  313. /*
  314. * the stmpe_block_read() call below, imposes to set statmsbreg
  315. * with the register located at the lowest address. As STMPE1600
  316. * variant is the only one which respect registers address's order
  317. * (LSB regs located at lowest address than MSB ones) whereas all
  318. * the others have a registers layout with MSB located before the
  319. * LSB regs.
  320. */
  321. if (stmpe->partnum == STMPE1600)
  322. statmsbreg = stmpe->regs[STMPE_IDX_ISGPIOR_LSB];
  323. else
  324. statmsbreg = stmpe->regs[STMPE_IDX_ISGPIOR_MSB];
  325. ret = stmpe_block_read(stmpe, statmsbreg, num_banks, status);
  326. if (ret < 0)
  327. return IRQ_NONE;
  328. for (i = 0; i < num_banks; i++) {
  329. int bank = (stmpe_gpio->stmpe->partnum == STMPE1600) ? i :
  330. num_banks - i - 1;
  331. unsigned int enabled = stmpe_gpio->regs[REG_IE][bank];
  332. unsigned int stat = status[i];
  333. stat &= enabled;
  334. if (!stat)
  335. continue;
  336. while (stat) {
  337. int bit = __ffs(stat);
  338. int line = bank * 8 + bit;
  339. int child_irq = irq_find_mapping(stmpe_gpio->chip.irqdomain,
  340. line);
  341. handle_nested_irq(child_irq);
  342. stat &= ~BIT(bit);
  343. }
  344. /*
  345. * interrupt status register write has no effect on
  346. * 801/1801/1600, bits are cleared when read.
  347. * Edge detect register is not present on 801/1600/1801
  348. */
  349. if (stmpe->partnum != STMPE801 && stmpe->partnum != STMPE1600 &&
  350. stmpe->partnum != STMPE1801) {
  351. stmpe_reg_write(stmpe, statmsbreg + i, status[i]);
  352. stmpe_reg_write(stmpe,
  353. stmpe->regs[STMPE_IDX_GPEDR_MSB] + i,
  354. status[i]);
  355. }
  356. }
  357. return IRQ_HANDLED;
  358. }
  359. static int stmpe_gpio_probe(struct platform_device *pdev)
  360. {
  361. struct stmpe *stmpe = dev_get_drvdata(pdev->dev.parent);
  362. struct device_node *np = pdev->dev.of_node;
  363. struct stmpe_gpio *stmpe_gpio;
  364. int ret;
  365. int irq = 0;
  366. irq = platform_get_irq(pdev, 0);
  367. stmpe_gpio = kzalloc(sizeof(struct stmpe_gpio), GFP_KERNEL);
  368. if (!stmpe_gpio)
  369. return -ENOMEM;
  370. mutex_init(&stmpe_gpio->irq_lock);
  371. stmpe_gpio->dev = &pdev->dev;
  372. stmpe_gpio->stmpe = stmpe;
  373. stmpe_gpio->chip = template_chip;
  374. stmpe_gpio->chip.ngpio = stmpe->num_gpios;
  375. stmpe_gpio->chip.parent = &pdev->dev;
  376. stmpe_gpio->chip.of_node = np;
  377. stmpe_gpio->chip.base = -1;
  378. if (IS_ENABLED(CONFIG_DEBUG_FS))
  379. stmpe_gpio->chip.dbg_show = stmpe_dbg_show;
  380. of_property_read_u32(np, "st,norequest-mask",
  381. &stmpe_gpio->norequest_mask);
  382. if (stmpe_gpio->norequest_mask)
  383. stmpe_gpio->chip.irq_need_valid_mask = true;
  384. if (irq < 0)
  385. dev_info(&pdev->dev,
  386. "device configured in no-irq mode: "
  387. "irqs are not available\n");
  388. ret = stmpe_enable(stmpe, STMPE_BLOCK_GPIO);
  389. if (ret)
  390. goto out_free;
  391. ret = gpiochip_add_data(&stmpe_gpio->chip, stmpe_gpio);
  392. if (ret) {
  393. dev_err(&pdev->dev, "unable to add gpiochip: %d\n", ret);
  394. goto out_disable;
  395. }
  396. if (irq > 0) {
  397. ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
  398. stmpe_gpio_irq, IRQF_ONESHOT,
  399. "stmpe-gpio", stmpe_gpio);
  400. if (ret) {
  401. dev_err(&pdev->dev, "unable to get irq: %d\n", ret);
  402. goto out_disable;
  403. }
  404. if (stmpe_gpio->norequest_mask) {
  405. int i;
  406. /* Forbid unused lines to be mapped as IRQs */
  407. for (i = 0; i < sizeof(u32); i++)
  408. if (stmpe_gpio->norequest_mask & BIT(i))
  409. clear_bit(i, stmpe_gpio->chip.irq_valid_mask);
  410. }
  411. ret = gpiochip_irqchip_add(&stmpe_gpio->chip,
  412. &stmpe_gpio_irq_chip,
  413. 0,
  414. handle_simple_irq,
  415. IRQ_TYPE_NONE);
  416. if (ret) {
  417. dev_err(&pdev->dev,
  418. "could not connect irqchip to gpiochip\n");
  419. goto out_disable;
  420. }
  421. gpiochip_set_chained_irqchip(&stmpe_gpio->chip,
  422. &stmpe_gpio_irq_chip,
  423. irq,
  424. NULL);
  425. }
  426. platform_set_drvdata(pdev, stmpe_gpio);
  427. return 0;
  428. out_disable:
  429. stmpe_disable(stmpe, STMPE_BLOCK_GPIO);
  430. gpiochip_remove(&stmpe_gpio->chip);
  431. out_free:
  432. kfree(stmpe_gpio);
  433. return ret;
  434. }
  435. static struct platform_driver stmpe_gpio_driver = {
  436. .driver = {
  437. .suppress_bind_attrs = true,
  438. .name = "stmpe-gpio",
  439. },
  440. .probe = stmpe_gpio_probe,
  441. };
  442. static int __init stmpe_gpio_init(void)
  443. {
  444. return platform_driver_register(&stmpe_gpio_driver);
  445. }
  446. subsys_initcall(stmpe_gpio_init);