Skip to content

Commit 107ccc4

Browse files
committed
Merge tag 'rtc-5.15' of git://git.kernel.org/pub/scm/linux/kernel/git/abelloni/linux
Pull RTC updates from Alexandre Belloni: "The broken down time conversion is similar to what is done in the time subsystem since v5.14. The rest is fairly straightforward. Subsystem: - Switch to Neri and Schneider time conversion algorithm Drivers: - rx8025: add rx8035 support - s5m: modernize driver and set range" * tag 'rtc-5.15' of git://git.kernel.org/pub/scm/linux/kernel/git/abelloni/linux: rtc: rx8010: select REGMAP_I2C dt-bindings: rtc: add Epson RX-8025 and RX-8035 rtc: rx8025: implement RX-8035 support rtc: cmos: remove stale REVISIT comments rtc: tps65910: Correct driver module alias rtc: move RTC_LIB_KUNIT_TEST to proper location rtc: lib_test: add MODULE_LICENSE rtc: Improve performance of rtc_time64_to_tm(). Add tests. rtc: s5m: set range rtc: s5m: enable wakeup only when available rtc: s5m: signal the core when alarm are not available rtc: s5m: switch to devm_rtc_allocate_device
2 parents 5292622 + 0c45d3e commit 107ccc4

File tree

9 files changed

+243
-64
lines changed

9 files changed

+243
-64
lines changed

Documentation/devicetree/bindings/rtc/trivial-rtc.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ properties:
3232
- dallas,ds3232
3333
# I2C-BUS INTERFACE REAL TIME CLOCK MODULE
3434
- epson,rx8010
35+
# I2C-BUS INTERFACE REAL TIME CLOCK MODULE
36+
- epson,rx8025
37+
- epson,rx8035
3538
# I2C-BUS INTERFACE REAL TIME CLOCK MODULE with Battery Backed RAM
3639
- epson,rx8571
3740
# I2C-BUS INTERFACE REAL TIME CLOCK MODULE

drivers/rtc/Kconfig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,15 @@ config RTC_DEBUG
7575
Say yes here to enable debugging support in the RTC framework
7676
and individual RTC drivers.
7777

78+
config RTC_LIB_KUNIT_TEST
79+
tristate "KUnit test for RTC lib functions" if !KUNIT_ALL_TESTS
80+
depends on KUNIT
81+
default KUNIT_ALL_TESTS
82+
help
83+
Enable this option to test RTC library functions.
84+
85+
If unsure, say N.
86+
7887
config RTC_NVMEM
7988
bool "RTC non volatile storage support"
8089
select NVMEM
@@ -624,6 +633,7 @@ config RTC_DRV_FM3130
624633

625634
config RTC_DRV_RX8010
626635
tristate "Epson RX8010SJ"
636+
select REGMAP_I2C
627637
help
628638
If you say yes here you get support for the Epson RX8010SJ RTC
629639
chip.

drivers/rtc/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ rtc-core-$(CONFIG_RTC_INTF_DEV) += dev.o
1515
rtc-core-$(CONFIG_RTC_INTF_PROC) += proc.o
1616
rtc-core-$(CONFIG_RTC_INTF_SYSFS) += sysfs.o
1717

18+
obj-$(CONFIG_RTC_LIB_KUNIT_TEST) += lib_test.o
19+
1820
# Keep the list ordered.
1921

2022
obj-$(CONFIG_RTC_DRV_88PM80X) += rtc-88pm80x.o

drivers/rtc/lib.c

Lines changed: 80 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
* Author: Alessandro Zummo <[email protected]>
77
*
88
* based on arch/arm/common/rtctime.c and other bits
9+
*
10+
* Author: Cassio Neri <[email protected]> (rtc_time64_to_tm)
911
*/
1012

1113
#include <linux/export.h>
@@ -22,8 +24,6 @@ static const unsigned short rtc_ydays[2][13] = {
2224
{ 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
2325
};
2426

25-
#define LEAPS_THRU_END_OF(y) ((y) / 4 - (y) / 100 + (y) / 400)
26-
2727
/*
2828
* The number of days in the month.
2929
*/
@@ -42,42 +42,95 @@ int rtc_year_days(unsigned int day, unsigned int month, unsigned int year)
4242
}
4343
EXPORT_SYMBOL(rtc_year_days);
4444

