Skip to content

Commit

Permalink
drivers: led: lp50xx: check the number of LED colors
Browse files Browse the repository at this point in the history
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 <simon.guinot@sequanux.org>
(cherry picked from commit ffbb8f9)
  • Loading branch information
simonguinot authored and github-actions[bot] committed Feb 18, 2025
1 parent f001cf6 commit ebb5827
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions drivers/led/lp50xx.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ static int lp50xx_set_color(const struct device *dev, uint32_t led,
{
const struct lp50xx_config *config = dev->config;
const struct led_info *led_info = lp50xx_led_to_info(config, led);
uint8_t buf[4];
uint8_t buf[LP50XX_COLORS_PER_LED + 1];
uint8_t i;

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

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

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

static int lp50xx_write_channels(const struct device *dev,
Expand Down Expand Up @@ -266,20 +267,32 @@ static int lp50xx_enable(const struct device *dev, bool enable)
static int lp50xx_init(const struct device *dev)
{
const struct lp50xx_config *config = dev->config;
uint8_t led;
int err;

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

/* Check LED configuration found in DT */
if (config->num_leds > config->max_leds) {
LOG_ERR("%s: invalid number of LEDs %d (max %d)",
dev->name,
config->num_leds,
config->max_leds);
return -EINVAL;
}
for (led = 0; led < config->num_leds; led++) {
const struct led_info *led_info =
lp50xx_led_to_info(config, led);

if (led_info->num_colors > LP50XX_COLORS_PER_LED) {
LOG_ERR("%s: LED %d: invalid number of colors (max %d)",
dev->name, led, LP50XX_COLORS_PER_LED);
return -EINVAL;

Check notice on line 293 in drivers/led/lp50xx.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

You may want to run clang-format on this change

drivers/led/lp50xx.c:293 - const struct led_info *led_info = - lp50xx_led_to_info(config, led); + const struct led_info *led_info = lp50xx_led_to_info(config, led); if (led_info->num_colors > LP50XX_COLORS_PER_LED) { - LOG_ERR("%s: LED %d: invalid number of colors (max %d)", - dev->name, led, LP50XX_COLORS_PER_LED); + LOG_ERR("%s: LED %d: invalid number of colors (max %d)", dev->name, led, + LP50XX_COLORS_PER_LED);

Check notice on line 293 in drivers/led/lp50xx.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

You may want to run clang-format on this change

drivers/led/lp50xx.c:293 - const struct led_info *led_info = - lp50xx_led_to_info(config, led); + const struct led_info *led_info = lp50xx_led_to_info(config, led); if (led_info->num_colors > LP50XX_COLORS_PER_LED) { - LOG_ERR("%s: LED %d: invalid number of colors (max %d)", - dev->name, led, LP50XX_COLORS_PER_LED); + LOG_ERR("%s: LED %d: invalid number of colors (max %d)", dev->name, led, + LP50XX_COLORS_PER_LED);

Check notice on line 293 in drivers/led/lp50xx.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

You may want to run clang-format on this change

drivers/led/lp50xx.c:293 - const struct led_info *led_info = - lp50xx_led_to_info(config, led); + const struct led_info *led_info = lp50xx_led_to_info(config, led); if (led_info->num_colors > LP50XX_COLORS_PER_LED) { - LOG_ERR("%s: LED %d: invalid number of colors (max %d)", - dev->name, led, LP50XX_COLORS_PER_LED); + LOG_ERR("%s: LED %d: invalid number of colors (max %d)", dev->name, led, + LP50XX_COLORS_PER_LED);
}
}

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

0 comments on commit ebb5827

Please sign in to comment.