kernel: rtc: rs5c372: fix alarm support
[openwrt/openwrt.git] / target / linux / generic / pending-5.4 / 190-rtc-rs5c372-support_alarms_up_to_1_week.patch
1 From: Daniel González Cabanelas <dgcbueu@gmail.com>
2 Subject: [PATCH 1/2] rtc: rs5c372: support alarms up to 1 week
3
4 The Ricoh R2221x, R2223x, RS5C372, RV5C387A chips can handle 1 week
5 alarms.
6
7 Read the "wday" alarm register and convert it to a date to support up 1
8 week in our driver.
9
10 Signed-off-by: Daniel González Cabanelas <dgcbueu@gmail.com>
11 ---
12 drivers/rtc/rtc-rs5c372.c | 48 ++++++++++++++++++++++++++++++++++-----
13 1 file changed, 42 insertions(+), 6 deletions(-)
14
15 diff --git a/drivers/rtc/rtc-rs5c372.c b/drivers/rtc/rtc-rs5c372.c
16 index 3bd6eaa0d..94b778c6e 100644
17 --- a/drivers/rtc/rtc-rs5c372.c
18 +++ b/drivers/rtc/rtc-rs5c372.c
19 @@ -393,7 +393,9 @@ static int rs5c_read_alarm(struct device *dev, struct rtc_wkalrm *t)
20 {
21 struct i2c_client *client = to_i2c_client(dev);
22 struct rs5c372 *rs5c = i2c_get_clientdata(client);
23 - int status;
24 + int status, wday_offs;
25 + struct rtc_time rtc;
26 + unsigned long alarm_secs;
27
28 status = rs5c_get_regs(rs5c);
29 if (status < 0)
30 @@ -403,6 +405,30 @@ static int rs5c_read_alarm(struct device *dev, struct rtc_wkalrm *t)
31 t->time.tm_sec = 0;
32 t->time.tm_min = bcd2bin(rs5c->regs[RS5C_REG_ALARM_A_MIN] & 0x7f);
33 t->time.tm_hour = rs5c_reg2hr(rs5c, rs5c->regs[RS5C_REG_ALARM_A_HOURS]);
34 + t->time.tm_wday = ffs(rs5c->regs[RS5C_REG_ALARM_A_WDAY] & 0x7f) - 1;
35 +
36 + /* determine the day, month and year based on alarm wday, taking as a
37 + * reference the current time from the rtc
38 + */
39 + status = rs5c372_rtc_read_time(dev, &rtc);
40 + if (status < 0)
41 + return status;
42 +
43 + wday_offs = t->time.tm_wday - rtc.tm_wday;
44 + alarm_secs = mktime64(rtc.tm_year + 1900,
45 + rtc.tm_mon + 1,
46 + rtc.tm_mday + wday_offs,
47 + t->time.tm_hour,
48 + t->time.tm_min,
49 + t->time.tm_sec);
50 +
51 + if (wday_offs < 0 || (wday_offs == 0 &&
52 + (t->time.tm_hour < rtc.tm_hour ||
53 + (t->time.tm_hour == rtc.tm_hour &&
54 + t->time.tm_min <= rtc.tm_min))))
55 + alarm_secs += 7 * 86400;
56 +
57 + rtc_time64_to_tm(alarm_secs, &t->time);
58
59 /* ... and status */
60 t->enabled = !!(rs5c->regs[RS5C_REG_CTRL1] & RS5C_CTRL1_AALE);
61 @@ -417,12 +443,20 @@ static int rs5c_set_alarm(struct device *dev, struct rtc_wkalrm *t)
62 struct rs5c372 *rs5c = i2c_get_clientdata(client);
63 int status, addr, i;
64 unsigned char buf[3];
65 + struct rtc_time rtc_tm;
66 + unsigned long rtc_secs, alarm_secs;
67
68 - /* only handle up to 24 hours in the future, like RTC_ALM_SET */
69 - if (t->time.tm_mday != -1
70 - || t->time.tm_mon != -1
71 - || t->time.tm_year != -1)
72 + /* chip only can handle alarms up to one week in the future*/
73 + status = rs5c372_rtc_read_time(dev, &rtc_tm);
74 + if (status)
75 + return status;
76 + rtc_secs = rtc_tm_to_time64(&rtc_tm);
77 + alarm_secs = rtc_tm_to_time64(&t->time);
78 + if (alarm_secs >= rtc_secs + 7 * 86400) {
79 + dev_err(dev, "%s: alarm maximum is one week in the future (%d)\n",
80 + __func__, status);
81 return -EINVAL;
82 + }
83
84 /* REVISIT: round up tm_sec */
85
86 @@ -443,7 +477,9 @@ static int rs5c_set_alarm(struct device *dev, struct rtc_wkalrm *t)
87 /* set alarm */
88 buf[0] = bin2bcd(t->time.tm_min);
89 buf[1] = rs5c_hr2reg(rs5c, t->time.tm_hour);
90 - buf[2] = 0x7f; /* any/all days */
91 + /* each bit is the day of the week, 0x7f means all days */
92 + buf[2] = (t->time.tm_wday >= 0 && t->time.tm_wday < 7) ?
93 + BIT(t->time.tm_wday) : 0x7f;
94
95 for (i = 0; i < sizeof(buf); i++) {
96 addr = RS5C_ADDR(RS5C_REG_ALARM_A_MIN + i);