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