sbs-battery.c 24.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/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 28 29 30 31 32

enum {
	REG_MANUFACTURER_DATA,
	REG_TEMPERATURE,
	REG_VOLTAGE,
	REG_CURRENT,
	REG_CAPACITY,
	REG_TIME_TO_EMPTY,
	REG_TIME_TO_FULL,
	REG_STATUS,
33
	REG_CAPACITY_LEVEL,
34
	REG_CYCLE_COUNT,
35 36
	REG_SERIAL_NUMBER,
	REG_REMAINING_CAPACITY,
37
	REG_REMAINING_CAPACITY_CHARGE,
38
	REG_FULL_CHARGE_CAPACITY,
39
	REG_FULL_CHARGE_CAPACITY_CHARGE,
40
	REG_DESIGN_CAPACITY,
41
	REG_DESIGN_CAPACITY_CHARGE,
42 43
	REG_DESIGN_VOLTAGE_MIN,
	REG_DESIGN_VOLTAGE_MAX,
44 45
	REG_MANUFACTURER,
	REG_MODEL_NAME,
46 47
};

48 49
/* Battery Mode defines */
#define BATTERY_MODE_OFFSET		0x03
50 51 52 53
#define BATTERY_MODE_CAPACITY_MASK	BIT(15)
enum sbs_capacity_mode {
	CAPACITY_MODE_AMPS = 0,
	CAPACITY_MODE_WATTS = BATTERY_MODE_CAPACITY_MASK
54 55
};

56 57 58 59 60
/* manufacturer access defines */
#define MANUFACTURER_ACCESS_STATUS	0x0006
#define MANUFACTURER_ACCESS_SLEEP	0x0011

/* battery status value bits */
61
#define BATTERY_INITIALIZED		0x80
62
#define BATTERY_DISCHARGING		0x40
63 64 65
#define BATTERY_FULL_CHARGED		0x20
#define BATTERY_FULL_DISCHARGED		0x10

66
/* min_value and max_value are only valid for numerical data */
67
#define SBS_DATA(_psp, _addr, _min_value, _max_value) { \
68 69 70 71 72 73
	.psp = _psp, \
	.addr = _addr, \
	.min_value = _min_value, \
	.max_value = _max_value, \
}

74
static const struct chip_data {
75 76 77 78
	enum power_supply_property psp;
	u8 addr;
	int min_value;
	int max_value;
79
} sbs_data[] = {
80
	[REG_MANUFACTURER_DATA] =
81
		SBS_DATA(POWER_SUPPLY_PROP_PRESENT, 0x00, 0, 65535),
82
	[REG_TEMPERATURE] =
83
		SBS_DATA(POWER_SUPPLY_PROP_TEMP, 0x08, 0, 65535),
84
	[REG_VOLTAGE] =
85
		SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_NOW, 0x09, 0, 20000),
86
	[REG_CURRENT] =
87
		SBS_DATA(POWER_SUPPLY_PROP_CURRENT_NOW, 0x0A, -32768, 32767),
88
	[REG_CAPACITY] =
89
		SBS_DATA(POWER_SUPPLY_PROP_CAPACITY, 0x0D, 0, 100),
90
	[REG_REMAINING_CAPACITY] =
91
		SBS_DATA(POWER_SUPPLY_PROP_ENERGY_NOW, 0x0F, 0, 65535),
92
	[REG_REMAINING_CAPACITY_CHARGE] =
93
		SBS_DATA(POWER_SUPPLY_PROP_CHARGE_NOW, 0x0F, 0, 65535),
94
	[REG_FULL_CHARGE_CAPACITY] =
95
		SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL, 0x10, 0, 65535),
96
	[REG_FULL_CHARGE_CAPACITY_CHARGE] =
97
		SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL, 0x10, 0, 65535),
98
	[REG_TIME_TO_EMPTY] =
99
		SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, 0x12, 0, 65535),
100
	[REG_TIME_TO_FULL] =
101
		SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_FULL_AVG, 0x13, 0, 65535),
102
	[REG_STATUS] =
