sandbox_i2c.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Simulate an I2C port
  3. *
  4. * Copyright (c) 2014 Google, Inc
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <common.h>
  9. #include <dm.h>
  10. #include <errno.h>
  11. #include <fdtdec.h>
  12. #include <i2c.h>
  13. #include <asm/test.h>
  14. #include <dm/lists.h>
  15. #include <dm/device-internal.h>
  16. #include <dm/root.h>
  17. DECLARE_GLOBAL_DATA_PTR;
  18. struct sandbox_i2c_priv {
  19. bool test_mode;
  20. };
  21. static int get_emul(struct udevice *dev, struct udevice **devp,
  22. struct dm_i2c_ops **opsp)
  23. {
  24. struct dm_i2c_chip *plat;
  25. struct udevice *child;
  26. int ret;
  27. *devp = NULL;
  28. *opsp = NULL;
  29. plat = dev_get_parent_platdata(dev);
  30. if (!plat->emul) {
  31. ret = dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset,
  32. false);
  33. if (ret)
  34. return ret;
  35. for (device_find_first_child(dev, &child); child;
  36. device_find_next_child(&child)) {
  37. if (device_get_uclass_id(child) != UCLASS_I2C_EMUL)
  38. continue;
  39. ret = device_probe(child);
  40. if (ret)
  41. return ret;
  42. break;
  43. }
  44. if (child)
  45. plat->emul = child;
  46. else
  47. return -ENODEV;
  48. }
  49. *devp = plat->emul;
  50. *opsp = i2c_get_ops(plat->emul);
  51. return 0;
  52. }
  53. void sandbox_i2c_set_test_mode(struct udevice *bus, bool test_mode)
  54. {
  55. struct sandbox_i2c_priv *priv = dev_get_priv(bus);
  56. priv->test_mode = test_mode;
  57. }
  58. static int sandbox_i2c_xfer(struct udevice *bus, struct i2c_msg *msg,
  59. int nmsgs)
  60. {
  61. struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
  62. struct sandbox_i2c_priv *priv = dev_get_priv(bus);
  63. struct dm_i2c_ops *ops;
  64. struct udevice *emul, *dev;
  65. bool is_read;
  66. int ret;
  67. /* Special test code to return success but with no emulation */
  68. if (priv->test_mode && msg->addr == SANDBOX_I2C_TEST_ADDR)
  69. return 0;
  70. ret = i2c_get_chip(bus, msg->addr, 1, &dev);
  71. if (ret)
  72. return ret;
  73. ret = get_emul(dev, &emul, &ops);
  74. if (ret)
  75. return ret;
  76. if (priv->test_mode) {
  77. /*
  78. * For testing, don't allow writing above 100KHz for writes and
  79. * 400KHz for reads.
  80. */
  81. is_read = nmsgs > 1;
  82. if (i2c->speed_hz > (is_read ? 400000 : 100000)) {
  83. debug("%s: Max speed exceeded\n", __func__);
  84. return -EINVAL;
  85. }
  86. }
  87. return ops->xfer(emul, msg, nmsgs);
  88. }
  89. static const struct dm_i2c_ops sandbox_i2c_ops = {
  90. .xfer = sandbox_i2c_xfer,
  91. };
  92. static const struct udevice_id sandbox_i2c_ids[] = {
  93. { .compatible = "sandbox,i2c" },
  94. { }
  95. };
  96. U_BOOT_DRIVER(i2c_sandbox) = {
  97. .name = "i2c_sandbox",
  98. .id = UCLASS_I2C,
  99. .of_match = sandbox_i2c_ids,
  100. .ops = &sandbox_i2c_ops,
  101. .priv_auto_alloc_size = sizeof(struct sandbox_i2c_priv),
  102. };