diff options
Diffstat (limited to 'drivers/power/olpc_battery.c')
-rw-r--r-- | drivers/power/olpc_battery.c | 273 |
1 files changed, 197 insertions, 76 deletions
diff --git a/drivers/power/olpc_battery.c b/drivers/power/olpc_battery.c index ab1e8289f07f..32570af3c5c9 100644 --- a/drivers/power/olpc_battery.c +++ b/drivers/power/olpc_battery.c | |||
@@ -19,7 +19,7 @@ | |||
19 | 19 | ||
20 | #define EC_BAT_VOLTAGE 0x10 /* uint16_t, *9.76/32, mV */ | 20 | #define EC_BAT_VOLTAGE 0x10 /* uint16_t, *9.76/32, mV */ |
21 | #define EC_BAT_CURRENT 0x11 /* int16_t, *15.625/120, mA */ | 21 | #define EC_BAT_CURRENT 0x11 /* int16_t, *15.625/120, mA */ |
22 | #define EC_BAT_ACR 0x12 | 22 | #define EC_BAT_ACR 0x12 /* int16_t, *6250/15, µAh */ |
23 | #define EC_BAT_TEMP 0x13 /* uint16_t, *100/256, °C */ | 23 | #define EC_BAT_TEMP 0x13 /* uint16_t, *100/256, °C */ |
24 | #define EC_AMB_TEMP 0x14 /* 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 */ | 25 | #define EC_BAT_STATUS 0x15 /* uint8_t, bitmask */ |
@@ -84,6 +84,119 @@ static struct power_supply olpc_ac = { | |||
84 | .get_property = olpc_ac_get_prop, | 84 | .get_property = olpc_ac_get_prop, |
85 | }; | 85 | }; |
86 | 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 | |||
87 | /********************************************************************* | 200 | /********************************************************************* |
88 | * Battery properties | 201 | * Battery properties |
89 | *********************************************************************/ | 202 | *********************************************************************/ |
@@ -94,6 +207,7 @@ static int olpc_bat_get_property(struct power_supply *psy, | |||
94 | int ret = 0; | 207 | int ret = 0; |
95 | int16_t ec_word; | 208 | int16_t ec_word; |
96 | uint8_t ec_byte; | 209 | uint8_t ec_byte; |
210 | uint64_t ser_buf; | ||
97 | 211 | ||
98 | ret = olpc_ec_cmd(EC_BAT_STATUS, NULL, 0, &ec_byte, 1); | 212 | ret = olpc_ec_cmd(EC_BAT_STATUS, NULL, 0, &ec_byte, 1); |
99 | if (ret) | 213 | if (ret) |
@@ -110,25 +224,10 @@ static int olpc_bat_get_property(struct power_supply *psy, | |||
110 | 224 | ||
111 | switch (psp) { | 225 | switch (psp) { |
112 | case POWER_SUPPLY_PROP_STATUS: | 226 | case POWER_SUPPLY_PROP_STATUS: |
113 | if (olpc_platform_info.ecver > 0x44) { | 227 | ret = olpc_bat_get_status(val, ec_byte); |
114 | if (ec_byte & BAT_STAT_CHARGING) | 228 | if (ret) |
115 | val->intval = POWER_SUPPLY_STATUS_CHARGING; | 229 | return ret; |
116 | else if (ec_byte & BAT_STAT_DISCHARGING) | 230 | break; |
117 | val->intval = POWER_SUPPLY_STATUS_DISCHARGING; | ||
118 | else if (ec_byte & BAT_STAT_FULL) | ||
119 | val->intval = POWER_SUPPLY_STATUS_FULL; | ||
120 | else /* er,... */ | ||
121 | val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING; | ||
122 | } else { | ||
123 | /* Older EC didn't report charge/discharge bits */ | ||
124 | if (!(ec_byte & BAT_STAT_AC)) /* No AC means discharging */ | ||
125 | val->intval = POWER_SUPPLY_STATUS_DISCHARGING; | ||
126 | else if (ec_byte & BAT_STAT_FULL) | ||
127 | val->intval = POWER_SUPPLY_STATUS_FULL; | ||
128 | else /* Not _necessarily_ true but EC doesn't tell all yet */ | ||
129 | val->intval = POWER_SUPPLY_STATUS_CHARGING; | ||
130 | break; | ||
131 | } | ||
132 | case POWER_SUPPLY_PROP_PRESENT: | 231 | case POWER_SUPPLY_PROP_PRESENT: |
133 | val->intval = !!(ec_byte & BAT_STAT_PRESENT); | 232 | val->intval = !!(ec_byte & BAT_STAT_PRESENT); |
134 | break; | 233 | break; |
@@ -137,72 +236,21 @@ static int olpc_bat_get_property(struct power_supply *psy, | |||
137 | if (ec_byte & BAT_STAT_DESTROY) | 236 | if (ec_byte & BAT_STAT_DESTROY) |
138 | val->intval = POWER_SUPPLY_HEALTH_DEAD; | 237 | val->intval = POWER_SUPPLY_HEALTH_DEAD; |
139 | else { | 238 | else { |
140 | ret = olpc_ec_cmd(EC_BAT_ERRCODE, NULL, 0, &ec_byte, 1); | 239 | ret = olpc_bat_get_health(val); |
141 | if (ret) | 240 | if (ret) |
142 | return ret; | 241 | return ret; |
143 | |||
144 | switch (ec_byte) { | ||
145 | case 0: | ||
146 | val->intval = POWER_SUPPLY_HEALTH_GOOD; | ||
147 | break; | ||
148 | |||
149 | case BAT_ERR_OVERTEMP: | ||
150 | val->intval = POWER_SUPPLY_HEALTH_OVERHEAT; | ||
151 | break; | ||
152 | |||
153 | case BAT_ERR_OVERVOLTAGE: | ||
154 | val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE; | ||
155 | break; | ||
156 | |||
157 | case BAT_ERR_INFOFAIL: | ||
158 | case BAT_ERR_OUT_OF_CONTROL: | ||
159 | case BAT_ERR_ID_FAIL: | ||
160 | case BAT_ERR_ACR_FAIL: | ||
161 | val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE; | ||
162 | break; | ||
163 | |||
164 | default: | ||
165 | /* Eep. We don't know this failure code */ | ||
166 | return -EIO; | ||
167 | } | ||
168 | } | 242 | } |
169 | break; | 243 | break; |
170 | 244 | ||
171 | case POWER_SUPPLY_PROP_MANUFACTURER: | 245 | case POWER_SUPPLY_PROP_MANUFACTURER: |
172 | ec_byte = BAT_ADDR_MFR_TYPE; | 246 | ret = olpc_bat_get_mfr(val); |
173 | ret = olpc_ec_cmd(EC_BAT_EEPROM, &ec_byte, 1, &ec_byte, 1); | ||
174 | if (ret) | 247 | if (ret) |
175 | return ret; | 248 | return ret; |
176 | |||
177 | switch (ec_byte >> 4) { | ||
178 | case 1: | ||
179 | val->strval = "Gold Peak"; | ||
180 | break; | ||
181 | case 2: | ||
182 | val->strval = "BYD"; | ||
183 | break; | ||
184 | default: | ||
185 | val->strval = "Unknown"; | ||
186 | break; | ||
187 | } | ||
188 | break; | 249 | break; |
189 | case POWER_SUPPLY_PROP_TECHNOLOGY: | 250 | case POWER_SUPPLY_PROP_TECHNOLOGY: |
190 | ec_byte = BAT_ADDR_MFR_TYPE; | 251 | ret = olpc_bat_get_tech(val); |
191 | ret = olpc_ec_cmd(EC_BAT_EEPROM, &ec_byte, 1, &ec_byte, 1); | ||
192 | if (ret) | 252 | if (ret) |
193 | return ret; | 253 | return ret; |
194 | |||
195 | switch (ec_byte & 0xf) { | ||
196 | case 1: | ||
197 | val->intval = POWER_SUPPLY_TECHNOLOGY_NiMH; | ||
198 | break; | ||
199 | case 2: | ||
200 | val->intval = POWER_SUPPLY_TECHNOLOGY_LiFe; | ||
201 | break; | ||
202 | default: | ||
203 | val->intval = POWER_SUPPLY_TECHNOLOGY_UNKNOWN; | ||
204 | break; | ||
205 | } | ||
206 | break; | 254 | break; |
207 | case POWER_SUPPLY_PROP_VOLTAGE_AVG: | 255 | case POWER_SUPPLY_PROP_VOLTAGE_AVG: |
208 | ret = olpc_ec_cmd(EC_BAT_VOLTAGE, NULL, 0, (void *)&ec_word, 2); | 256 | ret = olpc_ec_cmd(EC_BAT_VOLTAGE, NULL, 0, (void *)&ec_word, 2); |
@@ -241,6 +289,22 @@ static int olpc_bat_get_property(struct power_supply *psy, | |||
241 | ec_word = be16_to_cpu(ec_word); | 289 | ec_word = be16_to_cpu(ec_word); |
242 | val->intval = ec_word * 100 / 256; | 290 | val->intval = ec_word * 100 / 256; |
243 | break; | 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; | ||
244 | default: | 308 | default: |
245 | ret = -EINVAL; | 309 | ret = -EINVAL; |
246 | break; | 310 | break; |
@@ -260,6 +324,50 @@ static enum power_supply_property olpc_bat_props[] = { | |||
260 | POWER_SUPPLY_PROP_TEMP, | 324 | POWER_SUPPLY_PROP_TEMP, |
261 | POWER_SUPPLY_PROP_TEMP_AMBIENT, | 325 | POWER_SUPPLY_PROP_TEMP_AMBIENT, |
262 | POWER_SUPPLY_PROP_MANUFACTURER, | 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, | ||
263 | }; | 371 | }; |
264 | 372 | ||
265 | /********************************************************************* | 373 | /********************************************************************* |
@@ -290,8 +398,14 @@ static int __init olpc_bat_init(void) | |||
290 | 398 | ||
291 | if (!olpc_platform_info.ecver) | 399 | if (!olpc_platform_info.ecver) |
292 | return -ENXIO; | 400 | return -ENXIO; |
293 | if (olpc_platform_info.ecver < 0x43) { | 401 | |
294 | printk(KERN_NOTICE "OLPC EC version 0x%02x too old for battery driver.\n", olpc_platform_info.ecver); | 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); | ||
295 | return -ENXIO; | 409 | return -ENXIO; |
296 | } | 410 | } |
297 | 411 | ||
@@ -315,8 +429,14 @@ static int __init olpc_bat_init(void) | |||
315 | if (ret) | 429 | if (ret) |
316 | goto battery_failed; | 430 | goto battery_failed; |
317 | 431 | ||
432 | ret = device_create_bin_file(olpc_bat.dev, &olpc_bat_eeprom); | ||
433 | if (ret) | ||
434 | goto eeprom_failed; | ||
435 | |||
318 | goto success; | 436 | goto success; |
319 | 437 | ||
438 | eeprom_failed: | ||
439 | power_supply_unregister(&olpc_bat); | ||
320 | battery_failed: | 440 | battery_failed: |
321 | power_supply_unregister(&olpc_ac); | 441 | power_supply_unregister(&olpc_ac); |
322 | ac_failed: | 442 | ac_failed: |
@@ -327,6 +447,7 @@ success: | |||
327 | 447 | ||
328 | static void __exit olpc_bat_exit(void) | 448 | static void __exit olpc_bat_exit(void) |
329 | { | 449 | { |
450 | device_remove_bin_file(olpc_bat.dev, &olpc_bat_eeprom); | ||
330 | power_supply_unregister(&olpc_bat); | 451 | power_supply_unregister(&olpc_bat); |
331 | power_supply_unregister(&olpc_ac); | 452 | power_supply_unregister(&olpc_ac); |
332 | platform_device_unregister(bat_pdev); | 453 | platform_device_unregister(bat_pdev); |