Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[linux-2.6] / drivers / power / olpc_battery.c
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    /* int16_t,     *6250/15,    µAh  */
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
53 static 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
75 static enum power_supply_property olpc_ac_props[] = {
76         POWER_SUPPLY_PROP_ONLINE,
77 };
78
79 static 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
87 static char bat_serial[17]; /* Ick */
88
89 static 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
113 static 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
150 static 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
175 static 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
200 /*********************************************************************
201  *              Battery properties
202  *********************************************************************/
203 static 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;
210         uint64_t ser_buf;
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:
227                 ret = olpc_bat_get_status(val, ec_byte);
228                 if (ret)
229                         return ret;
230                 break;
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 {
239                         ret = olpc_bat_get_health(val);
240                         if (ret)
241                                 return ret;
242                 }
243                 break;
244
245         case POWER_SUPPLY_PROP_MANUFACTURER:
246                 ret = olpc_bat_get_mfr(val);
247                 if (ret)
248                         return ret;
249                 break;
250         case POWER_SUPPLY_PROP_TECHNOLOGY:
251                 ret = olpc_bat_get_tech(val);
252                 if (ret)
253                         return ret;
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;
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;
292         case POWER_SUPPLY_PROP_CHARGE_COUNTER:
293                 ret = olpc_ec_cmd(EC_BAT_ACR, NULL, 0, (void *)&ec_word, 2);
294                 if (ret)
295                         return ret;
296
297                 ec_word = be16_to_cpu(ec_word);
298                 val->intval = ec_word * 6250 / 15;
299                 break;
300         case POWER_SUPPLY_PROP_SERIAL_NUMBER:
301                 ret = olpc_ec_cmd(EC_BAT_SERIAL, NULL, 0, (void *)&ser_buf, 8);
302                 if (ret)
303                         return ret;
304
305                 sprintf(bat_serial, "%016llx", (long long)be64_to_cpu(ser_buf));
306                 val->strval = bat_serial;
307                 break;
308         default:
309                 ret = -EINVAL;
310                 break;
311         }
312
313         return ret;
314 }
315
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,
329 };
330
331 /* EEPROM reading goes completely around the power_supply API, sadly */
332
333 #define EEPROM_START    0x20
334 #define EEPROM_END      0x80
335 #define EEPROM_SIZE     (EEPROM_END - EEPROM_START)
336
337 static ssize_t olpc_bat_eeprom_read(struct kobject *kobj,
338                 struct bin_attribute *attr, char *buf, loff_t off, size_t count)
339 {
340         uint8_t ec_byte;
341         int ret, end;
342
343         if (off >= EEPROM_SIZE)
344                 return 0;
345         if (off + count > EEPROM_SIZE)
346                 count = EEPROM_SIZE - off;
347
348         end = EEPROM_START + off + count;
349         for (ec_byte = EEPROM_START + off; ec_byte < end; ec_byte++) {
350                 ret = olpc_ec_cmd(EC_BAT_EEPROM, &ec_byte, 1,
351                                 &buf[ec_byte - EEPROM_START], 1);
352                 if (ret) {
353                         printk(KERN_ERR "olpc-battery:  EC command "
354                                         "EC_BAT_EEPROM @ 0x%x failed -"
355                                         " %d!\n", ec_byte, ret);
356                         return -EIO;
357                 }
358         }
359
360         return count;
361 }
362
363 static struct bin_attribute olpc_bat_eeprom = {
364         .attr = {
365                 .name = "eeprom",
366                 .mode = S_IRUGO,
367                 .owner = THIS_MODULE,
368         },
369         .size = 0,
370         .read = olpc_bat_eeprom_read,
371 };
372
373 /*********************************************************************
374  *              Initialisation
375  *********************************************************************/
376
377 static struct platform_device *bat_pdev;
378
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,
383         .use_for_apm = 1,
384 };
385
386 void olpc_battery_trigger_uevent(unsigned long cause)
387 {
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);
392 }
393
394 static int __init olpc_bat_init(void)
395 {
396         int ret = 0;
397         uint8_t status;
398
399         if (!olpc_platform_info.ecver)
400                 return -ENXIO;
401
402         /*
403          * We've seen a number of EC protocol changes; this driver requires
404          * the latest EC protocol, supported by 0x44 and above.
405          */
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);
409                 return -ENXIO;
410         }
411
412         ret = olpc_ec_cmd(EC_BAT_STATUS, NULL, 0, &status, 1);
413         if (ret)
414                 return ret;
415
416         /* Ignore the status. It doesn't actually matter */
417
418         bat_pdev = platform_device_register_simple("olpc-battery", 0, NULL, 0);
419         if (IS_ERR(bat_pdev))
420                 return PTR_ERR(bat_pdev);
421
422         ret = power_supply_register(&bat_pdev->dev, &olpc_ac);
423         if (ret)
424                 goto ac_failed;
425
426         olpc_bat.name = bat_pdev->name;
427
428         ret = power_supply_register(&bat_pdev->dev, &olpc_bat);
429         if (ret)
430                 goto battery_failed;
431
432         ret = device_create_bin_file(olpc_bat.dev, &olpc_bat_eeprom);
433         if (ret)
434                 goto eeprom_failed;
435
436         goto success;
437
438 eeprom_failed:
439         power_supply_unregister(&olpc_bat);
440 battery_failed:
441         power_supply_unregister(&olpc_ac);
442 ac_failed:
443         platform_device_unregister(bat_pdev);
444 success:
445         return ret;
446 }
447
448 static void __exit olpc_bat_exit(void)
449 {
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);
454 }
455
456 module_init(olpc_bat_init);
457 module_exit(olpc_bat_exit);
458
459 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
460 MODULE_LICENSE("GPL");
461 MODULE_DESCRIPTION("Battery driver for One Laptop Per Child 'XO' machine");