blob: 7917b63c55b1f53b3e39e1d6283febd0c44256fb [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassc70c71d2014-12-10 08:55:49 -07002/*
3 * Copyright (c) 2014 Google, Inc
Simon Glassc70c71d2014-12-10 08:55:49 -07004 */
5
6#include <common.h>
7#include <dm.h>
8#include <i2c.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -06009#include <log.h>
Simon Glasse62ad9c2021-03-15 17:25:27 +130010#include <asm/i2c.h>
Simon Glassb7c25b12018-11-18 08:14:33 -070011#include <dm/device-internal.h>
12#include <dm/uclass-internal.h>
13
14/*
15 * i2c emulation works using an 'emul' node at the bus level. Each device in
16 * that node is in the UCLASS_I2C_EMUL uclass, and emulates one i2c device. A
17 * pointer to the device it emulates is in the 'dev' property of the emul device
Simon Glass8a8d24b2020-12-03 16:55:23 -070018 * uclass plat (struct i2c_emul_plat), put there by i2c_emul_find().
Simon Glassb7c25b12018-11-18 08:14:33 -070019 * When sandbox wants an emulator for a device, it calls i2c_emul_find() which
20 * searches for the emulator with the correct address. To find the device for an
21 * emulator, call i2c_emul_get_device().
22 *
23 * The 'emul' node is in the UCLASS_I2C_EMUL_PARENT uclass. We use a separate
24 * uclass so avoid having strange devices on the I2C bus.
25 */
26
Simon Glassb7c25b12018-11-18 08:14:33 -070027struct udevice *i2c_emul_get_device(struct udevice *emul)
28{
Simon Glass8a8d24b2020-12-03 16:55:23 -070029 struct i2c_emul_uc_plat *uc_plat = dev_get_uclass_plat(emul);
Simon Glassb7c25b12018-11-18 08:14:33 -070030
31 return uc_plat->dev;
32}
33
Simon Glass728d04c2021-03-15 17:25:30 +130034void i2c_emul_set_idx(struct udevice *dev, int emul_idx)
35{
36 struct dm_i2c_chip *plat = dev_get_parent_plat(dev);
37
38 plat->emul_idx = emul_idx;
39}
40
Simon Glassb7c25b12018-11-18 08:14:33 -070041int i2c_emul_find(struct udevice *dev, struct udevice **emulp)
42{
Simon Glass8a8d24b2020-12-03 16:55:23 -070043 struct i2c_emul_uc_plat *uc_plat;
Simon Glassb7c25b12018-11-18 08:14:33 -070044 struct udevice *emul;
45 int ret;
46
Simon Glass728d04c2021-03-15 17:25:30 +130047 if (!CONFIG_IS_ENABLED(OF_PLATDATA)) {
48 ret = uclass_find_device_by_phandle(UCLASS_I2C_EMUL, dev,
49 "sandbox,emul", &emul);
50 } else {
51 struct dm_i2c_chip *plat = dev_get_parent_plat(dev);
52
53 ret = device_get_by_ofplat_idx(plat->emul_idx, &emul);
54 }
Simon Glassb7c25b12018-11-18 08:14:33 -070055 if (ret) {
56 log_err("No emulators for device '%s'\n", dev->name);
57 return ret;
58 }
Simon Glasscaa4daa2020-12-03 16:55:18 -070059 uc_plat = dev_get_uclass_plat(emul);
Simon Glassb7c25b12018-11-18 08:14:33 -070060 uc_plat->dev = dev;
61 *emulp = emul;
62
63 return device_probe(emul);
64}
Simon Glassc70c71d2014-12-10 08:55:49 -070065
66UCLASS_DRIVER(i2c_emul) = {
67 .id = UCLASS_I2C_EMUL,
68 .name = "i2c_emul",
Simon Glass8a8d24b2020-12-03 16:55:23 -070069 .per_device_plat_auto = sizeof(struct i2c_emul_uc_plat),
Simon Glassb7c25b12018-11-18 08:14:33 -070070};
71
72/*
Simon Glasscaa4daa2020-12-03 16:55:18 -070073 * This uclass is a child of the i2c bus. Its plat is not defined here so
Simon Glassb7c25b12018-11-18 08:14:33 -070074 * is defined by its parent, UCLASS_I2C, which uses struct dm_i2c_chip. See
Simon Glasscaa4daa2020-12-03 16:55:18 -070075 * per_child_plat_auto in UCLASS_DRIVER(i2c).
Simon Glassb7c25b12018-11-18 08:14:33 -070076 */
77UCLASS_DRIVER(i2c_emul_parent) = {
78 .id = UCLASS_I2C_EMUL_PARENT,
79 .name = "i2c_emul_parent",
Simon Glass67507e42020-10-03 11:31:34 -060080#if !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glassb7c25b12018-11-18 08:14:33 -070081 .post_bind = dm_scan_fdt_dev,
Simon Glass67507e42020-10-03 11:31:34 -060082#endif
Simon Glassb7c25b12018-11-18 08:14:33 -070083};
84
85static const struct udevice_id i2c_emul_parent_ids[] = {
86 { .compatible = "sandbox,i2c-emul-parent" },
87 { }
88};
89
Simon Glassc4085d72021-02-03 06:01:17 -070090U_BOOT_DRIVER(sandbox_i2c_emul_parent) = {
91 .name = "sandbox_i2c_emul_parent",
Simon Glassb7c25b12018-11-18 08:14:33 -070092 .id = UCLASS_I2C_EMUL_PARENT,
93 .of_match = i2c_emul_parent_ids,
Simon Glassc70c71d2014-12-10 08:55:49 -070094};