diff mbox

[U-Boot,03/24] mxc_i2c: create tx_byte function

Message ID 1340338339-11626-3-git-send-email-troy.kisky@boundarydevices.com
State Superseded
Delegated to: Heiko Schocher
Headers show

Commit Message

Troy Kisky June 22, 2012, 4:11 a.m. UTC
Use tx_byte function instead of having 3 copies
of the code.

Signed-off-by: Troy Kisky <troy.kisky@boundarydevices.com>
---
 drivers/i2c/mxc_i2c.c |   72 +++++++++++++++----------------------------------
 1 file changed, 21 insertions(+), 51 deletions(-)

Comments

Marek Vasut June 22, 2012, 4:58 p.m. UTC | #1
Dear Troy Kisky,

> Use tx_byte function instead of having 3 copies
> of the code.
> 
> Signed-off-by: Troy Kisky <troy.kisky@boundarydevices.com>
> ---
>  drivers/i2c/mxc_i2c.c |   72
> +++++++++++++++---------------------------------- 1 file changed, 21
> insertions(+), 51 deletions(-)
> 
> diff --git a/drivers/i2c/mxc_i2c.c b/drivers/i2c/mxc_i2c.c
> index 0b46c9c..0fd508a 100644
> --- a/drivers/i2c/mxc_i2c.c
> +++ b/drivers/i2c/mxc_i2c.c
> @@ -33,6 +33,7 @@
>  #include <common.h>
>  #include <asm/arch/clock.h>
>  #include <asm/arch/imx-regs.h>
> +#include <asm/errno.h>
>  #include <asm/io.h>
>  #include <i2c.h>
> 
> @@ -207,17 +208,21 @@ int i2c_imx_trx_complete(void)
>  		udelay(1);
>  	}
> 
> -	return 1;
> +	return -ETIMEDOUT;
>  }
> 
> -/*
> - * Check if the transaction was ACKed
> - */
> -int i2c_imx_acked(void)
> +static int tx_byte(struct mxc_i2c_regs *i2c_regs, u8 byte)
>  {
> -	struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE;
> +	unsigned ret;
> 
> -	return readb(&i2c_regs->i2sr) & I2SR_RX_NO_AK;
> +	writeb(byte, &i2c_regs->i2dr);
> +	ret = i2c_imx_trx_complete();
> +	if (ret < 0)
> +		return ret;
> +	ret = readb(&i2c_regs->i2sr);
> +	if (ret & I2SR_RX_NO_AK)
> +		return -ENODEV;
> +	return 0;
>  }
> 
>  /*
> @@ -271,30 +276,6 @@ void i2c_imx_stop(void)
>  }
> 
>  /*
> - * Set chip address and access mode
> - *
> - * read = 1: READ access
> - * read = 0: WRITE access
> - */
> -int i2c_imx_set_chip_addr(uchar chip, int read)
> -{
> -	struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE;
> -	int ret;
> -
> -	writeb((chip << 1) | read, &i2c_regs->i2dr);
> -
> -	ret = i2c_imx_trx_complete();
> -	if (ret)
> -		return ret;
> -
> -	ret = i2c_imx_acked();
> -	if (ret)
> -		return ret;
> -
> -	return ret;
> -}
> -
> -/*
>   * Write register address
>   */
>  int i2c_imx_set_reg_addr(uint addr, int alen)
> @@ -303,14 +284,8 @@ int i2c_imx_set_reg_addr(uint addr, int alen)
>  	int ret = 0;
> 
>  	while (alen--) {
> -		writeb((addr >> (alen * 8)) & 0xff, &i2c_regs->i2dr);
> -
> -		ret = i2c_imx_trx_complete();
> -		if (ret)
> -			break;
> -
> -		ret = i2c_imx_acked();
> -		if (ret)
> +		ret =  tx_byte(i2c_regs, (addr >> (alen * 8)) & 0xff);
> +		if (ret < 0)
>  			break;
>  	}
> 
> @@ -322,13 +297,14 @@ int i2c_imx_set_reg_addr(uint addr, int alen)
>   */
>  int i2c_probe(uchar chip)
>  {
> +	struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE;
>  	int ret;
> 
>  	ret = i2c_imx_start();
>  	if (ret)
>  		return ret;
> 
> -	ret = i2c_imx_set_chip_addr(chip, 0);
> +	ret = tx_byte(i2c_regs, chip << 1);
>  	if (ret)
>  		return ret;
> 
> @@ -352,7 +328,7 @@ int i2c_read(uchar chip, uint addr, int alen, uchar
> *buf, int len) return ret;
> 
>  	/* write slave address */
> -	ret = i2c_imx_set_chip_addr(chip, 0);
> +	ret = tx_byte(i2c_regs, chip << 1);
>  	if (ret)
>  		return ret;
> 
> @@ -364,7 +340,7 @@ int i2c_read(uchar chip, uint addr, int alen, uchar
> *buf, int len) temp |= I2CR_RSTA;
>  	writeb(temp, &i2c_regs->i2cr);
> 
> -	ret = i2c_imx_set_chip_addr(chip, 1);
> +	ret = tx_byte(i2c_regs, (chip << 1) | 1);

Isn't this | 1 and | 0 stuff #define-d somewhere? I think there was 
I2C_READ_SOMETHING in i2c.h and I2C_WRITE_SOMETHING...

>  	if (ret)
>  		return ret;
> 

Otherwise
Acked-by: Marek Vasut <marex@denx.de>

Best regards,
Marek Vasut
Troy Kisky June 22, 2012, 11:05 p.m. UTC | #2
On 6/22/2012 9:58 AM, Marek Vasut wrote:
> Dear Troy Kisky,
>
>> Use tx_byte function instead of having 3 copies
>> of the code.
>>
>> Signed-off-by: Troy Kisky <troy.kisky@boundarydevices.com>
>> ---
>>   drivers/i2c/mxc_i2c.c |   72
>> +++++++++++++++---------------------------------- 1 file changed, 21
>> insertions(+), 51 deletions(-)
>>
>> diff --git a/drivers/i2c/mxc_i2c.c b/drivers/i2c/mxc_i2c.c
>> index 0b46c9c..0fd508a 100644
>> --- a/drivers/i2c/mxc_i2c.c
>> +++ b/drivers/i2c/mxc_i2c.c
>> @@ -33,6 +33,7 @@
>>   #include <common.h>
>>   #include <asm/arch/clock.h>
>>   #include <asm/arch/imx-regs.h>
>> +#include <asm/errno.h>
>>   #include <asm/io.h>
>>   #include <i2c.h>
>>
>> @@ -207,17 +208,21 @@ int i2c_imx_trx_complete(void)
>>   		udelay(1);
>>   	}
>>
>> -	return 1;
>> +	return -ETIMEDOUT;
>>   }
>>
>> -/*
>> - * Check if the transaction was ACKed
>> - */
>> -int i2c_imx_acked(void)
>> +static int tx_byte(struct mxc_i2c_regs *i2c_regs, u8 byte)
>>   {
>> -	struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE;
>> +	unsigned ret;
>>
>> -	return readb(&i2c_regs->i2sr) & I2SR_RX_NO_AK;
>> +	writeb(byte, &i2c_regs->i2dr);
>> +	ret = i2c_imx_trx_complete();
>> +	if (ret < 0)
>> +		return ret;
>> +	ret = readb(&i2c_regs->i2sr);
>> +	if (ret & I2SR_RX_NO_AK)
>> +		return -ENODEV;
>> +	return 0;
>>   }
>>
>>   /*
>> @@ -271,30 +276,6 @@ void i2c_imx_stop(void)
>>   }
>>
>>   /*
>> - * Set chip address and access mode
>> - *
>> - * read = 1: READ access
>> - * read = 0: WRITE access
>> - */
>> -int i2c_imx_set_chip_addr(uchar chip, int read)
>> -{
>> -	struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE;
>> -	int ret;
>> -
>> -	writeb((chip << 1) | read, &i2c_regs->i2dr);
>> -
>> -	ret = i2c_imx_trx_complete();
>> -	if (ret)
>> -		return ret;
>> -
>> -	ret = i2c_imx_acked();
>> -	if (ret)
>> -		return ret;
>> -
>> -	return ret;
>> -}
>> -
>> -/*
>>    * Write register address
>>    */
>>   int i2c_imx_set_reg_addr(uint addr, int alen)
>> @@ -303,14 +284,8 @@ int i2c_imx_set_reg_addr(uint addr, int alen)
>>   	int ret = 0;
>>
>>   	while (alen--) {
>> -		writeb((addr >> (alen * 8)) & 0xff, &i2c_regs->i2dr);
>> -
>> -		ret = i2c_imx_trx_complete();
>> -		if (ret)
>> -			break;
>> -
>> -		ret = i2c_imx_acked();
>> -		if (ret)
>> +		ret =  tx_byte(i2c_regs, (addr >> (alen * 8)) & 0xff);
>> +		if (ret < 0)
>>   			break;
>>   	}
>>
>> @@ -322,13 +297,14 @@ int i2c_imx_set_reg_addr(uint addr, int alen)
>>    */
>>   int i2c_probe(uchar chip)
>>   {
>> +	struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE;
>>   	int ret;
>>
>>   	ret = i2c_imx_start();
>>   	if (ret)
>>   		return ret;
>>
>> -	ret = i2c_imx_set_chip_addr(chip, 0);
>> +	ret = tx_byte(i2c_regs, chip << 1);
>>   	if (ret)
>>   		return ret;
>>
>> @@ -352,7 +328,7 @@ int i2c_read(uchar chip, uint addr, int alen, uchar
>> *buf, int len) return ret;
>>
>>   	/* write slave address */
>> -	ret = i2c_imx_set_chip_addr(chip, 0);
>> +	ret = tx_byte(i2c_regs, chip << 1);
>>   	if (ret)
>>   		return ret;
>>
>> @@ -364,7 +340,7 @@ int i2c_read(uchar chip, uint addr, int alen, uchar
>> *buf, int len) temp |= I2CR_RSTA;
>>   	writeb(temp, &i2c_regs->i2cr);
>>
>> -	ret = i2c_imx_set_chip_addr(chip, 1);
>> +	ret = tx_byte(i2c_regs, (chip << 1) | 1);
> Isn't this | 1 and | 0 stuff #define-d somewhere? I think there was
> I2C_READ_SOMETHING in i2c.h and I2C_WRITE_SOMETHING...

I could not find what you are referring to. All drivers in i2c seem to 
use "| 1"
"| dir" and I2C_READ_BIT, I2C_WRITE_BIT

#define I2C_READ_BIT  1
#define I2C_WRITE_BIT 0

in fsl_i2c.c


But these are not defined in a header file.

>>   	if (ret)
>>   		return ret;
>>
> Otherwise
> Acked-by: Marek Vasut <marex@denx.de>
>
> Best regards,
> Marek Vasut
>
Marek Vasut June 23, 2012, 1:51 a.m. UTC | #3
Dear Troy Kisky,

[...]

> >> @@ -364,7 +340,7 @@ int i2c_read(uchar chip, uint addr, int alen, uchar
> >> *buf, int len) temp |= I2CR_RSTA;
> >> 
> >>   	writeb(temp, &i2c_regs->i2cr);
> >> 
> >> -	ret = i2c_imx_set_chip_addr(chip, 1);
> >> +	ret = tx_byte(i2c_regs, (chip << 1) | 1);
> > 
> > Isn't this | 1 and | 0 stuff #define-d somewhere? I think there was
> > I2C_READ_SOMETHING in i2c.h and I2C_WRITE_SOMETHING...
> 
> I could not find what you are referring to. All drivers in i2c seem to
> use "| 1"
> "| dir" and I2C_READ_BIT, I2C_WRITE_BIT
> 
> #define I2C_READ_BIT  1
> #define I2C_WRITE_BIT 0
> 
> in fsl_i2c.c
> 
> 
> But these are not defined in a header file.

You're right. I must have mistaken them for something else. Sorry!

> >>   	if (ret)
> >>   	
> >>   		return ret;
> > 
> > Otherwise
> > Acked-by: Marek Vasut <marex@denx.de>
> > 
> > Best regards,
> > Marek Vasut

Best regards,
Marek Vasut
diff mbox

Patch

diff --git a/drivers/i2c/mxc_i2c.c b/drivers/i2c/mxc_i2c.c
index 0b46c9c..0fd508a 100644
--- a/drivers/i2c/mxc_i2c.c
+++ b/drivers/i2c/mxc_i2c.c
@@ -33,6 +33,7 @@ 
 #include <common.h>
 #include <asm/arch/clock.h>
 #include <asm/arch/imx-regs.h>
+#include <asm/errno.h>
 #include <asm/io.h>
 #include <i2c.h>
 
@@ -207,17 +208,21 @@  int i2c_imx_trx_complete(void)
 		udelay(1);
 	}
 
