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_priv
{
39 struct gpio_chip gpio_chip
;
40 struct irq_chip irq_chip
;
41 unsigned int irq_parent
;
43 bool has_both_edge_trigger
;
44 struct gpio_rcar_bank_info bank_info
;
47 #define IOINTSEL 0x00 /* General IO/Interrupt Switching Register */
48 #define INOUTSEL 0x04 /* General Input/Output Switching Register */
49 #define OUTDT 0x08 /* General Output Register */
50 #define INDT 0x0c /* General Input Register */
51 #define INTDT 0x10 /* Interrupt Display Register */
52 #define INTCLR 0x14 /* Interrupt Clear Register */
53 #define INTMSK 0x18 /* Interrupt Mask Register */
54 #define MSKCLR 0x1c /* Interrupt Mask Clear Register */
55 #define POSNEG 0x20 /* Positive/Negative Logic Select Register */
56 #define EDGLEVEL 0x24 /* Edge/level Select Register */
57 #define FILONOFF 0x28 /* Chattering Prevention On/Off Register */
58 #define BOTHEDGE 0x4c /* One Edge/Both Edge Select Register */
60 #define RCAR_MAX_GPIO_PER_BANK 32
62 static inline u32
gpio_rcar_read(struct gpio_rcar_priv
*p
, int offs
)
64 return ioread32(p
->base
+ offs
);
67 static inline void gpio_rcar_write(struct gpio_rcar_priv
*p
, int offs
,
70 iowrite32(value
, p
->base
+ offs
);
73 static void gpio_rcar_modify_bit(struct gpio_rcar_priv
*p
, int offs
,
76 u32 tmp
= gpio_rcar_read(p
, offs
);
83 gpio_rcar_write(p
, offs
, tmp
);
86 static void gpio_rcar_irq_disable(struct irq_data
*d
)
88 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
89 struct gpio_rcar_priv
*p
= gpiochip_get_data(gc
);
91 gpio_rcar_write(p
, INTMSK
, ~BIT(irqd_to_hwirq(d
)));
94 static void gpio_rcar_irq_enable(struct irq_data
*d
)
96 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
97 struct gpio_rcar_priv
*p
= gpiochip_get_data(gc
);
99 gpio_rcar_write(p
, MSKCLR
, BIT(irqd_to_hwirq(d
)));
102 static void gpio_rcar_config_interrupt_input_mode(struct gpio_rcar_priv
*p
,
104 bool active_high_rising_edge
,
110 /* follow steps in the GPIO documentation for
111 * "Setting Edge-Sensitive Interrupt Input Mode" and
112 * "Setting Level-Sensitive Interrupt Input Mode"
115 spin_lock_irqsave(&p
->lock
, flags
);
117 /* Configure postive or negative logic in POSNEG */
118 gpio_rcar_modify_bit(p
, POSNEG
, hwirq
, !active_high_rising_edge
);
120 /* Configure edge or level trigger in EDGLEVEL */
121 gpio_rcar_modify_bit(p
, EDGLEVEL
, hwirq
, !level_trigger
);
123 /* Select one edge or both edges in BOTHEDGE */
124 if (p
->has_both_edge_trigger
)
125 gpio_rcar_modify_bit(p
, BOTHEDGE
, hwirq
, both
);
127 /* Select "Interrupt Input Mode" in IOINTSEL */
128 gpio_rcar_modify_bit(p
, IOINTSEL
, hwirq
, true);
130 /* Write INTCLR in case of edge trigger */
132 gpio_rcar_write(p
, INTCLR
, BIT(hwirq
));
134 spin_unlock_irqrestore(&p
->lock
, flags
);
137 static int gpio_rcar_irq_set_type(struct irq_data
*d
, unsigned int type
)
139 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
140 struct gpio_rcar_priv
*p
= gpiochip_get_data(gc
);
141 unsigned int hwirq
= irqd_to_hwirq(d
);
143 dev_dbg(p
->dev
, "sense irq = %d, type = %d\n", hwirq
, type
);
145 switch (type
& IRQ_TYPE_SENSE_MASK
) {
146 case IRQ_TYPE_LEVEL_HIGH
:
147 gpio_rcar_config_interrupt_input_mode(p
, hwirq
, true, true,
150 case IRQ_TYPE_LEVEL_LOW
:
151 gpio_rcar_config_interrupt_input_mode(p
, hwirq
, false, true,
154 case IRQ_TYPE_EDGE_RISING
:
155 gpio_rcar_config_interrupt_input_mode(p
, hwirq
, true, false,
158 case IRQ_TYPE_EDGE_FALLING
:
159 gpio_rcar_config_interrupt_input_mode(p
, hwirq
, false, false,
162 case IRQ_TYPE_EDGE_BOTH
:
163 if (!p
->has_both_edge_trigger
)
165 gpio_rcar_config_interrupt_input_mode(p
, hwirq
, true, false,
174 static int gpio_rcar_irq_set_wake(struct irq_data
*d
, unsigned int on
)
176 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
177 struct gpio_rcar_priv
*p
= gpiochip_get_data(gc
);
181 error
= irq_set_irq_wake(p
->irq_parent
, on
);
183 dev_dbg(p
->dev
, "irq %u doesn't support irq_set_wake\n",
190 atomic_inc(&p
->wakeup_path
);
192 atomic_dec(&p
->wakeup_path
);
197 static irqreturn_t
gpio_rcar_irq_handler(int irq
, void *dev_id
)
199 struct gpio_rcar_priv
*p
= dev_id
;
201 unsigned int offset
, irqs_handled
= 0;
203 while ((pending
= gpio_rcar_read(p
, INTDT
) &
204 gpio_rcar_read(p
, INTMSK
))) {
205 offset
= __ffs(pending
);
206 gpio_rcar_write(p
, INTCLR
, BIT(offset
));
207 generic_handle_irq(irq_find_mapping(p
->gpio_chip
.irq
.domain
,
212 return irqs_handled
? IRQ_HANDLED
: IRQ_NONE
;
215 static void gpio_rcar_config_general_input_output_mode(struct gpio_chip
*chip
,
219 struct gpio_rcar_priv
*p
= gpiochip_get_data(chip
);
222 /* follow steps in the GPIO documentation for
223 * "Setting General Output Mode" and
224 * "Setting General Input Mode"
227 spin_lock_irqsave(&p
->lock
, flags
);
229 /* Configure postive logic in POSNEG */
230 gpio_rcar_modify_bit(p
, POSNEG
, gpio
, false);
232 /* Select "General Input/Output Mode" in IOINTSEL */
233 gpio_rcar_modify_bit(p
, IOINTSEL
, gpio
, false);
235 /* Select Input Mode or Output Mode in INOUTSEL */
236 gpio_rcar_modify_bit(p
, INOUTSEL
, gpio
, output
);
238 spin_unlock_irqrestore(&p
->lock
, flags
);
241 static int gpio_rcar_request(struct gpio_chip
*chip
, unsigned offset
)
243 struct gpio_rcar_priv
*p
= gpiochip_get_data(chip
);
246 error
= pm_runtime_get_sync(p
->dev
);
250 error
= pinctrl_gpio_request(chip
->base
+ offset
);
252 pm_runtime_put(p
->dev
);
257 static void gpio_rcar_free(struct gpio_chip
*chip
, unsigned offset
)
259 struct gpio_rcar_priv
*p
= gpiochip_get_data(chip
);
261 pinctrl_gpio_free(chip
->base
+ offset
);
264 * Set the GPIO as an input to ensure that the next GPIO request won't
265 * drive the GPIO pin as an output.
267 gpio_rcar_config_general_input_output_mode(chip
, offset
, false);
269 pm_runtime_put(p
->dev
);
272 static int gpio_rcar_get_direction(struct gpio_chip
*chip
, unsigned int offset
)
274 struct gpio_rcar_priv
*p
= gpiochip_get_data(chip
);
276 return !(gpio_rcar_read(p
, INOUTSEL
) & BIT(offset
));
279 static int gpio_rcar_direction_input(struct gpio_chip
*chip
, unsigned offset
)
281 gpio_rcar_config_general_input_output_mode(chip
, offset
, false);
285 static int gpio_rcar_get(struct gpio_chip
*chip
, unsigned offset
)
287 u32 bit
= BIT(offset
);
289 /* testing on r8a7790 shows that INDT does not show correct pin state
290 * when configured as output, so use OUTDT in case of output pins */
291 if (gpio_rcar_read(gpiochip_get_data(chip
), INOUTSEL
) & bit
)
292 return !!(gpio_rcar_read(gpiochip_get_data(chip
), OUTDT
) & bit
);
294 return !!(gpio_rcar_read(gpiochip_get_data(chip
), INDT
) & bit
);
297 static void gpio_rcar_set(struct gpio_chip
*chip
, unsigned offset
, int value
)
299 struct gpio_rcar_priv
*p
= gpiochip_get_data(chip
);
302 spin_lock_irqsave(&p
->lock
, flags
);
303 gpio_rcar_modify_bit(p
, OUTDT
, offset
, value
);
304 spin_unlock_irqrestore(&p
->lock
, flags
);
307 static void gpio_rcar_set_multiple(struct gpio_chip
*chip
, unsigned long *mask
,
310 struct gpio_rcar_priv
*p
= gpiochip_get_data(chip
);
314 bankmask
= mask
[0] & GENMASK(chip
->ngpio
- 1, 0);
315 if (chip
->valid_mask
)
316 bankmask
&= chip
->valid_mask
[0];
321 spin_lock_irqsave(&p
->lock
, flags
);
322 val
= gpio_rcar_read(p
, OUTDT
);
324 val
|= (bankmask
& bits
[0]);
325 gpio_rcar_write(p
, OUTDT
, val
);
326 spin_unlock_irqrestore(&p
->lock
, flags
);
329 static int gpio_rcar_direction_output(struct gpio_chip
*chip
, unsigned offset
,
332 /* write GPIO value to output before selecting output mode of pin */
333 gpio_rcar_set(chip
, offset
, value
);
334 gpio_rcar_config_general_input_output_mode(chip
, offset
, true);
338 struct gpio_rcar_info
{
339 bool has_both_edge_trigger
;
342 static const struct gpio_rcar_info gpio_rcar_info_gen1
= {
343 .has_both_edge_trigger
= false,
346 static const struct gpio_rcar_info gpio_rcar_info_gen2
= {
347 .has_both_edge_trigger
= true,
350 static const struct of_device_id gpio_rcar_of_table
[] = {
352 .compatible
= "renesas,gpio-r8a7743",
353 /* RZ/G1 GPIO is identical to R-Car Gen2. */
354 .data
= &gpio_rcar_info_gen2
,
356 .compatible
= "renesas,gpio-r8a7790",
357 .data
= &gpio_rcar_info_gen2
,
359 .compatible
= "renesas,gpio-r8a7791",
360 .data
= &gpio_rcar_info_gen2
,
362 .compatible
= "renesas,gpio-r8a7792",
363 .data
= &gpio_rcar_info_gen2
,
365 .compatible
= "renesas,gpio-r8a7793",
366 .data
= &gpio_rcar_info_gen2
,
368 .compatible
= "renesas,gpio-r8a7794",
369 .data
= &gpio_rcar_info_gen2
,
371 .compatible
= "renesas,gpio-r8a7795",
372 /* Gen3 GPIO is identical to Gen2. */
373 .data
= &gpio_rcar_info_gen2
,
375 .compatible
= "renesas,gpio-r8a7796",
376 /* Gen3 GPIO is identical to Gen2. */
377 .data
= &gpio_rcar_info_gen2
,
379 .compatible
= "renesas,rcar-gen1-gpio",
380 .data
= &gpio_rcar_info_gen1
,
382 .compatible
= "renesas,rcar-gen2-gpio",
383 .data
= &gpio_rcar_info_gen2
,
385 .compatible
= "renesas,rcar-gen3-gpio",
386 /* Gen3 GPIO is identical to Gen2. */
387 .data
= &gpio_rcar_info_gen2
,
389 .compatible
= "renesas,gpio-rcar",
390 .data
= &gpio_rcar_info_gen1
,
396 MODULE_DEVICE_TABLE(of
, gpio_rcar_of_table
);
398 static int gpio_rcar_parse_dt(struct gpio_rcar_priv
*p
, unsigned int *npins
)
400 struct device_node
*np
= p
->dev
->of_node
;
401 const struct gpio_rcar_info
*info
;
402 struct of_phandle_args args
;
405 info
= of_device_get_match_data(p
->dev
);
407 ret
= of_parse_phandle_with_fixed_args(np
, "gpio-ranges", 3, 0, &args
);
408 *npins
= ret
== 0 ? args
.args
[2] : RCAR_MAX_GPIO_PER_BANK
;
409 p
->has_both_edge_trigger
= info
->has_both_edge_trigger
;
411 if (*npins
== 0 || *npins
> RCAR_MAX_GPIO_PER_BANK
) {
412 dev_warn(p
->dev
, "Invalid number of gpio lines %u, using %u\n",
413 *npins
, RCAR_MAX_GPIO_PER_BANK
);
414 *npins
= RCAR_MAX_GPIO_PER_BANK
;
420 static int gpio_rcar_probe(struct platform_device
*pdev
)
422 struct gpio_rcar_priv
*p
;
423 struct resource
*io
, *irq
;
424 struct gpio_chip
*gpio_chip
;
425 struct irq_chip
*irq_chip
;
426 struct device
*dev
= &pdev
->dev
;
427 const char *name
= dev_name(dev
);
431 p
= devm_kzalloc(dev
, sizeof(*p
), GFP_KERNEL
);
436 spin_lock_init(&p
->lock
);
438 /* Get device configuration from DT node */
439 ret
= gpio_rcar_parse_dt(p
, &npins
);
443 platform_set_drvdata(pdev
, p
);
445 pm_runtime_enable(dev
);
447 irq
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
449 dev_err(dev
, "missing IRQ\n");
454 io
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
455 p
->base
= devm_ioremap_resource(dev
, io
);
456 if (IS_ERR(p
->base
)) {
457 ret
= PTR_ERR(p
->base
);
461 gpio_chip
= &p
->gpio_chip
;
462 gpio_chip
->request
= gpio_rcar_request
;
463 gpio_chip
->free
= gpio_rcar_free
;
464 gpio_chip
->get_direction
= gpio_rcar_get_direction
;
465 gpio_chip
->direction_input
= gpio_rcar_direction_input
;
466 gpio_chip
->get
= gpio_rcar_get
;
467 gpio_chip
->direction_output
= gpio_rcar_direction_output
;
468 gpio_chip
->set
= gpio_rcar_set
;
469 gpio_chip
->set_multiple
= gpio_rcar_set_multiple
;
470 gpio_chip
->label
= name
;
471 gpio_chip
->parent
= dev
;
472 gpio_chip
->owner
= THIS_MODULE
;
473 gpio_chip
->base
= -1;
474 gpio_chip
->ngpio
= npins
;
476 irq_chip
= &p
->irq_chip
;
477 irq_chip
->name
= name
;
478 irq_chip
->parent_device
= dev
;
479 irq_chip
->irq_mask
= gpio_rcar_irq_disable
;
480 irq_chip
->irq_unmask
= gpio_rcar_irq_enable
;
481 irq_chip
->irq_set_type
= gpio_rcar_irq_set_type
;
482 irq_chip
->irq_set_wake
= gpio_rcar_irq_set_wake
;
483 irq_chip
->flags
= IRQCHIP_SET_TYPE_MASKED
| IRQCHIP_MASK_ON_SUSPEND
;
485 ret
= gpiochip_add_data(gpio_chip
, p
);
487 dev_err(dev
, "failed to add GPIO controller\n");
491 ret
= gpiochip_irqchip_add(gpio_chip
, irq_chip
, 0, handle_level_irq
,
494 dev_err(dev
, "cannot add irqchip\n");
498 p
->irq_parent
= irq
->start
;
499 if (devm_request_irq(dev
, irq
->start
, gpio_rcar_irq_handler
,
500 IRQF_SHARED
, name
, p
)) {
501 dev_err(dev
, "failed to request IRQ\n");
506 dev_info(dev
, "driving %d GPIOs\n", npins
);
511 gpiochip_remove(gpio_chip
);
513 pm_runtime_disable(dev
);
517 static int gpio_rcar_remove(struct platform_device
*pdev
)
519 struct gpio_rcar_priv
*p
= platform_get_drvdata(pdev
);
521 gpiochip_remove(&p
->gpio_chip
);
523 pm_runtime_disable(&pdev
->dev
);
527 #ifdef CONFIG_PM_SLEEP
528 static int gpio_rcar_suspend(struct device
*dev
)
530 struct gpio_rcar_priv
*p
= dev_get_drvdata(dev
);
532 p
->bank_info
.iointsel
= gpio_rcar_read(p
, IOINTSEL
);
533 p
->bank_info
.inoutsel
= gpio_rcar_read(p
, INOUTSEL
);
534 p
->bank_info
.outdt
= gpio_rcar_read(p
, OUTDT
);
535 p
->bank_info
.intmsk
= gpio_rcar_read(p
, INTMSK
);
536 p
->bank_info
.posneg
= gpio_rcar_read(p
, POSNEG
);
537 p
->bank_info
.edglevel
= gpio_rcar_read(p
, EDGLEVEL
);
538 if (p
->has_both_edge_trigger
)
539 p
->bank_info
.bothedge
= gpio_rcar_read(p
, BOTHEDGE
);
541 if (atomic_read(&p
->wakeup_path
))
542 device_set_wakeup_path(dev
);
547 static int gpio_rcar_resume(struct device
*dev
)
549 struct gpio_rcar_priv
*p
= dev_get_drvdata(dev
);
553 for (offset
= 0; offset
< p
->gpio_chip
.ngpio
; offset
++) {
554 if (!gpiochip_line_is_valid(&p
->gpio_chip
, offset
))
559 if (!(p
->bank_info
.iointsel
& mask
)) {
560 if (p
->bank_info
.inoutsel
& mask
)
561 gpio_rcar_direction_output(
562 &p
->gpio_chip
, offset
,
563 !!(p
->bank_info
.outdt
& mask
));
565 gpio_rcar_direction_input(&p
->gpio_chip
,
569 gpio_rcar_config_interrupt_input_mode(
572 !(p
->bank_info
.posneg
& mask
),
573 !(p
->bank_info
.edglevel
& mask
),
574 !!(p
->bank_info
.bothedge
& mask
));
576 if (p
->bank_info
.intmsk
& mask
)
577 gpio_rcar_write(p
, MSKCLR
, mask
);
583 #endif /* CONFIG_PM_SLEEP*/
585 static SIMPLE_DEV_PM_OPS(gpio_rcar_pm_ops
, gpio_rcar_suspend
, gpio_rcar_resume
);
587 static struct platform_driver gpio_rcar_device_driver
= {
588 .probe
= gpio_rcar_probe
,
589 .remove
= gpio_rcar_remove
,
592 .pm
= &gpio_rcar_pm_ops
,
593 .of_match_table
= of_match_ptr(gpio_rcar_of_table
),
597 module_platform_driver(gpio_rcar_device_driver
);
599 MODULE_AUTHOR("Magnus Damm");
600 MODULE_DESCRIPTION("Renesas R-Car GPIO Driver");
601 MODULE_LICENSE("GPL v2");