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];
37 static int i2c_gpio_sda_get(struct gpio_desc *sda)
39 return dm_gpio_get_value(sda);
42 static void i2c_gpio_sda_set(struct gpio_desc *sda, int bit)
45 dm_gpio_set_dir_flags(sda, GPIOD_IS_IN);
47 dm_gpio_set_dir_flags(sda, GPIOD_IS_OUT);
48 dm_gpio_set_value(sda, 0);
52 static void i2c_gpio_scl_set(struct gpio_desc *scl, int bit)
54 dm_gpio_set_dir_flags(scl, GPIOD_IS_OUT);
55 dm_gpio_set_value(scl, bit);
58 static void i2c_gpio_write_bit(struct gpio_desc *scl, struct gpio_desc *sda,
61 i2c_gpio_scl_set(scl, 0);
63 i2c_gpio_sda_set(sda, bit);
65 i2c_gpio_scl_set(scl, 1);
69 static int i2c_gpio_read_bit(struct gpio_desc *scl, struct gpio_desc *sda,
74 i2c_gpio_scl_set(scl, 1);
76 value = i2c_gpio_sda_get(sda);
78 i2c_gpio_scl_set(scl, 0);
84 /* START: High -> Low on SDA while SCL is High */
85 static void i2c_gpio_send_start(struct gpio_desc *scl, struct gpio_desc *sda,
89 i2c_gpio_sda_set(sda, 1);
91 i2c_gpio_scl_set(scl, 1);
93 i2c_gpio_sda_set(sda, 0);
97 /* STOP: Low -> High on SDA while SCL is High */
98 static void i2c_gpio_send_stop(struct gpio_desc *scl, struct gpio_desc *sda,
101 i2c_gpio_scl_set(scl, 0);
103 i2c_gpio_sda_set(sda, 0);
105 i2c_gpio_scl_set(scl, 1);
107 i2c_gpio_sda_set(sda, 1);
111 /* ack should be I2C_ACK or I2C_NOACK */
112 static void i2c_gpio_send_ack(struct gpio_desc *scl, struct gpio_desc *sda,
115 i2c_gpio_write_bit(scl, sda, delay, ack);
116 i2c_gpio_scl_set(scl, 0);
121 * Send a reset sequence consisting of 9 clocks with the data signal high
122 * to clock any confused device back into an idle state. Also send a
123 * <stop> at the end of the sequence for belts & suspenders.
125 static void i2c_gpio_send_reset(struct gpio_desc *scl, struct gpio_desc *sda,
130 for (j = 0; j < 9; j++)
131 i2c_gpio_write_bit(scl, sda, delay, 1);
133 i2c_gpio_send_stop(scl, sda, delay);
136 /* Set sda high with low clock, before reading slave data */
137 static void i2c_gpio_sda_high(struct gpio_desc *scl, struct gpio_desc *sda,
140 i2c_gpio_scl_set(scl, 0);
142 i2c_gpio_sda_set(sda, 1);
146 /* Send 8 bits and look for an acknowledgement */
147 static int i2c_gpio_write_byte(struct gpio_desc *scl, struct gpio_desc *sda,
148 int delay, uchar data)
153 for (j = 0; j < 8; j++) {
154 i2c_gpio_write_bit(scl, sda, delay, data & 0x80);
160 /* Look for an <ACK>(negative logic) and return it */
161 i2c_gpio_sda_high(scl, sda, delay);
162 nack = i2c_gpio_read_bit(scl, sda, delay);
164 return nack; /* not a nack is an ack */
168 * if ack == I2C_ACK, ACK the byte so can continue reading, else
169 * send I2C_NOACK to end the read.
171 static uchar i2c_gpio_read_byte(struct gpio_desc *scl, struct gpio_desc *sda,
177 i2c_gpio_sda_high(scl, sda, delay);
179 for (j = 0; j < 8; j++) {
181 data |= i2c_gpio_read_bit(scl, sda, delay);
183 i2c_gpio_send_ack(scl, sda, delay, ack);
188 /* send start and the slave chip address */
189 int i2c_send_slave_addr(struct gpio_desc *scl, struct gpio_desc *sda, int delay,
192 i2c_gpio_send_start(scl, sda, delay);
194 if (i2c_gpio_write_byte(scl, sda, delay, chip)) {
195 i2c_gpio_send_stop(scl, sda, 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 struct gpio_desc *scl = &bus->gpios[PIN_SCL];
207 struct gpio_desc *sda = &bus->gpios[PIN_SDA];
208 unsigned int delay = bus->udelay;
211 debug("%s: chip %x buffer %p len %d\n", __func__, chip, buffer, len);
213 if (i2c_send_slave_addr(scl, sda, delay, chip << 1)) {
214 debug("i2c_write, no chip responded %02X\n", chip);
219 if (i2c_gpio_write_byte(scl, sda, delay, *buffer++))
223 if (!end_with_repeated_start) {
224 i2c_gpio_send_stop(scl, sda, delay);
228 if (i2c_send_slave_addr(scl, sda, delay, (chip << 1) | 0x1)) {
229 debug("i2c_write, no chip responded %02X\n", chip);
236 static int i2c_gpio_read_data(struct i2c_gpio_bus *bus, uchar chip,
237 uchar *buffer, int len)
239 struct gpio_desc *scl = &bus->gpios[PIN_SCL];
240 struct gpio_desc *sda = &bus->gpios[PIN_SDA];
241 unsigned int delay = bus->udelay;
243 debug("%s: chip %x buffer: %p len %d\n", __func__, chip, buffer, len);
246 *buffer++ = i2c_gpio_read_byte(scl, sda, delay, len == 0);
248 i2c_gpio_send_stop(scl, sda, delay);
253 static int i2c_gpio_xfer(struct udevice *dev, struct i2c_msg *msg, int nmsgs)
255 struct i2c_gpio_bus *bus = dev_get_priv(dev);
258 for (; nmsgs > 0; nmsgs--, msg++) {
259 bool next_is_read = nmsgs > 1 && (msg[1].flags & I2C_M_RD);
261 if (msg->flags & I2C_M_RD) {
262 ret = i2c_gpio_read_data(bus, msg->addr, msg->buf,
265 ret = i2c_gpio_write_data(bus, msg->addr, msg->buf,
266 msg->len, next_is_read);
276 static int i2c_gpio_probe(struct udevice *dev, uint chip, uint chip_flags)
278 struct i2c_gpio_bus *bus = dev_get_priv(dev);
279 struct gpio_desc *scl = &bus->gpios[PIN_SCL];
280 struct gpio_desc *sda = &bus->gpios[PIN_SDA];
281 unsigned int delay = bus->udelay;
284 i2c_gpio_send_start(scl, sda, delay);
285 ret = i2c_gpio_write_byte(scl, sda, delay, (chip << 1) | 0);
286 i2c_gpio_send_stop(scl, sda, delay);
288 debug("%s: bus: %d (%s) chip: %x flags: %x ret: %d\n",
289 __func__, dev->seq, dev->name, chip, chip_flags, ret);
294 static int i2c_gpio_set_bus_speed(struct udevice *dev, unsigned int speed_hz)
296 struct i2c_gpio_bus *bus = dev_get_priv(dev);
297 struct gpio_desc *scl = &bus->gpios[PIN_SCL];
298 struct gpio_desc *sda = &bus->gpios[PIN_SDA];
300 bus->udelay = 1000000 / (speed_hz << 2);
302 i2c_gpio_send_reset(scl, sda, bus->udelay);
307 static int i2c_gpio_ofdata_to_platdata(struct udevice *dev)
309 struct i2c_gpio_bus *bus = dev_get_priv(dev);
310 const void *blob = gd->fdt_blob;
311 int node = dev->of_offset;
314 ret = gpio_request_list_by_name(dev, "gpios", bus->gpios,
315 ARRAY_SIZE(bus->gpios), 0);
319 bus->udelay = fdtdec_get_int(blob, node, "i2c-gpio,delay-us",
324 error("Can't get %s gpios! Error: %d", dev->name, ret);
328 static const struct dm_i2c_ops i2c_gpio_ops = {
329 .xfer = i2c_gpio_xfer,
330 .probe_chip = i2c_gpio_probe,
331 .set_bus_speed = i2c_gpio_set_bus_speed,
334 static const struct udevice_id i2c_gpio_ids[] = {
335 { .compatible = "i2c-gpio" },
339 U_BOOT_DRIVER(i2c_gpio) = {
342 .of_match = i2c_gpio_ids,
343 .ofdata_to_platdata = i2c_gpio_ofdata_to_platdata,
344 .priv_auto_alloc_size = sizeof(struct i2c_gpio_bus),
345 .ops = &i2c_gpio_ops,