1 // SPDX-License-Identifier: GPL-2.0-only
3 * Broadcom Kona GPIO Driver
5 * Author: Broadcom Corporation <bcm-kernel-feedback-list@broadcom.com>
6 * Copyright (C) 2012-2014 Broadcom Corporation
9 #include <linux/bitops.h>
10 #include <linux/err.h>
12 #include <linux/gpio/driver.h>
13 #include <linux/of_device.h>
14 #include <linux/init.h>
15 #include <linux/irqdomain.h>
16 #include <linux/irqchip/chained_irq.h>
18 #define BCM_GPIO_PASSWD 0x00a5a501
19 #define GPIO_PER_BANK 32
20 #define GPIO_MAX_BANK_NUM 8
22 #define GPIO_BANK(gpio) ((gpio) >> 5)
23 #define GPIO_BIT(gpio) ((gpio) & (GPIO_PER_BANK - 1))
25 /* There is a GPIO control register for each GPIO */
26 #define GPIO_CONTROL(gpio) (0x00000100 + ((gpio) << 2))
28 /* The remaining registers are per GPIO bank */
29 #define GPIO_OUT_STATUS(bank) (0x00000000 + ((bank) << 2))
30 #define GPIO_IN_STATUS(bank) (0x00000020 + ((bank) << 2))
31 #define GPIO_OUT_SET(bank) (0x00000040 + ((bank) << 2))
32 #define GPIO_OUT_CLEAR(bank) (0x00000060 + ((bank) << 2))
33 #define GPIO_INT_STATUS(bank) (0x00000080 + ((bank) << 2))
34 #define GPIO_INT_MASK(bank) (0x000000a0 + ((bank) << 2))
35 #define GPIO_INT_MSKCLR(bank) (0x000000c0 + ((bank) << 2))
36 #define GPIO_PWD_STATUS(bank) (0x00000500 + ((bank) << 2))
38 #define GPIO_GPPWR_OFFSET 0x00000520
40 #define GPIO_GPCTR0_DBR_SHIFT 5
41 #define GPIO_GPCTR0_DBR_MASK 0x000001e0
43 #define GPIO_GPCTR0_ITR_SHIFT 3
44 #define GPIO_GPCTR0_ITR_MASK 0x00000018
45 #define GPIO_GPCTR0_ITR_CMD_RISING_EDGE 0x00000001
46 #define GPIO_GPCTR0_ITR_CMD_FALLING_EDGE 0x00000002
47 #define GPIO_GPCTR0_ITR_CMD_BOTH_EDGE 0x00000003
49 #define GPIO_GPCTR0_IOTR_MASK 0x00000001
50 #define GPIO_GPCTR0_IOTR_CMD_0UTPUT 0x00000000
51 #define GPIO_GPCTR0_IOTR_CMD_INPUT 0x00000001
53 #define GPIO_GPCTR0_DB_ENABLE_MASK 0x00000100
55 #define LOCK_CODE 0xffffffff
56 #define UNLOCK_CODE 0x00000000
58 struct bcm_kona_gpio {
59 void __iomem *reg_base;
62 struct gpio_chip gpio_chip;
63 struct irq_domain *irq_domain;
64 struct bcm_kona_gpio_bank *banks;
65 struct platform_device *pdev;
68 struct bcm_kona_gpio_bank {
71 /* Used in the interrupt handler */
72 struct bcm_kona_gpio *kona_gpio;
75 static inline void bcm_kona_gpio_write_lock_regs(void __iomem *reg_base,
76 int bank_id, u32 lockcode)
78 writel(BCM_GPIO_PASSWD, reg_base + GPIO_GPPWR_OFFSET);
79 writel(lockcode, reg_base + GPIO_PWD_STATUS(bank_id));
82 static void bcm_kona_gpio_lock_gpio(struct bcm_kona_gpio *kona_gpio,
87 int bank_id = GPIO_BANK(gpio);
89 raw_spin_lock_irqsave(&kona_gpio->lock, flags);
91 val = readl(kona_gpio->reg_base + GPIO_PWD_STATUS(bank_id));
93 bcm_kona_gpio_write_lock_regs(kona_gpio->reg_base, bank_id, val);
95 raw_spin_unlock_irqrestore(&kona_gpio->lock, flags);
98 static void bcm_kona_gpio_unlock_gpio(struct bcm_kona_gpio *kona_gpio,
103 int bank_id = GPIO_BANK(gpio);
105 raw_spin_lock_irqsave(&kona_gpio->lock, flags);
107 val = readl(kona_gpio->reg_base + GPIO_PWD_STATUS(bank_id));
109 bcm_kona_gpio_write_lock_regs(kona_gpio->reg_base, bank_id, val);
111 raw_spin_unlock_irqrestore(&kona_gpio->lock, flags);
114 static int bcm_kona_gpio_get_dir(struct gpio_chip *chip, unsigned gpio)
116 struct bcm_kona_gpio *kona_gpio = gpiochip_get_data(chip);
117 void __iomem *reg_base = kona_gpio->reg_base;
120 val = readl(reg_base + GPIO_CONTROL(gpio)) & GPIO_GPCTR0_IOTR_MASK;
121 return val ? GPIO_LINE_DIRECTION_IN : GPIO_LINE_DIRECTION_OUT;
124 static void bcm_kona_gpio_set(struct gpio_chip *chip, unsigned gpio, int value)
126 struct bcm_kona_gpio *kona_gpio;
127 void __iomem *reg_base;
128 int bank_id = GPIO_BANK(gpio);
129 int bit = GPIO_BIT(gpio);
133 kona_gpio = gpiochip_get_data(chip);
134 reg_base = kona_gpio->reg_base;
135 raw_spin_lock_irqsave(&kona_gpio->lock, flags);
137 /* this function only applies to output pin */
138 if (bcm_kona_gpio_get_dir(chip, gpio) == GPIO_LINE_DIRECTION_IN)
141 reg_offset = value ? GPIO_OUT_SET(bank_id) : GPIO_OUT_CLEAR(bank_id);
143 val = readl(reg_base + reg_offset);
145 writel(val, reg_base + reg_offset);
148 raw_spin_unlock_irqrestore(&kona_gpio->lock, flags);
151 static int bcm_kona_gpio_get(struct gpio_chip *chip, unsigned gpio)
153 struct bcm_kona_gpio *kona_gpio;
154 void __iomem *reg_base;
155 int bank_id = GPIO_BANK(gpio);
156 int bit = GPIO_BIT(gpio);
160 kona_gpio = gpiochip_get_data(chip);
161 reg_base = kona_gpio->reg_base;
162 raw_spin_lock_irqsave(&kona_gpio->lock, flags);
164 if (bcm_kona_gpio_get_dir(chip, gpio) == GPIO_LINE_DIRECTION_IN)
165 reg_offset = GPIO_IN_STATUS(bank_id);
167 reg_offset = GPIO_OUT_STATUS(bank_id);
169 /* read the GPIO bank status */
170 val = readl(reg_base + reg_offset);
172 raw_spin_unlock_irqrestore(&kona_gpio->lock, flags);
174 /* return the specified bit status */
175 return !!(val & BIT(bit));
178 static int bcm_kona_gpio_request(struct gpio_chip *chip, unsigned gpio)
180 struct bcm_kona_gpio *kona_gpio = gpiochip_get_data(chip);
182 bcm_kona_gpio_unlock_gpio(kona_gpio, gpio);
186 static void bcm_kona_gpio_free(struct gpio_chip *chip, unsigned gpio)
188 struct bcm_kona_gpio *kona_gpio = gpiochip_get_data(chip);
190 bcm_kona_gpio_lock_gpio(kona_gpio, gpio);
193 static int bcm_kona_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
195 struct bcm_kona_gpio *kona_gpio;
196 void __iomem *reg_base;
200 kona_gpio = gpiochip_get_data(chip);
201 reg_base = kona_gpio->reg_base;
202 raw_spin_lock_irqsave(&kona_gpio->lock, flags);
204 val = readl(reg_base + GPIO_CONTROL(gpio));
205 val &= ~GPIO_GPCTR0_IOTR_MASK;
206 val |= GPIO_GPCTR0_IOTR_CMD_INPUT;
207 writel(val, reg_base + GPIO_CONTROL(gpio));
209 raw_spin_unlock_irqrestore(&kona_gpio->lock, flags);
214 static int bcm_kona_gpio_direction_output(struct gpio_chip *chip,
215 unsigned gpio, int value)
217 struct bcm_kona_gpio *kona_gpio;
218 void __iomem *reg_base;
219 int bank_id = GPIO_BANK(gpio);
220 int bit = GPIO_BIT(gpio);
224 kona_gpio = gpiochip_get_data(chip);
225 reg_base = kona_gpio->reg_base;
226 raw_spin_lock_irqsave(&kona_gpio->lock, flags);
228 val = readl(reg_base + GPIO_CONTROL(gpio));
229 val &= ~GPIO_GPCTR0_IOTR_MASK;
230 val |= GPIO_GPCTR0_IOTR_CMD_0UTPUT;
231 writel(val, reg_base + GPIO_CONTROL(gpio));
232 reg_offset = value ? GPIO_OUT_SET(bank_id) : GPIO_OUT_CLEAR(bank_id);
234 val = readl(reg_base + reg_offset);
236 writel(val, reg_base + reg_offset);
238 raw_spin_unlock_irqrestore(&kona_gpio->lock, flags);
243 static int bcm_kona_gpio_to_irq(struct gpio_chip *chip, unsigned gpio)
245 struct bcm_kona_gpio *kona_gpio;
247 kona_gpio = gpiochip_get_data(chip);
248 if (gpio >= kona_gpio->gpio_chip.ngpio)
250 return irq_create_mapping(kona_gpio->irq_domain, gpio);
253 static int bcm_kona_gpio_set_debounce(struct gpio_chip *chip, unsigned gpio,
256 struct bcm_kona_gpio *kona_gpio;
257 void __iomem *reg_base;
261 kona_gpio = gpiochip_get_data(chip);
262 reg_base = kona_gpio->reg_base;
263 /* debounce must be 1-128ms (or 0) */
264 if ((debounce > 0 && debounce < 1000) || debounce > 128000) {
265 dev_err(chip->parent, "Debounce value %u not in range\n",
270 /* calculate debounce bit value */
275 res = fls(debounce) - 1;
276 /* Check if MSB-1 is set (round up or down) */
277 if (res > 0 && (debounce & BIT(res - 1)))
281 /* spin lock for read-modify-write of the GPIO register */
282 raw_spin_lock_irqsave(&kona_gpio->lock, flags);
284 val = readl(reg_base + GPIO_CONTROL(gpio));
285 val &= ~GPIO_GPCTR0_DBR_MASK;
288 /* disable debounce */
289 val &= ~GPIO_GPCTR0_DB_ENABLE_MASK;
291 val |= GPIO_GPCTR0_DB_ENABLE_MASK |
292 (res << GPIO_GPCTR0_DBR_SHIFT);
295 writel(val, reg_base + GPIO_CONTROL(gpio));
297 raw_spin_unlock_irqrestore(&kona_gpio->lock, flags);
302 static int bcm_kona_gpio_set_config(struct gpio_chip *chip, unsigned gpio,
303 unsigned long config)
307 if (pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE)
310 debounce = pinconf_to_config_argument(config);
311 return bcm_kona_gpio_set_debounce(chip, gpio, debounce);
314 static const struct gpio_chip template_chip = {
315 .label = "bcm-kona-gpio",
316 .owner = THIS_MODULE,
317 .request = bcm_kona_gpio_request,
318 .free = bcm_kona_gpio_free,
319 .get_direction = bcm_kona_gpio_get_dir,
320 .direction_input = bcm_kona_gpio_direction_input,
321 .get = bcm_kona_gpio_get,
322 .direction_output = bcm_kona_gpio_direction_output,
323 .set = bcm_kona_gpio_set,
324 .set_config = bcm_kona_gpio_set_config,
325 .to_irq = bcm_kona_gpio_to_irq,
329 static void bcm_kona_gpio_irq_ack(struct irq_data *d)
331 struct bcm_kona_gpio *kona_gpio;
332 void __iomem *reg_base;
333 unsigned gpio = d->hwirq;
334 int bank_id = GPIO_BANK(gpio);
335 int bit = GPIO_BIT(gpio);
339 kona_gpio = irq_data_get_irq_chip_data(d);
340 reg_base = kona_gpio->reg_base;
341 raw_spin_lock_irqsave(&kona_gpio->lock, flags);
343 val = readl(reg_base + GPIO_INT_STATUS(bank_id));
345 writel(val, reg_base + GPIO_INT_STATUS(bank_id));
347 raw_spin_unlock_irqrestore(&kona_gpio->lock, flags);
350 static void bcm_kona_gpio_irq_mask(struct irq_data *d)
352 struct bcm_kona_gpio *kona_gpio;
353 void __iomem *reg_base;
354 unsigned gpio = d->hwirq;
355 int bank_id = GPIO_BANK(gpio);
356 int bit = GPIO_BIT(gpio);
360 kona_gpio = irq_data_get_irq_chip_data(d);
361 reg_base = kona_gpio->reg_base;
362 raw_spin_lock_irqsave(&kona_gpio->lock, flags);
364 val = readl(reg_base + GPIO_INT_MASK(bank_id));
366 writel(val, reg_base + GPIO_INT_MASK(bank_id));
367 gpiochip_disable_irq(&kona_gpio->gpio_chip, gpio);
369 raw_spin_unlock_irqrestore(&kona_gpio->lock, flags);
372 static void bcm_kona_gpio_irq_unmask(struct irq_data *d)
374 struct bcm_kona_gpio *kona_gpio;
375 void __iomem *reg_base;
376 unsigned gpio = d->hwirq;
377 int bank_id = GPIO_BANK(gpio);
378 int bit = GPIO_BIT(gpio);
382 kona_gpio = irq_data_get_irq_chip_data(d);
383 reg_base = kona_gpio->reg_base;
384 raw_spin_lock_irqsave(&kona_gpio->lock, flags);
386 val = readl(reg_base + GPIO_INT_MSKCLR(bank_id));
388 writel(val, reg_base + GPIO_INT_MSKCLR(bank_id));
389 gpiochip_enable_irq(&kona_gpio->gpio_chip, gpio);
391 raw_spin_unlock_irqrestore(&kona_gpio->lock, flags);
394 static int bcm_kona_gpio_irq_set_type(struct irq_data *d, unsigned int type)
396 struct bcm_kona_gpio *kona_gpio;
397 void __iomem *reg_base;
398 unsigned gpio = d->hwirq;
403 kona_gpio = irq_data_get_irq_chip_data(d);
404 reg_base = kona_gpio->reg_base;
405 switch (type & IRQ_TYPE_SENSE_MASK) {
406 case IRQ_TYPE_EDGE_RISING:
407 lvl_type = GPIO_GPCTR0_ITR_CMD_RISING_EDGE;
410 case IRQ_TYPE_EDGE_FALLING:
411 lvl_type = GPIO_GPCTR0_ITR_CMD_FALLING_EDGE;
414 case IRQ_TYPE_EDGE_BOTH:
415 lvl_type = GPIO_GPCTR0_ITR_CMD_BOTH_EDGE;
418 case IRQ_TYPE_LEVEL_HIGH:
419 case IRQ_TYPE_LEVEL_LOW:
420 /* BCM GPIO doesn't support level triggering */
422 dev_err(kona_gpio->gpio_chip.parent,
423 "Invalid BCM GPIO irq type 0x%x\n", type);
427 raw_spin_lock_irqsave(&kona_gpio->lock, flags);
429 val = readl(reg_base + GPIO_CONTROL(gpio));
430 val &= ~GPIO_GPCTR0_ITR_MASK;
431 val |= lvl_type << GPIO_GPCTR0_ITR_SHIFT;
432 writel(val, reg_base + GPIO_CONTROL(gpio));
434 raw_spin_unlock_irqrestore(&kona_gpio->lock, flags);
439 static void bcm_kona_gpio_irq_handler(struct irq_desc *desc)
441 void __iomem *reg_base;
444 struct bcm_kona_gpio_bank *bank = irq_desc_get_handler_data(desc);
445 struct irq_chip *chip = irq_desc_get_chip(desc);
447 chained_irq_enter(chip, desc);
450 * For bank interrupts, we can't use chip_data to store the kona_gpio
451 * pointer, since GIC needs it for its own purposes. Therefore, we get
452 * our pointer from the bank structure.
454 reg_base = bank->kona_gpio->reg_base;
457 while ((sta = readl(reg_base + GPIO_INT_STATUS(bank_id)) &
458 (~(readl(reg_base + GPIO_INT_MASK(bank_id)))))) {
459 for_each_set_bit(bit, &sta, 32) {
460 int hwirq = GPIO_PER_BANK * bank_id + bit;
462 * Clear interrupt before handler is called so we don't
463 * miss any interrupt occurred during executing them.
465 writel(readl(reg_base + GPIO_INT_STATUS(bank_id)) |
466 BIT(bit), reg_base + GPIO_INT_STATUS(bank_id));
467 /* Invoke interrupt handler */
468 generic_handle_domain_irq(bank->kona_gpio->irq_domain,
473 chained_irq_exit(chip, desc);
476 static int bcm_kona_gpio_irq_reqres(struct irq_data *d)
478 struct bcm_kona_gpio *kona_gpio = irq_data_get_irq_chip_data(d);
480 return gpiochip_reqres_irq(&kona_gpio->gpio_chip, d->hwirq);
483 static void bcm_kona_gpio_irq_relres(struct irq_data *d)
485 struct bcm_kona_gpio *kona_gpio = irq_data_get_irq_chip_data(d);
487 gpiochip_relres_irq(&kona_gpio->gpio_chip, d->hwirq);
490 static struct irq_chip bcm_gpio_irq_chip = {
491 .name = "bcm-kona-gpio",
492 .irq_ack = bcm_kona_gpio_irq_ack,
493 .irq_mask = bcm_kona_gpio_irq_mask,
494 .irq_unmask = bcm_kona_gpio_irq_unmask,
495 .irq_set_type = bcm_kona_gpio_irq_set_type,
496 .irq_request_resources = bcm_kona_gpio_irq_reqres,
497 .irq_release_resources = bcm_kona_gpio_irq_relres,
500 static struct of_device_id const bcm_kona_gpio_of_match[] = {
501 { .compatible = "brcm,kona-gpio" },
506 * This lock class tells lockdep that GPIO irqs are in a different
507 * category than their parents, so it won't report false recursion.
509 static struct lock_class_key gpio_lock_class;
510 static struct lock_class_key gpio_request_class;
512 static int bcm_kona_gpio_irq_map(struct irq_domain *d, unsigned int irq,
513 irq_hw_number_t hwirq)
517 ret = irq_set_chip_data(irq, d->host_data);
520 irq_set_lockdep_class(irq, &gpio_lock_class, &gpio_request_class);
521 irq_set_chip_and_handler(irq, &bcm_gpio_irq_chip, handle_simple_irq);
522 irq_set_noprobe(irq);
527 static void bcm_kona_gpio_irq_unmap(struct irq_domain *d, unsigned int irq)
529 irq_set_chip_and_handler(irq, NULL, NULL);
530 irq_set_chip_data(irq, NULL);
533 static const struct irq_domain_ops bcm_kona_irq_ops = {
534 .map = bcm_kona_gpio_irq_map,
535 .unmap = bcm_kona_gpio_irq_unmap,
536 .xlate = irq_domain_xlate_twocell,
539 static void bcm_kona_gpio_reset(struct bcm_kona_gpio *kona_gpio)
541 void __iomem *reg_base;
544 reg_base = kona_gpio->reg_base;
545 /* disable interrupts and clear status */
546 for (i = 0; i < kona_gpio->num_bank; i++) {
547 /* Unlock the entire bank first */
548 bcm_kona_gpio_write_lock_regs(reg_base, i, UNLOCK_CODE);
549 writel(0xffffffff, reg_base + GPIO_INT_MASK(i));
550 writel(0xffffffff, reg_base + GPIO_INT_STATUS(i));
551 /* Now re-lock the bank */
552 bcm_kona_gpio_write_lock_regs(reg_base, i, LOCK_CODE);
556 static int bcm_kona_gpio_probe(struct platform_device *pdev)
558 struct device *dev = &pdev->dev;
559 const struct of_device_id *match;
560 struct bcm_kona_gpio_bank *bank;
561 struct bcm_kona_gpio *kona_gpio;
562 struct gpio_chip *chip;
566 match = of_match_device(bcm_kona_gpio_of_match, dev);
568 dev_err(dev, "Failed to find gpio controller\n");
572 kona_gpio = devm_kzalloc(dev, sizeof(*kona_gpio), GFP_KERNEL);
576 kona_gpio->gpio_chip = template_chip;
577 chip = &kona_gpio->gpio_chip;
578 ret = platform_irq_count(pdev);
580 dev_err(dev, "Couldn't determine # GPIO banks\n");
582 } else if (ret < 0) {
583 return dev_err_probe(dev, ret, "Couldn't determine GPIO banks\n");
585 kona_gpio->num_bank = ret;
587 if (kona_gpio->num_bank > GPIO_MAX_BANK_NUM) {
588 dev_err(dev, "Too many GPIO banks configured (max=%d)\n",
592 kona_gpio->banks = devm_kcalloc(dev,
594 sizeof(*kona_gpio->banks),
596 if (!kona_gpio->banks)
599 kona_gpio->pdev = pdev;
600 platform_set_drvdata(pdev, kona_gpio);
602 chip->ngpio = kona_gpio->num_bank * GPIO_PER_BANK;
604 kona_gpio->irq_domain = irq_domain_add_linear(dev->of_node,
608 if (!kona_gpio->irq_domain) {
609 dev_err(dev, "Couldn't allocate IRQ domain\n");
613 kona_gpio->reg_base = devm_platform_ioremap_resource(pdev, 0);
614 if (IS_ERR(kona_gpio->reg_base)) {
615 ret = PTR_ERR(kona_gpio->reg_base);
619 for (i = 0; i < kona_gpio->num_bank; i++) {
620 bank = &kona_gpio->banks[i];
622 bank->irq = platform_get_irq(pdev, i);
623 bank->kona_gpio = kona_gpio;
625 dev_err(dev, "Couldn't get IRQ for bank %d", i);
631 dev_info(&pdev->dev, "Setting up Kona GPIO\n");
633 bcm_kona_gpio_reset(kona_gpio);
635 ret = devm_gpiochip_add_data(dev, chip, kona_gpio);
637 dev_err(dev, "Couldn't add GPIO chip -- %d\n", ret);
640 for (i = 0; i < kona_gpio->num_bank; i++) {
641 bank = &kona_gpio->banks[i];
642 irq_set_chained_handler_and_data(bank->irq,
643 bcm_kona_gpio_irq_handler,
647 raw_spin_lock_init(&kona_gpio->lock);
652 irq_domain_remove(kona_gpio->irq_domain);
657 static struct platform_driver bcm_kona_gpio_driver = {
659 .name = "bcm-kona-gpio",
660 .of_match_table = bcm_kona_gpio_of_match,
662 .probe = bcm_kona_gpio_probe,
664 builtin_platform_driver(bcm_kona_gpio_driver);