Skip to contentSkip to conversation

gpiolib: Take MUX usage into account

PATCH In review Ramon Fried 3 participants 5 messages Aug 3, 2019 – Aug 6, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 gpiolib: Take MUX usage into account

From: Stefan Wahren <[email protected]>

The user space like gpioinfo only see the GPIO usage but not the MUX usage (e.g. I2C or SPI usage) of a pin. As a user we want to know which pin is free/safe to use. So take the MUX usage of strict pinmux controllers into account to get a more realistic view for ioctl GPIO_GET_LINEINFO_IOCTL.

---
Sending Stefan's RFC as patch, as I tested it and it seems to work, additionally, an accompanying fix was made by me to gpiolibd to fix a display error of the actual result:
https://patchwork.ozlabs.org/patch/1139923/

 drivers/gpio/gpiolib.c           |  3 ++-
 drivers/pinctrl/core.c           | 23 +++++++++++++++++++++++
 drivers/pinctrl/pinmux.c         | 18 ++++++++++++++++++
 drivers/pinctrl/pinmux.h         |  7 +++++++
 include/linux/pinctrl/consumer.h |  6 ++++++
 5 files changed, 56 insertions(+), 1 deletion(-)
drivers/gpio/gpiolib.c +2 -1
index e013d417a936..2fd9eee0b98c 100644
@@ -1082,7 +1082,8 @@ static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
 		    test_bit(FLAG_IS_HOGGED, &desc->flags) ||
 		    test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
 		    test_bit(FLAG_EXPORT, &desc->flags) ||
-		    test_bit(FLAG_SYSFS, &desc->flags))
+		    test_bit(FLAG_SYSFS, &desc->flags) ||
+		    pinctrl_gpio_is_in_use(chip->base + lineinfo.line_offset))
 			lineinfo.flags |= GPIOLINE_FLAG_KERNEL;
 		if (test_bit(FLAG_IS_OUT, &desc->flags))
 			lineinfo.flags |= GPIOLINE_FLAG_IS_OUT;
drivers/pinctrl/core.c +23 -0
index a64849a9d1b0..0dd00c175eed 100644
@@ -759,6 +759,29 @@ int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
 	return -EINVAL;
 }
 
+bool pinctrl_gpio_is_in_use(unsigned gpio)
+{
+	struct pinctrl_dev *pctldev;
+	struct pinctrl_gpio_range *range;
+	bool result;
+	int pin;
+
+	if (pinctrl_get_device_gpio_range(gpio, &pctldev, &range))
+		return false;
+
+	mutex_lock(&pctldev->mutex);
+
+	/* Convert to the pin controllers number space */
+	pin = gpio_to_pin(range, gpio);
+
+	result = pinmux_is_in_use(pctldev, pin);
+
+	mutex_unlock(&pctldev->mutex);
+
+	return result;
+}
+EXPORT_SYMBOL_GPL(pinctrl_gpio_is_in_use);
+
 /**
  * pinctrl_gpio_request() - request a single pin to be used as GPIO
  * @gpio: the GPIO pin number from the GPIO subsystem number space
drivers/pinctrl/pinmux.c +18 -0
index 020e54f843f9..02d2751a4884 100644
@@ -70,6 +70,24 @@ int pinmux_validate_map(const struct pinctrl_map *map, int i)
 	return 0;
 }
 
+bool pinmux_is_in_use(struct pinctrl_dev *pctldev, unsigned pin)
+{
+	struct pin_desc *desc = pin_desc_get(pctldev, pin);
+	const struct pinmux_ops *ops = pctldev->desc->pmxops;
+
+	if (!desc) {
+		dev_err(pctldev->dev,
+			"pin %u is not registered so it cannot be requested\n",
+			pin);
+		return false;
+	}
+
+	if (ops->strict && desc->mux_usecount)
+		return true;
+
+	return ops->strict && !!desc->gpio_owner;
+}
+
 /**
  * pin_request() - request a single pin to be muxed in, typically for GPIO
  * @pin: the pin number in the global pin space
drivers/pinctrl/pinmux.h +7 -0
index 794cb3a003ff..24ae61136803 100644
@@ -15,6 +15,8 @@ int pinmux_check_ops(struct pinctrl_dev *pctldev);
 
 int pinmux_validate_map(const struct pinctrl_map *map, int i);
 
+bool pinmux_is_in_use(struct pinctrl_dev *pctldev, unsigned pin);
+
 int pinmux_request_gpio(struct pinctrl_dev *pctldev,
 			struct pinctrl_gpio_range *range,
 			unsigned pin, unsigned gpio);
@@ -42,6 +44,11 @@ static inline int pinmux_validate_map(const struct pinctrl_map *map, int i)
 	return 0;
 }
 
+static inline bool pinmux_is_in_use(struct pinctrl_dev *pctldev, unsigned pin)
+{
+	return false;
+}
+
 static inline int pinmux_request_gpio(struct pinctrl_dev *pctldev,
 			struct pinctrl_gpio_range *range,
 			unsigned pin, unsigned gpio)
include/linux/pinctrl/consumer.h +6 -0
index 86720a5a384f..d26826b057a1 100644
@@ -24,6 +24,7 @@ struct device;
 #ifdef CONFIG_PINCTRL
 
 /* External interface to pin control */
