2 * Copyright (C) 2012 Boundary Devices Inc.
4 * SPDX-License-Identifier: GPL-2.0+
8 #include <asm/arch/clock.h>
9 #include <asm/arch/imx-regs.h>
10 #include <asm/errno.h>
12 #include <asm/imx-common/mxc_i2c.h>
15 static int force_idle_bus(void *priv)
19 ulong elapsed, start_time;
20 struct i2c_pads_info *p = (struct i2c_pads_info *)priv;
23 gpio_direction_input(p->sda.gp);
24 gpio_direction_input(p->scl.gp);
26 imx_iomux_v3_setup_pad(p->sda.gpio_mode);
27 imx_iomux_v3_setup_pad(p->scl.gpio_mode);
29 sda = gpio_get_value(p->sda.gp);
30 scl = gpio_get_value(p->scl.gp);
32 goto exit; /* Bus is idle already */
34 printf("%s: sda=%d scl=%d sda.gp=0x%x scl.gp=0x%x\n", __func__,
35 sda, scl, p->sda.gp, p->scl.gp);
36 /* Send high and low on the SCL line */
37 for (i = 0; i < 9; i++) {
38 gpio_direction_output(p->scl.gp, 0);
40 gpio_direction_input(p->scl.gp);
43 start_time = get_timer(0);
45 sda = gpio_get_value(p->sda.gp);
46 scl = gpio_get_value(p->scl.gp);
50 elapsed = get_timer(start_time);
51 if (elapsed > (CONFIG_SYS_HZ / 5)) { /* .2 seconds */
53 printf("%s: failed to clear bus, sda=%d scl=%d\n",
59 imx_iomux_v3_setup_pad(p->sda.i2c_mode);
60 imx_iomux_v3_setup_pad(p->scl.i2c_mode);
64 static void * const i2c_bases[] = {
65 (void *)I2C1_BASE_ADDR,
66 (void *)I2C2_BASE_ADDR,
68 (void *)I2C3_BASE_ADDR,
72 /* i2c_index can be from 0 - 2 */
73 int setup_i2c(unsigned i2c_index, int speed, int slave_addr,
74 struct i2c_pads_info *p)
79 if (i2c_index >= ARRAY_SIZE(i2c_bases))
87 sprintf(name1, "i2c_sda%d", i2c_index);
88 sprintf(name2, "i2c_scl%d", i2c_index);
89 ret = gpio_request(p->sda.gp, name1);
93 ret = gpio_request(p->scl.gp, name2);
97 /* Enable i2c clock */
98 ret = enable_i2c_clk(1, i2c_index);
102 /* Make sure bus is idle */
103 ret = force_idle_bus(p);
107 bus_i2c_init(i2c_bases[i2c_index], speed, slave_addr,
114 gpio_free(p->scl.gp);
116 gpio_free(p->sda.gp);