Skip to content

Commit ef70bbe

Browse files
pH5Linus Walleij
authored andcommitted
gpio: make gpiod_direction_output take a logical value
The documentation was not clear about whether gpio_direction_output should take a logical value or the physical level on the output line, i.e. whether the ACTIVE_LOW status would be taken into account. This converts gpiod_direction_output to use the logical level and adds a new gpiod_direction_output_raw for the raw value. Signed-off-by: Philipp Zabel <p.zabel@pengutronix.de> Reviewed-by: Alexandre Courbot <acourbot@nvidia.com> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
1 parent 25b35da commit ef70bbe

4 files changed

Lines changed: 57 additions & 20 deletions

File tree

‎Documentation/gpio/consumer.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ raw line value:
154154
void gpiod_set_raw_value(struct gpio_desc *desc, int value)
155155
int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
156156
void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value)
157+
int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
157158

158159
The active-low state of a GPIO can also be queried using the following call:
159160

‎drivers/gpio/gpiolib.c‎

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -350,9 +350,9 @@ static ssize_t gpio_direction_store(struct device *dev,
350350
if (!test_bit(FLAG_EXPORT, &desc->flags))
351351
status = -EIO;
352352
else if (sysfs_streq(buf, "high"))
353-
status = gpiod_direction_output(desc, 1);
353+
status = gpiod_direction_output_raw(desc, 1);
354354
else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
355-
status = gpiod_direction_output(desc, 0);
355+
status = gpiod_direction_output_raw(desc, 0);
356356
else if (sysfs_streq(buf, "in"))
357357
status = gpiod_direction_input(desc);
358358
else
@@ -1590,7 +1590,7 @@ int gpio_request_one(unsigned gpio, unsigned long flags, const char *label)
15901590
if (flags & GPIOF_DIR_IN)
15911591
err = gpiod_direction_input(desc);
15921592
else
1593-
err = gpiod_direction_output(desc,
1593+
err = gpiod_direction_output_raw(desc,
15941594
(flags & GPIOF_INIT_HIGH) ? 1 : 0);
15951595

15961596
if (err)
@@ -1756,28 +1756,13 @@ int gpiod_direction_input(struct gpio_desc *desc)
17561756
}
17571757
EXPORT_SYMBOL_GPL(gpiod_direction_input);
17581758

1759-
/**
1760-
* gpiod_direction_output - set the GPIO direction to input
1761-
* @desc: GPIO to set to output
1762-
* @value: initial output value of the GPIO
1763-
*
1764-
* Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1765-
* be called safely on it. The initial value of the output must be specified.
1766-
*
1767-
* Return 0 in case of success, else an error code.
1768-
*/
1769-
int gpiod_direction_output(struct gpio_desc *desc, int value)
1759+
static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
17701760
{
17711761
unsigned long flags;
17721762
struct gpio_chip *chip;
17731763
int status = -EINVAL;
17741764
int offset;
17751765

1776-
if (!desc || !desc->chip) {
1777-
pr_warn("%s: invalid GPIO\n", __func__);
1778-
return -EINVAL;
1779-
}
1780-
17811766
/* GPIOs used for IRQs shall not be set as output */
17821767
if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) {
17831768
gpiod_err(desc,
@@ -1840,6 +1825,50 @@ int gpiod_direction_output(struct gpio_desc *desc, int value)
18401825
gpiod_dbg(desc, "%s: gpio status %d\n", __func__, status);
18411826
return status;
18421827
}
1828+
1829+
/**
1830+
* gpiod_direction_output_raw - set the GPIO direction to output
1831+
* @desc: GPIO to set to output
1832+
* @value: initial output value of the GPIO
1833+
*
1834+
* Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1835+
* be called safely on it. The initial value of the output must be specified
1836+
* as raw value on the physical line without regard for the ACTIVE_LOW status.
1837+
*
1838+
* Return 0 in case of success, else an error code.
1839+
*/
1840+
int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
1841+
{
1842+
if (!desc || !desc->chip) {
1843+
pr_warn("%s: invalid GPIO\n", __func__);
1844+
return -EINVAL;
1845+
}
1846+
return _gpiod_direction_output_raw(desc, value);
1847+
}
1848+
EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
1849+
1850+
/**
1851+
* gpiod_direction_output - set the GPIO direction to input
1852+
* @desc: GPIO to set to output
1853+
* @value: initial output value of the GPIO
1854+
*
1855+
* Set the direction of the passed GPIO to output, such as gpiod_set_value() can
1856+
* be called safely on it. The initial value of the output must be specified
1857+
* as the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into
1858+
* account.
1859+
*
1860+
* Return 0 in case of success, else an error code.
1861+
*/
1862+
int gpiod_direction_output(struct gpio_desc *desc, int value)
1863+
{
1864+
if (!desc || !desc->chip) {
1865+
pr_warn("%s: invalid GPIO\n", __func__);
1866+
return -EINVAL;
1867+
}
1868+
if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
1869+
value = !value;
1870+
return _gpiod_direction_output_raw(desc, value);
1871+
}
18431872
EXPORT_SYMBOL_GPL(gpiod_direction_output);
18441873

18451874
/**

‎include/asm-generic/gpio.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ static inline int gpio_direction_input(unsigned gpio)
6969
}
7070
static inline int gpio_direction_output(unsigned gpio, int value)
7171
{
72-
return gpiod_direction_output(gpio_to_desc(gpio), value);
72+
return gpiod_direction_output_raw(gpio_to_desc(gpio), value);
7373
}
7474

7575
static inline int gpio_set_debounce(unsigned gpio, unsigned debounce)

‎include/linux/gpio/consumer.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ void devm_gpiod_put(struct device *dev, struct gpio_desc *desc);
3636
int gpiod_get_direction(const struct gpio_desc *desc);
3737
int gpiod_direction_input(struct gpio_desc *desc);
3838
int gpiod_direction_output(struct gpio_desc *desc, int value);
39+
int gpiod_direction_output_raw(struct gpio_desc *desc, int value);
3940

4041
/* Value get/set from non-sleeping context */
4142
int gpiod_get_value(const struct gpio_desc *desc);
@@ -121,6 +122,12 @@ static inline int gpiod_direction_output(struct gpio_desc *desc, int value)
121122
WARN_ON(1);
122123
return -ENOSYS;
123124
}
125+
static inline int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
126+
{
127+
/* GPIO can never have been requested */
128+
WARN_ON(1);
129+
return -ENOSYS;
130+
}
124131

125132

126133
static inline int gpiod_get_value(const struct gpio_desc *desc)

0 commit comments

Comments
 (0)