xref: /openbmc/qemu/tests/qtest/emc141x-test.c (revision 9516034d05a8c71ef157a59f525e4c4f7ed79827)
15e623f2bSJohn Wang /*
25e623f2bSJohn Wang  * QTest testcase for the EMC141X temperature sensor
35e623f2bSJohn Wang  *
45e623f2bSJohn Wang  * This work is licensed under the terms of the GNU GPL, version 2 or later.
55e623f2bSJohn Wang  * See the COPYING file in the top-level directory.
65e623f2bSJohn Wang  */
75e623f2bSJohn Wang 
85e623f2bSJohn Wang #include "qemu/osdep.h"
95e623f2bSJohn Wang 
105e623f2bSJohn Wang #include "libqtest-single.h"
115e623f2bSJohn Wang #include "libqos/qgraph.h"
125e623f2bSJohn Wang #include "libqos/i2c.h"
135e623f2bSJohn Wang #include "qapi/qmp/qdict.h"
14*5e9ae4b1SCorey Minyard #include "hw/sensor/emc141x_regs.h"
155e623f2bSJohn Wang 
165e623f2bSJohn Wang #define EMC1414_TEST_ID   "emc1414-test"
175e623f2bSJohn Wang 
qmp_emc1414_get_temperature(const char * id)185e623f2bSJohn Wang static int qmp_emc1414_get_temperature(const char *id)
195e623f2bSJohn Wang {
205e623f2bSJohn Wang     QDict *response;
215e623f2bSJohn Wang     int ret;
225e623f2bSJohn Wang 
235e623f2bSJohn Wang     response = qmp("{ 'execute': 'qom-get', 'arguments': { 'path': %s, "
245e623f2bSJohn Wang                    "'property': 'temperature0' } }", id);
255e623f2bSJohn Wang     g_assert(qdict_haskey(response, "return"));
265e623f2bSJohn Wang     ret = qdict_get_int(response, "return");
275e623f2bSJohn Wang     qobject_unref(response);
285e623f2bSJohn Wang     return ret;
295e623f2bSJohn Wang }
305e623f2bSJohn Wang 
qmp_emc1414_set_temperature(const char * id,int value)315e623f2bSJohn Wang static void qmp_emc1414_set_temperature(const char *id, int value)
325e623f2bSJohn Wang {
335e623f2bSJohn Wang     QDict *response;
345e623f2bSJohn Wang 
355e623f2bSJohn Wang     response = qmp("{ 'execute': 'qom-set', 'arguments': { 'path': %s, "
365e623f2bSJohn Wang                    "'property': 'temperature0', 'value': %d } }", id, value);
375e623f2bSJohn Wang     g_assert(qdict_haskey(response, "return"));
385e623f2bSJohn Wang     qobject_unref(response);
395e623f2bSJohn Wang }
405e623f2bSJohn Wang 
send_and_receive(void * obj,void * data,QGuestAllocator * alloc)415e623f2bSJohn Wang static void send_and_receive(void *obj, void *data, QGuestAllocator *alloc)
425e623f2bSJohn Wang {
435e623f2bSJohn Wang     uint16_t value;
445e623f2bSJohn Wang     QI2CDevice *i2cdev = (QI2CDevice *)obj;
455e623f2bSJohn Wang 
465e623f2bSJohn Wang     value = qmp_emc1414_get_temperature(EMC1414_TEST_ID);
475e623f2bSJohn Wang     g_assert_cmpuint(value, ==, 0);
485e623f2bSJohn Wang 
495e623f2bSJohn Wang     value = i2c_get8(i2cdev, EMC141X_TEMP_HIGH0);
505e623f2bSJohn Wang     g_assert_cmphex(value, ==, 0);
515e623f2bSJohn Wang 
525e623f2bSJohn Wang     /* The default max value is 85C, 0x55=85 */
535e623f2bSJohn Wang     value = i2c_get8(i2cdev, EMC141X_TEMP_MAX_HIGH0);
545e623f2bSJohn Wang     g_assert_cmphex(value, ==, 0x55);
555e623f2bSJohn Wang 
565e623f2bSJohn Wang     value = i2c_get8(i2cdev, EMC141X_TEMP_MIN_HIGH0);
575e623f2bSJohn Wang     g_assert_cmphex(value, ==, 0);
585e623f2bSJohn Wang 
595e623f2bSJohn Wang     /* 3000mc = 30C */
605e623f2bSJohn Wang     qmp_emc1414_set_temperature(EMC1414_TEST_ID, 30000);
615e623f2bSJohn Wang     value = qmp_emc1414_get_temperature(EMC1414_TEST_ID);
625e623f2bSJohn Wang     g_assert_cmpuint(value, ==, 30000);
635e623f2bSJohn Wang 
645e623f2bSJohn Wang     value = i2c_get8(i2cdev, EMC141X_TEMP_HIGH0);
655e623f2bSJohn Wang     g_assert_cmphex(value, ==, 30);
665e623f2bSJohn Wang 
675e623f2bSJohn Wang }
685e623f2bSJohn Wang 
emc1414_register_nodes(void)695e623f2bSJohn Wang static void emc1414_register_nodes(void)
705e623f2bSJohn Wang {
715e623f2bSJohn Wang     QOSGraphEdgeOptions opts = {
725e623f2bSJohn Wang         .extra_device_opts = "id=" EMC1414_TEST_ID ",address=0x70"
735e623f2bSJohn Wang     };
745e623f2bSJohn Wang     add_qi2c_address(&opts, &(QI2CAddress) { 0x70 });
755e623f2bSJohn Wang 
765e623f2bSJohn Wang     qos_node_create_driver("emc1414", i2c_device_create);
775e623f2bSJohn Wang     qos_node_consumes("emc1414", "i2c-bus", &opts);
785e623f2bSJohn Wang 
795e623f2bSJohn Wang     qos_add_test("tx-rx", "emc1414", send_and_receive, NULL);
805e623f2bSJohn Wang }
815e623f2bSJohn Wang libqos_init(emc1414_register_nodes);
82