aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/power
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/power')
-rw-r--r--drivers/power/Kconfig7
-rw-r--r--drivers/power/Makefile1
-rw-r--r--drivers/power/olpc_battery.c273
-rw-r--r--drivers/power/power_supply_sysfs.c1
-rw-r--r--drivers/power/tosa_battery.c486
5 files changed, 692 insertions, 76 deletions
diff --git a/drivers/power/Kconfig b/drivers/power/Kconfig
index 4d17d384578d..9ce55850271a 100644
--- a/drivers/power/Kconfig
+++ b/drivers/power/Kconfig
@@ -49,6 +49,13 @@ config BATTERY_OLPC
49 help 49 help
50 Say Y to enable support for the battery on the OLPC laptop. 50 Say Y to enable support for the battery on the OLPC laptop.
51 51
52config 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
52config BATTERY_PALMTX 59config BATTERY_PALMTX
53 tristate "Palm T|X battery" 60 tristate "Palm T|X battery"
54 depends on MACH_PALMTX 61 depends on MACH_PALMTX
diff --git a/drivers/power/Makefile b/drivers/power/Makefile
index 6f43a54ee420..4706bf8ff459 100644
--- a/drivers/power/Makefile
+++ b/drivers/power/Makefile
@@ -20,4 +20,5 @@ obj-$(CONFIG_APM_POWER) += apm_power.o
20obj-$(CONFIG_BATTERY_DS2760) += ds2760_battery.o 20obj-$(CONFIG_BATTERY_DS2760) += ds2760_battery.o
21obj-$(CONFIG_BATTERY_PMU) += pmu_battery.o 21obj-$(CONFIG_BATTERY_PMU) += pmu_battery.o
22obj-$(CONFIG_BATTERY_OLPC) += olpc_battery.o 22obj-$(CONFIG_BATTERY_OLPC) += olpc_battery.o
23obj-$(CONFIG_BATTERY_TOSA) += tosa_battery.o
23obj-$(CONFIG_BATTERY_PALMTX) += palmtx_battery.o 24obj-$(CONFIG_BATTERY_PALMTX) += palmtx_battery.o
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
87static char bat_serial[17]; /* Ick */
88
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
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
337static 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
363static 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
438eeprom_failed:
439 power_supply_unregister(&olpc_bat);
320battery_failed: 440battery_failed:
321 power_supply_unregister(&olpc_ac); 441 power_supply_unregister(&olpc_ac);
322ac_failed: 442ac_failed:
@@ -327,6 +447,7 @@ success:
327 447
328static void __exit olpc_bat_exit(void) 448static 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);
diff --git a/drivers/power/power_supply_sysfs.c b/drivers/power/power_supply_sysfs.c
index 49215da5249b..fe2aeb11939b 100644
--- a/drivers/power/power_supply_sysfs.c
+++ b/drivers/power/power_supply_sysfs.c
@@ -99,6 +99,7 @@ static struct device_attribute power_supply_attrs[] = {
99 POWER_SUPPLY_ATTR(charge_empty), 99 POWER_SUPPLY_ATTR(charge_empty),
100 POWER_SUPPLY_ATTR(charge_now), 100 POWER_SUPPLY_ATTR(charge_now),
101 POWER_SUPPLY_ATTR(charge_avg), 101 POWER_SUPPLY_ATTR(charge_avg),
102 POWER_SUPPLY_ATTR(charge_counter),
102 POWER_SUPPLY_ATTR(energy_full_design), 103 POWER_SUPPLY_ATTR(energy_full_design),
103 POWER_SUPPLY_ATTR(energy_empty_design), 104 POWER_SUPPLY_ATTR(energy_empty_design),
104 POWER_SUPPLY_ATTR(energy_full), 105 POWER_SUPPLY_ATTR(energy_full),
diff --git a/drivers/power/tosa_battery.c b/drivers/power/tosa_battery.c
new file mode 100644
index 000000000000..bf664fbd6610
--- /dev/null
+++ b/drivers/power/tosa_battery.c
@@ -0,0 +1,486 @@
1/*
2 * Battery and Power Management code for the Sharp SL-6000x
3 *
4 * Copyright (c) 2005 Dirk Opfer
5 * Copyright (c) 2008 Dmitry Baryshkov
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 */
12#include <linux/kernel.h>
13#include <linux/module.h>
14#include <linux/power_supply.h>
15#include <linux/wm97xx.h>
16#include <linux/delay.h>
17#include <linux/spinlock.h>
18#include <linux/interrupt.h>
19#include <linux/gpio.h>
20
21#include <asm/mach-types.h>
22#include <asm/arch/tosa.h>
23
24static DEFINE_MUTEX(bat_lock); /* protects gpio pins */
25static struct work_struct bat_work;
26
27struct tosa_bat {
28 int status;
29 struct power_supply psy;
30 int full_chrg;
31
32 struct mutex work_lock; /* protects data */
33
34 bool (*is_present)(struct tosa_bat *bat);
35 int gpio_full;
36 int gpio_charge_off;
37
38 int technology;
39
40 int gpio_bat;
41 int adc_bat;
42 int adc_bat_divider;
43 int bat_max;
44 int bat_min;
45
46 int gpio_temp;
47 int adc_temp;
48 int adc_temp_divider;
49};
50
51static struct tosa_bat tosa_bat_main;
52static struct tosa_bat tosa_bat_jacket;
53
54static unsigned long tosa_read_bat(struct tosa_bat *bat)
55{
56 unsigned long value = 0;
57
58 if (bat->gpio_bat < 0 || bat->adc_bat < 0)
59 return 0;
60
61 mutex_lock(&bat_lock);
62 gpio_set_value(bat->gpio_bat, 1);
63 msleep(5);
64 value = wm97xx_read_aux_adc(bat->psy.dev->parent->driver_data,
65 bat->adc_bat);
66 gpio_set_value(bat->gpio_bat, 0);
67 mutex_unlock(&bat_lock);
68
69 value = value * 1000000 / bat->adc_bat_divider;
70
71 return value;
72}
73
74static unsigned long tosa_read_temp(struct tosa_bat *bat)
75{
76 unsigned long value = 0;
77
78 if (bat->gpio_temp < 0 || bat->adc_temp < 0)
79 return 0;
80
81 mutex_lock(&bat_lock);
82 gpio_set_value(bat->gpio_temp, 1);
83 msleep(5);
84 value = wm97xx_read_aux_adc(bat->psy.dev->parent->driver_data,
85 bat->adc_temp);
86 gpio_set_value(bat->gpio_temp, 0);
87 mutex_unlock(&bat_lock);
88
89 value = value * 10000 / bat->adc_temp_divider;
90
91 return value;
92}
93
94static int tosa_bat_get_property(struct power_supply *psy,
95 enum power_supply_property psp,
96 union power_supply_propval *val)
97{
98 int ret = 0;
99 struct tosa_bat *bat = container_of(psy, struct tosa_bat, psy);
100
101 if (bat->is_present && !bat->is_present(bat)
102 && psp != POWER_SUPPLY_PROP_PRESENT) {
103 return -ENODEV;
104 }
105
106 switch (psp) {
107 case POWER_SUPPLY_PROP_STATUS:
108 val->intval = bat->status;
109 break;
110 case POWER_SUPPLY_PROP_TECHNOLOGY:
111 val->intval = bat->technology;
112 break;
113 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
114 val->intval = tosa_read_bat(bat);
115 break;
116 case POWER_SUPPLY_PROP_VOLTAGE_MAX:
117 if (bat->full_chrg == -1)
118 val->intval = bat->bat_max;
119 else
120 val->intval = bat->full_chrg;
121 break;
122 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
123 val->intval = bat->bat_max;
124 break;
125 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
126 val->intval = bat->bat_min;
127 break;
128 case POWER_SUPPLY_PROP_TEMP:
129 val->intval = tosa_read_temp(bat);
130 break;
131 case POWER_SUPPLY_PROP_PRESENT:
132 val->intval = bat->is_present ? bat->is_present(bat) : 1;
133 break;
134 default:
135 ret = -EINVAL;
136 break;
137 }
138 return ret;
139}
140
141static bool tosa_jacket_bat_is_present(struct tosa_bat *bat)
142{
143 return gpio_get_value(TOSA_GPIO_JACKET_DETECT) == 0;
144}
145
146static void tosa_bat_external_power_changed(struct power_supply *psy)
147{
148 schedule_work(&bat_work);
149}
150
151static irqreturn_t tosa_bat_gpio_isr(int irq, void *data)
152{
153 pr_info("tosa_bat_gpio irq: %d\n", gpio_get_value(irq_to_gpio(irq)));
154 schedule_work(&bat_work);
155 return IRQ_HANDLED;
156}
157
158static void tosa_bat_update(struct tosa_bat *bat)
159{
160 int old;
161 struct power_supply *psy = &bat->psy;
162
163 mutex_lock(&bat->work_lock);
164
165 old = bat->status;
166
167 if (bat->is_present && !bat->is_present(bat)) {
168 printk(KERN_NOTICE "%s not present\n", psy->name);
169 bat->status = POWER_SUPPLY_STATUS_UNKNOWN;
170 bat->full_chrg = -1;
171 } else if (power_supply_am_i_supplied(psy)) {
172 if (bat->status == POWER_SUPPLY_STATUS_DISCHARGING) {
173 gpio_set_value(bat->gpio_charge_off, 0);
174 mdelay(15);
175 }
176
177 if (gpio_get_value(bat->gpio_full)) {
178 if (old == POWER_SUPPLY_STATUS_CHARGING ||
179 bat->full_chrg == -1)
180 bat->full_chrg = tosa_read_bat(bat);
181
182 gpio_set_value(bat->gpio_charge_off, 1);
183 bat->status = POWER_SUPPLY_STATUS_FULL;
184 } else {
185 gpio_set_value(bat->gpio_charge_off, 0);
186 bat->status = POWER_SUPPLY_STATUS_CHARGING;
187 }
188 } else {
189 gpio_set_value(bat->gpio_charge_off, 1);
190 bat->status = POWER_SUPPLY_STATUS_DISCHARGING;
191 }
192
193 if (old != bat->status)
194 power_supply_changed(psy);
195
196 mutex_unlock(&bat->work_lock);
197}
198
199static void tosa_bat_work(struct work_struct *work)
200{
201 tosa_bat_update(&tosa_bat_main);
202 tosa_bat_update(&tosa_bat_jacket);
203}
204
205
206static enum power_supply_property tosa_bat_main_props[] = {
207 POWER_SUPPLY_PROP_STATUS,
208 POWER_SUPPLY_PROP_TECHNOLOGY,
209 POWER_SUPPLY_PROP_VOLTAGE_NOW,
210 POWER_SUPPLY_PROP_VOLTAGE_MAX,
211 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
212 POWER_SUPPLY_PROP_TEMP,
213 POWER_SUPPLY_PROP_PRESENT,
214};
215
216static enum power_supply_property tosa_bat_bu_props[] = {
217 POWER_SUPPLY_PROP_STATUS,
218 POWER_SUPPLY_PROP_TECHNOLOGY,
219 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
220 POWER_SUPPLY_PROP_VOLTAGE_NOW,
221 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
222 POWER_SUPPLY_PROP_PRESENT,
223};
224
225static struct tosa_bat tosa_bat_main = {
226 .status = POWER_SUPPLY_STATUS_DISCHARGING,
227 .full_chrg = -1,
228 .psy = {
229 .name = "main-battery",
230 .type = POWER_SUPPLY_TYPE_BATTERY,
231 .properties = tosa_bat_main_props,
232 .num_properties = ARRAY_SIZE(tosa_bat_main_props),
233 .get_property = tosa_bat_get_property,
234 .external_power_changed = tosa_bat_external_power_changed,
235 .use_for_apm = 1,
236 },
237
238 .gpio_full = TOSA_GPIO_BAT0_CRG,
239 .gpio_charge_off = TOSA_GPIO_CHARGE_OFF,
240
241 .technology = POWER_SUPPLY_TECHNOLOGY_LIPO,
242
243 .gpio_bat = TOSA_GPIO_BAT0_V_ON,
244 .adc_bat = WM97XX_AUX_ID3,
245 .adc_bat_divider = 414,
246 .bat_max = 4310000,
247 .bat_min = 1551 * 1000000 / 414,
248
249 .gpio_temp = TOSA_GPIO_BAT1_TH_ON,
250 .adc_temp = WM97XX_AUX_ID2,
251 .adc_temp_divider = 10000,
252};
253
254static struct tosa_bat tosa_bat_jacket = {
255 .status = POWER_SUPPLY_STATUS_DISCHARGING,
256 .full_chrg = -1,
257 .psy = {
258 .name = "jacket-battery",
259 .type = POWER_SUPPLY_TYPE_BATTERY,
260 .properties = tosa_bat_main_props,
261 .num_properties = ARRAY_SIZE(tosa_bat_main_props),
262 .get_property = tosa_bat_get_property,
263 .external_power_changed = tosa_bat_external_power_changed,
264 },
265
266 .is_present = tosa_jacket_bat_is_present,
267 .gpio_full = TOSA_GPIO_BAT1_CRG,
268 .gpio_charge_off = TOSA_GPIO_CHARGE_OFF_JC,
269
270 .technology = POWER_SUPPLY_TECHNOLOGY_LIPO,
271
272 .gpio_bat = TOSA_GPIO_BAT1_V_ON,
273 .adc_bat = WM97XX_AUX_ID3,
274 .adc_bat_divider = 414,
275 .bat_max = 4310000,
276 .bat_min = 1551 * 1000000 / 414,
277
278 .gpio_temp = TOSA_GPIO_BAT0_TH_ON,
279 .adc_temp = WM97XX_AUX_ID2,
280 .adc_temp_divider = 10000,
281};
282
283static struct tosa_bat tosa_bat_bu = {
284 .status = POWER_SUPPLY_STATUS_UNKNOWN,
285 .full_chrg = -1,
286
287 .psy = {
288 .name = "backup-battery",
289 .type = POWER_SUPPLY_TYPE_BATTERY,
290 .properties = tosa_bat_bu_props,
291 .num_properties = ARRAY_SIZE(tosa_bat_bu_props),
292 .get_property = tosa_bat_get_property,
293 .external_power_changed = tosa_bat_external_power_changed,
294 },
295
296 .gpio_full = -1,
297 .gpio_charge_off = -1,
298
299 .technology = POWER_SUPPLY_TECHNOLOGY_LiMn,
300
301 .gpio_bat = TOSA_GPIO_BU_CHRG_ON,
302 .adc_bat = WM97XX_AUX_ID4,
303 .adc_bat_divider = 1266,
304
305 .gpio_temp = -1,
306 .adc_temp = -1,
307 .adc_temp_divider = -1,
308};
309
310static struct {
311 int gpio;
312 char *name;
313 bool output;
314 int value;
315} gpios[] = {
316 { TOSA_GPIO_CHARGE_OFF, "main charge off", 1, 1 },
317 { TOSA_GPIO_CHARGE_OFF_JC, "jacket charge off", 1, 1 },
318 { TOSA_GPIO_BAT_SW_ON, "battery switch", 1, 0 },
319 { TOSA_GPIO_BAT0_V_ON, "main battery", 1, 0 },
320 { TOSA_GPIO_BAT1_V_ON, "jacket battery", 1, 0 },
321 { TOSA_GPIO_BAT1_TH_ON, "main battery temp", 1, 0 },
322 { TOSA_GPIO_BAT0_TH_ON, "jacket battery temp", 1, 0 },
323 { TOSA_GPIO_BU_CHRG_ON, "backup battery", 1, 0 },
324 { TOSA_GPIO_BAT0_CRG, "main battery full", 0, 0 },
325 { TOSA_GPIO_BAT1_CRG, "jacket battery full", 0, 0 },
326 { TOSA_GPIO_BAT0_LOW, "main battery low", 0, 0 },
327 { TOSA_GPIO_BAT1_LOW, "jacket battery low", 0, 0 },
328 { TOSA_GPIO_JACKET_DETECT, "jacket detect", 0, 0 },
329};
330
331#ifdef CONFIG_PM
332static int tosa_bat_suspend(struct platform_device *dev, pm_message_t state)
333{
334 /* flush all pending status updates */
335 flush_scheduled_work();
336 return 0;
337}
338
339static int tosa_bat_resume(struct platform_device *dev)
340{
341 /* things may have changed while we were away */
342 schedule_work(&bat_work);
343 return 0;
344}
345#else
346#define tosa_bat_suspend NULL
347#define tosa_bat_resume NULL
348#endif
349
350static int __devinit tosa_bat_probe(struct platform_device *dev)
351{
352 int ret;
353 int i;
354
355 if (!machine_is_tosa())
356 return -ENODEV;
357
358 for (i = 0; i < ARRAY_SIZE(gpios); i++) {
359 ret = gpio_request(gpios[i].gpio, gpios[i].name);
360 if (ret) {
361 i--;
362 goto err_gpio;
363 }
364
365 if (gpios[i].output)
366 ret = gpio_direction_output(gpios[i].gpio,
367 gpios[i].value);
368 else
369 ret = gpio_direction_input(gpios[i].gpio);
370
371 if (ret)
372 goto err_gpio;
373 }
374
375 mutex_init(&tosa_bat_main.work_lock);
376 mutex_init(&tosa_bat_jacket.work_lock);
377
378 INIT_WORK(&bat_work, tosa_bat_work);
379
380 ret = power_supply_register(&dev->dev, &tosa_bat_main.psy);
381 if (ret)
382 goto err_psy_reg_main;
383 ret = power_supply_register(&dev->dev, &tosa_bat_jacket.psy);
384 if (ret)
385 goto err_psy_reg_jacket;
386 ret = power_supply_register(&dev->dev, &tosa_bat_bu.psy);
387 if (ret)
388 goto err_psy_reg_bu;
389
390 ret = request_irq(gpio_to_irq(TOSA_GPIO_BAT0_CRG),
391 tosa_bat_gpio_isr,
392 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
393 "main full", &tosa_bat_main);
394 if (ret)
395 goto err_req_main;
396
397 ret = request_irq(gpio_to_irq(TOSA_GPIO_BAT1_CRG),
398 tosa_bat_gpio_isr,
399 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
400 "jacket full", &tosa_bat_jacket);
401 if (ret)
402 goto err_req_jacket;
403
404 ret = request_irq(gpio_to_irq(TOSA_GPIO_JACKET_DETECT),
405 tosa_bat_gpio_isr,
406 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
407 "jacket detect", &tosa_bat_jacket);
408 if (!ret) {
409 schedule_work(&bat_work);
410 return 0;
411 }
412
413 free_irq(gpio_to_irq(TOSA_GPIO_BAT1_CRG), &tosa_bat_jacket);
414err_req_jacket:
415 free_irq(gpio_to_irq(TOSA_GPIO_BAT0_CRG), &tosa_bat_main);
416err_req_main:
417 power_supply_unregister(&tosa_bat_bu.psy);
418err_psy_reg_bu:
419 power_supply_unregister(&tosa_bat_jacket.psy);
420err_psy_reg_jacket:
421 power_supply_unregister(&tosa_bat_main.psy);
422err_psy_reg_main:
423
424 /* see comment in tosa_bat_remove */
425 flush_scheduled_work();
426
427 i--;
428err_gpio:
429 for (; i >= 0; i--)
430 gpio_free(gpios[i].gpio);
431
432 return ret;
433}
434
435static int __devexit tosa_bat_remove(struct platform_device *dev)
436{
437 int i;
438
439 free_irq(gpio_to_irq(TOSA_GPIO_JACKET_DETECT), &tosa_bat_jacket);
440 free_irq(gpio_to_irq(TOSA_GPIO_BAT1_CRG), &tosa_bat_jacket);
441 free_irq(gpio_to_irq(TOSA_GPIO_BAT0_CRG), &tosa_bat_main);
442
443 power_supply_unregister(&tosa_bat_bu.psy);
444 power_supply_unregister(&tosa_bat_jacket.psy);
445 power_supply_unregister(&tosa_bat_main.psy);
446
447 /*
448 * now flush all pending work.
449 * we won't get any more schedules, since all
450 * sources (isr and external_power_changed)
451 * are unregistered now.
452 */
453 flush_scheduled_work();
454
455 for (i = ARRAY_SIZE(gpios) - 1; i >= 0; i--)
456 gpio_free(gpios[i].gpio);
457
458 return 0;
459}
460
461static struct platform_driver tosa_bat_driver = {
462 .driver.name = "wm97xx-battery",
463 .driver.owner = THIS_MODULE,
464 .probe = tosa_bat_probe,
465 .remove = __devexit_p(tosa_bat_remove),
466 .suspend = tosa_bat_suspend,
467 .resume = tosa_bat_resume,
468};
469
470static int __init tosa_bat_init(void)
471{
472 return platform_driver_register(&tosa_bat_driver);
473}
474
475static void __exit tosa_bat_exit(void)
476{
477 platform_driver_unregister(&tosa_bat_driver);
478}
479
480module_init(tosa_bat_init);
481module_exit(tosa_bat_exit);
482
483MODULE_LICENSE("GPL");
484MODULE_AUTHOR("Dmitry Baryshkov");
485MODULE_DESCRIPTION("Tosa battery driver");
486MODULE_ALIAS("platform:wm97xx-battery");