#include <asm/arch/clock.h>
#include <asm/arch/imx-regs.h>
+#include <i2c.h>
struct mxc_i2c_regs {
uint32_t iadr;
{ 3072, 0x1E }, { 3840, 0x1F }
};
-static u8 clk_div;
-
/*
* Calculate and set proper clock divider
*/
-static void i2c_imx_set_clk(unsigned int rate)
+static uint8_t i2c_imx_get_clk(unsigned int rate)
{
- struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE;
unsigned int i2c_clk_rate;
unsigned int div;
+ u8 clk_div;
#if defined(CONFIG_MX31)
struct clock_control_regs *sc_regs =
;
/* Store divider value */
- writeb(i2c_clk_div[clk_div][1], &i2c_regs->ifdr);
+ return clk_div;
}
/*
*/
void i2c_init(int speed, int unused)
{
- i2c_imx_set_clk(speed);
+ struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE;
+ u8 clk_idx = i2c_imx_get_clk(speed);
+ u8 idx = i2c_clk_div[clk_idx][1];
+
+ /* Store divider value */
+ writeb(idx, &i2c_regs->ifdr);
+
i2c_reset();
}
*/
unsigned int i2c_get_bus_speed(void)
{
+ struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE;
+ u8 clk_idx = readb(&i2c_regs->ifdr);
+ u8 clk_div;
+
+ for (clk_div = 0; i2c_clk_div[clk_div][1] != clk_idx; clk_div++)
+ ;
+
return mxc_get_clock(MXC_IPG_PERCLK) / i2c_clk_div[clk_div][0];
}
struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE;
unsigned int temp = 0;
int result;
+ int speed = i2c_get_bus_speed();
+ u8 clk_idx = i2c_imx_get_clk(speed);
+ u8 idx = i2c_clk_div[clk_idx][1];
- writeb(i2c_clk_div[clk_div][1], &i2c_regs->ifdr);
+ /* Store divider value */
+ writeb(idx, &i2c_regs->ifdr);
/* Enable I2C controller */
writeb(0, &i2c_regs->i2sr);
int i2c_imx_set_reg_addr(uint addr, int alen)
{
struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE;
- int ret;
- int i;
+ int ret = 0;
- for (i = 0; i < (8 * alen); i += 8) {
- writeb((addr >> i) & 0xff, &i2c_regs->i2dr);
+ while (alen--) {
+ writeb((addr >> (alen * 8)) & 0xff, &i2c_regs->i2dr);
ret = i2c_imx_trx_complete();
if (ret)