bq27xxx_battery.c 29.0 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
 * Datasheets:
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
 * http://www.ti.com/product/bq27000
 * http://www.ti.com/product/bq27200
 * http://www.ti.com/product/bq27010
 * http://www.ti.com/product/bq27210
 * http://www.ti.com/product/bq27500
 * http://www.ti.com/product/bq27510-g3
 * http://www.ti.com/product/bq27520-g4
 * http://www.ti.com/product/bq27530-g1
 * http://www.ti.com/product/bq27531-g1
 * http://www.ti.com/product/bq27541-g1
 * http://www.ti.com/product/bq27542-g1
 * http://www.ti.com/product/bq27546-g1
 * http://www.ti.com/product/bq27742-g1
 * http://www.ti.com/product/bq27545-g1
 * http://www.ti.com/product/bq27421-g1
35
 * http://www.ti.com/product/bq27425-g1
36 37
 * http://www.ti.com/product/bq27411-g1
 * http://www.ti.com/product/bq27621-g1
P
Pali Rohár 已提交
38 39
 */

40
#include <linux/device.h>
41 42 43 44 45 46 47
#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>
48
#include <linux/slab.h>
49
#include <linux/of.h>
50

51
#include <linux/power/bq27xxx_battery.h>
52

53
#define DRIVER_VERSION		"1.2.0"
54

55 56
#define BQ27XXX_MANUFACTURER	"Texas Instruments"

57 58 59 60 61 62 63
/* BQ27XXX Flags */
#define BQ27XXX_FLAG_DSC	BIT(0)
#define BQ27XXX_FLAG_SOCF	BIT(1) /* State-of-Charge threshold final */
#define BQ27XXX_FLAG_SOC1	BIT(2) /* State-of-Charge threshold 1 */
#define BQ27XXX_FLAG_FC		BIT(9)
#define BQ27XXX_FLAG_OTD	BIT(14)
#define BQ27XXX_FLAG_OTC	BIT(15)
64 65
#define BQ27XXX_FLAG_UT		BIT(14)
#define BQ27XXX_FLAG_OT		BIT(15)
66 67

/* BQ27000 has different layout for Flags register */
68 69 70 71 72 73
#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 */

74 75 76
#define BQ27XXX_RS			(20) /* Resistor sense mOhm */
#define BQ27XXX_POWER_CONSTANT		(29200) /* 29.2 µV^2 * 1000 */
#define BQ27XXX_CURRENT_CONSTANT	(3570) /* 3.57 µV * 1000 */
P
Pali Rohár 已提交
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
#define INVALID_REG_ADDR	0xff

/*
 * bq27xxx_reg_index - Register names
 *
 * These are indexes into a device's register mapping array.
 */
enum bq27xxx_reg_index {
	BQ27XXX_REG_CTRL = 0,	/* Control */
	BQ27XXX_REG_TEMP,	/* Temperature */
	BQ27XXX_REG_INT_TEMP,	/* Internal Temperature */
	BQ27XXX_REG_VOLT,	/* Voltage */
	BQ27XXX_REG_AI,		/* Average Current */
	BQ27XXX_REG_FLAGS,	/* Flags */
	BQ27XXX_REG_TTE,	/* Time-to-Empty */
	BQ27XXX_REG_TTF,	/* Time-to-Full */
	BQ27XXX_REG_TTES,	/* Time-to-Empty Standby */
	BQ27XXX_REG_TTECP,	/* Time-to-Empty at Constant Power */
	BQ27XXX_REG_NAC,	/* Nominal Available Capacity */
	BQ27XXX_REG_FCC,	/* Full Charge Capacity */
	BQ27XXX_REG_CYCT,	/* Cycle Count */
	BQ27XXX_REG_AE,		/* Available Energy */
	BQ27XXX_REG_SOC,	/* State-of-Charge */
	BQ27XXX_REG_DCAP,	/* Design Capacity */
	BQ27XXX_REG_AP,		/* Average Power */
};

/* Register mappings */
static u8 bq27000_regs[] = {
	0x00,	/* CONTROL	*/
	0x06,	/* TEMP		*/
	INVALID_REG_ADDR,	/* INT TEMP - NA*/
	0x08,	/* VOLT		*/
	0x14,	/* AVG CURR	*/
	0x0a,	/* FLAGS	*/
	0x16,	/* TTE		*/
	0x18,	/* TTF		*/
	0x1c,	/* TTES		*/
	0x26,	/* TTECP	*/
	0x0c,	/* NAC		*/
	0x12,	/* LMD(FCC)	*/
	0x2a,	/* CYCT		*/
	0x22,	/* AE		*/
	0x0b,	/* SOC(RSOC)	*/
	0x76,	/* DCAP(ILMD)	*/
	0x24,	/* AP		*/
};

