blob: f4ae2397a03f98822e4515cbb47b295c626712a3 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass3834c842014-12-10 08:55:50 -07002/*
3 * Simulate an I2C port
4 *
5 * Copyright (c) 2014 Google, Inc
Simon Glass3834c842014-12-10 08:55:50 -07006 */
7
8#include <common.h>
9#include <dm.h>
10#include <errno.h>
Simon Glass3834c842014-12-10 08:55:50 -070011#include <i2c.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Simon Glass3834c842014-12-10 08:55:50 -070013#include <asm/test.h>
14#include <dm/lists.h>
15#include <dm/device-internal.h>
Simon Glass3834c842014-12-10 08:55:50 -070016
Simon Glass4c70ed92015-04-20 12:37:15 -060017struct sandbox_i2c_priv {
18 bool test_mode;
Simon Glass3834c842014-12-10 08:55:50 -070019};
20
21static int get_emul(struct udevice *dev, struct udevice **devp,
22 struct dm_i2c_ops **opsp)
23{
Simon Glass713c3f02015-01-25 08:27:13 -070024 struct dm_i2c_chip *plat;
Simon Glass3834c842014-12-10 08:55:50 -070025 int ret;
26
27 *devp = NULL;
28 *opsp = NULL;
Simon Glass713c3f02015-01-25 08:27:13 -070029 plat = dev_get_parent_platdata(dev);
30 if (!plat->emul) {
Simon Glass17b56f62018-11-18 08:14:34 -070031 ret = i2c_emul_find(dev, &plat->emul);
Simon Glass3834c842014-12-10 08:55:50 -070032 if (ret)
33 return ret;
Simon Glass3834c842014-12-10 08:55:50 -070034 }
Simon Glass713c3f02015-01-25 08:27:13 -070035 *devp = plat->emul;
36 *opsp = i2c_get_ops(plat->emul);
Simon Glass3834c842014-12-10 08:55:50 -070037
38 return 0;
39}
40
Simon Glass4c70ed92015-04-20 12:37:15 -060041void sandbox_i2c_set_test_mode(struct udevice *bus, bool test_mode)
42{
43 struct sandbox_i2c_priv *priv = dev_get_priv(bus);
44
45 priv->test_mode = test_mode;
46}
47
Simon Glass3834c842014-12-10 08:55:50 -070048static int sandbox_i2c_xfer(struct udevice *bus, struct i2c_msg *msg,
49 int nmsgs)
50{
Simon Glassde0977b2015-03-05 12:25:20 -070051 struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
Simon Glass4c70ed92015-04-20 12:37:15 -060052 struct sandbox_i2c_priv *priv = dev_get_priv(bus);
Simon Glass3834c842014-12-10 08:55:50 -070053 struct dm_i2c_ops *ops;
54 struct udevice *emul, *dev;
55 bool is_read;
56 int ret;
57
58 /* Special test code to return success but with no emulation */
Simon Glass4c70ed92015-04-20 12:37:15 -060059 if (priv->test_mode && msg->addr == SANDBOX_I2C_TEST_ADDR)
Simon Glass3834c842014-12-10 08:55:50 -070060 return 0;
61
Simon Glassa2723ae2015-01-25 08:26:55 -070062 ret = i2c_get_chip(bus, msg->addr, 1, &dev);
Simon Glass3834c842014-12-10 08:55:50 -070063 if (ret)
64 return ret;
65
66 ret = get_emul(dev, &emul, &ops);
67 if (ret)
68 return ret;
69
Simon Glass4c70ed92015-04-20 12:37:15 -060070 if (priv->test_mode) {
71 /*
72 * For testing, don't allow writing above 100KHz for writes and
73 * 400KHz for reads.
74 */
75 is_read = nmsgs > 1;
Simon Glassf0c99c52020-01-23 11:48:22 -070076 if (i2c->speed_hz > (is_read ? I2C_SPEED_FAST_RATE :
77 I2C_SPEED_STANDARD_RATE)) {
Simon Glass4c70ed92015-04-20 12:37:15 -060078 debug("%s: Max speed exceeded\n", __func__);
79 return -EINVAL;
80 }
Simon Glass34d8e952015-04-20 12:37:13 -060081 }
Simon Glass4c70ed92015-04-20 12:37:15 -060082
Simon Glass3834c842014-12-10 08:55:50 -070083 return ops->xfer(emul, msg, nmsgs);
84}
85
86static const struct dm_i2c_ops sandbox_i2c_ops = {
87 .xfer = sandbox_i2c_xfer,
88};
89
Simon Glass3834c842014-12-10 08:55:50 -070090static const struct udevice_id sandbox_i2c_ids[] = {
91 { .compatible = "sandbox,i2c" },
92 { }
93};
94
95U_BOOT_DRIVER(i2c_sandbox) = {
96 .name = "i2c_sandbox",
97 .id = UCLASS_I2C,
98 .of_match = sandbox_i2c_ids,
Simon Glass3834c842014-12-10 08:55:50 -070099 .ops = &sandbox_i2c_ops,
Simon Glass4c70ed92015-04-20 12:37:15 -0600100 .priv_auto_alloc_size = sizeof(struct sandbox_i2c_priv),
Simon Glass3834c842014-12-10 08:55:50 -0700101};