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 | |
Patrick Delaunay | b953ec2 | 2021-04-27 11:02:19 +0200 | [diff] [blame] | 6 | #define LOG_CATEGORY UCLASS_I2C_EMUL |
| 7 | |
Simon Glass | c70c71d | 2014-12-10 08:55:49 -0700 | [diff] [blame] | 8 | #include <common.h> |
| 9 | #include <dm.h> |
| 10 | #include <i2c.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Simon Glass | e62ad9c | 2021-03-15 17:25:27 +1300 | [diff] [blame] | 12 | #include <asm/i2c.h> |
Simon Glass | b7c25b1 | 2018-11-18 08:14:33 -0700 | [diff] [blame] | 13 | #include <dm/device-internal.h> |
| 14 | #include <dm/uclass-internal.h> |
| 15 | |
| 16 | /* |
| 17 | * i2c emulation works using an 'emul' node at the bus level. Each device in |
| 18 | * that node is in the UCLASS_I2C_EMUL uclass, and emulates one i2c device. A |
| 19 | * 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] | 20 | * uclass plat (struct i2c_emul_plat), put there by i2c_emul_find(). |
Simon Glass | b7c25b1 | 2018-11-18 08:14:33 -0700 | [diff] [blame] | 21 | * When sandbox wants an emulator for a device, it calls i2c_emul_find() which |
| 22 | * searches for the emulator with the correct address. To find the device for an |
| 23 | * emulator, call i2c_emul_get_device(). |
| 24 | * |
| 25 | * The 'emul' node is in the UCLASS_I2C_EMUL_PARENT uclass. We use a separate |
| 26 | * uclass so avoid having strange devices on the I2C bus. |
| 27 | */ |
| 28 | |
Simon Glass | b7c25b1 | 2018-11-18 08:14:33 -0700 | [diff] [blame] | 29 | struct udevice *i2c_emul_get_device(struct udevice *emul) |
| 30 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 31 | struct i2c_emul_uc_plat *uc_plat = dev_get_uclass_plat(emul); |
Simon Glass | b7c25b1 | 2018-11-18 08:14:33 -0700 | [diff] [blame] | 32 | |
| 33 | return uc_plat->dev; |
| 34 | } |
| 35 | |
Simon Glass | 728d04c | 2021-03-15 17:25:30 +1300 | [diff] [blame] | 36 | void i2c_emul_set_idx(struct udevice *dev, int emul_idx) |
| 37 | { |
| 38 | struct dm_i2c_chip *plat = dev_get_parent_plat(dev); |
| 39 | |
| 40 | plat->emul_idx = emul_idx; |
| 41 | } |
| 42 | |
Simon Glass | b7c25b1 | 2018-11-18 08:14:33 -0700 | [diff] [blame] | 43 | int i2c_emul_find(struct udevice *dev, struct udevice **emulp) |
| 44 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 45 | struct i2c_emul_uc_plat *uc_plat; |
Simon Glass | b7c25b1 | 2018-11-18 08:14:33 -0700 | [diff] [blame] | 46 | struct udevice *emul; |
| 47 | int ret; |
| 48 | |
Simon Glass | 728d04c | 2021-03-15 17:25:30 +1300 | [diff] [blame] | 49 | if (!CONFIG_IS_ENABLED(OF_PLATDATA)) { |
| 50 | ret = uclass_find_device_by_phandle(UCLASS_I2C_EMUL, dev, |
| 51 | "sandbox,emul", &emul); |
| 52 | } else { |
| 53 | struct dm_i2c_chip *plat = dev_get_parent_plat(dev); |
| 54 | |
| 55 | ret = device_get_by_ofplat_idx(plat->emul_idx, &emul); |
| 56 | } |
Simon Glass | b7c25b1 | 2018-11-18 08:14:33 -0700 | [diff] [blame] | 57 | if (ret) { |
| 58 | log_err("No emulators for device '%s'\n", dev->name); |
| 59 | return ret; |
| 60 | } |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 61 | uc_plat = dev_get_uclass_plat(emul); |
Simon Glass | b7c25b1 | 2018-11-18 08:14:33 -0700 | [diff] [blame] | 62 | uc_plat->dev = dev; |
| 63 | *emulp = emul; |
| 64 | |
| 65 | return device_probe(emul); |
| 66 | } |
Simon Glass | c70c71d | 2014-12-10 08:55:49 -0700 | [diff] [blame] | 67 | |
| 68 | UCLASS_DRIVER(i2c_emul) = { |
| 69 | .id = UCLASS_I2C_EMUL, |
| 70 | .name = "i2c_emul", |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 71 | .per_device_plat_auto = sizeof(struct i2c_emul_uc_plat), |
Simon Glass | b7c25b1 | 2018-11-18 08:14:33 -0700 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | /* |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 75 | * 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] | 76 | * 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] | 77 | * per_child_plat_auto in UCLASS_DRIVER(i2c). |
Simon Glass | b7c25b1 | 2018-11-18 08:14:33 -0700 | [diff] [blame] | 78 | */ |
| 79 | UCLASS_DRIVER(i2c_emul_parent) = { |
| 80 | .id = UCLASS_I2C_EMUL_PARENT, |
| 81 | .name = "i2c_emul_parent", |
Simon Glass | 9539738 | 2021-08-07 07:24:04 -0600 | [diff] [blame] | 82 | #if CONFIG_IS_ENABLED(OF_REAL) |
Simon Glass | b7c25b1 | 2018-11-18 08:14:33 -0700 | [diff] [blame] | 83 | .post_bind = dm_scan_fdt_dev, |
Simon Glass | 67507e4 | 2020-10-03 11:31:34 -0600 | [diff] [blame] | 84 | #endif |
Simon Glass | b7c25b1 | 2018-11-18 08:14:33 -0700 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | static const struct udevice_id i2c_emul_parent_ids[] = { |
| 88 | { .compatible = "sandbox,i2c-emul-parent" }, |
| 89 | { } |
| 90 | }; |
| 91 | |
Simon Glass | c4085d7 | 2021-02-03 06:01:17 -0700 | [diff] [blame] | 92 | U_BOOT_DRIVER(sandbox_i2c_emul_parent) = { |
| 93 | .name = "sandbox_i2c_emul_parent", |
Simon Glass | b7c25b1 | 2018-11-18 08:14:33 -0700 | [diff] [blame] | 94 | .id = UCLASS_I2C_EMUL_PARENT, |
| 95 | .of_match = i2c_emul_parent_ids, |
Simon Glass | c70c71d | 2014-12-10 08:55:49 -0700 | [diff] [blame] | 96 | }; |