gpio-ep93xx.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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 <hsweeten@visionengravers.com>
  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. /* Maximum value for irq capable line identifiers */
  25. #define EP93XX_GPIO_LINE_MAX_IRQ 23
  26. /*
  27. * Static mapping of GPIO bank F IRQS:
  28. * F0..F7 (16..24) to irq 80..87.
  29. */
  30. #define EP93XX_GPIO_F_IRQ_BASE 80
  31. struct ep93xx_gpio {
  32. void __iomem *base;
  33. struct gpio_chip gc[8];
  34. };
  35. /*************************************************************************
  36. * Interrupt handling for EP93xx on-chip GPIOs
  37. *************************************************************************/
  38. static unsigned char gpio_int_unmasked[3];
  39. static unsigned char gpio_int_enabled[3];
  40. static unsigned char gpio_int_type1[3];
  41. static unsigned char gpio_int_type2[3];
  42. static unsigned char gpio_int_debounce[3];
  43. /* Port ordering is: A B F */
  44. static const u8 int_type1_register_offset[3] = { 0x90, 0xac, 0x4c };
  45. static const u8 int_type2_register_offset[3] = { 0x94, 0xb0, 0x50 };
  46. static const u8 eoi_register_offset[3] = { 0x98, 0xb4, 0x54 };
  47. static const u8 int_en_register_offset[3] = { 0x9c, 0xb8, 0x58 };
  48. static const u8 int_debounce_register_offset[3] = { 0xa8, 0xc4, 0x64 };
  49. static void ep93xx_gpio_update_int_params(struct ep93xx_gpio *epg, unsigned port)
  50. {
  51. BUG_ON(port > 2);
  52. writeb_relaxed(0, epg->base + int_en_register_offset[port]);
  53. writeb_relaxed(gpio_int_type2[port],
  54. epg->base + int_type2_register_offset[port]);
  55. writeb_relaxed(gpio_int_type1[port],
  56. epg->base + int_type1_register_offset[port]);
  57. writeb(gpio_int_unmasked[port] & gpio_int_enabled[port],
  58. epg->base + int_en_register_offset[port]);
  59. }
  60. static int ep93xx_gpio_port(struct gpio_chip *gc)
  61. {
  62. struct ep93xx_gpio *epg = gpiochip_get_data(gc);
  63. int port = 0;
  64. while (port < ARRAY_SIZE(epg->gc) && gc != &epg->gc[port])
  65. port++;
  66. /* This should not happen but is there as a last safeguard */
  67. if (port == ARRAY_SIZE(epg->gc)) {
  68. pr_crit("can't find the GPIO port\n");
  69. return 0;
  70. }
  71. return port;
  72. }
  73. static void ep93xx_gpio_int_debounce(struct gpio_chip *gc,
  74. unsigned int offset, bool enable)
  75. {
  76. struct ep93xx_gpio *epg = gpiochip_get_data(gc);
  77. int port = ep93xx_gpio_port(gc);
  78. int port_mask = BIT(offset);
  79. if (enable)
  80. gpio_int_debounce[port] |= port_mask;
  81. else
  82. gpio_int_debounce[port] &= ~port_mask;
  83. writeb(gpio_int_debounce[port],
  84. epg->base + int_debounce_register_offset[port]);
  85. }
  86. static void ep93xx_gpio_ab_irq_handler(struct irq_desc *desc)
  87. {
  88. struct gpio_chip *gc = irq_desc_get_handler_data(desc);
  89. struct ep93xx_gpio *epg = gpiochip_get_data(gc);
  90. struct irq_chip *irqchip = irq_desc_get_chip(desc);
  91. unsigned long stat;
  92. int offset;
  93. chained_irq_enter(irqchip, desc);
  94. /*
  95. * Dispatch the IRQs to the irqdomain of each A and B
  96. * gpiochip irqdomains depending on what has fired.
  97. * The tricky part is that the IRQ line is shared
  98. * between bank A and B and each has their own gpiochip.
  99. */
  100. stat = readb(epg->base + EP93XX_GPIO_A_INT_STATUS);
  101. for_each_set_bit(offset, &stat, 8)
  102. generic_handle_irq(irq_find_mapping(epg->gc[0].irq.domain,
  103. offset));
  104. stat = readb(epg->base + EP93XX_GPIO_B_INT_STATUS);
  105. for_each_set_bit(offset, &stat, 8)
  106. generic_handle_irq(irq_find_mapping(epg->gc[1].irq.domain,
  107. offset));
  108. chained_irq_exit(irqchip, desc);
  109. }
  110. static void ep93xx_gpio_f_irq_handler(struct irq_desc *desc)
  111. {
  112. /*
  113. * map discontiguous hw irq range to continuous sw irq range:
  114. *
  115. * IRQ_EP93XX_GPIO{0..7}MUX -> EP93XX_GPIO_LINE_F{0..7}
  116. */
  117. struct irq_chip *irqchip = irq_desc_get_chip(desc);
  118. unsigned int irq = irq_desc_get_irq(desc);
  119. int port_f_idx = ((irq + 1) & 7) ^ 4; /* {19..22,47..50} -> {0..7} */
  120. int gpio_irq = EP93XX_GPIO_F_IRQ_BASE + port_f_idx;
  121. chained_irq_enter(irqchip, desc);
  122. generic_handle_irq(gpio_irq);
  123. chained_irq_exit(irqchip, desc);
  124. }
  125. static void ep93xx_gpio_irq_ack(struct irq_data *d)
  126. {
  127. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  128. struct ep93xx_gpio *epg = gpiochip_get_data(gc);
  129. int port = ep93xx_gpio_port(gc);
  130. int port_mask = BIT(d->irq & 7);
  131. if (irqd_get_trigger_type(d) == IRQ_TYPE_EDGE_BOTH) {
  132. gpio_int_type2[port] ^= port_mask; /* switch edge direction */
  133. ep93xx_gpio_update_int_params(epg, port);
  134. }
  135. writeb(port_mask, epg->base + eoi_register_offset[port]);
  136. }
  137. static void ep93xx_gpio_irq_mask_ack(struct irq_data *d)
  138. {
  139. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  140. struct ep93xx_gpio *epg = gpiochip_get_data(gc);
  141. int port = ep93xx_gpio_port(gc);
  142. int port_mask = BIT(d->irq & 7);
  143. if (irqd_get_trigger_type(d) == IRQ_TYPE_EDGE_BOTH)
  144. gpio_int_type2[port] ^= port_mask; /* switch edge direction */
  145. gpio_int_unmasked[port] &= ~port_mask;
  146. ep93xx_gpio_update_int_params(epg, port);
  147. writeb(port_mask, epg->base + eoi_register_offset[port]);
  148. }
  149. static void ep93xx_gpio_irq_mask(struct irq_data *d)
  150. {
  151. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  152. struct ep93xx_gpio *epg = gpiochip_get_data(gc);
  153. int port = ep93xx_gpio_port(gc);
  154. gpio_int_unmasked[port] &= ~BIT(d->irq & 7);
  155. ep93xx_gpio_update_int_params(epg, port);
  156. }
  157. static void ep93xx_gpio_irq_unmask(struct irq_data *d)
  158. {
  159. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  160. struct ep93xx_gpio *epg = gpiochip_get_data(gc);
  161. int port = ep93xx_gpio_port(gc);
  162. gpio_int_unmasked[port] |= BIT(d->irq & 7);
  163. ep93xx_gpio_update_int_params(epg, port);
  164. }
  165. /*
  166. * gpio_int_type1 controls whether the interrupt is level (0) or
  167. * edge (1) triggered, while gpio_int_type2 controls whether it
  168. * triggers on low/falling (0) or high/rising (1).
  169. */
  170. static int ep93xx_gpio_irq_type(struct irq_data *d, unsigned int type)
  171. {
  172. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  173. struct ep93xx_gpio *epg = gpiochip_get_data(gc);
  174. int port = ep93xx_gpio_port(gc);
  175. int offset = d->irq & 7;
  176. int port_mask = BIT(offset);
  177. irq_flow_handler_t handler;
  178. gc->direction_input(gc, offset);
  179. switch (type) {
  180. case IRQ_TYPE_EDGE_RISING:
  181. gpio_int_type1[port] |= port_mask;
  182. gpio_int_type2[port] |= port_mask;
  183. handler = handle_edge_irq;
  184. break;
  185. case IRQ_TYPE_EDGE_FALLING:
  186. gpio_int_type1[port] |= port_mask;
  187. gpio_int_type2[port] &= ~port_mask;
  188. handler = handle_edge_irq;
  189. break;
  190. case IRQ_TYPE_LEVEL_HIGH:
  191. gpio_int_type1[port] &= ~port_mask;
  192. gpio_int_type2[port] |= port_mask;
  193. handler = handle_level_irq;
  194. break;
  195. case IRQ_TYPE_LEVEL_LOW:
  196. gpio_int_type1[port] &= ~port_mask;
  197. gpio_int_type2[port] &= ~port_mask;
  198. handler = handle_level_irq;
  199. break;
  200. case IRQ_TYPE_EDGE_BOTH:
  201. gpio_int_type1[port] |= port_mask;
  202. /* set initial polarity based on current input level */
  203. if (gc->get(gc, offset))
  204. gpio_int_type2[port] &= ~port_mask; /* falling */
  205. else
  206. gpio_int_type2[port] |= port_mask; /* rising */
  207. handler = handle_edge_irq;
  208. break;
  209. default:
  210. return -EINVAL;
  211. }
  212. irq_set_handler_locked(d, handler);
  213. gpio_int_enabled[port] |= port_mask;
  214. ep93xx_gpio_update_int_params(epg, port);
  215. return 0;
  216. }
  217. static struct irq_chip ep93xx_gpio_irq_chip = {
  218. .name = "GPIO",
  219. .irq_ack = ep93xx_gpio_irq_ack,
  220. .irq_mask_ack = ep93xx_gpio_irq_mask_ack,
  221. .irq_mask = ep93xx_gpio_irq_mask,
  222. .irq_unmask = ep93xx_gpio_irq_unmask,
  223. .irq_set_type = ep93xx_gpio_irq_type,
  224. };
  225. static int ep93xx_gpio_init_irq(struct platform_device *pdev,
  226. struct ep93xx_gpio *epg)
  227. {
  228. int ab_parent_irq = platform_get_irq(pdev, 0);
  229. struct device *dev = &pdev->dev;
  230. int gpio_irq;
  231. int ret;
  232. int i;
  233. /* The A bank */
  234. ret = gpiochip_irqchip_add(&epg->gc[0], &ep93xx_gpio_irq_chip,
  235. 64, handle_level_irq,
  236. IRQ_TYPE_NONE);
  237. if (ret) {
  238. dev_err(dev, "Could not add irqchip 0\n");
  239. return ret;
  240. }
  241. gpiochip_set_chained_irqchip(&epg->gc[0], &ep93xx_gpio_irq_chip,
  242. ab_parent_irq,
  243. ep93xx_gpio_ab_irq_handler);
  244. /* The B bank */
  245. ret = gpiochip_irqchip_add(&epg->gc[1], &ep93xx_gpio_irq_chip,
  246. 72, handle_level_irq,
  247. IRQ_TYPE_NONE);
  248. if (ret) {
  249. dev_err(dev, "Could not add irqchip 1\n");
  250. return ret;
  251. }
  252. gpiochip_set_chained_irqchip(&epg->gc[1], &ep93xx_gpio_irq_chip,
  253. ab_parent_irq,
  254. ep93xx_gpio_ab_irq_handler);
  255. /* The F bank */
  256. for (i = 0; i < 8; i++) {
  257. gpio_irq = EP93XX_GPIO_F_IRQ_BASE + i;
  258. irq_set_chip_data(gpio_irq, &epg->gc[5]);
  259. irq_set_chip_and_handler(gpio_irq, &ep93xx_gpio_irq_chip,
  260. handle_level_irq);
  261. irq_clear_status_flags(gpio_irq, IRQ_NOREQUEST);
  262. }
  263. for (i = 1; i <= 8; i++)
  264. irq_set_chained_handler_and_data(platform_get_irq(pdev, i),
  265. ep93xx_gpio_f_irq_handler,
  266. &epg->gc[i]);
  267. return 0;
  268. }
  269. /*************************************************************************
  270. * gpiolib interface for EP93xx on-chip GPIOs
  271. *************************************************************************/
  272. struct ep93xx_gpio_bank {
  273. const char *label;
  274. int data;
  275. int dir;
  276. int base;
  277. bool has_irq;
  278. };
  279. #define EP93XX_GPIO_BANK(_label, _data, _dir, _base, _has_irq) \
  280. { \
  281. .label = _label, \
  282. .data = _data, \
  283. .dir = _dir, \
  284. .base = _base, \
  285. .has_irq = _has_irq, \
  286. }
  287. static struct ep93xx_gpio_bank ep93xx_gpio_banks[] = {
  288. EP93XX_GPIO_BANK("A", 0x00, 0x10, 0, true), /* Bank A has 8 IRQs */
  289. EP93XX_GPIO_BANK("B", 0x04, 0x14, 8, true), /* Bank B has 8 IRQs */
  290. EP93XX_GPIO_BANK("C", 0x08, 0x18, 40, false),
  291. EP93XX_GPIO_BANK("D", 0x0c, 0x1c, 24, false),
  292. EP93XX_GPIO_BANK("E", 0x20, 0x24, 32, false),
  293. EP93XX_GPIO_BANK("F", 0x30, 0x34, 16, true), /* Bank F has 8 IRQs */
  294. EP93XX_GPIO_BANK("G", 0x38, 0x3c, 48, false),
  295. EP93XX_GPIO_BANK("H", 0x40, 0x44, 56, false),
  296. };
  297. static int ep93xx_gpio_set_config(struct gpio_chip *gc, unsigned offset,
  298. unsigned long config)
  299. {
  300. u32 debounce;
  301. if (pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE)
  302. return -ENOTSUPP;
  303. debounce = pinconf_to_config_argument(config);
  304. ep93xx_gpio_int_debounce(gc, offset, debounce ? true : false);
  305. return 0;
  306. }
  307. static int ep93xx_gpio_f_to_irq(struct gpio_chip *gc, unsigned offset)
  308. {
  309. return EP93XX_GPIO_F_IRQ_BASE + offset;
  310. }
  311. static int ep93xx_gpio_add_bank(struct gpio_chip *gc, struct device *dev,
  312. struct ep93xx_gpio *epg,
  313. struct ep93xx_gpio_bank *bank)
  314. {
  315. void __iomem *data = epg->base + bank->data;
  316. void __iomem *dir = epg->base + bank->dir;
  317. int err;
  318. err = bgpio_init(gc, dev, 1, data, NULL, NULL, dir, NULL, 0);
  319. if (err)
  320. return err;
  321. gc->label = bank->label;
  322. gc->base = bank->base;
  323. if (bank->has_irq)
  324. gc->set_config = ep93xx_gpio_set_config;
  325. return devm_gpiochip_add_data(dev, gc, epg);
  326. }
  327. static int ep93xx_gpio_probe(struct platform_device *pdev)
  328. {
  329. struct ep93xx_gpio *epg;
  330. struct resource *res;
  331. int i;
  332. struct device *dev = &pdev->dev;
  333. epg = devm_kzalloc(dev, sizeof(*epg), GFP_KERNEL);
  334. if (!epg)
  335. return -ENOMEM;
  336. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  337. epg->base = devm_ioremap_resource(dev, res);
  338. if (IS_ERR(epg->base))
  339. return PTR_ERR(epg->base);
  340. for (i = 0; i < ARRAY_SIZE(ep93xx_gpio_banks); i++) {
  341. struct gpio_chip *gc = &epg->gc[i];
  342. struct ep93xx_gpio_bank *bank = &ep93xx_gpio_banks[i];
  343. if (ep93xx_gpio_add_bank(gc, &pdev->dev, epg, bank))
  344. dev_warn(&pdev->dev, "Unable to add gpio bank %s\n",
  345. bank->label);
  346. /* Only bank F has especially funky IRQ handling */
  347. if (i == 5)
  348. gc->to_irq = ep93xx_gpio_f_to_irq;
  349. }
  350. ep93xx_gpio_init_irq(pdev, epg);
  351. return 0;
  352. }
  353. static struct platform_driver ep93xx_gpio_driver = {
  354. .driver = {
  355. .name = "gpio-ep93xx",
  356. },
  357. .probe = ep93xx_gpio_probe,
  358. };
  359. static int __init ep93xx_gpio_init(void)
  360. {
  361. return platform_driver_register(&ep93xx_gpio_driver);
  362. }
  363. postcore_initcall(ep93xx_gpio_init);
  364. MODULE_AUTHOR("Ryan Mallon <ryan@bluewatersys.com> "
  365. "H Hartley Sweeten <hsweeten@visionengravers.com>");
  366. MODULE_DESCRIPTION("EP93XX GPIO driver");
  367. MODULE_LICENSE("GPL");