power: supply: max17042_battery: Always fall back to default platform-data
[sfrench/cifs-2.6.git] / drivers / power / supply / max17042_battery.c
index 5b556a13f517f8e7859182fbb6c9ccfe422dd609..35dde81b1c9b35a8959c50d63e69596491a094c2 100644 (file)
@@ -123,6 +123,8 @@ static int max17042_get_temperature(struct max17042_chip *chip, int *temp)
 static int max17042_get_status(struct max17042_chip *chip, int *status)
 {
        int ret, charge_full, charge_now;
+       int avg_current;
+       u32 data;
 
        ret = power_supply_am_i_supplied(chip->battery);
        if (ret < 0) {
@@ -152,10 +154,31 @@ static int max17042_get_status(struct max17042_chip *chip, int *status)
        if (ret < 0)
                return ret;
 
-       if ((charge_full - charge_now) <= MAX17042_FULL_THRESHOLD)
+       if ((charge_full - charge_now) <= MAX17042_FULL_THRESHOLD) {
                *status = POWER_SUPPLY_STATUS_FULL;
-       else
+               return 0;
+       }
+
+       /*
+        * Even though we are supplied, we may still be discharging if the
+        * supply is e.g. only delivering 5V 0.5A. Check current if available.
+        */
+       if (!chip->pdata->enable_current_sense) {
                *status = POWER_SUPPLY_STATUS_CHARGING;
+               return 0;
+       }
+
+       ret = regmap_read(chip->regmap, MAX17042_AvgCurrent, &data);
+       if (ret < 0)
+               return ret;
+
+       avg_current = sign_extend32(data, 15);
+       avg_current *= 1562500 / chip->pdata->r_sns;
+
+       if (avg_current > 0)
+               *status = POWER_SUPPLY_STATUS_CHARGING;
+       else
+               *status = POWER_SUPPLY_STATUS_DISCHARGING;
 
        return 0;
 }
@@ -863,16 +886,13 @@ static void max17042_init_worker(struct work_struct *work)
 
 #ifdef CONFIG_OF
 static struct max17042_platform_data *
-max17042_get_pdata(struct max17042_chip *chip)
+max17042_get_of_pdata(struct max17042_chip *chip)
 {
        struct device *dev = &chip->client->dev;
        struct device_node *np = dev->of_node;
        u32 prop;
        struct max17042_platform_data *pdata;
 
-       if (!np)
-               return dev->platform_data;
-
        pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
        if (!pdata)
                return NULL;
@@ -897,7 +917,8 @@ max17042_get_pdata(struct max17042_chip *chip)
 
        return pdata;
 }
-#else
+#endif
+
 static struct max17042_reg_data max17047_default_pdata_init_regs[] = {
        /*
         * Some firmwares do not set FullSOCThr, Enable End-of-Charge Detection
@@ -907,15 +928,12 @@ static struct max17042_reg_data max17047_default_pdata_init_regs[] = {
 };
 
 static struct max17042_platform_data *
-max17042_get_pdata(struct max17042_chip *chip)
+max17042_get_default_pdata(struct max17042_chip *chip)
 {
        struct device *dev = &chip->client->dev;
        struct max17042_platform_data *pdata;
        int ret, misc_cfg;
 
-       if (dev->platform_data)
-               return dev->platform_data;
-
        /*
         * The MAX17047 gets used on x86 where we might not have pdata, assume
         * the firmware will already have initialized the fuel-gauge and provide
@@ -948,7 +966,21 @@ max17042_get_pdata(struct max17042_chip *chip)
 
        return pdata;
 }
+
+static struct max17042_platform_data *
+max17042_get_pdata(struct max17042_chip *chip)
+{
+       struct device *dev = &chip->client->dev;
+
+#ifdef CONFIG_OF
+       if (dev->of_node)
+               return max17042_get_of_pdata(chip);
 #endif
+       if (dev->platform_data)
+               return dev->platform_data;
+
+       return max17042_get_default_pdata(chip);
+}
 
 static const struct regmap_config max17042_regmap_config = {
        .reg_bits = 8,