1 // SPDX-License-Identifier: GPL-2.0
3 * Renesas R-Car GPIO Support
5 * Copyright (C) 2014 Renesas Electronics Corporation
6 * Copyright (C) 2013 Magnus Damm
10 #include <linux/gpio/driver.h>
11 #include <linux/init.h>
12 #include <linux/interrupt.h>
14 #include <linux/ioport.h>
15 #include <linux/irq.h>
16 #include <linux/module.h>
18 #include <linux/of_device.h>
19 #include <linux/pinctrl/consumer.h>
20 #include <linux/platform_device.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/spinlock.h>
23 #include <linux/slab.h>
25 struct gpio_rcar_bank_info {
35 struct gpio_rcar_info {
37 bool has_both_edge_trigger;
42 struct gpio_rcar_priv {
46 struct gpio_chip gpio_chip;
47 struct irq_chip irq_chip;
48 unsigned int irq_parent;
50 struct gpio_rcar_info info;
51 struct gpio_rcar_bank_info bank_info;
54 #define IOINTSEL 0x00 /* General IO/Interrupt Switching Register */
55 #define INOUTSEL 0x04 /* General Input/Output Switching Register */
56 #define OUTDT 0x08 /* General Output Register */
57 #define INDT 0x0c /* General Input Register */
58 #define INTDT 0x10 /* Interrupt Display Register */
59 #define INTCLR 0x14 /* Interrupt Clear Register */
60 #define INTMSK 0x18 /* Interrupt Mask Register */
61 #define MSKCLR 0x1c /* Interrupt Mask Clear Register */
62 #define POSNEG 0x20 /* Positive/Negative Logic Select Register */
63 #define EDGLEVEL 0x24 /* Edge/level Select Register */
64 #define FILONOFF 0x28 /* Chattering Prevention On/Off Register */
65 #define OUTDTSEL 0x40 /* Output Data Select Register */
66 #define BOTHEDGE 0x4c /* One Edge/Both Edge Select Register */
67 #define INEN 0x50 /* General Input Enable Register */
69 #define RCAR_MAX_GPIO_PER_BANK 32
71 static inline u32 gpio_rcar_read(struct gpio_rcar_priv *p, int offs)
73 return ioread32(p->base + offs);
76 static inline void gpio_rcar_write(struct gpio_rcar_priv *p, int offs,
79 iowrite32(value, p->base + offs);
82 static void gpio_rcar_modify_bit(struct gpio_rcar_priv *p, int offs,
85 u32 tmp = gpio_rcar_read(p, offs);
92 gpio_rcar_write(p, offs, tmp);
95 static void gpio_rcar_irq_disable(struct irq_data *d)
97 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
98 struct gpio_rcar_priv *p = gpiochip_get_data(gc);
100 gpio_rcar_write(p, INTMSK, ~BIT(irqd_to_hwirq(d)));
103 static void gpio_rcar_irq_enable(struct irq_data *d)
105 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
106 struct gpio_rcar_priv *p = gpiochip_get_data(gc);
108 gpio_rcar_write(p, MSKCLR, BIT(irqd_to_hwirq(d)));
111 static void gpio_rcar_config_interrupt_input_mode(struct gpio_rcar_priv *p,
113 bool active_high_rising_edge,
119 /* follow steps in the GPIO documentation for
120 * "Setting Edge-Sensitive Interrupt Input Mode" and
121 * "Setting Level-Sensitive Interrupt Input Mode"
124 spin_lock_irqsave(&p->lock, flags);
126 /* Configure positive or negative logic in POSNEG */
127 gpio_rcar_modify_bit(p, POSNEG, hwirq, !active_high_rising_edge);
129 /* Configure edge or level trigger in EDGLEVEL */
130 gpio_rcar_modify_bit(p, EDGLEVEL, hwirq, !level_trigger);
132 /* Select one edge or both edges in BOTHEDGE */
133 if (p->info.has_both_edge_trigger)
134 gpio_rcar_modify_bit(p, BOTHEDGE, hwirq, both);
136 /* Select "Interrupt Input Mode" in IOINTSEL */
137 gpio_rcar_modify_bit(p, IOINTSEL, hwirq, true);
139 /* Write INTCLR in case of edge trigger */
141 gpio_rcar_write(p, INTCLR, BIT(hwirq));
143 spin_unlock_irqrestore(&p->lock, flags);
146 static int gpio_rcar_irq_set_type(struct irq_data *d, unsigned int type)
148 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
149 struct gpio_rcar_priv *p = gpiochip_get_data(gc);
150 unsigned int hwirq = irqd_to_hwirq(d);
152 dev_dbg(p->dev, "sense irq = %d, type = %d\n", hwirq, type);
154 switch (type & IRQ_TYPE_SENSE_MASK) {
155 case IRQ_TYPE_LEVEL_HIGH:
156 gpio_rcar_config_interrupt_input_mode(p, hwirq, true, true,
159 case IRQ_TYPE_LEVEL_LOW:
160 gpio_rcar_config_interrupt_input_mode(p, hwirq, false, true,
163 case IRQ_TYPE_EDGE_RISING:
164 gpio_rcar_config_interrupt_input_mode(p, hwirq, true, false,
167 case IRQ_TYPE_EDGE_FALLING:
168 gpio_rcar_config_interrupt_input_mode(p, hwirq, false, false,
171 case IRQ_TYPE_EDGE_BOTH:
172 if (!p->info.has_both_edge_trigger)
174 gpio_rcar_config_interrupt_input_mode(p, hwirq, true, false,
183 static int gpio_rcar_irq_set_wake(struct irq_data *d, unsigned int on)
185 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
186 struct gpio_rcar_priv *p = gpiochip_get_data(gc);
190 error = irq_set_irq_wake(p->irq_parent, on);
192 dev_dbg(p->dev, "irq %u doesn't support irq_set_wake\n",
199 atomic_inc(&p->wakeup_path);
201 atomic_dec(&p->wakeup_path);
206 static irqreturn_t gpio_rcar_irq_handler(int irq, void *dev_id)
208 struct gpio_rcar_priv *p = dev_id;
210 unsigned int offset, irqs_handled = 0;
212 while ((pending = gpio_rcar_read(p, INTDT) &
213 gpio_rcar_read(p, INTMSK))) {
214 offset = __ffs(pending);
215 gpio_rcar_write(p, INTCLR, BIT(offset));
216 generic_handle_domain_irq(p->gpio_chip.irq.domain,
221 return irqs_handled ? IRQ_HANDLED : IRQ_NONE;
224 static void gpio_rcar_config_general_input_output_mode(struct gpio_chip *chip,
228 struct gpio_rcar_priv *p = gpiochip_get_data(chip);
231 /* follow steps in the GPIO documentation for
232 * "Setting General Output Mode" and
233 * "Setting General Input Mode"
236 spin_lock_irqsave(&p->lock, flags);
238 /* Configure positive logic in POSNEG */
239 gpio_rcar_modify_bit(p, POSNEG, gpio, false);
241 /* Select "General Input/Output Mode" in IOINTSEL */
242 gpio_rcar_modify_bit(p, IOINTSEL, gpio, false);
244 /* Select Input Mode or Output Mode in INOUTSEL */
245 gpio_rcar_modify_bit(p, INOUTSEL, gpio, output);
247 /* Select General Output Register to output data in OUTDTSEL */
248 if (p->info.has_outdtsel && output)
249 gpio_rcar_modify_bit(p, OUTDTSEL, gpio, false);
251 spin_unlock_irqrestore(&p->lock, flags);
254 static int gpio_rcar_request(struct gpio_chip *chip, unsigned offset)
256 struct gpio_rcar_priv *p = gpiochip_get_data(chip);
259 error = pm_runtime_get_sync(p->dev);
261 pm_runtime_put(p->dev);
265 error = pinctrl_gpio_request(chip->base + offset);
267 pm_runtime_put(p->dev);
272 static void gpio_rcar_free(struct gpio_chip *chip, unsigned offset)
274 struct gpio_rcar_priv *p = gpiochip_get_data(chip);
276 pinctrl_gpio_free(chip->base + offset);
279 * Set the GPIO as an input to ensure that the next GPIO request won't
280 * drive the GPIO pin as an output.
282 gpio_rcar_config_general_input_output_mode(chip, offset, false);
284 pm_runtime_put(p->dev);
287 static int gpio_rcar_get_direction(struct gpio_chip *chip, unsigned int offset)
289 struct gpio_rcar_priv *p = gpiochip_get_data(chip);
291 if (gpio_rcar_read(p, INOUTSEL) & BIT(offset))
292 return GPIO_LINE_DIRECTION_OUT;
294 return GPIO_LINE_DIRECTION_IN;
297 static int gpio_rcar_direction_input(struct gpio_chip *chip, unsigned offset)
299 gpio_rcar_config_general_input_output_mode(chip, offset, false);
303 static int gpio_rcar_get(struct gpio_chip *chip, unsigned offset)
305 struct gpio_rcar_priv *p = gpiochip_get_data(chip);
306 u32 bit = BIT(offset);
309 * Before R-Car Gen3, INDT does not show correct pin state when
310 * configured as output, so use OUTDT in case of output pins
312 if (!p->info.has_always_in && (gpio_rcar_read(p, INOUTSEL) & bit))
313 return !!(gpio_rcar_read(p, OUTDT) & bit);
315 return !!(gpio_rcar_read(p, INDT) & bit);
318 static int gpio_rcar_get_multiple(struct gpio_chip *chip, unsigned long *mask,
321 struct gpio_rcar_priv *p = gpiochip_get_data(chip);
322 u32 bankmask, outputs, m, val = 0;
325 bankmask = mask[0] & GENMASK(chip->ngpio - 1, 0);
326 if (chip->valid_mask)
327 bankmask &= chip->valid_mask[0];
332 if (p->info.has_always_in) {
333 bits[0] = gpio_rcar_read(p, INDT) & bankmask;
337 spin_lock_irqsave(&p->lock, flags);
338 outputs = gpio_rcar_read(p, INOUTSEL);
339 m = outputs & bankmask;
341 val |= gpio_rcar_read(p, OUTDT) & m;
343 m = ~outputs & bankmask;
345 val |= gpio_rcar_read(p, INDT) & m;
346 spin_unlock_irqrestore(&p->lock, flags);
352 static void gpio_rcar_set(struct gpio_chip *chip, unsigned offset, int value)
354 struct gpio_rcar_priv *p = gpiochip_get_data(chip);
357 spin_lock_irqsave(&p->lock, flags);
358 gpio_rcar_modify_bit(p, OUTDT, offset, value);
359 spin_unlock_irqrestore(&p->lock, flags);
362 static void gpio_rcar_set_multiple(struct gpio_chip *chip, unsigned long *mask,
365 struct gpio_rcar_priv *p = gpiochip_get_data(chip);
369 bankmask = mask[0] & GENMASK(chip->ngpio - 1, 0);
370 if (chip->valid_mask)
371 bankmask &= chip->valid_mask[0];
376 spin_lock_irqsave(&p->lock, flags);
377 val = gpio_rcar_read(p, OUTDT);
379 val |= (bankmask & bits[0]);
380 gpio_rcar_write(p, OUTDT, val);
381 spin_unlock_irqrestore(&p->lock, flags);
384 static int gpio_rcar_direction_output(struct gpio_chip *chip, unsigned offset,
387 /* write GPIO value to output before selecting output mode of pin */
388 gpio_rcar_set(chip, offset, value);
389 gpio_rcar_config_general_input_output_mode(chip, offset, true);
393 static const struct gpio_rcar_info gpio_rcar_info_gen1 = {
394 .has_outdtsel = false,
395 .has_both_edge_trigger = false,
396 .has_always_in = false,
400 static const struct gpio_rcar_info gpio_rcar_info_gen2 = {
401 .has_outdtsel = true,
402 .has_both_edge_trigger = true,
403 .has_always_in = false,
407 static const struct gpio_rcar_info gpio_rcar_info_gen3 = {
408 .has_outdtsel = true,
409 .has_both_edge_trigger = true,
410 .has_always_in = true,
414 static const struct gpio_rcar_info gpio_rcar_info_v3u = {
415 .has_outdtsel = true,
416 .has_both_edge_trigger = true,
417 .has_always_in = true,
421 static const struct of_device_id gpio_rcar_of_table[] = {
423 .compatible = "renesas,gpio-r8a779a0",
424 .data = &gpio_rcar_info_v3u,
426 .compatible = "renesas,rcar-gen1-gpio",
427 .data = &gpio_rcar_info_gen1,
429 .compatible = "renesas,rcar-gen2-gpio",
430 .data = &gpio_rcar_info_gen2,
432 .compatible = "renesas,rcar-gen3-gpio",
433 .data = &gpio_rcar_info_gen3,
435 .compatible = "renesas,gpio-rcar",
436 .data = &gpio_rcar_info_gen1,
442 MODULE_DEVICE_TABLE(of, gpio_rcar_of_table);
444 static int gpio_rcar_parse_dt(struct gpio_rcar_priv *p, unsigned int *npins)
446 struct device_node *np = p->dev->of_node;
447 const struct gpio_rcar_info *info;
448 struct of_phandle_args args;
451 info = of_device_get_match_data(p->dev);
454 ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3, 0, &args);
455 *npins = ret == 0 ? args.args[2] : RCAR_MAX_GPIO_PER_BANK;
457 if (*npins == 0 || *npins > RCAR_MAX_GPIO_PER_BANK) {
458 dev_warn(p->dev, "Invalid number of gpio lines %u, using %u\n",
459 *npins, RCAR_MAX_GPIO_PER_BANK);
460 *npins = RCAR_MAX_GPIO_PER_BANK;
466 static void gpio_rcar_enable_inputs(struct gpio_rcar_priv *p)
468 u32 mask = GENMASK(p->gpio_chip.ngpio - 1, 0);
470 /* Select "Input Enable" in INEN */
471 if (p->gpio_chip.valid_mask)
472 mask &= p->gpio_chip.valid_mask[0];
474 gpio_rcar_write(p, INEN, gpio_rcar_read(p, INEN) | mask);
477 static int gpio_rcar_probe(struct platform_device *pdev)
479 struct gpio_rcar_priv *p;
480 struct resource *irq;
481 struct gpio_chip *gpio_chip;
482 struct irq_chip *irq_chip;
483 struct gpio_irq_chip *girq;
484 struct device *dev = &pdev->dev;
485 const char *name = dev_name(dev);
489 p = devm_kzalloc(dev, sizeof(*p), GFP_KERNEL);
494 spin_lock_init(&p->lock);
496 /* Get device configuration from DT node */
497 ret = gpio_rcar_parse_dt(p, &npins);
501 platform_set_drvdata(pdev, p);
503 pm_runtime_enable(dev);
505 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
507 dev_err(dev, "missing IRQ\n");
512 p->base = devm_platform_ioremap_resource(pdev, 0);
513 if (IS_ERR(p->base)) {
514 ret = PTR_ERR(p->base);
518 gpio_chip = &p->gpio_chip;
519 gpio_chip->request = gpio_rcar_request;
520 gpio_chip->free = gpio_rcar_free;
521 gpio_chip->get_direction = gpio_rcar_get_direction;
522 gpio_chip->direction_input = gpio_rcar_direction_input;
523 gpio_chip->get = gpio_rcar_get;
524 gpio_chip->get_multiple = gpio_rcar_get_multiple;
525 gpio_chip->direction_output = gpio_rcar_direction_output;
526 gpio_chip->set = gpio_rcar_set;
527 gpio_chip->set_multiple = gpio_rcar_set_multiple;
528 gpio_chip->label = name;
529 gpio_chip->parent = dev;
530 gpio_chip->owner = THIS_MODULE;
531 gpio_chip->base = -1;
532 gpio_chip->ngpio = npins;
534 irq_chip = &p->irq_chip;
535 irq_chip->name = "gpio-rcar";
536 irq_chip->parent_device = dev;
537 irq_chip->irq_mask = gpio_rcar_irq_disable;
538 irq_chip->irq_unmask = gpio_rcar_irq_enable;
539 irq_chip->irq_set_type = gpio_rcar_irq_set_type;
540 irq_chip->irq_set_wake = gpio_rcar_irq_set_wake;
541 irq_chip->flags = IRQCHIP_SET_TYPE_MASKED | IRQCHIP_MASK_ON_SUSPEND;
543 girq = &gpio_chip->irq;
544 girq->chip = irq_chip;
545 /* This will let us handle the parent IRQ in the driver */
546 girq->parent_handler = NULL;
547 girq->num_parents = 0;
548 girq->parents = NULL;
549 girq->default_type = IRQ_TYPE_NONE;
550 girq->handler = handle_level_irq;
552 ret = gpiochip_add_data(gpio_chip, p);
554 dev_err(dev, "failed to add GPIO controller\n");
558 p->irq_parent = irq->start;
559 if (devm_request_irq(dev, irq->start, gpio_rcar_irq_handler,
560 IRQF_SHARED, name, p)) {
561 dev_err(dev, "failed to request IRQ\n");
566 if (p->info.has_inen) {
567 pm_runtime_get_sync(dev);
568 gpio_rcar_enable_inputs(p);
572 dev_info(dev, "driving %d GPIOs\n", npins);
577 gpiochip_remove(gpio_chip);
579 pm_runtime_disable(dev);
583 static int gpio_rcar_remove(struct platform_device *pdev)
585 struct gpio_rcar_priv *p = platform_get_drvdata(pdev);
587 gpiochip_remove(&p->gpio_chip);
589 pm_runtime_disable(&pdev->dev);
593 #ifdef CONFIG_PM_SLEEP
594 static int gpio_rcar_suspend(struct device *dev)
596 struct gpio_rcar_priv *p = dev_get_drvdata(dev);
598 p->bank_info.iointsel = gpio_rcar_read(p, IOINTSEL);
599 p->bank_info.inoutsel = gpio_rcar_read(p, INOUTSEL);
600 p->bank_info.outdt = gpio_rcar_read(p, OUTDT);
601 p->bank_info.intmsk = gpio_rcar_read(p, INTMSK);
602 p->bank_info.posneg = gpio_rcar_read(p, POSNEG);
603 p->bank_info.edglevel = gpio_rcar_read(p, EDGLEVEL);
604 if (p->info.has_both_edge_trigger)
605 p->bank_info.bothedge = gpio_rcar_read(p, BOTHEDGE);
607 if (atomic_read(&p->wakeup_path))
608 device_set_wakeup_path(dev);
613 static int gpio_rcar_resume(struct device *dev)
615 struct gpio_rcar_priv *p = dev_get_drvdata(dev);
619 for (offset = 0; offset < p->gpio_chip.ngpio; offset++) {
620 if (!gpiochip_line_is_valid(&p->gpio_chip, offset))
625 if (!(p->bank_info.iointsel & mask)) {
626 if (p->bank_info.inoutsel & mask)
627 gpio_rcar_direction_output(
628 &p->gpio_chip, offset,
629 !!(p->bank_info.outdt & mask));
631 gpio_rcar_direction_input(&p->gpio_chip,
635 gpio_rcar_config_interrupt_input_mode(
638 !(p->bank_info.posneg & mask),
639 !(p->bank_info.edglevel & mask),
640 !!(p->bank_info.bothedge & mask));
642 if (p->bank_info.intmsk & mask)
643 gpio_rcar_write(p, MSKCLR, mask);
647 if (p->info.has_inen)
648 gpio_rcar_enable_inputs(p);
652 #endif /* CONFIG_PM_SLEEP*/
654 static SIMPLE_DEV_PM_OPS(gpio_rcar_pm_ops, gpio_rcar_suspend, gpio_rcar_resume);
656 static struct platform_driver gpio_rcar_device_driver = {
657 .probe = gpio_rcar_probe,
658 .remove = gpio_rcar_remove,
661 .pm = &gpio_rcar_pm_ops,
662 .of_match_table = of_match_ptr(gpio_rcar_of_table),
666 module_platform_driver(gpio_rcar_device_driver);
668 MODULE_AUTHOR("Magnus Damm");
669 MODULE_DESCRIPTION("Renesas R-Car GPIO Driver");
670 MODULE_LICENSE("GPL v2");