xref: /openbmc/linux/drivers/gpio/gpio-vf610.c (revision 0c874100)
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/err.h>
11 #include <linux/gpio/driver.h>
12 #include <linux/init.h>
13 #include <linux/interrupt.h>
14 #include <linux/io.h>
15 #include <linux/ioport.h>
16 #include <linux/irq.h>
17 #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 
24 struct fsl_gpio_soc_data {
25 	/* SoCs has a Port Data Direction Register (PDDR) */
26 	bool have_paddr;
27 };
28 
29 struct vf610_gpio_port {
30 	struct gpio_chip gc;
31 	void __iomem *base;
32 	void __iomem *gpio_base;
33 	const struct fsl_gpio_soc_data *sdata;
34 	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
43 #define GPIO_PDDR		0x14
44 
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 
60 static struct irq_chip vf610_gpio_irq_chip;
61 
62 static const struct fsl_gpio_soc_data imx_data = {
63 	.have_paddr = true,
64 };
65 
66 static const struct of_device_id vf610_gpio_dt_ids[] = {
67 	{ .compatible = "fsl,vf610-gpio",	.data = NULL, },
68 	{ .compatible = "fsl,imx7ulp-gpio",	.data = &imx_data, },
69 	{ /* sentinel */ }
70 };
71 
72 static inline void vf610_gpio_writel(u32 val, void __iomem *reg)
73 {
74 	writel_relaxed(val, reg);
75 }
76 
77 static inline u32 vf610_gpio_readl(void __iomem *reg)
78 {
79 	return readl_relaxed(reg);
80 }
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 	unsigned long mask = BIT(gpio);
86 	void __iomem *addr;
87 
88 	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 	}
97 }
98 
99 static 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 
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 
110 static int vf610_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
111 {
112 	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 
122 	return pinctrl_gpio_direction_input(chip->base + gpio);
123 }
124 
125 static int vf610_gpio_direction_output(struct gpio_chip *chip, unsigned gpio,
126 				       int value)
127 {
128 	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 
134 	vf610_gpio_set(chip, gpio, value);
135 
136 	return pinctrl_gpio_direction_output(chip->base + gpio);
137 }
138 
139 static void vf610_gpio_irq_handler(struct irq_desc *desc)
140 {
141 	struct vf610_gpio_port *port =
142 		gpiochip_get_data(irq_desc_get_handler_data(desc));
143 	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 
154 		generic_handle_irq(irq_find_mapping(port->gc.irq.domain, pin));
155 	}
156 
157 	chained_irq_exit(chip, desc);
158 }
159 
160 static void vf610_gpio_irq_ack(struct irq_data *d)
161 {
162 	struct vf610_gpio_port *port =
163 		gpiochip_get_data(irq_data_get_irq_chip_data(d));
164 	int gpio = d->hwirq;
165 
166 	vf610_gpio_writel(BIT(gpio), port->base + PORT_ISFR);
167 }
168 
169 static int vf610_gpio_irq_set_type(struct irq_data *d, u32 type)
170 {
171 	struct vf610_gpio_port *port =
172 		gpiochip_get_data(irq_data_get_irq_chip_data(d));
173 	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 
197 	if (type & IRQ_TYPE_LEVEL_MASK)
198 		irq_set_handler_locked(d, handle_level_irq);
199 	else
200 		irq_set_handler_locked(d, handle_edge_irq);
201 
202 	return 0;
203 }
204 
205 static void vf610_gpio_irq_mask(struct irq_data *d)
206 {
207 	struct vf610_gpio_port *port =
208 		gpiochip_get_data(irq_data_get_irq_chip_data(d));
209 	void __iomem *pcr_base = port->base + PORT_PCR(d->hwirq);
210 
211 	vf610_gpio_writel(0, pcr_base);
212 }
213 
214 static void vf610_gpio_irq_unmask(struct irq_data *d)
215 {
216 	struct vf610_gpio_port *port =
217 		gpiochip_get_data(irq_data_get_irq_chip_data(d));
218 	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 
224 static int vf610_gpio_irq_set_wake(struct irq_data *d, u32 enable)
225 {
226 	struct vf610_gpio_port *port =
227 		gpiochip_get_data(irq_data_get_irq_chip_data(d));
228 
229 	if (enable)
230 		enable_irq_wake(port->irq);
231 	else
232 		disable_irq_wake(port->irq);
233 
234 	return 0;
235 }
236 
237 static 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 
246 static 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 
259 	port->sdata = of_device_get_match_data(dev);
260 	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;
276 	gc->parent = dev;
277 	gc->label = "vf610-gpio";
278 	gc->ngpio = VF610_GPIO_PER_PORT;
279 	gc->base = of_alias_get_id(np, "gpio") * VF610_GPIO_PER_PORT;
280 
281 	gc->request = gpiochip_generic_request;
282 	gc->free = gpiochip_generic_free;
283 	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;
287 
288 	ret = gpiochip_add_data(gc, port);
289 	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,
296 				   handle_edge_irq, IRQ_TYPE_NONE);
297 	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 
308 static struct platform_driver vf610_gpio_driver = {
309 	.driver		= {
310 		.name	= "gpio-vf610",
311 		.of_match_table = vf610_gpio_dt_ids,
312 	},
313 	.probe		= vf610_gpio_probe,
314 };
315 
316 builtin_platform_driver(vf610_gpio_driver);
317