sbs-battery.c 31.7 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
2
/*
3
 * Gas Gauge driver for SBS Compliant Batteries
4 5 6 7
 *
 * Copyright (c) 2010, NVIDIA Corporation.
 */

8
#include <linux/bits.h>
9
#include <linux/delay.h>
10
#include <linux/err.h>
11
#include <linux/gpio/consumer.h>
12
#include <linux/i2c.h>
13
#include <linux/init.h>
14
#include <linux/interrupt.h>
15 16
#include <linux/kernel.h>
#include <linux/module.h>
17
#include <linux/property.h>
18
#include <linux/of_device.h>
19
#include <linux/power/sbs-battery.h>
20 21 22
#include <linux/power_supply.h>
#include <linux/slab.h>
#include <linux/stat.h>
23 24 25

enum {
	REG_MANUFACTURER_DATA,
26
	REG_BATTERY_MODE,
27 28
	REG_TEMPERATURE,
	REG_VOLTAGE,
29 30
	REG_CURRENT_NOW,
	REG_CURRENT_AVG,
31
	REG_MAX_ERR,
32 33 34 35
	REG_CAPACITY,
	REG_TIME_TO_EMPTY,
	REG_TIME_TO_FULL,
	REG_STATUS,
36
	REG_CAPACITY_LEVEL,
37
	REG_CYCLE_COUNT,
38 39
	REG_SERIAL_NUMBER,
	REG_REMAINING_CAPACITY,
40
	REG_REMAINING_CAPACITY_CHARGE,
41
	REG_FULL_CHARGE_CAPACITY,
42
	REG_FULL_CHARGE_CAPACITY_CHARGE,
43
	REG_DESIGN_CAPACITY,
44
	REG_DESIGN_CAPACITY_CHARGE,
45 46
	REG_DESIGN_VOLTAGE_MIN,
	REG_DESIGN_VOLTAGE_MAX,
47
	REG_CHEMISTRY,
48 49
	REG_MANUFACTURER,
	REG_MODEL_NAME,
50 51
	REG_CHARGE_CURRENT,
	REG_CHARGE_VOLTAGE,
52 53
};

54 55 56 57 58 59 60 61
#define REG_ADDR_SPEC_INFO		0x1A
#define SPEC_INFO_VERSION_MASK		GENMASK(7, 4)
#define SPEC_INFO_VERSION_SHIFT		4

#define SBS_VERSION_1_0			1
#define SBS_VERSION_1_1			2
#define SBS_VERSION_1_1_WITH_PEC	3

62 63
#define REG_ADDR_MANUFACTURE_DATE	0x1B

64 65
/* Battery Mode defines */
#define BATTERY_MODE_OFFSET		0x03
66 67 68 69
#define BATTERY_MODE_CAPACITY_MASK	BIT(15)
enum sbs_capacity_mode {
	CAPACITY_MODE_AMPS = 0,
	CAPACITY_MODE_WATTS = BATTERY_MODE_CAPACITY_MASK
70
};
71
#define BATTERY_MODE_CHARGER_MASK	(1<<14)
72

73 74 75 76 77
/* manufacturer access defines */
#define MANUFACTURER_ACCESS_STATUS	0x0006
#define MANUFACTURER_ACCESS_SLEEP	0x0011

/* battery status value bits */
78
#define BATTERY_INITIALIZED		0x80
79
#define BATTERY_DISCHARGING		0x40
80 81 82
#define BATTERY_FULL_CHARGED		0x20
#define BATTERY_FULL_DISCHARGED		0x10

83
/* min_value and max_value are only valid for numerical data */
84
#define SBS_DATA(_psp, _addr, _min_value, _max_value) { \
85 86 87 88 89 90
	.psp = _psp, \
	.addr = _addr, \
	.min_value = _min_value, \
	.max_value = _max_value, \
}

91
static const struct chip_data {
92 93 94 95
	enum power_supply_property psp;
	u8 addr;
	int min_value;
	int max_value;
96
} sbs_data[] = {
97
	[REG_MANUFACTURER_DATA] =
98
		SBS_DATA(POWER_SUPPLY_PROP_PRESENT, 0x00, 0, 65535),
99 100
	[REG_BATTERY_MODE] =
		SBS_DATA(-1, 0x03, 0, 65535),
101
	[REG_TEMPERATURE] =
102
		SBS_DATA(POWER_SUPPLY_PROP_TEMP, 0x08, 0, 65535),
103
	[REG_VOLTAGE] =
104
		SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_NOW, 0x09, 0, 20000),
105
	[REG_CURRENT_NOW] =
106
		SBS_DATA(POWER_SUPPLY_PROP_CURRENT_NOW, 0x0A, -32768, 32767),
107 108
	[REG_CURRENT_AVG] =
		SBS_DATA(POWER_SUPPLY_PROP_CURRENT_AVG, 0x0B, -32768, 32767),
109 110
	[REG_MAX_ERR] =
		SBS_DATA(POWER_SUPPLY_PROP_CAPACITY_ERROR_MARGIN, 0x0c, 0, 100),
111
	[REG_CAPACITY] =
112
		SBS_DATA(POWER_SUPPLY_PROP_CAPACITY, 0x0D, 0, 100),
113
	[REG_REMAINING_CAPACITY] =
114
		SBS_DATA(POWER_SUPPLY_PROP_ENERGY_NOW, 0x0F, 0, 65535),
115
	[REG_REMAINING_CAPACITY_CHARGE] =
116
		SBS_DATA(POWER_SUPPLY_PROP_CHARGE_NOW, 0x0F, 0, 65535),
117
	[REG_FULL_CHARGE_CAPACITY] =
118
		SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL, 0x10, 0, 65535),
119
	[REG_FULL_CHARGE_CAPACITY_CHARGE] =
120
		SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL, 0x10, 0, 65535),
121
	[REG_TIME_TO_EMPTY] =
122
		SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, 0x12, 0, 65535),
123
	[REG_TIME_TO_FULL] =
124
		SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_FULL_AVG, 0x13, 0, 65535),
