1 // SPDX-License-Identifier: GPL-2.0-only
3 * Battery driver for One Laptop Per Child board.
5 * Copyright © 2006-2010 David Woodhouse <dwmw2@infradead.org>
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/mod_devicetable.h>
11 #include <linux/types.h>
12 #include <linux/err.h>
13 #include <linux/device.h>
15 #include <linux/platform_device.h>
16 #include <linux/power_supply.h>
17 #include <linux/jiffies.h>
18 #include <linux/sched.h>
19 #include <linux/olpc-ec.h>
23 #define EC_BAT_VOLTAGE 0x10 /* uint16_t, *9.76/32, mV */
24 #define EC_BAT_CURRENT 0x11 /* int16_t, *15.625/120, mA */
25 #define EC_BAT_ACR 0x12 /* int16_t, *6250/15, µAh */
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 */
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
41 #define BAT_STAT_TRICKLE 0x80
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
51 #define BAT_ADDR_MFR_TYPE 0x5F
53 struct olpc_battery_data {
54 struct power_supply *olpc_ac;
55 struct power_supply *olpc_bat;
61 /*********************************************************************
63 *********************************************************************/
65 static int olpc_ac_get_prop(struct power_supply *psy,
66 enum power_supply_property psp,
67 union power_supply_propval *val)
73 case POWER_SUPPLY_PROP_ONLINE:
74 ret = olpc_ec_cmd(EC_BAT_STATUS, NULL, 0, &status, 1);
78 val->intval = !!(status & BAT_STAT_AC);
87 static enum power_supply_property olpc_ac_props[] = {
88 POWER_SUPPLY_PROP_ONLINE,
91 static const struct power_supply_desc olpc_ac_desc = {
93 .type = POWER_SUPPLY_TYPE_MAINS,
94 .properties = olpc_ac_props,
95 .num_properties = ARRAY_SIZE(olpc_ac_props),
96 .get_property = olpc_ac_get_prop,
99 static int olpc_bat_get_status(struct olpc_battery_data *data,
100 union power_supply_propval *val, uint8_t ec_byte)
102 if (data->new_proto) {
103 if (ec_byte & (BAT_STAT_CHARGING | BAT_STAT_TRICKLE))
104 val->intval = POWER_SUPPLY_STATUS_CHARGING;
105 else if (ec_byte & BAT_STAT_DISCHARGING)
106 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
107 else if (ec_byte & BAT_STAT_FULL)
108 val->intval = POWER_SUPPLY_STATUS_FULL;
110 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
112 /* Older EC didn't report charge/discharge bits */
113 if (!(ec_byte & BAT_STAT_AC)) /* No AC means discharging */
114 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
115 else if (ec_byte & BAT_STAT_FULL)
116 val->intval = POWER_SUPPLY_STATUS_FULL;
117 else /* Not _necessarily_ true but EC doesn't tell all yet */
118 val->intval = POWER_SUPPLY_STATUS_CHARGING;
124 static int olpc_bat_get_health(union power_supply_propval *val)
129 ret = olpc_ec_cmd(EC_BAT_ERRCODE, NULL, 0, &ec_byte, 1);
135 val->intval = POWER_SUPPLY_HEALTH_GOOD;
138 case BAT_ERR_OVERTEMP:
139 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
142 case BAT_ERR_OVERVOLTAGE:
143 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
146 case BAT_ERR_INFOFAIL:
147 case BAT_ERR_OUT_OF_CONTROL:
148 case BAT_ERR_ID_FAIL:
149 case BAT_ERR_ACR_FAIL:
150 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
154 /* Eep. We don't know this failure code */
161 static int olpc_bat_get_mfr(union power_supply_propval *val)
166 ec_byte = BAT_ADDR_MFR_TYPE;
167 ret = olpc_ec_cmd(EC_BAT_EEPROM, &ec_byte, 1, &ec_byte, 1);
171 switch (ec_byte >> 4) {
173 val->strval = "Gold Peak";
179 val->strval = "Unknown";
186 static int olpc_bat_get_tech(union power_supply_propval *val)
191 ec_byte = BAT_ADDR_MFR_TYPE;
192 ret = olpc_ec_cmd(EC_BAT_EEPROM, &ec_byte, 1, &ec_byte, 1);
196 switch (ec_byte & 0xf) {
198 val->intval = POWER_SUPPLY_TECHNOLOGY_NiMH;
201 val->intval = POWER_SUPPLY_TECHNOLOGY_LiFe;
204 val->intval = POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
211 static int olpc_bat_get_charge_full_design(union power_supply_propval *val)
214 union power_supply_propval tech;
217 ret = olpc_bat_get_tech(&tech);
221 ec_byte = BAT_ADDR_MFR_TYPE;
222 ret = olpc_ec_cmd(EC_BAT_EEPROM, &ec_byte, 1, &ec_byte, 1);
228 switch (tech.intval) {
229 case POWER_SUPPLY_TECHNOLOGY_NiMH:
231 case 1: /* Gold Peak */
232 val->intval = 3000000*.8;
239 case POWER_SUPPLY_TECHNOLOGY_LiFe:
241 case 1: /* Gold Peak, fall through */
243 val->intval = 2800000;
257 static int olpc_bat_get_charge_now(union power_supply_propval *val)
260 union power_supply_propval full;
263 ret = olpc_ec_cmd(EC_BAT_SOC, NULL, 0, &soc, 1);
267 ret = olpc_bat_get_charge_full_design(&full);
271 val->intval = soc * (full.intval / 100);
275 static int olpc_bat_get_voltage_max_design(union power_supply_propval *val)
278 union power_supply_propval tech;
282 ret = olpc_bat_get_tech(&tech);
286 ec_byte = BAT_ADDR_MFR_TYPE;
287 ret = olpc_ec_cmd(EC_BAT_EEPROM, &ec_byte, 1, &ec_byte, 1);
293 switch (tech.intval) {
294 case POWER_SUPPLY_TECHNOLOGY_NiMH:
296 case 1: /* Gold Peak */
297 val->intval = 6000000;
304 case POWER_SUPPLY_TECHNOLOGY_LiFe:
306 case 1: /* Gold Peak */
307 val->intval = 6400000;
310 val->intval = 6500000;
324 static u16 ecword_to_cpu(struct olpc_battery_data *data, u16 ec_word)
326 if (data->little_endian)
327 return le16_to_cpu((__force __le16)ec_word);
329 return be16_to_cpu((__force __be16)ec_word);
332 /*********************************************************************
334 *********************************************************************/
335 static int olpc_bat_get_property(struct power_supply *psy,
336 enum power_supply_property psp,
337 union power_supply_propval *val)
339 struct olpc_battery_data *data = power_supply_get_drvdata(psy);
345 ret = olpc_ec_cmd(EC_BAT_STATUS, NULL, 0, &ec_byte, 1);
349 /* Theoretically there's a race here -- the battery could be
350 removed immediately after we check whether it's present, and
351 then we query for some other property of the now-absent battery.
352 It doesn't matter though -- the EC will return the last-known
353 information, and it's as if we just ran that _little_ bit faster
354 and managed to read it out before the battery went away. */
355 if (!(ec_byte & (BAT_STAT_PRESENT | BAT_STAT_TRICKLE)) &&
356 psp != POWER_SUPPLY_PROP_PRESENT)
360 case POWER_SUPPLY_PROP_STATUS:
361 ret = olpc_bat_get_status(data, val, ec_byte);
365 case POWER_SUPPLY_PROP_CHARGE_TYPE:
366 if (ec_byte & BAT_STAT_TRICKLE)
367 val->intval = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
368 else if (ec_byte & BAT_STAT_CHARGING)
369 val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
371 val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;
373 case POWER_SUPPLY_PROP_PRESENT:
374 val->intval = !!(ec_byte & (BAT_STAT_PRESENT |
378 case POWER_SUPPLY_PROP_HEALTH:
379 if (ec_byte & BAT_STAT_DESTROY)
380 val->intval = POWER_SUPPLY_HEALTH_DEAD;
382 ret = olpc_bat_get_health(val);
388 case POWER_SUPPLY_PROP_MANUFACTURER:
389 ret = olpc_bat_get_mfr(val);
393 case POWER_SUPPLY_PROP_TECHNOLOGY:
394 ret = olpc_bat_get_tech(val);
398 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
399 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
400 ret = olpc_ec_cmd(EC_BAT_VOLTAGE, NULL, 0, (void *)&ec_word, 2);
404 val->intval = ecword_to_cpu(data, ec_word) * 9760L / 32;
406 case POWER_SUPPLY_PROP_CURRENT_AVG:
407 case POWER_SUPPLY_PROP_CURRENT_NOW:
408 ret = olpc_ec_cmd(EC_BAT_CURRENT, NULL, 0, (void *)&ec_word, 2);
412 val->intval = ecword_to_cpu(data, ec_word) * 15625L / 120;
414 case POWER_SUPPLY_PROP_CAPACITY:
415 ret = olpc_ec_cmd(EC_BAT_SOC, NULL, 0, &ec_byte, 1);
418 val->intval = ec_byte;
420 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
421 if (ec_byte & BAT_STAT_FULL)
422 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
423 else if (ec_byte & BAT_STAT_LOW)
424 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
426 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
428 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
429 ret = olpc_bat_get_charge_full_design(val);
433 case POWER_SUPPLY_PROP_CHARGE_NOW:
434 ret = olpc_bat_get_charge_now(val);
438 case POWER_SUPPLY_PROP_TEMP:
439 ret = olpc_ec_cmd(EC_BAT_TEMP, NULL, 0, (void *)&ec_word, 2);
443 val->intval = ecword_to_cpu(data, ec_word) * 10 / 256;
445 case POWER_SUPPLY_PROP_TEMP_AMBIENT:
446 ret = olpc_ec_cmd(EC_AMB_TEMP, NULL, 0, (void *)&ec_word, 2);
450 val->intval = (int)ecword_to_cpu(data, ec_word) * 10 / 256;
452 case POWER_SUPPLY_PROP_CHARGE_COUNTER:
453 ret = olpc_ec_cmd(EC_BAT_ACR, NULL, 0, (void *)&ec_word, 2);
457 val->intval = ecword_to_cpu(data, ec_word) * 6250 / 15;
459 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
460 ret = olpc_ec_cmd(EC_BAT_SERIAL, NULL, 0, (void *)&ser_buf, 8);
464 sprintf(data->bat_serial, "%016llx", (long long)be64_to_cpu(ser_buf));
465 val->strval = data->bat_serial;
467 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
468 ret = olpc_bat_get_voltage_max_design(val);
480 static enum power_supply_property olpc_xo1_bat_props[] = {
481 POWER_SUPPLY_PROP_STATUS,
482 POWER_SUPPLY_PROP_CHARGE_TYPE,
483 POWER_SUPPLY_PROP_PRESENT,
484 POWER_SUPPLY_PROP_HEALTH,
485 POWER_SUPPLY_PROP_TECHNOLOGY,
486 POWER_SUPPLY_PROP_VOLTAGE_AVG,
487 POWER_SUPPLY_PROP_VOLTAGE_NOW,
488 POWER_SUPPLY_PROP_CURRENT_AVG,
489 POWER_SUPPLY_PROP_CURRENT_NOW,
490 POWER_SUPPLY_PROP_CAPACITY,
491 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
492 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
493 POWER_SUPPLY_PROP_CHARGE_NOW,
494 POWER_SUPPLY_PROP_TEMP,
495 POWER_SUPPLY_PROP_TEMP_AMBIENT,
496 POWER_SUPPLY_PROP_MANUFACTURER,
497 POWER_SUPPLY_PROP_SERIAL_NUMBER,
498 POWER_SUPPLY_PROP_CHARGE_COUNTER,
499 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
502 /* XO-1.5 does not have ambient temperature property */
503 static enum power_supply_property olpc_xo15_bat_props[] = {
504 POWER_SUPPLY_PROP_STATUS,
505 POWER_SUPPLY_PROP_CHARGE_TYPE,
506 POWER_SUPPLY_PROP_PRESENT,
507 POWER_SUPPLY_PROP_HEALTH,
508 POWER_SUPPLY_PROP_TECHNOLOGY,
509 POWER_SUPPLY_PROP_VOLTAGE_AVG,
510 POWER_SUPPLY_PROP_VOLTAGE_NOW,
511 POWER_SUPPLY_PROP_CURRENT_AVG,
512 POWER_SUPPLY_PROP_CURRENT_NOW,
513 POWER_SUPPLY_PROP_CAPACITY,
514 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
515 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
516 POWER_SUPPLY_PROP_CHARGE_NOW,
517 POWER_SUPPLY_PROP_TEMP,
518 POWER_SUPPLY_PROP_MANUFACTURER,
519 POWER_SUPPLY_PROP_SERIAL_NUMBER,
520 POWER_SUPPLY_PROP_CHARGE_COUNTER,
521 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
524 /* EEPROM reading goes completely around the power_supply API, sadly */
526 #define EEPROM_START 0x20
527 #define EEPROM_END 0x80
528 #define EEPROM_SIZE (EEPROM_END - EEPROM_START)
530 static ssize_t olpc_bat_eeprom_read(struct file *filp, struct kobject *kobj,
531 struct bin_attribute *attr, char *buf, loff_t off, size_t count)
537 for (i = 0; i < count; i++) {
538 ec_byte = EEPROM_START + off + i;
539 ret = olpc_ec_cmd(EC_BAT_EEPROM, &ec_byte, 1, &buf[i], 1);
541 pr_err("olpc-battery: "
542 "EC_BAT_EEPROM cmd @ 0x%x failed - %d!\n",
551 static struct bin_attribute olpc_bat_eeprom = {
557 .read = olpc_bat_eeprom_read,
560 /* Allow userspace to see the specific error value pulled from the EC */
562 static ssize_t olpc_bat_error_read(struct device *dev,
563 struct device_attribute *attr, char *buf)
568 ret = olpc_ec_cmd(EC_BAT_ERRCODE, NULL, 0, &ec_byte, 1);
572 return sprintf(buf, "%d\n", ec_byte);
575 static struct device_attribute olpc_bat_error = {
580 .show = olpc_bat_error_read,
583 static struct attribute *olpc_bat_sysfs_attrs[] = {
584 &olpc_bat_error.attr,
588 static struct bin_attribute *olpc_bat_sysfs_bin_attrs[] = {
593 static const struct attribute_group olpc_bat_sysfs_group = {
594 .attrs = olpc_bat_sysfs_attrs,
595 .bin_attrs = olpc_bat_sysfs_bin_attrs,
599 static const struct attribute_group *olpc_bat_sysfs_groups[] = {
600 &olpc_bat_sysfs_group,
604 /*********************************************************************
606 *********************************************************************/
608 static struct power_supply_desc olpc_bat_desc = {
609 .name = "olpc-battery",
610 .get_property = olpc_bat_get_property,
614 static int olpc_battery_suspend(struct platform_device *pdev,
617 struct olpc_battery_data *data = platform_get_drvdata(pdev);
619 if (device_may_wakeup(&data->olpc_ac->dev))
620 olpc_ec_wakeup_set(EC_SCI_SRC_ACPWR);
622 olpc_ec_wakeup_clear(EC_SCI_SRC_ACPWR);
624 if (device_may_wakeup(&data->olpc_bat->dev))
625 olpc_ec_wakeup_set(EC_SCI_SRC_BATTERY | EC_SCI_SRC_BATSOC
626 | EC_SCI_SRC_BATERR);
628 olpc_ec_wakeup_clear(EC_SCI_SRC_BATTERY | EC_SCI_SRC_BATSOC
629 | EC_SCI_SRC_BATERR);
634 static int olpc_battery_probe(struct platform_device *pdev)
636 struct power_supply_config bat_psy_cfg = {};
637 struct power_supply_config ac_psy_cfg = {};
638 struct olpc_battery_data *data;
643 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
646 platform_set_drvdata(pdev, data);
648 /* See if the EC is already there and get the EC revision */
649 ret = olpc_ec_cmd(EC_FIRMWARE_REV, NULL, 0, &ecver, 1);
653 if (of_find_compatible_node(NULL, NULL, "olpc,xo1.75-ec")) {
655 data->new_proto = true;
656 data->little_endian = true;
657 } else if (ecver > 0x44) {
658 /* XO 1 or 1.5 with a new EC firmware. */
659 data->new_proto = true;
660 } else if (ecver < 0x44) {
662 * We've seen a number of EC protocol changes; this driver
663 * requires the latest EC protocol, supported by 0x44 and above.
665 printk(KERN_NOTICE "OLPC EC version 0x%02x too old for "
666 "battery driver.\n", ecver);
670 ret = olpc_ec_cmd(EC_BAT_STATUS, NULL, 0, &status, 1);
674 /* Ignore the status. It doesn't actually matter */
676 ac_psy_cfg.of_node = pdev->dev.of_node;
677 ac_psy_cfg.drv_data = data;
679 data->olpc_ac = devm_power_supply_register(&pdev->dev, &olpc_ac_desc,
681 if (IS_ERR(data->olpc_ac))
682 return PTR_ERR(data->olpc_ac);
684 if (of_device_is_compatible(pdev->dev.of_node, "olpc,xo1.5-battery")) {
686 olpc_bat_desc.properties = olpc_xo15_bat_props;
687 olpc_bat_desc.num_properties = ARRAY_SIZE(olpc_xo15_bat_props);
690 olpc_bat_desc.properties = olpc_xo1_bat_props;
691 olpc_bat_desc.num_properties = ARRAY_SIZE(olpc_xo1_bat_props);
694 bat_psy_cfg.of_node = pdev->dev.of_node;
695 bat_psy_cfg.drv_data = data;
696 bat_psy_cfg.attr_grp = olpc_bat_sysfs_groups;
698 data->olpc_bat = devm_power_supply_register(&pdev->dev, &olpc_bat_desc,
700 if (IS_ERR(data->olpc_bat))
701 return PTR_ERR(data->olpc_bat);
703 if (olpc_ec_wakeup_available()) {
704 device_set_wakeup_capable(&data->olpc_ac->dev, true);
705 device_set_wakeup_capable(&data->olpc_bat->dev, true);
711 static const struct of_device_id olpc_battery_ids[] = {
712 { .compatible = "olpc,xo1-battery" },
713 { .compatible = "olpc,xo1.5-battery" },
716 MODULE_DEVICE_TABLE(of, olpc_battery_ids);
718 static struct platform_driver olpc_battery_driver = {
720 .name = "olpc-battery",
721 .of_match_table = olpc_battery_ids,
723 .probe = olpc_battery_probe,
724 .suspend = olpc_battery_suspend,
727 module_platform_driver(olpc_battery_driver);
729 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
730 MODULE_LICENSE("GPL");
731 MODULE_DESCRIPTION("Battery driver for One Laptop Per Child 'XO' machine");