1 // SPDX-License-Identifier: GPL-2.0+
3 * Driver for BCM6328 GPIO unit (pinctrl + GPIO)
5 * Copyright (C) 2021 Álvaro Fernández Rojas <noltari@gmail.com>
6 * Copyright (C) 2016 Jonas Gorski <jonas.gorski@gmail.com>
9 #include <linux/bits.h>
10 #include <linux/gpio/driver.h>
11 #include <linux/kernel.h>
13 #include <linux/pinctrl/pinmux.h>
14 #include <linux/platform_device.h>
15 #include <linux/regmap.h>
17 #include "../pinctrl-utils.h"
19 #include "pinctrl-bcm63xx.h"
21 #define BCM6328_NUM_GPIOS 32
23 #define BCM6328_MODE_REG 0x18
24 #define BCM6328_MUX_HI_REG 0x1c
25 #define BCM6328_MUX_LO_REG 0x20
26 #define BCM6328_MUX_OTHER_REG 0x24
27 #define BCM6328_MUX_MASK GENMASK(1, 0)
29 struct bcm6328_pingroup {
31 const unsigned * const pins;
32 const unsigned num_pins;
35 struct bcm6328_function {
37 const char * const *groups;
38 const unsigned num_groups;
44 static const unsigned int bcm6328_mux[] = {
50 static const struct pinctrl_pin_desc bcm6328_pins[] = {
51 PINCTRL_PIN(0, "gpio0"),
52 PINCTRL_PIN(1, "gpio1"),
53 PINCTRL_PIN(2, "gpio2"),
54 PINCTRL_PIN(3, "gpio3"),
55 PINCTRL_PIN(4, "gpio4"),
56 PINCTRL_PIN(5, "gpio5"),
57 PINCTRL_PIN(6, "gpio6"),
58 PINCTRL_PIN(7, "gpio7"),
59 PINCTRL_PIN(8, "gpio8"),
60 PINCTRL_PIN(9, "gpio9"),
61 PINCTRL_PIN(10, "gpio10"),
62 PINCTRL_PIN(11, "gpio11"),
63 PINCTRL_PIN(12, "gpio12"),
64 PINCTRL_PIN(13, "gpio13"),
65 PINCTRL_PIN(14, "gpio14"),
66 PINCTRL_PIN(15, "gpio15"),
67 PINCTRL_PIN(16, "gpio16"),
68 PINCTRL_PIN(17, "gpio17"),
69 PINCTRL_PIN(18, "gpio18"),
70 PINCTRL_PIN(19, "gpio19"),
71 PINCTRL_PIN(20, "gpio20"),
72 PINCTRL_PIN(21, "gpio21"),
73 PINCTRL_PIN(22, "gpio22"),
74 PINCTRL_PIN(23, "gpio23"),
75 PINCTRL_PIN(24, "gpio24"),
76 PINCTRL_PIN(25, "gpio25"),
77 PINCTRL_PIN(26, "gpio26"),
78 PINCTRL_PIN(27, "gpio27"),
79 PINCTRL_PIN(28, "gpio28"),
80 PINCTRL_PIN(29, "gpio29"),
81 PINCTRL_PIN(30, "gpio30"),
82 PINCTRL_PIN(31, "gpio31"),
85 * No idea where they really are; so let's put them according
86 * to their mux offsets.
88 PINCTRL_PIN(36, "hsspi_cs1"),
89 PINCTRL_PIN(38, "usb_p2"),
92 static unsigned gpio0_pins[] = { 0 };
93 static unsigned gpio1_pins[] = { 1 };
94 static unsigned gpio2_pins[] = { 2 };
95 static unsigned gpio3_pins[] = { 3 };
96 static unsigned gpio4_pins[] = { 4 };
97 static unsigned gpio5_pins[] = { 5 };
98 static unsigned gpio6_pins[] = { 6 };
99 static unsigned gpio7_pins[] = { 7 };
100 static unsigned gpio8_pins[] = { 8 };
101 static unsigned gpio9_pins[] = { 9 };
102 static unsigned gpio10_pins[] = { 10 };
103 static unsigned gpio11_pins[] = { 11 };
104 static unsigned gpio12_pins[] = { 12 };
105 static unsigned gpio13_pins[] = { 13 };
106 static unsigned gpio14_pins[] = { 14 };
107 static unsigned gpio15_pins[] = { 15 };
108 static unsigned gpio16_pins[] = { 16 };
109 static unsigned gpio17_pins[] = { 17 };
110 static unsigned gpio18_pins[] = { 18 };
111 static unsigned gpio19_pins[] = { 19 };
112 static unsigned gpio20_pins[] = { 20 };
113 static unsigned gpio21_pins[] = { 21 };
114 static unsigned gpio22_pins[] = { 22 };
115 static unsigned gpio23_pins[] = { 23 };
116 static unsigned gpio24_pins[] = { 24 };
117 static unsigned gpio25_pins[] = { 25 };
118 static unsigned gpio26_pins[] = { 26 };
119 static unsigned gpio27_pins[] = { 27 };
120 static unsigned gpio28_pins[] = { 28 };
121 static unsigned gpio29_pins[] = { 29 };
122 static unsigned gpio30_pins[] = { 30 };
123 static unsigned gpio31_pins[] = { 31 };
125 static unsigned hsspi_cs1_pins[] = { 36 };
126 static unsigned usb_port1_pins[] = { 38 };
128 #define BCM6328_GROUP(n) \
132 .num_pins = ARRAY_SIZE(n##_pins), \
135 static struct bcm6328_pingroup bcm6328_groups[] = {
136 BCM6328_GROUP(gpio0),
137 BCM6328_GROUP(gpio1),
138 BCM6328_GROUP(gpio2),
139 BCM6328_GROUP(gpio3),
140 BCM6328_GROUP(gpio4),
141 BCM6328_GROUP(gpio5),
142 BCM6328_GROUP(gpio6),
143 BCM6328_GROUP(gpio7),
144 BCM6328_GROUP(gpio8),
145 BCM6328_GROUP(gpio9),
146 BCM6328_GROUP(gpio10),
147 BCM6328_GROUP(gpio11),
148 BCM6328_GROUP(gpio12),
149 BCM6328_GROUP(gpio13),
150 BCM6328_GROUP(gpio14),
151 BCM6328_GROUP(gpio15),
152 BCM6328_GROUP(gpio16),
153 BCM6328_GROUP(gpio17),
154 BCM6328_GROUP(gpio18),
155 BCM6328_GROUP(gpio19),
156 BCM6328_GROUP(gpio20),
157 BCM6328_GROUP(gpio21),
158 BCM6328_GROUP(gpio22),
159 BCM6328_GROUP(gpio23),
160 BCM6328_GROUP(gpio24),
161 BCM6328_GROUP(gpio25),
162 BCM6328_GROUP(gpio26),
163 BCM6328_GROUP(gpio27),
164 BCM6328_GROUP(gpio28),
165 BCM6328_GROUP(gpio29),
166 BCM6328_GROUP(gpio30),
167 BCM6328_GROUP(gpio31),
169 BCM6328_GROUP(hsspi_cs1),
170 BCM6328_GROUP(usb_port1),
174 static const char * const led_groups[] = {
202 static const char * const serial_led_data_groups[] = {
206 static const char * const serial_led_clk_groups[] = {
210 static const char * const inet_act_led_groups[] = {
214 static const char * const pcie_clkreq_groups[] = {
218 static const char * const ephy0_act_led_groups[] = {
222 static const char * const ephy1_act_led_groups[] = {
226 static const char * const ephy2_act_led_groups[] = {
230 static const char * const ephy3_act_led_groups[] = {
234 static const char * const hsspi_cs1_groups[] = {
238 static const char * const usb_host_port_groups[] = {
242 static const char * const usb_device_port_groups[] = {
246 #define BCM6328_MODE_FUN(n) \
249 .groups = n##_groups, \
250 .num_groups = ARRAY_SIZE(n##_groups), \
254 #define BCM6328_MUX_FUN(n, mux) \
257 .groups = n##_groups, \
258 .num_groups = ARRAY_SIZE(n##_groups), \
262 static const struct bcm6328_function bcm6328_funcs[] = {
263 BCM6328_MODE_FUN(led),
264 BCM6328_MUX_FUN(serial_led_data, 2),
265 BCM6328_MUX_FUN(serial_led_clk, 2),
266 BCM6328_MUX_FUN(inet_act_led, 1),
267 BCM6328_MUX_FUN(pcie_clkreq, 2),
268 BCM6328_MUX_FUN(ephy0_act_led, 1),
269 BCM6328_MUX_FUN(ephy1_act_led, 1),
270 BCM6328_MUX_FUN(ephy2_act_led, 1),
271 BCM6328_MUX_FUN(ephy3_act_led, 1),
272 BCM6328_MUX_FUN(hsspi_cs1, 2),
273 BCM6328_MUX_FUN(usb_host_port, 1),
274 BCM6328_MUX_FUN(usb_device_port, 2),
277 static inline unsigned int bcm6328_mux_off(unsigned int pin)
279 return bcm6328_mux[pin / 16];
282 static int bcm6328_pinctrl_get_group_count(struct pinctrl_dev *pctldev)
284 return ARRAY_SIZE(bcm6328_groups);
287 static const char *bcm6328_pinctrl_get_group_name(struct pinctrl_dev *pctldev,
290 return bcm6328_groups[group].name;
293 static int bcm6328_pinctrl_get_group_pins(struct pinctrl_dev *pctldev,
294 unsigned group, const unsigned **pins,
297 *pins = bcm6328_groups[group].pins;
298 *num_pins = bcm6328_groups[group].num_pins;
303 static int bcm6328_pinctrl_get_func_count(struct pinctrl_dev *pctldev)
305 return ARRAY_SIZE(bcm6328_funcs);
308 static const char *bcm6328_pinctrl_get_func_name(struct pinctrl_dev *pctldev,
311 return bcm6328_funcs[selector].name;
314 static int bcm6328_pinctrl_get_groups(struct pinctrl_dev *pctldev,
316 const char * const **groups,
317 unsigned * const num_groups)
319 *groups = bcm6328_funcs[selector].groups;
320 *num_groups = bcm6328_funcs[selector].num_groups;
325 static void bcm6328_rmw_mux(struct bcm63xx_pinctrl *pc, unsigned pin,
326 unsigned int mode, unsigned int mux)
328 if (pin < BCM6328_NUM_GPIOS)
329 regmap_update_bits(pc->regs, BCM6328_MODE_REG, BIT(pin),
330 mode ? BIT(pin) : 0);
332 regmap_update_bits(pc->regs, bcm6328_mux_off(pin),
333 BCM6328_MUX_MASK << ((pin % 16) * 2),
334 mux << ((pin % 16) * 2));
337 static int bcm6328_pinctrl_set_mux(struct pinctrl_dev *pctldev,
338 unsigned selector, unsigned group)
340 struct bcm63xx_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
341 const struct bcm6328_pingroup *pg = &bcm6328_groups[group];
342 const struct bcm6328_function *f = &bcm6328_funcs[selector];
344 bcm6328_rmw_mux(pc, pg->pins[0], f->mode_val, f->mux_val);
349 static int bcm6328_gpio_request_enable(struct pinctrl_dev *pctldev,
350 struct pinctrl_gpio_range *range,
353 struct bcm63xx_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
355 /* disable all functions using this pin */
356 bcm6328_rmw_mux(pc, offset, 0, 0);
361 static const struct pinctrl_ops bcm6328_pctl_ops = {
362 .dt_free_map = pinctrl_utils_free_map,
363 .dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
364 .get_group_name = bcm6328_pinctrl_get_group_name,
365 .get_group_pins = bcm6328_pinctrl_get_group_pins,
366 .get_groups_count = bcm6328_pinctrl_get_group_count,
369 static const struct pinmux_ops bcm6328_pmx_ops = {
370 .get_function_groups = bcm6328_pinctrl_get_groups,
371 .get_function_name = bcm6328_pinctrl_get_func_name,
372 .get_functions_count = bcm6328_pinctrl_get_func_count,
373 .gpio_request_enable = bcm6328_gpio_request_enable,
374 .set_mux = bcm6328_pinctrl_set_mux,
378 static const struct bcm63xx_pinctrl_soc bcm6328_soc = {
379 .ngpios = BCM6328_NUM_GPIOS,
380 .npins = ARRAY_SIZE(bcm6328_pins),
381 .pctl_ops = &bcm6328_pctl_ops,
382 .pins = bcm6328_pins,
383 .pmx_ops = &bcm6328_pmx_ops,
386 static int bcm6328_pinctrl_probe(struct platform_device *pdev)
388 return bcm63xx_pinctrl_probe(pdev, &bcm6328_soc, NULL);
391 static const struct of_device_id bcm6328_pinctrl_match[] = {
392 { .compatible = "brcm,bcm6328-pinctrl", },
396 static struct platform_driver bcm6328_pinctrl_driver = {
397 .probe = bcm6328_pinctrl_probe,
399 .name = "bcm6328-pinctrl",
400 .of_match_table = bcm6328_pinctrl_match,
404 builtin_platform_driver(bcm6328_pinctrl_driver);