1 // SPDX-License-Identifier: GPL-2.0+
3 * Driver for Broadcom BCM2835 GPIO unit (pinctrl + GPIO)
5 * Copyright (C) 2012 Chris Boot, Simon Arlott, Stephen Warren
7 * This driver is inspired by:
8 * pinctrl-nomadik.c, please see original file for copyright information
9 * pinctrl-tegra.c, please see original file for copyright information
12 #include <linux/bitmap.h>
13 #include <linux/bug.h>
14 #include <linux/delay.h>
15 #include <linux/device.h>
16 #include <linux/err.h>
17 #include <linux/gpio/driver.h>
19 #include <linux/irq.h>
20 #include <linux/irqdesc.h>
21 #include <linux/init.h>
22 #include <linux/interrupt.h>
23 #include <linux/module.h>
24 #include <linux/of_address.h>
26 #include <linux/of_irq.h>
27 #include <linux/pinctrl/consumer.h>
28 #include <linux/pinctrl/machine.h>
29 #include <linux/pinctrl/pinconf.h>
30 #include <linux/pinctrl/pinctrl.h>
31 #include <linux/pinctrl/pinmux.h>
32 #include <linux/pinctrl/pinconf-generic.h>
33 #include <linux/platform_device.h>
34 #include <linux/seq_file.h>
35 #include <linux/slab.h>
36 #include <linux/spinlock.h>
37 #include <linux/string_choices.h>
38 #include <linux/types.h>
39 #include <dt-bindings/pinctrl/bcm2835.h>
41 #define MODULE_NAME "pinctrl-bcm2835"
42 #define BCM2835_NUM_GPIOS 54
43 #define BCM2711_NUM_GPIOS 58
44 #define BCM2835_NUM_BANKS 2
45 #define BCM2835_NUM_IRQS 3
47 /* GPIO register offsets */
48 #define GPFSEL0 0x0 /* Function Select */
49 #define GPSET0 0x1c /* Pin Output Set */
50 #define GPCLR0 0x28 /* Pin Output Clear */
51 #define GPLEV0 0x34 /* Pin Level */
52 #define GPEDS0 0x40 /* Pin Event Detect Status */
53 #define GPREN0 0x4c /* Pin Rising Edge Detect Enable */
54 #define GPFEN0 0x58 /* Pin Falling Edge Detect Enable */
55 #define GPHEN0 0x64 /* Pin High Detect Enable */
56 #define GPLEN0 0x70 /* Pin Low Detect Enable */
57 #define GPAREN0 0x7c /* Pin Async Rising Edge Detect */
58 #define GPAFEN0 0x88 /* Pin Async Falling Edge Detect */
59 #define GPPUD 0x94 /* Pin Pull-up/down Enable */
60 #define GPPUDCLK0 0x98 /* Pin Pull-up/down Enable Clock */
61 #define GP_GPIO_PUP_PDN_CNTRL_REG0 0xe4 /* 2711 Pin Pull-up/down select */
63 #define FSEL_REG(p) (GPFSEL0 + (((p) / 10) * 4))
64 #define FSEL_SHIFT(p) (((p) % 10) * 3)
65 #define GPIO_REG_OFFSET(p) ((p) / 32)
66 #define GPIO_REG_SHIFT(p) ((p) % 32)
68 #define PUD_2711_MASK 0x3
69 #define PUD_2711_REG_OFFSET(p) ((p) / 16)
70 #define PUD_2711_REG_SHIFT(p) (((p) % 16) * 2)
72 /* argument: bcm2835_pinconf_pull */
73 #define BCM2835_PINCONF_PARAM_PULL (PIN_CONFIG_END + 1)
75 #define BCM2711_PULL_NONE 0x0
76 #define BCM2711_PULL_UP 0x1
77 #define BCM2711_PULL_DOWN 0x2
79 struct bcm2835_pinctrl {
84 /* note: locking assumes each bank will have its own unsigned long */
85 unsigned long enabled_irq_map[BCM2835_NUM_BANKS];
86 unsigned int irq_type[BCM2711_NUM_GPIOS];
88 struct pinctrl_dev *pctl_dev;
89 struct gpio_chip gpio_chip;
90 struct pinctrl_desc pctl_desc;
91 struct pinctrl_gpio_range gpio_range;
93 raw_spinlock_t irq_lock[BCM2835_NUM_BANKS];
94 /* Protect FSEL registers */
98 /* pins are just named GPIO0..GPIO53 */
99 #define BCM2835_GPIO_PIN(a) PINCTRL_PIN(a, "gpio" #a)
100 static struct pinctrl_pin_desc bcm2835_gpio_pins[] = {
111 BCM2835_GPIO_PIN(10),
112 BCM2835_GPIO_PIN(11),
113 BCM2835_GPIO_PIN(12),
114 BCM2835_GPIO_PIN(13),
115 BCM2835_GPIO_PIN(14),
116 BCM2835_GPIO_PIN(15),
117 BCM2835_GPIO_PIN(16),
118 BCM2835_GPIO_PIN(17),
119 BCM2835_GPIO_PIN(18),
120 BCM2835_GPIO_PIN(19),
121 BCM2835_GPIO_PIN(20),
122 BCM2835_GPIO_PIN(21),
123 BCM2835_GPIO_PIN(22),
124 BCM2835_GPIO_PIN(23),
125 BCM2835_GPIO_PIN(24),
126 BCM2835_GPIO_PIN(25),
127 BCM2835_GPIO_PIN(26),
128 BCM2835_GPIO_PIN(27),
129 BCM2835_GPIO_PIN(28),
130 BCM2835_GPIO_PIN(29),
131 BCM2835_GPIO_PIN(30),
132 BCM2835_GPIO_PIN(31),
133 BCM2835_GPIO_PIN(32),
134 BCM2835_GPIO_PIN(33),
135 BCM2835_GPIO_PIN(34),
136 BCM2835_GPIO_PIN(35),
137 BCM2835_GPIO_PIN(36),
138 BCM2835_GPIO_PIN(37),
139 BCM2835_GPIO_PIN(38),
140 BCM2835_GPIO_PIN(39),
141 BCM2835_GPIO_PIN(40),
142 BCM2835_GPIO_PIN(41),
143 BCM2835_GPIO_PIN(42),
144 BCM2835_GPIO_PIN(43),
145 BCM2835_GPIO_PIN(44),
146 BCM2835_GPIO_PIN(45),
147 BCM2835_GPIO_PIN(46),
148 BCM2835_GPIO_PIN(47),
149 BCM2835_GPIO_PIN(48),
150 BCM2835_GPIO_PIN(49),
151 BCM2835_GPIO_PIN(50),
152 BCM2835_GPIO_PIN(51),
153 BCM2835_GPIO_PIN(52),
154 BCM2835_GPIO_PIN(53),
155 BCM2835_GPIO_PIN(54),
156 BCM2835_GPIO_PIN(55),
157 BCM2835_GPIO_PIN(56),
158 BCM2835_GPIO_PIN(57),
161 /* one pin per group */
162 static const char * const bcm2835_gpio_groups[] = {
224 BCM2835_FSEL_COUNT = 8,
225 BCM2835_FSEL_MASK = 0x7,
228 static const char * const bcm2835_functions[BCM2835_FSEL_COUNT] = {
229 [BCM2835_FSEL_GPIO_IN] = "gpio_in",
230 [BCM2835_FSEL_GPIO_OUT] = "gpio_out",
231 [BCM2835_FSEL_ALT0] = "alt0",
232 [BCM2835_FSEL_ALT1] = "alt1",
233 [BCM2835_FSEL_ALT2] = "alt2",
234 [BCM2835_FSEL_ALT3] = "alt3",
235 [BCM2835_FSEL_ALT4] = "alt4",
236 [BCM2835_FSEL_ALT5] = "alt5",
239 static const char * const irq_type_names[] = {
240 [IRQ_TYPE_NONE] = "none",
241 [IRQ_TYPE_EDGE_RISING] = "edge-rising",
242 [IRQ_TYPE_EDGE_FALLING] = "edge-falling",
243 [IRQ_TYPE_EDGE_BOTH] = "edge-both",
244 [IRQ_TYPE_LEVEL_HIGH] = "level-high",
245 [IRQ_TYPE_LEVEL_LOW] = "level-low",
248 static bool persist_gpio_outputs;
249 module_param(persist_gpio_outputs, bool, 0444);
250 MODULE_PARM_DESC(persist_gpio_outputs, "Enable GPIO_OUT persistence when pin is freed");
252 static inline u32 bcm2835_gpio_rd(struct bcm2835_pinctrl *pc, unsigned reg)
254 return readl(pc->base + reg);
257 static inline void bcm2835_gpio_wr(struct bcm2835_pinctrl *pc, unsigned reg,
260 writel(val, pc->base + reg);
263 static inline int bcm2835_gpio_get_bit(struct bcm2835_pinctrl *pc, unsigned reg,
266 reg += GPIO_REG_OFFSET(bit) * 4;
267 return (bcm2835_gpio_rd(pc, reg) >> GPIO_REG_SHIFT(bit)) & 1;
270 /* note NOT a read/modify/write cycle */
271 static inline void bcm2835_gpio_set_bit(struct bcm2835_pinctrl *pc,
272 unsigned reg, unsigned bit)
274 reg += GPIO_REG_OFFSET(bit) * 4;
275 bcm2835_gpio_wr(pc, reg, BIT(GPIO_REG_SHIFT(bit)));
278 static inline enum bcm2835_fsel bcm2835_pinctrl_fsel_get(
279 struct bcm2835_pinctrl *pc, unsigned pin)
281 u32 val = bcm2835_gpio_rd(pc, FSEL_REG(pin));
282 enum bcm2835_fsel status = (val >> FSEL_SHIFT(pin)) & BCM2835_FSEL_MASK;
284 dev_dbg(pc->dev, "get %08x (%u => %s)\n", val, pin,
285 bcm2835_functions[status]);
290 static inline void bcm2835_pinctrl_fsel_set(
291 struct bcm2835_pinctrl *pc, unsigned pin,
292 enum bcm2835_fsel fsel)
295 enum bcm2835_fsel cur;
298 spin_lock_irqsave(&pc->fsel_lock, flags);
299 val = bcm2835_gpio_rd(pc, FSEL_REG(pin));
300 cur = (val >> FSEL_SHIFT(pin)) & BCM2835_FSEL_MASK;
302 dev_dbg(pc->dev, "read %08x (%u => %s)\n", val, pin,
303 bcm2835_functions[cur]);
308 if (cur != BCM2835_FSEL_GPIO_IN && fsel != BCM2835_FSEL_GPIO_IN) {
309 /* always transition through GPIO_IN */
310 val &= ~(BCM2835_FSEL_MASK << FSEL_SHIFT(pin));
311 val |= BCM2835_FSEL_GPIO_IN << FSEL_SHIFT(pin);
313 dev_dbg(pc->dev, "trans %08x (%u <= %s)\n", val, pin,
314 bcm2835_functions[BCM2835_FSEL_GPIO_IN]);
315 bcm2835_gpio_wr(pc, FSEL_REG(pin), val);
318 val &= ~(BCM2835_FSEL_MASK << FSEL_SHIFT(pin));
319 val |= fsel << FSEL_SHIFT(pin);
321 dev_dbg(pc->dev, "write %08x (%u <= %s)\n", val, pin,
322 bcm2835_functions[fsel]);
323 bcm2835_gpio_wr(pc, FSEL_REG(pin), val);
326 spin_unlock_irqrestore(&pc->fsel_lock, flags);
329 static int bcm2835_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
331 struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
333 bcm2835_pinctrl_fsel_set(pc, offset, BCM2835_FSEL_GPIO_IN);
337 static int bcm2835_gpio_get(struct gpio_chip *chip, unsigned offset)
339 struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
341 return bcm2835_gpio_get_bit(pc, GPLEV0, offset);
344 static int bcm2835_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
346 struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
347 enum bcm2835_fsel fsel = bcm2835_pinctrl_fsel_get(pc, offset);
349 /* Alternative function doesn't clearly provide a direction */
350 if (fsel > BCM2835_FSEL_GPIO_OUT)
353 if (fsel == BCM2835_FSEL_GPIO_IN)
354 return GPIO_LINE_DIRECTION_IN;
356 return GPIO_LINE_DIRECTION_OUT;
359 static void bcm2835_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
361 struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
363 bcm2835_gpio_set_bit(pc, value ? GPSET0 : GPCLR0, offset);
366 static int bcm2835_gpio_direction_output(struct gpio_chip *chip,
367 unsigned offset, int value)
369 struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
371 bcm2835_gpio_set_bit(pc, value ? GPSET0 : GPCLR0, offset);
372 bcm2835_pinctrl_fsel_set(pc, offset, BCM2835_FSEL_GPIO_OUT);
376 static int bcm2835_add_pin_ranges_fallback(struct gpio_chip *gc)
378 struct device_node *np = dev_of_node(gc->parent);
379 struct pinctrl_dev *pctldev = of_pinctrl_get(np);
384 return gpiochip_add_pin_range(gc, pinctrl_dev_get_devname(pctldev), 0, 0,
388 static const struct gpio_chip bcm2835_gpio_chip = {
389 .label = MODULE_NAME,
390 .owner = THIS_MODULE,
391 .request = gpiochip_generic_request,
392 .free = gpiochip_generic_free,
393 .direction_input = bcm2835_gpio_direction_input,
394 .direction_output = bcm2835_gpio_direction_output,
395 .get_direction = bcm2835_gpio_get_direction,
396 .get = bcm2835_gpio_get,
397 .set = bcm2835_gpio_set,
398 .set_config = gpiochip_generic_config,
400 .ngpio = BCM2835_NUM_GPIOS,
402 .add_pin_ranges = bcm2835_add_pin_ranges_fallback,
405 static const struct gpio_chip bcm2711_gpio_chip = {
406 .label = "pinctrl-bcm2711",
407 .owner = THIS_MODULE,
408 .request = gpiochip_generic_request,
409 .free = gpiochip_generic_free,
410 .direction_input = bcm2835_gpio_direction_input,
411 .direction_output = bcm2835_gpio_direction_output,
412 .get_direction = bcm2835_gpio_get_direction,
413 .get = bcm2835_gpio_get,
414 .set = bcm2835_gpio_set,
415 .set_config = gpiochip_generic_config,
417 .ngpio = BCM2711_NUM_GPIOS,
419 .add_pin_ranges = bcm2835_add_pin_ranges_fallback,
422 static void bcm2835_gpio_irq_handle_bank(struct bcm2835_pinctrl *pc,
423 unsigned int bank, u32 mask)
425 unsigned long events;
429 events = bcm2835_gpio_rd(pc, GPEDS0 + bank * 4);
431 events &= pc->enabled_irq_map[bank];
432 for_each_set_bit(offset, &events, 32) {
433 gpio = (32 * bank) + offset;
434 generic_handle_domain_irq(pc->gpio_chip.irq.domain,
439 static void bcm2835_gpio_irq_handler(struct irq_desc *desc)
441 struct gpio_chip *chip = irq_desc_get_handler_data(desc);
442 struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
443 struct irq_chip *host_chip = irq_desc_get_chip(desc);
444 int irq = irq_desc_get_irq(desc);
448 for (i = 0; i < BCM2835_NUM_IRQS; i++) {
449 if (chip->irq.parents[i] == irq) {
454 /* This should not happen, every IRQ has a bank */
455 BUG_ON(i == BCM2835_NUM_IRQS);
457 chained_irq_enter(host_chip, desc);
460 case 0: /* IRQ0 covers GPIOs 0-27 */
461 bcm2835_gpio_irq_handle_bank(pc, 0, 0x0fffffff);
463 case 1: /* IRQ1 covers GPIOs 28-45 */
464 bcm2835_gpio_irq_handle_bank(pc, 0, 0xf0000000);
465 bcm2835_gpio_irq_handle_bank(pc, 1, 0x00003fff);
467 case 2: /* IRQ2 covers GPIOs 46-57 */
468 bcm2835_gpio_irq_handle_bank(pc, 1, 0x003fc000);
472 chained_irq_exit(host_chip, desc);
475 static irqreturn_t bcm2835_gpio_wake_irq_handler(int irq, void *dev_id)
480 static inline void __bcm2835_gpio_irq_config(struct bcm2835_pinctrl *pc,
481 unsigned reg, unsigned offset, bool enable)
484 reg += GPIO_REG_OFFSET(offset) * 4;
485 value = bcm2835_gpio_rd(pc, reg);
487 value |= BIT(GPIO_REG_SHIFT(offset));
489 value &= ~(BIT(GPIO_REG_SHIFT(offset)));
490 bcm2835_gpio_wr(pc, reg, value);
493 /* fast path for IRQ handler */
494 static void bcm2835_gpio_irq_config(struct bcm2835_pinctrl *pc,
495 unsigned offset, bool enable)
497 switch (pc->irq_type[offset]) {
498 case IRQ_TYPE_EDGE_RISING:
499 __bcm2835_gpio_irq_config(pc, GPREN0, offset, enable);
502 case IRQ_TYPE_EDGE_FALLING:
503 __bcm2835_gpio_irq_config(pc, GPFEN0, offset, enable);
506 case IRQ_TYPE_EDGE_BOTH:
507 __bcm2835_gpio_irq_config(pc, GPREN0, offset, enable);
508 __bcm2835_gpio_irq_config(pc, GPFEN0, offset, enable);
511 case IRQ_TYPE_LEVEL_HIGH:
512 __bcm2835_gpio_irq_config(pc, GPHEN0, offset, enable);
515 case IRQ_TYPE_LEVEL_LOW:
516 __bcm2835_gpio_irq_config(pc, GPLEN0, offset, enable);
521 static void bcm2835_gpio_irq_unmask(struct irq_data *data)
523 struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
524 struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
525 unsigned gpio = irqd_to_hwirq(data);
526 unsigned offset = GPIO_REG_SHIFT(gpio);
527 unsigned bank = GPIO_REG_OFFSET(gpio);
530 gpiochip_enable_irq(chip, gpio);
532 raw_spin_lock_irqsave(&pc->irq_lock[bank], flags);
533 set_bit(offset, &pc->enabled_irq_map[bank]);
534 bcm2835_gpio_irq_config(pc, gpio, true);
535 raw_spin_unlock_irqrestore(&pc->irq_lock[bank], flags);
538 static void bcm2835_gpio_irq_mask(struct irq_data *data)
540 struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
541 struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
542 unsigned gpio = irqd_to_hwirq(data);
543 unsigned offset = GPIO_REG_SHIFT(gpio);
544 unsigned bank = GPIO_REG_OFFSET(gpio);
547 raw_spin_lock_irqsave(&pc->irq_lock[bank], flags);
548 bcm2835_gpio_irq_config(pc, gpio, false);
549 /* Clear events that were latched prior to clearing event sources */
550 bcm2835_gpio_set_bit(pc, GPEDS0, gpio);
551 clear_bit(offset, &pc->enabled_irq_map[bank]);
552 raw_spin_unlock_irqrestore(&pc->irq_lock[bank], flags);
554 gpiochip_disable_irq(chip, gpio);
557 static int __bcm2835_gpio_irq_set_type_disabled(struct bcm2835_pinctrl *pc,
558 unsigned offset, unsigned int type)
562 case IRQ_TYPE_EDGE_RISING:
563 case IRQ_TYPE_EDGE_FALLING:
564 case IRQ_TYPE_EDGE_BOTH:
565 case IRQ_TYPE_LEVEL_HIGH:
566 case IRQ_TYPE_LEVEL_LOW:
567 pc->irq_type[offset] = type;
576 /* slower path for reconfiguring IRQ type */
577 static int __bcm2835_gpio_irq_set_type_enabled(struct bcm2835_pinctrl *pc,
578 unsigned offset, unsigned int type)
582 if (pc->irq_type[offset] != type) {
583 bcm2835_gpio_irq_config(pc, offset, false);
584 pc->irq_type[offset] = type;
588 case IRQ_TYPE_EDGE_RISING:
589 if (pc->irq_type[offset] == IRQ_TYPE_EDGE_BOTH) {
590 /* RISING already enabled, disable FALLING */
591 pc->irq_type[offset] = IRQ_TYPE_EDGE_FALLING;
592 bcm2835_gpio_irq_config(pc, offset, false);
593 pc->irq_type[offset] = type;
594 } else if (pc->irq_type[offset] != type) {
595 bcm2835_gpio_irq_config(pc, offset, false);
596 pc->irq_type[offset] = type;
597 bcm2835_gpio_irq_config(pc, offset, true);
601 case IRQ_TYPE_EDGE_FALLING:
602 if (pc->irq_type[offset] == IRQ_TYPE_EDGE_BOTH) {
603 /* FALLING already enabled, disable RISING */
604 pc->irq_type[offset] = IRQ_TYPE_EDGE_RISING;
605 bcm2835_gpio_irq_config(pc, offset, false);
606 pc->irq_type[offset] = type;
607 } else if (pc->irq_type[offset] != type) {
608 bcm2835_gpio_irq_config(pc, offset, false);
609 pc->irq_type[offset] = type;
610 bcm2835_gpio_irq_config(pc, offset, true);
614 case IRQ_TYPE_EDGE_BOTH:
615 if (pc->irq_type[offset] == IRQ_TYPE_EDGE_RISING) {
616 /* RISING already enabled, enable FALLING too */
617 pc->irq_type[offset] = IRQ_TYPE_EDGE_FALLING;
618 bcm2835_gpio_irq_config(pc, offset, true);
619 pc->irq_type[offset] = type;
620 } else if (pc->irq_type[offset] == IRQ_TYPE_EDGE_FALLING) {
621 /* FALLING already enabled, enable RISING too */
622 pc->irq_type[offset] = IRQ_TYPE_EDGE_RISING;
623 bcm2835_gpio_irq_config(pc, offset, true);
624 pc->irq_type[offset] = type;
625 } else if (pc->irq_type[offset] != type) {
626 bcm2835_gpio_irq_config(pc, offset, false);
627 pc->irq_type[offset] = type;
628 bcm2835_gpio_irq_config(pc, offset, true);
632 case IRQ_TYPE_LEVEL_HIGH:
633 case IRQ_TYPE_LEVEL_LOW:
634 if (pc->irq_type[offset] != type) {
635 bcm2835_gpio_irq_config(pc, offset, false);
636 pc->irq_type[offset] = type;
637 bcm2835_gpio_irq_config(pc, offset, true);
647 static int bcm2835_gpio_irq_set_type(struct irq_data *data, unsigned int type)
649 struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
650 struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
651 unsigned gpio = irqd_to_hwirq(data);
652 unsigned offset = GPIO_REG_SHIFT(gpio);
653 unsigned bank = GPIO_REG_OFFSET(gpio);
657 raw_spin_lock_irqsave(&pc->irq_lock[bank], flags);
659 if (test_bit(offset, &pc->enabled_irq_map[bank]))
660 ret = __bcm2835_gpio_irq_set_type_enabled(pc, gpio, type);
662 ret = __bcm2835_gpio_irq_set_type_disabled(pc, gpio, type);
664 if (type & IRQ_TYPE_EDGE_BOTH)
665 irq_set_handler_locked(data, handle_edge_irq);
667 irq_set_handler_locked(data, handle_level_irq);
669 raw_spin_unlock_irqrestore(&pc->irq_lock[bank], flags);
674 static void bcm2835_gpio_irq_ack(struct irq_data *data)
676 struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
677 struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
678 unsigned gpio = irqd_to_hwirq(data);
680 bcm2835_gpio_set_bit(pc, GPEDS0, gpio);
683 static int bcm2835_gpio_irq_set_wake(struct irq_data *data, unsigned int on)
685 struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
686 struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
687 unsigned gpio = irqd_to_hwirq(data);
688 unsigned int irqgroup;
696 else if (gpio >= 28 && gpio <= 45)
698 else if (gpio >= 46 && gpio <= 57)
704 ret = enable_irq_wake(pc->wake_irq[irqgroup]);
706 ret = disable_irq_wake(pc->wake_irq[irqgroup]);
711 static const struct irq_chip bcm2835_gpio_irq_chip = {
713 .irq_set_type = bcm2835_gpio_irq_set_type,
714 .irq_ack = bcm2835_gpio_irq_ack,
715 .irq_mask = bcm2835_gpio_irq_mask,
716 .irq_unmask = bcm2835_gpio_irq_unmask,
717 .irq_set_wake = bcm2835_gpio_irq_set_wake,
718 .flags = (IRQCHIP_MASK_ON_SUSPEND | IRQCHIP_IMMUTABLE),
719 GPIOCHIP_IRQ_RESOURCE_HELPERS,
722 static int bcm2835_pctl_get_groups_count(struct pinctrl_dev *pctldev)
724 return BCM2835_NUM_GPIOS;
727 static const char *bcm2835_pctl_get_group_name(struct pinctrl_dev *pctldev,
730 return bcm2835_gpio_groups[selector];
733 static int bcm2835_pctl_get_group_pins(struct pinctrl_dev *pctldev,
735 const unsigned **pins,
738 *pins = &bcm2835_gpio_pins[selector].number;
744 static void bcm2835_pctl_pin_dbg_show(struct pinctrl_dev *pctldev,
748 struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
749 struct gpio_chip *chip = &pc->gpio_chip;
750 enum bcm2835_fsel fsel = bcm2835_pinctrl_fsel_get(pc, offset);
751 const char *fname = bcm2835_functions[fsel];
752 int value = bcm2835_gpio_get_bit(pc, GPLEV0, offset);
753 int irq = irq_find_mapping(chip->irq.domain, offset);
755 seq_printf(s, "function %s in %s; irq %d (%s)",
756 fname, str_hi_lo(value),
757 irq, irq_type_names[pc->irq_type[offset]]);
760 static void bcm2835_pctl_dt_free_map(struct pinctrl_dev *pctldev,
761 struct pinctrl_map *maps, unsigned num_maps)
765 for (i = 0; i < num_maps; i++)
766 if (maps[i].type == PIN_MAP_TYPE_CONFIGS_PIN)
767 kfree(maps[i].data.configs.configs);
772 static int bcm2835_pctl_dt_node_to_map_func(struct bcm2835_pinctrl *pc,
773 struct device_node *np, u32 pin, u32 fnum,
774 struct pinctrl_map **maps)
776 struct pinctrl_map *map = *maps;
778 if (fnum >= ARRAY_SIZE(bcm2835_functions)) {
779 dev_err(pc->dev, "%pOF: invalid brcm,function %d\n", np, fnum);
783 map->type = PIN_MAP_TYPE_MUX_GROUP;
784 map->data.mux.group = bcm2835_gpio_groups[pin];
785 map->data.mux.function = bcm2835_functions[fnum];
791 static int bcm2835_pctl_dt_node_to_map_pull(struct bcm2835_pinctrl *pc,
792 struct device_node *np, u32 pin, u32 pull,
793 struct pinctrl_map **maps)
795 struct pinctrl_map *map = *maps;
796 unsigned long *configs;
799 dev_err(pc->dev, "%pOF: invalid brcm,pull %d\n", np, pull);
803 configs = kzalloc(sizeof(*configs), GFP_KERNEL);
806 configs[0] = pinconf_to_config_packed(BCM2835_PINCONF_PARAM_PULL, pull);
808 map->type = PIN_MAP_TYPE_CONFIGS_PIN;
809 map->data.configs.group_or_pin = bcm2835_gpio_pins[pin].name;
810 map->data.configs.configs = configs;
811 map->data.configs.num_configs = 1;
817 static int bcm2835_pctl_dt_node_to_map(struct pinctrl_dev *pctldev,
818 struct device_node *np,
819 struct pinctrl_map **map, unsigned int *num_maps)
821 struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
822 struct property *pins, *funcs, *pulls;
823 int num_pins, num_funcs, num_pulls, maps_per_pin;
824 struct pinctrl_map *maps, *cur_map;
828 /* Check for generic binding in this node */
829 err = pinconf_generic_dt_node_to_map_all(pctldev, np, map, num_maps);
830 if (err || *num_maps)
833 /* Generic binding did not find anything continue with legacy parse */
834 pins = of_find_property(np, "brcm,pins", NULL);
836 dev_err(pc->dev, "%pOF: missing brcm,pins property\n", np);
840 funcs = of_find_property(np, "brcm,function", NULL);
841 pulls = of_find_property(np, "brcm,pull", NULL);
843 if (!funcs && !pulls) {
845 "%pOF: neither brcm,function nor brcm,pull specified\n",
850 num_pins = pins->length / 4;
851 num_funcs = funcs ? (funcs->length / 4) : 0;
852 num_pulls = pulls ? (pulls->length / 4) : 0;
854 if (num_funcs > 1 && num_funcs != num_pins) {
856 "%pOF: brcm,function must have 1 or %d entries\n",
861 if (num_pulls > 1 && num_pulls != num_pins) {
863 "%pOF: brcm,pull must have 1 or %d entries\n",
873 cur_map = maps = kcalloc(num_pins * maps_per_pin, sizeof(*maps),
878 for (i = 0; i < num_pins; i++) {
879 err = of_property_read_u32_index(np, "brcm,pins", i, &pin);
882 if (pin >= pc->pctl_desc.npins) {
883 dev_err(pc->dev, "%pOF: invalid brcm,pins value %d\n",
890 err = of_property_read_u32_index(np, "brcm,function",
891 (num_funcs > 1) ? i : 0, &func);
894 err = bcm2835_pctl_dt_node_to_map_func(pc, np, pin,
900 err = of_property_read_u32_index(np, "brcm,pull",
901 (num_pulls > 1) ? i : 0, &pull);
904 err = bcm2835_pctl_dt_node_to_map_pull(pc, np, pin,
912 *num_maps = num_pins * maps_per_pin;
917 bcm2835_pctl_dt_free_map(pctldev, maps, num_pins * maps_per_pin);
921 static const struct pinctrl_ops bcm2835_pctl_ops = {
922 .get_groups_count = bcm2835_pctl_get_groups_count,
923 .get_group_name = bcm2835_pctl_get_group_name,
924 .get_group_pins = bcm2835_pctl_get_group_pins,
925 .pin_dbg_show = bcm2835_pctl_pin_dbg_show,
926 .dt_node_to_map = bcm2835_pctl_dt_node_to_map,
927 .dt_free_map = bcm2835_pctl_dt_free_map,
930 static int bcm2835_pmx_free(struct pinctrl_dev *pctldev,
933 struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
934 enum bcm2835_fsel fsel = bcm2835_pinctrl_fsel_get(pc, offset);
936 if (fsel == BCM2835_FSEL_GPIO_IN)
939 if (persist_gpio_outputs && fsel == BCM2835_FSEL_GPIO_OUT)
942 /* disable by setting to GPIO_IN */
943 bcm2835_pinctrl_fsel_set(pc, offset, BCM2835_FSEL_GPIO_IN);
947 static int bcm2835_pmx_get_functions_count(struct pinctrl_dev *pctldev)
949 return BCM2835_FSEL_COUNT;
952 static const char *bcm2835_pmx_get_function_name(struct pinctrl_dev *pctldev,
955 return bcm2835_functions[selector];
958 static int bcm2835_pmx_get_function_groups(struct pinctrl_dev *pctldev,
960 const char * const **groups,
961 unsigned * const num_groups)
963 /* every pin can do every function */
964 *groups = bcm2835_gpio_groups;
965 *num_groups = BCM2835_NUM_GPIOS;
970 static int bcm2835_pmx_set(struct pinctrl_dev *pctldev,
971 unsigned func_selector,
972 unsigned group_selector)
974 struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
976 bcm2835_pinctrl_fsel_set(pc, group_selector, func_selector);
981 static void bcm2835_pmx_gpio_disable_free(struct pinctrl_dev *pctldev,
982 struct pinctrl_gpio_range *range,
985 bcm2835_pmx_free(pctldev, offset);
988 static int bcm2835_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
989 struct pinctrl_gpio_range *range,
993 struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
994 enum bcm2835_fsel fsel = input ?
995 BCM2835_FSEL_GPIO_IN : BCM2835_FSEL_GPIO_OUT;
997 bcm2835_pinctrl_fsel_set(pc, offset, fsel);
1002 static const struct pinmux_ops bcm2835_pmx_ops = {
1003 .free = bcm2835_pmx_free,
1004 .get_functions_count = bcm2835_pmx_get_functions_count,
1005 .get_function_name = bcm2835_pmx_get_function_name,
1006 .get_function_groups = bcm2835_pmx_get_function_groups,
1007 .set_mux = bcm2835_pmx_set,
1008 .gpio_disable_free = bcm2835_pmx_gpio_disable_free,
1009 .gpio_set_direction = bcm2835_pmx_gpio_set_direction,
1012 static int bcm2835_pinconf_get(struct pinctrl_dev *pctldev,
1013 unsigned pin, unsigned long *config)
1015 enum pin_config_param param = pinconf_to_config_param(*config);
1016 struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
1017 enum bcm2835_fsel fsel = bcm2835_pinctrl_fsel_get(pc, pin);
1020 /* No way to read back bias config in HW */
1023 case PIN_CONFIG_OUTPUT:
1024 if (fsel != BCM2835_FSEL_GPIO_OUT)
1027 val = bcm2835_gpio_get_bit(pc, GPLEV0, pin);
1028 *config = pinconf_to_config_packed(param, val);
1038 static void bcm2835_pull_config_set(struct bcm2835_pinctrl *pc,
1039 unsigned int pin, unsigned int arg)
1043 off = GPIO_REG_OFFSET(pin);
1044 bit = GPIO_REG_SHIFT(pin);
1046 bcm2835_gpio_wr(pc, GPPUD, arg & 3);
1048 * BCM2835 datasheet say to wait 150 cycles, but not of what.
1049 * But the VideoCore firmware delay for this operation
1050 * based nearly on the same amount of VPU cycles and this clock
1054 bcm2835_gpio_wr(pc, GPPUDCLK0 + (off * 4), BIT(bit));
1056 bcm2835_gpio_wr(pc, GPPUDCLK0 + (off * 4), 0);
1059 static int bcm2835_pinconf_set(struct pinctrl_dev *pctldev,
1060 unsigned int pin, unsigned long *configs,
1061 unsigned int num_configs)
1063 struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
1067 for (i = 0; i < num_configs; i++) {
1068 param = pinconf_to_config_param(configs[i]);
1069 arg = pinconf_to_config_argument(configs[i]);
1072 /* Set legacy brcm,pull */
1073 case BCM2835_PINCONF_PARAM_PULL:
1074 bcm2835_pull_config_set(pc, pin, arg);
1077 /* Set pull generic bindings */
1078 case PIN_CONFIG_BIAS_DISABLE:
1079 bcm2835_pull_config_set(pc, pin, BCM2835_PUD_OFF);
1082 case PIN_CONFIG_BIAS_PULL_DOWN:
1083 bcm2835_pull_config_set(pc, pin, BCM2835_PUD_DOWN);
1086 case PIN_CONFIG_BIAS_PULL_UP:
1087 bcm2835_pull_config_set(pc, pin, BCM2835_PUD_UP);
1090 /* Set output-high or output-low */
1091 case PIN_CONFIG_OUTPUT:
1092 bcm2835_gpio_set_bit(pc, arg ? GPSET0 : GPCLR0, pin);
1098 } /* switch param type */
1099 } /* for each config */
1104 static const struct pinconf_ops bcm2835_pinconf_ops = {
1106 .pin_config_get = bcm2835_pinconf_get,
1107 .pin_config_set = bcm2835_pinconf_set,
1110 static int bcm2711_pinconf_get(struct pinctrl_dev *pctldev, unsigned pin,
1111 unsigned long *config)
1113 struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
1114 enum pin_config_param param = pinconf_to_config_param(*config);
1115 u32 offset, shift, val;
1117 offset = PUD_2711_REG_OFFSET(pin);
1118 shift = PUD_2711_REG_SHIFT(pin);
1119 val = bcm2835_gpio_rd(pc, GP_GPIO_PUP_PDN_CNTRL_REG0 + (offset * 4));
1122 case PIN_CONFIG_BIAS_DISABLE:
1123 if (((val >> shift) & PUD_2711_MASK) != BCM2711_PULL_NONE)
1128 case PIN_CONFIG_BIAS_PULL_UP:
1129 if (((val >> shift) & PUD_2711_MASK) != BCM2711_PULL_UP)
1132 *config = pinconf_to_config_packed(param, 50000);
1135 case PIN_CONFIG_BIAS_PULL_DOWN:
1136 if (((val >> shift) & PUD_2711_MASK) != BCM2711_PULL_DOWN)
1139 *config = pinconf_to_config_packed(param, 50000);
1143 return bcm2835_pinconf_get(pctldev, pin, config);
1149 static void bcm2711_pull_config_set(struct bcm2835_pinctrl *pc,
1150 unsigned int pin, unsigned int arg)
1156 off = PUD_2711_REG_OFFSET(pin);
1157 shifter = PUD_2711_REG_SHIFT(pin);
1159 value = bcm2835_gpio_rd(pc, GP_GPIO_PUP_PDN_CNTRL_REG0 + (off * 4));
1160 value &= ~(PUD_2711_MASK << shifter);
1161 value |= (arg << shifter);
1162 bcm2835_gpio_wr(pc, GP_GPIO_PUP_PDN_CNTRL_REG0 + (off * 4), value);
1165 static int bcm2711_pinconf_set(struct pinctrl_dev *pctldev,
1166 unsigned int pin, unsigned long *configs,
1167 unsigned int num_configs)
1169 struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
1173 for (i = 0; i < num_configs; i++) {
1174 param = pinconf_to_config_param(configs[i]);
1175 arg = pinconf_to_config_argument(configs[i]);
1178 /* convert legacy brcm,pull */
1179 case BCM2835_PINCONF_PARAM_PULL:
1180 if (arg == BCM2835_PUD_UP)
1181 arg = BCM2711_PULL_UP;
1182 else if (arg == BCM2835_PUD_DOWN)
1183 arg = BCM2711_PULL_DOWN;
1185 arg = BCM2711_PULL_NONE;
1187 bcm2711_pull_config_set(pc, pin, arg);
1190 /* Set pull generic bindings */
1191 case PIN_CONFIG_BIAS_DISABLE:
1192 bcm2711_pull_config_set(pc, pin, BCM2711_PULL_NONE);
1194 case PIN_CONFIG_BIAS_PULL_DOWN:
1195 bcm2711_pull_config_set(pc, pin, BCM2711_PULL_DOWN);
1197 case PIN_CONFIG_BIAS_PULL_UP:
1198 bcm2711_pull_config_set(pc, pin, BCM2711_PULL_UP);
1201 /* Set output-high or output-low */
1202 case PIN_CONFIG_OUTPUT:
1203 bcm2835_gpio_set_bit(pc, arg ? GPSET0 : GPCLR0, pin);
1209 } /* for each config */
1214 static const struct pinconf_ops bcm2711_pinconf_ops = {
1216 .pin_config_get = bcm2711_pinconf_get,
1217 .pin_config_set = bcm2711_pinconf_set,
1220 static const struct pinctrl_desc bcm2835_pinctrl_desc = {
1221 .name = MODULE_NAME,
1222 .pins = bcm2835_gpio_pins,
1223 .npins = BCM2835_NUM_GPIOS,
1224 .pctlops = &bcm2835_pctl_ops,
1225 .pmxops = &bcm2835_pmx_ops,
1226 .confops = &bcm2835_pinconf_ops,
1227 .owner = THIS_MODULE,
1230 static const struct pinctrl_desc bcm2711_pinctrl_desc = {
1231 .name = "pinctrl-bcm2711",
1232 .pins = bcm2835_gpio_pins,
1233 .npins = BCM2711_NUM_GPIOS,
1234 .pctlops = &bcm2835_pctl_ops,
1235 .pmxops = &bcm2835_pmx_ops,
1236 .confops = &bcm2711_pinconf_ops,
1237 .owner = THIS_MODULE,
1240 static const struct pinctrl_gpio_range bcm2835_pinctrl_gpio_range = {
1241 .name = MODULE_NAME,
1242 .npins = BCM2835_NUM_GPIOS,
1245 static const struct pinctrl_gpio_range bcm2711_pinctrl_gpio_range = {
1246 .name = "pinctrl-bcm2711",
1247 .npins = BCM2711_NUM_GPIOS,
1250 struct bcm_plat_data {
1251 const struct gpio_chip *gpio_chip;
1252 const struct pinctrl_desc *pctl_desc;
1253 const struct pinctrl_gpio_range *gpio_range;
1256 static const struct bcm_plat_data bcm2835_plat_data = {
1257 .gpio_chip = &bcm2835_gpio_chip,
1258 .pctl_desc = &bcm2835_pinctrl_desc,
1259 .gpio_range = &bcm2835_pinctrl_gpio_range,
1262 static const struct bcm_plat_data bcm2711_plat_data = {
1263 .gpio_chip = &bcm2711_gpio_chip,
1264 .pctl_desc = &bcm2711_pinctrl_desc,
1265 .gpio_range = &bcm2711_pinctrl_gpio_range,
1268 static const struct of_device_id bcm2835_pinctrl_match[] = {
1270 .compatible = "brcm,bcm2835-gpio",
1271 .data = &bcm2835_plat_data,
1274 .compatible = "brcm,bcm2711-gpio",
1275 .data = &bcm2711_plat_data,
1278 .compatible = "brcm,bcm7211-gpio",
1279 .data = &bcm2711_plat_data,
1283 MODULE_DEVICE_TABLE(of, bcm2835_pinctrl_match);
1285 static int bcm2835_pinctrl_probe(struct platform_device *pdev)
1287 struct device *dev = &pdev->dev;
1288 struct device_node *np = dev->of_node;
1289 const struct bcm_plat_data *pdata;
1290 struct bcm2835_pinctrl *pc;
1291 struct gpio_irq_chip *girq;
1292 struct resource iomem;
1294 const struct of_device_id *match;
1297 BUILD_BUG_ON(ARRAY_SIZE(bcm2835_gpio_pins) != BCM2711_NUM_GPIOS);
1298 BUILD_BUG_ON(ARRAY_SIZE(bcm2835_gpio_groups) != BCM2711_NUM_GPIOS);
1300 pc = devm_kzalloc(dev, sizeof(*pc), GFP_KERNEL);
1304 platform_set_drvdata(pdev, pc);
1307 err = of_address_to_resource(np, 0, &iomem);
1309 dev_err(dev, "could not get IO memory\n");
1313 pc->base = devm_ioremap_resource(dev, &iomem);
1314 if (IS_ERR(pc->base))
1315 return PTR_ERR(pc->base);
1317 match = of_match_node(bcm2835_pinctrl_match, pdev->dev.of_node);
1321 pdata = match->data;
1322 is_7211 = of_device_is_compatible(np, "brcm,bcm7211-gpio");
1324 pc->gpio_chip = *pdata->gpio_chip;
1325 pc->gpio_chip.parent = dev;
1327 spin_lock_init(&pc->fsel_lock);
1328 for (i = 0; i < BCM2835_NUM_BANKS; i++) {
1329 unsigned long events;
1332 /* clear event detection flags */
1333 bcm2835_gpio_wr(pc, GPREN0 + i * 4, 0);
1334 bcm2835_gpio_wr(pc, GPFEN0 + i * 4, 0);
1335 bcm2835_gpio_wr(pc, GPHEN0 + i * 4, 0);
1336 bcm2835_gpio_wr(pc, GPLEN0 + i * 4, 0);
1337 bcm2835_gpio_wr(pc, GPAREN0 + i * 4, 0);
1338 bcm2835_gpio_wr(pc, GPAFEN0 + i * 4, 0);
1340 /* clear all the events */
1341 events = bcm2835_gpio_rd(pc, GPEDS0 + i * 4);
1342 for_each_set_bit(offset, &events, 32)
1343 bcm2835_gpio_wr(pc, GPEDS0 + i * 4, BIT(offset));
1345 raw_spin_lock_init(&pc->irq_lock[i]);
1348 pc->pctl_desc = *pdata->pctl_desc;
1349 pc->pctl_dev = devm_pinctrl_register(dev, &pc->pctl_desc, pc);
1350 if (IS_ERR(pc->pctl_dev)) {
1351 gpiochip_remove(&pc->gpio_chip);
1352 return PTR_ERR(pc->pctl_dev);
1355 pc->gpio_range = *pdata->gpio_range;
1356 pc->gpio_range.base = pc->gpio_chip.base;
1357 pc->gpio_range.gc = &pc->gpio_chip;
1358 pinctrl_add_gpio_range(pc->pctl_dev, &pc->gpio_range);
1360 girq = &pc->gpio_chip.irq;
1361 gpio_irq_chip_set_chip(girq, &bcm2835_gpio_irq_chip);
1362 girq->parent_handler = bcm2835_gpio_irq_handler;
1363 girq->num_parents = BCM2835_NUM_IRQS;
1364 girq->parents = devm_kcalloc(dev, BCM2835_NUM_IRQS,
1365 sizeof(*girq->parents),
1367 if (!girq->parents) {
1373 pc->wake_irq = devm_kcalloc(dev, BCM2835_NUM_IRQS,
1374 sizeof(*pc->wake_irq),
1376 if (!pc->wake_irq) {
1383 * Use the same handler for all groups: this is necessary
1384 * since we use one gpiochip to cover all lines - the
1385 * irq handler then needs to figure out which group and
1386 * bank that was firing the IRQ and look up the per-group
1389 for (i = 0; i < BCM2835_NUM_IRQS; i++) {
1393 girq->parents[i] = irq_of_parse_and_map(np, i);
1395 if (!girq->parents[i]) {
1396 girq->num_parents = i;
1401 /* Skip over the all banks interrupts */
1402 pc->wake_irq[i] = irq_of_parse_and_map(np, i +
1403 BCM2835_NUM_IRQS + 1);
1405 len = strlen(dev_name(pc->dev)) + 16;
1406 name = devm_kzalloc(pc->dev, len, GFP_KERNEL);
1412 snprintf(name, len, "%s:bank%d", dev_name(pc->dev), i);
1414 /* These are optional interrupts */
1415 err = devm_request_irq(dev, pc->wake_irq[i],
1416 bcm2835_gpio_wake_irq_handler,
1417 IRQF_SHARED, name, pc);
1419 dev_warn(dev, "unable to request wake IRQ %d\n",
1423 girq->default_type = IRQ_TYPE_NONE;
1424 girq->handler = handle_level_irq;
1426 err = gpiochip_add_data(&pc->gpio_chip, pc);
1428 dev_err(dev, "could not add GPIO chip\n");
1432 dev_info(dev, "GPIO_OUT persistence: %s\n",
1433 str_yes_no(persist_gpio_outputs));
1438 pinctrl_remove_gpio_range(pc->pctl_dev, &pc->gpio_range);
1442 static struct platform_driver bcm2835_pinctrl_driver = {
1443 .probe = bcm2835_pinctrl_probe,
1445 .name = MODULE_NAME,
1446 .of_match_table = bcm2835_pinctrl_match,
1447 .suppress_bind_attrs = true,
1450 module_platform_driver(bcm2835_pinctrl_driver);
1452 MODULE_AUTHOR("Chris Boot");
1453 MODULE_AUTHOR("Simon Arlott");
1454 MODULE_AUTHOR("Stephen Warren");
1455 MODULE_DESCRIPTION("Broadcom BCM2835/2711 pinctrl and GPIO driver");
1456 MODULE_LICENSE("GPL");