bq27xxx_battery.c 27.7 KB
Newer Older
1
/*
2
 * BQ27xxx battery driver
3 4 5
 *
 * Copyright (C) 2008 Rodolfo Giometti <giometti@linux.it>
 * Copyright (C) 2008 Eurotech S.p.A. <info@eurotech.it>
6
 * Copyright (C) 2010-2011 Lars-Peter Clausen <lars@metafoo.de>
7
 * Copyright (C) 2011 Pali Rohár <pali.rohar@gmail.com>
8 9 10 11 12 13 14 15 16 17 18
 *
 * Based on a previous work by Copyright (C) 2008 Texas Instruments, Inc.
 *
 * This package is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
P
Pali Rohár 已提交
19 20 21
 * Datasheets:
 * http://focus.ti.com/docs/prod/folders/print/bq27000.html
 * http://focus.ti.com/docs/prod/folders/print/bq27500.html
22
 * http://www.ti.com/product/bq27425-g1
23
 * http://www.ti.com/product/BQ27742-G1
24
 * http://www.ti.com/product/BQ27510-G3
P
Pali Rohár 已提交
25 26
 */

27
#include <linux/device.h>
28 29 30 31 32 33 34 35 36
#include <linux/module.h>
#include <linux/param.h>
#include <linux/jiffies.h>
#include <linux/workqueue.h>
#include <linux/delay.h>
#include <linux/platform_device.h>
#include <linux/power_supply.h>
#include <linux/idr.h>
#include <linux/i2c.h>
37
#include <linux/slab.h>
38
#include <asm/unaligned.h>
39

40
#include <linux/power/bq27xxx_battery.h>
41

42
#define DRIVER_VERSION		"1.2.0"
43

44 45
#define BQ27XXX_MANUFACTURER	"Texas Instruments"

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
#define BQ27x00_REG_TEMP	0x06
#define BQ27x00_REG_VOLT	0x08
#define BQ27x00_REG_AI		0x14
#define BQ27x00_REG_FLAGS	0x0A
#define BQ27x00_REG_TTE		0x16
#define BQ27x00_REG_TTF		0x18
#define BQ27x00_REG_TTECP	0x26
#define BQ27x00_REG_NAC		0x0C /* Nominal available capacity */
#define BQ27x00_REG_LMD		0x12 /* Last measured discharge */
#define BQ27x00_REG_CYCT	0x2A /* Cycle count total */
#define BQ27x00_REG_AE		0x22 /* Available energy */
#define BQ27x00_POWER_AVG	0x24

#define BQ27000_REG_RSOC	0x0B /* Relative State-of-Charge */
#define BQ27000_REG_ILMD	0x76 /* Initial last measured discharge */
#define BQ27000_FLAG_EDVF	BIT(0) /* Final End-of-Discharge-Voltage flag */
#define BQ27000_FLAG_EDV1	BIT(1) /* First End-of-Discharge-Voltage flag */
#define BQ27000_FLAG_CI		BIT(4) /* Capacity Inaccurate flag */
#define BQ27000_FLAG_FC		BIT(5)
#define BQ27000_FLAG_CHGS	BIT(7) /* Charge state flag */

#define BQ27500_REG_SOC		0x2C
#define BQ27500_REG_DCAP	0x3C /* Design capacity */
#define BQ27500_FLAG_DSC	BIT(0)
#define BQ27500_FLAG_SOCF	BIT(1) /* State-of-Charge threshold final */
#define BQ27500_FLAG_SOC1	BIT(2) /* State-of-Charge threshold 1 */
#define BQ27500_FLAG_FC		BIT(9)
#define BQ27500_FLAG_OTC	BIT(15)

#define BQ27742_POWER_AVG	0x76

#define BQ27510_REG_SOC		0x20
#define BQ27510_REG_DCAP	0x2E /* Design capacity */
#define BQ27510_REG_CYCT	0x1E /* Cycle count total */
80

81
/* bq27425 register addresses are same as bq27x00 addresses minus 4 */
82
#define BQ27425_REG_OFFSET	0x04
83
#define BQ27425_REG_SOC		(0x1C + BQ27425_REG_OFFSET)
84
#define BQ27425_REG_DCAP	(0x3C + BQ27425_REG_OFFSET)
85

86 87
#define BQ27XXX_RS			20 /* Resistor sense */
#define BQ27XXX_POWER_CONSTANT		(256 * 29200 / 1000)
P
Pali Rohár 已提交
88

89 90 91
struct bq27xxx_device_info;
struct bq27xxx_access_methods {
	int (*read)(struct bq27xxx_device_info *di, u8 reg, bool single);
92 93
};

94
enum bq27xxx_chip { BQ27000, BQ27500, BQ27425, BQ27742, BQ27510};
95

96
struct bq27xxx_reg_cache {
97 98 99 100
	int temperature;
	int time_to_empty;
	int time_to_empty_avg;
	int time_to_full;
P
Pali Rohár 已提交
101
	int charge_full;
102
	int cycle_count;
103
	int capacity;
104
	int energy;
105
	int flags;
106 107
	int power_avg;
	int health;
108 109
};

110
struct bq27xxx_device_info {
111
	struct device		*dev;
112
	int			id;
113
	enum bq27xxx_chip	chip;
114

115
	struct bq27xxx_reg_cache cache;
P
Pali Rohár 已提交
116 117
	int charge_design_full;

118
	unsigned long last_update;
L
Lars-Peter Clausen 已提交
119
	struct delayed_work work;
120

121
	struct power_supply	*bat;
122

123
	struct bq27xxx_access_methods bus;
L
Lars-Peter Clausen 已提交
124 125

