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/power_supply.h>
15 /* Battery specific LEDs triggers. */
17 static void power_supply_update_bat_leds(struct power_supply
*psy
)
19 union power_supply_propval status
;
21 if (psy
->get_property(psy
, POWER_SUPPLY_PROP_STATUS
, &status
))
24 dev_dbg(psy
->dev
, "%s %d\n", __FUNCTION__
, status
.intval
);
26 switch (status
.intval
) {
27 case POWER_SUPPLY_STATUS_FULL
:
28 led_trigger_event(psy
->charging_full_trig
, LED_FULL
);
29 led_trigger_event(psy
->charging_trig
, LED_OFF
);
30 led_trigger_event(psy
->full_trig
, LED_FULL
);
32 case POWER_SUPPLY_STATUS_CHARGING
:
33 led_trigger_event(psy
->charging_full_trig
, LED_FULL
);
34 led_trigger_event(psy
->charging_trig
, LED_FULL
);
35 led_trigger_event(psy
->full_trig
, LED_OFF
);
38 led_trigger_event(psy
->charging_full_trig
, LED_OFF
);
39 led_trigger_event(psy
->charging_trig
, LED_OFF
);
40 led_trigger_event(psy
->full_trig
, LED_OFF
);
45 static int power_supply_create_bat_triggers(struct power_supply
*psy
)
49 psy
->charging_full_trig_name
= kmalloc(strlen(psy
->name
) +
50 sizeof("-charging-or-full"), GFP_KERNEL
);
51 if (!psy
->charging_full_trig_name
)
52 goto charging_full_failed
;
54 psy
->charging_trig_name
= kmalloc(strlen(psy
->name
) +
55 sizeof("-charging"), GFP_KERNEL
);
56 if (!psy
->charging_trig_name
)
59 psy
->full_trig_name
= kmalloc(strlen(psy
->name
) +
60 sizeof("-full"), GFP_KERNEL
);
61 if (!psy
->full_trig_name
)
64 strcpy(psy
->charging_full_trig_name
, psy
->name
);
65 strcat(psy
->charging_full_trig_name
, "-charging-or-full");
66 strcpy(psy
->charging_trig_name
, psy
->name
);
67 strcat(psy
->charging_trig_name
, "-charging");
68 strcpy(psy
->full_trig_name
, psy
->name
);
69 strcat(psy
->full_trig_name
, "-full");
71 led_trigger_register_simple(psy
->charging_full_trig_name
,
72 &psy
->charging_full_trig
);
73 led_trigger_register_simple(psy
->charging_trig_name
,
75 led_trigger_register_simple(psy
->full_trig_name
,
81 kfree(psy
->charging_trig_name
);
83 kfree(psy
->charging_full_trig_name
);
90 static void power_supply_remove_bat_triggers(struct power_supply
*psy
)
92 led_trigger_unregister_simple(psy
->charging_full_trig
);
93 led_trigger_unregister_simple(psy
->charging_trig
);
94 led_trigger_unregister_simple(psy
->full_trig
);
95 kfree(psy
->full_trig_name
);
96 kfree(psy
->charging_trig_name
);
97 kfree(psy
->charging_full_trig_name
);
100 /* Generated power specific LEDs triggers. */
102 static void power_supply_update_gen_leds(struct power_supply
*psy
)
104 union power_supply_propval online
;
106 if (psy
->get_property(psy
, POWER_SUPPLY_PROP_ONLINE
, &online
))
109 dev_dbg(psy
->dev
, "%s %d\n", __FUNCTION__
, online
.intval
);
112 led_trigger_event(psy
->online_trig
, LED_FULL
);
114 led_trigger_event(psy
->online_trig
, LED_OFF
);
117 static int power_supply_create_gen_triggers(struct power_supply
*psy
)
121 psy
->online_trig_name
= kmalloc(strlen(psy
->name
) + sizeof("-online"),
123 if (!psy
->online_trig_name
)
126 strcpy(psy
->online_trig_name
, psy
->name
);
127 strcat(psy
->online_trig_name
, "-online");
129 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
->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
->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
->type
== POWER_SUPPLY_TYPE_BATTERY
)
165 power_supply_remove_bat_triggers(psy
);
167 power_supply_remove_gen_triggers(psy
);