i2c-emul-uclass.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2014 Google, Inc
  4. */
  5. #define LOG_CATEGORY UCLASS_I2C_EMUL
  6. #include <common.h>
  7. #include <dm.h>
  8. #include <i2c.h>
  9. #include <log.h>
  10. #include <asm/i2c.h>
  11. #include <dm/device-internal.h>
  12. #include <dm/uclass-internal.h>
  13. /*
  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().
  21. *
  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.
  24. */
  25. struct udevice *i2c_emul_get_device(struct udevice *emul)
  26. {
  27. struct i2c_emul_uc_plat *uc_plat = dev_get_uclass_plat(emul);
  28. return uc_plat->dev;
  29. }
  30. void i2c_emul_set_idx(struct udevice *dev, int emul_idx)
  31. {
  32. struct dm_i2c_chip *plat = dev_get_parent_plat(dev);
  33. plat->emul_idx = emul_idx;
  34. }
  35. int i2c_emul_find(struct udevice *dev, struct udevice **emulp)
  36. {
  37. struct i2c_emul_uc_plat *uc_plat;
  38. struct udevice *emul;
  39. int ret;
  40. if (!CONFIG_IS_ENABLED(OF_PLATDATA)) {
  41. ret = uclass_find_device_by_phandle(UCLASS_I2C_EMUL, dev,
  42. "sandbox,emul", &emul);
  43. } else {
  44. struct dm_i2c_chip *plat = dev_get_parent_plat(dev);
  45. ret = device_get_by_ofplat_idx(plat->emul_idx, &emul);
  46. }
  47. if (ret) {
  48. log_err("No emulators for device '%s'\n", dev->name);
  49. return ret;
  50. }
  51. uc_plat = dev_get_uclass_plat(emul);
  52. uc_plat->dev = dev;
  53. *emulp = emul;
  54. return device_probe(emul);
  55. }
  56. UCLASS_DRIVER(i2c_emul) = {
  57. .id = UCLASS_I2C_EMUL,
  58. .name = "i2c_emul",
  59. .per_device_plat_auto = sizeof(struct i2c_emul_uc_plat),
  60. };
  61. /*
  62. * This uclass is a child of the i2c bus. Its plat is not defined here so
  63. * is defined by its parent, UCLASS_I2C, which uses struct dm_i2c_chip. See
  64. * per_child_plat_auto in UCLASS_DRIVER(i2c).
  65. */
  66. UCLASS_DRIVER(i2c_emul_parent) = {
  67. .id = UCLASS_I2C_EMUL_PARENT,
  68. .name = "i2c_emul_parent",
  69. #if !CONFIG_IS_ENABLED(OF_PLATDATA)
  70. .post_bind = dm_scan_fdt_dev,
  71. #endif
  72. };
  73. static const struct udevice_id i2c_emul_parent_ids[] = {
  74. { .compatible = "sandbox,i2c-emul-parent" },
  75. { }
  76. };
  77. U_BOOT_DRIVER(sandbox_i2c_emul_parent) = {
  78. .name = "sandbox_i2c_emul_parent",
  79. .id = UCLASS_I2C_EMUL_PARENT,
  80. .of_match = i2c_emul_parent_ids,
  81. };