gpio-stmpe.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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. static const char * const edge_det_values[] = {
  232. "edge-inactive",
  233. "edge-asserted",
  234. "not-supported"
  235. };
  236. static const char * const rise_values[] = {
  237. "no-rising-edge-detection",
  238. "rising-edge-detection",
  239. "not-supported"
  240. };
  241. static const char * const fall_values[] = {
  242. "no-falling-edge-detection",
  243. "falling-edge-detection",
  244. "not-supported"
  245. };
  246. #define NOT_SUPPORTED_IDX 2
  247. u8 edge_det = NOT_SUPPORTED_IDX;
  248. u8 rise = NOT_SUPPORTED_IDX;
  249. u8 fall = NOT_SUPPORTED_IDX;
  250. bool irqen;
  251. switch (stmpe->partnum) {
  252. case STMPE610:
  253. case STMPE811:
  254. case STMPE1601:
  255. case STMPE2401:
  256. case STMPE2403:
  257. edge_det_reg = stmpe->regs[STMPE_IDX_GPEDR_LSB + bank];
  258. ret = stmpe_reg_read(stmpe, edge_det_reg);
  259. if (ret < 0)
  260. return;
  261. edge_det = !!(ret & mask);
  262. /* fall through */
  263. case STMPE1801:
  264. rise_reg = stmpe->regs[STMPE_IDX_GPRER_LSB + bank];
  265. fall_reg = stmpe->regs[STMPE_IDX_GPFER_LSB + bank];
  266. ret = stmpe_reg_read(stmpe, rise_reg);
  267. if (ret < 0)
  268. return;
  269. rise = !!(ret & mask);
  270. ret = stmpe_reg_read(stmpe, fall_reg);
  271. if (ret < 0)
  272. return;
  273. fall = !!(ret & mask);
  274. /* fall through */
  275. case STMPE801:
  276. case STMPE1600:
  277. irqen_reg = stmpe->regs[STMPE_IDX_IEGPIOR_LSB + bank];
  278. break;
  279. default:
  280. return;
  281. }
  282. ret = stmpe_reg_read(stmpe, irqen_reg);
  283. if (ret < 0)
  284. return;
  285. irqen = !!(ret & mask);
  286. seq_printf(s, " gpio-%-3d (%-20.20s) in %s %13s %13s %25s %25s",
  287. gpio, label ?: "(none)",
  288. val ? "hi" : "lo",
  289. edge_det_values[edge_det],
  290. irqen ? "IRQ-enabled" : "IRQ-disabled",
  291. rise_values[rise],
  292. fall_values[fall]);
  293. }
  294. }
  295. static void stmpe_dbg_show(struct seq_file *s, struct gpio_chip *gc)
  296. {
  297. unsigned i;
  298. unsigned gpio = gc->base;
  299. for (i = 0; i < gc->ngpio; i++, gpio++) {
  300. stmpe_dbg_show_one(s, gc, i, gpio);
  301. seq_putc(s, '\n');
  302. }
  303. }
  304. static struct irq_chip stmpe_gpio_irq_chip = {
  305. .name = "stmpe-gpio",
  306. .irq_bus_lock = stmpe_gpio_irq_lock,
  307. .irq_bus_sync_unlock = stmpe_gpio_irq_sync_unlock,
  308. .irq_mask = stmpe_gpio_irq_mask,
  309. .irq_unmask = stmpe_gpio_irq_unmask,
  310. .irq_set_type = stmpe_gpio_irq_set_type,
  311. };
  312. static irqreturn_t stmpe_gpio_irq(int irq, void *dev)
  313. {
  314. struct stmpe_gpio *stmpe_gpio = dev;
  315. struct stmpe *stmpe = stmpe_gpio->stmpe;
  316. u8 statmsbreg;
  317. int num_banks = DIV_ROUND_UP(stmpe->num_gpios, 8);
  318. u8 status[num_banks];
  319. int ret;
  320. int i;
  321. /*
  322. * the stmpe_block_read() call below, imposes to set statmsbreg
  323. * with the register located at the lowest address. As STMPE1600
  324. * variant is the only one which respect registers address's order
  325. * (LSB regs located at lowest address than MSB ones) whereas all
  326. * the others have a registers layout with MSB located before the
  327. * LSB regs.
  328. */
  329. if (stmpe->partnum == STMPE1600)
  330. statmsbreg = stmpe->regs[STMPE_IDX_ISGPIOR_LSB];
  331. else
  332. statmsbreg = stmpe->regs[STMPE_IDX_ISGPIOR_MSB];
  333. ret = stmpe_block_read(stmpe, statmsbreg, num_banks, status);
  334. if (ret < 0)
  335. return IRQ_NONE;
  336. for (i = 0; i < num_banks; i++) {
  337. int bank = (stmpe_gpio->stmpe->partnum == STMPE1600) ? i :
  338. num_banks - i - 1;
  339. unsigned int enabled = stmpe_gpio->regs[REG_IE][bank];
  340. unsigned int stat = status[i];
  341. stat &= enabled;
  342. if (!stat)
  343. continue;
  344. while (stat) {
  345. int bit = __ffs(stat);
  346. int line = bank * 8 + bit;
  347. int child_irq = irq_find_mapping(stmpe_gpio->chip.irq.domain,
  348. line);
  349. handle_nested_irq(child_irq);
  350. stat &= ~BIT(bit);
  351. }
  352. /*
  353. * interrupt status register write has no effect on
  354. * 801/1801/1600, bits are cleared when read.
  355. * Edge detect register is not present on 801/1600/1801
  356. */
  357. if (stmpe->partnum != STMPE801 && stmpe->partnum != STMPE1600 &&
  358. stmpe->partnum != STMPE1801) {
  359. stmpe_reg_write(stmpe, statmsbreg + i, status[i]);
  360. stmpe_reg_write(stmpe,
  361. stmpe->regs[STMPE_IDX_GPEDR_MSB] + i,
  362. status[i]);
  363. }
  364. }
  365. return IRQ_HANDLED;
  366. }
  367. static int stmpe_gpio_probe(struct platform_device *pdev)
  368. {
  369. struct stmpe *stmpe = dev_get_drvdata(pdev->dev.parent);
  370. struct device_node *np = pdev->dev.of_node;
  371. struct stmpe_gpio *stmpe_gpio;
  372. int ret, irq;
  373. stmpe_gpio = kzalloc(sizeof(*stmpe_gpio), GFP_KERNEL);
  374. if (!stmpe_gpio)
  375. return -ENOMEM;
  376. mutex_init(&stmpe_gpio->irq_lock);
  377. stmpe_gpio->dev = &pdev->dev;
  378. stmpe_gpio->stmpe = stmpe;
  379. stmpe_gpio->chip = template_chip;
  380. stmpe_gpio->chip.ngpio = stmpe->num_gpios;
  381. stmpe_gpio->chip.parent = &pdev->dev;
  382. stmpe_gpio->chip.of_node = np;
  383. stmpe_gpio->chip.base = -1;
  384. if (IS_ENABLED(CONFIG_DEBUG_FS))
  385. stmpe_gpio->chip.dbg_show = stmpe_dbg_show;
  386. of_property_read_u32(np, "st,norequest-mask",
  387. &stmpe_gpio->norequest_mask);
  388. if (stmpe_gpio->norequest_mask)
  389. stmpe_gpio->chip.irq.need_valid_mask = true;
  390. irq = platform_get_irq(pdev, 0);
  391. if (irq < 0)
  392. dev_info(&pdev->dev,
  393. "device configured in no-irq mode: "
  394. "irqs are not available\n");
  395. ret = stmpe_enable(stmpe, STMPE_BLOCK_GPIO);
  396. if (ret)
  397. goto out_free;
  398. ret = gpiochip_add_data(&stmpe_gpio->chip, stmpe_gpio);
  399. if (ret) {
  400. dev_err(&pdev->dev, "unable to add gpiochip: %d\n", ret);
  401. goto out_disable;
  402. }
  403. if (irq > 0) {
  404. ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
  405. stmpe_gpio_irq, IRQF_ONESHOT,
  406. "stmpe-gpio", stmpe_gpio);
  407. if (ret) {
  408. dev_err(&pdev->dev, "unable to get irq: %d\n", ret);
  409. goto out_disable;
  410. }
  411. if (stmpe_gpio->norequest_mask) {
  412. int i;
  413. /* Forbid unused lines to be mapped as IRQs */
  414. for (i = 0; i < sizeof(u32); i++)
  415. if (stmpe_gpio->norequest_mask & BIT(i))
  416. clear_bit(i, stmpe_gpio->chip.irq.valid_mask);
  417. }
  418. ret = gpiochip_irqchip_add_nested(&stmpe_gpio->chip,
  419. &stmpe_gpio_irq_chip,
  420. 0,
  421. handle_simple_irq,
  422. IRQ_TYPE_NONE);
  423. if (ret) {
  424. dev_err(&pdev->dev,
  425. "could not connect irqchip to gpiochip\n");
  426. goto out_disable;
  427. }
  428. gpiochip_set_nested_irqchip(&stmpe_gpio->chip,
  429. &stmpe_gpio_irq_chip,
  430. irq);
  431. }
  432. platform_set_drvdata(pdev, stmpe_gpio);
  433. return 0;
  434. out_disable:
  435. stmpe_disable(stmpe, STMPE_BLOCK_GPIO);
  436. gpiochip_remove(&stmpe_gpio->chip);
  437. out_free:
  438. kfree(stmpe_gpio);
  439. return ret;
  440. }
  441. static struct platform_driver stmpe_gpio_driver = {
  442. .driver = {
  443. .suppress_bind_attrs = true,
  444. .name = "stmpe-gpio",
  445. },
  446. .probe = stmpe_gpio_probe,
  447. };
  448. static int __init stmpe_gpio_init(void)
  449. {
  450. return platform_driver_register(&stmpe_gpio_driver);
  451. }
  452. subsys_initcall(stmpe_gpio_init);