1 /* Copyright (C) 2015 Broadcom Corporation
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU General Public License as
5 * published by the Free Software Foundation version 2.
7 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
8 * kind, whether express or implied; without even the implied warranty
9 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * This file contains the Northstar plus (NSP) IOMUX driver that supports
13 * group based PINMUX configuration. The Northstar plus IOMUX controller
14 * allows pins to be individually muxed to GPIO function. The NAND and MMC is
15 * a group based selection. The gpio_a 8 - 11 are muxed with gpio_b and pwm.
16 * To select PWM, one need to enable the corresponding gpio_b as well.
21 * gpio_a (8-11) | gpio_b (0 - 3)
22 * ------------------------+-------+----------
28 #include <linux/err.h>
31 #include <linux/pinctrl/pinconf.h>
32 #include <linux/pinctrl/pinconf-generic.h>
33 #include <linux/pinctrl/pinctrl.h>
34 #include <linux/pinctrl/pinmux.h>
35 #include <linux/platform_device.h>
36 #include <linux/slab.h>
39 #include "../pinctrl-utils.h"
41 #define NSP_MUX_BASE0 0x00
42 #define NSP_MUX_BASE1 0x01
43 #define NSP_MUX_BASE2 0x02
45 * nsp IOMUX register description
47 * @base: base 0 or base 1
48 * @shift: bit shift for mux configuration of a group
49 * @mask: bit mask of the function
50 * @alt: alternate function to set to
60 * Keep track of nsp IOMUX configuration and prevent double configuration
62 * @nsp_mux: nsp IOMUX register description
63 * @is_configured: flag to indicate whether a mux setting has already been
72 * Group based IOMUX configuration
74 * @name: name of the group
75 * @pins: array of pins used by this group
76 * @num_pins: total number of pins used by this group
77 * @mux: nsp group based IOMUX configuration
79 struct nsp_pin_group
{
81 const unsigned int *pins
;
82 const unsigned int num_pins
;
83 const struct nsp_mux mux
;
87 * nsp mux function and supported pin groups
89 * @name: name of the function
90 * @groups: array of groups that can be supported by this function
91 * @num_groups: total number of groups that can be supported by this function
93 struct nsp_pin_function
{
95 const char * const *groups
;
96 const unsigned int num_groups
;
100 * nsp IOMUX pinctrl core
102 * @pctl: pointer to pinctrl_dev
103 * @dev: pointer to device
104 * @base0: first mux register
105 * @base1: second mux register
106 * @base2: third mux register
107 * @groups: pointer to array of groups
108 * @num_groups: total number of groups
109 * @functions: pointer to array of functions
110 * @num_functions: total number of functions
111 * @mux_log: pointer to the array of mux logs
112 * @lock: lock to protect register access
115 struct pinctrl_dev
*pctl
;
120 const struct nsp_pin_group
*groups
;
121 unsigned int num_groups
;
122 const struct nsp_pin_function
*functions
;
123 unsigned int num_functions
;
124 struct nsp_mux_log
*mux_log
;
129 * Description of a pin in nsp
133 * @gpio_select: reg data to select GPIO
138 unsigned int gpio_select
;
141 #define NSP_PIN_DESC(p, n, g) \
149 * List of muxable pins in nsp
151 static struct nsp_pin nsp_pins
[] = {
152 NSP_PIN_DESC(0, "spi_clk", 1),
153 NSP_PIN_DESC(1, "spi_ss", 1),
154 NSP_PIN_DESC(2, "spi_mosi", 1),
155 NSP_PIN_DESC(3, "spi_miso", 1),
156 NSP_PIN_DESC(4, "scl", 1),
157 NSP_PIN_DESC(5, "sda", 1),
158 NSP_PIN_DESC(6, "mdc", 1),
159 NSP_PIN_DESC(7, "mdio", 1),
160 NSP_PIN_DESC(8, "pwm0", 1),
161 NSP_PIN_DESC(9, "pwm1", 1),
162 NSP_PIN_DESC(10, "pwm2", 1),
163 NSP_PIN_DESC(11, "pwm3", 1),
164 NSP_PIN_DESC(12, "uart1_rx", 1),
165 NSP_PIN_DESC(13, "uart1_tx", 1),
166 NSP_PIN_DESC(14, "uart1_cts", 1),
167 NSP_PIN_DESC(15, "uart1_rts", 1),
168 NSP_PIN_DESC(16, "uart2_rx", 1),
169 NSP_PIN_DESC(17, "uart2_tx", 1),
170 NSP_PIN_DESC(18, "synce", 0),
171 NSP_PIN_DESC(19, "sata0_led", 0),
172 NSP_PIN_DESC(20, "sata1_led", 0),
173 NSP_PIN_DESC(21, "xtal_out", 1),
174 NSP_PIN_DESC(22, "sdio_pwr", 1),
175 NSP_PIN_DESC(23, "sdio_en_1p8v", 1),
176 NSP_PIN_DESC(24, "gpio_24", 1),
177 NSP_PIN_DESC(25, "gpio_25", 1),
178 NSP_PIN_DESC(26, "p5_led0", 0),
179 NSP_PIN_DESC(27, "p5_led1", 0),
180 NSP_PIN_DESC(28, "gpio_28", 1),
181 NSP_PIN_DESC(29, "gpio_29", 1),
182 NSP_PIN_DESC(30, "gpio_30", 1),
183 NSP_PIN_DESC(31, "gpio_31", 1),
184 NSP_PIN_DESC(32, "nand_ale", 0),
185 NSP_PIN_DESC(33, "nand_ce0", 0),
186 NSP_PIN_DESC(34, "nand_r/b", 0),
187 NSP_PIN_DESC(35, "nand_dq0", 0),
188 NSP_PIN_DESC(36, "nand_dq1", 0),
189 NSP_PIN_DESC(37, "nand_dq2", 0),
190 NSP_PIN_DESC(38, "nand_dq3", 0),
191 NSP_PIN_DESC(39, "nand_dq4", 0),
192 NSP_PIN_DESC(40, "nand_dq5", 0),
193 NSP_PIN_DESC(41, "nand_dq6", 0),
194 NSP_PIN_DESC(42, "nand_dq7", 0),
198 * List of groups of pins
201 static const unsigned int spi_pins
[] = {0, 1, 2, 3};
202 static const unsigned int i2c_pins
[] = {4, 5};
203 static const unsigned int mdio_pins
[] = {6, 7};
204 static const unsigned int pwm0_pins
[] = {8};
205 static const unsigned int gpio_b_0_pins
[] = {8};
206 static const unsigned int pwm1_pins
[] = {9};
207 static const unsigned int gpio_b_1_pins
[] = {9};
208 static const unsigned int pwm2_pins
[] = {10};
209 static const unsigned int gpio_b_2_pins
[] = {10};
210 static const unsigned int pwm3_pins
[] = {11};
211 static const unsigned int gpio_b_3_pins
[] = {11};
212 static const unsigned int uart1_pins
[] = {12, 13, 14, 15};
213 static const unsigned int uart2_pins
[] = {16, 17};
214 static const unsigned int synce_pins
[] = {18};
215 static const unsigned int sata0_led_pins
[] = {19};
216 static const unsigned int sata1_led_pins
[] = {20};
217 static const unsigned int xtal_out_pins
[] = {21};
218 static const unsigned int sdio_pwr_pins
[] = {22};
219 static const unsigned int sdio_1p8v_pins
[] = {23};
220 static const unsigned int switch_p05_led0_pins
[] = {26};
221 static const unsigned int switch_p05_led1_pins
[] = {27};
222 static const unsigned int nand_pins
[] = {32, 33, 34, 35, 36, 37, 38, 39,
224 static const unsigned int emmc_pins
[] = {32, 33, 34, 35, 36, 37, 38, 39,
227 #define NSP_PIN_GROUP(group_name, ba, sh, ma, al) \
229 .name = __stringify(group_name) "_grp", \
230 .pins = group_name ## _pins, \
231 .num_pins = ARRAY_SIZE(group_name ## _pins), \
241 * List of nsp pin groups
243 static const struct nsp_pin_group nsp_pin_groups
[] = {
244 NSP_PIN_GROUP(spi
, NSP_MUX_BASE0
, 0, 0x0f, 0x00),
245 NSP_PIN_GROUP(i2c
, NSP_MUX_BASE0
, 3, 0x03, 0x00),
246 NSP_PIN_GROUP(mdio
, NSP_MUX_BASE0
, 5, 0x03, 0x00),
247 NSP_PIN_GROUP(gpio_b_0
, NSP_MUX_BASE0
, 7, 0x01, 0x00),
248 NSP_PIN_GROUP(pwm0
, NSP_MUX_BASE1
, 0, 0x01, 0x01),
249 NSP_PIN_GROUP(gpio_b_1
, NSP_MUX_BASE0
, 8, 0x01, 0x00),
250 NSP_PIN_GROUP(pwm1
, NSP_MUX_BASE1
, 1, 0x01, 0x01),
251 NSP_PIN_GROUP(gpio_b_2
, NSP_MUX_BASE0
, 9, 0x01, 0x00),
252 NSP_PIN_GROUP(pwm2
, NSP_MUX_BASE1
, 2, 0x01, 0x01),
253 NSP_PIN_GROUP(gpio_b_3
, NSP_MUX_BASE0
, 10, 0x01, 0x00),
254 NSP_PIN_GROUP(pwm3
, NSP_MUX_BASE1
, 3, 0x01, 0x01),
255 NSP_PIN_GROUP(uart1
, NSP_MUX_BASE0
, 11, 0x0f, 0x00),
256 NSP_PIN_GROUP(uart2
, NSP_MUX_BASE0
, 15, 0x03, 0x00),
257 NSP_PIN_GROUP(synce
, NSP_MUX_BASE0
, 17, 0x01, 0x01),
258 NSP_PIN_GROUP(sata0_led
, NSP_MUX_BASE0
, 18, 0x01, 0x01),
259 NSP_PIN_GROUP(sata1_led
, NSP_MUX_BASE0
, 19, 0x01, 0x01),
260 NSP_PIN_GROUP(xtal_out
, NSP_MUX_BASE0
, 20, 0x01, 0x00),
261 NSP_PIN_GROUP(sdio_pwr
, NSP_MUX_BASE0
, 21, 0x01, 0x00),
262 NSP_PIN_GROUP(sdio_1p8v
, NSP_MUX_BASE0
, 22, 0x01, 0x00),
263 NSP_PIN_GROUP(switch_p05_led0
, NSP_MUX_BASE0
, 26, 0x01, 0x01),
264 NSP_PIN_GROUP(switch_p05_led1
, NSP_MUX_BASE0
, 27, 0x01, 0x01),
265 NSP_PIN_GROUP(nand
, NSP_MUX_BASE2
, 0, 0x01, 0x00),
266 NSP_PIN_GROUP(emmc
, NSP_MUX_BASE2
, 0, 0x01, 0x01)
270 * List of groups supported by functions
273 static const char * const spi_grps
[] = {"spi_grp"};
274 static const char * const i2c_grps
[] = {"i2c_grp"};
275 static const char * const mdio_grps
[] = {"mdio_grp"};
276 static const char * const pwm_grps
[] = {"pwm0_grp", "pwm1_grp", "pwm2_grp"
278 static const char * const gpio_b_grps
[] = {"gpio_b_0_grp", "gpio_b_1_grp",
279 "gpio_b_2_grp", "gpio_b_3_grp"};
280 static const char * const uart1_grps
[] = {"uart1_grp"};
281 static const char * const uart2_grps
[] = {"uart2_grp"};
282 static const char * const synce_grps
[] = {"synce_grp"};
283 static const char * const sata_led_grps
[] = {"sata0_led_grp", "sata1_led_grp"};
284 static const char * const xtal_out_grps
[] = {"xtal_out_grp"};
285 static const char * const sdio_grps
[] = {"sdio_pwr_grp", "sdio_1p8v_grp"};
286 static const char * const switch_led_grps
[] = {"switch_p05_led0_grp",
287 "switch_p05_led1_grp"};
288 static const char * const nand_grps
[] = {"nand_grp"};
289 static const char * const emmc_grps
[] = {"emmc_grp"};
291 #define NSP_PIN_FUNCTION(func) \
294 .groups = func ## _grps, \
295 .num_groups = ARRAY_SIZE(func ## _grps), \
299 * List of supported functions in nsp
301 static const struct nsp_pin_function nsp_pin_functions
[] = {
302 NSP_PIN_FUNCTION(spi
),
303 NSP_PIN_FUNCTION(i2c
),
304 NSP_PIN_FUNCTION(mdio
),
305 NSP_PIN_FUNCTION(pwm
),
306 NSP_PIN_FUNCTION(gpio_b
),
307 NSP_PIN_FUNCTION(uart1
),
308 NSP_PIN_FUNCTION(uart2
),
309 NSP_PIN_FUNCTION(synce
),
310 NSP_PIN_FUNCTION(sata_led
),
311 NSP_PIN_FUNCTION(xtal_out
),
312 NSP_PIN_FUNCTION(sdio
),
313 NSP_PIN_FUNCTION(switch_led
),
314 NSP_PIN_FUNCTION(nand
),
315 NSP_PIN_FUNCTION(emmc
)
318 static int nsp_get_groups_count(struct pinctrl_dev
*pctrl_dev
)
320 struct nsp_pinctrl
*pinctrl
= pinctrl_dev_get_drvdata(pctrl_dev
);
322 return pinctrl
->num_groups
;
325 static const char *nsp_get_group_name(struct pinctrl_dev
*pctrl_dev
,
326 unsigned int selector
)
328 struct nsp_pinctrl
*pinctrl
= pinctrl_dev_get_drvdata(pctrl_dev
);
330 return pinctrl
->groups
[selector
].name
;
333 static int nsp_get_group_pins(struct pinctrl_dev
*pctrl_dev
,
334 unsigned int selector
, const unsigned int **pins
,
335 unsigned int *num_pins
)
337 struct nsp_pinctrl
*pinctrl
= pinctrl_dev_get_drvdata(pctrl_dev
);
339 *pins
= pinctrl
->groups
[selector
].pins
;
340 *num_pins
= pinctrl
->groups
[selector
].num_pins
;
345 static void nsp_pin_dbg_show(struct pinctrl_dev
*pctrl_dev
,
346 struct seq_file
*s
, unsigned int offset
)
348 seq_printf(s
, " %s", dev_name(pctrl_dev
->dev
));
351 static const struct pinctrl_ops nsp_pinctrl_ops
= {
352 .get_groups_count
= nsp_get_groups_count
,
353 .get_group_name
= nsp_get_group_name
,
354 .get_group_pins
= nsp_get_group_pins
,
355 .pin_dbg_show
= nsp_pin_dbg_show
,
356 .dt_node_to_map
= pinconf_generic_dt_node_to_map_group
,
357 .dt_free_map
= pinctrl_utils_free_map
,
360 static int nsp_get_functions_count(struct pinctrl_dev
*pctrl_dev
)
362 struct nsp_pinctrl
*pinctrl
= pinctrl_dev_get_drvdata(pctrl_dev
);
364 return pinctrl
->num_functions
;
367 static const char *nsp_get_function_name(struct pinctrl_dev
*pctrl_dev
,
368 unsigned int selector
)
370 struct nsp_pinctrl
*pinctrl
= pinctrl_dev_get_drvdata(pctrl_dev
);
372 return pinctrl
->functions
[selector
].name
;
375 static int nsp_get_function_groups(struct pinctrl_dev
*pctrl_dev
,
376 unsigned int selector
,
377 const char * const **groups
,
378 unsigned * const num_groups
)
380 struct nsp_pinctrl
*pinctrl
= pinctrl_dev_get_drvdata(pctrl_dev
);
382 *groups
= pinctrl
->functions
[selector
].groups
;
383 *num_groups
= pinctrl
->functions
[selector
].num_groups
;
388 static int nsp_pinmux_set(struct nsp_pinctrl
*pinctrl
,
389 const struct nsp_pin_function
*func
,
390 const struct nsp_pin_group
*grp
,
391 struct nsp_mux_log
*mux_log
)
393 const struct nsp_mux
*mux
= &grp
->mux
;
397 void __iomem
*base_address
;
399 for (i
= 0; i
< pinctrl
->num_groups
; i
++) {
400 if ((mux
->shift
!= mux_log
[i
].mux
.shift
) ||
401 (mux
->base
!= mux_log
[i
].mux
.base
))
404 /* if this is a new configuration, just do it! */
405 if (!mux_log
[i
].is_configured
)
409 * IOMUX has been configured previously and one is trying to
410 * configure it to a different function
412 if (mux_log
[i
].mux
.alt
!= mux
->alt
) {
413 dev_err(pinctrl
->dev
,
414 "double configuration error detected!\n");
415 dev_err(pinctrl
->dev
, "func:%s grp:%s\n",
416 func
->name
, grp
->name
);
422 if (i
== pinctrl
->num_groups
)
426 mux_log
[i
].mux
.alt
= mux
->alt
;
427 mux_log
[i
].is_configured
= true;
431 base_address
= pinctrl
->base0
;
435 base_address
= pinctrl
->base1
;
439 base_address
= pinctrl
->base2
;
446 spin_lock_irqsave(&pinctrl
->lock
, flags
);
447 val
= readl(base_address
);
448 val
&= ~(mask
<< grp
->mux
.shift
);
449 val
|= grp
->mux
.alt
<< grp
->mux
.shift
;
450 writel(val
, base_address
);
451 spin_unlock_irqrestore(&pinctrl
->lock
, flags
);
456 static int nsp_pinmux_enable(struct pinctrl_dev
*pctrl_dev
,
457 unsigned int func_select
, unsigned int grp_select
)
459 struct nsp_pinctrl
*pinctrl
= pinctrl_dev_get_drvdata(pctrl_dev
);
460 const struct nsp_pin_function
*func
;
461 const struct nsp_pin_group
*grp
;
463 if (grp_select
> pinctrl
->num_groups
||
464 func_select
> pinctrl
->num_functions
)
467 func
= &pinctrl
->functions
[func_select
];
468 grp
= &pinctrl
->groups
[grp_select
];
470 dev_dbg(pctrl_dev
->dev
, "func:%u name:%s grp:%u name:%s\n",
471 func_select
, func
->name
, grp_select
, grp
->name
);
473 dev_dbg(pctrl_dev
->dev
, "shift:%u alt:%u\n", grp
->mux
.shift
,
476 return nsp_pinmux_set(pinctrl
, func
, grp
, pinctrl
->mux_log
);
480 static int nsp_gpio_request_enable(struct pinctrl_dev
*pctrl_dev
,
481 struct pinctrl_gpio_range
*range
,
484 struct nsp_pinctrl
*pinctrl
= pinctrl_dev_get_drvdata(pctrl_dev
);
485 u32
*gpio_select
= pctrl_dev
->desc
->pins
[pin
].drv_data
;
489 spin_lock_irqsave(&pinctrl
->lock
, flags
);
490 val
= readl(pinctrl
->base0
);
491 if ((val
& BIT(pin
)) != (*gpio_select
<< pin
)) {
493 val
|= *gpio_select
<< pin
;
494 writel(val
, pinctrl
->base0
);
496 spin_unlock_irqrestore(&pinctrl
->lock
, flags
);
501 static void nsp_gpio_disable_free(struct pinctrl_dev
*pctrl_dev
,
502 struct pinctrl_gpio_range
*range
,
505 struct nsp_pinctrl
*pinctrl
= pinctrl_dev_get_drvdata(pctrl_dev
);
506 u32
*gpio_select
= pctrl_dev
->desc
->pins
[pin
].drv_data
;
510 spin_lock_irqsave(&pinctrl
->lock
, flags
);
511 val
= readl(pinctrl
->base0
);
512 if ((val
& (1 << pin
)) == (*gpio_select
<< pin
)) {
516 writel(val
, pinctrl
->base0
);
518 spin_unlock_irqrestore(&pinctrl
->lock
, flags
);
521 static const struct pinmux_ops nsp_pinmux_ops
= {
522 .get_functions_count
= nsp_get_functions_count
,
523 .get_function_name
= nsp_get_function_name
,
524 .get_function_groups
= nsp_get_function_groups
,
525 .set_mux
= nsp_pinmux_enable
,
526 .gpio_request_enable
= nsp_gpio_request_enable
,
527 .gpio_disable_free
= nsp_gpio_disable_free
,
530 static struct pinctrl_desc nsp_pinctrl_desc
= {
531 .name
= "nsp-pinmux",
532 .pctlops
= &nsp_pinctrl_ops
,
533 .pmxops
= &nsp_pinmux_ops
,
536 static int nsp_mux_log_init(struct nsp_pinctrl
*pinctrl
)
538 struct nsp_mux_log
*log
;
540 u32 no_of_groups
= ARRAY_SIZE(nsp_pin_groups
);
542 pinctrl
->mux_log
= devm_kcalloc(pinctrl
->dev
, no_of_groups
,
543 sizeof(struct nsp_mux_log
),
545 if (!pinctrl
->mux_log
)
548 for (i
= 0; i
< no_of_groups
; i
++) {
549 log
= &pinctrl
->mux_log
[i
];
550 log
->mux
.base
= nsp_pin_groups
[i
].mux
.base
;
551 log
->mux
.shift
= nsp_pin_groups
[i
].mux
.shift
;
553 log
->is_configured
= false;
559 static int nsp_pinmux_probe(struct platform_device
*pdev
)
561 struct nsp_pinctrl
*pinctrl
;
562 struct resource
*res
;
564 struct pinctrl_pin_desc
*pins
;
565 unsigned int num_pins
= ARRAY_SIZE(nsp_pins
);
567 pinctrl
= devm_kzalloc(&pdev
->dev
, sizeof(*pinctrl
), GFP_KERNEL
);
570 pinctrl
->dev
= &pdev
->dev
;
571 platform_set_drvdata(pdev
, pinctrl
);
572 spin_lock_init(&pinctrl
->lock
);
574 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
575 pinctrl
->base0
= devm_ioremap_resource(&pdev
->dev
, res
);
576 if (IS_ERR(pinctrl
->base0
))
577 return PTR_ERR(pinctrl
->base0
);
579 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
580 pinctrl
->base1
= devm_ioremap_nocache(&pdev
->dev
, res
->start
,
582 if (!pinctrl
->base1
) {
583 dev_err(&pdev
->dev
, "unable to map I/O space\n");
587 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 2);
588 pinctrl
->base2
= devm_ioremap_resource(&pdev
->dev
, res
);
589 if (IS_ERR(pinctrl
->base2
))
590 return PTR_ERR(pinctrl
->base2
);
592 ret
= nsp_mux_log_init(pinctrl
);
594 dev_err(&pdev
->dev
, "unable to initialize IOMUX log\n");
598 pins
= devm_kcalloc(&pdev
->dev
, num_pins
, sizeof(*pins
), GFP_KERNEL
);
602 for (i
= 0; i
< num_pins
; i
++) {
603 pins
[i
].number
= nsp_pins
[i
].pin
;
604 pins
[i
].name
= nsp_pins
[i
].name
;
605 pins
[i
].drv_data
= &nsp_pins
[i
].gpio_select
;
608 pinctrl
->groups
= nsp_pin_groups
;
609 pinctrl
->num_groups
= ARRAY_SIZE(nsp_pin_groups
);
610 pinctrl
->functions
= nsp_pin_functions
;
611 pinctrl
->num_functions
= ARRAY_SIZE(nsp_pin_functions
);
612 nsp_pinctrl_desc
.pins
= pins
;
613 nsp_pinctrl_desc
.npins
= num_pins
;
615 pinctrl
->pctl
= devm_pinctrl_register(&pdev
->dev
, &nsp_pinctrl_desc
,
617 if (IS_ERR(pinctrl
->pctl
)) {
618 dev_err(&pdev
->dev
, "unable to register nsp IOMUX pinctrl\n");
619 return PTR_ERR(pinctrl
->pctl
);
625 static const struct of_device_id nsp_pinmux_of_match
[] = {
626 { .compatible
= "brcm,nsp-pinmux" },
630 static struct platform_driver nsp_pinmux_driver
= {
632 .name
= "nsp-pinmux",
633 .of_match_table
= nsp_pinmux_of_match
,
635 .probe
= nsp_pinmux_probe
,
638 static int __init
nsp_pinmux_init(void)
640 return platform_driver_register(&nsp_pinmux_driver
);
642 arch_initcall(nsp_pinmux_init
);