olpc_battery: Add an 'error' sysfs device that displays raw errors
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / power / power_supply_sysfs.c
CommitLineData
4a11b59d
AV
1/*
2 * Sysfs interface for the universal power supply monitor class
3 *
4 * Copyright © 2007 David Woodhouse <dwmw2@infradead.org>
5 * Copyright © 2007 Anton Vorontsov <cbou@mail.ru>
6 * Copyright © 2004 Szabolcs Gyurko
7 * Copyright © 2003 Ian Molton <spyro@f2s.com>
8 *
9 * Modified: 2004, Oct Szabolcs Gyurko
10 *
11 * You may use this code as per GPL version 2
12 */
13
14#include <linux/ctype.h>
15#include <linux/power_supply.h>
16
25f12141
AB
17#include "power_supply.h"
18
4a11b59d
AV
19/*
20 * This is because the name "current" breaks the device attr macro.
21 * The "current" word resolves to "(get_current())" so instead of
22 * "current" "(get_current())" appears in the sysfs.
23 *
24 * The source of this definition is the device.h which calls __ATTR
25 * macro in sysfs.h which calls the __stringify macro.
26 *
27 * Only modification that the name is not tried to be resolved
28 * (as a macro let's say).
29 */
30
31#define POWER_SUPPLY_ATTR(_name) \
32{ \
01e8ef11 33 .attr = { .name = #_name, .mode = 0444 }, \
4a11b59d
AV
34 .show = power_supply_show_property, \
35 .store = NULL, \
36}
37
38static struct device_attribute power_supply_attrs[];
39
40static ssize_t power_supply_show_property(struct device *dev,
41 struct device_attribute *attr,
42 char *buf) {
43 static char *status_text[] = {
44 "Unknown", "Charging", "Discharging", "Not charging", "Full"
45 };
46 static char *health_text[] = {
47 "Unknown", "Good", "Overheat", "Dead", "Over voltage",
7e386e6e 48 "Unspecified failure", "Cold",
4a11b59d
AV
49 };
50 static char *technology_text[] = {
c7cc930f
DES
51 "Unknown", "NiMH", "Li-ion", "Li-poly", "LiFe", "NiCd",
52 "LiMn"
4a11b59d 53 };
b294a290
AS
54 static char *capacity_level_text[] = {
55 "Unknown", "Critical", "Low", "Normal", "High", "Full"
56 };
4a11b59d
AV
57 ssize_t ret;
58 struct power_supply *psy = dev_get_drvdata(dev);
59 const ptrdiff_t off = attr - power_supply_attrs;
60 union power_supply_propval value;
61
62 ret = psy->get_property(psy, off, &value);
63
64 if (ret < 0) {
65 if (ret != -ENODEV)
66 dev_err(dev, "driver failed to report `%s' property\n",
67 attr->attr.name);
68 return ret;
69 }
70
71 if (off == POWER_SUPPLY_PROP_STATUS)
72 return sprintf(buf, "%s\n", status_text[value.intval]);
73 else if (off == POWER_SUPPLY_PROP_HEALTH)
74 return sprintf(buf, "%s\n", health_text[value.intval]);
75 else if (off == POWER_SUPPLY_PROP_TECHNOLOGY)
76 return sprintf(buf, "%s\n", technology_text[value.intval]);
b294a290
AS
77 else if (off == POWER_SUPPLY_PROP_CAPACITY_LEVEL)
78 return sprintf(buf, "%s\n", capacity_level_text[value.intval]);
4a11b59d
AV
79 else if (off >= POWER_SUPPLY_PROP_MODEL_NAME)
80 return sprintf(buf, "%s\n", value.strval);
81
82 return sprintf(buf, "%d\n", value.intval);
83}
84
85/* Must be in the same order as POWER_SUPPLY_PROP_* */
86static struct device_attribute power_supply_attrs[] = {
87 /* Properties of type `int' */
88 POWER_SUPPLY_ATTR(status),
89 POWER_SUPPLY_ATTR(health),
90 POWER_SUPPLY_ATTR(present),
91 POWER_SUPPLY_ATTR(online),
92 POWER_SUPPLY_ATTR(technology),
c7cc930f
DES
93 POWER_SUPPLY_ATTR(voltage_max),
94 POWER_SUPPLY_ATTR(voltage_min),
4a11b59d
AV
95 POWER_SUPPLY_ATTR(voltage_max_design),
96 POWER_SUPPLY_ATTR(voltage_min_design),
97 POWER_SUPPLY_ATTR(voltage_now),
98 POWER_SUPPLY_ATTR(voltage_avg),
99 POWER_SUPPLY_ATTR(current_now),
100 POWER_SUPPLY_ATTR(current_avg),
7faa144a
AS
101 POWER_SUPPLY_ATTR(power_now),
102 POWER_SUPPLY_ATTR(power_avg),
4a11b59d
AV
103 POWER_SUPPLY_ATTR(charge_full_design),
104 POWER_SUPPLY_ATTR(charge_empty_design),
105 POWER_SUPPLY_ATTR(charge_full),
106 POWER_SUPPLY_ATTR(charge_empty),
107 POWER_SUPPLY_ATTR(charge_now),
108 POWER_SUPPLY_ATTR(charge_avg),
8e552c36 109 POWER_SUPPLY_ATTR(charge_counter),
4a11b59d
AV
110 POWER_SUPPLY_ATTR(energy_full_design),
111 POWER_SUPPLY_ATTR(energy_empty_design),
112 POWER_SUPPLY_ATTR(energy_full),
113 POWER_SUPPLY_ATTR(energy_empty),
114 POWER_SUPPLY_ATTR(energy_now),
115 POWER_SUPPLY_ATTR(energy_avg),
116 POWER_SUPPLY_ATTR(capacity),
b294a290 117 POWER_SUPPLY_ATTR(capacity_level),
4a11b59d
AV
118 POWER_SUPPLY_ATTR(temp),
119 POWER_SUPPLY_ATTR(temp_ambient),
120 POWER_SUPPLY_ATTR(time_to_empty_now),
121 POWER_SUPPLY_ATTR(time_to_empty_avg),
122 POWER_SUPPLY_ATTR(time_to_full_now),
123 POWER_SUPPLY_ATTR(time_to_full_avg),
124 /* Properties of type `const char *' */
125 POWER_SUPPLY_ATTR(model_name),
126 POWER_SUPPLY_ATTR(manufacturer),
7c2670bb 127 POWER_SUPPLY_ATTR(serial_number),
4a11b59d
AV
128};
129
130static ssize_t power_supply_show_static_attrs(struct device *dev,
131 struct device_attribute *attr,
132 char *buf) {
133 static char *type_text[] = { "Battery", "UPS", "Mains", "USB" };
134 struct power_supply *psy = dev_get_drvdata(dev);
135
136 return sprintf(buf, "%s\n", type_text[psy->type]);
137}
138
139static struct device_attribute power_supply_static_attrs[] = {
140 __ATTR(type, 0444, power_supply_show_static_attrs, NULL),
141};
142
143int power_supply_create_attrs(struct power_supply *psy)
144{
145 int rc = 0;
146 int i, j;
147
148 for (i = 0; i < ARRAY_SIZE(power_supply_static_attrs); i++) {
149 rc = device_create_file(psy->dev,
150 &power_supply_static_attrs[i]);
151 if (rc)
152 goto statics_failed;
153 }
154
155 for (j = 0; j < psy->num_properties; j++) {
156 rc = device_create_file(psy->dev,
157 &power_supply_attrs[psy->properties[j]]);
158 if (rc)
159 goto dynamics_failed;
160 }
161
162 goto succeed;
163
164dynamics_failed:
165 while (j--)
166 device_remove_file(psy->dev,
167 &power_supply_attrs[psy->properties[j]]);
168statics_failed:
169 while (i--)
839dc9f1 170 device_remove_file(psy->dev, &power_supply_static_attrs[i]);
4a11b59d
AV
171succeed:
172 return rc;
173}
174
175void power_supply_remove_attrs(struct power_supply *psy)
176{
177 int i;
178
179 for (i = 0; i < ARRAY_SIZE(power_supply_static_attrs); i++)
839dc9f1 180 device_remove_file(psy->dev, &power_supply_static_attrs[i]);
4a11b59d
AV
181
182 for (i = 0; i < psy->num_properties; i++)
183 device_remove_file(psy->dev,
184 &power_supply_attrs[psy->properties[i]]);
4a11b59d
AV
185}
186
187static char *kstruprdup(const char *str, gfp_t gfp)
188{
189 char *ret, *ustr;
190
191 ustr = ret = kmalloc(strlen(str) + 1, gfp);
192
193 if (!ret)
194 return NULL;
195
196 while (*str)
197 *ustr++ = toupper(*str++);
198
199 *ustr = 0;
200
201 return ret;
202}
203
7eff2e7a 204int power_supply_uevent(struct device *dev, struct kobj_uevent_env *env)
4a11b59d
AV
205{
206 struct power_supply *psy = dev_get_drvdata(dev);
7eff2e7a 207 int ret = 0, j;
4a11b59d
AV
208 char *prop_buf;
209 char *attrname;
210
211 dev_dbg(dev, "uevent\n");
212
56fa18e8 213 if (!psy || !psy->dev) {
4a11b59d
AV
214 dev_dbg(dev, "No power supply yet\n");
215 return ret;
216 }
217
218 dev_dbg(dev, "POWER_SUPPLY_NAME=%s\n", psy->name);
219
7eff2e7a 220 ret = add_uevent_var(env, "POWER_SUPPLY_NAME=%s", psy->name);
4a11b59d
AV
221 if (ret)
222 return ret;
223
224 prop_buf = (char *)get_zeroed_page(GFP_KERNEL);
225 if (!prop_buf)
226 return -ENOMEM;
227
228 for (j = 0; j < ARRAY_SIZE(power_supply_static_attrs); j++) {
229 struct device_attribute *attr;
230 char *line;
231
232 attr = &power_supply_static_attrs[j];
233
234 ret = power_supply_show_static_attrs(dev, attr, prop_buf);
235 if (ret < 0)
236 goto out;
237
238 line = strchr(prop_buf, '\n');
239 if (line)
240 *line = 0;
241
242 attrname = kstruprdup(attr->attr.name, GFP_KERNEL);
243 if (!attrname) {
244 ret = -ENOMEM;
245 goto out;
246 }
247
248 dev_dbg(dev, "Static prop %s=%s\n", attrname, prop_buf);
249
7eff2e7a 250 ret = add_uevent_var(env, "POWER_SUPPLY_%s=%s", attrname, prop_buf);
4a11b59d
AV
251 kfree(attrname);
252 if (ret)
253 goto out;
254 }
255
256 dev_dbg(dev, "%zd dynamic props\n", psy->num_properties);
257
258 for (j = 0; j < psy->num_properties; j++) {
259 struct device_attribute *attr;
260 char *line;
261
262 attr = &power_supply_attrs[psy->properties[j]];
263
264 ret = power_supply_show_property(dev, attr, prop_buf);
265 if (ret == -ENODEV) {
266 /* When a battery is absent, we expect -ENODEV. Don't abort;
267 send the uevent with at least the the PRESENT=0 property */
268 ret = 0;
269 continue;
270 }
271
272 if (ret < 0)
273 goto out;
274
275 line = strchr(prop_buf, '\n');
276 if (line)
277 *line = 0;
278
279 attrname = kstruprdup(attr->attr.name, GFP_KERNEL);
280 if (!attrname) {
281 ret = -ENOMEM;
282 goto out;
283 }
284
285 dev_dbg(dev, "prop %s=%s\n", attrname, prop_buf);
286
7eff2e7a 287 ret = add_uevent_var(env, "POWER_SUPPLY_%s=%s", attrname, prop_buf);
4a11b59d
AV
288 kfree(attrname);
289 if (ret)
290 goto out;
291 }
292
293out:
294 free_page((unsigned long)prop_buf);
295
296 return ret;
297}