aboutsummaryrefslogtreecommitdiffstats
path: root/target/linux/s3c24xx/files-2.6.31/drivers/power/bq27000_battery.c
blob: 32655dce1f3673fecb109be778b7c390e1b29fdd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/*
 * Driver for batteries with bq27000 chips inside via HDQ
 *
 * Copyright 2008 Openmoko, Inc
 * Andy Green <andy@openmoko.com>
 *
 * based on ds2760 driver, original copyright notice for that --->
 *
 * Copyright © 2007 Anton Vorontsov
 *	       2004-2007 Matt Reimer
 *	       2004 Szabolcs Gyurko
 *
 * Use consistent with the GNU GPL is permitted,
 * provided that this copyright notice is
 * preserved in its entirety in all copies and derived works.
 *
 * Author:  Anton Vorontsov <cbou@mail.ru>
 *	    February 2007
 *
 *	    Matt Reimer <mreimer@vpop.net>
 *	    April 2004, 2005, 2007
 *
 *	    Szabolcs Gyurko <szabolcs.gyurko@tlt.hu>
 *	    September 2004
 */

#include <linux/module.h>
#include <linux/param.h>
#include <linux/jiffies.h>
#include <linux/delay.h>
#include <linux/pm.h>
#include <linux/workqueue.h>
#include <linux/platform_device.h>
#include <linux/power_supply.h>
#include <linux/bq27000_battery.h>

enum bq27000_regs {
	/* RAM regs */
		/* read-write after this */
	BQ27000_CTRL = 0, /* Device Control Register */
	BQ27000_MODE, /* Device Mode Register */
	BQ27000_AR_L, /* At-Rate H L */
	BQ27000_AR_H,
		/* read-only after this */
	BQ27000_ARTTE_L, /* At-Rate Time To Empty H L */
	BQ27000_ARTTE_H,
	BQ27000_TEMP_L, /* Reported Temperature H L */
	BQ27000_TEMP_H,
	BQ27000_VOLT_L, /* Reported Voltage H L */
	BQ27000_VOLT_H,
	BQ27000_FLAGS, /* Status Flags */
	BQ27000_RSOC, /* Relative State of Charge */
	BQ27000_NAC_L, /* Nominal Available Capacity H L */
	BQ27000_NAC_H,
	BQ27000_CACD_L, /* Discharge Compensated H L */
	BQ27000_CACD_H,
	BQ27000_CACT_L, /* Temperature Compensated H L */
	BQ27000_CACT_H,
	BQ27000_LMD_L, /* Last measured discharge H L */
	BQ27000_LMD_H,
	BQ27000_AI_L, /* Average Current H L */
	BQ27000_AI_H,
	BQ27000_TTE_L, /* Time to Empty H L */
	BQ27000_TTE_H,
	BQ27000_TTF_L, /* Time to Full H L */
	BQ27000_TTF_H,
	BQ27000_SI_L, /* Standby Current H L */
	BQ27000_SI_H,
	BQ27000_STTE_L, /* Standby Time To Empty H L */
	BQ27000_STTE_H,
	BQ27000_MLI_L, /* Max Load Current H L */
	BQ27000_MLI_H,
	BQ27000_MLTTE_L, /* Max Load Time To Empty H L */
	BQ27000_MLTTE_H,
	BQ27000_SAE_L, /* Available Energy H L */
	BQ27000_SAE_H,
	BQ27000_AP_L, /* Available Power H L */
	BQ27000_AP_H,
	BQ27000_TTECP_L, /* Time to Empty at Constant Power H L */
	BQ27000_TTECP_H,
	BQ27000_CYCL_L, /* Cycle count since learning cycle H L */
	BQ27000_CYCL_H,
	BQ27000_CYCT_L, /* Cycle Count Total H L */
	BQ27000_CYCT_H,
	BQ27000_CSOC, /* Compensated State Of Charge */
	/* EEPROM regs */
		/* read-write after this */
	BQ27000_EE_EE_EN = 0x6e, /* EEPROM Program Enable */
	BQ27000_EE_ILMD = 0x76, /* Initial Last Measured Discharge High Byte */
	BQ27000_EE_SEDVF, /* Scaled EDVF Threshold */
	BQ27000_EE_SEDV1, /* Scaled EDV1 Threshold */
	BQ27000_EE_ISLC, /* Initial Standby Load Current */
	BQ27000_EE_DMFSD, /* Digital Magnitude Filter and Self Discharge */
	BQ27000_EE_TAPER, /* Aging Estimate Enable, Charge Termination Taper */
	BQ27000_EE_PKCFG, /* Pack Configuration Values */
	BQ27000_EE_IMLC, /* Initial Max Load Current or ID #3 */
	BQ27000_EE_DCOMP, /* Discharge rate compensation constants or ID #2 */
	BQ27000_EE_TCOMP, /* Temperature Compensation constants or ID #1 */
};

