1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * RTC subsystem, interface functions
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2005 Tower Technologies
6*4882a593Smuzhiyun * Author: Alessandro Zummo <a.zummo@towertech.it>
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * based on arch/arm/common/rtctime.c
9*4882a593Smuzhiyun */
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun #include <linux/rtc.h>
12*4882a593Smuzhiyun #include <linux/sched.h>
13*4882a593Smuzhiyun #include <linux/module.h>
14*4882a593Smuzhiyun #include <linux/log2.h>
15*4882a593Smuzhiyun #include <linux/workqueue.h>
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun #define CREATE_TRACE_POINTS
18*4882a593Smuzhiyun #include <trace/events/rtc.h>
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer);
21*4882a593Smuzhiyun static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer);
22*4882a593Smuzhiyun
rtc_add_offset(struct rtc_device * rtc,struct rtc_time * tm)23*4882a593Smuzhiyun static void rtc_add_offset(struct rtc_device *rtc, struct rtc_time *tm)
24*4882a593Smuzhiyun {
25*4882a593Smuzhiyun time64_t secs;
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun if (!rtc->offset_secs)
28*4882a593Smuzhiyun return;
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun secs = rtc_tm_to_time64(tm);
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun /*
33*4882a593Smuzhiyun * Since the reading time values from RTC device are always in the RTC
34*4882a593Smuzhiyun * original valid range, but we need to skip the overlapped region
35*4882a593Smuzhiyun * between expanded range and original range, which is no need to add
36*4882a593Smuzhiyun * the offset.
37*4882a593Smuzhiyun */
38*4882a593Smuzhiyun if ((rtc->start_secs > rtc->range_min && secs >= rtc->start_secs) ||
39*4882a593Smuzhiyun (rtc->start_secs < rtc->range_min &&
40*4882a593Smuzhiyun secs <= (rtc->start_secs + rtc->range_max - rtc->range_min)))
41*4882a593Smuzhiyun return;
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun rtc_time64_to_tm(secs + rtc->offset_secs, tm);
44*4882a593Smuzhiyun }
45*4882a593Smuzhiyun
rtc_subtract_offset(struct rtc_device * rtc,struct rtc_time * tm)46*4882a593Smuzhiyun static void rtc_subtract_offset(struct rtc_device *rtc, struct rtc_time *tm)
47*4882a593Smuzhiyun {
48*4882a593Smuzhiyun time64_t secs;
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun if (!rtc->offset_secs)
51*4882a593Smuzhiyun return;
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun secs = rtc_tm_to_time64(tm);
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun /*
56*4882a593Smuzhiyun * If the setting time values are in the valid range of RTC hardware
57*4882a593Smuzhiyun * device, then no need to subtract the offset when setting time to RTC
58*4882a593Smuzhiyun * device. Otherwise we need to subtract the offset to make the time
59*4882a593Smuzhiyun * values are valid for RTC hardware device.
60*4882a593Smuzhiyun */
61*4882a593Smuzhiyun if (secs >= rtc->range_min && secs <= rtc->range_max)
62*4882a593Smuzhiyun return;
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun rtc_time64_to_tm(secs - rtc->offset_secs, tm);
65*4882a593Smuzhiyun }
66*4882a593Smuzhiyun
rtc_valid_range(struct rtc_device * rtc,struct rtc_time * tm)67*4882a593Smuzhiyun static int rtc_valid_range(struct rtc_device *rtc, struct rtc_time *tm)
68*4882a593Smuzhiyun {
69*4882a593Smuzhiyun if (rtc->range_min != rtc->range_max) {
70*4882a593Smuzhiyun time64_t time = rtc_tm_to_time64(tm);
71*4882a593Smuzhiyun time64_t range_min = rtc->set_start_time ? rtc->start_secs :
72*4882a593Smuzhiyun rtc->range_min;
73*4882a593Smuzhiyun timeu64_t range_max = rtc->set_start_time ?
74*4882a593Smuzhiyun (rtc->start_secs + rtc->range_max - rtc->range_min) :
75*4882a593Smuzhiyun rtc->range_max;
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun if (time < range_min || time > range_max)
78*4882a593Smuzhiyun return -ERANGE;
79*4882a593Smuzhiyun }
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun return 0;
82*4882a593Smuzhiyun }
83*4882a593Smuzhiyun
__rtc_read_time(struct rtc_device * rtc,struct rtc_time * tm)84*4882a593Smuzhiyun static int __rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm)
85*4882a593Smuzhiyun {
86*4882a593Smuzhiyun int err;
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun if (!rtc->ops) {
89*4882a593Smuzhiyun err = -ENODEV;
90*4882a593Smuzhiyun } else if (!rtc->ops->read_time) {
91*4882a593Smuzhiyun err = -EINVAL;
92*4882a593Smuzhiyun } else {
93*4882a593Smuzhiyun memset(tm, 0, sizeof(struct rtc_time));
94*4882a593Smuzhiyun err = rtc->ops->read_time(rtc->dev.parent, tm);
95*4882a593Smuzhiyun if (err < 0) {
96*4882a593Smuzhiyun dev_dbg(&rtc->dev, "read_time: fail to read: %d\n",
97*4882a593Smuzhiyun err);
98*4882a593Smuzhiyun return err;
99*4882a593Smuzhiyun }
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun rtc_add_offset(rtc, tm);
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun err = rtc_valid_tm(tm);
104*4882a593Smuzhiyun if (err < 0)
105*4882a593Smuzhiyun dev_dbg(&rtc->dev, "read_time: rtc_time isn't valid\n");
106*4882a593Smuzhiyun }
107*4882a593Smuzhiyun return err;
108*4882a593Smuzhiyun }
109*4882a593Smuzhiyun
rtc_read_time(struct rtc_device * rtc,struct rtc_time * tm)110*4882a593Smuzhiyun int rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm)
111*4882a593Smuzhiyun {
112*4882a593Smuzhiyun int err;
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun err = mutex_lock_interruptible(&rtc->ops_lock);
115*4882a593Smuzhiyun if (err)
116*4882a593Smuzhiyun return err;
117*4882a593Smuzhiyun
118*4882a593Smuzhiyun err = __rtc_read_time(rtc, tm);
119*4882a593Smuzhiyun mutex_unlock(&rtc->ops_lock);
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun trace_rtc_read_time(rtc_tm_to_time64(tm), err);
122*4882a593Smuzhiyun return err;
123*4882a593Smuzhiyun }
124*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(rtc_read_time);
125*4882a593Smuzhiyun
rtc_set_time(struct rtc_device * rtc,struct rtc_time * tm)126*4882a593Smuzhiyun int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm)
127*4882a593Smuzhiyun {
128*4882a593Smuzhiyun int err, uie;
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun err = rtc_valid_tm(tm);
131*4882a593Smuzhiyun if (err != 0)
132*4882a593Smuzhiyun return err;
133*4882a593Smuzhiyun
134*4882a593Smuzhiyun err = rtc_valid_range(rtc, tm);
135*4882a593Smuzhiyun if (err)
136*4882a593Smuzhiyun return err;
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun rtc_subtract_offset(rtc, tm);
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
141*4882a593Smuzhiyun uie = rtc->uie_rtctimer.enabled || rtc->uie_irq_active;
142*4882a593Smuzhiyun #else
143*4882a593Smuzhiyun uie = rtc->uie_rtctimer.enabled;
144*4882a593Smuzhiyun #endif
145*4882a593Smuzhiyun if (uie) {
146*4882a593Smuzhiyun err = rtc_update_irq_enable(rtc, 0);
147*4882a593Smuzhiyun if (err)
148*4882a593Smuzhiyun return err;
149*4882a593Smuzhiyun }
150*4882a593Smuzhiyun
151*4882a593Smuzhiyun err = mutex_lock_interruptible(&rtc->ops_lock);
152*4882a593Smuzhiyun if (err)
153*4882a593Smuzhiyun return err;
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun if (!rtc->ops)
156*4882a593Smuzhiyun err = -ENODEV;
157*4882a593Smuzhiyun else if (rtc->ops->set_time)
158*4882a593Smuzhiyun err = rtc->ops->set_time(rtc->dev.parent, tm);
159*4882a593Smuzhiyun else
160*4882a593Smuzhiyun err = -EINVAL;
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun pm_stay_awake(rtc->dev.parent);
163*4882a593Smuzhiyun mutex_unlock(&rtc->ops_lock);
164*4882a593Smuzhiyun /* A timer might have just expired */
165*4882a593Smuzhiyun schedule_work(&rtc->irqwork);
166*4882a593Smuzhiyun
167*4882a593Smuzhiyun if (uie) {
168*4882a593Smuzhiyun err = rtc_update_irq_enable(rtc, 1);
169*4882a593Smuzhiyun if (err)
170*4882a593Smuzhiyun return err;
171*4882a593Smuzhiyun }
172*4882a593Smuzhiyun
173*4882a593Smuzhiyun trace_rtc_set_time(rtc_tm_to_time64(tm), err);
174*4882a593Smuzhiyun return err;
175*4882a593Smuzhiyun }
176*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(rtc_set_time);
177*4882a593Smuzhiyun
rtc_read_alarm_internal(struct rtc_device * rtc,struct rtc_wkalrm * alarm)178*4882a593Smuzhiyun static int rtc_read_alarm_internal(struct rtc_device *rtc,
179*4882a593Smuzhiyun struct rtc_wkalrm *alarm)
180*4882a593Smuzhiyun {
181*4882a593Smuzhiyun int err;
182*4882a593Smuzhiyun
183*4882a593Smuzhiyun err = mutex_lock_interruptible(&rtc->ops_lock);
184*4882a593Smuzhiyun if (err)
185*4882a593Smuzhiyun return err;
186*4882a593Smuzhiyun
187*4882a593Smuzhiyun if (!rtc->ops) {
188*4882a593Smuzhiyun err = -ENODEV;
189*4882a593Smuzhiyun } else if (!rtc->ops->read_alarm) {
190*4882a593Smuzhiyun err = -EINVAL;
191*4882a593Smuzhiyun } else {
192*4882a593Smuzhiyun alarm->enabled = 0;
193*4882a593Smuzhiyun alarm->pending = 0;
194*4882a593Smuzhiyun alarm->time.tm_sec = -1;
195*4882a593Smuzhiyun alarm->time.tm_min = -1;
196*4882a593Smuzhiyun alarm->time.tm_hour = -1;
197*4882a593Smuzhiyun alarm->time.tm_mday = -1;
198*4882a593Smuzhiyun alarm->time.tm_mon = -1;
199*4882a593Smuzhiyun alarm->time.tm_year = -1;
200*4882a593Smuzhiyun alarm->time.tm_wday = -1;
201*4882a593Smuzhiyun alarm->time.tm_yday = -1;
202*4882a593Smuzhiyun alarm->time.tm_isdst = -1;
203*4882a593Smuzhiyun err = rtc->ops->read_alarm(rtc->dev.parent, alarm);
204*4882a593Smuzhiyun }
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun mutex_unlock(&rtc->ops_lock);
207*4882a593Smuzhiyun
208*4882a593Smuzhiyun trace_rtc_read_alarm(rtc_tm_to_time64(&alarm->time), err);
209*4882a593Smuzhiyun return err;
210*4882a593Smuzhiyun }
211*4882a593Smuzhiyun
__rtc_read_alarm(struct rtc_device * rtc,struct rtc_wkalrm * alarm)212*4882a593Smuzhiyun int __rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
213*4882a593Smuzhiyun {
214*4882a593Smuzhiyun int err;
215*4882a593Smuzhiyun struct rtc_time before, now;
216*4882a593Smuzhiyun int first_time = 1;
217*4882a593Smuzhiyun time64_t t_now, t_alm;
218*4882a593Smuzhiyun enum { none, day, month, year } missing = none;
219*4882a593Smuzhiyun unsigned int days;
220*4882a593Smuzhiyun
221*4882a593Smuzhiyun /* The lower level RTC driver may return -1 in some fields,
222*4882a593Smuzhiyun * creating invalid alarm->time values, for reasons like:
223*4882a593Smuzhiyun *
224*4882a593Smuzhiyun * - The hardware may not be capable of filling them in;
225*4882a593Smuzhiyun * many alarms match only on time-of-day fields, not
226*4882a593Smuzhiyun * day/month/year calendar data.
227*4882a593Smuzhiyun *
228*4882a593Smuzhiyun * - Some hardware uses illegal values as "wildcard" match
229*4882a593Smuzhiyun * values, which non-Linux firmware (like a BIOS) may try
230*4882a593Smuzhiyun * to set up as e.g. "alarm 15 minutes after each hour".
231*4882a593Smuzhiyun * Linux uses only oneshot alarms.
232*4882a593Smuzhiyun *
233*4882a593Smuzhiyun * When we see that here, we deal with it by using values from
234*4882a593Smuzhiyun * a current RTC timestamp for any missing (-1) values. The
235*4882a593Smuzhiyun * RTC driver prevents "periodic alarm" modes.
236*4882a593Smuzhiyun *
237*4882a593Smuzhiyun * But this can be racey, because some fields of the RTC timestamp
238*4882a593Smuzhiyun * may have wrapped in the interval since we read the RTC alarm,
239*4882a593Smuzhiyun * which would lead to us inserting inconsistent values in place
240*4882a593Smuzhiyun * of the -1 fields.
241*4882a593Smuzhiyun *
242*4882a593Smuzhiyun * Reading the alarm and timestamp in the reverse sequence
243*4882a593Smuzhiyun * would have the same race condition, and not solve the issue.
244*4882a593Smuzhiyun *
245*4882a593Smuzhiyun * So, we must first read the RTC timestamp,
246*4882a593Smuzhiyun * then read the RTC alarm value,
247*4882a593Smuzhiyun * and then read a second RTC timestamp.
248*4882a593Smuzhiyun *
249*4882a593Smuzhiyun * If any fields of the second timestamp have changed
250*4882a593Smuzhiyun * when compared with the first timestamp, then we know
251*4882a593Smuzhiyun * our timestamp may be inconsistent with that used by
252*4882a593Smuzhiyun * the low-level rtc_read_alarm_internal() function.
253*4882a593Smuzhiyun *
254*4882a593Smuzhiyun * So, when the two timestamps disagree, we just loop and do
255*4882a593Smuzhiyun * the process again to get a fully consistent set of values.
256*4882a593Smuzhiyun *
257*4882a593Smuzhiyun * This could all instead be done in the lower level driver,
258*4882a593Smuzhiyun * but since more than one lower level RTC implementation needs it,
259*4882a593Smuzhiyun * then it's probably best best to do it here instead of there..
260*4882a593Smuzhiyun */
261*4882a593Smuzhiyun
262*4882a593Smuzhiyun /* Get the "before" timestamp */
263*4882a593Smuzhiyun err = rtc_read_time(rtc, &before);
264*4882a593Smuzhiyun if (err < 0)
265*4882a593Smuzhiyun return err;
266*4882a593Smuzhiyun do {
267*4882a593Smuzhiyun if (!first_time)
268*4882a593Smuzhiyun memcpy(&before, &now, sizeof(struct rtc_time));
269*4882a593Smuzhiyun first_time = 0;
270*4882a593Smuzhiyun
271*4882a593Smuzhiyun /* get the RTC alarm values, which may be incomplete */
272*4882a593Smuzhiyun err = rtc_read_alarm_internal(rtc, alarm);
273*4882a593Smuzhiyun if (err)
274*4882a593Smuzhiyun return err;
275*4882a593Smuzhiyun
276*4882a593Smuzhiyun /* full-function RTCs won't have such missing fields */
277*4882a593Smuzhiyun if (rtc_valid_tm(&alarm->time) == 0) {
278*4882a593Smuzhiyun rtc_add_offset(rtc, &alarm->time);
279*4882a593Smuzhiyun return 0;
280*4882a593Smuzhiyun }
281*4882a593Smuzhiyun
282*4882a593Smuzhiyun /* get the "after" timestamp, to detect wrapped fields */
283*4882a593Smuzhiyun err = rtc_read_time(rtc, &now);
284*4882a593Smuzhiyun if (err < 0)
285*4882a593Smuzhiyun return err;
286*4882a593Smuzhiyun
287*4882a593Smuzhiyun /* note that tm_sec is a "don't care" value here: */
288*4882a593Smuzhiyun } while (before.tm_min != now.tm_min ||
289*4882a593Smuzhiyun before.tm_hour != now.tm_hour ||
290*4882a593Smuzhiyun before.tm_mon != now.tm_mon ||
291*4882a593Smuzhiyun before.tm_year != now.tm_year);
292*4882a593Smuzhiyun
293*4882a593Smuzhiyun /* Fill in the missing alarm fields using the timestamp; we
294*4882a593Smuzhiyun * know there's at least one since alarm->time is invalid.
295*4882a593Smuzhiyun */
296*4882a593Smuzhiyun if (alarm->time.tm_sec == -1)
297*4882a593Smuzhiyun alarm->time.tm_sec = now.tm_sec;
298*4882a593Smuzhiyun if (alarm->time.tm_min == -1)
299*4882a593Smuzhiyun alarm->time.tm_min = now.tm_min;
300*4882a593Smuzhiyun if (alarm->time.tm_hour == -1)
301*4882a593Smuzhiyun alarm->time.tm_hour = now.tm_hour;
302*4882a593Smuzhiyun
303*4882a593Smuzhiyun /* For simplicity, only support date rollover for now */
304*4882a593Smuzhiyun if (alarm->time.tm_mday < 1 || alarm->time.tm_mday > 31) {
305*4882a593Smuzhiyun alarm->time.tm_mday = now.tm_mday;
306*4882a593Smuzhiyun missing = day;
307*4882a593Smuzhiyun }
308*4882a593Smuzhiyun if ((unsigned int)alarm->time.tm_mon >= 12) {
309*4882a593Smuzhiyun alarm->time.tm_mon = now.tm_mon;
310*4882a593Smuzhiyun if (missing == none)
311*4882a593Smuzhiyun missing = month;
312*4882a593Smuzhiyun }
313*4882a593Smuzhiyun if (alarm->time.tm_year == -1) {
314*4882a593Smuzhiyun alarm->time.tm_year = now.tm_year;
315*4882a593Smuzhiyun if (missing == none)
316*4882a593Smuzhiyun missing = year;
317*4882a593Smuzhiyun }
318*4882a593Smuzhiyun
319*4882a593Smuzhiyun /* Can't proceed if alarm is still invalid after replacing
320*4882a593Smuzhiyun * missing fields.
321*4882a593Smuzhiyun */
322*4882a593Smuzhiyun err = rtc_valid_tm(&alarm->time);
323*4882a593Smuzhiyun if (err)
324*4882a593Smuzhiyun goto done;
325*4882a593Smuzhiyun
326*4882a593Smuzhiyun /* with luck, no rollover is needed */
327*4882a593Smuzhiyun t_now = rtc_tm_to_time64(&now);
328*4882a593Smuzhiyun t_alm = rtc_tm_to_time64(&alarm->time);
329*4882a593Smuzhiyun if (t_now < t_alm)
330*4882a593Smuzhiyun goto done;
331*4882a593Smuzhiyun
332*4882a593Smuzhiyun switch (missing) {
333*4882a593Smuzhiyun /* 24 hour rollover ... if it's now 10am Monday, an alarm that
334*4882a593Smuzhiyun * that will trigger at 5am will do so at 5am Tuesday, which
335*4882a593Smuzhiyun * could also be in the next month or year. This is a common
336*4882a593Smuzhiyun * case, especially for PCs.
337*4882a593Smuzhiyun */
338*4882a593Smuzhiyun case day:
339*4882a593Smuzhiyun dev_dbg(&rtc->dev, "alarm rollover: %s\n", "day");
340*4882a593Smuzhiyun t_alm += 24 * 60 * 60;
341*4882a593Smuzhiyun rtc_time64_to_tm(t_alm, &alarm->time);
342*4882a593Smuzhiyun break;
343*4882a593Smuzhiyun
344*4882a593Smuzhiyun /* Month rollover ... if it's the 31th, an alarm on the 3rd will
345*4882a593Smuzhiyun * be next month. An alarm matching on the 30th, 29th, or 28th
346*4882a593Smuzhiyun * may end up in the month after that! Many newer PCs support
347*4882a593Smuzhiyun * this type of alarm.
348*4882a593Smuzhiyun */
349*4882a593Smuzhiyun case month:
350*4882a593Smuzhiyun dev_dbg(&rtc->dev, "alarm rollover: %s\n", "month");
351*4882a593Smuzhiyun do {
352*4882a593Smuzhiyun if (alarm->time.tm_mon < 11) {
353*4882a593Smuzhiyun alarm->time.tm_mon++;
354*4882a593Smuzhiyun } else {
355*4882a593Smuzhiyun alarm->time.tm_mon = 0;
356*4882a593Smuzhiyun alarm->time.tm_year++;
357*4882a593Smuzhiyun }
358*4882a593Smuzhiyun days = rtc_month_days(alarm->time.tm_mon,
359*4882a593Smuzhiyun alarm->time.tm_year);
360*4882a593Smuzhiyun } while (days < alarm->time.tm_mday);
361*4882a593Smuzhiyun break;
362*4882a593Smuzhiyun
363*4882a593Smuzhiyun /* Year rollover ... easy except for leap years! */
364*4882a593Smuzhiyun case year:
365*4882a593Smuzhiyun dev_dbg(&rtc->dev, "alarm rollover: %s\n", "year");
366*4882a593Smuzhiyun do {
367*4882a593Smuzhiyun alarm->time.tm_year++;
368*4882a593Smuzhiyun } while (!is_leap_year(alarm->time.tm_year + 1900) &&
369*4882a593Smuzhiyun rtc_valid_tm(&alarm->time) != 0);
370*4882a593Smuzhiyun break;
371*4882a593Smuzhiyun
372*4882a593Smuzhiyun default:
373*4882a593Smuzhiyun dev_warn(&rtc->dev, "alarm rollover not handled\n");
374*4882a593Smuzhiyun }
375*4882a593Smuzhiyun
376*4882a593Smuzhiyun err = rtc_valid_tm(&alarm->time);
377*4882a593Smuzhiyun
378*4882a593Smuzhiyun done:
379*4882a593Smuzhiyun if (err)
380*4882a593Smuzhiyun dev_warn(&rtc->dev, "invalid alarm value: %ptR\n",
381*4882a593Smuzhiyun &alarm->time);
382*4882a593Smuzhiyun
383*4882a593Smuzhiyun return err;
384*4882a593Smuzhiyun }
385*4882a593Smuzhiyun
rtc_read_alarm(struct rtc_device * rtc,struct rtc_wkalrm * alarm)386*4882a593Smuzhiyun int rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
387*4882a593Smuzhiyun {
388*4882a593Smuzhiyun int err;
389*4882a593Smuzhiyun
390*4882a593Smuzhiyun err = mutex_lock_interruptible(&rtc->ops_lock);
391*4882a593Smuzhiyun if (err)
392*4882a593Smuzhiyun return err;
393*4882a593Smuzhiyun if (!rtc->ops) {
394*4882a593Smuzhiyun err = -ENODEV;
395*4882a593Smuzhiyun } else if (!rtc->ops->read_alarm) {
396*4882a593Smuzhiyun err = -EINVAL;
397*4882a593Smuzhiyun } else {
398*4882a593Smuzhiyun memset(alarm, 0, sizeof(struct rtc_wkalrm));
399*4882a593Smuzhiyun alarm->enabled = rtc->aie_timer.enabled;
400*4882a593Smuzhiyun alarm->time = rtc_ktime_to_tm(rtc->aie_timer.node.expires);
401*4882a593Smuzhiyun }
402*4882a593Smuzhiyun mutex_unlock(&rtc->ops_lock);
403*4882a593Smuzhiyun
404*4882a593Smuzhiyun trace_rtc_read_alarm(rtc_tm_to_time64(&alarm->time), err);
405*4882a593Smuzhiyun return err;
406*4882a593Smuzhiyun }
407*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(rtc_read_alarm);
408*4882a593Smuzhiyun
__rtc_set_alarm(struct rtc_device * rtc,struct rtc_wkalrm * alarm)409*4882a593Smuzhiyun static int __rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
410*4882a593Smuzhiyun {
411*4882a593Smuzhiyun struct rtc_time tm;
412*4882a593Smuzhiyun time64_t now, scheduled;
413*4882a593Smuzhiyun int err;
414*4882a593Smuzhiyun
415*4882a593Smuzhiyun err = rtc_valid_tm(&alarm->time);
416*4882a593Smuzhiyun if (err)
417*4882a593Smuzhiyun return err;
418*4882a593Smuzhiyun
419*4882a593Smuzhiyun scheduled = rtc_tm_to_time64(&alarm->time);
420*4882a593Smuzhiyun
421*4882a593Smuzhiyun /* Make sure we're not setting alarms in the past */
422*4882a593Smuzhiyun err = __rtc_read_time(rtc, &tm);
423*4882a593Smuzhiyun if (err)
424*4882a593Smuzhiyun return err;
425*4882a593Smuzhiyun now = rtc_tm_to_time64(&tm);
426*4882a593Smuzhiyun if (scheduled <= now)
427*4882a593Smuzhiyun return -ETIME;
428*4882a593Smuzhiyun /*
429*4882a593Smuzhiyun * XXX - We just checked to make sure the alarm time is not
430*4882a593Smuzhiyun * in the past, but there is still a race window where if
431*4882a593Smuzhiyun * the is alarm set for the next second and the second ticks
432*4882a593Smuzhiyun * over right here, before we set the alarm.
433*4882a593Smuzhiyun */
434*4882a593Smuzhiyun
435*4882a593Smuzhiyun rtc_subtract_offset(rtc, &alarm->time);
436*4882a593Smuzhiyun
437*4882a593Smuzhiyun if (!rtc->ops)
438*4882a593Smuzhiyun err = -ENODEV;
439*4882a593Smuzhiyun else if (!rtc->ops->set_alarm)
440*4882a593Smuzhiyun err = -EINVAL;
441*4882a593Smuzhiyun else
442*4882a593Smuzhiyun err = rtc->ops->set_alarm(rtc->dev.parent, alarm);
443*4882a593Smuzhiyun
444*4882a593Smuzhiyun trace_rtc_set_alarm(rtc_tm_to_time64(&alarm->time), err);
445*4882a593Smuzhiyun return err;
446*4882a593Smuzhiyun }
447*4882a593Smuzhiyun
rtc_set_alarm(struct rtc_device * rtc,struct rtc_wkalrm * alarm)448*4882a593Smuzhiyun int rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
449*4882a593Smuzhiyun {
450*4882a593Smuzhiyun int err;
451*4882a593Smuzhiyun
452*4882a593Smuzhiyun if (!rtc->ops)
453*4882a593Smuzhiyun return -ENODEV;
454*4882a593Smuzhiyun else if (!rtc->ops->set_alarm)
455*4882a593Smuzhiyun return -EINVAL;
456*4882a593Smuzhiyun
457*4882a593Smuzhiyun err = rtc_valid_tm(&alarm->time);
458*4882a593Smuzhiyun if (err != 0)
459*4882a593Smuzhiyun return err;
460*4882a593Smuzhiyun
461*4882a593Smuzhiyun err = rtc_valid_range(rtc, &alarm->time);
462*4882a593Smuzhiyun if (err)
463*4882a593Smuzhiyun return err;
464*4882a593Smuzhiyun
465*4882a593Smuzhiyun err = mutex_lock_interruptible(&rtc->ops_lock);
466*4882a593Smuzhiyun if (err)
467*4882a593Smuzhiyun return err;
468*4882a593Smuzhiyun if (rtc->aie_timer.enabled)
469*4882a593Smuzhiyun rtc_timer_remove(rtc, &rtc->aie_timer);
470*4882a593Smuzhiyun
471*4882a593Smuzhiyun rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time);
472*4882a593Smuzhiyun rtc->aie_timer.period = 0;
473*4882a593Smuzhiyun if (alarm->enabled)
474*4882a593Smuzhiyun err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
475*4882a593Smuzhiyun
476*4882a593Smuzhiyun mutex_unlock(&rtc->ops_lock);
477*4882a593Smuzhiyun
478*4882a593Smuzhiyun return err;
479*4882a593Smuzhiyun }
480*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(rtc_set_alarm);
481*4882a593Smuzhiyun
482*4882a593Smuzhiyun /* Called once per device from rtc_device_register */
rtc_initialize_alarm(struct rtc_device * rtc,struct rtc_wkalrm * alarm)483*4882a593Smuzhiyun int rtc_initialize_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
484*4882a593Smuzhiyun {
485*4882a593Smuzhiyun int err;
486*4882a593Smuzhiyun struct rtc_time now;
487*4882a593Smuzhiyun
488*4882a593Smuzhiyun err = rtc_valid_tm(&alarm->time);
489*4882a593Smuzhiyun if (err != 0)
490*4882a593Smuzhiyun return err;
491*4882a593Smuzhiyun
492*4882a593Smuzhiyun err = rtc_read_time(rtc, &now);
493*4882a593Smuzhiyun if (err)
494*4882a593Smuzhiyun return err;
495*4882a593Smuzhiyun
496*4882a593Smuzhiyun err = mutex_lock_interruptible(&rtc->ops_lock);
497*4882a593Smuzhiyun if (err)
498*4882a593Smuzhiyun return err;
499*4882a593Smuzhiyun
500*4882a593Smuzhiyun rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time);
501*4882a593Smuzhiyun rtc->aie_timer.period = 0;
502*4882a593Smuzhiyun
503*4882a593Smuzhiyun /* Alarm has to be enabled & in the future for us to enqueue it */
504*4882a593Smuzhiyun if (alarm->enabled && (rtc_tm_to_ktime(now) <
505*4882a593Smuzhiyun rtc->aie_timer.node.expires)) {
506*4882a593Smuzhiyun rtc->aie_timer.enabled = 1;
507*4882a593Smuzhiyun timerqueue_add(&rtc->timerqueue, &rtc->aie_timer.node);
508*4882a593Smuzhiyun trace_rtc_timer_enqueue(&rtc->aie_timer);
509*4882a593Smuzhiyun }
510*4882a593Smuzhiyun mutex_unlock(&rtc->ops_lock);
511*4882a593Smuzhiyun return err;
512*4882a593Smuzhiyun }
513*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(rtc_initialize_alarm);
514*4882a593Smuzhiyun
rtc_alarm_irq_enable(struct rtc_device * rtc,unsigned int enabled)515*4882a593Smuzhiyun int rtc_alarm_irq_enable(struct rtc_device *rtc, unsigned int enabled)
516*4882a593Smuzhiyun {
517*4882a593Smuzhiyun int err;
518*4882a593Smuzhiyun
519*4882a593Smuzhiyun err = mutex_lock_interruptible(&rtc->ops_lock);
520*4882a593Smuzhiyun if (err)
521*4882a593Smuzhiyun return err;
522*4882a593Smuzhiyun
523*4882a593Smuzhiyun if (rtc->aie_timer.enabled != enabled) {
524*4882a593Smuzhiyun if (enabled)
525*4882a593Smuzhiyun err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
526*4882a593Smuzhiyun else
527*4882a593Smuzhiyun rtc_timer_remove(rtc, &rtc->aie_timer);
528*4882a593Smuzhiyun }
529*4882a593Smuzhiyun
530*4882a593Smuzhiyun if (err)
531*4882a593Smuzhiyun /* nothing */;
532*4882a593Smuzhiyun else if (!rtc->ops)
533*4882a593Smuzhiyun err = -ENODEV;
534*4882a593Smuzhiyun else if (!rtc->ops->alarm_irq_enable)
535*4882a593Smuzhiyun err = -EINVAL;
536*4882a593Smuzhiyun else
537*4882a593Smuzhiyun err = rtc->ops->alarm_irq_enable(rtc->dev.parent, enabled);
538*4882a593Smuzhiyun
539*4882a593Smuzhiyun mutex_unlock(&rtc->ops_lock);
540*4882a593Smuzhiyun
541*4882a593Smuzhiyun trace_rtc_alarm_irq_enable(enabled, err);
542*4882a593Smuzhiyun return err;
543*4882a593Smuzhiyun }
544*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(rtc_alarm_irq_enable);
545*4882a593Smuzhiyun
rtc_update_irq_enable(struct rtc_device * rtc,unsigned int enabled)546*4882a593Smuzhiyun int rtc_update_irq_enable(struct rtc_device *rtc, unsigned int enabled)
547*4882a593Smuzhiyun {
548*4882a593Smuzhiyun int rc = 0, err;
549*4882a593Smuzhiyun
550*4882a593Smuzhiyun err = mutex_lock_interruptible(&rtc->ops_lock);
551*4882a593Smuzhiyun if (err)
552*4882a593Smuzhiyun return err;
553*4882a593Smuzhiyun
554*4882a593Smuzhiyun #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
555*4882a593Smuzhiyun if (enabled == 0 && rtc->uie_irq_active) {
556*4882a593Smuzhiyun mutex_unlock(&rtc->ops_lock);
557*4882a593Smuzhiyun return rtc_dev_update_irq_enable_emul(rtc, 0);
558*4882a593Smuzhiyun }
559*4882a593Smuzhiyun #endif
560*4882a593Smuzhiyun /* make sure we're changing state */
561*4882a593Smuzhiyun if (rtc->uie_rtctimer.enabled == enabled)
562*4882a593Smuzhiyun goto out;
563*4882a593Smuzhiyun
564*4882a593Smuzhiyun if (rtc->uie_unsupported) {
565*4882a593Smuzhiyun err = -EINVAL;
566*4882a593Smuzhiyun goto out;
567*4882a593Smuzhiyun }
568*4882a593Smuzhiyun
569*4882a593Smuzhiyun if (enabled) {
570*4882a593Smuzhiyun struct rtc_time tm;
571*4882a593Smuzhiyun ktime_t now, onesec;
572*4882a593Smuzhiyun
573*4882a593Smuzhiyun rc = __rtc_read_time(rtc, &tm);
574*4882a593Smuzhiyun if (rc)
575*4882a593Smuzhiyun goto out;
576*4882a593Smuzhiyun onesec = ktime_set(1, 0);
577*4882a593Smuzhiyun now = rtc_tm_to_ktime(tm);
578*4882a593Smuzhiyun rtc->uie_rtctimer.node.expires = ktime_add(now, onesec);
579*4882a593Smuzhiyun rtc->uie_rtctimer.period = ktime_set(1, 0);
580*4882a593Smuzhiyun err = rtc_timer_enqueue(rtc, &rtc->uie_rtctimer);
581*4882a593Smuzhiyun } else {
582*4882a593Smuzhiyun rtc_timer_remove(rtc, &rtc->uie_rtctimer);
583*4882a593Smuzhiyun }
584*4882a593Smuzhiyun
585*4882a593Smuzhiyun out:
586*4882a593Smuzhiyun mutex_unlock(&rtc->ops_lock);
587*4882a593Smuzhiyun
588*4882a593Smuzhiyun /*
589*4882a593Smuzhiyun * __rtc_read_time() failed, this probably means that the RTC time has
590*4882a593Smuzhiyun * never been set or less probably there is a transient error on the
591*4882a593Smuzhiyun * bus. In any case, avoid enabling emulation has this will fail when
592*4882a593Smuzhiyun * reading the time too.
593*4882a593Smuzhiyun */
594*4882a593Smuzhiyun if (rc)
595*4882a593Smuzhiyun return rc;
596*4882a593Smuzhiyun
597*4882a593Smuzhiyun #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
598*4882a593Smuzhiyun /*
599*4882a593Smuzhiyun * Enable emulation if the driver returned -EINVAL to signal that it has
600*4882a593Smuzhiyun * been configured without interrupts or they are not available at the
601*4882a593Smuzhiyun * moment.
602*4882a593Smuzhiyun */
603*4882a593Smuzhiyun if (err == -EINVAL)
604*4882a593Smuzhiyun err = rtc_dev_update_irq_enable_emul(rtc, enabled);
605*4882a593Smuzhiyun #endif
606*4882a593Smuzhiyun return err;
607*4882a593Smuzhiyun }
608*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(rtc_update_irq_enable);
609*4882a593Smuzhiyun
610*4882a593Smuzhiyun /**
611*4882a593Smuzhiyun * rtc_handle_legacy_irq - AIE, UIE and PIE event hook
612*4882a593Smuzhiyun * @rtc: pointer to the rtc device
613*4882a593Smuzhiyun * @num: number of occurence of the event
614*4882a593Smuzhiyun * @mode: type of the event, RTC_AF, RTC_UF of RTC_PF
615*4882a593Smuzhiyun *
616*4882a593Smuzhiyun * This function is called when an AIE, UIE or PIE mode interrupt
617*4882a593Smuzhiyun * has occurred (or been emulated).
618*4882a593Smuzhiyun *
619*4882a593Smuzhiyun */
rtc_handle_legacy_irq(struct rtc_device * rtc,int num,int mode)620*4882a593Smuzhiyun void rtc_handle_legacy_irq(struct rtc_device *rtc, int num, int mode)
621*4882a593Smuzhiyun {
622*4882a593Smuzhiyun unsigned long flags;
623*4882a593Smuzhiyun
624*4882a593Smuzhiyun /* mark one irq of the appropriate mode */
625*4882a593Smuzhiyun spin_lock_irqsave(&rtc->irq_lock, flags);
626*4882a593Smuzhiyun rtc->irq_data = (rtc->irq_data + (num << 8)) | (RTC_IRQF | mode);
627*4882a593Smuzhiyun spin_unlock_irqrestore(&rtc->irq_lock, flags);
628*4882a593Smuzhiyun
629*4882a593Smuzhiyun wake_up_interruptible(&rtc->irq_queue);
630*4882a593Smuzhiyun kill_fasync(&rtc->async_queue, SIGIO, POLL_IN);
631*4882a593Smuzhiyun }
632*4882a593Smuzhiyun
633*4882a593Smuzhiyun /**
634*4882a593Smuzhiyun * rtc_aie_update_irq - AIE mode rtctimer hook
635*4882a593Smuzhiyun * @rtc: pointer to the rtc_device
636*4882a593Smuzhiyun *
637*4882a593Smuzhiyun * This functions is called when the aie_timer expires.
638*4882a593Smuzhiyun */
rtc_aie_update_irq(struct rtc_device * rtc)639*4882a593Smuzhiyun void rtc_aie_update_irq(struct rtc_device *rtc)
640*4882a593Smuzhiyun {
641*4882a593Smuzhiyun rtc_handle_legacy_irq(rtc, 1, RTC_AF);
642*4882a593Smuzhiyun }
643*4882a593Smuzhiyun
644*4882a593Smuzhiyun /**
645*4882a593Smuzhiyun * rtc_uie_update_irq - UIE mode rtctimer hook
646*4882a593Smuzhiyun * @rtc: pointer to the rtc_device
647*4882a593Smuzhiyun *
648*4882a593Smuzhiyun * This functions is called when the uie_timer expires.
649*4882a593Smuzhiyun */
rtc_uie_update_irq(struct rtc_device * rtc)650*4882a593Smuzhiyun void rtc_uie_update_irq(struct rtc_device *rtc)
651*4882a593Smuzhiyun {
652*4882a593Smuzhiyun rtc_handle_legacy_irq(rtc, 1, RTC_UF);
653*4882a593Smuzhiyun }
654*4882a593Smuzhiyun
655*4882a593Smuzhiyun /**
656*4882a593Smuzhiyun * rtc_pie_update_irq - PIE mode hrtimer hook
657*4882a593Smuzhiyun * @timer: pointer to the pie mode hrtimer
658*4882a593Smuzhiyun *
659*4882a593Smuzhiyun * This function is used to emulate PIE mode interrupts
660*4882a593Smuzhiyun * using an hrtimer. This function is called when the periodic
661*4882a593Smuzhiyun * hrtimer expires.
662*4882a593Smuzhiyun */
rtc_pie_update_irq(struct hrtimer * timer)663*4882a593Smuzhiyun enum hrtimer_restart rtc_pie_update_irq(struct hrtimer *timer)
664*4882a593Smuzhiyun {
665*4882a593Smuzhiyun struct rtc_device *rtc;
666*4882a593Smuzhiyun ktime_t period;
667*4882a593Smuzhiyun u64 count;
668*4882a593Smuzhiyun
669*4882a593Smuzhiyun rtc = container_of(timer, struct rtc_device, pie_timer);
670*4882a593Smuzhiyun
671*4882a593Smuzhiyun period = NSEC_PER_SEC / rtc->irq_freq;
672*4882a593Smuzhiyun count = hrtimer_forward_now(timer, period);
673*4882a593Smuzhiyun
674*4882a593Smuzhiyun rtc_handle_legacy_irq(rtc, count, RTC_PF);
675*4882a593Smuzhiyun
676*4882a593Smuzhiyun return HRTIMER_RESTART;
677*4882a593Smuzhiyun }
678*4882a593Smuzhiyun
679*4882a593Smuzhiyun /**
680*4882a593Smuzhiyun * rtc_update_irq - Triggered when a RTC interrupt occurs.
681*4882a593Smuzhiyun * @rtc: the rtc device
682*4882a593Smuzhiyun * @num: how many irqs are being reported (usually one)
683*4882a593Smuzhiyun * @events: mask of RTC_IRQF with one or more of RTC_PF, RTC_AF, RTC_UF
684*4882a593Smuzhiyun * Context: any
685*4882a593Smuzhiyun */
rtc_update_irq(struct rtc_device * rtc,unsigned long num,unsigned long events)686*4882a593Smuzhiyun void rtc_update_irq(struct rtc_device *rtc,
687*4882a593Smuzhiyun unsigned long num, unsigned long events)
688*4882a593Smuzhiyun {
689*4882a593Smuzhiyun if (IS_ERR_OR_NULL(rtc))
690*4882a593Smuzhiyun return;
691*4882a593Smuzhiyun
692*4882a593Smuzhiyun pm_stay_awake(rtc->dev.parent);
693*4882a593Smuzhiyun schedule_work(&rtc->irqwork);
694*4882a593Smuzhiyun }
695*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(rtc_update_irq);
696*4882a593Smuzhiyun
rtc_class_open(const char * name)697*4882a593Smuzhiyun struct rtc_device *rtc_class_open(const char *name)
698*4882a593Smuzhiyun {
699*4882a593Smuzhiyun struct device *dev;
700*4882a593Smuzhiyun struct rtc_device *rtc = NULL;
701*4882a593Smuzhiyun
702*4882a593Smuzhiyun dev = class_find_device_by_name(rtc_class, name);
703*4882a593Smuzhiyun if (dev)
704*4882a593Smuzhiyun rtc = to_rtc_device(dev);
705*4882a593Smuzhiyun
706*4882a593Smuzhiyun if (rtc) {
707*4882a593Smuzhiyun if (!try_module_get(rtc->owner)) {
708*4882a593Smuzhiyun put_device(dev);
709*4882a593Smuzhiyun rtc = NULL;
710*4882a593Smuzhiyun }
711*4882a593Smuzhiyun }
712*4882a593Smuzhiyun
713*4882a593Smuzhiyun return rtc;
714*4882a593Smuzhiyun }
715*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(rtc_class_open);
716*4882a593Smuzhiyun
rtc_class_close(struct rtc_device * rtc)717*4882a593Smuzhiyun void rtc_class_close(struct rtc_device *rtc)
718*4882a593Smuzhiyun {
719*4882a593Smuzhiyun module_put(rtc->owner);
720*4882a593Smuzhiyun put_device(&rtc->dev);
721*4882a593Smuzhiyun }
722*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(rtc_class_close);
723*4882a593Smuzhiyun
rtc_update_hrtimer(struct rtc_device * rtc,int enabled)724*4882a593Smuzhiyun static int rtc_update_hrtimer(struct rtc_device *rtc, int enabled)
725*4882a593Smuzhiyun {
726*4882a593Smuzhiyun /*
727*4882a593Smuzhiyun * We always cancel the timer here first, because otherwise
728*4882a593Smuzhiyun * we could run into BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
729*4882a593Smuzhiyun * when we manage to start the timer before the callback
730*4882a593Smuzhiyun * returns HRTIMER_RESTART.
731*4882a593Smuzhiyun *
732*4882a593Smuzhiyun * We cannot use hrtimer_cancel() here as a running callback
733*4882a593Smuzhiyun * could be blocked on rtc->irq_task_lock and hrtimer_cancel()
734*4882a593Smuzhiyun * would spin forever.
735*4882a593Smuzhiyun */
736*4882a593Smuzhiyun if (hrtimer_try_to_cancel(&rtc->pie_timer) < 0)
737*4882a593Smuzhiyun return -1;
738*4882a593Smuzhiyun
739*4882a593Smuzhiyun if (enabled) {
740*4882a593Smuzhiyun ktime_t period = NSEC_PER_SEC / rtc->irq_freq;
741*4882a593Smuzhiyun
742*4882a593Smuzhiyun hrtimer_start(&rtc->pie_timer, period, HRTIMER_MODE_REL);
743*4882a593Smuzhiyun }
744*4882a593Smuzhiyun return 0;
745*4882a593Smuzhiyun }
746*4882a593Smuzhiyun
747*4882a593Smuzhiyun /**
748*4882a593Smuzhiyun * rtc_irq_set_state - enable/disable 2^N Hz periodic IRQs
749*4882a593Smuzhiyun * @rtc: the rtc device
750*4882a593Smuzhiyun * @enabled: true to enable periodic IRQs
751*4882a593Smuzhiyun * Context: any
752*4882a593Smuzhiyun *
753*4882a593Smuzhiyun * Note that rtc_irq_set_freq() should previously have been used to
754*4882a593Smuzhiyun * specify the desired frequency of periodic IRQ.
755*4882a593Smuzhiyun */
rtc_irq_set_state(struct rtc_device * rtc,int enabled)756*4882a593Smuzhiyun int rtc_irq_set_state(struct rtc_device *rtc, int enabled)
757*4882a593Smuzhiyun {
758*4882a593Smuzhiyun int err = 0;
759*4882a593Smuzhiyun
760*4882a593Smuzhiyun while (rtc_update_hrtimer(rtc, enabled) < 0)
761*4882a593Smuzhiyun cpu_relax();
762*4882a593Smuzhiyun
763*4882a593Smuzhiyun rtc->pie_enabled = enabled;
764*4882a593Smuzhiyun
765*4882a593Smuzhiyun trace_rtc_irq_set_state(enabled, err);
766*4882a593Smuzhiyun return err;
767*4882a593Smuzhiyun }
768*4882a593Smuzhiyun
769*4882a593Smuzhiyun /**
770*4882a593Smuzhiyun * rtc_irq_set_freq - set 2^N Hz periodic IRQ frequency for IRQ
771*4882a593Smuzhiyun * @rtc: the rtc device
772*4882a593Smuzhiyun * @freq: positive frequency
773*4882a593Smuzhiyun * Context: any
774*4882a593Smuzhiyun *
775*4882a593Smuzhiyun * Note that rtc_irq_set_state() is used to enable or disable the
776*4882a593Smuzhiyun * periodic IRQs.
777*4882a593Smuzhiyun */
rtc_irq_set_freq(struct rtc_device * rtc,int freq)778*4882a593Smuzhiyun int rtc_irq_set_freq(struct rtc_device *rtc, int freq)
779*4882a593Smuzhiyun {
780*4882a593Smuzhiyun int err = 0;
781*4882a593Smuzhiyun
782*4882a593Smuzhiyun if (freq <= 0 || freq > RTC_MAX_FREQ)
783*4882a593Smuzhiyun return -EINVAL;
784*4882a593Smuzhiyun
785*4882a593Smuzhiyun rtc->irq_freq = freq;
786*4882a593Smuzhiyun while (rtc->pie_enabled && rtc_update_hrtimer(rtc, 1) < 0)
787*4882a593Smuzhiyun cpu_relax();
788*4882a593Smuzhiyun
789*4882a593Smuzhiyun trace_rtc_irq_set_freq(freq, err);
790*4882a593Smuzhiyun return err;
791*4882a593Smuzhiyun }
792*4882a593Smuzhiyun
793*4882a593Smuzhiyun /**
794*4882a593Smuzhiyun * rtc_timer_enqueue - Adds a rtc_timer to the rtc_device timerqueue
795*4882a593Smuzhiyun * @rtc: rtc device
796*4882a593Smuzhiyun * @timer: timer being added.
797*4882a593Smuzhiyun *
798*4882a593Smuzhiyun * Enqueues a timer onto the rtc devices timerqueue and sets
799*4882a593Smuzhiyun * the next alarm event appropriately.
800*4882a593Smuzhiyun *
801*4882a593Smuzhiyun * Sets the enabled bit on the added timer.
802*4882a593Smuzhiyun *
803*4882a593Smuzhiyun * Must hold ops_lock for proper serialization of timerqueue
804*4882a593Smuzhiyun */
rtc_timer_enqueue(struct rtc_device * rtc,struct rtc_timer * timer)805*4882a593Smuzhiyun static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer)
806*4882a593Smuzhiyun {
807*4882a593Smuzhiyun struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue);
808*4882a593Smuzhiyun struct rtc_time tm;
809*4882a593Smuzhiyun ktime_t now;
810*4882a593Smuzhiyun int err;
811*4882a593Smuzhiyun
812*4882a593Smuzhiyun err = __rtc_read_time(rtc, &tm);
813*4882a593Smuzhiyun if (err)
814*4882a593Smuzhiyun return err;
815*4882a593Smuzhiyun
816*4882a593Smuzhiyun timer->enabled = 1;
817*4882a593Smuzhiyun now = rtc_tm_to_ktime(tm);
818*4882a593Smuzhiyun
819*4882a593Smuzhiyun /* Skip over expired timers */
820*4882a593Smuzhiyun while (next) {
821*4882a593Smuzhiyun if (next->expires >= now)
822*4882a593Smuzhiyun break;
823*4882a593Smuzhiyun next = timerqueue_iterate_next(next);
824*4882a593Smuzhiyun }
825*4882a593Smuzhiyun
826*4882a593Smuzhiyun timerqueue_add(&rtc->timerqueue, &timer->node);
827*4882a593Smuzhiyun trace_rtc_timer_enqueue(timer);
828*4882a593Smuzhiyun if (!next || ktime_before(timer->node.expires, next->expires)) {
829*4882a593Smuzhiyun struct rtc_wkalrm alarm;
830*4882a593Smuzhiyun
831*4882a593Smuzhiyun alarm.time = rtc_ktime_to_tm(timer->node.expires);
832*4882a593Smuzhiyun alarm.enabled = 1;
833*4882a593Smuzhiyun err = __rtc_set_alarm(rtc, &alarm);
834*4882a593Smuzhiyun if (err == -ETIME) {
835*4882a593Smuzhiyun pm_stay_awake(rtc->dev.parent);
836*4882a593Smuzhiyun schedule_work(&rtc->irqwork);
837*4882a593Smuzhiyun } else if (err) {
838*4882a593Smuzhiyun timerqueue_del(&rtc->timerqueue, &timer->node);
839*4882a593Smuzhiyun trace_rtc_timer_dequeue(timer);
840*4882a593Smuzhiyun timer->enabled = 0;
841*4882a593Smuzhiyun return err;
842*4882a593Smuzhiyun }
843*4882a593Smuzhiyun }
844*4882a593Smuzhiyun return 0;
845*4882a593Smuzhiyun }
846*4882a593Smuzhiyun
rtc_alarm_disable(struct rtc_device * rtc)847*4882a593Smuzhiyun static void rtc_alarm_disable(struct rtc_device *rtc)
848*4882a593Smuzhiyun {
849*4882a593Smuzhiyun if (!rtc->ops || !rtc->ops->alarm_irq_enable)
850*4882a593Smuzhiyun return;
851*4882a593Smuzhiyun
852*4882a593Smuzhiyun rtc->ops->alarm_irq_enable(rtc->dev.parent, false);
853*4882a593Smuzhiyun trace_rtc_alarm_irq_enable(0, 0);
854*4882a593Smuzhiyun }
855*4882a593Smuzhiyun
856*4882a593Smuzhiyun /**
857*4882a593Smuzhiyun * rtc_timer_remove - Removes a rtc_timer from the rtc_device timerqueue
858*4882a593Smuzhiyun * @rtc: rtc device
859*4882a593Smuzhiyun * @timer: timer being removed.
860*4882a593Smuzhiyun *
861*4882a593Smuzhiyun * Removes a timer onto the rtc devices timerqueue and sets
862*4882a593Smuzhiyun * the next alarm event appropriately.
863*4882a593Smuzhiyun *
864*4882a593Smuzhiyun * Clears the enabled bit on the removed timer.
865*4882a593Smuzhiyun *
866*4882a593Smuzhiyun * Must hold ops_lock for proper serialization of timerqueue
867*4882a593Smuzhiyun */
rtc_timer_remove(struct rtc_device * rtc,struct rtc_timer * timer)868*4882a593Smuzhiyun static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer)
869*4882a593Smuzhiyun {
870*4882a593Smuzhiyun struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue);
871*4882a593Smuzhiyun
872*4882a593Smuzhiyun timerqueue_del(&rtc->timerqueue, &timer->node);
873*4882a593Smuzhiyun trace_rtc_timer_dequeue(timer);
874*4882a593Smuzhiyun timer->enabled = 0;
875*4882a593Smuzhiyun if (next == &timer->node) {
876*4882a593Smuzhiyun struct rtc_wkalrm alarm;
877*4882a593Smuzhiyun int err;
878*4882a593Smuzhiyun
879*4882a593Smuzhiyun next = timerqueue_getnext(&rtc->timerqueue);
880*4882a593Smuzhiyun if (!next) {
881*4882a593Smuzhiyun rtc_alarm_disable(rtc);
882*4882a593Smuzhiyun return;
883*4882a593Smuzhiyun }
884*4882a593Smuzhiyun alarm.time = rtc_ktime_to_tm(next->expires);
885*4882a593Smuzhiyun alarm.enabled = 1;
886*4882a593Smuzhiyun err = __rtc_set_alarm(rtc, &alarm);
887*4882a593Smuzhiyun if (err == -ETIME) {
888*4882a593Smuzhiyun pm_stay_awake(rtc->dev.parent);
889*4882a593Smuzhiyun schedule_work(&rtc->irqwork);
890*4882a593Smuzhiyun }
891*4882a593Smuzhiyun }
892*4882a593Smuzhiyun }
893*4882a593Smuzhiyun
894*4882a593Smuzhiyun /**
895*4882a593Smuzhiyun * rtc_timer_do_work - Expires rtc timers
896*4882a593Smuzhiyun * @work: work item
897*4882a593Smuzhiyun *
898*4882a593Smuzhiyun * Expires rtc timers. Reprograms next alarm event if needed.
899*4882a593Smuzhiyun * Called via worktask.
900*4882a593Smuzhiyun *
901*4882a593Smuzhiyun * Serializes access to timerqueue via ops_lock mutex
902*4882a593Smuzhiyun */
rtc_timer_do_work(struct work_struct * work)903*4882a593Smuzhiyun void rtc_timer_do_work(struct work_struct *work)
904*4882a593Smuzhiyun {
905*4882a593Smuzhiyun struct rtc_timer *timer;
906*4882a593Smuzhiyun struct timerqueue_node *next;
907*4882a593Smuzhiyun ktime_t now;
908*4882a593Smuzhiyun struct rtc_time tm;
909*4882a593Smuzhiyun
910*4882a593Smuzhiyun struct rtc_device *rtc =
911*4882a593Smuzhiyun container_of(work, struct rtc_device, irqwork);
912*4882a593Smuzhiyun
913*4882a593Smuzhiyun mutex_lock(&rtc->ops_lock);
914*4882a593Smuzhiyun again:
915*4882a593Smuzhiyun __rtc_read_time(rtc, &tm);
916*4882a593Smuzhiyun now = rtc_tm_to_ktime(tm);
917*4882a593Smuzhiyun while ((next = timerqueue_getnext(&rtc->timerqueue))) {
918*4882a593Smuzhiyun if (next->expires > now)
919*4882a593Smuzhiyun break;
920*4882a593Smuzhiyun
921*4882a593Smuzhiyun /* expire timer */
922*4882a593Smuzhiyun timer = container_of(next, struct rtc_timer, node);
923*4882a593Smuzhiyun timerqueue_del(&rtc->timerqueue, &timer->node);
924*4882a593Smuzhiyun trace_rtc_timer_dequeue(timer);
925*4882a593Smuzhiyun timer->enabled = 0;
926*4882a593Smuzhiyun if (timer->func)
927*4882a593Smuzhiyun timer->func(timer->rtc);
928*4882a593Smuzhiyun
929*4882a593Smuzhiyun trace_rtc_timer_fired(timer);
930*4882a593Smuzhiyun /* Re-add/fwd periodic timers */
931*4882a593Smuzhiyun if (ktime_to_ns(timer->period)) {
932*4882a593Smuzhiyun timer->node.expires = ktime_add(timer->node.expires,
933*4882a593Smuzhiyun timer->period);
934*4882a593Smuzhiyun timer->enabled = 1;
935*4882a593Smuzhiyun timerqueue_add(&rtc->timerqueue, &timer->node);
936*4882a593Smuzhiyun trace_rtc_timer_enqueue(timer);
937*4882a593Smuzhiyun }
938*4882a593Smuzhiyun }
939*4882a593Smuzhiyun
940*4882a593Smuzhiyun /* Set next alarm */
941*4882a593Smuzhiyun if (next) {
942*4882a593Smuzhiyun struct rtc_wkalrm alarm;
943*4882a593Smuzhiyun int err;
944*4882a593Smuzhiyun int retry = 3;
945*4882a593Smuzhiyun
946*4882a593Smuzhiyun alarm.time = rtc_ktime_to_tm(next->expires);
947*4882a593Smuzhiyun alarm.enabled = 1;
948*4882a593Smuzhiyun reprogram:
949*4882a593Smuzhiyun err = __rtc_set_alarm(rtc, &alarm);
950*4882a593Smuzhiyun if (err == -ETIME) {
951*4882a593Smuzhiyun goto again;
952*4882a593Smuzhiyun } else if (err) {
953*4882a593Smuzhiyun if (retry-- > 0)
954*4882a593Smuzhiyun goto reprogram;
955*4882a593Smuzhiyun
956*4882a593Smuzhiyun timer = container_of(next, struct rtc_timer, node);
957*4882a593Smuzhiyun timerqueue_del(&rtc->timerqueue, &timer->node);
958*4882a593Smuzhiyun trace_rtc_timer_dequeue(timer);
959*4882a593Smuzhiyun timer->enabled = 0;
960*4882a593Smuzhiyun dev_err(&rtc->dev, "__rtc_set_alarm: err=%d\n", err);
961*4882a593Smuzhiyun goto again;
962*4882a593Smuzhiyun }
963*4882a593Smuzhiyun } else {
964*4882a593Smuzhiyun rtc_alarm_disable(rtc);
965*4882a593Smuzhiyun }
966*4882a593Smuzhiyun
967*4882a593Smuzhiyun pm_relax(rtc->dev.parent);
968*4882a593Smuzhiyun mutex_unlock(&rtc->ops_lock);
969*4882a593Smuzhiyun }
970*4882a593Smuzhiyun
971*4882a593Smuzhiyun /* rtc_timer_init - Initializes an rtc_timer
972*4882a593Smuzhiyun * @timer: timer to be intiialized
973*4882a593Smuzhiyun * @f: function pointer to be called when timer fires
974*4882a593Smuzhiyun * @rtc: pointer to the rtc_device
975*4882a593Smuzhiyun *
976*4882a593Smuzhiyun * Kernel interface to initializing an rtc_timer.
977*4882a593Smuzhiyun */
rtc_timer_init(struct rtc_timer * timer,void (* f)(struct rtc_device * r),struct rtc_device * rtc)978*4882a593Smuzhiyun void rtc_timer_init(struct rtc_timer *timer, void (*f)(struct rtc_device *r),
979*4882a593Smuzhiyun struct rtc_device *rtc)
980*4882a593Smuzhiyun {
981*4882a593Smuzhiyun timerqueue_init(&timer->node);
982*4882a593Smuzhiyun timer->enabled = 0;
983*4882a593Smuzhiyun timer->func = f;
984*4882a593Smuzhiyun timer->rtc = rtc;
985*4882a593Smuzhiyun }
986*4882a593Smuzhiyun
987*4882a593Smuzhiyun /* rtc_timer_start - Sets an rtc_timer to fire in the future
988*4882a593Smuzhiyun * @ rtc: rtc device to be used
989*4882a593Smuzhiyun * @ timer: timer being set
990*4882a593Smuzhiyun * @ expires: time at which to expire the timer
991*4882a593Smuzhiyun * @ period: period that the timer will recur
992*4882a593Smuzhiyun *
993*4882a593Smuzhiyun * Kernel interface to set an rtc_timer
994*4882a593Smuzhiyun */
rtc_timer_start(struct rtc_device * rtc,struct rtc_timer * timer,ktime_t expires,ktime_t period)995*4882a593Smuzhiyun int rtc_timer_start(struct rtc_device *rtc, struct rtc_timer *timer,
996*4882a593Smuzhiyun ktime_t expires, ktime_t period)
997*4882a593Smuzhiyun {
998*4882a593Smuzhiyun int ret = 0;
999*4882a593Smuzhiyun
1000*4882a593Smuzhiyun mutex_lock(&rtc->ops_lock);
1001*4882a593Smuzhiyun if (timer->enabled)
1002*4882a593Smuzhiyun rtc_timer_remove(rtc, timer);
1003*4882a593Smuzhiyun
1004*4882a593Smuzhiyun timer->node.expires = expires;
1005*4882a593Smuzhiyun timer->period = period;
1006*4882a593Smuzhiyun
1007*4882a593Smuzhiyun ret = rtc_timer_enqueue(rtc, timer);
1008*4882a593Smuzhiyun
1009*4882a593Smuzhiyun mutex_unlock(&rtc->ops_lock);
1010*4882a593Smuzhiyun return ret;
1011*4882a593Smuzhiyun }
1012*4882a593Smuzhiyun
1013*4882a593Smuzhiyun /* rtc_timer_cancel - Stops an rtc_timer
1014*4882a593Smuzhiyun * @ rtc: rtc device to be used
1015*4882a593Smuzhiyun * @ timer: timer being set
1016*4882a593Smuzhiyun *
1017*4882a593Smuzhiyun * Kernel interface to cancel an rtc_timer
1018*4882a593Smuzhiyun */
rtc_timer_cancel(struct rtc_device * rtc,struct rtc_timer * timer)1019*4882a593Smuzhiyun void rtc_timer_cancel(struct rtc_device *rtc, struct rtc_timer *timer)
1020*4882a593Smuzhiyun {
1021*4882a593Smuzhiyun mutex_lock(&rtc->ops_lock);
1022*4882a593Smuzhiyun if (timer->enabled)
1023*4882a593Smuzhiyun rtc_timer_remove(rtc, timer);
1024*4882a593Smuzhiyun mutex_unlock(&rtc->ops_lock);
1025*4882a593Smuzhiyun }
1026*4882a593Smuzhiyun
1027*4882a593Smuzhiyun /**
1028*4882a593Smuzhiyun * rtc_read_offset - Read the amount of rtc offset in parts per billion
1029*4882a593Smuzhiyun * @rtc: rtc device to be used
1030*4882a593Smuzhiyun * @offset: the offset in parts per billion
1031*4882a593Smuzhiyun *
1032*4882a593Smuzhiyun * see below for details.
1033*4882a593Smuzhiyun *
1034*4882a593Smuzhiyun * Kernel interface to read rtc clock offset
1035*4882a593Smuzhiyun * Returns 0 on success, or a negative number on error.
1036*4882a593Smuzhiyun * If read_offset() is not implemented for the rtc, return -EINVAL
1037*4882a593Smuzhiyun */
rtc_read_offset(struct rtc_device * rtc,long * offset)1038*4882a593Smuzhiyun int rtc_read_offset(struct rtc_device *rtc, long *offset)
1039*4882a593Smuzhiyun {
1040*4882a593Smuzhiyun int ret;
1041*4882a593Smuzhiyun
1042*4882a593Smuzhiyun if (!rtc->ops)
1043*4882a593Smuzhiyun return -ENODEV;
1044*4882a593Smuzhiyun
1045*4882a593Smuzhiyun if (!rtc->ops->read_offset)
1046*4882a593Smuzhiyun return -EINVAL;
1047*4882a593Smuzhiyun
1048*4882a593Smuzhiyun mutex_lock(&rtc->ops_lock);
1049*4882a593Smuzhiyun ret = rtc->ops->read_offset(rtc->dev.parent, offset);
1050*4882a593Smuzhiyun mutex_unlock(&rtc->ops_lock);
1051*4882a593Smuzhiyun
1052*4882a593Smuzhiyun trace_rtc_read_offset(*offset, ret);
1053*4882a593Smuzhiyun return ret;
1054*4882a593Smuzhiyun }
1055*4882a593Smuzhiyun
1056*4882a593Smuzhiyun /**
1057*4882a593Smuzhiyun * rtc_set_offset - Adjusts the duration of the average second
1058*4882a593Smuzhiyun * @rtc: rtc device to be used
1059*4882a593Smuzhiyun * @offset: the offset in parts per billion
1060*4882a593Smuzhiyun *
1061*4882a593Smuzhiyun * Some rtc's allow an adjustment to the average duration of a second
1062*4882a593Smuzhiyun * to compensate for differences in the actual clock rate due to temperature,
1063*4882a593Smuzhiyun * the crystal, capacitor, etc.
1064*4882a593Smuzhiyun *
1065*4882a593Smuzhiyun * The adjustment applied is as follows:
1066*4882a593Smuzhiyun * t = t0 * (1 + offset * 1e-9)
1067*4882a593Smuzhiyun * where t0 is the measured length of 1 RTC second with offset = 0
1068*4882a593Smuzhiyun *
1069*4882a593Smuzhiyun * Kernel interface to adjust an rtc clock offset.
1070*4882a593Smuzhiyun * Return 0 on success, or a negative number on error.
1071*4882a593Smuzhiyun * If the rtc offset is not setable (or not implemented), return -EINVAL
1072*4882a593Smuzhiyun */
rtc_set_offset(struct rtc_device * rtc,long offset)1073*4882a593Smuzhiyun int rtc_set_offset(struct rtc_device *rtc, long offset)
1074*4882a593Smuzhiyun {
1075*4882a593Smuzhiyun int ret;
1076*4882a593Smuzhiyun
1077*4882a593Smuzhiyun if (!rtc->ops)
1078*4882a593Smuzhiyun return -ENODEV;
1079*4882a593Smuzhiyun
1080*4882a593Smuzhiyun if (!rtc->ops->set_offset)
1081*4882a593Smuzhiyun return -EINVAL;
1082*4882a593Smuzhiyun
1083*4882a593Smuzhiyun mutex_lock(&rtc->ops_lock);
1084*4882a593Smuzhiyun ret = rtc->ops->set_offset(rtc->dev.parent, offset);
1085*4882a593Smuzhiyun mutex_unlock(&rtc->ops_lock);
1086*4882a593Smuzhiyun
1087*4882a593Smuzhiyun trace_rtc_set_offset(offset, ret);
1088*4882a593Smuzhiyun return ret;
1089*4882a593Smuzhiyun }
1090