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> |
Simon Glass | 3834c84 | 2014-12-10 08:55:50 -0700 | [diff] [blame] | 17 | |
| 18 | DECLARE_GLOBAL_DATA_PTR; |
| 19 | |
Simon Glass | 4c70ed9 | 2015-04-20 12:37:15 -0600 | [diff] [blame] | 20 | struct sandbox_i2c_priv { |
| 21 | bool test_mode; |
Simon Glass | 3834c84 | 2014-12-10 08:55:50 -0700 | [diff] [blame] | 22 | }; |
| 23 | |
| 24 | static int get_emul(struct udevice *dev, struct udevice **devp, |
| 25 | struct dm_i2c_ops **opsp) |
| 26 | { |
Simon Glass | 713c3f0 | 2015-01-25 08:27:13 -0700 | [diff] [blame] | 27 | struct dm_i2c_chip *plat; |
Przemyslaw Marczak | daa4577 | 2015-05-13 13:38:31 +0200 | [diff] [blame] | 28 | struct udevice *child; |
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 | 09128c5 | 2016-07-05 17:10:09 -0600 | [diff] [blame] | 35 | ret = dm_scan_fdt_dev(dev); |
Simon Glass | 3834c84 | 2014-12-10 08:55:50 -0700 | [diff] [blame] | 36 | if (ret) |
| 37 | return ret; |
| 38 | |
Przemyslaw Marczak | daa4577 | 2015-05-13 13:38:31 +0200 | [diff] [blame] | 39 | for (device_find_first_child(dev, &child); child; |
| 40 | device_find_next_child(&child)) { |
| 41 | if (device_get_uclass_id(child) != UCLASS_I2C_EMUL) |
| 42 | continue; |
| 43 | |
| 44 | ret = device_probe(child); |
| 45 | if (ret) |
| 46 | return ret; |
| 47 | |
| 48 | break; |
| 49 | } |
| 50 | |
| 51 | if (child) |
| 52 | plat->emul = child; |
| 53 | else |
| 54 | return -ENODEV; |
Simon Glass | 3834c84 | 2014-12-10 08:55:50 -0700 | [diff] [blame] | 55 | } |
Simon Glass | 713c3f0 | 2015-01-25 08:27:13 -0700 | [diff] [blame] | 56 | *devp = plat->emul; |
| 57 | *opsp = i2c_get_ops(plat->emul); |
Simon Glass | 3834c84 | 2014-12-10 08:55:50 -0700 | [diff] [blame] | 58 | |
| 59 | return 0; |
| 60 | } |
| 61 | |
Simon Glass | 4c70ed9 | 2015-04-20 12:37:15 -0600 | [diff] [blame] | 62 | void sandbox_i2c_set_test_mode(struct udevice *bus, bool test_mode) |
| 63 | { |
| 64 | struct sandbox_i2c_priv *priv = dev_get_priv(bus); |
| 65 | |
| 66 | priv->test_mode = test_mode; |
| 67 | } |
| 68 | |
Simon Glass | 3834c84 | 2014-12-10 08:55:50 -0700 | [diff] [blame] | 69 | static int sandbox_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, |
| 70 | int nmsgs) |
| 71 | { |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 72 | struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus); |
Simon Glass | 4c70ed9 | 2015-04-20 12:37:15 -0600 | [diff] [blame] | 73 | struct sandbox_i2c_priv *priv = dev_get_priv(bus); |
Simon Glass | 3834c84 | 2014-12-10 08:55:50 -0700 | [diff] [blame] | 74 | struct dm_i2c_ops *ops; |
| 75 | struct udevice *emul, *dev; |
| 76 | bool is_read; |
| 77 | int ret; |
| 78 | |
| 79 | /* Special test code to return success but with no emulation */ |
Simon Glass | 4c70ed9 | 2015-04-20 12:37:15 -0600 | [diff] [blame] | 80 | if (priv->test_mode && msg->addr == SANDBOX_I2C_TEST_ADDR) |
Simon Glass | 3834c84 | 2014-12-10 08:55:50 -0700 | [diff] [blame] | 81 | return 0; |
| 82 | |
Simon Glass | a2723ae | 2015-01-25 08:26:55 -0700 | [diff] [blame] | 83 | ret = i2c_get_chip(bus, msg->addr, 1, &dev); |
Simon Glass | 3834c84 | 2014-12-10 08:55:50 -0700 | [diff] [blame] | 84 | if (ret) |
| 85 | return ret; |
| 86 | |
| 87 | ret = get_emul(dev, &emul, &ops); |
| 88 | if (ret) |
| 89 | return ret; |
| 90 | |
Simon Glass | 4c70ed9 | 2015-04-20 12:37:15 -0600 | [diff] [blame] | 91 | if (priv->test_mode) { |
| 92 | /* |
| 93 | * For testing, don't allow writing above 100KHz for writes and |
| 94 | * 400KHz for reads. |
| 95 | */ |
| 96 | is_read = nmsgs > 1; |
| 97 | if (i2c->speed_hz > (is_read ? 400000 : 100000)) { |
| 98 | debug("%s: Max speed exceeded\n", __func__); |
| 99 | return -EINVAL; |
| 100 | } |
Simon Glass | 34d8e95 | 2015-04-20 12:37:13 -0600 | [diff] [blame] | 101 | } |
Simon Glass | 4c70ed9 | 2015-04-20 12:37:15 -0600 | [diff] [blame] | 102 | |
Simon Glass | 3834c84 | 2014-12-10 08:55:50 -0700 | [diff] [blame] | 103 | return ops->xfer(emul, msg, nmsgs); |
| 104 | } |
| 105 | |
| 106 | static const struct dm_i2c_ops sandbox_i2c_ops = { |
| 107 | .xfer = sandbox_i2c_xfer, |
| 108 | }; |
| 109 | |
Simon Glass | 3834c84 | 2014-12-10 08:55:50 -0700 | [diff] [blame] | 110 | static const struct udevice_id sandbox_i2c_ids[] = { |
| 111 | { .compatible = "sandbox,i2c" }, |
| 112 | { } |
| 113 | }; |
| 114 | |
| 115 | U_BOOT_DRIVER(i2c_sandbox) = { |
| 116 | .name = "i2c_sandbox", |
| 117 | .id = UCLASS_I2C, |
| 118 | .of_match = sandbox_i2c_ids, |
Simon Glass | 3834c84 | 2014-12-10 08:55:50 -0700 | [diff] [blame] | 119 | .ops = &sandbox_i2c_ops, |
Simon Glass | 4c70ed9 | 2015-04-20 12:37:15 -0600 | [diff] [blame] | 120 | .priv_auto_alloc_size = sizeof(struct sandbox_i2c_priv), |
Simon Glass | 3834c84 | 2014-12-10 08:55:50 -0700 | [diff] [blame] | 121 | }; |