enum bq27000_status_flags {
	BQ27000_STATUS_CHGS = 0x80, /* 1 = being charged */
	BQ27000_STATUS_NOACT = 0x40, /* 1 = no activity */
	BQ27000_STATUS_IMIN = 0x20, /* 1 = Lion taper current mode */
	BQ27000_STATUS_CI = 0x10, /* 1 = capacity likely  innacurate */
	BQ27000_STATUS_CALIP = 0x08, /* 1 = calibration in progress */
	BQ27000_STATUS_VDQ = 0x04, /* 1 = capacity should be accurate */
	BQ27000_STATUS_EDV1 = 0x02, /* 1 = end of discharge.. <6% left */
	BQ27000_STATUS_EDVF = 0x01, /* 1 = no, it's really empty now */
};

#define NANOVOLTS_UNIT 3750

struct bq27000_bat_regs {
	int		ai;
	int		flags;
	int		lmd;
	int		rsoc;
	int		temp;
	int		tte;
	int		ttf;
	int		volt;
};

struct bq27000_device_info {
	struct device *dev;
	struct power_supply bat;
	struct power_supply ac;
	struct power_supply usb;
	struct delayed_work work;
	struct bq27000_platform_data *pdata;

	struct bq27000_bat_regs regs;
};

static unsigned int cache_time = 5000;
module_param(cache_time, uint, 0644);
MODULE_PARM_DESC(cache_time, "cache time in milliseconds");

/*
 * reading 16 bit values over HDQ has a special hazard where the
 * hdq device firmware can update the 16-bit register during the time we
 * read the two halves.  TI document SLUS556D recommends the algorithm here
 * to avoid trouble
 */

static int hdq_read16(struct bq27000_device_info *di, int address)
{
	int acc;
	int high;
	int retries = 3;

	while (retries--) {

		high = (di->pdata->hdq_read)(address + 1); /* high part */

		if (high < 0)
			return high;
		acc = (di->pdata->hdq_read)(address);
		if (acc < 0)
			return acc;

		/* confirm high didn't change between reading it and low */
		if (high == (di->pdata->hdq_read)(address + 1))
			return (high << 8) | acc;
	}

	return -ETIME;
}

static void bq27000_battery_external_power_changed(struct power_supply *psy)
{
	struct bq27000_device_info *di = container_of(psy, struct bq27000_device_info, bat);

	dev_dbg(di->dev, "%s\n", __FUNCTION__);
	schedule_delayed_work(&di->work, 0);
}

static int bq27000_battery_get_property(struct power_supply *psy,
				       enum power_supply_property psp,
				       union power_supply_propval *val)
{
	int n;
	struct bq27000_device_info *di = container_of(psy, struct bq27000_device_info, bat);

	if (di->regs.rsoc < 0 && psp != POWER_SUPPLY_PROP_PRESENT)
		return -ENODEV;

