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