Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 3834c84 | 2014-12-10 08:55:50 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Simulate an I2C port |
| 4 | * |
| 5 | * Copyright (c) 2014 Google, Inc |
Simon Glass | 3834c84 | 2014-12-10 08:55:50 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
Tom Rini | abb9a04 | 2024-05-18 20:20:43 -0600 | [diff] [blame] | 8 | #include <common.h> |
Simon Glass | 3834c84 | 2014-12-10 08:55:50 -0700 | [diff] [blame] | 9 | #include <dm.h> |
| 10 | #include <errno.h> |
Simon Glass | 3834c84 | 2014-12-10 08:55:50 -0700 | [diff] [blame] | 11 | #include <i2c.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Simon Glass | 4662f61 | 2020-12-19 10:39:54 -0700 | [diff] [blame] | 13 | #include <asm/i2c.h> |
Simon Glass | 3834c84 | 2014-12-10 08:55:50 -0700 | [diff] [blame] | 14 | #include <asm/test.h> |
Simon Glass | 39ab867 | 2020-07-07 13:11:48 -0600 | [diff] [blame] | 15 | #include <dm/acpi.h> |
Simon Glass | 3834c84 | 2014-12-10 08:55:50 -0700 | [diff] [blame] | 16 | #include <dm/lists.h> |
| 17 | #include <dm/device-internal.h> |
Simon Glass | 3834c84 | 2014-12-10 08:55:50 -0700 | [diff] [blame] | 18 | |
Simon Glass | 3834c84 | 2014-12-10 08:55:50 -0700 | [diff] [blame] | 19 | static int get_emul(struct udevice *dev, struct udevice **devp, |
| 20 | struct dm_i2c_ops **opsp) |
| 21 | { |
Simon Glass | 713c3f0 | 2015-01-25 08:27:13 -0700 | [diff] [blame] | 22 | struct dm_i2c_chip *plat; |
Simon Glass | 3834c84 | 2014-12-10 08:55:50 -0700 | [diff] [blame] | 23 | int ret; |
| 24 | |
| 25 | *devp = NULL; |
| 26 | *opsp = NULL; |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 27 | plat = dev_get_parent_plat(dev); |
Simon Glass | 713c3f0 | 2015-01-25 08:27:13 -0700 | [diff] [blame] | 28 | if (!plat->emul) { |
Simon Glass | 17b56f6 | 2018-11-18 08:14:34 -0700 | [diff] [blame] | 29 | ret = i2c_emul_find(dev, &plat->emul); |
Simon Glass | 3834c84 | 2014-12-10 08:55:50 -0700 | [diff] [blame] | 30 | if (ret) |
| 31 | return ret; |
Simon Glass | 3834c84 | 2014-12-10 08:55:50 -0700 | [diff] [blame] | 32 | } |
Simon Glass | 713c3f0 | 2015-01-25 08:27:13 -0700 | [diff] [blame] | 33 | *devp = plat->emul; |
| 34 | *opsp = i2c_get_ops(plat->emul); |
Simon Glass | 3834c84 | 2014-12-10 08:55:50 -0700 | [diff] [blame] | 35 | |
| 36 | return 0; |
| 37 | } |
| 38 | |
Simon Glass | 4c70ed9 | 2015-04-20 12:37:15 -0600 | [diff] [blame] | 39 | void sandbox_i2c_set_test_mode(struct udevice *bus, bool test_mode) |
| 40 | { |
| 41 | struct sandbox_i2c_priv *priv = dev_get_priv(bus); |
| 42 | |
| 43 | priv->test_mode = test_mode; |
| 44 | } |
| 45 | |
Simon Glass | 3834c84 | 2014-12-10 08:55:50 -0700 | [diff] [blame] | 46 | static int sandbox_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, |
| 47 | int nmsgs) |
| 48 | { |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 49 | struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus); |
Simon Glass | 4c70ed9 | 2015-04-20 12:37:15 -0600 | [diff] [blame] | 50 | struct sandbox_i2c_priv *priv = dev_get_priv(bus); |
Simon Glass | 3834c84 | 2014-12-10 08:55:50 -0700 | [diff] [blame] | 51 | struct dm_i2c_ops *ops; |
| 52 | struct udevice *emul, *dev; |
| 53 | bool is_read; |
| 54 | int ret; |
| 55 | |
| 56 | /* Special test code to return success but with no emulation */ |
Simon Glass | 4c70ed9 | 2015-04-20 12:37:15 -0600 | [diff] [blame] | 57 | if (priv->test_mode && msg->addr == SANDBOX_I2C_TEST_ADDR) |
Simon Glass | 3834c84 | 2014-12-10 08:55:50 -0700 | [diff] [blame] | 58 | return 0; |
| 59 | |
Simon Glass | a2723ae | 2015-01-25 08:26:55 -0700 | [diff] [blame] | 60 | ret = i2c_get_chip(bus, msg->addr, 1, &dev); |
Simon Glass | 3834c84 | 2014-12-10 08:55:50 -0700 | [diff] [blame] | 61 | if (ret) |
| 62 | return ret; |
| 63 | |
| 64 | ret = get_emul(dev, &emul, &ops); |
| 65 | if (ret) |
| 66 | return ret; |
| 67 | |
Simon Glass | 4c70ed9 | 2015-04-20 12:37:15 -0600 | [diff] [blame] | 68 | if (priv->test_mode) { |
| 69 | /* |
| 70 | * For testing, don't allow writing above 100KHz for writes and |
| 71 | * 400KHz for reads. |
| 72 | */ |
| 73 | is_read = nmsgs > 1; |
Simon Glass | f0c99c5 | 2020-01-23 11:48:22 -0700 | [diff] [blame] | 74 | if (i2c->speed_hz > (is_read ? I2C_SPEED_FAST_RATE : |
| 75 | I2C_SPEED_STANDARD_RATE)) { |
Simon Glass | 4c70ed9 | 2015-04-20 12:37:15 -0600 | [diff] [blame] | 76 | debug("%s: Max speed exceeded\n", __func__); |
| 77 | return -EINVAL; |
| 78 | } |
Simon Glass | 34d8e95 | 2015-04-20 12:37:13 -0600 | [diff] [blame] | 79 | } |
Simon Glass | 4c70ed9 | 2015-04-20 12:37:15 -0600 | [diff] [blame] | 80 | |
Simon Glass | 3834c84 | 2014-12-10 08:55:50 -0700 | [diff] [blame] | 81 | return ops->xfer(emul, msg, nmsgs); |
| 82 | } |
| 83 | |
| 84 | static const struct dm_i2c_ops sandbox_i2c_ops = { |
| 85 | .xfer = sandbox_i2c_xfer, |
| 86 | }; |
| 87 | |
Simon Glass | 3834c84 | 2014-12-10 08:55:50 -0700 | [diff] [blame] | 88 | static const struct udevice_id sandbox_i2c_ids[] = { |
| 89 | { .compatible = "sandbox,i2c" }, |
| 90 | { } |
| 91 | }; |
| 92 | |
Simon Glass | 4d4558e | 2020-10-03 11:31:36 -0600 | [diff] [blame] | 93 | U_BOOT_DRIVER(sandbox_i2c) = { |
| 94 | .name = "sandbox_i2c", |
Simon Glass | 3834c84 | 2014-12-10 08:55:50 -0700 | [diff] [blame] | 95 | .id = UCLASS_I2C, |
| 96 | .of_match = sandbox_i2c_ids, |
Simon Glass | 3834c84 | 2014-12-10 08:55:50 -0700 | [diff] [blame] | 97 | .ops = &sandbox_i2c_ops, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 98 | .priv_auto = sizeof(struct sandbox_i2c_priv), |
Simon Glass | 3834c84 | 2014-12-10 08:55:50 -0700 | [diff] [blame] | 99 | }; |