sysfs: Remove usage of S_BIAS to avoid merge conflict with the vfs tree
[GitHub/mt8127/android_kernel_alcatel_ttab.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
04a820ea 11#include <linux/kernel.h>
fb972873 12#include <linux/module.h>
144bbeae 13#include <linux/types.h>
fb972873 14#include <linux/err.h>
144bbeae 15#include <linux/device.h>
fb972873
DW
16#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 */
75d88079 25#define EC_BAT_ACR 0x12 /* int16_t, *6250/15, µAh */
fb972873
DW
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 */
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
8f7e5798 41#define BAT_STAT_TRICKLE 0x80
fb972873
DW
42
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
1ca5b9d2
DW
91static char bat_serial[17]; /* Ick */
92
b2bd8a3b
AS
93static int olpc_bat_get_status(union power_supply_propval *val, uint8_t ec_byte)
94{
95 if (olpc_platform_info.ecver > 0x44) {
8f7e5798 96 if (ec_byte & (BAT_STAT_CHARGING | BAT_STAT_TRICKLE))
b2bd8a3b
AS
97 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
fb972873
DW
204/*********************************************************************
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;
8e9c7716 212 __be16 ec_word;
fb972873 213 uint8_t ec_byte;
8e9c7716 214 __be64 ser_buf;
fb972873
DW
215
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. */
8f7e5798
AS
226 if (!(ec_byte & (BAT_STAT_PRESENT | BAT_STAT_TRICKLE)) &&
227 psp != POWER_SUPPLY_PROP_PRESENT)
fb972873
DW
228 return -ENODEV;
229
230 switch (psp) {
231 case POWER_SUPPLY_PROP_STATUS:
b2bd8a3b
AS
232 ret = olpc_bat_get_status(val, ec_byte);
233 if (ret)
234 return ret;
235 break;
ee8076ed
AS
236 case POWER_SUPPLY_PROP_CHARGE_TYPE:
237 if (ec_byte & BAT_STAT_TRICKLE)
238 val->intval = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
239 else if (ec_byte & BAT_STAT_CHARGING)
240 val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
241 else
242 val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;
243 break;
fb972873 244 case POWER_SUPPLY_PROP_PRESENT:
8f7e5798
AS
245 val->intval = !!(ec_byte & (BAT_STAT_PRESENT |
246 BAT_STAT_TRICKLE));
fb972873
DW
247 break;
248
249 case POWER_SUPPLY_PROP_HEALTH:
250 if (ec_byte & BAT_STAT_DESTROY)
251 val->intval = POWER_SUPPLY_HEALTH_DEAD;
252 else {
b2bd8a3b 253 ret = olpc_bat_get_health(val);
fb972873
DW
254 if (ret)
255 return ret;
fb972873
DW
256 }
257 break;
258
259 case POWER_SUPPLY_PROP_MANUFACTURER:
b2bd8a3b 260 ret = olpc_bat_get_mfr(val);
fb972873
DW
261 if (ret)
262 return ret;
fb972873
DW
263 break;
264 case POWER_SUPPLY_PROP_TECHNOLOGY:
b2bd8a3b 265 ret = olpc_bat_get_tech(val);
fb972873
DW
266 if (ret)
267 return ret;
fb972873
DW
268 break;
269 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
270 ret = olpc_ec_cmd(EC_BAT_VOLTAGE, NULL, 0, (void *)&ec_word, 2);
271 if (ret)
272 return ret;
273
8e9c7716 274 val->intval = (int)be16_to_cpu(ec_word) * 9760L / 32;
fb972873
DW
275 break;
276 case POWER_SUPPLY_PROP_CURRENT_AVG:
277 ret = olpc_ec_cmd(EC_BAT_CURRENT, NULL, 0, (void *)&ec_word, 2);
278 if (ret)
279 return ret;
280
8e9c7716 281 val->intval = (int)be16_to_cpu(ec_word) * 15625L / 120;
fb972873
DW
282 break;
283 case POWER_SUPPLY_PROP_CAPACITY:
284 ret = olpc_ec_cmd(EC_BAT_SOC, NULL, 0, &ec_byte, 1);
285 if (ret)
286 return ret;
287 val->intval = ec_byte;
288 break;
b294a290
AS
289 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
290 if (ec_byte & BAT_STAT_FULL)
291 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
292 else if (ec_byte & BAT_STAT_LOW)
293 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
294 else
295 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
296 break;
fb972873
DW
297 case POWER_SUPPLY_PROP_TEMP:
298 ret = olpc_ec_cmd(EC_BAT_TEMP, NULL, 0, (void *)&ec_word, 2);
299 if (ret)
300 return ret;
8e9c7716
HH
301
302 val->intval = (int)be16_to_cpu(ec_word) * 100 / 256;
fb972873
DW
303 break;
304 case POWER_SUPPLY_PROP_TEMP_AMBIENT:
305 ret = olpc_ec_cmd(EC_AMB_TEMP, NULL, 0, (void *)&ec_word, 2);
306 if (ret)
307 return ret;
308
8e9c7716 309 val->intval = (int)be16_to_cpu(ec_word) * 100 / 256;
fb972873 310 break;
8e552c36
AS
311 case POWER_SUPPLY_PROP_CHARGE_COUNTER:
312 ret = olpc_ec_cmd(EC_BAT_ACR, NULL, 0, (void *)&ec_word, 2);
313 if (ret)
314 return ret;
315
8e9c7716 316 val->intval = (int)be16_to_cpu(ec_word) * 6250 / 15;
8e552c36 317 break;
1ca5b9d2
DW
318 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
319 ret = olpc_ec_cmd(EC_BAT_SERIAL, NULL, 0, (void *)&ser_buf, 8);
320 if (ret)
321 return ret;
322
323 sprintf(bat_serial, "%016llx", (long long)be64_to_cpu(ser_buf));
324 val->strval = bat_serial;
325 break;
fb972873
DW
326 default:
327 ret = -EINVAL;
328 break;
329 }
330
331 return ret;
332}
333
334static enum power_supply_property olpc_bat_props[] = {
335 POWER_SUPPLY_PROP_STATUS,
ee8076ed 336 POWER_SUPPLY_PROP_CHARGE_TYPE,
fb972873
DW
337 POWER_SUPPLY_PROP_PRESENT,
338 POWER_SUPPLY_PROP_HEALTH,
339 POWER_SUPPLY_PROP_TECHNOLOGY,
340 POWER_SUPPLY_PROP_VOLTAGE_AVG,
341 POWER_SUPPLY_PROP_CURRENT_AVG,
342 POWER_SUPPLY_PROP_CAPACITY,
b294a290 343 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
fb972873
DW
344 POWER_SUPPLY_PROP_TEMP,
345 POWER_SUPPLY_PROP_TEMP_AMBIENT,
346 POWER_SUPPLY_PROP_MANUFACTURER,
1ca5b9d2 347 POWER_SUPPLY_PROP_SERIAL_NUMBER,
8e552c36 348 POWER_SUPPLY_PROP_CHARGE_COUNTER,
fb972873
DW
349};
350
d7eb9e36
AS
351/* EEPROM reading goes completely around the power_supply API, sadly */
352
353#define EEPROM_START 0x20
354#define EEPROM_END 0x80
355#define EEPROM_SIZE (EEPROM_END - EEPROM_START)
356
357static ssize_t olpc_bat_eeprom_read(struct kobject *kobj,
358 struct bin_attribute *attr, char *buf, loff_t off, size_t count)
359{
360 uint8_t ec_byte;
04a820ea
AS
361 int ret;
362 int i;
d7eb9e36
AS
363
364 if (off >= EEPROM_SIZE)
365 return 0;
366 if (off + count > EEPROM_SIZE)
367 count = EEPROM_SIZE - off;
368
04a820ea
AS
369 for (i = 0; i < count; i++) {
370 ec_byte = EEPROM_START + off + i;
371 ret = olpc_ec_cmd(EC_BAT_EEPROM, &ec_byte, 1, &buf[i], 1);
d7eb9e36 372 if (ret) {
04a820ea
AS
373 pr_err("olpc-battery: "
374 "EC_BAT_EEPROM cmd @ 0x%x failed - %d!\n",
375 ec_byte, ret);
d7eb9e36
AS
376 return -EIO;
377 }
378 }
379
380 return count;
381}
382
383static struct bin_attribute olpc_bat_eeprom = {
384 .attr = {
385 .name = "eeprom",
386 .mode = S_IRUGO,
387 .owner = THIS_MODULE,
388 },
389 .size = 0,
390 .read = olpc_bat_eeprom_read,
391};
392
144bbeae
AS
393/* Allow userspace to see the specific error value pulled from the EC */
394
395static ssize_t olpc_bat_error_read(struct device *dev,
396 struct device_attribute *attr, char *buf)
397{
398 uint8_t ec_byte;
399 ssize_t ret;
400
401 ret = olpc_ec_cmd(EC_BAT_ERRCODE, NULL, 0, &ec_byte, 1);
402 if (ret < 0)
403 return ret;
404
405 return sprintf(buf, "%d\n", ec_byte);
406}
407
408static struct device_attribute olpc_bat_error = {
409 .attr = {
410 .name = "error",
411 .mode = S_IRUGO,
412 },
413 .show = olpc_bat_error_read,
414};
415
fb972873
DW
416/*********************************************************************
417 * Initialisation
418 *********************************************************************/
419
420static struct platform_device *bat_pdev;
421
422static struct power_supply olpc_bat = {
423 .properties = olpc_bat_props,
424 .num_properties = ARRAY_SIZE(olpc_bat_props),
425 .get_property = olpc_bat_get_property,
426 .use_for_apm = 1,
427};
428
429void olpc_battery_trigger_uevent(unsigned long cause)
430{
431 if (cause & EC_SCI_SRC_ACPWR)
432 kobject_uevent(&olpc_ac.dev->kobj, KOBJ_CHANGE);
433 if (cause & (EC_SCI_SRC_BATERR|EC_SCI_SRC_BATSOC|EC_SCI_SRC_BATTERY))
434 kobject_uevent(&olpc_bat.dev->kobj, KOBJ_CHANGE);
435}
436
437static int __init olpc_bat_init(void)
438{
439 int ret = 0;
440 uint8_t status;
441
442 if (!olpc_platform_info.ecver)
443 return -ENXIO;
484d6d50
AS
444
445 /*
446 * We've seen a number of EC protocol changes; this driver requires
447 * the latest EC protocol, supported by 0x44 and above.
448 */
449 if (olpc_platform_info.ecver < 0x44) {
450 printk(KERN_NOTICE "OLPC EC version 0x%02x too old for "
451 "battery driver.\n", olpc_platform_info.ecver);
fb972873
DW
452 return -ENXIO;
453 }
454
455 ret = olpc_ec_cmd(EC_BAT_STATUS, NULL, 0, &status, 1);
456 if (ret)
457 return ret;
458
459 /* Ignore the status. It doesn't actually matter */
460
461 bat_pdev = platform_device_register_simple("olpc-battery", 0, NULL, 0);
462 if (IS_ERR(bat_pdev))
463 return PTR_ERR(bat_pdev);
464
465 ret = power_supply_register(&bat_pdev->dev, &olpc_ac);
466 if (ret)
467 goto ac_failed;
468
469 olpc_bat.name = bat_pdev->name;
470
471 ret = power_supply_register(&bat_pdev->dev, &olpc_bat);
472 if (ret)
473 goto battery_failed;
474
d7eb9e36
AS
475 ret = device_create_bin_file(olpc_bat.dev, &olpc_bat_eeprom);
476 if (ret)
477 goto eeprom_failed;
478
144bbeae
AS
479 ret = device_create_file(olpc_bat.dev, &olpc_bat_error);
480 if (ret)
481 goto error_failed;
482
fb972873
DW
483 goto success;
484
144bbeae
AS
485error_failed:
486 device_remove_bin_file(olpc_bat.dev, &olpc_bat_eeprom);
d7eb9e36
AS
487eeprom_failed:
488 power_supply_unregister(&olpc_bat);
fb972873
DW
489battery_failed:
490 power_supply_unregister(&olpc_ac);
491ac_failed:
492 platform_device_unregister(bat_pdev);
493success:
494 return ret;
495}
496
497static void __exit olpc_bat_exit(void)
498{
144bbeae 499 device_remove_file(olpc_bat.dev, &olpc_bat_error);
d7eb9e36 500 device_remove_bin_file(olpc_bat.dev, &olpc_bat_eeprom);
fb972873
DW
501 power_supply_unregister(&olpc_bat);
502 power_supply_unregister(&olpc_ac);
503 platform_device_unregister(bat_pdev);
fb972873
DW
504}
505
506module_init(olpc_bat_init);
507module_exit(olpc_bat_exit);
508
509MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
510MODULE_LICENSE("GPL");
511MODULE_DESCRIPTION("Battery driver for One Laptop Per Child 'XO' machine");