From: Simon Glass Date: Sun, 18 Nov 2018 15:14:33 +0000 (-0700) Subject: dm: sandbox: i2c: Add a new 'emulation parent' uclass X-Git-Url: http://git.dujemihanovic.xyz/?a=commitdiff_plain;h=b7c25b11b6a1e3f840464224309b72a91b65bc56;p=u-boot.git dm: sandbox: i2c: Add a new 'emulation parent' uclass Sandbox i2c works using emulation drivers which are currently children of the i2c device: rtc_0: rtc@43 { reg = <0x43>; compatible = "sandbox-rtc"; emul { compatible = "sandbox,i2c-rtc"; }; }; In this case the emulation device is attached to i2c bus on address 0x43 and provides the Real-Time-Clock (RTC) functionality. However this is not ideal, since every device on an I2C bus has a child device. This is only really the case for sandbox, but we want to avoid special-case code for sandbox. A better approach seems to be to add a separate node on the bus, an 'emulation parent'. This can be given a bogus address (such as 0xff) and hides all the emulators away. Then we can use a phandle to point from the device to the correct emualtor, and only on sandbox. The code to find an emulator does not interfere with normal i2c operation. Add a new UCLASS_I2C_EMUL_PARENT uclass which allows finding an emulator given a bus, and finding a bus given an emulator. This will be used in a follow-on patch. Signed-off-by: Simon Glass --- diff --git a/drivers/i2c/i2c-emul-uclass.c b/drivers/i2c/i2c-emul-uclass.c index a2bdd5aa84..ae5aae03e7 100644 --- a/drivers/i2c/i2c-emul-uclass.c +++ b/drivers/i2c/i2c-emul-uclass.c @@ -6,8 +6,85 @@ #include #include #include +#include +#include + +/* + * i2c emulation works using an 'emul' node at the bus level. Each device in + * that node is in the UCLASS_I2C_EMUL uclass, and emulates one i2c device. A + * pointer to the device it emulates is in the 'dev' property of the emul device + * uclass platdata (struct i2c_emul_platdata), put there by i2c_emul_find(). + * When sandbox wants an emulator for a device, it calls i2c_emul_find() which + * searches for the emulator with the correct address. To find the device for an + * emulator, call i2c_emul_get_device(). + * + * The 'emul' node is in the UCLASS_I2C_EMUL_PARENT uclass. We use a separate + * uclass so avoid having strange devices on the I2C bus. + */ + +/** + * struct i2c_emul_uc_platdata - information about the emulator for this device + * + * This is used by devices in UCLASS_I2C_EMUL to record information about the + * device being emulated. It is accessible with dev_get_uclass_platdata() + * + * @dev: Device being emulated + */ +struct i2c_emul_uc_platdata { + struct udevice *dev; +}; + +struct udevice *i2c_emul_get_device(struct udevice *emul) +{ + struct i2c_emul_uc_platdata *uc_plat = dev_get_uclass_platdata(emul); + + return uc_plat->dev; +} + +int i2c_emul_find(struct udevice *dev, struct udevice **emulp) +{ + struct i2c_emul_uc_platdata *uc_plat; + struct udevice *emul; + int ret; + + ret = uclass_find_device_by_phandle(UCLASS_I2C_EMUL, dev, + "sandbox,emul", &emul); + if (ret) { + log_err("No emulators for device '%s'\n", dev->name); + return ret; + } + uc_plat = dev_get_uclass_platdata(emul); + uc_plat->dev = dev; + *emulp = emul; + + return device_probe(emul); +} UCLASS_DRIVER(i2c_emul) = { .id = UCLASS_I2C_EMUL, .name = "i2c_emul", + .per_device_platdata_auto_alloc_size = + sizeof(struct i2c_emul_uc_platdata), +}; + +/* + * This uclass is a child of the i2c bus. Its platdata is not defined here so + * is defined by its parent, UCLASS_I2C, which uses struct dm_i2c_chip. See + * per_child_platdata_auto_alloc_size in UCLASS_DRIVER(i2c). + */ +UCLASS_DRIVER(i2c_emul_parent) = { + .id = UCLASS_I2C_EMUL_PARENT, + .name = "i2c_emul_parent", + .post_bind = dm_scan_fdt_dev, +}; + +static const struct udevice_id i2c_emul_parent_ids[] = { + { .compatible = "sandbox,i2c-emul-parent" }, + { } +}; + +U_BOOT_DRIVER(i2c_emul_parent_drv) = { + .name = "i2c_emul_parent_drv", + .id = UCLASS_I2C_EMUL_PARENT, + .of_match = i2c_emul_parent_ids, }; diff --git a/include/dm/uclass-id.h b/include/dm/uclass-id.h index cd4387e5ae..a5fcb69dba 100644 --- a/include/dm/uclass-id.h +++ b/include/dm/uclass-id.h @@ -21,6 +21,7 @@ enum uclass_id { UCLASS_TEST_DUMMY, UCLASS_SPI_EMUL, /* sandbox SPI device emulator */ UCLASS_I2C_EMUL, /* sandbox I2C device emulator */ + UCLASS_I2C_EMUL_PARENT, /* parent for I2C device emulators */ UCLASS_PCI_EMUL, /* sandbox PCI device emulator */ UCLASS_USB_EMUL, /* sandbox USB bus device emulator */ UCLASS_AXI_EMUL, /* sandbox AXI bus device emulator */ diff --git a/include/i2c.h b/include/i2c.h index d33f827500..ccffc19552 100644 --- a/include/i2c.h +++ b/include/i2c.h @@ -536,6 +536,27 @@ int i2c_chip_ofdata_to_platdata(struct udevice *dev, struct dm_i2c_chip *chip); */ void i2c_dump_msgs(struct i2c_msg *msg, int nmsgs); +/** + * i2c_emul_find() - Find an emulator for an i2c sandbox device + * + * This looks at the device's 'emul' phandle + * + * @dev: Device to find an emulator for + * @emulp: Returns the associated emulator, if found * + * @return 0 if OK, -ENOENT or -ENODEV if not found + */ +int i2c_emul_find(struct udevice *dev, struct udevice **emulp); + +/** + * i2c_emul_get_device() - Find the device being emulated + * + * Given an emulator this returns the associated device + * + * @emul: Emulator for the device + * @return device that @emul is emulating + */ +struct udevice *i2c_emul_get_device(struct udevice *emul); + #ifndef CONFIG_DM_I2C /*