static u8 bq27010_regs[] = {
	0x00,	/* CONTROL	*/
	0x06,	/* TEMP		*/
	INVALID_REG_ADDR,	/* INT TEMP - NA*/
	0x08,	/* VOLT		*/
	0x14,	/* AVG CURR	*/
	0x0a,	/* FLAGS	*/
	0x16,	/* TTE		*/
	0x18,	/* TTF		*/
	0x1c,	/* TTES		*/
	0x26,	/* TTECP	*/
	0x0c,	/* NAC		*/
	0x12,	/* LMD(FCC)	*/
	0x2a,	/* CYCT		*/
	INVALID_REG_ADDR,	/* AE - NA	*/
	0x0b,	/* SOC(RSOC)	*/
	0x76,	/* DCAP(ILMD)	*/
	INVALID_REG_ADDR,	/* AP - NA	*/
};

static u8 bq27500_regs[] = {
	0x00,	/* CONTROL	*/
	0x06,	/* TEMP		*/
	0x28,	/* INT TEMP	*/
	0x08,	/* VOLT		*/
	0x14,	/* AVG CURR	*/
	0x0a,	/* FLAGS	*/
	0x16,	/* TTE		*/
	INVALID_REG_ADDR,	/* TTF - NA	*/
	0x1a,	/* TTES		*/
	INVALID_REG_ADDR,	/* TTECP - NA	*/
	0x0c,	/* NAC		*/
	0x12,	/* LMD(FCC)	*/
159
	0x2a,	/* CYCT		*/
160
	INVALID_REG_ADDR,	/* AE - NA	*/
161 162
	0x2c,	/* SOC(RSOC)	*/
	0x3c,	/* DCAP(ILMD)	*/
163
	INVALID_REG_ADDR,	/* AP - NA	*/
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
static u8 bq27530_regs[] = {
	0x00,	/* CONTROL	*/
	0x06,	/* TEMP		*/
	0x32,	/* INT TEMP	*/
	0x08,	/* VOLT		*/
	0x14,	/* AVG CURR	*/
	0x0a,	/* FLAGS	*/
	0x16,	/* TTE		*/
	INVALID_REG_ADDR,	/* TTF - NA	*/
	INVALID_REG_ADDR,	/* TTES - NA	*/
	INVALID_REG_ADDR,	/* TTECP - NA	*/
	0x0c,	/* NAC		*/
	0x12,	/* LMD(FCC)	*/
	0x2a,	/* CYCT		*/
	INVALID_REG_ADDR,	/* AE - NA	*/
	0x2c,	/* SOC(RSOC)	*/
	INVALID_REG_ADDR,	/* DCAP - NA	*/
	0x24,	/* AP		*/
};

static u8 bq27541_regs[] = {
	0x00,	/* CONTROL	*/
	0x06,	/* TEMP		*/
	0x28,	/* INT TEMP	*/
	0x08,	/* VOLT		*/
	0x14,	/* AVG CURR	*/
	0x0a,	/* FLAGS	*/
	0x16,	/* TTE		*/
	INVALID_REG_ADDR,	/* TTF - NA	*/
	INVALID_REG_ADDR,	/* TTES - NA	*/
	INVALID_REG_ADDR,	/* TTECP - NA	*/
	0x0c,	/* NAC		*/
	0x12,	/* LMD(FCC)	*/
	0x2a,	/* CYCT		*/
	INVALID_REG_ADDR,	/* AE - NA	*/
	0x2c,	/* SOC(RSOC)	*/
	0x3c,	/* DCAP		*/
203
	0x24,	/* AP		*/
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
};

static u8 bq27545_regs[] = {
	0x00,	/* CONTROL	*/
	0x06,	/* TEMP		*/
	0x28,	/* INT TEMP	*/
	0x08,	/* VOLT		*/
	0x14,	/* AVG CURR	*/
	0x0a,	/* FLAGS	*/
	0x16,	/* TTE		*/
	INVALID_REG_ADDR,	/* TTF - NA	*/
	INVALID_REG_ADDR,	/* TTES - NA	*/
	INVALID_REG_ADDR,	/* TTECP - NA	*/
	0x0c,	/* NAC		*/
	0x12,	/* LMD(FCC)	*/
	0x2a,	/* CYCT		*/
	INVALID_REG_ADDR,	/* AE - NA	*/
	0x2c,	/* SOC(RSOC)	*/
	INVALID_REG_ADDR,	/* DCAP - NA */
	0x24,	/* AP		*/
};

static u8 bq27421_regs[] = {
	0x00,	/* CONTROL	*/
	0x02,	/* TEMP		*/
	0x1e,	/* INT TEMP	*/
	0x04,	/* VOLT		*/
	0x10,	/* AVG CURR	*/
	0x06,	/* FLAGS	*/
	INVALID_REG_ADDR,	/* TTE - NA	*/
	INVALID_REG_ADDR,	/* TTF - NA	*/
	INVALID_REG_ADDR,	/* TTES - NA	*/
	INVALID_REG_ADDR,	/* TTECP - NA	*/
	0x08,	/* NAC		*/
	0x0e,	/* FCC		*/
	INVALID_REG_ADDR,	/* CYCT - NA	*/
	INVALID_REG_ADDR,	/* AE - NA	*/
	0x1c,	/* SOC		*/
	0x3c,	/* DCAP		*/
	0x18,	/* AP		*/
};

static u8 *bq27xxx_regs[] = {
	[BQ27000] = bq27000_regs,
	[BQ27010] = bq27010_regs,
	[BQ27500] = bq27500_regs,
	[BQ27530] = bq27530_regs,
	[BQ27541] = bq27541_regs,
	[BQ27545] = bq27545_regs,
	[BQ27421] = bq27421_regs,
};

static enum power_supply_property bq27000_battery_props[] = {
257
	POWER_SUPPLY_PROP_STATUS,
258 259 260 261
	POWER_SUPPLY_PROP_PRESENT,
	POWER_SUPPLY_PROP_VOLTAGE_NOW,
	POWER_SUPPLY_PROP_CURRENT_NOW,
	POWER_SUPPLY_PROP_CAPACITY,
262
	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
263
	POWER_SUPPLY_PROP_TEMP,
264 265 266
	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 已提交
267
	POWER_SUPPLY_PROP_TECHNOLOGY,
P
Pali Rohár 已提交
268 269 270
	POWER_SUPPLY_PROP_CHARGE_FULL,
	POWER_SUPPLY_PROP_CHARGE_NOW,
	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
271
	POWER_SUPPLY_PROP_CYCLE_COUNT,
P
Pali Rohár 已提交
272
	POWER_SUPPLY_PROP_ENERGY_NOW,
273 274
	POWER_SUPPLY_PROP_POWER_AVG,
	POWER_SUPPLY_PROP_HEALTH,
275
	POWER_SUPPLY_PROP_MANUFACTURER,
276 277
};

278
static enum power_supply_property bq27010_battery_props[] = {
279 280 281 282 283 284 285
	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,
286 287 288
	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
	POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
	POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
289 290 291 292
	POWER_SUPPLY_PROP_TECHNOLOGY,
	POWER_SUPPLY_PROP_CHARGE_FULL,
	POWER_SUPPLY_PROP_CHARGE_NOW,
	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
293 294
	POWER_SUPPLY_PROP_CYCLE_COUNT,
	POWER_SUPPLY_PROP_HEALTH,
295
	POWER_SUPPLY_PROP_MANUFACTURER,
296 297
};

298
static enum power_supply_property bq27500_battery_props[] = {
299 300 301 302 303 304 305 306 307 308 309 310 311
	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,
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
	POWER_SUPPLY_PROP_HEALTH,
	POWER_SUPPLY_PROP_MANUFACTURER,
};

static enum power_supply_property bq27530_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,
328 329
	POWER_SUPPLY_PROP_POWER_AVG,
	POWER_SUPPLY_PROP_HEALTH,
330
	POWER_SUPPLY_PROP_CYCLE_COUNT,
331
	POWER_SUPPLY_PROP_MANUFACTURER,
332 333
};

