Skip to content
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

[Backport v4.0-branch] drivers: led: lp50xx: check the number of LED colors #85912

Merged
merged 1 commit into from
Feb 26, 2025
Merged
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
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 @@
{
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 @@
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_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
Loading