Skip to content

Commit af81a8b

Browse files
simonguinotmmahadevan108
authored andcommitted
drivers: led: lp50xx: check the number of LED colors
The current code assumes (especially in the lp50xx_set_color function) that the number of LED colors defined in DT is not greater than 3. But since this is not checked, then this is not necessarily the case... This patch consolidates the initialization of the lp50xx LED driver by checking the number of colors for each LED found in DT. Signed-off-by: Simon Guinot <[email protected]> (cherry picked from commit ffbb8f9)
1 parent 546386c commit af81a8b

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

drivers/led/lp50xx.c

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@ static int lp50xx_set_color(const struct device *dev, uint32_t led,
153153
{
154154
const struct lp50xx_config *config = dev->config;
155155
const struct led_info *led_info = lp50xx_led_to_info(config, led);
156-
uint8_t buf[4];
156+
uint8_t buf[LP50XX_COLORS_PER_LED + 1];
157+
uint8_t i;
157158

158159
if (!led_info) {
159160
return -ENODEV;
@@ -170,11 +171,11 @@ static int lp50xx_set_color(const struct device *dev, uint32_t led,
170171
buf[0] = LP50XX_OUT0_COLOR(config->num_modules);
171172
buf[0] += LP50XX_COLORS_PER_LED * led_info->index;
172173

173-
buf[1] = color[0];
174-
buf[2] = color[1];
175-
buf[3] = color[2];
174+
for (i = 0; i < led_info->num_colors; i++) {
175+
buf[1 + i] = color[i];
176+
}
176177

177-
return i2c_write_dt(&config->bus, buf, sizeof(buf));
178+
return i2c_write_dt(&config->bus, buf, led_info->num_colors + 1);
178179
}
179180

180181
static int lp50xx_write_channels(const struct device *dev,
@@ -266,20 +267,32 @@ static int lp50xx_enable(const struct device *dev, bool enable)
266267
static int lp50xx_init(const struct device *dev)
267268
{
268269
const struct lp50xx_config *config = dev->config;
270+
uint8_t led;
269271
int err;
270272

271273
if (!i2c_is_ready_dt(&config->bus)) {
272274
LOG_ERR("%s: I2C device not ready", dev->name);
273275
return -ENODEV;
274276
}
275277

278+
/* Check LED configuration found in DT */
276279
if (config->num_leds > config->max_leds) {
277280
LOG_ERR("%s: invalid number of LEDs %d (max %d)",
278281
dev->name,
279282
config->num_leds,
280283
config->max_leds);
281284
return -EINVAL;
282285
}
286+
for (led = 0; led < config->num_leds; led++) {
287+
const struct led_info *led_info =
288+
lp50xx_led_to_info(config, led);
289+
290+
if (led_info->num_colors > LP50XX_COLORS_PER_LED) {
291+
LOG_ERR("%s: LED %d: invalid number of colors (max %d)",
292+
dev->name, led, LP50XX_COLORS_PER_LED);
293+
return -EINVAL;
294+
}
295+
}
283296

284297
/* Configure GPIO if present */
285298
if (config->gpio_enable.port != NULL) {

0 commit comments

Comments
 (0)