334
static enum power_supply_property bq27541_battery_props[] = {
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
	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,
350
	POWER_SUPPLY_PROP_MANUFACTURER,
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
static enum power_supply_property bq27545_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_HEALTH,
	POWER_SUPPLY_PROP_CYCLE_COUNT,
	POWER_SUPPLY_PROP_POWER_AVG,
	POWER_SUPPLY_PROP_MANUFACTURER,
};

static enum power_supply_property bq27421_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,
	POWER_SUPPLY_PROP_MANUFACTURER,
};

#define BQ27XXX_PROP(_id, _prop)		\
	[_id] = {				\
		.props = _prop,			\
		.size = ARRAY_SIZE(_prop),	\
	}

static struct {
	enum power_supply_property *props;
	size_t size;
} bq27xxx_battery_props[] = {
	BQ27XXX_PROP(BQ27000, bq27000_battery_props),
	BQ27XXX_PROP(BQ27010, bq27010_battery_props),
	BQ27XXX_PROP(BQ27500, bq27500_battery_props),
	BQ27XXX_PROP(BQ27530, bq27530_battery_props),
	BQ27XXX_PROP(BQ27541, bq27541_battery_props),
	BQ27XXX_PROP(BQ27545, bq27545_battery_props),
	BQ27XXX_PROP(BQ27421, bq27421_battery_props),
};

L
Lars-Peter Clausen 已提交
405 406
static unsigned int poll_interval = 360;
module_param(poll_interval, uint, 0644);
407 408
MODULE_PARM_DESC(poll_interval,
		 "battery poll interval in seconds - 0 disables polling");
L
Lars-Peter Clausen 已提交
409

410
/*
411
 * Common code for BQ27xxx devices
412 413
 */

414
static inline int bq27xxx_read(struct bq27xxx_device_info *di, int reg_index,
415
			       bool single)
416
{
417 418 419
	/* Reports EINVAL for invalid/missing registers */
	if (!di || di->regs[reg_index] == INVALID_REG_ADDR)
		return -EINVAL;
420

421
	return di->bus.read(di, di->regs[reg_index], single);
422 423
}

424
/*
425
 * Return the battery State-of-Charge
426 427
 * Or < 0 if something fails.
 */
428
static int bq27xxx_battery_read_soc(struct bq27xxx_device_info *di)
429
{
430
	int soc;
431

432 433 434 435
	if (di->chip == BQ27000 || di->chip == BQ27010)
		soc = bq27xxx_read(di, BQ27XXX_REG_SOC, true);
	else
		soc = bq27xxx_read(di, BQ27XXX_REG_SOC, false);
436

437 438
	if (soc < 0)
		dev_dbg(di->dev, "error reading State-of-Charge\n");
439

440
	return soc;
441 442
}

