xref: /linux/drivers/pinctrl/bcm/pinctrl-bcm6328.c (revision 0e3db16300fbae5e47ce6c298bf63a7862e5d576)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Driver for BCM6328 GPIO unit (pinctrl + GPIO)
4  *
5  * Copyright (C) 2021 Álvaro Fernández Rojas <noltari@gmail.com>
6  * Copyright (C) 2016 Jonas Gorski <jonas.gorski@gmail.com>
7  */
8 
9 #include <linux/bits.h>
10 #include <linux/gpio/driver.h>
11 #include <linux/kernel.h>
12 #include <linux/of.h>
13 #include <linux/pinctrl/pinmux.h>
14 #include <linux/platform_device.h>
15 #include <linux/regmap.h>
16 
17 #include "../pinctrl-utils.h"
18 
19 #include "pinctrl-bcm63xx.h"
20 
21 #define BCM6328_NUM_GPIOS	32
22 
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)
28 
29 struct bcm6328_pingroup {
30 	const char *name;
31 	const unsigned * const pins;
32 	const unsigned num_pins;
33 };
34 
35 struct bcm6328_function {
36 	const char *name;
37 	const char * const *groups;
38 	const unsigned num_groups;
39 
40 	unsigned mode_val:1;
41 	unsigned mux_val:2;
42 };
43 
44 static const unsigned int bcm6328_mux[] = {
45 	BCM6328_MUX_LO_REG,
46 	BCM6328_MUX_HI_REG,
47 	BCM6328_MUX_OTHER_REG
48 };
49 
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"),
83 
84 	/*
85 	 * No idea where they really are; so let's put them according
86 	 * to their mux offsets.
87 	 */
88 	PINCTRL_PIN(36, "hsspi_cs1"),
89 	PINCTRL_PIN(38, "usb_p2"),
90 };
91 
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 };
124 
125 static unsigned hsspi_cs1_pins[] = { 36 };
126 static unsigned usb_port1_pins[] = { 38 };
127 
128 static struct pingroup bcm6328_groups[] = {
129 	BCM_PIN_GROUP(gpio0),
130 	BCM_PIN_GROUP(gpio1),
131 	BCM_PIN_GROUP(gpio2),
132 	BCM_PIN_GROUP(gpio3),
133 	BCM_PIN_GROUP(gpio4),
134 	BCM_PIN_GROUP(gpio5),
135 	BCM_PIN_GROUP(gpio6),
136 	BCM_PIN_GROUP(gpio7),
137 	BCM_PIN_GROUP(gpio8),
138 	BCM_PIN_GROUP(gpio9),
139 	BCM_PIN_GROUP(gpio10),
140 	BCM_PIN_GROUP(gpio11),
141 	BCM_PIN_GROUP(gpio12),
142 	BCM_PIN_GROUP(gpio13),
143 	BCM_PIN_GROUP(gpio14),
144 	BCM_PIN_GROUP(gpio15),
145 	BCM_PIN_GROUP(gpio16),
146 	BCM_PIN_GROUP(gpio17),
147 	BCM_PIN_GROUP(gpio18),
148 	BCM_PIN_GROUP(gpio19),
149 	BCM_PIN_GROUP(gpio20),
150 	BCM_PIN_GROUP(gpio21),
151 	BCM_PIN_GROUP(gpio22),
152 	BCM_PIN_GROUP(gpio23),
153 	BCM_PIN_GROUP(gpio24),
154 	BCM_PIN_GROUP(gpio25),
155 	BCM_PIN_GROUP(gpio26),
156 	BCM_PIN_GROUP(gpio27),
157 	BCM_PIN_GROUP(gpio28),
158 	BCM_PIN_GROUP(gpio29),
159 	BCM_PIN_GROUP(gpio30),
160 	BCM_PIN_GROUP(gpio31),
161 
162 	BCM_PIN_GROUP(hsspi_cs1),
163 	BCM_PIN_GROUP(usb_port1),
164 };
165 
166 /* GPIO_MODE */
167 static const char * const led_groups[] = {
168 	"gpio0",
169 	"gpio1",
170 	"gpio2",
171 	"gpio3",
172 	"gpio4",
173 	"gpio5",
174 	"gpio6",
175 	"gpio7",
176 	"gpio8",
177 	"gpio9",
178 	"gpio10",
179 	"gpio11",
180 	"gpio12",
181 	"gpio13",
182 	"gpio14",
183 	"gpio15",
184 	"gpio16",
185 	"gpio17",
186 	"gpio18",
187 	"gpio19",
188 	"gpio20",
189 	"gpio21",
190 	"gpio22",
191 	"gpio23",
192 };
193 
194 /* PINMUX_SEL */
195 static const char * const serial_led_data_groups[] = {
196 	"gpio6",
197 };
198 
199 static const char * const serial_led_clk_groups[] = {
200 	"gpio7",
201 };
202 
203 static const char * const inet_act_led_groups[] = {
204 	"gpio11",
205 };
206 
207 static const char * const pcie_clkreq_groups[] = {
208 	"gpio16",
209 };
210 
211 static const char * const ephy0_act_led_groups[] = {
212 	"gpio25",
213 };
214 
215 static const char * const ephy1_act_led_groups[] = {
216 	"gpio26",
217 };
218 
219 static const char * const ephy2_act_led_groups[] = {
220 	"gpio27",
221 };
222 
223 static const char * const ephy3_act_led_groups[] = {
224 	"gpio28",
225 };
226 
227 static const char * const hsspi_cs1_groups[] = {
228 	"hsspi_cs1"
229 };
230 
231 static const char * const usb_host_port_groups[] = {
232 	"usb_port1",
233 };
234 
235 static const char * const usb_device_port_groups[] = {
236 	"usb_port1",
237 };
238 
239 #define BCM6328_MODE_FUN(n)				\
240 	{						\
241 		.name = #n,				\
242 		.groups = n##_groups,			\
243 		.num_groups = ARRAY_SIZE(n##_groups),	\
244 		.mode_val = 1,				\
245 	}
246 
247 #define BCM6328_MUX_FUN(n, mux)				\
248 	{						\
249 		.name = #n,				\
250 		.groups = n##_groups,			\
251 		.num_groups = ARRAY_SIZE(n##_groups),	\
252 		.mux_val = mux,				\
253 	}
254 
255 static const struct bcm6328_function bcm6328_funcs[] = {
256 	BCM6328_MODE_FUN(led),
257 	BCM6328_MUX_FUN(serial_led_data, 2),
258 	BCM6328_MUX_FUN(serial_led_clk, 2),
259 	BCM6328_MUX_FUN(inet_act_led, 1),
260 	BCM6328_MUX_FUN(pcie_clkreq, 2),
261 	BCM6328_MUX_FUN(ephy0_act_led, 1),
262 	BCM6328_MUX_FUN(ephy1_act_led, 1),
263 	BCM6328_MUX_FUN(ephy2_act_led, 1),
264 	BCM6328_MUX_FUN(ephy3_act_led, 1),
265 	BCM6328_MUX_FUN(hsspi_cs1, 2),
266 	BCM6328_MUX_FUN(usb_host_port, 1),
267 	BCM6328_MUX_FUN(usb_device_port, 2),
268 };
269 
270 static inline unsigned int bcm6328_mux_off(unsigned int pin)
271 {
272 	return bcm6328_mux[pin / 16];
273 }
274 
275 static int bcm6328_pinctrl_get_group_count(struct pinctrl_dev *pctldev)
276 {
277 	return ARRAY_SIZE(bcm6328_groups);
278 }
279 
280 static const char *bcm6328_pinctrl_get_group_name(struct pinctrl_dev *pctldev,
281 						  unsigned group)
282 {
283 	return bcm6328_groups[group].name;
284 }
285 
286 static int bcm6328_pinctrl_get_group_pins(struct pinctrl_dev *pctldev,
287 					  unsigned group, const unsigned **pins,
288 					  unsigned *npins)
289 {
290 	*pins = bcm6328_groups[group].pins;
291 	*npins = bcm6328_groups[group].npins;
292 
293 	return 0;
294 }
295 
296 static int bcm6328_pinctrl_get_func_count(struct pinctrl_dev *pctldev)
297 {
298 	return ARRAY_SIZE(bcm6328_funcs);
299 }
300 
301 static const char *bcm6328_pinctrl_get_func_name(struct pinctrl_dev *pctldev,
302 						 unsigned selector)
303 {
304 	return bcm6328_funcs[selector].name;
305 }
306 
307 static int bcm6328_pinctrl_get_groups(struct pinctrl_dev *pctldev,
308 				      unsigned selector,
309 				      const char * const **groups,
310 				      unsigned * const num_groups)
311 {
312 	*groups = bcm6328_funcs[selector].groups;
313 	*num_groups = bcm6328_funcs[selector].num_groups;
314 
315 	return 0;
316 }
317 
318 static void bcm6328_rmw_mux(struct bcm63xx_pinctrl *pc, unsigned pin,
319 			    unsigned int mode, unsigned int mux)
320 {
321 	if (pin < BCM6328_NUM_GPIOS)
322 		regmap_update_bits(pc->regs, BCM6328_MODE_REG, BIT(pin),
323 				   mode ? BIT(pin) : 0);
324 
325 	regmap_update_bits(pc->regs, bcm6328_mux_off(pin),
326 			   BCM6328_MUX_MASK << ((pin % 16) * 2),
327 			   mux << ((pin % 16) * 2));
328 }
329 
330 static int bcm6328_pinctrl_set_mux(struct pinctrl_dev *pctldev,
331 				   unsigned selector, unsigned group)
332 {
333 	struct bcm63xx_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
334 	const struct pingroup *pg = &bcm6328_groups[group];
335 	const struct bcm6328_function *f = &bcm6328_funcs[selector];
336 
337 	bcm6328_rmw_mux(pc, pg->pins[0], f->mode_val, f->mux_val);
338 
339 	return 0;
340 }
341 
342 static int bcm6328_gpio_request_enable(struct pinctrl_dev *pctldev,
343 				       struct pinctrl_gpio_range *range,
344 				       unsigned offset)
345 {
346 	struct bcm63xx_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
347 
348 	/* disable all functions using this pin */
349 	bcm6328_rmw_mux(pc, offset, 0, 0);
350 
351 	return 0;
352 }
353 
354 static const struct pinctrl_ops bcm6328_pctl_ops = {
355 	.dt_free_map = pinctrl_utils_free_map,
356 	.dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
357 	.get_group_name = bcm6328_pinctrl_get_group_name,
358 	.get_group_pins = bcm6328_pinctrl_get_group_pins,
359 	.get_groups_count = bcm6328_pinctrl_get_group_count,
360 };
361 
362 static const struct pinmux_ops bcm6328_pmx_ops = {
363 	.get_function_groups = bcm6328_pinctrl_get_groups,
364 	.get_function_name = bcm6328_pinctrl_get_func_name,
365 	.get_functions_count = bcm6328_pinctrl_get_func_count,
366 	.gpio_request_enable = bcm6328_gpio_request_enable,
367 	.set_mux = bcm6328_pinctrl_set_mux,
368 	.strict = true,
369 };
370 
371 static const struct bcm63xx_pinctrl_soc bcm6328_soc = {
372 	.ngpios = BCM6328_NUM_GPIOS,
373 	.npins = ARRAY_SIZE(bcm6328_pins),
374 	.pctl_ops = &bcm6328_pctl_ops,
375 	.pins = bcm6328_pins,
376 	.pmx_ops = &bcm6328_pmx_ops,
377 };
378 
379 static int bcm6328_pinctrl_probe(struct platform_device *pdev)
380 {
381 	return bcm63xx_pinctrl_probe(pdev, &bcm6328_soc, NULL);
382 }
383 
384 static const struct of_device_id bcm6328_pinctrl_match[] = {
385 	{ .compatible = "brcm,bcm6328-pinctrl", },
386 	{ /* sentinel */ }
387 };
388 
389 static struct platform_driver bcm6328_pinctrl_driver = {
390 	.probe = bcm6328_pinctrl_probe,
391 	.driver = {
392 		.name = "bcm6328-pinctrl",
393 		.of_match_table = bcm6328_pinctrl_match,
394 	},
395 };
396 
397 builtin_platform_driver(bcm6328_pinctrl_driver);
398