Re: [PATCH v12 12/16] leds: lp55xx: Add multicolor framework support to lp55xx

From: Jacek Anaszewski
Date: Sat Oct 12 2019 - 09:27:14 EST


Dan,

On 10/12/19 2:50 AM, Dan Murphy wrote:
> Jacek
>
> On 10/11/19 3:36 PM, Jacek Anaszewski wrote:
>> Dan,
>>
>> On 10/11/19 3:06 PM, Dan Murphy wrote:
>>> Add multicolor framework support for the lp55xx family.
>>>
>>> Signed-off-by: Dan Murphy <dmurphy@xxxxxx>
>>> ---
>>> Â drivers/leds/KconfigÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ |ÂÂ 1 +
>>> Â drivers/leds/leds-lp55xx-common.cÂÂÂÂÂÂÂÂ | 176 +++++++++++++++++++---
>>> Â drivers/leds/leds-lp55xx-common.hÂÂÂÂÂÂÂÂ |ÂÂ 9 ++
>>> Â include/linux/platform_data/leds-lp55xx.h |ÂÂ 7 +
>>> Â 4 files changed, 169 insertions(+), 24 deletions(-)
>>>
>>> diff --git a/drivers/leds/Kconfig b/drivers/leds/Kconfig
>>> index fb614a6b9afa..5706bf8d8bd1 100644
>>> --- a/drivers/leds/Kconfig
>>> +++ b/drivers/leds/Kconfig
>>> @@ -377,6 +377,7 @@ config LEDS_LP50XX
>>> Â config LEDS_LP55XX_COMMON
>>> ÂÂÂÂÂ tristate "Common Driver for TI/National
>>> LP5521/5523/55231/5562/8501"
>>> ÂÂÂÂÂ depends on LEDS_LP5521 || LEDS_LP5523 || LEDS_LP5562 ||
>>> LEDS_LP8501
>>> +ÂÂÂ depends on OF
>>> ÂÂÂÂÂ select FW_LOADER
>>> ÂÂÂÂÂ select FW_LOADER_USER_HELPER
>>> ÂÂÂÂÂ help
>>> diff --git a/drivers/leds/leds-lp55xx-common.c
>>> b/drivers/leds/leds-lp55xx-common.c
>>> index 44ced02b49f9..1417df6df8a7 100644
>>> --- a/drivers/leds/leds-lp55xx-common.c
>>> +++ b/drivers/leds/leds-lp55xx-common.c
>>> @@ -131,14 +131,54 @@ static struct attribute *lp55xx_led_attrs[] = {
>>> Â };
>>> Â ATTRIBUTE_GROUPS(lp55xx_led);
>>> Â +static int lp55xx_map_channel(struct lp55xx_led *led, int color_id,
>>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ enum led_brightness brightness)
>>> +{
>>> +ÂÂÂ int i;
>>> +
>>> +ÂÂÂ for (i = 0; i < led->mc_cdev.num_leds; i++) {
>>> +ÂÂÂÂÂÂÂ if (led->color_component[i].color_id == color_id) {
>>> +ÂÂÂÂÂÂÂÂÂÂÂ led->color_component[i].brightness = brightness;
>>> +ÂÂÂÂÂÂÂÂÂÂÂ return 0;
>>> +ÂÂÂÂÂÂÂ }
>>> +ÂÂÂ }
>>> +
>>> +ÂÂÂ return -EINVAL;
>>> +}
>>> +
>>> Â static int lp55xx_set_brightness(struct led_classdev *cdev,
>>> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ enum led_brightness brightness)
>>> Â {
>>> +ÂÂÂ struct led_mc_color_conversion
>>> color_component[LP55XX_MAX_GROUPED_CHAN];
>>> ÂÂÂÂÂ struct lp55xx_led *led = cdev_to_lp55xx_led(cdev);
>>> ÂÂÂÂÂ struct lp55xx_device_config *cfg = led->chip->cfg;
>>> +ÂÂÂ int ret;
>>> +ÂÂÂ int i;
>>> Â -ÂÂÂ led->brightness = (u8)brightness;
>>> -ÂÂÂ return cfg->brightness_fn(led);
>>> +ÂÂÂ if (led->mc_cdev.num_leds > 1) {
>>> +ÂÂÂÂÂÂÂ if (!cfg->color_intensity_fn)
>>> +ÂÂÂÂÂÂÂÂÂÂÂ return -EINVAL;
>>> +
>>> +ÂÂÂÂÂÂÂ led_mc_calc_color_components(&led->mc_cdev, brightness,
>>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ color_component);
>>> +
>>> +ÂÂÂÂÂÂÂ for (i = 0; i < led->mc_cdev.num_leds; i++) {
>>> +ÂÂÂÂÂÂÂÂÂÂÂ ret = lp55xx_map_channel(led,
>>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ color_component[i].color_id,
>>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ color_component[i].brightness);
>>> +ÂÂÂÂÂÂÂÂÂÂÂ if (ret)
>>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ return ret;
>>> +ÂÂÂÂÂÂÂ }
>>> +
>>> +ÂÂÂÂÂÂÂ ret = cfg->color_intensity_fn(led);
>>> +ÂÂÂÂÂÂÂ if (ret)
>>> +ÂÂÂÂÂÂÂÂÂÂÂ return ret;
>>> +ÂÂÂ } else {
>>> +ÂÂÂÂÂÂÂ led->brightness = (u8)brightness;
>>> +ÂÂÂÂÂÂÂ ret = cfg->brightness_fn(led);
>>> +ÂÂÂ }
>>> +
>>> +ÂÂÂ return ret;
>>> Â }
>>> Â Â static int lp55xx_init_led(struct lp55xx_led *led,
>>> @@ -147,9 +187,9 @@ static int lp55xx_init_led(struct lp55xx_led *led,
>>> ÂÂÂÂÂ struct lp55xx_platform_data *pdata = chip->pdata;
>>> ÂÂÂÂÂ struct lp55xx_device_config *cfg = chip->cfg;
>>> ÂÂÂÂÂ struct device *dev = &chip->cl->dev;
>>> +ÂÂÂ int max_channel = cfg->max_channel;
>>> ÂÂÂÂÂ char name[32];
>>> ÂÂÂÂÂ int ret;
>>> -ÂÂÂ int max_channel = cfg->max_channel;
>>> Â ÂÂÂÂÂ if (chan >= max_channel) {
>>> ÂÂÂÂÂÂÂÂÂ dev_err(dev, "invalid channel: %d / %d\n", chan, max_channel);
>>> @@ -159,10 +199,34 @@ static int lp55xx_init_led(struct lp55xx_led *led,
>>> ÂÂÂÂÂ if (pdata->led_config[chan].led_current == 0)
>>> ÂÂÂÂÂÂÂÂÂ return 0;
>>> Â +ÂÂÂ if (pdata->led_config[chan].name) {
>>> +ÂÂÂÂÂÂÂ led->cdev.name = pdata->led_config[chan].name;
>>> +ÂÂÂ } else {
>>> +ÂÂÂÂÂÂÂ snprintf(name, sizeof(name), "%s:channel%d",
>>> +ÂÂÂÂÂÂÂÂÂÂÂ pdata->label ? : chip->cl->name, chan);
>>> +ÂÂÂÂÂÂÂ led->cdev.name = name;
>>> +ÂÂÂ }
>>> +
>>> +ÂÂÂ if (pdata->led_config[chan].num_colors > 1) {
>>> +ÂÂÂÂÂÂÂ led->mc_cdev.led_cdev = &led->cdev;
>>> +ÂÂÂÂÂÂÂ led->cdev.brightness_set_blocking = lp55xx_set_brightness;
>>> +ÂÂÂÂÂÂÂ led->cdev.groups = lp55xx_led_groups;
>>> +ÂÂÂÂÂÂÂ led->mc_cdev.num_leds = pdata->led_config[chan].num_colors;
>>> +ÂÂÂÂÂÂÂ led->mc_cdev.available_colors =
>>> +ÂÂÂÂÂÂÂÂÂÂÂ pdata->led_config[chan].available_colors;
>>> +ÂÂÂÂÂÂÂ memcpy(led->color_component,
>>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂ pdata->led_config[chan].color_component,
>>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂ sizeof(led->color_component));
>>> +ÂÂÂ } else {
>>> +
>>> +ÂÂÂÂÂÂÂ led->cdev.default_trigger =
>>> +ÂÂÂÂÂÂÂÂÂÂÂ pdata->led_config[chan].default_trigger;
>>> +ÂÂÂÂÂÂÂ led->cdev.brightness_set_blocking = lp55xx_set_brightness;
>>> +ÂÂÂ }ÂÂÂ led->cdev.groups = lp55xx_led_groups;
>>> +
>>> ÂÂÂÂÂ led->led_current = pdata->led_config[chan].led_current;
>>> ÂÂÂÂÂ led->max_current = pdata->led_config[chan].max_current;
>>> ÂÂÂÂÂ led->chan_nr = pdata->led_config[chan].chan_nr;
>>> -ÂÂÂ led->cdev.default_trigger =
>>> pdata->led_config[chan].default_trigger;
>>> Â ÂÂÂÂÂ if (led->chan_nr >= max_channel) {
>>> ÂÂÂÂÂÂÂÂÂ dev_err(dev, "Use channel numbers between 0 and %d\n",
>>> @@ -170,18 +234,11 @@ static int lp55xx_init_led(struct lp55xx_led *led,
>>> ÂÂÂÂÂÂÂÂÂ return -EINVAL;
>>> ÂÂÂÂÂ }
>>> Â -ÂÂÂ led->cdev.brightness_set_blocking = lp55xx_set_brightness;
>>> -ÂÂÂ led->cdev.groups = lp55xx_led_groups;
>>> -
>>> -ÂÂÂ if (pdata->led_config[chan].name) {
>>> -ÂÂÂÂÂÂÂ led->cdev.name = pdata->led_config[chan].name;
>>> -ÂÂÂ } else {
>>> -ÂÂÂÂÂÂÂ snprintf(name, sizeof(name), "%s:channel%d",
>>> -ÂÂÂÂÂÂÂÂÂÂÂ pdata->label ? : chip->cl->name, chan);
>>> -ÂÂÂÂÂÂÂ led->cdev.name = name;
>>> -ÂÂÂ }
>>> +ÂÂÂ if (pdata->led_config[chan].num_colors > 1)
>>> +ÂÂÂÂÂÂÂ ret = led_classdev_multicolor_register(dev, &led->mc_cdev);
>>> +ÂÂÂ else
>>> +ÂÂÂÂÂÂÂ ret = led_classdev_register(dev, &led->cdev);
>> Why not devm versions?
>
> I will change the multicolor but not the led_classdev

Having one devm and the other not will look awkward.
This does not seem to be a big effort. I propose to add a preceding
patch that will first switch monochrome led_classdev_register to devm,
and of course clean up error paths (remove lp55xx_unregister_leds()),
and update error paths in all callers which will be just removing
the calls to lp55xx_unregister_leds().

>>
>>> -ÂÂÂ ret = led_classdev_register(dev, &led->cdev);
>>> ÂÂÂÂÂ if (ret) {
>>> ÂÂÂÂÂÂÂÂÂ dev_err(dev, "led register err: %d\n", ret);
>>> ÂÂÂÂÂÂÂÂÂ return ret;
>>> @@ -466,7 +523,6 @@ int lp55xx_register_leds(struct lp55xx_led *led,
>>> struct lp55xx_chip *chip)
>>> ÂÂÂÂÂÂÂÂÂ dev_err(&chip->cl->dev, "empty brightness configuration\n");
>>> ÂÂÂÂÂÂÂÂÂ return -EINVAL;
>>> ÂÂÂÂÂ }
>>> -
>> This empty line removal was not intended I suppose.
>
> I can put it back
>
>
>>
>>> ÂÂÂÂÂ for (i = 0; i < num_channels; i++) {
>>> Â ÂÂÂÂÂÂÂÂÂ /* do not initialize channels that are not connected */
>>> @@ -538,6 +594,82 @@ void lp55xx_unregister_sysfs(struct lp55xx_chip
>>> *chip)
>>> Â }
>>> Â EXPORT_SYMBOL_GPL(lp55xx_unregister_sysfs);
>>> Â +static int lp5xx_parse_common_child(struct device_node *np,
>>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ struct lp55xx_led_config *cfg,
>>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ int chan_num, bool is_multicolor,
>>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ int color_num)
>>> +{
>>> +ÂÂÂ u32 led_number;
>>> +ÂÂÂ int ret;
>>> +
>>> +ÂÂÂ of_property_read_string(np, "chan-name",
>>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ &cfg[chan_num].name);
>>> +ÂÂÂ of_property_read_u8(np, "led-cur",
>>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ &cfg[chan_num].led_current);
>>> +ÂÂÂ of_property_read_u8(np, "max-cur",
>>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ &cfg[chan_num].max_current);
>>> +
>>> +ÂÂÂ ret = of_property_read_u32(np, "reg", &led_number);
>>> +ÂÂÂ if (ret)
>>> +ÂÂÂÂÂÂÂ return ret;
>>> +
>>> +ÂÂÂ if (led_number < 0 || led_number > 6)
>>> +ÂÂÂÂÂÂÂ return -EINVAL;
>>> +
>>> +ÂÂÂ if (is_multicolor)
>>> +ÂÂÂÂÂÂÂ cfg[chan_num].color_component[color_num].output_num =
>>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ led_number;
>>> +ÂÂÂ else
>>> +ÂÂÂÂÂÂÂ cfg[chan_num].chan_nr = led_number;
>>> +
>>> +ÂÂÂ return 0;
>>> +}
>>> +
>>> +static int lp5xx_parse_channel_child(struct device_node *np,
>>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ struct lp55xx_led_config *cfg,
>>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ int child_number)
>>> +{
>>> +ÂÂÂ struct device_node *child;
>>> +ÂÂÂ int channel_color;
>>> +ÂÂÂ int num_colors = 0;
>>> +ÂÂÂ u32 color_id;
>>> +ÂÂÂ int ret;
>>> +
>>> +ÂÂÂ cfg[child_number].default_trigger =
>>> +ÂÂÂÂÂÂÂÂÂÂÂ of_get_property(np, "linux,default-trigger", NULL);
>>> +
>>> +ÂÂÂ ret = of_property_read_u32(np, "color", &channel_color);
>>> +ÂÂÂ if (ret)
>>> +ÂÂÂÂÂÂÂ channel_color = ret;
>>> +
>>> +
>>> +ÂÂÂ if (channel_color == LED_COLOR_ID_MULTI) {
>>> +ÂÂÂÂÂÂÂ for_each_child_of_node(np, child) {
>>> +ÂÂÂÂÂÂÂÂÂÂÂ ret = lp5xx_parse_common_child(child, cfg,
>>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ child_number, true,
>>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ num_colors);
>>> +ÂÂÂÂÂÂÂÂÂÂÂ if (ret)
>>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ return ret;
>>> +
>>> +ÂÂÂÂÂÂÂÂÂÂÂ ret = of_property_read_u32(child, "color", &color_id);
>>> +ÂÂÂÂÂÂÂÂÂÂÂ if (ret)
>>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ return ret;
>>> +
>>> +ÂÂÂÂÂÂÂÂÂÂÂ cfg[child_number].color_component[num_colors].color_id =
>>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ color_id;
>>> +ÂÂÂÂÂÂÂÂÂÂÂ set_bit(color_id, &cfg[child_number].available_colors);
>>> +ÂÂÂÂÂÂÂÂÂÂÂ num_colors++;
>>> +ÂÂÂÂÂÂÂ }
>>> +
>>> +ÂÂÂÂÂÂÂ cfg[child_number].num_colors = num_colors;
>>> +ÂÂÂ } else {
>>> +ÂÂÂÂÂÂÂ return lp5xx_parse_common_child(np, cfg, child_number, false,
>>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ num_colors);
>>> +ÂÂÂ }
>>> +
>>> +ÂÂÂ return 0;
>>> +}
>>> +
>>> Â struct lp55xx_platform_data *lp55xx_of_populate_pdata(struct device
>>> *dev,
>>> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ struct device_node *np)
>>> Â {
>>> @@ -546,6 +678,7 @@ struct lp55xx_platform_data
>>> *lp55xx_of_populate_pdata(struct device *dev,
>>> ÂÂÂÂÂ struct lp55xx_led_config *cfg;
>>> ÂÂÂÂÂ int num_channels;
>>> ÂÂÂÂÂ int i = 0;
>>> +ÂÂÂ int ret;
>>> Â ÂÂÂÂÂ pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
>>> ÂÂÂÂÂ if (!pdata)
>>> @@ -565,14 +698,9 @@ struct lp55xx_platform_data
>>> *lp55xx_of_populate_pdata(struct device *dev,
>>> ÂÂÂÂÂ pdata->num_channels = num_channels;
>>> Â ÂÂÂÂÂ for_each_child_of_node(np, child) {
>>> -ÂÂÂÂÂÂÂ cfg[i].chan_nr = i;
>>> -
>>> -ÂÂÂÂÂÂÂ of_property_read_string(child, "chan-name", &cfg[i].name);
>>> -ÂÂÂÂÂÂÂ of_property_read_u8(child, "led-cur", &cfg[i].led_current);
>>> -ÂÂÂÂÂÂÂ of_property_read_u8(child, "max-cur", &cfg[i].max_current);
>>> -ÂÂÂÂÂÂÂ cfg[i].default_trigger =
>>> -ÂÂÂÂÂÂÂÂÂÂÂ of_get_property(child, "linux,default-trigger", NULL);
>>> -
>>> +ÂÂÂÂÂÂÂ ret = lp5xx_parse_channel_child(child, cfg, i);
>>> +ÂÂÂÂÂÂÂ if (ret)
>>> +ÂÂÂÂÂÂÂÂÂÂÂ return ERR_PTR(-EINVAL);
>>> ÂÂÂÂÂÂÂÂÂ i++;
>>> ÂÂÂÂÂ }
>>> Â diff --git a/drivers/leds/leds-lp55xx-common.h
>>> b/drivers/leds/leds-lp55xx-common.h
>>> index 783ed5103ce5..75d57fb835c3 100644
>>> --- a/drivers/leds/leds-lp55xx-common.h
>>> +++ b/drivers/leds/leds-lp55xx-common.h
>>> @@ -12,6 +12,8 @@
>>> Â #ifndef _LEDS_LP55XX_COMMON_H
>>> Â #define _LEDS_LP55XX_COMMON_H
>>> Â +#include <linux/led-class-multicolor.h>
>>> +
>>> Â enum lp55xx_engine_index {
>>> ÂÂÂÂÂ LP55XX_ENGINE_INVALID,
>>> ÂÂÂÂÂ LP55XX_ENGINE_1,
>>> @@ -109,6 +111,9 @@ struct lp55xx_device_config {
>>> ÂÂÂÂÂ /* access brightness register */
>>> ÂÂÂÂÂ int (*brightness_fn)(struct lp55xx_led *led);
>>> Â +ÂÂÂ /* access specific brightness register */
>>> +ÂÂÂ int (*color_intensity_fn)(struct lp55xx_led *led);
>> Wouldn't multicolor_brightness_fn be more accurate now ?
>
> So just change the name to multicolor_brightness_fn?

And the op implementations in the clients, like:

s/lp5521_led_intensity/lp5521_led_mc_brightness/

--
Best regards,
Jacek Anaszewski