bq27x00_battery.c 13.6 KB
Newer Older
1 2 3 4 5
/*
 * BQ27x00 battery driver
 *
 * 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 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 *
 * 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.
 *
 */
#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>
28
#include <linux/slab.h>
29
#include <asm/unaligned.h>
30

31 32
#include <linux/power/bq27x00_battery.h>

33
#define DRIVER_VERSION			"1.1.0"
34 35 36 37 38

#define BQ27x00_REG_TEMP		0x06
#define BQ27x00_REG_VOLT		0x08
#define BQ27x00_REG_AI			0x14
#define BQ27x00_REG_FLAGS		0x0A
39 40 41
#define BQ27x00_REG_TTE			0x16
#define BQ27x00_REG_TTF			0x18
#define BQ27x00_REG_TTECP		0x26
42

43
#define BQ27000_REG_RSOC		0x0B /* Relative State-of-Charge */
44
#define BQ27000_FLAG_CHGS		BIT(7)
45 46

#define BQ27500_REG_SOC			0x2c
47 48
#define BQ27500_FLAG_DSC		BIT(0)
#define BQ27500_FLAG_FC			BIT(9)
49

P
Pali Rohár 已提交
50 51
#define BQ27000_RS			20 /* Resistor sense */

52 53
struct bq27x00_device_info;
struct bq27x00_access_methods {
54 55
	int (*read)(struct bq27x00_device_info *, u8 reg, int *rt_value,
			bool single);
56 57
};

58 59
enum bq27x00_chip { BQ27000, BQ27500 };

60 61 62
struct bq27x00_device_info {
	struct device 		*dev;
	int			id;
63
	enum bq27x00_chip	chip;
64

65 66 67
	struct power_supply	bat;

	struct bq27x00_access_methods bus;
68 69 70
};

static enum power_supply_property bq27x00_battery_props[] = {
71
	POWER_SUPPLY_PROP_STATUS,
72 73 74 75 76
	POWER_SUPPLY_PROP_PRESENT,
	POWER_SUPPLY_PROP_VOLTAGE_NOW,
	POWER_SUPPLY_PROP_CURRENT_NOW,
	POWER_SUPPLY_PROP_CAPACITY,
	POWER_SUPPLY_PROP_TEMP,
77 78 79
	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 已提交
80
	POWER_SUPPLY_PROP_TECHNOLOGY,
81 82 83 84 85 86
};

/*
 * Common code for BQ27x00 devices
 */

87 88
static inline int bq27x00_read(struct bq27x00_device_info *di, u8 reg,
		int *rt_value, bool single)
89
{
90
	return di->bus.read(di, reg, rt_value, single);
91 92 93
}

/*
94
 * Return the battery temperature in tenths of degree Celsius
95 96 97 98 99 100 101
 * Or < 0 if something fails.
 */
static int bq27x00_battery_temperature(struct bq27x00_device_info *di)
{
	int ret;
	int temp = 0;

102
	ret = bq27x00_read(di, BQ27x00_REG_TEMP, &temp, false);
103 104 105 106 107
	if (ret) {
		dev_err(di->dev, "error reading temperature\n");
		return ret;
	}

108 109 110
	if (di->chip == BQ27500)
		return temp - 2731;
	else
111
		return ((temp * 5) - 5463) / 2;
112 113 114 115 116 117 118 119 120 121 122
}

/*
 * Return the battery Voltage in milivolts
 * Or < 0 if something fails.
 */
static int bq27x00_battery_voltage(struct bq27x00_device_info *di)
{
	int ret;
	int volt = 0;

123
	ret = bq27x00_read(di, BQ27x00_REG_VOLT, &volt, false);
124 125 126 127 128
	if (ret) {
		dev_err(di->dev, "error reading voltage\n");
		return ret;
	}

129
	return volt * 1000;
130 131 132 133 134 135 136 137 138 139 140 141 142
}

/*
 * Return the battery average current
 * Note that current can be negative signed as well
 * Or 0 if something fails.
 */
