power: rk81x-battery: add calc seconds availability check
authorJianhong Chen <chenjh@rock-chips.com>
Mon, 13 Jul 2015 14:21:22 +0000 (22:21 +0800)
committer许盛飞 <xsf@rock-chips.com>
Tue, 14 Jul 2015 05:51:49 +0000 (13:51 +0800)
Signed-off-by: Jianhong Chen <chenjh@rock-chips.com>
Signed-off-by: 许盛飞 <xsf@rock-chips.com>
drivers/power/rk818_battery.c

index 6c8d31bf9c82ed8ed64d198a7ae13aef5ba8b23f..8b2fd397cfc2563823f1f92252b996defe9b2271 100644 (file)
@@ -380,7 +380,7 @@ static u64 is_local_clock_reset(void)
 static inline unsigned long  BASE_TO_SEC(unsigned long x)
 {
        if (x)
 static inline unsigned long  BASE_TO_SEC(unsigned long x)
 {
        if (x)
-               return get_runtime_sec() - x;
+               return (get_runtime_sec() > x) ? (get_runtime_sec() - x) : 0;
        else
                return 0;
 }
        else
                return 0;
 }
@@ -2958,6 +2958,11 @@ static void rk81x_bat_finish_chrg(struct rk81x_battery *di)
        }
 }
 
        }
 }
 
+static u8 rk81x_bat_get_valid_soc(unsigned long soc)
+{
+       return (soc <= 100) ? soc : 0;
+}
+
 static void rk81x_bat_normal_chrg(struct rk81x_battery *di)
 {
        int now_current;
 static void rk81x_bat_normal_chrg(struct rk81x_battery *di)
 {
        int now_current;
@@ -2986,7 +2991,7 @@ static void rk81x_bat_normal_chrg(struct rk81x_battery *di)
        } else if (di->rsoc < di->dsoc + 1) {
                DBG("<%s>. rsoc < dsoc + 1\n", __func__);
                unit_sec = soc_time * 3 / 2;
        } else if (di->rsoc < di->dsoc + 1) {
                DBG("<%s>. rsoc < dsoc + 1\n", __func__);
                unit_sec = soc_time * 3 / 2;
-               plus_soc = chrg_normal_sec / unit_sec;
+               plus_soc = rk81x_bat_get_valid_soc(chrg_normal_sec / unit_sec);
                if  (chrg_normal_sec > unit_sec) {
                        di->dsoc += plus_soc;
                        di->chrg_normal_base = get_runtime_sec();
                if  (chrg_normal_sec > unit_sec) {
                        di->dsoc += plus_soc;
                        di->chrg_normal_base = get_runtime_sec();
@@ -2996,7 +3001,7 @@ static void rk81x_bat_normal_chrg(struct rk81x_battery *di)
        } else if (di->rsoc > di->dsoc + 1) {
                DBG("<%s>. rsoc > dsoc + 1\n", __func__);
                unit_sec = soc_time * 3 / 4;
        } else if (di->rsoc > di->dsoc + 1) {
                DBG("<%s>. rsoc > dsoc + 1\n", __func__);
                unit_sec = soc_time * 3 / 4;
-               plus_soc = chrg_normal_sec / unit_sec;
+               plus_soc = rk81x_bat_get_valid_soc(chrg_normal_sec / unit_sec);
                if  (chrg_normal_sec > unit_sec) {
                        di->dsoc += plus_soc;
                        di->chrg_normal_base = get_runtime_sec();
                if  (chrg_normal_sec > unit_sec) {
                        di->dsoc += plus_soc;
                        di->chrg_normal_base = get_runtime_sec();
@@ -3970,7 +3975,7 @@ err:
 static int rk81x_bat_get_suspend_sec(struct rk81x_battery *di)
 {
        int err;
 static int rk81x_bat_get_suspend_sec(struct rk81x_battery *di)
 {
        int err;
-       int delta_sec;
+       int delta_sec = 0;
        struct rtc_time tm;
        struct timespec tv = {
                .tv_nsec = NSEC_PER_SEC >> 1,
        struct rtc_time tm;
        struct timespec tv = {
                .tv_nsec = NSEC_PER_SEC >> 1,
@@ -3981,17 +3986,19 @@ static int rk81x_bat_get_suspend_sec(struct rk81x_battery *di)
        if (err) {
                dev_err(rtc->dev.parent,
                        "hctosys: unable to read the hardware clock\n");
        if (err) {
                dev_err(rtc->dev.parent,
                        "hctosys: unable to read the hardware clock\n");
+               goto out;
        }
        err = rtc_valid_tm(&tm);
        if (err) {
                dev_err(rtc->dev.parent,
                        "hctosys: invalid date/time\n");
        }
        err = rtc_valid_tm(&tm);
        if (err) {
                dev_err(rtc->dev.parent,
                        "hctosys: invalid date/time\n");
+               goto out;
        }
 
        rtc_tm_to_time(&tm, &tv.tv_sec);
        delta_sec = tv.tv_sec - di->suspend_rtc_base.tv_sec;
        }
 
        rtc_tm_to_time(&tm, &tv.tv_sec);
        delta_sec = tv.tv_sec - di->suspend_rtc_base.tv_sec;
-
-       return delta_sec;
+out:
+       return (delta_sec > 0) ? delta_sec : 0;
 }
 
 #ifdef CONFIG_OF
 }
 
 #ifdef CONFIG_OF