1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * regmap based generic GPIO driver 4 * 5 * Copyright 2020 Michael Walle <michael@walle.cc> 6 */ 7 8 #include <linux/bits.h> 9 #include <linux/cleanup.h> 10 #include <linux/compiler_attributes.h> 11 #include <linux/device.h> 12 #include <linux/err.h> 13 #include <linux/io.h> 14 #include <linux/module.h> 15 #include <linux/pm_runtime.h> 16 #include <linux/regmap.h> 17 #include <linux/slab.h> 18 #include <linux/types.h> 19 20 #include <linux/gpio/driver.h> 21 #include <linux/gpio/regmap.h> 22 23 #include "gpiolib.h" 24 25 struct gpio_regmap { 26 struct device *parent; 27 struct regmap *regmap; 28 struct device *pm_dev; 29 30 struct gpio_chip gpio_chip; 31 32 int reg_stride; 33 int ngpio_per_reg; 34 unsigned int reg_dat_base; 35 unsigned int reg_set_base; 36 unsigned int reg_clr_base; 37 unsigned int reg_dir_in_base; 38 unsigned int reg_dir_out_base; 39 unsigned long *fixed_direction_mask; 40 unsigned long *fixed_direction_output; 41 42 #ifdef CONFIG_REGMAP_IRQ 43 int regmap_irq_line; 44 struct regmap_irq_chip_data *irq_chip_data; 45 #endif 46 47 int (*reg_mask_xlate)(struct gpio_regmap *gpio, 48 enum gpio_regmap_operation op, 49 unsigned int base, unsigned int offset, 50 unsigned int *reg, unsigned int *mask); 51 52 int (*value_xlate)(struct gpio_regmap *gpio, 53 enum gpio_regmap_operation op, 54 unsigned int base, unsigned int offset, 55 unsigned int reg, unsigned int *mask, 56 unsigned int *val); 57 58 int (*set_config)(struct gpio_regmap *gpio, struct gpio_chip *chip, 59 unsigned int offset, unsigned long config); 60 61 void *driver_data; 62 }; 63 64 static unsigned int gpio_regmap_addr(unsigned int addr) 65 { 66 if (addr == GPIO_REGMAP_ADDR_ZERO) 67 return 0; 68 69 return addr; 70 } 71 72 static int gpio_regmap_simple_xlate(struct gpio_regmap *gpio, 73 enum gpio_regmap_operation __maybe_unused op, 74 unsigned int base, unsigned int offset, 75 unsigned int *reg, unsigned int *mask) 76 { 77 unsigned int line = offset % gpio->ngpio_per_reg; 78 unsigned int stride = offset / gpio->ngpio_per_reg; 79 80 *reg = base + stride * gpio->reg_stride; 81 *mask = BIT(line); 82 83 return 0; 84 } 85 86 static int gpio_regmap_runtime_get(struct gpio_regmap *gpio) 87 { 88 if (!gpio->pm_dev) 89 return 0; 90 91 return pm_runtime_get_active(gpio->pm_dev, RPM_TRANSPARENT); 92 } 93 94 static void gpio_regmap_runtime_put(struct gpio_regmap *gpio) 95 { 96 if (!gpio->pm_dev) 97 return; 98 99 pm_runtime_put_autosuspend(gpio->pm_dev); 100 } 101 102 DEFINE_GUARD(gpio_regmap_runtime, struct gpio_regmap *, 103 gpio_regmap_runtime_get(_T), gpio_regmap_runtime_put(_T)) 104 DEFINE_GUARD_COND(gpio_regmap_runtime, _try, 105 gpio_regmap_runtime_get(_T), _RET == 0) 106 107 #define GPIO_REGMAP_RUNTIME_ACQUIRE(_gpio, _var) \ 108 ACQUIRE(gpio_regmap_runtime_try, _var)(_gpio) 109 110 #define GPIO_REGMAP_RUNTIME_ACQUIRE_ERR(_var_ptr) \ 111 ACQUIRE_ERR(gpio_regmap_runtime, _var_ptr) 112 113 static int gpio_regmap_get(struct gpio_chip *chip, unsigned int offset) 114 { 115 struct gpio_regmap *gpio = gpiochip_get_data(chip); 116 unsigned int base, val, reg, mask; 117 int ret; 118 119 /* we might not have an output register if we are input only */ 120 if (gpio->reg_dat_base) 121 base = gpio_regmap_addr(gpio->reg_dat_base); 122 else 123 base = gpio_regmap_addr(gpio->reg_set_base); 124 125 GPIO_REGMAP_RUNTIME_ACQUIRE(gpio, pm); 126 ret = GPIO_REGMAP_RUNTIME_ACQUIRE_ERR(&pm); 127 if (ret) 128 return ret; 129 130 ret = gpio->reg_mask_xlate(gpio, GPIO_REGMAP_GET_OP, base, offset, 131 ®, &mask); 132 if (ret) 133 return ret; 134 135 if (gpio->reg_dat_base != gpio->reg_set_base) 136 return regmap_test_bits(gpio->regmap, reg, mask); 137 138 /* ensure we don't spoil any register cache with pin input values */ 139 ret = regmap_read_bypassed(gpio->regmap, reg, &val); 140 if (ret) 141 return ret; 142 143 return !!(val & mask); 144 } 145 146 static int gpio_regmap_set(struct gpio_chip *chip, unsigned int offset, 147 int val) 148 { 149 struct gpio_regmap *gpio = gpiochip_get_data(chip); 150 unsigned int base = gpio_regmap_addr(gpio->reg_set_base); 151 unsigned int reg, mask, mask_val; 152 int ret; 153 154 GPIO_REGMAP_RUNTIME_ACQUIRE(gpio, pm); 155 ret = GPIO_REGMAP_RUNTIME_ACQUIRE_ERR(&pm); 156 if (ret) 157 return ret; 158 159 ret = gpio->reg_mask_xlate(gpio, GPIO_REGMAP_SET_OP, base, offset, 160 ®, &mask); 161 if (ret) 162 return ret; 163 164 if (val) 165 mask_val = mask; 166 else 167 mask_val = 0; 168 169 if (gpio->value_xlate) { 170 ret = gpio->value_xlate(gpio, GPIO_REGMAP_SET_OP, base, offset, 171 reg, &mask, &mask_val); 172 if (ret) 173 return ret; 174 } 175 176 /* ignore input values which shadow the old output value */ 177 if (gpio->reg_dat_base == gpio->reg_set_base) 178 ret = regmap_write_bits(gpio->regmap, reg, mask, mask_val); 179 else 180 ret = regmap_update_bits(gpio->regmap, reg, mask, mask_val); 181 182 return ret; 183 } 184 185 static int gpio_regmap_set_with_clear(struct gpio_chip *chip, 186 unsigned int offset, int val) 187 { 188 struct gpio_regmap *gpio = gpiochip_get_data(chip); 189 unsigned int base, reg, mask, value = 0; 190 int ret; 191 192 GPIO_REGMAP_RUNTIME_ACQUIRE(gpio, pm); 193 ret = GPIO_REGMAP_RUNTIME_ACQUIRE_ERR(&pm); 194 if (ret) 195 return ret; 196 197 if (val) 198 base = gpio_regmap_addr(gpio->reg_set_base); 199 else 200 base = gpio_regmap_addr(gpio->reg_clr_base); 201 202 ret = gpio->reg_mask_xlate(gpio, GPIO_REGMAP_SET_OP, base, offset, ®, &mask); 203 if (ret) 204 return ret; 205 206 if (gpio->value_xlate) { 207 ret = gpio->value_xlate(gpio, GPIO_REGMAP_SET_OP, base, offset, 208 reg, &mask, &value); 209 if (ret) 210 return ret; 211 } 212 213 return regmap_write(gpio->regmap, reg, mask); 214 } 215 216 static bool gpio_regmap_fixed_direction(struct gpio_regmap *gpio, 217 unsigned int offset) 218 { 219 if (!gpio->fixed_direction_output) 220 return false; 221 222 /* In this case only some GPIOs are fixed as input/output */ 223 if (gpio->fixed_direction_mask && 224 !test_bit(offset, gpio->fixed_direction_mask)) 225 return false; 226 227 return true; 228 } 229 230 static int gpio_regmap_get_direction(struct gpio_chip *chip, 231 unsigned int offset) 232 { 233 struct gpio_regmap *gpio = gpiochip_get_data(chip); 234 unsigned int base, reg, mask; 235 int invert, ret; 236 237 if (gpio_regmap_fixed_direction(gpio, offset)) { 238 if (test_bit(offset, gpio->fixed_direction_output)) 239 return GPIO_LINE_DIRECTION_OUT; 240 else 241 return GPIO_LINE_DIRECTION_IN; 242 } 243 244 if (gpio->reg_dat_base && !gpio->reg_set_base) 245 return GPIO_LINE_DIRECTION_IN; 246 if (gpio->reg_set_base && !gpio->reg_dat_base) 247 return GPIO_LINE_DIRECTION_OUT; 248 249 if (gpio->reg_dir_out_base) { 250 base = gpio_regmap_addr(gpio->reg_dir_out_base); 251 invert = 0; 252 } else if (gpio->reg_dir_in_base) { 253 base = gpio_regmap_addr(gpio->reg_dir_in_base); 254 invert = 1; 255 } else { 256 return -ENOTSUPP; 257 } 258 259 GPIO_REGMAP_RUNTIME_ACQUIRE(gpio, pm); 260 ret = GPIO_REGMAP_RUNTIME_ACQUIRE_ERR(&pm); 261 if (ret) 262 return ret; 263 264 ret = gpio->reg_mask_xlate(gpio, GPIO_REGMAP_GET_DIR_OP, base, offset, 265 ®, &mask); 266 if (ret) 267 return ret; 268 269 ret = regmap_test_bits(gpio->regmap, reg, mask); 270 if (ret < 0) 271 return ret; 272 273 if (ret ^ invert) 274 return GPIO_LINE_DIRECTION_OUT; 275 276 return GPIO_LINE_DIRECTION_IN; 277 } 278 279 static int gpio_regmap_try_direction_fixed(struct gpio_regmap *gpio, 280 unsigned int offset, bool output) 281 { 282 if (test_bit(offset, gpio->fixed_direction_output)) { 283 if (output) 284 return 0; 285 else 286 return -EINVAL; 287 } else { 288 if (output) 289 return -EINVAL; 290 else 291 return 0; 292 } 293 } 294 295 static int gpio_regmap_set_direction(struct gpio_chip *chip, 296 unsigned int offset, bool output) 297 { 298 struct gpio_regmap *gpio = gpiochip_get_data(chip); 299 unsigned int base, val, reg, mask; 300 int invert, ret; 301 302 /* 303 * If the direction is fixed, only accept the fixed 304 * direction in this call. 305 */ 306 if (gpio_regmap_fixed_direction(gpio, offset)) 307 return gpio_regmap_try_direction_fixed(gpio, offset, output); 308 309 if (gpio->reg_dir_out_base) { 310 base = gpio_regmap_addr(gpio->reg_dir_out_base); 311 invert = 0; 312 } else if (gpio->reg_dir_in_base) { 313 base = gpio_regmap_addr(gpio->reg_dir_in_base); 314 invert = 1; 315 } else { 316 return -ENOTSUPP; 317 } 318 319 GPIO_REGMAP_RUNTIME_ACQUIRE(gpio, pm); 320 ret = GPIO_REGMAP_RUNTIME_ACQUIRE_ERR(&pm); 321 if (ret) 322 return ret; 323 324 ret = gpio->reg_mask_xlate(gpio, GPIO_REGMAP_SET_DIR_OP, base, offset, 325 ®, &mask); 326 if (ret) 327 return ret; 328 329 if (invert) 330 val = output ? 0 : mask; 331 else 332 val = output ? mask : 0; 333 334 if (gpio->value_xlate) { 335 ret = gpio->value_xlate(gpio, GPIO_REGMAP_SET_DIR_OP, base, offset, 336 reg, &mask, &val); 337 if (ret) 338 return ret; 339 } 340 341 return regmap_update_bits(gpio->regmap, reg, mask, val); 342 } 343 344 static int gpio_regmap_direction_input(struct gpio_chip *chip, 345 unsigned int offset) 346 { 347 return gpio_regmap_set_direction(chip, offset, false); 348 } 349 350 static int gpio_regmap_direction_output(struct gpio_chip *chip, 351 unsigned int offset, int value) 352 { 353 struct gpio_regmap *gpio = gpiochip_get_data(chip); 354 int ret; 355 356 GPIO_REGMAP_RUNTIME_ACQUIRE(gpio, pm); 357 ret = GPIO_REGMAP_RUNTIME_ACQUIRE_ERR(&pm); 358 if (ret) 359 return ret; 360 361 /* 362 * First check if this is gonna work on a fixed direction line, 363 * if it doesn't (i.e. this is a fixed input line), then do not 364 * attempt to set the output value either and just bail out. 365 */ 366 if (gpio_regmap_fixed_direction(gpio, offset)) { 367 ret = gpio_regmap_try_direction_fixed(gpio, offset, true); 368 if (ret) 369 return ret; 370 } 371 372 ret = gpio_regmap_set(chip, offset, value); 373 if (ret) 374 return ret; 375 376 return gpio_regmap_set_direction(chip, offset, true); 377 } 378 379 static int gpio_regmap_set_config(struct gpio_chip *chip, 380 unsigned int offset, 381 unsigned long cfg) 382 { 383 struct gpio_regmap *gpio = gpiochip_get_data(chip); 384 385 return gpio->set_config(gpio, chip, offset, cfg); 386 } 387 388 int gpio_regmap_reqres_irq(struct gpio_regmap *gpio, unsigned int offset) 389 { 390 return gpiochip_reqres_irq(&gpio->gpio_chip, offset); 391 } 392 EXPORT_SYMBOL_GPL(gpio_regmap_reqres_irq); 393 394 void gpio_regmap_relres_irq(struct gpio_regmap *gpio, unsigned int offset) 395 { 396 gpiochip_relres_irq(&gpio->gpio_chip, offset); 397 } 398 EXPORT_SYMBOL_GPL(gpio_regmap_relres_irq); 399 400 static int __maybe_unused gpio_regmap_irq_reqres(void *irq_drv_data, irq_hw_number_t hwirq) 401 { 402 return gpio_regmap_reqres_irq(irq_drv_data, hwirq); 403 } 404 405 static void __maybe_unused gpio_regmap_irq_relres(void *irq_drv_data, irq_hw_number_t hwirq) 406 { 407 gpio_regmap_relres_irq(irq_drv_data, hwirq); 408 } 409 410 void *gpio_regmap_get_drvdata(struct gpio_regmap *gpio) 411 { 412 return gpio->driver_data; 413 } 414 EXPORT_SYMBOL_GPL(gpio_regmap_get_drvdata); 415 416 void gpio_regmap_enable_irq(struct gpio_regmap *gpio, irq_hw_number_t hwirq) 417 { 418 gpiochip_enable_irq(&gpio->gpio_chip, hwirq); 419 } 420 EXPORT_SYMBOL_GPL(gpio_regmap_enable_irq); 421 422 void gpio_regmap_disable_irq(struct gpio_regmap *gpio, irq_hw_number_t hwirq) 423 { 424 gpiochip_disable_irq(&gpio->gpio_chip, hwirq); 425 } 426 EXPORT_SYMBOL_GPL(gpio_regmap_disable_irq); 427 428 /** 429 * gpio_regmap_register() - Register a generic regmap GPIO controller 430 * @config: configuration for gpio_regmap 431 * 432 * Return: A pointer to the registered gpio_regmap or ERR_PTR error value. 433 */ 434 struct gpio_regmap *gpio_regmap_register(const struct gpio_regmap_config *config) 435 { 436 struct irq_domain *irq_domain; 437 struct gpio_regmap *gpio; 438 struct gpio_chip *chip; 439 int ret; 440 441 if (!config->parent) 442 return ERR_PTR(-EINVAL); 443 444 /* we need at least one */ 445 if (!config->reg_dat_base && !config->reg_set_base) 446 return ERR_PTR(-EINVAL); 447 448 /* if we have a direction register we need both input and output */ 449 if ((config->reg_dir_out_base || config->reg_dir_in_base) && 450 (!config->reg_dat_base || !config->reg_set_base)) 451 return ERR_PTR(-EINVAL); 452 453 /* we don't support having both registers simultaneously for now */ 454 if (config->reg_dir_out_base && config->reg_dir_in_base) 455 return ERR_PTR(-EINVAL); 456 457 gpio = kzalloc_obj(*gpio); 458 if (!gpio) 459 return ERR_PTR(-ENOMEM); 460 461 gpio->parent = config->parent; 462 gpio->driver_data = config->drvdata; 463 gpio->regmap = config->regmap; 464 gpio->reg_dat_base = config->reg_dat_base; 465 gpio->reg_set_base = config->reg_set_base; 466 gpio->reg_clr_base = config->reg_clr_base; 467 gpio->reg_dir_in_base = config->reg_dir_in_base; 468 gpio->reg_dir_out_base = config->reg_dir_out_base; 469 gpio->pm_dev = config->pm_dev; 470 471 chip = &gpio->gpio_chip; 472 chip->parent = config->parent; 473 chip->fwnode = config->fwnode; 474 chip->base = -1; 475 chip->names = config->names; 476 chip->label = config->label ?: dev_name(config->parent); 477 chip->can_sleep = config->pm_dev || regmap_might_sleep(config->regmap); 478 chip->init_valid_mask = config->init_valid_mask; 479 480 chip->request = gpiochip_generic_request; 481 chip->free = gpiochip_generic_free; 482 chip->get = gpio_regmap_get; 483 if (gpio->reg_set_base && gpio->reg_clr_base) 484 chip->set = gpio_regmap_set_with_clear; 485 else if (gpio->reg_set_base) 486 chip->set = gpio_regmap_set; 487 488 chip->get_direction = gpio_regmap_get_direction; 489 if (gpio->reg_dir_in_base || gpio->reg_dir_out_base) { 490 chip->direction_input = gpio_regmap_direction_input; 491 chip->direction_output = gpio_regmap_direction_output; 492 } 493 494 chip->ngpio = config->ngpio; 495 if (!chip->ngpio) { 496 ret = gpiochip_get_ngpios(chip, chip->parent); 497 if (ret) 498 goto err_free_gpio; 499 } 500 501 if (config->fixed_direction_mask) { 502 gpio->fixed_direction_mask = bitmap_alloc(chip->ngpio, 503 GFP_KERNEL); 504 if (!gpio->fixed_direction_mask) { 505 ret = -ENOMEM; 506 goto err_free_gpio; 507 } 508 bitmap_copy(gpio->fixed_direction_mask, 509 config->fixed_direction_mask, chip->ngpio); 510 } 511 512 if (config->fixed_direction_output) { 513 gpio->fixed_direction_output = bitmap_alloc(chip->ngpio, 514 GFP_KERNEL); 515 if (!gpio->fixed_direction_output) { 516 ret = -ENOMEM; 517 goto err_free_bitmap_dirmask; 518 } 519 bitmap_copy(gpio->fixed_direction_output, 520 config->fixed_direction_output, chip->ngpio); 521 } 522 523 /* if not set, assume there is only one register */ 524 gpio->ngpio_per_reg = config->ngpio_per_reg; 525 if (!gpio->ngpio_per_reg) 526 gpio->ngpio_per_reg = config->ngpio; 527 528 /* if not set, assume they are consecutive */ 529 gpio->reg_stride = config->reg_stride; 530 if (!gpio->reg_stride) 531 gpio->reg_stride = 1; 532 533 gpio->reg_mask_xlate = config->reg_mask_xlate; 534 if (!gpio->reg_mask_xlate) 535 gpio->reg_mask_xlate = gpio_regmap_simple_xlate; 536 537 gpio->value_xlate = config->value_xlate; 538 539 if (config->set_config) { 540 gpio->set_config = config->set_config; 541 chip->set_config = gpio_regmap_set_config; 542 } 543 544 ret = gpiochip_add_data(chip, gpio); 545 if (ret < 0) 546 goto err_free_bitmap_output; 547 548 #ifdef CONFIG_REGMAP_IRQ 549 if (config->regmap_irq_chip) { 550 gpio->regmap_irq_line = config->regmap_irq_line; 551 config->regmap_irq_chip->irq_reqres = gpio_regmap_irq_reqres; 552 config->regmap_irq_chip->irq_relres = gpio_regmap_irq_relres; 553 config->regmap_irq_chip->irq_drv_data = gpio; 554 ret = regmap_add_irq_chip_fwnode(dev_fwnode(config->parent), config->regmap, 555 config->regmap_irq_line, config->regmap_irq_flags, 556 0, config->regmap_irq_chip, &gpio->irq_chip_data); 557 if (ret) 558 goto err_remove_gpiochip; 559 560 irq_domain = regmap_irq_get_domain(gpio->irq_chip_data); 561 } else 562 #endif 563 irq_domain = config->irq_domain; 564 565 if (irq_domain) { 566 ret = gpiochip_irqchip_add_domain(chip, irq_domain); 567 if (ret) 568 goto err_remove_gpiochip; 569 } 570 571 return gpio; 572 573 err_remove_gpiochip: 574 gpiochip_remove(chip); 575 err_free_bitmap_output: 576 bitmap_free(gpio->fixed_direction_output); 577 err_free_bitmap_dirmask: 578 bitmap_free(gpio->fixed_direction_mask); 579 err_free_gpio: 580 kfree(gpio); 581 return ERR_PTR(ret); 582 } 583 EXPORT_SYMBOL_GPL(gpio_regmap_register); 584 585 /** 586 * gpio_regmap_unregister() - Unregister a generic regmap GPIO controller 587 * @gpio: gpio_regmap device to unregister 588 */ 589 void gpio_regmap_unregister(struct gpio_regmap *gpio) 590 { 591 #ifdef CONFIG_REGMAP_IRQ 592 if (gpio->irq_chip_data) 593 regmap_del_irq_chip(gpio->regmap_irq_line, gpio->irq_chip_data); 594 #endif 595 596 gpiochip_remove(&gpio->gpio_chip); 597 bitmap_free(gpio->fixed_direction_output); 598 bitmap_free(gpio->fixed_direction_mask); 599 kfree(gpio); 600 } 601 EXPORT_SYMBOL_GPL(gpio_regmap_unregister); 602 603 static void devm_gpio_regmap_unregister(void *res) 604 { 605 gpio_regmap_unregister(res); 606 } 607 608 /** 609 * devm_gpio_regmap_register() - resource managed gpio_regmap_register() 610 * @dev: device that is registering this GPIO device 611 * @config: configuration for gpio_regmap 612 * 613 * Managed gpio_regmap_register(). For generic regmap GPIO device registered by 614 * this function, gpio_regmap_unregister() is automatically called on driver 615 * detach. See gpio_regmap_register() for more information. 616 * 617 * Return: A pointer to the registered gpio_regmap or ERR_PTR error value. 618 */ 619 struct gpio_regmap *devm_gpio_regmap_register(struct device *dev, 620 const struct gpio_regmap_config *config) 621 { 622 struct gpio_regmap *gpio; 623 int ret; 624 625 gpio = gpio_regmap_register(config); 626 627 if (IS_ERR(gpio)) 628 return gpio; 629 630 ret = devm_add_action_or_reset(dev, devm_gpio_regmap_unregister, gpio); 631 if (ret) 632 return ERR_PTR(ret); 633 634 return gpio; 635 } 636 EXPORT_SYMBOL_GPL(devm_gpio_regmap_register); 637 638 MODULE_AUTHOR("Michael Walle <michael@walle.cc>"); 639 MODULE_DESCRIPTION("GPIO generic regmap driver core"); 640 MODULE_LICENSE("GPL"); 641