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