Skip to content

Commit 2bc6279

Browse files
gmarullcarlescufi
authored andcommitted
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 <[email protected]>
1 parent 9e964d0 commit 2bc6279

File tree

1 file changed

+17
-36
lines changed

1 file changed

+17
-36
lines changed

drivers/led/led_pwm.c

Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,16 @@
2121
#include <logging/log.h>
2222
LOG_MODULE_REGISTER(led_pwm, CONFIG_LED_LOG_LEVEL);
2323

24-
struct led_pwm {
25-
const struct device *dev;
26-
uint32_t channel;
27-
uint32_t period;
28-
pwm_flags_t flags;
29-
};
30-
3124
struct led_pwm_config {
3225
int num_leds;
33-
const struct led_pwm *led;
26+
const struct pwm_dt_spec *led;
3427
};
3528

3629
static int led_pwm_blink(const struct device *dev, uint32_t led,
3730
uint32_t delay_on, uint32_t delay_off)
3831
{
3932
const struct led_pwm_config *config = dev->config;
40-
const struct led_pwm *led_pwm;
33+
const struct pwm_dt_spec *dt_led;
4134
uint32_t period_usec, pulse_usec;
4235

4336
if (led >= config->num_leds) {
@@ -54,29 +47,25 @@ static int led_pwm_blink(const struct device *dev, uint32_t led,
5447
return -EINVAL;
5548
}
5649

57-
led_pwm = &config->led[led];
50+
dt_led = &config->led[led];
5851

59-
return pwm_set_usec(led_pwm->dev, led_pwm->channel, period_usec,
60-
pulse_usec, led_pwm->flags);
52+
return pwm_set_usec_dt(dt_led, period_usec, pulse_usec);
6153
}
6254

6355
static int led_pwm_set_brightness(const struct device *dev,
6456
uint32_t led, uint8_t value)
6557
{
6658
const struct led_pwm_config *config = dev->config;
67-
const struct led_pwm *led_pwm;
68-
uint32_t pulse;
59+
const struct pwm_dt_spec *dt_led;
6960

7061
if (led >= config->num_leds || value > 100) {
7162
return -EINVAL;
7263
}
7364

74-
led_pwm = &config->led[led];
75-
76-
pulse = led_pwm->period * value / 100;
65+
dt_led = &config->led[led];
7766

78-
return pwm_set_nsec(led_pwm->dev, led_pwm->channel, led_pwm->period,
79-
pulse, led_pwm->flags);
67+
return pwm_set_nsec_pulse_dt(&config->led[led],
68+
dt_led->period * value / 100);
8069
}
8170

8271
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)
10190
}
10291

10392
for (i = 0; i < config->num_leds; i++) {
104-
const struct led_pwm *led = &config->led[i];
93+
const struct pwm_dt_spec *led = &config->led[i];
10594

10695
if (!device_is_ready(led->dev)) {
107-
LOG_ERR("%s: pwm device not ready", dev->name);
96+
LOG_ERR("%s: pwm device not ready", led->dev->name);
10897
return -ENODEV;
10998
}
11099
}
@@ -121,14 +110,13 @@ static int led_pwm_pm_action(const struct device *dev,
121110
/* switch all underlying PWM devices to the new state */
122111
for (size_t i = 0; i < config->num_leds; i++) {
123112
int err;
124-
const struct led_pwm *led_pwm = &config->led[i];
113+
const struct pwm_dt_spec *led = &config->led[i];
125114

126-
LOG_DBG("PWM %p running pm action %" PRIu32, led_pwm->dev,
127-
action);
115+
LOG_DBG("PWM %p running pm action %" PRIu32, led->dev, action);
128116

129-
err = pm_device_action_run(led_pwm->dev, action);
117+
err = pm_device_action_run(led->dev, action);
130118
if (err && (err != -EALREADY)) {
131-
LOG_ERR("Cannot switch PWM %p power state", led_pwm->dev);
119+
LOG_ERR("Cannot switch PWM %p power state", led->dev);
132120
}
133121
}
134122

@@ -143,19 +131,12 @@ static const struct led_driver_api led_pwm_api = {
143131
.set_brightness = led_pwm_set_brightness,
144132
};
145133

146-
#define LED_PWM(led_node_id) \
147-
{ \
148-
.dev = DEVICE_DT_GET(DT_PWMS_CTLR(led_node_id)), \
149-
.channel = DT_PWMS_CHANNEL(led_node_id), \
150-
.period = DT_PHA_OR(led_node_id, pwms, period, 100000), \
151-
.flags = DT_PHA_OR(led_node_id, pwms, flags, \
152-
PWM_POLARITY_NORMAL), \
153-
},
134+
#define PWM_DT_SPEC_GET_AND_COMMA(node_id) PWM_DT_SPEC_GET(node_id)),
154135

155136
#define LED_PWM_DEVICE(id) \
156137
\
157-
static const struct led_pwm led_pwm_##id[] = { \
158-
DT_INST_FOREACH_CHILD(id, LED_PWM) \
138+
static const struct pwm_dt_spec led_pwm_##id[] = { \
139+
DT_INST_FOREACH_CHILD(id, PWM_DT_SPEC_GET_AND_COMMA) \
159140
}; \
160141
\
161142
static const struct led_pwm_config led_pwm_config_##id = { \

0 commit comments

Comments
 (0)