xref: /openbmc/linux/drivers/gpio/gpio-rcar.c (revision b470cef1)
18b37eb74SKuninori Morimoto // SPDX-License-Identifier: GPL-2.0
2119f5e44SMagnus Damm /*
3119f5e44SMagnus Damm  * Renesas R-Car GPIO Support
4119f5e44SMagnus Damm  *
51fd2b49dSHisashi Nakamura  *  Copyright (C) 2014 Renesas Electronics Corporation
6119f5e44SMagnus Damm  *  Copyright (C) 2013 Magnus Damm
7119f5e44SMagnus Damm  */
8119f5e44SMagnus Damm 
9119f5e44SMagnus Damm #include <linux/err.h>
104b1d8007SLinus Walleij #include <linux/gpio/driver.h>
11119f5e44SMagnus Damm #include <linux/init.h>
12119f5e44SMagnus Damm #include <linux/interrupt.h>
13119f5e44SMagnus Damm #include <linux/io.h>
14119f5e44SMagnus Damm #include <linux/ioport.h>
15119f5e44SMagnus Damm #include <linux/irq.h>
16119f5e44SMagnus Damm #include <linux/module.h>
17bd0bf468SSachin Kamat #include <linux/of.h>
18f9f2a6feSGeert Uytterhoeven #include <linux/of_device.h>
19dc3465a9SLaurent Pinchart #include <linux/pinctrl/consumer.h>
20119f5e44SMagnus Damm #include <linux/platform_device.h>
21df0c6c80SGeert Uytterhoeven #include <linux/pm_runtime.h>
22119f5e44SMagnus Damm #include <linux/spinlock.h>
23119f5e44SMagnus Damm #include <linux/slab.h>
24119f5e44SMagnus Damm 
2551750fb1SHien Dang struct gpio_rcar_bank_info {
2651750fb1SHien Dang 	u32 iointsel;
2751750fb1SHien Dang 	u32 inoutsel;
2851750fb1SHien Dang 	u32 outdt;
2951750fb1SHien Dang 	u32 posneg;
3051750fb1SHien Dang 	u32 edglevel;
3151750fb1SHien Dang 	u32 bothedge;
3251750fb1SHien Dang 	u32 intmsk;
3351750fb1SHien Dang };
3451750fb1SHien Dang 
35119f5e44SMagnus Damm struct gpio_rcar_priv {
36119f5e44SMagnus Damm 	void __iomem *base;
37119f5e44SMagnus Damm 	spinlock_t lock;
38a53f7953SVladimir Zapolskiy 	struct device *dev;
39119f5e44SMagnus Damm 	struct gpio_chip gpio_chip;
40119f5e44SMagnus Damm 	struct irq_chip irq_chip;
418b092be9SGeert Uytterhoeven 	unsigned int irq_parent;
429ac79ba9SGeert Uytterhoeven 	atomic_t wakeup_path;
433ae4f3aaSVladimir Zapolskiy 	bool has_outdtsel;
448b092be9SGeert Uytterhoeven 	bool has_both_edge_trigger;
4551750fb1SHien Dang 	struct gpio_rcar_bank_info bank_info;
46119f5e44SMagnus Damm };
47119f5e44SMagnus Damm 
483dc1e685SGeert Uytterhoeven #define IOINTSEL 0x00	/* General IO/Interrupt Switching Register */
493dc1e685SGeert Uytterhoeven #define INOUTSEL 0x04	/* General Input/Output Switching Register */
503dc1e685SGeert Uytterhoeven #define OUTDT 0x08	/* General Output Register */
513dc1e685SGeert Uytterhoeven #define INDT 0x0c	/* General Input Register */
523dc1e685SGeert Uytterhoeven #define INTDT 0x10	/* Interrupt Display Register */
533dc1e685SGeert Uytterhoeven #define INTCLR 0x14	/* Interrupt Clear Register */
543dc1e685SGeert Uytterhoeven #define INTMSK 0x18	/* Interrupt Mask Register */
553dc1e685SGeert Uytterhoeven #define MSKCLR 0x1c	/* Interrupt Mask Clear Register */
563dc1e685SGeert Uytterhoeven #define POSNEG 0x20	/* Positive/Negative Logic Select Register */
573dc1e685SGeert Uytterhoeven #define EDGLEVEL 0x24	/* Edge/level Select Register */
583dc1e685SGeert Uytterhoeven #define FILONOFF 0x28	/* Chattering Prevention On/Off Register */
593ae4f3aaSVladimir Zapolskiy #define OUTDTSEL 0x40	/* Output Data Select Register */
603dc1e685SGeert Uytterhoeven #define BOTHEDGE 0x4c	/* One Edge/Both Edge Select Register */
61119f5e44SMagnus Damm 
62159f8a02SLaurent Pinchart #define RCAR_MAX_GPIO_PER_BANK		32
63159f8a02SLaurent Pinchart 
64119f5e44SMagnus Damm static inline u32 gpio_rcar_read(struct gpio_rcar_priv *p, int offs)
65119f5e44SMagnus Damm {
66119f5e44SMagnus Damm 	return ioread32(p->base + offs);
67119f5e44SMagnus Damm }
68119f5e44SMagnus Damm 
69119f5e44SMagnus Damm static inline void gpio_rcar_write(struct gpio_rcar_priv *p, int offs,
70119f5e44SMagnus Damm 				   u32 value)
71119f5e44SMagnus Damm {
72119f5e44SMagnus Damm 	iowrite32(value, p->base + offs);
73119f5e44SMagnus Damm }
74119f5e44SMagnus Damm 
75119f5e44SMagnus Damm static void gpio_rcar_modify_bit(struct gpio_rcar_priv *p, int offs,
76119f5e44SMagnus Damm 				 int bit, bool value)
77119f5e44SMagnus Damm {
78119f5e44SMagnus Damm 	u32 tmp = gpio_rcar_read(p, offs);
79119f5e44SMagnus Damm 
80119f5e44SMagnus Damm 	if (value)
81119f5e44SMagnus Damm 		tmp |= BIT(bit);
82119f5e44SMagnus Damm 	else
83119f5e44SMagnus Damm 		tmp &= ~BIT(bit);
84119f5e44SMagnus Damm 
85119f5e44SMagnus Damm 	gpio_rcar_write(p, offs, tmp);
86119f5e44SMagnus Damm }
87119f5e44SMagnus Damm 
88119f5e44SMagnus Damm static void gpio_rcar_irq_disable(struct irq_data *d)
89119f5e44SMagnus Damm {
90c7f3c5d3SGeert Uytterhoeven 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
91c7b6f457SLinus Walleij 	struct gpio_rcar_priv *p = gpiochip_get_data(gc);
92119f5e44SMagnus Damm 
93119f5e44SMagnus Damm 	gpio_rcar_write(p, INTMSK, ~BIT(irqd_to_hwirq(d)));
94119f5e44SMagnus Damm }
95119f5e44SMagnus Damm 
96119f5e44SMagnus Damm static void gpio_rcar_irq_enable(struct irq_data *d)
97119f5e44SMagnus Damm {
98c7f3c5d3SGeert Uytterhoeven 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
99c7b6f457SLinus Walleij 	struct gpio_rcar_priv *p = gpiochip_get_data(gc);
100119f5e44SMagnus Damm 
101119f5e44SMagnus Damm 	gpio_rcar_write(p, MSKCLR, BIT(irqd_to_hwirq(d)));
102119f5e44SMagnus Damm }
103119f5e44SMagnus Damm 
104119f5e44SMagnus Damm static void gpio_rcar_config_interrupt_input_mode(struct gpio_rcar_priv *p,
105119f5e44SMagnus Damm 						  unsigned int hwirq,
106119f5e44SMagnus Damm 						  bool active_high_rising_edge,
1077e1092b5SSimon Horman 						  bool level_trigger,
1087e1092b5SSimon Horman 						  bool both)
109119f5e44SMagnus Damm {
110119f5e44SMagnus Damm 	unsigned long flags;
111119f5e44SMagnus Damm 
112119f5e44SMagnus Damm 	/* follow steps in the GPIO documentation for
113119f5e44SMagnus Damm 	 * "Setting Edge-Sensitive Interrupt Input Mode" and
114119f5e44SMagnus Damm 	 * "Setting Level-Sensitive Interrupt Input Mode"
115119f5e44SMagnus Damm 	 */
116119f5e44SMagnus Damm 
117119f5e44SMagnus Damm 	spin_lock_irqsave(&p->lock, flags);
118119f5e44SMagnus Damm 
119b36368f6SAshish Chavan 	/* Configure positive or negative logic in POSNEG */
120119f5e44SMagnus Damm 	gpio_rcar_modify_bit(p, POSNEG, hwirq, !active_high_rising_edge);
121119f5e44SMagnus Damm 
122119f5e44SMagnus Damm 	/* Configure edge or level trigger in EDGLEVEL */
123119f5e44SMagnus Damm 	gpio_rcar_modify_bit(p, EDGLEVEL, hwirq, !level_trigger);
124119f5e44SMagnus Damm 
1257e1092b5SSimon Horman 	/* Select one edge or both edges in BOTHEDGE */
1268b092be9SGeert Uytterhoeven 	if (p->has_both_edge_trigger)
1277e1092b5SSimon Horman 		gpio_rcar_modify_bit(p, BOTHEDGE, hwirq, both);
1287e1092b5SSimon Horman 
129119f5e44SMagnus Damm 	/* Select "Interrupt Input Mode" in IOINTSEL */
130119f5e44SMagnus Damm 	gpio_rcar_modify_bit(p, IOINTSEL, hwirq, true);
131119f5e44SMagnus Damm 
132119f5e44SMagnus Damm 	/* Write INTCLR in case of edge trigger */
133119f5e44SMagnus Damm 	if (!level_trigger)
134119f5e44SMagnus Damm 		gpio_rcar_write(p, INTCLR, BIT(hwirq));
135119f5e44SMagnus Damm 
136119f5e44SMagnus Damm 	spin_unlock_irqrestore(&p->lock, flags);
137119f5e44SMagnus Damm }
138119f5e44SMagnus Damm 
139119f5e44SMagnus Damm static int gpio_rcar_irq_set_type(struct irq_data *d, unsigned int type)
140119f5e44SMagnus Damm {
141c7f3c5d3SGeert Uytterhoeven 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
142c7b6f457SLinus Walleij 	struct gpio_rcar_priv *p = gpiochip_get_data(gc);
143119f5e44SMagnus Damm 	unsigned int hwirq = irqd_to_hwirq(d);
144119f5e44SMagnus Damm 
145a53f7953SVladimir Zapolskiy 	dev_dbg(p->dev, "sense irq = %d, type = %d\n", hwirq, type);
146119f5e44SMagnus Damm 
147119f5e44SMagnus Damm 	switch (type & IRQ_TYPE_SENSE_MASK) {
148119f5e44SMagnus Damm 	case IRQ_TYPE_LEVEL_HIGH:
1497e1092b5SSimon Horman 		gpio_rcar_config_interrupt_input_mode(p, hwirq, true, true,
1507e1092b5SSimon Horman 						      false);
151119f5e44SMagnus Damm 		break;
152119f5e44SMagnus Damm 	case IRQ_TYPE_LEVEL_LOW:
1537e1092b5SSimon Horman 		gpio_rcar_config_interrupt_input_mode(p, hwirq, false, true,
1547e1092b5SSimon Horman 						      false);
155119f5e44SMagnus Damm 		break;
156119f5e44SMagnus Damm 	case IRQ_TYPE_EDGE_RISING:
1577e1092b5SSimon Horman 		gpio_rcar_config_interrupt_input_mode(p, hwirq, true, false,
1587e1092b5SSimon Horman 						      false);
159119f5e44SMagnus Damm 		break;
160119f5e44SMagnus Damm 	case IRQ_TYPE_EDGE_FALLING:
1617e1092b5SSimon Horman 		gpio_rcar_config_interrupt_input_mode(p, hwirq, false, false,
1627e1092b5SSimon Horman 						      false);
1637e1092b5SSimon Horman 		break;
1647e1092b5SSimon Horman 	case IRQ_TYPE_EDGE_BOTH:
1658b092be9SGeert Uytterhoeven 		if (!p->has_both_edge_trigger)
1667e1092b5SSimon Horman 			return -EINVAL;
1677e1092b5SSimon Horman 		gpio_rcar_config_interrupt_input_mode(p, hwirq, true, false,
1687e1092b5SSimon Horman 						      true);
169119f5e44SMagnus Damm 		break;
170119f5e44SMagnus Damm 	default:
171119f5e44SMagnus Damm 		return -EINVAL;
172119f5e44SMagnus Damm 	}
173119f5e44SMagnus Damm 	return 0;
174119f5e44SMagnus Damm }
175119f5e44SMagnus Damm 
176ab82fa7dSGeert Uytterhoeven static int gpio_rcar_irq_set_wake(struct irq_data *d, unsigned int on)
177ab82fa7dSGeert Uytterhoeven {
178ab82fa7dSGeert Uytterhoeven 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
179c7b6f457SLinus Walleij 	struct gpio_rcar_priv *p = gpiochip_get_data(gc);
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) {
185a53f7953SVladimir Zapolskiy 			dev_dbg(p->dev, "irq %u doesn't support irq_set_wake\n",
186501ef0f9SGeert Uytterhoeven 				p->irq_parent);
187501ef0f9SGeert Uytterhoeven 			p->irq_parent = 0;
188501ef0f9SGeert Uytterhoeven 		}
189501ef0f9SGeert Uytterhoeven 	}
190ab82fa7dSGeert Uytterhoeven 
191ab82fa7dSGeert Uytterhoeven 	if (on)
1929ac79ba9SGeert Uytterhoeven 		atomic_inc(&p->wakeup_path);
193ab82fa7dSGeert Uytterhoeven 	else
1949ac79ba9SGeert Uytterhoeven 		atomic_dec(&p->wakeup_path);
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));
209f0fbe7bcSThierry Reding 		generic_handle_irq(irq_find_mapping(p->gpio_chip.irq.domain,
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 
231b36368f6SAshish Chavan 	/* Configure positive 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 
2403ae4f3aaSVladimir Zapolskiy 	/* Select General Output Register to output data in OUTDTSEL */
2413ae4f3aaSVladimir Zapolskiy 	if (p->has_outdtsel && output)
2423ae4f3aaSVladimir Zapolskiy 		gpio_rcar_modify_bit(p, OUTDTSEL, gpio, false);
2433ae4f3aaSVladimir Zapolskiy 
244119f5e44SMagnus Damm 	spin_unlock_irqrestore(&p->lock, flags);
245119f5e44SMagnus Damm }
246119f5e44SMagnus Damm 
247dc3465a9SLaurent Pinchart static int gpio_rcar_request(struct gpio_chip *chip, unsigned offset)
248dc3465a9SLaurent Pinchart {
2492d65472bSGeert Uytterhoeven 	struct gpio_rcar_priv *p = gpiochip_get_data(chip);
2502d65472bSGeert Uytterhoeven 	int error;
2512d65472bSGeert Uytterhoeven 
252a53f7953SVladimir Zapolskiy 	error = pm_runtime_get_sync(p->dev);
2536f8cd246SDinghao Liu 	if (error < 0) {
2546f8cd246SDinghao Liu 		pm_runtime_put(p->dev);
2552d65472bSGeert Uytterhoeven 		return error;
2566f8cd246SDinghao Liu 	}
2572d65472bSGeert Uytterhoeven 
258a9a1d2a7SLinus Walleij 	error = pinctrl_gpio_request(chip->base + offset);
2592d65472bSGeert Uytterhoeven 	if (error)
260a53f7953SVladimir Zapolskiy 		pm_runtime_put(p->dev);
2612d65472bSGeert Uytterhoeven 
2622d65472bSGeert Uytterhoeven 	return error;
263dc3465a9SLaurent Pinchart }
264dc3465a9SLaurent Pinchart 
265dc3465a9SLaurent Pinchart static void gpio_rcar_free(struct gpio_chip *chip, unsigned offset)
266dc3465a9SLaurent Pinchart {
2672d65472bSGeert Uytterhoeven 	struct gpio_rcar_priv *p = gpiochip_get_data(chip);
2682d65472bSGeert Uytterhoeven 
269a9a1d2a7SLinus Walleij 	pinctrl_gpio_free(chip->base + offset);
270dc3465a9SLaurent Pinchart 
271ce0e2c60SLinus Walleij 	/*
272ce0e2c60SLinus Walleij 	 * Set the GPIO as an input to ensure that the next GPIO request won't
273dc3465a9SLaurent Pinchart 	 * drive the GPIO pin as an output.
274dc3465a9SLaurent Pinchart 	 */
275dc3465a9SLaurent Pinchart 	gpio_rcar_config_general_input_output_mode(chip, offset, false);
2762d65472bSGeert Uytterhoeven 
277a53f7953SVladimir Zapolskiy 	pm_runtime_put(p->dev);
278dc3465a9SLaurent Pinchart }
279dc3465a9SLaurent Pinchart 
280ad817297SGeert Uytterhoeven static int gpio_rcar_get_direction(struct gpio_chip *chip, unsigned int offset)
281ad817297SGeert Uytterhoeven {
282ad817297SGeert Uytterhoeven 	struct gpio_rcar_priv *p = gpiochip_get_data(chip);
283ad817297SGeert Uytterhoeven 
284e42615ecSMatti Vaittinen 	if (gpio_rcar_read(p, INOUTSEL) & BIT(offset))
285e42615ecSMatti Vaittinen 		return GPIO_LINE_DIRECTION_OUT;
286e42615ecSMatti Vaittinen 
287e42615ecSMatti Vaittinen 	return GPIO_LINE_DIRECTION_IN;
288ad817297SGeert Uytterhoeven }
289ad817297SGeert Uytterhoeven 
290119f5e44SMagnus Damm static int gpio_rcar_direction_input(struct gpio_chip *chip, unsigned offset)
291119f5e44SMagnus Damm {
292119f5e44SMagnus Damm 	gpio_rcar_config_general_input_output_mode(chip, offset, false);
293119f5e44SMagnus Damm 	return 0;
294119f5e44SMagnus Damm }
295119f5e44SMagnus Damm 
296119f5e44SMagnus Damm static int gpio_rcar_get(struct gpio_chip *chip, unsigned offset)
297119f5e44SMagnus Damm {
298ae9550f6SMagnus Damm 	u32 bit = BIT(offset);
299ae9550f6SMagnus Damm 
300ae9550f6SMagnus Damm 	/* testing on r8a7790 shows that INDT does not show correct pin state
301ae9550f6SMagnus Damm 	 * when configured as output, so use OUTDT in case of output pins */
302c7b6f457SLinus Walleij 	if (gpio_rcar_read(gpiochip_get_data(chip), INOUTSEL) & bit)
303c7b6f457SLinus Walleij 		return !!(gpio_rcar_read(gpiochip_get_data(chip), OUTDT) & bit);
304ae9550f6SMagnus Damm 	else
305c7b6f457SLinus Walleij 		return !!(gpio_rcar_read(gpiochip_get_data(chip), INDT) & bit);
306119f5e44SMagnus Damm }
307119f5e44SMagnus Damm 
308119f5e44SMagnus Damm static void gpio_rcar_set(struct gpio_chip *chip, unsigned offset, int value)
309119f5e44SMagnus Damm {
310c7b6f457SLinus Walleij 	struct gpio_rcar_priv *p = gpiochip_get_data(chip);
311119f5e44SMagnus Damm 	unsigned long flags;
312119f5e44SMagnus Damm 
313119f5e44SMagnus Damm 	spin_lock_irqsave(&p->lock, flags);
314119f5e44SMagnus Damm 	gpio_rcar_modify_bit(p, OUTDT, offset, value);
315119f5e44SMagnus Damm 	spin_unlock_irqrestore(&p->lock, flags);
316119f5e44SMagnus Damm }
317119f5e44SMagnus Damm 
318dbb763b8SGeert Uytterhoeven static void gpio_rcar_set_multiple(struct gpio_chip *chip, unsigned long *mask,
319dbb763b8SGeert Uytterhoeven 				   unsigned long *bits)
320dbb763b8SGeert Uytterhoeven {
321dbb763b8SGeert Uytterhoeven 	struct gpio_rcar_priv *p = gpiochip_get_data(chip);
322dbb763b8SGeert Uytterhoeven 	unsigned long flags;
323dbb763b8SGeert Uytterhoeven 	u32 val, bankmask;
324dbb763b8SGeert Uytterhoeven 
325dbb763b8SGeert Uytterhoeven 	bankmask = mask[0] & GENMASK(chip->ngpio - 1, 0);
326496069b8SBiju Das 	if (chip->valid_mask)
327496069b8SBiju Das 		bankmask &= chip->valid_mask[0];
328496069b8SBiju Das 
329dbb763b8SGeert Uytterhoeven 	if (!bankmask)
330dbb763b8SGeert Uytterhoeven 		return;
331dbb763b8SGeert Uytterhoeven 
332dbb763b8SGeert Uytterhoeven 	spin_lock_irqsave(&p->lock, flags);
333dbb763b8SGeert Uytterhoeven 	val = gpio_rcar_read(p, OUTDT);
334dbb763b8SGeert Uytterhoeven 	val &= ~bankmask;
335dbb763b8SGeert Uytterhoeven 	val |= (bankmask & bits[0]);
336dbb763b8SGeert Uytterhoeven 	gpio_rcar_write(p, OUTDT, val);
337dbb763b8SGeert Uytterhoeven 	spin_unlock_irqrestore(&p->lock, flags);
338dbb763b8SGeert Uytterhoeven }
339dbb763b8SGeert Uytterhoeven 
340119f5e44SMagnus Damm static int gpio_rcar_direction_output(struct gpio_chip *chip, unsigned offset,
341119f5e44SMagnus Damm 				      int value)
342119f5e44SMagnus Damm {
343119f5e44SMagnus Damm 	/* write GPIO value to output before selecting output mode of pin */
344119f5e44SMagnus Damm 	gpio_rcar_set(chip, offset, value);
345119f5e44SMagnus Damm 	gpio_rcar_config_general_input_output_mode(chip, offset, true);
346119f5e44SMagnus Damm 	return 0;
347119f5e44SMagnus Damm }
348119f5e44SMagnus Damm 
349850dfe17SLaurent Pinchart struct gpio_rcar_info {
3503ae4f3aaSVladimir Zapolskiy 	bool has_outdtsel;
351850dfe17SLaurent Pinchart 	bool has_both_edge_trigger;
352850dfe17SLaurent Pinchart };
353850dfe17SLaurent Pinchart 
3541fd2b49dSHisashi Nakamura static const struct gpio_rcar_info gpio_rcar_info_gen1 = {
3553ae4f3aaSVladimir Zapolskiy 	.has_outdtsel = false,
3561fd2b49dSHisashi Nakamura 	.has_both_edge_trigger = false,
3571fd2b49dSHisashi Nakamura };
3581fd2b49dSHisashi Nakamura 
3591fd2b49dSHisashi Nakamura static const struct gpio_rcar_info gpio_rcar_info_gen2 = {
3603ae4f3aaSVladimir Zapolskiy 	.has_outdtsel = true,
3611fd2b49dSHisashi Nakamura 	.has_both_edge_trigger = true,
3621fd2b49dSHisashi Nakamura };
3631fd2b49dSHisashi Nakamura 
364850dfe17SLaurent Pinchart static const struct of_device_id gpio_rcar_of_table[] = {
365850dfe17SLaurent Pinchart 	{
36685bb4646SBiju Das 		.compatible = "renesas,gpio-r8a7743",
36785bb4646SBiju Das 		/* RZ/G1 GPIO is identical to R-Car Gen2. */
36885bb4646SBiju Das 		.data = &gpio_rcar_info_gen2,
36985bb4646SBiju Das 	}, {
370850dfe17SLaurent Pinchart 		.compatible = "renesas,gpio-r8a7790",
3711fd2b49dSHisashi Nakamura 		.data = &gpio_rcar_info_gen2,
372850dfe17SLaurent Pinchart 	}, {
373850dfe17SLaurent Pinchart 		.compatible = "renesas,gpio-r8a7791",
3741fd2b49dSHisashi Nakamura 		.data = &gpio_rcar_info_gen2,
3751fd2b49dSHisashi Nakamura 	}, {
376e79c5830SSergei Shtylyov 		.compatible = "renesas,gpio-r8a7792",
377e79c5830SSergei Shtylyov 		.data = &gpio_rcar_info_gen2,
378e79c5830SSergei Shtylyov 	}, {
3791fd2b49dSHisashi Nakamura 		.compatible = "renesas,gpio-r8a7793",
3801fd2b49dSHisashi Nakamura 		.data = &gpio_rcar_info_gen2,
3811fd2b49dSHisashi Nakamura 	}, {
3821fd2b49dSHisashi Nakamura 		.compatible = "renesas,gpio-r8a7794",
3831fd2b49dSHisashi Nakamura 		.data = &gpio_rcar_info_gen2,
384850dfe17SLaurent Pinchart 	}, {
3858cd14702SUlrich Hecht 		.compatible = "renesas,gpio-r8a7795",
3868cd14702SUlrich Hecht 		/* Gen3 GPIO is identical to Gen2. */
3878cd14702SUlrich Hecht 		.data = &gpio_rcar_info_gen2,
3888cd14702SUlrich Hecht 	}, {
3895d2f1d6eSSimon Horman 		.compatible = "renesas,gpio-r8a7796",
3905d2f1d6eSSimon Horman 		/* Gen3 GPIO is identical to Gen2. */
3915d2f1d6eSSimon Horman 		.data = &gpio_rcar_info_gen2,
3925d2f1d6eSSimon Horman 	}, {
393dbd1dad2SSimon Horman 		.compatible = "renesas,rcar-gen1-gpio",
394dbd1dad2SSimon Horman 		.data = &gpio_rcar_info_gen1,
395dbd1dad2SSimon Horman 	}, {
396dbd1dad2SSimon Horman 		.compatible = "renesas,rcar-gen2-gpio",
397dbd1dad2SSimon Horman 		.data = &gpio_rcar_info_gen2,
398dbd1dad2SSimon Horman 	}, {
399dbd1dad2SSimon Horman 		.compatible = "renesas,rcar-gen3-gpio",
400dbd1dad2SSimon Horman 		/* Gen3 GPIO is identical to Gen2. */
401dbd1dad2SSimon Horman 		.data = &gpio_rcar_info_gen2,
402dbd1dad2SSimon Horman 	}, {
403850dfe17SLaurent Pinchart 		.compatible = "renesas,gpio-rcar",
4041fd2b49dSHisashi Nakamura 		.data = &gpio_rcar_info_gen1,
405850dfe17SLaurent Pinchart 	}, {
406850dfe17SLaurent Pinchart 		/* Terminator */
407850dfe17SLaurent Pinchart 	},
408850dfe17SLaurent Pinchart };
409850dfe17SLaurent Pinchart 
410850dfe17SLaurent Pinchart MODULE_DEVICE_TABLE(of, gpio_rcar_of_table);
411850dfe17SLaurent Pinchart 
4128b092be9SGeert Uytterhoeven static int gpio_rcar_parse_dt(struct gpio_rcar_priv *p, unsigned int *npins)
413159f8a02SLaurent Pinchart {
414a53f7953SVladimir Zapolskiy 	struct device_node *np = p->dev->of_node;
415850dfe17SLaurent Pinchart 	const struct gpio_rcar_info *info;
4168b092be9SGeert Uytterhoeven 	struct of_phandle_args args;
4178b092be9SGeert Uytterhoeven 	int ret;
418850dfe17SLaurent Pinchart 
419a53f7953SVladimir Zapolskiy 	info = of_device_get_match_data(p->dev);
4203ae4f3aaSVladimir Zapolskiy 	p->has_outdtsel = info->has_outdtsel;
4213ae4f3aaSVladimir Zapolskiy 	p->has_both_edge_trigger = info->has_both_edge_trigger;
422850dfe17SLaurent Pinchart 
4238b092be9SGeert Uytterhoeven 	ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3, 0, &args);
4248b092be9SGeert Uytterhoeven 	*npins = ret == 0 ? args.args[2] : RCAR_MAX_GPIO_PER_BANK;
425159f8a02SLaurent Pinchart 
4268b092be9SGeert Uytterhoeven 	if (*npins == 0 || *npins > RCAR_MAX_GPIO_PER_BANK) {
427a53f7953SVladimir Zapolskiy 		dev_warn(p->dev, "Invalid number of gpio lines %u, using %u\n",
428a53f7953SVladimir Zapolskiy 			 *npins, RCAR_MAX_GPIO_PER_BANK);
4298b092be9SGeert Uytterhoeven 		*npins = RCAR_MAX_GPIO_PER_BANK;
430159f8a02SLaurent Pinchart 	}
431850dfe17SLaurent Pinchart 
432850dfe17SLaurent Pinchart 	return 0;
433159f8a02SLaurent Pinchart }
434159f8a02SLaurent Pinchart 
435119f5e44SMagnus Damm static int gpio_rcar_probe(struct platform_device *pdev)
436119f5e44SMagnus Damm {
437119f5e44SMagnus Damm 	struct gpio_rcar_priv *p;
438ecbf7c2eSEnrico Weigelt, metux IT consult 	struct resource *irq;
439119f5e44SMagnus Damm 	struct gpio_chip *gpio_chip;
440119f5e44SMagnus Damm 	struct irq_chip *irq_chip;
441b470cef1SLinus Walleij 	struct gpio_irq_chip *girq;
442b22978fcSGeert Uytterhoeven 	struct device *dev = &pdev->dev;
443b22978fcSGeert Uytterhoeven 	const char *name = dev_name(dev);
4448b092be9SGeert Uytterhoeven 	unsigned int npins;
445119f5e44SMagnus Damm 	int ret;
446119f5e44SMagnus Damm 
447b22978fcSGeert Uytterhoeven 	p = devm_kzalloc(dev, sizeof(*p), GFP_KERNEL);
4487d82bf34SGeert Uytterhoeven 	if (!p)
4497d82bf34SGeert Uytterhoeven 		return -ENOMEM;
450119f5e44SMagnus Damm 
451a53f7953SVladimir Zapolskiy 	p->dev = dev;
452119f5e44SMagnus Damm 	spin_lock_init(&p->lock);
453119f5e44SMagnus Damm 
4548b092be9SGeert Uytterhoeven 	/* Get device configuration from DT node */
4558b092be9SGeert Uytterhoeven 	ret = gpio_rcar_parse_dt(p, &npins);
456850dfe17SLaurent Pinchart 	if (ret < 0)
457850dfe17SLaurent Pinchart 		return ret;
458159f8a02SLaurent Pinchart 
459159f8a02SLaurent Pinchart 	platform_set_drvdata(pdev, p);
460159f8a02SLaurent Pinchart 
461df0c6c80SGeert Uytterhoeven 	pm_runtime_enable(dev);
462df0c6c80SGeert Uytterhoeven 
463119f5e44SMagnus Damm 	irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
4645a24d4b6SSergei Shtylyov 	if (!irq) {
4655a24d4b6SSergei Shtylyov 		dev_err(dev, "missing IRQ\n");
466119f5e44SMagnus Damm 		ret = -EINVAL;
467119f5e44SMagnus Damm 		goto err0;
468119f5e44SMagnus Damm 	}
469119f5e44SMagnus Damm 
470ecbf7c2eSEnrico Weigelt, metux IT consult 	p->base = devm_platform_ioremap_resource(pdev, 0);
4715a24d4b6SSergei Shtylyov 	if (IS_ERR(p->base)) {
4725a24d4b6SSergei Shtylyov 		ret = PTR_ERR(p->base);
473119f5e44SMagnus Damm 		goto err0;
474119f5e44SMagnus Damm 	}
475119f5e44SMagnus Damm 
476119f5e44SMagnus Damm 	gpio_chip = &p->gpio_chip;
477dc3465a9SLaurent Pinchart 	gpio_chip->request = gpio_rcar_request;
478dc3465a9SLaurent Pinchart 	gpio_chip->free = gpio_rcar_free;
479ad817297SGeert Uytterhoeven 	gpio_chip->get_direction = gpio_rcar_get_direction;
480119f5e44SMagnus Damm 	gpio_chip->direction_input = gpio_rcar_direction_input;
481119f5e44SMagnus Damm 	gpio_chip->get = gpio_rcar_get;
482119f5e44SMagnus Damm 	gpio_chip->direction_output = gpio_rcar_direction_output;
483119f5e44SMagnus Damm 	gpio_chip->set = gpio_rcar_set;
484dbb763b8SGeert Uytterhoeven 	gpio_chip->set_multiple = gpio_rcar_set_multiple;
485119f5e44SMagnus Damm 	gpio_chip->label = name;
48658383c78SLinus Walleij 	gpio_chip->parent = dev;
487119f5e44SMagnus Damm 	gpio_chip->owner = THIS_MODULE;
4888b092be9SGeert Uytterhoeven 	gpio_chip->base = -1;
4898b092be9SGeert Uytterhoeven 	gpio_chip->ngpio = npins;
490119f5e44SMagnus Damm 
491119f5e44SMagnus Damm 	irq_chip = &p->irq_chip;
492f932a686SGeert Uytterhoeven 	irq_chip->name = "gpio-rcar";
49347bd38a3SNiklas Söderlund 	irq_chip->parent_device = dev;
494119f5e44SMagnus Damm 	irq_chip->irq_mask = gpio_rcar_irq_disable;
495119f5e44SMagnus Damm 	irq_chip->irq_unmask = gpio_rcar_irq_enable;
496119f5e44SMagnus Damm 	irq_chip->irq_set_type = gpio_rcar_irq_set_type;
497ab82fa7dSGeert Uytterhoeven 	irq_chip->irq_set_wake = gpio_rcar_irq_set_wake;
498ab82fa7dSGeert Uytterhoeven 	irq_chip->flags = IRQCHIP_SET_TYPE_MASKED | IRQCHIP_MASK_ON_SUSPEND;
499119f5e44SMagnus Damm 
500b470cef1SLinus Walleij 	girq = &gpio_chip->irq;
501b470cef1SLinus Walleij 	girq->chip = irq_chip;
502b470cef1SLinus Walleij 	/* This will let us handle the parent IRQ in the driver */
503b470cef1SLinus Walleij 	girq->parent_handler = NULL;
504b470cef1SLinus Walleij 	girq->num_parents = 0;
505b470cef1SLinus Walleij 	girq->parents = NULL;
506b470cef1SLinus Walleij 	girq->default_type = IRQ_TYPE_NONE;
507b470cef1SLinus Walleij 	girq->handler = handle_level_irq;
508b470cef1SLinus Walleij 
509c7b6f457SLinus Walleij 	ret = gpiochip_add_data(gpio_chip, p);
510c7f3c5d3SGeert Uytterhoeven 	if (ret) {
511c7f3c5d3SGeert Uytterhoeven 		dev_err(dev, "failed to add GPIO controller\n");
5120c8aab8eSDan Carpenter 		goto err0;
513119f5e44SMagnus Damm 	}
514119f5e44SMagnus Damm 
515ab82fa7dSGeert Uytterhoeven 	p->irq_parent = irq->start;
516b22978fcSGeert Uytterhoeven 	if (devm_request_irq(dev, irq->start, gpio_rcar_irq_handler,
517b22978fcSGeert Uytterhoeven 			     IRQF_SHARED, name, p)) {
518b22978fcSGeert Uytterhoeven 		dev_err(dev, "failed to request IRQ\n");
519119f5e44SMagnus Damm 		ret = -ENOENT;
520119f5e44SMagnus Damm 		goto err1;
521119f5e44SMagnus Damm 	}
522119f5e44SMagnus Damm 
5238b092be9SGeert Uytterhoeven 	dev_info(dev, "driving %d GPIOs\n", npins);
524dc3465a9SLaurent Pinchart 
525119f5e44SMagnus Damm 	return 0;
526119f5e44SMagnus Damm 
527119f5e44SMagnus Damm err1:
5284d84b9e4SGeert Uytterhoeven 	gpiochip_remove(gpio_chip);
529119f5e44SMagnus Damm err0:
530df0c6c80SGeert Uytterhoeven 	pm_runtime_disable(dev);
531119f5e44SMagnus Damm 	return ret;
532119f5e44SMagnus Damm }
533119f5e44SMagnus Damm 
534119f5e44SMagnus Damm static int gpio_rcar_remove(struct platform_device *pdev)
535119f5e44SMagnus Damm {
536119f5e44SMagnus Damm 	struct gpio_rcar_priv *p = platform_get_drvdata(pdev);
537119f5e44SMagnus Damm 
5389f5132aeSabdoulaye berthe 	gpiochip_remove(&p->gpio_chip);
539119f5e44SMagnus Damm 
540df0c6c80SGeert Uytterhoeven 	pm_runtime_disable(&pdev->dev);
541119f5e44SMagnus Damm 	return 0;
542119f5e44SMagnus Damm }
543119f5e44SMagnus Damm 
54451750fb1SHien Dang #ifdef CONFIG_PM_SLEEP
54551750fb1SHien Dang static int gpio_rcar_suspend(struct device *dev)
54651750fb1SHien Dang {
54751750fb1SHien Dang 	struct gpio_rcar_priv *p = dev_get_drvdata(dev);
54851750fb1SHien Dang 
54951750fb1SHien Dang 	p->bank_info.iointsel = gpio_rcar_read(p, IOINTSEL);
55051750fb1SHien Dang 	p->bank_info.inoutsel = gpio_rcar_read(p, INOUTSEL);
55151750fb1SHien Dang 	p->bank_info.outdt = gpio_rcar_read(p, OUTDT);
55251750fb1SHien Dang 	p->bank_info.intmsk = gpio_rcar_read(p, INTMSK);
55351750fb1SHien Dang 	p->bank_info.posneg = gpio_rcar_read(p, POSNEG);
55451750fb1SHien Dang 	p->bank_info.edglevel = gpio_rcar_read(p, EDGLEVEL);
55551750fb1SHien Dang 	if (p->has_both_edge_trigger)
55651750fb1SHien Dang 		p->bank_info.bothedge = gpio_rcar_read(p, BOTHEDGE);
55751750fb1SHien Dang 
5589ac79ba9SGeert Uytterhoeven 	if (atomic_read(&p->wakeup_path))
5599ac79ba9SGeert Uytterhoeven 		device_set_wakeup_path(dev);
5609ac79ba9SGeert Uytterhoeven 
56151750fb1SHien Dang 	return 0;
56251750fb1SHien Dang }
56351750fb1SHien Dang 
56451750fb1SHien Dang static int gpio_rcar_resume(struct device *dev)
56551750fb1SHien Dang {
56651750fb1SHien Dang 	struct gpio_rcar_priv *p = dev_get_drvdata(dev);
56751750fb1SHien Dang 	unsigned int offset;
56851750fb1SHien Dang 	u32 mask;
56951750fb1SHien Dang 
57051750fb1SHien Dang 	for (offset = 0; offset < p->gpio_chip.ngpio; offset++) {
571496069b8SBiju Das 		if (!gpiochip_line_is_valid(&p->gpio_chip, offset))
572496069b8SBiju Das 			continue;
573496069b8SBiju Das 
57451750fb1SHien Dang 		mask = BIT(offset);
57551750fb1SHien Dang 		/* I/O pin */
57651750fb1SHien Dang 		if (!(p->bank_info.iointsel & mask)) {
57751750fb1SHien Dang 			if (p->bank_info.inoutsel & mask)
57851750fb1SHien Dang 				gpio_rcar_direction_output(
57951750fb1SHien Dang 					&p->gpio_chip, offset,
58051750fb1SHien Dang 					!!(p->bank_info.outdt & mask));
58151750fb1SHien Dang 			else
58251750fb1SHien Dang 				gpio_rcar_direction_input(&p->gpio_chip,
58351750fb1SHien Dang 							  offset);
58451750fb1SHien Dang 		} else {
58551750fb1SHien Dang 			/* Interrupt pin */
58651750fb1SHien Dang 			gpio_rcar_config_interrupt_input_mode(
58751750fb1SHien Dang 				p,
58851750fb1SHien Dang 				offset,
58951750fb1SHien Dang 				!(p->bank_info.posneg & mask),
59051750fb1SHien Dang 				!(p->bank_info.edglevel & mask),
59151750fb1SHien Dang 				!!(p->bank_info.bothedge & mask));
59251750fb1SHien Dang 
59351750fb1SHien Dang 			if (p->bank_info.intmsk & mask)
59451750fb1SHien Dang 				gpio_rcar_write(p, MSKCLR, mask);
59551750fb1SHien Dang 		}
59651750fb1SHien Dang 	}
59751750fb1SHien Dang 
59851750fb1SHien Dang 	return 0;
59951750fb1SHien Dang }
60051750fb1SHien Dang #endif /* CONFIG_PM_SLEEP*/
60151750fb1SHien Dang 
60251750fb1SHien Dang static SIMPLE_DEV_PM_OPS(gpio_rcar_pm_ops, gpio_rcar_suspend, gpio_rcar_resume);
60351750fb1SHien Dang 
604119f5e44SMagnus Damm static struct platform_driver gpio_rcar_device_driver = {
605119f5e44SMagnus Damm 	.probe		= gpio_rcar_probe,
606119f5e44SMagnus Damm 	.remove		= gpio_rcar_remove,
607119f5e44SMagnus Damm 	.driver		= {
608119f5e44SMagnus Damm 		.name	= "gpio_rcar",
60951750fb1SHien Dang 		.pm     = &gpio_rcar_pm_ops,
610159f8a02SLaurent Pinchart 		.of_match_table = of_match_ptr(gpio_rcar_of_table),
611119f5e44SMagnus Damm 	}
612119f5e44SMagnus Damm };
613119f5e44SMagnus Damm 
614119f5e44SMagnus Damm module_platform_driver(gpio_rcar_device_driver);
615119f5e44SMagnus Damm 
616119f5e44SMagnus Damm MODULE_AUTHOR("Magnus Damm");
617119f5e44SMagnus Damm MODULE_DESCRIPTION("Renesas R-Car GPIO Driver");
618119f5e44SMagnus Damm MODULE_LICENSE("GPL v2");
619