1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Gas Gauge driver for SBS Compliant Batteries
5 * Copyright (c) 2010, NVIDIA Corporation.
8 #include <linux/bits.h>
9 #include <linux/delay.h>
10 #include <linux/devm-helpers.h>
11 #include <linux/err.h>
12 #include <linux/gpio/consumer.h>
13 #include <linux/i2c.h>
14 #include <linux/init.h>
15 #include <linux/interrupt.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/property.h>
20 #include <linux/power/sbs-battery.h>
21 #include <linux/power_supply.h>
22 #include <linux/slab.h>
23 #include <linux/stat.h>
26 REG_MANUFACTURER_DATA
,
34 REG_TIME_TO_EMPTY_NOW
,
35 REG_TIME_TO_EMPTY_AVG
,
41 REG_REMAINING_CAPACITY
,
42 REG_REMAINING_CAPACITY_CHARGE
,
43 REG_FULL_CHARGE_CAPACITY
,
44 REG_FULL_CHARGE_CAPACITY_CHARGE
,
46 REG_DESIGN_CAPACITY_CHARGE
,
47 REG_DESIGN_VOLTAGE_MIN
,
48 REG_DESIGN_VOLTAGE_MAX
,
56 #define REG_ADDR_SPEC_INFO 0x1A
57 #define SPEC_INFO_VERSION_MASK GENMASK(7, 4)
58 #define SPEC_INFO_VERSION_SHIFT 4
60 #define SBS_VERSION_1_0 1
61 #define SBS_VERSION_1_1 2
62 #define SBS_VERSION_1_1_WITH_PEC 3
64 #define REG_ADDR_MANUFACTURE_DATE 0x1B
66 /* Battery Mode defines */
67 #define BATTERY_MODE_OFFSET 0x03
68 #define BATTERY_MODE_CAPACITY_MASK BIT(15)
69 enum sbs_capacity_mode
{
70 CAPACITY_MODE_AMPS
= 0,
71 CAPACITY_MODE_WATTS
= BATTERY_MODE_CAPACITY_MASK
73 #define BATTERY_MODE_CHARGER_MASK (1<<14)
75 /* manufacturer access defines */
76 #define MANUFACTURER_ACCESS_STATUS 0x0006
77 #define MANUFACTURER_ACCESS_SLEEP 0x0011
79 /* battery status value bits */
80 #define BATTERY_INITIALIZED 0x80
81 #define BATTERY_DISCHARGING 0x40
82 #define BATTERY_FULL_CHARGED 0x20
83 #define BATTERY_FULL_DISCHARGED 0x10
85 /* min_value and max_value are only valid for numerical data */
86 #define SBS_DATA(_psp, _addr, _min_value, _max_value) { \
89 .min_value = _min_value, \
90 .max_value = _max_value, \
93 static const struct chip_data
{
94 enum power_supply_property psp
;
99 [REG_MANUFACTURER_DATA
] =
100 SBS_DATA(POWER_SUPPLY_PROP_PRESENT
, 0x00, 0, 65535),
102 SBS_DATA(-1, 0x03, 0, 65535),
104 SBS_DATA(POWER_SUPPLY_PROP_TEMP
, 0x08, 0, 65535),
106 SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_NOW
, 0x09, 0, 65535),
108 SBS_DATA(POWER_SUPPLY_PROP_CURRENT_NOW
, 0x0A, -32768, 32767),
110 SBS_DATA(POWER_SUPPLY_PROP_CURRENT_AVG
, 0x0B, -32768, 32767),
112 SBS_DATA(POWER_SUPPLY_PROP_CAPACITY_ERROR_MARGIN
, 0x0c, 0, 100),
114 SBS_DATA(POWER_SUPPLY_PROP_CAPACITY
, 0x0D, 0, 100),
115 [REG_REMAINING_CAPACITY
] =
116 SBS_DATA(POWER_SUPPLY_PROP_ENERGY_NOW
, 0x0F, 0, 65535),
117 [REG_REMAINING_CAPACITY_CHARGE
] =
118 SBS_DATA(POWER_SUPPLY_PROP_CHARGE_NOW
, 0x0F, 0, 65535),
119 [REG_FULL_CHARGE_CAPACITY
] =
120 SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL
, 0x10, 0, 65535),
121 [REG_FULL_CHARGE_CAPACITY_CHARGE
] =
122 SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL
, 0x10, 0, 65535),
123 [REG_TIME_TO_EMPTY_NOW
] =
124 SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW
, 0x11, 0, 65535),
125 [REG_TIME_TO_EMPTY_AVG
] =
126 SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG
, 0x12, 0, 65535),
127 [REG_TIME_TO_FULL_AVG
] =
128 SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_FULL_AVG
, 0x13, 0, 65535),
129 [REG_CHARGE_CURRENT
] =
130 SBS_DATA(POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX
, 0x14, 0, 65535),
131 [REG_CHARGE_VOLTAGE
] =
132 SBS_DATA(POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX
, 0x15, 0, 65535),
134 SBS_DATA(POWER_SUPPLY_PROP_STATUS
, 0x16, 0, 65535),
135 [REG_CAPACITY_LEVEL
] =
136 SBS_DATA(POWER_SUPPLY_PROP_CAPACITY_LEVEL
, 0x16, 0, 65535),
138 SBS_DATA(POWER_SUPPLY_PROP_CYCLE_COUNT
, 0x17, 0, 65535),
139 [REG_DESIGN_CAPACITY
] =
140 SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN
, 0x18, 0, 65535),
141 [REG_DESIGN_CAPACITY_CHARGE
] =
142 SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
, 0x18, 0, 65535),
143 [REG_DESIGN_VOLTAGE_MIN
] =
144 SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
, 0x19, 0, 65535),
145 [REG_DESIGN_VOLTAGE_MAX
] =
146 SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN
, 0x19, 0, 65535),
147 [REG_SERIAL_NUMBER
] =
148 SBS_DATA(POWER_SUPPLY_PROP_SERIAL_NUMBER
, 0x1C, 0, 65535),
149 /* Properties of type `const char *' */
151 SBS_DATA(POWER_SUPPLY_PROP_MANUFACTURER
, 0x20, 0, 65535),
153 SBS_DATA(POWER_SUPPLY_PROP_MODEL_NAME
, 0x21, 0, 65535),
155 SBS_DATA(POWER_SUPPLY_PROP_TECHNOLOGY
, 0x22, 0, 65535)
158 static const enum power_supply_property sbs_properties
[] = {
159 POWER_SUPPLY_PROP_STATUS
,
160 POWER_SUPPLY_PROP_CAPACITY_LEVEL
,
161 POWER_SUPPLY_PROP_HEALTH
,
162 POWER_SUPPLY_PROP_PRESENT
,
163 POWER_SUPPLY_PROP_TECHNOLOGY
,
164 POWER_SUPPLY_PROP_CYCLE_COUNT
,
165 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
166 POWER_SUPPLY_PROP_CURRENT_NOW
,
167 POWER_SUPPLY_PROP_CURRENT_AVG
,
168 POWER_SUPPLY_PROP_CAPACITY
,
169 POWER_SUPPLY_PROP_CAPACITY_ERROR_MARGIN
,
170 POWER_SUPPLY_PROP_TEMP
,
171 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW
,
172 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG
,
173 POWER_SUPPLY_PROP_TIME_TO_FULL_AVG
,
174 POWER_SUPPLY_PROP_SERIAL_NUMBER
,
175 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
,
176 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN
,
177 POWER_SUPPLY_PROP_ENERGY_NOW
,
178 POWER_SUPPLY_PROP_ENERGY_FULL
,
179 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN
,
180 POWER_SUPPLY_PROP_CHARGE_NOW
,
181 POWER_SUPPLY_PROP_CHARGE_FULL
,
182 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
,
183 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX
,
184 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX
,
185 POWER_SUPPLY_PROP_MANUFACTURE_YEAR
,
186 POWER_SUPPLY_PROP_MANUFACTURE_MONTH
,
187 POWER_SUPPLY_PROP_MANUFACTURE_DAY
,
188 /* Properties of type `const char *' */
189 POWER_SUPPLY_PROP_MANUFACTURER
,
190 POWER_SUPPLY_PROP_MODEL_NAME
193 /* Supports special manufacturer commands from TI BQ20Z65 and BQ20Z75 IC. */
194 #define SBS_FLAGS_TI_BQ20ZX5 BIT(0)
196 static const enum power_supply_property string_properties
[] = {
197 POWER_SUPPLY_PROP_TECHNOLOGY
,
198 POWER_SUPPLY_PROP_MANUFACTURER
,
199 POWER_SUPPLY_PROP_MODEL_NAME
,
202 #define NR_STRING_BUFFERS ARRAY_SIZE(string_properties)
205 struct i2c_client
*client
;
206 struct power_supply
*power_supply
;
208 struct gpio_desc
*gpio_detect
;
209 bool charger_broadcasts
;
213 u32 poll_retry_count
;
214 struct delayed_work work
;
215 struct mutex mode_lock
;
218 char strings
[NR_STRING_BUFFERS
][I2C_SMBUS_BLOCK_MAX
+ 1];
221 static char *sbs_get_string_buf(struct sbs_info
*chip
,
222 enum power_supply_property psp
)
226 for (i
= 0; i
< NR_STRING_BUFFERS
; i
++)
227 if (string_properties
[i
] == psp
)
228 return chip
->strings
[i
];
230 return ERR_PTR(-EINVAL
);
233 static void sbs_invalidate_cached_props(struct sbs_info
*chip
)
237 chip
->technology
= -1;
239 for (i
= 0; i
< NR_STRING_BUFFERS
; i
++)
240 chip
->strings
[i
][0] = 0;
243 static bool force_load
;
245 static int sbs_read_word_data(struct i2c_client
*client
, u8 address
);
246 static int sbs_write_word_data(struct i2c_client
*client
, u8 address
, u16 value
);
248 static void sbs_disable_charger_broadcasts(struct sbs_info
*chip
)
250 int val
= sbs_read_word_data(chip
->client
, BATTERY_MODE_OFFSET
);
254 val
|= BATTERY_MODE_CHARGER_MASK
;
256 val
= sbs_write_word_data(chip
->client
, BATTERY_MODE_OFFSET
, val
);
260 dev_err(&chip
->client
->dev
,
261 "Failed to disable charger broadcasting: %d\n", val
);
263 dev_dbg(&chip
->client
->dev
, "%s\n", __func__
);
266 static int sbs_update_presence(struct sbs_info
*chip
, bool is_present
)
268 struct i2c_client
*client
= chip
->client
;
269 int retries
= chip
->i2c_retry_count
;
273 if (chip
->is_present
== is_present
)
277 chip
->is_present
= false;
278 /* Disable PEC when no device is present */
279 client
->flags
&= ~I2C_CLIENT_PEC
;
280 sbs_invalidate_cached_props(chip
);
284 /* Check if device supports packet error checking and use it */
285 while (retries
> 0) {
286 ret
= i2c_smbus_read_word_data(client
, REG_ADDR_SPEC_INFO
);
291 * Some batteries trigger the detection pin before the
292 * I2C bus is properly connected. This works around the
301 dev_dbg(&client
->dev
, "failed to read spec info: %d\n", ret
);
303 /* fallback to old behaviour */
304 client
->flags
&= ~I2C_CLIENT_PEC
;
305 chip
->is_present
= true;
310 version
= (ret
& SPEC_INFO_VERSION_MASK
) >> SPEC_INFO_VERSION_SHIFT
;
312 if (version
== SBS_VERSION_1_1_WITH_PEC
)
313 client
->flags
|= I2C_CLIENT_PEC
;
315 client
->flags
&= ~I2C_CLIENT_PEC
;
317 if (of_device_is_compatible(client
->dev
.parent
->of_node
, "google,cros-ec-i2c-tunnel")
318 && client
->flags
& I2C_CLIENT_PEC
) {
319 dev_info(&client
->dev
, "Disabling PEC because of broken Cros-EC implementation\n");
320 client
->flags
&= ~I2C_CLIENT_PEC
;
323 dev_dbg(&client
->dev
, "PEC: %s\n", (client
->flags
& I2C_CLIENT_PEC
) ?
324 "enabled" : "disabled");
326 if (!chip
->is_present
&& is_present
&& !chip
->charger_broadcasts
)
327 sbs_disable_charger_broadcasts(chip
);
329 chip
->is_present
= true;
334 static int sbs_read_word_data(struct i2c_client
*client
, u8 address
)
336 struct sbs_info
*chip
= i2c_get_clientdata(client
);
337 int retries
= chip
->i2c_retry_count
;
340 while (retries
> 0) {
341 ret
= i2c_smbus_read_word_data(client
, address
);
348 dev_dbg(&client
->dev
,
349 "%s: i2c read at address 0x%x failed\n",
357 static int sbs_read_string_data_fallback(struct i2c_client
*client
, u8 address
, char *values
)
359 struct sbs_info
*chip
= i2c_get_clientdata(client
);
360 s32 ret
= 0, block_length
= 0;
361 int retries_length
, retries_block
;
362 u8 block_buffer
[I2C_SMBUS_BLOCK_MAX
+ 1];
364 retries_length
= chip
->i2c_retry_count
;
365 retries_block
= chip
->i2c_retry_count
;
367 dev_warn_once(&client
->dev
, "I2C adapter does not support I2C_FUNC_SMBUS_READ_BLOCK_DATA.\n"
368 "Fallback method does not support PEC.\n");
370 /* Adapter needs to support these two functions */
371 if (!i2c_check_functionality(client
->adapter
,
372 I2C_FUNC_SMBUS_BYTE_DATA
|
373 I2C_FUNC_SMBUS_I2C_BLOCK
)){
377 /* Get the length of block data */
378 while (retries_length
> 0) {
379 ret
= i2c_smbus_read_byte_data(client
, address
);
386 dev_dbg(&client
->dev
,
387 "%s: i2c read at address 0x%x failed\n",
392 /* block_length does not include NULL terminator */
394 if (block_length
> I2C_SMBUS_BLOCK_MAX
) {
395 dev_err(&client
->dev
,
396 "%s: Returned block_length is longer than 0x%x\n",
397 __func__
, I2C_SMBUS_BLOCK_MAX
);
401 /* Get the block data */
402 while (retries_block
> 0) {
403 ret
= i2c_smbus_read_i2c_block_data(
405 block_length
+ 1, block_buffer
);
412 dev_dbg(&client
->dev
,
413 "%s: i2c read at address 0x%x failed\n",
418 /* block_buffer[0] == block_length */
419 memcpy(values
, block_buffer
+ 1, block_length
);
420 values
[block_length
] = '\0';
425 static int sbs_read_string_data(struct i2c_client
*client
, u8 address
, char *values
)
427 struct sbs_info
*chip
= i2c_get_clientdata(client
);
428 int retries
= chip
->i2c_retry_count
;
431 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_READ_BLOCK_DATA
)) {
432 bool pec
= client
->flags
& I2C_CLIENT_PEC
;
433 client
->flags
&= ~I2C_CLIENT_PEC
;
434 ret
= sbs_read_string_data_fallback(client
, address
, values
);
436 client
->flags
|= I2C_CLIENT_PEC
;
440 while (retries
> 0) {
441 ret
= i2c_smbus_read_block_data(client
, address
, values
);
448 dev_dbg(&client
->dev
, "failed to read block 0x%x: %d\n", address
, ret
);
452 /* add string termination */
457 static int sbs_write_word_data(struct i2c_client
*client
, u8 address
,
460 struct sbs_info
*chip
= i2c_get_clientdata(client
);
461 int retries
= chip
->i2c_retry_count
;
464 while (retries
> 0) {
465 ret
= i2c_smbus_write_word_data(client
, address
, value
);
472 dev_dbg(&client
->dev
,
473 "%s: i2c write to address 0x%x failed\n",
481 static int sbs_status_correct(struct i2c_client
*client
, int *intval
)
485 ret
= sbs_read_word_data(client
, sbs_data
[REG_CURRENT_NOW
].addr
);
491 /* Not drawing current -> not charging (i.e. idle) */
492 if (*intval
!= POWER_SUPPLY_STATUS_FULL
&& ret
== 0)
493 *intval
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
495 if (*intval
== POWER_SUPPLY_STATUS_FULL
) {
496 /* Drawing or providing current when full */
498 *intval
= POWER_SUPPLY_STATUS_CHARGING
;
500 *intval
= POWER_SUPPLY_STATUS_DISCHARGING
;
506 static bool sbs_bat_needs_calibration(struct i2c_client
*client
)
510 ret
= sbs_read_word_data(client
, sbs_data
[REG_BATTERY_MODE
].addr
);
514 return !!(ret
& BIT(7));
517 static int sbs_get_ti_battery_presence_and_health(
518 struct i2c_client
*client
, enum power_supply_property psp
,
519 union power_supply_propval
*val
)
524 * Write to ManufacturerAccess with ManufacturerAccess command
525 * and then read the status.
527 ret
= sbs_write_word_data(client
, sbs_data
[REG_MANUFACTURER_DATA
].addr
,
528 MANUFACTURER_ACCESS_STATUS
);
530 if (psp
== POWER_SUPPLY_PROP_PRESENT
)
531 val
->intval
= 0; /* battery removed */
535 ret
= sbs_read_word_data(client
, sbs_data
[REG_MANUFACTURER_DATA
].addr
);
537 if (psp
== POWER_SUPPLY_PROP_PRESENT
)
538 val
->intval
= 0; /* battery removed */
542 if (ret
< sbs_data
[REG_MANUFACTURER_DATA
].min_value
||
543 ret
> sbs_data
[REG_MANUFACTURER_DATA
].max_value
) {
548 /* Mask the upper nibble of 2nd byte and
549 * lower byte of response then
550 * shift the result by 8 to get status*/
553 if (psp
== POWER_SUPPLY_PROP_PRESENT
) {
555 /* battery removed */
559 } else if (psp
== POWER_SUPPLY_PROP_HEALTH
) {
561 val
->intval
= POWER_SUPPLY_HEALTH_UNSPEC_FAILURE
;
562 else if (ret
== 0x0B)
563 val
->intval
= POWER_SUPPLY_HEALTH_OVERHEAT
;
564 else if (ret
== 0x0C)
565 val
->intval
= POWER_SUPPLY_HEALTH_DEAD
;
566 else if (sbs_bat_needs_calibration(client
))
567 val
->intval
= POWER_SUPPLY_HEALTH_CALIBRATION_REQUIRED
;
569 val
->intval
= POWER_SUPPLY_HEALTH_GOOD
;
575 static int sbs_get_battery_presence_and_health(
576 struct i2c_client
*client
, enum power_supply_property psp
,
577 union power_supply_propval
*val
)
579 struct sbs_info
*chip
= i2c_get_clientdata(client
);
582 if (chip
->flags
& SBS_FLAGS_TI_BQ20ZX5
)
583 return sbs_get_ti_battery_presence_and_health(client
, psp
, val
);
585 /* Dummy command; if it succeeds, battery is present. */
586 ret
= sbs_read_word_data(client
, sbs_data
[REG_STATUS
].addr
);
588 if (ret
< 0) { /* battery not present*/
589 if (psp
== POWER_SUPPLY_PROP_PRESENT
) {
596 if (psp
== POWER_SUPPLY_PROP_PRESENT
)
597 val
->intval
= 1; /* battery present */
598 else { /* POWER_SUPPLY_PROP_HEALTH */
599 if (sbs_bat_needs_calibration(client
)) {
600 val
->intval
= POWER_SUPPLY_HEALTH_CALIBRATION_REQUIRED
;
602 /* SBS spec doesn't have a general health command. */
603 val
->intval
= POWER_SUPPLY_HEALTH_UNKNOWN
;
610 static int sbs_get_battery_property(struct i2c_client
*client
,
611 int reg_offset
, enum power_supply_property psp
,
612 union power_supply_propval
*val
)
614 struct sbs_info
*chip
= i2c_get_clientdata(client
);
617 ret
= sbs_read_word_data(client
, sbs_data
[reg_offset
].addr
);
621 /* returned values are 16 bit */
622 if (sbs_data
[reg_offset
].min_value
< 0)
625 if (ret
>= sbs_data
[reg_offset
].min_value
&&
626 ret
<= sbs_data
[reg_offset
].max_value
) {
628 if (psp
== POWER_SUPPLY_PROP_CAPACITY_LEVEL
) {
629 if (!(ret
& BATTERY_INITIALIZED
))
631 POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN
;
632 else if (ret
& BATTERY_FULL_CHARGED
)
634 POWER_SUPPLY_CAPACITY_LEVEL_FULL
;
635 else if (ret
& BATTERY_FULL_DISCHARGED
)
637 POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL
;
640 POWER_SUPPLY_CAPACITY_LEVEL_NORMAL
;
642 } else if (psp
!= POWER_SUPPLY_PROP_STATUS
) {
646 if (ret
& BATTERY_FULL_CHARGED
)
647 val
->intval
= POWER_SUPPLY_STATUS_FULL
;
648 else if (ret
& BATTERY_DISCHARGING
)
649 val
->intval
= POWER_SUPPLY_STATUS_DISCHARGING
;
651 val
->intval
= POWER_SUPPLY_STATUS_CHARGING
;
653 sbs_status_correct(client
, &val
->intval
);
655 if (chip
->poll_time
== 0)
656 chip
->last_state
= val
->intval
;
657 else if (chip
->last_state
!= val
->intval
) {
658 cancel_delayed_work_sync(&chip
->work
);
659 power_supply_changed(chip
->power_supply
);
663 if (psp
== POWER_SUPPLY_PROP_STATUS
)
664 val
->intval
= POWER_SUPPLY_STATUS_UNKNOWN
;
665 else if (psp
== POWER_SUPPLY_PROP_CAPACITY
)
666 /* sbs spec says that this can be >100 %
667 * even if max value is 100 %
669 val
->intval
= min(ret
, 100);
677 static int sbs_get_property_index(struct i2c_client
*client
,
678 enum power_supply_property psp
)
682 for (count
= 0; count
< ARRAY_SIZE(sbs_data
); count
++)
683 if (psp
== sbs_data
[count
].psp
)
686 dev_warn(&client
->dev
,
687 "%s: Invalid Property - %d\n", __func__
, psp
);
692 static const char *sbs_get_constant_string(struct sbs_info
*chip
,
693 enum power_supply_property psp
)
699 buf
= sbs_get_string_buf(chip
, psp
);
704 ret
= sbs_get_property_index(chip
->client
, psp
);
708 addr
= sbs_data
[ret
].addr
;
710 ret
= sbs_read_string_data(chip
->client
, addr
, buf
);
718 static void sbs_unit_adjustment(struct i2c_client
*client
,
719 enum power_supply_property psp
, union power_supply_propval
*val
)
721 #define BASE_UNIT_CONVERSION 1000
722 #define BATTERY_MODE_CAP_MULT_WATT (10 * BASE_UNIT_CONVERSION)
723 #define TIME_UNIT_CONVERSION 60
724 #define TEMP_KELVIN_TO_CELSIUS 2731
726 case POWER_SUPPLY_PROP_ENERGY_NOW
:
727 case POWER_SUPPLY_PROP_ENERGY_FULL
:
728 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN
:
729 /* sbs provides energy in units of 10mWh.
732 val
->intval
*= BATTERY_MODE_CAP_MULT_WATT
;
735 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
736 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
:
737 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN
:
738 case POWER_SUPPLY_PROP_CURRENT_NOW
:
739 case POWER_SUPPLY_PROP_CURRENT_AVG
:
740 case POWER_SUPPLY_PROP_CHARGE_NOW
:
741 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX
:
742 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX
:
743 case POWER_SUPPLY_PROP_CHARGE_FULL
:
744 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
:
745 val
->intval
*= BASE_UNIT_CONVERSION
;
748 case POWER_SUPPLY_PROP_TEMP
:
749 /* sbs provides battery temperature in 0.1K
750 * so convert it to 0.1°C
752 val
->intval
-= TEMP_KELVIN_TO_CELSIUS
;
755 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW
:
756 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG
:
757 case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG
:
758 /* sbs provides time to empty and time to full in minutes.
761 val
->intval
*= TIME_UNIT_CONVERSION
;
765 dev_dbg(&client
->dev
,
766 "%s: no need for unit conversion %d\n", __func__
, psp
);
770 static enum sbs_capacity_mode
sbs_set_capacity_mode(struct i2c_client
*client
,
771 enum sbs_capacity_mode mode
)
773 int ret
, original_val
;
775 original_val
= sbs_read_word_data(client
, BATTERY_MODE_OFFSET
);
776 if (original_val
< 0)
779 if ((original_val
& BATTERY_MODE_CAPACITY_MASK
) == mode
)
782 if (mode
== CAPACITY_MODE_AMPS
)
783 ret
= original_val
& ~BATTERY_MODE_CAPACITY_MASK
;
785 ret
= original_val
| BATTERY_MODE_CAPACITY_MASK
;
787 ret
= sbs_write_word_data(client
, BATTERY_MODE_OFFSET
, ret
);
791 usleep_range(1000, 2000);
793 return original_val
& BATTERY_MODE_CAPACITY_MASK
;
796 static int sbs_get_battery_capacity(struct i2c_client
*client
,
797 int reg_offset
, enum power_supply_property psp
,
798 union power_supply_propval
*val
)
801 enum sbs_capacity_mode mode
= CAPACITY_MODE_WATTS
;
803 if (power_supply_is_amp_property(psp
))
804 mode
= CAPACITY_MODE_AMPS
;
806 mode
= sbs_set_capacity_mode(client
, mode
);
810 ret
= sbs_read_word_data(client
, sbs_data
[reg_offset
].addr
);
816 ret
= sbs_set_capacity_mode(client
, mode
);
823 static char sbs_serial
[5];
824 static int sbs_get_battery_serial_number(struct i2c_client
*client
,
825 union power_supply_propval
*val
)
829 ret
= sbs_read_word_data(client
, sbs_data
[REG_SERIAL_NUMBER
].addr
);
833 sprintf(sbs_serial
, "%04x", ret
);
834 val
->strval
= sbs_serial
;
839 static int sbs_get_chemistry(struct sbs_info
*chip
,
840 union power_supply_propval
*val
)
842 const char *chemistry
;
844 if (chip
->technology
!= -1) {
845 val
->intval
= chip
->technology
;
849 chemistry
= sbs_get_constant_string(chip
, POWER_SUPPLY_PROP_TECHNOLOGY
);
851 if (IS_ERR(chemistry
))
852 return PTR_ERR(chemistry
);
854 if (!strncasecmp(chemistry
, "LION", 4))
855 chip
->technology
= POWER_SUPPLY_TECHNOLOGY_LION
;
856 else if (!strncasecmp(chemistry
, "LiP", 3))
857 chip
->technology
= POWER_SUPPLY_TECHNOLOGY_LIPO
;
858 else if (!strncasecmp(chemistry
, "NiCd", 4))
859 chip
->technology
= POWER_SUPPLY_TECHNOLOGY_NiCd
;
860 else if (!strncasecmp(chemistry
, "NiMH", 4))
861 chip
->technology
= POWER_SUPPLY_TECHNOLOGY_NiMH
;
863 chip
->technology
= POWER_SUPPLY_TECHNOLOGY_UNKNOWN
;
865 if (chip
->technology
== POWER_SUPPLY_TECHNOLOGY_UNKNOWN
)
866 dev_warn(&chip
->client
->dev
, "Unknown chemistry: %s\n", chemistry
);
868 val
->intval
= chip
->technology
;
873 static int sbs_get_battery_manufacture_date(struct i2c_client
*client
,
874 enum power_supply_property psp
,
875 union power_supply_propval
*val
)
878 u16 day
, month
, year
;
880 ret
= sbs_read_word_data(client
, REG_ADDR_MANUFACTURE_DATE
);
884 day
= ret
& GENMASK(4, 0);
885 month
= (ret
& GENMASK(8, 5)) >> 5;
886 year
= ((ret
& GENMASK(15, 9)) >> 9) + 1980;
889 case POWER_SUPPLY_PROP_MANUFACTURE_YEAR
:
892 case POWER_SUPPLY_PROP_MANUFACTURE_MONTH
:
895 case POWER_SUPPLY_PROP_MANUFACTURE_DAY
:
905 static int sbs_get_property(struct power_supply
*psy
,
906 enum power_supply_property psp
,
907 union power_supply_propval
*val
)
910 struct sbs_info
*chip
= power_supply_get_drvdata(psy
);
911 struct i2c_client
*client
= chip
->client
;
914 if (chip
->gpio_detect
) {
915 ret
= gpiod_get_value_cansleep(chip
->gpio_detect
);
918 if (psp
== POWER_SUPPLY_PROP_PRESENT
) {
920 sbs_update_presence(chip
, ret
);
928 case POWER_SUPPLY_PROP_PRESENT
:
929 case POWER_SUPPLY_PROP_HEALTH
:
930 ret
= sbs_get_battery_presence_and_health(client
, psp
, val
);
932 /* this can only be true if no gpio is used */
933 if (psp
== POWER_SUPPLY_PROP_PRESENT
)
937 case POWER_SUPPLY_PROP_TECHNOLOGY
:
938 ret
= sbs_get_chemistry(chip
, val
);
942 goto done
; /* don't trigger power_supply_changed()! */
944 case POWER_SUPPLY_PROP_ENERGY_NOW
:
945 case POWER_SUPPLY_PROP_ENERGY_FULL
:
946 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN
:
947 case POWER_SUPPLY_PROP_CHARGE_NOW
:
948 case POWER_SUPPLY_PROP_CHARGE_FULL
:
949 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
:
950 ret
= sbs_get_property_index(client
, psp
);
954 /* sbs_get_battery_capacity() will change the battery mode
955 * temporarily to read the requested attribute. Ensure we stay
956 * in the desired mode for the duration of the attribute read.
958 mutex_lock(&chip
->mode_lock
);
959 ret
= sbs_get_battery_capacity(client
, ret
, psp
, val
);
960 mutex_unlock(&chip
->mode_lock
);
963 case POWER_SUPPLY_PROP_SERIAL_NUMBER
:
964 ret
= sbs_get_battery_serial_number(client
, val
);
967 case POWER_SUPPLY_PROP_STATUS
:
968 case POWER_SUPPLY_PROP_CAPACITY_LEVEL
:
969 case POWER_SUPPLY_PROP_CYCLE_COUNT
:
970 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
971 case POWER_SUPPLY_PROP_CURRENT_NOW
:
972 case POWER_SUPPLY_PROP_CURRENT_AVG
:
973 case POWER_SUPPLY_PROP_TEMP
:
974 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW
:
975 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG
:
976 case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG
:
977 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
:
978 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN
:
979 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX
:
980 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX
:
981 case POWER_SUPPLY_PROP_CAPACITY
:
982 case POWER_SUPPLY_PROP_CAPACITY_ERROR_MARGIN
:
983 ret
= sbs_get_property_index(client
, psp
);
987 ret
= sbs_get_battery_property(client
, ret
, psp
, val
);
990 case POWER_SUPPLY_PROP_MODEL_NAME
:
991 case POWER_SUPPLY_PROP_MANUFACTURER
:
992 str
= sbs_get_constant_string(chip
, psp
);
999 case POWER_SUPPLY_PROP_MANUFACTURE_YEAR
:
1000 case POWER_SUPPLY_PROP_MANUFACTURE_MONTH
:
1001 case POWER_SUPPLY_PROP_MANUFACTURE_DAY
:
1002 ret
= sbs_get_battery_manufacture_date(client
, psp
, val
);
1006 dev_err(&client
->dev
,
1007 "%s: INVALID property\n", __func__
);
1011 if (!chip
->gpio_detect
&& chip
->is_present
!= (ret
>= 0)) {
1012 bool old_present
= chip
->is_present
;
1013 union power_supply_propval val
;
1014 int err
= sbs_get_battery_presence_and_health(
1015 client
, POWER_SUPPLY_PROP_PRESENT
, &val
);
1017 sbs_update_presence(chip
, !err
&& val
.intval
);
1019 if (old_present
!= chip
->is_present
)
1020 power_supply_changed(chip
->power_supply
);
1025 /* Convert units to match requirements for power supply class */
1026 sbs_unit_adjustment(client
, psp
, val
);
1027 dev_dbg(&client
->dev
,
1028 "%s: property = %d, value = %x\n", __func__
,
1030 } else if (!chip
->is_present
) {
1031 /* battery not present, so return NODATA for properties */
1037 static void sbs_supply_changed(struct sbs_info
*chip
)
1039 struct power_supply
*battery
= chip
->power_supply
;
1042 ret
= gpiod_get_value_cansleep(chip
->gpio_detect
);
1045 sbs_update_presence(chip
, ret
);
1046 power_supply_changed(battery
);
1049 static irqreturn_t
sbs_irq(int irq
, void *devid
)
1051 sbs_supply_changed(devid
);
1055 static void sbs_alert(struct i2c_client
*client
, enum i2c_alert_protocol prot
,
1058 sbs_supply_changed(i2c_get_clientdata(client
));
1061 static void sbs_external_power_changed(struct power_supply
*psy
)
1063 struct sbs_info
*chip
= power_supply_get_drvdata(psy
);
1065 /* cancel outstanding work */
1066 cancel_delayed_work_sync(&chip
->work
);
1068 schedule_delayed_work(&chip
->work
, HZ
);
1069 chip
->poll_time
= chip
->poll_retry_count
;
1072 static void sbs_delayed_work(struct work_struct
*work
)
1074 struct sbs_info
*chip
;
1077 chip
= container_of(work
, struct sbs_info
, work
.work
);
1079 ret
= sbs_read_word_data(chip
->client
, sbs_data
[REG_STATUS
].addr
);
1080 /* if the read failed, give up on this work */
1082 chip
->poll_time
= 0;
1086 if (ret
& BATTERY_FULL_CHARGED
)
1087 ret
= POWER_SUPPLY_STATUS_FULL
;
1088 else if (ret
& BATTERY_DISCHARGING
)
1089 ret
= POWER_SUPPLY_STATUS_DISCHARGING
;
1091 ret
= POWER_SUPPLY_STATUS_CHARGING
;
1093 sbs_status_correct(chip
->client
, &ret
);
1095 if (chip
->last_state
!= ret
) {
1096 chip
->poll_time
= 0;
1097 power_supply_changed(chip
->power_supply
);
1100 if (chip
->poll_time
> 0) {
1101 schedule_delayed_work(&chip
->work
, HZ
);
1107 static const struct power_supply_desc sbs_default_desc
= {
1108 .type
= POWER_SUPPLY_TYPE_BATTERY
,
1109 .properties
= sbs_properties
,
1110 .num_properties
= ARRAY_SIZE(sbs_properties
),
1111 .get_property
= sbs_get_property
,
1112 .external_power_changed
= sbs_external_power_changed
,
1115 static int sbs_probe(struct i2c_client
*client
)
1117 struct sbs_info
*chip
;
1118 struct power_supply_desc
*sbs_desc
;
1119 struct sbs_platform_data
*pdata
= client
->dev
.platform_data
;
1120 struct power_supply_config psy_cfg
= {};
1124 sbs_desc
= devm_kmemdup(&client
->dev
, &sbs_default_desc
,
1125 sizeof(*sbs_desc
), GFP_KERNEL
);
1129 sbs_desc
->name
= devm_kasprintf(&client
->dev
, GFP_KERNEL
, "sbs-%s",
1130 dev_name(&client
->dev
));
1131 if (!sbs_desc
->name
)
1134 chip
= devm_kzalloc(&client
->dev
, sizeof(struct sbs_info
), GFP_KERNEL
);
1138 chip
->flags
= (uintptr_t)i2c_get_match_data(client
);
1139 chip
->client
= client
;
1140 psy_cfg
.of_node
= client
->dev
.of_node
;
1141 psy_cfg
.drv_data
= chip
;
1142 chip
->last_state
= POWER_SUPPLY_STATUS_UNKNOWN
;
1143 sbs_invalidate_cached_props(chip
);
1144 mutex_init(&chip
->mode_lock
);
1146 /* use pdata if available, fall back to DT properties,
1147 * or hardcoded defaults if not
1149 rc
= device_property_read_u32(&client
->dev
, "sbs,i2c-retry-count",
1150 &chip
->i2c_retry_count
);
1152 chip
->i2c_retry_count
= 0;
1154 rc
= device_property_read_u32(&client
->dev
, "sbs,poll-retry-count",
1155 &chip
->poll_retry_count
);
1157 chip
->poll_retry_count
= 0;
1160 chip
->poll_retry_count
= pdata
->poll_retry_count
;
1161 chip
->i2c_retry_count
= pdata
->i2c_retry_count
;
1163 chip
->i2c_retry_count
= chip
->i2c_retry_count
+ 1;
1165 chip
->charger_broadcasts
= !device_property_read_bool(&client
->dev
,
1166 "sbs,disable-charger-broadcasts");
1168 chip
->gpio_detect
= devm_gpiod_get_optional(&client
->dev
,
1169 "sbs,battery-detect", GPIOD_IN
);
1170 if (IS_ERR(chip
->gpio_detect
))
1171 return dev_err_probe(&client
->dev
, PTR_ERR(chip
->gpio_detect
),
1172 "Failed to get gpio\n");
1174 i2c_set_clientdata(client
, chip
);
1176 if (!chip
->gpio_detect
)
1179 irq
= gpiod_to_irq(chip
->gpio_detect
);
1181 dev_warn(&client
->dev
, "Failed to get gpio as irq: %d\n", irq
);
1185 rc
= devm_request_threaded_irq(&client
->dev
, irq
, NULL
, sbs_irq
,
1186 IRQF_TRIGGER_RISING
| IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
1187 dev_name(&client
->dev
), chip
);
1189 dev_warn(&client
->dev
, "Failed to request irq: %d\n", rc
);
1195 * Before we register, we might need to make sure we can actually talk
1198 if (!(force_load
|| chip
->gpio_detect
)) {
1199 union power_supply_propval val
;
1201 rc
= sbs_get_battery_presence_and_health(
1202 client
, POWER_SUPPLY_PROP_PRESENT
, &val
);
1203 if (rc
< 0 || !val
.intval
)
1204 return dev_err_probe(&client
->dev
, -ENODEV
,
1205 "Failed to get present status\n");
1208 rc
= devm_delayed_work_autocancel(&client
->dev
, &chip
->work
,
1213 chip
->power_supply
= devm_power_supply_register(&client
->dev
, sbs_desc
,
1215 if (IS_ERR(chip
->power_supply
))
1216 return dev_err_probe(&client
->dev
, PTR_ERR(chip
->power_supply
),
1217 "Failed to register power supply\n");
1219 dev_info(&client
->dev
,
1220 "%s: battery gas gauge device registered\n", client
->name
);
1225 #if defined CONFIG_PM_SLEEP
1227 static int sbs_suspend(struct device
*dev
)
1229 struct i2c_client
*client
= to_i2c_client(dev
);
1230 struct sbs_info
*chip
= i2c_get_clientdata(client
);
1233 if (chip
->poll_time
> 0)
1234 cancel_delayed_work_sync(&chip
->work
);
1236 if (chip
->flags
& SBS_FLAGS_TI_BQ20ZX5
) {
1237 /* Write to manufacturer access with sleep command. */
1238 ret
= sbs_write_word_data(client
,
1239 sbs_data
[REG_MANUFACTURER_DATA
].addr
,
1240 MANUFACTURER_ACCESS_SLEEP
);
1241 if (chip
->is_present
&& ret
< 0)
1248 static SIMPLE_DEV_PM_OPS(sbs_pm_ops
, sbs_suspend
, NULL
);
1249 #define SBS_PM_OPS (&sbs_pm_ops)
1252 #define SBS_PM_OPS NULL
1255 static const struct i2c_device_id sbs_id
[] = {
1256 { "bq20z65", SBS_FLAGS_TI_BQ20ZX5
},
1257 { "bq20z75", SBS_FLAGS_TI_BQ20ZX5
},
1258 { "sbs-battery", 0 },
1261 MODULE_DEVICE_TABLE(i2c
, sbs_id
);
1263 static const struct of_device_id sbs_dt_ids
[] = {
1264 { .compatible
= "sbs,sbs-battery" },
1266 .compatible
= "ti,bq20z65",
1267 .data
= (void *)SBS_FLAGS_TI_BQ20ZX5
,
1270 .compatible
= "ti,bq20z75",
1271 .data
= (void *)SBS_FLAGS_TI_BQ20ZX5
,
1275 MODULE_DEVICE_TABLE(of
, sbs_dt_ids
);
1277 static struct i2c_driver sbs_battery_driver
= {
1282 .name
= "sbs-battery",
1283 .of_match_table
= sbs_dt_ids
,
1287 module_i2c_driver(sbs_battery_driver
);
1289 MODULE_DESCRIPTION("SBS battery monitor driver");
1290 MODULE_LICENSE("GPL");
1292 module_param(force_load
, bool, 0444);
1293 MODULE_PARM_DESC(force_load
,
1294 "Attempt to load the driver even if no battery is connected");