4 * Copyright (c) 2014 Google, Inc
6 * SPDX-License-Identifier: GPL-2.0+
15 #include <dm/device-internal.h>
17 DECLARE_GLOBAL_DATA_PTR
;
19 struct sandbox_i2c_priv
{
23 static int get_emul(struct udevice
*dev
, struct udevice
**devp
,
24 struct dm_i2c_ops
**opsp
)
26 struct dm_i2c_chip
*plat
;
27 struct udevice
*child
;
32 plat
= dev_get_parent_platdata(dev
);
34 ret
= dm_scan_fdt_dev(dev
);
38 for (device_find_first_child(dev
, &child
); child
;
39 device_find_next_child(&child
)) {
40 if (device_get_uclass_id(child
) != UCLASS_I2C_EMUL
)
43 ret
= device_probe(child
);
56 *opsp
= i2c_get_ops(plat
->emul
);
61 void sandbox_i2c_set_test_mode(struct udevice
*bus
, bool test_mode
)
63 struct sandbox_i2c_priv
*priv
= dev_get_priv(bus
);
65 priv
->test_mode
= test_mode
;
68 static int sandbox_i2c_xfer(struct udevice
*bus
, struct i2c_msg
*msg
,
71 struct dm_i2c_bus
*i2c
= dev_get_uclass_priv(bus
);
72 struct sandbox_i2c_priv
*priv
= dev_get_priv(bus
);
73 struct dm_i2c_ops
*ops
;
74 struct udevice
*emul
, *dev
;
78 /* Special test code to return success but with no emulation */
79 if (priv
->test_mode
&& msg
->addr
== SANDBOX_I2C_TEST_ADDR
)
82 ret
= i2c_get_chip(bus
, msg
->addr
, 1, &dev
);
86 ret
= get_emul(dev
, &emul
, &ops
);
90 if (priv
->test_mode
) {
92 * For testing, don't allow writing above 100KHz for writes and
96 if (i2c
->speed_hz
> (is_read
? 400000 : 100000)) {
97 debug("%s: Max speed exceeded\n", __func__
);
102 return ops
->xfer(emul
, msg
, nmsgs
);
105 static const struct dm_i2c_ops sandbox_i2c_ops
= {
106 .xfer
= sandbox_i2c_xfer
,
109 static const struct udevice_id sandbox_i2c_ids
[] = {
110 { .compatible
= "sandbox,i2c" },
114 U_BOOT_DRIVER(i2c_sandbox
) = {
115 .name
= "i2c_sandbox",
117 .of_match
= sandbox_i2c_ids
,
118 .ops
= &sandbox_i2c_ops
,
119 .priv_auto_alloc_size
= sizeof(struct sandbox_i2c_priv
),