+extern bool pinctrl_gpio_is_in_use(unsigned gpio);
 extern int pinctrl_gpio_request(unsigned gpio);
 extern void pinctrl_gpio_free(unsigned gpio);
 extern int pinctrl_gpio_direction_input(unsigned gpio);
@@ -61,6 +62,11 @@ static inline int pinctrl_pm_select_idle_state(struct device *dev)
 
 #else /* !CONFIG_PINCTRL */
 
+static inline bool pinctrl_gpio_is_in_use(unsigned gpio)
+{
+	return 0;
+}
+
 static inline int pinctrl_gpio_request(unsigned gpio)
 {
 	return 0;
Signature · 1 line
2.22.0
#1 lore raw

sob., 3 sie 2019 o 15:34 Ramon Fried <[email protected]> napisał(a):

Quoted 151 lines

From: Stefan Wahren <[email protected]>

The user space like gpioinfo only see the GPIO usage but not the MUX usage (e.g. I2C or SPI usage) of a pin. As a user we want to know which pin is free/safe to use. So take the MUX usage of strict pinmux controllers into account to get a more realistic view for ioctl GPIO_GET_LINEINFO_IOCTL.

---
Sending Stefan's RFC as patch, as I tested it and it seems to work, additionally, an accompanying fix was made by me to gpiolibd to fix a display error of the actual result:
https://patchwork.ozlabs.org/patch/1139923/

 drivers/gpio/gpiolib.c           |  3 ++-
 drivers/pinctrl/core.c           | 23 +++++++++++++++++++++++
 drivers/pinctrl/pinmux.c         | 18 ++++++++++++++++++
 drivers/pinctrl/pinmux.h         |  7 +++++++
 include/linux/pinctrl/consumer.h |  6 ++++++
 5 files changed, 56 insertions(+), 1 deletion(-)
drivers/gpio/gpiolib.c +2 -1
index e013d417a936..2fd9eee0b98c 100644
@@ -1082,7 +1082,8 @@ static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
                    test_bit(FLAG_IS_HOGGED, &desc->flags) ||
                    test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
                    test_bit(FLAG_EXPORT, &desc->flags) ||
-                   test_bit(FLAG_SYSFS, &desc->flags))
+                   test_bit(FLAG_SYSFS, &desc->flags) ||
+                   pinctrl_gpio_is_in_use(chip->base + lineinfo.line_offset))
                        lineinfo.flags |= GPIOLINE_FLAG_KERNEL;
                if (test_bit(FLAG_IS_OUT, &desc->flags))
                        lineinfo.flags |= GPIOLINE_FLAG_IS_OUT;
