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