blob: 56ed0c3a8c85e590ee54e67385817ae382849b62 [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;
Alexandre Belloni606cc432019-03-20 12:59:09 +010087
88 if (!rtc->ops) {
John Stultz6610e082010-09-23 15:07:34 -070089 err = -ENODEV;
Alexandre Belloni606cc432019-03-20 12:59:09 +010090 } else if (!rtc->ops->read_time) {
John Stultz6610e082010-09-23 15:07:34 -070091 err = -EINVAL;
Alexandre Belloni606cc432019-03-20 12:59:09 +010092 } else {
John Stultz6610e082010-09-23 15:07:34 -070093 memset(tm, 0, sizeof(struct rtc_time));
94 err = rtc->ops->read_time(rtc->dev.parent, tm);
Hyogi Gim16682c862014-12-10 15:52:27 -080095 if (err < 0) {
Aaro Koskinend0bddb52015-04-16 12:45:51 -070096 dev_dbg(&rtc->dev, "read_time: fail to read: %d\n",
97 err);
Hyogi Gim16682c862014-12-10 15:52:27 -080098 return err;
99 }
100
Baolin Wang98951562018-01-08 14:04:50 +0800101 rtc_add_offset(rtc, tm);
102
Hyogi Gim16682c862014-12-10 15:52:27 -0800103 err = rtc_valid_tm(tm);
104 if (err < 0)
Aaro Koskinend0bddb52015-04-16 12:45:51 -0700105 dev_dbg(&rtc->dev, "read_time: rtc_time isn't valid\n");
John Stultz6610e082010-09-23 15:07:34 -0700106 }
107 return err;
108}
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800109
David Brownellab6a2d72007-05-08 00:33:30 -0700110int rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800111{
112 int err;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800113
114 err = mutex_lock_interruptible(&rtc->ops_lock);
115 if (err)
David Brownellb68bb262008-07-29 22:33:30 -0700116 return err;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800117
John Stultz6610e082010-09-23 15:07:34 -0700118 err = __rtc_read_time(rtc, tm);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800119 mutex_unlock(&rtc->ops_lock);
Baolin Wang29a1f592017-12-14 13:31:43 +0800120
121 trace_rtc_read_time(rtc_tm_to_time64(tm), err);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800122 return err;
123}
124EXPORT_SYMBOL_GPL(rtc_read_time);
125
David Brownellab6a2d72007-05-08 00:33:30 -0700126int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800127{
128 int err;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800129
130 err = rtc_valid_tm(tm);
131 if (err != 0)
132 return err;
133
Baolin Wang4c4e5df2018-01-08 14:04:49 +0800134 err = rtc_valid_range(rtc, tm);
135 if (err)
136 return err;
Alexandre Belloni71db0492018-02-17 14:58:40 +0100137
Baolin Wang98951562018-01-08 14:04:50 +0800138 rtc_subtract_offset(rtc, tm);
139
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800140 err = mutex_lock_interruptible(&rtc->ops_lock);
141 if (err)
David Brownellb68bb262008-07-29 22:33:30 -0700142 return err;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800143
144 if (!rtc->ops)
145 err = -ENODEV;
Alessandro Zummobbccf832009-01-06 14:42:21 -0800146 else if (rtc->ops->set_time)
David Brownellcd966202007-05-08 00:33:40 -0700147 err = rtc->ops->set_time(rtc->dev.parent, tm);
Alexandre Belloni606cc432019-03-20 12:59:09 +0100148 else if (rtc->ops->set_mmss64)
149 err = rtc->ops->set_mmss64(rtc->dev.parent,
150 rtc_tm_to_time64(tm));
151 else if (rtc->ops->set_mmss)
152 err = rtc->ops->set_mmss(rtc->dev.parent,
153 rtc_tm_to_time64(tm));
154 else
Alessandro Zummobbccf832009-01-06 14:42:21 -0800155 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
Alexandre Belloni606cc432019-03-20 12:59:09 +0100167static int rtc_read_alarm_internal(struct rtc_device *rtc,
168 struct rtc_wkalrm *alarm)
John Stultzf44f7f92011-02-21 22:58:51 -0800169{
170 int err;
171
172 err = mutex_lock_interruptible(&rtc->ops_lock);
173 if (err)
174 return err;
175
Alexandre Belloni606cc432019-03-20 12:59:09 +0100176 if (!rtc->ops) {
John Stultzf44f7f92011-02-21 22:58:51 -0800177 err = -ENODEV;
Alexandre Belloni606cc432019-03-20 12:59:09 +0100178 } else if (!rtc->ops->read_alarm) {
John Stultzf44f7f92011-02-21 22:58:51 -0800179 err = -EINVAL;
Alexandre Belloni606cc432019-03-20 12:59:09 +0100180 } else {
Uwe Kleine-Königd68778b2016-05-11 09:11:23 +0200181 alarm->enabled = 0;
182 alarm->pending = 0;
183 alarm->time.tm_sec = -1;
184 alarm->time.tm_min = -1;
185 alarm->time.tm_hour = -1;
186 alarm->time.tm_mday = -1;
187 alarm->time.tm_mon = -1;
188 alarm->time.tm_year = -1;
189 alarm->time.tm_wday = -1;
190 alarm->time.tm_yday = -1;
191 alarm->time.tm_isdst = -1;
John Stultzf44f7f92011-02-21 22:58:51 -0800192 err = rtc->ops->read_alarm(rtc->dev.parent, alarm);
193 }
194
195 mutex_unlock(&rtc->ops_lock);
Baolin Wang29a1f592017-12-14 13:31:43 +0800196
197 trace_rtc_read_alarm(rtc_tm_to_time64(&alarm->time), err);
John Stultzf44f7f92011-02-21 22:58:51 -0800198 return err;
199}
200
201int __rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
202{
203 int err;
204 struct rtc_time before, now;
205 int first_time = 1;
Xunlei Pangbc10aa92015-01-22 02:31:51 +0000206 time64_t t_now, t_alm;
John Stultzf44f7f92011-02-21 22:58:51 -0800207 enum { none, day, month, year } missing = none;
Alexandre Belloni606cc432019-03-20 12:59:09 +0100208 unsigned int days;
John Stultzf44f7f92011-02-21 22:58:51 -0800209
210 /* The lower level RTC driver may return -1 in some fields,
211 * creating invalid alarm->time values, for reasons like:
212 *
213 * - The hardware may not be capable of filling them in;
214 * many alarms match only on time-of-day fields, not
215 * day/month/year calendar data.
216 *
217 * - Some hardware uses illegal values as "wildcard" match
218 * values, which non-Linux firmware (like a BIOS) may try
219 * to set up as e.g. "alarm 15 minutes after each hour".
220 * Linux uses only oneshot alarms.
221 *
222 * When we see that here, we deal with it by using values from
223 * a current RTC timestamp for any missing (-1) values. The
224 * RTC driver prevents "periodic alarm" modes.
225 *
226 * But this can be racey, because some fields of the RTC timestamp
227 * may have wrapped in the interval since we read the RTC alarm,
228 * which would lead to us inserting inconsistent values in place
229 * of the -1 fields.
230 *
231 * Reading the alarm and timestamp in the reverse sequence
232 * would have the same race condition, and not solve the issue.
233 *
234 * So, we must first read the RTC timestamp,
235 * then read the RTC alarm value,
236 * and then read a second RTC timestamp.
237 *
238 * If any fields of the second timestamp have changed
239 * when compared with the first timestamp, then we know
240 * our timestamp may be inconsistent with that used by
241 * the low-level rtc_read_alarm_internal() function.
242 *
243 * So, when the two timestamps disagree, we just loop and do
244 * the process again to get a fully consistent set of values.
245 *
246 * This could all instead be done in the lower level driver,
247 * but since more than one lower level RTC implementation needs it,
248 * then it's probably best best to do it here instead of there..
249 */
250
251 /* Get the "before" timestamp */
252 err = rtc_read_time(rtc, &before);
253 if (err < 0)
254 return err;
255 do {
256 if (!first_time)
257 memcpy(&before, &now, sizeof(struct rtc_time));
258 first_time = 0;
259
260 /* get the RTC alarm values, which may be incomplete */
261 err = rtc_read_alarm_internal(rtc, alarm);
262 if (err)
263 return err;
264
265 /* full-function RTCs won't have such missing fields */
Alexandre Bellonifd6792b2018-07-12 12:22:44 +0200266 if (rtc_valid_tm(&alarm->time) == 0) {
267 rtc_add_offset(rtc, &alarm->time);
John Stultzf44f7f92011-02-21 22:58:51 -0800268 return 0;
Alexandre Bellonifd6792b2018-07-12 12:22:44 +0200269 }
John Stultzf44f7f92011-02-21 22:58:51 -0800270
271 /* get the "after" timestamp, to detect wrapped fields */
272 err = rtc_read_time(rtc, &now);
273 if (err < 0)
274 return err;
275
276 /* note that tm_sec is a "don't care" value here: */
Alexandre Belloni606cc432019-03-20 12:59:09 +0100277 } while (before.tm_min != now.tm_min ||
278 before.tm_hour != now.tm_hour ||
279 before.tm_mon != now.tm_mon ||
280 before.tm_year != now.tm_year);
John Stultzf44f7f92011-02-21 22:58:51 -0800281
282 /* Fill in the missing alarm fields using the timestamp; we
283 * know there's at least one since alarm->time is invalid.
284 */
285 if (alarm->time.tm_sec == -1)
286 alarm->time.tm_sec = now.tm_sec;
287 if (alarm->time.tm_min == -1)
288 alarm->time.tm_min = now.tm_min;
289 if (alarm->time.tm_hour == -1)
290 alarm->time.tm_hour = now.tm_hour;
291
292 /* For simplicity, only support date rollover for now */
Ben Hutchingse74a8f22012-01-10 15:11:02 -0800293 if (alarm->time.tm_mday < 1 || alarm->time.tm_mday > 31) {
John Stultzf44f7f92011-02-21 22:58:51 -0800294 alarm->time.tm_mday = now.tm_mday;
295 missing = day;
296 }
Alexandre Belloni606cc432019-03-20 12:59:09 +0100297 if ((unsigned int)alarm->time.tm_mon >= 12) {
John Stultzf44f7f92011-02-21 22:58:51 -0800298 alarm->time.tm_mon = now.tm_mon;
299 if (missing == none)
300 missing = month;
301 }
302 if (alarm->time.tm_year == -1) {
303 alarm->time.tm_year = now.tm_year;
304 if (missing == none)
305 missing = year;
306 }
307
Vaibhav Jainda96aea2017-05-19 22:18:55 +0530308 /* Can't proceed if alarm is still invalid after replacing
309 * missing fields.
310 */
311 err = rtc_valid_tm(&alarm->time);
312 if (err)
313 goto done;
314
John Stultzf44f7f92011-02-21 22:58:51 -0800315 /* with luck, no rollover is needed */
Xunlei Pangbc10aa92015-01-22 02:31:51 +0000316 t_now = rtc_tm_to_time64(&now);
317 t_alm = rtc_tm_to_time64(&alarm->time);
John Stultzf44f7f92011-02-21 22:58:51 -0800318 if (t_now < t_alm)
319 goto done;
320
321 switch (missing) {
John Stultzf44f7f92011-02-21 22:58:51 -0800322 /* 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 {
Alexandre Belloni606cc432019-03-20 12:59:09 +0100341 if (alarm->time.tm_mon < 11) {
John Stultzf44f7f92011-02-21 22:58:51 -0800342 alarm->time.tm_mon++;
Alexandre Belloni606cc432019-03-20 12:59:09 +0100343 } else {
John Stultzf44f7f92011-02-21 22:58:51 -0800344 alarm->time.tm_mon = 0;
345 alarm->time.tm_year++;
346 }
347 days = rtc_month_days(alarm->time.tm_mon,
Alexandre Belloni606cc432019-03-20 12:59:09 +0100348 alarm->time.tm_year);
John Stultzf44f7f92011-02-21 22:58:51 -0800349 } 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++;
Alexandre Belloni606cc432019-03-20 12:59:09 +0100357 } 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)
Alexandre Belloni606cc432019-03-20 12:59:09 +0100369 dev_warn(&rtc->dev, "invalid alarm value: %ptR\n",
370 &alarm->time);
Ales Novakee1d9012014-06-06 14:35:39 -0700371
372 return err;
John Stultzf44f7f92011-02-21 22:58:51 -0800373}
374
John Stultz6610e082010-09-23 15:07:34 -0700375int rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800376{
377 int err;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800378
379 err = mutex_lock_interruptible(&rtc->ops_lock);
380 if (err)
David Brownellb68bb262008-07-29 22:33:30 -0700381 return err;
Alexandre Belloni606cc432019-03-20 12:59:09 +0100382 if (!rtc->ops) {
John Stultzd5553a52011-01-20 15:26:13 -0800383 err = -ENODEV;
Alexandre Belloni606cc432019-03-20 12:59:09 +0100384 } else if (!rtc->ops->read_alarm) {
John Stultzd5553a52011-01-20 15:26:13 -0800385 err = -EINVAL;
Alexandre Belloni606cc432019-03-20 12:59:09 +0100386 } else {
John Stultzd5553a52011-01-20 15:26:13 -0800387 memset(alarm, 0, sizeof(struct rtc_wkalrm));
388 alarm->enabled = rtc->aie_timer.enabled;
John Stultz6610e082010-09-23 15:07:34 -0700389 alarm->time = rtc_ktime_to_tm(rtc->aie_timer.node.expires);
John Stultzd5553a52011-01-20 15:26:13 -0800390 }
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800391 mutex_unlock(&rtc->ops_lock);
Mark Lord0e36a9a2007-10-16 01:28:21 -0700392
Baolin Wang29a1f592017-12-14 13:31:43 +0800393 trace_rtc_read_alarm(rtc_tm_to_time64(&alarm->time), err);
John Stultzd5553a52011-01-20 15:26:13 -0800394 return err;
Mark Lord0e36a9a2007-10-16 01:28:21 -0700395}
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800396EXPORT_SYMBOL_GPL(rtc_read_alarm);
397
Mark Brownd576fe42011-06-01 11:13:16 +0100398static int __rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
John Stultz6610e082010-09-23 15:07:34 -0700399{
400 struct rtc_time tm;
Xunlei Pangbc10aa92015-01-22 02:31:51 +0000401 time64_t now, scheduled;
John Stultz6610e082010-09-23 15:07:34 -0700402 int err;
403
404 err = rtc_valid_tm(&alarm->time);
405 if (err)
406 return err;
Baolin Wang98951562018-01-08 14:04:50 +0800407
Xunlei Pangbc10aa92015-01-22 02:31:51 +0000408 scheduled = rtc_tm_to_time64(&alarm->time);
John Stultz6610e082010-09-23 15:07:34 -0700409
410 /* Make sure we're not setting alarms in the past */
411 err = __rtc_read_time(rtc, &tm);
Hyogi Gimca6dc2d2014-08-08 14:20:11 -0700412 if (err)
413 return err;
Xunlei Pangbc10aa92015-01-22 02:31:51 +0000414 now = rtc_tm_to_time64(&tm);
John Stultz6610e082010-09-23 15:07:34 -0700415 if (scheduled <= now)
416 return -ETIME;
417 /*
418 * XXX - We just checked to make sure the alarm time is not
419 * in the past, but there is still a race window where if
420 * the is alarm set for the next second and the second ticks
421 * over right here, before we set the alarm.
422 */
423
Alexandre Bellonifd6792b2018-07-12 12:22:44 +0200424 rtc_subtract_offset(rtc, &alarm->time);
425
Linus Torvalds157e8bf2012-01-03 17:32:13 -0800426 if (!rtc->ops)
427 err = -ENODEV;
428 else if (!rtc->ops->set_alarm)
429 err = -EINVAL;
430 else
431 err = rtc->ops->set_alarm(rtc->dev.parent, alarm);
432
Baolin Wang29a1f592017-12-14 13:31:43 +0800433 trace_rtc_set_alarm(rtc_tm_to_time64(&alarm->time), err);
Linus Torvalds157e8bf2012-01-03 17:32:13 -0800434 return err;
John Stultz6610e082010-09-23 15:07:34 -0700435}
436
David Brownellab6a2d72007-05-08 00:33:30 -0700437int rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800438{
439 int err;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800440
Alexandre Belloniabfdff42018-06-05 23:09:14 +0200441 if (!rtc->ops)
442 return -ENODEV;
443 else if (!rtc->ops->set_alarm)
444 return -EINVAL;
445
David Brownellf8245c22007-05-08 00:34:07 -0700446 err = rtc_valid_tm(&alarm->time);
447 if (err != 0)
448 return err;
449
Baolin Wang4c4e5df2018-01-08 14:04:49 +0800450 err = rtc_valid_range(rtc, &alarm->time);
451 if (err)
452 return err;
Alexandre Belloni71db0492018-02-17 14:58:40 +0100453
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800454 err = mutex_lock_interruptible(&rtc->ops_lock);
455 if (err)
David Brownellb68bb262008-07-29 22:33:30 -0700456 return err;
Sachin Kamat3ff2e132013-07-03 15:05:42 -0700457 if (rtc->aie_timer.enabled)
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100458 rtc_timer_remove(rtc, &rtc->aie_timer);
Sachin Kamat3ff2e132013-07-03 15:05:42 -0700459
John Stultz6610e082010-09-23 15:07:34 -0700460 rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time);
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100461 rtc->aie_timer.period = 0;
Sachin Kamat3ff2e132013-07-03 15:05:42 -0700462 if (alarm->enabled)
John Stultzaa0be0f2011-01-20 15:26:12 -0800463 err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
Sachin Kamat3ff2e132013-07-03 15:05:42 -0700464
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800465 mutex_unlock(&rtc->ops_lock);
Baolin Wang98951562018-01-08 14:04:50 +0800466
John Stultzaa0be0f2011-01-20 15:26:12 -0800467 return err;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800468}
469EXPORT_SYMBOL_GPL(rtc_set_alarm);
470
John Stultzf6d5b332011-03-29 18:00:27 -0700471/* Called once per device from rtc_device_register */
472int rtc_initialize_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
473{
474 int err;
John Stultzbd729d72012-01-05 15:21:19 -0800475 struct rtc_time now;
John Stultzf6d5b332011-03-29 18:00:27 -0700476
477 err = rtc_valid_tm(&alarm->time);
478 if (err != 0)
479 return err;
480
John Stultzbd729d72012-01-05 15:21:19 -0800481 err = rtc_read_time(rtc, &now);
482 if (err)
483 return err;
484
John Stultzf6d5b332011-03-29 18:00:27 -0700485 err = mutex_lock_interruptible(&rtc->ops_lock);
486 if (err)
487 return err;
488
489 rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time);
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100490 rtc->aie_timer.period = 0;
John Stultzbd729d72012-01-05 15:21:19 -0800491
Uwe Kleine-König6785b3b2016-07-02 17:28:12 +0200492 /* Alarm has to be enabled & in the future for us to enqueue it */
Thomas Gleixner2456e852016-12-25 11:38:40 +0100493 if (alarm->enabled && (rtc_tm_to_ktime(now) <
494 rtc->aie_timer.node.expires)) {
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{
Alexandre Belloni606cc432019-03-20 12:59:09 +0100506 int err;
507
508 err = mutex_lock_interruptible(&rtc->ops_lock);
Alessandro Zummo099e6572009-01-04 12:00:54 -0800509 if (err)
510 return err;
511
John Stultz6610e082010-09-23 15:07:34 -0700512 if (rtc->aie_timer.enabled != enabled) {
John Stultzaa0be0f2011-01-20 15:26:12 -0800513 if (enabled)
514 err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
515 else
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100516 rtc_timer_remove(rtc, &rtc->aie_timer);
John Stultz6610e082010-09-23 15:07:34 -0700517 }
518
John Stultzaa0be0f2011-01-20 15:26:12 -0800519 if (err)
Uwe Kleine-König516373b2011-02-14 11:33:17 +0100520 /* nothing */;
521 else if (!rtc->ops)
Alessandro Zummo099e6572009-01-04 12:00:54 -0800522 err = -ENODEV;
523 else if (!rtc->ops->alarm_irq_enable)
524 err = -EINVAL;
525 else
526 err = rtc->ops->alarm_irq_enable(rtc->dev.parent, enabled);
527
528 mutex_unlock(&rtc->ops_lock);
Baolin Wang29a1f592017-12-14 13:31:43 +0800529
530 trace_rtc_alarm_irq_enable(enabled, err);
Alessandro Zummo099e6572009-01-04 12:00:54 -0800531 return err;
532}
533EXPORT_SYMBOL_GPL(rtc_alarm_irq_enable);
534
535int rtc_update_irq_enable(struct rtc_device *rtc, unsigned int enabled)
536{
Alexandre Belloni606cc432019-03-20 12:59:09 +0100537 int err;
538
539 err = mutex_lock_interruptible(&rtc->ops_lock);
Alessandro Zummo099e6572009-01-04 12:00:54 -0800540 if (err)
541 return err;
542
John Stultz456d66e2011-02-11 18:15:23 -0800543#ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
544 if (enabled == 0 && rtc->uie_irq_active) {
545 mutex_unlock(&rtc->ops_lock);
546 return rtc_dev_update_irq_enable_emul(rtc, 0);
547 }
548#endif
John Stultz6610e082010-09-23 15:07:34 -0700549 /* make sure we're changing state */
550 if (rtc->uie_rtctimer.enabled == enabled)
551 goto out;
552
John Stultz4a649902012-03-06 17:16:09 -0800553 if (rtc->uie_unsupported) {
554 err = -EINVAL;
555 goto out;
556 }
557
John Stultz6610e082010-09-23 15:07:34 -0700558 if (enabled) {
559 struct rtc_time tm;
560 ktime_t now, onesec;
561
562 __rtc_read_time(rtc, &tm);
563 onesec = ktime_set(1, 0);
564 now = rtc_tm_to_ktime(tm);
565 rtc->uie_rtctimer.node.expires = ktime_add(now, onesec);
566 rtc->uie_rtctimer.period = ktime_set(1, 0);
John Stultzaa0be0f2011-01-20 15:26:12 -0800567 err = rtc_timer_enqueue(rtc, &rtc->uie_rtctimer);
Alexandre Belloni606cc432019-03-20 12:59:09 +0100568 } else {
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100569 rtc_timer_remove(rtc, &rtc->uie_rtctimer);
Alexandre Belloni606cc432019-03-20 12:59:09 +0100570 }
Alessandro Zummo099e6572009-01-04 12:00:54 -0800571
John Stultz6610e082010-09-23 15:07:34 -0700572out:
Alessandro Zummo099e6572009-01-04 12:00:54 -0800573 mutex_unlock(&rtc->ops_lock);
John Stultz456d66e2011-02-11 18:15:23 -0800574#ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
575 /*
Wolfram Sangc48cadf2019-04-03 17:19:52 +0200576 * Enable emulation if the driver returned -EINVAL to signal that it has
577 * been configured without interrupts or they are not available at the
578 * moment.
John Stultz456d66e2011-02-11 18:15:23 -0800579 */
580 if (err == -EINVAL)
581 err = rtc_dev_update_irq_enable_emul(rtc, enabled);
582#endif
Alessandro Zummo099e6572009-01-04 12:00:54 -0800583 return err;
584}
585EXPORT_SYMBOL_GPL(rtc_update_irq_enable);
586
David Brownelld728b1e2006-11-25 11:09:28 -0800587/**
John Stultz6610e082010-09-23 15:07:34 -0700588 * rtc_handle_legacy_irq - AIE, UIE and PIE event hook
589 * @rtc: pointer to the rtc device
590 *
591 * This function is called when an AIE, UIE or PIE mode interrupt
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300592 * has occurred (or been emulated).
John Stultz6610e082010-09-23 15:07:34 -0700593 *
John Stultz6610e082010-09-23 15:07:34 -0700594 */
John Stultz456d66e2011-02-11 18:15:23 -0800595void rtc_handle_legacy_irq(struct rtc_device *rtc, int num, int mode)
John Stultz6610e082010-09-23 15:07:34 -0700596{
597 unsigned long flags;
598
599 /* mark one irq of the appropriate mode */
600 spin_lock_irqsave(&rtc->irq_lock, flags);
Alexandre Belloni606cc432019-03-20 12:59:09 +0100601 rtc->irq_data = (rtc->irq_data + (num << 8)) | (RTC_IRQF | mode);
John Stultz6610e082010-09-23 15:07:34 -0700602 spin_unlock_irqrestore(&rtc->irq_lock, flags);
603
John Stultz6610e082010-09-23 15:07:34 -0700604 wake_up_interruptible(&rtc->irq_queue);
605 kill_fasync(&rtc->async_queue, SIGIO, POLL_IN);
606}
607
John Stultz6610e082010-09-23 15:07:34 -0700608/**
609 * rtc_aie_update_irq - AIE mode rtctimer hook
Alexandre Belloni9a0320112018-12-18 22:11:26 +0100610 * @rtc: pointer to the rtc_device
John Stultz6610e082010-09-23 15:07:34 -0700611 *
612 * This functions is called when the aie_timer expires.
613 */
Alexandre Belloni9a0320112018-12-18 22:11:26 +0100614void rtc_aie_update_irq(struct rtc_device *rtc)
John Stultz6610e082010-09-23 15:07:34 -0700615{
John Stultz6610e082010-09-23 15:07:34 -0700616 rtc_handle_legacy_irq(rtc, 1, RTC_AF);
617}
618
John Stultz6610e082010-09-23 15:07:34 -0700619/**
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
John Stultz6610e082010-09-23 15:07:34 -0700630/**
631 * rtc_pie_update_irq - PIE mode hrtimer hook
632 * @timer: pointer to the pie mode hrtimer
633 *
634 * This function is used to emulate PIE mode interrupts
635 * using an hrtimer. This function is called when the periodic
636 * hrtimer expires.
637 */
638enum hrtimer_restart rtc_pie_update_irq(struct hrtimer *timer)
639{
640 struct rtc_device *rtc;
641 ktime_t period;
642 int count;
Alexandre Belloni606cc432019-03-20 12:59:09 +0100643
John Stultz6610e082010-09-23 15:07:34 -0700644 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,
Alexandre Belloni606cc432019-03-20 12:59:09 +0100662 unsigned long num, unsigned long events)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800663{
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;
Alexandre Belloni606cc432019-03-20 12:59:09 +0100811
John Stultz6610e082010-09-23 15:07:34 -0700812 alarm.time = rtc_ktime_to_tm(timer->node.expires);
813 alarm.enabled = 1;
814 err = __rtc_set_alarm(rtc, &alarm);
Zoran Markovic14d0e342013-06-26 16:09:13 -0700815 if (err == -ETIME) {
816 pm_stay_awake(rtc->dev.parent);
John Stultz6610e082010-09-23 15:07:34 -0700817 schedule_work(&rtc->irqwork);
Zoran Markovic14d0e342013-06-26 16:09:13 -0700818 } else if (err) {
John Stultzaa0be0f2011-01-20 15:26:12 -0800819 timerqueue_del(&rtc->timerqueue, &timer->node);
Baolin Wang29a1f592017-12-14 13:31:43 +0800820 trace_rtc_timer_dequeue(timer);
John Stultzaa0be0f2011-01-20 15:26:12 -0800821 timer->enabled = 0;
822 return err;
823 }
John Stultz6610e082010-09-23 15:07:34 -0700824 }
John Stultzaa0be0f2011-01-20 15:26:12 -0800825 return 0;
John Stultz6610e082010-09-23 15:07:34 -0700826}
827
Rabin Vincent41c7f742011-11-22 11:03:14 +0100828static void rtc_alarm_disable(struct rtc_device *rtc)
829{
830 if (!rtc->ops || !rtc->ops->alarm_irq_enable)
831 return;
832
833 rtc->ops->alarm_irq_enable(rtc->dev.parent, false);
Baolin Wang29a1f592017-12-14 13:31:43 +0800834 trace_rtc_alarm_irq_enable(0, 0);
Rabin Vincent41c7f742011-11-22 11:03:14 +0100835}
836
John Stultz6610e082010-09-23 15:07:34 -0700837/**
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100838 * rtc_timer_remove - Removes a rtc_timer from the rtc_device timerqueue
John Stultz6610e082010-09-23 15:07:34 -0700839 * @rtc rtc device
840 * @timer timer being removed.
841 *
842 * Removes a timer onto the rtc devices timerqueue and sets
843 * the next alarm event appropriately.
844 *
John Stultzaa0be0f2011-01-20 15:26:12 -0800845 * Clears the enabled bit on the removed timer.
846 *
John Stultz6610e082010-09-23 15:07:34 -0700847 * Must hold ops_lock for proper serialization of timerqueue
848 */
John Stultzaa0be0f2011-01-20 15:26:12 -0800849static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer)
John Stultz6610e082010-09-23 15:07:34 -0700850{
851 struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue);
Alexandre Belloni606cc432019-03-20 12:59:09 +0100852
John Stultz6610e082010-09-23 15:07:34 -0700853 timerqueue_del(&rtc->timerqueue, &timer->node);
Baolin Wang29a1f592017-12-14 13:31:43 +0800854 trace_rtc_timer_dequeue(timer);
John Stultzaa0be0f2011-01-20 15:26:12 -0800855 timer->enabled = 0;
John Stultz6610e082010-09-23 15:07:34 -0700856 if (next == &timer->node) {
857 struct rtc_wkalrm alarm;
858 int err;
Alexandre Belloni606cc432019-03-20 12:59:09 +0100859
John Stultz6610e082010-09-23 15:07:34 -0700860 next = timerqueue_getnext(&rtc->timerqueue);
Rabin Vincent41c7f742011-11-22 11:03:14 +0100861 if (!next) {
862 rtc_alarm_disable(rtc);
John Stultz6610e082010-09-23 15:07:34 -0700863 return;
Rabin Vincent41c7f742011-11-22 11:03:14 +0100864 }
John Stultz6610e082010-09-23 15:07:34 -0700865 alarm.time = rtc_ktime_to_tm(next->expires);
866 alarm.enabled = 1;
867 err = __rtc_set_alarm(rtc, &alarm);
Zoran Markovic14d0e342013-06-26 16:09:13 -0700868 if (err == -ETIME) {
869 pm_stay_awake(rtc->dev.parent);
John Stultz6610e082010-09-23 15:07:34 -0700870 schedule_work(&rtc->irqwork);
Zoran Markovic14d0e342013-06-26 16:09:13 -0700871 }
John Stultz6610e082010-09-23 15:07:34 -0700872 }
873}
874
875/**
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100876 * rtc_timer_do_work - Expires rtc timers
John Stultz6610e082010-09-23 15:07:34 -0700877 * @rtc rtc device
878 * @timer timer being removed.
879 *
880 * Expires rtc timers. Reprograms next alarm event if needed.
881 * Called via worktask.
882 *
883 * Serializes access to timerqueue via ops_lock mutex
884 */
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100885void rtc_timer_do_work(struct work_struct *work)
John Stultz6610e082010-09-23 15:07:34 -0700886{
887 struct rtc_timer *timer;
888 struct timerqueue_node *next;
889 ktime_t now;
890 struct rtc_time tm;
891
892 struct rtc_device *rtc =
893 container_of(work, struct rtc_device, irqwork);
894
895 mutex_lock(&rtc->ops_lock);
896again:
897 __rtc_read_time(rtc, &tm);
898 now = rtc_tm_to_ktime(tm);
899 while ((next = timerqueue_getnext(&rtc->timerqueue))) {
Thomas Gleixner2456e852016-12-25 11:38:40 +0100900 if (next->expires > now)
John Stultz6610e082010-09-23 15:07:34 -0700901 break;
902
903 /* expire timer */
904 timer = container_of(next, struct rtc_timer, node);
905 timerqueue_del(&rtc->timerqueue, &timer->node);
Baolin Wang29a1f592017-12-14 13:31:43 +0800906 trace_rtc_timer_dequeue(timer);
John Stultz6610e082010-09-23 15:07:34 -0700907 timer->enabled = 0;
Alexandre Belloni5a5ba10f2018-07-26 15:40:56 +0200908 if (timer->func)
Alexandre Belloni9a0320112018-12-18 22:11:26 +0100909 timer->func(timer->rtc);
John Stultz6610e082010-09-23 15:07:34 -0700910
Baolin Wang29a1f592017-12-14 13:31:43 +0800911 trace_rtc_timer_fired(timer);
John Stultz6610e082010-09-23 15:07:34 -0700912 /* Re-add/fwd periodic timers */
913 if (ktime_to_ns(timer->period)) {
914 timer->node.expires = ktime_add(timer->node.expires,
915 timer->period);
916 timer->enabled = 1;
917 timerqueue_add(&rtc->timerqueue, &timer->node);
Baolin Wang29a1f592017-12-14 13:31:43 +0800918 trace_rtc_timer_enqueue(timer);
John Stultz6610e082010-09-23 15:07:34 -0700919 }
920 }
921
922 /* Set next alarm */
923 if (next) {
924 struct rtc_wkalrm alarm;
925 int err;
Xunlei Pang6528b882014-12-10 15:54:26 -0800926 int retry = 3;
927
John Stultz6610e082010-09-23 15:07:34 -0700928 alarm.time = rtc_ktime_to_tm(next->expires);
929 alarm.enabled = 1;
Xunlei Pang6528b882014-12-10 15:54:26 -0800930reprogram:
John Stultz6610e082010-09-23 15:07:34 -0700931 err = __rtc_set_alarm(rtc, &alarm);
Alexandre Belloni606cc432019-03-20 12:59:09 +0100932 if (err == -ETIME) {
John Stultz6610e082010-09-23 15:07:34 -0700933 goto again;
Alexandre Belloni606cc432019-03-20 12:59:09 +0100934 } else if (err) {
Xunlei Pang6528b882014-12-10 15:54:26 -0800935 if (retry-- > 0)
936 goto reprogram;
937
938 timer = container_of(next, struct rtc_timer, node);
939 timerqueue_del(&rtc->timerqueue, &timer->node);
Baolin Wang29a1f592017-12-14 13:31:43 +0800940 trace_rtc_timer_dequeue(timer);
Xunlei Pang6528b882014-12-10 15:54:26 -0800941 timer->enabled = 0;
942 dev_err(&rtc->dev, "__rtc_set_alarm: err=%d\n", err);
943 goto again;
944 }
Alexandre Belloni606cc432019-03-20 12:59:09 +0100945 } else {
Rabin Vincent41c7f742011-11-22 11:03:14 +0100946 rtc_alarm_disable(rtc);
Alexandre Belloni606cc432019-03-20 12:59:09 +0100947 }
John Stultz6610e082010-09-23 15:07:34 -0700948
Zoran Markovic14d0e342013-06-26 16:09:13 -0700949 pm_relax(rtc->dev.parent);
John Stultz6610e082010-09-23 15:07:34 -0700950 mutex_unlock(&rtc->ops_lock);
951}
952
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100953/* rtc_timer_init - Initializes an rtc_timer
John Stultz6610e082010-09-23 15:07:34 -0700954 * @timer: timer to be intiialized
955 * @f: function pointer to be called when timer fires
Alexandre Belloni9a0320112018-12-18 22:11:26 +0100956 * @rtc: pointer to the rtc_device
John Stultz6610e082010-09-23 15:07:34 -0700957 *
958 * Kernel interface to initializing an rtc_timer.
959 */
Alexandre Belloni9a0320112018-12-18 22:11:26 +0100960void rtc_timer_init(struct rtc_timer *timer, void (*f)(struct rtc_device *r),
961 struct rtc_device *rtc)
John Stultz6610e082010-09-23 15:07:34 -0700962{
963 timerqueue_init(&timer->node);
964 timer->enabled = 0;
Alexandre Belloni5a5ba10f2018-07-26 15:40:56 +0200965 timer->func = f;
Alexandre Belloni9a0320112018-12-18 22:11:26 +0100966 timer->rtc = rtc;
John Stultz6610e082010-09-23 15:07:34 -0700967}
968
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100969/* rtc_timer_start - Sets an rtc_timer to fire in the future
John Stultz6610e082010-09-23 15:07:34 -0700970 * @ rtc: rtc device to be used
971 * @ timer: timer being set
972 * @ expires: time at which to expire the timer
973 * @ period: period that the timer will recur
974 *
975 * Kernel interface to set an rtc_timer
976 */
Sachin Kamat3ff2e132013-07-03 15:05:42 -0700977int rtc_timer_start(struct rtc_device *rtc, struct rtc_timer *timer,
Alexandre Belloni606cc432019-03-20 12:59:09 +0100978 ktime_t expires, ktime_t period)
John Stultz6610e082010-09-23 15:07:34 -0700979{
980 int ret = 0;
Alexandre Belloni606cc432019-03-20 12:59:09 +0100981
John Stultz6610e082010-09-23 15:07:34 -0700982 mutex_lock(&rtc->ops_lock);
983 if (timer->enabled)
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100984 rtc_timer_remove(rtc, timer);
John Stultz6610e082010-09-23 15:07:34 -0700985
986 timer->node.expires = expires;
987 timer->period = period;
988
John Stultzaa0be0f2011-01-20 15:26:12 -0800989 ret = rtc_timer_enqueue(rtc, timer);
John Stultz6610e082010-09-23 15:07:34 -0700990
991 mutex_unlock(&rtc->ops_lock);
992 return ret;
993}
994
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100995/* rtc_timer_cancel - Stops an rtc_timer
John Stultz6610e082010-09-23 15:07:34 -0700996 * @ rtc: rtc device to be used
997 * @ timer: timer being set
998 *
999 * Kernel interface to cancel an rtc_timer
1000 */
Krzysztof Kozlowski73744a62015-05-03 18:57:11 +09001001void rtc_timer_cancel(struct rtc_device *rtc, struct rtc_timer *timer)
John Stultz6610e082010-09-23 15:07:34 -07001002{
John Stultz6610e082010-09-23 15:07:34 -07001003 mutex_lock(&rtc->ops_lock);
1004 if (timer->enabled)
Thomas Gleixner96c8f062010-12-13 22:45:48 +01001005 rtc_timer_remove(rtc, timer);
John Stultz6610e082010-09-23 15:07:34 -07001006 mutex_unlock(&rtc->ops_lock);
John Stultz6610e082010-09-23 15:07:34 -07001007}
1008
Joshua Claytonb3967062016-02-05 12:41:11 -08001009/**
1010 * rtc_read_offset - Read the amount of rtc offset in parts per billion
1011 * @ rtc: rtc device to be used
1012 * @ offset: the offset in parts per billion
1013 *
1014 * see below for details.
1015 *
1016 * Kernel interface to read rtc clock offset
1017 * Returns 0 on success, or a negative number on error.
1018 * If read_offset() is not implemented for the rtc, return -EINVAL
1019 */
1020int rtc_read_offset(struct rtc_device *rtc, long *offset)
1021{
1022 int ret;
John Stultz6610e082010-09-23 15:07:34 -07001023
Joshua Claytonb3967062016-02-05 12:41:11 -08001024 if (!rtc->ops)
1025 return -ENODEV;
1026
1027 if (!rtc->ops->read_offset)
1028 return -EINVAL;
1029
1030 mutex_lock(&rtc->ops_lock);
1031 ret = rtc->ops->read_offset(rtc->dev.parent, offset);
1032 mutex_unlock(&rtc->ops_lock);
Baolin Wang29a1f592017-12-14 13:31:43 +08001033
1034 trace_rtc_read_offset(*offset, ret);
Joshua Claytonb3967062016-02-05 12:41:11 -08001035 return ret;
1036}
1037
1038/**
1039 * rtc_set_offset - Adjusts the duration of the average second
1040 * @ rtc: rtc device to be used
1041 * @ offset: the offset in parts per billion
1042 *
1043 * Some rtc's allow an adjustment to the average duration of a second
1044 * to compensate for differences in the actual clock rate due to temperature,
1045 * the crystal, capacitor, etc.
1046 *
Russell King8a25c8f2017-09-29 11:23:25 +01001047 * The adjustment applied is as follows:
1048 * t = t0 * (1 + offset * 1e-9)
1049 * where t0 is the measured length of 1 RTC second with offset = 0
1050 *
Joshua Claytonb3967062016-02-05 12:41:11 -08001051 * Kernel interface to adjust an rtc clock offset.
1052 * Return 0 on success, or a negative number on error.
1053 * If the rtc offset is not setable (or not implemented), return -EINVAL
1054 */
1055int rtc_set_offset(struct rtc_device *rtc, long offset)
1056{
1057 int ret;
1058
1059 if (!rtc->ops)
1060 return -ENODEV;
1061
1062 if (!rtc->ops->set_offset)
1063 return -EINVAL;
1064
1065 mutex_lock(&rtc->ops_lock);
1066 ret = rtc->ops->set_offset(rtc->dev.parent, offset);
1067 mutex_unlock(&rtc->ops_lock);
Baolin Wang29a1f592017-12-14 13:31:43 +08001068
1069 trace_rtc_set_offset(offset, ret);
Joshua Claytonb3967062016-02-05 12:41:11 -08001070 return ret;
1071}