1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2014 Google, Inc
10 #include <dm/device-internal.h>
11 #include <dm/uclass-internal.h>
14 * i2c emulation works using an 'emul' node at the bus level. Each device in
15 * that node is in the UCLASS_I2C_EMUL uclass, and emulates one i2c device. A
16 * pointer to the device it emulates is in the 'dev' property of the emul device
17 * uclass plat (struct i2c_emul_plat), put there by i2c_emul_find().
18 * When sandbox wants an emulator for a device, it calls i2c_emul_find() which
19 * searches for the emulator with the correct address. To find the device for an
20 * emulator, call i2c_emul_get_device().
22 * The 'emul' node is in the UCLASS_I2C_EMUL_PARENT uclass. We use a separate
23 * uclass so avoid having strange devices on the I2C bus.
27 * struct i2c_emul_uc_plat - information about the emulator for this device
29 * This is used by devices in UCLASS_I2C_EMUL to record information about the
30 * device being emulated. It is accessible with dev_get_uclass_plat()
32 * @dev: Device being emulated
34 struct i2c_emul_uc_plat {
38 struct udevice *i2c_emul_get_device(struct udevice *emul)
40 struct i2c_emul_uc_plat *uc_plat = dev_get_uclass_plat(emul);
45 int i2c_emul_find(struct udevice *dev, struct udevice **emulp)
47 struct i2c_emul_uc_plat *uc_plat;
51 ret = uclass_find_device_by_phandle(UCLASS_I2C_EMUL, dev,
52 "sandbox,emul", &emul);
54 log_err("No emulators for device '%s'\n", dev->name);
57 uc_plat = dev_get_uclass_plat(emul);
61 return device_probe(emul);
64 UCLASS_DRIVER(i2c_emul) = {
65 .id = UCLASS_I2C_EMUL,
67 .per_device_plat_auto = sizeof(struct i2c_emul_uc_plat),
71 * This uclass is a child of the i2c bus. Its plat is not defined here so
72 * is defined by its parent, UCLASS_I2C, which uses struct dm_i2c_chip. See
73 * per_child_plat_auto in UCLASS_DRIVER(i2c).
75 UCLASS_DRIVER(i2c_emul_parent) = {
76 .id = UCLASS_I2C_EMUL_PARENT,
77 .name = "i2c_emul_parent",
78 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
79 .post_bind = dm_scan_fdt_dev,
83 static const struct udevice_id i2c_emul_parent_ids[] = {
84 { .compatible = "sandbox,i2c-emul-parent" },
88 U_BOOT_DRIVER(i2c_emul_parent_drv) = {
89 .name = "i2c_emul_parent_drv",
90 .id = UCLASS_I2C_EMUL_PARENT,
91 .of_match = i2c_emul_parent_ids,