drivers/pinctrl/core.c +23 -0
index a64849a9d1b0..0dd00c175eed 100644
@@ -759,6 +759,29 @@ int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
        return -EINVAL;
 }
 
+bool pinctrl_gpio_is_in_use(unsigned gpio)
+{
+       struct pinctrl_dev *pctldev;
+       struct pinctrl_gpio_range *range;
+       bool result;
+       int pin;
+
+       if (pinctrl_get_device_gpio_range(gpio, &pctldev, &range))
+               return false;
+
+       mutex_lock(&pctldev->mutex);
+
+       /* Convert to the pin controllers number space */
+       pin = gpio_to_pin(range, gpio);
+
+       result = pinmux_is_in_use(pctldev, pin);
+
+       mutex_unlock(&pctldev->mutex);
+
+       return result;
+}
+EXPORT_SYMBOL_GPL(pinctrl_gpio_is_in_use);
+
 /**
  * pinctrl_gpio_request() - request a single pin to be used as GPIO
  * @gpio: the GPIO pin number from the GPIO subsystem number space
drivers/pinctrl/pinmux.c +18 -0
index 020e54f843f9..02d2751a4884 100644
@@ -70,6 +70,24 @@ int pinmux_validate_map(const struct pinctrl_map *map, int i)
        return 0;
 }
 
+bool pinmux_is_in_use(struct pinctrl_dev *pctldev, unsigned pin)
+{
+       struct pin_desc *desc = pin_desc_get(pctldev, pin);
+       const struct pinmux_ops *ops = pctldev->desc->pmxops;
+
+       if (!desc) {
+               dev_err(pctldev->dev,
+                       "pin %u is not registered so it cannot be requested\n",
+                       pin);
+               return false;
+       }
+
+       if (ops->strict && desc->mux_usecount)
+               return true;
+
+       return ops->strict && !!desc->gpio_owner;
+}
+
 /**
  * pin_request() - request a single pin to be muxed in, typically for GPIO
  * @pin: the pin number in the global pin space
drivers/pinctrl/pinmux.h +7 -0
index 794cb3a003ff..24ae61136803 100644
@@ -15,6 +15,8 @@ int pinmux_check_ops(struct pinctrl_dev *pctldev);
 
 int pinmux_validate_map(const struct pinctrl_map *map, int i);
 
+bool pinmux_is_in_use(struct pinctrl_dev *pctldev, unsigned pin);
+
 int pinmux_request_gpio(struct pinctrl_dev *pctldev,
                        struct pinctrl_gpio_range *range,
                        unsigned pin, unsigned gpio);
@@ -42,6 +44,11 @@ static inline int pinmux_validate_map(const struct pinctrl_map *map, int i)
        return 0;
 }
 
+static inline bool pinmux_is_in_use(struct pinctrl_dev *pctldev, unsigned pin)
+{
+       return false;
+}
+
 static inline int pinmux_request_gpio(struct pinctrl_dev *pctldev,
                        struct pinctrl_gpio_range *range,
                        unsigned pin, unsigned gpio)
include/linux/pinctrl/consumer.h +6 -1
index 86720a5a384f..d26826b057a1 100644
@@ -24,6 +24,7 @@ struct device;
 #ifdef CONFIG_PINCTRL
 
 /* External interface to pin control */
+extern bool pinctrl_gpio_is_in_use(unsigned gpio);
 extern int pinctrl_gpio_request(unsigned gpio);
 extern void pinctrl_gpio_free(unsigned gpio);
 extern int pinctrl_gpio_direction_input(unsigned gpio);
@@ -61,6 +62,11 @@ static inline int pinctrl_pm_select_idle_state(struct device *dev)
 
 #else /* !CONFIG_PINCTRL */
 