	struct mutex lock;
126 127 128
};

static enum power_supply_property bq27x00_battery_props[] = {
129
	POWER_SUPPLY_PROP_STATUS,
130 131 132 133
	POWER_SUPPLY_PROP_PRESENT,
	POWER_SUPPLY_PROP_VOLTAGE_NOW,
	POWER_SUPPLY_PROP_CURRENT_NOW,
	POWER_SUPPLY_PROP_CAPACITY,
134
	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
135
	POWER_SUPPLY_PROP_TEMP,
136 137 138
	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
	POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
	POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
L
Lars-Peter Clausen 已提交
139
	POWER_SUPPLY_PROP_TECHNOLOGY,
P
Pali Rohár 已提交
140 141 142
	POWER_SUPPLY_PROP_CHARGE_FULL,
	POWER_SUPPLY_PROP_CHARGE_NOW,
	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
143
	POWER_SUPPLY_PROP_CYCLE_COUNT,
P
Pali Rohár 已提交
144
	POWER_SUPPLY_PROP_ENERGY_NOW,
145 146
	POWER_SUPPLY_PROP_POWER_AVG,
	POWER_SUPPLY_PROP_HEALTH,
147
	POWER_SUPPLY_PROP_MANUFACTURER,
148 149
};

150 151 152 153 154 155 156 157 158 159 160 161
static enum power_supply_property bq27425_battery_props[] = {
	POWER_SUPPLY_PROP_STATUS,
	POWER_SUPPLY_PROP_PRESENT,
	POWER_SUPPLY_PROP_VOLTAGE_NOW,
	POWER_SUPPLY_PROP_CURRENT_NOW,
	POWER_SUPPLY_PROP_CAPACITY,
	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
	POWER_SUPPLY_PROP_TEMP,
	POWER_SUPPLY_PROP_TECHNOLOGY,
	POWER_SUPPLY_PROP_CHARGE_FULL,
	POWER_SUPPLY_PROP_CHARGE_NOW,
	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
162
	POWER_SUPPLY_PROP_MANUFACTURER,
163 164
};

165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
static enum power_supply_property bq27742_battery_props[] = {
	POWER_SUPPLY_PROP_STATUS,
	POWER_SUPPLY_PROP_PRESENT,
	POWER_SUPPLY_PROP_VOLTAGE_NOW,
	POWER_SUPPLY_PROP_CURRENT_NOW,
	POWER_SUPPLY_PROP_CAPACITY,
	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
	POWER_SUPPLY_PROP_TEMP,
	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
	POWER_SUPPLY_PROP_TECHNOLOGY,
	POWER_SUPPLY_PROP_CHARGE_FULL,
	POWER_SUPPLY_PROP_CHARGE_NOW,
	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
	POWER_SUPPLY_PROP_CYCLE_COUNT,
	POWER_SUPPLY_PROP_POWER_AVG,
	POWER_SUPPLY_PROP_HEALTH,
181
	POWER_SUPPLY_PROP_MANUFACTURER,
182 183
};

184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
static enum power_supply_property bq27510_battery_props[] = {
	POWER_SUPPLY_PROP_STATUS,
	POWER_SUPPLY_PROP_PRESENT,
	POWER_SUPPLY_PROP_VOLTAGE_NOW,
	POWER_SUPPLY_PROP_CURRENT_NOW,
	POWER_SUPPLY_PROP_CAPACITY,
	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
	POWER_SUPPLY_PROP_TEMP,
	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
	POWER_SUPPLY_PROP_TECHNOLOGY,
	POWER_SUPPLY_PROP_CHARGE_FULL,
	POWER_SUPPLY_PROP_CHARGE_NOW,
	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
	POWER_SUPPLY_PROP_CYCLE_COUNT,
	POWER_SUPPLY_PROP_POWER_AVG,
	POWER_SUPPLY_PROP_HEALTH,
200
	POWER_SUPPLY_PROP_MANUFACTURER,
201 202
};

L
Lars-Peter Clausen 已提交
203 204
static unsigned int poll_interval = 360;
module_param(poll_interval, uint, 0644);
205 206
MODULE_PARM_DESC(poll_interval,
		 "battery poll interval in seconds - 0 disables polling");
L
Lars-Peter Clausen 已提交
207

208
/*
209
 * Common code for BQ27xxx devices
210 211
 */

212
static inline int bq27xxx_read(struct bq27xxx_device_info *di, u8 reg,
213
			       bool single)
214
{
215 216
	if (di->chip == BQ27425)
		return di->bus.read(di, reg - BQ27425_REG_OFFSET, single);
217
	return di->bus.read(di, reg, single);
218 219
}

220 221 222 223 224
/*
 * Higher versions of the chip like BQ27425 and BQ27500
 * differ from BQ27000 and BQ27200 in calculation of certain
 * parameters. Hence we need to check for the chip type.
 */
225
static bool bq27xxx_is_chip_version_higher(struct bq27xxx_device_info *di)
226
{
227 228
	if (di->chip == BQ27425 || di->chip == BQ27500 || di->chip == BQ27742
	    || di->chip == BQ27510)
229 230 231 232
		return true;
	return false;
}

233
/*
234
 * Return the battery Relative State-of-Charge
235 236
 * Or < 0 if something fails.
 */
