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.
15 #define DEFAULT_UDELAY 5
20 DECLARE_GLOBAL_DATA_PTR;
30 * udelay - delay [us] between GPIO toggle operations,
31 * which is 1/4 of I2C speed clock period.
35 struct gpio_desc gpios[PIN_COUNT];
37 int (*get_sda)(struct i2c_gpio_bus *bus);
38 void (*set_sda)(struct i2c_gpio_bus *bus, int bit);
39 void (*set_scl)(struct i2c_gpio_bus *bus, int bit);
42 static int i2c_gpio_sda_get(struct i2c_gpio_bus *bus)
44 struct gpio_desc *sda = &bus->gpios[PIN_SDA];
46 return dm_gpio_get_value(sda);
49 static void i2c_gpio_sda_set(struct i2c_gpio_bus *bus, int bit)
51 struct gpio_desc *sda = &bus->gpios[PIN_SDA];
54 dm_gpio_set_dir_flags(sda, GPIOD_IS_IN);
56 dm_gpio_set_dir_flags(sda, GPIOD_IS_OUT);
59 static void i2c_gpio_scl_set(struct i2c_gpio_bus *bus, int bit)
61 struct gpio_desc *scl = &bus->gpios[PIN_SCL];
65 dm_gpio_set_dir_flags(scl, GPIOD_IS_IN);
66 while (!dm_gpio_get_value(scl) && count++ < 100000)
69 if (!dm_gpio_get_value(scl))
70 pr_err("timeout waiting on slave to release scl\n");
72 dm_gpio_set_dir_flags(scl, GPIOD_IS_OUT);
76 /* variant for output only gpios which cannot support clock stretching */
77 static void i2c_gpio_scl_set_output_only(struct i2c_gpio_bus *bus, int bit)
79 struct gpio_desc *scl = &bus->gpios[PIN_SCL];
80 ulong flags = GPIOD_IS_OUT;
83 flags |= GPIOD_IS_OUT_ACTIVE;
84 dm_gpio_set_dir_flags(scl, flags);
87 static void i2c_gpio_write_bit(struct i2c_gpio_bus *bus, int delay, uchar bit)
91 bus->set_sda(bus, bit);
97 static int i2c_gpio_read_bit(struct i2c_gpio_bus *bus, int delay)
101 bus->set_scl(bus, 1);
103 value = bus->get_sda(bus);
105 bus->set_scl(bus, 0);
111 /* START: High -> Low on SDA while SCL is High */
112 static void i2c_gpio_send_start(struct i2c_gpio_bus *bus, int delay)
115 bus->set_sda(bus, 1);
117 bus->set_scl(bus, 1);
119 bus->set_sda(bus, 0);
123 /* STOP: Low -> High on SDA while SCL is High */
124 static void i2c_gpio_send_stop(struct i2c_gpio_bus *bus, int delay)
126 bus->set_scl(bus, 0);
128 bus->set_sda(bus, 0);
130 bus->set_scl(bus, 1);
132 bus->set_sda(bus, 1);
136 /* ack should be I2C_ACK or I2C_NOACK */
137 static void i2c_gpio_send_ack(struct i2c_gpio_bus *bus, int delay, int ack)
139 i2c_gpio_write_bit(bus, delay, ack);
140 bus->set_scl(bus, 0);
145 * Send a reset sequence consisting of 9 clocks with the data signal high
146 * to clock any confused device back into an idle state. Also send a
147 * <stop> at the end of the sequence for belts & suspenders.
149 static void i2c_gpio_send_reset(struct i2c_gpio_bus *bus, int delay)
153 for (j = 0; j < 9; j++)
154 i2c_gpio_write_bit(bus, delay, 1);
156 i2c_gpio_send_stop(bus, delay);
159 /* Set sda high with low clock, before reading slave data */
160 static void i2c_gpio_sda_high(struct i2c_gpio_bus *bus, int delay)
162 bus->set_scl(bus, 0);
164 bus->set_sda(bus, 1);
168 /* Send 8 bits and look for an acknowledgement */
169 static int i2c_gpio_write_byte(struct i2c_gpio_bus *bus, int delay, uchar data)
174 for (j = 0; j < 8; j++) {
175 i2c_gpio_write_bit(bus, delay, data & 0x80);
181 /* Look for an <ACK>(negative logic) and return it */
182 i2c_gpio_sda_high(bus, delay);
183 nack = i2c_gpio_read_bit(bus, delay);
185 return nack; /* not a nack is an ack */
189 * if ack == I2C_ACK, ACK the byte so can continue reading, else
190 * send I2C_NOACK to end the read.
192 static uchar i2c_gpio_read_byte(struct i2c_gpio_bus *bus, int delay, int ack)
197 i2c_gpio_sda_high(bus, delay);
199 for (j = 0; j < 8; j++) {
201 data |= i2c_gpio_read_bit(bus, delay);
203 i2c_gpio_send_ack(bus, delay, ack);
208 /* send start and the slave chip address */
209 int i2c_send_slave_addr(struct i2c_gpio_bus *bus, int delay, uchar chip)
211 i2c_gpio_send_start(bus, delay);
213 if (i2c_gpio_write_byte(bus, delay, chip)) {
214 i2c_gpio_send_stop(bus, delay);
221 static int i2c_gpio_write_data(struct i2c_gpio_bus *bus, uchar chip,
222 uchar *buffer, int len,
223 bool end_with_repeated_start)
225 unsigned int delay = bus->udelay;
228 debug("%s: chip %x buffer %p len %d\n", __func__, chip, buffer, len);
230 if (i2c_send_slave_addr(bus, delay, chip << 1)) {
231 debug("i2c_write, no chip responded %02X\n", chip);
236 if (i2c_gpio_write_byte(bus, delay, *buffer++))
240 if (!end_with_repeated_start) {
241 i2c_gpio_send_stop(bus, delay);
245 if (i2c_send_slave_addr(bus, delay, (chip << 1) | 0x1)) {
246 debug("i2c_write, no chip responded %02X\n", chip);
253 static int i2c_gpio_read_data(struct i2c_gpio_bus *bus, uchar chip,
254 uchar *buffer, int len)
256 unsigned int delay = bus->udelay;
258 debug("%s: chip %x buffer: %p len %d\n", __func__, chip, buffer, len);
261 *buffer++ = i2c_gpio_read_byte(bus, delay, len == 0);
263 i2c_gpio_send_stop(bus, delay);
268 static int i2c_gpio_xfer(struct udevice *dev, struct i2c_msg *msg, int nmsgs)
270 struct i2c_gpio_bus *bus = dev_get_priv(dev);
273 for (; nmsgs > 0; nmsgs--, msg++) {
274 bool next_is_read = nmsgs > 1 && (msg[1].flags & I2C_M_RD);
276 if (msg->flags & I2C_M_RD) {
277 ret = i2c_gpio_read_data(bus, msg->addr, msg->buf,
280 ret = i2c_gpio_write_data(bus, msg->addr, msg->buf,
281 msg->len, next_is_read);
291 static int i2c_gpio_probe(struct udevice *dev, uint chip, uint chip_flags)
293 struct i2c_gpio_bus *bus = dev_get_priv(dev);
294 unsigned int delay = bus->udelay;
297 i2c_gpio_send_start(bus, delay);
298 ret = i2c_gpio_write_byte(bus, delay, (chip << 1) | 0);
299 i2c_gpio_send_stop(bus, delay);
301 debug("%s: bus: %d (%s) chip: %x flags: %x ret: %d\n",
302 __func__, dev->seq, dev->name, chip, chip_flags, ret);
307 static int i2c_gpio_set_bus_speed(struct udevice *dev, unsigned int speed_hz)
309 struct i2c_gpio_bus *bus = dev_get_priv(dev);
311 bus->udelay = 1000000 / (speed_hz << 2);
313 i2c_gpio_send_reset(bus, bus->udelay);
318 static int i2c_gpio_drv_probe(struct udevice *dev)
320 if (dev_read_bool(dev, "i2c-gpio,deblock")) {
321 /* @200kHz 9 clocks = 44us, 62us is ok */
322 const unsigned int DELAY_ABORT_SEQ = 62;
323 struct i2c_gpio_bus *bus = dev_get_priv(dev);
325 return i2c_deblock_gpio_loop(&bus->gpios[PIN_SDA],
326 &bus->gpios[PIN_SCL],
327 16, 5, DELAY_ABORT_SEQ);
333 static int i2c_gpio_ofdata_to_platdata(struct udevice *dev)
335 struct i2c_gpio_bus *bus = dev_get_priv(dev);
336 const void *blob = gd->fdt_blob;
337 int node = dev_of_offset(dev);
340 ret = gpio_request_list_by_name(dev, "gpios", bus->gpios,
341 ARRAY_SIZE(bus->gpios), 0);
345 bus->udelay = fdtdec_get_int(blob, node, "i2c-gpio,delay-us",
348 bus->get_sda = i2c_gpio_sda_get;
349 bus->set_sda = i2c_gpio_sda_set;
350 if (fdtdec_get_bool(blob, node, "i2c-gpio,scl-output-only"))
351 bus->set_scl = i2c_gpio_scl_set_output_only;
353 bus->set_scl = i2c_gpio_scl_set;
357 pr_err("Can't get %s gpios! Error: %d", dev->name, ret);
361 static const struct dm_i2c_ops i2c_gpio_ops = {
362 .xfer = i2c_gpio_xfer,
363 .probe_chip = i2c_gpio_probe,
364 .set_bus_speed = i2c_gpio_set_bus_speed,
367 static const struct udevice_id i2c_gpio_ids[] = {
368 { .compatible = "i2c-gpio" },
372 U_BOOT_DRIVER(i2c_gpio) = {
375 .of_match = i2c_gpio_ids,
376 .probe = i2c_gpio_drv_probe,
377 .ofdata_to_platdata = i2c_gpio_ofdata_to_platdata,
378 .priv_auto_alloc_size = sizeof(struct i2c_gpio_bus),
379 .ops = &i2c_gpio_ops,