gpio-vf610.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Freescale vf610 GPIO support through PORT and GPIO
  4. *
  5. * Copyright (c) 2014 Toradex AG.
  6. *
  7. * Author: Stefan Agner <stefan@agner.ch>.
  8. */
  9. #include <linux/bitops.h>
  10. #include <linux/err.h>
  11. #include <linux/gpio/driver.h>
  12. #include <linux/init.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/io.h>
  15. #include <linux/ioport.h>
  16. #include <linux/irq.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/of.h>
  19. #include <linux/of_device.h>
  20. #include <linux/of_irq.h>
  21. #define VF610_GPIO_PER_PORT 32
  22. struct fsl_gpio_soc_data {
  23. /* SoCs has a Port Data Direction Register (PDDR) */
  24. bool have_paddr;
  25. };
  26. struct vf610_gpio_port {
  27. struct gpio_chip gc;
  28. void __iomem *base;
  29. void __iomem *gpio_base;
  30. const struct fsl_gpio_soc_data *sdata;
  31. u8 irqc[VF610_GPIO_PER_PORT];
  32. int irq;
  33. };
  34. #define GPIO_PDOR 0x00
  35. #define GPIO_PSOR 0x04
  36. #define GPIO_PCOR 0x08
  37. #define GPIO_PTOR 0x0c
  38. #define GPIO_PDIR 0x10
  39. #define GPIO_PDDR 0x14
  40. #define PORT_PCR(n) ((n) * 0x4)
  41. #define PORT_PCR_IRQC_OFFSET 16
  42. #define PORT_ISFR 0xa0
  43. #define PORT_DFER 0xc0
  44. #define PORT_DFCR 0xc4
  45. #define PORT_DFWR 0xc8
  46. #define PORT_INT_OFF 0x0
  47. #define PORT_INT_LOGIC_ZERO 0x8
  48. #define PORT_INT_RISING_EDGE 0x9
  49. #define PORT_INT_FALLING_EDGE 0xa
  50. #define PORT_INT_EITHER_EDGE 0xb
  51. #define PORT_INT_LOGIC_ONE 0xc
  52. static struct irq_chip vf610_gpio_irq_chip;
  53. static const struct fsl_gpio_soc_data imx_data = {
  54. .have_paddr = true,
  55. };
  56. static const struct of_device_id vf610_gpio_dt_ids[] = {
  57. { .compatible = "fsl,vf610-gpio", .data = NULL, },
  58. { .compatible = "fsl,imx7ulp-gpio", .data = &imx_data, },
  59. { /* sentinel */ }
  60. };
  61. static inline void vf610_gpio_writel(u32 val, void __iomem *reg)
  62. {
  63. writel_relaxed(val, reg);
  64. }
  65. static inline u32 vf610_gpio_readl(void __iomem *reg)
  66. {
  67. return readl_relaxed(reg);
  68. }
  69. static int vf610_gpio_get(struct gpio_chip *gc, unsigned int gpio)
  70. {
  71. struct vf610_gpio_port *port = gpiochip_get_data(gc);
  72. unsigned long mask = BIT(gpio);
  73. void __iomem *addr;
  74. if (port->sdata && port->sdata->have_paddr) {
  75. mask &= vf610_gpio_readl(port->gpio_base + GPIO_PDDR);
  76. addr = mask ? port->gpio_base + GPIO_PDOR :
  77. port->gpio_base + GPIO_PDIR;
  78. return !!(vf610_gpio_readl(addr) & BIT(gpio));
  79. } else {
  80. return !!(vf610_gpio_readl(port->gpio_base + GPIO_PDIR)
  81. & BIT(gpio));
  82. }
  83. }
  84. static void vf610_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
  85. {
  86. struct vf610_gpio_port *port = gpiochip_get_data(gc);
  87. unsigned long mask = BIT(gpio);
  88. if (val)
  89. vf610_gpio_writel(mask, port->gpio_base + GPIO_PSOR);
  90. else
  91. vf610_gpio_writel(mask, port->gpio_base + GPIO_PCOR);
  92. }
  93. static int vf610_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
  94. {
  95. struct vf610_gpio_port *port = gpiochip_get_data(chip);
  96. unsigned long mask = BIT(gpio);
  97. u32 val;
  98. if (port->sdata && port->sdata->have_paddr) {
  99. val = vf610_gpio_readl(port->gpio_base + GPIO_PDDR);
  100. val &= ~mask;
  101. vf610_gpio_writel(val, port->gpio_base + GPIO_PDDR);
  102. }
  103. return pinctrl_gpio_direction_input(chip->base + gpio);
  104. }
  105. static int vf610_gpio_direction_output(struct gpio_chip *chip, unsigned gpio,
  106. int value)
  107. {
  108. struct vf610_gpio_port *port = gpiochip_get_data(chip);
  109. unsigned long mask = BIT(gpio);
  110. if (port->sdata && port->sdata->have_paddr)
  111. vf610_gpio_writel(mask, port->gpio_base + GPIO_PDDR);
  112. vf610_gpio_set(chip, gpio, value);
  113. return pinctrl_gpio_direction_output(chip->base + gpio);
  114. }
  115. static void vf610_gpio_irq_handler(struct irq_desc *desc)
  116. {
  117. struct vf610_gpio_port *port =
  118. gpiochip_get_data(irq_desc_get_handler_data(desc));
  119. struct irq_chip *chip = irq_desc_get_chip(desc);
  120. int pin;
  121. unsigned long irq_isfr;
  122. chained_irq_enter(chip, desc);
  123. irq_isfr = vf610_gpio_readl(port->base + PORT_ISFR);
  124. for_each_set_bit(pin, &irq_isfr, VF610_GPIO_PER_PORT) {
  125. vf610_gpio_writel(BIT(pin), port->base + PORT_ISFR);
  126. generic_handle_irq(irq_find_mapping(port->gc.irq.domain, pin));
  127. }
  128. chained_irq_exit(chip, desc);
  129. }
  130. static void vf610_gpio_irq_ack(struct irq_data *d)
  131. {
  132. struct vf610_gpio_port *port =
  133. gpiochip_get_data(irq_data_get_irq_chip_data(d));
  134. int gpio = d->hwirq;
  135. vf610_gpio_writel(BIT(gpio), port->base + PORT_ISFR);
  136. }
  137. static int vf610_gpio_irq_set_type(struct irq_data *d, u32 type)
  138. {
  139. struct vf610_gpio_port *port =
  140. gpiochip_get_data(irq_data_get_irq_chip_data(d));
  141. u8 irqc;
  142. switch (type) {
  143. case IRQ_TYPE_EDGE_RISING:
  144. irqc = PORT_INT_RISING_EDGE;
  145. break;
  146. case IRQ_TYPE_EDGE_FALLING:
  147. irqc = PORT_INT_FALLING_EDGE;
  148. break;
  149. case IRQ_TYPE_EDGE_BOTH:
  150. irqc = PORT_INT_EITHER_EDGE;
  151. break;
  152. case IRQ_TYPE_LEVEL_LOW:
  153. irqc = PORT_INT_LOGIC_ZERO;
  154. break;
  155. case IRQ_TYPE_LEVEL_HIGH:
  156. irqc = PORT_INT_LOGIC_ONE;
  157. break;
  158. default:
  159. return -EINVAL;
  160. }
  161. port->irqc[d->hwirq] = irqc;
  162. if (type & IRQ_TYPE_LEVEL_MASK)
  163. irq_set_handler_locked(d, handle_level_irq);
  164. else
  165. irq_set_handler_locked(d, handle_edge_irq);
  166. return 0;
  167. }
  168. static void vf610_gpio_irq_mask(struct irq_data *d)
  169. {
  170. struct vf610_gpio_port *port =
  171. gpiochip_get_data(irq_data_get_irq_chip_data(d));
  172. void __iomem *pcr_base = port->base + PORT_PCR(d->hwirq);
  173. vf610_gpio_writel(0, pcr_base);
  174. }
  175. static void vf610_gpio_irq_unmask(struct irq_data *d)
  176. {
  177. struct vf610_gpio_port *port =
  178. gpiochip_get_data(irq_data_get_irq_chip_data(d));
  179. void __iomem *pcr_base = port->base + PORT_PCR(d->hwirq);
  180. vf610_gpio_writel(port->irqc[d->hwirq] << PORT_PCR_IRQC_OFFSET,
  181. pcr_base);
  182. }
  183. static int vf610_gpio_irq_set_wake(struct irq_data *d, u32 enable)
  184. {
  185. struct vf610_gpio_port *port =
  186. gpiochip_get_data(irq_data_get_irq_chip_data(d));
  187. if (enable)
  188. enable_irq_wake(port->irq);
  189. else
  190. disable_irq_wake(port->irq);
  191. return 0;
  192. }
  193. static struct irq_chip vf610_gpio_irq_chip = {
  194. .name = "gpio-vf610",
  195. .irq_ack = vf610_gpio_irq_ack,
  196. .irq_mask = vf610_gpio_irq_mask,
  197. .irq_unmask = vf610_gpio_irq_unmask,
  198. .irq_set_type = vf610_gpio_irq_set_type,
  199. .irq_set_wake = vf610_gpio_irq_set_wake,
  200. };
  201. static int vf610_gpio_probe(struct platform_device *pdev)
  202. {
  203. struct device *dev = &pdev->dev;
  204. struct device_node *np = dev->of_node;
  205. struct vf610_gpio_port *port;
  206. struct resource *iores;
  207. struct gpio_chip *gc;
  208. int ret;
  209. port = devm_kzalloc(&pdev->dev, sizeof(*port), GFP_KERNEL);
  210. if (!port)
  211. return -ENOMEM;
  212. port->sdata = of_device_get_match_data(dev);
  213. iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  214. port->base = devm_ioremap_resource(dev, iores);
  215. if (IS_ERR(port->base))
  216. return PTR_ERR(port->base);
  217. iores = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  218. port->gpio_base = devm_ioremap_resource(dev, iores);
  219. if (IS_ERR(port->gpio_base))
  220. return PTR_ERR(port->gpio_base);
  221. port->irq = platform_get_irq(pdev, 0);
  222. if (port->irq < 0)
  223. return port->irq;
  224. gc = &port->gc;
  225. gc->of_node = np;
  226. gc->parent = dev;
  227. gc->label = "vf610-gpio";
  228. gc->ngpio = VF610_GPIO_PER_PORT;
  229. gc->base = of_alias_get_id(np, "gpio") * VF610_GPIO_PER_PORT;
  230. gc->request = gpiochip_generic_request;
  231. gc->free = gpiochip_generic_free;
  232. gc->direction_input = vf610_gpio_direction_input;
  233. gc->get = vf610_gpio_get;
  234. gc->direction_output = vf610_gpio_direction_output;
  235. gc->set = vf610_gpio_set;
  236. ret = gpiochip_add_data(gc, port);
  237. if (ret < 0)
  238. return ret;
  239. /* Clear the interrupt status register for all GPIO's */
  240. vf610_gpio_writel(~0, port->base + PORT_ISFR);
  241. ret = gpiochip_irqchip_add(gc, &vf610_gpio_irq_chip, 0,
  242. handle_edge_irq, IRQ_TYPE_NONE);
  243. if (ret) {
  244. dev_err(dev, "failed to add irqchip\n");
  245. gpiochip_remove(gc);
  246. return ret;
  247. }
  248. gpiochip_set_chained_irqchip(gc, &vf610_gpio_irq_chip, port->irq,
  249. vf610_gpio_irq_handler);
  250. return 0;
  251. }
  252. static struct platform_driver vf610_gpio_driver = {
  253. .driver = {
  254. .name = "gpio-vf610",
  255. .of_match_table = vf610_gpio_dt_ids,
  256. },
  257. .probe = vf610_gpio_probe,
  258. };
  259. builtin_platform_driver(vf610_gpio_driver);