237
static int bq27xxx_battery_read_rsoc(struct bq27xxx_device_info *di)
238
{
239
	int rsoc;
240

241
	if (di->chip == BQ27500 || di->chip == BQ27742)
242
		rsoc = bq27xxx_read(di, BQ27500_REG_SOC, false);
243
	else if (di->chip == BQ27510)
244
		rsoc = bq27xxx_read(di, BQ27510_REG_SOC, false);
245
	else if (di->chip == BQ27425)
246
		rsoc = bq27xxx_read(di, BQ27425_REG_SOC, false);
247
	else
248
		rsoc = bq27xxx_read(di, BQ27000_REG_RSOC, true);
249 250

	if (rsoc < 0)
251
		dev_dbg(di->dev, "error reading relative State-of-Charge\n");
252 253

	return rsoc;
254 255
}

P
Pali Rohár 已提交
256 257 258 259
/*
 * Return a battery charge value in µAh
 * Or < 0 if something fails.
 */
260
static int bq27xxx_battery_read_charge(struct bq27xxx_device_info *di, u8 reg)
P
Pali Rohár 已提交
261 262 263
{
	int charge;

264
	charge = bq27xxx_read(di, reg, false);
P
Pali Rohár 已提交
265
	if (charge < 0) {
266 267
		dev_dbg(di->dev, "error reading charge register %02x: %d\n",
			reg, charge);
P
Pali Rohár 已提交
268 269 270
		return charge;
	}

271
	if (bq27xxx_is_chip_version_higher(di))
P
Pali Rohár 已提交
272 273
		charge *= 1000;
	else
274
		charge = charge * 3570 / BQ27XXX_RS;
P
Pali Rohár 已提交
275 276 277 278 279 280 281 282

	return charge;
}

/*
 * Return the battery Nominal available capaciy in µAh
 * Or < 0 if something fails.
 */
283
static inline int bq27xxx_battery_read_nac(struct bq27xxx_device_info *di)
P
Pali Rohár 已提交
284
{
285 286
	int flags;
	bool is_bq27500 = di->chip == BQ27500;
287
	bool is_bq27742 = di->chip == BQ27742;
288
	bool is_higher = bq27xxx_is_chip_version_higher(di);
289
	bool flags_1b = !(is_bq27500 || is_bq27742);
290

291
	flags = bq27xxx_read(di, BQ27x00_REG_FLAGS, flags_1b);
292 293 294
	if (flags >= 0 && !is_higher && (flags & BQ27000_FLAG_CI))
		return -ENODATA;

295
	return bq27xxx_battery_read_charge(di, BQ27x00_REG_NAC);
P
Pali Rohár 已提交
296 297 298 299 300 301
}

/*
 * Return the battery Last measured discharge in µAh
 * Or < 0 if something fails.
 */
302
static inline int bq27xxx_battery_read_lmd(struct bq27xxx_device_info *di)
P
Pali Rohár 已提交
303
{
304
	return bq27xxx_battery_read_charge(di, BQ27x00_REG_LMD);
P
Pali Rohár 已提交
305 306 307 308 309 310
}

/*
 * Return the battery Initial last measured discharge in µAh
 * Or < 0 if something fails.
 */
311
static int bq27xxx_battery_read_ilmd(struct bq27xxx_device_info *di)
P
Pali Rohár 已提交
312 313 314
{
	int ilmd;

315 316
	if (bq27xxx_is_chip_version_higher(di)) {
		if (di->chip == BQ27425)
317
			ilmd = bq27xxx_read(di, BQ27425_REG_DCAP, false);
318
		else if (di->chip == BQ27510)
319
			ilmd = bq27xxx_read(di, BQ27510_REG_DCAP, false);
320
		else
321
			ilmd = bq27xxx_read(di, BQ27500_REG_DCAP, false);
322
	} else {
323
		ilmd = bq27xxx_read(di, BQ27000_REG_ILMD, true);
324
	}
P
Pali Rohár 已提交
325 326

	if (ilmd < 0) {
327
		dev_dbg(di->dev, "error reading initial last measured discharge\n");
P
Pali Rohár 已提交
328 329 330
		return ilmd;
	}

331
	if (bq27xxx_is_chip_version_higher(di))
P
Pali Rohár 已提交
332 333
		ilmd *= 1000;
	else
334
		ilmd = ilmd * 256 * 3570 / BQ27XXX_RS;
P
Pali Rohár 已提交
335 336 337 338

	return ilmd;
}

339 340 341 342
/*
 * Return the battery Available energy in µWh
 * Or < 0 if something fails.
 */
343
static int bq27xxx_battery_read_energy(struct bq27xxx_device_info *di)
344 345 346
{
	int ae;

347
	ae = bq27xxx_read(di, BQ27x00_REG_AE, false);
348
	if (ae < 0) {
349
		dev_dbg(di->dev, "error reading available energy\n");
350 351 352 353 354 355
		return ae;
	}

	if (di->chip == BQ27500)
		ae *= 1000;
	else
356
		ae = ae * 29200 / BQ27XXX_RS;
357 358 359 360

	return ae;
}

361
/*
362
 * Return the battery temperature in tenths of degree Kelvin
363 364
 * Or < 0 if something fails.
 */
365
static int bq27xxx_battery_read_temperature(struct bq27xxx_device_info *di)
366 367 368
{
	int temp;

369
	temp = bq27xxx_read(di, BQ27x00_REG_TEMP, false);
370 371 372 373 374
	if (temp < 0) {
		dev_err(di->dev, "error reading temperature\n");
		return temp;
	}

375 376
	if (!bq27xxx_is_chip_version_higher(di))
		temp = 5 * temp / 2;
377 378 379 380

	return temp;
}

