blob: 380a9f8f3addc6a4d86ccab35d46b52dc7247f65 [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 <dm.h>
9#include <errno.h>
Simon Glassc7a58902014-12-10 08:55:47 -070010#include <i2c.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Simon Glassc7a58902014-12-10 08:55:47 -070012#include <malloc.h>
Simon Glass31cc5b42020-09-22 12:45:01 -060013#include <acpi/acpi_device.h>
14#include <dm/acpi.h>
Simon Glassc7a58902014-12-10 08:55:47 -070015#include <dm/device-internal.h>
16#include <dm/lists.h>
Alexander Kochetkovdc96eb72018-03-27 17:52:27 +030017#include <dm/pinctrl.h>
Simon Glassfa4689a2019-12-06 21:41:35 -070018#if CONFIG_IS_ENABLED(DM_GPIO)
Alexander Kochetkovdc96eb72018-03-27 17:52:27 +030019#include <asm/gpio.h>
20#endif
Simon Glassdbd79542020-05-10 11:40:11 -060021#include <linux/delay.h>
Simon Glass31cc5b42020-09-22 12:45:01 -060022#include "acpi_i2c.h"
Simon Glassc7a58902014-12-10 08:55:47 -070023
Simon Glassc7a58902014-12-10 08:55:47 -070024#define I2C_MAX_OFFSET_LEN 4
25
Alexander Kochetkovdc96eb72018-03-27 17:52:27 +030026enum {
27 PIN_SDA = 0,
28 PIN_SCL,
29 PIN_COUNT,
30};
31
Simon Glassa8e64c42015-07-02 18:15:39 -060032/* Useful debugging function */
33void i2c_dump_msgs(struct i2c_msg *msg, int nmsgs)
34{
35 int i;
36
37 for (i = 0; i < nmsgs; i++) {
38 struct i2c_msg *m = &msg[i];
39
40 printf(" %s %x len=%x", m->flags & I2C_M_RD ? "R" : "W",
41 msg->addr, msg->len);
42 if (!(m->flags & I2C_M_RD))
43 printf(": %x", m->buf[0]);
44 printf("\n");
45 }
46}
47
Simon Glassc7a58902014-12-10 08:55:47 -070048/**
49 * i2c_setup_offset() - Set up a new message with a chip offset
50 *
51 * @chip: Chip to use
52 * @offset: Byte offset within chip
53 * @offset_buf: Place to put byte offset
54 * @msg: Message buffer
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010055 * Return: 0 if OK, -EADDRNOTAVAIL if the offset length is 0. In that case the
Simon Glassc7a58902014-12-10 08:55:47 -070056 * message is still set up but will not contain an offset.
57 */
58static int i2c_setup_offset(struct dm_i2c_chip *chip, uint offset,
59 uint8_t offset_buf[], struct i2c_msg *msg)
60{
Robert Beckett5f8e41a2019-10-28 17:44:57 +000061 int offset_len = chip->offset_len;
Simon Glassc7a58902014-12-10 08:55:47 -070062
63 msg->addr = chip->chip_addr;
Robert Beckett5f8e41a2019-10-28 17:44:57 +000064 if (chip->chip_addr_offset_mask)
65 msg->addr |= (offset >> (8 * offset_len)) &
66 chip->chip_addr_offset_mask;
Simon Glassc7a58902014-12-10 08:55:47 -070067 msg->flags = chip->flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
68 msg->len = chip->offset_len;
69 msg->buf = offset_buf;
Robert Beckett5f8e41a2019-10-28 17:44:57 +000070 if (!offset_len)
Simon Glassc7a58902014-12-10 08:55:47 -070071 return -EADDRNOTAVAIL;
Robert Beckett5f8e41a2019-10-28 17:44:57 +000072 assert(offset_len <= I2C_MAX_OFFSET_LEN);
73
Simon Glassc7a58902014-12-10 08:55:47 -070074 while (offset_len--)
75 *offset_buf++ = offset >> (8 * offset_len);
76
77 return 0;
78}
79
80static int i2c_read_bytewise(struct udevice *dev, uint offset,
81 uint8_t *buffer, int len)
82{
Simon Glass71fa5b42020-12-03 16:55:18 -070083 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
Simon Glassc7a58902014-12-10 08:55:47 -070084 struct udevice *bus = dev_get_parent(dev);
85 struct dm_i2c_ops *ops = i2c_get_ops(bus);
86 struct i2c_msg msg[2], *ptr;
87 uint8_t offset_buf[I2C_MAX_OFFSET_LEN];
88 int ret;
89 int i;
90
91 for (i = 0; i < len; i++) {
92 if (i2c_setup_offset(chip, offset + i, offset_buf, msg))
93 return -EINVAL;
94 ptr = msg + 1;
Robert Beckett5f8e41a2019-10-28 17:44:57 +000095 ptr->addr = msg->addr;
Simon Glassc7a58902014-12-10 08:55:47 -070096 ptr->flags = msg->flags | I2C_M_RD;
97 ptr->len = 1;
98 ptr->buf = &buffer[i];
99 ptr++;
100
101 ret = ops->xfer(bus, msg, ptr - msg);
102 if (ret)
103 return ret;
104 }
105
106 return 0;
107}
108
109static int i2c_write_bytewise(struct udevice *dev, uint offset,
110 const uint8_t *buffer, int len)
111{
Simon Glass71fa5b42020-12-03 16:55:18 -0700112 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
Simon Glassc7a58902014-12-10 08:55:47 -0700113 struct udevice *bus = dev_get_parent(dev);
114 struct dm_i2c_ops *ops = i2c_get_ops(bus);
115 struct i2c_msg msg[1];
116 uint8_t buf[I2C_MAX_OFFSET_LEN + 1];
117 int ret;
118 int i;
119
120 for (i = 0; i < len; i++) {
121 if (i2c_setup_offset(chip, offset + i, buf, msg))
122 return -EINVAL;
123 buf[msg->len++] = buffer[i];
124
125 ret = ops->xfer(bus, msg, 1);
126 if (ret)
127 return ret;
128 }
129
130 return 0;
131}
132
Simon Glass7d722762015-01-12 18:02:07 -0700133int dm_i2c_read(struct udevice *dev, uint offset, uint8_t *buffer, int len)
Simon Glassc7a58902014-12-10 08:55:47 -0700134{
Simon Glass71fa5b42020-12-03 16:55:18 -0700135 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
Simon Glassc7a58902014-12-10 08:55:47 -0700136 struct udevice *bus = dev_get_parent(dev);
137 struct dm_i2c_ops *ops = i2c_get_ops(bus);
138 struct i2c_msg msg[2], *ptr;
139 uint8_t offset_buf[I2C_MAX_OFFSET_LEN];
140 int msg_count;
141
142 if (!ops->xfer)
143 return -ENOSYS;
144 if (chip->flags & DM_I2C_CHIP_RD_ADDRESS)
145 return i2c_read_bytewise(dev, offset, buffer, len);
146 ptr = msg;
147 if (!i2c_setup_offset(chip, offset, offset_buf, ptr))
148 ptr++;
149
150 if (len) {
Robert Beckett5f8e41a2019-10-28 17:44:57 +0000151 ptr->addr = msg->addr;
Simon Glassc7a58902014-12-10 08:55:47 -0700152 ptr->flags = chip->flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
153 ptr->flags |= I2C_M_RD;
154 ptr->len = len;
155 ptr->buf = buffer;
156 ptr++;
157 }
158 msg_count = ptr - msg;
159
160 return ops->xfer(bus, msg, msg_count);
161}
162
Simon Glass7d722762015-01-12 18:02:07 -0700163int dm_i2c_write(struct udevice *dev, uint offset, const uint8_t *buffer,
164 int len)
Simon Glassc7a58902014-12-10 08:55:47 -0700165{
Simon Glass71fa5b42020-12-03 16:55:18 -0700166 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
Simon Glassc7a58902014-12-10 08:55:47 -0700167 struct udevice *bus = dev_get_parent(dev);
168 struct dm_i2c_ops *ops = i2c_get_ops(bus);
169 struct i2c_msg msg[1];
Rasmus Villemoes92371712022-07-07 15:12:17 +0200170 uint8_t _buf[I2C_MAX_OFFSET_LEN + 64];
171 uint8_t *buf = _buf;
172 int ret;
Simon Glassc7a58902014-12-10 08:55:47 -0700173
174 if (!ops->xfer)
175 return -ENOSYS;
176
177 if (chip->flags & DM_I2C_CHIP_WR_ADDRESS)
178 return i2c_write_bytewise(dev, offset, buffer, len);
179 /*
180 * The simple approach would be to send two messages here: one to
181 * set the offset and one to write the bytes. However some drivers
182 * will not be expecting this, and some chips won't like how the
183 * driver presents this on the I2C bus.
184 *
185 * The API does not support separate offset and data. We could extend
186 * it with a flag indicating that there is data in the next message
187 * that needs to be processed in the same transaction. We could
188 * instead add an additional buffer to each message. For now, handle
189 * this in the uclass since it isn't clear what the impact on drivers
190 * would be with this extra complication. Unfortunately this means
191 * copying the message.
192 *
193 * Use the stack for small messages, malloc() for larger ones. We
194 * need to allow space for the offset (up to 4 bytes) and the message
195 * itself.
196 */
Rasmus Villemoes92371712022-07-07 15:12:17 +0200197 if (len > sizeof(_buf) - I2C_MAX_OFFSET_LEN) {
Simon Glassc7a58902014-12-10 08:55:47 -0700198 buf = malloc(I2C_MAX_OFFSET_LEN + len);
199 if (!buf)
200 return -ENOMEM;
Rasmus Villemoes92371712022-07-07 15:12:17 +0200201 }
202
203 i2c_setup_offset(chip, offset, buf, msg);
204 msg->len += len;
205 memcpy(buf + chip->offset_len, buffer, len);
Simon Glassc7a58902014-12-10 08:55:47 -0700206
Rasmus Villemoes92371712022-07-07 15:12:17 +0200207 ret = ops->xfer(bus, msg, 1);
208 if (buf != _buf)
Simon Glassc7a58902014-12-10 08:55:47 -0700209 free(buf);
Rasmus Villemoes92371712022-07-07 15:12:17 +0200210 return ret;
Simon Glassc7a58902014-12-10 08:55:47 -0700211}
212
Simon Glassf2818852015-07-02 18:15:42 -0600213int dm_i2c_xfer(struct udevice *dev, struct i2c_msg *msg, int nmsgs)
214{
215 struct udevice *bus = dev_get_parent(dev);
216 struct dm_i2c_ops *ops = i2c_get_ops(bus);
217
218 if (!ops->xfer)
219 return -ENOSYS;
220
221 return ops->xfer(bus, msg, nmsgs);
222}
223
Simon Glass0c702612015-04-20 12:37:14 -0600224int dm_i2c_reg_read(struct udevice *dev, uint offset)
225{
226 uint8_t val;
227 int ret;
228
229 ret = dm_i2c_read(dev, offset, &val, 1);
230 if (ret < 0)
231 return ret;
232
233 return val;
234}
235
236int dm_i2c_reg_write(struct udevice *dev, uint offset, uint value)
237{
238 uint8_t val = value;
239
240 return dm_i2c_write(dev, offset, &val, 1);
241}
242
Sebastian Reichel8e841fb2021-07-15 17:39:59 +0200243int dm_i2c_reg_clrset(struct udevice *dev, uint offset, u32 clr, u32 set)
244{
245 uint8_t val;
246 int ret;
247
248 ret = dm_i2c_read(dev, offset, &val, 1);
249 if (ret < 0)
250 return ret;
251
252 val &= ~clr;
253 val |= set;
254
255 return dm_i2c_write(dev, offset, &val, 1);
256}
257
Simon Glassc7a58902014-12-10 08:55:47 -0700258/**
259 * i2c_probe_chip() - probe for a chip on a bus
260 *
261 * @bus: Bus to probe
262 * @chip_addr: Chip address to probe
263 * @flags: Flags for the chip
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100264 * Return: 0 if found, -ENOSYS if the driver is invalid, -EREMOTEIO if the chip
Simon Glassc7a58902014-12-10 08:55:47 -0700265 * does not respond to probe
266 */
267static int i2c_probe_chip(struct udevice *bus, uint chip_addr,
268 enum dm_i2c_chip_flags chip_flags)
269{
270 struct dm_i2c_ops *ops = i2c_get_ops(bus);
271 struct i2c_msg msg[1];
272 int ret;
273
274 if (ops->probe_chip) {
275 ret = ops->probe_chip(bus, chip_addr, chip_flags);
Nikita Yushchenko38fdfc72022-02-15 20:58:52 +0300276 if (ret != -ENOSYS)
Simon Glassc7a58902014-12-10 08:55:47 -0700277 return ret;
278 }
279
280 if (!ops->xfer)
281 return -ENOSYS;
282
283 /* Probe with a zero-length message */
284 msg->addr = chip_addr;
285 msg->flags = chip_flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
286 msg->len = 0;
287 msg->buf = NULL;
288
289 return ops->xfer(bus, msg, 1);
290}
291
Simon Glassa2723ae2015-01-25 08:26:55 -0700292static int i2c_bind_driver(struct udevice *bus, uint chip_addr, uint offset_len,
Simon Glassc7a58902014-12-10 08:55:47 -0700293 struct udevice **devp)
294{
Simon Glass713c3f02015-01-25 08:27:13 -0700295 struct dm_i2c_chip *chip;
Simon Glassc7a58902014-12-10 08:55:47 -0700296 char name[30], *str;
297 struct udevice *dev;
298 int ret;
299
300 snprintf(name, sizeof(name), "generic_%x", chip_addr);
301 str = strdup(name);
Simon Glassb75c4492015-02-18 14:10:28 -0700302 if (!str)
303 return -ENOMEM;
Simon Glassc7a58902014-12-10 08:55:47 -0700304 ret = device_bind_driver(bus, "i2c_generic_chip_drv", str, &dev);
305 debug("%s: device_bind_driver: ret=%d\n", __func__, ret);
306 if (ret)
307 goto err_bind;
308
309 /* Tell the device what we know about it */
Simon Glass71fa5b42020-12-03 16:55:18 -0700310 chip = dev_get_parent_plat(dev);
Simon Glass713c3f02015-01-25 08:27:13 -0700311 chip->chip_addr = chip_addr;
312 chip->offset_len = offset_len;
313 ret = device_probe(dev);
314 debug("%s: device_probe: ret=%d\n", __func__, ret);
Simon Glassc7a58902014-12-10 08:55:47 -0700315 if (ret)
316 goto err_probe;
317
318 *devp = dev;
319 return 0;
320
321err_probe:
Simon Glass713c3f02015-01-25 08:27:13 -0700322 /*
323 * If the device failed to probe, unbind it. There is nothing there
324 * on the bus so we don't want to leave it lying around
325 */
Simon Glassc7a58902014-12-10 08:55:47 -0700326 device_unbind(dev);
327err_bind:
328 free(str);
329 return ret;
330}
331
Simon Glassa2723ae2015-01-25 08:26:55 -0700332int i2c_get_chip(struct udevice *bus, uint chip_addr, uint offset_len,
333 struct udevice **devp)
Simon Glassc7a58902014-12-10 08:55:47 -0700334{
335 struct udevice *dev;
336
337 debug("%s: Searching bus '%s' for address %02x: ", __func__,
338 bus->name, chip_addr);
339 for (device_find_first_child(bus, &dev); dev;
340 device_find_next_child(&dev)) {
Simon Glass71fa5b42020-12-03 16:55:18 -0700341 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
Simon Glassc7a58902014-12-10 08:55:47 -0700342 int ret;
343
Robert Beckett5f8e41a2019-10-28 17:44:57 +0000344 if (chip->chip_addr == (chip_addr &
345 ~chip->chip_addr_offset_mask)) {
Simon Glassc7a58902014-12-10 08:55:47 -0700346 ret = device_probe(dev);
347 debug("found, ret=%d\n", ret);
348 if (ret)
349 return ret;
350 *devp = dev;
351 return 0;
352 }
353 }
354 debug("not found\n");
Simon Glassa2723ae2015-01-25 08:26:55 -0700355 return i2c_bind_driver(bus, chip_addr, offset_len, devp);
Simon Glassc7a58902014-12-10 08:55:47 -0700356}
357
Simon Glassa2723ae2015-01-25 08:26:55 -0700358int i2c_get_chip_for_busnum(int busnum, int chip_addr, uint offset_len,
359 struct udevice **devp)
Simon Glassc7a58902014-12-10 08:55:47 -0700360{
361 struct udevice *bus;
362 int ret;
363
364 ret = uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus);
365 if (ret) {
366 debug("Cannot find I2C bus %d\n", busnum);
367 return ret;
368 }
Jean-Jacques Hiblotac9ab242018-12-07 14:50:38 +0100369
370 /* detect the presence of the chip on the bus */
371 ret = i2c_probe_chip(bus, chip_addr, 0);
372 debug("%s: bus='%s', address %02x, ret=%d\n", __func__, bus->name,
373 chip_addr, ret);
374 if (ret) {
375 debug("Cannot detect I2C chip %02x on bus %d\n", chip_addr,
376 busnum);
377 return ret;
378 }
379
Simon Glassa2723ae2015-01-25 08:26:55 -0700380 ret = i2c_get_chip(bus, chip_addr, offset_len, devp);
Simon Glassc7a58902014-12-10 08:55:47 -0700381 if (ret) {
382 debug("Cannot find I2C chip %02x on bus %d\n", chip_addr,
383 busnum);
384 return ret;
385 }
386
387 return 0;
388}
389
Philip Richard Oberfichtnere63a8402023-10-31 08:38:46 +0100390/* Find and probe I2C bus based on a chip attached to it */
391static int i2c_get_parent_bus(ofnode chip, struct udevice **devp)
392{
393 ofnode node;
394 struct udevice *dev;
395 int ret;
396
397 node = ofnode_get_parent(chip);
398 if (!ofnode_valid(node))
399 return -ENODEV;
400
401 ret = uclass_get_device_by_ofnode(UCLASS_I2C, node, &dev);
402 if (ret) {
403 *devp = NULL;
404 return ret;
405 }
406
407 *devp = dev;
408 return 0;
409}
410
411int i2c_get_chip_by_phandle(const struct udevice *parent, const char *prop_name,
412 struct udevice **devp)
413{
414 ofnode node;
415 uint phandle;
416 struct udevice *bus, *chip;
417 char *dev_name;
418 int ret;
419
420 debug("%s: Searching I2C chip for phandle \"%s\"\n",
421 __func__, prop_name);
422
423 dev_name = strdup(prop_name);
424 if (!dev_name) {
425 ret = -ENOMEM;
426 goto err_exit;
427 }
428
Philip Oberfichtner79dd0ef2023-11-24 15:04:01 +0100429 ret = dev_read_u32(parent, prop_name, &phandle);
Philip Richard Oberfichtnere63a8402023-10-31 08:38:46 +0100430 if (ret)
431 goto err_exit;
432
433 node = ofnode_get_by_phandle(phandle);
434 if (!ofnode_valid(node)) {
435 ret = -ENODEV;
436 goto err_exit;
437 }
438
439 ret = i2c_get_parent_bus(node, &bus);
440 if (ret)
441 goto err_exit;
442
443 ret = device_bind_driver_to_node(bus, "i2c_generic_chip_drv",
444 dev_name, node, &chip);
445 if (ret)
446 goto err_exit;
447
448 ret = device_probe(chip);
449 if (ret) {
450 device_unbind(chip);
451 goto err_exit;
452 }
453
454 debug("%s succeeded\n", __func__);
455 *devp = chip;
456 return 0;
457
458err_exit:
459 free(dev_name);
460 debug("%s failed, ret = %d\n", __func__, ret);
461 *devp = NULL;
462 return ret;
463}
464
Simon Glass7d722762015-01-12 18:02:07 -0700465int dm_i2c_probe(struct udevice *bus, uint chip_addr, uint chip_flags,
466 struct udevice **devp)
Simon Glassc7a58902014-12-10 08:55:47 -0700467{
468 int ret;
469
470 *devp = NULL;
471
472 /* First probe that chip */
473 ret = i2c_probe_chip(bus, chip_addr, chip_flags);
474 debug("%s: bus='%s', address %02x, ret=%d\n", __func__, bus->name,
475 chip_addr, ret);
476 if (ret)
477 return ret;
478
479 /* The chip was found, see if we have a driver, and probe it */
Simon Glassa2723ae2015-01-25 08:26:55 -0700480 ret = i2c_get_chip(bus, chip_addr, 1, devp);
Simon Glassc7a58902014-12-10 08:55:47 -0700481 debug("%s: i2c_get_chip: ret=%d\n", __func__, ret);
482
483 return ret;
484}
485
Simon Glasse4e8ff22015-02-05 21:41:32 -0700486int dm_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
Simon Glassc7a58902014-12-10 08:55:47 -0700487{
488 struct dm_i2c_ops *ops = i2c_get_ops(bus);
Simon Glassde0977b2015-03-05 12:25:20 -0700489 struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
Simon Glassc7a58902014-12-10 08:55:47 -0700490 int ret;
491
492 /*
493 * If we have a method, call it. If not then the driver probably wants
494 * to deal with speed changes on the next transfer. It can easily read
495 * the current speed from this uclass
496 */
497 if (ops->set_bus_speed) {
498 ret = ops->set_bus_speed(bus, speed);
499 if (ret)
500 return ret;
501 }
502 i2c->speed_hz = speed;
503
504 return 0;
505}
506
Simon Glasse4e8ff22015-02-05 21:41:32 -0700507int dm_i2c_get_bus_speed(struct udevice *bus)
Simon Glassc7a58902014-12-10 08:55:47 -0700508{
509 struct dm_i2c_ops *ops = i2c_get_ops(bus);
Simon Glassde0977b2015-03-05 12:25:20 -0700510 struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
Simon Glassc7a58902014-12-10 08:55:47 -0700511
512 if (!ops->get_bus_speed)
513 return i2c->speed_hz;
514
515 return ops->get_bus_speed(bus);
516}
517
518int i2c_set_chip_flags(struct udevice *dev, uint flags)
519{
520 struct udevice *bus = dev->parent;
Simon Glass71fa5b42020-12-03 16:55:18 -0700521 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
Simon Glassc7a58902014-12-10 08:55:47 -0700522 struct dm_i2c_ops *ops = i2c_get_ops(bus);
523 int ret;
524
525 if (ops->set_flags) {
526 ret = ops->set_flags(dev, flags);
527 if (ret)
528 return ret;
529 }
530 chip->flags = flags;
531
532 return 0;
533}
534
535int i2c_get_chip_flags(struct udevice *dev, uint *flagsp)
536{
Simon Glass71fa5b42020-12-03 16:55:18 -0700537 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
Simon Glassc7a58902014-12-10 08:55:47 -0700538
539 *flagsp = chip->flags;
540
541 return 0;
542}
543
544int i2c_set_chip_offset_len(struct udevice *dev, uint offset_len)
545{
Simon Glass71fa5b42020-12-03 16:55:18 -0700546 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
Simon Glassc7a58902014-12-10 08:55:47 -0700547
548 if (offset_len > I2C_MAX_OFFSET_LEN)
Simon Glasse9983052020-07-07 21:32:28 -0600549 return log_ret(-EINVAL);
Simon Glassc7a58902014-12-10 08:55:47 -0700550 chip->offset_len = offset_len;
551
552 return 0;
553}
554
Simon Glass9a1589a2015-05-04 11:30:58 -0600555int i2c_get_chip_offset_len(struct udevice *dev)
556{
Simon Glass71fa5b42020-12-03 16:55:18 -0700557 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
Simon Glass9a1589a2015-05-04 11:30:58 -0600558
559 return chip->offset_len;
560}
561
Robert Beckett5f8e41a2019-10-28 17:44:57 +0000562int i2c_set_chip_addr_offset_mask(struct udevice *dev, uint mask)
563{
Simon Glass71fa5b42020-12-03 16:55:18 -0700564 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
Robert Beckett5f8e41a2019-10-28 17:44:57 +0000565
566 chip->chip_addr_offset_mask = mask;
567
568 return 0;
569}
570
571uint i2c_get_chip_addr_offset_mask(struct udevice *dev)
572{
Simon Glass71fa5b42020-12-03 16:55:18 -0700573 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
Robert Beckett5f8e41a2019-10-28 17:44:57 +0000574
575 return chip->chip_addr_offset_mask;
576}
577
Simon Glassfa4689a2019-12-06 21:41:35 -0700578#if CONFIG_IS_ENABLED(DM_GPIO)
Alexander Kochetkovdc96eb72018-03-27 17:52:27 +0300579static void i2c_gpio_set_pin(struct gpio_desc *pin, int bit)
580{
581 if (bit)
582 dm_gpio_set_dir_flags(pin, GPIOD_IS_IN);
583 else
584 dm_gpio_set_dir_flags(pin, GPIOD_IS_OUT |
Alexander Kochetkovdc96eb72018-03-27 17:52:27 +0300585 GPIOD_IS_OUT_ACTIVE);
586}
587
588static int i2c_gpio_get_pin(struct gpio_desc *pin)
589{
Haibo Chen56105aa2023-03-27 19:21:43 +0800590 /* DTS need config GPIO_ACTIVE_LOW */
591 return !dm_gpio_get_value(pin);
Alexander Kochetkovdc96eb72018-03-27 17:52:27 +0300592}
593
Marek Vasute2af3412020-02-07 16:57:50 +0100594int i2c_deblock_gpio_loop(struct gpio_desc *sda_pin,
595 struct gpio_desc *scl_pin,
596 unsigned int scl_count,
Marek Vasut39de9252020-02-07 16:57:51 +0100597 unsigned int start_count,
Marek Vasute2af3412020-02-07 16:57:50 +0100598 unsigned int delay)
Alexander Kochetkovdc96eb72018-03-27 17:52:27 +0300599{
Marek Vasut39de9252020-02-07 16:57:51 +0100600 int i, ret = -EREMOTEIO;
Alexander Kochetkovdc96eb72018-03-27 17:52:27 +0300601
602 i2c_gpio_set_pin(sda_pin, 1);
603 i2c_gpio_set_pin(scl_pin, 1);
Marek Vasut2ffffc32020-02-07 16:57:49 +0100604 udelay(delay);
Alexander Kochetkovdc96eb72018-03-27 17:52:27 +0300605
606 /* Toggle SCL until slave release SDA */
Heinrich Schuchardtf1524a22020-05-09 18:20:18 +0200607 for (; scl_count; --scl_count) {
Alexander Kochetkovdc96eb72018-03-27 17:52:27 +0300608 i2c_gpio_set_pin(scl_pin, 1);
Marek Vasut2ffffc32020-02-07 16:57:49 +0100609 udelay(delay);
Alexander Kochetkovdc96eb72018-03-27 17:52:27 +0300610 i2c_gpio_set_pin(scl_pin, 0);
Marek Vasut2ffffc32020-02-07 16:57:49 +0100611 udelay(delay);
Marek Vasut39de9252020-02-07 16:57:51 +0100612 if (i2c_gpio_get_pin(sda_pin)) {
613 ret = 0;
Alexander Kochetkovdc96eb72018-03-27 17:52:27 +0300614 break;
Marek Vasut39de9252020-02-07 16:57:51 +0100615 }
616 }
617
618 if (!ret && start_count) {
619 for (i = 0; i < start_count; i++) {
620 /* Send start condition */
621 udelay(delay);
622 i2c_gpio_set_pin(sda_pin, 1);
623 udelay(delay);
624 i2c_gpio_set_pin(scl_pin, 1);
625 udelay(delay);
626 i2c_gpio_set_pin(sda_pin, 0);
627 udelay(delay);
628 i2c_gpio_set_pin(scl_pin, 0);
629 }
Alexander Kochetkovdc96eb72018-03-27 17:52:27 +0300630 }
631
632 /* Then, send I2C stop */
633 i2c_gpio_set_pin(sda_pin, 0);
Marek Vasut2ffffc32020-02-07 16:57:49 +0100634 udelay(delay);
Alexander Kochetkovdc96eb72018-03-27 17:52:27 +0300635
636 i2c_gpio_set_pin(scl_pin, 1);
Marek Vasut2ffffc32020-02-07 16:57:49 +0100637 udelay(delay);
Alexander Kochetkovdc96eb72018-03-27 17:52:27 +0300638
639 i2c_gpio_set_pin(sda_pin, 1);
Marek Vasut2ffffc32020-02-07 16:57:49 +0100640 udelay(delay);
Alexander Kochetkovdc96eb72018-03-27 17:52:27 +0300641
642 if (!i2c_gpio_get_pin(sda_pin) || !i2c_gpio_get_pin(scl_pin))
643 ret = -EREMOTEIO;
644
645 return ret;
646}
647
648static int i2c_deblock_gpio(struct udevice *bus)
649{
650 struct gpio_desc gpios[PIN_COUNT];
651 int ret, ret0;
652
653 ret = gpio_request_list_by_name(bus, "gpios", gpios,
654 ARRAY_SIZE(gpios), GPIOD_IS_IN);
655 if (ret != ARRAY_SIZE(gpios)) {
656 debug("%s: I2C Node '%s' has no 'gpios' property %s\n",
657 __func__, dev_read_name(bus), bus->name);
658 if (ret >= 0) {
659 gpio_free_list(bus, gpios, ret);
660 ret = -ENOENT;
661 }
662 goto out;
663 }
664
665 ret = pinctrl_select_state(bus, "gpio");
666 if (ret) {
667 debug("%s: I2C Node '%s' has no 'gpio' pinctrl state. %s\n",
668 __func__, dev_read_name(bus), bus->name);
669 goto out_no_pinctrl;
670 }
671
Marek Vasut39de9252020-02-07 16:57:51 +0100672 ret0 = i2c_deblock_gpio_loop(&gpios[PIN_SDA], &gpios[PIN_SCL], 9, 0, 5);
Alexander Kochetkovdc96eb72018-03-27 17:52:27 +0300673
674 ret = pinctrl_select_state(bus, "default");
675 if (ret) {
676 debug("%s: I2C Node '%s' has no 'default' pinctrl state. %s\n",
677 __func__, dev_read_name(bus), bus->name);
678 }
679
680 ret = !ret ? ret0 : ret;
681
682out_no_pinctrl:
683 gpio_free_list(bus, gpios, ARRAY_SIZE(gpios));
684out:
685 return ret;
686}
687#else
688static int i2c_deblock_gpio(struct udevice *bus)
689{
690 return -ENOSYS;
691}
Simon Glassfa4689a2019-12-06 21:41:35 -0700692#endif /* DM_GPIO */
Alexander Kochetkovdc96eb72018-03-27 17:52:27 +0300693
Simon Glassc7a58902014-12-10 08:55:47 -0700694int i2c_deblock(struct udevice *bus)
695{
696 struct dm_i2c_ops *ops = i2c_get_ops(bus);
697
Simon Glassc7a58902014-12-10 08:55:47 -0700698 if (!ops->deblock)
Alexander Kochetkovdc96eb72018-03-27 17:52:27 +0300699 return i2c_deblock_gpio(bus);
Simon Glassc7a58902014-12-10 08:55:47 -0700700
701 return ops->deblock(bus);
702}
703
Simon Glass3580f6d2021-08-07 07:24:03 -0600704#if CONFIG_IS_ENABLED(OF_REAL)
Simon Glassaad29ae2020-12-03 16:55:21 -0700705int i2c_chip_of_to_plat(struct udevice *dev, struct dm_i2c_chip *chip)
Simon Glassc7a58902014-12-10 08:55:47 -0700706{
Simon Glass654e3a02017-05-18 20:09:30 -0600707 int addr;
708
709 chip->offset_len = dev_read_u32_default(dev, "u-boot,i2c-offset-len",
710 1);
Simon Glassc7a58902014-12-10 08:55:47 -0700711 chip->flags = 0;
Simon Glass654e3a02017-05-18 20:09:30 -0600712 addr = dev_read_u32_default(dev, "reg", -1);
713 if (addr == -1) {
714 debug("%s: I2C Node '%s' has no 'reg' property %s\n", __func__,
715 dev_read_name(dev), dev->name);
Simon Glasse9983052020-07-07 21:32:28 -0600716 return log_ret(-EINVAL);
Simon Glassc7a58902014-12-10 08:55:47 -0700717 }
Simon Glass654e3a02017-05-18 20:09:30 -0600718 chip->chip_addr = addr;
Simon Glassc7a58902014-12-10 08:55:47 -0700719
720 return 0;
721}
Mugunthan V Nbbf1ccf2016-07-18 15:10:58 +0530722#endif
Simon Glassc7a58902014-12-10 08:55:47 -0700723
Lukasz Majewskibc9aad62019-04-04 12:35:34 +0200724static int i2c_pre_probe(struct udevice *dev)
725{
Simon Glass3580f6d2021-08-07 07:24:03 -0600726#if CONFIG_IS_ENABLED(OF_REAL)
Lukasz Majewskibc9aad62019-04-04 12:35:34 +0200727 struct dm_i2c_bus *i2c = dev_get_uclass_priv(dev);
728 unsigned int max = 0;
729 ofnode node;
730 int ret;
731
732 i2c->max_transaction_bytes = 0;
733 dev_for_each_subnode(node, dev) {
734 ret = ofnode_read_u32(node,
735 "u-boot,i2c-transaction-bytes",
736 &max);
737 if (!ret && max > i2c->max_transaction_bytes)
738 i2c->max_transaction_bytes = max;
739 }
740
741 debug("%s: I2C bus: %s max transaction bytes: %d\n", __func__,
742 dev->name, i2c->max_transaction_bytes);
743#endif
744 return 0;
745}
746
Simon Glassc7a58902014-12-10 08:55:47 -0700747static int i2c_post_probe(struct udevice *dev)
748{
Simon Glass3580f6d2021-08-07 07:24:03 -0600749#if CONFIG_IS_ENABLED(OF_REAL)
Simon Glassde0977b2015-03-05 12:25:20 -0700750 struct dm_i2c_bus *i2c = dev_get_uclass_priv(dev);
Simon Glassc7a58902014-12-10 08:55:47 -0700751
Simon Glassf0c99c52020-01-23 11:48:22 -0700752 i2c->speed_hz = dev_read_u32_default(dev, "clock-frequency",
753 I2C_SPEED_STANDARD_RATE);
Simon Glassc7a58902014-12-10 08:55:47 -0700754
Simon Glasse4e8ff22015-02-05 21:41:32 -0700755 return dm_i2c_set_bus_speed(dev, i2c->speed_hz);
Mugunthan V Nbbf1ccf2016-07-18 15:10:58 +0530756#else
757 return 0;
758#endif
Simon Glassc7a58902014-12-10 08:55:47 -0700759}
760
Simon Glass713c3f02015-01-25 08:27:13 -0700761static int i2c_child_post_bind(struct udevice *dev)
762{
Simon Glass3580f6d2021-08-07 07:24:03 -0600763#if CONFIG_IS_ENABLED(OF_REAL)
Simon Glass71fa5b42020-12-03 16:55:18 -0700764 struct dm_i2c_chip *plat = dev_get_parent_plat(dev);
Simon Glass713c3f02015-01-25 08:27:13 -0700765
Simon Glassf1d50f72020-12-19 10:40:13 -0700766 if (!dev_has_ofnode(dev))
Simon Glass713c3f02015-01-25 08:27:13 -0700767 return 0;
Simon Glassaad29ae2020-12-03 16:55:21 -0700768 return i2c_chip_of_to_plat(dev, plat);
Mugunthan V Nbbf1ccf2016-07-18 15:10:58 +0530769#else
770 return 0;
771#endif
Simon Glass713c3f02015-01-25 08:27:13 -0700772}
773
Michal Simekf7ab6202019-01-31 16:31:02 +0100774static int i2c_post_bind(struct udevice *dev)
775{
Michal Simekf7ab6202019-01-31 16:31:02 +0100776 int ret = 0;
777
Simon Glass4123ba02020-12-16 21:20:15 -0700778 debug("%s: %s, seq=%d\n", __func__, dev->name, dev_seq(dev));
Michal Simekf7ab6202019-01-31 16:31:02 +0100779
Simon Glass3580f6d2021-08-07 07:24:03 -0600780#if CONFIG_IS_ENABLED(OF_REAL)
Michal Simekf7ab6202019-01-31 16:31:02 +0100781 ret = dm_scan_fdt_dev(dev);
782#endif
783 return ret;
784}
785
Simon Glassc7a58902014-12-10 08:55:47 -0700786UCLASS_DRIVER(i2c) = {
787 .id = UCLASS_I2C,
788 .name = "i2c",
Simon Glass0ccb0972015-01-25 08:27:05 -0700789 .flags = DM_UC_FLAG_SEQ_ALIAS,
Michal Simekf7ab6202019-01-31 16:31:02 +0100790 .post_bind = i2c_post_bind,
Lukasz Majewskibc9aad62019-04-04 12:35:34 +0200791 .pre_probe = i2c_pre_probe,
Simon Glassc7a58902014-12-10 08:55:47 -0700792 .post_probe = i2c_post_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700793 .per_device_auto = sizeof(struct dm_i2c_bus),
Simon Glass71fa5b42020-12-03 16:55:18 -0700794 .per_child_plat_auto = sizeof(struct dm_i2c_chip),
Simon Glass713c3f02015-01-25 08:27:13 -0700795 .child_post_bind = i2c_child_post_bind,
Simon Glassc7a58902014-12-10 08:55:47 -0700796};
797
798UCLASS_DRIVER(i2c_generic) = {
799 .id = UCLASS_I2C_GENERIC,
800 .name = "i2c_generic",
801};
802
Simon Glass31cc5b42020-09-22 12:45:01 -0600803static const struct udevice_id generic_chip_i2c_ids[] = {
804 { .compatible = "i2c-chip", .data = I2C_DEVICE_GENERIC },
805#if CONFIG_IS_ENABLED(ACPIGEN)
806 { .compatible = "hid-over-i2c", .data = I2C_DEVICE_HID_OVER_I2C },
807#endif
808 { }
809};
810
Simon Glassc7a58902014-12-10 08:55:47 -0700811U_BOOT_DRIVER(i2c_generic_chip_drv) = {
812 .name = "i2c_generic_chip_drv",
813 .id = UCLASS_I2C_GENERIC,
Simon Glass31cc5b42020-09-22 12:45:01 -0600814 .of_match = generic_chip_i2c_ids,
815#if CONFIG_IS_ENABLED(ACPIGEN)
Simon Glassaad29ae2020-12-03 16:55:21 -0700816 .of_to_plat = acpi_i2c_of_to_plat,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700817 .priv_auto = sizeof(struct acpi_i2c_priv),
Simon Glass31cc5b42020-09-22 12:45:01 -0600818#endif
819 ACPI_OPS_PTR(&acpi_i2c_ops)
Simon Glassc7a58902014-12-10 08:55:47 -0700820};