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