125 126 127 128
	[REG_CHARGE_CURRENT] =
		SBS_DATA(POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX, 0x14, 0, 65535),
	[REG_CHARGE_VOLTAGE] =
		SBS_DATA(POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX, 0x15, 0, 65535),
129
	[REG_STATUS] =
130
		SBS_DATA(POWER_SUPPLY_PROP_STATUS, 0x16, 0, 65535),
131 132
	[REG_CAPACITY_LEVEL] =
		SBS_DATA(POWER_SUPPLY_PROP_CAPACITY_LEVEL, 0x16, 0, 65535),
133
	[REG_CYCLE_COUNT] =
134
		SBS_DATA(POWER_SUPPLY_PROP_CYCLE_COUNT, 0x17, 0, 65535),
135
	[REG_DESIGN_CAPACITY] =
136
		SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, 0x18, 0, 65535),
137
	[REG_DESIGN_CAPACITY_CHARGE] =
138
		SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 0x18, 0, 65535),
139 140 141
	[REG_DESIGN_VOLTAGE_MIN] =
		SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, 0x19, 0, 65535),
	[REG_DESIGN_VOLTAGE_MAX] =
142
		SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, 0x19, 0, 65535),
143
	[REG_SERIAL_NUMBER] =
144
		SBS_DATA(POWER_SUPPLY_PROP_SERIAL_NUMBER, 0x1C, 0, 65535),
145 146 147 148
	/* Properties of type `const char *' */
	[REG_MANUFACTURER] =
		SBS_DATA(POWER_SUPPLY_PROP_MANUFACTURER, 0x20, 0, 65535),
	[REG_MODEL_NAME] =
149 150 151
		SBS_DATA(POWER_SUPPLY_PROP_MODEL_NAME, 0x21, 0, 65535),
	[REG_CHEMISTRY] =
		SBS_DATA(POWER_SUPPLY_PROP_TECHNOLOGY, 0x22, 0, 65535)
152 153
};

154
static const enum power_supply_property sbs_properties[] = {
155
	POWER_SUPPLY_PROP_STATUS,
156
	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
157 158 159 160 161 162
	POWER_SUPPLY_PROP_HEALTH,
	POWER_SUPPLY_PROP_PRESENT,
	POWER_SUPPLY_PROP_TECHNOLOGY,
	POWER_SUPPLY_PROP_CYCLE_COUNT,
	POWER_SUPPLY_PROP_VOLTAGE_NOW,
	POWER_SUPPLY_PROP_CURRENT_NOW,
163
	POWER_SUPPLY_PROP_CURRENT_AVG,
164
	POWER_SUPPLY_PROP_CAPACITY,
165
	POWER_SUPPLY_PROP_CAPACITY_ERROR_MARGIN,
166 167 168 169
	POWER_SUPPLY_PROP_TEMP,
	POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
	POWER_SUPPLY_PROP_TIME_TO_FULL_AVG,
	POWER_SUPPLY_PROP_SERIAL_NUMBER,
170
	POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
171 172 173 174
	POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
	POWER_SUPPLY_PROP_ENERGY_NOW,
	POWER_SUPPLY_PROP_ENERGY_FULL,
	POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
175 176 177
	POWER_SUPPLY_PROP_CHARGE_NOW,
	POWER_SUPPLY_PROP_CHARGE_FULL,
	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
178 179
	POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
	POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
180 181 182
	POWER_SUPPLY_PROP_MANUFACTURE_YEAR,
	POWER_SUPPLY_PROP_MANUFACTURE_MONTH,
	POWER_SUPPLY_PROP_MANUFACTURE_DAY,
183 184 185
	/* Properties of type `const char *' */
	POWER_SUPPLY_PROP_MANUFACTURER,
	POWER_SUPPLY_PROP_MODEL_NAME
186 187
};

188 189
/* Supports special manufacturer commands from TI BQ20Z65 and BQ20Z75 IC. */
#define SBS_FLAGS_TI_BQ20ZX5		BIT(0)
190

191
struct sbs_info {
192
	struct i2c_client		*client;
193
	struct power_supply		*power_supply;
194
	bool				is_present;
195
	struct gpio_desc		*gpio_detect;
196
	bool				charger_broadcasts;
197 198
	int				last_state;
	int				poll_time;
199 200
	u32				i2c_retry_count;
	u32				poll_retry_count;
201
	struct delayed_work		work;
202
	struct mutex			mode_lock;
203
	u32				flags;
204 205
};

206 207
static char model_name[I2C_SMBUS_BLOCK_MAX + 1];
static char manufacturer[I2C_SMBUS_BLOCK_MAX + 1];
208
static char chemistry[I2C_SMBUS_BLOCK_MAX + 1];
209
static bool force_load;
210

211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
static int sbs_read_word_data(struct i2c_client *client, u8 address);
static int sbs_write_word_data(struct i2c_client *client, u8 address, u16 value);

static void sbs_disable_charger_broadcasts(struct sbs_info *chip)
{
	int val = sbs_read_word_data(chip->client, BATTERY_MODE_OFFSET);
	if (val < 0)
		goto exit;

	val |= BATTERY_MODE_CHARGER_MASK;

	val = sbs_write_word_data(chip->client, BATTERY_MODE_OFFSET, val);

exit:
	if (val < 0)
		dev_err(&chip->client->dev,
			"Failed to disable charger broadcasting: %d\n", val);
	else
		dev_dbg(&chip->client->dev, "%s\n", __func__);
}

