sbs-battery.c 28.3 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/of.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 26 27

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

53 54 55 56 57 58 59 60
#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

61 62
#define REG_ADDR_MANUFACTURE_DATE	0x1B

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

71 72 73 74 75
/* manufacturer access defines */
#define MANUFACTURER_ACCESS_STATUS	0x0006
#define MANUFACTURER_ACCESS_SLEEP	0x0011

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

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

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

150
static enum power_supply_property sbs_properties[] = {
151
	POWER_SUPPLY_PROP_STATUS,
152
	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
153 154 155 156 157 158
	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,
159
	POWER_SUPPLY_PROP_CURRENT_AVG,
160
	POWER_SUPPLY_PROP_CAPACITY,
161
	POWER_SUPPLY_PROP_CAPACITY_ERROR_MARGIN,
162 163 164 165
	POWER_SUPPLY_PROP_TEMP,
	POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
	POWER_SUPPLY_PROP_TIME_TO_FULL_AVG,
	POWER_SUPPLY_PROP_SERIAL_NUMBER,
166
	POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
167 168 169 170
	POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
	POWER_SUPPLY_PROP_ENERGY_NOW,
	POWER_SUPPLY_PROP_ENERGY_FULL,
	POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
171 172 173
	POWER_SUPPLY_PROP_CHARGE_NOW,
	POWER_SUPPLY_PROP_CHARGE_FULL,
	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
174 175
	POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
	POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
176 177 178
	POWER_SUPPLY_PROP_MANUFACTURE_YEAR,
	POWER_SUPPLY_PROP_MANUFACTURE_MONTH,
	POWER_SUPPLY_PROP_MANUFACTURE_DAY,
179 180 181
	/* Properties of type `const char *' */
	POWER_SUPPLY_PROP_MANUFACTURER,
	POWER_SUPPLY_PROP_MODEL_NAME
182 183
};

184 185
/* Supports special manufacturer commands from TI BQ20Z65 and BQ20Z75 IC. */
#define SBS_FLAGS_TI_BQ20ZX5		BIT(0)
186

187
struct sbs_info {
188
	struct i2c_client		*client;
189
	struct power_supply		*power_supply;
190
	bool				is_present;
191
	struct gpio_desc		*gpio_detect;
192
	bool				enable_detection;
193 194
	int				last_state;
	int				poll_time;
195 196
	u32				i2c_retry_count;
	u32				poll_retry_count;
197
	struct delayed_work		work;
198
	struct mutex			mode_lock;
199
	u32				flags;
200 201
};

202 203
static char model_name[I2C_SMBUS_BLOCK_MAX + 1];
static char manufacturer[I2C_SMBUS_BLOCK_MAX + 1];
204
static char chemistry[I2C_SMBUS_BLOCK_MAX + 1];
205
static bool force_load;
206

207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
static int sbs_update_presence(struct sbs_info *chip, bool is_present)
{
	struct i2c_client *client = chip->client;
	int retries = chip->i2c_retry_count;
	s32 ret = 0;
	u8 version;

	if (chip->is_present == is_present)
		return 0;

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

	/* 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");

	chip->is_present = true;

	return 0;
}

265
static int sbs_read_word_data(struct i2c_client *client, u8 address)
266
{
267
	struct sbs_info *chip = i2c_get_clientdata(client);
268
	int retries = chip->i2c_retry_count;
269 270 271 272 273 274 275 276
	s32 ret = 0;

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

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

285
	return ret;
286 287
}

288 289 290 291
static int sbs_read_string_data(struct i2c_client *client, u8 address,
				char *values)
{
	struct sbs_info *chip = i2c_get_clientdata(client);
292 293
	int retries = chip->i2c_retry_count;
	s32 ret = 0;
294 295

	if (!i2c_check_functionality(client->adapter,
296
				     I2C_FUNC_SMBUS_READ_BLOCK_DATA)) {
297 298 299 300
		return -ENODEV;
	}

	/* Get the block data */
301 302
	while (retries > 0) {
		ret = i2c_smbus_read_block_data(client, address, values);
303 304
		if (ret >= 0)
			break;
305
		retries--;
306 307 308
	}

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

314 315
	/* add string termination */
	values[ret] = '\0';
316

317
	return 0;
318 319
}

