Skip to content

Commit e48d194

Browse files
author
Linus Walleij
committed
gpio: Add comments on single direction chips
A patch from Ricardo got me thinking about some gpio chip semantics so let's drop in some comments to make things more clear around that. Cc: Ricardo Ribalda Delgado <ricardo.ribalda@gmail.com> Cc: Bartosz Golaszewski <brgl@bgdev.pl> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
1 parent ae9847f commit e48d194

2 files changed

Lines changed: 32 additions & 7 deletions

File tree

‎drivers/gpio/gpiolib.c‎

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2548,20 +2548,31 @@ int gpiod_direction_input(struct gpio_desc *desc)
25482548
VALIDATE_DESC(desc);
25492549
chip = desc->gdev->chip;
25502550

2551+
/*
2552+
* It is legal to have no .get() and .direction_input() specified if
2553+
* the chip is output-only, but you can't specify .direction_input()
2554+
* and not support the .get() operation, that doesn't make sense.
2555+
*/
25512556
if (!chip->get && chip->direction_input) {
25522557
gpiod_warn(desc,
2553-
"%s: missing get() and direction_input() operations\n",
2554-
__func__);
2558+
"%s: missing get() but have direction_input()\n",
2559+
__func__);
25552560
return -EIO;
25562561
}
25572562

2563+
/*
2564+
* If we have a .direction_input() callback, things are simple,
2565+
* just call it. Else we are some input-only chip so try to check the
2566+
* direction (if .get_direction() is supported) else we silently
2567+
* assume we are in input mode after this.
2568+
*/
25582569
if (chip->direction_input) {
25592570
status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
25602571
} else if (chip->get_direction &&
25612572
(chip->get_direction(chip, gpio_chip_hwgpio(desc)) != 1)) {
25622573
gpiod_warn(desc,
2563-
"%s: missing direction_input() operation\n",
2564-
__func__);
2574+
"%s: missing direction_input() operation and line is output\n",
2575+
__func__);
25652576
return -EIO;
25662577
}
25672578
if (status == 0)
@@ -2587,23 +2598,33 @@ static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value)
25872598
int val = !!value;
25882599
int ret = 0;
25892600

2601+
/*
2602+
* It's OK not to specify .direction_output() if the gpiochip is
2603+
* output-only, but if there is then not even a .set() operation it
2604+
* is pretty tricky to drive the output line.
2605+
*/
25902606
if (!gc->set && !gc->direction_output) {
25912607
gpiod_warn(desc,
2592-
"%s: missing set() and direction_output() operations\n",
2593-
__func__);
2608+
"%s: missing set() and direction_output() operations\n",
2609+
__func__);
25942610
return -EIO;
25952611
}
25962612

25972613
if (gc->direction_output) {
25982614
ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val);
25992615
} else {
2616+
/* Check that we are in output mode if we can */
26002617
if (gc->get_direction &&
26012618
gc->get_direction(gc, gpio_chip_hwgpio(desc))) {
26022619
gpiod_warn(desc,
26032620
"%s: missing direction_output() operation\n",
26042621
__func__);
26052622
return -EIO;
26062623
}
2624+
/*
2625+
* If we can't actively set the direction, we are some
2626+
* output-only chip, so just drive the output as desired.
2627+
*/
26072628
gc->set(gc, gpio_chip_hwgpio(desc), val);
26082629
}
26092630

‎include/linux/gpio/driver.h‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,13 @@ static inline struct gpio_irq_chip *to_gpio_irq_chip(struct irq_chip *chip)
178178
* @free: optional hook for chip-specific deactivation, such as
179179
* disabling module power and clock; may sleep
180180
* @get_direction: returns direction for signal "offset", 0=out, 1=in,
181-
* (same as GPIOF_DIR_XXX), or negative error
181+
* (same as GPIOF_DIR_XXX), or negative error.
182+
* It is recommended to always implement this function, even on
183+
* input-only or output-only gpio chips.
182184
* @direction_input: configures signal "offset" as input, or returns error
185+
* This can be omitted on input-only or output-only gpio chips.
183186
* @direction_output: configures signal "offset" as output, or returns error
187+
* This can be omitted on input-only or output-only gpio chips.
184188
* @get: returns value for signal "offset", 0=low, 1=high, or negative error
185189
* @get_multiple: reads values for multiple signals defined by "mask" and
186190
* stores them in "bits", returns 0 on success or negative error

0 commit comments

Comments
 (0)