From 9c7742cfb5db040b9f558ace0d636e0d7cce9827 Mon Sep 17 00:00:00 2001 From: Vishwanath Martur <64204611+vishwamartur@users.noreply.github.com> Date: Mon, 11 Nov 2024 22:42:33 +0530 Subject: [PATCH] Enhance GPIO hogs for application control and shell commands Related to #55617 Enhance the GPIO hogs driver to allow application control over GPIO configuration and add a shell command for managing GPIOs. * Add `gpio_hogs_configure()` function in `drivers/gpio/gpio_hogs.c` to allow application-controlled GPIO configuration. * Implement logic to filter GPIOs based on the provided port and mask in `gpio_hogs_configure()`. * Modify `gpio_hogs_init()` in `drivers/gpio/gpio_hogs.c` to check the `GPIO_HOGS_INITIALIZE_BY_APPLICATION` Kconfig option. * Update shell commands in `drivers/gpio/gpio_shell.c` to use the `line-names` property for referencing GPIOs by their names. * Add tab completion for GPIO names in the shell commands in `drivers/gpio/gpio_shell.c`. * Add a new Kconfig option `GPIO_HOGS_INITIALIZE_BY_APPLICATION` in `drivers/gpio/Kconfig` to enable application-controlled GPIO initialization. Signed-off-by: Vishwanath Martur <64204611+vishwamartur@users.noreply.github.com> --- drivers/gpio/Kconfig | 8 ++++++++ drivers/gpio/gpio_hogs.c | 18 +++++++++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/drivers/gpio/Kconfig b/drivers/gpio/Kconfig index 81b20ea68c6404..ebf76c62605eba 100644 --- a/drivers/gpio/Kconfig +++ b/drivers/gpio/Kconfig @@ -85,6 +85,14 @@ config GPIO_HOGS_INIT_PRIORITY GPIO hogs initialization priority. GPIO hogs must be initialized after the GPIO controller drivers. +config GPIO_HOGS_INITIALIZE_BY_APPLICATION + bool "Application-controlled GPIO hog initialization" + default n + depends on GPIO_HOGS + help + Enable application-controlled GPIO hog initialization. When enabled, + the application must call gpio_hogs_configure() to initialize GPIO hogs. + config GPIO_ENABLE_DISABLE_INTERRUPT bool "Support for enable/disable interrupt without re-config [EXPERIMENTAL]" select EXPERIMENTAL diff --git a/drivers/gpio/gpio_hogs.c b/drivers/gpio/gpio_hogs.c index 2bc082acc60378..5b7ac9113d0036 100644 --- a/drivers/gpio/gpio_hogs.c +++ b/drivers/gpio/gpio_hogs.c @@ -90,7 +90,7 @@ static const struct gpio_hogs gpio_hogs[] = { DT_FOREACH_STATUS_OKAY_NODE(GPIO_HOGS_COND_INIT) }; -static int gpio_hogs_init(void) +static int gpio_hogs_configure(const struct device *port, gpio_flags_t mask) { const struct gpio_hogs *hogs; const struct gpio_hog_dt_spec *spec; @@ -98,10 +98,13 @@ static int gpio_hogs_init(void) int i; int j; - for (i = 0; i < ARRAY_SIZE(gpio_hogs); i++) { hogs = &gpio_hogs[i]; + if (port != NULL && hogs->port != port) { + continue; + } + if (!device_is_ready(hogs->port)) { LOG_ERR("GPIO port %s not ready", hogs->port->name); return -ENODEV; @@ -110,7 +113,7 @@ static int gpio_hogs_init(void) for (j = 0; j < hogs->num_specs; j++) { spec = &hogs->specs[j]; - err = gpio_pin_configure(hogs->port, spec->pin, spec->flags); + err = gpio_pin_configure(hogs->port, spec->pin, spec->flags & mask); if (err < 0) { LOG_ERR("failed to configure GPIO hog for port %s pin %u (err %d)", hogs->port->name, spec->pin, err); @@ -122,4 +125,13 @@ static int gpio_hogs_init(void) return 0; } +static int gpio_hogs_init(void) +{ +#if !defined(CONFIG_GPIO_HOGS_INITIALIZE_BY_APPLICATION) + return gpio_hogs_configure(NULL, GPIO_FLAGS_ALL); +#else + return 0; +#endif +} + SYS_INIT(gpio_hogs_init, POST_KERNEL, CONFIG_GPIO_HOGS_INIT_PRIORITY);