xref: /linux/drivers/power/supply/sbs-battery.c (revision 7db28abbea0f7dc1ec4fdfdc149db5fbd9e4c994)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Gas Gauge driver for SBS Compliant Batteries
4  *
5  * Copyright (c) 2010, NVIDIA Corporation.
6  */
7 
8 #include <linux/bits.h>
9 #include <linux/delay.h>
10 #include <linux/devm-helpers.h>
11 #include <linux/err.h>
12 #include <linux/gpio/consumer.h>
13 #include <linux/i2c.h>
14 #include <linux/init.h>
15 #include <linux/interrupt.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/property.h>
19 #include <linux/of.h>
20 #include <linux/power/sbs-battery.h>
21 #include <linux/power_supply.h>
22 #include <linux/slab.h>
23 #include <linux/stat.h>
24 #include <linux/string_choices.h>
25 
26 enum {
27 	REG_MANUFACTURER_DATA,
28 	REG_BATTERY_MODE,
29 	REG_TEMPERATURE,
30 	REG_VOLTAGE,
31 	REG_CURRENT_NOW,
32 	REG_CURRENT_AVG,
33 	REG_MAX_ERR,
34 	REG_CAPACITY,
35 	REG_TIME_TO_EMPTY_NOW,
36 	REG_TIME_TO_EMPTY_AVG,
37 	REG_TIME_TO_FULL_AVG,
38 	REG_STATUS,
39 	REG_CAPACITY_LEVEL,
40 	REG_CYCLE_COUNT,
41 	REG_SERIAL_NUMBER,
42 	REG_REMAINING_CAPACITY,
43 	REG_REMAINING_CAPACITY_CHARGE,
44 	REG_FULL_CHARGE_CAPACITY,
45 	REG_FULL_CHARGE_CAPACITY_CHARGE,
46 	REG_DESIGN_CAPACITY,
47 	REG_DESIGN_CAPACITY_CHARGE,
48 	REG_DESIGN_VOLTAGE_MIN,
49 	REG_DESIGN_VOLTAGE_MAX,
50 	REG_CHEMISTRY,
51 	REG_MANUFACTURER,
52 	REG_MODEL_NAME,
53 	REG_CHARGE_CURRENT,
54 	REG_CHARGE_VOLTAGE,
55 };
56 
57 #define REG_ADDR_SPEC_INFO		0x1A
58 #define SPEC_INFO_VERSION_MASK		GENMASK(7, 4)
59 #define SPEC_INFO_VERSION_SHIFT		4
60 
61 #define SBS_VERSION_1_0			1
62 #define SBS_VERSION_1_1			2
63 #define SBS_VERSION_1_1_WITH_PEC	3
64 
65 #define REG_ADDR_MANUFACTURE_DATE	0x1B
66 
67 /* Battery Mode defines */
68 #define BATTERY_MODE_OFFSET		0x03
69 #define BATTERY_MODE_CAPACITY_MASK	BIT(15)
70 enum sbs_capacity_mode {
71 	CAPACITY_MODE_AMPS = 0,
72 	CAPACITY_MODE_WATTS = BATTERY_MODE_CAPACITY_MASK
73 };
74 #define BATTERY_MODE_CHARGER_MASK	(1<<14)
75 
76 /* manufacturer access defines */
77 #define MANUFACTURER_ACCESS_STATUS	0x0006
78 #define MANUFACTURER_ACCESS_SLEEP	0x0011
79 
80 /* battery status value bits */
81 #define BATTERY_INITIALIZED		0x80
82 #define BATTERY_DISCHARGING		0x40
83 #define BATTERY_FULL_CHARGED		0x20
84 #define BATTERY_FULL_DISCHARGED		0x10
85 
86 /* min_value and max_value are only valid for numerical data */
87 #define SBS_DATA(_psp, _addr, _min_value, _max_value) { \
88 	.psp = _psp, \
89 	.addr = _addr, \
90 	.min_value = _min_value, \
91 	.max_value = _max_value, \
92 }
93 
94 static const struct chip_data {
95 	enum power_supply_property psp;
96 	u8 addr;
97 	int min_value;
98 	int max_value;
99 } sbs_data[] = {
100 	[REG_MANUFACTURER_DATA] =
101 		SBS_DATA(POWER_SUPPLY_PROP_PRESENT, 0x00, 0, 65535),
102 	[REG_BATTERY_MODE] =
103 		SBS_DATA(-1, 0x03, 0, 65535),
104 	[REG_TEMPERATURE] =
105 		SBS_DATA(POWER_SUPPLY_PROP_TEMP, 0x08, 0, 65535),
106 	[REG_VOLTAGE] =
107 		SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_NOW, 0x09, 0, 65535),
108 	[REG_CURRENT_NOW] =
109 		SBS_DATA(POWER_SUPPLY_PROP_CURRENT_NOW, 0x0A, -32768, 32767),
110 	[REG_CURRENT_AVG] =
111 		SBS_DATA(POWER_SUPPLY_PROP_CURRENT_AVG, 0x0B, -32768, 32767),
112 	[REG_MAX_ERR] =
113 		SBS_DATA(POWER_SUPPLY_PROP_CAPACITY_ERROR_MARGIN, 0x0c, 0, 100),
114 	[REG_CAPACITY] =
115 		SBS_DATA(POWER_SUPPLY_PROP_CAPACITY, 0x0D, 0, 100),
116 	[REG_REMAINING_CAPACITY] =
117 		SBS_DATA(POWER_SUPPLY_PROP_ENERGY_NOW, 0x0F, 0, 65535),
118 	[REG_REMAINING_CAPACITY_CHARGE] =
119 		SBS_DATA(POWER_SUPPLY_PROP_CHARGE_NOW, 0x0F, 0, 65535),
120 	[REG_FULL_CHARGE_CAPACITY] =
121 		SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL, 0x10, 0, 65535),
122 	[REG_FULL_CHARGE_CAPACITY_CHARGE] =
123 		SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL, 0x10, 0, 65535),
124 	[REG_TIME_TO_EMPTY_NOW] =
125 		SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, 0x11, 0, 65535),
126 	[REG_TIME_TO_EMPTY_AVG] =
127 		SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, 0x12, 0, 65535),
128 	[REG_TIME_TO_FULL_AVG] =
129 		SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_FULL_AVG, 0x13, 0, 65535),
130 	[REG_CHARGE_CURRENT] =
131 		SBS_DATA(POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX, 0x14, 0, 65535),
132 	[REG_CHARGE_VOLTAGE] =
133 		SBS_DATA(POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX, 0x15, 0, 65535),
134 	[REG_STATUS] =
135 		SBS_DATA(POWER_SUPPLY_PROP_STATUS, 0x16, 0, 65535),
136 	[REG_CAPACITY_LEVEL] =
137 		SBS_DATA(POWER_SUPPLY_PROP_CAPACITY_LEVEL, 0x16, 0, 65535),
138 	[REG_CYCLE_COUNT] =
139 		SBS_DATA(POWER_SUPPLY_PROP_CYCLE_COUNT, 0x17, 0, 65535),
140 	[REG_DESIGN_CAPACITY] =
141 		SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, 0x18, 0, 65535),
142 	[REG_DESIGN_CAPACITY_CHARGE] =
143 		SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 0x18, 0, 65535),
144 	[REG_DESIGN_VOLTAGE_MIN] =
145 		SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, 0x19, 0, 65535),
146 	[REG_DESIGN_VOLTAGE_MAX] =
147 		SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, 0x19, 0, 65535),
148 	[REG_SERIAL_NUMBER] =
149 		SBS_DATA(POWER_SUPPLY_PROP_SERIAL_NUMBER, 0x1C, 0, 65535),
150 	/* Properties of type `const char *' */
151 	[REG_MANUFACTURER] =
152 		SBS_DATA(POWER_SUPPLY_PROP_MANUFACTURER, 0x20, 0, 65535),
153 	[REG_MODEL_NAME] =
154 		SBS_DATA(POWER_SUPPLY_PROP_MODEL_NAME, 0x21, 0, 65535),
155 	[REG_CHEMISTRY] =
156 		SBS_DATA(POWER_SUPPLY_PROP_TECHNOLOGY, 0x22, 0, 65535)
157 };
158 
159 static const enum power_supply_property sbs_properties[] = {
160 	POWER_SUPPLY_PROP_STATUS,
161 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
162 	POWER_SUPPLY_PROP_HEALTH,
163 	POWER_SUPPLY_PROP_PRESENT,
164 	POWER_SUPPLY_PROP_TECHNOLOGY,
165 	POWER_SUPPLY_PROP_CYCLE_COUNT,
166 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
167 	POWER_SUPPLY_PROP_CURRENT_NOW,
168 	POWER_SUPPLY_PROP_CURRENT_AVG,
169 	POWER_SUPPLY_PROP_CAPACITY,
170 	POWER_SUPPLY_PROP_CAPACITY_ERROR_MARGIN,
171 	POWER_SUPPLY_PROP_TEMP,
172 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
173 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
174 	POWER_SUPPLY_PROP_TIME_TO_FULL_AVG,
175 	POWER_SUPPLY_PROP_SERIAL_NUMBER,
176 	POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
177 	POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
178 	POWER_SUPPLY_PROP_ENERGY_NOW,
179 	POWER_SUPPLY_PROP_ENERGY_FULL,
180 	POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
181 	POWER_SUPPLY_PROP_CHARGE_NOW,
182 	POWER_SUPPLY_PROP_CHARGE_FULL,
183 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
184 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
185 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
186 	POWER_SUPPLY_PROP_MANUFACTURE_YEAR,
187 	POWER_SUPPLY_PROP_MANUFACTURE_MONTH,
188 	POWER_SUPPLY_PROP_MANUFACTURE_DAY,
189 	/* Properties of type `const char *' */
190 	POWER_SUPPLY_PROP_MANUFACTURER,
191 	POWER_SUPPLY_PROP_MODEL_NAME
192 };
193 
194 /* Supports special manufacturer commands from TI BQ20Z65 and BQ20Z75 IC. */
195 #define SBS_FLAGS_TI_BQ20ZX5		BIT(0)
196 
197 static const enum power_supply_property string_properties[] = {
198 	POWER_SUPPLY_PROP_TECHNOLOGY,
199 	POWER_SUPPLY_PROP_MANUFACTURER,
200 	POWER_SUPPLY_PROP_MODEL_NAME,
201 };
202 
203 #define NR_STRING_BUFFERS	ARRAY_SIZE(string_properties)
204 
205 struct sbs_info {
206 	struct i2c_client		*client;
207 	struct power_supply		*power_supply;
208 	bool				is_present;
209 	struct gpio_desc		*gpio_detect;
210 	bool				charger_broadcasts;
211 	int				last_state;
212 	int				poll_time;
213 	u32				i2c_retry_count;
214 	u32				poll_retry_count;
215 	struct delayed_work		work;
216 	struct mutex			mode_lock;
217 	u32				flags;
218 	int				technology;
219 	char				strings[NR_STRING_BUFFERS][I2C_SMBUS_BLOCK_MAX + 1];
220 	char				serial[5];
221 };
222 
223 static char *sbs_get_string_buf(struct sbs_info *chip,
224 				enum power_supply_property psp)
225 {
226 	int i = 0;
227 
228 	for (i = 0; i < NR_STRING_BUFFERS; i++)
229 		if (string_properties[i] == psp)
230 			return chip->strings[i];
231 
232 	return ERR_PTR(-EINVAL);
233 }
234 
235 static void sbs_invalidate_cached_props(struct sbs_info *chip)
236 {
237 	int i = 0;
238 
239 	chip->technology = -1;
240 
241 	for (i = 0; i < NR_STRING_BUFFERS; i++)
242 		chip->strings[i][0] = 0;
243 }
244 
245 static bool force_load;
246 
247 static int sbs_read_word_data(struct i2c_client *client, u8 address);
248 static int sbs_write_word_data(struct i2c_client *client, u8 address, u16 value);
249 
250 static void sbs_disable_charger_broadcasts(struct sbs_info *chip)
251 {
252 	int val = sbs_read_word_data(chip->client, BATTERY_MODE_OFFSET);
253 	if (val < 0)
254 		goto exit;
255 
256 	val |= BATTERY_MODE_CHARGER_MASK;
257 
258 	val = sbs_write_word_data(chip->client, BATTERY_MODE_OFFSET, val);
259 
260 exit:
261 	if (val < 0)
262 		dev_err(&chip->client->dev,
263 			"Failed to disable charger broadcasting: %d\n", val);
264 	else
265 		dev_dbg(&chip->client->dev, "%s\n", __func__);
266 }
267 
268 static int sbs_update_presence(struct sbs_info *chip, bool is_present)
269 {
270 	struct i2c_client *client = chip->client;
271 	int retries = chip->i2c_retry_count;
272 	s32 ret = 0;
273 	u8 version;
274 
275 	if (chip->is_present == is_present)
276 		return 0;
277 
278 	if (!is_present) {
279 		chip->is_present = false;
280 		/* Disable PEC when no device is present */
281 		client->flags &= ~I2C_CLIENT_PEC;
282 		sbs_invalidate_cached_props(chip);
283 		return 0;
284 	}
285 
286 	/* Check if device supports packet error checking and use it */
287 	while (retries > 0) {
288 		ret = i2c_smbus_read_word_data(client, REG_ADDR_SPEC_INFO);
289 		if (ret >= 0)
290 			break;
291 
292 		/*
293 		 * Some batteries trigger the detection pin before the
294 		 * I2C bus is properly connected. This works around the
295 		 * issue.
296 		 */
297 		msleep(100);
298 
299 		retries--;
300 	}
301 
302 	if (ret < 0) {
303 		dev_dbg(&client->dev, "failed to read spec info: %d\n", ret);
304 
305 		/* fallback to old behaviour */
306 		client->flags &= ~I2C_CLIENT_PEC;
307 		chip->is_present = true;
308 
309 		return ret;
310 	}
311 
312 	version = (ret & SPEC_INFO_VERSION_MASK) >> SPEC_INFO_VERSION_SHIFT;
313 
314 	if (version == SBS_VERSION_1_1_WITH_PEC)
315 		client->flags |= I2C_CLIENT_PEC;
316 	else
317 		client->flags &= ~I2C_CLIENT_PEC;
318 
319 	if (of_device_is_compatible(client->dev.parent->of_node, "google,cros-ec-i2c-tunnel")
320 	    && client->flags & I2C_CLIENT_PEC) {
321 		dev_info(&client->dev, "Disabling PEC because of broken Cros-EC implementation\n");
322 		client->flags &= ~I2C_CLIENT_PEC;
323 	}
324 
325 	dev_dbg(&client->dev, "PEC: %s\n",
326 		str_enabled_disabled(client->flags & I2C_CLIENT_PEC));
327 
328 	if (!chip->is_present && is_present && !chip->charger_broadcasts)
329 		sbs_disable_charger_broadcasts(chip);
330 
331 	chip->is_present = true;
332 
333 	return 0;
334 }
335 
336 static int sbs_read_word_data(struct i2c_client *client, u8 address)
337 {
338 	struct sbs_info *chip = i2c_get_clientdata(client);
339 	int retries = chip->i2c_retry_count;
340 	s32 ret = 0;
341 
342 	while (retries > 0) {
343 		ret = i2c_smbus_read_word_data(client, address);
344 		if (ret >= 0)
345 			break;
346 		retries--;
347 	}
348 
349 	if (ret < 0) {
350 		dev_dbg(&client->dev,
351 			"%s: i2c read at address 0x%x failed\n",
352 			__func__, address);
353 		return ret;
354 	}
355 
356 	return ret;
357 }
358 
359 static int sbs_read_string_data_fallback(struct i2c_client *client, u8 address, char *values)
360 {
361 	struct sbs_info *chip = i2c_get_clientdata(client);
362 	s32 ret = 0, block_length = 0;
363 	int retries_length, retries_block;
364 	u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1];
365 
366 	retries_length = chip->i2c_retry_count;
367 	retries_block = chip->i2c_retry_count;
368 
369 	dev_warn_once(&client->dev, "I2C adapter does not support I2C_FUNC_SMBUS_READ_BLOCK_DATA.\n"
370 				    "Fallback method does not support PEC.\n");
371 
372 	/* Adapter needs to support these two functions */
373 	if (!i2c_check_functionality(client->adapter,
374 				     I2C_FUNC_SMBUS_BYTE_DATA |
375 				     I2C_FUNC_SMBUS_I2C_BLOCK)){
376 		return -ENODEV;
377 	}
378 
379 	/* Get the length of block data */
380 	while (retries_length > 0) {
381 		ret = i2c_smbus_read_byte_data(client, address);
382 		if (ret >= 0)
383 			break;
384 		retries_length--;
385 	}
386 
387 	if (ret < 0) {
388 		dev_dbg(&client->dev,
389 			"%s: i2c read at address 0x%x failed\n",
390 			__func__, address);
391 		return ret;
392 	}
393 
394 	/* block_length does not include NULL terminator */
395 	block_length = ret;
396 	if (block_length > I2C_SMBUS_BLOCK_MAX) {
397 		dev_err(&client->dev,
398 			"%s: Returned block_length is longer than 0x%x\n",
399 			__func__, I2C_SMBUS_BLOCK_MAX);
400 		return -EINVAL;
401 	}
402 
403 	/* Get the block data */
404 	while (retries_block > 0) {
405 		ret = i2c_smbus_read_i2c_block_data(
406 				client, address,
407 				block_length + 1, block_buffer);
408 		if (ret >= 0)
409 			break;
410 		retries_block--;
411 	}
412 
413 	if (ret < 0) {
414 		dev_dbg(&client->dev,
415 			"%s: i2c read at address 0x%x failed\n",
416 			__func__, address);
417 		return ret;
418 	}
419 
420 	/* block_buffer[0] == block_length */
421 	memcpy(values, block_buffer + 1, block_length);
422 	values[block_length] = '\0';
423 
424 	return ret;
425 }
426 
427 static int sbs_read_string_data(struct i2c_client *client, u8 address, char *values)
428 {
429 	struct sbs_info *chip = i2c_get_clientdata(client);
430 	int retries = chip->i2c_retry_count;
431 	int ret = 0;
432 
433 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_BLOCK_DATA)) {
434 		bool pec = client->flags & I2C_CLIENT_PEC;
435 		client->flags &= ~I2C_CLIENT_PEC;
436 		ret = sbs_read_string_data_fallback(client, address, values);
437 		if (pec)
438 			client->flags |= I2C_CLIENT_PEC;
439 		return ret;
440 	}
441 
442 	while (retries > 0) {
443 		ret = i2c_smbus_read_block_data(client, address, values);
444 		if (ret >= 0)
445 			break;
446 		retries--;
447 	}
448 
449 	if (ret < 0) {
450 		dev_dbg(&client->dev, "failed to read block 0x%x: %d\n", address, ret);
451 		return ret;
452 	}
453 
454 	/* add string termination */
455 	values[ret] = '\0';
456 	return ret;
457 }
458 
459 static int sbs_write_word_data(struct i2c_client *client, u8 address,
460 	u16 value)
461 {
462 	struct sbs_info *chip = i2c_get_clientdata(client);
463 	int retries = chip->i2c_retry_count;
464 	s32 ret = 0;
465 
466 	while (retries > 0) {
467 		ret = i2c_smbus_write_word_data(client, address, value);
468 		if (ret >= 0)
469 			break;
470 		retries--;
471 	}
472 
473 	if (ret < 0) {
474 		dev_dbg(&client->dev,
475 			"%s: i2c write to address 0x%x failed\n",
476 			__func__, address);
477 		return ret;
478 	}
479 
480 	return 0;
481 }
482 
483 static int sbs_status_correct(struct i2c_client *client, int *intval)
484 {
485 	int ret;
486 
487 	ret = sbs_read_word_data(client, sbs_data[REG_CURRENT_NOW].addr);
488 	if (ret < 0)
489 		return ret;
490 
491 	ret = (s16)ret;
492 
493 	/* Not drawing current -> not charging (i.e. idle) */
494 	if (*intval != POWER_SUPPLY_STATUS_FULL && ret == 0)
495 		*intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
496 
497 	if (*intval == POWER_SUPPLY_STATUS_FULL) {
498 		/* Drawing or providing current when full */
499 		if (ret > 0)
500 			*intval = POWER_SUPPLY_STATUS_CHARGING;
501 		else if (ret < 0)
502 			*intval = POWER_SUPPLY_STATUS_DISCHARGING;
503 	}
504 
505 	return 0;
506 }
507 
508 static bool sbs_bat_needs_calibration(struct i2c_client *client)
509 {
510 	int ret;
511 
512 	ret = sbs_read_word_data(client, sbs_data[REG_BATTERY_MODE].addr);
513 	if (ret < 0)
514 		return false;
515 
516 	return !!(ret & BIT(7));
517 }
518 
519 static int sbs_get_ti_battery_presence_and_health(
520 	struct i2c_client *client, enum power_supply_property psp,
521 	union power_supply_propval *val)
522 {
523 	s32 ret;
524 
525 	/*
526 	 * Write to ManufacturerAccess with ManufacturerAccess command
527 	 * and then read the status.
528 	 */
529 	ret = sbs_write_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr,
530 				  MANUFACTURER_ACCESS_STATUS);
531 	if (ret < 0) {
532 		if (psp == POWER_SUPPLY_PROP_PRESENT)
533 			val->intval = 0; /* battery removed */
534 		return ret;
535 	}
536 
537 	ret = sbs_read_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr);
538 	if (ret < 0) {
539 		if (psp == POWER_SUPPLY_PROP_PRESENT)
540 			val->intval = 0; /* battery removed */
541 		return ret;
542 	}
543 
544 	if (ret < sbs_data[REG_MANUFACTURER_DATA].min_value ||
545 	    ret > sbs_data[REG_MANUFACTURER_DATA].max_value) {
546 		val->intval = 0;
547 		return 0;
548 	}
549 
550 	/* Mask the upper nibble of 2nd byte and
551 	 * lower byte of response then
552 	 * shift the result by 8 to get status*/
553 	ret &= 0x0F00;
554 	ret >>= 8;
555 	if (psp == POWER_SUPPLY_PROP_PRESENT) {
556 		if (ret == 0x0F)
557 			/* battery removed */
558 			val->intval = 0;
559 		else
560 			val->intval = 1;
561 	} else if (psp == POWER_SUPPLY_PROP_HEALTH) {
562 		if (ret == 0x09)
563 			val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
564 		else if (ret == 0x0B)
565 			val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
566 		else if (ret == 0x0C)
567 			val->intval = POWER_SUPPLY_HEALTH_DEAD;
568 		else if (sbs_bat_needs_calibration(client))
569 			val->intval = POWER_SUPPLY_HEALTH_CALIBRATION_REQUIRED;
570 		else
571 			val->intval = POWER_SUPPLY_HEALTH_GOOD;
572 	}
573 
574 	return 0;
575 }
576 
577 static int sbs_get_battery_presence_and_health(
578 	struct i2c_client *client, enum power_supply_property psp,
579 	union power_supply_propval *val)
580 {
581 	struct sbs_info *chip = i2c_get_clientdata(client);
582 	int ret;
583 
584 	if (chip->flags & SBS_FLAGS_TI_BQ20ZX5)
585 		return sbs_get_ti_battery_presence_and_health(client, psp, val);
586 
587 	/* Dummy command; if it succeeds, battery is present. */
588 	ret = sbs_read_word_data(client, sbs_data[REG_STATUS].addr);
589 
590 	if (ret < 0) { /* battery not present*/
591 		if (psp == POWER_SUPPLY_PROP_PRESENT) {
592 			val->intval = 0;
593 			return 0;
594 		}
595 		return ret;
596 	}
597 
598 	if (psp == POWER_SUPPLY_PROP_PRESENT)
599 		val->intval = 1; /* battery present */
600 	else { /* POWER_SUPPLY_PROP_HEALTH */
601 		if (sbs_bat_needs_calibration(client)) {
602 			val->intval = POWER_SUPPLY_HEALTH_CALIBRATION_REQUIRED;
603 		} else {
604 			/* SBS spec doesn't have a general health command. */
605 			val->intval = POWER_SUPPLY_HEALTH_UNKNOWN;
606 		}
607 	}
608 
609 	return 0;
610 }
611 
612 static int sbs_get_battery_property(struct i2c_client *client,
613 	int reg_offset, enum power_supply_property psp,
614 	union power_supply_propval *val)
615 {
616 	struct sbs_info *chip = i2c_get_clientdata(client);
617 	s32 ret;
618 
619 	ret = sbs_read_word_data(client, sbs_data[reg_offset].addr);
620 	if (ret < 0)
621 		return ret;
622 
623 	/* returned values are 16 bit */
624 	if (sbs_data[reg_offset].min_value < 0)
625 		ret = (s16)ret;
626 
627 	if (ret >= sbs_data[reg_offset].min_value &&
628 	    ret <= sbs_data[reg_offset].max_value) {
629 		val->intval = ret;
630 		if (psp == POWER_SUPPLY_PROP_CAPACITY_LEVEL) {
631 			if (!(ret & BATTERY_INITIALIZED))
632 				val->intval =
633 					POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
634 			else if (ret & BATTERY_FULL_CHARGED)
635 				val->intval =
636 					POWER_SUPPLY_CAPACITY_LEVEL_FULL;
637 			else if (ret & BATTERY_FULL_DISCHARGED)
638 				val->intval =
639 					POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
640 			else
641 				val->intval =
642 					POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
643 			return 0;
644 		} else if (psp != POWER_SUPPLY_PROP_STATUS) {
645 			return 0;
646 		}
647 
648 		if (ret & BATTERY_FULL_CHARGED)
649 			val->intval = POWER_SUPPLY_STATUS_FULL;
650 		else if (ret & BATTERY_DISCHARGING)
651 			val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
652 		else
653 			val->intval = POWER_SUPPLY_STATUS_CHARGING;
654 
655 		sbs_status_correct(client, &val->intval);
656 
657 		if (chip->poll_time == 0)
658 			chip->last_state = val->intval;
659 		else if (chip->last_state != val->intval) {
660 			cancel_delayed_work_sync(&chip->work);
661 			power_supply_changed(chip->power_supply);
662 			chip->poll_time = 0;
663 		}
664 	} else {
665 		if (psp == POWER_SUPPLY_PROP_STATUS)
666 			val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
667 		else if (psp == POWER_SUPPLY_PROP_CAPACITY)
668 			/* sbs spec says that this can be >100 %
669 			 * even if max value is 100 %
670 			 */
671 			val->intval = min(ret, 100);
672 		else
673 			val->intval = 0;
674 	}
675 
676 	return 0;
677 }
678 
679 static int sbs_get_property_index(struct i2c_client *client,
680 	enum power_supply_property psp)
681 {
682 	int count;
683 
684 	for (count = 0; count < ARRAY_SIZE(sbs_data); count++)
685 		if (psp == sbs_data[count].psp)
686 			return count;
687 
688 	dev_warn(&client->dev,
689 		"%s: Invalid Property - %d\n", __func__, psp);
690 
691 	return -EINVAL;
692 }
693 
694 static const char *sbs_get_constant_string(struct sbs_info *chip,
695 			enum power_supply_property psp)
696 {
697 	int ret;
698 	char *buf;
699 	u8 addr;
700 
701 	buf = sbs_get_string_buf(chip, psp);
702 	if (IS_ERR(buf))
703 		return buf;
704 
705 	if (!buf[0]) {
706 		ret = sbs_get_property_index(chip->client, psp);
707 		if (ret < 0)
708 			return ERR_PTR(ret);
709 
710 		addr = sbs_data[ret].addr;
711 
712 		ret = sbs_read_string_data(chip->client, addr, buf);
713 		if (ret < 0)
714 			return ERR_PTR(ret);
715 	}
716 
717 	return buf;
718 }
719 
720 static void  sbs_unit_adjustment(struct i2c_client *client,
721 	enum power_supply_property psp, union power_supply_propval *val)
722 {
723 #define BASE_UNIT_CONVERSION		1000
724 #define BATTERY_MODE_CAP_MULT_WATT	(10 * BASE_UNIT_CONVERSION)
725 #define TIME_UNIT_CONVERSION		60
726 #define TEMP_KELVIN_TO_CELSIUS		2731
727 	switch (psp) {
728 	case POWER_SUPPLY_PROP_ENERGY_NOW:
729 	case POWER_SUPPLY_PROP_ENERGY_FULL:
730 	case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
731 		/* sbs provides energy in units of 10mWh.
732 		 * Convert to µWh
733 		 */
734 		val->intval *= BATTERY_MODE_CAP_MULT_WATT;
735 		break;
736 
737 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
738 	case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
739 	case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
740 	case POWER_SUPPLY_PROP_CURRENT_NOW:
741 	case POWER_SUPPLY_PROP_CURRENT_AVG:
742 	case POWER_SUPPLY_PROP_CHARGE_NOW:
743 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
744 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
745 	case POWER_SUPPLY_PROP_CHARGE_FULL:
746 	case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
747 		val->intval *= BASE_UNIT_CONVERSION;
748 		break;
749 
750 	case POWER_SUPPLY_PROP_TEMP:
751 		/* sbs provides battery temperature in 0.1K
752 		 * so convert it to 0.1°C
753 		 */
754 		val->intval -= TEMP_KELVIN_TO_CELSIUS;
755 		break;
756 
757 	case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
758 	case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
759 	case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
760 		/* sbs provides time to empty and time to full in minutes.
761 		 * Convert to seconds
762 		 */
763 		val->intval *= TIME_UNIT_CONVERSION;
764 		break;
765 
766 	default:
767 		dev_dbg(&client->dev,
768 			"%s: no need for unit conversion %d\n", __func__, psp);
769 	}
770 }
771 
772 static enum sbs_capacity_mode sbs_set_capacity_mode(struct i2c_client *client,
773 	enum sbs_capacity_mode mode)
774 {
775 	int ret, original_val;
776 
777 	original_val = sbs_read_word_data(client, BATTERY_MODE_OFFSET);
778 	if (original_val < 0)
779 		return original_val;
780 
781 	if ((original_val & BATTERY_MODE_CAPACITY_MASK) == mode)
782 		return mode;
783 
784 	if (mode == CAPACITY_MODE_AMPS)
785 		ret = original_val & ~BATTERY_MODE_CAPACITY_MASK;
786 	else
787 		ret = original_val | BATTERY_MODE_CAPACITY_MASK;
788 
789 	ret = sbs_write_word_data(client, BATTERY_MODE_OFFSET, ret);
790 	if (ret < 0)
791 		return ret;
792 
793 	usleep_range(1000, 2000);
794 
795 	return original_val & BATTERY_MODE_CAPACITY_MASK;
796 }
797 
798 static int sbs_get_battery_capacity(struct i2c_client *client,
799 	int reg_offset, enum power_supply_property psp,
800 	union power_supply_propval *val)
801 {
802 	s32 ret;
803 	enum sbs_capacity_mode mode = CAPACITY_MODE_WATTS;
804 
805 	if (power_supply_is_amp_property(psp))
806 		mode = CAPACITY_MODE_AMPS;
807 
808 	mode = sbs_set_capacity_mode(client, mode);
809 	if ((int)mode < 0)
810 		return mode;
811 
812 	ret = sbs_read_word_data(client, sbs_data[reg_offset].addr);
813 	if (ret < 0)
814 		return ret;
815 
816 	val->intval = ret;
817 
818 	ret = sbs_set_capacity_mode(client, mode);
819 	if (ret < 0)
820 		return ret;
821 
822 	return 0;
823 }
824 
825 static int sbs_get_battery_serial_number(struct i2c_client *client,
826 	union power_supply_propval *val)
827 {
828 	struct sbs_info *chip = i2c_get_clientdata(client);
829 	int ret;
830 
831 	ret = sbs_read_word_data(client, sbs_data[REG_SERIAL_NUMBER].addr);
832 	if (ret < 0)
833 		return ret;
834 
835 	sprintf(chip->serial, "%04x", (u16)ret);
836 	val->strval = chip->serial;
837 
838 	return 0;
839 }
840 
841 static int sbs_get_chemistry(struct sbs_info *chip,
842 		union power_supply_propval *val)
843 {
844 	const char *chemistry;
845 
846 	if (chip->technology != -1) {
847 		val->intval = chip->technology;
848 		return 0;
849 	}
850 
851 	chemistry = sbs_get_constant_string(chip, POWER_SUPPLY_PROP_TECHNOLOGY);
852 
853 	if (IS_ERR(chemistry))
854 		return PTR_ERR(chemistry);
855 
856 	if (!strncasecmp(chemistry, "LION", 4))
857 		chip->technology = POWER_SUPPLY_TECHNOLOGY_LION;
858 	else if (!strncasecmp(chemistry, "LiP", 3))
859 		chip->technology = POWER_SUPPLY_TECHNOLOGY_LIPO;
860 	else if (!strncasecmp(chemistry, "NiCd", 4))
861 		chip->technology = POWER_SUPPLY_TECHNOLOGY_NiCd;
862 	else if (!strncasecmp(chemistry, "NiMH", 4))
863 		chip->technology = POWER_SUPPLY_TECHNOLOGY_NiMH;
864 	else if (!strncasecmp(chemistry, "PbAc", 4))
865 		chip->technology = POWER_SUPPLY_TECHNOLOGY_PbAc;
866 	else if (!strncasecmp(chemistry, "NiZn", 4))
867 		chip->technology = POWER_SUPPLY_TECHNOLOGY_NiZn;
868 	else if (!strncasecmp(chemistry, "RAM", 3))
869 		chip->technology = POWER_SUPPLY_TECHNOLOGY_RAM;
870 	else if (!strncasecmp(chemistry, "ZnAr", 4))
871 		chip->technology = POWER_SUPPLY_TECHNOLOGY_ZnAr;
872 	else
873 		chip->technology = POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
874 
875 	if (chip->technology == POWER_SUPPLY_TECHNOLOGY_UNKNOWN)
876 		dev_warn(&chip->client->dev, "Unknown chemistry: %s\n", chemistry);
877 
878 	val->intval = chip->technology;
879 
880 	return 0;
881 }
882 
883 static int sbs_get_battery_manufacture_date(struct i2c_client *client,
884 	enum power_supply_property psp,
885 	union power_supply_propval *val)
886 {
887 	int ret;
888 	u16 day, month, year;
889 
890 	ret = sbs_read_word_data(client, REG_ADDR_MANUFACTURE_DATE);
891 	if (ret < 0)
892 		return ret;
893 
894 	day   = ret   & GENMASK(4,  0);
895 	month = (ret  & GENMASK(8,  5)) >> 5;
896 	year  = ((ret & GENMASK(15, 9)) >> 9) + 1980;
897 
898 	switch (psp) {
899 	case POWER_SUPPLY_PROP_MANUFACTURE_YEAR:
900 		val->intval = year;
901 		break;
902 	case POWER_SUPPLY_PROP_MANUFACTURE_MONTH:
903 		val->intval = month;
904 		break;
905 	case POWER_SUPPLY_PROP_MANUFACTURE_DAY:
906 		val->intval = day;
907 		break;
908 	default:
909 		return -EINVAL;
910 	}
911 
912 	return 0;
913 }
914 
915 static int sbs_get_property(struct power_supply *psy,
916 	enum power_supply_property psp,
917 	union power_supply_propval *val)
918 {
919 	int ret = 0;
920 	struct sbs_info *chip = power_supply_get_drvdata(psy);
921 	struct i2c_client *client = chip->client;
922 	const char *str;
923 
924 	if (chip->gpio_detect) {
925 		ret = gpiod_get_value_cansleep(chip->gpio_detect);
926 		if (ret < 0)
927 			return ret;
928 		if (psp == POWER_SUPPLY_PROP_PRESENT) {
929 			val->intval = ret;
930 			sbs_update_presence(chip, ret);
931 			return 0;
932 		}
933 		if (ret == 0)
934 			return -ENODATA;
935 	}
936 
937 	switch (psp) {
938 	case POWER_SUPPLY_PROP_PRESENT:
939 	case POWER_SUPPLY_PROP_HEALTH:
940 		ret = sbs_get_battery_presence_and_health(client, psp, val);
941 
942 		/* this can only be true if no gpio is used */
943 		if (psp == POWER_SUPPLY_PROP_PRESENT)
944 			return 0;
945 		break;
946 
947 	case POWER_SUPPLY_PROP_TECHNOLOGY:
948 		ret = sbs_get_chemistry(chip, val);
949 		if (ret < 0)
950 			break;
951 
952 		goto done; /* don't trigger power_supply_changed()! */
953 
954 	case POWER_SUPPLY_PROP_ENERGY_NOW:
955 	case POWER_SUPPLY_PROP_ENERGY_FULL:
956 	case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
957 	case POWER_SUPPLY_PROP_CHARGE_NOW:
958 	case POWER_SUPPLY_PROP_CHARGE_FULL:
959 	case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
960 		ret = sbs_get_property_index(client, psp);
961 		if (ret < 0)
962 			break;
963 
964 		/* sbs_get_battery_capacity() will change the battery mode
965 		 * temporarily to read the requested attribute. Ensure we stay
966 		 * in the desired mode for the duration of the attribute read.
967 		 */
968 		mutex_lock(&chip->mode_lock);
969 		ret = sbs_get_battery_capacity(client, ret, psp, val);
970 		mutex_unlock(&chip->mode_lock);
971 		break;
972 
973 	case POWER_SUPPLY_PROP_SERIAL_NUMBER:
974 		ret = sbs_get_battery_serial_number(client, val);
975 		break;
976 
977 	case POWER_SUPPLY_PROP_STATUS:
978 	case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
979 	case POWER_SUPPLY_PROP_CYCLE_COUNT:
980 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
981 	case POWER_SUPPLY_PROP_CURRENT_NOW:
982 	case POWER_SUPPLY_PROP_CURRENT_AVG:
983 	case POWER_SUPPLY_PROP_TEMP:
984 	case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
985 	case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
986 	case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
987 	case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
988 	case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
989 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
990 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
991 	case POWER_SUPPLY_PROP_CAPACITY:
992 	case POWER_SUPPLY_PROP_CAPACITY_ERROR_MARGIN:
993 		ret = sbs_get_property_index(client, psp);
994 		if (ret < 0)
995 			break;
996 
997 		ret = sbs_get_battery_property(client, ret, psp, val);
998 		break;
999 
1000 	case POWER_SUPPLY_PROP_MODEL_NAME:
1001 	case POWER_SUPPLY_PROP_MANUFACTURER:
1002 		str = sbs_get_constant_string(chip, psp);
1003 		if (IS_ERR(str))
1004 			ret = PTR_ERR(str);
1005 		else
1006 			val->strval = str;
1007 		break;
1008 
1009 	case POWER_SUPPLY_PROP_MANUFACTURE_YEAR:
1010 	case POWER_SUPPLY_PROP_MANUFACTURE_MONTH:
1011 	case POWER_SUPPLY_PROP_MANUFACTURE_DAY:
1012 		ret = sbs_get_battery_manufacture_date(client, psp, val);
1013 		break;
1014 
1015 	default:
1016 		dev_err(&client->dev,
1017 			"%s: INVALID property\n", __func__);
1018 		return -EINVAL;
1019 	}
1020 
1021 	if (!chip->gpio_detect && chip->is_present != (ret >= 0)) {
1022 		bool old_present = chip->is_present;
1023 		union power_supply_propval val;
1024 		int err = sbs_get_battery_presence_and_health(
1025 				client, POWER_SUPPLY_PROP_PRESENT, &val);
1026 
1027 		sbs_update_presence(chip, !err && val.intval);
1028 
1029 		if (old_present != chip->is_present)
1030 			power_supply_changed(chip->power_supply);
1031 	}
1032 
1033 done:
1034 	if (!ret) {
1035 		/* Convert units to match requirements for power supply class */
1036 		sbs_unit_adjustment(client, psp, val);
1037 		dev_dbg(&client->dev,
1038 			"%s: property = %d, value = %x\n", __func__,
1039 			psp, val->intval);
1040 	} else if (!chip->is_present)  {
1041 		/* battery not present, so return NODATA for properties */
1042 		ret = -ENODATA;
1043 	}
1044 	return ret;
1045 }
1046 
1047 static void sbs_supply_changed(struct sbs_info *chip)
1048 {
1049 	struct power_supply *battery = chip->power_supply;
1050 	int ret;
1051 
1052 	ret = gpiod_get_value_cansleep(chip->gpio_detect);
1053 	if (ret < 0)
1054 		return;
1055 	sbs_update_presence(chip, ret);
1056 	power_supply_changed(battery);
1057 }
1058 
1059 static irqreturn_t sbs_irq(int irq, void *devid)
1060 {
1061 	sbs_supply_changed(devid);
1062 	return IRQ_HANDLED;
1063 }
1064 
1065 static void sbs_alert(struct i2c_client *client, enum i2c_alert_protocol prot,
1066 	unsigned int data)
1067 {
1068 	sbs_supply_changed(i2c_get_clientdata(client));
1069 }
1070 
1071 static void sbs_external_power_changed(struct power_supply *psy)
1072 {
1073 	struct sbs_info *chip = power_supply_get_drvdata(psy);
1074 
1075 	/* cancel outstanding work */
1076 	cancel_delayed_work_sync(&chip->work);
1077 
1078 	schedule_delayed_work(&chip->work, HZ);
1079 	chip->poll_time = chip->poll_retry_count;
1080 }
1081 
1082 static void sbs_delayed_work(struct work_struct *work)
1083 {
1084 	struct sbs_info *chip;
1085 	s32 ret;
1086 
1087 	chip = container_of(work, struct sbs_info, work.work);
1088 
1089 	ret = sbs_read_word_data(chip->client, sbs_data[REG_STATUS].addr);
1090 	/* if the read failed, give up on this work */
1091 	if (ret < 0) {
1092 		chip->poll_time = 0;
1093 		return;
1094 	}
1095 
1096 	if (ret & BATTERY_FULL_CHARGED)
1097 		ret = POWER_SUPPLY_STATUS_FULL;
1098 	else if (ret & BATTERY_DISCHARGING)
1099 		ret = POWER_SUPPLY_STATUS_DISCHARGING;
1100 	else
1101 		ret = POWER_SUPPLY_STATUS_CHARGING;
1102 
1103 	sbs_status_correct(chip->client, &ret);
1104 
1105 	if (chip->last_state != ret) {
1106 		chip->poll_time = 0;
1107 		power_supply_changed(chip->power_supply);
1108 		return;
1109 	}
1110 	if (chip->poll_time > 0) {
1111 		schedule_delayed_work(&chip->work, HZ);
1112 		chip->poll_time--;
1113 		return;
1114 	}
1115 }
1116 
1117 static const struct power_supply_desc sbs_default_desc = {
1118 	.type = POWER_SUPPLY_TYPE_BATTERY,
1119 	.properties = sbs_properties,
1120 	.num_properties = ARRAY_SIZE(sbs_properties),
1121 	.get_property = sbs_get_property,
1122 	.external_power_changed = sbs_external_power_changed,
1123 };
1124 
1125 static int sbs_probe(struct i2c_client *client)
1126 {
1127 	struct sbs_info *chip;
1128 	struct power_supply_desc *sbs_desc;
1129 	struct sbs_platform_data *pdata = client->dev.platform_data;
1130 	struct power_supply_config psy_cfg = {};
1131 	int rc;
1132 	int irq;
1133 
1134 	sbs_desc = devm_kmemdup(&client->dev, &sbs_default_desc,
1135 			sizeof(*sbs_desc), GFP_KERNEL);
1136 	if (!sbs_desc)
1137 		return -ENOMEM;
1138 
1139 	sbs_desc->name = devm_kasprintf(&client->dev, GFP_KERNEL, "sbs-%s",
1140 			dev_name(&client->dev));
1141 	if (!sbs_desc->name)
1142 		return -ENOMEM;
1143 
1144 	chip = devm_kzalloc(&client->dev, sizeof(struct sbs_info), GFP_KERNEL);
1145 	if (!chip)
1146 		return -ENOMEM;
1147 
1148 	chip->flags = (uintptr_t)i2c_get_match_data(client);
1149 	chip->client = client;
1150 	psy_cfg.fwnode = dev_fwnode(&client->dev);
1151 	psy_cfg.drv_data = chip;
1152 	chip->last_state = POWER_SUPPLY_STATUS_UNKNOWN;
1153 	sbs_invalidate_cached_props(chip);
1154 	mutex_init(&chip->mode_lock);
1155 
1156 	/* use pdata if available, fall back to DT properties,
1157 	 * or hardcoded defaults if not
1158 	 */
1159 	rc = device_property_read_u32(&client->dev, "sbs,i2c-retry-count",
1160 				      &chip->i2c_retry_count);
1161 	if (rc)
1162 		chip->i2c_retry_count = 0;
1163 
1164 	rc = device_property_read_u32(&client->dev, "sbs,poll-retry-count",
1165 				      &chip->poll_retry_count);
1166 	if (rc)
1167 		chip->poll_retry_count = 0;
1168 
1169 	if (pdata) {
1170 		chip->poll_retry_count = pdata->poll_retry_count;
1171 		chip->i2c_retry_count  = pdata->i2c_retry_count;
1172 	}
1173 	chip->i2c_retry_count = chip->i2c_retry_count + 1;
1174 
1175 	chip->charger_broadcasts = !device_property_read_bool(&client->dev,
1176 					"sbs,disable-charger-broadcasts");
1177 
1178 	chip->gpio_detect = devm_gpiod_get_optional(&client->dev,
1179 			"sbs,battery-detect", GPIOD_IN);
1180 	if (IS_ERR(chip->gpio_detect))
1181 		return dev_err_probe(&client->dev, PTR_ERR(chip->gpio_detect),
1182 				     "Failed to get gpio\n");
1183 
1184 	i2c_set_clientdata(client, chip);
1185 
1186 	/*
1187 	 * Before we register, we might need to make sure we can actually talk
1188 	 * to the battery.
1189 	 */
1190 	if (!(force_load || chip->gpio_detect)) {
1191 		union power_supply_propval val;
1192 
1193 		rc = sbs_get_battery_presence_and_health(
1194 				client, POWER_SUPPLY_PROP_PRESENT, &val);
1195 		if (rc < 0 || !val.intval)
1196 			return dev_err_probe(&client->dev, -ENODEV,
1197 					     "Failed to get present status\n");
1198 	}
1199 
1200 	rc = devm_delayed_work_autocancel(&client->dev, &chip->work,
1201 					  sbs_delayed_work);
1202 	if (rc)
1203 		return rc;
1204 
1205 	chip->power_supply = devm_power_supply_register(&client->dev, sbs_desc,
1206 						   &psy_cfg);
1207 	if (IS_ERR(chip->power_supply))
1208 		return dev_err_probe(&client->dev, PTR_ERR(chip->power_supply),
1209 				     "Failed to register power supply\n");
1210 
1211 	if (!chip->gpio_detect)
1212 		goto out;
1213 
1214 	irq = gpiod_to_irq(chip->gpio_detect);
1215 	if (irq <= 0) {
1216 		dev_warn(&client->dev, "Failed to get gpio as irq: %d\n", irq);
1217 		goto out;
1218 	}
1219 
1220 	rc = devm_request_threaded_irq(&client->dev, irq, NULL, sbs_irq,
1221 		IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
1222 		dev_name(&client->dev), chip);
1223 	if (rc) {
1224 		dev_warn(&client->dev, "Failed to request irq: %d\n", rc);
1225 		goto out;
1226 	}
1227 
1228 out:
1229 	dev_info(&client->dev,
1230 		"%s: battery gas gauge device registered\n", client->name);
1231 
1232 	return 0;
1233 }
1234 
1235 #if defined CONFIG_PM_SLEEP
1236 
1237 static int sbs_suspend(struct device *dev)
1238 {
1239 	struct i2c_client *client = to_i2c_client(dev);
1240 	struct sbs_info *chip = i2c_get_clientdata(client);
1241 	int ret;
1242 
1243 	if (chip->poll_time > 0)
1244 		cancel_delayed_work_sync(&chip->work);
1245 
1246 	if (chip->flags & SBS_FLAGS_TI_BQ20ZX5) {
1247 		/* Write to manufacturer access with sleep command. */
1248 		ret = sbs_write_word_data(client,
1249 					  sbs_data[REG_MANUFACTURER_DATA].addr,
1250 					  MANUFACTURER_ACCESS_SLEEP);
1251 		if (chip->is_present && ret < 0)
1252 			return ret;
1253 	}
1254 
1255 	return 0;
1256 }
1257 
1258 static SIMPLE_DEV_PM_OPS(sbs_pm_ops, sbs_suspend, NULL);
1259 #define SBS_PM_OPS (&sbs_pm_ops)
1260 
1261 #else
1262 #define SBS_PM_OPS NULL
1263 #endif
1264 
1265 static const struct i2c_device_id sbs_id[] = {
1266 	{ .name = "bq20z65", .driver_data = SBS_FLAGS_TI_BQ20ZX5 },
1267 	{ .name = "bq20z75", .driver_data = SBS_FLAGS_TI_BQ20ZX5 },
1268 	{ .name = "sbs-battery", .driver_data = 0 },
1269 	{ }
1270 };
1271 MODULE_DEVICE_TABLE(i2c, sbs_id);
1272 
1273 static const struct of_device_id sbs_dt_ids[] = {
1274 	{ .compatible = "sbs,sbs-battery" },
1275 	{
1276 		.compatible = "ti,bq20z65",
1277 		.data = (void *)SBS_FLAGS_TI_BQ20ZX5,
1278 	},
1279 	{
1280 		.compatible = "ti,bq20z75",
1281 		.data = (void *)SBS_FLAGS_TI_BQ20ZX5,
1282 	},
1283 	{ }
1284 };
1285 MODULE_DEVICE_TABLE(of, sbs_dt_ids);
1286 
1287 static struct i2c_driver sbs_battery_driver = {
1288 	.probe		= sbs_probe,
1289 	.alert		= sbs_alert,
1290 	.id_table	= sbs_id,
1291 	.driver = {
1292 		.name	= "sbs-battery",
1293 		.of_match_table = sbs_dt_ids,
1294 		.pm	= SBS_PM_OPS,
1295 	},
1296 };
1297 module_i2c_driver(sbs_battery_driver);
1298 
1299 MODULE_DESCRIPTION("SBS battery monitor driver");
1300 MODULE_LICENSE("GPL");
1301 
1302 module_param(force_load, bool, 0444);
1303 MODULE_PARM_DESC(force_load,
1304 		 "Attempt to load the driver even if no battery is connected");
1305