blob: 8bc69e870fd4389cc532eaa7e95af561acb75b99 [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 Glassc05ed002020-05-10 11:40:11 -060018#include <linux/delay.h>
Simon Glassc6202d82014-12-10 08:55:47 -070019
Simon Glassc6202d82014-12-10 08:55:47 -070020#define I2C_MAX_OFFSET_LEN 4
21
Alexander Kochetkovaa541922018-03-27 17:52:27 +030022enum {
23 PIN_SDA = 0,
24 PIN_SCL,
25 PIN_COUNT,
26};
27
Simon Glass7d7db222015-07-02 18:15:39 -060028/* Useful debugging function */
29void i2c_dump_msgs(struct i2c_msg *msg, int nmsgs)
30{
31 int i;
32
33 for (i = 0; i < nmsgs; i++) {
34 struct i2c_msg *m = &msg[i];
35
36 printf(" %s %x len=%x", m->flags & I2C_M_RD ? "R" : "W",
37 msg->addr, msg->len);
38 if (!(m->flags & I2C_M_RD))
39 printf(": %x", m->buf[0]);
40 printf("\n");
41 }
42}
43
Simon Glassc6202d82014-12-10 08:55:47 -070044/**
45 * i2c_setup_offset() - Set up a new message with a chip offset
46 *
47 * @chip: Chip to use
48 * @offset: Byte offset within chip
49 * @offset_buf: Place to put byte offset
50 * @msg: Message buffer
51 * @return 0 if OK, -EADDRNOTAVAIL if the offset length is 0. In that case the
52 * message is still set up but will not contain an offset.
53 */
54static int i2c_setup_offset(struct dm_i2c_chip *chip, uint offset,
55 uint8_t offset_buf[], struct i2c_msg *msg)
56{
Robert Beckett85968522019-10-28 17:44:57 +000057 int offset_len = chip->offset_len;
Simon Glassc6202d82014-12-10 08:55:47 -070058
59 msg->addr = chip->chip_addr;
Robert Beckett85968522019-10-28 17:44:57 +000060 if (chip->chip_addr_offset_mask)
61 msg->addr |= (offset >> (8 * offset_len)) &
62 chip->chip_addr_offset_mask;
Simon Glassc6202d82014-12-10 08:55:47 -070063 msg->flags = chip->flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
64 msg->len = chip->offset_len;
65 msg->buf = offset_buf;
Robert Beckett85968522019-10-28 17:44:57 +000066 if (!offset_len)
Simon Glassc6202d82014-12-10 08:55:47 -070067 return -EADDRNOTAVAIL;
Robert Beckett85968522019-10-28 17:44:57 +000068 assert(offset_len <= I2C_MAX_OFFSET_LEN);
69
Simon Glassc6202d82014-12-10 08:55:47 -070070 while (offset_len--)
71 *offset_buf++ = offset >> (8 * offset_len);
72
73 return 0;
74}
75
76static int i2c_read_bytewise(struct udevice *dev, uint offset,
77 uint8_t *buffer, int len)
78{
Simon Glasse6f66ec2015-01-25 08:27:13 -070079 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
Simon Glassc6202d82014-12-10 08:55:47 -070080 struct udevice *bus = dev_get_parent(dev);
81 struct dm_i2c_ops *ops = i2c_get_ops(bus);
82 struct i2c_msg msg[2], *ptr;
83 uint8_t offset_buf[I2C_MAX_OFFSET_LEN];
84 int ret;
85 int i;
86
87 for (i = 0; i < len; i++) {
88 if (i2c_setup_offset(chip, offset + i, offset_buf, msg))
89 return -EINVAL;
90 ptr = msg + 1;
Robert Beckett85968522019-10-28 17:44:57 +000091 ptr->addr = msg->addr;
Simon Glassc6202d82014-12-10 08:55:47 -070092 ptr->flags = msg->flags | I2C_M_RD;
93 ptr->len = 1;
94 ptr->buf = &buffer[i];
95 ptr++;
96
97 ret = ops->xfer(bus, msg, ptr - msg);
98 if (ret)
99 return ret;
100 }
101
102 return 0;
103}
104
105static int i2c_write_bytewise(struct udevice *dev, uint offset,
106 const uint8_t *buffer, int len)
107{
Simon Glasse6f66ec2015-01-25 08:27:13 -0700108 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
Simon Glassc6202d82014-12-10 08:55:47 -0700109 struct udevice *bus = dev_get_parent(dev);
110 struct dm_i2c_ops *ops = i2c_get_ops(bus);
111 struct i2c_msg msg[1];
112 uint8_t buf[I2C_MAX_OFFSET_LEN + 1];
113 int ret;
114 int i;
115
116 for (i = 0; i < len; i++) {
117 if (i2c_setup_offset(chip, offset + i, buf, msg))
118 return -EINVAL;
119 buf[msg->len++] = buffer[i];
120
121 ret = ops->xfer(bus, msg, 1);
122 if (ret)
123 return ret;
124 }
125
126 return 0;
127}
128
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700129int dm_i2c_read(struct udevice *dev, uint offset, uint8_t *buffer, int len)
Simon Glassc6202d82014-12-10 08:55:47 -0700130{
Simon Glasse6f66ec2015-01-25 08:27:13 -0700131 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
Simon Glassc6202d82014-12-10 08:55:47 -0700132 struct udevice *bus = dev_get_parent(dev);
133 struct dm_i2c_ops *ops = i2c_get_ops(bus);
134 struct i2c_msg msg[2], *ptr;
135 uint8_t offset_buf[I2C_MAX_OFFSET_LEN];
136 int msg_count;
137
138 if (!ops->xfer)
139 return -ENOSYS;
140 if (chip->flags & DM_I2C_CHIP_RD_ADDRESS)
141 return i2c_read_bytewise(dev, offset, buffer, len);
142 ptr = msg;
143 if (!i2c_setup_offset(chip, offset, offset_buf, ptr))
144 ptr++;
145
146 if (len) {
Robert Beckett85968522019-10-28 17:44:57 +0000147 ptr->addr = msg->addr;
Simon Glassc6202d82014-12-10 08:55:47 -0700148 ptr->flags = chip->flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
149 ptr->flags |= I2C_M_RD;
150 ptr->len = len;
151 ptr->buf = buffer;
152 ptr++;
153 }
154 msg_count = ptr - msg;
155
156 return ops->xfer(bus, msg, msg_count);
157}
158
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700159int dm_i2c_write(struct udevice *dev, uint offset, const uint8_t *buffer,
160 int len)
Simon Glassc6202d82014-12-10 08:55:47 -0700161{
Simon Glasse6f66ec2015-01-25 08:27:13 -0700162 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
Simon Glassc6202d82014-12-10 08:55:47 -0700163 struct udevice *bus = dev_get_parent(dev);
164 struct dm_i2c_ops *ops = i2c_get_ops(bus);
165 struct i2c_msg msg[1];
166
167 if (!ops->xfer)
168 return -ENOSYS;
169
170 if (chip->flags & DM_I2C_CHIP_WR_ADDRESS)
171 return i2c_write_bytewise(dev, offset, buffer, len);
172 /*
173 * The simple approach would be to send two messages here: one to
174 * set the offset and one to write the bytes. However some drivers
175 * will not be expecting this, and some chips won't like how the
176 * driver presents this on the I2C bus.
177 *
178 * The API does not support separate offset and data. We could extend
179 * it with a flag indicating that there is data in the next message
180 * that needs to be processed in the same transaction. We could
181 * instead add an additional buffer to each message. For now, handle
182 * this in the uclass since it isn't clear what the impact on drivers
183 * would be with this extra complication. Unfortunately this means
184 * copying the message.
185 *
186 * Use the stack for small messages, malloc() for larger ones. We
187 * need to allow space for the offset (up to 4 bytes) and the message
188 * itself.
189 */
190 if (len < 64) {
191 uint8_t buf[I2C_MAX_OFFSET_LEN + len];
192
193 i2c_setup_offset(chip, offset, buf, msg);
194 msg->len += len;
195 memcpy(buf + chip->offset_len, buffer, len);
196
197 return ops->xfer(bus, msg, 1);
198 } else {
199 uint8_t *buf;
200 int ret;
201
202 buf = malloc(I2C_MAX_OFFSET_LEN + len);
203 if (!buf)
204 return -ENOMEM;
205 i2c_setup_offset(chip, offset, buf, msg);
206 msg->len += len;
207 memcpy(buf + chip->offset_len, buffer, len);
208
209 ret = ops->xfer(bus, msg, 1);
210 free(buf);
211 return ret;
212 }
213}
214
Simon Glassdf358c62015-07-02 18:15:42 -0600215int dm_i2c_xfer(struct udevice *dev, struct i2c_msg *msg, int nmsgs)
216{
217 struct udevice *bus = dev_get_parent(dev);
218 struct dm_i2c_ops *ops = i2c_get_ops(bus);
219
220 if (!ops->xfer)
221 return -ENOSYS;
222
223 return ops->xfer(bus, msg, nmsgs);
224}
225
Simon Glassba3864f2015-04-20 12:37:14 -0600226int dm_i2c_reg_read(struct udevice *dev, uint offset)
227{
228 uint8_t val;
229 int ret;
230
231 ret = dm_i2c_read(dev, offset, &val, 1);
232 if (ret < 0)
233 return ret;
234
235 return val;
236}
237
238int dm_i2c_reg_write(struct udevice *dev, uint offset, uint value)
239{
240 uint8_t val = value;
241
242 return dm_i2c_write(dev, offset, &val, 1);
243}
244
Simon Glassc6202d82014-12-10 08:55:47 -0700245/**
246 * i2c_probe_chip() - probe for a chip on a bus
247 *
248 * @bus: Bus to probe
249 * @chip_addr: Chip address to probe
250 * @flags: Flags for the chip
251 * @return 0 if found, -ENOSYS if the driver is invalid, -EREMOTEIO if the chip
252 * does not respond to probe
253 */
254static int i2c_probe_chip(struct udevice *bus, uint chip_addr,
255 enum dm_i2c_chip_flags chip_flags)
256{
257 struct dm_i2c_ops *ops = i2c_get_ops(bus);
258 struct i2c_msg msg[1];
259 int ret;
260
261 if (ops->probe_chip) {
262 ret = ops->probe_chip(bus, chip_addr, chip_flags);
263 if (!ret || ret != -ENOSYS)
264 return ret;
265 }
266
267 if (!ops->xfer)
268 return -ENOSYS;
269
270 /* Probe with a zero-length message */
271 msg->addr = chip_addr;
272 msg->flags = chip_flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
273 msg->len = 0;
274 msg->buf = NULL;
275
276 return ops->xfer(bus, msg, 1);
277}
278
Simon Glass25ab4b02015-01-25 08:26:55 -0700279static int i2c_bind_driver(struct udevice *bus, uint chip_addr, uint offset_len,
Simon Glassc6202d82014-12-10 08:55:47 -0700280 struct udevice **devp)
281{
Simon Glasse6f66ec2015-01-25 08:27:13 -0700282 struct dm_i2c_chip *chip;
Simon Glassc6202d82014-12-10 08:55:47 -0700283 char name[30], *str;
284 struct udevice *dev;
285 int ret;
286
287 snprintf(name, sizeof(name), "generic_%x", chip_addr);
288 str = strdup(name);
Simon Glass5c2d23b2015-02-18 14:10:28 -0700289 if (!str)
290 return -ENOMEM;
Simon Glassc6202d82014-12-10 08:55:47 -0700291 ret = device_bind_driver(bus, "i2c_generic_chip_drv", str, &dev);
292 debug("%s: device_bind_driver: ret=%d\n", __func__, ret);
293 if (ret)
294 goto err_bind;
295
296 /* Tell the device what we know about it */
Simon Glasse6f66ec2015-01-25 08:27:13 -0700297 chip = dev_get_parent_platdata(dev);
298 chip->chip_addr = chip_addr;
299 chip->offset_len = offset_len;
300 ret = device_probe(dev);
301 debug("%s: device_probe: ret=%d\n", __func__, ret);
Simon Glassc6202d82014-12-10 08:55:47 -0700302 if (ret)
303 goto err_probe;
304
305 *devp = dev;
306 return 0;
307
308err_probe:
Simon Glasse6f66ec2015-01-25 08:27:13 -0700309 /*
310 * If the device failed to probe, unbind it. There is nothing there
311 * on the bus so we don't want to leave it lying around
312 */
Simon Glassc6202d82014-12-10 08:55:47 -0700313 device_unbind(dev);
314err_bind:
315 free(str);
316 return ret;
317}
318
Simon Glass25ab4b02015-01-25 08:26:55 -0700319int i2c_get_chip(struct udevice *bus, uint chip_addr, uint offset_len,
320 struct udevice **devp)
Simon Glassc6202d82014-12-10 08:55:47 -0700321{
322 struct udevice *dev;
323
324 debug("%s: Searching bus '%s' for address %02x: ", __func__,
325 bus->name, chip_addr);
326 for (device_find_first_child(bus, &dev); dev;
327 device_find_next_child(&dev)) {
Simon Glasse6f66ec2015-01-25 08:27:13 -0700328 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
Simon Glassc6202d82014-12-10 08:55:47 -0700329 int ret;
330
Robert Beckett85968522019-10-28 17:44:57 +0000331 if (chip->chip_addr == (chip_addr &
332 ~chip->chip_addr_offset_mask)) {
Simon Glassc6202d82014-12-10 08:55:47 -0700333 ret = device_probe(dev);
334 debug("found, ret=%d\n", ret);
335 if (ret)
336 return ret;
337 *devp = dev;
338 return 0;
339 }
340 }
341 debug("not found\n");
Simon Glass25ab4b02015-01-25 08:26:55 -0700342 return i2c_bind_driver(bus, chip_addr, offset_len, devp);
Simon Glassc6202d82014-12-10 08:55:47 -0700343}
344
Simon Glass25ab4b02015-01-25 08:26:55 -0700345int i2c_get_chip_for_busnum(int busnum, int chip_addr, uint offset_len,
346 struct udevice **devp)
Simon Glassc6202d82014-12-10 08:55:47 -0700347{
348 struct udevice *bus;
349 int ret;
350
351 ret = uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus);
352 if (ret) {
353 debug("Cannot find I2C bus %d\n", busnum);
354 return ret;
355 }
Jean-Jacques Hiblotf32a8002018-12-07 14:50:38 +0100356
357 /* detect the presence of the chip on the bus */
358 ret = i2c_probe_chip(bus, chip_addr, 0);
359 debug("%s: bus='%s', address %02x, ret=%d\n", __func__, bus->name,
360 chip_addr, ret);
361 if (ret) {
362 debug("Cannot detect I2C chip %02x on bus %d\n", chip_addr,
363 busnum);
364 return ret;
365 }
366
Simon Glass25ab4b02015-01-25 08:26:55 -0700367 ret = i2c_get_chip(bus, chip_addr, offset_len, devp);
Simon Glassc6202d82014-12-10 08:55:47 -0700368 if (ret) {
369 debug("Cannot find I2C chip %02x on bus %d\n", chip_addr,
370 busnum);
371 return ret;
372 }
373
374 return 0;
375}
376
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700377int dm_i2c_probe(struct udevice *bus, uint chip_addr, uint chip_flags,
378 struct udevice **devp)
Simon Glassc6202d82014-12-10 08:55:47 -0700379{
380 int ret;
381
382 *devp = NULL;
383
384 /* First probe that chip */
385 ret = i2c_probe_chip(bus, chip_addr, chip_flags);
386 debug("%s: bus='%s', address %02x, ret=%d\n", __func__, bus->name,
387 chip_addr, ret);
388 if (ret)
389 return ret;
390
391 /* The chip was found, see if we have a driver, and probe it */
Simon Glass25ab4b02015-01-25 08:26:55 -0700392 ret = i2c_get_chip(bus, chip_addr, 1, devp);
Simon Glassc6202d82014-12-10 08:55:47 -0700393 debug("%s: i2c_get_chip: ret=%d\n", __func__, ret);
394
395 return ret;
396}
397
Simon Glassca88b9b2015-02-05 21:41:32 -0700398int dm_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
Simon Glassc6202d82014-12-10 08:55:47 -0700399{
400 struct dm_i2c_ops *ops = i2c_get_ops(bus);
Simon Glasse564f052015-03-05 12:25:20 -0700401 struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
Simon Glassc6202d82014-12-10 08:55:47 -0700402 int ret;
403
404 /*
405 * If we have a method, call it. If not then the driver probably wants
406 * to deal with speed changes on the next transfer. It can easily read
407 * the current speed from this uclass
408 */
409 if (ops->set_bus_speed) {
410 ret = ops->set_bus_speed(bus, speed);
411 if (ret)
412 return ret;
413 }
414 i2c->speed_hz = speed;
415
416 return 0;
417}
418
Simon Glassca88b9b2015-02-05 21:41:32 -0700419int dm_i2c_get_bus_speed(struct udevice *bus)
Simon Glassc6202d82014-12-10 08:55:47 -0700420{
421 struct dm_i2c_ops *ops = i2c_get_ops(bus);
Simon Glasse564f052015-03-05 12:25:20 -0700422 struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
Simon Glassc6202d82014-12-10 08:55:47 -0700423
424 if (!ops->get_bus_speed)
425 return i2c->speed_hz;
426
427 return ops->get_bus_speed(bus);
428}
429
430int i2c_set_chip_flags(struct udevice *dev, uint flags)
431{
432 struct udevice *bus = dev->parent;
Simon Glasse6f66ec2015-01-25 08:27:13 -0700433 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
Simon Glassc6202d82014-12-10 08:55:47 -0700434 struct dm_i2c_ops *ops = i2c_get_ops(bus);
435 int ret;
436
437 if (ops->set_flags) {
438 ret = ops->set_flags(dev, flags);
439 if (ret)
440 return ret;
441 }
442 chip->flags = flags;
443
444 return 0;
445}
446
447int i2c_get_chip_flags(struct udevice *dev, uint *flagsp)
448{
Simon Glasse6f66ec2015-01-25 08:27:13 -0700449 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
Simon Glassc6202d82014-12-10 08:55:47 -0700450
451 *flagsp = chip->flags;
452
453 return 0;
454}
455
456int i2c_set_chip_offset_len(struct udevice *dev, uint offset_len)
457{
Simon Glasse6f66ec2015-01-25 08:27:13 -0700458 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
Simon Glassc6202d82014-12-10 08:55:47 -0700459
460 if (offset_len > I2C_MAX_OFFSET_LEN)
461 return -EINVAL;
462 chip->offset_len = offset_len;
463
464 return 0;
465}
466
Simon Glass01501802015-05-04 11:30:58 -0600467int i2c_get_chip_offset_len(struct udevice *dev)
468{
469 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
470
471 return chip->offset_len;
472}
473
Robert Beckett85968522019-10-28 17:44:57 +0000474int i2c_set_chip_addr_offset_mask(struct udevice *dev, uint mask)
475{
476 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
477
478 chip->chip_addr_offset_mask = mask;
479
480 return 0;
481}
482
483uint i2c_get_chip_addr_offset_mask(struct udevice *dev)
484{
485 struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
486
487 return chip->chip_addr_offset_mask;
488}
489
Simon Glassbcee8d62019-12-06 21:41:35 -0700490#if CONFIG_IS_ENABLED(DM_GPIO)
Alexander Kochetkovaa541922018-03-27 17:52:27 +0300491static void i2c_gpio_set_pin(struct gpio_desc *pin, int bit)
492{
493 if (bit)
494 dm_gpio_set_dir_flags(pin, GPIOD_IS_IN);
495 else
496 dm_gpio_set_dir_flags(pin, GPIOD_IS_OUT |
497 GPIOD_ACTIVE_LOW |
498 GPIOD_IS_OUT_ACTIVE);
499}
500
501static int i2c_gpio_get_pin(struct gpio_desc *pin)
502{
503 return dm_gpio_get_value(pin);
504}
505
Marek Vasut72315222020-02-07 16:57:50 +0100506int i2c_deblock_gpio_loop(struct gpio_desc *sda_pin,
507 struct gpio_desc *scl_pin,
508 unsigned int scl_count,
Marek Vasuta1917282020-02-07 16:57:51 +0100509 unsigned int start_count,
Marek Vasut72315222020-02-07 16:57:50 +0100510 unsigned int delay)
Alexander Kochetkovaa541922018-03-27 17:52:27 +0300511{
Marek Vasuta1917282020-02-07 16:57:51 +0100512 int i, ret = -EREMOTEIO;
Alexander Kochetkovaa541922018-03-27 17:52:27 +0300513
514 i2c_gpio_set_pin(sda_pin, 1);
515 i2c_gpio_set_pin(scl_pin, 1);
Marek Vasut1f746a22020-02-07 16:57:49 +0100516 udelay(delay);
Alexander Kochetkovaa541922018-03-27 17:52:27 +0300517
518 /* Toggle SCL until slave release SDA */
Heinrich Schuchardtda585c32020-05-09 18:20:18 +0200519 for (; scl_count; --scl_count) {
Alexander Kochetkovaa541922018-03-27 17:52:27 +0300520 i2c_gpio_set_pin(scl_pin, 1);
Marek Vasut1f746a22020-02-07 16:57:49 +0100521 udelay(delay);
Alexander Kochetkovaa541922018-03-27 17:52:27 +0300522 i2c_gpio_set_pin(scl_pin, 0);
Marek Vasut1f746a22020-02-07 16:57:49 +0100523 udelay(delay);
Marek Vasuta1917282020-02-07 16:57:51 +0100524 if (i2c_gpio_get_pin(sda_pin)) {
525 ret = 0;
Alexander Kochetkovaa541922018-03-27 17:52:27 +0300526 break;
Marek Vasuta1917282020-02-07 16:57:51 +0100527 }
528 }
529
530 if (!ret && start_count) {
531 for (i = 0; i < start_count; i++) {
532 /* Send start condition */
533 udelay(delay);
534 i2c_gpio_set_pin(sda_pin, 1);
535 udelay(delay);
536 i2c_gpio_set_pin(scl_pin, 1);
537 udelay(delay);
538 i2c_gpio_set_pin(sda_pin, 0);
539 udelay(delay);
540 i2c_gpio_set_pin(scl_pin, 0);
541 }
Alexander Kochetkovaa541922018-03-27 17:52:27 +0300542 }
543
544 /* Then, send I2C stop */
545 i2c_gpio_set_pin(sda_pin, 0);
Marek Vasut1f746a22020-02-07 16:57:49 +0100546 udelay(delay);
Alexander Kochetkovaa541922018-03-27 17:52:27 +0300547
548 i2c_gpio_set_pin(scl_pin, 1);
Marek Vasut1f746a22020-02-07 16:57:49 +0100549 udelay(delay);
Alexander Kochetkovaa541922018-03-27 17:52:27 +0300550
551 i2c_gpio_set_pin(sda_pin, 1);
Marek Vasut1f746a22020-02-07 16:57:49 +0100552 udelay(delay);
Alexander Kochetkovaa541922018-03-27 17:52:27 +0300553
554 if (!i2c_gpio_get_pin(sda_pin) || !i2c_gpio_get_pin(scl_pin))
555 ret = -EREMOTEIO;
556
557 return ret;
558}
559
560static int i2c_deblock_gpio(struct udevice *bus)
561{
562 struct gpio_desc gpios[PIN_COUNT];
563 int ret, ret0;
564
565 ret = gpio_request_list_by_name(bus, "gpios", gpios,
566 ARRAY_SIZE(gpios), GPIOD_IS_IN);
567 if (ret != ARRAY_SIZE(gpios)) {
568 debug("%s: I2C Node '%s' has no 'gpios' property %s\n",
569 __func__, dev_read_name(bus), bus->name);
570 if (ret >= 0) {
571 gpio_free_list(bus, gpios, ret);
572 ret = -ENOENT;
573 }
574 goto out;
575 }
576
577 ret = pinctrl_select_state(bus, "gpio");
578 if (ret) {
579 debug("%s: I2C Node '%s' has no 'gpio' pinctrl state. %s\n",
580 __func__, dev_read_name(bus), bus->name);
581 goto out_no_pinctrl;
582 }
583
Marek Vasuta1917282020-02-07 16:57:51 +0100584 ret0 = i2c_deblock_gpio_loop(&gpios[PIN_SDA], &gpios[PIN_SCL], 9, 0, 5);
Alexander Kochetkovaa541922018-03-27 17:52:27 +0300585
586 ret = pinctrl_select_state(bus, "default");
587 if (ret) {
588 debug("%s: I2C Node '%s' has no 'default' pinctrl state. %s\n",
589 __func__, dev_read_name(bus), bus->name);
590 }
591
592 ret = !ret ? ret0 : ret;
593
594out_no_pinctrl:
595 gpio_free_list(bus, gpios, ARRAY_SIZE(gpios));
596out:
597 return ret;
598}
599#else
600static int i2c_deblock_gpio(struct udevice *bus)
601{
602 return -ENOSYS;
603}
Simon Glassbcee8d62019-12-06 21:41:35 -0700604#endif /* DM_GPIO */
Alexander Kochetkovaa541922018-03-27 17:52:27 +0300605
Simon Glassc6202d82014-12-10 08:55:47 -0700606int i2c_deblock(struct udevice *bus)
607{
608 struct dm_i2c_ops *ops = i2c_get_ops(bus);
609
Simon Glassc6202d82014-12-10 08:55:47 -0700610 if (!ops->deblock)
Alexander Kochetkovaa541922018-03-27 17:52:27 +0300611 return i2c_deblock_gpio(bus);
Simon Glassc6202d82014-12-10 08:55:47 -0700612
613 return ops->deblock(bus);
614}
615
Adam Fordafa8cdd2018-08-20 20:24:34 -0500616#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass17043082017-05-18 20:09:30 -0600617int i2c_chip_ofdata_to_platdata(struct udevice *dev, struct dm_i2c_chip *chip)
Simon Glassc6202d82014-12-10 08:55:47 -0700618{
Simon Glass17043082017-05-18 20:09:30 -0600619 int addr;
620
621 chip->offset_len = dev_read_u32_default(dev, "u-boot,i2c-offset-len",
622 1);
Simon Glassc6202d82014-12-10 08:55:47 -0700623 chip->flags = 0;
Simon Glass17043082017-05-18 20:09:30 -0600624 addr = dev_read_u32_default(dev, "reg", -1);
625 if (addr == -1) {
626 debug("%s: I2C Node '%s' has no 'reg' property %s\n", __func__,
627 dev_read_name(dev), dev->name);
Simon Glassc6202d82014-12-10 08:55:47 -0700628 return -EINVAL;
629 }
Simon Glass17043082017-05-18 20:09:30 -0600630 chip->chip_addr = addr;
Simon Glassc6202d82014-12-10 08:55:47 -0700631
632 return 0;
633}
Mugunthan V N5142ac72016-07-18 15:10:58 +0530634#endif
Simon Glassc6202d82014-12-10 08:55:47 -0700635
Lukasz Majewskia40fe212019-04-04 12:35:34 +0200636static int i2c_pre_probe(struct udevice *dev)
637{
638#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
639 struct dm_i2c_bus *i2c = dev_get_uclass_priv(dev);
640 unsigned int max = 0;
641 ofnode node;
642 int ret;
643
644 i2c->max_transaction_bytes = 0;
645 dev_for_each_subnode(node, dev) {
646 ret = ofnode_read_u32(node,
647 "u-boot,i2c-transaction-bytes",
648 &max);
649 if (!ret && max > i2c->max_transaction_bytes)
650 i2c->max_transaction_bytes = max;
651 }
652
653 debug("%s: I2C bus: %s max transaction bytes: %d\n", __func__,
654 dev->name, i2c->max_transaction_bytes);
655#endif
656 return 0;
657}
658
Simon Glassc6202d82014-12-10 08:55:47 -0700659static int i2c_post_probe(struct udevice *dev)
660{
Adam Fordafa8cdd2018-08-20 20:24:34 -0500661#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glasse564f052015-03-05 12:25:20 -0700662 struct dm_i2c_bus *i2c = dev_get_uclass_priv(dev);
Simon Glassc6202d82014-12-10 08:55:47 -0700663
Simon Glassf3d46152020-01-23 11:48:22 -0700664 i2c->speed_hz = dev_read_u32_default(dev, "clock-frequency",
665 I2C_SPEED_STANDARD_RATE);
Simon Glassc6202d82014-12-10 08:55:47 -0700666
Simon Glassca88b9b2015-02-05 21:41:32 -0700667 return dm_i2c_set_bus_speed(dev, i2c->speed_hz);
Mugunthan V N5142ac72016-07-18 15:10:58 +0530668#else
669 return 0;
670#endif
Simon Glassc6202d82014-12-10 08:55:47 -0700671}
672
Simon Glasse6f66ec2015-01-25 08:27:13 -0700673static int i2c_child_post_bind(struct udevice *dev)
674{
Adam Fordafa8cdd2018-08-20 20:24:34 -0500675#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glasse6f66ec2015-01-25 08:27:13 -0700676 struct dm_i2c_chip *plat = dev_get_parent_platdata(dev);
677
Simon Glass17043082017-05-18 20:09:30 -0600678 if (!dev_of_valid(dev))
Simon Glasse6f66ec2015-01-25 08:27:13 -0700679 return 0;
Simon Glass17043082017-05-18 20:09:30 -0600680 return i2c_chip_ofdata_to_platdata(dev, plat);
Mugunthan V N5142ac72016-07-18 15:10:58 +0530681#else
682 return 0;
683#endif
Simon Glasse6f66ec2015-01-25 08:27:13 -0700684}
685
Michal Simek6bfacc82019-01-31 16:31:01 +0100686struct i2c_priv {
687 int max_id;
688};
689
Michal Simek61607222019-01-31 16:31:02 +0100690static int i2c_post_bind(struct udevice *dev)
691{
692 struct uclass *class = dev->uclass;
693 struct i2c_priv *priv = class->priv;
694 int ret = 0;
695
696 /* Just for sure */
697 if (!priv)
698 return -ENOMEM;
699
700 debug("%s: %s, req_seq=%d\n", __func__, dev->name, dev->req_seq);
701
702 /* if there is no alias ID, use the first free */
703 if (dev->req_seq == -1)
704 dev->req_seq = ++priv->max_id;
705
706 debug("%s: %s, new req_seq=%d\n", __func__, dev->name, dev->req_seq);
707
708#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
709 ret = dm_scan_fdt_dev(dev);
710#endif
711 return ret;
712}
713
Michal Simek6bfacc82019-01-31 16:31:01 +0100714int i2c_uclass_init(struct uclass *class)
715{
716 struct i2c_priv *priv = class->priv;
717
718 /* Just for sure */
719 if (!priv)
720 return -ENOMEM;
721
722 /* Get the last allocated alias. */
Simon Glassf3d46152020-01-23 11:48:22 -0700723 if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA))
724 priv->max_id = dev_read_alias_highest_id("i2c");
725 else
726 priv->max_id = -1;
Michal Simek6bfacc82019-01-31 16:31:01 +0100727
728 debug("%s: highest alias id is %d\n", __func__, priv->max_id);
729
730 return 0;
731}
732
Simon Glassc6202d82014-12-10 08:55:47 -0700733UCLASS_DRIVER(i2c) = {
734 .id = UCLASS_I2C,
735 .name = "i2c",
Simon Glass9cc36a22015-01-25 08:27:05 -0700736 .flags = DM_UC_FLAG_SEQ_ALIAS,
Michal Simek61607222019-01-31 16:31:02 +0100737 .post_bind = i2c_post_bind,
Michal Simek6bfacc82019-01-31 16:31:01 +0100738 .init = i2c_uclass_init,
739 .priv_auto_alloc_size = sizeof(struct i2c_priv),
Lukasz Majewskia40fe212019-04-04 12:35:34 +0200740 .pre_probe = i2c_pre_probe,
Simon Glassc6202d82014-12-10 08:55:47 -0700741 .post_probe = i2c_post_probe,
Simon Glasse6f66ec2015-01-25 08:27:13 -0700742 .per_device_auto_alloc_size = sizeof(struct dm_i2c_bus),
743 .per_child_platdata_auto_alloc_size = sizeof(struct dm_i2c_chip),
744 .child_post_bind = i2c_child_post_bind,
Simon Glassc6202d82014-12-10 08:55:47 -0700745};
746
747UCLASS_DRIVER(i2c_generic) = {
748 .id = UCLASS_I2C_GENERIC,
749 .name = "i2c_generic",
750};
751
752U_BOOT_DRIVER(i2c_generic_chip_drv) = {
753 .name = "i2c_generic_chip_drv",
754 .id = UCLASS_I2C_GENERIC,
755};