static int bq27x00_battery_current(struct bq27x00_device_info *di)
{
	int ret;
	int curr = 0;
	int flags = 0;

143
	ret = bq27x00_read(di, BQ27x00_REG_AI, &curr, false);
144 145 146 147
	if (ret) {
		dev_err(di->dev, "error reading current\n");
		return 0;
	}
148 149 150

	if (di->chip == BQ27500) {
		/* bq27500 returns signed value */
P
Pali Rohár 已提交
151
		curr = (int)((s16)curr) * 1000;
152
	} else {
153
		ret = bq27x00_read(di, BQ27x00_REG_FLAGS, &flags, false);
154 155 156 157
		if (ret < 0) {
			dev_err(di->dev, "error reading flags\n");
			return 0;
		}
158
		if (flags & BQ27000_FLAG_CHGS) {
159
			dev_dbg(di->dev, "negative current!\n");
160
			curr = -curr;
161
		}
P
Pali Rohár 已提交
162
		curr = curr * 3570 / BQ27000_RS;
163
	}
164

P
Pali Rohár 已提交
165
	return curr;
166 167 168 169 170 171 172 173 174 175 176
}

/*
 * Return the battery Relative State-of-Charge
 * Or < 0 if something fails.
 */
static int bq27x00_battery_rsoc(struct bq27x00_device_info *di)
{
	int ret;
	int rsoc = 0;

177
	if (di->chip == BQ27500)
178
		ret = bq27x00_read(di, BQ27500_REG_SOC, &rsoc, false);
179
	else
180
		ret = bq27x00_read(di, BQ27000_REG_RSOC, &rsoc, true);
181 182 183 184 185
	if (ret) {
		dev_err(di->dev, "error reading relative State-of-Charge\n");
		return ret;
	}

186
	return rsoc;
187 188
}

189 190 191 192 193 194 195
static int bq27x00_battery_status(struct bq27x00_device_info *di,
				  union power_supply_propval *val)
{
	int flags = 0;
	int status;
	int ret;

196
	ret = bq27x00_read(di, BQ27x00_REG_FLAGS, &flags, false);
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
	if (ret < 0) {
		dev_err(di->dev, "error reading flags\n");
		return ret;
	}

	if (di->chip == BQ27500) {
		if (flags & BQ27500_FLAG_FC)
			status = POWER_SUPPLY_STATUS_FULL;
		else if (flags & BQ27500_FLAG_DSC)
			status = POWER_SUPPLY_STATUS_DISCHARGING;
		else
			status = POWER_SUPPLY_STATUS_CHARGING;
	} else {
		if (flags & BQ27000_FLAG_CHGS)
			status = POWER_SUPPLY_STATUS_CHARGING;
		else
			status = POWER_SUPPLY_STATUS_DISCHARGING;
	}

	val->intval = status;
	return 0;
}

/*
 * Read a time register.
 * Return < 0 if something fails.
 */
static int bq27x00_battery_time(struct bq27x00_device_info *di, int reg,
				union power_supply_propval *val)
{
	int tval = 0;
	int ret;

230
	ret = bq27x00_read(di, reg, &tval, false);
231 232 233 234 235 236 237 238 239 240 241 242
	if (ret) {
		dev_err(di->dev, "error reading register %02x\n", reg);
		return ret;
	}

	if (tval == 65535)
		return -ENODATA;

	val->intval = tval * 60;
	return 0;
}

243 244 245 246 247 248 249
#define to_bq27x00_device_info(x) container_of((x), \
				struct bq27x00_device_info, bat);

static int bq27x00_battery_get_property(struct power_supply *psy,
					enum power_supply_property psp,
					union power_supply_propval *val)
{
250
	int ret = 0;
251
	struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
252 253 254 255
	int voltage = bq27x00_battery_voltage(di);

	if (psp != POWER_SUPPLY_PROP_PRESENT && voltage <= 0)
		return -ENODEV;
256 257