45-
/*
46-
* rtc_time64_to_tm - Converts time64_t to rtc_time.
47-
* Convert seconds since 01-01-1970 00:00:00 to Gregorian date.
45+
/**
46+
* rtc_time64_to_tm - converts time64_t to rtc_time.
47+
*
48+
* @time: The number of seconds since 01-01-1970 00:00:00.
49+
* (Must be positive.)
50+
* @tm: Pointer to the struct rtc_time.
4851
*/
4952
void rtc_time64_to_tm(time64_t time, struct rtc_time *tm)
5053
{
51-
unsigned int month, year, secs;
54+
unsigned int secs;
5255
int days;
5356

57+
u64 u64tmp;
58+
u32 u32tmp, udays, century, day_of_century, year_of_century, year,
59+
day_of_year, month, day;
60+
bool is_Jan_or_Feb, is_leap_year;
61+
5462
/* time must be positive */
5563
days = div_s64_rem(time, 86400, &secs);
5664

5765
/* day of the week, 1970-01-01 was a Thursday */
5866
tm->tm_wday = (days + 4) % 7;
5967

60-
year = 1970 + days / 365;
61-
days -= (year - 1970) * 365
62-
+ LEAPS_THRU_END_OF(year - 1)
63-
- LEAPS_THRU_END_OF(1970 - 1);
64-
while (days < 0) {
65-
year -= 1;
66-
days += 365 + is_leap_year(year);
67-
}
68-
tm->tm_year = year - 1900;
69-
tm->tm_yday = days + 1;
70-
71-
for (month = 0; month < 11; month++) {
72-
int newdays;
73-
74-
newdays = days - rtc_month_days(month, year);
75-
if (newdays < 0)
76-
break;
77-
days = newdays;
78-
}
79-
tm->tm_mon = month;
80-
tm->tm_mday = days + 1;
68+
/*
69+
* The following algorithm is, basically, Proposition 6.3 of Neri
70+
* and Schneider [1]. In a few words: it works on the computational
71+
* (fictitious) calendar where the year starts in March, month = 2
72+
* (*), and finishes in February, month = 13. This calendar is
73+
* mathematically convenient because the day of the year does not
74+
* depend on whether the year is leap or not. For instance:
75+
*
76+
* March 1st 0-th day of the year;
77+
* ...
78+
* April 1st 31-st day of the year;
79+
* ...
80+
* January 1st 306-th day of the year; (Important!)
81+
* ...
82+
* February 28th 364-th day of the year;
83+
* February 29th 365-th day of the year (if it exists).
84+
*
85+
* After having worked out the date in the computational calendar
86+
* (using just arithmetics) it's easy to convert it to the
87+
* corresponding date in the Gregorian calendar.
88+
*
89+
* [1] "Euclidean Affine Functions and Applications to Calendar
90+
* Algorithms". https://arxiv.org/abs/2102.06959
91+
*
92+
* (*) The numbering of months follows rtc_time more closely and
93+
* thus, is slightly different from [1].
94+
*/
95+
96+
udays = ((u32) days) + 719468;
97+
98+
u32tmp = 4 * udays + 3;
99+
century = u32tmp / 146097;
100+
day_of_century = u32tmp % 146097 / 4;
101+
102+
u32tmp = 4 * day_of_century + 3;
103+
u64tmp = 2939745ULL * u32tmp;
104+
year_of_century = upper_32_bits(u64tmp);
105+
day_of_year = lower_32_bits(u64tmp) / 2939745 / 4;
106+
107+
year = 100 * century + year_of_century;
108+
is_leap_year = year_of_century != 0 ?
109+
year_of_century % 4 == 0 : century % 4 == 0;
110+
111+
u32tmp = 2141 * day_of_year + 132377;
112+
month = u32tmp >> 16;
113+
day = ((u16) u32tmp) / 2141;
114+
115+
/*
116+
* Recall that January 01 is the 306-th day of the year in the
117+
* computational (not Gregorian) calendar.
118+
*/
119+
is_Jan_or_Feb = day_of_year >= 306;
120+
121+
/* Converts to the Gregorian calendar. */
122+
year = year + is_Jan_or_Feb;
123+
month = is_Jan_or_Feb ? month - 12 : month;
124+
day = day + 1;
125+
126+
day_of_year = is_Jan_or_Feb ?
127+
day_of_year - 306 : day_of_year + 31 + 28 + is_leap_year;
128+
129+
/* Converts to rtc_time's format. */
130+
tm->tm_year = (int) (year - 1900);
131+
tm->tm_mon = (int) month;
132+
tm->tm_mday = (int) day;
133+
tm->tm_yday = (int) day_of_year + 1;
81134

82135
tm->tm_hour = secs / 3600;
83136
secs -= tm->tm_hour * 3600;

drivers/rtc/lib_test.c

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// SPDX-License-Identifier: LGPL-2.1+
2+
3+
#include <kunit/test.h>
4+
#include <linux/rtc.h>
5+
6+
/*
7+
* Advance a date by one day.
8+
*/
9+
static void advance_date(int *year, int *month, int *mday, int *yday)
10+
{
11+
if (*mday != rtc_month_days(*month - 1, *year)) {
12+
++*mday;
13+
++*yday;
14+
return;
15+
}
16+
17+
*mday = 1;
18+
if (*month != 12) {
19+
++*month;
20+
++*yday;
21+
return;
22+
}
23+
24+
*month = 1;
25+
*yday = 1;
26+
++*year;
27+
}
28+
29+
/*
30+
* Checks every day in a 160000 years interval starting on 1970-01-01
31+
* against the expected result.
32+
*/
33+
static void rtc_time64_to_tm_test_date_range(struct kunit *test)
34+
{
35+
/*
36+
* 160000 years = (160000 / 400) * 400 years
37+
* = (160000 / 400) * 146097 days
38+
* = (160000 / 400) * 146097 * 86400 seconds
39+
*/
40+
time64_t total_secs = ((time64_t) 160000) / 400 * 146097 * 86400;
41+
42+
int year = 1970;
43+
int month = 1;
44+
int mday = 1;
45+
int yday = 1;
46+
47+
struct rtc_time result;
48+
time64_t secs;
49+
s64 days;
50+
51+
for (secs = 0; secs <= total_secs; secs += 86400) {
52+
53+
rtc_time64_to_tm(secs, &result);
54+
55+
days = div_s64(secs, 86400);
56+
57+
#define FAIL_MSG "%d/%02d/%02d (%2d) : %ld", \
58+
year, month, mday, yday, days
59+
60+
KUNIT_ASSERT_EQ_MSG(test, year - 1900, result.tm_year, FAIL_MSG);
61+
KUNIT_ASSERT_EQ_MSG(test, month - 1, result.tm_mon, FAIL_MSG);
62+
KUNIT_ASSERT_EQ_MSG(test, mday, result.tm_mday, FAIL_MSG);
63+
KUNIT_ASSERT_EQ_MSG(test, yday, result.tm_yday, FAIL_MSG);
64+
65+
advance_date(&year, &month, &mday, &yday);
66+
}
67+
}
68+
69+
static struct kunit_case rtc_lib_test_cases[] = {
70+
KUNIT_CASE(rtc_time64_to_tm_test_date_range),
71+
{}
72+
};
73+
74+
static struct kunit_suite rtc_lib_test_suite = {
75+
.name = "rtc_lib_test_cases",
76+
.test_cases = rtc_lib_test_cases,
77+
};
78+
79+
kunit_test_suite(rtc_lib_test_suite);
80+
81+
MODULE_LICENSE("GPL");

drivers/rtc/rtc-cmos.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -229,19 +229,13 @@ static int cmos_read_time(struct device *dev, struct rtc_time *t)
229229
if (!pm_trace_rtc_valid())
230230
return -EIO;
231231

232-
/* REVISIT: if the clock has a "century" register, use
233-
* that instead of the heuristic in mc146818_get_time().
234-
* That'll make Y3K compatility (year > 2070) easy!
235-
*/
236232
mc146818_get_time(t);
237233
return 0;
238234
}
239235

