Add qemu 2.4.0
[kvmfornfv.git] / qemu / tests / tmp105-test.c
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  */
9
10 #include <glib.h>
11
12 #include "libqtest.h"
13 #include "libqos/i2c.h"
14 #include "hw/misc/tmp105_regs.h"
15
16 #define OMAP2_I2C_1_BASE 0x48070000
17
18 #define TMP105_TEST_ID   "tmp105-test"
19 #define TMP105_TEST_ADDR 0x49
20
21 static I2CAdapter *i2c;
22
23 static uint16_t tmp105_get8(I2CAdapter *i2c, uint8_t addr, uint8_t reg)
24 {
25     uint8_t resp[1];
26     i2c_send(i2c, addr, &reg, 1);
27     i2c_recv(i2c, addr, resp, 1);
28     return resp[0];
29 }
30
31 static uint16_t tmp105_get16(I2CAdapter *i2c, uint8_t addr, uint8_t reg)
32 {
33     uint8_t resp[2];
34     i2c_send(i2c, addr, &reg, 1);
35     i2c_recv(i2c, addr, resp, 2);
36     return (resp[0] << 8) | resp[1];
37 }
38
39 static void tmp105_set8(I2CAdapter *i2c, uint8_t addr, uint8_t reg,
40                         uint8_t value)
41 {
42     uint8_t cmd[2];
43     uint8_t resp[1];
44
45     cmd[0] = reg;
46     cmd[1] = value;
47     i2c_send(i2c, addr, cmd, 2);
48     i2c_recv(i2c, addr, resp, 1);
49     g_assert_cmphex(resp[0], ==, cmd[1]);
50 }
51
52 static void tmp105_set16(I2CAdapter *i2c, uint8_t addr, uint8_t reg,
53                          uint16_t value)
54 {
55     uint8_t cmd[3];
56     uint8_t resp[2];
57
58     cmd[0] = reg;
59     cmd[1] = value >> 8;
60     cmd[2] = value & 255;
61     i2c_send(i2c, addr, cmd, 3);
62     i2c_recv(i2c, addr, resp, 2);
63     g_assert_cmphex(resp[0], ==, cmd[1]);
64     g_assert_cmphex(resp[1], ==, cmd[2]);
65 }
66
67 static int qmp_tmp105_get_temperature(const char *id)
68 {
69     QDict *response;
70     int ret;
71
72     response = qmp("{ 'execute': 'qom-get', 'arguments': { 'path': %s, "
73                    "'property': 'temperature' } }", id);
74     g_assert(qdict_haskey(response, "return"));
75     ret = qdict_get_int(response, "return");
76     QDECREF(response);
77     return ret;
78 }
79
80 static void qmp_tmp105_set_temperature(const char *id, int value)
81 {
82     QDict *response;
83
84     response = qmp("{ 'execute': 'qom-set', 'arguments': { 'path': %s, "
85                    "'property': 'temperature', 'value': %d } }", id, value);
86     g_assert(qdict_haskey(response, "return"));
87     QDECREF(response);
88 }
89
90 #define TMP105_PRECISION (1000/16)
91 static void send_and_receive(void)
92 {
93     uint16_t value;
94
95     value = qmp_tmp105_get_temperature(TMP105_TEST_ID);
96     g_assert_cmpuint(value, ==, 0);
97
98     value = tmp105_get16(i2c, TMP105_TEST_ADDR, TMP105_REG_TEMPERATURE);
99     g_assert_cmphex(value, ==, 0);
100
101     qmp_tmp105_set_temperature(TMP105_TEST_ID, 20000);
102     value = qmp_tmp105_get_temperature(TMP105_TEST_ID);
103     g_assert_cmpuint(value, ==, 20000);
104
105     value = tmp105_get16(i2c, TMP105_TEST_ADDR, TMP105_REG_TEMPERATURE);
106     g_assert_cmphex(value, ==, 0x1400);
107
108     qmp_tmp105_set_temperature(TMP105_TEST_ID, 20938); /* 20 + 15/16 */
109     value = qmp_tmp105_get_temperature(TMP105_TEST_ID);
110     g_assert_cmpuint(value, >=, 20938 - TMP105_PRECISION/2);
111     g_assert_cmpuint(value, <, 20938 + TMP105_PRECISION/2);
112
113     /* Set config */
114     tmp105_set8(i2c, TMP105_TEST_ADDR, TMP105_REG_CONFIG, 0x60);
115     value = tmp105_get8(i2c, TMP105_TEST_ADDR, TMP105_REG_CONFIG);
116     g_assert_cmphex(value, ==, 0x60);
117
118     value = tmp105_get16(i2c, TMP105_TEST_ADDR, TMP105_REG_TEMPERATURE);
119     g_assert_cmphex(value, ==, 0x14f0);
120
121     /* Set precision to 9, 10, 11 bits.  */
122     tmp105_set8(i2c, TMP105_TEST_ADDR, TMP105_REG_CONFIG, 0x00);
123     value = tmp105_get16(i2c, TMP105_TEST_ADDR, TMP105_REG_TEMPERATURE);
124     g_assert_cmphex(value, ==, 0x1480);
125
126     tmp105_set8(i2c, TMP105_TEST_ADDR, TMP105_REG_CONFIG, 0x20);
127     value = tmp105_get16(i2c, TMP105_TEST_ADDR, TMP105_REG_TEMPERATURE);
128     g_assert_cmphex(value, ==, 0x14c0);
129
130     tmp105_set8(i2c, TMP105_TEST_ADDR, TMP105_REG_CONFIG, 0x40);
131     value = tmp105_get16(i2c, TMP105_TEST_ADDR, TMP105_REG_TEMPERATURE);
132     g_assert_cmphex(value, ==, 0x14e0);
133
134     /* stored precision remains the same */
135     value = qmp_tmp105_get_temperature(TMP105_TEST_ID);
136     g_assert_cmpuint(value, >=, 20938 - TMP105_PRECISION/2);
137     g_assert_cmpuint(value, <, 20938 + TMP105_PRECISION/2);
138
139     tmp105_set8(i2c, TMP105_TEST_ADDR, TMP105_REG_CONFIG, 0x60);
140     value = tmp105_get16(i2c, TMP105_TEST_ADDR, TMP105_REG_TEMPERATURE);
141     g_assert_cmphex(value, ==, 0x14f0);
142
143     tmp105_set16(i2c, TMP105_TEST_ADDR, TMP105_REG_T_LOW, 0x1234);
144     tmp105_set16(i2c, TMP105_TEST_ADDR, TMP105_REG_T_HIGH, 0x4231);
145 }
146
147 int main(int argc, char **argv)
148 {
149     QTestState *s = NULL;
150     int ret;
151
152     g_test_init(&argc, &argv, NULL);
153
154     s = qtest_start("-machine n800 "
155                     "-device tmp105,bus=i2c-bus.0,id=" TMP105_TEST_ID
156                     ",address=0x49");
157     i2c = omap_i2c_create(OMAP2_I2C_1_BASE);
158
159     qtest_add_func("/tmp105/tx-rx", send_and_receive);
160
161     ret = g_test_run();
162
163     if (s) {
164         qtest_quit(s);
165     }
166     g_free(i2c);
167
168     return ret;
169 }