Skip to content

Commit 472a61e

Browse files
Stefan WahrenLinus Walleij
authored andcommitted
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>
1 parent 2dc889a commit 472a61e

5 files changed

Lines changed: 68 additions & 1 deletion

File tree

‎drivers/gpio/gpiolib.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1084,7 +1084,8 @@ static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
10841084
test_bit(FLAG_IS_HOGGED, &desc->flags) ||
10851085
test_bit(FLAG_USED_AS_IRQ, &desc->flags) ||
10861086
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))
10881089
lineinfo.flags |= GPIOLINE_FLAG_KERNEL;
10891090
if (test_bit(FLAG_IS_OUT, &desc->flags))
10901091
lineinfo.flags |= GPIOLINE_FLAG_IS_OUT;

‎drivers/pinctrl/core.c‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,34 @@ int pinctrl_get_group_selector(struct pinctrl_dev *pctldev,
736736
return -EINVAL;
737737
}
738738

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+
739767
/**
740768
* pinctrl_gpio_request() - request a single pin to be used as GPIO
741769
* @gpio: the GPIO pin number from the GPIO subsystem number space

‎drivers/pinctrl/pinmux.c‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,30 @@ int pinmux_validate_map(const struct pinctrl_map *map, int i)
7070
return 0;
7171
}
7272

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+
7397
/**
7498
* pin_request() - request a single pin to be muxed in, typically for GPIO
7599
* @pin: the pin number in the global pin space

‎drivers/pinctrl/pinmux.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ int pinmux_check_ops(struct pinctrl_dev *pctldev);
1515

1616
int pinmux_validate_map(const struct pinctrl_map *map, int i);
1717

18+
bool pinmux_can_be_used_for_gpio(struct pinctrl_dev *pctldev, unsigned pin);
19+
1820
int pinmux_request_gpio(struct pinctrl_dev *pctldev,
1921
struct pinctrl_gpio_range *range,
2022
unsigned pin, unsigned gpio);
@@ -42,6 +44,12 @@ static inline int pinmux_validate_map(const struct pinctrl_map *map, int i)
4244
return 0;
4345
}
4446

47+
static inline bool pinmux_can_be_used_for_gpio(struct pinctrl_dev *pctldev,
48+
unsigned pin)
49+
{
50+
return true;
51+
}
52+
4553
static inline int pinmux_request_gpio(struct pinctrl_dev *pctldev,
4654
struct pinctrl_gpio_range *range,
4755
unsigned pin, unsigned gpio)

‎include/linux/pinctrl/consumer.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ struct device;
2424
#ifdef CONFIG_PINCTRL
2525

2626
/* External interface to pin control */
27+
extern bool pinctrl_gpio_can_use_line(unsigned gpio);
2728
extern int pinctrl_gpio_request(unsigned gpio);
2829
extern void pinctrl_gpio_free(unsigned gpio);
2930
extern int pinctrl_gpio_direction_input(unsigned gpio);
@@ -61,6 +62,11 @@ static inline int pinctrl_pm_select_idle_state(struct device *dev)
6162

6263
#else /* !CONFIG_PINCTRL */
6364

65+
static inline bool pinctrl_gpio_can_use_line(unsigned gpio)
66+
{
67+
return true;
68+
}
69+
6470
static inline int pinctrl_gpio_request(unsigned gpio)
6571
{
6672
return 0;

0 commit comments

Comments
 (0)