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.h>
31 #include <linux/stat.h>
33 #include <linux/power/sbs-battery.h>
36 REG_MANUFACTURER_DATA
,
46 REG_REMAINING_CAPACITY
,
47 REG_REMAINING_CAPACITY_CHARGE
,
48 REG_FULL_CHARGE_CAPACITY
,
49 REG_FULL_CHARGE_CAPACITY_CHARGE
,
51 REG_DESIGN_CAPACITY_CHARGE
,
52 REG_DESIGN_VOLTAGE_MIN
,
53 REG_DESIGN_VOLTAGE_MAX
,
58 /* Battery Mode defines */
59 #define BATTERY_MODE_OFFSET 0x03
60 #define BATTERY_MODE_MASK 0x8000
61 enum sbs_battery_mode
{
66 /* manufacturer access defines */
67 #define MANUFACTURER_ACCESS_STATUS 0x0006
68 #define MANUFACTURER_ACCESS_SLEEP 0x0011
70 /* battery status value bits */
71 #define BATTERY_DISCHARGING 0x40
72 #define BATTERY_FULL_CHARGED 0x20
73 #define BATTERY_FULL_DISCHARGED 0x10
75 /* min_value and max_value are only valid for numerical data */
76 #define SBS_DATA(_psp, _addr, _min_value, _max_value) { \
79 .min_value = _min_value, \
80 .max_value = _max_value, \
83 static const struct chip_data
{
84 enum power_supply_property psp
;
89 [REG_MANUFACTURER_DATA
] =
90 SBS_DATA(POWER_SUPPLY_PROP_PRESENT
, 0x00, 0, 65535),
92 SBS_DATA(POWER_SUPPLY_PROP_TEMP
, 0x08, 0, 65535),
94 SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_NOW
, 0x09, 0, 20000),
96 SBS_DATA(POWER_SUPPLY_PROP_CURRENT_NOW
, 0x0A, -32768, 32767),
98 SBS_DATA(POWER_SUPPLY_PROP_CAPACITY
, 0x0D, 0, 100),
99 [REG_REMAINING_CAPACITY
] =
100 SBS_DATA(POWER_SUPPLY_PROP_ENERGY_NOW
, 0x0F, 0, 65535),
101 [REG_REMAINING_CAPACITY_CHARGE
] =
102 SBS_DATA(POWER_SUPPLY_PROP_CHARGE_NOW
, 0x0F, 0, 65535),
103 [REG_FULL_CHARGE_CAPACITY
] =
104 SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL
, 0x10, 0, 65535),
105 [REG_FULL_CHARGE_CAPACITY_CHARGE
] =
106 SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL
, 0x10, 0, 65535),
107 [REG_TIME_TO_EMPTY
] =
108 SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG
, 0x12, 0, 65535),
110 SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_FULL_AVG
, 0x13, 0, 65535),
112 SBS_DATA(POWER_SUPPLY_PROP_STATUS
, 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_HEALTH
,
135 POWER_SUPPLY_PROP_PRESENT
,
136 POWER_SUPPLY_PROP_TECHNOLOGY
,
137 POWER_SUPPLY_PROP_CYCLE_COUNT
,
138 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
139 POWER_SUPPLY_PROP_CURRENT_NOW
,
140 POWER_SUPPLY_PROP_CAPACITY
,
141 POWER_SUPPLY_PROP_TEMP
,
142 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG
,
143 POWER_SUPPLY_PROP_TIME_TO_FULL_AVG
,
144 POWER_SUPPLY_PROP_SERIAL_NUMBER
,
145 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
,
146 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN
,
147 POWER_SUPPLY_PROP_ENERGY_NOW
,
148 POWER_SUPPLY_PROP_ENERGY_FULL
,
149 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN
,
150 POWER_SUPPLY_PROP_CHARGE_NOW
,
151 POWER_SUPPLY_PROP_CHARGE_FULL
,
152 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
,
153 /* Properties of type `const char *' */
154 POWER_SUPPLY_PROP_MANUFACTURER
,
155 POWER_SUPPLY_PROP_MODEL_NAME
159 struct i2c_client
*client
;
160 struct power_supply
*power_supply
;
161 struct sbs_platform_data
*pdata
;
164 bool enable_detection
;
168 struct delayed_work work
;
172 static char model_name
[I2C_SMBUS_BLOCK_MAX
+ 1];
173 static char manufacturer
[I2C_SMBUS_BLOCK_MAX
+ 1];
174 static bool force_load
;
176 static int sbs_read_word_data(struct i2c_client
*client
, u8 address
)
178 struct sbs_info
*chip
= i2c_get_clientdata(client
);
183 retries
= max(chip
->pdata
->i2c_retry_count
+ 1, 1);
185 while (retries
> 0) {
186 ret
= i2c_smbus_read_word_data(client
, address
);
193 dev_dbg(&client
->dev
,
194 "%s: i2c read at address 0x%x failed\n",
199 return le16_to_cpu(ret
);
202 static int sbs_read_string_data(struct i2c_client
*client
, u8 address
,
205 struct sbs_info
*chip
= i2c_get_clientdata(client
);
206 s32 ret
= 0, block_length
= 0;
207 int retries_length
= 1, retries_block
= 1;
208 u8 block_buffer
[I2C_SMBUS_BLOCK_MAX
+ 1];
211 retries_length
= max(chip
->pdata
->i2c_retry_count
+ 1, 1);
212 retries_block
= max(chip
->pdata
->i2c_retry_count
+ 1, 1);
215 /* Adapter needs to support these two functions */
216 if (!i2c_check_functionality(client
->adapter
,
217 I2C_FUNC_SMBUS_BYTE_DATA
|
218 I2C_FUNC_SMBUS_I2C_BLOCK
)){
222 /* Get the length of block data */
223 while (retries_length
> 0) {
224 ret
= i2c_smbus_read_byte_data(client
, address
);
231 dev_dbg(&client
->dev
,
232 "%s: i2c read at address 0x%x failed\n",
237 /* block_length does not include NULL terminator */
239 if (block_length
> I2C_SMBUS_BLOCK_MAX
) {
240 dev_err(&client
->dev
,
241 "%s: Returned block_length is longer than 0x%x\n",
242 __func__
, I2C_SMBUS_BLOCK_MAX
);
246 /* Get the block data */
247 while (retries_block
> 0) {
248 ret
= i2c_smbus_read_i2c_block_data(
250 block_length
+ 1, block_buffer
);
257 dev_dbg(&client
->dev
,
258 "%s: i2c read at address 0x%x failed\n",
263 /* block_buffer[0] == block_length */
264 memcpy(values
, block_buffer
+ 1, block_length
);
265 values
[block_length
] = '\0';
267 return le16_to_cpu(ret
);
270 static int sbs_write_word_data(struct i2c_client
*client
, u8 address
,
273 struct sbs_info
*chip
= i2c_get_clientdata(client
);
278 retries
= max(chip
->pdata
->i2c_retry_count
+ 1, 1);
280 while (retries
> 0) {
281 ret
= i2c_smbus_write_word_data(client
, address
,
289 dev_dbg(&client
->dev
,
290 "%s: i2c write to address 0x%x failed\n",
298 static int sbs_get_battery_presence_and_health(
299 struct i2c_client
*client
, enum power_supply_property psp
,
300 union power_supply_propval
*val
)
303 struct sbs_info
*chip
= i2c_get_clientdata(client
);
305 if (psp
== POWER_SUPPLY_PROP_PRESENT
&&
307 ret
= gpio_get_value(chip
->pdata
->battery_detect
);
308 if (ret
== chip
->pdata
->battery_detect_present
)
312 chip
->is_present
= val
->intval
;
316 /* Write to ManufacturerAccess with
317 * ManufacturerAccess command and then
319 ret
= sbs_write_word_data(client
, sbs_data
[REG_MANUFACTURER_DATA
].addr
,
320 MANUFACTURER_ACCESS_STATUS
);
322 if (psp
== POWER_SUPPLY_PROP_PRESENT
)
323 val
->intval
= 0; /* battery removed */
327 ret
= sbs_read_word_data(client
, sbs_data
[REG_MANUFACTURER_DATA
].addr
);
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_STATUS
)
383 if (ret
& BATTERY_FULL_CHARGED
)
384 val
->intval
= POWER_SUPPLY_STATUS_FULL
;
385 else if (ret
& BATTERY_FULL_DISCHARGED
)
386 val
->intval
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
387 else if (ret
& BATTERY_DISCHARGING
)
388 val
->intval
= POWER_SUPPLY_STATUS_DISCHARGING
;
390 val
->intval
= POWER_SUPPLY_STATUS_CHARGING
;
392 if (chip
->poll_time
== 0)
393 chip
->last_state
= val
->intval
;
394 else if (chip
->last_state
!= val
->intval
) {
395 cancel_delayed_work_sync(&chip
->work
);
396 power_supply_changed(chip
->power_supply
);
400 if (psp
== POWER_SUPPLY_PROP_STATUS
)
401 val
->intval
= POWER_SUPPLY_STATUS_UNKNOWN
;
409 static int sbs_get_battery_string_property(struct i2c_client
*client
,
410 int reg_offset
, enum power_supply_property psp
, char *val
)
414 ret
= sbs_read_string_data(client
, sbs_data
[reg_offset
].addr
, val
);
422 static void sbs_unit_adjustment(struct i2c_client
*client
,
423 enum power_supply_property psp
, union power_supply_propval
*val
)
425 #define BASE_UNIT_CONVERSION 1000
426 #define BATTERY_MODE_CAP_MULT_WATT (10 * BASE_UNIT_CONVERSION)
427 #define TIME_UNIT_CONVERSION 60
428 #define TEMP_KELVIN_TO_CELSIUS 2731
430 case POWER_SUPPLY_PROP_ENERGY_NOW
:
431 case POWER_SUPPLY_PROP_ENERGY_FULL
:
432 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN
:
433 /* sbs provides energy in units of 10mWh.
436 val
->intval
*= BATTERY_MODE_CAP_MULT_WATT
;
439 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
440 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
:
441 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN
:
442 case POWER_SUPPLY_PROP_CURRENT_NOW
:
443 case POWER_SUPPLY_PROP_CHARGE_NOW
:
444 case POWER_SUPPLY_PROP_CHARGE_FULL
:
445 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
:
446 val
->intval
*= BASE_UNIT_CONVERSION
;
449 case POWER_SUPPLY_PROP_TEMP
:
450 /* sbs provides battery temperature in 0.1K
451 * so convert it to 0.1°C
453 val
->intval
-= TEMP_KELVIN_TO_CELSIUS
;
456 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG
:
457 case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG
:
458 /* sbs provides time to empty and time to full in minutes.
461 val
->intval
*= TIME_UNIT_CONVERSION
;
465 dev_dbg(&client
->dev
,
466 "%s: no need for unit conversion %d\n", __func__
, psp
);
470 static enum sbs_battery_mode
sbs_set_battery_mode(struct i2c_client
*client
,
471 enum sbs_battery_mode mode
)
473 int ret
, original_val
;
475 original_val
= sbs_read_word_data(client
, BATTERY_MODE_OFFSET
);
476 if (original_val
< 0)
479 if ((original_val
& BATTERY_MODE_MASK
) == mode
)
482 if (mode
== BATTERY_MODE_AMPS
)
483 ret
= original_val
& ~BATTERY_MODE_MASK
;
485 ret
= original_val
| BATTERY_MODE_MASK
;
487 ret
= sbs_write_word_data(client
, BATTERY_MODE_OFFSET
, ret
);
491 return original_val
& BATTERY_MODE_MASK
;
494 static int sbs_get_battery_capacity(struct i2c_client
*client
,
495 int reg_offset
, enum power_supply_property psp
,
496 union power_supply_propval
*val
)
499 enum sbs_battery_mode mode
= BATTERY_MODE_WATTS
;
501 if (power_supply_is_amp_property(psp
))
502 mode
= BATTERY_MODE_AMPS
;
504 mode
= sbs_set_battery_mode(client
, mode
);
508 ret
= sbs_read_word_data(client
, sbs_data
[reg_offset
].addr
);
512 if (psp
== POWER_SUPPLY_PROP_CAPACITY
) {
513 /* sbs spec says that this can be >100 %
514 * even if max value is 100 % */
515 val
->intval
= min(ret
, 100);
519 ret
= sbs_set_battery_mode(client
, mode
);
526 static char sbs_serial
[5];
527 static int sbs_get_battery_serial_number(struct i2c_client
*client
,
528 union power_supply_propval
*val
)
532 ret
= sbs_read_word_data(client
, sbs_data
[REG_SERIAL_NUMBER
].addr
);
536 ret
= sprintf(sbs_serial
, "%04x", ret
);
537 val
->strval
= sbs_serial
;
542 static int sbs_get_property_index(struct i2c_client
*client
,
543 enum power_supply_property psp
)
546 for (count
= 0; count
< ARRAY_SIZE(sbs_data
); count
++)
547 if (psp
== sbs_data
[count
].psp
)
550 dev_warn(&client
->dev
,
551 "%s: Invalid Property - %d\n", __func__
, psp
);
556 static int sbs_get_property(struct power_supply
*psy
,
557 enum power_supply_property psp
,
558 union power_supply_propval
*val
)
561 struct sbs_info
*chip
= power_supply_get_drvdata(psy
);
562 struct i2c_client
*client
= chip
->client
;
565 case POWER_SUPPLY_PROP_PRESENT
:
566 case POWER_SUPPLY_PROP_HEALTH
:
567 ret
= sbs_get_battery_presence_and_health(client
, psp
, val
);
568 if (psp
== POWER_SUPPLY_PROP_PRESENT
)
572 case POWER_SUPPLY_PROP_TECHNOLOGY
:
573 val
->intval
= POWER_SUPPLY_TECHNOLOGY_LION
;
574 goto done
; /* don't trigger power_supply_changed()! */
576 case POWER_SUPPLY_PROP_ENERGY_NOW
:
577 case POWER_SUPPLY_PROP_ENERGY_FULL
:
578 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN
:
579 case POWER_SUPPLY_PROP_CHARGE_NOW
:
580 case POWER_SUPPLY_PROP_CHARGE_FULL
:
581 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
:
582 case POWER_SUPPLY_PROP_CAPACITY
:
583 ret
= sbs_get_property_index(client
, psp
);
587 ret
= sbs_get_battery_capacity(client
, ret
, psp
, val
);
590 case POWER_SUPPLY_PROP_SERIAL_NUMBER
:
591 ret
= sbs_get_battery_serial_number(client
, val
);
594 case POWER_SUPPLY_PROP_STATUS
:
595 case POWER_SUPPLY_PROP_CYCLE_COUNT
:
596 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
597 case POWER_SUPPLY_PROP_CURRENT_NOW
:
598 case POWER_SUPPLY_PROP_TEMP
:
599 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG
:
600 case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG
:
601 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
:
602 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN
:
603 ret
= sbs_get_property_index(client
, psp
);
607 ret
= sbs_get_battery_property(client
, ret
, psp
, val
);
610 case POWER_SUPPLY_PROP_MODEL_NAME
:
611 ret
= sbs_get_property_index(client
, psp
);
615 ret
= sbs_get_battery_string_property(client
, ret
, psp
,
617 val
->strval
= model_name
;
620 case POWER_SUPPLY_PROP_MANUFACTURER
:
621 ret
= sbs_get_property_index(client
, psp
);
625 ret
= sbs_get_battery_string_property(client
, ret
, psp
,
627 val
->strval
= manufacturer
;
631 dev_err(&client
->dev
,
632 "%s: INVALID property\n", __func__
);
636 if (!chip
->enable_detection
)
639 if (!chip
->gpio_detect
&&
640 chip
->is_present
!= (ret
>= 0)) {
641 chip
->is_present
= (ret
>= 0);
642 power_supply_changed(chip
->power_supply
);
647 /* Convert units to match requirements for power supply class */
648 sbs_unit_adjustment(client
, psp
, val
);
651 dev_dbg(&client
->dev
,
652 "%s: property = %d, value = %x\n", __func__
, psp
, val
->intval
);
654 if (ret
&& chip
->is_present
)
657 /* battery not present, so return NODATA for properties */
664 static irqreturn_t
sbs_irq(int irq
, void *devid
)
666 struct power_supply
*battery
= devid
;
668 power_supply_changed(battery
);
673 static void sbs_external_power_changed(struct power_supply
*psy
)
675 struct sbs_info
*chip
= power_supply_get_drvdata(psy
);
677 if (chip
->ignore_changes
> 0) {
678 chip
->ignore_changes
--;
682 /* cancel outstanding work */
683 cancel_delayed_work_sync(&chip
->work
);
685 schedule_delayed_work(&chip
->work
, HZ
);
686 chip
->poll_time
= chip
->pdata
->poll_retry_count
;
689 static void sbs_delayed_work(struct work_struct
*work
)
691 struct sbs_info
*chip
;
694 chip
= container_of(work
, struct sbs_info
, work
.work
);
696 ret
= sbs_read_word_data(chip
->client
, sbs_data
[REG_STATUS
].addr
);
697 /* if the read failed, give up on this work */
703 if (ret
& BATTERY_FULL_CHARGED
)
704 ret
= POWER_SUPPLY_STATUS_FULL
;
705 else if (ret
& BATTERY_FULL_DISCHARGED
)
706 ret
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
707 else if (ret
& BATTERY_DISCHARGING
)
708 ret
= POWER_SUPPLY_STATUS_DISCHARGING
;
710 ret
= POWER_SUPPLY_STATUS_CHARGING
;
712 if (chip
->last_state
!= ret
) {
714 power_supply_changed(chip
->power_supply
);
717 if (chip
->poll_time
> 0) {
718 schedule_delayed_work(&chip
->work
, HZ
);
724 #if defined(CONFIG_OF)
726 #include <linux/of_device.h>
727 #include <linux/of_gpio.h>
729 static const struct of_device_id sbs_dt_ids
[] = {
730 { .compatible
= "sbs,sbs-battery" },
731 { .compatible
= "ti,bq20z75" },
734 MODULE_DEVICE_TABLE(of
, sbs_dt_ids
);
736 static struct sbs_platform_data
*sbs_of_populate_pdata(
737 struct i2c_client
*client
)
739 struct device_node
*of_node
= client
->dev
.of_node
;
740 struct sbs_platform_data
*pdata
= client
->dev
.platform_data
;
741 enum of_gpio_flags gpio_flags
;
745 /* verify this driver matches this device */
749 /* if platform data is set, honor it */
753 /* first make sure at least one property is set, otherwise
754 * it won't change behavior from running without pdata.
756 if (!of_get_property(of_node
, "sbs,i2c-retry-count", NULL
) &&
757 !of_get_property(of_node
, "sbs,poll-retry-count", NULL
) &&
758 !of_get_property(of_node
, "sbs,battery-detect-gpios", NULL
))
761 pdata
= devm_kzalloc(&client
->dev
, sizeof(struct sbs_platform_data
),
766 rc
= of_property_read_u32(of_node
, "sbs,i2c-retry-count", &prop
);
768 pdata
->i2c_retry_count
= prop
;
770 rc
= of_property_read_u32(of_node
, "sbs,poll-retry-count", &prop
);
772 pdata
->poll_retry_count
= prop
;
774 if (!of_get_property(of_node
, "sbs,battery-detect-gpios", NULL
)) {
775 pdata
->battery_detect
= -1;
779 pdata
->battery_detect
= of_get_named_gpio_flags(of_node
,
780 "sbs,battery-detect-gpios", 0, &gpio_flags
);
782 if (gpio_flags
& OF_GPIO_ACTIVE_LOW
)
783 pdata
->battery_detect_present
= 0;
785 pdata
->battery_detect_present
= 1;
791 static struct sbs_platform_data
*sbs_of_populate_pdata(
792 struct i2c_client
*client
)
794 return client
->dev
.platform_data
;
798 static const struct power_supply_desc sbs_default_desc
= {
799 .type
= POWER_SUPPLY_TYPE_BATTERY
,
800 .properties
= sbs_properties
,
801 .num_properties
= ARRAY_SIZE(sbs_properties
),
802 .get_property
= sbs_get_property
,
803 .external_power_changed
= sbs_external_power_changed
,
806 static int sbs_probe(struct i2c_client
*client
,
807 const struct i2c_device_id
*id
)
809 struct sbs_info
*chip
;
810 struct power_supply_desc
*sbs_desc
;
811 struct sbs_platform_data
*pdata
= client
->dev
.platform_data
;
812 struct power_supply_config psy_cfg
= {};
816 sbs_desc
= devm_kmemdup(&client
->dev
, &sbs_default_desc
,
817 sizeof(*sbs_desc
), GFP_KERNEL
);
821 sbs_desc
->name
= devm_kasprintf(&client
->dev
, GFP_KERNEL
, "sbs-%s",
822 dev_name(&client
->dev
));
826 chip
= kzalloc(sizeof(struct sbs_info
), GFP_KERNEL
);
830 chip
->client
= client
;
831 chip
->enable_detection
= false;
832 chip
->gpio_detect
= false;
833 psy_cfg
.of_node
= client
->dev
.of_node
;
834 psy_cfg
.drv_data
= chip
;
835 /* ignore first notification of external change, it is generated
836 * from the power_supply_register call back
838 chip
->ignore_changes
= 1;
839 chip
->last_state
= POWER_SUPPLY_STATUS_UNKNOWN
;
841 pdata
= sbs_of_populate_pdata(client
);
844 chip
->gpio_detect
= gpio_is_valid(pdata
->battery_detect
);
848 i2c_set_clientdata(client
, chip
);
850 if (!chip
->gpio_detect
)
853 rc
= gpio_request(pdata
->battery_detect
, dev_name(&client
->dev
));
855 dev_warn(&client
->dev
, "Failed to request gpio: %d\n", rc
);
856 chip
->gpio_detect
= false;
860 rc
= gpio_direction_input(pdata
->battery_detect
);
862 dev_warn(&client
->dev
, "Failed to get gpio as input: %d\n", rc
);
863 gpio_free(pdata
->battery_detect
);
864 chip
->gpio_detect
= false;
868 irq
= gpio_to_irq(pdata
->battery_detect
);
870 dev_warn(&client
->dev
, "Failed to get gpio as irq: %d\n", irq
);
871 gpio_free(pdata
->battery_detect
);
872 chip
->gpio_detect
= false;
876 rc
= request_irq(irq
, sbs_irq
,
877 IRQF_TRIGGER_RISING
| IRQF_TRIGGER_FALLING
,
878 dev_name(&client
->dev
), chip
->power_supply
);
880 dev_warn(&client
->dev
, "Failed to request irq: %d\n", rc
);
881 gpio_free(pdata
->battery_detect
);
882 chip
->gpio_detect
= false;
890 * Before we register, we might need to make sure we can actually talk
894 rc
= sbs_read_word_data(client
, sbs_data
[REG_STATUS
].addr
);
897 dev_err(&client
->dev
, "%s: Failed to get device status\n",
903 chip
->power_supply
= power_supply_register(&client
->dev
, sbs_desc
,
905 if (IS_ERR(chip
->power_supply
)) {
906 dev_err(&client
->dev
,
907 "%s: Failed to register power supply\n", __func__
);
908 rc
= PTR_ERR(chip
->power_supply
);
912 dev_info(&client
->dev
,
913 "%s: battery gas gauge device registered\n", client
->name
);
915 INIT_DELAYED_WORK(&chip
->work
, sbs_delayed_work
);
917 chip
->enable_detection
= true;
923 free_irq(chip
->irq
, chip
->power_supply
);
924 if (chip
->gpio_detect
)
925 gpio_free(pdata
->battery_detect
);
932 static int sbs_remove(struct i2c_client
*client
)
934 struct sbs_info
*chip
= i2c_get_clientdata(client
);
937 free_irq(chip
->irq
, chip
->power_supply
);
938 if (chip
->gpio_detect
)
939 gpio_free(chip
->pdata
->battery_detect
);
941 power_supply_unregister(chip
->power_supply
);
943 cancel_delayed_work_sync(&chip
->work
);
951 #if defined CONFIG_PM_SLEEP
953 static int sbs_suspend(struct device
*dev
)
955 struct i2c_client
*client
= to_i2c_client(dev
);
956 struct sbs_info
*chip
= i2c_get_clientdata(client
);
959 if (chip
->poll_time
> 0)
960 cancel_delayed_work_sync(&chip
->work
);
962 /* write to manufacturer access with sleep command */
963 ret
= sbs_write_word_data(client
, sbs_data
[REG_MANUFACTURER_DATA
].addr
,
964 MANUFACTURER_ACCESS_SLEEP
);
965 if (chip
->is_present
&& ret
< 0)
971 static SIMPLE_DEV_PM_OPS(sbs_pm_ops
, sbs_suspend
, NULL
);
972 #define SBS_PM_OPS (&sbs_pm_ops)
975 #define SBS_PM_OPS NULL
978 static const struct i2c_device_id sbs_id
[] = {
980 { "sbs-battery", 1 },
983 MODULE_DEVICE_TABLE(i2c
, sbs_id
);
985 static struct i2c_driver sbs_battery_driver
= {
987 .remove
= sbs_remove
,
990 .name
= "sbs-battery",
991 .of_match_table
= of_match_ptr(sbs_dt_ids
),
995 module_i2c_driver(sbs_battery_driver
);
997 MODULE_DESCRIPTION("SBS battery monitor driver");
998 MODULE_LICENSE("GPL");
1000 module_param(force_load
, bool, S_IRUSR
| S_IRGRP
| S_IROTH
);
1001 MODULE_PARM_DESC(force_load
,
1002 "Attempt to load the driver even if no battery is connected");