+static inline bool pinctrl_gpio_is_in_use(unsigned gpio)
+{
+       return 0;
+}
+
 static inline int pinctrl_gpio_request(unsigned gpio)
 {
        return 0;
--
Quoted 2 lines

2.22.0

Looks good to me.

Linus: do you see any issues with that? If not, I can pick it up.

Bart

#2 lore raw
In reply to Ramon Fried
Quoted 7 lines · Ramon Fried

From: Stefan Wahren <[email protected]>

The user space like gpioinfo only see the GPIO usage but not the MUX usage (e.g. I2C or SPI usage) of a pin. As a user we want to know which pin is free/safe to use. So take the MUX usage of strict pinmux controllers into account to get a more realistic view for ioctl GPIO_GET_LINEINFO_IOCTL.

Quoted 8 lines

---
Sending Stefan's RFC as patch, as I tested it and it seems to work, additionally, an accompanying fix was made by me to gpiolibd to fix a display error of the actual result:
https://patchwork.ozlabs.org/patch/1139923/

This is mostly fine, some style nits so I fixed it up when applying.

Yours,
Linus Walleij

#3 lore raw
Quoted 18 lines · Linus Walleij
Quoted 7 lines · Ramon Fried

From: Stefan Wahren <[email protected]>

The user space like gpioinfo only see the GPIO usage but not the MUX usage (e.g. I2C or SPI usage) of a pin. As a user we want to know which pin is free/safe to use. So take the MUX usage of strict pinmux controllers into account to get a more realistic view for ioctl GPIO_GET_LINEINFO_IOCTL.

Quoted 8 lines

---
Sending Stefan's RFC as patch, as I tested it and it seems to work, additionally, an accompanying fix was made by me to gpiolibd to fix a display error of the actual result:
https://patchwork.ozlabs.org/patch/1139923/

Quoted 2 lines

This is mostly fine, some style nits so I fixed it up when applying.

Ooops no. It needs a deeper rework in accordance to my comments last time it was posted. Please read this reply to Stefan's patch and address the comments:

https://lore.kernel.org/linux-gpio/CACRpkdb5DjAMRYkT+b0U6HVk7E6ccLT79-LB=QGQWWtE17aPUg@mail.gmail.com/

Yours,
Linus Walleij

#4 lore raw
Quoted 25 lines · Linus Walleij
Quoted 1 line · Linus Walleij

On Sat, Aug 3, 2019 at 3:34 PM Ramon Fried <[email protected]>

Quoted 5 lines · wrote:
Quoted 5 lines

From: Stefan Wahren <[email protected]>

The user space like gpioinfo only see the GPIO usage but not the MUX usage (e.g. I2C or SPI usage) of a pin. As a user we want to

know which

Quoted 1 line
Quoted 1 line

pin is free/safe to use. So take the MUX usage of strict pinmux

controllers

Quoted 1 line
Quoted 1 line

into account to get a more realistic view for ioctl

GPIO_GET_LINEINFO_IOCTL.

Quoted 7 lines
Quoted 7 lines

---
Sending Stefan's RFC as patch, as I tested it and it seems to work, additionally, an accompanying fix was made by me to gpiolibd to fix

a

Quoted 3 lines
Quoted 2 lines

display error of the actual result:
https://patchwork.ozlabs.org/patch/1139923/

Quoted 7 lines
Quoted 2 lines

This is mostly fine, some style nits so I fixed it up when applying.

Ooops no. It needs a deeper rework in accordance to my comments last time it was posted. Please read this reply to Stefan's patch and address the comments:

NP, I'll try to address these in a new patch.
Thanks.
Ramon

Quoted 4 lines

https://lore.kernel.org/linux-gpio/CACRpkdb5DjAMRYkT+b0U6HVk7E6ccLT79-LB=QGQWWtE17aPUg@mail.gmail.com/

Yours,
Linus Walleij

Signature · 1 line
Sent from my Android device with K-9 Mail. Please excuse my brevity.