sandbox_i2c.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. DECLARE_GLOBAL_DATA_PTR;
  17. struct sandbox_i2c_priv {
  18. bool test_mode;
  19. };
  20. static int get_emul(struct udevice *dev, struct udevice **devp,
  21. struct dm_i2c_ops **opsp)
  22. {
  23. struct dm_i2c_chip *plat;
  24. struct udevice *child;
  25. int ret;
  26. *devp = NULL;
  27. *opsp = NULL;
  28. plat = dev_get_parent_platdata(dev);
  29. if (!plat->emul) {
  30. ret = dm_scan_fdt_dev(dev);
  31. if (ret)
  32. return ret;
  33. for (device_find_first_child(dev, &child); child;
  34. device_find_next_child(&child)) {
  35. if (device_get_uclass_id(child) != UCLASS_I2C_EMUL)
  36. continue;
  37. ret = device_probe(child);
  38. if (ret)
  39. return ret;
  40. break;
  41. }
  42. if (child)
  43. plat->emul = child;
  44. else
  45. return -ENODEV;
  46. }
  47. *devp = plat->emul;
  48. *opsp = i2c_get_ops(plat->emul);
  49. return 0;
  50. }
  51. void sandbox_i2c_set_test_mode(struct udevice *bus, bool test_mode)
  52. {
  53. struct sandbox_i2c_priv *priv = dev_get_priv(bus);
  54. priv->test_mode = test_mode;
  55. }
  56. static int sandbox_i2c_xfer(struct udevice *bus, struct i2c_msg *msg,
  57. int nmsgs)
  58. {
  59. struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
  60. struct sandbox_i2c_priv *priv = dev_get_priv(bus);
  61. struct dm_i2c_ops *ops;
  62. struct udevice *emul, *dev;
  63. bool is_read;
  64. int ret;
  65. /* Special test code to return success but with no emulation */
  66. if (priv->test_mode && msg->addr == SANDBOX_I2C_TEST_ADDR)
  67. return 0;
  68. ret = i2c_get_chip(bus, msg->addr, 1, &dev);
  69. if (ret)
  70. return ret;
  71. ret = get_emul(dev, &emul, &ops);
  72. if (ret)
  73. return ret;
  74. if (priv->test_mode) {
  75. /*
  76. * For testing, don't allow writing above 100KHz for writes and
  77. * 400KHz for reads.
  78. */
  79. is_read = nmsgs > 1;
  80. if (i2c->speed_hz > (is_read ? 400000 : 100000)) {
  81. debug("%s: Max speed exceeded\n", __func__);
  82. return -EINVAL;
  83. }
  84. }
  85. return ops->xfer(emul, msg, nmsgs);
  86. }
  87. static const struct dm_i2c_ops sandbox_i2c_ops = {
  88. .xfer = sandbox_i2c_xfer,
  89. };
  90. static const struct udevice_id sandbox_i2c_ids[] = {
  91. { .compatible = "sandbox,i2c" },
  92. { }
  93. };
  94. U_BOOT_DRIVER(i2c_sandbox) = {
  95. .name = "i2c_sandbox",
  96. .id = UCLASS_I2C,
  97. .of_match = sandbox_i2c_ids,
  98. .ops = &sandbox_i2c_ops,
  99. .priv_auto_alloc_size = sizeof(struct sandbox_i2c_priv),
  100. };