ramips: add clock strecthing support to i2c driver
authorJohn Crispin <john@openwrt.org>
Fri, 24 Jul 2015 09:11:42 +0000 (09:11 +0000)
committerJohn Crispin <john@openwrt.org>
Fri, 24 Jul 2015 09:11:42 +0000 (09:11 +0000)
fixes #19816

Signed-off-by: John Crispin <blogic@openwrt.org>
SVN-Revision: 46472

target/linux/ramips/patches-3.18/0052-i2c-MIPS-adds-ralink-I2C-driver.patch

index 9b7fba84108247de5ccd079c997f7a83cdb16191..9c2538879c6b580e5f05b34d744329b933a8635d 100644 (file)
@@ -45,7 +45,7 @@ Signed-off-by: John Crispin <blogic@openwrt.org>
 +};
 --- a/drivers/i2c/busses/Kconfig
 +++ b/drivers/i2c/busses/Kconfig
-@@ -710,6 +710,10 @@ config I2C_RK3X
+@@ -710,6 +710,10 @@
          This driver can also be built as a module. If so, the module will
          be called i2c-rk3x.
  
@@ -58,7 +58,7 @@ Signed-off-by: John Crispin <blogic@openwrt.org>
        help
 --- a/drivers/i2c/busses/Makefile
 +++ b/drivers/i2c/busses/Makefile
-@@ -66,6 +66,7 @@ obj-$(CONFIG_I2C_PNX)                += i2c-pnx.o
+@@ -66,6 +66,7 @@
  obj-$(CONFIG_I2C_PUV3)                += i2c-puv3.o
  obj-$(CONFIG_I2C_PXA)         += i2c-pxa.o
  obj-$(CONFIG_I2C_PXA_PCI)     += i2c-pxa-pci.o
@@ -68,7 +68,7 @@ Signed-off-by: John Crispin <blogic@openwrt.org>
  obj-$(CONFIG_I2C_RK3X)                += i2c-rk3x.o
 --- /dev/null
 +++ b/drivers/i2c/busses/i2c-ralink.c
-@@ -0,0 +1,299 @@
+@@ -0,0 +1,302 @@
 +/*
 + * drivers/i2c/busses/i2c-ralink.c
 + *
@@ -122,8 +122,6 @@ Signed-off-by: John Crispin <blogic@openwrt.org>
 +#define I2C_DEVADLEN_7                (6 << 2)
 +#define I2C_ADDRDIS           BIT(1)
 +
-+#define I2C_RETRY             0x1000
-+
 +#define CLKDIV_VALUE          200 // clock rate is 40M, 40M / (200*2) = 100k (standard i2c bus rate).
 +//#define CLKDIV_VALUE                50 // clock rate is 40M, 40M / (50*2) = 400k (fast i2c bus rate).
 +
@@ -131,6 +129,9 @@ Signed-off-by: John Crispin <blogic@openwrt.org>
 +#define WRITE_CMD             0x00
 +#define READ_BLOCK              64
 +
++/* timeout waiting for I2C devices to respond (clock streching) */
++#define RT_I2C_TIMEOUT (msecs_to_jiffies(1000))
++
 +static void __iomem *membase;
 +static struct i2c_adapter *adapter;
 +
