bmips: switch to linux 5.15
[openwrt/staging/noltari.git] / target / linux / bmips / patches-5.10 / 061-v5.13-pinctrl-add-a-pincontrol-driver-for-BCM6358.patch
1 From 9494b16976e1ae3afc643abf638a25f2ce4c3f2b Mon Sep 17 00:00:00 2001
2 From: =?UTF-8?q?=C3=81lvaro=20Fern=C3=A1ndez=20Rojas?= <noltari@gmail.com>
3 Date: Wed, 24 Mar 2021 09:19:11 +0100
4 Subject: [PATCH 10/22] pinctrl: add a pincontrol driver for BCM6358
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
9 Add a pincotrol driver for BCM6358. BCM6358 allow overlaying different
10 functions onto the GPIO pins. It does not support configuring individual
11 pins but only whole groups. These groups may overlap, and still require
12 the directions to be set correctly in the GPIO register. In addition the
13 functions register controls other, not directly mux related functions.
14
15 Co-developed-by: Jonas Gorski <jonas.gorski@gmail.com>
16 Signed-off-by: Jonas Gorski <jonas.gorski@gmail.com>
17 Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>
18 Link: https://lore.kernel.org/r/20210324081923.20379-11-noltari@gmail.com
19 Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
20 ---
21 drivers/pinctrl/bcm/Kconfig | 8 +
22 drivers/pinctrl/bcm/Makefile | 1 +
23 drivers/pinctrl/bcm/pinctrl-bcm6358.c | 369 ++++++++++++++++++++++++++
24 3 files changed, 378 insertions(+)
25 create mode 100644 drivers/pinctrl/bcm/pinctrl-bcm6358.c
26
27 --- a/drivers/pinctrl/bcm/Kconfig
28 +++ b/drivers/pinctrl/bcm/Kconfig
29 @@ -44,6 +44,14 @@ config PINCTRL_BCM6328
30 help
31 Say Y here to enable the Broadcom BCM6328 GPIO driver.
32
33 +config PINCTRL_BCM6358
34 + bool "Broadcom BCM6358 GPIO driver"
35 + depends on (BMIPS_GENERIC || COMPILE_TEST)
36 + select PINCTRL_BCM63XX
37 + default BMIPS_GENERIC
38 + help
39 + Say Y here to enable the Broadcom BCM6358 GPIO driver.
40 +
41 config PINCTRL_IPROC_GPIO
42 bool "Broadcom iProc GPIO (with PINCONF) driver"
43 depends on OF_GPIO && (ARCH_BCM_IPROC || COMPILE_TEST)
44 --- a/drivers/pinctrl/bcm/Makefile
45 +++ b/drivers/pinctrl/bcm/Makefile
46 @@ -5,6 +5,7 @@ obj-$(CONFIG_PINCTRL_BCM281XX) += pinct
47 obj-$(CONFIG_PINCTRL_BCM2835) += pinctrl-bcm2835.o
48 obj-$(CONFIG_PINCTRL_BCM63XX) += pinctrl-bcm63xx.o
49 obj-$(CONFIG_PINCTRL_BCM6328) += pinctrl-bcm6328.o
50 +obj-$(CONFIG_PINCTRL_BCM6358) += pinctrl-bcm6358.o
51 obj-$(CONFIG_PINCTRL_IPROC_GPIO) += pinctrl-iproc-gpio.o
52 obj-$(CONFIG_PINCTRL_CYGNUS_MUX) += pinctrl-cygnus-mux.o
53 obj-$(CONFIG_PINCTRL_NS) += pinctrl-ns.o
54 --- /dev/null
55 +++ b/drivers/pinctrl/bcm/pinctrl-bcm6358.c
56 @@ -0,0 +1,369 @@
57 +// SPDX-License-Identifier: GPL-2.0+
58 +/*
59 + * Driver for BCM6358 GPIO unit (pinctrl + GPIO)
60 + *
61 + * Copyright (C) 2021 Álvaro Fernández Rojas <noltari@gmail.com>
62 + * Copyright (C) 2016 Jonas Gorski <jonas.gorski@gmail.com>
63 + */
64 +
65 +#include <linux/bits.h>
66 +#include <linux/gpio/driver.h>
67 +#include <linux/kernel.h>
68 +#include <linux/of.h>
69 +#include <linux/pinctrl/pinmux.h>
70 +#include <linux/platform_device.h>
71 +#include <linux/regmap.h>
72 +
73 +#include "../pinctrl-utils.h"
74 +
75 +#include "pinctrl-bcm63xx.h"
76 +
77 +#define BCM6358_NUM_GPIOS 40
78 +
79 +#define BCM6358_MODE_REG 0x18
80 +#define BCM6358_MODE_MUX_NONE 0
81 +#define BCM6358_MODE_MUX_EBI_CS BIT(5)
82 +#define BCM6358_MODE_MUX_UART1 BIT(6)
83 +#define BCM6358_MODE_MUX_SPI_CS BIT(7)
84 +#define BCM6358_MODE_MUX_ASYNC_MODEM BIT(8)
85 +#define BCM6358_MODE_MUX_LEGACY_LED BIT(9)
86 +#define BCM6358_MODE_MUX_SERIAL_LED BIT(10)
87 +#define BCM6358_MODE_MUX_LED BIT(11)
88 +#define BCM6358_MODE_MUX_UTOPIA BIT(12)
89 +#define BCM6358_MODE_MUX_CLKRST BIT(13)
90 +#define BCM6358_MODE_MUX_PWM_SYN_CLK BIT(14)
91 +#define BCM6358_MODE_MUX_SYS_IRQ BIT(15)
92 +
93 +struct bcm6358_pingroup {
94 + const char *name;
95 + const unsigned * const pins;
96 + const unsigned num_pins;
97 +
98 + const uint16_t mode_val;
99 +
100 + /* non-GPIO function muxes require the gpio direction to be set */
101 + const uint16_t direction;
102 +};
103 +
104 +struct bcm6358_function {
105 + const char *name;
106 + const char * const *groups;
107 + const unsigned num_groups;
108 +};
109 +
110 +struct bcm6358_priv {
111 + struct regmap_field *overlays;
112 +};
113 +
114 +#define BCM6358_GPIO_PIN(a, b, bit1, bit2, bit3) \
115 + { \
116 + .number = a, \
117 + .name = b, \
118 + .drv_data = (void *)(BCM6358_MODE_MUX_##bit1 | \
119 + BCM6358_MODE_MUX_##bit2 | \
120 + BCM6358_MODE_MUX_##bit3), \
121 + }
122 +
123 +static const struct pinctrl_pin_desc bcm6358_pins[] = {
124 + BCM6358_GPIO_PIN(0, "gpio0", LED, NONE, NONE),
125 + BCM6358_GPIO_PIN(1, "gpio1", LED, NONE, NONE),
126 + BCM6358_GPIO_PIN(2, "gpio2", LED, NONE, NONE),
127 + BCM6358_GPIO_PIN(3, "gpio3", LED, NONE, NONE),
128 + PINCTRL_PIN(4, "gpio4"),
129 + BCM6358_GPIO_PIN(5, "gpio5", SYS_IRQ, NONE, NONE),
130 + BCM6358_GPIO_PIN(6, "gpio6", SERIAL_LED, NONE, NONE),
131 + BCM6358_GPIO_PIN(7, "gpio7", SERIAL_LED, NONE, NONE),
132 + BCM6358_GPIO_PIN(8, "gpio8", PWM_SYN_CLK, NONE, NONE),
133 + BCM6358_GPIO_PIN(9, "gpio09", LEGACY_LED, NONE, NONE),
134 + BCM6358_GPIO_PIN(10, "gpio10", LEGACY_LED, NONE, NONE),
135 + BCM6358_GPIO_PIN(11, "gpio11", LEGACY_LED, NONE, NONE),
136 + BCM6358_GPIO_PIN(12, "gpio12", LEGACY_LED, ASYNC_MODEM, UTOPIA),
137 + BCM6358_GPIO_PIN(13, "gpio13", LEGACY_LED, ASYNC_MODEM, UTOPIA),
138 + BCM6358_GPIO_PIN(14, "gpio14", LEGACY_LED, ASYNC_MODEM, UTOPIA),
139 + BCM6358_GPIO_PIN(15, "gpio15", LEGACY_LED, ASYNC_MODEM, UTOPIA),
140 + PINCTRL_PIN(16, "gpio16"),
141 + PINCTRL_PIN(17, "gpio17"),
142 + PINCTRL_PIN(18, "gpio18"),
143 + PINCTRL_PIN(19, "gpio19"),
144 + PINCTRL_PIN(20, "gpio20"),
145 + PINCTRL_PIN(21, "gpio21"),
146 + BCM6358_GPIO_PIN(22, "gpio22", UTOPIA, NONE, NONE),
147 + BCM6358_GPIO_PIN(23, "gpio23", UTOPIA, NONE, NONE),
148 + BCM6358_GPIO_PIN(24, "gpio24", UTOPIA, NONE, NONE),
149 + BCM6358_GPIO_PIN(25, "gpio25", UTOPIA, NONE, NONE),
150 + BCM6358_GPIO_PIN(26, "gpio26", UTOPIA, NONE, NONE),
151 + BCM6358_GPIO_PIN(27, "gpio27", UTOPIA, NONE, NONE),
152 + BCM6358_GPIO_PIN(28, "gpio28", UTOPIA, UART1, NONE),
153 + BCM6358_GPIO_PIN(29, "gpio29", UTOPIA, UART1, NONE),
154 + BCM6358_GPIO_PIN(30, "gpio30", UTOPIA, UART1, EBI_CS),
155 + BCM6358_GPIO_PIN(31, "gpio31", UTOPIA, UART1, EBI_CS),
156 + BCM6358_GPIO_PIN(32, "gpio32", SPI_CS, NONE, NONE),
157 + BCM6358_GPIO_PIN(33, "gpio33", SPI_CS, NONE, NONE),
158 + PINCTRL_PIN(34, "gpio34"),
159 + PINCTRL_PIN(35, "gpio35"),
160 + PINCTRL_PIN(36, "gpio36"),
161 + PINCTRL_PIN(37, "gpio37"),
162 + PINCTRL_PIN(38, "gpio38"),
163 + PINCTRL_PIN(39, "gpio39"),
164 +};
165 +
166 +static unsigned ebi_cs_grp_pins[] = { 30, 31 };
167 +
168 +static unsigned uart1_grp_pins[] = { 28, 29, 30, 31 };
169 +
170 +static unsigned spi_cs_grp_pins[] = { 32, 33 };
171 +
172 +static unsigned async_modem_grp_pins[] = { 12, 13, 14, 15 };
173 +
174 +static unsigned serial_led_grp_pins[] = { 6, 7 };
175 +
176 +static unsigned legacy_led_grp_pins[] = { 9, 10, 11, 12, 13, 14, 15 };
177 +
178 +static unsigned led_grp_pins[] = { 0, 1, 2, 3 };
179 +
180 +static unsigned utopia_grp_pins[] = {
181 + 12, 13, 14, 15, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
182 +};
183 +
184 +static unsigned pwm_syn_clk_grp_pins[] = { 8 };
185 +
186 +static unsigned sys_irq_grp_pins[] = { 5 };
187 +
188 +#define BCM6358_GPIO_MUX_GROUP(n, bit, dir) \
189 + { \
190 + .name = #n, \
191 + .pins = n##_pins, \
192 + .num_pins = ARRAY_SIZE(n##_pins), \
193 + .mode_val = BCM6358_MODE_MUX_##bit, \
194 + .direction = dir, \
195 + }
196 +
197 +static const struct bcm6358_pingroup bcm6358_groups[] = {
198 + BCM6358_GPIO_MUX_GROUP(ebi_cs_grp, EBI_CS, 0x3),
199 + BCM6358_GPIO_MUX_GROUP(uart1_grp, UART1, 0x2),
200 + BCM6358_GPIO_MUX_GROUP(spi_cs_grp, SPI_CS, 0x6),
201 + BCM6358_GPIO_MUX_GROUP(async_modem_grp, ASYNC_MODEM, 0x6),
202 + BCM6358_GPIO_MUX_GROUP(legacy_led_grp, LEGACY_LED, 0x7f),
203 + BCM6358_GPIO_MUX_GROUP(serial_led_grp, SERIAL_LED, 0x3),
204 + BCM6358_GPIO_MUX_GROUP(led_grp, LED, 0xf),
205 + BCM6358_GPIO_MUX_GROUP(utopia_grp, UTOPIA, 0x000f),
206 + BCM6358_GPIO_MUX_GROUP(pwm_syn_clk_grp, PWM_SYN_CLK, 0x1),
207 + BCM6358_GPIO_MUX_GROUP(sys_irq_grp, SYS_IRQ, 0x1),
208 +};
209 +
210 +static const char * const ebi_cs_groups[] = {
211 + "ebi_cs_grp"
212 +};
213 +
214 +static const char * const uart1_groups[] = {
215 + "uart1_grp"
216 +};
217 +
218 +static const char * const spi_cs_2_3_groups[] = {
219 + "spi_cs_2_3_grp"
220 +};
221 +
222 +static const char * const async_modem_groups[] = {
223 + "async_modem_grp"
224 +};
225 +
226 +static const char * const legacy_led_groups[] = {
227 + "legacy_led_grp",
228 +};
229 +
230 +static const char * const serial_led_groups[] = {
231 + "serial_led_grp",
232 +};
233 +
234 +static const char * const led_groups[] = {
235 + "led_grp",
236 +};
237 +
238 +static const char * const clkrst_groups[] = {
239 + "clkrst_grp",
240 +};
241 +
242 +static const char * const pwm_syn_clk_groups[] = {
243 + "pwm_syn_clk_grp",
244 +};
245 +
246 +static const char * const sys_irq_groups[] = {
247 + "sys_irq_grp",
248 +};
249 +
250 +#define BCM6358_FUN(n) \
251 + { \
252 + .name = #n, \
253 + .groups = n##_groups, \
254 + .num_groups = ARRAY_SIZE(n##_groups), \
255 + }
256 +
257 +static const struct bcm6358_function bcm6358_funcs[] = {
258 + BCM6358_FUN(ebi_cs),
259 + BCM6358_FUN(uart1),
260 + BCM6358_FUN(spi_cs_2_3),
261 + BCM6358_FUN(async_modem),
262 + BCM6358_FUN(legacy_led),
263 + BCM6358_FUN(serial_led),
264 + BCM6358_FUN(led),
265 + BCM6358_FUN(clkrst),
266 + BCM6358_FUN(pwm_syn_clk),
267 + BCM6358_FUN(sys_irq),
268 +};
269 +
270 +static int bcm6358_pinctrl_get_group_count(struct pinctrl_dev *pctldev)
271 +{
272 + return ARRAY_SIZE(bcm6358_groups);
273 +}
274 +
275 +static const char *bcm6358_pinctrl_get_group_name(struct pinctrl_dev *pctldev,
276 + unsigned group)
277 +{
278 + return bcm6358_groups[group].name;
279 +}
280 +
281 +static int bcm6358_pinctrl_get_group_pins(struct pinctrl_dev *pctldev,
282 + unsigned group, const unsigned **pins,
283 + unsigned *num_pins)
284 +{
285 + *pins = bcm6358_groups[group].pins;
286 + *num_pins = bcm6358_groups[group].num_pins;
287 +
288 + return 0;
289 +}
290 +
291 +static int bcm6358_pinctrl_get_func_count(struct pinctrl_dev *pctldev)
292 +{
293 + return ARRAY_SIZE(bcm6358_funcs);
294 +}
295 +
296 +static const char *bcm6358_pinctrl_get_func_name(struct pinctrl_dev *pctldev,
297 + unsigned selector)
298 +{
299 + return bcm6358_funcs[selector].name;
300 +}
301 +
302 +static int bcm6358_pinctrl_get_groups(struct pinctrl_dev *pctldev,
303 + unsigned selector,
304 + const char * const **groups,
305 + unsigned * const num_groups)
306 +{
307 + *groups = bcm6358_funcs[selector].groups;
308 + *num_groups = bcm6358_funcs[selector].num_groups;
309 +
310 + return 0;
311 +}
312 +
313 +static int bcm6358_pinctrl_set_mux(struct pinctrl_dev *pctldev,
314 + unsigned selector, unsigned group)
315 +{
316 + struct bcm63xx_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
317 + struct bcm6358_priv *priv = pc->driver_data;
318 + const struct bcm6358_pingroup *pg = &bcm6358_groups[group];
319 + unsigned int val = pg->mode_val;
320 + unsigned int mask = val;
321 + unsigned pin;
322 +
323 + for (pin = 0; pin < pg->num_pins; pin++)
324 + mask |= (unsigned long)bcm6358_pins[pin].drv_data;
325 +
326 + regmap_field_update_bits(priv->overlays, mask, val);
327 +
328 + for (pin = 0; pin < pg->num_pins; pin++) {
329 + struct pinctrl_gpio_range *range;
330 + unsigned int hw_gpio = bcm6358_pins[pin].number;
331 +
332 + range = pinctrl_find_gpio_range_from_pin(pctldev, hw_gpio);
333 + if (range) {
334 + struct gpio_chip *gc = range->gc;
335 +
336 + if (pg->direction & BIT(pin))
337 + gc->direction_output(gc, hw_gpio, 0);
338 + else
339 + gc->direction_input(gc, hw_gpio);
340 + }
341 + }
342 +
343 + return 0;
344 +}
345 +
346 +static int bcm6358_gpio_request_enable(struct pinctrl_dev *pctldev,
347 + struct pinctrl_gpio_range *range,
348 + unsigned offset)
349 +{
350 + struct bcm63xx_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
351 + struct bcm6358_priv *priv = pc->driver_data;
352 + unsigned int mask;
353 +
354 + mask = (unsigned long) bcm6358_pins[offset].drv_data;
355 + if (!mask)
356 + return 0;
357 +
358 + /* disable all functions using this pin */
359 + return regmap_field_update_bits(priv->overlays, mask, 0);
360 +}
361 +
362 +static struct pinctrl_ops bcm6358_pctl_ops = {
363 + .dt_free_map = pinctrl_utils_free_map,
364 + .dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
365 + .get_group_name = bcm6358_pinctrl_get_group_name,
366 + .get_group_pins = bcm6358_pinctrl_get_group_pins,
367 + .get_groups_count = bcm6358_pinctrl_get_group_count,
368 +};
369 +
370 +static struct pinmux_ops bcm6358_pmx_ops = {
371 + .get_function_groups = bcm6358_pinctrl_get_groups,
372 + .get_function_name = bcm6358_pinctrl_get_func_name,
373 + .get_functions_count = bcm6358_pinctrl_get_func_count,
374 + .gpio_request_enable = bcm6358_gpio_request_enable,
375 + .set_mux = bcm6358_pinctrl_set_mux,
376 + .strict = true,
377 +};
378 +
379 +static const struct bcm63xx_pinctrl_soc bcm6358_soc = {
380 + .ngpios = BCM6358_NUM_GPIOS,
381 + .npins = ARRAY_SIZE(bcm6358_pins),
382 + .pctl_ops = &bcm6358_pctl_ops,
383 + .pins = bcm6358_pins,
384 + .pmx_ops = &bcm6358_pmx_ops,
385 +};
386 +
387 +static int bcm6358_pinctrl_probe(struct platform_device *pdev)
388 +{
389 + struct reg_field overlays = REG_FIELD(BCM6358_MODE_REG, 0, 15);
390 + struct device *dev = &pdev->dev;
391 + struct bcm63xx_pinctrl *pc;
392 + struct bcm6358_priv *priv;
393 + int err;
394 +
395 + priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
396 + if (!priv)
397 + return -ENOMEM;
398 +
399 + err = bcm63xx_pinctrl_probe(pdev, &bcm6358_soc, (void *) priv);
400 + if (err)
401 + return err;
402 +
403 + pc = platform_get_drvdata(pdev);
404 +
405 + priv->overlays = devm_regmap_field_alloc(dev, pc->regs, overlays);
406 + if (IS_ERR(priv->overlays))
407 + return PTR_ERR(priv->overlays);
408 +
409 + return 0;
410 +}
411 +
412 +static const struct of_device_id bcm6358_pinctrl_match[] = {
413 + { .compatible = "brcm,bcm6358-pinctrl", },
414 + { /* sentinel */ }
415 +};
416 +
417 +static struct platform_driver bcm6358_pinctrl_driver = {
418 + .probe = bcm6358_pinctrl_probe,
419 + .driver = {
420 + .name = "bcm6358-pinctrl",
421 + .of_match_table = bcm6358_pinctrl_match,
422 + },
423 +};
424 +
425 +builtin_platform_driver(bcm6358_pinctrl_driver);