xref: /openbmc/linux/drivers/gpio/gpio-rcar.c (revision 2d65472b)
1119f5e44SMagnus Damm /*
2119f5e44SMagnus Damm  * Renesas R-Car GPIO Support
3119f5e44SMagnus Damm  *
41fd2b49dSHisashi Nakamura  *  Copyright (C) 2014 Renesas Electronics Corporation
5119f5e44SMagnus Damm  *  Copyright (C) 2013 Magnus Damm
6119f5e44SMagnus Damm  *
7119f5e44SMagnus Damm  * This program is free software; you can redistribute it and/or modify
8119f5e44SMagnus Damm  * it under the terms of the GNU General Public License as published by
9119f5e44SMagnus Damm  * the Free Software Foundation; either version 2 of the License
10119f5e44SMagnus Damm  *
11119f5e44SMagnus Damm  * This program is distributed in the hope that it will be useful,
12119f5e44SMagnus Damm  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13119f5e44SMagnus Damm  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14119f5e44SMagnus Damm  * GNU General Public License for more details.
15119f5e44SMagnus Damm  */
16119f5e44SMagnus Damm 
17ab82fa7dSGeert Uytterhoeven #include <linux/clk.h>
18119f5e44SMagnus Damm #include <linux/err.h>
19119f5e44SMagnus Damm #include <linux/gpio.h>
20119f5e44SMagnus Damm #include <linux/init.h>
21119f5e44SMagnus Damm #include <linux/interrupt.h>
22119f5e44SMagnus Damm #include <linux/io.h>
23119f5e44SMagnus Damm #include <linux/ioport.h>
24119f5e44SMagnus Damm #include <linux/irq.h>
25119f5e44SMagnus Damm #include <linux/module.h>
26bd0bf468SSachin Kamat #include <linux/of.h>
27dc3465a9SLaurent Pinchart #include <linux/pinctrl/consumer.h>
28119f5e44SMagnus Damm #include <linux/platform_device.h>
29df0c6c80SGeert Uytterhoeven #include <linux/pm_runtime.h>
30119f5e44SMagnus Damm #include <linux/spinlock.h>
31119f5e44SMagnus Damm #include <linux/slab.h>
32119f5e44SMagnus Damm 
33119f5e44SMagnus Damm struct gpio_rcar_priv {
34119f5e44SMagnus Damm 	void __iomem *base;
35119f5e44SMagnus Damm 	spinlock_t lock;
36119f5e44SMagnus Damm 	struct platform_device *pdev;
37119f5e44SMagnus Damm 	struct gpio_chip gpio_chip;
38119f5e44SMagnus Damm 	struct irq_chip irq_chip;
39ab82fa7dSGeert Uytterhoeven 	struct clk *clk;
408b092be9SGeert Uytterhoeven 	unsigned int irq_parent;
418b092be9SGeert Uytterhoeven 	bool has_both_edge_trigger;
42e1fef9e2SGeert Uytterhoeven 	bool needs_clk;
43119f5e44SMagnus Damm };
44119f5e44SMagnus Damm 
453dc1e685SGeert Uytterhoeven #define IOINTSEL 0x00	/* General IO/Interrupt Switching Register */
463dc1e685SGeert Uytterhoeven #define INOUTSEL 0x04	/* General Input/Output Switching Register */
473dc1e685SGeert Uytterhoeven #define OUTDT 0x08	/* General Output Register */
483dc1e685SGeert Uytterhoeven #define INDT 0x0c	/* General Input Register */
493dc1e685SGeert Uytterhoeven #define INTDT 0x10	/* Interrupt Display Register */
503dc1e685SGeert Uytterhoeven #define INTCLR 0x14	/* Interrupt Clear Register */
513dc1e685SGeert Uytterhoeven #define INTMSK 0x18	/* Interrupt Mask Register */
523dc1e685SGeert Uytterhoeven #define MSKCLR 0x1c	/* Interrupt Mask Clear Register */
533dc1e685SGeert Uytterhoeven #define POSNEG 0x20	/* Positive/Negative Logic Select Register */
543dc1e685SGeert Uytterhoeven #define EDGLEVEL 0x24	/* Edge/level Select Register */
553dc1e685SGeert Uytterhoeven #define FILONOFF 0x28	/* Chattering Prevention On/Off Register */
563dc1e685SGeert Uytterhoeven #define BOTHEDGE 0x4c	/* One Edge/Both Edge Select Register */
57119f5e44SMagnus Damm 
58159f8a02SLaurent Pinchart #define RCAR_MAX_GPIO_PER_BANK		32
59159f8a02SLaurent Pinchart 
60119f5e44SMagnus Damm static inline u32 gpio_rcar_read(struct gpio_rcar_priv *p, int offs)
61119f5e44SMagnus Damm {
62119f5e44SMagnus Damm 	return ioread32(p->base + offs);
63119f5e44SMagnus Damm }
64119f5e44SMagnus Damm 
65119f5e44SMagnus Damm static inline void gpio_rcar_write(struct gpio_rcar_priv *p, int offs,
66119f5e44SMagnus Damm 				   u32 value)
67119f5e44SMagnus Damm {
68119f5e44SMagnus Damm 	iowrite32(value, p->base + offs);
69119f5e44SMagnus Damm }
70119f5e44SMagnus Damm 
71119f5e44SMagnus Damm static void gpio_rcar_modify_bit(struct gpio_rcar_priv *p, int offs,
72119f5e44SMagnus Damm 				 int bit, bool value)
73119f5e44SMagnus Damm {
74119f5e44SMagnus Damm 	u32 tmp = gpio_rcar_read(p, offs);
75119f5e44SMagnus Damm 
76119f5e44SMagnus Damm 	if (value)
77119f5e44SMagnus Damm 		tmp |= BIT(bit);
78119f5e44SMagnus Damm 	else
79119f5e44SMagnus Damm 		tmp &= ~BIT(bit);
80119f5e44SMagnus Damm 
81119f5e44SMagnus Damm 	gpio_rcar_write(p, offs, tmp);
82119f5e44SMagnus Damm }
83119f5e44SMagnus Damm 
84119f5e44SMagnus Damm static void gpio_rcar_irq_disable(struct irq_data *d)
85119f5e44SMagnus Damm {
86c7f3c5d3SGeert Uytterhoeven 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
87c7b6f457SLinus Walleij 	struct gpio_rcar_priv *p = gpiochip_get_data(gc);
88119f5e44SMagnus Damm 
89119f5e44SMagnus Damm 	gpio_rcar_write(p, INTMSK, ~BIT(irqd_to_hwirq(d)));
90119f5e44SMagnus Damm }
91119f5e44SMagnus Damm 
92119f5e44SMagnus Damm static void gpio_rcar_irq_enable(struct irq_data *d)
93119f5e44SMagnus Damm {
94c7f3c5d3SGeert Uytterhoeven 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
95c7b6f457SLinus Walleij 	struct gpio_rcar_priv *p = gpiochip_get_data(gc);
96119f5e44SMagnus Damm 
97119f5e44SMagnus Damm 	gpio_rcar_write(p, MSKCLR, BIT(irqd_to_hwirq(d)));
98119f5e44SMagnus Damm }
99119f5e44SMagnus Damm 
100119f5e44SMagnus Damm static void gpio_rcar_config_interrupt_input_mode(struct gpio_rcar_priv *p,
101119f5e44SMagnus Damm 						  unsigned int hwirq,
102119f5e44SMagnus Damm 						  bool active_high_rising_edge,
1037e1092b5SSimon Horman 						  bool level_trigger,
1047e1092b5SSimon Horman 						  bool both)
105119f5e44SMagnus Damm {
106119f5e44SMagnus Damm 	unsigned long flags;
107119f5e44SMagnus Damm 
108119f5e44SMagnus Damm 	/* follow steps in the GPIO documentation for
109119f5e44SMagnus Damm 	 * "Setting Edge-Sensitive Interrupt Input Mode" and
110119f5e44SMagnus Damm 	 * "Setting Level-Sensitive Interrupt Input Mode"
111119f5e44SMagnus Damm 	 */
112119f5e44SMagnus Damm 
113119f5e44SMagnus Damm 	spin_lock_irqsave(&p->lock, flags);
114119f5e44SMagnus Damm 
115119f5e44SMagnus Damm 	/* Configure postive or negative logic in POSNEG */
116119f5e44SMagnus Damm 	gpio_rcar_modify_bit(p, POSNEG, hwirq, !active_high_rising_edge);
117119f5e44SMagnus Damm 
118119f5e44SMagnus Damm 	/* Configure edge or level trigger in EDGLEVEL */
119119f5e44SMagnus Damm 	gpio_rcar_modify_bit(p, EDGLEVEL, hwirq, !level_trigger);
120119f5e44SMagnus Damm 
1217e1092b5SSimon Horman 	/* Select one edge or both edges in BOTHEDGE */
1228b092be9SGeert Uytterhoeven 	if (p->has_both_edge_trigger)
1237e1092b5SSimon Horman 		gpio_rcar_modify_bit(p, BOTHEDGE, hwirq, both);
1247e1092b5SSimon Horman 
125119f5e44SMagnus Damm 	/* Select "Interrupt Input Mode" in IOINTSEL */
126119f5e44SMagnus Damm 	gpio_rcar_modify_bit(p, IOINTSEL, hwirq, true);
127119f5e44SMagnus Damm 
128119f5e44SMagnus Damm 	/* Write INTCLR in case of edge trigger */
129119f5e44SMagnus Damm 	if (!level_trigger)
130119f5e44SMagnus Damm 		gpio_rcar_write(p, INTCLR, BIT(hwirq));
131119f5e44SMagnus Damm 
132119f5e44SMagnus Damm 	spin_unlock_irqrestore(&p->lock, flags);
133119f5e44SMagnus Damm }
134119f5e44SMagnus Damm 
135119f5e44SMagnus Damm static int gpio_rcar_irq_set_type(struct irq_data *d, unsigned int type)
136119f5e44SMagnus Damm {
137c7f3c5d3SGeert Uytterhoeven 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
138c7b6f457SLinus Walleij 	struct gpio_rcar_priv *p = gpiochip_get_data(gc);
139119f5e44SMagnus Damm 	unsigned int hwirq = irqd_to_hwirq(d);
140119f5e44SMagnus Damm 
141119f5e44SMagnus Damm 	dev_dbg(&p->pdev->dev, "sense irq = %d, type = %d\n", hwirq, type);
142119f5e44SMagnus Damm 
143119f5e44SMagnus Damm 	switch (type & IRQ_TYPE_SENSE_MASK) {
144119f5e44SMagnus Damm 	case IRQ_TYPE_LEVEL_HIGH:
1457e1092b5SSimon Horman 		gpio_rcar_config_interrupt_input_mode(p, hwirq, true, true,
1467e1092b5SSimon Horman 						      false);
147119f5e44SMagnus Damm 		break;
148119f5e44SMagnus Damm 	case IRQ_TYPE_LEVEL_LOW:
1497e1092b5SSimon Horman 		gpio_rcar_config_interrupt_input_mode(p, hwirq, false, true,
1507e1092b5SSimon Horman 						      false);
151119f5e44SMagnus Damm 		break;
152119f5e44SMagnus Damm 	case IRQ_TYPE_EDGE_RISING:
1537e1092b5SSimon Horman 		gpio_rcar_config_interrupt_input_mode(p, hwirq, true, false,
1547e1092b5SSimon Horman 						      false);
155119f5e44SMagnus Damm 		break;
156119f5e44SMagnus Damm 	case IRQ_TYPE_EDGE_FALLING:
1577e1092b5SSimon Horman 		gpio_rcar_config_interrupt_input_mode(p, hwirq, false, false,
1587e1092b5SSimon Horman 						      false);
1597e1092b5SSimon Horman 		break;
1607e1092b5SSimon Horman 	case IRQ_TYPE_EDGE_BOTH:
1618b092be9SGeert Uytterhoeven 		if (!p->has_both_edge_trigger)
1627e1092b5SSimon Horman 			return -EINVAL;
1637e1092b5SSimon Horman 		gpio_rcar_config_interrupt_input_mode(p, hwirq, true, false,
1647e1092b5SSimon Horman 						      true);
165119f5e44SMagnus Damm 		break;
166119f5e44SMagnus Damm 	default:
167119f5e44SMagnus Damm 		return -EINVAL;
168119f5e44SMagnus Damm 	}
169119f5e44SMagnus Damm 	return 0;
170119f5e44SMagnus Damm }
171119f5e44SMagnus Damm 
172ab82fa7dSGeert Uytterhoeven static int gpio_rcar_irq_set_wake(struct irq_data *d, unsigned int on)
173ab82fa7dSGeert Uytterhoeven {
174ab82fa7dSGeert Uytterhoeven 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
175c7b6f457SLinus Walleij 	struct gpio_rcar_priv *p = gpiochip_get_data(gc);
176501ef0f9SGeert Uytterhoeven 	int error;
177ab82fa7dSGeert Uytterhoeven 
178501ef0f9SGeert Uytterhoeven 	if (p->irq_parent) {
179501ef0f9SGeert Uytterhoeven 		error = irq_set_irq_wake(p->irq_parent, on);
180501ef0f9SGeert Uytterhoeven 		if (error) {
181501ef0f9SGeert Uytterhoeven 			dev_dbg(&p->pdev->dev,
182501ef0f9SGeert Uytterhoeven 				"irq %u doesn't support irq_set_wake\n",
183501ef0f9SGeert Uytterhoeven 				p->irq_parent);
184501ef0f9SGeert Uytterhoeven 			p->irq_parent = 0;
185501ef0f9SGeert Uytterhoeven 		}
186501ef0f9SGeert Uytterhoeven 	}
187ab82fa7dSGeert Uytterhoeven 
188ab82fa7dSGeert Uytterhoeven 	if (!p->clk)
189ab82fa7dSGeert Uytterhoeven 		return 0;
190ab82fa7dSGeert Uytterhoeven 
191ab82fa7dSGeert Uytterhoeven 	if (on)
192ab82fa7dSGeert Uytterhoeven 		clk_enable(p->clk);
193ab82fa7dSGeert Uytterhoeven 	else
194ab82fa7dSGeert Uytterhoeven 		clk_disable(p->clk);
195ab82fa7dSGeert Uytterhoeven 
196ab82fa7dSGeert Uytterhoeven 	return 0;
197ab82fa7dSGeert Uytterhoeven }
198ab82fa7dSGeert Uytterhoeven 
199119f5e44SMagnus Damm static irqreturn_t gpio_rcar_irq_handler(int irq, void *dev_id)
200119f5e44SMagnus Damm {
201119f5e44SMagnus Damm 	struct gpio_rcar_priv *p = dev_id;
202119f5e44SMagnus Damm 	u32 pending;
203119f5e44SMagnus Damm 	unsigned int offset, irqs_handled = 0;
204119f5e44SMagnus Damm 
2058808b64dSValentine Barshak 	while ((pending = gpio_rcar_read(p, INTDT) &
2068808b64dSValentine Barshak 			  gpio_rcar_read(p, INTMSK))) {
207119f5e44SMagnus Damm 		offset = __ffs(pending);
208119f5e44SMagnus Damm 		gpio_rcar_write(p, INTCLR, BIT(offset));
209c7f3c5d3SGeert Uytterhoeven 		generic_handle_irq(irq_find_mapping(p->gpio_chip.irqdomain,
210c7f3c5d3SGeert Uytterhoeven 						    offset));
211119f5e44SMagnus Damm 		irqs_handled++;
212119f5e44SMagnus Damm 	}
213119f5e44SMagnus Damm 
214119f5e44SMagnus Damm 	return irqs_handled ? IRQ_HANDLED : IRQ_NONE;
215119f5e44SMagnus Damm }
216119f5e44SMagnus Damm 
217119f5e44SMagnus Damm static void gpio_rcar_config_general_input_output_mode(struct gpio_chip *chip,
218119f5e44SMagnus Damm 						       unsigned int gpio,
219119f5e44SMagnus Damm 						       bool output)
220119f5e44SMagnus Damm {
221c7b6f457SLinus Walleij 	struct gpio_rcar_priv *p = gpiochip_get_data(chip);
222119f5e44SMagnus Damm 	unsigned long flags;
223119f5e44SMagnus Damm 
224119f5e44SMagnus Damm 	/* follow steps in the GPIO documentation for
225119f5e44SMagnus Damm 	 * "Setting General Output Mode" and
226119f5e44SMagnus Damm 	 * "Setting General Input Mode"
227119f5e44SMagnus Damm 	 */
228119f5e44SMagnus Damm 
229119f5e44SMagnus Damm 	spin_lock_irqsave(&p->lock, flags);
230119f5e44SMagnus Damm 
231119f5e44SMagnus Damm 	/* Configure postive logic in POSNEG */
232119f5e44SMagnus Damm 	gpio_rcar_modify_bit(p, POSNEG, gpio, false);
233119f5e44SMagnus Damm 
234119f5e44SMagnus Damm 	/* Select "General Input/Output Mode" in IOINTSEL */
235119f5e44SMagnus Damm 	gpio_rcar_modify_bit(p, IOINTSEL, gpio, false);
236119f5e44SMagnus Damm 
237119f5e44SMagnus Damm 	/* Select Input Mode or Output Mode in INOUTSEL */
238119f5e44SMagnus Damm 	gpio_rcar_modify_bit(p, INOUTSEL, gpio, output);
239119f5e44SMagnus Damm 
240119f5e44SMagnus Damm 	spin_unlock_irqrestore(&p->lock, flags);
241119f5e44SMagnus Damm }
242119f5e44SMagnus Damm 
243dc3465a9SLaurent Pinchart static int gpio_rcar_request(struct gpio_chip *chip, unsigned offset)
244dc3465a9SLaurent Pinchart {
2452d65472bSGeert Uytterhoeven 	struct gpio_rcar_priv *p = gpiochip_get_data(chip);
2462d65472bSGeert Uytterhoeven 	int error;
2472d65472bSGeert Uytterhoeven 
2482d65472bSGeert Uytterhoeven 	error = pm_runtime_get_sync(&p->pdev->dev);
2492d65472bSGeert Uytterhoeven 	if (error < 0)
2502d65472bSGeert Uytterhoeven 		return error;
2512d65472bSGeert Uytterhoeven 
2522d65472bSGeert Uytterhoeven 	error = pinctrl_request_gpio(chip->base + offset);
2532d65472bSGeert Uytterhoeven 	if (error)
2542d65472bSGeert Uytterhoeven 		pm_runtime_put(&p->pdev->dev);
2552d65472bSGeert Uytterhoeven 
2562d65472bSGeert Uytterhoeven 	return error;
257dc3465a9SLaurent Pinchart }
258dc3465a9SLaurent Pinchart 
259dc3465a9SLaurent Pinchart static void gpio_rcar_free(struct gpio_chip *chip, unsigned offset)
260dc3465a9SLaurent Pinchart {
2612d65472bSGeert Uytterhoeven 	struct gpio_rcar_priv *p = gpiochip_get_data(chip);
2622d65472bSGeert Uytterhoeven 
263dc3465a9SLaurent Pinchart 	pinctrl_free_gpio(chip->base + offset);
264dc3465a9SLaurent Pinchart 
265ce0e2c60SLinus Walleij 	/*
266ce0e2c60SLinus Walleij 	 * Set the GPIO as an input to ensure that the next GPIO request won't
267dc3465a9SLaurent Pinchart 	 * drive the GPIO pin as an output.
268dc3465a9SLaurent Pinchart 	 */
269dc3465a9SLaurent Pinchart 	gpio_rcar_config_general_input_output_mode(chip, offset, false);
2702d65472bSGeert Uytterhoeven 
2712d65472bSGeert Uytterhoeven 	pm_runtime_put(&p->pdev->dev);
272dc3465a9SLaurent Pinchart }
273dc3465a9SLaurent Pinchart 
274119f5e44SMagnus Damm static int gpio_rcar_direction_input(struct gpio_chip *chip, unsigned offset)
275119f5e44SMagnus Damm {
276119f5e44SMagnus Damm 	gpio_rcar_config_general_input_output_mode(chip, offset, false);
277119f5e44SMagnus Damm 	return 0;
278119f5e44SMagnus Damm }
279119f5e44SMagnus Damm 
280119f5e44SMagnus Damm static int gpio_rcar_get(struct gpio_chip *chip, unsigned offset)
281119f5e44SMagnus Damm {
282ae9550f6SMagnus Damm 	u32 bit = BIT(offset);
283ae9550f6SMagnus Damm 
284ae9550f6SMagnus Damm 	/* testing on r8a7790 shows that INDT does not show correct pin state
285ae9550f6SMagnus Damm 	 * when configured as output, so use OUTDT in case of output pins */
286c7b6f457SLinus Walleij 	if (gpio_rcar_read(gpiochip_get_data(chip), INOUTSEL) & bit)
287c7b6f457SLinus Walleij 		return !!(gpio_rcar_read(gpiochip_get_data(chip), OUTDT) & bit);
288ae9550f6SMagnus Damm 	else
289c7b6f457SLinus Walleij 		return !!(gpio_rcar_read(gpiochip_get_data(chip), INDT) & bit);
290119f5e44SMagnus Damm }
291119f5e44SMagnus Damm 
292119f5e44SMagnus Damm static void gpio_rcar_set(struct gpio_chip *chip, unsigned offset, int value)
293119f5e44SMagnus Damm {
294c7b6f457SLinus Walleij 	struct gpio_rcar_priv *p = gpiochip_get_data(chip);
295119f5e44SMagnus Damm 	unsigned long flags;
296119f5e44SMagnus Damm 
297119f5e44SMagnus Damm 	spin_lock_irqsave(&p->lock, flags);
298119f5e44SMagnus Damm 	gpio_rcar_modify_bit(p, OUTDT, offset, value);
299119f5e44SMagnus Damm 	spin_unlock_irqrestore(&p->lock, flags);
300119f5e44SMagnus Damm }
301119f5e44SMagnus Damm 
302dbb763b8SGeert Uytterhoeven static void gpio_rcar_set_multiple(struct gpio_chip *chip, unsigned long *mask,
303dbb763b8SGeert Uytterhoeven 				   unsigned long *bits)
304dbb763b8SGeert Uytterhoeven {
305dbb763b8SGeert Uytterhoeven 	struct gpio_rcar_priv *p = gpiochip_get_data(chip);
306dbb763b8SGeert Uytterhoeven 	unsigned long flags;
307dbb763b8SGeert Uytterhoeven 	u32 val, bankmask;
308dbb763b8SGeert Uytterhoeven 
309dbb763b8SGeert Uytterhoeven 	bankmask = mask[0] & GENMASK(chip->ngpio - 1, 0);
310dbb763b8SGeert Uytterhoeven 	if (!bankmask)
311dbb763b8SGeert Uytterhoeven 		return;
312dbb763b8SGeert Uytterhoeven 
313dbb763b8SGeert Uytterhoeven 	spin_lock_irqsave(&p->lock, flags);
314dbb763b8SGeert Uytterhoeven 	val = gpio_rcar_read(p, OUTDT);
315dbb763b8SGeert Uytterhoeven 	val &= ~bankmask;
316dbb763b8SGeert Uytterhoeven 	val |= (bankmask & bits[0]);
317dbb763b8SGeert Uytterhoeven 	gpio_rcar_write(p, OUTDT, val);
318dbb763b8SGeert Uytterhoeven 	spin_unlock_irqrestore(&p->lock, flags);
319dbb763b8SGeert Uytterhoeven }
320dbb763b8SGeert Uytterhoeven 
321119f5e44SMagnus Damm static int gpio_rcar_direction_output(struct gpio_chip *chip, unsigned offset,
322119f5e44SMagnus Damm 				      int value)
323119f5e44SMagnus Damm {
324119f5e44SMagnus Damm 	/* write GPIO value to output before selecting output mode of pin */
325119f5e44SMagnus Damm 	gpio_rcar_set(chip, offset, value);
326119f5e44SMagnus Damm 	gpio_rcar_config_general_input_output_mode(chip, offset, true);
327119f5e44SMagnus Damm 	return 0;
328119f5e44SMagnus Damm }
329119f5e44SMagnus Damm 
330850dfe17SLaurent Pinchart struct gpio_rcar_info {
331850dfe17SLaurent Pinchart 	bool has_both_edge_trigger;
332e1fef9e2SGeert Uytterhoeven 	bool needs_clk;
333850dfe17SLaurent Pinchart };
334850dfe17SLaurent Pinchart 
3351fd2b49dSHisashi Nakamura static const struct gpio_rcar_info gpio_rcar_info_gen1 = {
3361fd2b49dSHisashi Nakamura 	.has_both_edge_trigger = false,
337e1fef9e2SGeert Uytterhoeven 	.needs_clk = false,
3381fd2b49dSHisashi Nakamura };
3391fd2b49dSHisashi Nakamura 
3401fd2b49dSHisashi Nakamura static const struct gpio_rcar_info gpio_rcar_info_gen2 = {
3411fd2b49dSHisashi Nakamura 	.has_both_edge_trigger = true,
342e1fef9e2SGeert Uytterhoeven 	.needs_clk = true,
3431fd2b49dSHisashi Nakamura };
3441fd2b49dSHisashi Nakamura 
345850dfe17SLaurent Pinchart static const struct of_device_id gpio_rcar_of_table[] = {
346850dfe17SLaurent Pinchart 	{
347850dfe17SLaurent Pinchart 		.compatible = "renesas,gpio-r8a7790",
3481fd2b49dSHisashi Nakamura 		.data = &gpio_rcar_info_gen2,
349850dfe17SLaurent Pinchart 	}, {
350850dfe17SLaurent Pinchart 		.compatible = "renesas,gpio-r8a7791",
3511fd2b49dSHisashi Nakamura 		.data = &gpio_rcar_info_gen2,
3521fd2b49dSHisashi Nakamura 	}, {
353e79c5830SSergei Shtylyov 		.compatible = "renesas,gpio-r8a7792",
354e79c5830SSergei Shtylyov 		.data = &gpio_rcar_info_gen2,
355e79c5830SSergei Shtylyov 	}, {
3561fd2b49dSHisashi Nakamura 		.compatible = "renesas,gpio-r8a7793",
3571fd2b49dSHisashi Nakamura 		.data = &gpio_rcar_info_gen2,
3581fd2b49dSHisashi Nakamura 	}, {
3591fd2b49dSHisashi Nakamura 		.compatible = "renesas,gpio-r8a7794",
3601fd2b49dSHisashi Nakamura 		.data = &gpio_rcar_info_gen2,
361850dfe17SLaurent Pinchart 	}, {
3628cd14702SUlrich Hecht 		.compatible = "renesas,gpio-r8a7795",
3638cd14702SUlrich Hecht 		/* Gen3 GPIO is identical to Gen2. */
3648cd14702SUlrich Hecht 		.data = &gpio_rcar_info_gen2,
3658cd14702SUlrich Hecht 	}, {
3665d2f1d6eSSimon Horman 		.compatible = "renesas,gpio-r8a7796",
3675d2f1d6eSSimon Horman 		/* Gen3 GPIO is identical to Gen2. */
3685d2f1d6eSSimon Horman 		.data = &gpio_rcar_info_gen2,
3695d2f1d6eSSimon Horman 	}, {
370850dfe17SLaurent Pinchart 		.compatible = "renesas,gpio-rcar",
3711fd2b49dSHisashi Nakamura 		.data = &gpio_rcar_info_gen1,
372850dfe17SLaurent Pinchart 	}, {
373850dfe17SLaurent Pinchart 		/* Terminator */
374850dfe17SLaurent Pinchart 	},
375850dfe17SLaurent Pinchart };
376850dfe17SLaurent Pinchart 
377850dfe17SLaurent Pinchart MODULE_DEVICE_TABLE(of, gpio_rcar_of_table);
378850dfe17SLaurent Pinchart 
3798b092be9SGeert Uytterhoeven static int gpio_rcar_parse_dt(struct gpio_rcar_priv *p, unsigned int *npins)
380159f8a02SLaurent Pinchart {
381159f8a02SLaurent Pinchart 	struct device_node *np = p->pdev->dev.of_node;
382850dfe17SLaurent Pinchart 	const struct of_device_id *match;
383850dfe17SLaurent Pinchart 	const struct gpio_rcar_info *info;
3848b092be9SGeert Uytterhoeven 	struct of_phandle_args args;
3858b092be9SGeert Uytterhoeven 	int ret;
386850dfe17SLaurent Pinchart 
387850dfe17SLaurent Pinchart 	match = of_match_node(gpio_rcar_of_table, np);
388850dfe17SLaurent Pinchart 	if (!match)
389850dfe17SLaurent Pinchart 		return -EINVAL;
390850dfe17SLaurent Pinchart 
391850dfe17SLaurent Pinchart 	info = match->data;
392850dfe17SLaurent Pinchart 
3938b092be9SGeert Uytterhoeven 	ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3, 0, &args);
3948b092be9SGeert Uytterhoeven 	*npins = ret == 0 ? args.args[2] : RCAR_MAX_GPIO_PER_BANK;
3958b092be9SGeert Uytterhoeven 	p->has_both_edge_trigger = info->has_both_edge_trigger;
396e1fef9e2SGeert Uytterhoeven 	p->needs_clk = info->needs_clk;
397159f8a02SLaurent Pinchart 
3988b092be9SGeert Uytterhoeven 	if (*npins == 0 || *npins > RCAR_MAX_GPIO_PER_BANK) {
399159f8a02SLaurent Pinchart 		dev_warn(&p->pdev->dev,
4008b092be9SGeert Uytterhoeven 			 "Invalid number of gpio lines %u, using %u\n", *npins,
4018b092be9SGeert Uytterhoeven 			 RCAR_MAX_GPIO_PER_BANK);
4028b092be9SGeert Uytterhoeven 		*npins = RCAR_MAX_GPIO_PER_BANK;
403159f8a02SLaurent Pinchart 	}
404850dfe17SLaurent Pinchart 
405850dfe17SLaurent Pinchart 	return 0;
406159f8a02SLaurent Pinchart }
407159f8a02SLaurent Pinchart 
408119f5e44SMagnus Damm static int gpio_rcar_probe(struct platform_device *pdev)
409119f5e44SMagnus Damm {
410119f5e44SMagnus Damm 	struct gpio_rcar_priv *p;
411119f5e44SMagnus Damm 	struct resource *io, *irq;
412119f5e44SMagnus Damm 	struct gpio_chip *gpio_chip;
413119f5e44SMagnus Damm 	struct irq_chip *irq_chip;
414b22978fcSGeert Uytterhoeven 	struct device *dev = &pdev->dev;
415b22978fcSGeert Uytterhoeven 	const char *name = dev_name(dev);
4168b092be9SGeert Uytterhoeven 	unsigned int npins;
417119f5e44SMagnus Damm 	int ret;
418119f5e44SMagnus Damm 
419b22978fcSGeert Uytterhoeven 	p = devm_kzalloc(dev, sizeof(*p), GFP_KERNEL);
4207d82bf34SGeert Uytterhoeven 	if (!p)
4217d82bf34SGeert Uytterhoeven 		return -ENOMEM;
422119f5e44SMagnus Damm 
423119f5e44SMagnus Damm 	p->pdev = pdev;
424119f5e44SMagnus Damm 	spin_lock_init(&p->lock);
425119f5e44SMagnus Damm 
4268b092be9SGeert Uytterhoeven 	/* Get device configuration from DT node */
4278b092be9SGeert Uytterhoeven 	ret = gpio_rcar_parse_dt(p, &npins);
428850dfe17SLaurent Pinchart 	if (ret < 0)
429850dfe17SLaurent Pinchart 		return ret;
430159f8a02SLaurent Pinchart 
431159f8a02SLaurent Pinchart 	platform_set_drvdata(pdev, p);
432159f8a02SLaurent Pinchart 
433ab82fa7dSGeert Uytterhoeven 	p->clk = devm_clk_get(dev, NULL);
434ab82fa7dSGeert Uytterhoeven 	if (IS_ERR(p->clk)) {
435e1fef9e2SGeert Uytterhoeven 		if (p->needs_clk) {
436e1fef9e2SGeert Uytterhoeven 			dev_err(dev, "unable to get clock\n");
437e1fef9e2SGeert Uytterhoeven 			ret = PTR_ERR(p->clk);
438e1fef9e2SGeert Uytterhoeven 			goto err0;
439e1fef9e2SGeert Uytterhoeven 		}
440ab82fa7dSGeert Uytterhoeven 		p->clk = NULL;
441ab82fa7dSGeert Uytterhoeven 	}
442ab82fa7dSGeert Uytterhoeven 
443df0c6c80SGeert Uytterhoeven 	pm_runtime_enable(dev);
444df0c6c80SGeert Uytterhoeven 
445119f5e44SMagnus Damm 	io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
446119f5e44SMagnus Damm 	irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
447119f5e44SMagnus Damm 
448119f5e44SMagnus Damm 	if (!io || !irq) {
449b22978fcSGeert Uytterhoeven 		dev_err(dev, "missing IRQ or IOMEM\n");
450119f5e44SMagnus Damm 		ret = -EINVAL;
451119f5e44SMagnus Damm 		goto err0;
452119f5e44SMagnus Damm 	}
453119f5e44SMagnus Damm 
454b22978fcSGeert Uytterhoeven 	p->base = devm_ioremap_nocache(dev, io->start, resource_size(io));
455119f5e44SMagnus Damm 	if (!p->base) {
456b22978fcSGeert Uytterhoeven 		dev_err(dev, "failed to remap I/O memory\n");
457119f5e44SMagnus Damm 		ret = -ENXIO;
458119f5e44SMagnus Damm 		goto err0;
459119f5e44SMagnus Damm 	}
460119f5e44SMagnus Damm 
461119f5e44SMagnus Damm 	gpio_chip = &p->gpio_chip;
462dc3465a9SLaurent Pinchart 	gpio_chip->request = gpio_rcar_request;
463dc3465a9SLaurent Pinchart 	gpio_chip->free = gpio_rcar_free;
464119f5e44SMagnus Damm 	gpio_chip->direction_input = gpio_rcar_direction_input;
465119f5e44SMagnus Damm 	gpio_chip->get = gpio_rcar_get;
466119f5e44SMagnus Damm 	gpio_chip->direction_output = gpio_rcar_direction_output;
467119f5e44SMagnus Damm 	gpio_chip->set = gpio_rcar_set;
468dbb763b8SGeert Uytterhoeven 	gpio_chip->set_multiple = gpio_rcar_set_multiple;
469119f5e44SMagnus Damm 	gpio_chip->label = name;
47058383c78SLinus Walleij 	gpio_chip->parent = dev;
471119f5e44SMagnus Damm 	gpio_chip->owner = THIS_MODULE;
4728b092be9SGeert Uytterhoeven 	gpio_chip->base = -1;
4738b092be9SGeert Uytterhoeven 	gpio_chip->ngpio = npins;
474119f5e44SMagnus Damm 
475119f5e44SMagnus Damm 	irq_chip = &p->irq_chip;
476119f5e44SMagnus Damm 	irq_chip->name = name;
47747bd38a3SNiklas Söderlund 	irq_chip->parent_device = dev;
478119f5e44SMagnus Damm 	irq_chip->irq_mask = gpio_rcar_irq_disable;
479119f5e44SMagnus Damm 	irq_chip->irq_unmask = gpio_rcar_irq_enable;
480119f5e44SMagnus Damm 	irq_chip->irq_set_type = gpio_rcar_irq_set_type;
481ab82fa7dSGeert Uytterhoeven 	irq_chip->irq_set_wake = gpio_rcar_irq_set_wake;
482ab82fa7dSGeert Uytterhoeven 	irq_chip->flags	= IRQCHIP_SET_TYPE_MASKED | IRQCHIP_MASK_ON_SUSPEND;
483119f5e44SMagnus Damm 
484c7b6f457SLinus Walleij 	ret = gpiochip_add_data(gpio_chip, p);
485c7f3c5d3SGeert Uytterhoeven 	if (ret) {
486c7f3c5d3SGeert Uytterhoeven 		dev_err(dev, "failed to add GPIO controller\n");
4870c8aab8eSDan Carpenter 		goto err0;
488119f5e44SMagnus Damm 	}
489119f5e44SMagnus Damm 
4908b092be9SGeert Uytterhoeven 	ret = gpiochip_irqchip_add(gpio_chip, irq_chip, 0, handle_level_irq,
4918b092be9SGeert Uytterhoeven 				   IRQ_TYPE_NONE);
492c7f3c5d3SGeert Uytterhoeven 	if (ret) {
493c7f3c5d3SGeert Uytterhoeven 		dev_err(dev, "cannot add irqchip\n");
494c7f3c5d3SGeert Uytterhoeven 		goto err1;
495c7f3c5d3SGeert Uytterhoeven 	}
496c7f3c5d3SGeert Uytterhoeven 
497ab82fa7dSGeert Uytterhoeven 	p->irq_parent = irq->start;
498b22978fcSGeert Uytterhoeven 	if (devm_request_irq(dev, irq->start, gpio_rcar_irq_handler,
499b22978fcSGeert Uytterhoeven 			     IRQF_SHARED, name, p)) {
500b22978fcSGeert Uytterhoeven 		dev_err(dev, "failed to request IRQ\n");
501119f5e44SMagnus Damm 		ret = -ENOENT;
502119f5e44SMagnus Damm 		goto err1;
503119f5e44SMagnus Damm 	}
504119f5e44SMagnus Damm 
5058b092be9SGeert Uytterhoeven 	dev_info(dev, "driving %d GPIOs\n", npins);
506dc3465a9SLaurent Pinchart 
507119f5e44SMagnus Damm 	return 0;
508119f5e44SMagnus Damm 
509119f5e44SMagnus Damm err1:
5104d84b9e4SGeert Uytterhoeven 	gpiochip_remove(gpio_chip);
511119f5e44SMagnus Damm err0:
512df0c6c80SGeert Uytterhoeven 	pm_runtime_disable(dev);
513119f5e44SMagnus Damm 	return ret;
514119f5e44SMagnus Damm }
515119f5e44SMagnus Damm 
516119f5e44SMagnus Damm static int gpio_rcar_remove(struct platform_device *pdev)
517119f5e44SMagnus Damm {
518119f5e44SMagnus Damm 	struct gpio_rcar_priv *p = platform_get_drvdata(pdev);
519119f5e44SMagnus Damm 
5209f5132aeSabdoulaye berthe 	gpiochip_remove(&p->gpio_chip);
521119f5e44SMagnus Damm 
522df0c6c80SGeert Uytterhoeven 	pm_runtime_disable(&pdev->dev);
523119f5e44SMagnus Damm 	return 0;
524119f5e44SMagnus Damm }
525119f5e44SMagnus Damm 
526119f5e44SMagnus Damm static struct platform_driver gpio_rcar_device_driver = {
527119f5e44SMagnus Damm 	.probe		= gpio_rcar_probe,
528119f5e44SMagnus Damm 	.remove		= gpio_rcar_remove,
529119f5e44SMagnus Damm 	.driver		= {
530119f5e44SMagnus Damm 		.name	= "gpio_rcar",
531159f8a02SLaurent Pinchart 		.of_match_table = of_match_ptr(gpio_rcar_of_table),
532119f5e44SMagnus Damm 	}
533119f5e44SMagnus Damm };
534119f5e44SMagnus Damm 
535119f5e44SMagnus Damm module_platform_driver(gpio_rcar_device_driver);
536119f5e44SMagnus Damm 
537119f5e44SMagnus Damm MODULE_AUTHOR("Magnus Damm");
538119f5e44SMagnus Damm MODULE_DESCRIPTION("Renesas R-Car GPIO Driver");
539119f5e44SMagnus Damm MODULE_LICENSE("GPL v2");
540