+
+static int ltc2497_wait_conv(struct ltc2497_st *st)
+{
+ s64 time_elapsed;
+
+ time_elapsed = ktime_ms_delta(ktime_get(), st->time_prev);
+
+ if (time_elapsed < LTC2497_CONVERSION_TIME_MS) {
+ /* delay if conversion time not passed
+ * since last read or write
+ */
+ msleep(LTC2497_CONVERSION_TIME_MS - time_elapsed);
Considering how long this sleeps msleep_interruptible() might be the better
choice.
+ return 0;
+ }
+
+ if (time_elapsed - LTC2497_CONVERSION_TIME_MS <= 0) {
+ /* We're in automatic mode -
+ * so the last reading is stil not outdated
+ */
+ return 0;
+ }
+
+ return -ETIMEDOUT;
+}
+
+static int ltc2497_read(struct ltc2497_st *st, u8 address, int *val)
+{
+ struct i2c_client *client = st->client;
+ __be32 buf = 0;
transfer buffers must not be on the stack to avoid issues if the controller
should use DMA.
+ int ret;[...]
+
+ ret = ltc2497_wait_conv(st);
+ if (ret < 0 || st->addr_prev != address) {
+ ret = i2c_smbus_write_byte(st->client, 0xA0 | address);
+ if (ret < 0)
+ return ret;
+ st->addr_prev = address;
+ msleep(LTC2497_CONVERSION_TIME_MS);
+ }
+ ret = i2c_master_recv(client, (char *)&buf, 3);
+ if (ret < 0) {
+ dev_err(&client->dev, "i2c_master_recv failed\n");
+ return ret;
+ }
+ st->time_prev = ktime_get();
+ *val = (be32_to_cpu(buf) >> 14) - (1 << 17);
+
+ return ret;
+}