320
static int sbs_write_word_data(struct i2c_client *client, u8 address,
321 322
	u16 value)
{
323
	struct sbs_info *chip = i2c_get_clientdata(client);
324
	int retries = chip->i2c_retry_count;
325 326 327
	s32 ret = 0;

	while (retries > 0) {
328
		ret = i2c_smbus_write_word_data(client, address, value);
329 330 331 332
		if (ret >= 0)
			break;
		retries--;
	}
333 334

	if (ret < 0) {
335
		dev_dbg(&client->dev,
336 337 338 339
			"%s: i2c write to address 0x%x failed\n",
			__func__, address);
		return ret;
	}
340

341 342 343
	return 0;
}

344 345 346 347
static int sbs_status_correct(struct i2c_client *client, int *intval)
{
	int ret;

348
	ret = sbs_read_word_data(client, sbs_data[REG_CURRENT_NOW].addr);
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
	if (ret < 0)
		return ret;

	ret = (s16)ret;

	/* Not drawing current means full (cannot be not charging) */
	if (ret == 0)
		*intval = POWER_SUPPLY_STATUS_FULL;

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

369
static int sbs_get_battery_presence_and_health(
370 371
	struct i2c_client *client, enum power_supply_property psp,
	union power_supply_propval *val)
372 373 374
{
	int ret;

375 376 377 378 379 380 381 382 383 384 385 386 387 388
	/* 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 */
389 390 391 392 393 394 395 396 397
		/* SBS spec doesn't have a general health command. */
		val->intval = POWER_SUPPLY_HEALTH_UNKNOWN;

	return 0;
}

static int sbs_get_ti_battery_presence_and_health(
	struct i2c_client *client, enum power_supply_property psp,
	union power_supply_propval *val)
398 399 400
{
	s32 ret;

401 402
	/*
	 * Write to ManufacturerAccess with ManufacturerAccess command
403
	 * and then read the status.
404
	 */
405 406 407 408 409 410 411
	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;
	}
412 413

	ret = sbs_read_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr);
414 415 416
	if (ret < 0) {
		if (psp == POWER_SUPPLY_PROP_PRESENT)
			val->intval = 0; /* battery removed */
417
		return ret;
418
	}
419

420 421
	if (ret < sbs_data[REG_MANUFACTURER_DATA].min_value ||
	    ret > sbs_data[REG_MANUFACTURER_DATA].max_value) {
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
		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;
		else
			val->intval = POWER_SUPPLY_HEALTH_GOOD;
	}

	return 0;
}

451
static int sbs_get_battery_property(struct i2c_client *client,
452 453 454
	int reg_offset, enum power_supply_property psp,
	union power_supply_propval *val)
{
455
	struct sbs_info *chip = i2c_get_clientdata(client);
456 457
	s32 ret;

458
	ret = sbs_read_word_data(client, sbs_data[reg_offset].addr);
459 460 461 462
	if (ret < 0)
		return ret;

	/* returned values are 16 bit */
463
	if (sbs_data[reg_offset].min_value < 0)
464
		ret = (s16)ret;
465

466 467
	if (ret >= sbs_data[reg_offset].min_value &&
	    ret <= sbs_data[reg_offset].max_value) {
468
		val->intval = ret;
469 470 471 472 473 474 475 476 477 478 479 480 481
		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;
482
			return 0;
483 484 485
		} else if (psp != POWER_SUPPLY_PROP_STATUS) {
			return 0;
		}
486 487 488 489 490 491 492 493

		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;

494 495
		sbs_status_correct(client, &val->intval);

496 497 498 499
		if (chip->poll_time == 0)
			chip->last_state = val->intval;
		else if (chip->last_state != val->intval) {
			cancel_delayed_work_sync(&chip->work);
500
			power_supply_changed(chip->power_supply);
501
			chip->poll_time = 0;
502 503 504 505
		}
	} else {
		if (psp == POWER_SUPPLY_PROP_STATUS)
			val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
506 507 508 509 510
		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);
511 512 513 514 515 516 517
		else
			val->intval = 0;
	}

	return 0;
}

