thermal: imx_tmu: support i.MX8MP
[oweals/u-boot.git] / drivers / i2c / i2c-emul-uclass.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2014 Google, Inc
4  */
5
6 #include <common.h>
7 #include <dm.h>
8 #include <i2c.h>
9 #include <dm/device-internal.h>
10 #include <dm/uclass-internal.h>
11
12 /*
13  * i2c emulation works using an 'emul' node at the bus level. Each device in
14  * that node is in the UCLASS_I2C_EMUL uclass, and emulates one i2c device. A
15  * pointer to the device it emulates is in the 'dev' property of the emul device
16  * uclass platdata (struct i2c_emul_platdata), put there by i2c_emul_find().
17  * When sandbox wants an emulator for a device, it calls i2c_emul_find() which
18  * searches for the emulator with the correct address. To find the device for an
19  * emulator, call i2c_emul_get_device().
20  *
21  * The 'emul' node is in the UCLASS_I2C_EMUL_PARENT uclass. We use a separate
22  * uclass so avoid having strange devices on the I2C bus.
23  */
24
25 /**
26  * struct i2c_emul_uc_platdata - information about the emulator for this device
27  *
28  * This is used by devices in UCLASS_I2C_EMUL to record information about the
29  * device being emulated. It is accessible with dev_get_uclass_platdata()
30  *
31  * @dev: Device being emulated
32  */
33 struct i2c_emul_uc_platdata {
34         struct udevice *dev;
35 };
36
37 struct udevice *i2c_emul_get_device(struct udevice *emul)
38 {
39         struct i2c_emul_uc_platdata *uc_plat = dev_get_uclass_platdata(emul);
40
41         return uc_plat->dev;
42 }
43
44 int i2c_emul_find(struct udevice *dev, struct udevice **emulp)
45 {
46         struct i2c_emul_uc_platdata *uc_plat;
47         struct udevice *emul;
48         int ret;
49
50         ret = uclass_find_device_by_phandle(UCLASS_I2C_EMUL, dev,
51                                             "sandbox,emul", &emul);
52         if (ret) {
53                 log_err("No emulators for device '%s'\n", dev->name);
54                 return ret;
55         }
56         uc_plat = dev_get_uclass_platdata(emul);
57         uc_plat->dev = dev;
58         *emulp = emul;
59
60         return device_probe(emul);
61 }
62
63 UCLASS_DRIVER(i2c_emul) = {
64         .id             = UCLASS_I2C_EMUL,
65         .name           = "i2c_emul",
66         .per_device_platdata_auto_alloc_size =
67                  sizeof(struct i2c_emul_uc_platdata),
68 };
69
70 /*
71  * This uclass is a child of the i2c bus. Its platdata is not defined here so
72  * is defined by its parent, UCLASS_I2C, which uses struct dm_i2c_chip. See
73  * per_child_platdata_auto_alloc_size in UCLASS_DRIVER(i2c).
74  */
75 UCLASS_DRIVER(i2c_emul_parent) = {
76         .id             = UCLASS_I2C_EMUL_PARENT,
77         .name           = "i2c_emul_parent",
78         .post_bind      = dm_scan_fdt_dev,
79 };
80
81 static const struct udevice_id i2c_emul_parent_ids[] = {
82         { .compatible = "sandbox,i2c-emul-parent" },
83         { }
84 };
85
86 U_BOOT_DRIVER(i2c_emul_parent_drv) = {
87         .name           = "i2c_emul_parent_drv",
88         .id             = UCLASS_I2C_EMUL_PARENT,
89         .of_match       = i2c_emul_parent_ids,
90 };