blob: 00aef40e3412b4ef99c18e2b09a4615ada88c883 [file] [log] [blame]
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +02001/*
2 * (C) Copyright 2015, Samsung Electronics
3 * Przemyslaw Marczak <p.marczak@samsung.com>
4 *
5 * This file is based on: drivers/i2c/soft-i2c.c,
6 * with added driver-model support and code cleanup.
7 */
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +02008#include <errno.h>
9#include <dm.h>
10#include <i2c.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060011#include <log.h>
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +020012#include <asm/gpio.h>
Simon Glassc05ed002020-05-10 11:40:11 -060013#include <linux/delay.h>
Simon Glass1e94b462023-09-14 18:21:46 -060014#include <linux/printk.h>
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +020015
16#define DEFAULT_UDELAY 5
17#define RETRIES 0
18#define I2C_ACK 0
19#define I2C_NOACK 1
20
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +020021enum {
22 PIN_SDA = 0,
23 PIN_SCL,
24 PIN_COUNT,
25};
26
27struct i2c_gpio_bus {
28 /**
29 * udelay - delay [us] between GPIO toggle operations,
30 * which is 1/4 of I2C speed clock period.
31 */
32 int udelay;
33 /* sda, scl */
34 struct gpio_desc gpios[PIN_COUNT];
Michael Auchter7383edc2020-02-07 10:55:31 -060035
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);
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +020039};
40
Michael Auchter7383edc2020-02-07 10:55:31 -060041static int i2c_gpio_sda_get(struct i2c_gpio_bus *bus)
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +020042{
Michael Auchter7383edc2020-02-07 10:55:31 -060043 struct gpio_desc *sda = &bus->gpios[PIN_SDA];
44
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +020045 return dm_gpio_get_value(sda);
46}
47
Michael Auchter7383edc2020-02-07 10:55:31 -060048static void i2c_gpio_sda_set(struct i2c_gpio_bus *bus, int bit)
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +020049{
Michael Auchter7383edc2020-02-07 10:55:31 -060050 struct gpio_desc *sda = &bus->gpios[PIN_SDA];
Simon Glassa12aa792021-03-23 14:52:06 +130051 ulong flags;
Michael Auchter7383edc2020-02-07 10:55:31 -060052
Axel Lin76382aa2015-04-26 11:05:38 +080053 if (bit)
Simon Glassa12aa792021-03-23 14:52:06 +130054 flags = GPIOD_IS_IN;
Axel Lin76382aa2015-04-26 11:05:38 +080055 else
Simon Glassa12aa792021-03-23 14:52:06 +130056 flags = GPIOD_IS_OUT;
57 dm_gpio_clrset_flags(sda, GPIOD_MASK_DIR, flags);
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +020058}
59
Michael Auchter7383edc2020-02-07 10:55:31 -060060static void i2c_gpio_scl_set(struct i2c_gpio_bus *bus, int bit)
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +020061{
Michael Auchter7383edc2020-02-07 10:55:31 -060062 struct gpio_desc *scl = &bus->gpios[PIN_SCL];
Heiko Schocherba6fb2f2020-03-16 07:55:06 +010063 int count = 0;
64
65 if (bit) {
Simon Glassa12aa792021-03-23 14:52:06 +130066 dm_gpio_clrset_flags(scl, GPIOD_MASK_DIR, GPIOD_IS_IN);
Heiko Schocherba6fb2f2020-03-16 07:55:06 +010067 while (!dm_gpio_get_value(scl) && count++ < 100000)
68 udelay(1);
69
70 if (!dm_gpio_get_value(scl))
71 pr_err("timeout waiting on slave to release scl\n");
72 } else {
Simon Glassa12aa792021-03-23 14:52:06 +130073 dm_gpio_clrset_flags(scl, GPIOD_MASK_DIR, GPIOD_IS_OUT);
Heiko Schocherba6fb2f2020-03-16 07:55:06 +010074 }
75}
76
77/* variant for output only gpios which cannot support clock stretching */
78static void i2c_gpio_scl_set_output_only(struct i2c_gpio_bus *bus, int bit)
79{
80 struct gpio_desc *scl = &bus->gpios[PIN_SCL];
Simon Glassa12aa792021-03-23 14:52:06 +130081 ulong flags = GPIOD_IS_OUT;
Axel Lin76382aa2015-04-26 11:05:38 +080082
83 if (bit)
Simon Glassa12aa792021-03-23 14:52:06 +130084 flags |= GPIOD_IS_OUT_ACTIVE;
85 dm_gpio_clrset_flags(scl, GPIOD_MASK_DIR, flags);
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +020086}
87
Michael Auchter7383edc2020-02-07 10:55:31 -060088static void i2c_gpio_write_bit(struct i2c_gpio_bus *bus, int delay, uchar bit)
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +020089{
Michael Auchter7383edc2020-02-07 10:55:31 -060090 bus->set_scl(bus, 0);
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +020091 udelay(delay);
Michael Auchter7383edc2020-02-07 10:55:31 -060092 bus->set_sda(bus, bit);
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +020093 udelay(delay);
Michael Auchter7383edc2020-02-07 10:55:31 -060094 bus->set_scl(bus, 1);
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +020095 udelay(2 * delay);
96}
97
Michael Auchter7383edc2020-02-07 10:55:31 -060098static int i2c_gpio_read_bit(struct i2c_gpio_bus *bus, int delay)
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +020099{
100 int value;
101
Michael Auchter7383edc2020-02-07 10:55:31 -0600102 bus->set_scl(bus, 1);
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +0200103 udelay(delay);
Alex Shumskyf315a482024-10-15 23:29:31 +0300104 value = bus->get_sda ? bus->get_sda(bus) : 0;
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +0200105 udelay(delay);
Michael Auchter7383edc2020-02-07 10:55:31 -0600106 bus->set_scl(bus, 0);
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +0200107 udelay(2 * delay);
108
109 return value;
110}
111
112/* START: High -> Low on SDA while SCL is High */
Michael Auchter7383edc2020-02-07 10:55:31 -0600113static void i2c_gpio_send_start(struct i2c_gpio_bus *bus, int delay)
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +0200114{
115 udelay(delay);
Michael Auchter7383edc2020-02-07 10:55:31 -0600116 bus->set_sda(bus, 1);
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +0200117 udelay(delay);
Michael Auchter7383edc2020-02-07 10:55:31 -0600118 bus->set_scl(bus, 1);
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +0200119 udelay(delay);
Michael Auchter7383edc2020-02-07 10:55:31 -0600120 bus->set_sda(bus, 0);
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +0200121 udelay(delay);
122}
123
124/* STOP: Low -> High on SDA while SCL is High */
Michael Auchter7383edc2020-02-07 10:55:31 -0600125static void i2c_gpio_send_stop(struct i2c_gpio_bus *bus, int delay)
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +0200126{
Michael Auchter7383edc2020-02-07 10:55:31 -0600127 bus->set_scl(bus, 0);
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +0200128 udelay(delay);
Michael Auchter7383edc2020-02-07 10:55:31 -0600129 bus->set_sda(bus, 0);
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +0200130 udelay(delay);
Michael Auchter7383edc2020-02-07 10:55:31 -0600131 bus->set_scl(bus, 1);
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +0200132 udelay(delay);
Michael Auchter7383edc2020-02-07 10:55:31 -0600133 bus->set_sda(bus, 1);
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +0200134 udelay(delay);
135}
136
137/* ack should be I2C_ACK or I2C_NOACK */
Michael Auchter7383edc2020-02-07 10:55:31 -0600138static void i2c_gpio_send_ack(struct i2c_gpio_bus *bus, int delay, int ack)
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +0200139{
Michael Auchter7383edc2020-02-07 10:55:31 -0600140 i2c_gpio_write_bit(bus, delay, ack);
141 bus->set_scl(bus, 0);
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +0200142 udelay(delay);
143}
144
145/**
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.
149 */
Michael Auchter7383edc2020-02-07 10:55:31 -0600150static void i2c_gpio_send_reset(struct i2c_gpio_bus *bus, int delay)
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +0200151{
152 int j;
153
154 for (j = 0; j < 9; j++)
Michael Auchter7383edc2020-02-07 10:55:31 -0600155 i2c_gpio_write_bit(bus, delay, 1);
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +0200156
Michael Auchter7383edc2020-02-07 10:55:31 -0600157 i2c_gpio_send_stop(bus, delay);
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +0200158}
159
160/* Set sda high with low clock, before reading slave data */
Michael Auchter7383edc2020-02-07 10:55:31 -0600161static void i2c_gpio_sda_high(struct i2c_gpio_bus *bus, int delay)
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +0200162{
Michael Auchter7383edc2020-02-07 10:55:31 -0600163 bus->set_scl(bus, 0);
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +0200164 udelay(delay);
Michael Auchter7383edc2020-02-07 10:55:31 -0600165 bus->set_sda(bus, 1);
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +0200166 udelay(delay);
167}
168
169/* Send 8 bits and look for an acknowledgement */
Michael Auchter7383edc2020-02-07 10:55:31 -0600170static int i2c_gpio_write_byte(struct i2c_gpio_bus *bus, int delay, uchar data)
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +0200171{
172 int j;
173 int nack;
174
175 for (j = 0; j < 8; j++) {
Michael Auchter7383edc2020-02-07 10:55:31 -0600176 i2c_gpio_write_bit(bus, delay, data & 0x80);
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +0200177 data <<= 1;
178 }
179
180 udelay(delay);
181
182 /* Look for an <ACK>(negative logic) and return it */
Michael Auchter7383edc2020-02-07 10:55:31 -0600183 i2c_gpio_sda_high(bus, delay);
184 nack = i2c_gpio_read_bit(bus, delay);
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +0200185
186 return nack; /* not a nack is an ack */
187}
188
189/**
190 * if ack == I2C_ACK, ACK the byte so can continue reading, else
191 * send I2C_NOACK to end the read.
192 */
Michael Auchter7383edc2020-02-07 10:55:31 -0600193static uchar i2c_gpio_read_byte(struct i2c_gpio_bus *bus, int delay, int ack)
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +0200194{
195 int data;
196 int j;
197
Michael Auchter7383edc2020-02-07 10:55:31 -0600198 i2c_gpio_sda_high(bus, delay);
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +0200199 data = 0;
200 for (j = 0; j < 8; j++) {
201 data <<= 1;
Michael Auchter7383edc2020-02-07 10:55:31 -0600202 data |= i2c_gpio_read_bit(bus, delay);
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +0200203 }
Michael Auchter7383edc2020-02-07 10:55:31 -0600204 i2c_gpio_send_ack(bus, delay, ack);
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +0200205
206 return data;
207}
208
209/* send start and the slave chip address */
Michael Auchter7383edc2020-02-07 10:55:31 -0600210int i2c_send_slave_addr(struct i2c_gpio_bus *bus, int delay, uchar chip)
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +0200211{
Michael Auchter7383edc2020-02-07 10:55:31 -0600212 i2c_gpio_send_start(bus, delay);
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +0200213
Michael Auchter7383edc2020-02-07 10:55:31 -0600214 if (i2c_gpio_write_byte(bus, delay, chip)) {
215 i2c_gpio_send_stop(bus, delay);
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +0200216 return -EIO;
217 }
218
219 return 0;
220}
221
222static int i2c_gpio_write_data(struct i2c_gpio_bus *bus, uchar chip,
223 uchar *buffer, int len,
224 bool end_with_repeated_start)
225{
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +0200226 unsigned int delay = bus->udelay;
227 int failures = 0;
228
229 debug("%s: chip %x buffer %p len %d\n", __func__, chip, buffer, len);
230
Michael Auchter7383edc2020-02-07 10:55:31 -0600231 if (i2c_send_slave_addr(bus, delay, chip << 1)) {
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +0200232 debug("i2c_write, no chip responded %02X\n", chip);
233 return -EIO;
234 }
235
236 while (len-- > 0) {
Michael Auchter7383edc2020-02-07 10:55:31 -0600237 if (i2c_gpio_write_byte(bus, delay, *buffer++))
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +0200238 failures++;
239 }
240
241 if (!end_with_repeated_start) {
Michael Auchter7383edc2020-02-07 10:55:31 -0600242 i2c_gpio_send_stop(bus, delay);
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +0200243 return failures;
244 }
245
Michael Auchter7383edc2020-02-07 10:55:31 -0600246 if (i2c_send_slave_addr(bus, delay, (chip << 1) | 0x1)) {
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +0200247 debug("i2c_write, no chip responded %02X\n", chip);
248 return -EIO;
249 }
250
251 return failures;
252}
253
254static int i2c_gpio_read_data(struct i2c_gpio_bus *bus, uchar chip,
255 uchar *buffer, int len)
256{
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +0200257 unsigned int delay = bus->udelay;
258
Alex Shumskyf315a482024-10-15 23:29:31 +0300259 if (!bus->get_sda)
260 return -EOPNOTSUPP;
261
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +0200262 debug("%s: chip %x buffer: %p len %d\n", __func__, chip, buffer, len);
263
264 while (len-- > 0)
Michael Auchter7383edc2020-02-07 10:55:31 -0600265 *buffer++ = i2c_gpio_read_byte(bus, delay, len == 0);
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +0200266
Michael Auchter7383edc2020-02-07 10:55:31 -0600267 i2c_gpio_send_stop(bus, delay);
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +0200268
269 return 0;
270}
271
272static int i2c_gpio_xfer(struct udevice *dev, struct i2c_msg *msg, int nmsgs)
273{
274 struct i2c_gpio_bus *bus = dev_get_priv(dev);
275 int ret;
276
277 for (; nmsgs > 0; nmsgs--, msg++) {
278 bool next_is_read = nmsgs > 1 && (msg[1].flags & I2C_M_RD);
279
280 if (msg->flags & I2C_M_RD) {
281 ret = i2c_gpio_read_data(bus, msg->addr, msg->buf,
282 msg->len);
283 } else {
284 ret = i2c_gpio_write_data(bus, msg->addr, msg->buf,
285 msg->len, next_is_read);
286 }
287
288 if (ret)
289 return -EREMOTEIO;
290 }
291
292 return 0;
293}
294
295static int i2c_gpio_probe(struct udevice *dev, uint chip, uint chip_flags)
296{
297 struct i2c_gpio_bus *bus = dev_get_priv(dev);
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +0200298 unsigned int delay = bus->udelay;
299 int ret;
300
Michael Auchter7383edc2020-02-07 10:55:31 -0600301 i2c_gpio_send_start(bus, delay);
302 ret = i2c_gpio_write_byte(bus, delay, (chip << 1) | 0);
303 i2c_gpio_send_stop(bus, delay);
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +0200304
305 debug("%s: bus: %d (%s) chip: %x flags: %x ret: %d\n",
Simon Glass8b85dfc2020-12-16 21:20:07 -0700306 __func__, dev_seq(dev), dev->name, chip, chip_flags, ret);
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +0200307
308 return ret;
309}
310
311static int i2c_gpio_set_bus_speed(struct udevice *dev, unsigned int speed_hz)
312{
313 struct i2c_gpio_bus *bus = dev_get_priv(dev);
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +0200314
315 bus->udelay = 1000000 / (speed_hz << 2);
316
Michael Auchter7383edc2020-02-07 10:55:31 -0600317 i2c_gpio_send_reset(bus, bus->udelay);
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +0200318
319 return 0;
320}
321
Marek Vasut4368c6a2020-02-07 16:57:52 +0100322static int i2c_gpio_drv_probe(struct udevice *dev)
323{
324 if (dev_read_bool(dev, "i2c-gpio,deblock")) {
325 /* @200kHz 9 clocks = 44us, 62us is ok */
326 const unsigned int DELAY_ABORT_SEQ = 62;
327 struct i2c_gpio_bus *bus = dev_get_priv(dev);
328
329 return i2c_deblock_gpio_loop(&bus->gpios[PIN_SDA],
330 &bus->gpios[PIN_SCL],
331 16, 5, DELAY_ABORT_SEQ);
332 }
333
334 return 0;
335}
336
Simon Glassd1998a92020-12-03 16:55:21 -0700337static int i2c_gpio_of_to_plat(struct udevice *dev)
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +0200338{
339 struct i2c_gpio_bus *bus = dev_get_priv(dev);
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +0200340 int ret;
341
Samuel Holland7282b432021-08-21 19:25:43 -0500342 /* "gpios" is deprecated and replaced by "sda-gpios" + "scl-gpios". */
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +0200343 ret = gpio_request_list_by_name(dev, "gpios", bus->gpios,
344 ARRAY_SIZE(bus->gpios), 0);
Chris Packham414236b2023-07-26 11:13:08 +1200345 if (ret == 0) {
Samuel Holland7282b432021-08-21 19:25:43 -0500346 ret = gpio_request_by_name(dev, "sda-gpios", 0,
347 &bus->gpios[PIN_SDA], 0);
348 if (ret < 0)
349 goto error;
350 ret = gpio_request_by_name(dev, "scl-gpios", 0,
351 &bus->gpios[PIN_SCL], 0);
352 }
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +0200353 if (ret < 0)
354 goto error;
355
Patrick Delaunay7a462742020-09-10 18:25:09 +0200356 bus->udelay = dev_read_u32_default(dev, "i2c-gpio,delay-us",
357 DEFAULT_UDELAY);
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +0200358
Alex Shumskyf315a482024-10-15 23:29:31 +0300359 if (dev_read_bool(dev, "i2c-gpio,sda-output-only"))
360 bus->get_sda = NULL;
361 else
362 bus->get_sda = i2c_gpio_sda_get;
Michael Auchter7383edc2020-02-07 10:55:31 -0600363 bus->set_sda = i2c_gpio_sda_set;
Patrick Delaunay7a462742020-09-10 18:25:09 +0200364 if (dev_read_bool(dev, "i2c-gpio,scl-output-only"))
Heiko Schocherba6fb2f2020-03-16 07:55:06 +0100365 bus->set_scl = i2c_gpio_scl_set_output_only;
366 else
367 bus->set_scl = i2c_gpio_scl_set;
Michael Auchter7383edc2020-02-07 10:55:31 -0600368
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +0200369 return 0;
370error:
Sergei Antonovb0c485f2022-10-20 17:28:14 +0300371 pr_err("Can't get %s gpios! Error: %d\n", dev->name, ret);
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +0200372 return ret;
373}
374
375static const struct dm_i2c_ops i2c_gpio_ops = {
376 .xfer = i2c_gpio_xfer,
377 .probe_chip = i2c_gpio_probe,
378 .set_bus_speed = i2c_gpio_set_bus_speed,
379};
380
381static const struct udevice_id i2c_gpio_ids[] = {
382 { .compatible = "i2c-gpio" },
383 { }
384};
385
386U_BOOT_DRIVER(i2c_gpio) = {
387 .name = "i2c-gpio",
388 .id = UCLASS_I2C,
389 .of_match = i2c_gpio_ids,
Marek Vasut4368c6a2020-02-07 16:57:52 +0100390 .probe = i2c_gpio_drv_probe,
Simon Glassd1998a92020-12-03 16:55:21 -0700391 .of_to_plat = i2c_gpio_of_to_plat,
Simon Glass41575d82020-12-03 16:55:17 -0700392 .priv_auto = sizeof(struct i2c_gpio_bus),
Przemyslaw Marczakc54473c2015-03-31 18:57:18 +0200393 .ops = &i2c_gpio_ops,
394};