blob: 5405067861eb9c66d20854e0a989ff04727ba8fe [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
Patrick Delaunayb953ec22021-04-27 11:02:19 +02006#define LOG_CATEGORY UCLASS_I2C
7
Simon Glassc6202d82014-12-10 08:55:47 -07008#include <common.h>
9#include <dm.h>
10#include <errno.h>
Simon Glassc6202d82014-12-10 08:55:47 -070011#include <i2c.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060012#include <log.h>
Simon Glassc6202d82014-12-10 08:55:47 -070013#include <malloc.h>
Simon Glassfd42f262020-09-22 12:45:01 -060014#include <acpi/acpi_device.h>
15#include <dm/acpi.h>
Simon Glassc6202d82014-12-10 08:55:47 -070016#include <dm/device-internal.h>
17#include <dm/lists.h>
Alexander Kochetkovaa541922018-03-27 17:52:27 +030018#include <dm/pinctrl.h>
Simon Glassbcee8d62019-12-06 21:41:35 -070019#if CONFIG_IS_ENABLED(DM_GPIO)
Alexander Kochetkovaa541922018-03-27 17:52:27 +030020#include <asm/gpio.h>
21#endif
Simon Glassc05ed002020-05-10 11:40:11 -060022#include <linux/delay.h>
Simon Glassfd42f262020-09-22 12:45:01 -060023#include "acpi_i2c.h"
Simon Glassc6202d82014-12-10 08:55:47 -070024
Simon Glassc6202d82014-12-10 08:55:47 -070025#define I2C_MAX_OFFSET_LEN 4
26
Alexander Kochetkovaa541922018-03-27 17:52:27 +030027enum {
28 PIN_SDA = 0,
29 PIN_SCL,
30 PIN_COUNT,
31};
32
Simon Glass7d7db222015-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 Glassc6202d82014-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
Heinrich Schuchardt185f8122022-01-19 18:05:50 +010056 * Return: 0 if OK, -EADDRNOTAVAIL if the offset length is 0. In that case the
Simon Glassc6202d82014-12-10 08:55:47 -070057 * 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 Beckett85968522019-10-28 17:44:57 +000062 int offset_len = chip->offset_len;
Simon Glassc6202d82014-12-10 08:55:47 -070063
64 msg->addr = chip->chip_addr;
Robert Beckett85968522019-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 Glassc6202d82014-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 Beckett85968522019-10-28 17:44:57 +000071 if (!offset_len)
Simon Glassc6202d82014-12-10 08:55:47 -070072 return -EADDRNOTAVAIL;
Robert Beckett85968522019-10-28 17:44:57 +000073 assert(offset_len <= I2C_MAX_OFFSET_LEN);
74
Simon Glassc6202d82014-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 Glasscaa4daa2020-12-03 16:55:18 -070084 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
Simon Glassc6202d82014-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 Beckett85968522019-10-28 17:44:57 +000096 ptr->addr = msg->addr;
Simon Glassc6202d82014-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 Glasscaa4daa2020-12-03 16:55:18 -0700113 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
Simon Glassc6202d82014-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 Glassf9a4c2d2015-01-12 18:02:07 -0700134int dm_i2c_read(struct udevice *dev, uint offset, uint8_t *buffer, int len)
Simon Glassc6202d82014-12-10 08:55:47 -0700135{
Simon Glasscaa4daa2020-12-03 16:55:18 -0700136 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
Simon Glassc6202d82014-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 Beckett85968522019-10-28 17:44:57 +0000152 ptr->addr = msg->addr;
Simon Glassc6202d82014-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 Glassf9a4c2d2015-01-12 18:02:07 -0700164int dm_i2c_write(struct udevice *dev, uint offset, const uint8_t *buffer,
165 int len)
Simon Glassc6202d82014-12-10 08:55:47 -0700166{
Simon Glasscaa4daa2020-12-03 16:55:18 -0700167 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
Simon Glassc6202d82014-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];
Rasmus Villemoes49f3a422022-07-07 15:12:17 +0200171 uint8_t _buf[I2C_MAX_OFFSET_LEN + 64];
172 uint8_t *buf = _buf;
173 int ret;
Simon Glassc6202d82014-12-10 08:55:47 -0700174
175 if (!ops->xfer)
176 return -ENOSYS;
177
178 if (chip->flags & DM_I2C_CHIP_WR_ADDRESS)
179 return i2c_write_bytewise(dev, offset, buffer, len);
180 /*
181 * The simple approach would be to send two messages here: one to
182 * set the offset and one to write the bytes. However some drivers
183 * will not be expecting this, and some chips won't like how the
184 * driver presents this on the I2C bus.
185 *
186 * The API does not support separate offset and data. We could extend
187 * it with a flag indicating that there is data in the next message
188 * that needs to be processed in the same transaction. We could
189 * instead add an additional buffer to each message. For now, handle
190 * this in the uclass since it isn't clear what the impact on drivers
191 * would be with this extra complication. Unfortunately this means
192 * copying the message.
193 *
194 * Use the stack for small messages, malloc() for larger ones. We
195 * need to allow space for the offset (up to 4 bytes) and the message
196 * itself.
197 */
Rasmus Villemoes49f3a422022-07-07 15:12:17 +0200198 if (len > sizeof(_buf) - I2C_MAX_OFFSET_LEN) {
Simon Glassc6202d82014-12-10 08:55:47 -0700199 buf = malloc(I2C_MAX_OFFSET_LEN + len);
200 if (!buf)
201 return -ENOMEM;
Simon Glassc6202d82014-12-10 08:55:47 -0700202 }
Rasmus Villemoes49f3a422022-07-07 15:12:17 +0200203
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 if (buf != _buf)
210 free(buf);
211 return ret;
Simon Glassc6202d82014-12-10 08:55:47 -0700212}
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
Sebastian Reichel2aefa6e2021-07-15 17:39:59 +0200244int dm_i2c_reg_clrset(struct udevice *dev, uint offset, u32 clr, u32 set)
245{
246 uint8_t val;
247 int ret;
248
249 ret = dm_i2c_read(dev, offset, &val, 1);
250 if (ret < 0)
251 return ret;
252
253 val &= ~clr;
254 val |= set;
255
256 return dm_i2c_write(dev, offset, &val, 1);
257}
258
Simon Glassc6202d82014-12-10 08:55:47 -0700259/**
260 * i2c_probe_chip() - probe for a chip on a bus
261 *
262 * @bus: Bus to probe
263 * @chip_addr: Chip address to probe
264 * @flags: Flags for the chip
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100265 * Return: 0 if found, -ENOSYS if the driver is invalid, -EREMOTEIO if the chip
Simon Glassc6202d82014-12-10 08:55:47 -0700266 * does not respond to probe
267 */
268static int i2c_probe_chip(struct udevice *bus, uint chip_addr,
269 enum dm_i2c_chip_flags chip_flags)
270{
271 struct dm_i2c_ops *ops = i2c_get_ops(bus);
272 struct i2c_msg msg[1];
273 int ret;
274
275 if (ops->probe_chip) {
276 ret = ops->probe_chip(bus, chip_addr, chip_flags);
Nikita Yushchenko6db539f2022-02-15 20:58:52 +0300277 if (ret != -ENOSYS)
Simon Glassc6202d82014-12-10 08:55:47 -0700278 return ret;
279 }
280
281 if (!ops->xfer)
282 return -ENOSYS;
283
284 /* Probe with a zero-length message */
285 msg->addr = chip_addr;
286 msg->flags = chip_flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
287 msg->len = 0;
288 msg->buf = NULL;
289
290 return ops->xfer(bus, msg, 1);
291}
292
Simon Glass25ab4b02015-01-25 08:26:55 -0700293static int i2c_bind_driver(struct udevice *bus, uint chip_addr, uint offset_len,
Simon Glassc6202d82014-12-10 08:55:47 -0700294 struct udevice **devp)
295{
Simon Glasse6f66ec2015-01-25 08:27:13 -0700296 struct dm_i2c_chip *chip;
Simon Glassc6202d82014-12-10 08:55:47 -0700297 char name[30], *str;
298 struct udevice *dev;
299 int ret;
300
301 snprintf(name, sizeof(name), "generic_%x", chip_addr);
302 str = strdup(name);
Simon Glass5c2d23b2015-02-18 14:10:28 -0700303 if (!str)
304 return -ENOMEM;
Simon Glassc6202d82014-12-10 08:55:47 -0700305 ret = device_bind_driver(bus, "i2c_generic_chip_drv", str, &dev);
306 debug("%s: device_bind_driver: ret=%d\n", __func__, ret);
307 if (ret)
308 goto err_bind;
309
310 /* Tell the device what we know about it */
Simon Glasscaa4daa2020-12-03 16:55:18 -0700311 chip = dev_get_parent_plat(dev);
Simon Glasse6f66ec2015-01-25 08:27:13 -0700312 chip->chip_addr = chip_addr;
313 chip->offset_len = offset_len;
314 ret = device_probe(dev);
315 debug("%s: device_probe: ret=%d\n", __func__, ret);
Simon Glassc6202d82014-12-10 08:55:47 -0700316 if (ret)
317 goto err_probe;
318
319 *devp = dev;
320 return 0;
321
322err_probe:
Simon Glasse6f66ec2015-01-25 08:27:13 -0700323 /*
324 * If the device failed to probe, unbind it. There is nothing there
325 * on the bus so we don't want to leave it lying around
326 */
Simon Glassc6202d82014-12-10 08:55:47 -0700327 device_unbind(dev);
328err_bind:
329 free(str);
330 return ret;
331}
332
Simon Glass25ab4b02015-01-25 08:26:55 -0700333int i2c_get_chip(struct udevice *bus, uint chip_addr, uint offset_len,
334 struct udevice **devp)
Simon Glassc6202d82014-12-10 08:55:47 -0700335{
336 struct udevice *dev;
337
338 debug("%s: Searching bus '%s' for address %02x: ", __func__,
339 bus->name, chip_addr);
340 for (device_find_first_child(bus, &dev); dev;
341 device_find_next_child(&dev)) {
Simon Glasscaa4daa2020-12-03 16:55:18 -0700342 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
Simon Glassc6202d82014-12-10 08:55:47 -0700343 int ret;
344
Robert Beckett85968522019-10-28 17:44:57 +0000345 if (chip->chip_addr == (chip_addr &
346 ~chip->chip_addr_offset_mask)) {
Simon Glassc6202d82014-12-10 08:55:47 -0700347 ret = device_probe(dev);
348 debug("found, ret=%d\n", ret);
349 if (ret)
350 return ret;
351 *devp = dev;
352 return 0;
353 }
354 }
355 debug("not found\n");
Simon Glass25ab4b02015-01-25 08:26:55 -0700356 return i2c_bind_driver(bus, chip_addr, offset_len, devp);
Simon Glassc6202d82014-12-10 08:55:47 -0700357}
358
Simon Glass25ab4b02015-01-25 08:26:55 -0700359int i2c_get_chip_for_busnum(int busnum, int chip_addr, uint offset_len,
360 struct udevice **devp)
Simon Glassc6202d82014-12-10 08:55:47 -0700361{
362 struct udevice *bus;
363 int ret;
364
365 ret = uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus);
366 if (ret) {
367 debug("Cannot find I2C bus %d\n", busnum);
368 return ret;
369 }
Jean-Jacques Hiblotf32a8002018-12-07 14:50:38 +0100370
371 /* detect the presence of the chip on the bus */
372 ret = i2c_probe_chip(bus, chip_addr, 0);
373 debug("%s: bus='%s', address %02x, ret=%d\n", __func__, bus->name,
374 chip_addr, ret);
375 if (ret) {
376 debug("Cannot detect I2C chip %02x on bus %d\n", chip_addr,
377 busnum);
378 return ret;
379 }
380
Simon Glass25ab4b02015-01-25 08:26:55 -0700381 ret = i2c_get_chip(bus, chip_addr, offset_len, devp);
Simon Glassc6202d82014-12-10 08:55:47 -0700382 if (ret) {
383 debug("Cannot find I2C chip %02x on bus %d\n", chip_addr,
384 busnum);
385 return ret;
386 }
387
388 return 0;
389}
390
Philip Richard Oberfichtnerb4835522023-10-31 08:38:46 +0100391/* Find and probe I2C bus based on a chip attached to it */
392static int i2c_get_parent_bus(ofnode chip, struct udevice **devp)
393{
394 ofnode node;
395 struct udevice *dev;
396 int ret;
397
398 node = ofnode_get_parent(chip);
399 if (!ofnode_valid(node))
400 return -ENODEV;
401
402 ret = uclass_get_device_by_ofnode(UCLASS_I2C, node, &dev);
403 if (ret) {
404 *devp = NULL;
405 return ret;
406 }
407
408 *devp = dev;
409 return 0;
410}
411
412int i2c_get_chip_by_phandle(const struct udevice *parent, const char *prop_name,
413 struct udevice **devp)
414{
415 ofnode node;
416 uint phandle;
417 struct udevice *bus, *chip;
418 char *dev_name;
419 int ret;
420
421 debug("%s: Searching I2C chip for phandle \"%s\"\n",
422 __func__, prop_name);
423
424 dev_name = strdup(prop_name);
425 if (!dev_name) {
426 ret = -ENOMEM;
427 goto err_exit;
428 }
429
430 ret = dev_read_u32(parent, "i2cbcdev", &phandle);
431 if (ret)
432 goto err_exit;
433
434 node = ofnode_get_by_phandle(phandle);
435 if (!ofnode_valid(node)) {
436 ret = -ENODEV;
437 goto err_exit;
438 }
439
440 ret = i2c_get_parent_bus(node, &bus);
441 if (ret)
442 goto err_exit;
443
444 ret = device_bind_driver_to_node(bus, "i2c_generic_chip_drv",
445 dev_name, node, &chip);
446 if (ret)
447 goto err_exit;
448
449 ret = device_probe(chip);
450 if (ret) {
451 device_unbind(chip);
452 goto err_exit;
453 }
454
455 debug("%s succeeded\n", __func__);
456 *devp = chip;
457 return 0;
458
459err_exit:
460 free(dev_name);
461 debug("%s failed, ret = %d\n", __func__, ret);
462 *devp = NULL;
463 return ret;
464}
465
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700466int dm_i2c_probe(struct udevice *bus, uint chip_addr, uint chip_flags,
467 struct udevice **devp)
Simon Glassc6202d82014-12-10 08:55:47 -0700468{
469 int ret;
470
471 *devp = NULL;
472
473 /* First probe that chip */
474 ret = i2c_probe_chip(bus, chip_addr, chip_flags);
475 debug("%s: bus='%s', address %02x, ret=%d\n", __func__, bus->name,
476 chip_addr, ret);
477 if (ret)
478 return ret;
479
480 /* The chip was found, see if we have a driver, and probe it */
Simon Glass25ab4b02015-01-25 08:26:55 -0700481 ret = i2c_get_chip(bus, chip_addr, 1, devp);
Simon Glassc6202d82014-12-10 08:55:47 -0700482 debug("%s: i2c_get_chip: ret=%d\n", __func__, ret);
483
484 return ret;
485}
486
Simon Glassca88b9b2015-02-05 21:41:32 -0700487int dm_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
Simon Glassc6202d82014-12-10 08:55:47 -0700488{
489 struct dm_i2c_ops *ops = i2c_get_ops(bus);
Simon Glasse564f052015-03-05 12:25:20 -0700490 struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
Simon Glassc6202d82014-12-10 08:55:47 -0700491 int ret;
492
493 /*
494 * If we have a method, call it. If not then the driver probably wants
495 * to deal with speed changes on the next transfer. It can easily read
496 * the current speed from this uclass
497 */
498 if (ops->set_bus_speed) {
499 ret = ops->set_bus_speed(bus, speed);
500 if (ret)
501 return ret;
502 }
503 i2c->speed_hz = speed;
504
505 return 0;
506}
507
Simon Glassca88b9b2015-02-05 21:41:32 -0700508int dm_i2c_get_bus_speed(struct udevice *bus)
Simon Glassc6202d82014-12-10 08:55:47 -0700509{
510 struct dm_i2c_ops *ops = i2c_get_ops(bus);
Simon Glasse564f052015-03-05 12:25:20 -0700511 struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
Simon Glassc6202d82014-12-10 08:55:47 -0700512
513 if (!ops->get_bus_speed)
514 return i2c->speed_hz;
515
516 return ops->get_bus_speed(bus);
517}
518
519int i2c_set_chip_flags(struct udevice *dev, uint flags)
520{
521 struct udevice *bus = dev->parent;
Simon Glasscaa4daa2020-12-03 16:55:18 -0700522 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
Simon Glassc6202d82014-12-10 08:55:47 -0700523 struct dm_i2c_ops *ops = i2c_get_ops(bus);
524 int ret;
525
526 if (ops->set_flags) {
527 ret = ops->set_flags(dev, flags);
528 if (ret)
529 return ret;
530 }
531 chip->flags = flags;
532
533 return 0;
534}
535
536int i2c_get_chip_flags(struct udevice *dev, uint *flagsp)
537{
Simon Glasscaa4daa2020-12-03 16:55:18 -0700538 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
Simon Glassc6202d82014-12-10 08:55:47 -0700539
540 *flagsp = chip->flags;
541
542 return 0;
543}
544
545int i2c_set_chip_offset_len(struct udevice *dev, uint offset_len)
546{
Simon Glasscaa4daa2020-12-03 16:55:18 -0700547 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
Simon Glassc6202d82014-12-10 08:55:47 -0700548
549 if (offset_len > I2C_MAX_OFFSET_LEN)
Simon Glassc61c8ef2020-07-07 21:32:28 -0600550 return log_ret(-EINVAL);
Simon Glassc6202d82014-12-10 08:55:47 -0700551 chip->offset_len = offset_len;
552
553 return 0;
554}
555
Simon Glass01501802015-05-04 11:30:58 -0600556int i2c_get_chip_offset_len(struct udevice *dev)
557{
Simon Glasscaa4daa2020-12-03 16:55:18 -0700558 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
Simon Glass01501802015-05-04 11:30:58 -0600559
560 return chip->offset_len;
561}
562
Robert Beckett85968522019-10-28 17:44:57 +0000563int i2c_set_chip_addr_offset_mask(struct udevice *dev, uint mask)
564{
Simon Glasscaa4daa2020-12-03 16:55:18 -0700565 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
Robert Beckett85968522019-10-28 17:44:57 +0000566
567 chip->chip_addr_offset_mask = mask;
568
569 return 0;
570}
571
572uint i2c_get_chip_addr_offset_mask(struct udevice *dev)
573{
Simon Glasscaa4daa2020-12-03 16:55:18 -0700574 struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
Robert Beckett85968522019-10-28 17:44:57 +0000575
576 return chip->chip_addr_offset_mask;
577}
578
Simon Glassbcee8d62019-12-06 21:41:35 -0700579#if CONFIG_IS_ENABLED(DM_GPIO)
Alexander Kochetkovaa541922018-03-27 17:52:27 +0300580static void i2c_gpio_set_pin(struct gpio_desc *pin, int bit)
581{
582 if (bit)
583 dm_gpio_set_dir_flags(pin, GPIOD_IS_IN);
584 else
585 dm_gpio_set_dir_flags(pin, GPIOD_IS_OUT |
Alexander Kochetkovaa541922018-03-27 17:52:27 +0300586 GPIOD_IS_OUT_ACTIVE);
587}
588
589static int i2c_gpio_get_pin(struct gpio_desc *pin)
590{
Haibo Chen7f6a2e62023-03-27 19:21:43 +0800591 /* DTS need config GPIO_ACTIVE_LOW */
592 return !dm_gpio_get_value(pin);
Alexander Kochetkovaa541922018-03-27 17:52:27 +0300593}
594
Marek Vasut72315222020-02-07 16:57:50 +0100595int i2c_deblock_gpio_loop(struct gpio_desc *sda_pin,
596 struct gpio_desc *scl_pin,
597 unsigned int scl_count,
Marek Vasuta1917282020-02-07 16:57:51 +0100598 unsigned int start_count,
Marek Vasut72315222020-02-07 16:57:50 +0100599 unsigned int delay)
Alexander Kochetkovaa541922018-03-27 17:52:27 +0300600{
Marek Vasuta1917282020-02-07 16:57:51 +0100601 int i, ret = -EREMOTEIO;
Alexander Kochetkovaa541922018-03-27 17:52:27 +0300602
603 i2c_gpio_set_pin(sda_pin, 1);
604 i2c_gpio_set_pin(scl_pin, 1);
Marek Vasut1f746a22020-02-07 16:57:49 +0100605 udelay(delay);
Alexander Kochetkovaa541922018-03-27 17:52:27 +0300606
607 /* Toggle SCL until slave release SDA */
Heinrich Schuchardtda585c32020-05-09 18:20:18 +0200608 for (; scl_count; --scl_count) {
Alexander Kochetkovaa541922018-03-27 17:52:27 +0300609 i2c_gpio_set_pin(scl_pin, 1);
Marek Vasut1f746a22020-02-07 16:57:49 +0100610 udelay(delay);
Alexander Kochetkovaa541922018-03-27 17:52:27 +0300611 i2c_gpio_set_pin(scl_pin, 0);
Marek Vasut1f746a22020-02-07 16:57:49 +0100612 udelay(delay);
Marek Vasuta1917282020-02-07 16:57:51 +0100613 if (i2c_gpio_get_pin(sda_pin)) {
614 ret = 0;
Alexander Kochetkovaa541922018-03-27 17:52:27 +0300615 break;
Marek Vasuta1917282020-02-07 16:57:51 +0100616 }
617 }
618
619 if (!ret && start_count) {
620 for (i = 0; i < start_count; i++) {
621 /* Send start condition */
622 udelay(delay);
623 i2c_gpio_set_pin(sda_pin, 1);
624 udelay(delay);
625 i2c_gpio_set_pin(scl_pin, 1);
626 udelay(delay);
627 i2c_gpio_set_pin(sda_pin, 0);
628 udelay(delay);
629 i2c_gpio_set_pin(scl_pin, 0);
630 }
Alexander Kochetkovaa541922018-03-27 17:52:27 +0300631 }
632
633 /* Then, send I2C stop */
634 i2c_gpio_set_pin(sda_pin, 0);
Marek Vasut1f746a22020-02-07 16:57:49 +0100635 udelay(delay);
Alexander Kochetkovaa541922018-03-27 17:52:27 +0300636
637 i2c_gpio_set_pin(scl_pin, 1);
Marek Vasut1f746a22020-02-07 16:57:49 +0100638 udelay(delay);
Alexander Kochetkovaa541922018-03-27 17:52:27 +0300639
640 i2c_gpio_set_pin(sda_pin, 1);
Marek Vasut1f746a22020-02-07 16:57:49 +0100641 udelay(delay);
Alexander Kochetkovaa541922018-03-27 17:52:27 +0300642
643 if (!i2c_gpio_get_pin(sda_pin) || !i2c_gpio_get_pin(scl_pin))
644 ret = -EREMOTEIO;
645
646 return ret;
647}
648
649static int i2c_deblock_gpio(struct udevice *bus)
650{
651 struct gpio_desc gpios[PIN_COUNT];
652 int ret, ret0;
653
654 ret = gpio_request_list_by_name(bus, "gpios", gpios,
655 ARRAY_SIZE(gpios), GPIOD_IS_IN);
656 if (ret != ARRAY_SIZE(gpios)) {
657 debug("%s: I2C Node '%s' has no 'gpios' property %s\n",
658 __func__, dev_read_name(bus), bus->name);
659 if (ret >= 0) {
660 gpio_free_list(bus, gpios, ret);
661 ret = -ENOENT;
662 }
663 goto out;
664 }
665
666 ret = pinctrl_select_state(bus, "gpio");
667 if (ret) {
668 debug("%s: I2C Node '%s' has no 'gpio' pinctrl state. %s\n",
669 __func__, dev_read_name(bus), bus->name);
670 goto out_no_pinctrl;
671 }
672
Marek Vasuta1917282020-02-07 16:57:51 +0100673 ret0 = i2c_deblock_gpio_loop(&gpios[PIN_SDA], &gpios[PIN_SCL], 9, 0, 5);
Alexander Kochetkovaa541922018-03-27 17:52:27 +0300674
675 ret = pinctrl_select_state(bus, "default");
676 if (ret) {
677 debug("%s: I2C Node '%s' has no 'default' pinctrl state. %s\n",
678 __func__, dev_read_name(bus), bus->name);
679 }
680
681 ret = !ret ? ret0 : ret;
682
683out_no_pinctrl:
684 gpio_free_list(bus, gpios, ARRAY_SIZE(gpios));
685out:
686 return ret;
687}
688#else
689static int i2c_deblock_gpio(struct udevice *bus)
690{
691 return -ENOSYS;
692}
Simon Glassbcee8d62019-12-06 21:41:35 -0700693#endif /* DM_GPIO */
Alexander Kochetkovaa541922018-03-27 17:52:27 +0300694
Simon Glassc6202d82014-12-10 08:55:47 -0700695int i2c_deblock(struct udevice *bus)
696{
697 struct dm_i2c_ops *ops = i2c_get_ops(bus);
698
Simon Glassc6202d82014-12-10 08:55:47 -0700699 if (!ops->deblock)
Alexander Kochetkovaa541922018-03-27 17:52:27 +0300700 return i2c_deblock_gpio(bus);
Simon Glassc6202d82014-12-10 08:55:47 -0700701
702 return ops->deblock(bus);
703}
704
Simon Glass414cc152021-08-07 07:24:03 -0600705#if CONFIG_IS_ENABLED(OF_REAL)
Simon Glassd1998a92020-12-03 16:55:21 -0700706int i2c_chip_of_to_plat(struct udevice *dev, struct dm_i2c_chip *chip)
Simon Glassc6202d82014-12-10 08:55:47 -0700707{
Simon Glass17043082017-05-18 20:09:30 -0600708 int addr;
709
710 chip->offset_len = dev_read_u32_default(dev, "u-boot,i2c-offset-len",
711 1);
Simon Glassc6202d82014-12-10 08:55:47 -0700712 chip->flags = 0;
Simon Glass17043082017-05-18 20:09:30 -0600713 addr = dev_read_u32_default(dev, "reg", -1);
714 if (addr == -1) {
715 debug("%s: I2C Node '%s' has no 'reg' property %s\n", __func__,
716 dev_read_name(dev), dev->name);
Simon Glassc61c8ef2020-07-07 21:32:28 -0600717 return log_ret(-EINVAL);
Simon Glassc6202d82014-12-10 08:55:47 -0700718 }
Simon Glass17043082017-05-18 20:09:30 -0600719 chip->chip_addr = addr;
Simon Glassc6202d82014-12-10 08:55:47 -0700720
721 return 0;
722}
Mugunthan V N5142ac72016-07-18 15:10:58 +0530723#endif
Simon Glassc6202d82014-12-10 08:55:47 -0700724
Lukasz Majewskia40fe212019-04-04 12:35:34 +0200725static int i2c_pre_probe(struct udevice *dev)
726{
Simon Glass414cc152021-08-07 07:24:03 -0600727#if CONFIG_IS_ENABLED(OF_REAL)
Lukasz Majewskia40fe212019-04-04 12:35:34 +0200728 struct dm_i2c_bus *i2c = dev_get_uclass_priv(dev);
729 unsigned int max = 0;
730 ofnode node;
731 int ret;
732
733 i2c->max_transaction_bytes = 0;
734 dev_for_each_subnode(node, dev) {
735 ret = ofnode_read_u32(node,
736 "u-boot,i2c-transaction-bytes",
737 &max);
738 if (!ret && max > i2c->max_transaction_bytes)
739 i2c->max_transaction_bytes = max;
740 }
741
742 debug("%s: I2C bus: %s max transaction bytes: %d\n", __func__,
743 dev->name, i2c->max_transaction_bytes);
744#endif
745 return 0;
746}
747
Simon Glassc6202d82014-12-10 08:55:47 -0700748static int i2c_post_probe(struct udevice *dev)
749{
Simon Glass414cc152021-08-07 07:24:03 -0600750#if CONFIG_IS_ENABLED(OF_REAL)
Simon Glasse564f052015-03-05 12:25:20 -0700751 struct dm_i2c_bus *i2c = dev_get_uclass_priv(dev);
Simon Glassc6202d82014-12-10 08:55:47 -0700752
Simon Glassf3d46152020-01-23 11:48:22 -0700753 i2c->speed_hz = dev_read_u32_default(dev, "clock-frequency",
754 I2C_SPEED_STANDARD_RATE);
Simon Glassc6202d82014-12-10 08:55:47 -0700755
Simon Glassca88b9b2015-02-05 21:41:32 -0700756 return dm_i2c_set_bus_speed(dev, i2c->speed_hz);
Mugunthan V N5142ac72016-07-18 15:10:58 +0530757#else
758 return 0;
759#endif
Simon Glassc6202d82014-12-10 08:55:47 -0700760}
761
Simon Glasse6f66ec2015-01-25 08:27:13 -0700762static int i2c_child_post_bind(struct udevice *dev)
763{
Simon Glass414cc152021-08-07 07:24:03 -0600764#if CONFIG_IS_ENABLED(OF_REAL)
Simon Glasscaa4daa2020-12-03 16:55:18 -0700765 struct dm_i2c_chip *plat = dev_get_parent_plat(dev);
Simon Glasse6f66ec2015-01-25 08:27:13 -0700766
Simon Glass7d14ee42020-12-19 10:40:13 -0700767 if (!dev_has_ofnode(dev))
Simon Glasse6f66ec2015-01-25 08:27:13 -0700768 return 0;
Simon Glassd1998a92020-12-03 16:55:21 -0700769 return i2c_chip_of_to_plat(dev, plat);
Mugunthan V N5142ac72016-07-18 15:10:58 +0530770#else
771 return 0;
772#endif
Simon Glasse6f66ec2015-01-25 08:27:13 -0700773}
774
Michal Simek61607222019-01-31 16:31:02 +0100775static int i2c_post_bind(struct udevice *dev)
776{
Michal Simek61607222019-01-31 16:31:02 +0100777 int ret = 0;
778
Simon Glass16df9932020-12-16 21:20:15 -0700779 debug("%s: %s, seq=%d\n", __func__, dev->name, dev_seq(dev));
Michal Simek61607222019-01-31 16:31:02 +0100780
Simon Glass414cc152021-08-07 07:24:03 -0600781#if CONFIG_IS_ENABLED(OF_REAL)
Michal Simek61607222019-01-31 16:31:02 +0100782 ret = dm_scan_fdt_dev(dev);
783#endif
784 return ret;
785}
786
Simon Glassc6202d82014-12-10 08:55:47 -0700787UCLASS_DRIVER(i2c) = {
788 .id = UCLASS_I2C,
789 .name = "i2c",
Simon Glass9cc36a22015-01-25 08:27:05 -0700790 .flags = DM_UC_FLAG_SEQ_ALIAS,
Michal Simek61607222019-01-31 16:31:02 +0100791 .post_bind = i2c_post_bind,
Lukasz Majewskia40fe212019-04-04 12:35:34 +0200792 .pre_probe = i2c_pre_probe,
Simon Glassc6202d82014-12-10 08:55:47 -0700793 .post_probe = i2c_post_probe,
Simon Glass41575d82020-12-03 16:55:17 -0700794 .per_device_auto = sizeof(struct dm_i2c_bus),
Simon Glasscaa4daa2020-12-03 16:55:18 -0700795 .per_child_plat_auto = sizeof(struct dm_i2c_chip),
Simon Glasse6f66ec2015-01-25 08:27:13 -0700796 .child_post_bind = i2c_child_post_bind,
Simon Glassc6202d82014-12-10 08:55:47 -0700797};
798
799UCLASS_DRIVER(i2c_generic) = {
800 .id = UCLASS_I2C_GENERIC,
801 .name = "i2c_generic",
802};
803
Simon Glassfd42f262020-09-22 12:45:01 -0600804static const struct udevice_id generic_chip_i2c_ids[] = {
805 { .compatible = "i2c-chip", .data = I2C_DEVICE_GENERIC },
806#if CONFIG_IS_ENABLED(ACPIGEN)
807 { .compatible = "hid-over-i2c", .data = I2C_DEVICE_HID_OVER_I2C },
808#endif
809 { }
810};
811
Simon Glassc6202d82014-12-10 08:55:47 -0700812U_BOOT_DRIVER(i2c_generic_chip_drv) = {
813 .name = "i2c_generic_chip_drv",
814 .id = UCLASS_I2C_GENERIC,
Simon Glassfd42f262020-09-22 12:45:01 -0600815 .of_match = generic_chip_i2c_ids,
816#if CONFIG_IS_ENABLED(ACPIGEN)
Simon Glassd1998a92020-12-03 16:55:21 -0700817 .of_to_plat = acpi_i2c_of_to_plat,
Simon Glass41575d82020-12-03 16:55:17 -0700818 .priv_auto = sizeof(struct acpi_i2c_priv),
Simon Glassfd42f262020-09-22 12:45:01 -0600819#endif
820 ACPI_OPS_PTR(&acpi_i2c_ops)
Simon Glassc6202d82014-12-10 08:55:47 -0700821};