mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 2/2] misc: ds1682: Ignore update-in-progress ETC reads
@ 2017-12-22 16:51 Aaron Sierra
  0 siblings, 0 replies; only message in thread
From: Aaron Sierra @ 2017-12-22 16:51 UTC (permalink / raw)
  To: Arnd Bergmann, Greg Kroah-Hartman; +Cc: linux-kernel, Thomas Vanselus

From: Thomas VanSelus <tvanselus@xes-inc.com>

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 <tvanselus@xes-inc.com>
Signed-off-by: Aaron Sierra <asierra@xes-inc.com>
---
 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

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2017-12-22 16:58 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-12-22 16:51 [PATCH 2/2] misc: ds1682: Ignore update-in-progress ETC reads Aaron Sierra

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®