1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2018 Spreadtrum Communications Inc.
4 #include <linux/gpio/consumer.h>
5 #include <linux/iio/consumer.h>
6 #include <linux/interrupt.h>
7 #include <linux/kernel.h>
8 #include <linux/math64.h>
9 #include <linux/module.h>
10 #include <linux/nvmem-consumer.h>
12 #include <linux/platform_device.h>
13 #include <linux/power_supply.h>
14 #include <linux/regmap.h>
15 #include <linux/slab.h>
17 /* PMIC global control registers definition */
18 #define SC27XX_MODULE_EN0 0xc08
19 #define SC27XX_CLK_EN0 0xc18
20 #define SC27XX_FGU_EN BIT(7)
21 #define SC27XX_FGU_RTC_EN BIT(6)
23 /* FGU registers definition */
24 #define SC27XX_FGU_START 0x0
25 #define SC27XX_FGU_CONFIG 0x4
26 #define SC27XX_FGU_ADC_CONFIG 0x8
27 #define SC27XX_FGU_STATUS 0xc
28 #define SC27XX_FGU_INT_EN 0x10
29 #define SC27XX_FGU_INT_CLR 0x14
30 #define SC27XX_FGU_INT_STS 0x1c
31 #define SC27XX_FGU_VOLTAGE 0x20
32 #define SC27XX_FGU_OCV 0x24
33 #define SC27XX_FGU_POCV 0x28
34 #define SC27XX_FGU_CURRENT 0x2c
35 #define SC27XX_FGU_LOW_OVERLOAD 0x34
36 #define SC27XX_FGU_CLBCNT_SETH 0x50
37 #define SC27XX_FGU_CLBCNT_SETL 0x54
38 #define SC27XX_FGU_CLBCNT_DELTH 0x58
39 #define SC27XX_FGU_CLBCNT_DELTL 0x5c
40 #define SC27XX_FGU_CLBCNT_VALH 0x68
41 #define SC27XX_FGU_CLBCNT_VALL 0x6c
42 #define SC27XX_FGU_CLBCNT_QMAXL 0x74
43 #define SC27XX_FGU_USER_AREA_SET 0xa0
44 #define SC27XX_FGU_USER_AREA_CLEAR 0xa4
45 #define SC27XX_FGU_USER_AREA_STATUS 0xa8
46 #define SC27XX_FGU_VOLTAGE_BUF 0xd0
47 #define SC27XX_FGU_CURRENT_BUF 0xf0
49 #define SC27XX_WRITE_SELCLB_EN BIT(0)
50 #define SC27XX_FGU_CLBCNT_MASK GENMASK(15, 0)
51 #define SC27XX_FGU_CLBCNT_SHIFT 16
52 #define SC27XX_FGU_LOW_OVERLOAD_MASK GENMASK(12, 0)
54 #define SC27XX_FGU_INT_MASK GENMASK(9, 0)
55 #define SC27XX_FGU_LOW_OVERLOAD_INT BIT(0)
56 #define SC27XX_FGU_CLBCNT_DELTA_INT BIT(2)
58 #define SC27XX_FGU_MODE_AREA_MASK GENMASK(15, 12)
59 #define SC27XX_FGU_CAP_AREA_MASK GENMASK(11, 0)
60 #define SC27XX_FGU_MODE_AREA_SHIFT 12
62 #define SC27XX_FGU_FIRST_POWERTON GENMASK(3, 0)
63 #define SC27XX_FGU_DEFAULT_CAP GENMASK(11, 0)
64 #define SC27XX_FGU_NORMAIL_POWERTON 0x5
66 #define SC27XX_FGU_CUR_BASIC_ADC 8192
67 #define SC27XX_FGU_SAMPLE_HZ 2
69 #define SC27XX_FGU_IDEAL_RESISTANCE 20000
72 * struct sc27xx_fgu_data: describe the FGU device
73 * @regmap: regmap for register access
74 * @dev: platform device
75 * @battery: battery power supply
76 * @base: the base offset for the controller
77 * @lock: protect the structure
78 * @gpiod: GPIO for battery detection
79 * @channel: IIO channel to get battery temperature
80 * @charge_chan: IIO channel to get charge voltage
81 * @internal_resist: the battery internal resistance in mOhm
82 * @total_cap: the total capacity of the battery in mAh
83 * @init_cap: the initial capacity of the battery in mAh
84 * @alarm_cap: the alarm capacity
85 * @init_clbcnt: the initial coulomb counter
86 * @max_volt: the maximum constant input voltage in millivolt
87 * @min_volt: the minimum drained battery voltage in microvolt
88 * @boot_volt: the voltage measured during boot in microvolt
89 * @table_len: the capacity table length
90 * @resist_table_len: the resistance table length
91 * @cur_1000ma_adc: ADC value corresponding to 1000 mA
92 * @vol_1000mv_adc: ADC value corresponding to 1000 mV
93 * @calib_resist: the real resistance of coulomb counter chip in uOhm
94 * @cap_table: capacity table with corresponding ocv
95 * @resist_table: resistance percent table with corresponding temperature
97 struct sc27xx_fgu_data
{
98 struct regmap
*regmap
;
100 struct power_supply
*battery
;
103 struct gpio_desc
*gpiod
;
104 struct iio_channel
*channel
;
105 struct iio_channel
*charge_chan
;
116 int resist_table_len
;
120 struct power_supply_battery_ocv_table
*cap_table
;
121 struct power_supply_resistance_temp_table
*resist_table
;
124 static int sc27xx_fgu_cap_to_clbcnt(struct sc27xx_fgu_data
*data
, int capacity
);
125 static void sc27xx_fgu_capacity_calibration(struct sc27xx_fgu_data
*data
,
126 int cap
, bool int_mode
);
127 static void sc27xx_fgu_adjust_cap(struct sc27xx_fgu_data
*data
, int cap
);
128 static int sc27xx_fgu_get_temp(struct sc27xx_fgu_data
*data
, int *temp
);
130 static const char * const sc27xx_charger_supply_name
[] = {
137 static int sc27xx_fgu_adc_to_current(struct sc27xx_fgu_data
*data
, s64 adc
)
139 return DIV_S64_ROUND_CLOSEST(adc
* 1000, data
->cur_1000ma_adc
);
142 static int sc27xx_fgu_adc_to_voltage(struct sc27xx_fgu_data
*data
, s64 adc
)
144 return DIV_S64_ROUND_CLOSEST(adc
* 1000, data
->vol_1000mv_adc
);
147 static int sc27xx_fgu_voltage_to_adc(struct sc27xx_fgu_data
*data
, int vol
)
149 return DIV_ROUND_CLOSEST(vol
* data
->vol_1000mv_adc
, 1000);
152 static bool sc27xx_fgu_is_first_poweron(struct sc27xx_fgu_data
*data
)
154 int ret
, status
, cap
, mode
;
156 ret
= regmap_read(data
->regmap
,
157 data
->base
+ SC27XX_FGU_USER_AREA_STATUS
, &status
);
162 * We use low 4 bits to save the last battery capacity and high 12 bits
163 * to save the system boot mode.
165 mode
= (status
& SC27XX_FGU_MODE_AREA_MASK
) >> SC27XX_FGU_MODE_AREA_SHIFT
;
166 cap
= status
& SC27XX_FGU_CAP_AREA_MASK
;
169 * When FGU has been powered down, the user area registers became
170 * default value (0xffff), which can be used to valid if the system is
171 * first power on or not.
173 if (mode
== SC27XX_FGU_FIRST_POWERTON
|| cap
== SC27XX_FGU_DEFAULT_CAP
)
179 static int sc27xx_fgu_save_boot_mode(struct sc27xx_fgu_data
*data
,
184 ret
= regmap_update_bits(data
->regmap
,
185 data
->base
+ SC27XX_FGU_USER_AREA_CLEAR
,
186 SC27XX_FGU_MODE_AREA_MASK
,
187 SC27XX_FGU_MODE_AREA_MASK
);
192 * Since the user area registers are put on power always-on region,
193 * then these registers changing time will be a little long. Thus
194 * here we should delay 200us to wait until values are updated
195 * successfully according to the datasheet.
199 ret
= regmap_update_bits(data
->regmap
,
200 data
->base
+ SC27XX_FGU_USER_AREA_SET
,
201 SC27XX_FGU_MODE_AREA_MASK
,
202 boot_mode
<< SC27XX_FGU_MODE_AREA_SHIFT
);
207 * Since the user area registers are put on power always-on region,
208 * then these registers changing time will be a little long. Thus
209 * here we should delay 200us to wait until values are updated
210 * successfully according to the datasheet.
215 * According to the datasheet, we should set the USER_AREA_CLEAR to 0 to
216 * make the user area data available, otherwise we can not save the user
219 return regmap_update_bits(data
->regmap
,
220 data
->base
+ SC27XX_FGU_USER_AREA_CLEAR
,
221 SC27XX_FGU_MODE_AREA_MASK
, 0);
224 static int sc27xx_fgu_save_last_cap(struct sc27xx_fgu_data
*data
, int cap
)
228 ret
= regmap_update_bits(data
->regmap
,
229 data
->base
+ SC27XX_FGU_USER_AREA_CLEAR
,
230 SC27XX_FGU_CAP_AREA_MASK
,
231 SC27XX_FGU_CAP_AREA_MASK
);
236 * Since the user area registers are put on power always-on region,
237 * then these registers changing time will be a little long. Thus
238 * here we should delay 200us to wait until values are updated
239 * successfully according to the datasheet.
243 ret
= regmap_update_bits(data
->regmap
,
244 data
->base
+ SC27XX_FGU_USER_AREA_SET
,
245 SC27XX_FGU_CAP_AREA_MASK
, cap
);
250 * Since the user area registers are put on power always-on region,
251 * then these registers changing time will be a little long. Thus
252 * here we should delay 200us to wait until values are updated
253 * successfully according to the datasheet.
258 * According to the datasheet, we should set the USER_AREA_CLEAR to 0 to
259 * make the user area data available, otherwise we can not save the user
262 return regmap_update_bits(data
->regmap
,
263 data
->base
+ SC27XX_FGU_USER_AREA_CLEAR
,
264 SC27XX_FGU_CAP_AREA_MASK
, 0);
267 static int sc27xx_fgu_read_last_cap(struct sc27xx_fgu_data
*data
, int *cap
)
271 ret
= regmap_read(data
->regmap
,
272 data
->base
+ SC27XX_FGU_USER_AREA_STATUS
, &value
);
276 *cap
= value
& SC27XX_FGU_CAP_AREA_MASK
;
281 * When system boots on, we can not read battery capacity from coulomb
282 * registers, since now the coulomb registers are invalid. So we should
283 * calculate the battery open circuit voltage, and get current battery
284 * capacity according to the capacity table.
286 static int sc27xx_fgu_get_boot_capacity(struct sc27xx_fgu_data
*data
, int *cap
)
288 int volt
, cur
, oci
, ocv
, ret
;
289 bool is_first_poweron
= sc27xx_fgu_is_first_poweron(data
);
292 * If system is not the first power on, we should use the last saved
293 * battery capacity as the initial battery capacity. Otherwise we should
294 * re-calculate the initial battery capacity.
296 if (!is_first_poweron
) {
297 ret
= sc27xx_fgu_read_last_cap(data
, cap
);
301 return sc27xx_fgu_save_boot_mode(data
, SC27XX_FGU_NORMAIL_POWERTON
);
305 * After system booting on, the SC27XX_FGU_CLBCNT_QMAXL register saved
306 * the first sampled open circuit current.
308 ret
= regmap_read(data
->regmap
, data
->base
+ SC27XX_FGU_CLBCNT_QMAXL
,
314 oci
= sc27xx_fgu_adc_to_current(data
, cur
- SC27XX_FGU_CUR_BASIC_ADC
);
317 * Should get the OCV from SC27XX_FGU_POCV register at the system
318 * beginning. It is ADC values reading from registers which need to
319 * convert the corresponding voltage.
321 ret
= regmap_read(data
->regmap
, data
->base
+ SC27XX_FGU_POCV
, &volt
);
325 volt
= sc27xx_fgu_adc_to_voltage(data
, volt
);
326 ocv
= volt
* 1000 - oci
* data
->internal_resist
;
327 data
->boot_volt
= ocv
;
330 * Parse the capacity table to look up the correct capacity percent
331 * according to current battery's corresponding OCV values.
333 *cap
= power_supply_ocv2cap_simple(data
->cap_table
, data
->table_len
,
336 ret
= sc27xx_fgu_save_last_cap(data
, *cap
);
340 return sc27xx_fgu_save_boot_mode(data
, SC27XX_FGU_NORMAIL_POWERTON
);
343 static int sc27xx_fgu_set_clbcnt(struct sc27xx_fgu_data
*data
, int clbcnt
)
347 ret
= regmap_update_bits(data
->regmap
,
348 data
->base
+ SC27XX_FGU_CLBCNT_SETL
,
349 SC27XX_FGU_CLBCNT_MASK
, clbcnt
);
353 ret
= regmap_update_bits(data
->regmap
,
354 data
->base
+ SC27XX_FGU_CLBCNT_SETH
,
355 SC27XX_FGU_CLBCNT_MASK
,
356 clbcnt
>> SC27XX_FGU_CLBCNT_SHIFT
);
360 return regmap_update_bits(data
->regmap
, data
->base
+ SC27XX_FGU_START
,
361 SC27XX_WRITE_SELCLB_EN
,
362 SC27XX_WRITE_SELCLB_EN
);
365 static int sc27xx_fgu_get_clbcnt(struct sc27xx_fgu_data
*data
, int *clb_cnt
)
369 ret
= regmap_read(data
->regmap
, data
->base
+ SC27XX_FGU_CLBCNT_VALL
,
374 ret
= regmap_read(data
->regmap
, data
->base
+ SC27XX_FGU_CLBCNT_VALH
,
379 *clb_cnt
= ccl
& SC27XX_FGU_CLBCNT_MASK
;
380 *clb_cnt
|= (cch
& SC27XX_FGU_CLBCNT_MASK
) << SC27XX_FGU_CLBCNT_SHIFT
;
385 static int sc27xx_fgu_get_vol_now(struct sc27xx_fgu_data
*data
, int *val
)
390 ret
= regmap_read(data
->regmap
, data
->base
+ SC27XX_FGU_VOLTAGE_BUF
,
396 * It is ADC values reading from registers which need to convert to
397 * corresponding voltage values.
399 *val
= sc27xx_fgu_adc_to_voltage(data
, vol
);
404 static int sc27xx_fgu_get_cur_now(struct sc27xx_fgu_data
*data
, int *val
)
409 ret
= regmap_read(data
->regmap
, data
->base
+ SC27XX_FGU_CURRENT_BUF
,
415 * It is ADC values reading from registers which need to convert to
416 * corresponding current values.
418 *val
= sc27xx_fgu_adc_to_current(data
, cur
- SC27XX_FGU_CUR_BASIC_ADC
);
423 static int sc27xx_fgu_get_capacity(struct sc27xx_fgu_data
*data
, int *cap
)
425 int ret
, cur_clbcnt
, delta_clbcnt
, delta_cap
, temp
;
427 /* Get current coulomb counters firstly */
428 ret
= sc27xx_fgu_get_clbcnt(data
, &cur_clbcnt
);
432 delta_clbcnt
= cur_clbcnt
- data
->init_clbcnt
;
435 * Convert coulomb counter to delta capacity (mAh), and set multiplier
436 * as 10 to improve the precision.
438 temp
= DIV_ROUND_CLOSEST(delta_clbcnt
* 10, 36 * SC27XX_FGU_SAMPLE_HZ
);
439 temp
= sc27xx_fgu_adc_to_current(data
, temp
/ 1000);
442 * Convert to capacity percent of the battery total capacity,
443 * and multiplier is 100 too.
445 delta_cap
= DIV_ROUND_CLOSEST(temp
* 100, data
->total_cap
);
446 *cap
= delta_cap
+ data
->init_cap
;
448 /* Calibrate the battery capacity in a normal range. */
449 sc27xx_fgu_capacity_calibration(data
, *cap
, false);
454 static int sc27xx_fgu_get_vbat_vol(struct sc27xx_fgu_data
*data
, int *val
)
458 ret
= regmap_read(data
->regmap
, data
->base
+ SC27XX_FGU_VOLTAGE
, &vol
);
463 * It is ADC values reading from registers which need to convert to
464 * corresponding voltage values.
466 *val
= sc27xx_fgu_adc_to_voltage(data
, vol
);
471 static int sc27xx_fgu_get_current(struct sc27xx_fgu_data
*data
, int *val
)
475 ret
= regmap_read(data
->regmap
, data
->base
+ SC27XX_FGU_CURRENT
, &cur
);
480 * It is ADC values reading from registers which need to convert to
481 * corresponding current values.
483 *val
= sc27xx_fgu_adc_to_current(data
, cur
- SC27XX_FGU_CUR_BASIC_ADC
);
488 static int sc27xx_fgu_get_vbat_ocv(struct sc27xx_fgu_data
*data
, int *val
)
490 int vol
, cur
, ret
, temp
, resistance
;
492 ret
= sc27xx_fgu_get_vbat_vol(data
, &vol
);
496 ret
= sc27xx_fgu_get_current(data
, &cur
);
500 resistance
= data
->internal_resist
;
501 if (data
->resist_table_len
> 0) {
502 ret
= sc27xx_fgu_get_temp(data
, &temp
);
506 resistance
= power_supply_temp2resist_simple(data
->resist_table
,
507 data
->resist_table_len
, temp
);
508 resistance
= data
->internal_resist
* resistance
/ 100;
511 /* Return the battery OCV in micro volts. */
512 *val
= vol
* 1000 - cur
* resistance
;
517 static int sc27xx_fgu_get_charge_vol(struct sc27xx_fgu_data
*data
, int *val
)
521 ret
= iio_read_channel_processed(data
->charge_chan
, &vol
);
529 static int sc27xx_fgu_get_temp(struct sc27xx_fgu_data
*data
, int *temp
)
531 return iio_read_channel_processed(data
->channel
, temp
);
534 static int sc27xx_fgu_get_health(struct sc27xx_fgu_data
*data
, int *health
)
538 ret
= sc27xx_fgu_get_vbat_vol(data
, &vol
);
542 if (vol
> data
->max_volt
)
543 *health
= POWER_SUPPLY_HEALTH_OVERVOLTAGE
;
545 *health
= POWER_SUPPLY_HEALTH_GOOD
;
550 static int sc27xx_fgu_get_status(struct sc27xx_fgu_data
*data
, int *status
)
552 union power_supply_propval val
;
553 struct power_supply
*psy
;
554 int i
, ret
= -EINVAL
;
556 for (i
= 0; i
< ARRAY_SIZE(sc27xx_charger_supply_name
); i
++) {
557 psy
= power_supply_get_by_name(sc27xx_charger_supply_name
[i
]);
561 ret
= power_supply_get_property(psy
, POWER_SUPPLY_PROP_STATUS
,
563 power_supply_put(psy
);
567 *status
= val
.intval
;
573 static int sc27xx_fgu_get_property(struct power_supply
*psy
,
574 enum power_supply_property psp
,
575 union power_supply_propval
*val
)
577 struct sc27xx_fgu_data
*data
= power_supply_get_drvdata(psy
);
581 mutex_lock(&data
->lock
);
584 case POWER_SUPPLY_PROP_STATUS
:
585 ret
= sc27xx_fgu_get_status(data
, &value
);
592 case POWER_SUPPLY_PROP_HEALTH
:
593 ret
= sc27xx_fgu_get_health(data
, &value
);
600 case POWER_SUPPLY_PROP_PRESENT
:
601 val
->intval
= data
->bat_present
;
604 case POWER_SUPPLY_PROP_TEMP
:
605 ret
= sc27xx_fgu_get_temp(data
, &value
);
612 case POWER_SUPPLY_PROP_TECHNOLOGY
:
613 val
->intval
= POWER_SUPPLY_TECHNOLOGY_LION
;
616 case POWER_SUPPLY_PROP_CAPACITY
:
617 ret
= sc27xx_fgu_get_capacity(data
, &value
);
624 case POWER_SUPPLY_PROP_VOLTAGE_AVG
:
625 ret
= sc27xx_fgu_get_vbat_vol(data
, &value
);
629 val
->intval
= value
* 1000;
632 case POWER_SUPPLY_PROP_VOLTAGE_OCV
:
633 ret
= sc27xx_fgu_get_vbat_ocv(data
, &value
);
640 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE
:
641 ret
= sc27xx_fgu_get_charge_vol(data
, &value
);
648 case POWER_SUPPLY_PROP_CURRENT_AVG
:
649 ret
= sc27xx_fgu_get_current(data
, &value
);
653 val
->intval
= value
* 1000;
656 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN
:
657 val
->intval
= data
->total_cap
* 1000;
660 case POWER_SUPPLY_PROP_CHARGE_NOW
:
661 ret
= sc27xx_fgu_get_clbcnt(data
, &value
);
665 value
= DIV_ROUND_CLOSEST(value
* 10,
666 36 * SC27XX_FGU_SAMPLE_HZ
);
667 val
->intval
= sc27xx_fgu_adc_to_current(data
, value
);
671 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
672 ret
= sc27xx_fgu_get_vol_now(data
, &value
);
676 val
->intval
= value
* 1000;
679 case POWER_SUPPLY_PROP_CURRENT_NOW
:
680 ret
= sc27xx_fgu_get_cur_now(data
, &value
);
684 val
->intval
= value
* 1000;
687 case POWER_SUPPLY_PROP_VOLTAGE_BOOT
:
688 val
->intval
= data
->boot_volt
;
697 mutex_unlock(&data
->lock
);
701 static int sc27xx_fgu_set_property(struct power_supply
*psy
,
702 enum power_supply_property psp
,
703 const union power_supply_propval
*val
)
705 struct sc27xx_fgu_data
*data
= power_supply_get_drvdata(psy
);
708 mutex_lock(&data
->lock
);
711 case POWER_SUPPLY_PROP_CAPACITY
:
712 ret
= sc27xx_fgu_save_last_cap(data
, val
->intval
);
714 dev_err(data
->dev
, "failed to save battery capacity\n");
717 case POWER_SUPPLY_PROP_CALIBRATE
:
718 sc27xx_fgu_adjust_cap(data
, val
->intval
);
722 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN
:
723 data
->total_cap
= val
->intval
/ 1000;
731 mutex_unlock(&data
->lock
);
736 static int sc27xx_fgu_property_is_writeable(struct power_supply
*psy
,
737 enum power_supply_property psp
)
739 return psp
== POWER_SUPPLY_PROP_CAPACITY
||
740 psp
== POWER_SUPPLY_PROP_CALIBRATE
||
741 psp
== POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN
;
744 static enum power_supply_property sc27xx_fgu_props
[] = {
745 POWER_SUPPLY_PROP_STATUS
,
746 POWER_SUPPLY_PROP_HEALTH
,
747 POWER_SUPPLY_PROP_PRESENT
,
748 POWER_SUPPLY_PROP_TEMP
,
749 POWER_SUPPLY_PROP_TECHNOLOGY
,
750 POWER_SUPPLY_PROP_CAPACITY
,
751 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
752 POWER_SUPPLY_PROP_VOLTAGE_OCV
,
753 POWER_SUPPLY_PROP_VOLTAGE_AVG
,
754 POWER_SUPPLY_PROP_VOLTAGE_BOOT
,
755 POWER_SUPPLY_PROP_CURRENT_NOW
,
756 POWER_SUPPLY_PROP_CURRENT_AVG
,
757 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE
,
758 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN
,
759 POWER_SUPPLY_PROP_CALIBRATE
,
760 POWER_SUPPLY_PROP_CHARGE_NOW
763 static const struct power_supply_desc sc27xx_fgu_desc
= {
764 .name
= "sc27xx-fgu",
765 .type
= POWER_SUPPLY_TYPE_BATTERY
,
766 .properties
= sc27xx_fgu_props
,
767 .num_properties
= ARRAY_SIZE(sc27xx_fgu_props
),
768 .get_property
= sc27xx_fgu_get_property
,
769 .set_property
= sc27xx_fgu_set_property
,
770 .external_power_changed
= power_supply_changed
,
771 .property_is_writeable
= sc27xx_fgu_property_is_writeable
,
775 static void sc27xx_fgu_adjust_cap(struct sc27xx_fgu_data
*data
, int cap
)
779 data
->init_cap
= cap
;
780 ret
= sc27xx_fgu_get_clbcnt(data
, &data
->init_clbcnt
);
782 dev_err(data
->dev
, "failed to get init coulomb counter\n");
785 static void sc27xx_fgu_capacity_calibration(struct sc27xx_fgu_data
*data
,
786 int cap
, bool int_mode
)
788 int ret
, ocv
, chg_sts
, adc
;
790 ret
= sc27xx_fgu_get_vbat_ocv(data
, &ocv
);
792 dev_err(data
->dev
, "get battery ocv error.\n");
796 ret
= sc27xx_fgu_get_status(data
, &chg_sts
);
798 dev_err(data
->dev
, "get charger status error.\n");
803 * If we are in charging mode, then we do not need to calibrate the
806 if (chg_sts
== POWER_SUPPLY_STATUS_CHARGING
)
809 if ((ocv
> data
->cap_table
[0].ocv
&& cap
< 100) || cap
> 100) {
811 * If current OCV value is larger than the max OCV value in
812 * OCV table, or the current capacity is larger than 100,
813 * we should force the inititial capacity to 100.
815 sc27xx_fgu_adjust_cap(data
, 100);
816 } else if (ocv
<= data
->cap_table
[data
->table_len
- 1].ocv
) {
818 * If current OCV value is leass than the minimum OCV value in
819 * OCV table, we should force the inititial capacity to 0.
821 sc27xx_fgu_adjust_cap(data
, 0);
822 } else if ((ocv
> data
->cap_table
[data
->table_len
- 1].ocv
&& cap
<= 0) ||
823 (ocv
> data
->min_volt
&& cap
<= data
->alarm_cap
)) {
825 * If current OCV value is not matchable with current capacity,
826 * we should re-calculate current capacity by looking up the
829 int cur_cap
= power_supply_ocv2cap_simple(data
->cap_table
,
830 data
->table_len
, ocv
);
832 sc27xx_fgu_adjust_cap(data
, cur_cap
);
833 } else if (ocv
<= data
->min_volt
) {
835 * If current OCV value is less than the low alarm voltage, but
836 * current capacity is larger than the alarm capacity, we should
837 * adjust the inititial capacity to alarm capacity.
839 if (cap
> data
->alarm_cap
) {
840 sc27xx_fgu_adjust_cap(data
, data
->alarm_cap
);
845 * If current capacity is equal with 0 or less than 0
846 * (some error occurs), we should adjust inititial
847 * capacity to the capacity corresponding to current OCV
850 cur_cap
= power_supply_ocv2cap_simple(data
->cap_table
,
853 sc27xx_fgu_adjust_cap(data
, cur_cap
);
860 * After adjusting the battery capacity, we should set the
861 * lowest alarm voltage instead.
863 data
->min_volt
= data
->cap_table
[data
->table_len
- 1].ocv
;
864 data
->alarm_cap
= power_supply_ocv2cap_simple(data
->cap_table
,
868 adc
= sc27xx_fgu_voltage_to_adc(data
, data
->min_volt
/ 1000);
869 regmap_update_bits(data
->regmap
,
870 data
->base
+ SC27XX_FGU_LOW_OVERLOAD
,
871 SC27XX_FGU_LOW_OVERLOAD_MASK
, adc
);
875 static irqreturn_t
sc27xx_fgu_interrupt(int irq
, void *dev_id
)
877 struct sc27xx_fgu_data
*data
= dev_id
;
881 mutex_lock(&data
->lock
);
883 ret
= regmap_read(data
->regmap
, data
->base
+ SC27XX_FGU_INT_STS
,
888 ret
= regmap_update_bits(data
->regmap
, data
->base
+ SC27XX_FGU_INT_CLR
,
894 * When low overload voltage interrupt happens, we should calibrate the
895 * battery capacity in lower voltage stage.
897 if (!(status
& SC27XX_FGU_LOW_OVERLOAD_INT
))
900 ret
= sc27xx_fgu_get_capacity(data
, &cap
);
904 sc27xx_fgu_capacity_calibration(data
, cap
, true);
907 mutex_unlock(&data
->lock
);
909 power_supply_changed(data
->battery
);
913 static irqreturn_t
sc27xx_fgu_bat_detection(int irq
, void *dev_id
)
915 struct sc27xx_fgu_data
*data
= dev_id
;
918 mutex_lock(&data
->lock
);
920 state
= gpiod_get_value_cansleep(data
->gpiod
);
922 dev_err(data
->dev
, "failed to get gpio state\n");
923 mutex_unlock(&data
->lock
);
924 return IRQ_RETVAL(state
);
927 data
->bat_present
= !!state
;
929 mutex_unlock(&data
->lock
);
931 power_supply_changed(data
->battery
);
935 static void sc27xx_fgu_disable(void *_data
)
937 struct sc27xx_fgu_data
*data
= _data
;
939 regmap_update_bits(data
->regmap
, SC27XX_CLK_EN0
, SC27XX_FGU_RTC_EN
, 0);
940 regmap_update_bits(data
->regmap
, SC27XX_MODULE_EN0
, SC27XX_FGU_EN
, 0);
943 static int sc27xx_fgu_cap_to_clbcnt(struct sc27xx_fgu_data
*data
, int capacity
)
946 * Get current capacity (mAh) = battery total capacity (mAh) *
947 * current capacity percent (capacity / 100).
949 int cur_cap
= DIV_ROUND_CLOSEST(data
->total_cap
* capacity
, 100);
952 * Convert current capacity (mAh) to coulomb counter according to the
953 * formula: 1 mAh =3.6 coulomb.
955 return DIV_ROUND_CLOSEST(cur_cap
* 36 * data
->cur_1000ma_adc
* SC27XX_FGU_SAMPLE_HZ
, 10);
958 static int sc27xx_fgu_calibration(struct sc27xx_fgu_data
*data
)
960 struct nvmem_cell
*cell
;
961 int calib_data
, cal_4200mv
;
965 cell
= nvmem_cell_get(data
->dev
, "fgu_calib");
967 return PTR_ERR(cell
);
969 buf
= nvmem_cell_read(cell
, &len
);
970 nvmem_cell_put(cell
);
975 memcpy(&calib_data
, buf
, min(len
, sizeof(u32
)));
978 * Get the ADC value corresponding to 4200 mV from eFuse controller
979 * according to below formula. Then convert to ADC values corresponding
980 * to 1000 mV and 1000 mA.
982 cal_4200mv
= (calib_data
& 0x1ff) + 6963 - 4096 - 256;
983 data
->vol_1000mv_adc
= DIV_ROUND_CLOSEST(cal_4200mv
* 10, 42);
984 data
->cur_1000ma_adc
=
985 DIV_ROUND_CLOSEST(data
->vol_1000mv_adc
* 4 * data
->calib_resist
,
986 SC27XX_FGU_IDEAL_RESISTANCE
);
992 static int sc27xx_fgu_hw_init(struct sc27xx_fgu_data
*data
)
994 struct power_supply_battery_info
*info
;
995 const struct power_supply_battery_ocv_table
*table
;
996 int ret
, delta_clbcnt
, alarm_adc
;
998 ret
= power_supply_get_battery_info(data
->battery
, &info
);
1000 dev_err(data
->dev
, "failed to get battery information\n");
1004 data
->total_cap
= info
->charge_full_design_uah
/ 1000;
1005 data
->max_volt
= info
->constant_charge_voltage_max_uv
/ 1000;
1006 data
->internal_resist
= info
->factory_internal_resistance_uohm
/ 1000;
1007 data
->min_volt
= info
->voltage_min_design_uv
;
1010 * For SC27XX fuel gauge device, we only use one ocv-capacity
1011 * table in normal temperature 20 Celsius.
1013 table
= power_supply_find_ocv2cap_table(info
, 20, &data
->table_len
);
1017 data
->cap_table
= devm_kmemdup(data
->dev
, table
,
1018 data
->table_len
* sizeof(*table
),
1020 if (!data
->cap_table
) {
1021 power_supply_put_battery_info(data
->battery
, info
);
1025 data
->alarm_cap
= power_supply_ocv2cap_simple(data
->cap_table
,
1028 if (!data
->alarm_cap
)
1029 data
->alarm_cap
+= 1;
1031 data
->resist_table_len
= info
->resist_table_size
;
1032 if (data
->resist_table_len
> 0) {
1033 data
->resist_table
= devm_kmemdup(data
->dev
, info
->resist_table
,
1034 data
->resist_table_len
*
1035 sizeof(struct power_supply_resistance_temp_table
),
1037 if (!data
->resist_table
) {
1038 power_supply_put_battery_info(data
->battery
, info
);
1043 power_supply_put_battery_info(data
->battery
, info
);
1045 ret
= sc27xx_fgu_calibration(data
);
1049 /* Enable the FGU module */
1050 ret
= regmap_update_bits(data
->regmap
, SC27XX_MODULE_EN0
,
1051 SC27XX_FGU_EN
, SC27XX_FGU_EN
);
1053 dev_err(data
->dev
, "failed to enable fgu\n");
1057 /* Enable the FGU RTC clock to make it work */
1058 ret
= regmap_update_bits(data
->regmap
, SC27XX_CLK_EN0
,
1059 SC27XX_FGU_RTC_EN
, SC27XX_FGU_RTC_EN
);
1061 dev_err(data
->dev
, "failed to enable fgu RTC clock\n");
1065 ret
= regmap_update_bits(data
->regmap
, data
->base
+ SC27XX_FGU_INT_CLR
,
1066 SC27XX_FGU_INT_MASK
, SC27XX_FGU_INT_MASK
);
1068 dev_err(data
->dev
, "failed to clear interrupt status\n");
1073 * Set the voltage low overload threshold, which means when the battery
1074 * voltage is lower than this threshold, the controller will generate
1075 * one interrupt to notify.
1077 alarm_adc
= sc27xx_fgu_voltage_to_adc(data
, data
->min_volt
/ 1000);
1078 ret
= regmap_update_bits(data
->regmap
, data
->base
+ SC27XX_FGU_LOW_OVERLOAD
,
1079 SC27XX_FGU_LOW_OVERLOAD_MASK
, alarm_adc
);
1081 dev_err(data
->dev
, "failed to set fgu low overload\n");
1086 * Set the coulomb counter delta threshold, that means when the coulomb
1087 * counter change is multiples of the delta threshold, the controller
1088 * will generate one interrupt to notify the users to update the battery
1089 * capacity. Now we set the delta threshold as a counter value of 1%
1092 delta_clbcnt
= sc27xx_fgu_cap_to_clbcnt(data
, 1);
1094 ret
= regmap_update_bits(data
->regmap
, data
->base
+ SC27XX_FGU_CLBCNT_DELTL
,
1095 SC27XX_FGU_CLBCNT_MASK
, delta_clbcnt
);
1097 dev_err(data
->dev
, "failed to set low delta coulomb counter\n");
1101 ret
= regmap_update_bits(data
->regmap
, data
->base
+ SC27XX_FGU_CLBCNT_DELTH
,
1102 SC27XX_FGU_CLBCNT_MASK
,
1103 delta_clbcnt
>> SC27XX_FGU_CLBCNT_SHIFT
);
1105 dev_err(data
->dev
, "failed to set high delta coulomb counter\n");
1110 * Get the boot battery capacity when system powers on, which is used to
1111 * initialize the coulomb counter. After that, we can read the coulomb
1112 * counter to measure the battery capacity.
1114 ret
= sc27xx_fgu_get_boot_capacity(data
, &data
->init_cap
);
1116 dev_err(data
->dev
, "failed to get boot capacity\n");
1121 * Convert battery capacity to the corresponding initial coulomb counter
1122 * and set into coulomb counter registers.
1124 data
->init_clbcnt
= sc27xx_fgu_cap_to_clbcnt(data
, data
->init_cap
);
1125 ret
= sc27xx_fgu_set_clbcnt(data
, data
->init_clbcnt
);
1127 dev_err(data
->dev
, "failed to initialize coulomb counter\n");
1134 regmap_update_bits(data
->regmap
, SC27XX_CLK_EN0
, SC27XX_FGU_RTC_EN
, 0);
1136 regmap_update_bits(data
->regmap
, SC27XX_MODULE_EN0
, SC27XX_FGU_EN
, 0);
1141 static int sc27xx_fgu_probe(struct platform_device
*pdev
)
1143 struct device
*dev
= &pdev
->dev
;
1144 struct device_node
*np
= dev
->of_node
;
1145 struct power_supply_config fgu_cfg
= { };
1146 struct sc27xx_fgu_data
*data
;
1149 data
= devm_kzalloc(dev
, sizeof(*data
), GFP_KERNEL
);
1153 data
->regmap
= dev_get_regmap(dev
->parent
, NULL
);
1154 if (!data
->regmap
) {
1155 dev_err(dev
, "failed to get regmap\n");
1159 ret
= device_property_read_u32(dev
, "reg", &data
->base
);
1161 dev_err(dev
, "failed to get fgu address\n");
1165 ret
= device_property_read_u32(&pdev
->dev
,
1166 "sprd,calib-resistance-micro-ohms",
1167 &data
->calib_resist
);
1170 "failed to get fgu calibration resistance\n");
1174 data
->channel
= devm_iio_channel_get(dev
, "bat-temp");
1175 if (IS_ERR(data
->channel
)) {
1176 dev_err(dev
, "failed to get IIO channel\n");
1177 return PTR_ERR(data
->channel
);
1180 data
->charge_chan
= devm_iio_channel_get(dev
, "charge-vol");
1181 if (IS_ERR(data
->charge_chan
)) {
1182 dev_err(dev
, "failed to get charge IIO channel\n");
1183 return PTR_ERR(data
->charge_chan
);
1186 data
->gpiod
= devm_gpiod_get(dev
, "battery-detect", GPIOD_IN
);
1187 if (IS_ERR(data
->gpiod
)) {
1188 data
->gpiod
= devm_gpiod_get(dev
, "bat-detect", GPIOD_IN
);
1189 if (IS_ERR(data
->gpiod
)) {
1190 dev_err(dev
, "failed to get battery detection GPIO\n");
1191 return PTR_ERR(data
->gpiod
);
1193 dev_warn(dev
, "bat-detect is deprecated, please use battery-detect\n");
1196 ret
= gpiod_get_value_cansleep(data
->gpiod
);
1198 dev_err(dev
, "failed to get gpio state\n");
1202 data
->bat_present
= !!ret
;
1203 mutex_init(&data
->lock
);
1205 platform_set_drvdata(pdev
, data
);
1207 fgu_cfg
.drv_data
= data
;
1208 fgu_cfg
.of_node
= np
;
1209 data
->battery
= devm_power_supply_register(dev
, &sc27xx_fgu_desc
,
1211 if (IS_ERR(data
->battery
)) {
1212 dev_err(dev
, "failed to register power supply\n");
1213 return PTR_ERR(data
->battery
);
1216 ret
= sc27xx_fgu_hw_init(data
);
1218 dev_err(dev
, "failed to initialize fgu hardware\n");
1222 ret
= devm_add_action_or_reset(dev
, sc27xx_fgu_disable
, data
);
1224 dev_err(dev
, "failed to add fgu disable action\n");
1228 irq
= platform_get_irq(pdev
, 0);
1232 ret
= devm_request_threaded_irq(data
->dev
, irq
, NULL
,
1233 sc27xx_fgu_interrupt
,
1234 IRQF_NO_SUSPEND
| IRQF_ONESHOT
,
1237 dev_err(data
->dev
, "failed to request fgu IRQ\n");
1241 irq
= gpiod_to_irq(data
->gpiod
);
1243 dev_err(dev
, "failed to translate GPIO to IRQ\n");
1247 ret
= devm_request_threaded_irq(dev
, irq
, NULL
,
1248 sc27xx_fgu_bat_detection
,
1249 IRQF_ONESHOT
| IRQF_TRIGGER_RISING
|
1250 IRQF_TRIGGER_FALLING
,
1253 dev_err(dev
, "failed to request IRQ\n");
1260 #ifdef CONFIG_PM_SLEEP
1261 static int sc27xx_fgu_resume(struct device
*dev
)
1263 struct sc27xx_fgu_data
*data
= dev_get_drvdata(dev
);
1266 ret
= regmap_update_bits(data
->regmap
, data
->base
+ SC27XX_FGU_INT_EN
,
1267 SC27XX_FGU_LOW_OVERLOAD_INT
|
1268 SC27XX_FGU_CLBCNT_DELTA_INT
, 0);
1270 dev_err(data
->dev
, "failed to disable fgu interrupts\n");
1277 static int sc27xx_fgu_suspend(struct device
*dev
)
1279 struct sc27xx_fgu_data
*data
= dev_get_drvdata(dev
);
1280 int ret
, status
, ocv
;
1282 ret
= sc27xx_fgu_get_status(data
, &status
);
1287 * If we are charging, then no need to enable the FGU interrupts to
1288 * adjust the battery capacity.
1290 if (status
!= POWER_SUPPLY_STATUS_NOT_CHARGING
&&
1291 status
!= POWER_SUPPLY_STATUS_DISCHARGING
)
1294 ret
= regmap_update_bits(data
->regmap
, data
->base
+ SC27XX_FGU_INT_EN
,
1295 SC27XX_FGU_LOW_OVERLOAD_INT
,
1296 SC27XX_FGU_LOW_OVERLOAD_INT
);
1298 dev_err(data
->dev
, "failed to enable low voltage interrupt\n");
1302 ret
= sc27xx_fgu_get_vbat_ocv(data
, &ocv
);
1307 * If current OCV is less than the minimum voltage, we should enable the
1308 * coulomb counter threshold interrupt to notify events to adjust the
1311 if (ocv
< data
->min_volt
) {
1312 ret
= regmap_update_bits(data
->regmap
,
1313 data
->base
+ SC27XX_FGU_INT_EN
,
1314 SC27XX_FGU_CLBCNT_DELTA_INT
,
1315 SC27XX_FGU_CLBCNT_DELTA_INT
);
1318 "failed to enable coulomb threshold int\n");
1326 regmap_update_bits(data
->regmap
, data
->base
+ SC27XX_FGU_INT_EN
,
1327 SC27XX_FGU_LOW_OVERLOAD_INT
, 0);
1332 static const struct dev_pm_ops sc27xx_fgu_pm_ops
= {
1333 SET_SYSTEM_SLEEP_PM_OPS(sc27xx_fgu_suspend
, sc27xx_fgu_resume
)
1336 static const struct of_device_id sc27xx_fgu_of_match
[] = {
1337 { .compatible
= "sprd,sc2731-fgu", },
1340 MODULE_DEVICE_TABLE(of
, sc27xx_fgu_of_match
);
1342 static struct platform_driver sc27xx_fgu_driver
= {
1343 .probe
= sc27xx_fgu_probe
,
1345 .name
= "sc27xx-fgu",
1346 .of_match_table
= sc27xx_fgu_of_match
,
1347 .pm
= &sc27xx_fgu_pm_ops
,
1351 module_platform_driver(sc27xx_fgu_driver
);
1353 MODULE_DESCRIPTION("Spreadtrum SC27XX PMICs Fual Gauge Unit Driver");
1354 MODULE_LICENSE("GPL v2");