Skip to content

Commit b14f7fb

Browse files
committed
Merge git://git.infradead.org/battery-2.6
* git://git.infradead.org/battery-2.6: power_supply: Sharp SL-6000 (tosa) batteries support power_supply: fix up CHARGE_COUNTER output to be more precise power_supply: add CHARGE_COUNTER property and olpc_battery support for it power_supply: bump EC version check that we refuse to run with in olpc_battery power_supply: cleanup of the OLPC battery driver power_supply: add eeprom dump file to olpc_battery's sysfs power_supply: Support serial number in olpc_battery
2 parents 00e9028 + 9fec606 commit b14f7fb

7 files changed

Lines changed: 697 additions & 76 deletions

File tree

‎Documentation/power/power_supply_class.txt‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ of charge when battery became full/empty". It also could mean "value of
101101
charge when battery considered full/empty at given conditions (temperature,
102102
age)". I.e. these attributes represents real thresholds, not design values.
103103

104+
CHARGE_COUNTER - the current charge counter (in µAh). This could easily
105+
be negative; there is no empty or full value. It is only useful for
106+
relative, time-based measurements.
107+
104108
ENERGY_FULL, ENERGY_EMPTY - same as above but for energy.
105109

106110
CAPACITY - capacity in percents.

‎drivers/power/Kconfig‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ config BATTERY_OLPC
4949
help
5050
Say Y to enable support for the battery on the OLPC laptop.
5151

52+
config BATTERY_TOSA
53+
tristate "Sharp SL-6000 (tosa) battery"
54+
depends on MACH_TOSA && MFD_TC6393XB
55+
help
56+
Say Y to enable support for the battery on the Sharp Zaurus
57+
SL-6000 (tosa) models.
58+
5259
config BATTERY_PALMTX
5360
tristate "Palm T|X battery"
5461
depends on MACH_PALMTX

‎drivers/power/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ obj-$(CONFIG_APM_POWER) += apm_power.o
2020
obj-$(CONFIG_BATTERY_DS2760) += ds2760_battery.o
2121
obj-$(CONFIG_BATTERY_PMU) += pmu_battery.o
2222
obj-$(CONFIG_BATTERY_OLPC) += olpc_battery.o
23+
obj-$(CONFIG_BATTERY_TOSA) += tosa_battery.o
2324
obj-$(CONFIG_BATTERY_PALMTX) += palmtx_battery.o

‎drivers/power/olpc_battery.c‎

