2 * Copyright (c) 2014 Google, Inc
4 * SPDX-License-Identifier: GPL-2.0+
12 #include <dm/device-internal.h>
15 #define I2C_MAX_OFFSET_LEN 4
17 /* Useful debugging function */
18 void i2c_dump_msgs(struct i2c_msg
*msg
, int nmsgs
)
22 for (i
= 0; i
< nmsgs
; i
++) {
23 struct i2c_msg
*m
= &msg
[i
];
25 printf(" %s %x len=%x", m
->flags
& I2C_M_RD
? "R" : "W",
27 if (!(m
->flags
& I2C_M_RD
))
28 printf(": %x", m
->buf
[0]);
34 * i2c_setup_offset() - Set up a new message with a chip offset
37 * @offset: Byte offset within chip
38 * @offset_buf: Place to put byte offset
39 * @msg: Message buffer
40 * @return 0 if OK, -EADDRNOTAVAIL if the offset length is 0. In that case the
41 * message is still set up but will not contain an offset.
43 static int i2c_setup_offset(struct dm_i2c_chip
*chip
, uint offset
,
44 uint8_t offset_buf
[], struct i2c_msg
*msg
)
48 msg
->addr
= chip
->chip_addr
;
49 msg
->flags
= chip
->flags
& DM_I2C_CHIP_10BIT
? I2C_M_TEN
: 0;
50 msg
->len
= chip
->offset_len
;
51 msg
->buf
= offset_buf
;
52 if (!chip
->offset_len
)
53 return -EADDRNOTAVAIL
;
54 assert(chip
->offset_len
<= I2C_MAX_OFFSET_LEN
);
55 offset_len
= chip
->offset_len
;
57 *offset_buf
++ = offset
>> (8 * offset_len
);
62 static int i2c_read_bytewise(struct udevice
*dev
, uint offset
,
63 uint8_t *buffer
, int len
)
65 struct dm_i2c_chip
*chip
= dev_get_parent_platdata(dev
);
66 struct udevice
*bus
= dev_get_parent(dev
);
67 struct dm_i2c_ops
*ops
= i2c_get_ops(bus
);
68 struct i2c_msg msg
[2], *ptr
;
69 uint8_t offset_buf
[I2C_MAX_OFFSET_LEN
];
73 for (i
= 0; i
< len
; i
++) {
74 if (i2c_setup_offset(chip
, offset
+ i
, offset_buf
, msg
))
77 ptr
->addr
= chip
->chip_addr
;
78 ptr
->flags
= msg
->flags
| I2C_M_RD
;
80 ptr
->buf
= &buffer
[i
];
83 ret
= ops
->xfer(bus
, msg
, ptr
- msg
);
91 static int i2c_write_bytewise(struct udevice
*dev
, uint offset
,
92 const uint8_t *buffer
, int len
)
94 struct dm_i2c_chip
*chip
= dev_get_parent_platdata(dev
);
95 struct udevice
*bus
= dev_get_parent(dev
);
96 struct dm_i2c_ops
*ops
= i2c_get_ops(bus
);
97 struct i2c_msg msg
[1];
98 uint8_t buf
[I2C_MAX_OFFSET_LEN
+ 1];
102 for (i
= 0; i
< len
; i
++) {
103 if (i2c_setup_offset(chip
, offset
+ i
, buf
, msg
))
105 buf
[msg
->len
++] = buffer
[i
];
107 ret
= ops
->xfer(bus
, msg
, 1);
115 int dm_i2c_read(struct udevice
*dev
, uint offset
, uint8_t *buffer
, int len
)
117 struct dm_i2c_chip
*chip
= dev_get_parent_platdata(dev
);
118 struct udevice
*bus
= dev_get_parent(dev
);
119 struct dm_i2c_ops
*ops
= i2c_get_ops(bus
);
120 struct i2c_msg msg
[2], *ptr
;
121 uint8_t offset_buf
[I2C_MAX_OFFSET_LEN
];
126 if (chip
->flags
& DM_I2C_CHIP_RD_ADDRESS
)
127 return i2c_read_bytewise(dev
, offset
, buffer
, len
);
129 if (!i2c_setup_offset(chip
, offset
, offset_buf
, ptr
))
133 ptr
->addr
= chip
->chip_addr
;
134 ptr
->flags
= chip
->flags
& DM_I2C_CHIP_10BIT
? I2C_M_TEN
: 0;
135 ptr
->flags
|= I2C_M_RD
;
140 msg_count
= ptr
- msg
;
142 return ops
->xfer(bus
, msg
, msg_count
);
145 int dm_i2c_write(struct udevice
*dev
, uint offset
, const uint8_t *buffer
,
148 struct dm_i2c_chip
*chip
= dev_get_parent_platdata(dev
);
149 struct udevice
*bus
= dev_get_parent(dev
);
150 struct dm_i2c_ops
*ops
= i2c_get_ops(bus
);
151 struct i2c_msg msg
[1];
156 if (chip
->flags
& DM_I2C_CHIP_WR_ADDRESS
)
157 return i2c_write_bytewise(dev
, offset
, buffer
, len
);
159 * The simple approach would be to send two messages here: one to
160 * set the offset and one to write the bytes. However some drivers
161 * will not be expecting this, and some chips won't like how the
162 * driver presents this on the I2C bus.
164 * The API does not support separate offset and data. We could extend
165 * it with a flag indicating that there is data in the next message
166 * that needs to be processed in the same transaction. We could
167 * instead add an additional buffer to each message. For now, handle
168 * this in the uclass since it isn't clear what the impact on drivers
169 * would be with this extra complication. Unfortunately this means
170 * copying the message.
172 * Use the stack for small messages, malloc() for larger ones. We
173 * need to allow space for the offset (up to 4 bytes) and the message
177 uint8_t buf
[I2C_MAX_OFFSET_LEN
+ len
];
179 i2c_setup_offset(chip
, offset
, buf
, msg
);
181 memcpy(buf
+ chip
->offset_len
, buffer
, len
);
183 return ops
->xfer(bus
, msg
, 1);
188 buf
= malloc(I2C_MAX_OFFSET_LEN
+ len
);
191 i2c_setup_offset(chip
, offset
, buf
, msg
);
193 memcpy(buf
+ chip
->offset_len
, buffer
, len
);
195 ret
= ops
->xfer(bus
, msg
, 1);
201 int dm_i2c_xfer(struct udevice
*dev
, struct i2c_msg
*msg
, int nmsgs
)
203 struct udevice
*bus
= dev_get_parent(dev
);
204 struct dm_i2c_ops
*ops
= i2c_get_ops(bus
);
209 return ops
->xfer(bus
, msg
, nmsgs
);
212 int dm_i2c_reg_read(struct udevice
*dev
, uint offset
)
217 ret
= dm_i2c_read(dev
, offset
, &val
, 1);
224 int dm_i2c_reg_write(struct udevice
*dev
, uint offset
, uint value
)
228 return dm_i2c_write(dev
, offset
, &val
, 1);
232 * i2c_probe_chip() - probe for a chip on a bus
235 * @chip_addr: Chip address to probe
236 * @flags: Flags for the chip
237 * @return 0 if found, -ENOSYS if the driver is invalid, -EREMOTEIO if the chip
238 * does not respond to probe
240 static int i2c_probe_chip(struct udevice
*bus
, uint chip_addr
,
241 enum dm_i2c_chip_flags chip_flags
)
243 struct dm_i2c_ops
*ops
= i2c_get_ops(bus
);
244 struct i2c_msg msg
[1];
247 if (ops
->probe_chip
) {
248 ret
= ops
->probe_chip(bus
, chip_addr
, chip_flags
);
249 if (!ret
|| ret
!= -ENOSYS
)
256 /* Probe with a zero-length message */
257 msg
->addr
= chip_addr
;
258 msg
->flags
= chip_flags
& DM_I2C_CHIP_10BIT
? I2C_M_TEN
: 0;
262 return ops
->xfer(bus
, msg
, 1);
265 static int i2c_bind_driver(struct udevice
*bus
, uint chip_addr
, uint offset_len
,
266 struct udevice
**devp
)
268 struct dm_i2c_chip
*chip
;
273 snprintf(name
, sizeof(name
), "generic_%x", chip_addr
);
277 ret
= device_bind_driver(bus
, "i2c_generic_chip_drv", str
, &dev
);
278 debug("%s: device_bind_driver: ret=%d\n", __func__
, ret
);
282 /* Tell the device what we know about it */
283 chip
= dev_get_parent_platdata(dev
);
284 chip
->chip_addr
= chip_addr
;
285 chip
->offset_len
= offset_len
;
286 ret
= device_probe(dev
);
287 debug("%s: device_probe: ret=%d\n", __func__
, ret
);
296 * If the device failed to probe, unbind it. There is nothing there
297 * on the bus so we don't want to leave it lying around
305 int i2c_get_chip(struct udevice
*bus
, uint chip_addr
, uint offset_len
,
306 struct udevice
**devp
)
310 debug("%s: Searching bus '%s' for address %02x: ", __func__
,
311 bus
->name
, chip_addr
);
312 for (device_find_first_child(bus
, &dev
); dev
;
313 device_find_next_child(&dev
)) {
314 struct dm_i2c_chip
*chip
= dev_get_parent_platdata(dev
);
317 if (chip
->chip_addr
== chip_addr
) {
318 ret
= device_probe(dev
);
319 debug("found, ret=%d\n", ret
);
326 debug("not found\n");
327 return i2c_bind_driver(bus
, chip_addr
, offset_len
, devp
);
330 int i2c_get_chip_for_busnum(int busnum
, int chip_addr
, uint offset_len
,
331 struct udevice
**devp
)
336 ret
= uclass_get_device_by_seq(UCLASS_I2C
, busnum
, &bus
);
338 debug("Cannot find I2C bus %d\n", busnum
);
341 ret
= i2c_get_chip(bus
, chip_addr
, offset_len
, devp
);
343 debug("Cannot find I2C chip %02x on bus %d\n", chip_addr
,
351 int dm_i2c_probe(struct udevice
*bus
, uint chip_addr
, uint chip_flags
,
352 struct udevice
**devp
)
358 /* First probe that chip */
359 ret
= i2c_probe_chip(bus
, chip_addr
, chip_flags
);
360 debug("%s: bus='%s', address %02x, ret=%d\n", __func__
, bus
->name
,
365 /* The chip was found, see if we have a driver, and probe it */
366 ret
= i2c_get_chip(bus
, chip_addr
, 1, devp
);
367 debug("%s: i2c_get_chip: ret=%d\n", __func__
, ret
);
372 int dm_i2c_set_bus_speed(struct udevice
*bus
, unsigned int speed
)
374 struct dm_i2c_ops
*ops
= i2c_get_ops(bus
);
375 struct dm_i2c_bus
*i2c
= dev_get_uclass_priv(bus
);
379 * If we have a method, call it. If not then the driver probably wants
380 * to deal with speed changes on the next transfer. It can easily read
381 * the current speed from this uclass
383 if (ops
->set_bus_speed
) {
384 ret
= ops
->set_bus_speed(bus
, speed
);
388 i2c
->speed_hz
= speed
;
393 int dm_i2c_get_bus_speed(struct udevice
*bus
)
395 struct dm_i2c_ops
*ops
= i2c_get_ops(bus
);
396 struct dm_i2c_bus
*i2c
= dev_get_uclass_priv(bus
);
398 if (!ops
->get_bus_speed
)
399 return i2c
->speed_hz
;
401 return ops
->get_bus_speed(bus
);
404 int i2c_set_chip_flags(struct udevice
*dev
, uint flags
)
406 struct udevice
*bus
= dev
->parent
;
407 struct dm_i2c_chip
*chip
= dev_get_parent_platdata(dev
);
408 struct dm_i2c_ops
*ops
= i2c_get_ops(bus
);
411 if (ops
->set_flags
) {
412 ret
= ops
->set_flags(dev
, flags
);
421 int i2c_get_chip_flags(struct udevice
*dev
, uint
*flagsp
)
423 struct dm_i2c_chip
*chip
= dev_get_parent_platdata(dev
);
425 *flagsp
= chip
->flags
;
430 int i2c_set_chip_offset_len(struct udevice
*dev
, uint offset_len
)
432 struct dm_i2c_chip
*chip
= dev_get_parent_platdata(dev
);
434 if (offset_len
> I2C_MAX_OFFSET_LEN
)
436 chip
->offset_len
= offset_len
;
441 int i2c_get_chip_offset_len(struct udevice
*dev
)
443 struct dm_i2c_chip
*chip
= dev_get_parent_platdata(dev
);
445 return chip
->offset_len
;
448 int i2c_deblock(struct udevice
*bus
)
450 struct dm_i2c_ops
*ops
= i2c_get_ops(bus
);
453 * We could implement a software deblocking here if we could get
454 * access to the GPIOs used by I2C, and switch them to GPIO mode
455 * and then back to I2C. This is somewhat beyond our powers in
456 * driver model at present, so for now just fail.
458 * See https://patchwork.ozlabs.org/patch/399040/
463 return ops
->deblock(bus
);
466 #if CONFIG_IS_ENABLED(OF_CONTROL)
467 int i2c_chip_ofdata_to_platdata(struct udevice
*dev
, struct dm_i2c_chip
*chip
)
471 chip
->offset_len
= dev_read_u32_default(dev
, "u-boot,i2c-offset-len",
474 addr
= dev_read_u32_default(dev
, "reg", -1);
476 debug("%s: I2C Node '%s' has no 'reg' property %s\n", __func__
,
477 dev_read_name(dev
), dev
->name
);
480 chip
->chip_addr
= addr
;
486 static int i2c_post_probe(struct udevice
*dev
)
488 #if CONFIG_IS_ENABLED(OF_CONTROL)
489 struct dm_i2c_bus
*i2c
= dev_get_uclass_priv(dev
);
491 i2c
->speed_hz
= dev_read_u32_default(dev
, "clock-frequency", 100000);
493 return dm_i2c_set_bus_speed(dev
, i2c
->speed_hz
);
499 static int i2c_child_post_bind(struct udevice
*dev
)
501 #if CONFIG_IS_ENABLED(OF_CONTROL)
502 struct dm_i2c_chip
*plat
= dev_get_parent_platdata(dev
);
504 if (!dev_of_valid(dev
))
506 return i2c_chip_ofdata_to_platdata(dev
, plat
);
512 UCLASS_DRIVER(i2c
) = {
515 .flags
= DM_UC_FLAG_SEQ_ALIAS
,
516 #if CONFIG_IS_ENABLED(OF_CONTROL)
517 .post_bind
= dm_scan_fdt_dev
,
519 .post_probe
= i2c_post_probe
,
520 .per_device_auto_alloc_size
= sizeof(struct dm_i2c_bus
),
521 .per_child_platdata_auto_alloc_size
= sizeof(struct dm_i2c_chip
),
522 .child_post_bind
= i2c_child_post_bind
,
525 UCLASS_DRIVER(i2c_generic
) = {
526 .id
= UCLASS_I2C_GENERIC
,
527 .name
= "i2c_generic",
530 U_BOOT_DRIVER(i2c_generic_chip_drv
) = {
531 .name
= "i2c_generic_chip_drv",
532 .id
= UCLASS_I2C_GENERIC
,