1
2
3
4
5
6#define LOG_CATEGORY UCLASS_I2C_EMUL
7
8#include <common.h>
9#include <dm.h>
10#include <i2c.h>
11#include <log.h>
12#include <asm/i2c.h>
13#include <dm/device-internal.h>
14#include <dm/uclass-internal.h>
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29struct udevice *i2c_emul_get_device(struct udevice *emul)
30{
31 struct i2c_emul_uc_plat *uc_plat = dev_get_uclass_plat(emul);
32
33 return uc_plat->dev;
34}
35
36void i2c_emul_set_idx(struct udevice *dev, int emul_idx)
37{
38 struct dm_i2c_chip *plat = dev_get_parent_plat(dev);
39
40 plat->emul_idx = emul_idx;
41}
42
43int i2c_emul_find(struct udevice *dev, struct udevice **emulp)
44{
45 struct i2c_emul_uc_plat *uc_plat;
46 struct udevice *emul;
47 int ret;
48
49 if (!CONFIG_IS_ENABLED(OF_PLATDATA)) {
50 ret = uclass_find_device_by_phandle(UCLASS_I2C_EMUL, dev,
51 "sandbox,emul", &emul);
52 } else {
53 struct dm_i2c_chip *plat = dev_get_parent_plat(dev);
54
55 ret = device_get_by_ofplat_idx(plat->emul_idx, &emul);
56 }
57 if (ret) {
58 log_err("No emulators for device '%s'\n", dev->name);
59 return ret;
60 }
61 uc_plat = dev_get_uclass_plat(emul);
62 uc_plat->dev = dev;
63 *emulp = emul;
64
65 return device_probe(emul);
66}
67
68UCLASS_DRIVER(i2c_emul) = {
69 .id = UCLASS_I2C_EMUL,
70 .name = "i2c_emul",
71 .per_device_plat_auto = sizeof(struct i2c_emul_uc_plat),
72};
73
74
75
76
77
78
79UCLASS_DRIVER(i2c_emul_parent) = {
80 .id = UCLASS_I2C_EMUL_PARENT,
81 .name = "i2c_emul_parent",
82#if !CONFIG_IS_ENABLED(OF_PLATDATA)
83 .post_bind = dm_scan_fdt_dev,
84#endif
85};
86
87static const struct udevice_id i2c_emul_parent_ids[] = {
88 { .compatible = "sandbox,i2c-emul-parent" },
89 { }
90};
91
92U_BOOT_DRIVER(sandbox_i2c_emul_parent) = {
93 .name = "sandbox_i2c_emul_parent",
94 .id = UCLASS_I2C_EMUL_PARENT,
95 .of_match = i2c_emul_parent_ids,
96};
97