2 * (C) Copyright 2015, Samsung Electronics
3 * Przemyslaw Marczak <p.marczak@samsung.com>
5 * This file is based on: drivers/i2c/soft-i2c.c,
6 * with added driver-model support and code cleanup.
14 #include <linux/delay.h>
16 #define DEFAULT_UDELAY 5
29 * udelay - delay [us] between GPIO toggle operations,
30 * which is 1/4 of I2C speed clock period.
34 struct gpio_desc gpios[PIN_COUNT];
36 int (*get_sda)(struct i2c_gpio_bus *bus);
37 void (*set_sda)(struct i2c_gpio_bus *bus, int bit);
38 void (*set_scl)(struct i2c_gpio_bus *bus, int bit);
41 static int i2c_gpio_sda_get(struct i2c_gpio_bus *bus)
43 struct gpio_desc *sda = &bus->gpios[PIN_SDA];
45 return dm_gpio_get_value(sda);
48 static void i2c_gpio_sda_set(struct i2c_gpio_bus *bus, int bit)
50 struct gpio_desc *sda = &bus->gpios[PIN_SDA];
57 dm_gpio_clrset_flags(sda, GPIOD_MASK_DIR, flags);
60 static void i2c_gpio_scl_set(struct i2c_gpio_bus *bus, int bit)
62 struct gpio_desc *scl = &bus->gpios[PIN_SCL];
66 dm_gpio_clrset_flags(scl, GPIOD_MASK_DIR, GPIOD_IS_IN);
67 while (!dm_gpio_get_value(scl) && count++ < 100000)
70 if (!dm_gpio_get_value(scl))
71 pr_err("timeout waiting on slave to release scl\n");
73 dm_gpio_clrset_flags(scl, GPIOD_MASK_DIR, GPIOD_IS_OUT);
77 /* variant for output only gpios which cannot support clock stretching */
78 static void i2c_gpio_scl_set_output_only(struct i2c_gpio_bus *bus, int bit)
80 struct gpio_desc *scl = &bus->gpios[PIN_SCL];
81 ulong flags = GPIOD_IS_OUT;
84 flags |= GPIOD_IS_OUT_ACTIVE;
85 dm_gpio_clrset_flags(scl, GPIOD_MASK_DIR, flags);
88 static void i2c_gpio_write_bit(struct i2c_gpio_bus *bus, int delay, uchar bit)
92 bus->set_sda(bus, bit);
98 static int i2c_gpio_read_bit(struct i2c_gpio_bus *bus, int delay)
102 bus->set_scl(bus, 1);
104 value = bus->get_sda(bus);
106 bus->set_scl(bus, 0);
112 /* START: High -> Low on SDA while SCL is High */
113 static void i2c_gpio_send_start(struct i2c_gpio_bus *bus, int delay)
116 bus->set_sda(bus, 1);
118 bus->set_scl(bus, 1);
120 bus->set_sda(bus, 0);
124 /* STOP: Low -> High on SDA while SCL is High */
125 static void i2c_gpio_send_stop(struct i2c_gpio_bus *bus, int delay)
127 bus->set_scl(bus, 0);
129 bus->set_sda(bus, 0);
131 bus->set_scl(bus, 1);
133 bus->set_sda(bus, 1);
137 /* ack should be I2C_ACK or I2C_NOACK */
138 static void i2c_gpio_send_ack(struct i2c_gpio_bus *bus, int delay, int ack)
140 i2c_gpio_write_bit(bus, delay, ack);
141 bus->set_scl(bus, 0);
146 * Send a reset sequence consisting of 9 clocks with the data signal high
147 * to clock any confused device back into an idle state. Also send a
148 * <stop> at the end of the sequence for belts & suspenders.
150 static void i2c_gpio_send_reset(struct i2c_gpio_bus *bus, int delay)
154 for (j = 0; j < 9; j++)
155 i2c_gpio_write_bit(bus, delay, 1);
157 i2c_gpio_send_stop(bus, delay);
160 /* Set sda high with low clock, before reading slave data */
161 static void i2c_gpio_sda_high(struct i2c_gpio_bus *bus, int delay)
163 bus->set_scl(bus, 0);
165 bus->set_sda(bus, 1);
169 /* Send 8 bits and look for an acknowledgement */
170 static int i2c_gpio_write_byte(struct i2c_gpio_bus *bus, int delay, uchar data)
175 for (j = 0; j < 8; j++) {
176 i2c_gpio_write_bit(bus, delay, data & 0x80);
182 /* Look for an <ACK>(negative logic) and return it */
183 i2c_gpio_sda_high(bus, delay);
184 nack = i2c_gpio_read_bit(bus, delay);
186 return nack; /* not a nack is an ack */
190 * if ack == I2C_ACK, ACK the byte so can continue reading, else
191 * send I2C_NOACK to end the read.
193 static uchar i2c_gpio_read_byte(struct i2c_gpio_bus *bus, int delay, int ack)
198 i2c_gpio_sda_high(bus, delay);
200 for (j = 0; j < 8; j++) {
202 data |= i2c_gpio_read_bit(bus, delay);
204 i2c_gpio_send_ack(bus, delay, ack);
209 /* send start and the slave chip address */
210 int i2c_send_slave_addr(struct i2c_gpio_bus *bus, int delay, uchar chip)
212 i2c_gpio_send_start(bus, delay);
214 if (i2c_gpio_write_byte(bus, delay, chip)) {
215 i2c_gpio_send_stop(bus, delay);
222 static int i2c_gpio_write_data(struct i2c_gpio_bus *bus, uchar chip,
223 uchar *buffer, int len,
224 bool end_with_repeated_start)
226 unsigned int delay = bus->udelay;
229 debug("%s: chip %x buffer %p len %d\n", __func__, chip, buffer, len);
231 if (i2c_send_slave_addr(bus, delay, chip << 1)) {
232 debug("i2c_write, no chip responded %02X\n", chip);
237 if (i2c_gpio_write_byte(bus, delay, *buffer++))
241 if (!end_with_repeated_start) {
242 i2c_gpio_send_stop(bus, delay);
246 if (i2c_send_slave_addr(bus, delay, (chip << 1) | 0x1)) {
247 debug("i2c_write, no chip responded %02X\n", chip);
254 static int i2c_gpio_read_data(struct i2c_gpio_bus *bus, uchar chip,
255 uchar *buffer, int len)
257 unsigned int delay = bus->udelay;
259 debug("%s: chip %x buffer: %p len %d\n", __func__, chip, buffer, len);
262 *buffer++ = i2c_gpio_read_byte(bus, delay, len == 0);
264 i2c_gpio_send_stop(bus, delay);
269 static int i2c_gpio_xfer(struct udevice *dev, struct i2c_msg *msg, int nmsgs)
271 struct i2c_gpio_bus *bus = dev_get_priv(dev);
274 for (; nmsgs > 0; nmsgs--, msg++) {
275 bool next_is_read = nmsgs > 1 && (msg[1].flags & I2C_M_RD);
277 if (msg->flags & I2C_M_RD) {
278 ret = i2c_gpio_read_data(bus, msg->addr, msg->buf,
281 ret = i2c_gpio_write_data(bus, msg->addr, msg->buf,
282 msg->len, next_is_read);
292 static int i2c_gpio_probe(struct udevice *dev, uint chip, uint chip_flags)
294 struct i2c_gpio_bus *bus = dev_get_priv(dev);
295 unsigned int delay = bus->udelay;
298 i2c_gpio_send_start(bus, delay);
299 ret = i2c_gpio_write_byte(bus, delay, (chip << 1) | 0);
300 i2c_gpio_send_stop(bus, delay);
302 debug("%s: bus: %d (%s) chip: %x flags: %x ret: %d\n",
303 __func__, dev_seq(dev), dev->name, chip, chip_flags, ret);
308 static int i2c_gpio_set_bus_speed(struct udevice *dev, unsigned int speed_hz)
310 struct i2c_gpio_bus *bus = dev_get_priv(dev);
312 bus->udelay = 1000000 / (speed_hz << 2);
314 i2c_gpio_send_reset(bus, bus->udelay);
319 static int i2c_gpio_drv_probe(struct udevice *dev)
321 if (dev_read_bool(dev, "i2c-gpio,deblock")) {
322 /* @200kHz 9 clocks = 44us, 62us is ok */
323 const unsigned int DELAY_ABORT_SEQ = 62;
324 struct i2c_gpio_bus *bus = dev_get_priv(dev);
326 return i2c_deblock_gpio_loop(&bus->gpios[PIN_SDA],
327 &bus->gpios[PIN_SCL],
328 16, 5, DELAY_ABORT_SEQ);
334 static int i2c_gpio_of_to_plat(struct udevice *dev)
336 struct i2c_gpio_bus *bus = dev_get_priv(dev);
339 /* "gpios" is deprecated and replaced by "sda-gpios" + "scl-gpios". */
340 ret = gpio_request_list_by_name(dev, "gpios", bus->gpios,
341 ARRAY_SIZE(bus->gpios), 0);
342 if (ret == -ENOENT) {
343 ret = gpio_request_by_name(dev, "sda-gpios", 0,
344 &bus->gpios[PIN_SDA], 0);
347 ret = gpio_request_by_name(dev, "scl-gpios", 0,
348 &bus->gpios[PIN_SCL], 0);
353 bus->udelay = dev_read_u32_default(dev, "i2c-gpio,delay-us",
356 bus->get_sda = i2c_gpio_sda_get;
357 bus->set_sda = i2c_gpio_sda_set;
358 if (dev_read_bool(dev, "i2c-gpio,scl-output-only"))
359 bus->set_scl = i2c_gpio_scl_set_output_only;
361 bus->set_scl = i2c_gpio_scl_set;
365 pr_err("Can't get %s gpios! Error: %d\n", dev->name, ret);
369 static const struct dm_i2c_ops i2c_gpio_ops = {
370 .xfer = i2c_gpio_xfer,
371 .probe_chip = i2c_gpio_probe,
372 .set_bus_speed = i2c_gpio_set_bus_speed,
375 static const struct udevice_id i2c_gpio_ids[] = {
376 { .compatible = "i2c-gpio" },
380 U_BOOT_DRIVER(i2c_gpio) = {
383 .of_match = i2c_gpio_ids,
384 .probe = i2c_gpio_drv_probe,
385 .of_to_plat = i2c_gpio_of_to_plat,
386 .priv_auto = sizeof(struct i2c_gpio_bus),
387 .ops = &i2c_gpio_ops,