blob: ccca2b3d91408e532dd14a79dda8ccdd7ed149da [file] [log] [blame]
Cédric Le Goater5141d412018-06-08 13:15:32 +01001/*
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é Lureau907b5102022-03-30 13:39:05 +040012#include "libqtest.h"
Paolo Bonzini93c3fe22019-03-18 14:07:13 +010013#include "libqos/qgraph.h"
Cédric Le Goater5141d412018-06-08 13:15:32 +010014#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
Paolo Bonzini93c3fe22019-03-18 14:07:13 +010020static void pca9552_init(QI2CDevice *i2cdev)
Paolo Bonzinieadcd3b2019-03-18 14:56:21 +010021{
22 /* Switch on LEDs 0 and 12 */
Paolo Bonzini06599472019-03-18 15:06:50 +010023 i2c_set8(i2cdev, PCA9552_LS0, 0x54);
24 i2c_set8(i2cdev, PCA9552_LS3, 0x54);
Paolo Bonzinieadcd3b2019-03-18 14:56:21 +010025}
26
Paolo Bonzini93c3fe22019-03-18 14:07:13 +010027static void receive_autoinc(void *obj, void *data, QGuestAllocator *alloc)
Cédric Le Goater5141d412018-06-08 13:15:32 +010028{
Paolo Bonzini93c3fe22019-03-18 14:07:13 +010029 QI2CDevice *i2cdev = (QI2CDevice *)obj;
Cédric Le Goater5141d412018-06-08 13:15:32 +010030 uint8_t resp;
31 uint8_t reg = PCA9552_LS0 | PCA9552_AUTOINC;
32
Paolo Bonzinieadcd3b2019-03-18 14:56:21 +010033 pca9552_init(i2cdev);
34
Alexander Bulekov39397a92020-02-19 23:11:05 -050035 qi2c_send(i2cdev, &reg, 1);
Cédric Le Goater5141d412018-06-08 13:15:32 +010036
37 /* PCA9552_LS0 */
Alexander Bulekov39397a92020-02-19 23:11:05 -050038 qi2c_recv(i2cdev, &resp, 1);
Cédric Le Goater5141d412018-06-08 13:15:32 +010039 g_assert_cmphex(resp, ==, 0x54);
40
41 /* PCA9552_LS1 */
Alexander Bulekov39397a92020-02-19 23:11:05 -050042 qi2c_recv(i2cdev, &resp, 1);
Cédric Le Goater5141d412018-06-08 13:15:32 +010043 g_assert_cmphex(resp, ==, 0x55);
44
45 /* PCA9552_LS2 */
Alexander Bulekov39397a92020-02-19 23:11:05 -050046 qi2c_recv(i2cdev, &resp, 1);
Cédric Le Goater5141d412018-06-08 13:15:32 +010047 g_assert_cmphex(resp, ==, 0x55);
48
49 /* PCA9552_LS3 */
Alexander Bulekov39397a92020-02-19 23:11:05 -050050 qi2c_recv(i2cdev, &resp, 1);
Cédric Le Goater5141d412018-06-08 13:15:32 +010051 g_assert_cmphex(resp, ==, 0x54);
52}
53
Paolo Bonzini93c3fe22019-03-18 14:07:13 +010054static void send_and_receive(void *obj, void *data, QGuestAllocator *alloc)
Cédric Le Goater5141d412018-06-08 13:15:32 +010055{
Paolo Bonzini93c3fe22019-03-18 14:07:13 +010056 QI2CDevice *i2cdev = (QI2CDevice *)obj;
Cédric Le Goater5141d412018-06-08 13:15:32 +010057 uint8_t value;
58
Paolo Bonzini06599472019-03-18 15:06:50 +010059 value = i2c_get8(i2cdev, PCA9552_LS0);
Cédric Le Goater5141d412018-06-08 13:15:32 +010060 g_assert_cmphex(value, ==, 0x55);
61
Paolo Bonzini06599472019-03-18 15:06:50 +010062 value = i2c_get8(i2cdev, PCA9552_INPUT0);
Glenn Miles7b99fb32024-01-25 16:48:10 -060063 g_assert_cmphex(value, ==, 0xFF);
Cédric Le Goater5141d412018-06-08 13:15:32 +010064
Paolo Bonzinieadcd3b2019-03-18 14:56:21 +010065 pca9552_init(i2cdev);
66
Paolo Bonzini06599472019-03-18 15:06:50 +010067 value = i2c_get8(i2cdev, PCA9552_LS0);
Cédric Le Goater5141d412018-06-08 13:15:32 +010068 g_assert_cmphex(value, ==, 0x54);
69
Paolo Bonzini06599472019-03-18 15:06:50 +010070 value = i2c_get8(i2cdev, PCA9552_INPUT0);
Glenn Miles7b99fb32024-01-25 16:48:10 -060071 g_assert_cmphex(value, ==, 0xFE);
Cédric Le Goater5141d412018-06-08 13:15:32 +010072
Paolo Bonzini06599472019-03-18 15:06:50 +010073 value = i2c_get8(i2cdev, PCA9552_LS3);
Cédric Le Goater5141d412018-06-08 13:15:32 +010074 g_assert_cmphex(value, ==, 0x54);
75
Paolo Bonzini06599472019-03-18 15:06:50 +010076 value = i2c_get8(i2cdev, PCA9552_INPUT1);
Glenn Miles7b99fb32024-01-25 16:48:10 -060077 g_assert_cmphex(value, ==, 0xEF);
Cédric Le Goater5141d412018-06-08 13:15:32 +010078}
79
Paolo Bonzini93c3fe22019-03-18 14:07:13 +010080static void pca9552_register_nodes(void)
Cédric Le Goater5141d412018-06-08 13:15:32 +010081{
Paolo Bonzini93c3fe22019-03-18 14:07:13 +010082 QOSGraphEdgeOptions opts = {
83 .extra_device_opts = "address=0x60"
84 };
Paolo Bonzini06599472019-03-18 15:06:50 +010085 add_qi2c_address(&opts, &(QI2CAddress) { 0x60 });
Cédric Le Goater5141d412018-06-08 13:15:32 +010086
Paolo Bonzini93c3fe22019-03-18 14:07:13 +010087 qos_node_create_driver("pca9552", i2c_device_create);
88 qos_node_consumes("pca9552", "i2c-bus", &opts);
Cédric Le Goater5141d412018-06-08 13:15:32 +010089
Paolo Bonzini93c3fe22019-03-18 14:07:13 +010090 qos_add_test("tx-rx", "pca9552", send_and_receive, NULL);
91 qos_add_test("rx-autoinc", "pca9552", receive_autoinc, NULL);
Cédric Le Goater5141d412018-06-08 13:15:32 +010092}
Paolo Bonzini93c3fe22019-03-18 14:07:13 +010093libqos_init(pca9552_register_nodes);