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_GPIO_OUT_SET 0x0
29 #define OCELOT_GPIO_OUT_CLR 0x4
30 #define OCELOT_GPIO_OUT 0x8
31 #define OCELOT_GPIO_IN 0xc
32 #define OCELOT_GPIO_OE 0x10
33 #define OCELOT_GPIO_INTR 0x14
34 #define OCELOT_GPIO_INTR_ENA 0x18
35 #define OCELOT_GPIO_INTR_IDENT 0x1c
36 #define OCELOT_GPIO_ALT0 0x20
37 #define OCELOT_GPIO_ALT1 0x24
38 #define OCELOT_GPIO_SD_MAP 0x28
40 #define OCELOT_FUNC_PER_PIN 4
71 static const char *const ocelot_function_names[] = {
74 [FUNC_IRQ0_IN] = "irq0_in",
75 [FUNC_IRQ0_OUT] = "irq0_out",
76 [FUNC_IRQ1_IN] = "irq1_in",
77 [FUNC_IRQ1_OUT] = "irq1_out",
79 [FUNC_PCI_WAKE] = "pci_wake",
85 [FUNC_RECO_CLK] = "reco_clk",
91 [FUNC_TACHO] = "tacho",
94 [FUNC_TWI_SCL_M] = "twi_scl_m",
96 [FUNC_UART2] = "uart2",
99 struct ocelot_pmx_func {
101 unsigned int ngroups;
104 struct ocelot_pin_caps {
106 unsigned char functions[OCELOT_FUNC_PER_PIN];
109 struct ocelot_pinctrl {
111 struct pinctrl_dev *pctl;
112 struct gpio_chip gpio_chip;
114 struct pinctrl_desc *desc;
115 struct ocelot_pmx_func func[FUNC_MAX];
119 #define OCELOT_P(p, f0, f1, f2) \
120 static struct ocelot_pin_caps ocelot_pin_##p = { \
123 FUNC_GPIO, FUNC_##f0, FUNC_##f1, FUNC_##f2, \
127 OCELOT_P(0, SG0, NONE, NONE);
128 OCELOT_P(1, SG0, NONE, NONE);
129 OCELOT_P(2, SG0, NONE, NONE);
130 OCELOT_P(3, SG0, NONE, NONE);
131 OCELOT_P(4, IRQ0_IN, IRQ0_OUT, TWI_SCL_M);
132 OCELOT_P(5, IRQ1_IN, IRQ1_OUT, PCI_WAKE);
133 OCELOT_P(6, UART, TWI_SCL_M, NONE);
134 OCELOT_P(7, UART, TWI_SCL_M, NONE);
135 OCELOT_P(8, SI, TWI_SCL_M, IRQ0_OUT);
136 OCELOT_P(9, SI, TWI_SCL_M, IRQ1_OUT);
137 OCELOT_P(10, PTP2, TWI_SCL_M, SFP);
138 OCELOT_P(11, PTP3, TWI_SCL_M, SFP);
139 OCELOT_P(12, UART2, TWI_SCL_M, SFP);
140 OCELOT_P(13, UART2, TWI_SCL_M, SFP);
141 OCELOT_P(14, MIIM, TWI_SCL_M, SFP);
142 OCELOT_P(15, MIIM, TWI_SCL_M, SFP);
143 OCELOT_P(16, TWI, NONE, SI);
144 OCELOT_P(17, TWI, TWI_SCL_M, SI);
145 OCELOT_P(18, PTP0, TWI_SCL_M, NONE);
146 OCELOT_P(19, PTP1, TWI_SCL_M, NONE);
147 OCELOT_P(20, RECO_CLK, TACHO, TWI_SCL_M);
148 OCELOT_P(21, RECO_CLK, PWM, TWI_SCL_M);
150 #define OCELOT_PIN(n) { \
153 .drv_data = &ocelot_pin_##n \
156 static const struct pinctrl_pin_desc ocelot_pins[] = {
181 #define JAGUAR2_P(p, f0, f1) \
182 static struct ocelot_pin_caps jaguar2_pin_##p = { \
185 FUNC_GPIO, FUNC_##f0, FUNC_##f1, FUNC_NONE \
189 JAGUAR2_P(0, SG0, NONE);
190 JAGUAR2_P(1, SG0, NONE);
191 JAGUAR2_P(2, SG0, NONE);
192 JAGUAR2_P(3, SG0, NONE);
193 JAGUAR2_P(4, SG1, NONE);
194 JAGUAR2_P(5, SG1, NONE);
195 JAGUAR2_P(6, IRQ0_IN, IRQ0_OUT);
196 JAGUAR2_P(7, IRQ1_IN, IRQ1_OUT);
197 JAGUAR2_P(8, PTP0, NONE);
198 JAGUAR2_P(9, PTP1, NONE);
199 JAGUAR2_P(10, UART, NONE);
200 JAGUAR2_P(11, UART, NONE);
201 JAGUAR2_P(12, SG1, NONE);
202 JAGUAR2_P(13, SG1, NONE);
203 JAGUAR2_P(14, TWI, TWI_SCL_M);
204 JAGUAR2_P(15, TWI, NONE);
205 JAGUAR2_P(16, SI, TWI_SCL_M);
206 JAGUAR2_P(17, SI, TWI_SCL_M);
207 JAGUAR2_P(18, SI, TWI_SCL_M);
208 JAGUAR2_P(19, PCI_WAKE, NONE);
209 JAGUAR2_P(20, IRQ0_OUT, TWI_SCL_M);
210 JAGUAR2_P(21, IRQ1_OUT, TWI_SCL_M);
211 JAGUAR2_P(22, TACHO, NONE);
212 JAGUAR2_P(23, PWM, NONE);
213 JAGUAR2_P(24, UART2, NONE);
214 JAGUAR2_P(25, UART2, SI);
215 JAGUAR2_P(26, PTP2, SI);
216 JAGUAR2_P(27, PTP3, SI);
217 JAGUAR2_P(28, TWI2, SI);
218 JAGUAR2_P(29, TWI2, SI);
219 JAGUAR2_P(30, SG2, SI);
220 JAGUAR2_P(31, SG2, SI);
221 JAGUAR2_P(32, SG2, SI);
222 JAGUAR2_P(33, SG2, SI);
223 JAGUAR2_P(34, NONE, TWI_SCL_M);
224 JAGUAR2_P(35, NONE, TWI_SCL_M);
225 JAGUAR2_P(36, NONE, TWI_SCL_M);
226 JAGUAR2_P(37, NONE, TWI_SCL_M);
227 JAGUAR2_P(38, NONE, TWI_SCL_M);
228 JAGUAR2_P(39, NONE, TWI_SCL_M);
229 JAGUAR2_P(40, NONE, TWI_SCL_M);
230 JAGUAR2_P(41, NONE, TWI_SCL_M);
231 JAGUAR2_P(42, NONE, TWI_SCL_M);
232 JAGUAR2_P(43, NONE, TWI_SCL_M);
233 JAGUAR2_P(44, NONE, SFP);
234 JAGUAR2_P(45, NONE, SFP);
235 JAGUAR2_P(46, NONE, SFP);
236 JAGUAR2_P(47, NONE, SFP);
237 JAGUAR2_P(48, SFP, NONE);
238 JAGUAR2_P(49, SFP, SI);
239 JAGUAR2_P(50, SFP, SI);
240 JAGUAR2_P(51, SFP, SI);
241 JAGUAR2_P(52, SFP, NONE);
242 JAGUAR2_P(53, SFP, NONE);
243 JAGUAR2_P(54, SFP, NONE);
244 JAGUAR2_P(55, SFP, NONE);
245 JAGUAR2_P(56, MIIM, SFP);
246 JAGUAR2_P(57, MIIM, SFP);
247 JAGUAR2_P(58, MIIM, SFP);
248 JAGUAR2_P(59, MIIM, SFP);
249 JAGUAR2_P(60, NONE, NONE);
250 JAGUAR2_P(61, NONE, NONE);
251 JAGUAR2_P(62, NONE, NONE);
252 JAGUAR2_P(63, NONE, NONE);
254 #define JAGUAR2_PIN(n) { \
257 .drv_data = &jaguar2_pin_##n \
260 static const struct pinctrl_pin_desc jaguar2_pins[] = {
327 static int ocelot_get_functions_count(struct pinctrl_dev *pctldev)
329 return ARRAY_SIZE(ocelot_function_names);
332 static const char *ocelot_get_function_name(struct pinctrl_dev *pctldev,
333 unsigned int function)
335 return ocelot_function_names[function];
338 static int ocelot_get_function_groups(struct pinctrl_dev *pctldev,
339 unsigned int function,
340 const char *const **groups,
341 unsigned *const num_groups)
343 struct ocelot_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
345 *groups = info->func[function].groups;
346 *num_groups = info->func[function].ngroups;
351 static int ocelot_pin_function_idx(struct ocelot_pinctrl *info,
352 unsigned int pin, unsigned int function)
354 struct ocelot_pin_caps *p = info->desc->pins[pin].drv_data;
357 for (i = 0; i < OCELOT_FUNC_PER_PIN; i++) {
358 if (function == p->functions[i])
365 #define REG_ALT(msb, info, p) (OCELOT_GPIO_ALT0 * (info)->stride + 4 * ((msb) + ((info)->stride * ((p) / 32))))
367 static int ocelot_pinmux_set_mux(struct pinctrl_dev *pctldev,
368 unsigned int selector, unsigned int group)
370 struct ocelot_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
371 struct ocelot_pin_caps *pin = info->desc->pins[group].drv_data;
372 unsigned int p = pin->pin % 32;
375 f = ocelot_pin_function_idx(info, group, selector);
380 * f is encoded on two bits.
381 * bit 0 of f goes in BIT(pin) of ALT[0], bit 1 of f goes in BIT(pin) of
383 * This is racy because both registers can't be updated at the same time
384 * but it doesn't matter much for now.
386 regmap_update_bits(info->map, REG_ALT(0, info, pin->pin),
388 regmap_update_bits(info->map, REG_ALT(1, info, pin->pin),
389 BIT(p), f << (p - 1));
394 #define REG(r, info, p) ((r) * (info)->stride + (4 * ((p) / 32)))
396 static int ocelot_gpio_set_direction(struct pinctrl_dev *pctldev,
397 struct pinctrl_gpio_range *range,
398 unsigned int pin, bool input)
400 struct ocelot_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
401 unsigned int p = pin % 32;
403 regmap_update_bits(info->map, REG(OCELOT_GPIO_OE, info, pin), BIT(p),
409 static int ocelot_gpio_request_enable(struct pinctrl_dev *pctldev,
410 struct pinctrl_gpio_range *range,
413 struct ocelot_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
414 unsigned int p = offset % 32;
416 regmap_update_bits(info->map, REG_ALT(0, info, offset),
418 regmap_update_bits(info->map, REG_ALT(1, info, offset),
424 static const struct pinmux_ops ocelot_pmx_ops = {
425 .get_functions_count = ocelot_get_functions_count,
426 .get_function_name = ocelot_get_function_name,
427 .get_function_groups = ocelot_get_function_groups,
428 .set_mux = ocelot_pinmux_set_mux,
429 .gpio_set_direction = ocelot_gpio_set_direction,
430 .gpio_request_enable = ocelot_gpio_request_enable,
433 static int ocelot_pctl_get_groups_count(struct pinctrl_dev *pctldev)
435 struct ocelot_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
437 return info->desc->npins;
440 static const char *ocelot_pctl_get_group_name(struct pinctrl_dev *pctldev,
443 struct ocelot_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
445 return info->desc->pins[group].name;
448 static int ocelot_pctl_get_group_pins(struct pinctrl_dev *pctldev,
450 const unsigned int **pins,
451 unsigned int *num_pins)
453 struct ocelot_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
455 *pins = &info->desc->pins[group].number;
461 static const struct pinctrl_ops ocelot_pctl_ops = {
462 .get_groups_count = ocelot_pctl_get_groups_count,
463 .get_group_name = ocelot_pctl_get_group_name,
464 .get_group_pins = ocelot_pctl_get_group_pins,
465 .dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
466 .dt_free_map = pinconf_generic_dt_free_map,
469 static struct pinctrl_desc ocelot_desc = {
470 .name = "ocelot-pinctrl",
472 .npins = ARRAY_SIZE(ocelot_pins),
473 .pctlops = &ocelot_pctl_ops,
474 .pmxops = &ocelot_pmx_ops,
475 .owner = THIS_MODULE,
478 static struct pinctrl_desc jaguar2_desc = {
479 .name = "jaguar2-pinctrl",
480 .pins = jaguar2_pins,
481 .npins = ARRAY_SIZE(jaguar2_pins),
482 .pctlops = &ocelot_pctl_ops,
483 .pmxops = &ocelot_pmx_ops,
484 .owner = THIS_MODULE,
487 static int ocelot_create_group_func_map(struct device *dev,
488 struct ocelot_pinctrl *info)
491 u8 *pins = kcalloc(info->desc->npins, sizeof(u8), GFP_KERNEL);
496 for (f = 0; f < FUNC_MAX; f++) {
497 for (npins = 0, i = 0; i < info->desc->npins; i++) {
498 if (ocelot_pin_function_idx(info, i, f) >= 0)
505 info->func[f].ngroups = npins;
506 info->func[f].groups = devm_kcalloc(dev, npins, sizeof(char *),
508 if (!info->func[f].groups) {
513 for (i = 0; i < npins; i++)
514 info->func[f].groups[i] = info->desc->pins[pins[i]].name;
522 static int ocelot_pinctrl_register(struct platform_device *pdev,
523 struct ocelot_pinctrl *info)
527 ret = ocelot_create_group_func_map(&pdev->dev, info);
529 dev_err(&pdev->dev, "Unable to create group func map.\n");
533 info->pctl = devm_pinctrl_register(&pdev->dev, info->desc, info);
534 if (IS_ERR(info->pctl)) {
535 dev_err(&pdev->dev, "Failed to register pinctrl\n");
536 return PTR_ERR(info->pctl);
542 static int ocelot_gpio_get(struct gpio_chip *chip, unsigned int offset)
544 struct ocelot_pinctrl *info = gpiochip_get_data(chip);
547 regmap_read(info->map, REG(OCELOT_GPIO_IN, info, offset), &val);
549 return !!(val & BIT(offset % 32));
552 static void ocelot_gpio_set(struct gpio_chip *chip, unsigned int offset,
555 struct ocelot_pinctrl *info = gpiochip_get_data(chip);
558 regmap_write(info->map, REG(OCELOT_GPIO_OUT_SET, info, offset),
561 regmap_write(info->map, REG(OCELOT_GPIO_OUT_CLR, info, offset),
565 static int ocelot_gpio_get_direction(struct gpio_chip *chip,
568 struct ocelot_pinctrl *info = gpiochip_get_data(chip);
571 regmap_read(info->map, REG(OCELOT_GPIO_OE, info, offset), &val);
573 if (val & BIT(offset % 32))
574 return GPIO_LINE_DIRECTION_OUT;
576 return GPIO_LINE_DIRECTION_IN;
579 static int ocelot_gpio_direction_input(struct gpio_chip *chip,
582 return pinctrl_gpio_direction_input(chip->base + offset);
585 static int ocelot_gpio_direction_output(struct gpio_chip *chip,
586 unsigned int offset, int value)
588 struct ocelot_pinctrl *info = gpiochip_get_data(chip);
589 unsigned int pin = BIT(offset % 32);
592 regmap_write(info->map, REG(OCELOT_GPIO_OUT_SET, info, offset),
595 regmap_write(info->map, REG(OCELOT_GPIO_OUT_CLR, info, offset),
598 return pinctrl_gpio_direction_output(chip->base + offset);
601 static const struct gpio_chip ocelot_gpiolib_chip = {
602 .request = gpiochip_generic_request,
603 .free = gpiochip_generic_free,
604 .set = ocelot_gpio_set,
605 .get = ocelot_gpio_get,
606 .get_direction = ocelot_gpio_get_direction,
607 .direction_input = ocelot_gpio_direction_input,
608 .direction_output = ocelot_gpio_direction_output,
609 .owner = THIS_MODULE,
612 static void ocelot_irq_mask(struct irq_data *data)
614 struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
615 struct ocelot_pinctrl *info = gpiochip_get_data(chip);
616 unsigned int gpio = irqd_to_hwirq(data);
618 regmap_update_bits(info->map, REG(OCELOT_GPIO_INTR_ENA, info, gpio),
622 static void ocelot_irq_unmask(struct irq_data *data)
624 struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
625 struct ocelot_pinctrl *info = gpiochip_get_data(chip);
626 unsigned int gpio = irqd_to_hwirq(data);
628 regmap_update_bits(info->map, REG(OCELOT_GPIO_INTR_ENA, info, gpio),
629 BIT(gpio % 32), BIT(gpio % 32));
632 static void ocelot_irq_ack(struct irq_data *data)
634 struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
635 struct ocelot_pinctrl *info = gpiochip_get_data(chip);
636 unsigned int gpio = irqd_to_hwirq(data);
638 regmap_write_bits(info->map, REG(OCELOT_GPIO_INTR, info, gpio),
639 BIT(gpio % 32), BIT(gpio % 32));
642 static int ocelot_irq_set_type(struct irq_data *data, unsigned int type);
644 static struct irq_chip ocelot_eoi_irqchip = {
646 .irq_mask = ocelot_irq_mask,
647 .irq_eoi = ocelot_irq_ack,
648 .irq_unmask = ocelot_irq_unmask,
649 .flags = IRQCHIP_EOI_THREADED | IRQCHIP_EOI_IF_HANDLED,
650 .irq_set_type = ocelot_irq_set_type,
653 static struct irq_chip ocelot_irqchip = {
655 .irq_mask = ocelot_irq_mask,
656 .irq_ack = ocelot_irq_ack,
657 .irq_unmask = ocelot_irq_unmask,
658 .irq_set_type = ocelot_irq_set_type,
661 static int ocelot_irq_set_type(struct irq_data *data, unsigned int type)
663 type &= IRQ_TYPE_SENSE_MASK;
665 if (!(type & (IRQ_TYPE_EDGE_BOTH | IRQ_TYPE_LEVEL_HIGH)))
668 if (type & IRQ_TYPE_LEVEL_HIGH)
669 irq_set_chip_handler_name_locked(data, &ocelot_eoi_irqchip,
670 handle_fasteoi_irq, NULL);
671 if (type & IRQ_TYPE_EDGE_BOTH)
672 irq_set_chip_handler_name_locked(data, &ocelot_irqchip,
673 handle_edge_irq, NULL);
678 static void ocelot_irq_handler(struct irq_desc *desc)
680 struct irq_chip *parent_chip = irq_desc_get_chip(desc);
681 struct gpio_chip *chip = irq_desc_get_handler_data(desc);
682 struct ocelot_pinctrl *info = gpiochip_get_data(chip);
683 unsigned int id_reg = OCELOT_GPIO_INTR_IDENT * info->stride;
684 unsigned int reg = 0, irq, i;
687 for (i = 0; i < info->stride; i++) {
688 regmap_read(info->map, id_reg + 4 * i, ®);
692 chained_irq_enter(parent_chip, desc);
696 for_each_set_bit(irq, &irqs,
697 min(32U, info->desc->npins - 32 * i))
698 generic_handle_irq(irq_linear_revmap(chip->irq.domain,
701 chained_irq_exit(parent_chip, desc);
705 static int ocelot_gpiochip_register(struct platform_device *pdev,
706 struct ocelot_pinctrl *info)
708 struct gpio_chip *gc;
709 struct gpio_irq_chip *girq;
712 info->gpio_chip = ocelot_gpiolib_chip;
714 gc = &info->gpio_chip;
715 gc->ngpio = info->desc->npins;
716 gc->parent = &pdev->dev;
718 gc->of_node = info->dev->of_node;
719 gc->label = "ocelot-gpio";
721 irq = irq_of_parse_and_map(gc->of_node, 0);
724 girq->chip = &ocelot_irqchip;
725 girq->parent_handler = ocelot_irq_handler;
726 girq->num_parents = 1;
727 girq->parents = devm_kcalloc(&pdev->dev, 1,
728 sizeof(*girq->parents),
732 girq->parents[0] = irq;
733 girq->default_type = IRQ_TYPE_NONE;
734 girq->handler = handle_edge_irq;
737 ret = devm_gpiochip_add_data(&pdev->dev, gc, info);
744 static const struct of_device_id ocelot_pinctrl_of_match[] = {
745 { .compatible = "mscc,ocelot-pinctrl", .data = &ocelot_desc },
746 { .compatible = "mscc,jaguar2-pinctrl", .data = &jaguar2_desc },
750 static int ocelot_pinctrl_probe(struct platform_device *pdev)
752 struct device *dev = &pdev->dev;
753 struct ocelot_pinctrl *info;
756 struct regmap_config regmap_config = {
762 info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
766 info->desc = (struct pinctrl_desc *)device_get_match_data(dev);
768 base = devm_ioremap_resource(dev,
769 platform_get_resource(pdev, IORESOURCE_MEM, 0));
771 dev_err(dev, "Failed to ioremap registers\n");
772 return PTR_ERR(base);
775 info->stride = 1 + (info->desc->npins - 1) / 32;
776 regmap_config.max_register = OCELOT_GPIO_SD_MAP * info->stride + 15 * 4;
778 info->map = devm_regmap_init_mmio(dev, base, ®map_config);
779 if (IS_ERR(info->map)) {
780 dev_err(dev, "Failed to create regmap\n");
781 return PTR_ERR(info->map);
783 dev_set_drvdata(dev, info->map);
786 ret = ocelot_pinctrl_register(pdev, info);
790 ret = ocelot_gpiochip_register(pdev, info);
797 static struct platform_driver ocelot_pinctrl_driver = {
799 .name = "pinctrl-ocelot",
800 .of_match_table = of_match_ptr(ocelot_pinctrl_of_match),
801 .suppress_bind_attrs = true,
803 .probe = ocelot_pinctrl_probe,
805 builtin_platform_driver(ocelot_pinctrl_driver);