P
Pali Rohár 已提交
443 444 445 446
/*
 * Return a battery charge value in µAh
 * Or < 0 if something fails.
 */
447
static int bq27xxx_battery_read_charge(struct bq27xxx_device_info *di, u8 reg)
P
Pali Rohár 已提交
448 449 450
{
	int charge;

451
	charge = bq27xxx_read(di, reg, false);
P
Pali Rohár 已提交
452
	if (charge < 0) {
453 454
		dev_dbg(di->dev, "error reading charge register %02x: %d\n",
			reg, charge);
P
Pali Rohár 已提交
455 456 457
		return charge;
	}

458 459
	if (di->chip == BQ27000 || di->chip == BQ27010)
		charge *= BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS;
P
Pali Rohár 已提交
460
	else
461
		charge *= 1000;
P
Pali Rohár 已提交
462 463 464 465 466

	return charge;
}

/*
467
 * Return the battery Nominal available capacity in µAh
P
Pali Rohár 已提交
468 469
 * Or < 0 if something fails.
 */
470
static inline int bq27xxx_battery_read_nac(struct bq27xxx_device_info *di)
P
Pali Rohár 已提交
471
{
472 473
	int flags;

474 475 476 477 478
	if (di->chip == BQ27000 || di->chip == BQ27010) {
		flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, true);
		if (flags >= 0 && (flags & BQ27000_FLAG_CI))
			return -ENODATA;
	}
479

480
	return bq27xxx_battery_read_charge(di, BQ27XXX_REG_NAC);
P
Pali Rohár 已提交
481 482 483
}

/*
484
 * Return the battery Full Charge Capacity in µAh
P
Pali Rohár 已提交
485 486
 * Or < 0 if something fails.
 */
487
static inline int bq27xxx_battery_read_fcc(struct bq27xxx_device_info *di)
P
Pali Rohár 已提交
488
{
489
	return bq27xxx_battery_read_charge(di, BQ27XXX_REG_FCC);
P
Pali Rohár 已提交
490 491 492
}

/*
493
 * Return the Design Capacity in µAh
P
Pali Rohár 已提交
494 495
 * Or < 0 if something fails.
 */
496
static int bq27xxx_battery_read_dcap(struct bq27xxx_device_info *di)
P
Pali Rohár 已提交
497
{
498
	int dcap;
P
Pali Rohár 已提交
499

500 501 502 503
	if (di->chip == BQ27000 || di->chip == BQ27010)
		dcap = bq27xxx_read(di, BQ27XXX_REG_DCAP, true);
	else
		dcap = bq27xxx_read(di, BQ27XXX_REG_DCAP, false);
P
Pali Rohár 已提交
504

505
	if (dcap < 0) {
506
		dev_dbg(di->dev, "error reading initial last measured discharge\n");
507
		return dcap;
P
Pali Rohár 已提交
508 509
	}

510
	if (di->chip == BQ27000 || di->chip == BQ27010)
511
		dcap = (dcap << 8) * BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS;
P
Pali Rohár 已提交
512
	else
513
		dcap *= 1000;
P
Pali Rohár 已提交
514

515
	return dcap;
P
Pali Rohár 已提交
516 517
}

518 519 520 521
/*
 * Return the battery Available energy in µWh
 * Or < 0 if something fails.
 */
522
static int bq27xxx_battery_read_energy(struct bq27xxx_device_info *di)
523 524 525
{
	int ae;

526
	ae = bq27xxx_read(di, BQ27XXX_REG_AE, false);
527
	if (ae < 0) {
528
		dev_dbg(di->dev, "error reading available energy\n");
529 530 531
		return ae;
	}

532 533
	if (di->chip == BQ27000 || di->chip == BQ27010)
		ae *= BQ27XXX_POWER_CONSTANT / BQ27XXX_RS;
534
	else
535
		ae *= 1000;
536 537 538 539

	return ae;
}

540
/*
541
 * Return the battery temperature in tenths of degree Kelvin
542 543
 * Or < 0 if something fails.
 */
544
static int bq27xxx_battery_read_temperature(struct bq27xxx_device_info *di)
545 546 547
{
	int temp;

548
	temp = bq27xxx_read(di, BQ27XXX_REG_TEMP, false);
549 550 551 552 553
	if (temp < 0) {
		dev_err(di->dev, "error reading temperature\n");
		return temp;
	}

554
	if (di->chip == BQ27000 || di->chip == BQ27010)
555
		temp = 5 * temp / 2;
556 557 558 559

	return temp;
}

P
Pali Rohár 已提交
560 561 562 563
/*
 * Return the battery Cycle count total
 * Or < 0 if something fails.
 */
564
static int bq27xxx_battery_read_cyct(struct bq27xxx_device_info *di)
P
Pali Rohár 已提交
565 566 567
{
	int cyct;

568
	cyct = bq27xxx_read(di, BQ27XXX_REG_CYCT, false);
P
Pali Rohár 已提交
569 570 571 572 573 574
	if (cyct < 0)
		dev_err(di->dev, "error reading cycle count total\n");

	return cyct;
}

