Merge branch 'merge' of git://git.kernel.org/pub/scm/linux/kernel/git/benh/powerpc
[linux-2.6] / drivers / power / power_supply_leds.c
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/kernel.h>
14 #include <linux/power_supply.h>
15
16 #include "power_supply.h"
17
18 /* Battery specific LEDs triggers. */
19
20 static void power_supply_update_bat_leds(struct power_supply *psy)
21 {
22         union power_supply_propval status;
23
24         if (psy->get_property(psy, POWER_SUPPLY_PROP_STATUS, &status))
25                 return;
26
27         dev_dbg(psy->dev, "%s %d\n", __func__, status.intval);
28
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;
45         }
46 }
47
48 static int power_supply_create_bat_triggers(struct power_supply *psy)
49 {
50         int rc = 0;
51
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;
56
57         psy->charging_trig_name = kasprintf(GFP_KERNEL,
58                                         "%s-charging", psy->name);
59         if (!psy->charging_trig_name)
60                 goto charging_failed;
61
62         psy->full_trig_name = kasprintf(GFP_KERNEL, "%s-full", psy->name);
63         if (!psy->full_trig_name)
64                 goto full_failed;
65
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);
72
73         goto success;
74
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;
83 }
84
85 static void power_supply_remove_bat_triggers(struct power_supply *psy)
86 {
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);
93 }
94
95 /* Generated power specific LEDs triggers. */
96
97 static void power_supply_update_gen_leds(struct power_supply *psy)
98 {
99         union power_supply_propval online;
100
101         if (psy->get_property(psy, POWER_SUPPLY_PROP_ONLINE, &online))
102                 return;
103
104         dev_dbg(psy->dev, "%s %d\n", __func__, online.intval);
105
106         if (online.intval)
107                 led_trigger_event(psy->online_trig, LED_FULL);
108         else
109                 led_trigger_event(psy->online_trig, LED_OFF);
110 }
111
112 static int power_supply_create_gen_triggers(struct power_supply *psy)
113 {
114         int rc = 0;
115
116         psy->online_trig_name = kasprintf(GFP_KERNEL, "%s-online", psy->name);
117         if (!psy->online_trig_name)
118                 goto online_failed;
119
120         led_trigger_register_simple(psy->online_trig_name, &psy->online_trig);
121
122         goto success;
123
124 online_failed:
125         rc = -ENOMEM;
126 success:
127         return rc;
128 }
129
130 static void power_supply_remove_gen_triggers(struct power_supply *psy)
131 {
132         led_trigger_unregister_simple(psy->online_trig);
133         kfree(psy->online_trig_name);
134 }
135
136 /* Choice what triggers to create&update. */
137
138 void power_supply_update_leds(struct power_supply *psy)
139 {
140         if (psy->type == POWER_SUPPLY_TYPE_BATTERY)
141                 power_supply_update_bat_leds(psy);
142         else
143                 power_supply_update_gen_leds(psy);
144 }
145
146 int power_supply_create_triggers(struct power_supply *psy)
147 {
148         if (psy->type == POWER_SUPPLY_TYPE_BATTERY)
149                 return power_supply_create_bat_triggers(psy);
150         return power_supply_create_gen_triggers(psy);
151 }
152
153 void power_supply_remove_triggers(struct power_supply *psy)
154 {
155         if (psy->type == POWER_SUPPLY_TYPE_BATTERY)
156                 power_supply_remove_bat_triggers(psy);
157         else
158                 power_supply_remove_gen_triggers(psy);
159 }