clean-move-defconfigs-stable.patch
[linux-2.6/openmoko-kernel/knife-kernel.git] / drivers / power / bq27000_battery.c
blobcd4968edad93708a1a102c41f974190e2de2f86d
1 /*
2 * Driver for batteries with bq27000 chips inside via HDQ
4 * Copyright 2008 Openmoko, Inc
5 * Andy Green <andy@openmoko.com>
7 * based on ds2760 driver, original copyright notice for that --->
9 * Copyright © 2007 Anton Vorontsov
10 * 2004-2007 Matt Reimer
11 * 2004 Szabolcs Gyurko
13 * Use consistent with the GNU GPL is permitted,
14 * provided that this copyright notice is
15 * preserved in its entirety in all copies and derived works.
17 * Author: Anton Vorontsov <cbou@mail.ru>
18 * February 2007
20 * Matt Reimer <mreimer@vpop.net>
21 * April 2004, 2005, 2007
23 * Szabolcs Gyurko <szabolcs.gyurko@tlt.hu>
24 * September 2004
27 #include <linux/module.h>
28 #include <linux/param.h>
29 #include <linux/jiffies.h>
30 #include <linux/delay.h>
31 #include <linux/pm.h>
32 #include <linux/platform_device.h>
33 #include <linux/power_supply.h>
34 #include <linux/bq27000_battery.h>
36 enum bq27000_regs {
37 /* RAM regs */
38 /* read-write after this */
39 BQ27000_CTRL = 0, /* Device Control Register */
40 BQ27000_MODE, /* Device Mode Register */
41 BQ27000_AR_L, /* At-Rate H L */
42 BQ27000_AR_H,
43 /* read-only after this */
44 BQ27000_ARTTE_L, /* At-Rate Time To Empty H L */
45 BQ27000_ARTTE_H,
46 BQ27000_TEMP_L, /* Reported Temperature H L */
47 BQ27000_TEMP_H,
48 BQ27000_VOLT_L, /* Reported Voltage H L */
49 BQ27000_VOLT_H,
50 BQ27000_FLAGS, /* Status Flags */
51 BQ27000_RSOC, /* Relative State of Charge */
52 BQ27000_NAC_L, /* Nominal Available Capacity H L */
53 BQ27000_NAC_H,
54 BQ27000_CACD_L, /* Discharge Compensated H L */
55 BQ27000_CACD_H,
56 BQ27000_CACT_L, /* Temperature Compensated H L */
57 BQ27000_CACT_H,
58 BQ27000_LMD_L, /* Last measured discharge H L */
59 BQ27000_LMD_H,
60 BQ27000_AI_L, /* Average Current H L */
61 BQ27000_AI_H,
62 BQ27000_TTE_L, /* Time to Empty H L */
63 BQ27000_TTE_H,
64 BQ27000_TTF_L, /* Time to Full H L */
65 BQ27000_TTF_H,
66 BQ27000_SI_L, /* Standby Current H L */
67 BQ27000_SI_H,
68 BQ27000_STTE_L, /* Standby Time To Empty H L */
69 BQ27000_STTE_H,
70 BQ27000_MLI_L, /* Max Load Current H L */
71 BQ27000_MLI_H,
72 BQ27000_MLTTE_L, /* Max Load Time To Empty H L */
73 BQ27000_MLTTE_H,
74 BQ27000_SAE_L, /* Available Energy H L */
75 BQ27000_SAE_H,
76 BQ27000_AP_L, /* Available Power H L */
77 BQ27000_AP_H,
78 BQ27000_TTECP_L, /* Time to Empty at Constant Power H L */
79 BQ27000_TTECP_H,
80 BQ27000_CYCL_L, /* Cycle count since learning cycle H L */
81 BQ27000_CYCL_H,
82 BQ27000_CYCT_L, /* Cycle Count Total H L */
83 BQ27000_CYCT_H,
84 BQ27000_CSOC, /* Compensated State Of Charge */
85 /* EEPROM regs */
86 /* read-write after this */
87 BQ27000_EE_EE_EN = 0x6e, /* EEPROM Program Enable */
88 BQ27000_EE_ILMD = 0x76, /* Initial Last Measured Discharge High Byte */
89 BQ27000_EE_SEDVF, /* Scaled EDVF Threshold */
90 BQ27000_EE_SEDV1, /* Scaled EDV1 Threshold */
91 BQ27000_EE_ISLC, /* Initial Standby Load Current */
92 BQ27000_EE_DMFSD, /* Digital Magnitude Filter and Self Discharge */
93 BQ27000_EE_TAPER, /* Aging Estimate Enable, Charge Termination Taper */
94 BQ27000_EE_PKCFG, /* Pack Configuration Values */
95 BQ27000_EE_IMLC, /* Initial Max Load Current or ID #3 */
96 BQ27000_EE_DCOMP, /* Discharge rate compensation constants or ID #2 */
97 BQ27000_EE_TCOMP, /* Temperature Compensation constants or ID #1 */
100 enum bq27000_status_flags {
101 BQ27000_STATUS_CHGS = 0x80, /* 1 = being charged */
102 BQ27000_STATUS_NOACT = 0x40, /* 1 = no activity */
103 BQ27000_STATUS_IMIN = 0x20, /* 1 = Lion taper current mode */
104 BQ27000_STATUS_CI = 0x10, /* 1 = capacity likely innacurate */
105 BQ27000_STATUS_CALIP = 0x08, /* 1 = calibration in progress */
106 BQ27000_STATUS_VDQ = 0x04, /* 1 = capacity should be accurate */
107 BQ27000_STATUS_EDV1 = 0x02, /* 1 = end of discharge.. <6% left */
108 BQ27000_STATUS_EDVF = 0x01, /* 1 = no, it's really empty now */
111 #define NANOVOLTS_UNIT 3750
113 struct bq27000_device_info {
114 struct device *dev;
115 struct power_supply bat;
116 struct bq27000_platform_data *pdata;
120 * reading 16 bit values over HDQ has a special hazard where the
121 * hdq device firmware can update the 16-bit register during the time we
122 * read the two halves. TI document SLUS556D recommends the algorithm here
123 * to avoid trouble
126 static int hdq_read16(struct bq27000_device_info *di, int address)
128 int acc;
129 int high;
130 int retries = 3;
132 while (retries--) {
134 high = (di->pdata->hdq_read)(address + 1); /* high part */
136 if (high < 0)
137 return high;
138 acc = (di->pdata->hdq_read)(address);
139 if (acc < 0)
140 return acc;
142 /* confirm high didn't change between reading it and low */
143 if (high == (di->pdata->hdq_read)(address + 1))
144 return (high << 8) | acc;
147 return -ETIME;
150 #define to_bq27000_device_info(x) container_of((x), \
151 struct bq27000_device_info, \
152 bat);
154 static void bq27000_battery_external_power_changed(struct power_supply *psy)
156 struct bq27000_device_info *di = to_bq27000_device_info(psy);
158 dev_dbg(di->dev, "%s\n", __FUNCTION__);
161 static int bq27000_battery_get_property(struct power_supply *psy,
162 enum power_supply_property psp,
163 union power_supply_propval *val)
165 int v, n;
166 struct bq27000_device_info *di = to_bq27000_device_info(psy);
168 if (!(di->pdata->hdq_initialized)())
169 return -EINVAL;
171 switch (psp) {
172 case POWER_SUPPLY_PROP_STATUS:
173 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
175 if (!di->pdata->get_charger_online_status)
176 goto use_bat;
177 if ((di->pdata->get_charger_online_status)()) {
179 * charger is definitively present
180 * we report our state in terms of what it says it
181 * is doing
183 if (!di->pdata->get_charger_active_status)
184 goto use_bat;
186 if ((di->pdata->get_charger_active_status)()) {
187 val->intval = POWER_SUPPLY_STATUS_CHARGING;
188 break;
190 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
191 break;
195 * platform provided definite indication of charger presence,
196 * and it is telling us it isn't there... but we are on so we
197 * must be running from battery --->
200 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
201 break;
203 use_bat:
205 * either the charger is not connected, or the
206 * platform doesn't give info about charger, use battery state
207 * but... battery state can be out of date by 4 seconds or
208 * so... use the platform callbacks if possible.
210 v = hdq_read16(di, BQ27000_AI_L);
211 if (v < 0)
212 return v;
214 /* no real activity on the battery */
215 if (v < 2) {
216 if (!hdq_read16(di, BQ27000_TTF_L))
217 val->intval = POWER_SUPPLY_STATUS_FULL;
218 else
219 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
220 break;
222 /* power is actually going in or out... */
223 v = (di->pdata->hdq_read)(BQ27000_FLAGS);
224 if (v < 0)
225 return v;
226 if (v & BQ27000_STATUS_CHGS)
227 val->intval = POWER_SUPPLY_STATUS_CHARGING;
228 else
229 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
230 break;
231 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
232 v = hdq_read16(di, BQ27000_VOLT_L);
233 if (v < 0)
234 return v;
235 /* mV -> uV */
236 val->intval = v * 1000;
237 break;
238 case POWER_SUPPLY_PROP_CURRENT_NOW:
239 v = (di->pdata->hdq_read)(BQ27000_FLAGS);
240 if (v < 0)
241 return v;
242 if (v & BQ27000_STATUS_CHGS)
243 n = -NANOVOLTS_UNIT;
244 else
245 n = NANOVOLTS_UNIT;
246 v = hdq_read16(di, BQ27000_AI_L);
247 if (v < 0)
248 return v;
249 val->intval = (v * n) / di->pdata->rsense_mohms;
250 break;
251 case POWER_SUPPLY_PROP_CHARGE_FULL:
252 v = hdq_read16(di, BQ27000_LMD_L);
253 if (v < 0)
254 return v;
255 val->intval = (v * 3570) / di->pdata->rsense_mohms;
256 break;
257 case POWER_SUPPLY_PROP_TEMP:
258 v = hdq_read16(di, BQ27000_TEMP_L);
259 if (v < 0)
260 return v;
261 /* K (in 0.25K units) is 273.15 up from C (in 0.1C)*/
262 /* 10926 = 27315 * 4 / 10 */
263 val->intval = (((long)v * 10l) - 10926) / 4;
264 break;
265 case POWER_SUPPLY_PROP_TECHNOLOGY:
266 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
267 break;
268 case POWER_SUPPLY_PROP_CAPACITY:
269 val->intval = (di->pdata->hdq_read)(BQ27000_RSOC);
270 if (val->intval < 0)
271 return val->intval;
272 break;
273 case POWER_SUPPLY_PROP_PRESENT:
274 v = (di->pdata->hdq_read)(BQ27000_RSOC);
275 val->intval = !(v < 0);
276 break;
277 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
278 v = hdq_read16(di, BQ27000_TTE_L);
279 if (v < 0)
280 return v;
281 val->intval = 60 * v;
282 break;
283 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
284 v = hdq_read16(di, BQ27000_TTF_L);
285 if (v < 0)
286 return v;
287 val->intval = 60 * v;
288 break;
289 case POWER_SUPPLY_PROP_ONLINE:
290 if (di->pdata->get_charger_online_status)
291 val->intval = (di->pdata->get_charger_online_status)();
292 else
293 return -EINVAL;
294 break;
295 default:
296 return -EINVAL;
299 return 0;
302 static enum power_supply_property bq27000_battery_props[] = {
303 POWER_SUPPLY_PROP_STATUS,
304 POWER_SUPPLY_PROP_VOLTAGE_NOW,
305 POWER_SUPPLY_PROP_CURRENT_NOW,
306 POWER_SUPPLY_PROP_CHARGE_FULL,
307 POWER_SUPPLY_PROP_TEMP,
308 POWER_SUPPLY_PROP_TECHNOLOGY,
309 POWER_SUPPLY_PROP_PRESENT,
310 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
311 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
312 POWER_SUPPLY_PROP_CAPACITY,
313 POWER_SUPPLY_PROP_ONLINE
316 static int bq27000_battery_probe(struct platform_device *pdev)
318 int retval = 0;
319 struct bq27000_device_info *di;
320 struct bq27000_platform_data *pdata;
322 dev_info(&pdev->dev, "BQ27000 Battery Driver (C) 2008 Openmoko, Inc\n");
324 di = kzalloc(sizeof(*di), GFP_KERNEL);
325 if (!di) {
326 retval = -ENOMEM;
327 goto di_alloc_failed;
330 platform_set_drvdata(pdev, di);
332 pdata = pdev->dev.platform_data;
333 di->dev = &pdev->dev;
334 /* di->w1_dev = pdev->dev.parent; */
335 di->bat.name = pdata->name;
336 di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
337 di->bat.properties = bq27000_battery_props;
338 di->bat.num_properties = ARRAY_SIZE(bq27000_battery_props);
339 di->bat.get_property = bq27000_battery_get_property;
340 di->bat.external_power_changed =
341 bq27000_battery_external_power_changed;
342 di->bat.use_for_apm = 1;
343 di->pdata = pdata;
345 retval = power_supply_register(&pdev->dev, &di->bat);
346 if (retval) {
347 dev_err(di->dev, "failed to register battery\n");
348 goto batt_failed;
351 return 0;
353 batt_failed:
354 kfree(di);
355 di_alloc_failed:
356 return retval;
359 static int bq27000_battery_remove(struct platform_device *pdev)
361 struct bq27000_device_info *di = platform_get_drvdata(pdev);
363 power_supply_unregister(&di->bat);
365 return 0;
368 void bq27000_charging_state_change(struct platform_device *pdev)
370 struct bq27000_device_info *di = platform_get_drvdata(pdev);
372 if (!di)
373 return;
375 power_supply_changed(&di->bat);
377 EXPORT_SYMBOL_GPL(bq27000_charging_state_change);
379 #ifdef CONFIG_PM
381 static int bq27000_battery_suspend(struct platform_device *pdev,
382 pm_message_t state)
384 return 0;
387 static int bq27000_battery_resume(struct platform_device *pdev)
389 return 0;
392 #else
394 #define bq27000_battery_suspend NULL
395 #define bq27000_battery_resume NULL
397 #endif /* CONFIG_PM */
399 static struct platform_driver bq27000_battery_driver = {
400 .driver = {
401 .name = "bq27000-battery",
403 .probe = bq27000_battery_probe,
404 .remove = bq27000_battery_remove,
405 .suspend = bq27000_battery_suspend,
406 .resume = bq27000_battery_resume,
409 static int __init bq27000_battery_init(void)
411 return platform_driver_register(&bq27000_battery_driver);
414 static void __exit bq27000_battery_exit(void)
416 platform_driver_unregister(&bq27000_battery_driver);
419 module_init(bq27000_battery_init);
420 module_exit(bq27000_battery_exit);
422 MODULE_LICENSE("GPL");
423 MODULE_AUTHOR("Andy Green <andy@openmoko.com>");
424 MODULE_DESCRIPTION("bq27000 battery driver");