xref: /openbmc/linux/drivers/gpio/gpio-rcar.c (revision 8cd14702)
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_data/gpio-rcar.h>
29119f5e44SMagnus Damm #include <linux/platform_device.h>
30df0c6c80SGeert Uytterhoeven #include <linux/pm_runtime.h>
31119f5e44SMagnus Damm #include <linux/spinlock.h>
32119f5e44SMagnus Damm #include <linux/slab.h>
33119f5e44SMagnus Damm 
34119f5e44SMagnus Damm struct gpio_rcar_priv {
35119f5e44SMagnus Damm 	void __iomem *base;
36119f5e44SMagnus Damm 	spinlock_t lock;
37119f5e44SMagnus Damm 	struct gpio_rcar_config config;
38119f5e44SMagnus Damm 	struct platform_device *pdev;
39119f5e44SMagnus Damm 	struct gpio_chip gpio_chip;
40119f5e44SMagnus Damm 	struct irq_chip irq_chip;
41ab82fa7dSGeert Uytterhoeven 	unsigned int irq_parent;
42ab82fa7dSGeert Uytterhoeven 	struct clk *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);
87c7f3c5d3SGeert Uytterhoeven 	struct gpio_rcar_priv *p = container_of(gc, struct gpio_rcar_priv,
88c7f3c5d3SGeert Uytterhoeven 						gpio_chip);
89119f5e44SMagnus Damm 
90119f5e44SMagnus Damm 	gpio_rcar_write(p, INTMSK, ~BIT(irqd_to_hwirq(d)));
91119f5e44SMagnus Damm }
92119f5e44SMagnus Damm 
93119f5e44SMagnus Damm static void gpio_rcar_irq_enable(struct irq_data *d)
94119f5e44SMagnus Damm {
95c7f3c5d3SGeert Uytterhoeven 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
96c7f3c5d3SGeert Uytterhoeven 	struct gpio_rcar_priv *p = container_of(gc, struct gpio_rcar_priv,
97c7f3c5d3SGeert Uytterhoeven 						gpio_chip);
98119f5e44SMagnus Damm 
99119f5e44SMagnus Damm 	gpio_rcar_write(p, MSKCLR, BIT(irqd_to_hwirq(d)));
100119f5e44SMagnus Damm }
101119f5e44SMagnus Damm 
102119f5e44SMagnus Damm static void gpio_rcar_config_interrupt_input_mode(struct gpio_rcar_priv *p,
103119f5e44SMagnus Damm 						  unsigned int hwirq,
104119f5e44SMagnus Damm 						  bool active_high_rising_edge,
1057e1092b5SSimon Horman 						  bool level_trigger,
1067e1092b5SSimon Horman 						  bool both)
107119f5e44SMagnus Damm {
108119f5e44SMagnus Damm 	unsigned long flags;
109119f5e44SMagnus Damm 
110119f5e44SMagnus Damm 	/* follow steps in the GPIO documentation for
111119f5e44SMagnus Damm 	 * "Setting Edge-Sensitive Interrupt Input Mode" and
112119f5e44SMagnus Damm 	 * "Setting Level-Sensitive Interrupt Input Mode"
113119f5e44SMagnus Damm 	 */
114119f5e44SMagnus Damm 
115119f5e44SMagnus Damm 	spin_lock_irqsave(&p->lock, flags);
116119f5e44SMagnus Damm 
117119f5e44SMagnus Damm 	/* Configure postive or negative logic in POSNEG */
118119f5e44SMagnus Damm 	gpio_rcar_modify_bit(p, POSNEG, hwirq, !active_high_rising_edge);
119119f5e44SMagnus Damm 
120119f5e44SMagnus Damm 	/* Configure edge or level trigger in EDGLEVEL */
121119f5e44SMagnus Damm 	gpio_rcar_modify_bit(p, EDGLEVEL, hwirq, !level_trigger);
122119f5e44SMagnus Damm 
1237e1092b5SSimon Horman 	/* Select one edge or both edges in BOTHEDGE */
1247e1092b5SSimon Horman 	if (p->config.has_both_edge_trigger)
1257e1092b5SSimon Horman 		gpio_rcar_modify_bit(p, BOTHEDGE, hwirq, both);
1267e1092b5SSimon Horman 
127119f5e44SMagnus Damm 	/* Select "Interrupt Input Mode" in IOINTSEL */
128119f5e44SMagnus Damm 	gpio_rcar_modify_bit(p, IOINTSEL, hwirq, true);
129119f5e44SMagnus Damm 
130119f5e44SMagnus Damm 	/* Write INTCLR in case of edge trigger */
131119f5e44SMagnus Damm 	if (!level_trigger)
132119f5e44SMagnus Damm 		gpio_rcar_write(p, INTCLR, BIT(hwirq));
133119f5e44SMagnus Damm 
134119f5e44SMagnus Damm 	spin_unlock_irqrestore(&p->lock, flags);
135119f5e44SMagnus Damm }
136119f5e44SMagnus Damm 
137119f5e44SMagnus Damm static int gpio_rcar_irq_set_type(struct irq_data *d, unsigned int type)
138119f5e44SMagnus Damm {
139c7f3c5d3SGeert Uytterhoeven 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
140c7f3c5d3SGeert Uytterhoeven 	struct gpio_rcar_priv *p = container_of(gc, struct gpio_rcar_priv,
141c7f3c5d3SGeert Uytterhoeven 						gpio_chip);
142119f5e44SMagnus Damm 	unsigned int hwirq = irqd_to_hwirq(d);
143119f5e44SMagnus Damm 
144119f5e44SMagnus Damm 	dev_dbg(&p->pdev->dev, "sense irq = %d, type = %d\n", hwirq, type);
145119f5e44SMagnus Damm 
146119f5e44SMagnus Damm 	switch (type & IRQ_TYPE_SENSE_MASK) {
147119f5e44SMagnus Damm 	case IRQ_TYPE_LEVEL_HIGH:
1487e1092b5SSimon Horman 		gpio_rcar_config_interrupt_input_mode(p, hwirq, true, true,
1497e1092b5SSimon Horman 						      false);
150119f5e44SMagnus Damm 		break;
151119f5e44SMagnus Damm 	case IRQ_TYPE_LEVEL_LOW:
1527e1092b5SSimon Horman 		gpio_rcar_config_interrupt_input_mode(p, hwirq, false, true,
1537e1092b5SSimon Horman 						      false);
154119f5e44SMagnus Damm 		break;
155119f5e44SMagnus Damm 	case IRQ_TYPE_EDGE_RISING:
1567e1092b5SSimon Horman 		gpio_rcar_config_interrupt_input_mode(p, hwirq, true, false,
1577e1092b5SSimon Horman 						      false);
158119f5e44SMagnus Damm 		break;
159119f5e44SMagnus Damm 	case IRQ_TYPE_EDGE_FALLING:
1607e1092b5SSimon Horman 		gpio_rcar_config_interrupt_input_mode(p, hwirq, false, false,
1617e1092b5SSimon Horman 						      false);
1627e1092b5SSimon Horman 		break;
1637e1092b5SSimon Horman 	case IRQ_TYPE_EDGE_BOTH:
1647e1092b5SSimon Horman 		if (!p->config.has_both_edge_trigger)
1657e1092b5SSimon Horman 			return -EINVAL;
1667e1092b5SSimon Horman 		gpio_rcar_config_interrupt_input_mode(p, hwirq, true, false,
1677e1092b5SSimon Horman 						      true);
168119f5e44SMagnus Damm 		break;
169119f5e44SMagnus Damm 	default:
170119f5e44SMagnus Damm 		return -EINVAL;
171119f5e44SMagnus Damm 	}
172119f5e44SMagnus Damm 	return 0;
173119f5e44SMagnus Damm }
174119f5e44SMagnus Damm 
175ab82fa7dSGeert Uytterhoeven static int gpio_rcar_irq_set_wake(struct irq_data *d, unsigned int on)
176ab82fa7dSGeert Uytterhoeven {
177ab82fa7dSGeert Uytterhoeven 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
178ab82fa7dSGeert Uytterhoeven 	struct gpio_rcar_priv *p = container_of(gc, struct gpio_rcar_priv,
179ab82fa7dSGeert Uytterhoeven 						gpio_chip);
180501ef0f9SGeert Uytterhoeven 	int error;
181ab82fa7dSGeert Uytterhoeven 
182501ef0f9SGeert Uytterhoeven 	if (p->irq_parent) {
183501ef0f9SGeert Uytterhoeven 		error = irq_set_irq_wake(p->irq_parent, on);
184501ef0f9SGeert Uytterhoeven 		if (error) {
185501ef0f9SGeert Uytterhoeven 			dev_dbg(&p->pdev->dev,
186501ef0f9SGeert Uytterhoeven 				"irq %u doesn't support irq_set_wake\n",
187501ef0f9SGeert Uytterhoeven 				p->irq_parent);
188501ef0f9SGeert Uytterhoeven 			p->irq_parent = 0;
189501ef0f9SGeert Uytterhoeven 		}
190501ef0f9SGeert Uytterhoeven 	}
191ab82fa7dSGeert Uytterhoeven 
192ab82fa7dSGeert Uytterhoeven 	if (!p->clk)
193ab82fa7dSGeert Uytterhoeven 		return 0;
194ab82fa7dSGeert Uytterhoeven 
195ab82fa7dSGeert Uytterhoeven 	if (on)
196ab82fa7dSGeert Uytterhoeven 		clk_enable(p->clk);
197ab82fa7dSGeert Uytterhoeven 	else
198ab82fa7dSGeert Uytterhoeven 		clk_disable(p->clk);
199ab82fa7dSGeert Uytterhoeven 
200ab82fa7dSGeert Uytterhoeven 	return 0;
201ab82fa7dSGeert Uytterhoeven }
202ab82fa7dSGeert Uytterhoeven 
203119f5e44SMagnus Damm static irqreturn_t gpio_rcar_irq_handler(int irq, void *dev_id)
204119f5e44SMagnus Damm {
205119f5e44SMagnus Damm 	struct gpio_rcar_priv *p = dev_id;
206119f5e44SMagnus Damm 	u32 pending;
207119f5e44SMagnus Damm 	unsigned int offset, irqs_handled = 0;
208119f5e44SMagnus Damm 
2098808b64dSValentine Barshak 	while ((pending = gpio_rcar_read(p, INTDT) &
2108808b64dSValentine Barshak 			  gpio_rcar_read(p, INTMSK))) {
211119f5e44SMagnus Damm 		offset = __ffs(pending);
212119f5e44SMagnus Damm 		gpio_rcar_write(p, INTCLR, BIT(offset));
213c7f3c5d3SGeert Uytterhoeven 		generic_handle_irq(irq_find_mapping(p->gpio_chip.irqdomain,
214c7f3c5d3SGeert Uytterhoeven 						    offset));
215119f5e44SMagnus Damm 		irqs_handled++;
216119f5e44SMagnus Damm 	}
217119f5e44SMagnus Damm 
218119f5e44SMagnus Damm 	return irqs_handled ? IRQ_HANDLED : IRQ_NONE;
219119f5e44SMagnus Damm }
220119f5e44SMagnus Damm 
221119f5e44SMagnus Damm static inline struct gpio_rcar_priv *gpio_to_priv(struct gpio_chip *chip)
222119f5e44SMagnus Damm {
223119f5e44SMagnus Damm 	return container_of(chip, struct gpio_rcar_priv, gpio_chip);
224119f5e44SMagnus Damm }
225119f5e44SMagnus Damm 
226119f5e44SMagnus Damm static void gpio_rcar_config_general_input_output_mode(struct gpio_chip *chip,
227119f5e44SMagnus Damm 						       unsigned int gpio,
228119f5e44SMagnus Damm 						       bool output)
229119f5e44SMagnus Damm {
230119f5e44SMagnus Damm 	struct gpio_rcar_priv *p = gpio_to_priv(chip);
231119f5e44SMagnus Damm 	unsigned long flags;
232119f5e44SMagnus Damm 
233119f5e44SMagnus Damm 	/* follow steps in the GPIO documentation for
234119f5e44SMagnus Damm 	 * "Setting General Output Mode" and
235119f5e44SMagnus Damm 	 * "Setting General Input Mode"
236119f5e44SMagnus Damm 	 */
237119f5e44SMagnus Damm 
238119f5e44SMagnus Damm 	spin_lock_irqsave(&p->lock, flags);
239119f5e44SMagnus Damm 
240119f5e44SMagnus Damm 	/* Configure postive logic in POSNEG */
241119f5e44SMagnus Damm 	gpio_rcar_modify_bit(p, POSNEG, gpio, false);
242119f5e44SMagnus Damm 
243119f5e44SMagnus Damm 	/* Select "General Input/Output Mode" in IOINTSEL */
244119f5e44SMagnus Damm 	gpio_rcar_modify_bit(p, IOINTSEL, gpio, false);
245119f5e44SMagnus Damm 
246119f5e44SMagnus Damm 	/* Select Input Mode or Output Mode in INOUTSEL */
247119f5e44SMagnus Damm 	gpio_rcar_modify_bit(p, INOUTSEL, gpio, output);
248119f5e44SMagnus Damm 
249119f5e44SMagnus Damm 	spin_unlock_irqrestore(&p->lock, flags);
250119f5e44SMagnus Damm }
251119f5e44SMagnus Damm 
252dc3465a9SLaurent Pinchart static int gpio_rcar_request(struct gpio_chip *chip, unsigned offset)
253dc3465a9SLaurent Pinchart {
25465194cb1SGeert Uytterhoeven 	struct gpio_rcar_priv *p = gpio_to_priv(chip);
25565194cb1SGeert Uytterhoeven 	int error;
25665194cb1SGeert Uytterhoeven 
25765194cb1SGeert Uytterhoeven 	error = pm_runtime_get_sync(&p->pdev->dev);
25865194cb1SGeert Uytterhoeven 	if (error < 0)
25965194cb1SGeert Uytterhoeven 		return error;
26065194cb1SGeert Uytterhoeven 
26165194cb1SGeert Uytterhoeven 	error = pinctrl_request_gpio(chip->base + offset);
26265194cb1SGeert Uytterhoeven 	if (error)
26365194cb1SGeert Uytterhoeven 		pm_runtime_put(&p->pdev->dev);
26465194cb1SGeert Uytterhoeven 
26565194cb1SGeert Uytterhoeven 	return error;
266dc3465a9SLaurent Pinchart }
267dc3465a9SLaurent Pinchart 
268dc3465a9SLaurent Pinchart static void gpio_rcar_free(struct gpio_chip *chip, unsigned offset)
269dc3465a9SLaurent Pinchart {
27065194cb1SGeert Uytterhoeven 	struct gpio_rcar_priv *p = gpio_to_priv(chip);
27165194cb1SGeert Uytterhoeven 
272dc3465a9SLaurent Pinchart 	pinctrl_free_gpio(chip->base + offset);
273dc3465a9SLaurent Pinchart 
274dc3465a9SLaurent Pinchart 	/* Set the GPIO as an input to ensure that the next GPIO request won't
275dc3465a9SLaurent Pinchart 	 * drive the GPIO pin as an output.
276dc3465a9SLaurent Pinchart 	 */
277dc3465a9SLaurent Pinchart 	gpio_rcar_config_general_input_output_mode(chip, offset, false);
27865194cb1SGeert Uytterhoeven 
27965194cb1SGeert Uytterhoeven 	pm_runtime_put(&p->pdev->dev);
280dc3465a9SLaurent Pinchart }
281dc3465a9SLaurent Pinchart 
282119f5e44SMagnus Damm static int gpio_rcar_direction_input(struct gpio_chip *chip, unsigned offset)
283119f5e44SMagnus Damm {
284119f5e44SMagnus Damm 	gpio_rcar_config_general_input_output_mode(chip, offset, false);
285119f5e44SMagnus Damm 	return 0;
286119f5e44SMagnus Damm }
287119f5e44SMagnus Damm 
288119f5e44SMagnus Damm static int gpio_rcar_get(struct gpio_chip *chip, unsigned offset)
289119f5e44SMagnus Damm {
290ae9550f6SMagnus Damm 	u32 bit = BIT(offset);
291ae9550f6SMagnus Damm 
292ae9550f6SMagnus Damm 	/* testing on r8a7790 shows that INDT does not show correct pin state
293ae9550f6SMagnus Damm 	 * when configured as output, so use OUTDT in case of output pins */
294ae9550f6SMagnus Damm 	if (gpio_rcar_read(gpio_to_priv(chip), INOUTSEL) & bit)
2957cb5409bSJürg Billeter 		return !!(gpio_rcar_read(gpio_to_priv(chip), OUTDT) & bit);
296ae9550f6SMagnus Damm 	else
2977cb5409bSJürg Billeter 		return !!(gpio_rcar_read(gpio_to_priv(chip), INDT) & bit);
298119f5e44SMagnus Damm }
299119f5e44SMagnus Damm 
300119f5e44SMagnus Damm static void gpio_rcar_set(struct gpio_chip *chip, unsigned offset, int value)
301119f5e44SMagnus Damm {
302119f5e44SMagnus Damm 	struct gpio_rcar_priv *p = gpio_to_priv(chip);
303119f5e44SMagnus Damm 	unsigned long flags;
304119f5e44SMagnus Damm 
305119f5e44SMagnus Damm 	spin_lock_irqsave(&p->lock, flags);
306119f5e44SMagnus Damm 	gpio_rcar_modify_bit(p, OUTDT, offset, value);
307119f5e44SMagnus Damm 	spin_unlock_irqrestore(&p->lock, flags);
308119f5e44SMagnus Damm }
309119f5e44SMagnus Damm 
310119f5e44SMagnus Damm static int gpio_rcar_direction_output(struct gpio_chip *chip, unsigned offset,
311119f5e44SMagnus Damm 				      int value)
312119f5e44SMagnus Damm {
313119f5e44SMagnus Damm 	/* write GPIO value to output before selecting output mode of pin */
314119f5e44SMagnus Damm 	gpio_rcar_set(chip, offset, value);
315119f5e44SMagnus Damm 	gpio_rcar_config_general_input_output_mode(chip, offset, true);
316119f5e44SMagnus Damm 	return 0;
317119f5e44SMagnus Damm }
318119f5e44SMagnus Damm 
319850dfe17SLaurent Pinchart struct gpio_rcar_info {
320850dfe17SLaurent Pinchart 	bool has_both_edge_trigger;
321850dfe17SLaurent Pinchart };
322850dfe17SLaurent Pinchart 
3231fd2b49dSHisashi Nakamura static const struct gpio_rcar_info gpio_rcar_info_gen1 = {
3241fd2b49dSHisashi Nakamura 	.has_both_edge_trigger = false,
3251fd2b49dSHisashi Nakamura };
3261fd2b49dSHisashi Nakamura 
3271fd2b49dSHisashi Nakamura static const struct gpio_rcar_info gpio_rcar_info_gen2 = {
3281fd2b49dSHisashi Nakamura 	.has_both_edge_trigger = true,
3291fd2b49dSHisashi Nakamura };
3301fd2b49dSHisashi Nakamura 
331850dfe17SLaurent Pinchart static const struct of_device_id gpio_rcar_of_table[] = {
332850dfe17SLaurent Pinchart 	{
333850dfe17SLaurent Pinchart 		.compatible = "renesas,gpio-r8a7790",
3341fd2b49dSHisashi Nakamura 		.data = &gpio_rcar_info_gen2,
335850dfe17SLaurent Pinchart 	}, {
336850dfe17SLaurent Pinchart 		.compatible = "renesas,gpio-r8a7791",
3371fd2b49dSHisashi Nakamura 		.data = &gpio_rcar_info_gen2,
3381fd2b49dSHisashi Nakamura 	}, {
3391fd2b49dSHisashi Nakamura 		.compatible = "renesas,gpio-r8a7793",
3401fd2b49dSHisashi Nakamura 		.data = &gpio_rcar_info_gen2,
3411fd2b49dSHisashi Nakamura 	}, {
3421fd2b49dSHisashi Nakamura 		.compatible = "renesas,gpio-r8a7794",
3431fd2b49dSHisashi Nakamura 		.data = &gpio_rcar_info_gen2,
344850dfe17SLaurent Pinchart 	}, {
3458cd14702SUlrich Hecht 		.compatible = "renesas,gpio-r8a7795",
3468cd14702SUlrich Hecht 		/* Gen3 GPIO is identical to Gen2. */
3478cd14702SUlrich Hecht 		.data = &gpio_rcar_info_gen2,
3488cd14702SUlrich Hecht 	}, {
349850dfe17SLaurent Pinchart 		.compatible = "renesas,gpio-rcar",
3501fd2b49dSHisashi Nakamura 		.data = &gpio_rcar_info_gen1,
351850dfe17SLaurent Pinchart 	}, {
352850dfe17SLaurent Pinchart 		/* Terminator */
353850dfe17SLaurent Pinchart 	},
354850dfe17SLaurent Pinchart };
355850dfe17SLaurent Pinchart 
356850dfe17SLaurent Pinchart MODULE_DEVICE_TABLE(of, gpio_rcar_of_table);
357850dfe17SLaurent Pinchart 
358850dfe17SLaurent Pinchart static int gpio_rcar_parse_pdata(struct gpio_rcar_priv *p)
359159f8a02SLaurent Pinchart {
360e56aee18SJingoo Han 	struct gpio_rcar_config *pdata = dev_get_platdata(&p->pdev->dev);
361159f8a02SLaurent Pinchart 	struct device_node *np = p->pdev->dev.of_node;
362159f8a02SLaurent Pinchart 	struct of_phandle_args args;
363159f8a02SLaurent Pinchart 	int ret;
364159f8a02SLaurent Pinchart 
365e305062eSLaurent Pinchart 	if (pdata) {
366159f8a02SLaurent Pinchart 		p->config = *pdata;
367e305062eSLaurent Pinchart 	} else if (IS_ENABLED(CONFIG_OF) && np) {
368850dfe17SLaurent Pinchart 		const struct of_device_id *match;
369850dfe17SLaurent Pinchart 		const struct gpio_rcar_info *info;
370850dfe17SLaurent Pinchart 
371850dfe17SLaurent Pinchart 		match = of_match_node(gpio_rcar_of_table, np);
372850dfe17SLaurent Pinchart 		if (!match)
373850dfe17SLaurent Pinchart 			return -EINVAL;
374850dfe17SLaurent Pinchart 
375850dfe17SLaurent Pinchart 		info = match->data;
376850dfe17SLaurent Pinchart 
37701eb2d18SLaurent Pinchart 		ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3, 0,
37801eb2d18SLaurent Pinchart 						       &args);
37901eb2d18SLaurent Pinchart 		p->config.number_of_pins = ret == 0 ? args.args[2]
380159f8a02SLaurent Pinchart 					 : RCAR_MAX_GPIO_PER_BANK;
381159f8a02SLaurent Pinchart 		p->config.gpio_base = -1;
382850dfe17SLaurent Pinchart 		p->config.has_both_edge_trigger = info->has_both_edge_trigger;
383159f8a02SLaurent Pinchart 	}
384159f8a02SLaurent Pinchart 
385159f8a02SLaurent Pinchart 	if (p->config.number_of_pins == 0 ||
386159f8a02SLaurent Pinchart 	    p->config.number_of_pins > RCAR_MAX_GPIO_PER_BANK) {
387159f8a02SLaurent Pinchart 		dev_warn(&p->pdev->dev,
388159f8a02SLaurent Pinchart 			 "Invalid number of gpio lines %u, using %u\n",
389159f8a02SLaurent Pinchart 			 p->config.number_of_pins, RCAR_MAX_GPIO_PER_BANK);
390159f8a02SLaurent Pinchart 		p->config.number_of_pins = RCAR_MAX_GPIO_PER_BANK;
391159f8a02SLaurent Pinchart 	}
392850dfe17SLaurent Pinchart 
393850dfe17SLaurent Pinchart 	return 0;
394159f8a02SLaurent Pinchart }
395159f8a02SLaurent Pinchart 
396119f5e44SMagnus Damm static int gpio_rcar_probe(struct platform_device *pdev)
397119f5e44SMagnus Damm {
398119f5e44SMagnus Damm 	struct gpio_rcar_priv *p;
399119f5e44SMagnus Damm 	struct resource *io, *irq;
400119f5e44SMagnus Damm 	struct gpio_chip *gpio_chip;
401119f5e44SMagnus Damm 	struct irq_chip *irq_chip;
402b22978fcSGeert Uytterhoeven 	struct device *dev = &pdev->dev;
403b22978fcSGeert Uytterhoeven 	const char *name = dev_name(dev);
404119f5e44SMagnus Damm 	int ret;
405119f5e44SMagnus Damm 
406b22978fcSGeert Uytterhoeven 	p = devm_kzalloc(dev, sizeof(*p), GFP_KERNEL);
4077d82bf34SGeert Uytterhoeven 	if (!p)
4087d82bf34SGeert Uytterhoeven 		return -ENOMEM;
409119f5e44SMagnus Damm 
410119f5e44SMagnus Damm 	p->pdev = pdev;
411119f5e44SMagnus Damm 	spin_lock_init(&p->lock);
412119f5e44SMagnus Damm 
413159f8a02SLaurent Pinchart 	/* Get device configuration from DT node or platform data. */
414850dfe17SLaurent Pinchart 	ret = gpio_rcar_parse_pdata(p);
415850dfe17SLaurent Pinchart 	if (ret < 0)
416850dfe17SLaurent Pinchart 		return ret;
417159f8a02SLaurent Pinchart 
418159f8a02SLaurent Pinchart 	platform_set_drvdata(pdev, p);
419159f8a02SLaurent Pinchart 
420ab82fa7dSGeert Uytterhoeven 	p->clk = devm_clk_get(dev, NULL);
421ab82fa7dSGeert Uytterhoeven 	if (IS_ERR(p->clk)) {
422ab82fa7dSGeert Uytterhoeven 		dev_warn(dev, "unable to get clock\n");
423ab82fa7dSGeert Uytterhoeven 		p->clk = NULL;
424ab82fa7dSGeert Uytterhoeven 	}
425ab82fa7dSGeert Uytterhoeven 
426df0c6c80SGeert Uytterhoeven 	pm_runtime_enable(dev);
427df0c6c80SGeert Uytterhoeven 
428119f5e44SMagnus Damm 	io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
429119f5e44SMagnus Damm 	irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
430119f5e44SMagnus Damm 
431119f5e44SMagnus Damm 	if (!io || !irq) {
432b22978fcSGeert Uytterhoeven 		dev_err(dev, "missing IRQ or IOMEM\n");
433119f5e44SMagnus Damm 		ret = -EINVAL;
434119f5e44SMagnus Damm 		goto err0;
435119f5e44SMagnus Damm 	}
436119f5e44SMagnus Damm 
437b22978fcSGeert Uytterhoeven 	p->base = devm_ioremap_nocache(dev, io->start, resource_size(io));
438119f5e44SMagnus Damm 	if (!p->base) {
439b22978fcSGeert Uytterhoeven 		dev_err(dev, "failed to remap I/O memory\n");
440119f5e44SMagnus Damm 		ret = -ENXIO;
441119f5e44SMagnus Damm 		goto err0;
442119f5e44SMagnus Damm 	}
443119f5e44SMagnus Damm 
444119f5e44SMagnus Damm 	gpio_chip = &p->gpio_chip;
445dc3465a9SLaurent Pinchart 	gpio_chip->request = gpio_rcar_request;
446dc3465a9SLaurent Pinchart 	gpio_chip->free = gpio_rcar_free;
447119f5e44SMagnus Damm 	gpio_chip->direction_input = gpio_rcar_direction_input;
448119f5e44SMagnus Damm 	gpio_chip->get = gpio_rcar_get;
449119f5e44SMagnus Damm 	gpio_chip->direction_output = gpio_rcar_direction_output;
450119f5e44SMagnus Damm 	gpio_chip->set = gpio_rcar_set;
451119f5e44SMagnus Damm 	gpio_chip->label = name;
452b22978fcSGeert Uytterhoeven 	gpio_chip->dev = dev;
453119f5e44SMagnus Damm 	gpio_chip->owner = THIS_MODULE;
454119f5e44SMagnus Damm 	gpio_chip->base = p->config.gpio_base;
455119f5e44SMagnus Damm 	gpio_chip->ngpio = p->config.number_of_pins;
456119f5e44SMagnus Damm 
457119f5e44SMagnus Damm 	irq_chip = &p->irq_chip;
458119f5e44SMagnus Damm 	irq_chip->name = name;
459119f5e44SMagnus Damm 	irq_chip->irq_mask = gpio_rcar_irq_disable;
460119f5e44SMagnus Damm 	irq_chip->irq_unmask = gpio_rcar_irq_enable;
461119f5e44SMagnus Damm 	irq_chip->irq_set_type = gpio_rcar_irq_set_type;
462ab82fa7dSGeert Uytterhoeven 	irq_chip->irq_set_wake = gpio_rcar_irq_set_wake;
463ab82fa7dSGeert Uytterhoeven 	irq_chip->flags	= IRQCHIP_SET_TYPE_MASKED | IRQCHIP_MASK_ON_SUSPEND;
464119f5e44SMagnus Damm 
465c7f3c5d3SGeert Uytterhoeven 	ret = gpiochip_add(gpio_chip);
466c7f3c5d3SGeert Uytterhoeven 	if (ret) {
467c7f3c5d3SGeert Uytterhoeven 		dev_err(dev, "failed to add GPIO controller\n");
4680c8aab8eSDan Carpenter 		goto err0;
469119f5e44SMagnus Damm 	}
470119f5e44SMagnus Damm 
4714d84b9e4SGeert Uytterhoeven 	ret = gpiochip_irqchip_add(gpio_chip, irq_chip, p->config.irq_base,
472c7f3c5d3SGeert Uytterhoeven 				   handle_level_irq, IRQ_TYPE_NONE);
473c7f3c5d3SGeert Uytterhoeven 	if (ret) {
474c7f3c5d3SGeert Uytterhoeven 		dev_err(dev, "cannot add irqchip\n");
475c7f3c5d3SGeert Uytterhoeven 		goto err1;
476c7f3c5d3SGeert Uytterhoeven 	}
477c7f3c5d3SGeert Uytterhoeven 
478ab82fa7dSGeert Uytterhoeven 	p->irq_parent = irq->start;
479b22978fcSGeert Uytterhoeven 	if (devm_request_irq(dev, irq->start, gpio_rcar_irq_handler,
480b22978fcSGeert Uytterhoeven 			     IRQF_SHARED, name, p)) {
481b22978fcSGeert Uytterhoeven 		dev_err(dev, "failed to request IRQ\n");
482119f5e44SMagnus Damm 		ret = -ENOENT;
483119f5e44SMagnus Damm 		goto err1;
484119f5e44SMagnus Damm 	}
485119f5e44SMagnus Damm 
486b22978fcSGeert Uytterhoeven 	dev_info(dev, "driving %d GPIOs\n", p->config.number_of_pins);
487119f5e44SMagnus Damm 
488119f5e44SMagnus Damm 	/* warn in case of mismatch if irq base is specified */
489119f5e44SMagnus Damm 	if (p->config.irq_base) {
4904d84b9e4SGeert Uytterhoeven 		ret = irq_find_mapping(gpio_chip->irqdomain, 0);
491119f5e44SMagnus Damm 		if (p->config.irq_base != ret)
492b22978fcSGeert Uytterhoeven 			dev_warn(dev, "irq base mismatch (%u/%u)\n",
493119f5e44SMagnus Damm 				 p->config.irq_base, ret);
494119f5e44SMagnus Damm 	}
495119f5e44SMagnus Damm 
496159f8a02SLaurent Pinchart 	if (p->config.pctl_name) {
497dc3465a9SLaurent Pinchart 		ret = gpiochip_add_pin_range(gpio_chip, p->config.pctl_name, 0,
498dc3465a9SLaurent Pinchart 					     gpio_chip->base, gpio_chip->ngpio);
499dc3465a9SLaurent Pinchart 		if (ret < 0)
500b22978fcSGeert Uytterhoeven 			dev_warn(dev, "failed to add pin range\n");
501159f8a02SLaurent Pinchart 	}
502dc3465a9SLaurent Pinchart 
503119f5e44SMagnus Damm 	return 0;
504119f5e44SMagnus Damm 
505119f5e44SMagnus Damm err1:
5064d84b9e4SGeert Uytterhoeven 	gpiochip_remove(gpio_chip);
507119f5e44SMagnus Damm err0:
508df0c6c80SGeert Uytterhoeven 	pm_runtime_disable(dev);
509119f5e44SMagnus Damm 	return ret;
510119f5e44SMagnus Damm }
511119f5e44SMagnus Damm 
512119f5e44SMagnus Damm static int gpio_rcar_remove(struct platform_device *pdev)
513119f5e44SMagnus Damm {
514119f5e44SMagnus Damm 	struct gpio_rcar_priv *p = platform_get_drvdata(pdev);
515119f5e44SMagnus Damm 
5169f5132aeSabdoulaye berthe 	gpiochip_remove(&p->gpio_chip);
517119f5e44SMagnus Damm 
518df0c6c80SGeert Uytterhoeven 	pm_runtime_disable(&pdev->dev);
519119f5e44SMagnus Damm 	return 0;
520119f5e44SMagnus Damm }
521119f5e44SMagnus Damm 
522119f5e44SMagnus Damm static struct platform_driver gpio_rcar_device_driver = {
523119f5e44SMagnus Damm 	.probe		= gpio_rcar_probe,
524119f5e44SMagnus Damm 	.remove		= gpio_rcar_remove,
525119f5e44SMagnus Damm 	.driver		= {
526119f5e44SMagnus Damm 		.name	= "gpio_rcar",
527159f8a02SLaurent Pinchart 		.of_match_table = of_match_ptr(gpio_rcar_of_table),
528119f5e44SMagnus Damm 	}
529119f5e44SMagnus Damm };
530119f5e44SMagnus Damm 
531119f5e44SMagnus Damm module_platform_driver(gpio_rcar_device_driver);
532119f5e44SMagnus Damm 
533119f5e44SMagnus Damm MODULE_AUTHOR("Magnus Damm");
534119f5e44SMagnus Damm MODULE_DESCRIPTION("Renesas R-Car GPIO Driver");
535119f5e44SMagnus Damm MODULE_LICENSE("GPL v2");
536