Re: [PATCH v15 04/19] leds: multicolor: Introduce a multicolor class definition

From: Jacek Anaszewski
Date: Mon Oct 28 2019 - 16:05:03 EST


Dan,

On 10/28/19 8:36 PM, Dan Murphy wrote:
> Jacek
>
> On 10/28/19 2:22 PM, Jacek Anaszewski wrote:
>> Dan,
>>
>> Thank you for the v15.
>>
>> On 10/28/19 7:36 PM, Dan Murphy wrote:
>>> Introduce a multicolor class that groups colored LEDs
>>> within a LED node.
>>>
>>> The multi color class groups monochrome LEDs and allows controlling two
>>> aspects of the final combined color: hue and lightness. The former is
>>> controlled via <color>_intensity files and the latter is controlled
>>> via brightness file.
>>>
>>> Signed-off-by: Dan Murphy <dmurphy@xxxxxx>
>>> ---
>>> Â .../ABI/testing/sysfs-class-led-multicolorÂÂÂ |Â 36 +++
>>> Â Documentation/leds/index.rstÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ |ÂÂ 1 +
>>>  Documentation/leds/leds-class-multicolor.rst | 100 +++++++
>>> Â drivers/leds/KconfigÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ |Â 10 +
>>> Â drivers/leds/MakefileÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ |ÂÂ 1 +
>>> Â drivers/leds/led-class-multicolor.cÂÂÂÂÂÂÂÂÂÂ | 271 ++++++++++++++++++
>>> Â include/linux/led-class-multicolor.hÂÂÂÂÂÂÂÂÂ | 139 +++++++++
>>> Â 7 files changed, 558 insertions(+)
>>> Â create mode 100644
>>> Documentation/ABI/testing/sysfs-class-led-multicolor
>>> Â create mode 100644 Documentation/leds/leds-class-multicolor.rst
>>> Â create mode 100644 drivers/leds/led-class-multicolor.c
>>> Â create mode 100644 include/linux/led-class-multicolor.h
>>>
>>> diff --git a/Documentation/ABI/testing/sysfs-class-led-multicolor
>>> b/Documentation/ABI/testing/sysfs-class-led-multicolor
>>> new file mode 100644
>>> index 000000000000..3d1f9d726c70
>>> --- /dev/null
>>> +++ b/Documentation/ABI/testing/sysfs-class-led-multicolor
>>> @@ -0,0 +1,36 @@
>>> +What:ÂÂÂÂÂÂÂ /sys/class/leds/<led>/brightness
>>> +Date:ÂÂÂÂÂÂÂ Sept 2019
>>> +KernelVersion:ÂÂÂ 5.5
>>> +Contact:ÂÂÂ Dan Murphy <dmurphy@xxxxxx>
>>> +Description:ÂÂÂ read/write
>>> +ÂÂÂÂÂÂÂ Writing to this file will update all LEDs within the group to a
>>> +ÂÂÂÂÂÂÂ calculated percentage of what each color LED intensity is set
>>> +ÂÂÂÂÂÂÂ to. The percentage is calculated for each grouped LED via the
>>> +ÂÂÂÂÂÂÂ equation below:
>>> +
>>> +ÂÂÂÂÂÂÂ led_brightness = brightness *
>>> <color>_intensity/<color>_max_intensity
>>> +
>>> +ÂÂÂÂÂÂÂ For additional details please refer to
>>> +ÂÂÂÂÂÂÂ Documentation/leds/leds-class-multicolor.rst.
>>> +
>>> +ÂÂÂÂÂÂÂ The value of the color is from 0 to
>>> +ÂÂÂÂÂÂÂ /sys/class/leds/<led>/max_brightness.
>>> +
>>> +What:ÂÂÂÂÂÂÂ /sys/class/leds/<led>/colors/<color>_intensity
>>> +Date:ÂÂÂÂÂÂÂ Sept 2019
>>> +KernelVersion:ÂÂÂ 5.5
>>> +Contact:ÂÂÂ Dan Murphy <dmurphy@xxxxxx>
>>> +Description:ÂÂÂ read/write
>>> +ÂÂÂÂÂÂÂ The <color>_intensity file is created based on the color
>>> +ÂÂÂÂÂÂÂ defined by the registrar of the class.
>>> +ÂÂÂÂÂÂÂ There is one file per color presented.
>>> +
>>> +ÂÂÂÂÂÂÂ The value of the color is from 0 to
>>> +ÂÂÂÂÂÂÂ /sys/class/leds/<led>/colors/<color>_max_intensity.
>>> +
>>> +What:ÂÂÂÂÂÂÂ /sys/class/leds/<led>/colors/<color>_max_intensity
>>> +Date:ÂÂÂÂÂÂÂ Sept 2019
>>> +KernelVersion:ÂÂÂ 5.5
>>> +Contact:ÂÂÂ Dan Murphy <dmurphy@xxxxxx>
>>> +Description:ÂÂÂ read only
>>> +ÂÂÂÂÂÂÂ Maximum intensity level for the LED color.
>>> diff --git a/Documentation/leds/index.rst b/Documentation/leds/index.rst
>>> index 060f4e485897..bc70c6aa7138 100644
>>> --- a/Documentation/leds/index.rst
>>> +++ b/Documentation/leds/index.rst
>>> @@ -9,6 +9,7 @@ LEDs
>>> Â ÂÂÂÂ leds-class
>>> ÂÂÂÂ leds-class-flash
>>> +ÂÂ leds-class-multicolor
>>> ÂÂÂÂ ledtrig-oneshot
>>> ÂÂÂÂ ledtrig-transient
>>> ÂÂÂÂ ledtrig-usbport
>>> diff --git a/Documentation/leds/leds-class-multicolor.rst
>>> b/Documentation/leds/leds-class-multicolor.rst
>>> new file mode 100644
>>> index 000000000000..c8a224cead51
>>> --- /dev/null
>>> +++ b/Documentation/leds/leds-class-multicolor.rst
>>> @@ -0,0 +1,100 @@
>>> +====================================
>>> +Multi Color LED handling under Linux
>>> +====================================
>>> +
>>> +Description
>>> +===========
>>> +The multi color class groups monochrome LEDs and allows controlling two
>>> +aspects of the final combined color: hue and lightness. The former is
>>> +controlled via <color>_intensity files and the latter is controlled
>>> +via brightness file.
>>> +
>>> +For more details on hue and lightness notions please refer to
>>> +https://en.wikipedia.org/wiki/CIECAM02.
>>> +
>>> +Note that intensity files only cache the written value and the actual
>>> +change of hardware state occurs upon writing brightness file. This
>>> +allows for changing many factors of the perceived color in a virtually
>>> +unnoticeable way for the human observer.
>>> +
>>> +Multicolor Class Control
>>> +========================
>>> +The multicolor class presents the LED groups under a directory
>>> called "colors".
>>> +This directory is a child under the LED parent node created by the
>>> led_class
>>> +framework. The led_class framework is documented in led-class.rst
>>> within this
>>> +documentation directory.
>>> +
>>> +Each colored LED will have two files created under the colors directory
>>> +<color>_intensity and <color>_max_intensity. These files will contain
>>> +one of LED_COLOR_ID_* definitions from the header
>>> +include/dt-bindings/leds/common.h.
>>> +
>>> +Directory Layout Example
>>> +========================
>>> +root:/sys/class/leds/rgb:grouped_leds# ls -lR colors/
>> s/rgb/multicolor/ according to the led_colors[LED_COLOR_ID_MULTI].
>
> Ack but this probably would not be multicolor either based on LED node
> naming conventions.

