[PATCH v1] led: add function naming option from linux

Heiko Schocher hs at denx.de
Sun Jan 19 06:41:28 CET 2025


Hi!

On 18.01.25 14:22, Heiko Schocher wrote:
> in linux we have the option to create the name of a led
> optionally through the following properties:
> 
> - function
> - color
> - function-enumerator
> 
> This patch adds support for parsing this properties if there
> is no label property.
> 
> To be as close as possible to linux import the following files
> from it:
> 
> include/dt-bindings/leds/common.h
> include/linux/uapi/linux/uleds.h
> 
> base commit was:
> commit: b8f52214c61a ("Merge tag 'audit-pr-20241205' of git://git.kernel.org/pub/scm/linux/kernel/git/pcmoore/audit")
> 
> The led name is created in led_post_bind() and we need some
> storage place for it. Currently this patch prevents to use
> malloc() instead it stores the name in new member :
> 
>          char name[LED_MAX_NAME_SIZE];
> 
> of struct led_uc_plat. While at it append led tests for the
> new feature.
> 
> Signed-off-by: Heiko Schocher <hs at denx.de>
> ---
> as we introduce a new element in struct led_uc_priv, may we should add
> this functionality through a new Kconfig option? (Which also would make
> this optional and save some bytes for boards who do not need this...)
> 
> Ignored a lot of checkpatch warnigs for file include/dt-bindings/leds/common.h
> 
> WARNING: Block comments use * on subsequent lines
> +/*   Obsolete equivalents: "tpacpi::thinklight" (IBM/Lenovo Thinkpads),
> +     "lp5523:kb{1,2,3,4,5,6}" (Nokia N900) */
> WARNING: Block comments use a trailing */ on a separate line
> +     "lp5523:kb{1,2,3,4,5,6}" (Nokia N900) */
> WARNING: Block comments use * on subsequent lines
> +/* System LEDs, usually found on system body.
> +   platform::mute (etc) is sometimes seen, :mute would be better */
> WARNING: Block comments use a trailing */ on a separate line
> +   platform::mute (etc) is sometimes seen, :mute would be better */
> WARNING: Block comments use * on subsequent lines
> +/*   Used RGB notification LEDs common on phones.
> +     Obsolete equivalents: "status-led:{red,green,blue}" (Motorola Droid 4),
> WARNING: Block comments use a trailing */ on a separate line
> +     "lp5523:{r,g,b}" (Nokia N900) */
> 
> as it is copied from linux
> 
> commit: b8f52214c61a ("Merge tag 'audit-pr-20241205' of git://git.kernel.org/pub/scm/linux/kernel/git/pcmoore/audit")
> 
> Azure build:
> https://dev.azure.com/hs0298/hs/_build/results?buildId=148&view=results
> 
> 
>   arch/sandbox/dts/test.dts                |  37 +++++++-
>   doc/device-tree-bindings/leds/common.txt |  28 ++++++
>   drivers/led/led-uclass.c                 |  72 +++++++++++++-
>   include/dt-bindings/leds/common.h        | 114 +++++++++++++++++++++++
>   include/led.h                            |   2 +
>   include/linux/uapi/linux/uleds.h         |  25 +++++
>   test/dm/led.c                            |  22 ++++-
>   test/dm/ofnode.c                         |   2 +-
>   8 files changed, 295 insertions(+), 7 deletions(-)
>   create mode 100644 include/dt-bindings/leds/common.h
>   create mode 100644 include/linux/uapi/linux/uleds.h
> 
[...]
> diff --git a/drivers/led/led-uclass.c b/drivers/led/led-uclass.c
> index 760750568c0..564abf7eaca 100644
> --- a/drivers/led/led-uclass.c
> +++ b/drivers/led/led-uclass.c
> @@ -13,6 +13,25 @@
>   #include <dm/lists.h>
>   #include <dm/root.h>
>   #include <dm/uclass-internal.h>
> +#include <dt-bindings/leds/common.h>
> +
> +static const char * const led_colors[LED_COLOR_ID_MAX] = {
> +	[LED_COLOR_ID_WHITE] = "white",
> +	[LED_COLOR_ID_RED] = "red",
> +	[LED_COLOR_ID_GREEN] = "green",
> +	[LED_COLOR_ID_BLUE] = "blue",
> +	[LED_COLOR_ID_AMBER] = "amber",
> +	[LED_COLOR_ID_VIOLET] = "violet",
> +	[LED_COLOR_ID_YELLOW] = "yellow",
> +	[LED_COLOR_ID_IR] = "ir",
> +	[LED_COLOR_ID_MULTI] = "multicolor",
> +	[LED_COLOR_ID_RGB] = "rgb",
> +	[LED_COLOR_ID_PURPLE] = "purple",
> +	[LED_COLOR_ID_ORANGE] = "orange",
> +	[LED_COLOR_ID_PINK] = "pink",
> +	[LED_COLOR_ID_CYAN] = "cyan",
> +	[LED_COLOR_ID_LIME] = "lime",
> +};
>   
>   int led_bind_generic(struct udevice *parent, const char *driver_name)
>   {
> @@ -232,11 +251,56 @@ int led_activity_blink(void)
>   #endif
>   #endif
>   
> -static const char *led_get_label(ofnode node)
> +static const char *led_get_function_name(struct udevice *dev)
> +{
> +	struct led_uc_plat *uc_plat;
> +	const char *func;
> +	u32 color;
> +	u32 enumerator;
> +	int ret;
> +	int cp;
> +
> +	if (!dev)
> +		return NULL;
> +
> +	uc_plat = dev_get_uclass_plat(dev);
> +	if (!uc_plat)
> +		return NULL;
> +
> +	if (uc_plat->label)
> +		return uc_plat->label;
> +
> +	/* Now try to detect function label name */
> +	func = dev_read_string(dev, "function");
> +	cp = dev_read_u32(dev, "color", &color);
> +	if (cp == 0 || func) {
> +		ret = dev_read_u32(dev, "function-enumerator", &enumerator);
> +		if (!ret) {
> +			snprintf(uc_plat->name, LED_MAX_NAME_SIZE,
> +				 "%s:%s-%d",
> +				 cp ? "" : led_colors[color],
> +				 func ? func : "", enumerator);
> +		} else {
> +			snprintf(uc_plat->name, LED_MAX_NAME_SIZE,
> +				 "%s:%s",
> +				 cp ? "" : led_colors[color],
> +				 func ? func : "");
> +		}
> +		uc_plat->label = uc_plat->name;
> +	}
> +
> +	return uc_plat->label;
> +}
> +
> +static const char *led_get_label(struct udevice *dev, ofnode node)
>   {
>   	const char *label;
> +	const char *name;
>   
> +	name = ofnode_get_name(node);

name is not used, crap from debug... I remove it in v2, but wait for
some comments before posting it.

[...]

bye,
Heiko
-- 
DENX Software Engineering GmbH,      Managing Director: Erika Unter
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: +49-8142-66989-52   Fax: +49-8142-66989-80   Email: hs at denx.de


More information about the U-Boot mailing list