gpio-vf610.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 const struct of_device_id vf610_gpio_dt_ids[] = {
  56. { .compatible = "fsl,vf610-gpio" },
  57. { /* sentinel */ }
  58. };
  59. static inline void vf610_gpio_writel(u32 val, void __iomem *reg)
  60. {
  61. writel_relaxed(val, reg);
  62. }
  63. static inline u32 vf610_gpio_readl(void __iomem *reg)
  64. {
  65. return readl_relaxed(reg);
  66. }
  67. static int vf610_gpio_request(struct gpio_chip *chip, unsigned offset)
  68. {
  69. return pinctrl_request_gpio(chip->base + offset);
  70. }
  71. static void vf610_gpio_free(struct gpio_chip *chip, unsigned offset)
  72. {
  73. pinctrl_free_gpio(chip->base + offset);
  74. }
  75. static int vf610_gpio_get(struct gpio_chip *gc, unsigned int gpio)
  76. {
  77. struct vf610_gpio_port *port =
  78. container_of(gc, struct vf610_gpio_port, gc);
  79. return !!(vf610_gpio_readl(port->gpio_base + GPIO_PDIR) & BIT(gpio));
  80. }
  81. static void vf610_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
  82. {
  83. struct vf610_gpio_port *port =
  84. container_of(gc, struct vf610_gpio_port, gc);
  85. unsigned long mask = BIT(gpio);
  86. if (val)
  87. vf610_gpio_writel(mask, port->gpio_base + GPIO_PSOR);
  88. else
  89. vf610_gpio_writel(mask, port->gpio_base + GPIO_PCOR);
  90. }
  91. static int vf610_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
  92. {
  93. return pinctrl_gpio_direction_input(chip->base + gpio);
  94. }
  95. static int vf610_gpio_direction_output(struct gpio_chip *chip, unsigned gpio,
  96. int value)
  97. {
  98. vf610_gpio_set(chip, gpio, value);
  99. return pinctrl_gpio_direction_output(chip->base + gpio);
  100. }
  101. static void vf610_gpio_irq_handler(u32 irq, struct irq_desc *desc)
  102. {
  103. struct vf610_gpio_port *port = irq_get_handler_data(irq);
  104. struct irq_chip *chip = irq_desc_get_chip(desc);
  105. int pin;
  106. unsigned long irq_isfr;
  107. chained_irq_enter(chip, desc);
  108. irq_isfr = vf610_gpio_readl(port->base + PORT_ISFR);
  109. for_each_set_bit(pin, &irq_isfr, VF610_GPIO_PER_PORT) {
  110. vf610_gpio_writel(BIT(pin), port->base + PORT_ISFR);
  111. generic_handle_irq(irq_find_mapping(port->gc.irqdomain, pin));
  112. }
  113. chained_irq_exit(chip, desc);
  114. }
  115. static void vf610_gpio_irq_ack(struct irq_data *d)
  116. {
  117. struct vf610_gpio_port *port = irq_data_get_irq_chip_data(d);
  118. int gpio = d->hwirq;
  119. vf610_gpio_writel(BIT(gpio), port->base + PORT_ISFR);
  120. }
  121. static int vf610_gpio_irq_set_type(struct irq_data *d, u32 type)
  122. {
  123. struct vf610_gpio_port *port = irq_data_get_irq_chip_data(d);
  124. u8 irqc;
  125. switch (type) {
  126. case IRQ_TYPE_EDGE_RISING:
  127. irqc = PORT_INT_RISING_EDGE;
  128. break;
  129. case IRQ_TYPE_EDGE_FALLING:
  130. irqc = PORT_INT_FALLING_EDGE;
  131. break;
  132. case IRQ_TYPE_EDGE_BOTH:
  133. irqc = PORT_INT_EITHER_EDGE;
  134. break;
  135. case IRQ_TYPE_LEVEL_LOW:
  136. irqc = PORT_INT_LOGIC_ZERO;
  137. break;
  138. case IRQ_TYPE_LEVEL_HIGH:
  139. irqc = PORT_INT_LOGIC_ONE;
  140. break;
  141. default:
  142. return -EINVAL;
  143. }
  144. port->irqc[d->hwirq] = irqc;
  145. return 0;
  146. }
  147. static void vf610_gpio_irq_mask(struct irq_data *d)
  148. {
  149. struct vf610_gpio_port *port = irq_data_get_irq_chip_data(d);
  150. void __iomem *pcr_base = port->base + PORT_PCR(d->hwirq);
  151. vf610_gpio_writel(0, pcr_base);
  152. }
  153. static void vf610_gpio_irq_unmask(struct irq_data *d)
  154. {
  155. struct vf610_gpio_port *port = irq_data_get_irq_chip_data(d);
  156. void __iomem *pcr_base = port->base + PORT_PCR(d->hwirq);
  157. vf610_gpio_writel(port->irqc[d->hwirq] << PORT_PCR_IRQC_OFFSET,
  158. pcr_base);
  159. }
  160. static int vf610_gpio_irq_set_wake(struct irq_data *d, u32 enable)
  161. {
  162. struct vf610_gpio_port *port = irq_data_get_irq_chip_data(d);
  163. if (enable)
  164. enable_irq_wake(port->irq);
  165. else
  166. disable_irq_wake(port->irq);
  167. return 0;
  168. }
  169. static struct irq_chip vf610_gpio_irq_chip = {
  170. .name = "gpio-vf610",
  171. .irq_ack = vf610_gpio_irq_ack,
  172. .irq_mask = vf610_gpio_irq_mask,
  173. .irq_unmask = vf610_gpio_irq_unmask,
  174. .irq_set_type = vf610_gpio_irq_set_type,
  175. .irq_set_wake = vf610_gpio_irq_set_wake,
  176. };
  177. static int vf610_gpio_probe(struct platform_device *pdev)
  178. {
  179. struct device *dev = &pdev->dev;
  180. struct device_node *np = dev->of_node;
  181. struct vf610_gpio_port *port;
  182. struct resource *iores;
  183. struct gpio_chip *gc;
  184. int ret;
  185. port = devm_kzalloc(&pdev->dev, sizeof(*port), GFP_KERNEL);
  186. if (!port)
  187. return -ENOMEM;
  188. iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  189. port->base = devm_ioremap_resource(dev, iores);
  190. if (IS_ERR(port->base))
  191. return PTR_ERR(port->base);
  192. iores = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  193. port->gpio_base = devm_ioremap_resource(dev, iores);
  194. if (IS_ERR(port->gpio_base))
  195. return PTR_ERR(port->gpio_base);
  196. port->irq = platform_get_irq(pdev, 0);
  197. if (port->irq < 0)
  198. return port->irq;
  199. gc = &port->gc;
  200. gc->of_node = np;
  201. gc->dev = dev;
  202. gc->label = "vf610-gpio";
  203. gc->ngpio = VF610_GPIO_PER_PORT;
  204. gc->base = of_alias_get_id(np, "gpio") * VF610_GPIO_PER_PORT;
  205. gc->request = vf610_gpio_request;
  206. gc->free = vf610_gpio_free;
  207. gc->direction_input = vf610_gpio_direction_input;
  208. gc->get = vf610_gpio_get;
  209. gc->direction_output = vf610_gpio_direction_output;
  210. gc->set = vf610_gpio_set;
  211. ret = gpiochip_add(gc);
  212. if (ret < 0)
  213. return ret;
  214. /* Clear the interrupt status register for all GPIO's */
  215. vf610_gpio_writel(~0, port->base + PORT_ISFR);
  216. ret = gpiochip_irqchip_add(gc, &vf610_gpio_irq_chip, 0,
  217. handle_simple_irq, IRQ_TYPE_NONE);
  218. if (ret) {
  219. dev_err(dev, "failed to add irqchip\n");
  220. gpiochip_remove(gc);
  221. return ret;
  222. }
  223. gpiochip_set_chained_irqchip(gc, &vf610_gpio_irq_chip, port->irq,
  224. vf610_gpio_irq_handler);
  225. return 0;
  226. }
  227. static struct platform_driver vf610_gpio_driver = {
  228. .driver = {
  229. .name = "gpio-vf610",
  230. .of_match_table = vf610_gpio_dt_ids,
  231. },
  232. .probe = vf610_gpio_probe,
  233. };
  234. static int __init gpio_vf610_init(void)
  235. {
  236. return platform_driver_register(&vf610_gpio_driver);
  237. }
  238. device_initcall(gpio_vf610_init);
  239. MODULE_AUTHOR("Stefan Agner <stefan@agner.ch>");
  240. MODULE_DESCRIPTION("Freescale VF610 GPIO");
  241. MODULE_LICENSE("GPL v2");