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>
17 #define FLAG_BCM4708 BIT(1)
18 #define FLAG_BCM4709 BIT(2)
19 #define FLAG_BCM53012 BIT(3)
23 unsigned int chipset_flag;
24 struct pinctrl_dev *pctldev;
27 struct pinctrl_desc pctldesc;
28 struct ns_pinctrl_group *groups;
29 unsigned int num_groups;
30 struct ns_pinctrl_function *functions;
31 unsigned int num_functions;
38 static const struct pinctrl_pin_desc ns_pinctrl_pins[] = {
39 { 0, "spi_clk", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
40 { 1, "spi_ss", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
41 { 2, "spi_mosi", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
42 { 3, "spi_miso", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
43 { 4, "i2c_scl", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
44 { 5, "i2c_sda", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
45 { 6, "mdc", (void *)(FLAG_BCM4709 | FLAG_BCM53012) },
46 { 7, "mdio", (void *)(FLAG_BCM4709 | FLAG_BCM53012) },
47 { 8, "pwm0", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
48 { 9, "pwm1", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
49 { 10, "pwm2", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
50 { 11, "pwm3", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
51 { 12, "uart1_rx", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
52 { 13, "uart1_tx", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
53 { 14, "uart1_cts", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
54 { 15, "uart1_rts", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
55 { 16, "uart2_rx", (void *)(FLAG_BCM4709 | FLAG_BCM53012) },
56 { 17, "uart2_tx", (void *)(FLAG_BCM4709 | FLAG_BCM53012) },
57 /* TODO { ??, "xtal_out", (void *)(FLAG_BCM4709) }, */
58 { 22, "sdio_pwr", (void *)(FLAG_BCM4709 | FLAG_BCM53012) },
59 { 23, "sdio_en_1p8v", (void *)(FLAG_BCM4709 | FLAG_BCM53012) },
66 struct ns_pinctrl_group {
68 const unsigned int *pins;
69 const unsigned int num_pins;
70 unsigned int chipsets;
73 static const unsigned int spi_pins[] = { 0, 1, 2, 3 };
74 static const unsigned int i2c_pins[] = { 4, 5 };
75 static const unsigned int mdio_pins[] = { 6, 7 };
76 static const unsigned int pwm0_pins[] = { 8 };
77 static const unsigned int pwm1_pins[] = { 9 };
78 static const unsigned int pwm2_pins[] = { 10 };
79 static const unsigned int pwm3_pins[] = { 11 };
80 static const unsigned int uart1_pins[] = { 12, 13, 14, 15 };
81 static const unsigned int uart2_pins[] = { 16, 17 };
82 static const unsigned int sdio_pwr_pins[] = { 22 };
83 static const unsigned int sdio_1p8v_pins[] = { 23 };
85 #define NS_GROUP(_name, _pins, _chipsets) \
89 .num_pins = ARRAY_SIZE(_pins), \
90 .chipsets = _chipsets, \
93 static const struct ns_pinctrl_group ns_pinctrl_groups[] = {
94 NS_GROUP("spi_grp", spi_pins, FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012),
95 NS_GROUP("i2c_grp", i2c_pins, FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012),
96 NS_GROUP("mdio_grp", mdio_pins, FLAG_BCM4709 | FLAG_BCM53012),
97 NS_GROUP("pwm0_grp", pwm0_pins, FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012),
98 NS_GROUP("pwm1_grp", pwm1_pins, FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012),
99 NS_GROUP("pwm2_grp", pwm2_pins, FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012),
100 NS_GROUP("pwm3_grp", pwm3_pins, FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012),
101 NS_GROUP("uart1_grp", uart1_pins, FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012),
102 NS_GROUP("uart2_grp", uart2_pins, FLAG_BCM4709 | FLAG_BCM53012),
103 NS_GROUP("sdio_pwr_grp", sdio_pwr_pins, FLAG_BCM4709 | FLAG_BCM53012),
104 NS_GROUP("sdio_1p8v_grp", sdio_1p8v_pins, FLAG_BCM4709 | FLAG_BCM53012),
111 struct ns_pinctrl_function {
113 const char * const *groups;
114 const unsigned int num_groups;
115 unsigned int chipsets;
118 static const char * const spi_groups[] = { "spi_grp" };
119 static const char * const i2c_groups[] = { "i2c_grp" };
120 static const char * const mdio_groups[] = { "mdio_grp" };
121 static const char * const pwm_groups[] = { "pwm0_grp", "pwm1_grp", "pwm2_grp",
123 static const char * const uart1_groups[] = { "uart1_grp" };
124 static const char * const uart2_groups[] = { "uart2_grp" };
125 static const char * const sdio_groups[] = { "sdio_pwr_grp", "sdio_1p8v_grp" };
127 #define NS_FUNCTION(_name, _groups, _chipsets) \
131 .num_groups = ARRAY_SIZE(_groups), \
132 .chipsets = _chipsets, \
135 static const struct ns_pinctrl_function ns_pinctrl_functions[] = {
136 NS_FUNCTION("spi", spi_groups, FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012),
137 NS_FUNCTION("i2c", i2c_groups, FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012),
138 NS_FUNCTION("mdio", mdio_groups, FLAG_BCM4709 | FLAG_BCM53012),
139 NS_FUNCTION("pwm", pwm_groups, FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012),
140 NS_FUNCTION("uart1", uart1_groups, FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012),
141 NS_FUNCTION("uart2", uart2_groups, FLAG_BCM4709 | FLAG_BCM53012),
142 NS_FUNCTION("sdio", sdio_groups, FLAG_BCM4709 | FLAG_BCM53012),
149 static int ns_pinctrl_get_groups_count(struct pinctrl_dev *pctrl_dev)
151 struct ns_pinctrl *ns_pinctrl = pinctrl_dev_get_drvdata(pctrl_dev);
153 return ns_pinctrl->num_groups;
156 static const char *ns_pinctrl_get_group_name(struct pinctrl_dev *pctrl_dev,
157 unsigned int selector)
159 struct ns_pinctrl *ns_pinctrl = pinctrl_dev_get_drvdata(pctrl_dev);
161 return ns_pinctrl->groups[selector].name;
164 static int ns_pinctrl_get_group_pins(struct pinctrl_dev *pctrl_dev,
165 unsigned int selector,
166 const unsigned int **pins,
167 unsigned int *num_pins)
169 struct ns_pinctrl *ns_pinctrl = pinctrl_dev_get_drvdata(pctrl_dev);
171 *pins = ns_pinctrl->groups[selector].pins;
172 *num_pins = ns_pinctrl->groups[selector].num_pins;
177 static const struct pinctrl_ops ns_pinctrl_ops = {
178 .get_groups_count = ns_pinctrl_get_groups_count,
179 .get_group_name = ns_pinctrl_get_group_name,
180 .get_group_pins = ns_pinctrl_get_group_pins,
181 .dt_node_to_map = pinconf_generic_dt_node_to_map_group,
182 .dt_free_map = pinconf_generic_dt_free_map,
189 static int ns_pinctrl_get_functions_count(struct pinctrl_dev *pctrl_dev)
191 struct ns_pinctrl *ns_pinctrl = pinctrl_dev_get_drvdata(pctrl_dev);
193 return ns_pinctrl->num_functions;
196 static const char *ns_pinctrl_get_function_name(struct pinctrl_dev *pctrl_dev,
197 unsigned int selector)
199 struct ns_pinctrl *ns_pinctrl = pinctrl_dev_get_drvdata(pctrl_dev);
201 return ns_pinctrl->functions[selector].name;
204 static int ns_pinctrl_get_function_groups(struct pinctrl_dev *pctrl_dev,
205 unsigned int selector,
206 const char * const **groups,
207 unsigned * const num_groups)
209 struct ns_pinctrl *ns_pinctrl = pinctrl_dev_get_drvdata(pctrl_dev);
211 *groups = ns_pinctrl->functions[selector].groups;
212 *num_groups = ns_pinctrl->functions[selector].num_groups;
217 static int ns_pinctrl_set_mux(struct pinctrl_dev *pctrl_dev,
218 unsigned int func_select,
219 unsigned int grp_select)
221 struct ns_pinctrl *ns_pinctrl = pinctrl_dev_get_drvdata(pctrl_dev);
226 for (i = 0; i < ns_pinctrl->groups[grp_select].num_pins; i++) {
227 int pin_number = ns_pinctrl->groups[grp_select].pins[i];
229 unset |= BIT(pin_number);
232 tmp = readl(ns_pinctrl->base);
234 writel(tmp, ns_pinctrl->base);
239 static const struct pinmux_ops ns_pinctrl_pmxops = {
240 .get_functions_count = ns_pinctrl_get_functions_count,
241 .get_function_name = ns_pinctrl_get_function_name,
242 .get_function_groups = ns_pinctrl_get_function_groups,
243 .set_mux = ns_pinctrl_set_mux,
250 static struct pinctrl_desc ns_pinctrl_desc = {
251 .name = "pinctrl-ns",
252 .pctlops = &ns_pinctrl_ops,
253 .pmxops = &ns_pinctrl_pmxops,
256 static const struct of_device_id ns_pinctrl_of_match_table[] = {
257 { .compatible = "brcm,bcm4708-pinmux", .data = (void *)FLAG_BCM4708, },
258 { .compatible = "brcm,bcm4709-pinmux", .data = (void *)FLAG_BCM4709, },
259 { .compatible = "brcm,bcm53012-pinmux", .data = (void *)FLAG_BCM53012, },
263 static int ns_pinctrl_probe(struct platform_device *pdev)
265 struct device *dev = &pdev->dev;
266 const struct of_device_id *of_id;
267 struct ns_pinctrl *ns_pinctrl;
268 struct pinctrl_desc *pctldesc;
269 struct pinctrl_pin_desc *pin;
270 struct ns_pinctrl_group *group;
271 struct ns_pinctrl_function *function;
272 struct resource *res;
275 ns_pinctrl = devm_kzalloc(dev, sizeof(*ns_pinctrl), GFP_KERNEL);
278 pctldesc = &ns_pinctrl->pctldesc;
279 platform_set_drvdata(pdev, ns_pinctrl);
281 /* Set basic properties */
283 ns_pinctrl->dev = dev;
285 of_id = of_match_device(ns_pinctrl_of_match_table, dev);
288 ns_pinctrl->chipset_flag = (uintptr_t)of_id->data;
290 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
292 ns_pinctrl->base = devm_ioremap_resource(dev, res);
293 if (IS_ERR(ns_pinctrl->base)) {
294 dev_err(dev, "Failed to map pinctrl regs\n");
295 return PTR_ERR(ns_pinctrl->base);
298 memcpy(pctldesc, &ns_pinctrl_desc, sizeof(*pctldesc));
300 /* Set pinctrl properties */
302 pctldesc->pins = devm_kcalloc(dev, ARRAY_SIZE(ns_pinctrl_pins),
303 sizeof(struct pinctrl_pin_desc),
307 for (i = 0, pin = (struct pinctrl_pin_desc *)&pctldesc->pins[0];
308 i < ARRAY_SIZE(ns_pinctrl_pins); i++) {
309 const struct pinctrl_pin_desc *src = &ns_pinctrl_pins[i];
310 unsigned int chipsets = (uintptr_t)src->drv_data;
312 if (chipsets & ns_pinctrl->chipset_flag) {
313 memcpy(pin++, src, sizeof(*src));
318 ns_pinctrl->groups = devm_kcalloc(dev, ARRAY_SIZE(ns_pinctrl_groups),
319 sizeof(struct ns_pinctrl_group),
321 if (!ns_pinctrl->groups)
323 for (i = 0, group = &ns_pinctrl->groups[0];
324 i < ARRAY_SIZE(ns_pinctrl_groups); i++) {
325 const struct ns_pinctrl_group *src = &ns_pinctrl_groups[i];
327 if (src->chipsets & ns_pinctrl->chipset_flag) {
328 memcpy(group++, src, sizeof(*src));
329 ns_pinctrl->num_groups++;
333 ns_pinctrl->functions = devm_kcalloc(dev,
334 ARRAY_SIZE(ns_pinctrl_functions),
335 sizeof(struct ns_pinctrl_function),
337 if (!ns_pinctrl->functions)
339 for (i = 0, function = &ns_pinctrl->functions[0];
340 i < ARRAY_SIZE(ns_pinctrl_functions); i++) {
341 const struct ns_pinctrl_function *src = &ns_pinctrl_functions[i];
343 if (src->chipsets & ns_pinctrl->chipset_flag) {
344 memcpy(function++, src, sizeof(*src));
345 ns_pinctrl->num_functions++;
351 ns_pinctrl->pctldev = devm_pinctrl_register(dev, pctldesc, ns_pinctrl);
352 if (IS_ERR(ns_pinctrl->pctldev)) {
353 dev_err(dev, "Failed to register pinctrl\n");
354 return PTR_ERR(ns_pinctrl->pctldev);
360 static struct platform_driver ns_pinctrl_driver = {
361 .probe = ns_pinctrl_probe,
364 .of_match_table = ns_pinctrl_of_match_table,
368 module_platform_driver(ns_pinctrl_driver);
370 MODULE_AUTHOR("Rafał Miłecki");
371 MODULE_LICENSE("GPL v2");
372 MODULE_DEVICE_TABLE(of, ns_pinctrl_of_match_table);