2 * (C) Copyright 2015, Samsung Electronics
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];
53 dm_gpio_set_dir_flags(sda, GPIOD_IS_IN);
55 dm_gpio_set_dir_flags(sda, GPIOD_IS_OUT);
58 static void i2c_gpio_scl_set(struct i2c_gpio_bus *bus, int bit)
60 struct gpio_desc *scl = &bus->gpios[PIN_SCL];
64 dm_gpio_set_dir_flags(scl, GPIOD_IS_IN);
65 while (!dm_gpio_get_value(scl) && count++ < 100000)
68 if (!dm_gpio_get_value(scl))
69 pr_err("timeout waiting on slave to release scl\n");
71 dm_gpio_set_dir_flags(scl, GPIOD_IS_OUT);
75 /* variant for output only gpios which cannot support clock stretching */
76 static void i2c_gpio_scl_set_output_only(struct i2c_gpio_bus *bus, int bit)
78 struct gpio_desc *scl = &bus->gpios[PIN_SCL];
79 ulong flags = GPIOD_IS_OUT;
82 flags |= GPIOD_IS_OUT_ACTIVE;
83 dm_gpio_set_dir_flags(scl, flags);
86 static void i2c_gpio_write_bit(struct i2c_gpio_bus *bus, int delay, uchar bit)
90 bus->set_sda(bus, bit);
96 static int i2c_gpio_read_bit(struct i2c_gpio_bus *bus, int delay)
100 bus->set_scl(bus, 1);
102 value = bus->get_sda(bus);
104 bus->set_scl(bus, 0);
110 /* START: High -> Low on SDA while SCL is High */
111 static void i2c_gpio_send_start(struct i2c_gpio_bus *bus, int delay)
114 bus->set_sda(bus, 1);
116 bus->set_scl(bus, 1);
118 bus->set_sda(bus, 0);
122 /* STOP: Low -> High on SDA while SCL is High */
123 static void i2c_gpio_send_stop(struct i2c_gpio_bus *bus, int delay)
125 bus->set_scl(bus, 0);
127 bus->set_sda(bus, 0);
129 bus->set_scl(bus, 1);
131 bus->set_sda(bus, 1);
135 /* ack should be I2C_ACK or I2C_NOACK */
136 static void i2c_gpio_send_ack(struct i2c_gpio_bus *bus, int delay, int ack)
138 i2c_gpio_write_bit(bus, delay, ack);
139 bus->set_scl(bus, 0);
144 * Send a reset sequence consisting of 9 clocks with the data signal high
145 * to clock any confused device back into an idle state. Also send a
146 * <stop> at the end of the sequence for belts & suspenders.
148 static void i2c_gpio_send_reset(struct i2c_gpio_bus *bus, int delay)
152 for (j = 0; j < 9; j++)
153 i2c_gpio_write_bit(bus, delay, 1);
155 i2c_gpio_send_stop(bus, delay);
158 /* Set sda high with low clock, before reading slave data */
159 static void i2c_gpio_sda_high(struct i2c_gpio_bus *bus, int delay)
161 bus->set_scl(bus, 0);
163 bus->set_sda(bus, 1);
167 /* Send 8 bits and look for an acknowledgement */
168 static int i2c_gpio_write_byte(struct i2c_gpio_bus *bus, int delay, uchar data)
173 for (j = 0; j < 8; j++) {
174 i2c_gpio_write_bit(bus, delay, data & 0x80);
180 /* Look for an <ACK>(negative logic) and return it */
181 i2c_gpio_sda_high(bus, delay);
182 nack = i2c_gpio_read_bit(bus, delay);
184 return nack; /* not a nack is an ack */
188 * if ack == I2C_ACK, ACK the byte so can continue reading, else
189 * send I2C_NOACK to end the read.
191 static uchar i2c_gpio_read_byte(struct i2c_gpio_bus *bus, int delay, int ack)
196 i2c_gpio_sda_high(bus, delay);
198 for (j = 0; j < 8; j++) {
200 data |= i2c_gpio_read_bit(bus, delay);
202 i2c_gpio_send_ack(bus, delay, ack);
207 /* send start and the slave chip address */
208 int i2c_send_slave_addr(struct i2c_gpio_bus *bus, int delay, uchar chip)
210 i2c_gpio_send_start(bus, delay);
212 if (i2c_gpio_write_byte(bus, delay, chip)) {
213 i2c_gpio_send_stop(bus, delay);
220 static int i2c_gpio_write_data(struct i2c_gpio_bus *bus, uchar chip,
221 uchar *buffer, int len,
222 bool end_with_repeated_start)
224 unsigned int delay = bus->udelay;
227 debug("%s: chip %x buffer %p len %d\n", __func__, chip, buffer, len);
229 if (i2c_send_slave_addr(bus, delay, chip << 1)) {
230 debug("i2c_write, no chip responded %02X\n", chip);
235 if (i2c_gpio_write_byte(bus, delay, *buffer++))
239 if (!end_with_repeated_start) {
240 i2c_gpio_send_stop(bus, delay);
244 if (i2c_send_slave_addr(bus, delay, (chip << 1) | 0x1)) {
245 debug("i2c_write, no chip responded %02X\n", chip);
252 static int i2c_gpio_read_data(struct i2c_gpio_bus *bus, uchar chip,
253 uchar *buffer, int len)
255 unsigned int delay = bus->udelay;
257 debug("%s: chip %x buffer: %p len %d\n", __func__, chip, buffer, len);
260 *buffer++ = i2c_gpio_read_byte(bus, delay, len == 0);
262 i2c_gpio_send_stop(bus, delay);
267 static int i2c_gpio_xfer(struct udevice *dev, struct i2c_msg *msg, int nmsgs)
269 struct i2c_gpio_bus *bus = dev_get_priv(dev);
272 for (; nmsgs > 0; nmsgs--, msg++) {
273 bool next_is_read = nmsgs > 1 && (msg[1].flags & I2C_M_RD);
275 if (msg->flags & I2C_M_RD) {
276 ret = i2c_gpio_read_data(bus, msg->addr, msg->buf,
279 ret = i2c_gpio_write_data(bus, msg->addr, msg->buf,
280 msg->len, next_is_read);
290 static int i2c_gpio_probe(struct udevice *dev, uint chip, uint chip_flags)
292 struct i2c_gpio_bus *bus = dev_get_priv(dev);
293 unsigned int delay = bus->udelay;
296 i2c_gpio_send_start(bus, delay);
297 ret = i2c_gpio_write_byte(bus, delay, (chip << 1) | 0);
298 i2c_gpio_send_stop(bus, delay);
300 debug("%s: bus: %d (%s) chip: %x flags: %x ret: %d\n",
301 __func__, dev->seq, dev->name, chip, chip_flags, ret);
306 static int i2c_gpio_set_bus_speed(struct udevice *dev, unsigned int speed_hz)
308 struct i2c_gpio_bus *bus = dev_get_priv(dev);
310 bus->udelay = 1000000 / (speed_hz << 2);
312 i2c_gpio_send_reset(bus, bus->udelay);
317 static int i2c_gpio_drv_probe(struct udevice *dev)
319 if (dev_read_bool(dev, "i2c-gpio,deblock")) {
320 /* @200kHz 9 clocks = 44us, 62us is ok */
321 const unsigned int DELAY_ABORT_SEQ = 62;
322 struct i2c_gpio_bus *bus = dev_get_priv(dev);
324 return i2c_deblock_gpio_loop(&bus->gpios[PIN_SDA],
325 &bus->gpios[PIN_SCL],
326 16, 5, DELAY_ABORT_SEQ);
332 static int i2c_gpio_ofdata_to_platdata(struct udevice *dev)
334 struct i2c_gpio_bus *bus = dev_get_priv(dev);
337 ret = gpio_request_list_by_name(dev, "gpios", bus->gpios,
338 ARRAY_SIZE(bus->gpios), 0);
342 bus->udelay = dev_read_u32_default(dev, "i2c-gpio,delay-us",
345 bus->get_sda = i2c_gpio_sda_get;
346 bus->set_sda = i2c_gpio_sda_set;
347 if (dev_read_bool(dev, "i2c-gpio,scl-output-only"))
348 bus->set_scl = i2c_gpio_scl_set_output_only;
350 bus->set_scl = i2c_gpio_scl_set;
354 pr_err("Can't get %s gpios! Error: %d", dev->name, ret);
358 static const struct dm_i2c_ops i2c_gpio_ops = {
359 .xfer = i2c_gpio_xfer,
360 .probe_chip = i2c_gpio_probe,
361 .set_bus_speed = i2c_gpio_set_bus_speed,
364 static const struct udevice_id i2c_gpio_ids[] = {
365 { .compatible = "i2c-gpio" },
369 U_BOOT_DRIVER(i2c_gpio) = {
372 .of_match = i2c_gpio_ids,
373 .probe = i2c_gpio_drv_probe,
374 .ofdata_to_platdata = i2c_gpio_ofdata_to_platdata,
375 .priv_auto = sizeof(struct i2c_gpio_bus),
376 .ops = &i2c_gpio_ops,