Skip to contentSkip to conversation

Add restart support to i2c-pnx

DISCUSSION [email protected] 5 participants 11 messages Feb 2, 2011 – Feb 5, 2011 8 messages not in the archive I2C subsystem host drivers

lore
Share & tools
lore
#0 lore raw

To all,

this patch adds support for restart to the i2c-pnx.c code.

Any comments?

Regards,
Matej

#2 lore raw
In reply to a message not in the archive

Hi,

Quoted 4 lines · On Wed, Feb 2, 2011 at 11:41 AM, <matej.kupljen-nWYkTt+LGLL1KXRcyAk9cg@public.…

To all,

this patch adds support for restart to the i2c-pnx.c code.

Sorry, can't see the patch itself :)

Thanks,
Vitaly

#6 lore raw
In reply to a message not in the archive
Quoted 4 lines · On Wed, 02 Feb 2011 12:43:03 +0100, matej.kupljen-nWYkTt+LGLL1KXRcyAk9cg@public…

Hi,

Quoted 2 lines
Quoted 2 lines

To all,

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

this patch adds support for restart to the i2c-pnx.c code.

Sorry, can't see the patch itself :)

Hit send to quick :D

I hope now you can see it.

No, we still can't.

Signature · 1 line
Jean Delvare
#8 lore raw
In reply to a message not in the archive

Hi,

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

To all,

this patch adds support for restart to the i2c-pnx.c code.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

Sorry, can't see the patch itself :)

Hit send to quick :D

I hope now you can see it.

No, we still can't.

