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/types.h>
38 #include <dt-bindings/pinctrl/bcm2835.h>
40 #define MODULE_NAME "pinctrl-bcm2835"
41 #define BCM2835_NUM_GPIOS 54
42 #define BCM2711_NUM_GPIOS 58
43 #define BCM2835_NUM_BANKS 2
44 #define BCM2835_NUM_IRQS 3
46 /* GPIO register offsets */
47 #define GPFSEL0 0x0 /* Function Select */
48 #define GPSET0 0x1c /* Pin Output Set */
49 #define GPCLR0 0x28 /* Pin Output Clear */
50 #define GPLEV0 0x34 /* Pin Level */
51 #define GPEDS0 0x40 /* Pin Event Detect Status */
52 #define GPREN0 0x4c /* Pin Rising Edge Detect Enable */
53 #define GPFEN0 0x58 /* Pin Falling Edge Detect Enable */
54 #define GPHEN0 0x64 /* Pin High Detect Enable */
55 #define GPLEN0 0x70 /* Pin Low Detect Enable */
56 #define GPAREN0 0x7c /* Pin Async Rising Edge Detect */
57 #define GPAFEN0 0x88 /* Pin Async Falling Edge Detect */
58 #define GPPUD 0x94 /* Pin Pull-up/down Enable */
59 #define GPPUDCLK0 0x98 /* Pin Pull-up/down Enable Clock */
60 #define GP_GPIO_PUP_PDN_CNTRL_REG0 0xe4 /* 2711 Pin Pull-up/down select */
62 #define FSEL_REG(p) (GPFSEL0 + (((p) / 10) * 4))
63 #define FSEL_SHIFT(p) (((p) % 10) * 3)
64 #define GPIO_REG_OFFSET(p) ((p) / 32)
65 #define GPIO_REG_SHIFT(p) ((p) % 32)
67 #define PUD_2711_MASK 0x3
68 #define PUD_2711_REG_OFFSET(p) ((p) / 16)
69 #define PUD_2711_REG_SHIFT(p) (((p) % 16) * 2)
71 /* argument: bcm2835_pinconf_pull */
72 #define BCM2835_PINCONF_PARAM_PULL (PIN_CONFIG_END + 1)
74 #define BCM2711_PULL_NONE 0x0
75 #define BCM2711_PULL_UP 0x1
76 #define BCM2711_PULL_DOWN 0x2
78 struct bcm2835_pinctrl {
83 /* note: locking assumes each bank will have its own unsigned long */
84 unsigned long enabled_irq_map[BCM2835_NUM_BANKS];
85 unsigned int irq_type[BCM2711_NUM_GPIOS];
87 struct pinctrl_dev *pctl_dev;
88 struct gpio_chip gpio_chip;
89 struct pinctrl_desc pctl_desc;
90 struct pinctrl_gpio_range gpio_range;
92 raw_spinlock_t irq_lock[BCM2835_NUM_BANKS];
95 /* pins are just named GPIO0..GPIO53 */
96 #define BCM2835_GPIO_PIN(a) PINCTRL_PIN(a, "gpio" #a)
97 static struct pinctrl_pin_desc bcm2835_gpio_pins[] = {
108 BCM2835_GPIO_PIN(10),
109 BCM2835_GPIO_PIN(11),
110 BCM2835_GPIO_PIN(12),
111 BCM2835_GPIO_PIN(13),
112 BCM2835_GPIO_PIN(14),
113 BCM2835_GPIO_PIN(15),
114 BCM2835_GPIO_PIN(16),
115 BCM2835_GPIO_PIN(17),
116 BCM2835_GPIO_PIN(18),
117 BCM2835_GPIO_PIN(19),
118 BCM2835_GPIO_PIN(20),
119 BCM2835_GPIO_PIN(21),
120 BCM2835_GPIO_PIN(22),
121 BCM2835_GPIO_PIN(23),
122 BCM2835_GPIO_PIN(24),
123 BCM2835_GPIO_PIN(25),
124 BCM2835_GPIO_PIN(26),
125 BCM2835_GPIO_PIN(27),
126 BCM2835_GPIO_PIN(28),
127 BCM2835_GPIO_PIN(29),
128 BCM2835_GPIO_PIN(30),
129 BCM2835_GPIO_PIN(31),
130 BCM2835_GPIO_PIN(32),
131 BCM2835_GPIO_PIN(33),
132 BCM2835_GPIO_PIN(34),
133 BCM2835_GPIO_PIN(35),
134 BCM2835_GPIO_PIN(36),
135 BCM2835_GPIO_PIN(37),
136 BCM2835_GPIO_PIN(38),
137 BCM2835_GPIO_PIN(39),
138 BCM2835_GPIO_PIN(40),
139 BCM2835_GPIO_PIN(41),
140 BCM2835_GPIO_PIN(42),
141 BCM2835_GPIO_PIN(43),
142 BCM2835_GPIO_PIN(44),
143 BCM2835_GPIO_PIN(45),
144 BCM2835_GPIO_PIN(46),
145 BCM2835_GPIO_PIN(47),
146 BCM2835_GPIO_PIN(48),
147 BCM2835_GPIO_PIN(49),
148 BCM2835_GPIO_PIN(50),
149 BCM2835_GPIO_PIN(51),
150 BCM2835_GPIO_PIN(52),
151 BCM2835_GPIO_PIN(53),
152 BCM2835_GPIO_PIN(54),
153 BCM2835_GPIO_PIN(55),
154 BCM2835_GPIO_PIN(56),
155 BCM2835_GPIO_PIN(57),
158 /* one pin per group */
159 static const char * const bcm2835_gpio_groups[] = {
221 BCM2835_FSEL_COUNT = 8,
222 BCM2835_FSEL_MASK = 0x7,
225 static const char * const bcm2835_functions[BCM2835_FSEL_COUNT] = {
226 [BCM2835_FSEL_GPIO_IN] = "gpio_in",
227 [BCM2835_FSEL_GPIO_OUT] = "gpio_out",
228 [BCM2835_FSEL_ALT0] = "alt0",
229 [BCM2835_FSEL_ALT1] = "alt1",
230 [BCM2835_FSEL_ALT2] = "alt2",
231 [BCM2835_FSEL_ALT3] = "alt3",
232 [BCM2835_FSEL_ALT4] = "alt4",
233 [BCM2835_FSEL_ALT5] = "alt5",
236 static const char * const irq_type_names[] = {
237 [IRQ_TYPE_NONE] = "none",
238 [IRQ_TYPE_EDGE_RISING] = "edge-rising",
239 [IRQ_TYPE_EDGE_FALLING] = "edge-falling",
240 [IRQ_TYPE_EDGE_BOTH] = "edge-both",
241 [IRQ_TYPE_LEVEL_HIGH] = "level-high",
242 [IRQ_TYPE_LEVEL_LOW] = "level-low",
245 static inline u32 bcm2835_gpio_rd(struct bcm2835_pinctrl *pc, unsigned reg)
247 return readl(pc->base + reg);
250 static inline void bcm2835_gpio_wr(struct bcm2835_pinctrl *pc, unsigned reg,
253 writel(val, pc->base + reg);
256 static inline int bcm2835_gpio_get_bit(struct bcm2835_pinctrl *pc, unsigned reg,
259 reg += GPIO_REG_OFFSET(bit) * 4;
260 return (bcm2835_gpio_rd(pc, reg) >> GPIO_REG_SHIFT(bit)) & 1;
263 /* note NOT a read/modify/write cycle */
264 static inline void bcm2835_gpio_set_bit(struct bcm2835_pinctrl *pc,
265 unsigned reg, unsigned bit)
267 reg += GPIO_REG_OFFSET(bit) * 4;
268 bcm2835_gpio_wr(pc, reg, BIT(GPIO_REG_SHIFT(bit)));
271 static inline enum bcm2835_fsel bcm2835_pinctrl_fsel_get(
272 struct bcm2835_pinctrl *pc, unsigned pin)
274 u32 val = bcm2835_gpio_rd(pc, FSEL_REG(pin));
275 enum bcm2835_fsel status = (val >> FSEL_SHIFT(pin)) & BCM2835_FSEL_MASK;
277 dev_dbg(pc->dev, "get %08x (%u => %s)\n", val, pin,
278 bcm2835_functions[status]);
283 static inline void bcm2835_pinctrl_fsel_set(
284 struct bcm2835_pinctrl *pc, unsigned pin,
285 enum bcm2835_fsel fsel)
287 u32 val = bcm2835_gpio_rd(pc, FSEL_REG(pin));
288 enum bcm2835_fsel cur = (val >> FSEL_SHIFT(pin)) & BCM2835_FSEL_MASK;
290 dev_dbg(pc->dev, "read %08x (%u => %s)\n", val, pin,
291 bcm2835_functions[cur]);
296 if (cur != BCM2835_FSEL_GPIO_IN && fsel != BCM2835_FSEL_GPIO_IN) {
297 /* always transition through GPIO_IN */
298 val &= ~(BCM2835_FSEL_MASK << FSEL_SHIFT(pin));
299 val |= BCM2835_FSEL_GPIO_IN << FSEL_SHIFT(pin);
301 dev_dbg(pc->dev, "trans %08x (%u <= %s)\n", val, pin,
302 bcm2835_functions[BCM2835_FSEL_GPIO_IN]);
303 bcm2835_gpio_wr(pc, FSEL_REG(pin), val);
306 val &= ~(BCM2835_FSEL_MASK << FSEL_SHIFT(pin));
307 val |= fsel << FSEL_SHIFT(pin);
309 dev_dbg(pc->dev, "write %08x (%u <= %s)\n", val, pin,
310 bcm2835_functions[fsel]);
311 bcm2835_gpio_wr(pc, FSEL_REG(pin), val);
314 static int bcm2835_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
316 struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
318 bcm2835_pinctrl_fsel_set(pc, offset, BCM2835_FSEL_GPIO_IN);
322 static int bcm2835_gpio_get(struct gpio_chip *chip, unsigned offset)
324 struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
326 return bcm2835_gpio_get_bit(pc, GPLEV0, offset);
329 static int bcm2835_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
331 struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
332 enum bcm2835_fsel fsel = bcm2835_pinctrl_fsel_get(pc, offset);
334 /* Alternative function doesn't clearly provide a direction */
335 if (fsel > BCM2835_FSEL_GPIO_OUT)
338 if (fsel == BCM2835_FSEL_GPIO_IN)
339 return GPIO_LINE_DIRECTION_IN;
341 return GPIO_LINE_DIRECTION_OUT;
344 static void bcm2835_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
346 struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
348 bcm2835_gpio_set_bit(pc, value ? GPSET0 : GPCLR0, offset);
351 static int bcm2835_gpio_direction_output(struct gpio_chip *chip,
352 unsigned offset, int value)
354 struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
356 bcm2835_gpio_set_bit(pc, value ? GPSET0 : GPCLR0, offset);
357 bcm2835_pinctrl_fsel_set(pc, offset, BCM2835_FSEL_GPIO_OUT);
361 static int bcm2835_of_gpio_ranges_fallback(struct gpio_chip *gc,
362 struct device_node *np)
364 struct pinctrl_dev *pctldev = of_pinctrl_get(np);
371 gpiochip_add_pin_range(gc, pinctrl_dev_get_devname(pctldev), 0, 0,
377 static const struct gpio_chip bcm2835_gpio_chip = {
378 .label = MODULE_NAME,
379 .owner = THIS_MODULE,
380 .request = gpiochip_generic_request,
381 .free = gpiochip_generic_free,
382 .direction_input = bcm2835_gpio_direction_input,
383 .direction_output = bcm2835_gpio_direction_output,
384 .get_direction = bcm2835_gpio_get_direction,
385 .get = bcm2835_gpio_get,
386 .set = bcm2835_gpio_set,
387 .set_config = gpiochip_generic_config,
389 .ngpio = BCM2835_NUM_GPIOS,
391 .of_gpio_ranges_fallback = bcm2835_of_gpio_ranges_fallback,
394 static const struct gpio_chip bcm2711_gpio_chip = {
395 .label = "pinctrl-bcm2711",
396 .owner = THIS_MODULE,
397 .request = gpiochip_generic_request,
398 .free = gpiochip_generic_free,
399 .direction_input = bcm2835_gpio_direction_input,
400 .direction_output = bcm2835_gpio_direction_output,
401 .get_direction = bcm2835_gpio_get_direction,
402 .get = bcm2835_gpio_get,
403 .set = bcm2835_gpio_set,
404 .set_config = gpiochip_generic_config,
406 .ngpio = BCM2711_NUM_GPIOS,
408 .of_gpio_ranges_fallback = bcm2835_of_gpio_ranges_fallback,
411 static void bcm2835_gpio_irq_handle_bank(struct bcm2835_pinctrl *pc,
412 unsigned int bank, u32 mask)
414 unsigned long events;
418 events = bcm2835_gpio_rd(pc, GPEDS0 + bank * 4);
420 events &= pc->enabled_irq_map[bank];
421 for_each_set_bit(offset, &events, 32) {
422 gpio = (32 * bank) + offset;
423 generic_handle_domain_irq(pc->gpio_chip.irq.domain,
428 static void bcm2835_gpio_irq_handler(struct irq_desc *desc)
430 struct gpio_chip *chip = irq_desc_get_handler_data(desc);
431 struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
432 struct irq_chip *host_chip = irq_desc_get_chip(desc);
433 int irq = irq_desc_get_irq(desc);
437 for (i = 0; i < BCM2835_NUM_IRQS; i++) {
438 if (chip->irq.parents[i] == irq) {
443 /* This should not happen, every IRQ has a bank */
444 BUG_ON(i == BCM2835_NUM_IRQS);
446 chained_irq_enter(host_chip, desc);
449 case 0: /* IRQ0 covers GPIOs 0-27 */
450 bcm2835_gpio_irq_handle_bank(pc, 0, 0x0fffffff);
452 case 1: /* IRQ1 covers GPIOs 28-45 */
453 bcm2835_gpio_irq_handle_bank(pc, 0, 0xf0000000);
454 bcm2835_gpio_irq_handle_bank(pc, 1, 0x00003fff);
456 case 2: /* IRQ2 covers GPIOs 46-57 */
457 bcm2835_gpio_irq_handle_bank(pc, 1, 0x003fc000);
461 chained_irq_exit(host_chip, desc);
464 static irqreturn_t bcm2835_gpio_wake_irq_handler(int irq, void *dev_id)
469 static inline void __bcm2835_gpio_irq_config(struct bcm2835_pinctrl *pc,
470 unsigned reg, unsigned offset, bool enable)
473 reg += GPIO_REG_OFFSET(offset) * 4;
474 value = bcm2835_gpio_rd(pc, reg);
476 value |= BIT(GPIO_REG_SHIFT(offset));
478 value &= ~(BIT(GPIO_REG_SHIFT(offset)));
479 bcm2835_gpio_wr(pc, reg, value);
482 /* fast path for IRQ handler */
483 static void bcm2835_gpio_irq_config(struct bcm2835_pinctrl *pc,
484 unsigned offset, bool enable)
486 switch (pc->irq_type[offset]) {
487 case IRQ_TYPE_EDGE_RISING:
488 __bcm2835_gpio_irq_config(pc, GPREN0, offset, enable);
491 case IRQ_TYPE_EDGE_FALLING:
492 __bcm2835_gpio_irq_config(pc, GPFEN0, offset, enable);
495 case IRQ_TYPE_EDGE_BOTH:
496 __bcm2835_gpio_irq_config(pc, GPREN0, offset, enable);
497 __bcm2835_gpio_irq_config(pc, GPFEN0, offset, enable);
500 case IRQ_TYPE_LEVEL_HIGH:
501 __bcm2835_gpio_irq_config(pc, GPHEN0, offset, enable);
504 case IRQ_TYPE_LEVEL_LOW:
505 __bcm2835_gpio_irq_config(pc, GPLEN0, offset, enable);
510 static void bcm2835_gpio_irq_unmask(struct irq_data *data)
512 struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
513 struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
514 unsigned gpio = irqd_to_hwirq(data);
515 unsigned offset = GPIO_REG_SHIFT(gpio);
516 unsigned bank = GPIO_REG_OFFSET(gpio);
519 raw_spin_lock_irqsave(&pc->irq_lock[bank], flags);
520 set_bit(offset, &pc->enabled_irq_map[bank]);
521 bcm2835_gpio_irq_config(pc, gpio, true);
522 raw_spin_unlock_irqrestore(&pc->irq_lock[bank], flags);
525 static void bcm2835_gpio_irq_mask(struct irq_data *data)
527 struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
528 struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
529 unsigned gpio = irqd_to_hwirq(data);
530 unsigned offset = GPIO_REG_SHIFT(gpio);
531 unsigned bank = GPIO_REG_OFFSET(gpio);
534 raw_spin_lock_irqsave(&pc->irq_lock[bank], flags);
535 bcm2835_gpio_irq_config(pc, gpio, false);
536 /* Clear events that were latched prior to clearing event sources */
537 bcm2835_gpio_set_bit(pc, GPEDS0, gpio);
538 clear_bit(offset, &pc->enabled_irq_map[bank]);
539 raw_spin_unlock_irqrestore(&pc->irq_lock[bank], flags);
542 static int __bcm2835_gpio_irq_set_type_disabled(struct bcm2835_pinctrl *pc,
543 unsigned offset, unsigned int type)
547 case IRQ_TYPE_EDGE_RISING:
548 case IRQ_TYPE_EDGE_FALLING:
549 case IRQ_TYPE_EDGE_BOTH:
550 case IRQ_TYPE_LEVEL_HIGH:
551 case IRQ_TYPE_LEVEL_LOW:
552 pc->irq_type[offset] = type;
561 /* slower path for reconfiguring IRQ type */
562 static int __bcm2835_gpio_irq_set_type_enabled(struct bcm2835_pinctrl *pc,
563 unsigned offset, unsigned int type)
567 if (pc->irq_type[offset] != type) {
568 bcm2835_gpio_irq_config(pc, offset, false);
569 pc->irq_type[offset] = type;
573 case IRQ_TYPE_EDGE_RISING:
574 if (pc->irq_type[offset] == IRQ_TYPE_EDGE_BOTH) {
575 /* RISING already enabled, disable FALLING */
576 pc->irq_type[offset] = IRQ_TYPE_EDGE_FALLING;
577 bcm2835_gpio_irq_config(pc, offset, false);
578 pc->irq_type[offset] = type;
579 } else if (pc->irq_type[offset] != type) {
580 bcm2835_gpio_irq_config(pc, offset, false);
581 pc->irq_type[offset] = type;
582 bcm2835_gpio_irq_config(pc, offset, true);
586 case IRQ_TYPE_EDGE_FALLING:
587 if (pc->irq_type[offset] == IRQ_TYPE_EDGE_BOTH) {
588 /* FALLING already enabled, disable RISING */
589 pc->irq_type[offset] = IRQ_TYPE_EDGE_RISING;
590 bcm2835_gpio_irq_config(pc, offset, false);
591 pc->irq_type[offset] = type;
592 } else if (pc->irq_type[offset] != type) {
593 bcm2835_gpio_irq_config(pc, offset, false);
594 pc->irq_type[offset] = type;
595 bcm2835_gpio_irq_config(pc, offset, true);
599 case IRQ_TYPE_EDGE_BOTH:
600 if (pc->irq_type[offset] == IRQ_TYPE_EDGE_RISING) {
601 /* RISING already enabled, enable FALLING too */
602 pc->irq_type[offset] = IRQ_TYPE_EDGE_FALLING;
603 bcm2835_gpio_irq_config(pc, offset, true);
604 pc->irq_type[offset] = type;
605 } else if (pc->irq_type[offset] == IRQ_TYPE_EDGE_FALLING) {
606 /* FALLING already enabled, enable RISING too */
607 pc->irq_type[offset] = IRQ_TYPE_EDGE_RISING;
608 bcm2835_gpio_irq_config(pc, offset, true);
609 pc->irq_type[offset] = type;
610 } else if (pc->irq_type[offset] != type) {
611 bcm2835_gpio_irq_config(pc, offset, false);
612 pc->irq_type[offset] = type;
613 bcm2835_gpio_irq_config(pc, offset, true);
617 case IRQ_TYPE_LEVEL_HIGH:
618 case IRQ_TYPE_LEVEL_LOW:
619 if (pc->irq_type[offset] != type) {
620 bcm2835_gpio_irq_config(pc, offset, false);
621 pc->irq_type[offset] = type;
622 bcm2835_gpio_irq_config(pc, offset, true);
632 static int bcm2835_gpio_irq_set_type(struct irq_data *data, unsigned int type)
634 struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
635 struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
636 unsigned gpio = irqd_to_hwirq(data);
637 unsigned offset = GPIO_REG_SHIFT(gpio);
638 unsigned bank = GPIO_REG_OFFSET(gpio);
642 raw_spin_lock_irqsave(&pc->irq_lock[bank], flags);
644 if (test_bit(offset, &pc->enabled_irq_map[bank]))
645 ret = __bcm2835_gpio_irq_set_type_enabled(pc, gpio, type);
647 ret = __bcm2835_gpio_irq_set_type_disabled(pc, gpio, type);
649 if (type & IRQ_TYPE_EDGE_BOTH)
650 irq_set_handler_locked(data, handle_edge_irq);
652 irq_set_handler_locked(data, handle_level_irq);
654 raw_spin_unlock_irqrestore(&pc->irq_lock[bank], flags);
659 static void bcm2835_gpio_irq_ack(struct irq_data *data)
661 struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
662 struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
663 unsigned gpio = irqd_to_hwirq(data);
665 bcm2835_gpio_set_bit(pc, GPEDS0, gpio);
668 static int bcm2835_gpio_irq_set_wake(struct irq_data *data, unsigned int on)
670 struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
671 struct bcm2835_pinctrl *pc = gpiochip_get_data(chip);
672 unsigned gpio = irqd_to_hwirq(data);
673 unsigned int irqgroup;
681 else if (gpio >= 28 && gpio <= 45)
683 else if (gpio >= 46 && gpio <= 57)
689 ret = enable_irq_wake(pc->wake_irq[irqgroup]);
691 ret = disable_irq_wake(pc->wake_irq[irqgroup]);
696 static struct irq_chip bcm2835_gpio_irq_chip = {
698 .irq_set_type = bcm2835_gpio_irq_set_type,
699 .irq_ack = bcm2835_gpio_irq_ack,
700 .irq_mask = bcm2835_gpio_irq_mask,
701 .irq_unmask = bcm2835_gpio_irq_unmask,
702 .irq_set_wake = bcm2835_gpio_irq_set_wake,
703 .flags = IRQCHIP_MASK_ON_SUSPEND,
706 static int bcm2835_pctl_get_groups_count(struct pinctrl_dev *pctldev)
708 return BCM2835_NUM_GPIOS;
711 static const char *bcm2835_pctl_get_group_name(struct pinctrl_dev *pctldev,
714 return bcm2835_gpio_groups[selector];
717 static int bcm2835_pctl_get_group_pins(struct pinctrl_dev *pctldev,
719 const unsigned **pins,
722 *pins = &bcm2835_gpio_pins[selector].number;
728 static void bcm2835_pctl_pin_dbg_show(struct pinctrl_dev *pctldev,
732 struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
733 struct gpio_chip *chip = &pc->gpio_chip;
734 enum bcm2835_fsel fsel = bcm2835_pinctrl_fsel_get(pc, offset);
735 const char *fname = bcm2835_functions[fsel];
736 int value = bcm2835_gpio_get_bit(pc, GPLEV0, offset);
737 int irq = irq_find_mapping(chip->irq.domain, offset);
739 seq_printf(s, "function %s in %s; irq %d (%s)",
740 fname, value ? "hi" : "lo",
741 irq, irq_type_names[pc->irq_type[offset]]);
744 static void bcm2835_pctl_dt_free_map(struct pinctrl_dev *pctldev,
745 struct pinctrl_map *maps, unsigned num_maps)
749 for (i = 0; i < num_maps; i++)
750 if (maps[i].type == PIN_MAP_TYPE_CONFIGS_PIN)
751 kfree(maps[i].data.configs.configs);
756 static int bcm2835_pctl_dt_node_to_map_func(struct bcm2835_pinctrl *pc,
757 struct device_node *np, u32 pin, u32 fnum,
758 struct pinctrl_map **maps)
760 struct pinctrl_map *map = *maps;
762 if (fnum >= ARRAY_SIZE(bcm2835_functions)) {
763 dev_err(pc->dev, "%pOF: invalid brcm,function %d\n", np, fnum);
767 map->type = PIN_MAP_TYPE_MUX_GROUP;
768 map->data.mux.group = bcm2835_gpio_groups[pin];
769 map->data.mux.function = bcm2835_functions[fnum];
775 static int bcm2835_pctl_dt_node_to_map_pull(struct bcm2835_pinctrl *pc,
776 struct device_node *np, u32 pin, u32 pull,
777 struct pinctrl_map **maps)
779 struct pinctrl_map *map = *maps;
780 unsigned long *configs;
783 dev_err(pc->dev, "%pOF: invalid brcm,pull %d\n", np, pull);
787 configs = kzalloc(sizeof(*configs), GFP_KERNEL);
790 configs[0] = pinconf_to_config_packed(BCM2835_PINCONF_PARAM_PULL, pull);
792 map->type = PIN_MAP_TYPE_CONFIGS_PIN;
793 map->data.configs.group_or_pin = bcm2835_gpio_pins[pin].name;
794 map->data.configs.configs = configs;
795 map->data.configs.num_configs = 1;
801 static int bcm2835_pctl_dt_node_to_map(struct pinctrl_dev *pctldev,
802 struct device_node *np,
803 struct pinctrl_map **map, unsigned int *num_maps)
805 struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
806 struct property *pins, *funcs, *pulls;
807 int num_pins, num_funcs, num_pulls, maps_per_pin;
808 struct pinctrl_map *maps, *cur_map;
812 /* Check for generic binding in this node */
813 err = pinconf_generic_dt_node_to_map_all(pctldev, np, map, num_maps);
814 if (err || *num_maps)
817 /* Generic binding did not find anything continue with legacy parse */
818 pins = of_find_property(np, "brcm,pins", NULL);
820 dev_err(pc->dev, "%pOF: missing brcm,pins property\n", np);
824 funcs = of_find_property(np, "brcm,function", NULL);
825 pulls = of_find_property(np, "brcm,pull", NULL);
827 if (!funcs && !pulls) {
829 "%pOF: neither brcm,function nor brcm,pull specified\n",
834 num_pins = pins->length / 4;
835 num_funcs = funcs ? (funcs->length / 4) : 0;
836 num_pulls = pulls ? (pulls->length / 4) : 0;
838 if (num_funcs > 1 && num_funcs != num_pins) {
840 "%pOF: brcm,function must have 1 or %d entries\n",
845 if (num_pulls > 1 && num_pulls != num_pins) {
847 "%pOF: brcm,pull must have 1 or %d entries\n",
857 cur_map = maps = kcalloc(num_pins * maps_per_pin, sizeof(*maps),
862 for (i = 0; i < num_pins; i++) {
863 err = of_property_read_u32_index(np, "brcm,pins", i, &pin);
866 if (pin >= pc->pctl_desc.npins) {
867 dev_err(pc->dev, "%pOF: invalid brcm,pins value %d\n",
874 err = of_property_read_u32_index(np, "brcm,function",
875 (num_funcs > 1) ? i : 0, &func);
878 err = bcm2835_pctl_dt_node_to_map_func(pc, np, pin,
884 err = of_property_read_u32_index(np, "brcm,pull",
885 (num_pulls > 1) ? i : 0, &pull);
888 err = bcm2835_pctl_dt_node_to_map_pull(pc, np, pin,
896 *num_maps = num_pins * maps_per_pin;
901 bcm2835_pctl_dt_free_map(pctldev, maps, num_pins * maps_per_pin);
905 static const struct pinctrl_ops bcm2835_pctl_ops = {
906 .get_groups_count = bcm2835_pctl_get_groups_count,
907 .get_group_name = bcm2835_pctl_get_group_name,
908 .get_group_pins = bcm2835_pctl_get_group_pins,
909 .pin_dbg_show = bcm2835_pctl_pin_dbg_show,
910 .dt_node_to_map = bcm2835_pctl_dt_node_to_map,
911 .dt_free_map = bcm2835_pctl_dt_free_map,
914 static int bcm2835_pmx_free(struct pinctrl_dev *pctldev,
917 struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
919 /* disable by setting to GPIO_IN */
920 bcm2835_pinctrl_fsel_set(pc, offset, BCM2835_FSEL_GPIO_IN);
924 static int bcm2835_pmx_get_functions_count(struct pinctrl_dev *pctldev)
926 return BCM2835_FSEL_COUNT;
929 static const char *bcm2835_pmx_get_function_name(struct pinctrl_dev *pctldev,
932 return bcm2835_functions[selector];
935 static int bcm2835_pmx_get_function_groups(struct pinctrl_dev *pctldev,
937 const char * const **groups,
938 unsigned * const num_groups)
940 /* every pin can do every function */
941 *groups = bcm2835_gpio_groups;
942 *num_groups = BCM2835_NUM_GPIOS;
947 static int bcm2835_pmx_set(struct pinctrl_dev *pctldev,
948 unsigned func_selector,
949 unsigned group_selector)
951 struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
953 bcm2835_pinctrl_fsel_set(pc, group_selector, func_selector);
958 static void bcm2835_pmx_gpio_disable_free(struct pinctrl_dev *pctldev,
959 struct pinctrl_gpio_range *range,
962 struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
964 /* disable by setting to GPIO_IN */
965 bcm2835_pinctrl_fsel_set(pc, offset, BCM2835_FSEL_GPIO_IN);
968 static int bcm2835_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
969 struct pinctrl_gpio_range *range,
973 struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
974 enum bcm2835_fsel fsel = input ?
975 BCM2835_FSEL_GPIO_IN : BCM2835_FSEL_GPIO_OUT;
977 bcm2835_pinctrl_fsel_set(pc, offset, fsel);
982 static const struct pinmux_ops bcm2835_pmx_ops = {
983 .free = bcm2835_pmx_free,
984 .get_functions_count = bcm2835_pmx_get_functions_count,
985 .get_function_name = bcm2835_pmx_get_function_name,
986 .get_function_groups = bcm2835_pmx_get_function_groups,
987 .set_mux = bcm2835_pmx_set,
988 .gpio_disable_free = bcm2835_pmx_gpio_disable_free,
989 .gpio_set_direction = bcm2835_pmx_gpio_set_direction,
992 static int bcm2835_pinconf_get(struct pinctrl_dev *pctldev,
993 unsigned pin, unsigned long *config)
995 /* No way to read back config in HW */
999 static void bcm2835_pull_config_set(struct bcm2835_pinctrl *pc,
1000 unsigned int pin, unsigned int arg)
1004 off = GPIO_REG_OFFSET(pin);
1005 bit = GPIO_REG_SHIFT(pin);
1007 bcm2835_gpio_wr(pc, GPPUD, arg & 3);
1009 * BCM2835 datasheet say to wait 150 cycles, but not of what.
1010 * But the VideoCore firmware delay for this operation
1011 * based nearly on the same amount of VPU cycles and this clock
1015 bcm2835_gpio_wr(pc, GPPUDCLK0 + (off * 4), BIT(bit));
1017 bcm2835_gpio_wr(pc, GPPUDCLK0 + (off * 4), 0);
1020 static int bcm2835_pinconf_set(struct pinctrl_dev *pctldev,
1021 unsigned int pin, unsigned long *configs,
1022 unsigned int num_configs)
1024 struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
1028 for (i = 0; i < num_configs; i++) {
1029 param = pinconf_to_config_param(configs[i]);
1030 arg = pinconf_to_config_argument(configs[i]);
1033 /* Set legacy brcm,pull */
1034 case BCM2835_PINCONF_PARAM_PULL:
1035 bcm2835_pull_config_set(pc, pin, arg);
1038 /* Set pull generic bindings */
1039 case PIN_CONFIG_BIAS_DISABLE:
1040 bcm2835_pull_config_set(pc, pin, BCM2835_PUD_OFF);
1043 case PIN_CONFIG_BIAS_PULL_DOWN:
1044 bcm2835_pull_config_set(pc, pin, BCM2835_PUD_DOWN);
1047 case PIN_CONFIG_BIAS_PULL_UP:
1048 bcm2835_pull_config_set(pc, pin, BCM2835_PUD_UP);
1051 /* Set output-high or output-low */
1052 case PIN_CONFIG_OUTPUT:
1053 bcm2835_gpio_set_bit(pc, arg ? GPSET0 : GPCLR0, pin);
1059 } /* switch param type */
1060 } /* for each config */
1065 static const struct pinconf_ops bcm2835_pinconf_ops = {
1067 .pin_config_get = bcm2835_pinconf_get,
1068 .pin_config_set = bcm2835_pinconf_set,
1071 static void bcm2711_pull_config_set(struct bcm2835_pinctrl *pc,
1072 unsigned int pin, unsigned int arg)
1078 off = PUD_2711_REG_OFFSET(pin);
1079 shifter = PUD_2711_REG_SHIFT(pin);
1081 value = bcm2835_gpio_rd(pc, GP_GPIO_PUP_PDN_CNTRL_REG0 + (off * 4));
1082 value &= ~(PUD_2711_MASK << shifter);
1083 value |= (arg << shifter);
1084 bcm2835_gpio_wr(pc, GP_GPIO_PUP_PDN_CNTRL_REG0 + (off * 4), value);
1087 static int bcm2711_pinconf_set(struct pinctrl_dev *pctldev,
1088 unsigned int pin, unsigned long *configs,
1089 unsigned int num_configs)
1091 struct bcm2835_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
1095 for (i = 0; i < num_configs; i++) {
1096 param = pinconf_to_config_param(configs[i]);
1097 arg = pinconf_to_config_argument(configs[i]);
1100 /* convert legacy brcm,pull */
1101 case BCM2835_PINCONF_PARAM_PULL:
1102 if (arg == BCM2835_PUD_UP)
1103 arg = BCM2711_PULL_UP;
1104 else if (arg == BCM2835_PUD_DOWN)
1105 arg = BCM2711_PULL_DOWN;
1107 arg = BCM2711_PULL_NONE;
1109 bcm2711_pull_config_set(pc, pin, arg);
1112 /* Set pull generic bindings */
1113 case PIN_CONFIG_BIAS_DISABLE:
1114 bcm2711_pull_config_set(pc, pin, BCM2711_PULL_NONE);
1116 case PIN_CONFIG_BIAS_PULL_DOWN:
1117 bcm2711_pull_config_set(pc, pin, BCM2711_PULL_DOWN);
1119 case PIN_CONFIG_BIAS_PULL_UP:
1120 bcm2711_pull_config_set(pc, pin, BCM2711_PULL_UP);
1123 /* Set output-high or output-low */
1124 case PIN_CONFIG_OUTPUT:
1125 bcm2835_gpio_set_bit(pc, arg ? GPSET0 : GPCLR0, pin);
1131 } /* for each config */
1136 static const struct pinconf_ops bcm2711_pinconf_ops = {
1138 .pin_config_get = bcm2835_pinconf_get,
1139 .pin_config_set = bcm2711_pinconf_set,
1142 static const struct pinctrl_desc bcm2835_pinctrl_desc = {
1143 .name = MODULE_NAME,
1144 .pins = bcm2835_gpio_pins,
1145 .npins = BCM2835_NUM_GPIOS,
1146 .pctlops = &bcm2835_pctl_ops,
1147 .pmxops = &bcm2835_pmx_ops,
1148 .confops = &bcm2835_pinconf_ops,
1149 .owner = THIS_MODULE,
1152 static const struct pinctrl_desc bcm2711_pinctrl_desc = {
1153 .name = "pinctrl-bcm2711",
1154 .pins = bcm2835_gpio_pins,
1155 .npins = BCM2711_NUM_GPIOS,
1156 .pctlops = &bcm2835_pctl_ops,
1157 .pmxops = &bcm2835_pmx_ops,
1158 .confops = &bcm2711_pinconf_ops,
1159 .owner = THIS_MODULE,
1162 static const struct pinctrl_gpio_range bcm2835_pinctrl_gpio_range = {
1163 .name = MODULE_NAME,
1164 .npins = BCM2835_NUM_GPIOS,
1167 static const struct pinctrl_gpio_range bcm2711_pinctrl_gpio_range = {
1168 .name = "pinctrl-bcm2711",
1169 .npins = BCM2711_NUM_GPIOS,
1172 struct bcm_plat_data {
1173 const struct gpio_chip *gpio_chip;
1174 const struct pinctrl_desc *pctl_desc;
1175 const struct pinctrl_gpio_range *gpio_range;
1178 static const struct bcm_plat_data bcm2835_plat_data = {
1179 .gpio_chip = &bcm2835_gpio_chip,
1180 .pctl_desc = &bcm2835_pinctrl_desc,
1181 .gpio_range = &bcm2835_pinctrl_gpio_range,
1184 static const struct bcm_plat_data bcm2711_plat_data = {
1185 .gpio_chip = &bcm2711_gpio_chip,
1186 .pctl_desc = &bcm2711_pinctrl_desc,
1187 .gpio_range = &bcm2711_pinctrl_gpio_range,
1190 static const struct of_device_id bcm2835_pinctrl_match[] = {
1192 .compatible = "brcm,bcm2835-gpio",
1193 .data = &bcm2835_plat_data,
1196 .compatible = "brcm,bcm2711-gpio",
1197 .data = &bcm2711_plat_data,
1200 .compatible = "brcm,bcm7211-gpio",
1201 .data = &bcm2711_plat_data,
1206 static int bcm2835_pinctrl_probe(struct platform_device *pdev)
1208 struct device *dev = &pdev->dev;
1209 struct device_node *np = dev->of_node;
1210 const struct bcm_plat_data *pdata;
1211 struct bcm2835_pinctrl *pc;
1212 struct gpio_irq_chip *girq;
1213 struct resource iomem;
1215 const struct of_device_id *match;
1218 BUILD_BUG_ON(ARRAY_SIZE(bcm2835_gpio_pins) != BCM2711_NUM_GPIOS);
1219 BUILD_BUG_ON(ARRAY_SIZE(bcm2835_gpio_groups) != BCM2711_NUM_GPIOS);
1221 pc = devm_kzalloc(dev, sizeof(*pc), GFP_KERNEL);
1225 platform_set_drvdata(pdev, pc);
1228 err = of_address_to_resource(np, 0, &iomem);
1230 dev_err(dev, "could not get IO memory\n");
1234 pc->base = devm_ioremap_resource(dev, &iomem);
1235 if (IS_ERR(pc->base))
1236 return PTR_ERR(pc->base);
1238 match = of_match_node(bcm2835_pinctrl_match, pdev->dev.of_node);
1242 pdata = match->data;
1243 is_7211 = of_device_is_compatible(np, "brcm,bcm7211-gpio");
1245 pc->gpio_chip = *pdata->gpio_chip;
1246 pc->gpio_chip.parent = dev;
1248 for (i = 0; i < BCM2835_NUM_BANKS; i++) {
1249 unsigned long events;
1252 /* clear event detection flags */
1253 bcm2835_gpio_wr(pc, GPREN0 + i * 4, 0);
1254 bcm2835_gpio_wr(pc, GPFEN0 + i * 4, 0);
1255 bcm2835_gpio_wr(pc, GPHEN0 + i * 4, 0);
1256 bcm2835_gpio_wr(pc, GPLEN0 + i * 4, 0);
1257 bcm2835_gpio_wr(pc, GPAREN0 + i * 4, 0);
1258 bcm2835_gpio_wr(pc, GPAFEN0 + i * 4, 0);
1260 /* clear all the events */
1261 events = bcm2835_gpio_rd(pc, GPEDS0 + i * 4);
1262 for_each_set_bit(offset, &events, 32)
1263 bcm2835_gpio_wr(pc, GPEDS0 + i * 4, BIT(offset));
1265 raw_spin_lock_init(&pc->irq_lock[i]);
1268 pc->pctl_desc = *pdata->pctl_desc;
1269 pc->pctl_dev = devm_pinctrl_register(dev, &pc->pctl_desc, pc);
1270 if (IS_ERR(pc->pctl_dev)) {
1271 gpiochip_remove(&pc->gpio_chip);
1272 return PTR_ERR(pc->pctl_dev);
1275 pc->gpio_range = *pdata->gpio_range;
1276 pc->gpio_range.base = pc->gpio_chip.base;
1277 pc->gpio_range.gc = &pc->gpio_chip;
1278 pinctrl_add_gpio_range(pc->pctl_dev, &pc->gpio_range);
1280 girq = &pc->gpio_chip.irq;
1281 girq->chip = &bcm2835_gpio_irq_chip;
1282 girq->parent_handler = bcm2835_gpio_irq_handler;
1283 girq->num_parents = BCM2835_NUM_IRQS;
1284 girq->parents = devm_kcalloc(dev, BCM2835_NUM_IRQS,
1285 sizeof(*girq->parents),
1287 if (!girq->parents) {
1293 pc->wake_irq = devm_kcalloc(dev, BCM2835_NUM_IRQS,
1294 sizeof(*pc->wake_irq),
1296 if (!pc->wake_irq) {
1303 * Use the same handler for all groups: this is necessary
1304 * since we use one gpiochip to cover all lines - the
1305 * irq handler then needs to figure out which group and
1306 * bank that was firing the IRQ and look up the per-group
1309 for (i = 0; i < BCM2835_NUM_IRQS; i++) {
1313 girq->parents[i] = irq_of_parse_and_map(np, i);
1315 if (!girq->parents[i]) {
1316 girq->num_parents = i;
1321 /* Skip over the all banks interrupts */
1322 pc->wake_irq[i] = irq_of_parse_and_map(np, i +
1323 BCM2835_NUM_IRQS + 1);
1325 len = strlen(dev_name(pc->dev)) + 16;
1326 name = devm_kzalloc(pc->dev, len, GFP_KERNEL);
1332 snprintf(name, len, "%s:bank%d", dev_name(pc->dev), i);
1334 /* These are optional interrupts */
1335 err = devm_request_irq(dev, pc->wake_irq[i],
1336 bcm2835_gpio_wake_irq_handler,
1337 IRQF_SHARED, name, pc);
1339 dev_warn(dev, "unable to request wake IRQ %d\n",
1343 girq->default_type = IRQ_TYPE_NONE;
1344 girq->handler = handle_level_irq;
1346 err = gpiochip_add_data(&pc->gpio_chip, pc);
1348 dev_err(dev, "could not add GPIO chip\n");
1355 pinctrl_remove_gpio_range(pc->pctl_dev, &pc->gpio_range);
1359 static struct platform_driver bcm2835_pinctrl_driver = {
1360 .probe = bcm2835_pinctrl_probe,
1362 .name = MODULE_NAME,
1363 .of_match_table = bcm2835_pinctrl_match,
1364 .suppress_bind_attrs = true,
1367 module_platform_driver(bcm2835_pinctrl_driver);
1369 MODULE_AUTHOR("Chris Boot");
1370 MODULE_AUTHOR("Simon Arlott");
1371 MODULE_AUTHOR("Stephen Warren");
1372 MODULE_DESCRIPTION("Broadcom BCM2835/2711 pinctrl and GPIO driver");
1373 MODULE_LICENSE("GPL");