Cédric Le Goater | 5141d41 | 2018-06-08 13:15:32 +0100 | [diff] [blame] | 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 | |
Marc-André Lureau | 907b510 | 2022-03-30 13:39:05 +0400 | [diff] [blame] | 12 | #include "libqtest.h" |
Paolo Bonzini | 93c3fe2 | 2019-03-18 14:07:13 +0100 | [diff] [blame] | 13 | #include "libqos/qgraph.h" |
Cédric Le Goater | 5141d41 | 2018-06-08 13:15:32 +0100 | [diff] [blame] | 14 | #include "libqos/i2c.h" |
Cédric Le Goater | 6328d8f | 2024-03-25 14:48:32 +0100 | [diff] [blame] | 15 | #include "hw/gpio/pca9552_regs.h" |
Cédric Le Goater | 5141d41 | 2018-06-08 13:15:32 +0100 | [diff] [blame] | 16 | |
| 17 | #define PCA9552_TEST_ID "pca9552-test" |
| 18 | #define PCA9552_TEST_ADDR 0x60 |
| 19 | |
Paolo Bonzini | 93c3fe2 | 2019-03-18 14:07:13 +0100 | [diff] [blame] | 20 | static void pca9552_init(QI2CDevice *i2cdev) |
Paolo Bonzini | eadcd3b | 2019-03-18 14:56:21 +0100 | [diff] [blame] | 21 | { |
| 22 | /* Switch on LEDs 0 and 12 */ |
Paolo Bonzini | 0659947 | 2019-03-18 15:06:50 +0100 | [diff] [blame] | 23 | i2c_set8(i2cdev, PCA9552_LS0, 0x54); |
| 24 | i2c_set8(i2cdev, PCA9552_LS3, 0x54); |
Paolo Bonzini | eadcd3b | 2019-03-18 14:56:21 +0100 | [diff] [blame] | 25 | } |
| 26 | |
Paolo Bonzini | 93c3fe2 | 2019-03-18 14:07:13 +0100 | [diff] [blame] | 27 | static void receive_autoinc(void *obj, void *data, QGuestAllocator *alloc) |
Cédric Le Goater | 5141d41 | 2018-06-08 13:15:32 +0100 | [diff] [blame] | 28 | { |
Paolo Bonzini | 93c3fe2 | 2019-03-18 14:07:13 +0100 | [diff] [blame] | 29 | QI2CDevice *i2cdev = (QI2CDevice *)obj; |
Cédric Le Goater | 5141d41 | 2018-06-08 13:15:32 +0100 | [diff] [blame] | 30 | uint8_t resp; |
| 31 | uint8_t reg = PCA9552_LS0 | PCA9552_AUTOINC; |
| 32 | |
Paolo Bonzini | eadcd3b | 2019-03-18 14:56:21 +0100 | [diff] [blame] | 33 | pca9552_init(i2cdev); |
| 34 | |
Alexander Bulekov | 39397a9 | 2020-02-19 23:11:05 -0500 | [diff] [blame] | 35 | qi2c_send(i2cdev, ®, 1); |
Cédric Le Goater | 5141d41 | 2018-06-08 13:15:32 +0100 | [diff] [blame] | 36 | |
| 37 | /* PCA9552_LS0 */ |
Alexander Bulekov | 39397a9 | 2020-02-19 23:11:05 -0500 | [diff] [blame] | 38 | qi2c_recv(i2cdev, &resp, 1); |
Cédric Le Goater | 5141d41 | 2018-06-08 13:15:32 +0100 | [diff] [blame] | 39 | g_assert_cmphex(resp, ==, 0x54); |
| 40 | |
| 41 | /* PCA9552_LS1 */ |
Alexander Bulekov | 39397a9 | 2020-02-19 23:11:05 -0500 | [diff] [blame] | 42 | qi2c_recv(i2cdev, &resp, 1); |
Cédric Le Goater | 5141d41 | 2018-06-08 13:15:32 +0100 | [diff] [blame] | 43 | g_assert_cmphex(resp, ==, 0x55); |
| 44 | |
| 45 | /* PCA9552_LS2 */ |
Alexander Bulekov | 39397a9 | 2020-02-19 23:11:05 -0500 | [diff] [blame] | 46 | qi2c_recv(i2cdev, &resp, 1); |
Cédric Le Goater | 5141d41 | 2018-06-08 13:15:32 +0100 | [diff] [blame] | 47 | g_assert_cmphex(resp, ==, 0x55); |
| 48 | |
| 49 | /* PCA9552_LS3 */ |
Alexander Bulekov | 39397a9 | 2020-02-19 23:11:05 -0500 | [diff] [blame] | 50 | qi2c_recv(i2cdev, &resp, 1); |
Cédric Le Goater | 5141d41 | 2018-06-08 13:15:32 +0100 | [diff] [blame] | 51 | g_assert_cmphex(resp, ==, 0x54); |
| 52 | } |
| 53 | |
Paolo Bonzini | 93c3fe2 | 2019-03-18 14:07:13 +0100 | [diff] [blame] | 54 | static void send_and_receive(void *obj, void *data, QGuestAllocator *alloc) |
Cédric Le Goater | 5141d41 | 2018-06-08 13:15:32 +0100 | [diff] [blame] | 55 | { |
Paolo Bonzini | 93c3fe2 | 2019-03-18 14:07:13 +0100 | [diff] [blame] | 56 | QI2CDevice *i2cdev = (QI2CDevice *)obj; |
Cédric Le Goater | 5141d41 | 2018-06-08 13:15:32 +0100 | [diff] [blame] | 57 | uint8_t value; |
| 58 | |
Paolo Bonzini | 0659947 | 2019-03-18 15:06:50 +0100 | [diff] [blame] | 59 | value = i2c_get8(i2cdev, PCA9552_LS0); |
Cédric Le Goater | 5141d41 | 2018-06-08 13:15:32 +0100 | [diff] [blame] | 60 | g_assert_cmphex(value, ==, 0x55); |
| 61 | |
Paolo Bonzini | 0659947 | 2019-03-18 15:06:50 +0100 | [diff] [blame] | 62 | value = i2c_get8(i2cdev, PCA9552_INPUT0); |
Glenn Miles | 7b99fb3 | 2024-01-25 16:48:10 -0600 | [diff] [blame] | 63 | g_assert_cmphex(value, ==, 0xFF); |
Cédric Le Goater | 5141d41 | 2018-06-08 13:15:32 +0100 | [diff] [blame] | 64 | |
Paolo Bonzini | eadcd3b | 2019-03-18 14:56:21 +0100 | [diff] [blame] | 65 | pca9552_init(i2cdev); |
| 66 | |
Paolo Bonzini | 0659947 | 2019-03-18 15:06:50 +0100 | [diff] [blame] | 67 | value = i2c_get8(i2cdev, PCA9552_LS0); |
Cédric Le Goater | 5141d41 | 2018-06-08 13:15:32 +0100 | [diff] [blame] | 68 | g_assert_cmphex(value, ==, 0x54); |
| 69 | |
Paolo Bonzini | 0659947 | 2019-03-18 15:06:50 +0100 | [diff] [blame] | 70 | value = i2c_get8(i2cdev, PCA9552_INPUT0); |
Glenn Miles | 7b99fb3 | 2024-01-25 16:48:10 -0600 | [diff] [blame] | 71 | g_assert_cmphex(value, ==, 0xFE); |
Cédric Le Goater | 5141d41 | 2018-06-08 13:15:32 +0100 | [diff] [blame] | 72 | |
Paolo Bonzini | 0659947 | 2019-03-18 15:06:50 +0100 | [diff] [blame] | 73 | value = i2c_get8(i2cdev, PCA9552_LS3); |
Cédric Le Goater | 5141d41 | 2018-06-08 13:15:32 +0100 | [diff] [blame] | 74 | g_assert_cmphex(value, ==, 0x54); |
| 75 | |
Paolo Bonzini | 0659947 | 2019-03-18 15:06:50 +0100 | [diff] [blame] | 76 | value = i2c_get8(i2cdev, PCA9552_INPUT1); |
Glenn Miles | 7b99fb3 | 2024-01-25 16:48:10 -0600 | [diff] [blame] | 77 | g_assert_cmphex(value, ==, 0xEF); |
Cédric Le Goater | 5141d41 | 2018-06-08 13:15:32 +0100 | [diff] [blame] | 78 | } |
| 79 | |
Paolo Bonzini | 93c3fe2 | 2019-03-18 14:07:13 +0100 | [diff] [blame] | 80 | static void pca9552_register_nodes(void) |
Cédric Le Goater | 5141d41 | 2018-06-08 13:15:32 +0100 | [diff] [blame] | 81 | { |
Paolo Bonzini | 93c3fe2 | 2019-03-18 14:07:13 +0100 | [diff] [blame] | 82 | QOSGraphEdgeOptions opts = { |
| 83 | .extra_device_opts = "address=0x60" |
| 84 | }; |
Paolo Bonzini | 0659947 | 2019-03-18 15:06:50 +0100 | [diff] [blame] | 85 | add_qi2c_address(&opts, &(QI2CAddress) { 0x60 }); |
Cédric Le Goater | 5141d41 | 2018-06-08 13:15:32 +0100 | [diff] [blame] | 86 | |
Paolo Bonzini | 93c3fe2 | 2019-03-18 14:07:13 +0100 | [diff] [blame] | 87 | qos_node_create_driver("pca9552", i2c_device_create); |
| 88 | qos_node_consumes("pca9552", "i2c-bus", &opts); |
Cédric Le Goater | 5141d41 | 2018-06-08 13:15:32 +0100 | [diff] [blame] | 89 | |
Paolo Bonzini | 93c3fe2 | 2019-03-18 14:07:13 +0100 | [diff] [blame] | 90 | qos_add_test("tx-rx", "pca9552", send_and_receive, NULL); |
| 91 | qos_add_test("rx-autoinc", "pca9552", receive_autoinc, NULL); |
Cédric Le Goater | 5141d41 | 2018-06-08 13:15:32 +0100 | [diff] [blame] | 92 | } |
Paolo Bonzini | 93c3fe2 | 2019-03-18 14:07:13 +0100 | [diff] [blame] | 93 | libqos_init(pca9552_register_nodes); |