i2c-emul-uclass.c 2.5 KB

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