Skip to content

drivers: led: gpio: Add pinctrl support #89797

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions drivers/led/led_gpio.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
#include <zephyr/drivers/gpio.h>
#include <zephyr/device.h>
#include <zephyr/kernel.h>
#include <zephyr/drivers/pinctrl.h>

#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(led_gpio, CONFIG_LED_LOG_LEVEL);

struct led_gpio_config {
size_t num_leds;
const struct gpio_dt_spec *led;
const struct pinctrl_dev_config *pcfg;
};

static int led_gpio_set_brightness(const struct device *dev, uint32_t led, uint8_t value)
Expand Down Expand Up @@ -59,6 +61,12 @@ static int led_gpio_init(const struct device *dev)
err = -ENODEV;
}

err = pinctrl_apply_state(config->pcfg, PINCTRL_STATE_DEFAULT);
if (err < 0 && err != -ENOENT) {
LOG_ERR("failed to apply pinctrl");
return err;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Linux, the default pin configuration is not applied by the end drivers but by the pinctrl driver. I am not sure that's the right way to do that. Let's ask for some advice. @gmarull, what do you think ?


for (size_t i = 0; (i < config->num_leds) && !err; i++) {
const struct gpio_dt_spec *led = &config->led[i];

Expand All @@ -84,6 +92,7 @@ static DEVICE_API(led, led_gpio_api) = {
};

#define LED_GPIO_DEVICE(i) \
PINCTRL_DT_INST_DEFINE(i); \
\
static const struct gpio_dt_spec gpio_dt_spec_##i[] = { \
DT_INST_FOREACH_CHILD_SEP_VARGS(i, GPIO_DT_SPEC_GET, (,), gpios) \
Expand All @@ -92,6 +101,7 @@ static const struct gpio_dt_spec gpio_dt_spec_##i[] = { \
static const struct led_gpio_config led_gpio_config_##i = { \
.num_leds = ARRAY_SIZE(gpio_dt_spec_##i), \
.led = gpio_dt_spec_##i, \
.pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(i), \
}; \
\
DEVICE_DT_INST_DEFINE(i, &led_gpio_init, NULL, \
Expand Down
Loading