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