diff mbox

[U-Boot,2/6,v3] i2c: designware_i2c: Add dw_i2c_enable() helper function

Message ID 1458719332-15215-1-git-send-email-sr@denx.de
State Accepted
Commit 1c8b089b45cddeef6fb76d9abb8d48601ec5e0a1
Delegated to: Heiko Schocher
Headers show

Commit Message

Stefan Roese March 23, 2016, 7:48 a.m. UTC
dw_i2c_enable() is used to dis-/en-able the I2C controller. It makes
sense to add such a function, as the controller is dis-/en-abled
multiple times in the code. Additionally, this function now checks,
if the controller is really dis-/en-abled. This code is copied
from the Linux I2C driver version.

Signed-off-by: Stefan Roese <sr@denx.de>
Cc: Simon Glass <sjg@chromium.org>
Cc: Bin Meng <bmeng.cn@gmail.com>
Cc: Marek Vasut <marex@denx.de>
Cc: Heiko Schocher <hs@denx.de>
---
v3:
- Rework dw_i2c_enable() as suggested by Marek

v2:
- Use true / false for binary values as suggested by Bin

 drivers/i2c/designware_i2c.c | 47 +++++++++++++++++++++++++-------------------
 1 file changed, 27 insertions(+), 20 deletions(-)

Comments

Bin Meng April 11, 2016, 4:33 a.m. UTC | #1
On Wed, Mar 23, 2016 at 3:48 PM, Stefan Roese <sr@denx.de> wrote:
> dw_i2c_enable() is used to dis-/en-able the I2C controller. It makes
> sense to add such a function, as the controller is dis-/en-abled
> multiple times in the code. Additionally, this function now checks,
> if the controller is really dis-/en-abled. This code is copied
> from the Linux I2C driver version.
>
> Signed-off-by: Stefan Roese <sr@denx.de>
> Cc: Simon Glass <sjg@chromium.org>
> Cc: Bin Meng <bmeng.cn@gmail.com>
> Cc: Marek Vasut <marex@denx.de>
> Cc: Heiko Schocher <hs@denx.de>
> ---
> v3:
> - Rework dw_i2c_enable() as suggested by Marek
>
> v2:
> - Use true / false for binary values as suggested by Bin
>
>  drivers/i2c/designware_i2c.c | 47 +++++++++++++++++++++++++-------------------
>  1 file changed, 27 insertions(+), 20 deletions(-)
>

Reviewed-by: Bin Meng <bmeng.cn@gmail.com>
diff mbox

Patch

diff --git a/drivers/i2c/designware_i2c.c b/drivers/i2c/designware_i2c.c
index e768cde..d5b3e29 100644
--- a/drivers/i2c/designware_i2c.c
+++ b/drivers/i2c/designware_i2c.c
@@ -34,6 +34,27 @@  static struct i2c_regs *i2c_get_base(struct i2c_adapter *adap)
 	return NULL;
 }
 
+static void dw_i2c_enable(struct i2c_regs *i2c_base, bool enable)
+{
+	u32 ena = enable ? IC_ENABLE_0B : 0;
+	int timeout = 100;
+
+	do {
+		writel(ena, &i2c_base->ic_enable);
+		if ((readl(&i2c_base->ic_enable_status) & IC_ENABLE_0B) == ena)
+			return;
+
+		/*
+		 * Wait 10 times the signaling period of the highest I2C
+		 * transfer supported by the driver (for 400KHz this is
+		 * 25us) as described in the DesignWare I2C databook.
+		 */
+		udelay(25);
+	} while (timeout--);
+
+	printf("timeout in %sabling I2C adapter\n", enable ? "en" : "dis");
+}
+
 /*
  * set_speed - Set the i2c speed mode (standard, high, fast)
  * @i2c_spd:	required i2c speed mode
@@ -45,12 +66,9 @@  static void set_speed(struct i2c_adapter *adap, int i2c_spd)
 	struct i2c_regs *i2c_base = i2c_get_base(adap);
 	unsigned int cntl;
 	unsigned int hcnt, lcnt;
-	unsigned int enbl;
 
 	/* to set speed cltr must be disabled */
-	enbl = readl(&i2c_base->ic_enable);
-	enbl &= ~IC_ENABLE_0B;
-	writel(enbl, &i2c_base->ic_enable);
+	dw_i2c_enable(i2c_base, false);
 
 	cntl = (readl(&i2c_base->ic_con) & (~IC_CON_SPD_MSK));
 
@@ -84,8 +102,7 @@  static void set_speed(struct i2c_adapter *adap, int i2c_spd)
 	writel(cntl, &i2c_base->ic_con);
 
 	/* Enable back i2c now speed set */
-	enbl |= IC_ENABLE_0B;
-	writel(enbl, &i2c_base->ic_enable);
+	dw_i2c_enable(i2c_base, true);
 }
 
 /*
@@ -123,12 +140,9 @@  static void dw_i2c_init(struct i2c_adapter *adap, int speed,
 			int slaveaddr)
 {
 	struct i2c_regs *i2c_base = i2c_get_base(adap);
-	unsigned int enbl;
 
 	/* Disable i2c */
-	enbl = readl(&i2c_base->ic_enable);
-	enbl &= ~IC_ENABLE_0B;
-	writel(enbl, &i2c_base->ic_enable);
+	dw_i2c_enable(i2c_base, false);
 
 	writel((IC_CON_SD | IC_CON_SPD_FS | IC_CON_MM), &i2c_base->ic_con);
 	writel(IC_RX_TL, &i2c_base->ic_rx_tl);
@@ -138,9 +152,7 @@  static void dw_i2c_init(struct i2c_adapter *adap, int speed,
 	writel(slaveaddr, &i2c_base->ic_sar);
 
 	/* Enable i2c */
-	enbl = readl(&i2c_base->ic_enable);
-	enbl |= IC_ENABLE_0B;
-	writel(enbl, &i2c_base->ic_enable);
+	dw_i2c_enable(i2c_base, true);
 }
 
 /*
@@ -152,19 +164,14 @@  static void dw_i2c_init(struct i2c_adapter *adap, int speed,
 static void i2c_setaddress(struct i2c_adapter *adap, unsigned int i2c_addr)
 {
 	struct i2c_regs *i2c_base = i2c_get_base(adap);
-	unsigned int enbl;
 
 	/* Disable i2c */
-	enbl = readl(&i2c_base->ic_enable);
-	enbl &= ~IC_ENABLE_0B;
-	writel(enbl, &i2c_base->ic_enable);
+	dw_i2c_enable(i2c_base, false);
 
 	writel(i2c_addr, &i2c_base->ic_tar);
 
 	/* Enable i2c */
-	enbl = readl(&i2c_base->ic_enable);
-	enbl |= IC_ENABLE_0B;
-	writel(enbl, &i2c_base->ic_enable);
+	dw_i2c_enable(i2c_base, true);
 }
 
 /*