-	return 1;
+	return -ETIMEDOUT;
 }
 
-/*
- * Check if the transaction was ACKed
- */
-int i2c_imx_acked(void)
+static int tx_byte(struct mxc_i2c_regs *i2c_regs, u8 byte)
 {
-	struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE;
+	unsigned ret;
 
-	return readb(&i2c_regs->i2sr) & I2SR_RX_NO_AK;
+	writeb(byte, &i2c_regs->i2dr);
+	ret = i2c_imx_trx_complete();
+	if (ret < 0)
+		return ret;
+	ret = readb(&i2c_regs->i2sr);
+	if (ret & I2SR_RX_NO_AK)
+		return -ENODEV;
+	return 0;
 }
 
 /*
@@ -271,30 +276,6 @@  void i2c_imx_stop(void)
 }
 
 /*
- * Set chip address and access mode
- *
- * read = 1: READ access
- * read = 0: WRITE access
- */
-int i2c_imx_set_chip_addr(uchar chip, int read)
-{
-	struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE;
-	int ret;
-
-	writeb((chip << 1) | read, &i2c_regs->i2dr);
-
-	ret = i2c_imx_trx_complete();
-	if (ret)
-		return ret;
-
-	ret = i2c_imx_acked();
-	if (ret)
-		return ret;
-
-	return ret;
-}
-
-/*
  * Write register address
  */
 int i2c_imx_set_reg_addr(uint addr, int alen)