232 233
static int sbs_update_presence(struct sbs_info *chip, bool is_present)
{
234 235 236 237 238
	struct i2c_client *client = chip->client;
	int retries = chip->i2c_retry_count;
	s32 ret = 0;
	u8 version;

239 240 241 242 243
	if (chip->is_present == is_present)
		return 0;

	if (!is_present) {
		chip->is_present = false;
244 245
		/* Disable PEC when no device is present */
		client->flags &= ~I2C_CLIENT_PEC;
246 247 248
		return 0;
	}

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
	/* Check if device supports packet error checking and use it */
	while (retries > 0) {
		ret = i2c_smbus_read_word_data(client, REG_ADDR_SPEC_INFO);
		if (ret >= 0)
			break;

		/*
		 * Some batteries trigger the detection pin before the
		 * I2C bus is properly connected. This works around the
		 * issue.
		 */
		msleep(100);

		retries--;
	}

	if (ret < 0) {
		dev_dbg(&client->dev, "failed to read spec info: %d\n", ret);

		/* fallback to old behaviour */
		client->flags &= ~I2C_CLIENT_PEC;
		chip->is_present = true;

		return ret;
	}

	version = (ret & SPEC_INFO_VERSION_MASK) >> SPEC_INFO_VERSION_SHIFT;

	if (version == SBS_VERSION_1_1_WITH_PEC)
		client->flags |= I2C_CLIENT_PEC;
	else
		client->flags &= ~I2C_CLIENT_PEC;

	dev_dbg(&client->dev, "PEC: %s\n", (client->flags & I2C_CLIENT_PEC) ?
		"enabled" : "disabled");

285 286 287
	if (!chip->is_present && is_present && !chip->charger_broadcasts)
		sbs_disable_charger_broadcasts(chip);

288 289 290 291 292
	chip->is_present = true;

	return 0;
}

293
static int sbs_read_word_data(struct i2c_client *client, u8 address)
294
{
295
	struct sbs_info *chip = i2c_get_clientdata(client);
296
	int retries = chip->i2c_retry_count;
297 298 299 300 301 302 303 304
	s32 ret = 0;

	while (retries > 0) {
		ret = i2c_smbus_read_word_data(client, address);
		if (ret >= 0)
			break;
		retries--;
	}
305 306

	if (ret < 0) {
307
		dev_dbg(&client->dev,
308 309 310 311
			"%s: i2c read at address 0x%x failed\n",
			__func__, address);
		return ret;
	}
312

313
	return ret;
314 315
}

316
static int sbs_read_string_data_fallback(struct i2c_client *client, u8 address, char *values)
317 318
{
	struct sbs_info *chip = i2c_get_clientdata(client);
319 320 321
	s32 ret = 0, block_length = 0;
	int retries_length, retries_block;
	u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1];
322

323 324 325
	retries_length = chip->i2c_retry_count;
	retries_block = chip->i2c_retry_count;

326 327
	dev_warn_once(&client->dev, "I2C adapter does not support I2C_FUNC_SMBUS_READ_BLOCK_DATA.\n"
				    "Fallback method does not support PEC.\n");
328

329
	/* Adapter needs to support these two functions */
330
	if (!i2c_check_functionality(client->adapter,
331 332
				     I2C_FUNC_SMBUS_BYTE_DATA |
				     I2C_FUNC_SMBUS_I2C_BLOCK)){
333 334 335
		return -ENODEV;
	}

336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
	/* Get the length of block data */
	while (retries_length > 0) {
		ret = i2c_smbus_read_byte_data(client, address);
		if (ret >= 0)
			break;
		retries_length--;
	}

	if (ret < 0) {
		dev_dbg(&client->dev,
			"%s: i2c read at address 0x%x failed\n",
			__func__, address);
		return ret;
	}

	/* block_length does not include NULL terminator */
	block_length = ret;
	if (block_length > I2C_SMBUS_BLOCK_MAX) {
		dev_err(&client->dev,
			"%s: Returned block_length is longer than 0x%x\n",
			__func__, I2C_SMBUS_BLOCK_MAX);
		return -EINVAL;
	}

360
	/* Get the block data */
361 362 363 364
	while (retries_block > 0) {
		ret = i2c_smbus_read_i2c_block_data(
				client, address,
				block_length + 1, block_buffer);
365 366
		if (ret >= 0)
			break;
367
		retries_block--;
368 369 370
	}

	if (ret < 0) {
371 372 373
		dev_dbg(&client->dev,
			"%s: i2c read at address 0x%x failed\n",
			__func__, address);
374 375 376
		return ret;
	}

377 378 379
	/* block_buffer[0] == block_length */
	memcpy(values, block_buffer + 1, block_length);
	values[block_length] = '\0';
380

381
	return ret;
382 383
}

384 385 386 387 388 389
static int sbs_read_string_data(struct i2c_client *client, u8 address, char *values)
{
	struct sbs_info *chip = i2c_get_clientdata(client);
	int retries = chip->i2c_retry_count;
	int ret = 0;

390 391 392 393 394 395 396 397
	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_BLOCK_DATA)) {
		bool pec = client->flags & I2C_CLIENT_PEC;
		client->flags &= ~I2C_CLIENT_PEC;
		ret = sbs_read_string_data_fallback(client, address, values);
		if (pec)
			client->flags |= I2C_CLIENT_PEC;
		return ret;
	}
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415

	while (retries > 0) {
		ret = i2c_smbus_read_block_data(client, address, values);
		if (ret >= 0)
			break;
		retries--;
	}

	if (ret < 0) {
		dev_dbg(&client->dev, "failed to read block 0x%x: %d\n", address, ret);
		return ret;
	}

	/* add string termination */
	values[ret] = '\0';
	return ret;
}

416
static int sbs_write_word_data(struct i2c_client *client, u8 address,
417 418
	u16 value)
{
419
	struct sbs_info *chip = i2c_get_clientdata(client);
420
	int retries = chip->i2c_retry_count;
421 422 423
	s32 ret = 0;

	while (retries > 0) {
424
		ret = i2c_smbus_write_word_data(client, address, value);
425 426 427 428
		if (ret >= 0)
			break;
		retries--;
	}
429 430

	if (ret < 0) {
431
		dev_dbg(&client->dev,
432 433 434 435
			"%s: i2c write to address 0x%x failed\n",
			__func__, address);
		return ret;
	}
436

437 438 439
	return 0;
}

