Merge git://git.kernel.org/pub/scm/linux/kernel/git/rusty/linux-2.6-for-linus
[wrt350n-kernel.git] / drivers / power / power_supply_leds.c
blobfa3034f85c382d4a3dc3b5bdb6ed36469c46042a
1 /*
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))
25 return;
27 dev_dbg(psy->dev, "%s %d\n", __FUNCTION__, 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);
34 break;
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);
39 break;
40 default:
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);
44 break;
48 static int power_supply_create_bat_triggers(struct power_supply *psy)
50 int rc = 0;
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)
60 goto charging_failed;
62 psy->full_trig_name = kasprintf(GFP_KERNEL, "%s-full", psy->name);
63 if (!psy->full_trig_name)
64 goto full_failed;
66 led_trigger_register_simple(psy->charging_full_trig_name,
67 &psy->charging_full_trig);
68 led_trigger_register_simple(psy->charging_trig_name,
69 &psy->charging_trig);
70 led_trigger_register_simple(psy->full_trig_name,
71 &psy->full_trig);
73 goto success;
75 full_failed:
76 kfree(psy->charging_trig_name);
77 charging_failed:
78 kfree(psy->charging_full_trig_name);
79 charging_full_failed:
80 rc = -ENOMEM;
81 success:
82 return rc;
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))
102 return;
104 dev_dbg(psy->dev, "%s %d\n", __FUNCTION__, online.intval);
106 if (online.intval)
107 led_trigger_event(psy->online_trig, LED_FULL);
108 else
109 led_trigger_event(psy->online_trig, LED_OFF);
112 static int power_supply_create_gen_triggers(struct power_supply *psy)
114 int rc = 0;
116 psy->online_trig_name = kasprintf(GFP_KERNEL, "%s-online", psy->name);
117 if (!psy->online_trig_name)
118 goto online_failed;
120 led_trigger_register_simple(psy->online_trig_name, &psy->online_trig);
122 goto success;
124 online_failed:
125 rc = -ENOMEM;
126 success:
127 return rc;
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);
142 else
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);
157 else
158 power_supply_remove_gen_triggers(psy);