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