summaryrefslogtreecommitdiff
path: root/util/comm-i2c.c
diff options
context:
space:
mode:
authorHarry Cutts <hcutts@chromium.org>2019-09-25 11:20:20 -0700
committerCommit Bot <commit-bot@chromium.org>2019-10-04 06:59:27 +0000
commit5f7e4c337dae601208f308efba15ad6d9ffbe788 (patch)
treedd88337d1ede373e381fad8c6c0df91634871890 /util/comm-i2c.c
parent2ee2d6fe8711f51343bb08e0fde599c2643c93e6 (diff)
downloadchrome-ec-5f7e4c337dae601208f308efba15ad6d9ffbe788.tar.gz
util/comm-i2c: Add switch to specify I2C bus
Currently, devices running CrOS EC that aren't actually the embedded controller (such as touchpad or fingerprint MCUs) can only be contacted over I2C if they are in the device tree. To avoid having to recompile the Kernel, Coreboot, or `ectool`, the `--i2c_bus` switch allows `ectool` to be used for testing as-is. BRANCH=none BUG=none TEST=Checked various commands (hello, version, inventory...) with `--i2c_bus=7`, connecting to a MAX32660. Verified that a contradictory `--interface` switch is rejected. Checked that invalid bus numbers (≥32) are rejected. Change-Id: I92f3307bbbdf88978b9f8271610a3ae222279767 Signed-off-by: Harry Cutts <hcutts@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/1828064 Reviewed-by: Daisuke Nojiri <dnojiri@chromium.org>
Diffstat (limited to 'util/comm-i2c.c')
-rw-r--r--util/comm-i2c.c47
1 files changed, 29 insertions, 18 deletions
diff --git a/util/comm-i2c.c b/util/comm-i2c.c
index d01ce3d023..d88454ee3b 100644
--- a/util/comm-i2c.c
+++ b/util/comm-i2c.c
@@ -196,33 +196,44 @@ done:
return ret;
}
-int comm_init_i2c(void)
+int comm_init_i2c(int i2c_bus)
{
char *file_path;
char buffer[64];
int i;
- /* find the device number based on the adapter name */
- for (i = 0; i < I2C_MAX_ADAPTER; i++) {
- FILE *f;
- if (asprintf(&file_path, I2C_ADAPTER_NODE,
- i, i, EC_I2C_ADDR) < 0)
+ if (i2c_bus != -1) {
+ i = i2c_bus;
+
+ if (i >= I2C_MAX_ADAPTER) {
+ fprintf(stderr, "Invalid I2C bus number %d. (The highest possible bus number is %d.)\n",
+ i, I2C_MAX_ADAPTER);
return -1;
- f = fopen(file_path, "r");
- if (f) {
- if (fgets(buffer, sizeof(buffer), f) &&
- !strncmp(buffer, I2C_ADAPTER_NAME, 6)) {
- free(file_path);
+ }
+ } else {
+ /* find the device number based on the adapter name */
+ for (i = 0; i < I2C_MAX_ADAPTER; i++) {
+ FILE *f;
+
+ if (asprintf(&file_path, I2C_ADAPTER_NODE,
+ i, i, EC_I2C_ADDR) < 0)
+ return -1;
+ f = fopen(file_path, "r");
+ if (f) {
+ if (fgets(buffer, sizeof(buffer), f) &&
+ !strncmp(buffer, I2C_ADAPTER_NAME, 6)) {
+ free(file_path);
+ fclose(f);
+ break;
+ }
fclose(f);
- break;
}
- fclose(f);
+ free(file_path);
+ }
+ if (i == I2C_MAX_ADAPTER) {
+ fprintf(stderr, "Cannot find I2C adapter\n");
+ return -1;
}
- free(file_path);
- }
- if (i == I2C_MAX_ADAPTER) {
- fprintf(stderr, "Cannot find I2C adapter\n");
- return -1;
}
if (asprintf(&file_path, I2C_NODE, i) < 0)