2 * Battery driver for One Laptop Per Child board.
4 * Copyright © 2006-2010 David Woodhouse <dwmw2@infradead.org>
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.
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/mod_devicetable.h>
14 #include <linux/types.h>
15 #include <linux/err.h>
16 #include <linux/device.h>
17 #include <linux/platform_device.h>
18 #include <linux/power_supply.h>
19 #include <linux/jiffies.h>
20 #include <linux/sched.h>
21 #include <linux/olpc-ec.h>
25 #define EC_BAT_VOLTAGE 0x10 /* uint16_t, *9.76/32, mV */
26 #define EC_BAT_CURRENT 0x11 /* int16_t, *15.625/120, mA */
27 #define EC_BAT_ACR 0x12 /* int16_t, *6250/15, µAh */
28 #define EC_BAT_TEMP 0x13 /* uint16_t, *100/256, °C */
29 #define EC_AMB_TEMP 0x14 /* uint16_t, *100/256, °C */
30 #define EC_BAT_STATUS 0x15 /* uint8_t, bitmask */
31 #define EC_BAT_SOC 0x16 /* uint8_t, percentage */
32 #define EC_BAT_SERIAL 0x17 /* uint8_t[6] */
33 #define EC_BAT_EEPROM 0x18 /* uint8_t adr as input, uint8_t output */
34 #define EC_BAT_ERRCODE 0x1f /* uint8_t, bitmask */
36 #define BAT_STAT_PRESENT 0x01
37 #define BAT_STAT_FULL 0x02
38 #define BAT_STAT_LOW 0x04
39 #define BAT_STAT_DESTROY 0x08
40 #define BAT_STAT_AC 0x10
41 #define BAT_STAT_CHARGING 0x20
42 #define BAT_STAT_DISCHARGING 0x40
43 #define BAT_STAT_TRICKLE 0x80
45 #define BAT_ERR_INFOFAIL 0x02
46 #define BAT_ERR_OVERVOLTAGE 0x04
47 #define BAT_ERR_OVERTEMP 0x05
48 #define BAT_ERR_GAUGESTOP 0x06
49 #define BAT_ERR_OUT_OF_CONTROL 0x07
50 #define BAT_ERR_ID_FAIL 0x09
51 #define BAT_ERR_ACR_FAIL 0x10
53 #define BAT_ADDR_MFR_TYPE 0x5F
55 /*********************************************************************
57 *********************************************************************/
59 static int olpc_ac_get_prop(struct power_supply *psy,
60 enum power_supply_property psp,
61 union power_supply_propval *val)
67 case POWER_SUPPLY_PROP_ONLINE:
68 ret = olpc_ec_cmd(EC_BAT_STATUS, NULL, 0, &status, 1);
72 val->intval = !!(status & BAT_STAT_AC);
81 static enum power_supply_property olpc_ac_props[] = {
82 POWER_SUPPLY_PROP_ONLINE,
85 static const struct power_supply_desc olpc_ac_desc = {
87 .type = POWER_SUPPLY_TYPE_MAINS,
88 .properties = olpc_ac_props,
89 .num_properties = ARRAY_SIZE(olpc_ac_props),
90 .get_property = olpc_ac_get_prop,
93 static struct power_supply *olpc_ac;
95 static char bat_serial[17]; /* Ick */
97 static int olpc_bat_get_status(union power_supply_propval *val, uint8_t ec_byte)
99 if (olpc_platform_info.ecver > 0x44) {
100 if (ec_byte & (BAT_STAT_CHARGING | BAT_STAT_TRICKLE))
101 val->intval = POWER_SUPPLY_STATUS_CHARGING;
102 else if (ec_byte & BAT_STAT_DISCHARGING)
103 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
104 else if (ec_byte & BAT_STAT_FULL)
105 val->intval = POWER_SUPPLY_STATUS_FULL;
107 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
109 /* Older EC didn't report charge/discharge bits */
110 if (!(ec_byte & BAT_STAT_AC)) /* No AC means discharging */
111 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
112 else if (ec_byte & BAT_STAT_FULL)
113 val->intval = POWER_SUPPLY_STATUS_FULL;
114 else /* Not _necessarily_ true but EC doesn't tell all yet */
115 val->intval = POWER_SUPPLY_STATUS_CHARGING;
121 static int olpc_bat_get_health(union power_supply_propval *val)
126 ret = olpc_ec_cmd(EC_BAT_ERRCODE, NULL, 0, &ec_byte, 1);
132 val->intval = POWER_SUPPLY_HEALTH_GOOD;
135 case BAT_ERR_OVERTEMP:
136 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
139 case BAT_ERR_OVERVOLTAGE:
140 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
143 case BAT_ERR_INFOFAIL:
144 case BAT_ERR_OUT_OF_CONTROL:
145 case BAT_ERR_ID_FAIL:
146 case BAT_ERR_ACR_FAIL:
147 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
151 /* Eep. We don't know this failure code */
158 static int olpc_bat_get_mfr(union power_supply_propval *val)
163 ec_byte = BAT_ADDR_MFR_TYPE;
164 ret = olpc_ec_cmd(EC_BAT_EEPROM, &ec_byte, 1, &ec_byte, 1);
168 switch (ec_byte >> 4) {
170 val->strval = "Gold Peak";
176 val->strval = "Unknown";
183 static int olpc_bat_get_tech(union power_supply_propval *val)
188 ec_byte = BAT_ADDR_MFR_TYPE;
189 ret = olpc_ec_cmd(EC_BAT_EEPROM, &ec_byte, 1, &ec_byte, 1);
193 switch (ec_byte & 0xf) {
195 val->intval = POWER_SUPPLY_TECHNOLOGY_NiMH;
198 val->intval = POWER_SUPPLY_TECHNOLOGY_LiFe;
201 val->intval = POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
208 static int olpc_bat_get_charge_full_design(union power_supply_propval *val)
211 union power_supply_propval tech;
214 ret = olpc_bat_get_tech(&tech);
218 ec_byte = BAT_ADDR_MFR_TYPE;
219 ret = olpc_ec_cmd(EC_BAT_EEPROM, &ec_byte, 1, &ec_byte, 1);
225 switch (tech.intval) {
226 case POWER_SUPPLY_TECHNOLOGY_NiMH:
228 case 1: /* Gold Peak */
229 val->intval = 3000000*.8;
236 case POWER_SUPPLY_TECHNOLOGY_LiFe:
238 case 1: /* Gold Peak, fall through */
240 val->intval = 2800000;
254 static int olpc_bat_get_charge_now(union power_supply_propval *val)
257 union power_supply_propval full;
260 ret = olpc_ec_cmd(EC_BAT_SOC, NULL, 0, &soc, 1);
264 ret = olpc_bat_get_charge_full_design(&full);
268 val->intval = soc * (full.intval / 100);
272 static int olpc_bat_get_voltage_max_design(union power_supply_propval *val)
275 union power_supply_propval tech;
279 ret = olpc_bat_get_tech(&tech);
283 ec_byte = BAT_ADDR_MFR_TYPE;
284 ret = olpc_ec_cmd(EC_BAT_EEPROM, &ec_byte, 1, &ec_byte, 1);
290 switch (tech.intval) {
291 case POWER_SUPPLY_TECHNOLOGY_NiMH:
293 case 1: /* Gold Peak */
294 val->intval = 6000000;
301 case POWER_SUPPLY_TECHNOLOGY_LiFe:
303 case 1: /* Gold Peak */
304 val->intval = 6400000;
307 val->intval = 6500000;
321 /*********************************************************************
323 *********************************************************************/
324 static int olpc_bat_get_property(struct power_supply *psy,
325 enum power_supply_property psp,
326 union power_supply_propval *val)
333 ret = olpc_ec_cmd(EC_BAT_STATUS, NULL, 0, &ec_byte, 1);
337 /* Theoretically there's a race here -- the battery could be
338 removed immediately after we check whether it's present, and
339 then we query for some other property of the now-absent battery.
340 It doesn't matter though -- the EC will return the last-known
341 information, and it's as if we just ran that _little_ bit faster
342 and managed to read it out before the battery went away. */
343 if (!(ec_byte & (BAT_STAT_PRESENT | BAT_STAT_TRICKLE)) &&
344 psp != POWER_SUPPLY_PROP_PRESENT)
348 case POWER_SUPPLY_PROP_STATUS:
349 ret = olpc_bat_get_status(val, ec_byte);
353 case POWER_SUPPLY_PROP_CHARGE_TYPE:
354 if (ec_byte & BAT_STAT_TRICKLE)
355 val->intval = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
356 else if (ec_byte & BAT_STAT_CHARGING)
357 val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
359 val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;
361 case POWER_SUPPLY_PROP_PRESENT:
362 val->intval = !!(ec_byte & (BAT_STAT_PRESENT |
366 case POWER_SUPPLY_PROP_HEALTH:
367 if (ec_byte & BAT_STAT_DESTROY)
368 val->intval = POWER_SUPPLY_HEALTH_DEAD;
370 ret = olpc_bat_get_health(val);
376 case POWER_SUPPLY_PROP_MANUFACTURER:
377 ret = olpc_bat_get_mfr(val);
381 case POWER_SUPPLY_PROP_TECHNOLOGY:
382 ret = olpc_bat_get_tech(val);
386 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
387 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
388 ret = olpc_ec_cmd(EC_BAT_VOLTAGE, NULL, 0, (void *)&ec_word, 2);
392 val->intval = (s16)be16_to_cpu(ec_word) * 9760L / 32;
394 case POWER_SUPPLY_PROP_CURRENT_AVG:
395 case POWER_SUPPLY_PROP_CURRENT_NOW:
396 ret = olpc_ec_cmd(EC_BAT_CURRENT, NULL, 0, (void *)&ec_word, 2);
400 val->intval = (s16)be16_to_cpu(ec_word) * 15625L / 120;
402 case POWER_SUPPLY_PROP_CAPACITY:
403 ret = olpc_ec_cmd(EC_BAT_SOC, NULL, 0, &ec_byte, 1);
406 val->intval = ec_byte;
408 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
409 if (ec_byte & BAT_STAT_FULL)
410 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
411 else if (ec_byte & BAT_STAT_LOW)
412 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
414 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
416 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
417 ret = olpc_bat_get_charge_full_design(val);
421 case POWER_SUPPLY_PROP_CHARGE_NOW:
422 ret = olpc_bat_get_charge_now(val);
426 case POWER_SUPPLY_PROP_TEMP:
427 ret = olpc_ec_cmd(EC_BAT_TEMP, NULL, 0, (void *)&ec_word, 2);
431 val->intval = (s16)be16_to_cpu(ec_word) * 10 / 256;
433 case POWER_SUPPLY_PROP_TEMP_AMBIENT:
434 ret = olpc_ec_cmd(EC_AMB_TEMP, NULL, 0, (void *)&ec_word, 2);
438 val->intval = (int)be16_to_cpu(ec_word) * 10 / 256;
440 case POWER_SUPPLY_PROP_CHARGE_COUNTER:
441 ret = olpc_ec_cmd(EC_BAT_ACR, NULL, 0, (void *)&ec_word, 2);
445 val->intval = (s16)be16_to_cpu(ec_word) * 6250 / 15;
447 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
448 ret = olpc_ec_cmd(EC_BAT_SERIAL, NULL, 0, (void *)&ser_buf, 8);
452 sprintf(bat_serial, "%016llx", (long long)be64_to_cpu(ser_buf));
453 val->strval = bat_serial;
455 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
456 ret = olpc_bat_get_voltage_max_design(val);
468 static enum power_supply_property olpc_xo1_bat_props[] = {
469 POWER_SUPPLY_PROP_STATUS,
470 POWER_SUPPLY_PROP_CHARGE_TYPE,
471 POWER_SUPPLY_PROP_PRESENT,
472 POWER_SUPPLY_PROP_HEALTH,
473 POWER_SUPPLY_PROP_TECHNOLOGY,
474 POWER_SUPPLY_PROP_VOLTAGE_AVG,
475 POWER_SUPPLY_PROP_VOLTAGE_NOW,
476 POWER_SUPPLY_PROP_CURRENT_AVG,
477 POWER_SUPPLY_PROP_CURRENT_NOW,
478 POWER_SUPPLY_PROP_CAPACITY,
479 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
480 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
481 POWER_SUPPLY_PROP_CHARGE_NOW,
482 POWER_SUPPLY_PROP_TEMP,
483 POWER_SUPPLY_PROP_TEMP_AMBIENT,
484 POWER_SUPPLY_PROP_MANUFACTURER,
485 POWER_SUPPLY_PROP_SERIAL_NUMBER,
486 POWER_SUPPLY_PROP_CHARGE_COUNTER,
487 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
490 /* XO-1.5 does not have ambient temperature property */
491 static enum power_supply_property olpc_xo15_bat_props[] = {
492 POWER_SUPPLY_PROP_STATUS,
493 POWER_SUPPLY_PROP_CHARGE_TYPE,
494 POWER_SUPPLY_PROP_PRESENT,
495 POWER_SUPPLY_PROP_HEALTH,
496 POWER_SUPPLY_PROP_TECHNOLOGY,
497 POWER_SUPPLY_PROP_VOLTAGE_AVG,
498 POWER_SUPPLY_PROP_VOLTAGE_NOW,
499 POWER_SUPPLY_PROP_CURRENT_AVG,
500 POWER_SUPPLY_PROP_CURRENT_NOW,
501 POWER_SUPPLY_PROP_CAPACITY,
502 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
503 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
504 POWER_SUPPLY_PROP_CHARGE_NOW,
505 POWER_SUPPLY_PROP_TEMP,
506 POWER_SUPPLY_PROP_MANUFACTURER,
507 POWER_SUPPLY_PROP_SERIAL_NUMBER,
508 POWER_SUPPLY_PROP_CHARGE_COUNTER,
509 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
512 /* EEPROM reading goes completely around the power_supply API, sadly */
514 #define EEPROM_START 0x20
515 #define EEPROM_END 0x80
516 #define EEPROM_SIZE (EEPROM_END - EEPROM_START)
518 static ssize_t olpc_bat_eeprom_read(struct file *filp, struct kobject *kobj,
519 struct bin_attribute *attr, char *buf, loff_t off, size_t count)
525 for (i = 0; i < count; i++) {
526 ec_byte = EEPROM_START + off + i;
527 ret = olpc_ec_cmd(EC_BAT_EEPROM, &ec_byte, 1, &buf[i], 1);
529 pr_err("olpc-battery: "
530 "EC_BAT_EEPROM cmd @ 0x%x failed - %d!\n",
539 static const struct bin_attribute olpc_bat_eeprom = {
545 .read = olpc_bat_eeprom_read,
548 /* Allow userspace to see the specific error value pulled from the EC */
550 static ssize_t olpc_bat_error_read(struct device *dev,
551 struct device_attribute *attr, char *buf)
556 ret = olpc_ec_cmd(EC_BAT_ERRCODE, NULL, 0, &ec_byte, 1);
560 return sprintf(buf, "%d\n", ec_byte);
563 static const struct device_attribute olpc_bat_error = {
568 .show = olpc_bat_error_read,
571 /*********************************************************************
573 *********************************************************************/
575 static struct power_supply_desc olpc_bat_desc = {
576 .name = "olpc-battery",
577 .get_property = olpc_bat_get_property,
581 static struct power_supply *olpc_bat;
583 static int olpc_battery_suspend(struct platform_device *pdev,
586 if (device_may_wakeup(&olpc_ac->dev))
587 olpc_ec_wakeup_set(EC_SCI_SRC_ACPWR);
589 olpc_ec_wakeup_clear(EC_SCI_SRC_ACPWR);
591 if (device_may_wakeup(&olpc_bat->dev))
592 olpc_ec_wakeup_set(EC_SCI_SRC_BATTERY | EC_SCI_SRC_BATSOC
593 | EC_SCI_SRC_BATERR);
595 olpc_ec_wakeup_clear(EC_SCI_SRC_BATTERY | EC_SCI_SRC_BATSOC
596 | EC_SCI_SRC_BATERR);
601 static int olpc_battery_probe(struct platform_device *pdev)
607 * We've seen a number of EC protocol changes; this driver requires
608 * the latest EC protocol, supported by 0x44 and above.
610 if (olpc_platform_info.ecver < 0x44) {
611 printk(KERN_NOTICE "OLPC EC version 0x%02x too old for "
612 "battery driver.\n", olpc_platform_info.ecver);
616 ret = olpc_ec_cmd(EC_BAT_STATUS, NULL, 0, &status, 1);
620 /* Ignore the status. It doesn't actually matter */
622 olpc_ac = power_supply_register(&pdev->dev, &olpc_ac_desc, NULL);
624 return PTR_ERR(olpc_ac);
626 if (olpc_board_at_least(olpc_board_pre(0xd0))) { /* XO-1.5 */
627 olpc_bat_desc.properties = olpc_xo15_bat_props;
628 olpc_bat_desc.num_properties = ARRAY_SIZE(olpc_xo15_bat_props);
630 olpc_bat_desc.properties = olpc_xo1_bat_props;
631 olpc_bat_desc.num_properties = ARRAY_SIZE(olpc_xo1_bat_props);
634 olpc_bat = power_supply_register(&pdev->dev, &olpc_bat_desc, NULL);
635 if (IS_ERR(olpc_bat)) {
636 ret = PTR_ERR(olpc_bat);
640 ret = device_create_bin_file(&olpc_bat->dev, &olpc_bat_eeprom);
644 ret = device_create_file(&olpc_bat->dev, &olpc_bat_error);
648 if (olpc_ec_wakeup_available()) {
649 device_set_wakeup_capable(&olpc_ac->dev, true);
650 device_set_wakeup_capable(&olpc_bat->dev, true);
656 device_remove_bin_file(&olpc_bat->dev, &olpc_bat_eeprom);
658 power_supply_unregister(olpc_bat);
660 power_supply_unregister(olpc_ac);
664 static int olpc_battery_remove(struct platform_device *pdev)
666 device_remove_file(&olpc_bat->dev, &olpc_bat_error);
667 device_remove_bin_file(&olpc_bat->dev, &olpc_bat_eeprom);
668 power_supply_unregister(olpc_bat);
669 power_supply_unregister(olpc_ac);
673 static const struct of_device_id olpc_battery_ids[] = {
674 { .compatible = "olpc,xo1-battery" },
677 MODULE_DEVICE_TABLE(of, olpc_battery_ids);
679 static struct platform_driver olpc_battery_driver = {
681 .name = "olpc-battery",
682 .of_match_table = olpc_battery_ids,
684 .probe = olpc_battery_probe,
685 .remove = olpc_battery_remove,
686 .suspend = olpc_battery_suspend,
689 module_platform_driver(olpc_battery_driver);
691 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
692 MODULE_LICENSE("GPL");
693 MODULE_DESCRIPTION("Battery driver for One Laptop Per Child 'XO' machine");