sandbox_i2c.c 2.3 KB

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