4 * Copyright (c) 2014 Google, Inc
6 * SPDX-License-Identifier: GPL-2.0+
16 #include <dm/device-internal.h>
19 DECLARE_GLOBAL_DATA_PTR;
21 struct dm_sandbox_i2c_emul_priv {
25 static int get_emul(struct udevice *dev, struct udevice **devp,
26 struct dm_i2c_ops **opsp)
28 struct dm_i2c_chip *plat;
33 plat = dev_get_parent_platdata(dev);
35 ret = dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset,
40 ret = device_get_child(dev, 0, &plat->emul);
45 *opsp = i2c_get_ops(plat->emul);
50 static int sandbox_i2c_xfer(struct udevice *bus, struct i2c_msg *msg,
53 struct dm_i2c_bus *i2c = bus->uclass_priv;
54 struct dm_i2c_ops *ops;
55 struct udevice *emul, *dev;
59 /* Special test code to return success but with no emulation */
60 if (msg->addr == SANDBOX_I2C_TEST_ADDR)
63 ret = i2c_get_chip(bus, msg->addr, 1, &dev);
67 ret = get_emul(dev, &emul, &ops);
72 * For testing, don't allow writing above 100KHz for writes and
76 if (i2c->speed_hz > (is_read ? 400000 : 100000))
78 return ops->xfer(emul, msg, nmsgs);
81 static const struct dm_i2c_ops sandbox_i2c_ops = {
82 .xfer = sandbox_i2c_xfer,
85 static const struct udevice_id sandbox_i2c_ids[] = {
86 { .compatible = "sandbox,i2c" },
90 U_BOOT_DRIVER(i2c_sandbox) = {
91 .name = "i2c_sandbox",
93 .of_match = sandbox_i2c_ids,
94 .ops = &sandbox_i2c_ops,