blob: 1322f7e0cfded12ace17f31f9374cecd6bbc4d66 [file] [log] [blame]
Kuninori Morimoto8b37eb72018-11-08 06:35:16 +00001// SPDX-License-Identifier: GPL-2.0
Magnus Damm119f5e42013-03-13 20:32:13 +09002/*
3 * Renesas R-Car GPIO Support
4 *
Hisashi Nakamura1fd2b492014-11-07 20:54:08 +09005 * Copyright (C) 2014 Renesas Electronics Corporation
Magnus Damm119f5e42013-03-13 20:32:13 +09006 * Copyright (C) 2013 Magnus Damm
Magnus Damm119f5e42013-03-13 20:32:13 +09007 */
8
9#include <linux/err.h>
Linus Walleij4b1d8002018-05-31 08:08:13 +020010#include <linux/gpio/driver.h>
Magnus Damm119f5e42013-03-13 20:32:13 +090011#include <linux/init.h>
12#include <linux/interrupt.h>
13#include <linux/io.h>
14#include <linux/ioport.h>
15#include <linux/irq.h>
Magnus Damm119f5e42013-03-13 20:32:13 +090016#include <linux/module.h>
Sachin Kamatbd0bf462013-10-16 15:35:02 +053017#include <linux/of.h>
Geert Uytterhoevenf9f2a6f2017-10-04 14:16:16 +020018#include <linux/of_device.h>
Laurent Pinchartdc3465a2013-03-10 03:27:00 +010019#include <linux/pinctrl/consumer.h>
Magnus Damm119f5e42013-03-13 20:32:13 +090020#include <linux/platform_device.h>
Geert Uytterhoevendf0c6c82014-04-14 20:33:13 +020021#include <linux/pm_runtime.h>
Magnus Damm119f5e42013-03-13 20:32:13 +090022#include <linux/spinlock.h>
23#include <linux/slab.h>
24
Hien Dang51750fb2018-02-05 04:15:02 +090025struct gpio_rcar_bank_info {
26 u32 iointsel;
27 u32 inoutsel;
28 u32 outdt;
29 u32 posneg;
30 u32 edglevel;
31 u32 bothedge;
32 u32 intmsk;
33};
34
Magnus Damm119f5e42013-03-13 20:32:13 +090035struct gpio_rcar_priv {
36 void __iomem *base;
37 spinlock_t lock;
Magnus Damm119f5e42013-03-13 20:32:13 +090038 struct platform_device *pdev;
39 struct gpio_chip gpio_chip;
40 struct irq_chip irq_chip;
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +010041 unsigned int irq_parent;
Geert Uytterhoeven9ac79ba2018-02-12 14:55:13 +010042 atomic_t wakeup_path;
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +010043 bool has_both_edge_trigger;
Hien Dang51750fb2018-02-05 04:15:02 +090044 struct gpio_rcar_bank_info bank_info;
Magnus Damm119f5e42013-03-13 20:32:13 +090045};
46
Geert Uytterhoeven3dc1e682015-03-18 19:41:08 +010047#define IOINTSEL 0x00 /* General IO/Interrupt Switching Register */
48#define INOUTSEL 0x04 /* General Input/Output Switching Register */
49#define OUTDT 0x08 /* General Output Register */
50#define INDT 0x0c /* General Input Register */
51#define INTDT 0x10 /* Interrupt Display Register */
52#define INTCLR 0x14 /* Interrupt Clear Register */
53#define INTMSK 0x18 /* Interrupt Mask Register */
54#define MSKCLR 0x1c /* Interrupt Mask Clear Register */
55#define POSNEG 0x20 /* Positive/Negative Logic Select Register */
56#define EDGLEVEL 0x24 /* Edge/level Select Register */
57#define FILONOFF 0x28 /* Chattering Prevention On/Off Register */
58#define BOTHEDGE 0x4c /* One Edge/Both Edge Select Register */
Magnus Damm119f5e42013-03-13 20:32:13 +090059
Laurent Pinchart159f8a02013-05-21 13:40:06 +020060#define RCAR_MAX_GPIO_PER_BANK 32
61
Magnus Damm119f5e42013-03-13 20:32:13 +090062static inline u32 gpio_rcar_read(struct gpio_rcar_priv *p, int offs)
63{
64 return ioread32(p->base + offs);
65}
66
67static inline void gpio_rcar_write(struct gpio_rcar_priv *p, int offs,
68 u32 value)
69{
70 iowrite32(value, p->base + offs);
71}
72
73static void gpio_rcar_modify_bit(struct gpio_rcar_priv *p, int offs,
74 int bit, bool value)
75{
76 u32 tmp = gpio_rcar_read(p, offs);
77
78 if (value)
79 tmp |= BIT(bit);
80 else
81 tmp &= ~BIT(bit);
82
83 gpio_rcar_write(p, offs, tmp);
84}
85
86static void gpio_rcar_irq_disable(struct irq_data *d)
87{
Geert Uytterhoevenc7f3c5d2015-01-12 11:07:59 +010088 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijc7b6f452015-12-07 14:12:45 +010089 struct gpio_rcar_priv *p = gpiochip_get_data(gc);
Magnus Damm119f5e42013-03-13 20:32:13 +090090
91 gpio_rcar_write(p, INTMSK, ~BIT(irqd_to_hwirq(d)));
92}
93
94static void gpio_rcar_irq_enable(struct irq_data *d)
95{
Geert Uytterhoevenc7f3c5d2015-01-12 11:07:59 +010096 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijc7b6f452015-12-07 14:12:45 +010097 struct gpio_rcar_priv *p = gpiochip_get_data(gc);
Magnus Damm119f5e42013-03-13 20:32:13 +090098
99 gpio_rcar_write(p, MSKCLR, BIT(irqd_to_hwirq(d)));
100}
101
102static void gpio_rcar_config_interrupt_input_mode(struct gpio_rcar_priv *p,
103 unsigned int hwirq,
104 bool active_high_rising_edge,
Simon Horman7e1092b2013-05-24 18:47:24 +0900105 bool level_trigger,
106 bool both)
Magnus Damm119f5e42013-03-13 20:32:13 +0900107{
108 unsigned long flags;
109
110 /* follow steps in the GPIO documentation for
111 * "Setting Edge-Sensitive Interrupt Input Mode" and
112 * "Setting Level-Sensitive Interrupt Input Mode"
113 */
114
115 spin_lock_irqsave(&p->lock, flags);
116
117 /* Configure postive or negative logic in POSNEG */
118 gpio_rcar_modify_bit(p, POSNEG, hwirq, !active_high_rising_edge);
119
120 /* Configure edge or level trigger in EDGLEVEL */
121 gpio_rcar_modify_bit(p, EDGLEVEL, hwirq, !level_trigger);
122
Simon Horman7e1092b2013-05-24 18:47:24 +0900123 /* Select one edge or both edges in BOTHEDGE */
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100124 if (p->has_both_edge_trigger)
Simon Horman7e1092b2013-05-24 18:47:24 +0900125 gpio_rcar_modify_bit(p, BOTHEDGE, hwirq, both);
126
Magnus Damm119f5e42013-03-13 20:32:13 +0900127 /* Select "Interrupt Input Mode" in IOINTSEL */
128 gpio_rcar_modify_bit(p, IOINTSEL, hwirq, true);
129
130 /* Write INTCLR in case of edge trigger */
131 if (!level_trigger)
132 gpio_rcar_write(p, INTCLR, BIT(hwirq));
133
134 spin_unlock_irqrestore(&p->lock, flags);
135}
136
137static int gpio_rcar_irq_set_type(struct irq_data *d, unsigned int type)
138{
Geert Uytterhoevenc7f3c5d2015-01-12 11:07:59 +0100139 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijc7b6f452015-12-07 14:12:45 +0100140 struct gpio_rcar_priv *p = gpiochip_get_data(gc);
Magnus Damm119f5e42013-03-13 20:32:13 +0900141 unsigned int hwirq = irqd_to_hwirq(d);
142
143 dev_dbg(&p->pdev->dev, "sense irq = %d, type = %d\n", hwirq, type);
144
145 switch (type & IRQ_TYPE_SENSE_MASK) {
146 case IRQ_TYPE_LEVEL_HIGH:
Simon Horman7e1092b2013-05-24 18:47:24 +0900147 gpio_rcar_config_interrupt_input_mode(p, hwirq, true, true,
148 false);
Magnus Damm119f5e42013-03-13 20:32:13 +0900149 break;
150 case IRQ_TYPE_LEVEL_LOW:
Simon Horman7e1092b2013-05-24 18:47:24 +0900151 gpio_rcar_config_interrupt_input_mode(p, hwirq, false, true,
152 false);
Magnus Damm119f5e42013-03-13 20:32:13 +0900153 break;
154 case IRQ_TYPE_EDGE_RISING:
Simon Horman7e1092b2013-05-24 18:47:24 +0900155 gpio_rcar_config_interrupt_input_mode(p, hwirq, true, false,
156 false);
Magnus Damm119f5e42013-03-13 20:32:13 +0900157 break;
158 case IRQ_TYPE_EDGE_FALLING:
Simon Horman7e1092b2013-05-24 18:47:24 +0900159 gpio_rcar_config_interrupt_input_mode(p, hwirq, false, false,
160 false);
161 break;
162 case IRQ_TYPE_EDGE_BOTH:
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100163 if (!p->has_both_edge_trigger)
Simon Horman7e1092b2013-05-24 18:47:24 +0900164 return -EINVAL;
165 gpio_rcar_config_interrupt_input_mode(p, hwirq, true, false,
166 true);
Magnus Damm119f5e42013-03-13 20:32:13 +0900167 break;
168 default:
169 return -EINVAL;
170 }
171 return 0;
172}
173
Geert Uytterhoevenab82fa72015-03-18 19:41:09 +0100174static int gpio_rcar_irq_set_wake(struct irq_data *d, unsigned int on)
175{
176 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijc7b6f452015-12-07 14:12:45 +0100177 struct gpio_rcar_priv *p = gpiochip_get_data(gc);
Geert Uytterhoeven501ef0f2015-05-21 13:21:37 +0200178 int error;
Geert Uytterhoevenab82fa72015-03-18 19:41:09 +0100179
Geert Uytterhoeven501ef0f2015-05-21 13:21:37 +0200180 if (p->irq_parent) {
181 error = irq_set_irq_wake(p->irq_parent, on);
182 if (error) {
183 dev_dbg(&p->pdev->dev,
184 "irq %u doesn't support irq_set_wake\n",
185 p->irq_parent);
186 p->irq_parent = 0;
187 }
188 }
Geert Uytterhoevenab82fa72015-03-18 19:41:09 +0100189
Geert Uytterhoevenab82fa72015-03-18 19:41:09 +0100190 if (on)
Geert Uytterhoeven9ac79ba2018-02-12 14:55:13 +0100191 atomic_inc(&p->wakeup_path);
Geert Uytterhoevenab82fa72015-03-18 19:41:09 +0100192 else
Geert Uytterhoeven9ac79ba2018-02-12 14:55:13 +0100193 atomic_dec(&p->wakeup_path);
Geert Uytterhoevenab82fa72015-03-18 19:41:09 +0100194
195 return 0;
196}
197
Magnus Damm119f5e42013-03-13 20:32:13 +0900198static irqreturn_t gpio_rcar_irq_handler(int irq, void *dev_id)
199{
200 struct gpio_rcar_priv *p = dev_id;
201 u32 pending;
202 unsigned int offset, irqs_handled = 0;
203
Valentine Barshak8808b642013-11-29 22:04:09 +0400204 while ((pending = gpio_rcar_read(p, INTDT) &
205 gpio_rcar_read(p, INTMSK))) {
Magnus Damm119f5e42013-03-13 20:32:13 +0900206 offset = __ffs(pending);
207 gpio_rcar_write(p, INTCLR, BIT(offset));
Thierry Redingf0fbe7b2017-11-07 19:15:47 +0100208 generic_handle_irq(irq_find_mapping(p->gpio_chip.irq.domain,
Geert Uytterhoevenc7f3c5d2015-01-12 11:07:59 +0100209 offset));
Magnus Damm119f5e42013-03-13 20:32:13 +0900210 irqs_handled++;
211 }
212
213 return irqs_handled ? IRQ_HANDLED : IRQ_NONE;
214}
215
Magnus Damm119f5e42013-03-13 20:32:13 +0900216static void gpio_rcar_config_general_input_output_mode(struct gpio_chip *chip,
217 unsigned int gpio,
218 bool output)
219{
Linus Walleijc7b6f452015-12-07 14:12:45 +0100220 struct gpio_rcar_priv *p = gpiochip_get_data(chip);
Magnus Damm119f5e42013-03-13 20:32:13 +0900221 unsigned long flags;
222
223 /* follow steps in the GPIO documentation for
224 * "Setting General Output Mode" and
225 * "Setting General Input Mode"
226 */
227
228 spin_lock_irqsave(&p->lock, flags);
229
230 /* Configure postive logic in POSNEG */
231 gpio_rcar_modify_bit(p, POSNEG, gpio, false);
232
233 /* Select "General Input/Output Mode" in IOINTSEL */
234 gpio_rcar_modify_bit(p, IOINTSEL, gpio, false);
235
236 /* Select Input Mode or Output Mode in INOUTSEL */
237 gpio_rcar_modify_bit(p, INOUTSEL, gpio, output);
238
239 spin_unlock_irqrestore(&p->lock, flags);
240}
241
Laurent Pinchartdc3465a2013-03-10 03:27:00 +0100242static int gpio_rcar_request(struct gpio_chip *chip, unsigned offset)
243{
Geert Uytterhoeven2d654722016-12-08 18:32:28 +0100244 struct gpio_rcar_priv *p = gpiochip_get_data(chip);
245 int error;
246
247 error = pm_runtime_get_sync(&p->pdev->dev);
248 if (error < 0)
249 return error;
250
Linus Walleija9a1d2a2017-09-22 11:02:10 +0200251 error = pinctrl_gpio_request(chip->base + offset);
Geert Uytterhoeven2d654722016-12-08 18:32:28 +0100252 if (error)
253 pm_runtime_put(&p->pdev->dev);
254
255 return error;
Laurent Pinchartdc3465a2013-03-10 03:27:00 +0100256}
257
258static void gpio_rcar_free(struct gpio_chip *chip, unsigned offset)
259{
Geert Uytterhoeven2d654722016-12-08 18:32:28 +0100260 struct gpio_rcar_priv *p = gpiochip_get_data(chip);
261
Linus Walleija9a1d2a2017-09-22 11:02:10 +0200262 pinctrl_gpio_free(chip->base + offset);
Laurent Pinchartdc3465a2013-03-10 03:27:00 +0100263
Linus Walleijce0e2c62016-04-12 10:05:22 +0200264 /*
265 * Set the GPIO as an input to ensure that the next GPIO request won't
Laurent Pinchartdc3465a2013-03-10 03:27:00 +0100266 * drive the GPIO pin as an output.
267 */
268 gpio_rcar_config_general_input_output_mode(chip, offset, false);
Geert Uytterhoeven2d654722016-12-08 18:32:28 +0100269
270 pm_runtime_put(&p->pdev->dev);
Laurent Pinchartdc3465a2013-03-10 03:27:00 +0100271}
272
Geert Uytterhoevenad817292018-07-12 11:15:01 +0200273static int gpio_rcar_get_direction(struct gpio_chip *chip, unsigned int offset)
274{
275 struct gpio_rcar_priv *p = gpiochip_get_data(chip);
276
277 return !(gpio_rcar_read(p, INOUTSEL) & BIT(offset));
278}
279
Magnus Damm119f5e42013-03-13 20:32:13 +0900280static int gpio_rcar_direction_input(struct gpio_chip *chip, unsigned offset)
281{
282 gpio_rcar_config_general_input_output_mode(chip, offset, false);
283 return 0;
284}
285
286static int gpio_rcar_get(struct gpio_chip *chip, unsigned offset)
287{
Magnus Dammae9550f2013-06-17 08:41:52 +0900288 u32 bit = BIT(offset);
289
290 /* testing on r8a7790 shows that INDT does not show correct pin state
291 * when configured as output, so use OUTDT in case of output pins */
Linus Walleijc7b6f452015-12-07 14:12:45 +0100292 if (gpio_rcar_read(gpiochip_get_data(chip), INOUTSEL) & bit)
293 return !!(gpio_rcar_read(gpiochip_get_data(chip), OUTDT) & bit);
Magnus Dammae9550f2013-06-17 08:41:52 +0900294 else
Linus Walleijc7b6f452015-12-07 14:12:45 +0100295 return !!(gpio_rcar_read(gpiochip_get_data(chip), INDT) & bit);
Magnus Damm119f5e42013-03-13 20:32:13 +0900296}
297
298static void gpio_rcar_set(struct gpio_chip *chip, unsigned offset, int value)
299{
Linus Walleijc7b6f452015-12-07 14:12:45 +0100300 struct gpio_rcar_priv *p = gpiochip_get_data(chip);
Magnus Damm119f5e42013-03-13 20:32:13 +0900301 unsigned long flags;
302
303 spin_lock_irqsave(&p->lock, flags);
304 gpio_rcar_modify_bit(p, OUTDT, offset, value);
305 spin_unlock_irqrestore(&p->lock, flags);
306}
307
Geert Uytterhoevendbb763b82016-03-14 16:21:44 +0100308static void gpio_rcar_set_multiple(struct gpio_chip *chip, unsigned long *mask,
309 unsigned long *bits)
310{
311 struct gpio_rcar_priv *p = gpiochip_get_data(chip);
312 unsigned long flags;
313 u32 val, bankmask;
314
315 bankmask = mask[0] & GENMASK(chip->ngpio - 1, 0);
Biju Das496069b2018-08-07 08:57:02 +0100316 if (chip->valid_mask)
317 bankmask &= chip->valid_mask[0];
318
Geert Uytterhoevendbb763b82016-03-14 16:21:44 +0100319 if (!bankmask)
320 return;
321
322 spin_lock_irqsave(&p->lock, flags);
323 val = gpio_rcar_read(p, OUTDT);
324 val &= ~bankmask;
325 val |= (bankmask & bits[0]);
326 gpio_rcar_write(p, OUTDT, val);
327 spin_unlock_irqrestore(&p->lock, flags);
328}
329
Magnus Damm119f5e42013-03-13 20:32:13 +0900330static int gpio_rcar_direction_output(struct gpio_chip *chip, unsigned offset,
331 int value)
332{
333 /* write GPIO value to output before selecting output mode of pin */
334 gpio_rcar_set(chip, offset, value);
335 gpio_rcar_config_general_input_output_mode(chip, offset, true);
336 return 0;
337}
338
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100339struct gpio_rcar_info {
340 bool has_both_edge_trigger;
341};
342
Hisashi Nakamura1fd2b492014-11-07 20:54:08 +0900343static const struct gpio_rcar_info gpio_rcar_info_gen1 = {
344 .has_both_edge_trigger = false,
345};
346
347static const struct gpio_rcar_info gpio_rcar_info_gen2 = {
348 .has_both_edge_trigger = true,
349};
350
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100351static const struct of_device_id gpio_rcar_of_table[] = {
352 {
Biju Das85bb4642017-06-21 15:27:09 +0100353 .compatible = "renesas,gpio-r8a7743",
354 /* RZ/G1 GPIO is identical to R-Car Gen2. */
355 .data = &gpio_rcar_info_gen2,
356 }, {
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100357 .compatible = "renesas,gpio-r8a7790",
Hisashi Nakamura1fd2b492014-11-07 20:54:08 +0900358 .data = &gpio_rcar_info_gen2,
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100359 }, {
360 .compatible = "renesas,gpio-r8a7791",
Hisashi Nakamura1fd2b492014-11-07 20:54:08 +0900361 .data = &gpio_rcar_info_gen2,
362 }, {
Sergei Shtylyove79c5832016-07-07 17:11:45 +0300363 .compatible = "renesas,gpio-r8a7792",
364 .data = &gpio_rcar_info_gen2,
365 }, {
Hisashi Nakamura1fd2b492014-11-07 20:54:08 +0900366 .compatible = "renesas,gpio-r8a7793",
367 .data = &gpio_rcar_info_gen2,
368 }, {
369 .compatible = "renesas,gpio-r8a7794",
370 .data = &gpio_rcar_info_gen2,
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100371 }, {
Ulrich Hecht8cd14702015-07-21 11:08:50 +0200372 .compatible = "renesas,gpio-r8a7795",
373 /* Gen3 GPIO is identical to Gen2. */
374 .data = &gpio_rcar_info_gen2,
375 }, {
Simon Horman5d2f1d62016-09-06 12:35:39 +0200376 .compatible = "renesas,gpio-r8a7796",
377 /* Gen3 GPIO is identical to Gen2. */
378 .data = &gpio_rcar_info_gen2,
379 }, {
Simon Hormandbd1dad2017-07-11 14:38:30 +0200380 .compatible = "renesas,rcar-gen1-gpio",
381 .data = &gpio_rcar_info_gen1,
382 }, {
383 .compatible = "renesas,rcar-gen2-gpio",
384 .data = &gpio_rcar_info_gen2,
385 }, {
386 .compatible = "renesas,rcar-gen3-gpio",
387 /* Gen3 GPIO is identical to Gen2. */
388 .data = &gpio_rcar_info_gen2,
389 }, {
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100390 .compatible = "renesas,gpio-rcar",
Hisashi Nakamura1fd2b492014-11-07 20:54:08 +0900391 .data = &gpio_rcar_info_gen1,
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100392 }, {
393 /* Terminator */
394 },
395};
396
397MODULE_DEVICE_TABLE(of, gpio_rcar_of_table);
398
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100399static int gpio_rcar_parse_dt(struct gpio_rcar_priv *p, unsigned int *npins)
Laurent Pinchart159f8a02013-05-21 13:40:06 +0200400{
Laurent Pinchart159f8a02013-05-21 13:40:06 +0200401 struct device_node *np = p->pdev->dev.of_node;
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100402 const struct gpio_rcar_info *info;
Laurent Pinchart159f8a02013-05-21 13:40:06 +0200403 struct of_phandle_args args;
404 int ret;
Laurent Pinchart159f8a02013-05-21 13:40:06 +0200405
Geert Uytterhoevenf9f2a6f2017-10-04 14:16:16 +0200406 info = of_device_get_match_data(&p->pdev->dev);
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100407
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100408 ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3, 0, &args);
409 *npins = ret == 0 ? args.args[2] : RCAR_MAX_GPIO_PER_BANK;
410 p->has_both_edge_trigger = info->has_both_edge_trigger;
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100411
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100412 if (*npins == 0 || *npins > RCAR_MAX_GPIO_PER_BANK) {
Laurent Pinchart159f8a02013-05-21 13:40:06 +0200413 dev_warn(&p->pdev->dev,
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100414 "Invalid number of gpio lines %u, using %u\n", *npins,
415 RCAR_MAX_GPIO_PER_BANK);
416 *npins = RCAR_MAX_GPIO_PER_BANK;
Laurent Pinchart159f8a02013-05-21 13:40:06 +0200417 }
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100418
419 return 0;
Laurent Pinchart159f8a02013-05-21 13:40:06 +0200420}
421
Magnus Damm119f5e42013-03-13 20:32:13 +0900422static int gpio_rcar_probe(struct platform_device *pdev)
423{
Magnus Damm119f5e42013-03-13 20:32:13 +0900424 struct gpio_rcar_priv *p;
425 struct resource *io, *irq;
426 struct gpio_chip *gpio_chip;
427 struct irq_chip *irq_chip;
Geert Uytterhoevenb22978f2014-03-27 21:47:36 +0100428 struct device *dev = &pdev->dev;
429 const char *name = dev_name(dev);
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100430 unsigned int npins;
Magnus Damm119f5e42013-03-13 20:32:13 +0900431 int ret;
432
Geert Uytterhoevenb22978f2014-03-27 21:47:36 +0100433 p = devm_kzalloc(dev, sizeof(*p), GFP_KERNEL);
Geert Uytterhoeven7d82bf32015-01-12 11:07:58 +0100434 if (!p)
435 return -ENOMEM;
Magnus Damm119f5e42013-03-13 20:32:13 +0900436
Magnus Damm119f5e42013-03-13 20:32:13 +0900437 p->pdev = pdev;
Magnus Damm119f5e42013-03-13 20:32:13 +0900438 spin_lock_init(&p->lock);
439
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100440 /* Get device configuration from DT node */
441 ret = gpio_rcar_parse_dt(p, &npins);
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100442 if (ret < 0)
443 return ret;
Laurent Pinchart159f8a02013-05-21 13:40:06 +0200444
445 platform_set_drvdata(pdev, p);
446
Geert Uytterhoevendf0c6c82014-04-14 20:33:13 +0200447 pm_runtime_enable(dev);
Geert Uytterhoevendf0c6c82014-04-14 20:33:13 +0200448
Magnus Damm119f5e42013-03-13 20:32:13 +0900449 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
Sergei Shtylyov5a24d4b2017-10-13 00:08:14 +0300450 if (!irq) {
451 dev_err(dev, "missing IRQ\n");
Magnus Damm119f5e42013-03-13 20:32:13 +0900452 ret = -EINVAL;
453 goto err0;
454 }
455
Sergei Shtylyov5a24d4b2017-10-13 00:08:14 +0300456 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
457 p->base = devm_ioremap_resource(dev, io);
458 if (IS_ERR(p->base)) {
459 ret = PTR_ERR(p->base);
Magnus Damm119f5e42013-03-13 20:32:13 +0900460 goto err0;
461 }
462
463 gpio_chip = &p->gpio_chip;
Laurent Pinchartdc3465a2013-03-10 03:27:00 +0100464 gpio_chip->request = gpio_rcar_request;
465 gpio_chip->free = gpio_rcar_free;
Geert Uytterhoevenad817292018-07-12 11:15:01 +0200466 gpio_chip->get_direction = gpio_rcar_get_direction;
Magnus Damm119f5e42013-03-13 20:32:13 +0900467 gpio_chip->direction_input = gpio_rcar_direction_input;
468 gpio_chip->get = gpio_rcar_get;
469 gpio_chip->direction_output = gpio_rcar_direction_output;
470 gpio_chip->set = gpio_rcar_set;
Geert Uytterhoevendbb763b82016-03-14 16:21:44 +0100471 gpio_chip->set_multiple = gpio_rcar_set_multiple;
Magnus Damm119f5e42013-03-13 20:32:13 +0900472 gpio_chip->label = name;
Linus Walleij58383c782015-11-04 09:56:26 +0100473 gpio_chip->parent = dev;
Magnus Damm119f5e42013-03-13 20:32:13 +0900474 gpio_chip->owner = THIS_MODULE;
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100475 gpio_chip->base = -1;
476 gpio_chip->ngpio = npins;
Magnus Damm119f5e42013-03-13 20:32:13 +0900477
478 irq_chip = &p->irq_chip;
479 irq_chip->name = name;
Niklas Söderlund47bd38a2016-12-08 18:32:27 +0100480 irq_chip->parent_device = dev;
Magnus Damm119f5e42013-03-13 20:32:13 +0900481 irq_chip->irq_mask = gpio_rcar_irq_disable;
482 irq_chip->irq_unmask = gpio_rcar_irq_enable;
Magnus Damm119f5e42013-03-13 20:32:13 +0900483 irq_chip->irq_set_type = gpio_rcar_irq_set_type;
Geert Uytterhoevenab82fa72015-03-18 19:41:09 +0100484 irq_chip->irq_set_wake = gpio_rcar_irq_set_wake;
485 irq_chip->flags = IRQCHIP_SET_TYPE_MASKED | IRQCHIP_MASK_ON_SUSPEND;
Magnus Damm119f5e42013-03-13 20:32:13 +0900486
Linus Walleijc7b6f452015-12-07 14:12:45 +0100487 ret = gpiochip_add_data(gpio_chip, p);
Geert Uytterhoevenc7f3c5d2015-01-12 11:07:59 +0100488 if (ret) {
489 dev_err(dev, "failed to add GPIO controller\n");
Dan Carpenter0c8aab82013-11-07 10:56:51 +0300490 goto err0;
Magnus Damm119f5e42013-03-13 20:32:13 +0900491 }
492
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100493 ret = gpiochip_irqchip_add(gpio_chip, irq_chip, 0, handle_level_irq,
494 IRQ_TYPE_NONE);
Geert Uytterhoevenc7f3c5d2015-01-12 11:07:59 +0100495 if (ret) {
496 dev_err(dev, "cannot add irqchip\n");
497 goto err1;
498 }
499
Geert Uytterhoevenab82fa72015-03-18 19:41:09 +0100500 p->irq_parent = irq->start;
Geert Uytterhoevenb22978f2014-03-27 21:47:36 +0100501 if (devm_request_irq(dev, irq->start, gpio_rcar_irq_handler,
502 IRQF_SHARED, name, p)) {
503 dev_err(dev, "failed to request IRQ\n");
Magnus Damm119f5e42013-03-13 20:32:13 +0900504 ret = -ENOENT;
505 goto err1;
506 }
507
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100508 dev_info(dev, "driving %d GPIOs\n", npins);
Laurent Pinchartdc3465a2013-03-10 03:27:00 +0100509
Magnus Damm119f5e42013-03-13 20:32:13 +0900510 return 0;
511
512err1:
Geert Uytterhoeven4d84b9e2015-03-18 19:41:07 +0100513 gpiochip_remove(gpio_chip);
Magnus Damm119f5e42013-03-13 20:32:13 +0900514err0:
Geert Uytterhoevendf0c6c82014-04-14 20:33:13 +0200515 pm_runtime_disable(dev);
Magnus Damm119f5e42013-03-13 20:32:13 +0900516 return ret;
517}
518
519static int gpio_rcar_remove(struct platform_device *pdev)
520{
521 struct gpio_rcar_priv *p = platform_get_drvdata(pdev);
Magnus Damm119f5e42013-03-13 20:32:13 +0900522
abdoulaye berthe9f5132a2014-07-12 22:30:12 +0200523 gpiochip_remove(&p->gpio_chip);
Magnus Damm119f5e42013-03-13 20:32:13 +0900524
Geert Uytterhoevendf0c6c82014-04-14 20:33:13 +0200525 pm_runtime_disable(&pdev->dev);
Magnus Damm119f5e42013-03-13 20:32:13 +0900526 return 0;
527}
528
Hien Dang51750fb2018-02-05 04:15:02 +0900529#ifdef CONFIG_PM_SLEEP
530static int gpio_rcar_suspend(struct device *dev)
531{
532 struct gpio_rcar_priv *p = dev_get_drvdata(dev);
533
534 p->bank_info.iointsel = gpio_rcar_read(p, IOINTSEL);
535 p->bank_info.inoutsel = gpio_rcar_read(p, INOUTSEL);
536 p->bank_info.outdt = gpio_rcar_read(p, OUTDT);
537 p->bank_info.intmsk = gpio_rcar_read(p, INTMSK);
538 p->bank_info.posneg = gpio_rcar_read(p, POSNEG);
539 p->bank_info.edglevel = gpio_rcar_read(p, EDGLEVEL);
540 if (p->has_both_edge_trigger)
541 p->bank_info.bothedge = gpio_rcar_read(p, BOTHEDGE);
542
Geert Uytterhoeven9ac79ba2018-02-12 14:55:13 +0100543 if (atomic_read(&p->wakeup_path))
544 device_set_wakeup_path(dev);
545
Hien Dang51750fb2018-02-05 04:15:02 +0900546 return 0;
547}
548
549static int gpio_rcar_resume(struct device *dev)
550{
551 struct gpio_rcar_priv *p = dev_get_drvdata(dev);
552 unsigned int offset;
553 u32 mask;
554
555 for (offset = 0; offset < p->gpio_chip.ngpio; offset++) {
Biju Das496069b2018-08-07 08:57:02 +0100556 if (!gpiochip_line_is_valid(&p->gpio_chip, offset))
557 continue;
558
Hien Dang51750fb2018-02-05 04:15:02 +0900559 mask = BIT(offset);
560 /* I/O pin */
561 if (!(p->bank_info.iointsel & mask)) {
562 if (p->bank_info.inoutsel & mask)
563 gpio_rcar_direction_output(
564 &p->gpio_chip, offset,
565 !!(p->bank_info.outdt & mask));
566 else
567 gpio_rcar_direction_input(&p->gpio_chip,
568 offset);
569 } else {
570 /* Interrupt pin */
571 gpio_rcar_config_interrupt_input_mode(
572 p,
573 offset,
574 !(p->bank_info.posneg & mask),
575 !(p->bank_info.edglevel & mask),
576 !!(p->bank_info.bothedge & mask));
577
578 if (p->bank_info.intmsk & mask)
579 gpio_rcar_write(p, MSKCLR, mask);
580 }
581 }
582
583 return 0;
584}
585#endif /* CONFIG_PM_SLEEP*/
586
587static SIMPLE_DEV_PM_OPS(gpio_rcar_pm_ops, gpio_rcar_suspend, gpio_rcar_resume);
588
Magnus Damm119f5e42013-03-13 20:32:13 +0900589static struct platform_driver gpio_rcar_device_driver = {
590 .probe = gpio_rcar_probe,
591 .remove = gpio_rcar_remove,
592 .driver = {
593 .name = "gpio_rcar",
Hien Dang51750fb2018-02-05 04:15:02 +0900594 .pm = &gpio_rcar_pm_ops,
Laurent Pinchart159f8a02013-05-21 13:40:06 +0200595 .of_match_table = of_match_ptr(gpio_rcar_of_table),
Magnus Damm119f5e42013-03-13 20:32:13 +0900596 }
597};
598
599module_platform_driver(gpio_rcar_device_driver);
600
601MODULE_AUTHOR("Magnus Damm");
602MODULE_DESCRIPTION("Renesas R-Car GPIO Driver");
603MODULE_LICENSE("GPL v2");