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/of_device.h>
20 #include <linux/pinctrl/consumer.h>
21 #include <linux/platform_device.h>
22 #include <linux/platform_data/gpio-davinci.h>
23 #include <linux/irqchip/chained_irq.h>
24 #include <linux/spinlock.h>
25 #include <linux/pm_runtime.h>
27 #include <asm-generic/gpio.h>
29 #define MAX_REGS_BANKS 5
30 #define MAX_INT_PER_BANK 32
32 struct davinci_gpio_regs {
45 typedef struct irq_chip *(*gpio_get_irq_chip_cb_t)(unsigned int irq);
47 #define BINTEN 0x8 /* GPIO Interrupt Per-Bank Enable Register */
49 static void __iomem *gpio_base;
50 static unsigned int offset_array[5] = {0x10, 0x38, 0x60, 0x88, 0xb0};
52 struct davinci_gpio_irq_data {
54 struct davinci_gpio_controller *chip;
58 struct davinci_gpio_controller {
59 struct gpio_chip chip;
60 struct irq_domain *irq_domain;
61 /* Serialize access to GPIO registers */
63 void __iomem *regs[MAX_REGS_BANKS];
65 int irqs[MAX_INT_PER_BANK];
66 struct davinci_gpio_regs context[MAX_REGS_BANKS];
70 static inline u32 __gpio_mask(unsigned gpio)
72 return 1 << (gpio % 32);
75 static inline struct davinci_gpio_regs __iomem *irq2regs(struct irq_data *d)
77 struct davinci_gpio_regs __iomem *g;
79 g = (__force struct davinci_gpio_regs __iomem *)irq_data_get_irq_chip_data(d);
84 static int davinci_gpio_irq_setup(struct platform_device *pdev);
86 /*--------------------------------------------------------------------------*/
88 /* board setup code *MUST* setup pinmux and enable the GPIO clock. */
89 static inline int __davinci_direction(struct gpio_chip *chip,
90 unsigned offset, bool out, int value)
92 struct davinci_gpio_controller *d = gpiochip_get_data(chip);
93 struct davinci_gpio_regs __iomem *g;
96 int bank = offset / 32;
97 u32 mask = __gpio_mask(offset);
100 spin_lock_irqsave(&d->lock, flags);
101 temp = readl_relaxed(&g->dir);
104 writel_relaxed(mask, value ? &g->set_data : &g->clr_data);
108 writel_relaxed(temp, &g->dir);
109 spin_unlock_irqrestore(&d->lock, flags);
114 static int davinci_direction_in(struct gpio_chip *chip, unsigned offset)
116 return __davinci_direction(chip, offset, false, 0);
120 davinci_direction_out(struct gpio_chip *chip, unsigned offset, int value)
122 return __davinci_direction(chip, offset, true, value);
126 * Read the pin's value (works even if it's set up as output);
127 * returns zero/nonzero.
129 * Note that changes are synched to the GPIO clock, so reading values back
130 * right after you've set them may give old values.
132 static int davinci_gpio_get(struct gpio_chip *chip, unsigned offset)
134 struct davinci_gpio_controller *d = gpiochip_get_data(chip);
135 struct davinci_gpio_regs __iomem *g;
136 int bank = offset / 32;
140 return !!(__gpio_mask(offset) & readl_relaxed(&g->in_data));
144 * Assuming the pin is muxed as a gpio output, set its output value.
147 davinci_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
149 struct davinci_gpio_controller *d = gpiochip_get_data(chip);
150 struct davinci_gpio_regs __iomem *g;
151 int bank = offset / 32;
155 writel_relaxed(__gpio_mask(offset),
156 value ? &g->set_data : &g->clr_data);
159 static struct davinci_gpio_platform_data *
160 davinci_gpio_get_pdata(struct platform_device *pdev)
162 struct device_node *dn = pdev->dev.of_node;
163 struct davinci_gpio_platform_data *pdata;
167 if (!IS_ENABLED(CONFIG_OF) || !pdev->dev.of_node)
168 return dev_get_platdata(&pdev->dev);
170 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
174 ret = of_property_read_u32(dn, "ti,ngpio", &val);
180 ret = of_property_read_u32(dn, "ti,davinci-gpio-unbanked", &val);
184 pdata->gpio_unbanked = val;
189 dev_err(&pdev->dev, "Populating pdata from DT failed: err %d\n", ret);
193 static int davinci_gpio_probe(struct platform_device *pdev)
195 int bank, i, ret = 0;
196 unsigned int ngpio, nbank, nirq;
197 struct davinci_gpio_controller *chips;
198 struct davinci_gpio_platform_data *pdata;
199 struct device *dev = &pdev->dev;
201 pdata = davinci_gpio_get_pdata(pdev);
203 dev_err(dev, "No platform data found\n");
207 dev->platform_data = pdata;
210 * The gpio banks conceptually expose a segmented bitmap,
211 * and "ngpio" is one more than the largest zero-based
212 * bit index that's valid.
214 ngpio = pdata->ngpio;
216 dev_err(dev, "How many GPIOs?\n");
220 if (WARN_ON(ARCH_NR_GPIOS < ngpio))
221 ngpio = ARCH_NR_GPIOS;
224 * If there are unbanked interrupts then the number of
225 * interrupts is equal to number of gpios else all are banked so
226 * number of interrupts is equal to number of banks(each with 16 gpios)
228 if (pdata->gpio_unbanked)
229 nirq = pdata->gpio_unbanked;
231 nirq = DIV_ROUND_UP(ngpio, 16);
233 chips = devm_kzalloc(dev, sizeof(*chips), GFP_KERNEL);
237 gpio_base = devm_platform_ioremap_resource(pdev, 0);
238 if (IS_ERR(gpio_base))
239 return PTR_ERR(gpio_base);
241 for (i = 0; i < nirq; i++) {
242 chips->irqs[i] = platform_get_irq(pdev, i);
243 if (chips->irqs[i] < 0)
244 return dev_err_probe(dev, chips->irqs[i], "IRQ not populated\n");
247 chips->chip.label = dev_name(dev);
249 chips->chip.direction_input = davinci_direction_in;
250 chips->chip.get = davinci_gpio_get;
251 chips->chip.direction_output = davinci_direction_out;
252 chips->chip.set = davinci_gpio_set;
254 chips->chip.ngpio = ngpio;
255 chips->chip.base = pdata->no_auto_base ? pdata->base : -1;
257 #ifdef CONFIG_OF_GPIO
258 chips->chip.of_gpio_n_cells = 2;
259 chips->chip.parent = dev;
260 chips->chip.request = gpiochip_generic_request;
261 chips->chip.free = gpiochip_generic_free;
263 spin_lock_init(&chips->lock);
265 nbank = DIV_ROUND_UP(ngpio, 32);
266 for (bank = 0; bank < nbank; bank++)
267 chips->regs[bank] = gpio_base + offset_array[bank];
269 ret = devm_gpiochip_add_data(dev, &chips->chip, chips);
273 platform_set_drvdata(pdev, chips);
274 ret = davinci_gpio_irq_setup(pdev);
281 /*--------------------------------------------------------------------------*/
283 * We expect irqs will normally be set up as input pins, but they can also be
284 * used as output pins ... which is convenient for testing.
286 * NOTE: The first few GPIOs also have direct INTC hookups in addition
287 * to their GPIOBNK0 irq, with a bit less overhead.
289 * All those INTC hookups (direct, plus several IRQ banks) can also
290 * serve as EDMA event triggers.
293 static void gpio_irq_disable(struct irq_data *d)
295 struct davinci_gpio_regs __iomem *g = irq2regs(d);
296 uintptr_t mask = (uintptr_t)irq_data_get_irq_handler_data(d);
298 writel_relaxed(mask, &g->clr_falling);
299 writel_relaxed(mask, &g->clr_rising);
302 static void gpio_irq_enable(struct irq_data *d)
304 struct davinci_gpio_regs __iomem *g = irq2regs(d);
305 uintptr_t mask = (uintptr_t)irq_data_get_irq_handler_data(d);
306 unsigned status = irqd_get_trigger_type(d);
308 status &= IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING;
310 status = IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING;
312 if (status & IRQ_TYPE_EDGE_FALLING)
313 writel_relaxed(mask, &g->set_falling);
314 if (status & IRQ_TYPE_EDGE_RISING)
315 writel_relaxed(mask, &g->set_rising);
318 static int gpio_irq_type(struct irq_data *d, unsigned trigger)
320 if (trigger & ~(IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
326 static struct irq_chip gpio_irqchip = {
328 .irq_enable = gpio_irq_enable,
329 .irq_disable = gpio_irq_disable,
330 .irq_set_type = gpio_irq_type,
331 .flags = IRQCHIP_SET_TYPE_MASKED,
334 static void gpio_irq_handler(struct irq_desc *desc)
336 struct davinci_gpio_regs __iomem *g;
339 struct davinci_gpio_controller *d;
340 struct davinci_gpio_irq_data *irqdata;
342 irqdata = (struct davinci_gpio_irq_data *)irq_desc_get_handler_data(desc);
343 bank_num = irqdata->bank_num;
347 /* we only care about one bank */
348 if ((bank_num % 2) == 1)
351 /* temporarily mask (level sensitive) parent IRQ */
352 chained_irq_enter(irq_desc_get_chip(desc), desc);
356 irq_hw_number_t hw_irq;
359 status = readl_relaxed(&g->intstat) & mask;
362 writel_relaxed(status, &g->intstat);
364 /* now demux them to the right lowlevel handler */
369 /* Max number of gpios per controller is 144 so
370 * hw_irq will be in [0..143]
372 hw_irq = (bank_num / 2) * 32 + bit;
374 generic_handle_domain_irq(d->irq_domain, hw_irq);
377 chained_irq_exit(irq_desc_get_chip(desc), desc);
378 /* now it may re-trigger */
381 static int gpio_to_irq_banked(struct gpio_chip *chip, unsigned offset)
383 struct davinci_gpio_controller *d = gpiochip_get_data(chip);
386 return irq_create_mapping(d->irq_domain, offset);
391 static int gpio_to_irq_unbanked(struct gpio_chip *chip, unsigned offset)
393 struct davinci_gpio_controller *d = gpiochip_get_data(chip);
396 * NOTE: we assume for now that only irqs in the first gpio_chip
397 * can provide direct-mapped IRQs to AINTC (up to 32 GPIOs).
399 if (offset < d->gpio_unbanked)
400 return d->irqs[offset];
405 static int gpio_irq_type_unbanked(struct irq_data *data, unsigned trigger)
407 struct davinci_gpio_controller *d;
408 struct davinci_gpio_regs __iomem *g;
411 d = (struct davinci_gpio_controller *)irq_data_get_irq_handler_data(data);
412 g = (struct davinci_gpio_regs __iomem *)d->regs[0];
413 for (i = 0; i < MAX_INT_PER_BANK; i++)
414 if (data->irq == d->irqs[i])
417 if (i == MAX_INT_PER_BANK)
420 mask = __gpio_mask(i);
422 if (trigger & ~(IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
425 writel_relaxed(mask, (trigger & IRQ_TYPE_EDGE_FALLING)
426 ? &g->set_falling : &g->clr_falling);
427 writel_relaxed(mask, (trigger & IRQ_TYPE_EDGE_RISING)
428 ? &g->set_rising : &g->clr_rising);
434 davinci_gpio_irq_map(struct irq_domain *d, unsigned int irq,
437 struct davinci_gpio_controller *chips =
438 (struct davinci_gpio_controller *)d->host_data;
439 struct davinci_gpio_regs __iomem *g = chips->regs[hw / 32];
441 irq_set_chip_and_handler_name(irq, &gpio_irqchip, handle_simple_irq,
443 irq_set_irq_type(irq, IRQ_TYPE_NONE);
444 irq_set_chip_data(irq, (__force void *)g);
445 irq_set_handler_data(irq, (void *)(uintptr_t)__gpio_mask(hw));
450 static const struct irq_domain_ops davinci_gpio_irq_ops = {
451 .map = davinci_gpio_irq_map,
452 .xlate = irq_domain_xlate_onetwocell,
455 static struct irq_chip *davinci_gpio_get_irq_chip(unsigned int irq)
457 static struct irq_chip_type gpio_unbanked;
459 gpio_unbanked = *irq_data_get_chip_type(irq_get_irq_data(irq));
461 return &gpio_unbanked.chip;
464 static struct irq_chip *keystone_gpio_get_irq_chip(unsigned int irq)
466 static struct irq_chip gpio_unbanked;
468 gpio_unbanked = *irq_get_chip(irq);
469 return &gpio_unbanked;
472 static const struct of_device_id davinci_gpio_ids[];
475 * NOTE: for suspend/resume, probably best to make a platform_device with
476 * suspend_late/resume_resume calls hooking into results of the set_wake()
477 * calls ... so if no gpios are wakeup events the clock can be disabled,
478 * with outputs left at previously set levels, and so that VDD3P3V.IOPWDN0
479 * (dm6446) can be set appropriately for GPIOV33 pins.
482 static int davinci_gpio_irq_setup(struct platform_device *pdev)
490 struct device *dev = &pdev->dev;
491 struct davinci_gpio_controller *chips = platform_get_drvdata(pdev);
492 struct davinci_gpio_platform_data *pdata = dev->platform_data;
493 struct davinci_gpio_regs __iomem *g;
494 struct irq_domain *irq_domain = NULL;
495 const struct of_device_id *match;
496 struct irq_chip *irq_chip;
497 struct davinci_gpio_irq_data *irqdata;
498 gpio_get_irq_chip_cb_t gpio_get_irq_chip;
501 * Use davinci_gpio_get_irq_chip by default to handle non DT cases
503 gpio_get_irq_chip = davinci_gpio_get_irq_chip;
504 match = of_match_device(of_match_ptr(davinci_gpio_ids),
507 gpio_get_irq_chip = (gpio_get_irq_chip_cb_t)match->data;
509 ngpio = pdata->ngpio;
511 clk = devm_clk_get(dev, "gpio");
513 dev_err(dev, "Error %ld getting gpio clock\n", PTR_ERR(clk));
517 ret = clk_prepare_enable(clk);
521 if (!pdata->gpio_unbanked) {
522 irq = devm_irq_alloc_descs(dev, -1, 0, ngpio, 0);
524 dev_err(dev, "Couldn't allocate IRQ numbers\n");
525 clk_disable_unprepare(clk);
529 irq_domain = irq_domain_add_legacy(dev->of_node, ngpio, irq, 0,
530 &davinci_gpio_irq_ops,
533 dev_err(dev, "Couldn't register an IRQ domain\n");
534 clk_disable_unprepare(clk);
540 * Arrange gpio_to_irq() support, handling either direct IRQs or
541 * banked IRQs. Having GPIOs in the first GPIO bank use direct
542 * IRQs, while the others use banked IRQs, would need some setup
543 * tweaks to recognize hardware which can do that.
545 chips->chip.to_irq = gpio_to_irq_banked;
546 chips->irq_domain = irq_domain;
549 * AINTC can handle direct/unbanked IRQs for GPIOs, with the GPIO
550 * controller only handling trigger modes. We currently assume no
551 * IRQ mux conflicts; gpio_irq_type_unbanked() is only for GPIOs.
553 if (pdata->gpio_unbanked) {
554 /* pass "bank 0" GPIO IRQs to AINTC */
555 chips->chip.to_irq = gpio_to_irq_unbanked;
556 chips->gpio_unbanked = pdata->gpio_unbanked;
557 binten = GENMASK(pdata->gpio_unbanked / 16, 0);
559 /* AINTC handles mask/unmask; GPIO handles triggering */
560 irq = chips->irqs[0];
561 irq_chip = gpio_get_irq_chip(irq);
562 irq_chip->name = "GPIO-AINTC";
563 irq_chip->irq_set_type = gpio_irq_type_unbanked;
565 /* default trigger: both edges */
567 writel_relaxed(~0, &g->set_falling);
568 writel_relaxed(~0, &g->set_rising);
570 /* set the direct IRQs up to use that irqchip */
571 for (gpio = 0; gpio < pdata->gpio_unbanked; gpio++) {
572 irq_set_chip(chips->irqs[gpio], irq_chip);
573 irq_set_handler_data(chips->irqs[gpio], chips);
574 irq_set_status_flags(chips->irqs[gpio],
582 * Or, AINTC can handle IRQs for banks of 16 GPIO IRQs, which we
583 * then chain through our own handler.
585 for (gpio = 0, bank = 0; gpio < ngpio; bank++, gpio += 16) {
586 /* disabled by default, enabled only as needed
587 * There are register sets for 32 GPIOs. 2 banks of 16
588 * GPIOs are covered by each set of registers hence divide by 2
590 g = chips->regs[bank / 2];
591 writel_relaxed(~0, &g->clr_falling);
592 writel_relaxed(~0, &g->clr_rising);
595 * Each chip handles 32 gpios, and each irq bank consists of 16
596 * gpio irqs. Pass the irq bank's corresponding controller to
597 * the chained irq handler.
599 irqdata = devm_kzalloc(&pdev->dev,
601 davinci_gpio_irq_data),
604 clk_disable_unprepare(clk);
609 irqdata->bank_num = bank;
610 irqdata->chip = chips;
612 irq_set_chained_handler_and_data(chips->irqs[bank],
613 gpio_irq_handler, irqdata);
620 * BINTEN -- per-bank interrupt enable. genirq would also let these
621 * bits be set/cleared dynamically.
623 writel_relaxed(binten, gpio_base + BINTEN);
628 static void davinci_gpio_save_context(struct davinci_gpio_controller *chips,
631 struct davinci_gpio_regs __iomem *g;
632 struct davinci_gpio_regs *context;
636 base = chips->regs[0] - offset_array[0];
637 chips->binten_context = readl_relaxed(base + BINTEN);
639 for (bank = 0; bank < nbank; bank++) {
640 g = chips->regs[bank];
641 context = &chips->context[bank];
642 context->dir = readl_relaxed(&g->dir);
643 context->set_data = readl_relaxed(&g->set_data);
644 context->set_rising = readl_relaxed(&g->set_rising);
645 context->set_falling = readl_relaxed(&g->set_falling);
648 /* Clear Bank interrupt enable bit */
649 writel_relaxed(0, base + BINTEN);
651 /* Clear all interrupt status registers */
652 writel_relaxed(GENMASK(31, 0), &g->intstat);
655 static void davinci_gpio_restore_context(struct davinci_gpio_controller *chips,
658 struct davinci_gpio_regs __iomem *g;
659 struct davinci_gpio_regs *context;
663 base = chips->regs[0] - offset_array[0];
665 if (readl_relaxed(base + BINTEN) != chips->binten_context)
666 writel_relaxed(chips->binten_context, base + BINTEN);
668 for (bank = 0; bank < nbank; bank++) {
669 g = chips->regs[bank];
670 context = &chips->context[bank];
671 if (readl_relaxed(&g->dir) != context->dir)
672 writel_relaxed(context->dir, &g->dir);
673 if (readl_relaxed(&g->set_data) != context->set_data)
674 writel_relaxed(context->set_data, &g->set_data);
675 if (readl_relaxed(&g->set_rising) != context->set_rising)
676 writel_relaxed(context->set_rising, &g->set_rising);
677 if (readl_relaxed(&g->set_falling) != context->set_falling)
678 writel_relaxed(context->set_falling, &g->set_falling);
682 static int davinci_gpio_suspend(struct device *dev)
684 struct davinci_gpio_controller *chips = dev_get_drvdata(dev);
685 struct davinci_gpio_platform_data *pdata = dev_get_platdata(dev);
686 u32 nbank = DIV_ROUND_UP(pdata->ngpio, 32);
688 davinci_gpio_save_context(chips, nbank);
693 static int davinci_gpio_resume(struct device *dev)
695 struct davinci_gpio_controller *chips = dev_get_drvdata(dev);
696 struct davinci_gpio_platform_data *pdata = dev_get_platdata(dev);
697 u32 nbank = DIV_ROUND_UP(pdata->ngpio, 32);
699 davinci_gpio_restore_context(chips, nbank);
704 DEFINE_SIMPLE_DEV_PM_OPS(davinci_gpio_dev_pm_ops, davinci_gpio_suspend,
705 davinci_gpio_resume);
707 static const struct of_device_id davinci_gpio_ids[] = {
708 { .compatible = "ti,keystone-gpio", keystone_gpio_get_irq_chip},
709 { .compatible = "ti,am654-gpio", keystone_gpio_get_irq_chip},
710 { .compatible = "ti,dm6441-gpio", davinci_gpio_get_irq_chip},
713 MODULE_DEVICE_TABLE(of, davinci_gpio_ids);
715 static struct platform_driver davinci_gpio_driver = {
716 .probe = davinci_gpio_probe,
718 .name = "davinci_gpio",
719 .pm = pm_sleep_ptr(&davinci_gpio_dev_pm_ops),
720 .of_match_table = of_match_ptr(davinci_gpio_ids),
725 * GPIO driver registration needs to be done before machine_init functions
726 * access GPIO. Hence davinci_gpio_drv_reg() is a postcore_initcall.
728 static int __init davinci_gpio_drv_reg(void)
730 return platform_driver_register(&davinci_gpio_driver);
732 postcore_initcall(davinci_gpio_drv_reg);