gpio-ftgpio010.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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/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_BYPASS_IN 0x0C
  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_RAW 0x24
  29. #define GPIO_INT_STAT_MASKED 0x28
  30. #define GPIO_INT_MASK 0x2C
  31. #define GPIO_INT_CLR 0x30
  32. #define GPIO_INT_TYPE 0x34
  33. #define GPIO_INT_BOTH_EDGE 0x38
  34. #define GPIO_INT_LEVEL 0x3C
  35. #define GPIO_DEBOUNCE_EN 0x40
  36. #define GPIO_DEBOUNCE_PRESCALE 0x44
  37. /**
  38. * struct ftgpio_gpio - Gemini GPIO state container
  39. * @dev: containing device for this instance
  40. * @gc: gpiochip for this instance
  41. */
  42. struct ftgpio_gpio {
  43. struct device *dev;
  44. struct gpio_chip gc;
  45. void __iomem *base;
  46. };
  47. static void ftgpio_gpio_ack_irq(struct irq_data *d)
  48. {
  49. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  50. struct ftgpio_gpio *g = gpiochip_get_data(gc);
  51. writel(BIT(irqd_to_hwirq(d)), g->base + GPIO_INT_CLR);
  52. }
  53. static void ftgpio_gpio_mask_irq(struct irq_data *d)
  54. {
  55. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  56. struct ftgpio_gpio *g = gpiochip_get_data(gc);
  57. u32 val;
  58. val = readl(g->base + GPIO_INT_EN);
  59. val &= ~BIT(irqd_to_hwirq(d));
  60. writel(val, g->base + GPIO_INT_EN);
  61. }
  62. static void ftgpio_gpio_unmask_irq(struct irq_data *d)
  63. {
  64. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  65. struct ftgpio_gpio *g = gpiochip_get_data(gc);
  66. u32 val;
  67. val = readl(g->base + GPIO_INT_EN);
  68. val |= BIT(irqd_to_hwirq(d));
  69. writel(val, g->base + GPIO_INT_EN);
  70. }
  71. static int ftgpio_gpio_set_irq_type(struct irq_data *d, unsigned int type)
  72. {
  73. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  74. struct ftgpio_gpio *g = gpiochip_get_data(gc);
  75. u32 mask = BIT(irqd_to_hwirq(d));
  76. u32 reg_both, reg_level, reg_type;
  77. reg_type = readl(g->base + GPIO_INT_TYPE);
  78. reg_level = readl(g->base + GPIO_INT_LEVEL);
  79. reg_both = readl(g->base + GPIO_INT_BOTH_EDGE);
  80. switch (type) {
  81. case IRQ_TYPE_EDGE_BOTH:
  82. irq_set_handler_locked(d, handle_edge_irq);
  83. reg_type &= ~mask;
  84. reg_both |= mask;
  85. break;
  86. case IRQ_TYPE_EDGE_RISING:
  87. irq_set_handler_locked(d, handle_edge_irq);
  88. reg_type &= ~mask;
  89. reg_both &= ~mask;
  90. reg_level &= ~mask;
  91. break;
  92. case IRQ_TYPE_EDGE_FALLING:
  93. irq_set_handler_locked(d, handle_edge_irq);
  94. reg_type &= ~mask;
  95. reg_both &= ~mask;
  96. reg_level |= mask;
  97. break;
  98. case IRQ_TYPE_LEVEL_HIGH:
  99. irq_set_handler_locked(d, handle_level_irq);
  100. reg_type |= mask;
  101. reg_level &= ~mask;
  102. break;
  103. case IRQ_TYPE_LEVEL_LOW:
  104. irq_set_handler_locked(d, handle_level_irq);
  105. reg_type |= mask;
  106. reg_level |= mask;
  107. break;
  108. default:
  109. irq_set_handler_locked(d, handle_bad_irq);
  110. return -EINVAL;
  111. }
  112. writel(reg_type, g->base + GPIO_INT_TYPE);
  113. writel(reg_level, g->base + GPIO_INT_LEVEL);
  114. writel(reg_both, g->base + GPIO_INT_BOTH_EDGE);
  115. ftgpio_gpio_ack_irq(d);
  116. return 0;
  117. }
  118. static struct irq_chip ftgpio_gpio_irqchip = {
  119. .name = "FTGPIO010",
  120. .irq_ack = ftgpio_gpio_ack_irq,
  121. .irq_mask = ftgpio_gpio_mask_irq,
  122. .irq_unmask = ftgpio_gpio_unmask_irq,
  123. .irq_set_type = ftgpio_gpio_set_irq_type,
  124. };
  125. static void ftgpio_gpio_irq_handler(struct irq_desc *desc)
  126. {
  127. struct gpio_chip *gc = irq_desc_get_handler_data(desc);
  128. struct ftgpio_gpio *g = gpiochip_get_data(gc);
  129. struct irq_chip *irqchip = irq_desc_get_chip(desc);
  130. int offset;
  131. unsigned long stat;
  132. chained_irq_enter(irqchip, desc);
  133. stat = readl(g->base + GPIO_INT_STAT_RAW);
  134. if (stat)
  135. for_each_set_bit(offset, &stat, gc->ngpio)
  136. generic_handle_irq(irq_find_mapping(gc->irq.domain,
  137. offset));
  138. chained_irq_exit(irqchip, desc);
  139. }
  140. static int ftgpio_gpio_probe(struct platform_device *pdev)
  141. {
  142. struct device *dev = &pdev->dev;
  143. struct resource *res;
  144. struct ftgpio_gpio *g;
  145. int irq;
  146. int ret;
  147. g = devm_kzalloc(dev, sizeof(*g), GFP_KERNEL);
  148. if (!g)
  149. return -ENOMEM;
  150. g->dev = dev;
  151. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  152. g->base = devm_ioremap_resource(dev, res);
  153. if (IS_ERR(g->base))
  154. return PTR_ERR(g->base);
  155. irq = platform_get_irq(pdev, 0);
  156. if (irq <= 0)
  157. return irq ? irq : -EINVAL;
  158. ret = bgpio_init(&g->gc, dev, 4,
  159. g->base + GPIO_DATA_IN,
  160. g->base + GPIO_DATA_SET,
  161. g->base + GPIO_DATA_CLR,
  162. g->base + GPIO_DIR,
  163. NULL,
  164. 0);
  165. if (ret) {
  166. dev_err(dev, "unable to init generic GPIO\n");
  167. return ret;
  168. }
  169. g->gc.label = "FTGPIO010";
  170. g->gc.base = -1;
  171. g->gc.parent = dev;
  172. g->gc.owner = THIS_MODULE;
  173. /* ngpio is set by bgpio_init() */
  174. ret = devm_gpiochip_add_data(dev, &g->gc, g);
  175. if (ret)
  176. return ret;
  177. /* Disable, unmask and clear all interrupts */
  178. writel(0x0, g->base + GPIO_INT_EN);
  179. writel(0x0, g->base + GPIO_INT_MASK);
  180. writel(~0x0, g->base + GPIO_INT_CLR);
  181. ret = gpiochip_irqchip_add(&g->gc, &ftgpio_gpio_irqchip,
  182. 0, handle_bad_irq,
  183. IRQ_TYPE_NONE);
  184. if (ret) {
  185. dev_info(dev, "could not add irqchip\n");
  186. return ret;
  187. }
  188. gpiochip_set_chained_irqchip(&g->gc, &ftgpio_gpio_irqchip,
  189. irq, ftgpio_gpio_irq_handler);
  190. dev_info(dev, "FTGPIO010 @%p registered\n", g->base);
  191. return 0;
  192. }
  193. static const struct of_device_id ftgpio_gpio_of_match[] = {
  194. {
  195. .compatible = "cortina,gemini-gpio",
  196. },
  197. {
  198. .compatible = "moxa,moxart-gpio",
  199. },
  200. {
  201. .compatible = "faraday,ftgpio010",
  202. },
  203. {},
  204. };
  205. static struct platform_driver ftgpio_gpio_driver = {
  206. .driver = {
  207. .name = "ftgpio010-gpio",
  208. .of_match_table = of_match_ptr(ftgpio_gpio_of_match),
  209. },
  210. .probe = ftgpio_gpio_probe,
  211. };
  212. builtin_platform_driver(ftgpio_gpio_driver);