diff mbox series

[3/3] power: supply: power-supply-leds: Add activate() callback to triggers

Message ID 20240510194012.138192-4-hdegoede@redhat.com
State New
Headers show
Series power: supply: power-supply-leds: Add activate() callback to triggers | expand

Commit Message

Hans de Goede May 10, 2024, 7:40 p.m. UTC
Add an activate() callback to the power-supply LED triggers so that
the LED being activated will properly reflect the current power-supply
state for power-supply devices which are already fully registered
when the trigger gets activated.

This fixes e.g. wrong LED state (1) when the LED gets registered
after the power-supply device.

1) Until the psy driver has a reason to call power_supply_changed()
   which may take quite a while

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
---
 drivers/power/supply/power_supply_leds.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

Comments

Hans de Goede May 31, 2024, 1:21 p.m. UTC | #1
Hi,

On 5/10/24 10:07 PM, Andy Shevchenko wrote:
> On Fri, May 10, 2024 at 09:40:12PM +0200, Hans de Goede wrote:
>> Add an activate() callback to the power-supply LED triggers so that
>> the LED being activated will properly reflect the current power-supply
>> state for power-supply devices which are already fully registered
>> when the trigger gets activated.
>>
>> This fixes e.g. wrong LED state (1) when the LED gets registered
>> after the power-supply device.
>>
>> 1) Until the psy driver has a reason to call power_supply_changed()
>>    which may take quite a while
> 
> ...
> 
>> +static int power_supply_led_trigger_activate(struct led_classdev *led_cdev)
>> +{
>> +	struct power_supply_led_trigger *psy_trig =
>> +		container_of(led_cdev->trigger, struct power_supply_led_trigger, trig);
> 
> Second time same container_of(), perhaps a helper [macro]?

Ack, fixed for v2.

Regards,

Hans



> 
>> +	/* Sync current power-supply state to LED being activated */
>> +	power_supply_update_leds(psy_trig->psy);
>> +	return 0;
>> +}
>
diff mbox series

Patch

diff --git a/drivers/power/supply/power_supply_leds.c b/drivers/power/supply/power_supply_leds.c
index 634f7d33afd0..7ff6d41671b9 100644
--- a/drivers/power/supply/power_supply_leds.c
+++ b/drivers/power/supply/power_supply_leds.c
@@ -24,6 +24,16 @@  struct power_supply_led_trigger {
 	struct power_supply *psy;
 };
 
+static int power_supply_led_trigger_activate(struct led_classdev *led_cdev)
+{
+	struct power_supply_led_trigger *psy_trig =
+		container_of(led_cdev->trigger, struct power_supply_led_trigger, trig);
+
+	/* Sync current power-supply state to LED being activated */
+	power_supply_update_leds(psy_trig->psy);
+	return 0;
+}
+
 static int power_supply_register_led_trigger(struct power_supply *psy,
 					     const char *name_template,
 					     struct led_trigger **tp)
@@ -41,6 +51,7 @@  static int power_supply_register_led_trigger(struct power_supply *psy,
 		goto err_free_trigger;
 	}
 
+	psy_trig->trig.activate = power_supply_led_trigger_activate;
 	psy_trig->psy = psy;
 
 	err = led_trigger_register(&psy_trig->trig);