WIP FPC-III support
[linux/fpc-iii.git] / drivers / pinctrl / bcm / pinctrl-ns.c
blobe79690bd8b85f25a9e8a9665dd4a1cc08bbb8ca8
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2018 Rafał Miłecki <rafal@milecki.pl>
4 */
6 #include <linux/err.h>
7 #include <linux/io.h>
8 #include <linux/mfd/syscon.h>
9 #include <linux/module.h>
10 #include <linux/of.h>
11 #include <linux/of_device.h>
12 #include <linux/pinctrl/pinconf-generic.h>
13 #include <linux/pinctrl/pinctrl.h>
14 #include <linux/pinctrl/pinmux.h>
15 #include <linux/platform_device.h>
16 #include <linux/regmap.h>
17 #include <linux/slab.h>
19 #define FLAG_BCM4708 BIT(1)
20 #define FLAG_BCM4709 BIT(2)
21 #define FLAG_BCM53012 BIT(3)
23 struct ns_pinctrl {
24 struct device *dev;
25 unsigned int chipset_flag;
26 struct pinctrl_dev *pctldev;
27 struct regmap *regmap;
28 u32 offset;
30 struct pinctrl_desc pctldesc;
31 struct ns_pinctrl_group *groups;
32 unsigned int num_groups;
33 struct ns_pinctrl_function *functions;
34 unsigned int num_functions;
38 * Pins
41 static const struct pinctrl_pin_desc ns_pinctrl_pins[] = {
42 { 0, "spi_clk", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
43 { 1, "spi_ss", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
44 { 2, "spi_mosi", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
45 { 3, "spi_miso", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
46 { 4, "i2c_scl", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
47 { 5, "i2c_sda", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
48 { 6, "mdc", (void *)(FLAG_BCM4709 | FLAG_BCM53012) },
49 { 7, "mdio", (void *)(FLAG_BCM4709 | FLAG_BCM53012) },
50 { 8, "pwm0", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
51 { 9, "pwm1", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
52 { 10, "pwm2", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
53 { 11, "pwm3", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
54 { 12, "uart1_rx", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
55 { 13, "uart1_tx", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
56 { 14, "uart1_cts", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
57 { 15, "uart1_rts", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
58 { 16, "uart2_rx", (void *)(FLAG_BCM4709 | FLAG_BCM53012) },
59 { 17, "uart2_tx", (void *)(FLAG_BCM4709 | FLAG_BCM53012) },
60 /* TODO { ??, "xtal_out", (void *)(FLAG_BCM4709) }, */
61 { 22, "sdio_pwr", (void *)(FLAG_BCM4709 | FLAG_BCM53012) },
62 { 23, "sdio_en_1p8v", (void *)(FLAG_BCM4709 | FLAG_BCM53012) },
66 * Groups
69 struct ns_pinctrl_group {
70 const char *name;
71 const unsigned int *pins;
72 const unsigned int num_pins;
73 unsigned int chipsets;
76 static const unsigned int spi_pins[] = { 0, 1, 2, 3 };
77 static const unsigned int i2c_pins[] = { 4, 5 };
78 static const unsigned int mdio_pins[] = { 6, 7 };
79 static const unsigned int pwm0_pins[] = { 8 };
80 static const unsigned int pwm1_pins[] = { 9 };
81 static const unsigned int pwm2_pins[] = { 10 };
82 static const unsigned int pwm3_pins[] = { 11 };
83 static const unsigned int uart1_pins[] = { 12, 13, 14, 15 };
84 static const unsigned int uart2_pins[] = { 16, 17 };
85 static const unsigned int sdio_pwr_pins[] = { 22 };
86 static const unsigned int sdio_1p8v_pins[] = { 23 };
88 #define NS_GROUP(_name, _pins, _chipsets) \
89 { \
90 .name = _name, \
91 .pins = _pins, \
92 .num_pins = ARRAY_SIZE(_pins), \
93 .chipsets = _chipsets, \
96 static const struct ns_pinctrl_group ns_pinctrl_groups[] = {
97 NS_GROUP("spi_grp", spi_pins, FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012),
98 NS_GROUP("i2c_grp", i2c_pins, FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012),
99 NS_GROUP("mdio_grp", mdio_pins, FLAG_BCM4709 | FLAG_BCM53012),
100 NS_GROUP("pwm0_grp", pwm0_pins, FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012),
101 NS_GROUP("pwm1_grp", pwm1_pins, FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012),
102 NS_GROUP("pwm2_grp", pwm2_pins, FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012),
103 NS_GROUP("pwm3_grp", pwm3_pins, FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012),
104 NS_GROUP("uart1_grp", uart1_pins, FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012),
105 NS_GROUP("uart2_grp", uart2_pins, FLAG_BCM4709 | FLAG_BCM53012),
106 NS_GROUP("sdio_pwr_grp", sdio_pwr_pins, FLAG_BCM4709 | FLAG_BCM53012),
107 NS_GROUP("sdio_1p8v_grp", sdio_1p8v_pins, FLAG_BCM4709 | FLAG_BCM53012),
111 * Functions
114 struct ns_pinctrl_function {
115 const char *name;
116 const char * const *groups;
117 const unsigned int num_groups;
118 unsigned int chipsets;
121 static const char * const spi_groups[] = { "spi_grp" };
122 static const char * const i2c_groups[] = { "i2c_grp" };
123 static const char * const mdio_groups[] = { "mdio_grp" };
124 static const char * const pwm_groups[] = { "pwm0_grp", "pwm1_grp", "pwm2_grp",
125 "pwm3_grp" };
126 static const char * const uart1_groups[] = { "uart1_grp" };
127 static const char * const uart2_groups[] = { "uart2_grp" };
128 static const char * const sdio_groups[] = { "sdio_pwr_grp", "sdio_1p8v_grp" };
130 #define NS_FUNCTION(_name, _groups, _chipsets) \
132 .name = _name, \
133 .groups = _groups, \
134 .num_groups = ARRAY_SIZE(_groups), \
135 .chipsets = _chipsets, \
138 static const struct ns_pinctrl_function ns_pinctrl_functions[] = {
139 NS_FUNCTION("spi", spi_groups, FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012),
140 NS_FUNCTION("i2c", i2c_groups, FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012),
141 NS_FUNCTION("mdio", mdio_groups, FLAG_BCM4709 | FLAG_BCM53012),
142 NS_FUNCTION("pwm", pwm_groups, FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012),
143 NS_FUNCTION("uart1", uart1_groups, FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012),
144 NS_FUNCTION("uart2", uart2_groups, FLAG_BCM4709 | FLAG_BCM53012),
145 NS_FUNCTION("sdio", sdio_groups, FLAG_BCM4709 | FLAG_BCM53012),
149 * Groups code
152 static int ns_pinctrl_get_groups_count(struct pinctrl_dev *pctrl_dev)
154 struct ns_pinctrl *ns_pinctrl = pinctrl_dev_get_drvdata(pctrl_dev);
156 return ns_pinctrl->num_groups;
159 static const char *ns_pinctrl_get_group_name(struct pinctrl_dev *pctrl_dev,
160 unsigned int selector)
162 struct ns_pinctrl *ns_pinctrl = pinctrl_dev_get_drvdata(pctrl_dev);
164 return ns_pinctrl->groups[selector].name;
167 static int ns_pinctrl_get_group_pins(struct pinctrl_dev *pctrl_dev,
168 unsigned int selector,
169 const unsigned int **pins,
170 unsigned int *num_pins)
172 struct ns_pinctrl *ns_pinctrl = pinctrl_dev_get_drvdata(pctrl_dev);
174 *pins = ns_pinctrl->groups[selector].pins;
175 *num_pins = ns_pinctrl->groups[selector].num_pins;
177 return 0;
180 static const struct pinctrl_ops ns_pinctrl_ops = {
181 .get_groups_count = ns_pinctrl_get_groups_count,
182 .get_group_name = ns_pinctrl_get_group_name,
183 .get_group_pins = ns_pinctrl_get_group_pins,
184 .dt_node_to_map = pinconf_generic_dt_node_to_map_group,
185 .dt_free_map = pinconf_generic_dt_free_map,
189 * Functions code
192 static int ns_pinctrl_get_functions_count(struct pinctrl_dev *pctrl_dev)
194 struct ns_pinctrl *ns_pinctrl = pinctrl_dev_get_drvdata(pctrl_dev);
196 return ns_pinctrl->num_functions;
199 static const char *ns_pinctrl_get_function_name(struct pinctrl_dev *pctrl_dev,
200 unsigned int selector)
202 struct ns_pinctrl *ns_pinctrl = pinctrl_dev_get_drvdata(pctrl_dev);
204 return ns_pinctrl->functions[selector].name;
207 static int ns_pinctrl_get_function_groups(struct pinctrl_dev *pctrl_dev,
208 unsigned int selector,
209 const char * const **groups,
210 unsigned * const num_groups)
212 struct ns_pinctrl *ns_pinctrl = pinctrl_dev_get_drvdata(pctrl_dev);
214 *groups = ns_pinctrl->functions[selector].groups;
215 *num_groups = ns_pinctrl->functions[selector].num_groups;
217 return 0;
220 static int ns_pinctrl_set_mux(struct pinctrl_dev *pctrl_dev,
221 unsigned int func_select,
222 unsigned int grp_select)
224 struct ns_pinctrl *ns_pinctrl = pinctrl_dev_get_drvdata(pctrl_dev);
225 u32 unset = 0;
226 u32 tmp;
227 int i;
229 for (i = 0; i < ns_pinctrl->groups[grp_select].num_pins; i++) {
230 int pin_number = ns_pinctrl->groups[grp_select].pins[i];
232 unset |= BIT(pin_number);
235 regmap_read(ns_pinctrl->regmap, ns_pinctrl->offset, &tmp);
236 tmp &= ~unset;
237 regmap_write(ns_pinctrl->regmap, ns_pinctrl->offset, tmp);
239 return 0;
242 static const struct pinmux_ops ns_pinctrl_pmxops = {
243 .get_functions_count = ns_pinctrl_get_functions_count,
244 .get_function_name = ns_pinctrl_get_function_name,
245 .get_function_groups = ns_pinctrl_get_function_groups,
246 .set_mux = ns_pinctrl_set_mux,
250 * Controller code
253 static struct pinctrl_desc ns_pinctrl_desc = {
254 .name = "pinctrl-ns",
255 .pctlops = &ns_pinctrl_ops,
256 .pmxops = &ns_pinctrl_pmxops,
259 static const struct of_device_id ns_pinctrl_of_match_table[] = {
260 { .compatible = "brcm,bcm4708-pinmux", .data = (void *)FLAG_BCM4708, },
261 { .compatible = "brcm,bcm4709-pinmux", .data = (void *)FLAG_BCM4709, },
262 { .compatible = "brcm,bcm53012-pinmux", .data = (void *)FLAG_BCM53012, },
266 static int ns_pinctrl_probe(struct platform_device *pdev)
268 struct device *dev = &pdev->dev;
269 struct device_node *np = dev->of_node;
270 const struct of_device_id *of_id;
271 struct ns_pinctrl *ns_pinctrl;
272 struct pinctrl_desc *pctldesc;
273 struct pinctrl_pin_desc *pin;
274 struct ns_pinctrl_group *group;
275 struct ns_pinctrl_function *function;
276 int i;
278 ns_pinctrl = devm_kzalloc(dev, sizeof(*ns_pinctrl), GFP_KERNEL);
279 if (!ns_pinctrl)
280 return -ENOMEM;
281 pctldesc = &ns_pinctrl->pctldesc;
282 platform_set_drvdata(pdev, ns_pinctrl);
284 /* Set basic properties */
286 ns_pinctrl->dev = dev;
288 of_id = of_match_device(ns_pinctrl_of_match_table, dev);
289 if (!of_id)
290 return -EINVAL;
291 ns_pinctrl->chipset_flag = (uintptr_t)of_id->data;
293 ns_pinctrl->regmap = syscon_node_to_regmap(of_get_parent(np));
294 if (IS_ERR(ns_pinctrl->regmap)) {
295 int err = PTR_ERR(ns_pinctrl->regmap);
297 dev_err(dev, "Failed to map pinctrl regs: %d\n", err);
299 return err;
302 if (of_property_read_u32(np, "offset", &ns_pinctrl->offset)) {
303 dev_err(dev, "Failed to get register offset\n");
304 return -ENOENT;
307 memcpy(pctldesc, &ns_pinctrl_desc, sizeof(*pctldesc));
309 /* Set pinctrl properties */
311 pctldesc->pins = devm_kcalloc(dev, ARRAY_SIZE(ns_pinctrl_pins),
312 sizeof(struct pinctrl_pin_desc),
313 GFP_KERNEL);
314 if (!pctldesc->pins)
315 return -ENOMEM;
316 for (i = 0, pin = (struct pinctrl_pin_desc *)&pctldesc->pins[0];
317 i < ARRAY_SIZE(ns_pinctrl_pins); i++) {
318 const struct pinctrl_pin_desc *src = &ns_pinctrl_pins[i];
319 unsigned int chipsets = (uintptr_t)src->drv_data;
321 if (chipsets & ns_pinctrl->chipset_flag) {
322 memcpy(pin++, src, sizeof(*src));
323 pctldesc->npins++;
327 ns_pinctrl->groups = devm_kcalloc(dev, ARRAY_SIZE(ns_pinctrl_groups),
328 sizeof(struct ns_pinctrl_group),
329 GFP_KERNEL);
330 if (!ns_pinctrl->groups)
331 return -ENOMEM;
332 for (i = 0, group = &ns_pinctrl->groups[0];
333 i < ARRAY_SIZE(ns_pinctrl_groups); i++) {
334 const struct ns_pinctrl_group *src = &ns_pinctrl_groups[i];
336 if (src->chipsets & ns_pinctrl->chipset_flag) {
337 memcpy(group++, src, sizeof(*src));
338 ns_pinctrl->num_groups++;
342 ns_pinctrl->functions = devm_kcalloc(dev,
343 ARRAY_SIZE(ns_pinctrl_functions),
344 sizeof(struct ns_pinctrl_function),
345 GFP_KERNEL);
346 if (!ns_pinctrl->functions)
347 return -ENOMEM;
348 for (i = 0, function = &ns_pinctrl->functions[0];
349 i < ARRAY_SIZE(ns_pinctrl_functions); i++) {
350 const struct ns_pinctrl_function *src = &ns_pinctrl_functions[i];
352 if (src->chipsets & ns_pinctrl->chipset_flag) {
353 memcpy(function++, src, sizeof(*src));
354 ns_pinctrl->num_functions++;
358 /* Register */
360 ns_pinctrl->pctldev = devm_pinctrl_register(dev, pctldesc, ns_pinctrl);
361 if (IS_ERR(ns_pinctrl->pctldev)) {
362 dev_err(dev, "Failed to register pinctrl\n");
363 return PTR_ERR(ns_pinctrl->pctldev);
366 return 0;
369 static struct platform_driver ns_pinctrl_driver = {
370 .probe = ns_pinctrl_probe,
371 .driver = {
372 .name = "ns-pinmux",
373 .of_match_table = ns_pinctrl_of_match_table,
377 module_platform_driver(ns_pinctrl_driver);
379 MODULE_AUTHOR("Rafał Miłecki");
380 MODULE_LICENSE("GPL v2");
381 MODULE_DEVICE_TABLE(of, ns_pinctrl_of_match_table);