P
Pali Rohár 已提交
381 382 383 384
/*
 * Return the battery Cycle count total
 * Or < 0 if something fails.
 */
385
static int bq27xxx_battery_read_cyct(struct bq27xxx_device_info *di)
P
Pali Rohár 已提交
386 387 388
{
	int cyct;

389
	if (di->chip == BQ27510)
390
		cyct = bq27xxx_read(di, BQ27510_REG_CYCT, false);
391
	else
392
		cyct = bq27xxx_read(di, BQ27x00_REG_CYCT, false);
P
Pali Rohár 已提交
393 394 395 396 397 398
	if (cyct < 0)
		dev_err(di->dev, "error reading cycle count total\n");

	return cyct;
}

399
/*
400 401
 * Read a time register.
 * Return < 0 if something fails.
402
 */
403
static int bq27xxx_battery_read_time(struct bq27xxx_device_info *di, u8 reg)
404
{
405
	int tval;
406

407
	tval = bq27xxx_read(di, reg, false);
408
	if (tval < 0) {
409 410
		dev_dbg(di->dev, "error reading time register %02x: %d\n",
			reg, tval);
411
		return tval;
412 413
	}

414 415 416 417 418 419
	if (tval == 65535)
		return -ENODATA;

	return tval * 60;
}

420 421 422 423
/*
 * Read a power avg register.
 * Return < 0 if something fails.
 */
424
static int bq27xxx_battery_read_pwr_avg(struct bq27xxx_device_info *di, u8 reg)
425 426 427
{
	int tval;

428
	tval = bq27xxx_read(di, reg, false);
429 430 431 432 433 434 435 436 437
	if (tval < 0) {
		dev_err(di->dev, "error reading power avg rgister  %02x: %d\n",
			reg, tval);
		return tval;
	}

	if (di->chip == BQ27500)
		return tval;
	else
438
		return (tval * BQ27XXX_POWER_CONSTANT) / BQ27XXX_RS;
439 440 441 442 443 444
}

/*
 * Read flag register.
 * Return < 0 if something fails.
 */
445
static int bq27xxx_battery_read_health(struct bq27xxx_device_info *di)
446 447 448
{
	int tval;

449
	tval = bq27xxx_read(di, BQ27x00_REG_FLAGS, false);
450 451 452 453 454
	if (tval < 0) {
		dev_err(di->dev, "error reading flag register:%d\n", tval);
		return tval;
	}

455
	if (di->chip == BQ27500) {
456 457 458 459 460 461 462
		if (tval & BQ27500_FLAG_SOCF)
			tval = POWER_SUPPLY_HEALTH_DEAD;
		else if (tval & BQ27500_FLAG_OTC)
			tval = POWER_SUPPLY_HEALTH_OVERHEAT;
		else
			tval = POWER_SUPPLY_HEALTH_GOOD;
		return tval;
463 464 465 466
	} else if (di->chip == BQ27510) {
		if (tval & BQ27500_FLAG_OTC)
			return POWER_SUPPLY_HEALTH_OVERHEAT;
		return POWER_SUPPLY_HEALTH_GOOD;
467 468 469 470 471 472 473 474 475 476 477
	} else {
		if (tval & BQ27000_FLAG_EDV1)
			tval = POWER_SUPPLY_HEALTH_DEAD;
		else
			tval = POWER_SUPPLY_HEALTH_GOOD;
		return tval;
	}

	return -1;
}

478
static void bq27xxx_battery_update(struct bq27xxx_device_info *di)
479
{
480
	struct bq27xxx_reg_cache cache = {0, };
481
	bool is_bq27500 = di->chip == BQ27500;
482
	bool is_bq27510 = di->chip == BQ27510;
483
	bool is_bq27425 = di->chip == BQ27425;
484
	bool is_bq27742 = di->chip == BQ27742;
485
	bool flags_1b = !(is_bq27500 || is_bq27742);
486

487
	cache.flags = bq27xxx_read(di, BQ27x00_REG_FLAGS, flags_1b);
488 489 490
	if ((cache.flags & 0xff) == 0xff)
		/* read error */
		cache.flags = -1;
491
	if (cache.flags >= 0) {
492
		if (!is_bq27500 && !is_bq27425 && !is_bq27742 && !is_bq27510
493
				&& (cache.flags & BQ27000_FLAG_CI)) {
494
			dev_info(di->dev, "battery is not calibrated! ignoring capacity values\n");
495
			cache.capacity = -ENODATA;
496
			cache.energy = -ENODATA;
497 498 499 500
			cache.time_to_empty = -ENODATA;
			cache.time_to_empty_avg = -ENODATA;
			cache.time_to_full = -ENODATA;
			cache.charge_full = -ENODATA;
501
			cache.health = -ENODATA;
502
		} else {
503
			cache.capacity = bq27xxx_battery_read_rsoc(di);
504
			if (is_bq27742 || is_bq27510)
505
				cache.time_to_empty =
506
					bq27xxx_battery_read_time(di,
507 508
							BQ27x00_REG_TTE);
			else if (!is_bq27425) {
509
				cache.energy = bq27xxx_battery_read_energy(di);
510
				cache.time_to_empty =
511
					bq27xxx_battery_read_time(di,
512 513
							BQ27x00_REG_TTE);
				cache.time_to_empty_avg =
514
					bq27xxx_battery_read_time(di,
515 516
							BQ27x00_REG_TTECP);
				cache.time_to_full =
517
					bq27xxx_battery_read_time(di,
518 519
							BQ27x00_REG_TTF);
			}
520 521
			cache.charge_full = bq27xxx_battery_read_lmd(di);
			cache.health = bq27xxx_battery_read_health(di);
522
		}
523
		cache.temperature = bq27xxx_battery_read_temperature(di);
524
		if (!is_bq27425)
525
			cache.cycle_count = bq27xxx_battery_read_cyct(di);
526 527
		if (is_bq27742)
			cache.power_avg =
528
				bq27xxx_battery_read_pwr_avg(di,
529 530 531
						BQ27742_POWER_AVG);
		else
			cache.power_avg =
532
				bq27xxx_battery_read_pwr_avg(di,
533
						BQ27x00_POWER_AVG);
534

P
Pali Rohár 已提交
535 536
		/* We only have to read charge design full once */
		if (di->charge_design_full <= 0)
537
			di->charge_design_full = bq27xxx_battery_read_ilmd(di);
538 539
	}

540
	if (di->cache.capacity != cache.capacity)
541
		power_supply_changed(di->bat);
542 543 544

	if (memcmp(&di->cache, &cache, sizeof(cache)) != 0)
		di->cache = cache;
545 546 547 548

	di->last_update = jiffies;
}

