2 * LEDs triggers for power supply class
4 * Copyright © 2007 Anton Vorontsov <cbou@mail.ru>
5 * Copyright © 2004 Szabolcs Gyurko
6 * Copyright © 2003 Ian Molton <spyro@f2s.com>
8 * Modified: 2004, Oct Szabolcs Gyurko
10 * You may use this code as per GPL version 2
13 #include <linux/kernel.h>
14 #include <linux/power_supply.h>
16 #include "power_supply.h"
18 /* Battery specific LEDs triggers. */
20 static void power_supply_update_bat_leds(struct power_supply *psy)
22 union power_supply_propval status;
24 if (psy->get_property(psy, POWER_SUPPLY_PROP_STATUS, &status))
27 dev_dbg(psy->dev, "%s %d\n", __func__, status.intval);
29 switch (status.intval) {
30 case POWER_SUPPLY_STATUS_FULL:
31 led_trigger_event(psy->charging_full_trig, LED_FULL);
32 led_trigger_event(psy->charging_trig, LED_OFF);
33 led_trigger_event(psy->full_trig, LED_FULL);
35 case POWER_SUPPLY_STATUS_CHARGING:
36 led_trigger_event(psy->charging_full_trig, LED_FULL);
37 led_trigger_event(psy->charging_trig, LED_FULL);
38 led_trigger_event(psy->full_trig, LED_OFF);
41 led_trigger_event(psy->charging_full_trig, LED_OFF);
42 led_trigger_event(psy->charging_trig, LED_OFF);
43 led_trigger_event(psy->full_trig, LED_OFF);
48 static int power_supply_create_bat_triggers(struct power_supply *psy)
52 psy->charging_full_trig_name = kasprintf(GFP_KERNEL,
53 "%s-charging-or-full", psy->name);
54 if (!psy->charging_full_trig_name)
55 goto charging_full_failed;
57 psy->charging_trig_name = kasprintf(GFP_KERNEL,
58 "%s-charging", psy->name);
59 if (!psy->charging_trig_name)
62 psy->full_trig_name = kasprintf(GFP_KERNEL, "%s-full", psy->name);
63 if (!psy->full_trig_name)
66 led_trigger_register_simple(psy->charging_full_trig_name,
67 &psy->charging_full_trig);
68 led_trigger_register_simple(psy->charging_trig_name,
70 led_trigger_register_simple(psy->full_trig_name,
76 kfree(psy->charging_trig_name);
78 kfree(psy->charging_full_trig_name);
85 static void power_supply_remove_bat_triggers(struct power_supply *psy)
87 led_trigger_unregister_simple(psy->charging_full_trig);
88 led_trigger_unregister_simple(psy->charging_trig);
89 led_trigger_unregister_simple(psy->full_trig);
90 kfree(psy->full_trig_name);
91 kfree(psy->charging_trig_name);
92 kfree(psy->charging_full_trig_name);
95 /* Generated power specific LEDs triggers. */
97 static void power_supply_update_gen_leds(struct power_supply *psy)
99 union power_supply_propval online;
101 if (psy->get_property(psy, POWER_SUPPLY_PROP_ONLINE, &online))
104 dev_dbg(psy->dev, "%s %d\n", __func__, online.intval);
107 led_trigger_event(psy->online_trig, LED_FULL);
109 led_trigger_event(psy->online_trig, LED_OFF);
112 static int power_supply_create_gen_triggers(struct power_supply *psy)
116 psy->online_trig_name = kasprintf(GFP_KERNEL, "%s-online", psy->name);
117 if (!psy->online_trig_name)
120 led_trigger_register_simple(psy->online_trig_name, &psy->online_trig);
130 static void power_supply_remove_gen_triggers(struct power_supply *psy)
132 led_trigger_unregister_simple(psy->online_trig);
133 kfree(psy->online_trig_name);
136 /* Choice what triggers to create&update. */
138 void power_supply_update_leds(struct power_supply *psy)
140 if (psy->type == POWER_SUPPLY_TYPE_BATTERY)
141 power_supply_update_bat_leds(psy);
143 power_supply_update_gen_leds(psy);
146 int power_supply_create_triggers(struct power_supply *psy)
148 if (psy->type == POWER_SUPPLY_TYPE_BATTERY)
149 return power_supply_create_bat_triggers(psy);
150 return power_supply_create_gen_triggers(psy);
153 void power_supply_remove_triggers(struct power_supply *psy)
155 if (psy->type == POWER_SUPPLY_TYPE_BATTERY)
156 power_supply_remove_bat_triggers(psy);
158 power_supply_remove_gen_triggers(psy);