19494b169SÁlvaro Fernández Rojas // SPDX-License-Identifier: GPL-2.0+
29494b169SÁlvaro Fernández Rojas /*
39494b169SÁlvaro Fernández Rojas * Driver for BCM6358 GPIO unit (pinctrl + GPIO)
49494b169SÁlvaro Fernández Rojas *
59494b169SÁlvaro Fernández Rojas * Copyright (C) 2021 Álvaro Fernández Rojas <noltari@gmail.com>
69494b169SÁlvaro Fernández Rojas * Copyright (C) 2016 Jonas Gorski <jonas.gorski@gmail.com>
79494b169SÁlvaro Fernández Rojas */
89494b169SÁlvaro Fernández Rojas
99494b169SÁlvaro Fernández Rojas #include <linux/bits.h>
109494b169SÁlvaro Fernández Rojas #include <linux/gpio/driver.h>
119494b169SÁlvaro Fernández Rojas #include <linux/kernel.h>
129494b169SÁlvaro Fernández Rojas #include <linux/of.h>
139494b169SÁlvaro Fernández Rojas #include <linux/pinctrl/pinmux.h>
149494b169SÁlvaro Fernández Rojas #include <linux/platform_device.h>
159494b169SÁlvaro Fernández Rojas #include <linux/regmap.h>
169494b169SÁlvaro Fernández Rojas
179494b169SÁlvaro Fernández Rojas #include "../pinctrl-utils.h"
189494b169SÁlvaro Fernández Rojas
199494b169SÁlvaro Fernández Rojas #include "pinctrl-bcm63xx.h"
209494b169SÁlvaro Fernández Rojas
219494b169SÁlvaro Fernández Rojas #define BCM6358_NUM_GPIOS 40
229494b169SÁlvaro Fernández Rojas
239494b169SÁlvaro Fernández Rojas #define BCM6358_MODE_REG 0x18
249494b169SÁlvaro Fernández Rojas #define BCM6358_MODE_MUX_NONE 0
259494b169SÁlvaro Fernández Rojas #define BCM6358_MODE_MUX_EBI_CS BIT(5)
269494b169SÁlvaro Fernández Rojas #define BCM6358_MODE_MUX_UART1 BIT(6)
279494b169SÁlvaro Fernández Rojas #define BCM6358_MODE_MUX_SPI_CS BIT(7)
289494b169SÁlvaro Fernández Rojas #define BCM6358_MODE_MUX_ASYNC_MODEM BIT(8)
299494b169SÁlvaro Fernández Rojas #define BCM6358_MODE_MUX_LEGACY_LED BIT(9)
309494b169SÁlvaro Fernández Rojas #define BCM6358_MODE_MUX_SERIAL_LED BIT(10)
319494b169SÁlvaro Fernández Rojas #define BCM6358_MODE_MUX_LED BIT(11)
329494b169SÁlvaro Fernández Rojas #define BCM6358_MODE_MUX_UTOPIA BIT(12)
339494b169SÁlvaro Fernández Rojas #define BCM6358_MODE_MUX_CLKRST BIT(13)
349494b169SÁlvaro Fernández Rojas #define BCM6358_MODE_MUX_PWM_SYN_CLK BIT(14)
359494b169SÁlvaro Fernández Rojas #define BCM6358_MODE_MUX_SYS_IRQ BIT(15)
369494b169SÁlvaro Fernández Rojas
379494b169SÁlvaro Fernández Rojas struct bcm6358_pingroup {
38*0e3db163SAndy Shevchenko struct pingroup grp;
399494b169SÁlvaro Fernández Rojas
409494b169SÁlvaro Fernández Rojas const uint16_t mode_val;
419494b169SÁlvaro Fernández Rojas
429494b169SÁlvaro Fernández Rojas /* non-GPIO function muxes require the gpio direction to be set */
439494b169SÁlvaro Fernández Rojas const uint16_t direction;
449494b169SÁlvaro Fernández Rojas };
459494b169SÁlvaro Fernández Rojas
469494b169SÁlvaro Fernández Rojas struct bcm6358_function {
479494b169SÁlvaro Fernández Rojas const char *name;
489494b169SÁlvaro Fernández Rojas const char * const *groups;
499494b169SÁlvaro Fernández Rojas const unsigned num_groups;
509494b169SÁlvaro Fernández Rojas };
519494b169SÁlvaro Fernández Rojas
529494b169SÁlvaro Fernández Rojas struct bcm6358_priv {
539494b169SÁlvaro Fernández Rojas struct regmap_field *overlays;
549494b169SÁlvaro Fernández Rojas };
559494b169SÁlvaro Fernández Rojas
569494b169SÁlvaro Fernández Rojas #define BCM6358_GPIO_PIN(a, b, bit1, bit2, bit3) \
579494b169SÁlvaro Fernández Rojas { \
589494b169SÁlvaro Fernández Rojas .number = a, \
599494b169SÁlvaro Fernández Rojas .name = b, \
609494b169SÁlvaro Fernández Rojas .drv_data = (void *)(BCM6358_MODE_MUX_##bit1 | \
619494b169SÁlvaro Fernández Rojas BCM6358_MODE_MUX_##bit2 | \
629494b169SÁlvaro Fernández Rojas BCM6358_MODE_MUX_##bit3), \
639494b169SÁlvaro Fernández Rojas }
649494b169SÁlvaro Fernández Rojas
659494b169SÁlvaro Fernández Rojas static const struct pinctrl_pin_desc bcm6358_pins[] = {
669494b169SÁlvaro Fernández Rojas BCM6358_GPIO_PIN(0, "gpio0", LED, NONE, NONE),
679494b169SÁlvaro Fernández Rojas BCM6358_GPIO_PIN(1, "gpio1", LED, NONE, NONE),
689494b169SÁlvaro Fernández Rojas BCM6358_GPIO_PIN(2, "gpio2", LED, NONE, NONE),
699494b169SÁlvaro Fernández Rojas BCM6358_GPIO_PIN(3, "gpio3", LED, NONE, NONE),
709494b169SÁlvaro Fernández Rojas PINCTRL_PIN(4, "gpio4"),
719494b169SÁlvaro Fernández Rojas BCM6358_GPIO_PIN(5, "gpio5", SYS_IRQ, NONE, NONE),
729494b169SÁlvaro Fernández Rojas BCM6358_GPIO_PIN(6, "gpio6", SERIAL_LED, NONE, NONE),
739494b169SÁlvaro Fernández Rojas BCM6358_GPIO_PIN(7, "gpio7", SERIAL_LED, NONE, NONE),
749494b169SÁlvaro Fernández Rojas BCM6358_GPIO_PIN(8, "gpio8", PWM_SYN_CLK, NONE, NONE),
759494b169SÁlvaro Fernández Rojas BCM6358_GPIO_PIN(9, "gpio09", LEGACY_LED, NONE, NONE),
769494b169SÁlvaro Fernández Rojas BCM6358_GPIO_PIN(10, "gpio10", LEGACY_LED, NONE, NONE),
779494b169SÁlvaro Fernández Rojas BCM6358_GPIO_PIN(11, "gpio11", LEGACY_LED, NONE, NONE),
789494b169SÁlvaro Fernández Rojas BCM6358_GPIO_PIN(12, "gpio12", LEGACY_LED, ASYNC_MODEM, UTOPIA),
799494b169SÁlvaro Fernández Rojas BCM6358_GPIO_PIN(13, "gpio13", LEGACY_LED, ASYNC_MODEM, UTOPIA),
809494b169SÁlvaro Fernández Rojas BCM6358_GPIO_PIN(14, "gpio14", LEGACY_LED, ASYNC_MODEM, UTOPIA),
819494b169SÁlvaro Fernández Rojas BCM6358_GPIO_PIN(15, "gpio15", LEGACY_LED, ASYNC_MODEM, UTOPIA),
829494b169SÁlvaro Fernández Rojas PINCTRL_PIN(16, "gpio16"),
839494b169SÁlvaro Fernández Rojas PINCTRL_PIN(17, "gpio17"),
849494b169SÁlvaro Fernández Rojas PINCTRL_PIN(18, "gpio18"),
859494b169SÁlvaro Fernández Rojas PINCTRL_PIN(19, "gpio19"),
869494b169SÁlvaro Fernández Rojas PINCTRL_PIN(20, "gpio20"),
879494b169SÁlvaro Fernández Rojas PINCTRL_PIN(21, "gpio21"),
889494b169SÁlvaro Fernández Rojas BCM6358_GPIO_PIN(22, "gpio22", UTOPIA, NONE, NONE),
899494b169SÁlvaro Fernández Rojas BCM6358_GPIO_PIN(23, "gpio23", UTOPIA, NONE, NONE),
909494b169SÁlvaro Fernández Rojas BCM6358_GPIO_PIN(24, "gpio24", UTOPIA, NONE, NONE),
919494b169SÁlvaro Fernández Rojas BCM6358_GPIO_PIN(25, "gpio25", UTOPIA, NONE, NONE),
929494b169SÁlvaro Fernández Rojas BCM6358_GPIO_PIN(26, "gpio26", UTOPIA, NONE, NONE),
939494b169SÁlvaro Fernández Rojas BCM6358_GPIO_PIN(27, "gpio27", UTOPIA, NONE, NONE),
949494b169SÁlvaro Fernández Rojas BCM6358_GPIO_PIN(28, "gpio28", UTOPIA, UART1, NONE),
959494b169SÁlvaro Fernández Rojas BCM6358_GPIO_PIN(29, "gpio29", UTOPIA, UART1, NONE),
969494b169SÁlvaro Fernández Rojas BCM6358_GPIO_PIN(30, "gpio30", UTOPIA, UART1, EBI_CS),
979494b169SÁlvaro Fernández Rojas BCM6358_GPIO_PIN(31, "gpio31", UTOPIA, UART1, EBI_CS),
989494b169SÁlvaro Fernández Rojas BCM6358_GPIO_PIN(32, "gpio32", SPI_CS, NONE, NONE),
999494b169SÁlvaro Fernández Rojas BCM6358_GPIO_PIN(33, "gpio33", SPI_CS, NONE, NONE),
1009494b169SÁlvaro Fernández Rojas PINCTRL_PIN(34, "gpio34"),
1019494b169SÁlvaro Fernández Rojas PINCTRL_PIN(35, "gpio35"),
1029494b169SÁlvaro Fernández Rojas PINCTRL_PIN(36, "gpio36"),
1039494b169SÁlvaro Fernández Rojas PINCTRL_PIN(37, "gpio37"),
1049494b169SÁlvaro Fernández Rojas PINCTRL_PIN(38, "gpio38"),
1059494b169SÁlvaro Fernández Rojas PINCTRL_PIN(39, "gpio39"),
1069494b169SÁlvaro Fernández Rojas };
1079494b169SÁlvaro Fernández Rojas
1089494b169SÁlvaro Fernández Rojas static unsigned ebi_cs_grp_pins[] = { 30, 31 };
1099494b169SÁlvaro Fernández Rojas
1109494b169SÁlvaro Fernández Rojas static unsigned uart1_grp_pins[] = { 28, 29, 30, 31 };
1119494b169SÁlvaro Fernández Rojas
1129494b169SÁlvaro Fernández Rojas static unsigned spi_cs_grp_pins[] = { 32, 33 };
1139494b169SÁlvaro Fernández Rojas
1149494b169SÁlvaro Fernández Rojas static unsigned async_modem_grp_pins[] = { 12, 13, 14, 15 };
1159494b169SÁlvaro Fernández Rojas
1169494b169SÁlvaro Fernández Rojas static unsigned serial_led_grp_pins[] = { 6, 7 };
1179494b169SÁlvaro Fernández Rojas
1189494b169SÁlvaro Fernández Rojas static unsigned legacy_led_grp_pins[] = { 9, 10, 11, 12, 13, 14, 15 };
1199494b169SÁlvaro Fernández Rojas
1209494b169SÁlvaro Fernández Rojas static unsigned led_grp_pins[] = { 0, 1, 2, 3 };
1219494b169SÁlvaro Fernández Rojas
1229494b169SÁlvaro Fernández Rojas static unsigned utopia_grp_pins[] = {
1239494b169SÁlvaro Fernández Rojas 12, 13, 14, 15, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
1249494b169SÁlvaro Fernández Rojas };
1259494b169SÁlvaro Fernández Rojas
1269494b169SÁlvaro Fernández Rojas static unsigned pwm_syn_clk_grp_pins[] = { 8 };
1279494b169SÁlvaro Fernández Rojas
1289494b169SÁlvaro Fernández Rojas static unsigned sys_irq_grp_pins[] = { 5 };
1299494b169SÁlvaro Fernández Rojas
1309494b169SÁlvaro Fernández Rojas #define BCM6358_GPIO_MUX_GROUP(n, bit, dir) \
1319494b169SÁlvaro Fernández Rojas { \
132*0e3db163SAndy Shevchenko .grp = BCM_PIN_GROUP(n), \
1339494b169SÁlvaro Fernández Rojas .mode_val = BCM6358_MODE_MUX_##bit, \
1349494b169SÁlvaro Fernández Rojas .direction = dir, \
1359494b169SÁlvaro Fernández Rojas }
1369494b169SÁlvaro Fernández Rojas
1379494b169SÁlvaro Fernández Rojas static const struct bcm6358_pingroup bcm6358_groups[] = {
1389494b169SÁlvaro Fernández Rojas BCM6358_GPIO_MUX_GROUP(ebi_cs_grp, EBI_CS, 0x3),
1399494b169SÁlvaro Fernández Rojas BCM6358_GPIO_MUX_GROUP(uart1_grp, UART1, 0x2),
1409494b169SÁlvaro Fernández Rojas BCM6358_GPIO_MUX_GROUP(spi_cs_grp, SPI_CS, 0x6),
1419494b169SÁlvaro Fernández Rojas BCM6358_GPIO_MUX_GROUP(async_modem_grp, ASYNC_MODEM, 0x6),
1429494b169SÁlvaro Fernández Rojas BCM6358_GPIO_MUX_GROUP(legacy_led_grp, LEGACY_LED, 0x7f),
1439494b169SÁlvaro Fernández Rojas BCM6358_GPIO_MUX_GROUP(serial_led_grp, SERIAL_LED, 0x3),
1449494b169SÁlvaro Fernández Rojas BCM6358_GPIO_MUX_GROUP(led_grp, LED, 0xf),
1459494b169SÁlvaro Fernández Rojas BCM6358_GPIO_MUX_GROUP(utopia_grp, UTOPIA, 0x000f),
1469494b169SÁlvaro Fernández Rojas BCM6358_GPIO_MUX_GROUP(pwm_syn_clk_grp, PWM_SYN_CLK, 0x1),
1479494b169SÁlvaro Fernández Rojas BCM6358_GPIO_MUX_GROUP(sys_irq_grp, SYS_IRQ, 0x1),
1489494b169SÁlvaro Fernández Rojas };
1499494b169SÁlvaro Fernández Rojas
1509494b169SÁlvaro Fernández Rojas static const char * const ebi_cs_groups[] = {
1519494b169SÁlvaro Fernández Rojas "ebi_cs_grp"
1529494b169SÁlvaro Fernández Rojas };
1539494b169SÁlvaro Fernández Rojas
1549494b169SÁlvaro Fernández Rojas static const char * const uart1_groups[] = {
1559494b169SÁlvaro Fernández Rojas "uart1_grp"
1569494b169SÁlvaro Fernández Rojas };
1579494b169SÁlvaro Fernández Rojas
1589494b169SÁlvaro Fernández Rojas static const char * const spi_cs_2_3_groups[] = {
1599494b169SÁlvaro Fernández Rojas "spi_cs_2_3_grp"
1609494b169SÁlvaro Fernández Rojas };
1619494b169SÁlvaro Fernández Rojas
1629494b169SÁlvaro Fernández Rojas static const char * const async_modem_groups[] = {
1639494b169SÁlvaro Fernández Rojas "async_modem_grp"
1649494b169SÁlvaro Fernández Rojas };
1659494b169SÁlvaro Fernández Rojas
1669494b169SÁlvaro Fernández Rojas static const char * const legacy_led_groups[] = {
1679494b169SÁlvaro Fernández Rojas "legacy_led_grp",
1689494b169SÁlvaro Fernández Rojas };
1699494b169SÁlvaro Fernández Rojas
1709494b169SÁlvaro Fernández Rojas static const char * const serial_led_groups[] = {
1719494b169SÁlvaro Fernández Rojas "serial_led_grp",
1729494b169SÁlvaro Fernández Rojas };
1739494b169SÁlvaro Fernández Rojas
1749494b169SÁlvaro Fernández Rojas static const char * const led_groups[] = {
1759494b169SÁlvaro Fernández Rojas "led_grp",
1769494b169SÁlvaro Fernández Rojas };
1779494b169SÁlvaro Fernández Rojas
1789494b169SÁlvaro Fernández Rojas static const char * const clkrst_groups[] = {
1799494b169SÁlvaro Fernández Rojas "clkrst_grp",
1809494b169SÁlvaro Fernández Rojas };
1819494b169SÁlvaro Fernández Rojas
1829494b169SÁlvaro Fernández Rojas static const char * const pwm_syn_clk_groups[] = {
1839494b169SÁlvaro Fernández Rojas "pwm_syn_clk_grp",
1849494b169SÁlvaro Fernández Rojas };
1859494b169SÁlvaro Fernández Rojas
1869494b169SÁlvaro Fernández Rojas static const char * const sys_irq_groups[] = {
1879494b169SÁlvaro Fernández Rojas "sys_irq_grp",
1889494b169SÁlvaro Fernández Rojas };
1899494b169SÁlvaro Fernández Rojas
1909494b169SÁlvaro Fernández Rojas #define BCM6358_FUN(n) \
1919494b169SÁlvaro Fernández Rojas { \
1929494b169SÁlvaro Fernández Rojas .name = #n, \
1939494b169SÁlvaro Fernández Rojas .groups = n##_groups, \
1949494b169SÁlvaro Fernández Rojas .num_groups = ARRAY_SIZE(n##_groups), \
1959494b169SÁlvaro Fernández Rojas }
1969494b169SÁlvaro Fernández Rojas
1979494b169SÁlvaro Fernández Rojas static const struct bcm6358_function bcm6358_funcs[] = {
1989494b169SÁlvaro Fernández Rojas BCM6358_FUN(ebi_cs),
1999494b169SÁlvaro Fernández Rojas BCM6358_FUN(uart1),
2009494b169SÁlvaro Fernández Rojas BCM6358_FUN(spi_cs_2_3),
2019494b169SÁlvaro Fernández Rojas BCM6358_FUN(async_modem),
2029494b169SÁlvaro Fernández Rojas BCM6358_FUN(legacy_led),
2039494b169SÁlvaro Fernández Rojas BCM6358_FUN(serial_led),
2049494b169SÁlvaro Fernández Rojas BCM6358_FUN(led),
2059494b169SÁlvaro Fernández Rojas BCM6358_FUN(clkrst),
2069494b169SÁlvaro Fernández Rojas BCM6358_FUN(pwm_syn_clk),
2079494b169SÁlvaro Fernández Rojas BCM6358_FUN(sys_irq),
2089494b169SÁlvaro Fernández Rojas };
2099494b169SÁlvaro Fernández Rojas
bcm6358_pinctrl_get_group_count(struct pinctrl_dev * pctldev)2109494b169SÁlvaro Fernández Rojas static int bcm6358_pinctrl_get_group_count(struct pinctrl_dev *pctldev)
2119494b169SÁlvaro Fernández Rojas {
2129494b169SÁlvaro Fernández Rojas return ARRAY_SIZE(bcm6358_groups);
2139494b169SÁlvaro Fernández Rojas }
2149494b169SÁlvaro Fernández Rojas
bcm6358_pinctrl_get_group_name(struct pinctrl_dev * pctldev,unsigned group)2159494b169SÁlvaro Fernández Rojas static const char *bcm6358_pinctrl_get_group_name(struct pinctrl_dev *pctldev,
2169494b169SÁlvaro Fernández Rojas unsigned group)
2179494b169SÁlvaro Fernández Rojas {
218*0e3db163SAndy Shevchenko return bcm6358_groups[group].grp.name;
2199494b169SÁlvaro Fernández Rojas }
2209494b169SÁlvaro Fernández Rojas
bcm6358_pinctrl_get_group_pins(struct pinctrl_dev * pctldev,unsigned group,const unsigned ** pins,unsigned * npins)2219494b169SÁlvaro Fernández Rojas static int bcm6358_pinctrl_get_group_pins(struct pinctrl_dev *pctldev,
2229494b169SÁlvaro Fernández Rojas unsigned group, const unsigned **pins,
223*0e3db163SAndy Shevchenko unsigned *npins)
2249494b169SÁlvaro Fernández Rojas {
225*0e3db163SAndy Shevchenko *pins = bcm6358_groups[group].grp.pins;
226*0e3db163SAndy Shevchenko *npins = bcm6358_groups[group].grp.npins;
2279494b169SÁlvaro Fernández Rojas
2289494b169SÁlvaro Fernández Rojas return 0;
2299494b169SÁlvaro Fernández Rojas }
2309494b169SÁlvaro Fernández Rojas
bcm6358_pinctrl_get_func_count(struct pinctrl_dev * pctldev)2319494b169SÁlvaro Fernández Rojas static int bcm6358_pinctrl_get_func_count(struct pinctrl_dev *pctldev)
2329494b169SÁlvaro Fernández Rojas {
2339494b169SÁlvaro Fernández Rojas return ARRAY_SIZE(bcm6358_funcs);
2349494b169SÁlvaro Fernández Rojas }
2359494b169SÁlvaro Fernández Rojas
bcm6358_pinctrl_get_func_name(struct pinctrl_dev * pctldev,unsigned selector)2369494b169SÁlvaro Fernández Rojas static const char *bcm6358_pinctrl_get_func_name(struct pinctrl_dev *pctldev,
2379494b169SÁlvaro Fernández Rojas unsigned selector)
2389494b169SÁlvaro Fernández Rojas {
2399494b169SÁlvaro Fernández Rojas return bcm6358_funcs[selector].name;
2409494b169SÁlvaro Fernández Rojas }
2419494b169SÁlvaro Fernández Rojas
bcm6358_pinctrl_get_groups(struct pinctrl_dev * pctldev,unsigned selector,const char * const ** groups,unsigned * const num_groups)2429494b169SÁlvaro Fernández Rojas static int bcm6358_pinctrl_get_groups(struct pinctrl_dev *pctldev,
2439494b169SÁlvaro Fernández Rojas unsigned selector,
2449494b169SÁlvaro Fernández Rojas const char * const **groups,
2459494b169SÁlvaro Fernández Rojas unsigned * const num_groups)
2469494b169SÁlvaro Fernández Rojas {
2479494b169SÁlvaro Fernández Rojas *groups = bcm6358_funcs[selector].groups;
2489494b169SÁlvaro Fernández Rojas *num_groups = bcm6358_funcs[selector].num_groups;
2499494b169SÁlvaro Fernández Rojas
2509494b169SÁlvaro Fernández Rojas return 0;
2519494b169SÁlvaro Fernández Rojas }
2529494b169SÁlvaro Fernández Rojas
bcm6358_pinctrl_set_mux(struct pinctrl_dev * pctldev,unsigned selector,unsigned group)2539494b169SÁlvaro Fernández Rojas static int bcm6358_pinctrl_set_mux(struct pinctrl_dev *pctldev,
2549494b169SÁlvaro Fernández Rojas unsigned selector, unsigned group)
2559494b169SÁlvaro Fernández Rojas {
2569494b169SÁlvaro Fernández Rojas struct bcm63xx_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
2579494b169SÁlvaro Fernández Rojas struct bcm6358_priv *priv = pc->driver_data;
2589494b169SÁlvaro Fernández Rojas const struct bcm6358_pingroup *pg = &bcm6358_groups[group];
2599494b169SÁlvaro Fernández Rojas unsigned int val = pg->mode_val;
2609494b169SÁlvaro Fernández Rojas unsigned int mask = val;
2619494b169SÁlvaro Fernández Rojas unsigned pin;
2629494b169SÁlvaro Fernández Rojas
263*0e3db163SAndy Shevchenko for (pin = 0; pin < pg->grp.npins; pin++)
2649494b169SÁlvaro Fernández Rojas mask |= (unsigned long)bcm6358_pins[pin].drv_data;
2659494b169SÁlvaro Fernández Rojas
2669494b169SÁlvaro Fernández Rojas regmap_field_update_bits(priv->overlays, mask, val);
2679494b169SÁlvaro Fernández Rojas
268*0e3db163SAndy Shevchenko for (pin = 0; pin < pg->grp.npins; pin++) {
2699494b169SÁlvaro Fernández Rojas struct pinctrl_gpio_range *range;
2709494b169SÁlvaro Fernández Rojas unsigned int hw_gpio = bcm6358_pins[pin].number;
2719494b169SÁlvaro Fernández Rojas
2729494b169SÁlvaro Fernández Rojas range = pinctrl_find_gpio_range_from_pin(pctldev, hw_gpio);
2739494b169SÁlvaro Fernández Rojas if (range) {
2749494b169SÁlvaro Fernández Rojas struct gpio_chip *gc = range->gc;
2759494b169SÁlvaro Fernández Rojas
2769494b169SÁlvaro Fernández Rojas if (pg->direction & BIT(pin))
2779494b169SÁlvaro Fernández Rojas gc->direction_output(gc, hw_gpio, 0);
2789494b169SÁlvaro Fernández Rojas else
2799494b169SÁlvaro Fernández Rojas gc->direction_input(gc, hw_gpio);
2809494b169SÁlvaro Fernández Rojas }
2819494b169SÁlvaro Fernández Rojas }
2829494b169SÁlvaro Fernández Rojas
2839494b169SÁlvaro Fernández Rojas return 0;
2849494b169SÁlvaro Fernández Rojas }
2859494b169SÁlvaro Fernández Rojas
bcm6358_gpio_request_enable(struct pinctrl_dev * pctldev,struct pinctrl_gpio_range * range,unsigned offset)2869494b169SÁlvaro Fernández Rojas static int bcm6358_gpio_request_enable(struct pinctrl_dev *pctldev,
2879494b169SÁlvaro Fernández Rojas struct pinctrl_gpio_range *range,
2889494b169SÁlvaro Fernández Rojas unsigned offset)
2899494b169SÁlvaro Fernández Rojas {
2909494b169SÁlvaro Fernández Rojas struct bcm63xx_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
2919494b169SÁlvaro Fernández Rojas struct bcm6358_priv *priv = pc->driver_data;
2929494b169SÁlvaro Fernández Rojas unsigned int mask;
2939494b169SÁlvaro Fernández Rojas
2949494b169SÁlvaro Fernández Rojas mask = (unsigned long) bcm6358_pins[offset].drv_data;
2959494b169SÁlvaro Fernández Rojas if (!mask)
2969494b169SÁlvaro Fernández Rojas return 0;
2979494b169SÁlvaro Fernández Rojas
2989494b169SÁlvaro Fernández Rojas /* disable all functions using this pin */
2999494b169SÁlvaro Fernández Rojas return regmap_field_update_bits(priv->overlays, mask, 0);
3009494b169SÁlvaro Fernández Rojas }
3019494b169SÁlvaro Fernández Rojas
302d9779093SRikard Falkeborn static const struct pinctrl_ops bcm6358_pctl_ops = {
3039494b169SÁlvaro Fernández Rojas .dt_free_map = pinctrl_utils_free_map,
3049494b169SÁlvaro Fernández Rojas .dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
3059494b169SÁlvaro Fernández Rojas .get_group_name = bcm6358_pinctrl_get_group_name,
3069494b169SÁlvaro Fernández Rojas .get_group_pins = bcm6358_pinctrl_get_group_pins,
3079494b169SÁlvaro Fernández Rojas .get_groups_count = bcm6358_pinctrl_get_group_count,
3089494b169SÁlvaro Fernández Rojas };
3099494b169SÁlvaro Fernández Rojas
3100c683876SRikard Falkeborn static const struct pinmux_ops bcm6358_pmx_ops = {
3119494b169SÁlvaro Fernández Rojas .get_function_groups = bcm6358_pinctrl_get_groups,
3129494b169SÁlvaro Fernández Rojas .get_function_name = bcm6358_pinctrl_get_func_name,
3139494b169SÁlvaro Fernández Rojas .get_functions_count = bcm6358_pinctrl_get_func_count,
3149494b169SÁlvaro Fernández Rojas .gpio_request_enable = bcm6358_gpio_request_enable,
3159494b169SÁlvaro Fernández Rojas .set_mux = bcm6358_pinctrl_set_mux,
3169494b169SÁlvaro Fernández Rojas .strict = true,
3179494b169SÁlvaro Fernández Rojas };
3189494b169SÁlvaro Fernández Rojas
3199494b169SÁlvaro Fernández Rojas static const struct bcm63xx_pinctrl_soc bcm6358_soc = {
3209494b169SÁlvaro Fernández Rojas .ngpios = BCM6358_NUM_GPIOS,
3219494b169SÁlvaro Fernández Rojas .npins = ARRAY_SIZE(bcm6358_pins),
3229494b169SÁlvaro Fernández Rojas .pctl_ops = &bcm6358_pctl_ops,
3239494b169SÁlvaro Fernández Rojas .pins = bcm6358_pins,
3249494b169SÁlvaro Fernández Rojas .pmx_ops = &bcm6358_pmx_ops,
3259494b169SÁlvaro Fernández Rojas };
3269494b169SÁlvaro Fernández Rojas
bcm6358_pinctrl_probe(struct platform_device * pdev)3279494b169SÁlvaro Fernández Rojas static int bcm6358_pinctrl_probe(struct platform_device *pdev)
3289494b169SÁlvaro Fernández Rojas {
3299494b169SÁlvaro Fernández Rojas struct reg_field overlays = REG_FIELD(BCM6358_MODE_REG, 0, 15);
3309494b169SÁlvaro Fernández Rojas struct device *dev = &pdev->dev;
3319494b169SÁlvaro Fernández Rojas struct bcm63xx_pinctrl *pc;
3329494b169SÁlvaro Fernández Rojas struct bcm6358_priv *priv;
3339494b169SÁlvaro Fernández Rojas int err;
3349494b169SÁlvaro Fernández Rojas
3359494b169SÁlvaro Fernández Rojas priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
3369494b169SÁlvaro Fernández Rojas if (!priv)
3379494b169SÁlvaro Fernández Rojas return -ENOMEM;
3389494b169SÁlvaro Fernández Rojas
3399494b169SÁlvaro Fernández Rojas err = bcm63xx_pinctrl_probe(pdev, &bcm6358_soc, (void *) priv);
3409494b169SÁlvaro Fernández Rojas if (err)
3419494b169SÁlvaro Fernández Rojas return err;
3429494b169SÁlvaro Fernández Rojas
3439494b169SÁlvaro Fernández Rojas pc = platform_get_drvdata(pdev);
3449494b169SÁlvaro Fernández Rojas
3459494b169SÁlvaro Fernández Rojas priv->overlays = devm_regmap_field_alloc(dev, pc->regs, overlays);
3469494b169SÁlvaro Fernández Rojas if (IS_ERR(priv->overlays))
3479494b169SÁlvaro Fernández Rojas return PTR_ERR(priv->overlays);
3489494b169SÁlvaro Fernández Rojas
3499494b169SÁlvaro Fernández Rojas return 0;
3509494b169SÁlvaro Fernández Rojas }
3519494b169SÁlvaro Fernández Rojas
3529494b169SÁlvaro Fernández Rojas static const struct of_device_id bcm6358_pinctrl_match[] = {
3539494b169SÁlvaro Fernández Rojas { .compatible = "brcm,bcm6358-pinctrl", },
3549494b169SÁlvaro Fernández Rojas { /* sentinel */ }
3559494b169SÁlvaro Fernández Rojas };
3569494b169SÁlvaro Fernández Rojas
3579494b169SÁlvaro Fernández Rojas static struct platform_driver bcm6358_pinctrl_driver = {
3589494b169SÁlvaro Fernández Rojas .probe = bcm6358_pinctrl_probe,
3599494b169SÁlvaro Fernández Rojas .driver = {
3609494b169SÁlvaro Fernández Rojas .name = "bcm6358-pinctrl",
3619494b169SÁlvaro Fernández Rojas .of_match_table = bcm6358_pinctrl_match,
3629494b169SÁlvaro Fernández Rojas },
3639494b169SÁlvaro Fernández Rojas };
3649494b169SÁlvaro Fernández Rojas
3659494b169SÁlvaro Fernández Rojas builtin_platform_driver(bcm6358_pinctrl_driver);
366