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_PINS 22
41 #define OCELOT_FUNC_PER_PIN 4
75 static const char *const ocelot_function_names
[] = {
78 [FUNC_IRQ0_IN
] = "irq0_in",
79 [FUNC_IRQ0_OUT
] = "irq0_out",
80 [FUNC_IRQ1_IN
] = "irq1_in",
81 [FUNC_IRQ1_OUT
] = "irq1_out",
82 [FUNC_MIIM1
] = "miim1",
83 [FUNC_PCI_WAKE
] = "pci_wake",
89 [FUNC_RECO_CLK0
] = "reco_clk0",
90 [FUNC_RECO_CLK1
] = "reco_clk1",
99 [FUNC_TACHO
] = "tacho",
101 [FUNC_TWI_SCL_M
] = "twi_scl_m",
102 [FUNC_UART
] = "uart",
103 [FUNC_UART2
] = "uart2",
106 struct ocelot_pmx_func
{
108 unsigned int ngroups
;
111 struct ocelot_pin_caps
{
113 unsigned char functions
[OCELOT_FUNC_PER_PIN
];
116 struct ocelot_pinctrl
{
118 struct pinctrl_dev
*pctl
;
119 struct gpio_chip gpio_chip
;
121 struct ocelot_pmx_func func
[FUNC_MAX
];
124 #define OCELOT_P(p, f0, f1, f2) \
125 static struct ocelot_pin_caps ocelot_pin_##p = { \
128 FUNC_GPIO, FUNC_##f0, FUNC_##f1, FUNC_##f2, \
132 OCELOT_P(0, SG0
, NONE
, NONE
);
133 OCELOT_P(1, SG0
, NONE
, NONE
);
134 OCELOT_P(2, SG0
, NONE
, NONE
);
135 OCELOT_P(3, SG0
, NONE
, NONE
);
136 OCELOT_P(4, IRQ0_IN
, IRQ0_OUT
, TWI_SCL_M
);
137 OCELOT_P(5, IRQ1_IN
, IRQ1_OUT
, PCI_WAKE
);
138 OCELOT_P(6, UART
, TWI_SCL_M
, NONE
);
139 OCELOT_P(7, UART
, TWI_SCL_M
, NONE
);
140 OCELOT_P(8, SI
, TWI_SCL_M
, IRQ0_OUT
);
141 OCELOT_P(9, SI
, TWI_SCL_M
, IRQ1_OUT
);
142 OCELOT_P(10, PTP2
, TWI_SCL_M
, SFP0
);
143 OCELOT_P(11, PTP3
, TWI_SCL_M
, SFP1
);
144 OCELOT_P(12, UART2
, TWI_SCL_M
, SFP2
);
145 OCELOT_P(13, UART2
, TWI_SCL_M
, SFP3
);
146 OCELOT_P(14, MIIM1
, TWI_SCL_M
, SFP4
);
147 OCELOT_P(15, MIIM1
, TWI_SCL_M
, SFP5
);
148 OCELOT_P(16, TWI
, NONE
, SI
);
149 OCELOT_P(17, TWI
, TWI_SCL_M
, SI
);
150 OCELOT_P(18, PTP0
, TWI_SCL_M
, NONE
);
151 OCELOT_P(19, PTP1
, TWI_SCL_M
, NONE
);
152 OCELOT_P(20, RECO_CLK0
, TACHO
, NONE
);
153 OCELOT_P(21, RECO_CLK1
, PWM
, NONE
);
155 #define OCELOT_PIN(n) { \
158 .drv_data = &ocelot_pin_##n \
161 static const struct pinctrl_pin_desc ocelot_pins
[] = {
186 static int ocelot_get_functions_count(struct pinctrl_dev
*pctldev
)
188 return ARRAY_SIZE(ocelot_function_names
);
191 static const char *ocelot_get_function_name(struct pinctrl_dev
*pctldev
,
192 unsigned int function
)
194 return ocelot_function_names
[function
];
197 static int ocelot_get_function_groups(struct pinctrl_dev
*pctldev
,
198 unsigned int function
,
199 const char *const **groups
,
200 unsigned *const num_groups
)
202 struct ocelot_pinctrl
*info
= pinctrl_dev_get_drvdata(pctldev
);
204 *groups
= info
->func
[function
].groups
;
205 *num_groups
= info
->func
[function
].ngroups
;
210 static int ocelot_pin_function_idx(unsigned int pin
, unsigned int function
)
212 struct ocelot_pin_caps
*p
= ocelot_pins
[pin
].drv_data
;
215 for (i
= 0; i
< OCELOT_FUNC_PER_PIN
; i
++) {
216 if (function
== p
->functions
[i
])
223 static int ocelot_pinmux_set_mux(struct pinctrl_dev
*pctldev
,
224 unsigned int selector
, unsigned int group
)
226 struct ocelot_pinctrl
*info
= pinctrl_dev_get_drvdata(pctldev
);
227 struct ocelot_pin_caps
*pin
= ocelot_pins
[group
].drv_data
;
230 f
= ocelot_pin_function_idx(group
, selector
);
235 * f is encoded on two bits.
236 * bit 0 of f goes in BIT(pin) of ALT0, bit 1 of f goes in BIT(pin) of
238 * This is racy because both registers can't be updated at the same time
239 * but it doesn't matter much for now.
241 regmap_update_bits(info
->map
, OCELOT_GPIO_ALT0
, BIT(pin
->pin
),
243 regmap_update_bits(info
->map
, OCELOT_GPIO_ALT1
, BIT(pin
->pin
),
244 f
<< (pin
->pin
- 1));
249 static int ocelot_gpio_set_direction(struct pinctrl_dev
*pctldev
,
250 struct pinctrl_gpio_range
*range
,
251 unsigned int pin
, bool input
)
253 struct ocelot_pinctrl
*info
= pinctrl_dev_get_drvdata(pctldev
);
255 regmap_update_bits(info
->map
, OCELOT_GPIO_OE
, BIT(pin
),
256 input
? 0 : BIT(pin
));
261 static int ocelot_gpio_request_enable(struct pinctrl_dev
*pctldev
,
262 struct pinctrl_gpio_range
*range
,
265 struct ocelot_pinctrl
*info
= pinctrl_dev_get_drvdata(pctldev
);
267 regmap_update_bits(info
->map
, OCELOT_GPIO_ALT0
, BIT(offset
), 0);
268 regmap_update_bits(info
->map
, OCELOT_GPIO_ALT1
, BIT(offset
), 0);
273 static const struct pinmux_ops ocelot_pmx_ops
= {
274 .get_functions_count
= ocelot_get_functions_count
,
275 .get_function_name
= ocelot_get_function_name
,
276 .get_function_groups
= ocelot_get_function_groups
,
277 .set_mux
= ocelot_pinmux_set_mux
,
278 .gpio_set_direction
= ocelot_gpio_set_direction
,
279 .gpio_request_enable
= ocelot_gpio_request_enable
,
282 static int ocelot_pctl_get_groups_count(struct pinctrl_dev
*pctldev
)
284 return ARRAY_SIZE(ocelot_pins
);
287 static const char *ocelot_pctl_get_group_name(struct pinctrl_dev
*pctldev
,
290 return ocelot_pins
[group
].name
;
293 static int ocelot_pctl_get_group_pins(struct pinctrl_dev
*pctldev
,
295 const unsigned int **pins
,
296 unsigned int *num_pins
)
298 *pins
= &ocelot_pins
[group
].number
;
304 static const struct pinctrl_ops ocelot_pctl_ops
= {
305 .get_groups_count
= ocelot_pctl_get_groups_count
,
306 .get_group_name
= ocelot_pctl_get_group_name
,
307 .get_group_pins
= ocelot_pctl_get_group_pins
,
308 .dt_node_to_map
= pinconf_generic_dt_node_to_map_pin
,
309 .dt_free_map
= pinconf_generic_dt_free_map
,
312 static struct pinctrl_desc ocelot_desc
= {
313 .name
= "ocelot-pinctrl",
315 .npins
= ARRAY_SIZE(ocelot_pins
),
316 .pctlops
= &ocelot_pctl_ops
,
317 .pmxops
= &ocelot_pmx_ops
,
318 .owner
= THIS_MODULE
,
321 static int ocelot_create_group_func_map(struct device
*dev
,
322 struct ocelot_pinctrl
*info
)
324 u16 pins
[ARRAY_SIZE(ocelot_pins
)];
327 for (f
= 0; f
< FUNC_MAX
; f
++) {
328 for (npins
= 0, i
= 0; i
< ARRAY_SIZE(ocelot_pins
); i
++) {
329 if (ocelot_pin_function_idx(i
, f
) >= 0)
333 info
->func
[f
].ngroups
= npins
;
334 info
->func
[f
].groups
= devm_kcalloc(dev
,
338 if (!info
->func
[f
].groups
)
341 for (i
= 0; i
< npins
; i
++)
342 info
->func
[f
].groups
[i
] = ocelot_pins
[pins
[i
]].name
;
348 static int ocelot_pinctrl_register(struct platform_device
*pdev
,
349 struct ocelot_pinctrl
*info
)
353 ret
= ocelot_create_group_func_map(&pdev
->dev
, info
);
355 dev_err(&pdev
->dev
, "Unable to create group func map.\n");
359 info
->pctl
= devm_pinctrl_register(&pdev
->dev
, &ocelot_desc
, info
);
360 if (IS_ERR(info
->pctl
)) {
361 dev_err(&pdev
->dev
, "Failed to register pinctrl\n");
362 return PTR_ERR(info
->pctl
);
368 static int ocelot_gpio_get(struct gpio_chip
*chip
, unsigned int offset
)
370 struct ocelot_pinctrl
*info
= gpiochip_get_data(chip
);
373 regmap_read(info
->map
, OCELOT_GPIO_IN
, &val
);
375 return !!(val
& BIT(offset
));
378 static void ocelot_gpio_set(struct gpio_chip
*chip
, unsigned int offset
,
381 struct ocelot_pinctrl
*info
= gpiochip_get_data(chip
);
384 regmap_write(info
->map
, OCELOT_GPIO_OUT_SET
, BIT(offset
));
386 regmap_write(info
->map
, OCELOT_GPIO_OUT_CLR
, BIT(offset
));
389 static int ocelot_gpio_get_direction(struct gpio_chip
*chip
,
392 struct ocelot_pinctrl
*info
= gpiochip_get_data(chip
);
395 regmap_read(info
->map
, OCELOT_GPIO_OE
, &val
);
397 return !(val
& BIT(offset
));
400 static int ocelot_gpio_direction_input(struct gpio_chip
*chip
,
403 return pinctrl_gpio_direction_input(chip
->base
+ offset
);
406 static int ocelot_gpio_direction_output(struct gpio_chip
*chip
,
407 unsigned int offset
, int value
)
409 struct ocelot_pinctrl
*info
= gpiochip_get_data(chip
);
410 unsigned int pin
= BIT(offset
);
413 regmap_write(info
->map
, OCELOT_GPIO_OUT_SET
, pin
);
415 regmap_write(info
->map
, OCELOT_GPIO_OUT_CLR
, pin
);
417 return pinctrl_gpio_direction_output(chip
->base
+ offset
);
420 static const struct gpio_chip ocelot_gpiolib_chip
= {
421 .request
= gpiochip_generic_request
,
422 .free
= gpiochip_generic_free
,
423 .set
= ocelot_gpio_set
,
424 .get
= ocelot_gpio_get
,
425 .get_direction
= ocelot_gpio_get_direction
,
426 .direction_input
= ocelot_gpio_direction_input
,
427 .direction_output
= ocelot_gpio_direction_output
,
428 .owner
= THIS_MODULE
,
431 static void ocelot_irq_mask(struct irq_data
*data
)
433 struct gpio_chip
*chip
= irq_data_get_irq_chip_data(data
);
434 struct ocelot_pinctrl
*info
= gpiochip_get_data(chip
);
435 unsigned int gpio
= irqd_to_hwirq(data
);
437 regmap_update_bits(info
->map
, OCELOT_GPIO_INTR_ENA
, BIT(gpio
), 0);
440 static void ocelot_irq_unmask(struct irq_data
*data
)
442 struct gpio_chip
*chip
= irq_data_get_irq_chip_data(data
);
443 struct ocelot_pinctrl
*info
= gpiochip_get_data(chip
);
444 unsigned int gpio
= irqd_to_hwirq(data
);
446 regmap_update_bits(info
->map
, OCELOT_GPIO_INTR_ENA
, BIT(gpio
),
450 static void ocelot_irq_ack(struct irq_data
*data
)
452 struct gpio_chip
*chip
= irq_data_get_irq_chip_data(data
);
453 struct ocelot_pinctrl
*info
= gpiochip_get_data(chip
);
454 unsigned int gpio
= irqd_to_hwirq(data
);
456 regmap_write_bits(info
->map
, OCELOT_GPIO_INTR
, BIT(gpio
), BIT(gpio
));
459 static int ocelot_irq_set_type(struct irq_data
*data
, unsigned int type
);
461 static struct irq_chip ocelot_eoi_irqchip
= {
463 .irq_mask
= ocelot_irq_mask
,
464 .irq_eoi
= ocelot_irq_ack
,
465 .irq_unmask
= ocelot_irq_unmask
,
466 .flags
= IRQCHIP_EOI_THREADED
| IRQCHIP_EOI_IF_HANDLED
,
467 .irq_set_type
= ocelot_irq_set_type
,
470 static struct irq_chip ocelot_irqchip
= {
472 .irq_mask
= ocelot_irq_mask
,
473 .irq_ack
= ocelot_irq_ack
,
474 .irq_unmask
= ocelot_irq_unmask
,
475 .irq_set_type
= ocelot_irq_set_type
,
478 static int ocelot_irq_set_type(struct irq_data
*data
, unsigned int type
)
480 type
&= IRQ_TYPE_SENSE_MASK
;
482 if (!(type
& (IRQ_TYPE_EDGE_BOTH
| IRQ_TYPE_LEVEL_HIGH
)))
485 if (type
& IRQ_TYPE_LEVEL_HIGH
)
486 irq_set_chip_handler_name_locked(data
, &ocelot_eoi_irqchip
,
487 handle_fasteoi_irq
, NULL
);
488 if (type
& IRQ_TYPE_EDGE_BOTH
)
489 irq_set_chip_handler_name_locked(data
, &ocelot_irqchip
,
490 handle_edge_irq
, NULL
);
495 static void ocelot_irq_handler(struct irq_desc
*desc
)
497 struct irq_chip
*parent_chip
= irq_desc_get_chip(desc
);
498 struct gpio_chip
*chip
= irq_desc_get_handler_data(desc
);
499 struct ocelot_pinctrl
*info
= gpiochip_get_data(chip
);
500 unsigned int reg
= 0, irq
;
503 regmap_read(info
->map
, OCELOT_GPIO_INTR_IDENT
, ®
);
507 chained_irq_enter(parent_chip
, desc
);
511 for_each_set_bit(irq
, &irqs
, OCELOT_PINS
) {
512 generic_handle_irq(irq_linear_revmap(chip
->irq
.domain
, irq
));
515 chained_irq_exit(parent_chip
, desc
);
518 static int ocelot_gpiochip_register(struct platform_device
*pdev
,
519 struct ocelot_pinctrl
*info
)
521 struct gpio_chip
*gc
;
524 info
->gpio_chip
= ocelot_gpiolib_chip
;
526 gc
= &info
->gpio_chip
;
527 gc
->ngpio
= OCELOT_PINS
;
528 gc
->parent
= &pdev
->dev
;
530 gc
->of_node
= info
->dev
->of_node
;
531 gc
->label
= "ocelot-gpio";
533 ret
= devm_gpiochip_add_data(&pdev
->dev
, gc
, info
);
537 irq
= irq_of_parse_and_map(pdev
->dev
.of_node
, 0);
541 ret
= gpiochip_irqchip_add(gc
, &ocelot_irqchip
, 0, handle_edge_irq
,
546 gpiochip_set_chained_irqchip(gc
, &ocelot_irqchip
, irq
,
552 static const struct regmap_config ocelot_pinctrl_regmap_config
= {
556 .max_register
= 0x64,
559 static const struct of_device_id ocelot_pinctrl_of_match
[] = {
560 { .compatible
= "mscc,ocelot-pinctrl" },
564 static int ocelot_pinctrl_probe(struct platform_device
*pdev
)
566 struct device
*dev
= &pdev
->dev
;
567 struct ocelot_pinctrl
*info
;
571 info
= devm_kzalloc(dev
, sizeof(*info
), GFP_KERNEL
);
575 base
= devm_ioremap_resource(dev
,
576 platform_get_resource(pdev
, IORESOURCE_MEM
, 0));
578 dev_err(dev
, "Failed to ioremap registers\n");
579 return PTR_ERR(base
);
582 info
->map
= devm_regmap_init_mmio(dev
, base
,
583 &ocelot_pinctrl_regmap_config
);
584 if (IS_ERR(info
->map
)) {
585 dev_err(dev
, "Failed to create regmap\n");
586 return PTR_ERR(info
->map
);
588 dev_set_drvdata(dev
, info
->map
);
591 ret
= ocelot_pinctrl_register(pdev
, info
);
595 ret
= ocelot_gpiochip_register(pdev
, info
);
602 static struct platform_driver ocelot_pinctrl_driver
= {
604 .name
= "pinctrl-ocelot",
605 .of_match_table
= of_match_ptr(ocelot_pinctrl_of_match
),
606 .suppress_bind_attrs
= true,
608 .probe
= ocelot_pinctrl_probe
,
610 builtin_platform_driver(ocelot_pinctrl_driver
);