Why? We still have LED color section in the LED name pattern.
And for multicolor LEDs this would make sense.

>>> +-rw-r--r-- 1 root root 4096 Jul 7 03:10 blue_intensity
>>> +-r--r--r-- 1 root root 4096 Jul 7 03:10
>>> blue_max_intensity
>>> +-rw-r--r-- 1 root root 4096 Jul 7 03:10
>>> green_intensity
>>> +-r--r--r-- 1 root root 4096 Jul 7 03:10
>>> green_max_intensity
>>> +-rw-r--r-- 1 root root 4096 Jul 7 03:10 red_intensity
>>> +-r--r--r-- 1 root root 4096 Jul 7 03:10
>>> red_max_intensity
>>> +
>> [...]
>>> +static int led_multicolor_init_color(struct led_classdev_mc
>>> *mcled_cdev,
>>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ int color_id)
>>> +{
>>> +ÂÂÂ struct led_classdev *led_cdev = mcled_cdev->led_cdev;
>>> +ÂÂÂ struct led_mc_color_entry *mc_priv;
>>> +ÂÂÂ char *intensity_file_name;
>>> +ÂÂÂ char *max_intensity_file_name;
>>> +ÂÂÂ size_t len;
>>> +ÂÂÂ int ret;
>>> +
>>> +ÂÂÂ mc_priv = devm_kzalloc(led_cdev->dev, sizeof(*mc_priv),
>>> GFP_KERNEL);
>>> +ÂÂÂ if (!mc_priv)
>>> +ÂÂÂÂÂÂÂ return -ENOMEM;
>>> +
>>> +ÂÂÂ mc_priv->led_color_id = color_id;
>>> +ÂÂÂ mc_priv->mcled_cdev = mcled_cdev;
>>> +
>>> +ÂÂÂ sysfs_attr_init(&mc_priv->intensity_attr.attr);
>>> +ÂÂÂ len = strlen(led_colors[color_id]) + strlen(INTENSITY_NAME) + 1;
>>> +ÂÂÂ intensity_file_name = kzalloc(len, GFP_KERNEL);
>>> +ÂÂÂ if (!intensity_file_name)
>>> +ÂÂÂÂÂÂÂ return -ENOMEM;
>>> +
>>> +ÂÂÂ snprintf(intensity_file_name, len, "%s%s",
>>> +ÂÂÂÂÂÂÂÂ led_colors[color_id], INTENSITY_NAME);
>>> +ÂÂÂ mc_priv->intensity_attr.attr.name = intensity_file_name;
>>> +ÂÂÂ mc_priv->intensity_attr.attr.mode = 0644;
>>> +ÂÂÂ mc_priv->intensity_attr.store = intensity_store;
>>> +ÂÂÂ mc_priv->intensity_attr.show = intensity_show;
>>> +ÂÂÂ ret = sysfs_add_file_to_group(&led_cdev->dev->kobj,
>>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ &mc_priv->intensity_attr.attr,
>>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ led_color_group.name);
>>> +ÂÂÂ if (ret)
>>> +ÂÂÂÂÂÂÂ goto intensity_err_out;
>>> +
>>> +ÂÂÂ sysfs_attr_init(&mc_priv->max_intensity_attr.attr);
>>> +ÂÂÂ len = strlen(led_colors[color_id]) + strlen(MAX_INTENSITY_NAME)
>>> + 1;
>>> +ÂÂÂ max_intensity_file_name = kzalloc(len, GFP_KERNEL);
>>> +ÂÂÂ if (!max_intensity_file_name) {
>>> +ÂÂÂÂÂÂÂ ret = -ENOMEM;
>>> +ÂÂÂÂÂÂÂ goto intensity_err_out;
>>> +ÂÂÂ }
>>> +
>>> +ÂÂÂ snprintf(max_intensity_file_name, len, "%s%s",
>>> +ÂÂÂÂÂÂÂÂ led_colors[color_id], MAX_INTENSITY_NAME);
>>> +ÂÂÂ mc_priv->max_intensity_attr.attr.name = max_intensity_file_name;
>>> +ÂÂÂ mc_priv->max_intensity_attr.attr.mode = 0444;
>>> +ÂÂÂ mc_priv->max_intensity_attr.show = max_intensity_show;
>>> +ÂÂÂ ret = sysfs_add_file_to_group(&led_cdev->dev->kobj,
>>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ &mc_priv->max_intensity_attr.attr,
>>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ led_color_group.name);
>>> +ÂÂÂ if (ret)
>>> +ÂÂÂÂÂÂÂ goto max_intensity_err_out;
>>> +
>>> +ÂÂÂ mc_priv->max_intensity = LED_FULL;
>>> +ÂÂÂ list_add_tail(&mc_priv->list, &mcled_cdev->color_list);
>> I still would prefer dynamically allocated array here. Also there are
>> things I would see differently in the patch 13/19 as already discussed.
>>
>> Nonetheless I am giving my preliminary ack for this series just to
>> give a hint to others to join the review.
>>
> Replies were given on the 13/19 patch.

AFAIR they referred to channel mapping but my request related to
replacing list with array was rather more general. We simply don't
need a list for fixed set of colors.

--
Best regards,
Jacek Anaszewski