	switch (psp) {
258 259 260
	case POWER_SUPPLY_PROP_STATUS:
		ret = bq27x00_battery_status(di, val);
		break;
261
	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
262 263
		val->intval = voltage;
		break;
264 265
	case POWER_SUPPLY_PROP_PRESENT:
		if (psp == POWER_SUPPLY_PROP_PRESENT)
266
			val->intval = voltage <= 0 ? 0 : 1;
267 268 269 270 271 272 273 274 275 276
		break;
	case POWER_SUPPLY_PROP_CURRENT_NOW:
		val->intval = bq27x00_battery_current(di);
		break;
	case POWER_SUPPLY_PROP_CAPACITY:
		val->intval = bq27x00_battery_rsoc(di);
		break;
	case POWER_SUPPLY_PROP_TEMP:
		val->intval = bq27x00_battery_temperature(di);
		break;
277 278 279 280 281 282 283 284 285
	case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
		ret = bq27x00_battery_time(di, BQ27x00_REG_TTE, val);
		break;
	case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
		ret = bq27x00_battery_time(di, BQ27x00_REG_TTECP, val);
		break;
	case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
		ret = bq27x00_battery_time(di, BQ27x00_REG_TTF, val);
		break;
L
Lars-Peter Clausen 已提交
286 287 288
	case POWER_SUPPLY_PROP_TECHNOLOGY:
		val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
		break;
289 290 291 292
	default:
		return -EINVAL;
	}

293
	return ret;
294 295
}

296
static int bq27x00_powersupply_init(struct bq27x00_device_info *di)
297
{
298 299
	int ret;

300 301 302 303 304
	di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
	di->bat.properties = bq27x00_battery_props;
	di->bat.num_properties = ARRAY_SIZE(bq27x00_battery_props);
	di->bat.get_property = bq27x00_battery_get_property;
	di->bat.external_power_changed = NULL;
305 306 307 308 309 310 311 312 313 314

	ret = power_supply_register(di->dev, &di->bat);
	if (ret) {
		dev_err(di->dev, "failed to register battery: %d\n", ret);
		return ret;
	}

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

	return 0;
315 316
}

317 318 319 320 321 322

/* i2c specific code */
#ifdef CONFIG_BATTERY_BQ27X00_I2C

/* If the system has several batteries we need a different name for each
 * of them...
323
 */
324 325
static DEFINE_IDR(battery_id);
static DEFINE_MUTEX(battery_mutex);
326

327 328
static int bq27x00_read_i2c(struct bq27x00_device_info *di, u8 reg,
			int *rt_value, bool single)
329
{
330
	struct i2c_client *client = to_i2c_client(di->dev);
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
	struct i2c_msg msg[1];
	unsigned char data[2];
	int err;

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

	msg->addr = client->addr;
	msg->flags = 0;
	msg->len = 1;
	msg->buf = data;

	data[0] = reg;
	err = i2c_transfer(client->adapter, msg, 1);

	if (err >= 0) {
347
		if (!single)
348 349 350 351 352 353 354
			msg->len = 2;
		else
			msg->len = 1;

		msg->flags = I2C_M_RD;
		err = i2c_transfer(client->adapter, msg, 1);
		if (err >= 0) {
355
			if (!single)
356
				*rt_value = get_unaligned_le16(data);
357 358 359 360 361 362 363 364 365
			else
				*rt_value = data[0];

			return 0;
		}
	}
	return err;
}

366
static int bq27x00_battery_probe(struct i2c_client *client,
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
				 const struct i2c_device_id *id)
{
	char *name;
	struct bq27x00_device_info *di;
	int num;
	int retval = 0;

	/* Get new ID for the new battery device */
	retval = idr_pre_get(&battery_id, GFP_KERNEL);
	if (retval == 0)
		return -ENOMEM;
	mutex_lock(&battery_mutex);
	retval = idr_get_new(&battery_id, client, &num);
	mutex_unlock(&battery_mutex);
	if (retval < 0)
		return retval;

384
	name = kasprintf(GFP_KERNEL, "%s-%d", id->name, num);
385 386 387 388 389 390 391 392 393 394 395 396
	if (!name) {
		dev_err(&client->dev, "failed to allocate device name\n");
		retval = -ENOMEM;
		goto batt_failed_1;
	}

	di = kzalloc(sizeof(*di), GFP_KERNEL);
	if (!di) {
		dev_err(&client->dev, "failed to allocate device info data\n");
		retval = -ENOMEM;
		goto batt_failed_2;
	}
397

398
	di->id = num;
399
	di->dev = &client->dev;
400
	di->chip = id->driver_data;
401 402
	di->bat.name = name;
	di->bus.read = &bq27x00_read_i2c;
403

404
	if (bq27x00_powersupply_init(di))
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
		goto batt_failed_3;

	i2c_set_clientdata(client, di);

	return 0;

batt_failed_3:
	kfree(di);
batt_failed_2:
	kfree(name);
batt_failed_1:
	mutex_lock(&battery_mutex);
	idr_remove(&battery_id, num);
	mutex_unlock(&battery_mutex);

	return retval;
}

