xref: /rk3399_rockchip-uboot/drivers/power/fuel_gauge/fg_rk818.c (revision b68be486b3f753dfd38b304cdf2f4e8c1b8a4a19)
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 <dm/uclass-internal.h>
16 #include <power/charge_display.h>
17 #include <power/charge_animation.h>
18 #include <power/fuel_gauge.h>
19 #include <power/rk8xx_pmic.h>
20 #include <linux/usb/phy-rockchip-inno-usb2.h>
21 #include "fg_regs.h"
22 
23 DECLARE_GLOBAL_DATA_PTR;
24 
25 static int dbg_enable = 0;
26 #define DBG(args...) \
27 	do { \
28 		if (dbg_enable) { \
29 			printf(args); \
30 		} \
31 	} while (0)
32 
33 #define BAT_INFO(fmt, args...) printf("rk818-bat: "fmt, ##args)
34 
35 #define DRIVER_VERSION		"2.0"
36 
37 /* THERMAL_REG */
38 #define TEMP_105C		(0x02 << 2)
39 #define FB_TEMP_MSK		0x0c
40 
41 /* CHRG_CTRL_REG2 */
42 #define FINISH_100MA		(0x00 << 6)
43 #define FINISH_150MA		(0x01 << 6)
44 #define FINISH_200MA		(0x02 << 6)
45 #define FINISH_250MA		(0x03 << 6)
46 #define FINISH_CUR_MSK		0xc7
47 
48 /* CHRG_CTRL_REG3 */
49 #define CHRG_TERM_DIG_SIGNAL	(1 << 5)
50 #define CHRG_TERM_ANA_SIGNAL	(0 << 5)
51 #define CHRG_TIMER_CCCV_EN	(1 << 2)
52 #define CHRG_TERM_SIG_MSK	(1 << 5)
53 
54 /* CHRG_CTRL_REG */
55 #define ILIM_450MA		(0x00)
56 #define ILIM_80MA		(0x01)
57 #define ILIM_850MA		(0x02)
58 #define ILIM_2000MA		(0x07)
59 #define CHRG_CT_EN		(1 << 7)
60 
61 /* USB_CTRL_REG */
62 #define INPUT_CUR_MSK		0x0f
63 
64 /* VB_MON_REG */
65 #define PLUG_IN_STS		(1 << 6)
66 
67 /* GGSTS */
68 #define BAT_CON			(1 << 4)
69 #define VOL_INSTANT		(1 << 0)
70 #define VOL_AVG			(0 << 0)
71 
72 /* TS_CTRL_REG */
73 #define GG_EN			(1 << 7)
74 
75 /* CHRG_USB_CTRL */
76 #define CHRG_EN			(1 << 7)
77 
78 /* ADC_CTRL_REG */
79 #define ADC_TS2_EN		(1 << 4)
80 #define ADC_TS1_EN		(1 << 5)
81 
82 /* TS_CTRL_REG */
83 #define TS2_ADC_MODE		(1 << 5)
84 
85 /* SUP_STS_REG */
86 #define BAT_EXS			(1 << 7)
87 #define USB_EXIST		(1 << 1)
88 #define USB_EFF			(1 << 0)
89 #define CHARGE_OFF		(0x00 << 4)
90 #define DEAD_CHARGE		(0x01 << 4)
91 #define TRICKLE_CHARGE		(0x02 << 4)
92 #define CC_OR_CV		(0x03 << 4)
93 #define CHARGE_FINISH		(0x04 << 4)
94 #define USB_OVER_VOL		(0x05 << 4)
95 #define BAT_TMP_ERR		(0x06 << 4)
96 #define TIMER_ERR		(0x07 << 4)
97 #define USB_VLIMIT_EN		(1 << 3)
98 #define USB_CLIMIT_EN		(1 << 2)
99 #define BAT_STATUS_MSK		0x70
100 
101 /* GGCON */
102 #define ADC_CUR_MODE		(1 << 1)
103 
104 /* CALI PARAM */
105 #define FINISH_CALI_CURR	1500
106 #define TERM_CALI_CURR		600
107 #define	VIRTUAL_POWER_VOL	4200
108 #define	VIRTUAL_POWER_CUR	1000
109 #define	VIRTUAL_POWER_SOC	66
110 #define SECONDS(n)		((n) * 1000)
111 
112 /* CALC PARAM */
113 #define MAX_PERCENTAGE		100
114 #define MAX_INTERPOLATE		1000
115 #define MAX_INT			0x7fff
116 #define MIN_FCC			500
117 
118 /* sample resistor and division */
119 #define SAMPLE_RES_10mR		10
120 #define SAMPLE_RES_20mR		20
121 #define SAMPLE_RES_DIV1		1
122 #define SAMPLE_RES_DIV2		2
123 
124 #define FG_INIT			(1 << 5)
125 #define FG_RESET_LATE		(1 << 4)
126 #define FG_RESET_NOW		(1 << 3)
127 
128 #define DEFAULT_POFFSET		42
129 #define DEFAULT_COFFSET		0x832
130 #define INVALID_COFFSET_MIN	0x780
131 #define INVALID_COFFSET_MAX	0x980
132 
133 #define CHRG_TERM_DSOC		90
134 #define CHRG_TERM_K		650
135 #define CHRG_FULL_K		400
136 #define ADC_CALIB_THRESHOLD	4
137 
138 #define TS2_THRESHOLD_VOL	4350
139 #define TS2_VALID_VOL		1000
140 #define TS2_VOL_MULTI		0
141 #define TS2_CHECK_CNT		5
142 
143 #define ADC_CUR_MSK		0x03
144 #define ADC_CUR_20UA		0x00
145 #define ADC_CUR_40UA		0x01
146 #define ADC_CUR_60UA		0x02
147 #define ADC_CUR_80UA		0x03
148 
149 #define NTC_CALC_FACTOR_80UA	7
150 #define NTC_CALC_FACTOR_60UA	9
151 #define NTC_CALC_FACTOR_40UA	13
152 #define NTC_CALC_FACTOR_20UA	27
153 #define NTC_80UA_MAX_MEASURE	27500
154 #define NTC_60UA_MAX_MEASURE	36666
155 #define NTC_40UA_MAX_MEASURE	55000
156 #define NTC_20UA_MAX_MEASURE	110000
157 
158 #define ZERO_MIN_VOLTAGE	3800
159 
160 #define TS1_NOT_READY		0xabcdabcd
161 
162 #define DIV(x)			((x) ? (x) : 1)
163 
164 /***********************************************************/
165 struct battery_priv {
166 	struct udevice *dev;
167 	int		chrg_type;
168 	int		poffset;
169 	int		bat_res;
170 	int		current_avg;
171 	int		voltage_avg;
172 	int		voltage_ocv;
173 	int		voltage_k;
174 	int		voltage_b;
175 	int		dsoc;
176 	int		rsoc;
177 	int		fcc;
178 	int		qmax;
179 	int		remain_cap;
180 	int		design_cap;
181 	int		nac;
182 	u32		*ocv_table;
183 	u32		ocv_size;
184 	u32		*ntc_table;
185 	u32		ntc_size;
186 	u32		ntc_factor;
187 	u32		ntc_uA;
188 	int		ntc_degree_from;
189 	int		temperature;
190 	int		virtual_power;
191 	int		ts2_vol_multi;
192 	int		pwroff_min;
193 	int		sm_old_cap;
194 	int		sm_linek;
195 	int		sm_chrg_dsoc;
196 	int		adc_allow_update;
197 	int		chrg_vol_sel;
198 	int		chrg_cur_input;
199 	int		chrg_cur_sel;
200 	int		dts_vol_sel;
201 	int		dts_cur_input;
202 	int		dts_cur_sel;
203 	int		max_soc_offset;
204 	int		sample_res;
205 	int		res_div;
206 	struct gpio_desc dc_det;
207 	int		dc_det_adc;
208 	ulong		finish_chrg_base;
209 	ulong		term_sig_base;
210 	u8		calc_dsoc;
211 	u8		calc_rsoc;
212 	int		sm_meet_soc;
213 	u8		halt_cnt;
214 	u8		dc_active_level;
215 	u8		dc_is_valid;
216 	bool		is_halt;
217 	bool		is_ocv_calib;
218 	bool		is_max_soc_offset;
219 	bool		is_first_power_on;
220 	bool		is_sw_reset;
221 	int		pwr_dsoc;
222 	int		pwr_rsoc;
223 	int		pwr_vol;
224 };
225 
226 enum charger_type {
227 	NO_CHARGER = 0,
228 	USB_CHARGER,
229 	AC_CHARGER,
230 	DC_CHARGER,
231 	UNDEF_CHARGER,
232 };
233 
234 static const u32 CHRG_VOL_SEL[] = {
235 	4050, 4100, 4150, 4200, 4250, 4300, 4350
236 };
237 
238 static const u32 CHRG_CUR_SEL[] = {
239 	1000, 1200, 1400, 1600, 1800, 2000, 2250, 2400, 2600, 2800, 3000
240 };
241 
242 static const u32 CHRG_CUR_INPUT[] = {
243 	450, 800, 850, 1000, 1250, 1500, 1750, 2000, 2250, 2500, 2750, 3000
244 };
245 
246 static int rk818_bat_read(struct battery_priv *di, u8 reg)
247 {
248 	return pmic_reg_read(di->dev->parent, reg);
249 }
250 
251 static void rk818_bat_write(struct battery_priv *di, u8 reg, u8 buf)
252 {
253 	pmic_reg_write(di->dev->parent, reg, buf);
254 }
255 
256 static int rk818_bat_dwc_otg_check_dpdm(void)
257 {
258 #ifdef CONFIG_PHY_ROCKCHIP_INNO_USB2
259 	return rockchip_chg_get_type();
260 #else
261 	debug("rockchip_chg_get_type() is not implement\n");
262 	return NO_CHARGER;
263 #endif
264 }
265 
266 static int rk818_bat_get_rsoc(struct battery_priv *di)
267 {
268 	return (di->remain_cap + di->fcc / 200) * 100 / DIV(di->fcc);
269 }
270 
271 static int rk818_bat_get_dsoc(struct  battery_priv *di)
272 {
273 	return rk818_bat_read(di, SOC_REG);
274 }
275 
276 static void rk818_bat_enable_gauge(struct battery_priv *di)
277 {
278 	u8 val;
279 
280 	val = rk818_bat_read(di, TS_CTRL_REG);
281 	val |= GG_EN;
282 	rk818_bat_write(di, TS_CTRL_REG, val);
283 }
284 
285 static int rk818_bat_get_vcalib0(struct battery_priv *di)
286 {
287 	int val = 0;
288 
289 	val |= rk818_bat_read(di, VCALIB0_REGL) << 0;
290 	val |= rk818_bat_read(di, VCALIB0_REGH) << 8;
291 
292 	return val;
293 }
294 
295 static int rk818_bat_get_vcalib1(struct battery_priv *di)
296 {
297 	int val = 0;
298 
299 	val |= rk818_bat_read(di, VCALIB1_REGL) << 0;
300 	val |= rk818_bat_read(di, VCALIB1_REGH) << 8;
301 
302 	return val;
303 }
304 
305 static int rk818_bat_get_ioffset(struct battery_priv *di)
306 {
307 	int val = 0;
308 
309 	val |= rk818_bat_read(di, IOFFSET_REGL) << 0;
310 	val |= rk818_bat_read(di, IOFFSET_REGH) << 8;
311 
312 	DBG("<%s>. ioffset: 0x%x\n", __func__, val);
313 	return val;
314 }
315 
316 static int rk818_bat_get_coffset(struct battery_priv *di)
317 {
318 	int val = 0;
319 
320 	val |= rk818_bat_read(di, CAL_OFFSET_REGL) << 0;
321 	val |= rk818_bat_read(di, CAL_OFFSET_REGH) << 8;
322 
323 	DBG("<%s>. coffset: 0x%x\n", __func__, val);
324 	return val;
325 }
326 
327 static void rk818_bat_set_coffset(struct battery_priv *di, int val)
328 {
329 	u8 buf;
330 
331 	buf = (val >> 0) & 0xff;
332 	rk818_bat_write(di, CAL_OFFSET_REGL, buf);
333 	buf = (val >> 8) & 0xff;
334 	rk818_bat_write(di, CAL_OFFSET_REGH, buf);
335 
336 	DBG("<%s>. set coffset: 0x%x\n", __func__, val);
337 }
338 
339 static void rk818_bat_init_coffset(struct battery_priv *di)
340 {
341 	int ioffset, coffset;
342 
343 	ioffset = rk818_bat_get_ioffset(di);
344 
345 	di->poffset = rk818_bat_read(di, POFFSET_REG);
346 	if (!di->poffset)
347 		di->poffset = DEFAULT_POFFSET;
348 
349 	coffset = di->poffset + ioffset;
350 	if (coffset < INVALID_COFFSET_MIN || coffset > INVALID_COFFSET_MAX)
351 		coffset = DEFAULT_COFFSET;
352 
353 	rk818_bat_set_coffset(di, coffset);
354 }
355 
356 static void rk818_bat_init_voltage_kb(struct battery_priv *di)
357 {
358 	int vcalib0, vcalib1;
359 
360 	vcalib0 = rk818_bat_get_vcalib0(di);
361 	vcalib1 = rk818_bat_get_vcalib1(di);
362 	di->voltage_k = (4200 - 3000) * 1000 / DIV(vcalib1 - vcalib0);
363 	di->voltage_b = 4200 - (di->voltage_k * vcalib1) / 1000;
364 	DBG("%s. vk=%d, vb=%d\n", __func__, di->voltage_k, di->voltage_b);
365 }
366 
367 static int rk818_bat_get_ocv_voltage(struct battery_priv *di)
368 {
369 	int vol, val = 0;
370 
371 	val |= rk818_bat_read(di, BAT_OCV_REGL) << 0;
372 	val |= rk818_bat_read(di, BAT_OCV_REGH) << 8;
373 	vol = di->voltage_k * val / 1000 + di->voltage_b;
374 
375 	return vol;
376 }
377 
378 static int rk818_bat_get_avg_current(struct battery_priv *di)
379 {
380 	int val = 0;
381 
382 	val |= rk818_bat_read(di, BAT_CUR_AVG_REGL) << 0;
383 	val |= rk818_bat_read(di, BAT_CUR_AVG_REGH) << 8;
384 
385 	if (val & 0x800)
386 		val -= 4096;
387 	val = val * di->res_div * 1506 / 1000;
388 
389 	return val;
390 }
391 
392 static int rk818_bat_get_avg_voltage(struct battery_priv *di)
393 {
394 	int vol, val = 0;
395 
396 	val |= rk818_bat_read(di, BAT_VOL_REGL) << 0;
397 	val |= rk818_bat_read(di, BAT_VOL_REGH) << 8;
398 	vol = di->voltage_k * val / 1000 + di->voltage_b;
399 
400 	return vol;
401 }
402 
403 static int rk818_bat_get_est_voltage(struct battery_priv *di)
404 {
405 	struct charge_animation_pdata *pdata = NULL;
406 	struct udevice *dev;
407 	int est_vol, vol, curr;
408 	int plugin, timeout = 0;
409 	int low_power_voltage = 0;
410 
411 	uclass_find_first_device(UCLASS_CHARGE_DISPLAY, &dev);
412 	pdata = dev_get_platdata(dev);
413 	low_power_voltage = pdata->low_power_voltage;
414 
415 	vol = rk818_bat_get_avg_voltage(di);
416 	curr = rk818_bat_get_avg_current(di);
417 	plugin = rk818_bat_read(di, VB_MON_REG) & PLUG_IN_STS ? 1 : 0;
418 	if (di->is_first_power_on || (!plugin && curr >= 0) || (plugin && curr <= 0)) {
419 		DBG("%s: curr=%d, plugin=%d, first_on=%d\n",
420 		    __func__, curr, plugin, di->is_first_power_on);
421 		curr = 0;
422 	}
423 	est_vol = vol - (di->bat_res * curr / 1000);
424 
425 	while ((est_vol <= low_power_voltage) &&
426 	       (vol <= low_power_voltage)) {
427 		mdelay(100);
428 
429 		/* Update */
430 		vol = rk818_bat_get_avg_voltage(di);
431 		curr = rk818_bat_get_avg_current(di);
432 		plugin = rk818_bat_read(di, VB_MON_REG) & PLUG_IN_STS;
433 		if (di->is_first_power_on || (!plugin && curr >= 0) || (plugin && curr <= 0)) {
434 			DBG("%s: while curr=%d, plugin=%d, first_on=%d\n",
435 			    __func__, curr, plugin, di->is_first_power_on);
436 			curr = 0;
437 		}
438 		est_vol = vol - (di->bat_res * curr / 1000);
439 
440 		timeout++;
441 		if (timeout >= 5)
442 			break;
443 	}
444 
445 	return (est_vol >= low_power_voltage) ? est_vol : vol;
446 }
447 
448 static u8 rk818_bat_finish_ma(struct battery_priv *di, int fcc)
449 {
450 	u8 ma;
451 
452 	if (di->res_div == 2)
453 		ma = FINISH_100MA;
454 	else if (fcc > 5000)
455 		ma = FINISH_250MA;
456 	else if (fcc >= 4000)
457 		ma = FINISH_200MA;
458 	else if (fcc >= 3000)
459 		ma = FINISH_150MA;
460 	else
461 		ma = FINISH_100MA;
462 
463 	return ma;
464 }
465 
466 static void rk818_bat_select_chrg_cv(struct battery_priv *di)
467 {
468 	int index, chrg_vol_sel, chrg_cur_sel, chrg_cur_input;
469 
470 	chrg_vol_sel = di->dts_vol_sel;
471 	chrg_cur_sel = di->dts_cur_sel;
472 	chrg_cur_input = di->dts_cur_input;
473 	if (di->sample_res == SAMPLE_RES_10mR) {
474 		if (chrg_cur_sel > 2000)
475 			chrg_cur_sel /= di->res_div;
476 		else
477 			chrg_cur_sel = 1000;
478 	}
479 
480 	for (index = 0; index < ARRAY_SIZE(CHRG_VOL_SEL); index++) {
481 		if (chrg_vol_sel < CHRG_VOL_SEL[index])
482 			break;
483 		di->chrg_vol_sel = (index << 4);
484 	}
485 
486 	for (index = 0; index < ARRAY_SIZE(CHRG_CUR_INPUT); index++) {
487 		if (chrg_cur_input < CHRG_CUR_INPUT[index])
488 			break;
489 		di->chrg_cur_input = (index << 0);
490 	}
491 
492 	for (index = 0; index < ARRAY_SIZE(CHRG_CUR_SEL); index++) {
493 		if (chrg_cur_sel < CHRG_CUR_SEL[index])
494 			break;
495 		di->chrg_cur_sel = (index << 0);
496 	}
497 
498 	DBG("<%s>. vol=0x%x, input=0x%x, sel=0x%x\n",
499 	    __func__, di->chrg_vol_sel, di->chrg_cur_input, di->chrg_cur_sel);
500 }
501 
502 static void rk818_bat_init_chrg_config(struct battery_priv *di)
503 {
504 	u8 chrg_ctrl1, usb_ctrl, chrg_ctrl2, chrg_ctrl3;
505 	u8 sup_sts, ggcon, thermal, finish_ma;
506 
507 	rk818_bat_select_chrg_cv(di);
508 	finish_ma = rk818_bat_finish_ma(di, di->fcc);
509 
510 	ggcon = rk818_bat_read(di, GGCON_REG);
511 	sup_sts = rk818_bat_read(di, SUP_STS_REG);
512 	usb_ctrl = rk818_bat_read(di, USB_CTRL_REG);
513 	thermal = rk818_bat_read(di, THERMAL_REG);
514 	chrg_ctrl2 = rk818_bat_read(di, CHRG_CTRL_REG2);
515 	chrg_ctrl3 = rk818_bat_read(di, CHRG_CTRL_REG3);
516 
517 	/* set charge current and voltage */
518 	usb_ctrl &= ~INPUT_CUR_MSK;
519 	usb_ctrl |= di->chrg_cur_input;
520 	chrg_ctrl1 = (CHRG_EN | di->chrg_vol_sel | di->chrg_cur_sel);
521 
522 	/* digital signal and finish current*/
523 	chrg_ctrl3 &= ~CHRG_TERM_SIG_MSK;
524 	chrg_ctrl3 |= CHRG_TERM_ANA_SIGNAL;
525 	chrg_ctrl2 &= ~FINISH_CUR_MSK;
526 	chrg_ctrl2 |= finish_ma;
527 
528 	/* cccv mode */
529 	chrg_ctrl3 &= ~CHRG_TIMER_CCCV_EN;
530 
531 	/* enable voltage limit and enable input current limit */
532 	sup_sts &= ~USB_VLIMIT_EN;
533 	sup_sts |= USB_CLIMIT_EN;
534 
535 	/* set feedback temperature */
536 	usb_ctrl |= CHRG_CT_EN;
537 	thermal &= ~FB_TEMP_MSK;
538 	thermal |= TEMP_105C;
539 
540 	/* adc current mode */
541 	ggcon |= ADC_CUR_MODE;
542 
543 	rk818_bat_write(di, GGCON_REG, ggcon);
544 	rk818_bat_write(di, SUP_STS_REG, sup_sts);
545 	rk818_bat_write(di, USB_CTRL_REG, usb_ctrl);
546 	rk818_bat_write(di, THERMAL_REG, thermal);
547 	rk818_bat_write(di, CHRG_CTRL_REG1, chrg_ctrl1);
548 	rk818_bat_write(di, CHRG_CTRL_REG2, chrg_ctrl2);
549 	rk818_bat_write(di, CHRG_CTRL_REG3, chrg_ctrl3);
550 }
551 
552 static u32 interpolate(int value, u32 *table, int size)
553 {
554 	uint8_t i;
555 	uint16_t d;
556 
557 	for (i = 0; i < size; i++) {
558 		if (value < table[i])
559 			break;
560 	}
561 
562 	if ((i > 0) && (i < size)) {
563 		d = (value - table[i - 1]) * (MAX_INTERPOLATE / (size - 1));
564 		d /= table[i] - table[i - 1];
565 		d = d + (i - 1) * (MAX_INTERPOLATE / (size - 1));
566 	} else {
567 		d = i * ((MAX_INTERPOLATE + size / 2) / size);
568 	}
569 
570 	if (d > 1000)
571 		d = 1000;
572 
573 	return d;
574 }
575 
576 /* returns (a * b) / c */
577 static int32_t ab_div_c(u32 a, u32 b, u32 c)
578 {
579 	bool sign;
580 	u32 ans = MAX_INT;
581 	int32_t tmp;
582 
583 	sign = ((((a ^ b) ^ c) & 0x80000000) != 0);
584 
585 	if (c != 0) {
586 		if (sign)
587 			c = -c;
588 		tmp = ((int32_t)a * b + (c >> 1)) / c;
589 		if (tmp < MAX_INT)
590 			ans = tmp;
591 	}
592 
593 	if (sign)
594 		ans = -ans;
595 
596 	return ans;
597 }
598 
599 static int rk818_bat_vol_to_cap(struct battery_priv *di, int voltage)
600 {
601 	u32 *ocv_table, tmp;
602 	int ocv_size, ocv_cap;
603 
604 	ocv_table = di->ocv_table;
605 	ocv_size = di->ocv_size;
606 	tmp = interpolate(voltage, ocv_table, ocv_size);
607 	ocv_cap = ab_div_c(tmp, di->fcc, MAX_INTERPOLATE);
608 
609 	return ocv_cap;
610 }
611 
612 static int rk818_bat_vol_to_soc(struct battery_priv *di, int voltage)
613 {
614 	u32 *ocv_table, tmp;
615 	int ocv_size, ocv_soc;
616 
617 	ocv_table = di->ocv_table;
618 	ocv_size = di->ocv_size;
619 	tmp = interpolate(voltage, ocv_table, ocv_size);
620 	ocv_soc = ab_div_c(tmp, MAX_PERCENTAGE, MAX_INTERPOLATE);
621 
622 	return ocv_soc;
623 }
624 
625 static int rk818_bat_get_prev_cap(struct battery_priv *di)
626 {
627 	int val = 0;
628 
629 	val |= rk818_bat_read(di, REMAIN_CAP_REG3) << 24;
630 	val |= rk818_bat_read(di, REMAIN_CAP_REG2) << 16;
631 	val |= rk818_bat_read(di, REMAIN_CAP_REG1) << 8;
632 	val |= rk818_bat_read(di, REMAIN_CAP_REG0) << 0;
633 
634 	return val;
635 }
636 
637 static void rk818_bat_save_fcc(struct battery_priv *di, u32 cap)
638 {
639 	u8 buf;
640 
641 	buf = (cap >> 24) & 0xff;
642 	rk818_bat_write(di, NEW_FCC_REG3, buf);
643 	buf = (cap >> 16) & 0xff;
644 	rk818_bat_write(di, NEW_FCC_REG2, buf);
645 	buf = (cap >> 8) & 0xff;
646 	rk818_bat_write(di, NEW_FCC_REG1, buf);
647 	buf = (cap >> 0) & 0xff;
648 	rk818_bat_write(di, NEW_FCC_REG0, buf);
649 }
650 
651 static int rk818_bat_get_fcc(struct battery_priv *di)
652 {
653 	int val = 0;
654 
655 	val |= rk818_bat_read(di, NEW_FCC_REG3) << 24;
656 	val |= rk818_bat_read(di, NEW_FCC_REG2) << 16;
657 	val |= rk818_bat_read(di, NEW_FCC_REG1) << 8;
658 	val |= rk818_bat_read(di, NEW_FCC_REG0) << 0;
659 
660 	if (val < MIN_FCC)
661 		val = di->design_cap;
662 	else if (val > di->qmax)
663 		val = di->qmax;
664 
665 	return val;
666 }
667 
668 static int rk818_bat_get_pwroff_min(struct battery_priv *di)
669 {
670 	u8 cur, last;
671 
672 	cur = rk818_bat_read(di, NON_ACT_TIMER_CNT_REG);
673 	last = rk818_bat_read(di, NON_ACT_TIMER_CNT_SAVE_REG);
674 	rk818_bat_write(di, NON_ACT_TIMER_CNT_SAVE_REG, cur);
675 
676 	return (cur != last) ? cur : 0;
677 }
678 
679 static int rk818_bat_get_coulomb_cap(struct battery_priv *di)
680 {
681 	int val = 0;
682 
683 	val |= rk818_bat_read(di, GASCNT_REG3) << 24;
684 	val |= rk818_bat_read(di, GASCNT_REG2) << 16;
685 	val |= rk818_bat_read(di, GASCNT_REG1) << 8;
686 	val |= rk818_bat_read(di, GASCNT_REG0) << 0;
687 	val /= 2390;
688 
689 	return val * di->res_div;
690 }
691 
692 static void rk818_bat_save_cap(struct battery_priv *di, int cap)
693 {
694 	u8 buf;
695 	static int old_cap;
696 
697 	if (old_cap == cap)
698 		return;
699 
700 	if (cap >= di->qmax)
701 		cap = di->qmax;
702 
703 	old_cap = cap;
704 	buf = (cap >> 24) & 0xff;
705 	rk818_bat_write(di, REMAIN_CAP_REG3, buf);
706 	buf = (cap >> 16) & 0xff;
707 	rk818_bat_write(di, REMAIN_CAP_REG2, buf);
708 	buf = (cap >> 8) & 0xff;
709 	rk818_bat_write(di, REMAIN_CAP_REG1, buf);
710 	buf = (cap >> 0) & 0xff;
711 	rk818_bat_write(di, REMAIN_CAP_REG0, buf);
712 }
713 
714 static void rk818_bat_init_capacity(struct battery_priv *di, u32 capacity)
715 {
716 	u8 buf;
717 	u32 cap;
718 	int delta;
719 
720 	delta = capacity - di->remain_cap;
721 	if (!delta)
722 		return;
723 
724 	cap = capacity * 2390 / di->res_div;
725 	buf = (cap >> 24) & 0xff;
726 	rk818_bat_write(di, GASCNT_CAL_REG3, buf);
727 	buf = (cap >> 16) & 0xff;
728 	rk818_bat_write(di, GASCNT_CAL_REG2, buf);
729 	buf = (cap >> 8) & 0xff;
730 	rk818_bat_write(di, GASCNT_CAL_REG1, buf);
731 	buf = (cap >> 0) & 0xff;
732 	rk818_bat_write(di, GASCNT_CAL_REG0, buf);
733 
734 	di->remain_cap = rk818_bat_get_coulomb_cap(di);
735 	di->rsoc = rk818_bat_get_rsoc(di);
736 	rk818_bat_save_cap(di, di->remain_cap);
737 }
738 
739 static bool is_rk818_bat_ocv_valid(struct battery_priv *di)
740 {
741 	return di->pwroff_min >= 30 ? true : false;
742 }
743 
744 static int rk818_bat_get_usb_state(struct battery_priv *di)
745 {
746 	int charger_type;
747 
748 	switch (rk818_bat_dwc_otg_check_dpdm()) {
749 	case 0:
750 		if ((rk818_bat_read(di, VB_MON_REG) & PLUG_IN_STS) != 0)
751 			charger_type = DC_CHARGER;
752 		else
753 			charger_type = NO_CHARGER;
754 		break;
755 	case 1:
756 	case 3:
757 		charger_type = USB_CHARGER;
758 		break;
759 	case 2:
760 		charger_type = AC_CHARGER;
761 		break;
762 	default:
763 		charger_type = NO_CHARGER;
764 	}
765 
766 	return charger_type;
767 }
768 
769 static void rk818_bat_clr_initialized_state(struct battery_priv *di)
770 {
771 	u8 val;
772 
773 	val = rk818_bat_read(di, MISC_MARK_REG);
774 	val &= ~FG_INIT;
775 	rk818_bat_write(di, MISC_MARK_REG, val);
776 }
777 
778 static bool rk818_bat_is_initialized(struct battery_priv *di)
779 {
780 	return (rk818_bat_read(di, MISC_MARK_REG) & FG_INIT) ? true : false;
781 }
782 
783 static void rk818_bat_set_initialized_state(struct battery_priv *di)
784 {
785 	u8 val;
786 
787 	val = rk818_bat_read(di, MISC_MARK_REG);
788 	if (rk818_bat_get_usb_state(di) != NO_CHARGER) {
789 		val |= FG_INIT;
790 		rk818_bat_write(di, MISC_MARK_REG, val);
791 		BAT_INFO("initialized... estv=%d, ch=%d\n",
792 			 rk818_bat_get_est_voltage(di),
793 			 rk818_bat_get_usb_state(di));
794 	}
795 }
796 
797 static void rk818_bat_save_dsoc(struct  battery_priv *di, u8 save_soc)
798 {
799 	static int old_soc = -1;
800 
801 	if (old_soc != save_soc) {
802 		old_soc = save_soc;
803 		rk818_bat_write(di, SOC_REG, save_soc);
804 	}
805 }
806 
807 static void rk818_bat_first_pwron(struct battery_priv *di)
808 {
809 	int ocv_vol, vol, curr;
810 
811 	rk818_bat_save_fcc(di, di->design_cap);
812 	ocv_vol = rk818_bat_get_ocv_voltage(di);
813 	curr = rk818_bat_get_avg_current(di);
814 	di->fcc = rk818_bat_get_fcc(di);
815 	di->nac = rk818_bat_vol_to_cap(di, ocv_vol);
816 	di->rsoc = rk818_bat_vol_to_soc(di, ocv_vol);
817 	di->dsoc = di->rsoc;
818 	vol = rk818_bat_get_avg_voltage(di);
819 	if (ocv_vol < vol) {
820 		BAT_INFO("%s: ocv voltage %d\n", __func__, ocv_vol);
821 		ocv_vol = vol;
822 	}
823 	rk818_bat_save_dsoc(di, di->dsoc);
824 	rk818_bat_init_capacity(di, di->nac);
825 	rk818_bat_set_initialized_state(di);
826 	BAT_INFO("first power on: soc=%d, Vavg=%d, Vocv=%d, c=%d, ch=%d, fcc=%d\n",
827 		 di->dsoc, vol, ocv_vol, curr, rk818_bat_get_usb_state(di), di->fcc);
828 }
829 
830 static u8 rk818_bat_get_halt_cnt(struct battery_priv *di)
831 {
832 	return rk818_bat_read(di, HALT_CNT_REG);
833 }
834 
835 static void rk818_bat_inc_halt_cnt(struct battery_priv *di)
836 {
837 	u8 cnt;
838 
839 	cnt = rk818_bat_read(di, HALT_CNT_REG);
840 	rk818_bat_write(di, HALT_CNT_REG, ++cnt);
841 }
842 
843 static bool is_rk818_bat_last_halt(struct battery_priv *di)
844 {
845 	int pre_cap = rk818_bat_get_prev_cap(di);
846 	int now_cap = rk818_bat_get_coulomb_cap(di);
847 
848 	/* over 5%: system halt last time */
849 	if (abs(now_cap - pre_cap) > (di->fcc / 20)) {
850 		rk818_bat_inc_halt_cnt(di);
851 		return true;
852 	} else {
853 		return false;
854 	}
855 }
856 
857 static void rk818_bat_not_first_pwron(struct battery_priv *di)
858 {
859 	int pre_soc, pre_cap, ocv_cap = 0, ocv_soc = 0, ocv_vol, now_cap;
860 	int voltage;
861 
862 	di->fcc = rk818_bat_get_fcc(di);
863 	pre_soc = rk818_bat_get_dsoc(di);
864 	pre_cap = rk818_bat_get_prev_cap(di);
865 	now_cap = rk818_bat_get_coulomb_cap(di);
866 	voltage = rk818_bat_get_est_voltage(di);
867 	di->pwr_dsoc = pre_soc;
868 	di->pwr_rsoc = (now_cap + di->fcc / 200) * 100 / DIV(di->fcc);
869 	di->is_halt = is_rk818_bat_last_halt(di);
870 	di->halt_cnt = rk818_bat_get_halt_cnt(di);
871 	di->is_ocv_calib = is_rk818_bat_ocv_valid(di);
872 
873 	if (di->is_halt) {
874 		BAT_INFO("system halt last time... cap: pre=%d, now=%d\n",
875 			 pre_cap, now_cap);
876 		if (now_cap < 0)
877 			now_cap = 0;
878 		rk818_bat_init_capacity(di, now_cap);
879 		pre_cap = di->remain_cap;
880 		pre_soc = di->rsoc;
881 		goto finish;
882 	} else if (di->is_ocv_calib) {
883 		ocv_vol = rk818_bat_get_ocv_voltage(di);
884 		ocv_soc = rk818_bat_vol_to_soc(di, ocv_vol);
885 		ocv_cap = rk818_bat_vol_to_cap(di, ocv_vol);
886 		pre_cap = ocv_cap;
887 		BAT_INFO("do ocv calib.. rsoc=%d\n", ocv_soc);
888 
889 		if (abs(ocv_soc - pre_soc) >= di->max_soc_offset) {
890 			BAT_INFO("trigger max soc offset, soc: %d -> %d\n",
891 				 pre_soc, ocv_soc);
892 			pre_soc = ocv_soc;
893 			di->is_max_soc_offset = true;
894 		}
895 		BAT_INFO("OCV calib: cap=%d, rsoc=%d\n", ocv_cap, ocv_soc);
896 	} else if ((pre_soc == 0) && (voltage >= ZERO_MIN_VOLTAGE)) {
897 		if (now_cap < 0)
898 			now_cap = 0;
899 		rk818_bat_init_capacity(di, now_cap);
900 		pre_cap = di->remain_cap;
901 		pre_soc = di->rsoc;
902 		BAT_INFO("zero calib: voltage=%d\n", voltage);
903 	}
904 finish:
905 	di->dsoc = pre_soc;
906 	di->nac = pre_cap;
907 	rk818_bat_init_capacity(di, di->nac);
908 	rk818_bat_save_dsoc(di, di->dsoc);
909 	rk818_bat_set_initialized_state(di);
910 	BAT_INFO("dl=%d rl=%d cap=%d m=%d v=%d ov=%d c=%d pl=%d ch=%d fcc=%d, Ver=%s\n",
911 		 di->dsoc, di->rsoc, di->remain_cap, di->pwroff_min,
912 		 rk818_bat_get_avg_voltage(di), rk818_bat_get_ocv_voltage(di),
913 		 rk818_bat_get_avg_current(di), rk818_bat_get_dsoc(di),
914 		 rk818_bat_get_usb_state(di), di->fcc, DRIVER_VERSION
915 		 );
916 }
917 
918 static bool is_rk818_bat_first_poweron(struct battery_priv *di)
919 {
920 	u8 buf;
921 
922 	buf = rk818_bat_read(di, GGSTS_REG);
923 	if (buf & BAT_CON) {
924 		buf &= ~BAT_CON;
925 		rk818_bat_write(di, GGSTS_REG, buf);
926 		return true;
927 	}
928 
929 	return false;
930 }
931 
932 static bool rk818_bat_ocv_sw_reset(struct battery_priv *di)
933 {
934 	u8 buf;
935 
936 	buf = rk818_bat_read(di, MISC_MARK_REG);
937 	if (((buf & FG_RESET_LATE) && di->pwroff_min >= 30) ||
938 	    (buf & FG_RESET_NOW)) {
939 		buf &= ~FG_RESET_LATE;
940 		buf &= ~FG_RESET_NOW;
941 		rk818_bat_write(di, MISC_MARK_REG, buf);
942 		BAT_INFO("manual reset fuel gauge\n");
943 		return true;
944 	} else {
945 		return false;
946 	}
947 }
948 
949 static void rk818_bat_init_rsoc(struct battery_priv *di)
950 {
951 	int charger, voltage, initialize = 0;
952 	struct charge_animation_pdata *pdata;
953 	struct udevice *dev;
954 
955 	uclass_find_first_device(UCLASS_CHARGE_DISPLAY, &dev);
956 	pdata = dev_get_platdata(dev);
957 
958 	charger = rk818_bat_get_usb_state(di);
959 	voltage = rk818_bat_get_est_voltage(di);
960 	di->is_first_power_on = is_rk818_bat_first_poweron(di);
961 
962 	/*
963 	 * Do rsoc initialization only when:
964 	 *
965 	 * 1. first power on;
966 	 * 2. charger online + voltage lower than low_power_voltage;
967 	 * 3. charger online + uboot_charge enabled.
968 	 * 4. dsoc is 0 but voltage high, obvious wrong.
969 	 */
970 	if (di->is_first_power_on) {
971 		initialize = 1;
972 	} else if ((di->dsoc == 0) && (voltage >= ZERO_MIN_VOLTAGE)) {
973 		initialize = 1;
974 	} else if (charger != NO_CHARGER) {
975 		if (voltage < pdata->low_power_voltage + 50)
976 			initialize = 1;
977 		else if (pdata->uboot_charge)
978 			initialize = 1;
979 	}
980 
981 	if (!initialize)
982 		return;
983 
984 	di->pwroff_min = rk818_bat_get_pwroff_min(di);
985 	di->is_sw_reset = rk818_bat_ocv_sw_reset(di);
986 
987 	if (di->is_first_power_on || di->is_sw_reset)
988 		rk818_bat_first_pwron(di);
989 	else
990 		rk818_bat_not_first_pwron(di);
991 }
992 
993 static int rk818_bat_calc_linek(struct battery_priv *di)
994 {
995 	int linek, diff, delta;
996 
997 	di->calc_dsoc = di->dsoc;
998 	di->calc_rsoc = di->rsoc;
999 	di->sm_old_cap = di->remain_cap;
1000 
1001 	delta = abs(di->dsoc - di->rsoc);
1002 	diff = delta * 3;
1003 	di->sm_meet_soc = (di->dsoc >= di->rsoc) ?
1004 			   (di->dsoc + diff) : (di->rsoc + diff);
1005 
1006 	if (di->dsoc < di->rsoc)
1007 		linek = 1000 * (delta + diff) / DIV(diff);
1008 	else if (di->dsoc > di->rsoc)
1009 		linek = 1000 * diff / DIV(delta + diff);
1010 	else
1011 		linek = 1000;
1012 
1013 	di->sm_chrg_dsoc = di->dsoc * 1000;
1014 
1015 	DBG("<%s>. meet=%d, diff=%d, link=%d, calc: dsoc=%d, rsoc=%d\n",
1016 	    __func__, di->sm_meet_soc, diff, linek,
1017 	    di->calc_dsoc, di->calc_rsoc);
1018 
1019 	return linek;
1020 }
1021 
1022 static void rk818_bat_init_ts1(struct battery_priv *di)
1023 {
1024 	u8 buf;
1025 	u32 *ntc_table = di->ntc_table;
1026 
1027 	if (!di->ntc_size)
1028 		return;
1029 
1030 	/* select uA */
1031 	buf = rk818_bat_read(di, TS_CTRL_REG);
1032 	buf &= ~ADC_CUR_MSK;
1033 	/* chose suitable UA for temperature detect */
1034 	if (ntc_table[0] < NTC_80UA_MAX_MEASURE) {
1035 		di->ntc_factor = NTC_CALC_FACTOR_80UA;
1036 		di->ntc_uA = 80;
1037 		buf |= ADC_CUR_80UA;
1038 	} else if (ntc_table[0] < NTC_60UA_MAX_MEASURE) {
1039 		di->ntc_factor = NTC_CALC_FACTOR_60UA;
1040 		di->ntc_uA = 60;
1041 		buf |= ADC_CUR_60UA;
1042 	} else if (ntc_table[0] < NTC_40UA_MAX_MEASURE) {
1043 		di->ntc_factor = NTC_CALC_FACTOR_40UA;
1044 		di->ntc_uA = 40;
1045 		buf |= ADC_CUR_40UA;
1046 	} else {
1047 		di->ntc_factor = NTC_CALC_FACTOR_20UA;
1048 		di->ntc_uA = 20;
1049 		buf |= ADC_CUR_20UA;
1050 	}
1051 	rk818_bat_write(di, TS_CTRL_REG, buf);
1052 
1053 	/* ADC_TS1_EN */
1054 	buf = rk818_bat_read(di, ADC_CTRL_REG);
1055 	buf |= ADC_TS1_EN;
1056 	rk818_bat_write(di, ADC_CTRL_REG, buf);
1057 }
1058 
1059 static void rk818_bat_init_ts2(struct battery_priv *di)
1060 {
1061 	u8 buf;
1062 
1063 	if (!di->ts2_vol_multi)
1064 		return;
1065 
1066 	/* TS2 adc mode */
1067 	buf = rk818_bat_read(di, TS_CTRL_REG);
1068 	buf |= TS2_ADC_MODE;
1069 	rk818_bat_write(di, TS_CTRL_REG, buf);
1070 
1071 	/* TS2 adc enable */
1072 	buf = rk818_bat_read(di, ADC_CTRL_REG);
1073 	buf |= ADC_TS2_EN;
1074 	rk818_bat_write(di, ADC_CTRL_REG, buf);
1075 }
1076 
1077 static int rk818_fg_init(struct battery_priv *di)
1078 {
1079 	int cap;
1080 
1081 	rk818_bat_enable_gauge(di);
1082 	rk818_bat_init_voltage_kb(di);
1083 	rk818_bat_init_coffset(di);
1084 	rk818_bat_init_ts1(di);
1085 	rk818_bat_init_ts2(di);
1086 	rk818_bat_clr_initialized_state(di);
1087 	cap = rk818_bat_get_coulomb_cap(di);
1088 	di->dsoc = rk818_bat_get_dsoc(di);
1089 	di->rsoc = (cap + di->fcc / 200) * 100 / DIV(di->fcc);
1090 
1091 	/* dsoc and rsoc maybe initialized here */
1092 	rk818_bat_init_rsoc(di);
1093 
1094 	rk818_bat_init_chrg_config(di);
1095 	di->voltage_avg = rk818_bat_get_avg_voltage(di);
1096 	di->voltage_ocv = rk818_bat_get_ocv_voltage(di);
1097 	di->current_avg = rk818_bat_get_avg_current(di);
1098 	di->sm_linek = rk818_bat_calc_linek(di);
1099 	di->finish_chrg_base = get_timer(0);
1100 	di->term_sig_base = get_timer(0);
1101 	di->pwr_vol = di->voltage_avg;
1102 
1103 	DBG("%s: dsoc=%d, rsoc=%d, v=%d, ov=%d, c=%d, estv=%d\n",
1104 	    __func__, di->dsoc, di->rsoc, di->voltage_avg, di->voltage_ocv,
1105 	    di->current_avg, rk818_bat_get_est_voltage(di));
1106 
1107 	return 0;
1108 }
1109 
1110 static bool is_rk818_bat_exist(struct  battery_priv *di)
1111 {
1112 	return (rk818_bat_read(di, SUP_STS_REG) & BAT_EXS) ? true : false;
1113 }
1114 
1115 static void rk818_bat_set_current(struct battery_priv *di, int input_current)
1116 {
1117 	u8 usb_ctrl;
1118 
1119 	usb_ctrl = rk818_bat_read(di, USB_CTRL_REG);
1120 	usb_ctrl &= ~INPUT_CUR_MSK;
1121 	usb_ctrl |= (input_current);
1122 	rk818_bat_write(di, USB_CTRL_REG, usb_ctrl);
1123 }
1124 
1125 static int rk818_bat_get_ts2_voltage(struct battery_priv *di)
1126 {
1127 	u32 val = 0;
1128 
1129 	val |= rk818_bat_read(di, RK818_TS2_ADC_REGL) << 0;
1130 	val |= rk818_bat_read(di, RK818_TS2_ADC_REGH) << 8;
1131 
1132 	/* refer voltage 2.2V, 12bit adc accuracy */
1133 	val = val * 2200 * di->ts2_vol_multi / 4095;
1134 	DBG("<%s>. ts2 voltage=%d\n", __func__, val);
1135 
1136 	return val;
1137 }
1138 
1139 static void rk818_bat_ts2_update_current(struct battery_priv *di)
1140 {
1141 	int ts2_vol, input_current, invalid_cnt = 0, confirm_cnt = 0;
1142 
1143 	rk818_bat_set_current(di, ILIM_450MA);
1144 	input_current = ILIM_850MA;
1145 	while (input_current < di->chrg_cur_input) {
1146 		mdelay(100);
1147 		ts2_vol = rk818_bat_get_ts2_voltage(di);
1148 		DBG("******** ts2 vol=%d\n", ts2_vol);
1149 		/* filter invalid voltage */
1150 		if (ts2_vol <= TS2_VALID_VOL) {
1151 			invalid_cnt++;
1152 			DBG("%s: invalid ts2 voltage: %d\n, cnt=%d",
1153 			    __func__, ts2_vol, invalid_cnt);
1154 			if (invalid_cnt < TS2_CHECK_CNT)
1155 				continue;
1156 
1157 			/* if fail, set max input current as default */
1158 			input_current = di->chrg_cur_input;
1159 			rk818_bat_set_current(di, input_current);
1160 			break;
1161 		}
1162 
1163 		/* update input current */
1164 		if (ts2_vol >= TS2_THRESHOLD_VOL) {
1165 			/* update input current */
1166 			input_current++;
1167 			rk818_bat_set_current(di, input_current);
1168 			DBG("********* input=%d\n",
1169 			    CHRG_CUR_INPUT[input_current & 0x0f]);
1170 		} else {
1171 			/* confirm lower threshold voltage */
1172 			confirm_cnt++;
1173 			if (confirm_cnt < TS2_CHECK_CNT) {
1174 				DBG("%s: confirm ts2 voltage: %d\n, cnt=%d",
1175 				    __func__, ts2_vol, confirm_cnt);
1176 				continue;
1177 			}
1178 
1179 			/* trigger threshold, so roll back 1 step */
1180 			input_current--;
1181 			if (input_current == ILIM_80MA ||
1182 			    input_current < 0)
1183 				input_current = ILIM_450MA;
1184 			rk818_bat_set_current(di, input_current);
1185 			break;
1186 		}
1187 	}
1188 
1189 	BAT_INFO("DC_CHARGER charge_cur_input=%d\n",
1190 		 CHRG_CUR_INPUT[input_current]);
1191 }
1192 
1193 static void rk818_bat_charger_setting(struct battery_priv *di, int charger)
1194 {
1195 	static u8 old_charger = UNDEF_CHARGER;
1196 	struct charge_animation_pdata *pdata;
1197 	struct udevice *dev;
1198 	int low_power_voltage = 0;
1199 
1200 	uclass_find_first_device(UCLASS_CHARGE_DISPLAY, &dev);
1201 	pdata = dev_get_platdata(dev);
1202 	low_power_voltage = pdata->low_power_voltage;
1203 
1204 	/* charger changed */
1205 	if (old_charger != charger) {
1206 		if (charger == NO_CHARGER) {
1207 			BAT_INFO("NO_CHARGER\n");
1208 			rk818_bat_set_current(di, ILIM_450MA);
1209 		} else if (charger == USB_CHARGER) {
1210 			BAT_INFO("USB_CHARGER\n");
1211 			rk818_bat_set_current(di, ILIM_450MA);
1212 		} else if (charger == DC_CHARGER || charger == AC_CHARGER) {
1213 			if (pdata->uboot_charge && di->ts2_vol_multi) {
1214 				rk818_bat_ts2_update_current(di);
1215 			} else if ((rk818_bat_get_est_voltage(di) < low_power_voltage) &&
1216 				   (di->ts2_vol_multi)) {
1217 				rk818_bat_ts2_update_current(di);
1218 			} else {
1219 				rk818_bat_set_current(di, di->chrg_cur_input);
1220 				BAT_INFO("DC_CHARGER\n");
1221 			}
1222 		} else {
1223 			BAT_INFO("charger setting error %d\n", charger);
1224 		}
1225 
1226 		old_charger = charger;
1227 	}
1228 }
1229 
1230 static int rk818_bat_get_dc_state(struct battery_priv *di)
1231 {
1232 	if (!di->dc_is_valid)
1233 		return NO_CHARGER;
1234 
1235 	return dm_gpio_get_value(&di->dc_det) ? DC_CHARGER : NO_CHARGER;
1236 }
1237 
1238 static int rk818_bat_get_charger_type(struct battery_priv *di)
1239 {
1240 	int charger_type = NO_CHARGER;
1241 
1242 	/* check by ic hardware: this check make check work safer */
1243 	if ((rk818_bat_read(di, VB_MON_REG) & PLUG_IN_STS) == 0)
1244 		return NO_CHARGER;
1245 
1246 	/* virtual or bat not exist */
1247 	if (di->virtual_power)
1248 		return DC_CHARGER;
1249 
1250 	/* check DC firstly */
1251 	charger_type = rk818_bat_get_dc_state(di);
1252 	if (charger_type == DC_CHARGER)
1253 		return charger_type;
1254 
1255 	/* check USB secondly */
1256 	return rk818_bat_get_usb_state(di);
1257 }
1258 
1259 static u8 rk818_bat_get_chrg_status(struct battery_priv *di)
1260 {
1261 	u8 status;
1262 
1263 	status = rk818_bat_read(di, SUP_STS_REG) & BAT_STATUS_MSK;
1264 	switch (status) {
1265 	case CHARGE_OFF:
1266 		DBG("CHARGE-OFF...\n");
1267 		break;
1268 	case DEAD_CHARGE:
1269 		DBG("DEAD CHARGE...\n");
1270 		break;
1271 	case  TRICKLE_CHARGE:
1272 		DBG("TRICKLE CHARGE...\n ");
1273 		break;
1274 	case  CC_OR_CV:
1275 		DBG("CC or CV...\n");
1276 		break;
1277 	case  CHARGE_FINISH:
1278 		DBG("CHARGE FINISH...\n");
1279 		break;
1280 	case  USB_OVER_VOL:
1281 		DBG("USB OVER VOL...\n");
1282 		break;
1283 	case  BAT_TMP_ERR:
1284 		DBG("BAT TMP ERROR...\n");
1285 		break;
1286 	case  TIMER_ERR:
1287 		DBG("TIMER ERROR...\n");
1288 		break;
1289 	case  USB_EXIST:
1290 		DBG("USB EXIST...\n");
1291 		break;
1292 	case  USB_EFF:
1293 		DBG("USB EFF...\n");
1294 		break;
1295 	default:
1296 		return -EINVAL;
1297 	}
1298 
1299 	return status;
1300 }
1301 
1302 static void rk818_bat_finish_chrg(struct battery_priv *di)
1303 {
1304 	u32 tgt_sec = 0;
1305 
1306 	if (di->dsoc < 100) {
1307 		tgt_sec = di->fcc * 3600 / 100 / FINISH_CALI_CURR;
1308 		if (get_timer(di->finish_chrg_base) > SECONDS(tgt_sec)) {
1309 			di->finish_chrg_base = get_timer(0);
1310 			di->dsoc++;
1311 		}
1312 	}
1313 	DBG("<%s>. sec=%d, finish_sec=%lu\n", __func__, SECONDS(tgt_sec),
1314 	    get_timer(di->finish_chrg_base));
1315 }
1316 
1317 static void rk818_bat_debug_info(struct battery_priv *di)
1318 {
1319 	u8 sup_sts, ggcon, ggsts, vb_mod, rtc, thermal, misc;
1320 	u8 usb_ctrl, chrg_ctrl1, chrg_ctrl2, chrg_ctrl3;
1321 	static const char *name[] = {"NONE", "USB", "AC", "DC", "UNDEF"};
1322 
1323 	if (!dbg_enable)
1324 		return;
1325 	ggcon = rk818_bat_read(di, GGCON_REG);
1326 	ggsts = rk818_bat_read(di, GGSTS_REG);
1327 	sup_sts = rk818_bat_read(di, SUP_STS_REG);
1328 	usb_ctrl = rk818_bat_read(di, USB_CTRL_REG);
1329 	thermal = rk818_bat_read(di, THERMAL_REG);
1330 	vb_mod = rk818_bat_read(di, VB_MON_REG);
1331 	misc = rk818_bat_read(di, MISC_MARK_REG);
1332 	rtc = rk818_bat_read(di, SECONDS_REG);
1333 	chrg_ctrl1 = rk818_bat_read(di, CHRG_CTRL_REG1);
1334 	chrg_ctrl2 = rk818_bat_read(di, CHRG_CTRL_REG2);
1335 	chrg_ctrl3 = rk818_bat_read(di, CHRG_CTRL_REG3);
1336 
1337 	DBG("\n---------------------- DEBUG REGS ------------------------\n"
1338 	    "GGCON=0x%2x, GGSTS=0x%2x, RTC=0x%2x, SUP_STS= 0x%2x\n"
1339 	    "VB_MOD=0x%2x, USB_CTRL=0x%2x, THERMAL=0x%2x, MISC=0x%2x\n"
1340 	    "CHRG_CTRL:REG1=0x%2x, REG2=0x%2x, REG3=0x%2x\n",
1341 	    ggcon, ggsts, rtc, sup_sts, vb_mod, usb_ctrl,
1342 	    thermal, misc, chrg_ctrl1, chrg_ctrl2, chrg_ctrl3
1343 	    );
1344 	DBG("----------------------------------------------------------\n"
1345 	    "Dsoc=%d, Rsoc=%d, Vavg=%d, Iavg=%d, Cap=%d, Fcc=%d, d=%d\n"
1346 	    "K=%d, old_cap=%d, charger=%s, Is=%d, Ip=%d, Vs=%d\n"
1347 	    "min=%d, meet: soc=%d, calc: dsoc=%d, rsoc=%d, Vocv=%d\n"
1348 	    "off: i=0x%x, c=0x%x, max=%d, ocv_c=%d, halt: st=%d, cnt=%d\n"
1349 	    "pwr: dsoc=%d, rsoc=%d, vol=%d, Res=%d, mode=%s, T=%d'C\n",
1350 	    di->dsoc, rk818_bat_get_rsoc(di), rk818_bat_get_avg_voltage(di),
1351 	    rk818_bat_get_avg_current(di), di->remain_cap, di->fcc,
1352 	    di->rsoc - di->dsoc,
1353 	    di->sm_linek, di->sm_old_cap, name[di->chrg_type],
1354 	    di->res_div * CHRG_CUR_SEL[chrg_ctrl1 & 0x0f],
1355 	    CHRG_CUR_INPUT[usb_ctrl & 0x0f],
1356 	    CHRG_VOL_SEL[(chrg_ctrl1 & 0x70) >> 4],  di->pwroff_min,
1357 	    di->sm_meet_soc, di->calc_dsoc, di->calc_rsoc,
1358 	    rk818_bat_get_ocv_voltage(di), rk818_bat_get_ioffset(di),
1359 	    rk818_bat_get_coffset(di), di->is_max_soc_offset,
1360 	    di->is_ocv_calib, di->is_halt, di->halt_cnt, di->pwr_dsoc,
1361 	    di->pwr_rsoc, di->pwr_vol, di->sample_res,
1362 	    di->virtual_power ? "VIRTUAL" : "BAT",
1363 	    di->temperature
1364 	    );
1365 	rk818_bat_get_chrg_status(di);
1366 	DBG("###########################################################\n");
1367 }
1368 
1369 static void rk818_bat_linek_algorithm(struct battery_priv *di)
1370 {
1371 	int delta_cap, ydsoc, tmp;
1372 	u8 chg_st = rk818_bat_get_chrg_status(di);
1373 
1374 	/* slow down */
1375 	if (di->dsoc == 99)
1376 		di->sm_linek = CHRG_FULL_K;
1377 	else if (di->dsoc >= CHRG_TERM_DSOC && di->current_avg > TERM_CALI_CURR)
1378 		di->sm_linek = CHRG_TERM_K;
1379 
1380 	delta_cap = di->remain_cap - di->sm_old_cap;
1381 	ydsoc = di->sm_linek * delta_cap * 100 / DIV(di->fcc);
1382 	if (ydsoc > 0) {
1383 		tmp = (di->sm_chrg_dsoc + 1) / 1000;
1384 		if (tmp != di->dsoc)
1385 			di->sm_chrg_dsoc = di->dsoc * 1000;
1386 		di->sm_chrg_dsoc += ydsoc;
1387 		di->dsoc = (di->sm_chrg_dsoc + 1) / 1000;
1388 		di->sm_old_cap = di->remain_cap;
1389 		if (di->dsoc == di->rsoc && di->sm_linek != CHRG_FULL_K &&
1390 		    di->sm_linek != CHRG_TERM_K)
1391 			di->sm_linek = 1000;
1392 	}
1393 
1394 	if ((di->sm_linek == 1000 || di->dsoc >= 100) &&
1395 	    (chg_st != CHARGE_FINISH)) {
1396 		if (di->sm_linek == 1000)
1397 			di->dsoc = di->rsoc;
1398 		di->sm_chrg_dsoc = di->dsoc * 1000;
1399 	}
1400 
1401 	DBG("linek=%d, sm_dsoc=%d, delta_cap=%d, ydsoc=%d, old_cap=%d\n"
1402 	    "calc: dsoc=%d, rsoc=%d, meet=%d\n",
1403 	    di->sm_linek, di->sm_chrg_dsoc, delta_cap, ydsoc, di->sm_old_cap,
1404 	    di->calc_dsoc, di->calc_rsoc, di->sm_meet_soc);
1405 }
1406 
1407 static void rk818_bat_set_term_mode(struct battery_priv *di, int mode)
1408 {
1409 	u8 buf;
1410 
1411 	buf = rk818_bat_read(di, CHRG_CTRL_REG3);
1412 	buf &= ~CHRG_TERM_SIG_MSK;
1413 	buf |= mode;
1414 	rk818_bat_write(di, CHRG_CTRL_REG3, buf);
1415 
1416 	DBG("set charge to %s term mode\n", mode ? "digital" : "analog");
1417 }
1418 
1419 static int rk818_bat_get_iadc(struct battery_priv *di)
1420 {
1421 	int val = 0;
1422 
1423 	val |= rk818_bat_read(di, BAT_CUR_AVG_REGL) << 0;
1424 	val |= rk818_bat_read(di, BAT_CUR_AVG_REGH) << 8;
1425 	if (val > 2047)
1426 		val -= 4096;
1427 
1428 	return val;
1429 }
1430 
1431 static bool rk818_bat_adc_calib(struct battery_priv *di)
1432 {
1433 	int i, ioffset, coffset, adc;
1434 
1435 	if (abs(di->current_avg) < ADC_CALIB_THRESHOLD)
1436 		return false;
1437 
1438 	for (i = 0; i < 5; i++) {
1439 		adc = rk818_bat_get_iadc(di);
1440 		coffset = rk818_bat_get_coffset(di);
1441 		rk818_bat_set_coffset(di, coffset + adc);
1442 		mdelay(200);
1443 		adc = rk818_bat_get_iadc(di);
1444 		if (abs(adc) < ADC_CALIB_THRESHOLD) {
1445 			coffset = rk818_bat_get_coffset(di);
1446 			ioffset = rk818_bat_get_ioffset(di);
1447 			di->poffset = coffset - ioffset;
1448 			rk818_bat_write(di, POFFSET_REG, di->poffset);
1449 			BAT_INFO("new offset:c=0x%x, i=0x%x, p=0x%x\n",
1450 				 coffset, ioffset, di->poffset);
1451 			return true;
1452 		} else {
1453 			BAT_INFO("coffset calib again %d..\n", i);
1454 			rk818_bat_set_coffset(di, coffset);
1455 			mdelay(200);
1456 		}
1457 	}
1458 
1459 	return false;
1460 }
1461 
1462 static void rk818_bat_smooth_charge(struct battery_priv *di)
1463 {
1464 	u8 chg_st = rk818_bat_get_chrg_status(di);
1465 
1466 	/* set terminal charge mode */
1467 	if (di->term_sig_base && get_timer(di->term_sig_base) > SECONDS(1)) {
1468 		DBG("%s: terminal signal finish mode\n", __func__);
1469 		rk818_bat_set_term_mode(di, CHRG_TERM_DIG_SIGNAL);
1470 		di->term_sig_base = 0;
1471 	}
1472 
1473 	/* not charge mode and not keep in uboot charge: exit */
1474 	if ((di->chrg_type == NO_CHARGER) ||
1475 	    !rk818_bat_is_initialized(di)) {
1476 		DBG("chrg=%d, initialized=%d\n", di->chrg_type,
1477 		    rk818_bat_is_initialized(di));
1478 		goto out;
1479 	}
1480 
1481 	/* update rsoc and remain cap */
1482 	di->remain_cap = rk818_bat_get_coulomb_cap(di);
1483 	di->rsoc = rk818_bat_get_rsoc(di);
1484 	if (di->remain_cap > di->fcc) {
1485 		di->sm_old_cap -= (di->remain_cap - di->fcc);
1486 		rk818_bat_init_capacity(di, di->fcc);
1487 		DBG("%s: init capacity: %d\n", __func__, di->fcc);
1488 	}
1489 
1490 	/* finish charge step */
1491 	if (chg_st == CHARGE_FINISH) {
1492 		DBG("%s: finish charge step...\n", __func__);
1493 		if (di->adc_allow_update)
1494 			di->adc_allow_update = !rk818_bat_adc_calib(di);
1495 		rk818_bat_finish_chrg(di);
1496 		rk818_bat_init_capacity(di, di->fcc);
1497 	} else {
1498 		DBG("%s: smooth charge step...\n", __func__);
1499 		di->adc_allow_update = true;
1500 		di->finish_chrg_base = get_timer(0);
1501 		rk818_bat_linek_algorithm(di);
1502 	}
1503 
1504 	/* dsoc limit */
1505 	if (di->dsoc > 100)
1506 		di->dsoc = 100;
1507 	else if (di->dsoc < 0)
1508 		di->dsoc = 0;
1509 
1510 	DBG("%s: save dsoc=%d and rsoc=%d\n",
1511 	    __func__, di->dsoc, rk818_bat_get_rsoc(di));
1512 
1513 	rk818_bat_save_dsoc(di, di->dsoc);
1514 	rk818_bat_save_cap(di, di->remain_cap);
1515 out:
1516 	rk818_bat_debug_info(di);
1517 }
1518 
1519 /*
1520  * Due to hardware design issue, Vdelta = "(R_sample + R_other) * I_avg" will be
1521  * included into TS1 adc value. We must subtract it to get correct adc value.
1522  * The solution:
1523  *
1524  * (1) calculate Vdelta:
1525  *
1526  *   adc1 - Vdelta    ua1			  (adc2 * ua1) - (adc1 * ua2)
1527  *   ------------- = -----  ==> equals: Vdelta = -----------------------------
1528  *   adc2 - Vdelta    ua2				ua1 - ua2
1529  *
1530  *
1531  * (2) calculate correct ADC value:
1532  *
1533  *     charging: ADC = adc1 - abs(Vdelta);
1534  *  discharging: ADC = adc1 + abs(Vdelta);
1535  */
1536 static int rk818_bat_get_ntc_res(struct battery_priv *di)
1537 {
1538 	static int adc1 = 0, adc2 = 0, ua1 = 0, ua2 = 0;
1539 	static int adc1_update = 0, first_in = 1;
1540 	static ulong seconds;
1541 	int v_delta, val, res;
1542 	u8 buf;
1543 
1544 	/* hold adc1 and wait 1s for adc2 updated */
1545 	if (!adc1_update) {
1546 		/* update flag and init adc1,adc2 !! */
1547 		adc1_update = 1;
1548 		seconds = get_timer(0);
1549 		adc1 = 0;
1550 		adc2 = 0;
1551 
1552 		/* read sample ua1 */
1553 		buf = rk818_bat_read(di, TS_CTRL_REG);
1554 		DBG("<%s>. read adc1, sample uA=%d\n",
1555 		    __func__, ((buf & 0x03) + 1) * 20);
1556 
1557 		/* read adc adc1 */
1558 		ua1 = di->ntc_uA;
1559 		adc1 |= rk818_bat_read(di, TS_ADC_REGL) << 0;
1560 		adc1 |= rk818_bat_read(di, TS_ADC_REGH) << 8;
1561 
1562 		/* chose reference UA for adc2 */
1563 		ua2 = (ua1 != 20) ? 20 : 40;
1564 		buf = rk818_bat_read(di, TS_CTRL_REG);
1565 		buf &= ~ADC_CUR_MSK;
1566 		buf |= ((ua2 - 20) / 20);
1567 		rk818_bat_write(di, TS_CTRL_REG, buf);
1568 	}
1569 
1570 	/* wait 1s for adc2 updated */
1571 	if (get_timer(seconds) < SECONDS(1)) {
1572 		if (first_in)
1573 			first_in = 0;
1574 		else
1575 			return TS1_NOT_READY;
1576 	}
1577 
1578 	/* update flags ! */
1579 	adc1_update = 0;
1580 
1581 	/* read sample ua2 */
1582 	buf = rk818_bat_read(di, TS_CTRL_REG);
1583 	DBG("<%s>. read adc2, sample uA=%d\n",
1584 	    __func__, ((buf & 0x03) + 1) * 20);
1585 
1586 	/* read adc adc2 */
1587 	adc2 |= rk818_bat_read(di, TS_ADC_REGL) << 0;
1588 	adc2 |= rk818_bat_read(di, TS_ADC_REGH) << 8;
1589 
1590 	DBG("<%s>. ua1=%d, ua2=%d, adc1=%d, adc2=%d\n",
1591 	    __func__, ua1, ua2, adc1, adc2);
1592 
1593 	/* calculate delta voltage */
1594 	if (adc2 != adc1)
1595 		v_delta = abs((adc2 * ua1 - adc1 * ua2) / (ua2 - ua1));
1596 	else
1597 		v_delta = 0;
1598 
1599 	/* considering current avg direction, calcuate real adc value */
1600 	val = (di->current_avg >= 0) ? (adc1 - v_delta) : (adc1 + v_delta);
1601 
1602 	DBG("<%s>. Iavg=%d, Vdelta=%d, Vadc=%d\n",
1603 	    __func__, di->current_avg, v_delta, val);
1604 
1605 	res = val * di->ntc_factor;
1606 
1607 	DBG("<%s>. val=%d, ntc_res=%d, ntc_factor=%d\n",
1608 	    __func__, val, res, di->ntc_factor);
1609 
1610 	DBG("<%s>. t=[%d'C(%d) ~ %dC(%d)]\n", __func__,
1611 	    di->ntc_degree_from, di->ntc_table[0],
1612 	    di->ntc_degree_from + di->ntc_size - 1,
1613 	    di->ntc_table[di->ntc_size - 1]);
1614 
1615 	rk818_bat_init_ts1(di);
1616 
1617 	return res;
1618 }
1619 
1620 static int rk818_bat_update_temperature(struct battery_priv *di)
1621 {
1622 	static int first_time = 1, old_temperature = 25;
1623 	u32 ntc_size, *ntc_table;
1624 	int i, res, temp;
1625 
1626 	ntc_table = di->ntc_table;
1627 	ntc_size = di->ntc_size;
1628 
1629 	if (ntc_size) {
1630 		res = rk818_bat_get_ntc_res(di);
1631 		if (res == TS1_NOT_READY) {
1632 			di->temperature = old_temperature;
1633 			return TS1_NOT_READY;
1634 		}
1635 
1636 		if (res < ntc_table[ntc_size - 1]) {
1637 			di->temperature = di->ntc_degree_from;
1638 			old_temperature = di->ntc_degree_from;
1639 			printf("bat ntc upper max degree: R=%d\n", res);
1640 		} else if (res > ntc_table[0]) {
1641 			di->temperature = di->ntc_degree_from + di->ntc_size - 1;
1642 			old_temperature = di->ntc_degree_from + di->ntc_size - 1;
1643 			printf("bat ntc lower min degree: R=%d\n", res);
1644 		} else {
1645 			for (i = 0; i < ntc_size; i++) {
1646 				if (res >= ntc_table[i])
1647 					break;
1648 			}
1649 
1650 			/* if first in, init old_temperature */
1651 			temp = (i + di->ntc_degree_from);
1652 			if (first_time) {
1653 				di->temperature = temp;
1654 				old_temperature = temp;
1655 				first_time = 0;
1656 			}
1657 
1658 			old_temperature = temp;
1659 			di->temperature = temp;
1660 		}
1661 	}
1662 
1663 	DBG("temperature=%d\n", di->temperature);
1664 
1665 	return 0;
1666 }
1667 
1668 static int rk818_bat_bat_is_exit(struct udevice *dev)
1669 {
1670 	struct battery_priv *di = dev_get_priv(dev);
1671 
1672 	return is_rk818_bat_exist(di);
1673 }
1674 
1675 static int rk818_bat_update_get_soc(struct udevice *dev)
1676 {
1677 	struct battery_priv *di = dev_get_priv(dev);
1678 	static ulong seconds, ts1_seconds;
1679 	int wait;
1680 
1681 	/* set charge current */
1682 	di->chrg_type =
1683 		rk818_bat_get_charger_type(di);
1684 	rk818_bat_charger_setting(di, di->chrg_type);
1685 
1686 	/* fg calc every 5 seconds */
1687 	if (!seconds || !ts1_seconds) {
1688 		seconds = get_timer(0);
1689 		ts1_seconds = get_timer(0);
1690 	}
1691 
1692 	/* temperature calc every 5 seconds */
1693 	if (get_timer(ts1_seconds) >= SECONDS(5)) {
1694 		DBG("%s: update temperature\n", __func__);
1695 		wait = rk818_bat_update_temperature(di);
1696 		if (!wait)
1697 			ts1_seconds = get_timer(0);
1698 	}
1699 
1700 	if (get_timer(seconds) >= SECONDS(5)) {
1701 		DBG("%s: smooth charge\n", __func__);
1702 		seconds = get_timer(0);
1703 		rk818_bat_smooth_charge(di);
1704 	}
1705 
1706 	/* bat exist, fg init success(dts pass) and uboot charge: report data */
1707 	if (!di->virtual_power && di->voltage_k)
1708 		return di->dsoc;
1709 	else
1710 		return VIRTUAL_POWER_SOC;
1711 }
1712 
1713 static int rk818_bat_update_get_current(struct udevice *dev)
1714 {
1715 	struct battery_priv *di = dev_get_priv(dev);
1716 
1717 	if (!di->virtual_power && di->voltage_k)
1718 		return rk818_bat_get_avg_current(di);
1719 	else
1720 		return VIRTUAL_POWER_CUR;
1721 }
1722 
1723 static int rk818_bat_update_get_voltage(struct udevice *dev)
1724 {
1725 	struct battery_priv *di = dev_get_priv(dev);
1726 
1727 	if (!di->virtual_power && di->voltage_k)
1728 		return rk818_bat_get_est_voltage(di);
1729 	else
1730 		return VIRTUAL_POWER_VOL;
1731 }
1732 
1733 static bool rk818_bat_update_get_chrg_online(struct udevice *dev)
1734 {
1735 	struct battery_priv *di = dev_get_priv(dev);
1736 
1737 	return rk818_bat_get_charger_type(di);
1738 }
1739 
1740 static struct dm_fuel_gauge_ops fg_ops = {
1741 	.bat_is_exist = rk818_bat_bat_is_exit,
1742 	.get_soc = rk818_bat_update_get_soc,
1743 	.get_voltage = rk818_bat_update_get_voltage,
1744 	.get_current = rk818_bat_update_get_current,
1745 	.get_chrg_online = rk818_bat_update_get_chrg_online,
1746 };
1747 
1748 static int rk818_fg_ofdata_to_platdata(struct udevice *dev)
1749 {
1750 	struct rk8xx_priv *rk8xx = dev_get_priv(dev->parent);
1751 	struct battery_priv *di = dev_get_priv(dev);
1752 	u32 sign, degree_from[2];
1753 	const char *prop;
1754 	int len, ret;
1755 
1756 	if (rk8xx->variant != 0x8180) {
1757 		debug("%s: Not support pmic variant: rk%x\n",
1758 		      __func__, rk8xx->variant);
1759 		return -EINVAL;
1760 	} else {
1761 		di->dev = dev;
1762 	}
1763 
1764 	/* Parse ocv table */
1765 	prop = dev_read_prop(dev, "ocv_table", &len);
1766 	if (!prop) {
1767 		printf("can't find ocv_table prop\n");
1768 		return -EINVAL;
1769 	}
1770 
1771 	di->ocv_table = calloc(len, 1);
1772 	if (!di->ocv_table) {
1773 		printf("can't calloc ocv_table\n");
1774 		return -ENOMEM;
1775 	}
1776 
1777 	di->ocv_size = len / 4;
1778 	if (dev_read_u32_array(dev, "ocv_table",
1779 			       di->ocv_table, di->ocv_size)) {
1780 		printf("can't read ocv_table\n");
1781 		free(di->ocv_table);
1782 		return -EINVAL;
1783 	}
1784 
1785 	/* Parse neccessay */
1786 	di->design_cap = dev_read_u32_default(dev, "design_capacity", -1);
1787 	if (di->design_cap < 0) {
1788 		printf("can't read design_capacity\n");
1789 		return -EINVAL;
1790 	}
1791 
1792 	di->qmax = dev_read_u32_default(dev, "design_qmax", -1);
1793 	if (di->qmax < 0) {
1794 		printf("can't read design_qmax\n");
1795 		return -EINVAL;
1796 	}
1797 
1798 	/* Parse un-neccessay */
1799 	di->dts_vol_sel = dev_read_u32_default(dev, "max_chrg_voltage", 4200);
1800 	if (di->dts_vol_sel < 0)
1801 		di->dts_vol_sel = dev_read_u32_default(dev,
1802 						"max_charge_voltagemV", 4200);
1803 
1804 	di->dts_cur_input = dev_read_u32_default(dev, "max_input_current", 2000);
1805 	if (di->dts_cur_input < 0)
1806 		di->dts_cur_input = dev_read_u32_default(dev,
1807 						"max_input_currentmA", 2000);
1808 
1809 	di->dts_cur_sel = dev_read_u32_default(dev, "max_chrg_current", 1200);
1810 	if (di->dts_cur_sel < 0)
1811 		di->dts_cur_sel = dev_read_u32_default(dev,
1812 						"max_chrg_currentmA", 1400);
1813 
1814 	di->max_soc_offset = dev_read_u32_default(dev, "max_soc_offset", 70);
1815 	di->virtual_power = dev_read_u32_default(dev, "virtual_power", 0);
1816 	di->bat_res = dev_read_u32_default(dev, "bat_res", 135);
1817 	di->sample_res = dev_read_u32_default(dev, "sample_res", SAMPLE_RES_20mR);
1818 	di->ts2_vol_multi = dev_read_u32_default(dev, "ts2_vol_multi", 0);
1819 
1820 	di->res_div = (di->sample_res == SAMPLE_RES_20mR) ?
1821 				SAMPLE_RES_DIV1 : SAMPLE_RES_DIV2;
1822 
1823 	ret = gpio_request_by_name_nodev(dev_ofnode(dev), "dc_det_gpio",
1824 					 0, &di->dc_det, GPIOD_IS_IN);
1825 	if (!ret) {
1826 		di->dc_is_valid = 1;
1827 		debug("DC is valid\n");
1828 	} else {
1829 		debug("DC is invalid, ret=%d\n", ret);
1830 	}
1831 
1832 	prop = dev_read_prop(dev, "ntc_table", &len);
1833 	if (!prop) {
1834 		di->ntc_size = 0;
1835 	} else {
1836 		ret = dev_read_u32_array(dev, "ntc_degree_from",
1837 					 degree_from, ARRAY_SIZE(degree_from));
1838 		if (ret < 0) {
1839 			printf("invalid ntc_degree_from\n");
1840 			return -EINVAL;
1841 		}
1842 
1843 		sign = degree_from[0];
1844 		di->ntc_degree_from = degree_from[1];
1845 		if (sign)
1846 			di->ntc_degree_from = -di->ntc_degree_from;
1847 
1848 		di->ntc_size = len / sizeof(u32);
1849 	}
1850 
1851 	if (di->ntc_size) {
1852 		di->ntc_table = calloc(len, 1);
1853 		if (!di->ntc_table) {
1854 			printf("calloc ocv_table fail\n");
1855 			return -ENOMEM;
1856 		}
1857 
1858 		ret = dev_read_u32_array(dev, "ntc_table",
1859 					 di->ntc_table, di->ntc_size);
1860 		if (ret < 0) {
1861 			printf("read ntc_table array failed\n");
1862 			return ret;
1863 		}
1864 	}
1865 
1866 	/* Is battery attached */
1867 	if (!is_rk818_bat_exist(di))
1868 		di->virtual_power = 1;
1869 
1870 	DBG("-------------------------------:\n");
1871 	DBG("max_input_current:%d\n", di->dts_cur_input);
1872 	DBG("max_chrg_current:%d\n", di->dts_cur_sel);
1873 	DBG("max_chrg_voltage:%d\n", di->dts_vol_sel);
1874 	DBG("design_capacity :%d\n", di->design_cap);
1875 	DBG("design_qmax:%d\n", di->qmax);
1876 	DBG("max_soc_offset:%d\n", di->max_soc_offset);
1877 	DBG("sample_res:%d\n", di->sample_res);
1878 	DBG("virtual_power:%d\n", di->virtual_power);
1879 	DBG("ts2_vol_multi:%d\n", di->ts2_vol_multi);
1880 	DBG("dc det: %d\n", di->dc_is_valid);
1881 	DBG("ntc_size:%d\n", di->ntc_size);
1882 	DBG("ntc_degree_from:%d\n", di->ntc_degree_from);
1883 	DBG("ntc_degree_to:%d\n", di->ntc_degree_from + di->ntc_size - 1);
1884 
1885 	return 0;
1886 }
1887 
1888 static int rk818_fg_probe(struct udevice *dev)
1889 {
1890 	struct rk8xx_priv *rk8xx = dev_get_priv(dev->parent);
1891 	struct battery_priv *di = dev_get_priv(dev);
1892 
1893 	if (rk8xx->variant != 0x8180) {
1894 		printf("Not support pmic variant: rk%x\n", rk8xx->variant);
1895 		return -EINVAL;
1896 	}
1897 
1898 	return rk818_fg_init(di);
1899 }
1900 
1901 U_BOOT_DRIVER(rk818_fg) = {
1902 	.name = "rk818_fg",
1903 	.id = UCLASS_FG,
1904 	.probe = rk818_fg_probe,
1905 	.ops = &fg_ops,
1906 	.ofdata_to_platdata = rk818_fg_ofdata_to_platdata,
1907 	.priv_auto_alloc_size = sizeof(struct battery_priv),
1908 };
1909