gpio-stmpe.c 11 KB

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