blob: 068ce25ffd28f2dffab6138c0d461314e26bac93 [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;
Vladimir Zapolskiya53f7952018-11-22 22:19:41 +020038 struct device *dev;
Magnus Damm119f5e42013-03-13 20:32:13 +090039 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
Vladimir Zapolskiya53f7952018-11-22 22:19:41 +0200143 dev_dbg(p->dev, "sense irq = %d, type = %d\n", hwirq, type);
Magnus Damm119f5e42013-03-13 20:32:13 +0900144
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) {
Vladimir Zapolskiya53f7952018-11-22 22:19:41 +0200183 dev_dbg(p->dev, "irq %u doesn't support irq_set_wake\n",
Geert Uytterhoeven501ef0f2015-05-21 13:21:37 +0200184 p->irq_parent);
185 p->irq_parent = 0;
186 }
187 }
Geert Uytterhoevenab82fa72015-03-18 19:41:09 +0100188
Geert Uytterhoevenab82fa72015-03-18 19:41:09 +0100189 if (on)
Geert Uytterhoeven9ac79ba2018-02-12 14:55:13 +0100190 atomic_inc(&p->wakeup_path);
Geert Uytterhoevenab82fa72015-03-18 19:41:09 +0100191 else
Geert Uytterhoeven9ac79ba2018-02-12 14:55:13 +0100192 atomic_dec(&p->wakeup_path);
Geert Uytterhoevenab82fa72015-03-18 19:41:09 +0100193
194 return 0;
195}
196
Magnus Damm119f5e42013-03-13 20:32:13 +0900197static irqreturn_t gpio_rcar_irq_handler(int irq, void *dev_id)
198{
199 struct gpio_rcar_priv *p = dev_id;
200 u32 pending;
201 unsigned int offset, irqs_handled = 0;
202
Valentine Barshak8808b642013-11-29 22:04:09 +0400203 while ((pending = gpio_rcar_read(p, INTDT) &
204 gpio_rcar_read(p, INTMSK))) {
Magnus Damm119f5e42013-03-13 20:32:13 +0900205 offset = __ffs(pending);
206 gpio_rcar_write(p, INTCLR, BIT(offset));
Thierry Redingf0fbe7b2017-11-07 19:15:47 +0100207 generic_handle_irq(irq_find_mapping(p->gpio_chip.irq.domain,
Geert Uytterhoevenc7f3c5d2015-01-12 11:07:59 +0100208 offset));
Magnus Damm119f5e42013-03-13 20:32:13 +0900209 irqs_handled++;
210 }
211
212 return irqs_handled ? IRQ_HANDLED : IRQ_NONE;
213}
214
Magnus Damm119f5e42013-03-13 20:32:13 +0900215static void gpio_rcar_config_general_input_output_mode(struct gpio_chip *chip,
216 unsigned int gpio,
217 bool output)
218{
Linus Walleijc7b6f452015-12-07 14:12:45 +0100219 struct gpio_rcar_priv *p = gpiochip_get_data(chip);
Magnus Damm119f5e42013-03-13 20:32:13 +0900220 unsigned long flags;
221
222 /* follow steps in the GPIO documentation for
223 * "Setting General Output Mode" and
224 * "Setting General Input Mode"
225 */
226
227 spin_lock_irqsave(&p->lock, flags);
228
229 /* Configure postive logic in POSNEG */
230 gpio_rcar_modify_bit(p, POSNEG, gpio, false);
231
232 /* Select "General Input/Output Mode" in IOINTSEL */
233 gpio_rcar_modify_bit(p, IOINTSEL, gpio, false);
234
235 /* Select Input Mode or Output Mode in INOUTSEL */
236 gpio_rcar_modify_bit(p, INOUTSEL, gpio, output);
237
238 spin_unlock_irqrestore(&p->lock, flags);
239}
240
Laurent Pinchartdc3465a2013-03-10 03:27:00 +0100241static int gpio_rcar_request(struct gpio_chip *chip, unsigned offset)
242{
Geert Uytterhoeven2d654722016-12-08 18:32:28 +0100243 struct gpio_rcar_priv *p = gpiochip_get_data(chip);
244 int error;
245
Vladimir Zapolskiya53f7952018-11-22 22:19:41 +0200246 error = pm_runtime_get_sync(p->dev);
Geert Uytterhoeven2d654722016-12-08 18:32:28 +0100247 if (error < 0)
248 return error;
249
Linus Walleija9a1d2a2017-09-22 11:02:10 +0200250 error = pinctrl_gpio_request(chip->base + offset);
Geert Uytterhoeven2d654722016-12-08 18:32:28 +0100251 if (error)
Vladimir Zapolskiya53f7952018-11-22 22:19:41 +0200252 pm_runtime_put(p->dev);
Geert Uytterhoeven2d654722016-12-08 18:32:28 +0100253
254 return error;
Laurent Pinchartdc3465a2013-03-10 03:27:00 +0100255}
256
257static void gpio_rcar_free(struct gpio_chip *chip, unsigned offset)
258{
Geert Uytterhoeven2d654722016-12-08 18:32:28 +0100259 struct gpio_rcar_priv *p = gpiochip_get_data(chip);
260
Linus Walleija9a1d2a2017-09-22 11:02:10 +0200261 pinctrl_gpio_free(chip->base + offset);
Laurent Pinchartdc3465a2013-03-10 03:27:00 +0100262
Linus Walleijce0e2c62016-04-12 10:05:22 +0200263 /*
264 * Set the GPIO as an input to ensure that the next GPIO request won't
Laurent Pinchartdc3465a2013-03-10 03:27:00 +0100265 * drive the GPIO pin as an output.
266 */
267 gpio_rcar_config_general_input_output_mode(chip, offset, false);
Geert Uytterhoeven2d654722016-12-08 18:32:28 +0100268
Vladimir Zapolskiya53f7952018-11-22 22:19:41 +0200269 pm_runtime_put(p->dev);
Laurent Pinchartdc3465a2013-03-10 03:27:00 +0100270}
271
Geert Uytterhoevenad817292018-07-12 11:15:01 +0200272static int gpio_rcar_get_direction(struct gpio_chip *chip, unsigned int offset)
273{
274 struct gpio_rcar_priv *p = gpiochip_get_data(chip);
275
276 return !(gpio_rcar_read(p, INOUTSEL) & BIT(offset));
277}
278
Magnus Damm119f5e42013-03-13 20:32:13 +0900279static int gpio_rcar_direction_input(struct gpio_chip *chip, unsigned offset)
280{
281 gpio_rcar_config_general_input_output_mode(chip, offset, false);
282 return 0;
283}
284
285static int gpio_rcar_get(struct gpio_chip *chip, unsigned offset)
286{
Magnus Dammae9550f2013-06-17 08:41:52 +0900287 u32 bit = BIT(offset);
288
289 /* testing on r8a7790 shows that INDT does not show correct pin state
290 * when configured as output, so use OUTDT in case of output pins */
Linus Walleijc7b6f452015-12-07 14:12:45 +0100291 if (gpio_rcar_read(gpiochip_get_data(chip), INOUTSEL) & bit)
292 return !!(gpio_rcar_read(gpiochip_get_data(chip), OUTDT) & bit);
Magnus Dammae9550f2013-06-17 08:41:52 +0900293 else
Linus Walleijc7b6f452015-12-07 14:12:45 +0100294 return !!(gpio_rcar_read(gpiochip_get_data(chip), INDT) & bit);
Magnus Damm119f5e42013-03-13 20:32:13 +0900295}
296
297static void gpio_rcar_set(struct gpio_chip *chip, unsigned offset, int value)
298{
Linus Walleijc7b6f452015-12-07 14:12:45 +0100299 struct gpio_rcar_priv *p = gpiochip_get_data(chip);
Magnus Damm119f5e42013-03-13 20:32:13 +0900300 unsigned long flags;
301
302 spin_lock_irqsave(&p->lock, flags);
303 gpio_rcar_modify_bit(p, OUTDT, offset, value);
304 spin_unlock_irqrestore(&p->lock, flags);
305}
306
Geert Uytterhoevendbb763b82016-03-14 16:21:44 +0100307static void gpio_rcar_set_multiple(struct gpio_chip *chip, unsigned long *mask,
308 unsigned long *bits)
309{
310 struct gpio_rcar_priv *p = gpiochip_get_data(chip);
311 unsigned long flags;
312 u32 val, bankmask;
313
314 bankmask = mask[0] & GENMASK(chip->ngpio - 1, 0);
Biju Das496069b2018-08-07 08:57:02 +0100315 if (chip->valid_mask)
316 bankmask &= chip->valid_mask[0];
317
Geert Uytterhoevendbb763b82016-03-14 16:21:44 +0100318 if (!bankmask)
319 return;
320
321 spin_lock_irqsave(&p->lock, flags);
322 val = gpio_rcar_read(p, OUTDT);
323 val &= ~bankmask;
324 val |= (bankmask & bits[0]);
325 gpio_rcar_write(p, OUTDT, val);
326 spin_unlock_irqrestore(&p->lock, flags);
327}
328
Magnus Damm119f5e42013-03-13 20:32:13 +0900329static int gpio_rcar_direction_output(struct gpio_chip *chip, unsigned offset,
330 int value)
331{
332 /* write GPIO value to output before selecting output mode of pin */
333 gpio_rcar_set(chip, offset, value);
334 gpio_rcar_config_general_input_output_mode(chip, offset, true);
335 return 0;
336}
337
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100338struct gpio_rcar_info {
339 bool has_both_edge_trigger;
340};
341
Hisashi Nakamura1fd2b492014-11-07 20:54:08 +0900342static const struct gpio_rcar_info gpio_rcar_info_gen1 = {
343 .has_both_edge_trigger = false,
344};
345
346static const struct gpio_rcar_info gpio_rcar_info_gen2 = {
347 .has_both_edge_trigger = true,
348};
349
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100350static const struct of_device_id gpio_rcar_of_table[] = {
351 {
Biju Das85bb4642017-06-21 15:27:09 +0100352 .compatible = "renesas,gpio-r8a7743",
353 /* RZ/G1 GPIO is identical to R-Car Gen2. */
354 .data = &gpio_rcar_info_gen2,
355 }, {
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100356 .compatible = "renesas,gpio-r8a7790",
Hisashi Nakamura1fd2b492014-11-07 20:54:08 +0900357 .data = &gpio_rcar_info_gen2,
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100358 }, {
359 .compatible = "renesas,gpio-r8a7791",
Hisashi Nakamura1fd2b492014-11-07 20:54:08 +0900360 .data = &gpio_rcar_info_gen2,
361 }, {
Sergei Shtylyove79c5832016-07-07 17:11:45 +0300362 .compatible = "renesas,gpio-r8a7792",
363 .data = &gpio_rcar_info_gen2,
364 }, {
Hisashi Nakamura1fd2b492014-11-07 20:54:08 +0900365 .compatible = "renesas,gpio-r8a7793",
366 .data = &gpio_rcar_info_gen2,
367 }, {
368 .compatible = "renesas,gpio-r8a7794",
369 .data = &gpio_rcar_info_gen2,
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100370 }, {
Ulrich Hecht8cd14702015-07-21 11:08:50 +0200371 .compatible = "renesas,gpio-r8a7795",
372 /* Gen3 GPIO is identical to Gen2. */
373 .data = &gpio_rcar_info_gen2,
374 }, {
Simon Horman5d2f1d62016-09-06 12:35:39 +0200375 .compatible = "renesas,gpio-r8a7796",
376 /* Gen3 GPIO is identical to Gen2. */
377 .data = &gpio_rcar_info_gen2,
378 }, {
Simon Hormandbd1dad2017-07-11 14:38:30 +0200379 .compatible = "renesas,rcar-gen1-gpio",
380 .data = &gpio_rcar_info_gen1,
381 }, {
382 .compatible = "renesas,rcar-gen2-gpio",
383 .data = &gpio_rcar_info_gen2,
384 }, {
385 .compatible = "renesas,rcar-gen3-gpio",
386 /* Gen3 GPIO is identical to Gen2. */
387 .data = &gpio_rcar_info_gen2,
388 }, {
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100389 .compatible = "renesas,gpio-rcar",
Hisashi Nakamura1fd2b492014-11-07 20:54:08 +0900390 .data = &gpio_rcar_info_gen1,
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100391 }, {
392 /* Terminator */
393 },
394};
395
396MODULE_DEVICE_TABLE(of, gpio_rcar_of_table);
397
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100398static int gpio_rcar_parse_dt(struct gpio_rcar_priv *p, unsigned int *npins)
Laurent Pinchart159f8a02013-05-21 13:40:06 +0200399{
Vladimir Zapolskiya53f7952018-11-22 22:19:41 +0200400 struct device_node *np = p->dev->of_node;
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100401 const struct gpio_rcar_info *info;
Laurent Pinchart159f8a02013-05-21 13:40:06 +0200402 struct of_phandle_args args;
403 int ret;
Laurent Pinchart159f8a02013-05-21 13:40:06 +0200404
Vladimir Zapolskiya53f7952018-11-22 22:19:41 +0200405 info = of_device_get_match_data(p->dev);
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100406
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100407 ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3, 0, &args);
408 *npins = ret == 0 ? args.args[2] : RCAR_MAX_GPIO_PER_BANK;
409 p->has_both_edge_trigger = info->has_both_edge_trigger;
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100410
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100411 if (*npins == 0 || *npins > RCAR_MAX_GPIO_PER_BANK) {
Vladimir Zapolskiya53f7952018-11-22 22:19:41 +0200412 dev_warn(p->dev, "Invalid number of gpio lines %u, using %u\n",
413 *npins, RCAR_MAX_GPIO_PER_BANK);
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100414 *npins = RCAR_MAX_GPIO_PER_BANK;
Laurent Pinchart159f8a02013-05-21 13:40:06 +0200415 }
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100416
417 return 0;
Laurent Pinchart159f8a02013-05-21 13:40:06 +0200418}
419
Magnus Damm119f5e42013-03-13 20:32:13 +0900420static int gpio_rcar_probe(struct platform_device *pdev)
421{
Magnus Damm119f5e42013-03-13 20:32:13 +0900422 struct gpio_rcar_priv *p;
423 struct resource *io, *irq;
424 struct gpio_chip *gpio_chip;
425 struct irq_chip *irq_chip;
Geert Uytterhoevenb22978f2014-03-27 21:47:36 +0100426 struct device *dev = &pdev->dev;
427 const char *name = dev_name(dev);
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100428 unsigned int npins;
Magnus Damm119f5e42013-03-13 20:32:13 +0900429 int ret;
430
Geert Uytterhoevenb22978f2014-03-27 21:47:36 +0100431 p = devm_kzalloc(dev, sizeof(*p), GFP_KERNEL);
Geert Uytterhoeven7d82bf32015-01-12 11:07:58 +0100432 if (!p)
433 return -ENOMEM;
Magnus Damm119f5e42013-03-13 20:32:13 +0900434
Vladimir Zapolskiya53f7952018-11-22 22:19:41 +0200435 p->dev = dev;
Magnus Damm119f5e42013-03-13 20:32:13 +0900436 spin_lock_init(&p->lock);
437
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100438 /* Get device configuration from DT node */
439 ret = gpio_rcar_parse_dt(p, &npins);
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100440 if (ret < 0)
441 return ret;
Laurent Pinchart159f8a02013-05-21 13:40:06 +0200442
443 platform_set_drvdata(pdev, p);
444
Geert Uytterhoevendf0c6c82014-04-14 20:33:13 +0200445 pm_runtime_enable(dev);
Geert Uytterhoevendf0c6c82014-04-14 20:33:13 +0200446
Magnus Damm119f5e42013-03-13 20:32:13 +0900447 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
Sergei Shtylyov5a24d4b2017-10-13 00:08:14 +0300448 if (!irq) {
449 dev_err(dev, "missing IRQ\n");
Magnus Damm119f5e42013-03-13 20:32:13 +0900450 ret = -EINVAL;
451 goto err0;
452 }
453
Sergei Shtylyov5a24d4b2017-10-13 00:08:14 +0300454 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
455 p->base = devm_ioremap_resource(dev, io);
456 if (IS_ERR(p->base)) {
457 ret = PTR_ERR(p->base);
Magnus Damm119f5e42013-03-13 20:32:13 +0900458 goto err0;
459 }
460
461 gpio_chip = &p->gpio_chip;
Laurent Pinchartdc3465a2013-03-10 03:27:00 +0100462 gpio_chip->request = gpio_rcar_request;
463 gpio_chip->free = gpio_rcar_free;
Geert Uytterhoevenad817292018-07-12 11:15:01 +0200464 gpio_chip->get_direction = gpio_rcar_get_direction;
Magnus Damm119f5e42013-03-13 20:32:13 +0900465 gpio_chip->direction_input = gpio_rcar_direction_input;
466 gpio_chip->get = gpio_rcar_get;
467 gpio_chip->direction_output = gpio_rcar_direction_output;
468 gpio_chip->set = gpio_rcar_set;
Geert Uytterhoevendbb763b82016-03-14 16:21:44 +0100469 gpio_chip->set_multiple = gpio_rcar_set_multiple;
Magnus Damm119f5e42013-03-13 20:32:13 +0900470 gpio_chip->label = name;
Linus Walleij58383c782015-11-04 09:56:26 +0100471 gpio_chip->parent = dev;
Magnus Damm119f5e42013-03-13 20:32:13 +0900472 gpio_chip->owner = THIS_MODULE;
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100473 gpio_chip->base = -1;
474 gpio_chip->ngpio = npins;
Magnus Damm119f5e42013-03-13 20:32:13 +0900475
476 irq_chip = &p->irq_chip;
477 irq_chip->name = name;
Niklas Söderlund47bd38a2016-12-08 18:32:27 +0100478 irq_chip->parent_device = dev;
Magnus Damm119f5e42013-03-13 20:32:13 +0900479 irq_chip->irq_mask = gpio_rcar_irq_disable;
480 irq_chip->irq_unmask = gpio_rcar_irq_enable;
Magnus Damm119f5e42013-03-13 20:32:13 +0900481 irq_chip->irq_set_type = gpio_rcar_irq_set_type;
Geert Uytterhoevenab82fa72015-03-18 19:41:09 +0100482 irq_chip->irq_set_wake = gpio_rcar_irq_set_wake;
483 irq_chip->flags = IRQCHIP_SET_TYPE_MASKED | IRQCHIP_MASK_ON_SUSPEND;
Magnus Damm119f5e42013-03-13 20:32:13 +0900484
Linus Walleijc7b6f452015-12-07 14:12:45 +0100485 ret = gpiochip_add_data(gpio_chip, p);
Geert Uytterhoevenc7f3c5d2015-01-12 11:07:59 +0100486 if (ret) {
487 dev_err(dev, "failed to add GPIO controller\n");
Dan Carpenter0c8aab82013-11-07 10:56:51 +0300488 goto err0;
Magnus Damm119f5e42013-03-13 20:32:13 +0900489 }
490
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100491 ret = gpiochip_irqchip_add(gpio_chip, irq_chip, 0, handle_level_irq,
492 IRQ_TYPE_NONE);
Geert Uytterhoevenc7f3c5d2015-01-12 11:07:59 +0100493 if (ret) {
494 dev_err(dev, "cannot add irqchip\n");
495 goto err1;
496 }
497
Geert Uytterhoevenab82fa72015-03-18 19:41:09 +0100498 p->irq_parent = irq->start;
Geert Uytterhoevenb22978f2014-03-27 21:47:36 +0100499 if (devm_request_irq(dev, irq->start, gpio_rcar_irq_handler,
500 IRQF_SHARED, name, p)) {
501 dev_err(dev, "failed to request IRQ\n");
Magnus Damm119f5e42013-03-13 20:32:13 +0900502 ret = -ENOENT;
503 goto err1;
504 }
505
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100506 dev_info(dev, "driving %d GPIOs\n", npins);
Laurent Pinchartdc3465a2013-03-10 03:27:00 +0100507
Magnus Damm119f5e42013-03-13 20:32:13 +0900508 return 0;
509
510err1:
Geert Uytterhoeven4d84b9e2015-03-18 19:41:07 +0100511 gpiochip_remove(gpio_chip);
Magnus Damm119f5e42013-03-13 20:32:13 +0900512err0:
Geert Uytterhoevendf0c6c82014-04-14 20:33:13 +0200513 pm_runtime_disable(dev);
Magnus Damm119f5e42013-03-13 20:32:13 +0900514 return ret;
515}
516
517static int gpio_rcar_remove(struct platform_device *pdev)
518{
519 struct gpio_rcar_priv *p = platform_get_drvdata(pdev);
Magnus Damm119f5e42013-03-13 20:32:13 +0900520
abdoulaye berthe9f5132a2014-07-12 22:30:12 +0200521 gpiochip_remove(&p->gpio_chip);
Magnus Damm119f5e42013-03-13 20:32:13 +0900522
Geert Uytterhoevendf0c6c82014-04-14 20:33:13 +0200523 pm_runtime_disable(&pdev->dev);
Magnus Damm119f5e42013-03-13 20:32:13 +0900524 return 0;
525}
526
Hien Dang51750fb2018-02-05 04:15:02 +0900527#ifdef CONFIG_PM_SLEEP
528static int gpio_rcar_suspend(struct device *dev)
529{
530 struct gpio_rcar_priv *p = dev_get_drvdata(dev);
531
532 p->bank_info.iointsel = gpio_rcar_read(p, IOINTSEL);
533 p->bank_info.inoutsel = gpio_rcar_read(p, INOUTSEL);
534 p->bank_info.outdt = gpio_rcar_read(p, OUTDT);
535 p->bank_info.intmsk = gpio_rcar_read(p, INTMSK);
536 p->bank_info.posneg = gpio_rcar_read(p, POSNEG);
537 p->bank_info.edglevel = gpio_rcar_read(p, EDGLEVEL);
538 if (p->has_both_edge_trigger)
539 p->bank_info.bothedge = gpio_rcar_read(p, BOTHEDGE);
540
Geert Uytterhoeven9ac79ba2018-02-12 14:55:13 +0100541 if (atomic_read(&p->wakeup_path))
542 device_set_wakeup_path(dev);
543
Hien Dang51750fb2018-02-05 04:15:02 +0900544 return 0;
545}
546
547static int gpio_rcar_resume(struct device *dev)
548{
549 struct gpio_rcar_priv *p = dev_get_drvdata(dev);
550 unsigned int offset;
551 u32 mask;
552
553 for (offset = 0; offset < p->gpio_chip.ngpio; offset++) {
Biju Das496069b2018-08-07 08:57:02 +0100554 if (!gpiochip_line_is_valid(&p->gpio_chip, offset))
555 continue;
556
Hien Dang51750fb2018-02-05 04:15:02 +0900557 mask = BIT(offset);
558 /* I/O pin */
559 if (!(p->bank_info.iointsel & mask)) {
560 if (p->bank_info.inoutsel & mask)
561 gpio_rcar_direction_output(
562 &p->gpio_chip, offset,
563 !!(p->bank_info.outdt & mask));
564 else
565 gpio_rcar_direction_input(&p->gpio_chip,
566 offset);
567 } else {
568 /* Interrupt pin */
569 gpio_rcar_config_interrupt_input_mode(
570 p,
571 offset,
572 !(p->bank_info.posneg & mask),
573 !(p->bank_info.edglevel & mask),
574 !!(p->bank_info.bothedge & mask));
575
576 if (p->bank_info.intmsk & mask)
577 gpio_rcar_write(p, MSKCLR, mask);
578 }
579 }
580
581 return 0;
582}
583#endif /* CONFIG_PM_SLEEP*/
584
585static SIMPLE_DEV_PM_OPS(gpio_rcar_pm_ops, gpio_rcar_suspend, gpio_rcar_resume);
586
Magnus Damm119f5e42013-03-13 20:32:13 +0900587static struct platform_driver gpio_rcar_device_driver = {
588 .probe = gpio_rcar_probe,
589 .remove = gpio_rcar_remove,
590 .driver = {
591 .name = "gpio_rcar",
Hien Dang51750fb2018-02-05 04:15:02 +0900592 .pm = &gpio_rcar_pm_ops,
Laurent Pinchart159f8a02013-05-21 13:40:06 +0200593 .of_match_table = of_match_ptr(gpio_rcar_of_table),
Magnus Damm119f5e42013-03-13 20:32:13 +0900594 }
595};
596
597module_platform_driver(gpio_rcar_device_driver);
598
599MODULE_AUTHOR("Magnus Damm");
600MODULE_DESCRIPTION("Renesas R-Car GPIO Driver");
601MODULE_LICENSE("GPL v2");