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/device.h>
15 #include <linux/power_supply.h>
16 #include <linux/slab.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
;
25 unsigned long delay_on
= 0;
26 unsigned long delay_off
= 0;
28 if (power_supply_get_property(psy
, POWER_SUPPLY_PROP_STATUS
, &status
))
31 dev_dbg(&psy
->dev
, "%s %d\n", __func__
, status
.intval
);
33 switch (status
.intval
) {
34 case POWER_SUPPLY_STATUS_FULL
:
35 led_trigger_event(psy
->charging_full_trig
, LED_FULL
);
36 led_trigger_event(psy
->charging_trig
, LED_OFF
);
37 led_trigger_event(psy
->full_trig
, LED_FULL
);
38 led_trigger_event(psy
->charging_blink_full_solid_trig
,
41 case POWER_SUPPLY_STATUS_CHARGING
:
42 led_trigger_event(psy
->charging_full_trig
, LED_FULL
);
43 led_trigger_event(psy
->charging_trig
, LED_FULL
);
44 led_trigger_event(psy
->full_trig
, LED_OFF
);
45 led_trigger_blink(psy
->charging_blink_full_solid_trig
,
46 &delay_on
, &delay_off
);
49 led_trigger_event(psy
->charging_full_trig
, LED_OFF
);
50 led_trigger_event(psy
->charging_trig
, LED_OFF
);
51 led_trigger_event(psy
->full_trig
, LED_OFF
);
52 led_trigger_event(psy
->charging_blink_full_solid_trig
,
58 static int power_supply_create_bat_triggers(struct power_supply
*psy
)
60 psy
->charging_full_trig_name
= kasprintf(GFP_KERNEL
,
61 "%s-charging-or-full", psy
->desc
->name
);
62 if (!psy
->charging_full_trig_name
)
63 goto charging_full_failed
;
65 psy
->charging_trig_name
= kasprintf(GFP_KERNEL
,
66 "%s-charging", psy
->desc
->name
);
67 if (!psy
->charging_trig_name
)
70 psy
->full_trig_name
= kasprintf(GFP_KERNEL
, "%s-full", psy
->desc
->name
);
71 if (!psy
->full_trig_name
)
74 psy
->charging_blink_full_solid_trig_name
= kasprintf(GFP_KERNEL
,
75 "%s-charging-blink-full-solid", psy
->desc
->name
);
76 if (!psy
->charging_blink_full_solid_trig_name
)
77 goto charging_blink_full_solid_failed
;
79 led_trigger_register_simple(psy
->charging_full_trig_name
,
80 &psy
->charging_full_trig
);
81 led_trigger_register_simple(psy
->charging_trig_name
,
83 led_trigger_register_simple(psy
->full_trig_name
,
85 led_trigger_register_simple(psy
->charging_blink_full_solid_trig_name
,
86 &psy
->charging_blink_full_solid_trig
);
90 charging_blink_full_solid_failed
:
91 kfree(psy
->full_trig_name
);
93 kfree(psy
->charging_trig_name
);
95 kfree(psy
->charging_full_trig_name
);
100 static void power_supply_remove_bat_triggers(struct power_supply
*psy
)
102 led_trigger_unregister_simple(psy
->charging_full_trig
);
103 led_trigger_unregister_simple(psy
->charging_trig
);
104 led_trigger_unregister_simple(psy
->full_trig
);
105 led_trigger_unregister_simple(psy
->charging_blink_full_solid_trig
);
106 kfree(psy
->charging_blink_full_solid_trig_name
);
107 kfree(psy
->full_trig_name
);
108 kfree(psy
->charging_trig_name
);
109 kfree(psy
->charging_full_trig_name
);
112 /* Generated power specific LEDs triggers. */
114 static void power_supply_update_gen_leds(struct power_supply
*psy
)
116 union power_supply_propval online
;
118 if (power_supply_get_property(psy
, POWER_SUPPLY_PROP_ONLINE
, &online
))
121 dev_dbg(&psy
->dev
, "%s %d\n", __func__
, online
.intval
);
124 led_trigger_event(psy
->online_trig
, LED_FULL
);
126 led_trigger_event(psy
->online_trig
, LED_OFF
);
129 static int power_supply_create_gen_triggers(struct power_supply
*psy
)
131 psy
->online_trig_name
= kasprintf(GFP_KERNEL
, "%s-online",
133 if (!psy
->online_trig_name
)
136 led_trigger_register_simple(psy
->online_trig_name
, &psy
->online_trig
);
141 static void power_supply_remove_gen_triggers(struct power_supply
*psy
)
143 led_trigger_unregister_simple(psy
->online_trig
);
144 kfree(psy
->online_trig_name
);
147 /* Choice what triggers to create&update. */
149 void power_supply_update_leds(struct power_supply
*psy
)
151 if (psy
->desc
->type
== POWER_SUPPLY_TYPE_BATTERY
)
152 power_supply_update_bat_leds(psy
);
154 power_supply_update_gen_leds(psy
);
157 int power_supply_create_triggers(struct power_supply
*psy
)
159 if (psy
->desc
->type
== POWER_SUPPLY_TYPE_BATTERY
)
160 return power_supply_create_bat_triggers(psy
);
161 return power_supply_create_gen_triggers(psy
);
164 void power_supply_remove_triggers(struct power_supply
*psy
)
166 if (psy
->desc
->type
== POWER_SUPPLY_TYPE_BATTERY
)
167 power_supply_remove_bat_triggers(psy
);
169 power_supply_remove_gen_triggers(psy
);