1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2014 Google, Inc
9 #include <dm/device-internal.h>
10 #include <dm/uclass-internal.h>
13 * i2c emulation works using an 'emul' node at the bus level. Each device in
14 * that node is in the UCLASS_I2C_EMUL uclass, and emulates one i2c device. A
15 * pointer to the device it emulates is in the 'dev' property of the emul device
16 * uclass platdata (struct i2c_emul_platdata), put there by i2c_emul_find().
17 * When sandbox wants an emulator for a device, it calls i2c_emul_find() which
18 * searches for the emulator with the correct address. To find the device for an
19 * emulator, call i2c_emul_get_device().
21 * The 'emul' node is in the UCLASS_I2C_EMUL_PARENT uclass. We use a separate
22 * uclass so avoid having strange devices on the I2C bus.
26 * struct i2c_emul_uc_platdata - information about the emulator for this device
28 * This is used by devices in UCLASS_I2C_EMUL to record information about the
29 * device being emulated. It is accessible with dev_get_uclass_platdata()
31 * @dev: Device being emulated
33 struct i2c_emul_uc_platdata {
37 struct udevice *i2c_emul_get_device(struct udevice *emul)
39 struct i2c_emul_uc_platdata *uc_plat = dev_get_uclass_platdata(emul);
44 int i2c_emul_find(struct udevice *dev, struct udevice **emulp)
46 struct i2c_emul_uc_platdata *uc_plat;
50 ret = uclass_find_device_by_phandle(UCLASS_I2C_EMUL, dev,
51 "sandbox,emul", &emul);
53 log_err("No emulators for device '%s'\n", dev->name);
56 uc_plat = dev_get_uclass_platdata(emul);
60 return device_probe(emul);
63 UCLASS_DRIVER(i2c_emul) = {
64 .id = UCLASS_I2C_EMUL,
66 .per_device_platdata_auto_alloc_size =
67 sizeof(struct i2c_emul_uc_platdata),
71 * This uclass is a child of the i2c bus. Its platdata is not defined here so
72 * is defined by its parent, UCLASS_I2C, which uses struct dm_i2c_chip. See
73 * per_child_platdata_auto_alloc_size in UCLASS_DRIVER(i2c).
75 UCLASS_DRIVER(i2c_emul_parent) = {
76 .id = UCLASS_I2C_EMUL_PARENT,
77 .name = "i2c_emul_parent",
78 .post_bind = dm_scan_fdt_dev,
81 static const struct udevice_id i2c_emul_parent_ids[] = {
82 { .compatible = "sandbox,i2c-emul-parent" },
86 U_BOOT_DRIVER(i2c_emul_parent_drv) = {
87 .name = "i2c_emul_parent_drv",
88 .id = UCLASS_I2C_EMUL_PARENT,
89 .of_match = i2c_emul_parent_ids,