blob: 74bb5e93397363328cf2cce491ac2cacb54ec449 [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
Simon Glassd19de0d2014-12-10 08:55:50 -07008#include <dm.h>
9#include <errno.h>
Simon Glassd19de0d2014-12-10 08:55:50 -070010#include <i2c.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060011#include <log.h>
Simon Glassf05a7c52020-12-19 10:39:54 -070012#include <asm/i2c.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 Glassd19de0d2014-12-10 08:55:50 -070018static int get_emul(struct udevice *dev, struct udevice **devp,
19 struct dm_i2c_ops **opsp)
20{
Simon Glasse6f66ec2015-01-25 08:27:13 -070021 struct dm_i2c_chip *plat;
Simon Glassd19de0d2014-12-10 08:55:50 -070022 int ret;
23
24 *devp = NULL;
25 *opsp = NULL;
Simon Glasscaa4daa2020-12-03 16:55:18 -070026 plat = dev_get_parent_plat(dev);
Simon Glasse6f66ec2015-01-25 08:27:13 -070027 if (!plat->emul) {
Simon Glass031a6502018-11-18 08:14:34 -070028 ret = i2c_emul_find(dev, &plat->emul);
Simon Glassd19de0d2014-12-10 08:55:50 -070029 if (ret)
30 return ret;
Simon Glassd19de0d2014-12-10 08:55:50 -070031 }
Simon Glasse6f66ec2015-01-25 08:27:13 -070032 *devp = plat->emul;
33 *opsp = i2c_get_ops(plat->emul);
Simon Glassd19de0d2014-12-10 08:55:50 -070034
35 return 0;
36}
37
Simon Glass182bf922015-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 Glassd19de0d2014-12-10 08:55:50 -070045static int sandbox_i2c_xfer(struct udevice *bus, struct i2c_msg *msg,
46 int nmsgs)
47{
Simon Glasse564f052015-03-05 12:25:20 -070048 struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
Simon Glass182bf922015-04-20 12:37:15 -060049 struct sandbox_i2c_priv *priv = dev_get_priv(bus);
Simon Glassd19de0d2014-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 Glass182bf922015-04-20 12:37:15 -060056 if (priv->test_mode && msg->addr == SANDBOX_I2C_TEST_ADDR)
Simon Glassd19de0d2014-12-10 08:55:50 -070057 return 0;
58
Simon Glass25ab4b02015-01-25 08:26:55 -070059 ret = i2c_get_chip(bus, msg->addr, 1, &dev);
Simon Glassd19de0d2014-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 Glass182bf922015-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 Glassf3d46152020-01-23 11:48:22 -070073 if (i2c->speed_hz > (is_read ? I2C_SPEED_FAST_RATE :
74 I2C_SPEED_STANDARD_RATE)) {
Simon Glass182bf922015-04-20 12:37:15 -060075 debug("%s: Max speed exceeded\n", __func__);
76 return -EINVAL;
77 }
Simon Glass1bde67b2015-04-20 12:37:13 -060078 }
Simon Glass182bf922015-04-20 12:37:15 -060079
Simon Glassd19de0d2014-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 Glassd19de0d2014-12-10 08:55:50 -070087static const struct udevice_id sandbox_i2c_ids[] = {
88 { .compatible = "sandbox,i2c" },
89 { }
90};
91
Simon Glassfbe27a52020-10-03 11:31:36 -060092U_BOOT_DRIVER(sandbox_i2c) = {
93 .name = "sandbox_i2c",
Simon Glassd19de0d2014-12-10 08:55:50 -070094 .id = UCLASS_I2C,
95 .of_match = sandbox_i2c_ids,
Simon Glassd19de0d2014-12-10 08:55:50 -070096 .ops = &sandbox_i2c_ops,
Simon Glass41575d82020-12-03 16:55:17 -070097 .priv_auto = sizeof(struct sandbox_i2c_priv),
Simon Glassd19de0d2014-12-10 08:55:50 -070098};