1 // SPDX-License-Identifier: (GPL-2.0 OR MIT)
3 * Microsemi SoCs pinctrl driver
5 * Author: <alexandre.belloni@free-electrons.com>
6 * License: Dual MIT/GPL
7 * Copyright (c) 2017 Microsemi Corporation
10 #include <linux/gpio/driver.h>
11 #include <linux/interrupt.h>
13 #include <linux/of_device.h>
14 #include <linux/of_irq.h>
15 #include <linux/of_platform.h>
16 #include <linux/pinctrl/pinctrl.h>
17 #include <linux/pinctrl/pinmux.h>
18 #include <linux/pinctrl/pinconf.h>
19 #include <linux/pinctrl/pinconf-generic.h>
20 #include <linux/platform_device.h>
21 #include <linux/regmap.h>
22 #include <linux/slab.h>
28 #define ocelot_clrsetbits(addr, clear, set) \
29 writel((readl(addr) & ~(clear)) | (set), (addr))
31 /* PINCONFIG bits (sparx5 only) */
35 PINCONF_DRIVE_STRENGTH,
38 #define BIAS_PD_BIT BIT(4)
39 #define BIAS_PU_BIT BIT(3)
40 #define BIAS_BITS (BIAS_PD_BIT|BIAS_PU_BIT)
41 #define SCHMITT_BIT BIT(2)
42 #define DRIVE_BITS GENMASK(1, 0)
44 /* GPIO standard registers */
45 #define OCELOT_GPIO_OUT_SET 0x0
46 #define OCELOT_GPIO_OUT_CLR 0x4
47 #define OCELOT_GPIO_OUT 0x8
48 #define OCELOT_GPIO_IN 0xc
49 #define OCELOT_GPIO_OE 0x10
50 #define OCELOT_GPIO_INTR 0x14
51 #define OCELOT_GPIO_INTR_ENA 0x18
52 #define OCELOT_GPIO_INTR_IDENT 0x1c
53 #define OCELOT_GPIO_ALT0 0x20
54 #define OCELOT_GPIO_ALT1 0x24
55 #define OCELOT_GPIO_SD_MAP 0x28
57 #define OCELOT_FUNC_PER_PIN 4
100 static const char *const ocelot_function_names[] = {
101 [FUNC_NONE] = "none",
102 [FUNC_GPIO] = "gpio",
103 [FUNC_IRQ0] = "irq0",
104 [FUNC_IRQ0_IN] = "irq0_in",
105 [FUNC_IRQ0_OUT] = "irq0_out",
106 [FUNC_IRQ1] = "irq1",
107 [FUNC_IRQ1_IN] = "irq1_in",
108 [FUNC_IRQ1_OUT] = "irq1_out",
109 [FUNC_EXT_IRQ] = "ext_irq",
110 [FUNC_MIIM] = "miim",
111 [FUNC_PHY_LED] = "phy_led",
112 [FUNC_PCI_WAKE] = "pci_wake",
114 [FUNC_PTP0] = "ptp0",
115 [FUNC_PTP1] = "ptp1",
116 [FUNC_PTP2] = "ptp2",
117 [FUNC_PTP3] = "ptp3",
119 [FUNC_RECO_CLK] = "reco_clk",
126 [FUNC_TACHO] = "tacho",
128 [FUNC_TWI2] = "twi2",
129 [FUNC_TWI3] = "twi3",
130 [FUNC_TWI_SCL_M] = "twi_scl_m",
131 [FUNC_UART] = "uart",
132 [FUNC_UART2] = "uart2",
133 [FUNC_UART3] = "uart3",
134 [FUNC_PLL_STAT] = "pll_stat",
135 [FUNC_EMMC] = "emmc",
136 [FUNC_REF_CLK] = "ref_clk",
137 [FUNC_RCVRD_CLK] = "rcvrd_clk",
140 struct ocelot_pmx_func {
142 unsigned int ngroups;
145 struct ocelot_pin_caps {
147 unsigned char functions[OCELOT_FUNC_PER_PIN];
150 struct ocelot_pinctrl {
152 struct pinctrl_dev *pctl;
153 struct gpio_chip gpio_chip;
155 void __iomem *pincfg;
156 struct pinctrl_desc *desc;
157 struct ocelot_pmx_func func[FUNC_MAX];
161 #define LUTON_P(p, f0, f1) \
162 static struct ocelot_pin_caps luton_pin_##p = { \
165 FUNC_GPIO, FUNC_##f0, FUNC_##f1, FUNC_NONE, \
169 LUTON_P(0, SG0, NONE);
170 LUTON_P(1, SG0, NONE);
171 LUTON_P(2, SG0, NONE);
172 LUTON_P(3, SG0, NONE);
173 LUTON_P(4, TACHO, NONE);
174 LUTON_P(5, TWI, PHY_LED);
175 LUTON_P(6, TWI, PHY_LED);
176 LUTON_P(7, NONE, PHY_LED);
177 LUTON_P(8, EXT_IRQ, PHY_LED);
178 LUTON_P(9, EXT_IRQ, PHY_LED);
179 LUTON_P(10, SFP, PHY_LED);
180 LUTON_P(11, SFP, PHY_LED);
181 LUTON_P(12, SFP, PHY_LED);
182 LUTON_P(13, SFP, PHY_LED);
183 LUTON_P(14, SI, PHY_LED);
184 LUTON_P(15, SI, PHY_LED);
185 LUTON_P(16, SI, PHY_LED);
186 LUTON_P(17, SFP, PHY_LED);
187 LUTON_P(18, SFP, PHY_LED);
188 LUTON_P(19, SFP, PHY_LED);
189 LUTON_P(20, SFP, PHY_LED);
190 LUTON_P(21, SFP, PHY_LED);
191 LUTON_P(22, SFP, PHY_LED);
192 LUTON_P(23, SFP, PHY_LED);
193 LUTON_P(24, SFP, PHY_LED);
194 LUTON_P(25, SFP, PHY_LED);
195 LUTON_P(26, SFP, PHY_LED);
196 LUTON_P(27, SFP, PHY_LED);
197 LUTON_P(28, SFP, PHY_LED);
198 LUTON_P(29, PWM, NONE);
199 LUTON_P(30, UART, NONE);
200 LUTON_P(31, UART, NONE);
202 #define LUTON_PIN(n) { \
205 .drv_data = &luton_pin_##n \
208 static const struct pinctrl_pin_desc luton_pins[] = {
243 #define SERVAL_P(p, f0, f1, f2) \
244 static struct ocelot_pin_caps serval_pin_##p = { \
247 FUNC_GPIO, FUNC_##f0, FUNC_##f1, FUNC_##f2, \
251 SERVAL_P(0, SG0, NONE, NONE);
252 SERVAL_P(1, SG0, NONE, NONE);
253 SERVAL_P(2, SG0, NONE, NONE);
254 SERVAL_P(3, SG0, NONE, NONE);
255 SERVAL_P(4, TACHO, NONE, NONE);
256 SERVAL_P(5, PWM, NONE, NONE);
257 SERVAL_P(6, TWI, NONE, NONE);
258 SERVAL_P(7, TWI, NONE, NONE);
259 SERVAL_P(8, SI, NONE, NONE);
260 SERVAL_P(9, SI, MD, NONE);
261 SERVAL_P(10, SI, MD, NONE);
262 SERVAL_P(11, SFP, MD, TWI_SCL_M);
263 SERVAL_P(12, SFP, MD, TWI_SCL_M);
264 SERVAL_P(13, SFP, UART2, TWI_SCL_M);
265 SERVAL_P(14, SFP, UART2, TWI_SCL_M);
266 SERVAL_P(15, SFP, PTP0, TWI_SCL_M);
267 SERVAL_P(16, SFP, PTP0, TWI_SCL_M);
268 SERVAL_P(17, SFP, PCI_WAKE, TWI_SCL_M);
269 SERVAL_P(18, SFP, NONE, TWI_SCL_M);
270 SERVAL_P(19, SFP, NONE, TWI_SCL_M);
271 SERVAL_P(20, SFP, NONE, TWI_SCL_M);
272 SERVAL_P(21, SFP, NONE, TWI_SCL_M);
273 SERVAL_P(22, NONE, NONE, NONE);
274 SERVAL_P(23, NONE, NONE, NONE);
275 SERVAL_P(24, NONE, NONE, NONE);
276 SERVAL_P(25, NONE, NONE, NONE);
277 SERVAL_P(26, UART, NONE, NONE);
278 SERVAL_P(27, UART, NONE, NONE);
279 SERVAL_P(28, IRQ0, NONE, NONE);
280 SERVAL_P(29, IRQ1, NONE, NONE);
281 SERVAL_P(30, PTP0, NONE, NONE);
282 SERVAL_P(31, PTP0, NONE, NONE);
284 #define SERVAL_PIN(n) { \
287 .drv_data = &serval_pin_##n \
290 static const struct pinctrl_pin_desc serval_pins[] = {
325 #define OCELOT_P(p, f0, f1, f2) \
326 static struct ocelot_pin_caps ocelot_pin_##p = { \
329 FUNC_GPIO, FUNC_##f0, FUNC_##f1, FUNC_##f2, \
333 OCELOT_P(0, SG0, NONE, NONE);
334 OCELOT_P(1, SG0, NONE, NONE);
335 OCELOT_P(2, SG0, NONE, NONE);
336 OCELOT_P(3, SG0, NONE, NONE);
337 OCELOT_P(4, IRQ0_IN, IRQ0_OUT, TWI_SCL_M);
338 OCELOT_P(5, IRQ1_IN, IRQ1_OUT, PCI_WAKE);
339 OCELOT_P(6, UART, TWI_SCL_M, NONE);
340 OCELOT_P(7, UART, TWI_SCL_M, NONE);
341 OCELOT_P(8, SI, TWI_SCL_M, IRQ0_OUT);
342 OCELOT_P(9, SI, TWI_SCL_M, IRQ1_OUT);
343 OCELOT_P(10, PTP2, TWI_SCL_M, SFP);
344 OCELOT_P(11, PTP3, TWI_SCL_M, SFP);
345 OCELOT_P(12, UART2, TWI_SCL_M, SFP);
346 OCELOT_P(13, UART2, TWI_SCL_M, SFP);
347 OCELOT_P(14, MIIM, TWI_SCL_M, SFP);
348 OCELOT_P(15, MIIM, TWI_SCL_M, SFP);
349 OCELOT_P(16, TWI, NONE, SI);
350 OCELOT_P(17, TWI, TWI_SCL_M, SI);
351 OCELOT_P(18, PTP0, TWI_SCL_M, NONE);
352 OCELOT_P(19, PTP1, TWI_SCL_M, NONE);
353 OCELOT_P(20, RECO_CLK, TACHO, TWI_SCL_M);
354 OCELOT_P(21, RECO_CLK, PWM, TWI_SCL_M);
356 #define OCELOT_PIN(n) { \
359 .drv_data = &ocelot_pin_##n \
362 static const struct pinctrl_pin_desc ocelot_pins[] = {
387 #define JAGUAR2_P(p, f0, f1) \
388 static struct ocelot_pin_caps jaguar2_pin_##p = { \
391 FUNC_GPIO, FUNC_##f0, FUNC_##f1, FUNC_NONE \
395 JAGUAR2_P(0, SG0, NONE);
396 JAGUAR2_P(1, SG0, NONE);
397 JAGUAR2_P(2, SG0, NONE);
398 JAGUAR2_P(3, SG0, NONE);
399 JAGUAR2_P(4, SG1, NONE);
400 JAGUAR2_P(5, SG1, NONE);
401 JAGUAR2_P(6, IRQ0_IN, IRQ0_OUT);
402 JAGUAR2_P(7, IRQ1_IN, IRQ1_OUT);
403 JAGUAR2_P(8, PTP0, NONE);
404 JAGUAR2_P(9, PTP1, NONE);
405 JAGUAR2_P(10, UART, NONE);
406 JAGUAR2_P(11, UART, NONE);
407 JAGUAR2_P(12, SG1, NONE);
408 JAGUAR2_P(13, SG1, NONE);
409 JAGUAR2_P(14, TWI, TWI_SCL_M);
410 JAGUAR2_P(15, TWI, NONE);
411 JAGUAR2_P(16, SI, TWI_SCL_M);
412 JAGUAR2_P(17, SI, TWI_SCL_M);
413 JAGUAR2_P(18, SI, TWI_SCL_M);
414 JAGUAR2_P(19, PCI_WAKE, NONE);
415 JAGUAR2_P(20, IRQ0_OUT, TWI_SCL_M);
416 JAGUAR2_P(21, IRQ1_OUT, TWI_SCL_M);
417 JAGUAR2_P(22, TACHO, NONE);
418 JAGUAR2_P(23, PWM, NONE);
419 JAGUAR2_P(24, UART2, NONE);
420 JAGUAR2_P(25, UART2, SI);
421 JAGUAR2_P(26, PTP2, SI);
422 JAGUAR2_P(27, PTP3, SI);
423 JAGUAR2_P(28, TWI2, SI);
424 JAGUAR2_P(29, TWI2, SI);
425 JAGUAR2_P(30, SG2, SI);
426 JAGUAR2_P(31, SG2, SI);
427 JAGUAR2_P(32, SG2, SI);
428 JAGUAR2_P(33, SG2, SI);
429 JAGUAR2_P(34, NONE, TWI_SCL_M);
430 JAGUAR2_P(35, NONE, TWI_SCL_M);
431 JAGUAR2_P(36, NONE, TWI_SCL_M);
432 JAGUAR2_P(37, NONE, TWI_SCL_M);
433 JAGUAR2_P(38, NONE, TWI_SCL_M);
434 JAGUAR2_P(39, NONE, TWI_SCL_M);
435 JAGUAR2_P(40, NONE, TWI_SCL_M);
436 JAGUAR2_P(41, NONE, TWI_SCL_M);
437 JAGUAR2_P(42, NONE, TWI_SCL_M);
438 JAGUAR2_P(43, NONE, TWI_SCL_M);
439 JAGUAR2_P(44, NONE, SFP);
440 JAGUAR2_P(45, NONE, SFP);
441 JAGUAR2_P(46, NONE, SFP);
442 JAGUAR2_P(47, NONE, SFP);
443 JAGUAR2_P(48, SFP, NONE);
444 JAGUAR2_P(49, SFP, SI);
445 JAGUAR2_P(50, SFP, SI);
446 JAGUAR2_P(51, SFP, SI);
447 JAGUAR2_P(52, SFP, NONE);
448 JAGUAR2_P(53, SFP, NONE);
449 JAGUAR2_P(54, SFP, NONE);
450 JAGUAR2_P(55, SFP, NONE);
451 JAGUAR2_P(56, MIIM, SFP);
452 JAGUAR2_P(57, MIIM, SFP);
453 JAGUAR2_P(58, MIIM, SFP);
454 JAGUAR2_P(59, MIIM, SFP);
455 JAGUAR2_P(60, NONE, NONE);
456 JAGUAR2_P(61, NONE, NONE);
457 JAGUAR2_P(62, NONE, NONE);
458 JAGUAR2_P(63, NONE, NONE);
460 #define JAGUAR2_PIN(n) { \
463 .drv_data = &jaguar2_pin_##n \
466 static const struct pinctrl_pin_desc jaguar2_pins[] = {
533 #define SPARX5_P(p, f0, f1, f2) \
534 static struct ocelot_pin_caps sparx5_pin_##p = { \
537 FUNC_GPIO, FUNC_##f0, FUNC_##f1, FUNC_##f2 \
541 SPARX5_P(0, SG0, PLL_STAT, NONE);
542 SPARX5_P(1, SG0, NONE, NONE);
543 SPARX5_P(2, SG0, NONE, NONE);
544 SPARX5_P(3, SG0, NONE, NONE);
545 SPARX5_P(4, SG1, NONE, NONE);
546 SPARX5_P(5, SG1, NONE, NONE);
547 SPARX5_P(6, IRQ0_IN, IRQ0_OUT, SFP);
548 SPARX5_P(7, IRQ1_IN, IRQ1_OUT, SFP);
549 SPARX5_P(8, PTP0, NONE, SFP);
550 SPARX5_P(9, PTP1, SFP, TWI_SCL_M);
551 SPARX5_P(10, UART, NONE, NONE);
552 SPARX5_P(11, UART, NONE, NONE);
553 SPARX5_P(12, SG1, NONE, NONE);
554 SPARX5_P(13, SG1, NONE, NONE);
555 SPARX5_P(14, TWI, TWI_SCL_M, NONE);
556 SPARX5_P(15, TWI, NONE, NONE);
557 SPARX5_P(16, SI, TWI_SCL_M, SFP);
558 SPARX5_P(17, SI, TWI_SCL_M, SFP);
559 SPARX5_P(18, SI, TWI_SCL_M, SFP);
560 SPARX5_P(19, PCI_WAKE, TWI_SCL_M, SFP);
561 SPARX5_P(20, IRQ0_OUT, TWI_SCL_M, SFP);
562 SPARX5_P(21, IRQ1_OUT, TACHO, SFP);
563 SPARX5_P(22, TACHO, IRQ0_OUT, TWI_SCL_M);
564 SPARX5_P(23, PWM, UART3, TWI_SCL_M);
565 SPARX5_P(24, PTP2, UART3, TWI_SCL_M);
566 SPARX5_P(25, PTP3, SI, TWI_SCL_M);
567 SPARX5_P(26, UART2, SI, TWI_SCL_M);
568 SPARX5_P(27, UART2, SI, TWI_SCL_M);
569 SPARX5_P(28, TWI2, SI, SFP);
570 SPARX5_P(29, TWI2, SI, SFP);
571 SPARX5_P(30, SG2, SI, PWM);
572 SPARX5_P(31, SG2, SI, TWI_SCL_M);
573 SPARX5_P(32, SG2, SI, TWI_SCL_M);
574 SPARX5_P(33, SG2, SI, SFP);
575 SPARX5_P(34, NONE, TWI_SCL_M, EMMC);
576 SPARX5_P(35, SFP, TWI_SCL_M, EMMC);
577 SPARX5_P(36, SFP, TWI_SCL_M, EMMC);
578 SPARX5_P(37, SFP, NONE, EMMC);
579 SPARX5_P(38, NONE, TWI_SCL_M, EMMC);
580 SPARX5_P(39, SI2, TWI_SCL_M, EMMC);
581 SPARX5_P(40, SI2, TWI_SCL_M, EMMC);
582 SPARX5_P(41, SI2, TWI_SCL_M, EMMC);
583 SPARX5_P(42, SI2, TWI_SCL_M, EMMC);
584 SPARX5_P(43, SI2, TWI_SCL_M, EMMC);
585 SPARX5_P(44, SI, SFP, EMMC);
586 SPARX5_P(45, SI, SFP, EMMC);
587 SPARX5_P(46, NONE, SFP, EMMC);
588 SPARX5_P(47, NONE, SFP, EMMC);
589 SPARX5_P(48, TWI3, SI, SFP);
590 SPARX5_P(49, TWI3, NONE, SFP);
591 SPARX5_P(50, SFP, NONE, TWI_SCL_M);
592 SPARX5_P(51, SFP, SI, TWI_SCL_M);
593 SPARX5_P(52, SFP, MIIM, TWI_SCL_M);
594 SPARX5_P(53, SFP, MIIM, TWI_SCL_M);
595 SPARX5_P(54, SFP, PTP2, TWI_SCL_M);
596 SPARX5_P(55, SFP, PTP3, PCI_WAKE);
597 SPARX5_P(56, MIIM, SFP, TWI_SCL_M);
598 SPARX5_P(57, MIIM, SFP, TWI_SCL_M);
599 SPARX5_P(58, MIIM, SFP, TWI_SCL_M);
600 SPARX5_P(59, MIIM, SFP, NONE);
601 SPARX5_P(60, RECO_CLK, NONE, NONE);
602 SPARX5_P(61, RECO_CLK, NONE, NONE);
603 SPARX5_P(62, RECO_CLK, PLL_STAT, NONE);
604 SPARX5_P(63, RECO_CLK, NONE, NONE);
606 #define SPARX5_PIN(n) { \
609 .drv_data = &sparx5_pin_##n \
612 static const struct pinctrl_pin_desc sparx5_pins[] = {
679 static int ocelot_get_functions_count(struct pinctrl_dev *pctldev)
681 return ARRAY_SIZE(ocelot_function_names);
684 static const char *ocelot_get_function_name(struct pinctrl_dev *pctldev,
685 unsigned int function)
687 return ocelot_function_names[function];
690 static int ocelot_get_function_groups(struct pinctrl_dev *pctldev,
691 unsigned int function,
692 const char *const **groups,
693 unsigned *const num_groups)
695 struct ocelot_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
697 *groups = info->func[function].groups;
698 *num_groups = info->func[function].ngroups;
703 static int ocelot_pin_function_idx(struct ocelot_pinctrl *info,
704 unsigned int pin, unsigned int function)
706 struct ocelot_pin_caps *p = info->desc->pins[pin].drv_data;
709 for (i = 0; i < OCELOT_FUNC_PER_PIN; i++) {
710 if (function == p->functions[i])
717 #define REG_ALT(msb, info, p) (OCELOT_GPIO_ALT0 * (info)->stride + 4 * ((msb) + ((info)->stride * ((p) / 32))))
719 static int ocelot_pinmux_set_mux(struct pinctrl_dev *pctldev,
720 unsigned int selector, unsigned int group)
722 struct ocelot_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
723 struct ocelot_pin_caps *pin = info->desc->pins[group].drv_data;
724 unsigned int p = pin->pin % 32;
727 f = ocelot_pin_function_idx(info, group, selector);
732 * f is encoded on two bits.
733 * bit 0 of f goes in BIT(pin) of ALT[0], bit 1 of f goes in BIT(pin) of
735 * This is racy because both registers can't be updated at the same time
736 * but it doesn't matter much for now.
737 * Note: ALT0/ALT1 are organized specially for 64 gpio targets
739 regmap_update_bits(info->map, REG_ALT(0, info, pin->pin),
741 regmap_update_bits(info->map, REG_ALT(1, info, pin->pin),
742 BIT(p), f << (p - 1));
747 #define REG(r, info, p) ((r) * (info)->stride + (4 * ((p) / 32)))
749 static int ocelot_gpio_set_direction(struct pinctrl_dev *pctldev,
750 struct pinctrl_gpio_range *range,
751 unsigned int pin, bool input)
753 struct ocelot_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
754 unsigned int p = pin % 32;
756 regmap_update_bits(info->map, REG(OCELOT_GPIO_OE, info, pin), BIT(p),
762 static int ocelot_gpio_request_enable(struct pinctrl_dev *pctldev,
763 struct pinctrl_gpio_range *range,
766 struct ocelot_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
767 unsigned int p = offset % 32;
769 regmap_update_bits(info->map, REG_ALT(0, info, offset),
771 regmap_update_bits(info->map, REG_ALT(1, info, offset),
777 static const struct pinmux_ops ocelot_pmx_ops = {
778 .get_functions_count = ocelot_get_functions_count,
779 .get_function_name = ocelot_get_function_name,
780 .get_function_groups = ocelot_get_function_groups,
781 .set_mux = ocelot_pinmux_set_mux,
782 .gpio_set_direction = ocelot_gpio_set_direction,
783 .gpio_request_enable = ocelot_gpio_request_enable,
786 static int ocelot_pctl_get_groups_count(struct pinctrl_dev *pctldev)
788 struct ocelot_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
790 return info->desc->npins;
793 static const char *ocelot_pctl_get_group_name(struct pinctrl_dev *pctldev,
796 struct ocelot_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
798 return info->desc->pins[group].name;
801 static int ocelot_pctl_get_group_pins(struct pinctrl_dev *pctldev,
803 const unsigned int **pins,
804 unsigned int *num_pins)
806 struct ocelot_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
808 *pins = &info->desc->pins[group].number;
814 static int ocelot_hw_get_value(struct ocelot_pinctrl *info,
819 int ret = -EOPNOTSUPP;
822 u32 regcfg = readl(info->pincfg + (pin * sizeof(u32)));
827 *val = regcfg & BIAS_BITS;
830 case PINCONF_SCHMITT:
831 *val = regcfg & SCHMITT_BIT;
834 case PINCONF_DRIVE_STRENGTH:
835 *val = regcfg & DRIVE_BITS;
846 static int ocelot_hw_set_value(struct ocelot_pinctrl *info,
851 int ret = -EOPNOTSUPP;
854 void __iomem *regaddr = info->pincfg + (pin * sizeof(u32));
859 ocelot_clrsetbits(regaddr, BIAS_BITS, val);
862 case PINCONF_SCHMITT:
863 ocelot_clrsetbits(regaddr, SCHMITT_BIT, val);
866 case PINCONF_DRIVE_STRENGTH:
868 ocelot_clrsetbits(regaddr, DRIVE_BITS, val);
881 static int ocelot_pinconf_get(struct pinctrl_dev *pctldev,
882 unsigned int pin, unsigned long *config)
884 struct ocelot_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
885 u32 param = pinconf_to_config_param(*config);
889 case PIN_CONFIG_BIAS_DISABLE:
890 case PIN_CONFIG_BIAS_PULL_UP:
891 case PIN_CONFIG_BIAS_PULL_DOWN:
892 err = ocelot_hw_get_value(info, pin, PINCONF_BIAS, &val);
895 if (param == PIN_CONFIG_BIAS_DISABLE)
897 else if (param == PIN_CONFIG_BIAS_PULL_DOWN)
898 val = (val & BIAS_PD_BIT ? true : false);
899 else /* PIN_CONFIG_BIAS_PULL_UP */
900 val = (val & BIAS_PU_BIT ? true : false);
903 case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
904 err = ocelot_hw_get_value(info, pin, PINCONF_SCHMITT, &val);
908 val = (val & SCHMITT_BIT ? true : false);
911 case PIN_CONFIG_DRIVE_STRENGTH:
912 err = ocelot_hw_get_value(info, pin, PINCONF_DRIVE_STRENGTH,
918 case PIN_CONFIG_OUTPUT:
919 err = regmap_read(info->map, REG(OCELOT_GPIO_OUT, info, pin),
923 val = !!(val & BIT(pin % 32));
926 case PIN_CONFIG_INPUT_ENABLE:
927 case PIN_CONFIG_OUTPUT_ENABLE:
928 err = regmap_read(info->map, REG(OCELOT_GPIO_OE, info, pin),
932 val = val & BIT(pin % 32);
933 if (param == PIN_CONFIG_OUTPUT_ENABLE)
943 *config = pinconf_to_config_packed(param, val);
948 static int ocelot_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
949 unsigned long *configs, unsigned int num_configs)
951 struct ocelot_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
955 for (cfg = 0; cfg < num_configs; cfg++) {
956 param = pinconf_to_config_param(configs[cfg]);
957 arg = pinconf_to_config_argument(configs[cfg]);
960 case PIN_CONFIG_BIAS_DISABLE:
961 case PIN_CONFIG_BIAS_PULL_UP:
962 case PIN_CONFIG_BIAS_PULL_DOWN:
963 arg = (param == PIN_CONFIG_BIAS_DISABLE) ? 0 :
964 (param == PIN_CONFIG_BIAS_PULL_UP) ? BIAS_PU_BIT :
967 err = ocelot_hw_set_value(info, pin, PINCONF_BIAS, arg);
973 case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
974 arg = arg ? SCHMITT_BIT : 0;
975 err = ocelot_hw_set_value(info, pin, PINCONF_SCHMITT,
982 case PIN_CONFIG_DRIVE_STRENGTH:
983 err = ocelot_hw_set_value(info, pin,
984 PINCONF_DRIVE_STRENGTH,
991 case PIN_CONFIG_OUTPUT_ENABLE:
992 case PIN_CONFIG_INPUT_ENABLE:
993 case PIN_CONFIG_OUTPUT:
996 regmap_write(info->map,
997 REG(OCELOT_GPIO_OUT_SET, info,
1001 regmap_write(info->map,
1002 REG(OCELOT_GPIO_OUT_CLR, info,
1005 regmap_update_bits(info->map,
1006 REG(OCELOT_GPIO_OE, info, pin),
1008 param == PIN_CONFIG_INPUT_ENABLE ?
1020 static const struct pinconf_ops ocelot_confops = {
1022 .pin_config_get = ocelot_pinconf_get,
1023 .pin_config_set = ocelot_pinconf_set,
1024 .pin_config_config_dbg_show = pinconf_generic_dump_config,
1027 static const struct pinctrl_ops ocelot_pctl_ops = {
1028 .get_groups_count = ocelot_pctl_get_groups_count,
1029 .get_group_name = ocelot_pctl_get_group_name,
1030 .get_group_pins = ocelot_pctl_get_group_pins,
1031 .dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
1032 .dt_free_map = pinconf_generic_dt_free_map,
1035 static struct pinctrl_desc luton_desc = {
1036 .name = "luton-pinctrl",
1038 .npins = ARRAY_SIZE(luton_pins),
1039 .pctlops = &ocelot_pctl_ops,
1040 .pmxops = &ocelot_pmx_ops,
1041 .owner = THIS_MODULE,
1044 static struct pinctrl_desc serval_desc = {
1045 .name = "serval-pinctrl",
1046 .pins = serval_pins,
1047 .npins = ARRAY_SIZE(serval_pins),
1048 .pctlops = &ocelot_pctl_ops,
1049 .pmxops = &ocelot_pmx_ops,
1050 .owner = THIS_MODULE,
1053 static struct pinctrl_desc ocelot_desc = {
1054 .name = "ocelot-pinctrl",
1055 .pins = ocelot_pins,
1056 .npins = ARRAY_SIZE(ocelot_pins),
1057 .pctlops = &ocelot_pctl_ops,
1058 .pmxops = &ocelot_pmx_ops,
1059 .owner = THIS_MODULE,
1062 static struct pinctrl_desc jaguar2_desc = {
1063 .name = "jaguar2-pinctrl",
1064 .pins = jaguar2_pins,
1065 .npins = ARRAY_SIZE(jaguar2_pins),
1066 .pctlops = &ocelot_pctl_ops,
1067 .pmxops = &ocelot_pmx_ops,
1068 .owner = THIS_MODULE,
1071 static struct pinctrl_desc sparx5_desc = {
1072 .name = "sparx5-pinctrl",
1073 .pins = sparx5_pins,
1074 .npins = ARRAY_SIZE(sparx5_pins),
1075 .pctlops = &ocelot_pctl_ops,
1076 .pmxops = &ocelot_pmx_ops,
1077 .confops = &ocelot_confops,
1078 .owner = THIS_MODULE,
1081 static int ocelot_create_group_func_map(struct device *dev,
1082 struct ocelot_pinctrl *info)
1085 u8 *pins = kcalloc(info->desc->npins, sizeof(u8), GFP_KERNEL);
1090 for (f = 0; f < FUNC_MAX; f++) {
1091 for (npins = 0, i = 0; i < info->desc->npins; i++) {
1092 if (ocelot_pin_function_idx(info, i, f) >= 0)
1099 info->func[f].ngroups = npins;
1100 info->func[f].groups = devm_kcalloc(dev, npins, sizeof(char *),
1102 if (!info->func[f].groups) {
1107 for (i = 0; i < npins; i++)
1108 info->func[f].groups[i] =
1109 info->desc->pins[pins[i]].name;
1117 static int ocelot_pinctrl_register(struct platform_device *pdev,
1118 struct ocelot_pinctrl *info)
1122 ret = ocelot_create_group_func_map(&pdev->dev, info);
1124 dev_err(&pdev->dev, "Unable to create group func map.\n");
1128 info->pctl = devm_pinctrl_register(&pdev->dev, info->desc, info);
1129 if (IS_ERR(info->pctl)) {
1130 dev_err(&pdev->dev, "Failed to register pinctrl\n");
1131 return PTR_ERR(info->pctl);
1137 static int ocelot_gpio_get(struct gpio_chip *chip, unsigned int offset)
1139 struct ocelot_pinctrl *info = gpiochip_get_data(chip);
1142 regmap_read(info->map, REG(OCELOT_GPIO_IN, info, offset), &val);
1144 return !!(val & BIT(offset % 32));
1147 static void ocelot_gpio_set(struct gpio_chip *chip, unsigned int offset,
1150 struct ocelot_pinctrl *info = gpiochip_get_data(chip);
1153 regmap_write(info->map, REG(OCELOT_GPIO_OUT_SET, info, offset),
1156 regmap_write(info->map, REG(OCELOT_GPIO_OUT_CLR, info, offset),
1160 static int ocelot_gpio_get_direction(struct gpio_chip *chip,
1161 unsigned int offset)
1163 struct ocelot_pinctrl *info = gpiochip_get_data(chip);
1166 regmap_read(info->map, REG(OCELOT_GPIO_OE, info, offset), &val);
1168 if (val & BIT(offset % 32))
1169 return GPIO_LINE_DIRECTION_OUT;
1171 return GPIO_LINE_DIRECTION_IN;
1174 static int ocelot_gpio_direction_input(struct gpio_chip *chip,
1175 unsigned int offset)
1177 return pinctrl_gpio_direction_input(chip->base + offset);
1180 static int ocelot_gpio_direction_output(struct gpio_chip *chip,
1181 unsigned int offset, int value)
1183 struct ocelot_pinctrl *info = gpiochip_get_data(chip);
1184 unsigned int pin = BIT(offset % 32);
1187 regmap_write(info->map, REG(OCELOT_GPIO_OUT_SET, info, offset),
1190 regmap_write(info->map, REG(OCELOT_GPIO_OUT_CLR, info, offset),
1193 return pinctrl_gpio_direction_output(chip->base + offset);
1196 static const struct gpio_chip ocelot_gpiolib_chip = {
1197 .request = gpiochip_generic_request,
1198 .free = gpiochip_generic_free,
1199 .set = ocelot_gpio_set,
1200 .get = ocelot_gpio_get,
1201 .get_direction = ocelot_gpio_get_direction,
1202 .direction_input = ocelot_gpio_direction_input,
1203 .direction_output = ocelot_gpio_direction_output,
1204 .owner = THIS_MODULE,
1207 static void ocelot_irq_mask(struct irq_data *data)
1209 struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
1210 struct ocelot_pinctrl *info = gpiochip_get_data(chip);
1211 unsigned int gpio = irqd_to_hwirq(data);
1213 regmap_update_bits(info->map, REG(OCELOT_GPIO_INTR_ENA, info, gpio),
1217 static void ocelot_irq_unmask(struct irq_data *data)
1219 struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
1220 struct ocelot_pinctrl *info = gpiochip_get_data(chip);
1221 unsigned int gpio = irqd_to_hwirq(data);
1223 regmap_update_bits(info->map, REG(OCELOT_GPIO_INTR_ENA, info, gpio),
1224 BIT(gpio % 32), BIT(gpio % 32));
1227 static void ocelot_irq_ack(struct irq_data *data)
1229 struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
1230 struct ocelot_pinctrl *info = gpiochip_get_data(chip);
1231 unsigned int gpio = irqd_to_hwirq(data);
1233 regmap_write_bits(info->map, REG(OCELOT_GPIO_INTR, info, gpio),
1234 BIT(gpio % 32), BIT(gpio % 32));
1237 static int ocelot_irq_set_type(struct irq_data *data, unsigned int type);
1239 static struct irq_chip ocelot_eoi_irqchip = {
1241 .irq_mask = ocelot_irq_mask,
1242 .irq_eoi = ocelot_irq_ack,
1243 .irq_unmask = ocelot_irq_unmask,
1244 .flags = IRQCHIP_EOI_THREADED | IRQCHIP_EOI_IF_HANDLED,
1245 .irq_set_type = ocelot_irq_set_type,
1248 static struct irq_chip ocelot_irqchip = {
1250 .irq_mask = ocelot_irq_mask,
1251 .irq_ack = ocelot_irq_ack,
1252 .irq_unmask = ocelot_irq_unmask,
1253 .irq_set_type = ocelot_irq_set_type,
1256 static int ocelot_irq_set_type(struct irq_data *data, unsigned int type)
1258 type &= IRQ_TYPE_SENSE_MASK;
1260 if (!(type & (IRQ_TYPE_EDGE_BOTH | IRQ_TYPE_LEVEL_HIGH)))
1263 if (type & IRQ_TYPE_LEVEL_HIGH)
1264 irq_set_chip_handler_name_locked(data, &ocelot_eoi_irqchip,
1265 handle_fasteoi_irq, NULL);
1266 if (type & IRQ_TYPE_EDGE_BOTH)
1267 irq_set_chip_handler_name_locked(data, &ocelot_irqchip,
1268 handle_edge_irq, NULL);
1273 static void ocelot_irq_handler(struct irq_desc *desc)
1275 struct irq_chip *parent_chip = irq_desc_get_chip(desc);
1276 struct gpio_chip *chip = irq_desc_get_handler_data(desc);
1277 struct ocelot_pinctrl *info = gpiochip_get_data(chip);
1278 unsigned int id_reg = OCELOT_GPIO_INTR_IDENT * info->stride;
1279 unsigned int reg = 0, irq, i;
1282 for (i = 0; i < info->stride; i++) {
1283 regmap_read(info->map, id_reg + 4 * i, ®);
1287 chained_irq_enter(parent_chip, desc);
1291 for_each_set_bit(irq, &irqs,
1292 min(32U, info->desc->npins - 32 * i))
1293 generic_handle_irq(irq_linear_revmap(chip->irq.domain,
1296 chained_irq_exit(parent_chip, desc);
1300 static int ocelot_gpiochip_register(struct platform_device *pdev,
1301 struct ocelot_pinctrl *info)
1303 struct gpio_chip *gc;
1304 struct gpio_irq_chip *girq;
1307 info->gpio_chip = ocelot_gpiolib_chip;
1309 gc = &info->gpio_chip;
1310 gc->ngpio = info->desc->npins;
1311 gc->parent = &pdev->dev;
1313 gc->of_node = info->dev->of_node;
1314 gc->label = "ocelot-gpio";
1316 irq = irq_of_parse_and_map(gc->of_node, 0);
1319 girq->chip = &ocelot_irqchip;
1320 girq->parent_handler = ocelot_irq_handler;
1321 girq->num_parents = 1;
1322 girq->parents = devm_kcalloc(&pdev->dev, 1,
1323 sizeof(*girq->parents),
1327 girq->parents[0] = irq;
1328 girq->default_type = IRQ_TYPE_NONE;
1329 girq->handler = handle_edge_irq;
1332 return devm_gpiochip_add_data(&pdev->dev, gc, info);
1335 static const struct of_device_id ocelot_pinctrl_of_match[] = {
1336 { .compatible = "mscc,luton-pinctrl", .data = &luton_desc },
1337 { .compatible = "mscc,serval-pinctrl", .data = &serval_desc },
1338 { .compatible = "mscc,ocelot-pinctrl", .data = &ocelot_desc },
1339 { .compatible = "mscc,jaguar2-pinctrl", .data = &jaguar2_desc },
1340 { .compatible = "microchip,sparx5-pinctrl", .data = &sparx5_desc },
1344 static int ocelot_pinctrl_probe(struct platform_device *pdev)
1346 struct device *dev = &pdev->dev;
1347 struct ocelot_pinctrl *info;
1349 struct resource *res;
1351 struct regmap_config regmap_config = {
1357 info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
1361 info->desc = (struct pinctrl_desc *)device_get_match_data(dev);
1363 base = devm_ioremap_resource(dev,
1364 platform_get_resource(pdev, IORESOURCE_MEM, 0));
1366 dev_err(dev, "Failed to ioremap registers\n");
1367 return PTR_ERR(base);
1370 info->stride = 1 + (info->desc->npins - 1) / 32;
1372 regmap_config.max_register = OCELOT_GPIO_SD_MAP * info->stride + 15 * 4;
1374 info->map = devm_regmap_init_mmio(dev, base, ®map_config);
1375 if (IS_ERR(info->map)) {
1376 dev_err(dev, "Failed to create regmap\n");
1377 return PTR_ERR(info->map);
1379 dev_set_drvdata(dev, info->map);
1382 /* Pinconf registers */
1383 if (info->desc->confops) {
1384 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1385 base = devm_ioremap_resource(dev, res);
1387 dev_dbg(dev, "Failed to ioremap config registers (no extended pinconf)\n");
1389 info->pincfg = base;
1392 ret = ocelot_pinctrl_register(pdev, info);
1396 ret = ocelot_gpiochip_register(pdev, info);
1400 dev_info(dev, "driver registered\n");
1405 static struct platform_driver ocelot_pinctrl_driver = {
1407 .name = "pinctrl-ocelot",
1408 .of_match_table = of_match_ptr(ocelot_pinctrl_of_match),
1409 .suppress_bind_attrs = true,
1411 .probe = ocelot_pinctrl_probe,
1413 builtin_platform_driver(ocelot_pinctrl_driver);