1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2014 Google, Inc
6 #define LOG_CATEGORY UCLASS_I2C_EMUL
13 #include <dm/device-internal.h>
14 #include <dm/uclass-internal.h>
17 * i2c emulation works using an 'emul' node at the bus level. Each device in
18 * that node is in the UCLASS_I2C_EMUL uclass, and emulates one i2c device. A
19 * pointer to the device it emulates is in the 'dev' property of the emul device
20 * uclass plat (struct i2c_emul_plat), put there by i2c_emul_find().
21 * When sandbox wants an emulator for a device, it calls i2c_emul_find() which
22 * searches for the emulator with the correct address. To find the device for an
23 * emulator, call i2c_emul_get_device().
25 * The 'emul' node is in the UCLASS_I2C_EMUL_PARENT uclass. We use a separate
26 * uclass so avoid having strange devices on the I2C bus.
29 struct udevice *i2c_emul_get_device(struct udevice *emul)
31 struct i2c_emul_uc_plat *uc_plat = dev_get_uclass_plat(emul);
36 void i2c_emul_set_idx(struct udevice *dev, int emul_idx)
38 struct dm_i2c_chip *plat = dev_get_parent_plat(dev);
40 plat->emul_idx = emul_idx;
43 int i2c_emul_find(struct udevice *dev, struct udevice **emulp)
45 struct i2c_emul_uc_plat *uc_plat;
49 if (!CONFIG_IS_ENABLED(OF_PLATDATA)) {
50 ret = uclass_find_device_by_phandle(UCLASS_I2C_EMUL, dev,
51 "sandbox,emul", &emul);
53 struct dm_i2c_chip *plat = dev_get_parent_plat(dev);
55 ret = device_get_by_ofplat_idx(plat->emul_idx, &emul);
58 log_err("No emulators for device '%s'\n", dev->name);
61 uc_plat = dev_get_uclass_plat(emul);
65 return device_probe(emul);
68 UCLASS_DRIVER(i2c_emul) = {
69 .id = UCLASS_I2C_EMUL,
71 .per_device_plat_auto = sizeof(struct i2c_emul_uc_plat),
75 * This uclass is a child of the i2c bus. Its plat is not defined here so
76 * is defined by its parent, UCLASS_I2C, which uses struct dm_i2c_chip. See
77 * per_child_plat_auto in UCLASS_DRIVER(i2c).
79 UCLASS_DRIVER(i2c_emul_parent) = {
80 .id = UCLASS_I2C_EMUL_PARENT,
81 .name = "i2c_emul_parent",
82 #if CONFIG_IS_ENABLED(OF_REAL)
83 .post_bind = dm_scan_fdt_dev,
87 static const struct udevice_id i2c_emul_parent_ids[] = {
88 { .compatible = "sandbox,i2c-emul-parent" },
92 U_BOOT_DRIVER(sandbox_i2c_emul_parent) = {
93 .name = "sandbox_i2c_emul_parent",
94 .id = UCLASS_I2C_EMUL_PARENT,
95 .of_match = i2c_emul_parent_ids,