2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2014-12-10 15:55:50 +00:00
|
|
|
/*
|
|
|
|
* Simulate an I2C port
|
|
|
|
*
|
|
|
|
* Copyright (c) 2014 Google, Inc
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <dm.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <i2c.h>
|
2020-05-10 17:40:05 +00:00
|
|
|
#include <log.h>
|
2014-12-10 15:55:50 +00:00
|
|
|
#include <asm/test.h>
|
2020-07-07 19:11:48 +00:00
|
|
|
#include <dm/acpi.h>
|
2014-12-10 15:55:50 +00:00
|
|
|
#include <dm/lists.h>
|
|
|
|
#include <dm/device-internal.h>
|
|
|
|
|
2015-04-20 18:37:15 +00:00
|
|
|
struct sandbox_i2c_priv {
|
|
|
|
bool test_mode;
|
2014-12-10 15:55:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static int get_emul(struct udevice *dev, struct udevice **devp,
|
|
|
|
struct dm_i2c_ops **opsp)
|
|
|
|
{
|
2015-01-25 15:27:13 +00:00
|
|
|
struct dm_i2c_chip *plat;
|
2014-12-10 15:55:50 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
*devp = NULL;
|
|
|
|
*opsp = NULL;
|
2015-01-25 15:27:13 +00:00
|
|
|
plat = dev_get_parent_platdata(dev);
|
|
|
|
if (!plat->emul) {
|
2018-11-18 15:14:34 +00:00
|
|
|
ret = i2c_emul_find(dev, &plat->emul);
|
2014-12-10 15:55:50 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
2015-01-25 15:27:13 +00:00
|
|
|
*devp = plat->emul;
|
|
|
|
*opsp = i2c_get_ops(plat->emul);
|
2014-12-10 15:55:50 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-04-20 18:37:15 +00:00
|
|
|
void sandbox_i2c_set_test_mode(struct udevice *bus, bool test_mode)
|
|
|
|
{
|
|
|
|
struct sandbox_i2c_priv *priv = dev_get_priv(bus);
|
|
|
|
|
|
|
|
priv->test_mode = test_mode;
|
|
|
|
}
|
|
|
|
|
2014-12-10 15:55:50 +00:00
|
|
|
static int sandbox_i2c_xfer(struct udevice *bus, struct i2c_msg *msg,
|
|
|
|
int nmsgs)
|
|
|
|
{
|
2015-03-05 19:25:20 +00:00
|
|
|
struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
|
2015-04-20 18:37:15 +00:00
|
|
|
struct sandbox_i2c_priv *priv = dev_get_priv(bus);
|
2014-12-10 15:55:50 +00:00
|
|
|
struct dm_i2c_ops *ops;
|
|
|
|
struct udevice *emul, *dev;
|
|
|
|
bool is_read;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Special test code to return success but with no emulation */
|
2015-04-20 18:37:15 +00:00
|
|
|
if (priv->test_mode && msg->addr == SANDBOX_I2C_TEST_ADDR)
|
2014-12-10 15:55:50 +00:00
|
|
|
return 0;
|
|
|
|
|
2015-01-25 15:26:55 +00:00
|
|
|
ret = i2c_get_chip(bus, msg->addr, 1, &dev);
|
2014-12-10 15:55:50 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = get_emul(dev, &emul, &ops);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2015-04-20 18:37:15 +00:00
|
|
|
if (priv->test_mode) {
|
|
|
|
/*
|
|
|
|
* For testing, don't allow writing above 100KHz for writes and
|
|
|
|
* 400KHz for reads.
|
|
|
|
*/
|
|
|
|
is_read = nmsgs > 1;
|
2020-01-23 18:48:22 +00:00
|
|
|
if (i2c->speed_hz > (is_read ? I2C_SPEED_FAST_RATE :
|
|
|
|
I2C_SPEED_STANDARD_RATE)) {
|
2015-04-20 18:37:15 +00:00
|
|
|
debug("%s: Max speed exceeded\n", __func__);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2015-04-20 18:37:13 +00:00
|
|
|
}
|
2015-04-20 18:37:15 +00:00
|
|
|
|
2014-12-10 15:55:50 +00:00
|
|
|
return ops->xfer(emul, msg, nmsgs);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct dm_i2c_ops sandbox_i2c_ops = {
|
|
|
|
.xfer = sandbox_i2c_xfer,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct udevice_id sandbox_i2c_ids[] = {
|
|
|
|
{ .compatible = "sandbox,i2c" },
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
U_BOOT_DRIVER(i2c_sandbox) = {
|
|
|
|
.name = "i2c_sandbox",
|
|
|
|
.id = UCLASS_I2C,
|
|
|
|
.of_match = sandbox_i2c_ids,
|
|
|
|
.ops = &sandbox_i2c_ops,
|
2015-04-20 18:37:15 +00:00
|
|
|
.priv_auto_alloc_size = sizeof(struct sandbox_i2c_priv),
|
2014-12-10 15:55:50 +00:00
|
|
|
};
|