2 * Battery driver for One Laptop Per Child board.
4 * Copyright © 2006 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/err.h>
14 #include <linux/platform_device.h>
15 #include <linux/power_supply.h>
16 #include <linux/jiffies.h>
17 #include <linux/sched.h>
21 #define EC_BAT_VOLTAGE 0x10 /* uint16_t, *9.76/32, mV */
22 #define EC_BAT_CURRENT 0x11 /* int16_t, *15.625/120, mA */
23 #define EC_BAT_ACR 0x12 /* int16_t, *6250/15, µAh */
24 #define EC_BAT_TEMP 0x13 /* uint16_t, *100/256, °C */
25 #define EC_AMB_TEMP 0x14 /* uint16_t, *100/256, °C */
26 #define EC_BAT_STATUS 0x15 /* uint8_t, bitmask */
27 #define EC_BAT_SOC 0x16 /* uint8_t, percentage */
28 #define EC_BAT_SERIAL 0x17 /* uint8_t[6] */
29 #define EC_BAT_EEPROM 0x18 /* uint8_t adr as input, uint8_t output */
30 #define EC_BAT_ERRCODE 0x1f /* uint8_t, bitmask */
32 #define BAT_STAT_PRESENT 0x01
33 #define BAT_STAT_FULL 0x02
34 #define BAT_STAT_LOW 0x04
35 #define BAT_STAT_DESTROY 0x08
36 #define BAT_STAT_AC 0x10
37 #define BAT_STAT_CHARGING 0x20
38 #define BAT_STAT_DISCHARGING 0x40
39 #define BAT_STAT_TRICKLE 0x80
41 #define BAT_ERR_INFOFAIL 0x02
42 #define BAT_ERR_OVERVOLTAGE 0x04
43 #define BAT_ERR_OVERTEMP 0x05
44 #define BAT_ERR_GAUGESTOP 0x06
45 #define BAT_ERR_OUT_OF_CONTROL 0x07
46 #define BAT_ERR_ID_FAIL 0x09
47 #define BAT_ERR_ACR_FAIL 0x10
49 #define BAT_ADDR_MFR_TYPE 0x5F
51 /*********************************************************************
53 *********************************************************************/
55 static int olpc_ac_get_prop(struct power_supply
*psy
,
56 enum power_supply_property psp
,
57 union power_supply_propval
*val
)
63 case POWER_SUPPLY_PROP_ONLINE
:
64 ret
= olpc_ec_cmd(EC_BAT_STATUS
, NULL
, 0, &status
, 1);
68 val
->intval
= !!(status
& BAT_STAT_AC
);
77 static enum power_supply_property olpc_ac_props
[] = {
78 POWER_SUPPLY_PROP_ONLINE
,
81 static struct power_supply olpc_ac
= {
83 .type
= POWER_SUPPLY_TYPE_MAINS
,
84 .properties
= olpc_ac_props
,
85 .num_properties
= ARRAY_SIZE(olpc_ac_props
),
86 .get_property
= olpc_ac_get_prop
,
89 static char bat_serial
[17]; /* Ick */
91 static int olpc_bat_get_status(union power_supply_propval
*val
, uint8_t ec_byte
)
93 if (olpc_platform_info
.ecver
> 0x44) {
94 if (ec_byte
& (BAT_STAT_CHARGING
| BAT_STAT_TRICKLE
))
95 val
->intval
= POWER_SUPPLY_STATUS_CHARGING
;
96 else if (ec_byte
& BAT_STAT_DISCHARGING
)
97 val
->intval
= POWER_SUPPLY_STATUS_DISCHARGING
;
98 else if (ec_byte
& BAT_STAT_FULL
)
99 val
->intval
= POWER_SUPPLY_STATUS_FULL
;
101 val
->intval
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
103 /* Older EC didn't report charge/discharge bits */
104 if (!(ec_byte
& BAT_STAT_AC
)) /* No AC means discharging */
105 val
->intval
= POWER_SUPPLY_STATUS_DISCHARGING
;
106 else if (ec_byte
& BAT_STAT_FULL
)
107 val
->intval
= POWER_SUPPLY_STATUS_FULL
;
108 else /* Not _necessarily_ true but EC doesn't tell all yet */
109 val
->intval
= POWER_SUPPLY_STATUS_CHARGING
;
115 static int olpc_bat_get_health(union power_supply_propval
*val
)
120 ret
= olpc_ec_cmd(EC_BAT_ERRCODE
, NULL
, 0, &ec_byte
, 1);
126 val
->intval
= POWER_SUPPLY_HEALTH_GOOD
;
129 case BAT_ERR_OVERTEMP
:
130 val
->intval
= POWER_SUPPLY_HEALTH_OVERHEAT
;
133 case BAT_ERR_OVERVOLTAGE
:
134 val
->intval
= POWER_SUPPLY_HEALTH_OVERVOLTAGE
;
137 case BAT_ERR_INFOFAIL
:
138 case BAT_ERR_OUT_OF_CONTROL
:
139 case BAT_ERR_ID_FAIL
:
140 case BAT_ERR_ACR_FAIL
:
141 val
->intval
= POWER_SUPPLY_HEALTH_UNSPEC_FAILURE
;
145 /* Eep. We don't know this failure code */
152 static int olpc_bat_get_mfr(union power_supply_propval
*val
)
157 ec_byte
= BAT_ADDR_MFR_TYPE
;
158 ret
= olpc_ec_cmd(EC_BAT_EEPROM
, &ec_byte
, 1, &ec_byte
, 1);
162 switch (ec_byte
>> 4) {
164 val
->strval
= "Gold Peak";
170 val
->strval
= "Unknown";
177 static int olpc_bat_get_tech(union power_supply_propval
*val
)
182 ec_byte
= BAT_ADDR_MFR_TYPE
;
183 ret
= olpc_ec_cmd(EC_BAT_EEPROM
, &ec_byte
, 1, &ec_byte
, 1);
187 switch (ec_byte
& 0xf) {
189 val
->intval
= POWER_SUPPLY_TECHNOLOGY_NiMH
;
192 val
->intval
= POWER_SUPPLY_TECHNOLOGY_LiFe
;
195 val
->intval
= POWER_SUPPLY_TECHNOLOGY_UNKNOWN
;
202 /*********************************************************************
204 *********************************************************************/
205 static int olpc_bat_get_property(struct power_supply
*psy
,
206 enum power_supply_property psp
,
207 union power_supply_propval
*val
)
214 ret
= olpc_ec_cmd(EC_BAT_STATUS
, NULL
, 0, &ec_byte
, 1);
218 /* Theoretically there's a race here -- the battery could be
219 removed immediately after we check whether it's present, and
220 then we query for some other property of the now-absent battery.
221 It doesn't matter though -- the EC will return the last-known
222 information, and it's as if we just ran that _little_ bit faster
223 and managed to read it out before the battery went away. */
224 if (!(ec_byte
& (BAT_STAT_PRESENT
| BAT_STAT_TRICKLE
)) &&
225 psp
!= POWER_SUPPLY_PROP_PRESENT
)
229 case POWER_SUPPLY_PROP_STATUS
:
230 ret
= olpc_bat_get_status(val
, ec_byte
);
234 case POWER_SUPPLY_PROP_PRESENT
:
235 val
->intval
= !!(ec_byte
& (BAT_STAT_PRESENT
|
239 case POWER_SUPPLY_PROP_HEALTH
:
240 if (ec_byte
& BAT_STAT_DESTROY
)
241 val
->intval
= POWER_SUPPLY_HEALTH_DEAD
;
243 ret
= olpc_bat_get_health(val
);
249 case POWER_SUPPLY_PROP_MANUFACTURER
:
250 ret
= olpc_bat_get_mfr(val
);
254 case POWER_SUPPLY_PROP_TECHNOLOGY
:
255 ret
= olpc_bat_get_tech(val
);
259 case POWER_SUPPLY_PROP_VOLTAGE_AVG
:
260 ret
= olpc_ec_cmd(EC_BAT_VOLTAGE
, NULL
, 0, (void *)&ec_word
, 2);
264 val
->intval
= (int)be16_to_cpu(ec_word
) * 9760L / 32;
266 case POWER_SUPPLY_PROP_CURRENT_AVG
:
267 ret
= olpc_ec_cmd(EC_BAT_CURRENT
, NULL
, 0, (void *)&ec_word
, 2);
271 val
->intval
= (int)be16_to_cpu(ec_word
) * 15625L / 120;
273 case POWER_SUPPLY_PROP_CAPACITY
:
274 ret
= olpc_ec_cmd(EC_BAT_SOC
, NULL
, 0, &ec_byte
, 1);
277 val
->intval
= ec_byte
;
279 case POWER_SUPPLY_PROP_TEMP
:
280 ret
= olpc_ec_cmd(EC_BAT_TEMP
, NULL
, 0, (void *)&ec_word
, 2);
284 val
->intval
= (int)be16_to_cpu(ec_word
) * 100 / 256;
286 case POWER_SUPPLY_PROP_TEMP_AMBIENT
:
287 ret
= olpc_ec_cmd(EC_AMB_TEMP
, NULL
, 0, (void *)&ec_word
, 2);
291 val
->intval
= (int)be16_to_cpu(ec_word
) * 100 / 256;
293 case POWER_SUPPLY_PROP_CHARGE_COUNTER
:
294 ret
= olpc_ec_cmd(EC_BAT_ACR
, NULL
, 0, (void *)&ec_word
, 2);
298 val
->intval
= (int)be16_to_cpu(ec_word
) * 6250 / 15;
300 case POWER_SUPPLY_PROP_SERIAL_NUMBER
:
301 ret
= olpc_ec_cmd(EC_BAT_SERIAL
, NULL
, 0, (void *)&ser_buf
, 8);
305 sprintf(bat_serial
, "%016llx", (long long)be64_to_cpu(ser_buf
));
306 val
->strval
= bat_serial
;
316 static enum power_supply_property olpc_bat_props
[] = {
317 POWER_SUPPLY_PROP_STATUS
,
318 POWER_SUPPLY_PROP_PRESENT
,
319 POWER_SUPPLY_PROP_HEALTH
,
320 POWER_SUPPLY_PROP_TECHNOLOGY
,
321 POWER_SUPPLY_PROP_VOLTAGE_AVG
,
322 POWER_SUPPLY_PROP_CURRENT_AVG
,
323 POWER_SUPPLY_PROP_CAPACITY
,
324 POWER_SUPPLY_PROP_TEMP
,
325 POWER_SUPPLY_PROP_TEMP_AMBIENT
,
326 POWER_SUPPLY_PROP_MANUFACTURER
,
327 POWER_SUPPLY_PROP_SERIAL_NUMBER
,
328 POWER_SUPPLY_PROP_CHARGE_COUNTER
,
331 /* EEPROM reading goes completely around the power_supply API, sadly */
333 #define EEPROM_START 0x20
334 #define EEPROM_END 0x80
335 #define EEPROM_SIZE (EEPROM_END - EEPROM_START)
337 static ssize_t
olpc_bat_eeprom_read(struct kobject
*kobj
,
338 struct bin_attribute
*attr
, char *buf
, loff_t off
, size_t count
)
344 if (off
>= EEPROM_SIZE
)
346 if (off
+ count
> EEPROM_SIZE
)
347 count
= EEPROM_SIZE
- off
;
349 for (i
= 0; i
< count
; i
++) {
350 ec_byte
= EEPROM_START
+ off
+ i
;
351 ret
= olpc_ec_cmd(EC_BAT_EEPROM
, &ec_byte
, 1, &buf
[i
], 1);
353 pr_err("olpc-battery: "
354 "EC_BAT_EEPROM cmd @ 0x%x failed - %d!\n",
363 static struct bin_attribute olpc_bat_eeprom
= {
367 .owner
= THIS_MODULE
,
370 .read
= olpc_bat_eeprom_read
,
373 /*********************************************************************
375 *********************************************************************/
377 static struct platform_device
*bat_pdev
;
379 static struct power_supply olpc_bat
= {
380 .properties
= olpc_bat_props
,
381 .num_properties
= ARRAY_SIZE(olpc_bat_props
),
382 .get_property
= olpc_bat_get_property
,
386 void olpc_battery_trigger_uevent(unsigned long cause
)
388 if (cause
& EC_SCI_SRC_ACPWR
)
389 kobject_uevent(&olpc_ac
.dev
->kobj
, KOBJ_CHANGE
);
390 if (cause
& (EC_SCI_SRC_BATERR
|EC_SCI_SRC_BATSOC
|EC_SCI_SRC_BATTERY
))
391 kobject_uevent(&olpc_bat
.dev
->kobj
, KOBJ_CHANGE
);
394 static int __init
olpc_bat_init(void)
399 if (!olpc_platform_info
.ecver
)
403 * We've seen a number of EC protocol changes; this driver requires
404 * the latest EC protocol, supported by 0x44 and above.
406 if (olpc_platform_info
.ecver
< 0x44) {
407 printk(KERN_NOTICE
"OLPC EC version 0x%02x too old for "
408 "battery driver.\n", olpc_platform_info
.ecver
);
412 ret
= olpc_ec_cmd(EC_BAT_STATUS
, NULL
, 0, &status
, 1);
416 /* Ignore the status. It doesn't actually matter */
418 bat_pdev
= platform_device_register_simple("olpc-battery", 0, NULL
, 0);
419 if (IS_ERR(bat_pdev
))
420 return PTR_ERR(bat_pdev
);
422 ret
= power_supply_register(&bat_pdev
->dev
, &olpc_ac
);
426 olpc_bat
.name
= bat_pdev
->name
;
428 ret
= power_supply_register(&bat_pdev
->dev
, &olpc_bat
);
432 ret
= device_create_bin_file(olpc_bat
.dev
, &olpc_bat_eeprom
);
439 power_supply_unregister(&olpc_bat
);
441 power_supply_unregister(&olpc_ac
);
443 platform_device_unregister(bat_pdev
);
448 static void __exit
olpc_bat_exit(void)
450 device_remove_bin_file(olpc_bat
.dev
, &olpc_bat_eeprom
);
451 power_supply_unregister(&olpc_bat
);
452 power_supply_unregister(&olpc_ac
);
453 platform_device_unregister(bat_pdev
);
456 module_init(olpc_bat_init
);
457 module_exit(olpc_bat_exit
);
459 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
460 MODULE_LICENSE("GPL");
461 MODULE_DESCRIPTION("Battery driver for One Laptop Per Child 'XO' machine");