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/mfd/ocelot.h>
16 #include <linux/mod_devicetable.h>
17 #include <linux/module.h>
18 #include <linux/platform_device.h>
19 #include <linux/property.h>
20 #include <linux/regmap.h>
21 #include <linux/reset.h>
22 #include <linux/spinlock.h>
24 #include <linux/pinctrl/pinconf.h>
25 #include <linux/pinctrl/pinmux.h>
30 #define SGPIO_BITS_PER_WORD 32
31 #define SGPIO_MAX_BITS 4
32 #define SGPIO_SRC_BITS 3 /* 3 bit wide field per pin */
55 SGPIO_FLAGS_HAS_IRQ = BIT(0),
58 struct sgpio_properties {
64 #define SGPIO_LUTON_AUTO_REPEAT BIT(5)
65 #define SGPIO_LUTON_PORT_WIDTH GENMASK(3, 2)
66 #define SGPIO_LUTON_CLK_FREQ GENMASK(11, 0)
67 #define SGPIO_LUTON_BIT_SOURCE GENMASK(11, 0)
69 #define SGPIO_OCELOT_AUTO_REPEAT BIT(10)
70 #define SGPIO_OCELOT_SINGLE_SHOT BIT(11)
71 #define SGPIO_OCELOT_PORT_WIDTH GENMASK(8, 7)
72 #define SGPIO_OCELOT_CLK_FREQ GENMASK(19, 8)
73 #define SGPIO_OCELOT_BIT_SOURCE GENMASK(23, 12)
75 #define SGPIO_SPARX5_AUTO_REPEAT BIT(6)
76 #define SGPIO_SPARX5_SINGLE_SHOT BIT(7)
77 #define SGPIO_SPARX5_PORT_WIDTH GENMASK(4, 3)
78 #define SGPIO_SPARX5_CLK_FREQ GENMASK(19, 8)
79 #define SGPIO_SPARX5_BIT_SOURCE GENMASK(23, 12)
81 #define SGPIO_MASTER_INTR_ENA BIT(0)
83 #define SGPIO_INT_TRG_LEVEL 0
84 #define SGPIO_INT_TRG_EDGE 1
85 #define SGPIO_INT_TRG_EDGE_FALL 2
86 #define SGPIO_INT_TRG_EDGE_RISE 3
88 #define SGPIO_TRG_LEVEL_HIGH 0
89 #define SGPIO_TRG_LEVEL_LOW 1
91 static const struct sgpio_properties properties_luton = {
92 .arch = SGPIO_ARCH_LUTON,
93 .regoff = { 0x00, 0x09, 0x29, 0x2a, 0x2b },
96 static const struct sgpio_properties properties_ocelot = {
97 .arch = SGPIO_ARCH_OCELOT,
98 .regoff = { 0x00, 0x06, 0x26, 0x04, 0x05 },
101 static const struct sgpio_properties properties_sparx5 = {
102 .arch = SGPIO_ARCH_SPARX5,
103 .flags = SGPIO_FLAGS_HAS_IRQ,
104 .regoff = { 0x00, 0x06, 0x26, 0x04, 0x05, 0x2a, 0x32, 0x3a, 0x3e, 0x42 },
107 static const char * const functions[] = { "gpio" };
110 struct sgpio_priv *priv;
112 struct gpio_chip gpio;
113 struct pinctrl_desc pctl_desc;
118 struct sgpio_bank in;
119 struct sgpio_bank out;
124 const struct sgpio_properties *properties;
126 /* protects the config register and single shot mode */
127 struct mutex poll_lock;
130 struct sgpio_port_addr {
135 static inline void sgpio_pin_to_addr(struct sgpio_priv *priv, int pin,
136 struct sgpio_port_addr *addr)
138 addr->port = pin / priv->bitcount;
139 addr->bit = pin % priv->bitcount;
142 static inline int sgpio_addr_to_pin(struct sgpio_priv *priv, int port, int bit)
144 return bit + port * priv->bitcount;
147 static inline u32 sgpio_get_addr(struct sgpio_priv *priv, u32 rno, u32 off)
149 return (priv->properties->regoff[rno] + off) *
150 regmap_get_reg_stride(priv->regs);
153 static u32 sgpio_readl(struct sgpio_priv *priv, u32 rno, u32 off)
155 u32 addr = sgpio_get_addr(priv, rno, off);
159 ret = regmap_read(priv->regs, addr, &val);
160 WARN_ONCE(ret, "error reading sgpio reg %d\n", ret);
165 static void sgpio_writel(struct sgpio_priv *priv,
166 u32 val, u32 rno, u32 off)
168 u32 addr = sgpio_get_addr(priv, rno, off);
171 ret = regmap_write(priv->regs, addr, val);
172 WARN_ONCE(ret, "error writing sgpio reg %d\n", ret);
175 static inline void sgpio_clrsetbits(struct sgpio_priv *priv,
176 u32 rno, u32 off, u32 clear, u32 set)
178 u32 addr = sgpio_get_addr(priv, rno, off);
181 ret = regmap_update_bits(priv->regs, addr, clear | set, set);
182 WARN_ONCE(ret, "error updating sgpio reg %d\n", ret);
185 static inline void sgpio_configure_bitstream(struct sgpio_priv *priv)
187 int width = priv->bitcount - 1;
190 switch (priv->properties->arch) {
191 case SGPIO_ARCH_LUTON:
192 clr = SGPIO_LUTON_PORT_WIDTH;
193 set = SGPIO_LUTON_AUTO_REPEAT |
194 FIELD_PREP(SGPIO_LUTON_PORT_WIDTH, width);
196 case SGPIO_ARCH_OCELOT:
197 clr = SGPIO_OCELOT_PORT_WIDTH;
198 set = SGPIO_OCELOT_AUTO_REPEAT |
199 FIELD_PREP(SGPIO_OCELOT_PORT_WIDTH, width);
201 case SGPIO_ARCH_SPARX5:
202 clr = SGPIO_SPARX5_PORT_WIDTH;
203 set = SGPIO_SPARX5_AUTO_REPEAT |
204 FIELD_PREP(SGPIO_SPARX5_PORT_WIDTH, width);
209 sgpio_clrsetbits(priv, REG_SIO_CONFIG, 0, clr, set);
212 static inline void sgpio_configure_clock(struct sgpio_priv *priv, u32 clkfrq)
216 switch (priv->properties->arch) {
217 case SGPIO_ARCH_LUTON:
218 clr = SGPIO_LUTON_CLK_FREQ;
219 set = FIELD_PREP(SGPIO_LUTON_CLK_FREQ, clkfrq);
221 case SGPIO_ARCH_OCELOT:
222 clr = SGPIO_OCELOT_CLK_FREQ;
223 set = FIELD_PREP(SGPIO_OCELOT_CLK_FREQ, clkfrq);
225 case SGPIO_ARCH_SPARX5:
226 clr = SGPIO_SPARX5_CLK_FREQ;
227 set = FIELD_PREP(SGPIO_SPARX5_CLK_FREQ, clkfrq);
232 sgpio_clrsetbits(priv, REG_SIO_CLOCK, 0, clr, set);
235 static int sgpio_single_shot(struct sgpio_priv *priv)
237 u32 addr = sgpio_get_addr(priv, REG_SIO_CONFIG, 0);
240 unsigned int single_shot;
241 unsigned int auto_repeat;
243 switch (priv->properties->arch) {
244 case SGPIO_ARCH_LUTON:
245 /* not supported for now */
247 case SGPIO_ARCH_OCELOT:
248 single_shot = SGPIO_OCELOT_SINGLE_SHOT;
249 auto_repeat = SGPIO_OCELOT_AUTO_REPEAT;
251 case SGPIO_ARCH_SPARX5:
252 single_shot = SGPIO_SPARX5_SINGLE_SHOT;
253 auto_repeat = SGPIO_SPARX5_AUTO_REPEAT;
260 * Trigger immediate burst. This only works when auto repeat is turned
261 * off. Otherwise, the single shot bit will never be cleared by the
262 * hardware. Measurements showed that an update might take as long as
263 * the burst gap. On a LAN9668 this is about 50ms for the largest
265 * After the manual burst, reenable the auto repeat mode again.
267 mutex_lock(&priv->poll_lock);
268 ret = regmap_update_bits(priv->regs, addr, single_shot | auto_repeat,
273 ret = regmap_read_poll_timeout(priv->regs, addr, ctrl,
274 !(ctrl & single_shot), 100, 60000);
276 /* reenable auto repeat mode even if there was an error */
277 ret2 = regmap_update_bits(priv->regs, addr, auto_repeat, auto_repeat);
279 mutex_unlock(&priv->poll_lock);
284 static int sgpio_output_set(struct sgpio_priv *priv,
285 struct sgpio_port_addr *addr,
288 unsigned int bit = SGPIO_SRC_BITS * addr->bit;
289 u32 reg = sgpio_get_addr(priv, REG_PORT_CONFIG, addr->port);
294 switch (priv->properties->arch) {
295 case SGPIO_ARCH_LUTON:
296 clr = FIELD_PREP(SGPIO_LUTON_BIT_SOURCE, BIT(bit));
297 set = FIELD_PREP(SGPIO_LUTON_BIT_SOURCE, value << bit);
299 case SGPIO_ARCH_OCELOT:
300 clr = FIELD_PREP(SGPIO_OCELOT_BIT_SOURCE, BIT(bit));
301 set = FIELD_PREP(SGPIO_OCELOT_BIT_SOURCE, value << bit);
303 case SGPIO_ARCH_SPARX5:
304 clr = FIELD_PREP(SGPIO_SPARX5_BIT_SOURCE, BIT(bit));
305 set = FIELD_PREP(SGPIO_SPARX5_BIT_SOURCE, value << bit);
311 ret = regmap_update_bits_check(priv->regs, reg, clr | set, set,
317 ret = sgpio_single_shot(priv);
325 static int sgpio_output_get(struct sgpio_priv *priv,
326 struct sgpio_port_addr *addr)
328 u32 val, portval = sgpio_readl(priv, REG_PORT_CONFIG, addr->port);
329 unsigned int bit = SGPIO_SRC_BITS * addr->bit;
331 switch (priv->properties->arch) {
332 case SGPIO_ARCH_LUTON:
333 val = FIELD_GET(SGPIO_LUTON_BIT_SOURCE, portval);
335 case SGPIO_ARCH_OCELOT:
336 val = FIELD_GET(SGPIO_OCELOT_BIT_SOURCE, portval);
338 case SGPIO_ARCH_SPARX5:
339 val = FIELD_GET(SGPIO_SPARX5_BIT_SOURCE, portval);
345 return !!(val & BIT(bit));
348 static int sgpio_input_get(struct sgpio_priv *priv,
349 struct sgpio_port_addr *addr)
351 return !!(sgpio_readl(priv, REG_INPUT_DATA, addr->bit) & BIT(addr->port));
354 static int sgpio_pinconf_get(struct pinctrl_dev *pctldev,
355 unsigned int pin, unsigned long *config)
357 struct sgpio_bank *bank = pinctrl_dev_get_drvdata(pctldev);
358 u32 param = pinconf_to_config_param(*config);
359 struct sgpio_priv *priv = bank->priv;
360 struct sgpio_port_addr addr;
363 sgpio_pin_to_addr(priv, pin, &addr);
366 case PIN_CONFIG_INPUT_ENABLE:
367 val = bank->is_input;
370 case PIN_CONFIG_OUTPUT_ENABLE:
371 val = !bank->is_input;
374 case PIN_CONFIG_OUTPUT:
377 val = sgpio_output_get(priv, &addr);
384 *config = pinconf_to_config_packed(param, val);
389 static int sgpio_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
390 unsigned long *configs, unsigned int num_configs)
392 struct sgpio_bank *bank = pinctrl_dev_get_drvdata(pctldev);
393 struct sgpio_priv *priv = bank->priv;
394 struct sgpio_port_addr addr;
398 sgpio_pin_to_addr(priv, pin, &addr);
400 for (cfg = 0; cfg < num_configs; cfg++) {
401 param = pinconf_to_config_param(configs[cfg]);
402 arg = pinconf_to_config_argument(configs[cfg]);
405 case PIN_CONFIG_OUTPUT:
408 err = sgpio_output_set(priv, &addr, arg);
419 static const struct pinconf_ops sgpio_confops = {
421 .pin_config_get = sgpio_pinconf_get,
422 .pin_config_set = sgpio_pinconf_set,
423 .pin_config_config_dbg_show = pinconf_generic_dump_config,
426 static int sgpio_get_functions_count(struct pinctrl_dev *pctldev)
431 static const char *sgpio_get_function_name(struct pinctrl_dev *pctldev,
432 unsigned int function)
437 static int sgpio_get_function_groups(struct pinctrl_dev *pctldev,
438 unsigned int function,
439 const char *const **groups,
440 unsigned *const num_groups)
443 *num_groups = ARRAY_SIZE(functions);
448 static int sgpio_pinmux_set_mux(struct pinctrl_dev *pctldev,
449 unsigned int selector, unsigned int group)
454 static int sgpio_gpio_set_direction(struct pinctrl_dev *pctldev,
455 struct pinctrl_gpio_range *range,
456 unsigned int pin, bool input)
458 struct sgpio_bank *bank = pinctrl_dev_get_drvdata(pctldev);
460 return (input == bank->is_input) ? 0 : -EINVAL;
463 static int sgpio_gpio_request_enable(struct pinctrl_dev *pctldev,
464 struct pinctrl_gpio_range *range,
467 struct sgpio_bank *bank = pinctrl_dev_get_drvdata(pctldev);
468 struct sgpio_priv *priv = bank->priv;
469 struct sgpio_port_addr addr;
471 sgpio_pin_to_addr(priv, offset, &addr);
473 if ((priv->ports & BIT(addr.port)) == 0) {
474 dev_warn(priv->dev, "Request port %d.%d: Port is not enabled\n",
475 addr.port, addr.bit);
482 static const struct pinmux_ops sgpio_pmx_ops = {
483 .get_functions_count = sgpio_get_functions_count,
484 .get_function_name = sgpio_get_function_name,
485 .get_function_groups = sgpio_get_function_groups,
486 .set_mux = sgpio_pinmux_set_mux,
487 .gpio_set_direction = sgpio_gpio_set_direction,
488 .gpio_request_enable = sgpio_gpio_request_enable,
491 static int sgpio_pctl_get_groups_count(struct pinctrl_dev *pctldev)
493 struct sgpio_bank *bank = pinctrl_dev_get_drvdata(pctldev);
495 return bank->pctl_desc.npins;
498 static const char *sgpio_pctl_get_group_name(struct pinctrl_dev *pctldev,
501 struct sgpio_bank *bank = pinctrl_dev_get_drvdata(pctldev);
503 return bank->pctl_desc.pins[group].name;
506 static int sgpio_pctl_get_group_pins(struct pinctrl_dev *pctldev,
508 const unsigned int **pins,
509 unsigned int *num_pins)
511 struct sgpio_bank *bank = pinctrl_dev_get_drvdata(pctldev);
513 *pins = &bank->pctl_desc.pins[group].number;
519 static const struct pinctrl_ops sgpio_pctl_ops = {
520 .get_groups_count = sgpio_pctl_get_groups_count,
521 .get_group_name = sgpio_pctl_get_group_name,
522 .get_group_pins = sgpio_pctl_get_group_pins,
523 .dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
524 .dt_free_map = pinconf_generic_dt_free_map,
527 static int microchip_sgpio_direction_input(struct gpio_chip *gc, unsigned int gpio)
529 struct sgpio_bank *bank = gpiochip_get_data(gc);
531 /* Fixed-position function */
532 return bank->is_input ? 0 : -EINVAL;
535 static int microchip_sgpio_direction_output(struct gpio_chip *gc,
536 unsigned int gpio, int value)
538 struct sgpio_bank *bank = gpiochip_get_data(gc);
539 struct sgpio_priv *priv = bank->priv;
540 struct sgpio_port_addr addr;
542 /* Fixed-position function */
546 sgpio_pin_to_addr(priv, gpio, &addr);
548 return sgpio_output_set(priv, &addr, value);
551 static int microchip_sgpio_get_direction(struct gpio_chip *gc, unsigned int gpio)
553 struct sgpio_bank *bank = gpiochip_get_data(gc);
555 return bank->is_input ? GPIO_LINE_DIRECTION_IN : GPIO_LINE_DIRECTION_OUT;
558 static void microchip_sgpio_set_value(struct gpio_chip *gc,
559 unsigned int gpio, int value)
561 microchip_sgpio_direction_output(gc, gpio, value);
564 static int microchip_sgpio_get_value(struct gpio_chip *gc, unsigned int gpio)
566 struct sgpio_bank *bank = gpiochip_get_data(gc);
567 struct sgpio_priv *priv = bank->priv;
568 struct sgpio_port_addr addr;
570 sgpio_pin_to_addr(priv, gpio, &addr);
572 return bank->is_input ? sgpio_input_get(priv, &addr) : sgpio_output_get(priv, &addr);
575 static int microchip_sgpio_of_xlate(struct gpio_chip *gc,
576 const struct of_phandle_args *gpiospec,
579 struct sgpio_bank *bank = gpiochip_get_data(gc);
580 struct sgpio_priv *priv = bank->priv;
584 * Note that the SGIO pin is defined by *2* numbers, a port
585 * number between 0 and 31, and a bit index, 0 to 3.
587 if (gpiospec->args[0] > SGPIO_BITS_PER_WORD ||
588 gpiospec->args[1] > priv->bitcount)
591 pin = sgpio_addr_to_pin(priv, gpiospec->args[0], gpiospec->args[1]);
597 *flags = gpiospec->args[2];
602 static int microchip_sgpio_get_ports(struct sgpio_priv *priv)
604 const char *range_property_name = "microchip,sgpio-port-ranges";
605 struct device *dev = priv->dev;
606 u32 range_params[64];
609 /* Calculate port mask */
610 nranges = device_property_count_u32(dev, range_property_name);
611 if (nranges < 2 || nranges % 2 || nranges > ARRAY_SIZE(range_params)) {
612 dev_err(dev, "%s port range: '%s' property\n",
613 nranges == -EINVAL ? "Missing" : "Invalid",
614 range_property_name);
618 ret = device_property_read_u32_array(dev, range_property_name,
619 range_params, nranges);
621 dev_err(dev, "failed to parse '%s' property: %d\n",
622 range_property_name, ret);
625 for (i = 0; i < nranges; i += 2) {
628 start = range_params[i];
629 end = range_params[i + 1];
630 if (start > end || end >= SGPIO_BITS_PER_WORD) {
631 dev_err(dev, "Ill-formed port-range [%d:%d]\n",
634 priv->ports |= GENMASK(end, start);
640 static void microchip_sgpio_irq_settype(struct irq_data *data,
644 struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
645 struct sgpio_bank *bank = gpiochip_get_data(chip);
646 unsigned int gpio = irqd_to_hwirq(data);
647 struct sgpio_port_addr addr;
651 sgpio_pin_to_addr(bank->priv, gpio, &addr);
653 spin_lock_irqsave(&bank->priv->lock, flags);
655 /* Disable interrupt while changing type */
656 ena = sgpio_readl(bank->priv, REG_INT_ENABLE, addr.bit);
657 sgpio_writel(bank->priv, ena & ~BIT(addr.port), REG_INT_ENABLE, addr.bit);
659 /* Type value spread over 2 registers sets: low, high bit */
660 sgpio_clrsetbits(bank->priv, REG_INT_TRIGGER, addr.bit,
661 BIT(addr.port), (!!(type & 0x1)) << addr.port);
662 sgpio_clrsetbits(bank->priv, REG_INT_TRIGGER, SGPIO_MAX_BITS + addr.bit,
663 BIT(addr.port), (!!(type & 0x2)) << addr.port);
665 if (type == SGPIO_INT_TRG_LEVEL)
666 sgpio_clrsetbits(bank->priv, REG_INT_POLARITY, addr.bit,
667 BIT(addr.port), polarity << addr.port);
669 /* Possibly re-enable interrupts */
670 sgpio_writel(bank->priv, ena, REG_INT_ENABLE, addr.bit);
672 spin_unlock_irqrestore(&bank->priv->lock, flags);
675 static void microchip_sgpio_irq_setreg(struct irq_data *data,
679 struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
680 struct sgpio_bank *bank = gpiochip_get_data(chip);
681 unsigned int gpio = irqd_to_hwirq(data);
682 struct sgpio_port_addr addr;
684 sgpio_pin_to_addr(bank->priv, gpio, &addr);
687 sgpio_clrsetbits(bank->priv, reg, addr.bit, BIT(addr.port), 0);
689 sgpio_clrsetbits(bank->priv, reg, addr.bit, 0, BIT(addr.port));
692 static void microchip_sgpio_irq_mask(struct irq_data *data)
694 struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
696 microchip_sgpio_irq_setreg(data, REG_INT_ENABLE, true);
697 gpiochip_disable_irq(chip, data->hwirq);
700 static void microchip_sgpio_irq_unmask(struct irq_data *data)
702 struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
704 gpiochip_enable_irq(chip, data->hwirq);
705 microchip_sgpio_irq_setreg(data, REG_INT_ENABLE, false);
708 static void microchip_sgpio_irq_ack(struct irq_data *data)
710 struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
711 struct sgpio_bank *bank = gpiochip_get_data(chip);
712 unsigned int gpio = irqd_to_hwirq(data);
713 struct sgpio_port_addr addr;
715 sgpio_pin_to_addr(bank->priv, gpio, &addr);
717 sgpio_writel(bank->priv, BIT(addr.port), REG_INT_ACK, addr.bit);
720 static int microchip_sgpio_irq_set_type(struct irq_data *data, unsigned int type)
723 case IRQ_TYPE_EDGE_BOTH:
724 irq_set_handler_locked(data, handle_edge_irq);
725 microchip_sgpio_irq_settype(data, SGPIO_INT_TRG_EDGE, 0);
727 case IRQ_TYPE_EDGE_RISING:
728 irq_set_handler_locked(data, handle_edge_irq);
729 microchip_sgpio_irq_settype(data, SGPIO_INT_TRG_EDGE_RISE, 0);
731 case IRQ_TYPE_EDGE_FALLING:
732 irq_set_handler_locked(data, handle_edge_irq);
733 microchip_sgpio_irq_settype(data, SGPIO_INT_TRG_EDGE_FALL, 0);
735 case IRQ_TYPE_LEVEL_HIGH:
736 irq_set_handler_locked(data, handle_level_irq);
737 microchip_sgpio_irq_settype(data, SGPIO_INT_TRG_LEVEL, SGPIO_TRG_LEVEL_HIGH);
739 case IRQ_TYPE_LEVEL_LOW:
740 irq_set_handler_locked(data, handle_level_irq);
741 microchip_sgpio_irq_settype(data, SGPIO_INT_TRG_LEVEL, SGPIO_TRG_LEVEL_LOW);
750 static const struct irq_chip microchip_sgpio_irqchip = {
752 .irq_mask = microchip_sgpio_irq_mask,
753 .irq_ack = microchip_sgpio_irq_ack,
754 .irq_unmask = microchip_sgpio_irq_unmask,
755 .irq_set_type = microchip_sgpio_irq_set_type,
756 .flags = IRQCHIP_IMMUTABLE,
757 GPIOCHIP_IRQ_RESOURCE_HELPERS,
760 static void sgpio_irq_handler(struct irq_desc *desc)
762 struct irq_chip *parent_chip = irq_desc_get_chip(desc);
763 struct gpio_chip *chip = irq_desc_get_handler_data(desc);
764 struct sgpio_bank *bank = gpiochip_get_data(chip);
765 struct sgpio_priv *priv = bank->priv;
769 for (bit = 0; bit < priv->bitcount; bit++) {
770 val = sgpio_readl(priv, REG_INT_IDENT, bit);
774 chained_irq_enter(parent_chip, desc);
776 for_each_set_bit(port, &val, SGPIO_BITS_PER_WORD) {
777 gpio = sgpio_addr_to_pin(priv, port, bit);
778 generic_handle_domain_irq(chip->irq.domain, gpio);
781 chained_irq_exit(parent_chip, desc);
785 static int microchip_sgpio_register_bank(struct device *dev,
786 struct sgpio_priv *priv,
787 struct fwnode_handle *fwnode,
790 struct pinctrl_pin_desc *pins;
791 struct pinctrl_desc *pctl_desc;
792 struct pinctrl_dev *pctldev;
793 struct sgpio_bank *bank;
794 struct gpio_chip *gc;
798 /* Get overall bank struct */
799 bank = (bankno == 0) ? &priv->in : &priv->out;
802 if (fwnode_property_read_u32(fwnode, "ngpios", &ngpios)) {
803 dev_info(dev, "failed to get number of gpios for bank%d\n",
808 priv->bitcount = ngpios / SGPIO_BITS_PER_WORD;
809 if (priv->bitcount > SGPIO_MAX_BITS) {
810 dev_err(dev, "Bit width exceeds maximum (%d)\n",
815 pctl_desc = &bank->pctl_desc;
816 pctl_desc->name = devm_kasprintf(dev, GFP_KERNEL, "%s-%sput",
818 bank->is_input ? "in" : "out");
819 if (!pctl_desc->name)
822 pctl_desc->pctlops = &sgpio_pctl_ops;
823 pctl_desc->pmxops = &sgpio_pmx_ops;
824 pctl_desc->confops = &sgpio_confops;
825 pctl_desc->owner = THIS_MODULE;
827 pins = devm_kzalloc(dev, sizeof(*pins)*ngpios, GFP_KERNEL);
831 pctl_desc->npins = ngpios;
832 pctl_desc->pins = pins;
834 for (i = 0; i < ngpios; i++) {
835 struct sgpio_port_addr addr;
837 sgpio_pin_to_addr(priv, i, &addr);
840 pins[i].name = devm_kasprintf(dev, GFP_KERNEL,
842 bank->is_input ? 'I' : 'O',
843 addr.port, addr.bit);
848 pctldev = devm_pinctrl_register(dev, pctl_desc, bank);
850 return dev_err_probe(dev, PTR_ERR(pctldev), "Failed to register pinctrl\n");
853 gc->label = pctl_desc->name;
856 gc->owner = THIS_MODULE;
857 gc->get_direction = microchip_sgpio_get_direction;
858 gc->direction_input = microchip_sgpio_direction_input;
859 gc->direction_output = microchip_sgpio_direction_output;
860 gc->get = microchip_sgpio_get_value;
861 gc->set = microchip_sgpio_set_value;
862 gc->request = gpiochip_generic_request;
863 gc->free = gpiochip_generic_free;
864 gc->of_xlate = microchip_sgpio_of_xlate;
865 gc->of_gpio_n_cells = 3;
868 gc->can_sleep = !bank->is_input;
870 if (bank->is_input && priv->properties->flags & SGPIO_FLAGS_HAS_IRQ) {
873 irq = fwnode_irq_get(fwnode, 0);
875 struct gpio_irq_chip *girq = &gc->irq;
877 gpio_irq_chip_set_chip(girq, µchip_sgpio_irqchip);
878 girq->parent_handler = sgpio_irq_handler;
879 girq->num_parents = 1;
880 girq->parents = devm_kcalloc(dev, 1,
881 sizeof(*girq->parents),
885 girq->parents[0] = irq;
886 girq->default_type = IRQ_TYPE_NONE;
887 girq->handler = handle_bad_irq;
889 /* Disable all individual pins */
890 for (i = 0; i < SGPIO_MAX_BITS; i++)
891 sgpio_writel(priv, 0, REG_INT_ENABLE, i);
893 sgpio_clrsetbits(priv, REG_SIO_CONFIG, 0, 0, SGPIO_MASTER_INTR_ENA);
897 ret = devm_gpiochip_add_data(dev, gc, bank);
899 dev_err(dev, "Failed to register: ret %d\n", ret);
904 static int microchip_sgpio_probe(struct platform_device *pdev)
906 int div_clock = 0, ret, port, i, nbanks;
907 struct device *dev = &pdev->dev;
908 struct fwnode_handle *fwnode;
909 struct reset_control *reset;
910 struct sgpio_priv *priv;
913 struct regmap_config regmap_config = {
919 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
924 spin_lock_init(&priv->lock);
925 mutex_init(&priv->poll_lock);
927 reset = devm_reset_control_get_optional_shared(&pdev->dev, "switch");
929 return dev_err_probe(dev, PTR_ERR(reset), "Failed to get reset\n");
930 reset_control_reset(reset);
932 clk = devm_clk_get(dev, NULL);
934 return dev_err_probe(dev, PTR_ERR(clk), "Failed to get clock\n");
936 div_clock = clk_get_rate(clk);
937 if (device_property_read_u32(dev, "bus-frequency", &priv->clock))
938 priv->clock = 12500000;
939 if (priv->clock == 0 || priv->clock > (div_clock / 2)) {
940 dev_err(dev, "Invalid frequency %d\n", priv->clock);
944 priv->regs = ocelot_regmap_from_resource(pdev, 0, ®map_config);
945 if (IS_ERR(priv->regs))
946 return PTR_ERR(priv->regs);
948 priv->properties = device_get_match_data(dev);
949 priv->in.is_input = true;
951 /* Get rest of device properties */
952 ret = microchip_sgpio_get_ports(priv);
956 nbanks = device_get_child_node_count(dev);
958 dev_err(dev, "Must have 2 banks (have %d)\n", nbanks);
963 device_for_each_child_node(dev, fwnode) {
964 ret = microchip_sgpio_register_bank(dev, priv, fwnode, i++);
966 fwnode_handle_put(fwnode);
971 if (priv->in.gpio.ngpio != priv->out.gpio.ngpio) {
972 dev_err(dev, "Banks must have same GPIO count\n");
976 sgpio_configure_bitstream(priv);
978 val = max(2U, div_clock / priv->clock);
979 sgpio_configure_clock(priv, val);
981 for (port = 0; port < SGPIO_BITS_PER_WORD; port++)
982 sgpio_writel(priv, 0, REG_PORT_CONFIG, port);
983 sgpio_writel(priv, priv->ports, REG_PORT_ENABLE, 0);
988 static const struct of_device_id microchip_sgpio_gpio_of_match[] = {
990 .compatible = "microchip,sparx5-sgpio",
991 .data = &properties_sparx5,
993 .compatible = "mscc,luton-sgpio",
994 .data = &properties_luton,
996 .compatible = "mscc,ocelot-sgpio",
997 .data = &properties_ocelot,
1002 MODULE_DEVICE_TABLE(of, microchip_sgpio_gpio_of_match);
1004 static struct platform_driver microchip_sgpio_pinctrl_driver = {
1006 .name = "pinctrl-microchip-sgpio",
1007 .of_match_table = microchip_sgpio_gpio_of_match,
1008 .suppress_bind_attrs = true,
1010 .probe = microchip_sgpio_probe,
1012 module_platform_driver(microchip_sgpio_pinctrl_driver);
1014 MODULE_DESCRIPTION("Microchip SGPIO Pinctrl Driver");
1015 MODULE_LICENSE("GPL");