518 519 520
static int sbs_get_battery_string_property(struct i2c_client *client,
	int reg_offset, enum power_supply_property psp, char *val)
{
521
	return sbs_read_string_data(client, sbs_data[reg_offset].addr, val);
522 523
}

524
static void  sbs_unit_adjustment(struct i2c_client *client,
525 526 527 528
	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)
529 530
#define TIME_UNIT_CONVERSION		60
#define TEMP_KELVIN_TO_CELSIUS		2731
531 532 533 534
	switch (psp) {
	case POWER_SUPPLY_PROP_ENERGY_NOW:
	case POWER_SUPPLY_PROP_ENERGY_FULL:
	case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
535
		/* sbs provides energy in units of 10mWh.
536 537
		 * Convert to µWh
		 */
538 539 540 541
		val->intval *= BATTERY_MODE_CAP_MULT_WATT;
		break;

	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
542
	case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
543 544
	case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
	case POWER_SUPPLY_PROP_CURRENT_NOW:
545
	case POWER_SUPPLY_PROP_CURRENT_AVG:
546
	case POWER_SUPPLY_PROP_CHARGE_NOW:
547 548
	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
549 550
	case POWER_SUPPLY_PROP_CHARGE_FULL:
	case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
551 552 553 554
		val->intval *= BASE_UNIT_CONVERSION;
		break;

	case POWER_SUPPLY_PROP_TEMP:
555
		/* sbs provides battery temperature in 0.1K
556 557 558
		 * so convert it to 0.1°C
		 */
		val->intval -= TEMP_KELVIN_TO_CELSIUS;
559 560 561 562
		break;

	case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
	case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
563
		/* sbs provides time to empty and time to full in minutes.
564 565
		 * Convert to seconds
		 */
566 567 568 569 570 571 572 573 574
		val->intval *= TIME_UNIT_CONVERSION;
		break;

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

575 576
static enum sbs_capacity_mode sbs_set_capacity_mode(struct i2c_client *client,
	enum sbs_capacity_mode mode)
577 578 579
{
	int ret, original_val;

580
	original_val = sbs_read_word_data(client, BATTERY_MODE_OFFSET);
581 582 583
	if (original_val < 0)
		return original_val;

584
	if ((original_val & BATTERY_MODE_CAPACITY_MASK) == mode)
585 586
		return mode;

587 588
	if (mode == CAPACITY_MODE_AMPS)
		ret = original_val & ~BATTERY_MODE_CAPACITY_MASK;
589
	else
590
		ret = original_val | BATTERY_MODE_CAPACITY_MASK;
591

592
	ret = sbs_write_word_data(client, BATTERY_MODE_OFFSET, ret);
593 594 595
	if (ret < 0)
		return ret;

596 597
	usleep_range(1000, 2000);

598
	return original_val & BATTERY_MODE_CAPACITY_MASK;
599 600
}

601
static int sbs_get_battery_capacity(struct i2c_client *client,
602
	int reg_offset, enum power_supply_property psp,
603 604 605
	union power_supply_propval *val)
{
	s32 ret;
606
	enum sbs_capacity_mode mode = CAPACITY_MODE_WATTS;
607 608

	if (power_supply_is_amp_property(psp))
609
		mode = CAPACITY_MODE_AMPS;
610

611
	mode = sbs_set_capacity_mode(client, mode);
612
	if ((int)mode < 0)
613
		return mode;
614

615
	ret = sbs_read_word_data(client, sbs_data[reg_offset].addr);
616 617
	if (ret < 0)
		return ret;
618

619
	val->intval = ret;
620

621
	ret = sbs_set_capacity_mode(client, mode);
622 623 624
	if (ret < 0)
		return ret;

625 626 627
	return 0;
}

628 629
static char sbs_serial[5];
static int sbs_get_battery_serial_number(struct i2c_client *client,
630 631 632 633
	union power_supply_propval *val)
{
	int ret;

634
	ret = sbs_read_word_data(client, sbs_data[REG_SERIAL_NUMBER].addr);
635 636 637
	if (ret < 0)
		return ret;

638
	sprintf(sbs_serial, "%04x", ret);
639
	val->strval = sbs_serial;
640 641 642 643

	return 0;
}

644
static int sbs_get_property_index(struct i2c_client *client,
645 646 647
	enum power_supply_property psp)
{
	int count;
648 649
	for (count = 0; count < ARRAY_SIZE(sbs_data); count++)
		if (psp == sbs_data[count].psp)
650 651 652 653 654 655 656 657
			return count;

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

	return -EINVAL;
}

658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
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;
}

