[PATCH 30/36] pinctrl: remove old GPIO helpers

From: Bartosz Golaszewski
Date: Tue Oct 03 2023 - 10:53:00 EST


From: Bartosz Golaszewski <bartosz.golaszewski@xxxxxxxxxx>

Old variants of pinctrl GPIO helpers are no longer used. Let's remove
them as well as convert all static functions in pinctrl/core.c that
still deal with global GPIO numbers to using the gpio_chip + offset
pairs.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@xxxxxxxxxx>
---
drivers/pinctrl/core.c | 242 ++++++++++++-------------------
include/linux/pinctrl/consumer.h | 35 -----
2 files changed, 94 insertions(+), 183 deletions(-)

diff --git a/drivers/pinctrl/core.c b/drivers/pinctrl/core.c
index 679f48c5beeb..5bad56ed8176 100644
--- a/drivers/pinctrl/core.c
+++ b/drivers/pinctrl/core.c
@@ -268,7 +268,8 @@ static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
/**
* gpio_to_pin() - GPIO range GPIO number to pin number translation
* @range: GPIO range used for the translation
- * @gpio: gpio pin to translate to a pin number
+ * @gc: GPIO chip structure from the GPIO subsystem
+ * @offset: hardware offset of the GPIO relative to the controller
*
* Finds the pin number for a given GPIO using the specified GPIO range
* as a base for translation. The distinction between linear GPIO ranges
@@ -279,25 +280,28 @@ static int pinctrl_register_pins(struct pinctrl_dev *pctldev,
* result of successful pinctrl_get_device_gpio_range calls)!
*/
static inline int gpio_to_pin(struct pinctrl_gpio_range *range,
- unsigned int gpio)
+ struct gpio_chip *gc, unsigned int offset)
{
- unsigned int offset = gpio - range->base;
+ unsigned int pin = gc->base + offset - range->base;
+
if (range->pins)
- return range->pins[offset];
+ return range->pins[pin];
else
- return range->pin_base + offset;
+ return range->pin_base + pin;
}

