xref: /qemu/tests/qtest/pca9552-test.c (revision 93c3fe2a349970aaba8d196b8c2cbcd7c472bb81)
1 /*
2  * QTest testcase for the PCA9552 LED blinker
3  *
4  * Copyright (c) 2017-2018, IBM Corporation.
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 "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 
20 static void pca9552_init(QI2CDevice *i2cdev)
21 {
22     I2CAdapter *i2c = i2cdev->bus;
23 
24     /* Switch on LEDs 0 and 12 */
25     i2c_set8(i2c, PCA9552_TEST_ADDR, PCA9552_LS0, 0x54);
26     i2c_set8(i2c, PCA9552_TEST_ADDR, PCA9552_LS3, 0x54);
27 }
28 
29 static void receive_autoinc(void *obj, void *data, QGuestAllocator *alloc)
30 {
31     QI2CDevice *i2cdev = (QI2CDevice *)obj;
32     I2CAdapter *i2c = i2cdev->bus;
33     uint8_t resp;
34     uint8_t reg = PCA9552_LS0 | PCA9552_AUTOINC;
35 
36     pca9552_init(i2cdev);
37 
38     i2c_send(i2c, PCA9552_TEST_ADDR, &reg, 1);
39 
40     /* PCA9552_LS0 */
41     i2c_recv(i2c, PCA9552_TEST_ADDR, &resp, 1);
42     g_assert_cmphex(resp, ==, 0x54);
43 
44     /* PCA9552_LS1 */
45     i2c_recv(i2c, PCA9552_TEST_ADDR, &resp, 1);
46     g_assert_cmphex(resp, ==, 0x55);
47 
48     /* PCA9552_LS2 */
49     i2c_recv(i2c, PCA9552_TEST_ADDR, &resp, 1);
50     g_assert_cmphex(resp, ==, 0x55);
51 
52     /* PCA9552_LS3 */
53     i2c_recv(i2c, PCA9552_TEST_ADDR, &resp, 1);
54     g_assert_cmphex(resp, ==, 0x54);
55 }
56 
57 static void send_and_receive(void *obj, void *data, QGuestAllocator *alloc)
58 {
59     QI2CDevice *i2cdev = (QI2CDevice *)obj;
60     I2CAdapter *i2c = i2cdev->bus;
61     uint8_t value;
62 
63     value = i2c_get8(i2c, PCA9552_TEST_ADDR, PCA9552_LS0);
64     g_assert_cmphex(value, ==, 0x55);
65 
66     value = i2c_get8(i2c, PCA9552_TEST_ADDR, PCA9552_INPUT0);
67     g_assert_cmphex(value, ==, 0x0);
68 
69     pca9552_init(i2cdev);
70 
71     value = i2c_get8(i2c, PCA9552_TEST_ADDR, PCA9552_LS0);
72     g_assert_cmphex(value, ==, 0x54);
73 
74     value = i2c_get8(i2c, PCA9552_TEST_ADDR, PCA9552_INPUT0);
75     g_assert_cmphex(value, ==, 0x01);
76 
77     value = i2c_get8(i2c, PCA9552_TEST_ADDR, PCA9552_LS3);
78     g_assert_cmphex(value, ==, 0x54);
79 
80     value = i2c_get8(i2c, PCA9552_TEST_ADDR, PCA9552_INPUT1);
81     g_assert_cmphex(value, ==, 0x10);
82 }
83 
84 static void pca9552_register_nodes(void)
85 {
86     QOSGraphEdgeOptions opts = {
87         .extra_device_opts = "address=0x60"
88     };
89 
90     qos_node_create_driver("pca9552", i2c_device_create);
91     qos_node_consumes("pca9552", "i2c-bus", &opts);
92 
93     qos_add_test("tx-rx", "pca9552", send_and_receive, NULL);
94     qos_add_test("rx-autoinc", "pca9552", receive_autoinc, NULL);
95 }
96 libqos_init(pca9552_register_nodes);
97