440 441 442 443
static int sbs_status_correct(struct i2c_client *client, int *intval)
{
	int ret;

444
	ret = sbs_read_word_data(client, sbs_data[REG_CURRENT_NOW].addr);
445 446 447 448 449
	if (ret < 0)
		return ret;

	ret = (s16)ret;

450 451 452
	/* Not drawing current -> not charging (i.e. idle) */
	if (*intval != POWER_SUPPLY_STATUS_FULL && ret == 0)
		*intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
453 454 455 456 457 458 459 460 461 462 463 464

	if (*intval == POWER_SUPPLY_STATUS_FULL) {
		/* Drawing or providing current when full */
		if (ret > 0)
			*intval = POWER_SUPPLY_STATUS_CHARGING;
		else if (ret < 0)
			*intval = POWER_SUPPLY_STATUS_DISCHARGING;
	}

	return 0;
}

465 466 467 468 469 470 471 472 473 474 475
static bool sbs_bat_needs_calibration(struct i2c_client *client)
{
	int ret;

	ret = sbs_read_word_data(client, sbs_data[REG_BATTERY_MODE].addr);
	if (ret < 0)
		return false;

	return !!(ret & BIT(7));
}

476 477 478
static int sbs_get_ti_battery_presence_and_health(
	struct i2c_client *client, enum power_supply_property psp,
	union power_supply_propval *val)
479 480 481
{
	s32 ret;

482 483
	/*
	 * Write to ManufacturerAccess with ManufacturerAccess command
484
	 * and then read the status.
485
	 */
486 487 488 489 490 491 492
	ret = sbs_write_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr,
				  MANUFACTURER_ACCESS_STATUS);
	if (ret < 0) {
		if (psp == POWER_SUPPLY_PROP_PRESENT)
			val->intval = 0; /* battery removed */
		return ret;
	}
493 494

	ret = sbs_read_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr);
495 496 497
	if (ret < 0) {
		if (psp == POWER_SUPPLY_PROP_PRESENT)
			val->intval = 0; /* battery removed */
498
		return ret;
499
	}
500

501 502
	if (ret < sbs_data[REG_MANUFACTURER_DATA].min_value ||
	    ret > sbs_data[REG_MANUFACTURER_DATA].max_value) {
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
		val->intval = 0;
		return 0;
	}

	/* Mask the upper nibble of 2nd byte and
	 * lower byte of response then
	 * shift the result by 8 to get status*/
	ret &= 0x0F00;
	ret >>= 8;
	if (psp == POWER_SUPPLY_PROP_PRESENT) {
		if (ret == 0x0F)
			/* battery removed */
			val->intval = 0;
		else
			val->intval = 1;
	} else if (psp == POWER_SUPPLY_PROP_HEALTH) {
		if (ret == 0x09)
			val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
		else if (ret == 0x0B)
			val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
		else if (ret == 0x0C)
			val->intval = POWER_SUPPLY_HEALTH_DEAD;
525 526
		else if (sbs_bat_needs_calibration(client))
			val->intval = POWER_SUPPLY_HEALTH_CALIBRATION_REQUIRED;
527 528 529 530 531 532 533
		else
			val->intval = POWER_SUPPLY_HEALTH_GOOD;
	}

	return 0;
}

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
static int sbs_get_battery_presence_and_health(
	struct i2c_client *client, enum power_supply_property psp,
	union power_supply_propval *val)
{
	struct sbs_info *chip = i2c_get_clientdata(client);
	int ret;

	if (chip->flags & SBS_FLAGS_TI_BQ20ZX5)
		return sbs_get_ti_battery_presence_and_health(client, psp, val);

	/* Dummy command; if it succeeds, battery is present. */
	ret = sbs_read_word_data(client, sbs_data[REG_STATUS].addr);

	if (ret < 0) { /* battery not present*/
		if (psp == POWER_SUPPLY_PROP_PRESENT) {
			val->intval = 0;
			return 0;
		}
		return ret;
	}

	if (psp == POWER_SUPPLY_PROP_PRESENT)
		val->intval = 1; /* battery present */
	else { /* POWER_SUPPLY_PROP_HEALTH */
		if (sbs_bat_needs_calibration(client)) {
			val->intval = POWER_SUPPLY_HEALTH_CALIBRATION_REQUIRED;
		} else {
			/* SBS spec doesn't have a general health command. */
			val->intval = POWER_SUPPLY_HEALTH_UNKNOWN;
		}
	}

	return 0;
}

569
static int sbs_get_battery_property(struct i2c_client *client,
570 571 572
	int reg_offset, enum power_supply_property psp,
	union power_supply_propval *val)
{
573
	struct sbs_info *chip = i2c_get_clientdata(client);
574 575
	s32 ret;

576
	ret = sbs_read_word_data(client, sbs_data[reg_offset].addr);
577 578 579 580
	if (ret < 0)
		return ret;

	/* returned values are 16 bit */
581
	if (sbs_data[reg_offset].min_value < 0)
582
		ret = (s16)ret;
583

584 585
	if (ret >= sbs_data[reg_offset].min_value &&
	    ret <= sbs_data[reg_offset].max_value) {
586
		val->intval = ret;
587 588 589 590 591 592 593 594 595 596 597 598 599
		if (psp == POWER_SUPPLY_PROP_CAPACITY_LEVEL) {
			if (!(ret & BATTERY_INITIALIZED))
				val->intval =
					POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
			else if (ret & BATTERY_FULL_CHARGED)
				val->intval =
					POWER_SUPPLY_CAPACITY_LEVEL_FULL;
			else if (ret & BATTERY_FULL_DISCHARGED)
				val->intval =
					POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
			else
				val->intval =
					POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
600
			return 0;
601 602 603
		} else if (psp != POWER_SUPPLY_PROP_STATUS) {
			return 0;
		}
604 605 606 607 608 609 610 611

		if (ret & BATTERY_FULL_CHARGED)
			val->intval = POWER_SUPPLY_STATUS_FULL;
		else if (ret & BATTERY_DISCHARGING)
			val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
		else
			val->intval = POWER_SUPPLY_STATUS_CHARGING;

612 613
		sbs_status_correct(client, &val->intval);

614 615 616 617
		if (chip->poll_time == 0)
			chip->last_state = val->intval;
		else if (chip->last_state != val->intval) {
			cancel_delayed_work_sync(&chip->work);
618
			power_supply_changed(chip->power_supply);
619
			chip->poll_time = 0;
620 621 622 623
		}
	} else {
		if (psp == POWER_SUPPLY_PROP_STATUS)
			val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
624 625 626 627 628
		else if (psp == POWER_SUPPLY_PROP_CAPACITY)
			/* sbs spec says that this can be >100 %
			 * even if max value is 100 %
			 */
			val->intval = min(ret, 100);
629 630 631 632 633 634 635
		else
			val->intval = 0;
	}

