i2c-emul-uclass.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 <asm/i2c.h>
  10. #include <dm/device-internal.h>
  11. #include <dm/uclass-internal.h>
  12. /*
  13. * i2c emulation works using an 'emul' node at the bus level. Each device in
  14. * that node is in the UCLASS_I2C_EMUL uclass, and emulates one i2c device. A
  15. * pointer to the device it emulates is in the 'dev' property of the emul device
  16. * uclass plat (struct i2c_emul_plat), put there by i2c_emul_find().
  17. * When sandbox wants an emulator for a device, it calls i2c_emul_find() which
  18. * searches for the emulator with the correct address. To find the device for an
  19. * emulator, call i2c_emul_get_device().
  20. *
  21. * The 'emul' node is in the UCLASS_I2C_EMUL_PARENT uclass. We use a separate
  22. * uclass so avoid having strange devices on the I2C bus.
  23. */
  24. struct udevice *i2c_emul_get_device(struct udevice *emul)
  25. {
  26. struct i2c_emul_uc_plat *uc_plat = dev_get_uclass_plat(emul);
  27. return uc_plat->dev;
  28. }
  29. void i2c_emul_set_idx(struct udevice *dev, int emul_idx)
  30. {
  31. struct dm_i2c_chip *plat = dev_get_parent_plat(dev);
  32. plat->emul_idx = emul_idx;
  33. }
  34. int i2c_emul_find(struct udevice *dev, struct udevice **emulp)
  35. {
  36. struct i2c_emul_uc_plat *uc_plat;
  37. struct udevice *emul;
  38. int ret;
  39. if (!CONFIG_IS_ENABLED(OF_PLATDATA)) {
  40. ret = uclass_find_device_by_phandle(UCLASS_I2C_EMUL, dev,
  41. "sandbox,emul", &emul);
  42. } else {
  43. struct dm_i2c_chip *plat = dev_get_parent_plat(dev);
  44. ret = device_get_by_ofplat_idx(plat->emul_idx, &emul);
  45. }
  46. if (ret) {
  47. log_err("No emulators for device '%s'\n", dev->name);
  48. return ret;
  49. }
  50. uc_plat = dev_get_uclass_plat(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_plat_auto = sizeof(struct i2c_emul_uc_plat),
  59. };
  60. /*
  61. * This uclass is a child of the i2c bus. Its plat is not defined here so
  62. * is defined by its parent, UCLASS_I2C, which uses struct dm_i2c_chip. See
  63. * per_child_plat_auto in UCLASS_DRIVER(i2c).
  64. */
  65. UCLASS_DRIVER(i2c_emul_parent) = {
  66. .id = UCLASS_I2C_EMUL_PARENT,
  67. .name = "i2c_emul_parent",
  68. #if !CONFIG_IS_ENABLED(OF_PLATDATA)
  69. .post_bind = dm_scan_fdt_dev,
  70. #endif
  71. };
  72. static const struct udevice_id i2c_emul_parent_ids[] = {
  73. { .compatible = "sandbox,i2c-emul-parent" },
  74. { }
  75. };
  76. U_BOOT_DRIVER(sandbox_i2c_emul_parent) = {
  77. .name = "sandbox_i2c_emul_parent",
  78. .id = UCLASS_I2C_EMUL_PARENT,
  79. .of_match = i2c_emul_parent_ids,
  80. };