blob: ae5aae03e72a4f5b97b4f50eed70d8dfd5e38078 [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
6#include <common.h>
7#include <dm.h>
8#include <i2c.h>
Simon Glass4b0ecc62018-11-18 08:14:33 -07009#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 */
33struct i2c_emul_uc_platdata {
34 struct udevice *dev;
35};
36
37struct 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
44int 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}
Simon Glassdaa54702014-12-10 08:55:49 -070062
63UCLASS_DRIVER(i2c_emul) = {
64 .id = UCLASS_I2C_EMUL,
65 .name = "i2c_emul",
Simon Glass4b0ecc62018-11-18 08:14:33 -070066 .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 */
75UCLASS_DRIVER(i2c_emul_parent) = {
76 .id = UCLASS_I2C_EMUL_PARENT,
77 .name = "i2c_emul_parent",
78 .post_bind = dm_scan_fdt_dev,
79};
80
81static const struct udevice_id i2c_emul_parent_ids[] = {
82 { .compatible = "sandbox,i2c-emul-parent" },
83 { }
84};
85
86U_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,
Simon Glassdaa54702014-12-10 08:55:49 -070090};