xref: /openbmc/linux/drivers/gpio/gpio-rcar.c (revision 9f5132ae)
1119f5e44SMagnus Damm /*
2119f5e44SMagnus Damm  * Renesas R-Car GPIO Support
3119f5e44SMagnus Damm  *
4119f5e44SMagnus Damm  *  Copyright (C) 2013 Magnus Damm
5119f5e44SMagnus Damm  *
6119f5e44SMagnus Damm  * This program is free software; you can redistribute it and/or modify
7119f5e44SMagnus Damm  * it under the terms of the GNU General Public License as published by
8119f5e44SMagnus Damm  * the Free Software Foundation; either version 2 of the License
9119f5e44SMagnus Damm  *
10119f5e44SMagnus Damm  * This program is distributed in the hope that it will be useful,
11119f5e44SMagnus Damm  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12119f5e44SMagnus Damm  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13119f5e44SMagnus Damm  * GNU General Public License for more details.
14119f5e44SMagnus Damm  */
15119f5e44SMagnus Damm 
16119f5e44SMagnus Damm #include <linux/err.h>
17119f5e44SMagnus Damm #include <linux/gpio.h>
18119f5e44SMagnus Damm #include <linux/init.h>
19119f5e44SMagnus Damm #include <linux/interrupt.h>
20119f5e44SMagnus Damm #include <linux/io.h>
21119f5e44SMagnus Damm #include <linux/ioport.h>
22119f5e44SMagnus Damm #include <linux/irq.h>
23119f5e44SMagnus Damm #include <linux/irqdomain.h>
24119f5e44SMagnus Damm #include <linux/module.h>
25bd0bf468SSachin Kamat #include <linux/of.h>
26dc3465a9SLaurent Pinchart #include <linux/pinctrl/consumer.h>
27119f5e44SMagnus Damm #include <linux/platform_data/gpio-rcar.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 gpio_rcar_config config;
37119f5e44SMagnus Damm 	struct platform_device *pdev;
38119f5e44SMagnus Damm 	struct gpio_chip gpio_chip;
39119f5e44SMagnus Damm 	struct irq_chip irq_chip;
40119f5e44SMagnus Damm 	struct irq_domain *irq_domain;
41119f5e44SMagnus Damm };
42119f5e44SMagnus Damm 
43119f5e44SMagnus Damm #define IOINTSEL 0x00
44119f5e44SMagnus Damm #define INOUTSEL 0x04
45119f5e44SMagnus Damm #define OUTDT 0x08
46119f5e44SMagnus Damm #define INDT 0x0c
47119f5e44SMagnus Damm #define INTDT 0x10
48119f5e44SMagnus Damm #define INTCLR 0x14
49119f5e44SMagnus Damm #define INTMSK 0x18
50119f5e44SMagnus Damm #define MSKCLR 0x1c
51119f5e44SMagnus Damm #define POSNEG 0x20
52119f5e44SMagnus Damm #define EDGLEVEL 0x24
53119f5e44SMagnus Damm #define FILONOFF 0x28
547e1092b5SSimon Horman #define BOTHEDGE 0x4c
55119f5e44SMagnus Damm 
56159f8a02SLaurent Pinchart #define RCAR_MAX_GPIO_PER_BANK		32
57159f8a02SLaurent Pinchart 
58119f5e44SMagnus Damm static inline u32 gpio_rcar_read(struct gpio_rcar_priv *p, int offs)
59119f5e44SMagnus Damm {
60119f5e44SMagnus Damm 	return ioread32(p->base + offs);
61119f5e44SMagnus Damm }
62119f5e44SMagnus Damm 
63119f5e44SMagnus Damm static inline void gpio_rcar_write(struct gpio_rcar_priv *p, int offs,
64119f5e44SMagnus Damm 				   u32 value)
65119f5e44SMagnus Damm {
66119f5e44SMagnus Damm 	iowrite32(value, p->base + offs);
67119f5e44SMagnus Damm }
68119f5e44SMagnus Damm 
69119f5e44SMagnus Damm static void gpio_rcar_modify_bit(struct gpio_rcar_priv *p, int offs,
70119f5e44SMagnus Damm 				 int bit, bool value)
71119f5e44SMagnus Damm {
72119f5e44SMagnus Damm 	u32 tmp = gpio_rcar_read(p, offs);
73119f5e44SMagnus Damm 
74119f5e44SMagnus Damm 	if (value)
75119f5e44SMagnus Damm 		tmp |= BIT(bit);
76119f5e44SMagnus Damm 	else
77119f5e44SMagnus Damm 		tmp &= ~BIT(bit);
78119f5e44SMagnus Damm 
79119f5e44SMagnus Damm 	gpio_rcar_write(p, offs, tmp);
80119f5e44SMagnus Damm }
81119f5e44SMagnus Damm 
82119f5e44SMagnus Damm static void gpio_rcar_irq_disable(struct irq_data *d)
83119f5e44SMagnus Damm {
84119f5e44SMagnus Damm 	struct gpio_rcar_priv *p = irq_data_get_irq_chip_data(d);
85119f5e44SMagnus Damm 
86119f5e44SMagnus Damm 	gpio_rcar_write(p, INTMSK, ~BIT(irqd_to_hwirq(d)));
87119f5e44SMagnus Damm }
88119f5e44SMagnus Damm 
89119f5e44SMagnus Damm static void gpio_rcar_irq_enable(struct irq_data *d)
90119f5e44SMagnus Damm {
91119f5e44SMagnus Damm 	struct gpio_rcar_priv *p = irq_data_get_irq_chip_data(d);
92119f5e44SMagnus Damm 
93119f5e44SMagnus Damm 	gpio_rcar_write(p, MSKCLR, BIT(irqd_to_hwirq(d)));
94119f5e44SMagnus Damm }
95119f5e44SMagnus Damm 
96119f5e44SMagnus Damm static void gpio_rcar_config_interrupt_input_mode(struct gpio_rcar_priv *p,
97119f5e44SMagnus Damm 						  unsigned int hwirq,
98119f5e44SMagnus Damm 						  bool active_high_rising_edge,
997e1092b5SSimon Horman 						  bool level_trigger,
1007e1092b5SSimon Horman 						  bool both)
101119f5e44SMagnus Damm {
102119f5e44SMagnus Damm 	unsigned long flags;
103119f5e44SMagnus Damm 
104119f5e44SMagnus Damm 	/* follow steps in the GPIO documentation for
105119f5e44SMagnus Damm 	 * "Setting Edge-Sensitive Interrupt Input Mode" and
106119f5e44SMagnus Damm 	 * "Setting Level-Sensitive Interrupt Input Mode"
107119f5e44SMagnus Damm 	 */
108119f5e44SMagnus Damm 
109119f5e44SMagnus Damm 	spin_lock_irqsave(&p->lock, flags);
110119f5e44SMagnus Damm 
111119f5e44SMagnus Damm 	/* Configure postive or negative logic in POSNEG */
112119f5e44SMagnus Damm 	gpio_rcar_modify_bit(p, POSNEG, hwirq, !active_high_rising_edge);
113119f5e44SMagnus Damm 
114119f5e44SMagnus Damm 	/* Configure edge or level trigger in EDGLEVEL */
115119f5e44SMagnus Damm 	gpio_rcar_modify_bit(p, EDGLEVEL, hwirq, !level_trigger);
116119f5e44SMagnus Damm 
1177e1092b5SSimon Horman 	/* Select one edge or both edges in BOTHEDGE */
1187e1092b5SSimon Horman 	if (p->config.has_both_edge_trigger)
1197e1092b5SSimon Horman 		gpio_rcar_modify_bit(p, BOTHEDGE, hwirq, both);
1207e1092b5SSimon Horman 
121119f5e44SMagnus Damm 	/* Select "Interrupt Input Mode" in IOINTSEL */
122119f5e44SMagnus Damm 	gpio_rcar_modify_bit(p, IOINTSEL, hwirq, true);
123119f5e44SMagnus Damm 
124119f5e44SMagnus Damm 	/* Write INTCLR in case of edge trigger */
125119f5e44SMagnus Damm 	if (!level_trigger)
126119f5e44SMagnus Damm 		gpio_rcar_write(p, INTCLR, BIT(hwirq));
127119f5e44SMagnus Damm 
128119f5e44SMagnus Damm 	spin_unlock_irqrestore(&p->lock, flags);
129119f5e44SMagnus Damm }
130119f5e44SMagnus Damm 
131119f5e44SMagnus Damm static int gpio_rcar_irq_set_type(struct irq_data *d, unsigned int type)
132119f5e44SMagnus Damm {
133119f5e44SMagnus Damm 	struct gpio_rcar_priv *p = irq_data_get_irq_chip_data(d);
134119f5e44SMagnus Damm 	unsigned int hwirq = irqd_to_hwirq(d);
135119f5e44SMagnus Damm 
136119f5e44SMagnus Damm 	dev_dbg(&p->pdev->dev, "sense irq = %d, type = %d\n", hwirq, type);
137119f5e44SMagnus Damm 
138119f5e44SMagnus Damm 	switch (type & IRQ_TYPE_SENSE_MASK) {
139119f5e44SMagnus Damm 	case IRQ_TYPE_LEVEL_HIGH:
1407e1092b5SSimon Horman 		gpio_rcar_config_interrupt_input_mode(p, hwirq, true, true,
1417e1092b5SSimon Horman 						      false);
142119f5e44SMagnus Damm 		break;
143119f5e44SMagnus Damm 	case IRQ_TYPE_LEVEL_LOW:
1447e1092b5SSimon Horman 		gpio_rcar_config_interrupt_input_mode(p, hwirq, false, true,
1457e1092b5SSimon Horman 						      false);
146119f5e44SMagnus Damm 		break;
147119f5e44SMagnus Damm 	case IRQ_TYPE_EDGE_RISING:
1487e1092b5SSimon Horman 		gpio_rcar_config_interrupt_input_mode(p, hwirq, true, false,
1497e1092b5SSimon Horman 						      false);
150119f5e44SMagnus Damm 		break;
151119f5e44SMagnus Damm 	case IRQ_TYPE_EDGE_FALLING:
1527e1092b5SSimon Horman 		gpio_rcar_config_interrupt_input_mode(p, hwirq, false, false,
1537e1092b5SSimon Horman 						      false);
1547e1092b5SSimon Horman 		break;
1557e1092b5SSimon Horman 	case IRQ_TYPE_EDGE_BOTH:
1567e1092b5SSimon Horman 		if (!p->config.has_both_edge_trigger)
1577e1092b5SSimon Horman 			return -EINVAL;
1587e1092b5SSimon Horman 		gpio_rcar_config_interrupt_input_mode(p, hwirq, true, false,
1597e1092b5SSimon Horman 						      true);
160119f5e44SMagnus Damm 		break;
161119f5e44SMagnus Damm 	default:
162119f5e44SMagnus Damm 		return -EINVAL;
163119f5e44SMagnus Damm 	}
164119f5e44SMagnus Damm 	return 0;
165119f5e44SMagnus Damm }
166119f5e44SMagnus Damm 
167119f5e44SMagnus Damm static irqreturn_t gpio_rcar_irq_handler(int irq, void *dev_id)
168119f5e44SMagnus Damm {
169119f5e44SMagnus Damm 	struct gpio_rcar_priv *p = dev_id;
170119f5e44SMagnus Damm 	u32 pending;
171119f5e44SMagnus Damm 	unsigned int offset, irqs_handled = 0;
172119f5e44SMagnus Damm 
1738808b64dSValentine Barshak 	while ((pending = gpio_rcar_read(p, INTDT) &
1748808b64dSValentine Barshak 			  gpio_rcar_read(p, INTMSK))) {
175119f5e44SMagnus Damm 		offset = __ffs(pending);
176119f5e44SMagnus Damm 		gpio_rcar_write(p, INTCLR, BIT(offset));
177119f5e44SMagnus Damm 		generic_handle_irq(irq_find_mapping(p->irq_domain, offset));
178119f5e44SMagnus Damm 		irqs_handled++;
179119f5e44SMagnus Damm 	}
180119f5e44SMagnus Damm 
181119f5e44SMagnus Damm 	return irqs_handled ? IRQ_HANDLED : IRQ_NONE;
182119f5e44SMagnus Damm }
183119f5e44SMagnus Damm 
184119f5e44SMagnus Damm static inline struct gpio_rcar_priv *gpio_to_priv(struct gpio_chip *chip)
185119f5e44SMagnus Damm {
186119f5e44SMagnus Damm 	return container_of(chip, struct gpio_rcar_priv, gpio_chip);
187119f5e44SMagnus Damm }
188119f5e44SMagnus Damm 
189119f5e44SMagnus Damm static void gpio_rcar_config_general_input_output_mode(struct gpio_chip *chip,
190119f5e44SMagnus Damm 						       unsigned int gpio,
191119f5e44SMagnus Damm 						       bool output)
192119f5e44SMagnus Damm {
193119f5e44SMagnus Damm 	struct gpio_rcar_priv *p = gpio_to_priv(chip);
194119f5e44SMagnus Damm 	unsigned long flags;
195119f5e44SMagnus Damm 
196119f5e44SMagnus Damm 	/* follow steps in the GPIO documentation for
197119f5e44SMagnus Damm 	 * "Setting General Output Mode" and
198119f5e44SMagnus Damm 	 * "Setting General Input Mode"
199119f5e44SMagnus Damm 	 */
200119f5e44SMagnus Damm 
201119f5e44SMagnus Damm 	spin_lock_irqsave(&p->lock, flags);
202119f5e44SMagnus Damm 
203119f5e44SMagnus Damm 	/* Configure postive logic in POSNEG */
204119f5e44SMagnus Damm 	gpio_rcar_modify_bit(p, POSNEG, gpio, false);
205119f5e44SMagnus Damm 
206119f5e44SMagnus Damm 	/* Select "General Input/Output Mode" in IOINTSEL */
207119f5e44SMagnus Damm 	gpio_rcar_modify_bit(p, IOINTSEL, gpio, false);
208119f5e44SMagnus Damm 
209119f5e44SMagnus Damm 	/* Select Input Mode or Output Mode in INOUTSEL */
210119f5e44SMagnus Damm 	gpio_rcar_modify_bit(p, INOUTSEL, gpio, output);
211119f5e44SMagnus Damm 
212119f5e44SMagnus Damm 	spin_unlock_irqrestore(&p->lock, flags);
213119f5e44SMagnus Damm }
214119f5e44SMagnus Damm 
215dc3465a9SLaurent Pinchart static int gpio_rcar_request(struct gpio_chip *chip, unsigned offset)
216dc3465a9SLaurent Pinchart {
217dc3465a9SLaurent Pinchart 	return pinctrl_request_gpio(chip->base + offset);
218dc3465a9SLaurent Pinchart }
219dc3465a9SLaurent Pinchart 
220dc3465a9SLaurent Pinchart static void gpio_rcar_free(struct gpio_chip *chip, unsigned offset)
221dc3465a9SLaurent Pinchart {
222dc3465a9SLaurent Pinchart 	pinctrl_free_gpio(chip->base + offset);
223dc3465a9SLaurent Pinchart 
224dc3465a9SLaurent Pinchart 	/* Set the GPIO as an input to ensure that the next GPIO request won't
225dc3465a9SLaurent Pinchart 	 * drive the GPIO pin as an output.
226dc3465a9SLaurent Pinchart 	 */
227dc3465a9SLaurent Pinchart 	gpio_rcar_config_general_input_output_mode(chip, offset, false);
228dc3465a9SLaurent Pinchart }
229dc3465a9SLaurent Pinchart 
230119f5e44SMagnus Damm static int gpio_rcar_direction_input(struct gpio_chip *chip, unsigned offset)
231119f5e44SMagnus Damm {
232119f5e44SMagnus Damm 	gpio_rcar_config_general_input_output_mode(chip, offset, false);
233119f5e44SMagnus Damm 	return 0;
234119f5e44SMagnus Damm }
235119f5e44SMagnus Damm 
236119f5e44SMagnus Damm static int gpio_rcar_get(struct gpio_chip *chip, unsigned offset)
237119f5e44SMagnus Damm {
238ae9550f6SMagnus Damm 	u32 bit = BIT(offset);
239ae9550f6SMagnus Damm 
240ae9550f6SMagnus Damm 	/* testing on r8a7790 shows that INDT does not show correct pin state
241ae9550f6SMagnus Damm 	 * when configured as output, so use OUTDT in case of output pins */
242ae9550f6SMagnus Damm 	if (gpio_rcar_read(gpio_to_priv(chip), INOUTSEL) & bit)
2437cb5409bSJürg Billeter 		return !!(gpio_rcar_read(gpio_to_priv(chip), OUTDT) & bit);
244ae9550f6SMagnus Damm 	else
2457cb5409bSJürg Billeter 		return !!(gpio_rcar_read(gpio_to_priv(chip), INDT) & bit);
246119f5e44SMagnus Damm }
247119f5e44SMagnus Damm 
248119f5e44SMagnus Damm static void gpio_rcar_set(struct gpio_chip *chip, unsigned offset, int value)
249119f5e44SMagnus Damm {
250119f5e44SMagnus Damm 	struct gpio_rcar_priv *p = gpio_to_priv(chip);
251119f5e44SMagnus Damm 	unsigned long flags;
252119f5e44SMagnus Damm 
253119f5e44SMagnus Damm 	spin_lock_irqsave(&p->lock, flags);
254119f5e44SMagnus Damm 	gpio_rcar_modify_bit(p, OUTDT, offset, value);
255119f5e44SMagnus Damm 	spin_unlock_irqrestore(&p->lock, flags);
256119f5e44SMagnus Damm }
257119f5e44SMagnus Damm 
258119f5e44SMagnus Damm static int gpio_rcar_direction_output(struct gpio_chip *chip, unsigned offset,
259119f5e44SMagnus Damm 				      int value)
260119f5e44SMagnus Damm {
261119f5e44SMagnus Damm 	/* write GPIO value to output before selecting output mode of pin */
262119f5e44SMagnus Damm 	gpio_rcar_set(chip, offset, value);
263119f5e44SMagnus Damm 	gpio_rcar_config_general_input_output_mode(chip, offset, true);
264119f5e44SMagnus Damm 	return 0;
265119f5e44SMagnus Damm }
266119f5e44SMagnus Damm 
267119f5e44SMagnus Damm static int gpio_rcar_to_irq(struct gpio_chip *chip, unsigned offset)
268119f5e44SMagnus Damm {
269119f5e44SMagnus Damm 	return irq_create_mapping(gpio_to_priv(chip)->irq_domain, offset);
270119f5e44SMagnus Damm }
271119f5e44SMagnus Damm 
272c0d6c1adSLinus Walleij static int gpio_rcar_irq_domain_map(struct irq_domain *h, unsigned int irq,
273c0d6c1adSLinus Walleij 				 irq_hw_number_t hwirq)
274119f5e44SMagnus Damm {
275119f5e44SMagnus Damm 	struct gpio_rcar_priv *p = h->host_data;
276119f5e44SMagnus Damm 
277c0d6c1adSLinus Walleij 	dev_dbg(&p->pdev->dev, "map hw irq = %d, irq = %d\n", (int)hwirq, irq);
278119f5e44SMagnus Damm 
279c0d6c1adSLinus Walleij 	irq_set_chip_data(irq, h->host_data);
280c0d6c1adSLinus Walleij 	irq_set_chip_and_handler(irq, &p->irq_chip, handle_level_irq);
281c0d6c1adSLinus Walleij 	set_irq_flags(irq, IRQF_VALID); /* kill me now */
282119f5e44SMagnus Damm 	return 0;
283119f5e44SMagnus Damm }
284119f5e44SMagnus Damm 
285119f5e44SMagnus Damm static struct irq_domain_ops gpio_rcar_irq_domain_ops = {
286119f5e44SMagnus Damm 	.map	= gpio_rcar_irq_domain_map,
287119f5e44SMagnus Damm };
288119f5e44SMagnus Damm 
289850dfe17SLaurent Pinchart struct gpio_rcar_info {
290850dfe17SLaurent Pinchart 	bool has_both_edge_trigger;
291850dfe17SLaurent Pinchart };
292850dfe17SLaurent Pinchart 
293850dfe17SLaurent Pinchart static const struct of_device_id gpio_rcar_of_table[] = {
294850dfe17SLaurent Pinchart 	{
295850dfe17SLaurent Pinchart 		.compatible = "renesas,gpio-r8a7790",
296850dfe17SLaurent Pinchart 		.data = (void *)&(const struct gpio_rcar_info) {
297850dfe17SLaurent Pinchart 			.has_both_edge_trigger = true,
298850dfe17SLaurent Pinchart 		},
299850dfe17SLaurent Pinchart 	}, {
300850dfe17SLaurent Pinchart 		.compatible = "renesas,gpio-r8a7791",
301850dfe17SLaurent Pinchart 		.data = (void *)&(const struct gpio_rcar_info) {
302850dfe17SLaurent Pinchart 			.has_both_edge_trigger = true,
303850dfe17SLaurent Pinchart 		},
304850dfe17SLaurent Pinchart 	}, {
305850dfe17SLaurent Pinchart 		.compatible = "renesas,gpio-rcar",
306850dfe17SLaurent Pinchart 		.data = (void *)&(const struct gpio_rcar_info) {
307850dfe17SLaurent Pinchart 			.has_both_edge_trigger = false,
308850dfe17SLaurent Pinchart 		},
309850dfe17SLaurent Pinchart 	}, {
310850dfe17SLaurent Pinchart 		/* Terminator */
311850dfe17SLaurent Pinchart 	},
312850dfe17SLaurent Pinchart };
313850dfe17SLaurent Pinchart 
314850dfe17SLaurent Pinchart MODULE_DEVICE_TABLE(of, gpio_rcar_of_table);
315850dfe17SLaurent Pinchart 
316850dfe17SLaurent Pinchart static int gpio_rcar_parse_pdata(struct gpio_rcar_priv *p)
317159f8a02SLaurent Pinchart {
318e56aee18SJingoo Han 	struct gpio_rcar_config *pdata = dev_get_platdata(&p->pdev->dev);
319159f8a02SLaurent Pinchart 	struct device_node *np = p->pdev->dev.of_node;
320159f8a02SLaurent Pinchart 	struct of_phandle_args args;
321159f8a02SLaurent Pinchart 	int ret;
322159f8a02SLaurent Pinchart 
323e305062eSLaurent Pinchart 	if (pdata) {
324159f8a02SLaurent Pinchart 		p->config = *pdata;
325e305062eSLaurent Pinchart 	} else if (IS_ENABLED(CONFIG_OF) && np) {
326850dfe17SLaurent Pinchart 		const struct of_device_id *match;
327850dfe17SLaurent Pinchart 		const struct gpio_rcar_info *info;
328850dfe17SLaurent Pinchart 
329850dfe17SLaurent Pinchart 		match = of_match_node(gpio_rcar_of_table, np);
330850dfe17SLaurent Pinchart 		if (!match)
331850dfe17SLaurent Pinchart 			return -EINVAL;
332850dfe17SLaurent Pinchart 
333850dfe17SLaurent Pinchart 		info = match->data;
334850dfe17SLaurent Pinchart 
33501eb2d18SLaurent Pinchart 		ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3, 0,
33601eb2d18SLaurent Pinchart 						       &args);
33701eb2d18SLaurent Pinchart 		p->config.number_of_pins = ret == 0 ? args.args[2]
338159f8a02SLaurent Pinchart 					 : RCAR_MAX_GPIO_PER_BANK;
339159f8a02SLaurent Pinchart 		p->config.gpio_base = -1;
340850dfe17SLaurent Pinchart 		p->config.has_both_edge_trigger = info->has_both_edge_trigger;
341159f8a02SLaurent Pinchart 	}
342159f8a02SLaurent Pinchart 
343159f8a02SLaurent Pinchart 	if (p->config.number_of_pins == 0 ||
344159f8a02SLaurent Pinchart 	    p->config.number_of_pins > RCAR_MAX_GPIO_PER_BANK) {
345159f8a02SLaurent Pinchart 		dev_warn(&p->pdev->dev,
346159f8a02SLaurent Pinchart 			 "Invalid number of gpio lines %u, using %u\n",
347159f8a02SLaurent Pinchart 			 p->config.number_of_pins, RCAR_MAX_GPIO_PER_BANK);
348159f8a02SLaurent Pinchart 		p->config.number_of_pins = RCAR_MAX_GPIO_PER_BANK;
349159f8a02SLaurent Pinchart 	}
350850dfe17SLaurent Pinchart 
351850dfe17SLaurent Pinchart 	return 0;
352159f8a02SLaurent Pinchart }
353159f8a02SLaurent Pinchart 
354119f5e44SMagnus Damm static int gpio_rcar_probe(struct platform_device *pdev)
355119f5e44SMagnus Damm {
356119f5e44SMagnus Damm 	struct gpio_rcar_priv *p;
357119f5e44SMagnus Damm 	struct resource *io, *irq;
358119f5e44SMagnus Damm 	struct gpio_chip *gpio_chip;
359119f5e44SMagnus Damm 	struct irq_chip *irq_chip;
360b22978fcSGeert Uytterhoeven 	struct device *dev = &pdev->dev;
361b22978fcSGeert Uytterhoeven 	const char *name = dev_name(dev);
362119f5e44SMagnus Damm 	int ret;
363119f5e44SMagnus Damm 
364b22978fcSGeert Uytterhoeven 	p = devm_kzalloc(dev, sizeof(*p), GFP_KERNEL);
365119f5e44SMagnus Damm 	if (!p) {
366119f5e44SMagnus Damm 		ret = -ENOMEM;
367119f5e44SMagnus Damm 		goto err0;
368119f5e44SMagnus Damm 	}
369119f5e44SMagnus Damm 
370119f5e44SMagnus Damm 	p->pdev = pdev;
371119f5e44SMagnus Damm 	spin_lock_init(&p->lock);
372119f5e44SMagnus Damm 
373159f8a02SLaurent Pinchart 	/* Get device configuration from DT node or platform data. */
374850dfe17SLaurent Pinchart 	ret = gpio_rcar_parse_pdata(p);
375850dfe17SLaurent Pinchart 	if (ret < 0)
376850dfe17SLaurent Pinchart 		return ret;
377159f8a02SLaurent Pinchart 
378159f8a02SLaurent Pinchart 	platform_set_drvdata(pdev, p);
379159f8a02SLaurent Pinchart 
380df0c6c80SGeert Uytterhoeven 	pm_runtime_enable(dev);
381df0c6c80SGeert Uytterhoeven 	pm_runtime_get_sync(dev);
382df0c6c80SGeert Uytterhoeven 
383119f5e44SMagnus Damm 	io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
384119f5e44SMagnus Damm 	irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
385119f5e44SMagnus Damm 
386119f5e44SMagnus Damm 	if (!io || !irq) {
387b22978fcSGeert Uytterhoeven 		dev_err(dev, "missing IRQ or IOMEM\n");
388119f5e44SMagnus Damm 		ret = -EINVAL;
389119f5e44SMagnus Damm 		goto err0;
390119f5e44SMagnus Damm 	}
391119f5e44SMagnus Damm 
392b22978fcSGeert Uytterhoeven 	p->base = devm_ioremap_nocache(dev, io->start, resource_size(io));
393119f5e44SMagnus Damm 	if (!p->base) {
394b22978fcSGeert Uytterhoeven 		dev_err(dev, "failed to remap I/O memory\n");
395119f5e44SMagnus Damm 		ret = -ENXIO;
396119f5e44SMagnus Damm 		goto err0;
397119f5e44SMagnus Damm 	}
398119f5e44SMagnus Damm 
399119f5e44SMagnus Damm 	gpio_chip = &p->gpio_chip;
400dc3465a9SLaurent Pinchart 	gpio_chip->request = gpio_rcar_request;
401dc3465a9SLaurent Pinchart 	gpio_chip->free = gpio_rcar_free;
402119f5e44SMagnus Damm 	gpio_chip->direction_input = gpio_rcar_direction_input;
403119f5e44SMagnus Damm 	gpio_chip->get = gpio_rcar_get;
404119f5e44SMagnus Damm 	gpio_chip->direction_output = gpio_rcar_direction_output;
405119f5e44SMagnus Damm 	gpio_chip->set = gpio_rcar_set;
406119f5e44SMagnus Damm 	gpio_chip->to_irq = gpio_rcar_to_irq;
407119f5e44SMagnus Damm 	gpio_chip->label = name;
408b22978fcSGeert Uytterhoeven 	gpio_chip->dev = dev;
409119f5e44SMagnus Damm 	gpio_chip->owner = THIS_MODULE;
410119f5e44SMagnus Damm 	gpio_chip->base = p->config.gpio_base;
411119f5e44SMagnus Damm 	gpio_chip->ngpio = p->config.number_of_pins;
412119f5e44SMagnus Damm 
413119f5e44SMagnus Damm 	irq_chip = &p->irq_chip;
414119f5e44SMagnus Damm 	irq_chip->name = name;
415119f5e44SMagnus Damm 	irq_chip->irq_mask = gpio_rcar_irq_disable;
416119f5e44SMagnus Damm 	irq_chip->irq_unmask = gpio_rcar_irq_enable;
417119f5e44SMagnus Damm 	irq_chip->irq_set_type = gpio_rcar_irq_set_type;
41840396112SMagnus Damm 	irq_chip->flags	= IRQCHIP_SKIP_SET_WAKE | IRQCHIP_SET_TYPE_MASKED
41940396112SMagnus Damm 			 | IRQCHIP_MASK_ON_SUSPEND;
420119f5e44SMagnus Damm 
421119f5e44SMagnus Damm 	p->irq_domain = irq_domain_add_simple(pdev->dev.of_node,
422119f5e44SMagnus Damm 					      p->config.number_of_pins,
423119f5e44SMagnus Damm 					      p->config.irq_base,
424119f5e44SMagnus Damm 					      &gpio_rcar_irq_domain_ops, p);
425119f5e44SMagnus Damm 	if (!p->irq_domain) {
426119f5e44SMagnus Damm 		ret = -ENXIO;
427b22978fcSGeert Uytterhoeven 		dev_err(dev, "cannot initialize irq domain\n");
4280c8aab8eSDan Carpenter 		goto err0;
429119f5e44SMagnus Damm 	}
430119f5e44SMagnus Damm 
431b22978fcSGeert Uytterhoeven 	if (devm_request_irq(dev, irq->start, gpio_rcar_irq_handler,
432b22978fcSGeert Uytterhoeven 			     IRQF_SHARED, name, p)) {
433b22978fcSGeert Uytterhoeven 		dev_err(dev, "failed to request IRQ\n");
434119f5e44SMagnus Damm 		ret = -ENOENT;
435119f5e44SMagnus Damm 		goto err1;
436119f5e44SMagnus Damm 	}
437119f5e44SMagnus Damm 
438119f5e44SMagnus Damm 	ret = gpiochip_add(gpio_chip);
439119f5e44SMagnus Damm 	if (ret) {
440b22978fcSGeert Uytterhoeven 		dev_err(dev, "failed to add GPIO controller\n");
441119f5e44SMagnus Damm 		goto err1;
442119f5e44SMagnus Damm 	}
443119f5e44SMagnus Damm 
444b22978fcSGeert Uytterhoeven 	dev_info(dev, "driving %d GPIOs\n", p->config.number_of_pins);
445119f5e44SMagnus Damm 
446119f5e44SMagnus Damm 	/* warn in case of mismatch if irq base is specified */
447119f5e44SMagnus Damm 	if (p->config.irq_base) {
448119f5e44SMagnus Damm 		ret = irq_find_mapping(p->irq_domain, 0);
449119f5e44SMagnus Damm 		if (p->config.irq_base != ret)
450b22978fcSGeert Uytterhoeven 			dev_warn(dev, "irq base mismatch (%u/%u)\n",
451119f5e44SMagnus Damm 				 p->config.irq_base, ret);
452119f5e44SMagnus Damm 	}
453119f5e44SMagnus Damm 
454159f8a02SLaurent Pinchart 	if (p->config.pctl_name) {
455dc3465a9SLaurent Pinchart 		ret = gpiochip_add_pin_range(gpio_chip, p->config.pctl_name, 0,
456dc3465a9SLaurent Pinchart 					     gpio_chip->base, gpio_chip->ngpio);
457dc3465a9SLaurent Pinchart 		if (ret < 0)
458b22978fcSGeert Uytterhoeven 			dev_warn(dev, "failed to add pin range\n");
459159f8a02SLaurent Pinchart 	}
460dc3465a9SLaurent Pinchart 
461119f5e44SMagnus Damm 	return 0;
462119f5e44SMagnus Damm 
463119f5e44SMagnus Damm err1:
464119f5e44SMagnus Damm 	irq_domain_remove(p->irq_domain);
465119f5e44SMagnus Damm err0:
466df0c6c80SGeert Uytterhoeven 	pm_runtime_put(dev);
467df0c6c80SGeert Uytterhoeven 	pm_runtime_disable(dev);
468119f5e44SMagnus Damm 	return ret;
469119f5e44SMagnus Damm }
470119f5e44SMagnus Damm 
471119f5e44SMagnus Damm static int gpio_rcar_remove(struct platform_device *pdev)
472119f5e44SMagnus Damm {
473119f5e44SMagnus Damm 	struct gpio_rcar_priv *p = platform_get_drvdata(pdev);
474119f5e44SMagnus Damm 
4759f5132aeSabdoulaye berthe 	gpiochip_remove(&p->gpio_chip);
476119f5e44SMagnus Damm 
477119f5e44SMagnus Damm 	irq_domain_remove(p->irq_domain);
478df0c6c80SGeert Uytterhoeven 	pm_runtime_put(&pdev->dev);
479df0c6c80SGeert Uytterhoeven 	pm_runtime_disable(&pdev->dev);
480119f5e44SMagnus Damm 	return 0;
481119f5e44SMagnus Damm }
482119f5e44SMagnus Damm 
483119f5e44SMagnus Damm static struct platform_driver gpio_rcar_device_driver = {
484119f5e44SMagnus Damm 	.probe		= gpio_rcar_probe,
485119f5e44SMagnus Damm 	.remove		= gpio_rcar_remove,
486119f5e44SMagnus Damm 	.driver		= {
487119f5e44SMagnus Damm 		.name	= "gpio_rcar",
488159f8a02SLaurent Pinchart 		.of_match_table = of_match_ptr(gpio_rcar_of_table),
489119f5e44SMagnus Damm 	}
490119f5e44SMagnus Damm };
491119f5e44SMagnus Damm 
492119f5e44SMagnus Damm module_platform_driver(gpio_rcar_device_driver);
493119f5e44SMagnus Damm 
494119f5e44SMagnus Damm MODULE_AUTHOR("Magnus Damm");
495119f5e44SMagnus Damm MODULE_DESCRIPTION("Renesas R-Car GPIO Driver");
496119f5e44SMagnus Damm MODULE_LICENSE("GPL v2");
497