1 From 0a7c0670525da604a8cee20bad31aeb645dacbe6 Mon Sep 17 00:00:00 2001
2 From: Jonas Gorski <jonas.gorski@gmail.com>
3 Date: Fri, 24 Jun 2016 22:12:50 +0200
4 Subject: [PATCH] pinctrl: add a pincontrol driver for BCM6328
6 Add a pincontrol driver for BCM6328. BCM628 supports muxing 32 pins as
7 GPIOs, as LEDs for the integrated LED controller, or various other
8 functions. Its pincontrol mux registers also control other aspects, like
9 switching the second USB port between host and device mode.
11 Signed-off-by: Jonas Gorski <jonas.gorski@gmail.com>
13 drivers/pinctrl/bcm63xx/Kconfig | 7 +
14 drivers/pinctrl/bcm63xx/Makefile | 1 +
15 drivers/pinctrl/bcm63xx/pinctrl-bcm6328.c | 501 ++++++++++++++++++++++++++++++
16 3 files changed, 509 insertions(+)
17 create mode 100644 drivers/pinctrl/bcm63xx/pinctrl-bcm6328.c
19 --- a/drivers/pinctrl/bcm63xx/Kconfig
20 +++ b/drivers/pinctrl/bcm63xx/Kconfig
22 config PINCTRL_BCM63XX
26 +config PINCTRL_BCM6328
27 + bool "BCM6328 pincontrol driver" if COMPILE_TEST
30 + select PINCTRL_BCM63XX
31 + select GENERIC_PINCONF
32 --- a/drivers/pinctrl/bcm63xx/Makefile
33 +++ b/drivers/pinctrl/bcm63xx/Makefile
35 obj-$(CONFIG_PINCTRL_BCM63XX) += pinctrl-bcm63xx.o
36 +obj-$(CONFIG_PINCTRL_BCM6328) += pinctrl-bcm6328.o
38 +++ b/drivers/pinctrl/bcm63xx/pinctrl-bcm6328.c
41 + * This file is subject to the terms and conditions of the GNU General Public
42 + * License. See the file "COPYING" in the main directory of this archive
45 + * Copyright (C) 2016 Jonas Gorski <jonas.gorski@gmail.com>
48 +#include <linux/bitops.h>
49 +#include <linux/gpio.h>
50 +#include <linux/kernel.h>
51 +#include <linux/slab.h>
52 +#include <linux/spinlock.h>
53 +#include <linux/of.h>
54 +#include <linux/of_gpio.h>
55 +#include <linux/platform_device.h>
57 +#include <linux/pinctrl/machine.h>
58 +#include <linux/pinctrl/pinconf.h>
59 +#include <linux/pinctrl/pinconf-generic.h>
60 +#include <linux/pinctrl/pinmux.h>
63 +#include "../pinctrl-utils.h"
65 +#include "pinctrl-bcm63xx.h"
67 +#define BCM6328_MUX_LO_REG 0x4
68 +#define BCM6328_MUX_HI_REG 0x0
69 +#define BCM6328_MUX_OTHER_REG 0xc
71 +#define BCM6328_NGPIO 32
73 +struct bcm6328_pingroup {
75 + const unsigned * const pins;
76 + const unsigned num_pins;
79 +struct bcm6328_function {
81 + const char * const *groups;
82 + const unsigned num_groups;
84 + unsigned mode_val:1;
88 +struct bcm6328_pinctrl {
90 + struct pinctrl_dev *pctldev;
91 + struct pinctrl_desc desc;
94 + void __iomem *mux[3];
98 + unsigned long requested[2];
99 + struct gpio_chip gpio;
102 +static const struct pinctrl_pin_desc bcm6328_pins[] = {
103 + PINCTRL_PIN(0, "gpio0"),
104 + PINCTRL_PIN(1, "gpio1"),
105 + PINCTRL_PIN(2, "gpio2"),
106 + PINCTRL_PIN(3, "gpio3"),
107 + PINCTRL_PIN(4, "gpio4"),
108 + PINCTRL_PIN(5, "gpio5"),
109 + PINCTRL_PIN(6, "gpio6"),
110 + PINCTRL_PIN(7, "gpio7"),
111 + PINCTRL_PIN(8, "gpio8"),
112 + PINCTRL_PIN(9, "gpio9"),
113 + PINCTRL_PIN(10, "gpio10"),
114 + PINCTRL_PIN(11, "gpio11"),
115 + PINCTRL_PIN(12, "gpio12"),
116 + PINCTRL_PIN(13, "gpio13"),
117 + PINCTRL_PIN(14, "gpio14"),
118 + PINCTRL_PIN(15, "gpio15"),
119 + PINCTRL_PIN(16, "gpio16"),
120 + PINCTRL_PIN(17, "gpio17"),
121 + PINCTRL_PIN(18, "gpio18"),
122 + PINCTRL_PIN(19, "gpio19"),
123 + PINCTRL_PIN(20, "gpio20"),
124 + PINCTRL_PIN(21, "gpio21"),
125 + PINCTRL_PIN(22, "gpio22"),
126 + PINCTRL_PIN(23, "gpio23"),
127 + PINCTRL_PIN(24, "gpio24"),
128 + PINCTRL_PIN(25, "gpio25"),
129 + PINCTRL_PIN(26, "gpio26"),
130 + PINCTRL_PIN(27, "gpio27"),
131 + PINCTRL_PIN(28, "gpio28"),
132 + PINCTRL_PIN(29, "gpio29"),
133 + PINCTRL_PIN(30, "gpio30"),
134 + PINCTRL_PIN(31, "gpio31"),
137 + * No idea where they really are; so let's put them according
138 + * to their mux offsets.
140 + PINCTRL_PIN(36, "hsspi_cs1"),
141 + PINCTRL_PIN(38, "usb_p2"),
144 +static unsigned gpio0_pins[] = { 0 };
145 +static unsigned gpio1_pins[] = { 1 };
146 +static unsigned gpio2_pins[] = { 2 };
147 +static unsigned gpio3_pins[] = { 3 };
148 +static unsigned gpio4_pins[] = { 4 };
149 +static unsigned gpio5_pins[] = { 5 };
150 +static unsigned gpio6_pins[] = { 6 };
151 +static unsigned gpio7_pins[] = { 7 };
152 +static unsigned gpio8_pins[] = { 8 };
153 +static unsigned gpio9_pins[] = { 9 };
154 +static unsigned gpio10_pins[] = { 10 };
155 +static unsigned gpio11_pins[] = { 11 };
156 +static unsigned gpio12_pins[] = { 12 };
157 +static unsigned gpio13_pins[] = { 13 };
158 +static unsigned gpio14_pins[] = { 14 };
159 +static unsigned gpio15_pins[] = { 15 };
160 +static unsigned gpio16_pins[] = { 16 };
161 +static unsigned gpio17_pins[] = { 17 };
162 +static unsigned gpio18_pins[] = { 18 };
163 +static unsigned gpio19_pins[] = { 19 };
164 +static unsigned gpio20_pins[] = { 20 };
165 +static unsigned gpio21_pins[] = { 21 };
166 +static unsigned gpio22_pins[] = { 22 };
167 +static unsigned gpio23_pins[] = { 23 };
168 +static unsigned gpio24_pins[] = { 24 };
169 +static unsigned gpio25_pins[] = { 25 };
170 +static unsigned gpio26_pins[] = { 26 };
171 +static unsigned gpio27_pins[] = { 27 };
172 +static unsigned gpio28_pins[] = { 28 };
173 +static unsigned gpio29_pins[] = { 29 };
174 +static unsigned gpio30_pins[] = { 30 };
175 +static unsigned gpio31_pins[] = { 31 };
177 +static unsigned hsspi_cs1_pins[] = { 36 };
178 +static unsigned usb_port1_pins[] = { 38 };
180 +#define BCM6328_GROUP(n) \
183 + .pins = n##_pins, \
184 + .num_pins = ARRAY_SIZE(n##_pins), \
187 +static struct bcm6328_pingroup bcm6328_groups[] = {
188 + BCM6328_GROUP(gpio0),
189 + BCM6328_GROUP(gpio1),
190 + BCM6328_GROUP(gpio2),
191 + BCM6328_GROUP(gpio3),
192 + BCM6328_GROUP(gpio4),
193 + BCM6328_GROUP(gpio5),
194 + BCM6328_GROUP(gpio6),
195 + BCM6328_GROUP(gpio7),
196 + BCM6328_GROUP(gpio8),
197 + BCM6328_GROUP(gpio9),
198 + BCM6328_GROUP(gpio10),
199 + BCM6328_GROUP(gpio11),
200 + BCM6328_GROUP(gpio12),
201 + BCM6328_GROUP(gpio13),
202 + BCM6328_GROUP(gpio14),
203 + BCM6328_GROUP(gpio15),
204 + BCM6328_GROUP(gpio16),
205 + BCM6328_GROUP(gpio17),
206 + BCM6328_GROUP(gpio18),
207 + BCM6328_GROUP(gpio19),
208 + BCM6328_GROUP(gpio20),
209 + BCM6328_GROUP(gpio21),
210 + BCM6328_GROUP(gpio22),
211 + BCM6328_GROUP(gpio23),
212 + BCM6328_GROUP(gpio24),
213 + BCM6328_GROUP(gpio25),
214 + BCM6328_GROUP(gpio26),
215 + BCM6328_GROUP(gpio27),
216 + BCM6328_GROUP(gpio28),
217 + BCM6328_GROUP(gpio29),
218 + BCM6328_GROUP(gpio30),
219 + BCM6328_GROUP(gpio31),
221 + BCM6328_GROUP(hsspi_cs1),
222 + BCM6328_GROUP(usb_port1),
226 +static const char * const led_groups[] = {
254 +static const char * const serial_led_data_groups[] = {
258 +static const char * const serial_led_clk_groups[] = {
262 +static const char * const inet_act_led_groups[] = {
266 +static const char * const pcie_clkreq_groups[] = {
270 +static const char * const ephy0_act_led_groups[] = {
274 +static const char * const ephy1_act_led_groups[] = {
278 +static const char * const ephy2_act_led_groups[] = {
282 +static const char * const ephy3_act_led_groups[] = {
286 +static const char * const hsspi_cs1_groups[] = {
290 +static const char * const usb_host_port_groups[] = {
294 +static const char * const usb_device_port_groups[] = {
298 +#define BCM6328_MODE_FUN(n) \
301 + .groups = n##_groups, \
302 + .num_groups = ARRAY_SIZE(n##_groups), \
306 +#define BCM6328_MUX_FUN(n, mux) \
309 + .groups = n##_groups, \
310 + .num_groups = ARRAY_SIZE(n##_groups), \
314 +static const struct bcm6328_function bcm6328_funcs[] = {
315 + BCM6328_MODE_FUN(led),
316 + BCM6328_MUX_FUN(serial_led_data, 2),
317 + BCM6328_MUX_FUN(serial_led_clk, 2),
318 + BCM6328_MUX_FUN(inet_act_led, 1),
319 + BCM6328_MUX_FUN(pcie_clkreq, 2),
320 + BCM6328_MUX_FUN(ephy0_act_led, 1),
321 + BCM6328_MUX_FUN(ephy1_act_led, 1),
322 + BCM6328_MUX_FUN(ephy2_act_led, 1),
323 + BCM6328_MUX_FUN(ephy3_act_led, 1),
324 + BCM6328_MUX_FUN(hsspi_cs1, 2),
325 + BCM6328_MUX_FUN(usb_host_port, 1),
326 + BCM6328_MUX_FUN(usb_device_port, 2),
329 +static int bcm6328_pinctrl_get_group_count(struct pinctrl_dev *pctldev)
331 + return ARRAY_SIZE(bcm6328_groups);
334 +static const char *bcm6328_pinctrl_get_group_name(struct pinctrl_dev *pctldev,
337 + return bcm6328_groups[group].name;
340 +static int bcm6328_pinctrl_get_group_pins(struct pinctrl_dev *pctldev,
341 + unsigned group, const unsigned **pins,
342 + unsigned *num_pins)
344 + *pins = bcm6328_groups[group].pins;
345 + *num_pins = bcm6328_groups[group].num_pins;
350 +static int bcm6328_pinctrl_get_func_count(struct pinctrl_dev *pctldev)
352 + return ARRAY_SIZE(bcm6328_funcs);
355 +static const char *bcm6328_pinctrl_get_func_name(struct pinctrl_dev *pctldev,
358 + return bcm6328_funcs[selector].name;
361 +static int bcm6328_pinctrl_get_groups(struct pinctrl_dev *pctldev,
363 + const char * const **groups,
364 + unsigned * const num_groups)
366 + *groups = bcm6328_funcs[selector].groups;
367 + *num_groups = bcm6328_funcs[selector].num_groups;
372 +static void bcm6328_pinctrl_pin_dbg_show(struct pinctrl_dev *pctldev,
373 + struct seq_file *s,
376 + seq_printf(s, " %s", dev_name(pctldev->dev));
379 +static void bcm6328_rmw_mux(struct bcm6328_pinctrl *pctl, unsigned pin,
382 + unsigned long flags;
385 + spin_lock_irqsave(&pctl->lock, flags);
387 + reg = __raw_readl(pctl->mode);
391 + __raw_writel(reg, pctl->mode);
394 + reg = __raw_readl(pctl->mux[pin / 16]);
395 + reg &= ~(3UL << (pin % 16));
396 + reg |= mux << (pin % 16);
397 + __raw_writel(reg, pctl->mux[pin / 16]);
399 + spin_unlock_irqrestore(&pctl->lock, flags);
402 +static int bcm6328_pinctrl_set_mux(struct pinctrl_dev *pctldev,
403 + unsigned selector, unsigned group)
405 + struct bcm6328_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
406 + const struct bcm6328_pingroup *grp = &bcm6328_groups[group];
407 + const struct bcm6328_function *f = &bcm6328_funcs[selector];
409 + bcm6328_rmw_mux(pctl, grp->pins[0], f->mode_val, f->mux_val);
414 +static int bcm6328_pinctrl_request(struct pinctrl_dev *pctldev,
417 + struct bcm6328_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
419 + if (test_and_set_bit(offset, pctl->requested))
425 +static int bcm6328_pinctrl_free(struct pinctrl_dev *pctldev,
428 + struct bcm6328_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
430 + clear_bit(offset, pctl->requested);
435 +static int bcm6328_gpio_request_enable(struct pinctrl_dev *pctldev,
436 + struct pinctrl_gpio_range *range,
439 + struct bcm6328_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
441 + if (test_and_set_bit(offset, pctl->requested))
444 + /* disable all functions using this pin */
445 + bcm6328_rmw_mux(pctl, offset, 0, 0);
450 +static void bcm6328_gpio_disable_free(struct pinctrl_dev *pctldev,
451 + struct pinctrl_gpio_range *range,
454 + struct bcm6328_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
456 + clear_bit(offset, pctl->requested);
459 +static struct pinctrl_ops bcm6328_pctl_ops = {
460 + .get_groups_count = bcm6328_pinctrl_get_group_count,
461 + .get_group_name = bcm6328_pinctrl_get_group_name,
462 + .get_group_pins = bcm6328_pinctrl_get_group_pins,
464 + .dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
465 + .dt_free_map = pinctrl_utils_dt_free_map,
467 + .pin_dbg_show = bcm6328_pinctrl_pin_dbg_show,
470 +static struct pinmux_ops bcm6328_pmx_ops = {
471 + .request = bcm6328_pinctrl_request,
472 + .free = bcm6328_pinctrl_free,
473 + .get_functions_count = bcm6328_pinctrl_get_func_count,
474 + .get_function_name = bcm6328_pinctrl_get_func_name,
475 + .get_function_groups = bcm6328_pinctrl_get_groups,
476 + .set_mux = bcm6328_pinctrl_set_mux,
477 + .gpio_request_enable = bcm6328_gpio_request_enable,
478 + .gpio_disable_free = bcm6328_gpio_disable_free,
481 +static int bcm6328_pinctrl_probe(struct platform_device *pdev)
483 + struct bcm6328_pinctrl *pctl;
484 + struct resource *res;
485 + void __iomem *mode, *mux;
487 + res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mode");
488 + mode = devm_ioremap_resource(&pdev->dev, res);
490 + return PTR_ERR(mode);
492 + res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mux");
493 + mux = devm_ioremap_resource(&pdev->dev, res);
495 + return PTR_ERR(mux);
497 + pctl = devm_kzalloc(&pdev->dev, sizeof(*pctl), GFP_KERNEL);
501 + spin_lock_init(&pctl->lock);
504 + pctl->mux[0] = mux + BCM6328_MUX_LO_REG;
505 + pctl->mux[1] = mux + BCM6328_MUX_HI_REG;
506 + pctl->mux[2] = mux + BCM6328_MUX_OTHER_REG;
508 + pctl->desc.name = dev_name(&pdev->dev);
509 + pctl->desc.owner = THIS_MODULE;
510 + pctl->desc.pctlops = &bcm6328_pctl_ops;
511 + pctl->desc.pmxops = &bcm6328_pmx_ops;
512 + pctl->dev = &pdev->dev;
514 + pctl->desc.npins = ARRAY_SIZE(bcm6328_pins);
515 + pctl->desc.pins = bcm6328_pins;
517 + platform_set_drvdata(pdev, pctl);
519 + pctl->pctldev = bcm63xx_pinctrl_register(pdev, &pctl->desc, pctl,
520 + &pctl->gpio, BCM6328_NGPIO);
521 + if (IS_ERR(pctl->pctldev))
522 + return PTR_ERR(pctl->pctldev);
527 +static const struct of_device_id bcm6328_pinctrl_match[] = {
528 + { .compatible = "brcm,bcm6328-pinctrl", },
532 +static struct platform_driver bcm6328_pinctrl_driver = {
533 + .probe = bcm6328_pinctrl_probe,
535 + .name = "bcm6328-pinctrl",
536 + .of_match_table = bcm6328_pinctrl_match,
540 +builtin_platform_driver(bcm6328_pinctrl_driver);