Lines Matching +full:- +full:gpio +full:- +full:bank

1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright (C) 2015-2017 Broadcom
5 #include <linux/gpio/driver.h>
26 #define GIO_BANK_OFF(bank, off) (((bank) * GIO_BANK_SIZE) + (off * sizeof(u32))) argument
27 #define GIO_ODEN(bank) GIO_BANK_OFF(bank, GIO_REG_ODEN) argument
28 #define GIO_DATA(bank) GIO_BANK_OFF(bank, GIO_REG_DATA) argument
29 #define GIO_IODIR(bank) GIO_BANK_OFF(bank, GIO_REG_IODIR) argument
30 #define GIO_EC(bank) GIO_BANK_OFF(bank, GIO_REG_EC) argument
31 #define GIO_EI(bank) GIO_BANK_OFF(bank, GIO_REG_EI) argument
32 #define GIO_MASK(bank) GIO_BANK_OFF(bank, GIO_REG_MASK) argument
33 #define GIO_LEVEL(bank) GIO_BANK_OFF(bank, GIO_REG_LEVEL) argument
34 #define GIO_STAT(bank) GIO_BANK_OFF(bank, GIO_REG_STAT) argument
59 #define GPIO_BANK(gpio) ((gpio) >> 5) argument
61 #define GPIO_BIT(gpio) ((gpio) & (MAX_GPIO_PER_BANK - 1)) argument
66 struct brcmstb_gpio_bank *bank = gpiochip_get_data(gc); in brcmstb_gpio_gc_to_priv() local
67 return bank->parent_priv; in brcmstb_gpio_gc_to_priv()
71 __brcmstb_gpio_get_active_irqs(struct brcmstb_gpio_bank *bank) in __brcmstb_gpio_get_active_irqs() argument
73 void __iomem *reg_base = bank->parent_priv->reg_base; in __brcmstb_gpio_get_active_irqs()
75 return bank->gc.read_reg(reg_base + GIO_STAT(bank->id)) & in __brcmstb_gpio_get_active_irqs()
76 bank->gc.read_reg(reg_base + GIO_MASK(bank->id)); in __brcmstb_gpio_get_active_irqs()
80 brcmstb_gpio_get_active_irqs(struct brcmstb_gpio_bank *bank) in brcmstb_gpio_get_active_irqs() argument
85 raw_spin_lock_irqsave(&bank->gc.bgpio_lock, flags); in brcmstb_gpio_get_active_irqs()
86 status = __brcmstb_gpio_get_active_irqs(bank); in brcmstb_gpio_get_active_irqs()
87 raw_spin_unlock_irqrestore(&bank->gc.bgpio_lock, flags); in brcmstb_gpio_get_active_irqs()
93 struct brcmstb_gpio_bank *bank) in brcmstb_gpio_hwirq_to_offset() argument
95 return hwirq - (bank->gc.base - bank->parent_priv->gpio_base); in brcmstb_gpio_hwirq_to_offset()
98 static void brcmstb_gpio_set_imask(struct brcmstb_gpio_bank *bank, in brcmstb_gpio_set_imask() argument
101 struct gpio_chip *gc = &bank->gc; in brcmstb_gpio_set_imask()
102 struct brcmstb_gpio_priv *priv = bank->parent_priv; in brcmstb_gpio_set_imask()
103 u32 mask = BIT(brcmstb_gpio_hwirq_to_offset(hwirq, bank)); in brcmstb_gpio_set_imask()
107 raw_spin_lock_irqsave(&gc->bgpio_lock, flags); in brcmstb_gpio_set_imask()
108 imask = gc->read_reg(priv->reg_base + GIO_MASK(bank->id)); in brcmstb_gpio_set_imask()
113 gc->write_reg(priv->reg_base + GIO_MASK(bank->id), imask); in brcmstb_gpio_set_imask()
114 raw_spin_unlock_irqrestore(&gc->bgpio_lock, flags); in brcmstb_gpio_set_imask()
121 int hwirq = offset + (gc->base - priv->gpio_base); in brcmstb_gpio_to_irq()
123 if (hwirq >= priv->num_gpios) in brcmstb_gpio_to_irq()
124 return -ENXIO; in brcmstb_gpio_to_irq()
125 return irq_create_mapping(priv->irq_domain, hwirq); in brcmstb_gpio_to_irq()
128 /* -------------------- IRQ chip functions -------------------- */
133 struct brcmstb_gpio_bank *bank = gpiochip_get_data(gc); in brcmstb_gpio_irq_mask() local
135 brcmstb_gpio_set_imask(bank, d->hwirq, false); in brcmstb_gpio_irq_mask()
141 struct brcmstb_gpio_bank *bank = gpiochip_get_data(gc); in brcmstb_gpio_irq_unmask() local
143 brcmstb_gpio_set_imask(bank, d->hwirq, true); in brcmstb_gpio_irq_unmask()
149 struct brcmstb_gpio_bank *bank = gpiochip_get_data(gc); in brcmstb_gpio_irq_ack() local
150 struct brcmstb_gpio_priv *priv = bank->parent_priv; in brcmstb_gpio_irq_ack()
151 u32 mask = BIT(brcmstb_gpio_hwirq_to_offset(d->hwirq, bank)); in brcmstb_gpio_irq_ack()
153 gc->write_reg(priv->reg_base + GIO_STAT(bank->id), mask); in brcmstb_gpio_irq_ack()
159 struct brcmstb_gpio_bank *bank = gpiochip_get_data(gc); in brcmstb_gpio_irq_set_type() local
160 struct brcmstb_gpio_priv *priv = bank->parent_priv; in brcmstb_gpio_irq_set_type()
161 u32 mask = BIT(brcmstb_gpio_hwirq_to_offset(d->hwirq, bank)); in brcmstb_gpio_irq_set_type()
194 return -EINVAL; in brcmstb_gpio_irq_set_type()
197 raw_spin_lock_irqsave(&bank->gc.bgpio_lock, flags); in brcmstb_gpio_irq_set_type()
199 iedge_config = bank->gc.read_reg(priv->reg_base + in brcmstb_gpio_irq_set_type()
200 GIO_EC(bank->id)) & ~mask; in brcmstb_gpio_irq_set_type()
201 iedge_insensitive = bank->gc.read_reg(priv->reg_base + in brcmstb_gpio_irq_set_type()
202 GIO_EI(bank->id)) & ~mask; in brcmstb_gpio_irq_set_type()
203 ilevel = bank->gc.read_reg(priv->reg_base + in brcmstb_gpio_irq_set_type()
204 GIO_LEVEL(bank->id)) & ~mask; in brcmstb_gpio_irq_set_type()
206 bank->gc.write_reg(priv->reg_base + GIO_EC(bank->id), in brcmstb_gpio_irq_set_type()
208 bank->gc.write_reg(priv->reg_base + GIO_EI(bank->id), in brcmstb_gpio_irq_set_type()
210 bank->gc.write_reg(priv->reg_base + GIO_LEVEL(bank->id), in brcmstb_gpio_irq_set_type()
213 raw_spin_unlock_irqrestore(&bank->gc.bgpio_lock, flags); in brcmstb_gpio_irq_set_type()
223 ret = enable_irq_wake(priv->parent_wake_irq); in brcmstb_gpio_priv_set_wake()
225 ret = disable_irq_wake(priv->parent_wake_irq); in brcmstb_gpio_priv_set_wake()
227 dev_err(&priv->pdev->dev, "failed to %s wake-up interrupt\n", in brcmstb_gpio_priv_set_wake()
235 struct brcmstb_gpio_bank *bank = gpiochip_get_data(gc); in brcmstb_gpio_irq_set_wake() local
236 struct brcmstb_gpio_priv *priv = bank->parent_priv; in brcmstb_gpio_irq_set_wake()
237 u32 mask = BIT(brcmstb_gpio_hwirq_to_offset(d->hwirq, bank)); in brcmstb_gpio_irq_set_wake()
244 bank->wake_active |= mask; in brcmstb_gpio_irq_set_wake()
246 bank->wake_active &= ~mask; in brcmstb_gpio_irq_set_wake()
255 if (!priv || irq != priv->parent_wake_irq) in brcmstb_gpio_wake_irq_handler()
262 static void brcmstb_gpio_irq_bank_handler(struct brcmstb_gpio_bank *bank) in brcmstb_gpio_irq_bank_handler() argument
264 struct brcmstb_gpio_priv *priv = bank->parent_priv; in brcmstb_gpio_irq_bank_handler()
265 struct irq_domain *domain = priv->irq_domain; in brcmstb_gpio_irq_bank_handler()
266 int hwbase = bank->gc.base - priv->gpio_base; in brcmstb_gpio_irq_bank_handler()
269 while ((status = brcmstb_gpio_get_active_irqs(bank))) { in brcmstb_gpio_irq_bank_handler()
273 if (offset >= bank->width) in brcmstb_gpio_irq_bank_handler()
274 dev_warn(&priv->pdev->dev, in brcmstb_gpio_irq_bank_handler()
275 "IRQ for invalid GPIO (bank=%d, offset=%d)\n", in brcmstb_gpio_irq_bank_handler()
276 bank->id, offset); in brcmstb_gpio_irq_bank_handler()
287 struct brcmstb_gpio_bank *bank; in brcmstb_gpio_irq_handler() local
293 list_for_each_entry(bank, &priv->bank_list, node) in brcmstb_gpio_irq_handler()
294 brcmstb_gpio_irq_bank_handler(bank); in brcmstb_gpio_irq_handler()
301 struct brcmstb_gpio_bank *bank; in brcmstb_gpio_hwirq_to_bank() local
305 list_for_each_entry_reverse(bank, &priv->bank_list, node) { in brcmstb_gpio_hwirq_to_bank()
306 i += bank->gc.ngpio; in brcmstb_gpio_hwirq_to_bank()
308 return bank; in brcmstb_gpio_hwirq_to_bank()
314 * This lock class tells lockdep that GPIO irqs are in a different
324 struct brcmstb_gpio_priv *priv = d->host_data; in brcmstb_gpio_irq_map()
325 struct brcmstb_gpio_bank *bank = in brcmstb_gpio_irq_map() local
327 struct platform_device *pdev = priv->pdev; in brcmstb_gpio_irq_map()
330 if (!bank) in brcmstb_gpio_irq_map()
331 return -EINVAL; in brcmstb_gpio_irq_map()
333 dev_dbg(&pdev->dev, "Mapping irq %d for gpio line %d (bank %d)\n", in brcmstb_gpio_irq_map()
334 irq, (int)hwirq, bank->id); in brcmstb_gpio_irq_map()
335 ret = irq_set_chip_data(irq, &bank->gc); in brcmstb_gpio_irq_map()
340 irq_set_chip_and_handler(irq, &priv->irq_chip, handle_level_irq); in brcmstb_gpio_irq_map()
363 of_property_count_u32_elems(np, "brcm,gpio-bank-widths"); in brcmstb_gpio_sanity_check_banks()
366 dev_err(dev, "Mismatch in banks: res had %d, bank-widths had %d\n", in brcmstb_gpio_sanity_check_banks()
368 return -EINVAL; in brcmstb_gpio_sanity_check_banks()
377 struct brcmstb_gpio_bank *bank; in brcmstb_gpio_remove() local
380 if (priv->parent_irq > 0) in brcmstb_gpio_remove()
381 irq_set_chained_handler_and_data(priv->parent_irq, NULL, NULL); in brcmstb_gpio_remove()
384 if (priv->irq_domain) { in brcmstb_gpio_remove()
385 for (offset = 0; offset < priv->num_gpios; offset++) { in brcmstb_gpio_remove()
386 virq = irq_find_mapping(priv->irq_domain, offset); in brcmstb_gpio_remove()
389 irq_domain_remove(priv->irq_domain); in brcmstb_gpio_remove()
396 list_for_each_entry(bank, &priv->bank_list, node) in brcmstb_gpio_remove()
397 gpiochip_remove(&bank->gc); in brcmstb_gpio_remove()
406 struct brcmstb_gpio_bank *bank = gpiochip_get_data(gc); in brcmstb_gpio_of_xlate() local
409 if (gc->of_gpio_n_cells != 2) { in brcmstb_gpio_of_xlate()
411 return -EINVAL; in brcmstb_gpio_of_xlate()
414 if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells)) in brcmstb_gpio_of_xlate()
415 return -EINVAL; in brcmstb_gpio_of_xlate()
417 offset = gpiospec->args[0] - (gc->base - priv->gpio_base); in brcmstb_gpio_of_xlate()
418 if (offset >= gc->ngpio || offset < 0) in brcmstb_gpio_of_xlate()
419 return -EINVAL; in brcmstb_gpio_of_xlate()
421 if (unlikely(offset >= bank->width)) { in brcmstb_gpio_of_xlate()
422 dev_warn_ratelimited(&priv->pdev->dev, in brcmstb_gpio_of_xlate()
423 "Received request for invalid GPIO offset %d\n", in brcmstb_gpio_of_xlate()
424 gpiospec->args[0]); in brcmstb_gpio_of_xlate()
428 *flags = gpiospec->args[1]; in brcmstb_gpio_of_xlate()
433 /* priv->parent_irq and priv->num_gpios must be set before calling */
437 struct device *dev = &pdev->dev; in brcmstb_gpio_irq_setup()
438 struct device_node *np = dev->of_node; in brcmstb_gpio_irq_setup()
441 priv->irq_domain = in brcmstb_gpio_irq_setup()
442 irq_domain_add_linear(np, priv->num_gpios, in brcmstb_gpio_irq_setup()
445 if (!priv->irq_domain) { in brcmstb_gpio_irq_setup()
447 return -ENXIO; in brcmstb_gpio_irq_setup()
450 if (of_property_read_bool(np, "wakeup-source")) { in brcmstb_gpio_irq_setup()
451 priv->parent_wake_irq = platform_get_irq(pdev, 1); in brcmstb_gpio_irq_setup()
452 if (priv->parent_wake_irq < 0) { in brcmstb_gpio_irq_setup()
453 priv->parent_wake_irq = 0; in brcmstb_gpio_irq_setup()
455 "Couldn't get wake IRQ - GPIOs will not be able to wake from sleep"); in brcmstb_gpio_irq_setup()
458 * Set wakeup capability so we can process boot-time in brcmstb_gpio_irq_setup()
463 err = devm_request_irq(dev, priv->parent_wake_irq, in brcmstb_gpio_irq_setup()
466 "brcmstb-gpio-wake", priv); in brcmstb_gpio_irq_setup()
475 priv->irq_chip.name = dev_name(dev); in brcmstb_gpio_irq_setup()
476 priv->irq_chip.irq_disable = brcmstb_gpio_irq_mask; in brcmstb_gpio_irq_setup()
477 priv->irq_chip.irq_mask = brcmstb_gpio_irq_mask; in brcmstb_gpio_irq_setup()
478 priv->irq_chip.irq_unmask = brcmstb_gpio_irq_unmask; in brcmstb_gpio_irq_setup()
479 priv->irq_chip.irq_ack = brcmstb_gpio_irq_ack; in brcmstb_gpio_irq_setup()
480 priv->irq_chip.irq_set_type = brcmstb_gpio_irq_set_type; in brcmstb_gpio_irq_setup()
482 if (priv->parent_wake_irq) in brcmstb_gpio_irq_setup()
483 priv->irq_chip.irq_set_wake = brcmstb_gpio_irq_set_wake; in brcmstb_gpio_irq_setup()
485 irq_set_chained_handler_and_data(priv->parent_irq, in brcmstb_gpio_irq_setup()
487 irq_set_status_flags(priv->parent_irq, IRQ_DISABLE_UNLAZY); in brcmstb_gpio_irq_setup()
492 irq_domain_remove(priv->irq_domain); in brcmstb_gpio_irq_setup()
498 struct brcmstb_gpio_bank *bank) in brcmstb_gpio_bank_save() argument
500 struct gpio_chip *gc = &bank->gc; in brcmstb_gpio_bank_save()
504 bank->saved_regs[i] = gc->read_reg(priv->reg_base + in brcmstb_gpio_bank_save()
505 GIO_BANK_OFF(bank->id, i)); in brcmstb_gpio_bank_save()
511 struct brcmstb_gpio_bank *bank; in brcmstb_gpio_quiesce() local
515 /* disable non-wake interrupt */ in brcmstb_gpio_quiesce()
516 if (priv->parent_irq >= 0) in brcmstb_gpio_quiesce()
517 disable_irq(priv->parent_irq); in brcmstb_gpio_quiesce()
519 list_for_each_entry(bank, &priv->bank_list, node) { in brcmstb_gpio_quiesce()
520 gc = &bank->gc; in brcmstb_gpio_quiesce()
523 brcmstb_gpio_bank_save(priv, bank); in brcmstb_gpio_quiesce()
525 /* Unmask GPIOs which have been flagged as wake-up sources */ in brcmstb_gpio_quiesce()
526 if (priv->parent_wake_irq) in brcmstb_gpio_quiesce()
527 imask = bank->wake_active; in brcmstb_gpio_quiesce()
530 gc->write_reg(priv->reg_base + GIO_MASK(bank->id), in brcmstb_gpio_quiesce()
537 /* Enable GPIO for S5 cold boot */ in brcmstb_gpio_shutdown()
538 brcmstb_gpio_quiesce(&pdev->dev, false); in brcmstb_gpio_shutdown()
543 struct brcmstb_gpio_bank *bank) in brcmstb_gpio_bank_restore() argument
545 struct gpio_chip *gc = &bank->gc; in brcmstb_gpio_bank_restore()
549 gc->write_reg(priv->reg_base + GIO_BANK_OFF(bank->id, i), in brcmstb_gpio_bank_restore()
550 bank->saved_regs[i]); in brcmstb_gpio_bank_restore()
562 struct brcmstb_gpio_bank *bank; in brcmstb_gpio_resume() local
565 list_for_each_entry(bank, &priv->bank_list, node) { in brcmstb_gpio_resume()
566 need_wakeup_event |= !!__brcmstb_gpio_get_active_irqs(bank); in brcmstb_gpio_resume()
567 brcmstb_gpio_bank_restore(priv, bank); in brcmstb_gpio_resume()
570 if (priv->parent_wake_irq && need_wakeup_event) in brcmstb_gpio_resume()
573 /* enable non-wake interrupt */ in brcmstb_gpio_resume()
574 if (priv->parent_irq >= 0) in brcmstb_gpio_resume()
575 enable_irq(priv->parent_irq); in brcmstb_gpio_resume()
592 struct device *dev = &pdev->dev; in brcmstb_gpio_probe()
593 struct device_node *np = dev->of_node; in brcmstb_gpio_probe()
608 return -ENOMEM; in brcmstb_gpio_probe()
610 INIT_LIST_HEAD(&priv->bank_list); in brcmstb_gpio_probe()
617 priv->gpio_base = gpio_base; in brcmstb_gpio_probe()
618 priv->reg_base = reg_base; in brcmstb_gpio_probe()
619 priv->pdev = pdev; in brcmstb_gpio_probe()
621 if (of_property_read_bool(np, "interrupt-controller")) { in brcmstb_gpio_probe()
622 priv->parent_irq = platform_get_irq(pdev, 0); in brcmstb_gpio_probe()
623 if (priv->parent_irq <= 0) in brcmstb_gpio_probe()
624 return -ENOENT; in brcmstb_gpio_probe()
626 priv->parent_irq = -ENOENT; in brcmstb_gpio_probe()
630 return -EINVAL; in brcmstb_gpio_probe()
634 * bus endianness (i.e., big-endian CPU + big endian bus ==> native in brcmstb_gpio_probe()
644 of_property_for_each_u32(np, "brcm,gpio-bank-widths", prop, p, in brcmstb_gpio_probe()
646 struct brcmstb_gpio_bank *bank; in brcmstb_gpio_probe() local
650 * If bank_width is 0, then there is an empty bank in the in brcmstb_gpio_probe()
654 dev_dbg(dev, "Width 0 found: Empty bank @ %d\n", in brcmstb_gpio_probe()
661 bank = devm_kzalloc(dev, sizeof(*bank), GFP_KERNEL); in brcmstb_gpio_probe()
662 if (!bank) { in brcmstb_gpio_probe()
663 err = -ENOMEM; in brcmstb_gpio_probe()
667 bank->parent_priv = priv; in brcmstb_gpio_probe()
668 bank->id = num_banks; in brcmstb_gpio_probe()
670 dev_err(dev, "Invalid bank width %d\n", bank_width); in brcmstb_gpio_probe()
671 err = -EINVAL; in brcmstb_gpio_probe()
674 bank->width = bank_width; in brcmstb_gpio_probe()
681 gc = &bank->gc; in brcmstb_gpio_probe()
683 reg_base + GIO_DATA(bank->id), in brcmstb_gpio_probe()
685 reg_base + GIO_IODIR(bank->id), flags); in brcmstb_gpio_probe()
691 gc->owner = THIS_MODULE; in brcmstb_gpio_probe()
692 gc->label = devm_kasprintf(dev, GFP_KERNEL, "%pOF", np); in brcmstb_gpio_probe()
693 if (!gc->label) { in brcmstb_gpio_probe()
694 err = -ENOMEM; in brcmstb_gpio_probe()
697 gc->base = gpio_base; in brcmstb_gpio_probe()
698 gc->of_gpio_n_cells = 2; in brcmstb_gpio_probe()
699 gc->of_xlate = brcmstb_gpio_of_xlate; in brcmstb_gpio_probe()
700 /* not all ngpio lines are valid, will use bank width later */ in brcmstb_gpio_probe()
701 gc->ngpio = MAX_GPIO_PER_BANK; in brcmstb_gpio_probe()
702 gc->offset = bank->id * MAX_GPIO_PER_BANK; in brcmstb_gpio_probe()
703 if (priv->parent_irq > 0) in brcmstb_gpio_probe()
704 gc->to_irq = brcmstb_gpio_to_irq; in brcmstb_gpio_probe()
710 need_wakeup_event |= !!__brcmstb_gpio_get_active_irqs(bank); in brcmstb_gpio_probe()
711 gc->write_reg(reg_base + GIO_MASK(bank->id), 0); in brcmstb_gpio_probe()
713 err = gpiochip_add_data(gc, bank); in brcmstb_gpio_probe()
715 dev_err(dev, "Could not add gpiochip for bank %d\n", in brcmstb_gpio_probe()
716 bank->id); in brcmstb_gpio_probe()
719 gpio_base += gc->ngpio; in brcmstb_gpio_probe()
721 dev_dbg(dev, "bank=%d, base=%d, ngpio=%d, width=%d\n", bank->id, in brcmstb_gpio_probe()
722 gc->base, gc->ngpio, bank->width); in brcmstb_gpio_probe()
724 /* Everything looks good, so add bank to list */ in brcmstb_gpio_probe()
725 list_add(&bank->node, &priv->bank_list); in brcmstb_gpio_probe()
730 priv->num_gpios = gpio_base - priv->gpio_base; in brcmstb_gpio_probe()
731 if (priv->parent_irq > 0) { in brcmstb_gpio_probe()
737 if (priv->parent_wake_irq && need_wakeup_event) in brcmstb_gpio_probe()
748 { .compatible = "brcm,brcmstb-gpio" },
756 .name = "brcmstb-gpio",
767 MODULE_DESCRIPTION("Driver for Broadcom BRCMSTB SoC UPG GPIO");