575
/*
576 577
 * Read a time register.
 * Return < 0 if something fails.
578
 */
579
static int bq27xxx_battery_read_time(struct bq27xxx_device_info *di, u8 reg)
580
{
581
	int tval;
582

583
	tval = bq27xxx_read(di, reg, false);
584
	if (tval < 0) {
585 586
		dev_dbg(di->dev, "error reading time register %02x: %d\n",
			reg, tval);
587
		return tval;
588 589
	}

590 591 592 593 594 595
	if (tval == 65535)
		return -ENODATA;

	return tval * 60;
}

596
/*
597
 * Read an average power register.
598 599
 * Return < 0 if something fails.
 */
600
static int bq27xxx_battery_read_pwr_avg(struct bq27xxx_device_info *di)
601 602 603
{
	int tval;

604
	tval = bq27xxx_read(di, BQ27XXX_REG_AP, false);
605
	if (tval < 0) {
606 607
		dev_err(di->dev, "error reading average power register  %02x: %d\n",
			BQ27XXX_REG_AP, tval);
608 609 610
		return tval;
	}

611 612 613
	if (di->chip == BQ27000 || di->chip == BQ27010)
		return (tval * BQ27XXX_POWER_CONSTANT) / BQ27XXX_RS;
	else
614
		return tval;
615 616 617 618 619
}

/*
 * Returns true if a battery over temperature condition is detected
 */
620
static bool bq27xxx_battery_overtemp(struct bq27xxx_device_info *di, u16 flags)
621
{
622
	if (di->chip == BQ27500 || di->chip == BQ27541 || di->chip == BQ27545)
623
		return flags & (BQ27XXX_FLAG_OTC | BQ27XXX_FLAG_OTD);
624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
	if (di->chip == BQ27530 || di->chip == BQ27421)
		return flags & BQ27XXX_FLAG_OT;

	return false;
}

/*
 * Returns true if a battery under temperature condition is detected
 */
static bool bq27xxx_battery_undertemp(struct bq27xxx_device_info *di, u16 flags)
{
	if (di->chip == BQ27530 || di->chip == BQ27421)
		return flags & BQ27XXX_FLAG_UT;

	return false;
}

/*
 * Returns true if a low state of charge condition is detected
 */
static bool bq27xxx_battery_dead(struct bq27xxx_device_info *di, u16 flags)
{
	if (di->chip == BQ27000 || di->chip == BQ27010)
		return flags & (BQ27000_FLAG_EDV1 | BQ27000_FLAG_EDVF);
648
	else
649
		return flags & (BQ27XXX_FLAG_SOC1 | BQ27XXX_FLAG_SOCF);
650 651 652 653 654 655
}

/*
 * Read flag register.
 * Return < 0 if something fails.
 */
656
static int bq27xxx_battery_read_health(struct bq27xxx_device_info *di)
657
{
658
	int flags;
659

660 661 662 663
	flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, false);
	if (flags < 0) {
		dev_err(di->dev, "error reading flag register:%d\n", flags);
		return flags;
664 665
	}

666 667 668 669 670 671 672
	/* Unlikely but important to return first */
	if (unlikely(bq27xxx_battery_overtemp(di, flags)))
		return POWER_SUPPLY_HEALTH_OVERHEAT;
	if (unlikely(bq27xxx_battery_undertemp(di, flags)))
		return POWER_SUPPLY_HEALTH_COLD;
	if (unlikely(bq27xxx_battery_dead(di, flags)))
		return POWER_SUPPLY_HEALTH_DEAD;
673

674
	return POWER_SUPPLY_HEALTH_GOOD;
675 676
}

677
void bq27xxx_battery_update(struct bq27xxx_device_info *di)
678
{
679
	struct bq27xxx_reg_cache cache = {0, };
680 681
	bool has_ci_flag = di->chip == BQ27000 || di->chip == BQ27010;
	bool has_singe_flag = di->chip == BQ27000 || di->chip == BQ27010;
682

683
	cache.flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, has_singe_flag);
684
	if ((cache.flags & 0xff) == 0xff)
685
		cache.flags = -1; /* read error */
686
	if (cache.flags >= 0) {
687 688
		cache.temperature = bq27xxx_battery_read_temperature(di);
		if (has_ci_flag && (cache.flags & BQ27000_FLAG_CI)) {
689
			dev_info_once(di->dev, "battery is not calibrated! ignoring capacity values\n");
690
			cache.capacity = -ENODATA;
691
			cache.energy = -ENODATA;
692 693 694 695
			cache.time_to_empty = -ENODATA;
			cache.time_to_empty_avg = -ENODATA;
			cache.time_to_full = -ENODATA;
			cache.charge_full = -ENODATA;
696
			cache.health = -ENODATA;
697
		} else {
698 699 700 701 702 703 704
			if (di->regs[BQ27XXX_REG_TTE] != INVALID_REG_ADDR)
				cache.time_to_empty = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTE);
			if (di->regs[BQ27XXX_REG_TTECP] != INVALID_REG_ADDR)
				cache.time_to_empty_avg = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTECP);
			if (di->regs[BQ27XXX_REG_TTF] != INVALID_REG_ADDR)
				cache.time_to_full = bq27xxx_battery_read_time(di, BQ27XXX_REG_TTF);
			cache.charge_full = bq27xxx_battery_read_fcc(di);
