blob: 32aed921b70a9479d51624b31e30518aa54d0d5d [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassc6202d82014-12-10 08:55:47 -07002/*
3 * Copyright (c) 2014 Google, Inc
Simon Glassc6202d82014-12-10 08:55:47 -07004 */
5
6#include <common.h>
7#include <dm.h>
8#include <errno.h>
Simon Glassc6202d82014-12-10 08:55:47 -07009#include <i2c.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060010#include <log.h>
Simon Glassc6202d82014-12-10 08:55:47 -070011#include <malloc.h>
12#include <dm/device-internal.h>
13#include <dm/lists.h>
Alexander Kochetkovaa541922018-03-27 17:52:27 +030014#include <dm/pinctrl.h>
Simon Glassbcee8d62019-12-06 21:41:35 -070015#if CONFIG_IS_ENABLED(DM_GPIO)
Alexander Kochetkovaa541922018-03-27 17:52:27 +030016#include <asm/gpio.h>
17#endif
Simon Glassc6202d82014-12-10 08:55:47 -070018
Simon Glassc6202d82014-12-10 08:55:47 -070019#define I2C_MAX_OFFSET_LEN 4
20
Alexander Kochetkovaa541922018-03-27 17:52:27 +030021enum {
22 PIN_SDA = 0,
23 PIN_SCL,
24 PIN_COUNT,
25};
26
Simon Glass7d7db222015-07-02 18:15:39 -060027/* Useful debugging function */
28void i2c_dump_msgs(struct i2c_msg *msg, int nmsgs)
29{
30 int i;
31
32 for (i = 0; i < nmsgs; i++) {
33 struct i2c_msg *m = &msg[i];
34
35 printf(" %s %x len=%x", m->flags & I2C_M_RD ? "R" : "W",
36 msg->addr, msg->len);
37 if (!(m->flags & I2C_M_RD))
38 printf(": %x", m->buf[0]);
39 printf("\n");
40 }
41}
42
Simon Glassc6202d82014-12-10 08:55:47 -070043/**
44 * i2c_setup_offset() - Set up a new message with a chip offset
45 *
46 * @chip: Chip to use
47 * @offset: Byte offset within chip
48 * @offset_buf: Place to put byte offset
49 * @msg: Message buffer
50 * @return 0 if OK, -EADDRNOTAVAIL if the offset length is 0. In that case the
51 * message is still set up but will not contain an offset.
52 */
53static int i2c_setup_offset(struct dm_i2c_chip *chip, uint offset,
54 uint8_t offset_buf[], struct i2c_msg *msg)
55{
Robert Beckett85968522019-10-28 17:44:57 +000056 int offset_len = chip->offset_len;
Simon Glassc6202d82014-12-10 08:55:47 -070057
58 msg->addr = chip->chip_addr;
Robert Beckett85968522019-10-28 17:44:57 +000059 if (chip->chip_addr_offset_mask)
60 msg->addr |= (offset >> (8 * offset_len)) &
61 chip->chip_addr_offset_mask;
Simon Glassc6202d82014-12-10 08:55:47 -070062 msg->flags = chip->flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
63 msg->len = chip->offset_len;
64 msg->buf = offset_buf;
Robert Beckett85968522019-10-28 17:44:57 +000065 if (!offset_len)
Simon Glassc6202d82014-12-10 08:55:47 -070066 return -EADDRNOTAVAIL;
Robert Beckett85968522019-10-28 17:44:57 +000067 assert(offset_len <= I2C_MAX_OFFSET_LEN);
68
Simon Glassc6202d82014-12-10 08:55:47 -070069 while (offset_len--)
70 *offset_buf++ = offset >> (8 * offset_len);
71
72 return 0;
73}
74
75static int i2c_read_bytewise(struct udevice *dev, uint offset,
76 uint8_t *buffer, int len)
77{
Simon Glasse6f66ec2015-01-25 08:27:13 -070078 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
Simon Glassc6202d82014-12-10 08:55:47 -070079 struct udevice *bus = dev_get_parent(dev);
80 struct dm_i2c_ops *ops = i2c_get_ops(bus);
81 struct i2c_msg msg[2], *ptr;
82 uint8_t offset_buf[I2C_MAX_OFFSET_LEN];
83 int ret;
84 int i;
85
86 for (i = 0; i < len; i++) {
87 if (i2c_setup_offset(chip, offset + i, offset_buf, msg))
88 return -EINVAL;
89 ptr = msg + 1;
Robert Beckett85968522019-10-28 17:44:57 +000090 ptr->addr = msg->addr;
Simon Glassc6202d82014-12-10 08:55:47 -070091 ptr->flags = msg->flags | I2C_M_RD;
92 ptr->len = 1;
93 ptr->buf = &buffer[i];
94 ptr++;
95
96 ret = ops->xfer(bus, msg, ptr - msg);
97 if (ret)
98 return ret;
99 }
100
101 return 0;
102}
103
104static int i2c_write_bytewise(struct udevice *dev, uint offset,
105 const uint8_t *buffer, int len)
106{
Simon Glasse6f66ec2015-01-25 08:27:13 -0700107 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
Simon Glassc6202d82014-12-10 08:55:47 -0700108 struct udevice *bus = dev_get_parent(dev);
109 struct dm_i2c_ops *ops = i2c_get_ops(bus);
110 struct i2c_msg msg[1];
111 uint8_t buf[I2C_MAX_OFFSET_LEN + 1];
112 int ret;
113 int i;
114
115 for (i = 0; i < len; i++) {
116 if (i2c_setup_offset(chip, offset + i, buf, msg))
117 return -EINVAL;
118 buf[msg->len++] = buffer[i];
119
120 ret = ops->xfer(bus, msg, 1);
121 if (ret)
122 return ret;
123 }
124
125 return 0;
126}
127
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700128int dm_i2c_read(struct udevice *dev, uint offset, uint8_t *buffer, int len)
Simon Glassc6202d82014-12-10 08:55:47 -0700129{
Simon Glasse6f66ec2015-01-25 08:27:13 -0700130 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
Simon Glassc6202d82014-12-10 08:55:47 -0700131 struct udevice *bus = dev_get_parent(dev);
132 struct dm_i2c_ops *ops = i2c_get_ops(bus);
133 struct i2c_msg msg[2], *ptr;
134 uint8_t offset_buf[I2C_MAX_OFFSET_LEN];
135 int msg_count;
136
137 if (!ops->xfer)
138 return -ENOSYS;
139 if (chip->flags & DM_I2C_CHIP_RD_ADDRESS)
140 return i2c_read_bytewise(dev, offset, buffer, len);
141 ptr = msg;
142 if (!i2c_setup_offset(chip, offset, offset_buf, ptr))
143 ptr++;
144
145 if (len) {
Robert Beckett85968522019-10-28 17:44:57 +0000146 ptr->addr = msg->addr;
Simon Glassc6202d82014-12-10 08:55:47 -0700147 ptr->flags = chip->flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
148 ptr->flags |= I2C_M_RD;
149 ptr->len = len;
150 ptr->buf = buffer;
151 ptr++;
152 }
153 msg_count = ptr - msg;
154
155 return ops->xfer(bus, msg, msg_count);
156}
157
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700158int dm_i2c_write(struct udevice *dev, uint offset, const uint8_t *buffer,
159 int len)
Simon Glassc6202d82014-12-10 08:55:47 -0700160{
Simon Glasse6f66ec2015-01-25 08:27:13 -0700161 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
Simon Glassc6202d82014-12-10 08:55:47 -0700162 struct udevice *bus = dev_get_parent(dev);
163 struct dm_i2c_ops *ops = i2c_get_ops(bus);
164 struct i2c_msg msg[1];
165
166 if (!ops->xfer)
167 return -ENOSYS;
168
169 if (chip->flags & DM_I2C_CHIP_WR_ADDRESS)
170 return i2c_write_bytewise(dev, offset, buffer, len);
171 /*
172 * The simple approach would be to send two messages here: one to
173 * set the offset and one to write the bytes. However some drivers
174 * will not be expecting this, and some chips won't like how the
175 * driver presents this on the I2C bus.
176 *
177 * The API does not support separate offset and data. We could extend
178 * it with a flag indicating that there is data in the next message
179 * that needs to be processed in the same transaction. We could
180 * instead add an additional buffer to each message. For now, handle
181 * this in the uclass since it isn't clear what the impact on drivers
182 * would be with this extra complication. Unfortunately this means
183 * copying the message.
184 *
185 * Use the stack for small messages, malloc() for larger ones. We
186 * need to allow space for the offset (up to 4 bytes) and the message
187 * itself.
188 */
189 if (len < 64) {
190 uint8_t buf[I2C_MAX_OFFSET_LEN + len];
191
192 i2c_setup_offset(chip, offset, buf, msg);
193 msg->len += len;
194 memcpy(buf + chip->offset_len, buffer, len);
195
196 return ops->xfer(bus, msg, 1);
197 } else {
198 uint8_t *buf;
199 int ret;
200
201 buf = malloc(I2C_MAX_OFFSET_LEN + len);
202 if (!buf)
203 return -ENOMEM;
204 i2c_setup_offset(chip, offset, buf, msg);
205 msg->len += len;
206 memcpy(buf + chip->offset_len, buffer, len);
207
208 ret = ops->xfer(bus, msg, 1);
209 free(buf);
210 return ret;
211 }
212}
213
Simon Glassdf358c62015-07-02 18:15:42 -0600214int dm_i2c_xfer(struct udevice *dev, struct i2c_msg *msg, int nmsgs)
215{
216 struct udevice *bus = dev_get_parent(dev);
217 struct dm_i2c_ops *ops = i2c_get_ops(bus);
218
219 if (!ops->xfer)
220 return -ENOSYS;
221
222 return ops->xfer(bus, msg, nmsgs);
223}
224
Simon Glassba3864f2015-04-20 12:37:14 -0600225int dm_i2c_reg_read(struct udevice *dev, uint offset)
226{
227 uint8_t val;
228 int ret;
229
230 ret = dm_i2c_read(dev, offset, &val, 1);
231 if (ret < 0)
232 return ret;
233
234 return val;
235}
236
237int dm_i2c_reg_write(struct udevice *dev, uint offset, uint value)
238{
239 uint8_t val = value;
240
241 return dm_i2c_write(dev, offset, &val, 1);
242}
243
Simon Glassc6202d82014-12-10 08:55:47 -0700244/**
245 * i2c_probe_chip() - probe for a chip on a bus
246 *
247 * @bus: Bus to probe
248 * @chip_addr: Chip address to probe
249 * @flags: Flags for the chip
250 * @return 0 if found, -ENOSYS if the driver is invalid, -EREMOTEIO if the chip
251 * does not respond to probe
252 */
253static int i2c_probe_chip(struct udevice *bus, uint chip_addr,
254 enum dm_i2c_chip_flags chip_flags)
255{
256 struct dm_i2c_ops *ops = i2c_get_ops(bus);
257 struct i2c_msg msg[1];
258 int ret;
259
260 if (ops->probe_chip) {
261 ret = ops->probe_chip(bus, chip_addr, chip_flags);
262 if (!ret || ret != -ENOSYS)
263 return ret;
264 }
265
266 if (!ops->xfer)
267 return -ENOSYS;
268
269 /* Probe with a zero-length message */
270 msg->addr = chip_addr;
271 msg->flags = chip_flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
272 msg->len = 0;
273 msg->buf = NULL;
274
275 return ops->xfer(bus, msg, 1);
276}
277
Simon Glass25ab4b02015-01-25 08:26:55 -0700278static int i2c_bind_driver(struct udevice *bus, uint chip_addr, uint offset_len,
Simon Glassc6202d82014-12-10 08:55:47 -0700279 struct udevice **devp)
280{
Simon Glasse6f66ec2015-01-25 08:27:13 -0700281 struct dm_i2c_chip *chip;
Simon Glassc6202d82014-12-10 08:55:47 -0700282 char name[30], *str;
283 struct udevice *dev;
284 int ret;
285
286 snprintf(name, sizeof(name), "generic_%x", chip_addr);
287 str = strdup(name);
Simon Glass5c2d23b2015-02-18 14:10:28 -0700288 if (!str)
289 return -ENOMEM;
Simon Glassc6202d82014-12-10 08:55:47 -0700290 ret = device_bind_driver(bus, "i2c_generic_chip_drv", str, &dev);
291 debug("%s: device_bind_driver: ret=%d\n", __func__, ret);
292 if (ret)
293 goto err_bind;
294
295 /* Tell the device what we know about it */
Simon Glasse6f66ec2015-01-25 08:27:13 -0700296 chip = dev_get_parent_platdata(dev);
297 chip->chip_addr = chip_addr;
298 chip->offset_len = offset_len;
299 ret = device_probe(dev);
300 debug("%s: device_probe: ret=%d\n", __func__, ret);
Simon Glassc6202d82014-12-10 08:55:47 -0700301 if (ret)
302 goto err_probe;
303
304 *devp = dev;
305 return 0;
306
307err_probe:
Simon Glasse6f66ec2015-01-25 08:27:13 -0700308 /*
309 * If the device failed to probe, unbind it. There is nothing there
310 * on the bus so we don't want to leave it lying around
311 */
Simon Glassc6202d82014-12-10 08:55:47 -0700312 device_unbind(dev);
313err_bind:
314 free(str);
315 return ret;
316}
317
Simon Glass25ab4b02015-01-25 08:26:55 -0700318int i2c_get_chip(struct udevice *bus, uint chip_addr, uint offset_len,
319 struct udevice **devp)
Simon Glassc6202d82014-12-10 08:55:47 -0700320{
321 struct udevice *dev;
322
323 debug("%s: Searching bus '%s' for address %02x: ", __func__,
324 bus->name, chip_addr);
325 for (device_find_first_child(bus, &dev); dev;
326 device_find_next_child(&dev)) {
Simon Glasse6f66ec2015-01-25 08:27:13 -0700327 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
Simon Glassc6202d82014-12-10 08:55:47 -0700328 int ret;
329
Robert Beckett85968522019-10-28 17:44:57 +0000330 if (chip->chip_addr == (chip_addr &
331 ~chip->chip_addr_offset_mask)) {
Simon Glassc6202d82014-12-10 08:55:47 -0700332 ret = device_probe(dev);
333 debug("found, ret=%d\n", ret);
334 if (ret)
335 return ret;
336 *devp = dev;
337 return 0;
338 }
339 }
340 debug("not found\n");
Simon Glass25ab4b02015-01-25 08:26:55 -0700341 return i2c_bind_driver(bus, chip_addr, offset_len, devp);
Simon Glassc6202d82014-12-10 08:55:47 -0700342}
343
Simon Glass25ab4b02015-01-25 08:26:55 -0700344int i2c_get_chip_for_busnum(int busnum, int chip_addr, uint offset_len,
345 struct udevice **devp)
Simon Glassc6202d82014-12-10 08:55:47 -0700346{
347 struct udevice *bus;
348 int ret;
349
350 ret = uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus);
351 if (ret) {
352 debug("Cannot find I2C bus %d\n", busnum);
353 return ret;
354 }
Jean-Jacques Hiblotf32a8002018-12-07 14:50:38 +0100355
356 /* detect the presence of the chip on the bus */
357 ret = i2c_probe_chip(bus, chip_addr, 0);
358 debug("%s: bus='%s', address %02x, ret=%d\n", __func__, bus->name,
359 chip_addr, ret);
360 if (ret) {
361 debug("Cannot detect I2C chip %02x on bus %d\n", chip_addr,
362 busnum);
363 return ret;
364 }
365
Simon Glass25ab4b02015-01-25 08:26:55 -0700366 ret = i2c_get_chip(bus, chip_addr, offset_len, devp);
Simon Glassc6202d82014-12-10 08:55:47 -0700367 if (ret) {
368 debug("Cannot find I2C chip %02x on bus %d\n", chip_addr,
369 busnum);
370 return ret;
371 }
372
373 return 0;
374}
375
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700376int dm_i2c_probe(struct udevice *bus, uint chip_addr, uint chip_flags,
377 struct udevice **devp)
Simon Glassc6202d82014-12-10 08:55:47 -0700378{
379 int ret;
380
381 *devp = NULL;
382
383 /* First probe that chip */
384 ret = i2c_probe_chip(bus, chip_addr, chip_flags);
385 debug("%s: bus='%s', address %02x, ret=%d\n", __func__, bus->name,
386 chip_addr, ret);
387 if (ret)
388 return ret;
389
390 /* The chip was found, see if we have a driver, and probe it */
Simon Glass25ab4b02015-01-25 08:26:55 -0700391 ret = i2c_get_chip(bus, chip_addr, 1, devp);
Simon Glassc6202d82014-12-10 08:55:47 -0700392 debug("%s: i2c_get_chip: ret=%d\n", __func__, ret);
393
394 return ret;
395}
396
Simon Glassca88b9b2015-02-05 21:41:32 -0700397int dm_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
Simon Glassc6202d82014-12-10 08:55:47 -0700398{
399 struct dm_i2c_ops *ops = i2c_get_ops(bus);
Simon Glasse564f052015-03-05 12:25:20 -0700400 struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
Simon Glassc6202d82014-12-10 08:55:47 -0700401 int ret;
402
403 /*
404 * If we have a method, call it. If not then the driver probably wants
405 * to deal with speed changes on the next transfer. It can easily read
406 * the current speed from this uclass
407 */
408 if (ops->set_bus_speed) {
409 ret = ops->set_bus_speed(bus, speed);
410 if (ret)
411 return ret;
412 }
413 i2c->speed_hz = speed;
414
415 return 0;
416}
417
Simon Glassca88b9b2015-02-05 21:41:32 -0700418int dm_i2c_get_bus_speed(struct udevice *bus)
Simon Glassc6202d82014-12-10 08:55:47 -0700419{
420 struct dm_i2c_ops *ops = i2c_get_ops(bus);
Simon Glasse564f052015-03-05 12:25:20 -0700421 struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
Simon Glassc6202d82014-12-10 08:55:47 -0700422
423 if (!ops->get_bus_speed)
424 return i2c->speed_hz;
425
426 return ops->get_bus_speed(bus);
427}
428
429int i2c_set_chip_flags(struct udevice *dev, uint flags)
430{
431 struct udevice *bus = dev->parent;
Simon Glasse6f66ec2015-01-25 08:27:13 -0700432 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
Simon Glassc6202d82014-12-10 08:55:47 -0700433 struct dm_i2c_ops *ops = i2c_get_ops(bus);
434 int ret;
435
436 if (ops->set_flags) {
437 ret = ops->set_flags(dev, flags);
438 if (ret)
439 return ret;
440 }
441 chip->flags = flags;
442
443 return 0;
444}
445
446int i2c_get_chip_flags(struct udevice *dev, uint *flagsp)
447{
Simon Glasse6f66ec2015-01-25 08:27:13 -0700448 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
Simon Glassc6202d82014-12-10 08:55:47 -0700449
450 *flagsp = chip->flags;
451
452 return 0;
453}
454
455int i2c_set_chip_offset_len(struct udevice *dev, uint offset_len)
456{
Simon Glasse6f66ec2015-01-25 08:27:13 -0700457 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
Simon Glassc6202d82014-12-10 08:55:47 -0700458
459 if (offset_len > I2C_MAX_OFFSET_LEN)
460 return -EINVAL;
461 chip->offset_len = offset_len;
462
463 return 0;
464}
465
Simon Glass01501802015-05-04 11:30:58 -0600466int i2c_get_chip_offset_len(struct udevice *dev)
467{
468 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
469
470 return chip->offset_len;
471}
472
Robert Beckett85968522019-10-28 17:44:57 +0000473int i2c_set_chip_addr_offset_mask(struct udevice *dev, uint mask)
474{
475 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
476
477 chip->chip_addr_offset_mask = mask;
478
479 return 0;
480}
481
482uint i2c_get_chip_addr_offset_mask(struct udevice *dev)
483{
484 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
485
486 return chip->chip_addr_offset_mask;
487}
488
Simon Glassbcee8d62019-12-06 21:41:35 -0700489#if CONFIG_IS_ENABLED(DM_GPIO)
Alexander Kochetkovaa541922018-03-27 17:52:27 +0300490static void i2c_gpio_set_pin(struct gpio_desc *pin, int bit)
491{
492 if (bit)
493 dm_gpio_set_dir_flags(pin, GPIOD_IS_IN);
494 else
495 dm_gpio_set_dir_flags(pin, GPIOD_IS_OUT |
496 GPIOD_ACTIVE_LOW |
497 GPIOD_IS_OUT_ACTIVE);
498}
499
500static int i2c_gpio_get_pin(struct gpio_desc *pin)
501{
502 return dm_gpio_get_value(pin);
503}
504
Marek Vasut72315222020-02-07 16:57:50 +0100505int i2c_deblock_gpio_loop(struct gpio_desc *sda_pin,
506 struct gpio_desc *scl_pin,
507 unsigned int scl_count,
Marek Vasuta1917282020-02-07 16:57:51 +0100508 unsigned int start_count,
Marek Vasut72315222020-02-07 16:57:50 +0100509 unsigned int delay)
Alexander Kochetkovaa541922018-03-27 17:52:27 +0300510{
Marek Vasuta1917282020-02-07 16:57:51 +0100511 int i, ret = -EREMOTEIO;
Alexander Kochetkovaa541922018-03-27 17:52:27 +0300512
513 i2c_gpio_set_pin(sda_pin, 1);
514 i2c_gpio_set_pin(scl_pin, 1);
Marek Vasut1f746a22020-02-07 16:57:49 +0100515 udelay(delay);
Alexander Kochetkovaa541922018-03-27 17:52:27 +0300516
517 /* Toggle SCL until slave release SDA */
Marek Vasut1f746a22020-02-07 16:57:49 +0100518 while (scl_count-- >= 0) {
Alexander Kochetkovaa541922018-03-27 17:52:27 +0300519 i2c_gpio_set_pin(scl_pin, 1);
Marek Vasut1f746a22020-02-07 16:57:49 +0100520 udelay(delay);
Alexander Kochetkovaa541922018-03-27 17:52:27 +0300521 i2c_gpio_set_pin(scl_pin, 0);
Marek Vasut1f746a22020-02-07 16:57:49 +0100522 udelay(delay);
Marek Vasuta1917282020-02-07 16:57:51 +0100523 if (i2c_gpio_get_pin(sda_pin)) {
524 ret = 0;
Alexander Kochetkovaa541922018-03-27 17:52:27 +0300525 break;
Marek Vasuta1917282020-02-07 16:57:51 +0100526 }
527 }
528
529 if (!ret && start_count) {
530 for (i = 0; i < start_count; i++) {
531 /* Send start condition */
532 udelay(delay);
533 i2c_gpio_set_pin(sda_pin, 1);
534 udelay(delay);
535 i2c_gpio_set_pin(scl_pin, 1);
536 udelay(delay);
537 i2c_gpio_set_pin(sda_pin, 0);
538 udelay(delay);
539 i2c_gpio_set_pin(scl_pin, 0);
540 }
Alexander Kochetkovaa541922018-03-27 17:52:27 +0300541 }
542
543 /* Then, send I2C stop */
544 i2c_gpio_set_pin(sda_pin, 0);
Marek Vasut1f746a22020-02-07 16:57:49 +0100545 udelay(delay);
Alexander Kochetkovaa541922018-03-27 17:52:27 +0300546
547 i2c_gpio_set_pin(scl_pin, 1);
Marek Vasut1f746a22020-02-07 16:57:49 +0100548 udelay(delay);
Alexander Kochetkovaa541922018-03-27 17:52:27 +0300549
550 i2c_gpio_set_pin(sda_pin, 1);
Marek Vasut1f746a22020-02-07 16:57:49 +0100551 udelay(delay);
Alexander Kochetkovaa541922018-03-27 17:52:27 +0300552
553 if (!i2c_gpio_get_pin(sda_pin) || !i2c_gpio_get_pin(scl_pin))
554 ret = -EREMOTEIO;
555
556 return ret;
557}
558
559static int i2c_deblock_gpio(struct udevice *bus)
560{
561 struct gpio_desc gpios[PIN_COUNT];
562 int ret, ret0;
563
564 ret = gpio_request_list_by_name(bus, "gpios", gpios,
565 ARRAY_SIZE(gpios), GPIOD_IS_IN);
566 if (ret != ARRAY_SIZE(gpios)) {
567 debug("%s: I2C Node '%s' has no 'gpios' property %s\n",
568 __func__, dev_read_name(bus), bus->name);
569 if (ret >= 0) {
570 gpio_free_list(bus, gpios, ret);
571 ret = -ENOENT;
572 }
573 goto out;
574 }
575
576 ret = pinctrl_select_state(bus, "gpio");
577 if (ret) {
578 debug("%s: I2C Node '%s' has no 'gpio' pinctrl state. %s\n",
579 __func__, dev_read_name(bus), bus->name);
580 goto out_no_pinctrl;
581 }
582
Marek Vasuta1917282020-02-07 16:57:51 +0100583 ret0 = i2c_deblock_gpio_loop(&gpios[PIN_SDA], &gpios[PIN_SCL], 9, 0, 5);
Alexander Kochetkovaa541922018-03-27 17:52:27 +0300584
585 ret = pinctrl_select_state(bus, "default");
586 if (ret) {
587 debug("%s: I2C Node '%s' has no 'default' pinctrl state. %s\n",
588 __func__, dev_read_name(bus), bus->name);
589 }
590
591 ret = !ret ? ret0 : ret;
592
593out_no_pinctrl:
594 gpio_free_list(bus, gpios, ARRAY_SIZE(gpios));
595out:
596 return ret;
597}
598#else
599static int i2c_deblock_gpio(struct udevice *bus)
600{
601 return -ENOSYS;
602}
Simon Glassbcee8d62019-12-06 21:41:35 -0700603#endif /* DM_GPIO */
Alexander Kochetkovaa541922018-03-27 17:52:27 +0300604
Simon Glassc6202d82014-12-10 08:55:47 -0700605int i2c_deblock(struct udevice *bus)
606{
607 struct dm_i2c_ops *ops = i2c_get_ops(bus);
608
Simon Glassc6202d82014-12-10 08:55:47 -0700609 if (!ops->deblock)
Alexander Kochetkovaa541922018-03-27 17:52:27 +0300610 return i2c_deblock_gpio(bus);
Simon Glassc6202d82014-12-10 08:55:47 -0700611
612 return ops->deblock(bus);
613}
614
Adam Fordafa8cdd2018-08-20 20:24:34 -0500615#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass17043082017-05-18 20:09:30 -0600616int i2c_chip_ofdata_to_platdata(struct udevice *dev, struct dm_i2c_chip *chip)
Simon Glassc6202d82014-12-10 08:55:47 -0700617{
Simon Glass17043082017-05-18 20:09:30 -0600618 int addr;
619
620 chip->offset_len = dev_read_u32_default(dev, "u-boot,i2c-offset-len",
621 1);
Simon Glassc6202d82014-12-10 08:55:47 -0700622 chip->flags = 0;
Simon Glass17043082017-05-18 20:09:30 -0600623 addr = dev_read_u32_default(dev, "reg", -1);
624 if (addr == -1) {
625 debug("%s: I2C Node '%s' has no 'reg' property %s\n", __func__,
626 dev_read_name(dev), dev->name);
Simon Glassc6202d82014-12-10 08:55:47 -0700627 return -EINVAL;
628 }
Simon Glass17043082017-05-18 20:09:30 -0600629 chip->chip_addr = addr;
Simon Glassc6202d82014-12-10 08:55:47 -0700630
631 return 0;
632}
Mugunthan V N5142ac72016-07-18 15:10:58 +0530633#endif
Simon Glassc6202d82014-12-10 08:55:47 -0700634
Lukasz Majewskia40fe212019-04-04 12:35:34 +0200635static int i2c_pre_probe(struct udevice *dev)
636{
637#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
638 struct dm_i2c_bus *i2c = dev_get_uclass_priv(dev);
639 unsigned int max = 0;
640 ofnode node;
641 int ret;
642
643 i2c->max_transaction_bytes = 0;
644 dev_for_each_subnode(node, dev) {
645 ret = ofnode_read_u32(node,
646 "u-boot,i2c-transaction-bytes",
647 &max);
648 if (!ret && max > i2c->max_transaction_bytes)
649 i2c->max_transaction_bytes = max;
650 }
651
652 debug("%s: I2C bus: %s max transaction bytes: %d\n", __func__,
653 dev->name, i2c->max_transaction_bytes);
654#endif
655 return 0;
656}
657
Simon Glassc6202d82014-12-10 08:55:47 -0700658static int i2c_post_probe(struct udevice *dev)
659{
Adam Fordafa8cdd2018-08-20 20:24:34 -0500660#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glasse564f052015-03-05 12:25:20 -0700661 struct dm_i2c_bus *i2c = dev_get_uclass_priv(dev);
Simon Glassc6202d82014-12-10 08:55:47 -0700662
Simon Glassf3d46152020-01-23 11:48:22 -0700663 i2c->speed_hz = dev_read_u32_default(dev, "clock-frequency",
664 I2C_SPEED_STANDARD_RATE);
Simon Glassc6202d82014-12-10 08:55:47 -0700665
Simon Glassca88b9b2015-02-05 21:41:32 -0700666 return dm_i2c_set_bus_speed(dev, i2c->speed_hz);
Mugunthan V N5142ac72016-07-18 15:10:58 +0530667#else
668 return 0;
669#endif
Simon Glassc6202d82014-12-10 08:55:47 -0700670}
671
Simon Glasse6f66ec2015-01-25 08:27:13 -0700672static int i2c_child_post_bind(struct udevice *dev)
673{
Adam Fordafa8cdd2018-08-20 20:24:34 -0500674#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glasse6f66ec2015-01-25 08:27:13 -0700675 struct dm_i2c_chip *plat = dev_get_parent_platdata(dev);
676
Simon Glass17043082017-05-18 20:09:30 -0600677 if (!dev_of_valid(dev))
Simon Glasse6f66ec2015-01-25 08:27:13 -0700678 return 0;
Simon Glass17043082017-05-18 20:09:30 -0600679 return i2c_chip_ofdata_to_platdata(dev, plat);
Mugunthan V N5142ac72016-07-18 15:10:58 +0530680#else
681 return 0;
682#endif
Simon Glasse6f66ec2015-01-25 08:27:13 -0700683}
684
Michal Simek6bfacc82019-01-31 16:31:01 +0100685struct i2c_priv {
686 int max_id;
687};
688
Michal Simek61607222019-01-31 16:31:02 +0100689static int i2c_post_bind(struct udevice *dev)
690{
691 struct uclass *class = dev->uclass;
692 struct i2c_priv *priv = class->priv;
693 int ret = 0;
694
695 /* Just for sure */
696 if (!priv)
697 return -ENOMEM;
698
699 debug("%s: %s, req_seq=%d\n", __func__, dev->name, dev->req_seq);
700
701 /* if there is no alias ID, use the first free */
702 if (dev->req_seq == -1)
703 dev->req_seq = ++priv->max_id;
704
705 debug("%s: %s, new req_seq=%d\n", __func__, dev->name, dev->req_seq);
706
707#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
708 ret = dm_scan_fdt_dev(dev);
709#endif
710 return ret;
711}
712
Michal Simek6bfacc82019-01-31 16:31:01 +0100713int i2c_uclass_init(struct uclass *class)
714{
715 struct i2c_priv *priv = class->priv;
716
717 /* Just for sure */
718 if (!priv)
719 return -ENOMEM;
720
721 /* Get the last allocated alias. */
Simon Glassf3d46152020-01-23 11:48:22 -0700722 if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA))
723 priv->max_id = dev_read_alias_highest_id("i2c");
724 else
725 priv->max_id = -1;
Michal Simek6bfacc82019-01-31 16:31:01 +0100726
727 debug("%s: highest alias id is %d\n", __func__, priv->max_id);
728
729 return 0;
730}
731
Simon Glassc6202d82014-12-10 08:55:47 -0700732UCLASS_DRIVER(i2c) = {
733 .id = UCLASS_I2C,
734 .name = "i2c",
Simon Glass9cc36a22015-01-25 08:27:05 -0700735 .flags = DM_UC_FLAG_SEQ_ALIAS,
Michal Simek61607222019-01-31 16:31:02 +0100736 .post_bind = i2c_post_bind,
Michal Simek6bfacc82019-01-31 16:31:01 +0100737 .init = i2c_uclass_init,
738 .priv_auto_alloc_size = sizeof(struct i2c_priv),
Lukasz Majewskia40fe212019-04-04 12:35:34 +0200739 .pre_probe = i2c_pre_probe,
Simon Glassc6202d82014-12-10 08:55:47 -0700740 .post_probe = i2c_post_probe,
Simon Glasse6f66ec2015-01-25 08:27:13 -0700741 .per_device_auto_alloc_size = sizeof(struct dm_i2c_bus),
742 .per_child_platdata_auto_alloc_size = sizeof(struct dm_i2c_chip),
743 .child_post_bind = i2c_child_post_bind,
Simon Glassc6202d82014-12-10 08:55:47 -0700744};
745
746UCLASS_DRIVER(i2c_generic) = {
747 .id = UCLASS_I2C_GENERIC,
748 .name = "i2c_generic",
749};
750
751U_BOOT_DRIVER(i2c_generic_chip_drv) = {
752 .name = "i2c_generic_chip_drv",
753 .id = UCLASS_I2C_GENERIC,
754};