blob: 5fc3cfe42ef7f8181d86eb8cde6f910384dcd46e [file] [log] [blame]
Przemyslaw Marczakd3aa7e12015-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 */
8#include <common.h>
9#include <errno.h>
10#include <dm.h>
11#include <i2c.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +020013#include <asm/gpio.h>
Simon Glassdbd79542020-05-10 11:40:11 -060014#include <linux/delay.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060015#include <linux/printk.h>
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +020016
17#define DEFAULT_UDELAY 5
18#define RETRIES 0
19#define I2C_ACK 0
20#define I2C_NOACK 1
21
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +020022enum {
23 PIN_SDA = 0,
24 PIN_SCL,
25 PIN_COUNT,
26};
27
28struct i2c_gpio_bus {
29 /**
30 * udelay - delay [us] between GPIO toggle operations,
31 * which is 1/4 of I2C speed clock period.
32 */
33 int udelay;
34 /* sda, scl */
35 struct gpio_desc gpios[PIN_COUNT];
Michael Auchter5f6b1852020-02-07 10:55:31 -060036
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);
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +020040};
41
Michael Auchter5f6b1852020-02-07 10:55:31 -060042static int i2c_gpio_sda_get(struct i2c_gpio_bus *bus)
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +020043{
Michael Auchter5f6b1852020-02-07 10:55:31 -060044 struct gpio_desc *sda = &bus->gpios[PIN_SDA];
45
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +020046 return dm_gpio_get_value(sda);
47}
48
Michael Auchter5f6b1852020-02-07 10:55:31 -060049static void i2c_gpio_sda_set(struct i2c_gpio_bus *bus, int bit)
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +020050{
Michael Auchter5f6b1852020-02-07 10:55:31 -060051 struct gpio_desc *sda = &bus->gpios[PIN_SDA];
Simon Glassd20d4b72021-03-23 14:52:06 +130052 ulong flags;
Michael Auchter5f6b1852020-02-07 10:55:31 -060053
Axel Lin05474832015-04-26 11:05:38 +080054 if (bit)
Simon Glassd20d4b72021-03-23 14:52:06 +130055 flags = GPIOD_IS_IN;
Axel Lin05474832015-04-26 11:05:38 +080056 else
Simon Glassd20d4b72021-03-23 14:52:06 +130057 flags = GPIOD_IS_OUT;
58 dm_gpio_clrset_flags(sda, GPIOD_MASK_DIR, flags);
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +020059}
60
Michael Auchter5f6b1852020-02-07 10:55:31 -060061static void i2c_gpio_scl_set(struct i2c_gpio_bus *bus, int bit)
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +020062{
Michael Auchter5f6b1852020-02-07 10:55:31 -060063 struct gpio_desc *scl = &bus->gpios[PIN_SCL];
Heiko Schocher48f39d82020-03-16 07:55:06 +010064 int count = 0;
65
66 if (bit) {
Simon Glassd20d4b72021-03-23 14:52:06 +130067 dm_gpio_clrset_flags(scl, GPIOD_MASK_DIR, GPIOD_IS_IN);
Heiko Schocher48f39d82020-03-16 07:55:06 +010068 while (!dm_gpio_get_value(scl) && count++ < 100000)
69 udelay(1);
70
71 if (!dm_gpio_get_value(scl))
72 pr_err("timeout waiting on slave to release scl\n");
73 } else {
Simon Glassd20d4b72021-03-23 14:52:06 +130074 dm_gpio_clrset_flags(scl, GPIOD_MASK_DIR, GPIOD_IS_OUT);
Heiko Schocher48f39d82020-03-16 07:55:06 +010075 }
76}
77
78/* variant for output only gpios which cannot support clock stretching */
79static void i2c_gpio_scl_set_output_only(struct i2c_gpio_bus *bus, int bit)
80{
81 struct gpio_desc *scl = &bus->gpios[PIN_SCL];
Simon Glassd20d4b72021-03-23 14:52:06 +130082 ulong flags = GPIOD_IS_OUT;
Axel Lin05474832015-04-26 11:05:38 +080083
84 if (bit)
Simon Glassd20d4b72021-03-23 14:52:06 +130085 flags |= GPIOD_IS_OUT_ACTIVE;
86 dm_gpio_clrset_flags(scl, GPIOD_MASK_DIR, flags);
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +020087}
88
Michael Auchter5f6b1852020-02-07 10:55:31 -060089static void i2c_gpio_write_bit(struct i2c_gpio_bus *bus, int delay, uchar bit)
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +020090{
Michael Auchter5f6b1852020-02-07 10:55:31 -060091 bus->set_scl(bus, 0);
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +020092 udelay(delay);
Michael Auchter5f6b1852020-02-07 10:55:31 -060093 bus->set_sda(bus, bit);
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +020094 udelay(delay);
Michael Auchter5f6b1852020-02-07 10:55:31 -060095 bus->set_scl(bus, 1);
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +020096 udelay(2 * delay);
97}
98
Michael Auchter5f6b1852020-02-07 10:55:31 -060099static int i2c_gpio_read_bit(struct i2c_gpio_bus *bus, int delay)
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +0200100{
101 int value;
102
Michael Auchter5f6b1852020-02-07 10:55:31 -0600103 bus->set_scl(bus, 1);
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +0200104 udelay(delay);
Michael Auchter5f6b1852020-02-07 10:55:31 -0600105 value = bus->get_sda(bus);
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +0200106 udelay(delay);
Michael Auchter5f6b1852020-02-07 10:55:31 -0600107 bus->set_scl(bus, 0);
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +0200108 udelay(2 * delay);
109
110 return value;
111}
112
113/* START: High -> Low on SDA while SCL is High */
Michael Auchter5f6b1852020-02-07 10:55:31 -0600114static void i2c_gpio_send_start(struct i2c_gpio_bus *bus, int delay)
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +0200115{
116 udelay(delay);
Michael Auchter5f6b1852020-02-07 10:55:31 -0600117 bus->set_sda(bus, 1);
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +0200118 udelay(delay);
Michael Auchter5f6b1852020-02-07 10:55:31 -0600119 bus->set_scl(bus, 1);
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +0200120 udelay(delay);
Michael Auchter5f6b1852020-02-07 10:55:31 -0600121 bus->set_sda(bus, 0);
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +0200122 udelay(delay);
123}
124
125/* STOP: Low -> High on SDA while SCL is High */
Michael Auchter5f6b1852020-02-07 10:55:31 -0600126static void i2c_gpio_send_stop(struct i2c_gpio_bus *bus, int delay)
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +0200127{
Michael Auchter5f6b1852020-02-07 10:55:31 -0600128 bus->set_scl(bus, 0);
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +0200129 udelay(delay);
Michael Auchter5f6b1852020-02-07 10:55:31 -0600130 bus->set_sda(bus, 0);
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +0200131 udelay(delay);
Michael Auchter5f6b1852020-02-07 10:55:31 -0600132 bus->set_scl(bus, 1);
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +0200133 udelay(delay);
Michael Auchter5f6b1852020-02-07 10:55:31 -0600134 bus->set_sda(bus, 1);
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +0200135 udelay(delay);
136}
137
138/* ack should be I2C_ACK or I2C_NOACK */
Michael Auchter5f6b1852020-02-07 10:55:31 -0600139static void i2c_gpio_send_ack(struct i2c_gpio_bus *bus, int delay, int ack)
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +0200140{
Michael Auchter5f6b1852020-02-07 10:55:31 -0600141 i2c_gpio_write_bit(bus, delay, ack);
142 bus->set_scl(bus, 0);
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +0200143 udelay(delay);
144}
145
146/**
147 * Send a reset sequence consisting of 9 clocks with the data signal high
148 * to clock any confused device back into an idle state. Also send a
149 * <stop> at the end of the sequence for belts & suspenders.
150 */
Michael Auchter5f6b1852020-02-07 10:55:31 -0600151static void i2c_gpio_send_reset(struct i2c_gpio_bus *bus, int delay)
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +0200152{
153 int j;
154
155 for (j = 0; j < 9; j++)
Michael Auchter5f6b1852020-02-07 10:55:31 -0600156 i2c_gpio_write_bit(bus, delay, 1);
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +0200157
Michael Auchter5f6b1852020-02-07 10:55:31 -0600158 i2c_gpio_send_stop(bus, delay);
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +0200159}
160
161/* Set sda high with low clock, before reading slave data */
Michael Auchter5f6b1852020-02-07 10:55:31 -0600162static void i2c_gpio_sda_high(struct i2c_gpio_bus *bus, int delay)
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +0200163{
Michael Auchter5f6b1852020-02-07 10:55:31 -0600164 bus->set_scl(bus, 0);
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +0200165 udelay(delay);
Michael Auchter5f6b1852020-02-07 10:55:31 -0600166 bus->set_sda(bus, 1);
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +0200167 udelay(delay);
168}
169
170/* Send 8 bits and look for an acknowledgement */
Michael Auchter5f6b1852020-02-07 10:55:31 -0600171static int i2c_gpio_write_byte(struct i2c_gpio_bus *bus, int delay, uchar data)
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +0200172{
173 int j;
174 int nack;
175
176 for (j = 0; j < 8; j++) {
Michael Auchter5f6b1852020-02-07 10:55:31 -0600177 i2c_gpio_write_bit(bus, delay, data & 0x80);
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +0200178 data <<= 1;
179 }
180
181 udelay(delay);
182
183 /* Look for an <ACK>(negative logic) and return it */
Michael Auchter5f6b1852020-02-07 10:55:31 -0600184 i2c_gpio_sda_high(bus, delay);
185 nack = i2c_gpio_read_bit(bus, delay);
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +0200186
187 return nack; /* not a nack is an ack */
188}
189
190/**
191 * if ack == I2C_ACK, ACK the byte so can continue reading, else
192 * send I2C_NOACK to end the read.
193 */
Michael Auchter5f6b1852020-02-07 10:55:31 -0600194static uchar i2c_gpio_read_byte(struct i2c_gpio_bus *bus, int delay, int ack)
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +0200195{
196 int data;
197 int j;
198
Michael Auchter5f6b1852020-02-07 10:55:31 -0600199 i2c_gpio_sda_high(bus, delay);
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +0200200 data = 0;
201 for (j = 0; j < 8; j++) {
202 data <<= 1;
Michael Auchter5f6b1852020-02-07 10:55:31 -0600203 data |= i2c_gpio_read_bit(bus, delay);
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +0200204 }
Michael Auchter5f6b1852020-02-07 10:55:31 -0600205 i2c_gpio_send_ack(bus, delay, ack);
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +0200206
207 return data;
208}
209
210/* send start and the slave chip address */
Michael Auchter5f6b1852020-02-07 10:55:31 -0600211int i2c_send_slave_addr(struct i2c_gpio_bus *bus, int delay, uchar chip)
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +0200212{
Michael Auchter5f6b1852020-02-07 10:55:31 -0600213 i2c_gpio_send_start(bus, delay);
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +0200214
Michael Auchter5f6b1852020-02-07 10:55:31 -0600215 if (i2c_gpio_write_byte(bus, delay, chip)) {
216 i2c_gpio_send_stop(bus, delay);
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +0200217 return -EIO;
218 }
219
220 return 0;
221}
222
223static int i2c_gpio_write_data(struct i2c_gpio_bus *bus, uchar chip,
224 uchar *buffer, int len,
225 bool end_with_repeated_start)
226{
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +0200227 unsigned int delay = bus->udelay;
228 int failures = 0;
229
230 debug("%s: chip %x buffer %p len %d\n", __func__, chip, buffer, len);
231
Michael Auchter5f6b1852020-02-07 10:55:31 -0600232 if (i2c_send_slave_addr(bus, delay, chip << 1)) {
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +0200233 debug("i2c_write, no chip responded %02X\n", chip);
234 return -EIO;
235 }
236
237 while (len-- > 0) {
Michael Auchter5f6b1852020-02-07 10:55:31 -0600238 if (i2c_gpio_write_byte(bus, delay, *buffer++))
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +0200239 failures++;
240 }
241
242 if (!end_with_repeated_start) {
Michael Auchter5f6b1852020-02-07 10:55:31 -0600243 i2c_gpio_send_stop(bus, delay);
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +0200244 return failures;
245 }
246
Michael Auchter5f6b1852020-02-07 10:55:31 -0600247 if (i2c_send_slave_addr(bus, delay, (chip << 1) | 0x1)) {
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +0200248 debug("i2c_write, no chip responded %02X\n", chip);
249 return -EIO;
250 }
251
252 return failures;
253}
254
255static int i2c_gpio_read_data(struct i2c_gpio_bus *bus, uchar chip,
256 uchar *buffer, int len)
257{
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +0200258 unsigned int delay = bus->udelay;
259
260 debug("%s: chip %x buffer: %p len %d\n", __func__, chip, buffer, len);
261
262 while (len-- > 0)
Michael Auchter5f6b1852020-02-07 10:55:31 -0600263 *buffer++ = i2c_gpio_read_byte(bus, delay, len == 0);
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +0200264
Michael Auchter5f6b1852020-02-07 10:55:31 -0600265 i2c_gpio_send_stop(bus, delay);
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +0200266
267 return 0;
268}
269
270static int i2c_gpio_xfer(struct udevice *dev, struct i2c_msg *msg, int nmsgs)
271{
272 struct i2c_gpio_bus *bus = dev_get_priv(dev);
273 int ret;
274
275 for (; nmsgs > 0; nmsgs--, msg++) {
276 bool next_is_read = nmsgs > 1 && (msg[1].flags & I2C_M_RD);
277
278 if (msg->flags & I2C_M_RD) {
279 ret = i2c_gpio_read_data(bus, msg->addr, msg->buf,
280 msg->len);
281 } else {
282 ret = i2c_gpio_write_data(bus, msg->addr, msg->buf,
283 msg->len, next_is_read);
284 }
285
286 if (ret)
287 return -EREMOTEIO;
288 }
289
290 return 0;
291}
292
293static int i2c_gpio_probe(struct udevice *dev, uint chip, uint chip_flags)
294{
295 struct i2c_gpio_bus *bus = dev_get_priv(dev);
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +0200296 unsigned int delay = bus->udelay;
297 int ret;
298
Michael Auchter5f6b1852020-02-07 10:55:31 -0600299 i2c_gpio_send_start(bus, delay);
300 ret = i2c_gpio_write_byte(bus, delay, (chip << 1) | 0);
301 i2c_gpio_send_stop(bus, delay);
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +0200302
303 debug("%s: bus: %d (%s) chip: %x flags: %x ret: %d\n",
Simon Glass75e534b2020-12-16 21:20:07 -0700304 __func__, dev_seq(dev), dev->name, chip, chip_flags, ret);
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +0200305
306 return ret;
307}
308
309static int i2c_gpio_set_bus_speed(struct udevice *dev, unsigned int speed_hz)
310{
311 struct i2c_gpio_bus *bus = dev_get_priv(dev);
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +0200312
313 bus->udelay = 1000000 / (speed_hz << 2);
314
Michael Auchter5f6b1852020-02-07 10:55:31 -0600315 i2c_gpio_send_reset(bus, bus->udelay);
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +0200316
317 return 0;
318}
319
Marek Vasut9f52cc42020-02-07 16:57:52 +0100320static int i2c_gpio_drv_probe(struct udevice *dev)
321{
322 if (dev_read_bool(dev, "i2c-gpio,deblock")) {
323 /* @200kHz 9 clocks = 44us, 62us is ok */
324 const unsigned int DELAY_ABORT_SEQ = 62;
325 struct i2c_gpio_bus *bus = dev_get_priv(dev);
326
327 return i2c_deblock_gpio_loop(&bus->gpios[PIN_SDA],
328 &bus->gpios[PIN_SCL],
329 16, 5, DELAY_ABORT_SEQ);
330 }
331
332 return 0;
333}
334
Simon Glassaad29ae2020-12-03 16:55:21 -0700335static int i2c_gpio_of_to_plat(struct udevice *dev)
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +0200336{
337 struct i2c_gpio_bus *bus = dev_get_priv(dev);
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +0200338 int ret;
339
Samuel Hollandb66238b2021-08-21 19:25:43 -0500340 /* "gpios" is deprecated and replaced by "sda-gpios" + "scl-gpios". */
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +0200341 ret = gpio_request_list_by_name(dev, "gpios", bus->gpios,
342 ARRAY_SIZE(bus->gpios), 0);
Chris Packhamd3447b52023-07-26 11:13:08 +1200343 if (ret == 0) {
Samuel Hollandb66238b2021-08-21 19:25:43 -0500344 ret = gpio_request_by_name(dev, "sda-gpios", 0,
345 &bus->gpios[PIN_SDA], 0);
346 if (ret < 0)
347 goto error;
348 ret = gpio_request_by_name(dev, "scl-gpios", 0,
349 &bus->gpios[PIN_SCL], 0);
350 }
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +0200351 if (ret < 0)
352 goto error;
353
Patrick Delaunay008573b2020-09-10 18:25:09 +0200354 bus->udelay = dev_read_u32_default(dev, "i2c-gpio,delay-us",
355 DEFAULT_UDELAY);
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +0200356
Michael Auchter5f6b1852020-02-07 10:55:31 -0600357 bus->get_sda = i2c_gpio_sda_get;
358 bus->set_sda = i2c_gpio_sda_set;
Patrick Delaunay008573b2020-09-10 18:25:09 +0200359 if (dev_read_bool(dev, "i2c-gpio,scl-output-only"))
Heiko Schocher48f39d82020-03-16 07:55:06 +0100360 bus->set_scl = i2c_gpio_scl_set_output_only;
361 else
362 bus->set_scl = i2c_gpio_scl_set;
Michael Auchter5f6b1852020-02-07 10:55:31 -0600363
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +0200364 return 0;
365error:
Sergei Antonov2ff09752022-10-20 17:28:14 +0300366 pr_err("Can't get %s gpios! Error: %d\n", dev->name, ret);
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +0200367 return ret;
368}
369
370static const struct dm_i2c_ops i2c_gpio_ops = {
371 .xfer = i2c_gpio_xfer,
372 .probe_chip = i2c_gpio_probe,
373 .set_bus_speed = i2c_gpio_set_bus_speed,
374};
375
376static const struct udevice_id i2c_gpio_ids[] = {
377 { .compatible = "i2c-gpio" },
378 { }
379};
380
381U_BOOT_DRIVER(i2c_gpio) = {
382 .name = "i2c-gpio",
383 .id = UCLASS_I2C,
384 .of_match = i2c_gpio_ids,
Marek Vasut9f52cc42020-02-07 16:57:52 +0100385 .probe = i2c_gpio_drv_probe,
Simon Glassaad29ae2020-12-03 16:55:21 -0700386 .of_to_plat = i2c_gpio_of_to_plat,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700387 .priv_auto = sizeof(struct i2c_gpio_bus),
Przemyslaw Marczakd3aa7e12015-03-31 18:57:18 +0200388 .ops = &i2c_gpio_ops,
389};