blob: 57b1c60fde6da10352fbe442cbd6e5feccb03ab8 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassd19de0d2014-12-10 08:55:50 -07002/*
3 * Simulate an I2C port
4 *
5 * Copyright (c) 2014 Google, Inc
Simon Glassd19de0d2014-12-10 08:55:50 -07006 */
7
8#include <common.h>
9#include <dm.h>
10#include <errno.h>
Simon Glassd19de0d2014-12-10 08:55:50 -070011#include <i2c.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060012#include <log.h>
Simon Glassd19de0d2014-12-10 08:55:50 -070013#include <asm/test.h>
Simon Glass31e17872020-07-07 13:11:48 -060014#include <dm/acpi.h>
Simon Glassd19de0d2014-12-10 08:55:50 -070015#include <dm/lists.h>
16#include <dm/device-internal.h>
Simon Glassd19de0d2014-12-10 08:55:50 -070017
Simon Glass182bf922015-04-20 12:37:15 -060018struct sandbox_i2c_priv {
19 bool test_mode;
Simon Glassd19de0d2014-12-10 08:55:50 -070020};
21
22static int get_emul(struct udevice *dev, struct udevice **devp,
23 struct dm_i2c_ops **opsp)
24{
Simon Glasse6f66ec2015-01-25 08:27:13 -070025 struct dm_i2c_chip *plat;
Simon Glassd19de0d2014-12-10 08:55:50 -070026 int ret;
27
28 *devp = NULL;
29 *opsp = NULL;
Simon Glasse6f66ec2015-01-25 08:27:13 -070030 plat = dev_get_parent_platdata(dev);
31 if (!plat->emul) {
Simon Glass031a6502018-11-18 08:14:34 -070032 ret = i2c_emul_find(dev, &plat->emul);
Simon Glassd19de0d2014-12-10 08:55:50 -070033 if (ret)
34 return ret;
Simon Glassd19de0d2014-12-10 08:55:50 -070035 }
Simon Glasse6f66ec2015-01-25 08:27:13 -070036 *devp = plat->emul;
37 *opsp = i2c_get_ops(plat->emul);
Simon Glassd19de0d2014-12-10 08:55:50 -070038
39 return 0;
40}
41
Simon Glass182bf922015-04-20 12:37:15 -060042void sandbox_i2c_set_test_mode(struct udevice *bus, bool test_mode)
43{
44 struct sandbox_i2c_priv *priv = dev_get_priv(bus);
45
46 priv->test_mode = test_mode;
47}
48
Simon Glassd19de0d2014-12-10 08:55:50 -070049static int sandbox_i2c_xfer(struct udevice *bus, struct i2c_msg *msg,
50 int nmsgs)
51{
Simon Glasse564f052015-03-05 12:25:20 -070052 struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
Simon Glass182bf922015-04-20 12:37:15 -060053 struct sandbox_i2c_priv *priv = dev_get_priv(bus);
Simon Glassd19de0d2014-12-10 08:55:50 -070054 struct dm_i2c_ops *ops;
55 struct udevice *emul, *dev;
56 bool is_read;
57 int ret;
58
59 /* Special test code to return success but with no emulation */
Simon Glass182bf922015-04-20 12:37:15 -060060 if (priv->test_mode && msg->addr == SANDBOX_I2C_TEST_ADDR)
Simon Glassd19de0d2014-12-10 08:55:50 -070061 return 0;
62
Simon Glass25ab4b02015-01-25 08:26:55 -070063 ret = i2c_get_chip(bus, msg->addr, 1, &dev);
Simon Glassd19de0d2014-12-10 08:55:50 -070064 if (ret)
65 return ret;
66
67 ret = get_emul(dev, &emul, &ops);
68 if (ret)
69 return ret;
70
Simon Glass182bf922015-04-20 12:37:15 -060071 if (priv->test_mode) {
72 /*
73 * For testing, don't allow writing above 100KHz for writes and
74 * 400KHz for reads.
75 */
76 is_read = nmsgs > 1;
Simon Glassf3d46152020-01-23 11:48:22 -070077 if (i2c->speed_hz > (is_read ? I2C_SPEED_FAST_RATE :
78 I2C_SPEED_STANDARD_RATE)) {
Simon Glass182bf922015-04-20 12:37:15 -060079 debug("%s: Max speed exceeded\n", __func__);
80 return -EINVAL;
81 }
Simon Glass1bde67b2015-04-20 12:37:13 -060082 }
Simon Glass182bf922015-04-20 12:37:15 -060083
Simon Glassd19de0d2014-12-10 08:55:50 -070084 return ops->xfer(emul, msg, nmsgs);
85}
86
87static const struct dm_i2c_ops sandbox_i2c_ops = {
88 .xfer = sandbox_i2c_xfer,
89};
90
Simon Glassd19de0d2014-12-10 08:55:50 -070091static const struct udevice_id sandbox_i2c_ids[] = {
92 { .compatible = "sandbox,i2c" },
93 { }
94};
95
96U_BOOT_DRIVER(i2c_sandbox) = {
97 .name = "i2c_sandbox",
98 .id = UCLASS_I2C,
99 .of_match = sandbox_i2c_ids,
Simon Glassd19de0d2014-12-10 08:55:50 -0700100 .ops = &sandbox_i2c_ops,
Simon Glass182bf922015-04-20 12:37:15 -0600101 .priv_auto_alloc_size = sizeof(struct sandbox_i2c_priv),
Simon Glassd19de0d2014-12-10 08:55:50 -0700102};