Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Battery driver for Marvell 88PM860x PMIC
0004  *
0005  * Copyright (c) 2012 Marvell International Ltd.
0006  * Author:  Jett Zhou <jtzhou@marvell.com>
0007  *      Haojian Zhuang <haojian.zhuang@marvell.com>
0008  */
0009 
0010 #include <linux/kernel.h>
0011 #include <linux/module.h>
0012 #include <linux/platform_device.h>
0013 #include <linux/slab.h>
0014 #include <linux/mutex.h>
0015 #include <linux/string.h>
0016 #include <linux/power_supply.h>
0017 #include <linux/mfd/88pm860x.h>
0018 #include <linux/delay.h>
0019 
0020 /* bit definitions of Status Query Interface 2 */
0021 #define STATUS2_CHG         (1 << 2)
0022 #define STATUS2_BAT         (1 << 3)
0023 #define STATUS2_VBUS            (1 << 4)
0024 
0025 /* bit definitions of Measurement Enable 1 Register */
0026 #define MEAS1_TINT          (1 << 3)
0027 #define MEAS1_GP1           (1 << 5)
0028 
0029 /* bit definitions of Measurement Enable 3 Register */
0030 #define MEAS3_IBAT          (1 << 0)
0031 #define MEAS3_BAT_DET           (1 << 1)
0032 #define MEAS3_CC            (1 << 2)
0033 
0034 /* bit definitions of Measurement Off Time Register */
0035 #define MEAS_OFF_SLEEP_EN       (1 << 1)
0036 
0037 /* bit definitions of GPADC Bias Current 2 Register */
0038 #define GPBIAS2_GPADC1_SET      (2 << 4)
0039 /* GPADC1 Bias Current value in uA unit */
0040 #define GPBIAS2_GPADC1_UA       ((GPBIAS2_GPADC1_SET >> 4) * 5 + 1)
0041 
0042 /* bit definitions of GPADC Misc 1 Register */
0043 #define GPMISC1_GPADC_EN        (1 << 0)
0044 
0045 /* bit definitions of Charger Control 6 Register */
0046 #define CC6_BAT_DET_GPADC1      1
0047 
0048 /* bit definitions of Coulomb Counter Reading Register */
0049 #define CCNT_AVG_SEL            (4 << 3)
0050 
0051 /* bit definitions of RTC miscellaneous Register1 */
0052 #define RTC_SOC_5LSB        (0x1F << 3)
0053 
0054 /* bit definitions of RTC Register1 */
0055 #define RTC_SOC_3MSB        (0x7)
0056 
0057 /* bit definitions of Power up Log register */
0058 #define BAT_WU_LOG          (1<<6)
0059 
0060 /* coulomb counter index */
0061 #define CCNT_POS1           0
0062 #define CCNT_POS2           1
0063 #define CCNT_NEG1           2
0064 #define CCNT_NEG2           3
0065 #define CCNT_SPOS           4
0066 #define CCNT_SNEG           5
0067 
0068 /* OCV -- Open Circuit Voltage */
0069 #define OCV_MODE_ACTIVE         0
0070 #define OCV_MODE_SLEEP          1
0071 
0072 /* Vbat range of CC for measuring Rbat */
0073 #define LOW_BAT_THRESHOLD       3600
0074 #define VBATT_RESISTOR_MIN      3800
0075 #define VBATT_RESISTOR_MAX      4100
0076 
0077 /* TBAT for batt, TINT for chip itself */
0078 #define PM860X_TEMP_TINT        (0)
0079 #define PM860X_TEMP_TBAT        (1)
0080 
0081 /*
0082  * Battery temperature based on NTC resistor, defined
0083  * corresponding resistor value  -- Ohm / C degeree.
0084  */
0085 #define TBAT_NEG_25D        127773  /* -25 */
0086 #define TBAT_NEG_10D        54564   /* -10 */
0087 #define TBAT_0D         32330   /* 0 */
0088 #define TBAT_10D        19785   /* 10 */
0089 #define TBAT_20D        12468   /* 20 */
0090 #define TBAT_30D        8072    /* 30 */
0091 #define TBAT_40D        5356    /* 40 */
0092 
0093 struct pm860x_battery_info {
0094     struct pm860x_chip *chip;
0095     struct i2c_client *i2c;
0096     struct device *dev;
0097 
0098     struct power_supply *battery;
0099     struct mutex lock;
0100     int status;
0101     int irq_cc;
0102     int irq_batt;
0103     int max_capacity;
0104     int resistor;       /* Battery Internal Resistor */
0105     int last_capacity;
0106     int start_soc;
0107     unsigned present:1;
0108     unsigned temp_type:1;   /* TINT or TBAT */
0109 };
0110 
0111 struct ccnt {
0112     unsigned long long pos;
0113     unsigned long long neg;
0114     unsigned int spos;
0115     unsigned int sneg;
0116 
0117     int total_chg;      /* mAh(3.6C) */
0118     int total_dischg;   /* mAh(3.6C) */
0119 };
0120 
0121 /*
0122  * State of Charge.
0123  * The first number is mAh(=3.6C), and the second number is percent point.
0124  */
0125 static int array_soc[][2] = {
0126     {4170, 100}, {4154, 99}, {4136, 98}, {4122, 97}, {4107, 96},
0127     {4102, 95}, {4088, 94}, {4081, 93}, {4070, 92}, {4060, 91},
0128     {4053, 90}, {4044, 89}, {4035, 88}, {4028, 87}, {4019, 86},
0129     {4013, 85}, {4006, 84}, {3995, 83}, {3987, 82}, {3982, 81},
0130     {3976, 80}, {3968, 79}, {3962, 78}, {3954, 77}, {3946, 76},
0131     {3941, 75}, {3934, 74}, {3929, 73}, {3922, 72}, {3916, 71},
0132     {3910, 70}, {3904, 69}, {3898, 68}, {3892, 67}, {3887, 66},
0133     {3880, 65}, {3874, 64}, {3868, 63}, {3862, 62}, {3854, 61},
0134     {3849, 60}, {3843, 59}, {3840, 58}, {3833, 57}, {3829, 56},
0135     {3824, 55}, {3818, 54}, {3815, 53}, {3810, 52}, {3808, 51},
0136     {3804, 50}, {3801, 49}, {3798, 48}, {3796, 47}, {3792, 46},
0137     {3789, 45}, {3785, 44}, {3784, 43}, {3782, 42}, {3780, 41},
0138     {3777, 40}, {3776, 39}, {3774, 38}, {3772, 37}, {3771, 36},
0139     {3769, 35}, {3768, 34}, {3764, 33}, {3763, 32}, {3760, 31},
0140     {3760, 30}, {3754, 29}, {3750, 28}, {3749, 27}, {3744, 26},
0141     {3740, 25}, {3734, 24}, {3732, 23}, {3728, 22}, {3726, 21},
0142     {3720, 20}, {3716, 19}, {3709, 18}, {3703, 17}, {3698, 16},
0143     {3692, 15}, {3683, 14}, {3675, 13}, {3670, 12}, {3665, 11},
0144     {3661, 10}, {3649, 9}, {3637, 8}, {3622, 7}, {3609, 6},
0145     {3580, 5}, {3558, 4}, {3540, 3}, {3510, 2}, {3429, 1},
0146 };
0147 
0148 static struct ccnt ccnt_data;
0149 
0150 /*
0151  * register 1 bit[7:0] -- bit[11:4] of measured value of voltage
0152  * register 0 bit[3:0] -- bit[3:0] of measured value of voltage
0153  */
0154 static int measure_12bit_voltage(struct pm860x_battery_info *info,
0155                  int offset, int *data)
0156 {
0157     unsigned char buf[2];
0158     int ret;
0159 
0160     ret = pm860x_bulk_read(info->i2c, offset, 2, buf);
0161     if (ret < 0)
0162         return ret;
0163 
0164     *data = ((buf[0] & 0xff) << 4) | (buf[1] & 0x0f);
0165     /* V_MEAS(mV) = data * 1.8 * 1000 / (2^12) */
0166     *data = ((*data & 0xfff) * 9 * 25) >> 9;
0167     return 0;
0168 }
0169 
0170 static int measure_vbatt(struct pm860x_battery_info *info, int state,
0171              int *data)
0172 {
0173     unsigned char buf[5];
0174     int ret;
0175 
0176     switch (state) {
0177     case OCV_MODE_ACTIVE:
0178         ret = measure_12bit_voltage(info, PM8607_VBAT_MEAS1, data);
0179         if (ret)
0180             return ret;
0181         /* V_BATT_MEAS(mV) = value * 3 * 1.8 * 1000 / (2^12) */
0182         *data *= 3;
0183         break;
0184     case OCV_MODE_SLEEP:
0185         /*
0186          * voltage value of VBATT in sleep mode is saved in different
0187          * registers.
0188          * bit[11:10] -- bit[7:6] of LDO9(0x18)
0189          * bit[9:8] -- bit[7:6] of LDO8(0x17)
0190          * bit[7:6] -- bit[7:6] of LDO7(0x16)
0191          * bit[5:4] -- bit[7:6] of LDO6(0x15)
0192          * bit[3:0] -- bit[7:4] of LDO5(0x14)
0193          */
0194         ret = pm860x_bulk_read(info->i2c, PM8607_LDO5, 5, buf);
0195         if (ret < 0)
0196             return ret;
0197         ret = ((buf[4] >> 6) << 10) | ((buf[3] >> 6) << 8)
0198             | ((buf[2] >> 6) << 6) | ((buf[1] >> 6) << 4)
0199             | (buf[0] >> 4);
0200         /* V_BATT_MEAS(mV) = data * 3 * 1.8 * 1000 / (2^12) */
0201         *data = ((*data & 0xff) * 27 * 25) >> 9;
0202         break;
0203     default:
0204         return -EINVAL;
0205     }
0206     return 0;
0207 }
0208 
0209 /*
0210  * Return value is signed data.
0211  * Negative value means discharging, and positive value means charging.
0212  */
0213 static int measure_current(struct pm860x_battery_info *info, int *data)
0214 {
0215     unsigned char buf[2];
0216     short s;
0217     int ret;
0218 
0219     ret = pm860x_bulk_read(info->i2c, PM8607_IBAT_MEAS1, 2, buf);
0220     if (ret < 0)
0221         return ret;
0222 
0223     s = ((buf[0] & 0xff) << 8) | (buf[1] & 0xff);
0224     /* current(mA) = value * 0.125 */
0225     *data = s >> 3;
0226     return 0;
0227 }
0228 
0229 static int set_charger_current(struct pm860x_battery_info *info, int data,
0230                    int *old)
0231 {
0232     int ret;
0233 
0234     if (data < 50 || data > 1600 || !old)
0235         return -EINVAL;
0236 
0237     data = ((data - 50) / 50) & 0x1f;
0238     *old = pm860x_reg_read(info->i2c, PM8607_CHG_CTRL2);
0239     *old = (*old & 0x1f) * 50 + 50;
0240     ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL2, 0x1f, data);
0241     if (ret < 0)
0242         return ret;
0243     return 0;
0244 }
0245 
0246 static int read_ccnt(struct pm860x_battery_info *info, int offset,
0247              int *ccnt)
0248 {
0249     unsigned char buf[2];
0250     int ret;
0251 
0252     ret = pm860x_set_bits(info->i2c, PM8607_CCNT, 7, offset & 7);
0253     if (ret < 0)
0254         goto out;
0255     ret = pm860x_bulk_read(info->i2c, PM8607_CCNT_MEAS1, 2, buf);
0256     if (ret < 0)
0257         goto out;
0258     *ccnt = ((buf[0] & 0xff) << 8) | (buf[1] & 0xff);
0259     return 0;
0260 out:
0261     return ret;
0262 }
0263 
0264 static int calc_ccnt(struct pm860x_battery_info *info, struct ccnt *ccnt)
0265 {
0266     unsigned int sum;
0267     int ret;
0268     int data;
0269 
0270     ret = read_ccnt(info, CCNT_POS1, &data);
0271     if (ret)
0272         goto out;
0273     sum = data & 0xffff;
0274     ret = read_ccnt(info, CCNT_POS2, &data);
0275     if (ret)
0276         goto out;
0277     sum |= (data & 0xffff) << 16;
0278     ccnt->pos += sum;
0279 
0280     ret = read_ccnt(info, CCNT_NEG1, &data);
0281     if (ret)
0282         goto out;
0283     sum = data & 0xffff;
0284     ret = read_ccnt(info, CCNT_NEG2, &data);
0285     if (ret)
0286         goto out;
0287     sum |= (data & 0xffff) << 16;
0288     sum = ~sum + 1;     /* since it's negative */
0289     ccnt->neg += sum;
0290 
0291     ret = read_ccnt(info, CCNT_SPOS, &data);
0292     if (ret)
0293         goto out;
0294     ccnt->spos += data;
0295     ret = read_ccnt(info, CCNT_SNEG, &data);
0296     if (ret)
0297         goto out;
0298 
0299     /*
0300      * charge(mAh)  = count * 1.6984 * 1e(-8)
0301      *              = count * 16984 * 1.024 * 1.024 * 1.024 / (2 ^ 40)
0302      *              = count * 18236 / (2 ^ 40)
0303      */
0304     ccnt->total_chg = (int) ((ccnt->pos * 18236) >> 40);
0305     ccnt->total_dischg = (int) ((ccnt->neg * 18236) >> 40);
0306     return 0;
0307 out:
0308     return ret;
0309 }
0310 
0311 static int clear_ccnt(struct pm860x_battery_info *info, struct ccnt *ccnt)
0312 {
0313     int data;
0314 
0315     memset(ccnt, 0, sizeof(*ccnt));
0316     /* read to clear ccnt */
0317     read_ccnt(info, CCNT_POS1, &data);
0318     read_ccnt(info, CCNT_POS2, &data);
0319     read_ccnt(info, CCNT_NEG1, &data);
0320     read_ccnt(info, CCNT_NEG2, &data);
0321     read_ccnt(info, CCNT_SPOS, &data);
0322     read_ccnt(info, CCNT_SNEG, &data);
0323     return 0;
0324 }
0325 
0326 /* Calculate Open Circuit Voltage */
0327 static int calc_ocv(struct pm860x_battery_info *info, int *ocv)
0328 {
0329     int ret;
0330     int i;
0331     int data;
0332     int vbatt_avg;
0333     int vbatt_sum;
0334     int ibatt_avg;
0335     int ibatt_sum;
0336 
0337     if (!ocv)
0338         return -EINVAL;
0339 
0340     for (i = 0, ibatt_sum = 0, vbatt_sum = 0; i < 10; i++) {
0341         ret = measure_vbatt(info, OCV_MODE_ACTIVE, &data);
0342         if (ret)
0343             goto out;
0344         vbatt_sum += data;
0345         ret = measure_current(info, &data);
0346         if (ret)
0347             goto out;
0348         ibatt_sum += data;
0349     }
0350     vbatt_avg = vbatt_sum / 10;
0351     ibatt_avg = ibatt_sum / 10;
0352 
0353     mutex_lock(&info->lock);
0354     if (info->present)
0355         *ocv = vbatt_avg - ibatt_avg * info->resistor / 1000;
0356     else
0357         *ocv = vbatt_avg;
0358     mutex_unlock(&info->lock);
0359     dev_dbg(info->dev, "VBAT average:%d, OCV:%d\n", vbatt_avg, *ocv);
0360     return 0;
0361 out:
0362     return ret;
0363 }
0364 
0365 /* Calculate State of Charge (percent points) */
0366 static int calc_soc(struct pm860x_battery_info *info, int state, int *soc)
0367 {
0368     int i;
0369     int ocv;
0370     int count;
0371     int ret = -EINVAL;
0372 
0373     if (!soc)
0374         return -EINVAL;
0375 
0376     switch (state) {
0377     case OCV_MODE_ACTIVE:
0378         ret = calc_ocv(info, &ocv);
0379         break;
0380     case OCV_MODE_SLEEP:
0381         ret = measure_vbatt(info, OCV_MODE_SLEEP, &ocv);
0382         break;
0383     }
0384     if (ret)
0385         return ret;
0386 
0387     count = ARRAY_SIZE(array_soc);
0388     if (ocv < array_soc[count - 1][0]) {
0389         *soc = 0;
0390         return 0;
0391     }
0392 
0393     for (i = 0; i < count; i++) {
0394         if (ocv >= array_soc[i][0]) {
0395             *soc = array_soc[i][1];
0396             break;
0397         }
0398     }
0399     return 0;
0400 }
0401 
0402 static irqreturn_t pm860x_coulomb_handler(int irq, void *data)
0403 {
0404     struct pm860x_battery_info *info = data;
0405 
0406     calc_ccnt(info, &ccnt_data);
0407     return IRQ_HANDLED;
0408 }
0409 
0410 static irqreturn_t pm860x_batt_handler(int irq, void *data)
0411 {
0412     struct pm860x_battery_info *info = data;
0413     int ret;
0414 
0415     mutex_lock(&info->lock);
0416     ret = pm860x_reg_read(info->i2c, PM8607_STATUS_2);
0417     if (ret & STATUS2_BAT) {
0418         info->present = 1;
0419         info->temp_type = PM860X_TEMP_TBAT;
0420     } else {
0421         info->present = 0;
0422         info->temp_type = PM860X_TEMP_TINT;
0423     }
0424     mutex_unlock(&info->lock);
0425     /* clear ccnt since battery is attached or dettached */
0426     clear_ccnt(info, &ccnt_data);
0427     return IRQ_HANDLED;
0428 }
0429 
0430 static void pm860x_init_battery(struct pm860x_battery_info *info)
0431 {
0432     unsigned char buf[2];
0433     int ret;
0434     int data;
0435     int bat_remove;
0436     int soc = 0;
0437 
0438     /* measure enable on GPADC1 */
0439     data = MEAS1_GP1;
0440     if (info->temp_type == PM860X_TEMP_TINT)
0441         data |= MEAS1_TINT;
0442     ret = pm860x_set_bits(info->i2c, PM8607_MEAS_EN1, data, data);
0443     if (ret)
0444         goto out;
0445 
0446     /* measure enable on IBAT, BAT_DET, CC. IBAT is depend on CC. */
0447     data = MEAS3_IBAT | MEAS3_BAT_DET | MEAS3_CC;
0448     ret = pm860x_set_bits(info->i2c, PM8607_MEAS_EN3, data, data);
0449     if (ret)
0450         goto out;
0451 
0452     /* measure disable CC in sleep time  */
0453     ret = pm860x_reg_write(info->i2c, PM8607_MEAS_OFF_TIME1, 0x82);
0454     if (ret)
0455         goto out;
0456     ret = pm860x_reg_write(info->i2c, PM8607_MEAS_OFF_TIME2, 0x6c);
0457     if (ret)
0458         goto out;
0459 
0460     /* enable GPADC */
0461     ret = pm860x_set_bits(info->i2c, PM8607_GPADC_MISC1,
0462                 GPMISC1_GPADC_EN, GPMISC1_GPADC_EN);
0463     if (ret < 0)
0464         goto out;
0465 
0466     /* detect battery via GPADC1 */
0467     ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL6,
0468                 CC6_BAT_DET_GPADC1, CC6_BAT_DET_GPADC1);
0469     if (ret < 0)
0470         goto out;
0471 
0472     ret = pm860x_set_bits(info->i2c, PM8607_CCNT, 7 << 3,
0473                   CCNT_AVG_SEL);
0474     if (ret < 0)
0475         goto out;
0476 
0477     /* set GPADC1 bias */
0478     ret = pm860x_set_bits(info->i2c, PM8607_GP_BIAS2, 0xF << 4,
0479                   GPBIAS2_GPADC1_SET);
0480     if (ret < 0)
0481         goto out;
0482 
0483     /* check whether battery present) */
0484     mutex_lock(&info->lock);
0485     ret = pm860x_reg_read(info->i2c, PM8607_STATUS_2);
0486     if (ret < 0) {
0487         mutex_unlock(&info->lock);
0488         goto out;
0489     }
0490     if (ret & STATUS2_BAT) {
0491         info->present = 1;
0492         info->temp_type = PM860X_TEMP_TBAT;
0493     } else {
0494         info->present = 0;
0495         info->temp_type = PM860X_TEMP_TINT;
0496     }
0497     mutex_unlock(&info->lock);
0498 
0499     ret = calc_soc(info, OCV_MODE_ACTIVE, &soc);
0500     if (ret < 0)
0501         goto out;
0502 
0503     data = pm860x_reg_read(info->i2c, PM8607_POWER_UP_LOG);
0504     bat_remove = data & BAT_WU_LOG;
0505 
0506     dev_dbg(info->dev, "battery wake up? %s\n",
0507         bat_remove != 0 ? "yes" : "no");
0508 
0509     /* restore SOC from RTC domain register */
0510     if (bat_remove == 0) {
0511         buf[0] = pm860x_reg_read(info->i2c, PM8607_RTC_MISC2);
0512         buf[1] = pm860x_reg_read(info->i2c, PM8607_RTC1);
0513         data = ((buf[1] & 0x3) << 5) | ((buf[0] >> 3) & 0x1F);
0514         if (data > soc + 15)
0515             info->start_soc = soc;
0516         else if (data < soc - 15)
0517             info->start_soc = soc;
0518         else
0519             info->start_soc = data;
0520         dev_dbg(info->dev, "soc_rtc %d, soc_ocv :%d\n", data, soc);
0521     } else {
0522         pm860x_set_bits(info->i2c, PM8607_POWER_UP_LOG,
0523                 BAT_WU_LOG, BAT_WU_LOG);
0524         info->start_soc = soc;
0525     }
0526     info->last_capacity = info->start_soc;
0527     dev_dbg(info->dev, "init soc : %d\n", info->last_capacity);
0528 out:
0529     return;
0530 }
0531 
0532 static void set_temp_threshold(struct pm860x_battery_info *info,
0533                    int min, int max)
0534 {
0535     int data;
0536 
0537     /* (tmp << 8) / 1800 */
0538     if (min <= 0)
0539         data = 0;
0540     else
0541         data = (min << 8) / 1800;
0542     pm860x_reg_write(info->i2c, PM8607_GPADC1_HIGHTH, data);
0543     dev_dbg(info->dev, "TEMP_HIGHTH : min: %d, 0x%x\n", min, data);
0544 
0545     if (max <= 0)
0546         data = 0xff;
0547     else
0548         data = (max << 8) / 1800;
0549     pm860x_reg_write(info->i2c, PM8607_GPADC1_LOWTH, data);
0550     dev_dbg(info->dev, "TEMP_LOWTH:max : %d, 0x%x\n", max, data);
0551 }
0552 
0553 static int measure_temp(struct pm860x_battery_info *info, int *data)
0554 {
0555     int ret;
0556     int temp;
0557     int min;
0558     int max;
0559 
0560     if (info->temp_type == PM860X_TEMP_TINT) {
0561         ret = measure_12bit_voltage(info, PM8607_TINT_MEAS1, data);
0562         if (ret)
0563             return ret;
0564         *data = (*data - 884) * 1000 / 3611;
0565     } else {
0566         ret = measure_12bit_voltage(info, PM8607_GPADC1_MEAS1, data);
0567         if (ret)
0568             return ret;
0569         /* meausered Vtbat(mV) / Ibias_current(11uA)*/
0570         *data = (*data * 1000) / GPBIAS2_GPADC1_UA;
0571 
0572         if (*data > TBAT_NEG_25D) {
0573             temp = -30; /* over cold , suppose -30 roughly */
0574             max = TBAT_NEG_10D * GPBIAS2_GPADC1_UA / 1000;
0575             set_temp_threshold(info, 0, max);
0576         } else if (*data > TBAT_NEG_10D) {
0577             temp = -15; /* -15 degree, code */
0578             max = TBAT_NEG_10D * GPBIAS2_GPADC1_UA / 1000;
0579             set_temp_threshold(info, 0, max);
0580         } else if (*data > TBAT_0D) {
0581             temp = -5;  /* -5 degree */
0582             min = TBAT_NEG_10D * GPBIAS2_GPADC1_UA / 1000;
0583             max = TBAT_40D * GPBIAS2_GPADC1_UA / 1000;
0584             set_temp_threshold(info, min, max);
0585         } else if (*data > TBAT_10D) {
0586             temp = 5;   /* in range of (0, 10) */
0587             min = TBAT_NEG_10D * GPBIAS2_GPADC1_UA / 1000;
0588             max = TBAT_40D * GPBIAS2_GPADC1_UA / 1000;
0589             set_temp_threshold(info, min, max);
0590         } else if (*data > TBAT_20D) {
0591             temp = 15;  /* in range of (10, 20) */
0592             min = TBAT_NEG_10D * GPBIAS2_GPADC1_UA / 1000;
0593             max = TBAT_40D * GPBIAS2_GPADC1_UA / 1000;
0594             set_temp_threshold(info, min, max);
0595         } else if (*data > TBAT_30D) {
0596             temp = 25;  /* in range of (20, 30) */
0597             min = TBAT_NEG_10D * GPBIAS2_GPADC1_UA / 1000;
0598             max = TBAT_40D * GPBIAS2_GPADC1_UA / 1000;
0599             set_temp_threshold(info, min, max);
0600         } else if (*data > TBAT_40D) {
0601             temp = 35;  /* in range of (30, 40) */
0602             min = TBAT_NEG_10D * GPBIAS2_GPADC1_UA / 1000;
0603             max = TBAT_40D * GPBIAS2_GPADC1_UA / 1000;
0604             set_temp_threshold(info, min, max);
0605         } else {
0606             min = TBAT_40D * GPBIAS2_GPADC1_UA / 1000;
0607             set_temp_threshold(info, min, 0);
0608             temp = 45;  /* over heat ,suppose 45 roughly */
0609         }
0610 
0611         dev_dbg(info->dev, "temp_C:%d C,temp_mv:%d mv\n", temp, *data);
0612         *data = temp;
0613     }
0614     return 0;
0615 }
0616 
0617 static int calc_resistor(struct pm860x_battery_info *info)
0618 {
0619     int vbatt_sum1;
0620     int vbatt_sum2;
0621     int chg_current;
0622     int ibatt_sum1;
0623     int ibatt_sum2;
0624     int data;
0625     int ret;
0626     int i;
0627 
0628     ret = measure_current(info, &data);
0629     /* make sure that charging is launched by data > 0 */
0630     if (ret || data < 0)
0631         goto out;
0632 
0633     ret = measure_vbatt(info, OCV_MODE_ACTIVE, &data);
0634     if (ret)
0635         goto out;
0636     /* calculate resistor only in CC charge mode */
0637     if (data < VBATT_RESISTOR_MIN || data > VBATT_RESISTOR_MAX)
0638         goto out;
0639 
0640     /* current is saved */
0641     if (set_charger_current(info, 500, &chg_current))
0642         goto out;
0643 
0644     /*
0645      * set charge current as 500mA, wait about 500ms till charging
0646      * process is launched and stable with the newer charging current.
0647      */
0648     msleep(500);
0649 
0650     for (i = 0, vbatt_sum1 = 0, ibatt_sum1 = 0; i < 10; i++) {
0651         ret = measure_vbatt(info, OCV_MODE_ACTIVE, &data);
0652         if (ret)
0653             goto out_meas;
0654         vbatt_sum1 += data;
0655         ret = measure_current(info, &data);
0656         if (ret)
0657             goto out_meas;
0658 
0659         if (data < 0)
0660             ibatt_sum1 = ibatt_sum1 - data; /* discharging */
0661         else
0662             ibatt_sum1 = ibatt_sum1 + data; /* charging */
0663     }
0664 
0665     if (set_charger_current(info, 100, &ret))
0666         goto out_meas;
0667     /*
0668      * set charge current as 100mA, wait about 500ms till charging
0669      * process is launched and stable with the newer charging current.
0670      */
0671     msleep(500);
0672 
0673     for (i = 0, vbatt_sum2 = 0, ibatt_sum2 = 0; i < 10; i++) {
0674         ret = measure_vbatt(info, OCV_MODE_ACTIVE, &data);
0675         if (ret)
0676             goto out_meas;
0677         vbatt_sum2 += data;
0678         ret = measure_current(info, &data);
0679         if (ret)
0680             goto out_meas;
0681 
0682         if (data < 0)
0683             ibatt_sum2 = ibatt_sum2 - data; /* discharging */
0684         else
0685             ibatt_sum2 = ibatt_sum2 + data; /* charging */
0686     }
0687 
0688     /* restore current setting */
0689     if (set_charger_current(info, chg_current, &ret))
0690         goto out_meas;
0691 
0692     if ((vbatt_sum1 > vbatt_sum2) && (ibatt_sum1 > ibatt_sum2) &&
0693             (ibatt_sum2 > 0)) {
0694         /* calculate resistor in discharging case */
0695         data = 1000 * (vbatt_sum1 - vbatt_sum2)
0696             / (ibatt_sum1 - ibatt_sum2);
0697         if ((data - info->resistor > 0) &&
0698                 (data - info->resistor < info->resistor))
0699             info->resistor = data;
0700         if ((info->resistor - data > 0) &&
0701                 (info->resistor - data < data))
0702             info->resistor = data;
0703     }
0704     return 0;
0705 
0706 out_meas:
0707     set_charger_current(info, chg_current, &ret);
0708 out:
0709     return -EINVAL;
0710 }
0711 
0712 static int calc_capacity(struct pm860x_battery_info *info, int *cap)
0713 {
0714     int ret;
0715     int data;
0716     int ibat;
0717     int cap_ocv = 0;
0718     int cap_cc = 0;
0719 
0720     ret = calc_ccnt(info, &ccnt_data);
0721     if (ret)
0722         goto out;
0723 soc:
0724     data = info->max_capacity * info->start_soc / 100;
0725     if (ccnt_data.total_dischg - ccnt_data.total_chg <= data) {
0726         cap_cc =
0727             data + ccnt_data.total_chg - ccnt_data.total_dischg;
0728     } else {
0729         clear_ccnt(info, &ccnt_data);
0730         calc_soc(info, OCV_MODE_ACTIVE, &info->start_soc);
0731         dev_dbg(info->dev, "restart soc = %d !\n",
0732             info->start_soc);
0733         goto soc;
0734     }
0735 
0736     cap_cc = cap_cc * 100 / info->max_capacity;
0737     if (cap_cc < 0)
0738         cap_cc = 0;
0739     else if (cap_cc > 100)
0740         cap_cc = 100;
0741 
0742     dev_dbg(info->dev, "%s, last cap : %d", __func__,
0743         info->last_capacity);
0744 
0745     ret = measure_current(info, &ibat);
0746     if (ret)
0747         goto out;
0748     /* Calculate the capacity when discharging(ibat < 0) */
0749     if (ibat < 0) {
0750         ret = calc_soc(info, OCV_MODE_ACTIVE, &cap_ocv);
0751         if (ret)
0752             cap_ocv = info->last_capacity;
0753         ret = measure_vbatt(info, OCV_MODE_ACTIVE, &data);
0754         if (ret)
0755             goto out;
0756         if (data <= LOW_BAT_THRESHOLD) {
0757             /* choose the lower capacity value to report
0758              * between vbat and CC when vbat < 3.6v;
0759              * than 3.6v;
0760              */
0761             *cap = min(cap_ocv, cap_cc);
0762         } else {
0763             /* when detect vbat > 3.6v, but cap_cc < 15,and
0764              * cap_ocv is 10% larger than cap_cc, we can think
0765              * CC have some accumulation error, switch to OCV
0766              * to estimate capacity;
0767              * */
0768             if (cap_cc < 15 && cap_ocv - cap_cc > 10)
0769                 *cap = cap_ocv;
0770             else
0771                 *cap = cap_cc;
0772         }
0773         /* when discharging, make sure current capacity
0774          * is lower than last*/
0775         if (*cap > info->last_capacity)
0776             *cap = info->last_capacity;
0777     } else {
0778         *cap = cap_cc;
0779     }
0780     info->last_capacity = *cap;
0781 
0782     dev_dbg(info->dev, "%s, cap_ocv:%d cap_cc:%d, cap:%d\n",
0783         (ibat < 0) ? "discharging" : "charging",
0784          cap_ocv, cap_cc, *cap);
0785     /*
0786      * store the current capacity to RTC domain register,
0787      * after next power up , it will be restored.
0788      */
0789     pm860x_set_bits(info->i2c, PM8607_RTC_MISC2, RTC_SOC_5LSB,
0790             (*cap & 0x1F) << 3);
0791     pm860x_set_bits(info->i2c, PM8607_RTC1, RTC_SOC_3MSB,
0792             ((*cap >> 5) & 0x3));
0793     return 0;
0794 out:
0795     return ret;
0796 }
0797 
0798 static void pm860x_external_power_changed(struct power_supply *psy)
0799 {
0800     struct pm860x_battery_info *info = dev_get_drvdata(psy->dev.parent);
0801 
0802     calc_resistor(info);
0803 }
0804 
0805 static int pm860x_batt_get_prop(struct power_supply *psy,
0806                 enum power_supply_property psp,
0807                 union power_supply_propval *val)
0808 {
0809     struct pm860x_battery_info *info = dev_get_drvdata(psy->dev.parent);
0810     int data;
0811     int ret;
0812 
0813     switch (psp) {
0814     case POWER_SUPPLY_PROP_PRESENT:
0815         val->intval = info->present;
0816         break;
0817     case POWER_SUPPLY_PROP_CAPACITY:
0818         ret = calc_capacity(info, &data);
0819         if (ret)
0820             return ret;
0821         if (data < 0)
0822             data = 0;
0823         else if (data > 100)
0824             data = 100;
0825         /* return 100 if battery is not attached */
0826         if (!info->present)
0827             data = 100;
0828         val->intval = data;
0829         break;
0830     case POWER_SUPPLY_PROP_TECHNOLOGY:
0831         val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
0832         break;
0833     case POWER_SUPPLY_PROP_VOLTAGE_NOW:
0834         /* return real vbatt Voltage */
0835         ret = measure_vbatt(info, OCV_MODE_ACTIVE, &data);
0836         if (ret)
0837             return ret;
0838         val->intval = data * 1000;
0839         break;
0840     case POWER_SUPPLY_PROP_VOLTAGE_AVG:
0841         /* return Open Circuit Voltage (not measured voltage) */
0842         ret = calc_ocv(info, &data);
0843         if (ret)
0844             return ret;
0845         val->intval = data * 1000;
0846         break;
0847     case POWER_SUPPLY_PROP_CURRENT_NOW:
0848         ret = measure_current(info, &data);
0849         if (ret)
0850             return ret;
0851         val->intval = data;
0852         break;
0853     case POWER_SUPPLY_PROP_TEMP:
0854         if (info->present) {
0855             ret = measure_temp(info, &data);
0856             if (ret)
0857                 return ret;
0858             data *= 10;
0859         } else {
0860             /* Fake Temp 25C Without Battery */
0861             data = 250;
0862         }
0863         val->intval = data;
0864         break;
0865     default:
0866         return -ENODEV;
0867     }
0868     return 0;
0869 }
0870 
0871 static int pm860x_batt_set_prop(struct power_supply *psy,
0872                        enum power_supply_property psp,
0873                        const union power_supply_propval *val)
0874 {
0875     struct pm860x_battery_info *info = dev_get_drvdata(psy->dev.parent);
0876 
0877     switch (psp) {
0878     case POWER_SUPPLY_PROP_CHARGE_FULL:
0879         clear_ccnt(info, &ccnt_data);
0880         info->start_soc = 100;
0881         dev_dbg(info->dev, "chg done, update soc = %d\n",
0882             info->start_soc);
0883         break;
0884     default:
0885         return -EPERM;
0886     }
0887 
0888     return 0;
0889 }
0890 
0891 
0892 static enum power_supply_property pm860x_batt_props[] = {
0893     POWER_SUPPLY_PROP_PRESENT,
0894     POWER_SUPPLY_PROP_CAPACITY,
0895     POWER_SUPPLY_PROP_TECHNOLOGY,
0896     POWER_SUPPLY_PROP_VOLTAGE_NOW,
0897     POWER_SUPPLY_PROP_VOLTAGE_AVG,
0898     POWER_SUPPLY_PROP_CURRENT_NOW,
0899     POWER_SUPPLY_PROP_TEMP,
0900 };
0901 
0902 static const struct power_supply_desc pm860x_battery_desc = {
0903     .name           = "battery-monitor",
0904     .type           = POWER_SUPPLY_TYPE_BATTERY,
0905     .properties     = pm860x_batt_props,
0906     .num_properties     = ARRAY_SIZE(pm860x_batt_props),
0907     .get_property       = pm860x_batt_get_prop,
0908     .set_property       = pm860x_batt_set_prop,
0909     .external_power_changed = pm860x_external_power_changed,
0910 };
0911 
0912 static int pm860x_battery_probe(struct platform_device *pdev)
0913 {
0914     struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
0915     struct pm860x_battery_info *info;
0916     struct pm860x_power_pdata *pdata;
0917     int ret;
0918 
0919     info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
0920     if (!info)
0921         return -ENOMEM;
0922 
0923     info->irq_cc = platform_get_irq(pdev, 0);
0924     if (info->irq_cc <= 0)
0925         return -EINVAL;
0926 
0927     info->irq_batt = platform_get_irq(pdev, 1);
0928     if (info->irq_batt <= 0)
0929         return -EINVAL;
0930 
0931     info->chip = chip;
0932     info->i2c =
0933         (chip->id == CHIP_PM8607) ? chip->client : chip->companion;
0934     info->dev = &pdev->dev;
0935     info->status = POWER_SUPPLY_STATUS_UNKNOWN;
0936     pdata = pdev->dev.platform_data;
0937 
0938     mutex_init(&info->lock);
0939     platform_set_drvdata(pdev, info);
0940 
0941     pm860x_init_battery(info);
0942 
0943     if (pdata && pdata->max_capacity)
0944         info->max_capacity = pdata->max_capacity;
0945     else
0946         info->max_capacity = 1500;  /* set default capacity */
0947     if (pdata && pdata->resistor)
0948         info->resistor = pdata->resistor;
0949     else
0950         info->resistor = 300;   /* set default internal resistor */
0951 
0952     info->battery = devm_power_supply_register(&pdev->dev,
0953                            &pm860x_battery_desc,
0954                            NULL);
0955     if (IS_ERR(info->battery))
0956         return PTR_ERR(info->battery);
0957     info->battery->dev.parent = &pdev->dev;
0958 
0959     ret = devm_request_threaded_irq(chip->dev, info->irq_cc, NULL,
0960                     pm860x_coulomb_handler, IRQF_ONESHOT,
0961                     "coulomb", info);
0962     if (ret < 0) {
0963         dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n",
0964             info->irq_cc, ret);
0965         return ret;
0966     }
0967 
0968     ret = devm_request_threaded_irq(chip->dev, info->irq_batt, NULL,
0969                     pm860x_batt_handler,
0970                     IRQF_ONESHOT, "battery", info);
0971     if (ret < 0) {
0972         dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n",
0973             info->irq_batt, ret);
0974         return ret;
0975     }
0976 
0977 
0978     return 0;
0979 }
0980 
0981 #ifdef CONFIG_PM_SLEEP
0982 static int pm860x_battery_suspend(struct device *dev)
0983 {
0984     struct platform_device *pdev = to_platform_device(dev);
0985     struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
0986 
0987     if (device_may_wakeup(dev))
0988         chip->wakeup_flag |= 1 << PM8607_IRQ_CC;
0989     return 0;
0990 }
0991 
0992 static int pm860x_battery_resume(struct device *dev)
0993 {
0994     struct platform_device *pdev = to_platform_device(dev);
0995     struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
0996 
0997     if (device_may_wakeup(dev))
0998         chip->wakeup_flag &= ~(1 << PM8607_IRQ_CC);
0999     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");