diff options
author | Anton Vorontsov <cbou@mail.ru> | 2007-05-03 16:27:45 -0400 |
---|---|---|
committer | David Woodhouse <dwmw2@infradead.org> | 2007-07-10 06:25:44 -0400 |
commit | 4a11b59d8283662193a9c6a9c14c58d1b9bf0617 (patch) | |
tree | 492bf022c806ec6c554ca5f8e7c05ea6db138137 /drivers/power/power_supply_leds.c | |
parent | 7dcca30a32aadb0520417521b0c44f42d09fe05c (diff) |
[BATTERY] Universal power supply class (was: battery class)
This class is result of "external power" and "battery" classes merge,
as suggested by David Woodhouse. He also implemented uevent support.
Here how userspace seeing it now:
# ls /sys/class/power\ supply/
ac main-battery usb
# cat /sys/class/power\ supply/ac/type
AC
# cat /sys/class/power\ supply/usb/type
USB
# cat /sys/class/power\ supply/main-battery/type
Battery
# cat /sys/class/power\ supply/ac/online
1
# cat /sys/class/power\ supply/usb/online
0
# cat /sys/class/power\ supply/main-battery/status
Charging
# cat /sys/class/leds/h5400\:red-left/trigger
none h5400-radio timer hwtimer ac-online usb-online
main-battery-charging-or-full [main-battery-charging]
main-battery-full
Signed-off-by: Anton Vorontsov <cbou@mail.ru>
Signed-off-by: David Woodhouse <dwmw2@infradead.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Diffstat (limited to 'drivers/power/power_supply_leds.c')
-rw-r--r-- | drivers/power/power_supply_leds.c | 176 |
1 files changed, 176 insertions, 0 deletions
diff --git a/drivers/power/power_supply_leds.c b/drivers/power/power_supply_leds.c new file mode 100644 index 000000000000..7232490bb595 --- /dev/null +++ b/drivers/power/power_supply_leds.c | |||
@@ -0,0 +1,176 @@ | |||
1 | /* | ||
2 | * LEDs triggers for power supply class | ||
3 | * | ||
4 | * Copyright © 2007 Anton Vorontsov <cbou@mail.ru> | ||
5 | * Copyright © 2004 Szabolcs Gyurko | ||
6 | * Copyright © 2003 Ian Molton <spyro@f2s.com> | ||
7 | * | ||
8 | * Modified: 2004, Oct Szabolcs Gyurko | ||
9 | * | ||
10 | * You may use this code as per GPL version 2 | ||
11 | */ | ||
12 | |||
13 | #include <linux/power_supply.h> | ||
14 | |||
15 | /* Battery specific LEDs triggers. */ | ||
16 | |||
17 | static void power_supply_update_bat_leds(struct power_supply *psy) | ||
18 | { | ||
19 | union power_supply_propval status; | ||
20 | |||
21 | if (psy->get_property(psy, POWER_SUPPLY_PROP_STATUS, &status)) | ||
22 | return; | ||
23 | |||
24 | dev_dbg(psy->dev, "%s %d\n", __FUNCTION__, status.intval); | ||
25 | |||
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); | ||
31 | break; | ||
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); | ||
36 | break; | ||
37 | default: | ||
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); | ||
41 | break; | ||
42 | } | ||
43 | |||
44 | return; | ||
45 | } | ||
46 | |||
47 | static int power_supply_create_bat_triggers(struct power_supply *psy) | ||
48 | { | ||
49 | int rc = 0; | ||
50 | |||
51 | psy->charging_full_trig_name = kmalloc(strlen(psy->name) + | ||
52 | sizeof("-charging-or-full"), GFP_KERNEL); | ||
53 | if (!psy->charging_full_trig_name) | ||
54 | goto charging_full_failed; | ||
55 | |||
56 | psy->charging_trig_name = kmalloc(strlen(psy->name) + | ||
57 | sizeof("-charging"), GFP_KERNEL); | ||
58 | if (!psy->charging_trig_name) | ||
59 | goto charging_failed; | ||
60 | |||
61 | psy->full_trig_name = kmalloc(strlen(psy->name) + | ||
62 | sizeof("-full"), GFP_KERNEL); | ||
63 | if (!psy->full_trig_name) | ||
64 | goto full_failed; | ||
65 | |||
66 | strcpy(psy->charging_full_trig_name, psy->name); | ||
67 | strcat(psy->charging_full_trig_name, "-charging-or-full"); | ||
68 | strcpy(psy->charging_trig_name, psy->name); | ||
69 | strcat(psy->charging_trig_name, "-charging"); | ||
70 | strcpy(psy->full_trig_name, psy->name); | ||
71 | strcat(psy->full_trig_name, "-full"); | ||
72 | |||
73 | led_trigger_register_simple(psy->charging_full_trig_name, | ||
74 | &psy->charging_full_trig); | ||
75 | led_trigger_register_simple(psy->charging_trig_name, | ||
76 | &psy->charging_trig); | ||
77 | led_trigger_register_simple(psy->full_trig_name, | ||
78 | &psy->full_trig); | ||
79 | |||
80 | goto success; | ||
81 | |||
82 | full_failed: | ||
83 | kfree(psy->charging_trig_name); | ||
84 | charging_failed: | ||
85 | kfree(psy->charging_full_trig_name); | ||
86 | charging_full_failed: | ||
87 | rc = -ENOMEM; | ||
88 | success: | ||
89 | return rc; | ||
90 | } | ||
91 | |||
92 | static void power_supply_remove_bat_triggers(struct power_supply *psy) | ||
93 | { | ||
94 | led_trigger_unregister_simple(psy->charging_full_trig); | ||
95 | led_trigger_unregister_simple(psy->charging_trig); | ||
96 | led_trigger_unregister_simple(psy->full_trig); | ||
97 | kfree(psy->full_trig_name); | ||
98 | kfree(psy->charging_trig_name); | ||
99 | kfree(psy->charging_full_trig_name); | ||
100 | return; | ||
101 | } | ||
102 | |||
103 | /* Generated power specific LEDs triggers. */ | ||
104 | |||
105 | static void power_supply_update_gen_leds(struct power_supply *psy) | ||
106 | { | ||
107 | union power_supply_propval online; | ||
108 | |||
109 | if (psy->get_property(psy, POWER_SUPPLY_PROP_ONLINE, &online)) | ||
110 | return; | ||
111 | |||
112 | dev_dbg(psy->dev, "%s %d\n", __FUNCTION__, online.intval); | ||
113 | |||
114 | if (online.intval) | ||
115 | led_trigger_event(psy->online_trig, LED_FULL); | ||
116 | else | ||
117 | led_trigger_event(psy->online_trig, LED_OFF); | ||
118 | |||
119 | return; | ||
120 | } | ||
121 | |||
122 | static int power_supply_create_gen_triggers(struct power_supply *psy) | ||
123 | { | ||
124 | int rc = 0; | ||
125 | |||
126 | psy->online_trig_name = kmalloc(strlen(psy->name) + sizeof("-online"), | ||
127 | GFP_KERNEL); | ||
128 | if (!psy->online_trig_name) | ||
129 | goto online_failed; | ||
130 | |||
131 | strcpy(psy->online_trig_name, psy->name); | ||
132 | strcat(psy->online_trig_name, "-online"); | ||
133 | |||
134 | led_trigger_register_simple(psy->online_trig_name, &psy->online_trig); | ||
135 | |||
136 | goto success; | ||
137 | |||
138 | online_failed: | ||
139 | rc = -ENOMEM; | ||
140 | success: | ||
141 | return rc; | ||
142 | } | ||
143 | |||
144 | static void power_supply_remove_gen_triggers(struct power_supply *psy) | ||
145 | { | ||
146 | led_trigger_unregister_simple(psy->online_trig); | ||
147 | kfree(psy->online_trig_name); | ||
148 | return; | ||
149 | } | ||
150 | |||
151 | /* Choice what triggers to create&update. */ | ||
152 | |||
153 | void power_supply_update_leds(struct power_supply *psy) | ||
154 | { | ||
155 | if (psy->type == POWER_SUPPLY_TYPE_BATTERY) | ||
156 | power_supply_update_bat_leds(psy); | ||
157 | else | ||
158 | power_supply_update_gen_leds(psy); | ||
159 | return; | ||
160 | } | ||
161 | |||
162 | int power_supply_create_triggers(struct power_supply *psy) | ||
163 | { | ||
164 | if (psy->type == POWER_SUPPLY_TYPE_BATTERY) | ||
165 | return power_supply_create_bat_triggers(psy); | ||
166 | return power_supply_create_gen_triggers(psy); | ||
167 | } | ||
168 | |||
169 | void power_supply_remove_triggers(struct power_supply *psy) | ||
170 | { | ||
171 | if (psy->type == POWER_SUPPLY_TYPE_BATTERY) | ||
172 | power_supply_remove_bat_triggers(psy); | ||
173 | else | ||
174 | power_supply_remove_gen_triggers(psy); | ||
175 | return; | ||
176 | } | ||