549
static void bq27xxx_battery_poll(struct work_struct *work)
L
Lars-Peter Clausen 已提交
550
{
551 552
	struct bq27xxx_device_info *di =
		container_of(work, struct bq27xxx_device_info, work.work);
L
Lars-Peter Clausen 已提交
553

554
	bq27xxx_battery_update(di);
L
Lars-Peter Clausen 已提交
555 556 557 558 559 560 561 562

	if (poll_interval > 0) {
		/* The timer does not have to be accurate. */
		set_timer_slack(&di->work.timer, poll_interval * HZ / 4);
		schedule_delayed_work(&di->work, poll_interval * HZ);
	}
}

563
/*
P
Pali Rohár 已提交
564
 * Return the battery average current in µA
565 566 567
 * Note that current can be negative signed as well
 * Or 0 if something fails.
 */
568
static int bq27xxx_battery_current(struct bq27xxx_device_info *di,
569
				   union power_supply_propval *val)
570
{
571
	int curr;
572
	int flags;
573

574
	curr = bq27xxx_read(di, BQ27x00_REG_AI, false);
575 576
	if (curr < 0) {
		dev_err(di->dev, "error reading current\n");
577
		return curr;
578
	}
579

580
	if (bq27xxx_is_chip_version_higher(di)) {
581
		/* bq27500 returns signed value */
582
		val->intval = (int)((s16)curr) * 1000;
583
	} else {
584
		flags = bq27xxx_read(di, BQ27x00_REG_FLAGS, false);
585
		if (flags & BQ27000_FLAG_CHGS) {
586
			dev_dbg(di->dev, "negative current!\n");
587
			curr = -curr;
588
		}
589

590
		val->intval = curr * 3570 / BQ27XXX_RS;
591 592
	}

593
	return 0;
594 595
}

596
static int bq27xxx_battery_status(struct bq27xxx_device_info *di,
597
				  union power_supply_propval *val)
598 599 600
{
	int status;

601
	if (bq27xxx_is_chip_version_higher(di)) {
602
		if (di->cache.flags & BQ27500_FLAG_FC)
603
			status = POWER_SUPPLY_STATUS_FULL;
604
		else if (di->cache.flags & BQ27500_FLAG_DSC)
605
			status = POWER_SUPPLY_STATUS_DISCHARGING;
606
		else
607
			status = POWER_SUPPLY_STATUS_CHARGING;
608
	} else {
609 610 611
		if (di->cache.flags & BQ27000_FLAG_FC)
			status = POWER_SUPPLY_STATUS_FULL;
		else if (di->cache.flags & BQ27000_FLAG_CHGS)
612
			status = POWER_SUPPLY_STATUS_CHARGING;
613
		else if (power_supply_am_i_supplied(di->bat))
614
			status = POWER_SUPPLY_STATUS_NOT_CHARGING;
615 616 617 618 619
		else
			status = POWER_SUPPLY_STATUS_DISCHARGING;
	}

	val->intval = status;
620

621 622 623
	return 0;
}

624
static int bq27xxx_battery_capacity_level(struct bq27xxx_device_info *di,
625
					  union power_supply_propval *val)
626 627 628
{
	int level;

629
	if (bq27xxx_is_chip_version_higher(di)) {
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
		if (di->cache.flags & BQ27500_FLAG_FC)
			level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
		else if (di->cache.flags & BQ27500_FLAG_SOC1)
			level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
		else if (di->cache.flags & BQ27500_FLAG_SOCF)
			level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
		else
			level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
	} else {
		if (di->cache.flags & BQ27000_FLAG_FC)
			level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
		else if (di->cache.flags & BQ27000_FLAG_EDV1)
			level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
		else if (di->cache.flags & BQ27000_FLAG_EDVF)
			level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
		else
			level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
	}

	val->intval = level;

	return 0;
}

654
/*
T
Thomas Weber 已提交
655
 * Return the battery Voltage in millivolts
656
 * Or < 0 if something fails.
657
 */
658
static int bq27xxx_battery_voltage(struct bq27xxx_device_info *di,
659
				   union power_supply_propval *val)
660
{
661
	int volt;
662

663
	volt = bq27xxx_read(di, BQ27x00_REG_VOLT, false);
664 665
	if (volt < 0) {
		dev_err(di->dev, "error reading voltage\n");
666
		return volt;
667
	}
668

669 670 671 672 673
	val->intval = volt * 1000;

	return 0;
}