103
		SBS_DATA(POWER_SUPPLY_PROP_STATUS, 0x16, 0, 65535),
104 105
	[REG_CAPACITY_LEVEL] =
		SBS_DATA(POWER_SUPPLY_PROP_CAPACITY_LEVEL, 0x16, 0, 65535),
106
	[REG_CYCLE_COUNT] =
107
		SBS_DATA(POWER_SUPPLY_PROP_CYCLE_COUNT, 0x17, 0, 65535),
108
	[REG_DESIGN_CAPACITY] =
109
		SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, 0x18, 0, 65535),
110
	[REG_DESIGN_CAPACITY_CHARGE] =
111
		SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 0x18, 0, 65535),
112 113 114
	[REG_DESIGN_VOLTAGE_MIN] =
		SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, 0x19, 0, 65535),
	[REG_DESIGN_VOLTAGE_MAX] =
115
		SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, 0x19, 0, 65535),
116
	[REG_SERIAL_NUMBER] =
117
		SBS_DATA(POWER_SUPPLY_PROP_SERIAL_NUMBER, 0x1C, 0, 65535),
118 119 120 121 122
	/* Properties of type `const char *' */
	[REG_MANUFACTURER] =
		SBS_DATA(POWER_SUPPLY_PROP_MANUFACTURER, 0x20, 0, 65535),
	[REG_MODEL_NAME] =
		SBS_DATA(POWER_SUPPLY_PROP_MODEL_NAME, 0x21, 0, 65535)
123 124
};

125
static enum power_supply_property sbs_properties[] = {
126
	POWER_SUPPLY_PROP_STATUS,
127
	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
128 129 130 131 132 133 134 135 136 137 138
	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,
	POWER_SUPPLY_PROP_CAPACITY,
	POWER_SUPPLY_PROP_TEMP,
	POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
	POWER_SUPPLY_PROP_TIME_TO_FULL_AVG,
	POWER_SUPPLY_PROP_SERIAL_NUMBER,
139
	POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
140 141 142 143
	POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
	POWER_SUPPLY_PROP_ENERGY_NOW,
	POWER_SUPPLY_PROP_ENERGY_FULL,
	POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
144 145 146
	POWER_SUPPLY_PROP_CHARGE_NOW,
	POWER_SUPPLY_PROP_CHARGE_FULL,
	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
147 148 149
	/* Properties of type `const char *' */
	POWER_SUPPLY_PROP_MANUFACTURER,
	POWER_SUPPLY_PROP_MODEL_NAME
150 151
};

152 153 154
/* Supports special manufacturer commands from TI BQ20Z75 IC. */
#define SBS_FLAGS_TI_BQ20Z75		BIT(0)

155
struct sbs_info {
156
	struct i2c_client		*client;
157
	struct power_supply		*power_supply;
158
	bool				is_present;
159
	struct gpio_desc		*gpio_detect;
160
	bool				enable_detection;
161 162
	int				last_state;
	int				poll_time;
163 164
	u32				i2c_retry_count;
	u32				poll_retry_count;
165
	struct delayed_work		work;
166
	struct mutex			mode_lock;
167
	u32				flags;
168 169
};

170 171
static char model_name[I2C_SMBUS_BLOCK_MAX + 1];
static char manufacturer[I2C_SMBUS_BLOCK_MAX + 1];
172
static bool force_load;
173

174
static int sbs_read_word_data(struct i2c_client *client, u8 address)
175
{
176
	struct sbs_info *chip = i2c_get_clientdata(client);
177
	int retries = chip->i2c_retry_count;
R
Rhyland Klein 已提交
178 179 180 181 182 183 184 185
	s32 ret = 0;

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

	if (ret < 0) {
188
		dev_dbg(&client->dev,
189 190 191 192
			"%s: i2c read at address 0x%x failed\n",
			__func__, address);
		return ret;
	}
R
Rhyland Klein 已提交
193

194
	return ret;
195 196
}

