ramips: add mt7621/3/8 support to the I2C driver
authorJohn Crispin <john@openwrt.org>
Fri, 2 Oct 2015 10:51:51 +0000 (10:51 +0000)
committerJohn Crispin <john@openwrt.org>
Fri, 2 Oct 2015 10:51:51 +0000 (10:51 +0000)
Signed-off-by: John Crispin <blogic@openwrt.org>
Backport of r46959

SVN-Revision: 47095

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

index 3064b17da577495cb4aac304d81726bd88d40b67..5618652dff71acab112638536fc3ac2599021cae 100644 (file)
@@ -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,327 @@
 +/*
 + * drivers/i2c/busses/i2c-ralink.c
 + *
@@ -97,6 +97,7 @@ Signed-off-by: John Crispin <blogic@openwrt.org>
 +#include <linux/init.h>
 +#include <linux/errno.h>
 +#include <linux/platform_device.h>
++#include <linux/of_platform.h>
 +#include <linux/i2c.h>
 +#include <linux/io.h>
 +#include <linux/err.h>
@@ -112,6 +113,8 @@ Signed-off-by: John Crispin <blogic@openwrt.org>
 +#define REG_STATUS_REG                0x18
 +#define REG_STARTXFR_REG      0x1C
 +#define REG_BYTECNT_REG               0x20
++#define REG_SM0CFG2           0x28
++#define REG_SM0CTL0           0x40
 +
 +#define I2C_STARTERR          BIT(4)
 +#define I2C_ACKERR            BIT(3)
@@ -122,8 +125,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,8 +132,23 @@ Signed-off-by: John Crispin <blogic@openwrt.org>
 +#define WRITE_CMD             0x00
 +#define READ_BLOCK              64
 +
++#define SM0CTL0_OD            BIT(31)
++#define SM0CTL0_VTRIG         BIT(28)
++#define SM0CTL0_OUTHI         BIT(6)
++#define SM0CTL0_STRETCH               BIT(1)
++#define SM0CTL0_DEFAULT               (SM0CTL0_OD | SM0CTL0_VTRIG | SM0CTL0_OUTHI | SM0CTL0_STRETCH)
++
++/* timeout waiting for I2C devices to respond (clock streching) */
++#define RT_I2C_TIMEOUT (msecs_to_jiffies(1000))
++
++enum {
++      I2C_TYPE_RALINK,
++      I2C_TYPE_MEDIATEK,
++};
++
 +static void __iomem *membase;
 +static struct i2c_adapter *adapter;
++static int hw_type;
 +
 +static void rt_i2c_w32(u32 val, unsigned reg)
 +{
@@ -151,38 +167,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);
++
++      } while (!(rt_i2c_r32(REG_STATUS_REG) & I2C_DATARDY));
 +
-+      return (retries < 0) ? -ETIMEDOUT : 0;
++      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 +225,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 +238,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 +253,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,16 +275,19 @@ 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);
 +
 +      rt_i2c_w32(m->addr, REG_DEVADDR_REG);
 +      rt_i2c_w32(I2C_DEVADLEN_7 | I2C_ADDRDIS, REG_CONFIG_REG);
-+      rt_i2c_w32(CLKDIV_VALUE, REG_CLKDIV_REG);
++      if (hw_type == I2C_TYPE_RALINK) {
++              rt_i2c_w32(CLKDIV_VALUE, REG_CLKDIV_REG);
++      } else {
++              rt_i2c_w32((CLKDIV_VALUE << 16) | SM0CTL0_DEFAULT, REG_SM0CTL0);
++              rt_i2c_w32(1, REG_SM0CFG2);
++      }
 +
 +      for (i = 0; i < n && !ret; i++) {
 +              ret = rt_i2c_handle_msg(a, &m[i]);
@@ -287,11 +310,23 @@ Signed-off-by: John Crispin <blogic@openwrt.org>
 +      .functionality  = rt_i2c_func,
 +};
 +
++static const struct of_device_id i2c_rt_dt_ids[] = {
++      { .compatible = "ralink,rt2880-i2c", .data = (void *) I2C_TYPE_RALINK },
++      { .compatible = "mediatek,mt7628-i2c", .data = (void *) I2C_TYPE_MEDIATEK },
++      { /* sentinel */ }
++};
++
++MODULE_DEVICE_TABLE(of, i2c_rt_dt_ids);
++
 +static int rt_i2c_probe(struct platform_device *pdev)
 +{
 +      struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
++      const struct of_device_id *match;
 +      int ret;
 +
++      match = of_match_device(i2c_rt_dt_ids, &pdev->dev);
++      hw_type = (int) match->data;
++
 +      if (!res) {
 +              dev_err(&pdev->dev, "no memory resource found\n");
 +              return -ENODEV;
@@ -334,13 +369,6 @@ Signed-off-by: John Crispin <blogic@openwrt.org>
 +      return 0;
 +}
 +
-+static const struct of_device_id i2c_rt_dt_ids[] = {
-+      { .compatible = "ralink,rt2880-i2c", },
-+      { /* sentinel */ }
-+};
-+
-+MODULE_DEVICE_TABLE(of, i2c_rt_dt_ids);
-+
 +static struct platform_driver rt_i2c_driver = {
 +      .probe          = rt_i2c_probe,
 +      .remove         = rt_i2c_remove,