1 From b0e1ebc79a6d7f84f71a758f5a504c8cf954e2e0 Mon Sep 17 00:00:00 2001
2 From: =?UTF-8?q?=C3=81lvaro=20Fern=C3=A1ndez=20Rojas?= <noltari@gmail.com>
3 Date: Fri, 24 Jun 2016 22:16:01 +0200
4 Subject: [PATCH 04/12] pinctrl: add a pincontrol driver for BCM6358
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
9 Add a pincotrol driver for BCM6358. BCM6358 allow overlaying different
10 functions onto the GPIO pins. It does not support configuring individual
11 pins but only whole groups. These groups may overlap, and still require
12 the directions to be set correctly in the GPIO register. In addition the
13 functions register controls other, not directly mux related functions.
15 Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>
16 Signed-off-by: Jonas Gorski <jonas.gorski@gmail.com>
18 drivers/pinctrl/bcm/Kconfig | 11 +
19 drivers/pinctrl/bcm/Makefile | 1 +
20 drivers/pinctrl/bcm/pinctrl-bcm6358.c | 526 ++++++++++++++++++++++++++
21 3 files changed, 538 insertions(+)
22 create mode 100644 drivers/pinctrl/bcm/pinctrl-bcm6358.c
24 --- a/drivers/pinctrl/bcm/Kconfig
25 +++ b/drivers/pinctrl/bcm/Kconfig
26 @@ -40,6 +40,17 @@ config PINCTRL_BCM6328
28 Say Y here to enable the Broadcom BCM6328 GPIO driver.
30 +config PINCTRL_BCM6358
31 + bool "Broadcom BCM6358 GPIO driver"
32 + depends on OF_GPIO && (BMIPS_GENERIC || COMPILE_TEST)
35 + select GENERIC_PINCONF
37 + default BMIPS_GENERIC
39 + Say Y here to enable the Broadcom BCM6358 GPIO driver.
41 config PINCTRL_IPROC_GPIO
42 bool "Broadcom iProc GPIO (with PINCONF) driver"
43 depends on OF_GPIO && (ARCH_BCM_IPROC || COMPILE_TEST)
44 --- a/drivers/pinctrl/bcm/Makefile
45 +++ b/drivers/pinctrl/bcm/Makefile
47 obj-$(CONFIG_PINCTRL_BCM281XX) += pinctrl-bcm281xx.o
48 obj-$(CONFIG_PINCTRL_BCM2835) += pinctrl-bcm2835.o
49 obj-$(CONFIG_PINCTRL_BCM6328) += pinctrl-bcm6328.o
50 +obj-$(CONFIG_PINCTRL_BCM6358) += pinctrl-bcm6358.o
51 obj-$(CONFIG_PINCTRL_IPROC_GPIO) += pinctrl-iproc-gpio.o
52 obj-$(CONFIG_PINCTRL_CYGNUS_MUX) += pinctrl-cygnus-mux.o
53 obj-$(CONFIG_PINCTRL_NS) += pinctrl-ns.o
55 +++ b/drivers/pinctrl/bcm/pinctrl-bcm6358.c
57 +// SPDX-License-Identifier: GPL-2.0+
59 + * Driver for BCM6358 GPIO unit (pinctrl + GPIO)
61 + * Copyright (C) 2021 Álvaro Fernández Rojas <noltari@gmail.com>
62 + * Copyright (C) 2016 Jonas Gorski <jonas.gorski@gmail.com>
65 +#include <linux/bitops.h>
66 +#include <linux/gpio.h>
67 +#include <linux/kernel.h>
68 +#include <linux/mfd/syscon.h>
69 +#include <linux/of.h>
70 +#include <linux/of_gpio.h>
71 +#include <linux/of_irq.h>
72 +#include <linux/platform_device.h>
73 +#include <linux/regmap.h>
75 +#include <linux/pinctrl/machine.h>
76 +#include <linux/pinctrl/pinconf.h>
77 +#include <linux/pinctrl/pinconf-generic.h>
78 +#include <linux/pinctrl/pinmux.h>
81 +#include "../pinctrl-utils.h"
83 +#define MODULE_NAME "bcm6358-pinctrl"
84 +#define BCM6358_NUM_GPIOS 40
86 +#define BANK_SIZE sizeof(uint32_t)
87 +#define PINS_PER_BANK (BANK_SIZE * BITS_PER_BYTE)
89 +#define BCM6358_DIROUT_REG 0x04
90 +#define BCM6358_DATA_REG 0x0c
91 +#define BCM6358_MODE_REG 0x18
93 +#define BCM6358_MODE_MUX_NONE 0
94 +#define BCM6358_MODE_MUX_EBI_CS BIT(5)
95 +#define BCM6358_MODE_MUX_UART1 BIT(6)
96 +#define BCM6358_MODE_MUX_SPI_CS BIT(7)
97 +#define BCM6358_MODE_MUX_ASYNC_MODEM BIT(8)
98 +#define BCM6358_MODE_MUX_LEGACY_LED BIT(9)
99 +#define BCM6358_MODE_MUX_SERIAL_LED BIT(10)
100 +#define BCM6358_MODE_MUX_LED BIT(11)
101 +#define BCM6358_MODE_MUX_UTOPIA BIT(12)
102 +#define BCM6358_MODE_MUX_CLKRST BIT(13)
103 +#define BCM6358_MODE_MUX_PWM_SYN_CLK BIT(14)
104 +#define BCM6358_MODE_MUX_SYS_IRQ BIT(15)
106 +struct bcm6358_pingroup {
108 + const unsigned * const pins;
109 + const unsigned num_pins;
111 + const uint16_t mode_val;
113 + /* non-GPIO function muxes require the gpio direction to be set */
114 + const uint16_t direction;
117 +struct bcm6358_function {
119 + const char * const *groups;
120 + const unsigned num_groups;
123 +struct bcm6358_pinctrl {
124 + struct device *dev;
125 + struct regmap *regs;
126 + struct regmap_field *overlays;
128 + struct pinctrl_dev *pctl_dev;
129 + struct gpio_chip gpio_chip;
130 + struct pinctrl_desc pctl_desc;
131 + struct pinctrl_gpio_range gpio_range;
134 +#define BCM6358_GPIO_PIN(a, b, bit1, bit2, bit3) \
138 + .drv_data = (void *)(BCM6358_MODE_MUX_##bit1 | \
139 + BCM6358_MODE_MUX_##bit2 | \
140 + BCM6358_MODE_MUX_##bit3), \
143 +static const struct pinctrl_pin_desc bcm6358_pins[] = {
144 + BCM6358_GPIO_PIN(0, "gpio0", LED, NONE, NONE),
145 + BCM6358_GPIO_PIN(1, "gpio1", LED, NONE, NONE),
146 + BCM6358_GPIO_PIN(2, "gpio2", LED, NONE, NONE),
147 + BCM6358_GPIO_PIN(3, "gpio3", LED, NONE, NONE),
148 + PINCTRL_PIN(4, "gpio4"),
149 + BCM6358_GPIO_PIN(5, "gpio5", SYS_IRQ, NONE, NONE),
150 + BCM6358_GPIO_PIN(6, "gpio6", SERIAL_LED, NONE, NONE),
151 + BCM6358_GPIO_PIN(7, "gpio7", SERIAL_LED, NONE, NONE),
152 + BCM6358_GPIO_PIN(8, "gpio8", PWM_SYN_CLK, NONE, NONE),
153 + BCM6358_GPIO_PIN(9, "gpio09", LEGACY_LED, NONE, NONE),
154 + BCM6358_GPIO_PIN(10, "gpio10", LEGACY_LED, NONE, NONE),
155 + BCM6358_GPIO_PIN(11, "gpio11", LEGACY_LED, NONE, NONE),
156 + BCM6358_GPIO_PIN(12, "gpio12", LEGACY_LED, ASYNC_MODEM, UTOPIA),
157 + BCM6358_GPIO_PIN(13, "gpio13", LEGACY_LED, ASYNC_MODEM, UTOPIA),
158 + BCM6358_GPIO_PIN(14, "gpio14", LEGACY_LED, ASYNC_MODEM, UTOPIA),
159 + BCM6358_GPIO_PIN(15, "gpio15", LEGACY_LED, ASYNC_MODEM, UTOPIA),
160 + PINCTRL_PIN(16, "gpio16"),
161 + PINCTRL_PIN(17, "gpio17"),
162 + PINCTRL_PIN(18, "gpio18"),
163 + PINCTRL_PIN(19, "gpio19"),
164 + PINCTRL_PIN(20, "gpio20"),
165 + PINCTRL_PIN(21, "gpio21"),
166 + BCM6358_GPIO_PIN(22, "gpio22", UTOPIA, NONE, NONE),
167 + BCM6358_GPIO_PIN(23, "gpio23", UTOPIA, NONE, NONE),
168 + BCM6358_GPIO_PIN(24, "gpio24", UTOPIA, NONE, NONE),
169 + BCM6358_GPIO_PIN(25, "gpio25", UTOPIA, NONE, NONE),
170 + BCM6358_GPIO_PIN(26, "gpio26", UTOPIA, NONE, NONE),
171 + BCM6358_GPIO_PIN(27, "gpio27", UTOPIA, NONE, NONE),
172 + BCM6358_GPIO_PIN(28, "gpio28", UTOPIA, UART1, NONE),
173 + BCM6358_GPIO_PIN(29, "gpio29", UTOPIA, UART1, NONE),
174 + BCM6358_GPIO_PIN(30, "gpio30", UTOPIA, UART1, EBI_CS),
175 + BCM6358_GPIO_PIN(31, "gpio31", UTOPIA, UART1, EBI_CS),
176 + BCM6358_GPIO_PIN(32, "gpio32", SPI_CS, NONE, NONE),
177 + BCM6358_GPIO_PIN(33, "gpio33", SPI_CS, NONE, NONE),
178 + PINCTRL_PIN(34, "gpio34"),
179 + PINCTRL_PIN(35, "gpio35"),
180 + PINCTRL_PIN(36, "gpio36"),
181 + PINCTRL_PIN(37, "gpio37"),
182 + PINCTRL_PIN(38, "gpio38"),
183 + PINCTRL_PIN(39, "gpio39"),
186 +static unsigned ebi_cs_grp_pins[] = { 30, 31 };
188 +static unsigned uart1_grp_pins[] = { 28, 29, 30, 31 };
190 +static unsigned spi_cs_grp_pins[] = { 32, 33 };
192 +static unsigned async_modem_grp_pins[] = { 12, 13, 14, 15 };
194 +static unsigned serial_led_grp_pins[] = { 6, 7 };
196 +static unsigned legacy_led_grp_pins[] = { 9, 10, 11, 12, 13, 14, 15 };
198 +static unsigned led_grp_pins[] = { 0, 1, 2, 3 };
200 +static unsigned utopia_grp_pins[] = {
201 + 12, 13, 14, 15, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
204 +static unsigned pwm_syn_clk_grp_pins[] = { 8 };
206 +static unsigned sys_irq_grp_pins[] = { 5 };
208 +#define BCM6358_GPIO_MUX_GROUP(n, bit, dir) \
211 + .pins = n##_pins, \
212 + .num_pins = ARRAY_SIZE(n##_pins), \
213 + .mode_val = BCM6358_MODE_MUX_##bit, \
214 + .direction = dir, \
217 +static const struct bcm6358_pingroup bcm6358_groups[] = {
218 + BCM6358_GPIO_MUX_GROUP(ebi_cs_grp, EBI_CS, 0x3),
219 + BCM6358_GPIO_MUX_GROUP(uart1_grp, UART1, 0x2),
220 + BCM6358_GPIO_MUX_GROUP(spi_cs_grp, SPI_CS, 0x6),
221 + BCM6358_GPIO_MUX_GROUP(async_modem_grp, ASYNC_MODEM, 0x6),
222 + BCM6358_GPIO_MUX_GROUP(legacy_led_grp, LEGACY_LED, 0x7f),
223 + BCM6358_GPIO_MUX_GROUP(serial_led_grp, SERIAL_LED, 0x3),
224 + BCM6358_GPIO_MUX_GROUP(led_grp, LED, 0xf),
225 + BCM6358_GPIO_MUX_GROUP(utopia_grp, UTOPIA, 0x000f),
226 + BCM6358_GPIO_MUX_GROUP(pwm_syn_clk_grp, PWM_SYN_CLK, 0x1),
227 + BCM6358_GPIO_MUX_GROUP(sys_irq_grp, SYS_IRQ, 0x1),
230 +static const char * const ebi_cs_groups[] = {
234 +static const char * const uart1_groups[] = {
238 +static const char * const spi_cs_2_3_groups[] = {
242 +static const char * const async_modem_groups[] = {
246 +static const char * const legacy_led_groups[] = {
250 +static const char * const serial_led_groups[] = {
254 +static const char * const led_groups[] = {
258 +static const char * const clkrst_groups[] = {
262 +static const char * const pwm_syn_clk_groups[] = {
266 +static const char * const sys_irq_groups[] = {
270 +#define BCM6358_FUN(n) \
273 + .groups = n##_groups, \
274 + .num_groups = ARRAY_SIZE(n##_groups), \
277 +static const struct bcm6358_function bcm6358_funcs[] = {
278 + BCM6358_FUN(ebi_cs),
279 + BCM6358_FUN(uart1),
280 + BCM6358_FUN(spi_cs_2_3),
281 + BCM6358_FUN(async_modem),
282 + BCM6358_FUN(legacy_led),
283 + BCM6358_FUN(serial_led),
285 + BCM6358_FUN(clkrst),
286 + BCM6358_FUN(pwm_syn_clk),
287 + BCM6358_FUN(sys_irq),
290 +static inline unsigned int bcm6358_bank_pin(unsigned int pin)
292 + return pin % PINS_PER_BANK;
295 +static inline unsigned int bcm6358_reg_off(unsigned int reg, unsigned int pin)
297 + return reg - (pin / PINS_PER_BANK) * BANK_SIZE;
300 +static int bcm6358_gpio_direction_input(struct gpio_chip *chip,
303 + struct bcm6358_pinctrl *pc = gpiochip_get_data(chip);
304 + unsigned int dirout = bcm6358_reg_off(BCM6358_DIROUT_REG, pin);
305 + unsigned int bank_pin = bcm6358_bank_pin(pin);
309 + * Check with the pinctrl driver whether this pin is usable as
312 + ret = pinctrl_gpio_direction_input(chip->base + pin);
316 + regmap_update_bits(pc->regs, dirout, BIT(bank_pin), 0);
321 +static int bcm6358_gpio_direction_output(struct gpio_chip *chip,
322 + unsigned int pin, int value)
324 + struct bcm6358_pinctrl *pc = gpiochip_get_data(chip);
325 + unsigned int data = bcm6358_reg_off(BCM6358_DATA_REG, pin);
326 + unsigned int dirout = bcm6358_reg_off(BCM6358_DIROUT_REG, pin);
327 + unsigned int bank_pin = bcm6358_bank_pin(pin);
328 + unsigned int val = value ? BIT(bank_pin) : 0;
332 + * Check with the pinctrl driver whether this pin is usable as
335 + ret = pinctrl_gpio_direction_output(chip->base + pin);
339 + regmap_update_bits(pc->regs, dirout, BIT(bank_pin), BIT(bank_pin));
340 + regmap_update_bits(pc->regs, data, BIT(bank_pin), val);
345 +static int bcm6358_gpio_get(struct gpio_chip *chip, unsigned int pin)
347 + struct bcm6358_pinctrl *pc = gpiochip_get_data(chip);
348 + unsigned int data = bcm6358_reg_off(BCM6358_DATA_REG, pin);
349 + unsigned int bank_pin = bcm6358_bank_pin(pin);
352 + regmap_read(pc->regs, data, &val);
354 + return !!(val & BIT(bank_pin));
357 +static int bcm6358_gpio_get_direction(struct gpio_chip *chip, unsigned int pin)
359 + struct bcm6358_pinctrl *pc = gpiochip_get_data(chip);
360 + unsigned int dirout = bcm6358_reg_off(BCM6358_DIROUT_REG, pin);
361 + unsigned int bank_pin = bcm6358_bank_pin(pin);
364 + regmap_read(pc->regs, dirout, &val);
366 + if (val & BIT(bank_pin))
367 + return GPIO_LINE_DIRECTION_OUT;
369 + return GPIO_LINE_DIRECTION_IN;
372 +static void bcm6358_gpio_set(struct gpio_chip *chip, unsigned int pin,
375 + struct bcm6358_pinctrl *pc = gpiochip_get_data(chip);
376 + unsigned int data = bcm6358_reg_off(BCM6358_DATA_REG, pin);
377 + unsigned int bank_pin = bcm6358_bank_pin(pin);
378 + unsigned int val = value ? BIT(bank_pin) : 0;
380 + regmap_update_bits(pc->regs, data, BIT(bank_pin), val);
383 +static int bcm6358_gpio_to_irq(struct gpio_chip *chip, unsigned gpio)
387 + sprintf(irq_name, "gpio%d", gpio);
389 + return of_irq_get_byname(chip->of_node, irq_name);
392 +static int bcm6358_pinctrl_get_group_count(struct pinctrl_dev *pctldev)
394 + return ARRAY_SIZE(bcm6358_groups);
397 +static const char *bcm6358_pinctrl_get_group_name(struct pinctrl_dev *pctldev,
400 + return bcm6358_groups[group].name;
403 +static int bcm6358_pinctrl_get_group_pins(struct pinctrl_dev *pctldev,
404 + unsigned group, const unsigned **pins,
405 + unsigned *num_pins)
407 + *pins = bcm6358_groups[group].pins;
408 + *num_pins = bcm6358_groups[group].num_pins;
413 +static int bcm6358_pinctrl_get_func_count(struct pinctrl_dev *pctldev)
415 + return ARRAY_SIZE(bcm6358_funcs);
418 +static const char *bcm6358_pinctrl_get_func_name(struct pinctrl_dev *pctldev,
421 + return bcm6358_funcs[selector].name;
424 +static int bcm6358_pinctrl_get_groups(struct pinctrl_dev *pctldev,
426 + const char * const **groups,
427 + unsigned * const num_groups)
429 + *groups = bcm6358_funcs[selector].groups;
430 + *num_groups = bcm6358_funcs[selector].num_groups;
435 +static int bcm6358_pinctrl_set_mux(struct pinctrl_dev *pctldev,
436 + unsigned selector, unsigned group)
438 + struct bcm6358_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
439 + const struct bcm6358_pingroup *pg = &bcm6358_groups[group];
440 + unsigned int val = pg->mode_val;
441 + unsigned int mask = val;
444 + for (pin = 0; pin < pg->num_pins; pin++)
445 + mask |= (unsigned long)bcm6358_pins[pin].drv_data;
447 + regmap_field_update_bits(pc->overlays, mask, val);
449 + for (pin = 0; pin < pg->num_pins; pin++) {
450 + int hw_gpio = bcm6358_pins[pin].number;
451 + struct gpio_chip *gc = &pc->gpio_chip;
453 + if (pg->direction & BIT(pin))
454 + gc->direction_output(gc, hw_gpio, 0);
456 + gc->direction_input(gc, hw_gpio);
462 +static int bcm6358_gpio_request_enable(struct pinctrl_dev *pctldev,
463 + struct pinctrl_gpio_range *range,
466 + struct bcm6358_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
469 + mask = (unsigned long) bcm6358_pins[offset].drv_data;
473 + /* disable all functions using this pin */
474 + return regmap_field_update_bits(pc->overlays, mask, 0);
477 +static struct pinctrl_ops bcm6358_pctl_ops = {
478 + .get_groups_count = bcm6358_pinctrl_get_group_count,
479 + .get_group_name = bcm6358_pinctrl_get_group_name,
480 + .get_group_pins = bcm6358_pinctrl_get_group_pins,
481 + .dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
482 + .dt_free_map = pinctrl_utils_free_map,
485 +static struct pinmux_ops bcm6358_pmx_ops = {
486 + .get_functions_count = bcm6358_pinctrl_get_func_count,
487 + .get_function_name = bcm6358_pinctrl_get_func_name,
488 + .get_function_groups = bcm6358_pinctrl_get_groups,
489 + .set_mux = bcm6358_pinctrl_set_mux,
490 + .gpio_request_enable = bcm6358_gpio_request_enable,
494 +static int bcm6358_pinctrl_probe(struct platform_device *pdev)
496 + struct reg_field overlays = REG_FIELD(BCM6358_MODE_REG, 0, 15);
497 + struct device *dev = &pdev->dev;
498 + struct device_node *np = dev->of_node;
499 + struct bcm6358_pinctrl *pc;
502 + pc = devm_kzalloc(dev, sizeof(*pc), GFP_KERNEL);
506 + platform_set_drvdata(pdev, pc);
509 + pc->regs = syscon_node_to_regmap(dev->parent->of_node);
510 + if (IS_ERR(pc->regs))
511 + return PTR_ERR(pc->regs);
513 + pc->overlays = devm_regmap_field_alloc(&pdev->dev, pc->regs, overlays);
514 + if (IS_ERR(pc->overlays))
515 + return PTR_ERR(pc->overlays);
517 + /* disable all muxes by default */
518 + regmap_field_write(pc->overlays, 0);
520 + pc->gpio_chip.label = MODULE_NAME;
521 + pc->gpio_chip.owner = THIS_MODULE;
522 + pc->gpio_chip.request = gpiochip_generic_request;
523 + pc->gpio_chip.free = gpiochip_generic_free;
524 + pc->gpio_chip.direction_input = bcm6358_gpio_direction_input;
525 + pc->gpio_chip.direction_output = bcm6358_gpio_direction_output;
526 + pc->gpio_chip.get_direction = bcm6358_gpio_get_direction;
527 + pc->gpio_chip.get = bcm6358_gpio_get;
528 + pc->gpio_chip.set = bcm6358_gpio_set;
529 + pc->gpio_chip.set_config = gpiochip_generic_config;
530 + pc->gpio_chip.base = -1;
531 + pc->gpio_chip.ngpio = BCM6358_NUM_GPIOS;
532 + pc->gpio_chip.can_sleep = false;
533 + pc->gpio_chip.parent = dev;
534 + pc->gpio_chip.of_node = np;
536 + if (of_get_property(np, "interrupt-names", NULL))
537 + pc->gpio_chip.to_irq = bcm6358_gpio_to_irq;
539 + err = gpiochip_add_data(&pc->gpio_chip, pc);
541 + dev_err(dev, "could not add GPIO chip\n");
545 + pc->pctl_desc.name = MODULE_NAME,
546 + pc->pctl_desc.pins = bcm6358_pins,
547 + pc->pctl_desc.npins = ARRAY_SIZE(bcm6358_pins),
548 + pc->pctl_desc.pctlops = &bcm6358_pctl_ops,
549 + pc->pctl_desc.pmxops = &bcm6358_pmx_ops,
550 + pc->pctl_desc.owner = THIS_MODULE,
552 + pc->pctl_dev = devm_pinctrl_register(dev, &pc->pctl_desc, pc);
553 + if (IS_ERR(pc->pctl_dev)) {
554 + gpiochip_remove(&pc->gpio_chip);
555 + return PTR_ERR(pc->pctl_dev);
558 + pc->gpio_range.name = MODULE_NAME;
559 + pc->gpio_range.npins = BCM6358_NUM_GPIOS;
560 + pc->gpio_range.base = pc->gpio_chip.base;
561 + pc->gpio_range.gc = &pc->gpio_chip;
562 + pinctrl_add_gpio_range(pc->pctl_dev, &pc->gpio_range);
564 + dev_info(dev, "registered\n");
569 +static const struct of_device_id bcm6358_pinctrl_match[] = {
570 + { .compatible = "brcm,bcm6358-pinctrl", },
574 +static struct platform_driver bcm6358_pinctrl_driver = {
575 + .probe = bcm6358_pinctrl_probe,
577 + .name = MODULE_NAME,
578 + .of_match_table = bcm6358_pinctrl_match,
582 +builtin_platform_driver(bcm6358_pinctrl_driver);