#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>
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;
+ int 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;
}
/*
writeb(0, &i2c_regs->i2cr);
}
-/*
- * 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 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;
}
*/
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);
- if (ret)
- return ret;
-
+ ret = tx_byte(i2c_regs, chip << 1);
i2c_imx_stop();
-
return ret;
}
return ret;
/* write slave address */
- ret = i2c_imx_set_chip_addr(chip, 0);
- if (ret)
+ ret = tx_byte(i2c_regs, chip << 1);
+ if (ret < 0)
return ret;
ret = i2c_imx_set_reg_addr(addr, alen);
temp |= I2CR_RSTA;
writeb(temp, &i2c_regs->i2cr);
- ret = i2c_imx_set_chip_addr(chip, 1);
- if (ret)
+ ret = tx_byte(i2c_regs, (chip << 1) | 1);
+ if (ret < 0)
return ret;
/* setup bus to read data */
return ret;
/* write slave address */
- ret = i2c_imx_set_chip_addr(chip, 0);
- if (ret)
+ ret = tx_byte(i2c_regs, chip << 1);
+ if (ret < 0)
return ret;
ret = i2c_imx_set_reg_addr(addr, alen);
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;
}