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
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>
26 #include <linux/power/sbs-battery.h>
27 #include <linux/power_supply.h>
28 #include <linux/slab.h>
29 #include <linux/stat.h>
32 REG_MANUFACTURER_DATA
,
43 REG_REMAINING_CAPACITY
,
44 REG_REMAINING_CAPACITY_CHARGE
,
45 REG_FULL_CHARGE_CAPACITY
,
46 REG_FULL_CHARGE_CAPACITY_CHARGE
,
48 REG_DESIGN_CAPACITY_CHARGE
,
49 REG_DESIGN_VOLTAGE_MIN
,
50 REG_DESIGN_VOLTAGE_MAX
,
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) { \
77 .min_value = _min_value, \
78 .max_value = _max_value, \
81 static const struct chip_data
{
82 enum power_supply_property psp
;
87 [REG_MANUFACTURER_DATA
] =
88 SBS_DATA(POWER_SUPPLY_PROP_PRESENT
, 0x00, 0, 65535),
90 SBS_DATA(POWER_SUPPLY_PROP_TEMP
, 0x08, 0, 65535),
92 SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_NOW
, 0x09, 0, 20000),
94 SBS_DATA(POWER_SUPPLY_PROP_CURRENT_NOW
, 0x0A, -32768, 32767),
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),
108 SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_FULL_AVG
, 0x13, 0, 65535),
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),
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 *' */
127 SBS_DATA(POWER_SUPPLY_PROP_MANUFACTURER
, 0x20, 0, 65535),
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
160 struct i2c_client
*client
;
161 struct power_supply
*power_supply
;
163 struct gpio_desc
*gpio_detect
;
164 bool enable_detection
;
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
;
183 while (retries
> 0) {
184 ret
= i2c_smbus_read_word_data(client
, address
);
191 dev_dbg(&client
->dev
,
192 "%s: i2c read at address 0x%x failed\n",
200 static int sbs_read_string_data(struct i2c_client
*client
, u8 address
,
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
)){
218 /* Get the length of block data */
219 while (retries_length
> 0) {
220 ret
= i2c_smbus_read_byte_data(client
, address
);
227 dev_dbg(&client
->dev
,
228 "%s: i2c read at address 0x%x failed\n",
233 /* block_length does not include NULL terminator */
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
);
242 /* Get the block data */
243 while (retries_block
> 0) {
244 ret
= i2c_smbus_read_i2c_block_data(
246 block_length
+ 1, block_buffer
);
253 dev_dbg(&client
->dev
,
254 "%s: i2c read at address 0x%x failed\n",
259 /* block_buffer[0] == block_length */
260 memcpy(values
, block_buffer
+ 1, block_length
);
261 values
[block_length
] = '\0';
266 static int sbs_write_word_data(struct i2c_client
*client
, u8 address
,
269 struct sbs_info
*chip
= i2c_get_clientdata(client
);
270 int retries
= chip
->i2c_retry_count
;
273 while (retries
> 0) {
274 ret
= i2c_smbus_write_word_data(client
, address
, value
);
281 dev_dbg(&client
->dev
,
282 "%s: i2c write to address 0x%x failed\n",
290 static int sbs_status_correct(struct i2c_client
*client
, int *intval
)
294 ret
= sbs_read_word_data(client
, sbs_data
[REG_CURRENT
].addr
);
300 /* Not drawing current means full (cannot be not charging) */
302 *intval
= POWER_SUPPLY_STATUS_FULL
;
304 if (*intval
== POWER_SUPPLY_STATUS_FULL
) {
305 /* Drawing or providing current when full */
307 *intval
= POWER_SUPPLY_STATUS_CHARGING
;
309 *intval
= POWER_SUPPLY_STATUS_DISCHARGING
;
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
)
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
);
332 if (psp
== POWER_SUPPLY_PROP_PRESENT
)
333 val
->intval
= 0; /* battery removed */
337 if (ret
< sbs_data
[REG_MANUFACTURER_DATA
].min_value
||
338 ret
> sbs_data
[REG_MANUFACTURER_DATA
].max_value
) {
343 /* Mask the upper nibble of 2nd byte and
344 * lower byte of response then
345 * shift the result by 8 to get status*/
348 if (psp
== POWER_SUPPLY_PROP_PRESENT
) {
350 /* battery removed */
354 } else if (psp
== POWER_SUPPLY_PROP_HEALTH
) {
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
;
362 val
->intval
= POWER_SUPPLY_HEALTH_GOOD
;
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
);
375 ret
= sbs_read_word_data(client
, sbs_data
[reg_offset
].addr
);
379 /* returned values are 16 bit */
380 if (sbs_data
[reg_offset
].min_value
< 0)
383 if (ret
>= sbs_data
[reg_offset
].min_value
&&
384 ret
<= sbs_data
[reg_offset
].max_value
) {
386 if (psp
== POWER_SUPPLY_PROP_CAPACITY_LEVEL
) {
387 if (!(ret
& BATTERY_INITIALIZED
))
389 POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN
;
390 else if (ret
& BATTERY_FULL_CHARGED
)
392 POWER_SUPPLY_CAPACITY_LEVEL_FULL
;
393 else if (ret
& BATTERY_FULL_DISCHARGED
)
395 POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL
;
398 POWER_SUPPLY_CAPACITY_LEVEL_NORMAL
;
400 } else if (psp
!= POWER_SUPPLY_PROP_STATUS
) {
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
;
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
);
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);
435 static int sbs_get_battery_string_property(struct i2c_client
*client
,
436 int reg_offset
, enum power_supply_property psp
, char *val
)
440 ret
= sbs_read_string_data(client
, sbs_data
[reg_offset
].addr
, val
);
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
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.
462 val
->intval
*= BATTERY_MODE_CAP_MULT_WATT
;
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
;
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
;
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.
487 val
->intval
*= TIME_UNIT_CONVERSION
;
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)
505 if ((original_val
& BATTERY_MODE_MASK
) == mode
)
508 if (mode
== BATTERY_MODE_AMPS
)
509 ret
= original_val
& ~BATTERY_MODE_MASK
;
511 ret
= original_val
| BATTERY_MODE_MASK
;
513 ret
= sbs_write_word_data(client
, BATTERY_MODE_OFFSET
, 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
)
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
);
536 ret
= sbs_read_word_data(client
, sbs_data
[reg_offset
].addr
);
542 ret
= sbs_set_battery_mode(client
, mode
);
549 static char sbs_serial
[5];
550 static int sbs_get_battery_serial_number(struct i2c_client
*client
,
551 union power_supply_propval
*val
)
555 ret
= sbs_read_word_data(client
, sbs_data
[REG_SERIAL_NUMBER
].addr
);
559 sprintf(sbs_serial
, "%04x", ret
);
560 val
->strval
= sbs_serial
;
565 static int sbs_get_property_index(struct i2c_client
*client
,
566 enum power_supply_property psp
)
569 for (count
= 0; count
< ARRAY_SIZE(sbs_data
); count
++)
570 if (psp
== sbs_data
[count
].psp
)
573 dev_warn(&client
->dev
,
574 "%s: Invalid Property - %d\n", __func__
, psp
);
579 static int sbs_get_property(struct power_supply
*psy
,
580 enum power_supply_property psp
,
581 union power_supply_propval
*val
)
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
);
591 if (psp
== POWER_SUPPLY_PROP_PRESENT
) {
593 chip
->is_present
= val
->intval
;
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
)
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
);
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
);
631 case POWER_SUPPLY_PROP_SERIAL_NUMBER
:
632 ret
= sbs_get_battery_serial_number(client
, val
);
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
);
650 ret
= sbs_get_battery_property(client
, ret
, psp
, val
);
653 case POWER_SUPPLY_PROP_MODEL_NAME
:
654 ret
= sbs_get_property_index(client
, psp
);
658 ret
= sbs_get_battery_string_property(client
, ret
, psp
,
660 val
->strval
= model_name
;
663 case POWER_SUPPLY_PROP_MANUFACTURER
:
664 ret
= sbs_get_property_index(client
, psp
);
668 ret
= sbs_get_battery_string_property(client
, ret
, psp
,
670 val
->strval
= manufacturer
;
674 dev_err(&client
->dev
,
675 "%s: INVALID property\n", __func__
);
679 if (!chip
->enable_detection
)
682 if (!chip
->gpio_detect
&&
683 chip
->is_present
!= (ret
>= 0)) {
684 chip
->is_present
= (ret
>= 0);
685 power_supply_changed(chip
->power_supply
);
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
)
700 /* battery not present, so return NODATA for properties */
707 static void sbs_supply_changed(struct sbs_info
*chip
)
709 struct power_supply
*battery
= chip
->power_supply
;
712 ret
= gpiod_get_value_cansleep(chip
->gpio_detect
);
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
);
725 static void sbs_alert(struct i2c_client
*client
, enum i2c_alert_protocol prot
,
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
;
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 */
756 if (ret
& BATTERY_FULL_CHARGED
)
757 ret
= POWER_SUPPLY_STATUS_FULL
;
758 else if (ret
& BATTERY_DISCHARGING
)
759 ret
= POWER_SUPPLY_STATUS_DISCHARGING
;
761 ret
= POWER_SUPPLY_STATUS_CHARGING
;
763 sbs_status_correct(chip
->client
, &ret
);
765 if (chip
->last_state
!= ret
) {
767 power_supply_changed(chip
->power_supply
);
770 if (chip
->poll_time
> 0) {
771 schedule_delayed_work(&chip
->work
, HZ
);
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
= {};
795 sbs_desc
= devm_kmemdup(&client
->dev
, &sbs_default_desc
,
796 sizeof(*sbs_desc
), GFP_KERNEL
);
800 sbs_desc
->name
= devm_kasprintf(&client
->dev
, GFP_KERNEL
, "sbs-%s",
801 dev_name(&client
->dev
));
805 chip
= devm_kzalloc(&client
->dev
, sizeof(struct sbs_info
), GFP_KERNEL
);
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
);
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
);
827 chip
->poll_retry_count
= 0;
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
)
848 irq
= gpiod_to_irq(chip
->gpio_detect
);
850 dev_warn(&client
->dev
, "Failed to get gpio as irq: %d\n", irq
);
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
);
858 dev_warn(&client
->dev
, "Failed to request irq: %d\n", rc
);
864 * Before we register, we might need to make sure we can actually talk
867 if (!(force_load
|| chip
->gpio_detect
)) {
868 rc
= sbs_read_word_data(client
, sbs_data
[REG_STATUS
].addr
);
871 dev_err(&client
->dev
, "%s: Failed to get device status\n",
877 chip
->power_supply
= devm_power_supply_register(&client
->dev
, sbs_desc
,
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
);
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;
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
);
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
);
928 static SIMPLE_DEV_PM_OPS(sbs_pm_ops
, sbs_suspend
, NULL
);
929 #define SBS_PM_OPS (&sbs_pm_ops)
932 #define SBS_PM_OPS NULL
935 static const struct i2c_device_id sbs_id
[] = {
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
= {
951 .remove
= sbs_remove
,
955 .name
= "sbs-battery",
956 .of_match_table
= sbs_dt_ids
,
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");