197 198 199 200 201
static int sbs_read_string_data(struct i2c_client *client, u8 address,
				char *values)
{
	struct sbs_info *chip = i2c_get_clientdata(client);
	s32 ret = 0, block_length = 0;
202
	int retries_length, retries_block;
203 204
	u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1];

205 206
	retries_length = chip->i2c_retry_count;
	retries_block = chip->i2c_retry_count;
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

	/* Adapter needs to support these two functions */
	if (!i2c_check_functionality(client->adapter,
				     I2C_FUNC_SMBUS_BYTE_DATA |
				     I2C_FUNC_SMBUS_I2C_BLOCK)){
		return -ENODEV;
	}

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

	/* Get the block data */
	while (retries_block > 0) {
		ret = i2c_smbus_read_i2c_block_data(
				client, address,
				block_length + 1, block_buffer);
		if (ret >= 0)
			break;
		retries_block--;
	}

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

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

260
	return ret;
261 262
}

263
static int sbs_write_word_data(struct i2c_client *client, u8 address,
264 265
	u16 value)
{
266
	struct sbs_info *chip = i2c_get_clientdata(client);
267
	int retries = chip->i2c_retry_count;
R
Rhyland Klein 已提交
268 269 270
	s32 ret = 0;

	while (retries > 0) {
271
		ret = i2c_smbus_write_word_data(client, address, value);
R
Rhyland Klein 已提交
272 273 274 275
		if (ret >= 0)
			break;
		retries--;
	}
276 277

	if (ret < 0) {
278
		dev_dbg(&client->dev,
279 280 281 282
			"%s: i2c write to address 0x%x failed\n",
			__func__, address);
		return ret;
	}
R
Rhyland Klein 已提交
283

284 285 286
	return 0;
}

287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
static int sbs_status_correct(struct i2c_client *client, int *intval)
{
	int ret;

	ret = sbs_read_word_data(client, sbs_data[REG_CURRENT].addr);
	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;
}

312
static int sbs_get_battery_presence_and_health(
313 314
	struct i2c_client *client, enum power_supply_property psp,
	union power_supply_propval *val)
315 316 317
{
	int ret;

318 319 320 321 322 323 324 325 326 327 328 329 330 331
	/* 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 */
332 333 334 335 336 337 338 339 340
		/* 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)
341 342 343
{
	s32 ret;

344 345
	/*
	 * Write to ManufacturerAccess with ManufacturerAccess command
346
	 * and then read the status.
347
	 */
348 349 350 351 352 353 354
	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;
	}
355 356

	ret = sbs_read_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr);
357 358 359
	if (ret < 0) {
		if (psp == POWER_SUPPLY_PROP_PRESENT)
			val->intval = 0; /* battery removed */
360
		return ret;
361
	}
362

363 364
	if (ret < sbs_data[REG_MANUFACTURER_DATA].min_value ||
	    ret > sbs_data[REG_MANUFACTURER_DATA].max_value) {
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
		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;
}

394
static int sbs_get_battery_property(struct i2c_client *client,
395 396 397
	int reg_offset, enum power_supply_property psp,
	union power_supply_propval *val)
{
398
	struct sbs_info *chip = i2c_get_clientdata(client);
399 400
	s32 ret;

401
	ret = sbs_read_word_data(client, sbs_data[reg_offset].addr);
402 403 404 405
	if (ret < 0)
		return ret;

	/* returned values are 16 bit */
406
	if (sbs_data[reg_offset].min_value < 0)
407
		ret = (s16)ret;
408

409 410
	if (ret >= sbs_data[reg_offset].min_value &&
	    ret <= sbs_data[reg_offset].max_value) {
411
		val->intval = ret;
412 413 414 415 416 417 418 419 420 421 422 423 424
		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;
425
			return 0;
426 427 428
		} else if (psp != POWER_SUPPLY_PROP_STATUS) {
			return 0;
		}
429 430 431 432 433 434 435 436

		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;

437 438
		sbs_status_correct(client, &val->intval);

439 440 441 442
		if (chip->poll_time == 0)
			chip->last_state = val->intval;
		else if (chip->last_state != val->intval) {
			cancel_delayed_work_sync(&chip->work);
443
			power_supply_changed(chip->power_supply);
444
			chip->poll_time = 0;
445 446 447 448
		}
	} else {
		if (psp == POWER_SUPPLY_PROP_STATUS)
			val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
449 450 451 452 453
		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);
