gpio-ftgpio010.c 5.8 KB

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