gpio-vf610.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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/clk.h>
  11. #include <linux/err.h>
  12. #include <linux/gpio/driver.h>
  13. #include <linux/init.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/io.h>
  16. #include <linux/ioport.h>
  17. #include <linux/irq.h>
  18. #include <linux/pinctrl/consumer.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/property.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. bool have_dual_base;
  26. };
  27. struct vf610_gpio_port {
  28. struct gpio_chip gc;
  29. void __iomem *base;
  30. void __iomem *gpio_base;
  31. const struct fsl_gpio_soc_data *sdata;
  32. u8 irqc[VF610_GPIO_PER_PORT];
  33. struct clk *clk_port;
  34. struct clk *clk_gpio;
  35. int irq;
  36. spinlock_t lock; /* protect gpio direction registers */
  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 GPIO_PDDR 0x14
  44. #define PORT_PCR(n) ((n) * 0x4)
  45. #define PORT_PCR_IRQC_OFFSET 16
  46. #define PORT_ISFR 0xa0
  47. #define PORT_DFER 0xc0
  48. #define PORT_DFCR 0xc4
  49. #define PORT_DFWR 0xc8
  50. #define PORT_INT_OFF 0x0
  51. #define PORT_INT_LOGIC_ZERO 0x8
  52. #define PORT_INT_RISING_EDGE 0x9
  53. #define PORT_INT_FALLING_EDGE 0xa
  54. #define PORT_INT_EITHER_EDGE 0xb
  55. #define PORT_INT_LOGIC_ONE 0xc
  56. #define IMX8ULP_GPIO_BASE_OFF 0x40
  57. #define IMX8ULP_BASE_OFF 0x80
  58. static const struct fsl_gpio_soc_data vf610_data = {
  59. .have_dual_base = true,
  60. };
  61. static const struct fsl_gpio_soc_data imx_data = {
  62. .have_paddr = true,
  63. .have_dual_base = true,
  64. };
  65. static const struct fsl_gpio_soc_data imx8ulp_data = {
  66. .have_paddr = true,
  67. };
  68. static const struct of_device_id vf610_gpio_dt_ids[] = {
  69. { .compatible = "fsl,vf610-gpio", .data = &vf610_data },
  70. { .compatible = "fsl,imx7ulp-gpio", .data = &imx_data, },
  71. { .compatible = "fsl,imx8ulp-gpio", .data = &imx8ulp_data, },
  72. { /* sentinel */ }
  73. };
  74. static inline void vf610_gpio_writel(u32 val, void __iomem *reg)
  75. {
  76. writel_relaxed(val, reg);
  77. }
  78. static inline u32 vf610_gpio_readl(void __iomem *reg)
  79. {
  80. return readl_relaxed(reg);
  81. }
  82. static int vf610_gpio_get(struct gpio_chip *gc, unsigned int gpio)
  83. {
  84. struct vf610_gpio_port *port = gpiochip_get_data(gc);
  85. u32 mask = BIT(gpio);
  86. unsigned long offset = GPIO_PDIR;
  87. if (port->sdata->have_paddr) {
  88. mask &= vf610_gpio_readl(port->gpio_base + GPIO_PDDR);
  89. if (mask)
  90. offset = GPIO_PDOR;
  91. }
  92. return !!(vf610_gpio_readl(port->gpio_base + offset) & BIT(gpio));
  93. }
  94. static void vf610_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
  95. {
  96. struct vf610_gpio_port *port = gpiochip_get_data(gc);
  97. u32 mask = BIT(gpio);
  98. unsigned long offset = val ? GPIO_PSOR : GPIO_PCOR;
  99. vf610_gpio_writel(mask, port->gpio_base + offset);
  100. }
  101. static int vf610_gpio_direction_input(struct gpio_chip *chip, unsigned int gpio)
  102. {
  103. struct vf610_gpio_port *port = gpiochip_get_data(chip);
  104. u32 mask = BIT(gpio);
  105. u32 val;
  106. if (port->sdata->have_paddr) {
  107. guard(spinlock_irqsave)(&port->lock);
  108. val = vf610_gpio_readl(port->gpio_base + GPIO_PDDR);
  109. val &= ~mask;
  110. vf610_gpio_writel(val, port->gpio_base + GPIO_PDDR);
  111. }
  112. return pinctrl_gpio_direction_input(chip, gpio);
  113. }
  114. static int vf610_gpio_direction_output(struct gpio_chip *chip, unsigned int gpio,
  115. int value)
  116. {
  117. struct vf610_gpio_port *port = gpiochip_get_data(chip);
  118. u32 mask = BIT(gpio);
  119. u32 val;
  120. vf610_gpio_set(chip, gpio, value);
  121. if (port->sdata->have_paddr) {
  122. guard(spinlock_irqsave)(&port->lock);
  123. val = vf610_gpio_readl(port->gpio_base + GPIO_PDDR);
  124. val |= mask;
  125. vf610_gpio_writel(val, port->gpio_base + GPIO_PDDR);
  126. }
  127. return pinctrl_gpio_direction_output(chip, gpio);
  128. }
  129. static int vf610_gpio_get_direction(struct gpio_chip *gc, unsigned int gpio)
  130. {
  131. struct vf610_gpio_port *port = gpiochip_get_data(gc);
  132. u32 mask = BIT(gpio);
  133. mask &= vf610_gpio_readl(port->gpio_base + GPIO_PDDR);
  134. if (mask)
  135. return GPIO_LINE_DIRECTION_OUT;
  136. return GPIO_LINE_DIRECTION_IN;
  137. }
  138. static void vf610_gpio_irq_handler(struct irq_desc *desc)
  139. {
  140. struct vf610_gpio_port *port =
  141. gpiochip_get_data(irq_desc_get_handler_data(desc));
  142. struct irq_chip *chip = irq_desc_get_chip(desc);
  143. int pin;
  144. unsigned long irq_isfr;
  145. chained_irq_enter(chip, desc);
  146. irq_isfr = vf610_gpio_readl(port->base + PORT_ISFR);
  147. for_each_set_bit(pin, &irq_isfr, VF610_GPIO_PER_PORT) {
  148. vf610_gpio_writel(BIT(pin), port->base + PORT_ISFR);
  149. generic_handle_domain_irq(port->gc.irq.domain, pin);
  150. }
  151. chained_irq_exit(chip, desc);
  152. }
  153. static void vf610_gpio_irq_ack(struct irq_data *d)
  154. {
  155. struct vf610_gpio_port *port =
  156. gpiochip_get_data(irq_data_get_irq_chip_data(d));
  157. int gpio = d->hwirq;
  158. vf610_gpio_writel(BIT(gpio), port->base + PORT_ISFR);
  159. }
  160. static int vf610_gpio_irq_set_type(struct irq_data *d, u32 type)
  161. {
  162. struct vf610_gpio_port *port =
  163. gpiochip_get_data(irq_data_get_irq_chip_data(d));
  164. u8 irqc;
  165. switch (type) {
  166. case IRQ_TYPE_EDGE_RISING:
  167. irqc = PORT_INT_RISING_EDGE;
  168. break;
  169. case IRQ_TYPE_EDGE_FALLING:
  170. irqc = PORT_INT_FALLING_EDGE;
  171. break;
  172. case IRQ_TYPE_EDGE_BOTH:
  173. irqc = PORT_INT_EITHER_EDGE;
  174. break;
  175. case IRQ_TYPE_LEVEL_LOW:
  176. irqc = PORT_INT_LOGIC_ZERO;
  177. break;
  178. case IRQ_TYPE_LEVEL_HIGH:
  179. irqc = PORT_INT_LOGIC_ONE;
  180. break;
  181. default:
  182. return -EINVAL;
  183. }
  184. port->irqc[d->hwirq] = irqc;
  185. if (type & IRQ_TYPE_LEVEL_MASK)
  186. irq_set_handler_locked(d, handle_level_irq);
  187. else
  188. irq_set_handler_locked(d, handle_edge_irq);
  189. return 0;
  190. }
  191. static void vf610_gpio_irq_mask(struct irq_data *d)
  192. {
  193. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  194. struct vf610_gpio_port *port = gpiochip_get_data(gc);
  195. irq_hw_number_t gpio_num = irqd_to_hwirq(d);
  196. void __iomem *pcr_base = port->base + PORT_PCR(gpio_num);
  197. vf610_gpio_writel(0, pcr_base);
  198. gpiochip_disable_irq(gc, gpio_num);
  199. }
  200. static void vf610_gpio_irq_unmask(struct irq_data *d)
  201. {
  202. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  203. struct vf610_gpio_port *port = gpiochip_get_data(gc);
  204. irq_hw_number_t gpio_num = irqd_to_hwirq(d);
  205. void __iomem *pcr_base = port->base + PORT_PCR(gpio_num);
  206. gpiochip_enable_irq(gc, gpio_num);
  207. vf610_gpio_writel(port->irqc[gpio_num] << PORT_PCR_IRQC_OFFSET,
  208. pcr_base);
  209. }
  210. static int vf610_gpio_irq_set_wake(struct irq_data *d, u32 enable)
  211. {
  212. struct vf610_gpio_port *port =
  213. gpiochip_get_data(irq_data_get_irq_chip_data(d));
  214. if (enable)
  215. enable_irq_wake(port->irq);
  216. else
  217. disable_irq_wake(port->irq);
  218. return 0;
  219. }
  220. static const struct irq_chip vf610_irqchip = {
  221. .name = "gpio-vf610",
  222. .irq_ack = vf610_gpio_irq_ack,
  223. .irq_mask = vf610_gpio_irq_mask,
  224. .irq_unmask = vf610_gpio_irq_unmask,
  225. .irq_set_type = vf610_gpio_irq_set_type,
  226. .irq_set_wake = vf610_gpio_irq_set_wake,
  227. .flags = IRQCHIP_IMMUTABLE | IRQCHIP_MASK_ON_SUSPEND
  228. | IRQCHIP_ENABLE_WAKEUP_ON_SUSPEND,
  229. GPIOCHIP_IRQ_RESOURCE_HELPERS,
  230. };
  231. static void vf610_gpio_disable_clk(void *data)
  232. {
  233. clk_disable_unprepare(data);
  234. }
  235. static int vf610_gpio_probe(struct platform_device *pdev)
  236. {
  237. struct device *dev = &pdev->dev;
  238. struct vf610_gpio_port *port;
  239. struct gpio_chip *gc;
  240. struct gpio_irq_chip *girq;
  241. int i;
  242. int ret;
  243. bool dual_base;
  244. port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
  245. if (!port)
  246. return -ENOMEM;
  247. port->sdata = device_get_match_data(dev);
  248. spin_lock_init(&port->lock);
  249. dual_base = port->sdata->have_dual_base;
  250. /*
  251. * Handle legacy compatible combinations which used two reg values
  252. * for the i.MX8ULP and i.MX93.
  253. */
  254. if (device_is_compatible(dev, "fsl,imx7ulp-gpio") &&
  255. (device_is_compatible(dev, "fsl,imx93-gpio") ||
  256. (device_is_compatible(dev, "fsl,imx8ulp-gpio"))))
  257. dual_base = true;
  258. if (dual_base) {
  259. port->base = devm_platform_ioremap_resource(pdev, 0);
  260. if (IS_ERR(port->base))
  261. return PTR_ERR(port->base);
  262. port->gpio_base = devm_platform_ioremap_resource(pdev, 1);
  263. if (IS_ERR(port->gpio_base))
  264. return PTR_ERR(port->gpio_base);
  265. } else {
  266. port->base = devm_platform_ioremap_resource(pdev, 0);
  267. if (IS_ERR(port->base))
  268. return PTR_ERR(port->base);
  269. port->gpio_base = port->base + IMX8ULP_GPIO_BASE_OFF;
  270. port->base = port->base + IMX8ULP_BASE_OFF;
  271. }
  272. port->irq = platform_get_irq(pdev, 0);
  273. if (port->irq < 0)
  274. return port->irq;
  275. port->clk_port = devm_clk_get(dev, "port");
  276. ret = PTR_ERR_OR_ZERO(port->clk_port);
  277. if (!ret) {
  278. ret = clk_prepare_enable(port->clk_port);
  279. if (ret)
  280. return ret;
  281. ret = devm_add_action_or_reset(dev, vf610_gpio_disable_clk,
  282. port->clk_port);
  283. if (ret)
  284. return ret;
  285. } else if (ret == -EPROBE_DEFER) {
  286. /*
  287. * Percolate deferrals, for anything else,
  288. * just live without the clocking.
  289. */
  290. return ret;
  291. }
  292. port->clk_gpio = devm_clk_get(dev, "gpio");
  293. ret = PTR_ERR_OR_ZERO(port->clk_gpio);
  294. if (!ret) {
  295. ret = clk_prepare_enable(port->clk_gpio);
  296. if (ret)
  297. return ret;
  298. ret = devm_add_action_or_reset(dev, vf610_gpio_disable_clk,
  299. port->clk_gpio);
  300. if (ret)
  301. return ret;
  302. } else if (ret == -EPROBE_DEFER) {
  303. return ret;
  304. }
  305. gc = &port->gc;
  306. gc->parent = dev;
  307. gc->label = dev_name(dev);
  308. gc->ngpio = VF610_GPIO_PER_PORT;
  309. gc->base = -1;
  310. gc->request = gpiochip_generic_request;
  311. gc->free = gpiochip_generic_free;
  312. gc->direction_input = vf610_gpio_direction_input;
  313. gc->get = vf610_gpio_get;
  314. gc->direction_output = vf610_gpio_direction_output;
  315. gc->set = vf610_gpio_set;
  316. /*
  317. * only IP has Port Data Direction Register(PDDR) can
  318. * support get direction
  319. */
  320. if (port->sdata->have_paddr)
  321. gc->get_direction = vf610_gpio_get_direction;
  322. /* Mask all GPIO interrupts */
  323. for (i = 0; i < gc->ngpio; i++)
  324. vf610_gpio_writel(0, port->base + PORT_PCR(i));
  325. /* Clear the interrupt status register for all GPIO's */
  326. vf610_gpio_writel(~0, port->base + PORT_ISFR);
  327. girq = &gc->irq;
  328. gpio_irq_chip_set_chip(girq, &vf610_irqchip);
  329. girq->parent_handler = vf610_gpio_irq_handler;
  330. girq->num_parents = 1;
  331. girq->parents = devm_kcalloc(&pdev->dev, 1,
  332. sizeof(*girq->parents),
  333. GFP_KERNEL);
  334. if (!girq->parents)
  335. return -ENOMEM;
  336. girq->parents[0] = port->irq;
  337. girq->default_type = IRQ_TYPE_NONE;
  338. girq->handler = handle_edge_irq;
  339. return devm_gpiochip_add_data(dev, gc, port);
  340. }
  341. static struct platform_driver vf610_gpio_driver = {
  342. .driver = {
  343. .name = "gpio-vf610",
  344. .of_match_table = vf610_gpio_dt_ids,
  345. },
  346. .probe = vf610_gpio_probe,
  347. };
  348. builtin_platform_driver(vf610_gpio_driver);