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/of_address.h>
24 #include <linux/of_irq.h>
25 #include <linux/pinctrl/consumer.h>
26 #include <linux/pinctrl/machine.h>
27 #include <linux/pinctrl/pinconf.h>
28 #include <linux/pinctrl/pinctrl.h>
29 #include <linux/pinctrl/pinmux.h>
30 #include <linux/pinctrl/pinconf-generic.h>
31 #include <linux/platform_device.h>
32 #include <linux/seq_file.h>
33 #include <linux/slab.h>
34 #include <linux/spinlock.h>
35 #include <linux/types.h>
36 #include <dt-bindings/pinctrl/bcm2835.h>
38 #define MODULE_NAME "pinctrl-bcm2835"
39 #define BCM2835_NUM_GPIOS 54
40 #define BCM2711_NUM_GPIOS 58
41 #define BCM2835_NUM_BANKS 2
42 #define BCM2835_NUM_IRQS 3
44 /* GPIO register offsets */
45 #define GPFSEL0 0x0 /* Function Select */
46 #define GPSET0 0x1c /* Pin Output Set */
47 #define GPCLR0 0x28 /* Pin Output Clear */
48 #define GPLEV0 0x34 /* Pin Level */
49 #define GPEDS0 0x40 /* Pin Event Detect Status */
50 #define GPREN0 0x4c /* Pin Rising Edge Detect Enable */
51 #define GPFEN0 0x58 /* Pin Falling Edge Detect Enable */
52 #define GPHEN0 0x64 /* Pin High Detect Enable */
53 #define GPLEN0 0x70 /* Pin Low Detect Enable */
54 #define GPAREN0 0x7c /* Pin Async Rising Edge Detect */
55 #define GPAFEN0 0x88 /* Pin Async Falling Edge Detect */
56 #define GPPUD 0x94 /* Pin Pull-up/down Enable */
57 #define GPPUDCLK0 0x98 /* Pin Pull-up/down Enable Clock */
58 #define GP_GPIO_PUP_PDN_CNTRL_REG0 0xe4 /* 2711 Pin Pull-up/down select */
60 #define FSEL_REG(p) (GPFSEL0 + (((p) / 10) * 4))
61 #define FSEL_SHIFT(p) (((p) % 10) * 3)
62 #define GPIO_REG_OFFSET(p) ((p) / 32)
63 #define GPIO_REG_SHIFT(p) ((p) % 32)
65 #define PUD_2711_MASK 0x3
66 #define PUD_2711_REG_OFFSET(p) ((p) / 16)
67 #define PUD_2711_REG_SHIFT(p) (((p) % 16) * 2)
69 /* argument: bcm2835_pinconf_pull */
70 #define BCM2835_PINCONF_PARAM_PULL (PIN_CONFIG_END + 1)
72 #define BCM2711_PULL_NONE 0x0
73 #define BCM2711_PULL_UP 0x1
74 #define BCM2711_PULL_DOWN 0x2
76 struct bcm2835_pinctrl
{
80 /* note: locking assumes each bank will have its own unsigned long */
81 unsigned long enabled_irq_map
[BCM2835_NUM_BANKS
];
82 unsigned int irq_type
[BCM2711_NUM_GPIOS
];
84 struct pinctrl_dev
*pctl_dev
;
85 struct gpio_chip gpio_chip
;
86 struct pinctrl_desc pctl_desc
;
87 struct pinctrl_gpio_range gpio_range
;
89 raw_spinlock_t irq_lock
[BCM2835_NUM_BANKS
];
92 /* pins are just named GPIO0..GPIO53 */
93 #define BCM2835_GPIO_PIN(a) PINCTRL_PIN(a, "gpio" #a)
94 static struct pinctrl_pin_desc bcm2835_gpio_pins
[] = {
105 BCM2835_GPIO_PIN(10),
106 BCM2835_GPIO_PIN(11),
107 BCM2835_GPIO_PIN(12),
108 BCM2835_GPIO_PIN(13),
109 BCM2835_GPIO_PIN(14),
110 BCM2835_GPIO_PIN(15),
111 BCM2835_GPIO_PIN(16),
112 BCM2835_GPIO_PIN(17),
113 BCM2835_GPIO_PIN(18),
114 BCM2835_GPIO_PIN(19),
115 BCM2835_GPIO_PIN(20),
116 BCM2835_GPIO_PIN(21),
117 BCM2835_GPIO_PIN(22),
118 BCM2835_GPIO_PIN(23),
119 BCM2835_GPIO_PIN(24),
120 BCM2835_GPIO_PIN(25),
121 BCM2835_GPIO_PIN(26),
122 BCM2835_GPIO_PIN(27),
123 BCM2835_GPIO_PIN(28),
124 BCM2835_GPIO_PIN(29),
125 BCM2835_GPIO_PIN(30),
126 BCM2835_GPIO_PIN(31),
127 BCM2835_GPIO_PIN(32),
128 BCM2835_GPIO_PIN(33),
129 BCM2835_GPIO_PIN(34),
130 BCM2835_GPIO_PIN(35),
131 BCM2835_GPIO_PIN(36),
132 BCM2835_GPIO_PIN(37),
133 BCM2835_GPIO_PIN(38),
134 BCM2835_GPIO_PIN(39),
135 BCM2835_GPIO_PIN(40),
136 BCM2835_GPIO_PIN(41),
137 BCM2835_GPIO_PIN(42),
138 BCM2835_GPIO_PIN(43),
139 BCM2835_GPIO_PIN(44),
140 BCM2835_GPIO_PIN(45),
141 BCM2835_GPIO_PIN(46),
142 BCM2835_GPIO_PIN(47),
143 BCM2835_GPIO_PIN(48),
144 BCM2835_GPIO_PIN(49),
145 BCM2835_GPIO_PIN(50),
146 BCM2835_GPIO_PIN(51),
147 BCM2835_GPIO_PIN(52),
148 BCM2835_GPIO_PIN(53),
149 BCM2835_GPIO_PIN(54),
150 BCM2835_GPIO_PIN(55),
151 BCM2835_GPIO_PIN(56),
152 BCM2835_GPIO_PIN(57),
155 /* one pin per group */
156 static const char * const bcm2835_gpio_groups
[] = {
218 BCM2835_FSEL_COUNT
= 8,
219 BCM2835_FSEL_MASK
= 0x7,
222 static const char * const bcm2835_functions
[BCM2835_FSEL_COUNT
] = {
223 [BCM2835_FSEL_GPIO_IN
] = "gpio_in",
224 [BCM2835_FSEL_GPIO_OUT
] = "gpio_out",
225 [BCM2835_FSEL_ALT0
] = "alt0",
226 [BCM2835_FSEL_ALT1
] = "alt1",
227 [BCM2835_FSEL_ALT2
] = "alt2",
228 [BCM2835_FSEL_ALT3
] = "alt3",
229 [BCM2835_FSEL_ALT4
] = "alt4",
230 [BCM2835_FSEL_ALT5
] = "alt5",
233 static const char * const irq_type_names
[] = {
234 [IRQ_TYPE_NONE
] = "none",
235 [IRQ_TYPE_EDGE_RISING
] = "edge-rising",
236 [IRQ_TYPE_EDGE_FALLING
] = "edge-falling",
237 [IRQ_TYPE_EDGE_BOTH
] = "edge-both",
238 [IRQ_TYPE_LEVEL_HIGH
] = "level-high",
239 [IRQ_TYPE_LEVEL_LOW
] = "level-low",
242 static inline u32
bcm2835_gpio_rd(struct bcm2835_pinctrl
*pc
, unsigned reg
)
244 return readl(pc
->base
+ reg
);
247 static inline void bcm2835_gpio_wr(struct bcm2835_pinctrl
*pc
, unsigned reg
,
250 writel(val
, pc
->base
+ reg
);
253 static inline int bcm2835_gpio_get_bit(struct bcm2835_pinctrl
*pc
, unsigned reg
,
256 reg
+= GPIO_REG_OFFSET(bit
) * 4;
257 return (bcm2835_gpio_rd(pc
, reg
) >> GPIO_REG_SHIFT(bit
)) & 1;
260 /* note NOT a read/modify/write cycle */
261 static inline void bcm2835_gpio_set_bit(struct bcm2835_pinctrl
*pc
,
262 unsigned reg
, unsigned bit
)
264 reg
+= GPIO_REG_OFFSET(bit
) * 4;
265 bcm2835_gpio_wr(pc
, reg
, BIT(GPIO_REG_SHIFT(bit
)));
268 static inline enum bcm2835_fsel
bcm2835_pinctrl_fsel_get(
269 struct bcm2835_pinctrl
*pc
, unsigned pin
)
271 u32 val
= bcm2835_gpio_rd(pc
, FSEL_REG(pin
));
272 enum bcm2835_fsel status
= (val
>> FSEL_SHIFT(pin
)) & BCM2835_FSEL_MASK
;
274 dev_dbg(pc
->dev
, "get %08x (%u => %s)\n", val
, pin
,
275 bcm2835_functions
[status
]);
280 static inline void bcm2835_pinctrl_fsel_set(
281 struct bcm2835_pinctrl
*pc
, unsigned pin
,
282 enum bcm2835_fsel fsel
)
284 u32 val
= bcm2835_gpio_rd(pc
, FSEL_REG(pin
));
285 enum bcm2835_fsel cur
= (val
>> FSEL_SHIFT(pin
)) & BCM2835_FSEL_MASK
;
287 dev_dbg(pc
->dev
, "read %08x (%u => %s)\n", val
, pin
,
288 bcm2835_functions
[cur
]);
293 if (cur
!= BCM2835_FSEL_GPIO_IN
&& fsel
!= BCM2835_FSEL_GPIO_IN
) {
294 /* always transition through GPIO_IN */
295 val
&= ~(BCM2835_FSEL_MASK
<< FSEL_SHIFT(pin
));
296 val
|= BCM2835_FSEL_GPIO_IN
<< FSEL_SHIFT(pin
);
298 dev_dbg(pc
->dev
, "trans %08x (%u <= %s)\n", val
, pin
,
299 bcm2835_functions
[BCM2835_FSEL_GPIO_IN
]);
300 bcm2835_gpio_wr(pc
, FSEL_REG(pin
), val
);
303 val
&= ~(BCM2835_FSEL_MASK
<< FSEL_SHIFT(pin
));
304 val
|= fsel
<< FSEL_SHIFT(pin
);
306 dev_dbg(pc
->dev
, "write %08x (%u <= %s)\n", val
, pin
,
307 bcm2835_functions
[fsel
]);
308 bcm2835_gpio_wr(pc
, FSEL_REG(pin
), val
);
311 static int bcm2835_gpio_direction_input(struct gpio_chip
*chip
, unsigned offset
)
313 return pinctrl_gpio_direction_input(chip
->base
+ offset
);
316 static int bcm2835_gpio_get(struct gpio_chip
*chip
, unsigned offset
)
318 struct bcm2835_pinctrl
*pc
= gpiochip_get_data(chip
);
320 return bcm2835_gpio_get_bit(pc
, GPLEV0
, offset
);
323 static int bcm2835_gpio_get_direction(struct gpio_chip
*chip
, unsigned int offset
)
325 struct bcm2835_pinctrl
*pc
= gpiochip_get_data(chip
);
326 enum bcm2835_fsel fsel
= bcm2835_pinctrl_fsel_get(pc
, offset
);
328 /* Alternative function doesn't clearly provide a direction */
329 if (fsel
> BCM2835_FSEL_GPIO_OUT
)
332 if (fsel
== BCM2835_FSEL_GPIO_IN
)
333 return GPIO_LINE_DIRECTION_IN
;
335 return GPIO_LINE_DIRECTION_OUT
;
338 static void bcm2835_gpio_set(struct gpio_chip
*chip
, unsigned offset
, int value
)
340 struct bcm2835_pinctrl
*pc
= gpiochip_get_data(chip
);
342 bcm2835_gpio_set_bit(pc
, value
? GPSET0
: GPCLR0
, offset
);
345 static int bcm2835_gpio_direction_output(struct gpio_chip
*chip
,
346 unsigned offset
, int value
)
348 bcm2835_gpio_set(chip
, offset
, value
);
349 return pinctrl_gpio_direction_output(chip
->base
+ offset
);
352 static const struct gpio_chip bcm2835_gpio_chip
= {
353 .label
= MODULE_NAME
,
354 .owner
= THIS_MODULE
,
355 .request
= gpiochip_generic_request
,
356 .free
= gpiochip_generic_free
,
357 .direction_input
= bcm2835_gpio_direction_input
,
358 .direction_output
= bcm2835_gpio_direction_output
,
359 .get_direction
= bcm2835_gpio_get_direction
,
360 .get
= bcm2835_gpio_get
,
361 .set
= bcm2835_gpio_set
,
362 .set_config
= gpiochip_generic_config
,
364 .ngpio
= BCM2835_NUM_GPIOS
,
368 static const struct gpio_chip bcm2711_gpio_chip
= {
369 .label
= "pinctrl-bcm2711",
370 .owner
= THIS_MODULE
,
371 .request
= gpiochip_generic_request
,
372 .free
= gpiochip_generic_free
,
373 .direction_input
= bcm2835_gpio_direction_input
,
374 .direction_output
= bcm2835_gpio_direction_output
,
375 .get_direction
= bcm2835_gpio_get_direction
,
376 .get
= bcm2835_gpio_get
,
377 .set
= bcm2835_gpio_set
,
378 .set_config
= gpiochip_generic_config
,
380 .ngpio
= BCM2711_NUM_GPIOS
,
384 static void bcm2835_gpio_irq_handle_bank(struct bcm2835_pinctrl
*pc
,
385 unsigned int bank
, u32 mask
)
387 unsigned long events
;
391 events
= bcm2835_gpio_rd(pc
, GPEDS0
+ bank
* 4);
393 events
&= pc
->enabled_irq_map
[bank
];
394 for_each_set_bit(offset
, &events
, 32) {
395 gpio
= (32 * bank
) + offset
;
396 generic_handle_irq(irq_linear_revmap(pc
->gpio_chip
.irq
.domain
,
401 static void bcm2835_gpio_irq_handler(struct irq_desc
*desc
)
403 struct gpio_chip
*chip
= irq_desc_get_handler_data(desc
);
404 struct bcm2835_pinctrl
*pc
= gpiochip_get_data(chip
);
405 struct irq_chip
*host_chip
= irq_desc_get_chip(desc
);
406 int irq
= irq_desc_get_irq(desc
);
410 for (i
= 0; i
< BCM2835_NUM_IRQS
; i
++) {
411 if (chip
->irq
.parents
[i
] == irq
) {
416 /* This should not happen, every IRQ has a bank */
417 if (i
== BCM2835_NUM_IRQS
)
420 chained_irq_enter(host_chip
, desc
);
423 case 0: /* IRQ0 covers GPIOs 0-27 */
424 bcm2835_gpio_irq_handle_bank(pc
, 0, 0x0fffffff);
426 case 1: /* IRQ1 covers GPIOs 28-45 */
427 bcm2835_gpio_irq_handle_bank(pc
, 0, 0xf0000000);
428 bcm2835_gpio_irq_handle_bank(pc
, 1, 0x00003fff);
430 case 2: /* IRQ2 covers GPIOs 46-57 */
431 bcm2835_gpio_irq_handle_bank(pc
, 1, 0x003fc000);
435 chained_irq_exit(host_chip
, desc
);
438 static inline void __bcm2835_gpio_irq_config(struct bcm2835_pinctrl
*pc
,
439 unsigned reg
, unsigned offset
, bool enable
)
442 reg
+= GPIO_REG_OFFSET(offset
) * 4;
443 value
= bcm2835_gpio_rd(pc
, reg
);
445 value
|= BIT(GPIO_REG_SHIFT(offset
));
447 value
&= ~(BIT(GPIO_REG_SHIFT(offset
)));
448 bcm2835_gpio_wr(pc
, reg
, value
);
451 /* fast path for IRQ handler */
452 static void bcm2835_gpio_irq_config(struct bcm2835_pinctrl
*pc
,
453 unsigned offset
, bool enable
)
455 switch (pc
->irq_type
[offset
]) {
456 case IRQ_TYPE_EDGE_RISING
:
457 __bcm2835_gpio_irq_config(pc
, GPREN0
, offset
, enable
);
460 case IRQ_TYPE_EDGE_FALLING
:
461 __bcm2835_gpio_irq_config(pc
, GPFEN0
, offset
, enable
);
464 case IRQ_TYPE_EDGE_BOTH
:
465 __bcm2835_gpio_irq_config(pc
, GPREN0
, offset
, enable
);
466 __bcm2835_gpio_irq_config(pc
, GPFEN0
, offset
, enable
);
469 case IRQ_TYPE_LEVEL_HIGH
:
470 __bcm2835_gpio_irq_config(pc
, GPHEN0
, offset
, enable
);
473 case IRQ_TYPE_LEVEL_LOW
:
474 __bcm2835_gpio_irq_config(pc
, GPLEN0
, offset
, enable
);
479 static void bcm2835_gpio_irq_enable(struct irq_data
*data
)
481 struct gpio_chip
*chip
= irq_data_get_irq_chip_data(data
);
482 struct bcm2835_pinctrl
*pc
= gpiochip_get_data(chip
);
483 unsigned gpio
= irqd_to_hwirq(data
);
484 unsigned offset
= GPIO_REG_SHIFT(gpio
);
485 unsigned bank
= GPIO_REG_OFFSET(gpio
);
488 raw_spin_lock_irqsave(&pc
->irq_lock
[bank
], flags
);
489 set_bit(offset
, &pc
->enabled_irq_map
[bank
]);
490 bcm2835_gpio_irq_config(pc
, gpio
, true);
491 raw_spin_unlock_irqrestore(&pc
->irq_lock
[bank
], flags
);
494 static void bcm2835_gpio_irq_disable(struct irq_data
*data
)
496 struct gpio_chip
*chip
= irq_data_get_irq_chip_data(data
);
497 struct bcm2835_pinctrl
*pc
= gpiochip_get_data(chip
);
498 unsigned gpio
= irqd_to_hwirq(data
);
499 unsigned offset
= GPIO_REG_SHIFT(gpio
);
500 unsigned bank
= GPIO_REG_OFFSET(gpio
);
503 raw_spin_lock_irqsave(&pc
->irq_lock
[bank
], flags
);
504 bcm2835_gpio_irq_config(pc
, gpio
, false);
505 /* Clear events that were latched prior to clearing event sources */
506 bcm2835_gpio_set_bit(pc
, GPEDS0
, gpio
);
507 clear_bit(offset
, &pc
->enabled_irq_map
[bank
]);
508 raw_spin_unlock_irqrestore(&pc
->irq_lock
[bank
], flags
);
511 static int __bcm2835_gpio_irq_set_type_disabled(struct bcm2835_pinctrl
*pc
,
512 unsigned offset
, unsigned int type
)
516 case IRQ_TYPE_EDGE_RISING
:
517 case IRQ_TYPE_EDGE_FALLING
:
518 case IRQ_TYPE_EDGE_BOTH
:
519 case IRQ_TYPE_LEVEL_HIGH
:
520 case IRQ_TYPE_LEVEL_LOW
:
521 pc
->irq_type
[offset
] = type
;
530 /* slower path for reconfiguring IRQ type */
531 static int __bcm2835_gpio_irq_set_type_enabled(struct bcm2835_pinctrl
*pc
,
532 unsigned offset
, unsigned int type
)
536 if (pc
->irq_type
[offset
] != type
) {
537 bcm2835_gpio_irq_config(pc
, offset
, false);
538 pc
->irq_type
[offset
] = type
;
542 case IRQ_TYPE_EDGE_RISING
:
543 if (pc
->irq_type
[offset
] == IRQ_TYPE_EDGE_BOTH
) {
544 /* RISING already enabled, disable FALLING */
545 pc
->irq_type
[offset
] = IRQ_TYPE_EDGE_FALLING
;
546 bcm2835_gpio_irq_config(pc
, offset
, false);
547 pc
->irq_type
[offset
] = type
;
548 } else if (pc
->irq_type
[offset
] != type
) {
549 bcm2835_gpio_irq_config(pc
, offset
, false);
550 pc
->irq_type
[offset
] = type
;
551 bcm2835_gpio_irq_config(pc
, offset
, true);
555 case IRQ_TYPE_EDGE_FALLING
:
556 if (pc
->irq_type
[offset
] == IRQ_TYPE_EDGE_BOTH
) {
557 /* FALLING already enabled, disable RISING */
558 pc
->irq_type
[offset
] = IRQ_TYPE_EDGE_RISING
;
559 bcm2835_gpio_irq_config(pc
, offset
, false);
560 pc
->irq_type
[offset
] = type
;
561 } else if (pc
->irq_type
[offset
] != type
) {
562 bcm2835_gpio_irq_config(pc
, offset
, false);
563 pc
->irq_type
[offset
] = type
;
564 bcm2835_gpio_irq_config(pc
, offset
, true);
568 case IRQ_TYPE_EDGE_BOTH
:
569 if (pc
->irq_type
[offset
] == IRQ_TYPE_EDGE_RISING
) {
570 /* RISING already enabled, enable FALLING too */
571 pc
->irq_type
[offset
] = IRQ_TYPE_EDGE_FALLING
;
572 bcm2835_gpio_irq_config(pc
, offset
, true);
573 pc
->irq_type
[offset
] = type
;
574 } else if (pc
->irq_type
[offset
] == IRQ_TYPE_EDGE_FALLING
) {
575 /* FALLING already enabled, enable RISING too */
576 pc
->irq_type
[offset
] = IRQ_TYPE_EDGE_RISING
;
577 bcm2835_gpio_irq_config(pc
, offset
, true);
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_LEVEL_HIGH
:
587 case IRQ_TYPE_LEVEL_LOW
:
588 if (pc
->irq_type
[offset
] != type
) {
589 bcm2835_gpio_irq_config(pc
, offset
, false);
590 pc
->irq_type
[offset
] = type
;
591 bcm2835_gpio_irq_config(pc
, offset
, true);
601 static int bcm2835_gpio_irq_set_type(struct irq_data
*data
, unsigned int type
)
603 struct gpio_chip
*chip
= irq_data_get_irq_chip_data(data
);
604 struct bcm2835_pinctrl
*pc
= gpiochip_get_data(chip
);
605 unsigned gpio
= irqd_to_hwirq(data
);
606 unsigned offset
= GPIO_REG_SHIFT(gpio
);
607 unsigned bank
= GPIO_REG_OFFSET(gpio
);
611 raw_spin_lock_irqsave(&pc
->irq_lock
[bank
], flags
);
613 if (test_bit(offset
, &pc
->enabled_irq_map
[bank
]))
614 ret
= __bcm2835_gpio_irq_set_type_enabled(pc
, gpio
, type
);
616 ret
= __bcm2835_gpio_irq_set_type_disabled(pc
, gpio
, type
);
618 if (type
& IRQ_TYPE_EDGE_BOTH
)
619 irq_set_handler_locked(data
, handle_edge_irq
);
621 irq_set_handler_locked(data
, handle_level_irq
);
623 raw_spin_unlock_irqrestore(&pc
->irq_lock
[bank
], flags
);
628 static void bcm2835_gpio_irq_ack(struct irq_data
*data
)
630 struct gpio_chip
*chip
= irq_data_get_irq_chip_data(data
);
631 struct bcm2835_pinctrl
*pc
= gpiochip_get_data(chip
);
632 unsigned gpio
= irqd_to_hwirq(data
);
634 bcm2835_gpio_set_bit(pc
, GPEDS0
, gpio
);
637 static struct irq_chip bcm2835_gpio_irq_chip
= {
639 .irq_enable
= bcm2835_gpio_irq_enable
,
640 .irq_disable
= bcm2835_gpio_irq_disable
,
641 .irq_set_type
= bcm2835_gpio_irq_set_type
,
642 .irq_ack
= bcm2835_gpio_irq_ack
,
643 .irq_mask
= bcm2835_gpio_irq_disable
,
644 .irq_unmask
= bcm2835_gpio_irq_enable
,
647 static int bcm2835_pctl_get_groups_count(struct pinctrl_dev
*pctldev
)
649 return BCM2835_NUM_GPIOS
;
652 static const char *bcm2835_pctl_get_group_name(struct pinctrl_dev
*pctldev
,
655 return bcm2835_gpio_groups
[selector
];
658 static int bcm2835_pctl_get_group_pins(struct pinctrl_dev
*pctldev
,
660 const unsigned **pins
,
663 *pins
= &bcm2835_gpio_pins
[selector
].number
;
669 static void bcm2835_pctl_pin_dbg_show(struct pinctrl_dev
*pctldev
,
673 struct bcm2835_pinctrl
*pc
= pinctrl_dev_get_drvdata(pctldev
);
674 struct gpio_chip
*chip
= &pc
->gpio_chip
;
675 enum bcm2835_fsel fsel
= bcm2835_pinctrl_fsel_get(pc
, offset
);
676 const char *fname
= bcm2835_functions
[fsel
];
677 int value
= bcm2835_gpio_get_bit(pc
, GPLEV0
, offset
);
678 int irq
= irq_find_mapping(chip
->irq
.domain
, offset
);
680 seq_printf(s
, "function %s in %s; irq %d (%s)",
681 fname
, value
? "hi" : "lo",
682 irq
, irq_type_names
[pc
->irq_type
[offset
]]);
685 static void bcm2835_pctl_dt_free_map(struct pinctrl_dev
*pctldev
,
686 struct pinctrl_map
*maps
, unsigned num_maps
)
690 for (i
= 0; i
< num_maps
; i
++)
691 if (maps
[i
].type
== PIN_MAP_TYPE_CONFIGS_PIN
)
692 kfree(maps
[i
].data
.configs
.configs
);
697 static int bcm2835_pctl_dt_node_to_map_func(struct bcm2835_pinctrl
*pc
,
698 struct device_node
*np
, u32 pin
, u32 fnum
,
699 struct pinctrl_map
**maps
)
701 struct pinctrl_map
*map
= *maps
;
703 if (fnum
>= ARRAY_SIZE(bcm2835_functions
)) {
704 dev_err(pc
->dev
, "%pOF: invalid brcm,function %d\n", np
, fnum
);
708 map
->type
= PIN_MAP_TYPE_MUX_GROUP
;
709 map
->data
.mux
.group
= bcm2835_gpio_groups
[pin
];
710 map
->data
.mux
.function
= bcm2835_functions
[fnum
];
716 static int bcm2835_pctl_dt_node_to_map_pull(struct bcm2835_pinctrl
*pc
,
717 struct device_node
*np
, u32 pin
, u32 pull
,
718 struct pinctrl_map
**maps
)
720 struct pinctrl_map
*map
= *maps
;
721 unsigned long *configs
;
724 dev_err(pc
->dev
, "%pOF: invalid brcm,pull %d\n", np
, pull
);
728 configs
= kzalloc(sizeof(*configs
), GFP_KERNEL
);
731 configs
[0] = pinconf_to_config_packed(BCM2835_PINCONF_PARAM_PULL
, pull
);
733 map
->type
= PIN_MAP_TYPE_CONFIGS_PIN
;
734 map
->data
.configs
.group_or_pin
= bcm2835_gpio_pins
[pin
].name
;
735 map
->data
.configs
.configs
= configs
;
736 map
->data
.configs
.num_configs
= 1;
742 static int bcm2835_pctl_dt_node_to_map(struct pinctrl_dev
*pctldev
,
743 struct device_node
*np
,
744 struct pinctrl_map
**map
, unsigned int *num_maps
)
746 struct bcm2835_pinctrl
*pc
= pinctrl_dev_get_drvdata(pctldev
);
747 struct property
*pins
, *funcs
, *pulls
;
748 int num_pins
, num_funcs
, num_pulls
, maps_per_pin
;
749 struct pinctrl_map
*maps
, *cur_map
;
753 /* Check for generic binding in this node */
754 err
= pinconf_generic_dt_node_to_map_all(pctldev
, np
, map
, num_maps
);
755 if (err
|| *num_maps
)
758 /* Generic binding did not find anything continue with legacy parse */
759 pins
= of_find_property(np
, "brcm,pins", NULL
);
761 dev_err(pc
->dev
, "%pOF: missing brcm,pins property\n", np
);
765 funcs
= of_find_property(np
, "brcm,function", NULL
);
766 pulls
= of_find_property(np
, "brcm,pull", NULL
);
768 if (!funcs
&& !pulls
) {
770 "%pOF: neither brcm,function nor brcm,pull specified\n",
775 num_pins
= pins
->length
/ 4;
776 num_funcs
= funcs
? (funcs
->length
/ 4) : 0;
777 num_pulls
= pulls
? (pulls
->length
/ 4) : 0;
779 if (num_funcs
> 1 && num_funcs
!= num_pins
) {
781 "%pOF: brcm,function must have 1 or %d entries\n",
786 if (num_pulls
> 1 && num_pulls
!= num_pins
) {
788 "%pOF: brcm,pull must have 1 or %d entries\n",
798 cur_map
= maps
= kcalloc(num_pins
* maps_per_pin
, sizeof(*maps
),
803 for (i
= 0; i
< num_pins
; i
++) {
804 err
= of_property_read_u32_index(np
, "brcm,pins", i
, &pin
);
807 if (pin
>= pc
->pctl_desc
.npins
) {
808 dev_err(pc
->dev
, "%pOF: invalid brcm,pins value %d\n",
815 err
= of_property_read_u32_index(np
, "brcm,function",
816 (num_funcs
> 1) ? i
: 0, &func
);
819 err
= bcm2835_pctl_dt_node_to_map_func(pc
, np
, pin
,
825 err
= of_property_read_u32_index(np
, "brcm,pull",
826 (num_pulls
> 1) ? i
: 0, &pull
);
829 err
= bcm2835_pctl_dt_node_to_map_pull(pc
, np
, pin
,
837 *num_maps
= num_pins
* maps_per_pin
;
842 bcm2835_pctl_dt_free_map(pctldev
, maps
, num_pins
* maps_per_pin
);
846 static const struct pinctrl_ops bcm2835_pctl_ops
= {
847 .get_groups_count
= bcm2835_pctl_get_groups_count
,
848 .get_group_name
= bcm2835_pctl_get_group_name
,
849 .get_group_pins
= bcm2835_pctl_get_group_pins
,
850 .pin_dbg_show
= bcm2835_pctl_pin_dbg_show
,
851 .dt_node_to_map
= bcm2835_pctl_dt_node_to_map
,
852 .dt_free_map
= bcm2835_pctl_dt_free_map
,
855 static int bcm2835_pmx_free(struct pinctrl_dev
*pctldev
,
858 struct bcm2835_pinctrl
*pc
= pinctrl_dev_get_drvdata(pctldev
);
860 /* disable by setting to GPIO_IN */
861 bcm2835_pinctrl_fsel_set(pc
, offset
, BCM2835_FSEL_GPIO_IN
);
865 static int bcm2835_pmx_get_functions_count(struct pinctrl_dev
*pctldev
)
867 return BCM2835_FSEL_COUNT
;
870 static const char *bcm2835_pmx_get_function_name(struct pinctrl_dev
*pctldev
,
873 return bcm2835_functions
[selector
];
876 static int bcm2835_pmx_get_function_groups(struct pinctrl_dev
*pctldev
,
878 const char * const **groups
,
879 unsigned * const num_groups
)
881 /* every pin can do every function */
882 *groups
= bcm2835_gpio_groups
;
883 *num_groups
= BCM2835_NUM_GPIOS
;
888 static int bcm2835_pmx_set(struct pinctrl_dev
*pctldev
,
889 unsigned func_selector
,
890 unsigned group_selector
)
892 struct bcm2835_pinctrl
*pc
= pinctrl_dev_get_drvdata(pctldev
);
894 bcm2835_pinctrl_fsel_set(pc
, group_selector
, func_selector
);
899 static void bcm2835_pmx_gpio_disable_free(struct pinctrl_dev
*pctldev
,
900 struct pinctrl_gpio_range
*range
,
903 struct bcm2835_pinctrl
*pc
= pinctrl_dev_get_drvdata(pctldev
);
905 /* disable by setting to GPIO_IN */
906 bcm2835_pinctrl_fsel_set(pc
, offset
, BCM2835_FSEL_GPIO_IN
);
909 static int bcm2835_pmx_gpio_set_direction(struct pinctrl_dev
*pctldev
,
910 struct pinctrl_gpio_range
*range
,
914 struct bcm2835_pinctrl
*pc
= pinctrl_dev_get_drvdata(pctldev
);
915 enum bcm2835_fsel fsel
= input
?
916 BCM2835_FSEL_GPIO_IN
: BCM2835_FSEL_GPIO_OUT
;
918 bcm2835_pinctrl_fsel_set(pc
, offset
, fsel
);
923 static const struct pinmux_ops bcm2835_pmx_ops
= {
924 .free
= bcm2835_pmx_free
,
925 .get_functions_count
= bcm2835_pmx_get_functions_count
,
926 .get_function_name
= bcm2835_pmx_get_function_name
,
927 .get_function_groups
= bcm2835_pmx_get_function_groups
,
928 .set_mux
= bcm2835_pmx_set
,
929 .gpio_disable_free
= bcm2835_pmx_gpio_disable_free
,
930 .gpio_set_direction
= bcm2835_pmx_gpio_set_direction
,
933 static int bcm2835_pinconf_get(struct pinctrl_dev
*pctldev
,
934 unsigned pin
, unsigned long *config
)
936 /* No way to read back config in HW */
940 static void bcm2835_pull_config_set(struct bcm2835_pinctrl
*pc
,
941 unsigned int pin
, unsigned int arg
)
945 off
= GPIO_REG_OFFSET(pin
);
946 bit
= GPIO_REG_SHIFT(pin
);
948 bcm2835_gpio_wr(pc
, GPPUD
, arg
& 3);
950 * BCM2835 datasheet say to wait 150 cycles, but not of what.
951 * But the VideoCore firmware delay for this operation
952 * based nearly on the same amount of VPU cycles and this clock
956 bcm2835_gpio_wr(pc
, GPPUDCLK0
+ (off
* 4), BIT(bit
));
958 bcm2835_gpio_wr(pc
, GPPUDCLK0
+ (off
* 4), 0);
961 static int bcm2835_pinconf_set(struct pinctrl_dev
*pctldev
,
962 unsigned int pin
, unsigned long *configs
,
963 unsigned int num_configs
)
965 struct bcm2835_pinctrl
*pc
= pinctrl_dev_get_drvdata(pctldev
);
969 for (i
= 0; i
< num_configs
; i
++) {
970 param
= pinconf_to_config_param(configs
[i
]);
971 arg
= pinconf_to_config_argument(configs
[i
]);
974 /* Set legacy brcm,pull */
975 case BCM2835_PINCONF_PARAM_PULL
:
976 bcm2835_pull_config_set(pc
, pin
, arg
);
979 /* Set pull generic bindings */
980 case PIN_CONFIG_BIAS_DISABLE
:
981 bcm2835_pull_config_set(pc
, pin
, BCM2835_PUD_OFF
);
984 case PIN_CONFIG_BIAS_PULL_DOWN
:
985 bcm2835_pull_config_set(pc
, pin
, BCM2835_PUD_DOWN
);
988 case PIN_CONFIG_BIAS_PULL_UP
:
989 bcm2835_pull_config_set(pc
, pin
, BCM2835_PUD_UP
);
992 /* Set output-high or output-low */
993 case PIN_CONFIG_OUTPUT
:
994 bcm2835_gpio_set_bit(pc
, arg
? GPSET0
: GPCLR0
, pin
);
1000 } /* switch param type */
1001 } /* for each config */
1006 static const struct pinconf_ops bcm2835_pinconf_ops
= {
1008 .pin_config_get
= bcm2835_pinconf_get
,
1009 .pin_config_set
= bcm2835_pinconf_set
,
1012 static void bcm2711_pull_config_set(struct bcm2835_pinctrl
*pc
,
1013 unsigned int pin
, unsigned int arg
)
1019 off
= PUD_2711_REG_OFFSET(pin
);
1020 shifter
= PUD_2711_REG_SHIFT(pin
);
1022 value
= bcm2835_gpio_rd(pc
, GP_GPIO_PUP_PDN_CNTRL_REG0
+ (off
* 4));
1023 value
&= ~(PUD_2711_MASK
<< shifter
);
1024 value
|= (arg
<< shifter
);
1025 bcm2835_gpio_wr(pc
, GP_GPIO_PUP_PDN_CNTRL_REG0
+ (off
* 4), value
);
1028 static int bcm2711_pinconf_set(struct pinctrl_dev
*pctldev
,
1029 unsigned int pin
, unsigned long *configs
,
1030 unsigned int num_configs
)
1032 struct bcm2835_pinctrl
*pc
= pinctrl_dev_get_drvdata(pctldev
);
1036 for (i
= 0; i
< num_configs
; i
++) {
1037 param
= pinconf_to_config_param(configs
[i
]);
1038 arg
= pinconf_to_config_argument(configs
[i
]);
1041 /* convert legacy brcm,pull */
1042 case BCM2835_PINCONF_PARAM_PULL
:
1043 if (arg
== BCM2835_PUD_UP
)
1044 arg
= BCM2711_PULL_UP
;
1045 else if (arg
== BCM2835_PUD_DOWN
)
1046 arg
= BCM2711_PULL_DOWN
;
1048 arg
= BCM2711_PULL_NONE
;
1050 bcm2711_pull_config_set(pc
, pin
, arg
);
1053 /* Set pull generic bindings */
1054 case PIN_CONFIG_BIAS_DISABLE
:
1055 bcm2711_pull_config_set(pc
, pin
, BCM2711_PULL_NONE
);
1057 case PIN_CONFIG_BIAS_PULL_DOWN
:
1058 bcm2711_pull_config_set(pc
, pin
, BCM2711_PULL_DOWN
);
1060 case PIN_CONFIG_BIAS_PULL_UP
:
1061 bcm2711_pull_config_set(pc
, pin
, BCM2711_PULL_UP
);
1064 /* Set output-high or output-low */
1065 case PIN_CONFIG_OUTPUT
:
1066 bcm2835_gpio_set_bit(pc
, arg
? GPSET0
: GPCLR0
, pin
);
1072 } /* for each config */
1077 static const struct pinconf_ops bcm2711_pinconf_ops
= {
1079 .pin_config_get
= bcm2835_pinconf_get
,
1080 .pin_config_set
= bcm2711_pinconf_set
,
1083 static const struct pinctrl_desc bcm2835_pinctrl_desc
= {
1084 .name
= MODULE_NAME
,
1085 .pins
= bcm2835_gpio_pins
,
1086 .npins
= BCM2835_NUM_GPIOS
,
1087 .pctlops
= &bcm2835_pctl_ops
,
1088 .pmxops
= &bcm2835_pmx_ops
,
1089 .confops
= &bcm2835_pinconf_ops
,
1090 .owner
= THIS_MODULE
,
1093 static const struct pinctrl_desc bcm2711_pinctrl_desc
= {
1094 .name
= "pinctrl-bcm2711",
1095 .pins
= bcm2835_gpio_pins
,
1096 .npins
= BCM2711_NUM_GPIOS
,
1097 .pctlops
= &bcm2835_pctl_ops
,
1098 .pmxops
= &bcm2835_pmx_ops
,
1099 .confops
= &bcm2711_pinconf_ops
,
1100 .owner
= THIS_MODULE
,
1103 static const struct pinctrl_gpio_range bcm2835_pinctrl_gpio_range
= {
1104 .name
= MODULE_NAME
,
1105 .npins
= BCM2835_NUM_GPIOS
,
1108 static const struct pinctrl_gpio_range bcm2711_pinctrl_gpio_range
= {
1109 .name
= "pinctrl-bcm2711",
1110 .npins
= BCM2711_NUM_GPIOS
,
1113 struct bcm_plat_data
{
1114 const struct gpio_chip
*gpio_chip
;
1115 const struct pinctrl_desc
*pctl_desc
;
1116 const struct pinctrl_gpio_range
*gpio_range
;
1119 static const struct bcm_plat_data bcm2835_plat_data
= {
1120 .gpio_chip
= &bcm2835_gpio_chip
,
1121 .pctl_desc
= &bcm2835_pinctrl_desc
,
1122 .gpio_range
= &bcm2835_pinctrl_gpio_range
,
1125 static const struct bcm_plat_data bcm2711_plat_data
= {
1126 .gpio_chip
= &bcm2711_gpio_chip
,
1127 .pctl_desc
= &bcm2711_pinctrl_desc
,
1128 .gpio_range
= &bcm2711_pinctrl_gpio_range
,
1131 static const struct of_device_id bcm2835_pinctrl_match
[] = {
1133 .compatible
= "brcm,bcm2835-gpio",
1134 .data
= &bcm2835_plat_data
,
1137 .compatible
= "brcm,bcm2711-gpio",
1138 .data
= &bcm2711_plat_data
,
1143 static int bcm2835_pinctrl_probe(struct platform_device
*pdev
)
1145 struct device
*dev
= &pdev
->dev
;
1146 struct device_node
*np
= dev
->of_node
;
1147 const struct bcm_plat_data
*pdata
;
1148 struct bcm2835_pinctrl
*pc
;
1149 struct gpio_irq_chip
*girq
;
1150 struct resource iomem
;
1152 const struct of_device_id
*match
;
1154 BUILD_BUG_ON(ARRAY_SIZE(bcm2835_gpio_pins
) != BCM2711_NUM_GPIOS
);
1155 BUILD_BUG_ON(ARRAY_SIZE(bcm2835_gpio_groups
) != BCM2711_NUM_GPIOS
);
1157 pc
= devm_kzalloc(dev
, sizeof(*pc
), GFP_KERNEL
);
1161 platform_set_drvdata(pdev
, pc
);
1164 err
= of_address_to_resource(np
, 0, &iomem
);
1166 dev_err(dev
, "could not get IO memory\n");
1170 pc
->base
= devm_ioremap_resource(dev
, &iomem
);
1171 if (IS_ERR(pc
->base
))
1172 return PTR_ERR(pc
->base
);
1174 match
= of_match_node(bcm2835_pinctrl_match
, pdev
->dev
.of_node
);
1178 pdata
= match
->data
;
1180 pc
->gpio_chip
= *pdata
->gpio_chip
;
1181 pc
->gpio_chip
.parent
= dev
;
1182 pc
->gpio_chip
.of_node
= np
;
1184 for (i
= 0; i
< BCM2835_NUM_BANKS
; i
++) {
1185 unsigned long events
;
1188 /* clear event detection flags */
1189 bcm2835_gpio_wr(pc
, GPREN0
+ i
* 4, 0);
1190 bcm2835_gpio_wr(pc
, GPFEN0
+ i
* 4, 0);
1191 bcm2835_gpio_wr(pc
, GPHEN0
+ i
* 4, 0);
1192 bcm2835_gpio_wr(pc
, GPLEN0
+ i
* 4, 0);
1193 bcm2835_gpio_wr(pc
, GPAREN0
+ i
* 4, 0);
1194 bcm2835_gpio_wr(pc
, GPAFEN0
+ i
* 4, 0);
1196 /* clear all the events */
1197 events
= bcm2835_gpio_rd(pc
, GPEDS0
+ i
* 4);
1198 for_each_set_bit(offset
, &events
, 32)
1199 bcm2835_gpio_wr(pc
, GPEDS0
+ i
* 4, BIT(offset
));
1201 raw_spin_lock_init(&pc
->irq_lock
[i
]);
1204 girq
= &pc
->gpio_chip
.irq
;
1205 girq
->chip
= &bcm2835_gpio_irq_chip
;
1206 girq
->parent_handler
= bcm2835_gpio_irq_handler
;
1207 girq
->num_parents
= BCM2835_NUM_IRQS
;
1208 girq
->parents
= devm_kcalloc(dev
, BCM2835_NUM_IRQS
,
1209 sizeof(*girq
->parents
),
1214 * Use the same handler for all groups: this is necessary
1215 * since we use one gpiochip to cover all lines - the
1216 * irq handler then needs to figure out which group and
1217 * bank that was firing the IRQ and look up the per-group
1220 for (i
= 0; i
< BCM2835_NUM_IRQS
; i
++)
1221 girq
->parents
[i
] = irq_of_parse_and_map(np
, i
);
1222 girq
->default_type
= IRQ_TYPE_NONE
;
1223 girq
->handler
= handle_level_irq
;
1225 err
= gpiochip_add_data(&pc
->gpio_chip
, pc
);
1227 dev_err(dev
, "could not add GPIO chip\n");
1231 pc
->pctl_desc
= *pdata
->pctl_desc
;
1232 pc
->pctl_dev
= devm_pinctrl_register(dev
, &pc
->pctl_desc
, pc
);
1233 if (IS_ERR(pc
->pctl_dev
)) {
1234 gpiochip_remove(&pc
->gpio_chip
);
1235 return PTR_ERR(pc
->pctl_dev
);
1238 pc
->gpio_range
= *pdata
->gpio_range
;
1239 pc
->gpio_range
.base
= pc
->gpio_chip
.base
;
1240 pc
->gpio_range
.gc
= &pc
->gpio_chip
;
1241 pinctrl_add_gpio_range(pc
->pctl_dev
, &pc
->gpio_range
);
1246 static struct platform_driver bcm2835_pinctrl_driver
= {
1247 .probe
= bcm2835_pinctrl_probe
,
1249 .name
= MODULE_NAME
,
1250 .of_match_table
= bcm2835_pinctrl_match
,
1251 .suppress_bind_attrs
= true,
1254 builtin_platform_driver(bcm2835_pinctrl_driver
);