drivers: led: led_pwm: use pwm_dt_spec

Simplify the driver by using pwm_dt_spec.

TODO: decide if pwm_dt_spec should also store period.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
This commit is contained in:
Gerard Marull-Paretas 2022-04-04 21:16:23 +02:00 committed by Carles Cufí
commit 2bc6279866

View file

@ -21,23 +21,16 @@
#include <logging/log.h> #include <logging/log.h>
LOG_MODULE_REGISTER(led_pwm, CONFIG_LED_LOG_LEVEL); LOG_MODULE_REGISTER(led_pwm, CONFIG_LED_LOG_LEVEL);
struct led_pwm {
const struct device *dev;
uint32_t channel;
uint32_t period;
pwm_flags_t flags;
};
struct led_pwm_config { struct led_pwm_config {
int num_leds; int num_leds;
const struct led_pwm *led; const struct pwm_dt_spec *led;
}; };
static int led_pwm_blink(const struct device *dev, uint32_t led, static int led_pwm_blink(const struct device *dev, uint32_t led,
uint32_t delay_on, uint32_t delay_off) uint32_t delay_on, uint32_t delay_off)
{ {
const struct led_pwm_config *config = dev->config; const struct led_pwm_config *config = dev->config;
const struct led_pwm *led_pwm; const struct pwm_dt_spec *dt_led;
uint32_t period_usec, pulse_usec; uint32_t period_usec, pulse_usec;
if (led >= config->num_leds) { if (led >= config->num_leds) {
@ -54,29 +47,25 @@ static int led_pwm_blink(const struct device *dev, uint32_t led,
return -EINVAL; return -EINVAL;
} }
led_pwm = &config->led[led]; dt_led = &config->led[led];
return pwm_set_usec(led_pwm->dev, led_pwm->channel, period_usec, return pwm_set_usec_dt(dt_led, period_usec, pulse_usec);
pulse_usec, led_pwm->flags);
} }
static int led_pwm_set_brightness(const struct device *dev, static int led_pwm_set_brightness(const struct device *dev,
uint32_t led, uint8_t value) uint32_t led, uint8_t value)
{ {
const struct led_pwm_config *config = dev->config; const struct led_pwm_config *config = dev->config;
const struct led_pwm *led_pwm; const struct pwm_dt_spec *dt_led;
uint32_t pulse;
if (led >= config->num_leds || value > 100) { if (led >= config->num_leds || value > 100) {
return -EINVAL; return -EINVAL;
} }
led_pwm = &config->led[led]; dt_led = &config->led[led];
pulse = led_pwm->period * value / 100; return pwm_set_nsec_pulse_dt(&config->led[led],
dt_led->period * value / 100);
return pwm_set_nsec(led_pwm->dev, led_pwm->channel, led_pwm->period,
pulse, led_pwm->flags);
} }
static int led_pwm_on(const struct device *dev, uint32_t led) static int led_pwm_on(const struct device *dev, uint32_t led)
@ -101,10 +90,10 @@ static int led_pwm_init(const struct device *dev)
} }
for (i = 0; i < config->num_leds; i++) { for (i = 0; i < config->num_leds; i++) {
const struct led_pwm *led = &config->led[i]; const struct pwm_dt_spec *led = &config->led[i];
if (!device_is_ready(led->dev)) { if (!device_is_ready(led->dev)) {
LOG_ERR("%s: pwm device not ready", dev->name); LOG_ERR("%s: pwm device not ready", led->dev->name);
return -ENODEV; return -ENODEV;
} }
} }
@ -121,14 +110,13 @@ static int led_pwm_pm_action(const struct device *dev,
/* switch all underlying PWM devices to the new state */ /* switch all underlying PWM devices to the new state */
for (size_t i = 0; i < config->num_leds; i++) { for (size_t i = 0; i < config->num_leds; i++) {
int err; int err;
const struct led_pwm *led_pwm = &config->led[i]; const struct pwm_dt_spec *led = &config->led[i];
LOG_DBG("PWM %p running pm action %" PRIu32, led_pwm->dev, LOG_DBG("PWM %p running pm action %" PRIu32, led->dev, action);
action);
err = pm_device_action_run(led_pwm->dev, action); err = pm_device_action_run(led->dev, action);
if (err && (err != -EALREADY)) { if (err && (err != -EALREADY)) {
LOG_ERR("Cannot switch PWM %p power state", led_pwm->dev); LOG_ERR("Cannot switch PWM %p power state", led->dev);
} }
} }
@ -143,19 +131,12 @@ static const struct led_driver_api led_pwm_api = {
.set_brightness = led_pwm_set_brightness, .set_brightness = led_pwm_set_brightness,
}; };
#define LED_PWM(led_node_id) \ #define PWM_DT_SPEC_GET_AND_COMMA(node_id) PWM_DT_SPEC_GET(node_id)),
{ \
.dev = DEVICE_DT_GET(DT_PWMS_CTLR(led_node_id)), \
.channel = DT_PWMS_CHANNEL(led_node_id), \
.period = DT_PHA_OR(led_node_id, pwms, period, 100000), \
.flags = DT_PHA_OR(led_node_id, pwms, flags, \
PWM_POLARITY_NORMAL), \
},
#define LED_PWM_DEVICE(id) \ #define LED_PWM_DEVICE(id) \
\ \
static const struct led_pwm led_pwm_##id[] = { \ static const struct pwm_dt_spec led_pwm_##id[] = { \
DT_INST_FOREACH_CHILD(id, LED_PWM) \ DT_INST_FOREACH_CHILD(id, PWM_DT_SPEC_GET_AND_COMMA) \
}; \ }; \
\ \
static const struct led_pwm_config led_pwm_config_##id = { \ static const struct led_pwm_config led_pwm_config_##id = { \