Lines Matching +full:i2c +full:- +full:sda +full:- +full:delay
5 * This file is based on: drivers/i2c/soft-i2c.c,
6 * with added driver-model support and code cleanup.
11 #include <i2c.h>
29 * udelay - delay [us] between GPIO toggle operations,
30 * which is 1/4 of I2C speed clock period.
33 /* sda, scl */
37 static int i2c_gpio_sda_get(struct gpio_desc *sda) in i2c_gpio_sda_get() argument
39 return dm_gpio_get_value(sda); in i2c_gpio_sda_get()
42 static void i2c_gpio_sda_set(struct gpio_desc *sda, int bit) in i2c_gpio_sda_set() argument
45 dm_gpio_set_dir_flags(sda, GPIOD_IS_IN); in i2c_gpio_sda_set()
47 dm_gpio_set_dir_flags(sda, GPIOD_IS_OUT); in i2c_gpio_sda_set()
59 static void i2c_gpio_write_bit(struct gpio_desc *scl, struct gpio_desc *sda, in i2c_gpio_write_bit() argument
60 int delay, uchar bit) in i2c_gpio_write_bit() argument
63 udelay(delay); in i2c_gpio_write_bit()
64 i2c_gpio_sda_set(sda, bit); in i2c_gpio_write_bit()
65 udelay(delay); in i2c_gpio_write_bit()
67 udelay(2 * delay); in i2c_gpio_write_bit()
70 static int i2c_gpio_read_bit(struct gpio_desc *scl, struct gpio_desc *sda, in i2c_gpio_read_bit() argument
71 int delay) in i2c_gpio_read_bit() argument
76 udelay(delay); in i2c_gpio_read_bit()
77 value = i2c_gpio_sda_get(sda); in i2c_gpio_read_bit()
78 udelay(delay); in i2c_gpio_read_bit()
80 udelay(2 * delay); in i2c_gpio_read_bit()
85 /* START: High -> Low on SDA while SCL is High */
86 static void i2c_gpio_send_start(struct gpio_desc *scl, struct gpio_desc *sda, in i2c_gpio_send_start() argument
87 int delay) in i2c_gpio_send_start() argument
89 udelay(delay); in i2c_gpio_send_start()
90 i2c_gpio_sda_set(sda, 1); in i2c_gpio_send_start()
91 udelay(delay); in i2c_gpio_send_start()
93 udelay(delay); in i2c_gpio_send_start()
94 i2c_gpio_sda_set(sda, 0); in i2c_gpio_send_start()
95 udelay(delay); in i2c_gpio_send_start()
98 /* STOP: Low -> High on SDA while SCL is High */
99 static void i2c_gpio_send_stop(struct gpio_desc *scl, struct gpio_desc *sda, in i2c_gpio_send_stop() argument
100 int delay) in i2c_gpio_send_stop() argument
103 udelay(delay); in i2c_gpio_send_stop()
104 i2c_gpio_sda_set(sda, 0); in i2c_gpio_send_stop()
105 udelay(delay); in i2c_gpio_send_stop()
107 udelay(delay); in i2c_gpio_send_stop()
108 i2c_gpio_sda_set(sda, 1); in i2c_gpio_send_stop()
109 udelay(delay); in i2c_gpio_send_stop()
113 static void i2c_gpio_send_ack(struct gpio_desc *scl, struct gpio_desc *sda, in i2c_gpio_send_ack() argument
114 int delay, int ack) in i2c_gpio_send_ack() argument
116 i2c_gpio_write_bit(scl, sda, delay, ack); in i2c_gpio_send_ack()
118 udelay(delay); in i2c_gpio_send_ack()
126 static void i2c_gpio_send_reset(struct gpio_desc *scl, struct gpio_desc *sda, in i2c_gpio_send_reset() argument
127 int delay) in i2c_gpio_send_reset() argument
132 i2c_gpio_write_bit(scl, sda, delay, 1); in i2c_gpio_send_reset()
134 i2c_gpio_send_stop(scl, sda, delay); in i2c_gpio_send_reset()
137 /* Set sda high with low clock, before reading slave data */
138 static void i2c_gpio_sda_high(struct gpio_desc *scl, struct gpio_desc *sda, in i2c_gpio_sda_high() argument
139 int delay) in i2c_gpio_sda_high() argument
142 udelay(delay); in i2c_gpio_sda_high()
143 i2c_gpio_sda_set(sda, 1); in i2c_gpio_sda_high()
144 udelay(delay); in i2c_gpio_sda_high()
148 static int i2c_gpio_write_byte(struct gpio_desc *scl, struct gpio_desc *sda, in i2c_gpio_write_byte() argument
149 int delay, uchar data) in i2c_gpio_write_byte() argument
155 i2c_gpio_write_bit(scl, sda, delay, data & 0x80); in i2c_gpio_write_byte()
159 udelay(delay); in i2c_gpio_write_byte()
162 i2c_gpio_sda_high(scl, sda, delay); in i2c_gpio_write_byte()
163 nack = i2c_gpio_read_bit(scl, sda, delay); in i2c_gpio_write_byte()
172 static uchar i2c_gpio_read_byte(struct gpio_desc *scl, struct gpio_desc *sda, in i2c_gpio_read_byte() argument
173 int delay, int ack) in i2c_gpio_read_byte() argument
178 i2c_gpio_sda_high(scl, sda, delay); in i2c_gpio_read_byte()
182 data |= i2c_gpio_read_bit(scl, sda, delay); in i2c_gpio_read_byte()
184 i2c_gpio_send_ack(scl, sda, delay, ack); in i2c_gpio_read_byte()
190 int i2c_send_slave_addr(struct gpio_desc *scl, struct gpio_desc *sda, int delay, in i2c_send_slave_addr() argument
193 i2c_gpio_send_start(scl, sda, delay); in i2c_send_slave_addr()
195 if (i2c_gpio_write_byte(scl, sda, delay, chip)) { in i2c_send_slave_addr()
196 i2c_gpio_send_stop(scl, sda, delay); in i2c_send_slave_addr()
197 return -EIO; in i2c_send_slave_addr()
207 struct gpio_desc *scl = &bus->gpios[PIN_SCL]; in i2c_gpio_write_data()
208 struct gpio_desc *sda = &bus->gpios[PIN_SDA]; in i2c_gpio_write_data() local
209 unsigned int delay = bus->udelay; in i2c_gpio_write_data() local
214 if (i2c_send_slave_addr(scl, sda, delay, chip << 1)) { in i2c_gpio_write_data()
216 return -EIO; in i2c_gpio_write_data()
219 while (len-- > 0) { in i2c_gpio_write_data()
220 if (i2c_gpio_write_byte(scl, sda, delay, *buffer++)) in i2c_gpio_write_data()
225 i2c_gpio_send_stop(scl, sda, delay); in i2c_gpio_write_data()
229 if (i2c_send_slave_addr(scl, sda, delay, (chip << 1) | 0x1)) { in i2c_gpio_write_data()
231 return -EIO; in i2c_gpio_write_data()
240 struct gpio_desc *scl = &bus->gpios[PIN_SCL]; in i2c_gpio_read_data()
241 struct gpio_desc *sda = &bus->gpios[PIN_SDA]; in i2c_gpio_read_data() local
242 unsigned int delay = bus->udelay; in i2c_gpio_read_data() local
246 while (len-- > 0) in i2c_gpio_read_data()
247 *buffer++ = i2c_gpio_read_byte(scl, sda, delay, len == 0); in i2c_gpio_read_data()
249 i2c_gpio_send_stop(scl, sda, delay); in i2c_gpio_read_data()
259 for (; nmsgs > 0; nmsgs--, msg++) { in i2c_gpio_xfer()
262 if (msg->flags & I2C_M_RD) { in i2c_gpio_xfer()
263 ret = i2c_gpio_read_data(bus, msg->addr, msg->buf, in i2c_gpio_xfer()
264 msg->len); in i2c_gpio_xfer()
266 ret = i2c_gpio_write_data(bus, msg->addr, msg->buf, in i2c_gpio_xfer()
267 msg->len, next_is_read); in i2c_gpio_xfer()
271 return -EREMOTEIO; in i2c_gpio_xfer()
280 struct gpio_desc *scl = &bus->gpios[PIN_SCL]; in i2c_gpio_probe()
281 struct gpio_desc *sda = &bus->gpios[PIN_SDA]; in i2c_gpio_probe() local
282 unsigned int delay = bus->udelay; in i2c_gpio_probe() local
285 i2c_gpio_send_start(scl, sda, delay); in i2c_gpio_probe()
286 ret = i2c_gpio_write_byte(scl, sda, delay, (chip << 1) | 0); in i2c_gpio_probe()
287 i2c_gpio_send_stop(scl, sda, delay); in i2c_gpio_probe()
290 __func__, dev->seq, dev->name, chip, chip_flags, ret); in i2c_gpio_probe()
298 struct gpio_desc *scl = &bus->gpios[PIN_SCL]; in i2c_gpio_set_bus_speed()
299 struct gpio_desc *sda = &bus->gpios[PIN_SDA]; in i2c_gpio_set_bus_speed() local
301 bus->udelay = 1000000 / (speed_hz << 2); in i2c_gpio_set_bus_speed()
303 i2c_gpio_send_reset(scl, sda, bus->udelay); in i2c_gpio_set_bus_speed()
311 const void *blob = gd->fdt_blob; in i2c_gpio_ofdata_to_platdata()
315 ret = gpio_request_list_by_name(dev, "gpios", bus->gpios, in i2c_gpio_ofdata_to_platdata()
316 ARRAY_SIZE(bus->gpios), 0); in i2c_gpio_ofdata_to_platdata()
320 bus->udelay = fdtdec_get_int(blob, node, "i2c-gpio,delay-us", in i2c_gpio_ofdata_to_platdata()
325 pr_err("Can't get %s gpios! Error: %d", dev->name, ret); in i2c_gpio_ofdata_to_platdata()
336 { .compatible = "i2c-gpio" },
341 .name = "i2c-gpio",