674
static int bq27xxx_simple_value(int value,
675
				union power_supply_propval *val)
676 677 678 679 680
{
	if (value < 0)
		return value;

	val->intval = value;
681 682 683 684

	return 0;
}

685
static int bq27xxx_battery_get_property(struct power_supply *psy,
686 687 688
					enum power_supply_property psp,
					union power_supply_propval *val)
{
689
	int ret = 0;
690
	struct bq27xxx_device_info *di = power_supply_get_drvdata(psy);
691

L
Lars-Peter Clausen 已提交
692 693 694
	mutex_lock(&di->lock);
	if (time_is_before_jiffies(di->last_update + 5 * HZ)) {
		cancel_delayed_work_sync(&di->work);
695
		bq27xxx_battery_poll(&di->work.work);
L
Lars-Peter Clausen 已提交
696 697
	}
	mutex_unlock(&di->lock);
698 699

	if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0)
700
		return -ENODEV;
701 702

	switch (psp) {
703
	case POWER_SUPPLY_PROP_STATUS:
704
		ret = bq27xxx_battery_status(di, val);
705
		break;
706
	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
707
		ret = bq27xxx_battery_voltage(di, val);
708
		break;
709
	case POWER_SUPPLY_PROP_PRESENT:
710
		val->intval = di->cache.flags < 0 ? 0 : 1;
711 712
		break;
	case POWER_SUPPLY_PROP_CURRENT_NOW:
713
		ret = bq27xxx_battery_current(di, val);
714 715
		break;
	case POWER_SUPPLY_PROP_CAPACITY:
716
		ret = bq27xxx_simple_value(di->cache.capacity, val);
717
		break;
718
	case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
719
		ret = bq27xxx_battery_capacity_level(di, val);
720
		break;
721
	case POWER_SUPPLY_PROP_TEMP:
722
		ret = bq27xxx_simple_value(di->cache.temperature, val);
723 724
		if (ret == 0)
			val->intval -= 2731;
725
		break;
726
	case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
727
		ret = bq27xxx_simple_value(di->cache.time_to_empty, val);
728 729
		break;
	case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
730
		ret = bq27xxx_simple_value(di->cache.time_to_empty_avg, val);
731 732
		break;
	case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
733
		ret = bq27xxx_simple_value(di->cache.time_to_full, val);
734
		break;
L
Lars-Peter Clausen 已提交
735 736 737
	case POWER_SUPPLY_PROP_TECHNOLOGY:
		val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
		break;
P
Pali Rohár 已提交
738
	case POWER_SUPPLY_PROP_CHARGE_NOW:
739
		ret = bq27xxx_simple_value(bq27xxx_battery_read_nac(di), val);
P
Pali Rohár 已提交
740 741
		break;
	case POWER_SUPPLY_PROP_CHARGE_FULL:
742
		ret = bq27xxx_simple_value(di->cache.charge_full, val);
P
Pali Rohár 已提交
743 744
		break;
	case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
745
		ret = bq27xxx_simple_value(di->charge_design_full, val);
P
Pali Rohár 已提交
746
		break;
747
	case POWER_SUPPLY_PROP_CYCLE_COUNT:
748
		ret = bq27xxx_simple_value(di->cache.cycle_count, val);
P
Pali Rohár 已提交
749 750
		break;
	case POWER_SUPPLY_PROP_ENERGY_NOW:
751
		ret = bq27xxx_simple_value(di->cache.energy, val);
P
Pali Rohár 已提交
752
		break;
753
	case POWER_SUPPLY_PROP_POWER_AVG:
754
		ret = bq27xxx_simple_value(di->cache.power_avg, val);
755 756
		break;
	case POWER_SUPPLY_PROP_HEALTH:
757
		ret = bq27xxx_simple_value(di->cache.health, val);
758
		break;
759 760 761
	case POWER_SUPPLY_PROP_MANUFACTURER:
		val->strval = BQ27XXX_MANUFACTURER;
		break;
762 763 764 765
	default:
		return -EINVAL;
	}

766
	return ret;
767 768
}

