1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Battery driver for Marvell 88PM860x PMIC
4  *
5  * Copyright (c) 2012 Marvell International Ltd.
6  * Author:	Jett Zhou <[email protected]>
7  *		Haojian Zhuang <[email protected]>
8  */
9 
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/platform_device.h>
13 #include <linux/slab.h>
14 #include <linux/mutex.h>
15 #include <linux/string.h>
16 #include <linux/power_supply.h>
17 #include <linux/string_choices.h>
18 #include <linux/mfd/88pm860x.h>
19 #include <linux/delay.h>
20 
21 /* bit definitions of Status Query Interface 2 */
22 #define STATUS2_CHG			(1 << 2)
23 #define STATUS2_BAT			(1 << 3)
24 #define STATUS2_VBUS			(1 << 4)
25 
26 /* bit definitions of Measurement Enable 1 Register */
27 #define MEAS1_TINT			(1 << 3)
28 #define MEAS1_GP1			(1 << 5)
29 
30 /* bit definitions of Measurement Enable 3 Register */
31 #define MEAS3_IBAT			(1 << 0)
32 #define MEAS3_BAT_DET			(1 << 1)
33 #define MEAS3_CC			(1 << 2)
34 
35 /* bit definitions of Measurement Off Time Register */
36 #define MEAS_OFF_SLEEP_EN		(1 << 1)
37 
38 /* bit definitions of GPADC Bias Current 2 Register */
39 #define GPBIAS2_GPADC1_SET		(2 << 4)
40 /* GPADC1 Bias Current value in uA unit */
41 #define GPBIAS2_GPADC1_UA		((GPBIAS2_GPADC1_SET >> 4) * 5 + 1)
42 
43 /* bit definitions of GPADC Misc 1 Register */
44 #define GPMISC1_GPADC_EN		(1 << 0)
45 
46 /* bit definitions of Charger Control 6 Register */
47 #define CC6_BAT_DET_GPADC1		1
48 
49 /* bit definitions of Coulomb Counter Reading Register */
50 #define CCNT_AVG_SEL			(4 << 3)
51 
52 /* bit definitions of RTC miscellaneous Register1 */
53 #define RTC_SOC_5LSB		(0x1F << 3)
54 
55 /* bit definitions of RTC Register1 */
56 #define RTC_SOC_3MSB		(0x7)
57 
58 /* bit definitions of Power up Log register */
59 #define BAT_WU_LOG			(1<<6)
60 
61 /* coulomb counter index */
62 #define CCNT_POS1			0
63 #define CCNT_POS2			1
64 #define CCNT_NEG1			2
65 #define CCNT_NEG2			3
66 #define CCNT_SPOS			4
67 #define CCNT_SNEG			5
68 
69 /* OCV -- Open Circuit Voltage */
70 #define OCV_MODE_ACTIVE			0
71 #define OCV_MODE_SLEEP			1
72 
73 /* Vbat range of CC for measuring Rbat */
74 #define LOW_BAT_THRESHOLD		3600
75 #define VBATT_RESISTOR_MIN		3800
76 #define VBATT_RESISTOR_MAX		4100
77 
78 /* TBAT for batt, TINT for chip itself */
79 #define PM860X_TEMP_TINT		(0)
80 #define PM860X_TEMP_TBAT		(1)
81 
82 /*
83  * Battery temperature based on NTC resistor, defined
84  * corresponding resistor value  -- Ohm / C degeree.
85  */
86 #define TBAT_NEG_25D		127773	/* -25 */
87 #define TBAT_NEG_10D		54564	/* -10 */
88 #define TBAT_0D			32330	/* 0 */
89 #define TBAT_10D		19785	/* 10 */
90 #define TBAT_20D		12468	/* 20 */
91 #define TBAT_30D		8072	/* 30 */
92 #define TBAT_40D		5356	/* 40 */
93 
94 struct pm860x_battery_info {
95 	struct pm860x_chip *chip;
96 	struct i2c_client *i2c;
97 	struct device *dev;
98 
99 	struct power_supply *battery;
100 	struct mutex lock;
101 	int status;
102 	int irq_cc;
103 	int irq_batt;
104 	int max_capacity;
105 	int resistor;		/* Battery Internal Resistor */
106 	int last_capacity;
107 	int start_soc;
108 	unsigned present:1;
109 	unsigned temp_type:1;	/* TINT or TBAT */
110 };
111 
112 struct ccnt {
113 	unsigned long long pos;
114 	unsigned long long neg;
115 	unsigned int spos;
116 	unsigned int sneg;
117 
118 	int total_chg;		/* mAh(3.6C) */
119 	int total_dischg;	/* mAh(3.6C) */
120 };
121 
122 /*
123  * State of Charge.
124  * The first number is mAh(=3.6C), and the second number is percent point.
125  */
126 static int array_soc[][2] = {
127 	{4170, 100}, {4154, 99}, {4136, 98}, {4122, 97}, {4107, 96},
128 	{4102, 95}, {4088, 94}, {4081, 93}, {4070, 92}, {4060, 91},
129 	{4053, 90}, {4044, 89}, {4035, 88}, {4028, 87}, {4019, 86},
130 	{4013, 85}, {4006, 84}, {3995, 83}, {3987, 82}, {3982, 81},
131 	{3976, 80}, {3968, 79}, {3962, 78}, {3954, 77}, {3946, 76},
132 	{3941, 75}, {3934, 74}, {3929, 73}, {3922, 72}, {3916, 71},
133 	{3910, 70}, {3904, 69}, {3898, 68}, {3892, 67}, {3887, 66},
134 	{3880, 65}, {3874, 64}, {3868, 63}, {3862, 62}, {3854, 61},
135 	{3849, 60}, {3843, 59}, {3840, 58}, {3833, 57}, {3829, 56},
136 	{3824, 55}, {3818, 54}, {3815, 53}, {3810, 52}, {3808, 51},
137 	{3804, 50}, {3801, 49}, {3798, 48}, {3796, 47}, {3792, 46},
138 	{3789, 45}, {3785, 44}, {3784, 43}, {3782, 42}, {3780, 41},
139 	{3777, 40}, {3776, 39}, {3774, 38}, {3772, 37}, {3771, 36},
140 	{3769, 35}, {3768, 34}, {3764, 33}, {3763, 32}, {3760, 31},
141 	{3760, 30}, {3754, 29}, {3750, 28}, {3749, 27}, {3744, 26},
142 	{3740, 25}, {3734, 24}, {3732, 23}, {3728, 22}, {3726, 21},
143 	{3720, 20}, {3716, 19}, {3709, 18}, {3703, 17}, {3698, 16},
144 	{3692, 15}, {3683, 14}, {3675, 13}, {3670, 12}, {3665, 11},
145 	{3661, 10}, {3649, 9}, {3637, 8}, {3622, 7}, {3609, 6},
146 	{3580, 5}, {3558, 4}, {3540, 3}, {3510, 2}, {3429, 1},
147 };
148 
149 static struct ccnt ccnt_data;
150 
151 /*
152  * register 1 bit[7:0] -- bit[11:4] of measured value of voltage
153  * register 0 bit[3:0] -- bit[3:0] of measured value of voltage
154  */
measure_12bit_voltage(struct pm860x_battery_info * info,int offset,int * data)155 static int measure_12bit_voltage(struct pm860x_battery_info *info,
156 				 int offset, int *data)
157 {
158 	unsigned char buf[2];
159 	int ret;
160 
161 	ret = pm860x_bulk_read(info->i2c, offset, 2, buf);
162 	if (ret < 0)
163 		return ret;
164 
165 	*data = ((buf[0] & 0xff) << 4) | (buf[1] & 0x0f);
166 	/* V_MEAS(mV) = data * 1.8 * 1000 / (2^12) */
167 	*data = ((*data & 0xfff) * 9 * 25) >> 9;
168 	return 0;
169 }
170 
measure_vbatt(struct pm860x_battery_info * info,int state,int * data)171 static int measure_vbatt(struct pm860x_battery_info *info, int state,
172 			 int *data)
173 {
174 	unsigned char buf[5];
175 	int ret;
176 
177 	switch (state) {
178 	case OCV_MODE_ACTIVE:
179 		ret = measure_12bit_voltage(info, PM8607_VBAT_MEAS1, data);
180 		if (ret)
181 			return ret;
182 		/* V_BATT_MEAS(mV) = value * 3 * 1.8 * 1000 / (2^12) */
183 		*data *= 3;
184 		break;
185 	case OCV_MODE_SLEEP:
186 		/*
187 		 * voltage value of VBATT in sleep mode is saved in different
188 		 * registers.
189 		 * bit[11:10] -- bit[7:6] of LDO9(0x18)
190 		 * bit[9:8] -- bit[7:6] of LDO8(0x17)
191 		 * bit[7:6] -- bit[7:6] of LDO7(0x16)
192 		 * bit[5:4] -- bit[7:6] of LDO6(0x15)
193 		 * bit[3:0] -- bit[7:4] of LDO5(0x14)
194 		 */
195 		ret = pm860x_bulk_read(info->i2c, PM8607_LDO5, 5, buf);
196 		if (ret < 0)
197 			return ret;
198 		ret = ((buf[4] >> 6) << 10) | ((buf[3] >> 6) << 8)
199 		    | ((buf[2] >> 6) << 6) | ((buf[1] >> 6) << 4)
200 		    | (buf[0] >> 4);
201 		/* V_BATT_MEAS(mV) = data * 3 * 1.8 * 1000 / (2^12) */
202 		*data = ((*data & 0xff) * 27 * 25) >> 9;
203 		break;
204 	default:
205 		return -EINVAL;
206 	}
207 	return 0;
208 }
209 
210 /*
211  * Return value is signed data.
212  * Negative value means discharging, and positive value means charging.
213  */
measure_current(struct pm860x_battery_info * info,int * data)214 static int measure_current(struct pm860x_battery_info *info, int *data)
215 {
216 	unsigned char buf[2];
217 	short s;
218 	int ret;
219 
220 	ret = pm860x_bulk_read(info->i2c, PM8607_IBAT_MEAS1, 2, buf);
221 	if (ret < 0)
222 		return ret;
223 
224 	s = ((buf[0] & 0xff) << 8) | (buf[1] & 0xff);
225 	/* current(mA) = value * 0.125 */
226 	*data = s >> 3;
227 	return 0;
228 }
229 
set_charger_current(struct pm860x_battery_info * info,int data,int * old)230 static int set_charger_current(struct pm860x_battery_info *info, int data,
231 			       int *old)
232 {
233 	int ret;
234 
235 	if (data < 50 || data > 1600 || !old)
236 		return -EINVAL;
237 
238 	data = ((data - 50) / 50) & 0x1f;
239 	*old = pm860x_reg_read(info->i2c, PM8607_CHG_CTRL2);
240 	*old = (*old & 0x1f) * 50 + 50;
241 	ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL2, 0x1f, data);
242 	if (ret < 0)
243 		return ret;
244 	return 0;
245 }
246 
read_ccnt(struct pm860x_battery_info * info,int offset,int * ccnt)247 static int read_ccnt(struct pm860x_battery_info *info, int offset,
248 		     int *ccnt)
249 {
250 	unsigned char buf[2];
251 	int ret;
252 
253 	ret = pm860x_set_bits(info->i2c, PM8607_CCNT, 7, offset & 7);
254 	if (ret < 0)
255 		goto out;
256 	ret = pm860x_bulk_read(info->i2c, PM8607_CCNT_MEAS1, 2, buf);
257 	if (ret < 0)
258 		goto out;
259 	*ccnt = ((buf[0] & 0xff) << 8) | (buf[1] & 0xff);
260 	return 0;
261 out:
262 	return ret;
263 }
264 
calc_ccnt(struct pm860x_battery_info * info,struct ccnt * ccnt)265 static int calc_ccnt(struct pm860x_battery_info *info, struct ccnt *ccnt)
266 {
267 	unsigned int sum;
268 	int ret;
269 	int data;
270 
271 	ret = read_ccnt(info, CCNT_POS1, &data);
272 	if (ret)
273 		goto out;
274 	sum = data & 0xffff;
275 	ret = read_ccnt(info, CCNT_POS2, &data);
276 	if (ret)
277 		goto out;
278 	sum |= (data & 0xffff) << 16;
279 	ccnt->pos += sum;
280 
281 	ret = read_ccnt(info, CCNT_NEG1, &data);
282 	if (ret)
283 		goto out;
284 	sum = data & 0xffff;
285 	ret = read_ccnt(info, CCNT_NEG2, &data);
286 	if (ret)
287 		goto out;
288 	sum |= (data & 0xffff) << 16;
289 	sum = ~sum + 1;		/* since it's negative */
290 	ccnt->neg += sum;
291 
292 	ret = read_ccnt(info, CCNT_SPOS, &data);
293 	if (ret)
294 		goto out;
295 	ccnt->spos += data;
296 	ret = read_ccnt(info, CCNT_SNEG, &data);
297 	if (ret)
298 		goto out;
299 
300 	/*
301 	 * charge(mAh)  = count * 1.6984 * 1e(-8)
302 	 *              = count * 16984 * 1.024 * 1.024 * 1.024 / (2 ^ 40)
303 	 *              = count * 18236 / (2 ^ 40)
304 	 */
305 	ccnt->total_chg = (int) ((ccnt->pos * 18236) >> 40);
306 	ccnt->total_dischg = (int) ((ccnt->neg * 18236) >> 40);
307 	return 0;
308 out:
309 	return ret;
310 }
311 
clear_ccnt(struct pm860x_battery_info * info,struct ccnt * ccnt)312 static int clear_ccnt(struct pm860x_battery_info *info, struct ccnt *ccnt)
313 {
314 	int data;
315 
316 	memset(ccnt, 0, sizeof(*ccnt));
317 	/* read to clear ccnt */
318 	read_ccnt(info, CCNT_POS1, &data);
319 	read_ccnt(info, CCNT_POS2, &data);
320 	read_ccnt(info, CCNT_NEG1, &data);
321 	read_ccnt(info, CCNT_NEG2, &data);
322 	read_ccnt(info, CCNT_SPOS, &data);
323 	read_ccnt(info, CCNT_SNEG, &data);
324 	return 0;
325 }
326 
327 /* Calculate Open Circuit Voltage */
calc_ocv(struct pm860x_battery_info * info,int * ocv)328 static int calc_ocv(struct pm860x_battery_info *info, int *ocv)
329 {
330 	int ret;
331 	int i;
332 	int data;
333 	int vbatt_avg;
334 	int vbatt_sum;
335 	int ibatt_avg;
336 	int ibatt_sum;
337 
338 	if (!ocv)
339 		return -EINVAL;
340 
341 	for (i = 0, ibatt_sum = 0, vbatt_sum = 0; i < 10; i++) {
342 		ret = measure_vbatt(info, OCV_MODE_ACTIVE, &data);
343 		if (ret)
344 			goto out;
345 		vbatt_sum += data;
346 		ret = measure_current(info, &data);
347 		if (ret)
348 			goto out;
349 		ibatt_sum += data;
350 	}
351 	vbatt_avg = vbatt_sum / 10;
352 	ibatt_avg = ibatt_sum / 10;
353 
354 	mutex_lock(&info->lock);
355 	if (info->present)
356 		*ocv = vbatt_avg - ibatt_avg * info->resistor / 1000;
357 	else
358 		*ocv = vbatt_avg;
359 	mutex_unlock(&info->lock);
360 	dev_dbg(info->dev, "VBAT average:%d, OCV:%d\n", vbatt_avg, *ocv);
361 	return 0;
362 out:
363 	return ret;
364 }
365 
366 /* Calculate State of Charge (percent points) */
calc_soc(struct pm860x_battery_info * info,int state,int * soc)367 static int calc_soc(struct pm860x_battery_info *info, int state, int *soc)
368 {
369 	int i;
370 	int ocv;
371 	int count;
372 	int ret = -EINVAL;
373 
374 	if (!soc)
375 		return -EINVAL;
376 
377 	switch (state) {
378 	case OCV_MODE_ACTIVE:
379 		ret = calc_ocv(info, &ocv);
380 		break;
381 	case OCV_MODE_SLEEP:
382 		ret = measure_vbatt(info, OCV_MODE_SLEEP, &ocv);
383 		break;
384 	}
385 	if (ret)
386 		return ret;
387 
388 	count = ARRAY_SIZE(array_soc);
389 	if (ocv < array_soc[count - 1][0]) {
390 		*soc = 0;
391 		return 0;
392 	}
393 
394 	for (i = 0; i < count; i++) {
395 		if (ocv >= array_soc[i][0]) {
396 			*soc = array_soc[i][1];
397 			break;
398 		}
399 	}
400 	return 0;
401 }
402 
pm860x_coulomb_handler(int irq,void * data)403 static irqreturn_t pm860x_coulomb_handler(int irq, void *data)
404 {
405 	struct pm860x_battery_info *info = data;
406 
407 	calc_ccnt(info, &ccnt_data);
408 	return IRQ_HANDLED;
409 }
410 
pm860x_batt_handler(int irq,void * data)411 static irqreturn_t pm860x_batt_handler(int irq, void *data)
412 {
413 	struct pm860x_battery_info *info = data;
414 	int ret;
415 
416 	mutex_lock(&info->lock);
417 	ret = pm860x_reg_read(info->i2c, PM8607_STATUS_2);
418 	if (ret & STATUS2_BAT) {
419 		info->present = 1;
420 		info->temp_type = PM860X_TEMP_TBAT;
421 	} else {
422 		info->present = 0;
423 		info->temp_type = PM860X_TEMP_TINT;
424 	}
425 	mutex_unlock(&info->lock);
426 	/* clear ccnt since battery is attached or detached */
427 	clear_ccnt(info, &ccnt_data);
428 	return IRQ_HANDLED;
429 }
430 
pm860x_init_battery(struct pm860x_battery_info * info)431 static void pm860x_init_battery(struct pm860x_battery_info *info)
432 {
433 	unsigned char buf[2];
434 	int ret;
435 	int data;
436 	int bat_remove;
437 	int soc = 0;
438 
439 	/* measure enable on GPADC1 */
440 	data = MEAS1_GP1;
441 	if (info->temp_type == PM860X_TEMP_TINT)
442 		data |= MEAS1_TINT;
443 	ret = pm860x_set_bits(info->i2c, PM8607_MEAS_EN1, data, data);
444 	if (ret)
445 		goto out;
446 
447 	/* measure enable on IBAT, BAT_DET, CC. IBAT is depend on CC. */
448 	data = MEAS3_IBAT | MEAS3_BAT_DET | MEAS3_CC;
449 	ret = pm860x_set_bits(info->i2c, PM8607_MEAS_EN3, data, data);
450 	if (ret)
451 		goto out;
452 
453 	/* measure disable CC in sleep time  */
454 	ret = pm860x_reg_write(info->i2c, PM8607_MEAS_OFF_TIME1, 0x82);
455 	if (ret)
456 		goto out;
457 	ret = pm860x_reg_write(info->i2c, PM8607_MEAS_OFF_TIME2, 0x6c);
458 	if (ret)
459 		goto out;
460 
461 	/* enable GPADC */
462 	ret = pm860x_set_bits(info->i2c, PM8607_GPADC_MISC1,
463 			    GPMISC1_GPADC_EN, GPMISC1_GPADC_EN);
464 	if (ret < 0)
465 		goto out;
466 
467 	/* detect battery via GPADC1 */
468 	ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL6,
469 			    CC6_BAT_DET_GPADC1, CC6_BAT_DET_GPADC1);
470 	if (ret < 0)
471 		goto out;
472 
473 	ret = pm860x_set_bits(info->i2c, PM8607_CCNT, 7 << 3,
474 			      CCNT_AVG_SEL);
475 	if (ret < 0)
476 		goto out;
477 
478 	/* set GPADC1 bias */
479 	ret = pm860x_set_bits(info->i2c, PM8607_GP_BIAS2, 0xF << 4,
480 			      GPBIAS2_GPADC1_SET);
481 	if (ret < 0)
482 		goto out;
483 
484 	/* check whether battery present) */
485 	mutex_lock(&info->lock);
486 	ret = pm860x_reg_read(info->i2c, PM8607_STATUS_2);
487 	if (ret < 0) {
488 		mutex_unlock(&info->lock);
489 		goto out;
490 	}
491 	if (ret & STATUS2_BAT) {
492 		info->present = 1;
493 		info->temp_type = PM860X_TEMP_TBAT;
494 	} else {
495 		info->present = 0;
496 		info->temp_type = PM860X_TEMP_TINT;
497 	}
498 	mutex_unlock(&info->lock);
499 
500 	ret = calc_soc(info, OCV_MODE_ACTIVE, &soc);
501 	if (ret < 0)
502 		goto out;
503 
504 	data = pm860x_reg_read(info->i2c, PM8607_POWER_UP_LOG);
505 	bat_remove = data & BAT_WU_LOG;
506 
507 	dev_dbg(info->dev, "battery wake up? %s\n", str_yes_no(bat_remove));
508 
509 	/* restore SOC from RTC domain register */
510 	if (bat_remove == 0) {
511 		buf[0] = pm860x_reg_read(info->i2c, PM8607_RTC_MISC2);
512 		buf[1] = pm860x_reg_read(info->i2c, PM8607_RTC1);
513 		data = ((buf[1] & 0x3) << 5) | ((buf[0] >> 3) & 0x1F);
514 		if (data > soc + 15)
515 			info->start_soc = soc;
516 		else if (data < soc - 15)
517 			info->start_soc = soc;
518 		else
519 			info->start_soc = data;
520 		dev_dbg(info->dev, "soc_rtc %d, soc_ocv :%d\n", data, soc);
521 	} else {
522 		pm860x_set_bits(info->i2c, PM8607_POWER_UP_LOG,
523 				BAT_WU_LOG, BAT_WU_LOG);
524 		info->start_soc = soc;
525 	}
526 	info->last_capacity = info->start_soc;
527 	dev_dbg(info->dev, "init soc : %d\n", info->last_capacity);
528 out:
529 	return;
530 }
531 
set_temp_threshold(struct pm860x_battery_info * info,int min,int max)532 static void set_temp_threshold(struct pm860x_battery_info *info,
533 			       int min, int max)
534 {
535 	int data;
536 
537 	/* (tmp << 8) / 1800 */
538 	if (min <= 0)
539 		data = 0;
540 	else
541 		data = (min << 8) / 1800;
542 	pm860x_reg_write(info->i2c, PM8607_GPADC1_HIGHTH, data);
543 	dev_dbg(info->dev, "TEMP_HIGHTH : min: %d, 0x%x\n", min, data);
544 
545 	if (max <= 0)
546 		data = 0xff;
547 	else
548 		data = (max << 8) / 1800;
549 	pm860x_reg_write(info->i2c, PM8607_GPADC1_LOWTH, data);
550 	dev_dbg(info->dev, "TEMP_LOWTH:max : %d, 0x%x\n", max, data);
551 }
552 
measure_temp(struct pm860x_battery_info * info,int * data)553 static int measure_temp(struct pm860x_battery_info *info, int *data)
554 {
555 	int ret;
556 	int temp;
557 	int min;
558 	int max;
559 
560 	if (info->temp_type == PM860X_TEMP_TINT) {
561 		ret = measure_12bit_voltage(info, PM8607_TINT_MEAS1, data);
562 		if (ret)
563 			return ret;
564 		*data = (*data - 884) * 1000 / 3611;
565 	} else {
566 		ret = measure_12bit_voltage(info, PM8607_GPADC1_MEAS1, data);
567 		if (ret)
568 			return ret;
569 		/* measured Vtbat(mV) / Ibias_current(11uA)*/
570 		*data = (*data * 1000) / GPBIAS2_GPADC1_UA;
571 
572 		if (*data > TBAT_NEG_25D) {
573 			temp = -30;	/* over cold , suppose -30 roughly */
574 			max = TBAT_NEG_10D * GPBIAS2_GPADC1_UA / 1000;
575 			set_temp_threshold(info, 0, max);
576 		} else if (*data > TBAT_NEG_10D) {
577 			temp = -15;	/* -15 degree, code */
578 			max = TBAT_NEG_10D * GPBIAS2_GPADC1_UA / 1000;
579 			set_temp_threshold(info, 0, max);
580 		} else if (*data > TBAT_0D) {
581 			temp = -5;	/* -5 degree */
582 			min = TBAT_NEG_10D * GPBIAS2_GPADC1_UA / 1000;
583 			max = TBAT_40D * GPBIAS2_GPADC1_UA / 1000;
584 			set_temp_threshold(info, min, max);
585 		} else if (*data > TBAT_10D) {
586 			temp = 5;	/* in range of (0, 10) */
587 			min = TBAT_NEG_10D * GPBIAS2_GPADC1_UA / 1000;
588 			max = TBAT_40D * GPBIAS2_GPADC1_UA / 1000;
589 			set_temp_threshold(info, min, max);
590 		} else if (*data > TBAT_20D) {
591 			temp = 15;	/* in range of (10, 20) */
592 			min = TBAT_NEG_10D * GPBIAS2_GPADC1_UA / 1000;
593 			max = TBAT_40D * GPBIAS2_GPADC1_UA / 1000;
594 			set_temp_threshold(info, min, max);
595 		} else if (*data > TBAT_30D) {
596 			temp = 25;	/* in range of (20, 30) */
597 			min = TBAT_NEG_10D * GPBIAS2_GPADC1_UA / 1000;
598 			max = TBAT_40D * GPBIAS2_GPADC1_UA / 1000;
599 			set_temp_threshold(info, min, max);
600 		} else if (*data > TBAT_40D) {
601 			temp = 35;	/* in range of (30, 40) */
602 			min = TBAT_NEG_10D * GPBIAS2_GPADC1_UA / 1000;
603 			max = TBAT_40D * GPBIAS2_GPADC1_UA / 1000;
604 			set_temp_threshold(info, min, max);
605 		} else {
606 			min = TBAT_40D * GPBIAS2_GPADC1_UA / 1000;
607 			set_temp_threshold(info, min, 0);
608 			temp = 45;	/* over heat ,suppose 45 roughly */
609 		}
610 
611 		dev_dbg(info->dev, "temp_C:%d C,temp_mv:%d mv\n", temp, *data);
612 		*data = temp;
613 	}
614 	return 0;
615 }
616 
calc_resistor(struct pm860x_battery_info * info)617 static int calc_resistor(struct pm860x_battery_info *info)
618 {
619 	int vbatt_sum1;
620 	int vbatt_sum2;
621 	int chg_current;
622 	int ibatt_sum1;
623 	int ibatt_sum2;
624 	int data;
625 	int ret;
626 	int i;
627 
628 	ret = measure_current(info, &data);
629 	/* make sure that charging is launched by data > 0 */
630 	if (ret || data < 0)
631 		goto out;
632 
633 	ret = measure_vbatt(info, OCV_MODE_ACTIVE, &data);
634 	if (ret)
635 		goto out;
636 	/* calculate resistor only in CC charge mode */
637 	if (data < VBATT_RESISTOR_MIN || data > VBATT_RESISTOR_MAX)
638 		goto out;
639 
640 	/* current is saved */
641 	if (set_charger_current(info, 500, &chg_current))
642 		goto out;
643 
644 	/*
645 	 * set charge current as 500mA, wait about 500ms till charging
646 	 * process is launched and stable with the newer charging current.
647 	 */
648 	msleep(500);
649 
650 	for (i = 0, vbatt_sum1 = 0, ibatt_sum1 = 0; i < 10; i++) {
651 		ret = measure_vbatt(info, OCV_MODE_ACTIVE, &data);
652 		if (ret)
653 			goto out_meas;
654 		vbatt_sum1 += data;
655 		ret = measure_current(info, &data);
656 		if (ret)
657 			goto out_meas;
658 
659 		if (data < 0)
660 			ibatt_sum1 = ibatt_sum1 - data;	/* discharging */
661 		else
662 			ibatt_sum1 = ibatt_sum1 + data;	/* charging */
663 	}
664 
665 	if (set_charger_current(info, 100, &ret))
666 		goto out_meas;
667 	/*
668 	 * set charge current as 100mA, wait about 500ms till charging
669 	 * process is launched and stable with the newer charging current.
670 	 */
671 	msleep(500);
672 
673 	for (i = 0, vbatt_sum2 = 0, ibatt_sum2 = 0; i < 10; i++) {
674 		ret = measure_vbatt(info, OCV_MODE_ACTIVE, &data);
675 		if (ret)
676 			goto out_meas;
677 		vbatt_sum2 += data;
678 		ret = measure_current(info, &data);
679 		if (ret)
680 			goto out_meas;
681 
682 		if (data < 0)
683 			ibatt_sum2 = ibatt_sum2 - data;	/* discharging */
684 		else
685 			ibatt_sum2 = ibatt_sum2 + data;	/* charging */
686 	}
687 
688 	/* restore current setting */
689 	if (set_charger_current(info, chg_current, &ret))
690 		goto out_meas;
691 
692 	if ((vbatt_sum1 > vbatt_sum2) && (ibatt_sum1 > ibatt_sum2) &&
693 			(ibatt_sum2 > 0)) {
694 		/* calculate resistor in discharging case */
695 		data = 1000 * (vbatt_sum1 - vbatt_sum2)
696 		    / (ibatt_sum1 - ibatt_sum2);
697 		if ((data - info->resistor > 0) &&
698 				(data - info->resistor < info->resistor))
699 			info->resistor = data;
700 		if ((info->resistor - data > 0) &&
701 				(info->resistor - data < data))
702 			info->resistor = data;
703 	}
704 	return 0;
705 
706 out_meas:
707 	set_charger_current(info, chg_current, &ret);
708 out:
709 	return -EINVAL;
710 }
711 
calc_capacity(struct pm860x_battery_info * info,int * cap)712 static int calc_capacity(struct pm860x_battery_info *info, int *cap)
713 {
714 	int ret;
715 	int data;
716 	int ibat;
717 	int cap_ocv = 0;
718 	int cap_cc = 0;
719 
720 	ret = calc_ccnt(info, &ccnt_data);
721 	if (ret)
722 		goto out;
723 soc:
724 	data = info->max_capacity * info->start_soc / 100;
725 	if (ccnt_data.total_dischg - ccnt_data.total_chg <= data) {
726 		cap_cc =
727 		    data + ccnt_data.total_chg - ccnt_data.total_dischg;
728 	} else {
729 		clear_ccnt(info, &ccnt_data);
730 		calc_soc(info, OCV_MODE_ACTIVE, &info->start_soc);
731 		dev_dbg(info->dev, "restart soc = %d !\n",
732 			info->start_soc);
733 		goto soc;
734 	}
735 
736 	cap_cc = cap_cc * 100 / info->max_capacity;
737 	if (cap_cc < 0)
738 		cap_cc = 0;
739 	else if (cap_cc > 100)
740 		cap_cc = 100;
741 
742 	dev_dbg(info->dev, "%s, last cap : %d", __func__,
743 		info->last_capacity);
744 
745 	ret = measure_current(info, &ibat);
746 	if (ret)
747 		goto out;
748 	/* Calculate the capacity when discharging(ibat < 0) */
749 	if (ibat < 0) {
750 		ret = calc_soc(info, OCV_MODE_ACTIVE, &cap_ocv);
751 		if (ret)
752 			cap_ocv = info->last_capacity;
753 		ret = measure_vbatt(info, OCV_MODE_ACTIVE, &data);
754 		if (ret)
755 			goto out;
756 		if (data <= LOW_BAT_THRESHOLD) {
757 			/* choose the lower capacity value to report
758 			 * between vbat and CC when vbat < 3.6v;
759 			 * than 3.6v;
760 			 */
761 			*cap = min(cap_ocv, cap_cc);
762 		} else {
763 			/* when detect vbat > 3.6v, but cap_cc < 15,and
764 			 * cap_ocv is 10% larger than cap_cc, we can think
765 			 * CC have some accumulation error, switch to OCV
766 			 * to estimate capacity;
767 			 * */
768 			if (cap_cc < 15 && cap_ocv - cap_cc > 10)
769 				*cap = cap_ocv;
770 			else
771 				*cap = cap_cc;
772 		}
773 		/* when discharging, make sure current capacity
774 		 * is lower than last*/
775 		if (*cap > info->last_capacity)
776 			*cap = info->last_capacity;
777 	} else {
778 		*cap = cap_cc;
779 	}
780 	info->last_capacity = *cap;
781 
782 	dev_dbg(info->dev, "%s, cap_ocv:%d cap_cc:%d, cap:%d\n",
783 		(ibat < 0) ? "discharging" : "charging",
784 		 cap_ocv, cap_cc, *cap);
785 	/*
786 	 * store the current capacity to RTC domain register,
787 	 * after next power up , it will be restored.
788 	 */
789 	pm860x_set_bits(info->i2c, PM8607_RTC_MISC2, RTC_SOC_5LSB,
790 			(*cap & 0x1F) << 3);
791 	pm860x_set_bits(info->i2c, PM8607_RTC1, RTC_SOC_3MSB,
792 			((*cap >> 5) & 0x3));
793 	return 0;
794 out:
795 	return ret;
796 }
797 
pm860x_external_power_changed(struct power_supply * psy)798 static void pm860x_external_power_changed(struct power_supply *psy)
799 {
800 	struct pm860x_battery_info *info = dev_get_drvdata(psy->dev.parent);
801 
802 	calc_resistor(info);
803 }
804 
pm860x_batt_get_prop(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)805 static int pm860x_batt_get_prop(struct power_supply *psy,
806 				enum power_supply_property psp,
807 				union power_supply_propval *val)
808 {
809 	struct pm860x_battery_info *info = dev_get_drvdata(psy->dev.parent);
810 	int data;
811 	int ret;
812 
813 	switch (psp) {
814 	case POWER_SUPPLY_PROP_PRESENT:
815 		val->intval = info->present;
816 		break;
817 	case POWER_SUPPLY_PROP_CAPACITY:
818 		ret = calc_capacity(info, &data);
819 		if (ret)
820 			return ret;
821 		if (data < 0)
822 			data = 0;
823 		else if (data > 100)
824 			data = 100;
825 		/* return 100 if battery is not attached */
826 		if (!info->present)
827 			data = 100;
828 		val->intval = data;
829 		break;
830 	case POWER_SUPPLY_PROP_TECHNOLOGY:
831 		val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
832 		break;
833 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
834 		/* return real vbatt Voltage */
835 		ret = measure_vbatt(info, OCV_MODE_ACTIVE, &data);
836 		if (ret)
837 			return ret;
838 		val->intval = data * 1000;
839 		break;
840 	case POWER_SUPPLY_PROP_VOLTAGE_AVG:
841 		/* return Open Circuit Voltage (not measured voltage) */
842 		ret = calc_ocv(info, &data);
843 		if (ret)
844 			return ret;
845 		val->intval = data * 1000;
846 		break;
847 	case POWER_SUPPLY_PROP_CURRENT_NOW:
848 		ret = measure_current(info, &data);
849 		if (ret)
850 			return ret;
851 		val->intval = data;
852 		break;
853 	case POWER_SUPPLY_PROP_TEMP:
854 		if (info->present) {
855 			ret = measure_temp(info, &data);
856 			if (ret)
857 				return ret;
858 			data *= 10;
859 		} else {
860 			/* Fake Temp 25C Without Battery */
861 			data = 250;
862 		}
863 		val->intval = data;
864 		break;
865 	default:
866 		return -ENODEV;
867 	}
868 	return 0;
869 }
870 
pm860x_batt_set_prop(struct power_supply * psy,enum power_supply_property psp,const union power_supply_propval * val)871 static int pm860x_batt_set_prop(struct power_supply *psy,
872 				       enum power_supply_property psp,
873 				       const union power_supply_propval *val)
874 {
875 	struct pm860x_battery_info *info = dev_get_drvdata(psy->dev.parent);
876 
877 	switch (psp) {
878 	case POWER_SUPPLY_PROP_CHARGE_FULL:
879 		clear_ccnt(info, &ccnt_data);
880 		info->start_soc = 100;
881 		dev_dbg(info->dev, "chg done, update soc = %d\n",
882 			info->start_soc);
883 		break;
884 	default:
885 		return -EPERM;
886 	}
887 
888 	return 0;
889 }
890 
891 
892 static enum power_supply_property pm860x_batt_props[] = {
893 	POWER_SUPPLY_PROP_PRESENT,
894 	POWER_SUPPLY_PROP_CAPACITY,
895 	POWER_SUPPLY_PROP_TECHNOLOGY,
896 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
897 	POWER_SUPPLY_PROP_VOLTAGE_AVG,
898 	POWER_SUPPLY_PROP_CURRENT_NOW,
899 	POWER_SUPPLY_PROP_TEMP,
900 };
901 
902 static const struct power_supply_desc pm860x_battery_desc = {
903 	.name			= "battery-monitor",
904 	.type			= POWER_SUPPLY_TYPE_BATTERY,
905 	.properties		= pm860x_batt_props,
906 	.num_properties		= ARRAY_SIZE(pm860x_batt_props),
907 	.get_property		= pm860x_batt_get_prop,
908 	.set_property		= pm860x_batt_set_prop,
909 	.external_power_changed	= pm860x_external_power_changed,
910 };
911 
pm860x_battery_probe(struct platform_device * pdev)912 static int pm860x_battery_probe(struct platform_device *pdev)
913 {
914 	struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
915 	struct pm860x_battery_info *info;
916 	struct pm860x_power_pdata *pdata;
917 	int ret;
918 
919 	info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
920 	if (!info)
921 		return -ENOMEM;
922 
923 	info->irq_cc = platform_get_irq(pdev, 0);
924 	if (info->irq_cc < 0)
925 		return info->irq_cc;
926 
927 	info->irq_batt = platform_get_irq(pdev, 1);
928 	if (info->irq_batt < 0)
929 		return info->irq_batt;
930 
931 	info->chip = chip;
932 	info->i2c =
933 	    (chip->id == CHIP_PM8607) ? chip->client : chip->companion;
934 	info->dev = &pdev->dev;
935 	info->status = POWER_SUPPLY_STATUS_UNKNOWN;
936 	pdata = pdev->dev.platform_data;
937 
938 	mutex_init(&info->lock);
939 	platform_set_drvdata(pdev, info);
940 
941 	pm860x_init_battery(info);
942 
943 	if (pdata && pdata->max_capacity)
944 		info->max_capacity = pdata->max_capacity;
945 	else
946 		info->max_capacity = 1500;	/* set default capacity */
947 	if (pdata && pdata->resistor)
948 		info->resistor = pdata->resistor;
949 	else
950 		info->resistor = 300;	/* set default internal resistor */
951 
952 	info->battery = devm_power_supply_register(&pdev->dev,
953 						   &pm860x_battery_desc,
954 						   NULL);
955 	if (IS_ERR(info->battery))
956 		return PTR_ERR(info->battery);
957 	info->battery->dev.parent = &pdev->dev;
958 
959 	ret = devm_request_threaded_irq(chip->dev, info->irq_cc, NULL,
960 					pm860x_coulomb_handler, IRQF_ONESHOT,
961 					"coulomb", info);
962 	if (ret < 0) {
963 		dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n",
964 			info->irq_cc, ret);
965 		return ret;
966 	}
967 
968 	ret = devm_request_threaded_irq(chip->dev, info->irq_batt, NULL,
969 					pm860x_batt_handler,
970 					IRQF_ONESHOT, "battery", info);
971 	if (ret < 0) {
972 		dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n",
973 			info->irq_batt, ret);
974 		return ret;
975 	}
976 
977 
978 	return 0;
979 }
980 
981 #ifdef CONFIG_PM_SLEEP
pm860x_battery_suspend(struct device * dev)982 static int pm860x_battery_suspend(struct device *dev)
983 {
984 	struct platform_device *pdev = to_platform_device(dev);
985 	struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
986 
987 	if (device_may_wakeup(dev))
988 		chip->wakeup_flag |= 1 << PM8607_IRQ_CC;
989 	return 0;
990 }
991 
pm860x_battery_resume(struct device * dev)992 static int pm860x_battery_resume(struct device *dev)
993 {
994 	struct platform_device *pdev = to_platform_device(dev);
995 	struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
996 
997 	if (device_may_wakeup(dev))
998 		chip->wakeup_flag &= ~(1 << PM8607_IRQ_CC);
999 	return 0;
1000 }
1001 #endif
1002 
1003 static SIMPLE_DEV_PM_OPS(pm860x_battery_pm_ops,
1004 			pm860x_battery_suspend, pm860x_battery_resume);
1005 
1006 static struct platform_driver pm860x_battery_driver = {
1007 	.driver = {
1008 		   .name = "88pm860x-battery",
1009 		   .pm = &pm860x_battery_pm_ops,
1010 	},
1011 	.probe = pm860x_battery_probe,
1012 };
1013 module_platform_driver(pm860x_battery_driver);
1014 
1015 MODULE_DESCRIPTION("Marvell 88PM860x Battery driver");
1016 MODULE_LICENSE("GPL");
1017