/* Copyright 2022 The ChromiumOS Authors * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #include "ec_commands.h" #include "host_command.h" #include "i2c.h" #include "test/drivers/test_state.h" #include #include ZTEST_USER(i2c, test_i2c_set_speed_success) { struct ec_response_i2c_control response; struct ec_params_i2c_control get_params = { .port = I2C_PORT_USB_C0, .cmd = EC_I2C_CONTROL_GET_SPEED, }; struct host_cmd_handler_args get_args; struct ec_params_i2c_control set_params = { .port = I2C_PORT_USB_C0, .cmd = EC_I2C_CONTROL_SET_SPEED, }; struct host_cmd_handler_args set_args; /* Get the speed: 100. */ zassert_ok(ec_cmd_i2c_control(&get_args, &get_params, &response), NULL); zassert_equal(get_args.response_size, sizeof(response), NULL); zassert_equal(response.cmd_response.speed_khz, 100, "response.cmd_response.speed_khz = %d", response.cmd_response.speed_khz); /* Set the speed to 400. */ set_params.cmd_params.speed_khz = 400; zassert_ok(ec_cmd_i2c_control(&set_args, &set_params, &response), NULL); zassert_equal(set_args.response_size, sizeof(response), NULL); zassert_equal(response.cmd_response.speed_khz, 100, "response.cmd_response.speed_khz = %d", response.cmd_response.speed_khz); /* Get the speed to verify. */ zassert_ok(ec_cmd_i2c_control(&get_args, &get_params, &response), NULL); zassert_equal(get_args.response_size, sizeof(response), NULL); zassert_equal(response.cmd_response.speed_khz, 400, "response.cmd_response.speed_khz = %d", response.cmd_response.speed_khz); /* Set the speed to 1000. */ set_params.cmd_params.speed_khz = 1000; zassert_ok(ec_cmd_i2c_control(&set_args, &set_params, &response), NULL); zassert_equal(set_args.response_size, sizeof(response), NULL); zassert_equal(response.cmd_response.speed_khz, 400, "response.cmd_response.speed_khz = %d", response.cmd_response.speed_khz); /* Get the speed to verify. */ zassert_ok(ec_cmd_i2c_control(&get_args, &get_params, &response), NULL); zassert_equal(get_args.response_size, sizeof(response), NULL); zassert_equal(response.cmd_response.speed_khz, 1000, "response.cmd_response.speed_khz = %d", response.cmd_response.speed_khz); /* Set the speed back to 100. */ set_params.cmd_params.speed_khz = 100; zassert_ok(ec_cmd_i2c_control(&set_args, &set_params, &response), NULL); zassert_equal(set_args.response_size, sizeof(response), NULL); zassert_equal(response.cmd_response.speed_khz, 1000, "response.cmd_response.speed_khz = %d", response.cmd_response.speed_khz); } ZTEST_USER(i2c, test_i2c_set_speed_not_dynamic) { struct ec_response_i2c_control response; struct ec_params_i2c_control set_params = { .port = I2C_PORT_POWER, .cmd = EC_I2C_CONTROL_SET_SPEED, .cmd_params.speed_khz = 400, }; /* Set the speed to 400 on a bus which doesn't support dynamic-speed. */ zassert_equal(EC_RES_ERROR, ec_cmd_i2c_control(NULL, &set_params, &response), NULL); } ZTEST_USER(i2c, test_i2c_control_wrong_port) { struct ec_response_i2c_control response; struct ec_params_i2c_control get_params = { .port = 10, .cmd = EC_I2C_CONTROL_GET_SPEED, }; /* Set the .port=10, which is not defined. */ zassert_equal(EC_RES_INVALID_PARAM, ec_cmd_i2c_control(NULL, &get_params, &response), NULL); } ZTEST_USER(i2c, test_i2c_control_wrong_cmd) { struct ec_response_i2c_control response; struct ec_params_i2c_control params = { .port = I2C_PORT_USB_C0, .cmd = 10, }; /* Call the .cmd=10, which is not defined. */ zassert_equal(EC_RES_INVALID_COMMAND, ec_cmd_i2c_control(NULL, ¶ms, &response), NULL); } ZTEST_USER(i2c, test_i2c_set_speed_wrong_freq) { struct ec_response_i2c_control response; struct ec_params_i2c_control set_params = { .port = I2C_PORT_USB_C0, .cmd = EC_I2C_CONTROL_SET_SPEED, .cmd_params.speed_khz = 123, }; /* Set the speed to 123 KHz (an invalid speed). */ zassert_equal(EC_RES_INVALID_PARAM, ec_cmd_i2c_control(NULL, &set_params, &response), NULL); } static void i2c_freq_reset(void) { /* The test modifies this port. Reset it to the DTS defined. */ zassert_ok(i2c_set_freq(I2C_PORT_USB_C0, I2C_FREQ_100KHZ), NULL); } static void *i2c_setup(void) { i2c_freq_reset(); return NULL; } static void i2c_teardown(void *state) { ARG_UNUSED(state); i2c_freq_reset(); } ZTEST_SUITE(i2c, drivers_predicate_post_main, i2c_setup, NULL, NULL, i2c_teardown);