gpio-tc3589x.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /*
  2. * Copyright (C) ST-Ericsson SA 2010
  3. *
  4. * License Terms: GNU General Public License, version 2
  5. * Author: Hanumath Prasad <hanumath.prasad@stericsson.com> for ST-Ericsson
  6. * Author: Rabin Vincent <rabin.vincent@stericsson.com> for ST-Ericsson
  7. */
  8. #include <linux/module.h>
  9. #include <linux/init.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/slab.h>
  12. #include <linux/gpio.h>
  13. #include <linux/irq.h>
  14. #include <linux/irqdomain.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/mfd/tc3589x.h>
  17. /*
  18. * These registers are modified under the irq bus lock and cached to avoid
  19. * unnecessary writes in bus_sync_unlock.
  20. */
  21. enum { REG_IBE, REG_IEV, REG_IS, REG_IE };
  22. #define CACHE_NR_REGS 4
  23. #define CACHE_NR_BANKS 3
  24. struct tc3589x_gpio {
  25. struct gpio_chip chip;
  26. struct tc3589x *tc3589x;
  27. struct device *dev;
  28. struct mutex irq_lock;
  29. struct irq_domain *domain;
  30. int irq_base;
  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 inline struct tc3589x_gpio *to_tc3589x_gpio(struct gpio_chip *chip)
  36. {
  37. return container_of(chip, struct tc3589x_gpio, chip);
  38. }
  39. static int tc3589x_gpio_get(struct gpio_chip *chip, unsigned offset)
  40. {
  41. struct tc3589x_gpio *tc3589x_gpio = to_tc3589x_gpio(chip);
  42. struct tc3589x *tc3589x = tc3589x_gpio->tc3589x;
  43. u8 reg = TC3589x_GPIODATA0 + (offset / 8) * 2;
  44. u8 mask = 1 << (offset % 8);
  45. int ret;
  46. ret = tc3589x_reg_read(tc3589x, reg);
  47. if (ret < 0)
  48. return ret;
  49. return ret & mask;
  50. }
  51. static void tc3589x_gpio_set(struct gpio_chip *chip, unsigned offset, int val)
  52. {
  53. struct tc3589x_gpio *tc3589x_gpio = to_tc3589x_gpio(chip);
  54. struct tc3589x *tc3589x = tc3589x_gpio->tc3589x;
  55. u8 reg = TC3589x_GPIODATA0 + (offset / 8) * 2;
  56. unsigned pos = offset % 8;
  57. u8 data[] = {!!val << pos, 1 << pos};
  58. tc3589x_block_write(tc3589x, reg, ARRAY_SIZE(data), data);
  59. }
  60. static int tc3589x_gpio_direction_output(struct gpio_chip *chip,
  61. unsigned offset, int val)
  62. {
  63. struct tc3589x_gpio *tc3589x_gpio = to_tc3589x_gpio(chip);
  64. struct tc3589x *tc3589x = tc3589x_gpio->tc3589x;
  65. u8 reg = TC3589x_GPIODIR0 + offset / 8;
  66. unsigned pos = offset % 8;
  67. tc3589x_gpio_set(chip, offset, val);
  68. return tc3589x_set_bits(tc3589x, reg, 1 << pos, 1 << pos);
  69. }
  70. static int tc3589x_gpio_direction_input(struct gpio_chip *chip,
  71. unsigned offset)
  72. {
  73. struct tc3589x_gpio *tc3589x_gpio = to_tc3589x_gpio(chip);
  74. struct tc3589x *tc3589x = tc3589x_gpio->tc3589x;
  75. u8 reg = TC3589x_GPIODIR0 + offset / 8;
  76. unsigned pos = offset % 8;
  77. return tc3589x_set_bits(tc3589x, reg, 1 << pos, 0);
  78. }
  79. /**
  80. * tc3589x_gpio_irq_get_virq(): Map an interrupt on a chip to a virtual IRQ
  81. *
  82. * @tc3589x_gpio: tc3589x_gpio_irq controller to operate on.
  83. * @irq: index of the interrupt requested in the chip IRQs
  84. *
  85. * Useful for drivers to request their own IRQs.
  86. */
  87. static int tc3589x_gpio_irq_get_virq(struct tc3589x_gpio *tc3589x_gpio,
  88. int irq)
  89. {
  90. if (!tc3589x_gpio)
  91. return -EINVAL;
  92. return irq_create_mapping(tc3589x_gpio->domain, irq);
  93. }
  94. static int tc3589x_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  95. {
  96. struct tc3589x_gpio *tc3589x_gpio = to_tc3589x_gpio(chip);
  97. return tc3589x_gpio_irq_get_virq(tc3589x_gpio, offset);
  98. }
  99. static struct gpio_chip template_chip = {
  100. .label = "tc3589x",
  101. .owner = THIS_MODULE,
  102. .direction_input = tc3589x_gpio_direction_input,
  103. .get = tc3589x_gpio_get,
  104. .direction_output = tc3589x_gpio_direction_output,
  105. .set = tc3589x_gpio_set,
  106. .to_irq = tc3589x_gpio_to_irq,
  107. .can_sleep = 1,
  108. };
  109. static int tc3589x_gpio_irq_set_type(struct irq_data *d, unsigned int type)
  110. {
  111. struct tc3589x_gpio *tc3589x_gpio = irq_data_get_irq_chip_data(d);
  112. int offset = d->hwirq;
  113. int regoffset = offset / 8;
  114. int mask = 1 << (offset % 8);
  115. if (type == IRQ_TYPE_EDGE_BOTH) {
  116. tc3589x_gpio->regs[REG_IBE][regoffset] |= mask;
  117. return 0;
  118. }
  119. tc3589x_gpio->regs[REG_IBE][regoffset] &= ~mask;
  120. if (type == IRQ_TYPE_LEVEL_LOW || type == IRQ_TYPE_LEVEL_HIGH)
  121. tc3589x_gpio->regs[REG_IS][regoffset] |= mask;
  122. else
  123. tc3589x_gpio->regs[REG_IS][regoffset] &= ~mask;
  124. if (type == IRQ_TYPE_EDGE_RISING || type == IRQ_TYPE_LEVEL_HIGH)
  125. tc3589x_gpio->regs[REG_IEV][regoffset] |= mask;
  126. else
  127. tc3589x_gpio->regs[REG_IEV][regoffset] &= ~mask;
  128. return 0;
  129. }
  130. static void tc3589x_gpio_irq_lock(struct irq_data *d)
  131. {
  132. struct tc3589x_gpio *tc3589x_gpio = irq_data_get_irq_chip_data(d);
  133. mutex_lock(&tc3589x_gpio->irq_lock);
  134. }
  135. static void tc3589x_gpio_irq_sync_unlock(struct irq_data *d)
  136. {
  137. struct tc3589x_gpio *tc3589x_gpio = irq_data_get_irq_chip_data(d);
  138. struct tc3589x *tc3589x = tc3589x_gpio->tc3589x;
  139. static const u8 regmap[] = {
  140. [REG_IBE] = TC3589x_GPIOIBE0,
  141. [REG_IEV] = TC3589x_GPIOIEV0,
  142. [REG_IS] = TC3589x_GPIOIS0,
  143. [REG_IE] = TC3589x_GPIOIE0,
  144. };
  145. int i, j;
  146. for (i = 0; i < CACHE_NR_REGS; i++) {
  147. for (j = 0; j < CACHE_NR_BANKS; j++) {
  148. u8 old = tc3589x_gpio->oldregs[i][j];
  149. u8 new = tc3589x_gpio->regs[i][j];
  150. if (new == old)
  151. continue;
  152. tc3589x_gpio->oldregs[i][j] = new;
  153. tc3589x_reg_write(tc3589x, regmap[i] + j * 8, new);
  154. }
  155. }
  156. mutex_unlock(&tc3589x_gpio->irq_lock);
  157. }
  158. static void tc3589x_gpio_irq_mask(struct irq_data *d)
  159. {
  160. struct tc3589x_gpio *tc3589x_gpio = irq_data_get_irq_chip_data(d);
  161. int offset = d->hwirq;
  162. int regoffset = offset / 8;
  163. int mask = 1 << (offset % 8);
  164. tc3589x_gpio->regs[REG_IE][regoffset] &= ~mask;
  165. }
  166. static void tc3589x_gpio_irq_unmask(struct irq_data *d)
  167. {
  168. struct tc3589x_gpio *tc3589x_gpio = irq_data_get_irq_chip_data(d);
  169. int offset = d->hwirq;
  170. int regoffset = offset / 8;
  171. int mask = 1 << (offset % 8);
  172. tc3589x_gpio->regs[REG_IE][regoffset] |= mask;
  173. }
  174. static struct irq_chip tc3589x_gpio_irq_chip = {
  175. .name = "tc3589x-gpio",
  176. .irq_bus_lock = tc3589x_gpio_irq_lock,
  177. .irq_bus_sync_unlock = tc3589x_gpio_irq_sync_unlock,
  178. .irq_mask = tc3589x_gpio_irq_mask,
  179. .irq_unmask = tc3589x_gpio_irq_unmask,
  180. .irq_set_type = tc3589x_gpio_irq_set_type,
  181. };
  182. static irqreturn_t tc3589x_gpio_irq(int irq, void *dev)
  183. {
  184. struct tc3589x_gpio *tc3589x_gpio = dev;
  185. struct tc3589x *tc3589x = tc3589x_gpio->tc3589x;
  186. u8 status[CACHE_NR_BANKS];
  187. int ret;
  188. int i;
  189. ret = tc3589x_block_read(tc3589x, TC3589x_GPIOMIS0,
  190. ARRAY_SIZE(status), status);
  191. if (ret < 0)
  192. return IRQ_NONE;
  193. for (i = 0; i < ARRAY_SIZE(status); i++) {
  194. unsigned int stat = status[i];
  195. if (!stat)
  196. continue;
  197. while (stat) {
  198. int bit = __ffs(stat);
  199. int line = i * 8 + bit;
  200. int virq = tc3589x_gpio_irq_get_virq(tc3589x_gpio, line);
  201. handle_nested_irq(virq);
  202. stat &= ~(1 << bit);
  203. }
  204. tc3589x_reg_write(tc3589x, TC3589x_GPIOIC0 + i, status[i]);
  205. }
  206. return IRQ_HANDLED;
  207. }
  208. static int tc3589x_gpio_irq_map(struct irq_domain *d, unsigned int virq,
  209. irq_hw_number_t hwirq)
  210. {
  211. struct tc3589x *tc3589x_gpio = d->host_data;
  212. irq_set_chip_data(virq, tc3589x_gpio);
  213. irq_set_chip_and_handler(virq, &tc3589x_gpio_irq_chip,
  214. handle_simple_irq);
  215. irq_set_nested_thread(virq, 1);
  216. #ifdef CONFIG_ARM
  217. set_irq_flags(virq, IRQF_VALID);
  218. #else
  219. irq_set_noprobe(virq);
  220. #endif
  221. return 0;
  222. }
  223. static void tc3589x_gpio_irq_unmap(struct irq_domain *d, unsigned int virq)
  224. {
  225. #ifdef CONFIG_ARM
  226. set_irq_flags(virq, 0);
  227. #endif
  228. irq_set_chip_and_handler(virq, NULL, NULL);
  229. irq_set_chip_data(virq, NULL);
  230. }
  231. static struct irq_domain_ops tc3589x_irq_ops = {
  232. .map = tc3589x_gpio_irq_map,
  233. .unmap = tc3589x_gpio_irq_unmap,
  234. .xlate = irq_domain_xlate_twocell,
  235. };
  236. static int tc3589x_gpio_irq_init(struct tc3589x_gpio *tc3589x_gpio)
  237. {
  238. int base = tc3589x_gpio->irq_base;
  239. if (base) {
  240. tc3589x_gpio->domain = irq_domain_add_legacy(
  241. NULL, tc3589x_gpio->chip.ngpio, base,
  242. 0, &tc3589x_irq_ops, tc3589x_gpio);
  243. }
  244. else {
  245. tc3589x_gpio->domain = irq_domain_add_linear(
  246. NULL, tc3589x_gpio->chip.ngpio,
  247. &tc3589x_irq_ops, tc3589x_gpio);
  248. }
  249. if (!tc3589x_gpio->domain) {
  250. dev_err(tc3589x_gpio->dev, "Failed to create irqdomain\n");
  251. return -ENOSYS;
  252. }
  253. return 0;
  254. }
  255. static int __devinit tc3589x_gpio_probe(struct platform_device *pdev)
  256. {
  257. struct tc3589x *tc3589x = dev_get_drvdata(pdev->dev.parent);
  258. struct tc3589x_gpio_platform_data *pdata;
  259. struct tc3589x_gpio *tc3589x_gpio;
  260. int ret;
  261. int irq;
  262. pdata = tc3589x->pdata->gpio;
  263. if (!pdata)
  264. return -ENODEV;
  265. irq = platform_get_irq(pdev, 0);
  266. if (irq < 0)
  267. return irq;
  268. tc3589x_gpio = kzalloc(sizeof(struct tc3589x_gpio), GFP_KERNEL);
  269. if (!tc3589x_gpio)
  270. return -ENOMEM;
  271. mutex_init(&tc3589x_gpio->irq_lock);
  272. tc3589x_gpio->dev = &pdev->dev;
  273. tc3589x_gpio->tc3589x = tc3589x;
  274. tc3589x_gpio->chip = template_chip;
  275. tc3589x_gpio->chip.ngpio = tc3589x->num_gpio;
  276. tc3589x_gpio->chip.dev = &pdev->dev;
  277. tc3589x_gpio->chip.base = pdata->gpio_base;
  278. tc3589x_gpio->irq_base = tc3589x->irq_base + TC3589x_INT_GPIO(0);
  279. tc3589x_gpio->irq_base = tc3589x->irq_base ?
  280. tc3589x->irq_base + TC3589x_INT_GPIO(0) : 0;
  281. /* Bring the GPIO module out of reset */
  282. ret = tc3589x_set_bits(tc3589x, TC3589x_RSTCTRL,
  283. TC3589x_RSTCTRL_GPIRST, 0);
  284. if (ret < 0)
  285. goto out_free;
  286. ret = tc3589x_gpio_irq_init(tc3589x_gpio);
  287. if (ret)
  288. goto out_free;
  289. ret = request_threaded_irq(irq, NULL, tc3589x_gpio_irq, IRQF_ONESHOT,
  290. "tc3589x-gpio", tc3589x_gpio);
  291. if (ret) {
  292. dev_err(&pdev->dev, "unable to get irq: %d\n", ret);
  293. goto out_free;
  294. }
  295. ret = gpiochip_add(&tc3589x_gpio->chip);
  296. if (ret) {
  297. dev_err(&pdev->dev, "unable to add gpiochip: %d\n", ret);
  298. goto out_freeirq;
  299. }
  300. if (pdata->setup)
  301. pdata->setup(tc3589x, tc3589x_gpio->chip.base);
  302. platform_set_drvdata(pdev, tc3589x_gpio);
  303. return 0;
  304. out_freeirq:
  305. free_irq(irq, tc3589x_gpio);
  306. out_free:
  307. kfree(tc3589x_gpio);
  308. return ret;
  309. }
  310. static int __devexit tc3589x_gpio_remove(struct platform_device *pdev)
  311. {
  312. struct tc3589x_gpio *tc3589x_gpio = platform_get_drvdata(pdev);
  313. struct tc3589x *tc3589x = tc3589x_gpio->tc3589x;
  314. struct tc3589x_gpio_platform_data *pdata = tc3589x->pdata->gpio;
  315. int irq = platform_get_irq(pdev, 0);
  316. int ret;
  317. if (pdata->remove)
  318. pdata->remove(tc3589x, tc3589x_gpio->chip.base);
  319. ret = gpiochip_remove(&tc3589x_gpio->chip);
  320. if (ret < 0) {
  321. dev_err(tc3589x_gpio->dev,
  322. "unable to remove gpiochip: %d\n", ret);
  323. return ret;
  324. }
  325. free_irq(irq, tc3589x_gpio);
  326. platform_set_drvdata(pdev, NULL);
  327. kfree(tc3589x_gpio);
  328. return 0;
  329. }
  330. static struct platform_driver tc3589x_gpio_driver = {
  331. .driver.name = "tc3589x-gpio",
  332. .driver.owner = THIS_MODULE,
  333. .probe = tc3589x_gpio_probe,
  334. .remove = __devexit_p(tc3589x_gpio_remove),
  335. };
  336. static int __init tc3589x_gpio_init(void)
  337. {
  338. return platform_driver_register(&tc3589x_gpio_driver);
  339. }
  340. subsys_initcall(tc3589x_gpio_init);
  341. static void __exit tc3589x_gpio_exit(void)
  342. {
  343. platform_driver_unregister(&tc3589x_gpio_driver);
  344. }
  345. module_exit(tc3589x_gpio_exit);
  346. MODULE_LICENSE("GPL v2");
  347. MODULE_DESCRIPTION("TC3589x GPIO driver");
  348. MODULE_AUTHOR("Hanumath Prasad, Rabin Vincent");