240236
static int cmos_set_time(struct device *dev, struct rtc_time *t)
241237
{
242-
/* REVISIT: set the "century" register if available
243-
*
244-
* NOTE: this ignores the issue whereby updating the seconds
238+
/* NOTE: this ignores the issue whereby updating the seconds
245239
* takes effect exactly 500ms after we write the register.
246240
* (Also queueing and other delays before we get this far.)
247241
*/

drivers/rtc/rtc-rx8025.c

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,23 @@
6060
#define RX8025_ADJ_DATA_MAX 62
6161
#define RX8025_ADJ_DATA_MIN -62
6262

63+
enum rx_model {
64+
model_rx_unknown,
65+
model_rx_8025,
66+
model_rx_8035,
67+
model_last
68+
};
69+
6370
static const struct i2c_device_id rx8025_id[] = {
64-
{ "rx8025", 0 },
71+
{ "rx8025", model_rx_8025 },
72+
{ "rx8035", model_rx_8035 },
6573
{ }
6674
};
6775
MODULE_DEVICE_TABLE(i2c, rx8025_id);
6876

6977
struct rx8025_data {
7078
struct rtc_device *rtc;
79+
enum rx_model model;
7180
u8 ctrl1;
7281
};
7382

@@ -100,10 +109,26 @@ static s32 rx8025_write_regs(const struct i2c_client *client,
100109
length, values);
101110
}
102111

