blob: ebaea8b1594b7fc4919a71fdac7ed9ef8418df7b [file] [log] [blame]
Magnus Damm119f5e42013-03-13 20:32:13 +09001/*
2 * Renesas R-Car GPIO Support
3 *
Hisashi Nakamura1fd2b492014-11-07 20:54:08 +09004 * Copyright (C) 2014 Renesas Electronics Corporation
Magnus Damm119f5e42013-03-13 20:32:13 +09005 * Copyright (C) 2013 Magnus Damm
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
17#include <linux/err.h>
18#include <linux/gpio.h>
19#include <linux/init.h>
20#include <linux/interrupt.h>
21#include <linux/io.h>
22#include <linux/ioport.h>
23#include <linux/irq.h>
Magnus Damm119f5e42013-03-13 20:32:13 +090024#include <linux/module.h>
Sachin Kamatbd0bf462013-10-16 15:35:02 +053025#include <linux/of.h>
Geert Uytterhoevenf9f2a6f2017-10-04 14:16:16 +020026#include <linux/of_device.h>
Laurent Pinchartdc3465a2013-03-10 03:27:00 +010027#include <linux/pinctrl/consumer.h>
Magnus Damm119f5e42013-03-13 20:32:13 +090028#include <linux/platform_device.h>
Geert Uytterhoevendf0c6c82014-04-14 20:33:13 +020029#include <linux/pm_runtime.h>
Magnus Damm119f5e42013-03-13 20:32:13 +090030#include <linux/spinlock.h>
31#include <linux/slab.h>
32
33struct gpio_rcar_priv {
34 void __iomem *base;
35 spinlock_t lock;
Magnus Damm119f5e42013-03-13 20:32:13 +090036 struct platform_device *pdev;
37 struct gpio_chip gpio_chip;
38 struct irq_chip irq_chip;
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +010039 unsigned int irq_parent;
Geert Uytterhoeven9ac79ba2018-02-12 14:55:13 +010040 atomic_t wakeup_path;
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +010041 bool has_both_edge_trigger;
Magnus Damm119f5e42013-03-13 20:32:13 +090042};
43
Geert Uytterhoeven3dc1e682015-03-18 19:41:08 +010044#define IOINTSEL 0x00 /* General IO/Interrupt Switching Register */
45#define INOUTSEL 0x04 /* General Input/Output Switching Register */
46#define OUTDT 0x08 /* General Output Register */
47#define INDT 0x0c /* General Input Register */
48#define INTDT 0x10 /* Interrupt Display Register */
49#define INTCLR 0x14 /* Interrupt Clear Register */
50#define INTMSK 0x18 /* Interrupt Mask Register */
51#define MSKCLR 0x1c /* Interrupt Mask Clear Register */
52#define POSNEG 0x20 /* Positive/Negative Logic Select Register */
53#define EDGLEVEL 0x24 /* Edge/level Select Register */
54#define FILONOFF 0x28 /* Chattering Prevention On/Off Register */
55#define BOTHEDGE 0x4c /* One Edge/Both Edge Select Register */
Magnus Damm119f5e42013-03-13 20:32:13 +090056
Laurent Pinchart159f8a02013-05-21 13:40:06 +020057#define RCAR_MAX_GPIO_PER_BANK 32
58
Magnus Damm119f5e42013-03-13 20:32:13 +090059static inline u32 gpio_rcar_read(struct gpio_rcar_priv *p, int offs)
60{
61 return ioread32(p->base + offs);
62}
63
64static inline void gpio_rcar_write(struct gpio_rcar_priv *p, int offs,
65 u32 value)
66{
67 iowrite32(value, p->base + offs);
68}
69
70static void gpio_rcar_modify_bit(struct gpio_rcar_priv *p, int offs,
71 int bit, bool value)
72{
73 u32 tmp = gpio_rcar_read(p, offs);
74
75 if (value)
76 tmp |= BIT(bit);
77 else
78 tmp &= ~BIT(bit);
79
80 gpio_rcar_write(p, offs, tmp);
81}
82
83static void gpio_rcar_irq_disable(struct irq_data *d)
84{
Geert Uytterhoevenc7f3c5d2015-01-12 11:07:59 +010085 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijc7b6f452015-12-07 14:12:45 +010086 struct gpio_rcar_priv *p = gpiochip_get_data(gc);
Magnus Damm119f5e42013-03-13 20:32:13 +090087
88 gpio_rcar_write(p, INTMSK, ~BIT(irqd_to_hwirq(d)));
89}
90
91static void gpio_rcar_irq_enable(struct irq_data *d)
92{
Geert Uytterhoevenc7f3c5d2015-01-12 11:07:59 +010093 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijc7b6f452015-12-07 14:12:45 +010094 struct gpio_rcar_priv *p = gpiochip_get_data(gc);
Magnus Damm119f5e42013-03-13 20:32:13 +090095
96 gpio_rcar_write(p, MSKCLR, BIT(irqd_to_hwirq(d)));
97}
98
99static void gpio_rcar_config_interrupt_input_mode(struct gpio_rcar_priv *p,
100 unsigned int hwirq,
101 bool active_high_rising_edge,
Simon Horman7e1092b2013-05-24 18:47:24 +0900102 bool level_trigger,
103 bool both)
Magnus Damm119f5e42013-03-13 20:32:13 +0900104{
105 unsigned long flags;
106
107 /* follow steps in the GPIO documentation for
108 * "Setting Edge-Sensitive Interrupt Input Mode" and
109 * "Setting Level-Sensitive Interrupt Input Mode"
110 */
111
112 spin_lock_irqsave(&p->lock, flags);
113
114 /* Configure postive or negative logic in POSNEG */
115 gpio_rcar_modify_bit(p, POSNEG, hwirq, !active_high_rising_edge);
116
117 /* Configure edge or level trigger in EDGLEVEL */
118 gpio_rcar_modify_bit(p, EDGLEVEL, hwirq, !level_trigger);
119
Simon Horman7e1092b2013-05-24 18:47:24 +0900120 /* Select one edge or both edges in BOTHEDGE */
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100121 if (p->has_both_edge_trigger)
Simon Horman7e1092b2013-05-24 18:47:24 +0900122 gpio_rcar_modify_bit(p, BOTHEDGE, hwirq, both);
123
Magnus Damm119f5e42013-03-13 20:32:13 +0900124 /* Select "Interrupt Input Mode" in IOINTSEL */
125 gpio_rcar_modify_bit(p, IOINTSEL, hwirq, true);
126
127 /* Write INTCLR in case of edge trigger */
128 if (!level_trigger)
129 gpio_rcar_write(p, INTCLR, BIT(hwirq));
130
131 spin_unlock_irqrestore(&p->lock, flags);
132}
133
134static int gpio_rcar_irq_set_type(struct irq_data *d, unsigned int type)
135{
Geert Uytterhoevenc7f3c5d2015-01-12 11:07:59 +0100136 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijc7b6f452015-12-07 14:12:45 +0100137 struct gpio_rcar_priv *p = gpiochip_get_data(gc);
Magnus Damm119f5e42013-03-13 20:32:13 +0900138 unsigned int hwirq = irqd_to_hwirq(d);
139
140 dev_dbg(&p->pdev->dev, "sense irq = %d, type = %d\n", hwirq, type);
141
142 switch (type & IRQ_TYPE_SENSE_MASK) {
143 case IRQ_TYPE_LEVEL_HIGH:
Simon Horman7e1092b2013-05-24 18:47:24 +0900144 gpio_rcar_config_interrupt_input_mode(p, hwirq, true, true,
145 false);
Magnus Damm119f5e42013-03-13 20:32:13 +0900146 break;
147 case IRQ_TYPE_LEVEL_LOW:
Simon Horman7e1092b2013-05-24 18:47:24 +0900148 gpio_rcar_config_interrupt_input_mode(p, hwirq, false, true,
149 false);
Magnus Damm119f5e42013-03-13 20:32:13 +0900150 break;
151 case IRQ_TYPE_EDGE_RISING:
Simon Horman7e1092b2013-05-24 18:47:24 +0900152 gpio_rcar_config_interrupt_input_mode(p, hwirq, true, false,
153 false);
Magnus Damm119f5e42013-03-13 20:32:13 +0900154 break;
155 case IRQ_TYPE_EDGE_FALLING:
Simon Horman7e1092b2013-05-24 18:47:24 +0900156 gpio_rcar_config_interrupt_input_mode(p, hwirq, false, false,
157 false);
158 break;
159 case IRQ_TYPE_EDGE_BOTH:
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100160 if (!p->has_both_edge_trigger)
Simon Horman7e1092b2013-05-24 18:47:24 +0900161 return -EINVAL;
162 gpio_rcar_config_interrupt_input_mode(p, hwirq, true, false,
163 true);
Magnus Damm119f5e42013-03-13 20:32:13 +0900164 break;
165 default:
166 return -EINVAL;
167 }
168 return 0;
169}
170
Geert Uytterhoevenab82fa72015-03-18 19:41:09 +0100171static int gpio_rcar_irq_set_wake(struct irq_data *d, unsigned int on)
172{
173 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijc7b6f452015-12-07 14:12:45 +0100174 struct gpio_rcar_priv *p = gpiochip_get_data(gc);
Geert Uytterhoeven501ef0f2015-05-21 13:21:37 +0200175 int error;
Geert Uytterhoevenab82fa72015-03-18 19:41:09 +0100176
Geert Uytterhoeven501ef0f2015-05-21 13:21:37 +0200177 if (p->irq_parent) {
178 error = irq_set_irq_wake(p->irq_parent, on);
179 if (error) {
180 dev_dbg(&p->pdev->dev,
181 "irq %u doesn't support irq_set_wake\n",
182 p->irq_parent);
183 p->irq_parent = 0;
184 }
185 }
Geert Uytterhoevenab82fa72015-03-18 19:41:09 +0100186
Geert Uytterhoevenab82fa72015-03-18 19:41:09 +0100187 if (on)
Geert Uytterhoeven9ac79ba2018-02-12 14:55:13 +0100188 atomic_inc(&p->wakeup_path);
Geert Uytterhoevenab82fa72015-03-18 19:41:09 +0100189 else
Geert Uytterhoeven9ac79ba2018-02-12 14:55:13 +0100190 atomic_dec(&p->wakeup_path);
Geert Uytterhoevenab82fa72015-03-18 19:41:09 +0100191
192 return 0;
193}
194
Magnus Damm119f5e42013-03-13 20:32:13 +0900195static irqreturn_t gpio_rcar_irq_handler(int irq, void *dev_id)
196{
197 struct gpio_rcar_priv *p = dev_id;
198 u32 pending;
199 unsigned int offset, irqs_handled = 0;
200
Valentine Barshak8808b642013-11-29 22:04:09 +0400201 while ((pending = gpio_rcar_read(p, INTDT) &
202 gpio_rcar_read(p, INTMSK))) {
Magnus Damm119f5e42013-03-13 20:32:13 +0900203 offset = __ffs(pending);
204 gpio_rcar_write(p, INTCLR, BIT(offset));
Thierry Redingf0fbe7b2017-11-07 19:15:47 +0100205 generic_handle_irq(irq_find_mapping(p->gpio_chip.irq.domain,
Geert Uytterhoevenc7f3c5d2015-01-12 11:07:59 +0100206 offset));
Magnus Damm119f5e42013-03-13 20:32:13 +0900207 irqs_handled++;
208 }
209
210 return irqs_handled ? IRQ_HANDLED : IRQ_NONE;
211}
212
Magnus Damm119f5e42013-03-13 20:32:13 +0900213static void gpio_rcar_config_general_input_output_mode(struct gpio_chip *chip,
214 unsigned int gpio,
215 bool output)
216{
Linus Walleijc7b6f452015-12-07 14:12:45 +0100217 struct gpio_rcar_priv *p = gpiochip_get_data(chip);
Magnus Damm119f5e42013-03-13 20:32:13 +0900218 unsigned long flags;
219
220 /* follow steps in the GPIO documentation for
221 * "Setting General Output Mode" and
222 * "Setting General Input Mode"
223 */
224
225 spin_lock_irqsave(&p->lock, flags);
226
227 /* Configure postive logic in POSNEG */
228 gpio_rcar_modify_bit(p, POSNEG, gpio, false);
229
230 /* Select "General Input/Output Mode" in IOINTSEL */
231 gpio_rcar_modify_bit(p, IOINTSEL, gpio, false);
232
233 /* Select Input Mode or Output Mode in INOUTSEL */
234 gpio_rcar_modify_bit(p, INOUTSEL, gpio, output);
235
236 spin_unlock_irqrestore(&p->lock, flags);
237}
238
Laurent Pinchartdc3465a2013-03-10 03:27:00 +0100239static int gpio_rcar_request(struct gpio_chip *chip, unsigned offset)
240{
Geert Uytterhoeven2d654722016-12-08 18:32:28 +0100241 struct gpio_rcar_priv *p = gpiochip_get_data(chip);
242 int error;
243
244 error = pm_runtime_get_sync(&p->pdev->dev);
245 if (error < 0)
246 return error;
247
Linus Walleija9a1d2a2017-09-22 11:02:10 +0200248 error = pinctrl_gpio_request(chip->base + offset);
Geert Uytterhoeven2d654722016-12-08 18:32:28 +0100249 if (error)
250 pm_runtime_put(&p->pdev->dev);
251
252 return error;
Laurent Pinchartdc3465a2013-03-10 03:27:00 +0100253}
254
255static void gpio_rcar_free(struct gpio_chip *chip, unsigned offset)
256{
Geert Uytterhoeven2d654722016-12-08 18:32:28 +0100257 struct gpio_rcar_priv *p = gpiochip_get_data(chip);
258
Linus Walleija9a1d2a2017-09-22 11:02:10 +0200259 pinctrl_gpio_free(chip->base + offset);
Laurent Pinchartdc3465a2013-03-10 03:27:00 +0100260
Linus Walleijce0e2c62016-04-12 10:05:22 +0200261 /*
262 * Set the GPIO as an input to ensure that the next GPIO request won't
Laurent Pinchartdc3465a2013-03-10 03:27:00 +0100263 * drive the GPIO pin as an output.
264 */
265 gpio_rcar_config_general_input_output_mode(chip, offset, false);
Geert Uytterhoeven2d654722016-12-08 18:32:28 +0100266
267 pm_runtime_put(&p->pdev->dev);
Laurent Pinchartdc3465a2013-03-10 03:27:00 +0100268}
269
Magnus Damm119f5e42013-03-13 20:32:13 +0900270static int gpio_rcar_direction_input(struct gpio_chip *chip, unsigned offset)
271{
272 gpio_rcar_config_general_input_output_mode(chip, offset, false);
273 return 0;
274}
275
276static int gpio_rcar_get(struct gpio_chip *chip, unsigned offset)
277{
Magnus Dammae9550f2013-06-17 08:41:52 +0900278 u32 bit = BIT(offset);
279
280 /* testing on r8a7790 shows that INDT does not show correct pin state
281 * when configured as output, so use OUTDT in case of output pins */
Linus Walleijc7b6f452015-12-07 14:12:45 +0100282 if (gpio_rcar_read(gpiochip_get_data(chip), INOUTSEL) & bit)
283 return !!(gpio_rcar_read(gpiochip_get_data(chip), OUTDT) & bit);
Magnus Dammae9550f2013-06-17 08:41:52 +0900284 else
Linus Walleijc7b6f452015-12-07 14:12:45 +0100285 return !!(gpio_rcar_read(gpiochip_get_data(chip), INDT) & bit);
Magnus Damm119f5e42013-03-13 20:32:13 +0900286}
287
288static void gpio_rcar_set(struct gpio_chip *chip, unsigned offset, int value)
289{
Linus Walleijc7b6f452015-12-07 14:12:45 +0100290 struct gpio_rcar_priv *p = gpiochip_get_data(chip);
Magnus Damm119f5e42013-03-13 20:32:13 +0900291 unsigned long flags;
292
293 spin_lock_irqsave(&p->lock, flags);
294 gpio_rcar_modify_bit(p, OUTDT, offset, value);
295 spin_unlock_irqrestore(&p->lock, flags);
296}
297
Geert Uytterhoevendbb763b82016-03-14 16:21:44 +0100298static void gpio_rcar_set_multiple(struct gpio_chip *chip, unsigned long *mask,
299 unsigned long *bits)
300{
301 struct gpio_rcar_priv *p = gpiochip_get_data(chip);
302 unsigned long flags;
303 u32 val, bankmask;
304
305 bankmask = mask[0] & GENMASK(chip->ngpio - 1, 0);
306 if (!bankmask)
307 return;
308
309 spin_lock_irqsave(&p->lock, flags);
310 val = gpio_rcar_read(p, OUTDT);
311 val &= ~bankmask;
312 val |= (bankmask & bits[0]);
313 gpio_rcar_write(p, OUTDT, val);
314 spin_unlock_irqrestore(&p->lock, flags);
315}
316
Magnus Damm119f5e42013-03-13 20:32:13 +0900317static int gpio_rcar_direction_output(struct gpio_chip *chip, unsigned offset,
318 int value)
319{
320 /* write GPIO value to output before selecting output mode of pin */
321 gpio_rcar_set(chip, offset, value);
322 gpio_rcar_config_general_input_output_mode(chip, offset, true);
323 return 0;
324}
325
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100326struct gpio_rcar_info {
327 bool has_both_edge_trigger;
328};
329
Hisashi Nakamura1fd2b492014-11-07 20:54:08 +0900330static const struct gpio_rcar_info gpio_rcar_info_gen1 = {
331 .has_both_edge_trigger = false,
332};
333
334static const struct gpio_rcar_info gpio_rcar_info_gen2 = {
335 .has_both_edge_trigger = true,
336};
337
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100338static const struct of_device_id gpio_rcar_of_table[] = {
339 {
Biju Das85bb4642017-06-21 15:27:09 +0100340 .compatible = "renesas,gpio-r8a7743",
341 /* RZ/G1 GPIO is identical to R-Car Gen2. */
342 .data = &gpio_rcar_info_gen2,
343 }, {
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100344 .compatible = "renesas,gpio-r8a7790",
Hisashi Nakamura1fd2b492014-11-07 20:54:08 +0900345 .data = &gpio_rcar_info_gen2,
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100346 }, {
347 .compatible = "renesas,gpio-r8a7791",
Hisashi Nakamura1fd2b492014-11-07 20:54:08 +0900348 .data = &gpio_rcar_info_gen2,
349 }, {
Sergei Shtylyove79c5832016-07-07 17:11:45 +0300350 .compatible = "renesas,gpio-r8a7792",
351 .data = &gpio_rcar_info_gen2,
352 }, {
Hisashi Nakamura1fd2b492014-11-07 20:54:08 +0900353 .compatible = "renesas,gpio-r8a7793",
354 .data = &gpio_rcar_info_gen2,
355 }, {
356 .compatible = "renesas,gpio-r8a7794",
357 .data = &gpio_rcar_info_gen2,
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100358 }, {
Ulrich Hecht8cd14702015-07-21 11:08:50 +0200359 .compatible = "renesas,gpio-r8a7795",
360 /* Gen3 GPIO is identical to Gen2. */
361 .data = &gpio_rcar_info_gen2,
362 }, {
Simon Horman5d2f1d62016-09-06 12:35:39 +0200363 .compatible = "renesas,gpio-r8a7796",
364 /* Gen3 GPIO is identical to Gen2. */
365 .data = &gpio_rcar_info_gen2,
366 }, {
Simon Hormandbd1dad2017-07-11 14:38:30 +0200367 .compatible = "renesas,rcar-gen1-gpio",
368 .data = &gpio_rcar_info_gen1,
369 }, {
370 .compatible = "renesas,rcar-gen2-gpio",
371 .data = &gpio_rcar_info_gen2,
372 }, {
373 .compatible = "renesas,rcar-gen3-gpio",
374 /* Gen3 GPIO is identical to Gen2. */
375 .data = &gpio_rcar_info_gen2,
376 }, {
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100377 .compatible = "renesas,gpio-rcar",
Hisashi Nakamura1fd2b492014-11-07 20:54:08 +0900378 .data = &gpio_rcar_info_gen1,
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100379 }, {
380 /* Terminator */
381 },
382};
383
384MODULE_DEVICE_TABLE(of, gpio_rcar_of_table);
385
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100386static int gpio_rcar_parse_dt(struct gpio_rcar_priv *p, unsigned int *npins)
Laurent Pinchart159f8a02013-05-21 13:40:06 +0200387{
Laurent Pinchart159f8a02013-05-21 13:40:06 +0200388 struct device_node *np = p->pdev->dev.of_node;
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100389 const struct gpio_rcar_info *info;
Laurent Pinchart159f8a02013-05-21 13:40:06 +0200390 struct of_phandle_args args;
391 int ret;
Laurent Pinchart159f8a02013-05-21 13:40:06 +0200392
Geert Uytterhoevenf9f2a6f2017-10-04 14:16:16 +0200393 info = of_device_get_match_data(&p->pdev->dev);
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100394
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100395 ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3, 0, &args);
396 *npins = ret == 0 ? args.args[2] : RCAR_MAX_GPIO_PER_BANK;
397 p->has_both_edge_trigger = info->has_both_edge_trigger;
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100398
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100399 if (*npins == 0 || *npins > RCAR_MAX_GPIO_PER_BANK) {
Laurent Pinchart159f8a02013-05-21 13:40:06 +0200400 dev_warn(&p->pdev->dev,
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100401 "Invalid number of gpio lines %u, using %u\n", *npins,
402 RCAR_MAX_GPIO_PER_BANK);
403 *npins = RCAR_MAX_GPIO_PER_BANK;
Laurent Pinchart159f8a02013-05-21 13:40:06 +0200404 }
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100405
406 return 0;
Laurent Pinchart159f8a02013-05-21 13:40:06 +0200407}
408
Magnus Damm119f5e42013-03-13 20:32:13 +0900409static int gpio_rcar_probe(struct platform_device *pdev)
410{
Magnus Damm119f5e42013-03-13 20:32:13 +0900411 struct gpio_rcar_priv *p;
412 struct resource *io, *irq;
413 struct gpio_chip *gpio_chip;
414 struct irq_chip *irq_chip;
Geert Uytterhoevenb22978f2014-03-27 21:47:36 +0100415 struct device *dev = &pdev->dev;
416 const char *name = dev_name(dev);
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100417 unsigned int npins;
Magnus Damm119f5e42013-03-13 20:32:13 +0900418 int ret;
419
Geert Uytterhoevenb22978f2014-03-27 21:47:36 +0100420 p = devm_kzalloc(dev, sizeof(*p), GFP_KERNEL);
Geert Uytterhoeven7d82bf32015-01-12 11:07:58 +0100421 if (!p)
422 return -ENOMEM;
Magnus Damm119f5e42013-03-13 20:32:13 +0900423
Magnus Damm119f5e42013-03-13 20:32:13 +0900424 p->pdev = pdev;
Magnus Damm119f5e42013-03-13 20:32:13 +0900425 spin_lock_init(&p->lock);
426
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100427 /* Get device configuration from DT node */
428 ret = gpio_rcar_parse_dt(p, &npins);
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100429 if (ret < 0)
430 return ret;
Laurent Pinchart159f8a02013-05-21 13:40:06 +0200431
432 platform_set_drvdata(pdev, p);
433
Geert Uytterhoevendf0c6c82014-04-14 20:33:13 +0200434 pm_runtime_enable(dev);
Geert Uytterhoevendf0c6c82014-04-14 20:33:13 +0200435
Magnus Damm119f5e42013-03-13 20:32:13 +0900436 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
Sergei Shtylyov5a24d4b2017-10-13 00:08:14 +0300437 if (!irq) {
438 dev_err(dev, "missing IRQ\n");
Magnus Damm119f5e42013-03-13 20:32:13 +0900439 ret = -EINVAL;
440 goto err0;
441 }
442
Sergei Shtylyov5a24d4b2017-10-13 00:08:14 +0300443 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
444 p->base = devm_ioremap_resource(dev, io);
445 if (IS_ERR(p->base)) {
446 ret = PTR_ERR(p->base);
Magnus Damm119f5e42013-03-13 20:32:13 +0900447 goto err0;
448 }
449
450 gpio_chip = &p->gpio_chip;
Laurent Pinchartdc3465a2013-03-10 03:27:00 +0100451 gpio_chip->request = gpio_rcar_request;
452 gpio_chip->free = gpio_rcar_free;
Magnus Damm119f5e42013-03-13 20:32:13 +0900453 gpio_chip->direction_input = gpio_rcar_direction_input;
454 gpio_chip->get = gpio_rcar_get;
455 gpio_chip->direction_output = gpio_rcar_direction_output;
456 gpio_chip->set = gpio_rcar_set;
Geert Uytterhoevendbb763b82016-03-14 16:21:44 +0100457 gpio_chip->set_multiple = gpio_rcar_set_multiple;
Magnus Damm119f5e42013-03-13 20:32:13 +0900458 gpio_chip->label = name;
Linus Walleij58383c782015-11-04 09:56:26 +0100459 gpio_chip->parent = dev;
Magnus Damm119f5e42013-03-13 20:32:13 +0900460 gpio_chip->owner = THIS_MODULE;
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100461 gpio_chip->base = -1;
462 gpio_chip->ngpio = npins;
Magnus Damm119f5e42013-03-13 20:32:13 +0900463
464 irq_chip = &p->irq_chip;
465 irq_chip->name = name;
Niklas Söderlund47bd38a2016-12-08 18:32:27 +0100466 irq_chip->parent_device = dev;
Magnus Damm119f5e42013-03-13 20:32:13 +0900467 irq_chip->irq_mask = gpio_rcar_irq_disable;
468 irq_chip->irq_unmask = gpio_rcar_irq_enable;
Magnus Damm119f5e42013-03-13 20:32:13 +0900469 irq_chip->irq_set_type = gpio_rcar_irq_set_type;
Geert Uytterhoevenab82fa72015-03-18 19:41:09 +0100470 irq_chip->irq_set_wake = gpio_rcar_irq_set_wake;
471 irq_chip->flags = IRQCHIP_SET_TYPE_MASKED | IRQCHIP_MASK_ON_SUSPEND;
Magnus Damm119f5e42013-03-13 20:32:13 +0900472
Linus Walleijc7b6f452015-12-07 14:12:45 +0100473 ret = gpiochip_add_data(gpio_chip, p);
Geert Uytterhoevenc7f3c5d2015-01-12 11:07:59 +0100474 if (ret) {
475 dev_err(dev, "failed to add GPIO controller\n");
Dan Carpenter0c8aab82013-11-07 10:56:51 +0300476 goto err0;
Magnus Damm119f5e42013-03-13 20:32:13 +0900477 }
478
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100479 ret = gpiochip_irqchip_add(gpio_chip, irq_chip, 0, handle_level_irq,
480 IRQ_TYPE_NONE);
Geert Uytterhoevenc7f3c5d2015-01-12 11:07:59 +0100481 if (ret) {
482 dev_err(dev, "cannot add irqchip\n");
483 goto err1;
484 }
485
Geert Uytterhoevenab82fa72015-03-18 19:41:09 +0100486 p->irq_parent = irq->start;
Geert Uytterhoevenb22978f2014-03-27 21:47:36 +0100487 if (devm_request_irq(dev, irq->start, gpio_rcar_irq_handler,
488 IRQF_SHARED, name, p)) {
489 dev_err(dev, "failed to request IRQ\n");
Magnus Damm119f5e42013-03-13 20:32:13 +0900490 ret = -ENOENT;
491 goto err1;
492 }
493
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100494 dev_info(dev, "driving %d GPIOs\n", npins);
Laurent Pinchartdc3465a2013-03-10 03:27:00 +0100495
Magnus Damm119f5e42013-03-13 20:32:13 +0900496 return 0;
497
498err1:
Geert Uytterhoeven4d84b9e2015-03-18 19:41:07 +0100499 gpiochip_remove(gpio_chip);
Magnus Damm119f5e42013-03-13 20:32:13 +0900500err0:
Geert Uytterhoevendf0c6c82014-04-14 20:33:13 +0200501 pm_runtime_disable(dev);
Magnus Damm119f5e42013-03-13 20:32:13 +0900502 return ret;
503}
504
505static int gpio_rcar_remove(struct platform_device *pdev)
506{
507 struct gpio_rcar_priv *p = platform_get_drvdata(pdev);
Magnus Damm119f5e42013-03-13 20:32:13 +0900508
abdoulaye berthe9f5132a2014-07-12 22:30:12 +0200509 gpiochip_remove(&p->gpio_chip);
Magnus Damm119f5e42013-03-13 20:32:13 +0900510
Geert Uytterhoevendf0c6c82014-04-14 20:33:13 +0200511 pm_runtime_disable(&pdev->dev);
Magnus Damm119f5e42013-03-13 20:32:13 +0900512 return 0;
513}
514
Geert Uytterhoeven9ac79ba2018-02-12 14:55:13 +0100515static int __maybe_unused gpio_rcar_suspend(struct device *dev)
516{
517 struct gpio_rcar_priv *p = dev_get_drvdata(dev);
518
519 if (atomic_read(&p->wakeup_path))
520 device_set_wakeup_path(dev);
521
522 return 0;
523}
524
525static SIMPLE_DEV_PM_OPS(gpio_rcar_pm_ops, gpio_rcar_suspend, NULL);
526
Magnus Damm119f5e42013-03-13 20:32:13 +0900527static struct platform_driver gpio_rcar_device_driver = {
528 .probe = gpio_rcar_probe,
529 .remove = gpio_rcar_remove,
530 .driver = {
531 .name = "gpio_rcar",
Geert Uytterhoeven9ac79ba2018-02-12 14:55:13 +0100532 .pm = &gpio_rcar_pm_ops,
Laurent Pinchart159f8a02013-05-21 13:40:06 +0200533 .of_match_table = of_match_ptr(gpio_rcar_of_table),
Magnus Damm119f5e42013-03-13 20:32:13 +0900534 }
535};
536
537module_platform_driver(gpio_rcar_device_driver);
538
539MODULE_AUTHOR("Magnus Damm");
540MODULE_DESCRIPTION("Renesas R-Car GPIO Driver");
541MODULE_LICENSE("GPL v2");