1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2018 Rafał Miłecki <rafal@milecki.pl>
8 #include <linux/module.h>
10 #include <linux/of_device.h>
11 #include <linux/pinctrl/pinconf-generic.h>
12 #include <linux/pinctrl/pinctrl.h>
13 #include <linux/pinctrl/pinmux.h>
14 #include <linux/platform_device.h>
15 #include <linux/slab.h>
18 #include "../pinmux.h"
20 #define FLAG_BCM4708 BIT(1)
21 #define FLAG_BCM4709 BIT(2)
22 #define FLAG_BCM53012 BIT(3)
26 unsigned int chipset_flag;
27 struct pinctrl_dev *pctldev;
30 struct pinctrl_desc pctldesc;
37 static const struct pinctrl_pin_desc ns_pinctrl_pins[] = {
38 { 0, "spi_clk", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
39 { 1, "spi_ss", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
40 { 2, "spi_mosi", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
41 { 3, "spi_miso", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
42 { 4, "i2c_scl", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
43 { 5, "i2c_sda", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
44 { 6, "mdc", (void *)(FLAG_BCM4709 | FLAG_BCM53012) },
45 { 7, "mdio", (void *)(FLAG_BCM4709 | FLAG_BCM53012) },
46 { 8, "pwm0", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
47 { 9, "pwm1", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
48 { 10, "pwm2", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
49 { 11, "pwm3", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
50 { 12, "uart1_rx", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
51 { 13, "uart1_tx", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
52 { 14, "uart1_cts", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
53 { 15, "uart1_rts", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
54 { 16, "uart2_rx", (void *)(FLAG_BCM4709 | FLAG_BCM53012) },
55 { 17, "uart2_tx", (void *)(FLAG_BCM4709 | FLAG_BCM53012) },
56 /* TODO { ??, "xtal_out", (void *)(FLAG_BCM4709) }, */
57 { 22, "sdio_pwr", (void *)(FLAG_BCM4709 | FLAG_BCM53012) },
58 { 23, "sdio_en_1p8v", (void *)(FLAG_BCM4709 | FLAG_BCM53012) },
65 struct ns_pinctrl_group {
68 const unsigned int num_pins;
69 unsigned int chipsets;
72 static unsigned int spi_pins[] = { 0, 1, 2, 3 };
73 static unsigned int i2c_pins[] = { 4, 5 };
74 static unsigned int mdio_pins[] = { 6, 7 };
75 static unsigned int pwm0_pins[] = { 8 };
76 static unsigned int pwm1_pins[] = { 9 };
77 static unsigned int pwm2_pins[] = { 10 };
78 static unsigned int pwm3_pins[] = { 11 };
79 static unsigned int uart1_pins[] = { 12, 13, 14, 15 };
80 static unsigned int uart2_pins[] = { 16, 17 };
81 static unsigned int sdio_pwr_pins[] = { 22 };
82 static unsigned int sdio_1p8v_pins[] = { 23 };
84 #define NS_GROUP(_name, _pins, _chipsets) \
88 .num_pins = ARRAY_SIZE(_pins), \
89 .chipsets = _chipsets, \
92 static const struct ns_pinctrl_group ns_pinctrl_groups[] = {
93 NS_GROUP("spi_grp", spi_pins, FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012),
94 NS_GROUP("i2c_grp", i2c_pins, FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012),
95 NS_GROUP("mdio_grp", mdio_pins, FLAG_BCM4709 | FLAG_BCM53012),
96 NS_GROUP("pwm0_grp", pwm0_pins, FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012),
97 NS_GROUP("pwm1_grp", pwm1_pins, FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012),
98 NS_GROUP("pwm2_grp", pwm2_pins, FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012),
99 NS_GROUP("pwm3_grp", pwm3_pins, FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012),
100 NS_GROUP("uart1_grp", uart1_pins, FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012),
101 NS_GROUP("uart2_grp", uart2_pins, FLAG_BCM4709 | FLAG_BCM53012),
102 NS_GROUP("sdio_pwr_grp", sdio_pwr_pins, FLAG_BCM4709 | FLAG_BCM53012),
103 NS_GROUP("sdio_1p8v_grp", sdio_1p8v_pins, FLAG_BCM4709 | FLAG_BCM53012),
110 struct ns_pinctrl_function {
112 const char * const *groups;
113 const unsigned int num_groups;
114 unsigned int chipsets;
117 static const char * const spi_groups[] = { "spi_grp" };
118 static const char * const i2c_groups[] = { "i2c_grp" };
119 static const char * const mdio_groups[] = { "mdio_grp" };
120 static const char * const pwm_groups[] = { "pwm0_grp", "pwm1_grp", "pwm2_grp",
122 static const char * const uart1_groups[] = { "uart1_grp" };
123 static const char * const uart2_groups[] = { "uart2_grp" };
124 static const char * const sdio_groups[] = { "sdio_pwr_grp", "sdio_1p8v_grp" };
126 #define NS_FUNCTION(_name, _groups, _chipsets) \
130 .num_groups = ARRAY_SIZE(_groups), \
131 .chipsets = _chipsets, \
134 static const struct ns_pinctrl_function ns_pinctrl_functions[] = {
135 NS_FUNCTION("spi", spi_groups, FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012),
136 NS_FUNCTION("i2c", i2c_groups, FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012),
137 NS_FUNCTION("mdio", mdio_groups, FLAG_BCM4709 | FLAG_BCM53012),
138 NS_FUNCTION("pwm", pwm_groups, FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012),
139 NS_FUNCTION("uart1", uart1_groups, FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012),
140 NS_FUNCTION("uart2", uart2_groups, FLAG_BCM4709 | FLAG_BCM53012),
141 NS_FUNCTION("sdio", sdio_groups, FLAG_BCM4709 | FLAG_BCM53012),
148 static const struct pinctrl_ops ns_pinctrl_ops = {
149 .get_groups_count = pinctrl_generic_get_group_count,
150 .get_group_name = pinctrl_generic_get_group_name,
151 .get_group_pins = pinctrl_generic_get_group_pins,
152 .dt_node_to_map = pinconf_generic_dt_node_to_map_group,
153 .dt_free_map = pinconf_generic_dt_free_map,
160 static int ns_pinctrl_set_mux(struct pinctrl_dev *pctrl_dev,
161 unsigned int func_select,
162 unsigned int group_selector)
164 struct ns_pinctrl *ns_pinctrl = pinctrl_dev_get_drvdata(pctrl_dev);
165 struct group_desc *group;
170 group = pinctrl_generic_get_group(pctrl_dev, group_selector);
174 for (i = 0; i < group->num_pins; i++)
175 unset |= BIT(group->pins[i]);
177 tmp = readl(ns_pinctrl->base);
179 writel(tmp, ns_pinctrl->base);
184 static const struct pinmux_ops ns_pinctrl_pmxops = {
185 .get_functions_count = pinmux_generic_get_function_count,
186 .get_function_name = pinmux_generic_get_function_name,
187 .get_function_groups = pinmux_generic_get_function_groups,
188 .set_mux = ns_pinctrl_set_mux,
195 static struct pinctrl_desc ns_pinctrl_desc = {
196 .name = "pinctrl-ns",
197 .pctlops = &ns_pinctrl_ops,
198 .pmxops = &ns_pinctrl_pmxops,
201 static const struct of_device_id ns_pinctrl_of_match_table[] = {
202 { .compatible = "brcm,bcm4708-pinmux", .data = (void *)FLAG_BCM4708, },
203 { .compatible = "brcm,bcm4709-pinmux", .data = (void *)FLAG_BCM4709, },
204 { .compatible = "brcm,bcm53012-pinmux", .data = (void *)FLAG_BCM53012, },
208 static int ns_pinctrl_probe(struct platform_device *pdev)
210 struct device *dev = &pdev->dev;
211 const struct of_device_id *of_id;
212 struct ns_pinctrl *ns_pinctrl;
213 struct pinctrl_desc *pctldesc;
214 struct pinctrl_pin_desc *pin;
215 struct resource *res;
218 ns_pinctrl = devm_kzalloc(dev, sizeof(*ns_pinctrl), GFP_KERNEL);
221 pctldesc = &ns_pinctrl->pctldesc;
222 platform_set_drvdata(pdev, ns_pinctrl);
224 /* Set basic properties */
226 ns_pinctrl->dev = dev;
228 of_id = of_match_device(ns_pinctrl_of_match_table, dev);
231 ns_pinctrl->chipset_flag = (uintptr_t)of_id->data;
233 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
235 ns_pinctrl->base = devm_ioremap_resource(dev, res);
236 if (IS_ERR(ns_pinctrl->base))
237 return PTR_ERR(ns_pinctrl->base);
239 memcpy(pctldesc, &ns_pinctrl_desc, sizeof(*pctldesc));
241 /* Set pinctrl properties */
243 pctldesc->pins = devm_kcalloc(dev, ARRAY_SIZE(ns_pinctrl_pins),
244 sizeof(struct pinctrl_pin_desc),
248 for (i = 0, pin = (struct pinctrl_pin_desc *)&pctldesc->pins[0];
249 i < ARRAY_SIZE(ns_pinctrl_pins); i++) {
250 const struct pinctrl_pin_desc *src = &ns_pinctrl_pins[i];
251 unsigned int chipsets = (uintptr_t)src->drv_data;
253 if (chipsets & ns_pinctrl->chipset_flag) {
254 memcpy(pin++, src, sizeof(*src));
261 ns_pinctrl->pctldev = devm_pinctrl_register(dev, pctldesc, ns_pinctrl);
262 if (IS_ERR(ns_pinctrl->pctldev)) {
263 dev_err(dev, "Failed to register pinctrl\n");
264 return PTR_ERR(ns_pinctrl->pctldev);
267 for (i = 0; i < ARRAY_SIZE(ns_pinctrl_groups); i++) {
268 const struct ns_pinctrl_group *group = &ns_pinctrl_groups[i];
270 if (!(group->chipsets & ns_pinctrl->chipset_flag))
273 pinctrl_generic_add_group(ns_pinctrl->pctldev, group->name,
274 group->pins, group->num_pins, NULL);
277 for (i = 0; i < ARRAY_SIZE(ns_pinctrl_functions); i++) {
278 const struct ns_pinctrl_function *function = &ns_pinctrl_functions[i];
280 if (!(function->chipsets & ns_pinctrl->chipset_flag))
283 pinmux_generic_add_function(ns_pinctrl->pctldev, function->name,
285 function->num_groups, NULL);
291 static struct platform_driver ns_pinctrl_driver = {
292 .probe = ns_pinctrl_probe,
295 .of_match_table = ns_pinctrl_of_match_table,
299 module_platform_driver(ns_pinctrl_driver);
301 MODULE_AUTHOR("Rafał Miłecki");
302 MODULE_LICENSE("GPL v2");
303 MODULE_DEVICE_TABLE(of, ns_pinctrl_of_match_table);