@@ -151,38 +152,48 @@ Signed-off-by: John Crispin <blogic@openwrt.org>
 +
 +static inline int rt_i2c_wait_rx_done(void)
 +{
-+      int retries = I2C_RETRY;
++      unsigned long timeout;
++
++      timeout = jiffies + RT_I2C_TIMEOUT;
 +
 +      do {
-+              if (!retries--)
-+                      break;
-+      } while(!(rt_i2c_r32(REG_STATUS_REG) & I2C_DATARDY));
++              if (time_after(jiffies, timeout))
++                      return (-ETIMEDOUT);
 +
-+      return (retries < 0) ? -ETIMEDOUT : 0;
++      } while (!(rt_i2c_r32(REG_STATUS_REG) & I2C_DATARDY));
++
++      return 0;
 +}
 +
 +static inline int rt_i2c_wait_idle(void)
 +{
-+      int retries = I2C_RETRY;
++      unsigned long timeout;
++
++      timeout = jiffies + RT_I2C_TIMEOUT;
 +
 +      do {
-+              if (!retries--)
-+                      break;
-+      } while(rt_i2c_r32(REG_STATUS_REG) & I2C_BUSY);
++              if (time_after(jiffies, timeout)) {
++                      printk("i2c-read line busy\n");
++                      return 1;
++              }
++      } while (rt_i2c_r32(REG_STATUS_REG) & I2C_BUSY);
 +
-+      return (retries < 0);
++      return 0;
 +}
 +
 +static inline int rt_i2c_wait_tx_done(void)
 +{
-+      int retries = I2C_RETRY;
++      unsigned long timeout;
++
++      timeout = jiffies + RT_I2C_TIMEOUT;
 +
 +      do {
-+              if (!retries--)
-+                      break;
-+      } while(!(rt_i2c_r32(REG_STATUS_REG) & I2C_SDOEMPTY));
++              if (time_after(jiffies, timeout))
++                      return (-ETIMEDOUT);
++
++      } while (!(rt_i2c_r32(REG_STATUS_REG) & I2C_SDOEMPTY));
 +
-+      return (retries < 0) ? -ETIMEDOUT : 0;
++      return 0;
 +}
 +
 +static int rt_i2c_handle_msg(struct i2c_adapter *a, struct i2c_msg* msg)
@@ -199,10 +210,8 @@ Signed-off-by: John Crispin <blogic@openwrt.org>
 +
 +      if (msg->flags & I2C_M_RD) {
 +              for (i = 0; i < nblock; i++) {
-+                      if (rt_i2c_wait_idle()) {
-+                          printk("i2c-read line busy\n");
++                      if (rt_i2c_wait_idle())
 +                              return -ETIMEDOUT;
-+                      }
 +                      rt_i2c_w32(READ_BLOCK - 1, REG_BYTECNT_REG);
 +                      rt_i2c_w32(READ_CMD, REG_STARTXFR_REG);
 +                      for (j = 0; j < READ_BLOCK; j++) {
@@ -214,10 +223,8 @@ Signed-off-by: John Crispin <blogic@openwrt.org>
 +                      }
 +              }
 +
-+              if (rt_i2c_wait_idle()) {
-+                      printk("i2c-read line busy\n");
++              if (rt_i2c_wait_idle())
 +                      return -ETIMEDOUT;
-+              }
 +              if (rem) {
 +                      rt_i2c_w32(rem - 1, REG_BYTECNT_REG);
 +                      rt_i2c_w32(READ_CMD, REG_STARTXFR_REG);
@@ -231,10 +238,8 @@ Signed-off-by: John Crispin <blogic@openwrt.org>
 +                      msg->buf[pos++] = rt_i2c_r32(REG_DATAIN_REG);
 +              }
 +      } else {
-+              if (rt_i2c_wait_idle()) {
-+                      printk("i2c-write line busy\n");
++              if (rt_i2c_wait_idle())
 +                      return -ETIMEDOUT;
-+              }
 +              rt_i2c_w32(msg->len - 1, REG_BYTECNT_REG);
 +              for (i = 0; i < msg->len; i++) {
 +                      rt_i2c_w32(msg->buf[i], REG_DATAOUT_REG);
@@ -255,10 +260,8 @@ Signed-off-by: John Crispin <blogic@openwrt.org>
 +      int i = 0;
 +      int ret = 0;
 +
-+      if (rt_i2c_wait_idle()) {
-+              printk("i2c-master_xfer line busy\n");
++      if (rt_i2c_wait_idle())
 +              return -ETIMEDOUT;
-+      }
 +
 +      device_reset(a->dev.parent);
 +