769
static void bq27xxx_external_power_changed(struct power_supply *psy)
L
Lars-Peter Clausen 已提交
770
{
771
	struct bq27xxx_device_info *di = power_supply_get_drvdata(psy);
L
Lars-Peter Clausen 已提交
772 773 774 775 776

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

777
static int bq27xxx_powersupply_init(struct bq27xxx_device_info *di,
778
				    const char *name)
779
{
780
	int ret;
781 782 783 784 785 786
	struct power_supply_desc *psy_desc;
	struct power_supply_config psy_cfg = { .drv_data = di, };

	psy_desc = devm_kzalloc(di->dev, sizeof(*psy_desc), GFP_KERNEL);
	if (!psy_desc)
		return -ENOMEM;
787

788 789
	psy_desc->name = name;
	psy_desc->type = POWER_SUPPLY_TYPE_BATTERY;
790
	if (di->chip == BQ27425) {
791 792
		psy_desc->properties = bq27425_battery_props;
		psy_desc->num_properties = ARRAY_SIZE(bq27425_battery_props);
793
	} else if (di->chip == BQ27742) {
794 795
		psy_desc->properties = bq27742_battery_props;
		psy_desc->num_properties = ARRAY_SIZE(bq27742_battery_props);
796
	} else if (di->chip == BQ27510) {
797 798
		psy_desc->properties = bq27510_battery_props;
		psy_desc->num_properties = ARRAY_SIZE(bq27510_battery_props);
799
	} else {
800 801
		psy_desc->properties = bq27x00_battery_props;
		psy_desc->num_properties = ARRAY_SIZE(bq27x00_battery_props);
802
	}
803 804
	psy_desc->get_property = bq27xxx_battery_get_property;
	psy_desc->external_power_changed = bq27xxx_external_power_changed;
L
Lars-Peter Clausen 已提交
805

806
	INIT_DELAYED_WORK(&di->work, bq27xxx_battery_poll);
L
Lars-Peter Clausen 已提交
807
	mutex_init(&di->lock);
808

809 810 811
	di->bat = power_supply_register_no_ws(di->dev, psy_desc, &psy_cfg);
	if (IS_ERR(di->bat)) {
		ret = PTR_ERR(di->bat);
812 813 814 815 816 817
		dev_err(di->dev, "failed to register battery: %d\n", ret);
		return ret;
	}

	dev_info(di->dev, "support ver. %s enabled\n", DRIVER_VERSION);

818
	bq27xxx_battery_update(di);
819

820
	return 0;
821 822
}

823
static void bq27xxx_powersupply_unregister(struct bq27xxx_device_info *di)
L
Lars-Peter Clausen 已提交
824
{
825
	/*
826 827 828
	 * power_supply_unregister call bq27xxx_battery_get_property which
	 * call bq27xxx_battery_poll.
	 * Make sure that bq27xxx_battery_poll will not call
829 830 831 832
	 * schedule_delayed_work again after unregister (which cause OOPS).
	 */
	poll_interval = 0;

L
Lars-Peter Clausen 已提交
833 834
	cancel_delayed_work_sync(&di->work);

835
	power_supply_unregister(di->bat);
L
Lars-Peter Clausen 已提交
836 837 838 839

	mutex_destroy(&di->lock);
}

840
/* i2c specific code */
841
#ifdef CONFIG_BATTERY_BQ27XXX_I2C
842 843 844

/* If the system has several batteries we need a different name for each
 * of them...
845
 */
846 847
static DEFINE_IDR(battery_id);
static DEFINE_MUTEX(battery_mutex);
848

849 850
static int bq27xxx_battery_i2c_read(struct bq27xxx_device_info *di, u8 reg,
				    bool single)
851
{
852
	struct i2c_client *client = to_i2c_client(di->dev);
853
	struct i2c_msg msg[2];
854
	unsigned char data[2];
855
	int ret;
856 857 858 859

	if (!client->adapter)
		return -ENODEV;

860 861 862 863 864 865 866
	msg[0].addr = client->addr;
	msg[0].flags = 0;
	msg[0].buf = &reg;
	msg[0].len = sizeof(reg);
	msg[1].addr = client->addr;
	msg[1].flags = I2C_M_RD;
	msg[1].buf = data;
867
	if (single)
868
		msg[1].len = 1;
869
	else
870
		msg[1].len = 2;
871

872
	ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
873 874 875 876 877 878 879
	if (ret < 0)
		return ret;

	if (!single)
		ret = get_unaligned_le16(data);
	else
		ret = data[0];
880

881
	return ret;
882 883
}

884 885
static int bq27xxx_battery_i2c_probe(struct i2c_client *client,
				     const struct i2c_device_id *id)
886 887
{
	char *name;
888
	struct bq27xxx_device_info *di;
889 890 891 892 893
	int num;
	int retval = 0;

	/* Get new ID for the new battery device */
	mutex_lock(&battery_mutex);
T
Tejun Heo 已提交
894
	num = idr_alloc(&battery_id, client, 0, 0, GFP_KERNEL);
895
	mutex_unlock(&battery_mutex);
T
Tejun Heo 已提交
896 897
	if (num < 0)
		return num;
898

899
	name = devm_kasprintf(&client->dev, GFP_KERNEL, "%s-%d", id->name, num);
900 901
	if (!name) {
		retval = -ENOMEM;
902
		goto batt_failed;
903 904
	}

905
	di = devm_kzalloc(&client->dev, sizeof(*di), GFP_KERNEL);
906 907
	if (!di) {
		retval = -ENOMEM;
908
		goto batt_failed;
909
	}
910

911
	di->id = num;
912
	di->dev = &client->dev;
913
	di->chip = id->driver_data;
914
	di->bus.read = &bq27xxx_battery_i2c_read;
915

916
	retval = bq27xxx_powersupply_init(di, name);
917
	if (retval)
918
		goto batt_failed;
919 920 921 922 923

	i2c_set_clientdata(client, di);

	return 0;

924
batt_failed:
925 926 927 928 929 930 931
	mutex_lock(&battery_mutex);
	idr_remove(&battery_id, num);
	mutex_unlock(&battery_mutex);

	return retval;
}

932
static int bq27xxx_battery_i2c_remove(struct i2c_client *client)
933
{
934
	struct bq27xxx_device_info *di = i2c_get_clientdata(client);
935

936
	bq27xxx_powersupply_unregister(di);
937 938 939 940 941 942 943 944

	mutex_lock(&battery_mutex);
	idr_remove(&battery_id, di->id);
	mutex_unlock(&battery_mutex);

	return 0;
}

945
static const struct i2c_device_id bq27xxx_id[] = {
946 947
	{ "bq27200", BQ27000 },	/* bq27200 is same as bq27000, but with i2c */
	{ "bq27500", BQ27500 },
948
	{ "bq27425", BQ27425 },
949
	{ "bq27742", BQ27742 },
950
	{ "bq27510", BQ27510 },
951 952
	{},
};
953
MODULE_DEVICE_TABLE(i2c, bq27xxx_id);
954

955
static struct i2c_driver bq27xxx_battery_i2c_driver = {
956
	.driver = {
957
		.name = "bq27xxx-battery",
958
	},
959 960 961
	.probe = bq27xxx_battery_i2c_probe,
	.remove = bq27xxx_battery_i2c_remove,
	.id_table = bq27xxx_id,
962 963
};

964
static inline int bq27xxx_battery_i2c_init(void)
965
{
966
	int ret = i2c_add_driver(&bq27xxx_battery_i2c_driver);
967

968
	if (ret)
969
		pr_err("Unable to register BQ27xxx i2c driver\n");
970 971 972 973

	return ret;
}

974
static inline void bq27xxx_battery_i2c_exit(void)
975
{
976
	i2c_del_driver(&bq27xxx_battery_i2c_driver);
977 978 979 980
}

#else

981 982
static inline int bq27xxx_battery_i2c_init(void) { return 0; }
static inline void bq27xxx_battery_i2c_exit(void) {};
983 984 985 986

#endif

/* platform specific code */
987
#ifdef CONFIG_BATTERY_BQ27XXX_PLATFORM
988

989 990
static int bq27xxx_battery_platform_read(struct bq27xxx_device_info *di, u8 reg,
					 bool single)
991 992
{
	struct device *dev = di->dev;
993
	struct bq27xxx_platform_data *pdata = dev->platform_data;
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
	unsigned int timeout = 3;
	int upper, lower;
	int temp;

	if (!single) {
		/* Make sure the value has not changed in between reading the
		 * lower and the upper part */
		upper = pdata->read(dev, reg + 1);
		do {
			temp = upper;
			if (upper < 0)
				return upper;

			lower = pdata->read(dev, reg);
			if (lower < 0)
				return lower;

			upper = pdata->read(dev, reg + 1);
		} while (temp != upper && --timeout);

		if (timeout == 0)
			return -EIO;

1017
		return (upper << 8) | lower;
1018
	}
1019 1020

	return pdata->read(dev, reg);
1021 1022
}

1023
static int bq27xxx_battery_platform_probe(struct platform_device *pdev)
1024
{
1025 1026
	struct bq27xxx_device_info *di;
	struct bq27xxx_platform_data *pdata = pdev->dev.platform_data;
1027
	const char *name;
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038

	if (!pdata) {
		dev_err(&pdev->dev, "no platform_data supplied\n");
		return -EINVAL;
	}

	if (!pdata->read) {
		dev_err(&pdev->dev, "no hdq read callback supplied\n");
		return -EINVAL;
	}

1039
	di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
1040
	if (!di)
1041 1042 1043 1044 1045 1046 1047
		return -ENOMEM;

	platform_set_drvdata(pdev, di);

	di->dev = &pdev->dev;
	di->chip = BQ27000;

1048
	name = pdata->name ?: dev_name(&pdev->dev);
1049
	di->bus.read = &bq27xxx_battery_platform_read;
1050

1051
	return bq27xxx_powersupply_init(di, name);
1052 1053
}

1054
static int bq27xxx_battery_platform_remove(struct platform_device *pdev)
1055
{
1056
	struct bq27xxx_device_info *di = platform_get_drvdata(pdev);
1057

1058
	bq27xxx_powersupply_unregister(di);
L
Lars-Peter Clausen 已提交
1059

1060 1061 1062
	return 0;
}

1063 1064 1065
static struct platform_driver bq27xxx_battery_platform_driver = {
	.probe	= bq27xxx_battery_platform_probe,
	.remove = bq27xxx_battery_platform_remove,
1066 1067 1068 1069 1070
	.driver = {
		.name = "bq27000-battery",
	},
};

1071
static inline int bq27xxx_battery_platform_init(void)
1072
{
1073
	int ret = platform_driver_register(&bq27xxx_battery_platform_driver);
1074

1075
	if (ret)
1076
		pr_err("Unable to register BQ27xxx platform driver\n");
1077 1078 1079 1080

	return ret;
}

1081
static inline void bq27xxx_battery_platform_exit(void)
1082
{
1083
	platform_driver_unregister(&bq27xxx_battery_platform_driver);
1084 1085 1086 1087
}

#else

1088 1089
static inline int bq27xxx_battery_platform_init(void) { return 0; }
static inline void bq27xxx_battery_platform_exit(void) {};
1090 1091 1092 1093 1094 1095 1096

#endif

/*
 * Module stuff
 */

1097
static int __init bq27xxx_battery_init(void)
1098 1099 1100
{
	int ret;

1101
	ret = bq27xxx_battery_i2c_init();
1102 1103 1104
	if (ret)
		return ret;

1105
	ret = bq27xxx_battery_platform_init();
1106
	if (ret)
1107
		bq27xxx_battery_i2c_exit();
1108 1109 1110

	return ret;
}
1111
module_init(bq27xxx_battery_init);
1112

1113
static void __exit bq27xxx_battery_exit(void)
1114
{
1115 1116
	bq27xxx_battery_platform_exit();
	bq27xxx_battery_i2c_exit();
1117
}
1118
module_exit(bq27xxx_battery_exit);
1119

1120
#ifdef CONFIG_BATTERY_BQ27XXX_PLATFORM
1121 1122 1123
MODULE_ALIAS("platform:bq27000-battery");
#endif

1124
MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
1125
MODULE_DESCRIPTION("BQ27xxx battery monitor driver");
1126
MODULE_LICENSE("GPL");