Merge branch 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[cris-mirror.git] / drivers / power / supply / sbs-battery.c
blob83d7b4115857e38fcb310dbbdadc373e6104dfc0
1 /*
2 * Gas Gauge driver for SBS Compliant Batteries
4 * Copyright (c) 2010, NVIDIA Corporation.
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.
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.
17 #include <linux/delay.h>
18 #include <linux/err.h>
19 #include <linux/gpio/consumer.h>
20 #include <linux/i2c.h>
21 #include <linux/init.h>
22 #include <linux/interrupt.h>
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/of.h>
26 #include <linux/power/sbs-battery.h>
27 #include <linux/power_supply.h>
28 #include <linux/slab.h>
29 #include <linux/stat.h>
31 enum {
32 REG_MANUFACTURER_DATA,
33 REG_TEMPERATURE,
34 REG_VOLTAGE,
35 REG_CURRENT,
36 REG_CAPACITY,
37 REG_TIME_TO_EMPTY,
38 REG_TIME_TO_FULL,
39 REG_STATUS,
40 REG_CAPACITY_LEVEL,
41 REG_CYCLE_COUNT,
42 REG_SERIAL_NUMBER,
43 REG_REMAINING_CAPACITY,
44 REG_REMAINING_CAPACITY_CHARGE,
45 REG_FULL_CHARGE_CAPACITY,
46 REG_FULL_CHARGE_CAPACITY_CHARGE,
47 REG_DESIGN_CAPACITY,
48 REG_DESIGN_CAPACITY_CHARGE,
49 REG_DESIGN_VOLTAGE_MIN,
50 REG_DESIGN_VOLTAGE_MAX,
51 REG_MANUFACTURER,
52 REG_MODEL_NAME,
55 /* Battery Mode defines */
56 #define BATTERY_MODE_OFFSET 0x03
57 #define BATTERY_MODE_MASK 0x8000
58 enum sbs_battery_mode {
59 BATTERY_MODE_AMPS = 0,
60 BATTERY_MODE_WATTS = 0x8000
63 /* manufacturer access defines */
64 #define MANUFACTURER_ACCESS_STATUS 0x0006
65 #define MANUFACTURER_ACCESS_SLEEP 0x0011
67 /* battery status value bits */
68 #define BATTERY_INITIALIZED 0x80
69 #define BATTERY_DISCHARGING 0x40
70 #define BATTERY_FULL_CHARGED 0x20
71 #define BATTERY_FULL_DISCHARGED 0x10
73 /* min_value and max_value are only valid for numerical data */
74 #define SBS_DATA(_psp, _addr, _min_value, _max_value) { \
75 .psp = _psp, \
76 .addr = _addr, \
77 .min_value = _min_value, \
78 .max_value = _max_value, \
81 static const struct chip_data {
82 enum power_supply_property psp;
83 u8 addr;
84 int min_value;
85 int max_value;
86 } sbs_data[] = {
87 [REG_MANUFACTURER_DATA] =
88 SBS_DATA(POWER_SUPPLY_PROP_PRESENT, 0x00, 0, 65535),
89 [REG_TEMPERATURE] =
90 SBS_DATA(POWER_SUPPLY_PROP_TEMP, 0x08, 0, 65535),
91 [REG_VOLTAGE] =
92 SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_NOW, 0x09, 0, 20000),
93 [REG_CURRENT] =
94 SBS_DATA(POWER_SUPPLY_PROP_CURRENT_NOW, 0x0A, -32768, 32767),
95 [REG_CAPACITY] =
96 SBS_DATA(POWER_SUPPLY_PROP_CAPACITY, 0x0D, 0, 100),
97 [REG_REMAINING_CAPACITY] =
98 SBS_DATA(POWER_SUPPLY_PROP_ENERGY_NOW, 0x0F, 0, 65535),
99 [REG_REMAINING_CAPACITY_CHARGE] =
100 SBS_DATA(POWER_SUPPLY_PROP_CHARGE_NOW, 0x0F, 0, 65535),
101 [REG_FULL_CHARGE_CAPACITY] =
102 SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL, 0x10, 0, 65535),
103 [REG_FULL_CHARGE_CAPACITY_CHARGE] =
104 SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL, 0x10, 0, 65535),
105 [REG_TIME_TO_EMPTY] =
106 SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, 0x12, 0, 65535),
107 [REG_TIME_TO_FULL] =
108 SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_FULL_AVG, 0x13, 0, 65535),
109 [REG_STATUS] =
110 SBS_DATA(POWER_SUPPLY_PROP_STATUS, 0x16, 0, 65535),
111 [REG_CAPACITY_LEVEL] =
112 SBS_DATA(POWER_SUPPLY_PROP_CAPACITY_LEVEL, 0x16, 0, 65535),
113 [REG_CYCLE_COUNT] =
114 SBS_DATA(POWER_SUPPLY_PROP_CYCLE_COUNT, 0x17, 0, 65535),
115 [REG_DESIGN_CAPACITY] =
116 SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, 0x18, 0, 65535),
117 [REG_DESIGN_CAPACITY_CHARGE] =
118 SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 0x18, 0, 65535),
119 [REG_DESIGN_VOLTAGE_MIN] =
120 SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, 0x19, 0, 65535),
121 [REG_DESIGN_VOLTAGE_MAX] =
122 SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, 0x19, 0, 65535),
123 [REG_SERIAL_NUMBER] =
124 SBS_DATA(POWER_SUPPLY_PROP_SERIAL_NUMBER, 0x1C, 0, 65535),
125 /* Properties of type `const char *' */
126 [REG_MANUFACTURER] =
127 SBS_DATA(POWER_SUPPLY_PROP_MANUFACTURER, 0x20, 0, 65535),
128 [REG_MODEL_NAME] =
129 SBS_DATA(POWER_SUPPLY_PROP_MODEL_NAME, 0x21, 0, 65535)
132 static enum power_supply_property sbs_properties[] = {
133 POWER_SUPPLY_PROP_STATUS,
134 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
135 POWER_SUPPLY_PROP_HEALTH,
136 POWER_SUPPLY_PROP_PRESENT,
137 POWER_SUPPLY_PROP_TECHNOLOGY,
138 POWER_SUPPLY_PROP_CYCLE_COUNT,
139 POWER_SUPPLY_PROP_VOLTAGE_NOW,
140 POWER_SUPPLY_PROP_CURRENT_NOW,
141 POWER_SUPPLY_PROP_CAPACITY,
142 POWER_SUPPLY_PROP_TEMP,
143 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
144 POWER_SUPPLY_PROP_TIME_TO_FULL_AVG,
145 POWER_SUPPLY_PROP_SERIAL_NUMBER,
146 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
147 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
148 POWER_SUPPLY_PROP_ENERGY_NOW,
149 POWER_SUPPLY_PROP_ENERGY_FULL,
150 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
151 POWER_SUPPLY_PROP_CHARGE_NOW,
152 POWER_SUPPLY_PROP_CHARGE_FULL,
153 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
154 /* Properties of type `const char *' */
155 POWER_SUPPLY_PROP_MANUFACTURER,
156 POWER_SUPPLY_PROP_MODEL_NAME
159 struct sbs_info {
160 struct i2c_client *client;
161 struct power_supply *power_supply;
162 bool is_present;
163 struct gpio_desc *gpio_detect;
164 bool enable_detection;
165 int last_state;
166 int poll_time;
167 u32 i2c_retry_count;
168 u32 poll_retry_count;
169 struct delayed_work work;
170 struct mutex mode_lock;
173 static char model_name[I2C_SMBUS_BLOCK_MAX + 1];
174 static char manufacturer[I2C_SMBUS_BLOCK_MAX + 1];
175 static bool force_load;
177 static int sbs_read_word_data(struct i2c_client *client, u8 address)
179 struct sbs_info *chip = i2c_get_clientdata(client);
180 int retries = chip->i2c_retry_count;
181 s32 ret = 0;
183 while (retries > 0) {
184 ret = i2c_smbus_read_word_data(client, address);
185 if (ret >= 0)
186 break;
187 retries--;
190 if (ret < 0) {
191 dev_dbg(&client->dev,
192 "%s: i2c read at address 0x%x failed\n",
193 __func__, address);
194 return ret;
197 return ret;
200 static int sbs_read_string_data(struct i2c_client *client, u8 address,
201 char *values)
203 struct sbs_info *chip = i2c_get_clientdata(client);
204 s32 ret = 0, block_length = 0;
205 int retries_length, retries_block;
206 u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1];
208 retries_length = chip->i2c_retry_count;
209 retries_block = chip->i2c_retry_count;
211 /* Adapter needs to support these two functions */
212 if (!i2c_check_functionality(client->adapter,
213 I2C_FUNC_SMBUS_BYTE_DATA |
214 I2C_FUNC_SMBUS_I2C_BLOCK)){
215 return -ENODEV;
218 /* Get the length of block data */
219 while (retries_length > 0) {
220 ret = i2c_smbus_read_byte_data(client, address);
221 if (ret >= 0)
222 break;
223 retries_length--;
226 if (ret < 0) {
227 dev_dbg(&client->dev,
228 "%s: i2c read at address 0x%x failed\n",
229 __func__, address);
230 return ret;
233 /* block_length does not include NULL terminator */
234 block_length = ret;
235 if (block_length > I2C_SMBUS_BLOCK_MAX) {
236 dev_err(&client->dev,
237 "%s: Returned block_length is longer than 0x%x\n",
238 __func__, I2C_SMBUS_BLOCK_MAX);
239 return -EINVAL;
242 /* Get the block data */
243 while (retries_block > 0) {
244 ret = i2c_smbus_read_i2c_block_data(
245 client, address,
246 block_length + 1, block_buffer);
247 if (ret >= 0)
248 break;
249 retries_block--;
252 if (ret < 0) {
253 dev_dbg(&client->dev,
254 "%s: i2c read at address 0x%x failed\n",
255 __func__, address);
256 return ret;
259 /* block_buffer[0] == block_length */
260 memcpy(values, block_buffer + 1, block_length);
261 values[block_length] = '\0';
263 return ret;
266 static int sbs_write_word_data(struct i2c_client *client, u8 address,
267 u16 value)
269 struct sbs_info *chip = i2c_get_clientdata(client);
270 int retries = chip->i2c_retry_count;
271 s32 ret = 0;
273 while (retries > 0) {
274 ret = i2c_smbus_write_word_data(client, address, value);
275 if (ret >= 0)
276 break;
277 retries--;
280 if (ret < 0) {
281 dev_dbg(&client->dev,
282 "%s: i2c write to address 0x%x failed\n",
283 __func__, address);
284 return ret;
287 return 0;
290 static int sbs_status_correct(struct i2c_client *client, int *intval)
292 int ret;
294 ret = sbs_read_word_data(client, sbs_data[REG_CURRENT].addr);
295 if (ret < 0)
296 return ret;
298 ret = (s16)ret;
300 /* Not drawing current means full (cannot be not charging) */
301 if (ret == 0)
302 *intval = POWER_SUPPLY_STATUS_FULL;
304 if (*intval == POWER_SUPPLY_STATUS_FULL) {
305 /* Drawing or providing current when full */
306 if (ret > 0)
307 *intval = POWER_SUPPLY_STATUS_CHARGING;
308 else if (ret < 0)
309 *intval = POWER_SUPPLY_STATUS_DISCHARGING;
312 return 0;
315 static int sbs_get_battery_presence_and_health(
316 struct i2c_client *client, enum power_supply_property psp,
317 union power_supply_propval *val)
319 s32 ret;
322 * Write to ManufacturerAccess with ManufacturerAccess command
323 * and then read the status. Do not check for error on the write
324 * since not all batteries implement write access to this command,
325 * while others mandate it.
327 sbs_write_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr,
328 MANUFACTURER_ACCESS_STATUS);
330 ret = sbs_read_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr);
331 if (ret < 0) {
332 if (psp == POWER_SUPPLY_PROP_PRESENT)
333 val->intval = 0; /* battery removed */
334 return ret;
337 if (ret < sbs_data[REG_MANUFACTURER_DATA].min_value ||
338 ret > sbs_data[REG_MANUFACTURER_DATA].max_value) {
339 val->intval = 0;
340 return 0;
343 /* Mask the upper nibble of 2nd byte and
344 * lower byte of response then
345 * shift the result by 8 to get status*/
346 ret &= 0x0F00;
347 ret >>= 8;
348 if (psp == POWER_SUPPLY_PROP_PRESENT) {
349 if (ret == 0x0F)
350 /* battery removed */
351 val->intval = 0;
352 else
353 val->intval = 1;
354 } else if (psp == POWER_SUPPLY_PROP_HEALTH) {
355 if (ret == 0x09)
356 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
357 else if (ret == 0x0B)
358 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
359 else if (ret == 0x0C)
360 val->intval = POWER_SUPPLY_HEALTH_DEAD;
361 else
362 val->intval = POWER_SUPPLY_HEALTH_GOOD;
365 return 0;
368 static int sbs_get_battery_property(struct i2c_client *client,
369 int reg_offset, enum power_supply_property psp,
370 union power_supply_propval *val)
372 struct sbs_info *chip = i2c_get_clientdata(client);
373 s32 ret;
375 ret = sbs_read_word_data(client, sbs_data[reg_offset].addr);
376 if (ret < 0)
377 return ret;
379 /* returned values are 16 bit */
380 if (sbs_data[reg_offset].min_value < 0)
381 ret = (s16)ret;
383 if (ret >= sbs_data[reg_offset].min_value &&
384 ret <= sbs_data[reg_offset].max_value) {
385 val->intval = ret;
386 if (psp == POWER_SUPPLY_PROP_CAPACITY_LEVEL) {
387 if (!(ret & BATTERY_INITIALIZED))
388 val->intval =
389 POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
390 else if (ret & BATTERY_FULL_CHARGED)
391 val->intval =
392 POWER_SUPPLY_CAPACITY_LEVEL_FULL;
393 else if (ret & BATTERY_FULL_DISCHARGED)
394 val->intval =
395 POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
396 else
397 val->intval =
398 POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
399 return 0;
400 } else if (psp != POWER_SUPPLY_PROP_STATUS) {
401 return 0;
404 if (ret & BATTERY_FULL_CHARGED)
405 val->intval = POWER_SUPPLY_STATUS_FULL;
406 else if (ret & BATTERY_DISCHARGING)
407 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
408 else
409 val->intval = POWER_SUPPLY_STATUS_CHARGING;
411 sbs_status_correct(client, &val->intval);
413 if (chip->poll_time == 0)
414 chip->last_state = val->intval;
415 else if (chip->last_state != val->intval) {
416 cancel_delayed_work_sync(&chip->work);
417 power_supply_changed(chip->power_supply);
418 chip->poll_time = 0;
420 } else {
421 if (psp == POWER_SUPPLY_PROP_STATUS)
422 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
423 else if (psp == POWER_SUPPLY_PROP_CAPACITY)
424 /* sbs spec says that this can be >100 %
425 * even if max value is 100 %
427 val->intval = min(ret, 100);
428 else
429 val->intval = 0;
432 return 0;
435 static int sbs_get_battery_string_property(struct i2c_client *client,
436 int reg_offset, enum power_supply_property psp, char *val)
438 s32 ret;
440 ret = sbs_read_string_data(client, sbs_data[reg_offset].addr, val);
442 if (ret < 0)
443 return ret;
445 return 0;
448 static void sbs_unit_adjustment(struct i2c_client *client,
449 enum power_supply_property psp, union power_supply_propval *val)
451 #define BASE_UNIT_CONVERSION 1000
452 #define BATTERY_MODE_CAP_MULT_WATT (10 * BASE_UNIT_CONVERSION)
453 #define TIME_UNIT_CONVERSION 60
454 #define TEMP_KELVIN_TO_CELSIUS 2731
455 switch (psp) {
456 case POWER_SUPPLY_PROP_ENERGY_NOW:
457 case POWER_SUPPLY_PROP_ENERGY_FULL:
458 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
459 /* sbs provides energy in units of 10mWh.
460 * Convert to µWh
462 val->intval *= BATTERY_MODE_CAP_MULT_WATT;
463 break;
465 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
466 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
467 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
468 case POWER_SUPPLY_PROP_CURRENT_NOW:
469 case POWER_SUPPLY_PROP_CHARGE_NOW:
470 case POWER_SUPPLY_PROP_CHARGE_FULL:
471 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
472 val->intval *= BASE_UNIT_CONVERSION;
473 break;
475 case POWER_SUPPLY_PROP_TEMP:
476 /* sbs provides battery temperature in 0.1K
477 * so convert it to 0.1°C
479 val->intval -= TEMP_KELVIN_TO_CELSIUS;
480 break;
482 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
483 case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
484 /* sbs provides time to empty and time to full in minutes.
485 * Convert to seconds
487 val->intval *= TIME_UNIT_CONVERSION;
488 break;
490 default:
491 dev_dbg(&client->dev,
492 "%s: no need for unit conversion %d\n", __func__, psp);
496 static enum sbs_battery_mode sbs_set_battery_mode(struct i2c_client *client,
497 enum sbs_battery_mode mode)
499 int ret, original_val;
501 original_val = sbs_read_word_data(client, BATTERY_MODE_OFFSET);
502 if (original_val < 0)
503 return original_val;
505 if ((original_val & BATTERY_MODE_MASK) == mode)
506 return mode;
508 if (mode == BATTERY_MODE_AMPS)
509 ret = original_val & ~BATTERY_MODE_MASK;
510 else
511 ret = original_val | BATTERY_MODE_MASK;
513 ret = sbs_write_word_data(client, BATTERY_MODE_OFFSET, ret);
514 if (ret < 0)
515 return ret;
517 usleep_range(1000, 2000);
519 return original_val & BATTERY_MODE_MASK;
522 static int sbs_get_battery_capacity(struct i2c_client *client,
523 int reg_offset, enum power_supply_property psp,
524 union power_supply_propval *val)
526 s32 ret;
527 enum sbs_battery_mode mode = BATTERY_MODE_WATTS;
529 if (power_supply_is_amp_property(psp))
530 mode = BATTERY_MODE_AMPS;
532 mode = sbs_set_battery_mode(client, mode);
533 if (mode < 0)
534 return mode;
536 ret = sbs_read_word_data(client, sbs_data[reg_offset].addr);
537 if (ret < 0)
538 return ret;
540 val->intval = ret;
542 ret = sbs_set_battery_mode(client, mode);
543 if (ret < 0)
544 return ret;
546 return 0;
549 static char sbs_serial[5];
550 static int sbs_get_battery_serial_number(struct i2c_client *client,
551 union power_supply_propval *val)
553 int ret;
555 ret = sbs_read_word_data(client, sbs_data[REG_SERIAL_NUMBER].addr);
556 if (ret < 0)
557 return ret;
559 sprintf(sbs_serial, "%04x", ret);
560 val->strval = sbs_serial;
562 return 0;
565 static int sbs_get_property_index(struct i2c_client *client,
566 enum power_supply_property psp)
568 int count;
569 for (count = 0; count < ARRAY_SIZE(sbs_data); count++)
570 if (psp == sbs_data[count].psp)
571 return count;
573 dev_warn(&client->dev,
574 "%s: Invalid Property - %d\n", __func__, psp);
576 return -EINVAL;
579 static int sbs_get_property(struct power_supply *psy,
580 enum power_supply_property psp,
581 union power_supply_propval *val)
583 int ret = 0;
584 struct sbs_info *chip = power_supply_get_drvdata(psy);
585 struct i2c_client *client = chip->client;
587 if (chip->gpio_detect) {
588 ret = gpiod_get_value_cansleep(chip->gpio_detect);
589 if (ret < 0)
590 return ret;
591 if (psp == POWER_SUPPLY_PROP_PRESENT) {
592 val->intval = ret;
593 chip->is_present = val->intval;
594 return 0;
596 if (ret == 0)
597 return -ENODATA;
600 switch (psp) {
601 case POWER_SUPPLY_PROP_PRESENT:
602 case POWER_SUPPLY_PROP_HEALTH:
603 ret = sbs_get_battery_presence_and_health(client, psp, val);
604 if (psp == POWER_SUPPLY_PROP_PRESENT)
605 return 0;
606 break;
608 case POWER_SUPPLY_PROP_TECHNOLOGY:
609 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
610 goto done; /* don't trigger power_supply_changed()! */
612 case POWER_SUPPLY_PROP_ENERGY_NOW:
613 case POWER_SUPPLY_PROP_ENERGY_FULL:
614 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
615 case POWER_SUPPLY_PROP_CHARGE_NOW:
616 case POWER_SUPPLY_PROP_CHARGE_FULL:
617 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
618 ret = sbs_get_property_index(client, psp);
619 if (ret < 0)
620 break;
622 /* sbs_get_battery_capacity() will change the battery mode
623 * temporarily to read the requested attribute. Ensure we stay
624 * in the desired mode for the duration of the attribute read.
626 mutex_lock(&chip->mode_lock);
627 ret = sbs_get_battery_capacity(client, ret, psp, val);
628 mutex_unlock(&chip->mode_lock);
629 break;
631 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
632 ret = sbs_get_battery_serial_number(client, val);
633 break;
635 case POWER_SUPPLY_PROP_STATUS:
636 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
637 case POWER_SUPPLY_PROP_CYCLE_COUNT:
638 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
639 case POWER_SUPPLY_PROP_CURRENT_NOW:
640 case POWER_SUPPLY_PROP_TEMP:
641 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
642 case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
643 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
644 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
645 case POWER_SUPPLY_PROP_CAPACITY:
646 ret = sbs_get_property_index(client, psp);
647 if (ret < 0)
648 break;
650 ret = sbs_get_battery_property(client, ret, psp, val);
651 break;
653 case POWER_SUPPLY_PROP_MODEL_NAME:
654 ret = sbs_get_property_index(client, psp);
655 if (ret < 0)
656 break;
658 ret = sbs_get_battery_string_property(client, ret, psp,
659 model_name);
660 val->strval = model_name;
661 break;
663 case POWER_SUPPLY_PROP_MANUFACTURER:
664 ret = sbs_get_property_index(client, psp);
665 if (ret < 0)
666 break;
668 ret = sbs_get_battery_string_property(client, ret, psp,
669 manufacturer);
670 val->strval = manufacturer;
671 break;
673 default:
674 dev_err(&client->dev,
675 "%s: INVALID property\n", __func__);
676 return -EINVAL;
679 if (!chip->enable_detection)
680 goto done;
682 if (!chip->gpio_detect &&
683 chip->is_present != (ret >= 0)) {
684 chip->is_present = (ret >= 0);
685 power_supply_changed(chip->power_supply);
688 done:
689 if (!ret) {
690 /* Convert units to match requirements for power supply class */
691 sbs_unit_adjustment(client, psp, val);
694 dev_dbg(&client->dev,
695 "%s: property = %d, value = %x\n", __func__, psp, val->intval);
697 if (ret && chip->is_present)
698 return ret;
700 /* battery not present, so return NODATA for properties */
701 if (ret)
702 return -ENODATA;
704 return 0;
707 static void sbs_supply_changed(struct sbs_info *chip)
709 struct power_supply *battery = chip->power_supply;
710 int ret;
712 ret = gpiod_get_value_cansleep(chip->gpio_detect);
713 if (ret < 0)
714 return;
715 chip->is_present = ret;
716 power_supply_changed(battery);
719 static irqreturn_t sbs_irq(int irq, void *devid)
721 sbs_supply_changed(devid);
722 return IRQ_HANDLED;
725 static void sbs_alert(struct i2c_client *client, enum i2c_alert_protocol prot,
726 unsigned int data)
728 sbs_supply_changed(i2c_get_clientdata(client));
731 static void sbs_external_power_changed(struct power_supply *psy)
733 struct sbs_info *chip = power_supply_get_drvdata(psy);
735 /* cancel outstanding work */
736 cancel_delayed_work_sync(&chip->work);
738 schedule_delayed_work(&chip->work, HZ);
739 chip->poll_time = chip->poll_retry_count;
742 static void sbs_delayed_work(struct work_struct *work)
744 struct sbs_info *chip;
745 s32 ret;
747 chip = container_of(work, struct sbs_info, work.work);
749 ret = sbs_read_word_data(chip->client, sbs_data[REG_STATUS].addr);
750 /* if the read failed, give up on this work */
751 if (ret < 0) {
752 chip->poll_time = 0;
753 return;
756 if (ret & BATTERY_FULL_CHARGED)
757 ret = POWER_SUPPLY_STATUS_FULL;
758 else if (ret & BATTERY_DISCHARGING)
759 ret = POWER_SUPPLY_STATUS_DISCHARGING;
760 else
761 ret = POWER_SUPPLY_STATUS_CHARGING;
763 sbs_status_correct(chip->client, &ret);
765 if (chip->last_state != ret) {
766 chip->poll_time = 0;
767 power_supply_changed(chip->power_supply);
768 return;
770 if (chip->poll_time > 0) {
771 schedule_delayed_work(&chip->work, HZ);
772 chip->poll_time--;
773 return;
777 static const struct power_supply_desc sbs_default_desc = {
778 .type = POWER_SUPPLY_TYPE_BATTERY,
779 .properties = sbs_properties,
780 .num_properties = ARRAY_SIZE(sbs_properties),
781 .get_property = sbs_get_property,
782 .external_power_changed = sbs_external_power_changed,
785 static int sbs_probe(struct i2c_client *client,
786 const struct i2c_device_id *id)
788 struct sbs_info *chip;
789 struct power_supply_desc *sbs_desc;
790 struct sbs_platform_data *pdata = client->dev.platform_data;
791 struct power_supply_config psy_cfg = {};
792 int rc;
793 int irq;
795 sbs_desc = devm_kmemdup(&client->dev, &sbs_default_desc,
796 sizeof(*sbs_desc), GFP_KERNEL);
797 if (!sbs_desc)
798 return -ENOMEM;
800 sbs_desc->name = devm_kasprintf(&client->dev, GFP_KERNEL, "sbs-%s",
801 dev_name(&client->dev));
802 if (!sbs_desc->name)
803 return -ENOMEM;
805 chip = devm_kzalloc(&client->dev, sizeof(struct sbs_info), GFP_KERNEL);
806 if (!chip)
807 return -ENOMEM;
809 chip->client = client;
810 chip->enable_detection = false;
811 psy_cfg.of_node = client->dev.of_node;
812 psy_cfg.drv_data = chip;
813 chip->last_state = POWER_SUPPLY_STATUS_UNKNOWN;
814 mutex_init(&chip->mode_lock);
816 /* use pdata if available, fall back to DT properties,
817 * or hardcoded defaults if not
819 rc = of_property_read_u32(client->dev.of_node, "sbs,i2c-retry-count",
820 &chip->i2c_retry_count);
821 if (rc)
822 chip->i2c_retry_count = 0;
824 rc = of_property_read_u32(client->dev.of_node, "sbs,poll-retry-count",
825 &chip->poll_retry_count);
826 if (rc)
827 chip->poll_retry_count = 0;
829 if (pdata) {
830 chip->poll_retry_count = pdata->poll_retry_count;
831 chip->i2c_retry_count = pdata->i2c_retry_count;
833 chip->i2c_retry_count = chip->i2c_retry_count + 1;
835 chip->gpio_detect = devm_gpiod_get_optional(&client->dev,
836 "sbs,battery-detect", GPIOD_IN);
837 if (IS_ERR(chip->gpio_detect)) {
838 dev_err(&client->dev, "Failed to get gpio: %ld\n",
839 PTR_ERR(chip->gpio_detect));
840 return PTR_ERR(chip->gpio_detect);
843 i2c_set_clientdata(client, chip);
845 if (!chip->gpio_detect)
846 goto skip_gpio;
848 irq = gpiod_to_irq(chip->gpio_detect);
849 if (irq <= 0) {
850 dev_warn(&client->dev, "Failed to get gpio as irq: %d\n", irq);
851 goto skip_gpio;
854 rc = devm_request_threaded_irq(&client->dev, irq, NULL, sbs_irq,
855 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
856 dev_name(&client->dev), chip);
857 if (rc) {
858 dev_warn(&client->dev, "Failed to request irq: %d\n", rc);
859 goto skip_gpio;
862 skip_gpio:
864 * Before we register, we might need to make sure we can actually talk
865 * to the battery.
867 if (!(force_load || chip->gpio_detect)) {
868 rc = sbs_read_word_data(client, sbs_data[REG_STATUS].addr);
870 if (rc < 0) {
871 dev_err(&client->dev, "%s: Failed to get device status\n",
872 __func__);
873 goto exit_psupply;
877 chip->power_supply = devm_power_supply_register(&client->dev, sbs_desc,
878 &psy_cfg);
879 if (IS_ERR(chip->power_supply)) {
880 dev_err(&client->dev,
881 "%s: Failed to register power supply\n", __func__);
882 rc = PTR_ERR(chip->power_supply);
883 goto exit_psupply;
886 dev_info(&client->dev,
887 "%s: battery gas gauge device registered\n", client->name);
889 INIT_DELAYED_WORK(&chip->work, sbs_delayed_work);
891 chip->enable_detection = true;
893 return 0;
895 exit_psupply:
896 return rc;
899 static int sbs_remove(struct i2c_client *client)
901 struct sbs_info *chip = i2c_get_clientdata(client);
903 cancel_delayed_work_sync(&chip->work);
905 return 0;
908 #if defined CONFIG_PM_SLEEP
910 static int sbs_suspend(struct device *dev)
912 struct i2c_client *client = to_i2c_client(dev);
913 struct sbs_info *chip = i2c_get_clientdata(client);
915 if (chip->poll_time > 0)
916 cancel_delayed_work_sync(&chip->work);
919 * Write to manufacturer access with sleep command.
920 * Support is manufacturer dependend, so ignore errors.
922 sbs_write_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr,
923 MANUFACTURER_ACCESS_SLEEP);
925 return 0;
928 static SIMPLE_DEV_PM_OPS(sbs_pm_ops, sbs_suspend, NULL);
929 #define SBS_PM_OPS (&sbs_pm_ops)
931 #else
932 #define SBS_PM_OPS NULL
933 #endif
935 static const struct i2c_device_id sbs_id[] = {
936 { "bq20z75", 0 },
937 { "sbs-battery", 1 },
940 MODULE_DEVICE_TABLE(i2c, sbs_id);
942 static const struct of_device_id sbs_dt_ids[] = {
943 { .compatible = "sbs,sbs-battery" },
944 { .compatible = "ti,bq20z75" },
947 MODULE_DEVICE_TABLE(of, sbs_dt_ids);
949 static struct i2c_driver sbs_battery_driver = {
950 .probe = sbs_probe,
951 .remove = sbs_remove,
952 .alert = sbs_alert,
953 .id_table = sbs_id,
954 .driver = {
955 .name = "sbs-battery",
956 .of_match_table = sbs_dt_ids,
957 .pm = SBS_PM_OPS,
960 module_i2c_driver(sbs_battery_driver);
962 MODULE_DESCRIPTION("SBS battery monitor driver");
963 MODULE_LICENSE("GPL");
965 module_param(force_load, bool, S_IRUSR | S_IRGRP | S_IROTH);
966 MODULE_PARM_DESC(force_load,
967 "Attempt to load the driver even if no battery is connected");