xref: /openbmc/linux/drivers/gpio/gpio-rcar.c (revision 183245c4)
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 
35208c80f1SGeert Uytterhoeven struct gpio_rcar_info {
36208c80f1SGeert Uytterhoeven 	bool has_outdtsel;
37208c80f1SGeert Uytterhoeven 	bool has_both_edge_trigger;
38208c80f1SGeert Uytterhoeven };
39208c80f1SGeert Uytterhoeven 
40119f5e44SMagnus Damm struct gpio_rcar_priv {
41119f5e44SMagnus Damm 	void __iomem *base;
42119f5e44SMagnus Damm 	spinlock_t lock;
43a53f7953SVladimir Zapolskiy 	struct device *dev;
44119f5e44SMagnus Damm 	struct gpio_chip gpio_chip;
45119f5e44SMagnus Damm 	struct irq_chip irq_chip;
468b092be9SGeert Uytterhoeven 	unsigned int irq_parent;
479ac79ba9SGeert Uytterhoeven 	atomic_t wakeup_path;
48208c80f1SGeert Uytterhoeven 	struct gpio_rcar_info info;
4951750fb1SHien Dang 	struct gpio_rcar_bank_info bank_info;
50119f5e44SMagnus Damm };
51119f5e44SMagnus Damm 
523dc1e685SGeert Uytterhoeven #define IOINTSEL	0x00	/* General IO/Interrupt Switching Register */
533dc1e685SGeert Uytterhoeven #define INOUTSEL	0x04	/* General Input/Output Switching Register */
543dc1e685SGeert Uytterhoeven #define OUTDT		0x08	/* General Output Register */
553dc1e685SGeert Uytterhoeven #define INDT		0x0c	/* General Input Register */
563dc1e685SGeert Uytterhoeven #define INTDT		0x10	/* Interrupt Display Register */
573dc1e685SGeert Uytterhoeven #define INTCLR		0x14	/* Interrupt Clear Register */
583dc1e685SGeert Uytterhoeven #define INTMSK		0x18	/* Interrupt Mask Register */
593dc1e685SGeert Uytterhoeven #define MSKCLR		0x1c	/* Interrupt Mask Clear Register */
603dc1e685SGeert Uytterhoeven #define POSNEG		0x20	/* Positive/Negative Logic Select Register */
613dc1e685SGeert Uytterhoeven #define EDGLEVEL	0x24	/* Edge/level Select Register */
623dc1e685SGeert Uytterhoeven #define FILONOFF	0x28	/* Chattering Prevention On/Off Register */
633ae4f3aaSVladimir Zapolskiy #define OUTDTSEL	0x40	/* Output Data Select Register */
643dc1e685SGeert Uytterhoeven #define BOTHEDGE	0x4c	/* One Edge/Both Edge Select Register */
65119f5e44SMagnus Damm 
66159f8a02SLaurent Pinchart #define RCAR_MAX_GPIO_PER_BANK		32
67159f8a02SLaurent Pinchart 
68119f5e44SMagnus Damm static inline u32 gpio_rcar_read(struct gpio_rcar_priv *p, int offs)
69119f5e44SMagnus Damm {
70119f5e44SMagnus Damm 	return ioread32(p->base + offs);
71119f5e44SMagnus Damm }
72119f5e44SMagnus Damm 
73119f5e44SMagnus Damm static inline void gpio_rcar_write(struct gpio_rcar_priv *p, int offs,
74119f5e44SMagnus Damm 				   u32 value)
75119f5e44SMagnus Damm {
76119f5e44SMagnus Damm 	iowrite32(value, p->base + offs);
77119f5e44SMagnus Damm }
78119f5e44SMagnus Damm 
79119f5e44SMagnus Damm static void gpio_rcar_modify_bit(struct gpio_rcar_priv *p, int offs,
80119f5e44SMagnus Damm 				 int bit, bool value)
81119f5e44SMagnus Damm {
82119f5e44SMagnus Damm 	u32 tmp = gpio_rcar_read(p, offs);
83119f5e44SMagnus Damm 
84119f5e44SMagnus Damm 	if (value)
85119f5e44SMagnus Damm 		tmp |= BIT(bit);
86119f5e44SMagnus Damm 	else
87119f5e44SMagnus Damm 		tmp &= ~BIT(bit);
88119f5e44SMagnus Damm 
89119f5e44SMagnus Damm 	gpio_rcar_write(p, offs, tmp);
90119f5e44SMagnus Damm }
91119f5e44SMagnus Damm 
92119f5e44SMagnus Damm static void gpio_rcar_irq_disable(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, INTMSK, ~BIT(irqd_to_hwirq(d)));
98119f5e44SMagnus Damm }
99119f5e44SMagnus Damm 
100119f5e44SMagnus Damm static void gpio_rcar_irq_enable(struct irq_data *d)
101119f5e44SMagnus Damm {
102c7f3c5d3SGeert Uytterhoeven 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
103c7b6f457SLinus Walleij 	struct gpio_rcar_priv *p = gpiochip_get_data(gc);
104119f5e44SMagnus Damm 
105119f5e44SMagnus Damm 	gpio_rcar_write(p, MSKCLR, BIT(irqd_to_hwirq(d)));
106119f5e44SMagnus Damm }
107119f5e44SMagnus Damm 
108119f5e44SMagnus Damm static void gpio_rcar_config_interrupt_input_mode(struct gpio_rcar_priv *p,
109119f5e44SMagnus Damm 						  unsigned int hwirq,
110119f5e44SMagnus Damm 						  bool active_high_rising_edge,
1117e1092b5SSimon Horman 						  bool level_trigger,
1127e1092b5SSimon Horman 						  bool both)
113119f5e44SMagnus Damm {
114119f5e44SMagnus Damm 	unsigned long flags;
115119f5e44SMagnus Damm 
116119f5e44SMagnus Damm 	/* follow steps in the GPIO documentation for
117119f5e44SMagnus Damm 	 * "Setting Edge-Sensitive Interrupt Input Mode" and
118119f5e44SMagnus Damm 	 * "Setting Level-Sensitive Interrupt Input Mode"
119119f5e44SMagnus Damm 	 */
120119f5e44SMagnus Damm 
121119f5e44SMagnus Damm 	spin_lock_irqsave(&p->lock, flags);
122119f5e44SMagnus Damm 
123b36368f6SAshish Chavan 	/* Configure positive or negative logic in POSNEG */
124119f5e44SMagnus Damm 	gpio_rcar_modify_bit(p, POSNEG, hwirq, !active_high_rising_edge);
125119f5e44SMagnus Damm 
126119f5e44SMagnus Damm 	/* Configure edge or level trigger in EDGLEVEL */
127119f5e44SMagnus Damm 	gpio_rcar_modify_bit(p, EDGLEVEL, hwirq, !level_trigger);
128119f5e44SMagnus Damm 
1297e1092b5SSimon Horman 	/* Select one edge or both edges in BOTHEDGE */
130208c80f1SGeert Uytterhoeven 	if (p->info.has_both_edge_trigger)
1317e1092b5SSimon Horman 		gpio_rcar_modify_bit(p, BOTHEDGE, hwirq, both);
1327e1092b5SSimon Horman 
133119f5e44SMagnus Damm 	/* Select "Interrupt Input Mode" in IOINTSEL */
134119f5e44SMagnus Damm 	gpio_rcar_modify_bit(p, IOINTSEL, hwirq, true);
135119f5e44SMagnus Damm 
136119f5e44SMagnus Damm 	/* Write INTCLR in case of edge trigger */
137119f5e44SMagnus Damm 	if (!level_trigger)
138119f5e44SMagnus Damm 		gpio_rcar_write(p, INTCLR, BIT(hwirq));
139119f5e44SMagnus Damm 
140119f5e44SMagnus Damm 	spin_unlock_irqrestore(&p->lock, flags);
141119f5e44SMagnus Damm }
142119f5e44SMagnus Damm 
143119f5e44SMagnus Damm static int gpio_rcar_irq_set_type(struct irq_data *d, unsigned int type)
144119f5e44SMagnus Damm {
145c7f3c5d3SGeert Uytterhoeven 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
146c7b6f457SLinus Walleij 	struct gpio_rcar_priv *p = gpiochip_get_data(gc);
147119f5e44SMagnus Damm 	unsigned int hwirq = irqd_to_hwirq(d);
148119f5e44SMagnus Damm 
149a53f7953SVladimir Zapolskiy 	dev_dbg(p->dev, "sense irq = %d, type = %d\n", hwirq, type);
150119f5e44SMagnus Damm 
151119f5e44SMagnus Damm 	switch (type & IRQ_TYPE_SENSE_MASK) {
152119f5e44SMagnus Damm 	case IRQ_TYPE_LEVEL_HIGH:
1537e1092b5SSimon Horman 		gpio_rcar_config_interrupt_input_mode(p, hwirq, true, true,
1547e1092b5SSimon Horman 						      false);
155119f5e44SMagnus Damm 		break;
156119f5e44SMagnus Damm 	case IRQ_TYPE_LEVEL_LOW:
1577e1092b5SSimon Horman 		gpio_rcar_config_interrupt_input_mode(p, hwirq, false, true,
1587e1092b5SSimon Horman 						      false);
159119f5e44SMagnus Damm 		break;
160119f5e44SMagnus Damm 	case IRQ_TYPE_EDGE_RISING:
1617e1092b5SSimon Horman 		gpio_rcar_config_interrupt_input_mode(p, hwirq, true, false,
1627e1092b5SSimon Horman 						      false);
163119f5e44SMagnus Damm 		break;
164119f5e44SMagnus Damm 	case IRQ_TYPE_EDGE_FALLING:
1657e1092b5SSimon Horman 		gpio_rcar_config_interrupt_input_mode(p, hwirq, false, false,
1667e1092b5SSimon Horman 						      false);
1677e1092b5SSimon Horman 		break;
1687e1092b5SSimon Horman 	case IRQ_TYPE_EDGE_BOTH:
169208c80f1SGeert Uytterhoeven 		if (!p->info.has_both_edge_trigger)
1707e1092b5SSimon Horman 			return -EINVAL;
1717e1092b5SSimon Horman 		gpio_rcar_config_interrupt_input_mode(p, hwirq, true, false,
1727e1092b5SSimon Horman 						      true);
173119f5e44SMagnus Damm 		break;
174119f5e44SMagnus Damm 	default:
175119f5e44SMagnus Damm 		return -EINVAL;
176119f5e44SMagnus Damm 	}
177119f5e44SMagnus Damm 	return 0;
178119f5e44SMagnus Damm }
179119f5e44SMagnus Damm 
180ab82fa7dSGeert Uytterhoeven static int gpio_rcar_irq_set_wake(struct irq_data *d, unsigned int on)
181ab82fa7dSGeert Uytterhoeven {
182ab82fa7dSGeert Uytterhoeven 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
183c7b6f457SLinus Walleij 	struct gpio_rcar_priv *p = gpiochip_get_data(gc);
184501ef0f9SGeert Uytterhoeven 	int error;
185ab82fa7dSGeert Uytterhoeven 
186501ef0f9SGeert Uytterhoeven 	if (p->irq_parent) {
187501ef0f9SGeert Uytterhoeven 		error = irq_set_irq_wake(p->irq_parent, on);
188501ef0f9SGeert Uytterhoeven 		if (error) {
189a53f7953SVladimir Zapolskiy 			dev_dbg(p->dev, "irq %u doesn't support irq_set_wake\n",
190501ef0f9SGeert Uytterhoeven 				p->irq_parent);
191501ef0f9SGeert Uytterhoeven 			p->irq_parent = 0;
192501ef0f9SGeert Uytterhoeven 		}
193501ef0f9SGeert Uytterhoeven 	}
194ab82fa7dSGeert Uytterhoeven 
195ab82fa7dSGeert Uytterhoeven 	if (on)
1969ac79ba9SGeert Uytterhoeven 		atomic_inc(&p->wakeup_path);
197ab82fa7dSGeert Uytterhoeven 	else
1989ac79ba9SGeert Uytterhoeven 		atomic_dec(&p->wakeup_path);
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));
213f0fbe7bcSThierry Reding 		generic_handle_irq(irq_find_mapping(p->gpio_chip.irq.domain,
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 void gpio_rcar_config_general_input_output_mode(struct gpio_chip *chip,
222119f5e44SMagnus Damm 						       unsigned int gpio,
223119f5e44SMagnus Damm 						       bool output)
224119f5e44SMagnus Damm {
225c7b6f457SLinus Walleij 	struct gpio_rcar_priv *p = gpiochip_get_data(chip);
226119f5e44SMagnus Damm 	unsigned long flags;
227119f5e44SMagnus Damm 
228119f5e44SMagnus Damm 	/* follow steps in the GPIO documentation for
229119f5e44SMagnus Damm 	 * "Setting General Output Mode" and
230119f5e44SMagnus Damm 	 * "Setting General Input Mode"
231119f5e44SMagnus Damm 	 */
232119f5e44SMagnus Damm 
233119f5e44SMagnus Damm 	spin_lock_irqsave(&p->lock, flags);
234119f5e44SMagnus Damm 
235b36368f6SAshish Chavan 	/* Configure positive logic in POSNEG */
236119f5e44SMagnus Damm 	gpio_rcar_modify_bit(p, POSNEG, gpio, false);
237119f5e44SMagnus Damm 
238119f5e44SMagnus Damm 	/* Select "General Input/Output Mode" in IOINTSEL */
239119f5e44SMagnus Damm 	gpio_rcar_modify_bit(p, IOINTSEL, gpio, false);
240119f5e44SMagnus Damm 
241119f5e44SMagnus Damm 	/* Select Input Mode or Output Mode in INOUTSEL */
242119f5e44SMagnus Damm 	gpio_rcar_modify_bit(p, INOUTSEL, gpio, output);
243119f5e44SMagnus Damm 
2443ae4f3aaSVladimir Zapolskiy 	/* Select General Output Register to output data in OUTDTSEL */
245208c80f1SGeert Uytterhoeven 	if (p->info.has_outdtsel && output)
2463ae4f3aaSVladimir Zapolskiy 		gpio_rcar_modify_bit(p, OUTDTSEL, gpio, false);
2473ae4f3aaSVladimir Zapolskiy 
248119f5e44SMagnus Damm 	spin_unlock_irqrestore(&p->lock, flags);
249119f5e44SMagnus Damm }
250119f5e44SMagnus Damm 
251dc3465a9SLaurent Pinchart static int gpio_rcar_request(struct gpio_chip *chip, unsigned offset)
252dc3465a9SLaurent Pinchart {
2532d65472bSGeert Uytterhoeven 	struct gpio_rcar_priv *p = gpiochip_get_data(chip);
2542d65472bSGeert Uytterhoeven 	int error;
2552d65472bSGeert Uytterhoeven 
256a53f7953SVladimir Zapolskiy 	error = pm_runtime_get_sync(p->dev);
2576f8cd246SDinghao Liu 	if (error < 0) {
2586f8cd246SDinghao Liu 		pm_runtime_put(p->dev);
2592d65472bSGeert Uytterhoeven 		return error;
2606f8cd246SDinghao Liu 	}
2612d65472bSGeert Uytterhoeven 
262a9a1d2a7SLinus Walleij 	error = pinctrl_gpio_request(chip->base + offset);
2632d65472bSGeert Uytterhoeven 	if (error)
264a53f7953SVladimir Zapolskiy 		pm_runtime_put(p->dev);
2652d65472bSGeert Uytterhoeven 
2662d65472bSGeert Uytterhoeven 	return error;
267dc3465a9SLaurent Pinchart }
268dc3465a9SLaurent Pinchart 
269dc3465a9SLaurent Pinchart static void gpio_rcar_free(struct gpio_chip *chip, unsigned offset)
270dc3465a9SLaurent Pinchart {
2712d65472bSGeert Uytterhoeven 	struct gpio_rcar_priv *p = gpiochip_get_data(chip);
2722d65472bSGeert Uytterhoeven 
273a9a1d2a7SLinus Walleij 	pinctrl_gpio_free(chip->base + offset);
274dc3465a9SLaurent Pinchart 
275ce0e2c60SLinus Walleij 	/*
276ce0e2c60SLinus Walleij 	 * Set the GPIO as an input to ensure that the next GPIO request won't
277dc3465a9SLaurent Pinchart 	 * drive the GPIO pin as an output.
278dc3465a9SLaurent Pinchart 	 */
279dc3465a9SLaurent Pinchart 	gpio_rcar_config_general_input_output_mode(chip, offset, false);
2802d65472bSGeert Uytterhoeven 
281a53f7953SVladimir Zapolskiy 	pm_runtime_put(p->dev);
282dc3465a9SLaurent Pinchart }
283dc3465a9SLaurent Pinchart 
284ad817297SGeert Uytterhoeven static int gpio_rcar_get_direction(struct gpio_chip *chip, unsigned int offset)
285ad817297SGeert Uytterhoeven {
286ad817297SGeert Uytterhoeven 	struct gpio_rcar_priv *p = gpiochip_get_data(chip);
287ad817297SGeert Uytterhoeven 
288e42615ecSMatti Vaittinen 	if (gpio_rcar_read(p, INOUTSEL) & BIT(offset))
289e42615ecSMatti Vaittinen 		return GPIO_LINE_DIRECTION_OUT;
290e42615ecSMatti Vaittinen 
291e42615ecSMatti Vaittinen 	return GPIO_LINE_DIRECTION_IN;
292ad817297SGeert Uytterhoeven }
293ad817297SGeert Uytterhoeven 
294119f5e44SMagnus Damm static int gpio_rcar_direction_input(struct gpio_chip *chip, unsigned offset)
295119f5e44SMagnus Damm {
296119f5e44SMagnus Damm 	gpio_rcar_config_general_input_output_mode(chip, offset, false);
297119f5e44SMagnus Damm 	return 0;
298119f5e44SMagnus Damm }
299119f5e44SMagnus Damm 
300119f5e44SMagnus Damm static int gpio_rcar_get(struct gpio_chip *chip, unsigned offset)
301119f5e44SMagnus Damm {
302714d3a29SGeert Uytterhoeven 	struct gpio_rcar_priv *p = gpiochip_get_data(chip);
303ae9550f6SMagnus Damm 	u32 bit = BIT(offset);
304ae9550f6SMagnus Damm 
305ae9550f6SMagnus Damm 	/* testing on r8a7790 shows that INDT does not show correct pin state
306ae9550f6SMagnus Damm 	 * when configured as output, so use OUTDT in case of output pins */
307714d3a29SGeert Uytterhoeven 	if (gpio_rcar_read(p, INOUTSEL) & bit)
308714d3a29SGeert Uytterhoeven 		return !!(gpio_rcar_read(p, OUTDT) & bit);
309ae9550f6SMagnus Damm 	else
310714d3a29SGeert Uytterhoeven 		return !!(gpio_rcar_read(p, INDT) & bit);
311119f5e44SMagnus Damm }
312119f5e44SMagnus Damm 
313*183245c4SGeert Uytterhoeven static int gpio_rcar_get_multiple(struct gpio_chip *chip, unsigned long *mask,
314*183245c4SGeert Uytterhoeven 				  unsigned long *bits)
315*183245c4SGeert Uytterhoeven {
316*183245c4SGeert Uytterhoeven 	struct gpio_rcar_priv *p = gpiochip_get_data(chip);
317*183245c4SGeert Uytterhoeven 	u32 bankmask, outputs, m, val = 0;
318*183245c4SGeert Uytterhoeven 	unsigned long flags;
319*183245c4SGeert Uytterhoeven 
320*183245c4SGeert Uytterhoeven 	bankmask = mask[0] & GENMASK(chip->ngpio - 1, 0);
321*183245c4SGeert Uytterhoeven 	if (chip->valid_mask)
322*183245c4SGeert Uytterhoeven 		bankmask &= chip->valid_mask[0];
323*183245c4SGeert Uytterhoeven 
324*183245c4SGeert Uytterhoeven 	if (!bankmask)
325*183245c4SGeert Uytterhoeven 		return 0;
326*183245c4SGeert Uytterhoeven 
327*183245c4SGeert Uytterhoeven 	spin_lock_irqsave(&p->lock, flags);
328*183245c4SGeert Uytterhoeven 	outputs = gpio_rcar_read(p, INOUTSEL);
329*183245c4SGeert Uytterhoeven 	m = outputs & bankmask;
330*183245c4SGeert Uytterhoeven 	if (m)
331*183245c4SGeert Uytterhoeven 		val |= gpio_rcar_read(p, OUTDT) & m;
332*183245c4SGeert Uytterhoeven 
333*183245c4SGeert Uytterhoeven 	m = ~outputs & bankmask;
334*183245c4SGeert Uytterhoeven 	if (m)
335*183245c4SGeert Uytterhoeven 		val |= gpio_rcar_read(p, INDT) & m;
336*183245c4SGeert Uytterhoeven 	spin_unlock_irqrestore(&p->lock, flags);
337*183245c4SGeert Uytterhoeven 
338*183245c4SGeert Uytterhoeven 	bits[0] = val;
339*183245c4SGeert Uytterhoeven 	return 0;
340*183245c4SGeert Uytterhoeven }
341*183245c4SGeert Uytterhoeven 
342119f5e44SMagnus Damm static void gpio_rcar_set(struct gpio_chip *chip, unsigned offset, int value)
343119f5e44SMagnus Damm {
344c7b6f457SLinus Walleij 	struct gpio_rcar_priv *p = gpiochip_get_data(chip);
345119f5e44SMagnus Damm 	unsigned long flags;
346119f5e44SMagnus Damm 
347119f5e44SMagnus Damm 	spin_lock_irqsave(&p->lock, flags);
348119f5e44SMagnus Damm 	gpio_rcar_modify_bit(p, OUTDT, offset, value);
349119f5e44SMagnus Damm 	spin_unlock_irqrestore(&p->lock, flags);
350119f5e44SMagnus Damm }
351119f5e44SMagnus Damm 
352dbb763b8SGeert Uytterhoeven static void gpio_rcar_set_multiple(struct gpio_chip *chip, unsigned long *mask,
353dbb763b8SGeert Uytterhoeven 				   unsigned long *bits)
354dbb763b8SGeert Uytterhoeven {
355dbb763b8SGeert Uytterhoeven 	struct gpio_rcar_priv *p = gpiochip_get_data(chip);
356dbb763b8SGeert Uytterhoeven 	unsigned long flags;
357dbb763b8SGeert Uytterhoeven 	u32 val, bankmask;
358dbb763b8SGeert Uytterhoeven 
359dbb763b8SGeert Uytterhoeven 	bankmask = mask[0] & GENMASK(chip->ngpio - 1, 0);
360496069b8SBiju Das 	if (chip->valid_mask)
361496069b8SBiju Das 		bankmask &= chip->valid_mask[0];
362496069b8SBiju Das 
363dbb763b8SGeert Uytterhoeven 	if (!bankmask)
364dbb763b8SGeert Uytterhoeven 		return;
365dbb763b8SGeert Uytterhoeven 
366dbb763b8SGeert Uytterhoeven 	spin_lock_irqsave(&p->lock, flags);
367dbb763b8SGeert Uytterhoeven 	val = gpio_rcar_read(p, OUTDT);
368dbb763b8SGeert Uytterhoeven 	val &= ~bankmask;
369dbb763b8SGeert Uytterhoeven 	val |= (bankmask & bits[0]);
370dbb763b8SGeert Uytterhoeven 	gpio_rcar_write(p, OUTDT, val);
371dbb763b8SGeert Uytterhoeven 	spin_unlock_irqrestore(&p->lock, flags);
372dbb763b8SGeert Uytterhoeven }
373dbb763b8SGeert Uytterhoeven 
374119f5e44SMagnus Damm static int gpio_rcar_direction_output(struct gpio_chip *chip, unsigned offset,
375119f5e44SMagnus Damm 				      int value)
376119f5e44SMagnus Damm {
377119f5e44SMagnus Damm 	/* write GPIO value to output before selecting output mode of pin */
378119f5e44SMagnus Damm 	gpio_rcar_set(chip, offset, value);
379119f5e44SMagnus Damm 	gpio_rcar_config_general_input_output_mode(chip, offset, true);
380119f5e44SMagnus Damm 	return 0;
381119f5e44SMagnus Damm }
382119f5e44SMagnus Damm 
3831fd2b49dSHisashi Nakamura static const struct gpio_rcar_info gpio_rcar_info_gen1 = {
3843ae4f3aaSVladimir Zapolskiy 	.has_outdtsel = false,
3851fd2b49dSHisashi Nakamura 	.has_both_edge_trigger = false,
3861fd2b49dSHisashi Nakamura };
3871fd2b49dSHisashi Nakamura 
3881fd2b49dSHisashi Nakamura static const struct gpio_rcar_info gpio_rcar_info_gen2 = {
3893ae4f3aaSVladimir Zapolskiy 	.has_outdtsel = true,
3901fd2b49dSHisashi Nakamura 	.has_both_edge_trigger = true,
3911fd2b49dSHisashi Nakamura };
3921fd2b49dSHisashi Nakamura 
393850dfe17SLaurent Pinchart static const struct of_device_id gpio_rcar_of_table[] = {
394850dfe17SLaurent Pinchart 	{
39585bb4646SBiju Das 		.compatible = "renesas,gpio-r8a7743",
39685bb4646SBiju Das 		/* RZ/G1 GPIO is identical to R-Car Gen2. */
39785bb4646SBiju Das 		.data = &gpio_rcar_info_gen2,
39885bb4646SBiju Das 	}, {
399850dfe17SLaurent Pinchart 		.compatible = "renesas,gpio-r8a7790",
4001fd2b49dSHisashi Nakamura 		.data = &gpio_rcar_info_gen2,
401850dfe17SLaurent Pinchart 	}, {
402850dfe17SLaurent Pinchart 		.compatible = "renesas,gpio-r8a7791",
4031fd2b49dSHisashi Nakamura 		.data = &gpio_rcar_info_gen2,
4041fd2b49dSHisashi Nakamura 	}, {
405e79c5830SSergei Shtylyov 		.compatible = "renesas,gpio-r8a7792",
406e79c5830SSergei Shtylyov 		.data = &gpio_rcar_info_gen2,
407e79c5830SSergei Shtylyov 	}, {
4081fd2b49dSHisashi Nakamura 		.compatible = "renesas,gpio-r8a7793",
4091fd2b49dSHisashi Nakamura 		.data = &gpio_rcar_info_gen2,
4101fd2b49dSHisashi Nakamura 	}, {
4111fd2b49dSHisashi Nakamura 		.compatible = "renesas,gpio-r8a7794",
4121fd2b49dSHisashi Nakamura 		.data = &gpio_rcar_info_gen2,
413850dfe17SLaurent Pinchart 	}, {
4148cd14702SUlrich Hecht 		.compatible = "renesas,gpio-r8a7795",
4158cd14702SUlrich Hecht 		/* Gen3 GPIO is identical to Gen2. */
4168cd14702SUlrich Hecht 		.data = &gpio_rcar_info_gen2,
4178cd14702SUlrich Hecht 	}, {
4185d2f1d6eSSimon Horman 		.compatible = "renesas,gpio-r8a7796",
4195d2f1d6eSSimon Horman 		/* Gen3 GPIO is identical to Gen2. */
4205d2f1d6eSSimon Horman 		.data = &gpio_rcar_info_gen2,
4215d2f1d6eSSimon Horman 	}, {
422dbd1dad2SSimon Horman 		.compatible = "renesas,rcar-gen1-gpio",
423dbd1dad2SSimon Horman 		.data = &gpio_rcar_info_gen1,
424dbd1dad2SSimon Horman 	}, {
425dbd1dad2SSimon Horman 		.compatible = "renesas,rcar-gen2-gpio",
426dbd1dad2SSimon Horman 		.data = &gpio_rcar_info_gen2,
427dbd1dad2SSimon Horman 	}, {
428dbd1dad2SSimon Horman 		.compatible = "renesas,rcar-gen3-gpio",
429dbd1dad2SSimon Horman 		/* Gen3 GPIO is identical to Gen2. */
430dbd1dad2SSimon Horman 		.data = &gpio_rcar_info_gen2,
431dbd1dad2SSimon Horman 	}, {
432850dfe17SLaurent Pinchart 		.compatible = "renesas,gpio-rcar",
4331fd2b49dSHisashi Nakamura 		.data = &gpio_rcar_info_gen1,
434850dfe17SLaurent Pinchart 	}, {
435850dfe17SLaurent Pinchart 		/* Terminator */
436850dfe17SLaurent Pinchart 	},
437850dfe17SLaurent Pinchart };
438850dfe17SLaurent Pinchart 
439850dfe17SLaurent Pinchart MODULE_DEVICE_TABLE(of, gpio_rcar_of_table);
440850dfe17SLaurent Pinchart 
4418b092be9SGeert Uytterhoeven static int gpio_rcar_parse_dt(struct gpio_rcar_priv *p, unsigned int *npins)
442159f8a02SLaurent Pinchart {
443a53f7953SVladimir Zapolskiy 	struct device_node *np = p->dev->of_node;
444850dfe17SLaurent Pinchart 	const struct gpio_rcar_info *info;
4458b092be9SGeert Uytterhoeven 	struct of_phandle_args args;
4468b092be9SGeert Uytterhoeven 	int ret;
447850dfe17SLaurent Pinchart 
448a53f7953SVladimir Zapolskiy 	info = of_device_get_match_data(p->dev);
449208c80f1SGeert Uytterhoeven 	p->info = *info;
450850dfe17SLaurent Pinchart 
4518b092be9SGeert Uytterhoeven 	ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3, 0, &args);
4528b092be9SGeert Uytterhoeven 	*npins = ret == 0 ? args.args[2] : RCAR_MAX_GPIO_PER_BANK;
453159f8a02SLaurent Pinchart 
4548b092be9SGeert Uytterhoeven 	if (*npins == 0 || *npins > RCAR_MAX_GPIO_PER_BANK) {
455a53f7953SVladimir Zapolskiy 		dev_warn(p->dev, "Invalid number of gpio lines %u, using %u\n",
456a53f7953SVladimir Zapolskiy 			 *npins, RCAR_MAX_GPIO_PER_BANK);
4578b092be9SGeert Uytterhoeven 		*npins = RCAR_MAX_GPIO_PER_BANK;
458159f8a02SLaurent Pinchart 	}
459850dfe17SLaurent Pinchart 
460850dfe17SLaurent Pinchart 	return 0;
461159f8a02SLaurent Pinchart }
462159f8a02SLaurent Pinchart 
463119f5e44SMagnus Damm static int gpio_rcar_probe(struct platform_device *pdev)
464119f5e44SMagnus Damm {
465119f5e44SMagnus Damm 	struct gpio_rcar_priv *p;
466ecbf7c2eSEnrico Weigelt, metux IT consult 	struct resource *irq;
467119f5e44SMagnus Damm 	struct gpio_chip *gpio_chip;
468119f5e44SMagnus Damm 	struct irq_chip *irq_chip;
469b470cef1SLinus Walleij 	struct gpio_irq_chip *girq;
470b22978fcSGeert Uytterhoeven 	struct device *dev = &pdev->dev;
471b22978fcSGeert Uytterhoeven 	const char *name = dev_name(dev);
4728b092be9SGeert Uytterhoeven 	unsigned int npins;
473119f5e44SMagnus Damm 	int ret;
474119f5e44SMagnus Damm 
475b22978fcSGeert Uytterhoeven 	p = devm_kzalloc(dev, sizeof(*p), GFP_KERNEL);
4767d82bf34SGeert Uytterhoeven 	if (!p)
4777d82bf34SGeert Uytterhoeven 		return -ENOMEM;
478119f5e44SMagnus Damm 
479a53f7953SVladimir Zapolskiy 	p->dev = dev;
480119f5e44SMagnus Damm 	spin_lock_init(&p->lock);
481119f5e44SMagnus Damm 
4828b092be9SGeert Uytterhoeven 	/* Get device configuration from DT node */
4838b092be9SGeert Uytterhoeven 	ret = gpio_rcar_parse_dt(p, &npins);
484850dfe17SLaurent Pinchart 	if (ret < 0)
485850dfe17SLaurent Pinchart 		return ret;
486159f8a02SLaurent Pinchart 
487159f8a02SLaurent Pinchart 	platform_set_drvdata(pdev, p);
488159f8a02SLaurent Pinchart 
489df0c6c80SGeert Uytterhoeven 	pm_runtime_enable(dev);
490df0c6c80SGeert Uytterhoeven 
491119f5e44SMagnus Damm 	irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
4925a24d4b6SSergei Shtylyov 	if (!irq) {
4935a24d4b6SSergei Shtylyov 		dev_err(dev, "missing IRQ\n");
494119f5e44SMagnus Damm 		ret = -EINVAL;
495119f5e44SMagnus Damm 		goto err0;
496119f5e44SMagnus Damm 	}
497119f5e44SMagnus Damm 
498ecbf7c2eSEnrico Weigelt, metux IT consult 	p->base = devm_platform_ioremap_resource(pdev, 0);
4995a24d4b6SSergei Shtylyov 	if (IS_ERR(p->base)) {
5005a24d4b6SSergei Shtylyov 		ret = PTR_ERR(p->base);
501119f5e44SMagnus Damm 		goto err0;
502119f5e44SMagnus Damm 	}
503119f5e44SMagnus Damm 
504119f5e44SMagnus Damm 	gpio_chip = &p->gpio_chip;
505dc3465a9SLaurent Pinchart 	gpio_chip->request = gpio_rcar_request;
506dc3465a9SLaurent Pinchart 	gpio_chip->free = gpio_rcar_free;
507ad817297SGeert Uytterhoeven 	gpio_chip->get_direction = gpio_rcar_get_direction;
508119f5e44SMagnus Damm 	gpio_chip->direction_input = gpio_rcar_direction_input;
509119f5e44SMagnus Damm 	gpio_chip->get = gpio_rcar_get;
510*183245c4SGeert Uytterhoeven 	gpio_chip->get_multiple = gpio_rcar_get_multiple;
511119f5e44SMagnus Damm 	gpio_chip->direction_output = gpio_rcar_direction_output;
512119f5e44SMagnus Damm 	gpio_chip->set = gpio_rcar_set;
513dbb763b8SGeert Uytterhoeven 	gpio_chip->set_multiple = gpio_rcar_set_multiple;
514119f5e44SMagnus Damm 	gpio_chip->label = name;
51558383c78SLinus Walleij 	gpio_chip->parent = dev;
516119f5e44SMagnus Damm 	gpio_chip->owner = THIS_MODULE;
5178b092be9SGeert Uytterhoeven 	gpio_chip->base = -1;
5188b092be9SGeert Uytterhoeven 	gpio_chip->ngpio = npins;
519119f5e44SMagnus Damm 
520119f5e44SMagnus Damm 	irq_chip = &p->irq_chip;
521f932a686SGeert Uytterhoeven 	irq_chip->name = "gpio-rcar";
52247bd38a3SNiklas Söderlund 	irq_chip->parent_device = dev;
523119f5e44SMagnus Damm 	irq_chip->irq_mask = gpio_rcar_irq_disable;
524119f5e44SMagnus Damm 	irq_chip->irq_unmask = gpio_rcar_irq_enable;
525119f5e44SMagnus Damm 	irq_chip->irq_set_type = gpio_rcar_irq_set_type;
526ab82fa7dSGeert Uytterhoeven 	irq_chip->irq_set_wake = gpio_rcar_irq_set_wake;
527ab82fa7dSGeert Uytterhoeven 	irq_chip->flags = IRQCHIP_SET_TYPE_MASKED | IRQCHIP_MASK_ON_SUSPEND;
528119f5e44SMagnus Damm 
529b470cef1SLinus Walleij 	girq = &gpio_chip->irq;
530b470cef1SLinus Walleij 	girq->chip = irq_chip;
531b470cef1SLinus Walleij 	/* This will let us handle the parent IRQ in the driver */
532b470cef1SLinus Walleij 	girq->parent_handler = NULL;
533b470cef1SLinus Walleij 	girq->num_parents = 0;
534b470cef1SLinus Walleij 	girq->parents = NULL;
535b470cef1SLinus Walleij 	girq->default_type = IRQ_TYPE_NONE;
536b470cef1SLinus Walleij 	girq->handler = handle_level_irq;
537b470cef1SLinus Walleij 
538c7b6f457SLinus Walleij 	ret = gpiochip_add_data(gpio_chip, p);
539c7f3c5d3SGeert Uytterhoeven 	if (ret) {
540c7f3c5d3SGeert Uytterhoeven 		dev_err(dev, "failed to add GPIO controller\n");
5410c8aab8eSDan Carpenter 		goto err0;
542119f5e44SMagnus Damm 	}
543119f5e44SMagnus Damm 
544ab82fa7dSGeert Uytterhoeven 	p->irq_parent = irq->start;
545b22978fcSGeert Uytterhoeven 	if (devm_request_irq(dev, irq->start, gpio_rcar_irq_handler,
546b22978fcSGeert Uytterhoeven 			     IRQF_SHARED, name, p)) {
547b22978fcSGeert Uytterhoeven 		dev_err(dev, "failed to request IRQ\n");
548119f5e44SMagnus Damm 		ret = -ENOENT;
549119f5e44SMagnus Damm 		goto err1;
550119f5e44SMagnus Damm 	}
551119f5e44SMagnus Damm 
5528b092be9SGeert Uytterhoeven 	dev_info(dev, "driving %d GPIOs\n", npins);
553dc3465a9SLaurent Pinchart 
554119f5e44SMagnus Damm 	return 0;
555119f5e44SMagnus Damm 
556119f5e44SMagnus Damm err1:
5574d84b9e4SGeert Uytterhoeven 	gpiochip_remove(gpio_chip);
558119f5e44SMagnus Damm err0:
559df0c6c80SGeert Uytterhoeven 	pm_runtime_disable(dev);
560119f5e44SMagnus Damm 	return ret;
561119f5e44SMagnus Damm }
562119f5e44SMagnus Damm 
563119f5e44SMagnus Damm static int gpio_rcar_remove(struct platform_device *pdev)
564119f5e44SMagnus Damm {
565119f5e44SMagnus Damm 	struct gpio_rcar_priv *p = platform_get_drvdata(pdev);
566119f5e44SMagnus Damm 
5679f5132aeSabdoulaye berthe 	gpiochip_remove(&p->gpio_chip);
568119f5e44SMagnus Damm 
569df0c6c80SGeert Uytterhoeven 	pm_runtime_disable(&pdev->dev);
570119f5e44SMagnus Damm 	return 0;
571119f5e44SMagnus Damm }
572119f5e44SMagnus Damm 
57351750fb1SHien Dang #ifdef CONFIG_PM_SLEEP
57451750fb1SHien Dang static int gpio_rcar_suspend(struct device *dev)
57551750fb1SHien Dang {
57651750fb1SHien Dang 	struct gpio_rcar_priv *p = dev_get_drvdata(dev);
57751750fb1SHien Dang 
57851750fb1SHien Dang 	p->bank_info.iointsel = gpio_rcar_read(p, IOINTSEL);
57951750fb1SHien Dang 	p->bank_info.inoutsel = gpio_rcar_read(p, INOUTSEL);
58051750fb1SHien Dang 	p->bank_info.outdt = gpio_rcar_read(p, OUTDT);
58151750fb1SHien Dang 	p->bank_info.intmsk = gpio_rcar_read(p, INTMSK);
58251750fb1SHien Dang 	p->bank_info.posneg = gpio_rcar_read(p, POSNEG);
58351750fb1SHien Dang 	p->bank_info.edglevel = gpio_rcar_read(p, EDGLEVEL);
584208c80f1SGeert Uytterhoeven 	if (p->info.has_both_edge_trigger)
58551750fb1SHien Dang 		p->bank_info.bothedge = gpio_rcar_read(p, BOTHEDGE);
58651750fb1SHien Dang 
5879ac79ba9SGeert Uytterhoeven 	if (atomic_read(&p->wakeup_path))
5889ac79ba9SGeert Uytterhoeven 		device_set_wakeup_path(dev);
5899ac79ba9SGeert Uytterhoeven 
59051750fb1SHien Dang 	return 0;
59151750fb1SHien Dang }
59251750fb1SHien Dang 
59351750fb1SHien Dang static int gpio_rcar_resume(struct device *dev)
59451750fb1SHien Dang {
59551750fb1SHien Dang 	struct gpio_rcar_priv *p = dev_get_drvdata(dev);
59651750fb1SHien Dang 	unsigned int offset;
59751750fb1SHien Dang 	u32 mask;
59851750fb1SHien Dang 
59951750fb1SHien Dang 	for (offset = 0; offset < p->gpio_chip.ngpio; offset++) {
600496069b8SBiju Das 		if (!gpiochip_line_is_valid(&p->gpio_chip, offset))
601496069b8SBiju Das 			continue;
602496069b8SBiju Das 
60351750fb1SHien Dang 		mask = BIT(offset);
60451750fb1SHien Dang 		/* I/O pin */
60551750fb1SHien Dang 		if (!(p->bank_info.iointsel & mask)) {
60651750fb1SHien Dang 			if (p->bank_info.inoutsel & mask)
60751750fb1SHien Dang 				gpio_rcar_direction_output(
60851750fb1SHien Dang 					&p->gpio_chip, offset,
60951750fb1SHien Dang 					!!(p->bank_info.outdt & mask));
61051750fb1SHien Dang 			else
61151750fb1SHien Dang 				gpio_rcar_direction_input(&p->gpio_chip,
61251750fb1SHien Dang 							  offset);
61351750fb1SHien Dang 		} else {
61451750fb1SHien Dang 			/* Interrupt pin */
61551750fb1SHien Dang 			gpio_rcar_config_interrupt_input_mode(
61651750fb1SHien Dang 				p,
61751750fb1SHien Dang 				offset,
61851750fb1SHien Dang 				!(p->bank_info.posneg & mask),
61951750fb1SHien Dang 				!(p->bank_info.edglevel & mask),
62051750fb1SHien Dang 				!!(p->bank_info.bothedge & mask));
62151750fb1SHien Dang 
62251750fb1SHien Dang 			if (p->bank_info.intmsk & mask)
62351750fb1SHien Dang 				gpio_rcar_write(p, MSKCLR, mask);
62451750fb1SHien Dang 		}
62551750fb1SHien Dang 	}
62651750fb1SHien Dang 
62751750fb1SHien Dang 	return 0;
62851750fb1SHien Dang }
62951750fb1SHien Dang #endif /* CONFIG_PM_SLEEP*/
63051750fb1SHien Dang 
63151750fb1SHien Dang static SIMPLE_DEV_PM_OPS(gpio_rcar_pm_ops, gpio_rcar_suspend, gpio_rcar_resume);
63251750fb1SHien Dang 
633119f5e44SMagnus Damm static struct platform_driver gpio_rcar_device_driver = {
634119f5e44SMagnus Damm 	.probe		= gpio_rcar_probe,
635119f5e44SMagnus Damm 	.remove		= gpio_rcar_remove,
636119f5e44SMagnus Damm 	.driver		= {
637119f5e44SMagnus Damm 		.name	= "gpio_rcar",
63851750fb1SHien Dang 		.pm     = &gpio_rcar_pm_ops,
639159f8a02SLaurent Pinchart 		.of_match_table = of_match_ptr(gpio_rcar_of_table),
640119f5e44SMagnus Damm 	}
641119f5e44SMagnus Damm };
642119f5e44SMagnus Damm 
643119f5e44SMagnus Damm module_platform_driver(gpio_rcar_device_driver);
644119f5e44SMagnus Damm 
645119f5e44SMagnus Damm MODULE_AUTHOR("Magnus Damm");
646119f5e44SMagnus Damm MODULE_DESCRIPTION("Renesas R-Car GPIO Driver");
647119f5e44SMagnus Damm MODULE_LICENSE("GPL v2");
648