690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
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;
}

722
static int sbs_get_property(struct power_supply *psy,
723 724 725
	enum power_supply_property psp,
	union power_supply_propval *val)
{
726
	int ret = 0;
727
	struct sbs_info *chip = power_supply_get_drvdata(psy);
728
	struct i2c_client *client = chip->client;
729

730 731 732 733 734 735
	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;
736
			sbs_update_presence(chip, ret);
737 738 739 740 741 742
			return 0;
		}
		if (ret == 0)
			return -ENODATA;
	}

743 744 745
	switch (psp) {
	case POWER_SUPPLY_PROP_PRESENT:
	case POWER_SUPPLY_PROP_HEALTH:
746
		if (chip->flags & SBS_FLAGS_TI_BQ20ZX5)
747 748 749 750 751
			ret = sbs_get_ti_battery_presence_and_health(client,
								     psp, val);
		else
			ret = sbs_get_battery_presence_and_health(client, psp,
								  val);
752 753

		/* this can only be true if no gpio is used */
754 755
		if (psp == POWER_SUPPLY_PROP_PRESENT)
			return 0;
756 757 758
		break;

	case POWER_SUPPLY_PROP_TECHNOLOGY:
759 760 761 762
		ret = sbs_get_chemistry(client, val);
		if (ret < 0)
			break;

763
		goto done; /* don't trigger power_supply_changed()! */
764

765 766 767
	case POWER_SUPPLY_PROP_ENERGY_NOW:
	case POWER_SUPPLY_PROP_ENERGY_FULL:
	case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
768 769 770
	case POWER_SUPPLY_PROP_CHARGE_NOW:
	case POWER_SUPPLY_PROP_CHARGE_FULL:
	case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
771
		ret = sbs_get_property_index(client, psp);
772 773
		if (ret < 0)
			break;
774

775 776 777 778 779
		/* 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);
780
		ret = sbs_get_battery_capacity(client, ret, psp, val);
781
		mutex_unlock(&chip->mode_lock);
782 783 784
		break;

	case POWER_SUPPLY_PROP_SERIAL_NUMBER:
785
		ret = sbs_get_battery_serial_number(client, val);
786 787 788
		break;

	case POWER_SUPPLY_PROP_STATUS:
789
	case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
790 791 792
	case POWER_SUPPLY_PROP_CYCLE_COUNT:
	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
	case POWER_SUPPLY_PROP_CURRENT_NOW:
793
	case POWER_SUPPLY_PROP_CURRENT_AVG:
794 795 796
	case POWER_SUPPLY_PROP_TEMP:
	case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
	case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
797
	case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
798
	case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
799 800
	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
801
	case POWER_SUPPLY_PROP_CAPACITY:
802
	case POWER_SUPPLY_PROP_CAPACITY_ERROR_MARGIN:
803
		ret = sbs_get_property_index(client, psp);
804 805
		if (ret < 0)
			break;
806

807
		ret = sbs_get_battery_property(client, ret, psp, val);
808 809
		break;

810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829
	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;

830 831 832 833 834 835
	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;

836 837 838 839 840 841
	default:
		dev_err(&client->dev,
			"%s: INVALID property\n", __func__);
		return -EINVAL;
	}

842
	if (!chip->enable_detection)
843 844
		goto done;

845 846
	if (!chip->gpio_detect &&
		chip->is_present != (ret >= 0)) {
847
		sbs_update_presence(chip, (ret >= 0));
848
		power_supply_changed(chip->power_supply);
849 850 851 852 853
	}

done:
	if (!ret) {
		/* Convert units to match requirements for power supply class */
854
		sbs_unit_adjustment(client, psp, val);
855
	}
856

857
	dev_dbg(&client->dev,
858 859
		"%s: property = %d, value = %x\n", __func__, psp, val->intval);

860
	if (ret && chip->is_present)
861 862 863 864 865
		return ret;

	/* battery not present, so return NODATA for properties */
	if (ret)
		return -ENODATA;
866

867
	return 0;
868 869
}

