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 <jtzhou@marvell.com>
7 * Haojian Zhuang <haojian.zhuang@marvell.com>
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/mfd/88pm860x.h>
18 #include <linux/delay.h>
19
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)
24
25 /* bit definitions of Measurement Enable 1 Register */
26 #define MEAS1_TINT (1 << 3)
27 #define MEAS1_GP1 (1 << 5)
28
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)
33
34 /* bit definitions of Measurement Off Time Register */
35 #define MEAS_OFF_SLEEP_EN (1 << 1)
36
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)
41
42 /* bit definitions of GPADC Misc 1 Register */
43 #define GPMISC1_GPADC_EN (1 << 0)
44
45 /* bit definitions of Charger Control 6 Register */
46 #define CC6_BAT_DET_GPADC1 1
47
48 /* bit definitions of Coulomb Counter Reading Register */
49 #define CCNT_AVG_SEL (4 << 3)
50
51 /* bit definitions of RTC miscellaneous Register1 */
52 #define RTC_SOC_5LSB (0x1F << 3)
53
54 /* bit definitions of RTC Register1 */
55 #define RTC_SOC_3MSB (0x7)
56
57 /* bit definitions of Power up Log register */
58 #define BAT_WU_LOG (1<<6)
59
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
67
68 /* OCV -- Open Circuit Voltage */
69 #define OCV_MODE_ACTIVE 0
70 #define OCV_MODE_SLEEP 1
71
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
76
77 /* TBAT for batt, TINT for chip itself */
78 #define PM860X_TEMP_TINT (0)
79 #define PM860X_TEMP_TBAT (1)
80
81 /*
82 * Battery temperature based on NTC resistor, defined
83 * corresponding resistor value -- Ohm / C degeree.
84 */
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 */
92
93 struct pm860x_battery_info {
94 struct pm860x_chip *chip;
95 struct i2c_client *i2c;
96 struct device *dev;
97
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 */
109 };
110
111 struct ccnt {
112 unsigned long long pos;
113 unsigned long long neg;
114 unsigned int spos;
115 unsigned int sneg;
116
117 int total_chg; /* mAh(3.6C) */
118 int total_dischg; /* mAh(3.6C) */
119 };
120
121 /*
122 * State of Charge.
123 * The first number is mAh(=3.6C), and the second number is percent point.
124 */
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},
146 };
147
148 static struct ccnt ccnt_data;
149
150 /*
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
153 */
measure_12bit_voltage(struct pm860x_battery_info * info,int offset,int * data)154 static int measure_12bit_voltage(struct pm860x_battery_info *info,
155 int offset, int *data)
156 {
157 unsigned char buf[2];
158 int ret;
159
160 ret = pm860x_bulk_read(info->i2c, offset, 2, buf);
161 if (ret < 0)
162 return ret;
163
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;
168 }
169
measure_vbatt(struct pm860x_battery_info * info,int state,int * data)170 static int measure_vbatt(struct pm860x_battery_info *info, int state,
171 int *data)
172 {
173 unsigned char buf[5];
174 int ret;
175
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:
185 /*
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)
193 */
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;
205 }
206 return 0;
207 }
208
209 /*
210 * Return value is signed data.
211 * Negative value means discharging, and positive value means charging.
212 */
measure_current(struct pm860x_battery_info * info,int * data)213 static int measure_current(struct pm860x_battery_info *info, int *data)
214 {
215 unsigned char buf[2];
216 short s;
217 int ret;
218
219 ret = pm860x_bulk_read(info->i2c, PM8607_IBAT_MEAS1, 2, buf);
220 if (ret < 0)
221 return ret;
222
223 s = ((buf[0] & 0xff) << 8) | (buf[1] & 0xff);
224 /* current(mA) = value * 0.125 */
225 *data = s >> 3;
226 return 0;
227 }
228
set_charger_current(struct pm860x_battery_info * info,int data,int * old)229 static int set_charger_current(struct pm860x_battery_info *info, int data,
230 int *old)
231 {
232 int ret;
233
234 if (data < 50 || data > 1600 || !old)
235 return -EINVAL;
236
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;
244 }
245
read_ccnt(struct pm860x_battery_info * info,int offset,int * ccnt)246 static int read_ccnt(struct pm860x_battery_info *info, int offset,
247 int *ccnt)
248 {
249 unsigned char buf[2];
250 int ret;
251
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;
262 }
263
calc_ccnt(struct pm860x_battery_info * info,struct ccnt * ccnt)264 static int calc_ccnt(struct pm860x_battery_info *info, struct ccnt *ccnt)
265 {
266 unsigned int sum;
267 int ret;
268 int data;
269
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;
279
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;
290
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;
298
299 /*
300 * charge(mAh) = count * 1.6984 * 1e(-8)
301 * = count * 16984 * 1.024 * 1.024 * 1.024 / (2 ^ 40)
302 * = count * 18236 / (2 ^ 40)
303 */
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;
309 }
310
clear_ccnt(struct pm860x_battery_info * info,struct ccnt * ccnt)311 static int clear_ccnt(struct pm860x_battery_info *info, struct ccnt *ccnt)
312 {
313 int data;
314
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;
324 }
325
326 /* Calculate Open Circuit Voltage */
calc_ocv(struct pm860x_battery_info * info,int * ocv)327 static int calc_ocv(struct pm860x_battery_info *info, int *ocv)
328 {
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;
336
337 if (!ocv)
338 return -EINVAL;
339
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;
349 }
350 vbatt_avg = vbatt_sum / 10;
351 ibatt_avg = ibatt_sum / 10;
352
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;
363 }
364
365 /* Calculate State of Charge (percent points) */
calc_soc(struct pm860x_battery_info * info,int state,int * soc)366 static int calc_soc(struct pm860x_battery_info *info, int state, int *soc)
367 {
368 int i;
369 int ocv;
370 int count;
371 int ret = -EINVAL;
372
373 if (!soc)
374 return -EINVAL;
375
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;
383 }
384 if (ret)
385 return ret;
386
387 count = ARRAY_SIZE(array_soc);
388 if (ocv < array_soc[count - 1][0]) {
389 *soc = 0;
390 return 0;
391 }
392
393 for (i = 0; i < count; i++) {
394 if (ocv >= array_soc[i][0]) {
395 *soc = array_soc[i][1];
396 break;
397 }
398 }
399 return 0;
400 }
401
pm860x_coulomb_handler(int irq,void * data)402 static irqreturn_t pm860x_coulomb_handler(int irq, void *data)
403 {
404 struct pm860x_battery_info *info = data;
405
406 calc_ccnt(info, &ccnt_data);
407 return IRQ_HANDLED;
408 }
409
pm860x_batt_handler(int irq,void * data)410 static irqreturn_t pm860x_batt_handler(int irq, void *data)
411 {
412 struct pm860x_battery_info *info = data;
413 int ret;
414
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;
423 }
424 mutex_unlock(&info->lock);
425 /* clear ccnt since battery is attached or dettached */
426 clear_ccnt(info, &ccnt_data);
427 return IRQ_HANDLED;
428 }
429
pm860x_init_battery(struct pm860x_battery_info * info)430 static void pm860x_init_battery(struct pm860x_battery_info *info)
431 {
432 unsigned char buf[2];
433 int ret;
434 int data;
435 int bat_remove;
436 int soc = 0;
437
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;
445
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;
451
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;
459
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;
465
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;
471
472 ret = pm860x_set_bits(info->i2c, PM8607_CCNT, 7 << 3,
473 CCNT_AVG_SEL);
474 if (ret < 0)
475 goto out;
476
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;
482
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;
489 }
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;
496 }
497 mutex_unlock(&info->lock);
498
499 ret = calc_soc(info, OCV_MODE_ACTIVE, &soc);
500 if (ret < 0)
501 goto out;
502
503 data = pm860x_reg_read(info->i2c, PM8607_POWER_UP_LOG);
504 bat_remove = data & BAT_WU_LOG;
505
506 dev_dbg(info->dev, "battery wake up? %s\n",
507 bat_remove != 0 ? "yes" : "no");
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 /* meausered 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 -EINVAL;
926
927 info->irq_batt = platform_get_irq(pdev, 1);
928 if (info->irq_batt <= 0)
929 return -EINVAL;
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