blob: 04c88503a2f335580f87bc2ef0563fddedae2d5c [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassc7a58902014-12-10 08:55:47 -07002/*
3 * Copyright (c) 2014 Google, Inc
Simon Glassc7a58902014-12-10 08:55:47 -07004 */
5
Patrick Delaunay81313352021-04-27 11:02:19 +02006#define LOG_CATEGORY UCLASS_I2C
7
Simon Glassc7a58902014-12-10 08:55:47 -07008#include <common.h>
9#include <dm.h>
10#include <errno.h>
Simon Glassc7a58902014-12-10 08:55:47 -070011#include <i2c.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Simon Glassc7a58902014-12-10 08:55:47 -070013#include <malloc.h>
Simon Glass31cc5b42020-09-22 12:45:01 -060014#include <acpi/acpi_device.h>
15#include <dm/acpi.h>
Simon Glassc7a58902014-12-10 08:55:47 -070016#include <dm/device-internal.h>
17#include <dm/lists.h>
Alexander Kochetkovdc96eb72018-03-27 17:52:27 +030018#include <dm/pinctrl.h>
Simon Glassfa4689a2019-12-06 21:41:35 -070019#if CONFIG_IS_ENABLED(DM_GPIO)
Alexander Kochetkovdc96eb72018-03-27 17:52:27 +030020#include <asm/gpio.h>
21#endif
Simon Glassdbd79542020-05-10 11:40:11 -060022#include <linux/delay.h>
Simon Glass31cc5b42020-09-22 12:45:01 -060023#include "acpi_i2c.h"
Simon Glassc7a58902014-12-10 08:55:47 -070024
Simon Glassc7a58902014-12-10 08:55:47 -070025#define I2C_MAX_OFFSET_LEN 4
26
Alexander Kochetkovdc96eb72018-03-27 17:52:27 +030027enum {
28 PIN_SDA = 0,
29 PIN_SCL,
30 PIN_COUNT,
31};
32
Simon Glassa8e64c42015-07-02 18:15:39 -060033/* Useful debugging function */
34void i2c_dump_msgs(struct i2c_msg *msg, int nmsgs)
35{
36 int i;
37
38 for (i = 0; i < nmsgs; i++) {
39 struct i2c_msg *m = &msg[i];
40
41 printf(" %s %x len=%x", m->flags & I2C_M_RD ? "R" : "W",
42 msg->addr, msg->len);
43 if (!(m->flags & I2C_M_RD))
44 printf(": %x", m->buf[0]);
45 printf("\n");
46 }
47}
48
Simon Glassc7a58902014-12-10 08:55:47 -070049/**
50 * i2c_setup_offset() - Set up a new message with a chip offset
51 *
52 * @chip: Chip to use
53 * @offset: Byte offset within chip
54 * @offset_buf: Place to put byte offset
55 * @msg: Message buffer
56 * @return 0 if OK, -EADDRNOTAVAIL if the offset length is 0. In that case the
57 * message is still set up but will not contain an offset.
58 */
59static int i2c_setup_offset(struct dm_i2c_chip *chip, uint offset,
60 uint8_t offset_buf[], struct i2c_msg *msg)
61{
Robert Beckett5f8e41a2019-10-28 17:44:57 +000062 int offset_len = chip->offset_len;
Simon Glassc7a58902014-12-10 08:55:47 -070063
64 msg->addr = chip->chip_addr;
Robert Beckett5f8e41a2019-10-28 17:44:57 +000065 if (chip->chip_addr_offset_mask)
66 msg->addr |= (offset >> (8 * offset_len)) &
67 chip->chip_addr_offset_mask;
Simon Glassc7a58902014-12-10 08:55:47 -070068 msg->flags = chip->flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
69 msg->len = chip->offset_len;
70 msg->buf = offset_buf;
Robert Beckett5f8e41a2019-10-28 17:44:57 +000071 if (!offset_len)
Simon Glassc7a58902014-12-10 08:55:47 -070072 return -EADDRNOTAVAIL;
Robert Beckett5f8e41a2019-10-28 17:44:57 +000073 assert(offset_len <= I2C_MAX_OFFSET_LEN);
74
Simon Glassc7a58902014-12-10 08:55:47 -070075 while (offset_len--)
76 *offset_buf++ = offset >> (8 * offset_len);
77
78 return 0;
79}
80
81static int i2c_read_bytewise(struct udevice *dev, uint offset,
82 uint8_t *buffer, int len)
83{
Simon Glass71fa5b42020-12-03 16:55:18 -070084 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
Simon Glassc7a58902014-12-10 08:55:47 -070085 struct udevice *bus = dev_get_parent(dev);
86 struct dm_i2c_ops *ops = i2c_get_ops(bus);
87 struct i2c_msg msg[2], *ptr;
88 uint8_t offset_buf[I2C_MAX_OFFSET_LEN];
89 int ret;
90 int i;
91
92 for (i = 0; i < len; i++) {
93 if (i2c_setup_offset(chip, offset + i, offset_buf, msg))
94 return -EINVAL;
95 ptr = msg + 1;
Robert Beckett5f8e41a2019-10-28 17:44:57 +000096 ptr->addr = msg->addr;
Simon Glassc7a58902014-12-10 08:55:47 -070097 ptr->flags = msg->flags | I2C_M_RD;
98 ptr->len = 1;
99 ptr->buf = &buffer[i];
100 ptr++;
101
102 ret = ops->xfer(bus, msg, ptr - msg);
103 if (ret)
104 return ret;
105 }
106
107 return 0;
108}
109
110static int i2c_write_bytewise(struct udevice *dev, uint offset,
111 const uint8_t *buffer, int len)
112{
Simon Glass71fa5b42020-12-03 16:55:18 -0700113 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
Simon Glassc7a58902014-12-10 08:55:47 -0700114 struct udevice *bus = dev_get_parent(dev);
115 struct dm_i2c_ops *ops = i2c_get_ops(bus);
116 struct i2c_msg msg[1];
117 uint8_t buf[I2C_MAX_OFFSET_LEN + 1];
118 int ret;
119 int i;
120
121 for (i = 0; i < len; i++) {
122 if (i2c_setup_offset(chip, offset + i, buf, msg))
123 return -EINVAL;
124 buf[msg->len++] = buffer[i];
125
126 ret = ops->xfer(bus, msg, 1);
127 if (ret)
128 return ret;
129 }
130
131 return 0;
132}
133
Simon Glass7d722762015-01-12 18:02:07 -0700134int dm_i2c_read(struct udevice *dev, uint offset, uint8_t *buffer, int len)
Simon Glassc7a58902014-12-10 08:55:47 -0700135{
Simon Glass71fa5b42020-12-03 16:55:18 -0700136 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
Simon Glassc7a58902014-12-10 08:55:47 -0700137 struct udevice *bus = dev_get_parent(dev);
138 struct dm_i2c_ops *ops = i2c_get_ops(bus);
139 struct i2c_msg msg[2], *ptr;
140 uint8_t offset_buf[I2C_MAX_OFFSET_LEN];
141 int msg_count;
142
143 if (!ops->xfer)
144 return -ENOSYS;
145 if (chip->flags & DM_I2C_CHIP_RD_ADDRESS)
146 return i2c_read_bytewise(dev, offset, buffer, len);
147 ptr = msg;
148 if (!i2c_setup_offset(chip, offset, offset_buf, ptr))
149 ptr++;
150
151 if (len) {
Robert Beckett5f8e41a2019-10-28 17:44:57 +0000152 ptr->addr = msg->addr;
Simon Glassc7a58902014-12-10 08:55:47 -0700153 ptr->flags = chip->flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
154 ptr->flags |= I2C_M_RD;
155 ptr->len = len;
156 ptr->buf = buffer;
157 ptr++;
158 }
159 msg_count = ptr - msg;
160
161 return ops->xfer(bus, msg, msg_count);
162}
163
Simon Glass7d722762015-01-12 18:02:07 -0700164int dm_i2c_write(struct udevice *dev, uint offset, const uint8_t *buffer,
165 int len)
Simon Glassc7a58902014-12-10 08:55:47 -0700166{
Simon Glass71fa5b42020-12-03 16:55:18 -0700167 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
Simon Glassc7a58902014-12-10 08:55:47 -0700168 struct udevice *bus = dev_get_parent(dev);
169 struct dm_i2c_ops *ops = i2c_get_ops(bus);
170 struct i2c_msg msg[1];
171
172 if (!ops->xfer)
173 return -ENOSYS;
174
175 if (chip->flags & DM_I2C_CHIP_WR_ADDRESS)
176 return i2c_write_bytewise(dev, offset, buffer, len);
177 /*
178 * The simple approach would be to send two messages here: one to
179 * set the offset and one to write the bytes. However some drivers
180 * will not be expecting this, and some chips won't like how the
181 * driver presents this on the I2C bus.
182 *
183 * The API does not support separate offset and data. We could extend
184 * it with a flag indicating that there is data in the next message
185 * that needs to be processed in the same transaction. We could
186 * instead add an additional buffer to each message. For now, handle
187 * this in the uclass since it isn't clear what the impact on drivers
188 * would be with this extra complication. Unfortunately this means
189 * copying the message.
190 *
191 * Use the stack for small messages, malloc() for larger ones. We
192 * need to allow space for the offset (up to 4 bytes) and the message
193 * itself.
194 */
195 if (len < 64) {
196 uint8_t buf[I2C_MAX_OFFSET_LEN + len];
197
198 i2c_setup_offset(chip, offset, buf, msg);
199 msg->len += len;
200 memcpy(buf + chip->offset_len, buffer, len);
201
202 return ops->xfer(bus, msg, 1);
203 } else {
204 uint8_t *buf;
205 int ret;
206
207 buf = malloc(I2C_MAX_OFFSET_LEN + len);
208 if (!buf)
209 return -ENOMEM;
210 i2c_setup_offset(chip, offset, buf, msg);
211 msg->len += len;
212 memcpy(buf + chip->offset_len, buffer, len);
213
214 ret = ops->xfer(bus, msg, 1);
215 free(buf);
216 return ret;
217 }
218}
219
Simon Glassf2818852015-07-02 18:15:42 -0600220int dm_i2c_xfer(struct udevice *dev, struct i2c_msg *msg, int nmsgs)
221{
222 struct udevice *bus = dev_get_parent(dev);
223 struct dm_i2c_ops *ops = i2c_get_ops(bus);
224
225 if (!ops->xfer)
226 return -ENOSYS;
227
228 return ops->xfer(bus, msg, nmsgs);
229}
230
Simon Glass0c702612015-04-20 12:37:14 -0600231int dm_i2c_reg_read(struct udevice *dev, uint offset)
232{
233 uint8_t val;
234 int ret;
235
236 ret = dm_i2c_read(dev, offset, &val, 1);
237 if (ret < 0)
238 return ret;
239
240 return val;
241}
242
243int dm_i2c_reg_write(struct udevice *dev, uint offset, uint value)
244{
245 uint8_t val = value;
246
247 return dm_i2c_write(dev, offset, &val, 1);
248}
249
Simon Glassc7a58902014-12-10 08:55:47 -0700250/**
251 * i2c_probe_chip() - probe for a chip on a bus
252 *
253 * @bus: Bus to probe
254 * @chip_addr: Chip address to probe
255 * @flags: Flags for the chip
256 * @return 0 if found, -ENOSYS if the driver is invalid, -EREMOTEIO if the chip
257 * does not respond to probe
258 */
259static int i2c_probe_chip(struct udevice *bus, uint chip_addr,
260 enum dm_i2c_chip_flags chip_flags)
261{
262 struct dm_i2c_ops *ops = i2c_get_ops(bus);
263 struct i2c_msg msg[1];
264 int ret;
265
266 if (ops->probe_chip) {
267 ret = ops->probe_chip(bus, chip_addr, chip_flags);
268 if (!ret || ret != -ENOSYS)
269 return ret;
270 }
271
272 if (!ops->xfer)
273 return -ENOSYS;
274
275 /* Probe with a zero-length message */
276 msg->addr = chip_addr;
277 msg->flags = chip_flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
278 msg->len = 0;
279 msg->buf = NULL;
280
281 return ops->xfer(bus, msg, 1);
282}
283
Simon Glassa2723ae2015-01-25 08:26:55 -0700284static int i2c_bind_driver(struct udevice *bus, uint chip_addr, uint offset_len,
Simon Glassc7a58902014-12-10 08:55:47 -0700285 struct udevice **devp)
286{
Simon Glass713c3f02015-01-25 08:27:13 -0700287 struct dm_i2c_chip *chip;
Simon Glassc7a58902014-12-10 08:55:47 -0700288 char name[30], *str;
289 struct udevice *dev;
290 int ret;
291
292 snprintf(name, sizeof(name), "generic_%x", chip_addr);
293 str = strdup(name);
Simon Glassb75c4492015-02-18 14:10:28 -0700294 if (!str)
295 return -ENOMEM;
Simon Glassc7a58902014-12-10 08:55:47 -0700296 ret = device_bind_driver(bus, "i2c_generic_chip_drv", str, &dev);
297 debug("%s: device_bind_driver: ret=%d\n", __func__, ret);
298 if (ret)
299 goto err_bind;
300
301 /* Tell the device what we know about it */
Simon Glass71fa5b42020-12-03 16:55:18 -0700302 chip = dev_get_parent_plat(dev);
Simon Glass713c3f02015-01-25 08:27:13 -0700303 chip->chip_addr = chip_addr;
304 chip->offset_len = offset_len;
305 ret = device_probe(dev);
306 debug("%s: device_probe: ret=%d\n", __func__, ret);
Simon Glassc7a58902014-12-10 08:55:47 -0700307 if (ret)
308 goto err_probe;
309
310 *devp = dev;
311 return 0;
312
313err_probe:
Simon Glass713c3f02015-01-25 08:27:13 -0700314 /*
315 * If the device failed to probe, unbind it. There is nothing there
316 * on the bus so we don't want to leave it lying around
317 */
Simon Glassc7a58902014-12-10 08:55:47 -0700318 device_unbind(dev);
319err_bind:
320 free(str);
321 return ret;
322}
323
Simon Glassa2723ae2015-01-25 08:26:55 -0700324int i2c_get_chip(struct udevice *bus, uint chip_addr, uint offset_len,
325 struct udevice **devp)
Simon Glassc7a58902014-12-10 08:55:47 -0700326{
327 struct udevice *dev;
328
329 debug("%s: Searching bus '%s' for address %02x: ", __func__,
330 bus->name, chip_addr);
331 for (device_find_first_child(bus, &dev); dev;
332 device_find_next_child(&dev)) {
Simon Glass71fa5b42020-12-03 16:55:18 -0700333 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
Simon Glassc7a58902014-12-10 08:55:47 -0700334 int ret;
335
Robert Beckett5f8e41a2019-10-28 17:44:57 +0000336 if (chip->chip_addr == (chip_addr &
337 ~chip->chip_addr_offset_mask)) {
Simon Glassc7a58902014-12-10 08:55:47 -0700338 ret = device_probe(dev);
339 debug("found, ret=%d\n", ret);
340 if (ret)
341 return ret;
342 *devp = dev;
343 return 0;
344 }
345 }
346 debug("not found\n");
Simon Glassa2723ae2015-01-25 08:26:55 -0700347 return i2c_bind_driver(bus, chip_addr, offset_len, devp);
Simon Glassc7a58902014-12-10 08:55:47 -0700348}
349
Simon Glassa2723ae2015-01-25 08:26:55 -0700350int i2c_get_chip_for_busnum(int busnum, int chip_addr, uint offset_len,
351 struct udevice **devp)
Simon Glassc7a58902014-12-10 08:55:47 -0700352{
353 struct udevice *bus;
354 int ret;
355
356 ret = uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus);
357 if (ret) {
358 debug("Cannot find I2C bus %d\n", busnum);
359 return ret;
360 }
Jean-Jacques Hiblotac9ab242018-12-07 14:50:38 +0100361
362 /* detect the presence of the chip on the bus */
363 ret = i2c_probe_chip(bus, chip_addr, 0);
364 debug("%s: bus='%s', address %02x, ret=%d\n", __func__, bus->name,
365 chip_addr, ret);
366 if (ret) {
367 debug("Cannot detect I2C chip %02x on bus %d\n", chip_addr,
368 busnum);
369 return ret;
370 }
371
Simon Glassa2723ae2015-01-25 08:26:55 -0700372 ret = i2c_get_chip(bus, chip_addr, offset_len, devp);
Simon Glassc7a58902014-12-10 08:55:47 -0700373 if (ret) {
374 debug("Cannot find I2C chip %02x on bus %d\n", chip_addr,
375 busnum);
376 return ret;
377 }
378
379 return 0;
380}
381
Simon Glass7d722762015-01-12 18:02:07 -0700382int dm_i2c_probe(struct udevice *bus, uint chip_addr, uint chip_flags,
383 struct udevice **devp)
Simon Glassc7a58902014-12-10 08:55:47 -0700384{
385 int ret;
386
387 *devp = NULL;
388
389 /* First probe that chip */
390 ret = i2c_probe_chip(bus, chip_addr, chip_flags);
391 debug("%s: bus='%s', address %02x, ret=%d\n", __func__, bus->name,
392 chip_addr, ret);
393 if (ret)
394 return ret;
395
396 /* The chip was found, see if we have a driver, and probe it */
Simon Glassa2723ae2015-01-25 08:26:55 -0700397 ret = i2c_get_chip(bus, chip_addr, 1, devp);
Simon Glassc7a58902014-12-10 08:55:47 -0700398 debug("%s: i2c_get_chip: ret=%d\n", __func__, ret);
399
400 return ret;
401}
402
Simon Glasse4e8ff22015-02-05 21:41:32 -0700403int dm_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
Simon Glassc7a58902014-12-10 08:55:47 -0700404{
405 struct dm_i2c_ops *ops = i2c_get_ops(bus);
Simon Glassde0977b2015-03-05 12:25:20 -0700406 struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
Simon Glassc7a58902014-12-10 08:55:47 -0700407 int ret;
408
409 /*
410 * If we have a method, call it. If not then the driver probably wants
411 * to deal with speed changes on the next transfer. It can easily read
412 * the current speed from this uclass
413 */
414 if (ops->set_bus_speed) {
415 ret = ops->set_bus_speed(bus, speed);
416 if (ret)
417 return ret;
418 }
419 i2c->speed_hz = speed;
420
421 return 0;
422}
423
Simon Glasse4e8ff22015-02-05 21:41:32 -0700424int dm_i2c_get_bus_speed(struct udevice *bus)
Simon Glassc7a58902014-12-10 08:55:47 -0700425{
426 struct dm_i2c_ops *ops = i2c_get_ops(bus);
Simon Glassde0977b2015-03-05 12:25:20 -0700427 struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
Simon Glassc7a58902014-12-10 08:55:47 -0700428
429 if (!ops->get_bus_speed)
430 return i2c->speed_hz;
431
432 return ops->get_bus_speed(bus);
433}
434
435int i2c_set_chip_flags(struct udevice *dev, uint flags)
436{
437 struct udevice *bus = dev->parent;
Simon Glass71fa5b42020-12-03 16:55:18 -0700438 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
Simon Glassc7a58902014-12-10 08:55:47 -0700439 struct dm_i2c_ops *ops = i2c_get_ops(bus);
440 int ret;
441
442 if (ops->set_flags) {
443 ret = ops->set_flags(dev, flags);
444 if (ret)
445 return ret;
446 }
447 chip->flags = flags;
448
449 return 0;
450}
451
452int i2c_get_chip_flags(struct udevice *dev, uint *flagsp)
453{
Simon Glass71fa5b42020-12-03 16:55:18 -0700454 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
Simon Glassc7a58902014-12-10 08:55:47 -0700455
456 *flagsp = chip->flags;
457
458 return 0;
459}
460
461int i2c_set_chip_offset_len(struct udevice *dev, uint offset_len)
462{
Simon Glass71fa5b42020-12-03 16:55:18 -0700463 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
Simon Glassc7a58902014-12-10 08:55:47 -0700464
465 if (offset_len > I2C_MAX_OFFSET_LEN)
Simon Glasse9983052020-07-07 21:32:28 -0600466 return log_ret(-EINVAL);
Simon Glassc7a58902014-12-10 08:55:47 -0700467 chip->offset_len = offset_len;
468
469 return 0;
470}
471
Simon Glass9a1589a2015-05-04 11:30:58 -0600472int i2c_get_chip_offset_len(struct udevice *dev)
473{
Simon Glass71fa5b42020-12-03 16:55:18 -0700474 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
Simon Glass9a1589a2015-05-04 11:30:58 -0600475
476 return chip->offset_len;
477}
478
Robert Beckett5f8e41a2019-10-28 17:44:57 +0000479int i2c_set_chip_addr_offset_mask(struct udevice *dev, uint mask)
480{
Simon Glass71fa5b42020-12-03 16:55:18 -0700481 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
Robert Beckett5f8e41a2019-10-28 17:44:57 +0000482
483 chip->chip_addr_offset_mask = mask;
484
485 return 0;
486}
487
488uint i2c_get_chip_addr_offset_mask(struct udevice *dev)
489{
Simon Glass71fa5b42020-12-03 16:55:18 -0700490 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
Robert Beckett5f8e41a2019-10-28 17:44:57 +0000491
492 return chip->chip_addr_offset_mask;
493}
494
Simon Glassfa4689a2019-12-06 21:41:35 -0700495#if CONFIG_IS_ENABLED(DM_GPIO)
Alexander Kochetkovdc96eb72018-03-27 17:52:27 +0300496static void i2c_gpio_set_pin(struct gpio_desc *pin, int bit)
497{
498 if (bit)
499 dm_gpio_set_dir_flags(pin, GPIOD_IS_IN);
500 else
501 dm_gpio_set_dir_flags(pin, GPIOD_IS_OUT |
502 GPIOD_ACTIVE_LOW |
503 GPIOD_IS_OUT_ACTIVE);
504}
505
506static int i2c_gpio_get_pin(struct gpio_desc *pin)
507{
508 return dm_gpio_get_value(pin);
509}
510
Marek Vasute2af3412020-02-07 16:57:50 +0100511int i2c_deblock_gpio_loop(struct gpio_desc *sda_pin,
512 struct gpio_desc *scl_pin,
513 unsigned int scl_count,
Marek Vasut39de9252020-02-07 16:57:51 +0100514 unsigned int start_count,
Marek Vasute2af3412020-02-07 16:57:50 +0100515 unsigned int delay)
Alexander Kochetkovdc96eb72018-03-27 17:52:27 +0300516{
Marek Vasut39de9252020-02-07 16:57:51 +0100517 int i, ret = -EREMOTEIO;
Alexander Kochetkovdc96eb72018-03-27 17:52:27 +0300518
519 i2c_gpio_set_pin(sda_pin, 1);
520 i2c_gpio_set_pin(scl_pin, 1);
Marek Vasut2ffffc32020-02-07 16:57:49 +0100521 udelay(delay);
Alexander Kochetkovdc96eb72018-03-27 17:52:27 +0300522
523 /* Toggle SCL until slave release SDA */
Heinrich Schuchardtf1524a22020-05-09 18:20:18 +0200524 for (; scl_count; --scl_count) {
Alexander Kochetkovdc96eb72018-03-27 17:52:27 +0300525 i2c_gpio_set_pin(scl_pin, 1);
Marek Vasut2ffffc32020-02-07 16:57:49 +0100526 udelay(delay);
Alexander Kochetkovdc96eb72018-03-27 17:52:27 +0300527 i2c_gpio_set_pin(scl_pin, 0);
Marek Vasut2ffffc32020-02-07 16:57:49 +0100528 udelay(delay);
Marek Vasut39de9252020-02-07 16:57:51 +0100529 if (i2c_gpio_get_pin(sda_pin)) {
530 ret = 0;
Alexander Kochetkovdc96eb72018-03-27 17:52:27 +0300531 break;
Marek Vasut39de9252020-02-07 16:57:51 +0100532 }
533 }
534
535 if (!ret && start_count) {
536 for (i = 0; i < start_count; i++) {
537 /* Send start condition */
538 udelay(delay);
539 i2c_gpio_set_pin(sda_pin, 1);
540 udelay(delay);
541 i2c_gpio_set_pin(scl_pin, 1);
542 udelay(delay);
543 i2c_gpio_set_pin(sda_pin, 0);
544 udelay(delay);
545 i2c_gpio_set_pin(scl_pin, 0);
546 }
Alexander Kochetkovdc96eb72018-03-27 17:52:27 +0300547 }
548
549 /* Then, send I2C stop */
550 i2c_gpio_set_pin(sda_pin, 0);
Marek Vasut2ffffc32020-02-07 16:57:49 +0100551 udelay(delay);
Alexander Kochetkovdc96eb72018-03-27 17:52:27 +0300552
553 i2c_gpio_set_pin(scl_pin, 1);
Marek Vasut2ffffc32020-02-07 16:57:49 +0100554 udelay(delay);
Alexander Kochetkovdc96eb72018-03-27 17:52:27 +0300555
556 i2c_gpio_set_pin(sda_pin, 1);
Marek Vasut2ffffc32020-02-07 16:57:49 +0100557 udelay(delay);
Alexander Kochetkovdc96eb72018-03-27 17:52:27 +0300558
559 if (!i2c_gpio_get_pin(sda_pin) || !i2c_gpio_get_pin(scl_pin))
560 ret = -EREMOTEIO;
561
562 return ret;
563}
564
565static int i2c_deblock_gpio(struct udevice *bus)
566{
567 struct gpio_desc gpios[PIN_COUNT];
568 int ret, ret0;
569
570 ret = gpio_request_list_by_name(bus, "gpios", gpios,
571 ARRAY_SIZE(gpios), GPIOD_IS_IN);
572 if (ret != ARRAY_SIZE(gpios)) {
573 debug("%s: I2C Node '%s' has no 'gpios' property %s\n",
574 __func__, dev_read_name(bus), bus->name);
575 if (ret >= 0) {
576 gpio_free_list(bus, gpios, ret);
577 ret = -ENOENT;
578 }
579 goto out;
580 }
581
582 ret = pinctrl_select_state(bus, "gpio");
583 if (ret) {
584 debug("%s: I2C Node '%s' has no 'gpio' pinctrl state. %s\n",
585 __func__, dev_read_name(bus), bus->name);
586 goto out_no_pinctrl;
587 }
588
Marek Vasut39de9252020-02-07 16:57:51 +0100589 ret0 = i2c_deblock_gpio_loop(&gpios[PIN_SDA], &gpios[PIN_SCL], 9, 0, 5);
Alexander Kochetkovdc96eb72018-03-27 17:52:27 +0300590
591 ret = pinctrl_select_state(bus, "default");
592 if (ret) {
593 debug("%s: I2C Node '%s' has no 'default' pinctrl state. %s\n",
594 __func__, dev_read_name(bus), bus->name);
595 }
596
597 ret = !ret ? ret0 : ret;
598
599out_no_pinctrl:
600 gpio_free_list(bus, gpios, ARRAY_SIZE(gpios));
601out:
602 return ret;
603}
604#else
605static int i2c_deblock_gpio(struct udevice *bus)
606{
607 return -ENOSYS;
608}
Simon Glassfa4689a2019-12-06 21:41:35 -0700609#endif /* DM_GPIO */
Alexander Kochetkovdc96eb72018-03-27 17:52:27 +0300610
Simon Glassc7a58902014-12-10 08:55:47 -0700611int i2c_deblock(struct udevice *bus)
612{
613 struct dm_i2c_ops *ops = i2c_get_ops(bus);
614
Simon Glassc7a58902014-12-10 08:55:47 -0700615 if (!ops->deblock)
Alexander Kochetkovdc96eb72018-03-27 17:52:27 +0300616 return i2c_deblock_gpio(bus);
Simon Glassc7a58902014-12-10 08:55:47 -0700617
618 return ops->deblock(bus);
619}
620
Adam Ford1004fcd2018-08-20 20:24:34 -0500621#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glassaad29ae2020-12-03 16:55:21 -0700622int i2c_chip_of_to_plat(struct udevice *dev, struct dm_i2c_chip *chip)
Simon Glassc7a58902014-12-10 08:55:47 -0700623{
Simon Glass654e3a02017-05-18 20:09:30 -0600624 int addr;
625
626 chip->offset_len = dev_read_u32_default(dev, "u-boot,i2c-offset-len",
627 1);
Simon Glassc7a58902014-12-10 08:55:47 -0700628 chip->flags = 0;
Simon Glass654e3a02017-05-18 20:09:30 -0600629 addr = dev_read_u32_default(dev, "reg", -1);
630 if (addr == -1) {
631 debug("%s: I2C Node '%s' has no 'reg' property %s\n", __func__,
632 dev_read_name(dev), dev->name);
Simon Glasse9983052020-07-07 21:32:28 -0600633 return log_ret(-EINVAL);
Simon Glassc7a58902014-12-10 08:55:47 -0700634 }
Simon Glass654e3a02017-05-18 20:09:30 -0600635 chip->chip_addr = addr;
Simon Glassc7a58902014-12-10 08:55:47 -0700636
637 return 0;
638}
Mugunthan V Nbbf1ccf2016-07-18 15:10:58 +0530639#endif
Simon Glassc7a58902014-12-10 08:55:47 -0700640
Lukasz Majewskibc9aad62019-04-04 12:35:34 +0200641static int i2c_pre_probe(struct udevice *dev)
642{
643#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
644 struct dm_i2c_bus *i2c = dev_get_uclass_priv(dev);
645 unsigned int max = 0;
646 ofnode node;
647 int ret;
648
649 i2c->max_transaction_bytes = 0;
650 dev_for_each_subnode(node, dev) {
651 ret = ofnode_read_u32(node,
652 "u-boot,i2c-transaction-bytes",
653 &max);
654 if (!ret && max > i2c->max_transaction_bytes)
655 i2c->max_transaction_bytes = max;
656 }
657
658 debug("%s: I2C bus: %s max transaction bytes: %d\n", __func__,
659 dev->name, i2c->max_transaction_bytes);
660#endif
661 return 0;
662}
663
Simon Glassc7a58902014-12-10 08:55:47 -0700664static int i2c_post_probe(struct udevice *dev)
665{
Adam Ford1004fcd2018-08-20 20:24:34 -0500666#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glassde0977b2015-03-05 12:25:20 -0700667 struct dm_i2c_bus *i2c = dev_get_uclass_priv(dev);
Simon Glassc7a58902014-12-10 08:55:47 -0700668
Simon Glassf0c99c52020-01-23 11:48:22 -0700669 i2c->speed_hz = dev_read_u32_default(dev, "clock-frequency",
670 I2C_SPEED_STANDARD_RATE);
Simon Glassc7a58902014-12-10 08:55:47 -0700671
Simon Glasse4e8ff22015-02-05 21:41:32 -0700672 return dm_i2c_set_bus_speed(dev, i2c->speed_hz);
Mugunthan V Nbbf1ccf2016-07-18 15:10:58 +0530673#else
674 return 0;
675#endif
Simon Glassc7a58902014-12-10 08:55:47 -0700676}
677
Simon Glass713c3f02015-01-25 08:27:13 -0700678static int i2c_child_post_bind(struct udevice *dev)
679{
Adam Ford1004fcd2018-08-20 20:24:34 -0500680#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass71fa5b42020-12-03 16:55:18 -0700681 struct dm_i2c_chip *plat = dev_get_parent_plat(dev);
Simon Glass713c3f02015-01-25 08:27:13 -0700682
Simon Glassf1d50f72020-12-19 10:40:13 -0700683 if (!dev_has_ofnode(dev))
Simon Glass713c3f02015-01-25 08:27:13 -0700684 return 0;
Simon Glassaad29ae2020-12-03 16:55:21 -0700685 return i2c_chip_of_to_plat(dev, plat);
Mugunthan V Nbbf1ccf2016-07-18 15:10:58 +0530686#else
687 return 0;
688#endif
Simon Glass713c3f02015-01-25 08:27:13 -0700689}
690
Michal Simekf7ab6202019-01-31 16:31:02 +0100691static int i2c_post_bind(struct udevice *dev)
692{
Michal Simekf7ab6202019-01-31 16:31:02 +0100693 int ret = 0;
694
Simon Glass4123ba02020-12-16 21:20:15 -0700695 debug("%s: %s, seq=%d\n", __func__, dev->name, dev_seq(dev));
Michal Simekf7ab6202019-01-31 16:31:02 +0100696
697#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
698 ret = dm_scan_fdt_dev(dev);
699#endif
700 return ret;
701}
702
Simon Glassc7a58902014-12-10 08:55:47 -0700703UCLASS_DRIVER(i2c) = {
704 .id = UCLASS_I2C,
705 .name = "i2c",
Simon Glass0ccb0972015-01-25 08:27:05 -0700706 .flags = DM_UC_FLAG_SEQ_ALIAS,
Michal Simekf7ab6202019-01-31 16:31:02 +0100707 .post_bind = i2c_post_bind,
Lukasz Majewskibc9aad62019-04-04 12:35:34 +0200708 .pre_probe = i2c_pre_probe,
Simon Glassc7a58902014-12-10 08:55:47 -0700709 .post_probe = i2c_post_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700710 .per_device_auto = sizeof(struct dm_i2c_bus),
Simon Glass71fa5b42020-12-03 16:55:18 -0700711 .per_child_plat_auto = sizeof(struct dm_i2c_chip),
Simon Glass713c3f02015-01-25 08:27:13 -0700712 .child_post_bind = i2c_child_post_bind,
Simon Glassc7a58902014-12-10 08:55:47 -0700713};
714
715UCLASS_DRIVER(i2c_generic) = {
716 .id = UCLASS_I2C_GENERIC,
717 .name = "i2c_generic",
718};
719
Simon Glass31cc5b42020-09-22 12:45:01 -0600720static const struct udevice_id generic_chip_i2c_ids[] = {
721 { .compatible = "i2c-chip", .data = I2C_DEVICE_GENERIC },
722#if CONFIG_IS_ENABLED(ACPIGEN)
723 { .compatible = "hid-over-i2c", .data = I2C_DEVICE_HID_OVER_I2C },
724#endif
725 { }
726};
727
Simon Glassc7a58902014-12-10 08:55:47 -0700728U_BOOT_DRIVER(i2c_generic_chip_drv) = {
729 .name = "i2c_generic_chip_drv",
730 .id = UCLASS_I2C_GENERIC,
Simon Glass31cc5b42020-09-22 12:45:01 -0600731 .of_match = generic_chip_i2c_ids,
732#if CONFIG_IS_ENABLED(ACPIGEN)
Simon Glassaad29ae2020-12-03 16:55:21 -0700733 .of_to_plat = acpi_i2c_of_to_plat,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700734 .priv_auto = sizeof(struct acpi_i2c_priv),
Simon Glass31cc5b42020-09-22 12:45:01 -0600735#endif
736 ACPI_OPS_PTR(&acpi_i2c_ops)
Simon Glassc7a58902014-12-10 08:55:47 -0700737};