blob: 74bb5e93397363328cf2cce491ac2cacb54ec449 [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
Simon Glass3834c842014-12-10 08:55:50 -07008#include <dm.h>
9#include <errno.h>
Simon Glass3834c842014-12-10 08:55:50 -070010#include <i2c.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Simon Glass4662f612020-12-19 10:39:54 -070012#include <asm/i2c.h>
Simon Glass3834c842014-12-10 08:55:50 -070013#include <asm/test.h>
Simon Glass39ab8672020-07-07 13:11:48 -060014#include <dm/acpi.h>
Simon Glass3834c842014-12-10 08:55:50 -070015#include <dm/lists.h>
16#include <dm/device-internal.h>
Simon Glass3834c842014-12-10 08:55:50 -070017
Simon Glass3834c842014-12-10 08:55:50 -070018static int get_emul(struct udevice *dev, struct udevice **devp,
19 struct dm_i2c_ops **opsp)
20{
Simon Glass713c3f02015-01-25 08:27:13 -070021 struct dm_i2c_chip *plat;
Simon Glass3834c842014-12-10 08:55:50 -070022 int ret;
23
24 *devp = NULL;
25 *opsp = NULL;
Simon Glass71fa5b42020-12-03 16:55:18 -070026 plat = dev_get_parent_plat(dev);
Simon Glass713c3f02015-01-25 08:27:13 -070027 if (!plat->emul) {
Simon Glass17b56f62018-11-18 08:14:34 -070028 ret = i2c_emul_find(dev, &plat->emul);
Simon Glass3834c842014-12-10 08:55:50 -070029 if (ret)
30 return ret;
Simon Glass3834c842014-12-10 08:55:50 -070031 }
Simon Glass713c3f02015-01-25 08:27:13 -070032 *devp = plat->emul;
33 *opsp = i2c_get_ops(plat->emul);
Simon Glass3834c842014-12-10 08:55:50 -070034
35 return 0;
36}
37
Simon Glass4c70ed92015-04-20 12:37:15 -060038void sandbox_i2c_set_test_mode(struct udevice *bus, bool test_mode)
39{
40 struct sandbox_i2c_priv *priv = dev_get_priv(bus);
41
42 priv->test_mode = test_mode;
43}
44
Simon Glass3834c842014-12-10 08:55:50 -070045static int sandbox_i2c_xfer(struct udevice *bus, struct i2c_msg *msg,
46 int nmsgs)
47{
Simon Glassde0977b2015-03-05 12:25:20 -070048 struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
Simon Glass4c70ed92015-04-20 12:37:15 -060049 struct sandbox_i2c_priv *priv = dev_get_priv(bus);
Simon Glass3834c842014-12-10 08:55:50 -070050 struct dm_i2c_ops *ops;
51 struct udevice *emul, *dev;
52 bool is_read;
53 int ret;
54
55 /* Special test code to return success but with no emulation */
Simon Glass4c70ed92015-04-20 12:37:15 -060056 if (priv->test_mode && msg->addr == SANDBOX_I2C_TEST_ADDR)
Simon Glass3834c842014-12-10 08:55:50 -070057 return 0;
58
Simon Glassa2723ae2015-01-25 08:26:55 -070059 ret = i2c_get_chip(bus, msg->addr, 1, &dev);
Simon Glass3834c842014-12-10 08:55:50 -070060 if (ret)
61 return ret;
62
63 ret = get_emul(dev, &emul, &ops);
64 if (ret)
65 return ret;
66
Simon Glass4c70ed92015-04-20 12:37:15 -060067 if (priv->test_mode) {
68 /*
69 * For testing, don't allow writing above 100KHz for writes and
70 * 400KHz for reads.
71 */
72 is_read = nmsgs > 1;
Simon Glassf0c99c52020-01-23 11:48:22 -070073 if (i2c->speed_hz > (is_read ? I2C_SPEED_FAST_RATE :
74 I2C_SPEED_STANDARD_RATE)) {
Simon Glass4c70ed92015-04-20 12:37:15 -060075 debug("%s: Max speed exceeded\n", __func__);
76 return -EINVAL;
77 }
Simon Glass34d8e952015-04-20 12:37:13 -060078 }
Simon Glass4c70ed92015-04-20 12:37:15 -060079
Simon Glass3834c842014-12-10 08:55:50 -070080 return ops->xfer(emul, msg, nmsgs);
81}
82
83static const struct dm_i2c_ops sandbox_i2c_ops = {
84 .xfer = sandbox_i2c_xfer,
85};
86
Simon Glass3834c842014-12-10 08:55:50 -070087static const struct udevice_id sandbox_i2c_ids[] = {
88 { .compatible = "sandbox,i2c" },
89 { }
90};
91
Simon Glass4d4558e2020-10-03 11:31:36 -060092U_BOOT_DRIVER(sandbox_i2c) = {
93 .name = "sandbox_i2c",
Simon Glass3834c842014-12-10 08:55:50 -070094 .id = UCLASS_I2C,
95 .of_match = sandbox_i2c_ids,
Simon Glass3834c842014-12-10 08:55:50 -070096 .ops = &sandbox_i2c_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -070097 .priv_auto = sizeof(struct sandbox_i2c_priv),
Simon Glass3834c842014-12-10 08:55:50 -070098};