sandbox_i2c.c 2.3 KB

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