From 16c1d44caebe58dbd4af36b603a0c929764873a9 Mon Sep 17 00:00:00 2001 From: Caveh Jalali Date: Thu, 23 Sep 2021 23:58:04 -0700 Subject: common/i2c: Implement i2cspeed console command This adds the "i2cspeed port [speed]" console command. If only the port number is given, then the current port bus speed is reported. With 2 arguments, the port bus speed is changed. Valid speeds are 100, 400, 1000 and the unit is kHz. BRANCH=none BUG=b:201039003 TEST=with follow-on patches, switched I2C bus speed between 400 kHz and 1 MHz. Change-Id: I7ca6b2c7a8fd9abe8e8ec77e4d1702529b297fe8 Signed-off-by: Caveh Jalali Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3181504 Reviewed-by: Keith Short Commit-Queue: Keith Short --- common/i2c_controller.c | 74 +++++++++++++++++++++++++++++++++++++++ include/config.h | 1 + zephyr/Kconfig | 10 ++++++ zephyr/shim/include/config_chip.h | 5 +++ 4 files changed, 90 insertions(+) diff --git a/common/i2c_controller.c b/common/i2c_controller.c index 9fd68aaa2d..59aff3d69e 100644 --- a/common/i2c_controller.c +++ b/common/i2c_controller.c @@ -1724,6 +1724,80 @@ DECLARE_CONSOLE_COMMAND(i2cxfer, command_i2cxfer, "Read write I2C"); #endif +#ifdef CONFIG_CMD_I2C_SPEED + +static const char * const i2c_freq_str[] = { + [I2C_FREQ_1000KHZ] = "1000 kHz", + [I2C_FREQ_400KHZ] = "400 kHz", + [I2C_FREQ_100KHZ] = "100 kHz", + [I2C_FREQ_COUNT] = "unknown", +}; + +BUILD_ASSERT(ARRAY_SIZE(i2c_freq_str) == I2C_FREQ_COUNT + 1); + +static int command_i2c_speed(int argc, char **argv) +{ + int port; + char *e; + enum i2c_freq freq; + enum i2c_freq new_freq = I2C_FREQ_COUNT; + + if (argc < 2 || argc > 3) + return EC_ERROR_PARAM_COUNT; + + port = strtoi(argv[1], &e, 0); + if (*e) + return EC_ERROR_PARAM1; + + if (port < 0 || port >= I2C_PORT_COUNT) + return EC_ERROR_INVAL; + + freq = i2c_get_freq(port); + if (freq < 0 || freq > I2C_FREQ_COUNT) + return EC_ERROR_UNKNOWN; + + if (argc == 3) { + int khz; + int rv; + + khz = strtoi(argv[2], &e, 0); + if (*e) + return EC_ERROR_PARAM2; + + switch (khz) { + case 100: + new_freq = I2C_FREQ_100KHZ; + break; + case 400: + new_freq = I2C_FREQ_400KHZ; + break; + case 1000: + new_freq = I2C_FREQ_1000KHZ; + break; + default: + return EC_ERROR_PARAM2; + } + rv = i2c_set_freq(port, new_freq); + if (rv != EC_SUCCESS) + return rv; + } + + if (new_freq != I2C_FREQ_COUNT) + ccprintf("Port %d speed changed from %s to %s\n", port, + i2c_freq_str[freq], + i2c_freq_str[new_freq]); + else + ccprintf("Port %d speed is %s\n", port, i2c_freq_str[freq]); + + return EC_SUCCESS; +} + +DECLARE_CONSOLE_COMMAND(i2cspeed, command_i2c_speed, + "port [speed in kHz]", + "Get or set I2C port speed"); + +#endif /* CONFIG_CMD_I2C_SPEED */ + #ifdef CONFIG_CMD_I2C_STRESS_TEST static void i2c_test_status(struct i2c_test_results *i2c_test, int test_dev) { diff --git a/include/config.h b/include/config.h index 5c0975c713..2753cc715c 100644 --- a/include/config.h +++ b/include/config.h @@ -1418,6 +1418,7 @@ #undef CONFIG_CMD_I2CWEDGE #undef CONFIG_CMD_I2C_PROTECT #define CONFIG_CMD_I2C_SCAN +#undef CONFIG_CMD_I2C_SPEED #undef CONFIG_CMD_I2C_STRESS_TEST #undef CONFIG_CMD_I2C_STRESS_TEST_ACCEL #undef CONFIG_CMD_I2C_STRESS_TEST_ALS diff --git a/zephyr/Kconfig b/zephyr/Kconfig index 2f4a653fea..07f09ae46e 100644 --- a/zephyr/Kconfig +++ b/zephyr/Kconfig @@ -484,6 +484,16 @@ config PLATFORM_EC_CONSOLE_CMD_I2C_PORTMAP display the mapping of the I2C ports defined by the named-i2c-ports node to the physical port and remote port indexes. +config PLATFORM_EC_CONSOLE_CMD_I2C_SPEED + bool "Console command: i2cspeed" + default n + depends on PLATFORM_EC_I2C + help + Enable the 'i2cspeed' console command. This comamnd is used to + display an I2C port's bus speed. Additionally, for ports with + the DYNAMIC_SPEED port flag set, the speed can be set. In all + cases, the bus speed is in units of kHz. + config PLATFORM_EC_SMBUS_PEC bool "Packet error checking support for SMBus" help diff --git a/zephyr/shim/include/config_chip.h b/zephyr/shim/include/config_chip.h index a19a2e1775..04d2ca1d5d 100644 --- a/zephyr/shim/include/config_chip.h +++ b/zephyr/shim/include/config_chip.h @@ -169,6 +169,11 @@ #define CONFIG_I2C_PASSTHRU_RESTRICTED #endif +#undef CONFIG_CMD_I2C_SPEED +#ifdef CONFIG_PLATFORM_EC_CONSOLE_CMD_I2C_SPEED +#define CONFIG_CMD_I2C_SPEED +#endif + #undef CONFIG_BATTERY_PRESENT_CUSTOM #ifdef CONFIG_PLATFORM_EC_BATTERY_PRESENT_CUSTOM #define CONFIG_BATTERY_PRESENT_CUSTOM -- cgit v1.2.1