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 void sc27xx_fgu_external_power_changed(struct power_supply
*psy
)
738 struct sc27xx_fgu_data
*data
= power_supply_get_drvdata(psy
);
740 power_supply_changed(data
->battery
);
743 static int sc27xx_fgu_property_is_writeable(struct power_supply
*psy
,
744 enum power_supply_property psp
)
746 return psp
== POWER_SUPPLY_PROP_CAPACITY
||
747 psp
== POWER_SUPPLY_PROP_CALIBRATE
||
748 psp
== POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN
;
751 static enum power_supply_property sc27xx_fgu_props
[] = {
752 POWER_SUPPLY_PROP_STATUS
,
753 POWER_SUPPLY_PROP_HEALTH
,
754 POWER_SUPPLY_PROP_PRESENT
,
755 POWER_SUPPLY_PROP_TEMP
,
756 POWER_SUPPLY_PROP_TECHNOLOGY
,
757 POWER_SUPPLY_PROP_CAPACITY
,
758 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
759 POWER_SUPPLY_PROP_VOLTAGE_OCV
,
760 POWER_SUPPLY_PROP_VOLTAGE_AVG
,
761 POWER_SUPPLY_PROP_VOLTAGE_BOOT
,
762 POWER_SUPPLY_PROP_CURRENT_NOW
,
763 POWER_SUPPLY_PROP_CURRENT_AVG
,
764 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE
,
765 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN
,
766 POWER_SUPPLY_PROP_CALIBRATE
,
767 POWER_SUPPLY_PROP_CHARGE_NOW
770 static const struct power_supply_desc sc27xx_fgu_desc
= {
771 .name
= "sc27xx-fgu",
772 .type
= POWER_SUPPLY_TYPE_BATTERY
,
773 .properties
= sc27xx_fgu_props
,
774 .num_properties
= ARRAY_SIZE(sc27xx_fgu_props
),
775 .get_property
= sc27xx_fgu_get_property
,
776 .set_property
= sc27xx_fgu_set_property
,
777 .external_power_changed
= sc27xx_fgu_external_power_changed
,
778 .property_is_writeable
= sc27xx_fgu_property_is_writeable
,
782 static void sc27xx_fgu_adjust_cap(struct sc27xx_fgu_data
*data
, int cap
)
786 data
->init_cap
= cap
;
787 ret
= sc27xx_fgu_get_clbcnt(data
, &data
->init_clbcnt
);
789 dev_err(data
->dev
, "failed to get init coulomb counter\n");
792 static void sc27xx_fgu_capacity_calibration(struct sc27xx_fgu_data
*data
,
793 int cap
, bool int_mode
)
795 int ret
, ocv
, chg_sts
, adc
;
797 ret
= sc27xx_fgu_get_vbat_ocv(data
, &ocv
);
799 dev_err(data
->dev
, "get battery ocv error.\n");
803 ret
= sc27xx_fgu_get_status(data
, &chg_sts
);
805 dev_err(data
->dev
, "get charger status error.\n");
810 * If we are in charging mode, then we do not need to calibrate the
813 if (chg_sts
== POWER_SUPPLY_STATUS_CHARGING
)
816 if ((ocv
> data
->cap_table
[0].ocv
&& cap
< 100) || cap
> 100) {
818 * If current OCV value is larger than the max OCV value in
819 * OCV table, or the current capacity is larger than 100,
820 * we should force the inititial capacity to 100.
822 sc27xx_fgu_adjust_cap(data
, 100);
823 } else if (ocv
<= data
->cap_table
[data
->table_len
- 1].ocv
) {
825 * If current OCV value is leass than the minimum OCV value in
826 * OCV table, we should force the inititial capacity to 0.
828 sc27xx_fgu_adjust_cap(data
, 0);
829 } else if ((ocv
> data
->cap_table
[data
->table_len
- 1].ocv
&& cap
<= 0) ||
830 (ocv
> data
->min_volt
&& cap
<= data
->alarm_cap
)) {
832 * If current OCV value is not matchable with current capacity,
833 * we should re-calculate current capacity by looking up the
836 int cur_cap
= power_supply_ocv2cap_simple(data
->cap_table
,
837 data
->table_len
, ocv
);
839 sc27xx_fgu_adjust_cap(data
, cur_cap
);
840 } else if (ocv
<= data
->min_volt
) {
842 * If current OCV value is less than the low alarm voltage, but
843 * current capacity is larger than the alarm capacity, we should
844 * adjust the inititial capacity to alarm capacity.
846 if (cap
> data
->alarm_cap
) {
847 sc27xx_fgu_adjust_cap(data
, data
->alarm_cap
);
852 * If current capacity is equal with 0 or less than 0
853 * (some error occurs), we should adjust inititial
854 * capacity to the capacity corresponding to current OCV
857 cur_cap
= power_supply_ocv2cap_simple(data
->cap_table
,
860 sc27xx_fgu_adjust_cap(data
, cur_cap
);
867 * After adjusting the battery capacity, we should set the
868 * lowest alarm voltage instead.
870 data
->min_volt
= data
->cap_table
[data
->table_len
- 1].ocv
;
871 data
->alarm_cap
= power_supply_ocv2cap_simple(data
->cap_table
,
875 adc
= sc27xx_fgu_voltage_to_adc(data
, data
->min_volt
/ 1000);
876 regmap_update_bits(data
->regmap
,
877 data
->base
+ SC27XX_FGU_LOW_OVERLOAD
,
878 SC27XX_FGU_LOW_OVERLOAD_MASK
, adc
);
882 static irqreturn_t
sc27xx_fgu_interrupt(int irq
, void *dev_id
)
884 struct sc27xx_fgu_data
*data
= dev_id
;
888 mutex_lock(&data
->lock
);
890 ret
= regmap_read(data
->regmap
, data
->base
+ SC27XX_FGU_INT_STS
,
895 ret
= regmap_update_bits(data
->regmap
, data
->base
+ SC27XX_FGU_INT_CLR
,
901 * When low overload voltage interrupt happens, we should calibrate the
902 * battery capacity in lower voltage stage.
904 if (!(status
& SC27XX_FGU_LOW_OVERLOAD_INT
))
907 ret
= sc27xx_fgu_get_capacity(data
, &cap
);
911 sc27xx_fgu_capacity_calibration(data
, cap
, true);
914 mutex_unlock(&data
->lock
);
916 power_supply_changed(data
->battery
);
920 static irqreturn_t
sc27xx_fgu_bat_detection(int irq
, void *dev_id
)
922 struct sc27xx_fgu_data
*data
= dev_id
;
925 mutex_lock(&data
->lock
);
927 state
= gpiod_get_value_cansleep(data
->gpiod
);
929 dev_err(data
->dev
, "failed to get gpio state\n");
930 mutex_unlock(&data
->lock
);
931 return IRQ_RETVAL(state
);
934 data
->bat_present
= !!state
;
936 mutex_unlock(&data
->lock
);
938 power_supply_changed(data
->battery
);
942 static void sc27xx_fgu_disable(void *_data
)
944 struct sc27xx_fgu_data
*data
= _data
;
946 regmap_update_bits(data
->regmap
, SC27XX_CLK_EN0
, SC27XX_FGU_RTC_EN
, 0);
947 regmap_update_bits(data
->regmap
, SC27XX_MODULE_EN0
, SC27XX_FGU_EN
, 0);
950 static int sc27xx_fgu_cap_to_clbcnt(struct sc27xx_fgu_data
*data
, int capacity
)
953 * Get current capacity (mAh) = battery total capacity (mAh) *
954 * current capacity percent (capacity / 100).
956 int cur_cap
= DIV_ROUND_CLOSEST(data
->total_cap
* capacity
, 100);
959 * Convert current capacity (mAh) to coulomb counter according to the
960 * formula: 1 mAh =3.6 coulomb.
962 return DIV_ROUND_CLOSEST(cur_cap
* 36 * data
->cur_1000ma_adc
* SC27XX_FGU_SAMPLE_HZ
, 10);
965 static int sc27xx_fgu_calibration(struct sc27xx_fgu_data
*data
)
967 struct nvmem_cell
*cell
;
968 int calib_data
, cal_4200mv
;
972 cell
= nvmem_cell_get(data
->dev
, "fgu_calib");
974 return PTR_ERR(cell
);
976 buf
= nvmem_cell_read(cell
, &len
);
977 nvmem_cell_put(cell
);
982 memcpy(&calib_data
, buf
, min(len
, sizeof(u32
)));
985 * Get the ADC value corresponding to 4200 mV from eFuse controller
986 * according to below formula. Then convert to ADC values corresponding
987 * to 1000 mV and 1000 mA.
989 cal_4200mv
= (calib_data
& 0x1ff) + 6963 - 4096 - 256;
990 data
->vol_1000mv_adc
= DIV_ROUND_CLOSEST(cal_4200mv
* 10, 42);
991 data
->cur_1000ma_adc
=
992 DIV_ROUND_CLOSEST(data
->vol_1000mv_adc
* 4 * data
->calib_resist
,
993 SC27XX_FGU_IDEAL_RESISTANCE
);
999 static int sc27xx_fgu_hw_init(struct sc27xx_fgu_data
*data
)
1001 struct power_supply_battery_info info
= { };
1002 struct power_supply_battery_ocv_table
*table
;
1003 int ret
, delta_clbcnt
, alarm_adc
;
1005 ret
= power_supply_get_battery_info(data
->battery
, &info
);
1007 dev_err(data
->dev
, "failed to get battery information\n");
1011 data
->total_cap
= info
.charge_full_design_uah
/ 1000;
1012 data
->max_volt
= info
.constant_charge_voltage_max_uv
/ 1000;
1013 data
->internal_resist
= info
.factory_internal_resistance_uohm
/ 1000;
1014 data
->min_volt
= info
.voltage_min_design_uv
;
1017 * For SC27XX fuel gauge device, we only use one ocv-capacity
1018 * table in normal temperature 20 Celsius.
1020 table
= power_supply_find_ocv2cap_table(&info
, 20, &data
->table_len
);
1024 data
->cap_table
= devm_kmemdup(data
->dev
, table
,
1025 data
->table_len
* sizeof(*table
),
1027 if (!data
->cap_table
) {
1028 power_supply_put_battery_info(data
->battery
, &info
);
1032 data
->alarm_cap
= power_supply_ocv2cap_simple(data
->cap_table
,
1035 if (!data
->alarm_cap
)
1036 data
->alarm_cap
+= 1;
1038 data
->resist_table_len
= info
.resist_table_size
;
1039 if (data
->resist_table_len
> 0) {
1040 data
->resist_table
= devm_kmemdup(data
->dev
, info
.resist_table
,
1041 data
->resist_table_len
*
1042 sizeof(struct power_supply_resistance_temp_table
),
1044 if (!data
->resist_table
) {
1045 power_supply_put_battery_info(data
->battery
, &info
);
1050 power_supply_put_battery_info(data
->battery
, &info
);
1052 ret
= sc27xx_fgu_calibration(data
);
1056 /* Enable the FGU module */
1057 ret
= regmap_update_bits(data
->regmap
, SC27XX_MODULE_EN0
,
1058 SC27XX_FGU_EN
, SC27XX_FGU_EN
);
1060 dev_err(data
->dev
, "failed to enable fgu\n");
1064 /* Enable the FGU RTC clock to make it work */
1065 ret
= regmap_update_bits(data
->regmap
, SC27XX_CLK_EN0
,
1066 SC27XX_FGU_RTC_EN
, SC27XX_FGU_RTC_EN
);
1068 dev_err(data
->dev
, "failed to enable fgu RTC clock\n");
1072 ret
= regmap_update_bits(data
->regmap
, data
->base
+ SC27XX_FGU_INT_CLR
,
1073 SC27XX_FGU_INT_MASK
, SC27XX_FGU_INT_MASK
);
1075 dev_err(data
->dev
, "failed to clear interrupt status\n");
1080 * Set the voltage low overload threshold, which means when the battery
1081 * voltage is lower than this threshold, the controller will generate
1082 * one interrupt to notify.
1084 alarm_adc
= sc27xx_fgu_voltage_to_adc(data
, data
->min_volt
/ 1000);
1085 ret
= regmap_update_bits(data
->regmap
, data
->base
+ SC27XX_FGU_LOW_OVERLOAD
,
1086 SC27XX_FGU_LOW_OVERLOAD_MASK
, alarm_adc
);
1088 dev_err(data
->dev
, "failed to set fgu low overload\n");
1093 * Set the coulomb counter delta threshold, that means when the coulomb
1094 * counter change is multiples of the delta threshold, the controller
1095 * will generate one interrupt to notify the users to update the battery
1096 * capacity. Now we set the delta threshold as a counter value of 1%
1099 delta_clbcnt
= sc27xx_fgu_cap_to_clbcnt(data
, 1);
1101 ret
= regmap_update_bits(data
->regmap
, data
->base
+ SC27XX_FGU_CLBCNT_DELTL
,
1102 SC27XX_FGU_CLBCNT_MASK
, delta_clbcnt
);
1104 dev_err(data
->dev
, "failed to set low delta coulomb counter\n");
1108 ret
= regmap_update_bits(data
->regmap
, data
->base
+ SC27XX_FGU_CLBCNT_DELTH
,
1109 SC27XX_FGU_CLBCNT_MASK
,
1110 delta_clbcnt
>> SC27XX_FGU_CLBCNT_SHIFT
);
1112 dev_err(data
->dev
, "failed to set high delta coulomb counter\n");
1117 * Get the boot battery capacity when system powers on, which is used to
1118 * initialize the coulomb counter. After that, we can read the coulomb
1119 * counter to measure the battery capacity.
1121 ret
= sc27xx_fgu_get_boot_capacity(data
, &data
->init_cap
);
1123 dev_err(data
->dev
, "failed to get boot capacity\n");
1128 * Convert battery capacity to the corresponding initial coulomb counter
1129 * and set into coulomb counter registers.
1131 data
->init_clbcnt
= sc27xx_fgu_cap_to_clbcnt(data
, data
->init_cap
);
1132 ret
= sc27xx_fgu_set_clbcnt(data
, data
->init_clbcnt
);
1134 dev_err(data
->dev
, "failed to initialize coulomb counter\n");
1141 regmap_update_bits(data
->regmap
, SC27XX_CLK_EN0
, SC27XX_FGU_RTC_EN
, 0);
1143 regmap_update_bits(data
->regmap
, SC27XX_MODULE_EN0
, SC27XX_FGU_EN
, 0);
1148 static int sc27xx_fgu_probe(struct platform_device
*pdev
)
1150 struct device
*dev
= &pdev
->dev
;
1151 struct device_node
*np
= dev
->of_node
;
1152 struct power_supply_config fgu_cfg
= { };
1153 struct sc27xx_fgu_data
*data
;
1156 data
= devm_kzalloc(dev
, sizeof(*data
), GFP_KERNEL
);
1160 data
->regmap
= dev_get_regmap(dev
->parent
, NULL
);
1161 if (!data
->regmap
) {
1162 dev_err(dev
, "failed to get regmap\n");
1166 ret
= device_property_read_u32(dev
, "reg", &data
->base
);
1168 dev_err(dev
, "failed to get fgu address\n");
1172 ret
= device_property_read_u32(&pdev
->dev
,
1173 "sprd,calib-resistance-micro-ohms",
1174 &data
->calib_resist
);
1177 "failed to get fgu calibration resistance\n");
1181 data
->channel
= devm_iio_channel_get(dev
, "bat-temp");
1182 if (IS_ERR(data
->channel
)) {
1183 dev_err(dev
, "failed to get IIO channel\n");
1184 return PTR_ERR(data
->channel
);
1187 data
->charge_chan
= devm_iio_channel_get(dev
, "charge-vol");
1188 if (IS_ERR(data
->charge_chan
)) {
1189 dev_err(dev
, "failed to get charge IIO channel\n");
1190 return PTR_ERR(data
->charge_chan
);
1193 data
->gpiod
= devm_gpiod_get(dev
, "bat-detect", GPIOD_IN
);
1194 if (IS_ERR(data
->gpiod
)) {
1195 dev_err(dev
, "failed to get battery detection GPIO\n");
1196 return PTR_ERR(data
->gpiod
);
1199 ret
= gpiod_get_value_cansleep(data
->gpiod
);
1201 dev_err(dev
, "failed to get gpio state\n");
1205 data
->bat_present
= !!ret
;
1206 mutex_init(&data
->lock
);
1208 platform_set_drvdata(pdev
, data
);
1210 fgu_cfg
.drv_data
= data
;
1211 fgu_cfg
.of_node
= np
;
1212 data
->battery
= devm_power_supply_register(dev
, &sc27xx_fgu_desc
,
1214 if (IS_ERR(data
->battery
)) {
1215 dev_err(dev
, "failed to register power supply\n");
1216 return PTR_ERR(data
->battery
);
1219 ret
= sc27xx_fgu_hw_init(data
);
1221 dev_err(dev
, "failed to initialize fgu hardware\n");
1225 ret
= devm_add_action_or_reset(dev
, sc27xx_fgu_disable
, data
);
1227 dev_err(dev
, "failed to add fgu disable action\n");
1231 irq
= platform_get_irq(pdev
, 0);
1233 dev_err(dev
, "no irq resource specified\n");
1237 ret
= devm_request_threaded_irq(data
->dev
, irq
, NULL
,
1238 sc27xx_fgu_interrupt
,
1239 IRQF_NO_SUSPEND
| IRQF_ONESHOT
,
1242 dev_err(data
->dev
, "failed to request fgu IRQ\n");
1246 irq
= gpiod_to_irq(data
->gpiod
);
1248 dev_err(dev
, "failed to translate GPIO to IRQ\n");
1252 ret
= devm_request_threaded_irq(dev
, irq
, NULL
,
1253 sc27xx_fgu_bat_detection
,
1254 IRQF_ONESHOT
| IRQF_TRIGGER_RISING
|
1255 IRQF_TRIGGER_FALLING
,
1258 dev_err(dev
, "failed to request IRQ\n");
1265 #ifdef CONFIG_PM_SLEEP
1266 static int sc27xx_fgu_resume(struct device
*dev
)
1268 struct sc27xx_fgu_data
*data
= dev_get_drvdata(dev
);
1271 ret
= regmap_update_bits(data
->regmap
, data
->base
+ SC27XX_FGU_INT_EN
,
1272 SC27XX_FGU_LOW_OVERLOAD_INT
|
1273 SC27XX_FGU_CLBCNT_DELTA_INT
, 0);
1275 dev_err(data
->dev
, "failed to disable fgu interrupts\n");
1282 static int sc27xx_fgu_suspend(struct device
*dev
)
1284 struct sc27xx_fgu_data
*data
= dev_get_drvdata(dev
);
1285 int ret
, status
, ocv
;
1287 ret
= sc27xx_fgu_get_status(data
, &status
);
1292 * If we are charging, then no need to enable the FGU interrupts to
1293 * adjust the battery capacity.
1295 if (status
!= POWER_SUPPLY_STATUS_NOT_CHARGING
&&
1296 status
!= POWER_SUPPLY_STATUS_DISCHARGING
)
1299 ret
= regmap_update_bits(data
->regmap
, data
->base
+ SC27XX_FGU_INT_EN
,
1300 SC27XX_FGU_LOW_OVERLOAD_INT
,
1301 SC27XX_FGU_LOW_OVERLOAD_INT
);
1303 dev_err(data
->dev
, "failed to enable low voltage interrupt\n");
1307 ret
= sc27xx_fgu_get_vbat_ocv(data
, &ocv
);
1312 * If current OCV is less than the minimum voltage, we should enable the
1313 * coulomb counter threshold interrupt to notify events to adjust the
1316 if (ocv
< data
->min_volt
) {
1317 ret
= regmap_update_bits(data
->regmap
,
1318 data
->base
+ SC27XX_FGU_INT_EN
,
1319 SC27XX_FGU_CLBCNT_DELTA_INT
,
1320 SC27XX_FGU_CLBCNT_DELTA_INT
);
1323 "failed to enable coulomb threshold int\n");
1331 regmap_update_bits(data
->regmap
, data
->base
+ SC27XX_FGU_INT_EN
,
1332 SC27XX_FGU_LOW_OVERLOAD_INT
, 0);
1337 static const struct dev_pm_ops sc27xx_fgu_pm_ops
= {
1338 SET_SYSTEM_SLEEP_PM_OPS(sc27xx_fgu_suspend
, sc27xx_fgu_resume
)
1341 static const struct of_device_id sc27xx_fgu_of_match
[] = {
1342 { .compatible
= "sprd,sc2731-fgu", },
1346 static struct platform_driver sc27xx_fgu_driver
= {
1347 .probe
= sc27xx_fgu_probe
,
1349 .name
= "sc27xx-fgu",
1350 .of_match_table
= sc27xx_fgu_of_match
,
1351 .pm
= &sc27xx_fgu_pm_ops
,
1355 module_platform_driver(sc27xx_fgu_driver
);
1357 MODULE_DESCRIPTION("Spreadtrum SC27XX PMICs Fual Gauge Unit Driver");
1358 MODULE_LICENSE("GPL v2");