/drivers/gpio/gpio-ep93xx.c

http://github.com/mirrors/linux · C · 445 lines · 323 code · 72 blank · 50 comment · 27 complexity · a8afa4e372c2afa2dfbabf1d03727252 MD5 · raw file

  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. /*************************************************************************
  226. * gpiolib interface for EP93xx on-chip GPIOs
  227. *************************************************************************/
  228. struct ep93xx_gpio_bank {
  229. const char *label;
  230. int data;
  231. int dir;
  232. int base;
  233. bool has_irq;
  234. bool has_hierarchical_irq;
  235. unsigned int irq_base;
  236. };
  237. #define EP93XX_GPIO_BANK(_label, _data, _dir, _base, _has_irq, _has_hier, _irq_base) \
  238. { \
  239. .label = _label, \
  240. .data = _data, \
  241. .dir = _dir, \
  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, 0, true, false, 64),
  250. /* Bank B has 8 IRQs */
  251. EP93XX_GPIO_BANK("B", 0x04, 0x14, 8, true, false, 72),
  252. EP93XX_GPIO_BANK("C", 0x08, 0x18, 40, false, false, 0),
  253. EP93XX_GPIO_BANK("D", 0x0c, 0x1c, 24, false, false, 0),
  254. EP93XX_GPIO_BANK("E", 0x20, 0x24, 32, false, false, 0),
  255. /* Bank F has 8 IRQs */
  256. EP93XX_GPIO_BANK("F", 0x30, 0x34, 16, false, true, 0),
  257. EP93XX_GPIO_BANK("G", 0x38, 0x3c, 48, false, false, 0),
  258. EP93XX_GPIO_BANK("H", 0x40, 0x44, 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 int ep93xx_gpio_f_to_irq(struct gpio_chip *gc, unsigned offset)
  271. {
  272. return EP93XX_GPIO_F_IRQ_BASE + offset;
  273. }
  274. static int ep93xx_gpio_add_bank(struct gpio_chip *gc,
  275. struct platform_device *pdev,
  276. struct ep93xx_gpio *epg,
  277. struct ep93xx_gpio_bank *bank)
  278. {
  279. void __iomem *data = epg->base + bank->data;
  280. void __iomem *dir = epg->base + bank->dir;
  281. struct device *dev = &pdev->dev;
  282. struct gpio_irq_chip *girq;
  283. int err;
  284. err = bgpio_init(gc, dev, 1, data, NULL, NULL, dir, NULL, 0);
  285. if (err)
  286. return err;
  287. gc->label = bank->label;
  288. gc->base = bank->base;
  289. girq = &gc->irq;
  290. if (bank->has_irq || bank->has_hierarchical_irq) {
  291. gc->set_config = ep93xx_gpio_set_config;
  292. girq->chip = &ep93xx_gpio_irq_chip;
  293. }
  294. if (bank->has_irq) {
  295. int ab_parent_irq = platform_get_irq(pdev, 0);
  296. girq->parent_handler = ep93xx_gpio_ab_irq_handler;
  297. girq->num_parents = 1;
  298. girq->parents = devm_kcalloc(dev, 1,
  299. sizeof(*girq->parents),
  300. GFP_KERNEL);
  301. if (!girq->parents)
  302. return -ENOMEM;
  303. girq->default_type = IRQ_TYPE_NONE;
  304. girq->handler = handle_level_irq;
  305. girq->parents[0] = ab_parent_irq;
  306. girq->first = bank->irq_base;
  307. }
  308. /* Only bank F has especially funky IRQ handling */
  309. if (bank->has_hierarchical_irq) {
  310. int gpio_irq;
  311. int i;
  312. /*
  313. * FIXME: convert this to use hierarchical IRQ support!
  314. * this requires fixing the root irqchip to be hierarchial.
  315. */
  316. girq->parent_handler = ep93xx_gpio_f_irq_handler;
  317. girq->num_parents = 8;
  318. girq->parents = devm_kcalloc(dev, 8,
  319. sizeof(*girq->parents),
  320. GFP_KERNEL);
  321. if (!girq->parents)
  322. return -ENOMEM;
  323. /* Pick resources 1..8 for these IRQs */
  324. for (i = 1; i <= 8; i++)
  325. girq->parents[i - 1] = platform_get_irq(pdev, i);
  326. for (i = 0; i < 8; i++) {
  327. gpio_irq = EP93XX_GPIO_F_IRQ_BASE + i;
  328. irq_set_chip_data(gpio_irq, &epg->gc[5]);
  329. irq_set_chip_and_handler(gpio_irq,
  330. &ep93xx_gpio_irq_chip,
  331. handle_level_irq);
  332. irq_clear_status_flags(gpio_irq, IRQ_NOREQUEST);
  333. }
  334. girq->default_type = IRQ_TYPE_NONE;
  335. girq->handler = handle_level_irq;
  336. gc->to_irq = ep93xx_gpio_f_to_irq;
  337. }
  338. return devm_gpiochip_add_data(dev, gc, epg);
  339. }
  340. static int ep93xx_gpio_probe(struct platform_device *pdev)
  341. {
  342. struct ep93xx_gpio *epg;
  343. int i;
  344. epg = devm_kzalloc(&pdev->dev, sizeof(*epg), GFP_KERNEL);
  345. if (!epg)
  346. return -ENOMEM;
  347. epg->base = devm_platform_ioremap_resource(pdev, 0);
  348. if (IS_ERR(epg->base))
  349. return PTR_ERR(epg->base);
  350. for (i = 0; i < ARRAY_SIZE(ep93xx_gpio_banks); i++) {
  351. struct gpio_chip *gc = &epg->gc[i];
  352. struct ep93xx_gpio_bank *bank = &ep93xx_gpio_banks[i];
  353. if (ep93xx_gpio_add_bank(gc, pdev, epg, bank))
  354. dev_warn(&pdev->dev, "Unable to add gpio bank %s\n",
  355. bank->label);
  356. }
  357. return 0;
  358. }
  359. static struct platform_driver ep93xx_gpio_driver = {
  360. .driver = {
  361. .name = "gpio-ep93xx",
  362. },
  363. .probe = ep93xx_gpio_probe,
  364. };
  365. static int __init ep93xx_gpio_init(void)
  366. {
  367. return platform_driver_register(&ep93xx_gpio_driver);
  368. }
  369. postcore_initcall(ep93xx_gpio_init);
  370. MODULE_AUTHOR("Ryan Mallon <ryan@bluewatersys.com> "
  371. "H Hartley Sweeten <hsweeten@visionengravers.com>");
  372. MODULE_DESCRIPTION("EP93XX GPIO driver");
  373. MODULE_LICENSE("GPL");