1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2021 Rafał Miłecki <rafal@milecki.pl> */
6 #include <linux/mod_devicetable.h>
7 #include <linux/module.h>
8 #include <linux/pinctrl/pinconf-generic.h>
9 #include <linux/pinctrl/pinctrl.h>
10 #include <linux/pinctrl/pinmux.h>
11 #include <linux/platform_device.h>
12 #include <linux/slab.h>
13 #include <linux/string_helpers.h>
16 #include "../pinmux.h"
18 #define BCM4908_NUM_PINS 86
20 #define BCM4908_TEST_PORT_BLOCK_EN_LSB 0x00
21 #define BCM4908_TEST_PORT_BLOCK_DATA_MSB 0x04
22 #define BCM4908_TEST_PORT_BLOCK_DATA_LSB 0x08
23 #define BCM4908_TEST_PORT_LSB_PINMUX_DATA_SHIFT 12
24 #define BCM4908_TEST_PORT_COMMAND 0x0c
25 #define BCM4908_TEST_PORT_CMD_LOAD_MUX_REG 0x00000021
27 struct bcm4908_pinctrl
{
31 struct pinctrl_dev
*pctldev
;
32 struct pinctrl_desc pctldesc
;
39 struct bcm4908_pinctrl_pin_setup
{
41 unsigned int function
;
44 static const struct bcm4908_pinctrl_pin_setup led_0_pins_a
[] = {
48 static const struct bcm4908_pinctrl_pin_setup led_1_pins_a
[] = {
52 static const struct bcm4908_pinctrl_pin_setup led_2_pins_a
[] = {
56 static const struct bcm4908_pinctrl_pin_setup led_3_pins_a
[] = {
60 static const struct bcm4908_pinctrl_pin_setup led_4_pins_a
[] = {
64 static const struct bcm4908_pinctrl_pin_setup led_5_pins_a
[] = {
68 static const struct bcm4908_pinctrl_pin_setup led_6_pins_a
[] = {
72 static const struct bcm4908_pinctrl_pin_setup led_7_pins_a
[] = {
76 static const struct bcm4908_pinctrl_pin_setup led_8_pins_a
[] = {
80 static const struct bcm4908_pinctrl_pin_setup led_9_pins_a
[] = {
84 static const struct bcm4908_pinctrl_pin_setup led_10_pins_a
[] = {
88 static const struct bcm4908_pinctrl_pin_setup led_11_pins_a
[] = {
92 static const struct bcm4908_pinctrl_pin_setup led_12_pins_a
[] = {
96 static const struct bcm4908_pinctrl_pin_setup led_13_pins_a
[] = {
100 static const struct bcm4908_pinctrl_pin_setup led_14_pins_a
[] = {
104 static const struct bcm4908_pinctrl_pin_setup led_15_pins_a
[] = {
108 static const struct bcm4908_pinctrl_pin_setup led_16_pins_a
[] = {
112 static const struct bcm4908_pinctrl_pin_setup led_17_pins_a
[] = {
116 static const struct bcm4908_pinctrl_pin_setup led_18_pins_a
[] = {
120 static const struct bcm4908_pinctrl_pin_setup led_19_pins_a
[] = {
124 static const struct bcm4908_pinctrl_pin_setup led_20_pins_a
[] = {
128 static const struct bcm4908_pinctrl_pin_setup led_21_pins_a
[] = {
132 static const struct bcm4908_pinctrl_pin_setup led_22_pins_a
[] = {
136 static const struct bcm4908_pinctrl_pin_setup led_23_pins_a
[] = {
140 static const struct bcm4908_pinctrl_pin_setup led_24_pins_a
[] = {
144 static const struct bcm4908_pinctrl_pin_setup led_25_pins_a
[] = {
148 static const struct bcm4908_pinctrl_pin_setup led_26_pins_a
[] = {
152 static const struct bcm4908_pinctrl_pin_setup led_27_pins_a
[] = {
156 static const struct bcm4908_pinctrl_pin_setup led_28_pins_a
[] = {
160 static const struct bcm4908_pinctrl_pin_setup led_29_pins_a
[] = {
164 static const struct bcm4908_pinctrl_pin_setup led_30_pins_a
[] = {
168 static const struct bcm4908_pinctrl_pin_setup led_31_pins_a
[] = {
172 static const struct bcm4908_pinctrl_pin_setup led_10_pins_b
[] = {
176 static const struct bcm4908_pinctrl_pin_setup led_11_pins_b
[] = {
180 static const struct bcm4908_pinctrl_pin_setup led_12_pins_b
[] = {
184 static const struct bcm4908_pinctrl_pin_setup led_13_pins_b
[] = {
188 static const struct bcm4908_pinctrl_pin_setup led_31_pins_b
[] = {
192 static const struct bcm4908_pinctrl_pin_setup hs_uart_pins
[] = {
199 static const struct bcm4908_pinctrl_pin_setup i2c_pins_a
[] = {
204 static const struct bcm4908_pinctrl_pin_setup i2c_pins_b
[] = {
209 static const struct bcm4908_pinctrl_pin_setup i2s_pins
[] = {
210 { 27, 0 }, /* MCLK */
211 { 28, 0 }, /* LRCK */
212 { 29, 0 }, /* SDATA */
213 { 30, 0 }, /* SCLK */
216 static const struct bcm4908_pinctrl_pin_setup nand_ctrl_pins
[] = {
226 static const struct bcm4908_pinctrl_pin_setup nand_data_pins
[] = {
237 static const struct bcm4908_pinctrl_pin_setup emmc_ctrl_pins
[] = {
242 static const struct bcm4908_pinctrl_pin_setup usb0_pwr_pins
[] = {
247 static const struct bcm4908_pinctrl_pin_setup usb1_pwr_pins
[] = {
252 struct bcm4908_pinctrl_grp
{
254 const struct bcm4908_pinctrl_pin_setup
*pins
;
255 const unsigned int num_pins
;
258 static const struct bcm4908_pinctrl_grp bcm4908_pinctrl_grps
[] = {
259 { "led_0_grp_a", led_0_pins_a
, ARRAY_SIZE(led_0_pins_a
) },
260 { "led_1_grp_a", led_1_pins_a
, ARRAY_SIZE(led_1_pins_a
) },
261 { "led_2_grp_a", led_2_pins_a
, ARRAY_SIZE(led_2_pins_a
) },
262 { "led_3_grp_a", led_3_pins_a
, ARRAY_SIZE(led_3_pins_a
) },
263 { "led_4_grp_a", led_4_pins_a
, ARRAY_SIZE(led_4_pins_a
) },
264 { "led_5_grp_a", led_5_pins_a
, ARRAY_SIZE(led_5_pins_a
) },
265 { "led_6_grp_a", led_6_pins_a
, ARRAY_SIZE(led_6_pins_a
) },
266 { "led_7_grp_a", led_7_pins_a
, ARRAY_SIZE(led_7_pins_a
) },
267 { "led_8_grp_a", led_8_pins_a
, ARRAY_SIZE(led_8_pins_a
) },
268 { "led_9_grp_a", led_9_pins_a
, ARRAY_SIZE(led_9_pins_a
) },
269 { "led_10_grp_a", led_10_pins_a
, ARRAY_SIZE(led_10_pins_a
) },
270 { "led_11_grp_a", led_11_pins_a
, ARRAY_SIZE(led_11_pins_a
) },
271 { "led_12_grp_a", led_12_pins_a
, ARRAY_SIZE(led_12_pins_a
) },
272 { "led_13_grp_a", led_13_pins_a
, ARRAY_SIZE(led_13_pins_a
) },
273 { "led_14_grp_a", led_14_pins_a
, ARRAY_SIZE(led_14_pins_a
) },
274 { "led_15_grp_a", led_15_pins_a
, ARRAY_SIZE(led_15_pins_a
) },
275 { "led_16_grp_a", led_16_pins_a
, ARRAY_SIZE(led_16_pins_a
) },
276 { "led_17_grp_a", led_17_pins_a
, ARRAY_SIZE(led_17_pins_a
) },
277 { "led_18_grp_a", led_18_pins_a
, ARRAY_SIZE(led_18_pins_a
) },
278 { "led_19_grp_a", led_19_pins_a
, ARRAY_SIZE(led_19_pins_a
) },
279 { "led_20_grp_a", led_20_pins_a
, ARRAY_SIZE(led_20_pins_a
) },
280 { "led_21_grp_a", led_21_pins_a
, ARRAY_SIZE(led_21_pins_a
) },
281 { "led_22_grp_a", led_22_pins_a
, ARRAY_SIZE(led_22_pins_a
) },
282 { "led_23_grp_a", led_23_pins_a
, ARRAY_SIZE(led_23_pins_a
) },
283 { "led_24_grp_a", led_24_pins_a
, ARRAY_SIZE(led_24_pins_a
) },
284 { "led_25_grp_a", led_25_pins_a
, ARRAY_SIZE(led_25_pins_a
) },
285 { "led_26_grp_a", led_26_pins_a
, ARRAY_SIZE(led_26_pins_a
) },
286 { "led_27_grp_a", led_27_pins_a
, ARRAY_SIZE(led_27_pins_a
) },
287 { "led_28_grp_a", led_28_pins_a
, ARRAY_SIZE(led_28_pins_a
) },
288 { "led_29_grp_a", led_29_pins_a
, ARRAY_SIZE(led_29_pins_a
) },
289 { "led_30_grp_a", led_30_pins_a
, ARRAY_SIZE(led_30_pins_a
) },
290 { "led_31_grp_a", led_31_pins_a
, ARRAY_SIZE(led_31_pins_a
) },
291 { "led_10_grp_b", led_10_pins_b
, ARRAY_SIZE(led_10_pins_b
) },
292 { "led_11_grp_b", led_11_pins_b
, ARRAY_SIZE(led_11_pins_b
) },
293 { "led_12_grp_b", led_12_pins_b
, ARRAY_SIZE(led_12_pins_b
) },
294 { "led_13_grp_b", led_13_pins_b
, ARRAY_SIZE(led_13_pins_b
) },
295 { "led_31_grp_b", led_31_pins_b
, ARRAY_SIZE(led_31_pins_b
) },
296 { "hs_uart_grp", hs_uart_pins
, ARRAY_SIZE(hs_uart_pins
) },
297 { "i2c_grp_a", i2c_pins_a
, ARRAY_SIZE(i2c_pins_a
) },
298 { "i2c_grp_b", i2c_pins_b
, ARRAY_SIZE(i2c_pins_b
) },
299 { "i2s_grp", i2s_pins
, ARRAY_SIZE(i2s_pins
) },
300 { "nand_ctrl_grp", nand_ctrl_pins
, ARRAY_SIZE(nand_ctrl_pins
) },
301 { "nand_data_grp", nand_data_pins
, ARRAY_SIZE(nand_data_pins
) },
302 { "emmc_ctrl_grp", emmc_ctrl_pins
, ARRAY_SIZE(emmc_ctrl_pins
) },
303 { "usb0_pwr_grp", usb0_pwr_pins
, ARRAY_SIZE(usb0_pwr_pins
) },
304 { "usb1_pwr_grp", usb1_pwr_pins
, ARRAY_SIZE(usb1_pwr_pins
) },
311 struct bcm4908_pinctrl_function
{
313 const char * const *groups
;
314 const unsigned int num_groups
;
317 static const char * const led_0_groups
[] = { "led_0_grp_a" };
318 static const char * const led_1_groups
[] = { "led_1_grp_a" };
319 static const char * const led_2_groups
[] = { "led_2_grp_a" };
320 static const char * const led_3_groups
[] = { "led_3_grp_a" };
321 static const char * const led_4_groups
[] = { "led_4_grp_a" };
322 static const char * const led_5_groups
[] = { "led_5_grp_a" };
323 static const char * const led_6_groups
[] = { "led_6_grp_a" };
324 static const char * const led_7_groups
[] = { "led_7_grp_a" };
325 static const char * const led_8_groups
[] = { "led_8_grp_a" };
326 static const char * const led_9_groups
[] = { "led_9_grp_a" };
327 static const char * const led_10_groups
[] = { "led_10_grp_a", "led_10_grp_b" };
328 static const char * const led_11_groups
[] = { "led_11_grp_a", "led_11_grp_b" };
329 static const char * const led_12_groups
[] = { "led_12_grp_a", "led_12_grp_b" };
330 static const char * const led_13_groups
[] = { "led_13_grp_a", "led_13_grp_b" };
331 static const char * const led_14_groups
[] = { "led_14_grp_a" };
332 static const char * const led_15_groups
[] = { "led_15_grp_a" };
333 static const char * const led_16_groups
[] = { "led_16_grp_a" };
334 static const char * const led_17_groups
[] = { "led_17_grp_a" };
335 static const char * const led_18_groups
[] = { "led_18_grp_a" };
336 static const char * const led_19_groups
[] = { "led_19_grp_a" };
337 static const char * const led_20_groups
[] = { "led_20_grp_a" };
338 static const char * const led_21_groups
[] = { "led_21_grp_a" };
339 static const char * const led_22_groups
[] = { "led_22_grp_a" };
340 static const char * const led_23_groups
[] = { "led_23_grp_a" };
341 static const char * const led_24_groups
[] = { "led_24_grp_a" };
342 static const char * const led_25_groups
[] = { "led_25_grp_a" };
343 static const char * const led_26_groups
[] = { "led_26_grp_a" };
344 static const char * const led_27_groups
[] = { "led_27_grp_a" };
345 static const char * const led_28_groups
[] = { "led_28_grp_a" };
346 static const char * const led_29_groups
[] = { "led_29_grp_a" };
347 static const char * const led_30_groups
[] = { "led_30_grp_a" };
348 static const char * const led_31_groups
[] = { "led_31_grp_a", "led_31_grp_b" };
349 static const char * const hs_uart_groups
[] = { "hs_uart_grp" };
350 static const char * const i2c_groups
[] = { "i2c_grp_a", "i2c_grp_b" };
351 static const char * const i2s_groups
[] = { "i2s_grp" };
352 static const char * const nand_ctrl_groups
[] = { "nand_ctrl_grp" };
353 static const char * const nand_data_groups
[] = { "nand_data_grp" };
354 static const char * const emmc_ctrl_groups
[] = { "emmc_ctrl_grp" };
355 static const char * const usb0_pwr_groups
[] = { "usb0_pwr_grp" };
356 static const char * const usb1_pwr_groups
[] = { "usb1_pwr_grp" };
358 static const struct bcm4908_pinctrl_function bcm4908_pinctrl_functions
[] = {
359 { "led_0", led_0_groups
, ARRAY_SIZE(led_0_groups
) },
360 { "led_1", led_1_groups
, ARRAY_SIZE(led_1_groups
) },
361 { "led_2", led_2_groups
, ARRAY_SIZE(led_2_groups
) },
362 { "led_3", led_3_groups
, ARRAY_SIZE(led_3_groups
) },
363 { "led_4", led_4_groups
, ARRAY_SIZE(led_4_groups
) },
364 { "led_5", led_5_groups
, ARRAY_SIZE(led_5_groups
) },
365 { "led_6", led_6_groups
, ARRAY_SIZE(led_6_groups
) },
366 { "led_7", led_7_groups
, ARRAY_SIZE(led_7_groups
) },
367 { "led_8", led_8_groups
, ARRAY_SIZE(led_8_groups
) },
368 { "led_9", led_9_groups
, ARRAY_SIZE(led_9_groups
) },
369 { "led_10", led_10_groups
, ARRAY_SIZE(led_10_groups
) },
370 { "led_11", led_11_groups
, ARRAY_SIZE(led_11_groups
) },
371 { "led_12", led_12_groups
, ARRAY_SIZE(led_12_groups
) },
372 { "led_13", led_13_groups
, ARRAY_SIZE(led_13_groups
) },
373 { "led_14", led_14_groups
, ARRAY_SIZE(led_14_groups
) },
374 { "led_15", led_15_groups
, ARRAY_SIZE(led_15_groups
) },
375 { "led_16", led_16_groups
, ARRAY_SIZE(led_16_groups
) },
376 { "led_17", led_17_groups
, ARRAY_SIZE(led_17_groups
) },
377 { "led_18", led_18_groups
, ARRAY_SIZE(led_18_groups
) },
378 { "led_19", led_19_groups
, ARRAY_SIZE(led_19_groups
) },
379 { "led_20", led_20_groups
, ARRAY_SIZE(led_20_groups
) },
380 { "led_21", led_21_groups
, ARRAY_SIZE(led_21_groups
) },
381 { "led_22", led_22_groups
, ARRAY_SIZE(led_22_groups
) },
382 { "led_23", led_23_groups
, ARRAY_SIZE(led_23_groups
) },
383 { "led_24", led_24_groups
, ARRAY_SIZE(led_24_groups
) },
384 { "led_25", led_25_groups
, ARRAY_SIZE(led_25_groups
) },
385 { "led_26", led_26_groups
, ARRAY_SIZE(led_26_groups
) },
386 { "led_27", led_27_groups
, ARRAY_SIZE(led_27_groups
) },
387 { "led_28", led_28_groups
, ARRAY_SIZE(led_28_groups
) },
388 { "led_29", led_29_groups
, ARRAY_SIZE(led_29_groups
) },
389 { "led_30", led_30_groups
, ARRAY_SIZE(led_30_groups
) },
390 { "led_31", led_31_groups
, ARRAY_SIZE(led_31_groups
) },
391 { "hs_uart", hs_uart_groups
, ARRAY_SIZE(hs_uart_groups
) },
392 { "i2c", i2c_groups
, ARRAY_SIZE(i2c_groups
) },
393 { "i2s", i2s_groups
, ARRAY_SIZE(i2s_groups
) },
394 { "nand_ctrl", nand_ctrl_groups
, ARRAY_SIZE(nand_ctrl_groups
) },
395 { "nand_data", nand_data_groups
, ARRAY_SIZE(nand_data_groups
) },
396 { "emmc_ctrl", emmc_ctrl_groups
, ARRAY_SIZE(emmc_ctrl_groups
) },
397 { "usb0_pwr", usb0_pwr_groups
, ARRAY_SIZE(usb0_pwr_groups
) },
398 { "usb1_pwr", usb1_pwr_groups
, ARRAY_SIZE(usb1_pwr_groups
) },
405 static const struct pinctrl_ops bcm4908_pinctrl_ops
= {
406 .get_groups_count
= pinctrl_generic_get_group_count
,
407 .get_group_name
= pinctrl_generic_get_group_name
,
408 .get_group_pins
= pinctrl_generic_get_group_pins
,
409 .dt_node_to_map
= pinconf_generic_dt_node_to_map_group
,
410 .dt_free_map
= pinconf_generic_dt_free_map
,
417 static int bcm4908_pinctrl_set_mux(struct pinctrl_dev
*pctrl_dev
,
418 unsigned int func_selector
,
419 unsigned int group_selector
)
421 struct bcm4908_pinctrl
*bcm4908_pinctrl
= pinctrl_dev_get_drvdata(pctrl_dev
);
422 const struct bcm4908_pinctrl_grp
*group
;
423 struct group_desc
*group_desc
;
426 group_desc
= pinctrl_generic_get_group(pctrl_dev
, group_selector
);
429 group
= group_desc
->data
;
431 mutex_lock(&bcm4908_pinctrl
->mutex
);
432 for (i
= 0; i
< group
->num_pins
; i
++) {
435 lsb
|= group
->pins
[i
].number
;
436 lsb
|= group
->pins
[i
].function
<< BCM4908_TEST_PORT_LSB_PINMUX_DATA_SHIFT
;
438 writel(0x0, bcm4908_pinctrl
->base
+ BCM4908_TEST_PORT_BLOCK_DATA_MSB
);
439 writel(lsb
, bcm4908_pinctrl
->base
+ BCM4908_TEST_PORT_BLOCK_DATA_LSB
);
440 writel(BCM4908_TEST_PORT_CMD_LOAD_MUX_REG
,
441 bcm4908_pinctrl
->base
+ BCM4908_TEST_PORT_COMMAND
);
443 mutex_unlock(&bcm4908_pinctrl
->mutex
);
448 static const struct pinmux_ops bcm4908_pinctrl_pmxops
= {
449 .get_functions_count
= pinmux_generic_get_function_count
,
450 .get_function_name
= pinmux_generic_get_function_name
,
451 .get_function_groups
= pinmux_generic_get_function_groups
,
452 .set_mux
= bcm4908_pinctrl_set_mux
,
459 static struct pinctrl_desc bcm4908_pinctrl_desc
= {
460 .name
= "bcm4908-pinctrl",
461 .pctlops
= &bcm4908_pinctrl_ops
,
462 .pmxops
= &bcm4908_pinctrl_pmxops
,
465 static const struct of_device_id bcm4908_pinctrl_of_match_table
[] = {
466 { .compatible
= "brcm,bcm4908-pinctrl", },
470 static int bcm4908_pinctrl_probe(struct platform_device
*pdev
)
472 struct device
*dev
= &pdev
->dev
;
473 struct bcm4908_pinctrl
*bcm4908_pinctrl
;
474 struct pinctrl_desc
*pctldesc
;
475 struct pinctrl_pin_desc
*pins
;
479 bcm4908_pinctrl
= devm_kzalloc(dev
, sizeof(*bcm4908_pinctrl
), GFP_KERNEL
);
480 if (!bcm4908_pinctrl
)
482 pctldesc
= &bcm4908_pinctrl
->pctldesc
;
483 platform_set_drvdata(pdev
, bcm4908_pinctrl
);
485 /* Set basic properties */
487 bcm4908_pinctrl
->dev
= dev
;
489 bcm4908_pinctrl
->base
= devm_platform_ioremap_resource(pdev
, 0);
490 if (IS_ERR(bcm4908_pinctrl
->base
))
491 return PTR_ERR(bcm4908_pinctrl
->base
);
493 mutex_init(&bcm4908_pinctrl
->mutex
);
495 memcpy(pctldesc
, &bcm4908_pinctrl_desc
, sizeof(*pctldesc
));
497 /* Set pinctrl properties */
499 pin_names
= devm_kasprintf_strarray(dev
, "pin", BCM4908_NUM_PINS
);
500 if (IS_ERR(pin_names
))
501 return PTR_ERR(pin_names
);
503 pins
= devm_kcalloc(dev
, BCM4908_NUM_PINS
, sizeof(*pins
), GFP_KERNEL
);
506 for (i
= 0; i
< BCM4908_NUM_PINS
; i
++) {
508 pins
[i
].name
= pin_names
[i
];
510 pctldesc
->pins
= pins
;
511 pctldesc
->npins
= BCM4908_NUM_PINS
;
515 bcm4908_pinctrl
->pctldev
= devm_pinctrl_register(dev
, pctldesc
, bcm4908_pinctrl
);
516 if (IS_ERR(bcm4908_pinctrl
->pctldev
))
517 return dev_err_probe(dev
, PTR_ERR(bcm4908_pinctrl
->pctldev
),
518 "Failed to register pinctrl\n");
522 for (i
= 0; i
< ARRAY_SIZE(bcm4908_pinctrl_grps
); i
++) {
523 const struct bcm4908_pinctrl_grp
*group
= &bcm4908_pinctrl_grps
[i
];
527 pins
= devm_kcalloc(dev
, group
->num_pins
, sizeof(*pins
), GFP_KERNEL
);
530 for (j
= 0; j
< group
->num_pins
; j
++)
531 pins
[j
] = group
->pins
[j
].number
;
533 pinctrl_generic_add_group(bcm4908_pinctrl
->pctldev
, group
->name
,
534 pins
, group
->num_pins
, (void *)group
);
539 for (i
= 0; i
< ARRAY_SIZE(bcm4908_pinctrl_functions
); i
++) {
540 const struct bcm4908_pinctrl_function
*function
= &bcm4908_pinctrl_functions
[i
];
542 pinmux_generic_add_function(bcm4908_pinctrl
->pctldev
,
545 function
->num_groups
, NULL
);
551 static struct platform_driver bcm4908_pinctrl_driver
= {
552 .probe
= bcm4908_pinctrl_probe
,
554 .name
= "bcm4908-pinctrl",
555 .of_match_table
= bcm4908_pinctrl_of_match_table
,
559 module_platform_driver(bcm4908_pinctrl_driver
);
561 MODULE_AUTHOR("Rafał Miłecki");
562 MODULE_DESCRIPTION("Broadcom BCM4908 pinmux driver");
563 MODULE_LICENSE("GPL v2");
564 MODULE_DEVICE_TABLE(of
, bcm4908_pinctrl_of_match_table
);