11e8a1faeSThomas Huth /*
21e8a1faeSThomas Huth * QTest testcase for the TMP105 temperature sensor
31e8a1faeSThomas Huth *
41e8a1faeSThomas Huth * Copyright (c) 2012 Andreas Färber
51e8a1faeSThomas Huth *
61e8a1faeSThomas Huth * This work is licensed under the terms of the GNU GPL, version 2 or later.
71e8a1faeSThomas Huth * See the COPYING file in the top-level directory.
81e8a1faeSThomas Huth */
91e8a1faeSThomas Huth
101e8a1faeSThomas Huth #include "qemu/osdep.h"
111e8a1faeSThomas Huth
121e8a1faeSThomas Huth #include "libqtest-single.h"
131e8a1faeSThomas Huth #include "libqos/qgraph.h"
141e8a1faeSThomas Huth #include "libqos/i2c.h"
151e8a1faeSThomas Huth #include "qapi/qmp/qdict.h"
165e9ae4b1SCorey Minyard #include "hw/sensor/tmp105_regs.h"
171e8a1faeSThomas Huth
181e8a1faeSThomas Huth #define TMP105_TEST_ID "tmp105-test"
191e8a1faeSThomas Huth #define TMP105_TEST_ADDR 0x49
201e8a1faeSThomas Huth
qmp_tmp105_get_temperature(const char * id)211e8a1faeSThomas Huth static int qmp_tmp105_get_temperature(const char *id)
221e8a1faeSThomas Huth {
231e8a1faeSThomas Huth QDict *response;
241e8a1faeSThomas Huth int ret;
251e8a1faeSThomas Huth
261e8a1faeSThomas Huth response = qmp("{ 'execute': 'qom-get', 'arguments': { 'path': %s, "
271e8a1faeSThomas Huth "'property': 'temperature' } }", id);
281e8a1faeSThomas Huth g_assert(qdict_haskey(response, "return"));
291e8a1faeSThomas Huth ret = qdict_get_int(response, "return");
301e8a1faeSThomas Huth qobject_unref(response);
311e8a1faeSThomas Huth return ret;
321e8a1faeSThomas Huth }
331e8a1faeSThomas Huth
qmp_tmp105_set_temperature(const char * id,int value)341e8a1faeSThomas Huth static void qmp_tmp105_set_temperature(const char *id, int value)
351e8a1faeSThomas Huth {
361e8a1faeSThomas Huth QDict *response;
371e8a1faeSThomas Huth
381e8a1faeSThomas Huth response = qmp("{ 'execute': 'qom-set', 'arguments': { 'path': %s, "
391e8a1faeSThomas Huth "'property': 'temperature', 'value': %d } }", id, value);
401e8a1faeSThomas Huth g_assert(qdict_haskey(response, "return"));
411e8a1faeSThomas Huth qobject_unref(response);
421e8a1faeSThomas Huth }
431e8a1faeSThomas Huth
441e8a1faeSThomas Huth #define TMP105_PRECISION (1000/16)
send_and_receive(void * obj,void * data,QGuestAllocator * alloc)451e8a1faeSThomas Huth static void send_and_receive(void *obj, void *data, QGuestAllocator *alloc)
461e8a1faeSThomas Huth {
471e8a1faeSThomas Huth uint16_t value;
481e8a1faeSThomas Huth QI2CDevice *i2cdev = (QI2CDevice *)obj;
491e8a1faeSThomas Huth
501e8a1faeSThomas Huth value = qmp_tmp105_get_temperature(TMP105_TEST_ID);
511e8a1faeSThomas Huth g_assert_cmpuint(value, ==, 0);
521e8a1faeSThomas Huth
531e8a1faeSThomas Huth value = i2c_get16(i2cdev, TMP105_REG_TEMPERATURE);
541e8a1faeSThomas Huth g_assert_cmphex(value, ==, 0);
551e8a1faeSThomas Huth
561e8a1faeSThomas Huth qmp_tmp105_set_temperature(TMP105_TEST_ID, 20000);
571e8a1faeSThomas Huth value = qmp_tmp105_get_temperature(TMP105_TEST_ID);
581e8a1faeSThomas Huth g_assert_cmpuint(value, ==, 20000);
591e8a1faeSThomas Huth
601e8a1faeSThomas Huth value = i2c_get16(i2cdev, TMP105_REG_TEMPERATURE);
611e8a1faeSThomas Huth g_assert_cmphex(value, ==, 0x1400);
621e8a1faeSThomas Huth
631e8a1faeSThomas Huth qmp_tmp105_set_temperature(TMP105_TEST_ID, 20938); /* 20 + 15/16 */
641e8a1faeSThomas Huth value = qmp_tmp105_get_temperature(TMP105_TEST_ID);
651e8a1faeSThomas Huth g_assert_cmpuint(value, >=, 20938 - TMP105_PRECISION/2);
661e8a1faeSThomas Huth g_assert_cmpuint(value, <, 20938 + TMP105_PRECISION/2);
671e8a1faeSThomas Huth
681e8a1faeSThomas Huth /* Set config */
691e8a1faeSThomas Huth i2c_set8(i2cdev, TMP105_REG_CONFIG, 0x60);
701e8a1faeSThomas Huth value = i2c_get8(i2cdev, TMP105_REG_CONFIG);
711e8a1faeSThomas Huth g_assert_cmphex(value, ==, 0x60);
721e8a1faeSThomas Huth
731e8a1faeSThomas Huth value = i2c_get16(i2cdev, TMP105_REG_TEMPERATURE);
741e8a1faeSThomas Huth g_assert_cmphex(value, ==, 0x14f0);
751e8a1faeSThomas Huth
761e8a1faeSThomas Huth /* Set precision to 9, 10, 11 bits. */
771e8a1faeSThomas Huth i2c_set8(i2cdev, TMP105_REG_CONFIG, 0x00);
781e8a1faeSThomas Huth g_assert_cmphex(i2c_get8(i2cdev, TMP105_REG_CONFIG), ==, 0x00);
791e8a1faeSThomas Huth value = i2c_get16(i2cdev, TMP105_REG_TEMPERATURE);
801e8a1faeSThomas Huth g_assert_cmphex(value, ==, 0x1480);
811e8a1faeSThomas Huth
821e8a1faeSThomas Huth i2c_set8(i2cdev, TMP105_REG_CONFIG, 0x20);
831e8a1faeSThomas Huth g_assert_cmphex(i2c_get8(i2cdev, TMP105_REG_CONFIG), ==, 0x20);
841e8a1faeSThomas Huth value = i2c_get16(i2cdev, TMP105_REG_TEMPERATURE);
851e8a1faeSThomas Huth g_assert_cmphex(value, ==, 0x14c0);
861e8a1faeSThomas Huth
871e8a1faeSThomas Huth i2c_set8(i2cdev, TMP105_REG_CONFIG, 0x40);
881e8a1faeSThomas Huth g_assert_cmphex(i2c_get8(i2cdev, TMP105_REG_CONFIG), ==, 0x40);
891e8a1faeSThomas Huth value = i2c_get16(i2cdev, TMP105_REG_TEMPERATURE);
901e8a1faeSThomas Huth g_assert_cmphex(value, ==, 0x14e0);
911e8a1faeSThomas Huth
921e8a1faeSThomas Huth /* stored precision remains the same */
931e8a1faeSThomas Huth value = qmp_tmp105_get_temperature(TMP105_TEST_ID);
941e8a1faeSThomas Huth g_assert_cmpuint(value, >=, 20938 - TMP105_PRECISION/2);
951e8a1faeSThomas Huth g_assert_cmpuint(value, <, 20938 + TMP105_PRECISION/2);
961e8a1faeSThomas Huth
971e8a1faeSThomas Huth i2c_set8(i2cdev, TMP105_REG_CONFIG, 0x60);
981e8a1faeSThomas Huth g_assert_cmphex(i2c_get8(i2cdev, TMP105_REG_CONFIG), ==, 0x60);
991e8a1faeSThomas Huth value = i2c_get16(i2cdev, TMP105_REG_TEMPERATURE);
1001e8a1faeSThomas Huth g_assert_cmphex(value, ==, 0x14f0);
1011e8a1faeSThomas Huth
1021e8a1faeSThomas Huth i2c_set16(i2cdev, TMP105_REG_T_LOW, 0x1234);
103*3a0b7588SGuenter Roeck g_assert_cmphex(i2c_get16(i2cdev, TMP105_REG_T_LOW), ==, 0x1230);
1041e8a1faeSThomas Huth i2c_set16(i2cdev, TMP105_REG_T_HIGH, 0x4231);
105*3a0b7588SGuenter Roeck g_assert_cmphex(i2c_get16(i2cdev, TMP105_REG_T_HIGH), ==, 0x4230);
1061e8a1faeSThomas Huth }
1071e8a1faeSThomas Huth
tmp105_register_nodes(void)1081e8a1faeSThomas Huth static void tmp105_register_nodes(void)
1091e8a1faeSThomas Huth {
1101e8a1faeSThomas Huth QOSGraphEdgeOptions opts = {
1111e8a1faeSThomas Huth .extra_device_opts = "id=" TMP105_TEST_ID ",address=0x49"
1121e8a1faeSThomas Huth };
1131e8a1faeSThomas Huth add_qi2c_address(&opts, &(QI2CAddress) { 0x49 });
1141e8a1faeSThomas Huth
1151e8a1faeSThomas Huth qos_node_create_driver("tmp105", i2c_device_create);
1161e8a1faeSThomas Huth qos_node_consumes("tmp105", "i2c-bus", &opts);
1171e8a1faeSThomas Huth
1181e8a1faeSThomas Huth qos_add_test("tx-rx", "tmp105", send_and_receive, NULL);
1191e8a1faeSThomas Huth }
1201e8a1faeSThomas Huth libqos_init(tmp105_register_nodes);
121