705
			cache.capacity = bq27xxx_battery_read_soc(di);
706
			if (di->regs[BQ27XXX_REG_AE] != INVALID_REG_ADDR)
707 708
				cache.energy = bq27xxx_battery_read_energy(di);
			cache.health = bq27xxx_battery_read_health(di);
709
		}
710
		if (di->regs[BQ27XXX_REG_CYCT] != INVALID_REG_ADDR)
711
			cache.cycle_count = bq27xxx_battery_read_cyct(di);
712 713
		if (di->regs[BQ27XXX_REG_AP] != INVALID_REG_ADDR)
			cache.power_avg = bq27xxx_battery_read_pwr_avg(di);
714

P
Pali Rohár 已提交
715 716
		/* We only have to read charge design full once */
		if (di->charge_design_full <= 0)
717
			di->charge_design_full = bq27xxx_battery_read_dcap(di);
718 719
	}

720
	if (di->cache.capacity != cache.capacity)
721
		power_supply_changed(di->bat);
722 723 724

	if (memcmp(&di->cache, &cache, sizeof(cache)) != 0)
		di->cache = cache;
725 726 727

	di->last_update = jiffies;
}
728
EXPORT_SYMBOL_GPL(bq27xxx_battery_update);
729

730
static void bq27xxx_battery_poll(struct work_struct *work)
L
Lars-Peter Clausen 已提交
731
{
732
	struct bq27xxx_device_info *di =
733 734
			container_of(work, struct bq27xxx_device_info,
				     work.work);
L
Lars-Peter Clausen 已提交
735

736
	bq27xxx_battery_update(di);
L
Lars-Peter Clausen 已提交
737

738
	if (poll_interval > 0)
L
Lars-Peter Clausen 已提交
739 740 741
		schedule_delayed_work(&di->work, poll_interval * HZ);
}

742
/*
P
Pali Rohár 已提交
743
 * Return the battery average current in µA
744 745 746
 * Note that current can be negative signed as well
 * Or 0 if something fails.
 */
747
static int bq27xxx_battery_current(struct bq27xxx_device_info *di,
748
				   union power_supply_propval *val)
749
{
750
	int curr;
751
	int flags;
752

753
	curr = bq27xxx_read(di, BQ27XXX_REG_AI, false);
754 755
	if (curr < 0) {
		dev_err(di->dev, "error reading current\n");
756
		return curr;
757
	}
758

759 760
	if (di->chip == BQ27000 || di->chip == BQ27010) {
		flags = bq27xxx_read(di, BQ27XXX_REG_FLAGS, false);
761
		if (flags & BQ27000_FLAG_CHGS) {
762
			dev_dbg(di->dev, "negative current!\n");
763
			curr = -curr;
764
		}
765

766 767 768 769
		val->intval = curr * BQ27XXX_CURRENT_CONSTANT / BQ27XXX_RS;
	} else {
		/* Other gauges return signed value */
		val->intval = (int)((s16)curr) * 1000;
770 771
	}

772
	return 0;
773 774
}

775
static int bq27xxx_battery_status(struct bq27xxx_device_info *di,
776
				  union power_supply_propval *val)
777 778 779
{
	int status;

780
	if (di->chip == BQ27000 || di->chip == BQ27010) {
781 782 783
		if (di->cache.flags & BQ27000_FLAG_FC)
			status = POWER_SUPPLY_STATUS_FULL;
		else if (di->cache.flags & BQ27000_FLAG_CHGS)
784
			status = POWER_SUPPLY_STATUS_CHARGING;
785
		else if (power_supply_am_i_supplied(di->bat))
786
			status = POWER_SUPPLY_STATUS_NOT_CHARGING;
787 788
		else
			status = POWER_SUPPLY_STATUS_DISCHARGING;
789 790 791 792 793 794 795
	} else {
		if (di->cache.flags & BQ27XXX_FLAG_FC)
			status = POWER_SUPPLY_STATUS_FULL;
		else if (di->cache.flags & BQ27XXX_FLAG_DSC)
			status = POWER_SUPPLY_STATUS_DISCHARGING;
		else
			status = POWER_SUPPLY_STATUS_CHARGING;
796 797 798
	}

	val->intval = status;
799

800 801 802
	return 0;
}

803
static int bq27xxx_battery_capacity_level(struct bq27xxx_device_info *di,
804
					  union power_supply_propval *val)
805 806 807
{
	int level;

808 809
	if (di->chip == BQ27000 || di->chip == BQ27010) {
		if (di->cache.flags & BQ27000_FLAG_FC)
810
			level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
811
		else if (di->cache.flags & BQ27000_FLAG_EDV1)
812
			level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
813
		else if (di->cache.flags & BQ27000_FLAG_EDVF)
814 815 816 817
			level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
		else
			level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
	} else {
818
		if (di->cache.flags & BQ27XXX_FLAG_FC)
819
			level = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
820
		else if (di->cache.flags & BQ27XXX_FLAG_SOC1)
821
			level = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
822
		else if (di->cache.flags & BQ27XXX_FLAG_SOCF)
823 824 825 826 827 828 829 830 831 832
			level = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
		else
			level = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
	}

	val->intval = level;

	return 0;
}