	return 0;
}

636 637 638
static int sbs_get_battery_string_property(struct i2c_client *client,
	int reg_offset, enum power_supply_property psp, char *val)
{
639 640 641 642 643 644 645 646
	s32 ret;

	ret = sbs_read_string_data(client, sbs_data[reg_offset].addr, val);

	if (ret < 0)
		return ret;

	return 0;
647 648
}

649
static void  sbs_unit_adjustment(struct i2c_client *client,
650 651 652 653
	enum power_supply_property psp, union power_supply_propval *val)
{
#define BASE_UNIT_CONVERSION		1000
#define BATTERY_MODE_CAP_MULT_WATT	(10 * BASE_UNIT_CONVERSION)
654 655
#define TIME_UNIT_CONVERSION		60
#define TEMP_KELVIN_TO_CELSIUS		2731
656 657 658 659
	switch (psp) {
	case POWER_SUPPLY_PROP_ENERGY_NOW:
	case POWER_SUPPLY_PROP_ENERGY_FULL:
	case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
660
		/* sbs provides energy in units of 10mWh.
661 662
		 * Convert to µWh
		 */
663 664 665 666
		val->intval *= BATTERY_MODE_CAP_MULT_WATT;
		break;

	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
667
	case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
668 669
	case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
	case POWER_SUPPLY_PROP_CURRENT_NOW:
670
	case POWER_SUPPLY_PROP_CURRENT_AVG:
671
	case POWER_SUPPLY_PROP_CHARGE_NOW:
672 673
	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
674 675
	case POWER_SUPPLY_PROP_CHARGE_FULL:
	case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
676 677 678 679
		val->intval *= BASE_UNIT_CONVERSION;
		break;

	case POWER_SUPPLY_PROP_TEMP:
680
		/* sbs provides battery temperature in 0.1K
681 682 683
		 * so convert it to 0.1°C
		 */
		val->intval -= TEMP_KELVIN_TO_CELSIUS;
684 685 686 687
		break;

	case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
	case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
688
		/* sbs provides time to empty and time to full in minutes.
689 690
		 * Convert to seconds
		 */
691 692 693 694 695 696 697 698 699
		val->intval *= TIME_UNIT_CONVERSION;
		break;

	default:
		dev_dbg(&client->dev,
			"%s: no need for unit conversion %d\n", __func__, psp);
	}
}

700 701
static enum sbs_capacity_mode sbs_set_capacity_mode(struct i2c_client *client,
	enum sbs_capacity_mode mode)
702 703 704
{
	int ret, original_val;

705
	original_val = sbs_read_word_data(client, BATTERY_MODE_OFFSET);
706 707 708
	if (original_val < 0)
		return original_val;

709
	if ((original_val & BATTERY_MODE_CAPACITY_MASK) == mode)
710 711
		return mode;

712 713
	if (mode == CAPACITY_MODE_AMPS)
		ret = original_val & ~BATTERY_MODE_CAPACITY_MASK;
714
	else
715
		ret = original_val | BATTERY_MODE_CAPACITY_MASK;
716

717
	ret = sbs_write_word_data(client, BATTERY_MODE_OFFSET, ret);
718 719 720
	if (ret < 0)
		return ret;

721 722
	usleep_range(1000, 2000);

723
	return original_val & BATTERY_MODE_CAPACITY_MASK;
724 725
}

726
static int sbs_get_battery_capacity(struct i2c_client *client,
727
	int reg_offset, enum power_supply_property psp,
728 729 730
	union power_supply_propval *val)
{
	s32 ret;
731
	enum sbs_capacity_mode mode = CAPACITY_MODE_WATTS;
732 733

	if (power_supply_is_amp_property(psp))
734
		mode = CAPACITY_MODE_AMPS;
735

736
	mode = sbs_set_capacity_mode(client, mode);
737
	if ((int)mode < 0)
738
		return mode;
739

740
	ret = sbs_read_word_data(client, sbs_data[reg_offset].addr);
741 742
	if (ret < 0)
		return ret;
743

744
	val->intval = ret;
745

746
	ret = sbs_set_capacity_mode(client, mode);
747 748 749
	if (ret < 0)
		return ret;

750 751 752
	return 0;
}

753 754
static char sbs_serial[5];
static int sbs_get_battery_serial_number(struct i2c_client *client,
755 756 757 758
	union power_supply_propval *val)
{
	int ret;

759
	ret = sbs_read_word_data(client, sbs_data[REG_SERIAL_NUMBER].addr);
760 761 762
	if (ret < 0)
		return ret;

763
	sprintf(sbs_serial, "%04x", ret);
764
	val->strval = sbs_serial;
765 766 767 768

	return 0;
}

769
static int sbs_get_property_index(struct i2c_client *client,
770 771 772
	enum power_supply_property psp)
{
	int count;
773 774
	for (count = 0; count < ARRAY_SIZE(sbs_data); count++)
		if (psp == sbs_data[count].psp)
775 776 777 778 779 780 781 782
			return count;

	dev_warn(&client->dev,
		"%s: Invalid Property - %d\n", __func__, psp);

	return -EINVAL;
}

783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814
static int sbs_get_chemistry(struct i2c_client *client,
		union power_supply_propval *val)
{
	enum power_supply_property psp = POWER_SUPPLY_PROP_TECHNOLOGY;
	int ret;

