base-files: leds: do reverse lookup in get_dt_led()
authorChristian Lamparter <chunkeey@gmail.com>
Wed, 20 Oct 2021 22:35:29 +0000 (00:35 +0200)
committerChristian Lamparter <chunkeey@gmail.com>
Thu, 19 May 2022 14:39:11 +0000 (16:39 +0200)
In order to match the "label"-less DT properties we have to get
creative. In theory the dt-node should have everything (i.e.:
color, function and function-enumerator) to make the device-name
in /sys/class/leds. But thanks to color being a binary value
and not a "string", we would have to maintain a lookup table that
keeps in sync with the dt-binding.

It's much easier to use the "uevent" property of every led-class
device and do reverse lookup with it by comparing the OF_FULLNAME
with the alias pathname.

(This works with gpio-leds ... let's see where it breaks)

Signed-off-by: Christian Lamparter <chunkeey@gmail.com>
package/base-files/files/lib/functions/leds.sh

index a7532faa2fd553f2ebc6555cad2001fc84297e6f..936b165e1cf9298dffe7cfa38e58b05c58391394 100644 (file)
@@ -13,13 +13,26 @@ get_dt_led_path() {
 
 get_dt_led() {
        local label
+       local of_path
        local ledpath=$(get_dt_led_path $1)
 
-       [ -n "$ledpath" ] && \
+       [ -n "$ledpath" ] && {
                label=$(cat "$ledpath/label" 2>/dev/null) || \
                label=$(cat "$ledpath/chan-name" 2>/dev/null) || \
                label=$(basename "$ledpath")
 
+               [ -d "/sys/class/leds/$label" ] || {
+                       label=""
+                       for node in /sys/class/leds/*; do
+                               of_path=$(grep OF_FULLNAME "$node/uevent" | cut -f2 -d'=')
+                               [ "/proc/device-tree$of_path" = "$ledpath" ] && {
+                                       label=$(basename "$node")
+                                       break
+                               }
+                       done
+               }
+       }
+
        echo "$label"
 }