gpio-vf610.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * Freescale vf610 GPIO support through PORT and GPIO
  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/platform_device.h>
  26. #include <linux/of.h>
  27. #include <linux/of_device.h>
  28. #include <linux/of_irq.h>
  29. #define VF610_GPIO_PER_PORT 32
  30. struct vf610_gpio_port {
  31. struct gpio_chip gc;
  32. void __iomem *base;
  33. void __iomem *gpio_base;
  34. u8 irqc[VF610_GPIO_PER_PORT];
  35. int irq;
  36. };
  37. #define GPIO_PDOR 0x00
  38. #define GPIO_PSOR 0x04
  39. #define GPIO_PCOR 0x08
  40. #define GPIO_PTOR 0x0c
  41. #define GPIO_PDIR 0x10
  42. #define PORT_PCR(n) ((n) * 0x4)
  43. #define PORT_PCR_IRQC_OFFSET 16
  44. #define PORT_ISFR 0xa0
  45. #define PORT_DFER 0xc0
  46. #define PORT_DFCR 0xc4
  47. #define PORT_DFWR 0xc8
  48. #define PORT_INT_OFF 0x0
  49. #define PORT_INT_LOGIC_ZERO 0x8
  50. #define PORT_INT_RISING_EDGE 0x9
  51. #define PORT_INT_FALLING_EDGE 0xa
  52. #define PORT_INT_EITHER_EDGE 0xb
  53. #define PORT_INT_LOGIC_ONE 0xc
  54. static struct irq_chip vf610_gpio_irq_chip;
  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_get(struct gpio_chip *gc, unsigned int gpio)
  68. {
  69. struct vf610_gpio_port *port = gpiochip_get_data(gc);
  70. return !!(vf610_gpio_readl(port->gpio_base + GPIO_PDIR) & BIT(gpio));
  71. }
  72. static void vf610_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
  73. {
  74. struct vf610_gpio_port *port = gpiochip_get_data(gc);
  75. unsigned long mask = BIT(gpio);
  76. if (val)
  77. vf610_gpio_writel(mask, port->gpio_base + GPIO_PSOR);
  78. else
  79. vf610_gpio_writel(mask, port->gpio_base + GPIO_PCOR);
  80. }
  81. static int vf610_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
  82. {
  83. return pinctrl_gpio_direction_input(chip->base + gpio);
  84. }
  85. static int vf610_gpio_direction_output(struct gpio_chip *chip, unsigned gpio,
  86. int value)
  87. {
  88. vf610_gpio_set(chip, gpio, value);
  89. return pinctrl_gpio_direction_output(chip->base + gpio);
  90. }
  91. static void vf610_gpio_irq_handler(struct irq_desc *desc)
  92. {
  93. struct vf610_gpio_port *port =
  94. gpiochip_get_data(irq_desc_get_handler_data(desc));
  95. struct irq_chip *chip = irq_desc_get_chip(desc);
  96. int pin;
  97. unsigned long irq_isfr;
  98. chained_irq_enter(chip, desc);
  99. irq_isfr = vf610_gpio_readl(port->base + PORT_ISFR);
  100. for_each_set_bit(pin, &irq_isfr, VF610_GPIO_PER_PORT) {
  101. vf610_gpio_writel(BIT(pin), port->base + PORT_ISFR);
  102. generic_handle_irq(irq_find_mapping(port->gc.irqdomain, pin));
  103. }
  104. chained_irq_exit(chip, desc);
  105. }
  106. static void vf610_gpio_irq_ack(struct irq_data *d)
  107. {
  108. struct vf610_gpio_port *port =
  109. gpiochip_get_data(irq_data_get_irq_chip_data(d));
  110. int gpio = d->hwirq;
  111. vf610_gpio_writel(BIT(gpio), port->base + PORT_ISFR);
  112. }
  113. static int vf610_gpio_irq_set_type(struct irq_data *d, u32 type)
  114. {
  115. struct vf610_gpio_port *port =
  116. gpiochip_get_data(irq_data_get_irq_chip_data(d));
  117. u8 irqc;
  118. switch (type) {
  119. case IRQ_TYPE_EDGE_RISING:
  120. irqc = PORT_INT_RISING_EDGE;
  121. break;
  122. case IRQ_TYPE_EDGE_FALLING:
  123. irqc = PORT_INT_FALLING_EDGE;
  124. break;
  125. case IRQ_TYPE_EDGE_BOTH:
  126. irqc = PORT_INT_EITHER_EDGE;
  127. break;
  128. case IRQ_TYPE_LEVEL_LOW:
  129. irqc = PORT_INT_LOGIC_ZERO;
  130. break;
  131. case IRQ_TYPE_LEVEL_HIGH:
  132. irqc = PORT_INT_LOGIC_ONE;
  133. break;
  134. default:
  135. return -EINVAL;
  136. }
  137. port->irqc[d->hwirq] = irqc;
  138. if (type & IRQ_TYPE_LEVEL_MASK)
  139. irq_set_handler_locked(d, handle_level_irq);
  140. else
  141. irq_set_handler_locked(d, handle_edge_irq);
  142. return 0;
  143. }
  144. static void vf610_gpio_irq_mask(struct irq_data *d)
  145. {
  146. struct vf610_gpio_port *port =
  147. gpiochip_get_data(irq_data_get_irq_chip_data(d));
  148. void __iomem *pcr_base = port->base + PORT_PCR(d->hwirq);
  149. vf610_gpio_writel(0, pcr_base);
  150. }
  151. static void vf610_gpio_irq_unmask(struct irq_data *d)
  152. {
  153. struct vf610_gpio_port *port =
  154. gpiochip_get_data(irq_data_get_irq_chip_data(d));
  155. void __iomem *pcr_base = port->base + PORT_PCR(d->hwirq);
  156. vf610_gpio_writel(port->irqc[d->hwirq] << PORT_PCR_IRQC_OFFSET,
  157. pcr_base);
  158. }
  159. static int vf610_gpio_irq_set_wake(struct irq_data *d, u32 enable)
  160. {
  161. struct vf610_gpio_port *port =
  162. gpiochip_get_data(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->parent = 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 = gpiochip_generic_request;
  206. gc->free = gpiochip_generic_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_data(gc, port);
  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_edge_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);