870
static void sbs_supply_changed(struct sbs_info *chip)
871
{
872
	struct power_supply *battery = chip->power_supply;
873
	int ret;
874

875 876
	ret = gpiod_get_value_cansleep(chip->gpio_detect);
	if (ret < 0)
877
		return;
878
	sbs_update_presence(chip, ret);
879
	power_supply_changed(battery);
880
}
881

882 883 884
static irqreturn_t sbs_irq(int irq, void *devid)
{
	sbs_supply_changed(devid);
885 886 887
	return IRQ_HANDLED;
}

888 889 890 891 892 893
static void sbs_alert(struct i2c_client *client, enum i2c_alert_protocol prot,
	unsigned int data)
{
	sbs_supply_changed(i2c_get_clientdata(client));
}

894
static void sbs_external_power_changed(struct power_supply *psy)
895
{
896
	struct sbs_info *chip = power_supply_get_drvdata(psy);
897 898

	/* cancel outstanding work */
899
	cancel_delayed_work_sync(&chip->work);
900

901
	schedule_delayed_work(&chip->work, HZ);
902
	chip->poll_time = chip->poll_retry_count;
903 904
}

905
static void sbs_delayed_work(struct work_struct *work)
906
{
907
	struct sbs_info *chip;
908 909
	s32 ret;

910
	chip = container_of(work, struct sbs_info, work.work);
911

912
	ret = sbs_read_word_data(chip->client, sbs_data[REG_STATUS].addr);
913 914
	/* if the read failed, give up on this work */
	if (ret < 0) {
915
		chip->poll_time = 0;
916 917 918 919 920 921 922 923 924 925
		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;

926 927
	sbs_status_correct(chip->client, &ret);

928 929
	if (chip->last_state != ret) {
		chip->poll_time = 0;
930
		power_supply_changed(chip->power_supply);
931 932
		return;
	}
933 934 935
	if (chip->poll_time > 0) {
		schedule_delayed_work(&chip->work, HZ);
		chip->poll_time--;
936 937 938 939
		return;
	}
}

940 941 942 943 944 945 946 947
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,
};

948
static int sbs_probe(struct i2c_client *client,
949 950
	const struct i2c_device_id *id)
{
951
	struct sbs_info *chip;
952
	struct power_supply_desc *sbs_desc;
953
	struct sbs_platform_data *pdata = client->dev.platform_data;
954
	struct power_supply_config psy_cfg = {};
955
	int rc;
956
	int irq;
957

958 959 960 961 962 963 964 965
	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)
966
		return -ENOMEM;
967

968
	chip = devm_kzalloc(&client->dev, sizeof(struct sbs_info), GFP_KERNEL);
969 970
	if (!chip)
		return -ENOMEM;
971

972
	chip->flags = (u32)(uintptr_t)of_device_get_match_data(&client->dev);
973 974
	chip->client = client;
	chip->enable_detection = false;
975
	psy_cfg.of_node = client->dev.of_node;
976
	psy_cfg.drv_data = chip;
977
	chip->last_state = POWER_SUPPLY_STATUS_UNKNOWN;
978
	mutex_init(&chip->mode_lock);
979

980 981 982 983 984 985
	/* use pdata if available, fall back to DT properties,
	 * or hardcoded defaults if not
	 */
	rc = of_property_read_u32(client->dev.of_node, "sbs,i2c-retry-count",
				  &chip->i2c_retry_count);
	if (rc)
986
		chip->i2c_retry_count = 0;
987 988 989 990 991 992 993 994 995 996

	rc = of_property_read_u32(client->dev.of_node, "sbs,poll-retry-count",
				  &chip->poll_retry_count);
	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;
	}
997
	chip->i2c_retry_count = chip->i2c_retry_count + 1;
998

999 1000 1001 1002 1003 1004
	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);
1005 1006
	}

1007
	i2c_set_clientdata(client, chip);
1008

1009
	if (!chip->gpio_detect)
1010 1011
		goto skip_gpio;

1012
	irq = gpiod_to_irq(chip->gpio_detect);
1013 1014 1015 1016 1017
	if (irq <= 0) {
		dev_warn(&client->dev, "Failed to get gpio as irq: %d\n", irq);
		goto skip_gpio;
	}

