blob: ccb7d6b4da3bb6b574eb2294dab112c36bce773c [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 /*
576 * Enable emulation if the driver did not provide
577 * the update_irq_enable function pointer or if returned
578 * -EINVAL to signal that it has been configured without
579 * interrupts or that are not available at the moment.
580 */
581 if (err == -EINVAL)
582 err = rtc_dev_update_irq_enable_emul(rtc, enabled);
583#endif
Alessandro Zummo099e6572009-01-04 12:00:54 -0800584 return err;
585}
586EXPORT_SYMBOL_GPL(rtc_update_irq_enable);
587
David Brownelld728b1e2006-11-25 11:09:28 -0800588/**
John Stultz6610e082010-09-23 15:07:34 -0700589 * rtc_handle_legacy_irq - AIE, UIE and PIE event hook
590 * @rtc: pointer to the rtc device
591 *
592 * This function is called when an AIE, UIE or PIE mode interrupt
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300593 * has occurred (or been emulated).
John Stultz6610e082010-09-23 15:07:34 -0700594 *
John Stultz6610e082010-09-23 15:07:34 -0700595 */
John Stultz456d66e2011-02-11 18:15:23 -0800596void rtc_handle_legacy_irq(struct rtc_device *rtc, int num, int mode)
John Stultz6610e082010-09-23 15:07:34 -0700597{
598 unsigned long flags;
599
600 /* mark one irq of the appropriate mode */
601 spin_lock_irqsave(&rtc->irq_lock, flags);
Alexandre Belloni606cc432019-03-20 12:59:09 +0100602 rtc->irq_data = (rtc->irq_data + (num << 8)) | (RTC_IRQF | mode);
John Stultz6610e082010-09-23 15:07:34 -0700603 spin_unlock_irqrestore(&rtc->irq_lock, flags);
604
John Stultz6610e082010-09-23 15:07:34 -0700605 wake_up_interruptible(&rtc->irq_queue);
606 kill_fasync(&rtc->async_queue, SIGIO, POLL_IN);
607}
608
John Stultz6610e082010-09-23 15:07:34 -0700609/**
610 * rtc_aie_update_irq - AIE mode rtctimer hook
Alexandre Belloni9a0320112018-12-18 22:11:26 +0100611 * @rtc: pointer to the rtc_device
John Stultz6610e082010-09-23 15:07:34 -0700612 *
613 * This functions is called when the aie_timer expires.
614 */
Alexandre Belloni9a0320112018-12-18 22:11:26 +0100615void rtc_aie_update_irq(struct rtc_device *rtc)
John Stultz6610e082010-09-23 15:07:34 -0700616{
John Stultz6610e082010-09-23 15:07:34 -0700617 rtc_handle_legacy_irq(rtc, 1, RTC_AF);
618}
619
John Stultz6610e082010-09-23 15:07:34 -0700620/**
621 * rtc_uie_update_irq - UIE mode rtctimer hook
Alexandre Belloni9a0320112018-12-18 22:11:26 +0100622 * @rtc: pointer to the rtc_device
John Stultz6610e082010-09-23 15:07:34 -0700623 *
624 * This functions is called when the uie_timer expires.
625 */
Alexandre Belloni9a0320112018-12-18 22:11:26 +0100626void rtc_uie_update_irq(struct rtc_device *rtc)
John Stultz6610e082010-09-23 15:07:34 -0700627{
John Stultz6610e082010-09-23 15:07:34 -0700628 rtc_handle_legacy_irq(rtc, 1, RTC_UF);
629}
630
John Stultz6610e082010-09-23 15:07:34 -0700631/**
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;
Alexandre Belloni606cc432019-03-20 12:59:09 +0100644
John Stultz6610e082010-09-23 15:07:34 -0700645 rtc = container_of(timer, struct rtc_device, pie_timer);
646
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100647 period = NSEC_PER_SEC / rtc->irq_freq;
John Stultz6610e082010-09-23 15:07:34 -0700648 count = hrtimer_forward_now(timer, period);
649
650 rtc_handle_legacy_irq(rtc, count, RTC_PF);
651
652 return HRTIMER_RESTART;
653}
654
655/**
656 * rtc_update_irq - Triggered when a RTC interrupt occurs.
David Brownellab6a2d72007-05-08 00:33:30 -0700657 * @rtc: the rtc device
David Brownelld728b1e2006-11-25 11:09:28 -0800658 * @num: how many irqs are being reported (usually one)
659 * @events: mask of RTC_IRQF with one or more of RTC_PF, RTC_AF, RTC_UF
Atsushi Nemotoe6229bec2009-06-18 16:49:09 -0700660 * Context: any
David Brownelld728b1e2006-11-25 11:09:28 -0800661 */
David Brownellab6a2d72007-05-08 00:33:30 -0700662void rtc_update_irq(struct rtc_device *rtc,
Alexandre Belloni606cc432019-03-20 12:59:09 +0100663 unsigned long num, unsigned long events)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800664{
viresh kumare7cba882015-07-31 16:23:43 +0530665 if (IS_ERR_OR_NULL(rtc))
Alessandro Zummo131c9cc2014-04-03 14:50:09 -0700666 return;
667
NeilBrown7523cee2012-08-05 22:56:20 +0200668 pm_stay_awake(rtc->dev.parent);
John Stultz6610e082010-09-23 15:07:34 -0700669 schedule_work(&rtc->irqwork);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800670}
671EXPORT_SYMBOL_GPL(rtc_update_irq);
672
Michał Mirosław9f3b7952013-02-01 20:40:17 +0100673static int __rtc_match(struct device *dev, const void *data)
Dave Young71da8902008-01-22 14:00:34 +0800674{
Michał Mirosław9f3b7952013-02-01 20:40:17 +0100675 const char *name = data;
Dave Young71da8902008-01-22 14:00:34 +0800676
Kay Sieversd4afc762009-01-06 14:42:11 -0800677 if (strcmp(dev_name(dev), name) == 0)
Dave Young71da8902008-01-22 14:00:34 +0800678 return 1;
679 return 0;
680}
681
Michał Mirosław9f3b7952013-02-01 20:40:17 +0100682struct rtc_device *rtc_class_open(const char *name)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800683{
David Brownellcd966202007-05-08 00:33:40 -0700684 struct device *dev;
David Brownellab6a2d72007-05-08 00:33:30 -0700685 struct rtc_device *rtc = NULL;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800686
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -0400687 dev = class_find_device(rtc_class, NULL, name, __rtc_match);
Dave Young71da8902008-01-22 14:00:34 +0800688 if (dev)
689 rtc = to_rtc_device(dev);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800690
David Brownellab6a2d72007-05-08 00:33:30 -0700691 if (rtc) {
692 if (!try_module_get(rtc->owner)) {
David Brownellcd966202007-05-08 00:33:40 -0700693 put_device(dev);
David Brownellab6a2d72007-05-08 00:33:30 -0700694 rtc = NULL;
695 }
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800696 }
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800697
David Brownellab6a2d72007-05-08 00:33:30 -0700698 return rtc;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800699}
700EXPORT_SYMBOL_GPL(rtc_class_open);
701
David Brownellab6a2d72007-05-08 00:33:30 -0700702void rtc_class_close(struct rtc_device *rtc)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800703{
David Brownellab6a2d72007-05-08 00:33:30 -0700704 module_put(rtc->owner);
David Brownellcd966202007-05-08 00:33:40 -0700705 put_device(&rtc->dev);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800706}
707EXPORT_SYMBOL_GPL(rtc_class_close);
708
Thomas Gleixner3c8bb90e2011-07-22 09:12:51 +0000709static int rtc_update_hrtimer(struct rtc_device *rtc, int enabled)
710{
711 /*
712 * We always cancel the timer here first, because otherwise
713 * we could run into BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
714 * when we manage to start the timer before the callback
715 * returns HRTIMER_RESTART.
716 *
717 * We cannot use hrtimer_cancel() here as a running callback
718 * could be blocked on rtc->irq_task_lock and hrtimer_cancel()
719 * would spin forever.
720 */
721 if (hrtimer_try_to_cancel(&rtc->pie_timer) < 0)
722 return -1;
723
724 if (enabled) {
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100725 ktime_t period = NSEC_PER_SEC / rtc->irq_freq;
Thomas Gleixner3c8bb90e2011-07-22 09:12:51 +0000726
727 hrtimer_start(&rtc->pie_timer, period, HRTIMER_MODE_REL);
728 }
729 return 0;
730}
731
David Brownell97144c62007-10-16 01:28:16 -0700732/**
733 * rtc_irq_set_state - enable/disable 2^N Hz periodic IRQs
734 * @rtc: the rtc device
David Brownell97144c62007-10-16 01:28:16 -0700735 * @enabled: true to enable periodic IRQs
736 * Context: any
737 *
738 * Note that rtc_irq_set_freq() should previously have been used to
Alexandre Belloniacecb3a2018-07-25 14:58:10 +0200739 * specify the desired frequency of periodic IRQ.
David Brownell97144c62007-10-16 01:28:16 -0700740 */
Alexandre Belloni8719d3c2018-07-25 15:07:09 +0200741int rtc_irq_set_state(struct rtc_device *rtc, int enabled)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800742{
743 int err = 0;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800744
Alexandre Belloniacecb3a2018-07-25 14:58:10 +0200745 while (rtc_update_hrtimer(rtc, enabled) < 0)
746 cpu_relax();
747
748 rtc->pie_enabled = enabled;
Baolin Wang29a1f592017-12-14 13:31:43 +0800749
750 trace_rtc_irq_set_state(enabled, err);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800751 return err;
752}
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800753
David Brownell97144c62007-10-16 01:28:16 -0700754/**
755 * rtc_irq_set_freq - set 2^N Hz periodic IRQ frequency for IRQ
756 * @rtc: the rtc device
Alexandre Belloniacecb3a2018-07-25 14:58:10 +0200757 * @freq: positive frequency
David Brownell97144c62007-10-16 01:28:16 -0700758 * Context: any
759 *
760 * Note that rtc_irq_set_state() is used to enable or disable the
761 * periodic IRQs.
762 */
Alexandre Belloni8719d3c2018-07-25 15:07:09 +0200763int rtc_irq_set_freq(struct rtc_device *rtc, int freq)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800764{
Alessandro Zummo56f10c62006-06-25 05:48:20 -0700765 int err = 0;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800766
Thomas Gleixner6e7a3332011-07-22 09:12:51 +0000767 if (freq <= 0 || freq > RTC_MAX_FREQ)
Marcelo Roberto Jimenez83a06bf2011-02-02 16:04:02 -0200768 return -EINVAL;
Alexandre Belloniacecb3a2018-07-25 14:58:10 +0200769
770 rtc->irq_freq = freq;
771 while (rtc->pie_enabled && rtc_update_hrtimer(rtc, 1) < 0)
772 cpu_relax();
Baolin Wang29a1f592017-12-14 13:31:43 +0800773
774 trace_rtc_irq_set_freq(freq, err);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800775 return err;
776}
John Stultz6610e082010-09-23 15:07:34 -0700777
778/**
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100779 * rtc_timer_enqueue - Adds a rtc_timer to the rtc_device timerqueue
John Stultz6610e082010-09-23 15:07:34 -0700780 * @rtc rtc device
781 * @timer timer being added.
782 *
783 * Enqueues a timer onto the rtc devices timerqueue and sets
784 * the next alarm event appropriately.
785 *
John Stultzaa0be0f2011-01-20 15:26:12 -0800786 * Sets the enabled bit on the added timer.
787 *
John Stultz6610e082010-09-23 15:07:34 -0700788 * Must hold ops_lock for proper serialization of timerqueue
789 */
John Stultzaa0be0f2011-01-20 15:26:12 -0800790static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer)
John Stultz6610e082010-09-23 15:07:34 -0700791{
Colin Ian King2b2f5ff2016-05-16 17:22:54 +0100792 struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue);
793 struct rtc_time tm;
794 ktime_t now;
795
John Stultzaa0be0f2011-01-20 15:26:12 -0800796 timer->enabled = 1;
Colin Ian King2b2f5ff2016-05-16 17:22:54 +0100797 __rtc_read_time(rtc, &tm);
798 now = rtc_tm_to_ktime(tm);
799
800 /* Skip over expired timers */
801 while (next) {
Thomas Gleixner2456e852016-12-25 11:38:40 +0100802 if (next->expires >= now)
Colin Ian King2b2f5ff2016-05-16 17:22:54 +0100803 break;
804 next = timerqueue_iterate_next(next);
805 }
806
John Stultz6610e082010-09-23 15:07:34 -0700807 timerqueue_add(&rtc->timerqueue, &timer->node);
Baolin Wang29a1f592017-12-14 13:31:43 +0800808 trace_rtc_timer_enqueue(timer);
Alexandre Belloni74717b22017-09-28 13:53:27 +0200809 if (!next || ktime_before(timer->node.expires, next->expires)) {
John Stultz6610e082010-09-23 15:07:34 -0700810 struct rtc_wkalrm alarm;
811 int err;
Alexandre Belloni606cc432019-03-20 12:59:09 +0100812
John Stultz6610e082010-09-23 15:07:34 -0700813 alarm.time = rtc_ktime_to_tm(timer->node.expires);
814 alarm.enabled = 1;
815 err = __rtc_set_alarm(rtc, &alarm);
Zoran Markovic14d0e342013-06-26 16:09:13 -0700816 if (err == -ETIME) {
817 pm_stay_awake(rtc->dev.parent);
John Stultz6610e082010-09-23 15:07:34 -0700818 schedule_work(&rtc->irqwork);
Zoran Markovic14d0e342013-06-26 16:09:13 -0700819 } else if (err) {
John Stultzaa0be0f2011-01-20 15:26:12 -0800820 timerqueue_del(&rtc->timerqueue, &timer->node);
Baolin Wang29a1f592017-12-14 13:31:43 +0800821 trace_rtc_timer_dequeue(timer);
John Stultzaa0be0f2011-01-20 15:26:12 -0800822 timer->enabled = 0;
823 return err;
824 }
John Stultz6610e082010-09-23 15:07:34 -0700825 }
John Stultzaa0be0f2011-01-20 15:26:12 -0800826 return 0;
John Stultz6610e082010-09-23 15:07:34 -0700827}
828
Rabin Vincent41c7f742011-11-22 11:03:14 +0100829static void rtc_alarm_disable(struct rtc_device *rtc)
830{
831 if (!rtc->ops || !rtc->ops->alarm_irq_enable)
832 return;
833
834 rtc->ops->alarm_irq_enable(rtc->dev.parent, false);
Baolin Wang29a1f592017-12-14 13:31:43 +0800835 trace_rtc_alarm_irq_enable(0, 0);
Rabin Vincent41c7f742011-11-22 11:03:14 +0100836}
837
John Stultz6610e082010-09-23 15:07:34 -0700838/**
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100839 * rtc_timer_remove - Removes a rtc_timer from the rtc_device timerqueue
John Stultz6610e082010-09-23 15:07:34 -0700840 * @rtc rtc device
841 * @timer timer being removed.
842 *
843 * Removes a timer onto the rtc devices timerqueue and sets
844 * the next alarm event appropriately.
845 *
John Stultzaa0be0f2011-01-20 15:26:12 -0800846 * Clears the enabled bit on the removed timer.
847 *
John Stultz6610e082010-09-23 15:07:34 -0700848 * Must hold ops_lock for proper serialization of timerqueue
849 */
John Stultzaa0be0f2011-01-20 15:26:12 -0800850static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer)
John Stultz6610e082010-09-23 15:07:34 -0700851{
852 struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue);
Alexandre Belloni606cc432019-03-20 12:59:09 +0100853
John Stultz6610e082010-09-23 15:07:34 -0700854 timerqueue_del(&rtc->timerqueue, &timer->node);
Baolin Wang29a1f592017-12-14 13:31:43 +0800855 trace_rtc_timer_dequeue(timer);
John Stultzaa0be0f2011-01-20 15:26:12 -0800856 timer->enabled = 0;
John Stultz6610e082010-09-23 15:07:34 -0700857 if (next == &timer->node) {
858 struct rtc_wkalrm alarm;
859 int err;
Alexandre Belloni606cc432019-03-20 12:59:09 +0100860
John Stultz6610e082010-09-23 15:07:34 -0700861 next = timerqueue_getnext(&rtc->timerqueue);
Rabin Vincent41c7f742011-11-22 11:03:14 +0100862 if (!next) {
863 rtc_alarm_disable(rtc);
John Stultz6610e082010-09-23 15:07:34 -0700864 return;
Rabin Vincent41c7f742011-11-22 11:03:14 +0100865 }
John Stultz6610e082010-09-23 15:07:34 -0700866 alarm.time = rtc_ktime_to_tm(next->expires);
867 alarm.enabled = 1;
868 err = __rtc_set_alarm(rtc, &alarm);
Zoran Markovic14d0e342013-06-26 16:09:13 -0700869 if (err == -ETIME) {
870 pm_stay_awake(rtc->dev.parent);
John Stultz6610e082010-09-23 15:07:34 -0700871 schedule_work(&rtc->irqwork);
Zoran Markovic14d0e342013-06-26 16:09:13 -0700872 }
John Stultz6610e082010-09-23 15:07:34 -0700873 }
874}
875
876/**
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100877 * rtc_timer_do_work - Expires rtc timers
John Stultz6610e082010-09-23 15:07:34 -0700878 * @rtc rtc device
879 * @timer timer being removed.
880 *
881 * Expires rtc timers. Reprograms next alarm event if needed.
882 * Called via worktask.
883 *
884 * Serializes access to timerqueue via ops_lock mutex
885 */
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100886void rtc_timer_do_work(struct work_struct *work)
John Stultz6610e082010-09-23 15:07:34 -0700887{
888 struct rtc_timer *timer;
889 struct timerqueue_node *next;
890 ktime_t now;
891 struct rtc_time tm;
892
893 struct rtc_device *rtc =
894 container_of(work, struct rtc_device, irqwork);
895
896 mutex_lock(&rtc->ops_lock);
897again:
898 __rtc_read_time(rtc, &tm);
899 now = rtc_tm_to_ktime(tm);
900 while ((next = timerqueue_getnext(&rtc->timerqueue))) {
Thomas Gleixner2456e852016-12-25 11:38:40 +0100901 if (next->expires > now)
John Stultz6610e082010-09-23 15:07:34 -0700902 break;
903
904 /* expire timer */
905 timer = container_of(next, struct rtc_timer, node);
906 timerqueue_del(&rtc->timerqueue, &timer->node);
Baolin Wang29a1f592017-12-14 13:31:43 +0800907 trace_rtc_timer_dequeue(timer);
John Stultz6610e082010-09-23 15:07:34 -0700908 timer->enabled = 0;
Alexandre Belloni5a5ba10f2018-07-26 15:40:56 +0200909 if (timer->func)
Alexandre Belloni9a0320112018-12-18 22:11:26 +0100910 timer->func(timer->rtc);
John Stultz6610e082010-09-23 15:07:34 -0700911
Baolin Wang29a1f592017-12-14 13:31:43 +0800912 trace_rtc_timer_fired(timer);
John Stultz6610e082010-09-23 15:07:34 -0700913 /* Re-add/fwd periodic timers */
914 if (ktime_to_ns(timer->period)) {
915 timer->node.expires = ktime_add(timer->node.expires,
916 timer->period);
917 timer->enabled = 1;
918 timerqueue_add(&rtc->timerqueue, &timer->node);
Baolin Wang29a1f592017-12-14 13:31:43 +0800919 trace_rtc_timer_enqueue(timer);
John Stultz6610e082010-09-23 15:07:34 -0700920 }
921 }
922
923 /* Set next alarm */
924 if (next) {
925 struct rtc_wkalrm alarm;
926 int err;
Xunlei Pang6528b882014-12-10 15:54:26 -0800927 int retry = 3;
928
John Stultz6610e082010-09-23 15:07:34 -0700929 alarm.time = rtc_ktime_to_tm(next->expires);
930 alarm.enabled = 1;
Xunlei Pang6528b882014-12-10 15:54:26 -0800931reprogram:
John Stultz6610e082010-09-23 15:07:34 -0700932 err = __rtc_set_alarm(rtc, &alarm);
Alexandre Belloni606cc432019-03-20 12:59:09 +0100933 if (err == -ETIME) {
John Stultz6610e082010-09-23 15:07:34 -0700934 goto again;
Alexandre Belloni606cc432019-03-20 12:59:09 +0100935 } else if (err) {
Xunlei Pang6528b882014-12-10 15:54:26 -0800936 if (retry-- > 0)
937 goto reprogram;
938
939 timer = container_of(next, struct rtc_timer, node);
940 timerqueue_del(&rtc->timerqueue, &timer->node);
Baolin Wang29a1f592017-12-14 13:31:43 +0800941 trace_rtc_timer_dequeue(timer);
Xunlei Pang6528b882014-12-10 15:54:26 -0800942 timer->enabled = 0;
943 dev_err(&rtc->dev, "__rtc_set_alarm: err=%d\n", err);
944 goto again;
945 }
Alexandre Belloni606cc432019-03-20 12:59:09 +0100946 } else {
Rabin Vincent41c7f742011-11-22 11:03:14 +0100947 rtc_alarm_disable(rtc);
Alexandre Belloni606cc432019-03-20 12:59:09 +0100948 }
John Stultz6610e082010-09-23 15:07:34 -0700949
Zoran Markovic14d0e342013-06-26 16:09:13 -0700950 pm_relax(rtc->dev.parent);
John Stultz6610e082010-09-23 15:07:34 -0700951 mutex_unlock(&rtc->ops_lock);
952}
953
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100954/* rtc_timer_init - Initializes an rtc_timer
John Stultz6610e082010-09-23 15:07:34 -0700955 * @timer: timer to be intiialized
956 * @f: function pointer to be called when timer fires
Alexandre Belloni9a0320112018-12-18 22:11:26 +0100957 * @rtc: pointer to the rtc_device
John Stultz6610e082010-09-23 15:07:34 -0700958 *
959 * Kernel interface to initializing an rtc_timer.
960 */
Alexandre Belloni9a0320112018-12-18 22:11:26 +0100961void rtc_timer_init(struct rtc_timer *timer, void (*f)(struct rtc_device *r),
962 struct rtc_device *rtc)
John Stultz6610e082010-09-23 15:07:34 -0700963{
964 timerqueue_init(&timer->node);
965 timer->enabled = 0;
Alexandre Belloni5a5ba10f2018-07-26 15:40:56 +0200966 timer->func = f;
Alexandre Belloni9a0320112018-12-18 22:11:26 +0100967 timer->rtc = rtc;
John Stultz6610e082010-09-23 15:07:34 -0700968}
969
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100970/* rtc_timer_start - Sets an rtc_timer to fire in the future
John Stultz6610e082010-09-23 15:07:34 -0700971 * @ rtc: rtc device to be used
972 * @ timer: timer being set
973 * @ expires: time at which to expire the timer
974 * @ period: period that the timer will recur
975 *
976 * Kernel interface to set an rtc_timer
977 */
Sachin Kamat3ff2e132013-07-03 15:05:42 -0700978int rtc_timer_start(struct rtc_device *rtc, struct rtc_timer *timer,
Alexandre Belloni606cc432019-03-20 12:59:09 +0100979 ktime_t expires, ktime_t period)
John Stultz6610e082010-09-23 15:07:34 -0700980{
981 int ret = 0;
Alexandre Belloni606cc432019-03-20 12:59:09 +0100982
John Stultz6610e082010-09-23 15:07:34 -0700983 mutex_lock(&rtc->ops_lock);
984 if (timer->enabled)
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100985 rtc_timer_remove(rtc, timer);
John Stultz6610e082010-09-23 15:07:34 -0700986
987 timer->node.expires = expires;
988 timer->period = period;
989
John Stultzaa0be0f2011-01-20 15:26:12 -0800990 ret = rtc_timer_enqueue(rtc, timer);
John Stultz6610e082010-09-23 15:07:34 -0700991
992 mutex_unlock(&rtc->ops_lock);
993 return ret;
994}
995
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100996/* rtc_timer_cancel - Stops an rtc_timer
John Stultz6610e082010-09-23 15:07:34 -0700997 * @ rtc: rtc device to be used
998 * @ timer: timer being set
999 *
1000 * Kernel interface to cancel an rtc_timer
1001 */
Krzysztof Kozlowski73744a62015-05-03 18:57:11 +09001002void rtc_timer_cancel(struct rtc_device *rtc, struct rtc_timer *timer)
John Stultz6610e082010-09-23 15:07:34 -07001003{
John Stultz6610e082010-09-23 15:07:34 -07001004 mutex_lock(&rtc->ops_lock);
1005 if (timer->enabled)
Thomas Gleixner96c8f062010-12-13 22:45:48 +01001006 rtc_timer_remove(rtc, timer);
John Stultz6610e082010-09-23 15:07:34 -07001007 mutex_unlock(&rtc->ops_lock);
John Stultz6610e082010-09-23 15:07:34 -07001008}
1009
Joshua Claytonb3967062016-02-05 12:41:11 -08001010/**
1011 * rtc_read_offset - Read the amount of rtc offset in parts per billion
1012 * @ rtc: rtc device to be used
1013 * @ offset: the offset in parts per billion
1014 *
1015 * see below for details.
1016 *
1017 * Kernel interface to read rtc clock offset
1018 * Returns 0 on success, or a negative number on error.
1019 * If read_offset() is not implemented for the rtc, return -EINVAL
1020 */
1021int rtc_read_offset(struct rtc_device *rtc, long *offset)
1022{
1023 int ret;
John Stultz6610e082010-09-23 15:07:34 -07001024
Joshua Claytonb3967062016-02-05 12:41:11 -08001025 if (!rtc->ops)
1026 return -ENODEV;
1027
1028 if (!rtc->ops->read_offset)
1029 return -EINVAL;
1030
1031 mutex_lock(&rtc->ops_lock);
1032 ret = rtc->ops->read_offset(rtc->dev.parent, offset);
1033 mutex_unlock(&rtc->ops_lock);
Baolin Wang29a1f592017-12-14 13:31:43 +08001034
1035 trace_rtc_read_offset(*offset, ret);
Joshua Claytonb3967062016-02-05 12:41:11 -08001036 return ret;
1037}
1038
1039/**
1040 * rtc_set_offset - Adjusts the duration of the average second
1041 * @ rtc: rtc device to be used
1042 * @ offset: the offset in parts per billion
1043 *
1044 * Some rtc's allow an adjustment to the average duration of a second
1045 * to compensate for differences in the actual clock rate due to temperature,
1046 * the crystal, capacitor, etc.
1047 *
Russell King8a25c8f2017-09-29 11:23:25 +01001048 * The adjustment applied is as follows:
1049 * t = t0 * (1 + offset * 1e-9)
1050 * where t0 is the measured length of 1 RTC second with offset = 0
1051 *
Joshua Claytonb3967062016-02-05 12:41:11 -08001052 * Kernel interface to adjust an rtc clock offset.
1053 * Return 0 on success, or a negative number on error.
1054 * If the rtc offset is not setable (or not implemented), return -EINVAL
1055 */
1056int rtc_set_offset(struct rtc_device *rtc, long offset)
1057{
1058 int ret;
1059
1060 if (!rtc->ops)
1061 return -ENODEV;
1062
1063 if (!rtc->ops->set_offset)
1064 return -EINVAL;
1065
1066 mutex_lock(&rtc->ops_lock);
1067 ret = rtc->ops->set_offset(rtc->dev.parent, offset);
1068 mutex_unlock(&rtc->ops_lock);
Baolin Wang29a1f592017-12-14 13:31:43 +08001069
1070 trace_rtc_set_offset(offset, ret);
Joshua Claytonb3967062016-02-05 12:41:11 -08001071 return ret;
1072}