1 // SPDX-License-Identifier: GPL-2.0+
5 * Copyright (c) 2014 Google, Inc
17 #include <dm/device-internal.h>
19 static int get_emul(struct udevice *dev, struct udevice **devp,
20 struct dm_i2c_ops **opsp)
22 struct dm_i2c_chip *plat;
27 plat = dev_get_parent_plat(dev);
29 ret = i2c_emul_find(dev, &plat->emul);
34 *opsp = i2c_get_ops(plat->emul);
39 void sandbox_i2c_set_test_mode(struct udevice *bus, bool test_mode)
41 struct sandbox_i2c_priv *priv = dev_get_priv(bus);
43 priv->test_mode = test_mode;
46 static int sandbox_i2c_xfer(struct udevice *bus, struct i2c_msg *msg,
49 struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
50 struct sandbox_i2c_priv *priv = dev_get_priv(bus);
51 struct dm_i2c_ops *ops;
52 struct udevice *emul, *dev;
56 /* Special test code to return success but with no emulation */
57 if (priv->test_mode && msg->addr == SANDBOX_I2C_TEST_ADDR)
60 ret = i2c_get_chip(bus, msg->addr, 1, &dev);
64 ret = get_emul(dev, &emul, &ops);
68 if (priv->test_mode) {
70 * For testing, don't allow writing above 100KHz for writes and
74 if (i2c->speed_hz > (is_read ? I2C_SPEED_FAST_RATE :
75 I2C_SPEED_STANDARD_RATE)) {
76 debug("%s: Max speed exceeded\n", __func__);
81 return ops->xfer(emul, msg, nmsgs);
84 static const struct dm_i2c_ops sandbox_i2c_ops = {
85 .xfer = sandbox_i2c_xfer,
88 static const struct udevice_id sandbox_i2c_ids[] = {
89 { .compatible = "sandbox,i2c" },
93 U_BOOT_DRIVER(sandbox_i2c) = {
94 .name = "sandbox_i2c",
96 .of_match = sandbox_i2c_ids,
97 .ops = &sandbox_i2c_ops,
98 .priv_auto = sizeof(struct sandbox_i2c_priv),