Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

pinctrl: pinctrl-microchip-sgpio: Add pinctrl driver for Microsemi Serial GPIO

This adds a pinctrl driver for the Microsemi/Microchip Serial GPIO
(SGPIO) device used in various SoC's.

The driver is added as a pinctrl driver, albeit only having just GPIO
support currently. The hardware supports other functions that will be
added following.

Signed-off-by: Lars Povlsen <lars.povlsen@microchip.com>
Link: https://lore.kernel.org/r/20201113145151.68900-3-lars.povlsen@microchip.com
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>

authored by

Lars Povlsen and committed by
Linus Walleij
7e5ea974 ce4d7816

+727
+1
MAINTAINERS
··· 2117 2117 S: Supported 2118 2118 T: git git://github.com/microchip-ung/linux-upstream.git 2119 2119 F: arch/arm64/boot/dts/microchip/ 2120 + F: drivers/pinctrl/pinctrl-microchip-sgpio.c 2120 2121 N: sparx5 2121 2122 2122 2123 ARM/MIOA701 MACHINE SUPPORT
+16
drivers/pinctrl/Kconfig
··· 374 374 select OF_GPIO 375 375 select REGMAP_MMIO 376 376 377 + config PINCTRL_MICROCHIP_SGPIO 378 + bool "Pinctrl driver for Microsemi/Microchip Serial GPIO" 379 + depends on HAS_IOMEM 380 + select GPIOLIB 381 + select GENERIC_PINCONF 382 + select GENERIC_PINCTRL_GROUPS 383 + select GENERIC_PINMUX_FUNCTIONS 384 + help 385 + Support for the serial GPIO interface used on Microsemi and 386 + Microchip SoC's. By using a serial interface, the SIO 387 + controller significantly extends the number of available 388 + GPIOs with a minimum number of additional pins on the 389 + device. The primary purpose of the SIO controller is to 390 + connect control signals from SFP modules and to act as an 391 + LED controller. 392 + 377 393 source "drivers/pinctrl/actions/Kconfig" 378 394 source "drivers/pinctrl/aspeed/Kconfig" 379 395 source "drivers/pinctrl/bcm/Kconfig"
+1
drivers/pinctrl/Makefile
··· 46 46 obj-$(CONFIG_PINCTRL_INGENIC) += pinctrl-ingenic.o 47 47 obj-$(CONFIG_PINCTRL_RK805) += pinctrl-rk805.o 48 48 obj-$(CONFIG_PINCTRL_OCELOT) += pinctrl-ocelot.o 49 + obj-$(CONFIG_PINCTRL_MICROCHIP_SGPIO) += pinctrl-microchip-sgpio.o 49 50 obj-$(CONFIG_PINCTRL_EQUILIBRIUM) += pinctrl-equilibrium.o 50 51 51 52 obj-y += actions/
+709
drivers/pinctrl/pinctrl-microchip-sgpio.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-or-later 2 + /* 3 + * Microsemi/Microchip SoCs serial gpio driver 4 + * 5 + * Author: Lars Povlsen <lars.povlsen@microchip.com> 6 + * 7 + * Copyright (c) 2020 Microchip Technology Inc. and its subsidiaries. 8 + */ 9 + 10 + #include <linux/bitfield.h> 11 + #include <linux/bits.h> 12 + #include <linux/clk.h> 13 + #include <linux/gpio/driver.h> 14 + #include <linux/io.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 + 21 + #include "core.h" 22 + #include "pinconf.h" 23 + 24 + #define SGPIO_BITS_PER_WORD 32 25 + #define SGPIO_MAX_BITS 4 26 + #define SGPIO_SRC_BITS 3 /* 3 bit wide field per pin */ 27 + 28 + enum { 29 + REG_INPUT_DATA, 30 + REG_PORT_CONFIG, 31 + REG_PORT_ENABLE, 32 + REG_SIO_CONFIG, 33 + REG_SIO_CLOCK, 34 + MAXREG 35 + }; 36 + 37 + enum { 38 + SGPIO_ARCH_LUTON, 39 + SGPIO_ARCH_OCELOT, 40 + SGPIO_ARCH_SPARX5, 41 + }; 42 + 43 + struct sgpio_properties { 44 + int arch; 45 + u8 regoff[MAXREG]; 46 + }; 47 + 48 + #define SGPIO_LUTON_AUTO_REPEAT BIT(5) 49 + #define SGPIO_LUTON_PORT_WIDTH GENMASK(3, 2) 50 + #define SGPIO_LUTON_CLK_FREQ GENMASK(11, 0) 51 + #define SGPIO_LUTON_BIT_SOURCE GENMASK(11, 0) 52 + 53 + #define SGPIO_OCELOT_AUTO_REPEAT BIT(10) 54 + #define SGPIO_OCELOT_PORT_WIDTH GENMASK(8, 7) 55 + #define SGPIO_OCELOT_CLK_FREQ GENMASK(19, 8) 56 + #define SGPIO_OCELOT_BIT_SOURCE GENMASK(23, 12) 57 + 58 + #define SGPIO_SPARX5_AUTO_REPEAT BIT(6) 59 + #define SGPIO_SPARX5_PORT_WIDTH GENMASK(4, 3) 60 + #define SGPIO_SPARX5_CLK_FREQ GENMASK(19, 8) 61 + #define SGPIO_SPARX5_BIT_SOURCE GENMASK(23, 12) 62 + 63 + const struct sgpio_properties properties_luton = { 64 + .arch = SGPIO_ARCH_LUTON, 65 + .regoff = { 0x00, 0x09, 0x29, 0x2a, 0x2b }, 66 + }; 67 + 68 + const struct sgpio_properties properties_ocelot = { 69 + .arch = SGPIO_ARCH_OCELOT, 70 + .regoff = { 0x00, 0x06, 0x26, 0x04, 0x05 }, 71 + }; 72 + 73 + const struct sgpio_properties properties_sparx5 = { 74 + .arch = SGPIO_ARCH_SPARX5, 75 + .regoff = { 0x00, 0x06, 0x26, 0x04, 0x05 }, 76 + }; 77 + 78 + static const char * const functions[] = { "gpio" }; 79 + 80 + struct sgpio_bank { 81 + struct sgpio_priv *priv; 82 + bool is_input; 83 + struct gpio_chip gpio; 84 + struct pinctrl_desc pctl_desc; 85 + }; 86 + 87 + struct sgpio_priv { 88 + struct device *dev; 89 + struct sgpio_bank in; 90 + struct sgpio_bank out; 91 + u32 bitcount; 92 + u32 ports; 93 + u32 clock; 94 + u32 __iomem *regs; 95 + const struct sgpio_properties *properties; 96 + }; 97 + 98 + struct sgpio_port_addr { 99 + u8 port; 100 + u8 bit; 101 + }; 102 + 103 + static inline void sgpio_pin_to_addr(struct sgpio_priv *priv, int pin, 104 + struct sgpio_port_addr *addr) 105 + { 106 + addr->port = pin / priv->bitcount; 107 + addr->bit = pin % priv->bitcount; 108 + } 109 + 110 + static inline u32 sgpio_readl(struct sgpio_priv *priv, u32 rno, u32 off) 111 + { 112 + u32 __iomem *reg = &priv->regs[priv->properties->regoff[rno] + off]; 113 + 114 + return readl(reg); 115 + } 116 + 117 + static inline void sgpio_writel(struct sgpio_priv *priv, 118 + u32 val, u32 rno, u32 off) 119 + { 120 + u32 __iomem *reg = &priv->regs[priv->properties->regoff[rno] + off]; 121 + 122 + writel(val, reg); 123 + } 124 + 125 + static inline void sgpio_clrsetbits(struct sgpio_priv *priv, 126 + u32 rno, u32 off, u32 clear, u32 set) 127 + { 128 + u32 __iomem *reg = &priv->regs[priv->properties->regoff[rno] + off]; 129 + u32 val = readl(reg); 130 + 131 + val &= ~clear; 132 + val |= set; 133 + 134 + writel(val, reg); 135 + } 136 + 137 + static inline void sgpio_configure_bitstream(struct sgpio_priv *priv) 138 + { 139 + int width = priv->bitcount - 1; 140 + u32 clr, set; 141 + 142 + switch (priv->properties->arch) { 143 + case SGPIO_ARCH_LUTON: 144 + clr = SGPIO_LUTON_PORT_WIDTH; 145 + set = SGPIO_LUTON_AUTO_REPEAT | 146 + FIELD_PREP(SGPIO_LUTON_PORT_WIDTH, width); 147 + break; 148 + case SGPIO_ARCH_OCELOT: 149 + clr = SGPIO_OCELOT_PORT_WIDTH; 150 + set = SGPIO_OCELOT_AUTO_REPEAT | 151 + FIELD_PREP(SGPIO_OCELOT_PORT_WIDTH, width); 152 + break; 153 + case SGPIO_ARCH_SPARX5: 154 + clr = SGPIO_SPARX5_PORT_WIDTH; 155 + set = SGPIO_SPARX5_AUTO_REPEAT | 156 + FIELD_PREP(SGPIO_SPARX5_PORT_WIDTH, width); 157 + break; 158 + default: 159 + return; 160 + } 161 + sgpio_clrsetbits(priv, REG_SIO_CONFIG, 0, clr, set); 162 + } 163 + 164 + static inline void sgpio_configure_clock(struct sgpio_priv *priv, u32 clkfrq) 165 + { 166 + u32 clr, set; 167 + 168 + switch (priv->properties->arch) { 169 + case SGPIO_ARCH_LUTON: 170 + clr = SGPIO_LUTON_CLK_FREQ; 171 + set = FIELD_PREP(SGPIO_LUTON_CLK_FREQ, clkfrq); 172 + break; 173 + case SGPIO_ARCH_OCELOT: 174 + clr = SGPIO_OCELOT_CLK_FREQ; 175 + set = FIELD_PREP(SGPIO_OCELOT_CLK_FREQ, clkfrq); 176 + break; 177 + case SGPIO_ARCH_SPARX5: 178 + clr = SGPIO_SPARX5_CLK_FREQ; 179 + set = FIELD_PREP(SGPIO_SPARX5_CLK_FREQ, clkfrq); 180 + break; 181 + default: 182 + return; 183 + } 184 + sgpio_clrsetbits(priv, REG_SIO_CLOCK, 0, clr, set); 185 + } 186 + 187 + static void sgpio_output_set(struct sgpio_priv *priv, 188 + struct sgpio_port_addr *addr, 189 + int value) 190 + { 191 + unsigned int bit = SGPIO_SRC_BITS * addr->bit; 192 + u32 clr, set; 193 + 194 + switch (priv->properties->arch) { 195 + case SGPIO_ARCH_LUTON: 196 + clr = FIELD_PREP(SGPIO_LUTON_BIT_SOURCE, BIT(bit)); 197 + set = FIELD_PREP(SGPIO_LUTON_BIT_SOURCE, value << bit); 198 + break; 199 + case SGPIO_ARCH_OCELOT: 200 + clr = FIELD_PREP(SGPIO_OCELOT_BIT_SOURCE, BIT(bit)); 201 + set = FIELD_PREP(SGPIO_OCELOT_BIT_SOURCE, value << bit); 202 + break; 203 + case SGPIO_ARCH_SPARX5: 204 + clr = FIELD_PREP(SGPIO_SPARX5_BIT_SOURCE, BIT(bit)); 205 + set = FIELD_PREP(SGPIO_SPARX5_BIT_SOURCE, value << bit); 206 + break; 207 + default: 208 + return; 209 + } 210 + sgpio_clrsetbits(priv, REG_PORT_CONFIG, addr->port, clr, set); 211 + } 212 + 213 + static int sgpio_output_get(struct sgpio_priv *priv, 214 + struct sgpio_port_addr *addr) 215 + { 216 + u32 val, portval = sgpio_readl(priv, REG_PORT_CONFIG, addr->port); 217 + unsigned int bit = SGPIO_SRC_BITS * addr->bit; 218 + 219 + switch (priv->properties->arch) { 220 + case SGPIO_ARCH_LUTON: 221 + val = FIELD_GET(SGPIO_LUTON_BIT_SOURCE, portval); 222 + break; 223 + case SGPIO_ARCH_OCELOT: 224 + val = FIELD_GET(SGPIO_OCELOT_BIT_SOURCE, portval); 225 + break; 226 + case SGPIO_ARCH_SPARX5: 227 + val = FIELD_GET(SGPIO_SPARX5_BIT_SOURCE, portval); 228 + break; 229 + default: 230 + val = 0; 231 + break; 232 + } 233 + return !!(val & BIT(bit)); 234 + } 235 + 236 + static int sgpio_input_get(struct sgpio_priv *priv, 237 + struct sgpio_port_addr *addr) 238 + { 239 + return !!(sgpio_readl(priv, REG_INPUT_DATA, addr->bit) & BIT(addr->port)); 240 + } 241 + 242 + static int sgpio_pinconf_get(struct pinctrl_dev *pctldev, 243 + unsigned int pin, unsigned long *config) 244 + { 245 + struct sgpio_bank *bank = pinctrl_dev_get_drvdata(pctldev); 246 + u32 param = pinconf_to_config_param(*config); 247 + struct sgpio_priv *priv = bank->priv; 248 + struct sgpio_port_addr addr; 249 + int val; 250 + 251 + sgpio_pin_to_addr(priv, pin, &addr); 252 + 253 + switch (param) { 254 + case PIN_CONFIG_INPUT_ENABLE: 255 + val = bank->is_input; 256 + break; 257 + 258 + case PIN_CONFIG_OUTPUT_ENABLE: 259 + val = !bank->is_input; 260 + break; 261 + 262 + case PIN_CONFIG_OUTPUT: 263 + if (bank->is_input) 264 + return -EINVAL; 265 + val = sgpio_output_get(priv, &addr); 266 + break; 267 + 268 + default: 269 + return -ENOTSUPP; 270 + } 271 + 272 + *config = pinconf_to_config_packed(param, val); 273 + 274 + return 0; 275 + } 276 + 277 + static int sgpio_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin, 278 + unsigned long *configs, unsigned int num_configs) 279 + { 280 + struct sgpio_bank *bank = pinctrl_dev_get_drvdata(pctldev); 281 + struct sgpio_priv *priv = bank->priv; 282 + struct sgpio_port_addr addr; 283 + int cfg, err = 0; 284 + u32 param, arg; 285 + 286 + sgpio_pin_to_addr(priv, pin, &addr); 287 + 288 + for (cfg = 0; cfg < num_configs; cfg++) { 289 + param = pinconf_to_config_param(configs[cfg]); 290 + arg = pinconf_to_config_argument(configs[cfg]); 291 + 292 + switch (param) { 293 + case PIN_CONFIG_OUTPUT: 294 + if (bank->is_input) 295 + return -EINVAL; 296 + sgpio_output_set(priv, &addr, arg); 297 + break; 298 + 299 + default: 300 + err = -ENOTSUPP; 301 + } 302 + } 303 + 304 + return err; 305 + } 306 + 307 + static const struct pinconf_ops sgpio_confops = { 308 + .is_generic = true, 309 + .pin_config_get = sgpio_pinconf_get, 310 + .pin_config_set = sgpio_pinconf_set, 311 + .pin_config_config_dbg_show = pinconf_generic_dump_config, 312 + }; 313 + 314 + static int sgpio_get_functions_count(struct pinctrl_dev *pctldev) 315 + { 316 + return 1; 317 + } 318 + 319 + static const char *sgpio_get_function_name(struct pinctrl_dev *pctldev, 320 + unsigned int function) 321 + { 322 + return functions[0]; 323 + } 324 + 325 + static int sgpio_get_function_groups(struct pinctrl_dev *pctldev, 326 + unsigned int function, 327 + const char *const **groups, 328 + unsigned *const num_groups) 329 + { 330 + *groups = functions; 331 + *num_groups = ARRAY_SIZE(functions); 332 + 333 + return 0; 334 + } 335 + 336 + static int sgpio_pinmux_set_mux(struct pinctrl_dev *pctldev, 337 + unsigned int selector, unsigned int group) 338 + { 339 + return 0; 340 + } 341 + 342 + static int sgpio_gpio_set_direction(struct pinctrl_dev *pctldev, 343 + struct pinctrl_gpio_range *range, 344 + unsigned int pin, bool input) 345 + { 346 + struct sgpio_bank *bank = pinctrl_dev_get_drvdata(pctldev); 347 + 348 + return (input == bank->is_input) ? 0 : -EINVAL; 349 + } 350 + 351 + static int sgpio_gpio_request_enable(struct pinctrl_dev *pctldev, 352 + struct pinctrl_gpio_range *range, 353 + unsigned int offset) 354 + { 355 + struct sgpio_bank *bank = pinctrl_dev_get_drvdata(pctldev); 356 + struct sgpio_priv *priv = bank->priv; 357 + struct sgpio_port_addr addr; 358 + 359 + sgpio_pin_to_addr(priv, offset, &addr); 360 + 361 + if ((priv->ports & BIT(addr.port)) == 0) { 362 + dev_warn(priv->dev, "Request port %d.%d: Port is not enabled\n", 363 + addr.port, addr.bit); 364 + return -EINVAL; 365 + } 366 + 367 + return 0; 368 + } 369 + 370 + static const struct pinmux_ops sgpio_pmx_ops = { 371 + .get_functions_count = sgpio_get_functions_count, 372 + .get_function_name = sgpio_get_function_name, 373 + .get_function_groups = sgpio_get_function_groups, 374 + .set_mux = sgpio_pinmux_set_mux, 375 + .gpio_set_direction = sgpio_gpio_set_direction, 376 + .gpio_request_enable = sgpio_gpio_request_enable, 377 + }; 378 + 379 + static int sgpio_pctl_get_groups_count(struct pinctrl_dev *pctldev) 380 + { 381 + struct sgpio_bank *bank = pinctrl_dev_get_drvdata(pctldev); 382 + 383 + return bank->pctl_desc.npins; 384 + } 385 + 386 + static const char *sgpio_pctl_get_group_name(struct pinctrl_dev *pctldev, 387 + unsigned int group) 388 + { 389 + struct sgpio_bank *bank = pinctrl_dev_get_drvdata(pctldev); 390 + 391 + return bank->pctl_desc.pins[group].name; 392 + } 393 + 394 + static int sgpio_pctl_get_group_pins(struct pinctrl_dev *pctldev, 395 + unsigned int group, 396 + const unsigned int **pins, 397 + unsigned int *num_pins) 398 + { 399 + struct sgpio_bank *bank = pinctrl_dev_get_drvdata(pctldev); 400 + 401 + *pins = &bank->pctl_desc.pins[group].number; 402 + *num_pins = 1; 403 + 404 + return 0; 405 + } 406 + 407 + static const struct pinctrl_ops sgpio_pctl_ops = { 408 + .get_groups_count = sgpio_pctl_get_groups_count, 409 + .get_group_name = sgpio_pctl_get_group_name, 410 + .get_group_pins = sgpio_pctl_get_group_pins, 411 + .dt_node_to_map = pinconf_generic_dt_node_to_map_pin, 412 + .dt_free_map = pinconf_generic_dt_free_map, 413 + }; 414 + 415 + static int microchip_sgpio_direction_input(struct gpio_chip *gc, unsigned int gpio) 416 + { 417 + struct sgpio_bank *bank = gpiochip_get_data(gc); 418 + 419 + /* Fixed-position function */ 420 + return bank->is_input ? 0 : -EINVAL; 421 + } 422 + 423 + static int microchip_sgpio_direction_output(struct gpio_chip *gc, 424 + unsigned int gpio, int value) 425 + { 426 + struct sgpio_bank *bank = gpiochip_get_data(gc); 427 + struct sgpio_priv *priv = bank->priv; 428 + struct sgpio_port_addr addr; 429 + 430 + /* Fixed-position function */ 431 + if (bank->is_input) 432 + return -EINVAL; 433 + 434 + sgpio_pin_to_addr(priv, gpio, &addr); 435 + 436 + sgpio_output_set(priv, &addr, value); 437 + 438 + return 0; 439 + } 440 + 441 + static int microchip_sgpio_get_direction(struct gpio_chip *gc, unsigned int gpio) 442 + { 443 + struct sgpio_bank *bank = gpiochip_get_data(gc); 444 + 445 + return bank->is_input ? GPIO_LINE_DIRECTION_IN : GPIO_LINE_DIRECTION_OUT; 446 + } 447 + 448 + static void microchip_sgpio_set_value(struct gpio_chip *gc, 449 + unsigned int gpio, int value) 450 + { 451 + microchip_sgpio_direction_output(gc, gpio, value); 452 + } 453 + 454 + static int microchip_sgpio_get_value(struct gpio_chip *gc, unsigned int gpio) 455 + { 456 + struct sgpio_bank *bank = gpiochip_get_data(gc); 457 + struct sgpio_priv *priv = bank->priv; 458 + struct sgpio_port_addr addr; 459 + 460 + sgpio_pin_to_addr(priv, gpio, &addr); 461 + 462 + return bank->is_input ? sgpio_input_get(priv, &addr) : sgpio_output_get(priv, &addr); 463 + } 464 + 465 + static int microchip_sgpio_of_xlate(struct gpio_chip *gc, 466 + const struct of_phandle_args *gpiospec, 467 + u32 *flags) 468 + { 469 + struct sgpio_bank *bank = gpiochip_get_data(gc); 470 + struct sgpio_priv *priv = bank->priv; 471 + int pin; 472 + 473 + /* 474 + * Note that the SGIO pin is defined by *2* numbers, a port 475 + * number between 0 and 31, and a bit index, 0 to 3. 476 + */ 477 + if (gpiospec->args[0] > SGPIO_BITS_PER_WORD || 478 + gpiospec->args[1] > priv->bitcount) 479 + return -EINVAL; 480 + 481 + pin = gpiospec->args[1] + gpiospec->args[0] * priv->bitcount; 482 + 483 + if (pin > gc->ngpio) 484 + return -EINVAL; 485 + 486 + if (flags) 487 + *flags = gpiospec->args[2]; 488 + 489 + return pin; 490 + } 491 + 492 + static int microchip_sgpio_get_ports(struct sgpio_priv *priv) 493 + { 494 + const char *range_property_name = "microchip,sgpio-port-ranges"; 495 + struct device *dev = priv->dev; 496 + u32 range_params[64]; 497 + int i, nranges, ret; 498 + 499 + /* Calculate port mask */ 500 + nranges = device_property_count_u32(dev, range_property_name); 501 + if (nranges < 2 || nranges % 2 || nranges > ARRAY_SIZE(range_params)) { 502 + dev_err(dev, "%s port range: '%s' property\n", 503 + nranges == -EINVAL ? "Missing" : "Invalid", 504 + range_property_name); 505 + return -EINVAL; 506 + } 507 + 508 + ret = device_property_read_u32_array(dev, range_property_name, 509 + range_params, nranges); 510 + if (ret) { 511 + dev_err(dev, "failed to parse '%s' property: %d\n", 512 + range_property_name, ret); 513 + return ret; 514 + } 515 + for (i = 0; i < nranges; i += 2) { 516 + int start, end; 517 + 518 + start = range_params[i]; 519 + end = range_params[i + 1]; 520 + if (start > end || end >= SGPIO_BITS_PER_WORD) { 521 + dev_err(dev, "Ill-formed port-range [%d:%d]\n", 522 + start, end); 523 + } 524 + priv->ports |= GENMASK(end, start); 525 + } 526 + 527 + return 0; 528 + } 529 + 530 + static int microchip_sgpio_register_bank(struct device *dev, 531 + struct sgpio_priv *priv, 532 + struct fwnode_handle *fwnode, 533 + int bankno) 534 + { 535 + struct pinctrl_pin_desc *pins; 536 + struct pinctrl_desc *pctl_desc; 537 + struct pinctrl_dev *pctldev; 538 + struct sgpio_bank *bank; 539 + struct gpio_chip *gc; 540 + u32 ngpios; 541 + int i, ret; 542 + 543 + /* Get overall bank struct */ 544 + bank = (bankno == 0) ? &priv->in : &priv->out; 545 + bank->priv = priv; 546 + 547 + if (fwnode_property_read_u32(fwnode, "ngpios", &ngpios)) { 548 + dev_info(dev, "failed to get number of gpios for bank%d\n", 549 + bankno); 550 + ngpios = 64; 551 + } 552 + 553 + priv->bitcount = ngpios / SGPIO_BITS_PER_WORD; 554 + if (priv->bitcount > SGPIO_MAX_BITS) { 555 + dev_err(dev, "Bit width exceeds maximum (%d)\n", 556 + SGPIO_MAX_BITS); 557 + return -EINVAL; 558 + } 559 + 560 + pctl_desc = &bank->pctl_desc; 561 + pctl_desc->name = devm_kasprintf(dev, GFP_KERNEL, "%s-%sput", 562 + dev_name(dev), 563 + bank->is_input ? "in" : "out"); 564 + pctl_desc->pctlops = &sgpio_pctl_ops; 565 + pctl_desc->pmxops = &sgpio_pmx_ops; 566 + pctl_desc->confops = &sgpio_confops; 567 + pctl_desc->owner = THIS_MODULE; 568 + 569 + pins = devm_kzalloc(dev, sizeof(*pins)*ngpios, GFP_KERNEL); 570 + if (!pins) 571 + return -ENOMEM; 572 + 573 + pctl_desc->npins = ngpios; 574 + pctl_desc->pins = pins; 575 + 576 + for (i = 0; i < ngpios; i++) { 577 + struct sgpio_port_addr addr; 578 + 579 + sgpio_pin_to_addr(priv, i, &addr); 580 + 581 + pins[i].number = i; 582 + pins[i].name = devm_kasprintf(dev, GFP_KERNEL, 583 + "SGPIO_%c_p%db%d", 584 + bank->is_input ? 'I' : 'O', 585 + addr.port, addr.bit); 586 + if (!pins[i].name) 587 + return -ENOMEM; 588 + } 589 + 590 + pctldev = devm_pinctrl_register(dev, pctl_desc, bank); 591 + if (IS_ERR(pctldev)) 592 + return dev_err_probe(dev, PTR_ERR(pctldev), "Failed to register pinctrl\n"); 593 + 594 + gc = &bank->gpio; 595 + gc->label = pctl_desc->name; 596 + gc->parent = dev; 597 + gc->of_node = to_of_node(fwnode); 598 + gc->owner = THIS_MODULE; 599 + gc->get_direction = microchip_sgpio_get_direction; 600 + gc->direction_input = microchip_sgpio_direction_input; 601 + gc->direction_output = microchip_sgpio_direction_output; 602 + gc->get = microchip_sgpio_get_value; 603 + gc->set = microchip_sgpio_set_value; 604 + gc->request = gpiochip_generic_request; 605 + gc->free = gpiochip_generic_free; 606 + gc->of_xlate = microchip_sgpio_of_xlate; 607 + gc->of_gpio_n_cells = 3; 608 + gc->base = -1; 609 + gc->ngpio = ngpios; 610 + 611 + ret = devm_gpiochip_add_data(dev, gc, bank); 612 + if (ret) 613 + dev_err(dev, "Failed to register: ret %d\n", ret); 614 + 615 + return ret; 616 + } 617 + 618 + static int microchip_sgpio_probe(struct platform_device *pdev) 619 + { 620 + int div_clock = 0, ret, port, i, nbanks; 621 + struct device *dev = &pdev->dev; 622 + struct fwnode_handle *fwnode; 623 + struct sgpio_priv *priv; 624 + struct clk *clk; 625 + u32 val; 626 + 627 + priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 628 + if (!priv) 629 + return -ENOMEM; 630 + 631 + priv->dev = dev; 632 + 633 + clk = devm_clk_get(dev, NULL); 634 + if (IS_ERR(clk)) 635 + return dev_err_probe(dev, PTR_ERR(clk), "Failed to get clock\n"); 636 + 637 + div_clock = clk_get_rate(clk); 638 + if (device_property_read_u32(dev, "bus-frequency", &priv->clock)) 639 + priv->clock = 12500000; 640 + if (priv->clock == 0 || priv->clock > (div_clock / 2)) { 641 + dev_err(dev, "Invalid frequency %d\n", priv->clock); 642 + return -EINVAL; 643 + } 644 + 645 + priv->regs = devm_platform_ioremap_resource(pdev, 0); 646 + if (IS_ERR(priv->regs)) 647 + return PTR_ERR(priv->regs); 648 + priv->properties = device_get_match_data(dev); 649 + priv->in.is_input = true; 650 + 651 + /* Get rest of device properties */ 652 + ret = microchip_sgpio_get_ports(priv); 653 + if (ret) 654 + return ret; 655 + 656 + nbanks = device_get_child_node_count(dev); 657 + if (nbanks != 2) { 658 + dev_err(dev, "Must have 2 banks (have %d)\n", nbanks); 659 + return -EINVAL; 660 + } 661 + 662 + i = 0; 663 + device_for_each_child_node(dev, fwnode) { 664 + ret = microchip_sgpio_register_bank(dev, priv, fwnode, i++); 665 + if (ret) 666 + return ret; 667 + } 668 + 669 + if (priv->in.gpio.ngpio != priv->out.gpio.ngpio) { 670 + dev_err(dev, "Banks must have same GPIO count\n"); 671 + return -ERANGE; 672 + } 673 + 674 + sgpio_configure_bitstream(priv); 675 + 676 + val = max(2U, div_clock / priv->clock); 677 + sgpio_configure_clock(priv, val); 678 + 679 + for (port = 0; port < SGPIO_BITS_PER_WORD; port++) 680 + sgpio_writel(priv, 0, REG_PORT_CONFIG, port); 681 + sgpio_writel(priv, priv->ports, REG_PORT_ENABLE, 0); 682 + 683 + return 0; 684 + } 685 + 686 + static const struct of_device_id microchip_sgpio_gpio_of_match[] = { 687 + { 688 + .compatible = "microchip,sparx5-sgpio", 689 + .data = &properties_sparx5, 690 + }, { 691 + .compatible = "mscc,luton-sgpio", 692 + .data = &properties_luton, 693 + }, { 694 + .compatible = "mscc,ocelot-sgpio", 695 + .data = &properties_ocelot, 696 + }, { 697 + /* sentinel */ 698 + } 699 + }; 700 + 701 + static struct platform_driver microchip_sgpio_pinctrl_driver = { 702 + .driver = { 703 + .name = "pinctrl-microchip-sgpio", 704 + .of_match_table = microchip_sgpio_gpio_of_match, 705 + .suppress_bind_attrs = true, 706 + }, 707 + .probe = microchip_sgpio_probe, 708 + }; 709 + builtin_platform_driver(microchip_sgpio_pinctrl_driver);