blob: c99e6de933279f1925be81042037ea957c7b66d6 [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 Glassf05a7c52020-12-19 10:39:54 -070013#include <asm/i2c.h>
Simon Glassd19de0d2014-12-10 08:55:50 -070014#include <asm/test.h>
Simon Glass31e17872020-07-07 13:11:48 -060015#include <dm/acpi.h>
Simon Glassd19de0d2014-12-10 08:55:50 -070016#include <dm/lists.h>
17#include <dm/device-internal.h>
Simon Glassd19de0d2014-12-10 08:55:50 -070018
Simon Glassd19de0d2014-12-10 08:55:50 -070019static int get_emul(struct udevice *dev, struct udevice **devp,
20 struct dm_i2c_ops **opsp)
21{
Simon Glasse6f66ec2015-01-25 08:27:13 -070022 struct dm_i2c_chip *plat;
Simon Glassd19de0d2014-12-10 08:55:50 -070023 int ret;
24
25 *devp = NULL;
26 *opsp = NULL;
Simon Glasscaa4daa2020-12-03 16:55:18 -070027 plat = dev_get_parent_plat(dev);
Simon Glasse6f66ec2015-01-25 08:27:13 -070028 if (!plat->emul) {
Simon Glass031a6502018-11-18 08:14:34 -070029 ret = i2c_emul_find(dev, &plat->emul);
Simon Glassd19de0d2014-12-10 08:55:50 -070030 if (ret)
31 return ret;
Simon Glassd19de0d2014-12-10 08:55:50 -070032 }
Simon Glasse6f66ec2015-01-25 08:27:13 -070033 *devp = plat->emul;
34 *opsp = i2c_get_ops(plat->emul);
Simon Glassd19de0d2014-12-10 08:55:50 -070035
36 return 0;
37}
38
Simon Glass182bf922015-04-20 12:37:15 -060039void sandbox_i2c_set_test_mode(struct udevice *bus, bool test_mode)
40{
41 struct sandbox_i2c_priv *priv = dev_get_priv(bus);
42
43 priv->test_mode = test_mode;
44}
45
Simon Glassd19de0d2014-12-10 08:55:50 -070046static int sandbox_i2c_xfer(struct udevice *bus, struct i2c_msg *msg,
47 int nmsgs)
48{
Simon Glasse564f052015-03-05 12:25:20 -070049 struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
Simon Glass182bf922015-04-20 12:37:15 -060050 struct sandbox_i2c_priv *priv = dev_get_priv(bus);
Simon Glassd19de0d2014-12-10 08:55:50 -070051 struct dm_i2c_ops *ops;
52 struct udevice *emul, *dev;
53 bool is_read;
54 int ret;
55
56 /* Special test code to return success but with no emulation */
Simon Glass182bf922015-04-20 12:37:15 -060057 if (priv->test_mode && msg->addr == SANDBOX_I2C_TEST_ADDR)
Simon Glassd19de0d2014-12-10 08:55:50 -070058 return 0;
59
Simon Glass25ab4b02015-01-25 08:26:55 -070060 ret = i2c_get_chip(bus, msg->addr, 1, &dev);
Simon Glassd19de0d2014-12-10 08:55:50 -070061 if (ret)
62 return ret;
63
64 ret = get_emul(dev, &emul, &ops);
65 if (ret)
66 return ret;
67
Simon Glass182bf922015-04-20 12:37:15 -060068 if (priv->test_mode) {
69 /*
70 * For testing, don't allow writing above 100KHz for writes and
71 * 400KHz for reads.
72 */
73 is_read = nmsgs > 1;
Simon Glassf3d46152020-01-23 11:48:22 -070074 if (i2c->speed_hz > (is_read ? I2C_SPEED_FAST_RATE :
75 I2C_SPEED_STANDARD_RATE)) {
Simon Glass182bf922015-04-20 12:37:15 -060076 debug("%s: Max speed exceeded\n", __func__);
77 return -EINVAL;
78 }
Simon Glass1bde67b2015-04-20 12:37:13 -060079 }
Simon Glass182bf922015-04-20 12:37:15 -060080
Simon Glassd19de0d2014-12-10 08:55:50 -070081 return ops->xfer(emul, msg, nmsgs);
82}
83
84static const struct dm_i2c_ops sandbox_i2c_ops = {
85 .xfer = sandbox_i2c_xfer,
86};
87
Simon Glassd19de0d2014-12-10 08:55:50 -070088static const struct udevice_id sandbox_i2c_ids[] = {
89 { .compatible = "sandbox,i2c" },
90 { }
91};
92
Simon Glassfbe27a52020-10-03 11:31:36 -060093U_BOOT_DRIVER(sandbox_i2c) = {
94 .name = "sandbox_i2c",
Simon Glassd19de0d2014-12-10 08:55:50 -070095 .id = UCLASS_I2C,
96 .of_match = sandbox_i2c_ids,
Simon Glassd19de0d2014-12-10 08:55:50 -070097 .ops = &sandbox_i2c_ops,
Simon Glass41575d82020-12-03 16:55:17 -070098 .priv_auto = sizeof(struct sandbox_i2c_priv),
Simon Glassd19de0d2014-12-10 08:55:50 -070099};