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/power_supply.h>
0015 #include <linux/mfd/88pm860x.h>
0016 #include <linux/delay.h>
0017 #include <linux/uaccess.h>
0018 #include <asm/div64.h>
0019 
0020 /* bit definitions of Status Query Interface 2 */
0021 #define STATUS2_CHG     (1 << 2)
0022 
0023 /* bit definitions of Reset Out Register */
0024 #define RESET_SW_PD     (1 << 7)
0025 
0026 /* bit definitions of PreReg 1 */
0027 #define PREREG1_90MA        (0x0)
0028 #define PREREG1_180MA       (0x1)
0029 #define PREREG1_450MA       (0x4)
0030 #define PREREG1_540MA       (0x5)
0031 #define PREREG1_1350MA      (0xE)
0032 #define PREREG1_VSYS_4_5V   (3 << 4)
0033 
0034 /* bit definitions of Charger Control 1 Register */
0035 #define CC1_MODE_OFF        (0)
0036 #define CC1_MODE_PRECHARGE  (1)
0037 #define CC1_MODE_FASTCHARGE (2)
0038 #define CC1_MODE_PULSECHARGE    (3)
0039 #define CC1_ITERM_20MA      (0 << 2)
0040 #define CC1_ITERM_60MA      (2 << 2)
0041 #define CC1_VFCHG_4_2V      (9 << 4)
0042 
0043 /* bit definitions of Charger Control 2 Register */
0044 #define CC2_ICHG_100MA      (0x1)
0045 #define CC2_ICHG_500MA      (0x9)
0046 #define CC2_ICHG_1000MA     (0x13)
0047 
0048 /* bit definitions of Charger Control 3 Register */
0049 #define CC3_180MIN_TIMEOUT  (0x6 << 4)
0050 #define CC3_270MIN_TIMEOUT  (0x7 << 4)
0051 #define CC3_360MIN_TIMEOUT  (0xA << 4)
0052 #define CC3_DISABLE_TIMEOUT (0xF << 4)
0053 
0054 /* bit definitions of Charger Control 4 Register */
0055 #define CC4_IPRE_40MA       (7)
0056 #define CC4_VPCHG_3_2V      (3 << 4)
0057 #define CC4_IFCHG_MON_EN    (1 << 6)
0058 #define CC4_BTEMP_MON_EN    (1 << 7)
0059 
0060 /* bit definitions of Charger Control 6 Register */
0061 #define CC6_BAT_OV_EN       (1 << 2)
0062 #define CC6_BAT_UV_EN       (1 << 3)
0063 #define CC6_UV_VBAT_SET     (0x3 << 6)  /* 2.8v */
0064 
0065 /* bit definitions of Charger Control 7 Register */
0066 #define CC7_BAT_REM_EN      (1 << 3)
0067 #define CC7_IFSM_EN     (1 << 7)
0068 
0069 /* bit definitions of Measurement Enable 1 Register */
0070 #define MEAS1_VBAT      (1 << 0)
0071 
0072 /* bit definitions of Measurement Enable 3 Register */
0073 #define MEAS3_IBAT_EN       (1 << 0)
0074 #define MEAS3_CC_EN     (1 << 2)
0075 
0076 #define FSM_INIT        0
0077 #define FSM_DISCHARGE       1
0078 #define FSM_PRECHARGE       2
0079 #define FSM_FASTCHARGE      3
0080 
0081 #define PRECHARGE_THRESHOLD 3100
0082 #define POWEROFF_THRESHOLD  3400
0083 #define CHARGE_THRESHOLD    4000
0084 #define DISCHARGE_THRESHOLD 4180
0085 
0086 /* over-temperature on PM8606 setting */
0087 #define OVER_TEMP_FLAG      (1 << 6)
0088 #define OVTEMP_AUTORECOVER  (1 << 3)
0089 
0090 /* over-voltage protect on vchg setting mv */
0091 #define VCHG_NORMAL_LOW     4200
0092 #define VCHG_NORMAL_CHECK   5800
0093 #define VCHG_NORMAL_HIGH    6000
0094 #define VCHG_OVP_LOW        5500
0095 
0096 struct pm860x_charger_info {
0097     struct pm860x_chip *chip;
0098     struct i2c_client *i2c;
0099     struct i2c_client *i2c_8606;
0100     struct device *dev;
0101 
0102     struct power_supply *usb;
0103     struct mutex lock;
0104     int irq_nums;
0105     int irq[7];
0106     unsigned state:3;   /* fsm state */
0107     unsigned online:1;  /* usb charger */
0108     unsigned present:1; /* battery present */
0109     unsigned allowed:1;
0110 };
0111 
0112 static char *pm860x_supplied_to[] = {
0113     "battery-monitor",
0114 };
0115 
0116 static int measure_vchg(struct pm860x_charger_info *info, int *data)
0117 {
0118     unsigned char buf[2];
0119     int ret = 0;
0120 
0121     ret = pm860x_bulk_read(info->i2c, PM8607_VCHG_MEAS1, 2, buf);
0122     if (ret < 0)
0123         return ret;
0124 
0125     *data = ((buf[0] & 0xff) << 4) | (buf[1] & 0x0f);
0126     /* V_BATT_MEAS(mV) = value * 5 * 1.8 * 1000 / (2^12) */
0127     *data = ((*data & 0xfff) * 9 * 125) >> 9;
0128 
0129     dev_dbg(info->dev, "%s, vchg: %d mv\n", __func__, *data);
0130 
0131     return ret;
0132 }
0133 
0134 static void set_vchg_threshold(struct pm860x_charger_info *info,
0135                    int min, int max)
0136 {
0137     int data;
0138 
0139     /* (tmp << 8) * / 5 / 1800 */
0140     if (min <= 0)
0141         data = 0;
0142     else
0143         data = (min << 5) / 1125;
0144     pm860x_reg_write(info->i2c, PM8607_VCHG_LOWTH, data);
0145     dev_dbg(info->dev, "VCHG_LOWTH:%dmv, 0x%x\n", min, data);
0146 
0147     if (max <= 0)
0148         data = 0xff;
0149     else
0150         data = (max << 5) / 1125;
0151     pm860x_reg_write(info->i2c, PM8607_VCHG_HIGHTH, data);
0152     dev_dbg(info->dev, "VCHG_HIGHTH:%dmv, 0x%x\n", max, data);
0153 
0154 }
0155 
0156 static void set_vbatt_threshold(struct pm860x_charger_info *info,
0157                 int min, int max)
0158 {
0159     int data;
0160 
0161     /* (tmp << 8) * 3 / 1800 */
0162     if (min <= 0)
0163         data = 0;
0164     else
0165         data = (min << 5) / 675;
0166     pm860x_reg_write(info->i2c, PM8607_VBAT_LOWTH, data);
0167     dev_dbg(info->dev, "VBAT Min:%dmv, LOWTH:0x%x\n", min, data);
0168 
0169     if (max <= 0)
0170         data = 0xff;
0171     else
0172         data = (max << 5) / 675;
0173     pm860x_reg_write(info->i2c, PM8607_VBAT_HIGHTH, data);
0174     dev_dbg(info->dev, "VBAT Max:%dmv, HIGHTH:0x%x\n", max, data);
0175 
0176     return;
0177 }
0178 
0179 static int start_precharge(struct pm860x_charger_info *info)
0180 {
0181     int ret;
0182 
0183     dev_dbg(info->dev, "Start Pre-charging!\n");
0184     set_vbatt_threshold(info, 0, 0);
0185 
0186     ret = pm860x_reg_write(info->i2c_8606, PM8606_PREREGULATORA,
0187                    PREREG1_1350MA | PREREG1_VSYS_4_5V);
0188     if (ret < 0)
0189         goto out;
0190     /* stop charging */
0191     ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL1, 3,
0192                   CC1_MODE_OFF);
0193     if (ret < 0)
0194         goto out;
0195     /* set 270 minutes timeout */
0196     ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL3, (0xf << 4),
0197                   CC3_270MIN_TIMEOUT);
0198     if (ret < 0)
0199         goto out;
0200     /* set precharge current, termination voltage, IBAT & TBAT monitor */
0201     ret = pm860x_reg_write(info->i2c, PM8607_CHG_CTRL4,
0202                    CC4_IPRE_40MA | CC4_VPCHG_3_2V |
0203                    CC4_IFCHG_MON_EN | CC4_BTEMP_MON_EN);
0204     if (ret < 0)
0205         goto out;
0206     ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL7,
0207                   CC7_BAT_REM_EN | CC7_IFSM_EN,
0208                   CC7_BAT_REM_EN | CC7_IFSM_EN);
0209     if (ret < 0)
0210         goto out;
0211     /* trigger precharge */
0212     ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL1, 3,
0213                   CC1_MODE_PRECHARGE);
0214 out:
0215     return ret;
0216 }
0217 
0218 static int start_fastcharge(struct pm860x_charger_info *info)
0219 {
0220     int ret;
0221 
0222     dev_dbg(info->dev, "Start Fast-charging!\n");
0223 
0224     /* set fastcharge termination current & voltage, disable charging */
0225     ret = pm860x_reg_write(info->i2c, PM8607_CHG_CTRL1,
0226                    CC1_MODE_OFF | CC1_ITERM_60MA |
0227                    CC1_VFCHG_4_2V);
0228     if (ret < 0)
0229         goto out;
0230     ret = pm860x_reg_write(info->i2c_8606, PM8606_PREREGULATORA,
0231                    PREREG1_540MA | PREREG1_VSYS_4_5V);
0232     if (ret < 0)
0233         goto out;
0234     ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL2, 0x1f,
0235                   CC2_ICHG_500MA);
0236     if (ret < 0)
0237         goto out;
0238     /* set 270 minutes timeout */
0239     ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL3, (0xf << 4),
0240                   CC3_270MIN_TIMEOUT);
0241     if (ret < 0)
0242         goto out;
0243     /* set IBAT & TBAT monitor */
0244     ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL4,
0245                   CC4_IFCHG_MON_EN | CC4_BTEMP_MON_EN,
0246                   CC4_IFCHG_MON_EN | CC4_BTEMP_MON_EN);
0247     if (ret < 0)
0248         goto out;
0249     ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL6,
0250                   CC6_BAT_OV_EN | CC6_BAT_UV_EN |
0251                   CC6_UV_VBAT_SET,
0252                   CC6_BAT_OV_EN | CC6_BAT_UV_EN |
0253                   CC6_UV_VBAT_SET);
0254     if (ret < 0)
0255         goto out;
0256     ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL7,
0257                   CC7_BAT_REM_EN | CC7_IFSM_EN,
0258                   CC7_BAT_REM_EN | CC7_IFSM_EN);
0259     if (ret < 0)
0260         goto out;
0261     /* launch fast-charge */
0262     ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL1, 3,
0263                   CC1_MODE_FASTCHARGE);
0264     /* vchg threshold setting */
0265     set_vchg_threshold(info, VCHG_NORMAL_LOW, VCHG_NORMAL_HIGH);
0266 out:
0267     return ret;
0268 }
0269 
0270 static void stop_charge(struct pm860x_charger_info *info, int vbatt)
0271 {
0272     dev_dbg(info->dev, "Stop charging!\n");
0273     pm860x_set_bits(info->i2c, PM8607_CHG_CTRL1, 3, CC1_MODE_OFF);
0274     if (vbatt > CHARGE_THRESHOLD && info->online)
0275         set_vbatt_threshold(info, CHARGE_THRESHOLD, 0);
0276 }
0277 
0278 static void power_off_notification(struct pm860x_charger_info *info)
0279 {
0280     dev_dbg(info->dev, "Power-off notification!\n");
0281 }
0282 
0283 static int set_charging_fsm(struct pm860x_charger_info *info)
0284 {
0285     struct power_supply *psy;
0286     union power_supply_propval data;
0287     unsigned char fsm_state[][16] = { "init", "discharge", "precharge",
0288         "fastcharge",
0289     };
0290     int ret;
0291     int vbatt;
0292 
0293     psy = power_supply_get_by_name(pm860x_supplied_to[0]);
0294     if (!psy)
0295         return -EINVAL;
0296     ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_VOLTAGE_NOW,
0297             &data);
0298     if (ret) {
0299         power_supply_put(psy);
0300         return ret;
0301     }
0302     vbatt = data.intval / 1000;
0303 
0304     ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_PRESENT, &data);
0305     if (ret) {
0306         power_supply_put(psy);
0307         return ret;
0308     }
0309     power_supply_put(psy);
0310 
0311     mutex_lock(&info->lock);
0312     info->present = data.intval;
0313 
0314     dev_dbg(info->dev, "Entering FSM:%s, Charger:%s, Battery:%s, "
0315         "Allowed:%d\n",
0316         &fsm_state[info->state][0],
0317         (info->online) ? "online" : "N/A",
0318         (info->present) ? "present" : "N/A", info->allowed);
0319     dev_dbg(info->dev, "set_charging_fsm:vbatt:%d(mV)\n", vbatt);
0320 
0321     switch (info->state) {
0322     case FSM_INIT:
0323         if (info->online && info->present && info->allowed) {
0324             if (vbatt < PRECHARGE_THRESHOLD) {
0325                 info->state = FSM_PRECHARGE;
0326                 start_precharge(info);
0327             } else if (vbatt > DISCHARGE_THRESHOLD) {
0328                 info->state = FSM_DISCHARGE;
0329                 stop_charge(info, vbatt);
0330             } else if (vbatt < DISCHARGE_THRESHOLD) {
0331                 info->state = FSM_FASTCHARGE;
0332                 start_fastcharge(info);
0333             }
0334         } else {
0335             if (vbatt < POWEROFF_THRESHOLD) {
0336                 power_off_notification(info);
0337             } else {
0338                 info->state = FSM_DISCHARGE;
0339                 stop_charge(info, vbatt);
0340             }
0341         }
0342         break;
0343     case FSM_PRECHARGE:
0344         if (info->online && info->present && info->allowed) {
0345             if (vbatt > PRECHARGE_THRESHOLD) {
0346                 info->state = FSM_FASTCHARGE;
0347                 start_fastcharge(info);
0348             }
0349         } else {
0350             info->state = FSM_DISCHARGE;
0351             stop_charge(info, vbatt);
0352         }
0353         break;
0354     case FSM_FASTCHARGE:
0355         if (info->online && info->present && info->allowed) {
0356             if (vbatt < PRECHARGE_THRESHOLD) {
0357                 info->state = FSM_PRECHARGE;
0358                 start_precharge(info);
0359             }
0360         } else {
0361             info->state = FSM_DISCHARGE;
0362             stop_charge(info, vbatt);
0363         }
0364         break;
0365     case FSM_DISCHARGE:
0366         if (info->online && info->present && info->allowed) {
0367             if (vbatt < PRECHARGE_THRESHOLD) {
0368                 info->state = FSM_PRECHARGE;
0369                 start_precharge(info);
0370             } else if (vbatt < DISCHARGE_THRESHOLD) {
0371                 info->state = FSM_FASTCHARGE;
0372                 start_fastcharge(info);
0373             }
0374         } else {
0375             if (vbatt < POWEROFF_THRESHOLD)
0376                 power_off_notification(info);
0377             else if (vbatt > CHARGE_THRESHOLD && info->online)
0378                 set_vbatt_threshold(info, CHARGE_THRESHOLD, 0);
0379         }
0380         break;
0381     default:
0382         dev_warn(info->dev, "FSM meets wrong state:%d\n",
0383              info->state);
0384         break;
0385     }
0386     dev_dbg(info->dev,
0387         "Out FSM:%s, Charger:%s, Battery:%s, Allowed:%d\n",
0388         &fsm_state[info->state][0],
0389         (info->online) ? "online" : "N/A",
0390         (info->present) ? "present" : "N/A", info->allowed);
0391     mutex_unlock(&info->lock);
0392 
0393     return 0;
0394 }
0395 
0396 static irqreturn_t pm860x_charger_handler(int irq, void *data)
0397 {
0398     struct pm860x_charger_info *info = data;
0399     int ret;
0400 
0401     mutex_lock(&info->lock);
0402     ret = pm860x_reg_read(info->i2c, PM8607_STATUS_2);
0403     if (ret < 0) {
0404         mutex_unlock(&info->lock);
0405         goto out;
0406     }
0407     if (ret & STATUS2_CHG) {
0408         info->online = 1;
0409         info->allowed = 1;
0410     } else {
0411         info->online = 0;
0412         info->allowed = 0;
0413     }
0414     mutex_unlock(&info->lock);
0415     dev_dbg(info->dev, "%s, Charger:%s, Allowed:%d\n", __func__,
0416         (info->online) ? "online" : "N/A", info->allowed);
0417 
0418     set_charging_fsm(info);
0419 
0420     power_supply_changed(info->usb);
0421 out:
0422     return IRQ_HANDLED;
0423 }
0424 
0425 static irqreturn_t pm860x_temp_handler(int irq, void *data)
0426 {
0427     struct power_supply *psy;
0428     struct pm860x_charger_info *info = data;
0429     union power_supply_propval temp;
0430     int value;
0431     int ret;
0432 
0433     psy = power_supply_get_by_name(pm860x_supplied_to[0]);
0434     if (!psy)
0435         return IRQ_HANDLED;
0436     ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_TEMP, &temp);
0437     if (ret)
0438         goto out;
0439     value = temp.intval / 10;
0440 
0441     mutex_lock(&info->lock);
0442     /* Temperature < -10 C or >40 C, Will not allow charge */
0443     if (value < -10 || value > 40)
0444         info->allowed = 0;
0445     else
0446         info->allowed = 1;
0447     dev_dbg(info->dev, "%s, Allowed: %d\n", __func__, info->allowed);
0448     mutex_unlock(&info->lock);
0449 
0450     set_charging_fsm(info);
0451 out:
0452     power_supply_put(psy);
0453     return IRQ_HANDLED;
0454 }
0455 
0456 static irqreturn_t pm860x_exception_handler(int irq, void *data)
0457 {
0458     struct pm860x_charger_info *info = data;
0459 
0460     mutex_lock(&info->lock);
0461     info->allowed = 0;
0462     mutex_unlock(&info->lock);
0463     dev_dbg(info->dev, "%s, irq: %d\n", __func__, irq);
0464 
0465     set_charging_fsm(info);
0466     return IRQ_HANDLED;
0467 }
0468 
0469 static irqreturn_t pm860x_done_handler(int irq, void *data)
0470 {
0471     struct pm860x_charger_info *info = data;
0472     struct power_supply *psy;
0473     union power_supply_propval val;
0474     int ret;
0475     int vbatt;
0476 
0477     mutex_lock(&info->lock);
0478     /* pre-charge done, will transimit to fast-charge stage */
0479     if (info->state == FSM_PRECHARGE) {
0480         info->allowed = 1;
0481         goto out;
0482     }
0483     /*
0484      * Fast charge done, delay to read
0485      * the correct status of CHG_DET.
0486      */
0487     mdelay(5);
0488     info->allowed = 0;
0489     psy = power_supply_get_by_name(pm860x_supplied_to[0]);
0490     if (!psy)
0491         goto out;
0492     ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_VOLTAGE_NOW,
0493             &val);
0494     if (ret)
0495         goto out_psy_put;
0496     vbatt = val.intval / 1000;
0497     /*
0498      * CHG_DONE interrupt is faster than CHG_DET interrupt when
0499      * plug in/out usb, So we can not rely on info->online, we
0500      * need check pm8607 status register to check usb is online
0501      * or not, then we can decide it is real charge done
0502      * automatically or it is triggered by usb plug out;
0503      */
0504     ret = pm860x_reg_read(info->i2c, PM8607_STATUS_2);
0505     if (ret < 0)
0506         goto out_psy_put;
0507     if (vbatt > CHARGE_THRESHOLD && ret & STATUS2_CHG)
0508         power_supply_set_property(psy, POWER_SUPPLY_PROP_CHARGE_FULL,
0509                 &val);
0510 
0511 out_psy_put:
0512     power_supply_put(psy);
0513 out:
0514     mutex_unlock(&info->lock);
0515     dev_dbg(info->dev, "%s, Allowed: %d\n", __func__, info->allowed);
0516     set_charging_fsm(info);
0517 
0518     return IRQ_HANDLED;
0519 }
0520 
0521 static irqreturn_t pm860x_vbattery_handler(int irq, void *data)
0522 {
0523     struct pm860x_charger_info *info = data;
0524 
0525     mutex_lock(&info->lock);
0526 
0527     set_vbatt_threshold(info, 0, 0);
0528 
0529     if (info->present && info->online)
0530         info->allowed = 1;
0531     else
0532         info->allowed = 0;
0533     mutex_unlock(&info->lock);
0534     dev_dbg(info->dev, "%s, Allowed: %d\n", __func__, info->allowed);
0535 
0536     set_charging_fsm(info);
0537 
0538     return IRQ_HANDLED;
0539 }
0540 
0541 static irqreturn_t pm860x_vchg_handler(int irq, void *data)
0542 {
0543     struct pm860x_charger_info *info = data;
0544     int vchg = 0;
0545 
0546     if (info->present)
0547         goto out;
0548 
0549     measure_vchg(info, &vchg);
0550 
0551     mutex_lock(&info->lock);
0552     if (!info->online) {
0553         int status;
0554         /* check if over-temp on pm8606 or not */
0555         status = pm860x_reg_read(info->i2c_8606, PM8606_FLAGS);
0556         if (status & OVER_TEMP_FLAG) {
0557             /* clear over temp flag and set auto recover */
0558             pm860x_set_bits(info->i2c_8606, PM8606_FLAGS,
0559                     OVER_TEMP_FLAG, OVER_TEMP_FLAG);
0560             pm860x_set_bits(info->i2c_8606,
0561                     PM8606_VSYS,
0562                     OVTEMP_AUTORECOVER,
0563                     OVTEMP_AUTORECOVER);
0564             dev_dbg(info->dev,
0565                 "%s, pm8606 over-temp occurred\n", __func__);
0566         }
0567     }
0568 
0569     if (vchg > VCHG_NORMAL_CHECK) {
0570         set_vchg_threshold(info, VCHG_OVP_LOW, 0);
0571         info->allowed = 0;
0572         dev_dbg(info->dev,
0573             "%s,pm8607 over-vchg occurred,vchg = %dmv\n",
0574             __func__, vchg);
0575     } else if (vchg < VCHG_OVP_LOW) {
0576         set_vchg_threshold(info, VCHG_NORMAL_LOW,
0577                    VCHG_NORMAL_HIGH);
0578         info->allowed = 1;
0579         dev_dbg(info->dev,
0580             "%s,pm8607 over-vchg recover,vchg = %dmv\n",
0581             __func__, vchg);
0582     }
0583     mutex_unlock(&info->lock);
0584 
0585     dev_dbg(info->dev, "%s, Allowed: %d\n", __func__, info->allowed);
0586     set_charging_fsm(info);
0587 out:
0588     return IRQ_HANDLED;
0589 }
0590 
0591 static int pm860x_usb_get_prop(struct power_supply *psy,
0592                    enum power_supply_property psp,
0593                    union power_supply_propval *val)
0594 {
0595     struct pm860x_charger_info *info = power_supply_get_drvdata(psy);
0596 
0597     switch (psp) {
0598     case POWER_SUPPLY_PROP_STATUS:
0599         if (info->state == FSM_FASTCHARGE ||
0600                 info->state == FSM_PRECHARGE)
0601             val->intval = POWER_SUPPLY_STATUS_CHARGING;
0602         else
0603             val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
0604         break;
0605     case POWER_SUPPLY_PROP_ONLINE:
0606         val->intval = info->online;
0607         break;
0608     default:
0609         return -ENODEV;
0610     }
0611     return 0;
0612 }
0613 
0614 static enum power_supply_property pm860x_usb_props[] = {
0615     POWER_SUPPLY_PROP_STATUS,
0616     POWER_SUPPLY_PROP_ONLINE,
0617 };
0618 
0619 static int pm860x_init_charger(struct pm860x_charger_info *info)
0620 {
0621     int ret;
0622 
0623     ret = pm860x_reg_read(info->i2c, PM8607_STATUS_2);
0624     if (ret < 0)
0625         return ret;
0626 
0627     mutex_lock(&info->lock);
0628     info->state = FSM_INIT;
0629     if (ret & STATUS2_CHG) {
0630         info->online = 1;
0631         info->allowed = 1;
0632     } else {
0633         info->online = 0;
0634         info->allowed = 0;
0635     }
0636     mutex_unlock(&info->lock);
0637 
0638     set_charging_fsm(info);
0639     return 0;
0640 }
0641 
0642 static struct pm860x_irq_desc {
0643     const char *name;
0644     irqreturn_t (*handler)(int irq, void *data);
0645 } pm860x_irq_descs[] = {
0646     { "usb supply detect", pm860x_charger_handler },
0647     { "charge done", pm860x_done_handler },
0648     { "charge timeout", pm860x_exception_handler },
0649     { "charge fault", pm860x_exception_handler },
0650     { "temperature", pm860x_temp_handler },
0651     { "vbatt", pm860x_vbattery_handler },
0652     { "vchg", pm860x_vchg_handler },
0653 };
0654 
0655 static const struct power_supply_desc pm860x_charger_desc = {
0656     .name       = "usb",
0657     .type       = POWER_SUPPLY_TYPE_USB,
0658     .properties = pm860x_usb_props,
0659     .num_properties = ARRAY_SIZE(pm860x_usb_props),
0660     .get_property   = pm860x_usb_get_prop,
0661 };
0662 
0663 static int pm860x_charger_probe(struct platform_device *pdev)
0664 {
0665     struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
0666     struct power_supply_config psy_cfg = {};
0667     struct pm860x_charger_info *info;
0668     int ret;
0669     int count;
0670     int i;
0671     int j;
0672 
0673     info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
0674     if (!info)
0675         return -ENOMEM;
0676 
0677     count = pdev->num_resources;
0678     for (i = 0, j = 0; i < count; i++) {
0679         info->irq[j] = platform_get_irq(pdev, i);
0680         if (info->irq[j] < 0)
0681             continue;
0682         j++;
0683     }
0684     info->irq_nums = j;
0685 
0686     info->chip = chip;
0687     info->i2c =
0688         (chip->id == CHIP_PM8607) ? chip->client : chip->companion;
0689     info->i2c_8606 =
0690         (chip->id == CHIP_PM8607) ? chip->companion : chip->client;
0691     if (!info->i2c_8606) {
0692         dev_err(&pdev->dev, "Missed I2C address of 88PM8606!\n");
0693         ret = -EINVAL;
0694         goto out;
0695     }
0696     info->dev = &pdev->dev;
0697 
0698     /* set init value for the case we are not using battery */
0699     set_vchg_threshold(info, VCHG_NORMAL_LOW, VCHG_OVP_LOW);
0700 
0701     mutex_init(&info->lock);
0702     platform_set_drvdata(pdev, info);
0703 
0704     psy_cfg.drv_data = info;
0705     psy_cfg.supplied_to = pm860x_supplied_to;
0706     psy_cfg.num_supplicants = ARRAY_SIZE(pm860x_supplied_to);
0707     info->usb = power_supply_register(&pdev->dev, &pm860x_charger_desc,
0708                       &psy_cfg);
0709     if (IS_ERR(info->usb)) {
0710         ret = PTR_ERR(info->usb);
0711         goto out;
0712     }
0713 
0714     pm860x_init_charger(info);
0715 
0716     for (i = 0; i < ARRAY_SIZE(info->irq); i++) {
0717         ret = request_threaded_irq(info->irq[i], NULL,
0718             pm860x_irq_descs[i].handler,
0719             IRQF_ONESHOT, pm860x_irq_descs[i].name, info);
0720         if (ret < 0) {
0721             dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n",
0722                 info->irq[i], ret);
0723             goto out_irq;
0724         }
0725     }
0726     return 0;
0727 
0728 out_irq:
0729     power_supply_unregister(info->usb);
0730     while (--i >= 0)
0731         free_irq(info->irq[i], info);
0732 out:
0733     return ret;
0734 }
0735 
0736 static int pm860x_charger_remove(struct platform_device *pdev)
0737 {
0738     struct pm860x_charger_info *info = platform_get_drvdata(pdev);
0739     int i;
0740 
0741     power_supply_unregister(info->usb);
0742     for (i = 0; i < info->irq_nums; i++)
0743         free_irq(info->irq[i], info);
0744     return 0;
0745 }
0746 
0747 static struct platform_driver pm860x_charger_driver = {
0748     .driver = {
0749            .name = "88pm860x-charger",
0750     },
0751     .probe = pm860x_charger_probe,
0752     .remove = pm860x_charger_remove,
0753 };
0754 module_platform_driver(pm860x_charger_driver);
0755 
0756 MODULE_DESCRIPTION("Marvell 88PM860x Charger driver");
0757 MODULE_LICENSE("GPL");