Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

leds: multicolor: Use rounded division when calculating color components

Given channel intensity, LED brightness and max LED brightness, the
multicolor LED framework helper led_mc_calc_color_components() computes
the color channel brightness as

chan_brightness = brightness * chan_intensity / max_brightness

Consider the situation when (brightness, intensity, max_brightness) is
for example (16, 15, 255), then chan_brightness is computed to 0
although the fractional divison would give 0.94, which should be rounded
to 1.

Use DIV_ROUND_CLOSEST here for the division to give more realistic
component computation:

chan_brightness = DIV_ROUND_CLOSEST(brightness * chan_intensity,
max_brightness)

Fixes: 55d5d3b46b08 ("leds: multicolor: Introduce a multicolor class definition")
Signed-off-by: Marek Behún <kabel@kernel.org>
Link: https://lore.kernel.org/r/20230801124931.8661-1-kabel@kernel.org
Signed-off-by: Lee Jones <lee@kernel.org>

authored by

Marek Behún and committed by
Lee Jones
065d099f 37d0849e

+5 -3
+5 -3
drivers/leds/led-class-multicolor.c
··· 6 6 #include <linux/device.h> 7 7 #include <linux/init.h> 8 8 #include <linux/led-class-multicolor.h> 9 + #include <linux/math.h> 9 10 #include <linux/module.h> 10 11 #include <linux/slab.h> 11 12 #include <linux/uaccess.h> ··· 20 19 int i; 21 20 22 21 for (i = 0; i < mcled_cdev->num_colors; i++) 23 - mcled_cdev->subled_info[i].brightness = brightness * 24 - mcled_cdev->subled_info[i].intensity / 25 - led_cdev->max_brightness; 22 + mcled_cdev->subled_info[i].brightness = 23 + DIV_ROUND_CLOSEST(brightness * 24 + mcled_cdev->subled_info[i].intensity, 25 + led_cdev->max_brightness); 26 26 27 27 return 0; 28 28 }