blob: 5960396c8d9a263d835f764b4e0f0de95fe06b30 [file] [log] [blame]
Linus Walleij36e2add2018-09-07 22:04:50 +02001// SPDX-License-Identifier: GPL-2.0+
Stefan Agner7f2691a2014-10-16 21:47:58 +02002/*
Paul Gortmakeradaaf632016-08-22 12:48:33 -04003 * Freescale vf610 GPIO support through PORT and GPIO
Stefan Agner7f2691a2014-10-16 21:47:58 +02004 *
5 * Copyright (c) 2014 Toradex AG.
6 *
7 * Author: Stefan Agner <stefan@agner.ch>.
Stefan Agner7f2691a2014-10-16 21:47:58 +02008 */
Stefan Agner7f2691a2014-10-16 21:47:58 +02009#include <linux/bitops.h>
10#include <linux/err.h>
Linus Walleij45e82962018-09-07 22:03:07 +020011#include <linux/gpio/driver.h>
Stefan Agner7f2691a2014-10-16 21:47:58 +020012#include <linux/init.h>
13#include <linux/interrupt.h>
14#include <linux/io.h>
15#include <linux/ioport.h>
16#include <linux/irq.h>
Stefan Agner7f2691a2014-10-16 21:47:58 +020017#include <linux/platform_device.h>
18#include <linux/of.h>
19#include <linux/of_device.h>
20#include <linux/of_irq.h>
21
22#define VF610_GPIO_PER_PORT 32
23
Dong Aisheng659d8a62017-08-01 22:04:17 +080024struct fsl_gpio_soc_data {
25 /* SoCs has a Port Data Direction Register (PDDR) */
26 bool have_paddr;
27};
28
Stefan Agner7f2691a2014-10-16 21:47:58 +020029struct vf610_gpio_port {
30 struct gpio_chip gc;
31 void __iomem *base;
32 void __iomem *gpio_base;
Dong Aisheng659d8a62017-08-01 22:04:17 +080033 const struct fsl_gpio_soc_data *sdata;
Stefan Agner7f2691a2014-10-16 21:47:58 +020034 u8 irqc[VF610_GPIO_PER_PORT];
35 int irq;
36};
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
Dong Aisheng659d8a62017-08-01 22:04:17 +080043#define GPIO_PDDR 0x14
Stefan Agner7f2691a2014-10-16 21:47:58 +020044
45#define PORT_PCR(n) ((n) * 0x4)
46#define PORT_PCR_IRQC_OFFSET 16
47
48#define PORT_ISFR 0xa0
49#define PORT_DFER 0xc0
50#define PORT_DFCR 0xc4
51#define PORT_DFWR 0xc8
52
53#define PORT_INT_OFF 0x0
54#define PORT_INT_LOGIC_ZERO 0x8
55#define PORT_INT_RISING_EDGE 0x9
56#define PORT_INT_FALLING_EDGE 0xa
57#define PORT_INT_EITHER_EDGE 0xb
58#define PORT_INT_LOGIC_ONE 0xc
59
Stefan Agnerfd968112015-08-21 15:56:42 -070060static struct irq_chip vf610_gpio_irq_chip;
61
Dong Aisheng659d8a62017-08-01 22:04:17 +080062static const struct fsl_gpio_soc_data imx_data = {
63 .have_paddr = true,
64};
65
Stefan Agner7f2691a2014-10-16 21:47:58 +020066static const struct of_device_id vf610_gpio_dt_ids[] = {
Dong Aisheng659d8a62017-08-01 22:04:17 +080067 { .compatible = "fsl,vf610-gpio", .data = NULL, },
68 { .compatible = "fsl,imx7ulp-gpio", .data = &imx_data, },
Stefan Agner7f2691a2014-10-16 21:47:58 +020069 { /* sentinel */ }
70};
71
72static inline void vf610_gpio_writel(u32 val, void __iomem *reg)
73{
74 writel_relaxed(val, reg);
75}
76
77static inline u32 vf610_gpio_readl(void __iomem *reg)
78{
79 return readl_relaxed(reg);
80}
81
Stefan Agner7f2691a2014-10-16 21:47:58 +020082static int vf610_gpio_get(struct gpio_chip *gc, unsigned int gpio)
83{
Linus Walleij65389b42015-12-07 15:03:30 +010084 struct vf610_gpio_port *port = gpiochip_get_data(gc);
Dong Aisheng659d8a62017-08-01 22:04:17 +080085 unsigned long mask = BIT(gpio);
86 void __iomem *addr;
Stefan Agner7f2691a2014-10-16 21:47:58 +020087
Dong Aisheng659d8a62017-08-01 22:04:17 +080088 if (port->sdata && port->sdata->have_paddr) {
89 mask &= vf610_gpio_readl(port->gpio_base + GPIO_PDDR);
90 addr = mask ? port->gpio_base + GPIO_PDOR :
91 port->gpio_base + GPIO_PDIR;
92 return !!(vf610_gpio_readl(addr) & BIT(gpio));
93 } else {
94 return !!(vf610_gpio_readl(port->gpio_base + GPIO_PDIR)
95 & BIT(gpio));
96 }
Stefan Agner7f2691a2014-10-16 21:47:58 +020097}
98
99static void vf610_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
100{
Linus Walleij65389b42015-12-07 15:03:30 +0100101 struct vf610_gpio_port *port = gpiochip_get_data(gc);
Stefan Agner7f2691a2014-10-16 21:47:58 +0200102 unsigned long mask = BIT(gpio);
103
104 if (val)
105 vf610_gpio_writel(mask, port->gpio_base + GPIO_PSOR);
106 else
107 vf610_gpio_writel(mask, port->gpio_base + GPIO_PCOR);
108}
109
110static int vf610_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
111{
Dong Aisheng659d8a62017-08-01 22:04:17 +0800112 struct vf610_gpio_port *port = gpiochip_get_data(chip);
113 unsigned long mask = BIT(gpio);
114 u32 val;
115
116 if (port->sdata && port->sdata->have_paddr) {
117 val = vf610_gpio_readl(port->gpio_base + GPIO_PDDR);
118 val &= ~mask;
119 vf610_gpio_writel(val, port->gpio_base + GPIO_PDDR);
120 }
121
Stefan Agner7f2691a2014-10-16 21:47:58 +0200122 return pinctrl_gpio_direction_input(chip->base + gpio);
123}
124
125static int vf610_gpio_direction_output(struct gpio_chip *chip, unsigned gpio,
126 int value)
127{
Dong Aisheng659d8a62017-08-01 22:04:17 +0800128 struct vf610_gpio_port *port = gpiochip_get_data(chip);
129 unsigned long mask = BIT(gpio);
130
131 if (port->sdata && port->sdata->have_paddr)
132 vf610_gpio_writel(mask, port->gpio_base + GPIO_PDDR);
133
Stefan Agner7f2691a2014-10-16 21:47:58 +0200134 vf610_gpio_set(chip, gpio, value);
135
136 return pinctrl_gpio_direction_output(chip->base + gpio);
137}
138
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +0200139static void vf610_gpio_irq_handler(struct irq_desc *desc)
Stefan Agner7f2691a2014-10-16 21:47:58 +0200140{
Linus Walleij2f930642015-08-27 14:13:46 +0200141 struct vf610_gpio_port *port =
Linus Walleij65389b42015-12-07 15:03:30 +0100142 gpiochip_get_data(irq_desc_get_handler_data(desc));
Stefan Agner7f2691a2014-10-16 21:47:58 +0200143 struct irq_chip *chip = irq_desc_get_chip(desc);
144 int pin;
145 unsigned long irq_isfr;
146
147 chained_irq_enter(chip, desc);
148
149 irq_isfr = vf610_gpio_readl(port->base + PORT_ISFR);
150
151 for_each_set_bit(pin, &irq_isfr, VF610_GPIO_PER_PORT) {
152 vf610_gpio_writel(BIT(pin), port->base + PORT_ISFR);
153
Thierry Redingf0fbe7b2017-11-07 19:15:47 +0100154 generic_handle_irq(irq_find_mapping(port->gc.irq.domain, pin));
Stefan Agner7f2691a2014-10-16 21:47:58 +0200155 }
156
157 chained_irq_exit(chip, desc);
158}
159
160static void vf610_gpio_irq_ack(struct irq_data *d)
161{
Linus Walleij2f930642015-08-27 14:13:46 +0200162 struct vf610_gpio_port *port =
Linus Walleij65389b42015-12-07 15:03:30 +0100163 gpiochip_get_data(irq_data_get_irq_chip_data(d));
Stefan Agner7f2691a2014-10-16 21:47:58 +0200164 int gpio = d->hwirq;
165
166 vf610_gpio_writel(BIT(gpio), port->base + PORT_ISFR);
167}
168
169static int vf610_gpio_irq_set_type(struct irq_data *d, u32 type)
170{
Linus Walleij2f930642015-08-27 14:13:46 +0200171 struct vf610_gpio_port *port =
Linus Walleij65389b42015-12-07 15:03:30 +0100172 gpiochip_get_data(irq_data_get_irq_chip_data(d));
Stefan Agner7f2691a2014-10-16 21:47:58 +0200173 u8 irqc;
174
175 switch (type) {
176 case IRQ_TYPE_EDGE_RISING:
177 irqc = PORT_INT_RISING_EDGE;
178 break;
179 case IRQ_TYPE_EDGE_FALLING:
180 irqc = PORT_INT_FALLING_EDGE;
181 break;
182 case IRQ_TYPE_EDGE_BOTH:
183 irqc = PORT_INT_EITHER_EDGE;
184 break;
185 case IRQ_TYPE_LEVEL_LOW:
186 irqc = PORT_INT_LOGIC_ZERO;
187 break;
188 case IRQ_TYPE_LEVEL_HIGH:
189 irqc = PORT_INT_LOGIC_ONE;
190 break;
191 default:
192 return -EINVAL;
193 }
194
195 port->irqc[d->hwirq] = irqc;
196
Stefan Agnerfd968112015-08-21 15:56:42 -0700197 if (type & IRQ_TYPE_LEVEL_MASK)
Thomas Gleixnera7147db2015-09-16 12:51:00 +0200198 irq_set_handler_locked(d, handle_level_irq);
Stefan Agnerfd968112015-08-21 15:56:42 -0700199 else
Thomas Gleixnera7147db2015-09-16 12:51:00 +0200200 irq_set_handler_locked(d, handle_edge_irq);
Stefan Agnerfd968112015-08-21 15:56:42 -0700201
Stefan Agner7f2691a2014-10-16 21:47:58 +0200202 return 0;
203}
204
205static void vf610_gpio_irq_mask(struct irq_data *d)
206{
Linus Walleij2f930642015-08-27 14:13:46 +0200207 struct vf610_gpio_port *port =
Linus Walleij65389b42015-12-07 15:03:30 +0100208 gpiochip_get_data(irq_data_get_irq_chip_data(d));
Stefan Agner7f2691a2014-10-16 21:47:58 +0200209 void __iomem *pcr_base = port->base + PORT_PCR(d->hwirq);
210
211 vf610_gpio_writel(0, pcr_base);
212}
213
214static void vf610_gpio_irq_unmask(struct irq_data *d)
215{
Linus Walleij2f930642015-08-27 14:13:46 +0200216 struct vf610_gpio_port *port =
Linus Walleij65389b42015-12-07 15:03:30 +0100217 gpiochip_get_data(irq_data_get_irq_chip_data(d));
Stefan Agner7f2691a2014-10-16 21:47:58 +0200218 void __iomem *pcr_base = port->base + PORT_PCR(d->hwirq);
219
220 vf610_gpio_writel(port->irqc[d->hwirq] << PORT_PCR_IRQC_OFFSET,
221 pcr_base);
222}
223
224static int vf610_gpio_irq_set_wake(struct irq_data *d, u32 enable)
225{
Linus Walleij2f930642015-08-27 14:13:46 +0200226 struct vf610_gpio_port *port =
Linus Walleij65389b42015-12-07 15:03:30 +0100227 gpiochip_get_data(irq_data_get_irq_chip_data(d));
Stefan Agner7f2691a2014-10-16 21:47:58 +0200228
229 if (enable)
230 enable_irq_wake(port->irq);
231 else
232 disable_irq_wake(port->irq);
233
234 return 0;
235}
236
237static struct irq_chip vf610_gpio_irq_chip = {
238 .name = "gpio-vf610",
239 .irq_ack = vf610_gpio_irq_ack,
240 .irq_mask = vf610_gpio_irq_mask,
241 .irq_unmask = vf610_gpio_irq_unmask,
242 .irq_set_type = vf610_gpio_irq_set_type,
243 .irq_set_wake = vf610_gpio_irq_set_wake,
244};
245
246static int vf610_gpio_probe(struct platform_device *pdev)
247{
248 struct device *dev = &pdev->dev;
249 struct device_node *np = dev->of_node;
250 struct vf610_gpio_port *port;
251 struct resource *iores;
252 struct gpio_chip *gc;
253 int ret;
254
255 port = devm_kzalloc(&pdev->dev, sizeof(*port), GFP_KERNEL);
256 if (!port)
257 return -ENOMEM;
258
Thierry Reding23e577e2018-04-30 09:38:18 +0200259 port->sdata = of_device_get_match_data(dev);
Stefan Agner7f2691a2014-10-16 21:47:58 +0200260 iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
261 port->base = devm_ioremap_resource(dev, iores);
262 if (IS_ERR(port->base))
263 return PTR_ERR(port->base);
264
265 iores = platform_get_resource(pdev, IORESOURCE_MEM, 1);
266 port->gpio_base = devm_ioremap_resource(dev, iores);
267 if (IS_ERR(port->gpio_base))
268 return PTR_ERR(port->gpio_base);
269
270 port->irq = platform_get_irq(pdev, 0);
271 if (port->irq < 0)
272 return port->irq;
273
274 gc = &port->gc;
275 gc->of_node = np;
Linus Walleij58383c782015-11-04 09:56:26 +0100276 gc->parent = dev;
Axel Lind32efe32015-02-13 21:04:42 +0800277 gc->label = "vf610-gpio";
278 gc->ngpio = VF610_GPIO_PER_PORT;
Stefan Agner7f2691a2014-10-16 21:47:58 +0200279 gc->base = of_alias_get_id(np, "gpio") * VF610_GPIO_PER_PORT;
280
Jonas Gorski203f0da2015-10-11 17:34:16 +0200281 gc->request = gpiochip_generic_request;
282 gc->free = gpiochip_generic_free;
Axel Lind32efe32015-02-13 21:04:42 +0800283 gc->direction_input = vf610_gpio_direction_input;
284 gc->get = vf610_gpio_get;
285 gc->direction_output = vf610_gpio_direction_output;
286 gc->set = vf610_gpio_set;
Stefan Agner7f2691a2014-10-16 21:47:58 +0200287
Linus Walleij65389b42015-12-07 15:03:30 +0100288 ret = gpiochip_add_data(gc, port);
Stefan Agner7f2691a2014-10-16 21:47:58 +0200289 if (ret < 0)
290 return ret;
291
292 /* Clear the interrupt status register for all GPIO's */
293 vf610_gpio_writel(~0, port->base + PORT_ISFR);
294
295 ret = gpiochip_irqchip_add(gc, &vf610_gpio_irq_chip, 0,
Stefan Agnerfd968112015-08-21 15:56:42 -0700296 handle_edge_irq, IRQ_TYPE_NONE);
Stefan Agner7f2691a2014-10-16 21:47:58 +0200297 if (ret) {
298 dev_err(dev, "failed to add irqchip\n");
299 gpiochip_remove(gc);
300 return ret;
301 }
302 gpiochip_set_chained_irqchip(gc, &vf610_gpio_irq_chip, port->irq,
303 vf610_gpio_irq_handler);
304
305 return 0;
306}
307
308static struct platform_driver vf610_gpio_driver = {
309 .driver = {
310 .name = "gpio-vf610",
Stefan Agner7f2691a2014-10-16 21:47:58 +0200311 .of_match_table = vf610_gpio_dt_ids,
312 },
313 .probe = vf610_gpio_probe,
314};
315
Geliang Tangdf950da2016-11-23 22:47:48 +0800316builtin_platform_driver(vf610_gpio_driver);