1 // SPDX-License-Identifier: GPL-2.0-only
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>
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 */
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
;
98 struct power_supply
*battery
;
104 int resistor
; /* Battery Internal Resistor */
108 unsigned temp_type
:1; /* TINT or TBAT */
112 unsigned long long int pos
;
113 unsigned long long int neg
;
117 int total_chg
; /* mAh(3.6C) */
118 int total_dischg
; /* mAh(3.6C) */
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];
160 ret
= pm860x_bulk_read(info
->i2c
, offset
, 2, buf
);
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;
170 static int measure_vbatt(struct pm860x_battery_info
*info
, int state
,
173 unsigned char buf
[5];
177 case OCV_MODE_ACTIVE
:
178 ret
= measure_12bit_voltage(info
, PM8607_VBAT_MEAS1
, data
);
181 /* V_BATT_MEAS(mV) = value * 3 * 1.8 * 1000 / (2^12) */
186 * voltage value of VBATT in sleep mode is saved in different
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
);
197 ret
= ((buf
[4] >> 6) << 10) | ((buf
[3] >> 6) << 8)
198 | ((buf
[2] >> 6) << 6) | ((buf
[1] >> 6) << 4)
200 /* V_BATT_MEAS(mV) = data * 3 * 1.8 * 1000 / (2^12) */
201 *data
= ((*data
& 0xff) * 27 * 25) >> 9;
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];
219 ret
= pm860x_bulk_read(info
->i2c
, PM8607_IBAT_MEAS1
, 2, buf
);
223 s
= ((buf
[0] & 0xff) << 8) | (buf
[1] & 0xff);
224 /* current(mA) = value * 0.125 */
229 static int set_charger_current(struct pm860x_battery_info
*info
, int data
,
234 if (data
< 50 || data
> 1600 || !old
)
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
);
246 static int read_ccnt(struct pm860x_battery_info
*info
, int offset
,
249 unsigned char buf
[2];
252 ret
= pm860x_set_bits(info
->i2c
, PM8607_CCNT
, 7, offset
& 7);
255 ret
= pm860x_bulk_read(info
->i2c
, PM8607_CCNT_MEAS1
, 2, buf
);
258 *ccnt
= ((buf
[0] & 0xff) << 8) | (buf
[1] & 0xff);
264 static int calc_ccnt(struct pm860x_battery_info
*info
, struct ccnt
*ccnt
)
270 ret
= read_ccnt(info
, CCNT_POS1
, &data
);
274 ret
= read_ccnt(info
, CCNT_POS2
, &data
);
277 sum
|= (data
& 0xffff) << 16;
280 ret
= read_ccnt(info
, CCNT_NEG1
, &data
);
284 ret
= read_ccnt(info
, CCNT_NEG2
, &data
);
287 sum
|= (data
& 0xffff) << 16;
288 sum
= ~sum
+ 1; /* since it's negative */
291 ret
= read_ccnt(info
, CCNT_SPOS
, &data
);
295 ret
= read_ccnt(info
, CCNT_SNEG
, &data
);
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);
311 static int clear_ccnt(struct pm860x_battery_info
*info
, struct ccnt
*ccnt
)
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
);
326 /* Calculate Open Circuit Voltage */
327 static int calc_ocv(struct pm860x_battery_info
*info
, int *ocv
)
340 for (i
= 0, ibatt_sum
= 0, vbatt_sum
= 0; i
< 10; i
++) {
341 ret
= measure_vbatt(info
, OCV_MODE_ACTIVE
, &data
);
345 ret
= measure_current(info
, &data
);
350 vbatt_avg
= vbatt_sum
/ 10;
351 ibatt_avg
= ibatt_sum
/ 10;
353 mutex_lock(&info
->lock
);
355 *ocv
= vbatt_avg
- ibatt_avg
* info
->resistor
/ 1000;
358 mutex_unlock(&info
->lock
);
359 dev_dbg(info
->dev
, "VBAT average:%d, OCV:%d\n", vbatt_avg
, *ocv
);
365 /* Calculate State of Charge (percent points) */
366 static int calc_soc(struct pm860x_battery_info
*info
, int state
, int *soc
)
377 case OCV_MODE_ACTIVE
:
378 ret
= calc_ocv(info
, &ocv
);
381 ret
= measure_vbatt(info
, OCV_MODE_SLEEP
, &ocv
);
387 count
= ARRAY_SIZE(array_soc
);
388 if (ocv
< array_soc
[count
- 1][0]) {
393 for (i
= 0; i
< count
; i
++) {
394 if (ocv
>= array_soc
[i
][0]) {
395 *soc
= array_soc
[i
][1];
402 static irqreturn_t
pm860x_coulomb_handler(int irq
, void *data
)
404 struct pm860x_battery_info
*info
= data
;
406 calc_ccnt(info
, &ccnt_data
);
410 static irqreturn_t
pm860x_batt_handler(int irq
, void *data
)
412 struct pm860x_battery_info
*info
= data
;
415 mutex_lock(&info
->lock
);
416 ret
= pm860x_reg_read(info
->i2c
, PM8607_STATUS_2
);
417 if (ret
& STATUS2_BAT
) {
419 info
->temp_type
= PM860X_TEMP_TBAT
;
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
);
430 static void pm860x_init_battery(struct pm860x_battery_info
*info
)
432 unsigned char buf
[2];
438 /* measure enable on GPADC1 */
440 if (info
->temp_type
== PM860X_TEMP_TINT
)
442 ret
= pm860x_set_bits(info
->i2c
, PM8607_MEAS_EN1
, data
, data
);
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
);
452 /* measure disable CC in sleep time */
453 ret
= pm860x_reg_write(info
->i2c
, PM8607_MEAS_OFF_TIME1
, 0x82);
456 ret
= pm860x_reg_write(info
->i2c
, PM8607_MEAS_OFF_TIME2
, 0x6c);
461 ret
= pm860x_set_bits(info
->i2c
, PM8607_GPADC_MISC1
,
462 GPMISC1_GPADC_EN
, GPMISC1_GPADC_EN
);
466 /* detect battery via GPADC1 */
467 ret
= pm860x_set_bits(info
->i2c
, PM8607_CHG_CTRL6
,
468 CC6_BAT_DET_GPADC1
, CC6_BAT_DET_GPADC1
);
472 ret
= pm860x_set_bits(info
->i2c
, PM8607_CCNT
, 7 << 3,
477 /* set GPADC1 bias */
478 ret
= pm860x_set_bits(info
->i2c
, PM8607_GP_BIAS2
, 0xF << 4,
483 /* check whether battery present) */
484 mutex_lock(&info
->lock
);
485 ret
= pm860x_reg_read(info
->i2c
, PM8607_STATUS_2
);
487 mutex_unlock(&info
->lock
);
490 if (ret
& STATUS2_BAT
) {
492 info
->temp_type
= PM860X_TEMP_TBAT
;
495 info
->temp_type
= PM860X_TEMP_TINT
;
497 mutex_unlock(&info
->lock
);
499 ret
= calc_soc(info
, OCV_MODE_ACTIVE
, &soc
);
503 data
= pm860x_reg_read(info
->i2c
, PM8607_POWER_UP_LOG
);
504 bat_remove
= data
& BAT_WU_LOG
;
506 dev_dbg(info
->dev
, "battery wake up? %s\n",
507 bat_remove
!= 0 ? "yes" : "no");
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);
515 info
->start_soc
= soc
;
516 else if (data
< soc
- 15)
517 info
->start_soc
= soc
;
519 info
->start_soc
= data
;
520 dev_dbg(info
->dev
, "soc_rtc %d, soc_ocv :%d\n", data
, soc
);
522 pm860x_set_bits(info
->i2c
, PM8607_POWER_UP_LOG
,
523 BAT_WU_LOG
, BAT_WU_LOG
);
524 info
->start_soc
= soc
;
526 info
->last_capacity
= info
->start_soc
;
527 dev_dbg(info
->dev
, "init soc : %d\n", info
->last_capacity
);
532 static void set_temp_threshold(struct pm860x_battery_info
*info
,
537 /* (tmp << 8) / 1800 */
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
);
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
);
553 static int measure_temp(struct pm860x_battery_info
*info
, int *data
)
560 if (info
->temp_type
== PM860X_TEMP_TINT
) {
561 ret
= measure_12bit_voltage(info
, PM8607_TINT_MEAS1
, data
);
564 *data
= (*data
- 884) * 1000 / 3611;
566 ret
= measure_12bit_voltage(info
, PM8607_GPADC1_MEAS1
, data
);
569 /* meausered Vtbat(mV) / Ibias_current(11uA)*/
570 *data
= (*data
* 1000) / GPBIAS2_GPADC1_UA
;
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
);
606 min
= TBAT_40D
* GPBIAS2_GPADC1_UA
/ 1000;
607 set_temp_threshold(info
, min
, 0);
608 temp
= 45; /* over heat ,suppose 45 roughly */
611 dev_dbg(info
->dev
, "temp_C:%d C,temp_mv:%d mv\n", temp
, *data
);
617 static int calc_resistor(struct pm860x_battery_info
*info
)
628 ret
= measure_current(info
, &data
);
629 /* make sure that charging is launched by data > 0 */
633 ret
= measure_vbatt(info
, OCV_MODE_ACTIVE
, &data
);
636 /* calculate resistor only in CC charge mode */
637 if (data
< VBATT_RESISTOR_MIN
|| data
> VBATT_RESISTOR_MAX
)
640 /* current is saved */
641 if (set_charger_current(info
, 500, &chg_current
))
645 * set charge current as 500mA, wait about 500ms till charging
646 * process is launched and stable with the newer charging current.
650 for (i
= 0, vbatt_sum1
= 0, ibatt_sum1
= 0; i
< 10; i
++) {
651 ret
= measure_vbatt(info
, OCV_MODE_ACTIVE
, &data
);
655 ret
= measure_current(info
, &data
);
660 ibatt_sum1
= ibatt_sum1
- data
; /* discharging */
662 ibatt_sum1
= ibatt_sum1
+ data
; /* charging */
665 if (set_charger_current(info
, 100, &ret
))
668 * set charge current as 100mA, wait about 500ms till charging
669 * process is launched and stable with the newer charging current.
673 for (i
= 0, vbatt_sum2
= 0, ibatt_sum2
= 0; i
< 10; i
++) {
674 ret
= measure_vbatt(info
, OCV_MODE_ACTIVE
, &data
);
678 ret
= measure_current(info
, &data
);
683 ibatt_sum2
= ibatt_sum2
- data
; /* discharging */
685 ibatt_sum2
= ibatt_sum2
+ data
; /* charging */
688 /* restore current setting */
689 if (set_charger_current(info
, chg_current
, &ret
))
692 if ((vbatt_sum1
> vbatt_sum2
) && (ibatt_sum1
> ibatt_sum2
) &&
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
;
707 set_charger_current(info
, chg_current
, &ret
);
712 static int calc_capacity(struct pm860x_battery_info
*info
, int *cap
)
720 ret
= calc_ccnt(info
, &ccnt_data
);
724 data
= info
->max_capacity
* info
->start_soc
/ 100;
725 if (ccnt_data
.total_dischg
- ccnt_data
.total_chg
<= data
) {
727 data
+ ccnt_data
.total_chg
- ccnt_data
.total_dischg
;
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",
736 cap_cc
= cap_cc
* 100 / info
->max_capacity
;
739 else if (cap_cc
> 100)
742 dev_dbg(info
->dev
, "%s, last cap : %d", __func__
,
743 info
->last_capacity
);
745 ret
= measure_current(info
, &ibat
);
748 /* Calculate the capacity when discharging(ibat < 0) */
750 ret
= calc_soc(info
, OCV_MODE_ACTIVE
, &cap_ocv
);
752 cap_ocv
= info
->last_capacity
;
753 ret
= measure_vbatt(info
, OCV_MODE_ACTIVE
, &data
);
756 if (data
<= LOW_BAT_THRESHOLD
) {
757 /* choose the lower capacity value to report
758 * between vbat and CC when vbat < 3.6v;
761 *cap
= min(cap_ocv
, cap_cc
);
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;
768 if (cap_cc
< 15 && cap_ocv
- cap_cc
> 10)
773 /* when discharging, make sure current capacity
774 * is lower than last*/
775 if (*cap
> info
->last_capacity
)
776 *cap
= info
->last_capacity
;
780 info
->last_capacity
= *cap
;
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
);
786 * store the current capacity to RTC domain register,
787 * after next power up , it will be restored.
789 pm860x_set_bits(info
->i2c
, PM8607_RTC_MISC2
, RTC_SOC_5LSB
,
791 pm860x_set_bits(info
->i2c
, PM8607_RTC1
, RTC_SOC_3MSB
,
792 ((*cap
>> 5) & 0x3));
798 static void pm860x_external_power_changed(struct power_supply
*psy
)
800 struct pm860x_battery_info
*info
= dev_get_drvdata(psy
->dev
.parent
);
805 static int pm860x_batt_get_prop(struct power_supply
*psy
,
806 enum power_supply_property psp
,
807 union power_supply_propval
*val
)
809 struct pm860x_battery_info
*info
= dev_get_drvdata(psy
->dev
.parent
);
814 case POWER_SUPPLY_PROP_PRESENT
:
815 val
->intval
= info
->present
;
817 case POWER_SUPPLY_PROP_CAPACITY
:
818 ret
= calc_capacity(info
, &data
);
825 /* return 100 if battery is not attached */
830 case POWER_SUPPLY_PROP_TECHNOLOGY
:
831 val
->intval
= POWER_SUPPLY_TECHNOLOGY_LION
;
833 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
834 /* return real vbatt Voltage */
835 ret
= measure_vbatt(info
, OCV_MODE_ACTIVE
, &data
);
838 val
->intval
= data
* 1000;
840 case POWER_SUPPLY_PROP_VOLTAGE_AVG
:
841 /* return Open Circuit Voltage (not measured voltage) */
842 ret
= calc_ocv(info
, &data
);
845 val
->intval
= data
* 1000;
847 case POWER_SUPPLY_PROP_CURRENT_NOW
:
848 ret
= measure_current(info
, &data
);
853 case POWER_SUPPLY_PROP_TEMP
:
855 ret
= measure_temp(info
, &data
);
860 /* Fake Temp 25C Without Battery */
871 static int pm860x_batt_set_prop(struct power_supply
*psy
,
872 enum power_supply_property psp
,
873 const union power_supply_propval
*val
)
875 struct pm860x_battery_info
*info
= dev_get_drvdata(psy
->dev
.parent
);
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",
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
,
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
,
912 static int pm860x_battery_probe(struct platform_device
*pdev
)
914 struct pm860x_chip
*chip
= dev_get_drvdata(pdev
->dev
.parent
);
915 struct pm860x_battery_info
*info
;
916 struct pm860x_power_pdata
*pdata
;
919 info
= devm_kzalloc(&pdev
->dev
, sizeof(*info
), GFP_KERNEL
);
923 info
->irq_cc
= platform_get_irq(pdev
, 0);
924 if (info
->irq_cc
<= 0)
927 info
->irq_batt
= platform_get_irq(pdev
, 1);
928 if (info
->irq_batt
<= 0)
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
;
938 mutex_init(&info
->lock
);
939 platform_set_drvdata(pdev
, info
);
941 pm860x_init_battery(info
);
943 if (pdata
&& pdata
->max_capacity
)
944 info
->max_capacity
= pdata
->max_capacity
;
946 info
->max_capacity
= 1500; /* set default capacity */
947 if (pdata
&& pdata
->resistor
)
948 info
->resistor
= pdata
->resistor
;
950 info
->resistor
= 300; /* set default internal resistor */
952 info
->battery
= devm_power_supply_register(&pdev
->dev
,
953 &pm860x_battery_desc
,
955 if (IS_ERR(info
->battery
))
956 return PTR_ERR(info
->battery
);
957 info
->battery
->dev
.parent
= &pdev
->dev
;
959 ret
= devm_request_threaded_irq(chip
->dev
, info
->irq_cc
, NULL
,
960 pm860x_coulomb_handler
, IRQF_ONESHOT
,
963 dev_err(chip
->dev
, "Failed to request IRQ: #%d: %d\n",
968 ret
= devm_request_threaded_irq(chip
->dev
, info
->irq_batt
, NULL
,
970 IRQF_ONESHOT
, "battery", info
);
972 dev_err(chip
->dev
, "Failed to request IRQ: #%d: %d\n",
973 info
->irq_batt
, ret
);
981 #ifdef CONFIG_PM_SLEEP
982 static int pm860x_battery_suspend(struct device
*dev
)
984 struct platform_device
*pdev
= to_platform_device(dev
);
985 struct pm860x_chip
*chip
= dev_get_drvdata(pdev
->dev
.parent
);
987 if (device_may_wakeup(dev
))
988 chip
->wakeup_flag
|= 1 << PM8607_IRQ_CC
;
992 static int pm860x_battery_resume(struct device
*dev
)
994 struct platform_device
*pdev
= to_platform_device(dev
);
995 struct pm860x_chip
*chip
= dev_get_drvdata(pdev
->dev
.parent
);
997 if (device_may_wakeup(dev
))
998 chip
->wakeup_flag
&= ~(1 << PM8607_IRQ_CC
);
1003 static SIMPLE_DEV_PM_OPS(pm860x_battery_pm_ops
,
1004 pm860x_battery_suspend
, pm860x_battery_resume
);
1006 static struct platform_driver pm860x_battery_driver
= {
1008 .name
= "88pm860x-battery",
1009 .pm
= &pm860x_battery_pm_ops
,
1011 .probe
= pm860x_battery_probe
,
1013 module_platform_driver(pm860x_battery_driver
);
1015 MODULE_DESCRIPTION("Marvell 88PM860x Battery driver");
1016 MODULE_LICENSE("GPL");