112+
static int rx8025_is_osc_stopped(enum rx_model model, int ctrl2)
113+
{
114+
int xstp = ctrl2 & RX8025_BIT_CTRL2_XST;
115+
/* XSTP bit has different polarity on RX-8025 vs RX-8035.
116+
* RX-8025: 0 == oscillator stopped
117+
* RX-8035: 1 == oscillator stopped
118+
*/
119+
120+
if (model == model_rx_8025)
121+
xstp = !xstp;
122+
123+
return xstp;
124+
}
125+
103126
static int rx8025_check_validity(struct device *dev)
104127
{
105128
struct i2c_client *client = to_i2c_client(dev);
129+
struct rx8025_data *drvdata = dev_get_drvdata(dev);
106130
int ctrl2;
131+
int xstp;
107132

108133
ctrl2 = rx8025_read_reg(client, RX8025_REG_CTRL2);
109134
if (ctrl2 < 0)
@@ -117,7 +142,8 @@ static int rx8025_check_validity(struct device *dev)
117142
return -EINVAL;
118143
}
119144

120-
if (!(ctrl2 & RX8025_BIT_CTRL2_XST)) {
145+
xstp = rx8025_is_osc_stopped(drvdata->model, ctrl2);
146+
if (xstp) {
121147
dev_warn(dev, "crystal stopped, date is invalid\n");
122148
return -EINVAL;
123149
}
@@ -127,29 +153,36 @@ static int rx8025_check_validity(struct device *dev)
127153

128154
static int rx8025_reset_validity(struct i2c_client *client)
129155
{
156+
struct rx8025_data *drvdata = i2c_get_clientdata(client);
130157
int ctrl2 = rx8025_read_reg(client, RX8025_REG_CTRL2);
131158

132159
if (ctrl2 < 0)
133160
return ctrl2;
134161

135162
ctrl2 &= ~(RX8025_BIT_CTRL2_PON | RX8025_BIT_CTRL2_VDET);
136163

164+
if (drvdata->model == model_rx_8025)
165+
ctrl2 |= RX8025_BIT_CTRL2_XST;
166+
else
167+
ctrl2 &= ~(RX8025_BIT_CTRL2_XST);
168+
137169
return rx8025_write_reg(client, RX8025_REG_CTRL2,
138-
ctrl2 | RX8025_BIT_CTRL2_XST);
170+
ctrl2);
139171
}
140172

141173
static irqreturn_t rx8025_handle_irq(int irq, void *dev_id)
142174
{
143175
struct i2c_client *client = dev_id;
144176
struct rx8025_data *rx8025 = i2c_get_clientdata(client);
145-
int status;
177+
int status, xstp;
146178

147179
rtc_lock(rx8025->rtc);
148180
status = rx8025_read_reg(client, RX8025_REG_CTRL2);
149181
if (status < 0)
150182
goto out;
151183

152-
if (!(status & RX8025_BIT_CTRL2_XST))
184+
xstp = rx8025_is_osc_stopped(rx8025->model, status);
185+
if (xstp)
153186
dev_warn(&client->dev, "Oscillation stop was detected,"
154187
"you may have to readjust the clock\n");
155188

@@ -519,6 +552,9 @@ static int rx8025_probe(struct i2c_client *client,
519552

520553
i2c_set_clientdata(client, rx8025);
521554

555+
if (id)
556+
rx8025->model = id->driver_data;
557+
522558
err = rx8025_init_client(client);
523559
if (err)
524560
return err;

0 commit comments

Comments
 (0)