gpio-vf610.c 7.4 KB

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