i2c-emul-uclass.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2014 Google, Inc
  4. */
  5. #include <common.h>
  6. #include <dm.h>
  7. #include <i2c.h>
  8. #include <log.h>
  9. #include <dm/device-internal.h>
  10. #include <dm/uclass-internal.h>
  11. /*
  12. * i2c emulation works using an 'emul' node at the bus level. Each device in
  13. * that node is in the UCLASS_I2C_EMUL uclass, and emulates one i2c device. A
  14. * pointer to the device it emulates is in the 'dev' property of the emul device
  15. * uclass platdata (struct i2c_emul_platdata), put there by i2c_emul_find().
  16. * When sandbox wants an emulator for a device, it calls i2c_emul_find() which
  17. * searches for the emulator with the correct address. To find the device for an
  18. * emulator, call i2c_emul_get_device().
  19. *
  20. * The 'emul' node is in the UCLASS_I2C_EMUL_PARENT uclass. We use a separate
  21. * uclass so avoid having strange devices on the I2C bus.
  22. */
  23. /**
  24. * struct i2c_emul_uc_platdata - information about the emulator for this device
  25. *
  26. * This is used by devices in UCLASS_I2C_EMUL to record information about the
  27. * device being emulated. It is accessible with dev_get_uclass_platdata()
  28. *
  29. * @dev: Device being emulated
  30. */
  31. struct i2c_emul_uc_platdata {
  32. struct udevice *dev;
  33. };
  34. struct udevice *i2c_emul_get_device(struct udevice *emul)
  35. {
  36. struct i2c_emul_uc_platdata *uc_plat = dev_get_uclass_platdata(emul);
  37. return uc_plat->dev;
  38. }
  39. int i2c_emul_find(struct udevice *dev, struct udevice **emulp)
  40. {
  41. struct i2c_emul_uc_platdata *uc_plat;
  42. struct udevice *emul;
  43. int ret;
  44. ret = uclass_find_device_by_phandle(UCLASS_I2C_EMUL, dev,
  45. "sandbox,emul", &emul);
  46. if (ret) {
  47. log_err("No emulators for device '%s'\n", dev->name);
  48. return ret;
  49. }
  50. uc_plat = dev_get_uclass_platdata(emul);
  51. uc_plat->dev = dev;
  52. *emulp = emul;
  53. return device_probe(emul);
  54. }
  55. UCLASS_DRIVER(i2c_emul) = {
  56. .id = UCLASS_I2C_EMUL,
  57. .name = "i2c_emul",
  58. .per_device_platdata_auto_alloc_size =
  59. sizeof(struct i2c_emul_uc_platdata),
  60. };
  61. /*
  62. * This uclass is a child of the i2c bus. Its platdata is not defined here so
  63. * is defined by its parent, UCLASS_I2C, which uses struct dm_i2c_chip. See
  64. * per_child_platdata_auto_alloc_size in UCLASS_DRIVER(i2c).
  65. */
  66. UCLASS_DRIVER(i2c_emul_parent) = {
  67. .id = UCLASS_I2C_EMUL_PARENT,
  68. .name = "i2c_emul_parent",
  69. .post_bind = dm_scan_fdt_dev,
  70. };
  71. static const struct udevice_id i2c_emul_parent_ids[] = {
  72. { .compatible = "sandbox,i2c-emul-parent" },
  73. { }
  74. };
  75. U_BOOT_DRIVER(i2c_emul_parent_drv) = {
  76. .name = "i2c_emul_parent_drv",
  77. .id = UCLASS_I2C_EMUL_PARENT,
  78. .of_match = i2c_emul_parent_ids,
  79. };