	ret = sbs_get_property_index(client, psp);
	if (ret < 0)
		return ret;

	ret = sbs_get_battery_string_property(client, ret, psp,
					      chemistry);
	if (ret < 0)
		return ret;

	if (!strncasecmp(chemistry, "LION", 4))
		val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
	else if (!strncasecmp(chemistry, "LiP", 3))
		val->intval = POWER_SUPPLY_TECHNOLOGY_LIPO;
	else if (!strncasecmp(chemistry, "NiCd", 4))
		val->intval = POWER_SUPPLY_TECHNOLOGY_NiCd;
	else if (!strncasecmp(chemistry, "NiMH", 4))
		val->intval = POWER_SUPPLY_TECHNOLOGY_NiMH;
	else
		val->intval = POWER_SUPPLY_TECHNOLOGY_UNKNOWN;

	if (val->intval == POWER_SUPPLY_TECHNOLOGY_UNKNOWN)
		dev_warn(&client->dev, "Unknown chemistry: %s\n", chemistry);

	return 0;
}

815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846
static int sbs_get_battery_manufacture_date(struct i2c_client *client,
	enum power_supply_property psp,
	union power_supply_propval *val)
{
	int ret;
	u16 day, month, year;

	ret = sbs_read_word_data(client, REG_ADDR_MANUFACTURE_DATE);
	if (ret < 0)
		return ret;

	day   = ret   & GENMASK(4,  0);
	month = (ret  & GENMASK(8,  5)) >> 5;
	year  = ((ret & GENMASK(15, 9)) >> 9) + 1980;

	switch (psp) {
	case POWER_SUPPLY_PROP_MANUFACTURE_YEAR:
		val->intval = year;
		break;
	case POWER_SUPPLY_PROP_MANUFACTURE_MONTH:
		val->intval = month;
		break;
	case POWER_SUPPLY_PROP_MANUFACTURE_DAY:
		val->intval = day;
		break;
	default:
		return -EINVAL;
	}

	return 0;
}

847
static int sbs_get_property(struct power_supply *psy,
848 849 850
	enum power_supply_property psp,
	union power_supply_propval *val)
{
851
	int ret = 0;
852
	struct sbs_info *chip = power_supply_get_drvdata(psy);
853
	struct i2c_client *client = chip->client;
854

855 856 857 858 859 860
	if (chip->gpio_detect) {
		ret = gpiod_get_value_cansleep(chip->gpio_detect);
		if (ret < 0)
			return ret;
		if (psp == POWER_SUPPLY_PROP_PRESENT) {
			val->intval = ret;
861
			sbs_update_presence(chip, ret);
862 863 864 865 866 867
			return 0;
		}
		if (ret == 0)
			return -ENODATA;
	}

868 869 870
	switch (psp) {
	case POWER_SUPPLY_PROP_PRESENT:
	case POWER_SUPPLY_PROP_HEALTH:
871
		ret = sbs_get_battery_presence_and_health(client, psp, val);
872 873

		/* this can only be true if no gpio is used */
874 875
		if (psp == POWER_SUPPLY_PROP_PRESENT)
			return 0;
876 877 878
		break;

	case POWER_SUPPLY_PROP_TECHNOLOGY:
879 880 881 882
		ret = sbs_get_chemistry(client, val);
		if (ret < 0)
			break;

883
		goto done; /* don't trigger power_supply_changed()! */
884

885 886 887
	case POWER_SUPPLY_PROP_ENERGY_NOW:
	case POWER_SUPPLY_PROP_ENERGY_FULL:
	case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
888 889 890
	case POWER_SUPPLY_PROP_CHARGE_NOW:
	case POWER_SUPPLY_PROP_CHARGE_FULL:
	case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
891
		ret = sbs_get_property_index(client, psp);
892 893
		if (ret < 0)
			break;
894

895 896 897 898 899
		/* sbs_get_battery_capacity() will change the battery mode
		 * temporarily to read the requested attribute. Ensure we stay
		 * in the desired mode for the duration of the attribute read.
		 */
		mutex_lock(&chip->mode_lock);
900
		ret = sbs_get_battery_capacity(client, ret, psp, val);
901
		mutex_unlock(&chip->mode_lock);
902 903 904
		break;

	case POWER_SUPPLY_PROP_SERIAL_NUMBER:
905
		ret = sbs_get_battery_serial_number(client, val);
906 907 908
		break;

	case POWER_SUPPLY_PROP_STATUS:
909
	case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
910 911 912
	case POWER_SUPPLY_PROP_CYCLE_COUNT:
	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
	case POWER_SUPPLY_PROP_CURRENT_NOW:
913
	case POWER_SUPPLY_PROP_CURRENT_AVG:
914 915 916
	case POWER_SUPPLY_PROP_TEMP:
	case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
	case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
917
	case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
918
	case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
919 920
	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
921
	case POWER_SUPPLY_PROP_CAPACITY:
922
	case POWER_SUPPLY_PROP_CAPACITY_ERROR_MARGIN:
923
		ret = sbs_get_property_index(client, psp);
924 925
		if (ret < 0)
			break;
926

927
		ret = sbs_get_battery_property(client, ret, psp, val);
928 929
		break;

930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949
	case POWER_SUPPLY_PROP_MODEL_NAME:
		ret = sbs_get_property_index(client, psp);
		if (ret < 0)
			break;

		ret = sbs_get_battery_string_property(client, ret, psp,
						      model_name);
		val->strval = model_name;
		break;

	case POWER_SUPPLY_PROP_MANUFACTURER:
		ret = sbs_get_property_index(client, psp);
		if (ret < 0)
			break;

		ret = sbs_get_battery_string_property(client, ret, psp,
						      manufacturer);
		val->strval = manufacturer;
		break;

950 951 952 953 954 955
	case POWER_SUPPLY_PROP_MANUFACTURE_YEAR:
	case POWER_SUPPLY_PROP_MANUFACTURE_MONTH:
	case POWER_SUPPLY_PROP_MANUFACTURE_DAY:
		ret = sbs_get_battery_manufacture_date(client, psp, val);
		break;

956 957 958 959 960 961
	default:
		dev_err(&client->dev,
			"%s: INVALID property\n", __func__);
		return -EINVAL;
	}

962 963 964
	if (!chip->gpio_detect && chip->is_present != (ret >= 0)) {
		bool old_present = chip->is_present;
		union power_supply_propval val;
965
		int err = sbs_get_battery_presence_and_health(
966 967
				client, POWER_SUPPLY_PROP_PRESENT, &val);

968
		sbs_update_presence(chip, !err && val.intval);
969 970 971

		if (old_present != chip->is_present)
			power_supply_changed(chip->power_supply);
972 973 974 975 976
	}

done:
	if (!ret) {
		/* Convert units to match requirements for power supply class */
977
		sbs_unit_adjustment(client, psp, val);
978 979 980 981 982 983
		dev_dbg(&client->dev,
			"%s: property = %d, value = %x\n", __func__,
			psp, val->intval);
	} else if (!chip->is_present)  {
		/* battery not present, so return NODATA for properties */
		ret = -ENODATA;
984
	}
985
	return ret;
986 987
}

