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