1 // SPDX-License-Identifier: GPL-2.0+
5 * Copyright (c) 2014 Google, Inc
15 #include <dm/device-internal.h>
17 struct sandbox_i2c_priv {
21 static int get_emul(struct udevice *dev, struct udevice **devp,
22 struct dm_i2c_ops **opsp)
24 struct dm_i2c_chip *plat;
29 plat = dev_get_parent_platdata(dev);
31 ret = i2c_emul_find(dev, &plat->emul);
36 *opsp = i2c_get_ops(plat->emul);
41 void sandbox_i2c_set_test_mode(struct udevice *bus, bool test_mode)
43 struct sandbox_i2c_priv *priv = dev_get_priv(bus);
45 priv->test_mode = test_mode;
48 static int sandbox_i2c_xfer(struct udevice *bus, struct i2c_msg *msg,
51 struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
52 struct sandbox_i2c_priv *priv = dev_get_priv(bus);
53 struct dm_i2c_ops *ops;
54 struct udevice *emul, *dev;
58 /* Special test code to return success but with no emulation */
59 if (priv->test_mode && msg->addr == SANDBOX_I2C_TEST_ADDR)
62 ret = i2c_get_chip(bus, msg->addr, 1, &dev);
66 ret = get_emul(dev, &emul, &ops);
70 if (priv->test_mode) {
72 * For testing, don't allow writing above 100KHz for writes and
76 if (i2c->speed_hz > (is_read ? I2C_SPEED_FAST_RATE :
77 I2C_SPEED_STANDARD_RATE)) {
78 debug("%s: Max speed exceeded\n", __func__);
83 return ops->xfer(emul, msg, nmsgs);
86 static const struct dm_i2c_ops sandbox_i2c_ops = {
87 .xfer = sandbox_i2c_xfer,
90 static const struct udevice_id sandbox_i2c_ids[] = {
91 { .compatible = "sandbox,i2c" },
95 U_BOOT_DRIVER(i2c_sandbox) = {
96 .name = "i2c_sandbox",
98 .of_match = sandbox_i2c_ids,
99 .ops = &sandbox_i2c_ops,
100 .priv_auto_alloc_size = sizeof(struct sandbox_i2c_priv),