Something is removing my attachments :(

This time in-line:
---------------------------------------------------------------

Current code in i2c-pnx.c doesn't support sending messages with restart condition in it, for example:
S - (address | W) - Sr - (address | R) - ... - P

This patch adds this to the current code. It has been tested on a custom board with LPC3152 CPU (similar to Embedded Artists EA313x board) with uBlox GPS receiver connected to I2C bus. It has been running over the night without errors.
Also, the patch extends the timeout value, but this was only needed, because there are a lot of data to be transferred and the timeout occurred before all the data was transferred.

The pathch is against 2.6.28.2, but it should apply to the cureent GIT version as well.

i2c-pnx.c renamed from i2c-pnx-orig.c +1 -1
@@ -54,7 +54,8 @@
 {
 	struct i2c_pnx_algo_data *data = adap->algo_data;
 	struct timer_list *timer = &data->mif.timer;
-	int expires = I2C_PNX_TIMEOUT / (1000 / HZ);
+	// We should probably take into account, how many bytes we need to

transfer!
+ int expires = I2C_PNX_TIMEOUT * 10 / (1000 / HZ);

del_timer_sync(timer);

@@ -74,7 +75,7 @@
*
* Generate a START signal in the desired mode.
*/
-static int i2c_pnx_start(unsigned char slave_addr, struct i2c_adapter *adap)
+static int i2c_pnx_start(unsigned char slave_addr, struct i2c_adapter *adap, int repeated)
{
struct i2c_pnx_algo_data *alg_data = adap->algo_data;

@@ -89,8 +90,8 @@
return -EINVAL;
}

- /* First, make sure bus is idle */
- if (wait_timeout(I2C_PNX_TIMEOUT, alg_data)) {
+ /* First, make sure bus is idle, if we are not doing repeated start */
+ if ((!repeated) && (wait_timeout(I2C_PNX_TIMEOUT, alg_data))) {
/* Somebody else is monopolizing the bus */ dev_err(&adap->dev, "%s: Bus busy. Slave addr = %02x, "
"cntrl = %x, stat = %x\n",
@@ -439,7 +440,7 @@
i2c_pnx_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num) {
struct i2c_msg *pmsg;
- int rc = 0, completed = 0, i;
+ int rc = 0, completed = 0, i, repeated;
struct i2c_pnx_algo_data *alg_data = adap->algo_data; u32 stat = ioread32(I2C_REG_STS(alg_data));

@@ -454,6 +455,7 @@

pmsg = &msgs[i];
addr = pmsg->addr;
+ repeated = 0;

if (pmsg->flags & I2C_M_TEN) {
dev_err(&adap->dev,
@@ -479,16 +481,22 @@
/* initialize the completion var */ init_completion(&alg_data->mif.complete);

- /* Enable master interrupt */
- iowrite32(ioread32(I2C_REG_CTL(alg_data)) | mcntrl_afie |
- mcntrl_naie | mcntrl_drmie,
- I2C_REG_CTL(alg_data));
+ /* If this is not the only message, then we need to
+ * send repeated start, but only if the flaga allow us
+ */
+ if((i > 0) && !(pmsg->flags & I2C_M_NOSTART))
+ repeated = 1;

/* Put start-code and slave-address on the bus. */
- rc = i2c_pnx_start(addr, adap);
+ rc = i2c_pnx_start(addr, adap, repeated);
if (rc < 0)
break;

+ /* Enable master interrupt */
+ iowrite32(ioread32(I2C_REG_CTL(alg_data)) | mcntrl_afie |
+ mcntrl_naie | mcntrl_drmie,
+ I2C_REG_CTL(alg_data));
+
/* Wait for completion */ wait_for_completion(&alg_data->mif.complete);

#10 lore raw
In reply to a message not in the archive

Hi Matel,

Thanks for posting this patch. See comments below.. I'll be happy to test your changes once the following items are clarified.

Quoted 2 lines

The pathch is against 2.6.28.2, but it should apply to the cureent GIT version as well.

This patch needs to be re-based against the latest version of the driver as it has changes that this patch will revert or won't apply to.

Quoted 11 lines
i2c-pnx.c renamed from i2c-pnx-orig.c +1 -1
@@ -54,7 +54,8 @@
 {
 	struct i2c_pnx_algo_data *data = adap->algo_data;
 	struct timer_list *timer = &data->mif.timer;
-	int expires = I2C_PNX_TIMEOUT / (1000 / HZ);
+	// We should probably take into account, how many bytes we need to
Quoted 1 line

transfer!

Use /* */ for comments instead of //
Regarding this comment, you should remove it or add some support for size based timeouts. The minimum timeout with the latest updates will always be 10mS waiting for a stop condition or the bus to clear, but the timeout doesn't come into play for the actual transfer.

i2c-pnx.c

Quoted 1 line
i2c-pnx.c renamed from i2c-pnx-orig.c +1 -0
+	int expires = I2C_PNX_TIMEOUT * 10 / (1000 / HZ);

The logic for handling timeout was changed in a later version of i2c-pnx driver. This might of already fixed the timeout issue.

Quoted 44 lines
i2c-pnx.c renamed from i2c-pnx-orig.c +1 -1
 	del_timer_sync(timer);
@@ -74,7 +75,7 @@
  *
  * Generate a START signal in the desired mode.
  */
-static int i2c_pnx_start(unsigned char slave_addr, struct i2c_adapter
 *adap)
+static int i2c_pnx_start(unsigned char slave_addr, struct i2c_adapter
 *adap, int repeated)
 {
i2c-pnx.c renamed from i2c-pnx-orig.c +2 -2
 	struct i2c_pnx_algo_data *alg_data = adap->algo_data;
@@ -89,8 +90,8 @@
 		return -EINVAL;
 	}
 
-	/* First, make sure bus is idle */
-	if (wait_timeout(I2C_PNX_TIMEOUT, alg_data)) {
+	/* First, make sure bus is idle, if we are not doing repeated start
 */
+	if ((!repeated) && (wait_timeout(I2C_PNX_TIMEOUT, alg_data))) {
 		/* Somebody else is monopolizing the bus */
 		dev_err(&adap->dev, "%s: Bus busy. Slave addr = %02x, "
i2c-pnx.c renamed from i2c-pnx-orig.c +2 -1
 		       "cntrl = %x, stat = %x\n",
@@ -439,7 +440,7 @@
 i2c_pnx_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
 {
 	struct i2c_msg *pmsg;
-	int rc = 0, completed = 0, i;
+	int rc = 0, completed = 0, i, repeated;
 	struct i2c_pnx_algo_data *alg_data = adap->algo_data;
 	u32 stat = ioread32(I2C_REG_STS(alg_data));
 
@@ -454,6 +455,7 @@
 
 		pmsg = &msgs[i];
 		addr = pmsg->addr;
+		repeated = 0;
 
 		if (pmsg->flags & I2C_M_TEN) {
 			dev_err(&adap->dev,

i2c-pnx.c line 486

Quoted 10 lines
i2c-pnx.c renamed from i2c-pnx-orig.c +3 -4
@@ -479,16 +481,22 @@
 		/* initialize the completion var */
 		init_completion(&alg_data->mif.complete);
 
-		/* Enable master interrupt */
-		iowrite32(ioread32(I2C_REG_CTL(alg_data)) | mcntrl_afie |
-				mcntrl_naie | mcntrl_drmie,
-			  I2C_REG_CTL(alg_data));
+		/* If this is not the only message, then we need to
+		 * send repeated start, but only if the flaga allow us
+		 */

Comments than span multiple lines should be in the format /*
* Line 1
* Line 2
*/
Single comment lines are in the format
/* Line 1 */
'Flaga allow us' should be 'flag allows it'

i2c-pnx.c line 488

Quoted 2 lines
i2c-pnx.c renamed from i2c-pnx-orig.c +2 -0
+		if((i > 0) && !(pmsg->flags & I2C_M_NOSTART))
+			repeated = 1;

All the repeated start logic seems like it will work ok.

Quoted 4 lines
i2c-pnx.c renamed from i2c-pnx-orig.c +1 -1
 		/* Put start-code and slave-address on the bus. */
-		rc = i2c_pnx_start(addr, adap);
+		rc = i2c_pnx_start(addr, adap, repeated);
#12 lore raw
In reply to a message not in the archive

Kevin,

Quoted 10 lines

Thanks for posting this patch. See comments below.. I'll be happy to test your changes once the following items are clarified.

Quoted 2 lines

The pathch is against 2.6.28.2, but it should apply to the cureent GIT version as well.

This patch needs to be re-based against the latest version of the driver as it has changes that this patch will revert or won't apply to.

I re-based it against latest version of the kernel in GIT and also included your comments in the mail.

Is it O.K. now?

Regards,
Matej

P.S.: Changed the email client. I now attached the file as well as pasted it in-line.

-------------------------------------------------------------------
Current code in i2c-pnx.c doesn't support sending messages with restart condition in it, for example:
S - (address | W) - Sr - (address | R) - ... - P

This patch adds this to the current code. It has been tested on a custom board with LPC3152 CPU (similar to Embedded Artists EA313x board) with uBlox GPS receiver connected to I2C bus. It has been running over the night without errors.

i2c-pnx.c renamed from i2c-pnx-orig.c +17 -9
@@ -78,7 +78,7 @@
  * Generate a START signal in the desired mode.
  */
 static int i2c_pnx_start(unsigned char slave_addr,
-	struct i2c_pnx_algo_data *alg_data)
+	struct i2c_pnx_algo_data *alg_data, int repeated)
 {
 	dev_dbg(&alg_data->adapter.dev, "%s(): addr 0x%x mode %d\n", __func__,
 		slave_addr, alg_data->mif.mode);
@@ -91,8 +91,8 @@
 		return -EINVAL;
 	}
 
-	/* First, make sure bus is idle */
-	if (wait_timeout(I2C_PNX_TIMEOUT, alg_data)) {
+	/* First, make sure bus is idle, if we are not doing repeated start */
+	if ((!repeated) && (wait_timeout(I2C_PNX_TIMEOUT, alg_data))) {
 		/* Somebody else is monopolizing the bus */
 		dev_err(&alg_data->adapter.dev,
 			"%s: Bus busy. Slave addr = %02x, cntrl = %x, stat = %x\n",
@@ -441,7 +441,7 @@
 i2c_pnx_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
 {
 	struct i2c_msg *pmsg;
-	int rc = 0, completed = 0, i;
+	int rc = 0, completed = 0, i, repeated;
 	struct i2c_pnx_algo_data *alg_data = adap->algo_data;
 	u32 stat = ioread32(I2C_REG_STS(alg_data));
 
@@ -457,6 +457,7 @@
 
 		pmsg = &msgs[i];
 		addr = pmsg->addr;
+		repeated = 0;
 
 		if (pmsg->flags & I2C_M_TEN) {
 			dev_err(&alg_data->adapter.dev,
@@ -481,16 +482,23 @@
 		/* initialize the completion var */
 		init_completion(&alg_data->mif.complete);
 
-		/* Enable master interrupt */
-		iowrite32(ioread32(I2C_REG_CTL(alg_data)) | mcntrl_afie |
-				mcntrl_naie | mcntrl_drmie,
-			  I2C_REG_CTL(alg_data));
+		/* 
+		 * If this is not the only message, then we need to
+		 * send repeated start, but only if the flag allows it
+		 */
+		if((i > 0) && !(pmsg->flags & I2C_M_NOSTART))
+			repeated = 1;
 
 		/* Put start-code and slave-address on the bus. */
-		rc = i2c_pnx_start(addr, alg_data);
+		rc = i2c_pnx_start(addr, alg_data, repeated);
 		if (rc < 0)
 			break;
 
+		/* Enable master interrupt */
+		iowrite32(ioread32(I2C_REG_CTL(alg_data)) | mcntrl_afie |
+				mcntrl_naie | mcntrl_drmie,
+			  I2C_REG_CTL(alg_data));
+
 		/* Wait for completion */
 		wait_for_completion(&alg_data->mif.complete);
 

Current code in i2c-pnx.c doesn't support sending messages with restart condition in it, for example:
S - (address | W) - Sr - (address | R) - ... - P

This patch adds this to the current code. It has been tested on a custom board with LPC3152 CPU (similar to Embedded Artists EA313x board) with uBlox GPS receiver connected to I2C bus. It has been running over the night without errors.

i2c-pnx.c renamed from i2c-pnx-orig.c +17 -9
@@ -78,7 +78,7 @@
  * Generate a START signal in the desired mode.
  */
 static int i2c_pnx_start(unsigned char slave_addr,
-	struct i2c_pnx_algo_data *alg_data)
+	struct i2c_pnx_algo_data *alg_data, int repeated)
 {
 	dev_dbg(&alg_data->adapter.dev, "%s(): addr 0x%x mode %d\n", __func__,
 		slave_addr, alg_data->mif.mode);
@@ -91,8 +91,8 @@
 		return -EINVAL;
 	}
 
-	/* First, make sure bus is idle */
-	if (wait_timeout(I2C_PNX_TIMEOUT, alg_data)) {
+	/* First, make sure bus is idle, if we are not doing repeated start */
+	if ((!repeated) && (wait_timeout(I2C_PNX_TIMEOUT, alg_data))) {
 		/* Somebody else is monopolizing the bus */
 		dev_err(&alg_data->adapter.dev,
 			"%s: Bus busy. Slave addr = %02x, cntrl = %x, stat = %x\n",
@@ -441,7 +441,7 @@
 i2c_pnx_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
 {
 	struct i2c_msg *pmsg;
-	int rc = 0, completed = 0, i;
+	int rc = 0, completed = 0, i, repeated;
 	struct i2c_pnx_algo_data *alg_data = adap->algo_data;
 	u32 stat = ioread32(I2C_REG_STS(alg_data));
 
@@ -457,6 +457,7 @@
 
 		pmsg = &msgs[i];
 		addr = pmsg->addr;
+		repeated = 0;
 
 		if (pmsg->flags & I2C_M_TEN) {
 			dev_err(&alg_data->adapter.dev,
@@ -481,16 +482,23 @@
 		/* initialize the completion var */
 		init_completion(&alg_data->mif.complete);
 
-		/* Enable master interrupt */
-		iowrite32(ioread32(I2C_REG_CTL(alg_data)) | mcntrl_afie |
-				mcntrl_naie | mcntrl_drmie,
-			  I2C_REG_CTL(alg_data));
+		/* 
+		 * If this is not the only message, then we need to
+		 * send repeated start, but only if the flag allows it
+		 */
+		if((i > 0) && !(pmsg->flags & I2C_M_NOSTART))
+			repeated = 1;
 
 		/* Put start-code and slave-address on the bus. */
-		rc = i2c_pnx_start(addr, alg_data);
+		rc = i2c_pnx_start(addr, alg_data, repeated);
 		if (rc < 0)
 			break;
 
+		/* Enable master interrupt */
+		iowrite32(ioread32(I2C_REG_CTL(alg_data)) | mcntrl_afie |
+				mcntrl_naie | mcntrl_drmie,
+			  I2C_REG_CTL(alg_data));
+
 		/* Wait for completion */
 		wait_for_completion(&alg_data->mif.complete);
 
#13 lore raw
Quoted 40 lines · Matej Kupljen

Kevin,

Quoted 10 lines

Thanks for posting this patch. See comments below.. I'll be happy to test your changes once the following items are clarified.

Quoted 2 lines

The pathch is against 2.6.28.2, but it should apply to the cureent GIT version as well.

This patch needs to be re-based against the latest version of the driver as it has changes that this patch will revert or won't apply to.

I re-based it against latest version of the kernel in GIT and also included your comments in the mail.

Is it O.K. now?

Regards,
Matej

P.S.: Changed the email client. I now attached the file as well as pasted it in-line.

-------------------------------------------------------------------
Current code in i2c-pnx.c doesn't support sending messages with restart condition in it, for example:
S - (address | W) - Sr - (address | R) - ... - P

This patch adds this to the current code. It has been tested on a custom board with LPC3152 CPU (similar to Embedded Artists EA313x board) with uBlox GPS receiver connected to I2C bus. It has been running over the night without errors.

i2c-pnx.c line 81

Quoted 5 lines
i2c-pnx.c renamed from i2c-pnx-orig.c +1 -1
@@ -78,7 +78,7 @@
  * Generate a START signal in the desired mode.
  */
 static int i2c_pnx_start(unsigned char slave_addr,
-	struct i2c_pnx_algo_data *alg_data)
+	struct i2c_pnx_algo_data *alg_data, int repeated)

bool repeated.

i2c-pnx.c line 95

Quoted 11 lines
i2c-pnx.c renamed from i2c-pnx-orig.c +2 -2
 {
 	dev_dbg(&alg_data->adapter.dev, "%s(): addr 0x%x mode %d\n", __func__,
 		slave_addr, alg_data->mif.mode);
@@ -91,8 +91,8 @@
 		return -EINVAL;
 	}
 
-	/* First, make sure bus is idle */
-	if (wait_timeout(I2C_PNX_TIMEOUT, alg_data)) {
+	/* First, make sure bus is idle, if we are not doing repeated start */
+	if ((!repeated) && (wait_timeout(I2C_PNX_TIMEOUT, alg_data))) {

no need for () around !repeated.

i2c-pnx.c line 444

Quoted 9 lines
i2c-pnx.c renamed from i2c-pnx-orig.c +1 -1
 		/* Somebody else is monopolizing the bus */
 		dev_err(&alg_data->adapter.dev,
 			"%s: Bus busy. Slave addr = %02x, cntrl = %x, stat = %x\n",
@@ -441,7 +441,7 @@
 i2c_pnx_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
 {
 	struct i2c_msg *pmsg;
-	int rc = 0, completed = 0, i;
+	int rc = 0, completed = 0, i, repeated;

see above.

Quoted 14 lines
i2c-pnx.c renamed from i2c-pnx-orig.c +1 -0
 	struct i2c_pnx_algo_data *alg_data = adap->algo_data;
 	u32 stat = ioread32(I2C_REG_STS(alg_data));
 
@@ -457,6 +457,7 @@
 
 		pmsg = &msgs[i];
 		addr = pmsg->addr;
+		repeated = 0;
 
 		if (pmsg->flags & I2C_M_TEN) {
 			dev_err(&alg_data->adapter.dev,
@@ -481,16 +482,23 @@
 		/* initialize the completion var */
 		init_completion(&alg_data->mif.complete);

i2c-pnx.c line 489

Quoted 10 lines
i2c-pnx.c renamed from i2c-pnx-orig.c +5 -4
 
-		/* Enable master interrupt */
-		iowrite32(ioread32(I2C_REG_CTL(alg_data)) | mcntrl_afie |
-				mcntrl_naie | mcntrl_drmie,
-			  I2C_REG_CTL(alg_data));
+		/* 
+		 * If this is not the only message, then we need to
+		 * send repeated start, but only if the flag allows it
+		 */
+		if((i > 0) && !(pmsg->flags & I2C_M_NOSTART))

no need for () around i > 0

Quoted 15 lines
i2c-pnx.c renamed from i2c-pnx-orig.c +7 -1
+			repeated = 1;
 
 		/* Put start-code and slave-address on the bus. */
-		rc = i2c_pnx_start(addr, alg_data);
+		rc = i2c_pnx_start(addr, alg_data, repeated);
 		if (rc < 0)
 			break;
 
+		/* Enable master interrupt */
+		iowrite32(ioread32(I2C_REG_CTL(alg_data)) | mcntrl_afie |
+				mcntrl_naie | mcntrl_drmie,
+			  I2C_REG_CTL(alg_data));
+
 		/* Wait for completion */
 		wait_for_completion(&alg_data->mif.complete);
Signature · 3 lines
Ben Dooks, [email protected], http://www.fluff.org/ben/

Large Hadron Colada: A large Pina Colada that makes the universe disappear.
#15 lore raw
In reply to a message not in the archive

Ben,

Quoted 4 lines

Review

Quoted 2 lines
(quoted diff) +1 -1
-	struct i2c_pnx_algo_data *alg_data)
+	struct i2c_pnx_algo_data *alg_data, int repeated)

bool repeated.

I don't agree.
Since this is C and C++, we do not have a bool keyword. And, there is no variable declared as bool in the source file this is why and defined this as int and not as bool.

Quoted 3 lines

Review

Quoted 1 line
(quoted diff) +1 -0
+	if ((!repeated) && (wait_timeout(I2C_PNX_TIMEOUT, alg_data))) {

no need for () around !repeated.

Agreed, but for the clarity sake.

Quoted 3 lines

Review

Quoted 1 line
(quoted diff) +1 -0
+		if((i > 0) && !(pmsg->flags & I2C_M_NOSTART))

no need for () around i > 0

Same as above. I my opinion it is better to use more parentheses then fewer, since you do not rely on compiler implementation.

Regards,
Matej

#18 lore raw
In reply to a message not in the archive
Quoted 11 lines · On Thu, Feb 03, 2011 at 08:54:30AM +0100…

Ben,

Quoted 4 lines

Review

Quoted 2 lines
(quoted diff) +1 -1
-	struct i2c_pnx_algo_data *alg_data)
+	struct i2c_pnx_algo_data *alg_data, int repeated)

bool repeated.

I don't agree.
Since this is C and C++, we do not have a bool keyword. And, there is no variable declared as bool in the source file this is why and defined this as int and not as bool.

the kernel has this.

Quoted 5 lines
Quoted 3 lines

Review

Quoted 1 line
(quoted diff) +1 -0
+	if ((!repeated) && (wait_timeout(I2C_PNX_TIMEOUT, alg_data))) {

no need for () around !repeated.

Agreed, but for the clarity sake.

Makes it less clear, there's more on each line.

Quoted 15 lines
Quoted 3 lines

Review

Quoted 1 line
(quoted diff) +1 -0
+		if((i > 0) && !(pmsg->flags & I2C_M_NOSTART))

no need for () around i > 0

Same as above. I my opinion it is better to use more parentheses then fewer, since you do not rely on compiler implementation.

Regards,
Matej

--
To unsubscribe from this list: send the line "unsubscribe linux-i2c" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

Signature · 3 lines
Ben Dooks, [email protected], http://www.fluff.org/ben/

Large Hadron Colada: A large Pina Colada that makes the universe disappear.