454 455 456 457 458 459 460
		else
			val->intval = 0;
	}

	return 0;
}

461 462 463 464 465 466 467 468 469 470 471 472 473
static int sbs_get_battery_string_property(struct i2c_client *client,
	int reg_offset, enum power_supply_property psp, char *val)
{
	s32 ret;

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

	if (ret < 0)
		return ret;

	return 0;
}

474
static void  sbs_unit_adjustment(struct i2c_client *client,
475 476 477 478
	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)
B
Benson Leung 已提交
479 480
#define TIME_UNIT_CONVERSION		60
#define TEMP_KELVIN_TO_CELSIUS		2731
481 482 483 484
	switch (psp) {
	case POWER_SUPPLY_PROP_ENERGY_NOW:
	case POWER_SUPPLY_PROP_ENERGY_FULL:
	case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
485
		/* sbs provides energy in units of 10mWh.
B
Benson Leung 已提交
486 487
		 * Convert to µWh
		 */
488 489 490 491
		val->intval *= BATTERY_MODE_CAP_MULT_WATT;
		break;

	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
492
	case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
493 494
	case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
	case POWER_SUPPLY_PROP_CURRENT_NOW:
495 496 497
	case POWER_SUPPLY_PROP_CHARGE_NOW:
	case POWER_SUPPLY_PROP_CHARGE_FULL:
	case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
498 499 500 501
		val->intval *= BASE_UNIT_CONVERSION;
		break;

	case POWER_SUPPLY_PROP_TEMP:
502
		/* sbs provides battery temperature in 0.1K
B
Benson Leung 已提交
503 504 505
		 * so convert it to 0.1°C
		 */
		val->intval -= TEMP_KELVIN_TO_CELSIUS;
506 507 508 509
		break;

	case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
	case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
510
		/* sbs provides time to empty and time to full in minutes.
B
Benson Leung 已提交
511 512
		 * Convert to seconds
		 */
513 514 515 516 517 518 519 520 521
		val->intval *= TIME_UNIT_CONVERSION;
		break;

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

522 523
static enum sbs_capacity_mode sbs_set_capacity_mode(struct i2c_client *client,
	enum sbs_capacity_mode mode)
524 525 526
{
	int ret, original_val;

527
	original_val = sbs_read_word_data(client, BATTERY_MODE_OFFSET);
528 529 530
	if (original_val < 0)
		return original_val;

531
	if ((original_val & BATTERY_MODE_CAPACITY_MASK) == mode)
532 533
		return mode;

534 535
	if (mode == CAPACITY_MODE_AMPS)
		ret = original_val & ~BATTERY_MODE_CAPACITY_MASK;
536
	else
537
		ret = original_val | BATTERY_MODE_CAPACITY_MASK;
538

539
	ret = sbs_write_word_data(client, BATTERY_MODE_OFFSET, ret);
540 541 542
	if (ret < 0)
		return ret;

543 544
	usleep_range(1000, 2000);

545
	return original_val & BATTERY_MODE_CAPACITY_MASK;
546 547
}

548
static int sbs_get_battery_capacity(struct i2c_client *client,
549
	int reg_offset, enum power_supply_property psp,
550 551 552
	union power_supply_propval *val)
{
	s32 ret;
553
	enum sbs_capacity_mode mode = CAPACITY_MODE_WATTS;
554 555

	if (power_supply_is_amp_property(psp))
556
		mode = CAPACITY_MODE_AMPS;
557

558
	mode = sbs_set_capacity_mode(client, mode);
559
	if ((int)mode < 0)
560
		return mode;
561

562
	ret = sbs_read_word_data(client, sbs_data[reg_offset].addr);
563 564
	if (ret < 0)
		return ret;
565

566
	val->intval = ret;
567

568
	ret = sbs_set_capacity_mode(client, mode);
569 570 571
	if (ret < 0)
		return ret;

572 573 574
	return 0;
}

575 576
static char sbs_serial[5];
static int sbs_get_battery_serial_number(struct i2c_client *client,
577 578 579 580
	union power_supply_propval *val)
{
	int ret;

581
	ret = sbs_read_word_data(client, sbs_data[REG_SERIAL_NUMBER].addr);
582 583 584
	if (ret < 0)
		return ret;

585
	sprintf(sbs_serial, "%04x", ret);
586
	val->strval = sbs_serial;
587 588 589 590

	return 0;
}

591
static int sbs_get_property_index(struct i2c_client *client,
592 593 594
	enum power_supply_property psp)
{
	int count;
595 596
	for (count = 0; count < ARRAY_SIZE(sbs_data); count++)
		if (psp == sbs_data[count].psp)
597 598 599 600 601 602 603 604
			return count;

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

	return -EINVAL;
}

605
static int sbs_get_property(struct power_supply *psy,
606 607 608
	enum power_supply_property psp,
	union power_supply_propval *val)
{
609
	int ret = 0;
610
	struct sbs_info *chip = power_supply_get_drvdata(psy);
611
	struct i2c_client *client = chip->client;
612

613 614 615 616 617 618 619 620 621 622 623 624 625
	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;
			chip->is_present = val->intval;
			return 0;
		}
		if (ret == 0)
			return -ENODATA;
	}

