blob: 0954d53847e62045cfb3939a64735f43b8221d17 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassdaa54702014-12-10 08:55:49 -07002/*
3 * Copyright (c) 2014 Google, Inc
Simon Glassdaa54702014-12-10 08:55:49 -07004 */
5
Patrick Delaunay81313352021-04-27 11:02:19 +02006#define LOG_CATEGORY UCLASS_I2C_EMUL
7
Simon Glassdaa54702014-12-10 08:55:49 -07008#include <dm.h>
9#include <i2c.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Simon Glass3ee1d392021-03-15 17:25:27 +130011#include <asm/i2c.h>
Simon Glass4b0ecc62018-11-18 08:14:33 -070012#include <dm/device-internal.h>
13#include <dm/uclass-internal.h>
14
15/*
16 * i2c emulation works using an 'emul' node at the bus level. Each device in
17 * that node is in the UCLASS_I2C_EMUL uclass, and emulates one i2c device. A
18 * pointer to the device it emulates is in the 'dev' property of the emul device
Simon Glassb75b15b2020-12-03 16:55:23 -070019 * uclass plat (struct i2c_emul_plat), put there by i2c_emul_find().
Simon Glass4b0ecc62018-11-18 08:14:33 -070020 * When sandbox wants an emulator for a device, it calls i2c_emul_find() which
21 * searches for the emulator with the correct address. To find the device for an
22 * emulator, call i2c_emul_get_device().
23 *
24 * The 'emul' node is in the UCLASS_I2C_EMUL_PARENT uclass. We use a separate
25 * uclass so avoid having strange devices on the I2C bus.
26 */
27
Simon Glass4b0ecc62018-11-18 08:14:33 -070028struct udevice *i2c_emul_get_device(struct udevice *emul)
29{
Simon Glassb75b15b2020-12-03 16:55:23 -070030 struct i2c_emul_uc_plat *uc_plat = dev_get_uclass_plat(emul);
Simon Glass4b0ecc62018-11-18 08:14:33 -070031
32 return uc_plat->dev;
33}
34
Simon Glass65cb7592021-03-15 17:25:30 +130035void i2c_emul_set_idx(struct udevice *dev, int emul_idx)
36{
37 struct dm_i2c_chip *plat = dev_get_parent_plat(dev);
38
39 plat->emul_idx = emul_idx;
40}
41
Simon Glass4b0ecc62018-11-18 08:14:33 -070042int i2c_emul_find(struct udevice *dev, struct udevice **emulp)
43{
Simon Glassb75b15b2020-12-03 16:55:23 -070044 struct i2c_emul_uc_plat *uc_plat;
Simon Glass4b0ecc62018-11-18 08:14:33 -070045 struct udevice *emul;
46 int ret;
47
Sean Anderson7bf2f8c2023-10-14 16:47:46 -040048 if (CONFIG_IS_ENABLED(OF_REAL)) {
Simon Glass65cb7592021-03-15 17:25:30 +130049 ret = uclass_find_device_by_phandle(UCLASS_I2C_EMUL, dev,
50 "sandbox,emul", &emul);
51 } else {
52 struct dm_i2c_chip *plat = dev_get_parent_plat(dev);
53
54 ret = device_get_by_ofplat_idx(plat->emul_idx, &emul);
55 }
Simon Glass4b0ecc62018-11-18 08:14:33 -070056 if (ret) {
57 log_err("No emulators for device '%s'\n", dev->name);
58 return ret;
59 }
Simon Glass71fa5b42020-12-03 16:55:18 -070060 uc_plat = dev_get_uclass_plat(emul);
Simon Glass4b0ecc62018-11-18 08:14:33 -070061 uc_plat->dev = dev;
62 *emulp = emul;
63
64 return device_probe(emul);
65}
Simon Glassdaa54702014-12-10 08:55:49 -070066
67UCLASS_DRIVER(i2c_emul) = {
68 .id = UCLASS_I2C_EMUL,
69 .name = "i2c_emul",
Simon Glassb75b15b2020-12-03 16:55:23 -070070 .per_device_plat_auto = sizeof(struct i2c_emul_uc_plat),
Simon Glass4b0ecc62018-11-18 08:14:33 -070071};
72
73/*
Simon Glass71fa5b42020-12-03 16:55:18 -070074 * This uclass is a child of the i2c bus. Its plat is not defined here so
Simon Glass4b0ecc62018-11-18 08:14:33 -070075 * is defined by its parent, UCLASS_I2C, which uses struct dm_i2c_chip. See
Simon Glass71fa5b42020-12-03 16:55:18 -070076 * per_child_plat_auto in UCLASS_DRIVER(i2c).
Simon Glass4b0ecc62018-11-18 08:14:33 -070077 */
78UCLASS_DRIVER(i2c_emul_parent) = {
79 .id = UCLASS_I2C_EMUL_PARENT,
80 .name = "i2c_emul_parent",
Simon Glass92882652021-08-07 07:24:04 -060081#if CONFIG_IS_ENABLED(OF_REAL)
Simon Glass4b0ecc62018-11-18 08:14:33 -070082 .post_bind = dm_scan_fdt_dev,
Simon Glasse45710b2020-10-03 11:31:34 -060083#endif
Simon Glass4b0ecc62018-11-18 08:14:33 -070084};
85
86static const struct udevice_id i2c_emul_parent_ids[] = {
87 { .compatible = "sandbox,i2c-emul-parent" },
88 { }
89};
90
Simon Glass98af3742021-02-03 06:01:17 -070091U_BOOT_DRIVER(sandbox_i2c_emul_parent) = {
92 .name = "sandbox_i2c_emul_parent",
Simon Glass4b0ecc62018-11-18 08:14:33 -070093 .id = UCLASS_I2C_EMUL_PARENT,
94 .of_match = i2c_emul_parent_ids,
Simon Glassdaa54702014-12-10 08:55:49 -070095};