xref: /rk3399_rockchip-uboot/drivers/power/fuel_gauge/fg_rk818.c (revision 5ce558eee1d84a2b85f2bbc4c4547c8ea1c1dae4)
1 /*
2  * (C) Copyright 2017 Rockchip Electronics Co., Ltd
3  *
4  * SPDX-License-Identifier:     GPL-2.0+
5  */
6 
7 #include <dm.h>
8 #include <errno.h>
9 #include <common.h>
10 #include <malloc.h>
11 #include <fdtdec.h>
12 #include <asm/gpio.h>
13 #include <common.h>
14 #include <power/pmic.h>
15 #include <power/fuel_gauge.h>
16 #include <power/rk8xx_pmic.h>
17 #include <linux/usb/phy-rockchip-inno-usb2.h>
18 #include "fg_regs.h"
19 
20 DECLARE_GLOBAL_DATA_PTR;
21 
22 static int dbg_enable = 0;
23 #define DBG(args...) \
24 	do { \
25 		if (dbg_enable) { \
26 			printf(args); \
27 		} \
28 	} while (0)
29 
30 #define BAT_INFO(fmt, args...) printf("rk818-bat: "fmt, ##args)
31 
32 #define DRIVER_VERSION		"2.0"
33 
34 /* THERMAL_REG */
35 #define TEMP_105C		(0x02 << 2)
36 #define TEMP_115C		(0x03 << 2)
37 #define FB_TEMP_MSK		0x0c
38 
39 /* CHRG_CTRL_REG2 */
40 #define FINISH_100MA		(0x00 << 6)
41 #define FINISH_150MA		(0x01 << 6)
42 #define FINISH_200MA		(0x02 << 6)
43 #define FINISH_250MA		(0x03 << 6)
44 #define FINISH_CUR_MSK		0xc7
45 
46 /* CHRG_CTRL_REG3 */
47 #define CHRG_TERM_DIG_SIGNAL	(1 << 5)
48 #define CHRG_TERM_ANA_SIGNAL	(0 << 5)
49 #define CHRG_TIMER_CCCV_EN	(1 << 2)
50 #define CHRG_TERM_SIG_MSK	(1 << 5)
51 
52 /* CHRG_CTRL_REG */
53 #define ILIM_450MA		(0x00)
54 #define ILIM_80MA		(0x01)
55 #define ILIM_850MA		(0x02)
56 #define ILIM_2000MA		(0x07)
57 #define CHRG_CT_EN		(1 << 7)
58 
59 /* USB_CTRL_REG */
60 #define INPUT_CUR_MSK		0x0f
61 
62 /* VB_MON_REG */
63 #define PLUG_IN_STS		(1 << 6)
64 
65 /* GGSTS */
66 #define BAT_CON			(1 << 4)
67 #define VOL_INSTANT		(1 << 0)
68 #define VOL_AVG			(0 << 0)
69 
70 /* TS_CTRL_REG */
71 #define GG_EN			(1 << 7)
72 
73 /* CHRG_USB_CTRL */
74 #define CHRG_EN			(1 << 7)
75 
76 #define ADC_TS2_EN		(1 << 4)
77 #define TS2_ADC_MODE		(1 << 5)
78 
79 /* SUP_STS_REG */
80 #define BAT_EXS			(1 << 7)
81 #define USB_EXIST		(1 << 1)
82 #define USB_EFF			(1 << 0)
83 #define CHARGE_OFF		(0x00 << 4)
84 #define DEAD_CHARGE		(0x01 << 4)
85 #define TRICKLE_CHARGE		(0x02 << 4)
86 #define CC_OR_CV		(0x03 << 4)
87 #define CHARGE_FINISH		(0x04 << 4)
88 #define USB_OVER_VOL		(0x05 << 4)
89 #define BAT_TMP_ERR		(0x06 << 4)
90 #define TIMER_ERR		(0x07 << 4)
91 #define USB_VLIMIT_EN		(1 << 3)
92 #define USB_CLIMIT_EN		(1 << 2)
93 #define BAT_STATUS_MSK		0x70
94 
95 /* GGCON */
96 #define ADC_CUR_MODE		(1 << 1)
97 
98 /* CALI PARAM */
99 #define FINISH_CALI_CURR	1500
100 #define TERM_CALI_CURR		600
101 #define	VIRTUAL_POWER_VOL	4200
102 #define	VIRTUAL_POWER_SOC	66
103 #define SECONDS(n)		((n) * 1000)
104 
105 /* CALC PARAM */
106 #define MAX_PERCENTAGE		100
107 #define MAX_INTERPOLATE		1000
108 #define MAX_INT			0x7fff
109 #define MIN_FCC			500
110 
111 /* sample resistor and division */
112 #define SAMPLE_RES_10mR		10
113 #define SAMPLE_RES_20mR		20
114 #define SAMPLE_RES_DIV1		1
115 #define SAMPLE_RES_DIV2		2
116 
117 #define FG_INIT			(1 << 5)
118 #define FG_RESET_LATE		(1 << 4)
119 #define FG_RESET_NOW		(1 << 3)
120 
121 #define DEFAULT_POFFSET		42
122 #define DEFAULT_COFFSET		0x832
123 #define INVALID_COFFSET_MIN	0x780
124 #define INVALID_COFFSET_MAX	0x980
125 
126 #define CHRG_TERM_DSOC		90
127 #define CHRG_TERM_K		650
128 #define CHRG_FULL_K		400
129 #define ADC_CALIB_THRESHOLD	4
130 
131 #define TS2_THRESHOLD_VOL	4350
132 #define TS2_VALID_VOL		1000
133 #define TS2_VOL_MULTI		0
134 #define TS2_CHECK_CNT		5
135 
136 #define DIV(x)			((x) ? (x) : 1)
137 
138 /* charger type definition */
139 enum charger_type {
140 	NO_CHARGER = 0,
141 	USB_CHARGER,
142 	AC_CHARGER,
143 	DC_CHARGER,
144 	UNDEF_CHARGER,
145 };
146 
147 struct battery_info {
148 	int		chrg_type;
149 	int		poffset;
150 	int		bat_res;
151 	int		current_avg;
152 	int		voltage_avg;
153 	int		voltage_ocv;
154 	int		voltage_k;
155 	int		voltage_b;
156 	int		dsoc;
157 	int		rsoc;
158 	int		fcc;
159 	int		qmax;
160 	int		remain_cap;
161 	int		design_cap;
162 	int		nac;
163 	u32		*ocv_table;
164 	u32		ocv_size;
165 	int		virtual_power;
166 	int		ts2_vol_multi;
167 	int		pwroff_min;
168 	int		sm_old_cap;
169 	int		sm_linek;
170 	int		sm_chrg_dsoc;
171 	int		adc_allow_update;
172 	int		chrg_vol_sel;
173 	int		chrg_cur_input;
174 	int		chrg_cur_sel;
175 	int		dts_vol_sel;
176 	int		dts_cur_input;
177 	int		dts_cur_sel;
178 	int		max_soc_offset;
179 	int		sample_res;
180 	int		res_div;
181 	struct gpio_desc *dc_det;
182 	int		dc_det_adc;
183 	ulong		finish_chrg_base;
184 	ulong		term_sig_base;
185 	u8		calc_dsoc;
186 	u8		calc_rsoc;
187 	int		sm_meet_soc;
188 	u8		halt_cnt;
189 	u8		dc_active_level;
190 	bool		dc_is_valid;
191 	bool		is_halt;
192 	bool		is_ocv_calib;
193 	bool		is_max_soc_offset;
194 	bool		is_first_power_on;
195 	bool		is_sw_reset;
196 	int		pwr_dsoc;
197 	int		pwr_rsoc;
198 	int		pwr_vol;
199 };
200 
201 static struct udevice *g_pmic_dev;
202 
203 /* TODO */
204 #define CONFIG_SCREEN_ON_VOL_THRESD	3400
205 
206 static const u32 CHRG_VOL_SEL[] = {
207 	4050, 4100, 4150, 4200, 4250, 4300, 4350
208 };
209 
210 static const u32 CHRG_CUR_SEL[] = {
211 	1000, 1200, 1400, 1600, 1800, 2000, 2250, 2400, 2600, 2800, 3000
212 };
213 
214 static const u32 CHRG_CUR_INPUT[] = {
215 	450, 800, 850, 1000, 1250, 1500, 1750, 2000, 2250, 2500, 2750, 3000
216 };
217 
218 static int rk818_bat_read(u8 reg)
219 {
220 	return pmic_reg_read(g_pmic_dev, reg);
221 }
222 
223 static void rk818_bat_write(u8 reg, u8 buf)
224 {
225 	pmic_reg_write(g_pmic_dev, reg, buf);
226 }
227 
228 static int rk818_bat_dwc_otg_check_dpdm(void)
229 {
230 	return rockchip_chg_get_type();
231 }
232 
233 static int rk818_bat_get_rsoc(struct battery_info *di)
234 {
235 	return (di->remain_cap + di->fcc / 200) * 100 / DIV(di->fcc);
236 }
237 
238 static int rk818_bat_get_dsoc(struct  battery_info *di)
239 {
240 	return rk818_bat_read(SOC_REG);
241 }
242 
243 static void rk818_bat_enable_gauge(struct battery_info *di)
244 {
245 	u8 val;
246 
247 	val = rk818_bat_read(TS_CTRL_REG);
248 	val |= GG_EN;
249 	rk818_bat_write(TS_CTRL_REG, val);
250 }
251 
252 static int rk818_bat_get_vcalib0(struct battery_info *di)
253 {
254 	int val = 0;
255 
256 	val |= rk818_bat_read(VCALIB0_REGL) << 0;
257 	val |= rk818_bat_read(VCALIB0_REGH) << 8;
258 
259 	return val;
260 }
261 
262 static int rk818_bat_get_vcalib1(struct battery_info *di)
263 {
264 	int val = 0;
265 
266 	val |= rk818_bat_read(VCALIB1_REGL) << 0;
267 	val |= rk818_bat_read(VCALIB1_REGH) << 8;
268 
269 	return val;
270 }
271 
272 static int rk818_bat_get_coffset(struct battery_info *di)
273 {
274 	int val = 0;
275 
276 	val |= rk818_bat_read(CAL_OFFSET_REGL) << 0;
277 	val |= rk818_bat_read(CAL_OFFSET_REGH) << 8;
278 
279 	DBG("<%s>. coffset: 0x%x\n", __func__, val);
280 	return val;
281 }
282 
283 static void rk818_bat_set_coffset(struct battery_info *di, int val)
284 {
285 	u8 buf;
286 
287 	buf = (val >> 0) & 0xff;
288 	rk818_bat_write(CAL_OFFSET_REGL, buf);
289 	buf = (val >> 8) & 0xff;
290 	rk818_bat_write(CAL_OFFSET_REGH, buf);
291 
292 	DBG("<%s>. set coffset: 0x%x\n", __func__, val);
293 }
294 
295 static int rk818_bat_get_ioffset(struct battery_info *di)
296 {
297 	int val = 0;
298 
299 	val |= rk818_bat_read(IOFFSET_REGL) << 0;
300 	val |= rk818_bat_read(IOFFSET_REGH) << 8;
301 
302 	DBG("<%s>. ioffset: 0x%x\n", __func__, val);
303 	return val;
304 }
305 
306 static void rk818_bat_init_coffset(struct battery_info *di)
307 {
308 	int ioffset, coffset;
309 
310 	ioffset = rk818_bat_get_ioffset(di);
311 
312 	di->poffset = rk818_bat_read(POFFSET_REG);
313 	if (!di->poffset)
314 		di->poffset = DEFAULT_POFFSET;
315 
316 	coffset = di->poffset + ioffset;
317 	if (coffset < INVALID_COFFSET_MIN || coffset > INVALID_COFFSET_MAX)
318 		coffset = DEFAULT_COFFSET;
319 
320 	rk818_bat_set_coffset(di, coffset);
321 }
322 
323 static void rk818_bat_init_voltage_kb(struct battery_info *di)
324 {
325 	int vcalib0, vcalib1;
326 
327 	vcalib0 = rk818_bat_get_vcalib0(di);
328 	vcalib1 = rk818_bat_get_vcalib1(di);
329 	di->voltage_k = (4200 - 3000) * 1000 / DIV(vcalib1 - vcalib0);
330 	di->voltage_b = 4200 - (di->voltage_k * vcalib1) / 1000;
331 	DBG("%s. vk=%d, vb=%d\n", __func__, di->voltage_k, di->voltage_b);
332 }
333 
334 static int rk818_bat_get_ocv_voltage(struct battery_info *di)
335 {
336 	int vol, val = 0;
337 
338 	val |= rk818_bat_read(BAT_OCV_REGL) << 0;
339 	val |= rk818_bat_read(BAT_OCV_REGH) << 8;
340 	vol = di->voltage_k * val / 1000 + di->voltage_b;
341 
342 	return vol;
343 }
344 
345 static int rk818_bat_get_avg_current(struct battery_info *di)
346 {
347 	int val = 0;
348 
349 	val |= rk818_bat_read(BAT_CUR_AVG_REGL) << 0;
350 	val |= rk818_bat_read(BAT_CUR_AVG_REGH) << 8;
351 
352 	if (val & 0x800)
353 		val -= 4096;
354 	val = val * di->res_div * 1506 / 1000;
355 
356 	return val;
357 }
358 
359 static int rk818_bat_get_avg_voltage(struct battery_info *di)
360 {
361 	int vol, val = 0;
362 
363 	val |= rk818_bat_read(BAT_VOL_REGL) << 0;
364 	val |= rk818_bat_read(BAT_VOL_REGH) << 8;
365 	vol = di->voltage_k * val / 1000 + di->voltage_b;
366 
367 	return vol;
368 }
369 
370 static int rk818_bat_get_est_voltage(struct battery_info *di)
371 {
372 	int est_vol, vol, curr;
373 
374 	vol = rk818_bat_get_avg_voltage(di);
375 	curr = rk818_bat_get_avg_current(di);
376 	est_vol = vol - (di->bat_res * curr / 1000);
377 
378 	return (est_vol > 2800) ? est_vol : vol;
379 }
380 
381 static u8 rk818_bat_finish_ma(struct battery_info *di, int fcc)
382 {
383 	u8 ma;
384 
385 	if (di->res_div == 2)
386 		ma = FINISH_100MA;
387 	else if (fcc > 5000)
388 		ma = FINISH_250MA;
389 	else if (fcc >= 4000)
390 		ma = FINISH_200MA;
391 	else if (fcc >= 3000)
392 		ma = FINISH_150MA;
393 	else
394 		ma = FINISH_100MA;
395 
396 	return ma;
397 }
398 
399 static void rk818_bat_select_chrg_cv(struct battery_info *di)
400 {
401 	int index, chrg_vol_sel, chrg_cur_sel, chrg_cur_input;
402 
403 	chrg_vol_sel = di->dts_vol_sel;
404 	chrg_cur_sel = di->dts_cur_sel;
405 	chrg_cur_input = di->dts_cur_input;
406 	if (di->sample_res == SAMPLE_RES_10mR) {
407 		if (chrg_cur_sel > 2000)
408 			chrg_cur_sel /= di->res_div;
409 		else
410 			chrg_cur_sel = 1000;
411 	}
412 
413 	for (index = 0; index < ARRAY_SIZE(CHRG_VOL_SEL); index++) {
414 		if (chrg_vol_sel < CHRG_VOL_SEL[index])
415 			break;
416 		di->chrg_vol_sel = (index << 4);
417 	}
418 
419 	for (index = 0; index < ARRAY_SIZE(CHRG_CUR_INPUT); index++) {
420 		if (chrg_cur_input < CHRG_CUR_INPUT[index])
421 			break;
422 		di->chrg_cur_input = (index << 0);
423 	}
424 
425 	for (index = 0; index < ARRAY_SIZE(CHRG_CUR_SEL); index++) {
426 		if (chrg_cur_sel < CHRG_CUR_SEL[index])
427 			break;
428 		di->chrg_cur_sel = (index << 0);
429 	}
430 
431 	DBG("<%s>. vol=0x%x, input=0x%x, sel=0x%x\n",
432 	    __func__, di->chrg_vol_sel, di->chrg_cur_input, di->chrg_cur_sel);
433 }
434 
435 static void rk818_bat_init_chrg_config(struct battery_info *di)
436 {
437 	u8 chrg_ctrl1, usb_ctrl, chrg_ctrl2, chrg_ctrl3;
438 	u8 sup_sts, ggcon, thermal, finish_ma;
439 
440 	rk818_bat_select_chrg_cv(di);
441 	finish_ma = rk818_bat_finish_ma(di, di->fcc);
442 
443 	ggcon = rk818_bat_read(GGCON_REG);
444 	sup_sts = rk818_bat_read(SUP_STS_REG);
445 	usb_ctrl = rk818_bat_read(USB_CTRL_REG);
446 	thermal = rk818_bat_read(THERMAL_REG);
447 	chrg_ctrl2 = rk818_bat_read(CHRG_CTRL_REG2);
448 	chrg_ctrl3 = rk818_bat_read(CHRG_CTRL_REG3);
449 
450 	/* set charge current and voltage */
451 	usb_ctrl &= ~INPUT_CUR_MSK;
452 	usb_ctrl |= di->chrg_cur_input;
453 	chrg_ctrl1 = (CHRG_EN | di->chrg_vol_sel | di->chrg_cur_sel);
454 
455 	/* digital signal and finish current*/
456 	chrg_ctrl3 &= ~CHRG_TERM_SIG_MSK;
457 	chrg_ctrl3 |= CHRG_TERM_ANA_SIGNAL;
458 	chrg_ctrl2 &= ~FINISH_CUR_MSK;
459 	chrg_ctrl2 |= finish_ma;
460 
461 	/* cccv mode */
462 	chrg_ctrl3 &= ~CHRG_TIMER_CCCV_EN;
463 
464 	/* enable voltage limit and enable input current limit */
465 	sup_sts &= ~USB_VLIMIT_EN;
466 	sup_sts |= USB_CLIMIT_EN;
467 
468 	/* set feedback temperature */
469 	usb_ctrl |= CHRG_CT_EN;
470 	thermal &= ~FB_TEMP_MSK;
471 	thermal |= TEMP_105C;
472 
473 	/* adc current mode */
474 	ggcon |= ADC_CUR_MODE;
475 
476 	rk818_bat_write(GGCON_REG, ggcon);
477 	rk818_bat_write(SUP_STS_REG, sup_sts);
478 	rk818_bat_write(USB_CTRL_REG, usb_ctrl);
479 	rk818_bat_write(THERMAL_REG, thermal);
480 	rk818_bat_write(CHRG_CTRL_REG1, chrg_ctrl1);
481 	rk818_bat_write(CHRG_CTRL_REG2, chrg_ctrl2);
482 	rk818_bat_write(CHRG_CTRL_REG3, chrg_ctrl3);
483 }
484 
485 static u32 interpolate(int value, u32 *table, int size)
486 {
487 	uint8_t i;
488 	uint16_t d;
489 
490 	for (i = 0; i < size; i++) {
491 		if (value < table[i])
492 			break;
493 	}
494 
495 	if ((i > 0) && (i < size)) {
496 		d = (value - table[i - 1]) * (MAX_INTERPOLATE / (size - 1));
497 		d /= table[i] - table[i - 1];
498 		d = d + (i - 1) * (MAX_INTERPOLATE / (size - 1));
499 	} else {
500 		d = i * ((MAX_INTERPOLATE + size / 2) / size);
501 	}
502 
503 	if (d > 1000)
504 		d = 1000;
505 
506 	return d;
507 }
508 
509 /* returns (a * b) / c */
510 static int32_t ab_div_c(u32 a, u32 b, u32 c)
511 {
512 	bool sign;
513 	u32 ans = MAX_INT;
514 	int32_t tmp;
515 
516 	sign = ((((a ^ b) ^ c) & 0x80000000) != 0);
517 
518 	if (c != 0) {
519 		if (sign)
520 			c = -c;
521 		tmp = ((int32_t)a * b + (c >> 1)) / c;
522 		if (tmp < MAX_INT)
523 			ans = tmp;
524 	}
525 
526 	if (sign)
527 		ans = -ans;
528 
529 	return ans;
530 }
531 
532 static int rk818_bat_vol_to_cap(struct battery_info *di, int voltage)
533 {
534 	u32 *ocv_table, tmp;
535 	int ocv_size, ocv_cap;
536 
537 	ocv_table = di->ocv_table;
538 	ocv_size = di->ocv_size;
539 	tmp = interpolate(voltage, ocv_table, ocv_size);
540 	ocv_cap = ab_div_c(tmp, di->fcc, MAX_INTERPOLATE);
541 
542 	return ocv_cap;
543 }
544 
545 static int rk818_bat_vol_to_soc(struct battery_info *di, int voltage)
546 {
547 	u32 *ocv_table, tmp;
548 	int ocv_size, ocv_soc;
549 
550 	ocv_table = di->ocv_table;
551 	ocv_size = di->ocv_size;
552 	tmp = interpolate(voltage, ocv_table, ocv_size);
553 	ocv_soc = ab_div_c(tmp, MAX_PERCENTAGE, MAX_INTERPOLATE);
554 
555 	return ocv_soc;
556 }
557 
558 static int rk818_bat_get_prev_cap(struct battery_info *di)
559 {
560 	int val = 0;
561 
562 	val |= rk818_bat_read(REMAIN_CAP_REG3) << 24;
563 	val |= rk818_bat_read(REMAIN_CAP_REG2) << 16;
564 	val |= rk818_bat_read(REMAIN_CAP_REG1) << 8;
565 	val |= rk818_bat_read(REMAIN_CAP_REG0) << 0;
566 
567 	return val;
568 }
569 
570 static void rk818_bat_save_fcc(struct battery_info *di, u32 cap)
571 {
572 	u8 buf;
573 
574 	buf = (cap >> 24) & 0xff;
575 	rk818_bat_write(NEW_FCC_REG3, buf);
576 	buf = (cap >> 16) & 0xff;
577 	rk818_bat_write(NEW_FCC_REG2, buf);
578 	buf = (cap >> 8) & 0xff;
579 	rk818_bat_write(NEW_FCC_REG1, buf);
580 	buf = (cap >> 0) & 0xff;
581 	rk818_bat_write(NEW_FCC_REG0, buf);
582 }
583 
584 static int rk818_bat_get_fcc(struct battery_info *di)
585 {
586 	int val = 0;
587 
588 	val |= rk818_bat_read(NEW_FCC_REG3) << 24;
589 	val |= rk818_bat_read(NEW_FCC_REG2) << 16;
590 	val |= rk818_bat_read(NEW_FCC_REG1) << 8;
591 	val |= rk818_bat_read(NEW_FCC_REG0) << 0;
592 
593 	if (val < MIN_FCC)
594 		val = di->design_cap;
595 	else if (val > di->qmax)
596 		val = di->qmax;
597 
598 	return val;
599 }
600 
601 static int rk818_bat_get_pwroff_min(struct battery_info *di)
602 {
603 	u8 cur, last;
604 
605 	cur = rk818_bat_read(NON_ACT_TIMER_CNT_REG);
606 	last = rk818_bat_read(NON_ACT_TIMER_CNT_SAVE_REG);
607 	rk818_bat_write(NON_ACT_TIMER_CNT_SAVE_REG, cur);
608 
609 	return (cur != last) ? cur : 0;
610 }
611 
612 static int rk818_bat_get_coulomb_cap(struct battery_info *di)
613 {
614 	int val = 0;
615 
616 	val |= rk818_bat_read(GASCNT_REG3) << 24;
617 	val |= rk818_bat_read(GASCNT_REG2) << 16;
618 	val |= rk818_bat_read(GASCNT_REG1) << 8;
619 	val |= rk818_bat_read(GASCNT_REG0) << 0;
620 	val /= 2390;
621 
622 	return val * di->res_div;
623 }
624 
625 static void rk818_bat_init_capacity(struct battery_info *di, u32 capacity)
626 {
627 	u8 buf;
628 	u32 cap;
629 	int delta;
630 
631 	delta = capacity - di->remain_cap;
632 	if (!delta)
633 		return;
634 
635 	cap = capacity * 2390 / di->res_div;
636 	buf = (cap >> 24) & 0xff;
637 	rk818_bat_write(GASCNT_CAL_REG3, buf);
638 	buf = (cap >> 16) & 0xff;
639 	rk818_bat_write(GASCNT_CAL_REG2, buf);
640 	buf = (cap >> 8) & 0xff;
641 	rk818_bat_write(GASCNT_CAL_REG1, buf);
642 	buf = (cap >> 0) & 0xff;
643 	rk818_bat_write(GASCNT_CAL_REG0, buf);
644 
645 	di->remain_cap = rk818_bat_get_coulomb_cap(di);
646 	di->rsoc = rk818_bat_get_rsoc(di);
647 }
648 
649 static bool is_rk818_bat_ocv_valid(struct battery_info *di)
650 {
651 	return di->pwroff_min >= 30 ? true : false;
652 }
653 
654 static int rk818_bat_get_usb_state(struct battery_info *di)
655 {
656 	int charger_type;
657 
658 	switch (rk818_bat_dwc_otg_check_dpdm()) {
659 	case 0:
660 		if ((rk818_bat_read(VB_MON_REG) & PLUG_IN_STS) != 0)
661 			charger_type = DC_CHARGER;
662 		else
663 			charger_type = NO_CHARGER;
664 		break;
665 	case 1:
666 	case 3:
667 		charger_type = USB_CHARGER;
668 		break;
669 	case 2:
670 		charger_type = AC_CHARGER;
671 		break;
672 	default:
673 		charger_type = NO_CHARGER;
674 	}
675 
676 	return charger_type;
677 }
678 
679 static void rk818_bat_clr_initialized_state(struct battery_info *di)
680 {
681 	u8 val;
682 
683 	val = rk818_bat_read(MISC_MARK_REG);
684 	val &= ~FG_INIT;
685 	rk818_bat_write(MISC_MARK_REG, val);
686 }
687 
688 static bool rk818_bat_is_initialized(struct battery_info *di)
689 {
690 	return (rk818_bat_read(MISC_MARK_REG) & FG_INIT) ? true : false;
691 }
692 
693 static void rk818_bat_set_initialized_state(struct battery_info *di)
694 {
695 	u8 val;
696 
697 	val = rk818_bat_read(MISC_MARK_REG);
698 	if (rk818_bat_get_usb_state(di) != NO_CHARGER) {
699 		val |= FG_INIT;
700 		rk818_bat_write(MISC_MARK_REG, val);
701 		BAT_INFO("fuel gauge initialized... estv=%d, ch=%d\n",
702 			 rk818_bat_get_est_voltage(di),
703 			 rk818_bat_get_usb_state(di));
704 	}
705 }
706 
707 static void rk818_bat_first_pwron(struct battery_info *di)
708 {
709 	int ocv_vol;
710 
711 	rk818_bat_save_fcc(di, di->design_cap);
712 	ocv_vol = rk818_bat_get_ocv_voltage(di);
713 	di->fcc = rk818_bat_get_fcc(di);
714 	di->nac = rk818_bat_vol_to_cap(di, ocv_vol);
715 	di->rsoc = rk818_bat_vol_to_soc(di, ocv_vol);
716 	di->dsoc = di->rsoc;
717 	rk818_bat_init_capacity(di, di->nac);
718 	rk818_bat_set_initialized_state(di);
719 	BAT_INFO("first power on: soc=%d\n", di->dsoc);
720 }
721 
722 static u8 rk818_bat_get_halt_cnt(struct battery_info *di)
723 {
724 	return rk818_bat_read(HALT_CNT_REG);
725 }
726 
727 static void rk818_bat_inc_halt_cnt(struct battery_info *di)
728 {
729 	u8 cnt;
730 
731 	cnt = rk818_bat_read(HALT_CNT_REG);
732 	rk818_bat_write(HALT_CNT_REG, ++cnt);
733 }
734 
735 static bool is_rk818_bat_last_halt(struct battery_info *di)
736 {
737 	int pre_cap = rk818_bat_get_prev_cap(di);
738 	int now_cap = rk818_bat_get_coulomb_cap(di);
739 
740 	/* over 5%: system halt last time */
741 	if (abs(now_cap - pre_cap) > (di->fcc / 20)) {
742 		rk818_bat_inc_halt_cnt(di);
743 		return true;
744 	} else {
745 		return false;
746 	}
747 }
748 
749 static void rk818_bat_not_first_pwron(struct battery_info *di)
750 {
751 	int pre_soc, pre_cap, ocv_cap, ocv_soc, ocv_vol, now_cap;
752 
753 	di->fcc = rk818_bat_get_fcc(di);
754 	pre_soc = rk818_bat_get_dsoc(di);
755 	pre_cap = rk818_bat_get_prev_cap(di);
756 	now_cap = rk818_bat_get_coulomb_cap(di);
757 	di->pwr_dsoc = pre_soc;
758 	di->pwr_rsoc = (now_cap + di->fcc / 200) * 100 / DIV(di->fcc);
759 	di->is_halt = is_rk818_bat_last_halt(di);
760 	di->halt_cnt = rk818_bat_get_halt_cnt(di);
761 	di->is_ocv_calib = is_rk818_bat_ocv_valid(di);
762 
763 	if (di->is_halt) {
764 		BAT_INFO("system halt last time... cap: pre=%d, now=%d\n",
765 			 pre_cap, now_cap);
766 		if (now_cap < 0)
767 			now_cap = 0;
768 		rk818_bat_init_capacity(di, now_cap);
769 		pre_cap = di->remain_cap;
770 		pre_soc = di->rsoc;
771 		goto finish;
772 	} else if (di->is_ocv_calib) {
773 		ocv_vol = rk818_bat_get_ocv_voltage(di);
774 		ocv_soc = rk818_bat_vol_to_soc(di, ocv_vol);
775 		ocv_cap = rk818_bat_vol_to_cap(di, ocv_vol);
776 		pre_cap = ocv_cap;
777 		BAT_INFO("do ocv calib.. rsoc=%d\n", ocv_soc);
778 
779 		if (abs(ocv_soc - pre_soc) >= di->max_soc_offset) {
780 			BAT_INFO("trigger max soc offset, soc: %d -> %d\n",
781 				 pre_soc, ocv_soc);
782 			pre_soc = ocv_soc;
783 			di->is_max_soc_offset = true;
784 		}
785 		BAT_INFO("OCV calib: cap=%d, rsoc=%d\n", ocv_cap, ocv_soc);
786 	}
787 finish:
788 	di->dsoc = pre_soc;
789 	di->nac = pre_cap;
790 	rk818_bat_init_capacity(di, di->nac);
791 	rk818_bat_set_initialized_state(di);
792 	BAT_INFO("dl=%d rl=%d cap=%d m=%d v=%d ov=%d c=%d pl=%d ch=%d Ver=%s\n",
793 		 di->dsoc, di->rsoc, di->remain_cap, di->pwroff_min,
794 		 rk818_bat_get_avg_voltage(di), rk818_bat_get_ocv_voltage(di),
795 		 rk818_bat_get_avg_current(di), rk818_bat_get_dsoc(di),
796 		 rk818_bat_get_usb_state(di), DRIVER_VERSION
797 		 );
798 }
799 
800 static bool is_rk818_bat_first_poweron(struct battery_info *di)
801 {
802 	u8 buf;
803 
804 	buf = rk818_bat_read(GGSTS_REG);
805 	if (buf & BAT_CON) {
806 		buf &= ~BAT_CON;
807 		rk818_bat_write(GGSTS_REG, buf);
808 		return true;
809 	}
810 
811 	return false;
812 }
813 
814 static bool rk818_bat_ocv_sw_reset(struct battery_info *di)
815 {
816 	u8 buf;
817 
818 	buf = rk818_bat_read(MISC_MARK_REG);
819 	if (((buf & FG_RESET_LATE) && di->pwroff_min >= 30) ||
820 	    (buf & FG_RESET_NOW)) {
821 		buf &= ~FG_RESET_LATE;
822 		buf &= ~FG_RESET_NOW;
823 		rk818_bat_write(MISC_MARK_REG, buf);
824 		BAT_INFO("manual reset fuel gauge\n");
825 		return true;
826 	} else {
827 		return false;
828 	}
829 }
830 
831 void rk818_bat_init_rsoc(struct battery_info *di)
832 {
833 	di->pwroff_min = rk818_bat_get_pwroff_min(di);
834 	di->is_first_power_on = is_rk818_bat_first_poweron(di);
835 	di->is_sw_reset = rk818_bat_ocv_sw_reset(di);
836 
837 	if (di->is_first_power_on || di->is_sw_reset)
838 		rk818_bat_first_pwron(di);
839 	else
840 		rk818_bat_not_first_pwron(di);
841 }
842 
843 static int rk818_bat_calc_linek(struct battery_info *di)
844 {
845 	int linek, diff, delta;
846 
847 	di->calc_dsoc = di->dsoc;
848 	di->calc_rsoc = di->rsoc;
849 	di->sm_old_cap = di->remain_cap;
850 
851 	delta = abs(di->dsoc - di->rsoc);
852 	diff = delta * 3;
853 	di->sm_meet_soc = (di->dsoc >= di->rsoc) ?
854 			   (di->dsoc + diff) : (di->rsoc + diff);
855 
856 	if (di->dsoc < di->rsoc)
857 		linek = 1000 * (delta + diff) / DIV(diff);
858 	else if (di->dsoc > di->rsoc)
859 		linek = 1000 * diff / DIV(delta + diff);
860 	else
861 		linek = 1000;
862 
863 	di->sm_chrg_dsoc = di->dsoc * 1000;
864 
865 	DBG("<%s>. meet=%d, diff=%d, link=%d, calc: dsoc=%d, rsoc=%d\n",
866 	    __func__, di->sm_meet_soc, diff, linek,
867 	    di->calc_dsoc, di->calc_rsoc);
868 
869 	return linek;
870 }
871 
872 static void rk818_bat_init_ts2(struct battery_info *di)
873 {
874 	u8 buf;
875 
876 	if (!di->ts2_vol_multi)
877 		return;
878 
879 	/* TS2 adc mode */
880 	buf = rk818_bat_read(TS_CTRL_REG);
881 	buf |= TS2_ADC_MODE;
882 	rk818_bat_write(TS_CTRL_REG, buf);
883 
884 	/* TS2 adc enable */
885 	buf = rk818_bat_read(ADC_CTRL_REG);
886 	buf |= ADC_TS2_EN;
887 	rk818_bat_write(ADC_CTRL_REG, buf);
888 }
889 
890 static int rk818_fg_init(struct battery_info *di)
891 {
892 	rk818_bat_enable_gauge(di);
893 	rk818_bat_init_voltage_kb(di);
894 	rk818_bat_init_coffset(di);
895 	rk818_bat_init_ts2(di);
896 	rk818_bat_clr_initialized_state(di);
897 	di->dsoc = rk818_bat_get_dsoc(di);
898 
899 	/*
900 	 * it's better to init fg in kernel,
901 	 * so avoid init in uboot as far as possible
902 	 */
903 	if (rk818_bat_get_usb_state(di) != NO_CHARGER) {
904 		if (rk818_bat_get_est_voltage(di) < CONFIG_SCREEN_ON_VOL_THRESD)
905 			rk818_bat_init_rsoc(di);
906 #ifdef CONFIG_UBOOT_CHARGE
907 		else
908 			rk818_bat_init_rsoc(di);
909 #endif
910 	}
911 
912 	rk818_bat_init_chrg_config(di);
913 	di->voltage_avg = rk818_bat_get_avg_voltage(di);
914 	di->voltage_ocv = rk818_bat_get_ocv_voltage(di);
915 	di->current_avg = rk818_bat_get_avg_current(di);
916 	di->sm_linek = rk818_bat_calc_linek(di);
917 	di->finish_chrg_base = get_timer(0);
918 	di->term_sig_base = get_timer(0);
919 	di->pwr_vol = di->voltage_avg;
920 
921 	return 0;
922 }
923 
924 static bool is_rk818_bat_exist(struct  battery_info *di)
925 {
926 	return (rk818_bat_read(SUP_STS_REG) & BAT_EXS) ? true : false;
927 }
928 
929 static void rk818_bat_set_current(int input_current)
930 {
931 	u8 usb_ctrl;
932 
933 	usb_ctrl = rk818_bat_read(USB_CTRL_REG);
934 	usb_ctrl &= ~INPUT_CUR_MSK;
935 	usb_ctrl |= (input_current);
936 	rk818_bat_write(USB_CTRL_REG, usb_ctrl);
937 }
938 
939 static int rk818_bat_get_ts2_voltage(struct battery_info *di)
940 {
941 	u32 val = 0;
942 
943 	val |= rk818_bat_read(RK818_TS2_ADC_REGL) << 0;
944 	val |= rk818_bat_read(RK818_TS2_ADC_REGH) << 8;
945 
946 	/* refer voltage 2.2V, 12bit adc accuracy */
947 	val = val * 2200 * di->ts2_vol_multi / 4095;
948 	DBG("<%s>. ts2 voltage=%d\n", __func__, val);
949 
950 	return val;
951 }
952 
953 static void rk818_bat_ts2_update_current(struct battery_info *di)
954 {
955 	int ts2_vol, input_current, invalid_cnt = 0, confirm_cnt = 0;
956 
957 	rk818_bat_set_current(ILIM_450MA);
958 	input_current = ILIM_850MA;
959 	while (input_current < di->chrg_cur_input) {
960 		mdelay(100);
961 		ts2_vol = rk818_bat_get_ts2_voltage(di);
962 		DBG("******** ts2 vol=%d\n", ts2_vol);
963 		/* filter invalid voltage */
964 		if (ts2_vol <= TS2_VALID_VOL) {
965 			invalid_cnt++;
966 			DBG("%s: invalid ts2 voltage: %d\n, cnt=%d",
967 			    __func__, ts2_vol, invalid_cnt);
968 			if (invalid_cnt < TS2_CHECK_CNT)
969 				continue;
970 
971 			/* if fail, set max input current as default */
972 			input_current = di->chrg_cur_input;
973 			rk818_bat_set_current(input_current);
974 			break;
975 		}
976 
977 		/* update input current */
978 		if (ts2_vol >= TS2_THRESHOLD_VOL) {
979 			/* update input current */
980 			input_current++;
981 			rk818_bat_set_current(input_current);
982 			DBG("********* input=%d\n",
983 			    CHRG_CUR_INPUT[input_current & 0x0f]);
984 		} else {
985 			/* confirm lower threshold voltage */
986 			confirm_cnt++;
987 			if (confirm_cnt < TS2_CHECK_CNT) {
988 				DBG("%s: confirm ts2 voltage: %d\n, cnt=%d",
989 				    __func__, ts2_vol, confirm_cnt);
990 				continue;
991 			}
992 
993 			/* trigger threshold, so roll back 1 step */
994 			input_current--;
995 			if (input_current == ILIM_80MA ||
996 			    input_current < 0)
997 				input_current = ILIM_450MA;
998 			rk818_bat_set_current(input_current);
999 			break;
1000 		}
1001 	}
1002 
1003 	BAT_INFO("DC_CHARGER charge_cur_input=%d\n",
1004 		 CHRG_CUR_INPUT[input_current]);
1005 }
1006 
1007 static void rk818_bat_charger_setting(struct battery_info *di, int charger)
1008 {
1009 	static u8 old_charger = UNDEF_CHARGER;
1010 
1011 	/* charger changed */
1012 	if (old_charger != charger) {
1013 		if (charger == NO_CHARGER) {
1014 			BAT_INFO("NO_CHARGER\n");
1015 			rk818_bat_set_current(ILIM_450MA);
1016 		} else if (charger == USB_CHARGER) {
1017 			BAT_INFO("USB_CHARGER\n");
1018 			rk818_bat_set_current(ILIM_450MA);
1019 		} else if (charger == DC_CHARGER || charger == AC_CHARGER) {
1020 #ifdef CONFIG_UBOOT_CHARGE
1021 			if (di->ts2_vol_multi) {
1022 #else
1023 			if ((rk818_bat_get_est_voltage(di) <
1024 			     CONFIG_SCREEN_ON_VOL_THRESD) &&
1025 			    (di->ts2_vol_multi)) {
1026 #endif
1027 				rk818_bat_ts2_update_current(di);
1028 			} else {
1029 				rk818_bat_set_current(di->chrg_cur_input);
1030 				BAT_INFO("DC_CHARGER charge_cur_input=%d\n",
1031 					 CHRG_CUR_INPUT[di->chrg_cur_input]);
1032 			}
1033 		} else {
1034 			BAT_INFO("charger setting error %d\n", charger);
1035 		}
1036 
1037 		old_charger = charger;
1038 	}
1039 }
1040 
1041 static int rk818_bat_get_dc_state(struct battery_info *di)
1042 {
1043 	if (!di->dc_is_valid)
1044 		return NO_CHARGER;
1045 
1046 	return dm_gpio_get_value(di->dc_det) ? DC_CHARGER : NO_CHARGER;
1047 }
1048 
1049 static int rk818_bat_get_charger_type(struct battery_info *di)
1050 {
1051 	int charger_type = NO_CHARGER;
1052 
1053 	/* check by ic hardware: this check make check work safer */
1054 	if ((rk818_bat_read(VB_MON_REG) & PLUG_IN_STS) == 0)
1055 		return NO_CHARGER;
1056 
1057 	/* virtual or bat not exist */
1058 	if (di->virtual_power)
1059 		return DC_CHARGER;
1060 
1061 	/* check DC firstly */
1062 	charger_type = rk818_bat_get_dc_state(di);
1063 	if (charger_type == DC_CHARGER)
1064 		return charger_type;
1065 
1066 	/* check USB secondly */
1067 	return rk818_bat_get_usb_state(di);
1068 }
1069 
1070 static void rk818_bat_save_dsoc(struct battery_info *di, u8 save_soc)
1071 {
1072 	static int old_soc = -1;
1073 
1074 	if (old_soc != save_soc) {
1075 		old_soc = save_soc;
1076 		rk818_bat_write(SOC_REG, save_soc);
1077 	}
1078 }
1079 
1080 static void rk818_bat_save_cap(struct battery_info *di, int cap)
1081 {
1082 	u8 buf;
1083 	static int old_cap;
1084 
1085 	if (old_cap == cap)
1086 		return;
1087 
1088 	if (cap >= di->qmax)
1089 		cap = di->qmax;
1090 
1091 	old_cap = cap;
1092 	buf = (cap >> 24) & 0xff;
1093 	rk818_bat_write(REMAIN_CAP_REG3, buf);
1094 	buf = (cap >> 16) & 0xff;
1095 	rk818_bat_write(REMAIN_CAP_REG2, buf);
1096 	buf = (cap >> 8) & 0xff;
1097 	rk818_bat_write(REMAIN_CAP_REG1, buf);
1098 	buf = (cap >> 0) & 0xff;
1099 	rk818_bat_write(REMAIN_CAP_REG0, buf);
1100 }
1101 
1102 static u8 rk818_bat_get_chrg_status(struct battery_info *di)
1103 {
1104 	u8 status;
1105 
1106 	status = rk818_bat_read(SUP_STS_REG) & BAT_STATUS_MSK;
1107 	switch (status) {
1108 	case CHARGE_OFF:
1109 		DBG("CHARGE-OFF...\n");
1110 		break;
1111 	case DEAD_CHARGE:
1112 		DBG("DEAD CHARGE...\n");
1113 		break;
1114 	case  TRICKLE_CHARGE:
1115 		DBG("TRICKLE CHARGE...\n ");
1116 		break;
1117 	case  CC_OR_CV:
1118 		DBG("CC or CV...\n");
1119 		break;
1120 	case  CHARGE_FINISH:
1121 		DBG("CHARGE FINISH...\n");
1122 		break;
1123 	case  USB_OVER_VOL:
1124 		DBG("USB OVER VOL...\n");
1125 		break;
1126 	case  BAT_TMP_ERR:
1127 		DBG("BAT TMP ERROR...\n");
1128 		break;
1129 	case  TIMER_ERR:
1130 		DBG("TIMER ERROR...\n");
1131 		break;
1132 	case  USB_EXIST:
1133 		DBG("USB EXIST...\n");
1134 		break;
1135 	case  USB_EFF:
1136 		DBG("USB EFF...\n");
1137 		break;
1138 	default:
1139 		return -EINVAL;
1140 	}
1141 
1142 	return status;
1143 }
1144 
1145 static void rk818_bat_finish_chrg(struct battery_info *di)
1146 {
1147 	u32 tgt_sec = 0;
1148 
1149 	if (di->dsoc < 100) {
1150 		tgt_sec = di->fcc * 3600 / 100 / FINISH_CALI_CURR;
1151 		if (get_timer(di->finish_chrg_base) > SECONDS(tgt_sec)) {
1152 			di->finish_chrg_base = get_timer(0);
1153 			di->dsoc++;
1154 		}
1155 	}
1156 	DBG("<%s>. sec=%d, finish_sec=%lu\n", __func__, SECONDS(tgt_sec),
1157 	    get_timer(di->finish_chrg_base));
1158 }
1159 
1160 static void rk818_bat_debug_info(struct battery_info *di)
1161 {
1162 	u8 sup_sts, ggcon, ggsts, vb_mod, rtc, thermal, misc;
1163 	u8 usb_ctrl, chrg_ctrl1, chrg_ctrl2, chrg_ctrl3;
1164 	static const char *name[] = {"NONE", "USB", "AC", "DC", "UNDEF"};
1165 
1166 	if (!dbg_enable)
1167 		return;
1168 	ggcon = rk818_bat_read(GGCON_REG);
1169 	ggsts = rk818_bat_read(GGSTS_REG);
1170 	sup_sts = rk818_bat_read(SUP_STS_REG);
1171 	usb_ctrl = rk818_bat_read(USB_CTRL_REG);
1172 	thermal = rk818_bat_read(THERMAL_REG);
1173 	vb_mod = rk818_bat_read(VB_MON_REG);
1174 	misc = rk818_bat_read(MISC_MARK_REG);
1175 	rtc = rk818_bat_read(SECONDS_REG);
1176 	chrg_ctrl1 = rk818_bat_read(CHRG_CTRL_REG1);
1177 	chrg_ctrl2 = rk818_bat_read(CHRG_CTRL_REG2);
1178 	chrg_ctrl3 = rk818_bat_read(CHRG_CTRL_REG3);
1179 
1180 	DBG("\n---------------------- DEBUG REGS ------------------------\n"
1181 	    "GGCON=0x%2x, GGSTS=0x%2x, RTC=0x%2x, SUP_STS= 0x%2x\n"
1182 	    "VB_MOD=0x%2x, USB_CTRL=0x%2x, THERMAL=0x%2x, MISC=0x%2x\n"
1183 	    "CHRG_CTRL:REG1=0x%2x, REG2=0x%2x, REG3=0x%2x\n",
1184 	    ggcon, ggsts, rtc, sup_sts, vb_mod, usb_ctrl,
1185 	    thermal, misc, chrg_ctrl1, chrg_ctrl2, chrg_ctrl3
1186 	    );
1187 	DBG("----------------------------------------------------------\n"
1188 	    "Dsoc=%d, Rsoc=%d, Vavg=%d, Iavg=%d, Cap=%d, Fcc=%d, d=%d\n"
1189 	    "K=%d, old_cap=%d, charger=%s, Is=%d, Ip=%d, Vs=%d\n"
1190 	    "min=%d, meet: soc=%d, calc: dsoc=%d, rsoc=%d, Vocv=%d\n"
1191 	    "off: i=0x%x, c=0x%x, max=%d, ocv_c=%d, halt: st=%d, cnt=%d\n"
1192 	    "pwr: dsoc=%d, rsoc=%d, vol=%d, Res=%d, exist=%d\n",
1193 	    di->dsoc, rk818_bat_get_rsoc(di), rk818_bat_get_avg_voltage(di),
1194 	    rk818_bat_get_avg_current(di), di->remain_cap, di->fcc,
1195 	    di->rsoc - di->dsoc,
1196 	    di->sm_linek, di->sm_old_cap, name[di->chrg_type],
1197 	    di->res_div * CHRG_CUR_SEL[chrg_ctrl1 & 0x0f],
1198 	    CHRG_CUR_INPUT[usb_ctrl & 0x0f],
1199 	    CHRG_VOL_SEL[(chrg_ctrl1 & 0x70) >> 4],  di->pwroff_min,
1200 	    di->sm_meet_soc, di->calc_dsoc, di->calc_rsoc,
1201 	    rk818_bat_get_ocv_voltage(di), rk818_bat_get_ioffset(di),
1202 	    rk818_bat_get_coffset(di), di->is_max_soc_offset,
1203 	    di->is_ocv_calib, di->is_halt, di->halt_cnt, di->pwr_dsoc,
1204 	    di->pwr_rsoc, di->pwr_vol, di->sample_res, is_rk818_bat_exist(di)
1205 	    );
1206 	rk818_bat_get_chrg_status(di);
1207 	DBG("###########################################################\n");
1208 }
1209 
1210 static void rk818_bat_linek_algorithm(struct battery_info *di)
1211 {
1212 	int delta_cap, ydsoc, tmp;
1213 	u8 chg_st = rk818_bat_get_chrg_status(di);
1214 
1215 	/* slow down */
1216 	if (di->dsoc == 99)
1217 		di->sm_linek = CHRG_FULL_K;
1218 	else if (di->dsoc >= CHRG_TERM_DSOC && di->current_avg > TERM_CALI_CURR)
1219 		di->sm_linek = CHRG_TERM_K;
1220 
1221 	delta_cap = di->remain_cap - di->sm_old_cap;
1222 	ydsoc = di->sm_linek * delta_cap * 100 / DIV(di->fcc);
1223 	if (ydsoc > 0) {
1224 		tmp = (di->sm_chrg_dsoc + 1) / 1000;
1225 		if (tmp != di->dsoc)
1226 			di->sm_chrg_dsoc = di->dsoc * 1000;
1227 		di->sm_chrg_dsoc += ydsoc;
1228 		di->dsoc = (di->sm_chrg_dsoc + 1) / 1000;
1229 		di->sm_old_cap = di->remain_cap;
1230 		if (di->dsoc == di->rsoc && di->sm_linek != CHRG_FULL_K &&
1231 		    di->sm_linek != CHRG_TERM_K)
1232 			di->sm_linek = 1000;
1233 	}
1234 
1235 	if ((di->sm_linek == 1000 || di->dsoc >= 100) &&
1236 	    (chg_st != CHARGE_FINISH)) {
1237 		if (di->sm_linek == 1000)
1238 			di->dsoc = di->rsoc;
1239 		di->sm_chrg_dsoc = di->dsoc * 1000;
1240 	}
1241 
1242 	DBG("linek=%d, sm_dsoc=%d, delta_cap=%d, ydsoc=%d, old_cap=%d\n"
1243 	    "calc: dsoc=%d, rsoc=%d, meet=%d\n",
1244 	    di->sm_linek, di->sm_chrg_dsoc, delta_cap, ydsoc, di->sm_old_cap,
1245 	    di->calc_dsoc, di->calc_rsoc, di->sm_meet_soc);
1246 }
1247 
1248 static void rk818_bat_set_term_mode(struct battery_info *di, int mode)
1249 {
1250 	u8 buf;
1251 
1252 	buf = rk818_bat_read(CHRG_CTRL_REG3);
1253 	buf &= ~CHRG_TERM_SIG_MSK;
1254 	buf |= mode;
1255 	rk818_bat_write(CHRG_CTRL_REG3, buf);
1256 
1257 	DBG("set charge to %s term mode\n", mode ? "digital" : "analog");
1258 }
1259 
1260 static int rk818_bat_get_iadc(struct battery_info *di)
1261 {
1262 	int val = 0;
1263 
1264 	val |= rk818_bat_read(BAT_CUR_AVG_REGL) << 0;
1265 	val |= rk818_bat_read(BAT_CUR_AVG_REGH) << 8;
1266 	if (val > 2047)
1267 		val -= 4096;
1268 
1269 	return val;
1270 }
1271 
1272 static bool rk818_bat_adc_calib(struct battery_info *di)
1273 {
1274 	int i, ioffset, coffset, adc;
1275 
1276 	if (abs(di->current_avg) < ADC_CALIB_THRESHOLD)
1277 		return false;
1278 
1279 	for (i = 0; i < 5; i++) {
1280 		adc = rk818_bat_get_iadc(di);
1281 		coffset = rk818_bat_get_coffset(di);
1282 		rk818_bat_set_coffset(di, coffset + adc);
1283 		mdelay(200);
1284 		adc = rk818_bat_get_iadc(di);
1285 		if (abs(adc) < ADC_CALIB_THRESHOLD) {
1286 			coffset = rk818_bat_get_coffset(di);
1287 			ioffset = rk818_bat_get_ioffset(di);
1288 			di->poffset = coffset - ioffset;
1289 			rk818_bat_write(POFFSET_REG, di->poffset);
1290 			BAT_INFO("new offset:c=0x%x, i=0x%x, p=0x%x\n",
1291 				 coffset, ioffset, di->poffset);
1292 			return true;
1293 		} else {
1294 			BAT_INFO("coffset calib again %d..\n", i);
1295 			rk818_bat_set_coffset(di, coffset);
1296 			mdelay(200);
1297 		}
1298 	}
1299 
1300 	return false;
1301 }
1302 
1303 static void rk818_bat_smooth_charge(struct battery_info *di)
1304 {
1305 	u8 chg_st = rk818_bat_get_chrg_status(di);
1306 
1307 	/* set terminal charge mode */
1308 	if (di->term_sig_base && get_timer(di->term_sig_base) > SECONDS(1)) {
1309 		rk818_bat_set_term_mode(di, CHRG_TERM_DIG_SIGNAL);
1310 		di->term_sig_base = 0;
1311 	}
1312 
1313 	/* not charge mode and not keep in uboot charge: exit */
1314 	if ((di->chrg_type == NO_CHARGER) ||
1315 	    !rk818_bat_is_initialized(di)) {
1316 		DBG("chrg=%d, initialized=%d\n", di->chrg_type,
1317 		    rk818_bat_is_initialized(di));
1318 		goto out;
1319 	}
1320 
1321 	/* update rsoc and remain cap */
1322 	di->remain_cap = rk818_bat_get_coulomb_cap(di);
1323 	di->rsoc = rk818_bat_get_rsoc(di);
1324 	if (di->remain_cap > di->fcc) {
1325 		di->sm_old_cap -= (di->remain_cap - di->fcc);
1326 		rk818_bat_init_capacity(di, di->fcc);
1327 	}
1328 
1329 	/* finish charge step */
1330 	if (chg_st == CHARGE_FINISH) {
1331 		DBG("finish charge step...\n");
1332 		if (di->adc_allow_update)
1333 			di->adc_allow_update = !rk818_bat_adc_calib(di);
1334 		rk818_bat_finish_chrg(di);
1335 		rk818_bat_init_capacity(di, di->fcc);
1336 	} else {
1337 		DBG("smooth charge step...\n");
1338 		di->adc_allow_update = true;
1339 		di->finish_chrg_base = get_timer(0);
1340 		rk818_bat_linek_algorithm(di);
1341 	}
1342 
1343 	/* dsoc limit */
1344 	if (di->dsoc > 100)
1345 		di->dsoc = 100;
1346 	else if (di->dsoc < 0)
1347 		di->dsoc = 0;
1348 
1349 	rk818_bat_save_dsoc(di, di->dsoc);
1350 	rk818_bat_save_cap(di, di->remain_cap);
1351 out:
1352 	rk818_bat_debug_info(di);
1353 }
1354 
1355 static int rk818_bat_update_get_soc(struct udevice *dev)
1356 {
1357 	struct battery_info *di = dev_get_priv(dev);
1358 	static ulong seconds;
1359 
1360 	/* set charge current */
1361 	di->chrg_type =
1362 		rk818_bat_get_charger_type(di);
1363 	rk818_bat_charger_setting(di, di->chrg_type);
1364 
1365 	/* fg calc every 5 seconds */
1366 	if (!seconds)
1367 		seconds = get_timer(0);
1368 	if (get_timer(seconds) >= SECONDS(5)) {
1369 		seconds = get_timer(0);
1370 		rk818_bat_smooth_charge(di);
1371 	}
1372 
1373 	/* bat exist, fg init success(dts pass) and uboot charge: report data */
1374 	if (!di->virtual_power && di->voltage_k)
1375 		return di->dsoc;
1376 	else
1377 		return VIRTUAL_POWER_SOC;
1378 }
1379 
1380 static int rk818_bat_update_get_voltage(struct udevice *dev)
1381 {
1382 	struct battery_info *di = dev_get_priv(dev);
1383 
1384 	if (!di->virtual_power && di->voltage_k)
1385 		return rk818_bat_get_est_voltage(di);
1386 	else
1387 		return VIRTUAL_POWER_VOL;
1388 }
1389 
1390 static bool rk818_bat_update_get_chrg_online(struct udevice *dev)
1391 {
1392 	return rk818_bat_dwc_otg_check_dpdm();
1393 }
1394 
1395 static struct dm_fuel_gauge_ops fg_ops = {
1396 	.get_soc = rk818_bat_update_get_soc,
1397 	.get_voltage = rk818_bat_update_get_voltage,
1398 	.get_chrg_online = rk818_bat_update_get_chrg_online,
1399 };
1400 
1401 static int rk818_bat_parse_dt(struct battery_info *di, void const *blob)
1402 {
1403 	int node, parent, len, err;
1404 	const char *prop;
1405 
1406 	parent = fdt_node_offset_by_compatible(blob, 0, "rockchip,rk818");
1407 	if (parent < 0) {
1408 		printf("can't find rockchip,rk818 node\n");
1409 		return -ENODEV;
1410 	}
1411 
1412 	if (!fdtdec_get_is_enabled(blob, parent)) {
1413 		DBG("rk818 node disabled\n");
1414 		return -ENODEV;
1415 	}
1416 
1417 	node = fdt_subnode_offset_namelen(blob, parent, "battery", 7);
1418 	if (node < 0) {
1419 		printf("can't find battery node\n");
1420 		di->chrg_cur_input = ILIM_2000MA;
1421 		return -EINVAL;
1422 	}
1423 
1424 	prop = fdt_getprop(blob, node, "ocv_table", &len);
1425 	if (!prop) {
1426 		printf("can't find ocv_table prop\n");
1427 		return -EINVAL;
1428 	}
1429 
1430 	di->ocv_table = calloc(len, 1);
1431 	if (!di->ocv_table) {
1432 		printf("can't calloc ocv_table\n");
1433 		return -ENOMEM;
1434 	}
1435 
1436 	di->ocv_size = len / 4;
1437 	err = fdtdec_get_int_array(blob, node, "ocv_table",
1438 				   di->ocv_table, di->ocv_size);
1439 	if (err < 0) {
1440 		printf("read ocv_table error\n");
1441 		free(di->ocv_table);
1442 		return -EINVAL;
1443 	}
1444 
1445 	di->design_cap = fdtdec_get_int(blob, node, "design_capacity", -1);
1446 	if (di->design_cap < 0) {
1447 		printf("read design_capacity error\n");
1448 		return -EINVAL;
1449 	}
1450 
1451 	di->qmax = fdtdec_get_int(blob, node, "design_qmax", -1);
1452 	if (di->qmax < 0) {
1453 		printf("read design_qmax error\n");
1454 		return -EINVAL;
1455 	}
1456 
1457 	di->dts_vol_sel = fdtdec_get_int(blob, node, "max_chrg_voltage", 4200);
1458 	di->dts_cur_input = fdtdec_get_int(blob, node,
1459 					   "max_input_current", 2000);
1460 	di->dts_cur_sel = fdtdec_get_int(blob, node, "max_chrg_current", 1200);
1461 	di->sample_res = fdtdec_get_int(blob, node, "sample_res",
1462 					SAMPLE_RES_20mR);
1463 	di->res_div = (di->sample_res == SAMPLE_RES_20mR) ?
1464 		       SAMPLE_RES_DIV1 : SAMPLE_RES_DIV2;
1465 	di->max_soc_offset = fdtdec_get_int(blob, node, "max_soc_offset", 70);
1466 	di->virtual_power = fdtdec_get_int(blob, node, "virtual_power", 0);
1467 	di->ts2_vol_multi = fdtdec_get_int(blob, node, "ts2_vol_multi", 0);
1468 	di->bat_res = fdtdec_get_int(blob, node, "bat_res", 135);
1469 	if (!gpio_request_by_name_nodev(offset_to_ofnode(node), "dc_det_gpio",
1470 					0, di->dc_det, GPIOD_IS_IN)) {
1471 		di->dc_is_valid = true;
1472 	}
1473 
1474 	if (!is_rk818_bat_exist(di))
1475 		di->virtual_power = 1;
1476 
1477 	DBG("-------------------------------:\n");
1478 	DBG("max_input_current:%d\n", di->dts_cur_input);
1479 	DBG("max_chrg_current:%d\n", di->dts_cur_sel);
1480 	DBG("max_chrg_voltage:%d\n", di->dts_vol_sel);
1481 	DBG("design_capacity :%d\n", di->design_cap);
1482 	DBG("design_qmax:%d\n", di->qmax);
1483 	DBG("max_soc_offset:%d\n", di->max_soc_offset);
1484 	DBG("sample_res:%d\n", di->sample_res);
1485 
1486 	return 0;
1487 }
1488 
1489 static int rk818_fg_probe(struct udevice *dev)
1490 {
1491 	struct rk8xx_priv *priv = dev_get_priv(dev->parent);
1492 	struct battery_info *di = dev_get_priv(dev);
1493 	int ret;
1494 
1495 	if (priv->variant != 0x8180) {
1496 		debug("Not support pmic variant: rk%x\n", priv->variant);
1497 		return -EINVAL;
1498 	}
1499 
1500 	g_pmic_dev = dev->parent;
1501 	ret = rk818_bat_parse_dt(di, gd->fdt_blob);
1502 	if (ret)
1503 		return ret;
1504 
1505 	return rk818_fg_init(di);
1506 }
1507 
1508 U_BOOT_DRIVER(rk818_fg) = {
1509 	.name = "rk818_fg",
1510 	.id = UCLASS_FG,
1511 	.probe = rk818_fg_probe,
1512 	.ops = &fg_ops,
1513 	.priv_auto_alloc_size = sizeof(struct battery_info),
1514 };
1515