833
/*
T
Thomas Weber 已提交
834
 * Return the battery Voltage in millivolts
835
 * Or < 0 if something fails.
836
 */
837
static int bq27xxx_battery_voltage(struct bq27xxx_device_info *di,
838
				   union power_supply_propval *val)
839
{
840
	int volt;
841

842
	volt = bq27xxx_read(di, BQ27XXX_REG_VOLT, false);
843 844
	if (volt < 0) {
		dev_err(di->dev, "error reading voltage\n");
845
		return volt;
846
	}
847

848 849 850 851 852
	val->intval = volt * 1000;

	return 0;
}

853
static int bq27xxx_simple_value(int value,
854
				union power_supply_propval *val)
855 856 857 858 859
{
	if (value < 0)
		return value;

	val->intval = value;
860 861 862 863

	return 0;
}

864
static int bq27xxx_battery_get_property(struct power_supply *psy,
865 866 867
					enum power_supply_property psp,
					union power_supply_propval *val)
{
868
	int ret = 0;
869
	struct bq27xxx_device_info *di = power_supply_get_drvdata(psy);
870

L
Lars-Peter Clausen 已提交
871 872 873
	mutex_lock(&di->lock);
	if (time_is_before_jiffies(di->last_update + 5 * HZ)) {
		cancel_delayed_work_sync(&di->work);
874
		bq27xxx_battery_poll(&di->work.work);
L
Lars-Peter Clausen 已提交
875 876
	}
	mutex_unlock(&di->lock);
877 878

	if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0)
879
		return -ENODEV;
880 881

	switch (psp) {
882
	case POWER_SUPPLY_PROP_STATUS:
883
		ret = bq27xxx_battery_status(di, val);
884
		break;
885
	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
886
		ret = bq27xxx_battery_voltage(di, val);
887
		break;
888
	case POWER_SUPPLY_PROP_PRESENT:
889
		val->intval = di->cache.flags < 0 ? 0 : 1;
890 891
		break;
	case POWER_SUPPLY_PROP_CURRENT_NOW:
892
		ret = bq27xxx_battery_current(di, val);
893 894
		break;
	case POWER_SUPPLY_PROP_CAPACITY:
895
		ret = bq27xxx_simple_value(di->cache.capacity, val);
896
		break;
897
	case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
898
		ret = bq27xxx_battery_capacity_level(di, val);
899
		break;
900
	case POWER_SUPPLY_PROP_TEMP:
901
		ret = bq27xxx_simple_value(di->cache.temperature, val);
902
		if (ret == 0)
903
			val->intval -= 2731; /* convert decidegree k to c */
904
		break;
905
	case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
906
		ret = bq27xxx_simple_value(di->cache.time_to_empty, val);
907 908
		break;
	case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
909
		ret = bq27xxx_simple_value(di->cache.time_to_empty_avg, val);
910 911
		break;
	case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
912
		ret = bq27xxx_simple_value(di->cache.time_to_full, val);
913
		break;
L
Lars-Peter Clausen 已提交
914 915 916
	case POWER_SUPPLY_PROP_TECHNOLOGY:
		val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
		break;
P
Pali Rohár 已提交
917
	case POWER_SUPPLY_PROP_CHARGE_NOW:
918
		ret = bq27xxx_simple_value(bq27xxx_battery_read_nac(di), val);
P
Pali Rohár 已提交
919 920
		break;
	case POWER_SUPPLY_PROP_CHARGE_FULL:
921
		ret = bq27xxx_simple_value(di->cache.charge_full, val);
P
Pali Rohár 已提交
922 923
		break;
	case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
924
		ret = bq27xxx_simple_value(di->charge_design_full, val);
P
Pali Rohár 已提交
925
		break;
926
	case POWER_SUPPLY_PROP_CYCLE_COUNT:
927
		ret = bq27xxx_simple_value(di->cache.cycle_count, val);
P
Pali Rohár 已提交
928 929
		break;
	case POWER_SUPPLY_PROP_ENERGY_NOW:
930
		ret = bq27xxx_simple_value(di->cache.energy, val);
P
Pali Rohár 已提交
931
		break;
932
	case POWER_SUPPLY_PROP_POWER_AVG:
933
		ret = bq27xxx_simple_value(di->cache.power_avg, val);
934 935
		break;
	case POWER_SUPPLY_PROP_HEALTH:
936
		ret = bq27xxx_simple_value(di->cache.health, val);
937
		break;
938 939 940
	case POWER_SUPPLY_PROP_MANUFACTURER:
		val->strval = BQ27XXX_MANUFACTURER;
		break;
941 942 943 944
	default:
		return -EINVAL;
	}

945
	return ret;
946 947
}

