aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/power
diff options
context:
space:
mode:
authorAnton Vorontsov <cbouatmailru@gmail.com>2011-02-28 09:37:13 -0500
committerAnton Vorontsov <cbouatmailru@gmail.com>2011-02-28 09:37:13 -0500
commitb5db7cde69f87178fb771970a2b512a452a9d680 (patch)
tree9936cb9f2fa9334ae770958fdbf18d42284fcc34 /drivers/power
parentd6ccc442b12102414c1343f0adacaa8a1aaa516c (diff)
parent9e912f452931b3da9d13d716a381ba0d5fb74ced (diff)
Merge branch 'bq27x00-for-upstream' of git://git.metafoo.de/linux-2.6
Diffstat (limited to 'drivers/power')
-rw-r--r--drivers/power/Kconfig14
-rw-r--r--drivers/power/bq27x00_battery.c725
-rw-r--r--drivers/power/power_supply_core.c4
-rw-r--r--drivers/power/power_supply_sysfs.c2
4 files changed, 574 insertions, 171 deletions
diff --git a/drivers/power/Kconfig b/drivers/power/Kconfig
index 61bf5d724139..52a462fc6b84 100644
--- a/drivers/power/Kconfig
+++ b/drivers/power/Kconfig
@@ -117,10 +117,24 @@ config BATTERY_BQ20Z75
117 117
118config BATTERY_BQ27x00 118config BATTERY_BQ27x00
119 tristate "BQ27x00 battery driver" 119 tristate "BQ27x00 battery driver"
120 help
121 Say Y here to enable support for batteries with BQ27x00 (I2C/HDQ) chips.
122
123config BATTERY_BQ27X00_I2C
124 bool "BQ27200/BQ27500 support"
125 depends on BATTERY_BQ27x00
120 depends on I2C 126 depends on I2C
127 default y
121 help 128 help
122 Say Y here to enable support for batteries with BQ27x00 (I2C) chips. 129 Say Y here to enable support for batteries with BQ27x00 (I2C) chips.
123 130
131config BATTERY_BQ27X00_PLATFORM
132 bool "BQ27000 support"
133 depends on BATTERY_BQ27x00
134 default y
135 help
136 Say Y here to enable support for batteries with BQ27000 (HDQ) chips.
137
124config BATTERY_DA9030 138config BATTERY_DA9030
125 tristate "DA9030 battery driver" 139 tristate "DA9030 battery driver"
126 depends on PMIC_DA903X 140 depends on PMIC_DA903X
diff --git a/drivers/power/bq27x00_battery.c b/drivers/power/bq27x00_battery.c
index eff0273d4030..59e68dbd028b 100644
--- a/drivers/power/bq27x00_battery.c
+++ b/drivers/power/bq27x00_battery.c
@@ -3,6 +3,7 @@
3 * 3 *
4 * Copyright (C) 2008 Rodolfo Giometti <giometti@linux.it> 4 * Copyright (C) 2008 Rodolfo Giometti <giometti@linux.it>
5 * Copyright (C) 2008 Eurotech S.p.A. <info@eurotech.it> 5 * Copyright (C) 2008 Eurotech S.p.A. <info@eurotech.it>
6 * Copyright (C) 2010-2011 Lars-Peter Clausen <lars@metafoo.de>
6 * 7 *
7 * Based on a previous work by Copyright (C) 2008 Texas Instruments, Inc. 8 * Based on a previous work by Copyright (C) 2008 Texas Instruments, Inc.
8 * 9 *
@@ -15,6 +16,13 @@
15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 16 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16 * 17 *
17 */ 18 */
19
20/*
21 * Datasheets:
22 * http://focus.ti.com/docs/prod/folders/print/bq27000.html
23 * http://focus.ti.com/docs/prod/folders/print/bq27500.html
24 */
25
18#include <linux/module.h> 26#include <linux/module.h>
19#include <linux/param.h> 27#include <linux/param.h>
20#include <linux/jiffies.h> 28#include <linux/jiffies.h>
@@ -27,7 +35,9 @@
27#include <linux/slab.h> 35#include <linux/slab.h>
28#include <asm/unaligned.h> 36#include <asm/unaligned.h>
29 37
30#define DRIVER_VERSION "1.1.0" 38#include <linux/power/bq27x00_battery.h>
39
40#define DRIVER_VERSION "1.2.0"
31 41
32#define BQ27x00_REG_TEMP 0x06 42#define BQ27x00_REG_TEMP 0x06
33#define BQ27x00_REG_VOLT 0x08 43#define BQ27x00_REG_VOLT 0x08
@@ -36,36 +46,59 @@
36#define BQ27x00_REG_TTE 0x16 46#define BQ27x00_REG_TTE 0x16
37#define BQ27x00_REG_TTF 0x18 47#define BQ27x00_REG_TTF 0x18
38#define BQ27x00_REG_TTECP 0x26 48#define BQ27x00_REG_TTECP 0x26
49#define BQ27x00_REG_NAC 0x0C /* Nominal available capaciy */
50#define BQ27x00_REG_LMD 0x12 /* Last measured discharge */
51#define BQ27x00_REG_CYCT 0x2A /* Cycle count total */
52#define BQ27x00_REG_AE 0x22 /* Available enery */
39 53
40#define BQ27000_REG_RSOC 0x0B /* Relative State-of-Charge */ 54#define BQ27000_REG_RSOC 0x0B /* Relative State-of-Charge */
55#define BQ27000_REG_ILMD 0x76 /* Initial last measured discharge */
41#define BQ27000_FLAG_CHGS BIT(7) 56#define BQ27000_FLAG_CHGS BIT(7)
57#define BQ27000_FLAG_FC BIT(5)
42 58
43#define BQ27500_REG_SOC 0x2c 59#define BQ27500_REG_SOC 0x2C
60#define BQ27500_REG_DCAP 0x3C /* Design capacity */
44#define BQ27500_FLAG_DSC BIT(0) 61#define BQ27500_FLAG_DSC BIT(0)
45#define BQ27500_FLAG_FC BIT(9) 62#define BQ27500_FLAG_FC BIT(9)
46 63
47/* If the system has several batteries we need a different name for each 64#define BQ27000_RS 20 /* Resistor sense */
48 * of them...
49 */
50static DEFINE_IDR(battery_id);
51static DEFINE_MUTEX(battery_mutex);
52 65
53struct bq27x00_device_info; 66struct bq27x00_device_info;
54struct bq27x00_access_methods { 67struct bq27x00_access_methods {
55 int (*read)(u8 reg, int *rt_value, int b_single, 68 int (*read)(struct bq27x00_device_info *di, u8 reg, bool single);
56 struct bq27x00_device_info *di);
57}; 69};
58 70
59enum bq27x00_chip { BQ27000, BQ27500 }; 71enum bq27x00_chip { BQ27000, BQ27500 };
60 72
73struct bq27x00_reg_cache {
74 int temperature;
75 int time_to_empty;
76 int time_to_empty_avg;
77 int time_to_full;
78 int charge_full;
79 int charge_counter;
80 int capacity;
81 int flags;
82
83 int current_now;
84};
85
61struct bq27x00_device_info { 86struct bq27x00_device_info {
62 struct device *dev; 87 struct device *dev;
63 int id; 88 int id;
64 struct bq27x00_access_methods *bus;
65 struct power_supply bat;
66 enum bq27x00_chip chip; 89 enum bq27x00_chip chip;
67 90
68 struct i2c_client *client; 91 struct bq27x00_reg_cache cache;
92 int charge_design_full;
93
94 unsigned long last_update;
95 struct delayed_work work;
96
97 struct power_supply bat;
98
99 struct bq27x00_access_methods bus;
100
101 struct mutex lock;
69}; 102};
70 103
71static enum power_supply_property bq27x00_battery_props[] = { 104static enum power_supply_property bq27x00_battery_props[] = {
@@ -78,164 +111,328 @@ static enum power_supply_property bq27x00_battery_props[] = {
78 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, 111 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
79 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, 112 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
80 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW, 113 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
114 POWER_SUPPLY_PROP_TECHNOLOGY,
115 POWER_SUPPLY_PROP_CHARGE_FULL,
116 POWER_SUPPLY_PROP_CHARGE_NOW,
117 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
118 POWER_SUPPLY_PROP_CHARGE_COUNTER,
119 POWER_SUPPLY_PROP_ENERGY_NOW,
81}; 120};
82 121
122static unsigned int poll_interval = 360;
123module_param(poll_interval, uint, 0644);
124MODULE_PARM_DESC(poll_interval, "battery poll interval in seconds - " \
125 "0 disables polling");
126
83/* 127/*
84 * Common code for BQ27x00 devices 128 * Common code for BQ27x00 devices
85 */ 129 */
86 130
87static int bq27x00_read(u8 reg, int *rt_value, int b_single, 131static inline int bq27x00_read(struct bq27x00_device_info *di, u8 reg,
88 struct bq27x00_device_info *di) 132 bool single)
89{ 133{
90 return di->bus->read(reg, rt_value, b_single, di); 134 return di->bus.read(di, reg, single);
91} 135}
92 136
93/* 137/*
94 * Return the battery temperature in tenths of degree Celsius 138 * Return the battery Relative State-of-Charge
95 * Or < 0 if something fails. 139 * Or < 0 if something fails.
96 */ 140 */
97static int bq27x00_battery_temperature(struct bq27x00_device_info *di) 141static int bq27x00_battery_read_rsoc(struct bq27x00_device_info *di)
98{ 142{
99 int ret; 143 int rsoc;
100 int temp = 0;
101 144
102 ret = bq27x00_read(BQ27x00_REG_TEMP, &temp, 0, di); 145 if (di->chip == BQ27500)
103 if (ret) { 146 rsoc = bq27x00_read(di, BQ27500_REG_SOC, false);
104 dev_err(di->dev, "error reading temperature\n"); 147 else
105 return ret; 148 rsoc = bq27x00_read(di, BQ27000_REG_RSOC, true);
149
150 if (rsoc < 0)
151 dev_err(di->dev, "error reading relative State-of-Charge\n");
152
153 return rsoc;
154}
155
156/*
157 * Return a battery charge value in µAh
158 * Or < 0 if something fails.
159 */
160static int bq27x00_battery_read_charge(struct bq27x00_device_info *di, u8 reg)
161{
162 int charge;
163
164 charge = bq27x00_read(di, reg, false);
165 if (charge < 0) {
166 dev_err(di->dev, "error reading nominal available capacity\n");
167 return charge;
106 } 168 }
107 169
108 if (di->chip == BQ27500) 170 if (di->chip == BQ27500)
109 return temp - 2731; 171 charge *= 1000;
110 else 172 else
111 return ((temp >> 2) - 273) * 10; 173 charge = charge * 3570 / BQ27000_RS;
174
175 return charge;
112} 176}
113 177
114/* 178/*
115 * Return the battery Voltage in milivolts 179 * Return the battery Nominal available capaciy in µAh
116 * Or < 0 if something fails. 180 * Or < 0 if something fails.
117 */ 181 */
118static int bq27x00_battery_voltage(struct bq27x00_device_info *di) 182static inline int bq27x00_battery_read_nac(struct bq27x00_device_info *di)
119{ 183{
120 int ret; 184 return bq27x00_battery_read_charge(di, BQ27x00_REG_NAC);
121 int volt = 0; 185}
122 186
123 ret = bq27x00_read(BQ27x00_REG_VOLT, &volt, 0, di); 187/*
124 if (ret) { 188 * Return the battery Last measured discharge in µAh
125 dev_err(di->dev, "error reading voltage\n"); 189 * Or < 0 if something fails.
126 return ret; 190 */
191static inline int bq27x00_battery_read_lmd(struct bq27x00_device_info *di)
192{
193 return bq27x00_battery_read_charge(di, BQ27x00_REG_LMD);
194}
195
196/*
197 * Return the battery Initial last measured discharge in µAh
198 * Or < 0 if something fails.
199 */
200static int bq27x00_battery_read_ilmd(struct bq27x00_device_info *di)
201{
202 int ilmd;
203
204 if (di->chip == BQ27500)
205 ilmd = bq27x00_read(di, BQ27500_REG_DCAP, false);
206 else
207 ilmd = bq27x00_read(di, BQ27000_REG_ILMD, true);
208
209 if (ilmd < 0) {
210 dev_err(di->dev, "error reading initial last measured discharge\n");
211 return ilmd;
127 } 212 }
128 213
129 return volt * 1000; 214 if (di->chip == BQ27500)
215 ilmd *= 1000;
216 else
217 ilmd = ilmd * 256 * 3570 / BQ27000_RS;
218
219 return ilmd;
130} 220}
131 221
132/* 222/*
133 * Return the battery average current 223 * Return the battery Cycle count total
134 * Note that current can be negative signed as well 224 * Or < 0 if something fails.
135 * Or 0 if something fails.
136 */ 225 */
137static int bq27x00_battery_current(struct bq27x00_device_info *di) 226static int bq27x00_battery_read_cyct(struct bq27x00_device_info *di)
138{ 227{
139 int ret; 228 int cyct;
140 int curr = 0;
141 int flags = 0;
142 229
143 ret = bq27x00_read(BQ27x00_REG_AI, &curr, 0, di); 230 cyct = bq27x00_read(di, BQ27x00_REG_CYCT, false);
144 if (ret) { 231 if (cyct < 0)
145 dev_err(di->dev, "error reading current\n"); 232 dev_err(di->dev, "error reading cycle count total\n");
146 return 0; 233
234 return cyct;
235}
236
237/*
238 * Read a time register.
239 * Return < 0 if something fails.
240 */
241static int bq27x00_battery_read_time(struct bq27x00_device_info *di, u8 reg)
242{
243 int tval;
244
245 tval = bq27x00_read(di, reg, false);
246 if (tval < 0) {
247 dev_err(di->dev, "error reading register %02x: %d\n", reg, tval);
248 return tval;
147 } 249 }
148 250
149 if (di->chip == BQ27500) { 251 if (tval == 65535)
150 /* bq27500 returns signed value */ 252 return -ENODATA;
151 curr = (int)(s16)curr; 253
152 } else { 254 return tval * 60;
153 ret = bq27x00_read(BQ27x00_REG_FLAGS, &flags, 0, di); 255}
154 if (ret < 0) { 256
155 dev_err(di->dev, "error reading flags\n"); 257static void bq27x00_update(struct bq27x00_device_info *di)
156 return 0; 258{
157 } 259 struct bq27x00_reg_cache cache = {0, };
158 if (flags & BQ27000_FLAG_CHGS) { 260 bool is_bq27500 = di->chip == BQ27500;
159 dev_dbg(di->dev, "negative current!\n"); 261
160 curr = -curr; 262 cache.flags = bq27x00_read(di, BQ27x00_REG_FLAGS, is_bq27500);
161 } 263 if (cache.flags >= 0) {
264 cache.capacity = bq27x00_battery_read_rsoc(di);
265 cache.temperature = bq27x00_read(di, BQ27x00_REG_TEMP, false);
266 cache.time_to_empty = bq27x00_battery_read_time(di, BQ27x00_REG_TTE);
267 cache.time_to_empty_avg = bq27x00_battery_read_time(di, BQ27x00_REG_TTECP);
268 cache.time_to_full = bq27x00_battery_read_time(di, BQ27x00_REG_TTF);
269 cache.charge_full = bq27x00_battery_read_lmd(di);
270 cache.charge_counter = bq27x00_battery_read_cyct(di);
271
272 if (!is_bq27500)
273 cache.current_now = bq27x00_read(di, BQ27x00_REG_AI, false);
274
275 /* We only have to read charge design full once */
276 if (di->charge_design_full <= 0)
277 di->charge_design_full = bq27x00_battery_read_ilmd(di);
278 }
279
280 /* Ignore current_now which is a snapshot of the current battery state
281 * and is likely to be different even between two consecutive reads */
282 if (memcmp(&di->cache, &cache, sizeof(cache) - sizeof(int)) != 0) {
283 di->cache = cache;
284 power_supply_changed(&di->bat);
162 } 285 }
163 286
164 return curr * 1000; 287 di->last_update = jiffies;
288}
289
290static void bq27x00_battery_poll(struct work_struct *work)
291{
292 struct bq27x00_device_info *di =
293 container_of(work, struct bq27x00_device_info, work.work);
294
295 bq27x00_update(di);
296
297 if (poll_interval > 0) {
298 /* The timer does not have to be accurate. */
299 set_timer_slack(&di->work.timer, poll_interval * HZ / 4);
300 schedule_delayed_work(&di->work, poll_interval * HZ);
301 }
165} 302}
166 303
304
167/* 305/*
168 * Return the battery Relative State-of-Charge 306 * Return the battery temperature in tenths of degree Celsius
169 * Or < 0 if something fails. 307 * Or < 0 if something fails.
170 */ 308 */
171static int bq27x00_battery_rsoc(struct bq27x00_device_info *di) 309static int bq27x00_battery_temperature(struct bq27x00_device_info *di,
310 union power_supply_propval *val)
172{ 311{
173 int ret; 312 if (di->cache.temperature < 0)
174 int rsoc = 0; 313 return di->cache.temperature;
175 314
176 if (di->chip == BQ27500) 315 if (di->chip == BQ27500)
177 ret = bq27x00_read(BQ27500_REG_SOC, &rsoc, 0, di); 316 val->intval = di->cache.temperature - 2731;
178 else 317 else
179 ret = bq27x00_read(BQ27000_REG_RSOC, &rsoc, 1, di); 318 val->intval = ((di->cache.temperature * 5) - 5463) / 2;
180 if (ret) { 319
181 dev_err(di->dev, "error reading relative State-of-Charge\n"); 320 return 0;
182 return ret; 321}
322
323/*
324 * Return the battery average current in µA
325 * Note that current can be negative signed as well
326 * Or 0 if something fails.
327 */
328static int bq27x00_battery_current(struct bq27x00_device_info *di,
329 union power_supply_propval *val)
330{
331 int curr;
332
333 if (di->chip == BQ27500)
334 curr = bq27x00_read(di, BQ27x00_REG_AI, false);
335 else
336 curr = di->cache.current_now;
337
338 if (curr < 0)
339 return curr;
340
341 if (di->chip == BQ27500) {
342 /* bq27500 returns signed value */
343 val->intval = (int)((s16)curr) * 1000;
344 } else {
345 if (di->cache.flags & BQ27000_FLAG_CHGS) {
346 dev_dbg(di->dev, "negative current!\n");
347 curr = -curr;
348 }
349
350 val->intval = curr * 3570 / BQ27000_RS;
183 } 351 }
184 352
185 return rsoc; 353 return 0;
186} 354}
187 355
188static int bq27x00_battery_status(struct bq27x00_device_info *di, 356static int bq27x00_battery_status(struct bq27x00_device_info *di,
189 union power_supply_propval *val) 357 union power_supply_propval *val)
190{ 358{
191 int flags = 0;
192 int status; 359 int status;
193 int ret;
194
195 ret = bq27x00_read(BQ27x00_REG_FLAGS, &flags, 0, di);
196 if (ret < 0) {
197 dev_err(di->dev, "error reading flags\n");
198 return ret;
199 }
200 360
201 if (di->chip == BQ27500) { 361 if (di->chip == BQ27500) {
202 if (flags & BQ27500_FLAG_FC) 362 if (di->cache.flags & BQ27500_FLAG_FC)
203 status = POWER_SUPPLY_STATUS_FULL; 363 status = POWER_SUPPLY_STATUS_FULL;
204 else if (flags & BQ27500_FLAG_DSC) 364 else if (di->cache.flags & BQ27500_FLAG_DSC)
205 status = POWER_SUPPLY_STATUS_DISCHARGING; 365 status = POWER_SUPPLY_STATUS_DISCHARGING;
206 else 366 else
207 status = POWER_SUPPLY_STATUS_CHARGING; 367 status = POWER_SUPPLY_STATUS_CHARGING;
208 } else { 368 } else {
209 if (flags & BQ27000_FLAG_CHGS) 369 if (di->cache.flags & BQ27000_FLAG_FC)
370 status = POWER_SUPPLY_STATUS_FULL;
371 else if (di->cache.flags & BQ27000_FLAG_CHGS)
210 status = POWER_SUPPLY_STATUS_CHARGING; 372 status = POWER_SUPPLY_STATUS_CHARGING;
373 else if (power_supply_am_i_supplied(&di->bat))
374 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
211 else 375 else
212 status = POWER_SUPPLY_STATUS_DISCHARGING; 376 status = POWER_SUPPLY_STATUS_DISCHARGING;
213 } 377 }
214 378
215 val->intval = status; 379 val->intval = status;
380
216 return 0; 381 return 0;
217} 382}
218 383
219/* 384/*
220 * Read a time register. 385 * Return the battery Voltage in milivolts
221 * Return < 0 if something fails. 386 * Or < 0 if something fails.
222 */ 387 */
223static int bq27x00_battery_time(struct bq27x00_device_info *di, int reg, 388static int bq27x00_battery_voltage(struct bq27x00_device_info *di,
224 union power_supply_propval *val) 389 union power_supply_propval *val)
225{ 390{
226 int tval = 0; 391 int volt;
227 int ret;
228 392
229 ret = bq27x00_read(reg, &tval, 0, di); 393 volt = bq27x00_read(di, BQ27x00_REG_VOLT, false);
230 if (ret) { 394 if (volt < 0)
231 dev_err(di->dev, "error reading register %02x\n", reg); 395 return volt;
232 return ret; 396
397 val->intval = volt * 1000;
398
399 return 0;
400}
401
402/*
403 * Return the battery Available energy in µWh
404 * Or < 0 if something fails.
405 */
406static int bq27x00_battery_energy(struct bq27x00_device_info *di,
407 union power_supply_propval *val)
408{
409 int ae;
410
411 ae = bq27x00_read(di, BQ27x00_REG_AE, false);
412 if (ae < 0) {
413 dev_err(di->dev, "error reading available energy\n");
414 return ae;
233 } 415 }
234 416
235 if (tval == 65535) 417 if (di->chip == BQ27500)
236 return -ENODATA; 418 ae *= 1000;
419 else
420 ae = ae * 29200 / BQ27000_RS;
421
422 val->intval = ae;
423
424 return 0;
425}
426
427
428static int bq27x00_simple_value(int value,
429 union power_supply_propval *val)
430{
431 if (value < 0)
432 return value;
433
434 val->intval = value;
237 435
238 val->intval = tval * 60;
239 return 0; 436 return 0;
240} 437}
241 438
@@ -249,33 +446,61 @@ static int bq27x00_battery_get_property(struct power_supply *psy,
249 int ret = 0; 446 int ret = 0;
250 struct bq27x00_device_info *di = to_bq27x00_device_info(psy); 447 struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
251 448
449 mutex_lock(&di->lock);
450 if (time_is_before_jiffies(di->last_update + 5 * HZ)) {
451 cancel_delayed_work_sync(&di->work);
452 bq27x00_battery_poll(&di->work.work);
453 }
454 mutex_unlock(&di->lock);
455
456 if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0)
457 return -ENODEV;
458
252 switch (psp) { 459 switch (psp) {
253 case POWER_SUPPLY_PROP_STATUS: 460 case POWER_SUPPLY_PROP_STATUS:
254 ret = bq27x00_battery_status(di, val); 461 ret = bq27x00_battery_status(di, val);
255 break; 462 break;
256 case POWER_SUPPLY_PROP_VOLTAGE_NOW: 463 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
464 ret = bq27x00_battery_voltage(di, val);
465 break;
257 case POWER_SUPPLY_PROP_PRESENT: 466 case POWER_SUPPLY_PROP_PRESENT:
258 val->intval = bq27x00_battery_voltage(di); 467 val->intval = di->cache.flags < 0 ? 0 : 1;
259 if (psp == POWER_SUPPLY_PROP_PRESENT)
260 val->intval = val->intval <= 0 ? 0 : 1;
261 break; 468 break;
262 case POWER_SUPPLY_PROP_CURRENT_NOW: 469 case POWER_SUPPLY_PROP_CURRENT_NOW:
263 val->intval = bq27x00_battery_current(di); 470 ret = bq27x00_battery_current(di, val);
264 break; 471 break;
265 case POWER_SUPPLY_PROP_CAPACITY: 472 case POWER_SUPPLY_PROP_CAPACITY:
266 val->intval = bq27x00_battery_rsoc(di); 473 ret = bq27x00_simple_value(di->cache.capacity, val);
267 break; 474 break;
268 case POWER_SUPPLY_PROP_TEMP: 475 case POWER_SUPPLY_PROP_TEMP:
269 val->intval = bq27x00_battery_temperature(di); 476 ret = bq27x00_battery_temperature(di, val);
270 break; 477 break;
271 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW: 478 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
272 ret = bq27x00_battery_time(di, BQ27x00_REG_TTE, val); 479 ret = bq27x00_simple_value(di->cache.time_to_empty, val);
273 break; 480 break;
274 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG: 481 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
275 ret = bq27x00_battery_time(di, BQ27x00_REG_TTECP, val); 482 ret = bq27x00_simple_value(di->cache.time_to_empty_avg, val);
276 break; 483 break;
277 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW: 484 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
278 ret = bq27x00_battery_time(di, BQ27x00_REG_TTF, val); 485 ret = bq27x00_simple_value(di->cache.time_to_full, val);
486 break;
487 case POWER_SUPPLY_PROP_TECHNOLOGY:
488 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
489 break;
490 case POWER_SUPPLY_PROP_CHARGE_NOW:
491 ret = bq27x00_simple_value(bq27x00_battery_read_nac(di), val);
492 break;
493 case POWER_SUPPLY_PROP_CHARGE_FULL:
494 ret = bq27x00_simple_value(di->cache.charge_full, val);
495 break;
496 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
497 ret = bq27x00_simple_value(di->charge_design_full, val);
498 break;
499 case POWER_SUPPLY_PROP_CHARGE_COUNTER:
500 ret = bq27x00_simple_value(di->cache.charge_counter, val);
501 break;
502 case POWER_SUPPLY_PROP_ENERGY_NOW:
503 ret = bq27x00_battery_energy(di, val);
279 break; 504 break;
280 default: 505 default:
281 return -EINVAL; 506 return -EINVAL;
@@ -284,56 +509,91 @@ static int bq27x00_battery_get_property(struct power_supply *psy,
284 return ret; 509 return ret;
285} 510}
286 511
287static void bq27x00_powersupply_init(struct bq27x00_device_info *di) 512static void bq27x00_external_power_changed(struct power_supply *psy)
288{ 513{
514 struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
515
516 cancel_delayed_work_sync(&di->work);
517 schedule_delayed_work(&di->work, 0);
518}
519
520static int bq27x00_powersupply_init(struct bq27x00_device_info *di)
521{
522 int ret;
523
289 di->bat.type = POWER_SUPPLY_TYPE_BATTERY; 524 di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
290 di->bat.properties = bq27x00_battery_props; 525 di->bat.properties = bq27x00_battery_props;
291 di->bat.num_properties = ARRAY_SIZE(bq27x00_battery_props); 526 di->bat.num_properties = ARRAY_SIZE(bq27x00_battery_props);
292 di->bat.get_property = bq27x00_battery_get_property; 527 di->bat.get_property = bq27x00_battery_get_property;
293 di->bat.external_power_changed = NULL; 528 di->bat.external_power_changed = bq27x00_external_power_changed;
529
530 INIT_DELAYED_WORK(&di->work, bq27x00_battery_poll);
531 mutex_init(&di->lock);
532
533 ret = power_supply_register(di->dev, &di->bat);
534 if (ret) {
535 dev_err(di->dev, "failed to register battery: %d\n", ret);
536 return ret;
537 }
538
539 dev_info(di->dev, "support ver. %s enabled\n", DRIVER_VERSION);
540
541 bq27x00_update(di);
542
543 return 0;
294} 544}
295 545
296/* 546static void bq27x00_powersupply_unregister(struct bq27x00_device_info *di)
297 * i2c specific code 547{
548 cancel_delayed_work_sync(&di->work);
549
550 power_supply_unregister(&di->bat);
551
552 mutex_destroy(&di->lock);
553}
554
555
556/* i2c specific code */
557#ifdef CONFIG_BATTERY_BQ27X00_I2C
558
559/* If the system has several batteries we need a different name for each
560 * of them...
298 */ 561 */
562static DEFINE_IDR(battery_id);
563static DEFINE_MUTEX(battery_mutex);
299 564
300static int bq27x00_read_i2c(u8 reg, int *rt_value, int b_single, 565static int bq27x00_read_i2c(struct bq27x00_device_info *di, u8 reg, bool single)
301 struct bq27x00_device_info *di)
302{ 566{
303 struct i2c_client *client = di->client; 567 struct i2c_client *client = to_i2c_client(di->dev);
304 struct i2c_msg msg[1]; 568 struct i2c_msg msg[2];
305 unsigned char data[2]; 569 unsigned char data[2];
306 int err; 570 int ret;
307 571
308 if (!client->adapter) 572 if (!client->adapter)
309 return -ENODEV; 573 return -ENODEV;
310 574
311 msg->addr = client->addr; 575 msg[0].addr = client->addr;
312 msg->flags = 0; 576 msg[0].flags = 0;
313 msg->len = 1; 577 msg[0].buf = &reg;
314 msg->buf = data; 578 msg[0].len = sizeof(reg);
315 579 msg[1].addr = client->addr;
316 data[0] = reg; 580 msg[1].flags = I2C_M_RD;
317 err = i2c_transfer(client->adapter, msg, 1); 581 msg[1].buf = data;
582 if (single)
583 msg[1].len = 1;
584 else
585 msg[1].len = 2;
318 586
319 if (err >= 0) { 587 ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
320 if (!b_single) 588 if (ret < 0)
321 msg->len = 2; 589 return ret;
322 else
323 msg->len = 1;
324 590
325 msg->flags = I2C_M_RD; 591 if (!single)
326 err = i2c_transfer(client->adapter, msg, 1); 592 ret = get_unaligned_le16(data);
327 if (err >= 0) { 593 else
328 if (!b_single) 594 ret = data[0];
329 *rt_value = get_unaligned_le16(data);
330 else
331 *rt_value = data[0];
332 595
333 return 0; 596 return ret;
334 }
335 }
336 return err;
337} 597}
338 598
339static int bq27x00_battery_probe(struct i2c_client *client, 599static int bq27x00_battery_probe(struct i2c_client *client,
@@ -341,7 +601,6 @@ static int bq27x00_battery_probe(struct i2c_client *client,
341{ 601{
342 char *name; 602 char *name;
343 struct bq27x00_device_info *di; 603 struct bq27x00_device_info *di;
344 struct bq27x00_access_methods *bus;
345 int num; 604 int num;
346 int retval = 0; 605 int retval = 0;
347 606
@@ -368,38 +627,20 @@ static int bq27x00_battery_probe(struct i2c_client *client,
368 retval = -ENOMEM; 627 retval = -ENOMEM;
369 goto batt_failed_2; 628 goto batt_failed_2;
370 } 629 }
630
371 di->id = num; 631 di->id = num;
632 di->dev = &client->dev;
372 di->chip = id->driver_data; 633 di->chip = id->driver_data;
634 di->bat.name = name;
635 di->bus.read = &bq27x00_read_i2c;
373 636
374 bus = kzalloc(sizeof(*bus), GFP_KERNEL); 637 if (bq27x00_powersupply_init(di))
375 if (!bus) {
376 dev_err(&client->dev, "failed to allocate access method "
377 "data\n");
378 retval = -ENOMEM;
379 goto batt_failed_3; 638 goto batt_failed_3;
380 }
381 639
382 i2c_set_clientdata(client, di); 640 i2c_set_clientdata(client, di);
383 di->dev = &client->dev;
384 di->bat.name = name;
385 bus->read = &bq27x00_read_i2c;
386 di->bus = bus;
387 di->client = client;
388
389 bq27x00_powersupply_init(di);
390
391 retval = power_supply_register(&client->dev, &di->bat);
392 if (retval) {
393 dev_err(&client->dev, "failed to register battery\n");
394 goto batt_failed_4;
395 }
396
397 dev_info(&client->dev, "support ver. %s enabled\n", DRIVER_VERSION);
398 641
399 return 0; 642 return 0;
400 643
401batt_failed_4:
402 kfree(bus);
403batt_failed_3: 644batt_failed_3:
404 kfree(di); 645 kfree(di);
405batt_failed_2: 646batt_failed_2:
@@ -416,9 +657,8 @@ static int bq27x00_battery_remove(struct i2c_client *client)
416{ 657{
417 struct bq27x00_device_info *di = i2c_get_clientdata(client); 658 struct bq27x00_device_info *di = i2c_get_clientdata(client);
418 659
419 power_supply_unregister(&di->bat); 660 bq27x00_powersupply_unregister(di);
420 661
421 kfree(di->bus);
422 kfree(di->bat.name); 662 kfree(di->bat.name);
423 663
424 mutex_lock(&battery_mutex); 664 mutex_lock(&battery_mutex);
@@ -430,15 +670,12 @@ static int bq27x00_battery_remove(struct i2c_client *client)
430 return 0; 670 return 0;
431} 671}
432 672
433/*
434 * Module stuff
435 */
436
437static const struct i2c_device_id bq27x00_id[] = { 673static const struct i2c_device_id bq27x00_id[] = {
438 { "bq27200", BQ27000 }, /* bq27200 is same as bq27000, but with i2c */ 674 { "bq27200", BQ27000 }, /* bq27200 is same as bq27000, but with i2c */
439 { "bq27500", BQ27500 }, 675 { "bq27500", BQ27500 },
440 {}, 676 {},
441}; 677};
678MODULE_DEVICE_TABLE(i2c, bq27x00_id);
442 679
443static struct i2c_driver bq27x00_battery_driver = { 680static struct i2c_driver bq27x00_battery_driver = {
444 .driver = { 681 .driver = {
@@ -449,13 +686,164 @@ static struct i2c_driver bq27x00_battery_driver = {
449 .id_table = bq27x00_id, 686 .id_table = bq27x00_id,
450}; 687};
451 688
689static inline int bq27x00_battery_i2c_init(void)
690{
691 int ret = i2c_add_driver(&bq27x00_battery_driver);
692 if (ret)
693 printk(KERN_ERR "Unable to register BQ27x00 i2c driver\n");
694
695 return ret;
696}
697
698static inline void bq27x00_battery_i2c_exit(void)
699{
700 i2c_del_driver(&bq27x00_battery_driver);
701}
702
703#else
704
705static inline int bq27x00_battery_i2c_init(void) { return 0; }
706static inline void bq27x00_battery_i2c_exit(void) {};
707
708#endif
709
710/* platform specific code */
711#ifdef CONFIG_BATTERY_BQ27X00_PLATFORM
712
713static int bq27000_read_platform(struct bq27x00_device_info *di, u8 reg,
714 bool single)
715{
716 struct device *dev = di->dev;
717 struct bq27000_platform_data *pdata = dev->platform_data;
718 unsigned int timeout = 3;
719 int upper, lower;
720 int temp;
721
722 if (!single) {
723 /* Make sure the value has not changed in between reading the
724 * lower and the upper part */
725 upper = pdata->read(dev, reg + 1);
726 do {
727 temp = upper;
728 if (upper < 0)
729 return upper;
730
731 lower = pdata->read(dev, reg);
732 if (lower < 0)
733 return lower;
734
735 upper = pdata->read(dev, reg + 1);
736 } while (temp != upper && --timeout);
737
738 if (timeout == 0)
739 return -EIO;
740
741 return (upper << 8) | lower;
742 }
743
744 return pdata->read(dev, reg);
745}
746
747static int __devinit bq27000_battery_probe(struct platform_device *pdev)
748{
749 struct bq27x00_device_info *di;
750 struct bq27000_platform_data *pdata = pdev->dev.platform_data;
751 int ret;
752
753 if (!pdata) {
754 dev_err(&pdev->dev, "no platform_data supplied\n");
755 return -EINVAL;
756 }
757
758 if (!pdata->read) {
759 dev_err(&pdev->dev, "no hdq read callback supplied\n");
760 return -EINVAL;
761 }
762
763 di = kzalloc(sizeof(*di), GFP_KERNEL);
764 if (!di) {
765 dev_err(&pdev->dev, "failed to allocate device info data\n");
766 return -ENOMEM;
767 }
768
769 platform_set_drvdata(pdev, di);
770
771 di->dev = &pdev->dev;
772 di->chip = BQ27000;
773
774 di->bat.name = pdata->name ?: dev_name(&pdev->dev);
775 di->bus.read = &bq27000_read_platform;
776
777 ret = bq27x00_powersupply_init(di);
778 if (ret)
779 goto err_free;
780
781 return 0;
782
783err_free:
784 platform_set_drvdata(pdev, NULL);
785 kfree(di);
786
787 return ret;
788}
789
790static int __devexit bq27000_battery_remove(struct platform_device *pdev)
791{
792 struct bq27x00_device_info *di = platform_get_drvdata(pdev);
793
794 bq27x00_powersupply_unregister(di);
795
796 platform_set_drvdata(pdev, NULL);
797 kfree(di);
798
799 return 0;
800}
801
802static struct platform_driver bq27000_battery_driver = {
803 .probe = bq27000_battery_probe,
804 .remove = __devexit_p(bq27000_battery_remove),
805 .driver = {
806 .name = "bq27000-battery",
807 .owner = THIS_MODULE,
808 },
809};
810
811static inline int bq27x00_battery_platform_init(void)
812{
813 int ret = platform_driver_register(&bq27000_battery_driver);
814 if (ret)
815 printk(KERN_ERR "Unable to register BQ27000 platform driver\n");
816
817 return ret;
818}
819
820static inline void bq27x00_battery_platform_exit(void)
821{
822 platform_driver_unregister(&bq27000_battery_driver);
823}
824
825#else
826
827static inline int bq27x00_battery_platform_init(void) { return 0; }
828static inline void bq27x00_battery_platform_exit(void) {};
829
830#endif
831
832/*
833 * Module stuff
834 */
835
452static int __init bq27x00_battery_init(void) 836static int __init bq27x00_battery_init(void)
453{ 837{
454 int ret; 838 int ret;
455 839
456 ret = i2c_add_driver(&bq27x00_battery_driver); 840 ret = bq27x00_battery_i2c_init();
457 if (ret) 841 if (ret)
458 printk(KERN_ERR "Unable to register BQ27x00 driver\n"); 842 return ret;
843
844 ret = bq27x00_battery_platform_init();
845 if (ret)
846 bq27x00_battery_i2c_exit();
459 847
460 return ret; 848 return ret;
461} 849}
@@ -463,7 +851,8 @@ module_init(bq27x00_battery_init);
463 851
464static void __exit bq27x00_battery_exit(void) 852static void __exit bq27x00_battery_exit(void)
465{ 853{
466 i2c_del_driver(&bq27x00_battery_driver); 854 bq27x00_battery_platform_exit();
855 bq27x00_battery_i2c_exit();
467} 856}
468module_exit(bq27x00_battery_exit); 857module_exit(bq27x00_battery_exit);
469 858
diff --git a/drivers/power/power_supply_core.c b/drivers/power/power_supply_core.c
index 970f7335d3a7..329b46b2327d 100644
--- a/drivers/power/power_supply_core.c
+++ b/drivers/power/power_supply_core.c
@@ -171,6 +171,8 @@ int power_supply_register(struct device *parent, struct power_supply *psy)
171 dev_set_drvdata(dev, psy); 171 dev_set_drvdata(dev, psy);
172 psy->dev = dev; 172 psy->dev = dev;
173 173
174 INIT_WORK(&psy->changed_work, power_supply_changed_work);
175
174 rc = kobject_set_name(&dev->kobj, "%s", psy->name); 176 rc = kobject_set_name(&dev->kobj, "%s", psy->name);
175 if (rc) 177 if (rc)
176 goto kobject_set_name_failed; 178 goto kobject_set_name_failed;
@@ -179,8 +181,6 @@ int power_supply_register(struct device *parent, struct power_supply *psy)
179 if (rc) 181 if (rc)
180 goto device_add_failed; 182 goto device_add_failed;
181 183
182 INIT_WORK(&psy->changed_work, power_supply_changed_work);
183
184 rc = power_supply_create_triggers(psy); 184 rc = power_supply_create_triggers(psy);
185 if (rc) 185 if (rc)
186 goto create_triggers_failed; 186 goto create_triggers_failed;
diff --git a/drivers/power/power_supply_sysfs.c b/drivers/power/power_supply_sysfs.c
index cd1f90754a3a..605514afc29f 100644
--- a/drivers/power/power_supply_sysfs.c
+++ b/drivers/power/power_supply_sysfs.c
@@ -270,7 +270,7 @@ int power_supply_uevent(struct device *dev, struct kobj_uevent_env *env)
270 attr = &power_supply_attrs[psy->properties[j]]; 270 attr = &power_supply_attrs[psy->properties[j]];
271 271
272 ret = power_supply_show_property(dev, attr, prop_buf); 272 ret = power_supply_show_property(dev, attr, prop_buf);
273 if (ret == -ENODEV) { 273 if (ret == -ENODEV || ret == -ENODATA) {
274 /* When a battery is absent, we expect -ENODEV. Don't abort; 274 /* When a battery is absent, we expect -ENODEV. Don't abort;
275 send the uevent with at least the the PRESENT=0 property */ 275 send the uevent with at least the the PRESENT=0 property */
276 ret = 0; 276 ret = 0;