4 * Copyright (c) 2014 Google, Inc
6 * SPDX-License-Identifier: GPL-2.0+
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;
25 struct udevice *child;
30 plat = dev_get_parent_platdata(dev);
32 ret = dm_scan_fdt_dev(dev);
36 for (device_find_first_child(dev, &child); child;
37 device_find_next_child(&child)) {
38 if (device_get_uclass_id(child) != UCLASS_I2C_EMUL)
41 ret = device_probe(child);
54 *opsp = i2c_get_ops(plat->emul);
59 void sandbox_i2c_set_test_mode(struct udevice *bus, bool test_mode)
61 struct sandbox_i2c_priv *priv = dev_get_priv(bus);
63 priv->test_mode = test_mode;
66 static int sandbox_i2c_xfer(struct udevice *bus, struct i2c_msg *msg,
69 struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
70 struct sandbox_i2c_priv *priv = dev_get_priv(bus);
71 struct dm_i2c_ops *ops;
72 struct udevice *emul, *dev;
76 /* Special test code to return success but with no emulation */
77 if (priv->test_mode && msg->addr == SANDBOX_I2C_TEST_ADDR)
80 ret = i2c_get_chip(bus, msg->addr, 1, &dev);
84 ret = get_emul(dev, &emul, &ops);
88 if (priv->test_mode) {
90 * For testing, don't allow writing above 100KHz for writes and
94 if (i2c->speed_hz > (is_read ? 400000 : 100000)) {
95 debug("%s: Max speed exceeded\n", __func__);
100 return ops->xfer(emul, msg, nmsgs);
103 static const struct dm_i2c_ops sandbox_i2c_ops = {
104 .xfer = sandbox_i2c_xfer,
107 static const struct udevice_id sandbox_i2c_ids[] = {
108 { .compatible = "sandbox,i2c" },
112 U_BOOT_DRIVER(i2c_sandbox) = {
113 .name = "i2c_sandbox",
115 .of_match = sandbox_i2c_ids,
116 .ops = &sandbox_i2c_ops,
117 .priv_auto_alloc_size = sizeof(struct sandbox_i2c_priv),