i2c-comcerto: Limit duration of I2C transaction

The Comcerto I2C driver implements a state machine that is interrupt
driven. The state machine stalls when interrupts are blocked out.
Depending on what state the state machine is blocked in, the signal SDA
or SCL or both might be held low by the I2C master controller while the
state machine is in this state.
Although this is not a violation of the I2C spec, the temperature sensor
LM96063 resets its SMBus interface if SDA or SCL are held low for more
than 35ms. Resetting the SMBus interface mid transaction might cause a
failure which we are unnable to detect.

Change-Id: I803848fc95f21ab557013fe35a68867cd9bd60cf
diff --git a/drivers/i2c/busses/i2c-comcerto.c b/drivers/i2c/busses/i2c-comcerto.c
index 1eadb81..c10712e 100644
--- a/drivers/i2c/busses/i2c-comcerto.c
+++ b/drivers/i2c/busses/i2c-comcerto.c
@@ -24,6 +24,7 @@
 #include <linux/delay.h>
 #include <linux/platform_device.h>
 #include <linux/slab.h>
+#include <linux/ktime.h>
 #include <asm/io.h>
 #include <asm/sizes.h>
 #include <mach/i2c.h>
@@ -64,6 +65,7 @@
 	int			msg_status;	/* < 0: error, == 0: success, > 0: message in progress */
 	int			msg_len;
 	int			msg_retries;
+	ktime_t			msg_start_time;
 };
 
 #define REG_ADDR(i2c, offset)		((i2c)->membase + (offset))
@@ -176,8 +178,17 @@
 
 static inline void comcerto_i2c_message_complete(struct comcerto_i2c *i2c, int status)
 {
-	i2c->msg_status = status;
+	s64 d;
 	WR_CNTR(i2c, CNTR_STP);
+	d = ktime_us_delta(ktime_get(), i2c->msg_start_time);
+	if (d > 22000) {
+		i2c->msg_status = -ETIME;
+		dev_warn(i2c->dev,
+			"I2C transaction took too long: %lld us. Returning error.\n",
+			(long long) d);
+	} else {
+		i2c->msg_status = status;
+	}
 }
 
 static inline int comcerto_i2c_message_in_progress(struct comcerto_i2c *i2c)
@@ -454,6 +465,7 @@
 	i2c->msg_state = TR_IDLE;
 	i2c->msg_status = 1;
 	i2c->msg_retries = i2c->adapter->retries;
+	i2c->msg_start_time = ktime_get();
 
 polling_mode:
 	if (msg->flags & I2C_M_RD)