i2c: imx: check busy bit when START/STOP
PATCH 1/1 i2c: imx: check busy bit when START/STOP
After START, wait busy bit to be set and
after STOP, wait busy bit to be clear.
Disable clock when it's possible to save power.
--- drivers/i2c/busses/i2c-imx.c | 116 ++++++++++++++++++++++++++++++++---------- 1 files changed, 88 insertions(+), 28 deletions(-)
drivers/i2c/busses/i2c-imx.c +1 -0
@@ -120,19 +120,40 @@ struct imx_i2c_struct {wait_queue_head_t queue;unsigned long i2csr;unsigned int disable_delay; +unsigned int ifdr;/* IMX_I2C_IFDR */ };/** Functions for IMX I2C adapter driver
***************************************
*******************************************************************************/
-static int i2c_imx_bus_busy(struct imx_i2c_struct *i2c_imx) +#ifdef CONFIG_I2C_DEBUG_BUS
+#define reg_dump(i2c_imx) \
+{ \
+ printk(KERN_DEBUG "fun %s:%d ", __func__, __LINE__); \
+ printk(KERN_DEBUG "IADR %02x IFDR %02x I2CR %02x I2SR %02x\n", \
+ readb(i2c_imx->base + IMX_I2C_IADR), \
+ readb(i2c_imx->base + IMX_I2C_IFDR), \
+ readb(i2c_imx->base + IMX_I2C_I2CR), \
+ readb(i2c_imx->base + IMX_I2C_I2SR)); \
+}
+#else
+#define reg_dump(i2c_imx)
+#endif
+
+static int i2c_imx_bus_busy(struct imx_i2c_struct *i2c_imx, int for_busy)
{
unsigned long orig_jiffies = jiffies;
+ unsigned int temp;
dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__);
/* wait for bus not busy */
- while (readb(i2c_imx->base + IMX_I2C_I2SR) & I2SR_IBB) {
+ temp = readb(i2c_imx->base + IMX_I2C_I2SR);
+ while (1) {
+ if (for_busy && (temp & I2SR_IBB))
+ break;
+ if (!for_busy && !(temp & I2SR_IBB))
+ break;
if (signal_pending(current)) {
dev_dbg(&i2c_imx->adapter.dev,
"<%s> I2C Interrupted\n", __func__);
@@ -141,9 +162,11 @@ static int i2c_imx_bus_busy(struct imx_i2c_struct *i2c_imx)
if (time_after(jiffies, orig_jiffies + HZ / 1000)) {
dev_dbg(&i2c_imx->adapter.dev,
"<%s> I2C bus is busy\n", __func__);
+ reg_dump(i2c_imx);
return -EIO;
}
schedule();
+ temp = readb(i2c_imx->base + IMX_I2C_I2SR);
}
return 0;
@@ -158,9 +181,11 @@ static int i2c_imx_trx_complete(struct imx_i2c_struct *i2c_imx)
if (unlikely(result < 0)) {
dev_dbg(&i2c_imx->adapter.dev, "<%s> result < 0\n", __func__);
+ reg_dump(i2c_imx);
return result;
} else if (unlikely(!(i2c_imx->i2csr & I2SR_IIF))) {
dev_dbg(&i2c_imx->adapter.dev, "<%s> Timeout\n", __func__);
+ reg_dump(i2c_imx);
return -ETIMEDOUT;
}
dev_dbg(&i2c_imx->adapter.dev, "<%s> TRX complete\n", __func__);
@@ -172,6 +197,7 @@ static int i2c_imx_acked(struct imx_i2c_struct *i2c_imx)
{
if (readb(i2c_imx->base + IMX_I2C_I2SR) & I2SR_RXAK) {
dev_dbg(&i2c_imx->adapter.dev, "<%s> No ACK\n", __func__);
+ reg_dump(i2c_imx);
return -EIO; /* No ACK */
}
@@ -179,20 +205,37 @@ static int i2c_imx_acked(struct imx_i2c_struct *i2c_imx)
return 0;
}
-static void i2c_imx_start(struct imx_i2c_struct *i2c_imx) +static int i2c_imx_start(struct imx_i2c_struct *i2c_imx)
{
unsigned int temp = 0;
+ int result;
dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__);
+ clk_enable(i2c_imx->clk);
+ writeb(i2c_imx->ifdr, i2c_imx->base + IMX_I2C_IFDR);
/* Enable I2C controller */
+ writeb(0, i2c_imx->base + IMX_I2C_I2SR);
writeb(I2CR_IEN, i2c_imx->base + IMX_I2C_I2CR);
+
+ result = i2c_imx_bus_busy(i2c_imx, 0);
+ if (result) {
+ reg_dump(i2c_imx);
+ return result;
+ }
+
/* Start I2C transaction */
temp = readb(i2c_imx->base + IMX_I2C_I2CR); temp |= I2CR_MSTA;
writeb(temp, i2c_imx->base + IMX_I2C_I2CR);
+ result = i2c_imx_bus_busy(i2c_imx, 1);
+ if (result) {
+ reg_dump(i2c_imx);
+ return result;
+ }
temp |= I2CR_IIEN | I2CR_MTX | I2CR_TXAK; writeb(temp, i2c_imx->base + IMX_I2C_I2CR);
+ return result;
}
static void i2c_imx_stop(struct imx_i2c_struct *i2c_imx)
@@ -202,18 +245,21 @@ static void i2c_imx_stop(struct imx_i2c_struct *i2c_imx)
/* Stop I2C transaction */
dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__);
temp = readb(i2c_imx->base + IMX_I2C_I2CR);
- temp &= ~I2CR_MSTA;
+ temp &= ~(I2CR_MSTA | I2CR_MTX);
writeb(temp, i2c_imx->base + IMX_I2C_I2CR);
- /* setup chip registers to defaults */
- writeb(I2CR_IEN, i2c_imx->base + IMX_I2C_I2CR);
- writeb(0, i2c_imx->base + IMX_I2C_I2SR);
/*
* This delay caused by an i.MXL hardware bug.
* If no (or too short) delay, no "STOP" bit will be generated. */
udelay(i2c_imx->disable_delay);
+
+
+ if (i2c_imx_bus_busy(i2c_imx, 0))
+ reg_dump(i2c_imx);
+
/* Disable I2C controller */
writeb(0, i2c_imx->base + IMX_I2C_I2CR);
+ clk_disable(i2c_imx->clk);
}
static void __init i2c_imx_set_clk(struct imx_i2c_struct *i2c_imx,
@@ -233,17 +279,19 @@ static void __init i2c_imx_set_clk(struct imx_i2c_struct *i2c_imx,
else
for (i = 0; i2c_clk_div[i][0] < div; i++);
- /* Write divider value to register */
- writeb(i2c_clk_div[i][1], i2c_imx->base + IMX_I2C_IFDR);
-
- /*
- * There dummy delay is calculated.
- * It should be about one I2C clock period long.
- * This delay is used in I2C bus disable function
- * to fix chip hardware bug.
- */
- i2c_imx->disable_delay = (500000U * i2c_clk_div[i][0]
- + (i2c_clk_rate / 2) - 1) / (i2c_clk_rate / 2);
+ /* Store divider value */
+ i2c_imx->ifdr = i2c_clk_div[i][1];
+
+ if (cpu_is_mx1()) {
+ /*
+ * There dummy delay is calculated.
+ * It should be about one I2C clock period long.
+ * This delay is used in I2C bus disable function
+ * to fix chip hardware bug.
+ */
+ i2c_imx->disable_delay = (500000U * i2c_clk_div[i][0]
+ + (i2c_clk_rate / 2) - 1) / (i2c_clk_rate / 2);
+ }
/* dev_dbg() can't be used, because adapter is not yet registered */
#ifdef CONFIG_I2C_DEBUG_BUS
@@ -344,7 +392,7 @@ static int i2c_imx_read(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs)
dev_dbg(&i2c_imx->adapter.dev,
"<%s> clear MSTA\n", __func__);
temp = readb(i2c_imx->base + IMX_I2C_I2CR);
- temp &= ~I2CR_MSTA;
+ temp &= ~(I2CR_MSTA | I2CR_MTX);
writeb(temp, i2c_imx->base + IMX_I2C_I2CR);
} else if (i == (msgs->len - 2)) {
dev_dbg(&i2c_imx->adapter.dev,
@@ -369,14 +417,24 @@ static int i2c_imx_xfer(struct i2c_adapter *adapter,
struct imx_i2c_struct *i2c_imx = i2c_get_adapdata(adapter);
dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__);
-
+#ifdef CONFIG_I2C_DEBUG_BUS
+ for (i = 0; i < num; i++) {
+ printk(KERN_DEBUG "msg%d addr %02x RD %d cnt %d d:", i,
+ msgs[i].addr, msgs[i].flags & I2C_M_RD, msgs[i].len);
+ if (!(msgs[i].flags & I2C_M_RD)) {
+ int j;
+ for (j = 0; j < msgs[i].len; j++)
+ printk("%02x ", msgs[i].buf[j]);
+ }
+ printk("\n");
+ }
+#endif
/* Check if i2c bus is not busy */
- result = i2c_imx_bus_busy(i2c_imx);
- if (result)
- goto fail0;
/* Start I2C transfer */
- i2c_imx_start(i2c_imx);
+ result = i2c_imx_start(i2c_imx);
+ if (result)
+ goto fail0;
/* read/write data */
for (i = 0; i < num; i++) {
@@ -386,6 +444,11 @@ static int i2c_imx_xfer(struct i2c_adapter *adapter,
temp = readb(i2c_imx->base + IMX_I2C_I2CR); temp |= I2CR_RSTA;
writeb(temp, i2c_imx->base + IMX_I2C_I2CR);
+ result = i2c_imx_bus_busy(i2c_imx, 1);
+ if (result) {
+ reg_dump(i2c_imx);
+ goto fail0;
+ }
}
dev_dbg(&i2c_imx->adapter.dev,
"<%s> transfer message: %d\n", __func__, i);
@@ -442,7 +505,7 @@ static int __init i2c_imx_probe(struct platform_device *pdev)
int irq;
int ret;
- dev_dbg(&pdev->dev, "<%s>\n", __func__);
+ dev_info(&pdev->dev, "<%s>\n", __func__);
res = platform_get_resource(pdev, IORESOURCE_MEM, 0); if (!res) {
@@ -500,7 +563,6 @@ static int __init i2c_imx_probe(struct platform_device *pdev)
dev_err(&pdev->dev, "can't get I2C clock\n"); goto fail3;
}
- clk_enable(i2c_imx->clk);
/* Request IRQ */
ret = request_irq(i2c_imx->irq, i2c_imx_isr, 0, pdev->name, i2c_imx);
@@ -549,7 +611,6 @@ static int __init i2c_imx_probe(struct platform_device *pdev)
fail5:
free_irq(i2c_imx->irq, i2c_imx);
fail4:
- clk_disable(i2c_imx->clk);
clk_put(i2c_imx->clk);
fail3:
release_mem_region(i2c_imx->res->start, resource_size(res));
@@ -587,7 +648,6 @@ static int __exit i2c_imx_remove(struct platform_device *pdev)
pdata->exit(&pdev->dev);
/* Disable I2C clock */
- clk_disable(i2c_imx->clk);
clk_put(i2c_imx->clk);
release_mem_region(i2c_imx->res->start, resource_size(i2c_imx->res));
Signature · 1 line
1.6.0.4
PATCH 1/1 i2c: imx: check busy bit when START/STOP
Hi Richard,
Quoted 4 lines · Richard Zhao
After START, wait busy bit to be set and
after STOP, wait busy bit to be clear.Disable clock when it's possible to save power.
As those are two different issues, could you make two patches out of it? Also, I think it will make reviewing easier if you'd drop the debug print-outs.
Regards,
Wolfram
Signature · 9 lines
Pengutronix e.K. | Wolfram Sang | Industrial Linux Solutions | http://www.pengutronix.de/ | -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 197 bytes Desc: Digital signature URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20090929/1bb44bad/attachment.sig>
Sascha Hauer
maintainerPATCH 1/1 i2c: imx: check busy bit when START/STOP
In reply to Richard ZhaoHi Richard,
Quoted 30 lines · Richard Zhao
After START, wait busy bit to be set and
after STOP, wait busy bit to be clear.Disable clock when it's possible to save power.
Signed-off-by Richard Zhao <[email protected]>--- drivers/i2c/busses/i2c-imx.c | 116 ++++++++++++++++++++++++++++++++---------- 1 files changed, 88 insertions(+), 28 deletions(-)drivers/i2c/busses/i2c-imx.c +5
-1@@ -120,19 +120,40 @@ struct imx_i2c_struct {wait_queue_head_t queue;unsigned long i2csr;unsigned int disable_delay; +unsigned int ifdr;/* IMX_I2C_IFDR */ };/** Functions for IMX I2C adapter driver *************************************** *******************************************************************************/ -static int i2c_imx_bus_busy (struct imx_i2c_struct *i2c_imx) +#ifdef CONFIG_I2C_DEBUG_BUS +#define reg_dump (i2c_imx) \ +{ \ + printk(KERN_DEBUG "fun %s:%d ", __func__, __LINE__); \
drivers/i2c/busses/i2c-imx.c struct imx_i2c_struct { · line 143
Quoted 10 lines
drivers/i2c/busses/i2c-imx.c +10
-0+printk (KERN_DEBUG "IADR %02x IFDR %02x I2CR %02x I2SR %02x\n" , \ +readb (i2c_imx->base +IMX_I2C_IADR ), \ +readb (i2c_imx->base +IMX_I2C_IFDR ), \ +readb (i2c_imx->base +IMX_I2C_I2CR ), \ + readb(i2c_imx->base + IMX_I2C_I2SR)); \ +} +#else +#define reg_dump (i2c_imx) +#endif +
Can you please remove this reg_dump? If we really need this it should be in an extra patch and not clutter this one.
drivers/i2c/busses/i2c-imx.c struct imx_i2c_struct { · line 151
Quoted 8 lines
drivers/i2c/busses/i2c-imx.c +2
-0+static int i2c_imx_bus_busy (struct imx_i2c_struct *i2c_imx,int for_busy) {unsigned long orig_jiffies = jiffies; +unsigned int temp;dev_dbg (&i2c_imx->adapter.dev,"<%s>\n" , __func__);/* wait for bus not busy */
This comment seems wrong now. This function waits for busy or not busy depending on for_busy.
Quoted 83 lines
drivers/i2c/busses/i2c-imx.c +6
-1-while (readb (i2c_imx->base +IMX_I2C_I2SR ) &I2SR_IBB ) { + temp =readb (i2c_imx->base +IMX_I2C_I2SR ); +while (1 ) { +if (for_busy && (temp &I2SR_IBB )) +break ; +if (!for_busy && !(temp &I2SR_IBB )) +break ;if (signal_pending (current)) { dev_dbg(&i2c_imx->adapter.dev,drivers/i2c/busses/i2c-imx.c +4
-0"<%s> I2C Interrupted\n", __func__);@@ -141,9 +162,11 @@ static int i2c_imx_bus_busy(struct imx_i2c_struct *i2c_imx)if (time_after (jiffies, orig_jiffies +HZ /1000 )) {dev_dbg (&i2c_imx->adapter.dev,"<%s> I2C bus is busy\n" , __func__); +reg_dump (i2c_imx);return -EIO ; }schedule (); + temp =readb (i2c_imx->base +IMX_I2C_I2SR ); }return 0 ;@@ -158,9 +181,11 @@ static int i2c_imx_trx_complete(struct imx_i2c_struct *i2c_imx)if (unlikely (result <0 )) {dev_dbg (&i2c_imx->adapter.dev,"<%s> result < 0\n" , __func__); +reg_dump (i2c_imx);return result; }else if (unlikely (!(i2c_imx->i2csr &I2SR_IIF ))) {dev_dbg (&i2c_imx->adapter.dev,"<%s> Timeout\n" , __func__); +reg_dump (i2c_imx);return -ETIMEDOUT ; }drivers/i2c/busses/i2c-imx.c +19
-1dev_dbg (&i2c_imx->adapter.dev,"<%s> TRX complete\n" , __func__);@@ -172,6 +197,7 @@ static int i2c_imx_acked(struct imx_i2c_struct *i2c_imx) {if (readb (i2c_imx->base +IMX_I2C_I2SR ) &I2SR_RXAK ) {dev_dbg (&i2c_imx->adapter.dev,"<%s> No ACK\n" , __func__); +reg_dump (i2c_imx);return -EIO ;/* No ACK */ }@@ -179,20 +205,37 @@ static int i2c_imx_acked(struct imx_i2c_struct *i2c_imx)return 0 ; } -static void i2c_imx_start (struct imx_i2c_struct *i2c_imx) +static int i2c_imx_start (struct imx_i2c_struct *i2c_imx) {unsigned int temp =0 ; +int result;dev_dbg (&i2c_imx->adapter.dev,"<%s>\n" , __func__); +clk_enable (i2c_imx->clk); +writeb (i2c_imx->ifdr, i2c_imx->base +IMX_I2C_IFDR );/* Enable I2C controller */ +writeb (0 , i2c_imx->base +IMX_I2C_I2SR );writeb (I2CR_IEN , i2c_imx->base +IMX_I2C_I2CR ); + + result =i2c_imx_bus_busy (i2c_imx,0 ); +if (result) { +reg_dump (i2c_imx); +return result; + } +/* Start I2C transaction */ temp =readb (i2c_imx->base +IMX_I2C_I2CR ); temp |=I2CR_MSTA ;writeb (temp, i2c_imx->base +IMX_I2C_I2CR ); + result =i2c_imx_bus_busy (i2c_imx,1 ); +if (result) { +reg_dump (i2c_imx); +return result; + } temp |=I2CR_IIEN |I2CR_MTX |I2CR_TXAK ;writeb (temp, i2c_imx->base +IMX_I2C_I2CR ); +return result; }static void i2c_imx_stop (struct imx_i2c_struct *i2c_imx)
drivers/i2c/busses/i2c-imx.c static void i2c_imx_stop(struct imx_i2c_struct *i2c_imx) · line 248
Quoted 5 lines
drivers/i2c/busses/i2c-imx.c +1
-1@@ -202,18 +245,21 @@ static void i2c_imx_stop(struct imx_i2c_struct *i2c_imx)/* Stop I2C transaction */ dev_dbg (&i2c_imx->adapter.dev,"<%s>\n" , __func__); temp =readb (i2c_imx->base +IMX_I2C_I2CR ); - temp &= ~I2CR_MSTA ; + temp &= ~(I2CR_MSTA |I2CR_MTX );
This change seems unrelated. Is it necessary?
drivers/i2c/busses/i2c-imx.c static void i2c_imx_stop(struct imx_i2c_struct *i2c_imx) · line 256
Quoted 11 lines
drivers/i2c/busses/i2c-imx.c +2
-3writeb (temp, i2c_imx->base +IMX_I2C_I2CR ); -/* setup chip registers to defaults */ -writeb (I2CR_IEN , i2c_imx->base +IMX_I2C_I2CR ); -writeb (0 , i2c_imx->base +IMX_I2C_I2SR );/* * This delay caused by an i.MXL hardware bug. * If no (or too short) delay, no "STOP" bit will be generated. */ udelay (i2c_imx->disable_delay); + +
double blank line
Quoted 10 lines
drivers/i2c/busses/i2c-imx.c +4
-0+if (i2c_imx_bus_busy (i2c_imx,0 )) +reg_dump (i2c_imx); +/* Disable I2C controller */ writeb (0 , i2c_imx->base +IMX_I2C_I2CR ); +clk_disable (i2c_imx->clk); } static void __init i2c_imx_set_clk(struct imx_i2c_struct *i2c_imx,
drivers/i2c/busses/i2c-imx.c static void __init i2c_imx_set_clk(struct · line 238
Quoted 6 lines
drivers/i2c/busses/i2c-imx.c +0
-2@@ -233,17 +279,19 @@ static void __init i2c_imx_set_clk(structimx_i2c_struct *i2c_imx, else for (i = 0; i2c_clk_div[i][0] < div; i++); -/* Write divider value to register */ -writeb (i2c_clk_div[i][1 ], i2c_imx->base +IMX_I2C_IFDR );
Why can't we write this value directly anymore...
drivers/i2c/busses/i2c-imx.c static void __init i2c_imx_set_clk(struct · line 284
Quoted 11 lines
drivers/i2c/busses/i2c-imx.c +2
-9- -/* -* There dummy delay is calculated. -* It should be about one I2C clock period long. -* This delay is used in I2C bus disable function -* to fix chip hardware bug. -*/ - i2c_imx->disable_delay = (500000U * i2c_clk_div[i][0 ] - + (i2c_clk_rate /2 ) -1 ) / (i2c_clk_rate /2 ); +/* Store divider value */ + i2c_imx->ifdr = i2c_clk_div[i][1 ];
...but have to store it in a variable instead?
Quoted 35 lines
drivers/i2c/busses/i2c-imx.c +11
-0+ +if (cpu_is_mx1 ()) { +/* +* There dummy delay is calculated. +* It should be about one I2C clock period long. +* This delay is used in I2C bus disable function +* to fix chip hardware bug. +*/ + i2c_imx->disable_delay = (500000U * i2c_clk_div[i][0 ] + + (i2c_clk_rate /2 ) -1 ) / (i2c_clk_rate /2 ); + }/* dev_dbg() can't be used, because adapter is not yet registered */ #ifdef
CONFIG_I2C_DEBUG_BUSdrivers/i2c/busses/i2c-imx.c +1
-1@@ -344,7 +392,7 @@ static int i2c_imx_read(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs)dev_dbg (&i2c_imx->adapter.dev,"<%s> clear MSTA\n" , __func__); temp =readb (i2c_imx->base +IMX_I2C_I2CR ); - temp &= ~I2CR_MSTA ; + temp &= ~(I2CR_MSTA |I2CR_MTX );writeb (temp, i2c_imx->base +IMX_I2C_I2CR ); }else if (i == (msgs->len -2 )) {drivers/i2c/busses/i2c-imx.c +6
-1dev_dbg(&i2c_imx->adapter.dev,@@ -369,14 +417,24 @@ static int i2c_imx_xfer(struct i2c_adapter *adapter,struct imx_i2c_struct *i2c_imx =i2c_get_adapdata (adapter);dev_dbg (&i2c_imx->adapter.dev,"<%s>\n" , __func__); - +#ifdef CONFIG_I2C_DEBUG_BUS +for (i =0 ; i < num; i++) { +printk (KERN_DEBUG "msg%d addr %02x RD %d cnt %d d:" , i, + msgs[i].addr, msgs[i].flags &I2C_M_RD , msgs[i].len); +if (!(msgs[i].flags &I2C_M_RD )) { +int j;
drivers/i2c/busses/i2c-imx.c static int i2c_imx_xfer(struct i2c_adapter *adapter, · line 376
Quoted 10 lines
drivers/i2c/busses/i2c-imx.c +6
-3+for (j =0 ; j < msgs[i].len; j++) + printk("%02x ", msgs[i].buf[j]); + } +printk ("\n" ); + } +#endif /* Check if i2c bus is not busy */ - result =i2c_imx_bus_busy (i2c_imx); -if (result) - goto fail0;
When removing the code you should also remove the comment.
Quoted 22 lines
drivers/i2c/busses/i2c-imx.c +8
-1/* Start I2C transfer */ -i2c_imx_start (i2c_imx); + result =i2c_imx_start (i2c_imx); +if (result) + goto fail0;/* read/write data */ for (i =0 ; i < num; i++) {@@ -386,6 +444,11 @@ static int i2c_imx_xfer(struct i2c_adapter *adapter, temp =readb (i2c_imx->base +IMX_I2C_I2CR ); temp |=I2CR_RSTA ;writeb (temp, i2c_imx->base +IMX_I2C_I2CR ); + result =i2c_imx_bus_busy (i2c_imx,1 ); +if (result) { +reg_dump (i2c_imx); + goto fail0; + } }dev_dbg (&i2c_imx->adapter.dev,"<%s> transfer message: %d\n" , __func__, i);
drivers/i2c/busses/i2c-imx.c static int __init i2c_imx_probe(struct · line 509
Quoted 6 lines
drivers/i2c/busses/i2c-imx.c +1
-1@@ -442,7 +505,7 @@ static int __init i2c_imx_probe(struct platform_device *pdev)int irq;int ret; -dev_dbg (&pdev->dev,"<%s>\n" , __func__); +dev_info (&pdev->dev,"<%s>\n" , __func__);
no
drivers/i2c/busses/i2c-imx.c static int __init i2c_imx_probe(struct · line 504
Quoted 9 lines
drivers/i2c/busses/i2c-imx.c +0
-1res =platform_get_resource (pdev,IORESOURCE_MEM ,0 );if (!res) {@@ -500,7 +563,6 @@ static int __init i2c_imx_probe(struct platform_device *pdev) dev_err(&pdev->dev, "can't get I2C clock\n"); goto fail3; } -clk_enable (i2c_imx->clk);
I think it's generally a good idea to start/stop the clocks when needed as you do here. It should be an extra patch though.
Quoted 24 lines
drivers/i2c/busses/i2c-imx.c +0
-1/* Request IRQ */ ret =request_irq (i2c_imx->irq, i2c_imx_isr,0 , pdev->name, i2c_imx);@@ -549,7 +611,6 @@ static int __init i2c_imx_probe(struct platform_device *pdev) fail5: free_irq(i2c_imx->irq, i2c_imx); fail4: -clk_disable (i2c_imx->clk);clk_put (i2c_imx->clk); fail3:drivers/i2c/busses/i2c-imx.c +0
-1release_mem_region (i2c_imx->res->start,resource_size (res));@@ -587,7 +648,6 @@ static int __exit i2c_imx_remove(struct platform_device *pdev) pdata->exit(&pdev->dev);/* Disable I2C clock */ -clk_disable (i2c_imx->clk);clk_put (i2c_imx->clk);
release_mem_region(i2c_imx->res->start, resource_size(i2c_imx->res));Signature · 1 line
1.6.0.4
Signature · 4 lines
Pengutronix e.K. | | Industrial Linux Solutions | http://www.pengutronix.de/ | Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
PATCH 1/1 i2c: imx: check busy bit when START/STOP
Thanks, Sacha. See comments inline.
Quoted 39 lines · Sascha Hauer
Hi Richard,
Quoted 35 lines · Richard Zhao
After START, wait busy bit to be set and
after STOP, wait busy bit to be clear.Disable clock when it's possible to save power.
Signed-off-by Richard Zhao <[email protected]>---
?drivers/i2c/busses/i2c-imx.c | ?116 ++++++++++++++++++++++++++++++++---------- ?1 files changed, 88 insertions(+), 28 deletions(-)drivers/i2c/busses/i2c-imx.c +1
-0@@ -120,19 +120,40 @@ struct imx_i2c_struct { ? ? ? wait_queue_head_t ? ? ? queue; ? ? ? unsigned long ? ? ? ? ? i2csr; ? ? ? unsigned int ? ? ? ? ? ?disable_delay; + ? ? unsigned int ? ? ? ? ? ?ifdr; /* IMX_I2C_IFDR */ ?};?/** Functions for IMX I2C adapter driver
***************************************
?*******************************************************************************/drivers/i2c/busses/i2c-imx.c +9
-1-static int i2c_imx_bus_busy (struct imx_i2c_struct *i2c_imx) +#ifdef CONFIG_I2C_DEBUG_BUS +#define reg_dump (i2c_imx) \ +{ \ + ? ? printk(KERN_DEBUG "fun %s:%d ", __func__, __LINE__); \ + ? ? printk(KERN_DEBUG "IADR %02x IFDR %02x I2CR %02x I2SR %02x\n", \ + ? ? ? ? ? ? readb(i2c_imx->base + IMX_I2C_IADR), \ + ? ? ? ? ? ? readb(i2c_imx->base + IMX_I2C_IFDR), \ + ? ? ? ? ? ? readb(i2c_imx->base + IMX_I2C_I2CR), \ + ? ? ? ? ? ? readb(i2c_imx->base + IMX_I2C_I2SR)); \
Quoted 8 lines
drivers/i2c/busses/i2c-imx.c
Quoted 5 lines
drivers/i2c/busses/i2c-imx.c +5
-0+} +#else +#define reg_dump (i2c_imx) +#endif +Can you please remove this
reg_dump? If we really need this it should be in an extra patch and not clutter this one.
I think it's needed. It helps much to debug and don't effect performance by default.
Quoted 6 lines
Quoted 6 lines
+static int
i2c_imx_bus_busy(struct imx_i2c_struct *i2c_imx, int for_busy)?{
? ? ? unsigned longorig_jiffies= jiffies;
+ ? ? unsigned int temp;
Quoted 6 lines
Quoted 3 lines
? ? ?
dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__);? ? ? /* wait for bus not busy */
This comment seems wrong now. This function waits for busy or not busy depending on
for_busy.
Right. Thanks
Quoted 82 lines
Quoted 82 lines
- ? ? while (
readb(i2c_imx->base + IMX_I2C_I2SR)&I2SR_IBB) {
+ ? ? temp =readb(i2c_imx->base + IMX_I2C_I2SR);
+ ? ? while (1) {
+ ? ? ? ? ? ? if (for_busy&& (temp &I2SR_IBB))
+ ? ? ? ? ? ? ? ? ? ? break;
+ ? ? ? ? ? ? if (!for_busy && !(temp &I2SR_IBB))
+ ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? if (signal_pending(current)) {
? ? ? ? ? ? ? ? ? ? ? dev_dbg(&i2c_imx->adapter.dev,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "<%s> I2C Interrupted\n",__func__);drivers/i2c/busses/i2c-imx.c +4
-0@@ -141,9 +162,11 @@ static int i2c_imx_bus_busy(struct imx_i2c_struct *i2c_imx) ? ? ? ? ? ? ? if (time_after(jiffies, orig_jiffies + HZ / 1000)) { ? ? ? ? ? ? ? ? ? ? ? dev_dbg(&i2c_imx->adapter.dev, ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "<%s> I2C bus is busy\n", __func__); + ? ? ? ? ? ? ? ? ? ? reg_dump(i2c_imx); ? ? ? ? ? ? ? ? ? ? ? return -EIO; ? ? ? ? ? ? ? } ? ? ? ? ? ? ? schedule(); + ? ? ? ? ? ? temp = readb(i2c_imx->base + IMX_I2C_I2SR); ? ? ? } ? ? ? return 0;@@ -158,9 +181,11 @@ static int i2c_imx_trx_complete(struct imx_i2c_struct *i2c_imx) ? ? ? if (unlikely(result < 0)) { ? ? ? ? ? ? ? dev_dbg(&i2c_imx->adapter.dev, "<%s> result < 0\n", __func__); + ? ? ? ? ? ? reg_dump(i2c_imx); ? ? ? ? ? ? ? return result; ? ? ? } else if (unlikely(!(i2c_imx->i2csr & I2SR_IIF))) { ? ? ? ? ? ? ? dev_dbg(&i2c_imx->adapter.dev, "<%s> Timeout\n", __func__); + ? ? ? ? ? ? reg_dump(i2c_imx); ? ? ? ? ? ? ? return -ETIMEDOUT; ? ? ? }? ? ?
dev_dbg(&i2c_imx->adapter.dev, "<%s> TRX complete\n", __func__);drivers/i2c/busses/i2c-imx.c +1
-0@@ -172,6 +197,7 @@ static int i2c_imx_acked(struct imx_i2c_struct *i2c_imx) ?{ ? ? ? if (readb(i2c_imx->base + IMX_I2C_I2SR) & I2SR_RXAK) { ? ? ? ? ? ? ? dev_dbg(&i2c_imx->adapter.dev, "<%s> No ACK\n", __func__); + ? ? ? ? ? ? reg_dump(i2c_imx);? ? ? ? ? ? ? return
-EIO; ?/* No ACK */
? ? ? }drivers/i2c/busses/i2c-imx.c +4
-1@@ -179,20 +205,37 @@ static int i2c_imx_acked(struct imx_i2c_struct *i2c_imx) ? ? ? return 0; ?} -static void i2c_imx_start (struct imx_i2c_struct *i2c_imx) +static int i2c_imx_start (struct imx_i2c_struct *i2c_imx) ?{ ? ? ? unsigned int temp = 0; + ? ? int result; ? ? ? dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__); + ? ? clk_enable(i2c_imx->clk); + ? ? writeb(i2c_imx->ifdr, i2c_imx->base + IMX_I2C_IFDR);? ? ? /* Enable I2C controller */
+ ? ?writeb(0, i2c_imx->base + IMX_I2C_I2SR); ? ? ?writeb(I2CR_IEN, i2c_imx->base + IMX_I2C_I2CR);drivers/i2c/busses/i2c-imx.c +7
-0+ + ? ? result = i2c_imx_bus_busy(i2c_imx, 0); + ? ? if (result) { + ? ? ? ? ? ? reg_dump(i2c_imx); + ? ? ? ? ? ? return result; + ? ? } +? ? ? /* Start I2C transaction */
? ? ? temp =readb(i2c_imx->base + IMX_I2C_I2CR);
? ? ? temp |=I2CR_MSTA;
? ? ?writeb(temp, i2c_imx->base + IMX_I2C_I2CR);
+ ? ? result =i2c_imx_bus_busy(i2c_imx, 1);
+ ? ? if (result) {
+ ? ? ? ? ? ?reg_dump(i2c_imx);
+ ? ? ? ? ? ? return result;
+ ? ? }
? ? ? temp |=I2CR_IIEN|I2CR_MTX|I2CR_TXAK;
? ? ?writeb(temp, i2c_imx->base + IMX_I2C_I2CR);
+ ? ? return result;
?}
Quoted 9 lines
Quoted 7 lines
?static void
i2c_imx_stop(struct imx_i2c_struct *i2c_imx)drivers/i2c/busses/i2c-imx.c +0
-0@@ -202,18 +245,21 @@ static void i2c_imx_stop(struct imx_i2c_struct *i2c_imx)? ? ? /* Stop I2C transaction */
? ? ?dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__); ? ? ? temp =readb(i2c_imx->base + IMX_I2C_I2CR);
- ? ? temp &= ~I2CR_MSTA;
+ ? ? temp &= ~(I2CR_MSTA |I2CR_MTX);This change seems unrelated. Is it necessary?
Yes. It's about STOP. It's better clear MTX. We must generate STOP before read the last byte of Data register. If not, there will be an extra 9bit clock.
Quoted 14 lines
drivers/i2c/busses/i2c-imx.c
Quoted 12 lines
? ? ?
writeb(temp, i2c_imx->base + IMX_I2C_I2CR);
- ? ? /* setup chip registers to defaults */
- ? ?writeb(I2CR_IEN, i2c_imx->base + IMX_I2C_I2CR);
- ? ?writeb(0, i2c_imx->base + IMX_I2C_I2SR);
? ? ? /*
? ? ? ?* This delay caused by an i.MXL hardware bug.
? ? ? ?* If no (or too short) delay, no "STOP" bit will be generated. ? ? ? ?*/
? ? ?udelay(i2c_imx->disable_delay);drivers/i2c/busses/i2c-imx.c +2
-0+ +double blank line
Right.
Quoted 15 lines
Quoted 15 lines
+ ? ? if (
i2c_imx_bus_busy(i2c_imx, 0))
+ ? ? ? ? ? ?reg_dump(i2c_imx);
+
? ? ? /* Disable I2C controller */
? ? ?writeb(0, i2c_imx->base + IMX_I2C_I2CR);
+ ? ?clk_disable(i2c_imx->clk);
?}?static void
__initi2c_imx_set_clk(structimx_i2c_struct*i2c_imx,drivers/i2c/busses/i2c-imx.c +0
-0@@ -233,17 +279,19 @@ static void __init i2c_imx_set_clk(struct imx_i2c_struct *i2c_imx,? ? ? else
? ? ? ? ? ? ? for (i = 0; i2c_clk_div[i][0] < div; i++);
Quoted 4 lines
Quoted 2 lines
- ? ? /* Write divider value to register */
- ? ?writeb(i2c_clk_div[i][1], i2c_imx->base + IMX_I2C_IFDR);Why can't we write this value directly anymore...
clock will be enable/disable in start/stop
Quoted 13 lines
Quoted 12 lines
-
- ? ? /*
- ? ? ?* There dummy delay is calculated.
- ? ? ?* It should be about one I2C clock period long.
- ? ? ?* This delay is used in I2C bus disable function
- ? ? ?* to fix chip hardware bug.
- ? ? ?*/
- ? ?i2c_imx->disable_delay= (500000U * i2c_clk_div[i][0]
- ? ? ? ? ? ? + (i2c_clk_rate/ 2) - 1) / (i2c_clk_rate/ 2);
+ ? ? /* Store divider value */
+ ? ?i2c_imx->ifdr= i2c_clk_div[i][1];
Quoted 1 line
...but have to store it in a variable instead?
To save power, because i2c is not aways be accessed, and it's very low speed bus.
Quoted 40 lines
Quoted 40 lines
+
+ ? ? if (cpu_is_mx1()) {
+ ? ? ? ? ? ? /*
+ ? ? ? ? ? ? ?* There dummy delay is calculated.
+ ? ? ? ? ? ? ?* It should be about one I2C clock period long.
+ ? ? ? ? ? ? ?* This delay is used in I2C bus disable function
+ ? ? ? ? ? ? ?* to fix chip hardware bug.
+ ? ? ? ? ? ? ?*/
+ ? ? ? ? ? ?i2c_imx->disable_delay= (500000U * i2c_clk_div[i][0]
+ ? ? ? ? ? ? ? ? ? ? + (i2c_clk_rate/ 2) - 1) / (i2c_clk_rate/ 2);
+ ? ? }? ? ? /*
dev_dbg()can't be used, because adapter is not yet registered */ ?#ifdefCONFIG_I2C_DEBUG_BUSdrivers/i2c/busses/i2c-imx.c +1
-1@@ -344,7 +392,7 @@ static int i2c_imx_read(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs) ? ? ? ? ? ? ? ? ? ? ? dev_dbg(&i2c_imx->adapter.dev, ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "<%s> clear MSTA\n", __func__); ? ? ? ? ? ? ? ? ? ? ? temp = readb(i2c_imx->base + IMX_I2C_I2CR); - ? ? ? ? ? ? ? ? ? ? temp &= ~I2CR_MSTA; + ? ? ? ? ? ? ? ? ? ? temp &= ~(I2CR_MSTA | I2CR_MTX); ? ? ? ? ? ? ? ? ? ? ? writeb(temp, i2c_imx->base + IMX_I2C_I2CR); ? ? ? ? ? ? ? } else if (i == (msgs->len - 2)) {? ? ? ? ? ? ? ? ? ? ? dev_dbg(&i2c_imx->adapter.dev,
drivers/i2c/busses/i2c-imx.c +10
-1@@ -369,14 +417,24 @@ static int i2c_imx_xfer(struct i2c_adapter *adapter, ? ? ? struct imx_i2c_struct *i2c_imx = i2c_get_adapdata(adapter); ? ? ? dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__); - +#ifdef CONFIG_I2C_DEBUG_BUS + ? ? for (i = 0; i < num; i++) { + ? ? ? ? ? ? printk(KERN_DEBUG "msg%d addr %02x RD %d cnt %d d:", i, + ? ? ? ? ? ? ? ? ? ? msgs[i].addr, msgs[i].flags & I2C_M_RD, msgs[i].len); + ? ? ? ? ? ? if (!(msgs[i].flags & I2C_M_RD)) { + ? ? ? ? ? ? ? ? ? ? int j; + ? ? ? ? ? ? ? ? ? ? for (j = 0; j < msgs[i].len; j++) + ? ? ? ? ? ? ? ? ? ? ? ? ? ? printk("%02x ", msgs[i].buf[j]); + ? ? ? ? ? ? } + ? ? ? ? ? ? printk("\n");
Quoted 8 lines
Quoted 6 lines
drivers/i2c/busses/i2c-imx.c +2
-0+ ? ? } +#endif ? ? ? /* Check if i2c bus is not busy */
- ? ? result =i2c_imx_bus_busy(i2c_imx);
- ? ? if (result)
- ? ? ? ? ? ? goto fail0;When removing the code you should also remove the comment.
Right
Quoted 27 lines
Quoted 27 lines
? ? ? /* Start I2C transfer */
- ? ?i2c_imx_start(i2c_imx);
+ ? ? result =i2c_imx_start(i2c_imx);
+ ? ? if (result)
+ ? ? ? ? ? ? goto fail0;? ? ? /* read/write data */
? ? ? for (i = 0; i < num; i++) {drivers/i2c/busses/i2c-imx.c +5
-0@@ -386,6 +444,11 @@ static int i2c_imx_xfer(struct i2c_adapter *adapter, ? ? ? ? ? ? ? ? ? ? ? temp = readb(i2c_imx->base + IMX_I2C_I2CR); ? ? ? ? ? ? ? ? ? ? ? temp |= I2CR_RSTA; ? ? ? ? ? ? ? ? ? ? ? writeb(temp, i2c_imx->base + IMX_I2C_I2CR); + ? ? ? ? ? ? ? ? ? ? result = ?i2c_imx_bus_busy(i2c_imx, 1); + ? ? ? ? ? ? ? ? ? ? if (result) { + ? ? ? ? ? ? ? ? ? ? ? ? ? ? reg_dump(i2c_imx); + ? ? ? ? ? ? ? ? ? ? ? ? ? ? goto fail0; + ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? } ? ? ? ? ? ? ? dev_dbg(&i2c_imx->adapter.dev, ? ? ? ? ? ? ? ? ? ? ? "<%s> transfer message: %d\n", __func__, i);@@ -442,7 +505,7 @@ static int __init i2c_imx_probe(struct platform_device *pdev) ? ? ? int irq; ? ? ? int ret;
Quoted 4 lines
drivers/i2c/busses/i2c-imx.c static int __init i2c_imx_probe(struct · line 509
Quoted 2 lines
drivers/i2c/busses/i2c-imx.c +1
-1- ? ? dev_dbg(&pdev->dev, "<%s>\n", __func__); + ? ? dev_info(&pdev->dev, "<%s>\n", __func__);no
Why? I even don't know how many i2c controllers we have, if I didn't get sysfs working. And it only print once.
Quoted 13 lines
drivers/i2c/busses/i2c-imx.c static int __init i2c_imx_probe(struct · line 504
Quoted 10 lines
drivers/i2c/busses/i2c-imx.c +0
-1? ? ? res = platform_get_resource(pdev, IORESOURCE_MEM, 0); ? ? ? if (!res) {@@ -500,7 +563,6 @@ static int __init i2c_imx_probe(struct platform_device *pdev) ? ? ? ? ? ? ? dev_err(&pdev->dev, "can't get I2C clock\n"); ? ? ? ? ? ? ? goto fail3; ? ? ? } - ? ? clk_enable(i2c_imx->clk);I think it's generally a good idea to start/stop the clocks when needed as you do here. It should be an extra patch though.
You think I should split the patch to three: IBB, reg_dump, clk dis/en ? The idea is good, more easy to revert. but sometimes, I think it make small things too complex.
Anyway, I'll follow your suggestion.
Quoted 32 lines
Quoted 21 lines
? ? ? /* Request IRQ */
? ? ? ret =request_irq(i2c_imx->irq, i2c_imx_isr, 0, pdev->name, i2c_imx);drivers/i2c/busses/i2c-imx.c +0
-0@@ -549,7 +611,6 @@ static int __init i2c_imx_probe(struct platform_device *pdev)?fail5:
? ? ?free_irq(i2c_imx->irq, i2c_imx);
?fail4:
- ? ?clk_disable(i2c_imx->clk);
? ? ?clk_put(i2c_imx->clk);
?fail3:
? ? ?release_mem_region(i2c_imx->res->start, resource_size(res));drivers/i2c/busses/i2c-imx.c +0
-0@@ -587,7 +648,6 @@ static int __exit i2c_imx_remove(struct platform_device *pdev) ? ? ? ? ? ? ? pdata->exit(&pdev->dev);? ? ? /* Disable I2C clock */
- ? ?clk_disable(i2c_imx->clk);
? ? ?clk_put(i2c_imx->clk);Quoted 4 lines
? ? ?
release_mem_region(i2c_imx->res->start, resource_size(i2c_imx->res)); --
1.6.0.4--
Pengutronix e.K. ? ? ? ? ? ? ? ? ? ? ? ? ? | ? ? ? ? ? ? ? ? ? ? ? ? ? ? | Industrial Linux Solutions ? ? ? ? ? ? ? ? | http://www.pengutronix.de/ ?| Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 ? ?| Amtsgericht Hildesheim, HRA 2686 ? ? ? ? ? | Fax: ? +49-5121-206917-5555 |
Sascha Hauer
maintainerPATCH 1/1 i2c: imx: check busy bit when START/STOP
Quoted 305 lines · Richard Zhao
Thanks, Sacha. See comments inline.
Quoted 39 lines · Sascha Hauer
Hi Richard,
Quoted 35 lines · Richard Zhao
After START, wait busy bit to be set and
after STOP, wait busy bit to be clear.Disable clock when it's possible to save power.
Signed-off-by Richard Zhao <[email protected]>---
?drivers/i2c/busses/i2c-imx.c | ?116 ++++++++++++++++++++++++++++++++---------- ?1 files changed, 88 insertions(+), 28 deletions(-)drivers/i2c/busses/i2c-imx.c +1
-0@@ -120,19 +120,40 @@ struct imx_i2c_struct { ? ? ? wait_queue_head_t ? ? ? queue; ? ? ? unsigned long ? ? ? ? ? i2csr; ? ? ? unsigned int ? ? ? ? ? ?disable_delay; + ? ? unsigned int ? ? ? ? ? ?ifdr; /* IMX_I2C_IFDR */ ?};?/** Functions for IMX I2C adapter driver
***************************************
?*******************************************************************************/drivers/i2c/busses/i2c-imx.c +9
-1-static int i2c_imx_bus_busy (struct imx_i2c_struct *i2c_imx) +#ifdef CONFIG_I2C_DEBUG_BUS +#define reg_dump (i2c_imx) \ +{ \ + ? ? printk(KERN_DEBUG "fun %s:%d ", __func__, __LINE__); \ + ? ? printk(KERN_DEBUG "IADR %02x IFDR %02x I2CR %02x I2SR %02x\n", \ + ? ? ? ? ? ? readb(i2c_imx->base + IMX_I2C_IADR), \ + ? ? ? ? ? ? readb(i2c_imx->base + IMX_I2C_IFDR), \ + ? ? ? ? ? ? readb(i2c_imx->base + IMX_I2C_I2CR), \ + ? ? ? ? ? ? readb(i2c_imx->base + IMX_I2C_I2SR)); \Quoted 8 lines
drivers/i2c/busses/i2c-imx.c
Quoted 5 lines
drivers/i2c/busses/i2c-imx.c +5
-0+} +#else +#define reg_dump (i2c_imx) +#endif +Can you please remove this
reg_dump? If we really need this it should be in an extra patch and not clutter this one.I think it's needed. It helps much to debug and don't effect performance by default.
Quoted 6 lines
Quoted 6 lines
+static int
i2c_imx_bus_busy(struct imx_i2c_struct *i2c_imx, int for_busy)?{
? ? ? unsigned longorig_jiffies= jiffies;
+ ? ? unsigned int temp;Quoted 6 lines
Quoted 3 lines
? ? ?
dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__);? ? ? /* wait for bus not busy */
This comment seems wrong now. This function waits for busy or not busy depending on
for_busy.Right. Thanks
Quoted 82 lines
Quoted 82 lines
- ? ? while (
readb(i2c_imx->base + IMX_I2C_I2SR)&I2SR_IBB) {
+ ? ? temp =readb(i2c_imx->base + IMX_I2C_I2SR);
+ ? ? while (1) {
+ ? ? ? ? ? ? if (for_busy&& (temp &I2SR_IBB))
+ ? ? ? ? ? ? ? ? ? ? break;
+ ? ? ? ? ? ? if (!for_busy && !(temp &I2SR_IBB))
+ ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? if (signal_pending(current)) {
? ? ? ? ? ? ? ? ? ? ? dev_dbg(&i2c_imx->adapter.dev,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "<%s> I2C Interrupted\n",__func__);drivers/i2c/busses/i2c-imx.c +4
-0@@ -141,9 +162,11 @@ static int i2c_imx_bus_busy(struct imx_i2c_struct *i2c_imx) ? ? ? ? ? ? ? if (time_after(jiffies, orig_jiffies + HZ / 1000)) { ? ? ? ? ? ? ? ? ? ? ? dev_dbg(&i2c_imx->adapter.dev, ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "<%s> I2C bus is busy\n", __func__); + ? ? ? ? ? ? ? ? ? ? reg_dump(i2c_imx); ? ? ? ? ? ? ? ? ? ? ? return -EIO; ? ? ? ? ? ? ? } ? ? ? ? ? ? ? schedule(); + ? ? ? ? ? ? temp = readb(i2c_imx->base + IMX_I2C_I2SR); ? ? ? } ? ? ? return 0;@@ -158,9 +181,11 @@ static int i2c_imx_trx_complete(struct imx_i2c_struct *i2c_imx) ? ? ? if (unlikely(result < 0)) { ? ? ? ? ? ? ? dev_dbg(&i2c_imx->adapter.dev, "<%s> result < 0\n", __func__); + ? ? ? ? ? ? reg_dump(i2c_imx); ? ? ? ? ? ? ? return result; ? ? ? } else if (unlikely(!(i2c_imx->i2csr & I2SR_IIF))) { ? ? ? ? ? ? ? dev_dbg(&i2c_imx->adapter.dev, "<%s> Timeout\n", __func__); + ? ? ? ? ? ? reg_dump(i2c_imx); ? ? ? ? ? ? ? return -ETIMEDOUT; ? ? ? }? ? ?
dev_dbg(&i2c_imx->adapter.dev, "<%s> TRX complete\n", __func__);drivers/i2c/busses/i2c-imx.c +1
-0@@ -172,6 +197,7 @@ static int i2c_imx_acked(struct imx_i2c_struct *i2c_imx) ?{ ? ? ? if (readb(i2c_imx->base + IMX_I2C_I2SR) & I2SR_RXAK) { ? ? ? ? ? ? ? dev_dbg(&i2c_imx->adapter.dev, "<%s> No ACK\n", __func__); + ? ? ? ? ? ? reg_dump(i2c_imx);? ? ? ? ? ? ? return
-EIO; ?/* No ACK */
? ? ? }drivers/i2c/busses/i2c-imx.c +4
-1@@ -179,20 +205,37 @@ static int i2c_imx_acked(struct imx_i2c_struct *i2c_imx) ? ? ? return 0; ?} -static void i2c_imx_start (struct imx_i2c_struct *i2c_imx) +static int i2c_imx_start (struct imx_i2c_struct *i2c_imx) ?{ ? ? ? unsigned int temp = 0; + ? ? int result; ? ? ? dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__); + ? ? clk_enable(i2c_imx->clk); + ? ? writeb(i2c_imx->ifdr, i2c_imx->base + IMX_I2C_IFDR);? ? ? /* Enable I2C controller */
+ ? ?writeb(0, i2c_imx->base + IMX_I2C_I2SR); ? ? ?writeb(I2CR_IEN, i2c_imx->base + IMX_I2C_I2CR);drivers/i2c/busses/i2c-imx.c +7
-0+ + ? ? result = i2c_imx_bus_busy(i2c_imx, 0); + ? ? if (result) { + ? ? ? ? ? ? reg_dump(i2c_imx); + ? ? ? ? ? ? return result; + ? ? } +? ? ? /* Start I2C transaction */
? ? ? temp =readb(i2c_imx->base + IMX_I2C_I2CR);
? ? ? temp |=I2CR_MSTA;
? ? ?writeb(temp, i2c_imx->base + IMX_I2C_I2CR);
+ ? ? result =i2c_imx_bus_busy(i2c_imx, 1);
+ ? ? if (result) {
+ ? ? ? ? ? ?reg_dump(i2c_imx);
+ ? ? ? ? ? ? return result;
+ ? ? }
? ? ? temp |=I2CR_IIEN|I2CR_MTX|I2CR_TXAK;
? ? ?writeb(temp, i2c_imx->base + IMX_I2C_I2CR);
+ ? ? return result;
?}Quoted 9 lines
Quoted 7 lines
?static void
i2c_imx_stop(struct imx_i2c_struct *i2c_imx)drivers/i2c/busses/i2c-imx.c +0
-0@@ -202,18 +245,21 @@ static void i2c_imx_stop(struct imx_i2c_struct *i2c_imx)? ? ? /* Stop I2C transaction */
? ? ?dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__); ? ? ? temp =readb(i2c_imx->base + IMX_I2C_I2CR);
- ? ? temp &= ~I2CR_MSTA;
+ ? ? temp &= ~(I2CR_MSTA |I2CR_MTX);This change seems unrelated. Is it necessary?
Yes. It's about STOP. It's better clear MTX. We must generate STOP before read the last byte of Data register. If not, there will be an extra 9bit clock.
Quoted 14 lines
drivers/i2c/busses/i2c-imx.c
Quoted 12 lines
? ? ?
writeb(temp, i2c_imx->base + IMX_I2C_I2CR);
- ? ? /* setup chip registers to defaults */
- ? ?writeb(I2CR_IEN, i2c_imx->base + IMX_I2C_I2CR);
- ? ?writeb(0, i2c_imx->base + IMX_I2C_I2SR);
? ? ? /*
? ? ? ?* This delay caused by an i.MXL hardware bug.
? ? ? ?* If no (or too short) delay, no "STOP" bit will be generated. ? ? ? ?*/
? ? ?udelay(i2c_imx->disable_delay);drivers/i2c/busses/i2c-imx.c +2
-0+ +double blank line
Right.
Quoted 15 lines
Quoted 15 lines
+ ? ? if (
i2c_imx_bus_busy(i2c_imx, 0))
+ ? ? ? ? ? ?reg_dump(i2c_imx);
+
? ? ? /* Disable I2C controller */
? ? ?writeb(0, i2c_imx->base + IMX_I2C_I2CR);
+ ? ?clk_disable(i2c_imx->clk);
?}?static void
__initi2c_imx_set_clk(structimx_i2c_struct*i2c_imx,drivers/i2c/busses/i2c-imx.c +0
-0@@ -233,17 +279,19 @@ static void __init i2c_imx_set_clk(struct imx_i2c_struct *i2c_imx,? ? ? else
? ? ? ? ? ? ? for (i = 0; i2c_clk_div[i][0] < div; i++);Quoted 4 lines
Quoted 2 lines
- ? ? /* Write divider value to register */
- ? ?writeb(i2c_clk_div[i][1], i2c_imx->base + IMX_I2C_IFDR);Why can't we write this value directly anymore...
clock will be enable/disable in start/stop
Quoted 13 lines
Quoted 12 lines
-
- ? ? /*
- ? ? ?* There dummy delay is calculated.
- ? ? ?* It should be about one I2C clock period long.
- ? ? ?* This delay is used in I2C bus disable function
- ? ? ?* to fix chip hardware bug.
- ? ? ?*/
- ? ?i2c_imx->disable_delay= (500000U * i2c_clk_div[i][0]
- ? ? ? ? ? ? + (i2c_clk_rate/ 2) - 1) / (i2c_clk_rate/ 2);
+ ? ? /* Store divider value */
+ ? ?i2c_imx->ifdr= i2c_clk_div[i][1];Quoted 1 line
...but have to store it in a variable instead?
To save power, because i2c is not aways be accessed, and it's very low speed bus.
Quoted 40 lines
Quoted 40 lines
+
+ ? ? if (cpu_is_mx1()) {
+ ? ? ? ? ? ? /*
+ ? ? ? ? ? ? ?* There dummy delay is calculated.
+ ? ? ? ? ? ? ?* It should be about one I2C clock period long.
+ ? ? ? ? ? ? ?* This delay is used in I2C bus disable function
+ ? ? ? ? ? ? ?* to fix chip hardware bug.
+ ? ? ? ? ? ? ?*/
+ ? ? ? ? ? ?i2c_imx->disable_delay= (500000U * i2c_clk_div[i][0]
+ ? ? ? ? ? ? ? ? ? ? + (i2c_clk_rate/ 2) - 1) / (i2c_clk_rate/ 2);
+ ? ? }? ? ? /*
dev_dbg()can't be used, because adapter is not yet registered */ ?#ifdefCONFIG_I2C_DEBUG_BUSdrivers/i2c/busses/i2c-imx.c +1
-1@@ -344,7 +392,7 @@ static int i2c_imx_read(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs) ? ? ? ? ? ? ? ? ? ? ? dev_dbg(&i2c_imx->adapter.dev, ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "<%s> clear MSTA\n", __func__); ? ? ? ? ? ? ? ? ? ? ? temp = readb(i2c_imx->base + IMX_I2C_I2CR); - ? ? ? ? ? ? ? ? ? ? temp &= ~I2CR_MSTA; + ? ? ? ? ? ? ? ? ? ? temp &= ~(I2CR_MSTA | I2CR_MTX); ? ? ? ? ? ? ? ? ? ? ? writeb(temp, i2c_imx->base + IMX_I2C_I2CR); ? ? ? ? ? ? ? } else if (i == (msgs->len - 2)) {? ? ? ? ? ? ? ? ? ? ? dev_dbg(&i2c_imx->adapter.dev,
drivers/i2c/busses/i2c-imx.c +10
-1@@ -369,14 +417,24 @@ static int i2c_imx_xfer(struct i2c_adapter *adapter, ? ? ? struct imx_i2c_struct *i2c_imx = i2c_get_adapdata(adapter); ? ? ? dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__); - +#ifdef CONFIG_I2C_DEBUG_BUS + ? ? for (i = 0; i < num; i++) { + ? ? ? ? ? ? printk(KERN_DEBUG "msg%d addr %02x RD %d cnt %d d:", i, + ? ? ? ? ? ? ? ? ? ? msgs[i].addr, msgs[i].flags & I2C_M_RD, msgs[i].len); + ? ? ? ? ? ? if (!(msgs[i].flags & I2C_M_RD)) { + ? ? ? ? ? ? ? ? ? ? int j; + ? ? ? ? ? ? ? ? ? ? for (j = 0; j < msgs[i].len; j++) + ? ? ? ? ? ? ? ? ? ? ? ? ? ? printk("%02x ", msgs[i].buf[j]); + ? ? ? ? ? ? } + ? ? ? ? ? ? printk("\n");Quoted 8 lines
Quoted 6 lines
drivers/i2c/busses/i2c-imx.c +2
-0+ ? ? } +#endif ? ? ? /* Check if i2c bus is not busy */
- ? ? result =i2c_imx_bus_busy(i2c_imx);
- ? ? if (result)
- ? ? ? ? ? ? goto fail0;When removing the code you should also remove the comment.
Right
Quoted 27 lines
Quoted 27 lines
? ? ? /* Start I2C transfer */
- ? ?i2c_imx_start(i2c_imx);
+ ? ? result =i2c_imx_start(i2c_imx);
+ ? ? if (result)
+ ? ? ? ? ? ? goto fail0;? ? ? /* read/write data */
? ? ? for (i = 0; i < num; i++) {drivers/i2c/busses/i2c-imx.c +5
-0@@ -386,6 +444,11 @@ static int i2c_imx_xfer(struct i2c_adapter *adapter, ? ? ? ? ? ? ? ? ? ? ? temp = readb(i2c_imx->base + IMX_I2C_I2CR); ? ? ? ? ? ? ? ? ? ? ? temp |= I2CR_RSTA; ? ? ? ? ? ? ? ? ? ? ? writeb(temp, i2c_imx->base + IMX_I2C_I2CR); + ? ? ? ? ? ? ? ? ? ? result = ?i2c_imx_bus_busy(i2c_imx, 1); + ? ? ? ? ? ? ? ? ? ? if (result) { + ? ? ? ? ? ? ? ? ? ? ? ? ? ? reg_dump(i2c_imx); + ? ? ? ? ? ? ? ? ? ? ? ? ? ? goto fail0; + ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? } ? ? ? ? ? ? ? dev_dbg(&i2c_imx->adapter.dev, ? ? ? ? ? ? ? ? ? ? ? "<%s> transfer message: %d\n", __func__, i);@@ -442,7 +505,7 @@ static int __init i2c_imx_probe(struct platform_device *pdev) ? ? ? int irq; ? ? ? int ret;Quoted 4 lines
drivers/i2c/busses/i2c-imx.c static int __init i2c_imx_probe(struct · line 509
Quoted 2 lines
drivers/i2c/busses/i2c-imx.c +1
-1- ? ? dev_dbg(&pdev->dev, "<%s>\n", __func__); + ? ? dev_info(&pdev->dev, "<%s>\n", __func__);no
Why? I even don't know how many i2c controllers we have, if I didn't get sysfs working. And it only print once.
Quoted 13 lines
drivers/i2c/busses/i2c-imx.c static int __init i2c_imx_probe(struct · line 504
Quoted 10 lines
drivers/i2c/busses/i2c-imx.c +0
-1? ? ? res = platform_get_resource(pdev, IORESOURCE_MEM, 0); ? ? ? if (!res) {@@ -500,7 +563,6 @@ static int __init i2c_imx_probe(struct platform_device *pdev) ? ? ? ? ? ? ? dev_err(&pdev->dev, "can't get I2C clock\n"); ? ? ? ? ? ? ? goto fail3; ? ? ? } - ? ? clk_enable(i2c_imx->clk);I think it's generally a good idea to start/stop the clocks when needed as you do here. It should be an extra patch though.
Quoted 4 lines
You think I should split the patch to three: IBB,
reg_dump, clk dis/en ? The idea is good, more easy to revert. but sometimes, I think it make small things too complex.
Anyway, I'll follow your suggestion.
In case of regressions everybody will thank you for doing it.
Sascha
Signature · 4 lines
Pengutronix e.K. | | Industrial Linux Solutions | http://www.pengutronix.de/ | Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
PATCH 1/1 i2c: imx: check busy bit when START/STOP
Quoted 313 lines · Sascha Hauer
Quoted 305 lines · Richard Zhao
Thanks, Sacha. See comments inline.
Quoted 39 lines · Sascha Hauer
Hi Richard,
Quoted 35 lines · Richard Zhao
After START, wait busy bit to be set and
after STOP, wait busy bit to be clear.Disable clock when it's possible to save power.
Signed-off-by Richard Zhao <[email protected]>---
?drivers/i2c/busses/i2c-imx.c | ?116 ++++++++++++++++++++++++++++++++---------- ?1 files changed, 88 insertions(+), 28 deletions(-)drivers/i2c/busses/i2c-imx.c +1
-0@@ -120,19 +120,40 @@ struct imx_i2c_struct { ? ? ? wait_queue_head_t ? ? ? queue; ? ? ? unsigned long ? ? ? ? ? i2csr; ? ? ? unsigned int ? ? ? ? ? ?disable_delay; + ? ? unsigned int ? ? ? ? ? ?ifdr; /* IMX_I2C_IFDR */ ?};?/** Functions for IMX I2C adapter driver
***************************************
?*******************************************************************************/drivers/i2c/busses/i2c-imx.c +9
-1-static int i2c_imx_bus_busy (struct imx_i2c_struct *i2c_imx) +#ifdef CONFIG_I2C_DEBUG_BUS +#define reg_dump (i2c_imx) \ +{ \ + ? ? printk(KERN_DEBUG "fun %s:%d ", __func__, __LINE__); \ + ? ? printk(KERN_DEBUG "IADR %02x IFDR %02x I2CR %02x I2SR %02x\n", \ + ? ? ? ? ? ? readb(i2c_imx->base + IMX_I2C_IADR), \ + ? ? ? ? ? ? readb(i2c_imx->base + IMX_I2C_IFDR), \ + ? ? ? ? ? ? readb(i2c_imx->base + IMX_I2C_I2CR), \ + ? ? ? ? ? ? readb(i2c_imx->base + IMX_I2C_I2SR)); \Quoted 8 lines
drivers/i2c/busses/i2c-imx.c
Quoted 5 lines
drivers/i2c/busses/i2c-imx.c +5
-0+} +#else +#define reg_dump (i2c_imx) +#endif +Can you please remove this
reg_dump? If we really need this it should be in an extra patch and not clutter this one.I think it's needed. It helps much to debug and don't effect performance by default.
Quoted 6 lines
Quoted 6 lines
+static int
i2c_imx_bus_busy(struct imx_i2c_struct *i2c_imx, int for_busy)?{
? ? ? unsigned longorig_jiffies= jiffies;
+ ? ? unsigned int temp;Quoted 6 lines
Quoted 3 lines
? ? ?
dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__);? ? ? /* wait for bus not busy */
This comment seems wrong now. This function waits for busy or not busy depending on
for_busy.Right. Thanks
Quoted 82 lines
Quoted 82 lines
- ? ? while (
readb(i2c_imx->base + IMX_I2C_I2SR)&I2SR_IBB) {
+ ? ? temp =readb(i2c_imx->base + IMX_I2C_I2SR);
+ ? ? while (1) {
+ ? ? ? ? ? ? if (for_busy&& (temp &I2SR_IBB))
+ ? ? ? ? ? ? ? ? ? ? break;
+ ? ? ? ? ? ? if (!for_busy && !(temp &I2SR_IBB))
+ ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? if (signal_pending(current)) {
? ? ? ? ? ? ? ? ? ? ? dev_dbg(&i2c_imx->adapter.dev,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "<%s> I2C Interrupted\n",__func__);drivers/i2c/busses/i2c-imx.c +4
-0@@ -141,9 +162,11 @@ static int i2c_imx_bus_busy(struct imx_i2c_struct *i2c_imx) ? ? ? ? ? ? ? if (time_after(jiffies, orig_jiffies + HZ / 1000)) { ? ? ? ? ? ? ? ? ? ? ? dev_dbg(&i2c_imx->adapter.dev, ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "<%s> I2C bus is busy\n", __func__); + ? ? ? ? ? ? ? ? ? ? reg_dump(i2c_imx); ? ? ? ? ? ? ? ? ? ? ? return -EIO; ? ? ? ? ? ? ? } ? ? ? ? ? ? ? schedule(); + ? ? ? ? ? ? temp = readb(i2c_imx->base + IMX_I2C_I2SR); ? ? ? } ? ? ? return 0;@@ -158,9 +181,11 @@ static int i2c_imx_trx_complete(struct imx_i2c_struct *i2c_imx) ? ? ? if (unlikely(result < 0)) { ? ? ? ? ? ? ? dev_dbg(&i2c_imx->adapter.dev, "<%s> result < 0\n", __func__); + ? ? ? ? ? ? reg_dump(i2c_imx); ? ? ? ? ? ? ? return result; ? ? ? } else if (unlikely(!(i2c_imx->i2csr & I2SR_IIF))) { ? ? ? ? ? ? ? dev_dbg(&i2c_imx->adapter.dev, "<%s> Timeout\n", __func__); + ? ? ? ? ? ? reg_dump(i2c_imx); ? ? ? ? ? ? ? return -ETIMEDOUT; ? ? ? }? ? ?
dev_dbg(&i2c_imx->adapter.dev, "<%s> TRX complete\n", __func__);drivers/i2c/busses/i2c-imx.c +1
-0@@ -172,6 +197,7 @@ static int i2c_imx_acked(struct imx_i2c_struct *i2c_imx) ?{ ? ? ? if (readb(i2c_imx->base + IMX_I2C_I2SR) & I2SR_RXAK) { ? ? ? ? ? ? ? dev_dbg(&i2c_imx->adapter.dev, "<%s> No ACK\n", __func__); + ? ? ? ? ? ? reg_dump(i2c_imx);? ? ? ? ? ? ? return
-EIO; ?/* No ACK */
? ? ? }drivers/i2c/busses/i2c-imx.c +4
-1@@ -179,20 +205,37 @@ static int i2c_imx_acked(struct imx_i2c_struct *i2c_imx) ? ? ? return 0; ?} -static void i2c_imx_start (struct imx_i2c_struct *i2c_imx) +static int i2c_imx_start (struct imx_i2c_struct *i2c_imx) ?{ ? ? ? unsigned int temp = 0; + ? ? int result; ? ? ? dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__); + ? ? clk_enable(i2c_imx->clk); + ? ? writeb(i2c_imx->ifdr, i2c_imx->base + IMX_I2C_IFDR);? ? ? /* Enable I2C controller */
+ ? ?writeb(0, i2c_imx->base + IMX_I2C_I2SR); ? ? ?writeb(I2CR_IEN, i2c_imx->base + IMX_I2C_I2CR);drivers/i2c/busses/i2c-imx.c +7
-0+ + ? ? result = i2c_imx_bus_busy(i2c_imx, 0); + ? ? if (result) { + ? ? ? ? ? ? reg_dump(i2c_imx); + ? ? ? ? ? ? return result; + ? ? } +? ? ? /* Start I2C transaction */
? ? ? temp =readb(i2c_imx->base + IMX_I2C_I2CR);
? ? ? temp |=I2CR_MSTA;
? ? ?writeb(temp, i2c_imx->base + IMX_I2C_I2CR);
+ ? ? result =i2c_imx_bus_busy(i2c_imx, 1);
+ ? ? if (result) {
+ ? ? ? ? ? ?reg_dump(i2c_imx);
+ ? ? ? ? ? ? return result;
+ ? ? }
? ? ? temp |=I2CR_IIEN|I2CR_MTX|I2CR_TXAK;
? ? ?writeb(temp, i2c_imx->base + IMX_I2C_I2CR);
+ ? ? return result;
?}Quoted 9 lines
Quoted 7 lines
?static void
i2c_imx_stop(struct imx_i2c_struct *i2c_imx)drivers/i2c/busses/i2c-imx.c +0
-0@@ -202,18 +245,21 @@ static void i2c_imx_stop(struct imx_i2c_struct *i2c_imx)? ? ? /* Stop I2C transaction */
? ? ?dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__); ? ? ? temp =readb(i2c_imx->base + IMX_I2C_I2CR);
- ? ? temp &= ~I2CR_MSTA;
+ ? ? temp &= ~(I2CR_MSTA |I2CR_MTX);This change seems unrelated. Is it necessary?
Yes. It's about STOP. It's better clear MTX. We must generate STOP before read the last byte of Data register. If not, there will be an extra 9bit clock.
Quoted 14 lines
drivers/i2c/busses/i2c-imx.c
Quoted 12 lines
? ? ?
writeb(temp, i2c_imx->base + IMX_I2C_I2CR);
- ? ? /* setup chip registers to defaults */
- ? ?writeb(I2CR_IEN, i2c_imx->base + IMX_I2C_I2CR);
- ? ?writeb(0, i2c_imx->base + IMX_I2C_I2SR);
? ? ? /*
? ? ? ?* This delay caused by an i.MXL hardware bug.
? ? ? ?* If no (or too short) delay, no "STOP" bit will be generated. ? ? ? ?*/
? ? ?udelay(i2c_imx->disable_delay);drivers/i2c/busses/i2c-imx.c +2
-0+ +double blank line
Right.
Quoted 15 lines
Quoted 15 lines
+ ? ? if (
i2c_imx_bus_busy(i2c_imx, 0))
+ ? ? ? ? ? ?reg_dump(i2c_imx);
+
? ? ? /* Disable I2C controller */
? ? ?writeb(0, i2c_imx->base + IMX_I2C_I2CR);
+ ? ?clk_disable(i2c_imx->clk);
?}?static void
__initi2c_imx_set_clk(structimx_i2c_struct*i2c_imx,drivers/i2c/busses/i2c-imx.c +0
-0@@ -233,17 +279,19 @@ static void __init i2c_imx_set_clk(struct imx_i2c_struct *i2c_imx,? ? ? else
? ? ? ? ? ? ? for (i = 0; i2c_clk_div[i][0] < div; i++);Quoted 4 lines
Quoted 2 lines
- ? ? /* Write divider value to register */
- ? ?writeb(i2c_clk_div[i][1], i2c_imx->base + IMX_I2C_IFDR);Why can't we write this value directly anymore...
clock will be enable/disable in start/stop
Quoted 13 lines
Quoted 12 lines
-
- ? ? /*
- ? ? ?* There dummy delay is calculated.
- ? ? ?* It should be about one I2C clock period long.
- ? ? ?* This delay is used in I2C bus disable function
- ? ? ?* to fix chip hardware bug.
- ? ? ?*/
- ? ?i2c_imx->disable_delay= (500000U * i2c_clk_div[i][0]
- ? ? ? ? ? ? + (i2c_clk_rate/ 2) - 1) / (i2c_clk_rate/ 2);
+ ? ? /* Store divider value */
+ ? ?i2c_imx->ifdr= i2c_clk_div[i][1];Quoted 1 line
...but have to store it in a variable instead?
To save power, because i2c is not aways be accessed, and it's very low speed bus.
Quoted 40 lines
Quoted 40 lines
+
+ ? ? if (cpu_is_mx1()) {
+ ? ? ? ? ? ? /*
+ ? ? ? ? ? ? ?* There dummy delay is calculated.
+ ? ? ? ? ? ? ?* It should be about one I2C clock period long.
+ ? ? ? ? ? ? ?* This delay is used in I2C bus disable function
+ ? ? ? ? ? ? ?* to fix chip hardware bug.
+ ? ? ? ? ? ? ?*/
+ ? ? ? ? ? ?i2c_imx->disable_delay= (500000U * i2c_clk_div[i][0]
+ ? ? ? ? ? ? ? ? ? ? + (i2c_clk_rate/ 2) - 1) / (i2c_clk_rate/ 2);
+ ? ? }? ? ? /*
dev_dbg()can't be used, because adapter is not yet registered */ ?#ifdefCONFIG_I2C_DEBUG_BUSdrivers/i2c/busses/i2c-imx.c +1
-1@@ -344,7 +392,7 @@ static int i2c_imx_read(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs) ? ? ? ? ? ? ? ? ? ? ? dev_dbg(&i2c_imx->adapter.dev, ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "<%s> clear MSTA\n", __func__); ? ? ? ? ? ? ? ? ? ? ? temp = readb(i2c_imx->base + IMX_I2C_I2CR); - ? ? ? ? ? ? ? ? ? ? temp &= ~I2CR_MSTA; + ? ? ? ? ? ? ? ? ? ? temp &= ~(I2CR_MSTA | I2CR_MTX); ? ? ? ? ? ? ? ? ? ? ? writeb(temp, i2c_imx->base + IMX_I2C_I2CR); ? ? ? ? ? ? ? } else if (i == (msgs->len - 2)) {? ? ? ? ? ? ? ? ? ? ? dev_dbg(&i2c_imx->adapter.dev,
drivers/i2c/busses/i2c-imx.c +10
-1@@ -369,14 +417,24 @@ static int i2c_imx_xfer(struct i2c_adapter *adapter, ? ? ? struct imx_i2c_struct *i2c_imx = i2c_get_adapdata(adapter); ? ? ? dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__); - +#ifdef CONFIG_I2C_DEBUG_BUS + ? ? for (i = 0; i < num; i++) { + ? ? ? ? ? ? printk(KERN_DEBUG "msg%d addr %02x RD %d cnt %d d:", i, + ? ? ? ? ? ? ? ? ? ? msgs[i].addr, msgs[i].flags & I2C_M_RD, msgs[i].len); + ? ? ? ? ? ? if (!(msgs[i].flags & I2C_M_RD)) { + ? ? ? ? ? ? ? ? ? ? int j; + ? ? ? ? ? ? ? ? ? ? for (j = 0; j < msgs[i].len; j++) + ? ? ? ? ? ? ? ? ? ? ? ? ? ? printk("%02x ", msgs[i].buf[j]); + ? ? ? ? ? ? } + ? ? ? ? ? ? printk("\n");Quoted 8 lines
Quoted 6 lines
drivers/i2c/busses/i2c-imx.c +2
-0+ ? ? } +#endif ? ? ? /* Check if i2c bus is not busy */
- ? ? result =i2c_imx_bus_busy(i2c_imx);
- ? ? if (result)
- ? ? ? ? ? ? goto fail0;When removing the code you should also remove the comment.
Right
Quoted 27 lines
Quoted 27 lines
? ? ? /* Start I2C transfer */
- ? ?i2c_imx_start(i2c_imx);
+ ? ? result =i2c_imx_start(i2c_imx);
+ ? ? if (result)
+ ? ? ? ? ? ? goto fail0;? ? ? /* read/write data */
? ? ? for (i = 0; i < num; i++) {drivers/i2c/busses/i2c-imx.c +5
-0@@ -386,6 +444,11 @@ static int i2c_imx_xfer(struct i2c_adapter *adapter, ? ? ? ? ? ? ? ? ? ? ? temp = readb(i2c_imx->base + IMX_I2C_I2CR); ? ? ? ? ? ? ? ? ? ? ? temp |= I2CR_RSTA; ? ? ? ? ? ? ? ? ? ? ? writeb(temp, i2c_imx->base + IMX_I2C_I2CR); + ? ? ? ? ? ? ? ? ? ? result = ?i2c_imx_bus_busy(i2c_imx, 1); + ? ? ? ? ? ? ? ? ? ? if (result) { + ? ? ? ? ? ? ? ? ? ? ? ? ? ? reg_dump(i2c_imx); + ? ? ? ? ? ? ? ? ? ? ? ? ? ? goto fail0; + ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? } ? ? ? ? ? ? ? dev_dbg(&i2c_imx->adapter.dev, ? ? ? ? ? ? ? ? ? ? ? "<%s> transfer message: %d\n", __func__, i);@@ -442,7 +505,7 @@ static int __init i2c_imx_probe(struct platform_device *pdev) ? ? ? int irq; ? ? ? int ret;Quoted 4 lines
drivers/i2c/busses/i2c-imx.c static int __init i2c_imx_probe(struct · line 509
Quoted 2 lines
drivers/i2c/busses/i2c-imx.c +1
-1- ? ? dev_dbg(&pdev->dev, "<%s>\n", __func__); + ? ? dev_info(&pdev->dev, "<%s>\n", __func__);no
Why? I even don't know how many i2c controllers we have, if I didn't get sysfs working. And it only print once.
Quoted 13 lines
drivers/i2c/busses/i2c-imx.c static int __init i2c_imx_probe(struct · line 504
Quoted 10 lines
drivers/i2c/busses/i2c-imx.c +0
-1? ? ? res = platform_get_resource(pdev, IORESOURCE_MEM, 0); ? ? ? if (!res) {@@ -500,7 +563,6 @@ static int __init i2c_imx_probe(struct platform_device *pdev) ? ? ? ? ? ? ? dev_err(&pdev->dev, "can't get I2C clock\n"); ? ? ? ? ? ? ? goto fail3; ? ? ? } - ? ? clk_enable(i2c_imx->clk);I think it's generally a good idea to start/stop the clocks when needed as you do here. It should be an extra patch though.
Quoted 4 lines
You think I should split the patch to three: IBB,
reg_dump, clk dis/en ? The idea is good, more easy to revert. but sometimes, I think it make small things too complex.
Anyway, I'll follow your suggestion.In case of regressions everybody will thank you for doing it.
Quoted 9 lines
Sascha
--
Pengutronix e.K. ? ? ? ? ? ? ? ? ? ? ? ? ? | ? ? ? ? ? ? ? ? ? ? ? ? ? ? | Industrial Linux Solutions ? ? ? ? ? ? ? ? | http://www.pengutronix.de/ ?| Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 ? ?| Amtsgericht Hildesheim, HRA 2686 ? ? ? ? ? | Fax: ? +49-5121-206917-5555 |
Hi Sascha,
Do you agree with my other comments?
Thanks
Richard
PATCH 1/1 i2c: imx: check busy bit when START/STOP
In reply to Richard ZhaoQuoted 8 lines · Richard Zhao
Quoted 2 lines · Sascha Hauer
I think it's generally a good idea to start/stop the clocks when needed as you do here. It should be an extra patch though.
You think I should split the patch to three: IBB,
reg_dump, clk dis/en ? The idea is good, more easy to revert. but sometimes, I think it make small things too complex.
Keeping small things simple is half the reason for doing this - it's not just bisection, it's also the fact that it's much easier to read and understand a small patch that does one thing than it is to do the same with a large patch which makes several unrelated changes. When several changes are lumped in together it's harder to see the complete version of any one change in isolation and more effort is required to work out which goals are being accomplished with any given chunk of the patch.
PATCH 1/1 i2c: imx: check busy bit when START/STOP
Quoted 10 lines · Mark Brown
Quoted 8 lines · Richard Zhao
Quoted 2 lines · Sascha Hauer
I think it's generally a good idea to start/stop the clocks when needed as you do here. It should be an extra patch though.
You think I should split the patch to three: IBB,
reg_dump, clk dis/en ? The idea is good, more easy to revert. but sometimes, I think it make small things too complex.
Quoted 8 lines
Keeping small things simple is half the reason for doing this - it's not just bisection, it's also the fact that it's much easier to read and understand a small patch that does one thing than it is to do the same with a large patch which makes several unrelated changes. ?When several changes are lumped in together it's harder to see the complete version of any one change in isolation and more effort is required to work out which goals are being accomplished with any given chunk of the patch.
Thank you all!
If you don't have other comments, I will split the patch and send out again tomorrow.
Thanks
Richard