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 #define DEFAULT_UDELAY 5
19 DECLARE_GLOBAL_DATA_PTR;
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];
61 ulong flags = GPIOD_IS_OUT;
64 flags |= GPIOD_IS_OUT_ACTIVE;
65 dm_gpio_set_dir_flags(scl, flags);
68 static void i2c_gpio_write_bit(struct i2c_gpio_bus *bus, int delay, uchar bit)
72 bus->set_sda(bus, bit);
78 static int i2c_gpio_read_bit(struct i2c_gpio_bus *bus, int delay)
84 value = bus->get_sda(bus);
92 /* START: High -> Low on SDA while SCL is High */
93 static void i2c_gpio_send_start(struct i2c_gpio_bus *bus, int delay)
100 bus->set_sda(bus, 0);
104 /* STOP: Low -> High on SDA while SCL is High */
105 static void i2c_gpio_send_stop(struct i2c_gpio_bus *bus, int delay)
107 bus->set_scl(bus, 0);
109 bus->set_sda(bus, 0);
111 bus->set_scl(bus, 1);
113 bus->set_sda(bus, 1);
117 /* ack should be I2C_ACK or I2C_NOACK */
118 static void i2c_gpio_send_ack(struct i2c_gpio_bus *bus, int delay, int ack)
120 i2c_gpio_write_bit(bus, delay, ack);
121 bus->set_scl(bus, 0);
126 * Send a reset sequence consisting of 9 clocks with the data signal high
127 * to clock any confused device back into an idle state. Also send a
128 * <stop> at the end of the sequence for belts & suspenders.
130 static void i2c_gpio_send_reset(struct i2c_gpio_bus *bus, int delay)
134 for (j = 0; j < 9; j++)
135 i2c_gpio_write_bit(bus, delay, 1);
137 i2c_gpio_send_stop(bus, delay);
140 /* Set sda high with low clock, before reading slave data */
141 static void i2c_gpio_sda_high(struct i2c_gpio_bus *bus, int delay)
143 bus->set_scl(bus, 0);
145 bus->set_sda(bus, 1);
149 /* Send 8 bits and look for an acknowledgement */
150 static int i2c_gpio_write_byte(struct i2c_gpio_bus *bus, int delay, uchar data)
155 for (j = 0; j < 8; j++) {
156 i2c_gpio_write_bit(bus, delay, data & 0x80);
162 /* Look for an <ACK>(negative logic) and return it */
163 i2c_gpio_sda_high(bus, delay);
164 nack = i2c_gpio_read_bit(bus, delay);
166 return nack; /* not a nack is an ack */
170 * if ack == I2C_ACK, ACK the byte so can continue reading, else
171 * send I2C_NOACK to end the read.
173 static uchar i2c_gpio_read_byte(struct i2c_gpio_bus *bus, int delay, int ack)
178 i2c_gpio_sda_high(bus, delay);
180 for (j = 0; j < 8; j++) {
182 data |= i2c_gpio_read_bit(bus, delay);
184 i2c_gpio_send_ack(bus, delay, ack);
189 /* send start and the slave chip address */
190 int i2c_send_slave_addr(struct i2c_gpio_bus *bus, int delay, uchar chip)
192 i2c_gpio_send_start(bus, delay);
194 if (i2c_gpio_write_byte(bus, delay, chip)) {
195 i2c_gpio_send_stop(bus, delay);
202 static int i2c_gpio_write_data(struct i2c_gpio_bus *bus, uchar chip,
203 uchar *buffer, int len,
204 bool end_with_repeated_start)
206 unsigned int delay = bus->udelay;
209 debug("%s: chip %x buffer %p len %d\n", __func__, chip, buffer, len);
211 if (i2c_send_slave_addr(bus, delay, chip << 1)) {
212 debug("i2c_write, no chip responded %02X\n", chip);
217 if (i2c_gpio_write_byte(bus, delay, *buffer++))
221 if (!end_with_repeated_start) {
222 i2c_gpio_send_stop(bus, delay);
226 if (i2c_send_slave_addr(bus, delay, (chip << 1) | 0x1)) {
227 debug("i2c_write, no chip responded %02X\n", chip);
234 static int i2c_gpio_read_data(struct i2c_gpio_bus *bus, uchar chip,
235 uchar *buffer, int len)
237 unsigned int delay = bus->udelay;
239 debug("%s: chip %x buffer: %p len %d\n", __func__, chip, buffer, len);
242 *buffer++ = i2c_gpio_read_byte(bus, delay, len == 0);
244 i2c_gpio_send_stop(bus, delay);
249 static int i2c_gpio_xfer(struct udevice *dev, struct i2c_msg *msg, int nmsgs)
251 struct i2c_gpio_bus *bus = dev_get_priv(dev);
254 for (; nmsgs > 0; nmsgs--, msg++) {
255 bool next_is_read = nmsgs > 1 && (msg[1].flags & I2C_M_RD);
257 if (msg->flags & I2C_M_RD) {
258 ret = i2c_gpio_read_data(bus, msg->addr, msg->buf,
261 ret = i2c_gpio_write_data(bus, msg->addr, msg->buf,
262 msg->len, next_is_read);
272 static int i2c_gpio_probe(struct udevice *dev, uint chip, uint chip_flags)
274 struct i2c_gpio_bus *bus = dev_get_priv(dev);
275 unsigned int delay = bus->udelay;
278 i2c_gpio_send_start(bus, delay);
279 ret = i2c_gpio_write_byte(bus, delay, (chip << 1) | 0);
280 i2c_gpio_send_stop(bus, delay);
282 debug("%s: bus: %d (%s) chip: %x flags: %x ret: %d\n",
283 __func__, dev->seq, dev->name, chip, chip_flags, ret);
288 static int i2c_gpio_set_bus_speed(struct udevice *dev, unsigned int speed_hz)
290 struct i2c_gpio_bus *bus = dev_get_priv(dev);
292 bus->udelay = 1000000 / (speed_hz << 2);
294 i2c_gpio_send_reset(bus, bus->udelay);
299 static int i2c_gpio_drv_probe(struct udevice *dev)
301 if (dev_read_bool(dev, "i2c-gpio,deblock")) {
302 /* @200kHz 9 clocks = 44us, 62us is ok */
303 const unsigned int DELAY_ABORT_SEQ = 62;
304 struct i2c_gpio_bus *bus = dev_get_priv(dev);
306 return i2c_deblock_gpio_loop(&bus->gpios[PIN_SDA],
307 &bus->gpios[PIN_SCL],
308 16, 5, DELAY_ABORT_SEQ);
314 static int i2c_gpio_ofdata_to_platdata(struct udevice *dev)
316 struct i2c_gpio_bus *bus = dev_get_priv(dev);
317 const void *blob = gd->fdt_blob;
318 int node = dev_of_offset(dev);
321 ret = gpio_request_list_by_name(dev, "gpios", bus->gpios,
322 ARRAY_SIZE(bus->gpios), 0);
326 bus->udelay = fdtdec_get_int(blob, node, "i2c-gpio,delay-us",
329 bus->get_sda = i2c_gpio_sda_get;
330 bus->set_sda = i2c_gpio_sda_set;
331 bus->set_scl = i2c_gpio_scl_set;
335 pr_err("Can't get %s gpios! Error: %d", dev->name, ret);
339 static const struct dm_i2c_ops i2c_gpio_ops = {
340 .xfer = i2c_gpio_xfer,
341 .probe_chip = i2c_gpio_probe,
342 .set_bus_speed = i2c_gpio_set_bus_speed,
345 static const struct udevice_id i2c_gpio_ids[] = {
346 { .compatible = "i2c-gpio" },
350 U_BOOT_DRIVER(i2c_gpio) = {
353 .of_match = i2c_gpio_ids,
354 .probe = i2c_gpio_drv_probe,
355 .ofdata_to_platdata = i2c_gpio_ofdata_to_platdata,
356 .priv_auto_alloc_size = sizeof(struct i2c_gpio_bus),
357 .ops = &i2c_gpio_ops,