1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * TI DaVinci GPIO Support
5 * Copyright (c) 2006-2007 David Brownell
6 * Copyright (c) 2007, MontaVista Software, Inc. <source@mvista.com>
9 #include <linux/gpio/driver.h>
10 #include <linux/errno.h>
11 #include <linux/kernel.h>
12 #include <linux/clk.h>
13 #include <linux/err.h>
15 #include <linux/irq.h>
16 #include <linux/irqdomain.h>
17 #include <linux/module.h>
18 #include <linux/pinctrl/consumer.h>
19 #include <linux/platform_device.h>
20 #include <linux/property.h>
21 #include <linux/irqchip/chained_irq.h>
22 #include <linux/spinlock.h>
23 #include <linux/pm_runtime.h>
25 #define MAX_REGS_BANKS 5
26 #define MAX_INT_PER_BANK 32
28 struct davinci_gpio_regs
{
41 typedef struct irq_chip
*(*gpio_get_irq_chip_cb_t
)(unsigned int irq
);
43 #define BINTEN 0x8 /* GPIO Interrupt Per-Bank Enable Register */
45 static void __iomem
*gpio_base
;
46 static unsigned int offset_array
[5] = {0x10, 0x38, 0x60, 0x88, 0xb0};
48 struct davinci_gpio_irq_data
{
50 struct davinci_gpio_controller
*chip
;
54 struct davinci_gpio_controller
{
55 struct gpio_chip chip
;
56 struct irq_domain
*irq_domain
;
57 /* Serialize access to GPIO registers */
59 void __iomem
*regs
[MAX_REGS_BANKS
];
61 int irqs
[MAX_INT_PER_BANK
];
62 struct davinci_gpio_regs context
[MAX_REGS_BANKS
];
66 static inline u32
__gpio_mask(unsigned gpio
)
68 return 1 << (gpio
% 32);
71 static inline struct davinci_gpio_regs __iomem
*irq2regs(struct irq_data
*d
)
73 struct davinci_gpio_regs __iomem
*g
;
75 g
= (__force
struct davinci_gpio_regs __iomem
*)irq_data_get_irq_chip_data(d
);
80 static int davinci_gpio_irq_setup(struct platform_device
*pdev
);
82 /*--------------------------------------------------------------------------*/
84 /* board setup code *MUST* setup pinmux and enable the GPIO clock. */
85 static inline int __davinci_direction(struct gpio_chip
*chip
,
86 unsigned offset
, bool out
, int value
)
88 struct davinci_gpio_controller
*d
= gpiochip_get_data(chip
);
89 struct davinci_gpio_regs __iomem
*g
;
92 int bank
= offset
/ 32;
93 u32 mask
= __gpio_mask(offset
);
96 spin_lock_irqsave(&d
->lock
, flags
);
97 temp
= readl_relaxed(&g
->dir
);
100 writel_relaxed(mask
, value
? &g
->set_data
: &g
->clr_data
);
104 writel_relaxed(temp
, &g
->dir
);
105 spin_unlock_irqrestore(&d
->lock
, flags
);
110 static int davinci_direction_in(struct gpio_chip
*chip
, unsigned offset
)
112 return __davinci_direction(chip
, offset
, false, 0);
116 davinci_direction_out(struct gpio_chip
*chip
, unsigned offset
, int value
)
118 return __davinci_direction(chip
, offset
, true, value
);
122 * Read the pin's value (works even if it's set up as output);
123 * returns zero/nonzero.
125 * Note that changes are synched to the GPIO clock, so reading values back
126 * right after you've set them may give old values.
128 static int davinci_gpio_get(struct gpio_chip
*chip
, unsigned offset
)
130 struct davinci_gpio_controller
*d
= gpiochip_get_data(chip
);
131 struct davinci_gpio_regs __iomem
*g
;
132 int bank
= offset
/ 32;
136 return !!(__gpio_mask(offset
) & readl_relaxed(&g
->in_data
));
140 * Assuming the pin is muxed as a gpio output, set its output value.
143 davinci_gpio_set(struct gpio_chip
*chip
, unsigned offset
, int value
)
145 struct davinci_gpio_controller
*d
= gpiochip_get_data(chip
);
146 struct davinci_gpio_regs __iomem
*g
;
147 int bank
= offset
/ 32;
151 writel_relaxed(__gpio_mask(offset
),
152 value
? &g
->set_data
: &g
->clr_data
);
155 static int davinci_gpio_probe(struct platform_device
*pdev
)
157 int bank
, i
, ret
= 0;
158 unsigned int ngpio
, nbank
, nirq
, gpio_unbanked
;
159 struct davinci_gpio_controller
*chips
;
160 struct device
*dev
= &pdev
->dev
;
163 * The gpio banks conceptually expose a segmented bitmap,
164 * and "ngpio" is one more than the largest zero-based
165 * bit index that's valid.
167 ret
= device_property_read_u32(dev
, "ti,ngpio", &ngpio
);
169 return dev_err_probe(dev
, ret
, "Failed to get the number of GPIOs\n");
171 return dev_err_probe(dev
, -EINVAL
, "How many GPIOs?\n");
174 * If there are unbanked interrupts then the number of
175 * interrupts is equal to number of gpios else all are banked so
176 * number of interrupts is equal to number of banks(each with 16 gpios)
178 ret
= device_property_read_u32(dev
, "ti,davinci-gpio-unbanked",
181 return dev_err_probe(dev
, ret
, "Failed to get the unbanked GPIOs property\n");
184 nirq
= gpio_unbanked
;
186 nirq
= DIV_ROUND_UP(ngpio
, 16);
188 if (nirq
> MAX_INT_PER_BANK
) {
189 dev_err(dev
, "Too many IRQs!\n");
193 chips
= devm_kzalloc(dev
, sizeof(*chips
), GFP_KERNEL
);
197 gpio_base
= devm_platform_ioremap_resource(pdev
, 0);
198 if (IS_ERR(gpio_base
))
199 return PTR_ERR(gpio_base
);
201 for (i
= 0; i
< nirq
; i
++) {
202 chips
->irqs
[i
] = platform_get_irq(pdev
, i
);
203 if (chips
->irqs
[i
] < 0)
204 return chips
->irqs
[i
];
207 chips
->chip
.label
= dev_name(dev
);
209 chips
->chip
.direction_input
= davinci_direction_in
;
210 chips
->chip
.get
= davinci_gpio_get
;
211 chips
->chip
.direction_output
= davinci_direction_out
;
212 chips
->chip
.set
= davinci_gpio_set
;
214 chips
->chip
.ngpio
= ngpio
;
215 chips
->chip
.base
= -1;
217 #ifdef CONFIG_OF_GPIO
218 chips
->chip
.parent
= dev
;
219 chips
->chip
.request
= gpiochip_generic_request
;
220 chips
->chip
.free
= gpiochip_generic_free
;
222 spin_lock_init(&chips
->lock
);
224 chips
->gpio_unbanked
= gpio_unbanked
;
226 nbank
= DIV_ROUND_UP(ngpio
, 32);
227 for (bank
= 0; bank
< nbank
; bank
++)
228 chips
->regs
[bank
] = gpio_base
+ offset_array
[bank
];
230 ret
= devm_gpiochip_add_data(dev
, &chips
->chip
, chips
);
234 platform_set_drvdata(pdev
, chips
);
235 ret
= davinci_gpio_irq_setup(pdev
);
242 /*--------------------------------------------------------------------------*/
244 * We expect irqs will normally be set up as input pins, but they can also be
245 * used as output pins ... which is convenient for testing.
247 * NOTE: The first few GPIOs also have direct INTC hookups in addition
248 * to their GPIOBNK0 irq, with a bit less overhead.
250 * All those INTC hookups (direct, plus several IRQ banks) can also
251 * serve as EDMA event triggers.
254 static void gpio_irq_mask(struct irq_data
*d
)
256 struct davinci_gpio_regs __iomem
*g
= irq2regs(d
);
257 uintptr_t mask
= (uintptr_t)irq_data_get_irq_handler_data(d
);
259 writel_relaxed(mask
, &g
->clr_falling
);
260 writel_relaxed(mask
, &g
->clr_rising
);
263 static void gpio_irq_unmask(struct irq_data
*d
)
265 struct davinci_gpio_regs __iomem
*g
= irq2regs(d
);
266 uintptr_t mask
= (uintptr_t)irq_data_get_irq_handler_data(d
);
267 unsigned status
= irqd_get_trigger_type(d
);
269 status
&= IRQ_TYPE_EDGE_FALLING
| IRQ_TYPE_EDGE_RISING
;
271 status
= IRQ_TYPE_EDGE_FALLING
| IRQ_TYPE_EDGE_RISING
;
273 if (status
& IRQ_TYPE_EDGE_FALLING
)
274 writel_relaxed(mask
, &g
->set_falling
);
275 if (status
& IRQ_TYPE_EDGE_RISING
)
276 writel_relaxed(mask
, &g
->set_rising
);
279 static int gpio_irq_type(struct irq_data
*d
, unsigned trigger
)
281 if (trigger
& ~(IRQ_TYPE_EDGE_FALLING
| IRQ_TYPE_EDGE_RISING
))
287 static struct irq_chip gpio_irqchip
= {
289 .irq_unmask
= gpio_irq_unmask
,
290 .irq_mask
= gpio_irq_mask
,
291 .irq_set_type
= gpio_irq_type
,
292 .flags
= IRQCHIP_SET_TYPE_MASKED
| IRQCHIP_SKIP_SET_WAKE
,
295 static void gpio_irq_handler(struct irq_desc
*desc
)
297 struct davinci_gpio_regs __iomem
*g
;
300 struct davinci_gpio_controller
*d
;
301 struct davinci_gpio_irq_data
*irqdata
;
303 irqdata
= (struct davinci_gpio_irq_data
*)irq_desc_get_handler_data(desc
);
304 bank_num
= irqdata
->bank_num
;
308 /* we only care about one bank */
309 if ((bank_num
% 2) == 1)
312 /* temporarily mask (level sensitive) parent IRQ */
313 chained_irq_enter(irq_desc_get_chip(desc
), desc
);
317 irq_hw_number_t hw_irq
;
320 status
= readl_relaxed(&g
->intstat
) & mask
;
323 writel_relaxed(status
, &g
->intstat
);
325 /* now demux them to the right lowlevel handler */
330 /* Max number of gpios per controller is 144 so
331 * hw_irq will be in [0..143]
333 hw_irq
= (bank_num
/ 2) * 32 + bit
;
335 generic_handle_domain_irq(d
->irq_domain
, hw_irq
);
338 chained_irq_exit(irq_desc_get_chip(desc
), desc
);
339 /* now it may re-trigger */
342 static int gpio_to_irq_banked(struct gpio_chip
*chip
, unsigned offset
)
344 struct davinci_gpio_controller
*d
= gpiochip_get_data(chip
);
347 return irq_create_mapping(d
->irq_domain
, offset
);
352 static int gpio_to_irq_unbanked(struct gpio_chip
*chip
, unsigned offset
)
354 struct davinci_gpio_controller
*d
= gpiochip_get_data(chip
);
357 * NOTE: we assume for now that only irqs in the first gpio_chip
358 * can provide direct-mapped IRQs to AINTC (up to 32 GPIOs).
360 if (offset
< d
->gpio_unbanked
)
361 return d
->irqs
[offset
];
366 static int gpio_irq_type_unbanked(struct irq_data
*data
, unsigned trigger
)
368 struct davinci_gpio_controller
*d
;
369 struct davinci_gpio_regs __iomem
*g
;
372 d
= (struct davinci_gpio_controller
*)irq_data_get_irq_handler_data(data
);
373 g
= (struct davinci_gpio_regs __iomem
*)d
->regs
[0];
374 for (i
= 0; i
< MAX_INT_PER_BANK
; i
++)
375 if (data
->irq
== d
->irqs
[i
])
378 if (i
== MAX_INT_PER_BANK
)
381 mask
= __gpio_mask(i
);
383 if (trigger
& ~(IRQ_TYPE_EDGE_FALLING
| IRQ_TYPE_EDGE_RISING
))
386 writel_relaxed(mask
, (trigger
& IRQ_TYPE_EDGE_FALLING
)
387 ? &g
->set_falling
: &g
->clr_falling
);
388 writel_relaxed(mask
, (trigger
& IRQ_TYPE_EDGE_RISING
)
389 ? &g
->set_rising
: &g
->clr_rising
);
395 davinci_gpio_irq_map(struct irq_domain
*d
, unsigned int irq
,
398 struct davinci_gpio_controller
*chips
=
399 (struct davinci_gpio_controller
*)d
->host_data
;
400 struct davinci_gpio_regs __iomem
*g
= chips
->regs
[hw
/ 32];
402 irq_set_chip_and_handler_name(irq
, &gpio_irqchip
, handle_simple_irq
,
404 irq_set_irq_type(irq
, IRQ_TYPE_NONE
);
405 irq_set_chip_data(irq
, (__force
void *)g
);
406 irq_set_handler_data(irq
, (void *)(uintptr_t)__gpio_mask(hw
));
411 static const struct irq_domain_ops davinci_gpio_irq_ops
= {
412 .map
= davinci_gpio_irq_map
,
413 .xlate
= irq_domain_xlate_onetwocell
,
416 static struct irq_chip
*davinci_gpio_get_irq_chip(unsigned int irq
)
418 static struct irq_chip_type gpio_unbanked
;
420 gpio_unbanked
= *irq_data_get_chip_type(irq_get_irq_data(irq
));
422 return &gpio_unbanked
.chip
;
425 static struct irq_chip
*keystone_gpio_get_irq_chip(unsigned int irq
)
427 static struct irq_chip gpio_unbanked
;
429 gpio_unbanked
= *irq_get_chip(irq
);
430 return &gpio_unbanked
;
433 static const struct of_device_id davinci_gpio_ids
[];
436 * NOTE: for suspend/resume, probably best to make a platform_device with
437 * suspend_late/resume_resume calls hooking into results of the set_wake()
438 * calls ... so if no gpios are wakeup events the clock can be disabled,
439 * with outputs left at previously set levels, and so that VDD3P3V.IOPWDN0
440 * (dm6446) can be set appropriately for GPIOV33 pins.
443 static int davinci_gpio_irq_setup(struct platform_device
*pdev
)
450 struct device
*dev
= &pdev
->dev
;
451 struct davinci_gpio_controller
*chips
= platform_get_drvdata(pdev
);
452 struct davinci_gpio_regs __iomem
*g
;
453 struct irq_domain
*irq_domain
= NULL
;
454 struct irq_chip
*irq_chip
;
455 struct davinci_gpio_irq_data
*irqdata
;
456 gpio_get_irq_chip_cb_t gpio_get_irq_chip
;
459 * Use davinci_gpio_get_irq_chip by default to handle non DT cases
461 gpio_get_irq_chip
= davinci_gpio_get_irq_chip
;
463 gpio_get_irq_chip
= (gpio_get_irq_chip_cb_t
)device_get_match_data(dev
);
465 ngpio
= chips
->chip
.ngpio
;
467 clk
= devm_clk_get_enabled(dev
, "gpio");
469 dev_err(dev
, "Error %ld getting gpio clock\n", PTR_ERR(clk
));
473 if (!chips
->gpio_unbanked
) {
474 irq
= devm_irq_alloc_descs(dev
, -1, 0, ngpio
, 0);
476 dev_err(dev
, "Couldn't allocate IRQ numbers\n");
480 irq_domain
= irq_domain_add_legacy(dev
->of_node
, ngpio
, irq
, 0,
481 &davinci_gpio_irq_ops
,
484 dev_err(dev
, "Couldn't register an IRQ domain\n");
490 * Arrange gpiod_to_irq() support, handling either direct IRQs or
491 * banked IRQs. Having GPIOs in the first GPIO bank use direct
492 * IRQs, while the others use banked IRQs, would need some setup
493 * tweaks to recognize hardware which can do that.
495 chips
->chip
.to_irq
= gpio_to_irq_banked
;
496 chips
->irq_domain
= irq_domain
;
499 * AINTC can handle direct/unbanked IRQs for GPIOs, with the GPIO
500 * controller only handling trigger modes. We currently assume no
501 * IRQ mux conflicts; gpio_irq_type_unbanked() is only for GPIOs.
503 if (chips
->gpio_unbanked
) {
504 /* pass "bank 0" GPIO IRQs to AINTC */
505 chips
->chip
.to_irq
= gpio_to_irq_unbanked
;
507 binten
= GENMASK(chips
->gpio_unbanked
/ 16, 0);
509 /* AINTC handles mask/unmask; GPIO handles triggering */
510 irq
= chips
->irqs
[0];
511 irq_chip
= gpio_get_irq_chip(irq
);
512 irq_chip
->name
= "GPIO-AINTC";
513 irq_chip
->irq_set_type
= gpio_irq_type_unbanked
;
515 /* default trigger: both edges */
517 writel_relaxed(~0, &g
->set_falling
);
518 writel_relaxed(~0, &g
->set_rising
);
520 /* set the direct IRQs up to use that irqchip */
521 for (gpio
= 0; gpio
< chips
->gpio_unbanked
; gpio
++) {
522 irq_set_chip(chips
->irqs
[gpio
], irq_chip
);
523 irq_set_handler_data(chips
->irqs
[gpio
], chips
);
524 irq_set_status_flags(chips
->irqs
[gpio
],
532 * Or, AINTC can handle IRQs for banks of 16 GPIO IRQs, which we
533 * then chain through our own handler.
535 for (gpio
= 0, bank
= 0; gpio
< ngpio
; bank
++, gpio
+= 16) {
536 /* disabled by default, enabled only as needed
537 * There are register sets for 32 GPIOs. 2 banks of 16
538 * GPIOs are covered by each set of registers hence divide by 2
540 g
= chips
->regs
[bank
/ 2];
541 writel_relaxed(~0, &g
->clr_falling
);
542 writel_relaxed(~0, &g
->clr_rising
);
545 * Each chip handles 32 gpios, and each irq bank consists of 16
546 * gpio irqs. Pass the irq bank's corresponding controller to
547 * the chained irq handler.
549 irqdata
= devm_kzalloc(&pdev
->dev
,
551 davinci_gpio_irq_data
),
557 irqdata
->bank_num
= bank
;
558 irqdata
->chip
= chips
;
560 irq_set_chained_handler_and_data(chips
->irqs
[bank
],
561 gpio_irq_handler
, irqdata
);
568 * BINTEN -- per-bank interrupt enable. genirq would also let these
569 * bits be set/cleared dynamically.
571 writel_relaxed(binten
, gpio_base
+ BINTEN
);
576 static void davinci_gpio_save_context(struct davinci_gpio_controller
*chips
,
579 struct davinci_gpio_regs __iomem
*g
;
580 struct davinci_gpio_regs
*context
;
584 base
= chips
->regs
[0] - offset_array
[0];
585 chips
->binten_context
= readl_relaxed(base
+ BINTEN
);
587 for (bank
= 0; bank
< nbank
; bank
++) {
588 g
= chips
->regs
[bank
];
589 context
= &chips
->context
[bank
];
590 context
->dir
= readl_relaxed(&g
->dir
);
591 context
->set_data
= readl_relaxed(&g
->set_data
);
592 context
->set_rising
= readl_relaxed(&g
->set_rising
);
593 context
->set_falling
= readl_relaxed(&g
->set_falling
);
596 /* Clear all interrupt status registers */
597 writel_relaxed(GENMASK(31, 0), &g
->intstat
);
600 static void davinci_gpio_restore_context(struct davinci_gpio_controller
*chips
,
603 struct davinci_gpio_regs __iomem
*g
;
604 struct davinci_gpio_regs
*context
;
608 base
= chips
->regs
[0] - offset_array
[0];
610 if (readl_relaxed(base
+ BINTEN
) != chips
->binten_context
)
611 writel_relaxed(chips
->binten_context
, base
+ BINTEN
);
613 for (bank
= 0; bank
< nbank
; bank
++) {
614 g
= chips
->regs
[bank
];
615 context
= &chips
->context
[bank
];
616 if (readl_relaxed(&g
->dir
) != context
->dir
)
617 writel_relaxed(context
->dir
, &g
->dir
);
618 if (readl_relaxed(&g
->set_data
) != context
->set_data
)
619 writel_relaxed(context
->set_data
, &g
->set_data
);
620 if (readl_relaxed(&g
->set_rising
) != context
->set_rising
)
621 writel_relaxed(context
->set_rising
, &g
->set_rising
);
622 if (readl_relaxed(&g
->set_falling
) != context
->set_falling
)
623 writel_relaxed(context
->set_falling
, &g
->set_falling
);
627 static int davinci_gpio_suspend(struct device
*dev
)
629 struct davinci_gpio_controller
*chips
= dev_get_drvdata(dev
);
630 u32 nbank
= DIV_ROUND_UP(chips
->chip
.ngpio
, 32);
632 davinci_gpio_save_context(chips
, nbank
);
637 static int davinci_gpio_resume(struct device
*dev
)
639 struct davinci_gpio_controller
*chips
= dev_get_drvdata(dev
);
640 u32 nbank
= DIV_ROUND_UP(chips
->chip
.ngpio
, 32);
642 davinci_gpio_restore_context(chips
, nbank
);
647 static DEFINE_SIMPLE_DEV_PM_OPS(davinci_gpio_dev_pm_ops
, davinci_gpio_suspend
,
648 davinci_gpio_resume
);
650 static const struct of_device_id davinci_gpio_ids
[] = {
651 { .compatible
= "ti,keystone-gpio", keystone_gpio_get_irq_chip
},
652 { .compatible
= "ti,am654-gpio", keystone_gpio_get_irq_chip
},
653 { .compatible
= "ti,dm6441-gpio", davinci_gpio_get_irq_chip
},
656 MODULE_DEVICE_TABLE(of
, davinci_gpio_ids
);
658 static struct platform_driver davinci_gpio_driver
= {
659 .probe
= davinci_gpio_probe
,
661 .name
= "davinci_gpio",
662 .pm
= pm_sleep_ptr(&davinci_gpio_dev_pm_ops
),
663 .of_match_table
= davinci_gpio_ids
,
668 * GPIO driver registration needs to be done before machine_init functions
669 * access GPIO. Hence davinci_gpio_drv_reg() is a postcore_initcall.
671 static int __init
davinci_gpio_drv_reg(void)
673 return platform_driver_register(&davinci_gpio_driver
);
675 postcore_initcall(davinci_gpio_drv_reg
);
677 static void __exit
davinci_gpio_exit(void)
679 platform_driver_unregister(&davinci_gpio_driver
);
681 module_exit(davinci_gpio_exit
);
683 MODULE_AUTHOR("Jan Kotas <jank@cadence.com>");
684 MODULE_DESCRIPTION("DAVINCI GPIO driver");
685 MODULE_LICENSE("GPL");
686 MODULE_ALIAS("platform:gpio-davinci");