1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Microsemi/Microchip SoCs serial gpio driver
5 * Author: Lars Povlsen <lars.povlsen@microchip.com>
7 * Copyright (c) 2020 Microchip Technology Inc. and its subsidiaries.
10 #include <linux/bitfield.h>
11 #include <linux/bits.h>
12 #include <linux/clk.h>
13 #include <linux/gpio/driver.h>
15 #include <linux/mod_devicetable.h>
16 #include <linux/module.h>
17 #include <linux/pinctrl/pinmux.h>
18 #include <linux/platform_device.h>
19 #include <linux/property.h>
20 #include <linux/reset.h>
21 #include <linux/spinlock.h>
26 #define SGPIO_BITS_PER_WORD 32
27 #define SGPIO_MAX_BITS 4
28 #define SGPIO_SRC_BITS 3 /* 3 bit wide field per pin */
51 SGPIO_FLAGS_HAS_IRQ = BIT(0),
54 struct sgpio_properties {
60 #define SGPIO_LUTON_AUTO_REPEAT BIT(5)
61 #define SGPIO_LUTON_PORT_WIDTH GENMASK(3, 2)
62 #define SGPIO_LUTON_CLK_FREQ GENMASK(11, 0)
63 #define SGPIO_LUTON_BIT_SOURCE GENMASK(11, 0)
65 #define SGPIO_OCELOT_AUTO_REPEAT BIT(10)
66 #define SGPIO_OCELOT_PORT_WIDTH GENMASK(8, 7)
67 #define SGPIO_OCELOT_CLK_FREQ GENMASK(19, 8)
68 #define SGPIO_OCELOT_BIT_SOURCE GENMASK(23, 12)
70 #define SGPIO_SPARX5_AUTO_REPEAT BIT(6)
71 #define SGPIO_SPARX5_PORT_WIDTH GENMASK(4, 3)
72 #define SGPIO_SPARX5_CLK_FREQ GENMASK(19, 8)
73 #define SGPIO_SPARX5_BIT_SOURCE GENMASK(23, 12)
75 #define SGPIO_MASTER_INTR_ENA BIT(0)
77 #define SGPIO_INT_TRG_LEVEL 0
78 #define SGPIO_INT_TRG_EDGE 1
79 #define SGPIO_INT_TRG_EDGE_FALL 2
80 #define SGPIO_INT_TRG_EDGE_RISE 3
82 #define SGPIO_TRG_LEVEL_HIGH 0
83 #define SGPIO_TRG_LEVEL_LOW 1
85 static const struct sgpio_properties properties_luton = {
86 .arch = SGPIO_ARCH_LUTON,
87 .regoff = { 0x00, 0x09, 0x29, 0x2a, 0x2b },
90 static const struct sgpio_properties properties_ocelot = {
91 .arch = SGPIO_ARCH_OCELOT,
92 .regoff = { 0x00, 0x06, 0x26, 0x04, 0x05 },
95 static const struct sgpio_properties properties_sparx5 = {
96 .arch = SGPIO_ARCH_SPARX5,
97 .flags = SGPIO_FLAGS_HAS_IRQ,
98 .regoff = { 0x00, 0x06, 0x26, 0x04, 0x05, 0x2a, 0x32, 0x3a, 0x3e, 0x42 },
101 static const char * const functions[] = { "gpio" };
104 struct sgpio_priv *priv;
106 struct gpio_chip gpio;
107 struct pinctrl_desc pctl_desc;
112 struct sgpio_bank in;
113 struct sgpio_bank out;
118 const struct sgpio_properties *properties;
122 struct sgpio_port_addr {
127 static inline void sgpio_pin_to_addr(struct sgpio_priv *priv, int pin,
128 struct sgpio_port_addr *addr)
130 addr->port = pin / priv->bitcount;
131 addr->bit = pin % priv->bitcount;
134 static inline int sgpio_addr_to_pin(struct sgpio_priv *priv, int port, int bit)
136 return bit + port * priv->bitcount;
139 static inline u32 sgpio_readl(struct sgpio_priv *priv, u32 rno, u32 off)
141 u32 __iomem *reg = &priv->regs[priv->properties->regoff[rno] + off];
146 static inline void sgpio_writel(struct sgpio_priv *priv,
147 u32 val, u32 rno, u32 off)
149 u32 __iomem *reg = &priv->regs[priv->properties->regoff[rno] + off];
154 static inline void sgpio_clrsetbits(struct sgpio_priv *priv,
155 u32 rno, u32 off, u32 clear, u32 set)
157 u32 __iomem *reg = &priv->regs[priv->properties->regoff[rno] + off];
158 u32 val = readl(reg);
166 static inline void sgpio_configure_bitstream(struct sgpio_priv *priv)
168 int width = priv->bitcount - 1;
171 switch (priv->properties->arch) {
172 case SGPIO_ARCH_LUTON:
173 clr = SGPIO_LUTON_PORT_WIDTH;
174 set = SGPIO_LUTON_AUTO_REPEAT |
175 FIELD_PREP(SGPIO_LUTON_PORT_WIDTH, width);
177 case SGPIO_ARCH_OCELOT:
178 clr = SGPIO_OCELOT_PORT_WIDTH;
179 set = SGPIO_OCELOT_AUTO_REPEAT |
180 FIELD_PREP(SGPIO_OCELOT_PORT_WIDTH, width);
182 case SGPIO_ARCH_SPARX5:
183 clr = SGPIO_SPARX5_PORT_WIDTH;
184 set = SGPIO_SPARX5_AUTO_REPEAT |
185 FIELD_PREP(SGPIO_SPARX5_PORT_WIDTH, width);
190 sgpio_clrsetbits(priv, REG_SIO_CONFIG, 0, clr, set);
193 static inline void sgpio_configure_clock(struct sgpio_priv *priv, u32 clkfrq)
197 switch (priv->properties->arch) {
198 case SGPIO_ARCH_LUTON:
199 clr = SGPIO_LUTON_CLK_FREQ;
200 set = FIELD_PREP(SGPIO_LUTON_CLK_FREQ, clkfrq);
202 case SGPIO_ARCH_OCELOT:
203 clr = SGPIO_OCELOT_CLK_FREQ;
204 set = FIELD_PREP(SGPIO_OCELOT_CLK_FREQ, clkfrq);
206 case SGPIO_ARCH_SPARX5:
207 clr = SGPIO_SPARX5_CLK_FREQ;
208 set = FIELD_PREP(SGPIO_SPARX5_CLK_FREQ, clkfrq);
213 sgpio_clrsetbits(priv, REG_SIO_CLOCK, 0, clr, set);
216 static void sgpio_output_set(struct sgpio_priv *priv,
217 struct sgpio_port_addr *addr,
220 unsigned int bit = SGPIO_SRC_BITS * addr->bit;
224 switch (priv->properties->arch) {
225 case SGPIO_ARCH_LUTON:
226 clr = FIELD_PREP(SGPIO_LUTON_BIT_SOURCE, BIT(bit));
227 set = FIELD_PREP(SGPIO_LUTON_BIT_SOURCE, value << bit);
229 case SGPIO_ARCH_OCELOT:
230 clr = FIELD_PREP(SGPIO_OCELOT_BIT_SOURCE, BIT(bit));
231 set = FIELD_PREP(SGPIO_OCELOT_BIT_SOURCE, value << bit);
233 case SGPIO_ARCH_SPARX5:
234 clr = FIELD_PREP(SGPIO_SPARX5_BIT_SOURCE, BIT(bit));
235 set = FIELD_PREP(SGPIO_SPARX5_BIT_SOURCE, value << bit);
241 spin_lock_irqsave(&priv->lock, flags);
242 sgpio_clrsetbits(priv, REG_PORT_CONFIG, addr->port, clr, set);
243 spin_unlock_irqrestore(&priv->lock, flags);
246 static int sgpio_output_get(struct sgpio_priv *priv,
247 struct sgpio_port_addr *addr)
249 u32 val, portval = sgpio_readl(priv, REG_PORT_CONFIG, addr->port);
250 unsigned int bit = SGPIO_SRC_BITS * addr->bit;
252 switch (priv->properties->arch) {
253 case SGPIO_ARCH_LUTON:
254 val = FIELD_GET(SGPIO_LUTON_BIT_SOURCE, portval);
256 case SGPIO_ARCH_OCELOT:
257 val = FIELD_GET(SGPIO_OCELOT_BIT_SOURCE, portval);
259 case SGPIO_ARCH_SPARX5:
260 val = FIELD_GET(SGPIO_SPARX5_BIT_SOURCE, portval);
266 return !!(val & BIT(bit));
269 static int sgpio_input_get(struct sgpio_priv *priv,
270 struct sgpio_port_addr *addr)
272 return !!(sgpio_readl(priv, REG_INPUT_DATA, addr->bit) & BIT(addr->port));
275 static int sgpio_pinconf_get(struct pinctrl_dev *pctldev,
276 unsigned int pin, unsigned long *config)
278 struct sgpio_bank *bank = pinctrl_dev_get_drvdata(pctldev);
279 u32 param = pinconf_to_config_param(*config);
280 struct sgpio_priv *priv = bank->priv;
281 struct sgpio_port_addr addr;
284 sgpio_pin_to_addr(priv, pin, &addr);
287 case PIN_CONFIG_INPUT_ENABLE:
288 val = bank->is_input;
291 case PIN_CONFIG_OUTPUT_ENABLE:
292 val = !bank->is_input;
295 case PIN_CONFIG_OUTPUT:
298 val = sgpio_output_get(priv, &addr);
305 *config = pinconf_to_config_packed(param, val);
310 static int sgpio_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
311 unsigned long *configs, unsigned int num_configs)
313 struct sgpio_bank *bank = pinctrl_dev_get_drvdata(pctldev);
314 struct sgpio_priv *priv = bank->priv;
315 struct sgpio_port_addr addr;
319 sgpio_pin_to_addr(priv, pin, &addr);
321 for (cfg = 0; cfg < num_configs; cfg++) {
322 param = pinconf_to_config_param(configs[cfg]);
323 arg = pinconf_to_config_argument(configs[cfg]);
326 case PIN_CONFIG_OUTPUT:
329 sgpio_output_set(priv, &addr, arg);
340 static const struct pinconf_ops sgpio_confops = {
342 .pin_config_get = sgpio_pinconf_get,
343 .pin_config_set = sgpio_pinconf_set,
344 .pin_config_config_dbg_show = pinconf_generic_dump_config,
347 static int sgpio_get_functions_count(struct pinctrl_dev *pctldev)
352 static const char *sgpio_get_function_name(struct pinctrl_dev *pctldev,
353 unsigned int function)
358 static int sgpio_get_function_groups(struct pinctrl_dev *pctldev,
359 unsigned int function,
360 const char *const **groups,
361 unsigned *const num_groups)
364 *num_groups = ARRAY_SIZE(functions);
369 static int sgpio_pinmux_set_mux(struct pinctrl_dev *pctldev,
370 unsigned int selector, unsigned int group)
375 static int sgpio_gpio_set_direction(struct pinctrl_dev *pctldev,
376 struct pinctrl_gpio_range *range,
377 unsigned int pin, bool input)
379 struct sgpio_bank *bank = pinctrl_dev_get_drvdata(pctldev);
381 return (input == bank->is_input) ? 0 : -EINVAL;
384 static int sgpio_gpio_request_enable(struct pinctrl_dev *pctldev,
385 struct pinctrl_gpio_range *range,
388 struct sgpio_bank *bank = pinctrl_dev_get_drvdata(pctldev);
389 struct sgpio_priv *priv = bank->priv;
390 struct sgpio_port_addr addr;
392 sgpio_pin_to_addr(priv, offset, &addr);
394 if ((priv->ports & BIT(addr.port)) == 0) {
395 dev_warn(priv->dev, "Request port %d.%d: Port is not enabled\n",
396 addr.port, addr.bit);
403 static const struct pinmux_ops sgpio_pmx_ops = {
404 .get_functions_count = sgpio_get_functions_count,
405 .get_function_name = sgpio_get_function_name,
406 .get_function_groups = sgpio_get_function_groups,
407 .set_mux = sgpio_pinmux_set_mux,
408 .gpio_set_direction = sgpio_gpio_set_direction,
409 .gpio_request_enable = sgpio_gpio_request_enable,
412 static int sgpio_pctl_get_groups_count(struct pinctrl_dev *pctldev)
414 struct sgpio_bank *bank = pinctrl_dev_get_drvdata(pctldev);
416 return bank->pctl_desc.npins;
419 static const char *sgpio_pctl_get_group_name(struct pinctrl_dev *pctldev,
422 struct sgpio_bank *bank = pinctrl_dev_get_drvdata(pctldev);
424 return bank->pctl_desc.pins[group].name;
427 static int sgpio_pctl_get_group_pins(struct pinctrl_dev *pctldev,
429 const unsigned int **pins,
430 unsigned int *num_pins)
432 struct sgpio_bank *bank = pinctrl_dev_get_drvdata(pctldev);
434 *pins = &bank->pctl_desc.pins[group].number;
440 static const struct pinctrl_ops sgpio_pctl_ops = {
441 .get_groups_count = sgpio_pctl_get_groups_count,
442 .get_group_name = sgpio_pctl_get_group_name,
443 .get_group_pins = sgpio_pctl_get_group_pins,
444 .dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
445 .dt_free_map = pinconf_generic_dt_free_map,
448 static int microchip_sgpio_direction_input(struct gpio_chip *gc, unsigned int gpio)
450 struct sgpio_bank *bank = gpiochip_get_data(gc);
452 /* Fixed-position function */
453 return bank->is_input ? 0 : -EINVAL;
456 static int microchip_sgpio_direction_output(struct gpio_chip *gc,
457 unsigned int gpio, int value)
459 struct sgpio_bank *bank = gpiochip_get_data(gc);
460 struct sgpio_priv *priv = bank->priv;
461 struct sgpio_port_addr addr;
463 /* Fixed-position function */
467 sgpio_pin_to_addr(priv, gpio, &addr);
469 sgpio_output_set(priv, &addr, value);
474 static int microchip_sgpio_get_direction(struct gpio_chip *gc, unsigned int gpio)
476 struct sgpio_bank *bank = gpiochip_get_data(gc);
478 return bank->is_input ? GPIO_LINE_DIRECTION_IN : GPIO_LINE_DIRECTION_OUT;
481 static void microchip_sgpio_set_value(struct gpio_chip *gc,
482 unsigned int gpio, int value)
484 microchip_sgpio_direction_output(gc, gpio, value);
487 static int microchip_sgpio_get_value(struct gpio_chip *gc, unsigned int gpio)
489 struct sgpio_bank *bank = gpiochip_get_data(gc);
490 struct sgpio_priv *priv = bank->priv;
491 struct sgpio_port_addr addr;
493 sgpio_pin_to_addr(priv, gpio, &addr);
495 return bank->is_input ? sgpio_input_get(priv, &addr) : sgpio_output_get(priv, &addr);
498 static int microchip_sgpio_of_xlate(struct gpio_chip *gc,
499 const struct of_phandle_args *gpiospec,
502 struct sgpio_bank *bank = gpiochip_get_data(gc);
503 struct sgpio_priv *priv = bank->priv;
507 * Note that the SGIO pin is defined by *2* numbers, a port
508 * number between 0 and 31, and a bit index, 0 to 3.
510 if (gpiospec->args[0] > SGPIO_BITS_PER_WORD ||
511 gpiospec->args[1] > priv->bitcount)
514 pin = sgpio_addr_to_pin(priv, gpiospec->args[0], gpiospec->args[1]);
520 *flags = gpiospec->args[2];
525 static int microchip_sgpio_get_ports(struct sgpio_priv *priv)
527 const char *range_property_name = "microchip,sgpio-port-ranges";
528 struct device *dev = priv->dev;
529 u32 range_params[64];
532 /* Calculate port mask */
533 nranges = device_property_count_u32(dev, range_property_name);
534 if (nranges < 2 || nranges % 2 || nranges > ARRAY_SIZE(range_params)) {
535 dev_err(dev, "%s port range: '%s' property\n",
536 nranges == -EINVAL ? "Missing" : "Invalid",
537 range_property_name);
541 ret = device_property_read_u32_array(dev, range_property_name,
542 range_params, nranges);
544 dev_err(dev, "failed to parse '%s' property: %d\n",
545 range_property_name, ret);
548 for (i = 0; i < nranges; i += 2) {
551 start = range_params[i];
552 end = range_params[i + 1];
553 if (start > end || end >= SGPIO_BITS_PER_WORD) {
554 dev_err(dev, "Ill-formed port-range [%d:%d]\n",
557 priv->ports |= GENMASK(end, start);
563 static void microchip_sgpio_irq_settype(struct irq_data *data,
567 struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
568 struct sgpio_bank *bank = gpiochip_get_data(chip);
569 unsigned int gpio = irqd_to_hwirq(data);
570 struct sgpio_port_addr addr;
574 sgpio_pin_to_addr(bank->priv, gpio, &addr);
576 spin_lock_irqsave(&bank->priv->lock, flags);
578 /* Disable interrupt while changing type */
579 ena = sgpio_readl(bank->priv, REG_INT_ENABLE, addr.bit);
580 sgpio_writel(bank->priv, ena & ~BIT(addr.port), REG_INT_ENABLE, addr.bit);
582 /* Type value spread over 2 registers sets: low, high bit */
583 sgpio_clrsetbits(bank->priv, REG_INT_TRIGGER, addr.bit,
584 BIT(addr.port), (!!(type & 0x1)) << addr.port);
585 sgpio_clrsetbits(bank->priv, REG_INT_TRIGGER, SGPIO_MAX_BITS + addr.bit,
586 BIT(addr.port), (!!(type & 0x2)) << addr.port);
588 if (type == SGPIO_INT_TRG_LEVEL)
589 sgpio_clrsetbits(bank->priv, REG_INT_POLARITY, addr.bit,
590 BIT(addr.port), polarity << addr.port);
592 /* Possibly re-enable interrupts */
593 sgpio_writel(bank->priv, ena, REG_INT_ENABLE, addr.bit);
595 spin_unlock_irqrestore(&bank->priv->lock, flags);
598 static void microchip_sgpio_irq_setreg(struct irq_data *data,
602 struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
603 struct sgpio_bank *bank = gpiochip_get_data(chip);
604 unsigned int gpio = irqd_to_hwirq(data);
605 struct sgpio_port_addr addr;
608 sgpio_pin_to_addr(bank->priv, gpio, &addr);
610 spin_lock_irqsave(&bank->priv->lock, flags);
612 sgpio_clrsetbits(bank->priv, reg, addr.bit, BIT(addr.port), 0);
614 sgpio_clrsetbits(bank->priv, reg, addr.bit, 0, BIT(addr.port));
615 spin_unlock_irqrestore(&bank->priv->lock, flags);
618 static void microchip_sgpio_irq_mask(struct irq_data *data)
620 microchip_sgpio_irq_setreg(data, REG_INT_ENABLE, true);
623 static void microchip_sgpio_irq_unmask(struct irq_data *data)
625 microchip_sgpio_irq_setreg(data, REG_INT_ENABLE, false);
628 static void microchip_sgpio_irq_ack(struct irq_data *data)
630 microchip_sgpio_irq_setreg(data, REG_INT_ACK, false);
633 static int microchip_sgpio_irq_set_type(struct irq_data *data, unsigned int type)
635 type &= IRQ_TYPE_SENSE_MASK;
638 case IRQ_TYPE_EDGE_BOTH:
639 irq_set_handler_locked(data, handle_edge_irq);
640 microchip_sgpio_irq_settype(data, SGPIO_INT_TRG_EDGE, 0);
642 case IRQ_TYPE_EDGE_RISING:
643 irq_set_handler_locked(data, handle_edge_irq);
644 microchip_sgpio_irq_settype(data, SGPIO_INT_TRG_EDGE_RISE, 0);
646 case IRQ_TYPE_EDGE_FALLING:
647 irq_set_handler_locked(data, handle_edge_irq);
648 microchip_sgpio_irq_settype(data, SGPIO_INT_TRG_EDGE_FALL, 0);
650 case IRQ_TYPE_LEVEL_HIGH:
651 irq_set_handler_locked(data, handle_level_irq);
652 microchip_sgpio_irq_settype(data, SGPIO_INT_TRG_LEVEL, SGPIO_TRG_LEVEL_HIGH);
654 case IRQ_TYPE_LEVEL_LOW:
655 irq_set_handler_locked(data, handle_level_irq);
656 microchip_sgpio_irq_settype(data, SGPIO_INT_TRG_LEVEL, SGPIO_TRG_LEVEL_LOW);
665 static const struct irq_chip microchip_sgpio_irqchip = {
667 .irq_mask = microchip_sgpio_irq_mask,
668 .irq_ack = microchip_sgpio_irq_ack,
669 .irq_unmask = microchip_sgpio_irq_unmask,
670 .irq_set_type = microchip_sgpio_irq_set_type,
673 static void sgpio_irq_handler(struct irq_desc *desc)
675 struct irq_chip *parent_chip = irq_desc_get_chip(desc);
676 struct gpio_chip *chip = irq_desc_get_handler_data(desc);
677 struct sgpio_bank *bank = gpiochip_get_data(chip);
678 struct sgpio_priv *priv = bank->priv;
682 for (bit = 0; bit < priv->bitcount; bit++) {
683 val = sgpio_readl(priv, REG_INT_IDENT, bit);
687 chained_irq_enter(parent_chip, desc);
689 for_each_set_bit(port, &val, SGPIO_BITS_PER_WORD) {
690 gpio = sgpio_addr_to_pin(priv, port, bit);
691 generic_handle_domain_irq(chip->irq.domain, gpio);
694 chained_irq_exit(parent_chip, desc);
698 static int microchip_sgpio_register_bank(struct device *dev,
699 struct sgpio_priv *priv,
700 struct fwnode_handle *fwnode,
703 struct pinctrl_pin_desc *pins;
704 struct pinctrl_desc *pctl_desc;
705 struct pinctrl_dev *pctldev;
706 struct sgpio_bank *bank;
707 struct gpio_chip *gc;
711 /* Get overall bank struct */
712 bank = (bankno == 0) ? &priv->in : &priv->out;
715 if (fwnode_property_read_u32(fwnode, "ngpios", &ngpios)) {
716 dev_info(dev, "failed to get number of gpios for bank%d\n",
721 priv->bitcount = ngpios / SGPIO_BITS_PER_WORD;
722 if (priv->bitcount > SGPIO_MAX_BITS) {
723 dev_err(dev, "Bit width exceeds maximum (%d)\n",
728 pctl_desc = &bank->pctl_desc;
729 pctl_desc->name = devm_kasprintf(dev, GFP_KERNEL, "%s-%sput",
731 bank->is_input ? "in" : "out");
732 pctl_desc->pctlops = &sgpio_pctl_ops;
733 pctl_desc->pmxops = &sgpio_pmx_ops;
734 pctl_desc->confops = &sgpio_confops;
735 pctl_desc->owner = THIS_MODULE;
737 pins = devm_kzalloc(dev, sizeof(*pins)*ngpios, GFP_KERNEL);
741 pctl_desc->npins = ngpios;
742 pctl_desc->pins = pins;
744 for (i = 0; i < ngpios; i++) {
745 struct sgpio_port_addr addr;
747 sgpio_pin_to_addr(priv, i, &addr);
750 pins[i].name = devm_kasprintf(dev, GFP_KERNEL,
752 bank->is_input ? 'I' : 'O',
753 addr.port, addr.bit);
758 pctldev = devm_pinctrl_register(dev, pctl_desc, bank);
760 return dev_err_probe(dev, PTR_ERR(pctldev), "Failed to register pinctrl\n");
763 gc->label = pctl_desc->name;
765 gc->of_node = to_of_node(fwnode);
766 gc->owner = THIS_MODULE;
767 gc->get_direction = microchip_sgpio_get_direction;
768 gc->direction_input = microchip_sgpio_direction_input;
769 gc->direction_output = microchip_sgpio_direction_output;
770 gc->get = microchip_sgpio_get_value;
771 gc->set = microchip_sgpio_set_value;
772 gc->request = gpiochip_generic_request;
773 gc->free = gpiochip_generic_free;
774 gc->of_xlate = microchip_sgpio_of_xlate;
775 gc->of_gpio_n_cells = 3;
779 if (bank->is_input && priv->properties->flags & SGPIO_FLAGS_HAS_IRQ) {
780 int irq = fwnode_irq_get(fwnode, 0);
783 struct gpio_irq_chip *girq = &gc->irq;
785 girq->chip = devm_kmemdup(dev, µchip_sgpio_irqchip,
786 sizeof(microchip_sgpio_irqchip),
790 girq->parent_handler = sgpio_irq_handler;
791 girq->num_parents = 1;
792 girq->parents = devm_kcalloc(dev, 1,
793 sizeof(*girq->parents),
797 girq->parents[0] = irq;
798 girq->default_type = IRQ_TYPE_NONE;
799 girq->handler = handle_bad_irq;
801 /* Disable all individual pins */
802 for (i = 0; i < SGPIO_MAX_BITS; i++)
803 sgpio_writel(priv, 0, REG_INT_ENABLE, i);
805 sgpio_clrsetbits(priv, REG_SIO_CONFIG, 0, 0, SGPIO_MASTER_INTR_ENA);
809 ret = devm_gpiochip_add_data(dev, gc, bank);
811 dev_err(dev, "Failed to register: ret %d\n", ret);
816 static int microchip_sgpio_probe(struct platform_device *pdev)
818 int div_clock = 0, ret, port, i, nbanks;
819 struct device *dev = &pdev->dev;
820 struct fwnode_handle *fwnode;
821 struct reset_control *reset;
822 struct sgpio_priv *priv;
826 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
831 spin_lock_init(&priv->lock);
833 reset = devm_reset_control_get_optional_shared(&pdev->dev, "switch");
835 return dev_err_probe(dev, PTR_ERR(reset), "Failed to get reset\n");
836 reset_control_reset(reset);
838 clk = devm_clk_get(dev, NULL);
840 return dev_err_probe(dev, PTR_ERR(clk), "Failed to get clock\n");
842 div_clock = clk_get_rate(clk);
843 if (device_property_read_u32(dev, "bus-frequency", &priv->clock))
844 priv->clock = 12500000;
845 if (priv->clock == 0 || priv->clock > (div_clock / 2)) {
846 dev_err(dev, "Invalid frequency %d\n", priv->clock);
850 priv->regs = devm_platform_ioremap_resource(pdev, 0);
851 if (IS_ERR(priv->regs))
852 return PTR_ERR(priv->regs);
853 priv->properties = device_get_match_data(dev);
854 priv->in.is_input = true;
856 /* Get rest of device properties */
857 ret = microchip_sgpio_get_ports(priv);
861 nbanks = device_get_child_node_count(dev);
863 dev_err(dev, "Must have 2 banks (have %d)\n", nbanks);
868 device_for_each_child_node(dev, fwnode) {
869 ret = microchip_sgpio_register_bank(dev, priv, fwnode, i++);
871 fwnode_handle_put(fwnode);
876 if (priv->in.gpio.ngpio != priv->out.gpio.ngpio) {
877 dev_err(dev, "Banks must have same GPIO count\n");
881 sgpio_configure_bitstream(priv);
883 val = max(2U, div_clock / priv->clock);
884 sgpio_configure_clock(priv, val);
886 for (port = 0; port < SGPIO_BITS_PER_WORD; port++)
887 sgpio_writel(priv, 0, REG_PORT_CONFIG, port);
888 sgpio_writel(priv, priv->ports, REG_PORT_ENABLE, 0);
893 static const struct of_device_id microchip_sgpio_gpio_of_match[] = {
895 .compatible = "microchip,sparx5-sgpio",
896 .data = &properties_sparx5,
898 .compatible = "mscc,luton-sgpio",
899 .data = &properties_luton,
901 .compatible = "mscc,ocelot-sgpio",
902 .data = &properties_ocelot,
908 static struct platform_driver microchip_sgpio_pinctrl_driver = {
910 .name = "pinctrl-microchip-sgpio",
911 .of_match_table = microchip_sgpio_gpio_of_match,
912 .suppress_bind_attrs = true,
914 .probe = microchip_sgpio_probe,
916 builtin_platform_driver(microchip_sgpio_pinctrl_driver);