423
static int bq27x00_battery_remove(struct i2c_client *client)
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
{
	struct bq27x00_device_info *di = i2c_get_clientdata(client);

	power_supply_unregister(&di->bat);

	kfree(di->bat.name);

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

	kfree(di);

	return 0;
}

440 441 442
static const struct i2c_device_id bq27x00_id[] = {
	{ "bq27200", BQ27000 },	/* bq27200 is same as bq27000, but with i2c */
	{ "bq27500", BQ27500 },
443 444 445
	{},
};

446
static struct i2c_driver bq27x00_battery_driver = {
447
	.driver = {
448
		.name = "bq27x00-battery",
449
	},
450 451 452
	.probe = bq27x00_battery_probe,
	.remove = bq27x00_battery_remove,
	.id_table = bq27x00_id,
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 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
static inline int bq27x00_battery_i2c_init(void)
{
	int ret = i2c_add_driver(&bq27x00_battery_driver);
	if (ret)
		printk(KERN_ERR "Unable to register BQ27x00 i2c driver\n");

	return ret;
}

static inline void bq27x00_battery_i2c_exit(void)
{
	i2c_del_driver(&bq27x00_battery_driver);
}

#else

static inline int bq27x00_battery_i2c_init(void) { return 0; }
static inline void bq27x00_battery_i2c_exit(void) {};

#endif

/* platform specific code */
#ifdef CONFIG_BATTERY_BQ27X00_PLATFORM

static int bq27000_read_platform(struct bq27x00_device_info *di, u8 reg,
			int *rt_value, bool single)
{
	struct device *dev = di->dev;
	struct bq27000_platform_data *pdata = dev->platform_data;
	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;

		*rt_value = (upper << 8) | lower;
	} else {
		lower = pdata->read(dev, reg);
		if (lower < 0)
			return lower;
		*rt_value = lower;
	}
	return 0;
}

static int __devinit bq27000_battery_probe(struct platform_device *pdev)
{
	struct bq27x00_device_info *di;
	struct bq27000_platform_data *pdata = pdev->dev.platform_data;
	int ret;

	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;
	}

	di = kzalloc(sizeof(*di), GFP_KERNEL);
	if (!di) {
		dev_err(&pdev->dev, "failed to allocate device info data\n");
		return -ENOMEM;
	}

	platform_set_drvdata(pdev, di);

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

	di->bat.name = pdata->name ?: dev_name(&pdev->dev);
	di->bus.read = &bq27000_read_platform;

	ret = bq27x00_powersupply_init(di);
	if (ret)
		goto err_free;

	return 0;

err_free:
	platform_set_drvdata(pdev, NULL);
	kfree(di);

	return ret;
}

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

	power_supply_unregister(&di->bat);
	platform_set_drvdata(pdev, NULL);
	kfree(di);

	return 0;
}

static struct platform_driver bq27000_battery_driver = {
	.probe	= bq27000_battery_probe,
	.remove = __devexit_p(bq27000_battery_remove),
	.driver = {
		.name = "bq27000-battery",
		.owner = THIS_MODULE,
	},
};

static inline int bq27x00_battery_platform_init(void)
{
	int ret = platform_driver_register(&bq27000_battery_driver);
	if (ret)
		printk(KERN_ERR "Unable to register BQ27000 platform driver\n");

	return ret;
}

static inline void bq27x00_battery_platform_exit(void)
{
	platform_driver_unregister(&bq27000_battery_driver);
}

#else

static inline int bq27x00_battery_platform_init(void) { return 0; }
static inline void bq27x00_battery_platform_exit(void) {};

#endif

/*
 * Module stuff
 */

605 606 607 608
static int __init bq27x00_battery_init(void)
{
	int ret;

609 610 611 612 613
	ret = bq27x00_battery_i2c_init();
	if (ret)
		return ret;

	ret = bq27x00_battery_platform_init();
614
	if (ret)
615
		bq27x00_battery_i2c_exit();
616 617 618 619 620 621 622

	return ret;
}
module_init(bq27x00_battery_init);

static void __exit bq27x00_battery_exit(void)
{
623 624
	bq27x00_battery_platform_exit();
	bq27x00_battery_i2c_exit();
625 626 627 628 629 630
}
module_exit(bq27x00_battery_exit);

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