gpio-ftgpio010.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Faraday Technolog FTGPIO010 gpiochip and interrupt routines
  4. * Copyright (C) 2017 Linus Walleij <linus.walleij@linaro.org>
  5. *
  6. * Based on arch/arm/mach-gemini/gpio.c:
  7. * Copyright (C) 2008-2009 Paulius Zaleckas <paulius.zaleckas@teltonika.lt>
  8. *
  9. * Based on plat-mxc/gpio.c:
  10. * MXC GPIO support. (c) 2008 Daniel Mack <daniel@caiaq.de>
  11. * Copyright 2008 Juergen Beisert, kernel@pengutronix.de
  12. */
  13. #include <linux/gpio/driver.h>
  14. #include <linux/io.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/of_gpio.h>
  18. #include <linux/bitops.h>
  19. /* GPIO registers definition */
  20. #define GPIO_DATA_OUT 0x00
  21. #define GPIO_DATA_IN 0x04
  22. #define GPIO_DIR 0x08
  23. #define GPIO_DATA_SET 0x10
  24. #define GPIO_DATA_CLR 0x14
  25. #define GPIO_PULL_EN 0x18
  26. #define GPIO_PULL_TYPE 0x1C
  27. #define GPIO_INT_EN 0x20
  28. #define GPIO_INT_STAT 0x24
  29. #define GPIO_INT_MASK 0x2C
  30. #define GPIO_INT_CLR 0x30
  31. #define GPIO_INT_TYPE 0x34
  32. #define GPIO_INT_BOTH_EDGE 0x38
  33. #define GPIO_INT_LEVEL 0x3C
  34. #define GPIO_DEBOUNCE_EN 0x40
  35. #define GPIO_DEBOUNCE_PRESCALE 0x44
  36. /**
  37. * struct ftgpio_gpio - Gemini GPIO state container
  38. * @dev: containing device for this instance
  39. * @gc: gpiochip for this instance
  40. */
  41. struct ftgpio_gpio {
  42. struct device *dev;
  43. struct gpio_chip gc;
  44. void __iomem *base;
  45. };
  46. static void ftgpio_gpio_ack_irq(struct irq_data *d)
  47. {
  48. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  49. struct ftgpio_gpio *g = gpiochip_get_data(gc);
  50. writel(BIT(irqd_to_hwirq(d)), g->base + GPIO_INT_CLR);
  51. }
  52. static void ftgpio_gpio_mask_irq(struct irq_data *d)
  53. {
  54. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  55. struct ftgpio_gpio *g = gpiochip_get_data(gc);
  56. u32 val;
  57. val = readl(g->base + GPIO_INT_EN);
  58. val &= ~BIT(irqd_to_hwirq(d));
  59. writel(val, g->base + GPIO_INT_EN);
  60. }
  61. static void ftgpio_gpio_unmask_irq(struct irq_data *d)
  62. {
  63. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  64. struct ftgpio_gpio *g = gpiochip_get_data(gc);
  65. u32 val;
  66. val = readl(g->base + GPIO_INT_EN);
  67. val |= BIT(irqd_to_hwirq(d));
  68. writel(val, g->base + GPIO_INT_EN);
  69. }
  70. static int ftgpio_gpio_set_irq_type(struct irq_data *d, unsigned int type)
  71. {
  72. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  73. struct ftgpio_gpio *g = gpiochip_get_data(gc);
  74. u32 mask = BIT(irqd_to_hwirq(d));
  75. u32 reg_both, reg_level, reg_type;
  76. reg_type = readl(g->base + GPIO_INT_TYPE);
  77. reg_level = readl(g->base + GPIO_INT_LEVEL);
  78. reg_both = readl(g->base + GPIO_INT_BOTH_EDGE);
  79. switch (type) {
  80. case IRQ_TYPE_EDGE_BOTH:
  81. irq_set_handler_locked(d, handle_edge_irq);
  82. reg_type &= ~mask;
  83. reg_both |= mask;
  84. break;
  85. case IRQ_TYPE_EDGE_RISING:
  86. irq_set_handler_locked(d, handle_edge_irq);
  87. reg_type &= ~mask;
  88. reg_both &= ~mask;
  89. reg_level &= ~mask;
  90. break;
  91. case IRQ_TYPE_EDGE_FALLING:
  92. irq_set_handler_locked(d, handle_edge_irq);
  93. reg_type &= ~mask;
  94. reg_both &= ~mask;
  95. reg_level |= mask;
  96. break;
  97. case IRQ_TYPE_LEVEL_HIGH:
  98. irq_set_handler_locked(d, handle_level_irq);
  99. reg_type |= mask;
  100. reg_level &= ~mask;
  101. break;
  102. case IRQ_TYPE_LEVEL_LOW:
  103. irq_set_handler_locked(d, handle_level_irq);
  104. reg_type |= mask;
  105. reg_level |= mask;
  106. break;
  107. default:
  108. irq_set_handler_locked(d, handle_bad_irq);
  109. return -EINVAL;
  110. }
  111. writel(reg_type, g->base + GPIO_INT_TYPE);
  112. writel(reg_level, g->base + GPIO_INT_LEVEL);
  113. writel(reg_both, g->base + GPIO_INT_BOTH_EDGE);
  114. ftgpio_gpio_ack_irq(d);
  115. return 0;
  116. }
  117. static struct irq_chip ftgpio_gpio_irqchip = {
  118. .name = "FTGPIO010",
  119. .irq_ack = ftgpio_gpio_ack_irq,
  120. .irq_mask = ftgpio_gpio_mask_irq,
  121. .irq_unmask = ftgpio_gpio_unmask_irq,
  122. .irq_set_type = ftgpio_gpio_set_irq_type,
  123. };
  124. static void ftgpio_gpio_irq_handler(struct irq_desc *desc)
  125. {
  126. struct gpio_chip *gc = irq_desc_get_handler_data(desc);
  127. struct ftgpio_gpio *g = gpiochip_get_data(gc);
  128. struct irq_chip *irqchip = irq_desc_get_chip(desc);
  129. int offset;
  130. unsigned long stat;
  131. chained_irq_enter(irqchip, desc);
  132. stat = readl(g->base + GPIO_INT_STAT);
  133. if (stat)
  134. for_each_set_bit(offset, &stat, gc->ngpio)
  135. generic_handle_irq(irq_find_mapping(gc->irqdomain,
  136. offset));
  137. chained_irq_exit(irqchip, desc);
  138. }
  139. static int ftgpio_gpio_probe(struct platform_device *pdev)
  140. {
  141. struct device *dev = &pdev->dev;
  142. struct resource *res;
  143. struct ftgpio_gpio *g;
  144. int irq;
  145. int ret;
  146. g = devm_kzalloc(dev, sizeof(*g), GFP_KERNEL);
  147. if (!g)
  148. return -ENOMEM;
  149. g->dev = dev;
  150. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  151. g->base = devm_ioremap_resource(dev, res);
  152. if (IS_ERR(g->base))
  153. return PTR_ERR(g->base);
  154. irq = platform_get_irq(pdev, 0);
  155. if (!irq)
  156. return -EINVAL;
  157. ret = bgpio_init(&g->gc, dev, 4,
  158. g->base + GPIO_DATA_IN,
  159. g->base + GPIO_DATA_SET,
  160. g->base + GPIO_DATA_CLR,
  161. g->base + GPIO_DIR,
  162. NULL,
  163. 0);
  164. if (ret) {
  165. dev_err(dev, "unable to init generic GPIO\n");
  166. return ret;
  167. }
  168. g->gc.label = "FTGPIO010";
  169. g->gc.base = -1;
  170. g->gc.parent = dev;
  171. g->gc.owner = THIS_MODULE;
  172. /* ngpio is set by bgpio_init() */
  173. ret = devm_gpiochip_add_data(dev, &g->gc, g);
  174. if (ret)
  175. return ret;
  176. /* Disable, unmask and clear all interrupts */
  177. writel(0x0, g->base + GPIO_INT_EN);
  178. writel(0x0, g->base + GPIO_INT_MASK);
  179. writel(~0x0, g->base + GPIO_INT_CLR);
  180. ret = gpiochip_irqchip_add(&g->gc, &ftgpio_gpio_irqchip,
  181. 0, handle_bad_irq,
  182. IRQ_TYPE_NONE);
  183. if (ret) {
  184. dev_info(dev, "could not add irqchip\n");
  185. return ret;
  186. }
  187. gpiochip_set_chained_irqchip(&g->gc, &ftgpio_gpio_irqchip,
  188. irq, ftgpio_gpio_irq_handler);
  189. dev_info(dev, "FTGPIO010 @%p registered\n", g->base);
  190. return 0;
  191. }
  192. static const struct of_device_id ftgpio_gpio_of_match[] = {
  193. {
  194. .compatible = "cortina,gemini-gpio",
  195. },
  196. {
  197. .compatible = "moxa,moxart-gpio",
  198. },
  199. {
  200. .compatible = "faraday,ftgpio010",
  201. },
  202. {},
  203. };
  204. static struct platform_driver ftgpio_gpio_driver = {
  205. .driver = {
  206. .name = "ftgpio010-gpio",
  207. .of_match_table = of_match_ptr(ftgpio_gpio_of_match),
  208. },
  209. .probe = ftgpio_gpio_probe,
  210. };
  211. builtin_platform_driver(ftgpio_gpio_driver);