Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

pinctrl: bcm2835: switch to GPIOLIB_IRQCHIP

It should be possible to use the GPIOLIB_IRQCHIP helper
library with the BCM2835 driver since it is a pretty straight
forward cascaded irqchip.

The only difference from other drivers is that the BCM2835
has several banks for a single gpiochip, and each bank has
a separate IRQ line. Instead of creating one gpiochip per
bank, a single gpiochip covers all banks GPIO lines. This
makes it necessary to resolve the bank ID in the IRQ
handler.

The GPIOLIB_IRQCHIP allows several IRQs to be cascaded off
the same gpiochip by calling gpiochip_set_chained_irqchip()
repeatedly, but we have been a bit short on examples
for how this should be handled in practice, so this is intended
as an example of how this can be achieved.

The old code did not model the chip as a chained interrupt
handler, but this patch also rectifies that situation.

Cc: Stephen Warren <swarren@wwwdotorg.org>
Tested-by: Stefan Wahren <stefan.wahren@i2se.com>
Tested-by: Eric Anholt <eric@anholt.net>
Acked-by: Eric Anholt <eric@anholt.net>
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>

+65 -76
+1
drivers/pinctrl/bcm/Kconfig
··· 20 20 bool 21 21 select PINMUX 22 22 select PINCONF 23 + select GPIOLIB_IRQCHIP 23 24 24 25 config PINCTRL_IPROC_GPIO 25 26 bool "Broadcom iProc GPIO (with PINCONF) driver"
+64 -76
drivers/pinctrl/bcm/pinctrl-bcm2835.c
··· 24 24 #include <linux/device.h> 25 25 #include <linux/err.h> 26 26 #include <linux/gpio/driver.h> 27 - #include <linux/interrupt.h> 28 27 #include <linux/io.h> 29 28 #include <linux/irq.h> 30 29 #include <linux/irqdesc.h> 31 - #include <linux/irqdomain.h> 32 30 #include <linux/module.h> 33 31 #include <linux/of_address.h> 34 32 #include <linux/of.h> ··· 85 87 #define BCM2835_PINCONF_UNPACK_PARAM(_conf_) ((_conf_) >> 16) 86 88 #define BCM2835_PINCONF_UNPACK_ARG(_conf_) ((_conf_) & 0xffff) 87 89 88 - struct bcm2835_gpio_irqdata { 89 - struct bcm2835_pinctrl *pc; 90 - int irqgroup; 91 - }; 92 - 93 90 struct bcm2835_pinctrl { 94 91 struct device *dev; 95 92 void __iomem *base; ··· 95 102 unsigned int irq_type[BCM2835_NUM_GPIOS]; 96 103 97 104 struct pinctrl_dev *pctl_dev; 98 - struct irq_domain *irq_domain; 99 105 struct gpio_chip gpio_chip; 100 106 struct pinctrl_gpio_range gpio_range; 101 107 102 - struct bcm2835_gpio_irqdata irq_data[BCM2835_NUM_IRQS]; 108 + int irq_group[BCM2835_NUM_IRQS]; 103 109 spinlock_t irq_lock[BCM2835_NUM_BANKS]; 104 110 }; 105 - 106 - static struct lock_class_key gpio_lock_class; 107 111 108 112 /* pins are just named GPIO0..GPIO53 */ 109 113 #define BCM2835_GPIO_PIN(a) PINCTRL_PIN(a, "gpio" #a) ··· 359 369 return pinctrl_gpio_direction_output(chip->base + offset); 360 370 } 361 371 362 - static int bcm2835_gpio_to_irq(struct gpio_chip *chip, unsigned offset) 363 - { 364 - struct bcm2835_pinctrl *pc = gpiochip_get_data(chip); 365 - 366 - return irq_linear_revmap(pc->irq_domain, offset); 367 - } 368 - 369 372 static struct gpio_chip bcm2835_gpio_chip = { 370 373 .label = MODULE_NAME, 371 374 .owner = THIS_MODULE, ··· 369 386 .get_direction = bcm2835_gpio_get_direction, 370 387 .get = bcm2835_gpio_get, 371 388 .set = bcm2835_gpio_set, 372 - .to_irq = bcm2835_gpio_to_irq, 373 389 .base = -1, 374 390 .ngpio = BCM2835_NUM_GPIOS, 375 391 .can_sleep = false, 376 392 }; 377 393 378 - static int bcm2835_gpio_irq_handle_bank(struct bcm2835_pinctrl *pc, 379 - unsigned int bank, u32 mask) 394 + static void bcm2835_gpio_irq_handle_bank(struct bcm2835_pinctrl *pc, 395 + unsigned int bank, u32 mask) 380 396 { 381 397 unsigned long events; 382 398 unsigned offset; ··· 387 405 events &= pc->enabled_irq_map[bank]; 388 406 for_each_set_bit(offset, &events, 32) { 389 407 gpio = (32 * bank) + offset; 408 + /* FIXME: no clue why the code looks up the type here */ 390 409 type = pc->irq_type[gpio]; 391 410 392 - generic_handle_irq(irq_linear_revmap(pc->irq_domain, gpio)); 411 + generic_handle_irq(irq_linear_revmap(pc->gpio_chip.irqdomain, 412 + gpio)); 393 413 } 394 - 395 - return (events != 0); 396 414 } 397 415 398 - static irqreturn_t bcm2835_gpio_irq_handler(int irq, void *dev_id) 416 + static void bcm2835_gpio_irq_handler(struct irq_desc *desc) 399 417 { 400 - struct bcm2835_gpio_irqdata *irqdata = dev_id; 401 - struct bcm2835_pinctrl *pc = irqdata->pc; 402 - int handled = 0; 418 + struct gpio_chip *chip = irq_desc_get_handler_data(desc); 419 + struct bcm2835_pinctrl *pc = gpiochip_get_data(chip); 420 + struct irq_chip *host_chip = irq_desc_get_chip(desc); 421 + int irq = irq_desc_get_irq(desc); 422 + int group; 423 + int i; 403 424 404 - switch (irqdata->irqgroup) { 425 + for (i = 0; i < ARRAY_SIZE(pc->irq); i++) { 426 + if (pc->irq[i] == irq) { 427 + group = pc->irq_group[i]; 428 + break; 429 + } 430 + } 431 + /* This should not happen, every IRQ has a bank */ 432 + if (i == ARRAY_SIZE(pc->irq)) 433 + BUG(); 434 + 435 + chained_irq_enter(host_chip, desc); 436 + 437 + switch (group) { 405 438 case 0: /* IRQ0 covers GPIOs 0-27 */ 406 - handled = bcm2835_gpio_irq_handle_bank(pc, 0, 0x0fffffff); 439 + bcm2835_gpio_irq_handle_bank(pc, 0, 0x0fffffff); 407 440 break; 408 441 case 1: /* IRQ1 covers GPIOs 28-45 */ 409 - handled = bcm2835_gpio_irq_handle_bank(pc, 0, 0xf0000000) | 410 - bcm2835_gpio_irq_handle_bank(pc, 1, 0x00003fff); 442 + bcm2835_gpio_irq_handle_bank(pc, 0, 0xf0000000); 443 + bcm2835_gpio_irq_handle_bank(pc, 1, 0x00003fff); 411 444 break; 412 445 case 2: /* IRQ2 covers GPIOs 46-53 */ 413 - handled = bcm2835_gpio_irq_handle_bank(pc, 1, 0x003fc000); 446 + bcm2835_gpio_irq_handle_bank(pc, 1, 0x003fc000); 414 447 break; 415 448 } 416 449 417 - return handled ? IRQ_HANDLED : IRQ_NONE; 450 + chained_irq_exit(host_chip, desc); 418 451 } 419 452 420 453 static inline void __bcm2835_gpio_irq_config(struct bcm2835_pinctrl *pc, ··· 475 478 476 479 static void bcm2835_gpio_irq_enable(struct irq_data *data) 477 480 { 478 - struct bcm2835_pinctrl *pc = irq_data_get_irq_chip_data(data); 481 + struct gpio_chip *chip = irq_data_get_irq_chip_data(data); 482 + struct bcm2835_pinctrl *pc = gpiochip_get_data(chip); 479 483 unsigned gpio = irqd_to_hwirq(data); 480 484 unsigned offset = GPIO_REG_SHIFT(gpio); 481 485 unsigned bank = GPIO_REG_OFFSET(gpio); ··· 490 492 491 493 static void bcm2835_gpio_irq_disable(struct irq_data *data) 492 494 { 493 - struct bcm2835_pinctrl *pc = irq_data_get_irq_chip_data(data); 495 + struct gpio_chip *chip = irq_data_get_irq_chip_data(data); 496 + struct bcm2835_pinctrl *pc = gpiochip_get_data(chip); 494 497 unsigned gpio = irqd_to_hwirq(data); 495 498 unsigned offset = GPIO_REG_SHIFT(gpio); 496 499 unsigned bank = GPIO_REG_OFFSET(gpio); ··· 597 598 598 599 static int bcm2835_gpio_irq_set_type(struct irq_data *data, unsigned int type) 599 600 { 600 - struct bcm2835_pinctrl *pc = irq_data_get_irq_chip_data(data); 601 + struct gpio_chip *chip = irq_data_get_irq_chip_data(data); 602 + struct bcm2835_pinctrl *pc = gpiochip_get_data(chip); 601 603 unsigned gpio = irqd_to_hwirq(data); 602 604 unsigned offset = GPIO_REG_SHIFT(gpio); 603 605 unsigned bank = GPIO_REG_OFFSET(gpio); ··· 624 624 625 625 static void bcm2835_gpio_irq_ack(struct irq_data *data) 626 626 { 627 - struct bcm2835_pinctrl *pc = irq_data_get_irq_chip_data(data); 627 + struct gpio_chip *chip = irq_data_get_irq_chip_data(data); 628 + struct bcm2835_pinctrl *pc = gpiochip_get_data(chip); 628 629 unsigned gpio = irqd_to_hwirq(data); 629 630 630 631 bcm2835_gpio_set_bit(pc, GPEDS0, gpio); ··· 668 667 unsigned offset) 669 668 { 670 669 struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev); 670 + struct gpio_chip *chip = &pc->gpio_chip; 671 671 enum bcm2835_fsel fsel = bcm2835_pinctrl_fsel_get(pc, offset); 672 672 const char *fname = bcm2835_functions[fsel]; 673 673 int value = bcm2835_gpio_get_bit(pc, GPLEV0, offset); 674 - int irq = irq_find_mapping(pc->irq_domain, offset); 674 + int irq = irq_find_mapping(chip->irqdomain, offset); 675 675 676 676 seq_printf(s, "function %s in %s; irq %d (%s)", 677 677 fname, value ? "hi" : "lo", ··· 1018 1016 pc->gpio_chip.parent = dev; 1019 1017 pc->gpio_chip.of_node = np; 1020 1018 1021 - pc->irq_domain = irq_domain_add_linear(np, BCM2835_NUM_GPIOS, 1022 - &irq_domain_simple_ops, NULL); 1023 - if (!pc->irq_domain) { 1024 - dev_err(dev, "could not create IRQ domain\n"); 1025 - return -ENOMEM; 1026 - } 1027 - 1028 - for (i = 0; i < BCM2835_NUM_GPIOS; i++) { 1029 - int irq = irq_create_mapping(pc->irq_domain, i); 1030 - irq_set_lockdep_class(irq, &gpio_lock_class); 1031 - irq_set_chip_and_handler(irq, &bcm2835_gpio_irq_chip, 1032 - handle_level_irq); 1033 - irq_set_chip_data(irq, pc); 1034 - } 1035 - 1036 1019 for (i = 0; i < BCM2835_NUM_BANKS; i++) { 1037 1020 unsigned long events; 1038 1021 unsigned offset; ··· 1038 1051 spin_lock_init(&pc->irq_lock[i]); 1039 1052 } 1040 1053 1041 - for (i = 0; i < BCM2835_NUM_IRQS; i++) { 1042 - int len; 1043 - char *name; 1044 - pc->irq[i] = irq_of_parse_and_map(np, i); 1045 - pc->irq_data[i].pc = pc; 1046 - pc->irq_data[i].irqgroup = i; 1047 - 1048 - len = strlen(dev_name(pc->dev)) + 16; 1049 - name = devm_kzalloc(pc->dev, len, GFP_KERNEL); 1050 - if (!name) 1051 - return -ENOMEM; 1052 - snprintf(name, len, "%s:bank%d", dev_name(pc->dev), i); 1053 - 1054 - err = devm_request_irq(dev, pc->irq[i], 1055 - bcm2835_gpio_irq_handler, IRQF_SHARED, 1056 - name, &pc->irq_data[i]); 1057 - if (err) { 1058 - dev_err(dev, "unable to request IRQ %d\n", pc->irq[i]); 1059 - return err; 1060 - } 1061 - } 1062 - 1063 1054 err = gpiochip_add_data(&pc->gpio_chip, pc); 1064 1055 if (err) { 1065 1056 dev_err(dev, "could not add GPIO chip\n"); 1066 1057 return err; 1058 + } 1059 + 1060 + err = gpiochip_irqchip_add(&pc->gpio_chip, &bcm2835_gpio_irq_chip, 1061 + 0, handle_level_irq, IRQ_TYPE_NONE); 1062 + if (err) { 1063 + dev_info(dev, "could not add irqchip\n"); 1064 + return err; 1065 + } 1066 + 1067 + for (i = 0; i < BCM2835_NUM_IRQS; i++) { 1068 + pc->irq[i] = irq_of_parse_and_map(np, i); 1069 + pc->irq_group[i] = i; 1070 + /* 1071 + * Use the same handler for all groups: this is necessary 1072 + * since we use one gpiochip to cover all lines - the 1073 + * irq handler then needs to figure out which group and 1074 + * bank that was firing the IRQ and look up the per-group 1075 + * and bank data. 1076 + */ 1077 + gpiochip_set_chained_irqchip(&pc->gpio_chip, 1078 + &bcm2835_gpio_irq_chip, 1079 + pc->irq[i], 1080 + bcm2835_gpio_irq_handler); 1067 1081 } 1068 1082 1069 1083 pc->pctl_dev = devm_pinctrl_register(dev, &bcm2835_pinctrl_desc, pc);