blob: e76de57dd617d7e2c918c057dcc0ced6636be7b2 [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
34struct gpio_rcar_priv {
35 void __iomem *base;
36 spinlock_t lock;
Magnus Damm119f5e42013-03-13 20:32:13 +090037 struct platform_device *pdev;
38 struct gpio_chip gpio_chip;
39 struct irq_chip irq_chip;
Geert Uytterhoevenab82fa72015-03-18 19:41:09 +010040 struct clk *clk;
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +010041 unsigned int irq_parent;
42 bool has_both_edge_trigger;
Geert Uytterhoevene1fef9e2015-12-04 16:33:53 +010043 bool needs_clk;
Magnus Damm119f5e42013-03-13 20:32:13 +090044};
45
Geert Uytterhoeven3dc1e682015-03-18 19:41:08 +010046#define IOINTSEL 0x00 /* General IO/Interrupt Switching Register */
47#define INOUTSEL 0x04 /* General Input/Output Switching Register */
48#define OUTDT 0x08 /* General Output Register */
49#define INDT 0x0c /* General Input Register */
50#define INTDT 0x10 /* Interrupt Display Register */
51#define INTCLR 0x14 /* Interrupt Clear Register */
52#define INTMSK 0x18 /* Interrupt Mask Register */
53#define MSKCLR 0x1c /* Interrupt Mask Clear Register */
54#define POSNEG 0x20 /* Positive/Negative Logic Select Register */
55#define EDGLEVEL 0x24 /* Edge/level Select Register */
56#define FILONOFF 0x28 /* Chattering Prevention On/Off Register */
57#define BOTHEDGE 0x4c /* One Edge/Both Edge Select Register */
Magnus Damm119f5e42013-03-13 20:32:13 +090058
Laurent Pinchart159f8a02013-05-21 13:40:06 +020059#define RCAR_MAX_GPIO_PER_BANK 32
60
Magnus Damm119f5e42013-03-13 20:32:13 +090061static inline u32 gpio_rcar_read(struct gpio_rcar_priv *p, int offs)
62{
63 return ioread32(p->base + offs);
64}
65
66static inline void gpio_rcar_write(struct gpio_rcar_priv *p, int offs,
67 u32 value)
68{
69 iowrite32(value, p->base + offs);
70}
71
72static void gpio_rcar_modify_bit(struct gpio_rcar_priv *p, int offs,
73 int bit, bool value)
74{
75 u32 tmp = gpio_rcar_read(p, offs);
76
77 if (value)
78 tmp |= BIT(bit);
79 else
80 tmp &= ~BIT(bit);
81
82 gpio_rcar_write(p, offs, tmp);
83}
84
85static void gpio_rcar_irq_disable(struct irq_data *d)
86{
Geert Uytterhoevenc7f3c5d2015-01-12 11:07:59 +010087 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijc7b6f452015-12-07 14:12:45 +010088 struct gpio_rcar_priv *p = gpiochip_get_data(gc);
Magnus Damm119f5e42013-03-13 20:32:13 +090089
90 gpio_rcar_write(p, INTMSK, ~BIT(irqd_to_hwirq(d)));
91}
92
93static void gpio_rcar_irq_enable(struct irq_data *d)
94{
Geert Uytterhoevenc7f3c5d2015-01-12 11:07:59 +010095 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijc7b6f452015-12-07 14:12:45 +010096 struct gpio_rcar_priv *p = gpiochip_get_data(gc);
Magnus Damm119f5e42013-03-13 20:32:13 +090097
98 gpio_rcar_write(p, MSKCLR, BIT(irqd_to_hwirq(d)));
99}
100
101static void gpio_rcar_config_interrupt_input_mode(struct gpio_rcar_priv *p,
102 unsigned int hwirq,
103 bool active_high_rising_edge,
Simon Horman7e1092b2013-05-24 18:47:24 +0900104 bool level_trigger,
105 bool both)
Magnus Damm119f5e42013-03-13 20:32:13 +0900106{
107 unsigned long flags;
108
109 /* follow steps in the GPIO documentation for
110 * "Setting Edge-Sensitive Interrupt Input Mode" and
111 * "Setting Level-Sensitive Interrupt Input Mode"
112 */
113
114 spin_lock_irqsave(&p->lock, flags);
115
116 /* Configure postive or negative logic in POSNEG */
117 gpio_rcar_modify_bit(p, POSNEG, hwirq, !active_high_rising_edge);
118
119 /* Configure edge or level trigger in EDGLEVEL */
120 gpio_rcar_modify_bit(p, EDGLEVEL, hwirq, !level_trigger);
121
Simon Horman7e1092b2013-05-24 18:47:24 +0900122 /* Select one edge or both edges in BOTHEDGE */
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100123 if (p->has_both_edge_trigger)
Simon Horman7e1092b2013-05-24 18:47:24 +0900124 gpio_rcar_modify_bit(p, BOTHEDGE, hwirq, both);
125
Magnus Damm119f5e42013-03-13 20:32:13 +0900126 /* Select "Interrupt Input Mode" in IOINTSEL */
127 gpio_rcar_modify_bit(p, IOINTSEL, hwirq, true);
128
129 /* Write INTCLR in case of edge trigger */
130 if (!level_trigger)
131 gpio_rcar_write(p, INTCLR, BIT(hwirq));
132
133 spin_unlock_irqrestore(&p->lock, flags);
134}
135
136static int gpio_rcar_irq_set_type(struct irq_data *d, unsigned int type)
137{
Geert Uytterhoevenc7f3c5d2015-01-12 11:07:59 +0100138 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijc7b6f452015-12-07 14:12:45 +0100139 struct gpio_rcar_priv *p = gpiochip_get_data(gc);
Magnus Damm119f5e42013-03-13 20:32:13 +0900140 unsigned int hwirq = irqd_to_hwirq(d);
141
142 dev_dbg(&p->pdev->dev, "sense irq = %d, type = %d\n", hwirq, type);
143
144 switch (type & IRQ_TYPE_SENSE_MASK) {
145 case IRQ_TYPE_LEVEL_HIGH:
Simon Horman7e1092b2013-05-24 18:47:24 +0900146 gpio_rcar_config_interrupt_input_mode(p, hwirq, true, true,
147 false);
Magnus Damm119f5e42013-03-13 20:32:13 +0900148 break;
149 case IRQ_TYPE_LEVEL_LOW:
Simon Horman7e1092b2013-05-24 18:47:24 +0900150 gpio_rcar_config_interrupt_input_mode(p, hwirq, false, true,
151 false);
Magnus Damm119f5e42013-03-13 20:32:13 +0900152 break;
153 case IRQ_TYPE_EDGE_RISING:
Simon Horman7e1092b2013-05-24 18:47:24 +0900154 gpio_rcar_config_interrupt_input_mode(p, hwirq, true, false,
155 false);
Magnus Damm119f5e42013-03-13 20:32:13 +0900156 break;
157 case IRQ_TYPE_EDGE_FALLING:
Simon Horman7e1092b2013-05-24 18:47:24 +0900158 gpio_rcar_config_interrupt_input_mode(p, hwirq, false, false,
159 false);
160 break;
161 case IRQ_TYPE_EDGE_BOTH:
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100162 if (!p->has_both_edge_trigger)
Simon Horman7e1092b2013-05-24 18:47:24 +0900163 return -EINVAL;
164 gpio_rcar_config_interrupt_input_mode(p, hwirq, true, false,
165 true);
Magnus Damm119f5e42013-03-13 20:32:13 +0900166 break;
167 default:
168 return -EINVAL;
169 }
170 return 0;
171}
172
Geert Uytterhoevenab82fa72015-03-18 19:41:09 +0100173static int gpio_rcar_irq_set_wake(struct irq_data *d, unsigned int on)
174{
175 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
Linus Walleijc7b6f452015-12-07 14:12:45 +0100176 struct gpio_rcar_priv *p = gpiochip_get_data(gc);
Geert Uytterhoeven501ef0f2015-05-21 13:21:37 +0200177 int error;
Geert Uytterhoevenab82fa72015-03-18 19:41:09 +0100178
Geert Uytterhoeven501ef0f2015-05-21 13:21:37 +0200179 if (p->irq_parent) {
180 error = irq_set_irq_wake(p->irq_parent, on);
181 if (error) {
182 dev_dbg(&p->pdev->dev,
183 "irq %u doesn't support irq_set_wake\n",
184 p->irq_parent);
185 p->irq_parent = 0;
186 }
187 }
Geert Uytterhoevenab82fa72015-03-18 19:41:09 +0100188
189 if (!p->clk)
190 return 0;
191
192 if (on)
193 clk_enable(p->clk);
194 else
195 clk_disable(p->clk);
196
197 return 0;
198}
199
Magnus Damm119f5e42013-03-13 20:32:13 +0900200static irqreturn_t gpio_rcar_irq_handler(int irq, void *dev_id)
201{
202 struct gpio_rcar_priv *p = dev_id;
203 u32 pending;
204 unsigned int offset, irqs_handled = 0;
205
Valentine Barshak8808b642013-11-29 22:04:09 +0400206 while ((pending = gpio_rcar_read(p, INTDT) &
207 gpio_rcar_read(p, INTMSK))) {
Magnus Damm119f5e42013-03-13 20:32:13 +0900208 offset = __ffs(pending);
209 gpio_rcar_write(p, INTCLR, BIT(offset));
Thierry Redingf0fbe7b2017-11-07 19:15:47 +0100210 generic_handle_irq(irq_find_mapping(p->gpio_chip.irq.domain,
Geert Uytterhoevenc7f3c5d2015-01-12 11:07:59 +0100211 offset));
Magnus Damm119f5e42013-03-13 20:32:13 +0900212 irqs_handled++;
213 }
214
215 return irqs_handled ? IRQ_HANDLED : IRQ_NONE;
216}
217
Magnus Damm119f5e42013-03-13 20:32:13 +0900218static void gpio_rcar_config_general_input_output_mode(struct gpio_chip *chip,
219 unsigned int gpio,
220 bool output)
221{
Linus Walleijc7b6f452015-12-07 14:12:45 +0100222 struct gpio_rcar_priv *p = gpiochip_get_data(chip);
Magnus Damm119f5e42013-03-13 20:32:13 +0900223 unsigned long flags;
224
225 /* follow steps in the GPIO documentation for
226 * "Setting General Output Mode" and
227 * "Setting General Input Mode"
228 */
229
230 spin_lock_irqsave(&p->lock, flags);
231
232 /* Configure postive logic in POSNEG */
233 gpio_rcar_modify_bit(p, POSNEG, gpio, false);
234
235 /* Select "General Input/Output Mode" in IOINTSEL */
236 gpio_rcar_modify_bit(p, IOINTSEL, gpio, false);
237
238 /* Select Input Mode or Output Mode in INOUTSEL */
239 gpio_rcar_modify_bit(p, INOUTSEL, gpio, output);
240
241 spin_unlock_irqrestore(&p->lock, flags);
242}
243
Laurent Pinchartdc3465a2013-03-10 03:27:00 +0100244static int gpio_rcar_request(struct gpio_chip *chip, unsigned offset)
245{
Geert Uytterhoeven2d654722016-12-08 18:32:28 +0100246 struct gpio_rcar_priv *p = gpiochip_get_data(chip);
247 int error;
248
249 error = pm_runtime_get_sync(&p->pdev->dev);
250 if (error < 0)
251 return error;
252
Linus Walleija9a1d2a2017-09-22 11:02:10 +0200253 error = pinctrl_gpio_request(chip->base + offset);
Geert Uytterhoeven2d654722016-12-08 18:32:28 +0100254 if (error)
255 pm_runtime_put(&p->pdev->dev);
256
257 return error;
Laurent Pinchartdc3465a2013-03-10 03:27:00 +0100258}
259
260static void gpio_rcar_free(struct gpio_chip *chip, unsigned offset)
261{
Geert Uytterhoeven2d654722016-12-08 18:32:28 +0100262 struct gpio_rcar_priv *p = gpiochip_get_data(chip);
263
Linus Walleija9a1d2a2017-09-22 11:02:10 +0200264 pinctrl_gpio_free(chip->base + offset);
Laurent Pinchartdc3465a2013-03-10 03:27:00 +0100265
Linus Walleijce0e2c62016-04-12 10:05:22 +0200266 /*
267 * Set the GPIO as an input to ensure that the next GPIO request won't
Laurent Pinchartdc3465a2013-03-10 03:27:00 +0100268 * drive the GPIO pin as an output.
269 */
270 gpio_rcar_config_general_input_output_mode(chip, offset, false);
Geert Uytterhoeven2d654722016-12-08 18:32:28 +0100271
272 pm_runtime_put(&p->pdev->dev);
Laurent Pinchartdc3465a2013-03-10 03:27:00 +0100273}
274
Magnus Damm119f5e42013-03-13 20:32:13 +0900275static int gpio_rcar_direction_input(struct gpio_chip *chip, unsigned offset)
276{
277 gpio_rcar_config_general_input_output_mode(chip, offset, false);
278 return 0;
279}
280
281static int gpio_rcar_get(struct gpio_chip *chip, unsigned offset)
282{
Magnus Dammae9550f2013-06-17 08:41:52 +0900283 u32 bit = BIT(offset);
284
285 /* testing on r8a7790 shows that INDT does not show correct pin state
286 * when configured as output, so use OUTDT in case of output pins */
Linus Walleijc7b6f452015-12-07 14:12:45 +0100287 if (gpio_rcar_read(gpiochip_get_data(chip), INOUTSEL) & bit)
288 return !!(gpio_rcar_read(gpiochip_get_data(chip), OUTDT) & bit);
Magnus Dammae9550f2013-06-17 08:41:52 +0900289 else
Linus Walleijc7b6f452015-12-07 14:12:45 +0100290 return !!(gpio_rcar_read(gpiochip_get_data(chip), INDT) & bit);
Magnus Damm119f5e42013-03-13 20:32:13 +0900291}
292
293static void gpio_rcar_set(struct gpio_chip *chip, unsigned offset, int value)
294{
Linus Walleijc7b6f452015-12-07 14:12:45 +0100295 struct gpio_rcar_priv *p = gpiochip_get_data(chip);
Magnus Damm119f5e42013-03-13 20:32:13 +0900296 unsigned long flags;
297
298 spin_lock_irqsave(&p->lock, flags);
299 gpio_rcar_modify_bit(p, OUTDT, offset, value);
300 spin_unlock_irqrestore(&p->lock, flags);
301}
302
Geert Uytterhoevendbb763b2016-03-14 16:21:44 +0100303static void gpio_rcar_set_multiple(struct gpio_chip *chip, unsigned long *mask,
304 unsigned long *bits)
305{
306 struct gpio_rcar_priv *p = gpiochip_get_data(chip);
307 unsigned long flags;
308 u32 val, bankmask;
309
310 bankmask = mask[0] & GENMASK(chip->ngpio - 1, 0);
311 if (!bankmask)
312 return;
313
314 spin_lock_irqsave(&p->lock, flags);
315 val = gpio_rcar_read(p, OUTDT);
316 val &= ~bankmask;
317 val |= (bankmask & bits[0]);
318 gpio_rcar_write(p, OUTDT, val);
319 spin_unlock_irqrestore(&p->lock, flags);
320}
321
Magnus Damm119f5e42013-03-13 20:32:13 +0900322static int gpio_rcar_direction_output(struct gpio_chip *chip, unsigned offset,
323 int value)
324{
325 /* write GPIO value to output before selecting output mode of pin */
326 gpio_rcar_set(chip, offset, value);
327 gpio_rcar_config_general_input_output_mode(chip, offset, true);
328 return 0;
329}
330
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100331struct gpio_rcar_info {
332 bool has_both_edge_trigger;
Geert Uytterhoevene1fef9e2015-12-04 16:33:53 +0100333 bool needs_clk;
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100334};
335
Hisashi Nakamura1fd2b492014-11-07 20:54:08 +0900336static const struct gpio_rcar_info gpio_rcar_info_gen1 = {
337 .has_both_edge_trigger = false,
Geert Uytterhoevene1fef9e2015-12-04 16:33:53 +0100338 .needs_clk = false,
Hisashi Nakamura1fd2b492014-11-07 20:54:08 +0900339};
340
341static const struct gpio_rcar_info gpio_rcar_info_gen2 = {
342 .has_both_edge_trigger = true,
Geert Uytterhoevene1fef9e2015-12-04 16:33:53 +0100343 .needs_clk = true,
Hisashi Nakamura1fd2b492014-11-07 20:54:08 +0900344};
345
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100346static const struct of_device_id gpio_rcar_of_table[] = {
347 {
Biju Das85bb4642017-06-21 15:27:09 +0100348 .compatible = "renesas,gpio-r8a7743",
349 /* RZ/G1 GPIO is identical to R-Car Gen2. */
350 .data = &gpio_rcar_info_gen2,
351 }, {
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100352 .compatible = "renesas,gpio-r8a7790",
Hisashi Nakamura1fd2b492014-11-07 20:54:08 +0900353 .data = &gpio_rcar_info_gen2,
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100354 }, {
355 .compatible = "renesas,gpio-r8a7791",
Hisashi Nakamura1fd2b492014-11-07 20:54:08 +0900356 .data = &gpio_rcar_info_gen2,
357 }, {
Sergei Shtylyove79c5832016-07-07 17:11:45 +0300358 .compatible = "renesas,gpio-r8a7792",
359 .data = &gpio_rcar_info_gen2,
360 }, {
Hisashi Nakamura1fd2b492014-11-07 20:54:08 +0900361 .compatible = "renesas,gpio-r8a7793",
362 .data = &gpio_rcar_info_gen2,
363 }, {
364 .compatible = "renesas,gpio-r8a7794",
365 .data = &gpio_rcar_info_gen2,
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100366 }, {
Ulrich Hecht8cd14702015-07-21 11:08:50 +0200367 .compatible = "renesas,gpio-r8a7795",
368 /* Gen3 GPIO is identical to Gen2. */
369 .data = &gpio_rcar_info_gen2,
370 }, {
Simon Horman5d2f1d62016-09-06 12:35:39 +0200371 .compatible = "renesas,gpio-r8a7796",
372 /* Gen3 GPIO is identical to Gen2. */
373 .data = &gpio_rcar_info_gen2,
374 }, {
Simon Hormandbd1dad2017-07-11 14:38:30 +0200375 .compatible = "renesas,rcar-gen1-gpio",
376 .data = &gpio_rcar_info_gen1,
377 }, {
378 .compatible = "renesas,rcar-gen2-gpio",
379 .data = &gpio_rcar_info_gen2,
380 }, {
381 .compatible = "renesas,rcar-gen3-gpio",
382 /* Gen3 GPIO is identical to Gen2. */
383 .data = &gpio_rcar_info_gen2,
384 }, {
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100385 .compatible = "renesas,gpio-rcar",
Hisashi Nakamura1fd2b492014-11-07 20:54:08 +0900386 .data = &gpio_rcar_info_gen1,
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100387 }, {
388 /* Terminator */
389 },
390};
391
392MODULE_DEVICE_TABLE(of, gpio_rcar_of_table);
393
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100394static int gpio_rcar_parse_dt(struct gpio_rcar_priv *p, unsigned int *npins)
Laurent Pinchart159f8a02013-05-21 13:40:06 +0200395{
Laurent Pinchart159f8a02013-05-21 13:40:06 +0200396 struct device_node *np = p->pdev->dev.of_node;
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100397 const struct gpio_rcar_info *info;
Laurent Pinchart159f8a02013-05-21 13:40:06 +0200398 struct of_phandle_args args;
399 int ret;
Laurent Pinchart159f8a02013-05-21 13:40:06 +0200400
Geert Uytterhoevenf9f2a6f2017-10-04 14:16:16 +0200401 info = of_device_get_match_data(&p->pdev->dev);
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100402
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100403 ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3, 0, &args);
404 *npins = ret == 0 ? args.args[2] : RCAR_MAX_GPIO_PER_BANK;
405 p->has_both_edge_trigger = info->has_both_edge_trigger;
Geert Uytterhoevene1fef9e2015-12-04 16:33:53 +0100406 p->needs_clk = info->needs_clk;
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100407
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100408 if (*npins == 0 || *npins > RCAR_MAX_GPIO_PER_BANK) {
Laurent Pinchart159f8a02013-05-21 13:40:06 +0200409 dev_warn(&p->pdev->dev,
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100410 "Invalid number of gpio lines %u, using %u\n", *npins,
411 RCAR_MAX_GPIO_PER_BANK);
412 *npins = RCAR_MAX_GPIO_PER_BANK;
Laurent Pinchart159f8a02013-05-21 13:40:06 +0200413 }
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100414
415 return 0;
Laurent Pinchart159f8a02013-05-21 13:40:06 +0200416}
417
Magnus Damm119f5e42013-03-13 20:32:13 +0900418static int gpio_rcar_probe(struct platform_device *pdev)
419{
Magnus Damm119f5e42013-03-13 20:32:13 +0900420 struct gpio_rcar_priv *p;
421 struct resource *io, *irq;
422 struct gpio_chip *gpio_chip;
423 struct irq_chip *irq_chip;
Geert Uytterhoevenb22978f2014-03-27 21:47:36 +0100424 struct device *dev = &pdev->dev;
425 const char *name = dev_name(dev);
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100426 unsigned int npins;
Magnus Damm119f5e42013-03-13 20:32:13 +0900427 int ret;
428
Geert Uytterhoevenb22978f2014-03-27 21:47:36 +0100429 p = devm_kzalloc(dev, sizeof(*p), GFP_KERNEL);
Geert Uytterhoeven7d82bf32015-01-12 11:07:58 +0100430 if (!p)
431 return -ENOMEM;
Magnus Damm119f5e42013-03-13 20:32:13 +0900432
Magnus Damm119f5e42013-03-13 20:32:13 +0900433 p->pdev = pdev;
Magnus Damm119f5e42013-03-13 20:32:13 +0900434 spin_lock_init(&p->lock);
435
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100436 /* Get device configuration from DT node */
437 ret = gpio_rcar_parse_dt(p, &npins);
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100438 if (ret < 0)
439 return ret;
Laurent Pinchart159f8a02013-05-21 13:40:06 +0200440
441 platform_set_drvdata(pdev, p);
442
Geert Uytterhoevenab82fa72015-03-18 19:41:09 +0100443 p->clk = devm_clk_get(dev, NULL);
444 if (IS_ERR(p->clk)) {
Geert Uytterhoevene1fef9e2015-12-04 16:33:53 +0100445 if (p->needs_clk) {
446 dev_err(dev, "unable to get clock\n");
447 ret = PTR_ERR(p->clk);
448 goto err0;
449 }
Geert Uytterhoevenab82fa72015-03-18 19:41:09 +0100450 p->clk = NULL;
451 }
452
Geert Uytterhoevendf0c6c82014-04-14 20:33:13 +0200453 pm_runtime_enable(dev);
Geert Uytterhoevendf0c6c82014-04-14 20:33:13 +0200454
Magnus Damm119f5e42013-03-13 20:32:13 +0900455 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
Sergei Shtylyov5a24d4b2017-10-13 00:08:14 +0300456 if (!irq) {
457 dev_err(dev, "missing IRQ\n");
Magnus Damm119f5e42013-03-13 20:32:13 +0900458 ret = -EINVAL;
459 goto err0;
460 }
461
Sergei Shtylyov5a24d4b2017-10-13 00:08:14 +0300462 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
463 p->base = devm_ioremap_resource(dev, io);
464 if (IS_ERR(p->base)) {
465 ret = PTR_ERR(p->base);
Magnus Damm119f5e42013-03-13 20:32:13 +0900466 goto err0;
467 }
468
469 gpio_chip = &p->gpio_chip;
Laurent Pinchartdc3465a2013-03-10 03:27:00 +0100470 gpio_chip->request = gpio_rcar_request;
471 gpio_chip->free = gpio_rcar_free;
Magnus Damm119f5e42013-03-13 20:32:13 +0900472 gpio_chip->direction_input = gpio_rcar_direction_input;
473 gpio_chip->get = gpio_rcar_get;
474 gpio_chip->direction_output = gpio_rcar_direction_output;
475 gpio_chip->set = gpio_rcar_set;
Geert Uytterhoevendbb763b2016-03-14 16:21:44 +0100476 gpio_chip->set_multiple = gpio_rcar_set_multiple;
Magnus Damm119f5e42013-03-13 20:32:13 +0900477 gpio_chip->label = name;
Linus Walleij58383c782015-11-04 09:56:26 +0100478 gpio_chip->parent = dev;
Magnus Damm119f5e42013-03-13 20:32:13 +0900479 gpio_chip->owner = THIS_MODULE;
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100480 gpio_chip->base = -1;
481 gpio_chip->ngpio = npins;
Magnus Damm119f5e42013-03-13 20:32:13 +0900482
483 irq_chip = &p->irq_chip;
484 irq_chip->name = name;
Niklas Söderlund47bd38a2016-12-08 18:32:27 +0100485 irq_chip->parent_device = dev;
Magnus Damm119f5e42013-03-13 20:32:13 +0900486 irq_chip->irq_mask = gpio_rcar_irq_disable;
487 irq_chip->irq_unmask = gpio_rcar_irq_enable;
Magnus Damm119f5e42013-03-13 20:32:13 +0900488 irq_chip->irq_set_type = gpio_rcar_irq_set_type;
Geert Uytterhoevenab82fa72015-03-18 19:41:09 +0100489 irq_chip->irq_set_wake = gpio_rcar_irq_set_wake;
490 irq_chip->flags = IRQCHIP_SET_TYPE_MASKED | IRQCHIP_MASK_ON_SUSPEND;
Magnus Damm119f5e42013-03-13 20:32:13 +0900491
Linus Walleijc7b6f452015-12-07 14:12:45 +0100492 ret = gpiochip_add_data(gpio_chip, p);
Geert Uytterhoevenc7f3c5d2015-01-12 11:07:59 +0100493 if (ret) {
494 dev_err(dev, "failed to add GPIO controller\n");
Dan Carpenter0c8aab82013-11-07 10:56:51 +0300495 goto err0;
Magnus Damm119f5e42013-03-13 20:32:13 +0900496 }
497
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100498 ret = gpiochip_irqchip_add(gpio_chip, irq_chip, 0, handle_level_irq,
499 IRQ_TYPE_NONE);
Geert Uytterhoevenc7f3c5d2015-01-12 11:07:59 +0100500 if (ret) {
501 dev_err(dev, "cannot add irqchip\n");
502 goto err1;
503 }
504
Geert Uytterhoevenab82fa72015-03-18 19:41:09 +0100505 p->irq_parent = irq->start;
Geert Uytterhoevenb22978f2014-03-27 21:47:36 +0100506 if (devm_request_irq(dev, irq->start, gpio_rcar_irq_handler,
507 IRQF_SHARED, name, p)) {
508 dev_err(dev, "failed to request IRQ\n");
Magnus Damm119f5e42013-03-13 20:32:13 +0900509 ret = -ENOENT;
510 goto err1;
511 }
512
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100513 dev_info(dev, "driving %d GPIOs\n", npins);
Laurent Pinchartdc3465a2013-03-10 03:27:00 +0100514
Magnus Damm119f5e42013-03-13 20:32:13 +0900515 return 0;
516
517err1:
Geert Uytterhoeven4d84b9e2015-03-18 19:41:07 +0100518 gpiochip_remove(gpio_chip);
Magnus Damm119f5e42013-03-13 20:32:13 +0900519err0:
Geert Uytterhoevendf0c6c82014-04-14 20:33:13 +0200520 pm_runtime_disable(dev);
Magnus Damm119f5e42013-03-13 20:32:13 +0900521 return ret;
522}
523
524static int gpio_rcar_remove(struct platform_device *pdev)
525{
526 struct gpio_rcar_priv *p = platform_get_drvdata(pdev);
Magnus Damm119f5e42013-03-13 20:32:13 +0900527
abdoulaye berthe9f5132a2014-07-12 22:30:12 +0200528 gpiochip_remove(&p->gpio_chip);
Magnus Damm119f5e42013-03-13 20:32:13 +0900529
Geert Uytterhoevendf0c6c82014-04-14 20:33:13 +0200530 pm_runtime_disable(&pdev->dev);
Magnus Damm119f5e42013-03-13 20:32:13 +0900531 return 0;
532}
533
534static struct platform_driver gpio_rcar_device_driver = {
535 .probe = gpio_rcar_probe,
536 .remove = gpio_rcar_remove,
537 .driver = {
538 .name = "gpio_rcar",
Laurent Pinchart159f8a02013-05-21 13:40:06 +0200539 .of_match_table = of_match_ptr(gpio_rcar_of_table),
Magnus Damm119f5e42013-03-13 20:32:13 +0900540 }
541};
542
543module_platform_driver(gpio_rcar_device_driver);
544
545MODULE_AUTHOR("Magnus Damm");
546MODULE_DESCRIPTION("Renesas R-Car GPIO Driver");
547MODULE_LICENSE("GPL v2");