]>
Commit | Line | Data |
---|---|---|
5141d415 CLG |
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" | |
93c3fe2a | 13 | #include "libqos/qgraph.h" |
5141d415 CLG |
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 | ||
93c3fe2a | 20 | static void pca9552_init(QI2CDevice *i2cdev) |
eadcd3b2 | 21 | { |
93c3fe2a PB |
22 | I2CAdapter *i2c = i2cdev->bus; |
23 | ||
eadcd3b2 PB |
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 | ||
93c3fe2a | 29 | static void receive_autoinc(void *obj, void *data, QGuestAllocator *alloc) |
5141d415 | 30 | { |
93c3fe2a PB |
31 | QI2CDevice *i2cdev = (QI2CDevice *)obj; |
32 | I2CAdapter *i2c = i2cdev->bus; | |
5141d415 CLG |
33 | uint8_t resp; |
34 | uint8_t reg = PCA9552_LS0 | PCA9552_AUTOINC; | |
35 | ||
eadcd3b2 PB |
36 | pca9552_init(i2cdev); |
37 | ||
5141d415 CLG |
38 | i2c_send(i2c, PCA9552_TEST_ADDR, ®, 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 | ||
93c3fe2a | 57 | static void send_and_receive(void *obj, void *data, QGuestAllocator *alloc) |
5141d415 | 58 | { |
93c3fe2a PB |
59 | QI2CDevice *i2cdev = (QI2CDevice *)obj; |
60 | I2CAdapter *i2c = i2cdev->bus; | |
5141d415 CLG |
61 | uint8_t value; |
62 | ||
e8ecb706 | 63 | value = i2c_get8(i2c, PCA9552_TEST_ADDR, PCA9552_LS0); |
5141d415 CLG |
64 | g_assert_cmphex(value, ==, 0x55); |
65 | ||
e8ecb706 | 66 | value = i2c_get8(i2c, PCA9552_TEST_ADDR, PCA9552_INPUT0); |
5141d415 CLG |
67 | g_assert_cmphex(value, ==, 0x0); |
68 | ||
eadcd3b2 PB |
69 | pca9552_init(i2cdev); |
70 | ||
e8ecb706 | 71 | value = i2c_get8(i2c, PCA9552_TEST_ADDR, PCA9552_LS0); |
5141d415 CLG |
72 | g_assert_cmphex(value, ==, 0x54); |
73 | ||
e8ecb706 | 74 | value = i2c_get8(i2c, PCA9552_TEST_ADDR, PCA9552_INPUT0); |
5141d415 CLG |
75 | g_assert_cmphex(value, ==, 0x01); |
76 | ||
e8ecb706 | 77 | value = i2c_get8(i2c, PCA9552_TEST_ADDR, PCA9552_LS3); |
5141d415 CLG |
78 | g_assert_cmphex(value, ==, 0x54); |
79 | ||
e8ecb706 | 80 | value = i2c_get8(i2c, PCA9552_TEST_ADDR, PCA9552_INPUT1); |
5141d415 CLG |
81 | g_assert_cmphex(value, ==, 0x10); |
82 | } | |
83 | ||
93c3fe2a | 84 | static void pca9552_register_nodes(void) |
5141d415 | 85 | { |
93c3fe2a PB |
86 | QOSGraphEdgeOptions opts = { |
87 | .extra_device_opts = "address=0x60" | |
88 | }; | |
5141d415 | 89 | |
93c3fe2a PB |
90 | qos_node_create_driver("pca9552", i2c_device_create); |
91 | qos_node_consumes("pca9552", "i2c-bus", &opts); | |
5141d415 | 92 | |
93c3fe2a PB |
93 | qos_add_test("tx-rx", "pca9552", send_and_receive, NULL); |
94 | qos_add_test("rx-autoinc", "pca9552", receive_autoinc, NULL); | |
5141d415 | 95 | } |
93c3fe2a | 96 | libqos_init(pca9552_register_nodes); |