]> git.proxmox.com Git - mirror_qemu.git/blame - tests/tmp105-test.c
i386: Update new x86_apicid parsing rules with die_offset support
[mirror_qemu.git] / tests / tmp105-test.c
CommitLineData
6e998903
AF
1/*
2 * QTest testcase for the TMP105 temperature sensor
3 *
4 * Copyright (c) 2012 Andreas Färber
5 *
6 * This work is licensed under the terms of the GNU GPL, version 2 or later.
7 * See the COPYING file in the top-level directory.
8 */
91f32b0c 9
681c28a3 10#include "qemu/osdep.h"
91f32b0c 11
6e998903 12#include "libqtest.h"
93c3fe2a 13#include "libqos/qgraph.h"
cc9936a3 14#include "libqos/i2c.h"
452fcdbc 15#include "qapi/qmp/qdict.h"
0d09e41a 16#include "hw/misc/tmp105_regs.h"
6e998903 17
a4ec5bb7
PB
18#define TMP105_TEST_ID "tmp105-test"
19#define TMP105_TEST_ADDR 0x49
6e998903 20
7373fc76
PB
21static int qmp_tmp105_get_temperature(const char *id)
22{
23 QDict *response;
24 int ret;
cebac614 25
563890c7 26 response = qmp("{ 'execute': 'qom-get', 'arguments': { 'path': %s, "
7373fc76
PB
27 "'property': 'temperature' } }", id);
28 g_assert(qdict_haskey(response, "return"));
29 ret = qdict_get_int(response, "return");
cb3e7f08 30 qobject_unref(response);
7373fc76
PB
31 return ret;
32}
33
34static void qmp_tmp105_set_temperature(const char *id, int value)
35{
36 QDict *response;
37
563890c7 38 response = qmp("{ 'execute': 'qom-set', 'arguments': { 'path': %s, "
7373fc76
PB
39 "'property': 'temperature', 'value': %d } }", id, value);
40 g_assert(qdict_haskey(response, "return"));
cb3e7f08 41 qobject_unref(response);
7373fc76
PB
42}
43
44#define TMP105_PRECISION (1000/16)
93c3fe2a 45static void send_and_receive(void *obj, void *data, QGuestAllocator *alloc)
cebac614
PB
46{
47 uint16_t value;
93c3fe2a 48 QI2CDevice *i2cdev = (QI2CDevice *)obj;
cebac614 49
7373fc76 50 value = qmp_tmp105_get_temperature(TMP105_TEST_ID);
cebac614
PB
51 g_assert_cmpuint(value, ==, 0);
52
06599472 53 value = i2c_get16(i2cdev, TMP105_REG_TEMPERATURE);
7373fc76
PB
54 g_assert_cmphex(value, ==, 0);
55
56 qmp_tmp105_set_temperature(TMP105_TEST_ID, 20000);
57 value = qmp_tmp105_get_temperature(TMP105_TEST_ID);
58 g_assert_cmpuint(value, ==, 20000);
59
06599472 60 value = i2c_get16(i2cdev, TMP105_REG_TEMPERATURE);
7373fc76
PB
61 g_assert_cmphex(value, ==, 0x1400);
62
63 qmp_tmp105_set_temperature(TMP105_TEST_ID, 20938); /* 20 + 15/16 */
64 value = qmp_tmp105_get_temperature(TMP105_TEST_ID);
65 g_assert_cmpuint(value, >=, 20938 - TMP105_PRECISION/2);
66 g_assert_cmpuint(value, <, 20938 + TMP105_PRECISION/2);
67
68 /* Set config */
06599472
PB
69 i2c_set8(i2cdev, TMP105_REG_CONFIG, 0x60);
70 value = i2c_get8(i2cdev, TMP105_REG_CONFIG);
7373fc76
PB
71 g_assert_cmphex(value, ==, 0x60);
72
06599472 73 value = i2c_get16(i2cdev, TMP105_REG_TEMPERATURE);
7373fc76
PB
74 g_assert_cmphex(value, ==, 0x14f0);
75
76 /* Set precision to 9, 10, 11 bits. */
06599472
PB
77 i2c_set8(i2cdev, TMP105_REG_CONFIG, 0x00);
78 g_assert_cmphex(i2c_get8(i2cdev, TMP105_REG_CONFIG), ==, 0x00);
79 value = i2c_get16(i2cdev, TMP105_REG_TEMPERATURE);
7373fc76
PB
80 g_assert_cmphex(value, ==, 0x1480);
81
06599472
PB
82 i2c_set8(i2cdev, TMP105_REG_CONFIG, 0x20);
83 g_assert_cmphex(i2c_get8(i2cdev, TMP105_REG_CONFIG), ==, 0x20);
84 value = i2c_get16(i2cdev, TMP105_REG_TEMPERATURE);
7373fc76
PB
85 g_assert_cmphex(value, ==, 0x14c0);
86
06599472
PB
87 i2c_set8(i2cdev, TMP105_REG_CONFIG, 0x40);
88 g_assert_cmphex(i2c_get8(i2cdev, TMP105_REG_CONFIG), ==, 0x40);
89 value = i2c_get16(i2cdev, TMP105_REG_TEMPERATURE);
7373fc76
PB
90 g_assert_cmphex(value, ==, 0x14e0);
91
92 /* stored precision remains the same */
93 value = qmp_tmp105_get_temperature(TMP105_TEST_ID);
94 g_assert_cmpuint(value, >=, 20938 - TMP105_PRECISION/2);
95 g_assert_cmpuint(value, <, 20938 + TMP105_PRECISION/2);
96
06599472
PB
97 i2c_set8(i2cdev, TMP105_REG_CONFIG, 0x60);
98 g_assert_cmphex(i2c_get8(i2cdev, TMP105_REG_CONFIG), ==, 0x60);
99 value = i2c_get16(i2cdev, TMP105_REG_TEMPERATURE);
7373fc76 100 g_assert_cmphex(value, ==, 0x14f0);
cebac614 101
06599472
PB
102 i2c_set16(i2cdev, TMP105_REG_T_LOW, 0x1234);
103 g_assert_cmphex(i2c_get16(i2cdev, TMP105_REG_T_LOW), ==, 0x1234);
104 i2c_set16(i2cdev, TMP105_REG_T_HIGH, 0x4231);
105 g_assert_cmphex(i2c_get16(i2cdev, TMP105_REG_T_HIGH), ==, 0x4231);
cebac614
PB
106}
107
93c3fe2a 108static void tmp105_register_nodes(void)
6e998903 109{
93c3fe2a
PB
110 QOSGraphEdgeOptions opts = {
111 .extra_device_opts = "id=" TMP105_TEST_ID ",address=0x49"
112 };
06599472 113 add_qi2c_address(&opts, &(QI2CAddress) { 0x49 });
6e998903 114
93c3fe2a
PB
115 qos_node_create_driver("tmp105", i2c_device_create);
116 qos_node_consumes("tmp105", "i2c-bus", &opts);
6e998903 117
93c3fe2a 118 qos_add_test("tx-rx", "tmp105", send_and_receive, NULL);
6e998903 119}
93c3fe2a 120libqos_init(tmp105_register_nodes);