988
static void sbs_supply_changed(struct sbs_info *chip)
989
{
990
	struct power_supply *battery = chip->power_supply;
991
	int ret;
992

993 994
	ret = gpiod_get_value_cansleep(chip->gpio_detect);
	if (ret < 0)
995
		return;
996
	sbs_update_presence(chip, ret);
997
	power_supply_changed(battery);
998
}
999

1000 1001 1002
static irqreturn_t sbs_irq(int irq, void *devid)
{
	sbs_supply_changed(devid);
1003 1004 1005
	return IRQ_HANDLED;
}

1006 1007 1008 1009 1010 1011
static void sbs_alert(struct i2c_client *client, enum i2c_alert_protocol prot,
	unsigned int data)
{
	sbs_supply_changed(i2c_get_clientdata(client));
}

1012
static void sbs_external_power_changed(struct power_supply *psy)
1013
{
1014
	struct sbs_info *chip = power_supply_get_drvdata(psy);
1015 1016

	/* cancel outstanding work */
1017
	cancel_delayed_work_sync(&chip->work);
1018

1019
	schedule_delayed_work(&chip->work, HZ);
1020
	chip->poll_time = chip->poll_retry_count;
1021 1022
}

1023
static void sbs_delayed_work(struct work_struct *work)
1024
{
1025
	struct sbs_info *chip;
1026 1027
	s32 ret;

1028
	chip = container_of(work, struct sbs_info, work.work);
1029

1030
	ret = sbs_read_word_data(chip->client, sbs_data[REG_STATUS].addr);
1031 1032
	/* if the read failed, give up on this work */
	if (ret < 0) {
1033
		chip->poll_time = 0;
1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
		return;
	}

	if (ret & BATTERY_FULL_CHARGED)
		ret = POWER_SUPPLY_STATUS_FULL;
	else if (ret & BATTERY_DISCHARGING)
		ret = POWER_SUPPLY_STATUS_DISCHARGING;
	else
		ret = POWER_SUPPLY_STATUS_CHARGING;

1044 1045
	sbs_status_correct(chip->client, &ret);

1046 1047
	if (chip->last_state != ret) {
		chip->poll_time = 0;
1048
		power_supply_changed(chip->power_supply);
1049 1050
		return;
	}
1051 1052 1053
	if (chip->poll_time > 0) {
		schedule_delayed_work(&chip->work, HZ);
		chip->poll_time--;
1054 1055 1056 1057
		return;
	}
}

1058 1059 1060 1061 1062 1063 1064 1065
static const struct power_supply_desc sbs_default_desc = {
	.type = POWER_SUPPLY_TYPE_BATTERY,
	.properties = sbs_properties,
	.num_properties = ARRAY_SIZE(sbs_properties),
	.get_property = sbs_get_property,
	.external_power_changed = sbs_external_power_changed,
};

