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