]> asedeno.scripts.mit.edu Git - linux.git/blobdiff - drivers/misc/ds1682.c
Merge branch 'x86-pti-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
[linux.git] / drivers / misc / ds1682.c
index 7231260ac287ab54ece61d7c74c6de8a670f84d2..98a921ea9ee898e6ad8cf7ac01b15545c0229590 100644 (file)
@@ -59,25 +59,42 @@ 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);
-       __le32 val = 0;
+       unsigned long long val, check;
+       __le32 val_le = 0;
        int rc;
 
        dev_dbg(dev, "ds1682_show() called on %s\n", attr->attr.name);
 
        /* Read the register */
        rc = i2c_smbus_read_i2c_block_data(client, sattr->index, sattr->nr,
-                                          (u8 *) & val);
+                                          (u8 *)&val_le);
        if (rc < 0)
                return -EIO;
 
-       /* Special case: the 32 bit regs are time values with 1/4s
-        * resolution, scale them up to milliseconds */
-       if (sattr->nr == 4)
-               return sprintf(buf, "%llu\n",
-                       ((unsigned long long)le32_to_cpu(val)) * 250);
+       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 */
-       return sprintf(buf, "%li\n", (long)le32_to_cpu(val));
+       /* 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
+        */
+       return sprintf(buf, "%llu\n", (sattr->nr == 4) ? (val * 250) : val);
 }
 
 static ssize_t ds1682_store(struct device *dev, struct device_attribute *attr,