626 627 628
	switch (psp) {
	case POWER_SUPPLY_PROP_PRESENT:
	case POWER_SUPPLY_PROP_HEALTH:
629
		if (chip->flags & SBS_FLAGS_TI_BQ20Z75)
630 631 632 633 634
			ret = sbs_get_ti_battery_presence_and_health(client,
								     psp, val);
		else
			ret = sbs_get_battery_presence_and_health(client, psp,
								  val);
635 636

		/* this can only be true if no gpio is used */
637 638
		if (psp == POWER_SUPPLY_PROP_PRESENT)
			return 0;
639 640 641 642
		break;

	case POWER_SUPPLY_PROP_TECHNOLOGY:
		val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
643
		goto done; /* don't trigger power_supply_changed()! */
644

645 646 647
	case POWER_SUPPLY_PROP_ENERGY_NOW:
	case POWER_SUPPLY_PROP_ENERGY_FULL:
	case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
648 649 650
	case POWER_SUPPLY_PROP_CHARGE_NOW:
	case POWER_SUPPLY_PROP_CHARGE_FULL:
	case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
651
		ret = sbs_get_property_index(client, psp);
652 653
		if (ret < 0)
			break;
654

655 656 657 658 659
		/* 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);
660
		ret = sbs_get_battery_capacity(client, ret, psp, val);
661
		mutex_unlock(&chip->mode_lock);
662 663 664
		break;

	case POWER_SUPPLY_PROP_SERIAL_NUMBER:
665
		ret = sbs_get_battery_serial_number(client, val);
666 667 668
		break;

	case POWER_SUPPLY_PROP_STATUS:
669
	case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
670 671 672 673 674 675
	case POWER_SUPPLY_PROP_CYCLE_COUNT:
	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
	case POWER_SUPPLY_PROP_CURRENT_NOW:
	case POWER_SUPPLY_PROP_TEMP:
	case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
	case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
676
	case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
677
	case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
678
	case POWER_SUPPLY_PROP_CAPACITY:
679
		ret = sbs_get_property_index(client, psp);
680 681
		if (ret < 0)
			break;
682

683
		ret = sbs_get_battery_property(client, ret, psp, val);
684 685
		break;

686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705
	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;

706 707 708 709 710 711
	default:
		dev_err(&client->dev,
			"%s: INVALID property\n", __func__);
		return -EINVAL;
	}

712
	if (!chip->enable_detection)
713 714
		goto done;

715 716 717
	if (!chip->gpio_detect &&
		chip->is_present != (ret >= 0)) {
		chip->is_present = (ret >= 0);
718
		power_supply_changed(chip->power_supply);
719 720 721 722 723
	}

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

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

730
	if (ret && chip->is_present)
731 732 733 734 735
		return ret;

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

737
	return 0;
738 739
}

740
static void sbs_supply_changed(struct sbs_info *chip)
741
{
742
	struct power_supply *battery = chip->power_supply;
743
	int ret;
744

745 746
	ret = gpiod_get_value_cansleep(chip->gpio_detect);
	if (ret < 0)
747
		return;
748
	chip->is_present = ret;
749
	power_supply_changed(battery);
750
}
751

752 753 754
static irqreturn_t sbs_irq(int irq, void *devid)
{
	sbs_supply_changed(devid);
755 756 757
	return IRQ_HANDLED;
}

758 759 760 761 762 763
static void sbs_alert(struct i2c_client *client, enum i2c_alert_protocol prot,
	unsigned int data)
{
	sbs_supply_changed(i2c_get_clientdata(client));
}

764
static void sbs_external_power_changed(struct power_supply *psy)
765
{
766
	struct sbs_info *chip = power_supply_get_drvdata(psy);
767 768

	/* cancel outstanding work */
769
	cancel_delayed_work_sync(&chip->work);
770

771
	schedule_delayed_work(&chip->work, HZ);
772
	chip->poll_time = chip->poll_retry_count;
773 774
}

