1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2012 Boundary Devices Inc.
7 #include <asm/arch/clock.h>
8 #include <asm/arch/imx-regs.h>
9 #include <linux/errno.h>
11 #include <asm/mach-imx/mxc_i2c.h>
14 int force_idle_bus(void *priv)
18 ulong elapsed, start_time;
19 struct i2c_pads_info *p = (struct i2c_pads_info *)priv;
22 gpio_direction_input(p->sda.gp);
23 gpio_direction_input(p->scl.gp);
25 imx_iomux_v3_setup_pad(p->sda.gpio_mode);
26 imx_iomux_v3_setup_pad(p->scl.gpio_mode);
28 sda = gpio_get_value(p->sda.gp);
29 scl = gpio_get_value(p->scl.gp);
31 goto exit; /* Bus is idle already */
33 printf("%s: sda=%d scl=%d sda.gp=0x%x scl.gp=0x%x\n", __func__,
34 sda, scl, p->sda.gp, p->scl.gp);
35 /* Send high and low on the SCL line */
36 for (i = 0; i < 9; i++) {
37 gpio_direction_output(p->scl.gp, 0);
39 gpio_direction_input(p->scl.gp);
42 start_time = get_timer(0);
44 sda = gpio_get_value(p->sda.gp);
45 scl = gpio_get_value(p->scl.gp);
49 elapsed = get_timer(start_time);
50 if (elapsed > (CONFIG_SYS_HZ / 5)) { /* .2 seconds */
52 printf("%s: failed to clear bus, sda=%d scl=%d\n",
58 imx_iomux_v3_setup_pad(p->sda.i2c_mode);
59 imx_iomux_v3_setup_pad(p->scl.i2c_mode);
63 static void * const i2c_bases[] = {
64 (void *)I2C1_BASE_ADDR,
65 (void *)I2C2_BASE_ADDR,
67 (void *)I2C3_BASE_ADDR,
70 (void *)I2C4_BASE_ADDR,
74 /* i2c_index can be from 0 - 3 */
75 int setup_i2c(unsigned i2c_index, int speed, int slave_addr,
76 struct i2c_pads_info *p)
81 if (i2c_index >= ARRAY_SIZE(i2c_bases))
84 snprintf(name, sizeof(name), "i2c_sda%01d", i2c_index);
85 ret = gpio_request(p->sda.gp, name);
89 snprintf(name, sizeof(name), "i2c_scl%01d", i2c_index);
90 ret = gpio_request(p->scl.gp, name);
94 /* Enable i2c clock */
95 ret = enable_i2c_clk(1, i2c_index);
99 /* Make sure bus is idle */
100 ret = force_idle_bus(p);
104 #ifndef CONFIG_DM_I2C
105 bus_i2c_init(i2c_index, speed, slave_addr, force_idle_bus, p);
112 gpio_free(p->scl.gp);
114 gpio_free(p->sda.gp);