Lines changed: 197 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
#define EC_BAT_VOLTAGE 0x10 /* uint16_t, *9.76/32, mV */
2121
#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 */
2323
#define EC_BAT_TEMP 0x13 /* uint16_t, *100/256, °C */
2424
#define EC_AMB_TEMP 0x14 /* uint16_t, *100/256, °C */
2525
#define EC_BAT_STATUS 0x15 /* uint8_t, bitmask */
@@ -84,6 +84,119 @@ static struct power_supply olpc_ac = {
8484
.get_property = olpc_ac_get_prop,
8585
};
8686

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+
87200
/*********************************************************************
88201
* Battery properties
89202
*********************************************************************/
@@ -94,6 +207,7 @@ static int olpc_bat_get_property(struct power_supply *psy,
94207
int ret = 0;
95208
int16_t ec_word;
96209
uint8_t ec_byte;
210+
uint64_t ser_buf;
97211

98212
ret = olpc_ec_cmd(EC_BAT_STATUS, NULL, 0, &ec_byte, 1);
99213
if (ret)
@@ -110,25 +224,10 @@ static int olpc_bat_get_property(struct power_supply *psy,
110224

111225
switch (psp) {
112226
case POWER_SUPPLY_PROP_STATUS:
113-
if (olpc_platform_info.ecver > 0x44) {
114-
if (ec_byte & BAT_STAT_CHARGING)
115-
val->intval = POWER_SUPPLY_STATUS_CHARGING;
116-
else if (ec_byte & BAT_STAT_DISCHARGING)
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-
}
227+
ret = olpc_bat_get_status(val, ec_byte);
228+
if (ret)
229+
return ret;
230+
break;
132231
case POWER_SUPPLY_PROP_PRESENT:
133232
val->intval = !!(ec_byte & BAT_STAT_PRESENT);
134233
break;
@@ -137,72 +236,21 @@ static int olpc_bat_get_property(struct power_supply *psy,
137236
if (ec_byte & BAT_STAT_DESTROY)
138237
val->intval = POWER_SUPPLY_HEALTH_DEAD;
139238
else {
140-
ret = olpc_ec_cmd(EC_BAT_ERRCODE, NULL, 0, &ec_byte, 1);
239+
ret = olpc_bat_get_health(val);
141240
if (ret)
142241
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-
}
168242
}
169243
break;
170244

171245
case POWER_SUPPLY_PROP_MANUFACTURER:
172-
ec_byte = BAT_ADDR_MFR_TYPE;
173-
ret = olpc_ec_cmd(EC_BAT_EEPROM, &ec_byte, 1, &ec_byte, 1);
246+
ret = olpc_bat_get_mfr(val);
174247
if (ret)
175248
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-
}
188249
break;
189250
case POWER_SUPPLY_PROP_TECHNOLOGY:
190-
ec_byte = BAT_ADDR_MFR_TYPE;
191-
ret = olpc_ec_cmd(EC_BAT_EEPROM, &ec_byte, 1, &ec_byte, 1);
251+
ret = olpc_bat_get_tech(val);
192252
if (ret)
193253
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-
}
206254
break;
207255
case POWER_SUPPLY_PROP_VOLTAGE_AVG:
208256
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,
241289
ec_word = be16_to_cpu(ec_word);
242290
val->intval = ec_word * 100 / 256;
243291
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;
244308
default:
245309
ret = -EINVAL;
246310
break;
@@ -260,6 +324,50 @@ static enum power_supply_property olpc_bat_props[] = {
260324
POWER_SUPPLY_PROP_TEMP,
261325
POWER_SUPPLY_PROP_TEMP_AMBIENT,
262326
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,
263371
};
264372

265373
/*********************************************************************
@@ -290,8 +398,14 @@ static int __init olpc_bat_init(void)
290398

291399
if (!olpc_platform_info.ecver)
292400
return -ENXIO;
293-
if (olpc_platform_info.ecver < 0x43) {
294-
printk(KERN_NOTICE "OLPC EC version 0x%02x too old for battery driver.\n", olpc_platform_info.ecver);
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);
295409
return -ENXIO;
296410
}
297411

@@ -315,8 +429,14 @@ static int __init olpc_bat_init(void)
315429
if (ret)
316430
goto battery_failed;
317431

432+
ret = device_create_bin_file(olpc_bat.dev, &olpc_bat_eeprom);
433+
if (ret)
434+
goto eeprom_failed;
435+
318436
goto success;
319437

438+
eeprom_failed:
439+
power_supply_unregister(&olpc_bat);
320440
battery_failed:
321441
power_supply_unregister(&olpc_ac);
322442
ac_failed:
@@ -327,6 +447,7 @@ static int __init olpc_bat_init(void)
327447

328448
static void __exit olpc_bat_exit(void)
329449
{
450+
device_remove_bin_file(olpc_bat.dev, &olpc_bat_eeprom);
330451
power_supply_unregister(&olpc_bat);
331452
power_supply_unregister(&olpc_ac);
332453
platform_device_unregister(bat_pdev);

‎drivers/power/power_supply_sysfs.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ static struct device_attribute power_supply_attrs[] = {
9999
POWER_SUPPLY_ATTR(charge_empty),
100100
POWER_SUPPLY_ATTR(charge_now),
101101
POWER_SUPPLY_ATTR(charge_avg),
102+
POWER_SUPPLY_ATTR(charge_counter),
102103
POWER_SUPPLY_ATTR(energy_full_design),
103104
POWER_SUPPLY_ATTR(energy_empty_design),
104105
POWER_SUPPLY_ATTR(energy_full),

0 commit comments

Comments
 (0)