1
2
3
4
5
6
7
8
9
10#include "qemu/osdep.h"
11
12#include "libqtest.h"
13#include "libqos/qgraph.h"
14#include "libqos/i2c.h"
15#include "hw/misc/pca9552_regs.h"
16
17#define PCA9552_TEST_ID "pca9552-test"
18#define PCA9552_TEST_ADDR 0x60
19
20static void pca9552_init(QI2CDevice *i2cdev)
21{
22
23 i2c_set8(i2cdev, PCA9552_LS0, 0x54);
24 i2c_set8(i2cdev, PCA9552_LS3, 0x54);
25}
26
27static void receive_autoinc(void *obj, void *data, QGuestAllocator *alloc)
28{
29 QI2CDevice *i2cdev = (QI2CDevice *)obj;
30 uint8_t resp;
31 uint8_t reg = PCA9552_LS0 | PCA9552_AUTOINC;
32
33 pca9552_init(i2cdev);
34
35 qi2c_send(i2cdev, ®, 1);
36
37
38 qi2c_recv(i2cdev, &resp, 1);
39 g_assert_cmphex(resp, ==, 0x54);
40
41
42 qi2c_recv(i2cdev, &resp, 1);
43 g_assert_cmphex(resp, ==, 0x55);
44
45
46 qi2c_recv(i2cdev, &resp, 1);
47 g_assert_cmphex(resp, ==, 0x55);
48
49
50 qi2c_recv(i2cdev, &resp, 1);
51 g_assert_cmphex(resp, ==, 0x54);
52}
53
54static void send_and_receive(void *obj, void *data, QGuestAllocator *alloc)
55{
56 QI2CDevice *i2cdev = (QI2CDevice *)obj;
57 uint8_t value;
58
59 value = i2c_get8(i2cdev, PCA9552_LS0);
60 g_assert_cmphex(value, ==, 0x55);
61
62 value = i2c_get8(i2cdev, PCA9552_INPUT0);
63 g_assert_cmphex(value, ==, 0x0);
64
65 pca9552_init(i2cdev);
66
67 value = i2c_get8(i2cdev, PCA9552_LS0);
68 g_assert_cmphex(value, ==, 0x54);
69
70 value = i2c_get8(i2cdev, PCA9552_INPUT0);
71 g_assert_cmphex(value, ==, 0x01);
72
73 value = i2c_get8(i2cdev, PCA9552_LS3);
74 g_assert_cmphex(value, ==, 0x54);
75
76 value = i2c_get8(i2cdev, PCA9552_INPUT1);
77 g_assert_cmphex(value, ==, 0x10);
78}
79
80static void pca9552_register_nodes(void)
81{
82 QOSGraphEdgeOptions opts = {
83 .extra_device_opts = "address=0x60"
84 };
85 add_qi2c_address(&opts, &(QI2CAddress) { 0x60 });
86
87 qos_node_create_driver("pca9552", i2c_device_create);
88 qos_node_consumes("pca9552", "i2c-bus", &opts);
89
90 qos_add_test("tx-rx", "pca9552", send_and_receive, NULL);
91 qos_add_test("rx-autoinc", "pca9552", receive_autoinc, NULL);
92}
93libqos_init(pca9552_register_nodes);
94