OSDN Git Service

rtc: Avoid accumulating time drift in suspend/resume
authorJohn Stultz <john.stultz@linaro.org>
Fri, 27 May 2011 18:33:18 +0000 (11:33 -0700)
committerArve Hjønnevåg <arve@android.com>
Wed, 23 Nov 2011 00:49:26 +0000 (16:49 -0800)
commit340ede367101d47c8fd69e83e5e6db9077f4caad
tree68cbc8208a3cb289188cb218fe99a4dfcf271550
parent9ab6a29787b1221a697f85835645549668258bdc
rtc: Avoid accumulating time drift in suspend/resume

Because the RTC interface is only a second granular interface,
each time we read from the RTC for suspend/resume, we introduce a
half second (on average) of error.

In order to avoid this error accumulating as the system is suspended
over and over, this patch measures the time delta between the RTC
and the system CLOCK_REALTIME.

If the delta is less then 2 seconds from the last suspend, we compensate
by using the previous time delta (keeping it close). If it is larger
then 2 seconds, we assume the clock was set or has been changed, so we
do no correction and update the delta.

Note: If NTP is running, ths could seem to "fight" with the NTP corrected
time, where as if the system time was off by 1 second, and NTP slewed the
value in, a suspend/resume cycle could undo this correction, by trying to
restore the previous offset from the RTC. However, without this patch,
since each read could cause almost a full second worth of error, its
possible to get almost 2 seconds of error just from the suspend/resume
cycle alone, so this about equal to any offset added by the compensation.

Further on systems that suspend/resume frequently, this should keep time
closer then NTP could compensate for if the errors were allowed to
accumulate.

Credits to Arve Hjønnevåg for suggesting this solution.

This patch also improves some of the variable names and adds more clear
comments.

CC: Arve Hjønnevåg <arve@android.com>
CC: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: John Stultz <john.stultz@linaro.org>
drivers/rtc/class.c