summaryrefslogtreecommitdiff
path: root/zephyr/test/drivers/default/src/i2c.c
blob: ca27fedbb2de0c05fa631495c27fe405a33f60c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/* 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 <zephyr/kernel.h>
#include <zephyr/ztest.h>

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 =
		BUILD_HOST_COMMAND(EC_CMD_I2C_CONTROL, 0, response, get_params);
	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 =
		BUILD_HOST_COMMAND(EC_CMD_I2C_CONTROL, 0, response, set_params);

	/* Get the speed: 100. */
	zassert_ok(host_command_process(&get_args), NULL);
	zassert_ok(get_args.result, 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(host_command_process(&set_args), NULL);
	zassert_ok(set_args.result, 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(host_command_process(&get_args), NULL);
	zassert_ok(get_args.result, 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(host_command_process(&set_args), NULL);
	zassert_ok(set_args.result, 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(host_command_process(&get_args), NULL);
	zassert_ok(get_args.result, 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(host_command_process(&set_args), NULL);
	zassert_ok(set_args.result, 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,
	};
	struct host_cmd_handler_args set_args =
		BUILD_HOST_COMMAND(EC_CMD_I2C_CONTROL, 0, response, set_params);

	/* Set the speed to 400 on a bus which doesn't support dynamic-speed. */
	zassert_equal(EC_RES_ERROR, host_command_process(&set_args), 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,
	};
	struct host_cmd_handler_args get_args =
		BUILD_HOST_COMMAND(EC_CMD_I2C_CONTROL, 0, response, get_params);

	/* Set the .port=10, which is not defined. */
	zassert_equal(EC_RES_INVALID_PARAM, host_command_process(&get_args),
		      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,
	};
	struct host_cmd_handler_args args =
		BUILD_HOST_COMMAND(EC_CMD_I2C_CONTROL, 0, response, params);

	/* Call the .cmd=10, which is not defined. */
	zassert_equal(EC_RES_INVALID_COMMAND, host_command_process(&args),
		      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,
	};
	struct host_cmd_handler_args set_args =
		BUILD_HOST_COMMAND(EC_CMD_I2C_CONTROL, 0, response, set_params);

	/* Set the speed to 123 KHz (an invalid speed). */
	zassert_equal(EC_RES_INVALID_PARAM, host_command_process(&set_args),
		      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);