1 // SPDX-License-Identifier: GPL-2.0-only
3 * LEDs triggers for power supply class
5 * Copyright © 2007 Anton Vorontsov <cbou@mail.ru>
6 * Copyright © 2004 Szabolcs Gyurko
7 * Copyright © 2003 Ian Molton <spyro@f2s.com>
9 * Modified: 2004, Oct Szabolcs Gyurko
12 #include <linux/kernel.h>
13 #include <linux/device.h>
14 #include <linux/power_supply.h>
15 #include <linux/slab.h>
16 #include <linux/leds.h>
18 #include "power_supply.h"
20 /* Battery specific LEDs triggers. */
22 static void power_supply_update_bat_leds(struct power_supply *psy)
24 union power_supply_propval status;
26 if (power_supply_get_property(psy, POWER_SUPPLY_PROP_STATUS, &status))
29 dev_dbg(&psy->dev, "%s %d\n", __func__, status.intval);
31 switch (status.intval) {
32 case POWER_SUPPLY_STATUS_FULL:
33 led_trigger_event(psy->charging_full_trig, LED_FULL);
34 led_trigger_event(psy->charging_trig, LED_OFF);
35 led_trigger_event(psy->full_trig, LED_FULL);
36 /* Going from blink to LED on requires a LED_OFF event to stop blink */
37 led_trigger_event(psy->charging_blink_full_solid_trig, LED_OFF);
38 led_trigger_event(psy->charging_blink_full_solid_trig, LED_FULL);
40 case POWER_SUPPLY_STATUS_CHARGING:
41 led_trigger_event(psy->charging_full_trig, LED_FULL);
42 led_trigger_event(psy->charging_trig, LED_FULL);
43 led_trigger_event(psy->full_trig, LED_OFF);
44 led_trigger_blink(psy->charging_blink_full_solid_trig, 0, 0);
47 led_trigger_event(psy->charging_full_trig, LED_OFF);
48 led_trigger_event(psy->charging_trig, LED_OFF);
49 led_trigger_event(psy->full_trig, LED_OFF);
50 led_trigger_event(psy->charging_blink_full_solid_trig,
56 static int power_supply_create_bat_triggers(struct power_supply *psy)
58 psy->charging_full_trig_name = kasprintf(GFP_KERNEL,
59 "%s-charging-or-full", psy->desc->name);
60 if (!psy->charging_full_trig_name)
61 goto charging_full_failed;
63 psy->charging_trig_name = kasprintf(GFP_KERNEL,
64 "%s-charging", psy->desc->name);
65 if (!psy->charging_trig_name)
68 psy->full_trig_name = kasprintf(GFP_KERNEL, "%s-full", psy->desc->name);
69 if (!psy->full_trig_name)
72 psy->charging_blink_full_solid_trig_name = kasprintf(GFP_KERNEL,
73 "%s-charging-blink-full-solid", psy->desc->name);
74 if (!psy->charging_blink_full_solid_trig_name)
75 goto charging_blink_full_solid_failed;
77 led_trigger_register_simple(psy->charging_full_trig_name,
78 &psy->charging_full_trig);
79 led_trigger_register_simple(psy->charging_trig_name,
81 led_trigger_register_simple(psy->full_trig_name,
83 led_trigger_register_simple(psy->charging_blink_full_solid_trig_name,
84 &psy->charging_blink_full_solid_trig);
88 charging_blink_full_solid_failed:
89 kfree(psy->full_trig_name);
91 kfree(psy->charging_trig_name);
93 kfree(psy->charging_full_trig_name);
98 static void power_supply_remove_bat_triggers(struct power_supply *psy)
100 led_trigger_unregister_simple(psy->charging_full_trig);
101 led_trigger_unregister_simple(psy->charging_trig);
102 led_trigger_unregister_simple(psy->full_trig);
103 led_trigger_unregister_simple(psy->charging_blink_full_solid_trig);
104 kfree(psy->charging_blink_full_solid_trig_name);
105 kfree(psy->full_trig_name);
106 kfree(psy->charging_trig_name);
107 kfree(psy->charging_full_trig_name);
110 /* Generated power specific LEDs triggers. */
112 static void power_supply_update_gen_leds(struct power_supply *psy)
114 union power_supply_propval online;
116 if (power_supply_get_property(psy, POWER_SUPPLY_PROP_ONLINE, &online))
119 dev_dbg(&psy->dev, "%s %d\n", __func__, online.intval);
122 led_trigger_event(psy->online_trig, LED_FULL);
124 led_trigger_event(psy->online_trig, LED_OFF);
127 static int power_supply_create_gen_triggers(struct power_supply *psy)
129 psy->online_trig_name = kasprintf(GFP_KERNEL, "%s-online",
131 if (!psy->online_trig_name)
134 led_trigger_register_simple(psy->online_trig_name, &psy->online_trig);
139 static void power_supply_remove_gen_triggers(struct power_supply *psy)
141 led_trigger_unregister_simple(psy->online_trig);
142 kfree(psy->online_trig_name);
145 /* Choice what triggers to create&update. */
147 void power_supply_update_leds(struct power_supply *psy)
149 if (psy->desc->type == POWER_SUPPLY_TYPE_BATTERY)
150 power_supply_update_bat_leds(psy);
152 power_supply_update_gen_leds(psy);
155 int power_supply_create_triggers(struct power_supply *psy)
157 if (psy->desc->type == POWER_SUPPLY_TYPE_BATTERY)
158 return power_supply_create_bat_triggers(psy);
159 return power_supply_create_gen_triggers(psy);
162 void power_supply_remove_triggers(struct power_supply *psy)
164 if (psy->desc->type == POWER_SUPPLY_TYPE_BATTERY)
165 power_supply_remove_bat_triggers(psy);
167 power_supply_remove_gen_triggers(psy);