	switch (psp) {
	case POWER_SUPPLY_PROP_STATUS:
		val->intval = POWER_SUPPLY_STATUS_UNKNOWN;

		if (!di->pdata->get_charger_online_status)
			goto use_bat;
		if ((di->pdata->get_charger_online_status)()) {
			/*
			 * charger is definitively present
			 * we report our state in terms of what it says it
			 * is doing
			 */
			if (!di->pdata->get_charger_active_status)
				goto use_bat;

			if ((di->pdata->get_charger_active_status)()) {
				val->intval = POWER_SUPPLY_STATUS_CHARGING;
				break;
			}
			val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
			break;
		}

		/*
		 * platform provided definite indication of charger presence,
		 * and it is telling us it isn't there... but we are on so we
		 * must be running from battery --->
		 */

		val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
		break;

use_bat:
		/*
		 * either the charger is not connected, or the
		 * platform doesn't give info about charger, use battery state
		 * but... battery state can be out of date by 4 seconds or
		 * so... use the platform callbacks if possible.
		 */

		/* no real activity on the battery */
		if (di->regs.ai < 2) {
			if (!di->regs.ttf)
				val->intval = POWER_SUPPLY_STATUS_FULL;
			else
				val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
			break;
		}
		/* power is actually going in or out... */
		if (di->regs.flags < 0)
			return di->regs.flags;
		if (di->regs.flags & BQ27000_STATUS_CHGS)
			val->intval = POWER_SUPPLY_STATUS_CHARGING;
		else
			val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
		break;
	case POWER_SUPPLY_PROP_HEALTH:
		val->intval = POWER_SUPPLY_HEALTH_UNKNOWN;
		/* Do we have accurate readings... */
		if (di->regs.flags < 0)
			return di->regs.flags;
		if (di->regs.flags & BQ27000_STATUS_VDQ)
			val->intval = POWER_SUPPLY_HEALTH_GOOD;
		break;
	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
		if (di->regs.volt < 0)
			return di->regs.volt;
		/* mV -> uV */
		val->intval = di->regs.volt * 1000;
		break;
	case POWER_SUPPLY_PROP_CURRENT_NOW:
		if (di->regs.flags < 0)
			return di->regs.flags;
		if (di->regs.flags & BQ27000_STATUS_CHGS)
			n = -NANOVOLTS_UNIT;
		else
			n = NANOVOLTS_UNIT;
		if (di->regs.ai < 0)
			return di->regs.ai;
		val->intval = (di->regs.ai * n) / di->pdata->rsense_mohms;
		break;
	case POWER_SUPPLY_PROP_CHARGE_FULL:
		if (di->regs.lmd < 0)
			return di->regs.lmd;
		val->intval = (di->regs.lmd * 3570) / di->pdata->rsense_mohms;
		break;
	case POWER_SUPPLY_PROP_TEMP:
		if (di->regs.temp < 0)
			return di->regs.temp;
		/* K (in 0.25K units) is 273.15 up from C (in 0.1C)*/
		/* 10926 = 27315 * 4 / 10 */
		val->intval = (((long)di->regs.temp * 10l) - 10926) / 4;
		break;
	case POWER_SUPPLY_PROP_TECHNOLOGY:
		val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
		break;
	case POWER_SUPPLY_PROP_CAPACITY:
		val->intval = di->regs.rsoc;
		if (val->intval < 0)
			return val->intval;
		break;
	case POWER_SUPPLY_PROP_PRESENT:
		val->intval = !(di->regs.rsoc < 0);
		break;
	case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
		if (di->regs.tte < 0)
			return di->regs.tte;
		val->intval = 60 * di->regs.tte;
		break;
	case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
		if (di->regs.ttf < 0)
			return di->regs.ttf;
		val->intval = 60 * di->regs.ttf;
		break;
	case POWER_SUPPLY_PROP_ONLINE:
		if (di->pdata->get_charger_online_status)
			val->intval = (di->pdata->get_charger_online_status)();
		else
			return -EINVAL;
		break;
	default:
		return -EINVAL;
	}

	return 0;
}

static void bq27000_battery_work(struct work_struct *work)
{
	struct bq27000_device_info *di =
		container_of(work, struct bq27000_device_info, work.work);

	if ((di->pdata->hdq_initialized)()) {
		struct bq27000_bat_regs regs;

		regs.ai    = hdq_read16(di, BQ27000_AI_L);
		regs.flags = (di->pdata->hdq_read)(BQ27000_FLAGS);
		regs.lmd   = hdq_read16(di, BQ27000_LMD_L);
		regs.rsoc  = (di->pdata->hdq_read)(BQ27000_RSOC);
		regs.temp  = hdq_read16(di, BQ27000_TEMP_L);
		regs.tte   = hdq_read16(di, BQ27000_TTE_L);
		regs.ttf   = hdq_read16(di, BQ27000_TTF_L);
		regs.volt  = hdq_read16(di, BQ27000_VOLT_L);

		if (memcmp (&regs, &di->regs, sizeof(regs)) != 0) {
			di->regs = regs;
			power_supply_changed(&di->bat);
		}
	}

	if (!schedule_delayed_work(&di->work, cache_time))
		dev_err(di->dev, "battery service reschedule failed\n");
}

