Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756350AbdLVQ6I (ORCPT ); Fri, 22 Dec 2017 11:58:08 -0500 Received: from xes-mad.com ([216.165.139.220]:52305 "EHLO mail.xes-mad.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753466AbdLVQ6H (ORCPT ); Fri, 22 Dec 2017 11:58:07 -0500 X-Greylist: delayed 416 seconds by postgrey-1.27 at vger.kernel.org; Fri, 22 Dec 2017 11:58:06 EST Date: Fri, 22 Dec 2017 10:51:23 -0600 (CST) From: Aaron Sierra To: Arnd Bergmann , Greg Kroah-Hartman Cc: linux-kernel@vger.kernel.org, Thomas Vanselus Message-ID: <1247697859.97142.1513961483361.JavaMail.zimbra@xes-inc.com> Subject: [PATCH 2/2] misc: ds1682: Ignore update-in-progress ETC reads MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Originating-IP: [10.52.0.127] X-Mailer: Zimbra 8.7.5_GA_1764 (ZimbraWebClient - FF57 (Linux)/8.7.5_GA_1764) Thread-Index: EUTIniUxOfZWRTgvVgCf659bYMKh9A== Thread-Topic: misc: ds1682: Ignore update-in-progress ETC reads Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 1848 Lines: 55 From: Thomas VanSelus The Elapsed Time Counter (ETC) registers are not buffered for reading. If a 250ms tick occurs while data is being read out, the result can be a combination of old and new values. This can occur at the byte level (giving a time in the future) or the individual bit level (giving a time in the past). We catch both these cases by reading until we get two equal or consecutive values. After five unsuccessful attempts we give up. Signed-off-by: Thomas VanSelus Signed-off-by: Aaron Sierra --- drivers/misc/ds1682.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/drivers/misc/ds1682.c b/drivers/misc/ds1682.c index 0fc5366..98a921e 100644 --- a/drivers/misc/ds1682.c +++ b/drivers/misc/ds1682.c @@ -59,7 +59,7 @@ static ssize_t ds1682_show(struct device *dev, struct device_attribute *attr, { struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); struct i2c_client *client = to_i2c_client(dev); - unsigned long long val; + unsigned long long val, check; __le32 val_le = 0; int rc; @@ -73,6 +73,23 @@ static ssize_t ds1682_show(struct device *dev, struct device_attribute *attr, val = le32_to_cpu(val_le); + if (sattr->index == DS1682_REG_ELAPSED) { + int retries = 5; + + /* Detect and retry when a tick occurs mid-read */ + do { + rc = i2c_smbus_read_i2c_block_data(client, sattr->index, + sattr->nr, + (u8 *)&val_le); + if (rc < 0 || retries <= 0) + return -EIO; + + check = val; + val = le32_to_cpu(val_le); + retries--; + } while (val != check && val != (check + 1)); + } + /* Format the output string and return # of bytes * Special case: the 32 bit regs are time values with 1/4s * resolution, scale them up to milliseconds -- 2.7.4