xref: /qemu/tests/qtest/pca9552-test.c (revision 732c919cf04c0aaf1b092238e8b84cdb7adf657a)
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/i2c.h"
14 #include "hw/misc/pca9552_regs.h"
15 
16 #define PCA9552_TEST_ID   "pca9552-test"
17 #define PCA9552_TEST_ADDR 0x60
18 
19 static I2CAdapter *i2c;
20 
21 static void pca9552_init(I2CAdapter *i2c)
22 {
23     /* Switch on LEDs 0 and 12 */
24     i2c_set8(i2c, PCA9552_TEST_ADDR, PCA9552_LS0, 0x54);
25     i2c_set8(i2c, PCA9552_TEST_ADDR, PCA9552_LS3, 0x54);
26 }
27 
28 static void receive_autoinc(void)
29 {
30     uint8_t resp;
31     uint8_t reg = PCA9552_LS0 | PCA9552_AUTOINC;
32 
33     pca9552_init(i2cdev);
34 
35     i2c_send(i2c, PCA9552_TEST_ADDR, &reg, 1);
36 
37     /* PCA9552_LS0 */
38     i2c_recv(i2c, PCA9552_TEST_ADDR, &resp, 1);
39     g_assert_cmphex(resp, ==, 0x54);
40 
41     /* PCA9552_LS1 */
42     i2c_recv(i2c, PCA9552_TEST_ADDR, &resp, 1);
43     g_assert_cmphex(resp, ==, 0x55);
44 
45     /* PCA9552_LS2 */
46     i2c_recv(i2c, PCA9552_TEST_ADDR, &resp, 1);
47     g_assert_cmphex(resp, ==, 0x55);
48 
49     /* PCA9552_LS3 */
50     i2c_recv(i2c, PCA9552_TEST_ADDR, &resp, 1);
51     g_assert_cmphex(resp, ==, 0x54);
52 }
53 
54 static void send_and_receive(void)
55 {
56     uint8_t value;
57 
58     value = i2c_get8(i2c, PCA9552_TEST_ADDR, PCA9552_LS0);
59     g_assert_cmphex(value, ==, 0x55);
60 
61     value = i2c_get8(i2c, PCA9552_TEST_ADDR, PCA9552_INPUT0);
62     g_assert_cmphex(value, ==, 0x0);
63 
64     pca9552_init(i2cdev);
65 
66     value = i2c_get8(i2c, PCA9552_TEST_ADDR, PCA9552_LS0);
67     g_assert_cmphex(value, ==, 0x54);
68 
69     value = i2c_get8(i2c, PCA9552_TEST_ADDR, PCA9552_INPUT0);
70     g_assert_cmphex(value, ==, 0x01);
71 
72     value = i2c_get8(i2c, PCA9552_TEST_ADDR, PCA9552_LS3);
73     g_assert_cmphex(value, ==, 0x54);
74 
75     value = i2c_get8(i2c, PCA9552_TEST_ADDR, PCA9552_INPUT1);
76     g_assert_cmphex(value, ==, 0x10);
77 }
78 
79 int main(int argc, char **argv)
80 {
81     QTestState *s = NULL;
82     int ret;
83 
84     g_test_init(&argc, &argv, NULL);
85 
86     s = qtest_start("-machine n800 "
87                     "-device pca9552,bus=i2c-bus.0,id=" PCA9552_TEST_ID
88                     ",address=0x60");
89     i2c = omap_i2c_create(s, OMAP2_I2C_1_BASE);
90 
91     qtest_add_func("/pca9552/tx-rx", send_and_receive);
92     qtest_add_func("/pca9552/rx-autoinc", receive_autoinc);
93 
94     ret = g_test_run();
95 
96     if (s) {
97         qtest_quit(s);
98     }
99     omap_i2c_free(i2c);
100 
101     return ret;
102 }
103