xref: /openbmc/linux/drivers/gpio/gpio-rcar.c (revision ce0e2c60)
1119f5e44SMagnus Damm /*
2119f5e44SMagnus Damm  * Renesas R-Car GPIO Support
3119f5e44SMagnus Damm  *
41fd2b49dSHisashi Nakamura  *  Copyright (C) 2014 Renesas Electronics Corporation
5119f5e44SMagnus Damm  *  Copyright (C) 2013 Magnus Damm
6119f5e44SMagnus Damm  *
7119f5e44SMagnus Damm  * This program is free software; you can redistribute it and/or modify
8119f5e44SMagnus Damm  * it under the terms of the GNU General Public License as published by
9119f5e44SMagnus Damm  * the Free Software Foundation; either version 2 of the License
10119f5e44SMagnus Damm  *
11119f5e44SMagnus Damm  * This program is distributed in the hope that it will be useful,
12119f5e44SMagnus Damm  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13119f5e44SMagnus Damm  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14119f5e44SMagnus Damm  * GNU General Public License for more details.
15119f5e44SMagnus Damm  */
16119f5e44SMagnus Damm 
17ab82fa7dSGeert Uytterhoeven #include <linux/clk.h>
18119f5e44SMagnus Damm #include <linux/err.h>
19119f5e44SMagnus Damm #include <linux/gpio.h>
20119f5e44SMagnus Damm #include <linux/init.h>
21119f5e44SMagnus Damm #include <linux/interrupt.h>
22119f5e44SMagnus Damm #include <linux/io.h>
23119f5e44SMagnus Damm #include <linux/ioport.h>
24119f5e44SMagnus Damm #include <linux/irq.h>
25119f5e44SMagnus Damm #include <linux/module.h>
26bd0bf468SSachin Kamat #include <linux/of.h>
27dc3465a9SLaurent Pinchart #include <linux/pinctrl/consumer.h>
28119f5e44SMagnus Damm #include <linux/platform_device.h>
29df0c6c80SGeert Uytterhoeven #include <linux/pm_runtime.h>
30119f5e44SMagnus Damm #include <linux/spinlock.h>
31119f5e44SMagnus Damm #include <linux/slab.h>
32119f5e44SMagnus Damm 
33119f5e44SMagnus Damm struct gpio_rcar_priv {
34119f5e44SMagnus Damm 	void __iomem *base;
35119f5e44SMagnus Damm 	spinlock_t lock;
36119f5e44SMagnus Damm 	struct platform_device *pdev;
37119f5e44SMagnus Damm 	struct gpio_chip gpio_chip;
38119f5e44SMagnus Damm 	struct irq_chip irq_chip;
39ab82fa7dSGeert Uytterhoeven 	struct clk *clk;
408b092be9SGeert Uytterhoeven 	unsigned int irq_parent;
418b092be9SGeert Uytterhoeven 	bool has_both_edge_trigger;
42e1fef9e2SGeert Uytterhoeven 	bool needs_clk;
43119f5e44SMagnus Damm };
44119f5e44SMagnus Damm 
453dc1e685SGeert Uytterhoeven #define IOINTSEL 0x00	/* General IO/Interrupt Switching Register */
463dc1e685SGeert Uytterhoeven #define INOUTSEL 0x04	/* General Input/Output Switching Register */
473dc1e685SGeert Uytterhoeven #define OUTDT 0x08	/* General Output Register */
483dc1e685SGeert Uytterhoeven #define INDT 0x0c	/* General Input Register */
493dc1e685SGeert Uytterhoeven #define INTDT 0x10	/* Interrupt Display Register */
503dc1e685SGeert Uytterhoeven #define INTCLR 0x14	/* Interrupt Clear Register */
513dc1e685SGeert Uytterhoeven #define INTMSK 0x18	/* Interrupt Mask Register */
523dc1e685SGeert Uytterhoeven #define MSKCLR 0x1c	/* Interrupt Mask Clear Register */
533dc1e685SGeert Uytterhoeven #define POSNEG 0x20	/* Positive/Negative Logic Select Register */
543dc1e685SGeert Uytterhoeven #define EDGLEVEL 0x24	/* Edge/level Select Register */
553dc1e685SGeert Uytterhoeven #define FILONOFF 0x28	/* Chattering Prevention On/Off Register */
563dc1e685SGeert Uytterhoeven #define BOTHEDGE 0x4c	/* One Edge/Both Edge Select Register */
57119f5e44SMagnus Damm 
58159f8a02SLaurent Pinchart #define RCAR_MAX_GPIO_PER_BANK		32
59159f8a02SLaurent Pinchart 
60119f5e44SMagnus Damm static inline u32 gpio_rcar_read(struct gpio_rcar_priv *p, int offs)
61119f5e44SMagnus Damm {
62119f5e44SMagnus Damm 	return ioread32(p->base + offs);
63119f5e44SMagnus Damm }
64119f5e44SMagnus Damm 
65119f5e44SMagnus Damm static inline void gpio_rcar_write(struct gpio_rcar_priv *p, int offs,
66119f5e44SMagnus Damm 				   u32 value)
67119f5e44SMagnus Damm {
68119f5e44SMagnus Damm 	iowrite32(value, p->base + offs);
69119f5e44SMagnus Damm }
70119f5e44SMagnus Damm 
71119f5e44SMagnus Damm static void gpio_rcar_modify_bit(struct gpio_rcar_priv *p, int offs,
72119f5e44SMagnus Damm 				 int bit, bool value)
73119f5e44SMagnus Damm {
74119f5e44SMagnus Damm 	u32 tmp = gpio_rcar_read(p, offs);
75119f5e44SMagnus Damm 
76119f5e44SMagnus Damm 	if (value)
77119f5e44SMagnus Damm 		tmp |= BIT(bit);
78119f5e44SMagnus Damm 	else
79119f5e44SMagnus Damm 		tmp &= ~BIT(bit);
80119f5e44SMagnus Damm 
81119f5e44SMagnus Damm 	gpio_rcar_write(p, offs, tmp);
82119f5e44SMagnus Damm }
83119f5e44SMagnus Damm 
84119f5e44SMagnus Damm static void gpio_rcar_irq_disable(struct irq_data *d)
85119f5e44SMagnus Damm {
86c7f3c5d3SGeert Uytterhoeven 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
87c7b6f457SLinus Walleij 	struct gpio_rcar_priv *p = gpiochip_get_data(gc);
88119f5e44SMagnus Damm 
89119f5e44SMagnus Damm 	gpio_rcar_write(p, INTMSK, ~BIT(irqd_to_hwirq(d)));
90119f5e44SMagnus Damm }
91119f5e44SMagnus Damm 
92119f5e44SMagnus Damm static void gpio_rcar_irq_enable(struct irq_data *d)
93119f5e44SMagnus Damm {
94c7f3c5d3SGeert Uytterhoeven 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
95c7b6f457SLinus Walleij 	struct gpio_rcar_priv *p = gpiochip_get_data(gc);
96119f5e44SMagnus Damm 
97119f5e44SMagnus Damm 	gpio_rcar_write(p, MSKCLR, BIT(irqd_to_hwirq(d)));
98119f5e44SMagnus Damm }
99119f5e44SMagnus Damm 
100119f5e44SMagnus Damm static void gpio_rcar_config_interrupt_input_mode(struct gpio_rcar_priv *p,
101119f5e44SMagnus Damm 						  unsigned int hwirq,
102119f5e44SMagnus Damm 						  bool active_high_rising_edge,
1037e1092b5SSimon Horman 						  bool level_trigger,
1047e1092b5SSimon Horman 						  bool both)
105119f5e44SMagnus Damm {
106119f5e44SMagnus Damm 	unsigned long flags;
107119f5e44SMagnus Damm 
108119f5e44SMagnus Damm 	/* follow steps in the GPIO documentation for
109119f5e44SMagnus Damm 	 * "Setting Edge-Sensitive Interrupt Input Mode" and
110119f5e44SMagnus Damm 	 * "Setting Level-Sensitive Interrupt Input Mode"
111119f5e44SMagnus Damm 	 */
112119f5e44SMagnus Damm 
113119f5e44SMagnus Damm 	spin_lock_irqsave(&p->lock, flags);
114119f5e44SMagnus Damm 
115119f5e44SMagnus Damm 	/* Configure postive or negative logic in POSNEG */
116119f5e44SMagnus Damm 	gpio_rcar_modify_bit(p, POSNEG, hwirq, !active_high_rising_edge);
117119f5e44SMagnus Damm 
118119f5e44SMagnus Damm 	/* Configure edge or level trigger in EDGLEVEL */
119119f5e44SMagnus Damm 	gpio_rcar_modify_bit(p, EDGLEVEL, hwirq, !level_trigger);
120119f5e44SMagnus Damm 
1217e1092b5SSimon Horman 	/* Select one edge or both edges in BOTHEDGE */
1228b092be9SGeert Uytterhoeven 	if (p->has_both_edge_trigger)
1237e1092b5SSimon Horman 		gpio_rcar_modify_bit(p, BOTHEDGE, hwirq, both);
1247e1092b5SSimon Horman 
125119f5e44SMagnus Damm 	/* Select "Interrupt Input Mode" in IOINTSEL */
126119f5e44SMagnus Damm 	gpio_rcar_modify_bit(p, IOINTSEL, hwirq, true);
127119f5e44SMagnus Damm 
128119f5e44SMagnus Damm 	/* Write INTCLR in case of edge trigger */
129119f5e44SMagnus Damm 	if (!level_trigger)
130119f5e44SMagnus Damm 		gpio_rcar_write(p, INTCLR, BIT(hwirq));
131119f5e44SMagnus Damm 
132119f5e44SMagnus Damm 	spin_unlock_irqrestore(&p->lock, flags);
133119f5e44SMagnus Damm }
134119f5e44SMagnus Damm 
135119f5e44SMagnus Damm static int gpio_rcar_irq_set_type(struct irq_data *d, unsigned int type)
136119f5e44SMagnus Damm {
137c7f3c5d3SGeert Uytterhoeven 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
138c7b6f457SLinus Walleij 	struct gpio_rcar_priv *p = gpiochip_get_data(gc);
139119f5e44SMagnus Damm 	unsigned int hwirq = irqd_to_hwirq(d);
140119f5e44SMagnus Damm 
141119f5e44SMagnus Damm 	dev_dbg(&p->pdev->dev, "sense irq = %d, type = %d\n", hwirq, type);
142119f5e44SMagnus Damm 
143119f5e44SMagnus Damm 	switch (type & IRQ_TYPE_SENSE_MASK) {
144119f5e44SMagnus Damm 	case IRQ_TYPE_LEVEL_HIGH:
1457e1092b5SSimon Horman 		gpio_rcar_config_interrupt_input_mode(p, hwirq, true, true,
1467e1092b5SSimon Horman 						      false);
147119f5e44SMagnus Damm 		break;
148119f5e44SMagnus Damm 	case IRQ_TYPE_LEVEL_LOW:
1497e1092b5SSimon Horman 		gpio_rcar_config_interrupt_input_mode(p, hwirq, false, true,
1507e1092b5SSimon Horman 						      false);
151119f5e44SMagnus Damm 		break;
152119f5e44SMagnus Damm 	case IRQ_TYPE_EDGE_RISING:
1537e1092b5SSimon Horman 		gpio_rcar_config_interrupt_input_mode(p, hwirq, true, false,
1547e1092b5SSimon Horman 						      false);
155119f5e44SMagnus Damm 		break;
156119f5e44SMagnus Damm 	case IRQ_TYPE_EDGE_FALLING:
1577e1092b5SSimon Horman 		gpio_rcar_config_interrupt_input_mode(p, hwirq, false, false,
1587e1092b5SSimon Horman 						      false);
1597e1092b5SSimon Horman 		break;
1607e1092b5SSimon Horman 	case IRQ_TYPE_EDGE_BOTH:
1618b092be9SGeert Uytterhoeven 		if (!p->has_both_edge_trigger)
1627e1092b5SSimon Horman 			return -EINVAL;
1637e1092b5SSimon Horman 		gpio_rcar_config_interrupt_input_mode(p, hwirq, true, false,
1647e1092b5SSimon Horman 						      true);
165119f5e44SMagnus Damm 		break;
166119f5e44SMagnus Damm 	default:
167119f5e44SMagnus Damm 		return -EINVAL;
168119f5e44SMagnus Damm 	}
169119f5e44SMagnus Damm 	return 0;
170119f5e44SMagnus Damm }
171119f5e44SMagnus Damm 
172ab82fa7dSGeert Uytterhoeven static int gpio_rcar_irq_set_wake(struct irq_data *d, unsigned int on)
173ab82fa7dSGeert Uytterhoeven {
174ab82fa7dSGeert Uytterhoeven 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
175c7b6f457SLinus Walleij 	struct gpio_rcar_priv *p = gpiochip_get_data(gc);
176501ef0f9SGeert Uytterhoeven 	int error;
177ab82fa7dSGeert Uytterhoeven 
178501ef0f9SGeert Uytterhoeven 	if (p->irq_parent) {
179501ef0f9SGeert Uytterhoeven 		error = irq_set_irq_wake(p->irq_parent, on);
180501ef0f9SGeert Uytterhoeven 		if (error) {
181501ef0f9SGeert Uytterhoeven 			dev_dbg(&p->pdev->dev,
182501ef0f9SGeert Uytterhoeven 				"irq %u doesn't support irq_set_wake\n",
183501ef0f9SGeert Uytterhoeven 				p->irq_parent);
184501ef0f9SGeert Uytterhoeven 			p->irq_parent = 0;
185501ef0f9SGeert Uytterhoeven 		}
186501ef0f9SGeert Uytterhoeven 	}
187ab82fa7dSGeert Uytterhoeven 
188ab82fa7dSGeert Uytterhoeven 	if (!p->clk)
189ab82fa7dSGeert Uytterhoeven 		return 0;
190ab82fa7dSGeert Uytterhoeven 
191ab82fa7dSGeert Uytterhoeven 	if (on)
192ab82fa7dSGeert Uytterhoeven 		clk_enable(p->clk);
193ab82fa7dSGeert Uytterhoeven 	else
194ab82fa7dSGeert Uytterhoeven 		clk_disable(p->clk);
195ab82fa7dSGeert Uytterhoeven 
196ab82fa7dSGeert Uytterhoeven 	return 0;
197ab82fa7dSGeert Uytterhoeven }
198ab82fa7dSGeert Uytterhoeven 
199119f5e44SMagnus Damm static irqreturn_t gpio_rcar_irq_handler(int irq, void *dev_id)
200119f5e44SMagnus Damm {
201119f5e44SMagnus Damm 	struct gpio_rcar_priv *p = dev_id;
202119f5e44SMagnus Damm 	u32 pending;
203119f5e44SMagnus Damm 	unsigned int offset, irqs_handled = 0;
204119f5e44SMagnus Damm 
2058808b64dSValentine Barshak 	while ((pending = gpio_rcar_read(p, INTDT) &
2068808b64dSValentine Barshak 			  gpio_rcar_read(p, INTMSK))) {
207119f5e44SMagnus Damm 		offset = __ffs(pending);
208119f5e44SMagnus Damm 		gpio_rcar_write(p, INTCLR, BIT(offset));
209c7f3c5d3SGeert Uytterhoeven 		generic_handle_irq(irq_find_mapping(p->gpio_chip.irqdomain,
210c7f3c5d3SGeert Uytterhoeven 						    offset));
211119f5e44SMagnus Damm 		irqs_handled++;
212119f5e44SMagnus Damm 	}
213119f5e44SMagnus Damm 
214119f5e44SMagnus Damm 	return irqs_handled ? IRQ_HANDLED : IRQ_NONE;
215119f5e44SMagnus Damm }
216119f5e44SMagnus Damm 
217119f5e44SMagnus Damm static void gpio_rcar_config_general_input_output_mode(struct gpio_chip *chip,
218119f5e44SMagnus Damm 						       unsigned int gpio,
219119f5e44SMagnus Damm 						       bool output)
220119f5e44SMagnus Damm {
221c7b6f457SLinus Walleij 	struct gpio_rcar_priv *p = gpiochip_get_data(chip);
222119f5e44SMagnus Damm 	unsigned long flags;
223119f5e44SMagnus Damm 
224119f5e44SMagnus Damm 	/* follow steps in the GPIO documentation for
225119f5e44SMagnus Damm 	 * "Setting General Output Mode" and
226119f5e44SMagnus Damm 	 * "Setting General Input Mode"
227119f5e44SMagnus Damm 	 */
228119f5e44SMagnus Damm 
229119f5e44SMagnus Damm 	spin_lock_irqsave(&p->lock, flags);
230119f5e44SMagnus Damm 
231119f5e44SMagnus Damm 	/* Configure postive logic in POSNEG */
232119f5e44SMagnus Damm 	gpio_rcar_modify_bit(p, POSNEG, gpio, false);
233119f5e44SMagnus Damm 
234119f5e44SMagnus Damm 	/* Select "General Input/Output Mode" in IOINTSEL */
235119f5e44SMagnus Damm 	gpio_rcar_modify_bit(p, IOINTSEL, gpio, false);
236119f5e44SMagnus Damm 
237119f5e44SMagnus Damm 	/* Select Input Mode or Output Mode in INOUTSEL */
238119f5e44SMagnus Damm 	gpio_rcar_modify_bit(p, INOUTSEL, gpio, output);
239119f5e44SMagnus Damm 
240119f5e44SMagnus Damm 	spin_unlock_irqrestore(&p->lock, flags);
241119f5e44SMagnus Damm }
242119f5e44SMagnus Damm 
243dc3465a9SLaurent Pinchart static int gpio_rcar_request(struct gpio_chip *chip, unsigned offset)
244dc3465a9SLaurent Pinchart {
245ce0e2c60SLinus Walleij 	return pinctrl_request_gpio(chip->base + offset);
246dc3465a9SLaurent Pinchart }
247dc3465a9SLaurent Pinchart 
248dc3465a9SLaurent Pinchart static void gpio_rcar_free(struct gpio_chip *chip, unsigned offset)
249dc3465a9SLaurent Pinchart {
250dc3465a9SLaurent Pinchart 	pinctrl_free_gpio(chip->base + offset);
251dc3465a9SLaurent Pinchart 
252ce0e2c60SLinus Walleij 	/*
253ce0e2c60SLinus Walleij 	 * Set the GPIO as an input to ensure that the next GPIO request won't
254dc3465a9SLaurent Pinchart 	 * drive the GPIO pin as an output.
255dc3465a9SLaurent Pinchart 	 */
256dc3465a9SLaurent Pinchart 	gpio_rcar_config_general_input_output_mode(chip, offset, false);
257dc3465a9SLaurent Pinchart }
258dc3465a9SLaurent Pinchart 
259119f5e44SMagnus Damm static int gpio_rcar_direction_input(struct gpio_chip *chip, unsigned offset)
260119f5e44SMagnus Damm {
261119f5e44SMagnus Damm 	gpio_rcar_config_general_input_output_mode(chip, offset, false);
262119f5e44SMagnus Damm 	return 0;
263119f5e44SMagnus Damm }
264119f5e44SMagnus Damm 
265119f5e44SMagnus Damm static int gpio_rcar_get(struct gpio_chip *chip, unsigned offset)
266119f5e44SMagnus Damm {
267ae9550f6SMagnus Damm 	u32 bit = BIT(offset);
268ae9550f6SMagnus Damm 
269ae9550f6SMagnus Damm 	/* testing on r8a7790 shows that INDT does not show correct pin state
270ae9550f6SMagnus Damm 	 * when configured as output, so use OUTDT in case of output pins */
271c7b6f457SLinus Walleij 	if (gpio_rcar_read(gpiochip_get_data(chip), INOUTSEL) & bit)
272c7b6f457SLinus Walleij 		return !!(gpio_rcar_read(gpiochip_get_data(chip), OUTDT) & bit);
273ae9550f6SMagnus Damm 	else
274c7b6f457SLinus Walleij 		return !!(gpio_rcar_read(gpiochip_get_data(chip), INDT) & bit);
275119f5e44SMagnus Damm }
276119f5e44SMagnus Damm 
277119f5e44SMagnus Damm static void gpio_rcar_set(struct gpio_chip *chip, unsigned offset, int value)
278119f5e44SMagnus Damm {
279c7b6f457SLinus Walleij 	struct gpio_rcar_priv *p = gpiochip_get_data(chip);
280119f5e44SMagnus Damm 	unsigned long flags;
281119f5e44SMagnus Damm 
282119f5e44SMagnus Damm 	spin_lock_irqsave(&p->lock, flags);
283119f5e44SMagnus Damm 	gpio_rcar_modify_bit(p, OUTDT, offset, value);
284119f5e44SMagnus Damm 	spin_unlock_irqrestore(&p->lock, flags);
285119f5e44SMagnus Damm }
286119f5e44SMagnus Damm 
287119f5e44SMagnus Damm static int gpio_rcar_direction_output(struct gpio_chip *chip, unsigned offset,
288119f5e44SMagnus Damm 				      int value)
289119f5e44SMagnus Damm {
290119f5e44SMagnus Damm 	/* write GPIO value to output before selecting output mode of pin */
291119f5e44SMagnus Damm 	gpio_rcar_set(chip, offset, value);
292119f5e44SMagnus Damm 	gpio_rcar_config_general_input_output_mode(chip, offset, true);
293119f5e44SMagnus Damm 	return 0;
294119f5e44SMagnus Damm }
295119f5e44SMagnus Damm 
296850dfe17SLaurent Pinchart struct gpio_rcar_info {
297850dfe17SLaurent Pinchart 	bool has_both_edge_trigger;
298e1fef9e2SGeert Uytterhoeven 	bool needs_clk;
299850dfe17SLaurent Pinchart };
300850dfe17SLaurent Pinchart 
3011fd2b49dSHisashi Nakamura static const struct gpio_rcar_info gpio_rcar_info_gen1 = {
3021fd2b49dSHisashi Nakamura 	.has_both_edge_trigger = false,
303e1fef9e2SGeert Uytterhoeven 	.needs_clk = false,
3041fd2b49dSHisashi Nakamura };
3051fd2b49dSHisashi Nakamura 
3061fd2b49dSHisashi Nakamura static const struct gpio_rcar_info gpio_rcar_info_gen2 = {
3071fd2b49dSHisashi Nakamura 	.has_both_edge_trigger = true,
308e1fef9e2SGeert Uytterhoeven 	.needs_clk = true,
3091fd2b49dSHisashi Nakamura };
3101fd2b49dSHisashi Nakamura 
311850dfe17SLaurent Pinchart static const struct of_device_id gpio_rcar_of_table[] = {
312850dfe17SLaurent Pinchart 	{
313850dfe17SLaurent Pinchart 		.compatible = "renesas,gpio-r8a7790",
3141fd2b49dSHisashi Nakamura 		.data = &gpio_rcar_info_gen2,
315850dfe17SLaurent Pinchart 	}, {
316850dfe17SLaurent Pinchart 		.compatible = "renesas,gpio-r8a7791",
3171fd2b49dSHisashi Nakamura 		.data = &gpio_rcar_info_gen2,
3181fd2b49dSHisashi Nakamura 	}, {
3191fd2b49dSHisashi Nakamura 		.compatible = "renesas,gpio-r8a7793",
3201fd2b49dSHisashi Nakamura 		.data = &gpio_rcar_info_gen2,
3211fd2b49dSHisashi Nakamura 	}, {
3221fd2b49dSHisashi Nakamura 		.compatible = "renesas,gpio-r8a7794",
3231fd2b49dSHisashi Nakamura 		.data = &gpio_rcar_info_gen2,
324850dfe17SLaurent Pinchart 	}, {
3258cd14702SUlrich Hecht 		.compatible = "renesas,gpio-r8a7795",
3268cd14702SUlrich Hecht 		/* Gen3 GPIO is identical to Gen2. */
3278cd14702SUlrich Hecht 		.data = &gpio_rcar_info_gen2,
3288cd14702SUlrich Hecht 	}, {
329850dfe17SLaurent Pinchart 		.compatible = "renesas,gpio-rcar",
3301fd2b49dSHisashi Nakamura 		.data = &gpio_rcar_info_gen1,
331850dfe17SLaurent Pinchart 	}, {
332850dfe17SLaurent Pinchart 		/* Terminator */
333850dfe17SLaurent Pinchart 	},
334850dfe17SLaurent Pinchart };
335850dfe17SLaurent Pinchart 
336850dfe17SLaurent Pinchart MODULE_DEVICE_TABLE(of, gpio_rcar_of_table);
337850dfe17SLaurent Pinchart 
3388b092be9SGeert Uytterhoeven static int gpio_rcar_parse_dt(struct gpio_rcar_priv *p, unsigned int *npins)
339159f8a02SLaurent Pinchart {
340159f8a02SLaurent Pinchart 	struct device_node *np = p->pdev->dev.of_node;
341850dfe17SLaurent Pinchart 	const struct of_device_id *match;
342850dfe17SLaurent Pinchart 	const struct gpio_rcar_info *info;
3438b092be9SGeert Uytterhoeven 	struct of_phandle_args args;
3448b092be9SGeert Uytterhoeven 	int ret;
345850dfe17SLaurent Pinchart 
346850dfe17SLaurent Pinchart 	match = of_match_node(gpio_rcar_of_table, np);
347850dfe17SLaurent Pinchart 	if (!match)
348850dfe17SLaurent Pinchart 		return -EINVAL;
349850dfe17SLaurent Pinchart 
350850dfe17SLaurent Pinchart 	info = match->data;
351850dfe17SLaurent Pinchart 
3528b092be9SGeert Uytterhoeven 	ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3, 0, &args);
3538b092be9SGeert Uytterhoeven 	*npins = ret == 0 ? args.args[2] : RCAR_MAX_GPIO_PER_BANK;
3548b092be9SGeert Uytterhoeven 	p->has_both_edge_trigger = info->has_both_edge_trigger;
355e1fef9e2SGeert Uytterhoeven 	p->needs_clk = info->needs_clk;
356159f8a02SLaurent Pinchart 
3578b092be9SGeert Uytterhoeven 	if (*npins == 0 || *npins > RCAR_MAX_GPIO_PER_BANK) {
358159f8a02SLaurent Pinchart 		dev_warn(&p->pdev->dev,
3598b092be9SGeert Uytterhoeven 			 "Invalid number of gpio lines %u, using %u\n", *npins,
3608b092be9SGeert Uytterhoeven 			 RCAR_MAX_GPIO_PER_BANK);
3618b092be9SGeert Uytterhoeven 		*npins = RCAR_MAX_GPIO_PER_BANK;
362159f8a02SLaurent Pinchart 	}
363850dfe17SLaurent Pinchart 
364850dfe17SLaurent Pinchart 	return 0;
365159f8a02SLaurent Pinchart }
366159f8a02SLaurent Pinchart 
367119f5e44SMagnus Damm static int gpio_rcar_probe(struct platform_device *pdev)
368119f5e44SMagnus Damm {
369119f5e44SMagnus Damm 	struct gpio_rcar_priv *p;
370119f5e44SMagnus Damm 	struct resource *io, *irq;
371119f5e44SMagnus Damm 	struct gpio_chip *gpio_chip;
372119f5e44SMagnus Damm 	struct irq_chip *irq_chip;
373b22978fcSGeert Uytterhoeven 	struct device *dev = &pdev->dev;
374b22978fcSGeert Uytterhoeven 	const char *name = dev_name(dev);
3758b092be9SGeert Uytterhoeven 	unsigned int npins;
376119f5e44SMagnus Damm 	int ret;
377119f5e44SMagnus Damm 
378b22978fcSGeert Uytterhoeven 	p = devm_kzalloc(dev, sizeof(*p), GFP_KERNEL);
3797d82bf34SGeert Uytterhoeven 	if (!p)
3807d82bf34SGeert Uytterhoeven 		return -ENOMEM;
381119f5e44SMagnus Damm 
382119f5e44SMagnus Damm 	p->pdev = pdev;
383119f5e44SMagnus Damm 	spin_lock_init(&p->lock);
384119f5e44SMagnus Damm 
3858b092be9SGeert Uytterhoeven 	/* Get device configuration from DT node */
3868b092be9SGeert Uytterhoeven 	ret = gpio_rcar_parse_dt(p, &npins);
387850dfe17SLaurent Pinchart 	if (ret < 0)
388850dfe17SLaurent Pinchart 		return ret;
389159f8a02SLaurent Pinchart 
390159f8a02SLaurent Pinchart 	platform_set_drvdata(pdev, p);
391159f8a02SLaurent Pinchart 
392ab82fa7dSGeert Uytterhoeven 	p->clk = devm_clk_get(dev, NULL);
393ab82fa7dSGeert Uytterhoeven 	if (IS_ERR(p->clk)) {
394e1fef9e2SGeert Uytterhoeven 		if (p->needs_clk) {
395e1fef9e2SGeert Uytterhoeven 			dev_err(dev, "unable to get clock\n");
396e1fef9e2SGeert Uytterhoeven 			ret = PTR_ERR(p->clk);
397e1fef9e2SGeert Uytterhoeven 			goto err0;
398e1fef9e2SGeert Uytterhoeven 		}
399ab82fa7dSGeert Uytterhoeven 		p->clk = NULL;
400ab82fa7dSGeert Uytterhoeven 	}
401ab82fa7dSGeert Uytterhoeven 
402df0c6c80SGeert Uytterhoeven 	pm_runtime_enable(dev);
403ce0e2c60SLinus Walleij 	pm_runtime_get_sync(dev);
404df0c6c80SGeert Uytterhoeven 
405119f5e44SMagnus Damm 	io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
406119f5e44SMagnus Damm 	irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
407119f5e44SMagnus Damm 
408119f5e44SMagnus Damm 	if (!io || !irq) {
409b22978fcSGeert Uytterhoeven 		dev_err(dev, "missing IRQ or IOMEM\n");
410119f5e44SMagnus Damm 		ret = -EINVAL;
411119f5e44SMagnus Damm 		goto err0;
412119f5e44SMagnus Damm 	}
413119f5e44SMagnus Damm 
414b22978fcSGeert Uytterhoeven 	p->base = devm_ioremap_nocache(dev, io->start, resource_size(io));
415119f5e44SMagnus Damm 	if (!p->base) {
416b22978fcSGeert Uytterhoeven 		dev_err(dev, "failed to remap I/O memory\n");
417119f5e44SMagnus Damm 		ret = -ENXIO;
418119f5e44SMagnus Damm 		goto err0;
419119f5e44SMagnus Damm 	}
420119f5e44SMagnus Damm 
421119f5e44SMagnus Damm 	gpio_chip = &p->gpio_chip;
422dc3465a9SLaurent Pinchart 	gpio_chip->request = gpio_rcar_request;
423dc3465a9SLaurent Pinchart 	gpio_chip->free = gpio_rcar_free;
424119f5e44SMagnus Damm 	gpio_chip->direction_input = gpio_rcar_direction_input;
425119f5e44SMagnus Damm 	gpio_chip->get = gpio_rcar_get;
426119f5e44SMagnus Damm 	gpio_chip->direction_output = gpio_rcar_direction_output;
427119f5e44SMagnus Damm 	gpio_chip->set = gpio_rcar_set;
428119f5e44SMagnus Damm 	gpio_chip->label = name;
42958383c78SLinus Walleij 	gpio_chip->parent = dev;
430119f5e44SMagnus Damm 	gpio_chip->owner = THIS_MODULE;
4318b092be9SGeert Uytterhoeven 	gpio_chip->base = -1;
4328b092be9SGeert Uytterhoeven 	gpio_chip->ngpio = npins;
433119f5e44SMagnus Damm 
434119f5e44SMagnus Damm 	irq_chip = &p->irq_chip;
435119f5e44SMagnus Damm 	irq_chip->name = name;
436119f5e44SMagnus Damm 	irq_chip->irq_mask = gpio_rcar_irq_disable;
437119f5e44SMagnus Damm 	irq_chip->irq_unmask = gpio_rcar_irq_enable;
438119f5e44SMagnus Damm 	irq_chip->irq_set_type = gpio_rcar_irq_set_type;
439ab82fa7dSGeert Uytterhoeven 	irq_chip->irq_set_wake = gpio_rcar_irq_set_wake;
440ab82fa7dSGeert Uytterhoeven 	irq_chip->flags	= IRQCHIP_SET_TYPE_MASKED | IRQCHIP_MASK_ON_SUSPEND;
441119f5e44SMagnus Damm 
442c7b6f457SLinus Walleij 	ret = gpiochip_add_data(gpio_chip, p);
443c7f3c5d3SGeert Uytterhoeven 	if (ret) {
444c7f3c5d3SGeert Uytterhoeven 		dev_err(dev, "failed to add GPIO controller\n");
4450c8aab8eSDan Carpenter 		goto err0;
446119f5e44SMagnus Damm 	}
447119f5e44SMagnus Damm 
4488b092be9SGeert Uytterhoeven 	ret = gpiochip_irqchip_add(gpio_chip, irq_chip, 0, handle_level_irq,
4498b092be9SGeert Uytterhoeven 				   IRQ_TYPE_NONE);
450c7f3c5d3SGeert Uytterhoeven 	if (ret) {
451c7f3c5d3SGeert Uytterhoeven 		dev_err(dev, "cannot add irqchip\n");
452c7f3c5d3SGeert Uytterhoeven 		goto err1;
453c7f3c5d3SGeert Uytterhoeven 	}
454c7f3c5d3SGeert Uytterhoeven 
455ab82fa7dSGeert Uytterhoeven 	p->irq_parent = irq->start;
456b22978fcSGeert Uytterhoeven 	if (devm_request_irq(dev, irq->start, gpio_rcar_irq_handler,
457b22978fcSGeert Uytterhoeven 			     IRQF_SHARED, name, p)) {
458b22978fcSGeert Uytterhoeven 		dev_err(dev, "failed to request IRQ\n");
459119f5e44SMagnus Damm 		ret = -ENOENT;
460119f5e44SMagnus Damm 		goto err1;
461119f5e44SMagnus Damm 	}
462119f5e44SMagnus Damm 
4638b092be9SGeert Uytterhoeven 	dev_info(dev, "driving %d GPIOs\n", npins);
464dc3465a9SLaurent Pinchart 
465119f5e44SMagnus Damm 	return 0;
466119f5e44SMagnus Damm 
467119f5e44SMagnus Damm err1:
4684d84b9e4SGeert Uytterhoeven 	gpiochip_remove(gpio_chip);
469119f5e44SMagnus Damm err0:
470ce0e2c60SLinus Walleij 	pm_runtime_put(dev);
471df0c6c80SGeert Uytterhoeven 	pm_runtime_disable(dev);
472119f5e44SMagnus Damm 	return ret;
473119f5e44SMagnus Damm }
474119f5e44SMagnus Damm 
475119f5e44SMagnus Damm static int gpio_rcar_remove(struct platform_device *pdev)
476119f5e44SMagnus Damm {
477119f5e44SMagnus Damm 	struct gpio_rcar_priv *p = platform_get_drvdata(pdev);
478119f5e44SMagnus Damm 
4799f5132aeSabdoulaye berthe 	gpiochip_remove(&p->gpio_chip);
480119f5e44SMagnus Damm 
481ce0e2c60SLinus Walleij 	pm_runtime_put(&pdev->dev);
482df0c6c80SGeert Uytterhoeven 	pm_runtime_disable(&pdev->dev);
483119f5e44SMagnus Damm 	return 0;
484119f5e44SMagnus Damm }
485119f5e44SMagnus Damm 
486119f5e44SMagnus Damm static struct platform_driver gpio_rcar_device_driver = {
487119f5e44SMagnus Damm 	.probe		= gpio_rcar_probe,
488119f5e44SMagnus Damm 	.remove		= gpio_rcar_remove,
489119f5e44SMagnus Damm 	.driver		= {
490119f5e44SMagnus Damm 		.name	= "gpio_rcar",
491159f8a02SLaurent Pinchart 		.of_match_table = of_match_ptr(gpio_rcar_of_table),
492119f5e44SMagnus Damm 	}
493119f5e44SMagnus Damm };
494119f5e44SMagnus Damm 
495119f5e44SMagnus Damm module_platform_driver(gpio_rcar_device_driver);
496119f5e44SMagnus Damm 
497119f5e44SMagnus Damm MODULE_AUTHOR("Magnus Damm");
498119f5e44SMagnus Damm MODULE_DESCRIPTION("Renesas R-Car GPIO Driver");
499119f5e44SMagnus Damm MODULE_LICENSE("GPL v2");
500