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