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

gpio: mmio: Add flag for calling pinctrl back-end

It turns out that with this flag we can switch over an entire
driver to use gpio-mmio instead of a bunch of custom code,
also providing get/set_multiple() to it in the process, so it
seems like a reasonable feature to add.

The generic pin control backend requires us to call the
gpiochip_generic_request(), gpiochip_generic_free(),
pinctrl_gpio_direction_output() and pinctrl_gpio_direction_input()
callbacks, so if the new flag for a pin control back-end
is set, we make sure these functions get called as
expected.

Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
Link: https://lore.kernel.org/r/20250219-vf610-mmio-v3-1-588b64f0b689@linaro.org
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>

authored by

Linus Walleij and committed by
Bartosz Golaszewski
2145ba37 45af02f0

+32 -8
+29 -8
drivers/gpio/gpio-mmio.c
··· 49 49 #include <linux/log2.h> 50 50 #include <linux/mod_devicetable.h> 51 51 #include <linux/module.h> 52 + #include <linux/pinctrl/consumer.h> 52 53 #include <linux/platform_device.h> 53 54 #include <linux/property.h> 54 55 #include <linux/slab.h> ··· 324 323 gc->write_reg(gc->reg_clr, clear_mask); 325 324 } 326 325 326 + static int bgpio_dir_return(struct gpio_chip *gc, unsigned int gpio, bool dir_out) 327 + { 328 + if (!gc->bgpio_pinctrl) 329 + return 0; 330 + 331 + if (dir_out) 332 + return pinctrl_gpio_direction_output(gc, gpio); 333 + else 334 + return pinctrl_gpio_direction_input(gc, gpio); 335 + } 336 + 327 337 static int bgpio_simple_dir_in(struct gpio_chip *gc, unsigned int gpio) 328 338 { 329 - return 0; 339 + return bgpio_dir_return(gc, gpio, false); 330 340 } 331 341 332 342 static int bgpio_dir_out_err(struct gpio_chip *gc, unsigned int gpio, ··· 351 339 { 352 340 gc->set(gc, gpio, val); 353 341 354 - return 0; 342 + return bgpio_dir_return(gc, gpio, true); 355 343 } 356 344 357 345 static int bgpio_dir_in(struct gpio_chip *gc, unsigned int gpio) ··· 369 357 370 358 raw_spin_unlock_irqrestore(&gc->bgpio_lock, flags); 371 359 372 - return 0; 360 + return bgpio_dir_return(gc, gpio, false); 373 361 } 374 362 375 363 static int bgpio_get_dir(struct gpio_chip *gc, unsigned int gpio) ··· 415 403 { 416 404 bgpio_dir_out(gc, gpio, val); 417 405 gc->set(gc, gpio, val); 418 - return 0; 406 + return bgpio_dir_return(gc, gpio, true); 419 407 } 420 408 421 409 static int bgpio_dir_out_val_first(struct gpio_chip *gc, unsigned int gpio, ··· 423 411 { 424 412 gc->set(gc, gpio, val); 425 413 bgpio_dir_out(gc, gpio, val); 426 - return 0; 414 + return bgpio_dir_return(gc, gpio, true); 427 415 } 428 416 429 417 static int bgpio_setup_accessors(struct device *dev, ··· 574 562 575 563 static int bgpio_request(struct gpio_chip *chip, unsigned gpio_pin) 576 564 { 577 - if (gpio_pin < chip->ngpio) 578 - return 0; 565 + if (gpio_pin >= chip->ngpio) 566 + return -EINVAL; 579 567 580 - return -EINVAL; 568 + if (chip->bgpio_pinctrl) 569 + return gpiochip_generic_request(chip, gpio_pin); 570 + 571 + return 0; 581 572 } 582 573 583 574 /** ··· 646 631 ret = bgpio_setup_direction(gc, dirout, dirin, flags); 647 632 if (ret) 648 633 return ret; 634 + 635 + if (flags & BGPIOF_PINCTRL_BACKEND) { 636 + gc->bgpio_pinctrl = true; 637 + /* Currently this callback is only used for pincontrol */ 638 + gc->free = gpiochip_generic_free; 639 + } 649 640 650 641 gc->bgpio_data = gc->read_reg(gc->reg_dat); 651 642 if (gc->set == bgpio_set_set &&
+3
include/linux/gpio/driver.h
··· 397 397 * @reg_dir_in: direction in setting register for generic GPIO 398 398 * @bgpio_dir_unreadable: indicates that the direction register(s) cannot 399 399 * be read and we need to rely on out internal state tracking. 400 + * @bgpio_pinctrl: the generic GPIO uses a pin control backend. 400 401 * @bgpio_bits: number of register bits used for a generic GPIO i.e. 401 402 * <register width> * 8 402 403 * @bgpio_lock: used to lock chip->bgpio_data. Also, this is needed to keep ··· 482 481 void __iomem *reg_dir_out; 483 482 void __iomem *reg_dir_in; 484 483 bool bgpio_dir_unreadable; 484 + bool bgpio_pinctrl; 485 485 int bgpio_bits; 486 486 raw_spinlock_t bgpio_lock; 487 487 unsigned long bgpio_data; ··· 723 721 #define BGPIOF_READ_OUTPUT_REG_SET BIT(4) /* reg_set stores output value */ 724 722 #define BGPIOF_NO_OUTPUT BIT(5) /* only input */ 725 723 #define BGPIOF_NO_SET_ON_INPUT BIT(6) 724 + #define BGPIOF_PINCTRL_BACKEND BIT(7) /* Call pinctrl direction setters */ 726 725 727 726 #ifdef CONFIG_GPIOLIB_IRQCHIP 728 727 int gpiochip_irqchip_add_domain(struct gpio_chip *gc,