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>
19 #include <linux/pinctrl/consumer.h>
20 #include <linux/platform_device.h>
21 #include <linux/property.h>
22 #include <linux/irqchip/chained_irq.h>
23 #include <linux/spinlock.h>
24 #include <linux/pm_runtime.h>
26 #define MAX_REGS_BANKS 5
27 #define MAX_INT_PER_BANK 32
29 struct davinci_gpio_regs
{
42 typedef struct irq_chip
*(*gpio_get_irq_chip_cb_t
)(unsigned int irq
);
44 #define BINTEN 0x8 /* GPIO Interrupt Per-Bank Enable Register */
46 static void __iomem
*gpio_base
;
47 static unsigned int offset_array
[5] = {0x10, 0x38, 0x60, 0x88, 0xb0};
49 struct davinci_gpio_irq_data
{
51 struct davinci_gpio_controller
*chip
;
55 struct davinci_gpio_controller
{
56 struct gpio_chip chip
;
57 struct irq_domain
*irq_domain
;
58 /* Serialize access to GPIO registers */
60 void __iomem
*regs
[MAX_REGS_BANKS
];
62 int irqs
[MAX_INT_PER_BANK
];
63 struct davinci_gpio_regs context
[MAX_REGS_BANKS
];
67 static inline u32
__gpio_mask(unsigned gpio
)
69 return 1 << (gpio
% 32);
72 static inline struct davinci_gpio_regs __iomem
*irq2regs(struct irq_data
*d
)
74 struct davinci_gpio_regs __iomem
*g
;
76 g
= (__force
struct davinci_gpio_regs __iomem
*)irq_data_get_irq_chip_data(d
);
81 static int davinci_gpio_irq_setup(struct platform_device
*pdev
);
83 /*--------------------------------------------------------------------------*/
85 /* board setup code *MUST* setup pinmux and enable the GPIO clock. */
86 static inline int __davinci_direction(struct gpio_chip
*chip
,
87 unsigned offset
, bool out
, int value
)
89 struct davinci_gpio_controller
*d
= gpiochip_get_data(chip
);
90 struct davinci_gpio_regs __iomem
*g
;
93 int bank
= offset
/ 32;
94 u32 mask
= __gpio_mask(offset
);
97 spin_lock_irqsave(&d
->lock
, flags
);
98 temp
= readl_relaxed(&g
->dir
);
101 writel_relaxed(mask
, value
? &g
->set_data
: &g
->clr_data
);
105 writel_relaxed(temp
, &g
->dir
);
106 spin_unlock_irqrestore(&d
->lock
, flags
);
111 static int davinci_direction_in(struct gpio_chip
*chip
, unsigned offset
)
113 return __davinci_direction(chip
, offset
, false, 0);
117 davinci_direction_out(struct gpio_chip
*chip
, unsigned offset
, int value
)
119 return __davinci_direction(chip
, offset
, true, value
);
123 * Read the pin's value (works even if it's set up as output);
124 * returns zero/nonzero.
126 * Note that changes are synched to the GPIO clock, so reading values back
127 * right after you've set them may give old values.
129 static int davinci_gpio_get(struct gpio_chip
*chip
, unsigned offset
)
131 struct davinci_gpio_controller
*d
= gpiochip_get_data(chip
);
132 struct davinci_gpio_regs __iomem
*g
;
133 int bank
= offset
/ 32;
137 return !!(__gpio_mask(offset
) & readl_relaxed(&g
->in_data
));
141 * Assuming the pin is muxed as a gpio output, set its output value.
144 davinci_gpio_set(struct gpio_chip
*chip
, unsigned offset
, int value
)
146 struct davinci_gpio_controller
*d
= gpiochip_get_data(chip
);
147 struct davinci_gpio_regs __iomem
*g
;
148 int bank
= offset
/ 32;
152 writel_relaxed(__gpio_mask(offset
),
153 value
? &g
->set_data
: &g
->clr_data
);
156 static int davinci_gpio_probe(struct platform_device
*pdev
)
158 int bank
, i
, ret
= 0;
159 unsigned int ngpio
, nbank
, nirq
, gpio_unbanked
;
160 struct davinci_gpio_controller
*chips
;
161 struct device
*dev
= &pdev
->dev
;
162 struct device_node
*dn
= dev_of_node(dev
);
165 * The gpio banks conceptually expose a segmented bitmap,
166 * and "ngpio" is one more than the largest zero-based
167 * bit index that's valid.
169 ret
= of_property_read_u32(dn
, "ti,ngpio", &ngpio
);
171 return dev_err_probe(dev
, ret
, "Failed to get the number of GPIOs\n");
173 return dev_err_probe(dev
, -EINVAL
, "How many GPIOs?\n");
176 * If there are unbanked interrupts then the number of
177 * interrupts is equal to number of gpios else all are banked so
178 * number of interrupts is equal to number of banks(each with 16 gpios)
180 ret
= of_property_read_u32(dn
, "ti,davinci-gpio-unbanked",
183 return dev_err_probe(dev
, ret
, "Failed to get the unbanked GPIOs property\n");
186 nirq
= gpio_unbanked
;
188 nirq
= DIV_ROUND_UP(ngpio
, 16);
190 if (nirq
> MAX_INT_PER_BANK
) {
191 dev_err(dev
, "Too many IRQs!\n");
195 chips
= devm_kzalloc(dev
, sizeof(*chips
), GFP_KERNEL
);
199 gpio_base
= devm_platform_ioremap_resource(pdev
, 0);
200 if (IS_ERR(gpio_base
))
201 return PTR_ERR(gpio_base
);
203 for (i
= 0; i
< nirq
; i
++) {
204 chips
->irqs
[i
] = platform_get_irq(pdev
, i
);
205 if (chips
->irqs
[i
] < 0)
206 return chips
->irqs
[i
];
209 chips
->chip
.label
= dev_name(dev
);
211 chips
->chip
.direction_input
= davinci_direction_in
;
212 chips
->chip
.get
= davinci_gpio_get
;
213 chips
->chip
.direction_output
= davinci_direction_out
;
214 chips
->chip
.set
= davinci_gpio_set
;
216 chips
->chip
.ngpio
= ngpio
;
217 chips
->chip
.base
= -1;
219 #ifdef CONFIG_OF_GPIO
220 chips
->chip
.parent
= dev
;
221 chips
->chip
.request
= gpiochip_generic_request
;
222 chips
->chip
.free
= gpiochip_generic_free
;
224 spin_lock_init(&chips
->lock
);
226 chips
->gpio_unbanked
= gpio_unbanked
;
228 nbank
= DIV_ROUND_UP(ngpio
, 32);
229 for (bank
= 0; bank
< nbank
; bank
++)
230 chips
->regs
[bank
] = gpio_base
+ offset_array
[bank
];
232 ret
= devm_gpiochip_add_data(dev
, &chips
->chip
, chips
);
236 platform_set_drvdata(pdev
, chips
);
237 ret
= davinci_gpio_irq_setup(pdev
);
244 /*--------------------------------------------------------------------------*/
246 * We expect irqs will normally be set up as input pins, but they can also be
247 * used as output pins ... which is convenient for testing.
249 * NOTE: The first few GPIOs also have direct INTC hookups in addition
250 * to their GPIOBNK0 irq, with a bit less overhead.
252 * All those INTC hookups (direct, plus several IRQ banks) can also
253 * serve as EDMA event triggers.
256 static void gpio_irq_mask(struct irq_data
*d
)
258 struct davinci_gpio_regs __iomem
*g
= irq2regs(d
);
259 uintptr_t mask
= (uintptr_t)irq_data_get_irq_handler_data(d
);
261 writel_relaxed(mask
, &g
->clr_falling
);
262 writel_relaxed(mask
, &g
->clr_rising
);
265 static void gpio_irq_unmask(struct irq_data
*d
)
267 struct davinci_gpio_regs __iomem
*g
= irq2regs(d
);
268 uintptr_t mask
= (uintptr_t)irq_data_get_irq_handler_data(d
);
269 unsigned status
= irqd_get_trigger_type(d
);
271 status
&= IRQ_TYPE_EDGE_FALLING
| IRQ_TYPE_EDGE_RISING
;
273 status
= IRQ_TYPE_EDGE_FALLING
| IRQ_TYPE_EDGE_RISING
;
275 if (status
& IRQ_TYPE_EDGE_FALLING
)
276 writel_relaxed(mask
, &g
->set_falling
);
277 if (status
& IRQ_TYPE_EDGE_RISING
)
278 writel_relaxed(mask
, &g
->set_rising
);
281 static int gpio_irq_type(struct irq_data
*d
, unsigned trigger
)
283 if (trigger
& ~(IRQ_TYPE_EDGE_FALLING
| IRQ_TYPE_EDGE_RISING
))
289 static struct irq_chip gpio_irqchip
= {
291 .irq_unmask
= gpio_irq_unmask
,
292 .irq_mask
= gpio_irq_mask
,
293 .irq_set_type
= gpio_irq_type
,
294 .flags
= IRQCHIP_SET_TYPE_MASKED
| IRQCHIP_SKIP_SET_WAKE
,
297 static void gpio_irq_handler(struct irq_desc
*desc
)
299 struct davinci_gpio_regs __iomem
*g
;
302 struct davinci_gpio_controller
*d
;
303 struct davinci_gpio_irq_data
*irqdata
;
305 irqdata
= (struct davinci_gpio_irq_data
*)irq_desc_get_handler_data(desc
);
306 bank_num
= irqdata
->bank_num
;
310 /* we only care about one bank */
311 if ((bank_num
% 2) == 1)
314 /* temporarily mask (level sensitive) parent IRQ */
315 chained_irq_enter(irq_desc_get_chip(desc
), desc
);
319 irq_hw_number_t hw_irq
;
322 status
= readl_relaxed(&g
->intstat
) & mask
;
325 writel_relaxed(status
, &g
->intstat
);
327 /* now demux them to the right lowlevel handler */
332 /* Max number of gpios per controller is 144 so
333 * hw_irq will be in [0..143]
335 hw_irq
= (bank_num
/ 2) * 32 + bit
;
337 generic_handle_domain_irq(d
->irq_domain
, hw_irq
);
340 chained_irq_exit(irq_desc_get_chip(desc
), desc
);
341 /* now it may re-trigger */
344 static int gpio_to_irq_banked(struct gpio_chip
*chip
, unsigned offset
)
346 struct davinci_gpio_controller
*d
= gpiochip_get_data(chip
);
349 return irq_create_mapping(d
->irq_domain
, offset
);
354 static int gpio_to_irq_unbanked(struct gpio_chip
*chip
, unsigned offset
)
356 struct davinci_gpio_controller
*d
= gpiochip_get_data(chip
);
359 * NOTE: we assume for now that only irqs in the first gpio_chip
360 * can provide direct-mapped IRQs to AINTC (up to 32 GPIOs).
362 if (offset
< d
->gpio_unbanked
)
363 return d
->irqs
[offset
];
368 static int gpio_irq_type_unbanked(struct irq_data
*data
, unsigned trigger
)
370 struct davinci_gpio_controller
*d
;
371 struct davinci_gpio_regs __iomem
*g
;
374 d
= (struct davinci_gpio_controller
*)irq_data_get_irq_handler_data(data
);
375 g
= (struct davinci_gpio_regs __iomem
*)d
->regs
[0];
376 for (i
= 0; i
< MAX_INT_PER_BANK
; i
++)
377 if (data
->irq
== d
->irqs
[i
])
380 if (i
== MAX_INT_PER_BANK
)
383 mask
= __gpio_mask(i
);
385 if (trigger
& ~(IRQ_TYPE_EDGE_FALLING
| IRQ_TYPE_EDGE_RISING
))
388 writel_relaxed(mask
, (trigger
& IRQ_TYPE_EDGE_FALLING
)
389 ? &g
->set_falling
: &g
->clr_falling
);
390 writel_relaxed(mask
, (trigger
& IRQ_TYPE_EDGE_RISING
)
391 ? &g
->set_rising
: &g
->clr_rising
);
397 davinci_gpio_irq_map(struct irq_domain
*d
, unsigned int irq
,
400 struct davinci_gpio_controller
*chips
=
401 (struct davinci_gpio_controller
*)d
->host_data
;
402 struct davinci_gpio_regs __iomem
*g
= chips
->regs
[hw
/ 32];
404 irq_set_chip_and_handler_name(irq
, &gpio_irqchip
, handle_simple_irq
,
406 irq_set_irq_type(irq
, IRQ_TYPE_NONE
);
407 irq_set_chip_data(irq
, (__force
void *)g
);
408 irq_set_handler_data(irq
, (void *)(uintptr_t)__gpio_mask(hw
));
413 static const struct irq_domain_ops davinci_gpio_irq_ops
= {
414 .map
= davinci_gpio_irq_map
,
415 .xlate
= irq_domain_xlate_onetwocell
,
418 static struct irq_chip
*davinci_gpio_get_irq_chip(unsigned int irq
)
420 static struct irq_chip_type gpio_unbanked
;
422 gpio_unbanked
= *irq_data_get_chip_type(irq_get_irq_data(irq
));
424 return &gpio_unbanked
.chip
;
427 static struct irq_chip
*keystone_gpio_get_irq_chip(unsigned int irq
)
429 static struct irq_chip gpio_unbanked
;
431 gpio_unbanked
= *irq_get_chip(irq
);
432 return &gpio_unbanked
;
435 static const struct of_device_id davinci_gpio_ids
[];
438 * NOTE: for suspend/resume, probably best to make a platform_device with
439 * suspend_late/resume_resume calls hooking into results of the set_wake()
440 * calls ... so if no gpios are wakeup events the clock can be disabled,
441 * with outputs left at previously set levels, and so that VDD3P3V.IOPWDN0
442 * (dm6446) can be set appropriately for GPIOV33 pins.
445 static int davinci_gpio_irq_setup(struct platform_device
*pdev
)
452 struct device
*dev
= &pdev
->dev
;
453 struct davinci_gpio_controller
*chips
= platform_get_drvdata(pdev
);
454 struct davinci_gpio_regs __iomem
*g
;
455 struct irq_domain
*irq_domain
= NULL
;
456 struct irq_chip
*irq_chip
;
457 struct davinci_gpio_irq_data
*irqdata
;
458 gpio_get_irq_chip_cb_t gpio_get_irq_chip
;
461 * Use davinci_gpio_get_irq_chip by default to handle non DT cases
463 gpio_get_irq_chip
= davinci_gpio_get_irq_chip
;
465 gpio_get_irq_chip
= (gpio_get_irq_chip_cb_t
)device_get_match_data(dev
);
467 ngpio
= chips
->chip
.ngpio
;
469 clk
= devm_clk_get_enabled(dev
, "gpio");
471 dev_err(dev
, "Error %ld getting gpio clock\n", PTR_ERR(clk
));
475 if (!chips
->gpio_unbanked
) {
476 irq
= devm_irq_alloc_descs(dev
, -1, 0, ngpio
, 0);
478 dev_err(dev
, "Couldn't allocate IRQ numbers\n");
482 irq_domain
= irq_domain_add_legacy(dev
->of_node
, ngpio
, irq
, 0,
483 &davinci_gpio_irq_ops
,
486 dev_err(dev
, "Couldn't register an IRQ domain\n");
492 * Arrange gpiod_to_irq() support, handling either direct IRQs or
493 * banked IRQs. Having GPIOs in the first GPIO bank use direct
494 * IRQs, while the others use banked IRQs, would need some setup
495 * tweaks to recognize hardware which can do that.
497 chips
->chip
.to_irq
= gpio_to_irq_banked
;
498 chips
->irq_domain
= irq_domain
;
501 * AINTC can handle direct/unbanked IRQs for GPIOs, with the GPIO
502 * controller only handling trigger modes. We currently assume no
503 * IRQ mux conflicts; gpio_irq_type_unbanked() is only for GPIOs.
505 if (chips
->gpio_unbanked
) {
506 /* pass "bank 0" GPIO IRQs to AINTC */
507 chips
->chip
.to_irq
= gpio_to_irq_unbanked
;
509 binten
= GENMASK(chips
->gpio_unbanked
/ 16, 0);
511 /* AINTC handles mask/unmask; GPIO handles triggering */
512 irq
= chips
->irqs
[0];
513 irq_chip
= gpio_get_irq_chip(irq
);
514 irq_chip
->name
= "GPIO-AINTC";
515 irq_chip
->irq_set_type
= gpio_irq_type_unbanked
;
517 /* default trigger: both edges */
519 writel_relaxed(~0, &g
->set_falling
);
520 writel_relaxed(~0, &g
->set_rising
);
522 /* set the direct IRQs up to use that irqchip */
523 for (gpio
= 0; gpio
< chips
->gpio_unbanked
; gpio
++) {
524 irq_set_chip(chips
->irqs
[gpio
], irq_chip
);
525 irq_set_handler_data(chips
->irqs
[gpio
], chips
);
526 irq_set_status_flags(chips
->irqs
[gpio
],
534 * Or, AINTC can handle IRQs for banks of 16 GPIO IRQs, which we
535 * then chain through our own handler.
537 for (gpio
= 0, bank
= 0; gpio
< ngpio
; bank
++, gpio
+= 16) {
538 /* disabled by default, enabled only as needed
539 * There are register sets for 32 GPIOs. 2 banks of 16
540 * GPIOs are covered by each set of registers hence divide by 2
542 g
= chips
->regs
[bank
/ 2];
543 writel_relaxed(~0, &g
->clr_falling
);
544 writel_relaxed(~0, &g
->clr_rising
);
547 * Each chip handles 32 gpios, and each irq bank consists of 16
548 * gpio irqs. Pass the irq bank's corresponding controller to
549 * the chained irq handler.
551 irqdata
= devm_kzalloc(&pdev
->dev
,
553 davinci_gpio_irq_data
),
559 irqdata
->bank_num
= bank
;
560 irqdata
->chip
= chips
;
562 irq_set_chained_handler_and_data(chips
->irqs
[bank
],
563 gpio_irq_handler
, irqdata
);
570 * BINTEN -- per-bank interrupt enable. genirq would also let these
571 * bits be set/cleared dynamically.
573 writel_relaxed(binten
, gpio_base
+ BINTEN
);
578 static void davinci_gpio_save_context(struct davinci_gpio_controller
*chips
,
581 struct davinci_gpio_regs __iomem
*g
;
582 struct davinci_gpio_regs
*context
;
586 base
= chips
->regs
[0] - offset_array
[0];
587 chips
->binten_context
= readl_relaxed(base
+ BINTEN
);
589 for (bank
= 0; bank
< nbank
; bank
++) {
590 g
= chips
->regs
[bank
];
591 context
= &chips
->context
[bank
];
592 context
->dir
= readl_relaxed(&g
->dir
);
593 context
->set_data
= readl_relaxed(&g
->set_data
);
594 context
->set_rising
= readl_relaxed(&g
->set_rising
);
595 context
->set_falling
= readl_relaxed(&g
->set_falling
);
598 /* Clear all interrupt status registers */
599 writel_relaxed(GENMASK(31, 0), &g
->intstat
);
602 static void davinci_gpio_restore_context(struct davinci_gpio_controller
*chips
,
605 struct davinci_gpio_regs __iomem
*g
;
606 struct davinci_gpio_regs
*context
;
610 base
= chips
->regs
[0] - offset_array
[0];
612 if (readl_relaxed(base
+ BINTEN
) != chips
->binten_context
)
613 writel_relaxed(chips
->binten_context
, base
+ BINTEN
);
615 for (bank
= 0; bank
< nbank
; bank
++) {
616 g
= chips
->regs
[bank
];
617 context
= &chips
->context
[bank
];
618 if (readl_relaxed(&g
->dir
) != context
->dir
)
619 writel_relaxed(context
->dir
, &g
->dir
);
620 if (readl_relaxed(&g
->set_data
) != context
->set_data
)
621 writel_relaxed(context
->set_data
, &g
->set_data
);
622 if (readl_relaxed(&g
->set_rising
) != context
->set_rising
)
623 writel_relaxed(context
->set_rising
, &g
->set_rising
);
624 if (readl_relaxed(&g
->set_falling
) != context
->set_falling
)
625 writel_relaxed(context
->set_falling
, &g
->set_falling
);
629 static int davinci_gpio_suspend(struct device
*dev
)
631 struct davinci_gpio_controller
*chips
= dev_get_drvdata(dev
);
632 u32 nbank
= DIV_ROUND_UP(chips
->chip
.ngpio
, 32);
634 davinci_gpio_save_context(chips
, nbank
);
639 static int davinci_gpio_resume(struct device
*dev
)
641 struct davinci_gpio_controller
*chips
= dev_get_drvdata(dev
);
642 u32 nbank
= DIV_ROUND_UP(chips
->chip
.ngpio
, 32);
644 davinci_gpio_restore_context(chips
, nbank
);
649 static DEFINE_SIMPLE_DEV_PM_OPS(davinci_gpio_dev_pm_ops
, davinci_gpio_suspend
,
650 davinci_gpio_resume
);
652 static const struct of_device_id davinci_gpio_ids
[] = {
653 { .compatible
= "ti,keystone-gpio", keystone_gpio_get_irq_chip
},
654 { .compatible
= "ti,am654-gpio", keystone_gpio_get_irq_chip
},
655 { .compatible
= "ti,dm6441-gpio", davinci_gpio_get_irq_chip
},
658 MODULE_DEVICE_TABLE(of
, davinci_gpio_ids
);
660 static struct platform_driver davinci_gpio_driver
= {
661 .probe
= davinci_gpio_probe
,
663 .name
= "davinci_gpio",
664 .pm
= pm_sleep_ptr(&davinci_gpio_dev_pm_ops
),
665 .of_match_table
= of_match_ptr(davinci_gpio_ids
),
670 * GPIO driver registration needs to be done before machine_init functions
671 * access GPIO. Hence davinci_gpio_drv_reg() is a postcore_initcall.
673 static int __init
davinci_gpio_drv_reg(void)
675 return platform_driver_register(&davinci_gpio_driver
);
677 postcore_initcall(davinci_gpio_drv_reg
);
679 static void __exit
davinci_gpio_exit(void)
681 platform_driver_unregister(&davinci_gpio_driver
);
683 module_exit(davinci_gpio_exit
);
685 MODULE_AUTHOR("Jan Kotas <jank@cadence.com>");
686 MODULE_DESCRIPTION("DAVINCI GPIO driver");
687 MODULE_LICENSE("GPL");
688 MODULE_ALIAS("platform:gpio-davinci");