]> git.proxmox.com Git - mirror_ubuntu-kernels.git/blob - drivers/pinctrl/bcm/pinctrl-ns.c
Merge tag 'for-5.16/block-2021-10-29' of git://git.kernel.dk/linux-block
[mirror_ubuntu-kernels.git] / drivers / pinctrl / bcm / pinctrl-ns.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2018 Rafał Miłecki <rafal@milecki.pl>
4 */
5
6 #include <linux/err.h>
7 #include <linux/io.h>
8 #include <linux/module.h>
9 #include <linux/of.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>
16
17 #define FLAG_BCM4708 BIT(1)
18 #define FLAG_BCM4709 BIT(2)
19 #define FLAG_BCM53012 BIT(3)
20
21 struct ns_pinctrl {
22 struct device *dev;
23 unsigned int chipset_flag;
24 struct pinctrl_dev *pctldev;
25 void __iomem *base;
26
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;
32 };
33
34 /*
35 * Pins
36 */
37
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) },
60 };
61
62 /*
63 * Groups
64 */
65
66 struct ns_pinctrl_group {
67 const char *name;
68 const unsigned int *pins;
69 const unsigned int num_pins;
70 unsigned int chipsets;
71 };
72
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 };
84
85 #define NS_GROUP(_name, _pins, _chipsets) \
86 { \
87 .name = _name, \
88 .pins = _pins, \
89 .num_pins = ARRAY_SIZE(_pins), \
90 .chipsets = _chipsets, \
91 }
92
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),
105 };
106
107 /*
108 * Functions
109 */
110
111 struct ns_pinctrl_function {
112 const char *name;
113 const char * const *groups;
114 const unsigned int num_groups;
115 unsigned int chipsets;
116 };
117
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",
122 "pwm3_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" };
126
127 #define NS_FUNCTION(_name, _groups, _chipsets) \
128 { \
129 .name = _name, \
130 .groups = _groups, \
131 .num_groups = ARRAY_SIZE(_groups), \
132 .chipsets = _chipsets, \
133 }
134
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),
143 };
144
145 /*
146 * Groups code
147 */
148
149 static int ns_pinctrl_get_groups_count(struct pinctrl_dev *pctrl_dev)
150 {
151 struct ns_pinctrl *ns_pinctrl = pinctrl_dev_get_drvdata(pctrl_dev);
152
153 return ns_pinctrl->num_groups;
154 }
155
156 static const char *ns_pinctrl_get_group_name(struct pinctrl_dev *pctrl_dev,
157 unsigned int selector)
158 {
159 struct ns_pinctrl *ns_pinctrl = pinctrl_dev_get_drvdata(pctrl_dev);
160
161 return ns_pinctrl->groups[selector].name;
162 }
163
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)
168 {
169 struct ns_pinctrl *ns_pinctrl = pinctrl_dev_get_drvdata(pctrl_dev);
170
171 *pins = ns_pinctrl->groups[selector].pins;
172 *num_pins = ns_pinctrl->groups[selector].num_pins;
173
174 return 0;
175 }
176
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,
183 };
184
185 /*
186 * Functions code
187 */
188
189 static int ns_pinctrl_get_functions_count(struct pinctrl_dev *pctrl_dev)
190 {
191 struct ns_pinctrl *ns_pinctrl = pinctrl_dev_get_drvdata(pctrl_dev);
192
193 return ns_pinctrl->num_functions;
194 }
195
196 static const char *ns_pinctrl_get_function_name(struct pinctrl_dev *pctrl_dev,
197 unsigned int selector)
198 {
199 struct ns_pinctrl *ns_pinctrl = pinctrl_dev_get_drvdata(pctrl_dev);
200
201 return ns_pinctrl->functions[selector].name;
202 }
203
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)
208 {
209 struct ns_pinctrl *ns_pinctrl = pinctrl_dev_get_drvdata(pctrl_dev);
210
211 *groups = ns_pinctrl->functions[selector].groups;
212 *num_groups = ns_pinctrl->functions[selector].num_groups;
213
214 return 0;
215 }
216
217 static int ns_pinctrl_set_mux(struct pinctrl_dev *pctrl_dev,
218 unsigned int func_select,
219 unsigned int grp_select)
220 {
221 struct ns_pinctrl *ns_pinctrl = pinctrl_dev_get_drvdata(pctrl_dev);
222 u32 unset = 0;
223 u32 tmp;
224 int i;
225
226 for (i = 0; i < ns_pinctrl->groups[grp_select].num_pins; i++) {
227 int pin_number = ns_pinctrl->groups[grp_select].pins[i];
228
229 unset |= BIT(pin_number);
230 }
231
232 tmp = readl(ns_pinctrl->base);
233 tmp &= ~unset;
234 writel(tmp, ns_pinctrl->base);
235
236 return 0;
237 }
238
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,
244 };
245
246 /*
247 * Controller code
248 */
249
250 static struct pinctrl_desc ns_pinctrl_desc = {
251 .name = "pinctrl-ns",
252 .pctlops = &ns_pinctrl_ops,
253 .pmxops = &ns_pinctrl_pmxops,
254 };
255
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, },
260 { }
261 };
262
263 static int ns_pinctrl_probe(struct platform_device *pdev)
264 {
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;
273 int i;
274
275 ns_pinctrl = devm_kzalloc(dev, sizeof(*ns_pinctrl), GFP_KERNEL);
276 if (!ns_pinctrl)
277 return -ENOMEM;
278 pctldesc = &ns_pinctrl->pctldesc;
279 platform_set_drvdata(pdev, ns_pinctrl);
280
281 /* Set basic properties */
282
283 ns_pinctrl->dev = dev;
284
285 of_id = of_match_device(ns_pinctrl_of_match_table, dev);
286 if (!of_id)
287 return -EINVAL;
288 ns_pinctrl->chipset_flag = (uintptr_t)of_id->data;
289
290 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
291 "cru_gpio_control");
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);
296 }
297
298 memcpy(pctldesc, &ns_pinctrl_desc, sizeof(*pctldesc));
299
300 /* Set pinctrl properties */
301
302 pctldesc->pins = devm_kcalloc(dev, ARRAY_SIZE(ns_pinctrl_pins),
303 sizeof(struct pinctrl_pin_desc),
304 GFP_KERNEL);
305 if (!pctldesc->pins)
306 return -ENOMEM;
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;
311
312 if (chipsets & ns_pinctrl->chipset_flag) {
313 memcpy(pin++, src, sizeof(*src));
314 pctldesc->npins++;
315 }
316 }
317
318 ns_pinctrl->groups = devm_kcalloc(dev, ARRAY_SIZE(ns_pinctrl_groups),
319 sizeof(struct ns_pinctrl_group),
320 GFP_KERNEL);
321 if (!ns_pinctrl->groups)
322 return -ENOMEM;
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];
326
327 if (src->chipsets & ns_pinctrl->chipset_flag) {
328 memcpy(group++, src, sizeof(*src));
329 ns_pinctrl->num_groups++;
330 }
331 }
332
333 ns_pinctrl->functions = devm_kcalloc(dev,
334 ARRAY_SIZE(ns_pinctrl_functions),
335 sizeof(struct ns_pinctrl_function),
336 GFP_KERNEL);
337 if (!ns_pinctrl->functions)
338 return -ENOMEM;
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];
342
343 if (src->chipsets & ns_pinctrl->chipset_flag) {
344 memcpy(function++, src, sizeof(*src));
345 ns_pinctrl->num_functions++;
346 }
347 }
348
349 /* Register */
350
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);
355 }
356
357 return 0;
358 }
359
360 static struct platform_driver ns_pinctrl_driver = {
361 .probe = ns_pinctrl_probe,
362 .driver = {
363 .name = "ns-pinmux",
364 .of_match_table = ns_pinctrl_of_match_table,
365 },
366 };
367
368 module_platform_driver(ns_pinctrl_driver);
369
370 MODULE_AUTHOR("Rafał Miłecki");
371 MODULE_LICENSE("GPL v2");
372 MODULE_DEVICE_TABLE(of, ns_pinctrl_of_match_table);