1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Microsemi/Microchip SoCs serial gpio driver
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>
25 #define SGPIO_BITS_PER_WORD 32
26 #define SGPIO_MAX_BITS 4
27 #define SGPIO_SRC_BITS 3 /* 3 bit wide field per pin */
50 SGPIO_FLAGS_HAS_IRQ = BIT(0),
53 struct sgpio_properties {
59 #define SGPIO_LUTON_AUTO_REPEAT BIT(5)
60 #define SGPIO_LUTON_PORT_WIDTH GENMASK(3, 2)
61 #define SGPIO_LUTON_CLK_FREQ GENMASK(11, 0)
62 #define SGPIO_LUTON_BIT_SOURCE GENMASK(11, 0)
64 #define SGPIO_OCELOT_AUTO_REPEAT BIT(10)
65 #define SGPIO_OCELOT_PORT_WIDTH GENMASK(8, 7)
66 #define SGPIO_OCELOT_CLK_FREQ GENMASK(19, 8)
67 #define SGPIO_OCELOT_BIT_SOURCE GENMASK(23, 12)
69 #define SGPIO_SPARX5_AUTO_REPEAT BIT(6)
70 #define SGPIO_SPARX5_PORT_WIDTH GENMASK(4, 3)
71 #define SGPIO_SPARX5_CLK_FREQ GENMASK(19, 8)
72 #define SGPIO_SPARX5_BIT_SOURCE GENMASK(23, 12)
74 #define SGPIO_MASTER_INTR_ENA BIT(0)
76 #define SGPIO_INT_TRG_LEVEL 0
77 #define SGPIO_INT_TRG_EDGE 1
78 #define SGPIO_INT_TRG_EDGE_FALL 2
79 #define SGPIO_INT_TRG_EDGE_RISE 3
81 #define SGPIO_TRG_LEVEL_HIGH 0
82 #define SGPIO_TRG_LEVEL_LOW 1
84 static const struct sgpio_properties properties_luton = {
85 .arch = SGPIO_ARCH_LUTON,
86 .regoff = { 0x00, 0x09, 0x29, 0x2a, 0x2b },
89 static const struct sgpio_properties properties_ocelot = {
90 .arch = SGPIO_ARCH_OCELOT,
91 .regoff = { 0x00, 0x06, 0x26, 0x04, 0x05 },
94 static const struct sgpio_properties properties_sparx5 = {
95 .arch = SGPIO_ARCH_SPARX5,
96 .flags = SGPIO_FLAGS_HAS_IRQ,
97 .regoff = { 0x00, 0x06, 0x26, 0x04, 0x05, 0x2a, 0x32, 0x3a, 0x3e, 0x42 },
100 static const char * const functions[] = { "gpio" };
103 struct sgpio_priv *priv;
105 struct gpio_chip gpio;
106 struct pinctrl_desc pctl_desc;
111 struct sgpio_bank in;
112 struct sgpio_bank out;
117 const struct sgpio_properties *properties;
120 struct sgpio_port_addr {
125 static inline void sgpio_pin_to_addr(struct sgpio_priv *priv, int pin,
126 struct sgpio_port_addr *addr)
128 addr->port = pin / priv->bitcount;
129 addr->bit = pin % priv->bitcount;
132 static inline int sgpio_addr_to_pin(struct sgpio_priv *priv, int port, int bit)
134 return bit + port * priv->bitcount;
137 static inline u32 sgpio_readl(struct sgpio_priv *priv, u32 rno, u32 off)
139 u32 __iomem *reg = &priv->regs[priv->properties->regoff[rno] + off];
144 static inline void sgpio_writel(struct sgpio_priv *priv,
145 u32 val, u32 rno, u32 off)
147 u32 __iomem *reg = &priv->regs[priv->properties->regoff[rno] + off];
152 static inline void sgpio_clrsetbits(struct sgpio_priv *priv,
153 u32 rno, u32 off, u32 clear, u32 set)
155 u32 __iomem *reg = &priv->regs[priv->properties->regoff[rno] + off];
156 u32 val = readl(reg);
164 static inline void sgpio_configure_bitstream(struct sgpio_priv *priv)
166 int width = priv->bitcount - 1;
169 switch (priv->properties->arch) {
170 case SGPIO_ARCH_LUTON:
171 clr = SGPIO_LUTON_PORT_WIDTH;
172 set = SGPIO_LUTON_AUTO_REPEAT |
173 FIELD_PREP(SGPIO_LUTON_PORT_WIDTH, width);
175 case SGPIO_ARCH_OCELOT:
176 clr = SGPIO_OCELOT_PORT_WIDTH;
177 set = SGPIO_OCELOT_AUTO_REPEAT |
178 FIELD_PREP(SGPIO_OCELOT_PORT_WIDTH, width);
180 case SGPIO_ARCH_SPARX5:
181 clr = SGPIO_SPARX5_PORT_WIDTH;
182 set = SGPIO_SPARX5_AUTO_REPEAT |
183 FIELD_PREP(SGPIO_SPARX5_PORT_WIDTH, width);
188 sgpio_clrsetbits(priv, REG_SIO_CONFIG, 0, clr, set);
191 static inline void sgpio_configure_clock(struct sgpio_priv *priv, u32 clkfrq)
195 switch (priv->properties->arch) {
196 case SGPIO_ARCH_LUTON:
197 clr = SGPIO_LUTON_CLK_FREQ;
198 set = FIELD_PREP(SGPIO_LUTON_CLK_FREQ, clkfrq);
200 case SGPIO_ARCH_OCELOT:
201 clr = SGPIO_OCELOT_CLK_FREQ;
202 set = FIELD_PREP(SGPIO_OCELOT_CLK_FREQ, clkfrq);
204 case SGPIO_ARCH_SPARX5:
205 clr = SGPIO_SPARX5_CLK_FREQ;
206 set = FIELD_PREP(SGPIO_SPARX5_CLK_FREQ, clkfrq);
211 sgpio_clrsetbits(priv, REG_SIO_CLOCK, 0, clr, set);
214 static void sgpio_output_set(struct sgpio_priv *priv,
215 struct sgpio_port_addr *addr,
218 unsigned int bit = SGPIO_SRC_BITS * addr->bit;
221 switch (priv->properties->arch) {
222 case SGPIO_ARCH_LUTON:
223 clr = FIELD_PREP(SGPIO_LUTON_BIT_SOURCE, BIT(bit));
224 set = FIELD_PREP(SGPIO_LUTON_BIT_SOURCE, value << bit);
226 case SGPIO_ARCH_OCELOT:
227 clr = FIELD_PREP(SGPIO_OCELOT_BIT_SOURCE, BIT(bit));
228 set = FIELD_PREP(SGPIO_OCELOT_BIT_SOURCE, value << bit);
230 case SGPIO_ARCH_SPARX5:
231 clr = FIELD_PREP(SGPIO_SPARX5_BIT_SOURCE, BIT(bit));
232 set = FIELD_PREP(SGPIO_SPARX5_BIT_SOURCE, value << bit);
237 sgpio_clrsetbits(priv, REG_PORT_CONFIG, addr->port, clr, set);
240 static int sgpio_output_get(struct sgpio_priv *priv,
241 struct sgpio_port_addr *addr)
243 u32 val, portval = sgpio_readl(priv, REG_PORT_CONFIG, addr->port);
244 unsigned int bit = SGPIO_SRC_BITS * addr->bit;
246 switch (priv->properties->arch) {
247 case SGPIO_ARCH_LUTON:
248 val = FIELD_GET(SGPIO_LUTON_BIT_SOURCE, portval);
250 case SGPIO_ARCH_OCELOT:
251 val = FIELD_GET(SGPIO_OCELOT_BIT_SOURCE, portval);
253 case SGPIO_ARCH_SPARX5:
254 val = FIELD_GET(SGPIO_SPARX5_BIT_SOURCE, portval);
260 return !!(val & BIT(bit));
263 static int sgpio_input_get(struct sgpio_priv *priv,
264 struct sgpio_port_addr *addr)
266 return !!(sgpio_readl(priv, REG_INPUT_DATA, addr->bit) & BIT(addr->port));
269 static int sgpio_pinconf_get(struct pinctrl_dev *pctldev,
270 unsigned int pin, unsigned long *config)
272 struct sgpio_bank *bank = pinctrl_dev_get_drvdata(pctldev);
273 u32 param = pinconf_to_config_param(*config);
274 struct sgpio_priv *priv = bank->priv;
275 struct sgpio_port_addr addr;
278 sgpio_pin_to_addr(priv, pin, &addr);
281 case PIN_CONFIG_INPUT_ENABLE:
282 val = bank->is_input;
285 case PIN_CONFIG_OUTPUT_ENABLE:
286 val = !bank->is_input;
289 case PIN_CONFIG_OUTPUT:
292 val = sgpio_output_get(priv, &addr);
299 *config = pinconf_to_config_packed(param, val);
304 static int sgpio_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
305 unsigned long *configs, unsigned int num_configs)
307 struct sgpio_bank *bank = pinctrl_dev_get_drvdata(pctldev);
308 struct sgpio_priv *priv = bank->priv;
309 struct sgpio_port_addr addr;
313 sgpio_pin_to_addr(priv, pin, &addr);
315 for (cfg = 0; cfg < num_configs; cfg++) {
316 param = pinconf_to_config_param(configs[cfg]);
317 arg = pinconf_to_config_argument(configs[cfg]);
320 case PIN_CONFIG_OUTPUT:
323 sgpio_output_set(priv, &addr, arg);
334 static const struct pinconf_ops sgpio_confops = {
336 .pin_config_get = sgpio_pinconf_get,
337 .pin_config_set = sgpio_pinconf_set,
338 .pin_config_config_dbg_show = pinconf_generic_dump_config,
341 static int sgpio_get_functions_count(struct pinctrl_dev *pctldev)
346 static const char *sgpio_get_function_name(struct pinctrl_dev *pctldev,
347 unsigned int function)
352 static int sgpio_get_function_groups(struct pinctrl_dev *pctldev,
353 unsigned int function,
354 const char *const **groups,
355 unsigned *const num_groups)
358 *num_groups = ARRAY_SIZE(functions);
363 static int sgpio_pinmux_set_mux(struct pinctrl_dev *pctldev,
364 unsigned int selector, unsigned int group)
369 static int sgpio_gpio_set_direction(struct pinctrl_dev *pctldev,
370 struct pinctrl_gpio_range *range,
371 unsigned int pin, bool input)
373 struct sgpio_bank *bank = pinctrl_dev_get_drvdata(pctldev);
375 return (input == bank->is_input) ? 0 : -EINVAL;
378 static int sgpio_gpio_request_enable(struct pinctrl_dev *pctldev,
379 struct pinctrl_gpio_range *range,
382 struct sgpio_bank *bank = pinctrl_dev_get_drvdata(pctldev);
383 struct sgpio_priv *priv = bank->priv;
384 struct sgpio_port_addr addr;
386 sgpio_pin_to_addr(priv, offset, &addr);
388 if ((priv->ports & BIT(addr.port)) == 0) {
389 dev_warn(priv->dev, "Request port %d.%d: Port is not enabled\n",
390 addr.port, addr.bit);
397 static const struct pinmux_ops sgpio_pmx_ops = {
398 .get_functions_count = sgpio_get_functions_count,
399 .get_function_name = sgpio_get_function_name,
400 .get_function_groups = sgpio_get_function_groups,
401 .set_mux = sgpio_pinmux_set_mux,
402 .gpio_set_direction = sgpio_gpio_set_direction,
403 .gpio_request_enable = sgpio_gpio_request_enable,
406 static int sgpio_pctl_get_groups_count(struct pinctrl_dev *pctldev)
408 struct sgpio_bank *bank = pinctrl_dev_get_drvdata(pctldev);
410 return bank->pctl_desc.npins;
413 static const char *sgpio_pctl_get_group_name(struct pinctrl_dev *pctldev,
416 struct sgpio_bank *bank = pinctrl_dev_get_drvdata(pctldev);
418 return bank->pctl_desc.pins[group].name;
421 static int sgpio_pctl_get_group_pins(struct pinctrl_dev *pctldev,
423 const unsigned int **pins,
424 unsigned int *num_pins)
426 struct sgpio_bank *bank = pinctrl_dev_get_drvdata(pctldev);
428 *pins = &bank->pctl_desc.pins[group].number;
434 static const struct pinctrl_ops sgpio_pctl_ops = {
435 .get_groups_count = sgpio_pctl_get_groups_count,
436 .get_group_name = sgpio_pctl_get_group_name,
437 .get_group_pins = sgpio_pctl_get_group_pins,
438 .dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
439 .dt_free_map = pinconf_generic_dt_free_map,
442 static int microchip_sgpio_direction_input(struct gpio_chip *gc, unsigned int gpio)
444 struct sgpio_bank *bank = gpiochip_get_data(gc);
446 /* Fixed-position function */
447 return bank->is_input ? 0 : -EINVAL;
450 static int microchip_sgpio_direction_output(struct gpio_chip *gc,
451 unsigned int gpio, int value)
453 struct sgpio_bank *bank = gpiochip_get_data(gc);
454 struct sgpio_priv *priv = bank->priv;
455 struct sgpio_port_addr addr;
457 /* Fixed-position function */
461 sgpio_pin_to_addr(priv, gpio, &addr);
463 sgpio_output_set(priv, &addr, value);
468 static int microchip_sgpio_get_direction(struct gpio_chip *gc, unsigned int gpio)
470 struct sgpio_bank *bank = gpiochip_get_data(gc);
472 return bank->is_input ? GPIO_LINE_DIRECTION_IN : GPIO_LINE_DIRECTION_OUT;
475 static void microchip_sgpio_set_value(struct gpio_chip *gc,
476 unsigned int gpio, int value)
478 microchip_sgpio_direction_output(gc, gpio, value);
481 static int microchip_sgpio_get_value(struct gpio_chip *gc, unsigned int gpio)
483 struct sgpio_bank *bank = gpiochip_get_data(gc);
484 struct sgpio_priv *priv = bank->priv;
485 struct sgpio_port_addr addr;
487 sgpio_pin_to_addr(priv, gpio, &addr);
489 return bank->is_input ? sgpio_input_get(priv, &addr) : sgpio_output_get(priv, &addr);
492 static int microchip_sgpio_of_xlate(struct gpio_chip *gc,
493 const struct of_phandle_args *gpiospec,
496 struct sgpio_bank *bank = gpiochip_get_data(gc);
497 struct sgpio_priv *priv = bank->priv;
501 * Note that the SGIO pin is defined by *2* numbers, a port
502 * number between 0 and 31, and a bit index, 0 to 3.
504 if (gpiospec->args[0] > SGPIO_BITS_PER_WORD ||
505 gpiospec->args[1] > priv->bitcount)
508 pin = sgpio_addr_to_pin(priv, gpiospec->args[0], gpiospec->args[1]);
514 *flags = gpiospec->args[2];
519 static int microchip_sgpio_get_ports(struct sgpio_priv *priv)
521 const char *range_property_name = "microchip,sgpio-port-ranges";
522 struct device *dev = priv->dev;
523 u32 range_params[64];
526 /* Calculate port mask */
527 nranges = device_property_count_u32(dev, range_property_name);
528 if (nranges < 2 || nranges % 2 || nranges > ARRAY_SIZE(range_params)) {
529 dev_err(dev, "%s port range: '%s' property\n",
530 nranges == -EINVAL ? "Missing" : "Invalid",
531 range_property_name);
535 ret = device_property_read_u32_array(dev, range_property_name,
536 range_params, nranges);
538 dev_err(dev, "failed to parse '%s' property: %d\n",
539 range_property_name, ret);
542 for (i = 0; i < nranges; i += 2) {
545 start = range_params[i];
546 end = range_params[i + 1];
547 if (start > end || end >= SGPIO_BITS_PER_WORD) {
548 dev_err(dev, "Ill-formed port-range [%d:%d]\n",
551 priv->ports |= GENMASK(end, start);
557 static void microchip_sgpio_irq_settype(struct irq_data *data,
561 struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
562 struct sgpio_bank *bank = gpiochip_get_data(chip);
563 unsigned int gpio = irqd_to_hwirq(data);
564 struct sgpio_port_addr addr;
567 sgpio_pin_to_addr(bank->priv, gpio, &addr);
569 /* Disable interrupt while changing type */
570 ena = sgpio_readl(bank->priv, REG_INT_ENABLE, addr.bit);
571 sgpio_writel(bank->priv, ena & ~BIT(addr.port), REG_INT_ENABLE, addr.bit);
573 /* Type value spread over 2 registers sets: low, high bit */
574 sgpio_clrsetbits(bank->priv, REG_INT_TRIGGER, addr.bit,
575 BIT(addr.port), (!!(type & 0x1)) << addr.port);
576 sgpio_clrsetbits(bank->priv, REG_INT_TRIGGER, SGPIO_MAX_BITS + addr.bit,
577 BIT(addr.port), (!!(type & 0x2)) << addr.port);
579 if (type == SGPIO_INT_TRG_LEVEL)
580 sgpio_clrsetbits(bank->priv, REG_INT_POLARITY, addr.bit,
581 BIT(addr.port), polarity << addr.port);
583 /* Possibly re-enable interrupts */
584 sgpio_writel(bank->priv, ena, REG_INT_ENABLE, addr.bit);
587 static void microchip_sgpio_irq_setreg(struct irq_data *data,
591 struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
592 struct sgpio_bank *bank = gpiochip_get_data(chip);
593 unsigned int gpio = irqd_to_hwirq(data);
594 struct sgpio_port_addr addr;
596 sgpio_pin_to_addr(bank->priv, gpio, &addr);
599 sgpio_clrsetbits(bank->priv, reg, addr.bit, BIT(addr.port), 0);
601 sgpio_clrsetbits(bank->priv, reg, addr.bit, 0, BIT(addr.port));
604 static void microchip_sgpio_irq_mask(struct irq_data *data)
606 microchip_sgpio_irq_setreg(data, REG_INT_ENABLE, true);
609 static void microchip_sgpio_irq_unmask(struct irq_data *data)
611 microchip_sgpio_irq_setreg(data, REG_INT_ENABLE, false);
614 static void microchip_sgpio_irq_ack(struct irq_data *data)
616 microchip_sgpio_irq_setreg(data, REG_INT_ACK, false);
619 static int microchip_sgpio_irq_set_type(struct irq_data *data, unsigned int type)
621 type &= IRQ_TYPE_SENSE_MASK;
624 case IRQ_TYPE_EDGE_BOTH:
625 irq_set_handler_locked(data, handle_edge_irq);
626 microchip_sgpio_irq_settype(data, SGPIO_INT_TRG_EDGE, 0);
628 case IRQ_TYPE_EDGE_RISING:
629 irq_set_handler_locked(data, handle_edge_irq);
630 microchip_sgpio_irq_settype(data, SGPIO_INT_TRG_EDGE_RISE, 0);
632 case IRQ_TYPE_EDGE_FALLING:
633 irq_set_handler_locked(data, handle_edge_irq);
634 microchip_sgpio_irq_settype(data, SGPIO_INT_TRG_EDGE_FALL, 0);
636 case IRQ_TYPE_LEVEL_HIGH:
637 irq_set_handler_locked(data, handle_level_irq);
638 microchip_sgpio_irq_settype(data, SGPIO_INT_TRG_LEVEL, SGPIO_TRG_LEVEL_HIGH);
640 case IRQ_TYPE_LEVEL_LOW:
641 irq_set_handler_locked(data, handle_level_irq);
642 microchip_sgpio_irq_settype(data, SGPIO_INT_TRG_LEVEL, SGPIO_TRG_LEVEL_LOW);
651 static const struct irq_chip microchip_sgpio_irqchip = {
653 .irq_mask = microchip_sgpio_irq_mask,
654 .irq_ack = microchip_sgpio_irq_ack,
655 .irq_unmask = microchip_sgpio_irq_unmask,
656 .irq_set_type = microchip_sgpio_irq_set_type,
659 static void sgpio_irq_handler(struct irq_desc *desc)
661 struct irq_chip *parent_chip = irq_desc_get_chip(desc);
662 struct gpio_chip *chip = irq_desc_get_handler_data(desc);
663 struct sgpio_bank *bank = gpiochip_get_data(chip);
664 struct sgpio_priv *priv = bank->priv;
668 for (bit = 0; bit < priv->bitcount; bit++) {
669 val = sgpio_readl(priv, REG_INT_IDENT, bit);
673 chained_irq_enter(parent_chip, desc);
675 for_each_set_bit(port, &val, SGPIO_BITS_PER_WORD) {
676 gpio = sgpio_addr_to_pin(priv, port, bit);
677 generic_handle_domain_irq(chip->irq.domain, gpio);
680 chained_irq_exit(parent_chip, desc);
684 static int microchip_sgpio_register_bank(struct device *dev,
685 struct sgpio_priv *priv,
686 struct fwnode_handle *fwnode,
689 struct pinctrl_pin_desc *pins;
690 struct pinctrl_desc *pctl_desc;
691 struct pinctrl_dev *pctldev;
692 struct sgpio_bank *bank;
693 struct gpio_chip *gc;
697 /* Get overall bank struct */
698 bank = (bankno == 0) ? &priv->in : &priv->out;
701 if (fwnode_property_read_u32(fwnode, "ngpios", &ngpios)) {
702 dev_info(dev, "failed to get number of gpios for bank%d\n",
707 priv->bitcount = ngpios / SGPIO_BITS_PER_WORD;
708 if (priv->bitcount > SGPIO_MAX_BITS) {
709 dev_err(dev, "Bit width exceeds maximum (%d)\n",
714 pctl_desc = &bank->pctl_desc;
715 pctl_desc->name = devm_kasprintf(dev, GFP_KERNEL, "%s-%sput",
717 bank->is_input ? "in" : "out");
718 pctl_desc->pctlops = &sgpio_pctl_ops;
719 pctl_desc->pmxops = &sgpio_pmx_ops;
720 pctl_desc->confops = &sgpio_confops;
721 pctl_desc->owner = THIS_MODULE;
723 pins = devm_kzalloc(dev, sizeof(*pins)*ngpios, GFP_KERNEL);
727 pctl_desc->npins = ngpios;
728 pctl_desc->pins = pins;
730 for (i = 0; i < ngpios; i++) {
731 struct sgpio_port_addr addr;
733 sgpio_pin_to_addr(priv, i, &addr);
736 pins[i].name = devm_kasprintf(dev, GFP_KERNEL,
738 bank->is_input ? 'I' : 'O',
739 addr.port, addr.bit);
744 pctldev = devm_pinctrl_register(dev, pctl_desc, bank);
746 return dev_err_probe(dev, PTR_ERR(pctldev), "Failed to register pinctrl\n");
749 gc->label = pctl_desc->name;
751 gc->of_node = to_of_node(fwnode);
752 gc->owner = THIS_MODULE;
753 gc->get_direction = microchip_sgpio_get_direction;
754 gc->direction_input = microchip_sgpio_direction_input;
755 gc->direction_output = microchip_sgpio_direction_output;
756 gc->get = microchip_sgpio_get_value;
757 gc->set = microchip_sgpio_set_value;
758 gc->request = gpiochip_generic_request;
759 gc->free = gpiochip_generic_free;
760 gc->of_xlate = microchip_sgpio_of_xlate;
761 gc->of_gpio_n_cells = 3;
765 if (bank->is_input && priv->properties->flags & SGPIO_FLAGS_HAS_IRQ) {
766 int irq = fwnode_irq_get(fwnode, 0);
769 struct gpio_irq_chip *girq = &gc->irq;
771 girq->chip = devm_kmemdup(dev, µchip_sgpio_irqchip,
772 sizeof(microchip_sgpio_irqchip),
776 girq->parent_handler = sgpio_irq_handler;
777 girq->num_parents = 1;
778 girq->parents = devm_kcalloc(dev, 1,
779 sizeof(*girq->parents),
783 girq->parents[0] = irq;
784 girq->default_type = IRQ_TYPE_NONE;
785 girq->handler = handle_bad_irq;
787 /* Disable all individual pins */
788 for (i = 0; i < SGPIO_MAX_BITS; i++)
789 sgpio_writel(priv, 0, REG_INT_ENABLE, i);
791 sgpio_clrsetbits(priv, REG_SIO_CONFIG, 0, 0, SGPIO_MASTER_INTR_ENA);
795 ret = devm_gpiochip_add_data(dev, gc, bank);
797 dev_err(dev, "Failed to register: ret %d\n", ret);
802 static int microchip_sgpio_probe(struct platform_device *pdev)
804 int div_clock = 0, ret, port, i, nbanks;
805 struct device *dev = &pdev->dev;
806 struct fwnode_handle *fwnode;
807 struct reset_control *reset;
808 struct sgpio_priv *priv;
812 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
818 reset = devm_reset_control_get_optional_shared(&pdev->dev, "switch");
820 return dev_err_probe(dev, PTR_ERR(reset), "Failed to get reset\n");
821 reset_control_reset(reset);
823 clk = devm_clk_get(dev, NULL);
825 return dev_err_probe(dev, PTR_ERR(clk), "Failed to get clock\n");
827 div_clock = clk_get_rate(clk);
828 if (device_property_read_u32(dev, "bus-frequency", &priv->clock))
829 priv->clock = 12500000;
830 if (priv->clock == 0 || priv->clock > (div_clock / 2)) {
831 dev_err(dev, "Invalid frequency %d\n", priv->clock);
835 priv->regs = devm_platform_ioremap_resource(pdev, 0);
836 if (IS_ERR(priv->regs))
837 return PTR_ERR(priv->regs);
838 priv->properties = device_get_match_data(dev);
839 priv->in.is_input = true;
841 /* Get rest of device properties */
842 ret = microchip_sgpio_get_ports(priv);
846 nbanks = device_get_child_node_count(dev);
848 dev_err(dev, "Must have 2 banks (have %d)\n", nbanks);
853 device_for_each_child_node(dev, fwnode) {
854 ret = microchip_sgpio_register_bank(dev, priv, fwnode, i++);
856 fwnode_handle_put(fwnode);
861 if (priv->in.gpio.ngpio != priv->out.gpio.ngpio) {
862 dev_err(dev, "Banks must have same GPIO count\n");
866 sgpio_configure_bitstream(priv);
868 val = max(2U, div_clock / priv->clock);
869 sgpio_configure_clock(priv, val);
871 for (port = 0; port < SGPIO_BITS_PER_WORD; port++)
872 sgpio_writel(priv, 0, REG_PORT_CONFIG, port);
873 sgpio_writel(priv, priv->ports, REG_PORT_ENABLE, 0);
878 static const struct of_device_id microchip_sgpio_gpio_of_match[] = {
880 .compatible = "microchip,sparx5-sgpio",
881 .data = &properties_sparx5,
883 .compatible = "mscc,luton-sgpio",
884 .data = &properties_luton,
886 .compatible = "mscc,ocelot-sgpio",
887 .data = &properties_ocelot,
893 static struct platform_driver microchip_sgpio_pinctrl_driver = {
895 .name = "pinctrl-microchip-sgpio",
896 .of_match_table = microchip_sgpio_gpio_of_match,
897 .suppress_bind_attrs = true,
899 .probe = microchip_sgpio_probe,
901 builtin_platform_driver(microchip_sgpio_pinctrl_driver);