@@ -303,14 +284,8 @@  int i2c_imx_set_reg_addr(uint addr, int alen)
 	int ret = 0;
 
 	while (alen--) {
-		writeb((addr >> (alen * 8)) & 0xff, &i2c_regs->i2dr);
-
-		ret = i2c_imx_trx_complete();
-		if (ret)
-			break;
-
-		ret = i2c_imx_acked();
-		if (ret)
+		ret =  tx_byte(i2c_regs, (addr >> (alen * 8)) & 0xff);
+		if (ret < 0)
 			break;
 	}
 
@@ -322,13 +297,14 @@  int i2c_imx_set_reg_addr(uint addr, int alen)
  */
 int i2c_probe(uchar chip)
 {
+	struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE;
 	int ret;
 
 	ret = i2c_imx_start();
 	if (ret)
 		return ret;
 
-	ret = i2c_imx_set_chip_addr(chip, 0);
+	ret = tx_byte(i2c_regs, chip << 1);
 	if (ret)
 		return ret;
 
@@ -352,7 +328,7 @@  int i2c_read(uchar chip, uint addr, int alen, uchar *buf, int len)
 		return ret;
 
 	/* write slave address */
-	ret = i2c_imx_set_chip_addr(chip, 0);
+	ret = tx_byte(i2c_regs, chip << 1);
 	if (ret)
 		return ret;
 
@@ -364,7 +340,7 @@  int i2c_read(uchar chip, uint addr, int alen, uchar *buf, int len)
 	temp |= I2CR_RSTA;
 	writeb(temp, &i2c_regs->i2cr);
 
-	ret = i2c_imx_set_chip_addr(chip, 1);
+	ret = tx_byte(i2c_regs, (chip << 1) | 1);
 	if (ret)
 		return ret;
 
@@ -419,7 +395,7 @@  int i2c_write(uchar chip, uint addr, int alen, uchar *buf, int len)
 		return ret;
 
 	/* write slave address */
-	ret = i2c_imx_set_chip_addr(chip, 0);
+	ret = tx_byte(i2c_regs, chip << 1);
 	if (ret)
 		return ret;
 
@@ -428,14 +404,8 @@  int i2c_write(uchar chip, uint addr, int alen, uchar *buf, int len)
 		return ret;
 
 	for (i = 0; i < len; i++) {
-		writeb(buf[i], &i2c_regs->i2dr);
-
-		ret = i2c_imx_trx_complete();
-		if (ret)
-			return ret;
-
-		ret = i2c_imx_acked();
-		if (ret)
+		ret =  tx_byte(i2c_regs, buf[i]);
+		if (ret < 0)
 			return ret;
 	}