Skip to content

Commit

Permalink
tests: convert OMAP i2c tests to qgraph
Browse files Browse the repository at this point in the history
This way, pca9952-test and tmp105-test will run for every machine
that exposes an i2c-bus.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
  • Loading branch information
bonzini committed Jun 3, 2019
1 parent 751a7a5 commit 93c3fe2
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 74 deletions.
9 changes: 3 additions & 6 deletions tests/Makefile.include
Expand Up @@ -255,8 +255,6 @@ check-qtest-sparc64-$(CONFIG_ISA_TESTDEV) = tests/endianness-test$(EXESUF)
check-qtest-sparc64-y += tests/prom-env-test$(EXESUF)
check-qtest-sparc64-y += tests/boot-serial-test$(EXESUF)

check-qtest-arm-y += tests/tmp105-test$(EXESUF)
check-qtest-arm-y += tests/pca9552-test$(EXESUF)
check-qtest-arm-y += tests/ds1338-test$(EXESUF)
check-qtest-arm-y += tests/microbit-test$(EXESUF)
check-qtest-arm-y += tests/m25p80-test$(EXESUF)
Expand Down Expand Up @@ -686,7 +684,6 @@ libqos-spapr-obj-y += tests/libqos/pci-spapr.o
libqos-pc-obj-y = $(libqos-obj-y) tests/libqos/pci-pc.o
libqos-pc-obj-y += tests/libqos/malloc-pc.o tests/libqos/libqos-pc.o
libqos-pc-obj-y += tests/libqos/ahci.o
libqos-omap-obj-y = $(libqos-obj-y) tests/libqos/i2c-omap.o
libqos-imx-obj-y = $(libqos-obj-y) tests/libqos/i2c-imx.o
libqos-usb-obj-y = $(libqos-spapr-obj-y) $(libqos-pc-obj-y) tests/libqos/usb.o

Expand All @@ -695,7 +692,7 @@ qos-test-obj-y = tests/qos-test.o $(libqgraph-obj-y)
qos-test-obj-y += $(libqos-pc-obj-y) $(libqos-spapr-obj-y)
qos-test-obj-y += tests/libqos/e1000e.o
qos-test-obj-y += $(libqos-imx-obj-y)
qos-test-obj-y += $(libqos-omap-obj-y)
qos-test-obj-y += tests/libqos/i2c-omap.o
qos-test-obj-y += tests/libqos/sdhci.o
qos-test-obj-y += tests/libqos/tpci200.o
qos-test-obj-y += tests/libqos/virtio.o
Expand Down Expand Up @@ -731,10 +728,12 @@ qos-test-obj-y += tests/ipoctal232-test.o
qos-test-obj-y += tests/megasas-test.o
qos-test-obj-y += tests/ne2000-test.o
qos-test-obj-y += tests/nvme-test.o
qos-test-obj-y += tests/pca9552-test.o
qos-test-obj-y += tests/pci-test.o
qos-test-obj-y += tests/pcnet-test.o
qos-test-obj-y += tests/sdhci-test.o
qos-test-obj-y += tests/spapr-phb-test.o
qos-test-obj-y += tests/tmp105-test.o
qos-test-obj-y += tests/usb-hcd-ohci-test.o $(libqos-usb-obj-y)
qos-test-obj-$(CONFIG_VHOST_NET_USER) += tests/vhost-user-test.o $(chardev-obj-y) $(test-io-obj-y)
qos-test-obj-y += tests/virtio-test.o
Expand Down Expand Up @@ -772,8 +771,6 @@ tests/boot-serial-test$(EXESUF): tests/boot-serial-test.o $(libqos-obj-y)
tests/bios-tables-test$(EXESUF): tests/bios-tables-test.o \
tests/boot-sector.o tests/acpi-utils.o $(libqos-obj-y)
tests/pxe-test$(EXESUF): tests/pxe-test.o tests/boot-sector.o $(libqos-obj-y)
tests/tmp105-test$(EXESUF): tests/tmp105-test.o $(libqos-omap-obj-y)
tests/pca9552-test$(EXESUF): tests/pca9552-test.o $(libqos-omap-obj-y)
tests/ds1338-test$(EXESUF): tests/ds1338-test.o $(libqos-imx-obj-y)
tests/microbit-test$(EXESUF): tests/microbit-test.o
tests/m25p80-test$(EXESUF): tests/m25p80-test.o
Expand Down
19 changes: 0 additions & 19 deletions tests/libqos/i2c-omap.c
Expand Up @@ -187,25 +187,6 @@ void omap_i2c_init(OMAPI2C *s, QTestState *qts, uint64_t addr)
s->parent.qts = qts;
}

I2CAdapter *omap_i2c_create(QTestState *qts, uint64_t addr)
{
OMAPI2C *s = g_malloc0(sizeof(*s));

omap_i2c_init(s, qts, addr);
return &s->parent;
}

void omap_i2c_free(I2CAdapter *i2c)
{
OMAPI2C *s;

if (!i2c) {
return;
}
s = container_of(i2c, OMAPI2C, parent);
g_free(s);
}

