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>
15 #include <linux/slab.h>
17 #include "power_supply.h"
19 /* Battery specific LEDs triggers. */
21 static void power_supply_update_bat_leds(struct power_supply
*psy
)
23 union power_supply_propval status
;
25 if (psy
->get_property(psy
, POWER_SUPPLY_PROP_STATUS
, &status
))
28 dev_dbg(psy
->dev
, "%s %d\n", __func__
, status
.intval
);
30 switch (status
.intval
) {
31 case POWER_SUPPLY_STATUS_FULL
:
32 led_trigger_event(psy
->charging_full_trig
, LED_FULL
);
33 led_trigger_event(psy
->charging_trig
, LED_OFF
);
34 led_trigger_event(psy
->full_trig
, LED_FULL
);
36 case POWER_SUPPLY_STATUS_CHARGING
:
37 led_trigger_event(psy
->charging_full_trig
, LED_FULL
);
38 led_trigger_event(psy
->charging_trig
, LED_FULL
);
39 led_trigger_event(psy
->full_trig
, LED_OFF
);
42 led_trigger_event(psy
->charging_full_trig
, LED_OFF
);
43 led_trigger_event(psy
->charging_trig
, LED_OFF
);
44 led_trigger_event(psy
->full_trig
, LED_OFF
);
49 static int power_supply_create_bat_triggers(struct power_supply
*psy
)
53 psy
->charging_full_trig_name
= kasprintf(GFP_KERNEL
,
54 "%s-charging-or-full", psy
->name
);
55 if (!psy
->charging_full_trig_name
)
56 goto charging_full_failed
;
58 psy
->charging_trig_name
= kasprintf(GFP_KERNEL
,
59 "%s-charging", psy
->name
);
60 if (!psy
->charging_trig_name
)
63 psy
->full_trig_name
= kasprintf(GFP_KERNEL
, "%s-full", psy
->name
);
64 if (!psy
->full_trig_name
)
67 led_trigger_register_simple(psy
->charging_full_trig_name
,
68 &psy
->charging_full_trig
);
69 led_trigger_register_simple(psy
->charging_trig_name
,
71 led_trigger_register_simple(psy
->full_trig_name
,
77 kfree(psy
->charging_trig_name
);
79 kfree(psy
->charging_full_trig_name
);
86 static void power_supply_remove_bat_triggers(struct power_supply
*psy
)
88 led_trigger_unregister_simple(psy
->charging_full_trig
);
89 led_trigger_unregister_simple(psy
->charging_trig
);
90 led_trigger_unregister_simple(psy
->full_trig
);
91 kfree(psy
->full_trig_name
);
92 kfree(psy
->charging_trig_name
);
93 kfree(psy
->charging_full_trig_name
);
96 /* Generated power specific LEDs triggers. */
98 static void power_supply_update_gen_leds(struct power_supply
*psy
)
100 union power_supply_propval online
;
102 if (psy
->get_property(psy
, POWER_SUPPLY_PROP_ONLINE
, &online
))
105 dev_dbg(psy
->dev
, "%s %d\n", __func__
, online
.intval
);
108 led_trigger_event(psy
->online_trig
, LED_FULL
);
110 led_trigger_event(psy
->online_trig
, LED_OFF
);
113 static int power_supply_create_gen_triggers(struct power_supply
*psy
)
117 psy
->online_trig_name
= kasprintf(GFP_KERNEL
, "%s-online", psy
->name
);
118 if (!psy
->online_trig_name
)
121 led_trigger_register_simple(psy
->online_trig_name
, &psy
->online_trig
);
131 static void power_supply_remove_gen_triggers(struct power_supply
*psy
)
133 led_trigger_unregister_simple(psy
->online_trig
);
134 kfree(psy
->online_trig_name
);
137 /* Choice what triggers to create&update. */
139 void power_supply_update_leds(struct power_supply
*psy
)
141 if (psy
->type
== POWER_SUPPLY_TYPE_BATTERY
)
142 power_supply_update_bat_leds(psy
);
144 power_supply_update_gen_leds(psy
);
147 int power_supply_create_triggers(struct power_supply
*psy
)
149 if (psy
->type
== POWER_SUPPLY_TYPE_BATTERY
)
150 return power_supply_create_bat_triggers(psy
);
151 return power_supply_create_gen_triggers(psy
);
154 void power_supply_remove_triggers(struct power_supply
*psy
)
156 if (psy
->type
== POWER_SUPPLY_TYPE_BATTERY
)
157 power_supply_remove_bat_triggers(psy
);
159 power_supply_remove_gen_triggers(psy
);