xref: /openbmc/linux/drivers/gpio/gpio-rcar.c (revision ab82fa7d)
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_data/gpio-rcar.h>
29119f5e44SMagnus Damm #include <linux/platform_device.h>
30df0c6c80SGeert Uytterhoeven #include <linux/pm_runtime.h>
31119f5e44SMagnus Damm #include <linux/spinlock.h>
32119f5e44SMagnus Damm #include <linux/slab.h>
33119f5e44SMagnus Damm 
34119f5e44SMagnus Damm struct gpio_rcar_priv {
35119f5e44SMagnus Damm 	void __iomem *base;
36119f5e44SMagnus Damm 	spinlock_t lock;
37119f5e44SMagnus Damm 	struct gpio_rcar_config config;
38119f5e44SMagnus Damm 	struct platform_device *pdev;
39119f5e44SMagnus Damm 	struct gpio_chip gpio_chip;
40119f5e44SMagnus Damm 	struct irq_chip irq_chip;
41ab82fa7dSGeert Uytterhoeven 	unsigned int irq_parent;
42ab82fa7dSGeert Uytterhoeven 	struct clk *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);
87c7f3c5d3SGeert Uytterhoeven 	struct gpio_rcar_priv *p = container_of(gc, struct gpio_rcar_priv,
88c7f3c5d3SGeert Uytterhoeven 						gpio_chip);
89119f5e44SMagnus Damm 
90119f5e44SMagnus Damm 	gpio_rcar_write(p, INTMSK, ~BIT(irqd_to_hwirq(d)));
91119f5e44SMagnus Damm }
92119f5e44SMagnus Damm 
93119f5e44SMagnus Damm static void gpio_rcar_irq_enable(struct irq_data *d)
94119f5e44SMagnus Damm {
95c7f3c5d3SGeert Uytterhoeven 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
96c7f3c5d3SGeert Uytterhoeven 	struct gpio_rcar_priv *p = container_of(gc, struct gpio_rcar_priv,
97c7f3c5d3SGeert Uytterhoeven 						gpio_chip);
98119f5e44SMagnus Damm 
99119f5e44SMagnus Damm 	gpio_rcar_write(p, MSKCLR, BIT(irqd_to_hwirq(d)));
100119f5e44SMagnus Damm }
101119f5e44SMagnus Damm 
102119f5e44SMagnus Damm static void gpio_rcar_config_interrupt_input_mode(struct gpio_rcar_priv *p,
103119f5e44SMagnus Damm 						  unsigned int hwirq,
104119f5e44SMagnus Damm 						  bool active_high_rising_edge,
1057e1092b5SSimon Horman 						  bool level_trigger,
1067e1092b5SSimon Horman 						  bool both)
107119f5e44SMagnus Damm {
108119f5e44SMagnus Damm 	unsigned long flags;
109119f5e44SMagnus Damm 
110119f5e44SMagnus Damm 	/* follow steps in the GPIO documentation for
111119f5e44SMagnus Damm 	 * "Setting Edge-Sensitive Interrupt Input Mode" and
112119f5e44SMagnus Damm 	 * "Setting Level-Sensitive Interrupt Input Mode"
113119f5e44SMagnus Damm 	 */
114119f5e44SMagnus Damm 
115119f5e44SMagnus Damm 	spin_lock_irqsave(&p->lock, flags);
116119f5e44SMagnus Damm 
117119f5e44SMagnus Damm 	/* Configure postive or negative logic in POSNEG */
118119f5e44SMagnus Damm 	gpio_rcar_modify_bit(p, POSNEG, hwirq, !active_high_rising_edge);
119119f5e44SMagnus Damm 
120119f5e44SMagnus Damm 	/* Configure edge or level trigger in EDGLEVEL */
121119f5e44SMagnus Damm 	gpio_rcar_modify_bit(p, EDGLEVEL, hwirq, !level_trigger);
122119f5e44SMagnus Damm 
1237e1092b5SSimon Horman 	/* Select one edge or both edges in BOTHEDGE */
1247e1092b5SSimon Horman 	if (p->config.has_both_edge_trigger)
1257e1092b5SSimon Horman 		gpio_rcar_modify_bit(p, BOTHEDGE, hwirq, both);
1267e1092b5SSimon Horman 
127119f5e44SMagnus Damm 	/* Select "Interrupt Input Mode" in IOINTSEL */
128119f5e44SMagnus Damm 	gpio_rcar_modify_bit(p, IOINTSEL, hwirq, true);
129119f5e44SMagnus Damm 
130119f5e44SMagnus Damm 	/* Write INTCLR in case of edge trigger */
131119f5e44SMagnus Damm 	if (!level_trigger)
132119f5e44SMagnus Damm 		gpio_rcar_write(p, INTCLR, BIT(hwirq));
133119f5e44SMagnus Damm 
134119f5e44SMagnus Damm 	spin_unlock_irqrestore(&p->lock, flags);
135119f5e44SMagnus Damm }
136119f5e44SMagnus Damm 
137119f5e44SMagnus Damm static int gpio_rcar_irq_set_type(struct irq_data *d, unsigned int type)
138119f5e44SMagnus Damm {
139c7f3c5d3SGeert Uytterhoeven 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
140c7f3c5d3SGeert Uytterhoeven 	struct gpio_rcar_priv *p = container_of(gc, struct gpio_rcar_priv,
141c7f3c5d3SGeert Uytterhoeven 						gpio_chip);
142119f5e44SMagnus Damm 	unsigned int hwirq = irqd_to_hwirq(d);
143119f5e44SMagnus Damm 
144119f5e44SMagnus Damm 	dev_dbg(&p->pdev->dev, "sense irq = %d, type = %d\n", hwirq, type);
145119f5e44SMagnus Damm 
146119f5e44SMagnus Damm 	switch (type & IRQ_TYPE_SENSE_MASK) {
147119f5e44SMagnus Damm 	case IRQ_TYPE_LEVEL_HIGH:
1487e1092b5SSimon Horman 		gpio_rcar_config_interrupt_input_mode(p, hwirq, true, true,
1497e1092b5SSimon Horman 						      false);
150119f5e44SMagnus Damm 		break;
151119f5e44SMagnus Damm 	case IRQ_TYPE_LEVEL_LOW:
1527e1092b5SSimon Horman 		gpio_rcar_config_interrupt_input_mode(p, hwirq, false, true,
1537e1092b5SSimon Horman 						      false);
154119f5e44SMagnus Damm 		break;
155119f5e44SMagnus Damm 	case IRQ_TYPE_EDGE_RISING:
1567e1092b5SSimon Horman 		gpio_rcar_config_interrupt_input_mode(p, hwirq, true, false,
1577e1092b5SSimon Horman 						      false);
158119f5e44SMagnus Damm 		break;
159119f5e44SMagnus Damm 	case IRQ_TYPE_EDGE_FALLING:
1607e1092b5SSimon Horman 		gpio_rcar_config_interrupt_input_mode(p, hwirq, false, false,
1617e1092b5SSimon Horman 						      false);
1627e1092b5SSimon Horman 		break;
1637e1092b5SSimon Horman 	case IRQ_TYPE_EDGE_BOTH:
1647e1092b5SSimon Horman 		if (!p->config.has_both_edge_trigger)
1657e1092b5SSimon Horman 			return -EINVAL;
1667e1092b5SSimon Horman 		gpio_rcar_config_interrupt_input_mode(p, hwirq, true, false,
1677e1092b5SSimon Horman 						      true);
168119f5e44SMagnus Damm 		break;
169119f5e44SMagnus Damm 	default:
170119f5e44SMagnus Damm 		return -EINVAL;
171119f5e44SMagnus Damm 	}
172119f5e44SMagnus Damm 	return 0;
173119f5e44SMagnus Damm }
174119f5e44SMagnus Damm 
175ab82fa7dSGeert Uytterhoeven static int gpio_rcar_irq_set_wake(struct irq_data *d, unsigned int on)
176ab82fa7dSGeert Uytterhoeven {
177ab82fa7dSGeert Uytterhoeven 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
178ab82fa7dSGeert Uytterhoeven 	struct gpio_rcar_priv *p = container_of(gc, struct gpio_rcar_priv,
179ab82fa7dSGeert Uytterhoeven 						gpio_chip);
180ab82fa7dSGeert Uytterhoeven 
181ab82fa7dSGeert Uytterhoeven 	irq_set_irq_wake(p->irq_parent, on);
182ab82fa7dSGeert Uytterhoeven 
183ab82fa7dSGeert Uytterhoeven 	if (!p->clk)
184ab82fa7dSGeert Uytterhoeven 		return 0;
185ab82fa7dSGeert Uytterhoeven 
186ab82fa7dSGeert Uytterhoeven 	if (on)
187ab82fa7dSGeert Uytterhoeven 		clk_enable(p->clk);
188ab82fa7dSGeert Uytterhoeven 	else
189ab82fa7dSGeert Uytterhoeven 		clk_disable(p->clk);
190ab82fa7dSGeert Uytterhoeven 
191ab82fa7dSGeert Uytterhoeven 	return 0;
192ab82fa7dSGeert Uytterhoeven }
193ab82fa7dSGeert Uytterhoeven 
194119f5e44SMagnus Damm static irqreturn_t gpio_rcar_irq_handler(int irq, void *dev_id)
195119f5e44SMagnus Damm {
196119f5e44SMagnus Damm 	struct gpio_rcar_priv *p = dev_id;
197119f5e44SMagnus Damm 	u32 pending;
198119f5e44SMagnus Damm 	unsigned int offset, irqs_handled = 0;
199119f5e44SMagnus Damm 
2008808b64dSValentine Barshak 	while ((pending = gpio_rcar_read(p, INTDT) &
2018808b64dSValentine Barshak 			  gpio_rcar_read(p, INTMSK))) {
202119f5e44SMagnus Damm 		offset = __ffs(pending);
203119f5e44SMagnus Damm 		gpio_rcar_write(p, INTCLR, BIT(offset));
204c7f3c5d3SGeert Uytterhoeven 		generic_handle_irq(irq_find_mapping(p->gpio_chip.irqdomain,
205c7f3c5d3SGeert Uytterhoeven 						    offset));
206119f5e44SMagnus Damm 		irqs_handled++;
207119f5e44SMagnus Damm 	}
208119f5e44SMagnus Damm 
209119f5e44SMagnus Damm 	return irqs_handled ? IRQ_HANDLED : IRQ_NONE;
210119f5e44SMagnus Damm }
211119f5e44SMagnus Damm 
212119f5e44SMagnus Damm static inline struct gpio_rcar_priv *gpio_to_priv(struct gpio_chip *chip)
213119f5e44SMagnus Damm {
214119f5e44SMagnus Damm 	return container_of(chip, struct gpio_rcar_priv, gpio_chip);
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 {
221119f5e44SMagnus Damm 	struct gpio_rcar_priv *p = gpio_to_priv(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 {
245dc3465a9SLaurent Pinchart 	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 
252dc3465a9SLaurent Pinchart 	/* Set the GPIO as an input to ensure that the next GPIO request won't
253dc3465a9SLaurent Pinchart 	 * drive the GPIO pin as an output.
254dc3465a9SLaurent Pinchart 	 */
255dc3465a9SLaurent Pinchart 	gpio_rcar_config_general_input_output_mode(chip, offset, false);
256dc3465a9SLaurent Pinchart }
257dc3465a9SLaurent Pinchart 
258119f5e44SMagnus Damm static int gpio_rcar_direction_input(struct gpio_chip *chip, unsigned offset)
259119f5e44SMagnus Damm {
260119f5e44SMagnus Damm 	gpio_rcar_config_general_input_output_mode(chip, offset, false);
261119f5e44SMagnus Damm 	return 0;
262119f5e44SMagnus Damm }
263119f5e44SMagnus Damm 
264119f5e44SMagnus Damm static int gpio_rcar_get(struct gpio_chip *chip, unsigned offset)
265119f5e44SMagnus Damm {
266ae9550f6SMagnus Damm 	u32 bit = BIT(offset);
267ae9550f6SMagnus Damm 
268ae9550f6SMagnus Damm 	/* testing on r8a7790 shows that INDT does not show correct pin state
269ae9550f6SMagnus Damm 	 * when configured as output, so use OUTDT in case of output pins */
270ae9550f6SMagnus Damm 	if (gpio_rcar_read(gpio_to_priv(chip), INOUTSEL) & bit)
2717cb5409bSJürg Billeter 		return !!(gpio_rcar_read(gpio_to_priv(chip), OUTDT) & bit);
272ae9550f6SMagnus Damm 	else
2737cb5409bSJürg Billeter 		return !!(gpio_rcar_read(gpio_to_priv(chip), INDT) & bit);
274119f5e44SMagnus Damm }
275119f5e44SMagnus Damm 
276119f5e44SMagnus Damm static void gpio_rcar_set(struct gpio_chip *chip, unsigned offset, int value)
277119f5e44SMagnus Damm {
278119f5e44SMagnus Damm 	struct gpio_rcar_priv *p = gpio_to_priv(chip);
279119f5e44SMagnus Damm 	unsigned long flags;
280119f5e44SMagnus Damm 
281119f5e44SMagnus Damm 	spin_lock_irqsave(&p->lock, flags);
282119f5e44SMagnus Damm 	gpio_rcar_modify_bit(p, OUTDT, offset, value);
283119f5e44SMagnus Damm 	spin_unlock_irqrestore(&p->lock, flags);
284119f5e44SMagnus Damm }
285119f5e44SMagnus Damm 
286119f5e44SMagnus Damm static int gpio_rcar_direction_output(struct gpio_chip *chip, unsigned offset,
287119f5e44SMagnus Damm 				      int value)
288119f5e44SMagnus Damm {
289119f5e44SMagnus Damm 	/* write GPIO value to output before selecting output mode of pin */
290119f5e44SMagnus Damm 	gpio_rcar_set(chip, offset, value);
291119f5e44SMagnus Damm 	gpio_rcar_config_general_input_output_mode(chip, offset, true);
292119f5e44SMagnus Damm 	return 0;
293119f5e44SMagnus Damm }
294119f5e44SMagnus Damm 
295850dfe17SLaurent Pinchart struct gpio_rcar_info {
296850dfe17SLaurent Pinchart 	bool has_both_edge_trigger;
297850dfe17SLaurent Pinchart };
298850dfe17SLaurent Pinchart 
2991fd2b49dSHisashi Nakamura static const struct gpio_rcar_info gpio_rcar_info_gen1 = {
3001fd2b49dSHisashi Nakamura 	.has_both_edge_trigger = false,
3011fd2b49dSHisashi Nakamura };
3021fd2b49dSHisashi Nakamura 
3031fd2b49dSHisashi Nakamura static const struct gpio_rcar_info gpio_rcar_info_gen2 = {
3041fd2b49dSHisashi Nakamura 	.has_both_edge_trigger = true,
3051fd2b49dSHisashi Nakamura };
3061fd2b49dSHisashi Nakamura 
307850dfe17SLaurent Pinchart static const struct of_device_id gpio_rcar_of_table[] = {
308850dfe17SLaurent Pinchart 	{
309850dfe17SLaurent Pinchart 		.compatible = "renesas,gpio-r8a7790",
3101fd2b49dSHisashi Nakamura 		.data = &gpio_rcar_info_gen2,
311850dfe17SLaurent Pinchart 	}, {
312850dfe17SLaurent Pinchart 		.compatible = "renesas,gpio-r8a7791",
3131fd2b49dSHisashi Nakamura 		.data = &gpio_rcar_info_gen2,
3141fd2b49dSHisashi Nakamura 	}, {
3151fd2b49dSHisashi Nakamura 		.compatible = "renesas,gpio-r8a7793",
3161fd2b49dSHisashi Nakamura 		.data = &gpio_rcar_info_gen2,
3171fd2b49dSHisashi Nakamura 	}, {
3181fd2b49dSHisashi Nakamura 		.compatible = "renesas,gpio-r8a7794",
3191fd2b49dSHisashi Nakamura 		.data = &gpio_rcar_info_gen2,
320850dfe17SLaurent Pinchart 	}, {
321850dfe17SLaurent Pinchart 		.compatible = "renesas,gpio-rcar",
3221fd2b49dSHisashi Nakamura 		.data = &gpio_rcar_info_gen1,
323850dfe17SLaurent Pinchart 	}, {
324850dfe17SLaurent Pinchart 		/* Terminator */
325850dfe17SLaurent Pinchart 	},
326850dfe17SLaurent Pinchart };
327850dfe17SLaurent Pinchart 
328850dfe17SLaurent Pinchart MODULE_DEVICE_TABLE(of, gpio_rcar_of_table);
329850dfe17SLaurent Pinchart 
330850dfe17SLaurent Pinchart static int gpio_rcar_parse_pdata(struct gpio_rcar_priv *p)
331159f8a02SLaurent Pinchart {
332e56aee18SJingoo Han 	struct gpio_rcar_config *pdata = dev_get_platdata(&p->pdev->dev);
333159f8a02SLaurent Pinchart 	struct device_node *np = p->pdev->dev.of_node;
334159f8a02SLaurent Pinchart 	struct of_phandle_args args;
335159f8a02SLaurent Pinchart 	int ret;
336159f8a02SLaurent Pinchart 
337e305062eSLaurent Pinchart 	if (pdata) {
338159f8a02SLaurent Pinchart 		p->config = *pdata;
339e305062eSLaurent Pinchart 	} else if (IS_ENABLED(CONFIG_OF) && np) {
340850dfe17SLaurent Pinchart 		const struct of_device_id *match;
341850dfe17SLaurent Pinchart 		const struct gpio_rcar_info *info;
342850dfe17SLaurent Pinchart 
343850dfe17SLaurent Pinchart 		match = of_match_node(gpio_rcar_of_table, np);
344850dfe17SLaurent Pinchart 		if (!match)
345850dfe17SLaurent Pinchart 			return -EINVAL;
346850dfe17SLaurent Pinchart 
347850dfe17SLaurent Pinchart 		info = match->data;
348850dfe17SLaurent Pinchart 
34901eb2d18SLaurent Pinchart 		ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3, 0,
35001eb2d18SLaurent Pinchart 						       &args);
35101eb2d18SLaurent Pinchart 		p->config.number_of_pins = ret == 0 ? args.args[2]
352159f8a02SLaurent Pinchart 					 : RCAR_MAX_GPIO_PER_BANK;
353159f8a02SLaurent Pinchart 		p->config.gpio_base = -1;
354850dfe17SLaurent Pinchart 		p->config.has_both_edge_trigger = info->has_both_edge_trigger;
355159f8a02SLaurent Pinchart 	}
356159f8a02SLaurent Pinchart 
357159f8a02SLaurent Pinchart 	if (p->config.number_of_pins == 0 ||
358159f8a02SLaurent Pinchart 	    p->config.number_of_pins > RCAR_MAX_GPIO_PER_BANK) {
359159f8a02SLaurent Pinchart 		dev_warn(&p->pdev->dev,
360159f8a02SLaurent Pinchart 			 "Invalid number of gpio lines %u, using %u\n",
361159f8a02SLaurent Pinchart 			 p->config.number_of_pins, RCAR_MAX_GPIO_PER_BANK);
362159f8a02SLaurent Pinchart 		p->config.number_of_pins = RCAR_MAX_GPIO_PER_BANK;
363159f8a02SLaurent Pinchart 	}
364850dfe17SLaurent Pinchart 
365850dfe17SLaurent Pinchart 	return 0;
366159f8a02SLaurent Pinchart }
367159f8a02SLaurent Pinchart 
368119f5e44SMagnus Damm static int gpio_rcar_probe(struct platform_device *pdev)
369119f5e44SMagnus Damm {
370119f5e44SMagnus Damm 	struct gpio_rcar_priv *p;
371119f5e44SMagnus Damm 	struct resource *io, *irq;
372119f5e44SMagnus Damm 	struct gpio_chip *gpio_chip;
373119f5e44SMagnus Damm 	struct irq_chip *irq_chip;
374b22978fcSGeert Uytterhoeven 	struct device *dev = &pdev->dev;
375b22978fcSGeert Uytterhoeven 	const char *name = dev_name(dev);
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 
385159f8a02SLaurent Pinchart 	/* Get device configuration from DT node or platform data. */
386850dfe17SLaurent Pinchart 	ret = gpio_rcar_parse_pdata(p);
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)) {
394ab82fa7dSGeert Uytterhoeven 		dev_warn(dev, "unable to get clock\n");
395ab82fa7dSGeert Uytterhoeven 		p->clk = NULL;
396ab82fa7dSGeert Uytterhoeven 	}
397ab82fa7dSGeert Uytterhoeven 
398df0c6c80SGeert Uytterhoeven 	pm_runtime_enable(dev);
399df0c6c80SGeert Uytterhoeven 	pm_runtime_get_sync(dev);
400df0c6c80SGeert Uytterhoeven 
401119f5e44SMagnus Damm 	io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
402119f5e44SMagnus Damm 	irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
403119f5e44SMagnus Damm 
404119f5e44SMagnus Damm 	if (!io || !irq) {
405b22978fcSGeert Uytterhoeven 		dev_err(dev, "missing IRQ or IOMEM\n");
406119f5e44SMagnus Damm 		ret = -EINVAL;
407119f5e44SMagnus Damm 		goto err0;
408119f5e44SMagnus Damm 	}
409119f5e44SMagnus Damm 
410b22978fcSGeert Uytterhoeven 	p->base = devm_ioremap_nocache(dev, io->start, resource_size(io));
411119f5e44SMagnus Damm 	if (!p->base) {
412b22978fcSGeert Uytterhoeven 		dev_err(dev, "failed to remap I/O memory\n");
413119f5e44SMagnus Damm 		ret = -ENXIO;
414119f5e44SMagnus Damm 		goto err0;
415119f5e44SMagnus Damm 	}
416119f5e44SMagnus Damm 
417119f5e44SMagnus Damm 	gpio_chip = &p->gpio_chip;
418dc3465a9SLaurent Pinchart 	gpio_chip->request = gpio_rcar_request;
419dc3465a9SLaurent Pinchart 	gpio_chip->free = gpio_rcar_free;
420119f5e44SMagnus Damm 	gpio_chip->direction_input = gpio_rcar_direction_input;
421119f5e44SMagnus Damm 	gpio_chip->get = gpio_rcar_get;
422119f5e44SMagnus Damm 	gpio_chip->direction_output = gpio_rcar_direction_output;
423119f5e44SMagnus Damm 	gpio_chip->set = gpio_rcar_set;
424119f5e44SMagnus Damm 	gpio_chip->label = name;
425b22978fcSGeert Uytterhoeven 	gpio_chip->dev = dev;
426119f5e44SMagnus Damm 	gpio_chip->owner = THIS_MODULE;
427119f5e44SMagnus Damm 	gpio_chip->base = p->config.gpio_base;
428119f5e44SMagnus Damm 	gpio_chip->ngpio = p->config.number_of_pins;
429119f5e44SMagnus Damm 
430119f5e44SMagnus Damm 	irq_chip = &p->irq_chip;
431119f5e44SMagnus Damm 	irq_chip->name = name;
432119f5e44SMagnus Damm 	irq_chip->irq_mask = gpio_rcar_irq_disable;
433119f5e44SMagnus Damm 	irq_chip->irq_unmask = gpio_rcar_irq_enable;
434119f5e44SMagnus Damm 	irq_chip->irq_set_type = gpio_rcar_irq_set_type;
435ab82fa7dSGeert Uytterhoeven 	irq_chip->irq_set_wake = gpio_rcar_irq_set_wake;
436ab82fa7dSGeert Uytterhoeven 	irq_chip->flags	= IRQCHIP_SET_TYPE_MASKED | IRQCHIP_MASK_ON_SUSPEND;
437119f5e44SMagnus Damm 
438c7f3c5d3SGeert Uytterhoeven 	ret = gpiochip_add(gpio_chip);
439c7f3c5d3SGeert Uytterhoeven 	if (ret) {
440c7f3c5d3SGeert Uytterhoeven 		dev_err(dev, "failed to add GPIO controller\n");
4410c8aab8eSDan Carpenter 		goto err0;
442119f5e44SMagnus Damm 	}
443119f5e44SMagnus Damm 
4444d84b9e4SGeert Uytterhoeven 	ret = gpiochip_irqchip_add(gpio_chip, irq_chip, p->config.irq_base,
445c7f3c5d3SGeert Uytterhoeven 				   handle_level_irq, IRQ_TYPE_NONE);
446c7f3c5d3SGeert Uytterhoeven 	if (ret) {
447c7f3c5d3SGeert Uytterhoeven 		dev_err(dev, "cannot add irqchip\n");
448c7f3c5d3SGeert Uytterhoeven 		goto err1;
449c7f3c5d3SGeert Uytterhoeven 	}
450c7f3c5d3SGeert Uytterhoeven 
451ab82fa7dSGeert Uytterhoeven 	p->irq_parent = irq->start;
452b22978fcSGeert Uytterhoeven 	if (devm_request_irq(dev, irq->start, gpio_rcar_irq_handler,
453b22978fcSGeert Uytterhoeven 			     IRQF_SHARED, name, p)) {
454b22978fcSGeert Uytterhoeven 		dev_err(dev, "failed to request IRQ\n");
455119f5e44SMagnus Damm 		ret = -ENOENT;
456119f5e44SMagnus Damm 		goto err1;
457119f5e44SMagnus Damm 	}
458119f5e44SMagnus Damm 
459b22978fcSGeert Uytterhoeven 	dev_info(dev, "driving %d GPIOs\n", p->config.number_of_pins);
460119f5e44SMagnus Damm 
461119f5e44SMagnus Damm 	/* warn in case of mismatch if irq base is specified */
462119f5e44SMagnus Damm 	if (p->config.irq_base) {
4634d84b9e4SGeert Uytterhoeven 		ret = irq_find_mapping(gpio_chip->irqdomain, 0);
464119f5e44SMagnus Damm 		if (p->config.irq_base != ret)
465b22978fcSGeert Uytterhoeven 			dev_warn(dev, "irq base mismatch (%u/%u)\n",
466119f5e44SMagnus Damm 				 p->config.irq_base, ret);
467119f5e44SMagnus Damm 	}
468119f5e44SMagnus Damm 
469159f8a02SLaurent Pinchart 	if (p->config.pctl_name) {
470dc3465a9SLaurent Pinchart 		ret = gpiochip_add_pin_range(gpio_chip, p->config.pctl_name, 0,
471dc3465a9SLaurent Pinchart 					     gpio_chip->base, gpio_chip->ngpio);
472dc3465a9SLaurent Pinchart 		if (ret < 0)
473b22978fcSGeert Uytterhoeven 			dev_warn(dev, "failed to add pin range\n");
474159f8a02SLaurent Pinchart 	}
475dc3465a9SLaurent Pinchart 
476119f5e44SMagnus Damm 	return 0;
477119f5e44SMagnus Damm 
478119f5e44SMagnus Damm err1:
4794d84b9e4SGeert Uytterhoeven 	gpiochip_remove(gpio_chip);
480119f5e44SMagnus Damm err0:
481df0c6c80SGeert Uytterhoeven 	pm_runtime_put(dev);
482df0c6c80SGeert Uytterhoeven 	pm_runtime_disable(dev);
483119f5e44SMagnus Damm 	return ret;
484119f5e44SMagnus Damm }
485119f5e44SMagnus Damm 
486119f5e44SMagnus Damm static int gpio_rcar_remove(struct platform_device *pdev)
487119f5e44SMagnus Damm {
488119f5e44SMagnus Damm 	struct gpio_rcar_priv *p = platform_get_drvdata(pdev);
489119f5e44SMagnus Damm 
4909f5132aeSabdoulaye berthe 	gpiochip_remove(&p->gpio_chip);
491119f5e44SMagnus Damm 
492df0c6c80SGeert Uytterhoeven 	pm_runtime_put(&pdev->dev);
493df0c6c80SGeert Uytterhoeven 	pm_runtime_disable(&pdev->dev);
494119f5e44SMagnus Damm 	return 0;
495119f5e44SMagnus Damm }
496119f5e44SMagnus Damm 
497119f5e44SMagnus Damm static struct platform_driver gpio_rcar_device_driver = {
498119f5e44SMagnus Damm 	.probe		= gpio_rcar_probe,
499119f5e44SMagnus Damm 	.remove		= gpio_rcar_remove,
500119f5e44SMagnus Damm 	.driver		= {
501119f5e44SMagnus Damm 		.name	= "gpio_rcar",
502159f8a02SLaurent Pinchart 		.of_match_table = of_match_ptr(gpio_rcar_of_table),
503119f5e44SMagnus Damm 	}
504119f5e44SMagnus Damm };
505119f5e44SMagnus Damm 
506119f5e44SMagnus Damm module_platform_driver(gpio_rcar_device_driver);
507119f5e44SMagnus Damm 
508119f5e44SMagnus Damm MODULE_AUTHOR("Magnus Damm");
509119f5e44SMagnus Damm MODULE_DESCRIPTION("Renesas R-Car GPIO Driver");
510119f5e44SMagnus Damm MODULE_LICENSE("GPL v2");
511