Currently we use the `linux,default-trigger` device tree property of a LED to define the default trigger which should be activated for a LED. But the LED device tree binding also documents the `trigger-sources` property, which specifies the source device which should be triggering the LED. The `trigger-sources` property is currently implemented only in drivers/usb/core/ledtrig-usbport.c. Lets add a method to struct led_trigger which, if implemented, can check whether this trigger should be enabled as default. This check shall be done by checking whether the specified `trigger-sources` refers to a device compatible with the trigger. Signed-off-by: Marek Behún <marek.behun@xxxxxx> Cc: Rob Herring <robh+dt@xxxxxxxxxx> Cc: devicetree@xxxxxxxxxxxxxxx --- drivers/leds/led-triggers.c | 26 ++++++++++++++++++-------- include/linux/leds.h | 6 ++++++ 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/drivers/leds/led-triggers.c b/drivers/leds/led-triggers.c index 91da90cfb11d9..c96577f0bfe97 100644 --- a/drivers/leds/led-triggers.c +++ b/drivers/leds/led-triggers.c @@ -243,18 +243,30 @@ void led_trigger_remove(struct led_classdev *led_cdev) } EXPORT_SYMBOL_GPL(led_trigger_remove); +static bool trigger_is_default(struct led_classdev *led_cdev, + struct led_trigger *trig) +{ + if (!trigger_relevant(led_cdev, trig)) + return false; + + if (led_cdev->default_trigger && + !strcmp(led_cdev->default_trigger, trig->name)) + return true; + + if (trig->has_valid_source && trig->has_valid_source(led_cdev)) + return true; + + return false; +} + void led_trigger_set_default(struct led_classdev *led_cdev) { struct led_trigger *trig; - if (!led_cdev->default_trigger) - return; - down_read(&triggers_list_lock); down_write(&led_cdev->trigger_lock); list_for_each_entry(trig, &trigger_list, next_trig) { - if (!strcmp(led_cdev->default_trigger, trig->name) && - trigger_relevant(led_cdev, trig)) { + if (trigger_is_default(led_cdev, trig)) { led_cdev->flags |= LED_INIT_DEFAULT_TRIGGER; led_trigger_set(led_cdev, trig); break; @@ -306,9 +318,7 @@ int led_trigger_register(struct led_trigger *trig) down_read(&leds_list_lock); list_for_each_entry(led_cdev, &leds_list, node) { down_write(&led_cdev->trigger_lock); - if (!led_cdev->trigger && led_cdev->default_trigger && - !strcmp(led_cdev->default_trigger, trig->name) && - trigger_relevant(led_cdev, trig)) { + if (!led_cdev->trigger && trigger_is_default(led_cdev, trig)) { led_cdev->flags |= LED_INIT_DEFAULT_TRIGGER; led_trigger_set(led_cdev, trig); } diff --git a/include/linux/leds.h b/include/linux/leds.h index 6a8d6409c993e..4cbb826e4bec4 100644 --- a/include/linux/leds.h +++ b/include/linux/leds.h @@ -352,6 +352,12 @@ struct led_trigger { int (*activate)(struct led_classdev *led_cdev); void (*deactivate)(struct led_classdev *led_cdev); + /* + * Check whether LED has defined valid source for this trigger. + * If yes, this trigger should be set as default trigger for LED. + */ + bool (*has_valid_source)(struct led_classdev *led_cdev); + /* LED-private triggers have this set */ struct led_hw_trigger_type *trigger_type; -- 2.26.2