blob: 28bd767e03a088d727c34b4205181805d5b6bf80 [file] [log] [blame]
Alexandre Bellonicdf75452019-03-13 23:02:48 +01001// SPDX-License-Identifier: GPL-2.0
Alessandro Zummo0c86edc2006-03-27 01:16:37 -08002/*
3 * RTC subsystem, interface functions
4 *
5 * Copyright (C) 2005 Tower Technologies
6 * Author: Alessandro Zummo <a.zummo@towertech.it>
7 *
8 * based on arch/arm/common/rtctime.c
Alexandre Bellonicdf75452019-03-13 23:02:48 +01009 */
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080010
11#include <linux/rtc.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040012#include <linux/sched.h>
Paul Gortmaker21138522011-05-27 09:57:25 -040013#include <linux/module.h>
David Brownell97144c62007-10-16 01:28:16 -070014#include <linux/log2.h>
John Stultz6610e082010-09-23 15:07:34 -070015#include <linux/workqueue.h>
16
Baolin Wang29a1f592017-12-14 13:31:43 +080017#define CREATE_TRACE_POINTS
18#include <trace/events/rtc.h>
19
John Stultzaa0be0f2011-01-20 15:26:12 -080020static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer);
21static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer);
22
Baolin Wang98951562018-01-08 14:04:50 +080023static void rtc_add_offset(struct rtc_device *rtc, struct rtc_time *tm)
24{
25 time64_t secs;
26
27 if (!rtc->offset_secs)
28 return;
29
30 secs = rtc_tm_to_time64(tm);
31
32 /*
33 * Since the reading time values from RTC device are always in the RTC
34 * original valid range, but we need to skip the overlapped region
35 * between expanded range and original range, which is no need to add
36 * the offset.
37 */
38 if ((rtc->start_secs > rtc->range_min && secs >= rtc->start_secs) ||
39 (rtc->start_secs < rtc->range_min &&
40 secs <= (rtc->start_secs + rtc->range_max - rtc->range_min)))
41 return;
42
43 rtc_time64_to_tm(secs + rtc->offset_secs, tm);
44}
45
46static void rtc_subtract_offset(struct rtc_device *rtc, struct rtc_time *tm)
47{
48 time64_t secs;
49
50 if (!rtc->offset_secs)
51 return;
52
53 secs = rtc_tm_to_time64(tm);
54
55 /*
56 * If the setting time values are in the valid range of RTC hardware
57 * device, then no need to subtract the offset when setting time to RTC
58 * device. Otherwise we need to subtract the offset to make the time
59 * values are valid for RTC hardware device.
60 */
61 if (secs >= rtc->range_min && secs <= rtc->range_max)
62 return;
63
64 rtc_time64_to_tm(secs - rtc->offset_secs, tm);
65}
66
Baolin Wang4c4e5df2018-01-08 14:04:49 +080067static int rtc_valid_range(struct rtc_device *rtc, struct rtc_time *tm)
68{
69 if (rtc->range_min != rtc->range_max) {
70 time64_t time = rtc_tm_to_time64(tm);
Baolin Wang98951562018-01-08 14:04:50 +080071 time64_t range_min = rtc->set_start_time ? rtc->start_secs :
72 rtc->range_min;
73 time64_t range_max = rtc->set_start_time ?
74 (rtc->start_secs + rtc->range_max - rtc->range_min) :
75 rtc->range_max;
Baolin Wang4c4e5df2018-01-08 14:04:49 +080076
Baolin Wang98951562018-01-08 14:04:50 +080077 if (time < range_min || time > range_max)
Baolin Wang4c4e5df2018-01-08 14:04:49 +080078 return -ERANGE;
79 }
80
81 return 0;
82}
83
John Stultz6610e082010-09-23 15:07:34 -070084static int __rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm)
85{
86 int err;
87 if (!rtc->ops)
88 err = -ENODEV;
89 else if (!rtc->ops->read_time)
90 err = -EINVAL;
91 else {
92 memset(tm, 0, sizeof(struct rtc_time));
93 err = rtc->ops->read_time(rtc->dev.parent, tm);
Hyogi Gim16682c862014-12-10 15:52:27 -080094 if (err < 0) {
Aaro Koskinend0bddb52015-04-16 12:45:51 -070095 dev_dbg(&rtc->dev, "read_time: fail to read: %d\n",
96 err);
Hyogi Gim16682c862014-12-10 15:52:27 -080097 return err;
98 }
99
Baolin Wang98951562018-01-08 14:04:50 +0800100 rtc_add_offset(rtc, tm);
101
Hyogi Gim16682c862014-12-10 15:52:27 -0800102 err = rtc_valid_tm(tm);
103 if (err < 0)
Aaro Koskinend0bddb52015-04-16 12:45:51 -0700104 dev_dbg(&rtc->dev, "read_time: rtc_time isn't valid\n");
John Stultz6610e082010-09-23 15:07:34 -0700105 }
106 return err;
107}
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800108
David Brownellab6a2d72007-05-08 00:33:30 -0700109int rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800110{
111 int err;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800112
113 err = mutex_lock_interruptible(&rtc->ops_lock);
114 if (err)
David Brownellb68bb262008-07-29 22:33:30 -0700115 return err;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800116
John Stultz6610e082010-09-23 15:07:34 -0700117 err = __rtc_read_time(rtc, tm);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800118 mutex_unlock(&rtc->ops_lock);
Baolin Wang29a1f592017-12-14 13:31:43 +0800119
120 trace_rtc_read_time(rtc_tm_to_time64(tm), err);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800121 return err;
122}
123EXPORT_SYMBOL_GPL(rtc_read_time);
124
David Brownellab6a2d72007-05-08 00:33:30 -0700125int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800126{
127 int err;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800128
129 err = rtc_valid_tm(tm);
130 if (err != 0)
131 return err;
132
Baolin Wang4c4e5df2018-01-08 14:04:49 +0800133 err = rtc_valid_range(rtc, tm);
134 if (err)
135 return err;
Alexandre Belloni71db0492018-02-17 14:58:40 +0100136
Baolin Wang98951562018-01-08 14:04:50 +0800137 rtc_subtract_offset(rtc, tm);
138
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800139 err = mutex_lock_interruptible(&rtc->ops_lock);
140 if (err)
David Brownellb68bb262008-07-29 22:33:30 -0700141 return err;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800142
143 if (!rtc->ops)
144 err = -ENODEV;
Alessandro Zummobbccf832009-01-06 14:42:21 -0800145 else if (rtc->ops->set_time)
David Brownellcd966202007-05-08 00:33:40 -0700146 err = rtc->ops->set_time(rtc->dev.parent, tm);
Xunlei Pang8e4ff1a2015-04-01 20:34:27 -0700147 else if (rtc->ops->set_mmss64) {
148 time64_t secs64 = rtc_tm_to_time64(tm);
149
150 err = rtc->ops->set_mmss64(rtc->dev.parent, secs64);
151 } else if (rtc->ops->set_mmss) {
Xunlei Pangbc10aa92015-01-22 02:31:51 +0000152 time64_t secs64 = rtc_tm_to_time64(tm);
153 err = rtc->ops->set_mmss(rtc->dev.parent, secs64);
Alessandro Zummobbccf832009-01-06 14:42:21 -0800154 } else
155 err = -EINVAL;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800156
Zoran Markovic14d0e342013-06-26 16:09:13 -0700157 pm_stay_awake(rtc->dev.parent);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800158 mutex_unlock(&rtc->ops_lock);
NeilBrown5f9679d2011-12-09 09:39:15 +1100159 /* A timer might have just expired */
160 schedule_work(&rtc->irqwork);
Baolin Wang29a1f592017-12-14 13:31:43 +0800161
162 trace_rtc_set_time(rtc_tm_to_time64(tm), err);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800163 return err;
164}
165EXPORT_SYMBOL_GPL(rtc_set_time);
166
John Stultzf44f7f92011-02-21 22:58:51 -0800167static int rtc_read_alarm_internal(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
168{
169 int err;
170
171 err = mutex_lock_interruptible(&rtc->ops_lock);
172 if (err)
173 return err;
174
175 if (rtc->ops == NULL)
176 err = -ENODEV;
177 else if (!rtc->ops->read_alarm)
178 err = -EINVAL;
179 else {
Uwe Kleine-Königd68778b2016-05-11 09:11:23 +0200180 alarm->enabled = 0;
181 alarm->pending = 0;
182 alarm->time.tm_sec = -1;
183 alarm->time.tm_min = -1;
184 alarm->time.tm_hour = -1;
185 alarm->time.tm_mday = -1;
186 alarm->time.tm_mon = -1;
187 alarm->time.tm_year = -1;
188 alarm->time.tm_wday = -1;
189 alarm->time.tm_yday = -1;
190 alarm->time.tm_isdst = -1;
John Stultzf44f7f92011-02-21 22:58:51 -0800191 err = rtc->ops->read_alarm(rtc->dev.parent, alarm);
192 }
193
194 mutex_unlock(&rtc->ops_lock);
Baolin Wang29a1f592017-12-14 13:31:43 +0800195
196 trace_rtc_read_alarm(rtc_tm_to_time64(&alarm->time), err);
John Stultzf44f7f92011-02-21 22:58:51 -0800197 return err;
198}
199
200int __rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
201{
202 int err;
203 struct rtc_time before, now;
204 int first_time = 1;
Xunlei Pangbc10aa92015-01-22 02:31:51 +0000205 time64_t t_now, t_alm;
John Stultzf44f7f92011-02-21 22:58:51 -0800206 enum { none, day, month, year } missing = none;
207 unsigned days;
208
209 /* The lower level RTC driver may return -1 in some fields,
210 * creating invalid alarm->time values, for reasons like:
211 *
212 * - The hardware may not be capable of filling them in;
213 * many alarms match only on time-of-day fields, not
214 * day/month/year calendar data.
215 *
216 * - Some hardware uses illegal values as "wildcard" match
217 * values, which non-Linux firmware (like a BIOS) may try
218 * to set up as e.g. "alarm 15 minutes after each hour".
219 * Linux uses only oneshot alarms.
220 *
221 * When we see that here, we deal with it by using values from
222 * a current RTC timestamp for any missing (-1) values. The
223 * RTC driver prevents "periodic alarm" modes.
224 *
225 * But this can be racey, because some fields of the RTC timestamp
226 * may have wrapped in the interval since we read the RTC alarm,
227 * which would lead to us inserting inconsistent values in place
228 * of the -1 fields.
229 *
230 * Reading the alarm and timestamp in the reverse sequence
231 * would have the same race condition, and not solve the issue.
232 *
233 * So, we must first read the RTC timestamp,
234 * then read the RTC alarm value,
235 * and then read a second RTC timestamp.
236 *
237 * If any fields of the second timestamp have changed
238 * when compared with the first timestamp, then we know
239 * our timestamp may be inconsistent with that used by
240 * the low-level rtc_read_alarm_internal() function.
241 *
242 * So, when the two timestamps disagree, we just loop and do
243 * the process again to get a fully consistent set of values.
244 *
245 * This could all instead be done in the lower level driver,
246 * but since more than one lower level RTC implementation needs it,
247 * then it's probably best best to do it here instead of there..
248 */
249
250 /* Get the "before" timestamp */
251 err = rtc_read_time(rtc, &before);
252 if (err < 0)
253 return err;
254 do {
255 if (!first_time)
256 memcpy(&before, &now, sizeof(struct rtc_time));
257 first_time = 0;
258
259 /* get the RTC alarm values, which may be incomplete */
260 err = rtc_read_alarm_internal(rtc, alarm);
261 if (err)
262 return err;
263
264 /* full-function RTCs won't have such missing fields */
Alexandre Bellonifd6792b2018-07-12 12:22:44 +0200265 if (rtc_valid_tm(&alarm->time) == 0) {
266 rtc_add_offset(rtc, &alarm->time);
John Stultzf44f7f92011-02-21 22:58:51 -0800267 return 0;
Alexandre Bellonifd6792b2018-07-12 12:22:44 +0200268 }
John Stultzf44f7f92011-02-21 22:58:51 -0800269
270 /* get the "after" timestamp, to detect wrapped fields */
271 err = rtc_read_time(rtc, &now);
272 if (err < 0)
273 return err;
274
275 /* note that tm_sec is a "don't care" value here: */
276 } while ( before.tm_min != now.tm_min
277 || before.tm_hour != now.tm_hour
278 || before.tm_mon != now.tm_mon
279 || before.tm_year != now.tm_year);
280
281 /* Fill in the missing alarm fields using the timestamp; we
282 * know there's at least one since alarm->time is invalid.
283 */
284 if (alarm->time.tm_sec == -1)
285 alarm->time.tm_sec = now.tm_sec;
286 if (alarm->time.tm_min == -1)
287 alarm->time.tm_min = now.tm_min;
288 if (alarm->time.tm_hour == -1)
289 alarm->time.tm_hour = now.tm_hour;
290
291 /* For simplicity, only support date rollover for now */
Ben Hutchingse74a8f22012-01-10 15:11:02 -0800292 if (alarm->time.tm_mday < 1 || alarm->time.tm_mday > 31) {
John Stultzf44f7f92011-02-21 22:58:51 -0800293 alarm->time.tm_mday = now.tm_mday;
294 missing = day;
295 }
Ben Hutchingse74a8f22012-01-10 15:11:02 -0800296 if ((unsigned)alarm->time.tm_mon >= 12) {
John Stultzf44f7f92011-02-21 22:58:51 -0800297 alarm->time.tm_mon = now.tm_mon;
298 if (missing == none)
299 missing = month;
300 }
301 if (alarm->time.tm_year == -1) {
302 alarm->time.tm_year = now.tm_year;
303 if (missing == none)
304 missing = year;
305 }
306
Vaibhav Jainda96aea2017-05-19 22:18:55 +0530307 /* Can't proceed if alarm is still invalid after replacing
308 * missing fields.
309 */
310 err = rtc_valid_tm(&alarm->time);
311 if (err)
312 goto done;
313
John Stultzf44f7f92011-02-21 22:58:51 -0800314 /* with luck, no rollover is needed */
Xunlei Pangbc10aa92015-01-22 02:31:51 +0000315 t_now = rtc_tm_to_time64(&now);
316 t_alm = rtc_tm_to_time64(&alarm->time);
John Stultzf44f7f92011-02-21 22:58:51 -0800317 if (t_now < t_alm)
318 goto done;
319
320 switch (missing) {
321
322 /* 24 hour rollover ... if it's now 10am Monday, an alarm that
323 * that will trigger at 5am will do so at 5am Tuesday, which
324 * could also be in the next month or year. This is a common
325 * case, especially for PCs.
326 */
327 case day:
328 dev_dbg(&rtc->dev, "alarm rollover: %s\n", "day");
329 t_alm += 24 * 60 * 60;
Xunlei Pangbc10aa92015-01-22 02:31:51 +0000330 rtc_time64_to_tm(t_alm, &alarm->time);
John Stultzf44f7f92011-02-21 22:58:51 -0800331 break;
332
333 /* Month rollover ... if it's the 31th, an alarm on the 3rd will
334 * be next month. An alarm matching on the 30th, 29th, or 28th
335 * may end up in the month after that! Many newer PCs support
336 * this type of alarm.
337 */
338 case month:
339 dev_dbg(&rtc->dev, "alarm rollover: %s\n", "month");
340 do {
341 if (alarm->time.tm_mon < 11)
342 alarm->time.tm_mon++;
343 else {
344 alarm->time.tm_mon = 0;
345 alarm->time.tm_year++;
346 }
347 days = rtc_month_days(alarm->time.tm_mon,
348 alarm->time.tm_year);
349 } while (days < alarm->time.tm_mday);
350 break;
351
352 /* Year rollover ... easy except for leap years! */
353 case year:
354 dev_dbg(&rtc->dev, "alarm rollover: %s\n", "year");
355 do {
356 alarm->time.tm_year++;
Ales Novakee1d9012014-06-06 14:35:39 -0700357 } while (!is_leap_year(alarm->time.tm_year + 1900)
358 && rtc_valid_tm(&alarm->time) != 0);
John Stultzf44f7f92011-02-21 22:58:51 -0800359 break;
360
361 default:
362 dev_warn(&rtc->dev, "alarm rollover not handled\n");
363 }
364
Ales Novakee1d9012014-06-06 14:35:39 -0700365 err = rtc_valid_tm(&alarm->time);
366
Vaibhav Jainda96aea2017-05-19 22:18:55 +0530367done:
Andy Shevchenko5548cbf2018-12-04 23:23:12 +0200368 if (err)
369 dev_warn(&rtc->dev, "invalid alarm value: %ptR\n", &alarm->time);
Ales Novakee1d9012014-06-06 14:35:39 -0700370
371 return err;
John Stultzf44f7f92011-02-21 22:58:51 -0800372}
373
John Stultz6610e082010-09-23 15:07:34 -0700374int rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800375{
376 int err;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800377
378 err = mutex_lock_interruptible(&rtc->ops_lock);
379 if (err)
David Brownellb68bb262008-07-29 22:33:30 -0700380 return err;
John Stultzd5553a52011-01-20 15:26:13 -0800381 if (rtc->ops == NULL)
382 err = -ENODEV;
383 else if (!rtc->ops->read_alarm)
384 err = -EINVAL;
385 else {
386 memset(alarm, 0, sizeof(struct rtc_wkalrm));
387 alarm->enabled = rtc->aie_timer.enabled;
John Stultz6610e082010-09-23 15:07:34 -0700388 alarm->time = rtc_ktime_to_tm(rtc->aie_timer.node.expires);
John Stultzd5553a52011-01-20 15:26:13 -0800389 }
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800390 mutex_unlock(&rtc->ops_lock);
Mark Lord0e36a9a2007-10-16 01:28:21 -0700391
Baolin Wang29a1f592017-12-14 13:31:43 +0800392 trace_rtc_read_alarm(rtc_tm_to_time64(&alarm->time), err);
John Stultzd5553a52011-01-20 15:26:13 -0800393 return err;
Mark Lord0e36a9a2007-10-16 01:28:21 -0700394}
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800395EXPORT_SYMBOL_GPL(rtc_read_alarm);
396
Mark Brownd576fe42011-06-01 11:13:16 +0100397static int __rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
John Stultz6610e082010-09-23 15:07:34 -0700398{
399 struct rtc_time tm;
Xunlei Pangbc10aa92015-01-22 02:31:51 +0000400 time64_t now, scheduled;
John Stultz6610e082010-09-23 15:07:34 -0700401 int err;
402
403 err = rtc_valid_tm(&alarm->time);
404 if (err)
405 return err;
Baolin Wang98951562018-01-08 14:04:50 +0800406
Xunlei Pangbc10aa92015-01-22 02:31:51 +0000407 scheduled = rtc_tm_to_time64(&alarm->time);
John Stultz6610e082010-09-23 15:07:34 -0700408
409 /* Make sure we're not setting alarms in the past */
410 err = __rtc_read_time(rtc, &tm);
Hyogi Gimca6dc2d2014-08-08 14:20:11 -0700411 if (err)
412 return err;
Xunlei Pangbc10aa92015-01-22 02:31:51 +0000413 now = rtc_tm_to_time64(&tm);
John Stultz6610e082010-09-23 15:07:34 -0700414 if (scheduled <= now)
415 return -ETIME;
416 /*
417 * XXX - We just checked to make sure the alarm time is not
418 * in the past, but there is still a race window where if
419 * the is alarm set for the next second and the second ticks
420 * over right here, before we set the alarm.
421 */
422
Alexandre Bellonifd6792b2018-07-12 12:22:44 +0200423 rtc_subtract_offset(rtc, &alarm->time);
424
Linus Torvalds157e8bf2012-01-03 17:32:13 -0800425 if (!rtc->ops)
426 err = -ENODEV;
427 else if (!rtc->ops->set_alarm)
428 err = -EINVAL;
429 else
430 err = rtc->ops->set_alarm(rtc->dev.parent, alarm);
431
Baolin Wang29a1f592017-12-14 13:31:43 +0800432 trace_rtc_set_alarm(rtc_tm_to_time64(&alarm->time), err);
Linus Torvalds157e8bf2012-01-03 17:32:13 -0800433 return err;
John Stultz6610e082010-09-23 15:07:34 -0700434}
435
David Brownellab6a2d72007-05-08 00:33:30 -0700436int rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800437{
438 int err;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800439
Alexandre Belloniabfdff42018-06-05 23:09:14 +0200440 if (!rtc->ops)
441 return -ENODEV;
442 else if (!rtc->ops->set_alarm)
443 return -EINVAL;
444
David Brownellf8245c22007-05-08 00:34:07 -0700445 err = rtc_valid_tm(&alarm->time);
446 if (err != 0)
447 return err;
448
Baolin Wang4c4e5df2018-01-08 14:04:49 +0800449 err = rtc_valid_range(rtc, &alarm->time);
450 if (err)
451 return err;
Alexandre Belloni71db0492018-02-17 14:58:40 +0100452
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800453 err = mutex_lock_interruptible(&rtc->ops_lock);
454 if (err)
David Brownellb68bb262008-07-29 22:33:30 -0700455 return err;
Sachin Kamat3ff2e132013-07-03 15:05:42 -0700456 if (rtc->aie_timer.enabled)
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100457 rtc_timer_remove(rtc, &rtc->aie_timer);
Sachin Kamat3ff2e132013-07-03 15:05:42 -0700458
John Stultz6610e082010-09-23 15:07:34 -0700459 rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time);
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100460 rtc->aie_timer.period = 0;
Sachin Kamat3ff2e132013-07-03 15:05:42 -0700461 if (alarm->enabled)
John Stultzaa0be0f2011-01-20 15:26:12 -0800462 err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
Sachin Kamat3ff2e132013-07-03 15:05:42 -0700463
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800464 mutex_unlock(&rtc->ops_lock);
Baolin Wang98951562018-01-08 14:04:50 +0800465
John Stultzaa0be0f2011-01-20 15:26:12 -0800466 return err;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800467}
468EXPORT_SYMBOL_GPL(rtc_set_alarm);
469
John Stultzf6d5b332011-03-29 18:00:27 -0700470/* Called once per device from rtc_device_register */
471int rtc_initialize_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
472{
473 int err;
John Stultzbd729d72012-01-05 15:21:19 -0800474 struct rtc_time now;
John Stultzf6d5b332011-03-29 18:00:27 -0700475
476 err = rtc_valid_tm(&alarm->time);
477 if (err != 0)
478 return err;
479
John Stultzbd729d72012-01-05 15:21:19 -0800480 err = rtc_read_time(rtc, &now);
481 if (err)
482 return err;
483
John Stultzf6d5b332011-03-29 18:00:27 -0700484 err = mutex_lock_interruptible(&rtc->ops_lock);
485 if (err)
486 return err;
487
488 rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time);
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100489 rtc->aie_timer.period = 0;
John Stultzbd729d72012-01-05 15:21:19 -0800490
Uwe Kleine-König6785b3b2016-07-02 17:28:12 +0200491 /* Alarm has to be enabled & in the future for us to enqueue it */
Thomas Gleixner2456e852016-12-25 11:38:40 +0100492 if (alarm->enabled && (rtc_tm_to_ktime(now) <
493 rtc->aie_timer.node.expires)) {
John Stultzbd729d72012-01-05 15:21:19 -0800494
John Stultzf6d5b332011-03-29 18:00:27 -0700495 rtc->aie_timer.enabled = 1;
496 timerqueue_add(&rtc->timerqueue, &rtc->aie_timer.node);
Baolin Wang29a1f592017-12-14 13:31:43 +0800497 trace_rtc_timer_enqueue(&rtc->aie_timer);
John Stultzf6d5b332011-03-29 18:00:27 -0700498 }
499 mutex_unlock(&rtc->ops_lock);
500 return err;
501}
502EXPORT_SYMBOL_GPL(rtc_initialize_alarm);
503
Alessandro Zummo099e6572009-01-04 12:00:54 -0800504int rtc_alarm_irq_enable(struct rtc_device *rtc, unsigned int enabled)
505{
506 int err = mutex_lock_interruptible(&rtc->ops_lock);
507 if (err)
508 return err;
509
John Stultz6610e082010-09-23 15:07:34 -0700510 if (rtc->aie_timer.enabled != enabled) {
John Stultzaa0be0f2011-01-20 15:26:12 -0800511 if (enabled)
512 err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
513 else
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100514 rtc_timer_remove(rtc, &rtc->aie_timer);
John Stultz6610e082010-09-23 15:07:34 -0700515 }
516
John Stultzaa0be0f2011-01-20 15:26:12 -0800517 if (err)
Uwe Kleine-König516373b2011-02-14 11:33:17 +0100518 /* nothing */;
519 else if (!rtc->ops)
Alessandro Zummo099e6572009-01-04 12:00:54 -0800520 err = -ENODEV;
521 else if (!rtc->ops->alarm_irq_enable)
522 err = -EINVAL;
523 else
524 err = rtc->ops->alarm_irq_enable(rtc->dev.parent, enabled);
525
526 mutex_unlock(&rtc->ops_lock);
Baolin Wang29a1f592017-12-14 13:31:43 +0800527
528 trace_rtc_alarm_irq_enable(enabled, err);
Alessandro Zummo099e6572009-01-04 12:00:54 -0800529 return err;
530}
531EXPORT_SYMBOL_GPL(rtc_alarm_irq_enable);
532
533int rtc_update_irq_enable(struct rtc_device *rtc, unsigned int enabled)
534{
535 int err = mutex_lock_interruptible(&rtc->ops_lock);
536 if (err)
537 return err;
538
John Stultz456d66e2011-02-11 18:15:23 -0800539#ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
540 if (enabled == 0 && rtc->uie_irq_active) {
541 mutex_unlock(&rtc->ops_lock);
542 return rtc_dev_update_irq_enable_emul(rtc, 0);
543 }
544#endif
John Stultz6610e082010-09-23 15:07:34 -0700545 /* make sure we're changing state */
546 if (rtc->uie_rtctimer.enabled == enabled)
547 goto out;
548
John Stultz4a649902012-03-06 17:16:09 -0800549 if (rtc->uie_unsupported) {
550 err = -EINVAL;
551 goto out;
552 }
553
John Stultz6610e082010-09-23 15:07:34 -0700554 if (enabled) {
555 struct rtc_time tm;
556 ktime_t now, onesec;
557
558 __rtc_read_time(rtc, &tm);
559 onesec = ktime_set(1, 0);
560 now = rtc_tm_to_ktime(tm);
561 rtc->uie_rtctimer.node.expires = ktime_add(now, onesec);
562 rtc->uie_rtctimer.period = ktime_set(1, 0);
John Stultzaa0be0f2011-01-20 15:26:12 -0800563 err = rtc_timer_enqueue(rtc, &rtc->uie_rtctimer);
564 } else
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100565 rtc_timer_remove(rtc, &rtc->uie_rtctimer);
Alessandro Zummo099e6572009-01-04 12:00:54 -0800566
John Stultz6610e082010-09-23 15:07:34 -0700567out:
Alessandro Zummo099e6572009-01-04 12:00:54 -0800568 mutex_unlock(&rtc->ops_lock);
John Stultz456d66e2011-02-11 18:15:23 -0800569#ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
570 /*
571 * Enable emulation if the driver did not provide
572 * the update_irq_enable function pointer or if returned
573 * -EINVAL to signal that it has been configured without
574 * interrupts or that are not available at the moment.
575 */
576 if (err == -EINVAL)
577 err = rtc_dev_update_irq_enable_emul(rtc, enabled);
578#endif
Alessandro Zummo099e6572009-01-04 12:00:54 -0800579 return err;
John Stultz6610e082010-09-23 15:07:34 -0700580
Alessandro Zummo099e6572009-01-04 12:00:54 -0800581}
582EXPORT_SYMBOL_GPL(rtc_update_irq_enable);
583
John Stultz6610e082010-09-23 15:07:34 -0700584
David Brownelld728b1e2006-11-25 11:09:28 -0800585/**
John Stultz6610e082010-09-23 15:07:34 -0700586 * rtc_handle_legacy_irq - AIE, UIE and PIE event hook
587 * @rtc: pointer to the rtc device
588 *
589 * This function is called when an AIE, UIE or PIE mode interrupt
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300590 * has occurred (or been emulated).
John Stultz6610e082010-09-23 15:07:34 -0700591 *
John Stultz6610e082010-09-23 15:07:34 -0700592 */
John Stultz456d66e2011-02-11 18:15:23 -0800593void rtc_handle_legacy_irq(struct rtc_device *rtc, int num, int mode)
John Stultz6610e082010-09-23 15:07:34 -0700594{
595 unsigned long flags;
596
597 /* mark one irq of the appropriate mode */
598 spin_lock_irqsave(&rtc->irq_lock, flags);
599 rtc->irq_data = (rtc->irq_data + (num << 8)) | (RTC_IRQF|mode);
600 spin_unlock_irqrestore(&rtc->irq_lock, flags);
601
John Stultz6610e082010-09-23 15:07:34 -0700602 wake_up_interruptible(&rtc->irq_queue);
603 kill_fasync(&rtc->async_queue, SIGIO, POLL_IN);
604}
605
606
607/**
608 * rtc_aie_update_irq - AIE mode rtctimer hook
Alexandre Belloni9a0320112018-12-18 22:11:26 +0100609 * @rtc: pointer to the rtc_device
John Stultz6610e082010-09-23 15:07:34 -0700610 *
611 * This functions is called when the aie_timer expires.
612 */
Alexandre Belloni9a0320112018-12-18 22:11:26 +0100613void rtc_aie_update_irq(struct rtc_device *rtc)
John Stultz6610e082010-09-23 15:07:34 -0700614{
John Stultz6610e082010-09-23 15:07:34 -0700615 rtc_handle_legacy_irq(rtc, 1, RTC_AF);
616}
617
618
619/**
620 * rtc_uie_update_irq - UIE mode rtctimer hook
Alexandre Belloni9a0320112018-12-18 22:11:26 +0100621 * @rtc: pointer to the rtc_device
John Stultz6610e082010-09-23 15:07:34 -0700622 *
623 * This functions is called when the uie_timer expires.
624 */
Alexandre Belloni9a0320112018-12-18 22:11:26 +0100625void rtc_uie_update_irq(struct rtc_device *rtc)
John Stultz6610e082010-09-23 15:07:34 -0700626{
John Stultz6610e082010-09-23 15:07:34 -0700627 rtc_handle_legacy_irq(rtc, 1, RTC_UF);
628}
629
630
631/**
632 * rtc_pie_update_irq - PIE mode hrtimer hook
633 * @timer: pointer to the pie mode hrtimer
634 *
635 * This function is used to emulate PIE mode interrupts
636 * using an hrtimer. This function is called when the periodic
637 * hrtimer expires.
638 */
639enum hrtimer_restart rtc_pie_update_irq(struct hrtimer *timer)
640{
641 struct rtc_device *rtc;
642 ktime_t period;
643 int count;
644 rtc = container_of(timer, struct rtc_device, pie_timer);
645
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100646 period = NSEC_PER_SEC / rtc->irq_freq;
John Stultz6610e082010-09-23 15:07:34 -0700647 count = hrtimer_forward_now(timer, period);
648
649 rtc_handle_legacy_irq(rtc, count, RTC_PF);
650
651 return HRTIMER_RESTART;
652}
653
654/**
655 * rtc_update_irq - Triggered when a RTC interrupt occurs.
David Brownellab6a2d72007-05-08 00:33:30 -0700656 * @rtc: the rtc device
David Brownelld728b1e2006-11-25 11:09:28 -0800657 * @num: how many irqs are being reported (usually one)
658 * @events: mask of RTC_IRQF with one or more of RTC_PF, RTC_AF, RTC_UF
Atsushi Nemotoe6229bec2009-06-18 16:49:09 -0700659 * Context: any
David Brownelld728b1e2006-11-25 11:09:28 -0800660 */
David Brownellab6a2d72007-05-08 00:33:30 -0700661void rtc_update_irq(struct rtc_device *rtc,
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800662 unsigned long num, unsigned long events)
663{
viresh kumare7cba882015-07-31 16:23:43 +0530664 if (IS_ERR_OR_NULL(rtc))
Alessandro Zummo131c9cc2014-04-03 14:50:09 -0700665 return;
666
NeilBrown7523cee2012-08-05 22:56:20 +0200667 pm_stay_awake(rtc->dev.parent);
John Stultz6610e082010-09-23 15:07:34 -0700668 schedule_work(&rtc->irqwork);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800669}
670EXPORT_SYMBOL_GPL(rtc_update_irq);
671
Michał Mirosław9f3b7952013-02-01 20:40:17 +0100672static int __rtc_match(struct device *dev, const void *data)
Dave Young71da8902008-01-22 14:00:34 +0800673{
Michał Mirosław9f3b7952013-02-01 20:40:17 +0100674 const char *name = data;
Dave Young71da8902008-01-22 14:00:34 +0800675
Kay Sieversd4afc762009-01-06 14:42:11 -0800676 if (strcmp(dev_name(dev), name) == 0)
Dave Young71da8902008-01-22 14:00:34 +0800677 return 1;
678 return 0;
679}
680
Michał Mirosław9f3b7952013-02-01 20:40:17 +0100681struct rtc_device *rtc_class_open(const char *name)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800682{
David Brownellcd966202007-05-08 00:33:40 -0700683 struct device *dev;
David Brownellab6a2d72007-05-08 00:33:30 -0700684 struct rtc_device *rtc = NULL;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800685
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -0400686 dev = class_find_device(rtc_class, NULL, name, __rtc_match);
Dave Young71da8902008-01-22 14:00:34 +0800687 if (dev)
688 rtc = to_rtc_device(dev);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800689
David Brownellab6a2d72007-05-08 00:33:30 -0700690 if (rtc) {
691 if (!try_module_get(rtc->owner)) {
David Brownellcd966202007-05-08 00:33:40 -0700692 put_device(dev);
David Brownellab6a2d72007-05-08 00:33:30 -0700693 rtc = NULL;
694 }
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800695 }
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800696
David Brownellab6a2d72007-05-08 00:33:30 -0700697 return rtc;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800698}
699EXPORT_SYMBOL_GPL(rtc_class_open);
700
David Brownellab6a2d72007-05-08 00:33:30 -0700701void rtc_class_close(struct rtc_device *rtc)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800702{
David Brownellab6a2d72007-05-08 00:33:30 -0700703 module_put(rtc->owner);
David Brownellcd966202007-05-08 00:33:40 -0700704 put_device(&rtc->dev);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800705}
706EXPORT_SYMBOL_GPL(rtc_class_close);
707
Thomas Gleixner3c8bb90e2011-07-22 09:12:51 +0000708static int rtc_update_hrtimer(struct rtc_device *rtc, int enabled)
709{
710 /*
711 * We always cancel the timer here first, because otherwise
712 * we could run into BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
713 * when we manage to start the timer before the callback
714 * returns HRTIMER_RESTART.
715 *
716 * We cannot use hrtimer_cancel() here as a running callback
717 * could be blocked on rtc->irq_task_lock and hrtimer_cancel()
718 * would spin forever.
719 */
720 if (hrtimer_try_to_cancel(&rtc->pie_timer) < 0)
721 return -1;
722
723 if (enabled) {
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100724 ktime_t period = NSEC_PER_SEC / rtc->irq_freq;
Thomas Gleixner3c8bb90e2011-07-22 09:12:51 +0000725
726 hrtimer_start(&rtc->pie_timer, period, HRTIMER_MODE_REL);
727 }
728 return 0;
729}
730
David Brownell97144c62007-10-16 01:28:16 -0700731/**
732 * rtc_irq_set_state - enable/disable 2^N Hz periodic IRQs
733 * @rtc: the rtc device
David Brownell97144c62007-10-16 01:28:16 -0700734 * @enabled: true to enable periodic IRQs
735 * Context: any
736 *
737 * Note that rtc_irq_set_freq() should previously have been used to
Alexandre Belloniacecb3a2018-07-25 14:58:10 +0200738 * specify the desired frequency of periodic IRQ.
David Brownell97144c62007-10-16 01:28:16 -0700739 */
Alexandre Belloni8719d3c2018-07-25 15:07:09 +0200740int rtc_irq_set_state(struct rtc_device *rtc, int enabled)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800741{
742 int err = 0;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800743
Alexandre Belloniacecb3a2018-07-25 14:58:10 +0200744 while (rtc_update_hrtimer(rtc, enabled) < 0)
745 cpu_relax();
746
747 rtc->pie_enabled = enabled;
Baolin Wang29a1f592017-12-14 13:31:43 +0800748
749 trace_rtc_irq_set_state(enabled, err);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800750 return err;
751}
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800752
David Brownell97144c62007-10-16 01:28:16 -0700753/**
754 * rtc_irq_set_freq - set 2^N Hz periodic IRQ frequency for IRQ
755 * @rtc: the rtc device
Alexandre Belloniacecb3a2018-07-25 14:58:10 +0200756 * @freq: positive frequency
David Brownell97144c62007-10-16 01:28:16 -0700757 * Context: any
758 *
759 * Note that rtc_irq_set_state() is used to enable or disable the
760 * periodic IRQs.
761 */
Alexandre Belloni8719d3c2018-07-25 15:07:09 +0200762int rtc_irq_set_freq(struct rtc_device *rtc, int freq)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800763{
Alessandro Zummo56f10c62006-06-25 05:48:20 -0700764 int err = 0;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800765
Thomas Gleixner6e7a3332011-07-22 09:12:51 +0000766 if (freq <= 0 || freq > RTC_MAX_FREQ)
Marcelo Roberto Jimenez83a06bf2011-02-02 16:04:02 -0200767 return -EINVAL;
Alexandre Belloniacecb3a2018-07-25 14:58:10 +0200768
769 rtc->irq_freq = freq;
770 while (rtc->pie_enabled && rtc_update_hrtimer(rtc, 1) < 0)
771 cpu_relax();
Baolin Wang29a1f592017-12-14 13:31:43 +0800772
773 trace_rtc_irq_set_freq(freq, err);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800774 return err;
775}
John Stultz6610e082010-09-23 15:07:34 -0700776
777/**
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100778 * rtc_timer_enqueue - Adds a rtc_timer to the rtc_device timerqueue
John Stultz6610e082010-09-23 15:07:34 -0700779 * @rtc rtc device
780 * @timer timer being added.
781 *
782 * Enqueues a timer onto the rtc devices timerqueue and sets
783 * the next alarm event appropriately.
784 *
John Stultzaa0be0f2011-01-20 15:26:12 -0800785 * Sets the enabled bit on the added timer.
786 *
John Stultz6610e082010-09-23 15:07:34 -0700787 * Must hold ops_lock for proper serialization of timerqueue
788 */
John Stultzaa0be0f2011-01-20 15:26:12 -0800789static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer)
John Stultz6610e082010-09-23 15:07:34 -0700790{
Colin Ian King2b2f5ff2016-05-16 17:22:54 +0100791 struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue);
792 struct rtc_time tm;
793 ktime_t now;
794
John Stultzaa0be0f2011-01-20 15:26:12 -0800795 timer->enabled = 1;
Colin Ian King2b2f5ff2016-05-16 17:22:54 +0100796 __rtc_read_time(rtc, &tm);
797 now = rtc_tm_to_ktime(tm);
798
799 /* Skip over expired timers */
800 while (next) {
Thomas Gleixner2456e852016-12-25 11:38:40 +0100801 if (next->expires >= now)
Colin Ian King2b2f5ff2016-05-16 17:22:54 +0100802 break;
803 next = timerqueue_iterate_next(next);
804 }
805
John Stultz6610e082010-09-23 15:07:34 -0700806 timerqueue_add(&rtc->timerqueue, &timer->node);
Baolin Wang29a1f592017-12-14 13:31:43 +0800807 trace_rtc_timer_enqueue(timer);
Alexandre Belloni74717b22017-09-28 13:53:27 +0200808 if (!next || ktime_before(timer->node.expires, next->expires)) {
John Stultz6610e082010-09-23 15:07:34 -0700809 struct rtc_wkalrm alarm;
810 int err;
811 alarm.time = rtc_ktime_to_tm(timer->node.expires);
812 alarm.enabled = 1;
813 err = __rtc_set_alarm(rtc, &alarm);
Zoran Markovic14d0e342013-06-26 16:09:13 -0700814 if (err == -ETIME) {
815 pm_stay_awake(rtc->dev.parent);
John Stultz6610e082010-09-23 15:07:34 -0700816 schedule_work(&rtc->irqwork);
Zoran Markovic14d0e342013-06-26 16:09:13 -0700817 } else if (err) {
John Stultzaa0be0f2011-01-20 15:26:12 -0800818 timerqueue_del(&rtc->timerqueue, &timer->node);
Baolin Wang29a1f592017-12-14 13:31:43 +0800819 trace_rtc_timer_dequeue(timer);
John Stultzaa0be0f2011-01-20 15:26:12 -0800820 timer->enabled = 0;
821 return err;
822 }
John Stultz6610e082010-09-23 15:07:34 -0700823 }
John Stultzaa0be0f2011-01-20 15:26:12 -0800824 return 0;
John Stultz6610e082010-09-23 15:07:34 -0700825}
826
Rabin Vincent41c7f742011-11-22 11:03:14 +0100827static void rtc_alarm_disable(struct rtc_device *rtc)
828{
829 if (!rtc->ops || !rtc->ops->alarm_irq_enable)
830 return;
831
832 rtc->ops->alarm_irq_enable(rtc->dev.parent, false);
Baolin Wang29a1f592017-12-14 13:31:43 +0800833 trace_rtc_alarm_irq_enable(0, 0);
Rabin Vincent41c7f742011-11-22 11:03:14 +0100834}
835
John Stultz6610e082010-09-23 15:07:34 -0700836/**
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100837 * rtc_timer_remove - Removes a rtc_timer from the rtc_device timerqueue
John Stultz6610e082010-09-23 15:07:34 -0700838 * @rtc rtc device
839 * @timer timer being removed.
840 *
841 * Removes a timer onto the rtc devices timerqueue and sets
842 * the next alarm event appropriately.
843 *
John Stultzaa0be0f2011-01-20 15:26:12 -0800844 * Clears the enabled bit on the removed timer.
845 *
John Stultz6610e082010-09-23 15:07:34 -0700846 * Must hold ops_lock for proper serialization of timerqueue
847 */
John Stultzaa0be0f2011-01-20 15:26:12 -0800848static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer)
John Stultz6610e082010-09-23 15:07:34 -0700849{
850 struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue);
851 timerqueue_del(&rtc->timerqueue, &timer->node);
Baolin Wang29a1f592017-12-14 13:31:43 +0800852 trace_rtc_timer_dequeue(timer);
John Stultzaa0be0f2011-01-20 15:26:12 -0800853 timer->enabled = 0;
John Stultz6610e082010-09-23 15:07:34 -0700854 if (next == &timer->node) {
855 struct rtc_wkalrm alarm;
856 int err;
857 next = timerqueue_getnext(&rtc->timerqueue);
Rabin Vincent41c7f742011-11-22 11:03:14 +0100858 if (!next) {
859 rtc_alarm_disable(rtc);
John Stultz6610e082010-09-23 15:07:34 -0700860 return;
Rabin Vincent41c7f742011-11-22 11:03:14 +0100861 }
John Stultz6610e082010-09-23 15:07:34 -0700862 alarm.time = rtc_ktime_to_tm(next->expires);
863 alarm.enabled = 1;
864 err = __rtc_set_alarm(rtc, &alarm);
Zoran Markovic14d0e342013-06-26 16:09:13 -0700865 if (err == -ETIME) {
866 pm_stay_awake(rtc->dev.parent);
John Stultz6610e082010-09-23 15:07:34 -0700867 schedule_work(&rtc->irqwork);
Zoran Markovic14d0e342013-06-26 16:09:13 -0700868 }
John Stultz6610e082010-09-23 15:07:34 -0700869 }
870}
871
872/**
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100873 * rtc_timer_do_work - Expires rtc timers
John Stultz6610e082010-09-23 15:07:34 -0700874 * @rtc rtc device
875 * @timer timer being removed.
876 *
877 * Expires rtc timers. Reprograms next alarm event if needed.
878 * Called via worktask.
879 *
880 * Serializes access to timerqueue via ops_lock mutex
881 */
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100882void rtc_timer_do_work(struct work_struct *work)
John Stultz6610e082010-09-23 15:07:34 -0700883{
884 struct rtc_timer *timer;
885 struct timerqueue_node *next;
886 ktime_t now;
887 struct rtc_time tm;
888
889 struct rtc_device *rtc =
890 container_of(work, struct rtc_device, irqwork);
891
892 mutex_lock(&rtc->ops_lock);
893again:
894 __rtc_read_time(rtc, &tm);
895 now = rtc_tm_to_ktime(tm);
896 while ((next = timerqueue_getnext(&rtc->timerqueue))) {
Thomas Gleixner2456e852016-12-25 11:38:40 +0100897 if (next->expires > now)
John Stultz6610e082010-09-23 15:07:34 -0700898 break;
899
900 /* expire timer */
901 timer = container_of(next, struct rtc_timer, node);
902 timerqueue_del(&rtc->timerqueue, &timer->node);
Baolin Wang29a1f592017-12-14 13:31:43 +0800903 trace_rtc_timer_dequeue(timer);
John Stultz6610e082010-09-23 15:07:34 -0700904 timer->enabled = 0;
Alexandre Belloni5a5ba10f2018-07-26 15:40:56 +0200905 if (timer->func)
Alexandre Belloni9a0320112018-12-18 22:11:26 +0100906 timer->func(timer->rtc);
John Stultz6610e082010-09-23 15:07:34 -0700907
Baolin Wang29a1f592017-12-14 13:31:43 +0800908 trace_rtc_timer_fired(timer);
John Stultz6610e082010-09-23 15:07:34 -0700909 /* Re-add/fwd periodic timers */
910 if (ktime_to_ns(timer->period)) {
911 timer->node.expires = ktime_add(timer->node.expires,
912 timer->period);
913 timer->enabled = 1;
914 timerqueue_add(&rtc->timerqueue, &timer->node);
Baolin Wang29a1f592017-12-14 13:31:43 +0800915 trace_rtc_timer_enqueue(timer);
John Stultz6610e082010-09-23 15:07:34 -0700916 }
917 }
918
919 /* Set next alarm */
920 if (next) {
921 struct rtc_wkalrm alarm;
922 int err;
Xunlei Pang6528b882014-12-10 15:54:26 -0800923 int retry = 3;
924
John Stultz6610e082010-09-23 15:07:34 -0700925 alarm.time = rtc_ktime_to_tm(next->expires);
926 alarm.enabled = 1;
Xunlei Pang6528b882014-12-10 15:54:26 -0800927reprogram:
John Stultz6610e082010-09-23 15:07:34 -0700928 err = __rtc_set_alarm(rtc, &alarm);
929 if (err == -ETIME)
930 goto again;
Xunlei Pang6528b882014-12-10 15:54:26 -0800931 else if (err) {
932 if (retry-- > 0)
933 goto reprogram;
934
935 timer = container_of(next, struct rtc_timer, node);
936 timerqueue_del(&rtc->timerqueue, &timer->node);
Baolin Wang29a1f592017-12-14 13:31:43 +0800937 trace_rtc_timer_dequeue(timer);
Xunlei Pang6528b882014-12-10 15:54:26 -0800938 timer->enabled = 0;
939 dev_err(&rtc->dev, "__rtc_set_alarm: err=%d\n", err);
940 goto again;
941 }
Rabin Vincent41c7f742011-11-22 11:03:14 +0100942 } else
943 rtc_alarm_disable(rtc);
John Stultz6610e082010-09-23 15:07:34 -0700944
Zoran Markovic14d0e342013-06-26 16:09:13 -0700945 pm_relax(rtc->dev.parent);
John Stultz6610e082010-09-23 15:07:34 -0700946 mutex_unlock(&rtc->ops_lock);
947}
948
949
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100950/* rtc_timer_init - Initializes an rtc_timer
John Stultz6610e082010-09-23 15:07:34 -0700951 * @timer: timer to be intiialized
952 * @f: function pointer to be called when timer fires
Alexandre Belloni9a0320112018-12-18 22:11:26 +0100953 * @rtc: pointer to the rtc_device
John Stultz6610e082010-09-23 15:07:34 -0700954 *
955 * Kernel interface to initializing an rtc_timer.
956 */
Alexandre Belloni9a0320112018-12-18 22:11:26 +0100957void rtc_timer_init(struct rtc_timer *timer, void (*f)(struct rtc_device *r),
958 struct rtc_device *rtc)
John Stultz6610e082010-09-23 15:07:34 -0700959{
960 timerqueue_init(&timer->node);
961 timer->enabled = 0;
Alexandre Belloni5a5ba10f2018-07-26 15:40:56 +0200962 timer->func = f;
Alexandre Belloni9a0320112018-12-18 22:11:26 +0100963 timer->rtc = rtc;
John Stultz6610e082010-09-23 15:07:34 -0700964}
965
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100966/* rtc_timer_start - Sets an rtc_timer to fire in the future
John Stultz6610e082010-09-23 15:07:34 -0700967 * @ rtc: rtc device to be used
968 * @ timer: timer being set
969 * @ expires: time at which to expire the timer
970 * @ period: period that the timer will recur
971 *
972 * Kernel interface to set an rtc_timer
973 */
Sachin Kamat3ff2e132013-07-03 15:05:42 -0700974int rtc_timer_start(struct rtc_device *rtc, struct rtc_timer *timer,
John Stultz6610e082010-09-23 15:07:34 -0700975 ktime_t expires, ktime_t period)
976{
977 int ret = 0;
978 mutex_lock(&rtc->ops_lock);
979 if (timer->enabled)
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100980 rtc_timer_remove(rtc, timer);
John Stultz6610e082010-09-23 15:07:34 -0700981
982 timer->node.expires = expires;
983 timer->period = period;
984
John Stultzaa0be0f2011-01-20 15:26:12 -0800985 ret = rtc_timer_enqueue(rtc, timer);
John Stultz6610e082010-09-23 15:07:34 -0700986
987 mutex_unlock(&rtc->ops_lock);
988 return ret;
989}
990
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100991/* rtc_timer_cancel - Stops an rtc_timer
John Stultz6610e082010-09-23 15:07:34 -0700992 * @ rtc: rtc device to be used
993 * @ timer: timer being set
994 *
995 * Kernel interface to cancel an rtc_timer
996 */
Krzysztof Kozlowski73744a62015-05-03 18:57:11 +0900997void rtc_timer_cancel(struct rtc_device *rtc, struct rtc_timer *timer)
John Stultz6610e082010-09-23 15:07:34 -0700998{
John Stultz6610e082010-09-23 15:07:34 -0700999 mutex_lock(&rtc->ops_lock);
1000 if (timer->enabled)
Thomas Gleixner96c8f062010-12-13 22:45:48 +01001001 rtc_timer_remove(rtc, timer);
John Stultz6610e082010-09-23 15:07:34 -07001002 mutex_unlock(&rtc->ops_lock);
John Stultz6610e082010-09-23 15:07:34 -07001003}
1004
Joshua Claytonb3967062016-02-05 12:41:11 -08001005/**
1006 * rtc_read_offset - Read the amount of rtc offset in parts per billion
1007 * @ rtc: rtc device to be used
1008 * @ offset: the offset in parts per billion
1009 *
1010 * see below for details.
1011 *
1012 * Kernel interface to read rtc clock offset
1013 * Returns 0 on success, or a negative number on error.
1014 * If read_offset() is not implemented for the rtc, return -EINVAL
1015 */
1016int rtc_read_offset(struct rtc_device *rtc, long *offset)
1017{
1018 int ret;
John Stultz6610e082010-09-23 15:07:34 -07001019
Joshua Claytonb3967062016-02-05 12:41:11 -08001020 if (!rtc->ops)
1021 return -ENODEV;
1022
1023 if (!rtc->ops->read_offset)
1024 return -EINVAL;
1025
1026 mutex_lock(&rtc->ops_lock);
1027 ret = rtc->ops->read_offset(rtc->dev.parent, offset);
1028 mutex_unlock(&rtc->ops_lock);
Baolin Wang29a1f592017-12-14 13:31:43 +08001029
1030 trace_rtc_read_offset(*offset, ret);
Joshua Claytonb3967062016-02-05 12:41:11 -08001031 return ret;
1032}
1033
1034/**
1035 * rtc_set_offset - Adjusts the duration of the average second
1036 * @ rtc: rtc device to be used
1037 * @ offset: the offset in parts per billion
1038 *
1039 * Some rtc's allow an adjustment to the average duration of a second
1040 * to compensate for differences in the actual clock rate due to temperature,
1041 * the crystal, capacitor, etc.
1042 *
Russell King8a25c8f2017-09-29 11:23:25 +01001043 * The adjustment applied is as follows:
1044 * t = t0 * (1 + offset * 1e-9)
1045 * where t0 is the measured length of 1 RTC second with offset = 0
1046 *
Joshua Claytonb3967062016-02-05 12:41:11 -08001047 * Kernel interface to adjust an rtc clock offset.
1048 * Return 0 on success, or a negative number on error.
1049 * If the rtc offset is not setable (or not implemented), return -EINVAL
1050 */
1051int rtc_set_offset(struct rtc_device *rtc, long offset)
1052{
1053 int ret;
1054
1055 if (!rtc->ops)
1056 return -ENODEV;
1057
1058 if (!rtc->ops->set_offset)
1059 return -EINVAL;
1060
1061 mutex_lock(&rtc->ops_lock);
1062 ret = rtc->ops->set_offset(rtc->dev.parent, offset);
1063 mutex_unlock(&rtc->ops_lock);
Baolin Wang29a1f592017-12-14 13:31:43 +08001064
1065 trace_rtc_set_offset(offset, ret);
Joshua Claytonb3967062016-02-05 12:41:11 -08001066 return ret;
1067}