blob: 1ed52fc026cb4dd25d70efac28a303c2b4bee306 [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>
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 Uytterhoevenab82fa72015-03-18 19:41:09 +010039 struct clk *clk;
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +010040 unsigned int irq_parent;
41 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);
86 struct gpio_rcar_priv *p = container_of(gc, struct gpio_rcar_priv,
87 gpio_chip);
Magnus Damm119f5e42013-03-13 20:32:13 +090088
89 gpio_rcar_write(p, INTMSK, ~BIT(irqd_to_hwirq(d)));
90}
91
92static void gpio_rcar_irq_enable(struct irq_data *d)
93{
Geert Uytterhoevenc7f3c5d2015-01-12 11:07:59 +010094 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
95 struct gpio_rcar_priv *p = container_of(gc, struct gpio_rcar_priv,
96 gpio_chip);
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);
139 struct gpio_rcar_priv *p = container_of(gc, struct gpio_rcar_priv,
140 gpio_chip);
Magnus Damm119f5e42013-03-13 20:32:13 +0900141 unsigned int hwirq = irqd_to_hwirq(d);
142
143 dev_dbg(&p->pdev->dev, "sense irq = %d, type = %d\n", hwirq, type);
144
145 switch (type & IRQ_TYPE_SENSE_MASK) {
146 case IRQ_TYPE_LEVEL_HIGH:
Simon Horman7e1092b2013-05-24 18:47:24 +0900147 gpio_rcar_config_interrupt_input_mode(p, hwirq, true, true,
148 false);
Magnus Damm119f5e42013-03-13 20:32:13 +0900149 break;
150 case IRQ_TYPE_LEVEL_LOW:
Simon Horman7e1092b2013-05-24 18:47:24 +0900151 gpio_rcar_config_interrupt_input_mode(p, hwirq, false, true,
152 false);
Magnus Damm119f5e42013-03-13 20:32:13 +0900153 break;
154 case IRQ_TYPE_EDGE_RISING:
Simon Horman7e1092b2013-05-24 18:47:24 +0900155 gpio_rcar_config_interrupt_input_mode(p, hwirq, true, false,
156 false);
Magnus Damm119f5e42013-03-13 20:32:13 +0900157 break;
158 case IRQ_TYPE_EDGE_FALLING:
Simon Horman7e1092b2013-05-24 18:47:24 +0900159 gpio_rcar_config_interrupt_input_mode(p, hwirq, false, false,
160 false);
161 break;
162 case IRQ_TYPE_EDGE_BOTH:
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100163 if (!p->has_both_edge_trigger)
Simon Horman7e1092b2013-05-24 18:47:24 +0900164 return -EINVAL;
165 gpio_rcar_config_interrupt_input_mode(p, hwirq, true, false,
166 true);
Magnus Damm119f5e42013-03-13 20:32:13 +0900167 break;
168 default:
169 return -EINVAL;
170 }
171 return 0;
172}
173
Geert Uytterhoevenab82fa72015-03-18 19:41:09 +0100174static int gpio_rcar_irq_set_wake(struct irq_data *d, unsigned int on)
175{
176 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
177 struct gpio_rcar_priv *p = container_of(gc, struct gpio_rcar_priv,
178 gpio_chip);
Geert Uytterhoeven501ef0f2015-05-21 13:21:37 +0200179 int error;
Geert Uytterhoevenab82fa72015-03-18 19:41:09 +0100180
Geert Uytterhoeven501ef0f2015-05-21 13:21:37 +0200181 if (p->irq_parent) {
182 error = irq_set_irq_wake(p->irq_parent, on);
183 if (error) {
184 dev_dbg(&p->pdev->dev,
185 "irq %u doesn't support irq_set_wake\n",
186 p->irq_parent);
187 p->irq_parent = 0;
188 }
189 }
Geert Uytterhoevenab82fa72015-03-18 19:41:09 +0100190
191 if (!p->clk)
192 return 0;
193
194 if (on)
195 clk_enable(p->clk);
196 else
197 clk_disable(p->clk);
198
199 return 0;
200}
201
Magnus Damm119f5e42013-03-13 20:32:13 +0900202static irqreturn_t gpio_rcar_irq_handler(int irq, void *dev_id)
203{
204 struct gpio_rcar_priv *p = dev_id;
205 u32 pending;
206 unsigned int offset, irqs_handled = 0;
207
Valentine Barshak8808b642013-11-29 22:04:09 +0400208 while ((pending = gpio_rcar_read(p, INTDT) &
209 gpio_rcar_read(p, INTMSK))) {
Magnus Damm119f5e42013-03-13 20:32:13 +0900210 offset = __ffs(pending);
211 gpio_rcar_write(p, INTCLR, BIT(offset));
Geert Uytterhoevenc7f3c5d2015-01-12 11:07:59 +0100212 generic_handle_irq(irq_find_mapping(p->gpio_chip.irqdomain,
213 offset));
Magnus Damm119f5e42013-03-13 20:32:13 +0900214 irqs_handled++;
215 }
216
217 return irqs_handled ? IRQ_HANDLED : IRQ_NONE;
218}
219
220static inline struct gpio_rcar_priv *gpio_to_priv(struct gpio_chip *chip)
221{
222 return container_of(chip, struct gpio_rcar_priv, gpio_chip);
223}
224
225static void gpio_rcar_config_general_input_output_mode(struct gpio_chip *chip,
226 unsigned int gpio,
227 bool output)
228{
229 struct gpio_rcar_priv *p = gpio_to_priv(chip);
230 unsigned long flags;
231
232 /* follow steps in the GPIO documentation for
233 * "Setting General Output Mode" and
234 * "Setting General Input Mode"
235 */
236
237 spin_lock_irqsave(&p->lock, flags);
238
239 /* Configure postive logic in POSNEG */
240 gpio_rcar_modify_bit(p, POSNEG, gpio, false);
241
242 /* Select "General Input/Output Mode" in IOINTSEL */
243 gpio_rcar_modify_bit(p, IOINTSEL, gpio, false);
244
245 /* Select Input Mode or Output Mode in INOUTSEL */
246 gpio_rcar_modify_bit(p, INOUTSEL, gpio, output);
247
248 spin_unlock_irqrestore(&p->lock, flags);
249}
250
Laurent Pinchartdc3465a2013-03-10 03:27:00 +0100251static int gpio_rcar_request(struct gpio_chip *chip, unsigned offset)
252{
Geert Uytterhoeven65194cb2015-06-25 16:45:57 +0200253 struct gpio_rcar_priv *p = gpio_to_priv(chip);
254 int error;
255
256 error = pm_runtime_get_sync(&p->pdev->dev);
257 if (error < 0)
258 return error;
259
260 error = pinctrl_request_gpio(chip->base + offset);
261 if (error)
262 pm_runtime_put(&p->pdev->dev);
263
264 return error;
Laurent Pinchartdc3465a2013-03-10 03:27:00 +0100265}
266
267static void gpio_rcar_free(struct gpio_chip *chip, unsigned offset)
268{
Geert Uytterhoeven65194cb2015-06-25 16:45:57 +0200269 struct gpio_rcar_priv *p = gpio_to_priv(chip);
270
Laurent Pinchartdc3465a2013-03-10 03:27:00 +0100271 pinctrl_free_gpio(chip->base + offset);
272
273 /* Set the GPIO as an input to ensure that the next GPIO request won't
274 * drive the GPIO pin as an output.
275 */
276 gpio_rcar_config_general_input_output_mode(chip, offset, false);
Geert Uytterhoeven65194cb2015-06-25 16:45:57 +0200277
278 pm_runtime_put(&p->pdev->dev);
Laurent Pinchartdc3465a2013-03-10 03:27:00 +0100279}
280
Magnus Damm119f5e42013-03-13 20:32:13 +0900281static int gpio_rcar_direction_input(struct gpio_chip *chip, unsigned offset)
282{
283 gpio_rcar_config_general_input_output_mode(chip, offset, false);
284 return 0;
285}
286
287static int gpio_rcar_get(struct gpio_chip *chip, unsigned offset)
288{
Magnus Dammae9550f2013-06-17 08:41:52 +0900289 u32 bit = BIT(offset);
290
291 /* testing on r8a7790 shows that INDT does not show correct pin state
292 * when configured as output, so use OUTDT in case of output pins */
293 if (gpio_rcar_read(gpio_to_priv(chip), INOUTSEL) & bit)
Jürg Billeter7cb54092014-06-24 04:19:50 +0200294 return !!(gpio_rcar_read(gpio_to_priv(chip), OUTDT) & bit);
Magnus Dammae9550f2013-06-17 08:41:52 +0900295 else
Jürg Billeter7cb54092014-06-24 04:19:50 +0200296 return !!(gpio_rcar_read(gpio_to_priv(chip), INDT) & bit);
Magnus Damm119f5e42013-03-13 20:32:13 +0900297}
298
299static void gpio_rcar_set(struct gpio_chip *chip, unsigned offset, int value)
300{
301 struct gpio_rcar_priv *p = gpio_to_priv(chip);
302 unsigned long flags;
303
304 spin_lock_irqsave(&p->lock, flags);
305 gpio_rcar_modify_bit(p, OUTDT, offset, value);
306 spin_unlock_irqrestore(&p->lock, flags);
307}
308
309static int gpio_rcar_direction_output(struct gpio_chip *chip, unsigned offset,
310 int value)
311{
312 /* write GPIO value to output before selecting output mode of pin */
313 gpio_rcar_set(chip, offset, value);
314 gpio_rcar_config_general_input_output_mode(chip, offset, true);
315 return 0;
316}
317
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100318struct gpio_rcar_info {
319 bool has_both_edge_trigger;
320};
321
Hisashi Nakamura1fd2b492014-11-07 20:54:08 +0900322static const struct gpio_rcar_info gpio_rcar_info_gen1 = {
323 .has_both_edge_trigger = false,
324};
325
326static const struct gpio_rcar_info gpio_rcar_info_gen2 = {
327 .has_both_edge_trigger = true,
328};
329
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100330static const struct of_device_id gpio_rcar_of_table[] = {
331 {
332 .compatible = "renesas,gpio-r8a7790",
Hisashi Nakamura1fd2b492014-11-07 20:54:08 +0900333 .data = &gpio_rcar_info_gen2,
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100334 }, {
335 .compatible = "renesas,gpio-r8a7791",
Hisashi Nakamura1fd2b492014-11-07 20:54:08 +0900336 .data = &gpio_rcar_info_gen2,
337 }, {
338 .compatible = "renesas,gpio-r8a7793",
339 .data = &gpio_rcar_info_gen2,
340 }, {
341 .compatible = "renesas,gpio-r8a7794",
342 .data = &gpio_rcar_info_gen2,
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100343 }, {
Ulrich Hecht8cd14702015-07-21 11:08:50 +0200344 .compatible = "renesas,gpio-r8a7795",
345 /* Gen3 GPIO is identical to Gen2. */
346 .data = &gpio_rcar_info_gen2,
347 }, {
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100348 .compatible = "renesas,gpio-rcar",
Hisashi Nakamura1fd2b492014-11-07 20:54:08 +0900349 .data = &gpio_rcar_info_gen1,
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100350 }, {
351 /* Terminator */
352 },
353};
354
355MODULE_DEVICE_TABLE(of, gpio_rcar_of_table);
356
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100357static int gpio_rcar_parse_dt(struct gpio_rcar_priv *p, unsigned int *npins)
Laurent Pinchart159f8a02013-05-21 13:40:06 +0200358{
Laurent Pinchart159f8a02013-05-21 13:40:06 +0200359 struct device_node *np = p->pdev->dev.of_node;
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100360 const struct of_device_id *match;
361 const struct gpio_rcar_info *info;
Laurent Pinchart159f8a02013-05-21 13:40:06 +0200362 struct of_phandle_args args;
363 int ret;
Laurent Pinchart159f8a02013-05-21 13:40:06 +0200364
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100365 match = of_match_node(gpio_rcar_of_table, np);
366 if (!match)
367 return -EINVAL;
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100368
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100369 info = match->data;
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100370
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100371 ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3, 0, &args);
372 *npins = ret == 0 ? args.args[2] : RCAR_MAX_GPIO_PER_BANK;
373 p->has_both_edge_trigger = info->has_both_edge_trigger;
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100374
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100375 if (*npins == 0 || *npins > RCAR_MAX_GPIO_PER_BANK) {
Laurent Pinchart159f8a02013-05-21 13:40:06 +0200376 dev_warn(&p->pdev->dev,
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100377 "Invalid number of gpio lines %u, using %u\n", *npins,
378 RCAR_MAX_GPIO_PER_BANK);
379 *npins = RCAR_MAX_GPIO_PER_BANK;
Laurent Pinchart159f8a02013-05-21 13:40:06 +0200380 }
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100381
382 return 0;
Laurent Pinchart159f8a02013-05-21 13:40:06 +0200383}
384
Magnus Damm119f5e42013-03-13 20:32:13 +0900385static int gpio_rcar_probe(struct platform_device *pdev)
386{
Magnus Damm119f5e42013-03-13 20:32:13 +0900387 struct gpio_rcar_priv *p;
388 struct resource *io, *irq;
389 struct gpio_chip *gpio_chip;
390 struct irq_chip *irq_chip;
Geert Uytterhoevenb22978f2014-03-27 21:47:36 +0100391 struct device *dev = &pdev->dev;
392 const char *name = dev_name(dev);
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100393 unsigned int npins;
Magnus Damm119f5e42013-03-13 20:32:13 +0900394 int ret;
395
Geert Uytterhoevenb22978f2014-03-27 21:47:36 +0100396 p = devm_kzalloc(dev, sizeof(*p), GFP_KERNEL);
Geert Uytterhoeven7d82bf32015-01-12 11:07:58 +0100397 if (!p)
398 return -ENOMEM;
Magnus Damm119f5e42013-03-13 20:32:13 +0900399
Magnus Damm119f5e42013-03-13 20:32:13 +0900400 p->pdev = pdev;
Magnus Damm119f5e42013-03-13 20:32:13 +0900401 spin_lock_init(&p->lock);
402
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100403 /* Get device configuration from DT node */
404 ret = gpio_rcar_parse_dt(p, &npins);
Laurent Pinchart850dfe12013-11-29 14:48:00 +0100405 if (ret < 0)
406 return ret;
Laurent Pinchart159f8a02013-05-21 13:40:06 +0200407
408 platform_set_drvdata(pdev, p);
409
Geert Uytterhoevenab82fa72015-03-18 19:41:09 +0100410 p->clk = devm_clk_get(dev, NULL);
411 if (IS_ERR(p->clk)) {
412 dev_warn(dev, "unable to get clock\n");
413 p->clk = NULL;
414 }
415
Geert Uytterhoevendf0c6c82014-04-14 20:33:13 +0200416 pm_runtime_enable(dev);
Geert Uytterhoevendf0c6c82014-04-14 20:33:13 +0200417
Magnus Damm119f5e42013-03-13 20:32:13 +0900418 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
419 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
420
421 if (!io || !irq) {
Geert Uytterhoevenb22978f2014-03-27 21:47:36 +0100422 dev_err(dev, "missing IRQ or IOMEM\n");
Magnus Damm119f5e42013-03-13 20:32:13 +0900423 ret = -EINVAL;
424 goto err0;
425 }
426
Geert Uytterhoevenb22978f2014-03-27 21:47:36 +0100427 p->base = devm_ioremap_nocache(dev, io->start, resource_size(io));
Magnus Damm119f5e42013-03-13 20:32:13 +0900428 if (!p->base) {
Geert Uytterhoevenb22978f2014-03-27 21:47:36 +0100429 dev_err(dev, "failed to remap I/O memory\n");
Magnus Damm119f5e42013-03-13 20:32:13 +0900430 ret = -ENXIO;
431 goto err0;
432 }
433
434 gpio_chip = &p->gpio_chip;
Laurent Pinchartdc3465a2013-03-10 03:27:00 +0100435 gpio_chip->request = gpio_rcar_request;
436 gpio_chip->free = gpio_rcar_free;
Magnus Damm119f5e42013-03-13 20:32:13 +0900437 gpio_chip->direction_input = gpio_rcar_direction_input;
438 gpio_chip->get = gpio_rcar_get;
439 gpio_chip->direction_output = gpio_rcar_direction_output;
440 gpio_chip->set = gpio_rcar_set;
Magnus Damm119f5e42013-03-13 20:32:13 +0900441 gpio_chip->label = name;
Linus Walleij58383c782015-11-04 09:56:26 +0100442 gpio_chip->parent = dev;
Magnus Damm119f5e42013-03-13 20:32:13 +0900443 gpio_chip->owner = THIS_MODULE;
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100444 gpio_chip->base = -1;
445 gpio_chip->ngpio = npins;
Magnus Damm119f5e42013-03-13 20:32:13 +0900446
447 irq_chip = &p->irq_chip;
448 irq_chip->name = name;
449 irq_chip->irq_mask = gpio_rcar_irq_disable;
450 irq_chip->irq_unmask = gpio_rcar_irq_enable;
Magnus Damm119f5e42013-03-13 20:32:13 +0900451 irq_chip->irq_set_type = gpio_rcar_irq_set_type;
Geert Uytterhoevenab82fa72015-03-18 19:41:09 +0100452 irq_chip->irq_set_wake = gpio_rcar_irq_set_wake;
453 irq_chip->flags = IRQCHIP_SET_TYPE_MASKED | IRQCHIP_MASK_ON_SUSPEND;
Magnus Damm119f5e42013-03-13 20:32:13 +0900454
Geert Uytterhoevenc7f3c5d2015-01-12 11:07:59 +0100455 ret = gpiochip_add(gpio_chip);
456 if (ret) {
457 dev_err(dev, "failed to add GPIO controller\n");
Dan Carpenter0c8aab82013-11-07 10:56:51 +0300458 goto err0;
Magnus Damm119f5e42013-03-13 20:32:13 +0900459 }
460
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100461 ret = gpiochip_irqchip_add(gpio_chip, irq_chip, 0, handle_level_irq,
462 IRQ_TYPE_NONE);
Geert Uytterhoevenc7f3c5d2015-01-12 11:07:59 +0100463 if (ret) {
464 dev_err(dev, "cannot add irqchip\n");
465 goto err1;
466 }
467
Geert Uytterhoevenab82fa72015-03-18 19:41:09 +0100468 p->irq_parent = irq->start;
Geert Uytterhoevenb22978f2014-03-27 21:47:36 +0100469 if (devm_request_irq(dev, irq->start, gpio_rcar_irq_handler,
470 IRQF_SHARED, name, p)) {
471 dev_err(dev, "failed to request IRQ\n");
Magnus Damm119f5e42013-03-13 20:32:13 +0900472 ret = -ENOENT;
473 goto err1;
474 }
475
Geert Uytterhoeven8b092be2015-12-04 16:33:52 +0100476 dev_info(dev, "driving %d GPIOs\n", npins);
Laurent Pinchartdc3465a2013-03-10 03:27:00 +0100477
Magnus Damm119f5e42013-03-13 20:32:13 +0900478 return 0;
479
480err1:
Geert Uytterhoeven4d84b9e2015-03-18 19:41:07 +0100481 gpiochip_remove(gpio_chip);
Magnus Damm119f5e42013-03-13 20:32:13 +0900482err0:
Geert Uytterhoevendf0c6c82014-04-14 20:33:13 +0200483 pm_runtime_disable(dev);
Magnus Damm119f5e42013-03-13 20:32:13 +0900484 return ret;
485}
486
487static int gpio_rcar_remove(struct platform_device *pdev)
488{
489 struct gpio_rcar_priv *p = platform_get_drvdata(pdev);
Magnus Damm119f5e42013-03-13 20:32:13 +0900490
abdoulaye berthe9f5132a2014-07-12 22:30:12 +0200491 gpiochip_remove(&p->gpio_chip);
Magnus Damm119f5e42013-03-13 20:32:13 +0900492
Geert Uytterhoevendf0c6c82014-04-14 20:33:13 +0200493 pm_runtime_disable(&pdev->dev);
Magnus Damm119f5e42013-03-13 20:32:13 +0900494 return 0;
495}
496
497static struct platform_driver gpio_rcar_device_driver = {
498 .probe = gpio_rcar_probe,
499 .remove = gpio_rcar_remove,
500 .driver = {
501 .name = "gpio_rcar",
Laurent Pinchart159f8a02013-05-21 13:40:06 +0200502 .of_match_table = of_match_ptr(gpio_rcar_of_table),
Magnus Damm119f5e42013-03-13 20:32:13 +0900503 }
504};
505
506module_platform_driver(gpio_rcar_device_driver);
507
508MODULE_AUTHOR("Magnus Damm");
509MODULE_DESCRIPTION("Renesas R-Car GPIO Driver");
510MODULE_LICENSE("GPL v2");