xref: /linux/drivers/power/supply/sbs-battery.c (revision e2be04c7f9958dde770eeb8b30e829ca969b37bb)
1 /*
2  * Gas Gauge driver for SBS Compliant Batteries
3  *
4  * Copyright (c) 2010, NVIDIA Corporation.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14  * more details.
15  */
16 
17 #include <linux/delay.h>
18 #include <linux/err.h>
19 #include <linux/gpio/consumer.h>
20 #include <linux/i2c.h>
21 #include <linux/init.h>
22 #include <linux/interrupt.h>
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/of.h>
26 #include <linux/power/sbs-battery.h>
27 #include <linux/power_supply.h>
28 #include <linux/slab.h>
29 #include <linux/stat.h>
30 
31 enum {
32 	REG_MANUFACTURER_DATA,
33 	REG_TEMPERATURE,
34 	REG_VOLTAGE,
35 	REG_CURRENT,
36 	REG_CAPACITY,
37 	REG_TIME_TO_EMPTY,
38 	REG_TIME_TO_FULL,
39 	REG_STATUS,
40 	REG_CAPACITY_LEVEL,
41 	REG_CYCLE_COUNT,
42 	REG_SERIAL_NUMBER,
43 	REG_REMAINING_CAPACITY,
44 	REG_REMAINING_CAPACITY_CHARGE,
45 	REG_FULL_CHARGE_CAPACITY,
46 	REG_FULL_CHARGE_CAPACITY_CHARGE,
47 	REG_DESIGN_CAPACITY,
48 	REG_DESIGN_CAPACITY_CHARGE,
49 	REG_DESIGN_VOLTAGE_MIN,
50 	REG_DESIGN_VOLTAGE_MAX,
51 	REG_MANUFACTURER,
52 	REG_MODEL_NAME,
53 };
54 
55 /* Battery Mode defines */
56 #define BATTERY_MODE_OFFSET		0x03
57 #define BATTERY_MODE_MASK		0x8000
58 enum sbs_battery_mode {
59 	BATTERY_MODE_AMPS = 0,
60 	BATTERY_MODE_WATTS = 0x8000
61 };
62 
63 /* manufacturer access defines */
64 #define MANUFACTURER_ACCESS_STATUS	0x0006
65 #define MANUFACTURER_ACCESS_SLEEP	0x0011
66 
67 /* battery status value bits */
68 #define BATTERY_INITIALIZED		0x80
69 #define BATTERY_DISCHARGING		0x40
70 #define BATTERY_FULL_CHARGED		0x20
71 #define BATTERY_FULL_DISCHARGED		0x10
72 
73 /* min_value and max_value are only valid for numerical data */
74 #define SBS_DATA(_psp, _addr, _min_value, _max_value) { \
75 	.psp = _psp, \
76 	.addr = _addr, \
77 	.min_value = _min_value, \
78 	.max_value = _max_value, \
79 }
80 
81 static const struct chip_data {
82 	enum power_supply_property psp;
83 	u8 addr;
84 	int min_value;
85 	int max_value;
86 } sbs_data[] = {
87 	[REG_MANUFACTURER_DATA] =
88 		SBS_DATA(POWER_SUPPLY_PROP_PRESENT, 0x00, 0, 65535),
89 	[REG_TEMPERATURE] =
90 		SBS_DATA(POWER_SUPPLY_PROP_TEMP, 0x08, 0, 65535),
91 	[REG_VOLTAGE] =
92 		SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_NOW, 0x09, 0, 20000),
93 	[REG_CURRENT] =
94 		SBS_DATA(POWER_SUPPLY_PROP_CURRENT_NOW, 0x0A, -32768, 32767),
95 	[REG_CAPACITY] =
96 		SBS_DATA(POWER_SUPPLY_PROP_CAPACITY, 0x0D, 0, 100),
97 	[REG_REMAINING_CAPACITY] =
98 		SBS_DATA(POWER_SUPPLY_PROP_ENERGY_NOW, 0x0F, 0, 65535),
99 	[REG_REMAINING_CAPACITY_CHARGE] =
100 		SBS_DATA(POWER_SUPPLY_PROP_CHARGE_NOW, 0x0F, 0, 65535),
101 	[REG_FULL_CHARGE_CAPACITY] =
102 		SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL, 0x10, 0, 65535),
103 	[REG_FULL_CHARGE_CAPACITY_CHARGE] =
104 		SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL, 0x10, 0, 65535),
105 	[REG_TIME_TO_EMPTY] =
106 		SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, 0x12, 0, 65535),
107 	[REG_TIME_TO_FULL] =
108 		SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_FULL_AVG, 0x13, 0, 65535),
109 	[REG_STATUS] =
110 		SBS_DATA(POWER_SUPPLY_PROP_STATUS, 0x16, 0, 65535),
111 	[REG_CAPACITY_LEVEL] =
112 		SBS_DATA(POWER_SUPPLY_PROP_CAPACITY_LEVEL, 0x16, 0, 65535),
113 	[REG_CYCLE_COUNT] =
114 		SBS_DATA(POWER_SUPPLY_PROP_CYCLE_COUNT, 0x17, 0, 65535),
115 	[REG_DESIGN_CAPACITY] =
116 		SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, 0x18, 0, 65535),
117 	[REG_DESIGN_CAPACITY_CHARGE] =
118 		SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 0x18, 0, 65535),
119 	[REG_DESIGN_VOLTAGE_MIN] =
120 		SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, 0x19, 0, 65535),
121 	[REG_DESIGN_VOLTAGE_MAX] =
122 		SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, 0x19, 0, 65535),
123 	[REG_SERIAL_NUMBER] =
124 		SBS_DATA(POWER_SUPPLY_PROP_SERIAL_NUMBER, 0x1C, 0, 65535),
125 	/* Properties of type `const char *' */
126 	[REG_MANUFACTURER] =
127 		SBS_DATA(POWER_SUPPLY_PROP_MANUFACTURER, 0x20, 0, 65535),
128 	[REG_MODEL_NAME] =
129 		SBS_DATA(POWER_SUPPLY_PROP_MODEL_NAME, 0x21, 0, 65535)
130 };
131 
132 static enum power_supply_property sbs_properties[] = {
133 	POWER_SUPPLY_PROP_STATUS,
134 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
135 	POWER_SUPPLY_PROP_HEALTH,
136 	POWER_SUPPLY_PROP_PRESENT,
137 	POWER_SUPPLY_PROP_TECHNOLOGY,
138 	POWER_SUPPLY_PROP_CYCLE_COUNT,
139 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
140 	POWER_SUPPLY_PROP_CURRENT_NOW,
141 	POWER_SUPPLY_PROP_CAPACITY,
142 	POWER_SUPPLY_PROP_TEMP,
143 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
144 	POWER_SUPPLY_PROP_TIME_TO_FULL_AVG,
145 	POWER_SUPPLY_PROP_SERIAL_NUMBER,
146 	POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
147 	POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
148 	POWER_SUPPLY_PROP_ENERGY_NOW,
149 	POWER_SUPPLY_PROP_ENERGY_FULL,
150 	POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
151 	POWER_SUPPLY_PROP_CHARGE_NOW,
152 	POWER_SUPPLY_PROP_CHARGE_FULL,
153 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
154 	/* Properties of type `const char *' */
155 	POWER_SUPPLY_PROP_MANUFACTURER,
156 	POWER_SUPPLY_PROP_MODEL_NAME
157 };
158 
159 struct sbs_info {
160 	struct i2c_client		*client;
161 	struct power_supply		*power_supply;
162 	bool				is_present;
163 	struct gpio_desc		*gpio_detect;
164 	bool				enable_detection;
165 	int				last_state;
166 	int				poll_time;
167 	u32				i2c_retry_count;
168 	u32				poll_retry_count;
169 	struct delayed_work		work;
170 	struct mutex			mode_lock;
171 };
172 
173 static char model_name[I2C_SMBUS_BLOCK_MAX + 1];
174 static char manufacturer[I2C_SMBUS_BLOCK_MAX + 1];
175 static bool force_load;
176 
177 static int sbs_read_word_data(struct i2c_client *client, u8 address)
178 {
179 	struct sbs_info *chip = i2c_get_clientdata(client);
180 	s32 ret = 0;
181 	int retries = 1;
182 
183 	retries = chip->i2c_retry_count;
184 
185 	while (retries > 0) {
186 		ret = i2c_smbus_read_word_data(client, address);
187 		if (ret >= 0)
188 			break;
189 		retries--;
190 	}
191 
192 	if (ret < 0) {
193 		dev_dbg(&client->dev,
194 			"%s: i2c read at address 0x%x failed\n",
195 			__func__, address);
196 		return ret;
197 	}
198 
199 	return ret;
200 }
201 
202 static int sbs_read_string_data(struct i2c_client *client, u8 address,
203 				char *values)
204 {
205 	struct sbs_info *chip = i2c_get_clientdata(client);
206 	s32 ret = 0, block_length = 0;
207 	int retries_length = 1, retries_block = 1;
208 	u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1];
209 
210 	retries_length = chip->i2c_retry_count;
211 	retries_block = chip->i2c_retry_count;
212 
213 	/* Adapter needs to support these two functions */
214 	if (!i2c_check_functionality(client->adapter,
215 				     I2C_FUNC_SMBUS_BYTE_DATA |
216 				     I2C_FUNC_SMBUS_I2C_BLOCK)){
217 		return -ENODEV;
218 	}
219 
220 	/* Get the length of block data */
221 	while (retries_length > 0) {
222 		ret = i2c_smbus_read_byte_data(client, address);
223 		if (ret >= 0)
224 			break;
225 		retries_length--;
226 	}
227 
228 	if (ret < 0) {
229 		dev_dbg(&client->dev,
230 			"%s: i2c read at address 0x%x failed\n",
231 			__func__, address);
232 		return ret;
233 	}
234 
235 	/* block_length does not include NULL terminator */
236 	block_length = ret;
237 	if (block_length > I2C_SMBUS_BLOCK_MAX) {
238 		dev_err(&client->dev,
239 			"%s: Returned block_length is longer than 0x%x\n",
240 			__func__, I2C_SMBUS_BLOCK_MAX);
241 		return -EINVAL;
242 	}
243 
244 	/* Get the block data */
245 	while (retries_block > 0) {
246 		ret = i2c_smbus_read_i2c_block_data(
247 				client, address,
248 				block_length + 1, block_buffer);
249 		if (ret >= 0)
250 			break;
251 		retries_block--;
252 	}
253 
254 	if (ret < 0) {
255 		dev_dbg(&client->dev,
256 			"%s: i2c read at address 0x%x failed\n",
257 			__func__, address);
258 		return ret;
259 	}
260 
261 	/* block_buffer[0] == block_length */
262 	memcpy(values, block_buffer + 1, block_length);
263 	values[block_length] = '\0';
264 
265 	return ret;
266 }
267 
268 static int sbs_write_word_data(struct i2c_client *client, u8 address,
269 	u16 value)
270 {
271 	struct sbs_info *chip = i2c_get_clientdata(client);
272 	s32 ret = 0;
273 	int retries = 1;
274 
275 	retries = chip->i2c_retry_count;
276 
277 	while (retries > 0) {
278 		ret = i2c_smbus_write_word_data(client, address, value);
279 		if (ret >= 0)
280 			break;
281 		retries--;
282 	}
283 
284 	if (ret < 0) {
285 		dev_dbg(&client->dev,
286 			"%s: i2c write to address 0x%x failed\n",
287 			__func__, address);
288 		return ret;
289 	}
290 
291 	return 0;
292 }
293 
294 static int sbs_status_correct(struct i2c_client *client, int *intval)
295 {
296 	int ret;
297 
298 	ret = sbs_read_word_data(client, sbs_data[REG_CURRENT].addr);
299 	if (ret < 0)
300 		return ret;
301 
302 	ret = (s16)ret;
303 
304 	/* Not drawing current means full (cannot be not charging) */
305 	if (ret == 0)
306 		*intval = POWER_SUPPLY_STATUS_FULL;
307 
308 	if (*intval == POWER_SUPPLY_STATUS_FULL) {
309 		/* Drawing or providing current when full */
310 		if (ret > 0)
311 			*intval = POWER_SUPPLY_STATUS_CHARGING;
312 		else if (ret < 0)
313 			*intval = POWER_SUPPLY_STATUS_DISCHARGING;
314 	}
315 
316 	return 0;
317 }
318 
319 static int sbs_get_battery_presence_and_health(
320 	struct i2c_client *client, enum power_supply_property psp,
321 	union power_supply_propval *val)
322 {
323 	s32 ret;
324 	struct sbs_info *chip = i2c_get_clientdata(client);
325 
326 	if (psp == POWER_SUPPLY_PROP_PRESENT && chip->gpio_detect) {
327 		ret = gpiod_get_value_cansleep(chip->gpio_detect);
328 		if (ret < 0)
329 			return ret;
330 		val->intval = ret;
331 		chip->is_present = val->intval;
332 		return ret;
333 	}
334 
335 	/*
336 	 * Write to ManufacturerAccess with ManufacturerAccess command
337 	 * and then read the status. Do not check for error on the write
338 	 * since not all batteries implement write access to this command,
339 	 * while others mandate it.
340 	 */
341 	sbs_write_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr,
342 			    MANUFACTURER_ACCESS_STATUS);
343 
344 	ret = sbs_read_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr);
345 	if (ret < 0) {
346 		if (psp == POWER_SUPPLY_PROP_PRESENT)
347 			val->intval = 0; /* battery removed */
348 		return ret;
349 	}
350 
351 	if (ret < sbs_data[REG_MANUFACTURER_DATA].min_value ||
352 	    ret > sbs_data[REG_MANUFACTURER_DATA].max_value) {
353 		val->intval = 0;
354 		return 0;
355 	}
356 
357 	/* Mask the upper nibble of 2nd byte and
358 	 * lower byte of response then
359 	 * shift the result by 8 to get status*/
360 	ret &= 0x0F00;
361 	ret >>= 8;
362 	if (psp == POWER_SUPPLY_PROP_PRESENT) {
363 		if (ret == 0x0F)
364 			/* battery removed */
365 			val->intval = 0;
366 		else
367 			val->intval = 1;
368 	} else if (psp == POWER_SUPPLY_PROP_HEALTH) {
369 		if (ret == 0x09)
370 			val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
371 		else if (ret == 0x0B)
372 			val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
373 		else if (ret == 0x0C)
374 			val->intval = POWER_SUPPLY_HEALTH_DEAD;
375 		else
376 			val->intval = POWER_SUPPLY_HEALTH_GOOD;
377 	}
378 
379 	return 0;
380 }
381 
382 static int sbs_get_battery_property(struct i2c_client *client,
383 	int reg_offset, enum power_supply_property psp,
384 	union power_supply_propval *val)
385 {
386 	struct sbs_info *chip = i2c_get_clientdata(client);
387 	s32 ret;
388 
389 	ret = sbs_read_word_data(client, sbs_data[reg_offset].addr);
390 	if (ret < 0)
391 		return ret;
392 
393 	/* returned values are 16 bit */
394 	if (sbs_data[reg_offset].min_value < 0)
395 		ret = (s16)ret;
396 
397 	if (ret >= sbs_data[reg_offset].min_value &&
398 	    ret <= sbs_data[reg_offset].max_value) {
399 		val->intval = ret;
400 		if (psp == POWER_SUPPLY_PROP_CAPACITY_LEVEL) {
401 			if (!(ret & BATTERY_INITIALIZED))
402 				val->intval =
403 					POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
404 			else if (ret & BATTERY_FULL_CHARGED)
405 				val->intval =
406 					POWER_SUPPLY_CAPACITY_LEVEL_FULL;
407 			else if (ret & BATTERY_FULL_DISCHARGED)
408 				val->intval =
409 					POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
410 			else
411 				val->intval =
412 					POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
413 			return 0;
414 		} else if (psp != POWER_SUPPLY_PROP_STATUS) {
415 			return 0;
416 		}
417 
418 		if (ret & BATTERY_FULL_CHARGED)
419 			val->intval = POWER_SUPPLY_STATUS_FULL;
420 		else if (ret & BATTERY_DISCHARGING)
421 			val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
422 		else
423 			val->intval = POWER_SUPPLY_STATUS_CHARGING;
424 
425 		sbs_status_correct(client, &val->intval);
426 
427 		if (chip->poll_time == 0)
428 			chip->last_state = val->intval;
429 		else if (chip->last_state != val->intval) {
430 			cancel_delayed_work_sync(&chip->work);
431 			power_supply_changed(chip->power_supply);
432 			chip->poll_time = 0;
433 		}
434 	} else {
435 		if (psp == POWER_SUPPLY_PROP_STATUS)
436 			val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
437 		else if (psp == POWER_SUPPLY_PROP_CAPACITY)
438 			/* sbs spec says that this can be >100 %
439 			 * even if max value is 100 %
440 			 */
441 			val->intval = min(ret, 100);
442 		else
443 			val->intval = 0;
444 	}
445 
446 	return 0;
447 }
448 
449 static int sbs_get_battery_string_property(struct i2c_client *client,
450 	int reg_offset, enum power_supply_property psp, char *val)
451 {
452 	s32 ret;
453 
454 	ret = sbs_read_string_data(client, sbs_data[reg_offset].addr, val);
455 
456 	if (ret < 0)
457 		return ret;
458 
459 	return 0;
460 }
461 
462 static void  sbs_unit_adjustment(struct i2c_client *client,
463 	enum power_supply_property psp, union power_supply_propval *val)
464 {
465 #define BASE_UNIT_CONVERSION		1000
466 #define BATTERY_MODE_CAP_MULT_WATT	(10 * BASE_UNIT_CONVERSION)
467 #define TIME_UNIT_CONVERSION		60
468 #define TEMP_KELVIN_TO_CELSIUS		2731
469 	switch (psp) {
470 	case POWER_SUPPLY_PROP_ENERGY_NOW:
471 	case POWER_SUPPLY_PROP_ENERGY_FULL:
472 	case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
473 		/* sbs provides energy in units of 10mWh.
474 		 * Convert to µWh
475 		 */
476 		val->intval *= BATTERY_MODE_CAP_MULT_WATT;
477 		break;
478 
479 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
480 	case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
481 	case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
482 	case POWER_SUPPLY_PROP_CURRENT_NOW:
483 	case POWER_SUPPLY_PROP_CHARGE_NOW:
484 	case POWER_SUPPLY_PROP_CHARGE_FULL:
485 	case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
486 		val->intval *= BASE_UNIT_CONVERSION;
487 		break;
488 
489 	case POWER_SUPPLY_PROP_TEMP:
490 		/* sbs provides battery temperature in 0.1K
491 		 * so convert it to 0.1°C
492 		 */
493 		val->intval -= TEMP_KELVIN_TO_CELSIUS;
494 		break;
495 
496 	case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
497 	case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
498 		/* sbs provides time to empty and time to full in minutes.
499 		 * Convert to seconds
500 		 */
501 		val->intval *= TIME_UNIT_CONVERSION;
502 		break;
503 
504 	default:
505 		dev_dbg(&client->dev,
506 			"%s: no need for unit conversion %d\n", __func__, psp);
507 	}
508 }
509 
510 static enum sbs_battery_mode sbs_set_battery_mode(struct i2c_client *client,
511 	enum sbs_battery_mode mode)
512 {
513 	int ret, original_val;
514 
515 	original_val = sbs_read_word_data(client, BATTERY_MODE_OFFSET);
516 	if (original_val < 0)
517 		return original_val;
518 
519 	if ((original_val & BATTERY_MODE_MASK) == mode)
520 		return mode;
521 
522 	if (mode == BATTERY_MODE_AMPS)
523 		ret = original_val & ~BATTERY_MODE_MASK;
524 	else
525 		ret = original_val | BATTERY_MODE_MASK;
526 
527 	ret = sbs_write_word_data(client, BATTERY_MODE_OFFSET, ret);
528 	if (ret < 0)
529 		return ret;
530 
531 	usleep_range(1000, 2000);
532 
533 	return original_val & BATTERY_MODE_MASK;
534 }
535 
536 static int sbs_get_battery_capacity(struct i2c_client *client,
537 	int reg_offset, enum power_supply_property psp,
538 	union power_supply_propval *val)
539 {
540 	s32 ret;
541 	enum sbs_battery_mode mode = BATTERY_MODE_WATTS;
542 
543 	if (power_supply_is_amp_property(psp))
544 		mode = BATTERY_MODE_AMPS;
545 
546 	mode = sbs_set_battery_mode(client, mode);
547 	if (mode < 0)
548 		return mode;
549 
550 	ret = sbs_read_word_data(client, sbs_data[reg_offset].addr);
551 	if (ret < 0)
552 		return ret;
553 
554 	val->intval = ret;
555 
556 	ret = sbs_set_battery_mode(client, mode);
557 	if (ret < 0)
558 		return ret;
559 
560 	return 0;
561 }
562 
563 static char sbs_serial[5];
564 static int sbs_get_battery_serial_number(struct i2c_client *client,
565 	union power_supply_propval *val)
566 {
567 	int ret;
568 
569 	ret = sbs_read_word_data(client, sbs_data[REG_SERIAL_NUMBER].addr);
570 	if (ret < 0)
571 		return ret;
572 
573 	ret = sprintf(sbs_serial, "%04x", ret);
574 	val->strval = sbs_serial;
575 
576 	return 0;
577 }
578 
579 static int sbs_get_property_index(struct i2c_client *client,
580 	enum power_supply_property psp)
581 {
582 	int count;
583 	for (count = 0; count < ARRAY_SIZE(sbs_data); count++)
584 		if (psp == sbs_data[count].psp)
585 			return count;
586 
587 	dev_warn(&client->dev,
588 		"%s: Invalid Property - %d\n", __func__, psp);
589 
590 	return -EINVAL;
591 }
592 
593 static int sbs_get_property(struct power_supply *psy,
594 	enum power_supply_property psp,
595 	union power_supply_propval *val)
596 {
597 	int ret = 0;
598 	struct sbs_info *chip = power_supply_get_drvdata(psy);
599 	struct i2c_client *client = chip->client;
600 
601 	switch (psp) {
602 	case POWER_SUPPLY_PROP_PRESENT:
603 	case POWER_SUPPLY_PROP_HEALTH:
604 		ret = sbs_get_battery_presence_and_health(client, psp, val);
605 		if (psp == POWER_SUPPLY_PROP_PRESENT)
606 			return 0;
607 		break;
608 
609 	case POWER_SUPPLY_PROP_TECHNOLOGY:
610 		val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
611 		goto done; /* don't trigger power_supply_changed()! */
612 
613 	case POWER_SUPPLY_PROP_ENERGY_NOW:
614 	case POWER_SUPPLY_PROP_ENERGY_FULL:
615 	case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
616 	case POWER_SUPPLY_PROP_CHARGE_NOW:
617 	case POWER_SUPPLY_PROP_CHARGE_FULL:
618 	case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
619 		ret = sbs_get_property_index(client, psp);
620 		if (ret < 0)
621 			break;
622 
623 		/* sbs_get_battery_capacity() will change the battery mode
624 		 * temporarily to read the requested attribute. Ensure we stay
625 		 * in the desired mode for the duration of the attribute read.
626 		 */
627 		mutex_lock(&chip->mode_lock);
628 		ret = sbs_get_battery_capacity(client, ret, psp, val);
629 		mutex_unlock(&chip->mode_lock);
630 		break;
631 
632 	case POWER_SUPPLY_PROP_SERIAL_NUMBER:
633 		ret = sbs_get_battery_serial_number(client, val);
634 		break;
635 
636 	case POWER_SUPPLY_PROP_STATUS:
637 	case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
638 	case POWER_SUPPLY_PROP_CYCLE_COUNT:
639 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
640 	case POWER_SUPPLY_PROP_CURRENT_NOW:
641 	case POWER_SUPPLY_PROP_TEMP:
642 	case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
643 	case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
644 	case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
645 	case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
646 	case POWER_SUPPLY_PROP_CAPACITY:
647 		ret = sbs_get_property_index(client, psp);
648 		if (ret < 0)
649 			break;
650 
651 		ret = sbs_get_battery_property(client, ret, psp, val);
652 		break;
653 
654 	case POWER_SUPPLY_PROP_MODEL_NAME:
655 		ret = sbs_get_property_index(client, psp);
656 		if (ret < 0)
657 			break;
658 
659 		ret = sbs_get_battery_string_property(client, ret, psp,
660 						      model_name);
661 		val->strval = model_name;
662 		break;
663 
664 	case POWER_SUPPLY_PROP_MANUFACTURER:
665 		ret = sbs_get_property_index(client, psp);
666 		if (ret < 0)
667 			break;
668 
669 		ret = sbs_get_battery_string_property(client, ret, psp,
670 						      manufacturer);
671 		val->strval = manufacturer;
672 		break;
673 
674 	default:
675 		dev_err(&client->dev,
676 			"%s: INVALID property\n", __func__);
677 		return -EINVAL;
678 	}
679 
680 	if (!chip->enable_detection)
681 		goto done;
682 
683 	if (!chip->gpio_detect &&
684 		chip->is_present != (ret >= 0)) {
685 		chip->is_present = (ret >= 0);
686 		power_supply_changed(chip->power_supply);
687 	}
688 
689 done:
690 	if (!ret) {
691 		/* Convert units to match requirements for power supply class */
692 		sbs_unit_adjustment(client, psp, val);
693 	}
694 
695 	dev_dbg(&client->dev,
696 		"%s: property = %d, value = %x\n", __func__, psp, val->intval);
697 
698 	if (ret && chip->is_present)
699 		return ret;
700 
701 	/* battery not present, so return NODATA for properties */
702 	if (ret)
703 		return -ENODATA;
704 
705 	return 0;
706 }
707 
708 static void sbs_supply_changed(struct sbs_info *chip)
709 {
710 	struct power_supply *battery = chip->power_supply;
711 	int ret;
712 
713 	ret = gpiod_get_value_cansleep(chip->gpio_detect);
714 	if (ret < 0)
715 		return;
716 	chip->is_present = ret;
717 	power_supply_changed(battery);
718 }
719 
720 static irqreturn_t sbs_irq(int irq, void *devid)
721 {
722 	sbs_supply_changed(devid);
723 	return IRQ_HANDLED;
724 }
725 
726 static void sbs_alert(struct i2c_client *client, enum i2c_alert_protocol prot,
727 	unsigned int data)
728 {
729 	sbs_supply_changed(i2c_get_clientdata(client));
730 }
731 
732 static void sbs_external_power_changed(struct power_supply *psy)
733 {
734 	struct sbs_info *chip = power_supply_get_drvdata(psy);
735 
736 	/* cancel outstanding work */
737 	cancel_delayed_work_sync(&chip->work);
738 
739 	schedule_delayed_work(&chip->work, HZ);
740 	chip->poll_time = chip->poll_retry_count;
741 }
742 
743 static void sbs_delayed_work(struct work_struct *work)
744 {
745 	struct sbs_info *chip;
746 	s32 ret;
747 
748 	chip = container_of(work, struct sbs_info, work.work);
749 
750 	ret = sbs_read_word_data(chip->client, sbs_data[REG_STATUS].addr);
751 	/* if the read failed, give up on this work */
752 	if (ret < 0) {
753 		chip->poll_time = 0;
754 		return;
755 	}
756 
757 	if (ret & BATTERY_FULL_CHARGED)
758 		ret = POWER_SUPPLY_STATUS_FULL;
759 	else if (ret & BATTERY_DISCHARGING)
760 		ret = POWER_SUPPLY_STATUS_DISCHARGING;
761 	else
762 		ret = POWER_SUPPLY_STATUS_CHARGING;
763 
764 	sbs_status_correct(chip->client, &ret);
765 
766 	if (chip->last_state != ret) {
767 		chip->poll_time = 0;
768 		power_supply_changed(chip->power_supply);
769 		return;
770 	}
771 	if (chip->poll_time > 0) {
772 		schedule_delayed_work(&chip->work, HZ);
773 		chip->poll_time--;
774 		return;
775 	}
776 }
777 
778 static const struct power_supply_desc sbs_default_desc = {
779 	.type = POWER_SUPPLY_TYPE_BATTERY,
780 	.properties = sbs_properties,
781 	.num_properties = ARRAY_SIZE(sbs_properties),
782 	.get_property = sbs_get_property,
783 	.external_power_changed = sbs_external_power_changed,
784 };
785 
786 static int sbs_probe(struct i2c_client *client,
787 	const struct i2c_device_id *id)
788 {
789 	struct sbs_info *chip;
790 	struct power_supply_desc *sbs_desc;
791 	struct sbs_platform_data *pdata = client->dev.platform_data;
792 	struct power_supply_config psy_cfg = {};
793 	int rc;
794 	int irq;
795 
796 	sbs_desc = devm_kmemdup(&client->dev, &sbs_default_desc,
797 			sizeof(*sbs_desc), GFP_KERNEL);
798 	if (!sbs_desc)
799 		return -ENOMEM;
800 
801 	sbs_desc->name = devm_kasprintf(&client->dev, GFP_KERNEL, "sbs-%s",
802 			dev_name(&client->dev));
803 	if (!sbs_desc->name)
804 		return -ENOMEM;
805 
806 	chip = devm_kzalloc(&client->dev, sizeof(struct sbs_info), GFP_KERNEL);
807 	if (!chip)
808 		return -ENOMEM;
809 
810 	chip->client = client;
811 	chip->enable_detection = false;
812 	psy_cfg.of_node = client->dev.of_node;
813 	psy_cfg.drv_data = chip;
814 	chip->last_state = POWER_SUPPLY_STATUS_UNKNOWN;
815 	mutex_init(&chip->mode_lock);
816 
817 	/* use pdata if available, fall back to DT properties,
818 	 * or hardcoded defaults if not
819 	 */
820 	rc = of_property_read_u32(client->dev.of_node, "sbs,i2c-retry-count",
821 				  &chip->i2c_retry_count);
822 	if (rc)
823 		chip->i2c_retry_count = 0;
824 
825 	rc = of_property_read_u32(client->dev.of_node, "sbs,poll-retry-count",
826 				  &chip->poll_retry_count);
827 	if (rc)
828 		chip->poll_retry_count = 0;
829 
830 	if (pdata) {
831 		chip->poll_retry_count = pdata->poll_retry_count;
832 		chip->i2c_retry_count  = pdata->i2c_retry_count;
833 	}
834 	chip->i2c_retry_count = chip->i2c_retry_count + 1;
835 
836 	chip->gpio_detect = devm_gpiod_get_optional(&client->dev,
837 			"sbs,battery-detect", GPIOD_IN);
838 	if (IS_ERR(chip->gpio_detect)) {
839 		dev_err(&client->dev, "Failed to get gpio: %ld\n",
840 			PTR_ERR(chip->gpio_detect));
841 		return PTR_ERR(chip->gpio_detect);
842 	}
843 
844 	i2c_set_clientdata(client, chip);
845 
846 	if (!chip->gpio_detect)
847 		goto skip_gpio;
848 
849 	irq = gpiod_to_irq(chip->gpio_detect);
850 	if (irq <= 0) {
851 		dev_warn(&client->dev, "Failed to get gpio as irq: %d\n", irq);
852 		goto skip_gpio;
853 	}
854 
855 	rc = devm_request_threaded_irq(&client->dev, irq, NULL, sbs_irq,
856 		IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
857 		dev_name(&client->dev), chip);
858 	if (rc) {
859 		dev_warn(&client->dev, "Failed to request irq: %d\n", rc);
860 		goto skip_gpio;
861 	}
862 
863 skip_gpio:
864 	/*
865 	 * Before we register, we might need to make sure we can actually talk
866 	 * to the battery.
867 	 */
868 	if (!(force_load || chip->gpio_detect)) {
869 		rc = sbs_read_word_data(client, sbs_data[REG_STATUS].addr);
870 
871 		if (rc < 0) {
872 			dev_err(&client->dev, "%s: Failed to get device status\n",
873 				__func__);
874 			goto exit_psupply;
875 		}
876 	}
877 
878 	chip->power_supply = devm_power_supply_register(&client->dev, sbs_desc,
879 						   &psy_cfg);
880 	if (IS_ERR(chip->power_supply)) {
881 		dev_err(&client->dev,
882 			"%s: Failed to register power supply\n", __func__);
883 		rc = PTR_ERR(chip->power_supply);
884 		goto exit_psupply;
885 	}
886 
887 	dev_info(&client->dev,
888 		"%s: battery gas gauge device registered\n", client->name);
889 
890 	INIT_DELAYED_WORK(&chip->work, sbs_delayed_work);
891 
892 	chip->enable_detection = true;
893 
894 	return 0;
895 
896 exit_psupply:
897 	return rc;
898 }
899 
900 static int sbs_remove(struct i2c_client *client)
901 {
902 	struct sbs_info *chip = i2c_get_clientdata(client);
903 
904 	cancel_delayed_work_sync(&chip->work);
905 
906 	return 0;
907 }
908 
909 #if defined CONFIG_PM_SLEEP
910 
911 static int sbs_suspend(struct device *dev)
912 {
913 	struct i2c_client *client = to_i2c_client(dev);
914 	struct sbs_info *chip = i2c_get_clientdata(client);
915 
916 	if (chip->poll_time > 0)
917 		cancel_delayed_work_sync(&chip->work);
918 
919 	/*
920 	 * Write to manufacturer access with sleep command.
921 	 * Support is manufacturer dependend, so ignore errors.
922 	 */
923 	sbs_write_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr,
924 		MANUFACTURER_ACCESS_SLEEP);
925 
926 	return 0;
927 }
928 
929 static SIMPLE_DEV_PM_OPS(sbs_pm_ops, sbs_suspend, NULL);
930 #define SBS_PM_OPS (&sbs_pm_ops)
931 
932 #else
933 #define SBS_PM_OPS NULL
934 #endif
935 
936 static const struct i2c_device_id sbs_id[] = {
937 	{ "bq20z75", 0 },
938 	{ "sbs-battery", 1 },
939 	{}
940 };
941 MODULE_DEVICE_TABLE(i2c, sbs_id);
942 
943 static const struct of_device_id sbs_dt_ids[] = {
944 	{ .compatible = "sbs,sbs-battery" },
945 	{ .compatible = "ti,bq20z75" },
946 	{ }
947 };
948 MODULE_DEVICE_TABLE(of, sbs_dt_ids);
949 
950 static struct i2c_driver sbs_battery_driver = {
951 	.probe		= sbs_probe,
952 	.remove		= sbs_remove,
953 	.alert		= sbs_alert,
954 	.id_table	= sbs_id,
955 	.driver = {
956 		.name	= "sbs-battery",
957 		.of_match_table = sbs_dt_ids,
958 		.pm	= SBS_PM_OPS,
959 	},
960 };
961 module_i2c_driver(sbs_battery_driver);
962 
963 MODULE_DESCRIPTION("SBS battery monitor driver");
964 MODULE_LICENSE("GPL");
965 
966 module_param(force_load, bool, S_IRUSR | S_IRGRP | S_IROTH);
967 MODULE_PARM_DESC(force_load,
968 		 "Attempt to load the driver even if no battery is connected");
969