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

pinctrl/gpio: Take MUX usage into account

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.

Signed-off-by: Stefan Wahren <stefan.wahren@i2se.com>
Tested-by: Ramon Fried <rfried.dev@gmail.com>
Signed-off-by: Ramon Fried <rfried.dev@gmail.com>
Link: https://lore.kernel.org/r/20190814110035.13451-1-ramon.fried@linux.intel.com
Acked-by: Stefan Wahren <stefan.wahren@i2se.com>
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>

authored by

Stefan Wahren and committed by
Linus Walleij
472a61e7 2dc889a8

+68 -1
+2 -1
drivers/gpio/gpiolib.c
··· 1084 1084 test_bit(FLAG_IS_HOGGED, &desc->flags) || 1085 1085 test_bit(FLAG_USED_AS_IRQ, &desc->flags) || 1086 1086 test_bit(FLAG_EXPORT, &desc->flags) || 1087 - test_bit(FLAG_SYSFS, &desc->flags)) 1087 + test_bit(FLAG_SYSFS, &desc->flags) || 1088 + !pinctrl_gpio_can_use_line(chip->base + lineinfo.line_offset)) 1088 1089 lineinfo.flags |= GPIOLINE_FLAG_KERNEL; 1089 1090 if (test_bit(FLAG_IS_OUT, &desc->flags)) 1090 1091 lineinfo.flags |= GPIOLINE_FLAG_IS_OUT;
+28
drivers/pinctrl/core.c
··· 736 736 return -EINVAL; 737 737 } 738 738 739 + bool pinctrl_gpio_can_use_line(unsigned gpio) 740 + { 741 + struct pinctrl_dev *pctldev; 742 + struct pinctrl_gpio_range *range; 743 + bool result; 744 + int pin; 745 + 746 + /* 747 + * Try to obtain GPIO range, if it fails 748 + * we're probably dealing with GPIO driver 749 + * without a backing pin controller - bail out. 750 + */ 751 + if (pinctrl_get_device_gpio_range(gpio, &pctldev, &range)) 752 + return true; 753 + 754 + mutex_lock(&pctldev->mutex); 755 + 756 + /* Convert to the pin controllers number space */ 757 + pin = gpio_to_pin(range, gpio); 758 + 759 + result = pinmux_can_be_used_for_gpio(pctldev, pin); 760 + 761 + mutex_unlock(&pctldev->mutex); 762 + 763 + return result; 764 + } 765 + EXPORT_SYMBOL_GPL(pinctrl_gpio_can_use_line); 766 + 739 767 /** 740 768 * pinctrl_gpio_request() - request a single pin to be used as GPIO 741 769 * @gpio: the GPIO pin number from the GPIO subsystem number space
+24
drivers/pinctrl/pinmux.c
··· 71 71 } 72 72 73 73 /** 74 + * pinmux_can_be_used_for_gpio() - check if a specific pin 75 + * is either muxed to a different function or used as gpio. 76 + * 77 + * @pin: the pin number in the global pin space 78 + * 79 + * Controllers not defined as strict will always return true, 80 + * menaning that the gpio can be used. 81 + */ 82 + bool pinmux_can_be_used_for_gpio(struct pinctrl_dev *pctldev, unsigned pin) 83 + { 84 + struct pin_desc *desc = pin_desc_get(pctldev, pin); 85 + const struct pinmux_ops *ops = pctldev->desc->pmxops; 86 + 87 + /* Can't inspect pin, assume it can be used */ 88 + if (!desc) 89 + return true; 90 + 91 + if (ops->strict && desc->mux_usecount) 92 + return false; 93 + 94 + return !(ops->strict && !!desc->gpio_owner); 95 + } 96 + 97 + /** 74 98 * pin_request() - request a single pin to be muxed in, typically for GPIO 75 99 * @pin: the pin number in the global pin space 76 100 * @owner: a representation of the owner of this pin; typically the device
+8
drivers/pinctrl/pinmux.h
··· 15 15 16 16 int pinmux_validate_map(const struct pinctrl_map *map, int i); 17 17 18 + bool pinmux_can_be_used_for_gpio(struct pinctrl_dev *pctldev, unsigned pin); 19 + 18 20 int pinmux_request_gpio(struct pinctrl_dev *pctldev, 19 21 struct pinctrl_gpio_range *range, 20 22 unsigned pin, unsigned gpio); ··· 42 40 static inline int pinmux_validate_map(const struct pinctrl_map *map, int i) 43 41 { 44 42 return 0; 43 + } 44 + 45 + static inline bool pinmux_can_be_used_for_gpio(struct pinctrl_dev *pctldev, 46 + unsigned pin) 47 + { 48 + return true; 45 49 } 46 50 47 51 static inline int pinmux_request_gpio(struct pinctrl_dev *pctldev,
+6
include/linux/pinctrl/consumer.h
··· 24 24 #ifdef CONFIG_PINCTRL 25 25 26 26 /* External interface to pin control */ 27 + extern bool pinctrl_gpio_can_use_line(unsigned gpio); 27 28 extern int pinctrl_gpio_request(unsigned gpio); 28 29 extern void pinctrl_gpio_free(unsigned gpio); 29 30 extern int pinctrl_gpio_direction_input(unsigned gpio); ··· 61 60 #endif 62 61 63 62 #else /* !CONFIG_PINCTRL */ 63 + 64 + static inline bool pinctrl_gpio_can_use_line(unsigned gpio) 65 + { 66 + return true; 67 + } 64 68 65 69 static inline int pinctrl_gpio_request(unsigned gpio) 66 70 {