775
static void sbs_delayed_work(struct work_struct *work)
776
{
777
	struct sbs_info *chip;
778 779
	s32 ret;

780
	chip = container_of(work, struct sbs_info, work.work);
781

782
	ret = sbs_read_word_data(chip->client, sbs_data[REG_STATUS].addr);
783 784
	/* if the read failed, give up on this work */
	if (ret < 0) {
785
		chip->poll_time = 0;
786 787 788 789 790 791 792 793 794 795
		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;

796 797
	sbs_status_correct(chip->client, &ret);

798 799
	if (chip->last_state != ret) {
		chip->poll_time = 0;
800
		power_supply_changed(chip->power_supply);
801 802
		return;
	}
803 804 805
	if (chip->poll_time > 0) {
		schedule_delayed_work(&chip->work, HZ);
		chip->poll_time--;
806 807 808 809
		return;
	}
}

810 811 812 813 814 815 816 817
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,
};

B
Bill Pemberton 已提交
818
static int sbs_probe(struct i2c_client *client,
819 820
	const struct i2c_device_id *id)
{
821
	struct sbs_info *chip;
822
	struct power_supply_desc *sbs_desc;
823
	struct sbs_platform_data *pdata = client->dev.platform_data;
824
	struct power_supply_config psy_cfg = {};
825
	int rc;
826
	int irq;
827

828 829 830 831 832 833 834 835
	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)
836
		return -ENOMEM;
837

838
	chip = devm_kzalloc(&client->dev, sizeof(struct sbs_info), GFP_KERNEL);
839 840
	if (!chip)
		return -ENOMEM;
841

842
	chip->flags = (u32)(uintptr_t)of_device_get_match_data(&client->dev);
843 844
	chip->client = client;
	chip->enable_detection = false;
845
	psy_cfg.of_node = client->dev.of_node;
846
	psy_cfg.drv_data = chip;
847
	chip->last_state = POWER_SUPPLY_STATUS_UNKNOWN;
848
	mutex_init(&chip->mode_lock);
849

