blob: 0dbbaa0c44730b86370b928bd67efc71dfae9ea3 [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>
12#include <asm/test.h>
13#include <dm/lists.h>
14#include <dm/device-internal.h>
Simon Glassd19de0d2014-12-10 08:55:50 -070015
Simon Glass182bf922015-04-20 12:37:15 -060016struct sandbox_i2c_priv {
17 bool test_mode;
Simon Glassd19de0d2014-12-10 08:55:50 -070018};
19
20static int get_emul(struct udevice *dev, struct udevice **devp,
21 struct dm_i2c_ops **opsp)
22{
Simon Glasse6f66ec2015-01-25 08:27:13 -070023 struct dm_i2c_chip *plat;
Simon Glassd19de0d2014-12-10 08:55:50 -070024 int ret;
25
26 *devp = NULL;
27 *opsp = NULL;
Simon Glasse6f66ec2015-01-25 08:27:13 -070028 plat = dev_get_parent_platdata(dev);
29 if (!plat->emul) {
Simon Glass031a6502018-11-18 08:14:34 -070030 ret = i2c_emul_find(dev, &plat->emul);
Simon Glassd19de0d2014-12-10 08:55:50 -070031 if (ret)
32 return ret;
Simon Glassd19de0d2014-12-10 08:55:50 -070033 }
Simon Glasse6f66ec2015-01-25 08:27:13 -070034 *devp = plat->emul;
35 *opsp = i2c_get_ops(plat->emul);
Simon Glassd19de0d2014-12-10 08:55:50 -070036
37 return 0;
38}
39
Simon Glass182bf922015-04-20 12:37:15 -060040void sandbox_i2c_set_test_mode(struct udevice *bus, bool test_mode)
41{
42 struct sandbox_i2c_priv *priv = dev_get_priv(bus);
43
44 priv->test_mode = test_mode;
45}
46
Simon Glassd19de0d2014-12-10 08:55:50 -070047static int sandbox_i2c_xfer(struct udevice *bus, struct i2c_msg *msg,
48 int nmsgs)
49{
Simon Glasse564f052015-03-05 12:25:20 -070050 struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
Simon Glass182bf922015-04-20 12:37:15 -060051 struct sandbox_i2c_priv *priv = dev_get_priv(bus);
Simon Glassd19de0d2014-12-10 08:55:50 -070052 struct dm_i2c_ops *ops;
53 struct udevice *emul, *dev;
54 bool is_read;
55 int ret;
56
57 /* Special test code to return success but with no emulation */
Simon Glass182bf922015-04-20 12:37:15 -060058 if (priv->test_mode && msg->addr == SANDBOX_I2C_TEST_ADDR)
Simon Glassd19de0d2014-12-10 08:55:50 -070059 return 0;
60
Simon Glass25ab4b02015-01-25 08:26:55 -070061 ret = i2c_get_chip(bus, msg->addr, 1, &dev);
Simon Glassd19de0d2014-12-10 08:55:50 -070062 if (ret)
63 return ret;
64
65 ret = get_emul(dev, &emul, &ops);
66 if (ret)
67 return ret;
68
Simon Glass182bf922015-04-20 12:37:15 -060069 if (priv->test_mode) {
70 /*
71 * For testing, don't allow writing above 100KHz for writes and
72 * 400KHz for reads.
73 */
74 is_read = nmsgs > 1;
75 if (i2c->speed_hz > (is_read ? 400000 : 100000)) {
76 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
93U_BOOT_DRIVER(i2c_sandbox) = {
94 .name = "i2c_sandbox",
95 .id = UCLASS_I2C,
96 .of_match = sandbox_i2c_ids,
Simon Glassd19de0d2014-12-10 08:55:50 -070097 .ops = &sandbox_i2c_ops,
Simon Glass182bf922015-04-20 12:37:15 -060098 .priv_auto_alloc_size = sizeof(struct sandbox_i2c_priv),
Simon Glassd19de0d2014-12-10 08:55:50 -070099};