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
;
166 struct sbs_platform_data
*pdata
;
168 struct gpio_desc
*gpio_detect
;
169 bool enable_detection
;
172 struct delayed_work work
;
176 static char model_name
[I2C_SMBUS_BLOCK_MAX
+ 1];
177 static char manufacturer
[I2C_SMBUS_BLOCK_MAX
+ 1];
178 static bool force_load
;
180 static int sbs_read_word_data(struct i2c_client
*client
, u8 address
)
182 struct sbs_info
*chip
= i2c_get_clientdata(client
);
187 retries
= max(chip
->pdata
->i2c_retry_count
+ 1, 1);
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];
215 retries_length
= max(chip
->pdata
->i2c_retry_count
+ 1, 1);
216 retries_block
= max(chip
->pdata
->i2c_retry_count
+ 1, 1);
219 /* Adapter needs to support these two functions */
220 if (!i2c_check_functionality(client
->adapter
,
221 I2C_FUNC_SMBUS_BYTE_DATA
|
222 I2C_FUNC_SMBUS_I2C_BLOCK
)){
226 /* Get the length of block data */
227 while (retries_length
> 0) {
228 ret
= i2c_smbus_read_byte_data(client
, address
);
235 dev_dbg(&client
->dev
,
236 "%s: i2c read at address 0x%x failed\n",
241 /* block_length does not include NULL terminator */
243 if (block_length
> I2C_SMBUS_BLOCK_MAX
) {
244 dev_err(&client
->dev
,
245 "%s: Returned block_length is longer than 0x%x\n",
246 __func__
, I2C_SMBUS_BLOCK_MAX
);
250 /* Get the block data */
251 while (retries_block
> 0) {
252 ret
= i2c_smbus_read_i2c_block_data(
254 block_length
+ 1, block_buffer
);
261 dev_dbg(&client
->dev
,
262 "%s: i2c read at address 0x%x failed\n",
267 /* block_buffer[0] == block_length */
268 memcpy(values
, block_buffer
+ 1, block_length
);
269 values
[block_length
] = '\0';
271 return le16_to_cpu(ret
);
274 static int sbs_write_word_data(struct i2c_client
*client
, u8 address
,
277 struct sbs_info
*chip
= i2c_get_clientdata(client
);
282 retries
= max(chip
->pdata
->i2c_retry_count
+ 1, 1);
284 while (retries
> 0) {
285 ret
= i2c_smbus_write_word_data(client
, address
,
293 dev_dbg(&client
->dev
,
294 "%s: i2c write to address 0x%x failed\n",
302 static int sbs_get_battery_presence_and_health(
303 struct i2c_client
*client
, enum power_supply_property psp
,
304 union power_supply_propval
*val
)
307 struct sbs_info
*chip
= i2c_get_clientdata(client
);
309 if (psp
== POWER_SUPPLY_PROP_PRESENT
&& chip
->gpio_detect
) {
310 ret
= gpiod_get_value_cansleep(chip
->gpio_detect
);
314 chip
->is_present
= val
->intval
;
318 /* Write to ManufacturerAccess with
319 * ManufacturerAccess command and then
321 ret
= sbs_write_word_data(client
, sbs_data
[REG_MANUFACTURER_DATA
].addr
,
322 MANUFACTURER_ACCESS_STATUS
);
324 if (psp
== POWER_SUPPLY_PROP_PRESENT
)
325 val
->intval
= 0; /* battery removed */
329 ret
= sbs_read_word_data(client
, sbs_data
[REG_MANUFACTURER_DATA
].addr
);
333 if (ret
< sbs_data
[REG_MANUFACTURER_DATA
].min_value
||
334 ret
> sbs_data
[REG_MANUFACTURER_DATA
].max_value
) {
339 /* Mask the upper nibble of 2nd byte and
340 * lower byte of response then
341 * shift the result by 8 to get status*/
344 if (psp
== POWER_SUPPLY_PROP_PRESENT
) {
346 /* battery removed */
350 } else if (psp
== POWER_SUPPLY_PROP_HEALTH
) {
352 val
->intval
= POWER_SUPPLY_HEALTH_UNSPEC_FAILURE
;
353 else if (ret
== 0x0B)
354 val
->intval
= POWER_SUPPLY_HEALTH_OVERHEAT
;
355 else if (ret
== 0x0C)
356 val
->intval
= POWER_SUPPLY_HEALTH_DEAD
;
358 val
->intval
= POWER_SUPPLY_HEALTH_GOOD
;
364 static int sbs_get_battery_property(struct i2c_client
*client
,
365 int reg_offset
, enum power_supply_property psp
,
366 union power_supply_propval
*val
)
368 struct sbs_info
*chip
= i2c_get_clientdata(client
);
371 ret
= sbs_read_word_data(client
, sbs_data
[reg_offset
].addr
);
375 /* returned values are 16 bit */
376 if (sbs_data
[reg_offset
].min_value
< 0)
379 if (ret
>= sbs_data
[reg_offset
].min_value
&&
380 ret
<= sbs_data
[reg_offset
].max_value
) {
382 if (psp
== POWER_SUPPLY_PROP_CAPACITY_LEVEL
) {
383 if (!(ret
& BATTERY_INITIALIZED
))
385 POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN
;
386 else if (ret
& BATTERY_FULL_CHARGED
)
388 POWER_SUPPLY_CAPACITY_LEVEL_FULL
;
389 else if (ret
& BATTERY_FULL_DISCHARGED
)
391 POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL
;
394 POWER_SUPPLY_CAPACITY_LEVEL_NORMAL
;
396 } else if (psp
!= POWER_SUPPLY_PROP_STATUS
) {
400 if (ret
& BATTERY_FULL_CHARGED
)
401 val
->intval
= POWER_SUPPLY_STATUS_FULL
;
402 else if (ret
& BATTERY_DISCHARGING
)
403 val
->intval
= POWER_SUPPLY_STATUS_DISCHARGING
;
405 val
->intval
= POWER_SUPPLY_STATUS_CHARGING
;
407 if (chip
->poll_time
== 0)
408 chip
->last_state
= val
->intval
;
409 else if (chip
->last_state
!= val
->intval
) {
410 cancel_delayed_work_sync(&chip
->work
);
411 power_supply_changed(chip
->power_supply
);
415 if (psp
== POWER_SUPPLY_PROP_STATUS
)
416 val
->intval
= POWER_SUPPLY_STATUS_UNKNOWN
;
424 static int sbs_get_battery_string_property(struct i2c_client
*client
,
425 int reg_offset
, enum power_supply_property psp
, char *val
)
429 ret
= sbs_read_string_data(client
, sbs_data
[reg_offset
].addr
, val
);
437 static void sbs_unit_adjustment(struct i2c_client
*client
,
438 enum power_supply_property psp
, union power_supply_propval
*val
)
440 #define BASE_UNIT_CONVERSION 1000
441 #define BATTERY_MODE_CAP_MULT_WATT (10 * BASE_UNIT_CONVERSION)
442 #define TIME_UNIT_CONVERSION 60
443 #define TEMP_KELVIN_TO_CELSIUS 2731
445 case POWER_SUPPLY_PROP_ENERGY_NOW
:
446 case POWER_SUPPLY_PROP_ENERGY_FULL
:
447 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN
:
448 /* sbs provides energy in units of 10mWh.
451 val
->intval
*= BATTERY_MODE_CAP_MULT_WATT
;
454 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
455 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
:
456 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN
:
457 case POWER_SUPPLY_PROP_CURRENT_NOW
:
458 case POWER_SUPPLY_PROP_CHARGE_NOW
:
459 case POWER_SUPPLY_PROP_CHARGE_FULL
:
460 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
:
461 val
->intval
*= BASE_UNIT_CONVERSION
;
464 case POWER_SUPPLY_PROP_TEMP
:
465 /* sbs provides battery temperature in 0.1K
466 * so convert it to 0.1°C
468 val
->intval
-= TEMP_KELVIN_TO_CELSIUS
;
471 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG
:
472 case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG
:
473 /* sbs provides time to empty and time to full in minutes.
476 val
->intval
*= TIME_UNIT_CONVERSION
;
480 dev_dbg(&client
->dev
,
481 "%s: no need for unit conversion %d\n", __func__
, psp
);
485 static enum sbs_battery_mode
sbs_set_battery_mode(struct i2c_client
*client
,
486 enum sbs_battery_mode mode
)
488 int ret
, original_val
;
490 original_val
= sbs_read_word_data(client
, BATTERY_MODE_OFFSET
);
491 if (original_val
< 0)
494 if ((original_val
& BATTERY_MODE_MASK
) == mode
)
497 if (mode
== BATTERY_MODE_AMPS
)
498 ret
= original_val
& ~BATTERY_MODE_MASK
;
500 ret
= original_val
| BATTERY_MODE_MASK
;
502 ret
= sbs_write_word_data(client
, BATTERY_MODE_OFFSET
, ret
);
506 return original_val
& BATTERY_MODE_MASK
;
509 static int sbs_get_battery_capacity(struct i2c_client
*client
,
510 int reg_offset
, enum power_supply_property psp
,
511 union power_supply_propval
*val
)
514 enum sbs_battery_mode mode
= BATTERY_MODE_WATTS
;
516 if (power_supply_is_amp_property(psp
))
517 mode
= BATTERY_MODE_AMPS
;
519 mode
= sbs_set_battery_mode(client
, mode
);
523 ret
= sbs_read_word_data(client
, sbs_data
[reg_offset
].addr
);
527 if (psp
== POWER_SUPPLY_PROP_CAPACITY
) {
528 /* sbs spec says that this can be >100 %
529 * even if max value is 100 % */
530 val
->intval
= min(ret
, 100);
534 ret
= sbs_set_battery_mode(client
, mode
);
541 static char sbs_serial
[5];
542 static int sbs_get_battery_serial_number(struct i2c_client
*client
,
543 union power_supply_propval
*val
)
547 ret
= sbs_read_word_data(client
, sbs_data
[REG_SERIAL_NUMBER
].addr
);
551 ret
= sprintf(sbs_serial
, "%04x", ret
);
552 val
->strval
= sbs_serial
;
557 static int sbs_get_property_index(struct i2c_client
*client
,
558 enum power_supply_property psp
)
561 for (count
= 0; count
< ARRAY_SIZE(sbs_data
); count
++)
562 if (psp
== sbs_data
[count
].psp
)
565 dev_warn(&client
->dev
,
566 "%s: Invalid Property - %d\n", __func__
, psp
);
571 static int sbs_get_property(struct power_supply
*psy
,
572 enum power_supply_property psp
,
573 union power_supply_propval
*val
)
576 struct sbs_info
*chip
= power_supply_get_drvdata(psy
);
577 struct i2c_client
*client
= chip
->client
;
580 case POWER_SUPPLY_PROP_PRESENT
:
581 case POWER_SUPPLY_PROP_HEALTH
:
582 ret
= sbs_get_battery_presence_and_health(client
, psp
, val
);
583 if (psp
== POWER_SUPPLY_PROP_PRESENT
)
587 case POWER_SUPPLY_PROP_TECHNOLOGY
:
588 val
->intval
= POWER_SUPPLY_TECHNOLOGY_LION
;
589 goto done
; /* don't trigger power_supply_changed()! */
591 case POWER_SUPPLY_PROP_ENERGY_NOW
:
592 case POWER_SUPPLY_PROP_ENERGY_FULL
:
593 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN
:
594 case POWER_SUPPLY_PROP_CHARGE_NOW
:
595 case POWER_SUPPLY_PROP_CHARGE_FULL
:
596 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
:
597 case POWER_SUPPLY_PROP_CAPACITY
:
598 ret
= sbs_get_property_index(client
, psp
);
602 ret
= sbs_get_battery_capacity(client
, ret
, psp
, val
);
605 case POWER_SUPPLY_PROP_SERIAL_NUMBER
:
606 ret
= sbs_get_battery_serial_number(client
, val
);
609 case POWER_SUPPLY_PROP_STATUS
:
610 case POWER_SUPPLY_PROP_CAPACITY_LEVEL
:
611 case POWER_SUPPLY_PROP_CYCLE_COUNT
:
612 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
613 case POWER_SUPPLY_PROP_CURRENT_NOW
:
614 case POWER_SUPPLY_PROP_TEMP
:
615 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG
:
616 case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG
:
617 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
:
618 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN
:
619 ret
= sbs_get_property_index(client
, psp
);
623 ret
= sbs_get_battery_property(client
, ret
, psp
, val
);
626 case POWER_SUPPLY_PROP_MODEL_NAME
:
627 ret
= sbs_get_property_index(client
, psp
);
631 ret
= sbs_get_battery_string_property(client
, ret
, psp
,
633 val
->strval
= model_name
;
636 case POWER_SUPPLY_PROP_MANUFACTURER
:
637 ret
= sbs_get_property_index(client
, psp
);
641 ret
= sbs_get_battery_string_property(client
, ret
, psp
,
643 val
->strval
= manufacturer
;
647 dev_err(&client
->dev
,
648 "%s: INVALID property\n", __func__
);
652 if (!chip
->enable_detection
)
655 if (!chip
->gpio_detect
&&
656 chip
->is_present
!= (ret
>= 0)) {
657 chip
->is_present
= (ret
>= 0);
658 power_supply_changed(chip
->power_supply
);
663 /* Convert units to match requirements for power supply class */
664 sbs_unit_adjustment(client
, psp
, val
);
667 dev_dbg(&client
->dev
,
668 "%s: property = %d, value = %x\n", __func__
, psp
, val
->intval
);
670 if (ret
&& chip
->is_present
)
673 /* battery not present, so return NODATA for properties */
680 static irqreturn_t
sbs_irq(int irq
, void *devid
)
682 struct sbs_info
*chip
= devid
;
683 struct power_supply
*battery
= chip
->power_supply
;
686 ret
= gpiod_get_value_cansleep(chip
->gpio_detect
);
689 chip
->is_present
= ret
;
690 power_supply_changed(battery
);
695 static void sbs_external_power_changed(struct power_supply
*psy
)
697 struct sbs_info
*chip
= power_supply_get_drvdata(psy
);
699 if (chip
->ignore_changes
> 0) {
700 chip
->ignore_changes
--;
704 /* cancel outstanding work */
705 cancel_delayed_work_sync(&chip
->work
);
707 schedule_delayed_work(&chip
->work
, HZ
);
708 chip
->poll_time
= chip
->pdata
->poll_retry_count
;
711 static void sbs_delayed_work(struct work_struct
*work
)
713 struct sbs_info
*chip
;
716 chip
= container_of(work
, struct sbs_info
, work
.work
);
718 ret
= sbs_read_word_data(chip
->client
, sbs_data
[REG_STATUS
].addr
);
719 /* if the read failed, give up on this work */
725 if (ret
& BATTERY_FULL_CHARGED
)
726 ret
= POWER_SUPPLY_STATUS_FULL
;
727 else if (ret
& BATTERY_DISCHARGING
)
728 ret
= POWER_SUPPLY_STATUS_DISCHARGING
;
730 ret
= POWER_SUPPLY_STATUS_CHARGING
;
732 if (chip
->last_state
!= ret
) {
734 power_supply_changed(chip
->power_supply
);
737 if (chip
->poll_time
> 0) {
738 schedule_delayed_work(&chip
->work
, HZ
);
744 #if defined(CONFIG_OF)
746 #include <linux/of_device.h>
747 #include <linux/of_gpio.h>
749 static const struct of_device_id sbs_dt_ids
[] = {
750 { .compatible
= "sbs,sbs-battery" },
751 { .compatible
= "ti,bq20z75" },
754 MODULE_DEVICE_TABLE(of
, sbs_dt_ids
);
756 static struct sbs_platform_data
*sbs_of_populate_pdata(struct sbs_info
*chip
)
758 struct i2c_client
*client
= chip
->client
;
759 struct device_node
*of_node
= client
->dev
.of_node
;
760 struct sbs_platform_data
*pdata
;
764 /* verify this driver matches this device */
768 /* first make sure at least one property is set, otherwise
769 * it won't change behavior from running without pdata.
771 if (!of_get_property(of_node
, "sbs,i2c-retry-count", NULL
) &&
772 !of_get_property(of_node
, "sbs,poll-retry-count", NULL
))
775 pdata
= devm_kzalloc(&client
->dev
, sizeof(struct sbs_platform_data
),
778 return ERR_PTR(-ENOMEM
);
780 rc
= of_property_read_u32(of_node
, "sbs,i2c-retry-count", &prop
);
782 pdata
->i2c_retry_count
= prop
;
784 rc
= of_property_read_u32(of_node
, "sbs,poll-retry-count", &prop
);
786 pdata
->poll_retry_count
= prop
;
792 static struct sbs_platform_data
*sbs_of_populate_pdata(
793 struct sbs_info
*client
)
795 return ERR_PTR(-ENOSYS
);
799 static const struct power_supply_desc sbs_default_desc
= {
800 .type
= POWER_SUPPLY_TYPE_BATTERY
,
801 .properties
= sbs_properties
,
802 .num_properties
= ARRAY_SIZE(sbs_properties
),
803 .get_property
= sbs_get_property
,
804 .external_power_changed
= sbs_external_power_changed
,
807 static int sbs_probe(struct i2c_client
*client
,
808 const struct i2c_device_id
*id
)
810 struct sbs_info
*chip
;
811 struct power_supply_desc
*sbs_desc
;
812 struct sbs_platform_data
*pdata
= client
->dev
.platform_data
;
813 struct power_supply_config psy_cfg
= {};
817 sbs_desc
= devm_kmemdup(&client
->dev
, &sbs_default_desc
,
818 sizeof(*sbs_desc
), GFP_KERNEL
);
822 sbs_desc
->name
= devm_kasprintf(&client
->dev
, GFP_KERNEL
, "sbs-%s",
823 dev_name(&client
->dev
));
827 chip
= devm_kzalloc(&client
->dev
, sizeof(struct sbs_info
), GFP_KERNEL
);
831 chip
->client
= client
;
832 chip
->enable_detection
= 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
;
842 pdata
= sbs_of_populate_pdata(chip
);
845 return PTR_ERR(pdata
);
849 chip
->gpio_detect
= devm_gpiod_get_optional(&client
->dev
,
850 "sbs,battery-detect", GPIOD_IN
);
851 if (IS_ERR(chip
->gpio_detect
)) {
852 dev_err(&client
->dev
, "Failed to get gpio: %ld\n",
853 PTR_ERR(chip
->gpio_detect
));
854 return PTR_ERR(chip
->gpio_detect
);
857 i2c_set_clientdata(client
, chip
);
859 if (!chip
->gpio_detect
)
862 irq
= gpiod_to_irq(chip
->gpio_detect
);
864 dev_warn(&client
->dev
, "Failed to get gpio as irq: %d\n", irq
);
868 rc
= devm_request_threaded_irq(&client
->dev
, irq
, NULL
, sbs_irq
,
869 IRQF_TRIGGER_RISING
| IRQF_TRIGGER_FALLING
,
870 dev_name(&client
->dev
), chip
);
872 dev_warn(&client
->dev
, "Failed to request irq: %d\n", rc
);
878 * Before we register, we might need to make sure we can actually talk
881 if (!(force_load
|| chip
->gpio_detect
)) {
882 rc
= sbs_read_word_data(client
, sbs_data
[REG_STATUS
].addr
);
885 dev_err(&client
->dev
, "%s: Failed to get device status\n",
891 chip
->power_supply
= devm_power_supply_register(&client
->dev
, sbs_desc
,
893 if (IS_ERR(chip
->power_supply
)) {
894 dev_err(&client
->dev
,
895 "%s: Failed to register power supply\n", __func__
);
896 rc
= PTR_ERR(chip
->power_supply
);
900 dev_info(&client
->dev
,
901 "%s: battery gas gauge device registered\n", client
->name
);
903 INIT_DELAYED_WORK(&chip
->work
, sbs_delayed_work
);
905 chip
->enable_detection
= true;
913 static int sbs_remove(struct i2c_client
*client
)
915 struct sbs_info
*chip
= i2c_get_clientdata(client
);
917 cancel_delayed_work_sync(&chip
->work
);
922 #if defined CONFIG_PM_SLEEP
924 static int sbs_suspend(struct device
*dev
)
926 struct i2c_client
*client
= to_i2c_client(dev
);
927 struct sbs_info
*chip
= i2c_get_clientdata(client
);
930 if (chip
->poll_time
> 0)
931 cancel_delayed_work_sync(&chip
->work
);
933 /* write to manufacturer access with sleep command */
934 ret
= sbs_write_word_data(client
, sbs_data
[REG_MANUFACTURER_DATA
].addr
,
935 MANUFACTURER_ACCESS_SLEEP
);
936 if (chip
->is_present
&& ret
< 0)
942 static SIMPLE_DEV_PM_OPS(sbs_pm_ops
, sbs_suspend
, NULL
);
943 #define SBS_PM_OPS (&sbs_pm_ops)
946 #define SBS_PM_OPS NULL
949 static const struct i2c_device_id sbs_id
[] = {
951 { "sbs-battery", 1 },
954 MODULE_DEVICE_TABLE(i2c
, sbs_id
);
956 static struct i2c_driver sbs_battery_driver
= {
958 .remove
= sbs_remove
,
961 .name
= "sbs-battery",
962 .of_match_table
= of_match_ptr(sbs_dt_ids
),
966 module_i2c_driver(sbs_battery_driver
);
968 MODULE_DESCRIPTION("SBS battery monitor driver");
969 MODULE_LICENSE("GPL");
971 module_param(force_load
, bool, S_IRUSR
| S_IRGRP
| S_IROTH
);
972 MODULE_PARM_DESC(force_load
,
973 "Attempt to load the driver even if no battery is connected");