From: Hakan Berg <hakan.b...@stericsson.com>

Battery tempreature readings sometimes fails and results in
a value far from recent values. This patch adds a software
filter that disposes such readings, by allowing direct
updates on temperature only if two samples result in the
same temperature. Else only allow 1 degree change from previous
reported value in the direction of the new measurement.

Signed-off-by: Hakan Berg <hakan.b...@stericsson.com>
Signed-off-by: Mathieu Poirier <mathieu.poir...@linaro.org>
Reviewed-by: Marcus COOPER <marcus.xm.coo...@stericsson.com>
Reviewed-by: Martin SJOBLOM <martin.w.sjob...@stericsson.com>
Reviewed-by: Rabin VINCENT <rabin.vinc...@stericsson.com>
---
 drivers/power/ab8500_btemp.c |   27 +++++++++++++++++++++------
 1 files changed, 21 insertions(+), 6 deletions(-)

diff --git a/drivers/power/ab8500_btemp.c b/drivers/power/ab8500_btemp.c
index 56a3bb9..b24835f 100644
--- a/drivers/power/ab8500_btemp.c
+++ b/drivers/power/ab8500_btemp.c
@@ -74,8 +74,8 @@ struct ab8500_btemp_ranges {
  * @dev:               Pointer to the structure device
  * @node:              List of AB8500 BTEMPs, hence prepared for reentrance
  * @curr_source:       What current source we use, in uA
- * @bat_temp:          Battery temperature in degree Celcius
- * @prev_bat_temp      Last dispatched battery temperature
+ * @bat_temp:          Dispatched battery temperature in degree Celcius
+ * @prev_bat_temp      Last measured battery temperature in degree Celcius
  * @parent:            Pointer to the struct ab8500
  * @gpadc:             Pointer to the struct gpadc
  * @fg:                        Pointer to the struct fg
@@ -608,6 +608,7 @@ static int ab8500_btemp_id(struct ab8500_btemp *di)
 static void ab8500_btemp_periodic_work(struct work_struct *work)
 {
        int interval;
+       int bat_temp;
        struct ab8500_btemp *di = container_of(work,
                struct ab8500_btemp, btemp_periodic_work.work);
 
@@ -618,12 +619,26 @@ static void ab8500_btemp_periodic_work(struct work_struct 
*work)
                        dev_warn(di->dev, "failed to identify the battery\n");
        }
 
-       di->bat_temp = ab8500_btemp_measure_temp(di);
-
-       if (di->bat_temp != di->prev_bat_temp) {
-               di->prev_bat_temp = di->bat_temp;
+       bat_temp = ab8500_btemp_measure_temp(di);
+       /*
+        * Filter battery temperature.
+        * Allow direct updates on temperature only if two samples result in
+        * same temperature. Else only allow 1 degree change from previous
+        * reported value in the direction of the new measurement.
+        */
+       if (bat_temp == di->prev_bat_temp || !di->initialized) {
+               if (di->bat_temp != di->prev_bat_temp || !di->initialized) {
+                       di->bat_temp = bat_temp;
+                       power_supply_changed(&di->btemp_psy);
+               }
+       } else if (bat_temp < di->prev_bat_temp) {
+               di->bat_temp--;
+               power_supply_changed(&di->btemp_psy);
+       } else if (bat_temp > di->prev_bat_temp) {
+               di->bat_temp++;
                power_supply_changed(&di->btemp_psy);
        }
+       di->prev_bat_temp = bat_temp;
 
        if (di->events.ac_conn || di->events.usb_conn)
                interval = di->bat->temp_interval_chg;
-- 
1.7.5.4

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

Reply via email to