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 | e62ad9c | 2021-03-15 17:25:27 +1300 | [diff] [blame^] | 10 | #include <asm/i2c.h> |
Simon Glass | b7c25b1 | 2018-11-18 08:14:33 -0700 | [diff] [blame] | 11 | #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 Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 18 | * uclass plat (struct i2c_emul_plat), put there by i2c_emul_find(). |
Simon Glass | b7c25b1 | 2018-11-18 08:14:33 -0700 | [diff] [blame] | 19 | * 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 Glass | b7c25b1 | 2018-11-18 08:14:33 -0700 | [diff] [blame] | 27 | struct udevice *i2c_emul_get_device(struct udevice *emul) |
| 28 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 29 | struct i2c_emul_uc_plat *uc_plat = dev_get_uclass_plat(emul); |
Simon Glass | b7c25b1 | 2018-11-18 08:14:33 -0700 | [diff] [blame] | 30 | |
| 31 | return uc_plat->dev; |
| 32 | } |
| 33 | |
| 34 | int i2c_emul_find(struct udevice *dev, struct udevice **emulp) |
| 35 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 36 | struct i2c_emul_uc_plat *uc_plat; |
Simon Glass | b7c25b1 | 2018-11-18 08:14:33 -0700 | [diff] [blame] | 37 | struct udevice *emul; |
| 38 | int ret; |
| 39 | |
| 40 | ret = uclass_find_device_by_phandle(UCLASS_I2C_EMUL, dev, |
| 41 | "sandbox,emul", &emul); |
| 42 | if (ret) { |
| 43 | log_err("No emulators for device '%s'\n", dev->name); |
| 44 | return ret; |
| 45 | } |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 46 | uc_plat = dev_get_uclass_plat(emul); |
Simon Glass | b7c25b1 | 2018-11-18 08:14:33 -0700 | [diff] [blame] | 47 | uc_plat->dev = dev; |
| 48 | *emulp = emul; |
| 49 | |
| 50 | return device_probe(emul); |
| 51 | } |
Simon Glass | c70c71d | 2014-12-10 08:55:49 -0700 | [diff] [blame] | 52 | |
| 53 | UCLASS_DRIVER(i2c_emul) = { |
| 54 | .id = UCLASS_I2C_EMUL, |
| 55 | .name = "i2c_emul", |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 56 | .per_device_plat_auto = sizeof(struct i2c_emul_uc_plat), |
Simon Glass | b7c25b1 | 2018-11-18 08:14:33 -0700 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | /* |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 60 | * This uclass is a child of the i2c bus. Its plat is not defined here so |
Simon Glass | b7c25b1 | 2018-11-18 08:14:33 -0700 | [diff] [blame] | 61 | * is defined by its parent, UCLASS_I2C, which uses struct dm_i2c_chip. See |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 62 | * per_child_plat_auto in UCLASS_DRIVER(i2c). |
Simon Glass | b7c25b1 | 2018-11-18 08:14:33 -0700 | [diff] [blame] | 63 | */ |
| 64 | UCLASS_DRIVER(i2c_emul_parent) = { |
| 65 | .id = UCLASS_I2C_EMUL_PARENT, |
| 66 | .name = "i2c_emul_parent", |
Simon Glass | 67507e4 | 2020-10-03 11:31:34 -0600 | [diff] [blame] | 67 | #if !CONFIG_IS_ENABLED(OF_PLATDATA) |
Simon Glass | b7c25b1 | 2018-11-18 08:14:33 -0700 | [diff] [blame] | 68 | .post_bind = dm_scan_fdt_dev, |
Simon Glass | 67507e4 | 2020-10-03 11:31:34 -0600 | [diff] [blame] | 69 | #endif |
Simon Glass | b7c25b1 | 2018-11-18 08:14:33 -0700 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | static const struct udevice_id i2c_emul_parent_ids[] = { |
| 73 | { .compatible = "sandbox,i2c-emul-parent" }, |
| 74 | { } |
| 75 | }; |
| 76 | |
Simon Glass | c4085d7 | 2021-02-03 06:01:17 -0700 | [diff] [blame] | 77 | U_BOOT_DRIVER(sandbox_i2c_emul_parent) = { |
| 78 | .name = "sandbox_i2c_emul_parent", |
Simon Glass | b7c25b1 | 2018-11-18 08:14:33 -0700 | [diff] [blame] | 79 | .id = UCLASS_I2C_EMUL_PARENT, |
| 80 | .of_match = i2c_emul_parent_ids, |
Simon Glass | c70c71d | 2014-12-10 08:55:49 -0700 | [diff] [blame] | 81 | }; |