blob: e5b0dbe43c018601579c7e9a3da39511f7c6f87e [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
Geert Uytterhoevenab82fa72015-03-18 19:41:09 +010017#include <linux/clk.h>
Magnus Damm119f5e42013-03-13 20:32:13 +090018#include <linux/err.h>
19#include <linux/gpio.h>
20#include <linux/init.h>
21#include <linux/interrupt.h>
22#include <linux/io.h>
23#include <linux/ioport.h>
24#include <linux/irq.h>
Magnus Damm119f5e42013-03-13 20:32:13 +090025#include <linux/module.h>
Sachin Kamatbd0bf462013-10-16 15:35:02 +053026#include <linux/of.h>
Geert Uytterhoevenf9f2a6f2017-10-04 14:16:16 +020027#include <linux/of_device.h>
Laurent Pinchartdc3465a2013-03-10 03:27:00 +010028#include <linux/pinctrl/consumer.h>
Magnus Damm119f5e42013-03-13 20:32:13 +090029#include <linux/platform_device.h>
Geert Uytterhoevendf0c6c82014-04-14 20:33:13 +020030#include <linux/pm_runtime.h>
Magnus Damm119f5e42013-03-13 20:32:13 +090031#include <linux/spinlock.h>
32#include <linux/slab.h>
33
Hien Dang51750fb2018-02-05 04:15:02 +090034struct gpio_rcar_bank_info {
35 u32 iointsel;
36 u32 inoutsel;
37 u32 outdt;
38 u32 posneg;
39 u32 edglevel;
40 u32 bothedge;
41 u32 intmsk;
42};
43
Magnus Damm119f5e42013-03-13 20:32:13 +090044struct gpio_rcar_priv {
45 void __iomem *base;
46 spinlock_t lock;
Magnus Damm119f5e42013-03-13 20:32:13 +090047 struct platform_device *pdev;
48 struct gpio_chip gpio_chip;
49 struct irq_chip irq_chip;
Geert Uytterhoevenab82fa72015-03-18 19:41:09 +010050 struct clk *clk;
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +010051 unsigned int irq_parent;
52 bool has_both_edge_trigger;
Geert Uytterhoevene1fef9e2015-12-04 16:33:53 +010053 bool needs_clk;
Hien Dang51750fb2018-02-05 04:15:02 +090054 struct gpio_rcar_bank_info bank_info;
Magnus Damm119f5e42013-03-13 20:32:13 +090055};
56
Geert Uytterhoeven3dc1e682015-03-18 19:41:08 +010057#define IOINTSEL 0x00 /* General IO/Interrupt Switching Register */
58#define INOUTSEL 0x04 /* General Input/Output Switching Register */
59#define OUTDT 0x08 /* General Output Register */
60#define INDT 0x0c /* General Input Register */
61#define INTDT 0x10 /* Interrupt Display Register */
62#define INTCLR 0x14 /* Interrupt Clear Register */
63#define INTMSK 0x18 /* Interrupt Mask Register */
64#define MSKCLR 0x1c /* Interrupt Mask Clear Register */
65#define POSNEG 0x20 /* Positive/Negative Logic Select Register */
66#define EDGLEVEL 0x24 /* Edge/level Select Register */
67#define FILONOFF 0x28 /* Chattering Prevention On/Off Register */
68#define BOTHEDGE 0x4c /* One Edge/Both Edge Select Register */
Magnus Damm119f5e42013-03-13 20:32:13 +090069
Laurent Pinchart159f8a02013-05-21 13:40:06 +020070#define RCAR_MAX_GPIO_PER_BANK 32
71
Magnus Damm119f5e42013-03-13 20:32:13 +090072static inline u32 gpio_rcar_read(struct gpio_rcar_priv *p, int offs)
73{
74 return ioread32(p->base + offs);
75}
76
77static inline void gpio_rcar_write(struct gpio_rcar_priv *p, int offs,
78 u32 value)
79{
80 iowrite32(value, p->base + offs);
81}
82
83static void gpio_rcar_modify_bit(struct gpio_rcar_priv *p, int offs,
84 int bit, bool value)
85{
86 u32 tmp = gpio_rcar_read(p, offs);
87
88 if (value)
89 tmp |= BIT(bit);
90 else
91 tmp &= ~BIT(bit);
92
93 gpio_rcar_write(p, offs, tmp);
94}
95
96static void gpio_rcar_irq_disable(struct irq_data *d)
97{
Geert Uytterhoevenc7f3c5d2015-01-12 11:07:59 +010098 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijc7b6f452015-12-07 14:12:45 +010099 struct gpio_rcar_priv *p = gpiochip_get_data(gc);
Magnus Damm119f5e42013-03-13 20:32:13 +0900100
101 gpio_rcar_write(p, INTMSK, ~BIT(irqd_to_hwirq(d)));
102}
103
104static void gpio_rcar_irq_enable(struct irq_data *d)
105{
Geert Uytterhoevenc7f3c5d2015-01-12 11:07:59 +0100106 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijc7b6f452015-12-07 14:12:45 +0100107 struct gpio_rcar_priv *p = gpiochip_get_data(gc);
Magnus Damm119f5e42013-03-13 20:32:13 +0900108
109 gpio_rcar_write(p, MSKCLR, BIT(irqd_to_hwirq(d)));
110}
111
112static void gpio_rcar_config_interrupt_input_mode(struct gpio_rcar_priv *p,
113 unsigned int hwirq,
114 bool active_high_rising_edge,
Simon Horman7e1092b2013-05-24 18:47:24 +0900115 bool level_trigger,
116 bool both)
Magnus Damm119f5e42013-03-13 20:32:13 +0900117{
118 unsigned long flags;
119
120 /* follow steps in the GPIO documentation for
121 * "Setting Edge-Sensitive Interrupt Input Mode" and
122 * "Setting Level-Sensitive Interrupt Input Mode"
123 */
124
125 spin_lock_irqsave(&p->lock, flags);
126
127 /* Configure postive or negative logic in POSNEG */
128 gpio_rcar_modify_bit(p, POSNEG, hwirq, !active_high_rising_edge);
129
130 /* Configure edge or level trigger in EDGLEVEL */
131 gpio_rcar_modify_bit(p, EDGLEVEL, hwirq, !level_trigger);
132
Simon Horman7e1092b2013-05-24 18:47:24 +0900133 /* Select one edge or both edges in BOTHEDGE */
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100134 if (p->has_both_edge_trigger)
Simon Horman7e1092b2013-05-24 18:47:24 +0900135 gpio_rcar_modify_bit(p, BOTHEDGE, hwirq, both);
136
Magnus Damm119f5e42013-03-13 20:32:13 +0900137 /* Select "Interrupt Input Mode" in IOINTSEL */
138 gpio_rcar_modify_bit(p, IOINTSEL, hwirq, true);
139
140 /* Write INTCLR in case of edge trigger */
141 if (!level_trigger)
142 gpio_rcar_write(p, INTCLR, BIT(hwirq));
143
144 spin_unlock_irqrestore(&p->lock, flags);
145}
146
147static int gpio_rcar_irq_set_type(struct irq_data *d, unsigned int type)
148{
Geert Uytterhoevenc7f3c5d2015-01-12 11:07:59 +0100149 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijc7b6f452015-12-07 14:12:45 +0100150 struct gpio_rcar_priv *p = gpiochip_get_data(gc);
Magnus Damm119f5e42013-03-13 20:32:13 +0900151 unsigned int hwirq = irqd_to_hwirq(d);
152
153 dev_dbg(&p->pdev->dev, "sense irq = %d, type = %d\n", hwirq, type);
154
155 switch (type & IRQ_TYPE_SENSE_MASK) {
156 case IRQ_TYPE_LEVEL_HIGH:
Simon Horman7e1092b2013-05-24 18:47:24 +0900157 gpio_rcar_config_interrupt_input_mode(p, hwirq, true, true,
158 false);
Magnus Damm119f5e42013-03-13 20:32:13 +0900159 break;
160 case IRQ_TYPE_LEVEL_LOW:
Simon Horman7e1092b2013-05-24 18:47:24 +0900161 gpio_rcar_config_interrupt_input_mode(p, hwirq, false, true,
162 false);
Magnus Damm119f5e42013-03-13 20:32:13 +0900163 break;
164 case IRQ_TYPE_EDGE_RISING:
Simon Horman7e1092b2013-05-24 18:47:24 +0900165 gpio_rcar_config_interrupt_input_mode(p, hwirq, true, false,
166 false);
Magnus Damm119f5e42013-03-13 20:32:13 +0900167 break;
168 case IRQ_TYPE_EDGE_FALLING:
Simon Horman7e1092b2013-05-24 18:47:24 +0900169 gpio_rcar_config_interrupt_input_mode(p, hwirq, false, false,
170 false);
171 break;
172 case IRQ_TYPE_EDGE_BOTH:
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100173 if (!p->has_both_edge_trigger)
Simon Horman7e1092b2013-05-24 18:47:24 +0900174 return -EINVAL;
175 gpio_rcar_config_interrupt_input_mode(p, hwirq, true, false,
176 true);
Magnus Damm119f5e42013-03-13 20:32:13 +0900177 break;
178 default:
179 return -EINVAL;
180 }
181 return 0;
182}
183
Geert Uytterhoevenab82fa72015-03-18 19:41:09 +0100184static int gpio_rcar_irq_set_wake(struct irq_data *d, unsigned int on)
185{
186 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijc7b6f452015-12-07 14:12:45 +0100187 struct gpio_rcar_priv *p = gpiochip_get_data(gc);
Geert Uytterhoeven501ef0f2015-05-21 13:21:37 +0200188 int error;
Geert Uytterhoevenab82fa72015-03-18 19:41:09 +0100189
Geert Uytterhoeven501ef0f2015-05-21 13:21:37 +0200190 if (p->irq_parent) {
191 error = irq_set_irq_wake(p->irq_parent, on);
192 if (error) {
193 dev_dbg(&p->pdev->dev,
194 "irq %u doesn't support irq_set_wake\n",
195 p->irq_parent);
196 p->irq_parent = 0;
197 }
198 }
Geert Uytterhoevenab82fa72015-03-18 19:41:09 +0100199
200 if (!p->clk)
201 return 0;
202
203 if (on)
204 clk_enable(p->clk);
205 else
206 clk_disable(p->clk);
207
208 return 0;
209}
210
Magnus Damm119f5e42013-03-13 20:32:13 +0900211static irqreturn_t gpio_rcar_irq_handler(int irq, void *dev_id)
212{
213 struct gpio_rcar_priv *p = dev_id;
214 u32 pending;
215 unsigned int offset, irqs_handled = 0;
216
Valentine Barshak8808b642013-11-29 22:04:09 +0400217 while ((pending = gpio_rcar_read(p, INTDT) &
218 gpio_rcar_read(p, INTMSK))) {
Magnus Damm119f5e42013-03-13 20:32:13 +0900219 offset = __ffs(pending);
220 gpio_rcar_write(p, INTCLR, BIT(offset));
Thierry Redingf0fbe7b2017-11-07 19:15:47 +0100221 generic_handle_irq(irq_find_mapping(p->gpio_chip.irq.domain,
Geert Uytterhoevenc7f3c5d2015-01-12 11:07:59 +0100222 offset));
Magnus Damm119f5e42013-03-13 20:32:13 +0900223 irqs_handled++;
224 }
225
226 return irqs_handled ? IRQ_HANDLED : IRQ_NONE;
227}
228
Magnus Damm119f5e42013-03-13 20:32:13 +0900229static void gpio_rcar_config_general_input_output_mode(struct gpio_chip *chip,
230 unsigned int gpio,
231 bool output)
232{
Linus Walleijc7b6f452015-12-07 14:12:45 +0100233 struct gpio_rcar_priv *p = gpiochip_get_data(chip);
Magnus Damm119f5e42013-03-13 20:32:13 +0900234 unsigned long flags;
235
236 /* follow steps in the GPIO documentation for
237 * "Setting General Output Mode" and
238 * "Setting General Input Mode"
239 */
240
241 spin_lock_irqsave(&p->lock, flags);
242
243 /* Configure postive logic in POSNEG */
244 gpio_rcar_modify_bit(p, POSNEG, gpio, false);
245
246 /* Select "General Input/Output Mode" in IOINTSEL */
247 gpio_rcar_modify_bit(p, IOINTSEL, gpio, false);
248
249 /* Select Input Mode or Output Mode in INOUTSEL */
250 gpio_rcar_modify_bit(p, INOUTSEL, gpio, output);
251
252 spin_unlock_irqrestore(&p->lock, flags);
253}
254
Laurent Pinchartdc3465a2013-03-10 03:27:00 +0100255static int gpio_rcar_request(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 int error;
259
260 error = pm_runtime_get_sync(&p->pdev->dev);
261 if (error < 0)
262 return error;
263
Linus Walleija9a1d2a2017-09-22 11:02:10 +0200264 error = pinctrl_gpio_request(chip->base + offset);
Geert Uytterhoeven2d654722016-12-08 18:32:28 +0100265 if (error)
266 pm_runtime_put(&p->pdev->dev);
267
268 return error;
Laurent Pinchartdc3465a2013-03-10 03:27:00 +0100269}
270
271static void gpio_rcar_free(struct gpio_chip *chip, unsigned offset)
272{
Geert Uytterhoeven2d654722016-12-08 18:32:28 +0100273 struct gpio_rcar_priv *p = gpiochip_get_data(chip);
274
Linus Walleija9a1d2a2017-09-22 11:02:10 +0200275 pinctrl_gpio_free(chip->base + offset);
Laurent Pinchartdc3465a2013-03-10 03:27:00 +0100276
Linus Walleijce0e2c62016-04-12 10:05:22 +0200277 /*
278 * Set the GPIO as an input to ensure that the next GPIO request won't
Laurent Pinchartdc3465a2013-03-10 03:27:00 +0100279 * drive the GPIO pin as an output.
280 */
281 gpio_rcar_config_general_input_output_mode(chip, offset, false);
Geert Uytterhoeven2d654722016-12-08 18:32:28 +0100282
283 pm_runtime_put(&p->pdev->dev);
Laurent Pinchartdc3465a2013-03-10 03:27:00 +0100284}
285
Magnus Damm119f5e42013-03-13 20:32:13 +0900286static int gpio_rcar_direction_input(struct gpio_chip *chip, unsigned offset)
287{
288 gpio_rcar_config_general_input_output_mode(chip, offset, false);
289 return 0;
290}
291
292static int gpio_rcar_get(struct gpio_chip *chip, unsigned offset)
293{
Magnus Dammae9550f2013-06-17 08:41:52 +0900294 u32 bit = BIT(offset);
295
296 /* testing on r8a7790 shows that INDT does not show correct pin state
297 * when configured as output, so use OUTDT in case of output pins */
Linus Walleijc7b6f452015-12-07 14:12:45 +0100298 if (gpio_rcar_read(gpiochip_get_data(chip), INOUTSEL) & bit)
299 return !!(gpio_rcar_read(gpiochip_get_data(chip), OUTDT) & bit);
Magnus Dammae9550f2013-06-17 08:41:52 +0900300 else
Linus Walleijc7b6f452015-12-07 14:12:45 +0100301 return !!(gpio_rcar_read(gpiochip_get_data(chip), INDT) & bit);
Magnus Damm119f5e42013-03-13 20:32:13 +0900302}
303
304static void gpio_rcar_set(struct gpio_chip *chip, unsigned offset, int value)
305{
Linus Walleijc7b6f452015-12-07 14:12:45 +0100306 struct gpio_rcar_priv *p = gpiochip_get_data(chip);
Magnus Damm119f5e42013-03-13 20:32:13 +0900307 unsigned long flags;
308
309 spin_lock_irqsave(&p->lock, flags);
310 gpio_rcar_modify_bit(p, OUTDT, offset, value);
311 spin_unlock_irqrestore(&p->lock, flags);
312}
313
Geert Uytterhoevendbb763b82016-03-14 16:21:44 +0100314static void gpio_rcar_set_multiple(struct gpio_chip *chip, unsigned long *mask,
315 unsigned long *bits)
316{
317 struct gpio_rcar_priv *p = gpiochip_get_data(chip);
318 unsigned long flags;
319 u32 val, bankmask;
320
321 bankmask = mask[0] & GENMASK(chip->ngpio - 1, 0);
322 if (!bankmask)
323 return;
324
325 spin_lock_irqsave(&p->lock, flags);
326 val = gpio_rcar_read(p, OUTDT);
327 val &= ~bankmask;
328 val |= (bankmask & bits[0]);
329 gpio_rcar_write(p, OUTDT, val);
330 spin_unlock_irqrestore(&p->lock, flags);
331}
332
Magnus Damm119f5e42013-03-13 20:32:13 +0900333static int gpio_rcar_direction_output(struct gpio_chip *chip, unsigned offset,
334 int value)
335{
336 /* write GPIO value to output before selecting output mode of pin */
337 gpio_rcar_set(chip, offset, value);
338 gpio_rcar_config_general_input_output_mode(chip, offset, true);
339 return 0;
340}
341
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100342struct gpio_rcar_info {
343 bool has_both_edge_trigger;
Geert Uytterhoevene1fef9e2015-12-04 16:33:53 +0100344 bool needs_clk;
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100345};
346
Hisashi Nakamura1fd2b492014-11-07 20:54:08 +0900347static const struct gpio_rcar_info gpio_rcar_info_gen1 = {
348 .has_both_edge_trigger = false,
Geert Uytterhoevene1fef9e2015-12-04 16:33:53 +0100349 .needs_clk = false,
Hisashi Nakamura1fd2b492014-11-07 20:54:08 +0900350};
351
352static const struct gpio_rcar_info gpio_rcar_info_gen2 = {
353 .has_both_edge_trigger = true,
Geert Uytterhoevene1fef9e2015-12-04 16:33:53 +0100354 .needs_clk = true,
Hisashi Nakamura1fd2b492014-11-07 20:54:08 +0900355};
356
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100357static const struct of_device_id gpio_rcar_of_table[] = {
358 {
Biju Das85bb4642017-06-21 15:27:09 +0100359 .compatible = "renesas,gpio-r8a7743",
360 /* RZ/G1 GPIO is identical to R-Car Gen2. */
361 .data = &gpio_rcar_info_gen2,
362 }, {
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100363 .compatible = "renesas,gpio-r8a7790",
Hisashi Nakamura1fd2b492014-11-07 20:54:08 +0900364 .data = &gpio_rcar_info_gen2,
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100365 }, {
366 .compatible = "renesas,gpio-r8a7791",
Hisashi Nakamura1fd2b492014-11-07 20:54:08 +0900367 .data = &gpio_rcar_info_gen2,
368 }, {
Sergei Shtylyove79c5832016-07-07 17:11:45 +0300369 .compatible = "renesas,gpio-r8a7792",
370 .data = &gpio_rcar_info_gen2,
371 }, {
Hisashi Nakamura1fd2b492014-11-07 20:54:08 +0900372 .compatible = "renesas,gpio-r8a7793",
373 .data = &gpio_rcar_info_gen2,
374 }, {
375 .compatible = "renesas,gpio-r8a7794",
376 .data = &gpio_rcar_info_gen2,
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100377 }, {
Ulrich Hecht8cd14702015-07-21 11:08:50 +0200378 .compatible = "renesas,gpio-r8a7795",
379 /* Gen3 GPIO is identical to Gen2. */
380 .data = &gpio_rcar_info_gen2,
381 }, {
Simon Horman5d2f1d62016-09-06 12:35:39 +0200382 .compatible = "renesas,gpio-r8a7796",
383 /* Gen3 GPIO is identical to Gen2. */
384 .data = &gpio_rcar_info_gen2,
385 }, {
Simon Hormandbd1dad2017-07-11 14:38:30 +0200386 .compatible = "renesas,rcar-gen1-gpio",
387 .data = &gpio_rcar_info_gen1,
388 }, {
389 .compatible = "renesas,rcar-gen2-gpio",
390 .data = &gpio_rcar_info_gen2,
391 }, {
392 .compatible = "renesas,rcar-gen3-gpio",
393 /* Gen3 GPIO is identical to Gen2. */
394 .data = &gpio_rcar_info_gen2,
395 }, {
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100396 .compatible = "renesas,gpio-rcar",
Hisashi Nakamura1fd2b492014-11-07 20:54:08 +0900397 .data = &gpio_rcar_info_gen1,
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100398 }, {
399 /* Terminator */
400 },
401};
402
403MODULE_DEVICE_TABLE(of, gpio_rcar_of_table);
404
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100405static int gpio_rcar_parse_dt(struct gpio_rcar_priv *p, unsigned int *npins)
Laurent Pinchart159f8a02013-05-21 13:40:06 +0200406{
Laurent Pinchart159f8a02013-05-21 13:40:06 +0200407 struct device_node *np = p->pdev->dev.of_node;
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100408 const struct gpio_rcar_info *info;
Laurent Pinchart159f8a02013-05-21 13:40:06 +0200409 struct of_phandle_args args;
410 int ret;
Laurent Pinchart159f8a02013-05-21 13:40:06 +0200411
Geert Uytterhoevenf9f2a6f2017-10-04 14:16:16 +0200412 info = of_device_get_match_data(&p->pdev->dev);
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100413
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100414 ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3, 0, &args);
415 *npins = ret == 0 ? args.args[2] : RCAR_MAX_GPIO_PER_BANK;
416 p->has_both_edge_trigger = info->has_both_edge_trigger;
Geert Uytterhoevene1fef9e2015-12-04 16:33:53 +0100417 p->needs_clk = info->needs_clk;
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100418
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100419 if (*npins == 0 || *npins > RCAR_MAX_GPIO_PER_BANK) {
Laurent Pinchart159f8a02013-05-21 13:40:06 +0200420 dev_warn(&p->pdev->dev,
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100421 "Invalid number of gpio lines %u, using %u\n", *npins,
422 RCAR_MAX_GPIO_PER_BANK);
423 *npins = RCAR_MAX_GPIO_PER_BANK;
Laurent Pinchart159f8a02013-05-21 13:40:06 +0200424 }
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100425
426 return 0;
Laurent Pinchart159f8a02013-05-21 13:40:06 +0200427}
428
Magnus Damm119f5e42013-03-13 20:32:13 +0900429static int gpio_rcar_probe(struct platform_device *pdev)
430{
Magnus Damm119f5e42013-03-13 20:32:13 +0900431 struct gpio_rcar_priv *p;
432 struct resource *io, *irq;
433 struct gpio_chip *gpio_chip;
434 struct irq_chip *irq_chip;
Geert Uytterhoevenb22978f2014-03-27 21:47:36 +0100435 struct device *dev = &pdev->dev;
436 const char *name = dev_name(dev);
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100437 unsigned int npins;
Magnus Damm119f5e42013-03-13 20:32:13 +0900438 int ret;
439
Geert Uytterhoevenb22978f2014-03-27 21:47:36 +0100440 p = devm_kzalloc(dev, sizeof(*p), GFP_KERNEL);
Geert Uytterhoeven7d82bf32015-01-12 11:07:58 +0100441 if (!p)
442 return -ENOMEM;
Magnus Damm119f5e42013-03-13 20:32:13 +0900443
Magnus Damm119f5e42013-03-13 20:32:13 +0900444 p->pdev = pdev;
Magnus Damm119f5e42013-03-13 20:32:13 +0900445 spin_lock_init(&p->lock);
446
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100447 /* Get device configuration from DT node */
448 ret = gpio_rcar_parse_dt(p, &npins);
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100449 if (ret < 0)
450 return ret;
Laurent Pinchart159f8a02013-05-21 13:40:06 +0200451
452 platform_set_drvdata(pdev, p);
453
Geert Uytterhoevenab82fa72015-03-18 19:41:09 +0100454 p->clk = devm_clk_get(dev, NULL);
455 if (IS_ERR(p->clk)) {
Geert Uytterhoevene1fef9e2015-12-04 16:33:53 +0100456 if (p->needs_clk) {
457 dev_err(dev, "unable to get clock\n");
458 ret = PTR_ERR(p->clk);
459 goto err0;
460 }
Geert Uytterhoevenab82fa72015-03-18 19:41:09 +0100461 p->clk = NULL;
462 }
463
Geert Uytterhoevendf0c6c82014-04-14 20:33:13 +0200464 pm_runtime_enable(dev);
Geert Uytterhoevendf0c6c82014-04-14 20:33:13 +0200465
Magnus Damm119f5e42013-03-13 20:32:13 +0900466 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
Sergei Shtylyov5a24d4b2017-10-13 00:08:14 +0300467 if (!irq) {
468 dev_err(dev, "missing IRQ\n");
Magnus Damm119f5e42013-03-13 20:32:13 +0900469 ret = -EINVAL;
470 goto err0;
471 }
472
Sergei Shtylyov5a24d4b2017-10-13 00:08:14 +0300473 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
474 p->base = devm_ioremap_resource(dev, io);
475 if (IS_ERR(p->base)) {
476 ret = PTR_ERR(p->base);
Magnus Damm119f5e42013-03-13 20:32:13 +0900477 goto err0;
478 }
479
480 gpio_chip = &p->gpio_chip;
Laurent Pinchartdc3465a2013-03-10 03:27:00 +0100481 gpio_chip->request = gpio_rcar_request;
482 gpio_chip->free = gpio_rcar_free;
Magnus Damm119f5e42013-03-13 20:32:13 +0900483 gpio_chip->direction_input = gpio_rcar_direction_input;
484 gpio_chip->get = gpio_rcar_get;
485 gpio_chip->direction_output = gpio_rcar_direction_output;
486 gpio_chip->set = gpio_rcar_set;
Geert Uytterhoevendbb763b82016-03-14 16:21:44 +0100487 gpio_chip->set_multiple = gpio_rcar_set_multiple;
Magnus Damm119f5e42013-03-13 20:32:13 +0900488 gpio_chip->label = name;
Linus Walleij58383c782015-11-04 09:56:26 +0100489 gpio_chip->parent = dev;
Magnus Damm119f5e42013-03-13 20:32:13 +0900490 gpio_chip->owner = THIS_MODULE;
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100491 gpio_chip->base = -1;
492 gpio_chip->ngpio = npins;
Magnus Damm119f5e42013-03-13 20:32:13 +0900493
494 irq_chip = &p->irq_chip;
495 irq_chip->name = name;
Niklas Söderlund47bd38a2016-12-08 18:32:27 +0100496 irq_chip->parent_device = dev;
Magnus Damm119f5e42013-03-13 20:32:13 +0900497 irq_chip->irq_mask = gpio_rcar_irq_disable;
498 irq_chip->irq_unmask = gpio_rcar_irq_enable;
Magnus Damm119f5e42013-03-13 20:32:13 +0900499 irq_chip->irq_set_type = gpio_rcar_irq_set_type;
Geert Uytterhoevenab82fa72015-03-18 19:41:09 +0100500 irq_chip->irq_set_wake = gpio_rcar_irq_set_wake;
501 irq_chip->flags = IRQCHIP_SET_TYPE_MASKED | IRQCHIP_MASK_ON_SUSPEND;
Magnus Damm119f5e42013-03-13 20:32:13 +0900502
Linus Walleijc7b6f452015-12-07 14:12:45 +0100503 ret = gpiochip_add_data(gpio_chip, p);
Geert Uytterhoevenc7f3c5d2015-01-12 11:07:59 +0100504 if (ret) {
505 dev_err(dev, "failed to add GPIO controller\n");
Dan Carpenter0c8aab82013-11-07 10:56:51 +0300506 goto err0;
Magnus Damm119f5e42013-03-13 20:32:13 +0900507 }
508
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100509 ret = gpiochip_irqchip_add(gpio_chip, irq_chip, 0, handle_level_irq,
510 IRQ_TYPE_NONE);
Geert Uytterhoevenc7f3c5d2015-01-12 11:07:59 +0100511 if (ret) {
512 dev_err(dev, "cannot add irqchip\n");
513 goto err1;
514 }
515
Geert Uytterhoevenab82fa72015-03-18 19:41:09 +0100516 p->irq_parent = irq->start;
Geert Uytterhoevenb22978f2014-03-27 21:47:36 +0100517 if (devm_request_irq(dev, irq->start, gpio_rcar_irq_handler,
518 IRQF_SHARED, name, p)) {
519 dev_err(dev, "failed to request IRQ\n");
Magnus Damm119f5e42013-03-13 20:32:13 +0900520 ret = -ENOENT;
521 goto err1;
522 }
523
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100524 dev_info(dev, "driving %d GPIOs\n", npins);
Laurent Pinchartdc3465a2013-03-10 03:27:00 +0100525
Magnus Damm119f5e42013-03-13 20:32:13 +0900526 return 0;
527
528err1:
Geert Uytterhoeven4d84b9e2015-03-18 19:41:07 +0100529 gpiochip_remove(gpio_chip);
Magnus Damm119f5e42013-03-13 20:32:13 +0900530err0:
Geert Uytterhoevendf0c6c82014-04-14 20:33:13 +0200531 pm_runtime_disable(dev);
Magnus Damm119f5e42013-03-13 20:32:13 +0900532 return ret;
533}
534
535static int gpio_rcar_remove(struct platform_device *pdev)
536{
537 struct gpio_rcar_priv *p = platform_get_drvdata(pdev);
Magnus Damm119f5e42013-03-13 20:32:13 +0900538
abdoulaye berthe9f5132a2014-07-12 22:30:12 +0200539 gpiochip_remove(&p->gpio_chip);
Magnus Damm119f5e42013-03-13 20:32:13 +0900540
Geert Uytterhoevendf0c6c82014-04-14 20:33:13 +0200541 pm_runtime_disable(&pdev->dev);
Magnus Damm119f5e42013-03-13 20:32:13 +0900542 return 0;
543}
544
Hien Dang51750fb2018-02-05 04:15:02 +0900545#ifdef CONFIG_PM_SLEEP
546static int gpio_rcar_suspend(struct device *dev)
547{
548 struct gpio_rcar_priv *p = dev_get_drvdata(dev);
549
550 p->bank_info.iointsel = gpio_rcar_read(p, IOINTSEL);
551 p->bank_info.inoutsel = gpio_rcar_read(p, INOUTSEL);
552 p->bank_info.outdt = gpio_rcar_read(p, OUTDT);
553 p->bank_info.intmsk = gpio_rcar_read(p, INTMSK);
554 p->bank_info.posneg = gpio_rcar_read(p, POSNEG);
555 p->bank_info.edglevel = gpio_rcar_read(p, EDGLEVEL);
556 if (p->has_both_edge_trigger)
557 p->bank_info.bothedge = gpio_rcar_read(p, BOTHEDGE);
558
559 return 0;
560}
561
562static int gpio_rcar_resume(struct device *dev)
563{
564 struct gpio_rcar_priv *p = dev_get_drvdata(dev);
565 unsigned int offset;
566 u32 mask;
567
568 for (offset = 0; offset < p->gpio_chip.ngpio; offset++) {
569 mask = BIT(offset);
570 /* I/O pin */
571 if (!(p->bank_info.iointsel & mask)) {
572 if (p->bank_info.inoutsel & mask)
573 gpio_rcar_direction_output(
574 &p->gpio_chip, offset,
575 !!(p->bank_info.outdt & mask));
576 else
577 gpio_rcar_direction_input(&p->gpio_chip,
578 offset);
579 } else {
580 /* Interrupt pin */
581 gpio_rcar_config_interrupt_input_mode(
582 p,
583 offset,
584 !(p->bank_info.posneg & mask),
585 !(p->bank_info.edglevel & mask),
586 !!(p->bank_info.bothedge & mask));
587
588 if (p->bank_info.intmsk & mask)
589 gpio_rcar_write(p, MSKCLR, mask);
590 }
591 }
592
593 return 0;
594}
595#endif /* CONFIG_PM_SLEEP*/
596
597static SIMPLE_DEV_PM_OPS(gpio_rcar_pm_ops, gpio_rcar_suspend, gpio_rcar_resume);
598
Magnus Damm119f5e42013-03-13 20:32:13 +0900599static struct platform_driver gpio_rcar_device_driver = {
600 .probe = gpio_rcar_probe,
601 .remove = gpio_rcar_remove,
602 .driver = {
603 .name = "gpio_rcar",
Hien Dang51750fb2018-02-05 04:15:02 +0900604 .pm = &gpio_rcar_pm_ops,
Laurent Pinchart159f8a02013-05-21 13:40:06 +0200605 .of_match_table = of_match_ptr(gpio_rcar_of_table),
Magnus Damm119f5e42013-03-13 20:32:13 +0900606 }
607};
608
609module_platform_driver(gpio_rcar_device_driver);
610
611MODULE_AUTHOR("Magnus Damm");
612MODULE_DESCRIPTION("Renesas R-Car GPIO Driver");
613MODULE_LICENSE("GPL v2");