blob: 7fd09efd708f0549ac0206a1ceb04e8090977c1b [file] [log] [blame]
Magnus Damm119f5e42013-03-13 20:32:13 +09001/*
2 * Renesas R-Car GPIO Support
3 *
4 * Copyright (C) 2013 Magnus Damm
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16#include <linux/err.h>
17#include <linux/gpio.h>
18#include <linux/init.h>
19#include <linux/interrupt.h>
20#include <linux/io.h>
21#include <linux/ioport.h>
22#include <linux/irq.h>
23#include <linux/irqdomain.h>
24#include <linux/module.h>
Laurent Pinchartdc3465a2013-03-10 03:27:00 +010025#include <linux/pinctrl/consumer.h>
Magnus Damm119f5e42013-03-13 20:32:13 +090026#include <linux/platform_data/gpio-rcar.h>
27#include <linux/platform_device.h>
28#include <linux/spinlock.h>
29#include <linux/slab.h>
30
31struct gpio_rcar_priv {
32 void __iomem *base;
33 spinlock_t lock;
34 struct gpio_rcar_config config;
35 struct platform_device *pdev;
36 struct gpio_chip gpio_chip;
37 struct irq_chip irq_chip;
38 struct irq_domain *irq_domain;
39};
40
41#define IOINTSEL 0x00
42#define INOUTSEL 0x04
43#define OUTDT 0x08
44#define INDT 0x0c
45#define INTDT 0x10
46#define INTCLR 0x14
47#define INTMSK 0x18
48#define MSKCLR 0x1c
49#define POSNEG 0x20
50#define EDGLEVEL 0x24
51#define FILONOFF 0x28
Simon Horman7e1092b2013-05-24 18:47:24 +090052#define BOTHEDGE 0x4c
Magnus Damm119f5e42013-03-13 20:32:13 +090053
Laurent Pinchart159f8a02013-05-21 13:40:06 +020054#define RCAR_MAX_GPIO_PER_BANK 32
55
Magnus Damm119f5e42013-03-13 20:32:13 +090056static inline u32 gpio_rcar_read(struct gpio_rcar_priv *p, int offs)
57{
58 return ioread32(p->base + offs);
59}
60
61static inline void gpio_rcar_write(struct gpio_rcar_priv *p, int offs,
62 u32 value)
63{
64 iowrite32(value, p->base + offs);
65}
66
67static void gpio_rcar_modify_bit(struct gpio_rcar_priv *p, int offs,
68 int bit, bool value)
69{
70 u32 tmp = gpio_rcar_read(p, offs);
71
72 if (value)
73 tmp |= BIT(bit);
74 else
75 tmp &= ~BIT(bit);
76
77 gpio_rcar_write(p, offs, tmp);
78}
79
80static void gpio_rcar_irq_disable(struct irq_data *d)
81{
82 struct gpio_rcar_priv *p = irq_data_get_irq_chip_data(d);
83
84 gpio_rcar_write(p, INTMSK, ~BIT(irqd_to_hwirq(d)));
85}
86
87static void gpio_rcar_irq_enable(struct irq_data *d)
88{
89 struct gpio_rcar_priv *p = irq_data_get_irq_chip_data(d);
90
91 gpio_rcar_write(p, MSKCLR, BIT(irqd_to_hwirq(d)));
92}
93
94static void gpio_rcar_config_interrupt_input_mode(struct gpio_rcar_priv *p,
95 unsigned int hwirq,
96 bool active_high_rising_edge,
Simon Horman7e1092b2013-05-24 18:47:24 +090097 bool level_trigger,
98 bool both)
Magnus Damm119f5e42013-03-13 20:32:13 +090099{
100 unsigned long flags;
101
102 /* follow steps in the GPIO documentation for
103 * "Setting Edge-Sensitive Interrupt Input Mode" and
104 * "Setting Level-Sensitive Interrupt Input Mode"
105 */
106
107 spin_lock_irqsave(&p->lock, flags);
108
109 /* Configure postive or negative logic in POSNEG */
110 gpio_rcar_modify_bit(p, POSNEG, hwirq, !active_high_rising_edge);
111
112 /* Configure edge or level trigger in EDGLEVEL */
113 gpio_rcar_modify_bit(p, EDGLEVEL, hwirq, !level_trigger);
114
Simon Horman7e1092b2013-05-24 18:47:24 +0900115 /* Select one edge or both edges in BOTHEDGE */
116 if (p->config.has_both_edge_trigger)
117 gpio_rcar_modify_bit(p, BOTHEDGE, hwirq, both);
118
Magnus Damm119f5e42013-03-13 20:32:13 +0900119 /* Select "Interrupt Input Mode" in IOINTSEL */
120 gpio_rcar_modify_bit(p, IOINTSEL, hwirq, true);
121
122 /* Write INTCLR in case of edge trigger */
123 if (!level_trigger)
124 gpio_rcar_write(p, INTCLR, BIT(hwirq));
125
126 spin_unlock_irqrestore(&p->lock, flags);
127}
128
129static int gpio_rcar_irq_set_type(struct irq_data *d, unsigned int type)
130{
131 struct gpio_rcar_priv *p = irq_data_get_irq_chip_data(d);
132 unsigned int hwirq = irqd_to_hwirq(d);
133
134 dev_dbg(&p->pdev->dev, "sense irq = %d, type = %d\n", hwirq, type);
135
136 switch (type & IRQ_TYPE_SENSE_MASK) {
137 case IRQ_TYPE_LEVEL_HIGH:
Simon Horman7e1092b2013-05-24 18:47:24 +0900138 gpio_rcar_config_interrupt_input_mode(p, hwirq, true, true,
139 false);
Magnus Damm119f5e42013-03-13 20:32:13 +0900140 break;
141 case IRQ_TYPE_LEVEL_LOW:
Simon Horman7e1092b2013-05-24 18:47:24 +0900142 gpio_rcar_config_interrupt_input_mode(p, hwirq, false, true,
143 false);
Magnus Damm119f5e42013-03-13 20:32:13 +0900144 break;
145 case IRQ_TYPE_EDGE_RISING:
Simon Horman7e1092b2013-05-24 18:47:24 +0900146 gpio_rcar_config_interrupt_input_mode(p, hwirq, true, false,
147 false);
Magnus Damm119f5e42013-03-13 20:32:13 +0900148 break;
149 case IRQ_TYPE_EDGE_FALLING:
Simon Horman7e1092b2013-05-24 18:47:24 +0900150 gpio_rcar_config_interrupt_input_mode(p, hwirq, false, false,
151 false);
152 break;
153 case IRQ_TYPE_EDGE_BOTH:
154 if (!p->config.has_both_edge_trigger)
155 return -EINVAL;
156 gpio_rcar_config_interrupt_input_mode(p, hwirq, true, false,
157 true);
Magnus Damm119f5e42013-03-13 20:32:13 +0900158 break;
159 default:
160 return -EINVAL;
161 }
162 return 0;
163}
164
165static irqreturn_t gpio_rcar_irq_handler(int irq, void *dev_id)
166{
167 struct gpio_rcar_priv *p = dev_id;
168 u32 pending;
169 unsigned int offset, irqs_handled = 0;
170
171 while ((pending = gpio_rcar_read(p, INTDT))) {
172 offset = __ffs(pending);
173 gpio_rcar_write(p, INTCLR, BIT(offset));
174 generic_handle_irq(irq_find_mapping(p->irq_domain, offset));
175 irqs_handled++;
176 }
177
178 return irqs_handled ? IRQ_HANDLED : IRQ_NONE;
179}
180
181static inline struct gpio_rcar_priv *gpio_to_priv(struct gpio_chip *chip)
182{
183 return container_of(chip, struct gpio_rcar_priv, gpio_chip);
184}
185
186static void gpio_rcar_config_general_input_output_mode(struct gpio_chip *chip,
187 unsigned int gpio,
188 bool output)
189{
190 struct gpio_rcar_priv *p = gpio_to_priv(chip);
191 unsigned long flags;
192
193 /* follow steps in the GPIO documentation for
194 * "Setting General Output Mode" and
195 * "Setting General Input Mode"
196 */
197
198 spin_lock_irqsave(&p->lock, flags);
199
200 /* Configure postive logic in POSNEG */
201 gpio_rcar_modify_bit(p, POSNEG, gpio, false);
202
203 /* Select "General Input/Output Mode" in IOINTSEL */
204 gpio_rcar_modify_bit(p, IOINTSEL, gpio, false);
205
206 /* Select Input Mode or Output Mode in INOUTSEL */
207 gpio_rcar_modify_bit(p, INOUTSEL, gpio, output);
208
209 spin_unlock_irqrestore(&p->lock, flags);
210}
211
Laurent Pinchartdc3465a2013-03-10 03:27:00 +0100212static int gpio_rcar_request(struct gpio_chip *chip, unsigned offset)
213{
214 return pinctrl_request_gpio(chip->base + offset);
215}
216
217static void gpio_rcar_free(struct gpio_chip *chip, unsigned offset)
218{
219 pinctrl_free_gpio(chip->base + offset);
220
221 /* Set the GPIO as an input to ensure that the next GPIO request won't
222 * drive the GPIO pin as an output.
223 */
224 gpio_rcar_config_general_input_output_mode(chip, offset, false);
225}
226
Magnus Damm119f5e42013-03-13 20:32:13 +0900227static int gpio_rcar_direction_input(struct gpio_chip *chip, unsigned offset)
228{
229 gpio_rcar_config_general_input_output_mode(chip, offset, false);
230 return 0;
231}
232
233static int gpio_rcar_get(struct gpio_chip *chip, unsigned offset)
234{
235 return (int)(gpio_rcar_read(gpio_to_priv(chip), INDT) & BIT(offset));
236}
237
238static void gpio_rcar_set(struct gpio_chip *chip, unsigned offset, int value)
239{
240 struct gpio_rcar_priv *p = gpio_to_priv(chip);
241 unsigned long flags;
242
243 spin_lock_irqsave(&p->lock, flags);
244 gpio_rcar_modify_bit(p, OUTDT, offset, value);
245 spin_unlock_irqrestore(&p->lock, flags);
246}
247
248static int gpio_rcar_direction_output(struct gpio_chip *chip, unsigned offset,
249 int value)
250{
251 /* write GPIO value to output before selecting output mode of pin */
252 gpio_rcar_set(chip, offset, value);
253 gpio_rcar_config_general_input_output_mode(chip, offset, true);
254 return 0;
255}
256
257static int gpio_rcar_to_irq(struct gpio_chip *chip, unsigned offset)
258{
259 return irq_create_mapping(gpio_to_priv(chip)->irq_domain, offset);
260}
261
262static int gpio_rcar_irq_domain_map(struct irq_domain *h, unsigned int virq,
263 irq_hw_number_t hw)
264{
265 struct gpio_rcar_priv *p = h->host_data;
266
267 dev_dbg(&p->pdev->dev, "map hw irq = %d, virq = %d\n", (int)hw, virq);
268
269 irq_set_chip_data(virq, h->host_data);
270 irq_set_chip_and_handler(virq, &p->irq_chip, handle_level_irq);
271 set_irq_flags(virq, IRQF_VALID); /* kill me now */
272 return 0;
273}
274
275static struct irq_domain_ops gpio_rcar_irq_domain_ops = {
276 .map = gpio_rcar_irq_domain_map,
277};
278
Laurent Pinchart159f8a02013-05-21 13:40:06 +0200279static void gpio_rcar_parse_pdata(struct gpio_rcar_priv *p)
280{
281 struct gpio_rcar_config *pdata = p->pdev->dev.platform_data;
Laurent Pinchart159f8a02013-05-21 13:40:06 +0200282 struct device_node *np = p->pdev->dev.of_node;
283 struct of_phandle_args args;
284 int ret;
Laurent Pinchart159f8a02013-05-21 13:40:06 +0200285
Laurent Pincharte3050622013-06-18 12:29:49 +0200286 if (pdata) {
Laurent Pinchart159f8a02013-05-21 13:40:06 +0200287 p->config = *pdata;
Laurent Pincharte3050622013-06-18 12:29:49 +0200288 } else if (IS_ENABLED(CONFIG_OF) && np) {
Laurent Pinchart159f8a02013-05-21 13:40:06 +0200289 ret = of_parse_phandle_with_args(np, "gpio-ranges",
290 "#gpio-range-cells", 0, &args);
291 p->config.number_of_pins = ret == 0 && args.args_count == 3
292 ? args.args[2]
293 : RCAR_MAX_GPIO_PER_BANK;
294 p->config.gpio_base = -1;
295 }
Laurent Pinchart159f8a02013-05-21 13:40:06 +0200296
297 if (p->config.number_of_pins == 0 ||
298 p->config.number_of_pins > RCAR_MAX_GPIO_PER_BANK) {
299 dev_warn(&p->pdev->dev,
300 "Invalid number of gpio lines %u, using %u\n",
301 p->config.number_of_pins, RCAR_MAX_GPIO_PER_BANK);
302 p->config.number_of_pins = RCAR_MAX_GPIO_PER_BANK;
303 }
304}
305
Magnus Damm119f5e42013-03-13 20:32:13 +0900306static int gpio_rcar_probe(struct platform_device *pdev)
307{
Magnus Damm119f5e42013-03-13 20:32:13 +0900308 struct gpio_rcar_priv *p;
309 struct resource *io, *irq;
310 struct gpio_chip *gpio_chip;
311 struct irq_chip *irq_chip;
312 const char *name = dev_name(&pdev->dev);
313 int ret;
314
315 p = devm_kzalloc(&pdev->dev, sizeof(*p), GFP_KERNEL);
316 if (!p) {
317 dev_err(&pdev->dev, "failed to allocate driver data\n");
318 ret = -ENOMEM;
319 goto err0;
320 }
321
Magnus Damm119f5e42013-03-13 20:32:13 +0900322 p->pdev = pdev;
Magnus Damm119f5e42013-03-13 20:32:13 +0900323 spin_lock_init(&p->lock);
324
Laurent Pinchart159f8a02013-05-21 13:40:06 +0200325 /* Get device configuration from DT node or platform data. */
326 gpio_rcar_parse_pdata(p);
327
328 platform_set_drvdata(pdev, p);
329
Magnus Damm119f5e42013-03-13 20:32:13 +0900330 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
331 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
332
333 if (!io || !irq) {
334 dev_err(&pdev->dev, "missing IRQ or IOMEM\n");
335 ret = -EINVAL;
336 goto err0;
337 }
338
339 p->base = devm_ioremap_nocache(&pdev->dev, io->start,
340 resource_size(io));
341 if (!p->base) {
342 dev_err(&pdev->dev, "failed to remap I/O memory\n");
343 ret = -ENXIO;
344 goto err0;
345 }
346
347 gpio_chip = &p->gpio_chip;
Laurent Pinchartdc3465a2013-03-10 03:27:00 +0100348 gpio_chip->request = gpio_rcar_request;
349 gpio_chip->free = gpio_rcar_free;
Magnus Damm119f5e42013-03-13 20:32:13 +0900350 gpio_chip->direction_input = gpio_rcar_direction_input;
351 gpio_chip->get = gpio_rcar_get;
352 gpio_chip->direction_output = gpio_rcar_direction_output;
353 gpio_chip->set = gpio_rcar_set;
354 gpio_chip->to_irq = gpio_rcar_to_irq;
355 gpio_chip->label = name;
Laurent Pinchart159f8a02013-05-21 13:40:06 +0200356 gpio_chip->dev = &pdev->dev;
Magnus Damm119f5e42013-03-13 20:32:13 +0900357 gpio_chip->owner = THIS_MODULE;
358 gpio_chip->base = p->config.gpio_base;
359 gpio_chip->ngpio = p->config.number_of_pins;
360
361 irq_chip = &p->irq_chip;
362 irq_chip->name = name;
363 irq_chip->irq_mask = gpio_rcar_irq_disable;
364 irq_chip->irq_unmask = gpio_rcar_irq_enable;
365 irq_chip->irq_enable = gpio_rcar_irq_enable;
366 irq_chip->irq_disable = gpio_rcar_irq_disable;
367 irq_chip->irq_set_type = gpio_rcar_irq_set_type;
368 irq_chip->flags = IRQCHIP_SKIP_SET_WAKE | IRQCHIP_SET_TYPE_MASKED;
369
370 p->irq_domain = irq_domain_add_simple(pdev->dev.of_node,
371 p->config.number_of_pins,
372 p->config.irq_base,
373 &gpio_rcar_irq_domain_ops, p);
374 if (!p->irq_domain) {
375 ret = -ENXIO;
376 dev_err(&pdev->dev, "cannot initialize irq domain\n");
377 goto err1;
378 }
379
380 if (devm_request_irq(&pdev->dev, irq->start,
Kuninori Morimotoc2349622013-04-17 23:40:57 -0700381 gpio_rcar_irq_handler, IRQF_SHARED, name, p)) {
Magnus Damm119f5e42013-03-13 20:32:13 +0900382 dev_err(&pdev->dev, "failed to request IRQ\n");
383 ret = -ENOENT;
384 goto err1;
385 }
386
387 ret = gpiochip_add(gpio_chip);
388 if (ret) {
389 dev_err(&pdev->dev, "failed to add GPIO controller\n");
390 goto err1;
391 }
392
393 dev_info(&pdev->dev, "driving %d GPIOs\n", p->config.number_of_pins);
394
395 /* warn in case of mismatch if irq base is specified */
396 if (p->config.irq_base) {
397 ret = irq_find_mapping(p->irq_domain, 0);
398 if (p->config.irq_base != ret)
399 dev_warn(&pdev->dev, "irq base mismatch (%u/%u)\n",
400 p->config.irq_base, ret);
401 }
402
Laurent Pinchart159f8a02013-05-21 13:40:06 +0200403 if (p->config.pctl_name) {
404 ret = gpiochip_add_pin_range(gpio_chip, p->config.pctl_name, 0,
405 gpio_chip->base, gpio_chip->ngpio);
406 if (ret < 0)
407 dev_warn(&pdev->dev, "failed to add pin range\n");
408 }
Laurent Pinchartdc3465a2013-03-10 03:27:00 +0100409
Magnus Damm119f5e42013-03-13 20:32:13 +0900410 return 0;
411
412err1:
413 irq_domain_remove(p->irq_domain);
414err0:
415 return ret;
416}
417
418static int gpio_rcar_remove(struct platform_device *pdev)
419{
420 struct gpio_rcar_priv *p = platform_get_drvdata(pdev);
421 int ret;
422
423 ret = gpiochip_remove(&p->gpio_chip);
424 if (ret)
425 return ret;
426
427 irq_domain_remove(p->irq_domain);
428 return 0;
429}
430
Laurent Pinchart159f8a02013-05-21 13:40:06 +0200431#ifdef CONFIG_OF
432static const struct of_device_id gpio_rcar_of_table[] = {
433 {
434 .compatible = "renesas,gpio-rcar",
435 },
436};
437
438MODULE_DEVICE_TABLE(of, gpio_rcar_of_table);
439#endif
440
Magnus Damm119f5e42013-03-13 20:32:13 +0900441static struct platform_driver gpio_rcar_device_driver = {
442 .probe = gpio_rcar_probe,
443 .remove = gpio_rcar_remove,
444 .driver = {
445 .name = "gpio_rcar",
Laurent Pinchart159f8a02013-05-21 13:40:06 +0200446 .of_match_table = of_match_ptr(gpio_rcar_of_table),
Magnus Damm119f5e42013-03-13 20:32:13 +0900447 }
448};
449
450module_platform_driver(gpio_rcar_device_driver);
451
452MODULE_AUTHOR("Magnus Damm");
453MODULE_DESCRIPTION("Renesas R-Car GPIO Driver");
454MODULE_LICENSE("GPL v2");