gpio-ep93xx.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Generic EP93xx GPIO handling
  4. *
  5. * Copyright (c) 2008 Ryan Mallon
  6. * Copyright (c) 2011 H Hartley Sweeten <[email protected]>
  7. *
  8. * Based on code originally from:
  9. * linux/arch/arm/mach-ep93xx/core.c
  10. */
  11. #include <linux/init.h>
  12. #include <linux/module.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/io.h>
  15. #include <linux/irq.h>
  16. #include <linux/slab.h>
  17. #include <linux/gpio/driver.h>
  18. #include <linux/bitops.h>
  19. #define EP93XX_GPIO_F_INT_STATUS 0x5c
  20. #define EP93XX_GPIO_A_INT_STATUS 0xa0
  21. #define EP93XX_GPIO_B_INT_STATUS 0xbc
  22. /* Maximum value for gpio line identifiers */
  23. #define EP93XX_GPIO_LINE_MAX 63
  24. /* Number of GPIO chips in EP93XX */
  25. #define EP93XX_GPIO_CHIP_NUM 8
  26. /* Maximum value for irq capable line identifiers */
  27. #define EP93XX_GPIO_LINE_MAX_IRQ 23
  28. #define EP93XX_GPIO_A_IRQ_BASE 64
  29. #define EP93XX_GPIO_B_IRQ_BASE 72
  30. /*
  31. * Static mapping of GPIO bank F IRQS:
  32. * F0..F7 (16..24) to irq 80..87.
  33. */
  34. #define EP93XX_GPIO_F_IRQ_BASE 80
  35. struct ep93xx_gpio_irq_chip {
  36. struct irq_chip ic;
  37. u8 irq_offset;
  38. u8 int_unmasked;
  39. u8 int_enabled;
  40. u8 int_type1;
  41. u8 int_type2;
  42. u8 int_debounce;
  43. };
  44. struct ep93xx_gpio_chip {
  45. struct gpio_chip gc;
  46. struct ep93xx_gpio_irq_chip *eic;
  47. };
  48. struct ep93xx_gpio {
  49. void __iomem *base;
  50. struct ep93xx_gpio_chip gc[EP93XX_GPIO_CHIP_NUM];
  51. };
  52. #define to_ep93xx_gpio_chip(x) container_of(x, struct ep93xx_gpio_chip, gc)
  53. static struct ep93xx_gpio_irq_chip *to_ep93xx_gpio_irq_chip(struct gpio_chip *gc)
  54. {
  55. struct ep93xx_gpio_chip *egc = to_ep93xx_gpio_chip(gc);
  56. return egc->eic;
  57. }
  58. /*************************************************************************
  59. * Interrupt handling for EP93xx on-chip GPIOs
  60. *************************************************************************/
  61. #define EP93XX_INT_TYPE1_OFFSET 0x00
  62. #define EP93XX_INT_TYPE2_OFFSET 0x04
  63. #define EP93XX_INT_EOI_OFFSET 0x08
  64. #define EP93XX_INT_EN_OFFSET 0x0c
  65. #define EP93XX_INT_STATUS_OFFSET 0x10
  66. #define EP93XX_INT_RAW_STATUS_OFFSET 0x14
  67. #define EP93XX_INT_DEBOUNCE_OFFSET 0x18
  68. static void ep93xx_gpio_update_int_params(struct ep93xx_gpio *epg,
  69. struct ep93xx_gpio_irq_chip *eic)
  70. {
  71. writeb_relaxed(0, epg->base + eic->irq_offset + EP93XX_INT_EN_OFFSET);
  72. writeb_relaxed(eic->int_type2,
  73. epg->base + eic->irq_offset + EP93XX_INT_TYPE2_OFFSET);
  74. writeb_relaxed(eic->int_type1,
  75. epg->base + eic->irq_offset + EP93XX_INT_TYPE1_OFFSET);
  76. writeb_relaxed(eic->int_unmasked & eic->int_enabled,
  77. epg->base + eic->irq_offset + EP93XX_INT_EN_OFFSET);
  78. }
  79. static void ep93xx_gpio_int_debounce(struct gpio_chip *gc,
  80. unsigned int offset, bool enable)
  81. {
  82. struct ep93xx_gpio *epg = gpiochip_get_data(gc);
  83. struct ep93xx_gpio_irq_chip *eic = to_ep93xx_gpio_irq_chip(gc);
  84. int port_mask = BIT(offset);
  85. if (enable)
  86. eic->int_debounce |= port_mask;
  87. else
  88. eic->int_debounce &= ~port_mask;
  89. writeb(eic->int_debounce,
  90. epg->base + eic->irq_offset + EP93XX_INT_DEBOUNCE_OFFSET);
  91. }
  92. static void ep93xx_gpio_ab_irq_handler(struct irq_desc *desc)
  93. {
  94. struct gpio_chip *gc = irq_desc_get_handler_data(desc);
  95. struct ep93xx_gpio *epg = gpiochip_get_data(gc);
  96. struct irq_chip *irqchip = irq_desc_get_chip(desc);
  97. unsigned long stat;
  98. int offset;
  99. chained_irq_enter(irqchip, desc);
  100. /*
  101. * Dispatch the IRQs to the irqdomain of each A and B
  102. * gpiochip irqdomains depending on what has fired.
  103. * The tricky part is that the IRQ line is shared
  104. * between bank A and B and each has their own gpiochip.
  105. */
  106. stat = readb(epg->base + EP93XX_GPIO_A_INT_STATUS);
  107. for_each_set_bit(offset, &stat, 8)
  108. generic_handle_domain_irq(epg->gc[0].gc.irq.domain,
  109. offset);
  110. stat = readb(epg->base + EP93XX_GPIO_B_INT_STATUS);
  111. for_each_set_bit(offset, &stat, 8)
  112. generic_handle_domain_irq(epg->gc[1].gc.irq.domain,
  113. offset);
  114. chained_irq_exit(irqchip, desc);
  115. }
  116. static void ep93xx_gpio_f_irq_handler(struct irq_desc *desc)
  117. {
  118. /*
  119. * map discontiguous hw irq range to continuous sw irq range:
  120. *
  121. * IRQ_EP93XX_GPIO{0..7}MUX -> EP93XX_GPIO_LINE_F{0..7}
  122. */
  123. struct irq_chip *irqchip = irq_desc_get_chip(desc);
  124. unsigned int irq = irq_desc_get_irq(desc);
  125. int port_f_idx = (irq & 7) ^ 4; /* {20..23,48..51} -> {0..7} */
  126. int gpio_irq = EP93XX_GPIO_F_IRQ_BASE + port_f_idx;
  127. chained_irq_enter(irqchip, desc);
  128. generic_handle_irq(gpio_irq);
  129. chained_irq_exit(irqchip, desc);
  130. }
  131. static void ep93xx_gpio_irq_ack(struct irq_data *d)
  132. {
  133. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  134. struct ep93xx_gpio_irq_chip *eic = to_ep93xx_gpio_irq_chip(gc);
  135. struct ep93xx_gpio *epg = gpiochip_get_data(gc);
  136. int port_mask = BIT(d->irq & 7);
  137. if (irqd_get_trigger_type(d) == IRQ_TYPE_EDGE_BOTH) {
  138. eic->int_type2 ^= port_mask; /* switch edge direction */
  139. ep93xx_gpio_update_int_params(epg, eic);
  140. }
  141. writeb(port_mask, epg->base + eic->irq_offset + EP93XX_INT_EOI_OFFSET);
  142. }
  143. static void ep93xx_gpio_irq_mask_ack(struct irq_data *d)
  144. {
  145. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  146. struct ep93xx_gpio_irq_chip *eic = to_ep93xx_gpio_irq_chip(gc);
  147. struct ep93xx_gpio *epg = gpiochip_get_data(gc);
  148. int port_mask = BIT(d->irq & 7);
  149. if (irqd_get_trigger_type(d) == IRQ_TYPE_EDGE_BOTH)
  150. eic->int_type2 ^= port_mask; /* switch edge direction */
  151. eic->int_unmasked &= ~port_mask;
  152. ep93xx_gpio_update_int_params(epg, eic);
  153. writeb(port_mask, epg->base + eic->irq_offset + EP93XX_INT_EOI_OFFSET);
  154. }
  155. static void ep93xx_gpio_irq_mask(struct irq_data *d)
  156. {
  157. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  158. struct ep93xx_gpio_irq_chip *eic = to_ep93xx_gpio_irq_chip(gc);
  159. struct ep93xx_gpio *epg = gpiochip_get_data(gc);
  160. eic->int_unmasked &= ~BIT(d->irq & 7);
  161. ep93xx_gpio_update_int_params(epg, eic);
  162. }
  163. static void ep93xx_gpio_irq_unmask(struct irq_data *d)
  164. {
  165. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  166. struct ep93xx_gpio_irq_chip *eic = to_ep93xx_gpio_irq_chip(gc);
  167. struct ep93xx_gpio *epg = gpiochip_get_data(gc);
  168. eic->int_unmasked |= BIT(d->irq & 7);
  169. ep93xx_gpio_update_int_params(epg, eic);
  170. }
  171. /*
  172. * gpio_int_type1 controls whether the interrupt is level (0) or
  173. * edge (1) triggered, while gpio_int_type2 controls whether it
  174. * triggers on low/falling (0) or high/rising (1).
  175. */
  176. static int ep93xx_gpio_irq_type(struct irq_data *d, unsigned int type)
  177. {
  178. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  179. struct ep93xx_gpio_irq_chip *eic = to_ep93xx_gpio_irq_chip(gc);
  180. struct ep93xx_gpio *epg = gpiochip_get_data(gc);
  181. int offset = d->irq & 7;
  182. int port_mask = BIT(offset);
  183. irq_flow_handler_t handler;
  184. gc->direction_input(gc, offset);
  185. switch (type) {
  186. case IRQ_TYPE_EDGE_RISING:
  187. eic->int_type1 |= port_mask;
  188. eic->int_type2 |= port_mask;
  189. handler = handle_edge_irq;
  190. break;
  191. case IRQ_TYPE_EDGE_FALLING:
  192. eic->int_type1 |= port_mask;
  193. eic->int_type2 &= ~port_mask;
  194. handler = handle_edge_irq;
  195. break;
  196. case IRQ_TYPE_LEVEL_HIGH:
  197. eic->int_type1 &= ~port_mask;
  198. eic->int_type2 |= port_mask;
  199. handler = handle_level_irq;
  200. break;
  201. case IRQ_TYPE_LEVEL_LOW:
  202. eic->int_type1 &= ~port_mask;
  203. eic->int_type2 &= ~port_mask;
  204. handler = handle_level_irq;
  205. break;
  206. case IRQ_TYPE_EDGE_BOTH:
  207. eic->int_type1 |= port_mask;
  208. /* set initial polarity based on current input level */
  209. if (gc->get(gc, offset))
  210. eic->int_type2 &= ~port_mask; /* falling */
  211. else
  212. eic->int_type2 |= port_mask; /* rising */
  213. handler = handle_edge_irq;
  214. break;
  215. default:
  216. return -EINVAL;
  217. }
  218. irq_set_handler_locked(d, handler);
  219. eic->int_enabled |= port_mask;
  220. ep93xx_gpio_update_int_params(epg, eic);
  221. return 0;
  222. }
  223. /*************************************************************************
  224. * gpiolib interface for EP93xx on-chip GPIOs
  225. *************************************************************************/
  226. struct ep93xx_gpio_bank {
  227. const char *label;
  228. int data;
  229. int dir;
  230. int irq;
  231. int base;
  232. bool has_irq;
  233. bool has_hierarchical_irq;
  234. unsigned int irq_base;
  235. };
  236. #define EP93XX_GPIO_BANK(_label, _data, _dir, _irq, _base, _has_irq, _has_hier, _irq_base) \
  237. { \
  238. .label = _label, \
  239. .data = _data, \
  240. .dir = _dir, \
  241. .irq = _irq, \
  242. .base = _base, \
  243. .has_irq = _has_irq, \
  244. .has_hierarchical_irq = _has_hier, \
  245. .irq_base = _irq_base, \
  246. }
  247. static struct ep93xx_gpio_bank ep93xx_gpio_banks[] = {
  248. /* Bank A has 8 IRQs */
  249. EP93XX_GPIO_BANK("A", 0x00, 0x10, 0x90, 0, true, false, EP93XX_GPIO_A_IRQ_BASE),
  250. /* Bank B has 8 IRQs */
  251. EP93XX_GPIO_BANK("B", 0x04, 0x14, 0xac, 8, true, false, EP93XX_GPIO_B_IRQ_BASE),
  252. EP93XX_GPIO_BANK("C", 0x08, 0x18, 0x00, 40, false, false, 0),
  253. EP93XX_GPIO_BANK("D", 0x0c, 0x1c, 0x00, 24, false, false, 0),
  254. EP93XX_GPIO_BANK("E", 0x20, 0x24, 0x00, 32, false, false, 0),
  255. /* Bank F has 8 IRQs */
  256. EP93XX_GPIO_BANK("F", 0x30, 0x34, 0x4c, 16, false, true, EP93XX_GPIO_F_IRQ_BASE),
  257. EP93XX_GPIO_BANK("G", 0x38, 0x3c, 0x00, 48, false, false, 0),
  258. EP93XX_GPIO_BANK("H", 0x40, 0x44, 0x00, 56, false, false, 0),
  259. };
  260. static int ep93xx_gpio_set_config(struct gpio_chip *gc, unsigned offset,
  261. unsigned long config)
  262. {
  263. u32 debounce;
  264. if (pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE)
  265. return -ENOTSUPP;
  266. debounce = pinconf_to_config_argument(config);
  267. ep93xx_gpio_int_debounce(gc, offset, debounce ? true : false);
  268. return 0;
  269. }
  270. static void ep93xx_init_irq_chip(struct device *dev, struct irq_chip *ic)
  271. {
  272. ic->irq_ack = ep93xx_gpio_irq_ack;
  273. ic->irq_mask_ack = ep93xx_gpio_irq_mask_ack;
  274. ic->irq_mask = ep93xx_gpio_irq_mask;
  275. ic->irq_unmask = ep93xx_gpio_irq_unmask;
  276. ic->irq_set_type = ep93xx_gpio_irq_type;
  277. }
  278. static int ep93xx_gpio_add_bank(struct ep93xx_gpio_chip *egc,
  279. struct platform_device *pdev,
  280. struct ep93xx_gpio *epg,
  281. struct ep93xx_gpio_bank *bank)
  282. {
  283. void __iomem *data = epg->base + bank->data;
  284. void __iomem *dir = epg->base + bank->dir;
  285. struct gpio_chip *gc = &egc->gc;
  286. struct device *dev = &pdev->dev;
  287. struct gpio_irq_chip *girq;
  288. int err;
  289. err = bgpio_init(gc, dev, 1, data, NULL, NULL, dir, NULL, 0);
  290. if (err)
  291. return err;
  292. gc->label = bank->label;
  293. gc->base = bank->base;
  294. girq = &gc->irq;
  295. if (bank->has_irq || bank->has_hierarchical_irq) {
  296. struct irq_chip *ic;
  297. gc->set_config = ep93xx_gpio_set_config;
  298. egc->eic = devm_kcalloc(dev, 1,
  299. sizeof(*egc->eic),
  300. GFP_KERNEL);
  301. if (!egc->eic)
  302. return -ENOMEM;
  303. egc->eic->irq_offset = bank->irq;
  304. ic = &egc->eic->ic;
  305. ic->name = devm_kasprintf(dev, GFP_KERNEL, "gpio-irq-%s", bank->label);
  306. if (!ic->name)
  307. return -ENOMEM;
  308. ep93xx_init_irq_chip(dev, ic);
  309. girq->chip = ic;
  310. }
  311. if (bank->has_irq) {
  312. int ab_parent_irq = platform_get_irq(pdev, 0);
  313. girq->parent_handler = ep93xx_gpio_ab_irq_handler;
  314. girq->num_parents = 1;
  315. girq->parents = devm_kcalloc(dev, girq->num_parents,
  316. sizeof(*girq->parents),
  317. GFP_KERNEL);
  318. if (!girq->parents)
  319. return -ENOMEM;
  320. girq->default_type = IRQ_TYPE_NONE;
  321. girq->handler = handle_level_irq;
  322. girq->parents[0] = ab_parent_irq;
  323. girq->first = bank->irq_base;
  324. }
  325. /* Only bank F has especially funky IRQ handling */
  326. if (bank->has_hierarchical_irq) {
  327. int gpio_irq;
  328. int i;
  329. /*
  330. * FIXME: convert this to use hierarchical IRQ support!
  331. * this requires fixing the root irqchip to be hierarchical.
  332. */
  333. girq->parent_handler = ep93xx_gpio_f_irq_handler;
  334. girq->num_parents = 8;
  335. girq->parents = devm_kcalloc(dev, girq->num_parents,
  336. sizeof(*girq->parents),
  337. GFP_KERNEL);
  338. if (!girq->parents)
  339. return -ENOMEM;
  340. /* Pick resources 1..8 for these IRQs */
  341. for (i = 0; i < girq->num_parents; i++) {
  342. girq->parents[i] = platform_get_irq(pdev, i + 1);
  343. gpio_irq = bank->irq_base + i;
  344. irq_set_chip_data(gpio_irq, &epg->gc[5]);
  345. irq_set_chip_and_handler(gpio_irq,
  346. girq->chip,
  347. handle_level_irq);
  348. irq_clear_status_flags(gpio_irq, IRQ_NOREQUEST);
  349. }
  350. girq->default_type = IRQ_TYPE_NONE;
  351. girq->handler = handle_level_irq;
  352. girq->first = bank->irq_base;
  353. }
  354. return devm_gpiochip_add_data(dev, gc, epg);
  355. }
  356. static int ep93xx_gpio_probe(struct platform_device *pdev)
  357. {
  358. struct ep93xx_gpio *epg;
  359. int i;
  360. epg = devm_kzalloc(&pdev->dev, sizeof(*epg), GFP_KERNEL);
  361. if (!epg)
  362. return -ENOMEM;
  363. epg->base = devm_platform_ioremap_resource(pdev, 0);
  364. if (IS_ERR(epg->base))
  365. return PTR_ERR(epg->base);
  366. for (i = 0; i < ARRAY_SIZE(ep93xx_gpio_banks); i++) {
  367. struct ep93xx_gpio_chip *gc = &epg->gc[i];
  368. struct ep93xx_gpio_bank *bank = &ep93xx_gpio_banks[i];
  369. if (ep93xx_gpio_add_bank(gc, pdev, epg, bank))
  370. dev_warn(&pdev->dev, "Unable to add gpio bank %s\n",
  371. bank->label);
  372. }
  373. return 0;
  374. }
  375. static struct platform_driver ep93xx_gpio_driver = {
  376. .driver = {
  377. .name = "gpio-ep93xx",
  378. },
  379. .probe = ep93xx_gpio_probe,
  380. };
  381. static int __init ep93xx_gpio_init(void)
  382. {
  383. return platform_driver_register(&ep93xx_gpio_driver);
  384. }
  385. postcore_initcall(ep93xx_gpio_init);
  386. MODULE_AUTHOR("Ryan Mallon <[email protected]> "
  387. "H Hartley Sweeten <[email protected]>");
  388. MODULE_DESCRIPTION("EP93XX GPIO driver");
  389. MODULE_LICENSE("GPL");