diff mbox series

[V2,2/2] lib: utils:/gpio: Move the invert logic of ACTIVE_LOW to gpio

Message ID 20211025070410.1228846-3-wxjstz@126.com
State Not Applicable
Headers show
Series lib: utils:/gpio: Improve the gpio driver | expand

Commit Message

Xiang W Oct. 25, 2021, 7:04 a.m. UTC
In order to reduce the repetitive code of the driver, move the invert
logic of ACTIVE_LOW to gpio

Signed-off-by: Xiang W <wxjstz@126.com>
---
 lib/utils/gpio/gpio.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/lib/utils/gpio/gpio.c b/lib/utils/gpio/gpio.c
index fb30c0f..e7d1bed 100644
--- a/lib/utils/gpio/gpio.c
+++ b/lib/utils/gpio/gpio.c
@@ -91,17 +91,20 @@  int gpio_direction_output(struct gpio_pin *gp, int value)
 	if (!gp->chip->direction_output)
 		return SBI_ENOSYS;
 
+	value = (gp->flags & GPIO_FLAG_ACTIVE_LOW) ? !value : value;
 	return gp->chip->direction_output(gp, value);
 }
 
 int gpio_get(struct gpio_pin *gp)
 {
+	int value;
 	if (!gp || !gp->chip || (gp->chip->ngpio <= gp->offset))
 		return SBI_EINVAL;
 	if (!gp->chip->get)
 		return SBI_ENOSYS;
 
-	return gp->chip->get(gp);
+	value = gp->chip->get(gp);
+	return (gp->flags & GPIO_FLAG_ACTIVE_LOW) ? !value : value;
 }
 
 int gpio_set(struct gpio_pin *gp, int value)
@@ -111,6 +114,7 @@  int gpio_set(struct gpio_pin *gp, int value)
 	if (!gp->chip->set)
 		return SBI_ENOSYS;
 
+	value = (gp->flags & GPIO_FLAG_ACTIVE_LOW) ? !value : value;
 	gp->chip->set(gp, value);
 	return 0;
 }