/**
* pinctrl_match_gpio_range() - check if a certain GPIO pin is in range
* @pctldev: pin controller device to check
- * @gpio: gpio pin to check taken from the global GPIO pin space
+ * @gc: GPIO chip structure from the GPIO subsystem
+ * @offset: hardware offset of the GPIO relative to the controller
*
* Tries to match a GPIO pin number to the ranges handled by a certain pin
* controller, return the range or NULL
*/
static struct pinctrl_gpio_range *
-pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
+pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, struct gpio_chip *gc,
+ unsigned int offset)
{
struct pinctrl_gpio_range *range;

@@ -305,8 +309,8 @@ pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
/* Loop over the ranges */
list_for_each_entry(range, &pctldev->gpio_ranges, node) {
/* Check if we're in the valid range */
- if (gpio >= range->base &&
- gpio < range->base + range->npins) {
+ if (gc->base + offset >= range->base &&
+ gc->base + offset < range->base + range->npins) {
mutex_unlock(&pctldev->mutex);
return range;
}
@@ -318,7 +322,8 @@ pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
/**
* pinctrl_ready_for_gpio_range() - check if other GPIO pins of
* the same GPIO chip are in range
- * @gpio: gpio pin to check taken from the global GPIO pin space
+ * @gc: GPIO chip structure from the GPIO subsystem
+ * @offset: hardware offset of the GPIO relative to the controller
*
* This function is complement of pinctrl_match_gpio_range(). If the return
* value of pinctrl_match_gpio_range() is NULL, this function could be used
@@ -329,19 +334,10 @@ pinctrl_match_gpio_range(struct pinctrl_dev *pctldev, unsigned gpio)
* is false, it means that pinctrl device may not be ready.
*/
#ifdef CONFIG_GPIOLIB
-static bool pinctrl_ready_for_gpio_range(unsigned gpio)
+static bool pinctrl_ready_for_gpio_range(struct gpio_chip *gc)
{
struct pinctrl_dev *pctldev;
struct pinctrl_gpio_range *range = NULL;
- /*
- * FIXME: "gpio" here is a number in the global GPIO numberspace.
- * get rid of this from the ranges eventually and get the GPIO
- * descriptor from the gpio_chip.
- */
- struct gpio_chip *chip = gpiod_to_chip(gpio_to_desc(gpio));
-
- if (WARN(!chip, "no gpio_chip for gpio%i?", gpio))
- return false;

mutex_lock(&pinctrldev_list_mutex);

@@ -351,8 +347,8 @@ static bool pinctrl_ready_for_gpio_range(unsigned gpio)
mutex_lock(&pctldev->mutex);
list_for_each_entry(range, &pctldev->gpio_ranges, node) {
/* Check if any gpio range overlapped with gpio chip */
- if (range->base + range->npins - 1 < chip->base ||
- range->base > chip->base + chip->ngpio - 1)
+ if (range->base + range->npins - 1 < gc->base ||
+ range->base > gc->base + gc->ngpio - 1)
continue;
mutex_unlock(&pctldev->mutex);
mutex_unlock(&pinctrldev_list_mutex);
@@ -366,12 +362,13 @@ static bool pinctrl_ready_for_gpio_range(unsigned gpio)
return false;
}
#else
-static bool pinctrl_ready_for_gpio_range(unsigned gpio) { return true; }
+static bool pinctrl_ready_for_gpio_range(struct gpio_chip *gc) { return true; }
#endif

/**
* pinctrl_get_device_gpio_range() - find device for GPIO range
- * @gpio: the pin to locate the pin controller for
+ * @gc: GPIO chip structure from the GPIO subsystem
+ * @offset: hardware offset of the GPIO relative to the controller
* @outdev: the pin control device if found
* @outrange: the GPIO range if found
*
@@ -380,7 +377,8 @@ static bool pinctrl_ready_for_gpio_range(unsigned gpio) { return true; }
* -EPROBE_DEFER if the GPIO range could not be found in any device since it
* may still have not been registered.
*/
-static int pinctrl_get_device_gpio_range(unsigned gpio,
+static int pinctrl_get_device_gpio_range(struct gpio_chip *gc,
+ unsigned int offset,
struct pinctrl_dev **outdev,
struct pinctrl_gpio_range **outrange)
{
@@ -392,7 +390,7 @@ static int pinctrl_get_device_gpio_range(unsigned gpio,
list_for_each_entry(pctldev, &pinctrldev_list, node) {
struct pinctrl_gpio_range *range;

- range = pinctrl_match_gpio_range(pctldev, gpio);
+ range = pinctrl_match_gpio_range(pctldev, gc, offset);
if (range) {
*outdev = pctldev;
*outrange = range;
@@ -754,10 +752,10 @@ int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
return -EINVAL;
}

-bool pinctrl_gpio_can_use_line(unsigned gpio)
+bool pinctrl_gpio_can_use_line_new(struct gpio_chip *gc, unsigned int offset)
{
- struct pinctrl_dev *pctldev;
struct pinctrl_gpio_range *range;
+ struct pinctrl_dev *pctldev;
bool result;
int pin;

@@ -766,56 +764,19 @@ bool pinctrl_gpio_can_use_line(unsigned gpio)
* we're probably dealing with GPIO driver
* without a backing pin controller - bail out.
*/
- if (pinctrl_get_device_gpio_range(gpio, &pctldev, &range))
+ if (pinctrl_get_device_gpio_range(gc, offset, &pctldev, &range))
return true;

- mutex_lock(&pctldev->mutex);
-
- /* Convert to the pin controllers number space */
- pin = gpio_to_pin(range, gpio);
-
- result = pinmux_can_be_used_for_gpio(pctldev, pin);
-
- mutex_unlock(&pctldev->mutex);
+ scoped_guard(mutex, &pctldev->mutex) {
+ /* Convert to the pin controllers number space */
+ pin = gpio_to_pin(range, gc, offset);
+ result = pinmux_can_be_used_for_gpio(pctldev, pin);
+ }

return result;
}
-EXPORT_SYMBOL_GPL(pinctrl_gpio_can_use_line);
-
-bool pinctrl_gpio_can_use_line_new(struct gpio_chip *gc, unsigned int offset)
-{
- return pinctrl_gpio_can_use_line(gc->base + offset);
-}
EXPORT_SYMBOL_GPL(pinctrl_gpio_can_use_line_new);

-/* This function is deprecated and will be removed. Don't use. */
-int pinctrl_gpio_request(unsigned gpio)
-{
- struct pinctrl_dev *pctldev;
- struct pinctrl_gpio_range *range;
- int ret;
- int pin;
-
- ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
- if (ret) {
- if (pinctrl_ready_for_gpio_range(gpio))
- ret = 0;
- return ret;
- }
-
- mutex_lock(&pctldev->mutex);
-
- /* Convert to the pin controllers number space */
- pin = gpio_to_pin(range, gpio);
-
- ret = pinmux_request_gpio(pctldev, range, pin, gpio);
-
- mutex_unlock(&pctldev->mutex);
-
- return ret;
-}
-EXPORT_SYMBOL_GPL(pinctrl_gpio_request);
-
/**
* pinctrl_gpio_request_new() - request a single pin to be used as GPIO
* @gc: GPIO chip structure from the GPIO subsystem
@@ -827,33 +788,29 @@ EXPORT_SYMBOL_GPL(pinctrl_gpio_request);
*/
int pinctrl_gpio_request_new(struct gpio_chip *gc, unsigned int offset)
{
- return pinctrl_gpio_request(gc->base + offset);
+ struct pinctrl_gpio_range *range;
+ struct pinctrl_dev *pctldev;
+ int ret, pin;
+
+ ret = pinctrl_get_device_gpio_range(gc, offset, &pctldev, &range);
+ if (ret) {
+ if (pinctrl_ready_for_gpio_range(gc))
+ ret = 0;
+
+ return ret;
+ }
+
+ scoped_guard(mutex, &pctldev->mutex) {
+ /* Convert to the pin controllers number space */
+ pin = gpio_to_pin(range, gc, offset);
+ ret = pinmux_request_gpio(pctldev, range, pin,
+ gc->base + offset);
+ }
+
+ return ret;
}
EXPORT_SYMBOL_GPL(pinctrl_gpio_request_new);

-/* This function is deprecated and will be removed. Don't use. */
-void pinctrl_gpio_free(unsigned gpio)
-{
- struct pinctrl_dev *pctldev;
- struct pinctrl_gpio_range *range;
- int ret;
- int pin;
-
- ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
- if (ret) {
- return;
- }
- mutex_lock(&pctldev->mutex);
-
- /* Convert to the pin controllers number space */
- pin = gpio_to_pin(range, gpio);
-
- pinmux_free_gpio(pctldev, pin, range);
-
- mutex_unlock(&pctldev->mutex);
-}
-EXPORT_SYMBOL_GPL(pinctrl_gpio_free);
-
/**
* pinctrl_gpio_free_new() - free control on a single pin, currently used as GPIO
* @gc: GPIO chip structure from the GPIO subsystem
@@ -865,40 +822,42 @@ EXPORT_SYMBOL_GPL(pinctrl_gpio_free);
*/
void pinctrl_gpio_free_new(struct gpio_chip *gc, unsigned int offset)
{
- return pinctrl_gpio_free(gc->base + offset);
+ struct pinctrl_gpio_range *range;
+ struct pinctrl_dev *pctldev;
+ int ret, pin;
+
+ ret = pinctrl_get_device_gpio_range(gc, offset, &pctldev, &range);
+ if (ret)
+ return;
+
+ guard(mutex)(&pctldev->mutex);
+
+ /* Convert to the pin controllers number space */
+ pin = gpio_to_pin(range, gc, offset);
+ pinmux_free_gpio(pctldev, pin, range);
}
EXPORT_SYMBOL_GPL(pinctrl_gpio_free_new);

-static int pinctrl_gpio_direction(unsigned gpio, bool input)
+static int
+pinctrl_gpio_direction(struct gpio_chip *gc, unsigned int offset, bool input)
{
- struct pinctrl_dev *pctldev;
struct pinctrl_gpio_range *range;
- int ret;
- int pin;
+ struct pinctrl_dev *pctldev;
+ int ret, pin;

- ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
- if (ret) {
+ ret = pinctrl_get_device_gpio_range(gc, offset, &pctldev, &range);
+ if (ret)
return ret;
+
+ scoped_guard(mutex, &pctldev->mutex) {
+ /* Convert to the pin controllers number space */
+ pin = gpio_to_pin(range, gc, offset);
+ ret = pinmux_gpio_direction(pctldev, range, pin, input);
}

- mutex_lock(&pctldev->mutex);
-
- /* Convert to the pin controllers number space */
- pin = gpio_to_pin(range, gpio);
- ret = pinmux_gpio_direction(pctldev, range, pin, input);
-
- mutex_unlock(&pctldev->mutex);
-
return ret;
}

-/* This function is deprecated and will be removed. Don't use. */
-int pinctrl_gpio_direction_input(unsigned gpio)
-{
- return pinctrl_gpio_direction(gpio, true);
-}
-EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
-
/**
* pinctrl_gpio_direction_input_new() - request a GPIO pin to go into input mode
* @gc: GPIO chip structure from the GPIO subsystem
@@ -910,17 +869,10 @@ EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input);
*/
int pinctrl_gpio_direction_input_new(struct gpio_chip *gc, unsigned int offset)
{
- return pinctrl_gpio_direction_input(gc->base + offset);
+ return pinctrl_gpio_direction(gc, offset, true);
}
EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_input_new);

-/* This function is deprecated and will be removed. Don't use. */
-int pinctrl_gpio_direction_output(unsigned gpio)
-{
- return pinctrl_gpio_direction(gpio, false);
-}
-EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
-
/**
* pinctrl_gpio_direction_output_new() - request a GPIO pin to go into output
* mode
@@ -933,31 +885,10 @@ EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output);
*/
int pinctrl_gpio_direction_output_new(struct gpio_chip *gc, unsigned int offset)
{
- return pinctrl_gpio_direction_output(gc->base + offset);
+ return pinctrl_gpio_direction(gc, offset, false);
}
EXPORT_SYMBOL_GPL(pinctrl_gpio_direction_output_new);

-/* This function is deprecated and will be removed. Don't use. */
-int pinctrl_gpio_set_config(unsigned gpio, unsigned long config)
-{
- unsigned long configs[] = { config };
- struct pinctrl_gpio_range *range;
- struct pinctrl_dev *pctldev;
- int ret, pin;
-
- ret = pinctrl_get_device_gpio_range(gpio, &pctldev, &range);
- if (ret)
- return ret;
-
- mutex_lock(&pctldev->mutex);
- pin = gpio_to_pin(range, gpio);
- ret = pinconf_set_config(pctldev, pin, configs, ARRAY_SIZE(configs));
- mutex_unlock(&pctldev->mutex);
-
- return ret;
-}
-EXPORT_SYMBOL_GPL(pinctrl_gpio_set_config);
-
/**
* pinctrl_gpio_set_config_new() - Apply config to given GPIO pin
* @gc: GPIO chip structure from the GPIO subsystem
@@ -971,7 +902,22 @@ EXPORT_SYMBOL_GPL(pinctrl_gpio_set_config);
int pinctrl_gpio_set_config_new(struct gpio_chip *gc, unsigned int offset,
unsigned long config)
{
- return pinctrl_gpio_set_config(gc->base + offset, config);
+ unsigned long configs[] = { config };
+ struct pinctrl_gpio_range *range;
+ struct pinctrl_dev *pctldev;
+ int ret, pin;
+
+ ret = pinctrl_get_device_gpio_range(gc, offset, &pctldev, &range);
+ if (ret)
+ return ret;
+
+ scoped_guard(mutex, &pctldev->mutex) {
+ pin = gpio_to_pin(range, gc, offset);
+ ret = pinconf_set_config(pctldev, pin, configs,
+ ARRAY_SIZE(configs));
+ }
+
+ return ret;
}
EXPORT_SYMBOL_GPL(pinctrl_gpio_set_config_new);

diff --git a/include/linux/pinctrl/consumer.h b/include/linux/pinctrl/consumer.h
index c95c13983376..aa3a7a7ca52d 100644
--- a/include/linux/pinctrl/consumer.h
+++ b/include/linux/pinctrl/consumer.h
@@ -26,19 +26,13 @@ struct pinctrl_state;
#ifdef CONFIG_PINCTRL

/* External interface to pin control */
-bool pinctrl_gpio_can_use_line(unsigned gpio);
bool pinctrl_gpio_can_use_line_new(struct gpio_chip *gc, unsigned int offset);
-int pinctrl_gpio_request(unsigned gpio);
int pinctrl_gpio_request_new(struct gpio_chip *gc, unsigned int offset);
-void pinctrl_gpio_free(unsigned gpio);
void pinctrl_gpio_free_new(struct gpio_chip *gc, unsigned int offset);
-int pinctrl_gpio_direction_input(unsigned gpio);
int pinctrl_gpio_direction_input_new(struct gpio_chip *gc,
unsigned int offset);
-int pinctrl_gpio_direction_output(unsigned gpio);
int pinctrl_gpio_direction_output_new(struct gpio_chip *gc,
unsigned int offset);
-int pinctrl_gpio_set_config(unsigned gpio, unsigned long config);
int pinctrl_gpio_set_config_new(struct gpio_chip *gc, unsigned int offset,
unsigned long config);

@@ -73,64 +67,35 @@ static inline int pinctrl_pm_select_idle_state(struct device *dev)

#else /* !CONFIG_PINCTRL */

-static inline bool pinctrl_gpio_can_use_line(unsigned gpio)
-{
- return true;
-}
-
static inline bool
pinctrl_gpio_can_use_line_new(struct gpio_chip *gc, unsigned int offset)
{
return true;
}

-static inline int pinctrl_gpio_request(unsigned gpio)
-{
- return 0;
-}
-
static inline int
pinctrl_gpio_request_new(struct gpio_chip *gc, unsigned int offset)
{
return 0;
}

-static inline void pinctrl_gpio_free(unsigned gpio)
-{
-}
-
static inline void
pinctrl_gpio_free_new(struct gpio_chip *gc, unsigned int offset)
{
}

-static inline int pinctrl_gpio_direction_input(unsigned gpio)
-{
- return 0;
-}
-
static inline int
pinctrl_gpio_direction_input_new(struct gpio_chip *gc, unsigned int offset)
{
return 0;
}

-static inline int pinctrl_gpio_direction_output(unsigned gpio)
-{
- return 0;
-}
-
static inline int
pinctrl_gpio_direction_output_new(struct gpio_chip *gc, unsigned int offset)
{
return 0;
}

-static inline int pinctrl_gpio_set_config(unsigned gpio, unsigned long config)
-{
- return 0;
-}
-
static inline int
pinctrl_gpio_set_config_new(struct gpio_chip *gc, unsigned int offset,
unsigned long config)
--
2.39.2