1066
static int sbs_probe(struct i2c_client *client)
1067
{
1068
	struct sbs_info *chip;
1069
	struct power_supply_desc *sbs_desc;
1070
	struct sbs_platform_data *pdata = client->dev.platform_data;
1071
	struct power_supply_config psy_cfg = {};
1072
	int rc;
1073
	int irq;
1074

1075 1076 1077 1078 1079 1080 1081 1082
	sbs_desc = devm_kmemdup(&client->dev, &sbs_default_desc,
			sizeof(*sbs_desc), GFP_KERNEL);
	if (!sbs_desc)
		return -ENOMEM;

	sbs_desc->name = devm_kasprintf(&client->dev, GFP_KERNEL, "sbs-%s",
			dev_name(&client->dev));
	if (!sbs_desc->name)
1083
		return -ENOMEM;
1084

1085
	chip = devm_kzalloc(&client->dev, sizeof(struct sbs_info), GFP_KERNEL);
1086 1087
	if (!chip)
		return -ENOMEM;
1088

1089
	chip->flags = (u32)(uintptr_t)device_get_match_data(&client->dev);
1090
	chip->client = client;
1091
	psy_cfg.of_node = client->dev.of_node;
1092
	psy_cfg.drv_data = chip;
1093
	chip->last_state = POWER_SUPPLY_STATUS_UNKNOWN;
1094
	mutex_init(&chip->mode_lock);
1095

1096 1097 1098
	/* use pdata if available, fall back to DT properties,
	 * or hardcoded defaults if not
	 */
1099 1100
	rc = device_property_read_u32(&client->dev, "sbs,i2c-retry-count",
				      &chip->i2c_retry_count);
1101
	if (rc)
1102
		chip->i2c_retry_count = 0;
1103

1104 1105
	rc = device_property_read_u32(&client->dev, "sbs,poll-retry-count",
				      &chip->poll_retry_count);
1106 1107 1108 1109 1110 1111 1112
	if (rc)
		chip->poll_retry_count = 0;

	if (pdata) {
		chip->poll_retry_count = pdata->poll_retry_count;
		chip->i2c_retry_count  = pdata->i2c_retry_count;
	}
1113
	chip->i2c_retry_count = chip->i2c_retry_count + 1;
1114

1115
	chip->charger_broadcasts = !device_property_read_bool(&client->dev,
1116 1117
					"sbs,disable-charger-broadcasts");

1118 1119 1120 1121 1122 1123
	chip->gpio_detect = devm_gpiod_get_optional(&client->dev,
			"sbs,battery-detect", GPIOD_IN);
	if (IS_ERR(chip->gpio_detect)) {
		dev_err(&client->dev, "Failed to get gpio: %ld\n",
			PTR_ERR(chip->gpio_detect));
		return PTR_ERR(chip->gpio_detect);
1124 1125
	}

1126
	i2c_set_clientdata(client, chip);
1127

1128
	if (!chip->gpio_detect)
1129 1130
		goto skip_gpio;

1131
	irq = gpiod_to_irq(chip->gpio_detect);
1132 1133 1134 1135 1136
	if (irq <= 0) {
		dev_warn(&client->dev, "Failed to get gpio as irq: %d\n", irq);
		goto skip_gpio;
	}

1137
	rc = devm_request_threaded_irq(&client->dev, irq, NULL, sbs_irq,
1138
		IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
1139
		dev_name(&client->dev), chip);
1140 1141 1142 1143 1144 1145
	if (rc) {
		dev_warn(&client->dev, "Failed to request irq: %d\n", rc);
		goto skip_gpio;
	}

skip_gpio:
1146
	/*
1147
	 * Before we register, we might need to make sure we can actually talk
1148 1149
	 * to the battery.
	 */
1150
	if (!(force_load || chip->gpio_detect)) {
1151
		union power_supply_propval val;
1152

1153 1154 1155 1156 1157
		rc = sbs_get_battery_presence_and_health(
				client, POWER_SUPPLY_PROP_PRESENT, &val);
		if (rc < 0 || !val.intval) {
			dev_err(&client->dev, "Failed to get present status\n");
			rc = -ENODEV;
1158 1159
			goto exit_psupply;
		}
1160
	}
1161

1162 1163
	INIT_DELAYED_WORK(&chip->work, sbs_delayed_work);

1164
	chip->power_supply = devm_power_supply_register(&client->dev, sbs_desc,
1165 1166
						   &psy_cfg);
	if (IS_ERR(chip->power_supply)) {
1167 1168
		dev_err(&client->dev,
			"%s: Failed to register power supply\n", __func__);
1169
		rc = PTR_ERR(chip->power_supply);
1170
		goto exit_psupply;
1171 1172 1173 1174 1175 1176
	}

	dev_info(&client->dev,
		"%s: battery gas gauge device registered\n", client->name);

	return 0;
1177 1178 1179

exit_psupply:
	return rc;
1180 1181
}

1182
static int sbs_remove(struct i2c_client *client)
1183
{
1184
	struct sbs_info *chip = i2c_get_clientdata(client);
1185

1186
	cancel_delayed_work_sync(&chip->work);
1187

1188 1189 1190
	return 0;
}

1191 1192 1193
#if defined CONFIG_PM_SLEEP

static int sbs_suspend(struct device *dev)
1194
{
1195
	struct i2c_client *client = to_i2c_client(dev);
1196
	struct sbs_info *chip = i2c_get_clientdata(client);
1197
	int ret;
1198

1199 1200
	if (chip->poll_time > 0)
		cancel_delayed_work_sync(&chip->work);
1201

1202
	if (chip->flags & SBS_FLAGS_TI_BQ20ZX5) {
1203 1204 1205 1206 1207 1208 1209
		/* Write to manufacturer access with sleep command. */
		ret = sbs_write_word_data(client,
					  sbs_data[REG_MANUFACTURER_DATA].addr,
					  MANUFACTURER_ACCESS_SLEEP);
		if (chip->is_present && ret < 0)
			return ret;
	}
1210 1211 1212

	return 0;
}
1213 1214 1215 1216

static SIMPLE_DEV_PM_OPS(sbs_pm_ops, sbs_suspend, NULL);
#define SBS_PM_OPS (&sbs_pm_ops)

1217
#else
1218
#define SBS_PM_OPS NULL
1219 1220
#endif

1221
static const struct i2c_device_id sbs_id[] = {
1222
	{ "bq20z65", 0 },
1223
	{ "bq20z75", 0 },
1224
	{ "sbs-battery", 1 },
1225 1226
	{}
};
1227 1228
MODULE_DEVICE_TABLE(i2c, sbs_id);

1229 1230
static const struct of_device_id sbs_dt_ids[] = {
	{ .compatible = "sbs,sbs-battery" },
1231 1232 1233 1234
	{
		.compatible = "ti,bq20z65",
		.data = (void *)SBS_FLAGS_TI_BQ20ZX5,
	},
1235 1236
	{
		.compatible = "ti,bq20z75",
1237
		.data = (void *)SBS_FLAGS_TI_BQ20ZX5,
1238
	},
1239 1240 1241 1242
	{ }
};
MODULE_DEVICE_TABLE(of, sbs_dt_ids);

1243
static struct i2c_driver sbs_battery_driver = {
1244
	.probe_new	= sbs_probe,
1245
	.remove		= sbs_remove,
1246
	.alert		= sbs_alert,
1247
	.id_table	= sbs_id,
1248
	.driver = {
1249
		.name	= "sbs-battery",
1250
		.of_match_table = sbs_dt_ids,
1251
		.pm	= SBS_PM_OPS,
1252 1253
	},
};
1254
module_i2c_driver(sbs_battery_driver);
1255

1256
MODULE_DESCRIPTION("SBS battery monitor driver");
1257
MODULE_LICENSE("GPL");
1258

1259
module_param(force_load, bool, 0444);
1260 1261
MODULE_PARM_DESC(force_load,
		 "Attempt to load the driver even if no battery is connected");