static enum power_supply_property bq27000_battery_props[] = {
	POWER_SUPPLY_PROP_STATUS,
	POWER_SUPPLY_PROP_HEALTH,
	POWER_SUPPLY_PROP_VOLTAGE_NOW,
	POWER_SUPPLY_PROP_CURRENT_NOW,
	POWER_SUPPLY_PROP_CHARGE_FULL,
	POWER_SUPPLY_PROP_TEMP,
	POWER_SUPPLY_PROP_TECHNOLOGY,
	POWER_SUPPLY_PROP_PRESENT,
	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
	POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
	POWER_SUPPLY_PROP_CAPACITY,
	POWER_SUPPLY_PROP_ONLINE
};

static int bq27000_battery_probe(struct platform_device *pdev)
{
	int retval = 0;
	struct bq27000_device_info *di;
	struct bq27000_platform_data *pdata;

	dev_info(&pdev->dev, "BQ27000 Battery Driver (C) 2008 Openmoko, Inc\n");

	di = kzalloc(sizeof(*di), GFP_KERNEL);
	if (!di) {
		retval = -ENOMEM;
		goto di_alloc_failed;
	}

	platform_set_drvdata(pdev, di);

	pdata = pdev->dev.platform_data;
	di->dev		= &pdev->dev;
	/* di->w1_dev	     = pdev->dev.parent; */
	di->bat.name	   = pdata->name;
	di->bat.type	   = POWER_SUPPLY_TYPE_BATTERY;
	di->bat.properties     = bq27000_battery_props;
	di->bat.num_properties = ARRAY_SIZE(bq27000_battery_props);
	di->bat.get_property   = bq27000_battery_get_property;
	di->bat.external_power_changed =
				  bq27000_battery_external_power_changed;
	di->bat.use_for_apm = 1;
	di->pdata = pdata;

	retval = power_supply_register(&pdev->dev, &di->bat);
	if (retval) {
		dev_err(di->dev, "failed to register battery\n");
		goto batt_failed;
	}

	INIT_DELAYED_WORK(&di->work, bq27000_battery_work);

	if (!schedule_delayed_work(&di->work, 0))
		dev_err(di->dev, "failed to schedule bq27000_battery_work\n");

	return 0;

batt_failed:
	kfree(di);
di_alloc_failed:
	return retval;
}

static int bq27000_battery_remove(struct platform_device *pdev)
{
	struct bq27000_device_info *di = platform_get_drvdata(pdev);

	cancel_delayed_work(&di->work);

	power_supply_unregister(&di->bat);

	return 0;
}

void bq27000_charging_state_change(struct platform_device *pdev)
{
	struct bq27000_device_info *di = platform_get_drvdata(pdev);

	if (!di)
	    return;
}
EXPORT_SYMBOL_GPL(bq27000_charging_state_change);

#ifdef CONFIG_PM

static int bq27000_battery_suspend(struct platform_device *pdev,
				  pm_message_t state)
{
	struct bq27000_device_info *di = platform_get_drvdata(pdev);

	cancel_delayed_work(&di->work);
	return 0;
}

static int bq27000_battery_resume(struct platform_device *pdev)
{
	struct bq27000_device_info *di = platform_get_drvdata(pdev);

	schedule_delayed_work(&di->work, 0);
	return 0;
}

#else

#define bq27000_battery_suspend NULL
#define bq27000_battery_resume NULL

#endif /* CONFIG_PM */

static struct platform_driver bq27000_battery_driver = {
	.driver = {
		.name = "bq27000-battery",
	},
	.probe	  = bq27000_battery_probe,
	.remove   = bq27000_battery_remove,
	.suspend  = bq27000_battery_suspend,
	.resume	  = bq27000_battery_resume,
};

static int __init bq27000_battery_init(void)
{
	return platform_driver_register(&bq27000_battery_driver);
}

static void __exit bq27000_battery_exit(void)
{
	platform_driver_unregister(&bq27000_battery_driver);
}

module_init(bq27000_battery_init);
module_exit(bq27000_battery_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Andy Green <andy@openmoko.com>");
MODULE_DESCRIPTION("bq27000 battery driver");