All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] gpiolib: Fix gpio_direction_* for single direction GPIOs
@ 2018-09-21 10:36 Ricardo Ribalda Delgado
  2018-09-21 10:36 ` [PATCH v2] gpiolib: Show correct direction from the beginning Ricardo Ribalda Delgado
  2018-09-25  7:36 ` [PATCH] gpiolib: Fix gpio_direction_* for single direction GPIOs Linus Walleij
  0 siblings, 2 replies; 23+ messages in thread
From: Ricardo Ribalda Delgado @ 2018-09-21 10:36 UTC (permalink / raw)
  To: Linus Walleij, Timur Tabi, swboyd, linux-gpio, LKML
  Cc: Ricardo Ribalda Delgado

GPIOs with no programmable direction are not required to implement
direction_output nor direction_input.

If we try to set an output direction on an output-only GPIO or input
direction on an input-only GPIO simply return 0.

This allows this single direction GPIO to be used by libgpiod.

Signed-off-by: Ricardo Ribalda Delgado <ricardo.ribalda@gmail.com>
---
 drivers/gpio/gpiolib.c | 36 ++++++++++++++++++++++++++++--------
 1 file changed, 28 insertions(+), 8 deletions(-)

diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index a57300c1d649..4b45de883ada 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -2512,19 +2512,27 @@ EXPORT_SYMBOL_GPL(gpiochip_free_own_desc);
 int gpiod_direction_input(struct gpio_desc *desc)
 {
 	struct gpio_chip	*chip;
-	int			status = -EINVAL;
+	int			status = 0;
 
 	VALIDATE_DESC(desc);
 	chip = desc->gdev->chip;
 
-	if (!chip->get || !chip->direction_input) {
+	if (!chip->get && chip->direction_input) {
 		gpiod_warn(desc,
-			"%s: missing get() or direction_input() operations\n",
+			"%s: missing get() and direction_input() operations\n",
 			__func__);
 		return -EIO;
 	}
 
-	status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
+	if (chip->direction_input) {
+		status = chip->direction_input(chip, gpio_chip_hwgpio(desc));
+	} else if (chip->get_direction &&
+		  (chip->get_direction(chip, gpio_chip_hwgpio(desc)) != 1)) {
+		gpiod_warn(desc,
+			"%s: missing direction_input() operation\n",
+			__func__);
+		return -EIO;
+	}
 	if (status == 0)
 		clear_bit(FLAG_IS_OUT, &desc->flags);
 
@@ -2546,16 +2554,28 @@ static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value)
 {
 	struct gpio_chip *gc = desc->gdev->chip;
 	int val = !!value;
-	int ret;
+	int ret = 0;
 
-	if (!gc->set || !gc->direction_output) {
+	if (!gc->set && !gc->direction_output) {
 		gpiod_warn(desc,
-		       "%s: missing set() or direction_output() operations\n",
+		       "%s: missing set() and direction_output() operations\n",
 		       __func__);
 		return -EIO;
 	}
 
-	ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val);
+	if (gc->direction_output) {
+		ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val);
+	} else {
+		if (gc->get_direction &&
+		    gc->get_direction(gc, gpio_chip_hwgpio(desc))) {
+			gpiod_warn(desc,
+				"%s: missing direction_output() operation\n",
+				__func__);
+			return -EIO;
+		}
+		gc->set(gc, gpio_chip_hwgpio(desc), val);
+	}
+
 	if (!ret)
 		set_bit(FLAG_IS_OUT, &desc->flags);
 	trace_gpio_value(desc_to_gpio(desc), 0, val);
-- 
2.18.0

^ permalink raw reply related	[flat|nested] 23+ messages in thread

end of thread, other threads:[~2018-10-02 12:51 UTC | newest]

Thread overview: 23+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-09-21 10:36 [PATCH] gpiolib: Fix gpio_direction_* for single direction GPIOs Ricardo Ribalda Delgado
2018-09-21 10:36 ` [PATCH v2] gpiolib: Show correct direction from the beginning Ricardo Ribalda Delgado
2018-09-21 12:25   ` Timur Tabi
2018-09-27  6:51   ` Stephen Boyd
2018-09-27  6:51     ` Stephen Boyd
2018-09-27 12:19     ` Timur Tabi
2018-09-27 14:04       ` Jeffrey Hugo
2018-09-27 14:19         ` Timur Tabi
2018-09-27 14:34           ` Jeffrey Hugo
2018-09-28 19:14         ` Jeffrey Hugo
2018-09-28 19:22           ` Timur Tabi
2018-09-29  6:23             ` Ricardo Ribalda Delgado
2018-09-29 13:21               ` Timur Tabi
2018-09-29 13:25                 ` Timur Tabi
     [not found]             ` <D3E6F4C4-E1C4-4D88-B118-878576BF5281@gmail.com>
2018-10-01 11:54               ` Linus Walleij
2018-10-01 13:36                 ` Ricardo Ribalda Delgado
2018-10-01 14:27                   ` Jeffrey Hugo
2018-10-01 21:20                   ` Linus Walleij
2018-10-02  7:15                     ` Ricardo Ribalda Delgado
2018-10-02  7:38                       ` Linus Walleij
2018-10-02 12:26                         ` Timur Tabi
2018-10-02 12:51                           ` Linus Walleij
2018-09-25  7:36 ` [PATCH] gpiolib: Fix gpio_direction_* for single direction GPIOs Linus Walleij

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.