xref: /qemu/tests/qtest/emc141x-test.c (revision 5e623f2bf1b4a43022c2fd31919c76ddb9556e17)
1*5e623f2bSJohn Wang /*
2*5e623f2bSJohn Wang  * QTest testcase for the EMC141X temperature sensor
3*5e623f2bSJohn Wang  *
4*5e623f2bSJohn Wang  * This work is licensed under the terms of the GNU GPL, version 2 or later.
5*5e623f2bSJohn Wang  * See the COPYING file in the top-level directory.
6*5e623f2bSJohn Wang  */
7*5e623f2bSJohn Wang 
8*5e623f2bSJohn Wang #include "qemu/osdep.h"
9*5e623f2bSJohn Wang 
10*5e623f2bSJohn Wang #include "libqtest-single.h"
11*5e623f2bSJohn Wang #include "libqos/qgraph.h"
12*5e623f2bSJohn Wang #include "libqos/i2c.h"
13*5e623f2bSJohn Wang #include "qapi/qmp/qdict.h"
14*5e623f2bSJohn Wang #include "hw/misc/emc141x_regs.h"
15*5e623f2bSJohn Wang 
16*5e623f2bSJohn Wang #define EMC1414_TEST_ID   "emc1414-test"
17*5e623f2bSJohn Wang 
18*5e623f2bSJohn Wang static int qmp_emc1414_get_temperature(const char *id)
19*5e623f2bSJohn Wang {
20*5e623f2bSJohn Wang     QDict *response;
21*5e623f2bSJohn Wang     int ret;
22*5e623f2bSJohn Wang 
23*5e623f2bSJohn Wang     response = qmp("{ 'execute': 'qom-get', 'arguments': { 'path': %s, "
24*5e623f2bSJohn Wang                    "'property': 'temperature0' } }", id);
25*5e623f2bSJohn Wang     g_assert(qdict_haskey(response, "return"));
26*5e623f2bSJohn Wang     ret = qdict_get_int(response, "return");
27*5e623f2bSJohn Wang     qobject_unref(response);
28*5e623f2bSJohn Wang     return ret;
29*5e623f2bSJohn Wang }
30*5e623f2bSJohn Wang 
31*5e623f2bSJohn Wang static void qmp_emc1414_set_temperature(const char *id, int value)
32*5e623f2bSJohn Wang {
33*5e623f2bSJohn Wang     QDict *response;
34*5e623f2bSJohn Wang 
35*5e623f2bSJohn Wang     response = qmp("{ 'execute': 'qom-set', 'arguments': { 'path': %s, "
36*5e623f2bSJohn Wang                    "'property': 'temperature0', 'value': %d } }", id, value);
37*5e623f2bSJohn Wang     g_assert(qdict_haskey(response, "return"));
38*5e623f2bSJohn Wang     qobject_unref(response);
39*5e623f2bSJohn Wang }
40*5e623f2bSJohn Wang 
41*5e623f2bSJohn Wang static void send_and_receive(void *obj, void *data, QGuestAllocator *alloc)
42*5e623f2bSJohn Wang {
43*5e623f2bSJohn Wang     uint16_t value;
44*5e623f2bSJohn Wang     QI2CDevice *i2cdev = (QI2CDevice *)obj;
45*5e623f2bSJohn Wang 
46*5e623f2bSJohn Wang     value = qmp_emc1414_get_temperature(EMC1414_TEST_ID);
47*5e623f2bSJohn Wang     g_assert_cmpuint(value, ==, 0);
48*5e623f2bSJohn Wang 
49*5e623f2bSJohn Wang     value = i2c_get8(i2cdev, EMC141X_TEMP_HIGH0);
50*5e623f2bSJohn Wang     g_assert_cmphex(value, ==, 0);
51*5e623f2bSJohn Wang 
52*5e623f2bSJohn Wang     /* The default max value is 85C, 0x55=85 */
53*5e623f2bSJohn Wang     value = i2c_get8(i2cdev, EMC141X_TEMP_MAX_HIGH0);
54*5e623f2bSJohn Wang     g_assert_cmphex(value, ==, 0x55);
55*5e623f2bSJohn Wang 
56*5e623f2bSJohn Wang     value = i2c_get8(i2cdev, EMC141X_TEMP_MIN_HIGH0);
57*5e623f2bSJohn Wang     g_assert_cmphex(value, ==, 0);
58*5e623f2bSJohn Wang 
59*5e623f2bSJohn Wang     /* 3000mc = 30C */
60*5e623f2bSJohn Wang     qmp_emc1414_set_temperature(EMC1414_TEST_ID, 30000);
61*5e623f2bSJohn Wang     value = qmp_emc1414_get_temperature(EMC1414_TEST_ID);
62*5e623f2bSJohn Wang     g_assert_cmpuint(value, ==, 30000);
63*5e623f2bSJohn Wang 
64*5e623f2bSJohn Wang     value = i2c_get8(i2cdev, EMC141X_TEMP_HIGH0);
65*5e623f2bSJohn Wang     g_assert_cmphex(value, ==, 30);
66*5e623f2bSJohn Wang 
67*5e623f2bSJohn Wang }
68*5e623f2bSJohn Wang 
69*5e623f2bSJohn Wang static void emc1414_register_nodes(void)
70*5e623f2bSJohn Wang {
71*5e623f2bSJohn Wang     QOSGraphEdgeOptions opts = {
72*5e623f2bSJohn Wang         .extra_device_opts = "id=" EMC1414_TEST_ID ",address=0x70"
73*5e623f2bSJohn Wang     };
74*5e623f2bSJohn Wang     add_qi2c_address(&opts, &(QI2CAddress) { 0x70 });
75*5e623f2bSJohn Wang 
76*5e623f2bSJohn Wang     qos_node_create_driver("emc1414", i2c_device_create);
77*5e623f2bSJohn Wang     qos_node_consumes("emc1414", "i2c-bus", &opts);
78*5e623f2bSJohn Wang 
79*5e623f2bSJohn Wang     qos_add_test("tx-rx", "emc1414", send_and_receive, NULL);
80*5e623f2bSJohn Wang }
81*5e623f2bSJohn Wang libqos_init(emc1414_register_nodes);
82