2 * Copyright (C) 2012-2014 Broadcom Corporation
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation version 2.
8 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
9 * kind, whether express or implied; without even the implied warranty
10 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #include <linux/bitops.h>
15 #include <linux/err.h>
17 #include <linux/gpio.h>
18 #include <linux/of_device.h>
19 #include <linux/of_irq.h>
20 #include <linux/module.h>
21 #include <linux/irqdomain.h>
22 #include <linux/irqchip/chained_irq.h>
24 #define BCM_GPIO_PASSWD 0x00a5a501
25 #define GPIO_PER_BANK 32
26 #define GPIO_MAX_BANK_NUM 8
28 #define GPIO_BANK(gpio) ((gpio) >> 5)
29 #define GPIO_BIT(gpio) ((gpio) & (GPIO_PER_BANK - 1))
31 /* There is a GPIO control register for each GPIO */
32 #define GPIO_CONTROL(gpio) (0x00000100 + ((gpio) << 2))
34 /* The remaining registers are per GPIO bank */
35 #define GPIO_OUT_STATUS(bank) (0x00000000 + ((bank) << 2))
36 #define GPIO_IN_STATUS(bank) (0x00000020 + ((bank) << 2))
37 #define GPIO_OUT_SET(bank) (0x00000040 + ((bank) << 2))
38 #define GPIO_OUT_CLEAR(bank) (0x00000060 + ((bank) << 2))
39 #define GPIO_INT_STATUS(bank) (0x00000080 + ((bank) << 2))
40 #define GPIO_INT_MASK(bank) (0x000000a0 + ((bank) << 2))
41 #define GPIO_INT_MSKCLR(bank) (0x000000c0 + ((bank) << 2))
42 #define GPIO_PWD_STATUS(bank) (0x00000500 + ((bank) << 2))
44 #define GPIO_GPPWR_OFFSET 0x00000520
46 #define GPIO_GPCTR0_DBR_SHIFT 5
47 #define GPIO_GPCTR0_DBR_MASK 0x000001e0
49 #define GPIO_GPCTR0_ITR_SHIFT 3
50 #define GPIO_GPCTR0_ITR_MASK 0x00000018
51 #define GPIO_GPCTR0_ITR_CMD_RISING_EDGE 0x00000001
52 #define GPIO_GPCTR0_ITR_CMD_FALLING_EDGE 0x00000002
53 #define GPIO_GPCTR0_ITR_CMD_BOTH_EDGE 0x00000003
55 #define GPIO_GPCTR0_IOTR_MASK 0x00000001
56 #define GPIO_GPCTR0_IOTR_CMD_0UTPUT 0x00000000
57 #define GPIO_GPCTR0_IOTR_CMD_INPUT 0x00000001
59 #define GPIO_GPCTR0_DB_ENABLE_MASK 0x00000100
61 #define LOCK_CODE 0xffffffff
62 #define UNLOCK_CODE 0x00000000
64 struct bcm_kona_gpio {
65 void __iomem *reg_base;
68 struct gpio_chip gpio_chip;
69 struct irq_domain *irq_domain;
70 struct bcm_kona_gpio_bank *banks;
71 struct platform_device *pdev;
74 struct bcm_kona_gpio_bank {
77 /* Used in the interrupt handler */
78 struct bcm_kona_gpio *kona_gpio;
81 static inline struct bcm_kona_gpio *to_kona_gpio(struct gpio_chip *chip)
83 return container_of(chip, struct bcm_kona_gpio, gpio_chip);
86 static inline void bcm_kona_gpio_write_lock_regs(void __iomem *reg_base,
87 int bank_id, u32 lockcode)
89 writel(BCM_GPIO_PASSWD, reg_base + GPIO_GPPWR_OFFSET);
90 writel(lockcode, reg_base + GPIO_PWD_STATUS(bank_id));
93 static void bcm_kona_gpio_lock_gpio(struct bcm_kona_gpio *kona_gpio,
98 int bank_id = GPIO_BANK(gpio);
100 spin_lock_irqsave(&kona_gpio->lock, flags);
102 val = readl(kona_gpio->reg_base + GPIO_PWD_STATUS(bank_id));
104 bcm_kona_gpio_write_lock_regs(kona_gpio->reg_base, bank_id, val);
106 spin_unlock_irqrestore(&kona_gpio->lock, flags);
109 static void bcm_kona_gpio_unlock_gpio(struct bcm_kona_gpio *kona_gpio,
114 int bank_id = GPIO_BANK(gpio);
116 spin_lock_irqsave(&kona_gpio->lock, flags);
118 val = readl(kona_gpio->reg_base + GPIO_PWD_STATUS(bank_id));
120 bcm_kona_gpio_write_lock_regs(kona_gpio->reg_base, bank_id, val);
122 spin_unlock_irqrestore(&kona_gpio->lock, flags);
125 static void bcm_kona_gpio_set(struct gpio_chip *chip, unsigned gpio, int value)
127 struct bcm_kona_gpio *kona_gpio;
128 void __iomem *reg_base;
129 int bank_id = GPIO_BANK(gpio);
130 int bit = GPIO_BIT(gpio);
134 kona_gpio = to_kona_gpio(chip);
135 reg_base = kona_gpio->reg_base;
136 spin_lock_irqsave(&kona_gpio->lock, flags);
138 /* determine the GPIO pin direction */
139 val = readl(reg_base + GPIO_CONTROL(gpio));
140 val &= GPIO_GPCTR0_IOTR_MASK;
142 /* this function only applies to output pin */
143 if (GPIO_GPCTR0_IOTR_CMD_INPUT == val)
146 reg_offset = value ? GPIO_OUT_SET(bank_id) : GPIO_OUT_CLEAR(bank_id);
148 val = readl(reg_base + reg_offset);
150 writel(val, reg_base + reg_offset);
153 spin_unlock_irqrestore(&kona_gpio->lock, flags);
156 static int bcm_kona_gpio_get(struct gpio_chip *chip, unsigned gpio)
158 struct bcm_kona_gpio *kona_gpio;
159 void __iomem *reg_base;
160 int bank_id = GPIO_BANK(gpio);
161 int bit = GPIO_BIT(gpio);
165 kona_gpio = to_kona_gpio(chip);
166 reg_base = kona_gpio->reg_base;
167 spin_lock_irqsave(&kona_gpio->lock, flags);
169 /* determine the GPIO pin direction */
170 val = readl(reg_base + GPIO_CONTROL(gpio));
171 val &= GPIO_GPCTR0_IOTR_MASK;
173 /* read the GPIO bank status */
174 reg_offset = (GPIO_GPCTR0_IOTR_CMD_INPUT == val) ?
175 GPIO_IN_STATUS(bank_id) : GPIO_OUT_STATUS(bank_id);
176 val = readl(reg_base + reg_offset);
178 spin_unlock_irqrestore(&kona_gpio->lock, flags);
180 /* return the specified bit status */
181 return !!(val & BIT(bit));
184 static int bcm_kona_gpio_request(struct gpio_chip *chip, unsigned gpio)
186 struct bcm_kona_gpio *kona_gpio = to_kona_gpio(chip);
188 bcm_kona_gpio_unlock_gpio(kona_gpio, gpio);
192 static void bcm_kona_gpio_free(struct gpio_chip *chip, unsigned gpio)
194 struct bcm_kona_gpio *kona_gpio = to_kona_gpio(chip);
196 bcm_kona_gpio_lock_gpio(kona_gpio, gpio);
199 static int bcm_kona_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
201 struct bcm_kona_gpio *kona_gpio;
202 void __iomem *reg_base;
206 kona_gpio = to_kona_gpio(chip);
207 reg_base = kona_gpio->reg_base;
208 spin_lock_irqsave(&kona_gpio->lock, flags);
210 val = readl(reg_base + GPIO_CONTROL(gpio));
211 val &= ~GPIO_GPCTR0_IOTR_MASK;
212 val |= GPIO_GPCTR0_IOTR_CMD_INPUT;
213 writel(val, reg_base + GPIO_CONTROL(gpio));
215 spin_unlock_irqrestore(&kona_gpio->lock, flags);
220 static int bcm_kona_gpio_direction_output(struct gpio_chip *chip,
221 unsigned gpio, int value)
223 struct bcm_kona_gpio *kona_gpio;
224 void __iomem *reg_base;
225 int bank_id = GPIO_BANK(gpio);
226 int bit = GPIO_BIT(gpio);
230 kona_gpio = to_kona_gpio(chip);
231 reg_base = kona_gpio->reg_base;
232 spin_lock_irqsave(&kona_gpio->lock, flags);
234 val = readl(reg_base + GPIO_CONTROL(gpio));
235 val &= ~GPIO_GPCTR0_IOTR_MASK;
236 val |= GPIO_GPCTR0_IOTR_CMD_0UTPUT;
237 writel(val, reg_base + GPIO_CONTROL(gpio));
238 reg_offset = value ? GPIO_OUT_SET(bank_id) : GPIO_OUT_CLEAR(bank_id);
240 val = readl(reg_base + reg_offset);
242 writel(val, reg_base + reg_offset);
244 spin_unlock_irqrestore(&kona_gpio->lock, flags);
249 static int bcm_kona_gpio_to_irq(struct gpio_chip *chip, unsigned gpio)
251 struct bcm_kona_gpio *kona_gpio;
253 kona_gpio = to_kona_gpio(chip);
254 if (gpio >= kona_gpio->gpio_chip.ngpio)
256 return irq_create_mapping(kona_gpio->irq_domain, gpio);
259 static int bcm_kona_gpio_set_debounce(struct gpio_chip *chip, unsigned gpio,
262 struct bcm_kona_gpio *kona_gpio;
263 void __iomem *reg_base;
267 kona_gpio = to_kona_gpio(chip);
268 reg_base = kona_gpio->reg_base;
269 /* debounce must be 1-128ms (or 0) */
270 if ((debounce > 0 && debounce < 1000) || debounce > 128000) {
271 dev_err(chip->dev, "Debounce value %u not in range\n",
276 /* calculate debounce bit value */
281 res = fls(debounce) - 1;
282 /* Check if MSB-1 is set (round up or down) */
283 if (res > 0 && (debounce & BIT(res - 1)))
287 /* spin lock for read-modify-write of the GPIO register */
288 spin_lock_irqsave(&kona_gpio->lock, flags);
290 val = readl(reg_base + GPIO_CONTROL(gpio));
291 val &= ~GPIO_GPCTR0_DBR_MASK;
294 /* disable debounce */
295 val &= ~GPIO_GPCTR0_DB_ENABLE_MASK;
297 val |= GPIO_GPCTR0_DB_ENABLE_MASK |
298 (res << GPIO_GPCTR0_DBR_SHIFT);
301 writel(val, reg_base + GPIO_CONTROL(gpio));
303 spin_unlock_irqrestore(&kona_gpio->lock, flags);
308 static struct gpio_chip template_chip = {
309 .label = "bcm-kona-gpio",
310 .owner = THIS_MODULE,
311 .request = bcm_kona_gpio_request,
312 .free = bcm_kona_gpio_free,
313 .direction_input = bcm_kona_gpio_direction_input,
314 .get = bcm_kona_gpio_get,
315 .direction_output = bcm_kona_gpio_direction_output,
316 .set = bcm_kona_gpio_set,
317 .set_debounce = bcm_kona_gpio_set_debounce,
318 .to_irq = bcm_kona_gpio_to_irq,
322 static void bcm_kona_gpio_irq_ack(struct irq_data *d)
324 struct bcm_kona_gpio *kona_gpio;
325 void __iomem *reg_base;
326 unsigned gpio = d->hwirq;
327 int bank_id = GPIO_BANK(gpio);
328 int bit = GPIO_BIT(gpio);
332 kona_gpio = irq_data_get_irq_chip_data(d);
333 reg_base = kona_gpio->reg_base;
334 spin_lock_irqsave(&kona_gpio->lock, flags);
336 val = readl(reg_base + GPIO_INT_STATUS(bank_id));
338 writel(val, reg_base + GPIO_INT_STATUS(bank_id));
340 spin_unlock_irqrestore(&kona_gpio->lock, flags);
343 static void bcm_kona_gpio_irq_mask(struct irq_data *d)
345 struct bcm_kona_gpio *kona_gpio;
346 void __iomem *reg_base;
347 unsigned gpio = d->hwirq;
348 int bank_id = GPIO_BANK(gpio);
349 int bit = GPIO_BIT(gpio);
353 kona_gpio = irq_data_get_irq_chip_data(d);
354 reg_base = kona_gpio->reg_base;
355 spin_lock_irqsave(&kona_gpio->lock, flags);
357 val = readl(reg_base + GPIO_INT_MASK(bank_id));
359 writel(val, reg_base + GPIO_INT_MASK(bank_id));
361 spin_unlock_irqrestore(&kona_gpio->lock, flags);
364 static void bcm_kona_gpio_irq_unmask(struct irq_data *d)
366 struct bcm_kona_gpio *kona_gpio;
367 void __iomem *reg_base;
368 unsigned gpio = d->hwirq;
369 int bank_id = GPIO_BANK(gpio);
370 int bit = GPIO_BIT(gpio);
374 kona_gpio = irq_data_get_irq_chip_data(d);
375 reg_base = kona_gpio->reg_base;
376 spin_lock_irqsave(&kona_gpio->lock, flags);
378 val = readl(reg_base + GPIO_INT_MSKCLR(bank_id));
380 writel(val, reg_base + GPIO_INT_MSKCLR(bank_id));
382 spin_unlock_irqrestore(&kona_gpio->lock, flags);
385 static int bcm_kona_gpio_irq_set_type(struct irq_data *d, unsigned int type)
387 struct bcm_kona_gpio *kona_gpio;
388 void __iomem *reg_base;
389 unsigned gpio = d->hwirq;
394 kona_gpio = irq_data_get_irq_chip_data(d);
395 reg_base = kona_gpio->reg_base;
396 switch (type & IRQ_TYPE_SENSE_MASK) {
397 case IRQ_TYPE_EDGE_RISING:
398 lvl_type = GPIO_GPCTR0_ITR_CMD_RISING_EDGE;
401 case IRQ_TYPE_EDGE_FALLING:
402 lvl_type = GPIO_GPCTR0_ITR_CMD_FALLING_EDGE;
405 case IRQ_TYPE_EDGE_BOTH:
406 lvl_type = GPIO_GPCTR0_ITR_CMD_BOTH_EDGE;
409 case IRQ_TYPE_LEVEL_HIGH:
410 case IRQ_TYPE_LEVEL_LOW:
411 /* BCM GPIO doesn't support level triggering */
413 dev_err(kona_gpio->gpio_chip.dev,
414 "Invalid BCM GPIO irq type 0x%x\n", type);
418 spin_lock_irqsave(&kona_gpio->lock, flags);
420 val = readl(reg_base + GPIO_CONTROL(gpio));
421 val &= ~GPIO_GPCTR0_ITR_MASK;
422 val |= lvl_type << GPIO_GPCTR0_ITR_SHIFT;
423 writel(val, reg_base + GPIO_CONTROL(gpio));
425 spin_unlock_irqrestore(&kona_gpio->lock, flags);
430 static void bcm_kona_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
432 void __iomem *reg_base;
435 struct bcm_kona_gpio_bank *bank = irq_get_handler_data(irq);
436 struct irq_chip *chip = irq_desc_get_chip(desc);
438 chained_irq_enter(chip, desc);
441 * For bank interrupts, we can't use chip_data to store the kona_gpio
442 * pointer, since GIC needs it for its own purposes. Therefore, we get
443 * our pointer from the bank structure.
445 reg_base = bank->kona_gpio->reg_base;
448 while ((sta = readl(reg_base + GPIO_INT_STATUS(bank_id)) &
449 (~(readl(reg_base + GPIO_INT_MASK(bank_id)))))) {
450 for_each_set_bit(bit, &sta, 32) {
451 int hwirq = GPIO_PER_BANK * bank_id + bit;
453 irq_find_mapping(bank->kona_gpio->irq_domain,
456 * Clear interrupt before handler is called so we don't
457 * miss any interrupt occurred during executing them.
459 writel(readl(reg_base + GPIO_INT_STATUS(bank_id)) |
460 BIT(bit), reg_base + GPIO_INT_STATUS(bank_id));
461 /* Invoke interrupt handler */
462 generic_handle_irq(child_irq);
466 chained_irq_exit(chip, desc);
469 static int bcm_kona_gpio_irq_reqres(struct irq_data *d)
471 struct bcm_kona_gpio *kona_gpio = irq_data_get_irq_chip_data(d);
473 if (gpio_lock_as_irq(&kona_gpio->gpio_chip, d->hwirq)) {
474 dev_err(kona_gpio->gpio_chip.dev,
475 "unable to lock HW IRQ %lu for IRQ\n",
482 static void bcm_kona_gpio_irq_relres(struct irq_data *d)
484 struct bcm_kona_gpio *kona_gpio = irq_data_get_irq_chip_data(d);
486 gpio_unlock_as_irq(&kona_gpio->gpio_chip, d->hwirq);
489 static struct irq_chip bcm_gpio_irq_chip = {
490 .name = "bcm-kona-gpio",
491 .irq_ack = bcm_kona_gpio_irq_ack,
492 .irq_mask = bcm_kona_gpio_irq_mask,
493 .irq_unmask = bcm_kona_gpio_irq_unmask,
494 .irq_set_type = bcm_kona_gpio_irq_set_type,
495 .irq_request_resources = bcm_kona_gpio_irq_reqres,
496 .irq_release_resources = bcm_kona_gpio_irq_relres,
499 static struct __initconst of_device_id bcm_kona_gpio_of_match[] = {
500 { .compatible = "brcm,kona-gpio" },
504 MODULE_DEVICE_TABLE(of, bcm_kona_gpio_of_match);
507 * This lock class tells lockdep that GPIO irqs are in a different
508 * category than their parents, so it won't report false recursion.
510 static struct lock_class_key gpio_lock_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);
521 irq_set_chip_and_handler(irq, &bcm_gpio_irq_chip, handle_simple_irq);
523 set_irq_flags(irq, IRQF_VALID);
525 irq_set_noprobe(irq);
531 static void bcm_kona_gpio_irq_unmap(struct irq_domain *d, unsigned int irq)
533 irq_set_chip_and_handler(irq, NULL, NULL);
534 irq_set_chip_data(irq, NULL);
537 static struct irq_domain_ops bcm_kona_irq_ops = {
538 .map = bcm_kona_gpio_irq_map,
539 .unmap = bcm_kona_gpio_irq_unmap,
540 .xlate = irq_domain_xlate_twocell,
543 static void bcm_kona_gpio_reset(struct bcm_kona_gpio *kona_gpio)
545 void __iomem *reg_base;
548 reg_base = kona_gpio->reg_base;
549 /* disable interrupts and clear status */
550 for (i = 0; i < kona_gpio->num_bank; i++) {
551 /* Unlock the entire bank first */
552 bcm_kona_gpio_write_lock_regs(kona_gpio, i, UNLOCK_CODE);
553 writel(0xffffffff, reg_base + GPIO_INT_MASK(i));
554 writel(0xffffffff, reg_base + GPIO_INT_STATUS(i));
555 /* Now re-lock the bank */
556 bcm_kona_gpio_write_lock_regs(kona_gpio, i, LOCK_CODE);
560 static int bcm_kona_gpio_probe(struct platform_device *pdev)
562 struct device *dev = &pdev->dev;
563 const struct of_device_id *match;
564 struct resource *res;
565 struct bcm_kona_gpio_bank *bank;
566 struct bcm_kona_gpio *kona_gpio;
567 struct gpio_chip *chip;
571 match = of_match_device(bcm_kona_gpio_of_match, dev);
573 dev_err(dev, "Failed to find gpio controller\n");
577 kona_gpio = devm_kzalloc(dev, sizeof(*kona_gpio), GFP_KERNEL);
581 kona_gpio->gpio_chip = template_chip;
582 chip = &kona_gpio->gpio_chip;
583 kona_gpio->num_bank = of_irq_count(dev->of_node);
584 if (kona_gpio->num_bank == 0) {
585 dev_err(dev, "Couldn't determine # GPIO banks\n");
588 if (kona_gpio->num_bank > GPIO_MAX_BANK_NUM) {
589 dev_err(dev, "Too many GPIO banks configured (max=%d)\n",
593 kona_gpio->banks = devm_kzalloc(dev,
594 kona_gpio->num_bank *
595 sizeof(*kona_gpio->banks), GFP_KERNEL);
596 if (!kona_gpio->banks)
599 kona_gpio->pdev = pdev;
600 platform_set_drvdata(pdev, kona_gpio);
601 chip->of_node = dev->of_node;
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 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
614 kona_gpio->reg_base = devm_ioremap_resource(dev, res);
615 if (IS_ERR(kona_gpio->reg_base)) {
620 for (i = 0; i < kona_gpio->num_bank; i++) {
621 bank = &kona_gpio->banks[i];
623 bank->irq = platform_get_irq(pdev, i);
624 bank->kona_gpio = kona_gpio;
626 dev_err(dev, "Couldn't get IRQ for bank %d", i);
632 dev_info(&pdev->dev, "Setting up Kona GPIO\n");
634 bcm_kona_gpio_reset(kona_gpio);
636 ret = gpiochip_add(chip);
638 dev_err(dev, "Couldn't add GPIO chip -- %d\n", ret);
641 for (i = 0; i < chip->ngpio; i++) {
642 int irq = bcm_kona_gpio_to_irq(chip, i);
643 irq_set_lockdep_class(irq, &gpio_lock_class);
644 irq_set_chip_and_handler(irq, &bcm_gpio_irq_chip,
647 set_irq_flags(irq, IRQF_VALID);
649 irq_set_noprobe(irq);
652 for (i = 0; i < kona_gpio->num_bank; i++) {
653 bank = &kona_gpio->banks[i];
654 irq_set_chained_handler(bank->irq, bcm_kona_gpio_irq_handler);
655 irq_set_handler_data(bank->irq, bank);
658 spin_lock_init(&kona_gpio->lock);
663 irq_domain_remove(kona_gpio->irq_domain);
668 static struct platform_driver bcm_kona_gpio_driver = {
670 .name = "bcm-kona-gpio",
671 .owner = THIS_MODULE,
672 .of_match_table = bcm_kona_gpio_of_match,
674 .probe = bcm_kona_gpio_probe,
677 module_platform_driver(bcm_kona_gpio_driver);
679 MODULE_AUTHOR("Broadcom Corporation <bcm-kernel-feedback-list@broadcom.com>");
680 MODULE_DESCRIPTION("Broadcom Kona GPIO Driver");
681 MODULE_LICENSE("GPL v2");