]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/power/olpc_battery.c
power_supply: bump EC version check that we refuse to run with in olpc_battery
[net-next-2.6.git] / drivers / power / olpc_battery.c
CommitLineData
fb972873
DW
1/*
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
11#include <linux/module.h>
12#include <linux/err.h>
13#include <linux/platform_device.h>
14#include <linux/power_supply.h>
15#include <linux/jiffies.h>
16#include <linux/sched.h>
17#include <asm/olpc.h>
18
19
20#define EC_BAT_VOLTAGE 0x10 /* uint16_t, *9.76/32, mV */
21#define EC_BAT_CURRENT 0x11 /* int16_t, *15.625/120, mA */
22#define EC_BAT_ACR 0x12
23#define EC_BAT_TEMP 0x13 /* uint16_t, *100/256, °C */
24#define EC_AMB_TEMP 0x14 /* uint16_t, *100/256, °C */
25#define EC_BAT_STATUS 0x15 /* uint8_t, bitmask */
26#define EC_BAT_SOC 0x16 /* uint8_t, percentage */
27#define EC_BAT_SERIAL 0x17 /* uint8_t[6] */
28#define EC_BAT_EEPROM 0x18 /* uint8_t adr as input, uint8_t output */
29#define EC_BAT_ERRCODE 0x1f /* uint8_t, bitmask */
30
31#define BAT_STAT_PRESENT 0x01
32#define BAT_STAT_FULL 0x02
33#define BAT_STAT_LOW 0x04
34#define BAT_STAT_DESTROY 0x08
35#define BAT_STAT_AC 0x10
36#define BAT_STAT_CHARGING 0x20
37#define BAT_STAT_DISCHARGING 0x40
38
39#define BAT_ERR_INFOFAIL 0x02
40#define BAT_ERR_OVERVOLTAGE 0x04
41#define BAT_ERR_OVERTEMP 0x05
42#define BAT_ERR_GAUGESTOP 0x06
43#define BAT_ERR_OUT_OF_CONTROL 0x07
44#define BAT_ERR_ID_FAIL 0x09
45#define BAT_ERR_ACR_FAIL 0x10
46
47#define BAT_ADDR_MFR_TYPE 0x5F
48
49/*********************************************************************
50 * Power
51 *********************************************************************/
52
53static int olpc_ac_get_prop(struct power_supply *psy,
54 enum power_supply_property psp,
55 union power_supply_propval *val)
56{
57 int ret = 0;
58 uint8_t status;
59
60 switch (psp) {
61 case POWER_SUPPLY_PROP_ONLINE:
62 ret = olpc_ec_cmd(EC_BAT_STATUS, NULL, 0, &status, 1);
63 if (ret)
64 return ret;
65
66 val->intval = !!(status & BAT_STAT_AC);
67 break;
68 default:
69 ret = -EINVAL;
70 break;
71 }
72 return ret;
73}
74
75static enum power_supply_property olpc_ac_props[] = {
76 POWER_SUPPLY_PROP_ONLINE,
77};
78
79static struct power_supply olpc_ac = {
80 .name = "olpc-ac",
81 .type = POWER_SUPPLY_TYPE_MAINS,
82 .properties = olpc_ac_props,
83 .num_properties = ARRAY_SIZE(olpc_ac_props),
84 .get_property = olpc_ac_get_prop,
85};
86
1ca5b9d2
DW
87static char bat_serial[17]; /* Ick */
88
b2bd8a3b
AS
89static int olpc_bat_get_status(union power_supply_propval *val, uint8_t ec_byte)
90{
91 if (olpc_platform_info.ecver > 0x44) {
92 if (ec_byte & BAT_STAT_CHARGING)
93 val->intval = POWER_SUPPLY_STATUS_CHARGING;
94 else if (ec_byte & BAT_STAT_DISCHARGING)
95 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
96 else if (ec_byte & BAT_STAT_FULL)
97 val->intval = POWER_SUPPLY_STATUS_FULL;
98 else /* er,... */
99 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
100 } else {
101 /* Older EC didn't report charge/discharge bits */
102 if (!(ec_byte & BAT_STAT_AC)) /* No AC means discharging */
103 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
104 else if (ec_byte & BAT_STAT_FULL)
105 val->intval = POWER_SUPPLY_STATUS_FULL;
106 else /* Not _necessarily_ true but EC doesn't tell all yet */
107 val->intval = POWER_SUPPLY_STATUS_CHARGING;
108 }
109
110 return 0;
111}
112
113static int olpc_bat_get_health(union power_supply_propval *val)
114{
115 uint8_t ec_byte;
116 int ret;
117
118 ret = olpc_ec_cmd(EC_BAT_ERRCODE, NULL, 0, &ec_byte, 1);
119 if (ret)
120 return ret;
121
122 switch (ec_byte) {
123 case 0:
124 val->intval = POWER_SUPPLY_HEALTH_GOOD;
125 break;
126
127 case BAT_ERR_OVERTEMP:
128 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
129 break;
130
131 case BAT_ERR_OVERVOLTAGE:
132 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
133 break;
134
135 case BAT_ERR_INFOFAIL:
136 case BAT_ERR_OUT_OF_CONTROL:
137 case BAT_ERR_ID_FAIL:
138 case BAT_ERR_ACR_FAIL:
139 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
140 break;
141
142 default:
143 /* Eep. We don't know this failure code */
144 ret = -EIO;
145 }
146
147 return ret;
148}
149
150static int olpc_bat_get_mfr(union power_supply_propval *val)
151{
152 uint8_t ec_byte;
153 int ret;
154
155 ec_byte = BAT_ADDR_MFR_TYPE;
156 ret = olpc_ec_cmd(EC_BAT_EEPROM, &ec_byte, 1, &ec_byte, 1);
157 if (ret)
158 return ret;
159
160 switch (ec_byte >> 4) {
161 case 1:
162 val->strval = "Gold Peak";
163 break;
164 case 2:
165 val->strval = "BYD";
166 break;
167 default:
168 val->strval = "Unknown";
169 break;
170 }
171
172 return ret;
173}
174
175static int olpc_bat_get_tech(union power_supply_propval *val)
176{
177 uint8_t ec_byte;
178 int ret;
179
180 ec_byte = BAT_ADDR_MFR_TYPE;
181 ret = olpc_ec_cmd(EC_BAT_EEPROM, &ec_byte, 1, &ec_byte, 1);
182 if (ret)
183 return ret;
184
185 switch (ec_byte & 0xf) {
186 case 1:
187 val->intval = POWER_SUPPLY_TECHNOLOGY_NiMH;
188 break;
189 case 2:
190 val->intval = POWER_SUPPLY_TECHNOLOGY_LiFe;
191 break;
192 default:
193 val->intval = POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
194 break;
195 }
196
197 return ret;
198}
199
fb972873
DW
200/*********************************************************************
201 * Battery properties
202 *********************************************************************/
203static int olpc_bat_get_property(struct power_supply *psy,
204 enum power_supply_property psp,
205 union power_supply_propval *val)
206{
207 int ret = 0;
208 int16_t ec_word;
209 uint8_t ec_byte;
1ca5b9d2 210 uint64_t ser_buf;
fb972873
DW
211
212 ret = olpc_ec_cmd(EC_BAT_STATUS, NULL, 0, &ec_byte, 1);
213 if (ret)
214 return ret;
215
216 /* Theoretically there's a race here -- the battery could be
217 removed immediately after we check whether it's present, and
218 then we query for some other property of the now-absent battery.
219 It doesn't matter though -- the EC will return the last-known
220 information, and it's as if we just ran that _little_ bit faster
221 and managed to read it out before the battery went away. */
222 if (!(ec_byte & BAT_STAT_PRESENT) && psp != POWER_SUPPLY_PROP_PRESENT)
223 return -ENODEV;
224
225 switch (psp) {
226 case POWER_SUPPLY_PROP_STATUS:
b2bd8a3b
AS
227 ret = olpc_bat_get_status(val, ec_byte);
228 if (ret)
229 return ret;
230 break;
fb972873
DW
231 case POWER_SUPPLY_PROP_PRESENT:
232 val->intval = !!(ec_byte & BAT_STAT_PRESENT);
233 break;
234
235 case POWER_SUPPLY_PROP_HEALTH:
236 if (ec_byte & BAT_STAT_DESTROY)
237 val->intval = POWER_SUPPLY_HEALTH_DEAD;
238 else {
b2bd8a3b 239 ret = olpc_bat_get_health(val);
fb972873
DW
240 if (ret)
241 return ret;
fb972873
DW
242 }
243 break;
244
245 case POWER_SUPPLY_PROP_MANUFACTURER:
b2bd8a3b 246 ret = olpc_bat_get_mfr(val);
fb972873
DW
247 if (ret)
248 return ret;
fb972873
DW
249 break;
250 case POWER_SUPPLY_PROP_TECHNOLOGY:
b2bd8a3b 251 ret = olpc_bat_get_tech(val);
fb972873
DW
252 if (ret)
253 return ret;
fb972873
DW
254 break;
255 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
256 ret = olpc_ec_cmd(EC_BAT_VOLTAGE, NULL, 0, (void *)&ec_word, 2);
257 if (ret)
258 return ret;
259
260 ec_word = be16_to_cpu(ec_word);
261 val->intval = ec_word * 9760L / 32;
262 break;
263 case POWER_SUPPLY_PROP_CURRENT_AVG:
264 ret = olpc_ec_cmd(EC_BAT_CURRENT, NULL, 0, (void *)&ec_word, 2);
265 if (ret)
266 return ret;
267
268 ec_word = be16_to_cpu(ec_word);
269 val->intval = ec_word * 15625L / 120;
270 break;
271 case POWER_SUPPLY_PROP_CAPACITY:
272 ret = olpc_ec_cmd(EC_BAT_SOC, NULL, 0, &ec_byte, 1);
273 if (ret)
274 return ret;
275 val->intval = ec_byte;
276 break;
fb972873
DW
277 case POWER_SUPPLY_PROP_TEMP:
278 ret = olpc_ec_cmd(EC_BAT_TEMP, NULL, 0, (void *)&ec_word, 2);
279 if (ret)
280 return ret;
281 ec_word = be16_to_cpu(ec_word);
282 val->intval = ec_word * 100 / 256;
283 break;
284 case POWER_SUPPLY_PROP_TEMP_AMBIENT:
285 ret = olpc_ec_cmd(EC_AMB_TEMP, NULL, 0, (void *)&ec_word, 2);
286 if (ret)
287 return ret;
288
289 ec_word = be16_to_cpu(ec_word);
290 val->intval = ec_word * 100 / 256;
291 break;
1ca5b9d2
DW
292 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
293 ret = olpc_ec_cmd(EC_BAT_SERIAL, NULL, 0, (void *)&ser_buf, 8);
294 if (ret)
295 return ret;
296
297 sprintf(bat_serial, "%016llx", (long long)be64_to_cpu(ser_buf));
298 val->strval = bat_serial;
299 break;
fb972873
DW
300 default:
301 ret = -EINVAL;
302 break;
303 }
304
305 return ret;
306}
307
308static enum power_supply_property olpc_bat_props[] = {
309 POWER_SUPPLY_PROP_STATUS,
310 POWER_SUPPLY_PROP_PRESENT,
311 POWER_SUPPLY_PROP_HEALTH,
312 POWER_SUPPLY_PROP_TECHNOLOGY,
313 POWER_SUPPLY_PROP_VOLTAGE_AVG,
314 POWER_SUPPLY_PROP_CURRENT_AVG,
315 POWER_SUPPLY_PROP_CAPACITY,
fb972873
DW
316 POWER_SUPPLY_PROP_TEMP,
317 POWER_SUPPLY_PROP_TEMP_AMBIENT,
318 POWER_SUPPLY_PROP_MANUFACTURER,
1ca5b9d2 319 POWER_SUPPLY_PROP_SERIAL_NUMBER,
fb972873
DW
320};
321
d7eb9e36
AS
322/* EEPROM reading goes completely around the power_supply API, sadly */
323
324#define EEPROM_START 0x20
325#define EEPROM_END 0x80
326#define EEPROM_SIZE (EEPROM_END - EEPROM_START)
327
328static ssize_t olpc_bat_eeprom_read(struct kobject *kobj,
329 struct bin_attribute *attr, char *buf, loff_t off, size_t count)
330{
331 uint8_t ec_byte;
332 int ret, end;
333
334 if (off >= EEPROM_SIZE)
335 return 0;
336 if (off + count > EEPROM_SIZE)
337 count = EEPROM_SIZE - off;
338
339 end = EEPROM_START + off + count;
340 for (ec_byte = EEPROM_START + off; ec_byte < end; ec_byte++) {
341 ret = olpc_ec_cmd(EC_BAT_EEPROM, &ec_byte, 1,
342 &buf[ec_byte - EEPROM_START], 1);
343 if (ret) {
344 printk(KERN_ERR "olpc-battery: EC command "
345 "EC_BAT_EEPROM @ 0x%x failed -"
346 " %d!\n", ec_byte, ret);
347 return -EIO;
348 }
349 }
350
351 return count;
352}
353
354static struct bin_attribute olpc_bat_eeprom = {
355 .attr = {
356 .name = "eeprom",
357 .mode = S_IRUGO,
358 .owner = THIS_MODULE,
359 },
360 .size = 0,
361 .read = olpc_bat_eeprom_read,
362};
363
fb972873
DW
364/*********************************************************************
365 * Initialisation
366 *********************************************************************/
367
368static struct platform_device *bat_pdev;
369
370static struct power_supply olpc_bat = {
371 .properties = olpc_bat_props,
372 .num_properties = ARRAY_SIZE(olpc_bat_props),
373 .get_property = olpc_bat_get_property,
374 .use_for_apm = 1,
375};
376
377void olpc_battery_trigger_uevent(unsigned long cause)
378{
379 if (cause & EC_SCI_SRC_ACPWR)
380 kobject_uevent(&olpc_ac.dev->kobj, KOBJ_CHANGE);
381 if (cause & (EC_SCI_SRC_BATERR|EC_SCI_SRC_BATSOC|EC_SCI_SRC_BATTERY))
382 kobject_uevent(&olpc_bat.dev->kobj, KOBJ_CHANGE);
383}
384
385static int __init olpc_bat_init(void)
386{
387 int ret = 0;
388 uint8_t status;
389
390 if (!olpc_platform_info.ecver)
391 return -ENXIO;
484d6d50
AS
392
393 /*
394 * We've seen a number of EC protocol changes; this driver requires
395 * the latest EC protocol, supported by 0x44 and above.
396 */
397 if (olpc_platform_info.ecver < 0x44) {
398 printk(KERN_NOTICE "OLPC EC version 0x%02x too old for "
399 "battery driver.\n", olpc_platform_info.ecver);
fb972873
DW
400 return -ENXIO;
401 }
402
403 ret = olpc_ec_cmd(EC_BAT_STATUS, NULL, 0, &status, 1);
404 if (ret)
405 return ret;
406
407 /* Ignore the status. It doesn't actually matter */
408
409 bat_pdev = platform_device_register_simple("olpc-battery", 0, NULL, 0);
410 if (IS_ERR(bat_pdev))
411 return PTR_ERR(bat_pdev);
412
413 ret = power_supply_register(&bat_pdev->dev, &olpc_ac);
414 if (ret)
415 goto ac_failed;
416
417 olpc_bat.name = bat_pdev->name;
418
419 ret = power_supply_register(&bat_pdev->dev, &olpc_bat);
420 if (ret)
421 goto battery_failed;
422
d7eb9e36
AS
423 ret = device_create_bin_file(olpc_bat.dev, &olpc_bat_eeprom);
424 if (ret)
425 goto eeprom_failed;
426
fb972873
DW
427 goto success;
428
d7eb9e36
AS
429eeprom_failed:
430 power_supply_unregister(&olpc_bat);
fb972873
DW
431battery_failed:
432 power_supply_unregister(&olpc_ac);
433ac_failed:
434 platform_device_unregister(bat_pdev);
435success:
436 return ret;
437}
438
439static void __exit olpc_bat_exit(void)
440{
d7eb9e36 441 device_remove_bin_file(olpc_bat.dev, &olpc_bat_eeprom);
fb972873
DW
442 power_supply_unregister(&olpc_bat);
443 power_supply_unregister(&olpc_ac);
444 platform_device_unregister(bat_pdev);
fb972873
DW
445}
446
447module_init(olpc_bat_init);
448module_exit(olpc_bat_exit);
449
450MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
451MODULE_LICENSE("GPL");
452MODULE_DESCRIPTION("Battery driver for One Laptop Per Child 'XO' machine");