create mode 100644 Documentation/devicetree/bindings/i2c/i2c-ralink.txt
create mode 100644 drivers/i2c/busses/i2c-ralink.c
---- /dev/null
-+++ b/Documentation/devicetree/bindings/i2c/i2c-ralink.txt
+Index: linux-3.14.18/Documentation/devicetree/bindings/i2c/i2c-ralink.txt
+===================================================================
+--- /dev/null 1970-01-01 00:00:00.000000000 +0000
++++ linux-3.14.18/Documentation/devicetree/bindings/i2c/i2c-ralink.txt 2014-10-06 14:28:14.296590159 +0200
@@ -0,0 +1,27 @@
+I2C for Ralink platforms
+
+ };
+ };
+};
---- a/drivers/i2c/busses/Kconfig
-+++ b/drivers/i2c/busses/Kconfig
-@@ -659,6 +659,10 @@ config I2C_RIIC
+Index: linux-3.14.18/drivers/i2c/busses/Kconfig
+===================================================================
+--- linux-3.14.18.orig/drivers/i2c/busses/Kconfig 2014-09-06 01:34:59.000000000 +0200
++++ linux-3.14.18/drivers/i2c/busses/Kconfig 2014-10-06 14:28:14.296590159 +0200
+@@ -659,6 +659,10 @@
This driver can also be built as a module. If so, the module
will be called i2c-riic.
config HAVE_S3C2410_I2C
bool
help
---- a/drivers/i2c/busses/Makefile
-+++ b/drivers/i2c/busses/Makefile
-@@ -63,6 +63,7 @@ obj-$(CONFIG_I2C_PNX) += i2c-pnx.o
+Index: linux-3.14.18/drivers/i2c/busses/Makefile
+===================================================================
+--- linux-3.14.18.orig/drivers/i2c/busses/Makefile 2014-09-06 01:34:59.000000000 +0200
++++ linux-3.14.18/drivers/i2c/busses/Makefile 2014-10-06 14:28:14.296590159 +0200
+@@ -63,6 +63,7 @@
obj-$(CONFIG_I2C_PUV3) += i2c-puv3.o
obj-$(CONFIG_I2C_PXA) += i2c-pxa.o
obj-$(CONFIG_I2C_PXA_PCI) += i2c-pxa-pci.o
obj-$(CONFIG_I2C_RIIC) += i2c-riic.o
obj-$(CONFIG_I2C_S3C2410) += i2c-s3c2410.o
obj-$(CONFIG_I2C_S6000) += i2c-s6000.o
---- /dev/null
-+++ b/drivers/i2c/busses/i2c-ralink.c
-@@ -0,0 +1,271 @@
+Index: linux-3.14.18/drivers/i2c/busses/i2c-ralink.c
+===================================================================
+--- /dev/null 1970-01-01 00:00:00.000000000 +0000
++++ linux-3.14.18/drivers/i2c/busses/i2c-ralink.c 2014-10-06 15:53:32.416500362 +0200
+@@ -0,0 +1,298 @@
+/*
+ * drivers/i2c/busses/i2c-ralink.c
+ *
+ * Copyright (C) 2013 Steven Liu <steven_liu@mediatek.com>
+ *
++ * Improve driver for i2cdetect from i2c-tools to detect i2c devices on the bus.
++ * (C) 2014 Sittisak <sittisaks@hotmail.com>
++ *
+ * This software is licensed under the terms of the GNU General Public
+ * License version 2, as published by the Free Software Foundation, and
+ * may be copied, distributed, and modified under those terms.
+#include <linux/platform_device.h>
+#include <linux/i2c.h>
+#include <linux/io.h>
++#include <linux/of_i2c.h>
+#include <linux/err.h>
+
+#include <asm/mach-ralink/ralink_regs.h>
+#define I2C_DEVADLEN_7 (6 << 2)
+#define I2C_ADDRDIS BIT(1)
+
-+#define I2C_RETRY 0x400
++#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).
+ return ioread32(membase + reg);
+}
+
++static inline int rt_i2c_get_ack(void)
++{
++ return (rt_i2c_r32(REG_STATUS_REG) & I2C_ACKERR) ? -EIO : 0;
++}
++
+static inline int rt_i2c_wait_rx_done(void)
+{
+ int retries = I2C_RETRY;
+ break;
+ } while(!(rt_i2c_r32(REG_STATUS_REG) & I2C_DATARDY));
+
-+ return (retries < 0);
++ return (retries < 0) ? -ETIMEDOUT : 0;
+}
+
+static inline int rt_i2c_wait_idle(void)
+ break;
+ } while(!(rt_i2c_r32(REG_STATUS_REG) & I2C_SDOEMPTY));
+
-+ return (retries < 0);
++ return (retries < 0) ? -ETIMEDOUT : 0;
+}
+
+static int rt_i2c_handle_msg(struct i2c_adapter *a, struct i2c_msg* msg)
+ int i = 0, j = 0, pos = 0;
+ int nblock = msg->len / READ_BLOCK;
+ int rem = msg->len % READ_BLOCK;
-+
++ int ret = 0;
++ /*printk("i2c handle msg len:%d\n",msg->len);*/
+ if (msg->flags & I2C_M_TEN) {
+ printk("10 bits addr not supported\n");
+ return -EINVAL;
+
+ if (msg->flags & I2C_M_RD) {
+ for (i = 0; i < nblock; i++) {
-+ rt_i2c_wait_idle();
++ if (rt_i2c_wait_idle()) {
++ printk("i2c-read line busy\n");
++ 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++) {
-+ if (rt_i2c_wait_rx_done())
-+ return -1;
++ if (rt_i2c_wait_rx_done()<0) ret = rt_i2c_wait_rx_done();
++ if (rt_i2c_get_ack()<0) ret = rt_i2c_get_ack();
+ msg->buf[pos++] = rt_i2c_r32(REG_DATAIN_REG);
+ }
+ }
+
-+ rt_i2c_wait_idle();
++ if (rt_i2c_wait_idle()) {
++ printk("i2c-read line busy\n");
++ return -ETIMEDOUT;
++ }
++ if (rem){
+ rt_i2c_w32(rem - 1, REG_BYTECNT_REG);
+ rt_i2c_w32(READ_CMD, REG_STARTXFR_REG);
++ }
+ for (i = 0; i < rem; i++) {
-+ if (rt_i2c_wait_rx_done())
-+ return -1;
++ if (rt_i2c_wait_rx_done()<0) ret = rt_i2c_wait_rx_done();
++ if (rt_i2c_get_ack() <0) ret = rt_i2c_get_ack();
++
+ msg->buf[pos++] = rt_i2c_r32(REG_DATAIN_REG);
+ }
+ } else {
-+ rt_i2c_wait_idle();
++ if (rt_i2c_wait_idle()) {
++ printk("i2c-write line busy\n");
++ 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);
+ rt_i2c_w32(WRITE_CMD, REG_STARTXFR_REG);
-+ if (rt_i2c_wait_tx_done())
-+ return -1;
++
++ if (rt_i2c_wait_tx_done()<0) ret = rt_i2c_wait_tx_done();
++ if (rt_i2c_get_ack()<0) ret = rt_i2c_get_ack();
++
++
+ }
+ }
+
-+ return 0;
++ return ret;
+}
+
+static int rt_i2c_master_xfer(struct i2c_adapter *a, struct i2c_msg *m, int n)
+ int ret = 0;
+
+ if (rt_i2c_wait_idle()) {
-+ printk("i2c transfer failed\n");
-+ return 0;
++ printk("i2c-master_xfer line busy\n");
++ return -ETIMEDOUT;
+ }
+
+ device_reset(a->dev.parent);
+ rt_i2c_w32(I2C_DEVADLEN_7 | I2C_ADDRDIS, REG_CONFIG_REG);
+ rt_i2c_w32(CLKDIV_VALUE, REG_CLKDIV_REG);
+
-+ for (i = 0; i < n && !ret; i++)
++ for (i = 0; i < n && !ret; i++) {
+ ret = rt_i2c_handle_msg(a, &m[i]);
+
-+ if (ret) {
-+ printk("i2c transfer failed\n");
-+ return 0;
++ if (ret < 0) {
++ return ret;
++ }
+ }
+
+ return n;
+ if (ret)
+ return ret;
+
++ of_i2c_register_devices(adapter);
++
+ platform_set_drvdata(pdev, adapter);
+
+ dev_info(&pdev->dev, "loaded\n");