1*822405b1SBernhard Beschow /*
2*822405b1SBernhard Beschow * QTest testcase for the RS5C372 RTC
3*822405b1SBernhard Beschow *
4*822405b1SBernhard Beschow * Copyright (c) 2025 Bernhard Beschow <shentey@gmail.com>
5*822405b1SBernhard Beschow *
6*822405b1SBernhard Beschow * Based on ds1338-test.c
7*822405b1SBernhard Beschow *
8*822405b1SBernhard Beschow * SPDX-License-Identifier: GPL-2.0-or-later
9*822405b1SBernhard Beschow */
10*822405b1SBernhard Beschow
11*822405b1SBernhard Beschow #include "qemu/osdep.h"
12*822405b1SBernhard Beschow #include "qemu/bcd.h"
13*822405b1SBernhard Beschow #include "libqos/i2c.h"
14*822405b1SBernhard Beschow
15*822405b1SBernhard Beschow #define RS5C372_ADDR 0x32
16*822405b1SBernhard Beschow
rs5c372_read_date(void * obj,void * data,QGuestAllocator * alloc)17*822405b1SBernhard Beschow static void rs5c372_read_date(void *obj, void *data, QGuestAllocator *alloc)
18*822405b1SBernhard Beschow {
19*822405b1SBernhard Beschow QI2CDevice *i2cdev = obj;
20*822405b1SBernhard Beschow
21*822405b1SBernhard Beschow uint8_t resp[0x10];
22*822405b1SBernhard Beschow time_t now = time(NULL);
23*822405b1SBernhard Beschow struct tm *utc = gmtime(&now);
24*822405b1SBernhard Beschow
25*822405b1SBernhard Beschow i2c_read_block(i2cdev, 0, resp, sizeof(resp));
26*822405b1SBernhard Beschow
27*822405b1SBernhard Beschow /* check retrieved time against local time */
28*822405b1SBernhard Beschow g_assert_cmpuint(from_bcd(resp[5]), == , utc->tm_mday);
29*822405b1SBernhard Beschow g_assert_cmpuint(from_bcd(resp[6]), == , 1 + utc->tm_mon);
30*822405b1SBernhard Beschow g_assert_cmpuint(2000 + from_bcd(resp[7]), == , 1900 + utc->tm_year);
31*822405b1SBernhard Beschow }
32*822405b1SBernhard Beschow
rs5c372_register_nodes(void)33*822405b1SBernhard Beschow static void rs5c372_register_nodes(void)
34*822405b1SBernhard Beschow {
35*822405b1SBernhard Beschow QOSGraphEdgeOptions opts = { };
36*822405b1SBernhard Beschow add_qi2c_address(&opts, &(QI2CAddress) { RS5C372_ADDR });
37*822405b1SBernhard Beschow
38*822405b1SBernhard Beschow qos_node_create_driver("rs5c372", i2c_device_create);
39*822405b1SBernhard Beschow qos_node_consumes("rs5c372", "i2c-bus", &opts);
40*822405b1SBernhard Beschow qos_add_test("read_date", "rs5c372", rs5c372_read_date, NULL);
41*822405b1SBernhard Beschow }
42*822405b1SBernhard Beschow
43*822405b1SBernhard Beschow libqos_init(rs5c372_register_nodes);
44