From 0dc08e53a962b8843ed04281274ce8939529a16d Mon Sep 17 00:00:00 2001 From: Marcin Bukat Date: Fri, 25 Sep 2026 15:43:35 +0200 Subject: [PATCH] S-35390A RTC: bring the driver back, handle 12-hour mode The S-35390A keeps the hour either as 0-23 or - its default after a power-on reset - as 0-11 plus a p.m. flag, selected by the 12/24 bit of status register 1. The driver assumed 24-hour mode and never set it: it masked the p.m. flag off, so on a chip left in 12-hour mode every afternoon read as morning, and writing an afternoon time stored an out-of-range hour. Read the mode in rtc_init() and convert the hour both ways. The chip's mode is left alone, as another firmware on the same player may depend on it - the Samsung YP-CP3's original firmware runs it in 12-hour mode and never touches the bit. If the status register cannot be read, the driver keeps assuming 24-hour mode, as before. Upstream removed the driver with its only users, the Meizu M3/M6 and Samsung YP-S3 ports (1a33d7990a); the Samsung YP-CP3 needs it, so it comes back here, with RTC_S35390A and its SOURCES entry. Also pick the I2C header by CONFIG_I2C, so rk27xx targets can use the driver; i2c_read()/i2c_write() have the same shape there. Co-Authored-By: Claude Opus 5.5 Change-Id: I54d8c8cfaa1b88237f6c4b08d2a510ec9fce8ec5 --- firmware/SOURCES | 2 + firmware/drivers/rtc/rtc_s35390a.c | 145 +++++++++++++++++++++++++++++ firmware/export/config.h | 1 + 3 files changed, 148 insertions(+) create mode 100644 firmware/drivers/rtc/rtc_s35390a.c diff --git a/firmware/SOURCES b/firmware/SOURCES index f3995bc224..2c8ff4bdbe 100644 --- a/firmware/SOURCES +++ b/firmware/SOURCES @@ -502,6 +502,8 @@ drivers/rtc/rtc_jz4740.c drivers/rtc/rtc_jz4760.c #elif (CONFIG_RTC == RTC_X1000) drivers/rtc/rtc_x1000.c +#elif (CONFIG_RTC == RTC_S35390A) +drivers/rtc/rtc_s35390a.c #elif (CONFIG_RTC == RTC_S35380A) drivers/rtc/rtc_s35380a.c #elif (CONFIG_RTC == RTC_D2) diff --git a/firmware/drivers/rtc/rtc_s35390a.c b/firmware/drivers/rtc/rtc_s35390a.c new file mode 100644 index 0000000000..5f12f90b87 --- /dev/null +++ b/firmware/drivers/rtc/rtc_s35390a.c @@ -0,0 +1,145 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * + * Copyright (C) 2009 by Bertrik Sikken + * Copyright (C) 2008 by Robert Kukla + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ****************************************************************************/ +#include "config.h" +#include "rtc.h" +#if CONFIG_I2C == I2C_S5L8700 +#include "i2c-s5l8700.h" +#elif CONFIG_I2C == I2C_RK27XX +#include "i2c-rk27xx.h" +#endif +#include "timefuncs.h" + +/* Driver for the Seiko S35390A real-time clock chip with i2c interface + + This driver was derived from rtc_mr100.c and adapted for the S35390A + used in the Meizu M3 (and possibly others). + */ + +#define RTC_ADDR 0x60 + +#define STATUS_REG1 0 +#define STATUS_REG2 1 +#define REALTIME_DATA1 2 +#define REALTIME_DATA2 3 +#define INT1_REG 4 +#define INT2_REG 5 +#define CLOCK_CORR_REG 6 +#define FREE_REG 7 + +/* bits, once reversed into the usual order */ +#define STATUS1_24H (1 << 1) /* 1 = 24-hour, 0 = 12-hour (reset) */ +#define HOUR_PM (1 << 6) + +/* The chip keeps the hour as 0-23 or, the default after a power-on reset, + * as 0-11 plus the p.m. flag. The mode is left as found - another firmware + * on the same player may expect it - and the hour converted instead. */ +static bool mode_24h = true; + + +static void reverse_bits(unsigned char* v, int size) +{ + static const unsigned char flipnibble[] = + {0x00, 0x08, 0x04, 0x0C, 0x02, 0x0A, 0x06, 0x0E, + 0x01, 0x09, 0x05, 0x0D, 0x03, 0x0B, 0x07, 0x0F}; + + for (int i = 0; i < size; i++) { + v[i] = (flipnibble[v[i] & 0x0F] << 4) | + flipnibble[(v[i] >> 4) & 0x0F]; + } +} + +void rtc_init(void) +{ + unsigned char status; + + if (i2c_read(RTC_ADDR | (STATUS_REG1 << 1), -1, 1, &status) == 0) + { + reverse_bits(&status, 1); + mode_24h = (status & STATUS1_24H) != 0; + } +} + +int rtc_read_datetime(struct tm *tm) +{ + unsigned char buf[7]; + unsigned int i; + bool pm; + int ret; + + ret = i2c_read(RTC_ADDR | (REALTIME_DATA1 << 1), -1, sizeof(buf), buf); + reverse_bits(buf, sizeof(buf)); + + pm = (buf[4] & HOUR_PM) != 0; + buf[4] &= 0x3f; /* mask out p.m. flag */ + + for (i = 0; i < sizeof(buf); i++) + buf[i] = BCD2DEC(buf[i]); + + tm->tm_sec = buf[6]; + tm->tm_min = buf[5]; + tm->tm_hour = buf[4]; + if (!mode_24h && pm) + { + tm->tm_hour += 12; + } + tm->tm_mday = buf[2]; + tm->tm_mon = buf[1] - 1; + tm->tm_year = buf[0] + 100; + + set_day_of_week(tm); + set_day_of_year(tm); + + return ret; +} + +int rtc_write_datetime(const struct tm *tm) +{ + unsigned char buf[7]; + unsigned int i; + int ret; + + buf[6] = tm->tm_sec; + buf[5] = tm->tm_min; + buf[4] = tm->tm_hour; + buf[3] = tm->tm_wday; + buf[2] = tm->tm_mday; + buf[1] = tm->tm_mon + 1; + buf[0] = tm->tm_year - 100; + + if (!mode_24h) + { + buf[4] = tm->tm_hour % 12; + } + + for (i = 0; i < sizeof(buf); i++) + buf[i] = DEC2BCD(buf[i]); + + if (!mode_24h && tm->tm_hour >= 12) + { + buf[4] |= HOUR_PM; + } + + reverse_bits(buf, sizeof(buf)); + ret = i2c_write(RTC_ADDR | (REALTIME_DATA1 << 1), -1, sizeof(buf), buf); + + return ret; +} + diff --git a/firmware/export/config.h b/firmware/export/config.h index bb07f05026..3c4e164da1 100644 --- a/firmware/export/config.h +++ b/firmware/export/config.h @@ -339,6 +339,7 @@ Lyre prototype 1 */ #define RTC_MR100 12 #define RTC_MC13783 13 /* Freescale MC13783 PMIC */ #define RTC_S5L8700 14 +#define RTC_S35390A 15 #define RTC_JZ4740 16 /* Ingenic Jz4740 */ #define RTC_NANO2G 17 /* This seems to be a PCF5063x */ #define RTC_D2 18 /* Either PCF50606 or PCF50635 */