xref: /openbmc/linux/drivers/gpio/gpio-rcar.c (revision b22978fc)
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>
29119f5e44SMagnus Damm #include <linux/spinlock.h>
30119f5e44SMagnus Damm #include <linux/slab.h>
31119f5e44SMagnus Damm 
32119f5e44SMagnus Damm struct gpio_rcar_priv {
33119f5e44SMagnus Damm 	void __iomem *base;
34119f5e44SMagnus Damm 	spinlock_t lock;
35119f5e44SMagnus Damm 	struct gpio_rcar_config config;
36119f5e44SMagnus Damm 	struct platform_device *pdev;
37119f5e44SMagnus Damm 	struct gpio_chip gpio_chip;
38119f5e44SMagnus Damm 	struct irq_chip irq_chip;
39119f5e44SMagnus Damm 	struct irq_domain *irq_domain;
40119f5e44SMagnus Damm };
41119f5e44SMagnus Damm 
42119f5e44SMagnus Damm #define IOINTSEL 0x00
43119f5e44SMagnus Damm #define INOUTSEL 0x04
44119f5e44SMagnus Damm #define OUTDT 0x08
45119f5e44SMagnus Damm #define INDT 0x0c
46119f5e44SMagnus Damm #define INTDT 0x10
47119f5e44SMagnus Damm #define INTCLR 0x14
48119f5e44SMagnus Damm #define INTMSK 0x18
49119f5e44SMagnus Damm #define MSKCLR 0x1c
50119f5e44SMagnus Damm #define POSNEG 0x20
51119f5e44SMagnus Damm #define EDGLEVEL 0x24
52119f5e44SMagnus Damm #define FILONOFF 0x28
537e1092b5SSimon Horman #define BOTHEDGE 0x4c
54119f5e44SMagnus Damm 
55159f8a02SLaurent Pinchart #define RCAR_MAX_GPIO_PER_BANK		32
56159f8a02SLaurent Pinchart 
57119f5e44SMagnus Damm static inline u32 gpio_rcar_read(struct gpio_rcar_priv *p, int offs)
58119f5e44SMagnus Damm {
59119f5e44SMagnus Damm 	return ioread32(p->base + offs);
60119f5e44SMagnus Damm }
61119f5e44SMagnus Damm 
62119f5e44SMagnus Damm static inline void gpio_rcar_write(struct gpio_rcar_priv *p, int offs,
63119f5e44SMagnus Damm 				   u32 value)
64119f5e44SMagnus Damm {
65119f5e44SMagnus Damm 	iowrite32(value, p->base + offs);
66119f5e44SMagnus Damm }
67119f5e44SMagnus Damm 
68119f5e44SMagnus Damm static void gpio_rcar_modify_bit(struct gpio_rcar_priv *p, int offs,
69119f5e44SMagnus Damm 				 int bit, bool value)
70119f5e44SMagnus Damm {
71119f5e44SMagnus Damm 	u32 tmp = gpio_rcar_read(p, offs);
72119f5e44SMagnus Damm 
73119f5e44SMagnus Damm 	if (value)
74119f5e44SMagnus Damm 		tmp |= BIT(bit);
75119f5e44SMagnus Damm 	else
76119f5e44SMagnus Damm 		tmp &= ~BIT(bit);
77119f5e44SMagnus Damm 
78119f5e44SMagnus Damm 	gpio_rcar_write(p, offs, tmp);
79119f5e44SMagnus Damm }
80119f5e44SMagnus Damm 
81119f5e44SMagnus Damm static void gpio_rcar_irq_disable(struct irq_data *d)
82119f5e44SMagnus Damm {
83119f5e44SMagnus Damm 	struct gpio_rcar_priv *p = irq_data_get_irq_chip_data(d);
84119f5e44SMagnus Damm 
85119f5e44SMagnus Damm 	gpio_rcar_write(p, INTMSK, ~BIT(irqd_to_hwirq(d)));
86119f5e44SMagnus Damm }
87119f5e44SMagnus Damm 
88119f5e44SMagnus Damm static void gpio_rcar_irq_enable(struct irq_data *d)
89119f5e44SMagnus Damm {
90119f5e44SMagnus Damm 	struct gpio_rcar_priv *p = irq_data_get_irq_chip_data(d);
91119f5e44SMagnus Damm 
92119f5e44SMagnus Damm 	gpio_rcar_write(p, MSKCLR, BIT(irqd_to_hwirq(d)));
93119f5e44SMagnus Damm }
94119f5e44SMagnus Damm 
95119f5e44SMagnus Damm static void gpio_rcar_config_interrupt_input_mode(struct gpio_rcar_priv *p,
96119f5e44SMagnus Damm 						  unsigned int hwirq,
97119f5e44SMagnus Damm 						  bool active_high_rising_edge,
987e1092b5SSimon Horman 						  bool level_trigger,
997e1092b5SSimon Horman 						  bool both)
100119f5e44SMagnus Damm {
101119f5e44SMagnus Damm 	unsigned long flags;
102119f5e44SMagnus Damm 
103119f5e44SMagnus Damm 	/* follow steps in the GPIO documentation for
104119f5e44SMagnus Damm 	 * "Setting Edge-Sensitive Interrupt Input Mode" and
105119f5e44SMagnus Damm 	 * "Setting Level-Sensitive Interrupt Input Mode"
106119f5e44SMagnus Damm 	 */
107119f5e44SMagnus Damm 
108119f5e44SMagnus Damm 	spin_lock_irqsave(&p->lock, flags);
109119f5e44SMagnus Damm 
110119f5e44SMagnus Damm 	/* Configure postive or negative logic in POSNEG */
111119f5e44SMagnus Damm 	gpio_rcar_modify_bit(p, POSNEG, hwirq, !active_high_rising_edge);
112119f5e44SMagnus Damm 
113119f5e44SMagnus Damm 	/* Configure edge or level trigger in EDGLEVEL */
114119f5e44SMagnus Damm 	gpio_rcar_modify_bit(p, EDGLEVEL, hwirq, !level_trigger);
115119f5e44SMagnus Damm 
1167e1092b5SSimon Horman 	/* Select one edge or both edges in BOTHEDGE */
1177e1092b5SSimon Horman 	if (p->config.has_both_edge_trigger)
1187e1092b5SSimon Horman 		gpio_rcar_modify_bit(p, BOTHEDGE, hwirq, both);
1197e1092b5SSimon Horman 
120119f5e44SMagnus Damm 	/* Select "Interrupt Input Mode" in IOINTSEL */
121119f5e44SMagnus Damm 	gpio_rcar_modify_bit(p, IOINTSEL, hwirq, true);
122119f5e44SMagnus Damm 
123119f5e44SMagnus Damm 	/* Write INTCLR in case of edge trigger */
124119f5e44SMagnus Damm 	if (!level_trigger)
125119f5e44SMagnus Damm 		gpio_rcar_write(p, INTCLR, BIT(hwirq));
126119f5e44SMagnus Damm 
127119f5e44SMagnus Damm 	spin_unlock_irqrestore(&p->lock, flags);
128119f5e44SMagnus Damm }
129119f5e44SMagnus Damm 
130119f5e44SMagnus Damm static int gpio_rcar_irq_set_type(struct irq_data *d, unsigned int type)
131119f5e44SMagnus Damm {
132119f5e44SMagnus Damm 	struct gpio_rcar_priv *p = irq_data_get_irq_chip_data(d);
133119f5e44SMagnus Damm 	unsigned int hwirq = irqd_to_hwirq(d);
134119f5e44SMagnus Damm 
135119f5e44SMagnus Damm 	dev_dbg(&p->pdev->dev, "sense irq = %d, type = %d\n", hwirq, type);
136119f5e44SMagnus Damm 
137119f5e44SMagnus Damm 	switch (type & IRQ_TYPE_SENSE_MASK) {
138119f5e44SMagnus Damm 	case IRQ_TYPE_LEVEL_HIGH:
1397e1092b5SSimon Horman 		gpio_rcar_config_interrupt_input_mode(p, hwirq, true, true,
1407e1092b5SSimon Horman 						      false);
141119f5e44SMagnus Damm 		break;
142119f5e44SMagnus Damm 	case IRQ_TYPE_LEVEL_LOW:
1437e1092b5SSimon Horman 		gpio_rcar_config_interrupt_input_mode(p, hwirq, false, true,
1447e1092b5SSimon Horman 						      false);
145119f5e44SMagnus Damm 		break;
146119f5e44SMagnus Damm 	case IRQ_TYPE_EDGE_RISING:
1477e1092b5SSimon Horman 		gpio_rcar_config_interrupt_input_mode(p, hwirq, true, false,
1487e1092b5SSimon Horman 						      false);
149119f5e44SMagnus Damm 		break;
150119f5e44SMagnus Damm 	case IRQ_TYPE_EDGE_FALLING:
1517e1092b5SSimon Horman 		gpio_rcar_config_interrupt_input_mode(p, hwirq, false, false,
1527e1092b5SSimon Horman 						      false);
1537e1092b5SSimon Horman 		break;
1547e1092b5SSimon Horman 	case IRQ_TYPE_EDGE_BOTH:
1557e1092b5SSimon Horman 		if (!p->config.has_both_edge_trigger)
1567e1092b5SSimon Horman 			return -EINVAL;
1577e1092b5SSimon Horman 		gpio_rcar_config_interrupt_input_mode(p, hwirq, true, false,
1587e1092b5SSimon Horman 						      true);
159119f5e44SMagnus Damm 		break;
160119f5e44SMagnus Damm 	default:
161119f5e44SMagnus Damm 		return -EINVAL;
162119f5e44SMagnus Damm 	}
163119f5e44SMagnus Damm 	return 0;
164119f5e44SMagnus Damm }
165119f5e44SMagnus Damm 
166119f5e44SMagnus Damm static irqreturn_t gpio_rcar_irq_handler(int irq, void *dev_id)
167119f5e44SMagnus Damm {
168119f5e44SMagnus Damm 	struct gpio_rcar_priv *p = dev_id;
169119f5e44SMagnus Damm 	u32 pending;
170119f5e44SMagnus Damm 	unsigned int offset, irqs_handled = 0;
171119f5e44SMagnus Damm 
1728808b64dSValentine Barshak 	while ((pending = gpio_rcar_read(p, INTDT) &
1738808b64dSValentine Barshak 			  gpio_rcar_read(p, INTMSK))) {
174119f5e44SMagnus Damm 		offset = __ffs(pending);
175119f5e44SMagnus Damm 		gpio_rcar_write(p, INTCLR, BIT(offset));
176119f5e44SMagnus Damm 		generic_handle_irq(irq_find_mapping(p->irq_domain, offset));
177119f5e44SMagnus Damm 		irqs_handled++;
178119f5e44SMagnus Damm 	}
179119f5e44SMagnus Damm 
180119f5e44SMagnus Damm 	return irqs_handled ? IRQ_HANDLED : IRQ_NONE;
181119f5e44SMagnus Damm }
182119f5e44SMagnus Damm 
183119f5e44SMagnus Damm static inline struct gpio_rcar_priv *gpio_to_priv(struct gpio_chip *chip)
184119f5e44SMagnus Damm {
185119f5e44SMagnus Damm 	return container_of(chip, struct gpio_rcar_priv, gpio_chip);
186119f5e44SMagnus Damm }
187119f5e44SMagnus Damm 
188119f5e44SMagnus Damm static void gpio_rcar_config_general_input_output_mode(struct gpio_chip *chip,
189119f5e44SMagnus Damm 						       unsigned int gpio,
190119f5e44SMagnus Damm 						       bool output)
191119f5e44SMagnus Damm {
192119f5e44SMagnus Damm 	struct gpio_rcar_priv *p = gpio_to_priv(chip);
193119f5e44SMagnus Damm 	unsigned long flags;
194119f5e44SMagnus Damm 
195119f5e44SMagnus Damm 	/* follow steps in the GPIO documentation for
196119f5e44SMagnus Damm 	 * "Setting General Output Mode" and
197119f5e44SMagnus Damm 	 * "Setting General Input Mode"
198119f5e44SMagnus Damm 	 */
199119f5e44SMagnus Damm 
200119f5e44SMagnus Damm 	spin_lock_irqsave(&p->lock, flags);
201119f5e44SMagnus Damm 
202119f5e44SMagnus Damm 	/* Configure postive logic in POSNEG */
203119f5e44SMagnus Damm 	gpio_rcar_modify_bit(p, POSNEG, gpio, false);
204119f5e44SMagnus Damm 
205119f5e44SMagnus Damm 	/* Select "General Input/Output Mode" in IOINTSEL */
206119f5e44SMagnus Damm 	gpio_rcar_modify_bit(p, IOINTSEL, gpio, false);
207119f5e44SMagnus Damm 
208119f5e44SMagnus Damm 	/* Select Input Mode or Output Mode in INOUTSEL */
209119f5e44SMagnus Damm 	gpio_rcar_modify_bit(p, INOUTSEL, gpio, output);
210119f5e44SMagnus Damm 
211119f5e44SMagnus Damm 	spin_unlock_irqrestore(&p->lock, flags);
212119f5e44SMagnus Damm }
213119f5e44SMagnus Damm 
214dc3465a9SLaurent Pinchart static int gpio_rcar_request(struct gpio_chip *chip, unsigned offset)
215dc3465a9SLaurent Pinchart {
216dc3465a9SLaurent Pinchart 	return pinctrl_request_gpio(chip->base + offset);
217dc3465a9SLaurent Pinchart }
218dc3465a9SLaurent Pinchart 
219dc3465a9SLaurent Pinchart static void gpio_rcar_free(struct gpio_chip *chip, unsigned offset)
220dc3465a9SLaurent Pinchart {
221dc3465a9SLaurent Pinchart 	pinctrl_free_gpio(chip->base + offset);
222dc3465a9SLaurent Pinchart 
223dc3465a9SLaurent Pinchart 	/* Set the GPIO as an input to ensure that the next GPIO request won't
224dc3465a9SLaurent Pinchart 	 * drive the GPIO pin as an output.
225dc3465a9SLaurent Pinchart 	 */
226dc3465a9SLaurent Pinchart 	gpio_rcar_config_general_input_output_mode(chip, offset, false);
227dc3465a9SLaurent Pinchart }
228dc3465a9SLaurent Pinchart 
229119f5e44SMagnus Damm static int gpio_rcar_direction_input(struct gpio_chip *chip, unsigned offset)
230119f5e44SMagnus Damm {
231119f5e44SMagnus Damm 	gpio_rcar_config_general_input_output_mode(chip, offset, false);
232119f5e44SMagnus Damm 	return 0;
233119f5e44SMagnus Damm }
234119f5e44SMagnus Damm 
235119f5e44SMagnus Damm static int gpio_rcar_get(struct gpio_chip *chip, unsigned offset)
236119f5e44SMagnus Damm {
237ae9550f6SMagnus Damm 	u32 bit = BIT(offset);
238ae9550f6SMagnus Damm 
239ae9550f6SMagnus Damm 	/* testing on r8a7790 shows that INDT does not show correct pin state
240ae9550f6SMagnus Damm 	 * when configured as output, so use OUTDT in case of output pins */
241ae9550f6SMagnus Damm 	if (gpio_rcar_read(gpio_to_priv(chip), INOUTSEL) & bit)
242ae9550f6SMagnus Damm 		return (int)(gpio_rcar_read(gpio_to_priv(chip), OUTDT) & bit);
243ae9550f6SMagnus Damm 	else
244ae9550f6SMagnus Damm 		return (int)(gpio_rcar_read(gpio_to_priv(chip), INDT) & bit);
245119f5e44SMagnus Damm }
246119f5e44SMagnus Damm 
247119f5e44SMagnus Damm static void gpio_rcar_set(struct gpio_chip *chip, unsigned offset, int value)
248119f5e44SMagnus Damm {
249119f5e44SMagnus Damm 	struct gpio_rcar_priv *p = gpio_to_priv(chip);
250119f5e44SMagnus Damm 	unsigned long flags;
251119f5e44SMagnus Damm 
252119f5e44SMagnus Damm 	spin_lock_irqsave(&p->lock, flags);
253119f5e44SMagnus Damm 	gpio_rcar_modify_bit(p, OUTDT, offset, value);
254119f5e44SMagnus Damm 	spin_unlock_irqrestore(&p->lock, flags);
255119f5e44SMagnus Damm }
256119f5e44SMagnus Damm 
257119f5e44SMagnus Damm static int gpio_rcar_direction_output(struct gpio_chip *chip, unsigned offset,
258119f5e44SMagnus Damm 				      int value)
259119f5e44SMagnus Damm {
260119f5e44SMagnus Damm 	/* write GPIO value to output before selecting output mode of pin */
261119f5e44SMagnus Damm 	gpio_rcar_set(chip, offset, value);
262119f5e44SMagnus Damm 	gpio_rcar_config_general_input_output_mode(chip, offset, true);
263119f5e44SMagnus Damm 	return 0;
264119f5e44SMagnus Damm }
265119f5e44SMagnus Damm 
266119f5e44SMagnus Damm static int gpio_rcar_to_irq(struct gpio_chip *chip, unsigned offset)
267119f5e44SMagnus Damm {
268119f5e44SMagnus Damm 	return irq_create_mapping(gpio_to_priv(chip)->irq_domain, offset);
269119f5e44SMagnus Damm }
270119f5e44SMagnus Damm 
271c0d6c1adSLinus Walleij static int gpio_rcar_irq_domain_map(struct irq_domain *h, unsigned int irq,
272c0d6c1adSLinus Walleij 				 irq_hw_number_t hwirq)
273119f5e44SMagnus Damm {
274119f5e44SMagnus Damm 	struct gpio_rcar_priv *p = h->host_data;
275119f5e44SMagnus Damm 
276c0d6c1adSLinus Walleij 	dev_dbg(&p->pdev->dev, "map hw irq = %d, irq = %d\n", (int)hwirq, irq);
277119f5e44SMagnus Damm 
278c0d6c1adSLinus Walleij 	irq_set_chip_data(irq, h->host_data);
279c0d6c1adSLinus Walleij 	irq_set_chip_and_handler(irq, &p->irq_chip, handle_level_irq);
280c0d6c1adSLinus Walleij 	set_irq_flags(irq, IRQF_VALID); /* kill me now */
281119f5e44SMagnus Damm 	return 0;
282119f5e44SMagnus Damm }
283119f5e44SMagnus Damm 
284119f5e44SMagnus Damm static struct irq_domain_ops gpio_rcar_irq_domain_ops = {
285119f5e44SMagnus Damm 	.map	= gpio_rcar_irq_domain_map,
286119f5e44SMagnus Damm };
287119f5e44SMagnus Damm 
288850dfe17SLaurent Pinchart struct gpio_rcar_info {
289850dfe17SLaurent Pinchart 	bool has_both_edge_trigger;
290850dfe17SLaurent Pinchart };
291850dfe17SLaurent Pinchart 
292850dfe17SLaurent Pinchart static const struct of_device_id gpio_rcar_of_table[] = {
293850dfe17SLaurent Pinchart 	{
294850dfe17SLaurent Pinchart 		.compatible = "renesas,gpio-r8a7790",
295850dfe17SLaurent Pinchart 		.data = (void *)&(const struct gpio_rcar_info) {
296850dfe17SLaurent Pinchart 			.has_both_edge_trigger = true,
297850dfe17SLaurent Pinchart 		},
298850dfe17SLaurent Pinchart 	}, {
299850dfe17SLaurent Pinchart 		.compatible = "renesas,gpio-r8a7791",
300850dfe17SLaurent Pinchart 		.data = (void *)&(const struct gpio_rcar_info) {
301850dfe17SLaurent Pinchart 			.has_both_edge_trigger = true,
302850dfe17SLaurent Pinchart 		},
303850dfe17SLaurent Pinchart 	}, {
304850dfe17SLaurent Pinchart 		.compatible = "renesas,gpio-rcar",
305850dfe17SLaurent Pinchart 		.data = (void *)&(const struct gpio_rcar_info) {
306850dfe17SLaurent Pinchart 			.has_both_edge_trigger = false,
307850dfe17SLaurent Pinchart 		},
308850dfe17SLaurent Pinchart 	}, {
309850dfe17SLaurent Pinchart 		/* Terminator */
310850dfe17SLaurent Pinchart 	},
311850dfe17SLaurent Pinchart };
312850dfe17SLaurent Pinchart 
313850dfe17SLaurent Pinchart MODULE_DEVICE_TABLE(of, gpio_rcar_of_table);
314850dfe17SLaurent Pinchart 
315850dfe17SLaurent Pinchart static int gpio_rcar_parse_pdata(struct gpio_rcar_priv *p)
316159f8a02SLaurent Pinchart {
317e56aee18SJingoo Han 	struct gpio_rcar_config *pdata = dev_get_platdata(&p->pdev->dev);
318159f8a02SLaurent Pinchart 	struct device_node *np = p->pdev->dev.of_node;
319159f8a02SLaurent Pinchart 	struct of_phandle_args args;
320159f8a02SLaurent Pinchart 	int ret;
321159f8a02SLaurent Pinchart 
322e305062eSLaurent Pinchart 	if (pdata) {
323159f8a02SLaurent Pinchart 		p->config = *pdata;
324e305062eSLaurent Pinchart 	} else if (IS_ENABLED(CONFIG_OF) && np) {
325850dfe17SLaurent Pinchart 		const struct of_device_id *match;
326850dfe17SLaurent Pinchart 		const struct gpio_rcar_info *info;
327850dfe17SLaurent Pinchart 
328850dfe17SLaurent Pinchart 		match = of_match_node(gpio_rcar_of_table, np);
329850dfe17SLaurent Pinchart 		if (!match)
330850dfe17SLaurent Pinchart 			return -EINVAL;
331850dfe17SLaurent Pinchart 
332850dfe17SLaurent Pinchart 		info = match->data;
333850dfe17SLaurent Pinchart 
33401eb2d18SLaurent Pinchart 		ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3, 0,
33501eb2d18SLaurent Pinchart 						       &args);
33601eb2d18SLaurent Pinchart 		p->config.number_of_pins = ret == 0 ? args.args[2]
337159f8a02SLaurent Pinchart 					 : RCAR_MAX_GPIO_PER_BANK;
338159f8a02SLaurent Pinchart 		p->config.gpio_base = -1;
339850dfe17SLaurent Pinchart 		p->config.has_both_edge_trigger = info->has_both_edge_trigger;
340159f8a02SLaurent Pinchart 	}
341159f8a02SLaurent Pinchart 
342159f8a02SLaurent Pinchart 	if (p->config.number_of_pins == 0 ||
343159f8a02SLaurent Pinchart 	    p->config.number_of_pins > RCAR_MAX_GPIO_PER_BANK) {
344159f8a02SLaurent Pinchart 		dev_warn(&p->pdev->dev,
345159f8a02SLaurent Pinchart 			 "Invalid number of gpio lines %u, using %u\n",
346159f8a02SLaurent Pinchart 			 p->config.number_of_pins, RCAR_MAX_GPIO_PER_BANK);
347159f8a02SLaurent Pinchart 		p->config.number_of_pins = RCAR_MAX_GPIO_PER_BANK;
348159f8a02SLaurent Pinchart 	}
349850dfe17SLaurent Pinchart 
350850dfe17SLaurent Pinchart 	return 0;
351159f8a02SLaurent Pinchart }
352159f8a02SLaurent Pinchart 
353119f5e44SMagnus Damm static int gpio_rcar_probe(struct platform_device *pdev)
354119f5e44SMagnus Damm {
355119f5e44SMagnus Damm 	struct gpio_rcar_priv *p;
356119f5e44SMagnus Damm 	struct resource *io, *irq;
357119f5e44SMagnus Damm 	struct gpio_chip *gpio_chip;
358119f5e44SMagnus Damm 	struct irq_chip *irq_chip;
359b22978fcSGeert Uytterhoeven 	struct device *dev = &pdev->dev;
360b22978fcSGeert Uytterhoeven 	const char *name = dev_name(dev);
361119f5e44SMagnus Damm 	int ret;
362119f5e44SMagnus Damm 
363b22978fcSGeert Uytterhoeven 	p = devm_kzalloc(dev, sizeof(*p), GFP_KERNEL);
364119f5e44SMagnus Damm 	if (!p) {
365b22978fcSGeert Uytterhoeven 		dev_err(dev, "failed to allocate driver data\n");
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 
380119f5e44SMagnus Damm 	io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
381119f5e44SMagnus Damm 	irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
382119f5e44SMagnus Damm 
383119f5e44SMagnus Damm 	if (!io || !irq) {
384b22978fcSGeert Uytterhoeven 		dev_err(dev, "missing IRQ or IOMEM\n");
385119f5e44SMagnus Damm 		ret = -EINVAL;
386119f5e44SMagnus Damm 		goto err0;
387119f5e44SMagnus Damm 	}
388119f5e44SMagnus Damm 
389b22978fcSGeert Uytterhoeven 	p->base = devm_ioremap_nocache(dev, io->start, resource_size(io));
390119f5e44SMagnus Damm 	if (!p->base) {
391b22978fcSGeert Uytterhoeven 		dev_err(dev, "failed to remap I/O memory\n");
392119f5e44SMagnus Damm 		ret = -ENXIO;
393119f5e44SMagnus Damm 		goto err0;
394119f5e44SMagnus Damm 	}
395119f5e44SMagnus Damm 
396119f5e44SMagnus Damm 	gpio_chip = &p->gpio_chip;
397dc3465a9SLaurent Pinchart 	gpio_chip->request = gpio_rcar_request;
398dc3465a9SLaurent Pinchart 	gpio_chip->free = gpio_rcar_free;
399119f5e44SMagnus Damm 	gpio_chip->direction_input = gpio_rcar_direction_input;
400119f5e44SMagnus Damm 	gpio_chip->get = gpio_rcar_get;
401119f5e44SMagnus Damm 	gpio_chip->direction_output = gpio_rcar_direction_output;
402119f5e44SMagnus Damm 	gpio_chip->set = gpio_rcar_set;
403119f5e44SMagnus Damm 	gpio_chip->to_irq = gpio_rcar_to_irq;
404119f5e44SMagnus Damm 	gpio_chip->label = name;
405b22978fcSGeert Uytterhoeven 	gpio_chip->dev = dev;
406119f5e44SMagnus Damm 	gpio_chip->owner = THIS_MODULE;
407119f5e44SMagnus Damm 	gpio_chip->base = p->config.gpio_base;
408119f5e44SMagnus Damm 	gpio_chip->ngpio = p->config.number_of_pins;
409119f5e44SMagnus Damm 
410119f5e44SMagnus Damm 	irq_chip = &p->irq_chip;
411119f5e44SMagnus Damm 	irq_chip->name = name;
412119f5e44SMagnus Damm 	irq_chip->irq_mask = gpio_rcar_irq_disable;
413119f5e44SMagnus Damm 	irq_chip->irq_unmask = gpio_rcar_irq_enable;
414119f5e44SMagnus Damm 	irq_chip->irq_set_type = gpio_rcar_irq_set_type;
41540396112SMagnus Damm 	irq_chip->flags	= IRQCHIP_SKIP_SET_WAKE | IRQCHIP_SET_TYPE_MASKED
41640396112SMagnus Damm 			 | IRQCHIP_MASK_ON_SUSPEND;
417119f5e44SMagnus Damm 
418119f5e44SMagnus Damm 	p->irq_domain = irq_domain_add_simple(pdev->dev.of_node,
419119f5e44SMagnus Damm 					      p->config.number_of_pins,
420119f5e44SMagnus Damm 					      p->config.irq_base,
421119f5e44SMagnus Damm 					      &gpio_rcar_irq_domain_ops, p);
422119f5e44SMagnus Damm 	if (!p->irq_domain) {
423119f5e44SMagnus Damm 		ret = -ENXIO;
424b22978fcSGeert Uytterhoeven 		dev_err(dev, "cannot initialize irq domain\n");
4250c8aab8eSDan Carpenter 		goto err0;
426119f5e44SMagnus Damm 	}
427119f5e44SMagnus Damm 
428b22978fcSGeert Uytterhoeven 	if (devm_request_irq(dev, irq->start, gpio_rcar_irq_handler,
429b22978fcSGeert Uytterhoeven 			     IRQF_SHARED, name, p)) {
430b22978fcSGeert Uytterhoeven 		dev_err(dev, "failed to request IRQ\n");
431119f5e44SMagnus Damm 		ret = -ENOENT;
432119f5e44SMagnus Damm 		goto err1;
433119f5e44SMagnus Damm 	}
434119f5e44SMagnus Damm 
435119f5e44SMagnus Damm 	ret = gpiochip_add(gpio_chip);
436119f5e44SMagnus Damm 	if (ret) {
437b22978fcSGeert Uytterhoeven 		dev_err(dev, "failed to add GPIO controller\n");
438119f5e44SMagnus Damm 		goto err1;
439119f5e44SMagnus Damm 	}
440119f5e44SMagnus Damm 
441b22978fcSGeert Uytterhoeven 	dev_info(dev, "driving %d GPIOs\n", p->config.number_of_pins);
442119f5e44SMagnus Damm 
443119f5e44SMagnus Damm 	/* warn in case of mismatch if irq base is specified */
444119f5e44SMagnus Damm 	if (p->config.irq_base) {
445119f5e44SMagnus Damm 		ret = irq_find_mapping(p->irq_domain, 0);
446119f5e44SMagnus Damm 		if (p->config.irq_base != ret)
447b22978fcSGeert Uytterhoeven 			dev_warn(dev, "irq base mismatch (%u/%u)\n",
448119f5e44SMagnus Damm 				 p->config.irq_base, ret);
449119f5e44SMagnus Damm 	}
450119f5e44SMagnus Damm 
451159f8a02SLaurent Pinchart 	if (p->config.pctl_name) {
452dc3465a9SLaurent Pinchart 		ret = gpiochip_add_pin_range(gpio_chip, p->config.pctl_name, 0,
453dc3465a9SLaurent Pinchart 					     gpio_chip->base, gpio_chip->ngpio);
454dc3465a9SLaurent Pinchart 		if (ret < 0)
455b22978fcSGeert Uytterhoeven 			dev_warn(dev, "failed to add pin range\n");
456159f8a02SLaurent Pinchart 	}
457dc3465a9SLaurent Pinchart 
458119f5e44SMagnus Damm 	return 0;
459119f5e44SMagnus Damm 
460119f5e44SMagnus Damm err1:
461119f5e44SMagnus Damm 	irq_domain_remove(p->irq_domain);
462119f5e44SMagnus Damm err0:
463119f5e44SMagnus Damm 	return ret;
464119f5e44SMagnus Damm }
465119f5e44SMagnus Damm 
466119f5e44SMagnus Damm static int gpio_rcar_remove(struct platform_device *pdev)
467119f5e44SMagnus Damm {
468119f5e44SMagnus Damm 	struct gpio_rcar_priv *p = platform_get_drvdata(pdev);
469119f5e44SMagnus Damm 	int ret;
470119f5e44SMagnus Damm 
471119f5e44SMagnus Damm 	ret = gpiochip_remove(&p->gpio_chip);
472119f5e44SMagnus Damm 	if (ret)
473119f5e44SMagnus Damm 		return ret;
474119f5e44SMagnus Damm 
475119f5e44SMagnus Damm 	irq_domain_remove(p->irq_domain);
476119f5e44SMagnus Damm 	return 0;
477119f5e44SMagnus Damm }
478119f5e44SMagnus Damm 
479119f5e44SMagnus Damm static struct platform_driver gpio_rcar_device_driver = {
480119f5e44SMagnus Damm 	.probe		= gpio_rcar_probe,
481119f5e44SMagnus Damm 	.remove		= gpio_rcar_remove,
482119f5e44SMagnus Damm 	.driver		= {
483119f5e44SMagnus Damm 		.name	= "gpio_rcar",
484159f8a02SLaurent Pinchart 		.of_match_table = of_match_ptr(gpio_rcar_of_table),
485119f5e44SMagnus Damm 	}
486119f5e44SMagnus Damm };
487119f5e44SMagnus Damm 
488119f5e44SMagnus Damm module_platform_driver(gpio_rcar_device_driver);
489119f5e44SMagnus Damm 
490119f5e44SMagnus Damm MODULE_AUTHOR("Magnus Damm");
491119f5e44SMagnus Damm MODULE_DESCRIPTION("Renesas R-Car GPIO Driver");
492119f5e44SMagnus Damm MODULE_LICENSE("GPL v2");
493