2018-05-07 05:58:06 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2014-12-10 23:55:49 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2014 Google, Inc
|
|
|
|
*/
|
|
|
|
|
2021-04-27 17:02:19 +08:00
|
|
|
#define LOG_CATEGORY UCLASS_I2C_EMUL
|
|
|
|
|
2024-05-19 10:20:43 +08:00
|
|
|
#include <common.h>
|
2014-12-10 23:55:49 +08:00
|
|
|
#include <dm.h>
|
|
|
|
#include <i2c.h>
|
2020-05-11 01:40:05 +08:00
|
|
|
#include <log.h>
|
2021-03-15 12:25:27 +08:00
|
|
|
#include <asm/i2c.h>
|
2018-11-18 23:14:33 +08:00
|
|
|
#include <dm/device-internal.h>
|
|
|
|
#include <dm/uclass-internal.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* i2c emulation works using an 'emul' node at the bus level. Each device in
|
|
|
|
* that node is in the UCLASS_I2C_EMUL uclass, and emulates one i2c device. A
|
|
|
|
* pointer to the device it emulates is in the 'dev' property of the emul device
|
2020-12-04 07:55:23 +08:00
|
|
|
* uclass plat (struct i2c_emul_plat), put there by i2c_emul_find().
|
2018-11-18 23:14:33 +08:00
|
|
|
* When sandbox wants an emulator for a device, it calls i2c_emul_find() which
|
|
|
|
* searches for the emulator with the correct address. To find the device for an
|
|
|
|
* emulator, call i2c_emul_get_device().
|
|
|
|
*
|
|
|
|
* The 'emul' node is in the UCLASS_I2C_EMUL_PARENT uclass. We use a separate
|
|
|
|
* uclass so avoid having strange devices on the I2C bus.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct udevice *i2c_emul_get_device(struct udevice *emul)
|
|
|
|
{
|
2020-12-04 07:55:23 +08:00
|
|
|
struct i2c_emul_uc_plat *uc_plat = dev_get_uclass_plat(emul);
|
2018-11-18 23:14:33 +08:00
|
|
|
|
|
|
|
return uc_plat->dev;
|
|
|
|
}
|
|
|
|
|
2021-03-15 12:25:30 +08:00
|
|
|
void i2c_emul_set_idx(struct udevice *dev, int emul_idx)
|
|
|
|
{
|
|
|
|
struct dm_i2c_chip *plat = dev_get_parent_plat(dev);
|
|
|
|
|
|
|
|
plat->emul_idx = emul_idx;
|
|
|
|
}
|
|
|
|
|
2018-11-18 23:14:33 +08:00
|
|
|
int i2c_emul_find(struct udevice *dev, struct udevice **emulp)
|
|
|
|
{
|
2020-12-04 07:55:23 +08:00
|
|
|
struct i2c_emul_uc_plat *uc_plat;
|
2018-11-18 23:14:33 +08:00
|
|
|
struct udevice *emul;
|
|
|
|
int ret;
|
|
|
|
|
2023-10-15 04:47:46 +08:00
|
|
|
if (CONFIG_IS_ENABLED(OF_REAL)) {
|
2021-03-15 12:25:30 +08:00
|
|
|
ret = uclass_find_device_by_phandle(UCLASS_I2C_EMUL, dev,
|
|
|
|
"sandbox,emul", &emul);
|
|
|
|
} else {
|
|
|
|
struct dm_i2c_chip *plat = dev_get_parent_plat(dev);
|
|
|
|
|
|
|
|
ret = device_get_by_ofplat_idx(plat->emul_idx, &emul);
|
|
|
|
}
|
2018-11-18 23:14:33 +08:00
|
|
|
if (ret) {
|
|
|
|
log_err("No emulators for device '%s'\n", dev->name);
|
|
|
|
return ret;
|
|
|
|
}
|
2020-12-04 07:55:18 +08:00
|
|
|
uc_plat = dev_get_uclass_plat(emul);
|
2018-11-18 23:14:33 +08:00
|
|
|
uc_plat->dev = dev;
|
|
|
|
*emulp = emul;
|
|
|
|
|
|
|
|
return device_probe(emul);
|
|
|
|
}
|
2014-12-10 23:55:49 +08:00
|
|
|
|
|
|
|
UCLASS_DRIVER(i2c_emul) = {
|
|
|
|
.id = UCLASS_I2C_EMUL,
|
|
|
|
.name = "i2c_emul",
|
2020-12-04 07:55:23 +08:00
|
|
|
.per_device_plat_auto = sizeof(struct i2c_emul_uc_plat),
|
2018-11-18 23:14:33 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
2020-12-04 07:55:18 +08:00
|
|
|
* This uclass is a child of the i2c bus. Its plat is not defined here so
|
2018-11-18 23:14:33 +08:00
|
|
|
* is defined by its parent, UCLASS_I2C, which uses struct dm_i2c_chip. See
|
2020-12-04 07:55:18 +08:00
|
|
|
* per_child_plat_auto in UCLASS_DRIVER(i2c).
|
2018-11-18 23:14:33 +08:00
|
|
|
*/
|
|
|
|
UCLASS_DRIVER(i2c_emul_parent) = {
|
|
|
|
.id = UCLASS_I2C_EMUL_PARENT,
|
|
|
|
.name = "i2c_emul_parent",
|
2021-08-07 21:24:04 +08:00
|
|
|
#if CONFIG_IS_ENABLED(OF_REAL)
|
2018-11-18 23:14:33 +08:00
|
|
|
.post_bind = dm_scan_fdt_dev,
|
2020-10-04 01:31:34 +08:00
|
|
|
#endif
|
2018-11-18 23:14:33 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct udevice_id i2c_emul_parent_ids[] = {
|
|
|
|
{ .compatible = "sandbox,i2c-emul-parent" },
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
2021-02-03 21:01:17 +08:00
|
|
|
U_BOOT_DRIVER(sandbox_i2c_emul_parent) = {
|
|
|
|
.name = "sandbox_i2c_emul_parent",
|
2018-11-18 23:14:33 +08:00
|
|
|
.id = UCLASS_I2C_EMUL_PARENT,
|
|
|
|
.of_match = i2c_emul_parent_ids,
|
2014-12-10 23:55:49 +08:00
|
|
|
};
|