blob: 3678646df52c3faa9689c3118b721d8c7185a5b5 [file] [log] [blame]
Andreas Färber6e998902013-01-16 01:57:57 +01001/*
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 */
Stefan Hajnoczi91f32b02014-02-08 11:41:07 +01009
Peter Maydell681c28a2016-02-08 18:08:51 +000010#include "qemu/osdep.h"
Stefan Hajnoczi91f32b02014-02-08 11:41:07 +010011
Thomas Huthdd210742019-09-03 07:50:26 +020012#include "libqtest-single.h"
Paolo Bonzini93c3fe22019-03-18 14:07:13 +010013#include "libqos/qgraph.h"
Andreas Färbercc9936a2013-05-02 15:56:26 +020014#include "libqos/i2c.h"
Markus Armbruster452fcdb2018-02-01 12:18:39 +010015#include "qapi/qmp/qdict.h"
Corey Minyard5e9ae4b2021-05-18 16:08:03 -050016#include "hw/sensor/tmp105_regs.h"
Andreas Färber6e998902013-01-16 01:57:57 +010017
Paolo Bonzinia4ec5bb2014-03-31 18:26:34 +020018#define TMP105_TEST_ID "tmp105-test"
19#define TMP105_TEST_ADDR 0x49
Andreas Färber6e998902013-01-16 01:57:57 +010020
Paolo Bonzini7373fc72014-03-31 18:26:35 +020021static int qmp_tmp105_get_temperature(const char *id)
22{
23 QDict *response;
24 int ret;
Paolo Bonzinicebac612014-03-31 18:26:33 +020025
Paolo Bonzini563890c2014-06-13 09:35:18 +020026 response = qmp("{ 'execute': 'qom-get', 'arguments': { 'path': %s, "
Paolo Bonzini7373fc72014-03-31 18:26:35 +020027 "'property': 'temperature' } }", id);
28 g_assert(qdict_haskey(response, "return"));
29 ret = qdict_get_int(response, "return");
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +020030 qobject_unref(response);
Paolo Bonzini7373fc72014-03-31 18:26:35 +020031 return ret;
32}
33
34static void qmp_tmp105_set_temperature(const char *id, int value)
35{
36 QDict *response;
37
Paolo Bonzini563890c2014-06-13 09:35:18 +020038 response = qmp("{ 'execute': 'qom-set', 'arguments': { 'path': %s, "
Paolo Bonzini7373fc72014-03-31 18:26:35 +020039 "'property': 'temperature', 'value': %d } }", id, value);
40 g_assert(qdict_haskey(response, "return"));
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +020041 qobject_unref(response);
Paolo Bonzini7373fc72014-03-31 18:26:35 +020042}
43
44#define TMP105_PRECISION (1000/16)
Paolo Bonzini93c3fe22019-03-18 14:07:13 +010045static void send_and_receive(void *obj, void *data, QGuestAllocator *alloc)
Paolo Bonzinicebac612014-03-31 18:26:33 +020046{
47 uint16_t value;
Paolo Bonzini93c3fe22019-03-18 14:07:13 +010048 QI2CDevice *i2cdev = (QI2CDevice *)obj;
Paolo Bonzinicebac612014-03-31 18:26:33 +020049
Paolo Bonzini7373fc72014-03-31 18:26:35 +020050 value = qmp_tmp105_get_temperature(TMP105_TEST_ID);
Paolo Bonzinicebac612014-03-31 18:26:33 +020051 g_assert_cmpuint(value, ==, 0);
52
Paolo Bonzini06599472019-03-18 15:06:50 +010053 value = i2c_get16(i2cdev, TMP105_REG_TEMPERATURE);
Paolo Bonzini7373fc72014-03-31 18:26:35 +020054 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
Paolo Bonzini06599472019-03-18 15:06:50 +010060 value = i2c_get16(i2cdev, TMP105_REG_TEMPERATURE);
Paolo Bonzini7373fc72014-03-31 18:26:35 +020061 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 */
Paolo Bonzini06599472019-03-18 15:06:50 +010069 i2c_set8(i2cdev, TMP105_REG_CONFIG, 0x60);
70 value = i2c_get8(i2cdev, TMP105_REG_CONFIG);
Paolo Bonzini7373fc72014-03-31 18:26:35 +020071 g_assert_cmphex(value, ==, 0x60);
72
Paolo Bonzini06599472019-03-18 15:06:50 +010073 value = i2c_get16(i2cdev, TMP105_REG_TEMPERATURE);
Paolo Bonzini7373fc72014-03-31 18:26:35 +020074 g_assert_cmphex(value, ==, 0x14f0);
75
76 /* Set precision to 9, 10, 11 bits. */
Paolo Bonzini06599472019-03-18 15:06:50 +010077 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);
Paolo Bonzini7373fc72014-03-31 18:26:35 +020080 g_assert_cmphex(value, ==, 0x1480);
81
Paolo Bonzini06599472019-03-18 15:06:50 +010082 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);
Paolo Bonzini7373fc72014-03-31 18:26:35 +020085 g_assert_cmphex(value, ==, 0x14c0);
86
Paolo Bonzini06599472019-03-18 15:06:50 +010087 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);
Paolo Bonzini7373fc72014-03-31 18:26:35 +020090 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
Paolo Bonzini06599472019-03-18 15:06:50 +010097 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);
Paolo Bonzini7373fc72014-03-31 18:26:35 +0200100 g_assert_cmphex(value, ==, 0x14f0);
Paolo Bonzinicebac612014-03-31 18:26:33 +0200101
Paolo Bonzini06599472019-03-18 15:06:50 +0100102 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);
Andreas Färber6e998902013-01-16 01:57:57 +0100106}
107
Paolo Bonzini93c3fe22019-03-18 14:07:13 +0100108static void tmp105_register_nodes(void)
Andreas Färber6e998902013-01-16 01:57:57 +0100109{
Paolo Bonzini93c3fe22019-03-18 14:07:13 +0100110 QOSGraphEdgeOptions opts = {
111 .extra_device_opts = "id=" TMP105_TEST_ID ",address=0x49"
112 };
Paolo Bonzini06599472019-03-18 15:06:50 +0100113 add_qi2c_address(&opts, &(QI2CAddress) { 0x49 });
Andreas Färber6e998902013-01-16 01:57:57 +0100114
Paolo Bonzini93c3fe22019-03-18 14:07:13 +0100115 qos_node_create_driver("tmp105", i2c_device_create);
116 qos_node_consumes("tmp105", "i2c-bus", &opts);
Andreas Färber6e998902013-01-16 01:57:57 +0100117
Paolo Bonzini93c3fe22019-03-18 14:07:13 +0100118 qos_add_test("tx-rx", "tmp105", send_and_receive, NULL);
Andreas Färber6e998902013-01-16 01:57:57 +0100119}
Paolo Bonzini93c3fe22019-03-18 14:07:13 +0100120libqos_init(tmp105_register_nodes);