Commit | Line | Data |
---|---|---|
fb972873 DW |
1 | /* |
2 | * Battery driver for One Laptop Per Child board. | |
3 | * | |
690e85a3 | 4 | * Copyright © 2006-2010 David Woodhouse <dwmw2@infradead.org> |
fb972873 DW |
5 | * |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License version 2 as | |
8 | * published by the Free Software Foundation. | |
9 | */ | |
10 | ||
04a820ea | 11 | #include <linux/kernel.h> |
fb972873 | 12 | #include <linux/module.h> |
144bbeae | 13 | #include <linux/types.h> |
fb972873 | 14 | #include <linux/err.h> |
144bbeae | 15 | #include <linux/device.h> |
fb972873 DW |
16 | #include <linux/platform_device.h> |
17 | #include <linux/power_supply.h> | |
18 | #include <linux/jiffies.h> | |
19 | #include <linux/sched.h> | |
20 | #include <asm/olpc.h> | |
21 | ||
22 | ||
23 | #define EC_BAT_VOLTAGE 0x10 /* uint16_t, *9.76/32, mV */ | |
24 | #define EC_BAT_CURRENT 0x11 /* int16_t, *15.625/120, mA */ | |
75d88079 | 25 | #define EC_BAT_ACR 0x12 /* int16_t, *6250/15, µAh */ |
fb972873 DW |
26 | #define EC_BAT_TEMP 0x13 /* uint16_t, *100/256, °C */ |
27 | #define EC_AMB_TEMP 0x14 /* uint16_t, *100/256, °C */ | |
28 | #define EC_BAT_STATUS 0x15 /* uint8_t, bitmask */ | |
29 | #define EC_BAT_SOC 0x16 /* uint8_t, percentage */ | |
30 | #define EC_BAT_SERIAL 0x17 /* uint8_t[6] */ | |
31 | #define EC_BAT_EEPROM 0x18 /* uint8_t adr as input, uint8_t output */ | |
32 | #define EC_BAT_ERRCODE 0x1f /* uint8_t, bitmask */ | |
33 | ||
34 | #define BAT_STAT_PRESENT 0x01 | |
35 | #define BAT_STAT_FULL 0x02 | |
36 | #define BAT_STAT_LOW 0x04 | |
37 | #define BAT_STAT_DESTROY 0x08 | |
38 | #define BAT_STAT_AC 0x10 | |
39 | #define BAT_STAT_CHARGING 0x20 | |
40 | #define BAT_STAT_DISCHARGING 0x40 | |
8f7e5798 | 41 | #define BAT_STAT_TRICKLE 0x80 |
fb972873 DW |
42 | |
43 | #define BAT_ERR_INFOFAIL 0x02 | |
44 | #define BAT_ERR_OVERVOLTAGE 0x04 | |
45 | #define BAT_ERR_OVERTEMP 0x05 | |
46 | #define BAT_ERR_GAUGESTOP 0x06 | |
47 | #define BAT_ERR_OUT_OF_CONTROL 0x07 | |
48 | #define BAT_ERR_ID_FAIL 0x09 | |
49 | #define BAT_ERR_ACR_FAIL 0x10 | |
50 | ||
51 | #define BAT_ADDR_MFR_TYPE 0x5F | |
52 | ||
53 | /********************************************************************* | |
54 | * Power | |
55 | *********************************************************************/ | |
56 | ||
57 | static int olpc_ac_get_prop(struct power_supply *psy, | |
58 | enum power_supply_property psp, | |
59 | union power_supply_propval *val) | |
60 | { | |
61 | int ret = 0; | |
62 | uint8_t status; | |
63 | ||
64 | switch (psp) { | |
65 | case POWER_SUPPLY_PROP_ONLINE: | |
66 | ret = olpc_ec_cmd(EC_BAT_STATUS, NULL, 0, &status, 1); | |
67 | if (ret) | |
68 | return ret; | |
69 | ||
70 | val->intval = !!(status & BAT_STAT_AC); | |
71 | break; | |
72 | default: | |
73 | ret = -EINVAL; | |
74 | break; | |
75 | } | |
76 | return ret; | |
77 | } | |
78 | ||
79 | static enum power_supply_property olpc_ac_props[] = { | |
80 | POWER_SUPPLY_PROP_ONLINE, | |
81 | }; | |
82 | ||
83 | static struct power_supply olpc_ac = { | |
84 | .name = "olpc-ac", | |
85 | .type = POWER_SUPPLY_TYPE_MAINS, | |
86 | .properties = olpc_ac_props, | |
87 | .num_properties = ARRAY_SIZE(olpc_ac_props), | |
88 | .get_property = olpc_ac_get_prop, | |
89 | }; | |
90 | ||
1ca5b9d2 DW |
91 | static char bat_serial[17]; /* Ick */ |
92 | ||
b2bd8a3b AS |
93 | static int olpc_bat_get_status(union power_supply_propval *val, uint8_t ec_byte) |
94 | { | |
95 | if (olpc_platform_info.ecver > 0x44) { | |
8f7e5798 | 96 | if (ec_byte & (BAT_STAT_CHARGING | BAT_STAT_TRICKLE)) |
b2bd8a3b AS |
97 | val->intval = POWER_SUPPLY_STATUS_CHARGING; |
98 | else if (ec_byte & BAT_STAT_DISCHARGING) | |
99 | val->intval = POWER_SUPPLY_STATUS_DISCHARGING; | |
100 | else if (ec_byte & BAT_STAT_FULL) | |
101 | val->intval = POWER_SUPPLY_STATUS_FULL; | |
102 | else /* er,... */ | |
103 | val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING; | |
104 | } else { | |
105 | /* Older EC didn't report charge/discharge bits */ | |
106 | if (!(ec_byte & BAT_STAT_AC)) /* No AC means discharging */ | |
107 | val->intval = POWER_SUPPLY_STATUS_DISCHARGING; | |
108 | else if (ec_byte & BAT_STAT_FULL) | |
109 | val->intval = POWER_SUPPLY_STATUS_FULL; | |
110 | else /* Not _necessarily_ true but EC doesn't tell all yet */ | |
111 | val->intval = POWER_SUPPLY_STATUS_CHARGING; | |
112 | } | |
113 | ||
114 | return 0; | |
115 | } | |
116 | ||
117 | static int olpc_bat_get_health(union power_supply_propval *val) | |
118 | { | |
119 | uint8_t ec_byte; | |
120 | int ret; | |
121 | ||
122 | ret = olpc_ec_cmd(EC_BAT_ERRCODE, NULL, 0, &ec_byte, 1); | |
123 | if (ret) | |
124 | return ret; | |
125 | ||
126 | switch (ec_byte) { | |
127 | case 0: | |
128 | val->intval = POWER_SUPPLY_HEALTH_GOOD; | |
129 | break; | |
130 | ||
131 | case BAT_ERR_OVERTEMP: | |
132 | val->intval = POWER_SUPPLY_HEALTH_OVERHEAT; | |
133 | break; | |
134 | ||
135 | case BAT_ERR_OVERVOLTAGE: | |
136 | val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE; | |
137 | break; | |
138 | ||
139 | case BAT_ERR_INFOFAIL: | |
140 | case BAT_ERR_OUT_OF_CONTROL: | |
141 | case BAT_ERR_ID_FAIL: | |
142 | case BAT_ERR_ACR_FAIL: | |
143 | val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE; | |
144 | break; | |
145 | ||
146 | default: | |
147 | /* Eep. We don't know this failure code */ | |
148 | ret = -EIO; | |
149 | } | |
150 | ||
151 | return ret; | |
152 | } | |
153 | ||
154 | static int olpc_bat_get_mfr(union power_supply_propval *val) | |
155 | { | |
156 | uint8_t ec_byte; | |
157 | int ret; | |
158 | ||
159 | ec_byte = BAT_ADDR_MFR_TYPE; | |
160 | ret = olpc_ec_cmd(EC_BAT_EEPROM, &ec_byte, 1, &ec_byte, 1); | |
161 | if (ret) | |
162 | return ret; | |
163 | ||
164 | switch (ec_byte >> 4) { | |
165 | case 1: | |
166 | val->strval = "Gold Peak"; | |
167 | break; | |
168 | case 2: | |
169 | val->strval = "BYD"; | |
170 | break; | |
171 | default: | |
172 | val->strval = "Unknown"; | |
173 | break; | |
174 | } | |
175 | ||
176 | return ret; | |
177 | } | |
178 | ||
179 | static int olpc_bat_get_tech(union power_supply_propval *val) | |
180 | { | |
181 | uint8_t ec_byte; | |
182 | int ret; | |
183 | ||
184 | ec_byte = BAT_ADDR_MFR_TYPE; | |
185 | ret = olpc_ec_cmd(EC_BAT_EEPROM, &ec_byte, 1, &ec_byte, 1); | |
186 | if (ret) | |
187 | return ret; | |
188 | ||
189 | switch (ec_byte & 0xf) { | |
190 | case 1: | |
191 | val->intval = POWER_SUPPLY_TECHNOLOGY_NiMH; | |
192 | break; | |
193 | case 2: | |
194 | val->intval = POWER_SUPPLY_TECHNOLOGY_LiFe; | |
195 | break; | |
196 | default: | |
197 | val->intval = POWER_SUPPLY_TECHNOLOGY_UNKNOWN; | |
198 | break; | |
199 | } | |
200 | ||
201 | return ret; | |
202 | } | |
203 | ||
b202a5e6 SS |
204 | static int olpc_bat_get_charge_full_design(union power_supply_propval *val) |
205 | { | |
206 | uint8_t ec_byte; | |
207 | union power_supply_propval tech; | |
208 | int ret, mfr; | |
209 | ||
210 | ret = olpc_bat_get_tech(&tech); | |
211 | if (ret) | |
212 | return ret; | |
213 | ||
214 | ec_byte = BAT_ADDR_MFR_TYPE; | |
215 | ret = olpc_ec_cmd(EC_BAT_EEPROM, &ec_byte, 1, &ec_byte, 1); | |
216 | if (ret) | |
217 | return ret; | |
218 | ||
219 | mfr = ec_byte >> 4; | |
220 | ||
221 | switch (tech.intval) { | |
222 | case POWER_SUPPLY_TECHNOLOGY_NiMH: | |
223 | switch (mfr) { | |
224 | case 1: /* Gold Peak */ | |
225 | val->intval = 3000000*.8; | |
226 | break; | |
227 | default: | |
228 | return -EIO; | |
229 | } | |
230 | break; | |
231 | ||
232 | case POWER_SUPPLY_TECHNOLOGY_LiFe: | |
233 | switch (mfr) { | |
ecc2edd5 | 234 | case 1: /* Gold Peak, fall through */ |
b202a5e6 | 235 | case 2: /* BYD */ |
ecc2edd5 | 236 | val->intval = 2800000; |
b202a5e6 SS |
237 | break; |
238 | default: | |
239 | return -EIO; | |
240 | } | |
241 | break; | |
242 | ||
243 | default: | |
244 | return -EIO; | |
245 | } | |
246 | ||
247 | return ret; | |
248 | } | |
249 | ||
20fd9830 SS |
250 | static int olpc_bat_get_charge_now(union power_supply_propval *val) |
251 | { | |
252 | uint8_t soc; | |
253 | union power_supply_propval full; | |
254 | int ret; | |
255 | ||
256 | ret = olpc_ec_cmd(EC_BAT_SOC, NULL, 0, &soc, 1); | |
257 | if (ret) | |
258 | return ret; | |
259 | ||
260 | ret = olpc_bat_get_charge_full_design(&full); | |
261 | if (ret) | |
262 | return ret; | |
263 | ||
264 | val->intval = soc * (full.intval / 100); | |
265 | return 0; | |
266 | } | |
267 | ||
5619d0ba RS |
268 | static int olpc_bat_get_voltage_max_design(union power_supply_propval *val) |
269 | { | |
270 | uint8_t ec_byte; | |
271 | union power_supply_propval tech; | |
272 | int mfr; | |
273 | int ret; | |
274 | ||
275 | ret = olpc_bat_get_tech(&tech); | |
276 | if (ret) | |
277 | return ret; | |
278 | ||
279 | ec_byte = BAT_ADDR_MFR_TYPE; | |
280 | ret = olpc_ec_cmd(EC_BAT_EEPROM, &ec_byte, 1, &ec_byte, 1); | |
281 | if (ret) | |
282 | return ret; | |
283 | ||
284 | mfr = ec_byte >> 4; | |
285 | ||
286 | switch (tech.intval) { | |
287 | case POWER_SUPPLY_TECHNOLOGY_NiMH: | |
288 | switch (mfr) { | |
289 | case 1: /* Gold Peak */ | |
290 | val->intval = 6000000; | |
291 | break; | |
292 | default: | |
293 | return -EIO; | |
294 | } | |
295 | break; | |
296 | ||
297 | case POWER_SUPPLY_TECHNOLOGY_LiFe: | |
298 | switch (mfr) { | |
299 | case 1: /* Gold Peak */ | |
300 | val->intval = 6400000; | |
301 | break; | |
302 | case 2: /* BYD */ | |
303 | val->intval = 6500000; | |
304 | break; | |
305 | default: | |
306 | return -EIO; | |
307 | } | |
308 | break; | |
309 | ||
310 | default: | |
311 | return -EIO; | |
312 | } | |
313 | ||
314 | return ret; | |
315 | } | |
316 | ||
fb972873 DW |
317 | /********************************************************************* |
318 | * Battery properties | |
319 | *********************************************************************/ | |
320 | static int olpc_bat_get_property(struct power_supply *psy, | |
321 | enum power_supply_property psp, | |
322 | union power_supply_propval *val) | |
323 | { | |
324 | int ret = 0; | |
8e9c7716 | 325 | __be16 ec_word; |
fb972873 | 326 | uint8_t ec_byte; |
8e9c7716 | 327 | __be64 ser_buf; |
fb972873 DW |
328 | |
329 | ret = olpc_ec_cmd(EC_BAT_STATUS, NULL, 0, &ec_byte, 1); | |
330 | if (ret) | |
331 | return ret; | |
332 | ||
333 | /* Theoretically there's a race here -- the battery could be | |
334 | removed immediately after we check whether it's present, and | |
335 | then we query for some other property of the now-absent battery. | |
336 | It doesn't matter though -- the EC will return the last-known | |
337 | information, and it's as if we just ran that _little_ bit faster | |
338 | and managed to read it out before the battery went away. */ | |
8f7e5798 AS |
339 | if (!(ec_byte & (BAT_STAT_PRESENT | BAT_STAT_TRICKLE)) && |
340 | psp != POWER_SUPPLY_PROP_PRESENT) | |
fb972873 DW |
341 | return -ENODEV; |
342 | ||
343 | switch (psp) { | |
344 | case POWER_SUPPLY_PROP_STATUS: | |
b2bd8a3b AS |
345 | ret = olpc_bat_get_status(val, ec_byte); |
346 | if (ret) | |
347 | return ret; | |
348 | break; | |
ee8076ed AS |
349 | case POWER_SUPPLY_PROP_CHARGE_TYPE: |
350 | if (ec_byte & BAT_STAT_TRICKLE) | |
351 | val->intval = POWER_SUPPLY_CHARGE_TYPE_TRICKLE; | |
352 | else if (ec_byte & BAT_STAT_CHARGING) | |
353 | val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST; | |
354 | else | |
355 | val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE; | |
356 | break; | |
fb972873 | 357 | case POWER_SUPPLY_PROP_PRESENT: |
8f7e5798 AS |
358 | val->intval = !!(ec_byte & (BAT_STAT_PRESENT | |
359 | BAT_STAT_TRICKLE)); | |
fb972873 DW |
360 | break; |
361 | ||
362 | case POWER_SUPPLY_PROP_HEALTH: | |
363 | if (ec_byte & BAT_STAT_DESTROY) | |
364 | val->intval = POWER_SUPPLY_HEALTH_DEAD; | |
365 | else { | |
b2bd8a3b | 366 | ret = olpc_bat_get_health(val); |
fb972873 DW |
367 | if (ret) |
368 | return ret; | |
fb972873 DW |
369 | } |
370 | break; | |
371 | ||
372 | case POWER_SUPPLY_PROP_MANUFACTURER: | |
b2bd8a3b | 373 | ret = olpc_bat_get_mfr(val); |
fb972873 DW |
374 | if (ret) |
375 | return ret; | |
fb972873 DW |
376 | break; |
377 | case POWER_SUPPLY_PROP_TECHNOLOGY: | |
b2bd8a3b | 378 | ret = olpc_bat_get_tech(val); |
fb972873 DW |
379 | if (ret) |
380 | return ret; | |
fb972873 DW |
381 | break; |
382 | case POWER_SUPPLY_PROP_VOLTAGE_AVG: | |
22fadd76 | 383 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
fb972873 DW |
384 | ret = olpc_ec_cmd(EC_BAT_VOLTAGE, NULL, 0, (void *)&ec_word, 2); |
385 | if (ret) | |
386 | return ret; | |
387 | ||
7cfbb294 | 388 | val->intval = (s16)be16_to_cpu(ec_word) * 9760L / 32; |
fb972873 DW |
389 | break; |
390 | case POWER_SUPPLY_PROP_CURRENT_AVG: | |
22fadd76 | 391 | case POWER_SUPPLY_PROP_CURRENT_NOW: |
fb972873 DW |
392 | ret = olpc_ec_cmd(EC_BAT_CURRENT, NULL, 0, (void *)&ec_word, 2); |
393 | if (ret) | |
394 | return ret; | |
395 | ||
7cfbb294 | 396 | val->intval = (s16)be16_to_cpu(ec_word) * 15625L / 120; |
fb972873 DW |
397 | break; |
398 | case POWER_SUPPLY_PROP_CAPACITY: | |
399 | ret = olpc_ec_cmd(EC_BAT_SOC, NULL, 0, &ec_byte, 1); | |
400 | if (ret) | |
401 | return ret; | |
402 | val->intval = ec_byte; | |
403 | break; | |
b294a290 AS |
404 | case POWER_SUPPLY_PROP_CAPACITY_LEVEL: |
405 | if (ec_byte & BAT_STAT_FULL) | |
406 | val->intval = POWER_SUPPLY_CAPACITY_LEVEL_FULL; | |
407 | else if (ec_byte & BAT_STAT_LOW) | |
408 | val->intval = POWER_SUPPLY_CAPACITY_LEVEL_LOW; | |
409 | else | |
410 | val->intval = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL; | |
411 | break; | |
b202a5e6 SS |
412 | case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: |
413 | ret = olpc_bat_get_charge_full_design(val); | |
414 | if (ret) | |
415 | return ret; | |
416 | break; | |
20fd9830 SS |
417 | case POWER_SUPPLY_PROP_CHARGE_NOW: |
418 | ret = olpc_bat_get_charge_now(val); | |
419 | if (ret) | |
420 | return ret; | |
421 | break; | |
fb972873 DW |
422 | case POWER_SUPPLY_PROP_TEMP: |
423 | ret = olpc_ec_cmd(EC_BAT_TEMP, NULL, 0, (void *)&ec_word, 2); | |
424 | if (ret) | |
425 | return ret; | |
8e9c7716 | 426 | |
7cfbb294 | 427 | val->intval = (s16)be16_to_cpu(ec_word) * 100 / 256; |
fb972873 DW |
428 | break; |
429 | case POWER_SUPPLY_PROP_TEMP_AMBIENT: | |
430 | ret = olpc_ec_cmd(EC_AMB_TEMP, NULL, 0, (void *)&ec_word, 2); | |
431 | if (ret) | |
432 | return ret; | |
433 | ||
8e9c7716 | 434 | val->intval = (int)be16_to_cpu(ec_word) * 100 / 256; |
fb972873 | 435 | break; |
8e552c36 AS |
436 | case POWER_SUPPLY_PROP_CHARGE_COUNTER: |
437 | ret = olpc_ec_cmd(EC_BAT_ACR, NULL, 0, (void *)&ec_word, 2); | |
438 | if (ret) | |
439 | return ret; | |
440 | ||
7cfbb294 | 441 | val->intval = (s16)be16_to_cpu(ec_word) * 6250 / 15; |
8e552c36 | 442 | break; |
1ca5b9d2 DW |
443 | case POWER_SUPPLY_PROP_SERIAL_NUMBER: |
444 | ret = olpc_ec_cmd(EC_BAT_SERIAL, NULL, 0, (void *)&ser_buf, 8); | |
445 | if (ret) | |
446 | return ret; | |
447 | ||
448 | sprintf(bat_serial, "%016llx", (long long)be64_to_cpu(ser_buf)); | |
449 | val->strval = bat_serial; | |
450 | break; | |
5619d0ba RS |
451 | case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN: |
452 | ret = olpc_bat_get_voltage_max_design(val); | |
453 | if (ret) | |
454 | return ret; | |
455 | break; | |
fb972873 DW |
456 | default: |
457 | ret = -EINVAL; | |
458 | break; | |
459 | } | |
460 | ||
461 | return ret; | |
462 | } | |
463 | ||
c566d299 | 464 | static enum power_supply_property olpc_xo1_bat_props[] = { |
fb972873 | 465 | POWER_SUPPLY_PROP_STATUS, |
ee8076ed | 466 | POWER_SUPPLY_PROP_CHARGE_TYPE, |
fb972873 DW |
467 | POWER_SUPPLY_PROP_PRESENT, |
468 | POWER_SUPPLY_PROP_HEALTH, | |
469 | POWER_SUPPLY_PROP_TECHNOLOGY, | |
470 | POWER_SUPPLY_PROP_VOLTAGE_AVG, | |
22fadd76 | 471 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
fb972873 | 472 | POWER_SUPPLY_PROP_CURRENT_AVG, |
22fadd76 | 473 | POWER_SUPPLY_PROP_CURRENT_NOW, |
fb972873 | 474 | POWER_SUPPLY_PROP_CAPACITY, |
b294a290 | 475 | POWER_SUPPLY_PROP_CAPACITY_LEVEL, |
b202a5e6 | 476 | POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, |
20fd9830 | 477 | POWER_SUPPLY_PROP_CHARGE_NOW, |
fb972873 DW |
478 | POWER_SUPPLY_PROP_TEMP, |
479 | POWER_SUPPLY_PROP_TEMP_AMBIENT, | |
480 | POWER_SUPPLY_PROP_MANUFACTURER, | |
1ca5b9d2 | 481 | POWER_SUPPLY_PROP_SERIAL_NUMBER, |
8e552c36 | 482 | POWER_SUPPLY_PROP_CHARGE_COUNTER, |
5619d0ba | 483 | POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, |
fb972873 DW |
484 | }; |
485 | ||
c566d299 DD |
486 | /* XO-1.5 does not have ambient temperature property */ |
487 | static enum power_supply_property olpc_xo15_bat_props[] = { | |
488 | POWER_SUPPLY_PROP_STATUS, | |
489 | POWER_SUPPLY_PROP_CHARGE_TYPE, | |
490 | POWER_SUPPLY_PROP_PRESENT, | |
491 | POWER_SUPPLY_PROP_HEALTH, | |
492 | POWER_SUPPLY_PROP_TECHNOLOGY, | |
493 | POWER_SUPPLY_PROP_VOLTAGE_AVG, | |
bf542a4e | 494 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
c566d299 | 495 | POWER_SUPPLY_PROP_CURRENT_AVG, |
bf542a4e | 496 | POWER_SUPPLY_PROP_CURRENT_NOW, |
c566d299 DD |
497 | POWER_SUPPLY_PROP_CAPACITY, |
498 | POWER_SUPPLY_PROP_CAPACITY_LEVEL, | |
bf542a4e SS |
499 | POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, |
500 | POWER_SUPPLY_PROP_CHARGE_NOW, | |
c566d299 DD |
501 | POWER_SUPPLY_PROP_TEMP, |
502 | POWER_SUPPLY_PROP_MANUFACTURER, | |
503 | POWER_SUPPLY_PROP_SERIAL_NUMBER, | |
504 | POWER_SUPPLY_PROP_CHARGE_COUNTER, | |
5619d0ba | 505 | POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, |
c566d299 DD |
506 | }; |
507 | ||
d7eb9e36 AS |
508 | /* EEPROM reading goes completely around the power_supply API, sadly */ |
509 | ||
510 | #define EEPROM_START 0x20 | |
511 | #define EEPROM_END 0x80 | |
512 | #define EEPROM_SIZE (EEPROM_END - EEPROM_START) | |
513 | ||
2c3c8bea | 514 | static ssize_t olpc_bat_eeprom_read(struct file *filp, struct kobject *kobj, |
d7eb9e36 AS |
515 | struct bin_attribute *attr, char *buf, loff_t off, size_t count) |
516 | { | |
517 | uint8_t ec_byte; | |
04a820ea AS |
518 | int ret; |
519 | int i; | |
d7eb9e36 AS |
520 | |
521 | if (off >= EEPROM_SIZE) | |
522 | return 0; | |
523 | if (off + count > EEPROM_SIZE) | |
524 | count = EEPROM_SIZE - off; | |
525 | ||
04a820ea AS |
526 | for (i = 0; i < count; i++) { |
527 | ec_byte = EEPROM_START + off + i; | |
528 | ret = olpc_ec_cmd(EC_BAT_EEPROM, &ec_byte, 1, &buf[i], 1); | |
d7eb9e36 | 529 | if (ret) { |
04a820ea AS |
530 | pr_err("olpc-battery: " |
531 | "EC_BAT_EEPROM cmd @ 0x%x failed - %d!\n", | |
532 | ec_byte, ret); | |
d7eb9e36 AS |
533 | return -EIO; |
534 | } | |
535 | } | |
536 | ||
537 | return count; | |
538 | } | |
539 | ||
540 | static struct bin_attribute olpc_bat_eeprom = { | |
541 | .attr = { | |
542 | .name = "eeprom", | |
543 | .mode = S_IRUGO, | |
d7eb9e36 AS |
544 | }, |
545 | .size = 0, | |
546 | .read = olpc_bat_eeprom_read, | |
547 | }; | |
548 | ||
144bbeae AS |
549 | /* Allow userspace to see the specific error value pulled from the EC */ |
550 | ||
551 | static ssize_t olpc_bat_error_read(struct device *dev, | |
552 | struct device_attribute *attr, char *buf) | |
553 | { | |
554 | uint8_t ec_byte; | |
555 | ssize_t ret; | |
556 | ||
557 | ret = olpc_ec_cmd(EC_BAT_ERRCODE, NULL, 0, &ec_byte, 1); | |
558 | if (ret < 0) | |
559 | return ret; | |
560 | ||
561 | return sprintf(buf, "%d\n", ec_byte); | |
562 | } | |
563 | ||
564 | static struct device_attribute olpc_bat_error = { | |
565 | .attr = { | |
566 | .name = "error", | |
567 | .mode = S_IRUGO, | |
568 | }, | |
569 | .show = olpc_bat_error_read, | |
570 | }; | |
571 | ||
fb972873 DW |
572 | /********************************************************************* |
573 | * Initialisation | |
574 | *********************************************************************/ | |
575 | ||
fb972873 | 576 | static struct power_supply olpc_bat = { |
c3503fd0 | 577 | .name = "olpc-battery", |
fb972873 DW |
578 | .get_property = olpc_bat_get_property, |
579 | .use_for_apm = 1, | |
580 | }; | |
581 | ||
cae659af DD |
582 | static int olpc_battery_suspend(struct platform_device *pdev, |
583 | pm_message_t state) | |
584 | { | |
585 | if (device_may_wakeup(olpc_ac.dev)) | |
586 | olpc_ec_wakeup_set(EC_SCI_SRC_ACPWR); | |
587 | else | |
588 | olpc_ec_wakeup_clear(EC_SCI_SRC_ACPWR); | |
589 | ||
590 | if (device_may_wakeup(olpc_bat.dev)) | |
591 | olpc_ec_wakeup_set(EC_SCI_SRC_BATTERY | EC_SCI_SRC_BATSOC | |
592 | | EC_SCI_SRC_BATERR); | |
593 | else | |
594 | olpc_ec_wakeup_clear(EC_SCI_SRC_BATTERY | EC_SCI_SRC_BATSOC | |
595 | | EC_SCI_SRC_BATERR); | |
596 | ||
597 | return 0; | |
598 | } | |
599 | ||
c3503fd0 | 600 | static int __devinit olpc_battery_probe(struct platform_device *pdev) |
fb972873 | 601 | { |
c3503fd0 | 602 | int ret; |
fb972873 DW |
603 | uint8_t status; |
604 | ||
484d6d50 AS |
605 | /* |
606 | * We've seen a number of EC protocol changes; this driver requires | |
607 | * the latest EC protocol, supported by 0x44 and above. | |
608 | */ | |
609 | if (olpc_platform_info.ecver < 0x44) { | |
610 | printk(KERN_NOTICE "OLPC EC version 0x%02x too old for " | |
611 | "battery driver.\n", olpc_platform_info.ecver); | |
fb972873 DW |
612 | return -ENXIO; |
613 | } | |
614 | ||
615 | ret = olpc_ec_cmd(EC_BAT_STATUS, NULL, 0, &status, 1); | |
616 | if (ret) | |
617 | return ret; | |
618 | ||
619 | /* Ignore the status. It doesn't actually matter */ | |
620 | ||
c3503fd0 | 621 | ret = power_supply_register(&pdev->dev, &olpc_ac); |
fb972873 | 622 | if (ret) |
c3503fd0 | 623 | return ret; |
fb972873 | 624 | |
c566d299 DD |
625 | if (olpc_board_at_least(olpc_board_pre(0xd0))) { /* XO-1.5 */ |
626 | olpc_bat.properties = olpc_xo15_bat_props; | |
627 | olpc_bat.num_properties = ARRAY_SIZE(olpc_xo15_bat_props); | |
628 | } else { /* XO-1 */ | |
629 | olpc_bat.properties = olpc_xo1_bat_props; | |
630 | olpc_bat.num_properties = ARRAY_SIZE(olpc_xo1_bat_props); | |
631 | } | |
fb972873 | 632 | |
c3503fd0 | 633 | ret = power_supply_register(&pdev->dev, &olpc_bat); |
fb972873 DW |
634 | if (ret) |
635 | goto battery_failed; | |
636 | ||
d7eb9e36 AS |
637 | ret = device_create_bin_file(olpc_bat.dev, &olpc_bat_eeprom); |
638 | if (ret) | |
639 | goto eeprom_failed; | |
640 | ||
144bbeae AS |
641 | ret = device_create_file(olpc_bat.dev, &olpc_bat_error); |
642 | if (ret) | |
643 | goto error_failed; | |
644 | ||
cae659af DD |
645 | if (olpc_ec_wakeup_available()) { |
646 | device_set_wakeup_capable(olpc_ac.dev, true); | |
647 | device_set_wakeup_capable(olpc_bat.dev, true); | |
648 | } | |
649 | ||
c3503fd0 | 650 | return 0; |
fb972873 | 651 | |
144bbeae AS |
652 | error_failed: |
653 | device_remove_bin_file(olpc_bat.dev, &olpc_bat_eeprom); | |
d7eb9e36 AS |
654 | eeprom_failed: |
655 | power_supply_unregister(&olpc_bat); | |
fb972873 DW |
656 | battery_failed: |
657 | power_supply_unregister(&olpc_ac); | |
fb972873 DW |
658 | return ret; |
659 | } | |
660 | ||
c3503fd0 | 661 | static int __devexit olpc_battery_remove(struct platform_device *pdev) |
fb972873 | 662 | { |
144bbeae | 663 | device_remove_file(olpc_bat.dev, &olpc_bat_error); |
d7eb9e36 | 664 | device_remove_bin_file(olpc_bat.dev, &olpc_bat_eeprom); |
fb972873 DW |
665 | power_supply_unregister(&olpc_bat); |
666 | power_supply_unregister(&olpc_ac); | |
c3503fd0 | 667 | return 0; |
fb972873 DW |
668 | } |
669 | ||
c3503fd0 DD |
670 | static const struct of_device_id olpc_battery_ids[] __devinitconst = { |
671 | { .compatible = "olpc,xo1-battery" }, | |
672 | {} | |
673 | }; | |
674 | MODULE_DEVICE_TABLE(of, olpc_battery_ids); | |
675 | ||
5519d00e | 676 | static struct platform_driver olpc_battery_driver = { |
c3503fd0 DD |
677 | .driver = { |
678 | .name = "olpc-battery", | |
679 | .owner = THIS_MODULE, | |
680 | .of_match_table = olpc_battery_ids, | |
681 | }, | |
682 | .probe = olpc_battery_probe, | |
683 | .remove = __devexit_p(olpc_battery_remove), | |
cae659af | 684 | .suspend = olpc_battery_suspend, |
c3503fd0 DD |
685 | }; |
686 | ||
300bac7f | 687 | module_platform_driver(olpc_battery_driver); |
fb972873 DW |
688 | |
689 | MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>"); | |
690 | MODULE_LICENSE("GPL"); | |
691 | MODULE_DESCRIPTION("Battery driver for One Laptop Per Child 'XO' machine"); |