From 1069032148f87dca8a5fce427271d324441c9dfe Mon Sep 17 00:00:00 2001 From: Ken Van Hoeylandt Date: Tue, 14 Jul 2026 00:02:40 +0200 Subject: [PATCH] Improve gpio-hog driver --- Devices/lilygo-thmi/lilygo,thmi.dts | 2 ++ .../bindings/tactility,gpio-hog.yaml | 10 ++++---- .../include/tactility/drivers/gpio_hog.h | 10 +++++--- TactilityKernel/source/drivers/gpio_hog.cpp | 23 ++++++++++++++++--- 4 files changed, 35 insertions(+), 10 deletions(-) diff --git a/Devices/lilygo-thmi/lilygo,thmi.dts b/Devices/lilygo-thmi/lilygo,thmi.dts index 6ef4ac943..f17b24b8a 100644 --- a/Devices/lilygo-thmi/lilygo,thmi.dts +++ b/Devices/lilygo-thmi/lilygo,thmi.dts @@ -41,11 +41,13 @@ power_on { compatible = "tactility,gpio-hog"; pin = <&gpio0 14 GPIO_FLAG_NONE>; + mode = ; }; power_en { compatible = "tactility,gpio-hog"; pin = <&gpio0 10 GPIO_FLAG_NONE>; + mode = ; }; adc0 { diff --git a/TactilityKernel/bindings/tactility,gpio-hog.yaml b/TactilityKernel/bindings/tactility,gpio-hog.yaml index 19e0ec76a..1cd39f002 100644 --- a/TactilityKernel/bindings/tactility,gpio-hog.yaml +++ b/TactilityKernel/bindings/tactility,gpio-hog.yaml @@ -13,7 +13,9 @@ properties: type: phandles required: true description: The GPIO pin to hog - output-high: - type: boolean - default: true - description: Level to drive the pin to (true = high, false = low) + mode: + type: int + default: 0 + description: | + Pin mode, see enum GpioHogMode in tactility/drivers/gpio_hog.h: + 0 = GPIO_HOG_MODE_OUTPUT_HIGH, 1 = GPIO_HOG_MODE_OUTPUT_LOW, 2 = GPIO_HOG_MODE_INPUT. diff --git a/TactilityKernel/include/tactility/drivers/gpio_hog.h b/TactilityKernel/include/tactility/drivers/gpio_hog.h index 8ece049c5..6bb159aad 100644 --- a/TactilityKernel/include/tactility/drivers/gpio_hog.h +++ b/TactilityKernel/include/tactility/drivers/gpio_hog.h @@ -5,13 +5,17 @@ extern "C" { #endif -#include - #include +enum GpioHogMode { + GPIO_HOG_MODE_OUTPUT_HIGH, + GPIO_HOG_MODE_OUTPUT_LOW, + GPIO_HOG_MODE_INPUT, +}; + struct GpioHogConfig { struct GpioPinSpec pin; - bool output_high; + enum GpioHogMode mode; }; #ifdef __cplusplus diff --git a/TactilityKernel/source/drivers/gpio_hog.cpp b/TactilityKernel/source/drivers/gpio_hog.cpp index 28b15ca01..d3f08cab0 100644 --- a/TactilityKernel/source/drivers/gpio_hog.cpp +++ b/TactilityKernel/source/drivers/gpio_hog.cpp @@ -24,9 +24,26 @@ static error_t start(Device* device) { return ERROR_RESOURCE; } - if (gpio_descriptor_set_flags(descriptor, GPIO_FLAG_DIRECTION_OUTPUT) != ERROR_NONE || - gpio_descriptor_set_level(descriptor, config->output_high) != ERROR_NONE) { - LOG_E(TAG, "Failed to configure hogged pin"); + bool ok; + switch (config->mode) { + case GPIO_HOG_MODE_OUTPUT_HIGH: + ok = gpio_descriptor_set_flags(descriptor, GPIO_FLAG_DIRECTION_OUTPUT) == ERROR_NONE && + gpio_descriptor_set_level(descriptor, true) == ERROR_NONE; + break; + case GPIO_HOG_MODE_OUTPUT_LOW: + ok = gpio_descriptor_set_flags(descriptor, GPIO_FLAG_DIRECTION_OUTPUT) == ERROR_NONE && + gpio_descriptor_set_level(descriptor, false) == ERROR_NONE; + break; + case GPIO_HOG_MODE_INPUT: + ok = gpio_descriptor_set_flags(descriptor, GPIO_FLAG_DIRECTION_INPUT) == ERROR_NONE; + break; + default: + ok = false; + break; + } + + if (!ok) { + LOG_E(TAG, "Failed to configure hogged pin %u", config->pin.pin); gpio_descriptor_release(descriptor); return ERROR_RESOURCE; }