1 /* Copyright (c) 2016-2017, The Linux Foundation. All rights reserved.
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
13 #define pr_fmt(fmt) "FG: %s: " fmt, __func__
15 #include <linux/ktime.h>
17 #include <linux/of_irq.h>
18 #include <linux/of_platform.h>
19 #include <linux/of_batterydata.h>
20 #include <linux/platform_device.h>
21 #include <linux/iio/consumer.h>
22 #include <linux/qpnp/qpnp-revid.h>
26 #define FG_GEN3_DEV_NAME "qcom,fg-gen3"
28 #define PERPH_SUBTYPE_REG 0x05
29 #define FG_BATT_SOC_PMI8998 0x10
30 #define FG_BATT_INFO_PMI8998 0x11
31 #define FG_MEM_INFO_PMI8998 0x0D
33 /* SRAM address and offset in ascending order */
34 #define ESR_PULSE_THRESH_WORD 2
35 #define ESR_PULSE_THRESH_OFFSET 3
36 #define SLOPE_LIMIT_WORD 3
37 #define SLOPE_LIMIT_OFFSET 0
38 #define CUTOFF_VOLT_WORD 5
39 #define CUTOFF_VOLT_OFFSET 0
40 #define SYS_TERM_CURR_WORD 6
41 #define SYS_TERM_CURR_OFFSET 0
42 #define VBATT_FULL_WORD 7
43 #define VBATT_FULL_OFFSET 0
44 #define ESR_FILTER_WORD 8
45 #define ESR_UPD_TIGHT_OFFSET 0
46 #define ESR_UPD_BROAD_OFFSET 1
47 #define ESR_UPD_TIGHT_LOW_TEMP_OFFSET 2
48 #define ESR_UPD_BROAD_LOW_TEMP_OFFSET 3
49 #define KI_COEFF_MED_DISCHG_WORD 9
50 #define TIMEBASE_OFFSET 1
51 #define KI_COEFF_MED_DISCHG_OFFSET 3
52 #define KI_COEFF_HI_DISCHG_WORD 10
53 #define KI_COEFF_HI_DISCHG_OFFSET 0
54 #define KI_COEFF_LOW_DISCHG_WORD 10
55 #define KI_COEFF_LOW_DISCHG_OFFSET 2
56 #define KI_COEFF_FULL_SOC_WORD 12
57 #define KI_COEFF_FULL_SOC_OFFSET 2
58 #define DELTA_MSOC_THR_WORD 12
59 #define DELTA_MSOC_THR_OFFSET 3
60 #define DELTA_BSOC_THR_WORD 13
61 #define DELTA_BSOC_THR_OFFSET 2
62 #define RECHARGE_SOC_THR_WORD 14
63 #define RECHARGE_SOC_THR_OFFSET 0
64 #define CHG_TERM_CURR_WORD 14
65 #define CHG_TERM_CURR_OFFSET 1
66 #define EMPTY_VOLT_WORD 15
67 #define EMPTY_VOLT_OFFSET 0
68 #define VBATT_LOW_WORD 15
69 #define VBATT_LOW_OFFSET 1
70 #define ESR_TIMER_DISCHG_MAX_WORD 17
71 #define ESR_TIMER_DISCHG_MAX_OFFSET 0
72 #define ESR_TIMER_DISCHG_INIT_WORD 17
73 #define ESR_TIMER_DISCHG_INIT_OFFSET 2
74 #define ESR_TIMER_CHG_MAX_WORD 18
75 #define ESR_TIMER_CHG_MAX_OFFSET 0
76 #define ESR_TIMER_CHG_INIT_WORD 18
77 #define ESR_TIMER_CHG_INIT_OFFSET 2
78 #define ESR_EXTRACTION_ENABLE_WORD 19
79 #define ESR_EXTRACTION_ENABLE_OFFSET 0
80 #define PROFILE_LOAD_WORD 24
81 #define PROFILE_LOAD_OFFSET 0
82 #define ESR_RSLOW_DISCHG_WORD 34
83 #define ESR_RSLOW_DISCHG_OFFSET 0
84 #define ESR_RSLOW_CHG_WORD 51
85 #define ESR_RSLOW_CHG_OFFSET 0
86 #define NOM_CAP_WORD 58
87 #define NOM_CAP_OFFSET 0
88 #define ACT_BATT_CAP_BKUP_WORD 74
89 #define ACT_BATT_CAP_BKUP_OFFSET 0
90 #define CYCLE_COUNT_WORD 75
91 #define CYCLE_COUNT_OFFSET 0
92 #define PROFILE_INTEGRITY_WORD 79
93 #define SW_CONFIG_OFFSET 0
94 #define PROFILE_INTEGRITY_OFFSET 3
95 #define BATT_SOC_WORD 91
96 #define BATT_SOC_OFFSET 0
97 #define FULL_SOC_WORD 93
98 #define FULL_SOC_OFFSET 2
99 #define MONOTONIC_SOC_WORD 94
100 #define MONOTONIC_SOC_OFFSET 2
101 #define CC_SOC_WORD 95
102 #define CC_SOC_OFFSET 0
103 #define CC_SOC_SW_WORD 96
104 #define CC_SOC_SW_OFFSET 0
105 #define VOLTAGE_PRED_WORD 97
106 #define VOLTAGE_PRED_OFFSET 0
111 #define RSLOW_WORD 101
112 #define RSLOW_OFFSET 0
113 #define ACT_BATT_CAP_WORD 117
114 #define ACT_BATT_CAP_OFFSET 0
115 #define LAST_BATT_SOC_WORD 119
116 #define LAST_BATT_SOC_OFFSET 0
117 #define LAST_MONOTONIC_SOC_WORD 119
118 #define LAST_MONOTONIC_SOC_OFFSET 2
119 #define ALG_FLAGS_WORD 120
120 #define ALG_FLAGS_OFFSET 1
122 /* v2 SRAM address and offset in ascending order */
123 #define KI_COEFF_LOW_DISCHG_v2_WORD 9
124 #define KI_COEFF_LOW_DISCHG_v2_OFFSET 3
125 #define KI_COEFF_MED_DISCHG_v2_WORD 10
126 #define KI_COEFF_MED_DISCHG_v2_OFFSET 0
127 #define KI_COEFF_HI_DISCHG_v2_WORD 10
128 #define KI_COEFF_HI_DISCHG_v2_OFFSET 1
129 #define DELTA_BSOC_THR_v2_WORD 12
130 #define DELTA_BSOC_THR_v2_OFFSET 3
131 #define DELTA_MSOC_THR_v2_WORD 13
132 #define DELTA_MSOC_THR_v2_OFFSET 0
133 #define RECHARGE_SOC_THR_v2_WORD 14
134 #define RECHARGE_SOC_THR_v2_OFFSET 1
135 #define CHG_TERM_CURR_v2_WORD 15
136 #define CHG_TERM_BASE_CURR_v2_OFFSET 0
137 #define CHG_TERM_CURR_v2_OFFSET 1
138 #define EMPTY_VOLT_v2_WORD 15
139 #define EMPTY_VOLT_v2_OFFSET 3
140 #define VBATT_LOW_v2_WORD 16
141 #define VBATT_LOW_v2_OFFSET 0
142 #define RECHARGE_VBATT_THR_v2_WORD 16
143 #define RECHARGE_VBATT_THR_v2_OFFSET 1
144 #define FLOAT_VOLT_v2_WORD 16
145 #define FLOAT_VOLT_v2_OFFSET 2
147 static int fg_decode_voltage_15b(struct fg_sram_param *sp,
148 enum fg_sram_param_id id, int val);
149 static int fg_decode_value_16b(struct fg_sram_param *sp,
150 enum fg_sram_param_id id, int val);
151 static int fg_decode_default(struct fg_sram_param *sp,
152 enum fg_sram_param_id id, int val);
153 static int fg_decode_cc_soc(struct fg_sram_param *sp,
154 enum fg_sram_param_id id, int value);
155 static void fg_encode_voltage(struct fg_sram_param *sp,
156 enum fg_sram_param_id id, int val_mv, u8 *buf);
157 static void fg_encode_current(struct fg_sram_param *sp,
158 enum fg_sram_param_id id, int val_ma, u8 *buf);
159 static void fg_encode_default(struct fg_sram_param *sp,
160 enum fg_sram_param_id id, int val, u8 *buf);
162 static struct fg_irq_info fg_irqs[FG_IRQ_MAX];
164 #define PARAM(_id, _addr_word, _addr_byte, _len, _num, _den, _offset, \
166 [FG_SRAM_##_id] = { \
167 .addr_word = _addr_word, \
168 .addr_byte = _addr_byte, \
177 static struct fg_sram_param pmi8998_v1_sram_params[] = {
178 PARAM(BATT_SOC, BATT_SOC_WORD, BATT_SOC_OFFSET, 4, 1, 1, 0, NULL,
180 PARAM(FULL_SOC, FULL_SOC_WORD, FULL_SOC_OFFSET, 2, 1, 1, 0, NULL,
182 PARAM(VOLTAGE_PRED, VOLTAGE_PRED_WORD, VOLTAGE_PRED_OFFSET, 2, 1000,
183 244141, 0, NULL, fg_decode_voltage_15b),
184 PARAM(OCV, OCV_WORD, OCV_OFFSET, 2, 1000, 244141, 0, NULL,
185 fg_decode_voltage_15b),
186 PARAM(ESR, ESR_WORD, ESR_OFFSET, 2, 1000, 244141, 0, fg_encode_default,
187 fg_decode_value_16b),
188 PARAM(RSLOW, RSLOW_WORD, RSLOW_OFFSET, 2, 1000, 244141, 0, NULL,
189 fg_decode_value_16b),
190 PARAM(ALG_FLAGS, ALG_FLAGS_WORD, ALG_FLAGS_OFFSET, 1, 1, 1, 0, NULL,
192 PARAM(CC_SOC, CC_SOC_WORD, CC_SOC_OFFSET, 4, 1, 1, 0, NULL,
194 PARAM(CC_SOC_SW, CC_SOC_SW_WORD, CC_SOC_SW_OFFSET, 4, 1, 1, 0, NULL,
196 PARAM(ACT_BATT_CAP, ACT_BATT_CAP_BKUP_WORD, ACT_BATT_CAP_BKUP_OFFSET, 2,
197 1, 1, 0, NULL, fg_decode_default),
198 /* Entries below here are configurable during initialization */
199 PARAM(CUTOFF_VOLT, CUTOFF_VOLT_WORD, CUTOFF_VOLT_OFFSET, 2, 1000000,
200 244141, 0, fg_encode_voltage, NULL),
201 PARAM(EMPTY_VOLT, EMPTY_VOLT_WORD, EMPTY_VOLT_OFFSET, 1, 100000, 390625,
202 -2500, fg_encode_voltage, NULL),
203 PARAM(VBATT_LOW, VBATT_LOW_WORD, VBATT_LOW_OFFSET, 1, 100000, 390625,
204 -2500, fg_encode_voltage, NULL),
205 PARAM(VBATT_FULL, VBATT_FULL_WORD, VBATT_FULL_OFFSET, 2, 1000,
206 244141, 0, fg_encode_voltage, fg_decode_voltage_15b),
207 PARAM(SYS_TERM_CURR, SYS_TERM_CURR_WORD, SYS_TERM_CURR_OFFSET, 3,
208 1000000, 122070, 0, fg_encode_current, NULL),
209 PARAM(CHG_TERM_CURR, CHG_TERM_CURR_WORD, CHG_TERM_CURR_OFFSET, 1,
210 100000, 390625, 0, fg_encode_current, NULL),
211 PARAM(DELTA_MSOC_THR, DELTA_MSOC_THR_WORD, DELTA_MSOC_THR_OFFSET, 1,
212 2048, 100, 0, fg_encode_default, NULL),
213 PARAM(DELTA_BSOC_THR, DELTA_BSOC_THR_WORD, DELTA_BSOC_THR_OFFSET, 1,
214 2048, 100, 0, fg_encode_default, NULL),
215 PARAM(RECHARGE_SOC_THR, RECHARGE_SOC_THR_WORD, RECHARGE_SOC_THR_OFFSET,
216 1, 256, 100, 0, fg_encode_default, NULL),
217 PARAM(ESR_TIMER_DISCHG_MAX, ESR_TIMER_DISCHG_MAX_WORD,
218 ESR_TIMER_DISCHG_MAX_OFFSET, 2, 1, 1, 0, fg_encode_default,
220 PARAM(ESR_TIMER_DISCHG_INIT, ESR_TIMER_DISCHG_INIT_WORD,
221 ESR_TIMER_DISCHG_INIT_OFFSET, 2, 1, 1, 0, fg_encode_default,
223 PARAM(ESR_TIMER_CHG_MAX, ESR_TIMER_CHG_MAX_WORD,
224 ESR_TIMER_CHG_MAX_OFFSET, 2, 1, 1, 0, fg_encode_default, NULL),
225 PARAM(ESR_TIMER_CHG_INIT, ESR_TIMER_CHG_INIT_WORD,
226 ESR_TIMER_CHG_INIT_OFFSET, 2, 1, 1, 0, fg_encode_default, NULL),
227 PARAM(ESR_PULSE_THRESH, ESR_PULSE_THRESH_WORD, ESR_PULSE_THRESH_OFFSET,
228 1, 100000, 390625, 0, fg_encode_default, NULL),
229 PARAM(KI_COEFF_MED_DISCHG, KI_COEFF_MED_DISCHG_WORD,
230 KI_COEFF_MED_DISCHG_OFFSET, 1, 1000, 244141, 0,
231 fg_encode_default, NULL),
232 PARAM(KI_COEFF_HI_DISCHG, KI_COEFF_HI_DISCHG_WORD,
233 KI_COEFF_HI_DISCHG_OFFSET, 1, 1000, 244141, 0,
234 fg_encode_default, NULL),
235 PARAM(KI_COEFF_FULL_SOC, KI_COEFF_FULL_SOC_WORD,
236 KI_COEFF_FULL_SOC_OFFSET, 1, 1000, 244141, 0,
237 fg_encode_default, NULL),
238 PARAM(ESR_TIGHT_FILTER, ESR_FILTER_WORD, ESR_UPD_TIGHT_OFFSET,
239 1, 512, 1000000, 0, fg_encode_default, NULL),
240 PARAM(ESR_BROAD_FILTER, ESR_FILTER_WORD, ESR_UPD_BROAD_OFFSET,
241 1, 512, 1000000, 0, fg_encode_default, NULL),
242 PARAM(SLOPE_LIMIT, SLOPE_LIMIT_WORD, SLOPE_LIMIT_OFFSET, 1, 8192, 1000,
243 0, fg_encode_default, NULL),
246 static struct fg_sram_param pmi8998_v2_sram_params[] = {
247 PARAM(BATT_SOC, BATT_SOC_WORD, BATT_SOC_OFFSET, 4, 1, 1, 0, NULL,
249 PARAM(FULL_SOC, FULL_SOC_WORD, FULL_SOC_OFFSET, 2, 1, 1, 0, NULL,
251 PARAM(VOLTAGE_PRED, VOLTAGE_PRED_WORD, VOLTAGE_PRED_OFFSET, 2, 1000,
252 244141, 0, NULL, fg_decode_voltage_15b),
253 PARAM(OCV, OCV_WORD, OCV_OFFSET, 2, 1000, 244141, 0, NULL,
254 fg_decode_voltage_15b),
255 PARAM(ESR, ESR_WORD, ESR_OFFSET, 2, 1000, 244141, 0, fg_encode_default,
256 fg_decode_value_16b),
257 PARAM(RSLOW, RSLOW_WORD, RSLOW_OFFSET, 2, 1000, 244141, 0, NULL,
258 fg_decode_value_16b),
259 PARAM(ALG_FLAGS, ALG_FLAGS_WORD, ALG_FLAGS_OFFSET, 1, 1, 1, 0, NULL,
261 PARAM(CC_SOC, CC_SOC_WORD, CC_SOC_OFFSET, 4, 1, 1, 0, NULL,
263 PARAM(CC_SOC_SW, CC_SOC_SW_WORD, CC_SOC_SW_OFFSET, 4, 1, 1, 0, NULL,
265 PARAM(ACT_BATT_CAP, ACT_BATT_CAP_BKUP_WORD, ACT_BATT_CAP_BKUP_OFFSET, 2,
266 1, 1, 0, NULL, fg_decode_default),
267 PARAM(TIMEBASE, KI_COEFF_MED_DISCHG_WORD, TIMEBASE_OFFSET, 2, 1000,
268 61000, 0, fg_encode_default, NULL),
269 /* Entries below here are configurable during initialization */
270 PARAM(CUTOFF_VOLT, CUTOFF_VOLT_WORD, CUTOFF_VOLT_OFFSET, 2, 1000000,
271 244141, 0, fg_encode_voltage, NULL),
272 PARAM(EMPTY_VOLT, EMPTY_VOLT_v2_WORD, EMPTY_VOLT_v2_OFFSET, 1, 1000,
273 15625, -2000, fg_encode_voltage, NULL),
274 PARAM(VBATT_LOW, VBATT_LOW_v2_WORD, VBATT_LOW_v2_OFFSET, 1, 1000,
275 15625, -2000, fg_encode_voltage, NULL),
276 PARAM(FLOAT_VOLT, FLOAT_VOLT_v2_WORD, FLOAT_VOLT_v2_OFFSET, 1, 1000,
277 15625, -2000, fg_encode_voltage, NULL),
278 PARAM(VBATT_FULL, VBATT_FULL_WORD, VBATT_FULL_OFFSET, 2, 1000,
279 244141, 0, fg_encode_voltage, fg_decode_voltage_15b),
280 PARAM(SYS_TERM_CURR, SYS_TERM_CURR_WORD, SYS_TERM_CURR_OFFSET, 3,
281 1000000, 122070, 0, fg_encode_current, NULL),
282 PARAM(CHG_TERM_CURR, CHG_TERM_CURR_v2_WORD, CHG_TERM_CURR_v2_OFFSET, 1,
283 100000, 390625, 0, fg_encode_current, NULL),
284 PARAM(CHG_TERM_BASE_CURR, CHG_TERM_CURR_v2_WORD,
285 CHG_TERM_BASE_CURR_v2_OFFSET, 1, 1024, 1000, 0,
286 fg_encode_current, NULL),
287 PARAM(DELTA_MSOC_THR, DELTA_MSOC_THR_v2_WORD, DELTA_MSOC_THR_v2_OFFSET,
288 1, 2048, 100, 0, fg_encode_default, NULL),
289 PARAM(DELTA_BSOC_THR, DELTA_BSOC_THR_v2_WORD, DELTA_BSOC_THR_v2_OFFSET,
290 1, 2048, 100, 0, fg_encode_default, NULL),
291 PARAM(RECHARGE_SOC_THR, RECHARGE_SOC_THR_v2_WORD,
292 RECHARGE_SOC_THR_v2_OFFSET, 1, 256, 100, 0, fg_encode_default,
294 PARAM(RECHARGE_VBATT_THR, RECHARGE_VBATT_THR_v2_WORD,
295 RECHARGE_VBATT_THR_v2_OFFSET, 1, 1000, 15625, -2000,
296 fg_encode_voltage, NULL),
297 PARAM(ESR_TIMER_DISCHG_MAX, ESR_TIMER_DISCHG_MAX_WORD,
298 ESR_TIMER_DISCHG_MAX_OFFSET, 2, 1, 1, 0, fg_encode_default,
300 PARAM(ESR_TIMER_DISCHG_INIT, ESR_TIMER_DISCHG_INIT_WORD,
301 ESR_TIMER_DISCHG_INIT_OFFSET, 2, 1, 1, 0, fg_encode_default,
303 PARAM(ESR_TIMER_CHG_MAX, ESR_TIMER_CHG_MAX_WORD,
304 ESR_TIMER_CHG_MAX_OFFSET, 2, 1, 1, 0, fg_encode_default, NULL),
305 PARAM(ESR_TIMER_CHG_INIT, ESR_TIMER_CHG_INIT_WORD,
306 ESR_TIMER_CHG_INIT_OFFSET, 2, 1, 1, 0, fg_encode_default, NULL),
307 PARAM(ESR_PULSE_THRESH, ESR_PULSE_THRESH_WORD, ESR_PULSE_THRESH_OFFSET,
308 1, 100000, 390625, 0, fg_encode_default, NULL),
309 PARAM(KI_COEFF_MED_DISCHG, KI_COEFF_MED_DISCHG_v2_WORD,
310 KI_COEFF_MED_DISCHG_v2_OFFSET, 1, 1000, 244141, 0,
311 fg_encode_default, NULL),
312 PARAM(KI_COEFF_HI_DISCHG, KI_COEFF_HI_DISCHG_v2_WORD,
313 KI_COEFF_HI_DISCHG_v2_OFFSET, 1, 1000, 244141, 0,
314 fg_encode_default, NULL),
315 PARAM(KI_COEFF_FULL_SOC, KI_COEFF_FULL_SOC_WORD,
316 KI_COEFF_FULL_SOC_OFFSET, 1, 1000, 244141, 0,
317 fg_encode_default, NULL),
318 PARAM(ESR_TIGHT_FILTER, ESR_FILTER_WORD, ESR_UPD_TIGHT_OFFSET,
319 1, 512, 1000000, 0, fg_encode_default, NULL),
320 PARAM(ESR_BROAD_FILTER, ESR_FILTER_WORD, ESR_UPD_BROAD_OFFSET,
321 1, 512, 1000000, 0, fg_encode_default, NULL),
322 PARAM(SLOPE_LIMIT, SLOPE_LIMIT_WORD, SLOPE_LIMIT_OFFSET, 1, 8192, 1000,
323 0, fg_encode_default, NULL),
326 static struct fg_alg_flag pmi8998_v1_alg_flags[] = {
327 [ALG_FLAG_SOC_LT_OTG_MIN] = {
328 .name = "SOC_LT_OTG_MIN",
331 [ALG_FLAG_SOC_LT_RECHARGE] = {
332 .name = "SOC_LT_RECHARGE",
335 [ALG_FLAG_IBATT_LT_ITERM] = {
336 .name = "IBATT_LT_ITERM",
339 [ALG_FLAG_IBATT_GT_HPM] = {
340 .name = "IBATT_GT_HPM",
343 [ALG_FLAG_IBATT_GT_UPM] = {
344 .name = "IBATT_GT_UPM",
347 [ALG_FLAG_VBATT_LT_RECHARGE] = {
348 .name = "VBATT_LT_RECHARGE",
351 [ALG_FLAG_VBATT_GT_VFLOAT] = {
356 static struct fg_alg_flag pmi8998_v2_alg_flags[] = {
357 [ALG_FLAG_SOC_LT_OTG_MIN] = {
358 .name = "SOC_LT_OTG_MIN",
361 [ALG_FLAG_SOC_LT_RECHARGE] = {
362 .name = "SOC_LT_RECHARGE",
365 [ALG_FLAG_IBATT_LT_ITERM] = {
366 .name = "IBATT_LT_ITERM",
369 [ALG_FLAG_IBATT_GT_HPM] = {
370 .name = "IBATT_GT_HPM",
373 [ALG_FLAG_IBATT_GT_UPM] = {
374 .name = "IBATT_GT_UPM",
377 [ALG_FLAG_VBATT_LT_RECHARGE] = {
378 .name = "VBATT_LT_RECHARGE",
381 [ALG_FLAG_VBATT_GT_VFLOAT] = {
382 .name = "VBATT_GT_VFLOAT",
387 static int fg_gen3_debug_mask;
389 debug_mask, fg_gen3_debug_mask, int, S_IRUSR | S_IWUSR
392 static bool fg_profile_dump;
394 profile_dump, fg_profile_dump, bool, S_IRUSR | S_IWUSR
397 static int fg_sram_dump_period_ms = 20000;
399 sram_dump_period_ms, fg_sram_dump_period_ms, int, S_IRUSR | S_IWUSR
402 static int fg_restart;
403 static bool fg_sram_dump;
405 /* All getters HERE */
407 #define VOLTAGE_15BIT_MASK GENMASK(14, 0)
408 static int fg_decode_voltage_15b(struct fg_sram_param *sp,
409 enum fg_sram_param_id id, int value)
411 value &= VOLTAGE_15BIT_MASK;
412 sp[id].value = div_u64((u64)value * sp[id].denmtr, sp[id].numrtr);
413 pr_debug("id: %d raw value: %x decoded value: %x\n", id, value,
418 static int fg_decode_cc_soc(struct fg_sram_param *sp,
419 enum fg_sram_param_id id, int value)
421 sp[id].value = div_s64((s64)value * sp[id].denmtr, sp[id].numrtr);
422 sp[id].value = sign_extend32(sp[id].value, 31);
423 pr_debug("id: %d raw value: %x decoded value: %x\n", id, value,
428 static int fg_decode_value_16b(struct fg_sram_param *sp,
429 enum fg_sram_param_id id, int value)
431 sp[id].value = div_u64((u64)(u16)value * sp[id].denmtr, sp[id].numrtr);
432 pr_debug("id: %d raw value: %x decoded value: %x\n", id, value,
437 static int fg_decode_default(struct fg_sram_param *sp, enum fg_sram_param_id id,
440 sp[id].value = value;
444 static int fg_decode(struct fg_sram_param *sp, enum fg_sram_param_id id,
447 if (!sp[id].decode) {
448 pr_err("No decoding function for parameter %d\n", id);
452 return sp[id].decode(sp, id, value);
455 static void fg_encode_voltage(struct fg_sram_param *sp,
456 enum fg_sram_param_id id, int val_mv, u8 *buf)
461 val_mv += sp[id].offset;
462 temp = (int64_t)div_u64((u64)val_mv * sp[id].numrtr, sp[id].denmtr);
463 pr_debug("temp: %llx id: %d, val_mv: %d, buf: [ ", temp, id, val_mv);
464 for (i = 0; i < sp[id].len; i++) {
465 buf[i] = temp & mask;
467 pr_debug("%x ", buf[i]);
472 static void fg_encode_current(struct fg_sram_param *sp,
473 enum fg_sram_param_id id, int val_ma, u8 *buf)
480 temp = (int64_t)div_s64(current_ma * sp[id].numrtr, sp[id].denmtr);
481 pr_debug("temp: %llx id: %d, val: %d, buf: [ ", temp, id, val_ma);
482 for (i = 0; i < sp[id].len; i++) {
483 buf[i] = temp & mask;
485 pr_debug("%x ", buf[i]);
490 static void fg_encode_default(struct fg_sram_param *sp,
491 enum fg_sram_param_id id, int val, u8 *buf)
496 temp = (int64_t)div_s64((s64)val * sp[id].numrtr, sp[id].denmtr);
497 pr_debug("temp: %llx id: %d, val: %d, buf: [ ", temp, id, val);
498 for (i = 0; i < sp[id].len; i++) {
499 buf[i] = temp & mask;
501 pr_debug("%x ", buf[i]);
506 static void fg_encode(struct fg_sram_param *sp, enum fg_sram_param_id id,
509 if (!sp[id].encode) {
510 pr_err("No encoding function for parameter %d\n", id);
514 sp[id].encode(sp, id, val, buf);
518 * Please make sure *_sram_params table has the entry for the parameter
519 * obtained through this function. In addition to address, offset,
520 * length from where this SRAM parameter is read, a decode function
521 * need to be specified.
523 static int fg_get_sram_prop(struct fg_chip *chip, enum fg_sram_param_id id,
529 if (id < 0 || id > FG_SRAM_MAX || chip->sp[id].len > sizeof(buf))
532 if (chip->battery_missing)
535 rc = fg_sram_read(chip, chip->sp[id].addr_word, chip->sp[id].addr_byte,
536 buf, chip->sp[id].len, FG_IMA_DEFAULT);
538 pr_err("Error reading address 0x%04x[%d] rc=%d\n",
539 chip->sp[id].addr_word, chip->sp[id].addr_byte, rc);
543 for (i = 0, temp = 0; i < chip->sp[id].len; i++)
544 temp |= buf[i] << (8 * i);
546 *val = fg_decode(chip->sp, id, temp);
550 #define CC_SOC_30BIT GENMASK(29, 0)
551 static int fg_get_charge_raw(struct fg_chip *chip, int *val)
555 rc = fg_get_sram_prop(chip, FG_SRAM_CC_SOC, &cc_soc);
557 pr_err("Error in getting CC_SOC, rc=%d\n", rc);
561 *val = div_s64(cc_soc * chip->cl.nom_cap_uah, CC_SOC_30BIT);
565 static int fg_get_charge_counter(struct fg_chip *chip, int *val)
569 rc = fg_get_sram_prop(chip, FG_SRAM_CC_SOC_SW, &cc_soc);
571 pr_err("Error in getting CC_SOC_SW, rc=%d\n", rc);
575 *val = div_s64(cc_soc * chip->cl.learned_cc_uah, CC_SOC_30BIT);
579 static int fg_get_jeita_threshold(struct fg_chip *chip,
580 enum jeita_levels level, int *temp_decidegC)
588 reg = BATT_INFO_JEITA_TOO_COLD(chip);
591 reg = BATT_INFO_JEITA_COLD(chip);
594 reg = BATT_INFO_JEITA_HOT(chip);
597 reg = BATT_INFO_JEITA_TOO_HOT(chip);
603 rc = fg_read(chip, reg, &val, 1);
605 pr_err("Error in reading jeita level %d, rc=%d\n", level, rc);
609 /* Resolution is 0.5C. Base is -30C. */
610 *temp_decidegC = (((5 * val) / 10) - 30) * 10;
614 #define BATT_TEMP_NUMR 1
615 #define BATT_TEMP_DENR 1
616 static int fg_get_battery_temp(struct fg_chip *chip, int *val)
621 rc = fg_read(chip, BATT_INFO_BATT_TEMP_LSB(chip), buf, 2);
623 pr_err("failed to read addr=0x%04x, rc=%d\n",
624 BATT_INFO_BATT_TEMP_LSB(chip), rc);
628 temp = ((buf[1] & BATT_TEMP_MSB_MASK) << 8) |
629 (buf[0] & BATT_TEMP_LSB_MASK);
630 temp = DIV_ROUND_CLOSEST(temp, 4);
632 /* Value is in Kelvin; Convert it to deciDegC */
633 temp = (temp - 273) * 10;
638 static int fg_get_battery_resistance(struct fg_chip *chip, int *val)
640 int rc, esr_uohms, rslow_uohms;
642 rc = fg_get_sram_prop(chip, FG_SRAM_ESR, &esr_uohms);
644 pr_err("failed to get ESR, rc=%d\n", rc);
648 rc = fg_get_sram_prop(chip, FG_SRAM_RSLOW, &rslow_uohms);
650 pr_err("failed to get Rslow, rc=%d\n", rc);
654 *val = esr_uohms + rslow_uohms;
658 #define BATT_CURRENT_NUMR 488281
659 #define BATT_CURRENT_DENR 1000
660 static int fg_get_battery_current(struct fg_chip *chip, int *val)
666 rc = fg_read(chip, BATT_INFO_IBATT_LSB(chip), buf, 2);
668 pr_err("failed to read addr=0x%04x, rc=%d\n",
669 BATT_INFO_IBATT_LSB(chip), rc);
673 if (chip->wa_flags & PMI8998_V1_REV_WA)
674 temp = buf[0] << 8 | buf[1];
676 temp = buf[1] << 8 | buf[0];
678 pr_debug("buf: %x %x temp: %llx\n", buf[0], buf[1], temp);
679 /* Sign bit is bit 15 */
680 temp = twos_compliment_extend(temp, 15);
681 *val = div_s64((s64)temp * BATT_CURRENT_NUMR, BATT_CURRENT_DENR);
685 #define BATT_VOLTAGE_NUMR 122070
686 #define BATT_VOLTAGE_DENR 1000
687 static int fg_get_battery_voltage(struct fg_chip *chip, int *val)
693 rc = fg_read(chip, BATT_INFO_VBATT_LSB(chip), buf, 2);
695 pr_err("failed to read addr=0x%04x, rc=%d\n",
696 BATT_INFO_VBATT_LSB(chip), rc);
700 if (chip->wa_flags & PMI8998_V1_REV_WA)
701 temp = buf[0] << 8 | buf[1];
703 temp = buf[1] << 8 | buf[0];
705 pr_debug("buf: %x %x temp: %x\n", buf[0], buf[1], temp);
706 *val = div_u64((u64)temp * BATT_VOLTAGE_NUMR, BATT_VOLTAGE_DENR);
710 #define MAX_TRIES_SOC 5
711 static int fg_get_msoc_raw(struct fg_chip *chip, int *val)
716 while (tries < MAX_TRIES_SOC) {
717 rc = fg_read(chip, BATT_SOC_FG_MONOTONIC_SOC(chip), cap, 2);
719 pr_err("failed to read addr=0x%04x, rc=%d\n",
720 BATT_SOC_FG_MONOTONIC_SOC(chip), rc);
724 if (cap[0] == cap[1])
730 if (tries == MAX_TRIES_SOC) {
731 pr_err("shadow registers do not match\n");
735 fg_dbg(chip, FG_POWER_SUPPLY, "raw: 0x%02x\n", cap[0]);
740 #define FULL_CAPACITY 100
741 #define FULL_SOC_RAW 255
742 static int fg_get_msoc(struct fg_chip *chip, int *msoc)
746 rc = fg_get_msoc_raw(chip, msoc);
750 *msoc = DIV_ROUND_CLOSEST(*msoc * FULL_CAPACITY, FULL_SOC_RAW);
754 static bool is_batt_empty(struct fg_chip *chip)
757 int rc, vbatt_uv, msoc;
759 rc = fg_read(chip, BATT_SOC_INT_RT_STS(chip), &status, 1);
761 pr_err("failed to read addr=0x%04x, rc=%d\n",
762 BATT_SOC_INT_RT_STS(chip), rc);
766 if (!(status & MSOC_EMPTY_BIT))
769 rc = fg_get_battery_voltage(chip, &vbatt_uv);
771 pr_err("failed to get battery voltage, rc=%d\n", rc);
775 rc = fg_get_msoc(chip, &msoc);
777 pr_warn("batt_soc_rt_sts: %x vbatt: %d uV msoc:%d\n", status,
780 return ((vbatt_uv < chip->dt.cutoff_volt_mv * 1000) ? true : false);
783 static int fg_get_debug_batt_id(struct fg_chip *chip, int *batt_id)
789 rc = fg_read(chip, ADC_RR_FAKE_BATT_LOW_LSB(chip), buf, 2);
791 pr_err("failed to read addr=0x%04x, rc=%d\n",
792 ADC_RR_FAKE_BATT_LOW_LSB(chip), rc);
797 * Fake battery threshold is encoded in the following format.
798 * Threshold (code) = (battery_id in Ohms) * 0.00015 * 2^10 / 2.5
800 temp = (buf[1] << 8 | buf[0]) * 2500000;
801 do_div(temp, 150 * 1024);
803 rc = fg_read(chip, ADC_RR_FAKE_BATT_HIGH_LSB(chip), buf, 2);
805 pr_err("failed to read addr=0x%04x, rc=%d\n",
806 ADC_RR_FAKE_BATT_HIGH_LSB(chip), rc);
810 temp = (buf[1] << 8 | buf[0]) * 2500000;
811 do_div(temp, 150 * 1024);
813 pr_debug("debug batt_id range: [%d %d]\n", batt_id[0], batt_id[1]);
817 static bool is_debug_batt_id(struct fg_chip *chip)
819 int debug_batt_id[2], rc;
821 if (!chip->batt_id_ohms)
824 rc = fg_get_debug_batt_id(chip, debug_batt_id);
826 pr_err("Failed to get debug batt_id, rc=%d\n", rc);
830 if (is_between(debug_batt_id[0], debug_batt_id[1],
831 chip->batt_id_ohms)) {
832 fg_dbg(chip, FG_POWER_SUPPLY, "Debug battery id: %dohms\n",
840 #define DEBUG_BATT_SOC 67
841 #define BATT_MISS_SOC 50
843 static int fg_get_prop_capacity(struct fg_chip *chip, int *val)
847 if (is_debug_batt_id(chip)) {
848 *val = DEBUG_BATT_SOC;
852 if (chip->fg_restarting) {
853 *val = chip->last_soc;
857 if (chip->battery_missing) {
858 *val = BATT_MISS_SOC;
862 if (is_batt_empty(chip)) {
867 if (chip->charge_full) {
868 *val = FULL_CAPACITY;
872 rc = fg_get_msoc(chip, &msoc);
876 if (chip->dt.linearize_soc && chip->delta_soc > 0)
877 *val = chip->maint_soc;
883 #define DEFAULT_BATT_TYPE "Unknown Battery"
884 #define MISSING_BATT_TYPE "Missing Battery"
885 #define LOADING_BATT_TYPE "Loading Battery"
886 static const char *fg_get_battery_type(struct fg_chip *chip)
888 if (chip->battery_missing)
889 return MISSING_BATT_TYPE;
891 if (chip->bp.batt_type_str) {
892 if (chip->profile_loaded)
893 return chip->bp.batt_type_str;
894 else if (chip->profile_available)
895 return LOADING_BATT_TYPE;
898 return DEFAULT_BATT_TYPE;
901 static int fg_batt_missing_config(struct fg_chip *chip, bool enable)
905 rc = fg_masked_write(chip, BATT_INFO_BATT_MISS_CFG(chip),
906 BM_FROM_BATT_ID_BIT, enable ? BM_FROM_BATT_ID_BIT : 0);
908 pr_err("Error in writing to %04x, rc=%d\n",
909 BATT_INFO_BATT_MISS_CFG(chip), rc);
913 static int fg_get_batt_id(struct fg_chip *chip)
915 int rc, ret, batt_id = 0;
917 if (!chip->batt_id_chan)
920 rc = fg_batt_missing_config(chip, false);
922 pr_err("Error in disabling BMD, rc=%d\n", rc);
926 rc = iio_read_channel_processed(chip->batt_id_chan, &batt_id);
928 pr_err("Error in reading batt_id channel, rc:%d\n", rc);
932 /* Wait for 200ms before enabling BMD again */
935 fg_dbg(chip, FG_STATUS, "batt_id: %d\n", batt_id);
936 chip->batt_id_ohms = batt_id;
938 ret = fg_batt_missing_config(chip, true);
940 pr_err("Error in enabling BMD, ret=%d\n", ret);
944 vote(chip->batt_miss_irq_en_votable, BATT_MISS_IRQ_VOTER, true, 0);
948 static int fg_get_batt_profile(struct fg_chip *chip)
950 struct device_node *node = chip->dev->of_node;
951 struct device_node *batt_node, *profile_node;
955 batt_node = of_find_node_by_name(node, "qcom,battery-data");
957 pr_err("Batterydata not available\n");
961 profile_node = of_batterydata_get_best_profile(batt_node,
962 chip->batt_id_ohms / 1000, NULL);
963 if (IS_ERR(profile_node))
964 return PTR_ERR(profile_node);
967 pr_err("couldn't find profile handle\n");
971 rc = of_property_read_string(profile_node, "qcom,battery-type",
972 &chip->bp.batt_type_str);
974 pr_err("battery type unavailable, rc:%d\n", rc);
978 rc = of_property_read_u32(profile_node, "qcom,max-voltage-uv",
979 &chip->bp.float_volt_uv);
981 pr_err("battery float voltage unavailable, rc:%d\n", rc);
982 chip->bp.float_volt_uv = -EINVAL;
985 rc = of_property_read_u32(profile_node, "qcom,fastchg-current-ma",
986 &chip->bp.fastchg_curr_ma);
988 pr_err("battery fastchg current unavailable, rc:%d\n", rc);
989 chip->bp.fastchg_curr_ma = -EINVAL;
992 rc = of_property_read_u32(profile_node, "qcom,fg-cc-cv-threshold-mv",
993 &chip->bp.vbatt_full_mv);
995 pr_err("battery cc_cv threshold unavailable, rc:%d\n", rc);
996 chip->bp.vbatt_full_mv = -EINVAL;
999 data = of_get_property(profile_node, "qcom,fg-profile-data", &len);
1001 pr_err("No profile data available\n");
1005 if (len != PROFILE_LEN) {
1006 pr_err("battery profile incorrect size: %d\n", len);
1010 chip->profile_available = true;
1011 memcpy(chip->batt_profile, data, len);
1016 static inline void get_batt_temp_delta(int delta, u8 *val)
1020 *val = BTEMP_DELTA_2K;
1023 *val = BTEMP_DELTA_4K;
1026 *val = BTEMP_DELTA_6K;
1029 *val = BTEMP_DELTA_10K;
1032 *val = BTEMP_DELTA_2K;
1037 static inline void get_esr_meas_current(int curr_ma, u8 *val)
1041 *val = ESR_MEAS_CUR_60MA;
1044 *val = ESR_MEAS_CUR_120MA;
1047 *val = ESR_MEAS_CUR_180MA;
1050 *val = ESR_MEAS_CUR_240MA;
1053 *val = ESR_MEAS_CUR_120MA;
1057 *val <<= ESR_PULL_DOWN_IVAL_SHIFT;
1060 static int fg_set_esr_timer(struct fg_chip *chip, int cycles_init,
1061 int cycles_max, bool charging, int flags)
1064 int rc, timer_max, timer_init;
1066 if (cycles_init < 0 || cycles_max < 0)
1070 timer_max = FG_SRAM_ESR_TIMER_CHG_MAX;
1071 timer_init = FG_SRAM_ESR_TIMER_CHG_INIT;
1073 timer_max = FG_SRAM_ESR_TIMER_DISCHG_MAX;
1074 timer_init = FG_SRAM_ESR_TIMER_DISCHG_INIT;
1077 fg_encode(chip->sp, timer_max, cycles_max, buf);
1078 rc = fg_sram_write(chip,
1079 chip->sp[timer_max].addr_word,
1080 chip->sp[timer_max].addr_byte, buf,
1081 chip->sp[timer_max].len, flags);
1083 pr_err("Error in writing esr_timer_dischg_max, rc=%d\n",
1088 fg_encode(chip->sp, timer_init, cycles_init, buf);
1089 rc = fg_sram_write(chip,
1090 chip->sp[timer_init].addr_word,
1091 chip->sp[timer_init].addr_byte, buf,
1092 chip->sp[timer_init].len, flags);
1094 pr_err("Error in writing esr_timer_dischg_init, rc=%d\n",
1099 fg_dbg(chip, FG_STATUS, "esr_%s_timer set to %d/%d\n",
1100 charging ? "charging" : "discharging", cycles_init, cycles_max);
1104 /* Other functions HERE */
1106 static void fg_notify_charger(struct fg_chip *chip)
1108 union power_supply_propval prop = {0, };
1111 if (!chip->batt_psy)
1114 if (!chip->profile_available)
1117 prop.intval = chip->bp.float_volt_uv;
1118 rc = power_supply_set_property(chip->batt_psy,
1119 POWER_SUPPLY_PROP_VOLTAGE_MAX, &prop);
1121 pr_err("Error in setting voltage_max property on batt_psy, rc=%d\n",
1126 prop.intval = chip->bp.fastchg_curr_ma * 1000;
1127 rc = power_supply_set_property(chip->batt_psy,
1128 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX, &prop);
1130 pr_err("Error in setting constant_charge_current_max property on batt_psy, rc=%d\n",
1135 fg_dbg(chip, FG_STATUS, "Notified charger on float voltage and FCC\n");
1138 static int fg_batt_miss_irq_en_cb(struct votable *votable, void *data,
1139 int enable, const char *client)
1141 struct fg_chip *chip = data;
1143 if (!chip->irqs[BATT_MISSING_IRQ].irq)
1147 enable_irq(chip->irqs[BATT_MISSING_IRQ].irq);
1148 enable_irq_wake(chip->irqs[BATT_MISSING_IRQ].irq);
1150 disable_irq_wake(chip->irqs[BATT_MISSING_IRQ].irq);
1151 disable_irq(chip->irqs[BATT_MISSING_IRQ].irq);
1157 static int fg_delta_bsoc_irq_en_cb(struct votable *votable, void *data,
1158 int enable, const char *client)
1160 struct fg_chip *chip = data;
1162 if (!chip->irqs[BSOC_DELTA_IRQ].irq)
1166 enable_irq(chip->irqs[BSOC_DELTA_IRQ].irq);
1167 enable_irq_wake(chip->irqs[BSOC_DELTA_IRQ].irq);
1169 disable_irq_wake(chip->irqs[BSOC_DELTA_IRQ].irq);
1170 disable_irq(chip->irqs[BSOC_DELTA_IRQ].irq);
1176 static int fg_awake_cb(struct votable *votable, void *data, int awake,
1179 struct fg_chip *chip = data;
1182 pm_stay_awake(chip->dev);
1184 pm_relax(chip->dev);
1186 pr_debug("client: %s awake: %d\n", client, awake);
1190 static bool batt_psy_initialized(struct fg_chip *chip)
1195 chip->batt_psy = power_supply_get_by_name("battery");
1196 if (!chip->batt_psy)
1199 /* batt_psy is initialized, set the fcc and fv */
1200 fg_notify_charger(chip);
1205 static bool usb_psy_initialized(struct fg_chip *chip)
1210 chip->usb_psy = power_supply_get_by_name("usb");
1217 static bool pc_port_psy_initialized(struct fg_chip *chip)
1219 if (chip->pc_port_psy)
1222 chip->pc_port_psy = power_supply_get_by_name("pc_port");
1223 if (!chip->pc_port_psy)
1229 static bool dc_psy_initialized(struct fg_chip *chip)
1234 chip->dc_psy = power_supply_get_by_name("dc");
1241 static bool is_parallel_charger_available(struct fg_chip *chip)
1243 if (!chip->parallel_psy)
1244 chip->parallel_psy = power_supply_get_by_name("parallel");
1246 if (!chip->parallel_psy)
1252 static int fg_save_learned_cap_to_sram(struct fg_chip *chip)
1257 if (chip->battery_missing || !chip->cl.learned_cc_uah)
1260 cc_mah = div64_s64(chip->cl.learned_cc_uah, 1000);
1261 /* Write to a backup register to use across reboot */
1262 rc = fg_sram_write(chip, chip->sp[FG_SRAM_ACT_BATT_CAP].addr_word,
1263 chip->sp[FG_SRAM_ACT_BATT_CAP].addr_byte, (u8 *)&cc_mah,
1264 chip->sp[FG_SRAM_ACT_BATT_CAP].len, FG_IMA_DEFAULT);
1266 pr_err("Error in writing act_batt_cap_bkup, rc=%d\n", rc);
1270 /* Write to actual capacity register for coulomb counter operation */
1271 rc = fg_sram_write(chip, ACT_BATT_CAP_WORD, ACT_BATT_CAP_OFFSET,
1272 (u8 *)&cc_mah, chip->sp[FG_SRAM_ACT_BATT_CAP].len,
1275 pr_err("Error in writing act_batt_cap, rc=%d\n", rc);
1279 fg_dbg(chip, FG_CAP_LEARN, "learned capacity %llduah/%dmah stored\n",
1280 chip->cl.learned_cc_uah, cc_mah);
1284 #define CAPACITY_DELTA_DECIPCT 500
1285 static int fg_load_learned_cap_from_sram(struct fg_chip *chip)
1287 int rc, act_cap_mah;
1288 int64_t delta_cc_uah, pct_nom_cap_uah;
1290 rc = fg_get_sram_prop(chip, FG_SRAM_ACT_BATT_CAP, &act_cap_mah);
1292 pr_err("Error in getting ACT_BATT_CAP, rc=%d\n", rc);
1296 chip->cl.learned_cc_uah = act_cap_mah * 1000;
1298 if (chip->cl.learned_cc_uah != chip->cl.nom_cap_uah) {
1299 if (chip->cl.learned_cc_uah == 0)
1300 chip->cl.learned_cc_uah = chip->cl.nom_cap_uah;
1302 delta_cc_uah = abs(chip->cl.learned_cc_uah -
1303 chip->cl.nom_cap_uah);
1304 pct_nom_cap_uah = div64_s64((int64_t)chip->cl.nom_cap_uah *
1305 CAPACITY_DELTA_DECIPCT, 1000);
1307 * If the learned capacity is out of range by 50% from the
1308 * nominal capacity, then overwrite the learned capacity with
1309 * the nominal capacity.
1311 if (chip->cl.nom_cap_uah && delta_cc_uah > pct_nom_cap_uah) {
1312 fg_dbg(chip, FG_CAP_LEARN, "learned_cc_uah: %lld is higher than expected, capping it to nominal: %lld\n",
1313 chip->cl.learned_cc_uah, chip->cl.nom_cap_uah);
1314 chip->cl.learned_cc_uah = chip->cl.nom_cap_uah;
1317 rc = fg_save_learned_cap_to_sram(chip);
1319 pr_err("Error in saving learned_cc_uah, rc=%d\n", rc);
1322 fg_dbg(chip, FG_CAP_LEARN, "learned_cc_uah:%lld nom_cap_uah: %lld\n",
1323 chip->cl.learned_cc_uah, chip->cl.nom_cap_uah);
1327 static bool is_temp_valid_cap_learning(struct fg_chip *chip)
1331 rc = fg_get_battery_temp(chip, &batt_temp);
1333 pr_err("Error in getting batt_temp\n");
1337 if (batt_temp > chip->dt.cl_max_temp ||
1338 batt_temp < chip->dt.cl_min_temp) {
1339 fg_dbg(chip, FG_CAP_LEARN, "batt temp %d out of range [%d %d]\n",
1340 batt_temp, chip->dt.cl_min_temp, chip->dt.cl_max_temp);
1347 #define QNOVO_CL_SKEW_DECIPCT -30
1348 static void fg_cap_learning_post_process(struct fg_chip *chip)
1350 int64_t max_inc_val, min_dec_val, old_cap;
1353 if (is_qnovo_en(chip)) {
1354 fg_dbg(chip, FG_CAP_LEARN, "applying skew %d on current learnt capacity %lld\n",
1355 QNOVO_CL_SKEW_DECIPCT, chip->cl.final_cc_uah);
1356 chip->cl.final_cc_uah = chip->cl.final_cc_uah *
1357 (1000 + QNOVO_CL_SKEW_DECIPCT);
1358 do_div(chip->cl.final_cc_uah, 1000);
1361 max_inc_val = chip->cl.learned_cc_uah
1362 * (1000 + chip->dt.cl_max_cap_inc);
1363 do_div(max_inc_val, 1000);
1365 min_dec_val = chip->cl.learned_cc_uah
1366 * (1000 - chip->dt.cl_max_cap_dec);
1367 do_div(min_dec_val, 1000);
1369 old_cap = chip->cl.learned_cc_uah;
1370 if (chip->cl.final_cc_uah > max_inc_val)
1371 chip->cl.learned_cc_uah = max_inc_val;
1372 else if (chip->cl.final_cc_uah < min_dec_val)
1373 chip->cl.learned_cc_uah = min_dec_val;
1375 chip->cl.learned_cc_uah =
1376 chip->cl.final_cc_uah;
1378 if (chip->dt.cl_max_cap_limit) {
1379 max_inc_val = (int64_t)chip->cl.nom_cap_uah * (1000 +
1380 chip->dt.cl_max_cap_limit);
1381 do_div(max_inc_val, 1000);
1382 if (chip->cl.final_cc_uah > max_inc_val) {
1383 fg_dbg(chip, FG_CAP_LEARN, "learning capacity %lld goes above max limit %lld\n",
1384 chip->cl.final_cc_uah, max_inc_val);
1385 chip->cl.learned_cc_uah = max_inc_val;
1389 if (chip->dt.cl_min_cap_limit) {
1390 min_dec_val = (int64_t)chip->cl.nom_cap_uah * (1000 -
1391 chip->dt.cl_min_cap_limit);
1392 do_div(min_dec_val, 1000);
1393 if (chip->cl.final_cc_uah < min_dec_val) {
1394 fg_dbg(chip, FG_CAP_LEARN, "learning capacity %lld goes below min limit %lld\n",
1395 chip->cl.final_cc_uah, min_dec_val);
1396 chip->cl.learned_cc_uah = min_dec_val;
1400 rc = fg_save_learned_cap_to_sram(chip);
1402 pr_err("Error in saving learned_cc_uah, rc=%d\n", rc);
1404 fg_dbg(chip, FG_CAP_LEARN, "final cc_uah = %lld, learned capacity %lld -> %lld uah\n",
1405 chip->cl.final_cc_uah, old_cap, chip->cl.learned_cc_uah);
1408 static int fg_cap_learning_process_full_data(struct fg_chip *chip)
1410 int rc, cc_soc_sw, cc_soc_delta_pct;
1411 int64_t delta_cc_uah;
1413 rc = fg_get_sram_prop(chip, FG_SRAM_CC_SOC_SW, &cc_soc_sw);
1415 pr_err("Error in getting CC_SOC_SW, rc=%d\n", rc);
1420 div64_s64((int64_t)(cc_soc_sw - chip->cl.init_cc_soc_sw) * 100,
1423 /* If the delta is < 50%, then skip processing full data */
1424 if (cc_soc_delta_pct < 50) {
1425 pr_err("cc_soc_delta_pct: %d\n", cc_soc_delta_pct);
1429 delta_cc_uah = div64_s64(chip->cl.learned_cc_uah * cc_soc_delta_pct,
1431 chip->cl.final_cc_uah = chip->cl.init_cc_uah + delta_cc_uah;
1432 fg_dbg(chip, FG_CAP_LEARN, "Current cc_soc=%d cc_soc_delta_pct=%d total_cc_uah=%lld\n",
1433 cc_soc_sw, cc_soc_delta_pct, chip->cl.final_cc_uah);
1437 #define BATT_SOC_32BIT GENMASK(31, 0)
1438 static int fg_cap_learning_begin(struct fg_chip *chip, u32 batt_soc)
1440 int rc, cc_soc_sw, batt_soc_msb;
1442 batt_soc_msb = batt_soc >> 24;
1443 if (DIV_ROUND_CLOSEST(batt_soc_msb * 100, FULL_SOC_RAW) >
1444 chip->dt.cl_start_soc) {
1445 fg_dbg(chip, FG_CAP_LEARN, "Battery SOC %d is high!, not starting\n",
1450 chip->cl.init_cc_uah = div64_s64(chip->cl.learned_cc_uah * batt_soc_msb,
1453 /* Prime cc_soc_sw with battery SOC when capacity learning begins */
1454 cc_soc_sw = div64_s64((int64_t)batt_soc * CC_SOC_30BIT,
1456 rc = fg_sram_write(chip, chip->sp[FG_SRAM_CC_SOC_SW].addr_word,
1457 chip->sp[FG_SRAM_CC_SOC_SW].addr_byte, (u8 *)&cc_soc_sw,
1458 chip->sp[FG_SRAM_CC_SOC_SW].len, FG_IMA_ATOMIC);
1460 pr_err("Error in writing cc_soc_sw, rc=%d\n", rc);
1464 chip->cl.init_cc_soc_sw = cc_soc_sw;
1465 chip->cl.active = true;
1466 fg_dbg(chip, FG_CAP_LEARN, "Capacity learning started @ battery SOC %d init_cc_soc_sw:%d\n",
1467 batt_soc_msb, chip->cl.init_cc_soc_sw);
1472 static int fg_cap_learning_done(struct fg_chip *chip)
1476 rc = fg_cap_learning_process_full_data(chip);
1478 pr_err("Error in processing cap learning full data, rc=%d\n",
1483 /* Write a FULL value to cc_soc_sw */
1484 cc_soc_sw = CC_SOC_30BIT;
1485 rc = fg_sram_write(chip, chip->sp[FG_SRAM_CC_SOC_SW].addr_word,
1486 chip->sp[FG_SRAM_CC_SOC_SW].addr_byte, (u8 *)&cc_soc_sw,
1487 chip->sp[FG_SRAM_CC_SOC_SW].len, FG_IMA_ATOMIC);
1489 pr_err("Error in writing cc_soc_sw, rc=%d\n", rc);
1493 fg_cap_learning_post_process(chip);
1498 static void fg_cap_learning_update(struct fg_chip *chip)
1500 int rc, batt_soc, batt_soc_msb;
1501 bool input_present = is_input_present(chip);
1503 mutex_lock(&chip->cl.lock);
1505 if (!is_temp_valid_cap_learning(chip) || !chip->cl.learned_cc_uah ||
1506 chip->battery_missing) {
1507 fg_dbg(chip, FG_CAP_LEARN, "Aborting cap_learning %lld\n",
1508 chip->cl.learned_cc_uah);
1509 chip->cl.active = false;
1510 chip->cl.init_cc_uah = 0;
1514 rc = fg_get_sram_prop(chip, FG_SRAM_BATT_SOC, &batt_soc);
1516 pr_err("Error in getting ACT_BATT_CAP, rc=%d\n", rc);
1520 batt_soc_msb = (u32)batt_soc >> 24;
1521 fg_dbg(chip, FG_CAP_LEARN, "Chg_status: %d cl_active: %d batt_soc: %d\n",
1522 chip->charge_status, chip->cl.active, batt_soc_msb);
1524 /* Initialize the starting point of learning capacity */
1525 if (!chip->cl.active) {
1526 if (chip->charge_status == POWER_SUPPLY_STATUS_CHARGING) {
1527 rc = fg_cap_learning_begin(chip, batt_soc);
1528 chip->cl.active = (rc == 0);
1532 if (chip->charge_done) {
1533 rc = fg_cap_learning_done(chip);
1535 pr_err("Error in completing capacity learning, rc=%d\n",
1538 chip->cl.active = false;
1539 chip->cl.init_cc_uah = 0;
1542 if (chip->charge_status == POWER_SUPPLY_STATUS_DISCHARGING) {
1543 if (!input_present) {
1544 fg_dbg(chip, FG_CAP_LEARN, "Capacity learning aborted @ battery SOC %d\n",
1546 chip->cl.active = false;
1547 chip->cl.init_cc_uah = 0;
1551 if (chip->charge_status == POWER_SUPPLY_STATUS_NOT_CHARGING) {
1552 if (is_qnovo_en(chip) && input_present) {
1554 * Don't abort the capacity learning when qnovo
1555 * is enabled and input is present where the
1556 * charging status can go to "not charging"
1560 fg_dbg(chip, FG_CAP_LEARN, "Capacity learning aborted @ battery SOC %d\n",
1562 chip->cl.active = false;
1563 chip->cl.init_cc_uah = 0;
1569 mutex_unlock(&chip->cl.lock);
1572 #define KI_COEFF_MED_DISCHG_DEFAULT 1500
1573 #define KI_COEFF_HI_DISCHG_DEFAULT 2200
1574 static int fg_adjust_ki_coeff_dischg(struct fg_chip *chip)
1577 int ki_coeff_med = KI_COEFF_MED_DISCHG_DEFAULT;
1578 int ki_coeff_hi = KI_COEFF_HI_DISCHG_DEFAULT;
1581 if (!chip->ki_coeff_dischg_en)
1584 rc = fg_get_prop_capacity(chip, &msoc);
1586 pr_err("Error in getting capacity, rc=%d\n", rc);
1590 if (chip->charge_status == POWER_SUPPLY_STATUS_DISCHARGING) {
1591 for (i = KI_COEFF_SOC_LEVELS - 1; i >= 0; i--) {
1592 if (msoc < chip->dt.ki_coeff_soc[i]) {
1593 ki_coeff_med = chip->dt.ki_coeff_med_dischg[i];
1594 ki_coeff_hi = chip->dt.ki_coeff_hi_dischg[i];
1599 fg_encode(chip->sp, FG_SRAM_KI_COEFF_MED_DISCHG, ki_coeff_med, &val);
1600 rc = fg_sram_write(chip,
1601 chip->sp[FG_SRAM_KI_COEFF_MED_DISCHG].addr_word,
1602 chip->sp[FG_SRAM_KI_COEFF_MED_DISCHG].addr_byte, &val,
1603 chip->sp[FG_SRAM_KI_COEFF_MED_DISCHG].len,
1606 pr_err("Error in writing ki_coeff_med, rc=%d\n", rc);
1610 fg_encode(chip->sp, FG_SRAM_KI_COEFF_HI_DISCHG, ki_coeff_hi, &val);
1611 rc = fg_sram_write(chip,
1612 chip->sp[FG_SRAM_KI_COEFF_HI_DISCHG].addr_word,
1613 chip->sp[FG_SRAM_KI_COEFF_HI_DISCHG].addr_byte, &val,
1614 chip->sp[FG_SRAM_KI_COEFF_HI_DISCHG].len,
1617 pr_err("Error in writing ki_coeff_hi, rc=%d\n", rc);
1621 fg_dbg(chip, FG_STATUS, "Wrote ki_coeff_med %d ki_coeff_hi %d\n",
1622 ki_coeff_med, ki_coeff_hi);
1626 #define KI_COEFF_FULL_SOC_DEFAULT 733
1627 static int fg_adjust_ki_coeff_full_soc(struct fg_chip *chip, int batt_temp)
1629 int rc, ki_coeff_full_soc;
1633 ki_coeff_full_soc = 0;
1634 else if (chip->charge_status == POWER_SUPPLY_STATUS_DISCHARGING)
1635 ki_coeff_full_soc = chip->dt.ki_coeff_full_soc_dischg;
1637 ki_coeff_full_soc = KI_COEFF_FULL_SOC_DEFAULT;
1639 if (chip->ki_coeff_full_soc == ki_coeff_full_soc)
1642 fg_encode(chip->sp, FG_SRAM_KI_COEFF_FULL_SOC, ki_coeff_full_soc, &val);
1643 rc = fg_sram_write(chip,
1644 chip->sp[FG_SRAM_KI_COEFF_FULL_SOC].addr_word,
1645 chip->sp[FG_SRAM_KI_COEFF_FULL_SOC].addr_byte, &val,
1646 chip->sp[FG_SRAM_KI_COEFF_FULL_SOC].len,
1649 pr_err("Error in writing ki_coeff_full_soc, rc=%d\n", rc);
1653 chip->ki_coeff_full_soc = ki_coeff_full_soc;
1654 fg_dbg(chip, FG_STATUS, "Wrote ki_coeff_full_soc %d\n",
1659 static int fg_set_recharge_voltage(struct fg_chip *chip, int voltage_mv)
1664 if (chip->dt.auto_recharge_soc)
1667 /* This configuration is available only for pmicobalt v2.0 and above */
1668 if (chip->wa_flags & PMI8998_V1_REV_WA)
1671 if (voltage_mv == chip->last_recharge_volt_mv)
1674 fg_dbg(chip, FG_STATUS, "Setting recharge voltage to %dmV\n",
1676 fg_encode(chip->sp, FG_SRAM_RECHARGE_VBATT_THR, voltage_mv, &buf);
1677 rc = fg_sram_write(chip,
1678 chip->sp[FG_SRAM_RECHARGE_VBATT_THR].addr_word,
1679 chip->sp[FG_SRAM_RECHARGE_VBATT_THR].addr_byte,
1680 &buf, chip->sp[FG_SRAM_RECHARGE_VBATT_THR].len,
1683 pr_err("Error in writing recharge_vbatt_thr, rc=%d\n",
1688 chip->last_recharge_volt_mv = voltage_mv;
1692 static int fg_configure_full_soc(struct fg_chip *chip, int bsoc)
1695 u8 full_soc[2] = {0xFF, 0xFF};
1698 * Once SOC masking condition is cleared, FULL_SOC and MONOTONIC_SOC
1699 * needs to be updated to reflect the same. Write battery SOC to
1700 * FULL_SOC and write a full value to MONOTONIC_SOC.
1702 rc = fg_sram_write(chip, FULL_SOC_WORD, FULL_SOC_OFFSET,
1703 (u8 *)&bsoc, 2, FG_IMA_ATOMIC);
1705 pr_err("failed to write full_soc rc=%d\n", rc);
1709 rc = fg_sram_write(chip, MONOTONIC_SOC_WORD, MONOTONIC_SOC_OFFSET,
1710 full_soc, 2, FG_IMA_ATOMIC);
1712 pr_err("failed to write monotonic_soc rc=%d\n", rc);
1719 #define AUTO_RECHG_VOLT_LOW_LIMIT_MV 3700
1720 static int fg_charge_full_update(struct fg_chip *chip)
1722 union power_supply_propval prop = {0, };
1723 int rc, msoc, bsoc, recharge_soc, msoc_raw;
1725 if (!chip->dt.hold_soc_while_full)
1728 if (!batt_psy_initialized(chip))
1731 mutex_lock(&chip->charge_full_lock);
1732 vote(chip->delta_bsoc_irq_en_votable, DELTA_BSOC_IRQ_VOTER,
1733 chip->charge_done, 0);
1734 rc = power_supply_get_property(chip->batt_psy, POWER_SUPPLY_PROP_HEALTH,
1737 pr_err("Error in getting battery health, rc=%d\n", rc);
1741 chip->health = prop.intval;
1742 recharge_soc = chip->dt.recharge_soc_thr;
1743 recharge_soc = DIV_ROUND_CLOSEST(recharge_soc * FULL_SOC_RAW,
1745 rc = fg_get_sram_prop(chip, FG_SRAM_BATT_SOC, &bsoc);
1747 pr_err("Error in getting BATT_SOC, rc=%d\n", rc);
1751 /* We need 2 most significant bytes here */
1752 bsoc = (u32)bsoc >> 16;
1753 rc = fg_get_msoc_raw(chip, &msoc_raw);
1755 pr_err("Error in getting msoc_raw, rc=%d\n", rc);
1758 msoc = DIV_ROUND_CLOSEST(msoc_raw * FULL_CAPACITY, FULL_SOC_RAW);
1760 fg_dbg(chip, FG_STATUS, "msoc: %d bsoc: %x health: %d status: %d full: %d\n",
1761 msoc, bsoc, chip->health, chip->charge_status,
1763 if (chip->charge_done && !chip->charge_full) {
1764 if (msoc >= 99 && chip->health == POWER_SUPPLY_HEALTH_GOOD) {
1765 fg_dbg(chip, FG_STATUS, "Setting charge_full to true\n");
1766 chip->charge_full = true;
1768 * Lower the recharge voltage so that VBAT_LT_RECHG
1769 * signal will not be asserted soon.
1771 rc = fg_set_recharge_voltage(chip,
1772 AUTO_RECHG_VOLT_LOW_LIMIT_MV);
1774 pr_err("Error in reducing recharge voltage, rc=%d\n",
1779 fg_dbg(chip, FG_STATUS, "Terminated charging @ SOC%d\n",
1782 } else if (msoc_raw <= recharge_soc && chip->charge_full) {
1783 if (chip->dt.linearize_soc) {
1784 chip->delta_soc = FULL_CAPACITY - msoc;
1787 * We're spreading out the delta SOC over every 10%
1788 * change in monotonic SOC. We cannot spread more than
1789 * 9% in the range of 0-100 skipping the first 10%.
1791 if (chip->delta_soc > 9) {
1792 chip->delta_soc = 0;
1793 chip->maint_soc = 0;
1795 chip->maint_soc = FULL_CAPACITY;
1796 chip->last_msoc = msoc;
1801 * Raise the recharge voltage so that VBAT_LT_RECHG signal
1802 * will be asserted soon as battery SOC had dropped below
1803 * the recharge SOC threshold.
1805 rc = fg_set_recharge_voltage(chip,
1806 chip->dt.recharge_volt_thr_mv);
1808 pr_err("Error in setting recharge voltage, rc=%d\n",
1814 * If charge_done is still set, wait for recharging or
1815 * discharging to happen.
1817 if (chip->charge_done)
1820 rc = fg_configure_full_soc(chip, bsoc);
1824 chip->charge_full = false;
1825 fg_dbg(chip, FG_STATUS, "msoc_raw = %d bsoc: %d recharge_soc: %d delta_soc: %d\n",
1826 msoc_raw, bsoc >> 8, recharge_soc, chip->delta_soc);
1830 mutex_unlock(&chip->charge_full_lock);
1834 #define RCONN_CONFIG_BIT BIT(0)
1835 static int fg_rconn_config(struct fg_chip *chip)
1841 if (!chip->dt.rconn_mohms)
1844 rc = fg_sram_read(chip, PROFILE_INTEGRITY_WORD,
1845 SW_CONFIG_OFFSET, (u8 *)&val, 1, FG_IMA_DEFAULT);
1847 pr_err("Error in reading SW_CONFIG_OFFSET, rc=%d\n", rc);
1851 if (val & RCONN_CONFIG_BIT) {
1852 fg_dbg(chip, FG_STATUS, "Rconn already configured: %x\n", val);
1856 rc = fg_get_sram_prop(chip, FG_SRAM_ESR, &esr_uohms);
1858 pr_err("failed to get ESR, rc=%d\n", rc);
1862 scaling_factor = div64_u64((u64)esr_uohms * 1000,
1863 esr_uohms + (chip->dt.rconn_mohms * 1000));
1865 rc = fg_sram_read(chip, ESR_RSLOW_CHG_WORD,
1866 ESR_RSLOW_CHG_OFFSET, (u8 *)&val, 1, FG_IMA_DEFAULT);
1868 pr_err("Error in reading ESR_RSLOW_CHG_OFFSET, rc=%d\n", rc);
1872 val *= scaling_factor;
1874 rc = fg_sram_write(chip, ESR_RSLOW_CHG_WORD,
1875 ESR_RSLOW_CHG_OFFSET, (u8 *)&val, 1, FG_IMA_DEFAULT);
1877 pr_err("Error in writing ESR_RSLOW_CHG_OFFSET, rc=%d\n", rc);
1880 fg_dbg(chip, FG_STATUS, "esr_rslow_chg modified to %x\n", val & 0xFF);
1882 rc = fg_sram_read(chip, ESR_RSLOW_DISCHG_WORD,
1883 ESR_RSLOW_DISCHG_OFFSET, (u8 *)&val, 1, FG_IMA_DEFAULT);
1885 pr_err("Error in reading ESR_RSLOW_DISCHG_OFFSET, rc=%d\n", rc);
1889 val *= scaling_factor;
1891 rc = fg_sram_write(chip, ESR_RSLOW_DISCHG_WORD,
1892 ESR_RSLOW_DISCHG_OFFSET, (u8 *)&val, 1, FG_IMA_DEFAULT);
1894 pr_err("Error in writing ESR_RSLOW_DISCHG_OFFSET, rc=%d\n", rc);
1897 fg_dbg(chip, FG_STATUS, "esr_rslow_dischg modified to %x\n",
1900 val = RCONN_CONFIG_BIT;
1901 rc = fg_sram_write(chip, PROFILE_INTEGRITY_WORD,
1902 SW_CONFIG_OFFSET, (u8 *)&val, 1, FG_IMA_DEFAULT);
1904 pr_err("Error in writing SW_CONFIG_OFFSET, rc=%d\n", rc);
1911 static int fg_set_jeita_threshold(struct fg_chip *chip,
1912 enum jeita_levels level, int temp_decidegC)
1918 if (temp_decidegC < -300 || temp_decidegC > 970)
1921 /* Resolution is 0.5C. Base is -30C. */
1922 val = DIV_ROUND_CLOSEST(((temp_decidegC / 10) + 30) * 10, 5);
1925 reg = BATT_INFO_JEITA_TOO_COLD(chip);
1928 reg = BATT_INFO_JEITA_COLD(chip);
1931 reg = BATT_INFO_JEITA_HOT(chip);
1934 reg = BATT_INFO_JEITA_TOO_HOT(chip);
1940 rc = fg_write(chip, reg, &val, 1);
1942 pr_err("Error in setting jeita level %d, rc=%d\n", level, rc);
1949 static int fg_set_constant_chg_voltage(struct fg_chip *chip, int volt_uv)
1954 if (volt_uv <= 0 || volt_uv > 15590000) {
1955 pr_err("Invalid voltage %d\n", volt_uv);
1959 fg_encode(chip->sp, FG_SRAM_VBATT_FULL, volt_uv, buf);
1961 rc = fg_sram_write(chip, chip->sp[FG_SRAM_VBATT_FULL].addr_word,
1962 chip->sp[FG_SRAM_VBATT_FULL].addr_byte, buf,
1963 chip->sp[FG_SRAM_VBATT_FULL].len, FG_IMA_DEFAULT);
1965 pr_err("Error in writing vbatt_full, rc=%d\n", rc);
1972 static int fg_set_recharge_soc(struct fg_chip *chip, int recharge_soc)
1977 if (!chip->dt.auto_recharge_soc)
1980 if (recharge_soc < 0 || recharge_soc > FULL_CAPACITY)
1983 fg_encode(chip->sp, FG_SRAM_RECHARGE_SOC_THR, recharge_soc, &buf);
1984 rc = fg_sram_write(chip,
1985 chip->sp[FG_SRAM_RECHARGE_SOC_THR].addr_word,
1986 chip->sp[FG_SRAM_RECHARGE_SOC_THR].addr_byte, &buf,
1987 chip->sp[FG_SRAM_RECHARGE_SOC_THR].len, FG_IMA_DEFAULT);
1989 pr_err("Error in writing recharge_soc_thr, rc=%d\n", rc);
1996 static int fg_adjust_recharge_soc(struct fg_chip *chip)
1998 int rc, msoc, recharge_soc, new_recharge_soc = 0;
1999 bool recharge_soc_status;
2001 if (!chip->dt.auto_recharge_soc)
2004 recharge_soc = chip->dt.recharge_soc_thr;
2005 recharge_soc_status = chip->recharge_soc_adjusted;
2007 * If the input is present and charging had been terminated, adjust
2008 * the recharge SOC threshold based on the monotonic SOC at which
2009 * the charge termination had happened.
2011 if (is_input_present(chip)) {
2012 if (chip->charge_done) {
2013 if (!chip->recharge_soc_adjusted) {
2014 /* Get raw monotonic SOC for calculation */
2015 rc = fg_get_msoc(chip, &msoc);
2017 pr_err("Error in getting msoc, rc=%d\n",
2022 /* Adjust the recharge_soc threshold */
2023 new_recharge_soc = msoc - (FULL_CAPACITY -
2025 chip->recharge_soc_adjusted = true;
2027 /* adjusted already, do nothing */
2031 /* Charging, do nothing */
2035 /* Restore the default value */
2036 new_recharge_soc = recharge_soc;
2037 chip->recharge_soc_adjusted = false;
2040 rc = fg_set_recharge_soc(chip, new_recharge_soc);
2042 chip->recharge_soc_adjusted = recharge_soc_status;
2043 pr_err("Couldn't set resume SOC for FG, rc=%d\n", rc);
2047 fg_dbg(chip, FG_STATUS, "resume soc set to %d\n", new_recharge_soc);
2051 static int fg_adjust_recharge_voltage(struct fg_chip *chip)
2053 int rc, recharge_volt_mv;
2055 if (chip->dt.auto_recharge_soc)
2058 fg_dbg(chip, FG_STATUS, "health: %d chg_status: %d chg_done: %d\n",
2059 chip->health, chip->charge_status, chip->charge_done);
2061 recharge_volt_mv = chip->dt.recharge_volt_thr_mv;
2063 /* Lower the recharge voltage in soft JEITA */
2064 if (chip->health == POWER_SUPPLY_HEALTH_WARM ||
2065 chip->health == POWER_SUPPLY_HEALTH_COOL)
2066 recharge_volt_mv -= 200;
2068 rc = fg_set_recharge_voltage(chip, recharge_volt_mv);
2070 pr_err("Error in setting recharge_voltage, rc=%d\n",
2078 static int fg_slope_limit_config(struct fg_chip *chip, int batt_temp)
2080 enum slope_limit_status status;
2084 if (!chip->slope_limit_en)
2087 if (chip->charge_status == POWER_SUPPLY_STATUS_CHARGING ||
2088 chip->charge_status == POWER_SUPPLY_STATUS_FULL) {
2089 if (batt_temp < chip->dt.slope_limit_temp)
2090 status = LOW_TEMP_CHARGE;
2092 status = HIGH_TEMP_CHARGE;
2094 if (batt_temp < chip->dt.slope_limit_temp)
2095 status = LOW_TEMP_DISCHARGE;
2097 status = HIGH_TEMP_DISCHARGE;
2100 if (chip->slope_limit_sts == status)
2103 fg_encode(chip->sp, FG_SRAM_SLOPE_LIMIT,
2104 chip->dt.slope_limit_coeffs[status], &buf);
2105 rc = fg_sram_write(chip, chip->sp[FG_SRAM_SLOPE_LIMIT].addr_word,
2106 chip->sp[FG_SRAM_SLOPE_LIMIT].addr_byte, &buf,
2107 chip->sp[FG_SRAM_SLOPE_LIMIT].len, FG_IMA_DEFAULT);
2109 pr_err("Error in configuring slope_limit coefficient, rc=%d\n",
2114 chip->slope_limit_sts = status;
2115 fg_dbg(chip, FG_STATUS, "Slope limit status: %d value: %x\n", status,
2120 static int fg_esr_filter_config(struct fg_chip *chip, int batt_temp)
2122 u8 esr_tight_lt_flt, esr_broad_lt_flt;
2123 bool cold_temp = false;
2127 * If the battery temperature is lower than -20 C, then skip modifying
2130 if (batt_temp < -210)
2134 * If battery temperature is lesser than 10 C (default), then apply the
2135 * ESR low temperature tight and broad filter values to ESR room
2136 * temperature tight and broad filters. If battery temperature is higher
2137 * than 10 C, then apply back the room temperature ESR filter
2138 * coefficients to ESR room temperature tight and broad filters.
2140 if (batt_temp > chip->dt.esr_flt_switch_temp
2141 && chip->esr_flt_cold_temp_en) {
2142 fg_encode(chip->sp, FG_SRAM_ESR_TIGHT_FILTER,
2143 chip->dt.esr_tight_flt_upct, &esr_tight_lt_flt);
2144 fg_encode(chip->sp, FG_SRAM_ESR_BROAD_FILTER,
2145 chip->dt.esr_broad_flt_upct, &esr_broad_lt_flt);
2146 } else if (batt_temp <= chip->dt.esr_flt_switch_temp
2147 && !chip->esr_flt_cold_temp_en) {
2148 fg_encode(chip->sp, FG_SRAM_ESR_TIGHT_FILTER,
2149 chip->dt.esr_tight_lt_flt_upct, &esr_tight_lt_flt);
2150 fg_encode(chip->sp, FG_SRAM_ESR_BROAD_FILTER,
2151 chip->dt.esr_broad_lt_flt_upct, &esr_broad_lt_flt);
2157 rc = fg_sram_write(chip, chip->sp[FG_SRAM_ESR_TIGHT_FILTER].addr_word,
2158 chip->sp[FG_SRAM_ESR_TIGHT_FILTER].addr_byte,
2160 chip->sp[FG_SRAM_ESR_TIGHT_FILTER].len, FG_IMA_DEFAULT);
2162 pr_err("Error in writing ESR LT tight filter, rc=%d\n", rc);
2166 rc = fg_sram_write(chip, chip->sp[FG_SRAM_ESR_BROAD_FILTER].addr_word,
2167 chip->sp[FG_SRAM_ESR_BROAD_FILTER].addr_byte,
2169 chip->sp[FG_SRAM_ESR_BROAD_FILTER].len, FG_IMA_DEFAULT);
2171 pr_err("Error in writing ESR LT broad filter, rc=%d\n", rc);
2175 chip->esr_flt_cold_temp_en = cold_temp;
2176 fg_dbg(chip, FG_STATUS, "applied %s ESR filter values\n",
2177 cold_temp ? "cold" : "normal");
2181 static int fg_esr_fcc_config(struct fg_chip *chip)
2183 union power_supply_propval prop = {0, };
2185 bool parallel_en = false, qnovo_en;
2187 if (is_parallel_charger_available(chip)) {
2188 rc = power_supply_get_property(chip->parallel_psy,
2189 POWER_SUPPLY_PROP_CHARGING_ENABLED, &prop);
2191 pr_err("Error in reading charging_enabled from parallel_psy, rc=%d\n",
2195 parallel_en = prop.intval;
2198 qnovo_en = is_qnovo_en(chip);
2200 fg_dbg(chip, FG_POWER_SUPPLY, "chg_sts: %d par_en: %d qnov_en: %d esr_fcc_ctrl_en: %d\n",
2201 chip->charge_status, parallel_en, qnovo_en,
2202 chip->esr_fcc_ctrl_en);
2204 if (chip->charge_status == POWER_SUPPLY_STATUS_CHARGING &&
2205 (parallel_en || qnovo_en)) {
2206 if (chip->esr_fcc_ctrl_en)
2210 * When parallel charging or Qnovo is enabled, configure ESR
2211 * FCC to 300mA to trigger an ESR pulse. Without this, FG can
2212 * request the main charger to increase FCC when it is supposed
2215 rc = fg_masked_write(chip, BATT_INFO_ESR_FAST_CRG_CFG(chip),
2216 ESR_FAST_CRG_IVAL_MASK |
2217 ESR_FAST_CRG_CTL_EN_BIT,
2218 ESR_FCC_300MA | ESR_FAST_CRG_CTL_EN_BIT);
2220 pr_err("Error in writing to %04x, rc=%d\n",
2221 BATT_INFO_ESR_FAST_CRG_CFG(chip), rc);
2225 chip->esr_fcc_ctrl_en = true;
2227 if (!chip->esr_fcc_ctrl_en)
2231 * If we're here, then it means either the device is not in
2232 * charging state or parallel charging / Qnovo is disabled.
2233 * Disable ESR fast charge current control in SW.
2235 rc = fg_masked_write(chip, BATT_INFO_ESR_FAST_CRG_CFG(chip),
2236 ESR_FAST_CRG_CTL_EN_BIT, 0);
2238 pr_err("Error in writing to %04x, rc=%d\n",
2239 BATT_INFO_ESR_FAST_CRG_CFG(chip), rc);
2243 chip->esr_fcc_ctrl_en = false;
2246 fg_dbg(chip, FG_STATUS, "esr_fcc_ctrl_en set to %d\n",
2247 chip->esr_fcc_ctrl_en);
2251 static int fg_esr_timer_config(struct fg_chip *chip, bool sleep)
2253 int rc, cycles_init, cycles_max;
2254 bool end_of_charge = false;
2256 end_of_charge = is_input_present(chip) && chip->charge_done;
2257 fg_dbg(chip, FG_STATUS, "sleep: %d eoc: %d\n", sleep, end_of_charge);
2259 /* ESR discharging timer configuration */
2260 cycles_init = sleep ? chip->dt.esr_timer_asleep[TIMER_RETRY] :
2261 chip->dt.esr_timer_awake[TIMER_RETRY];
2265 cycles_max = sleep ? chip->dt.esr_timer_asleep[TIMER_MAX] :
2266 chip->dt.esr_timer_awake[TIMER_MAX];
2268 rc = fg_set_esr_timer(chip, cycles_init, cycles_max, false,
2269 sleep ? FG_IMA_NO_WLOCK : FG_IMA_DEFAULT);
2271 pr_err("Error in setting ESR timer, rc=%d\n", rc);
2275 /* ESR charging timer configuration */
2276 cycles_init = cycles_max = -EINVAL;
2277 if (end_of_charge || sleep) {
2278 cycles_init = chip->dt.esr_timer_charging[TIMER_RETRY];
2279 cycles_max = chip->dt.esr_timer_charging[TIMER_MAX];
2280 } else if (is_input_present(chip)) {
2281 cycles_init = chip->esr_timer_charging_default[TIMER_RETRY];
2282 cycles_max = chip->esr_timer_charging_default[TIMER_MAX];
2285 rc = fg_set_esr_timer(chip, cycles_init, cycles_max, true,
2286 sleep ? FG_IMA_NO_WLOCK : FG_IMA_DEFAULT);
2288 pr_err("Error in setting ESR timer, rc=%d\n", rc);
2295 static void fg_ttf_update(struct fg_chip *chip)
2299 union power_supply_propval prop = {0, };
2302 if (usb_psy_initialized(chip)) {
2303 rc = power_supply_get_property(chip->usb_psy,
2304 POWER_SUPPLY_PROP_ONLINE, &prop);
2306 pr_err("Couldn't read usb ONLINE prop rc=%d\n", rc);
2310 online = online || prop.intval;
2313 if (pc_port_psy_initialized(chip)) {
2314 rc = power_supply_get_property(chip->pc_port_psy,
2315 POWER_SUPPLY_PROP_ONLINE, &prop);
2317 pr_err("Couldn't read pc_port ONLINE prop rc=%d\n", rc);
2321 online = online || prop.intval;
2324 if (dc_psy_initialized(chip)) {
2325 rc = power_supply_get_property(chip->dc_psy,
2326 POWER_SUPPLY_PROP_ONLINE, &prop);
2328 pr_err("Couldn't read dc ONLINE prop rc=%d\n", rc);
2332 online = online || prop.intval;
2336 if (chip->online_status == online)
2339 chip->online_status = online;
2341 /* wait 35 seconds for the input to settle */
2344 /* wait 5 seconds for current to settle during discharge */
2347 vote(chip->awake_votable, TTF_PRIMING, true, 0);
2348 cancel_delayed_work_sync(&chip->ttf_work);
2349 mutex_lock(&chip->ttf.lock);
2350 fg_circ_buf_clr(&chip->ttf.ibatt);
2351 fg_circ_buf_clr(&chip->ttf.vbatt);
2352 chip->ttf.last_ttf = 0;
2353 chip->ttf.last_ms = 0;
2354 mutex_unlock(&chip->ttf.lock);
2355 schedule_delayed_work(&chip->ttf_work, msecs_to_jiffies(delay_ms));
2358 static void restore_cycle_counter(struct fg_chip *chip)
2363 if (!chip->cyc_ctr.en)
2366 mutex_lock(&chip->cyc_ctr.lock);
2367 for (i = 0; i < BUCKET_COUNT; i++) {
2368 rc = fg_sram_read(chip, CYCLE_COUNT_WORD + (i / 2),
2369 CYCLE_COUNT_OFFSET + (i % 2) * 2, data, 2,
2372 pr_err("failed to read bucket %d rc=%d\n", i, rc);
2374 chip->cyc_ctr.count[i] = data[0] | data[1] << 8;
2376 mutex_unlock(&chip->cyc_ctr.lock);
2379 static void clear_cycle_counter(struct fg_chip *chip)
2383 if (!chip->cyc_ctr.en)
2386 mutex_lock(&chip->cyc_ctr.lock);
2387 memset(chip->cyc_ctr.count, 0, sizeof(chip->cyc_ctr.count));
2388 for (i = 0; i < BUCKET_COUNT; i++) {
2389 chip->cyc_ctr.started[i] = false;
2390 chip->cyc_ctr.last_soc[i] = 0;
2392 rc = fg_sram_write(chip, CYCLE_COUNT_WORD, CYCLE_COUNT_OFFSET,
2393 (u8 *)&chip->cyc_ctr.count,
2394 sizeof(chip->cyc_ctr.count) / sizeof(u8 *),
2397 pr_err("failed to clear cycle counter rc=%d\n", rc);
2399 mutex_unlock(&chip->cyc_ctr.lock);
2402 static int fg_inc_store_cycle_ctr(struct fg_chip *chip, int bucket)
2408 if (bucket < 0 || (bucket > BUCKET_COUNT - 1))
2411 cyc_count = chip->cyc_ctr.count[bucket];
2413 data[0] = cyc_count & 0xFF;
2414 data[1] = cyc_count >> 8;
2416 rc = fg_sram_write(chip, CYCLE_COUNT_WORD + (bucket / 2),
2417 CYCLE_COUNT_OFFSET + (bucket % 2) * 2, data, 2,
2420 pr_err("failed to write BATT_CYCLE[%d] rc=%d\n",
2425 chip->cyc_ctr.count[bucket] = cyc_count;
2426 fg_dbg(chip, FG_STATUS, "Stored count %d in bucket %d\n", cyc_count,
2432 static void fg_cycle_counter_update(struct fg_chip *chip)
2434 int rc = 0, bucket, i, batt_soc;
2436 if (!chip->cyc_ctr.en)
2439 mutex_lock(&chip->cyc_ctr.lock);
2440 rc = fg_get_sram_prop(chip, FG_SRAM_BATT_SOC, &batt_soc);
2442 pr_err("Failed to read battery soc rc: %d\n", rc);
2446 /* We need only the most significant byte here */
2447 batt_soc = (u32)batt_soc >> 24;
2449 /* Find out which bucket the SOC falls in */
2450 bucket = batt_soc / BUCKET_SOC_PCT;
2452 if (chip->charge_status == POWER_SUPPLY_STATUS_CHARGING) {
2453 if (!chip->cyc_ctr.started[bucket]) {
2454 chip->cyc_ctr.started[bucket] = true;
2455 chip->cyc_ctr.last_soc[bucket] = batt_soc;
2457 } else if (chip->charge_done || !is_input_present(chip)) {
2458 for (i = 0; i < BUCKET_COUNT; i++) {
2459 if (chip->cyc_ctr.started[i] &&
2460 batt_soc > chip->cyc_ctr.last_soc[i] + 2) {
2461 rc = fg_inc_store_cycle_ctr(chip, i);
2463 pr_err("Error in storing cycle_ctr rc: %d\n",
2465 chip->cyc_ctr.last_soc[i] = 0;
2466 chip->cyc_ctr.started[i] = false;
2471 fg_dbg(chip, FG_STATUS, "batt_soc: %d bucket: %d chg_status: %d\n",
2472 batt_soc, bucket, chip->charge_status);
2474 mutex_unlock(&chip->cyc_ctr.lock);
2477 static int fg_get_cycle_count(struct fg_chip *chip)
2481 if (!chip->cyc_ctr.en)
2484 if ((chip->cyc_ctr.id <= 0) || (chip->cyc_ctr.id > BUCKET_COUNT))
2487 mutex_lock(&chip->cyc_ctr.lock);
2488 count = chip->cyc_ctr.count[chip->cyc_ctr.id - 1];
2489 mutex_unlock(&chip->cyc_ctr.lock);
2493 static void status_change_work(struct work_struct *work)
2495 struct fg_chip *chip = container_of(work,
2496 struct fg_chip, status_change_work);
2497 union power_supply_propval prop = {0, };
2500 if (!batt_psy_initialized(chip)) {
2501 fg_dbg(chip, FG_STATUS, "Charger not available?!\n");
2505 rc = power_supply_get_property(chip->batt_psy, POWER_SUPPLY_PROP_STATUS,
2508 pr_err("Error in getting charging status, rc=%d\n", rc);
2512 chip->charge_status = prop.intval;
2513 rc = power_supply_get_property(chip->batt_psy,
2514 POWER_SUPPLY_PROP_CHARGE_TYPE, &prop);
2516 pr_err("Error in getting charge type, rc=%d\n", rc);
2520 chip->charge_type = prop.intval;
2521 rc = power_supply_get_property(chip->batt_psy,
2522 POWER_SUPPLY_PROP_CHARGE_DONE, &prop);
2524 pr_err("Error in getting charge_done, rc=%d\n", rc);
2528 chip->charge_done = prop.intval;
2529 fg_cycle_counter_update(chip);
2530 fg_cap_learning_update(chip);
2532 rc = fg_charge_full_update(chip);
2534 pr_err("Error in charge_full_update, rc=%d\n", rc);
2536 rc = fg_adjust_recharge_soc(chip);
2538 pr_err("Error in adjusting recharge_soc, rc=%d\n", rc);
2540 rc = fg_adjust_recharge_voltage(chip);
2542 pr_err("Error in adjusting recharge_voltage, rc=%d\n", rc);
2544 rc = fg_adjust_ki_coeff_dischg(chip);
2546 pr_err("Error in adjusting ki_coeff_dischg, rc=%d\n", rc);
2548 rc = fg_esr_fcc_config(chip);
2550 pr_err("Error in adjusting FCC for ESR, rc=%d\n", rc);
2552 rc = fg_get_battery_temp(chip, &batt_temp);
2554 rc = fg_slope_limit_config(chip, batt_temp);
2556 pr_err("Error in configuring slope limiter rc:%d\n",
2559 rc = fg_adjust_ki_coeff_full_soc(chip, batt_temp);
2561 pr_err("Error in configuring ki_coeff_full_soc rc:%d\n",
2565 fg_ttf_update(chip);
2568 fg_dbg(chip, FG_POWER_SUPPLY, "charge_status:%d charge_type:%d charge_done:%d\n",
2569 chip->charge_status, chip->charge_type, chip->charge_done);
2570 pm_relax(chip->dev);
2573 static int fg_bp_params_config(struct fg_chip *chip)
2578 /* This SRAM register is only present in v2.0 and above */
2579 if (!(chip->wa_flags & PMI8998_V1_REV_WA) &&
2580 chip->bp.float_volt_uv > 0) {
2581 fg_encode(chip->sp, FG_SRAM_FLOAT_VOLT,
2582 chip->bp.float_volt_uv / 1000, &buf);
2583 rc = fg_sram_write(chip, chip->sp[FG_SRAM_FLOAT_VOLT].addr_word,
2584 chip->sp[FG_SRAM_FLOAT_VOLT].addr_byte, &buf,
2585 chip->sp[FG_SRAM_FLOAT_VOLT].len, FG_IMA_DEFAULT);
2587 pr_err("Error in writing float_volt, rc=%d\n", rc);
2592 if (chip->bp.vbatt_full_mv > 0) {
2593 rc = fg_set_constant_chg_voltage(chip,
2594 chip->bp.vbatt_full_mv * 1000);
2602 #define PROFILE_LOAD_BIT BIT(0)
2603 #define BOOTLOADER_LOAD_BIT BIT(1)
2604 #define BOOTLOADER_RESTART_BIT BIT(2)
2605 #define HLOS_RESTART_BIT BIT(3)
2606 static bool is_profile_load_required(struct fg_chip *chip)
2608 u8 buf[PROFILE_COMP_LEN], val;
2609 bool profiles_same = false;
2612 rc = fg_sram_read(chip, PROFILE_INTEGRITY_WORD,
2613 PROFILE_INTEGRITY_OFFSET, &val, 1, FG_IMA_DEFAULT);
2615 pr_err("failed to read profile integrity rc=%d\n", rc);
2619 /* Check if integrity bit is set */
2620 if (val & PROFILE_LOAD_BIT) {
2621 fg_dbg(chip, FG_STATUS, "Battery profile integrity bit is set\n");
2623 /* Whitelist the values */
2624 val &= ~PROFILE_LOAD_BIT;
2625 if (val != HLOS_RESTART_BIT && val != BOOTLOADER_LOAD_BIT &&
2626 val != (BOOTLOADER_LOAD_BIT | BOOTLOADER_RESTART_BIT)) {
2627 val |= PROFILE_LOAD_BIT;
2628 pr_warn("Garbage value in profile integrity word: 0x%x\n",
2633 rc = fg_sram_read(chip, PROFILE_LOAD_WORD, PROFILE_LOAD_OFFSET,
2634 buf, PROFILE_COMP_LEN, FG_IMA_DEFAULT);
2636 pr_err("Error in reading battery profile, rc:%d\n", rc);
2639 profiles_same = memcmp(chip->batt_profile, buf,
2640 PROFILE_COMP_LEN) == 0;
2641 if (profiles_same) {
2642 fg_dbg(chip, FG_STATUS, "Battery profile is same, not loading it\n");
2646 if (!chip->dt.force_load_profile) {
2647 pr_warn("Profiles doesn't match, skipping loading it since force_load_profile is disabled\n");
2648 if (fg_profile_dump) {
2649 pr_info("FG: loaded profile:\n");
2650 dump_sram(buf, PROFILE_LOAD_WORD,
2652 pr_info("FG: available profile:\n");
2653 dump_sram(chip->batt_profile, PROFILE_LOAD_WORD,
2659 fg_dbg(chip, FG_STATUS, "Profiles are different, loading the correct one\n");
2661 fg_dbg(chip, FG_STATUS, "Profile integrity bit is not set\n");
2662 if (fg_profile_dump) {
2663 pr_info("FG: profile to be loaded:\n");
2664 dump_sram(chip->batt_profile, PROFILE_LOAD_WORD,
2671 static void clear_battery_profile(struct fg_chip *chip)
2676 rc = fg_sram_write(chip, PROFILE_INTEGRITY_WORD,
2677 PROFILE_INTEGRITY_OFFSET, &val, 1, FG_IMA_DEFAULT);
2679 pr_err("failed to write profile integrity rc=%d\n", rc);
2682 #define SOC_READY_WAIT_MS 2000
2683 static int __fg_restart(struct fg_chip *chip)
2686 bool tried_again = false;
2688 rc = fg_get_prop_capacity(chip, &msoc);
2690 pr_err("Error in getting capacity, rc=%d\n", rc);
2694 chip->last_soc = msoc;
2695 chip->fg_restarting = true;
2696 reinit_completion(&chip->soc_ready);
2697 rc = fg_masked_write(chip, BATT_SOC_RESTART(chip), RESTART_GO_BIT,
2700 pr_err("Error in writing to %04x, rc=%d\n",
2701 BATT_SOC_RESTART(chip), rc);
2706 rc = wait_for_completion_interruptible_timeout(&chip->soc_ready,
2707 msecs_to_jiffies(SOC_READY_WAIT_MS));
2709 /* If we were interrupted wait again one more time. */
2710 if (rc == -ERESTARTSYS && !tried_again) {
2713 } else if (rc <= 0) {
2714 pr_err("wait for soc_ready timed out rc=%d\n", rc);
2717 rc = fg_masked_write(chip, BATT_SOC_RESTART(chip), RESTART_GO_BIT, 0);
2719 pr_err("Error in writing to %04x, rc=%d\n",
2720 BATT_SOC_RESTART(chip), rc);
2724 chip->fg_restarting = false;
2728 static void profile_load_work(struct work_struct *work)
2730 struct fg_chip *chip = container_of(work,
2732 profile_load_work.work);
2736 vote(chip->awake_votable, PROFILE_LOAD, true, 0);
2738 rc = fg_get_batt_id(chip);
2740 pr_err("Error in getting battery id, rc:%d\n", rc);
2744 rc = fg_get_batt_profile(chip);
2746 pr_warn("profile for batt_id=%dKOhms not found..using OTP, rc:%d\n",
2747 chip->batt_id_ohms / 1000, rc);
2751 if (!chip->profile_available)
2754 if (!is_profile_load_required(chip))
2757 clear_cycle_counter(chip);
2758 mutex_lock(&chip->cl.lock);
2759 chip->cl.learned_cc_uah = 0;
2760 chip->cl.active = false;
2761 mutex_unlock(&chip->cl.lock);
2763 fg_dbg(chip, FG_STATUS, "profile loading started\n");
2764 rc = fg_masked_write(chip, BATT_SOC_RESTART(chip), RESTART_GO_BIT, 0);
2766 pr_err("Error in writing to %04x, rc=%d\n",
2767 BATT_SOC_RESTART(chip), rc);
2771 /* load battery profile */
2772 rc = fg_sram_write(chip, PROFILE_LOAD_WORD, PROFILE_LOAD_OFFSET,
2773 chip->batt_profile, PROFILE_LEN, FG_IMA_ATOMIC);
2775 pr_err("Error in writing battery profile, rc:%d\n", rc);
2779 rc = __fg_restart(chip);
2781 pr_err("Error in restarting FG, rc=%d\n", rc);
2785 fg_dbg(chip, FG_STATUS, "SOC is ready\n");
2787 /* Set the profile integrity bit */
2788 val = HLOS_RESTART_BIT | PROFILE_LOAD_BIT;
2789 rc = fg_sram_write(chip, PROFILE_INTEGRITY_WORD,
2790 PROFILE_INTEGRITY_OFFSET, &val, 1, FG_IMA_DEFAULT);
2792 pr_err("failed to write profile integrity rc=%d\n", rc);
2797 rc = fg_bp_params_config(chip);
2799 pr_err("Error in configuring battery profile params, rc:%d\n",
2802 rc = fg_sram_read(chip, NOM_CAP_WORD, NOM_CAP_OFFSET, buf, 2,
2805 pr_err("Error in reading %04x[%d] rc=%d\n", NOM_CAP_WORD,
2806 NOM_CAP_OFFSET, rc);
2808 chip->cl.nom_cap_uah = (int)(buf[0] | buf[1] << 8) * 1000;
2809 rc = fg_load_learned_cap_from_sram(chip);
2811 pr_err("Error in loading capacity learning data, rc:%d\n",
2815 batt_psy_initialized(chip);
2816 fg_notify_charger(chip);
2817 chip->profile_loaded = true;
2818 fg_dbg(chip, FG_STATUS, "profile loaded successfully");
2820 chip->soc_reporting_ready = true;
2821 vote(chip->awake_votable, PROFILE_LOAD, false, 0);
2824 static void sram_dump_work(struct work_struct *work)
2826 struct fg_chip *chip = container_of(work, struct fg_chip,
2827 sram_dump_work.work);
2828 u8 buf[FG_SRAM_LEN];
2830 s64 timestamp_ms, quotient;
2833 rc = fg_sram_read(chip, 0, 0, buf, FG_SRAM_LEN, FG_IMA_DEFAULT);
2835 pr_err("Error in reading FG SRAM, rc:%d\n", rc);
2839 timestamp_ms = ktime_to_ms(ktime_get_boottime());
2840 quotient = div_s64_rem(timestamp_ms, 1000, &remainder);
2841 fg_dbg(chip, FG_STATUS, "SRAM Dump Started at %lld.%d\n",
2842 quotient, remainder);
2843 dump_sram(buf, 0, FG_SRAM_LEN);
2844 timestamp_ms = ktime_to_ms(ktime_get_boottime());
2845 quotient = div_s64_rem(timestamp_ms, 1000, &remainder);
2846 fg_dbg(chip, FG_STATUS, "SRAM Dump done at %lld.%d\n",
2847 quotient, remainder);
2849 schedule_delayed_work(&chip->sram_dump_work,
2850 msecs_to_jiffies(fg_sram_dump_period_ms));
2853 static int fg_sram_dump_sysfs(const char *val, const struct kernel_param *kp)
2856 struct power_supply *bms_psy;
2857 struct fg_chip *chip;
2858 bool old_val = fg_sram_dump;
2860 rc = param_set_bool(val, kp);
2862 pr_err("Unable to set fg_sram_dump: %d\n", rc);
2866 if (fg_sram_dump == old_val)
2869 bms_psy = power_supply_get_by_name("bms");
2871 pr_err("bms psy not found\n");
2875 chip = power_supply_get_drvdata(bms_psy);
2877 schedule_delayed_work(&chip->sram_dump_work,
2878 msecs_to_jiffies(fg_sram_dump_period_ms));
2880 cancel_delayed_work_sync(&chip->sram_dump_work);
2885 static struct kernel_param_ops fg_sram_dump_ops = {
2886 .set = fg_sram_dump_sysfs,
2887 .get = param_get_bool,
2890 module_param_cb(sram_dump_en, &fg_sram_dump_ops, &fg_sram_dump, 0644);
2892 static int fg_restart_sysfs(const char *val, const struct kernel_param *kp)
2895 struct power_supply *bms_psy;
2896 struct fg_chip *chip;
2898 rc = param_set_int(val, kp);
2900 pr_err("Unable to set fg_restart: %d\n", rc);
2904 if (fg_restart != 1) {
2905 pr_err("Bad value %d\n", fg_restart);
2909 bms_psy = power_supply_get_by_name("bms");
2911 pr_err("bms psy not found\n");
2915 chip = power_supply_get_drvdata(bms_psy);
2916 rc = __fg_restart(chip);
2918 pr_err("Error in restarting FG, rc=%d\n", rc);
2922 pr_info("FG restart done\n");
2926 static struct kernel_param_ops fg_restart_ops = {
2927 .set = fg_restart_sysfs,
2928 .get = param_get_int,
2931 module_param_cb(restart, &fg_restart_ops, &fg_restart, 0644);
2933 #define HOURS_TO_SECONDS 3600
2934 #define OCV_SLOPE_UV 10869
2935 #define MILLI_UNIT 1000
2936 #define MICRO_UNIT 1000000
2937 #define NANO_UNIT 1000000000
2938 static int fg_get_time_to_full_locked(struct fg_chip *chip, int *val)
2940 int rc, ibatt_avg, vbatt_avg, rbatt, msoc, full_soc, act_cap_mah,
2941 i_cc2cv, soc_cc2cv, tau, divisor, iterm, ttf_mode,
2942 i, soc_per_step, msoc_this_step, msoc_next_step,
2943 ibatt_this_step, t_predicted_this_step, ttf_slope,
2944 t_predicted_cv, t_predicted = 0;
2947 if (chip->bp.float_volt_uv <= 0) {
2948 pr_err("battery profile is not loaded\n");
2952 if (!batt_psy_initialized(chip)) {
2953 fg_dbg(chip, FG_TTF, "charger is not available\n");
2957 rc = fg_get_prop_capacity(chip, &msoc);
2959 pr_err("failed to get msoc rc=%d\n", rc);
2962 fg_dbg(chip, FG_TTF, "msoc=%d\n", msoc);
2964 /* the battery is considered full if the SOC is 100% */
2970 if (is_qnovo_en(chip))
2971 ttf_mode = TTF_MODE_QNOVO;
2973 ttf_mode = TTF_MODE_NORMAL;
2975 /* when switching TTF algorithms the TTF needs to be reset */
2976 if (chip->ttf.mode != ttf_mode) {
2977 fg_circ_buf_clr(&chip->ttf.ibatt);
2978 fg_circ_buf_clr(&chip->ttf.vbatt);
2979 chip->ttf.last_ttf = 0;
2980 chip->ttf.last_ms = 0;
2981 chip->ttf.mode = ttf_mode;
2984 /* at least 10 samples are required to produce a stable IBATT */
2985 if (chip->ttf.ibatt.size < 10) {
2990 rc = fg_circ_buf_median(&chip->ttf.ibatt, &ibatt_avg);
2992 pr_err("failed to get IBATT AVG rc=%d\n", rc);
2996 rc = fg_circ_buf_median(&chip->ttf.vbatt, &vbatt_avg);
2998 pr_err("failed to get VBATT AVG rc=%d\n", rc);
3002 ibatt_avg = -ibatt_avg / MILLI_UNIT;
3003 vbatt_avg /= MILLI_UNIT;
3005 /* clamp ibatt_avg to iterm */
3006 if (ibatt_avg < abs(chip->dt.sys_term_curr_ma))
3007 ibatt_avg = abs(chip->dt.sys_term_curr_ma);
3009 fg_dbg(chip, FG_TTF, "ibatt_avg=%d\n", ibatt_avg);
3010 fg_dbg(chip, FG_TTF, "vbatt_avg=%d\n", vbatt_avg);
3012 rc = fg_get_battery_resistance(chip, &rbatt);
3014 pr_err("failed to get battery resistance rc=%d\n", rc);
3018 rbatt /= MILLI_UNIT;
3019 fg_dbg(chip, FG_TTF, "rbatt=%d\n", rbatt);
3021 rc = fg_get_sram_prop(chip, FG_SRAM_ACT_BATT_CAP, &act_cap_mah);
3023 pr_err("failed to get ACT_BATT_CAP rc=%d\n", rc);
3027 rc = fg_get_sram_prop(chip, FG_SRAM_FULL_SOC, &full_soc);
3029 pr_err("failed to get full soc rc=%d\n", rc);
3032 full_soc = DIV_ROUND_CLOSEST(((u16)full_soc >> 8) * FULL_CAPACITY,
3034 act_cap_mah = full_soc * act_cap_mah / 100;
3035 fg_dbg(chip, FG_TTF, "act_cap_mah=%d\n", act_cap_mah);
3037 /* estimated battery current at the CC to CV transition */
3038 switch (chip->ttf.mode) {
3039 case TTF_MODE_NORMAL:
3040 i_cc2cv = ibatt_avg * vbatt_avg /
3041 max(MILLI_UNIT, chip->bp.float_volt_uv / MILLI_UNIT);
3043 case TTF_MODE_QNOVO:
3045 chip->ttf.cc_step.arr[MAX_CC_STEPS - 1] / MILLI_UNIT,
3046 ibatt_avg * vbatt_avg /
3047 max(MILLI_UNIT, chip->bp.float_volt_uv / MILLI_UNIT));
3050 pr_err("TTF mode %d is not supported\n", chip->ttf.mode);
3053 fg_dbg(chip, FG_TTF, "i_cc2cv=%d\n", i_cc2cv);
3055 /* if we are already in CV state then we can skip estimating CC */
3056 if (chip->charge_type == POWER_SUPPLY_CHARGE_TYPE_TAPER)
3059 /* estimated SOC at the CC to CV transition */
3060 soc_cc2cv = DIV_ROUND_CLOSEST(rbatt * i_cc2cv, OCV_SLOPE_UV);
3061 soc_cc2cv = 100 - soc_cc2cv;
3062 fg_dbg(chip, FG_TTF, "soc_cc2cv=%d\n", soc_cc2cv);
3064 switch (chip->ttf.mode) {
3065 case TTF_MODE_NORMAL:
3066 if (soc_cc2cv - msoc <= 0)
3069 divisor = max(100, (ibatt_avg + i_cc2cv) / 2 * 100);
3070 t_predicted = div_s64((s64)act_cap_mah * (soc_cc2cv - msoc) *
3071 HOURS_TO_SECONDS, divisor);
3073 case TTF_MODE_QNOVO:
3074 soc_per_step = 100 / MAX_CC_STEPS;
3075 for (i = msoc / soc_per_step; i < MAX_CC_STEPS - 1; ++i) {
3076 msoc_next_step = (i + 1) * soc_per_step;
3077 if (i == msoc / soc_per_step)
3078 msoc_this_step = msoc;
3080 msoc_this_step = i * soc_per_step;
3082 /* scale ibatt by 85% to account for discharge pulses */
3083 ibatt_this_step = min(
3084 chip->ttf.cc_step.arr[i] / MILLI_UNIT,
3085 ibatt_avg) * 85 / 100;
3086 divisor = max(100, ibatt_this_step * 100);
3087 t_predicted_this_step = div_s64((s64)act_cap_mah *
3088 (msoc_next_step - msoc_this_step) *
3089 HOURS_TO_SECONDS, divisor);
3090 t_predicted += t_predicted_this_step;
3091 fg_dbg(chip, FG_TTF, "[%d, %d] ma=%d t=%d\n",
3092 msoc_this_step, msoc_next_step,
3093 ibatt_this_step, t_predicted_this_step);
3097 pr_err("TTF mode %d is not supported\n", chip->ttf.mode);
3102 fg_dbg(chip, FG_TTF, "t_predicted_cc=%d\n", t_predicted);
3104 iterm = max(100, abs(chip->dt.sys_term_curr_ma) + 200);
3105 fg_dbg(chip, FG_TTF, "iterm=%d\n", iterm);
3107 if (chip->charge_type == POWER_SUPPLY_CHARGE_TYPE_TAPER)
3108 tau = max(MILLI_UNIT, ibatt_avg * MILLI_UNIT / iterm);
3110 tau = max(MILLI_UNIT, i_cc2cv * MILLI_UNIT / iterm);
3112 rc = fg_lerp(fg_ln_table, ARRAY_SIZE(fg_ln_table), tau, &tau);
3114 pr_err("failed to interpolate tau rc=%d\n", rc);
3118 /* tau is scaled linearly from 95% to 100% SOC */
3120 tau = tau * 2 * (100 - msoc) / 10;
3122 fg_dbg(chip, FG_TTF, "tau=%d\n", tau);
3123 t_predicted_cv = div_s64((s64)act_cap_mah * rbatt * tau *
3124 HOURS_TO_SECONDS, NANO_UNIT);
3125 fg_dbg(chip, FG_TTF, "t_predicted_cv=%d\n", t_predicted_cv);
3126 t_predicted += t_predicted_cv;
3128 fg_dbg(chip, FG_TTF, "t_predicted_prefilter=%d\n", t_predicted);
3129 if (chip->ttf.last_ms != 0) {
3130 delta_ms = ktime_ms_delta(ktime_get_boottime(),
3131 ms_to_ktime(chip->ttf.last_ms));
3132 if (delta_ms > 10000) {
3133 ttf_slope = div64_s64(
3134 (s64)(t_predicted - chip->ttf.last_ttf) *
3135 MICRO_UNIT, delta_ms);
3136 if (ttf_slope > -100)
3138 else if (ttf_slope < -2000)
3141 t_predicted = div_s64(
3142 (s64)ttf_slope * delta_ms, MICRO_UNIT) +
3144 fg_dbg(chip, FG_TTF, "ttf_slope=%d\n", ttf_slope);
3146 t_predicted = chip->ttf.last_ttf;
3150 /* clamp the ttf to 0 */
3151 if (t_predicted < 0)
3154 fg_dbg(chip, FG_TTF, "t_predicted_postfilter=%d\n", t_predicted);
3159 static int fg_get_time_to_full(struct fg_chip *chip, int *val)
3163 mutex_lock(&chip->ttf.lock);
3164 rc = fg_get_time_to_full_locked(chip, val);
3165 mutex_unlock(&chip->ttf.lock);
3169 #define CENTI_ICORRECT_C0 105
3170 #define CENTI_ICORRECT_C1 20
3171 static int fg_get_time_to_empty(struct fg_chip *chip, int *val)
3173 int rc, ibatt_avg, msoc, full_soc, act_cap_mah, divisor;
3175 rc = fg_circ_buf_median(&chip->ttf.ibatt, &ibatt_avg);
3177 /* try to get instantaneous current */
3178 rc = fg_get_battery_current(chip, &ibatt_avg);
3180 pr_err("failed to get battery current, rc=%d\n", rc);
3185 ibatt_avg /= MILLI_UNIT;
3186 /* clamp ibatt_avg to 100mA */
3187 if (ibatt_avg < 100)
3190 rc = fg_get_prop_capacity(chip, &msoc);
3192 pr_err("Error in getting capacity, rc=%d\n", rc);
3196 rc = fg_get_sram_prop(chip, FG_SRAM_ACT_BATT_CAP, &act_cap_mah);
3198 pr_err("Error in getting ACT_BATT_CAP, rc=%d\n", rc);
3202 rc = fg_get_sram_prop(chip, FG_SRAM_FULL_SOC, &full_soc);
3204 pr_err("failed to get full soc rc=%d\n", rc);
3207 full_soc = DIV_ROUND_CLOSEST(((u16)full_soc >> 8) * FULL_CAPACITY,
3209 act_cap_mah = full_soc * act_cap_mah / 100;
3211 divisor = CENTI_ICORRECT_C0 * 100 + CENTI_ICORRECT_C1 * msoc;
3212 divisor = ibatt_avg * divisor / 100;
3213 divisor = max(100, divisor);
3214 *val = act_cap_mah * msoc * HOURS_TO_SECONDS / divisor;
3218 static int fg_update_maint_soc(struct fg_chip *chip)
3222 if (!chip->dt.linearize_soc)
3225 mutex_lock(&chip->charge_full_lock);
3226 if (chip->delta_soc <= 0)
3229 rc = fg_get_msoc(chip, &msoc);
3231 pr_err("Error in getting msoc, rc=%d\n", rc);
3235 if (msoc > chip->maint_soc) {
3237 * When the monotonic SOC goes above maintenance SOC, we should
3238 * stop showing the maintenance SOC.
3240 chip->delta_soc = 0;
3241 chip->maint_soc = 0;
3242 } else if (msoc <= chip->last_msoc) {
3243 /* MSOC is decreasing. Decrease maintenance SOC as well */
3244 chip->maint_soc -= 1;
3247 * Reduce the maintenance SOC additionally by 1 whenever
3248 * it crosses a SOC multiple of 10.
3250 chip->maint_soc -= 1;
3251 chip->delta_soc -= 1;
3255 fg_dbg(chip, FG_IRQ, "msoc: %d last_msoc: %d maint_soc: %d delta_soc: %d\n",
3256 msoc, chip->last_msoc, chip->maint_soc, chip->delta_soc);
3257 chip->last_msoc = msoc;
3259 mutex_unlock(&chip->charge_full_lock);
3263 static int fg_esr_validate(struct fg_chip *chip)
3268 if (chip->dt.esr_clamp_mohms <= 0)
3271 rc = fg_get_sram_prop(chip, FG_SRAM_ESR, &esr_uohms);
3273 pr_err("failed to get ESR, rc=%d\n", rc);
3277 if (esr_uohms >= chip->dt.esr_clamp_mohms * 1000) {
3278 pr_debug("ESR %d is > ESR_clamp\n", esr_uohms);
3282 esr_uohms = chip->dt.esr_clamp_mohms * 1000;
3283 fg_encode(chip->sp, FG_SRAM_ESR, esr_uohms, buf);
3284 rc = fg_sram_write(chip, chip->sp[FG_SRAM_ESR].addr_word,
3285 chip->sp[FG_SRAM_ESR].addr_byte, buf,
3286 chip->sp[FG_SRAM_ESR].len, FG_IMA_DEFAULT);
3288 pr_err("Error in writing ESR, rc=%d\n", rc);
3292 fg_dbg(chip, FG_STATUS, "ESR clamped to %duOhms\n", esr_uohms);
3296 static int fg_force_esr_meas(struct fg_chip *chip)
3301 mutex_lock(&chip->qnovo_esr_ctrl_lock);
3302 /* force esr extraction enable */
3303 rc = fg_sram_masked_write(chip, ESR_EXTRACTION_ENABLE_WORD,
3304 ESR_EXTRACTION_ENABLE_OFFSET, BIT(0), BIT(0),
3307 pr_err("failed to enable esr extn rc=%d\n", rc);
3311 rc = fg_masked_write(chip, BATT_INFO_QNOVO_CFG(chip),
3312 LD_REG_CTRL_BIT, 0);
3314 pr_err("Error in configuring qnovo_cfg rc=%d\n", rc);
3318 rc = fg_masked_write(chip, BATT_INFO_TM_MISC1(chip),
3319 ESR_REQ_CTL_BIT | ESR_REQ_CTL_EN_BIT,
3320 ESR_REQ_CTL_BIT | ESR_REQ_CTL_EN_BIT);
3322 pr_err("Error in configuring force ESR rc=%d\n", rc);
3327 * Release and grab the lock again after 1.5 seconds so that prepare
3328 * callback can succeed if the request comes in between.
3330 mutex_unlock(&chip->qnovo_esr_ctrl_lock);
3332 /* wait 1.5 seconds for hw to measure ESR */
3335 mutex_lock(&chip->qnovo_esr_ctrl_lock);
3336 rc = fg_masked_write(chip, BATT_INFO_TM_MISC1(chip),
3337 ESR_REQ_CTL_BIT | ESR_REQ_CTL_EN_BIT,
3340 pr_err("Error in restoring force ESR rc=%d\n", rc);
3344 /* If qnovo is disabled, then leave ESR extraction enabled */
3345 if (!chip->qnovo_enable)
3348 rc = fg_masked_write(chip, BATT_INFO_QNOVO_CFG(chip),
3349 LD_REG_CTRL_BIT, LD_REG_CTRL_BIT);
3351 pr_err("Error in restoring qnovo_cfg rc=%d\n", rc);
3355 /* force esr extraction disable */
3356 rc = fg_sram_masked_write(chip, ESR_EXTRACTION_ENABLE_WORD,
3357 ESR_EXTRACTION_ENABLE_OFFSET, BIT(0), 0,
3360 pr_err("failed to disable esr extn rc=%d\n", rc);
3365 fg_get_battery_resistance(chip, &esr_uohms);
3366 fg_dbg(chip, FG_STATUS, "ESR uohms = %d\n", esr_uohms);
3368 mutex_unlock(&chip->qnovo_esr_ctrl_lock);
3372 static int fg_prepare_for_qnovo(struct fg_chip *chip, int qnovo_enable)
3376 mutex_lock(&chip->qnovo_esr_ctrl_lock);
3377 /* force esr extraction disable when qnovo enables */
3378 rc = fg_sram_masked_write(chip, ESR_EXTRACTION_ENABLE_WORD,
3379 ESR_EXTRACTION_ENABLE_OFFSET,
3380 BIT(0), qnovo_enable ? 0 : BIT(0),
3383 pr_err("Error in configuring esr extraction rc=%d\n", rc);
3387 rc = fg_masked_write(chip, BATT_INFO_QNOVO_CFG(chip),
3389 qnovo_enable ? LD_REG_CTRL_BIT : 0);
3391 pr_err("Error in configuring qnovo_cfg rc=%d\n", rc);
3395 fg_dbg(chip, FG_STATUS, "%s for Qnovo\n",
3396 qnovo_enable ? "Prepared" : "Unprepared");
3397 chip->qnovo_enable = qnovo_enable;
3399 mutex_unlock(&chip->qnovo_esr_ctrl_lock);
3403 static void ttf_work(struct work_struct *work)
3405 struct fg_chip *chip = container_of(work, struct fg_chip,
3407 int rc, ibatt_now, vbatt_now, ttf;
3410 mutex_lock(&chip->ttf.lock);
3411 if (chip->charge_status != POWER_SUPPLY_STATUS_CHARGING &&
3412 chip->charge_status != POWER_SUPPLY_STATUS_DISCHARGING)
3415 rc = fg_get_battery_current(chip, &ibatt_now);
3417 pr_err("failed to get battery current, rc=%d\n", rc);
3421 rc = fg_get_battery_voltage(chip, &vbatt_now);
3423 pr_err("failed to get battery voltage, rc=%d\n", rc);
3427 fg_circ_buf_add(&chip->ttf.ibatt, ibatt_now);
3428 fg_circ_buf_add(&chip->ttf.vbatt, vbatt_now);
3430 if (chip->charge_status == POWER_SUPPLY_STATUS_CHARGING) {
3431 rc = fg_get_time_to_full_locked(chip, &ttf);
3433 pr_err("failed to get ttf, rc=%d\n", rc);
3437 /* keep the wake lock and prime the IBATT and VBATT buffers */
3439 /* delay for one FG cycle */
3440 schedule_delayed_work(&chip->ttf_work,
3441 msecs_to_jiffies(1500));
3442 mutex_unlock(&chip->ttf.lock);
3446 /* update the TTF reference point every minute */
3447 ktime_now = ktime_get_boottime();
3448 if (ktime_ms_delta(ktime_now,
3449 ms_to_ktime(chip->ttf.last_ms)) > 60000 ||
3450 chip->ttf.last_ms == 0) {
3451 chip->ttf.last_ttf = ttf;
3452 chip->ttf.last_ms = ktime_to_ms(ktime_now);
3456 /* recurse every 10 seconds */
3457 schedule_delayed_work(&chip->ttf_work, msecs_to_jiffies(10000));
3459 vote(chip->awake_votable, TTF_PRIMING, false, 0);
3460 mutex_unlock(&chip->ttf.lock);
3463 /* PSY CALLBACKS STAY HERE */
3465 static int fg_psy_get_property(struct power_supply *psy,
3466 enum power_supply_property psp,
3467 union power_supply_propval *pval)
3469 struct fg_chip *chip = power_supply_get_drvdata(psy);
3473 case POWER_SUPPLY_PROP_CAPACITY:
3474 rc = fg_get_prop_capacity(chip, &pval->intval);
3476 case POWER_SUPPLY_PROP_CAPACITY_RAW:
3477 rc = fg_get_msoc_raw(chip, &pval->intval);
3479 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
3480 if (chip->battery_missing)
3481 pval->intval = 3700000;
3483 rc = fg_get_battery_voltage(chip, &pval->intval);
3485 case POWER_SUPPLY_PROP_CURRENT_NOW:
3486 rc = fg_get_battery_current(chip, &pval->intval);
3488 case POWER_SUPPLY_PROP_TEMP:
3489 rc = fg_get_battery_temp(chip, &pval->intval);
3491 case POWER_SUPPLY_PROP_COLD_TEMP:
3492 rc = fg_get_jeita_threshold(chip, JEITA_COLD, &pval->intval);
3494 pr_err("Error in reading jeita_cold, rc=%d\n", rc);
3498 case POWER_SUPPLY_PROP_COOL_TEMP:
3499 rc = fg_get_jeita_threshold(chip, JEITA_COOL, &pval->intval);
3501 pr_err("Error in reading jeita_cool, rc=%d\n", rc);
3505 case POWER_SUPPLY_PROP_WARM_TEMP:
3506 rc = fg_get_jeita_threshold(chip, JEITA_WARM, &pval->intval);
3508 pr_err("Error in reading jeita_warm, rc=%d\n", rc);
3512 case POWER_SUPPLY_PROP_HOT_TEMP:
3513 rc = fg_get_jeita_threshold(chip, JEITA_HOT, &pval->intval);
3515 pr_err("Error in reading jeita_hot, rc=%d\n", rc);
3519 case POWER_SUPPLY_PROP_RESISTANCE:
3520 rc = fg_get_battery_resistance(chip, &pval->intval);
3522 case POWER_SUPPLY_PROP_VOLTAGE_OCV:
3523 rc = fg_get_sram_prop(chip, FG_SRAM_OCV, &pval->intval);
3525 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
3526 pval->intval = chip->cl.nom_cap_uah;
3528 case POWER_SUPPLY_PROP_RESISTANCE_ID:
3529 pval->intval = chip->batt_id_ohms;
3531 case POWER_SUPPLY_PROP_BATTERY_TYPE:
3532 pval->strval = fg_get_battery_type(chip);
3534 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
3535 pval->intval = chip->bp.float_volt_uv;
3537 case POWER_SUPPLY_PROP_CYCLE_COUNT:
3538 pval->intval = fg_get_cycle_count(chip);
3540 case POWER_SUPPLY_PROP_CYCLE_COUNT_ID:
3541 pval->intval = chip->cyc_ctr.id;
3543 case POWER_SUPPLY_PROP_CHARGE_NOW_RAW:
3544 rc = fg_get_charge_raw(chip, &pval->intval);
3546 case POWER_SUPPLY_PROP_CHARGE_NOW:
3547 pval->intval = chip->cl.init_cc_uah;
3549 case POWER_SUPPLY_PROP_CHARGE_FULL:
3550 pval->intval = chip->cl.learned_cc_uah;
3552 case POWER_SUPPLY_PROP_CHARGE_COUNTER:
3553 rc = fg_get_charge_counter(chip, &pval->intval);
3555 case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
3556 rc = fg_get_time_to_full(chip, &pval->intval);
3558 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
3559 rc = fg_get_time_to_empty(chip, &pval->intval);
3561 case POWER_SUPPLY_PROP_SOC_REPORTING_READY:
3562 pval->intval = chip->soc_reporting_ready;
3564 case POWER_SUPPLY_PROP_DEBUG_BATTERY:
3565 pval->intval = is_debug_batt_id(chip);
3567 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
3568 rc = fg_get_sram_prop(chip, FG_SRAM_VBATT_FULL, &pval->intval);
3570 case POWER_SUPPLY_PROP_CC_STEP:
3571 if ((chip->ttf.cc_step.sel >= 0) &&
3572 (chip->ttf.cc_step.sel < MAX_CC_STEPS)) {
3574 chip->ttf.cc_step.arr[chip->ttf.cc_step.sel];
3576 pr_err("cc_step_sel is out of bounds [0, %d]\n",
3577 chip->ttf.cc_step.sel);
3581 case POWER_SUPPLY_PROP_CC_STEP_SEL:
3582 pval->intval = chip->ttf.cc_step.sel;
3585 pr_err("unsupported property %d\n", psp);
3596 static int fg_psy_set_property(struct power_supply *psy,
3597 enum power_supply_property psp,
3598 const union power_supply_propval *pval)
3600 struct fg_chip *chip = power_supply_get_drvdata(psy);
3604 case POWER_SUPPLY_PROP_CYCLE_COUNT_ID:
3605 if ((pval->intval > 0) && (pval->intval <= BUCKET_COUNT)) {
3606 chip->cyc_ctr.id = pval->intval;
3608 pr_err("rejecting invalid cycle_count_id = %d\n",
3613 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
3614 rc = fg_set_constant_chg_voltage(chip, pval->intval);
3616 case POWER_SUPPLY_PROP_RESISTANCE:
3617 rc = fg_force_esr_meas(chip);
3619 case POWER_SUPPLY_PROP_CHARGE_QNOVO_ENABLE:
3620 rc = fg_prepare_for_qnovo(chip, pval->intval);
3622 case POWER_SUPPLY_PROP_CC_STEP:
3623 if ((chip->ttf.cc_step.sel >= 0) &&
3624 (chip->ttf.cc_step.sel < MAX_CC_STEPS)) {
3625 chip->ttf.cc_step.arr[chip->ttf.cc_step.sel] =
3628 pr_err("cc_step_sel is out of bounds [0, %d]\n",
3629 chip->ttf.cc_step.sel);
3633 case POWER_SUPPLY_PROP_CC_STEP_SEL:
3634 if ((pval->intval >= 0) && (pval->intval < MAX_CC_STEPS)) {
3635 chip->ttf.cc_step.sel = pval->intval;
3637 pr_err("cc_step_sel is out of bounds [0, %d]\n",
3642 case POWER_SUPPLY_PROP_CHARGE_FULL:
3643 if (chip->cl.active) {
3644 pr_warn("Capacity learning active!\n");
3647 if (pval->intval <= 0 || pval->intval > chip->cl.nom_cap_uah) {
3648 pr_err("charge_full is out of bounds\n");
3651 chip->cl.learned_cc_uah = pval->intval;
3652 rc = fg_save_learned_cap_to_sram(chip);
3654 pr_err("Error in saving learned_cc_uah, rc=%d\n", rc);
3656 case POWER_SUPPLY_PROP_COLD_TEMP:
3657 rc = fg_set_jeita_threshold(chip, JEITA_COLD, pval->intval);
3659 pr_err("Error in writing jeita_cold, rc=%d\n", rc);
3663 case POWER_SUPPLY_PROP_COOL_TEMP:
3664 rc = fg_set_jeita_threshold(chip, JEITA_COOL, pval->intval);
3666 pr_err("Error in writing jeita_cool, rc=%d\n", rc);
3670 case POWER_SUPPLY_PROP_WARM_TEMP:
3671 rc = fg_set_jeita_threshold(chip, JEITA_WARM, pval->intval);
3673 pr_err("Error in writing jeita_warm, rc=%d\n", rc);
3677 case POWER_SUPPLY_PROP_HOT_TEMP:
3678 rc = fg_set_jeita_threshold(chip, JEITA_HOT, pval->intval);
3680 pr_err("Error in writing jeita_hot, rc=%d\n", rc);
3691 static int fg_property_is_writeable(struct power_supply *psy,
3692 enum power_supply_property psp)
3695 case POWER_SUPPLY_PROP_CYCLE_COUNT_ID:
3696 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
3697 case POWER_SUPPLY_PROP_CC_STEP:
3698 case POWER_SUPPLY_PROP_CC_STEP_SEL:
3699 case POWER_SUPPLY_PROP_CHARGE_FULL:
3700 case POWER_SUPPLY_PROP_COLD_TEMP:
3701 case POWER_SUPPLY_PROP_COOL_TEMP:
3702 case POWER_SUPPLY_PROP_WARM_TEMP:
3703 case POWER_SUPPLY_PROP_HOT_TEMP:
3712 static void fg_external_power_changed(struct power_supply *psy)
3714 pr_debug("power supply changed\n");
3717 static int fg_notifier_cb(struct notifier_block *nb,
3718 unsigned long event, void *data)
3720 struct power_supply *psy = data;
3721 struct fg_chip *chip = container_of(nb, struct fg_chip, nb);
3723 if (event != PSY_EVENT_PROP_CHANGED)
3726 if (work_pending(&chip->status_change_work))
3729 if ((strcmp(psy->desc->name, "battery") == 0)
3730 || (strcmp(psy->desc->name, "usb") == 0)) {
3732 * We cannot vote for awake votable here as that takes
3733 * a mutex lock and this is executed in an atomic context.
3735 pm_stay_awake(chip->dev);
3736 schedule_work(&chip->status_change_work);
3742 static enum power_supply_property fg_psy_props[] = {
3743 POWER_SUPPLY_PROP_CAPACITY,
3744 POWER_SUPPLY_PROP_CAPACITY_RAW,
3745 POWER_SUPPLY_PROP_TEMP,
3746 POWER_SUPPLY_PROP_COLD_TEMP,
3747 POWER_SUPPLY_PROP_COOL_TEMP,
3748 POWER_SUPPLY_PROP_WARM_TEMP,
3749 POWER_SUPPLY_PROP_HOT_TEMP,
3750 POWER_SUPPLY_PROP_VOLTAGE_NOW,
3751 POWER_SUPPLY_PROP_VOLTAGE_OCV,
3752 POWER_SUPPLY_PROP_CURRENT_NOW,
3753 POWER_SUPPLY_PROP_RESISTANCE_ID,
3754 POWER_SUPPLY_PROP_RESISTANCE,
3755 POWER_SUPPLY_PROP_BATTERY_TYPE,
3756 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
3757 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
3758 POWER_SUPPLY_PROP_CYCLE_COUNT,
3759 POWER_SUPPLY_PROP_CYCLE_COUNT_ID,
3760 POWER_SUPPLY_PROP_CHARGE_NOW_RAW,
3761 POWER_SUPPLY_PROP_CHARGE_NOW,
3762 POWER_SUPPLY_PROP_CHARGE_FULL,
3763 POWER_SUPPLY_PROP_CHARGE_COUNTER,
3764 POWER_SUPPLY_PROP_TIME_TO_FULL_AVG,
3765 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
3766 POWER_SUPPLY_PROP_SOC_REPORTING_READY,
3767 POWER_SUPPLY_PROP_DEBUG_BATTERY,
3768 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
3769 POWER_SUPPLY_PROP_CC_STEP,
3770 POWER_SUPPLY_PROP_CC_STEP_SEL,
3773 static const struct power_supply_desc fg_psy_desc = {
3775 .type = POWER_SUPPLY_TYPE_BMS,
3776 .properties = fg_psy_props,
3777 .num_properties = ARRAY_SIZE(fg_psy_props),
3778 .get_property = fg_psy_get_property,
3779 .set_property = fg_psy_set_property,
3780 .external_power_changed = fg_external_power_changed,
3781 .property_is_writeable = fg_property_is_writeable,
3784 /* INIT FUNCTIONS STAY HERE */
3786 #define DEFAULT_ESR_CHG_TIMER_RETRY 8
3787 #define DEFAULT_ESR_CHG_TIMER_MAX 16
3788 static int fg_hw_init(struct fg_chip *chip)
3793 fg_encode(chip->sp, FG_SRAM_CUTOFF_VOLT, chip->dt.cutoff_volt_mv, buf);
3794 rc = fg_sram_write(chip, chip->sp[FG_SRAM_CUTOFF_VOLT].addr_word,
3795 chip->sp[FG_SRAM_CUTOFF_VOLT].addr_byte, buf,
3796 chip->sp[FG_SRAM_CUTOFF_VOLT].len, FG_IMA_DEFAULT);
3798 pr_err("Error in writing cutoff_volt, rc=%d\n", rc);
3802 fg_encode(chip->sp, FG_SRAM_EMPTY_VOLT, chip->dt.empty_volt_mv, buf);
3803 rc = fg_sram_write(chip, chip->sp[FG_SRAM_EMPTY_VOLT].addr_word,
3804 chip->sp[FG_SRAM_EMPTY_VOLT].addr_byte, buf,
3805 chip->sp[FG_SRAM_EMPTY_VOLT].len, FG_IMA_DEFAULT);
3807 pr_err("Error in writing empty_volt, rc=%d\n", rc);
3811 fg_encode(chip->sp, FG_SRAM_CHG_TERM_CURR, chip->dt.chg_term_curr_ma,
3813 rc = fg_sram_write(chip, chip->sp[FG_SRAM_CHG_TERM_CURR].addr_word,
3814 chip->sp[FG_SRAM_CHG_TERM_CURR].addr_byte, buf,
3815 chip->sp[FG_SRAM_CHG_TERM_CURR].len, FG_IMA_DEFAULT);
3817 pr_err("Error in writing chg_term_curr, rc=%d\n", rc);
3821 fg_encode(chip->sp, FG_SRAM_SYS_TERM_CURR, chip->dt.sys_term_curr_ma,
3823 rc = fg_sram_write(chip, chip->sp[FG_SRAM_SYS_TERM_CURR].addr_word,
3824 chip->sp[FG_SRAM_SYS_TERM_CURR].addr_byte, buf,
3825 chip->sp[FG_SRAM_SYS_TERM_CURR].len, FG_IMA_DEFAULT);
3827 pr_err("Error in writing sys_term_curr, rc=%d\n", rc);
3831 if (!(chip->wa_flags & PMI8998_V1_REV_WA)) {
3832 fg_encode(chip->sp, FG_SRAM_CHG_TERM_BASE_CURR,
3833 chip->dt.chg_term_base_curr_ma, buf);
3834 rc = fg_sram_write(chip,
3835 chip->sp[FG_SRAM_CHG_TERM_BASE_CURR].addr_word,
3836 chip->sp[FG_SRAM_CHG_TERM_BASE_CURR].addr_byte,
3837 buf, chip->sp[FG_SRAM_CHG_TERM_BASE_CURR].len,
3840 pr_err("Error in writing chg_term_base_curr, rc=%d\n",
3846 if (chip->dt.vbatt_low_thr_mv > 0) {
3847 fg_encode(chip->sp, FG_SRAM_VBATT_LOW,
3848 chip->dt.vbatt_low_thr_mv, buf);
3849 rc = fg_sram_write(chip, chip->sp[FG_SRAM_VBATT_LOW].addr_word,
3850 chip->sp[FG_SRAM_VBATT_LOW].addr_byte, buf,
3851 chip->sp[FG_SRAM_VBATT_LOW].len,
3854 pr_err("Error in writing vbatt_low_thr, rc=%d\n", rc);
3859 if (chip->dt.delta_soc_thr > 0 && chip->dt.delta_soc_thr < 100) {
3860 fg_encode(chip->sp, FG_SRAM_DELTA_MSOC_THR,
3861 chip->dt.delta_soc_thr, buf);
3862 rc = fg_sram_write(chip,
3863 chip->sp[FG_SRAM_DELTA_MSOC_THR].addr_word,
3864 chip->sp[FG_SRAM_DELTA_MSOC_THR].addr_byte,
3865 buf, chip->sp[FG_SRAM_DELTA_MSOC_THR].len,
3868 pr_err("Error in writing delta_msoc_thr, rc=%d\n", rc);
3872 fg_encode(chip->sp, FG_SRAM_DELTA_BSOC_THR,
3873 chip->dt.delta_soc_thr, buf);
3874 rc = fg_sram_write(chip,
3875 chip->sp[FG_SRAM_DELTA_BSOC_THR].addr_word,
3876 chip->sp[FG_SRAM_DELTA_BSOC_THR].addr_byte,
3877 buf, chip->sp[FG_SRAM_DELTA_BSOC_THR].len,
3880 pr_err("Error in writing delta_bsoc_thr, rc=%d\n", rc);
3886 * configure battery thermal coefficients c1,c2,c3
3887 * if its value is not zero.
3889 if (chip->dt.batt_therm_coeffs[0] > 0) {
3890 rc = fg_write(chip, BATT_INFO_THERM_C1(chip),
3891 chip->dt.batt_therm_coeffs, BATT_THERM_NUM_COEFFS);
3893 pr_err("Error in writing battery thermal coefficients, rc=%d\n",
3900 if (chip->dt.recharge_soc_thr > 0 && chip->dt.recharge_soc_thr < 100) {
3901 rc = fg_set_recharge_soc(chip, chip->dt.recharge_soc_thr);
3903 pr_err("Error in setting recharge_soc, rc=%d\n", rc);
3908 if (chip->dt.recharge_volt_thr_mv > 0) {
3909 rc = fg_set_recharge_voltage(chip,
3910 chip->dt.recharge_volt_thr_mv);
3912 pr_err("Error in setting recharge_voltage, rc=%d\n",
3918 if (chip->dt.rsense_sel >= SRC_SEL_BATFET &&
3919 chip->dt.rsense_sel < SRC_SEL_RESERVED) {
3920 rc = fg_masked_write(chip, BATT_INFO_IBATT_SENSING_CFG(chip),
3921 SOURCE_SELECT_MASK, chip->dt.rsense_sel);
3923 pr_err("Error in writing rsense_sel, rc=%d\n", rc);
3928 rc = fg_set_jeita_threshold(chip, JEITA_COLD,
3929 chip->dt.jeita_thresholds[JEITA_COLD] * 10);
3931 pr_err("Error in writing jeita_cold, rc=%d\n", rc);
3935 rc = fg_set_jeita_threshold(chip, JEITA_COOL,
3936 chip->dt.jeita_thresholds[JEITA_COOL] * 10);
3938 pr_err("Error in writing jeita_cool, rc=%d\n", rc);
3942 rc = fg_set_jeita_threshold(chip, JEITA_WARM,
3943 chip->dt.jeita_thresholds[JEITA_WARM] * 10);
3945 pr_err("Error in writing jeita_warm, rc=%d\n", rc);
3949 rc = fg_set_jeita_threshold(chip, JEITA_HOT,
3950 chip->dt.jeita_thresholds[JEITA_HOT] * 10);
3952 pr_err("Error in writing jeita_hot, rc=%d\n", rc);
3956 if (chip->pmic_rev_id->pmic_subtype == PMI8998_SUBTYPE) {
3957 chip->esr_timer_charging_default[TIMER_RETRY] =
3958 DEFAULT_ESR_CHG_TIMER_RETRY;
3959 chip->esr_timer_charging_default[TIMER_MAX] =
3960 DEFAULT_ESR_CHG_TIMER_MAX;
3962 /* We don't need this for pm660 at present */
3963 chip->esr_timer_charging_default[TIMER_RETRY] = -EINVAL;
3964 chip->esr_timer_charging_default[TIMER_MAX] = -EINVAL;
3967 rc = fg_set_esr_timer(chip, chip->dt.esr_timer_charging[TIMER_RETRY],
3968 chip->dt.esr_timer_charging[TIMER_MAX], true, FG_IMA_DEFAULT);
3970 pr_err("Error in setting ESR timer, rc=%d\n", rc);
3974 rc = fg_set_esr_timer(chip, chip->dt.esr_timer_awake[TIMER_RETRY],
3975 chip->dt.esr_timer_awake[TIMER_MAX], false, FG_IMA_DEFAULT);
3977 pr_err("Error in setting ESR timer, rc=%d\n", rc);
3981 restore_cycle_counter(chip);
3983 if (chip->dt.jeita_hyst_temp >= 0) {
3984 val = chip->dt.jeita_hyst_temp << JEITA_TEMP_HYST_SHIFT;
3985 rc = fg_masked_write(chip, BATT_INFO_BATT_TEMP_CFG(chip),
3986 JEITA_TEMP_HYST_MASK, val);
3988 pr_err("Error in writing batt_temp_cfg, rc=%d\n", rc);
3993 get_batt_temp_delta(chip->dt.batt_temp_delta, &val);
3994 rc = fg_masked_write(chip, BATT_INFO_BATT_TMPR_INTR(chip),
3995 CHANGE_THOLD_MASK, val);
3997 pr_err("Error in writing batt_temp_delta, rc=%d\n", rc);
4001 rc = fg_rconn_config(chip);
4003 pr_err("Error in configuring Rconn, rc=%d\n", rc);
4007 fg_encode(chip->sp, FG_SRAM_ESR_TIGHT_FILTER,
4008 chip->dt.esr_tight_flt_upct, buf);
4009 rc = fg_sram_write(chip, chip->sp[FG_SRAM_ESR_TIGHT_FILTER].addr_word,
4010 chip->sp[FG_SRAM_ESR_TIGHT_FILTER].addr_byte, buf,
4011 chip->sp[FG_SRAM_ESR_TIGHT_FILTER].len, FG_IMA_DEFAULT);
4013 pr_err("Error in writing ESR tight filter, rc=%d\n", rc);
4017 fg_encode(chip->sp, FG_SRAM_ESR_BROAD_FILTER,
4018 chip->dt.esr_broad_flt_upct, buf);
4019 rc = fg_sram_write(chip, chip->sp[FG_SRAM_ESR_BROAD_FILTER].addr_word,
4020 chip->sp[FG_SRAM_ESR_BROAD_FILTER].addr_byte, buf,
4021 chip->sp[FG_SRAM_ESR_BROAD_FILTER].len, FG_IMA_DEFAULT);
4023 pr_err("Error in writing ESR broad filter, rc=%d\n", rc);
4027 fg_encode(chip->sp, FG_SRAM_ESR_PULSE_THRESH,
4028 chip->dt.esr_pulse_thresh_ma, buf);
4029 rc = fg_sram_write(chip, chip->sp[FG_SRAM_ESR_PULSE_THRESH].addr_word,
4030 chip->sp[FG_SRAM_ESR_PULSE_THRESH].addr_byte, buf,
4031 chip->sp[FG_SRAM_ESR_PULSE_THRESH].len, FG_IMA_DEFAULT);
4033 pr_err("Error in writing esr_pulse_thresh_ma, rc=%d\n", rc);
4037 get_esr_meas_current(chip->dt.esr_meas_curr_ma, &val);
4038 rc = fg_masked_write(chip, BATT_INFO_ESR_PULL_DN_CFG(chip),
4039 ESR_PULL_DOWN_IVAL_MASK, val);
4041 pr_err("Error in writing esr_meas_curr_ma, rc=%d\n", rc);
4045 if (is_debug_batt_id(chip)) {
4046 val = ESR_NO_PULL_DOWN;
4047 rc = fg_masked_write(chip, BATT_INFO_ESR_PULL_DN_CFG(chip),
4048 ESR_PULL_DOWN_MODE_MASK, val);
4050 pr_err("Error in writing esr_pull_down, rc=%d\n", rc);
4058 static int fg_memif_init(struct fg_chip *chip)
4060 return fg_ima_init(chip);
4063 static int fg_adjust_timebase(struct fg_chip *chip)
4065 int rc = 0, die_temp;
4069 if ((chip->wa_flags & PM660_TSMC_OSC_WA) && chip->die_temp_chan) {
4070 rc = iio_read_channel_processed(chip->die_temp_chan, &die_temp);
4072 pr_err("Error in reading die_temp, rc:%d\n", rc);
4076 rc = fg_lerp(fg_tsmc_osc_table, ARRAY_SIZE(fg_tsmc_osc_table),
4077 die_temp / 1000, &time_base);
4079 pr_err("Error to lookup fg_tsmc_osc_table rc=%d\n", rc);
4083 fg_encode(chip->sp, FG_SRAM_TIMEBASE, time_base, buf);
4084 rc = fg_sram_write(chip,
4085 chip->sp[FG_SRAM_TIMEBASE].addr_word,
4086 chip->sp[FG_SRAM_TIMEBASE].addr_byte, buf,
4087 chip->sp[FG_SRAM_TIMEBASE].len, FG_IMA_DEFAULT);
4089 pr_err("Error in writing timebase, rc=%d\n", rc);
4097 /* INTERRUPT HANDLERS STAY HERE */
4099 static irqreturn_t fg_mem_xcp_irq_handler(int irq, void *data)
4101 struct fg_chip *chip = data;
4105 rc = fg_read(chip, MEM_IF_INT_RT_STS(chip), &status, 1);
4107 pr_err("failed to read addr=0x%04x, rc=%d\n",
4108 MEM_IF_INT_RT_STS(chip), rc);
4112 fg_dbg(chip, FG_IRQ, "irq %d triggered, status:%d\n", irq, status);
4114 mutex_lock(&chip->sram_rw_lock);
4115 rc = fg_clear_dma_errors_if_any(chip);
4117 pr_err("Error in clearing DMA error, rc=%d\n", rc);
4119 if (status & MEM_XCP_BIT) {
4120 rc = fg_clear_ima_errors_if_any(chip, true);
4121 if (rc < 0 && rc != -EAGAIN)
4122 pr_err("Error in checking IMA errors rc:%d\n", rc);
4125 mutex_unlock(&chip->sram_rw_lock);
4129 static irqreturn_t fg_vbatt_low_irq_handler(int irq, void *data)
4131 struct fg_chip *chip = data;
4133 fg_dbg(chip, FG_IRQ, "irq %d triggered\n", irq);
4137 static irqreturn_t fg_batt_missing_irq_handler(int irq, void *data)
4139 struct fg_chip *chip = data;
4143 rc = fg_read(chip, BATT_INFO_INT_RT_STS(chip), &status, 1);
4145 pr_err("failed to read addr=0x%04x, rc=%d\n",
4146 BATT_INFO_INT_RT_STS(chip), rc);
4150 fg_dbg(chip, FG_IRQ, "irq %d triggered sts:%d\n", irq, status);
4151 chip->battery_missing = (status & BT_MISS_BIT);
4153 if (chip->battery_missing) {
4154 chip->profile_available = false;
4155 chip->profile_loaded = false;
4156 chip->soc_reporting_ready = false;
4160 clear_battery_profile(chip);
4161 schedule_delayed_work(&chip->profile_load_work, 0);
4164 power_supply_changed(chip->fg_psy);
4169 static irqreturn_t fg_delta_batt_temp_irq_handler(int irq, void *data)
4171 struct fg_chip *chip = data;
4172 union power_supply_propval prop = {0, };
4175 fg_dbg(chip, FG_IRQ, "irq %d triggered\n", irq);
4176 rc = fg_get_battery_temp(chip, &batt_temp);
4178 pr_err("Error in getting batt_temp\n");
4182 rc = fg_esr_filter_config(chip, batt_temp);
4184 pr_err("Error in configuring ESR filter rc:%d\n", rc);
4186 rc = fg_slope_limit_config(chip, batt_temp);
4188 pr_err("Error in configuring slope limiter rc:%d\n", rc);
4190 rc = fg_adjust_ki_coeff_full_soc(chip, batt_temp);
4192 pr_err("Error in configuring ki_coeff_full_soc rc:%d\n", rc);
4194 if (!batt_psy_initialized(chip)) {
4195 chip->last_batt_temp = batt_temp;
4199 power_supply_get_property(chip->batt_psy, POWER_SUPPLY_PROP_HEALTH,
4201 chip->health = prop.intval;
4203 if (chip->last_batt_temp != batt_temp) {
4204 rc = fg_adjust_timebase(chip);
4206 pr_err("Error in adjusting timebase, rc=%d\n", rc);
4208 rc = fg_adjust_recharge_voltage(chip);
4210 pr_err("Error in adjusting recharge_voltage, rc=%d\n",
4213 chip->last_batt_temp = batt_temp;
4214 power_supply_changed(chip->batt_psy);
4217 if (abs(chip->last_batt_temp - batt_temp) > 30)
4218 pr_warn("Battery temperature last:%d current: %d\n",
4219 chip->last_batt_temp, batt_temp);
4223 static irqreturn_t fg_first_est_irq_handler(int irq, void *data)
4225 struct fg_chip *chip = data;
4227 fg_dbg(chip, FG_IRQ, "irq %d triggered\n", irq);
4228 complete_all(&chip->soc_ready);
4232 static irqreturn_t fg_soc_update_irq_handler(int irq, void *data)
4234 struct fg_chip *chip = data;
4236 fg_dbg(chip, FG_IRQ, "irq %d triggered\n", irq);
4237 complete_all(&chip->soc_update);
4241 static irqreturn_t fg_delta_bsoc_irq_handler(int irq, void *data)
4243 struct fg_chip *chip = data;
4246 fg_dbg(chip, FG_IRQ, "irq %d triggered\n", irq);
4247 rc = fg_charge_full_update(chip);
4249 pr_err("Error in charge_full_update, rc=%d\n", rc);
4254 static irqreturn_t fg_delta_msoc_irq_handler(int irq, void *data)
4256 struct fg_chip *chip = data;
4259 fg_dbg(chip, FG_IRQ, "irq %d triggered\n", irq);
4260 fg_cycle_counter_update(chip);
4262 if (chip->cl.active)
4263 fg_cap_learning_update(chip);
4265 rc = fg_charge_full_update(chip);
4267 pr_err("Error in charge_full_update, rc=%d\n", rc);
4269 rc = fg_adjust_ki_coeff_dischg(chip);
4271 pr_err("Error in adjusting ki_coeff_dischg, rc=%d\n", rc);
4273 rc = fg_update_maint_soc(chip);
4275 pr_err("Error in updating maint_soc, rc=%d\n", rc);
4277 rc = fg_esr_validate(chip);
4279 pr_err("Error in validating ESR, rc=%d\n", rc);
4281 rc = fg_adjust_timebase(chip);
4283 pr_err("Error in adjusting timebase, rc=%d\n", rc);
4285 if (batt_psy_initialized(chip))
4286 power_supply_changed(chip->batt_psy);
4291 static irqreturn_t fg_empty_soc_irq_handler(int irq, void *data)
4293 struct fg_chip *chip = data;
4295 fg_dbg(chip, FG_IRQ, "irq %d triggered\n", irq);
4296 if (batt_psy_initialized(chip))
4297 power_supply_changed(chip->batt_psy);
4302 static irqreturn_t fg_soc_irq_handler(int irq, void *data)
4304 struct fg_chip *chip = data;
4306 fg_dbg(chip, FG_IRQ, "irq %d triggered\n", irq);
4310 static irqreturn_t fg_dummy_irq_handler(int irq, void *data)
4312 pr_debug("irq %d triggered\n", irq);
4316 static struct fg_irq_info fg_irqs[FG_IRQ_MAX] = {
4319 .name = "msoc-full",
4320 .handler = fg_soc_irq_handler,
4323 .name = "msoc-high",
4324 .handler = fg_soc_irq_handler,
4327 [MSOC_EMPTY_IRQ] = {
4328 .name = "msoc-empty",
4329 .handler = fg_empty_soc_irq_handler,
4334 .handler = fg_soc_irq_handler,
4337 [MSOC_DELTA_IRQ] = {
4338 .name = "msoc-delta",
4339 .handler = fg_delta_msoc_irq_handler,
4342 [BSOC_DELTA_IRQ] = {
4343 .name = "bsoc-delta",
4344 .handler = fg_delta_bsoc_irq_handler,
4348 .name = "soc-ready",
4349 .handler = fg_first_est_irq_handler,
4352 [SOC_UPDATE_IRQ] = {
4353 .name = "soc-update",
4354 .handler = fg_soc_update_irq_handler,
4356 /* BATT_INFO irqs */
4357 [BATT_TEMP_DELTA_IRQ] = {
4358 .name = "batt-temp-delta",
4359 .handler = fg_delta_batt_temp_irq_handler,
4362 [BATT_MISSING_IRQ] = {
4363 .name = "batt-missing",
4364 .handler = fg_batt_missing_irq_handler,
4368 .name = "esr-delta",
4369 .handler = fg_dummy_irq_handler,
4372 .name = "vbatt-low",
4373 .handler = fg_vbatt_low_irq_handler,
4376 [VBATT_PRED_DELTA_IRQ] = {
4377 .name = "vbatt-pred-delta",
4378 .handler = fg_dummy_irq_handler,
4382 .name = "dma-grant",
4383 .handler = fg_dummy_irq_handler,
4387 .handler = fg_mem_xcp_irq_handler,
4391 .handler = fg_dummy_irq_handler,
4395 static int fg_get_irq_index_byname(const char *name)
4399 for (i = 0; i < ARRAY_SIZE(fg_irqs); i++) {
4400 if (strcmp(fg_irqs[i].name, name) == 0)
4404 pr_err("%s is not in irq list\n", name);
4408 static int fg_register_interrupts(struct fg_chip *chip)
4410 struct device_node *child, *node = chip->dev->of_node;
4411 struct property *prop;
4413 int rc, irq, irq_index;
4415 for_each_available_child_of_node(node, child) {
4416 of_property_for_each_string(child, "interrupt-names", prop,
4418 irq = of_irq_get_byname(child, name);
4420 dev_err(chip->dev, "failed to get irq %s irq:%d\n",
4425 irq_index = fg_get_irq_index_byname(name);
4429 rc = devm_request_threaded_irq(chip->dev, irq, NULL,
4430 fg_irqs[irq_index].handler,
4431 IRQF_ONESHOT, name, chip);
4433 dev_err(chip->dev, "failed to register irq handler for %s rc:%d\n",
4438 fg_irqs[irq_index].irq = irq;
4439 if (fg_irqs[irq_index].wakeable)
4440 enable_irq_wake(fg_irqs[irq_index].irq);
4447 static int fg_parse_dt_property_u32_array(struct device_node *node,
4448 const char *prop_name, int *buf, int len)
4452 rc = of_property_count_elems_of_size(node, prop_name, sizeof(u32));
4458 } else if (rc != len) {
4459 pr_err("Incorrect length %d for %s, rc=%d\n", len, prop_name,
4464 rc = of_property_read_u32_array(node, prop_name, buf, len);
4466 pr_err("Error in reading %s, rc=%d\n", prop_name, rc);
4473 static int fg_parse_slope_limit_coefficients(struct fg_chip *chip)
4475 struct device_node *node = chip->dev->of_node;
4478 rc = of_property_read_u32(node, "qcom,slope-limit-temp-threshold",
4479 &chip->dt.slope_limit_temp);
4483 rc = fg_parse_dt_property_u32_array(node, "qcom,slope-limit-coeffs",
4484 chip->dt.slope_limit_coeffs, SLOPE_LIMIT_NUM_COEFFS);
4488 for (i = 0; i < SLOPE_LIMIT_NUM_COEFFS; i++) {
4489 if (chip->dt.slope_limit_coeffs[i] > SLOPE_LIMIT_COEFF_MAX ||
4490 chip->dt.slope_limit_coeffs[i] < 0) {
4491 pr_err("Incorrect slope limit coefficient\n");
4496 chip->slope_limit_en = true;
4500 static int fg_parse_ki_coefficients(struct fg_chip *chip)
4502 struct device_node *node = chip->dev->of_node;
4505 rc = of_property_read_u32(node, "qcom,ki-coeff-full-dischg", &temp);
4507 chip->dt.ki_coeff_full_soc_dischg = temp;
4509 rc = fg_parse_dt_property_u32_array(node, "qcom,ki-coeff-soc-dischg",
4510 chip->dt.ki_coeff_soc, KI_COEFF_SOC_LEVELS);
4514 rc = fg_parse_dt_property_u32_array(node, "qcom,ki-coeff-med-dischg",
4515 chip->dt.ki_coeff_med_dischg, KI_COEFF_SOC_LEVELS);
4519 rc = fg_parse_dt_property_u32_array(node, "qcom,ki-coeff-hi-dischg",
4520 chip->dt.ki_coeff_hi_dischg, KI_COEFF_SOC_LEVELS);
4524 for (i = 0; i < KI_COEFF_SOC_LEVELS; i++) {
4525 if (chip->dt.ki_coeff_soc[i] < 0 ||
4526 chip->dt.ki_coeff_soc[i] > FULL_CAPACITY) {
4527 pr_err("Error in ki_coeff_soc_dischg values\n");
4531 if (chip->dt.ki_coeff_med_dischg[i] < 0 ||
4532 chip->dt.ki_coeff_med_dischg[i] > KI_COEFF_MAX) {
4533 pr_err("Error in ki_coeff_med_dischg values\n");
4537 if (chip->dt.ki_coeff_med_dischg[i] < 0 ||
4538 chip->dt.ki_coeff_med_dischg[i] > KI_COEFF_MAX) {
4539 pr_err("Error in ki_coeff_med_dischg values\n");
4543 chip->ki_coeff_dischg_en = true;
4547 #define DEFAULT_CUTOFF_VOLT_MV 3200
4548 #define DEFAULT_EMPTY_VOLT_MV 2850
4549 #define DEFAULT_RECHARGE_VOLT_MV 4250
4550 #define DEFAULT_CHG_TERM_CURR_MA 100
4551 #define DEFAULT_CHG_TERM_BASE_CURR_MA 75
4552 #define DEFAULT_SYS_TERM_CURR_MA -125
4553 #define DEFAULT_DELTA_SOC_THR 1
4554 #define DEFAULT_RECHARGE_SOC_THR 95
4555 #define DEFAULT_BATT_TEMP_COLD 0
4556 #define DEFAULT_BATT_TEMP_COOL 5
4557 #define DEFAULT_BATT_TEMP_WARM 45
4558 #define DEFAULT_BATT_TEMP_HOT 50
4559 #define DEFAULT_CL_START_SOC 15
4560 #define DEFAULT_CL_MIN_TEMP_DECIDEGC 150
4561 #define DEFAULT_CL_MAX_TEMP_DECIDEGC 450
4562 #define DEFAULT_CL_MAX_INC_DECIPERC 5
4563 #define DEFAULT_CL_MAX_DEC_DECIPERC 100
4564 #define DEFAULT_CL_MIN_LIM_DECIPERC 0
4565 #define DEFAULT_CL_MAX_LIM_DECIPERC 0
4566 #define BTEMP_DELTA_LOW 2
4567 #define BTEMP_DELTA_HIGH 10
4568 #define DEFAULT_ESR_FLT_TEMP_DECIDEGC 100
4569 #define DEFAULT_ESR_TIGHT_FLT_UPCT 3907
4570 #define DEFAULT_ESR_BROAD_FLT_UPCT 99610
4571 #define DEFAULT_ESR_TIGHT_LT_FLT_UPCT 48829
4572 #define DEFAULT_ESR_BROAD_LT_FLT_UPCT 148438
4573 #define DEFAULT_ESR_CLAMP_MOHMS 20
4574 #define DEFAULT_ESR_PULSE_THRESH_MA 110
4575 #define DEFAULT_ESR_MEAS_CURR_MA 120
4576 static int fg_parse_dt(struct fg_chip *chip)
4578 struct device_node *child, *revid_node, *node = chip->dev->of_node;
4584 dev_err(chip->dev, "device tree node missing\n");
4588 revid_node = of_parse_phandle(node, "qcom,pmic-revid", 0);
4590 pr_err("Missing qcom,pmic-revid property - driver failed\n");
4594 chip->pmic_rev_id = get_revid_data(revid_node);
4595 if (IS_ERR_OR_NULL(chip->pmic_rev_id)) {
4596 pr_err("Unable to get pmic_revid rc=%ld\n",
4597 PTR_ERR(chip->pmic_rev_id));
4599 * the revid peripheral must be registered, any failure
4600 * here only indicates that the rev-id module has not
4603 return -EPROBE_DEFER;
4606 pr_debug("PMIC subtype %d Digital major %d\n",
4607 chip->pmic_rev_id->pmic_subtype, chip->pmic_rev_id->rev4);
4609 switch (chip->pmic_rev_id->pmic_subtype) {
4610 case PMI8998_SUBTYPE:
4611 if (chip->pmic_rev_id->rev4 < PMI8998_V2P0_REV4) {
4612 chip->sp = pmi8998_v1_sram_params;
4613 chip->alg_flags = pmi8998_v1_alg_flags;
4614 chip->wa_flags |= PMI8998_V1_REV_WA;
4615 } else if (chip->pmic_rev_id->rev4 == PMI8998_V2P0_REV4) {
4616 chip->sp = pmi8998_v2_sram_params;
4617 chip->alg_flags = pmi8998_v2_alg_flags;
4623 chip->sp = pmi8998_v2_sram_params;
4624 chip->alg_flags = pmi8998_v2_alg_flags;
4625 chip->use_ima_single_mode = true;
4626 if (chip->pmic_rev_id->fab_id == PM660_FAB_ID_TSMC)
4627 chip->wa_flags |= PM660_TSMC_OSC_WA;
4633 if (of_get_available_child_count(node) == 0) {
4634 dev_err(chip->dev, "No child nodes specified!\n");
4638 for_each_available_child_of_node(node, child) {
4639 rc = of_property_read_u32(child, "reg", &base);
4641 dev_err(chip->dev, "reg not specified in node %s, rc=%d\n",
4642 child->full_name, rc);
4646 rc = fg_read(chip, base + PERPH_SUBTYPE_REG, &subtype, 1);
4648 dev_err(chip->dev, "Couldn't read subtype for base %d, rc=%d\n",
4654 case FG_BATT_SOC_PMI8998:
4655 chip->batt_soc_base = base;
4657 case FG_BATT_INFO_PMI8998:
4658 chip->batt_info_base = base;
4660 case FG_MEM_INFO_PMI8998:
4661 chip->mem_if_base = base;
4664 dev_err(chip->dev, "Invalid peripheral subtype 0x%x\n",
4670 rc = of_property_read_u32(node, "qcom,rradc-base", &base);
4672 dev_err(chip->dev, "rradc-base not specified, rc=%d\n", rc);
4675 chip->rradc_base = base;
4677 /* Read all the optional properties below */
4678 rc = of_property_read_u32(node, "qcom,fg-cutoff-voltage", &temp);
4680 chip->dt.cutoff_volt_mv = DEFAULT_CUTOFF_VOLT_MV;
4682 chip->dt.cutoff_volt_mv = temp;
4684 rc = of_property_read_u32(node, "qcom,fg-empty-voltage", &temp);
4686 chip->dt.empty_volt_mv = DEFAULT_EMPTY_VOLT_MV;
4688 chip->dt.empty_volt_mv = temp;
4690 rc = of_property_read_u32(node, "qcom,fg-vbatt-low-thr", &temp);
4692 chip->dt.vbatt_low_thr_mv = -EINVAL;
4694 chip->dt.vbatt_low_thr_mv = temp;
4696 rc = of_property_read_u32(node, "qcom,fg-chg-term-current", &temp);
4698 chip->dt.chg_term_curr_ma = DEFAULT_CHG_TERM_CURR_MA;
4700 chip->dt.chg_term_curr_ma = temp;
4702 rc = of_property_read_u32(node, "qcom,fg-sys-term-current", &temp);
4704 chip->dt.sys_term_curr_ma = DEFAULT_SYS_TERM_CURR_MA;
4706 chip->dt.sys_term_curr_ma = temp;
4708 rc = of_property_read_u32(node, "qcom,fg-chg-term-base-current", &temp);
4710 chip->dt.chg_term_base_curr_ma = DEFAULT_CHG_TERM_BASE_CURR_MA;
4712 chip->dt.chg_term_base_curr_ma = temp;
4714 rc = of_property_read_u32(node, "qcom,fg-delta-soc-thr", &temp);
4716 chip->dt.delta_soc_thr = DEFAULT_DELTA_SOC_THR;
4718 chip->dt.delta_soc_thr = temp;
4720 rc = of_property_read_u32(node, "qcom,fg-recharge-soc-thr", &temp);
4722 chip->dt.recharge_soc_thr = DEFAULT_RECHARGE_SOC_THR;
4724 chip->dt.recharge_soc_thr = temp;
4726 rc = of_property_read_u32(node, "qcom,fg-recharge-voltage", &temp);
4728 chip->dt.recharge_volt_thr_mv = DEFAULT_RECHARGE_VOLT_MV;
4730 chip->dt.recharge_volt_thr_mv = temp;
4732 chip->dt.auto_recharge_soc = of_property_read_bool(node,
4733 "qcom,fg-auto-recharge-soc");
4735 rc = of_property_read_u32(node, "qcom,fg-rsense-sel", &temp);
4737 chip->dt.rsense_sel = SRC_SEL_BATFET_SMB;
4739 chip->dt.rsense_sel = (u8)temp & SOURCE_SELECT_MASK;
4741 chip->dt.jeita_thresholds[JEITA_COLD] = DEFAULT_BATT_TEMP_COLD;
4742 chip->dt.jeita_thresholds[JEITA_COOL] = DEFAULT_BATT_TEMP_COOL;
4743 chip->dt.jeita_thresholds[JEITA_WARM] = DEFAULT_BATT_TEMP_WARM;
4744 chip->dt.jeita_thresholds[JEITA_HOT] = DEFAULT_BATT_TEMP_HOT;
4745 if (of_property_count_elems_of_size(node, "qcom,fg-jeita-thresholds",
4746 sizeof(u32)) == NUM_JEITA_LEVELS) {
4747 rc = of_property_read_u32_array(node,
4748 "qcom,fg-jeita-thresholds",
4749 chip->dt.jeita_thresholds, NUM_JEITA_LEVELS);
4751 pr_warn("Error reading Jeita thresholds, default values will be used rc:%d\n",
4755 if (of_property_count_elems_of_size(node,
4756 "qcom,battery-thermal-coefficients",
4757 sizeof(u8)) == BATT_THERM_NUM_COEFFS) {
4758 rc = of_property_read_u8_array(node,
4759 "qcom,battery-thermal-coefficients",
4760 chip->dt.batt_therm_coeffs,
4761 BATT_THERM_NUM_COEFFS);
4763 pr_warn("Error reading battery thermal coefficients, rc:%d\n",
4767 rc = fg_parse_dt_property_u32_array(node, "qcom,fg-esr-timer-charging",
4768 chip->dt.esr_timer_charging, NUM_ESR_TIMERS);
4770 chip->dt.esr_timer_charging[TIMER_RETRY] = -EINVAL;
4771 chip->dt.esr_timer_charging[TIMER_MAX] = -EINVAL;
4774 rc = fg_parse_dt_property_u32_array(node, "qcom,fg-esr-timer-awake",
4775 chip->dt.esr_timer_awake, NUM_ESR_TIMERS);
4777 chip->dt.esr_timer_awake[TIMER_RETRY] = -EINVAL;
4778 chip->dt.esr_timer_awake[TIMER_MAX] = -EINVAL;
4781 rc = fg_parse_dt_property_u32_array(node, "qcom,fg-esr-timer-asleep",
4782 chip->dt.esr_timer_asleep, NUM_ESR_TIMERS);
4784 chip->dt.esr_timer_asleep[TIMER_RETRY] = -EINVAL;
4785 chip->dt.esr_timer_asleep[TIMER_MAX] = -EINVAL;
4788 chip->cyc_ctr.en = of_property_read_bool(node, "qcom,cycle-counter-en");
4789 if (chip->cyc_ctr.en)
4790 chip->cyc_ctr.id = 1;
4792 chip->dt.force_load_profile = of_property_read_bool(node,
4793 "qcom,fg-force-load-profile");
4795 rc = of_property_read_u32(node, "qcom,cl-start-capacity", &temp);
4797 chip->dt.cl_start_soc = DEFAULT_CL_START_SOC;
4799 chip->dt.cl_start_soc = temp;
4801 rc = of_property_read_u32(node, "qcom,cl-min-temp", &temp);
4803 chip->dt.cl_min_temp = DEFAULT_CL_MIN_TEMP_DECIDEGC;
4805 chip->dt.cl_min_temp = temp;
4807 rc = of_property_read_u32(node, "qcom,cl-max-temp", &temp);
4809 chip->dt.cl_max_temp = DEFAULT_CL_MAX_TEMP_DECIDEGC;
4811 chip->dt.cl_max_temp = temp;
4813 rc = of_property_read_u32(node, "qcom,cl-max-increment", &temp);
4815 chip->dt.cl_max_cap_inc = DEFAULT_CL_MAX_INC_DECIPERC;
4817 chip->dt.cl_max_cap_inc = temp;
4819 rc = of_property_read_u32(node, "qcom,cl-max-decrement", &temp);
4821 chip->dt.cl_max_cap_dec = DEFAULT_CL_MAX_DEC_DECIPERC;
4823 chip->dt.cl_max_cap_dec = temp;
4825 rc = of_property_read_u32(node, "qcom,cl-min-limit", &temp);
4827 chip->dt.cl_min_cap_limit = DEFAULT_CL_MIN_LIM_DECIPERC;
4829 chip->dt.cl_min_cap_limit = temp;
4831 rc = of_property_read_u32(node, "qcom,cl-max-limit", &temp);
4833 chip->dt.cl_max_cap_limit = DEFAULT_CL_MAX_LIM_DECIPERC;
4835 chip->dt.cl_max_cap_limit = temp;
4837 rc = of_property_read_u32(node, "qcom,fg-jeita-hyst-temp", &temp);
4839 chip->dt.jeita_hyst_temp = -EINVAL;
4841 chip->dt.jeita_hyst_temp = temp;
4843 rc = of_property_read_u32(node, "qcom,fg-batt-temp-delta", &temp);
4845 chip->dt.batt_temp_delta = -EINVAL;
4846 else if (temp > BTEMP_DELTA_LOW && temp <= BTEMP_DELTA_HIGH)
4847 chip->dt.batt_temp_delta = temp;
4849 chip->dt.hold_soc_while_full = of_property_read_bool(node,
4850 "qcom,hold-soc-while-full");
4852 chip->dt.linearize_soc = of_property_read_bool(node,
4853 "qcom,linearize-soc");
4855 rc = fg_parse_ki_coefficients(chip);
4857 pr_err("Error in parsing Ki coefficients, rc=%d\n", rc);
4859 rc = of_property_read_u32(node, "qcom,fg-rconn-mohms", &temp);
4861 chip->dt.rconn_mohms = temp;
4863 rc = of_property_read_u32(node, "qcom,fg-esr-filter-switch-temp",
4866 chip->dt.esr_flt_switch_temp = DEFAULT_ESR_FLT_TEMP_DECIDEGC;
4868 chip->dt.esr_flt_switch_temp = temp;
4870 rc = of_property_read_u32(node, "qcom,fg-esr-tight-filter-micro-pct",
4873 chip->dt.esr_tight_flt_upct = DEFAULT_ESR_TIGHT_FLT_UPCT;
4875 chip->dt.esr_tight_flt_upct = temp;
4877 rc = of_property_read_u32(node, "qcom,fg-esr-broad-filter-micro-pct",
4880 chip->dt.esr_broad_flt_upct = DEFAULT_ESR_BROAD_FLT_UPCT;
4882 chip->dt.esr_broad_flt_upct = temp;
4884 rc = of_property_read_u32(node, "qcom,fg-esr-tight-lt-filter-micro-pct",
4887 chip->dt.esr_tight_lt_flt_upct = DEFAULT_ESR_TIGHT_LT_FLT_UPCT;
4889 chip->dt.esr_tight_lt_flt_upct = temp;
4891 rc = of_property_read_u32(node, "qcom,fg-esr-broad-lt-filter-micro-pct",
4894 chip->dt.esr_broad_lt_flt_upct = DEFAULT_ESR_BROAD_LT_FLT_UPCT;
4896 chip->dt.esr_broad_lt_flt_upct = temp;
4898 rc = fg_parse_slope_limit_coefficients(chip);
4900 pr_err("Error in parsing slope limit coeffs, rc=%d\n", rc);
4902 rc = of_property_read_u32(node, "qcom,fg-esr-clamp-mohms", &temp);
4904 chip->dt.esr_clamp_mohms = DEFAULT_ESR_CLAMP_MOHMS;
4906 chip->dt.esr_clamp_mohms = temp;
4908 chip->dt.esr_pulse_thresh_ma = DEFAULT_ESR_PULSE_THRESH_MA;
4909 rc = of_property_read_u32(node, "qcom,fg-esr-pulse-thresh-ma", &temp);
4911 /* ESR pulse qualification threshold range is 1-997 mA */
4912 if (temp > 0 && temp < 997)
4913 chip->dt.esr_pulse_thresh_ma = temp;
4916 chip->dt.esr_meas_curr_ma = DEFAULT_ESR_MEAS_CURR_MA;
4917 rc = of_property_read_u32(node, "qcom,fg-esr-meas-curr-ma", &temp);
4919 /* ESR measurement current range is 60-240 mA */
4920 if (temp >= 60 || temp <= 240)
4921 chip->dt.esr_meas_curr_ma = temp;
4927 static void fg_cleanup(struct fg_chip *chip)
4929 power_supply_unreg_notifier(&chip->nb);
4930 debugfs_remove_recursive(chip->dfs_root);
4931 if (chip->awake_votable)
4932 destroy_votable(chip->awake_votable);
4934 if (chip->delta_bsoc_irq_en_votable)
4935 destroy_votable(chip->delta_bsoc_irq_en_votable);
4937 if (chip->batt_miss_irq_en_votable)
4938 destroy_votable(chip->batt_miss_irq_en_votable);
4940 if (chip->batt_id_chan)
4941 iio_channel_release(chip->batt_id_chan);
4943 dev_set_drvdata(chip->dev, NULL);
4946 static int fg_gen3_probe(struct platform_device *pdev)
4948 struct fg_chip *chip;
4949 struct power_supply_config fg_psy_cfg;
4950 int rc, msoc, volt_uv, batt_temp;
4952 chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
4956 chip->dev = &pdev->dev;
4957 chip->debug_mask = &fg_gen3_debug_mask;
4958 chip->irqs = fg_irqs;
4959 chip->charge_status = -EINVAL;
4960 chip->ki_coeff_full_soc = -EINVAL;
4961 chip->online_status = -EINVAL;
4962 chip->regmap = dev_get_regmap(chip->dev->parent, NULL);
4963 if (!chip->regmap) {
4964 dev_err(chip->dev, "Parent regmap is unavailable\n");
4968 chip->batt_id_chan = iio_channel_get(chip->dev, "rradc_batt_id");
4969 if (IS_ERR(chip->batt_id_chan)) {
4970 if (PTR_ERR(chip->batt_id_chan) != -EPROBE_DEFER)
4971 pr_err("batt_id_chan unavailable %ld\n",
4972 PTR_ERR(chip->batt_id_chan));
4973 rc = PTR_ERR(chip->batt_id_chan);
4974 chip->batt_id_chan = NULL;
4978 rc = of_property_match_string(chip->dev->of_node,
4979 "io-channel-names", "rradc_die_temp");
4981 chip->die_temp_chan = iio_channel_get(chip->dev,
4983 if (IS_ERR(chip->die_temp_chan)) {
4984 if (PTR_ERR(chip->die_temp_chan) != -EPROBE_DEFER)
4985 pr_err("rradc_die_temp unavailable %ld\n",
4986 PTR_ERR(chip->die_temp_chan));
4987 rc = PTR_ERR(chip->die_temp_chan);
4988 chip->die_temp_chan = NULL;
4993 chip->awake_votable = create_votable("FG_WS", VOTE_SET_ANY, fg_awake_cb,
4995 if (IS_ERR(chip->awake_votable)) {
4996 rc = PTR_ERR(chip->awake_votable);
4997 chip->awake_votable = NULL;
5001 chip->delta_bsoc_irq_en_votable = create_votable("FG_DELTA_BSOC_IRQ",
5003 fg_delta_bsoc_irq_en_cb, chip);
5004 if (IS_ERR(chip->delta_bsoc_irq_en_votable)) {
5005 rc = PTR_ERR(chip->delta_bsoc_irq_en_votable);
5006 chip->delta_bsoc_irq_en_votable = NULL;
5010 chip->batt_miss_irq_en_votable = create_votable("FG_BATT_MISS_IRQ",
5012 fg_batt_miss_irq_en_cb, chip);
5013 if (IS_ERR(chip->batt_miss_irq_en_votable)) {
5014 rc = PTR_ERR(chip->batt_miss_irq_en_votable);
5015 chip->batt_miss_irq_en_votable = NULL;
5019 rc = fg_parse_dt(chip);
5021 dev_err(chip->dev, "Error in reading DT parameters, rc:%d\n",
5026 mutex_init(&chip->bus_lock);
5027 mutex_init(&chip->sram_rw_lock);
5028 mutex_init(&chip->cyc_ctr.lock);
5029 mutex_init(&chip->cl.lock);
5030 mutex_init(&chip->ttf.lock);
5031 mutex_init(&chip->charge_full_lock);
5032 mutex_init(&chip->qnovo_esr_ctrl_lock);
5033 init_completion(&chip->soc_update);
5034 init_completion(&chip->soc_ready);
5035 INIT_DELAYED_WORK(&chip->profile_load_work, profile_load_work);
5036 INIT_WORK(&chip->status_change_work, status_change_work);
5037 INIT_DELAYED_WORK(&chip->ttf_work, ttf_work);
5038 INIT_DELAYED_WORK(&chip->sram_dump_work, sram_dump_work);
5040 rc = fg_memif_init(chip);
5042 dev_err(chip->dev, "Error in initializing FG_MEMIF, rc:%d\n",
5047 rc = fg_hw_init(chip);
5049 dev_err(chip->dev, "Error in initializing FG hardware, rc:%d\n",
5054 platform_set_drvdata(pdev, chip);
5056 /* Register the power supply */
5057 fg_psy_cfg.drv_data = chip;
5058 fg_psy_cfg.of_node = NULL;
5059 fg_psy_cfg.supplied_to = NULL;
5060 fg_psy_cfg.num_supplicants = 0;
5061 chip->fg_psy = devm_power_supply_register(chip->dev, &fg_psy_desc,
5063 if (IS_ERR(chip->fg_psy)) {
5064 pr_err("failed to register fg_psy rc = %ld\n",
5065 PTR_ERR(chip->fg_psy));
5069 chip->nb.notifier_call = fg_notifier_cb;
5070 rc = power_supply_reg_notifier(&chip->nb);
5072 pr_err("Couldn't register psy notifier rc = %d\n", rc);
5076 rc = fg_register_interrupts(chip);
5078 dev_err(chip->dev, "Error in registering interrupts, rc:%d\n",
5083 /* Keep SOC_UPDATE_IRQ disabled until we require it */
5084 if (fg_irqs[SOC_UPDATE_IRQ].irq)
5085 disable_irq_nosync(fg_irqs[SOC_UPDATE_IRQ].irq);
5087 /* Keep BSOC_DELTA_IRQ disabled until we require it */
5088 vote(chip->delta_bsoc_irq_en_votable, DELTA_BSOC_IRQ_VOTER, false, 0);
5090 /* Keep BATT_MISSING_IRQ disabled until we require it */
5091 vote(chip->batt_miss_irq_en_votable, BATT_MISS_IRQ_VOTER, false, 0);
5093 rc = fg_debugfs_create(chip);
5095 dev_err(chip->dev, "Error in creating debugfs entries, rc:%d\n",
5100 rc = fg_get_battery_voltage(chip, &volt_uv);
5102 rc = fg_get_prop_capacity(chip, &msoc);
5105 rc = fg_get_battery_temp(chip, &batt_temp);
5108 pr_info("battery SOC:%d voltage: %duV temp: %d id: %dKOhms\n",
5109 msoc, volt_uv, batt_temp, chip->batt_id_ohms / 1000);
5110 rc = fg_esr_filter_config(chip, batt_temp);
5112 pr_err("Error in configuring ESR filter rc:%d\n", rc);
5115 device_init_wakeup(chip->dev, true);
5116 schedule_delayed_work(&chip->profile_load_work, 0);
5118 pr_debug("FG GEN3 driver probed successfully\n");
5125 static int fg_gen3_suspend(struct device *dev)
5127 struct fg_chip *chip = dev_get_drvdata(dev);
5130 rc = fg_esr_timer_config(chip, true);
5132 pr_err("Error in configuring ESR timer, rc=%d\n", rc);
5134 cancel_delayed_work_sync(&chip->ttf_work);
5136 cancel_delayed_work_sync(&chip->sram_dump_work);
5140 static int fg_gen3_resume(struct device *dev)
5142 struct fg_chip *chip = dev_get_drvdata(dev);
5145 rc = fg_esr_timer_config(chip, false);
5147 pr_err("Error in configuring ESR timer, rc=%d\n", rc);
5149 schedule_delayed_work(&chip->ttf_work, 0);
5151 schedule_delayed_work(&chip->sram_dump_work,
5152 msecs_to_jiffies(fg_sram_dump_period_ms));
5156 static const struct dev_pm_ops fg_gen3_pm_ops = {
5157 .suspend = fg_gen3_suspend,
5158 .resume = fg_gen3_resume,
5161 static int fg_gen3_remove(struct platform_device *pdev)
5163 struct fg_chip *chip = dev_get_drvdata(&pdev->dev);
5169 static void fg_gen3_shutdown(struct platform_device *pdev)
5171 struct fg_chip *chip = dev_get_drvdata(&pdev->dev);
5174 if (chip->charge_full) {
5175 rc = fg_get_sram_prop(chip, FG_SRAM_BATT_SOC, &bsoc);
5177 pr_err("Error in getting BATT_SOC, rc=%d\n", rc);
5181 /* We need 2 most significant bytes here */
5182 bsoc = (u32)bsoc >> 16;
5184 rc = fg_configure_full_soc(chip, bsoc);
5186 pr_err("Error in configuring full_soc, rc=%d\n", rc);
5192 static const struct of_device_id fg_gen3_match_table[] = {
5193 {.compatible = FG_GEN3_DEV_NAME},
5197 static struct platform_driver fg_gen3_driver = {
5199 .name = FG_GEN3_DEV_NAME,
5200 .owner = THIS_MODULE,
5201 .of_match_table = fg_gen3_match_table,
5202 .pm = &fg_gen3_pm_ops,
5204 .probe = fg_gen3_probe,
5205 .remove = fg_gen3_remove,
5206 .shutdown = fg_gen3_shutdown,
5209 static int __init fg_gen3_init(void)
5211 return platform_driver_register(&fg_gen3_driver);
5214 static void __exit fg_gen3_exit(void)
5216 return platform_driver_unregister(&fg_gen3_driver);
5219 module_init(fg_gen3_init);
5220 module_exit(fg_gen3_exit);
5222 MODULE_DESCRIPTION("QPNP Fuel gauge GEN3 driver");
5223 MODULE_LICENSE("GPL v2");
5224 MODULE_ALIAS("platform:" FG_GEN3_DEV_NAME);