static void omap_i2c_register_nodes(void)
{
qos_node_create_driver("omap_i2c", NULL);
Expand Down
4 changes: 0 additions & 4 deletions tests/libqos/i2c.h
Expand Up @@ -38,8 +38,6 @@ struct QI2CDevice {
I2CAdapter *bus;
};

#define OMAP2_I2C_1_BASE 0x48070000

void *i2c_device_create(void *i2c_bus, QGuestAllocator *alloc, void *addr);

void i2c_send(I2CAdapter *i2c, uint8_t addr,
Expand Down Expand Up @@ -67,8 +65,6 @@ typedef struct OMAPI2C {
} OMAPI2C;

void omap_i2c_init(OMAPI2C *s, QTestState *qts, uint64_t addr);
I2CAdapter *omap_i2c_create(QTestState *qts, uint64_t addr);
void omap_i2c_free(I2CAdapter *i2c);

/* i2c-imx.c */
typedef struct IMXI2C {
Expand Down
44 changes: 19 additions & 25 deletions tests/pca9552-test.c
Expand Up @@ -10,23 +10,26 @@
#include "qemu/osdep.h"

#include "libqtest.h"
#include "libqos/qgraph.h"
#include "libqos/i2c.h"
#include "hw/misc/pca9552_regs.h"

#define PCA9552_TEST_ID "pca9552-test"
#define PCA9552_TEST_ADDR 0x60

static I2CAdapter *i2c;

static void pca9552_init(I2CAdapter *i2c)
static void pca9552_init(QI2CDevice *i2cdev)
{
I2CAdapter *i2c = i2cdev->bus;

/* Switch on LEDs 0 and 12 */
i2c_set8(i2c, PCA9552_TEST_ADDR, PCA9552_LS0, 0x54);
i2c_set8(i2c, PCA9552_TEST_ADDR, PCA9552_LS3, 0x54);
}

static void receive_autoinc(void)
static void receive_autoinc(void *obj, void *data, QGuestAllocator *alloc)
{
QI2CDevice *i2cdev = (QI2CDevice *)obj;
I2CAdapter *i2c = i2cdev->bus;
uint8_t resp;
uint8_t reg = PCA9552_LS0 | PCA9552_AUTOINC;

Expand All @@ -51,8 +54,10 @@ static void receive_autoinc(void)
g_assert_cmphex(resp, ==, 0x54);
}

static void send_and_receive(void)
static void send_and_receive(void *obj, void *data, QGuestAllocator *alloc)
{
QI2CDevice *i2cdev = (QI2CDevice *)obj;
I2CAdapter *i2c = i2cdev->bus;
uint8_t value;

value = i2c_get8(i2c, PCA9552_TEST_ADDR, PCA9552_LS0);
Expand All @@ -76,27 +81,16 @@ static void send_and_receive(void)
g_assert_cmphex(value, ==, 0x10);
}

int main(int argc, char **argv)
static void pca9552_register_nodes(void)
{
QTestState *s = NULL;
int ret;

g_test_init(&argc, &argv, NULL);

s = qtest_start("-machine n800 "
"-device pca9552,bus=i2c-bus.0,id=" PCA9552_TEST_ID
",address=0x60");
i2c = omap_i2c_create(s, OMAP2_I2C_1_BASE);

qtest_add_func("/pca9552/tx-rx", send_and_receive);
qtest_add_func("/pca9552/rx-autoinc", receive_autoinc);

ret = g_test_run();
QOSGraphEdgeOptions opts = {
.extra_device_opts = "address=0x60"
};

if (s) {
qtest_quit(s);
}
omap_i2c_free(i2c);
qos_node_create_driver("pca9552", i2c_device_create);
qos_node_consumes("pca9552", "i2c-bus", &opts);

return ret;
qos_add_test("tx-rx", "pca9552", send_and_receive, NULL);
qos_add_test("rx-autoinc", "pca9552", receive_autoinc, NULL);
}
libqos_init(pca9552_register_nodes);
32 changes: 12 additions & 20 deletions tests/tmp105-test.c
Expand Up @@ -10,15 +10,14 @@
#include "qemu/osdep.h"

#include "libqtest.h"
#include "libqos/qgraph.h"
#include "libqos/i2c.h"
#include "qapi/qmp/qdict.h"
#include "hw/misc/tmp105_regs.h"

#define TMP105_TEST_ID "tmp105-test"
#define TMP105_TEST_ADDR 0x49

static I2CAdapter *i2c;

static int qmp_tmp105_get_temperature(const char *id)
{
QDict *response;
Expand All @@ -43,9 +42,11 @@ static void qmp_tmp105_set_temperature(const char *id, int value)
}

#define TMP105_PRECISION (1000/16)
static void send_and_receive(void)
static void send_and_receive(void *obj, void *data, QGuestAllocator *alloc)
{
uint16_t value;
QI2CDevice *i2cdev = (QI2CDevice *)obj;
I2CAdapter *i2c = i2cdev->bus;

value = qmp_tmp105_get_temperature(TMP105_TEST_ID);
g_assert_cmpuint(value, ==, 0);
Expand Down Expand Up @@ -105,24 +106,15 @@ static void send_and_receive(void)
g_assert_cmphex(i2c_get16(i2c, TMP105_TEST_ADDR, TMP105_REG_T_HIGH), ==, 0x4231);
}

int main(int argc, char **argv)
static void tmp105_register_nodes(void)
{
QTestState *s = NULL;
int ret;

g_test_init(&argc, &argv, NULL);

s = qtest_start("-machine n800 "
"-device tmp105,bus=i2c-bus.0,id=" TMP105_TEST_ID
",address=0x49");
i2c = omap_i2c_create(s, OMAP2_I2C_1_BASE);
QOSGraphEdgeOptions opts = {
.extra_device_opts = "id=" TMP105_TEST_ID ",address=0x49"
};

qtest_add_func("/tmp105/tx-rx", send_and_receive);
qos_node_create_driver("tmp105", i2c_device_create);
qos_node_consumes("tmp105", "i2c-bus", &opts);

ret = g_test_run();

qtest_quit(s);
omap_i2c_free(i2c);

return ret;
qos_add_test("tx-rx", "tmp105", send_and_receive, NULL);
}
libqos_init(tmp105_register_nodes);

0 comments on commit 93c3fe2

Please sign in to comment.