blob: 541fa6ac399d24b288d7f148717874d2392e9023 [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>
A.s. Dong91393622018-11-10 14:21:18 +000010#include <linux/clk.h>
Stefan Agner7f2691a2014-10-16 21:47:58 +020011#include <linux/err.h>
Linus Walleij45e82962018-09-07 22:03:07 +020012#include <linux/gpio/driver.h>
Stefan Agner7f2691a2014-10-16 21:47:58 +020013#include <linux/init.h>
14#include <linux/interrupt.h>
15#include <linux/io.h>
16#include <linux/ioport.h>
17#include <linux/irq.h>
Stefan Agner7f2691a2014-10-16 21:47:58 +020018#include <linux/platform_device.h>
19#include <linux/of.h>
20#include <linux/of_device.h>
21#include <linux/of_irq.h>
22
23#define VF610_GPIO_PER_PORT 32
24
Dong Aisheng659d8a62017-08-01 22:04:17 +080025struct fsl_gpio_soc_data {
26 /* SoCs has a Port Data Direction Register (PDDR) */
27 bool have_paddr;
28};
29
Stefan Agner7f2691a2014-10-16 21:47:58 +020030struct vf610_gpio_port {
31 struct gpio_chip gc;
32 void __iomem *base;
33 void __iomem *gpio_base;
Dong Aisheng659d8a62017-08-01 22:04:17 +080034 const struct fsl_gpio_soc_data *sdata;
Stefan Agner7f2691a2014-10-16 21:47:58 +020035 u8 irqc[VF610_GPIO_PER_PORT];
A.s. Dong91393622018-11-10 14:21:18 +000036 struct clk *clk_port;
37 struct clk *clk_gpio;
Stefan Agner7f2691a2014-10-16 21:47:58 +020038 int irq;
39};
40
41#define GPIO_PDOR 0x00
42#define GPIO_PSOR 0x04
43#define GPIO_PCOR 0x08
44#define GPIO_PTOR 0x0c
45#define GPIO_PDIR 0x10
Dong Aisheng659d8a62017-08-01 22:04:17 +080046#define GPIO_PDDR 0x14
Stefan Agner7f2691a2014-10-16 21:47:58 +020047
48#define PORT_PCR(n) ((n) * 0x4)
49#define PORT_PCR_IRQC_OFFSET 16
50
51#define PORT_ISFR 0xa0
52#define PORT_DFER 0xc0
53#define PORT_DFCR 0xc4
54#define PORT_DFWR 0xc8
55
56#define PORT_INT_OFF 0x0
57#define PORT_INT_LOGIC_ZERO 0x8
58#define PORT_INT_RISING_EDGE 0x9
59#define PORT_INT_FALLING_EDGE 0xa
60#define PORT_INT_EITHER_EDGE 0xb
61#define PORT_INT_LOGIC_ONE 0xc
62
Stefan Agnerfd968112015-08-21 15:56:42 -070063static struct irq_chip vf610_gpio_irq_chip;
64
Dong Aisheng659d8a62017-08-01 22:04:17 +080065static const struct fsl_gpio_soc_data imx_data = {
66 .have_paddr = true,
67};
68
Stefan Agner7f2691a2014-10-16 21:47:58 +020069static const struct of_device_id vf610_gpio_dt_ids[] = {
Dong Aisheng659d8a62017-08-01 22:04:17 +080070 { .compatible = "fsl,vf610-gpio", .data = NULL, },
71 { .compatible = "fsl,imx7ulp-gpio", .data = &imx_data, },
Stefan Agner7f2691a2014-10-16 21:47:58 +020072 { /* sentinel */ }
73};
74
75static inline void vf610_gpio_writel(u32 val, void __iomem *reg)
76{
77 writel_relaxed(val, reg);
78}
79
80static inline u32 vf610_gpio_readl(void __iomem *reg)
81{
82 return readl_relaxed(reg);
83}
84
Stefan Agner7f2691a2014-10-16 21:47:58 +020085static int vf610_gpio_get(struct gpio_chip *gc, unsigned int gpio)
86{
Linus Walleij65389b42015-12-07 15:03:30 +010087 struct vf610_gpio_port *port = gpiochip_get_data(gc);
Dong Aisheng659d8a62017-08-01 22:04:17 +080088 unsigned long mask = BIT(gpio);
89 void __iomem *addr;
Stefan Agner7f2691a2014-10-16 21:47:58 +020090
Dong Aisheng659d8a62017-08-01 22:04:17 +080091 if (port->sdata && port->sdata->have_paddr) {
92 mask &= vf610_gpio_readl(port->gpio_base + GPIO_PDDR);
93 addr = mask ? port->gpio_base + GPIO_PDOR :
94 port->gpio_base + GPIO_PDIR;
95 return !!(vf610_gpio_readl(addr) & BIT(gpio));
96 } else {
97 return !!(vf610_gpio_readl(port->gpio_base + GPIO_PDIR)
98 & BIT(gpio));
99 }
Stefan Agner7f2691a2014-10-16 21:47:58 +0200100}
101
102static void vf610_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
103{
Linus Walleij65389b42015-12-07 15:03:30 +0100104 struct vf610_gpio_port *port = gpiochip_get_data(gc);
Stefan Agner7f2691a2014-10-16 21:47:58 +0200105 unsigned long mask = BIT(gpio);
106
107 if (val)
108 vf610_gpio_writel(mask, port->gpio_base + GPIO_PSOR);
109 else
110 vf610_gpio_writel(mask, port->gpio_base + GPIO_PCOR);
111}
112
113static int vf610_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
114{
Dong Aisheng659d8a62017-08-01 22:04:17 +0800115 struct vf610_gpio_port *port = gpiochip_get_data(chip);
116 unsigned long mask = BIT(gpio);
117 u32 val;
118
119 if (port->sdata && port->sdata->have_paddr) {
120 val = vf610_gpio_readl(port->gpio_base + GPIO_PDDR);
121 val &= ~mask;
122 vf610_gpio_writel(val, port->gpio_base + GPIO_PDDR);
123 }
124
Stefan Agner7f2691a2014-10-16 21:47:58 +0200125 return pinctrl_gpio_direction_input(chip->base + gpio);
126}
127
128static int vf610_gpio_direction_output(struct gpio_chip *chip, unsigned gpio,
129 int value)
130{
Dong Aisheng659d8a62017-08-01 22:04:17 +0800131 struct vf610_gpio_port *port = gpiochip_get_data(chip);
132 unsigned long mask = BIT(gpio);
133
134 if (port->sdata && port->sdata->have_paddr)
135 vf610_gpio_writel(mask, port->gpio_base + GPIO_PDDR);
136
Stefan Agner7f2691a2014-10-16 21:47:58 +0200137 vf610_gpio_set(chip, gpio, value);
138
139 return pinctrl_gpio_direction_output(chip->base + gpio);
140}
141
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +0200142static void vf610_gpio_irq_handler(struct irq_desc *desc)
Stefan Agner7f2691a2014-10-16 21:47:58 +0200143{
Linus Walleij2f930642015-08-27 14:13:46 +0200144 struct vf610_gpio_port *port =
Linus Walleij65389b42015-12-07 15:03:30 +0100145 gpiochip_get_data(irq_desc_get_handler_data(desc));
Stefan Agner7f2691a2014-10-16 21:47:58 +0200146 struct irq_chip *chip = irq_desc_get_chip(desc);
147 int pin;
148 unsigned long irq_isfr;
149
150 chained_irq_enter(chip, desc);
151
152 irq_isfr = vf610_gpio_readl(port->base + PORT_ISFR);
153
154 for_each_set_bit(pin, &irq_isfr, VF610_GPIO_PER_PORT) {
155 vf610_gpio_writel(BIT(pin), port->base + PORT_ISFR);
156
Thierry Redingf0fbe7b2017-11-07 19:15:47 +0100157 generic_handle_irq(irq_find_mapping(port->gc.irq.domain, pin));
Stefan Agner7f2691a2014-10-16 21:47:58 +0200158 }
159
160 chained_irq_exit(chip, desc);
161}
162
163static void vf610_gpio_irq_ack(struct irq_data *d)
164{
Linus Walleij2f930642015-08-27 14:13:46 +0200165 struct vf610_gpio_port *port =
Linus Walleij65389b42015-12-07 15:03:30 +0100166 gpiochip_get_data(irq_data_get_irq_chip_data(d));
Stefan Agner7f2691a2014-10-16 21:47:58 +0200167 int gpio = d->hwirq;
168
169 vf610_gpio_writel(BIT(gpio), port->base + PORT_ISFR);
170}
171
172static int vf610_gpio_irq_set_type(struct irq_data *d, u32 type)
173{
Linus Walleij2f930642015-08-27 14:13:46 +0200174 struct vf610_gpio_port *port =
Linus Walleij65389b42015-12-07 15:03:30 +0100175 gpiochip_get_data(irq_data_get_irq_chip_data(d));
Stefan Agner7f2691a2014-10-16 21:47:58 +0200176 u8 irqc;
177
178 switch (type) {
179 case IRQ_TYPE_EDGE_RISING:
180 irqc = PORT_INT_RISING_EDGE;
181 break;
182 case IRQ_TYPE_EDGE_FALLING:
183 irqc = PORT_INT_FALLING_EDGE;
184 break;
185 case IRQ_TYPE_EDGE_BOTH:
186 irqc = PORT_INT_EITHER_EDGE;
187 break;
188 case IRQ_TYPE_LEVEL_LOW:
189 irqc = PORT_INT_LOGIC_ZERO;
190 break;
191 case IRQ_TYPE_LEVEL_HIGH:
192 irqc = PORT_INT_LOGIC_ONE;
193 break;
194 default:
195 return -EINVAL;
196 }
197
198 port->irqc[d->hwirq] = irqc;
199
Stefan Agnerfd968112015-08-21 15:56:42 -0700200 if (type & IRQ_TYPE_LEVEL_MASK)
Thomas Gleixnera7147db2015-09-16 12:51:00 +0200201 irq_set_handler_locked(d, handle_level_irq);
Stefan Agnerfd968112015-08-21 15:56:42 -0700202 else
Thomas Gleixnera7147db2015-09-16 12:51:00 +0200203 irq_set_handler_locked(d, handle_edge_irq);
Stefan Agnerfd968112015-08-21 15:56:42 -0700204
Stefan Agner7f2691a2014-10-16 21:47:58 +0200205 return 0;
206}
207
208static void vf610_gpio_irq_mask(struct irq_data *d)
209{
Linus Walleij2f930642015-08-27 14:13:46 +0200210 struct vf610_gpio_port *port =
Linus Walleij65389b42015-12-07 15:03:30 +0100211 gpiochip_get_data(irq_data_get_irq_chip_data(d));
Stefan Agner7f2691a2014-10-16 21:47:58 +0200212 void __iomem *pcr_base = port->base + PORT_PCR(d->hwirq);
213
214 vf610_gpio_writel(0, pcr_base);
215}
216
217static void vf610_gpio_irq_unmask(struct irq_data *d)
218{
Linus Walleij2f930642015-08-27 14:13:46 +0200219 struct vf610_gpio_port *port =
Linus Walleij65389b42015-12-07 15:03:30 +0100220 gpiochip_get_data(irq_data_get_irq_chip_data(d));
Stefan Agner7f2691a2014-10-16 21:47:58 +0200221 void __iomem *pcr_base = port->base + PORT_PCR(d->hwirq);
222
223 vf610_gpio_writel(port->irqc[d->hwirq] << PORT_PCR_IRQC_OFFSET,
224 pcr_base);
225}
226
227static int vf610_gpio_irq_set_wake(struct irq_data *d, u32 enable)
228{
Linus Walleij2f930642015-08-27 14:13:46 +0200229 struct vf610_gpio_port *port =
Linus Walleij65389b42015-12-07 15:03:30 +0100230 gpiochip_get_data(irq_data_get_irq_chip_data(d));
Stefan Agner7f2691a2014-10-16 21:47:58 +0200231
232 if (enable)
233 enable_irq_wake(port->irq);
234 else
235 disable_irq_wake(port->irq);
236
237 return 0;
238}
239
240static struct irq_chip vf610_gpio_irq_chip = {
241 .name = "gpio-vf610",
242 .irq_ack = vf610_gpio_irq_ack,
243 .irq_mask = vf610_gpio_irq_mask,
244 .irq_unmask = vf610_gpio_irq_unmask,
245 .irq_set_type = vf610_gpio_irq_set_type,
246 .irq_set_wake = vf610_gpio_irq_set_wake,
247};
248
249static int vf610_gpio_probe(struct platform_device *pdev)
250{
251 struct device *dev = &pdev->dev;
252 struct device_node *np = dev->of_node;
253 struct vf610_gpio_port *port;
254 struct resource *iores;
255 struct gpio_chip *gc;
Andrew Lunn7ae710f2019-01-27 22:58:00 +0100256 int i;
Stefan Agner7f2691a2014-10-16 21:47:58 +0200257 int ret;
258
259 port = devm_kzalloc(&pdev->dev, sizeof(*port), GFP_KERNEL);
260 if (!port)
261 return -ENOMEM;
262
Thierry Reding23e577e2018-04-30 09:38:18 +0200263 port->sdata = of_device_get_match_data(dev);
Stefan Agner7f2691a2014-10-16 21:47:58 +0200264 iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
265 port->base = devm_ioremap_resource(dev, iores);
266 if (IS_ERR(port->base))
267 return PTR_ERR(port->base);
268
269 iores = platform_get_resource(pdev, IORESOURCE_MEM, 1);
270 port->gpio_base = devm_ioremap_resource(dev, iores);
271 if (IS_ERR(port->gpio_base))
272 return PTR_ERR(port->gpio_base);
273
274 port->irq = platform_get_irq(pdev, 0);
275 if (port->irq < 0)
276 return port->irq;
277
A.s. Dong91393622018-11-10 14:21:18 +0000278 port->clk_port = devm_clk_get(&pdev->dev, "port");
279 if (!IS_ERR(port->clk_port)) {
280 ret = clk_prepare_enable(port->clk_port);
281 if (ret)
282 return ret;
283 } else if (port->clk_port == ERR_PTR(-EPROBE_DEFER)) {
284 /*
285 * Percolate deferrals, for anything else,
286 * just live without the clocking.
287 */
288 return PTR_ERR(port->clk_port);
289 }
290
291 port->clk_gpio = devm_clk_get(&pdev->dev, "gpio");
292 if (!IS_ERR(port->clk_gpio)) {
293 ret = clk_prepare_enable(port->clk_gpio);
294 if (ret) {
295 clk_disable_unprepare(port->clk_port);
296 return ret;
297 }
298 } else if (port->clk_gpio == ERR_PTR(-EPROBE_DEFER)) {
299 clk_disable_unprepare(port->clk_port);
300 return PTR_ERR(port->clk_gpio);
301 }
302
303 platform_set_drvdata(pdev, port);
304
Stefan Agner7f2691a2014-10-16 21:47:58 +0200305 gc = &port->gc;
306 gc->of_node = np;
Linus Walleij58383c782015-11-04 09:56:26 +0100307 gc->parent = dev;
Axel Lind32efe32015-02-13 21:04:42 +0800308 gc->label = "vf610-gpio";
309 gc->ngpio = VF610_GPIO_PER_PORT;
Stefan Agner7f2691a2014-10-16 21:47:58 +0200310 gc->base = of_alias_get_id(np, "gpio") * VF610_GPIO_PER_PORT;
311
Jonas Gorski203f0da2015-10-11 17:34:16 +0200312 gc->request = gpiochip_generic_request;
313 gc->free = gpiochip_generic_free;
Axel Lind32efe32015-02-13 21:04:42 +0800314 gc->direction_input = vf610_gpio_direction_input;
315 gc->get = vf610_gpio_get;
316 gc->direction_output = vf610_gpio_direction_output;
317 gc->set = vf610_gpio_set;
Stefan Agner7f2691a2014-10-16 21:47:58 +0200318
Linus Walleij65389b42015-12-07 15:03:30 +0100319 ret = gpiochip_add_data(gc, port);
Stefan Agner7f2691a2014-10-16 21:47:58 +0200320 if (ret < 0)
321 return ret;
322
Andrew Lunn7ae710f2019-01-27 22:58:00 +0100323 /* Mask all GPIO interrupts */
324 for (i = 0; i < gc->ngpio; i++)
325 vf610_gpio_writel(0, port->base + PORT_PCR(i));
326
Stefan Agner7f2691a2014-10-16 21:47:58 +0200327 /* Clear the interrupt status register for all GPIO's */
328 vf610_gpio_writel(~0, port->base + PORT_ISFR);
329
330 ret = gpiochip_irqchip_add(gc, &vf610_gpio_irq_chip, 0,
Stefan Agnerfd968112015-08-21 15:56:42 -0700331 handle_edge_irq, IRQ_TYPE_NONE);
Stefan Agner7f2691a2014-10-16 21:47:58 +0200332 if (ret) {
333 dev_err(dev, "failed to add irqchip\n");
334 gpiochip_remove(gc);
335 return ret;
336 }
337 gpiochip_set_chained_irqchip(gc, &vf610_gpio_irq_chip, port->irq,
338 vf610_gpio_irq_handler);
339
340 return 0;
341}
342
A.s. Dong91393622018-11-10 14:21:18 +0000343static int vf610_gpio_remove(struct platform_device *pdev)
344{
345 struct vf610_gpio_port *port = platform_get_drvdata(pdev);
346
347 gpiochip_remove(&port->gc);
348 if (!IS_ERR(port->clk_port))
349 clk_disable_unprepare(port->clk_port);
350 if (!IS_ERR(port->clk_gpio))
351 clk_disable_unprepare(port->clk_gpio);
352
353 return 0;
354}
355
Stefan Agner7f2691a2014-10-16 21:47:58 +0200356static struct platform_driver vf610_gpio_driver = {
357 .driver = {
358 .name = "gpio-vf610",
Stefan Agner7f2691a2014-10-16 21:47:58 +0200359 .of_match_table = vf610_gpio_dt_ids,
360 },
361 .probe = vf610_gpio_probe,
A.s. Dong91393622018-11-10 14:21:18 +0000362 .remove = vf610_gpio_remove,
Stefan Agner7f2691a2014-10-16 21:47:58 +0200363};
364
Geliang Tangdf950da2016-11-23 22:47:48 +0800365builtin_platform_driver(vf610_gpio_driver);