1018
	rc = devm_request_threaded_irq(&client->dev, irq, NULL, sbs_irq,
1019
		IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
1020
		dev_name(&client->dev), chip);
1021 1022 1023 1024 1025 1026
	if (rc) {
		dev_warn(&client->dev, "Failed to request irq: %d\n", rc);
		goto skip_gpio;
	}

skip_gpio:
1027
	/*
1028
	 * Before we register, we might need to make sure we can actually talk
1029 1030
	 * to the battery.
	 */
1031
	if (!(force_load || chip->gpio_detect)) {
1032 1033 1034 1035 1036 1037 1038
		rc = sbs_read_word_data(client, sbs_data[REG_STATUS].addr);

		if (rc < 0) {
			dev_err(&client->dev, "%s: Failed to get device status\n",
				__func__);
			goto exit_psupply;
		}
1039
	}
1040

1041
	chip->power_supply = devm_power_supply_register(&client->dev, sbs_desc,
1042 1043
						   &psy_cfg);
	if (IS_ERR(chip->power_supply)) {
1044 1045
		dev_err(&client->dev,
			"%s: Failed to register power supply\n", __func__);
1046
		rc = PTR_ERR(chip->power_supply);
1047
		goto exit_psupply;
1048 1049 1050 1051 1052
	}

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

1053
	INIT_DELAYED_WORK(&chip->work, sbs_delayed_work);
1054

1055
	chip->enable_detection = true;
1056

1057
	return 0;
1058 1059 1060

exit_psupply:
	return rc;
1061 1062
}

1063
static int sbs_remove(struct i2c_client *client)
1064
{
1065
	struct sbs_info *chip = i2c_get_clientdata(client);
1066

1067
	cancel_delayed_work_sync(&chip->work);
1068

1069 1070 1071
	return 0;
}

1072 1073 1074
#if defined CONFIG_PM_SLEEP

static int sbs_suspend(struct device *dev)
1075
{
1076
	struct i2c_client *client = to_i2c_client(dev);
1077
	struct sbs_info *chip = i2c_get_clientdata(client);
1078
	int ret;
1079

1080 1081
	if (chip->poll_time > 0)
		cancel_delayed_work_sync(&chip->work);
1082

1083
	if (chip->flags & SBS_FLAGS_TI_BQ20ZX5) {
1084 1085 1086 1087 1088 1089 1090
		/* 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;
	}
1091 1092 1093

	return 0;
}
1094 1095 1096 1097

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

1098
#else
1099
#define SBS_PM_OPS NULL
1100 1101
#endif

1102
static const struct i2c_device_id sbs_id[] = {
1103
	{ "bq20z65", 0 },
1104
	{ "bq20z75", 0 },
1105
	{ "sbs-battery", 1 },
1106 1107
	{}
};
1108 1109
MODULE_DEVICE_TABLE(i2c, sbs_id);

1110 1111
static const struct of_device_id sbs_dt_ids[] = {
	{ .compatible = "sbs,sbs-battery" },
1112 1113 1114 1115
	{
		.compatible = "ti,bq20z65",
		.data = (void *)SBS_FLAGS_TI_BQ20ZX5,
	},
1116 1117
	{
		.compatible = "ti,bq20z75",
1118
		.data = (void *)SBS_FLAGS_TI_BQ20ZX5,
1119
	},
1120 1121 1122 1123
	{ }
};
MODULE_DEVICE_TABLE(of, sbs_dt_ids);

1124 1125
static struct i2c_driver sbs_battery_driver = {
	.probe		= sbs_probe,
1126
	.remove		= sbs_remove,
1127
	.alert		= sbs_alert,
1128
	.id_table	= sbs_id,
1129
	.driver = {
1130
		.name	= "sbs-battery",
1131
		.of_match_table = sbs_dt_ids,
1132
		.pm	= SBS_PM_OPS,
1133 1134
	},
};
1135
module_i2c_driver(sbs_battery_driver);
1136

1137
MODULE_DESCRIPTION("SBS battery monitor driver");
1138
MODULE_LICENSE("GPL");
1139

1140
module_param(force_load, bool, 0444);
1141 1142
MODULE_PARM_DESC(force_load,
		 "Attempt to load the driver even if no battery is connected");