olpc-battery: Add VOLTAGE_MAX_DESIGN property
[linux-2.6-block.git] / drivers / power / olpc_battery.c
CommitLineData
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
57static 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
79static enum power_supply_property olpc_ac_props[] = {
80 POWER_SUPPLY_PROP_ONLINE,
81};
82
83static 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
91static char bat_serial[17]; /* Ick */
92
b2bd8a3b
AS
93static 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
117static 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
154static 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
179static 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
204static 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) {
234 case 1: /* Gold Peak */
235 val->intval = 2800000;
236 break;
237 case 2: /* BYD */
238 val->intval = 3100000;
239 break;
240 default:
241 return -EIO;
242 }
243 break;
244
245 default:
246 return -EIO;
247 }
248
249 return ret;
250}
251
20fd9830
SS
252static int olpc_bat_get_charge_now(union power_supply_propval *val)
253{
254 uint8_t soc;
255 union power_supply_propval full;
256 int ret;
257
258 ret = olpc_ec_cmd(EC_BAT_SOC, NULL, 0, &soc, 1);
259 if (ret)
260 return ret;
261
262 ret = olpc_bat_get_charge_full_design(&full);
263 if (ret)
264 return ret;
265
266 val->intval = soc * (full.intval / 100);
267 return 0;
268}
269
5619d0ba
RS
270static int olpc_bat_get_voltage_max_design(union power_supply_propval *val)
271{
272 uint8_t ec_byte;
273 union power_supply_propval tech;
274 int mfr;
275 int ret;
276
277 ret = olpc_bat_get_tech(&tech);
278 if (ret)
279 return ret;
280
281 ec_byte = BAT_ADDR_MFR_TYPE;
282 ret = olpc_ec_cmd(EC_BAT_EEPROM, &ec_byte, 1, &ec_byte, 1);
283 if (ret)
284 return ret;
285
286 mfr = ec_byte >> 4;
287
288 switch (tech.intval) {
289 case POWER_SUPPLY_TECHNOLOGY_NiMH:
290 switch (mfr) {
291 case 1: /* Gold Peak */
292 val->intval = 6000000;
293 break;
294 default:
295 return -EIO;
296 }
297 break;
298
299 case POWER_SUPPLY_TECHNOLOGY_LiFe:
300 switch (mfr) {
301 case 1: /* Gold Peak */
302 val->intval = 6400000;
303 break;
304 case 2: /* BYD */
305 val->intval = 6500000;
306 break;
307 default:
308 return -EIO;
309 }
310 break;
311
312 default:
313 return -EIO;
314 }
315
316 return ret;
317}
318
fb972873
DW
319/*********************************************************************
320 * Battery properties
321 *********************************************************************/
322static int olpc_bat_get_property(struct power_supply *psy,
323 enum power_supply_property psp,
324 union power_supply_propval *val)
325{
326 int ret = 0;
8e9c7716 327 __be16 ec_word;
fb972873 328 uint8_t ec_byte;
8e9c7716 329 __be64 ser_buf;
fb972873
DW
330
331 ret = olpc_ec_cmd(EC_BAT_STATUS, NULL, 0, &ec_byte, 1);
332 if (ret)
333 return ret;
334
335 /* Theoretically there's a race here -- the battery could be
336 removed immediately after we check whether it's present, and
337 then we query for some other property of the now-absent battery.
338 It doesn't matter though -- the EC will return the last-known
339 information, and it's as if we just ran that _little_ bit faster
340 and managed to read it out before the battery went away. */
8f7e5798
AS
341 if (!(ec_byte & (BAT_STAT_PRESENT | BAT_STAT_TRICKLE)) &&
342 psp != POWER_SUPPLY_PROP_PRESENT)
fb972873
DW
343 return -ENODEV;
344
345 switch (psp) {
346 case POWER_SUPPLY_PROP_STATUS:
b2bd8a3b
AS
347 ret = olpc_bat_get_status(val, ec_byte);
348 if (ret)
349 return ret;
350 break;
ee8076ed
AS
351 case POWER_SUPPLY_PROP_CHARGE_TYPE:
352 if (ec_byte & BAT_STAT_TRICKLE)
353 val->intval = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
354 else if (ec_byte & BAT_STAT_CHARGING)
355 val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
356 else
357 val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;
358 break;
fb972873 359 case POWER_SUPPLY_PROP_PRESENT:
8f7e5798
AS
360 val->intval = !!(ec_byte & (BAT_STAT_PRESENT |
361 BAT_STAT_TRICKLE));
fb972873
DW
362 break;
363
364 case POWER_SUPPLY_PROP_HEALTH:
365 if (ec_byte & BAT_STAT_DESTROY)
366 val->intval = POWER_SUPPLY_HEALTH_DEAD;
367 else {
b2bd8a3b 368 ret = olpc_bat_get_health(val);
fb972873
DW
369 if (ret)
370 return ret;
fb972873
DW
371 }
372 break;
373
374 case POWER_SUPPLY_PROP_MANUFACTURER:
b2bd8a3b 375 ret = olpc_bat_get_mfr(val);
fb972873
DW
376 if (ret)
377 return ret;
fb972873
DW
378 break;
379 case POWER_SUPPLY_PROP_TECHNOLOGY:
b2bd8a3b 380 ret = olpc_bat_get_tech(val);
fb972873
DW
381 if (ret)
382 return ret;
fb972873
DW
383 break;
384 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
22fadd76 385 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
fb972873
DW
386 ret = olpc_ec_cmd(EC_BAT_VOLTAGE, NULL, 0, (void *)&ec_word, 2);
387 if (ret)
388 return ret;
389
7cfbb294 390 val->intval = (s16)be16_to_cpu(ec_word) * 9760L / 32;
fb972873
DW
391 break;
392 case POWER_SUPPLY_PROP_CURRENT_AVG:
22fadd76 393 case POWER_SUPPLY_PROP_CURRENT_NOW:
fb972873
DW
394 ret = olpc_ec_cmd(EC_BAT_CURRENT, NULL, 0, (void *)&ec_word, 2);
395 if (ret)
396 return ret;
397
7cfbb294 398 val->intval = (s16)be16_to_cpu(ec_word) * 15625L / 120;
fb972873
DW
399 break;
400 case POWER_SUPPLY_PROP_CAPACITY:
401 ret = olpc_ec_cmd(EC_BAT_SOC, NULL, 0, &ec_byte, 1);
402 if (ret)
403 return ret;
404 val->intval = ec_byte;
405 break;
b294a290
AS
406 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
407 if (ec_byte & BAT_STAT_FULL)
408 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
409 else if (ec_byte & BAT_STAT_LOW)
410 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
411 else
412 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
413 break;
b202a5e6
SS
414 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
415 ret = olpc_bat_get_charge_full_design(val);
416 if (ret)
417 return ret;
418 break;
20fd9830
SS
419 case POWER_SUPPLY_PROP_CHARGE_NOW:
420 ret = olpc_bat_get_charge_now(val);
421 if (ret)
422 return ret;
423 break;
fb972873
DW
424 case POWER_SUPPLY_PROP_TEMP:
425 ret = olpc_ec_cmd(EC_BAT_TEMP, NULL, 0, (void *)&ec_word, 2);
426 if (ret)
427 return ret;
8e9c7716 428
7cfbb294 429 val->intval = (s16)be16_to_cpu(ec_word) * 100 / 256;
fb972873
DW
430 break;
431 case POWER_SUPPLY_PROP_TEMP_AMBIENT:
432 ret = olpc_ec_cmd(EC_AMB_TEMP, NULL, 0, (void *)&ec_word, 2);
433 if (ret)
434 return ret;
435
8e9c7716 436 val->intval = (int)be16_to_cpu(ec_word) * 100 / 256;
fb972873 437 break;
8e552c36
AS
438 case POWER_SUPPLY_PROP_CHARGE_COUNTER:
439 ret = olpc_ec_cmd(EC_BAT_ACR, NULL, 0, (void *)&ec_word, 2);
440 if (ret)
441 return ret;
442
7cfbb294 443 val->intval = (s16)be16_to_cpu(ec_word) * 6250 / 15;
8e552c36 444 break;
1ca5b9d2
DW
445 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
446 ret = olpc_ec_cmd(EC_BAT_SERIAL, NULL, 0, (void *)&ser_buf, 8);
447 if (ret)
448 return ret;
449
450 sprintf(bat_serial, "%016llx", (long long)be64_to_cpu(ser_buf));
451 val->strval = bat_serial;
452 break;
5619d0ba
RS
453 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
454 ret = olpc_bat_get_voltage_max_design(val);
455 if (ret)
456 return ret;
457 break;
fb972873
DW
458 default:
459 ret = -EINVAL;
460 break;
461 }
462
463 return ret;
464}
465
c566d299 466static enum power_supply_property olpc_xo1_bat_props[] = {
fb972873 467 POWER_SUPPLY_PROP_STATUS,
ee8076ed 468 POWER_SUPPLY_PROP_CHARGE_TYPE,
fb972873
DW
469 POWER_SUPPLY_PROP_PRESENT,
470 POWER_SUPPLY_PROP_HEALTH,
471 POWER_SUPPLY_PROP_TECHNOLOGY,
472 POWER_SUPPLY_PROP_VOLTAGE_AVG,
22fadd76 473 POWER_SUPPLY_PROP_VOLTAGE_NOW,
fb972873 474 POWER_SUPPLY_PROP_CURRENT_AVG,
22fadd76 475 POWER_SUPPLY_PROP_CURRENT_NOW,
fb972873 476 POWER_SUPPLY_PROP_CAPACITY,
b294a290 477 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
b202a5e6 478 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
20fd9830 479 POWER_SUPPLY_PROP_CHARGE_NOW,
fb972873
DW
480 POWER_SUPPLY_PROP_TEMP,
481 POWER_SUPPLY_PROP_TEMP_AMBIENT,
482 POWER_SUPPLY_PROP_MANUFACTURER,
1ca5b9d2 483 POWER_SUPPLY_PROP_SERIAL_NUMBER,
8e552c36 484 POWER_SUPPLY_PROP_CHARGE_COUNTER,
5619d0ba 485 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
fb972873
DW
486};
487
c566d299
DD
488/* XO-1.5 does not have ambient temperature property */
489static enum power_supply_property olpc_xo15_bat_props[] = {
490 POWER_SUPPLY_PROP_STATUS,
491 POWER_SUPPLY_PROP_CHARGE_TYPE,
492 POWER_SUPPLY_PROP_PRESENT,
493 POWER_SUPPLY_PROP_HEALTH,
494 POWER_SUPPLY_PROP_TECHNOLOGY,
495 POWER_SUPPLY_PROP_VOLTAGE_AVG,
bf542a4e 496 POWER_SUPPLY_PROP_VOLTAGE_NOW,
c566d299 497 POWER_SUPPLY_PROP_CURRENT_AVG,
bf542a4e 498 POWER_SUPPLY_PROP_CURRENT_NOW,
c566d299
DD
499 POWER_SUPPLY_PROP_CAPACITY,
500 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
bf542a4e
SS
501 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
502 POWER_SUPPLY_PROP_CHARGE_NOW,
c566d299
DD
503 POWER_SUPPLY_PROP_TEMP,
504 POWER_SUPPLY_PROP_MANUFACTURER,
505 POWER_SUPPLY_PROP_SERIAL_NUMBER,
506 POWER_SUPPLY_PROP_CHARGE_COUNTER,
5619d0ba 507 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
c566d299
DD
508};
509
d7eb9e36
AS
510/* EEPROM reading goes completely around the power_supply API, sadly */
511
512#define EEPROM_START 0x20
513#define EEPROM_END 0x80
514#define EEPROM_SIZE (EEPROM_END - EEPROM_START)
515
2c3c8bea 516static ssize_t olpc_bat_eeprom_read(struct file *filp, struct kobject *kobj,
d7eb9e36
AS
517 struct bin_attribute *attr, char *buf, loff_t off, size_t count)
518{
519 uint8_t ec_byte;
04a820ea
AS
520 int ret;
521 int i;
d7eb9e36
AS
522
523 if (off >= EEPROM_SIZE)
524 return 0;
525 if (off + count > EEPROM_SIZE)
526 count = EEPROM_SIZE - off;
527
04a820ea
AS
528 for (i = 0; i < count; i++) {
529 ec_byte = EEPROM_START + off + i;
530 ret = olpc_ec_cmd(EC_BAT_EEPROM, &ec_byte, 1, &buf[i], 1);
d7eb9e36 531 if (ret) {
04a820ea
AS
532 pr_err("olpc-battery: "
533 "EC_BAT_EEPROM cmd @ 0x%x failed - %d!\n",
534 ec_byte, ret);
d7eb9e36
AS
535 return -EIO;
536 }
537 }
538
539 return count;
540}
541
542static struct bin_attribute olpc_bat_eeprom = {
543 .attr = {
544 .name = "eeprom",
545 .mode = S_IRUGO,
d7eb9e36
AS
546 },
547 .size = 0,
548 .read = olpc_bat_eeprom_read,
549};
550
144bbeae
AS
551/* Allow userspace to see the specific error value pulled from the EC */
552
553static ssize_t olpc_bat_error_read(struct device *dev,
554 struct device_attribute *attr, char *buf)
555{
556 uint8_t ec_byte;
557 ssize_t ret;
558
559 ret = olpc_ec_cmd(EC_BAT_ERRCODE, NULL, 0, &ec_byte, 1);
560 if (ret < 0)
561 return ret;
562
563 return sprintf(buf, "%d\n", ec_byte);
564}
565
566static struct device_attribute olpc_bat_error = {
567 .attr = {
568 .name = "error",
569 .mode = S_IRUGO,
570 },
571 .show = olpc_bat_error_read,
572};
573
fb972873
DW
574/*********************************************************************
575 * Initialisation
576 *********************************************************************/
577
fb972873 578static struct power_supply olpc_bat = {
c3503fd0 579 .name = "olpc-battery",
fb972873
DW
580 .get_property = olpc_bat_get_property,
581 .use_for_apm = 1,
582};
583
cae659af
DD
584static int olpc_battery_suspend(struct platform_device *pdev,
585 pm_message_t state)
586{
587 if (device_may_wakeup(olpc_ac.dev))
588 olpc_ec_wakeup_set(EC_SCI_SRC_ACPWR);
589 else
590 olpc_ec_wakeup_clear(EC_SCI_SRC_ACPWR);
591
592 if (device_may_wakeup(olpc_bat.dev))
593 olpc_ec_wakeup_set(EC_SCI_SRC_BATTERY | EC_SCI_SRC_BATSOC
594 | EC_SCI_SRC_BATERR);
595 else
596 olpc_ec_wakeup_clear(EC_SCI_SRC_BATTERY | EC_SCI_SRC_BATSOC
597 | EC_SCI_SRC_BATERR);
598
599 return 0;
600}
601
c3503fd0 602static int __devinit olpc_battery_probe(struct platform_device *pdev)
fb972873 603{
c3503fd0 604 int ret;
fb972873
DW
605 uint8_t status;
606
484d6d50
AS
607 /*
608 * We've seen a number of EC protocol changes; this driver requires
609 * the latest EC protocol, supported by 0x44 and above.
610 */
611 if (olpc_platform_info.ecver < 0x44) {
612 printk(KERN_NOTICE "OLPC EC version 0x%02x too old for "
613 "battery driver.\n", olpc_platform_info.ecver);
fb972873
DW
614 return -ENXIO;
615 }
616
617 ret = olpc_ec_cmd(EC_BAT_STATUS, NULL, 0, &status, 1);
618 if (ret)
619 return ret;
620
621 /* Ignore the status. It doesn't actually matter */
622
c3503fd0 623 ret = power_supply_register(&pdev->dev, &olpc_ac);
fb972873 624 if (ret)
c3503fd0 625 return ret;
fb972873 626
c566d299
DD
627 if (olpc_board_at_least(olpc_board_pre(0xd0))) { /* XO-1.5 */
628 olpc_bat.properties = olpc_xo15_bat_props;
629 olpc_bat.num_properties = ARRAY_SIZE(olpc_xo15_bat_props);
630 } else { /* XO-1 */
631 olpc_bat.properties = olpc_xo1_bat_props;
632 olpc_bat.num_properties = ARRAY_SIZE(olpc_xo1_bat_props);
633 }
fb972873 634
c3503fd0 635 ret = power_supply_register(&pdev->dev, &olpc_bat);
fb972873
DW
636 if (ret)
637 goto battery_failed;
638
d7eb9e36
AS
639 ret = device_create_bin_file(olpc_bat.dev, &olpc_bat_eeprom);
640 if (ret)
641 goto eeprom_failed;
642
144bbeae
AS
643 ret = device_create_file(olpc_bat.dev, &olpc_bat_error);
644 if (ret)
645 goto error_failed;
646
cae659af
DD
647 if (olpc_ec_wakeup_available()) {
648 device_set_wakeup_capable(olpc_ac.dev, true);
649 device_set_wakeup_capable(olpc_bat.dev, true);
650 }
651
c3503fd0 652 return 0;
fb972873 653
144bbeae
AS
654error_failed:
655 device_remove_bin_file(olpc_bat.dev, &olpc_bat_eeprom);
d7eb9e36
AS
656eeprom_failed:
657 power_supply_unregister(&olpc_bat);
fb972873
DW
658battery_failed:
659 power_supply_unregister(&olpc_ac);
fb972873
DW
660 return ret;
661}
662
c3503fd0 663static int __devexit olpc_battery_remove(struct platform_device *pdev)
fb972873 664{
144bbeae 665 device_remove_file(olpc_bat.dev, &olpc_bat_error);
d7eb9e36 666 device_remove_bin_file(olpc_bat.dev, &olpc_bat_eeprom);
fb972873
DW
667 power_supply_unregister(&olpc_bat);
668 power_supply_unregister(&olpc_ac);
c3503fd0 669 return 0;
fb972873
DW
670}
671
c3503fd0
DD
672static const struct of_device_id olpc_battery_ids[] __devinitconst = {
673 { .compatible = "olpc,xo1-battery" },
674 {}
675};
676MODULE_DEVICE_TABLE(of, olpc_battery_ids);
677
5519d00e 678static struct platform_driver olpc_battery_driver = {
c3503fd0
DD
679 .driver = {
680 .name = "olpc-battery",
681 .owner = THIS_MODULE,
682 .of_match_table = olpc_battery_ids,
683 },
684 .probe = olpc_battery_probe,
685 .remove = __devexit_p(olpc_battery_remove),
cae659af 686 .suspend = olpc_battery_suspend,
c3503fd0
DD
687};
688
300bac7f 689module_platform_driver(olpc_battery_driver);
fb972873
DW
690
691MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
692MODULE_LICENSE("GPL");
693MODULE_DESCRIPTION("Battery driver for One Laptop Per Child 'XO' machine");