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>
18 #include <linux/platform_device.h>
19 #include <linux/power_supply.h>
20 #include <linux/jiffies.h>
21 #include <linux/sched.h>
22 #include <linux/olpc-ec.h>
26 #define EC_BAT_VOLTAGE 0x10 /* uint16_t, *9.76/32, mV */
27 #define EC_BAT_CURRENT 0x11 /* int16_t, *15.625/120, mA */
28 #define EC_BAT_ACR 0x12 /* int16_t, *6250/15, µAh */
29 #define EC_BAT_TEMP 0x13 /* uint16_t, *100/256, °C */
30 #define EC_AMB_TEMP 0x14 /* uint16_t, *100/256, °C */
31 #define EC_BAT_STATUS 0x15 /* uint8_t, bitmask */
32 #define EC_BAT_SOC 0x16 /* uint8_t, percentage */
33 #define EC_BAT_SERIAL 0x17 /* uint8_t[6] */
34 #define EC_BAT_EEPROM 0x18 /* uint8_t adr as input, uint8_t output */
35 #define EC_BAT_ERRCODE 0x1f /* uint8_t, bitmask */
37 #define BAT_STAT_PRESENT 0x01
38 #define BAT_STAT_FULL 0x02
39 #define BAT_STAT_LOW 0x04
40 #define BAT_STAT_DESTROY 0x08
41 #define BAT_STAT_AC 0x10
42 #define BAT_STAT_CHARGING 0x20
43 #define BAT_STAT_DISCHARGING 0x40
44 #define BAT_STAT_TRICKLE 0x80
46 #define BAT_ERR_INFOFAIL 0x02
47 #define BAT_ERR_OVERVOLTAGE 0x04
48 #define BAT_ERR_OVERTEMP 0x05
49 #define BAT_ERR_GAUGESTOP 0x06
50 #define BAT_ERR_OUT_OF_CONTROL 0x07
51 #define BAT_ERR_ID_FAIL 0x09
52 #define BAT_ERR_ACR_FAIL 0x10
54 #define BAT_ADDR_MFR_TYPE 0x5F
56 struct olpc_battery_data {
57 struct power_supply *olpc_ac;
58 struct power_supply *olpc_bat;
64 /*********************************************************************
66 *********************************************************************/
68 static int olpc_ac_get_prop(struct power_supply *psy,
69 enum power_supply_property psp,
70 union power_supply_propval *val)
76 case POWER_SUPPLY_PROP_ONLINE:
77 ret = olpc_ec_cmd(EC_BAT_STATUS, NULL, 0, &status, 1);
81 val->intval = !!(status & BAT_STAT_AC);
90 static enum power_supply_property olpc_ac_props[] = {
91 POWER_SUPPLY_PROP_ONLINE,
94 static const struct power_supply_desc olpc_ac_desc = {
96 .type = POWER_SUPPLY_TYPE_MAINS,
97 .properties = olpc_ac_props,
98 .num_properties = ARRAY_SIZE(olpc_ac_props),
99 .get_property = olpc_ac_get_prop,
102 static int olpc_bat_get_status(struct olpc_battery_data *data,
103 union power_supply_propval *val, uint8_t ec_byte)
105 if (data->new_proto) {
106 if (ec_byte & (BAT_STAT_CHARGING | BAT_STAT_TRICKLE))
107 val->intval = POWER_SUPPLY_STATUS_CHARGING;
108 else if (ec_byte & BAT_STAT_DISCHARGING)
109 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
110 else if (ec_byte & BAT_STAT_FULL)
111 val->intval = POWER_SUPPLY_STATUS_FULL;
113 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
115 /* Older EC didn't report charge/discharge bits */
116 if (!(ec_byte & BAT_STAT_AC)) /* No AC means discharging */
117 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
118 else if (ec_byte & BAT_STAT_FULL)
119 val->intval = POWER_SUPPLY_STATUS_FULL;
120 else /* Not _necessarily_ true but EC doesn't tell all yet */
121 val->intval = POWER_SUPPLY_STATUS_CHARGING;
127 static int olpc_bat_get_health(union power_supply_propval *val)
132 ret = olpc_ec_cmd(EC_BAT_ERRCODE, NULL, 0, &ec_byte, 1);
138 val->intval = POWER_SUPPLY_HEALTH_GOOD;
141 case BAT_ERR_OVERTEMP:
142 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
145 case BAT_ERR_OVERVOLTAGE:
146 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
149 case BAT_ERR_INFOFAIL:
150 case BAT_ERR_OUT_OF_CONTROL:
151 case BAT_ERR_ID_FAIL:
152 case BAT_ERR_ACR_FAIL:
153 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
157 /* Eep. We don't know this failure code */
164 static int olpc_bat_get_mfr(union power_supply_propval *val)
169 ec_byte = BAT_ADDR_MFR_TYPE;
170 ret = olpc_ec_cmd(EC_BAT_EEPROM, &ec_byte, 1, &ec_byte, 1);
174 switch (ec_byte >> 4) {
176 val->strval = "Gold Peak";
182 val->strval = "Unknown";
189 static int olpc_bat_get_tech(union power_supply_propval *val)
194 ec_byte = BAT_ADDR_MFR_TYPE;
195 ret = olpc_ec_cmd(EC_BAT_EEPROM, &ec_byte, 1, &ec_byte, 1);
199 switch (ec_byte & 0xf) {
201 val->intval = POWER_SUPPLY_TECHNOLOGY_NiMH;
204 val->intval = POWER_SUPPLY_TECHNOLOGY_LiFe;
207 val->intval = POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
214 static int olpc_bat_get_charge_full_design(union power_supply_propval *val)
217 union power_supply_propval tech;
220 ret = olpc_bat_get_tech(&tech);
224 ec_byte = BAT_ADDR_MFR_TYPE;
225 ret = olpc_ec_cmd(EC_BAT_EEPROM, &ec_byte, 1, &ec_byte, 1);
231 switch (tech.intval) {
232 case POWER_SUPPLY_TECHNOLOGY_NiMH:
234 case 1: /* Gold Peak */
235 val->intval = 3000000*.8;
242 case POWER_SUPPLY_TECHNOLOGY_LiFe:
244 case 1: /* Gold Peak, fall through */
246 val->intval = 2800000;
260 static int olpc_bat_get_charge_now(union power_supply_propval *val)
263 union power_supply_propval full;
266 ret = olpc_ec_cmd(EC_BAT_SOC, NULL, 0, &soc, 1);
270 ret = olpc_bat_get_charge_full_design(&full);
274 val->intval = soc * (full.intval / 100);
278 static int olpc_bat_get_voltage_max_design(union power_supply_propval *val)
281 union power_supply_propval tech;
285 ret = olpc_bat_get_tech(&tech);
289 ec_byte = BAT_ADDR_MFR_TYPE;
290 ret = olpc_ec_cmd(EC_BAT_EEPROM, &ec_byte, 1, &ec_byte, 1);
296 switch (tech.intval) {
297 case POWER_SUPPLY_TECHNOLOGY_NiMH:
299 case 1: /* Gold Peak */
300 val->intval = 6000000;
307 case POWER_SUPPLY_TECHNOLOGY_LiFe:
309 case 1: /* Gold Peak */
310 val->intval = 6400000;
313 val->intval = 6500000;
327 static u16 ecword_to_cpu(struct olpc_battery_data *data, u16 ec_word)
329 if (data->little_endian)
330 return le16_to_cpu((__force __le16)ec_word);
332 return be16_to_cpu((__force __be16)ec_word);
335 /*********************************************************************
337 *********************************************************************/
338 static int olpc_bat_get_property(struct power_supply *psy,
339 enum power_supply_property psp,
340 union power_supply_propval *val)
342 struct olpc_battery_data *data = power_supply_get_drvdata(psy);
348 ret = olpc_ec_cmd(EC_BAT_STATUS, NULL, 0, &ec_byte, 1);
352 /* Theoretically there's a race here -- the battery could be
353 removed immediately after we check whether it's present, and
354 then we query for some other property of the now-absent battery.
355 It doesn't matter though -- the EC will return the last-known
356 information, and it's as if we just ran that _little_ bit faster
357 and managed to read it out before the battery went away. */
358 if (!(ec_byte & (BAT_STAT_PRESENT | BAT_STAT_TRICKLE)) &&
359 psp != POWER_SUPPLY_PROP_PRESENT)
363 case POWER_SUPPLY_PROP_STATUS:
364 ret = olpc_bat_get_status(data, val, ec_byte);
368 case POWER_SUPPLY_PROP_CHARGE_TYPE:
369 if (ec_byte & BAT_STAT_TRICKLE)
370 val->intval = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
371 else if (ec_byte & BAT_STAT_CHARGING)
372 val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
374 val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;
376 case POWER_SUPPLY_PROP_PRESENT:
377 val->intval = !!(ec_byte & (BAT_STAT_PRESENT |
381 case POWER_SUPPLY_PROP_HEALTH:
382 if (ec_byte & BAT_STAT_DESTROY)
383 val->intval = POWER_SUPPLY_HEALTH_DEAD;
385 ret = olpc_bat_get_health(val);
391 case POWER_SUPPLY_PROP_MANUFACTURER:
392 ret = olpc_bat_get_mfr(val);
396 case POWER_SUPPLY_PROP_TECHNOLOGY:
397 ret = olpc_bat_get_tech(val);
401 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
402 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
403 ret = olpc_ec_cmd(EC_BAT_VOLTAGE, NULL, 0, (void *)&ec_word, 2);
407 val->intval = ecword_to_cpu(data, ec_word) * 9760L / 32;
409 case POWER_SUPPLY_PROP_CURRENT_AVG:
410 case POWER_SUPPLY_PROP_CURRENT_NOW:
411 ret = olpc_ec_cmd(EC_BAT_CURRENT, NULL, 0, (void *)&ec_word, 2);
415 val->intval = ecword_to_cpu(data, ec_word) * 15625L / 120;
417 case POWER_SUPPLY_PROP_CAPACITY:
418 ret = olpc_ec_cmd(EC_BAT_SOC, NULL, 0, &ec_byte, 1);
421 val->intval = ec_byte;
423 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
424 if (ec_byte & BAT_STAT_FULL)
425 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
426 else if (ec_byte & BAT_STAT_LOW)
427 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
429 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
431 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
432 ret = olpc_bat_get_charge_full_design(val);
436 case POWER_SUPPLY_PROP_CHARGE_NOW:
437 ret = olpc_bat_get_charge_now(val);
441 case POWER_SUPPLY_PROP_TEMP:
442 ret = olpc_ec_cmd(EC_BAT_TEMP, NULL, 0, (void *)&ec_word, 2);
446 val->intval = ecword_to_cpu(data, ec_word) * 10 / 256;
448 case POWER_SUPPLY_PROP_TEMP_AMBIENT:
449 ret = olpc_ec_cmd(EC_AMB_TEMP, NULL, 0, (void *)&ec_word, 2);
453 val->intval = (int)ecword_to_cpu(data, ec_word) * 10 / 256;
455 case POWER_SUPPLY_PROP_CHARGE_COUNTER:
456 ret = olpc_ec_cmd(EC_BAT_ACR, NULL, 0, (void *)&ec_word, 2);
460 val->intval = ecword_to_cpu(data, ec_word) * 6250 / 15;
462 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
463 ret = olpc_ec_cmd(EC_BAT_SERIAL, NULL, 0, (void *)&ser_buf, 8);
467 sprintf(data->bat_serial, "%016llx", (long long)be64_to_cpu(ser_buf));
468 val->strval = data->bat_serial;
470 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
471 ret = olpc_bat_get_voltage_max_design(val);
483 static enum power_supply_property olpc_xo1_bat_props[] = {
484 POWER_SUPPLY_PROP_STATUS,
485 POWER_SUPPLY_PROP_CHARGE_TYPE,
486 POWER_SUPPLY_PROP_PRESENT,
487 POWER_SUPPLY_PROP_HEALTH,
488 POWER_SUPPLY_PROP_TECHNOLOGY,
489 POWER_SUPPLY_PROP_VOLTAGE_AVG,
490 POWER_SUPPLY_PROP_VOLTAGE_NOW,
491 POWER_SUPPLY_PROP_CURRENT_AVG,
492 POWER_SUPPLY_PROP_CURRENT_NOW,
493 POWER_SUPPLY_PROP_CAPACITY,
494 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
495 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
496 POWER_SUPPLY_PROP_CHARGE_NOW,
497 POWER_SUPPLY_PROP_TEMP,
498 POWER_SUPPLY_PROP_TEMP_AMBIENT,
499 POWER_SUPPLY_PROP_MANUFACTURER,
500 POWER_SUPPLY_PROP_SERIAL_NUMBER,
501 POWER_SUPPLY_PROP_CHARGE_COUNTER,
502 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
505 /* XO-1.5 does not have ambient temperature property */
506 static enum power_supply_property olpc_xo15_bat_props[] = {
507 POWER_SUPPLY_PROP_STATUS,
508 POWER_SUPPLY_PROP_CHARGE_TYPE,
509 POWER_SUPPLY_PROP_PRESENT,
510 POWER_SUPPLY_PROP_HEALTH,
511 POWER_SUPPLY_PROP_TECHNOLOGY,
512 POWER_SUPPLY_PROP_VOLTAGE_AVG,
513 POWER_SUPPLY_PROP_VOLTAGE_NOW,
514 POWER_SUPPLY_PROP_CURRENT_AVG,
515 POWER_SUPPLY_PROP_CURRENT_NOW,
516 POWER_SUPPLY_PROP_CAPACITY,
517 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
518 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
519 POWER_SUPPLY_PROP_CHARGE_NOW,
520 POWER_SUPPLY_PROP_TEMP,
521 POWER_SUPPLY_PROP_MANUFACTURER,
522 POWER_SUPPLY_PROP_SERIAL_NUMBER,
523 POWER_SUPPLY_PROP_CHARGE_COUNTER,
524 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
527 /* EEPROM reading goes completely around the power_supply API, sadly */
529 #define EEPROM_START 0x20
530 #define EEPROM_END 0x80
531 #define EEPROM_SIZE (EEPROM_END - EEPROM_START)
533 static ssize_t olpc_bat_eeprom_read(struct file *filp, struct kobject *kobj,
534 struct bin_attribute *attr, char *buf, loff_t off, size_t count)
540 for (i = 0; i < count; i++) {
541 ec_byte = EEPROM_START + off + i;
542 ret = olpc_ec_cmd(EC_BAT_EEPROM, &ec_byte, 1, &buf[i], 1);
544 pr_err("olpc-battery: "
545 "EC_BAT_EEPROM cmd @ 0x%x failed - %d!\n",
554 static struct bin_attribute olpc_bat_eeprom = {
560 .read = olpc_bat_eeprom_read,
563 /* Allow userspace to see the specific error value pulled from the EC */
565 static ssize_t olpc_bat_error_read(struct device *dev,
566 struct device_attribute *attr, char *buf)
571 ret = olpc_ec_cmd(EC_BAT_ERRCODE, NULL, 0, &ec_byte, 1);
575 return sprintf(buf, "%d\n", ec_byte);
578 static struct device_attribute olpc_bat_error = {
583 .show = olpc_bat_error_read,
586 static struct attribute *olpc_bat_sysfs_attrs[] = {
587 &olpc_bat_error.attr,
591 static struct bin_attribute *olpc_bat_sysfs_bin_attrs[] = {
596 static const struct attribute_group olpc_bat_sysfs_group = {
597 .attrs = olpc_bat_sysfs_attrs,
598 .bin_attrs = olpc_bat_sysfs_bin_attrs,
602 static const struct attribute_group *olpc_bat_sysfs_groups[] = {
603 &olpc_bat_sysfs_group,
607 /*********************************************************************
609 *********************************************************************/
611 static struct power_supply_desc olpc_bat_desc = {
612 .name = "olpc-battery",
613 .get_property = olpc_bat_get_property,
617 static int olpc_battery_suspend(struct platform_device *pdev,
620 struct olpc_battery_data *data = platform_get_drvdata(pdev);
622 if (device_may_wakeup(&data->olpc_ac->dev))
623 olpc_ec_wakeup_set(EC_SCI_SRC_ACPWR);
625 olpc_ec_wakeup_clear(EC_SCI_SRC_ACPWR);
627 if (device_may_wakeup(&data->olpc_bat->dev))
628 olpc_ec_wakeup_set(EC_SCI_SRC_BATTERY | EC_SCI_SRC_BATSOC
629 | EC_SCI_SRC_BATERR);
631 olpc_ec_wakeup_clear(EC_SCI_SRC_BATTERY | EC_SCI_SRC_BATSOC
632 | EC_SCI_SRC_BATERR);
637 static int olpc_battery_probe(struct platform_device *pdev)
639 struct power_supply_config bat_psy_cfg = {};
640 struct power_supply_config ac_psy_cfg = {};
641 struct olpc_battery_data *data;
646 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
649 platform_set_drvdata(pdev, data);
651 /* See if the EC is already there and get the EC revision */
652 ret = olpc_ec_cmd(EC_FIRMWARE_REV, NULL, 0, &ecver, 1);
656 if (of_find_compatible_node(NULL, NULL, "olpc,xo1.75-ec")) {
658 data->new_proto = true;
659 data->little_endian = true;
660 } else if (ecver > 0x44) {
661 /* XO 1 or 1.5 with a new EC firmware. */
662 data->new_proto = true;
663 } else if (ecver < 0x44) {
665 * We've seen a number of EC protocol changes; this driver
666 * requires the latest EC protocol, supported by 0x44 and above.
668 printk(KERN_NOTICE "OLPC EC version 0x%02x too old for "
669 "battery driver.\n", ecver);
673 ret = olpc_ec_cmd(EC_BAT_STATUS, NULL, 0, &status, 1);
677 /* Ignore the status. It doesn't actually matter */
679 ac_psy_cfg.of_node = pdev->dev.of_node;
680 ac_psy_cfg.drv_data = data;
682 data->olpc_ac = devm_power_supply_register(&pdev->dev, &olpc_ac_desc,
684 if (IS_ERR(data->olpc_ac))
685 return PTR_ERR(data->olpc_ac);
687 if (of_device_is_compatible(pdev->dev.of_node, "olpc,xo1.5-battery")) {
689 olpc_bat_desc.properties = olpc_xo15_bat_props;
690 olpc_bat_desc.num_properties = ARRAY_SIZE(olpc_xo15_bat_props);
693 olpc_bat_desc.properties = olpc_xo1_bat_props;
694 olpc_bat_desc.num_properties = ARRAY_SIZE(olpc_xo1_bat_props);
697 bat_psy_cfg.of_node = pdev->dev.of_node;
698 bat_psy_cfg.drv_data = data;
699 bat_psy_cfg.attr_grp = olpc_bat_sysfs_groups;
701 data->olpc_bat = devm_power_supply_register(&pdev->dev, &olpc_bat_desc,
703 if (IS_ERR(data->olpc_bat))
704 return PTR_ERR(data->olpc_bat);
706 if (olpc_ec_wakeup_available()) {
707 device_set_wakeup_capable(&data->olpc_ac->dev, true);
708 device_set_wakeup_capable(&data->olpc_bat->dev, true);
714 static const struct of_device_id olpc_battery_ids[] = {
715 { .compatible = "olpc,xo1-battery" },
716 { .compatible = "olpc,xo1.5-battery" },
719 MODULE_DEVICE_TABLE(of, olpc_battery_ids);
721 static struct platform_driver olpc_battery_driver = {
723 .name = "olpc-battery",
724 .of_match_table = olpc_battery_ids,
726 .probe = olpc_battery_probe,
727 .suspend = olpc_battery_suspend,
730 module_platform_driver(olpc_battery_driver);
732 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
733 MODULE_LICENSE("GPL");
734 MODULE_DESCRIPTION("Battery driver for One Laptop Per Child 'XO' machine");