2 * Copyright (c) 2014 Google, Inc
4 * SPDX-License-Identifier: GPL-2.0+
13 #include <dm/device-internal.h>
17 DECLARE_GLOBAL_DATA_PTR
;
19 #define I2C_MAX_OFFSET_LEN 4
22 * i2c_setup_offset() - Set up a new message with a chip offset
25 * @offset: Byte offset within chip
26 * @offset_buf: Place to put byte offset
27 * @msg: Message buffer
28 * @return 0 if OK, -EADDRNOTAVAIL if the offset length is 0. In that case the
29 * message is still set up but will not contain an offset.
31 static int i2c_setup_offset(struct dm_i2c_chip
*chip
, uint offset
,
32 uint8_t offset_buf
[], struct i2c_msg
*msg
)
36 msg
->addr
= chip
->chip_addr
;
37 msg
->flags
= chip
->flags
& DM_I2C_CHIP_10BIT
? I2C_M_TEN
: 0;
38 msg
->len
= chip
->offset_len
;
39 msg
->buf
= offset_buf
;
40 if (!chip
->offset_len
)
41 return -EADDRNOTAVAIL
;
42 assert(chip
->offset_len
<= I2C_MAX_OFFSET_LEN
);
43 offset_len
= chip
->offset_len
;
45 *offset_buf
++ = offset
>> (8 * offset_len
);
50 static int i2c_read_bytewise(struct udevice
*dev
, uint offset
,
51 uint8_t *buffer
, int len
)
53 struct dm_i2c_chip
*chip
= dev_get_parent_platdata(dev
);
54 struct udevice
*bus
= dev_get_parent(dev
);
55 struct dm_i2c_ops
*ops
= i2c_get_ops(bus
);
56 struct i2c_msg msg
[2], *ptr
;
57 uint8_t offset_buf
[I2C_MAX_OFFSET_LEN
];
61 for (i
= 0; i
< len
; i
++) {
62 if (i2c_setup_offset(chip
, offset
+ i
, offset_buf
, msg
))
65 ptr
->addr
= chip
->chip_addr
;
66 ptr
->flags
= msg
->flags
| I2C_M_RD
;
68 ptr
->buf
= &buffer
[i
];
71 ret
= ops
->xfer(bus
, msg
, ptr
- msg
);
79 static int i2c_write_bytewise(struct udevice
*dev
, uint offset
,
80 const uint8_t *buffer
, int len
)
82 struct dm_i2c_chip
*chip
= dev_get_parent_platdata(dev
);
83 struct udevice
*bus
= dev_get_parent(dev
);
84 struct dm_i2c_ops
*ops
= i2c_get_ops(bus
);
85 struct i2c_msg msg
[1];
86 uint8_t buf
[I2C_MAX_OFFSET_LEN
+ 1];
90 for (i
= 0; i
< len
; i
++) {
91 if (i2c_setup_offset(chip
, offset
+ i
, buf
, msg
))
93 buf
[msg
->len
++] = buffer
[i
];
95 ret
= ops
->xfer(bus
, msg
, 1);
103 int dm_i2c_read(struct udevice
*dev
, uint offset
, uint8_t *buffer
, int len
)
105 struct dm_i2c_chip
*chip
= dev_get_parent_platdata(dev
);
106 struct udevice
*bus
= dev_get_parent(dev
);
107 struct dm_i2c_ops
*ops
= i2c_get_ops(bus
);
108 struct i2c_msg msg
[2], *ptr
;
109 uint8_t offset_buf
[I2C_MAX_OFFSET_LEN
];
114 if (chip
->flags
& DM_I2C_CHIP_RD_ADDRESS
)
115 return i2c_read_bytewise(dev
, offset
, buffer
, len
);
117 if (!i2c_setup_offset(chip
, offset
, offset_buf
, ptr
))
121 ptr
->addr
= chip
->chip_addr
;
122 ptr
->flags
= chip
->flags
& DM_I2C_CHIP_10BIT
? I2C_M_TEN
: 0;
123 ptr
->flags
|= I2C_M_RD
;
128 msg_count
= ptr
- msg
;
130 return ops
->xfer(bus
, msg
, msg_count
);
133 int dm_i2c_write(struct udevice
*dev
, uint offset
, const uint8_t *buffer
,
136 struct dm_i2c_chip
*chip
= dev_get_parent_platdata(dev
);
137 struct udevice
*bus
= dev_get_parent(dev
);
138 struct dm_i2c_ops
*ops
= i2c_get_ops(bus
);
139 struct i2c_msg msg
[1];
144 if (chip
->flags
& DM_I2C_CHIP_WR_ADDRESS
)
145 return i2c_write_bytewise(dev
, offset
, buffer
, len
);
147 * The simple approach would be to send two messages here: one to
148 * set the offset and one to write the bytes. However some drivers
149 * will not be expecting this, and some chips won't like how the
150 * driver presents this on the I2C bus.
152 * The API does not support separate offset and data. We could extend
153 * it with a flag indicating that there is data in the next message
154 * that needs to be processed in the same transaction. We could
155 * instead add an additional buffer to each message. For now, handle
156 * this in the uclass since it isn't clear what the impact on drivers
157 * would be with this extra complication. Unfortunately this means
158 * copying the message.
160 * Use the stack for small messages, malloc() for larger ones. We
161 * need to allow space for the offset (up to 4 bytes) and the message
165 uint8_t buf
[I2C_MAX_OFFSET_LEN
+ len
];
167 i2c_setup_offset(chip
, offset
, buf
, msg
);
169 memcpy(buf
+ chip
->offset_len
, buffer
, len
);
171 return ops
->xfer(bus
, msg
, 1);
176 buf
= malloc(I2C_MAX_OFFSET_LEN
+ len
);
179 i2c_setup_offset(chip
, offset
, buf
, msg
);
181 memcpy(buf
+ chip
->offset_len
, buffer
, len
);
183 ret
= ops
->xfer(bus
, msg
, 1);
190 * i2c_probe_chip() - probe for a chip on a bus
193 * @chip_addr: Chip address to probe
194 * @flags: Flags for the chip
195 * @return 0 if found, -ENOSYS if the driver is invalid, -EREMOTEIO if the chip
196 * does not respond to probe
198 static int i2c_probe_chip(struct udevice
*bus
, uint chip_addr
,
199 enum dm_i2c_chip_flags chip_flags
)
201 struct dm_i2c_ops
*ops
= i2c_get_ops(bus
);
202 struct i2c_msg msg
[1];
205 if (ops
->probe_chip
) {
206 ret
= ops
->probe_chip(bus
, chip_addr
, chip_flags
);
207 if (!ret
|| ret
!= -ENOSYS
)
214 /* Probe with a zero-length message */
215 msg
->addr
= chip_addr
;
216 msg
->flags
= chip_flags
& DM_I2C_CHIP_10BIT
? I2C_M_TEN
: 0;
220 return ops
->xfer(bus
, msg
, 1);
223 static int i2c_bind_driver(struct udevice
*bus
, uint chip_addr
, uint offset_len
,
224 struct udevice
**devp
)
226 struct dm_i2c_chip
*chip
;
231 snprintf(name
, sizeof(name
), "generic_%x", chip_addr
);
235 ret
= device_bind_driver(bus
, "i2c_generic_chip_drv", str
, &dev
);
236 debug("%s: device_bind_driver: ret=%d\n", __func__
, ret
);
240 /* Tell the device what we know about it */
241 chip
= dev_get_parent_platdata(dev
);
242 chip
->chip_addr
= chip_addr
;
243 chip
->offset_len
= offset_len
;
244 ret
= device_probe(dev
);
245 debug("%s: device_probe: ret=%d\n", __func__
, ret
);
254 * If the device failed to probe, unbind it. There is nothing there
255 * on the bus so we don't want to leave it lying around
263 int i2c_get_chip(struct udevice
*bus
, uint chip_addr
, uint offset_len
,
264 struct udevice
**devp
)
268 debug("%s: Searching bus '%s' for address %02x: ", __func__
,
269 bus
->name
, chip_addr
);
270 for (device_find_first_child(bus
, &dev
); dev
;
271 device_find_next_child(&dev
)) {
272 struct dm_i2c_chip
*chip
= dev_get_parent_platdata(dev
);
275 if (chip
->chip_addr
== chip_addr
) {
276 ret
= device_probe(dev
);
277 debug("found, ret=%d\n", ret
);
284 debug("not found\n");
285 return i2c_bind_driver(bus
, chip_addr
, offset_len
, devp
);
288 int i2c_get_chip_for_busnum(int busnum
, int chip_addr
, uint offset_len
,
289 struct udevice
**devp
)
294 ret
= uclass_get_device_by_seq(UCLASS_I2C
, busnum
, &bus
);
296 debug("Cannot find I2C bus %d\n", busnum
);
299 ret
= i2c_get_chip(bus
, chip_addr
, offset_len
, devp
);
301 debug("Cannot find I2C chip %02x on bus %d\n", chip_addr
,
309 int dm_i2c_probe(struct udevice
*bus
, uint chip_addr
, uint chip_flags
,
310 struct udevice
**devp
)
316 /* First probe that chip */
317 ret
= i2c_probe_chip(bus
, chip_addr
, chip_flags
);
318 debug("%s: bus='%s', address %02x, ret=%d\n", __func__
, bus
->name
,
323 /* The chip was found, see if we have a driver, and probe it */
324 ret
= i2c_get_chip(bus
, chip_addr
, 1, devp
);
325 debug("%s: i2c_get_chip: ret=%d\n", __func__
, ret
);
330 int dm_i2c_set_bus_speed(struct udevice
*bus
, unsigned int speed
)
332 struct dm_i2c_ops
*ops
= i2c_get_ops(bus
);
333 struct dm_i2c_bus
*i2c
= bus
->uclass_priv
;
337 * If we have a method, call it. If not then the driver probably wants
338 * to deal with speed changes on the next transfer. It can easily read
339 * the current speed from this uclass
341 if (ops
->set_bus_speed
) {
342 ret
= ops
->set_bus_speed(bus
, speed
);
346 i2c
->speed_hz
= speed
;
351 int dm_i2c_get_bus_speed(struct udevice
*bus
)
353 struct dm_i2c_ops
*ops
= i2c_get_ops(bus
);
354 struct dm_i2c_bus
*i2c
= bus
->uclass_priv
;
356 if (!ops
->get_bus_speed
)
357 return i2c
->speed_hz
;
359 return ops
->get_bus_speed(bus
);
362 int i2c_set_chip_flags(struct udevice
*dev
, uint flags
)
364 struct udevice
*bus
= dev
->parent
;
365 struct dm_i2c_chip
*chip
= dev_get_parent_platdata(dev
);
366 struct dm_i2c_ops
*ops
= i2c_get_ops(bus
);
369 if (ops
->set_flags
) {
370 ret
= ops
->set_flags(dev
, flags
);
379 int i2c_get_chip_flags(struct udevice
*dev
, uint
*flagsp
)
381 struct dm_i2c_chip
*chip
= dev_get_parent_platdata(dev
);
383 *flagsp
= chip
->flags
;
388 int i2c_set_chip_offset_len(struct udevice
*dev
, uint offset_len
)
390 struct dm_i2c_chip
*chip
= dev_get_parent_platdata(dev
);
392 if (offset_len
> I2C_MAX_OFFSET_LEN
)
394 chip
->offset_len
= offset_len
;
399 int i2c_deblock(struct udevice
*bus
)
401 struct dm_i2c_ops
*ops
= i2c_get_ops(bus
);
404 * We could implement a software deblocking here if we could get
405 * access to the GPIOs used by I2C, and switch them to GPIO mode
406 * and then back to I2C. This is somewhat beyond our powers in
407 * driver model at present, so for now just fail.
409 * See https://patchwork.ozlabs.org/patch/399040/
414 return ops
->deblock(bus
);
417 int i2c_chip_ofdata_to_platdata(const void *blob
, int node
,
418 struct dm_i2c_chip
*chip
)
420 chip
->offset_len
= fdtdec_get_int(gd
->fdt_blob
, node
,
421 "u-boot,i2c-offset-len", 1);
423 chip
->chip_addr
= fdtdec_get_int(gd
->fdt_blob
, node
, "reg", -1);
424 if (chip
->chip_addr
== -1) {
425 debug("%s: I2C Node '%s' has no 'reg' property\n", __func__
,
426 fdt_get_name(blob
, node
, NULL
));
433 static int i2c_post_probe(struct udevice
*dev
)
435 struct dm_i2c_bus
*i2c
= dev
->uclass_priv
;
437 i2c
->speed_hz
= fdtdec_get_int(gd
->fdt_blob
, dev
->of_offset
,
438 "clock-frequency", 100000);
440 return dm_i2c_set_bus_speed(dev
, i2c
->speed_hz
);
443 static int i2c_post_bind(struct udevice
*dev
)
445 /* Scan the bus for devices */
446 return dm_scan_fdt_node(dev
, gd
->fdt_blob
, dev
->of_offset
, false);
449 static int i2c_child_post_bind(struct udevice
*dev
)
451 struct dm_i2c_chip
*plat
= dev_get_parent_platdata(dev
);
453 if (dev
->of_offset
== -1)
456 return i2c_chip_ofdata_to_platdata(gd
->fdt_blob
, dev
->of_offset
, plat
);
459 UCLASS_DRIVER(i2c
) = {
462 .flags
= DM_UC_FLAG_SEQ_ALIAS
,
463 .post_bind
= i2c_post_bind
,
464 .post_probe
= i2c_post_probe
,
465 .per_device_auto_alloc_size
= sizeof(struct dm_i2c_bus
),
466 .per_child_platdata_auto_alloc_size
= sizeof(struct dm_i2c_chip
),
467 .child_post_bind
= i2c_child_post_bind
,
470 UCLASS_DRIVER(i2c_generic
) = {
471 .id
= UCLASS_I2C_GENERIC
,
472 .name
= "i2c_generic",
475 U_BOOT_DRIVER(i2c_generic_chip_drv
) = {
476 .name
= "i2c_generic_chip_drv",
477 .id
= UCLASS_I2C_GENERIC
,