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
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include <linux/init.h>
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/err.h>
25 #include <linux/power_supply.h>
26 #include <linux/i2c.h>
27 #include <linux/slab.h>
28 #include <linux/interrupt.h>
29 #include <linux/gpio/consumer.h>
31 #include <linux/stat.h>
33 #include <linux/power/sbs-battery.h>
36 REG_MANUFACTURER_DATA
,
47 REG_REMAINING_CAPACITY
,
48 REG_REMAINING_CAPACITY_CHARGE
,
49 REG_FULL_CHARGE_CAPACITY
,
50 REG_FULL_CHARGE_CAPACITY_CHARGE
,
52 REG_DESIGN_CAPACITY_CHARGE
,
53 REG_DESIGN_VOLTAGE_MIN
,
54 REG_DESIGN_VOLTAGE_MAX
,
59 /* Battery Mode defines */
60 #define BATTERY_MODE_OFFSET 0x03
61 #define BATTERY_MODE_MASK 0x8000
62 enum sbs_battery_mode
{
67 /* manufacturer access defines */
68 #define MANUFACTURER_ACCESS_STATUS 0x0006
69 #define MANUFACTURER_ACCESS_SLEEP 0x0011
71 /* battery status value bits */
72 #define BATTERY_INITIALIZED 0x80
73 #define BATTERY_DISCHARGING 0x40
74 #define BATTERY_FULL_CHARGED 0x20
75 #define BATTERY_FULL_DISCHARGED 0x10
77 /* min_value and max_value are only valid for numerical data */
78 #define SBS_DATA(_psp, _addr, _min_value, _max_value) { \
81 .min_value = _min_value, \
82 .max_value = _max_value, \
85 static const struct chip_data
{
86 enum power_supply_property psp
;
91 [REG_MANUFACTURER_DATA
] =
92 SBS_DATA(POWER_SUPPLY_PROP_PRESENT
, 0x00, 0, 65535),
94 SBS_DATA(POWER_SUPPLY_PROP_TEMP
, 0x08, 0, 65535),
96 SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_NOW
, 0x09, 0, 20000),
98 SBS_DATA(POWER_SUPPLY_PROP_CURRENT_NOW
, 0x0A, -32768, 32767),
100 SBS_DATA(POWER_SUPPLY_PROP_CAPACITY
, 0x0D, 0, 100),
101 [REG_REMAINING_CAPACITY
] =
102 SBS_DATA(POWER_SUPPLY_PROP_ENERGY_NOW
, 0x0F, 0, 65535),
103 [REG_REMAINING_CAPACITY_CHARGE
] =
104 SBS_DATA(POWER_SUPPLY_PROP_CHARGE_NOW
, 0x0F, 0, 65535),
105 [REG_FULL_CHARGE_CAPACITY
] =
106 SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL
, 0x10, 0, 65535),
107 [REG_FULL_CHARGE_CAPACITY_CHARGE
] =
108 SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL
, 0x10, 0, 65535),
109 [REG_TIME_TO_EMPTY
] =
110 SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG
, 0x12, 0, 65535),
112 SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_FULL_AVG
, 0x13, 0, 65535),
114 SBS_DATA(POWER_SUPPLY_PROP_STATUS
, 0x16, 0, 65535),
115 [REG_CAPACITY_LEVEL
] =
116 SBS_DATA(POWER_SUPPLY_PROP_CAPACITY_LEVEL
, 0x16, 0, 65535),
118 SBS_DATA(POWER_SUPPLY_PROP_CYCLE_COUNT
, 0x17, 0, 65535),
119 [REG_DESIGN_CAPACITY
] =
120 SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN
, 0x18, 0, 65535),
121 [REG_DESIGN_CAPACITY_CHARGE
] =
122 SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
, 0x18, 0, 65535),
123 [REG_DESIGN_VOLTAGE_MIN
] =
124 SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
, 0x19, 0, 65535),
125 [REG_DESIGN_VOLTAGE_MAX
] =
126 SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN
, 0x19, 0, 65535),
127 [REG_SERIAL_NUMBER
] =
128 SBS_DATA(POWER_SUPPLY_PROP_SERIAL_NUMBER
, 0x1C, 0, 65535),
129 /* Properties of type `const char *' */
131 SBS_DATA(POWER_SUPPLY_PROP_MANUFACTURER
, 0x20, 0, 65535),
133 SBS_DATA(POWER_SUPPLY_PROP_MODEL_NAME
, 0x21, 0, 65535)
136 static enum power_supply_property sbs_properties
[] = {
137 POWER_SUPPLY_PROP_STATUS
,
138 POWER_SUPPLY_PROP_CAPACITY_LEVEL
,
139 POWER_SUPPLY_PROP_HEALTH
,
140 POWER_SUPPLY_PROP_PRESENT
,
141 POWER_SUPPLY_PROP_TECHNOLOGY
,
142 POWER_SUPPLY_PROP_CYCLE_COUNT
,
143 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
144 POWER_SUPPLY_PROP_CURRENT_NOW
,
145 POWER_SUPPLY_PROP_CAPACITY
,
146 POWER_SUPPLY_PROP_TEMP
,
147 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG
,
148 POWER_SUPPLY_PROP_TIME_TO_FULL_AVG
,
149 POWER_SUPPLY_PROP_SERIAL_NUMBER
,
150 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
,
151 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN
,
152 POWER_SUPPLY_PROP_ENERGY_NOW
,
153 POWER_SUPPLY_PROP_ENERGY_FULL
,
154 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN
,
155 POWER_SUPPLY_PROP_CHARGE_NOW
,
156 POWER_SUPPLY_PROP_CHARGE_FULL
,
157 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
,
158 /* Properties of type `const char *' */
159 POWER_SUPPLY_PROP_MANUFACTURER
,
160 POWER_SUPPLY_PROP_MODEL_NAME
164 struct i2c_client
*client
;
165 struct power_supply
*power_supply
;
167 struct gpio_desc
*gpio_detect
;
168 bool enable_detection
;
172 u32 poll_retry_count
;
173 struct delayed_work work
;
177 static char model_name
[I2C_SMBUS_BLOCK_MAX
+ 1];
178 static char manufacturer
[I2C_SMBUS_BLOCK_MAX
+ 1];
179 static bool force_load
;
181 static int sbs_read_word_data(struct i2c_client
*client
, u8 address
)
183 struct sbs_info
*chip
= i2c_get_clientdata(client
);
187 retries
= chip
->i2c_retry_count
;
189 while (retries
> 0) {
190 ret
= i2c_smbus_read_word_data(client
, address
);
197 dev_dbg(&client
->dev
,
198 "%s: i2c read at address 0x%x failed\n",
203 return le16_to_cpu(ret
);
206 static int sbs_read_string_data(struct i2c_client
*client
, u8 address
,
209 struct sbs_info
*chip
= i2c_get_clientdata(client
);
210 s32 ret
= 0, block_length
= 0;
211 int retries_length
= 1, retries_block
= 1;
212 u8 block_buffer
[I2C_SMBUS_BLOCK_MAX
+ 1];
214 retries_length
= chip
->i2c_retry_count
;
215 retries_block
= chip
->i2c_retry_count
;
217 /* Adapter needs to support these two functions */
218 if (!i2c_check_functionality(client
->adapter
,
219 I2C_FUNC_SMBUS_BYTE_DATA
|
220 I2C_FUNC_SMBUS_I2C_BLOCK
)){
224 /* Get the length of block data */
225 while (retries_length
> 0) {
226 ret
= i2c_smbus_read_byte_data(client
, address
);
233 dev_dbg(&client
->dev
,
234 "%s: i2c read at address 0x%x failed\n",
239 /* block_length does not include NULL terminator */
241 if (block_length
> I2C_SMBUS_BLOCK_MAX
) {
242 dev_err(&client
->dev
,
243 "%s: Returned block_length is longer than 0x%x\n",
244 __func__
, I2C_SMBUS_BLOCK_MAX
);
248 /* Get the block data */
249 while (retries_block
> 0) {
250 ret
= i2c_smbus_read_i2c_block_data(
252 block_length
+ 1, block_buffer
);
259 dev_dbg(&client
->dev
,
260 "%s: i2c read at address 0x%x failed\n",
265 /* block_buffer[0] == block_length */
266 memcpy(values
, block_buffer
+ 1, block_length
);
267 values
[block_length
] = '\0';
269 return le16_to_cpu(ret
);
272 static int sbs_write_word_data(struct i2c_client
*client
, u8 address
,
275 struct sbs_info
*chip
= i2c_get_clientdata(client
);
279 retries
= chip
->i2c_retry_count
;
281 while (retries
> 0) {
282 ret
= i2c_smbus_write_word_data(client
, address
,
290 dev_dbg(&client
->dev
,
291 "%s: i2c write to address 0x%x failed\n",
299 static int sbs_get_battery_presence_and_health(
300 struct i2c_client
*client
, enum power_supply_property psp
,
301 union power_supply_propval
*val
)
304 struct sbs_info
*chip
= i2c_get_clientdata(client
);
306 if (psp
== POWER_SUPPLY_PROP_PRESENT
&& chip
->gpio_detect
) {
307 ret
= gpiod_get_value_cansleep(chip
->gpio_detect
);
311 chip
->is_present
= val
->intval
;
316 * Write to ManufacturerAccess with ManufacturerAccess command
317 * and then read the status. Do not check for error on the write
318 * since not all batteries implement write access to this command,
319 * while others mandate it.
321 sbs_write_word_data(client
, sbs_data
[REG_MANUFACTURER_DATA
].addr
,
322 MANUFACTURER_ACCESS_STATUS
);
324 ret
= sbs_read_word_data(client
, sbs_data
[REG_MANUFACTURER_DATA
].addr
);
326 if (psp
== POWER_SUPPLY_PROP_PRESENT
)
327 val
->intval
= 0; /* battery removed */
331 if (ret
< sbs_data
[REG_MANUFACTURER_DATA
].min_value
||
332 ret
> sbs_data
[REG_MANUFACTURER_DATA
].max_value
) {
337 /* Mask the upper nibble of 2nd byte and
338 * lower byte of response then
339 * shift the result by 8 to get status*/
342 if (psp
== POWER_SUPPLY_PROP_PRESENT
) {
344 /* battery removed */
348 } else if (psp
== POWER_SUPPLY_PROP_HEALTH
) {
350 val
->intval
= POWER_SUPPLY_HEALTH_UNSPEC_FAILURE
;
351 else if (ret
== 0x0B)
352 val
->intval
= POWER_SUPPLY_HEALTH_OVERHEAT
;
353 else if (ret
== 0x0C)
354 val
->intval
= POWER_SUPPLY_HEALTH_DEAD
;
356 val
->intval
= POWER_SUPPLY_HEALTH_GOOD
;
362 static int sbs_get_battery_property(struct i2c_client
*client
,
363 int reg_offset
, enum power_supply_property psp
,
364 union power_supply_propval
*val
)
366 struct sbs_info
*chip
= i2c_get_clientdata(client
);
369 ret
= sbs_read_word_data(client
, sbs_data
[reg_offset
].addr
);
373 /* returned values are 16 bit */
374 if (sbs_data
[reg_offset
].min_value
< 0)
377 if (ret
>= sbs_data
[reg_offset
].min_value
&&
378 ret
<= sbs_data
[reg_offset
].max_value
) {
380 if (psp
== POWER_SUPPLY_PROP_CAPACITY_LEVEL
) {
381 if (!(ret
& BATTERY_INITIALIZED
))
383 POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN
;
384 else if (ret
& BATTERY_FULL_CHARGED
)
386 POWER_SUPPLY_CAPACITY_LEVEL_FULL
;
387 else if (ret
& BATTERY_FULL_DISCHARGED
)
389 POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL
;
392 POWER_SUPPLY_CAPACITY_LEVEL_NORMAL
;
394 } else if (psp
!= POWER_SUPPLY_PROP_STATUS
) {
398 if (ret
& BATTERY_FULL_CHARGED
)
399 val
->intval
= POWER_SUPPLY_STATUS_FULL
;
400 else if (ret
& BATTERY_DISCHARGING
)
401 val
->intval
= POWER_SUPPLY_STATUS_DISCHARGING
;
403 val
->intval
= POWER_SUPPLY_STATUS_CHARGING
;
405 if (chip
->poll_time
== 0)
406 chip
->last_state
= val
->intval
;
407 else if (chip
->last_state
!= val
->intval
) {
408 cancel_delayed_work_sync(&chip
->work
);
409 power_supply_changed(chip
->power_supply
);
413 if (psp
== POWER_SUPPLY_PROP_STATUS
)
414 val
->intval
= POWER_SUPPLY_STATUS_UNKNOWN
;
422 static int sbs_get_battery_string_property(struct i2c_client
*client
,
423 int reg_offset
, enum power_supply_property psp
, char *val
)
427 ret
= sbs_read_string_data(client
, sbs_data
[reg_offset
].addr
, val
);
435 static void sbs_unit_adjustment(struct i2c_client
*client
,
436 enum power_supply_property psp
, union power_supply_propval
*val
)
438 #define BASE_UNIT_CONVERSION 1000
439 #define BATTERY_MODE_CAP_MULT_WATT (10 * BASE_UNIT_CONVERSION)
440 #define TIME_UNIT_CONVERSION 60
441 #define TEMP_KELVIN_TO_CELSIUS 2731
443 case POWER_SUPPLY_PROP_ENERGY_NOW
:
444 case POWER_SUPPLY_PROP_ENERGY_FULL
:
445 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN
:
446 /* sbs provides energy in units of 10mWh.
449 val
->intval
*= BATTERY_MODE_CAP_MULT_WATT
;
452 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
453 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
:
454 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN
:
455 case POWER_SUPPLY_PROP_CURRENT_NOW
:
456 case POWER_SUPPLY_PROP_CHARGE_NOW
:
457 case POWER_SUPPLY_PROP_CHARGE_FULL
:
458 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
:
459 val
->intval
*= BASE_UNIT_CONVERSION
;
462 case POWER_SUPPLY_PROP_TEMP
:
463 /* sbs provides battery temperature in 0.1K
464 * so convert it to 0.1°C
466 val
->intval
-= TEMP_KELVIN_TO_CELSIUS
;
469 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG
:
470 case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG
:
471 /* sbs provides time to empty and time to full in minutes.
474 val
->intval
*= TIME_UNIT_CONVERSION
;
478 dev_dbg(&client
->dev
,
479 "%s: no need for unit conversion %d\n", __func__
, psp
);
483 static enum sbs_battery_mode
sbs_set_battery_mode(struct i2c_client
*client
,
484 enum sbs_battery_mode mode
)
486 int ret
, original_val
;
488 original_val
= sbs_read_word_data(client
, BATTERY_MODE_OFFSET
);
489 if (original_val
< 0)
492 if ((original_val
& BATTERY_MODE_MASK
) == mode
)
495 if (mode
== BATTERY_MODE_AMPS
)
496 ret
= original_val
& ~BATTERY_MODE_MASK
;
498 ret
= original_val
| BATTERY_MODE_MASK
;
500 ret
= sbs_write_word_data(client
, BATTERY_MODE_OFFSET
, ret
);
504 return original_val
& BATTERY_MODE_MASK
;
507 static int sbs_get_battery_capacity(struct i2c_client
*client
,
508 int reg_offset
, enum power_supply_property psp
,
509 union power_supply_propval
*val
)
512 enum sbs_battery_mode mode
= BATTERY_MODE_WATTS
;
514 if (power_supply_is_amp_property(psp
))
515 mode
= BATTERY_MODE_AMPS
;
517 mode
= sbs_set_battery_mode(client
, mode
);
521 ret
= sbs_read_word_data(client
, sbs_data
[reg_offset
].addr
);
525 if (psp
== POWER_SUPPLY_PROP_CAPACITY
) {
526 /* sbs spec says that this can be >100 %
527 * even if max value is 100 % */
528 val
->intval
= min(ret
, 100);
532 ret
= sbs_set_battery_mode(client
, mode
);
539 static char sbs_serial
[5];
540 static int sbs_get_battery_serial_number(struct i2c_client
*client
,
541 union power_supply_propval
*val
)
545 ret
= sbs_read_word_data(client
, sbs_data
[REG_SERIAL_NUMBER
].addr
);
549 ret
= sprintf(sbs_serial
, "%04x", ret
);
550 val
->strval
= sbs_serial
;
555 static int sbs_get_property_index(struct i2c_client
*client
,
556 enum power_supply_property psp
)
559 for (count
= 0; count
< ARRAY_SIZE(sbs_data
); count
++)
560 if (psp
== sbs_data
[count
].psp
)
563 dev_warn(&client
->dev
,
564 "%s: Invalid Property - %d\n", __func__
, psp
);
569 static int sbs_get_property(struct power_supply
*psy
,
570 enum power_supply_property psp
,
571 union power_supply_propval
*val
)
574 struct sbs_info
*chip
= power_supply_get_drvdata(psy
);
575 struct i2c_client
*client
= chip
->client
;
578 case POWER_SUPPLY_PROP_PRESENT
:
579 case POWER_SUPPLY_PROP_HEALTH
:
580 ret
= sbs_get_battery_presence_and_health(client
, psp
, val
);
581 if (psp
== POWER_SUPPLY_PROP_PRESENT
)
585 case POWER_SUPPLY_PROP_TECHNOLOGY
:
586 val
->intval
= POWER_SUPPLY_TECHNOLOGY_LION
;
587 goto done
; /* don't trigger power_supply_changed()! */
589 case POWER_SUPPLY_PROP_ENERGY_NOW
:
590 case POWER_SUPPLY_PROP_ENERGY_FULL
:
591 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN
:
592 case POWER_SUPPLY_PROP_CHARGE_NOW
:
593 case POWER_SUPPLY_PROP_CHARGE_FULL
:
594 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
:
595 case POWER_SUPPLY_PROP_CAPACITY
:
596 ret
= sbs_get_property_index(client
, psp
);
600 ret
= sbs_get_battery_capacity(client
, ret
, psp
, val
);
603 case POWER_SUPPLY_PROP_SERIAL_NUMBER
:
604 ret
= sbs_get_battery_serial_number(client
, val
);
607 case POWER_SUPPLY_PROP_STATUS
:
608 case POWER_SUPPLY_PROP_CAPACITY_LEVEL
:
609 case POWER_SUPPLY_PROP_CYCLE_COUNT
:
610 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
611 case POWER_SUPPLY_PROP_CURRENT_NOW
:
612 case POWER_SUPPLY_PROP_TEMP
:
613 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG
:
614 case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG
:
615 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
:
616 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN
:
617 ret
= sbs_get_property_index(client
, psp
);
621 ret
= sbs_get_battery_property(client
, ret
, psp
, val
);
624 case POWER_SUPPLY_PROP_MODEL_NAME
:
625 ret
= sbs_get_property_index(client
, psp
);
629 ret
= sbs_get_battery_string_property(client
, ret
, psp
,
631 val
->strval
= model_name
;
634 case POWER_SUPPLY_PROP_MANUFACTURER
:
635 ret
= sbs_get_property_index(client
, psp
);
639 ret
= sbs_get_battery_string_property(client
, ret
, psp
,
641 val
->strval
= manufacturer
;
645 dev_err(&client
->dev
,
646 "%s: INVALID property\n", __func__
);
650 if (!chip
->enable_detection
)
653 if (!chip
->gpio_detect
&&
654 chip
->is_present
!= (ret
>= 0)) {
655 chip
->is_present
= (ret
>= 0);
656 power_supply_changed(chip
->power_supply
);
661 /* Convert units to match requirements for power supply class */
662 sbs_unit_adjustment(client
, psp
, val
);
665 dev_dbg(&client
->dev
,
666 "%s: property = %d, value = %x\n", __func__
, psp
, val
->intval
);
668 if (ret
&& chip
->is_present
)
671 /* battery not present, so return NODATA for properties */
678 static irqreturn_t
sbs_irq(int irq
, void *devid
)
680 struct sbs_info
*chip
= devid
;
681 struct power_supply
*battery
= chip
->power_supply
;
684 ret
= gpiod_get_value_cansleep(chip
->gpio_detect
);
687 chip
->is_present
= ret
;
688 power_supply_changed(battery
);
693 static void sbs_external_power_changed(struct power_supply
*psy
)
695 struct sbs_info
*chip
= power_supply_get_drvdata(psy
);
697 if (chip
->ignore_changes
> 0) {
698 chip
->ignore_changes
--;
702 /* cancel outstanding work */
703 cancel_delayed_work_sync(&chip
->work
);
705 schedule_delayed_work(&chip
->work
, HZ
);
706 chip
->poll_time
= chip
->poll_retry_count
;
709 static void sbs_delayed_work(struct work_struct
*work
)
711 struct sbs_info
*chip
;
714 chip
= container_of(work
, struct sbs_info
, work
.work
);
716 ret
= sbs_read_word_data(chip
->client
, sbs_data
[REG_STATUS
].addr
);
717 /* if the read failed, give up on this work */
723 if (ret
& BATTERY_FULL_CHARGED
)
724 ret
= POWER_SUPPLY_STATUS_FULL
;
725 else if (ret
& BATTERY_DISCHARGING
)
726 ret
= POWER_SUPPLY_STATUS_DISCHARGING
;
728 ret
= POWER_SUPPLY_STATUS_CHARGING
;
730 if (chip
->last_state
!= ret
) {
732 power_supply_changed(chip
->power_supply
);
735 if (chip
->poll_time
> 0) {
736 schedule_delayed_work(&chip
->work
, HZ
);
742 static const struct power_supply_desc sbs_default_desc
= {
743 .type
= POWER_SUPPLY_TYPE_BATTERY
,
744 .properties
= sbs_properties
,
745 .num_properties
= ARRAY_SIZE(sbs_properties
),
746 .get_property
= sbs_get_property
,
747 .external_power_changed
= sbs_external_power_changed
,
750 static int sbs_probe(struct i2c_client
*client
,
751 const struct i2c_device_id
*id
)
753 struct sbs_info
*chip
;
754 struct power_supply_desc
*sbs_desc
;
755 struct sbs_platform_data
*pdata
= client
->dev
.platform_data
;
756 struct power_supply_config psy_cfg
= {};
760 sbs_desc
= devm_kmemdup(&client
->dev
, &sbs_default_desc
,
761 sizeof(*sbs_desc
), GFP_KERNEL
);
765 sbs_desc
->name
= devm_kasprintf(&client
->dev
, GFP_KERNEL
, "sbs-%s",
766 dev_name(&client
->dev
));
770 chip
= devm_kzalloc(&client
->dev
, sizeof(struct sbs_info
), GFP_KERNEL
);
774 chip
->client
= client
;
775 chip
->enable_detection
= false;
776 psy_cfg
.of_node
= client
->dev
.of_node
;
777 psy_cfg
.drv_data
= chip
;
778 /* ignore first notification of external change, it is generated
779 * from the power_supply_register call back
781 chip
->ignore_changes
= 1;
782 chip
->last_state
= POWER_SUPPLY_STATUS_UNKNOWN
;
784 /* use pdata if available, fall back to DT properties,
785 * or hardcoded defaults if not
787 rc
= of_property_read_u32(client
->dev
.of_node
, "sbs,i2c-retry-count",
788 &chip
->i2c_retry_count
);
790 chip
->i2c_retry_count
= 0;
792 rc
= of_property_read_u32(client
->dev
.of_node
, "sbs,poll-retry-count",
793 &chip
->poll_retry_count
);
795 chip
->poll_retry_count
= 0;
798 chip
->poll_retry_count
= pdata
->poll_retry_count
;
799 chip
->i2c_retry_count
= pdata
->i2c_retry_count
;
801 chip
->i2c_retry_count
= chip
->i2c_retry_count
+ 1;
803 chip
->gpio_detect
= devm_gpiod_get_optional(&client
->dev
,
804 "sbs,battery-detect", GPIOD_IN
);
805 if (IS_ERR(chip
->gpio_detect
)) {
806 dev_err(&client
->dev
, "Failed to get gpio: %ld\n",
807 PTR_ERR(chip
->gpio_detect
));
808 return PTR_ERR(chip
->gpio_detect
);
811 i2c_set_clientdata(client
, chip
);
813 if (!chip
->gpio_detect
)
816 irq
= gpiod_to_irq(chip
->gpio_detect
);
818 dev_warn(&client
->dev
, "Failed to get gpio as irq: %d\n", irq
);
822 rc
= devm_request_threaded_irq(&client
->dev
, irq
, NULL
, sbs_irq
,
823 IRQF_TRIGGER_RISING
| IRQF_TRIGGER_FALLING
,
824 dev_name(&client
->dev
), chip
);
826 dev_warn(&client
->dev
, "Failed to request irq: %d\n", rc
);
832 * Before we register, we might need to make sure we can actually talk
835 if (!(force_load
|| chip
->gpio_detect
)) {
836 rc
= sbs_read_word_data(client
, sbs_data
[REG_STATUS
].addr
);
839 dev_err(&client
->dev
, "%s: Failed to get device status\n",
845 chip
->power_supply
= devm_power_supply_register(&client
->dev
, sbs_desc
,
847 if (IS_ERR(chip
->power_supply
)) {
848 dev_err(&client
->dev
,
849 "%s: Failed to register power supply\n", __func__
);
850 rc
= PTR_ERR(chip
->power_supply
);
854 dev_info(&client
->dev
,
855 "%s: battery gas gauge device registered\n", client
->name
);
857 INIT_DELAYED_WORK(&chip
->work
, sbs_delayed_work
);
859 chip
->enable_detection
= true;
867 static int sbs_remove(struct i2c_client
*client
)
869 struct sbs_info
*chip
= i2c_get_clientdata(client
);
871 cancel_delayed_work_sync(&chip
->work
);
876 #if defined CONFIG_PM_SLEEP
878 static int sbs_suspend(struct device
*dev
)
880 struct i2c_client
*client
= to_i2c_client(dev
);
881 struct sbs_info
*chip
= i2c_get_clientdata(client
);
883 if (chip
->poll_time
> 0)
884 cancel_delayed_work_sync(&chip
->work
);
887 * Write to manufacturer access with sleep command.
888 * Support is manufacturer dependend, so ignore errors.
890 sbs_write_word_data(client
, sbs_data
[REG_MANUFACTURER_DATA
].addr
,
891 MANUFACTURER_ACCESS_SLEEP
);
896 static SIMPLE_DEV_PM_OPS(sbs_pm_ops
, sbs_suspend
, NULL
);
897 #define SBS_PM_OPS (&sbs_pm_ops)
900 #define SBS_PM_OPS NULL
903 static const struct i2c_device_id sbs_id
[] = {
905 { "sbs-battery", 1 },
908 MODULE_DEVICE_TABLE(i2c
, sbs_id
);
910 static const struct of_device_id sbs_dt_ids
[] = {
911 { .compatible
= "sbs,sbs-battery" },
912 { .compatible
= "ti,bq20z75" },
915 MODULE_DEVICE_TABLE(of
, sbs_dt_ids
);
917 static struct i2c_driver sbs_battery_driver
= {
919 .remove
= sbs_remove
,
922 .name
= "sbs-battery",
923 .of_match_table
= sbs_dt_ids
,
927 module_i2c_driver(sbs_battery_driver
);
929 MODULE_DESCRIPTION("SBS battery monitor driver");
930 MODULE_LICENSE("GPL");
932 module_param(force_load
, bool, S_IRUSR
| S_IRGRP
| S_IROTH
);
933 MODULE_PARM_DESC(force_load
,
934 "Attempt to load the driver even if no battery is connected");