850 851 852 853 854 855
	/* 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)
856
		chip->i2c_retry_count = 0;
857 858 859 860 861 862 863 864 865 866

	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;
	}
867
	chip->i2c_retry_count = chip->i2c_retry_count + 1;
R
Rhyland Klein 已提交
868

869 870 871 872 873 874
	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);
875 876
	}

877
	i2c_set_clientdata(client, chip);
878

879
	if (!chip->gpio_detect)
880 881
		goto skip_gpio;

882
	irq = gpiod_to_irq(chip->gpio_detect);
883 884 885 886 887
	if (irq <= 0) {
		dev_warn(&client->dev, "Failed to get gpio as irq: %d\n", irq);
		goto skip_gpio;
	}

888
	rc = devm_request_threaded_irq(&client->dev, irq, NULL, sbs_irq,
889
		IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
890
		dev_name(&client->dev), chip);
891 892 893 894 895 896
	if (rc) {
		dev_warn(&client->dev, "Failed to request irq: %d\n", rc);
		goto skip_gpio;
	}

skip_gpio:
897
	/*
898
	 * Before we register, we might need to make sure we can actually talk
899 900
	 * to the battery.
	 */
901
	if (!(force_load || chip->gpio_detect)) {
902 903 904 905 906 907 908
		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;
		}
909
	}
910

911
	chip->power_supply = devm_power_supply_register(&client->dev, sbs_desc,
912 913
						   &psy_cfg);
	if (IS_ERR(chip->power_supply)) {
914 915
		dev_err(&client->dev,
			"%s: Failed to register power supply\n", __func__);
916
		rc = PTR_ERR(chip->power_supply);
917
		goto exit_psupply;
918 919 920 921 922
	}

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

923
	INIT_DELAYED_WORK(&chip->work, sbs_delayed_work);
924

925
	chip->enable_detection = true;
926

927
	return 0;
928 929 930

exit_psupply:
	return rc;
931 932
}

B
Bill Pemberton 已提交
933
static int sbs_remove(struct i2c_client *client)
934
{
935
	struct sbs_info *chip = i2c_get_clientdata(client);
936

937
	cancel_delayed_work_sync(&chip->work);
938

939 940 941
	return 0;
}

L
Lars-Peter Clausen 已提交
942 943 944
#if defined CONFIG_PM_SLEEP

static int sbs_suspend(struct device *dev)
945
{
L
Lars-Peter Clausen 已提交
946
	struct i2c_client *client = to_i2c_client(dev);
947
	struct sbs_info *chip = i2c_get_clientdata(client);
948
	int ret;
949

950 951
	if (chip->poll_time > 0)
		cancel_delayed_work_sync(&chip->work);
952

953 954 955 956 957 958 959 960
	if (chip->flags & SBS_FLAGS_TI_BQ20Z75) {
		/* 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;
	}
961 962 963

	return 0;
}
L
Lars-Peter Clausen 已提交
964 965 966 967

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

968
#else
L
Lars-Peter Clausen 已提交
969
#define SBS_PM_OPS NULL
970 971
#endif

972
static const struct i2c_device_id sbs_id[] = {
973
	{ "bq20z75", 0 },
974
	{ "sbs-battery", 1 },
975 976
	{}
};
977 978
MODULE_DEVICE_TABLE(i2c, sbs_id);

979 980
static const struct of_device_id sbs_dt_ids[] = {
	{ .compatible = "sbs,sbs-battery" },
981 982 983 984
	{
		.compatible = "ti,bq20z75",
		.data = (void *)SBS_FLAGS_TI_BQ20Z75,
	},
985 986 987 988
	{ }
};
MODULE_DEVICE_TABLE(of, sbs_dt_ids);

989 990
static struct i2c_driver sbs_battery_driver = {
	.probe		= sbs_probe,
B
Bill Pemberton 已提交
991
	.remove		= sbs_remove,
992
	.alert		= sbs_alert,
993
	.id_table	= sbs_id,
994
	.driver = {
995
		.name	= "sbs-battery",
996
		.of_match_table = sbs_dt_ids,
L
Lars-Peter Clausen 已提交
997
		.pm	= SBS_PM_OPS,
998 999
	},
};
1000
module_i2c_driver(sbs_battery_driver);
1001

1002
MODULE_DESCRIPTION("SBS battery monitor driver");
1003
MODULE_LICENSE("GPL");
1004

1005
module_param(force_load, bool, 0444);
1006 1007
MODULE_PARM_DESC(force_load,
		 "Attempt to load the driver even if no battery is connected");