xref: /openbmc/qemu/tests/qtest/pca9552-test.c (revision 907b5105)
11e8a1faeSThomas Huth /*
21e8a1faeSThomas Huth  * QTest testcase for the PCA9552 LED blinker
31e8a1faeSThomas Huth  *
41e8a1faeSThomas Huth  * Copyright (c) 2017-2018, IBM Corporation.
51e8a1faeSThomas Huth  *
61e8a1faeSThomas Huth  * This work is licensed under the terms of the GNU GPL, version 2 or later.
71e8a1faeSThomas Huth  * See the COPYING file in the top-level directory.
81e8a1faeSThomas Huth  */
91e8a1faeSThomas Huth 
101e8a1faeSThomas Huth #include "qemu/osdep.h"
111e8a1faeSThomas Huth 
12*907b5105SMarc-André Lureau #include "libqtest.h"
131e8a1faeSThomas Huth #include "libqos/qgraph.h"
141e8a1faeSThomas Huth #include "libqos/i2c.h"
151e8a1faeSThomas Huth #include "hw/misc/pca9552_regs.h"
161e8a1faeSThomas Huth 
171e8a1faeSThomas Huth #define PCA9552_TEST_ID   "pca9552-test"
181e8a1faeSThomas Huth #define PCA9552_TEST_ADDR 0x60
191e8a1faeSThomas Huth 
pca9552_init(QI2CDevice * i2cdev)201e8a1faeSThomas Huth static void pca9552_init(QI2CDevice *i2cdev)
211e8a1faeSThomas Huth {
221e8a1faeSThomas Huth     /* Switch on LEDs 0 and 12 */
231e8a1faeSThomas Huth     i2c_set8(i2cdev, PCA9552_LS0, 0x54);
241e8a1faeSThomas Huth     i2c_set8(i2cdev, PCA9552_LS3, 0x54);
251e8a1faeSThomas Huth }
261e8a1faeSThomas Huth 
receive_autoinc(void * obj,void * data,QGuestAllocator * alloc)271e8a1faeSThomas Huth static void receive_autoinc(void *obj, void *data, QGuestAllocator *alloc)
281e8a1faeSThomas Huth {
291e8a1faeSThomas Huth     QI2CDevice *i2cdev = (QI2CDevice *)obj;
301e8a1faeSThomas Huth     uint8_t resp;
311e8a1faeSThomas Huth     uint8_t reg = PCA9552_LS0 | PCA9552_AUTOINC;
321e8a1faeSThomas Huth 
331e8a1faeSThomas Huth     pca9552_init(i2cdev);
341e8a1faeSThomas Huth 
3539397a9aSAlexander Bulekov     qi2c_send(i2cdev, &reg, 1);
361e8a1faeSThomas Huth 
371e8a1faeSThomas Huth     /* PCA9552_LS0 */
3839397a9aSAlexander Bulekov     qi2c_recv(i2cdev, &resp, 1);
391e8a1faeSThomas Huth     g_assert_cmphex(resp, ==, 0x54);
401e8a1faeSThomas Huth 
411e8a1faeSThomas Huth     /* PCA9552_LS1 */
4239397a9aSAlexander Bulekov     qi2c_recv(i2cdev, &resp, 1);
431e8a1faeSThomas Huth     g_assert_cmphex(resp, ==, 0x55);
441e8a1faeSThomas Huth 
451e8a1faeSThomas Huth     /* PCA9552_LS2 */
4639397a9aSAlexander Bulekov     qi2c_recv(i2cdev, &resp, 1);
471e8a1faeSThomas Huth     g_assert_cmphex(resp, ==, 0x55);
481e8a1faeSThomas Huth 
491e8a1faeSThomas Huth     /* PCA9552_LS3 */
5039397a9aSAlexander Bulekov     qi2c_recv(i2cdev, &resp, 1);
511e8a1faeSThomas Huth     g_assert_cmphex(resp, ==, 0x54);
521e8a1faeSThomas Huth }
531e8a1faeSThomas Huth 
send_and_receive(void * obj,void * data,QGuestAllocator * alloc)541e8a1faeSThomas Huth static void send_and_receive(void *obj, void *data, QGuestAllocator *alloc)
551e8a1faeSThomas Huth {
561e8a1faeSThomas Huth     QI2CDevice *i2cdev = (QI2CDevice *)obj;
571e8a1faeSThomas Huth     uint8_t value;
581e8a1faeSThomas Huth 
591e8a1faeSThomas Huth     value = i2c_get8(i2cdev, PCA9552_LS0);
601e8a1faeSThomas Huth     g_assert_cmphex(value, ==, 0x55);
611e8a1faeSThomas Huth 
621e8a1faeSThomas Huth     value = i2c_get8(i2cdev, PCA9552_INPUT0);
631e8a1faeSThomas Huth     g_assert_cmphex(value, ==, 0x0);
641e8a1faeSThomas Huth 
651e8a1faeSThomas Huth     pca9552_init(i2cdev);
661e8a1faeSThomas Huth 
671e8a1faeSThomas Huth     value = i2c_get8(i2cdev, PCA9552_LS0);
681e8a1faeSThomas Huth     g_assert_cmphex(value, ==, 0x54);
691e8a1faeSThomas Huth 
701e8a1faeSThomas Huth     value = i2c_get8(i2cdev, PCA9552_INPUT0);
711e8a1faeSThomas Huth     g_assert_cmphex(value, ==, 0x01);
721e8a1faeSThomas Huth 
731e8a1faeSThomas Huth     value = i2c_get8(i2cdev, PCA9552_LS3);
741e8a1faeSThomas Huth     g_assert_cmphex(value, ==, 0x54);
751e8a1faeSThomas Huth 
761e8a1faeSThomas Huth     value = i2c_get8(i2cdev, PCA9552_INPUT1);
771e8a1faeSThomas Huth     g_assert_cmphex(value, ==, 0x10);
781e8a1faeSThomas Huth }
791e8a1faeSThomas Huth 
pca9552_register_nodes(void)801e8a1faeSThomas Huth static void pca9552_register_nodes(void)
811e8a1faeSThomas Huth {
821e8a1faeSThomas Huth     QOSGraphEdgeOptions opts = {
831e8a1faeSThomas Huth         .extra_device_opts = "address=0x60"
841e8a1faeSThomas Huth     };
851e8a1faeSThomas Huth     add_qi2c_address(&opts, &(QI2CAddress) { 0x60 });
861e8a1faeSThomas Huth 
871e8a1faeSThomas Huth     qos_node_create_driver("pca9552", i2c_device_create);
881e8a1faeSThomas Huth     qos_node_consumes("pca9552", "i2c-bus", &opts);
891e8a1faeSThomas Huth 
901e8a1faeSThomas Huth     qos_add_test("tx-rx", "pca9552", send_and_receive, NULL);
911e8a1faeSThomas Huth     qos_add_test("rx-autoinc", "pca9552", receive_autoinc, NULL);
921e8a1faeSThomas Huth }
931e8a1faeSThomas Huth libqos_init(pca9552_register_nodes);
94