948
static void bq27xxx_external_power_changed(struct power_supply *psy)
L
Lars-Peter Clausen 已提交
949
{
950
	struct bq27xxx_device_info *di = power_supply_get_drvdata(psy);
L
Lars-Peter Clausen 已提交
951 952 953 954 955

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

956
int bq27xxx_battery_setup(struct bq27xxx_device_info *di)
957
{
958 959 960
	struct power_supply_desc *psy_desc;
	struct power_supply_config psy_cfg = { .drv_data = di, };

961 962 963 964
	INIT_DELAYED_WORK(&di->work, bq27xxx_battery_poll);
	mutex_init(&di->lock);
	di->regs = bq27xxx_regs[di->chip];

965 966 967
	psy_desc = devm_kzalloc(di->dev, sizeof(*psy_desc), GFP_KERNEL);
	if (!psy_desc)
		return -ENOMEM;
968

969
	psy_desc->name = di->name;
970
	psy_desc->type = POWER_SUPPLY_TYPE_BATTERY;
971 972
	psy_desc->properties = bq27xxx_battery_props[di->chip].props;
	psy_desc->num_properties = bq27xxx_battery_props[di->chip].size;
973 974
	psy_desc->get_property = bq27xxx_battery_get_property;
	psy_desc->external_power_changed = bq27xxx_external_power_changed;
L
Lars-Peter Clausen 已提交
975

976 977
	di->bat = power_supply_register_no_ws(di->dev, psy_desc, &psy_cfg);
	if (IS_ERR(di->bat)) {
978 979
		dev_err(di->dev, "failed to register battery\n");
		return PTR_ERR(di->bat);
980 981 982 983
	}

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

984
	bq27xxx_battery_update(di);
985

986
	return 0;
987
}
988
EXPORT_SYMBOL_GPL(bq27xxx_battery_setup);
989

990
void bq27xxx_battery_teardown(struct bq27xxx_device_info *di)
L
Lars-Peter Clausen 已提交
991
{
992
	/*
993 994 995
	 * power_supply_unregister call bq27xxx_battery_get_property which
	 * call bq27xxx_battery_poll.
	 * Make sure that bq27xxx_battery_poll will not call
996 997 998 999
	 * schedule_delayed_work again after unregister (which cause OOPS).
	 */
	poll_interval = 0;

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

1002
	power_supply_unregister(di->bat);
L
Lars-Peter Clausen 已提交
1003 1004 1005

	mutex_destroy(&di->lock);
}
1006
EXPORT_SYMBOL_GPL(bq27xxx_battery_teardown);
1007

1008 1009
static int bq27xxx_battery_platform_read(struct bq27xxx_device_info *di, u8 reg,
					 bool single)
1010 1011
{
	struct device *dev = di->dev;
1012
	struct bq27xxx_platform_data *pdata = dev->platform_data;
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
	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;

1036
		return (upper << 8) | lower;
1037
	}
1038 1039

	return pdata->read(dev, reg);
1040 1041
}

1042
static int bq27xxx_battery_platform_probe(struct platform_device *pdev)
1043
{
1044 1045
	struct bq27xxx_device_info *di;
	struct bq27xxx_platform_data *pdata = pdev->dev.platform_data;
1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056

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

1057 1058 1059 1060 1061
	if (!pdata->chip) {
		dev_err(&pdev->dev, "no device supplied\n");
		return -EINVAL;
	}

1062
	di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
1063
	if (!di)
1064 1065 1066 1067 1068
		return -ENOMEM;

	platform_set_drvdata(pdev, di);

	di->dev = &pdev->dev;
1069
	di->chip = pdata->chip;
1070 1071
	di->name = pdata->name ?: dev_name(&pdev->dev);
	di->bus.read = bq27xxx_battery_platform_read;
1072

1073
	return bq27xxx_battery_setup(di);
1074 1075
}

1076
static int bq27xxx_battery_platform_remove(struct platform_device *pdev)
1077
{
1078
	struct bq27xxx_device_info *di = platform_get_drvdata(pdev);
1079

1080
	bq27xxx_battery_teardown(di);
L
Lars-Peter Clausen 已提交
1081

1082 1083 1084
	return 0;
}

1085 1086 1087 1088 1089 1090
static const struct platform_device_id bq27xxx_battery_platform_id_table[] = {
	{ "bq27000-battery", },
	{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(platform, bq27xxx_battery_platform_id_table);

1091 1092 1093 1094 1095 1096 1097 1098
#ifdef CONFIG_OF
static const struct of_device_id bq27xxx_battery_platform_of_match_table[] = {
	{ .compatible = "ti,bq27000" },
	{},
};
MODULE_DEVICE_TABLE(of, bq27xxx_battery_platform_of_match_table);
#endif

1099 1100 1101
static struct platform_driver bq27xxx_battery_platform_driver = {
	.probe	= bq27xxx_battery_platform_probe,
	.remove = bq27xxx_battery_platform_remove,
1102 1103
	.driver = {
		.name = "bq27000-battery",
1104
		.of_match_table = of_match_ptr(bq27xxx_battery_platform_of_match_table),
1105
	},
1106
	.id_table = bq27xxx_battery_platform_id_table,
1107
};
1108
module_platform_driver(bq27xxx_battery_platform_driver);
1109

1110 1111
MODULE_ALIAS("platform:bq27000-battery");

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