xref: /qemu/tests/qtest/isl_pmbus_vr-test.c (revision 1c0c52f76208c648a70b115fc99ab558008466da)
1ffcdae67STitus Rwantare /*
2ffcdae67STitus Rwantare  * QTests for the ISL_PMBUS digital voltage regulators
3ffcdae67STitus Rwantare  *
4ffcdae67STitus Rwantare  * Copyright 2021 Google LLC
5ffcdae67STitus Rwantare  *
6ffcdae67STitus Rwantare  * This program is free software; you can redistribute it and/or modify it
7ffcdae67STitus Rwantare  * under the terms of the GNU General Public License as published by the
8ffcdae67STitus Rwantare  * Free Software Foundation; either version 2 of the License, or
9ffcdae67STitus Rwantare  * (at your option) any later version.
10ffcdae67STitus Rwantare  *
11ffcdae67STitus Rwantare  * This program is distributed in the hope that it will be useful, but WITHOUT
12ffcdae67STitus Rwantare  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13ffcdae67STitus Rwantare  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14ffcdae67STitus Rwantare  * for more details.
15ffcdae67STitus Rwantare  */
16ffcdae67STitus Rwantare 
17ffcdae67STitus Rwantare #include "qemu/osdep.h"
18ffcdae67STitus Rwantare #include <math.h>
19ffcdae67STitus Rwantare #include "hw/i2c/pmbus_device.h"
20ffcdae67STitus Rwantare #include "hw/sensor/isl_pmbus_vr.h"
21ffcdae67STitus Rwantare #include "libqtest-single.h"
22ffcdae67STitus Rwantare #include "libqos/qgraph.h"
23ffcdae67STitus Rwantare #include "libqos/i2c.h"
24ffcdae67STitus Rwantare #include "qapi/qmp/qdict.h"
25ffcdae67STitus Rwantare #include "qapi/qmp/qnum.h"
26ffcdae67STitus Rwantare #include "qemu/bitops.h"
27ffcdae67STitus Rwantare 
28ffcdae67STitus Rwantare #define TEST_ID "isl_pmbus_vr-test"
29ffcdae67STitus Rwantare #define TEST_ADDR (0x43)
30ffcdae67STitus Rwantare 
31ffcdae67STitus Rwantare static uint16_t qmp_isl_pmbus_vr_get(const char *id, const char *property)
32ffcdae67STitus Rwantare {
33ffcdae67STitus Rwantare     QDict *response;
34ffcdae67STitus Rwantare     uint64_t ret;
35ffcdae67STitus Rwantare 
36ffcdae67STitus Rwantare     response = qmp("{ 'execute': 'qom-get', 'arguments': { 'path': %s, "
37ffcdae67STitus Rwantare                    "'property': %s } }", id, property);
38ffcdae67STitus Rwantare     g_assert(qdict_haskey(response, "return"));
39ffcdae67STitus Rwantare     ret = qnum_get_uint(qobject_to(QNum, qdict_get(response, "return")));
40ffcdae67STitus Rwantare     qobject_unref(response);
41ffcdae67STitus Rwantare     return ret;
42ffcdae67STitus Rwantare }
43ffcdae67STitus Rwantare 
44ffcdae67STitus Rwantare static void qmp_isl_pmbus_vr_set(const char *id,
45ffcdae67STitus Rwantare                             const char *property,
46ffcdae67STitus Rwantare                             uint16_t value)
47ffcdae67STitus Rwantare {
48ffcdae67STitus Rwantare     QDict *response;
49ffcdae67STitus Rwantare 
50ffcdae67STitus Rwantare     response = qmp("{ 'execute': 'qom-set', 'arguments': { 'path': %s, "
51ffcdae67STitus Rwantare                    "'property': %s, 'value': %u } }", id, property, value);
52ffcdae67STitus Rwantare     g_assert(qdict_haskey(response, "return"));
53ffcdae67STitus Rwantare     qobject_unref(response);
54ffcdae67STitus Rwantare }
55ffcdae67STitus Rwantare 
56ffcdae67STitus Rwantare /* PMBus commands are little endian vs i2c_set16 in i2c.h which is big endian */
57ffcdae67STitus Rwantare static uint16_t isl_pmbus_vr_i2c_get16(QI2CDevice *i2cdev, uint8_t reg)
58ffcdae67STitus Rwantare {
59ffcdae67STitus Rwantare     uint8_t resp[2];
60ffcdae67STitus Rwantare     i2c_read_block(i2cdev, reg, resp, sizeof(resp));
61ffcdae67STitus Rwantare     return (resp[1] << 8) | resp[0];
62ffcdae67STitus Rwantare }
63ffcdae67STitus Rwantare 
64ffcdae67STitus Rwantare /* PMBus commands are little endian vs i2c_set16 in i2c.h which is big endian */
65ffcdae67STitus Rwantare static void isl_pmbus_vr_i2c_set16(QI2CDevice *i2cdev, uint8_t reg,
66ffcdae67STitus Rwantare                                    uint16_t value)
67ffcdae67STitus Rwantare {
68ffcdae67STitus Rwantare     uint8_t data[2];
69ffcdae67STitus Rwantare 
70ffcdae67STitus Rwantare     data[0] = value & 255;
71ffcdae67STitus Rwantare     data[1] = value >> 8;
72ffcdae67STitus Rwantare     i2c_write_block(i2cdev, reg, data, sizeof(data));
73ffcdae67STitus Rwantare }
74ffcdae67STitus Rwantare 
75ffcdae67STitus Rwantare static void test_defaults(void *obj, void *data, QGuestAllocator *alloc)
76ffcdae67STitus Rwantare {
77ffcdae67STitus Rwantare     uint16_t value, i2c_value;
78ffcdae67STitus Rwantare     QI2CDevice *i2cdev = (QI2CDevice *)obj;
79ffcdae67STitus Rwantare 
80ffcdae67STitus Rwantare     value = qmp_isl_pmbus_vr_get(TEST_ID, "vout[0]");
81ffcdae67STitus Rwantare     g_assert_cmpuint(value, ==, ISL_READ_VOUT_DEFAULT);
82ffcdae67STitus Rwantare 
83ffcdae67STitus Rwantare     i2c_value = isl_pmbus_vr_i2c_get16(i2cdev, PMBUS_READ_IOUT);
84ffcdae67STitus Rwantare     g_assert_cmpuint(i2c_value, ==, ISL_READ_IOUT_DEFAULT);
85ffcdae67STitus Rwantare 
86ffcdae67STitus Rwantare     value = qmp_isl_pmbus_vr_get(TEST_ID, "pout[0]");
87ffcdae67STitus Rwantare     g_assert_cmpuint(value, ==, ISL_READ_POUT_DEFAULT);
88ffcdae67STitus Rwantare 
89ffcdae67STitus Rwantare     i2c_value = isl_pmbus_vr_i2c_get16(i2cdev, PMBUS_READ_VIN);
90ffcdae67STitus Rwantare     g_assert_cmpuint(i2c_value, ==, ISL_READ_VIN_DEFAULT);
91ffcdae67STitus Rwantare 
92ffcdae67STitus Rwantare     value = qmp_isl_pmbus_vr_get(TEST_ID, "iin[0]");
93ffcdae67STitus Rwantare     g_assert_cmpuint(value, ==, ISL_READ_IIN_DEFAULT);
94ffcdae67STitus Rwantare 
95ffcdae67STitus Rwantare     i2c_value = isl_pmbus_vr_i2c_get16(i2cdev, PMBUS_READ_PIN);
96ffcdae67STitus Rwantare     g_assert_cmpuint(i2c_value, ==, ISL_READ_PIN_DEFAULT);
97ffcdae67STitus Rwantare 
98ffcdae67STitus Rwantare     value = qmp_isl_pmbus_vr_get(TEST_ID, "temp1[0]");
99ffcdae67STitus Rwantare     g_assert_cmpuint(value, ==, ISL_READ_TEMP_DEFAULT);
100ffcdae67STitus Rwantare 
101ffcdae67STitus Rwantare     i2c_value = isl_pmbus_vr_i2c_get16(i2cdev, PMBUS_READ_TEMPERATURE_2);
102ffcdae67STitus Rwantare     g_assert_cmpuint(i2c_value, ==, ISL_READ_TEMP_DEFAULT);
103ffcdae67STitus Rwantare 
104ffcdae67STitus Rwantare     i2c_value = i2c_get8(i2cdev, PMBUS_CAPABILITY);
105ffcdae67STitus Rwantare     g_assert_cmphex(i2c_value, ==, ISL_CAPABILITY_DEFAULT);
106ffcdae67STitus Rwantare 
107ffcdae67STitus Rwantare     i2c_value = i2c_get8(i2cdev, PMBUS_OPERATION);
108ffcdae67STitus Rwantare     g_assert_cmphex(i2c_value, ==, ISL_OPERATION_DEFAULT);
109ffcdae67STitus Rwantare 
110ffcdae67STitus Rwantare     i2c_value = i2c_get8(i2cdev, PMBUS_ON_OFF_CONFIG);
111ffcdae67STitus Rwantare     g_assert_cmphex(i2c_value, ==, ISL_ON_OFF_CONFIG_DEFAULT);
112ffcdae67STitus Rwantare 
113ffcdae67STitus Rwantare     i2c_value = i2c_get8(i2cdev, PMBUS_VOUT_MODE);
114ffcdae67STitus Rwantare     g_assert_cmphex(i2c_value, ==, ISL_VOUT_MODE_DEFAULT);
115ffcdae67STitus Rwantare 
116ffcdae67STitus Rwantare     i2c_value = isl_pmbus_vr_i2c_get16(i2cdev, PMBUS_VOUT_COMMAND);
117ffcdae67STitus Rwantare     g_assert_cmphex(i2c_value, ==, ISL_VOUT_COMMAND_DEFAULT);
118ffcdae67STitus Rwantare 
119ffcdae67STitus Rwantare     i2c_value = isl_pmbus_vr_i2c_get16(i2cdev, PMBUS_VOUT_MAX);
120ffcdae67STitus Rwantare     g_assert_cmphex(i2c_value, ==, ISL_VOUT_MAX_DEFAULT);
121ffcdae67STitus Rwantare 
122ffcdae67STitus Rwantare     i2c_value = isl_pmbus_vr_i2c_get16(i2cdev, PMBUS_VOUT_MARGIN_HIGH);
123ffcdae67STitus Rwantare     g_assert_cmphex(i2c_value, ==, ISL_VOUT_MARGIN_HIGH_DEFAULT);
124ffcdae67STitus Rwantare 
125ffcdae67STitus Rwantare     i2c_value = isl_pmbus_vr_i2c_get16(i2cdev, PMBUS_VOUT_MARGIN_LOW);
126ffcdae67STitus Rwantare     g_assert_cmphex(i2c_value, ==, ISL_VOUT_MARGIN_LOW_DEFAULT);
127ffcdae67STitus Rwantare 
128ffcdae67STitus Rwantare     i2c_value = isl_pmbus_vr_i2c_get16(i2cdev, PMBUS_VOUT_TRANSITION_RATE);
129ffcdae67STitus Rwantare     g_assert_cmphex(i2c_value, ==, ISL_VOUT_TRANSITION_RATE_DEFAULT);
130ffcdae67STitus Rwantare 
131ffcdae67STitus Rwantare     i2c_value = isl_pmbus_vr_i2c_get16(i2cdev, PMBUS_VOUT_OV_FAULT_LIMIT);
132ffcdae67STitus Rwantare     g_assert_cmphex(i2c_value, ==, ISL_VOUT_OV_FAULT_LIMIT_DEFAULT);
133ffcdae67STitus Rwantare 
134ffcdae67STitus Rwantare     i2c_value = isl_pmbus_vr_i2c_get16(i2cdev, PMBUS_OT_FAULT_LIMIT);
135ffcdae67STitus Rwantare     g_assert_cmphex(i2c_value, ==, ISL_OT_FAULT_LIMIT_DEFAULT);
136ffcdae67STitus Rwantare 
137ffcdae67STitus Rwantare     i2c_value = isl_pmbus_vr_i2c_get16(i2cdev, PMBUS_OT_WARN_LIMIT);
138ffcdae67STitus Rwantare     g_assert_cmphex(i2c_value, ==, ISL_OT_WARN_LIMIT_DEFAULT);
139ffcdae67STitus Rwantare 
140ffcdae67STitus Rwantare     i2c_value = isl_pmbus_vr_i2c_get16(i2cdev, PMBUS_VIN_OV_WARN_LIMIT);
141ffcdae67STitus Rwantare     g_assert_cmphex(i2c_value, ==, ISL_VIN_OV_WARN_LIMIT_DEFAULT);
142ffcdae67STitus Rwantare 
143ffcdae67STitus Rwantare     i2c_value = isl_pmbus_vr_i2c_get16(i2cdev, PMBUS_VIN_UV_WARN_LIMIT);
144ffcdae67STitus Rwantare     g_assert_cmphex(i2c_value, ==, ISL_VIN_UV_WARN_LIMIT_DEFAULT);
145ffcdae67STitus Rwantare 
146ffcdae67STitus Rwantare     i2c_value = isl_pmbus_vr_i2c_get16(i2cdev, PMBUS_IIN_OC_FAULT_LIMIT);
147ffcdae67STitus Rwantare     g_assert_cmphex(i2c_value, ==, ISL_IIN_OC_FAULT_LIMIT_DEFAULT);
148ffcdae67STitus Rwantare 
149ffcdae67STitus Rwantare     i2c_value = i2c_get8(i2cdev, PMBUS_REVISION);
150ffcdae67STitus Rwantare     g_assert_cmphex(i2c_value, ==, ISL_REVISION_DEFAULT);
151ffcdae67STitus Rwantare }
152ffcdae67STitus Rwantare 
153ffcdae67STitus Rwantare /* test qmp access */
154ffcdae67STitus Rwantare static void test_tx_rx(void *obj, void *data, QGuestAllocator *alloc)
155ffcdae67STitus Rwantare {
156ffcdae67STitus Rwantare     uint16_t i2c_value, value;
157ffcdae67STitus Rwantare     QI2CDevice *i2cdev = (QI2CDevice *)obj;
158ffcdae67STitus Rwantare 
159ffcdae67STitus Rwantare     qmp_isl_pmbus_vr_set(TEST_ID, "vin[0]", 200);
160ffcdae67STitus Rwantare     value = qmp_isl_pmbus_vr_get(TEST_ID, "vin[0]");
161ffcdae67STitus Rwantare     i2c_value = isl_pmbus_vr_i2c_get16(i2cdev, PMBUS_READ_VIN);
162ffcdae67STitus Rwantare     g_assert_cmpuint(value, ==, i2c_value);
163ffcdae67STitus Rwantare 
164ffcdae67STitus Rwantare     qmp_isl_pmbus_vr_set(TEST_ID, "vout[0]", 2500);
165ffcdae67STitus Rwantare     value = qmp_isl_pmbus_vr_get(TEST_ID, "vout[0]");
166ffcdae67STitus Rwantare     i2c_value = isl_pmbus_vr_i2c_get16(i2cdev, PMBUS_READ_VOUT);
167ffcdae67STitus Rwantare     g_assert_cmpuint(value, ==, i2c_value);
168ffcdae67STitus Rwantare 
169ffcdae67STitus Rwantare     qmp_isl_pmbus_vr_set(TEST_ID, "iin[0]", 300);
170ffcdae67STitus Rwantare     value = qmp_isl_pmbus_vr_get(TEST_ID, "iin[0]");
171ffcdae67STitus Rwantare     i2c_value = isl_pmbus_vr_i2c_get16(i2cdev, PMBUS_READ_IIN);
172ffcdae67STitus Rwantare     g_assert_cmpuint(value, ==, i2c_value);
173ffcdae67STitus Rwantare 
174ffcdae67STitus Rwantare     qmp_isl_pmbus_vr_set(TEST_ID, "iout[0]", 310);
175ffcdae67STitus Rwantare     value = qmp_isl_pmbus_vr_get(TEST_ID, "iout[0]");
176ffcdae67STitus Rwantare     i2c_value = isl_pmbus_vr_i2c_get16(i2cdev, PMBUS_READ_IOUT);
177ffcdae67STitus Rwantare     g_assert_cmpuint(value, ==, i2c_value);
178ffcdae67STitus Rwantare 
179ffcdae67STitus Rwantare     qmp_isl_pmbus_vr_set(TEST_ID, "pin[0]", 100);
180ffcdae67STitus Rwantare     value = qmp_isl_pmbus_vr_get(TEST_ID, "pin[0]");
181ffcdae67STitus Rwantare     i2c_value = isl_pmbus_vr_i2c_get16(i2cdev, PMBUS_READ_PIN);
182ffcdae67STitus Rwantare     g_assert_cmpuint(value, ==, i2c_value);
183ffcdae67STitus Rwantare 
184ffcdae67STitus Rwantare     qmp_isl_pmbus_vr_set(TEST_ID, "pout[0]", 95);
185ffcdae67STitus Rwantare     value = qmp_isl_pmbus_vr_get(TEST_ID, "pout[0]");
186ffcdae67STitus Rwantare     i2c_value = isl_pmbus_vr_i2c_get16(i2cdev, PMBUS_READ_POUT);
187ffcdae67STitus Rwantare     g_assert_cmpuint(value, ==, i2c_value);
188ffcdae67STitus Rwantare 
189ffcdae67STitus Rwantare     qmp_isl_pmbus_vr_set(TEST_ID, "temp1[0]", 26);
190ffcdae67STitus Rwantare     value = qmp_isl_pmbus_vr_get(TEST_ID, "temp1[0]");
191ffcdae67STitus Rwantare     i2c_value = isl_pmbus_vr_i2c_get16(i2cdev, PMBUS_READ_TEMPERATURE_1);
192ffcdae67STitus Rwantare     g_assert_cmpuint(value, ==, i2c_value);
193ffcdae67STitus Rwantare 
194ffcdae67STitus Rwantare     qmp_isl_pmbus_vr_set(TEST_ID, "temp2[0]", 27);
195ffcdae67STitus Rwantare     value = qmp_isl_pmbus_vr_get(TEST_ID, "temp2[0]");
196ffcdae67STitus Rwantare     i2c_value = isl_pmbus_vr_i2c_get16(i2cdev, PMBUS_READ_TEMPERATURE_2);
197ffcdae67STitus Rwantare     g_assert_cmpuint(value, ==, i2c_value);
198ffcdae67STitus Rwantare 
199ffcdae67STitus Rwantare     qmp_isl_pmbus_vr_set(TEST_ID, "temp3[0]", 28);
200ffcdae67STitus Rwantare     value = qmp_isl_pmbus_vr_get(TEST_ID, "temp3[0]");
201ffcdae67STitus Rwantare     i2c_value = isl_pmbus_vr_i2c_get16(i2cdev, PMBUS_READ_TEMPERATURE_3);
202ffcdae67STitus Rwantare     g_assert_cmpuint(value, ==, i2c_value);
203ffcdae67STitus Rwantare 
204ffcdae67STitus Rwantare }
205ffcdae67STitus Rwantare 
206ffcdae67STitus Rwantare /* test r/w registers */
207ffcdae67STitus Rwantare static void test_rw_regs(void *obj, void *data, QGuestAllocator *alloc)
208ffcdae67STitus Rwantare {
209ffcdae67STitus Rwantare     uint16_t i2c_value;
210ffcdae67STitus Rwantare     QI2CDevice *i2cdev = (QI2CDevice *)obj;
211ffcdae67STitus Rwantare 
212ffcdae67STitus Rwantare     isl_pmbus_vr_i2c_set16(i2cdev, PMBUS_VOUT_COMMAND, 0x1234);
213ffcdae67STitus Rwantare     i2c_value = isl_pmbus_vr_i2c_get16(i2cdev, PMBUS_VOUT_COMMAND);
214ffcdae67STitus Rwantare     g_assert_cmphex(i2c_value, ==, 0x1234);
215ffcdae67STitus Rwantare 
216ffcdae67STitus Rwantare     isl_pmbus_vr_i2c_set16(i2cdev, PMBUS_VOUT_TRIM, 0x4567);
217ffcdae67STitus Rwantare     i2c_value = isl_pmbus_vr_i2c_get16(i2cdev, PMBUS_VOUT_TRIM);
218ffcdae67STitus Rwantare     g_assert_cmphex(i2c_value, ==, 0x4567);
219ffcdae67STitus Rwantare 
220ffcdae67STitus Rwantare     isl_pmbus_vr_i2c_set16(i2cdev, PMBUS_VOUT_MAX, 0x9876);
221ffcdae67STitus Rwantare     i2c_value = isl_pmbus_vr_i2c_get16(i2cdev, PMBUS_VOUT_MAX);
222ffcdae67STitus Rwantare     g_assert_cmphex(i2c_value, ==, 0x9876);
223ffcdae67STitus Rwantare 
224ffcdae67STitus Rwantare     isl_pmbus_vr_i2c_set16(i2cdev, PMBUS_VOUT_MARGIN_HIGH, 0xABCD);
225ffcdae67STitus Rwantare     i2c_value = isl_pmbus_vr_i2c_get16(i2cdev, PMBUS_VOUT_MARGIN_HIGH);
226ffcdae67STitus Rwantare     g_assert_cmphex(i2c_value, ==, 0xABCD);
227ffcdae67STitus Rwantare 
228ffcdae67STitus Rwantare     isl_pmbus_vr_i2c_set16(i2cdev, PMBUS_VOUT_MARGIN_LOW, 0xA1B2);
229ffcdae67STitus Rwantare     i2c_value = isl_pmbus_vr_i2c_get16(i2cdev, PMBUS_VOUT_MARGIN_LOW);
230ffcdae67STitus Rwantare     g_assert_cmphex(i2c_value, ==, 0xA1B2);
231ffcdae67STitus Rwantare 
232ffcdae67STitus Rwantare     isl_pmbus_vr_i2c_set16(i2cdev, PMBUS_VOUT_TRANSITION_RATE, 0xDEF1);
233ffcdae67STitus Rwantare     i2c_value = isl_pmbus_vr_i2c_get16(i2cdev, PMBUS_VOUT_TRANSITION_RATE);
234ffcdae67STitus Rwantare     g_assert_cmphex(i2c_value, ==, 0xDEF1);
235ffcdae67STitus Rwantare 
236ffcdae67STitus Rwantare     isl_pmbus_vr_i2c_set16(i2cdev, PMBUS_VOUT_DROOP, 0x5678);
237ffcdae67STitus Rwantare     i2c_value = isl_pmbus_vr_i2c_get16(i2cdev, PMBUS_VOUT_DROOP);
238ffcdae67STitus Rwantare     g_assert_cmphex(i2c_value, ==, 0x5678);
239ffcdae67STitus Rwantare 
240ffcdae67STitus Rwantare     isl_pmbus_vr_i2c_set16(i2cdev, PMBUS_VOUT_MIN, 0x1234);
241ffcdae67STitus Rwantare     i2c_value = isl_pmbus_vr_i2c_get16(i2cdev, PMBUS_VOUT_MIN);
242ffcdae67STitus Rwantare     g_assert_cmphex(i2c_value, ==, 0x1234);
243ffcdae67STitus Rwantare 
244ffcdae67STitus Rwantare     isl_pmbus_vr_i2c_set16(i2cdev, PMBUS_VOUT_OV_FAULT_LIMIT, 0x2345);
245ffcdae67STitus Rwantare     i2c_value = isl_pmbus_vr_i2c_get16(i2cdev, PMBUS_VOUT_OV_FAULT_LIMIT);
246ffcdae67STitus Rwantare     g_assert_cmphex(i2c_value, ==, 0x2345);
247ffcdae67STitus Rwantare 
248ffcdae67STitus Rwantare     isl_pmbus_vr_i2c_set16(i2cdev, PMBUS_VOUT_UV_FAULT_LIMIT, 0xFA12);
249ffcdae67STitus Rwantare     i2c_value = isl_pmbus_vr_i2c_get16(i2cdev, PMBUS_VOUT_UV_FAULT_LIMIT);
250ffcdae67STitus Rwantare     g_assert_cmphex(i2c_value, ==, 0xFA12);
251ffcdae67STitus Rwantare 
252ffcdae67STitus Rwantare     isl_pmbus_vr_i2c_set16(i2cdev, PMBUS_OT_FAULT_LIMIT, 0xF077);
253ffcdae67STitus Rwantare     i2c_value = isl_pmbus_vr_i2c_get16(i2cdev, PMBUS_OT_FAULT_LIMIT);
254ffcdae67STitus Rwantare     g_assert_cmphex(i2c_value, ==, 0xF077);
255ffcdae67STitus Rwantare 
256ffcdae67STitus Rwantare     isl_pmbus_vr_i2c_set16(i2cdev, PMBUS_OT_WARN_LIMIT, 0x7137);
257ffcdae67STitus Rwantare     i2c_value = isl_pmbus_vr_i2c_get16(i2cdev, PMBUS_OT_WARN_LIMIT);
258ffcdae67STitus Rwantare     g_assert_cmphex(i2c_value, ==, 0x7137);
259ffcdae67STitus Rwantare 
260ffcdae67STitus Rwantare     isl_pmbus_vr_i2c_set16(i2cdev, PMBUS_VIN_OV_FAULT_LIMIT, 0x3456);
261ffcdae67STitus Rwantare     i2c_value = isl_pmbus_vr_i2c_get16(i2cdev, PMBUS_VIN_OV_FAULT_LIMIT);
262ffcdae67STitus Rwantare     g_assert_cmphex(i2c_value, ==, 0x3456);
263ffcdae67STitus Rwantare 
264ffcdae67STitus Rwantare     isl_pmbus_vr_i2c_set16(i2cdev, PMBUS_VIN_UV_FAULT_LIMIT, 0xBADA);
265ffcdae67STitus Rwantare     i2c_value = isl_pmbus_vr_i2c_get16(i2cdev, PMBUS_VIN_UV_FAULT_LIMIT);
266ffcdae67STitus Rwantare     g_assert_cmphex(i2c_value, ==, 0xBADA);
267ffcdae67STitus Rwantare 
268ffcdae67STitus Rwantare     isl_pmbus_vr_i2c_set16(i2cdev, PMBUS_IIN_OC_FAULT_LIMIT, 0xB1B0);
269ffcdae67STitus Rwantare     i2c_value = isl_pmbus_vr_i2c_get16(i2cdev, PMBUS_IIN_OC_FAULT_LIMIT);
270ffcdae67STitus Rwantare     g_assert_cmphex(i2c_value, ==, 0xB1B0);
271ffcdae67STitus Rwantare 
272ffcdae67STitus Rwantare     i2c_set8(i2cdev, PMBUS_OPERATION, 0xA);
273ffcdae67STitus Rwantare     i2c_value = i2c_get8(i2cdev, PMBUS_OPERATION);
274ffcdae67STitus Rwantare     g_assert_cmphex(i2c_value, ==, 0xA);
275ffcdae67STitus Rwantare 
276ffcdae67STitus Rwantare     i2c_set8(i2cdev, PMBUS_ON_OFF_CONFIG, 0x42);
277ffcdae67STitus Rwantare     i2c_value = i2c_get8(i2cdev, PMBUS_ON_OFF_CONFIG);
278ffcdae67STitus Rwantare     g_assert_cmphex(i2c_value, ==, 0x42);
279ffcdae67STitus Rwantare }
280ffcdae67STitus Rwantare 
281ffcdae67STitus Rwantare /* test that devices with multiple pages can switch between them */
282ffcdae67STitus Rwantare static void test_pages_rw(void *obj, void *data, QGuestAllocator *alloc)
283ffcdae67STitus Rwantare {
284ffcdae67STitus Rwantare     uint16_t i2c_value;
285ffcdae67STitus Rwantare     QI2CDevice *i2cdev = (QI2CDevice *)obj;
286ffcdae67STitus Rwantare 
287ffcdae67STitus Rwantare     i2c_set8(i2cdev, PMBUS_PAGE, 1);
288ffcdae67STitus Rwantare     i2c_value = i2c_get8(i2cdev, PMBUS_PAGE);
289ffcdae67STitus Rwantare     g_assert_cmphex(i2c_value, ==, 1);
290ffcdae67STitus Rwantare 
291ffcdae67STitus Rwantare     i2c_set8(i2cdev, PMBUS_PAGE, 0);
292ffcdae67STitus Rwantare     i2c_value = i2c_get8(i2cdev, PMBUS_PAGE);
293ffcdae67STitus Rwantare     g_assert_cmphex(i2c_value, ==, 0);
294ffcdae67STitus Rwantare }
295ffcdae67STitus Rwantare 
296ffcdae67STitus Rwantare /* test read-only registers */
297ffcdae67STitus Rwantare static void test_ro_regs(void *obj, void *data, QGuestAllocator *alloc)
298ffcdae67STitus Rwantare {
299ffcdae67STitus Rwantare     uint16_t i2c_init_value, i2c_value;
300ffcdae67STitus Rwantare     QI2CDevice *i2cdev = (QI2CDevice *)obj;
301ffcdae67STitus Rwantare 
302ffcdae67STitus Rwantare     i2c_init_value = isl_pmbus_vr_i2c_get16(i2cdev, PMBUS_READ_VIN);
303ffcdae67STitus Rwantare     isl_pmbus_vr_i2c_set16(i2cdev, PMBUS_READ_VIN, 0xBEEF);
304ffcdae67STitus Rwantare     i2c_value = isl_pmbus_vr_i2c_get16(i2cdev, PMBUS_READ_VIN);
305ffcdae67STitus Rwantare     g_assert_cmphex(i2c_init_value, ==, i2c_value);
306ffcdae67STitus Rwantare 
307ffcdae67STitus Rwantare     i2c_init_value = isl_pmbus_vr_i2c_get16(i2cdev, PMBUS_READ_IIN);
308ffcdae67STitus Rwantare     isl_pmbus_vr_i2c_set16(i2cdev, PMBUS_READ_IIN, 0xB00F);
309ffcdae67STitus Rwantare     i2c_value = isl_pmbus_vr_i2c_get16(i2cdev, PMBUS_READ_IIN);
310ffcdae67STitus Rwantare     g_assert_cmphex(i2c_init_value, ==, i2c_value);
311ffcdae67STitus Rwantare 
312ffcdae67STitus Rwantare     i2c_init_value = isl_pmbus_vr_i2c_get16(i2cdev, PMBUS_READ_VOUT);
313ffcdae67STitus Rwantare     isl_pmbus_vr_i2c_set16(i2cdev, PMBUS_READ_VOUT, 0x1234);
314ffcdae67STitus Rwantare     i2c_value = isl_pmbus_vr_i2c_get16(i2cdev, PMBUS_READ_VOUT);
315ffcdae67STitus Rwantare     g_assert_cmphex(i2c_init_value, ==, i2c_value);
316ffcdae67STitus Rwantare 
317ffcdae67STitus Rwantare     i2c_init_value = isl_pmbus_vr_i2c_get16(i2cdev, PMBUS_READ_IOUT);
318ffcdae67STitus Rwantare     isl_pmbus_vr_i2c_set16(i2cdev, PMBUS_READ_IOUT, 0x6547);
319ffcdae67STitus Rwantare     i2c_value = isl_pmbus_vr_i2c_get16(i2cdev, PMBUS_READ_IOUT);
320ffcdae67STitus Rwantare     g_assert_cmphex(i2c_init_value, ==, i2c_value);
321ffcdae67STitus Rwantare 
322ffcdae67STitus Rwantare     i2c_init_value = isl_pmbus_vr_i2c_get16(i2cdev, PMBUS_READ_TEMPERATURE_1);
323ffcdae67STitus Rwantare     isl_pmbus_vr_i2c_set16(i2cdev, PMBUS_READ_TEMPERATURE_1, 0x1597);
324ffcdae67STitus Rwantare     i2c_value = isl_pmbus_vr_i2c_get16(i2cdev, PMBUS_READ_TEMPERATURE_1);
325ffcdae67STitus Rwantare     g_assert_cmphex(i2c_init_value, ==, i2c_value);
326ffcdae67STitus Rwantare 
327ffcdae67STitus Rwantare     i2c_init_value = isl_pmbus_vr_i2c_get16(i2cdev, PMBUS_READ_TEMPERATURE_2);
328ffcdae67STitus Rwantare     isl_pmbus_vr_i2c_set16(i2cdev, PMBUS_READ_TEMPERATURE_2, 0x1897);
329ffcdae67STitus Rwantare     i2c_value = isl_pmbus_vr_i2c_get16(i2cdev, PMBUS_READ_TEMPERATURE_2);
330ffcdae67STitus Rwantare     g_assert_cmphex(i2c_init_value, ==, i2c_value);
331ffcdae67STitus Rwantare 
332ffcdae67STitus Rwantare     i2c_init_value = isl_pmbus_vr_i2c_get16(i2cdev, PMBUS_READ_TEMPERATURE_3);
333ffcdae67STitus Rwantare     isl_pmbus_vr_i2c_set16(i2cdev, PMBUS_READ_TEMPERATURE_3, 0x1007);
334ffcdae67STitus Rwantare     i2c_value = isl_pmbus_vr_i2c_get16(i2cdev, PMBUS_READ_TEMPERATURE_3);
335ffcdae67STitus Rwantare     g_assert_cmphex(i2c_init_value, ==, i2c_value);
336ffcdae67STitus Rwantare 
337ffcdae67STitus Rwantare     i2c_init_value = isl_pmbus_vr_i2c_get16(i2cdev, PMBUS_READ_PIN);
338ffcdae67STitus Rwantare     isl_pmbus_vr_i2c_set16(i2cdev, PMBUS_READ_PIN, 0xDEAD);
339ffcdae67STitus Rwantare     i2c_value = isl_pmbus_vr_i2c_get16(i2cdev, PMBUS_READ_PIN);
340ffcdae67STitus Rwantare     g_assert_cmphex(i2c_init_value, ==, i2c_value);
341ffcdae67STitus Rwantare 
342ffcdae67STitus Rwantare     i2c_init_value = isl_pmbus_vr_i2c_get16(i2cdev, PMBUS_READ_POUT);
343ffcdae67STitus Rwantare     isl_pmbus_vr_i2c_set16(i2cdev, PMBUS_READ_POUT, 0xD00D);
344ffcdae67STitus Rwantare     i2c_value = isl_pmbus_vr_i2c_get16(i2cdev, PMBUS_READ_POUT);
345ffcdae67STitus Rwantare     g_assert_cmphex(i2c_init_value, ==, i2c_value);
346ffcdae67STitus Rwantare }
347ffcdae67STitus Rwantare 
348ffcdae67STitus Rwantare /* test voltage fault handling */
349ffcdae67STitus Rwantare static void test_voltage_faults(void *obj, void *data, QGuestAllocator *alloc)
350ffcdae67STitus Rwantare {
351ffcdae67STitus Rwantare     uint16_t i2c_value;
352ffcdae67STitus Rwantare     uint8_t i2c_byte;
353ffcdae67STitus Rwantare     QI2CDevice *i2cdev = (QI2CDevice *)obj;
354ffcdae67STitus Rwantare 
355ffcdae67STitus Rwantare     isl_pmbus_vr_i2c_set16(i2cdev, PMBUS_VOUT_OV_WARN_LIMIT, 5000);
356ffcdae67STitus Rwantare     qmp_isl_pmbus_vr_set(TEST_ID, "vout[0]", 5100);
357ffcdae67STitus Rwantare 
358ffcdae67STitus Rwantare     i2c_value = isl_pmbus_vr_i2c_get16(i2cdev, PMBUS_STATUS_WORD);
359ffcdae67STitus Rwantare     i2c_byte = i2c_get8(i2cdev, PMBUS_STATUS_VOUT);
360ffcdae67STitus Rwantare     g_assert_true((i2c_value & PB_STATUS_VOUT) != 0);
361ffcdae67STitus Rwantare     g_assert_true((i2c_byte & PB_STATUS_VOUT_OV_WARN) != 0);
362ffcdae67STitus Rwantare 
363ffcdae67STitus Rwantare     qmp_isl_pmbus_vr_set(TEST_ID, "vout[0]", 4500);
364ffcdae67STitus Rwantare     i2c_set8(i2cdev, PMBUS_CLEAR_FAULTS, 0);
365ffcdae67STitus Rwantare     i2c_byte = i2c_get8(i2cdev, PMBUS_STATUS_VOUT);
366ffcdae67STitus Rwantare     g_assert_true((i2c_byte & PB_STATUS_VOUT_OV_WARN) == 0);
367ffcdae67STitus Rwantare 
368ffcdae67STitus Rwantare     isl_pmbus_vr_i2c_set16(i2cdev, PMBUS_VOUT_UV_WARN_LIMIT, 4600);
369ffcdae67STitus Rwantare 
370ffcdae67STitus Rwantare     i2c_value = isl_pmbus_vr_i2c_get16(i2cdev, PMBUS_STATUS_WORD);
371ffcdae67STitus Rwantare     i2c_byte = i2c_get8(i2cdev, PMBUS_STATUS_VOUT);
372ffcdae67STitus Rwantare     g_assert_true((i2c_value & PB_STATUS_VOUT) != 0);
373ffcdae67STitus Rwantare     g_assert_true((i2c_byte & PB_STATUS_VOUT_UV_WARN) != 0);
374ffcdae67STitus Rwantare 
375ffcdae67STitus Rwantare }
376ffcdae67STitus Rwantare 
377ffcdae67STitus Rwantare static void isl_pmbus_vr_register_nodes(void)
378ffcdae67STitus Rwantare {
379ffcdae67STitus Rwantare     QOSGraphEdgeOptions opts = {
380ffcdae67STitus Rwantare         .extra_device_opts = "id=" TEST_ID ",address=0x43"
381ffcdae67STitus Rwantare     };
382ffcdae67STitus Rwantare     add_qi2c_address(&opts, &(QI2CAddress) { TEST_ADDR });
383ffcdae67STitus Rwantare 
384ffcdae67STitus Rwantare     qos_node_create_driver("isl69260", i2c_device_create);
385ffcdae67STitus Rwantare     qos_node_consumes("isl69260", "i2c-bus", &opts);
386ffcdae67STitus Rwantare 
387ffcdae67STitus Rwantare     qos_add_test("test_defaults", "isl69260", test_defaults, NULL);
388ffcdae67STitus Rwantare     qos_add_test("test_tx_rx", "isl69260", test_tx_rx, NULL);
389ffcdae67STitus Rwantare     qos_add_test("test_rw_regs", "isl69260", test_rw_regs, NULL);
390ffcdae67STitus Rwantare     qos_add_test("test_pages_rw", "isl69260", test_pages_rw, NULL);
391ffcdae67STitus Rwantare     qos_add_test("test_ro_regs", "isl69260", test_ro_regs, NULL);
392ffcdae67STitus Rwantare     qos_add_test("test_ov_faults", "isl69260", test_voltage_faults, NULL);
393*1c0c52f7STitus Rwantare 
394*1c0c52f7STitus Rwantare     qos_node_create_driver("raa229004", i2c_device_create);
395*1c0c52f7STitus Rwantare     qos_node_consumes("raa229004", "i2c-bus", &opts);
396*1c0c52f7STitus Rwantare 
397*1c0c52f7STitus Rwantare     qos_add_test("test_tx_rx", "raa229004", test_tx_rx, NULL);
398*1c0c52f7STitus Rwantare     qos_add_test("test_rw_regs", "raa229004", test_rw_regs, NULL);
399*1c0c52f7STitus Rwantare     qos_add_test("test_pages_rw", "raa229004", test_pages_rw, NULL);
400*1c0c52f7STitus Rwantare     qos_add_test("test_ov_faults", "raa229004", test_voltage_faults, NULL);
401ffcdae67STitus Rwantare }
402ffcdae67STitus Rwantare libqos_init(isl_pmbus_vr_register_nodes);
403