blob: 602bbd008f7840bbeb6ce58e3009701c64bb1cc2 [file] [log] [blame]
David Woodhousefb972872007-05-04 00:51:18 +04001/*
2 * Battery driver for One Laptop Per Child board.
3 *
4 * Copyright © 2006 David Woodhouse <dwmw2@infradead.org>
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
Andres Salomon04a820e2009-06-30 02:14:00 -040011#include <linux/kernel.h>
David Woodhousefb972872007-05-04 00:51:18 +040012#include <linux/module.h>
Andres Salomon144bbea2009-06-30 02:15:26 -040013#include <linux/types.h>
David Woodhousefb972872007-05-04 00:51:18 +040014#include <linux/err.h>
Andres Salomon144bbea2009-06-30 02:15:26 -040015#include <linux/device.h>
David Woodhousefb972872007-05-04 00:51:18 +040016#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 */
Andres Salomon75d88072008-05-14 16:20:38 -070025#define EC_BAT_ACR 0x12 /* int16_t, *6250/15, µAh */
David Woodhousefb972872007-05-04 00:51:18 +040026#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
Andres Salomon8f7e5792009-06-30 02:16:17 -040041#define BAT_STAT_TRICKLE 0x80
David Woodhousefb972872007-05-04 00:51:18 +040042
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
David Woodhouse1ca5b9d2008-05-04 01:31:42 -040091static char bat_serial[17]; /* Ick */
92
Andres Salomonb2bd8a32008-05-02 13:41:59 -070093static int olpc_bat_get_status(union power_supply_propval *val, uint8_t ec_byte)
94{
95 if (olpc_platform_info.ecver > 0x44) {
Andres Salomon8f7e5792009-06-30 02:16:17 -040096 if (ec_byte & (BAT_STAT_CHARGING | BAT_STAT_TRICKLE))
Andres Salomonb2bd8a32008-05-02 13:41:59 -070097 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
David Woodhousefb972872007-05-04 00:51:18 +0400204/*********************************************************************
205 * Battery properties
206 *********************************************************************/
207static int olpc_bat_get_property(struct power_supply *psy,
208 enum power_supply_property psp,
209 union power_supply_propval *val)
210{
211 int ret = 0;
Harvey Harrison8e9c7712008-10-15 22:01:23 -0700212 __be16 ec_word;
David Woodhousefb972872007-05-04 00:51:18 +0400213 uint8_t ec_byte;
Harvey Harrison8e9c7712008-10-15 22:01:23 -0700214 __be64 ser_buf;
David Woodhousefb972872007-05-04 00:51:18 +0400215
216 ret = olpc_ec_cmd(EC_BAT_STATUS, NULL, 0, &ec_byte, 1);
217 if (ret)
218 return ret;
219
220 /* Theoretically there's a race here -- the battery could be
221 removed immediately after we check whether it's present, and
222 then we query for some other property of the now-absent battery.
223 It doesn't matter though -- the EC will return the last-known
224 information, and it's as if we just ran that _little_ bit faster
225 and managed to read it out before the battery went away. */
Andres Salomon8f7e5792009-06-30 02:16:17 -0400226 if (!(ec_byte & (BAT_STAT_PRESENT | BAT_STAT_TRICKLE)) &&
227 psp != POWER_SUPPLY_PROP_PRESENT)
David Woodhousefb972872007-05-04 00:51:18 +0400228 return -ENODEV;
229
230 switch (psp) {
231 case POWER_SUPPLY_PROP_STATUS:
Andres Salomonb2bd8a32008-05-02 13:41:59 -0700232 ret = olpc_bat_get_status(val, ec_byte);
233 if (ret)
234 return ret;
235 break;
David Woodhousefb972872007-05-04 00:51:18 +0400236 case POWER_SUPPLY_PROP_PRESENT:
Andres Salomon8f7e5792009-06-30 02:16:17 -0400237 val->intval = !!(ec_byte & (BAT_STAT_PRESENT |
238 BAT_STAT_TRICKLE));
David Woodhousefb972872007-05-04 00:51:18 +0400239 break;
240
241 case POWER_SUPPLY_PROP_HEALTH:
242 if (ec_byte & BAT_STAT_DESTROY)
243 val->intval = POWER_SUPPLY_HEALTH_DEAD;
244 else {
Andres Salomonb2bd8a32008-05-02 13:41:59 -0700245 ret = olpc_bat_get_health(val);
David Woodhousefb972872007-05-04 00:51:18 +0400246 if (ret)
247 return ret;
David Woodhousefb972872007-05-04 00:51:18 +0400248 }
249 break;
250
251 case POWER_SUPPLY_PROP_MANUFACTURER:
Andres Salomonb2bd8a32008-05-02 13:41:59 -0700252 ret = olpc_bat_get_mfr(val);
David Woodhousefb972872007-05-04 00:51:18 +0400253 if (ret)
254 return ret;
David Woodhousefb972872007-05-04 00:51:18 +0400255 break;
256 case POWER_SUPPLY_PROP_TECHNOLOGY:
Andres Salomonb2bd8a32008-05-02 13:41:59 -0700257 ret = olpc_bat_get_tech(val);
David Woodhousefb972872007-05-04 00:51:18 +0400258 if (ret)
259 return ret;
David Woodhousefb972872007-05-04 00:51:18 +0400260 break;
261 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
262 ret = olpc_ec_cmd(EC_BAT_VOLTAGE, NULL, 0, (void *)&ec_word, 2);
263 if (ret)
264 return ret;
265
Harvey Harrison8e9c7712008-10-15 22:01:23 -0700266 val->intval = (int)be16_to_cpu(ec_word) * 9760L / 32;
David Woodhousefb972872007-05-04 00:51:18 +0400267 break;
268 case POWER_SUPPLY_PROP_CURRENT_AVG:
269 ret = olpc_ec_cmd(EC_BAT_CURRENT, NULL, 0, (void *)&ec_word, 2);
270 if (ret)
271 return ret;
272
Harvey Harrison8e9c7712008-10-15 22:01:23 -0700273 val->intval = (int)be16_to_cpu(ec_word) * 15625L / 120;
David Woodhousefb972872007-05-04 00:51:18 +0400274 break;
275 case POWER_SUPPLY_PROP_CAPACITY:
276 ret = olpc_ec_cmd(EC_BAT_SOC, NULL, 0, &ec_byte, 1);
277 if (ret)
278 return ret;
279 val->intval = ec_byte;
280 break;
Andres Salomonb294a292009-06-30 02:13:01 -0400281 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
282 if (ec_byte & BAT_STAT_FULL)
283 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
284 else if (ec_byte & BAT_STAT_LOW)
285 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
286 else
287 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
288 break;
David Woodhousefb972872007-05-04 00:51:18 +0400289 case POWER_SUPPLY_PROP_TEMP:
290 ret = olpc_ec_cmd(EC_BAT_TEMP, NULL, 0, (void *)&ec_word, 2);
291 if (ret)
292 return ret;
Harvey Harrison8e9c7712008-10-15 22:01:23 -0700293
294 val->intval = (int)be16_to_cpu(ec_word) * 100 / 256;
David Woodhousefb972872007-05-04 00:51:18 +0400295 break;
296 case POWER_SUPPLY_PROP_TEMP_AMBIENT:
297 ret = olpc_ec_cmd(EC_AMB_TEMP, NULL, 0, (void *)&ec_word, 2);
298 if (ret)
299 return ret;
300
Harvey Harrison8e9c7712008-10-15 22:01:23 -0700301 val->intval = (int)be16_to_cpu(ec_word) * 100 / 256;
David Woodhousefb972872007-05-04 00:51:18 +0400302 break;
Andres Salomon8e552c32008-05-12 21:46:29 -0400303 case POWER_SUPPLY_PROP_CHARGE_COUNTER:
304 ret = olpc_ec_cmd(EC_BAT_ACR, NULL, 0, (void *)&ec_word, 2);
305 if (ret)
306 return ret;
307
Harvey Harrison8e9c7712008-10-15 22:01:23 -0700308 val->intval = (int)be16_to_cpu(ec_word) * 6250 / 15;
Andres Salomon8e552c32008-05-12 21:46:29 -0400309 break;
David Woodhouse1ca5b9d2008-05-04 01:31:42 -0400310 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
311 ret = olpc_ec_cmd(EC_BAT_SERIAL, NULL, 0, (void *)&ser_buf, 8);
312 if (ret)
313 return ret;
314
315 sprintf(bat_serial, "%016llx", (long long)be64_to_cpu(ser_buf));
316 val->strval = bat_serial;
317 break;
David Woodhousefb972872007-05-04 00:51:18 +0400318 default:
319 ret = -EINVAL;
320 break;
321 }
322
323 return ret;
324}
325
326static enum power_supply_property olpc_bat_props[] = {
327 POWER_SUPPLY_PROP_STATUS,
328 POWER_SUPPLY_PROP_PRESENT,
329 POWER_SUPPLY_PROP_HEALTH,
330 POWER_SUPPLY_PROP_TECHNOLOGY,
331 POWER_SUPPLY_PROP_VOLTAGE_AVG,
332 POWER_SUPPLY_PROP_CURRENT_AVG,
333 POWER_SUPPLY_PROP_CAPACITY,
Andres Salomonb294a292009-06-30 02:13:01 -0400334 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
David Woodhousefb972872007-05-04 00:51:18 +0400335 POWER_SUPPLY_PROP_TEMP,
336 POWER_SUPPLY_PROP_TEMP_AMBIENT,
337 POWER_SUPPLY_PROP_MANUFACTURER,
David Woodhouse1ca5b9d2008-05-04 01:31:42 -0400338 POWER_SUPPLY_PROP_SERIAL_NUMBER,
Andres Salomon8e552c32008-05-12 21:46:29 -0400339 POWER_SUPPLY_PROP_CHARGE_COUNTER,
David Woodhousefb972872007-05-04 00:51:18 +0400340};
341
Andres Salomond7eb9e32008-05-02 13:41:58 -0700342/* EEPROM reading goes completely around the power_supply API, sadly */
343
344#define EEPROM_START 0x20
345#define EEPROM_END 0x80
346#define EEPROM_SIZE (EEPROM_END - EEPROM_START)
347
348static ssize_t olpc_bat_eeprom_read(struct kobject *kobj,
349 struct bin_attribute *attr, char *buf, loff_t off, size_t count)
350{
351 uint8_t ec_byte;
Andres Salomon04a820e2009-06-30 02:14:00 -0400352 int ret;
353 int i;
Andres Salomond7eb9e32008-05-02 13:41:58 -0700354
355 if (off >= EEPROM_SIZE)
356 return 0;
357 if (off + count > EEPROM_SIZE)
358 count = EEPROM_SIZE - off;
359
Andres Salomon04a820e2009-06-30 02:14:00 -0400360 for (i = 0; i < count; i++) {
361 ec_byte = EEPROM_START + off + i;
362 ret = olpc_ec_cmd(EC_BAT_EEPROM, &ec_byte, 1, &buf[i], 1);
Andres Salomond7eb9e32008-05-02 13:41:58 -0700363 if (ret) {
Andres Salomon04a820e2009-06-30 02:14:00 -0400364 pr_err("olpc-battery: "
365 "EC_BAT_EEPROM cmd @ 0x%x failed - %d!\n",
366 ec_byte, ret);
Andres Salomond7eb9e32008-05-02 13:41:58 -0700367 return -EIO;
368 }
369 }
370
371 return count;
372}
373
374static struct bin_attribute olpc_bat_eeprom = {
375 .attr = {
376 .name = "eeprom",
377 .mode = S_IRUGO,
378 .owner = THIS_MODULE,
379 },
380 .size = 0,
381 .read = olpc_bat_eeprom_read,
382};
383
Andres Salomon144bbea2009-06-30 02:15:26 -0400384/* Allow userspace to see the specific error value pulled from the EC */
385
386static ssize_t olpc_bat_error_read(struct device *dev,
387 struct device_attribute *attr, char *buf)
388{
389 uint8_t ec_byte;
390 ssize_t ret;
391
392 ret = olpc_ec_cmd(EC_BAT_ERRCODE, NULL, 0, &ec_byte, 1);
393 if (ret < 0)
394 return ret;
395
396 return sprintf(buf, "%d\n", ec_byte);
397}
398
399static struct device_attribute olpc_bat_error = {
400 .attr = {
401 .name = "error",
402 .mode = S_IRUGO,
403 },
404 .show = olpc_bat_error_read,
405};
406
David Woodhousefb972872007-05-04 00:51:18 +0400407/*********************************************************************
408 * Initialisation
409 *********************************************************************/
410
411static struct platform_device *bat_pdev;
412
413static struct power_supply olpc_bat = {
414 .properties = olpc_bat_props,
415 .num_properties = ARRAY_SIZE(olpc_bat_props),
416 .get_property = olpc_bat_get_property,
417 .use_for_apm = 1,
418};
419
420void olpc_battery_trigger_uevent(unsigned long cause)
421{
422 if (cause & EC_SCI_SRC_ACPWR)
423 kobject_uevent(&olpc_ac.dev->kobj, KOBJ_CHANGE);
424 if (cause & (EC_SCI_SRC_BATERR|EC_SCI_SRC_BATSOC|EC_SCI_SRC_BATTERY))
425 kobject_uevent(&olpc_bat.dev->kobj, KOBJ_CHANGE);
426}
427
428static int __init olpc_bat_init(void)
429{
430 int ret = 0;
431 uint8_t status;
432
433 if (!olpc_platform_info.ecver)
434 return -ENXIO;
Andres Salomon484d6d52008-05-02 13:41:59 -0700435
436 /*
437 * We've seen a number of EC protocol changes; this driver requires
438 * the latest EC protocol, supported by 0x44 and above.
439 */
440 if (olpc_platform_info.ecver < 0x44) {
441 printk(KERN_NOTICE "OLPC EC version 0x%02x too old for "
442 "battery driver.\n", olpc_platform_info.ecver);
David Woodhousefb972872007-05-04 00:51:18 +0400443 return -ENXIO;
444 }
445
446 ret = olpc_ec_cmd(EC_BAT_STATUS, NULL, 0, &status, 1);
447 if (ret)
448 return ret;
449
450 /* Ignore the status. It doesn't actually matter */
451
452 bat_pdev = platform_device_register_simple("olpc-battery", 0, NULL, 0);
453 if (IS_ERR(bat_pdev))
454 return PTR_ERR(bat_pdev);
455
456 ret = power_supply_register(&bat_pdev->dev, &olpc_ac);
457 if (ret)
458 goto ac_failed;
459
460 olpc_bat.name = bat_pdev->name;
461
462 ret = power_supply_register(&bat_pdev->dev, &olpc_bat);
463 if (ret)
464 goto battery_failed;
465
Andres Salomond7eb9e32008-05-02 13:41:58 -0700466 ret = device_create_bin_file(olpc_bat.dev, &olpc_bat_eeprom);
467 if (ret)
468 goto eeprom_failed;
469
Andres Salomon144bbea2009-06-30 02:15:26 -0400470 ret = device_create_file(olpc_bat.dev, &olpc_bat_error);
471 if (ret)
472 goto error_failed;
473
David Woodhousefb972872007-05-04 00:51:18 +0400474 goto success;
475
Andres Salomon144bbea2009-06-30 02:15:26 -0400476error_failed:
477 device_remove_bin_file(olpc_bat.dev, &olpc_bat_eeprom);
Andres Salomond7eb9e32008-05-02 13:41:58 -0700478eeprom_failed:
479 power_supply_unregister(&olpc_bat);
David Woodhousefb972872007-05-04 00:51:18 +0400480battery_failed:
481 power_supply_unregister(&olpc_ac);
482ac_failed:
483 platform_device_unregister(bat_pdev);
484success:
485 return ret;
486}
487
488static void __exit olpc_bat_exit(void)
489{
Andres Salomon144bbea2009-06-30 02:15:26 -0400490 device_remove_file(olpc_bat.dev, &olpc_bat_error);
Andres Salomond7eb9e32008-05-02 13:41:58 -0700491 device_remove_bin_file(olpc_bat.dev, &olpc_bat_eeprom);
David Woodhousefb972872007-05-04 00:51:18 +0400492 power_supply_unregister(&olpc_bat);
493 power_supply_unregister(&olpc_ac);
494 platform_device_unregister(bat_pdev);
David Woodhousefb972872007-05-04 00:51:18 +0400495}
496
497module_init(olpc_bat_init);
498module_exit(olpc_bat_exit);
499
500MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
501MODULE_LICENSE("GPL");
502MODULE_DESCRIPTION("Battery driver for One Laptop Per Child 'XO' machine");