blob: c068daebeec28f31bf3a37c1c616c4c0e9d75928 [file] [log] [blame]
Alessandro Zummo0c86edc2006-03-27 01:16:37 -08001/*
2 * RTC subsystem, interface functions
3 *
4 * Copyright (C) 2005 Tower Technologies
5 * Author: Alessandro Zummo <a.zummo@towertech.it>
6 *
7 * based on arch/arm/common/rtctime.c
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12*/
13
14#include <linux/rtc.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040015#include <linux/sched.h>
Paul Gortmaker21138522011-05-27 09:57:25 -040016#include <linux/module.h>
David Brownell97144c62007-10-16 01:28:16 -070017#include <linux/log2.h>
John Stultz6610e082010-09-23 15:07:34 -070018#include <linux/workqueue.h>
19
Baolin Wang29a1f592017-12-14 13:31:43 +080020#define CREATE_TRACE_POINTS
21#include <trace/events/rtc.h>
22
John Stultzaa0be0f2011-01-20 15:26:12 -080023static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer);
24static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer);
25
John Stultz6610e082010-09-23 15:07:34 -070026static int __rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm)
27{
28 int err;
29 if (!rtc->ops)
30 err = -ENODEV;
31 else if (!rtc->ops->read_time)
32 err = -EINVAL;
33 else {
34 memset(tm, 0, sizeof(struct rtc_time));
35 err = rtc->ops->read_time(rtc->dev.parent, tm);
Hyogi Gim16682c862014-12-10 15:52:27 -080036 if (err < 0) {
Aaro Koskinend0bddb52015-04-16 12:45:51 -070037 dev_dbg(&rtc->dev, "read_time: fail to read: %d\n",
38 err);
Hyogi Gim16682c862014-12-10 15:52:27 -080039 return err;
40 }
41
42 err = rtc_valid_tm(tm);
43 if (err < 0)
Aaro Koskinend0bddb52015-04-16 12:45:51 -070044 dev_dbg(&rtc->dev, "read_time: rtc_time isn't valid\n");
John Stultz6610e082010-09-23 15:07:34 -070045 }
46 return err;
47}
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080048
David Brownellab6a2d72007-05-08 00:33:30 -070049int rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080050{
51 int err;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080052
53 err = mutex_lock_interruptible(&rtc->ops_lock);
54 if (err)
David Brownellb68bb262008-07-29 22:33:30 -070055 return err;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080056
John Stultz6610e082010-09-23 15:07:34 -070057 err = __rtc_read_time(rtc, tm);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080058 mutex_unlock(&rtc->ops_lock);
Baolin Wang29a1f592017-12-14 13:31:43 +080059
60 trace_rtc_read_time(rtc_tm_to_time64(tm), err);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080061 return err;
62}
63EXPORT_SYMBOL_GPL(rtc_read_time);
64
David Brownellab6a2d72007-05-08 00:33:30 -070065int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080066{
67 int err;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080068
69 err = rtc_valid_tm(tm);
70 if (err != 0)
71 return err;
72
Alexandre Belloni71db0492018-02-17 14:58:40 +010073 if (rtc->range_min != rtc->range_max) {
74 time64_t time = rtc_tm_to_time64(tm);
75
76 if (time < rtc->range_min || time > rtc->range_max)
77 return -ERANGE;
78 }
79
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080080 err = mutex_lock_interruptible(&rtc->ops_lock);
81 if (err)
David Brownellb68bb262008-07-29 22:33:30 -070082 return err;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080083
84 if (!rtc->ops)
85 err = -ENODEV;
Alessandro Zummobbccf832009-01-06 14:42:21 -080086 else if (rtc->ops->set_time)
David Brownellcd966202007-05-08 00:33:40 -070087 err = rtc->ops->set_time(rtc->dev.parent, tm);
Xunlei Pang8e4ff1a2015-04-01 20:34:27 -070088 else if (rtc->ops->set_mmss64) {
89 time64_t secs64 = rtc_tm_to_time64(tm);
90
91 err = rtc->ops->set_mmss64(rtc->dev.parent, secs64);
92 } else if (rtc->ops->set_mmss) {
Xunlei Pangbc10aa92015-01-22 02:31:51 +000093 time64_t secs64 = rtc_tm_to_time64(tm);
94 err = rtc->ops->set_mmss(rtc->dev.parent, secs64);
Alessandro Zummobbccf832009-01-06 14:42:21 -080095 } else
96 err = -EINVAL;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080097
Zoran Markovic14d0e342013-06-26 16:09:13 -070098 pm_stay_awake(rtc->dev.parent);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080099 mutex_unlock(&rtc->ops_lock);
NeilBrown5f9679d2011-12-09 09:39:15 +1100100 /* A timer might have just expired */
101 schedule_work(&rtc->irqwork);
Baolin Wang29a1f592017-12-14 13:31:43 +0800102
103 trace_rtc_set_time(rtc_tm_to_time64(tm), err);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800104 return err;
105}
106EXPORT_SYMBOL_GPL(rtc_set_time);
107
John Stultzf44f7f92011-02-21 22:58:51 -0800108static int rtc_read_alarm_internal(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
109{
110 int err;
111
112 err = mutex_lock_interruptible(&rtc->ops_lock);
113 if (err)
114 return err;
115
116 if (rtc->ops == NULL)
117 err = -ENODEV;
118 else if (!rtc->ops->read_alarm)
119 err = -EINVAL;
120 else {
Uwe Kleine-Königd68778b2016-05-11 09:11:23 +0200121 alarm->enabled = 0;
122 alarm->pending = 0;
123 alarm->time.tm_sec = -1;
124 alarm->time.tm_min = -1;
125 alarm->time.tm_hour = -1;
126 alarm->time.tm_mday = -1;
127 alarm->time.tm_mon = -1;
128 alarm->time.tm_year = -1;
129 alarm->time.tm_wday = -1;
130 alarm->time.tm_yday = -1;
131 alarm->time.tm_isdst = -1;
John Stultzf44f7f92011-02-21 22:58:51 -0800132 err = rtc->ops->read_alarm(rtc->dev.parent, alarm);
133 }
134
135 mutex_unlock(&rtc->ops_lock);
Baolin Wang29a1f592017-12-14 13:31:43 +0800136
137 trace_rtc_read_alarm(rtc_tm_to_time64(&alarm->time), err);
John Stultzf44f7f92011-02-21 22:58:51 -0800138 return err;
139}
140
141int __rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
142{
143 int err;
144 struct rtc_time before, now;
145 int first_time = 1;
Xunlei Pangbc10aa92015-01-22 02:31:51 +0000146 time64_t t_now, t_alm;
John Stultzf44f7f92011-02-21 22:58:51 -0800147 enum { none, day, month, year } missing = none;
148 unsigned days;
149
150 /* The lower level RTC driver may return -1 in some fields,
151 * creating invalid alarm->time values, for reasons like:
152 *
153 * - The hardware may not be capable of filling them in;
154 * many alarms match only on time-of-day fields, not
155 * day/month/year calendar data.
156 *
157 * - Some hardware uses illegal values as "wildcard" match
158 * values, which non-Linux firmware (like a BIOS) may try
159 * to set up as e.g. "alarm 15 minutes after each hour".
160 * Linux uses only oneshot alarms.
161 *
162 * When we see that here, we deal with it by using values from
163 * a current RTC timestamp for any missing (-1) values. The
164 * RTC driver prevents "periodic alarm" modes.
165 *
166 * But this can be racey, because some fields of the RTC timestamp
167 * may have wrapped in the interval since we read the RTC alarm,
168 * which would lead to us inserting inconsistent values in place
169 * of the -1 fields.
170 *
171 * Reading the alarm and timestamp in the reverse sequence
172 * would have the same race condition, and not solve the issue.
173 *
174 * So, we must first read the RTC timestamp,
175 * then read the RTC alarm value,
176 * and then read a second RTC timestamp.
177 *
178 * If any fields of the second timestamp have changed
179 * when compared with the first timestamp, then we know
180 * our timestamp may be inconsistent with that used by
181 * the low-level rtc_read_alarm_internal() function.
182 *
183 * So, when the two timestamps disagree, we just loop and do
184 * the process again to get a fully consistent set of values.
185 *
186 * This could all instead be done in the lower level driver,
187 * but since more than one lower level RTC implementation needs it,
188 * then it's probably best best to do it here instead of there..
189 */
190
191 /* Get the "before" timestamp */
192 err = rtc_read_time(rtc, &before);
193 if (err < 0)
194 return err;
195 do {
196 if (!first_time)
197 memcpy(&before, &now, sizeof(struct rtc_time));
198 first_time = 0;
199
200 /* get the RTC alarm values, which may be incomplete */
201 err = rtc_read_alarm_internal(rtc, alarm);
202 if (err)
203 return err;
204
205 /* full-function RTCs won't have such missing fields */
206 if (rtc_valid_tm(&alarm->time) == 0)
207 return 0;
208
209 /* get the "after" timestamp, to detect wrapped fields */
210 err = rtc_read_time(rtc, &now);
211 if (err < 0)
212 return err;
213
214 /* note that tm_sec is a "don't care" value here: */
215 } while ( before.tm_min != now.tm_min
216 || before.tm_hour != now.tm_hour
217 || before.tm_mon != now.tm_mon
218 || before.tm_year != now.tm_year);
219
220 /* Fill in the missing alarm fields using the timestamp; we
221 * know there's at least one since alarm->time is invalid.
222 */
223 if (alarm->time.tm_sec == -1)
224 alarm->time.tm_sec = now.tm_sec;
225 if (alarm->time.tm_min == -1)
226 alarm->time.tm_min = now.tm_min;
227 if (alarm->time.tm_hour == -1)
228 alarm->time.tm_hour = now.tm_hour;
229
230 /* For simplicity, only support date rollover for now */
Ben Hutchingse74a8f22012-01-10 15:11:02 -0800231 if (alarm->time.tm_mday < 1 || alarm->time.tm_mday > 31) {
John Stultzf44f7f92011-02-21 22:58:51 -0800232 alarm->time.tm_mday = now.tm_mday;
233 missing = day;
234 }
Ben Hutchingse74a8f22012-01-10 15:11:02 -0800235 if ((unsigned)alarm->time.tm_mon >= 12) {
John Stultzf44f7f92011-02-21 22:58:51 -0800236 alarm->time.tm_mon = now.tm_mon;
237 if (missing == none)
238 missing = month;
239 }
240 if (alarm->time.tm_year == -1) {
241 alarm->time.tm_year = now.tm_year;
242 if (missing == none)
243 missing = year;
244 }
245
Vaibhav Jainda96aea2017-05-19 22:18:55 +0530246 /* Can't proceed if alarm is still invalid after replacing
247 * missing fields.
248 */
249 err = rtc_valid_tm(&alarm->time);
250 if (err)
251 goto done;
252
John Stultzf44f7f92011-02-21 22:58:51 -0800253 /* with luck, no rollover is needed */
Xunlei Pangbc10aa92015-01-22 02:31:51 +0000254 t_now = rtc_tm_to_time64(&now);
255 t_alm = rtc_tm_to_time64(&alarm->time);
John Stultzf44f7f92011-02-21 22:58:51 -0800256 if (t_now < t_alm)
257 goto done;
258
259 switch (missing) {
260
261 /* 24 hour rollover ... if it's now 10am Monday, an alarm that
262 * that will trigger at 5am will do so at 5am Tuesday, which
263 * could also be in the next month or year. This is a common
264 * case, especially for PCs.
265 */
266 case day:
267 dev_dbg(&rtc->dev, "alarm rollover: %s\n", "day");
268 t_alm += 24 * 60 * 60;
Xunlei Pangbc10aa92015-01-22 02:31:51 +0000269 rtc_time64_to_tm(t_alm, &alarm->time);
John Stultzf44f7f92011-02-21 22:58:51 -0800270 break;
271
272 /* Month rollover ... if it's the 31th, an alarm on the 3rd will
273 * be next month. An alarm matching on the 30th, 29th, or 28th
274 * may end up in the month after that! Many newer PCs support
275 * this type of alarm.
276 */
277 case month:
278 dev_dbg(&rtc->dev, "alarm rollover: %s\n", "month");
279 do {
280 if (alarm->time.tm_mon < 11)
281 alarm->time.tm_mon++;
282 else {
283 alarm->time.tm_mon = 0;
284 alarm->time.tm_year++;
285 }
286 days = rtc_month_days(alarm->time.tm_mon,
287 alarm->time.tm_year);
288 } while (days < alarm->time.tm_mday);
289 break;
290
291 /* Year rollover ... easy except for leap years! */
292 case year:
293 dev_dbg(&rtc->dev, "alarm rollover: %s\n", "year");
294 do {
295 alarm->time.tm_year++;
Ales Novakee1d9012014-06-06 14:35:39 -0700296 } while (!is_leap_year(alarm->time.tm_year + 1900)
297 && rtc_valid_tm(&alarm->time) != 0);
John Stultzf44f7f92011-02-21 22:58:51 -0800298 break;
299
300 default:
301 dev_warn(&rtc->dev, "alarm rollover not handled\n");
302 }
303
Ales Novakee1d9012014-06-06 14:35:39 -0700304 err = rtc_valid_tm(&alarm->time);
305
Vaibhav Jainda96aea2017-05-19 22:18:55 +0530306done:
Ales Novakee1d9012014-06-06 14:35:39 -0700307 if (err) {
308 dev_warn(&rtc->dev, "invalid alarm value: %d-%d-%d %d:%d:%d\n",
309 alarm->time.tm_year + 1900, alarm->time.tm_mon + 1,
310 alarm->time.tm_mday, alarm->time.tm_hour, alarm->time.tm_min,
311 alarm->time.tm_sec);
312 }
313
314 return err;
John Stultzf44f7f92011-02-21 22:58:51 -0800315}
316
John Stultz6610e082010-09-23 15:07:34 -0700317int rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800318{
319 int err;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800320
321 err = mutex_lock_interruptible(&rtc->ops_lock);
322 if (err)
David Brownellb68bb262008-07-29 22:33:30 -0700323 return err;
John Stultzd5553a52011-01-20 15:26:13 -0800324 if (rtc->ops == NULL)
325 err = -ENODEV;
326 else if (!rtc->ops->read_alarm)
327 err = -EINVAL;
328 else {
329 memset(alarm, 0, sizeof(struct rtc_wkalrm));
330 alarm->enabled = rtc->aie_timer.enabled;
John Stultz6610e082010-09-23 15:07:34 -0700331 alarm->time = rtc_ktime_to_tm(rtc->aie_timer.node.expires);
John Stultzd5553a52011-01-20 15:26:13 -0800332 }
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800333 mutex_unlock(&rtc->ops_lock);
Mark Lord0e36a9a2007-10-16 01:28:21 -0700334
Baolin Wang29a1f592017-12-14 13:31:43 +0800335 trace_rtc_read_alarm(rtc_tm_to_time64(&alarm->time), err);
John Stultzd5553a52011-01-20 15:26:13 -0800336 return err;
Mark Lord0e36a9a2007-10-16 01:28:21 -0700337}
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800338EXPORT_SYMBOL_GPL(rtc_read_alarm);
339
Mark Brownd576fe42011-06-01 11:13:16 +0100340static int __rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
John Stultz6610e082010-09-23 15:07:34 -0700341{
342 struct rtc_time tm;
Xunlei Pangbc10aa92015-01-22 02:31:51 +0000343 time64_t now, scheduled;
John Stultz6610e082010-09-23 15:07:34 -0700344 int err;
345
346 err = rtc_valid_tm(&alarm->time);
347 if (err)
348 return err;
Xunlei Pangbc10aa92015-01-22 02:31:51 +0000349 scheduled = rtc_tm_to_time64(&alarm->time);
John Stultz6610e082010-09-23 15:07:34 -0700350
351 /* Make sure we're not setting alarms in the past */
352 err = __rtc_read_time(rtc, &tm);
Hyogi Gimca6dc2d2014-08-08 14:20:11 -0700353 if (err)
354 return err;
Xunlei Pangbc10aa92015-01-22 02:31:51 +0000355 now = rtc_tm_to_time64(&tm);
John Stultz6610e082010-09-23 15:07:34 -0700356 if (scheduled <= now)
357 return -ETIME;
358 /*
359 * XXX - We just checked to make sure the alarm time is not
360 * in the past, but there is still a race window where if
361 * the is alarm set for the next second and the second ticks
362 * over right here, before we set the alarm.
363 */
364
Linus Torvalds157e8bf2012-01-03 17:32:13 -0800365 if (!rtc->ops)
366 err = -ENODEV;
367 else if (!rtc->ops->set_alarm)
368 err = -EINVAL;
369 else
370 err = rtc->ops->set_alarm(rtc->dev.parent, alarm);
371
Baolin Wang29a1f592017-12-14 13:31:43 +0800372 trace_rtc_set_alarm(rtc_tm_to_time64(&alarm->time), err);
Linus Torvalds157e8bf2012-01-03 17:32:13 -0800373 return err;
John Stultz6610e082010-09-23 15:07:34 -0700374}
375
David Brownellab6a2d72007-05-08 00:33:30 -0700376int rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800377{
378 int err;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800379
David Brownellf8245c22007-05-08 00:34:07 -0700380 err = rtc_valid_tm(&alarm->time);
381 if (err != 0)
382 return err;
383
Alexandre Belloni71db0492018-02-17 14:58:40 +0100384 if (rtc->range_min != rtc->range_max) {
385 time64_t time = rtc_tm_to_time64(&alarm->time);
386
387 if (time < rtc->range_min || time > rtc->range_max)
388 return -ERANGE;
389 }
390
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800391 err = mutex_lock_interruptible(&rtc->ops_lock);
392 if (err)
David Brownellb68bb262008-07-29 22:33:30 -0700393 return err;
Sachin Kamat3ff2e132013-07-03 15:05:42 -0700394 if (rtc->aie_timer.enabled)
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100395 rtc_timer_remove(rtc, &rtc->aie_timer);
Sachin Kamat3ff2e132013-07-03 15:05:42 -0700396
John Stultz6610e082010-09-23 15:07:34 -0700397 rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time);
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100398 rtc->aie_timer.period = 0;
Sachin Kamat3ff2e132013-07-03 15:05:42 -0700399 if (alarm->enabled)
John Stultzaa0be0f2011-01-20 15:26:12 -0800400 err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
Sachin Kamat3ff2e132013-07-03 15:05:42 -0700401
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800402 mutex_unlock(&rtc->ops_lock);
John Stultzaa0be0f2011-01-20 15:26:12 -0800403 return err;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800404}
405EXPORT_SYMBOL_GPL(rtc_set_alarm);
406
John Stultzf6d5b332011-03-29 18:00:27 -0700407/* Called once per device from rtc_device_register */
408int rtc_initialize_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
409{
410 int err;
John Stultzbd729d72012-01-05 15:21:19 -0800411 struct rtc_time now;
John Stultzf6d5b332011-03-29 18:00:27 -0700412
413 err = rtc_valid_tm(&alarm->time);
414 if (err != 0)
415 return err;
416
John Stultzbd729d72012-01-05 15:21:19 -0800417 err = rtc_read_time(rtc, &now);
418 if (err)
419 return err;
420
John Stultzf6d5b332011-03-29 18:00:27 -0700421 err = mutex_lock_interruptible(&rtc->ops_lock);
422 if (err)
423 return err;
424
425 rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time);
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100426 rtc->aie_timer.period = 0;
John Stultzbd729d72012-01-05 15:21:19 -0800427
Uwe Kleine-König6785b3b2016-07-02 17:28:12 +0200428 /* Alarm has to be enabled & in the future for us to enqueue it */
Thomas Gleixner2456e852016-12-25 11:38:40 +0100429 if (alarm->enabled && (rtc_tm_to_ktime(now) <
430 rtc->aie_timer.node.expires)) {
John Stultzbd729d72012-01-05 15:21:19 -0800431
John Stultzf6d5b332011-03-29 18:00:27 -0700432 rtc->aie_timer.enabled = 1;
433 timerqueue_add(&rtc->timerqueue, &rtc->aie_timer.node);
Baolin Wang29a1f592017-12-14 13:31:43 +0800434 trace_rtc_timer_enqueue(&rtc->aie_timer);
John Stultzf6d5b332011-03-29 18:00:27 -0700435 }
436 mutex_unlock(&rtc->ops_lock);
437 return err;
438}
439EXPORT_SYMBOL_GPL(rtc_initialize_alarm);
440
Alessandro Zummo099e6572009-01-04 12:00:54 -0800441int rtc_alarm_irq_enable(struct rtc_device *rtc, unsigned int enabled)
442{
443 int err = mutex_lock_interruptible(&rtc->ops_lock);
444 if (err)
445 return err;
446
John Stultz6610e082010-09-23 15:07:34 -0700447 if (rtc->aie_timer.enabled != enabled) {
John Stultzaa0be0f2011-01-20 15:26:12 -0800448 if (enabled)
449 err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
450 else
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100451 rtc_timer_remove(rtc, &rtc->aie_timer);
John Stultz6610e082010-09-23 15:07:34 -0700452 }
453
John Stultzaa0be0f2011-01-20 15:26:12 -0800454 if (err)
Uwe Kleine-König516373b2011-02-14 11:33:17 +0100455 /* nothing */;
456 else if (!rtc->ops)
Alessandro Zummo099e6572009-01-04 12:00:54 -0800457 err = -ENODEV;
458 else if (!rtc->ops->alarm_irq_enable)
459 err = -EINVAL;
460 else
461 err = rtc->ops->alarm_irq_enable(rtc->dev.parent, enabled);
462
463 mutex_unlock(&rtc->ops_lock);
Baolin Wang29a1f592017-12-14 13:31:43 +0800464
465 trace_rtc_alarm_irq_enable(enabled, err);
Alessandro Zummo099e6572009-01-04 12:00:54 -0800466 return err;
467}
468EXPORT_SYMBOL_GPL(rtc_alarm_irq_enable);
469
470int rtc_update_irq_enable(struct rtc_device *rtc, unsigned int enabled)
471{
472 int err = mutex_lock_interruptible(&rtc->ops_lock);
473 if (err)
474 return err;
475
John Stultz456d66e2011-02-11 18:15:23 -0800476#ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
477 if (enabled == 0 && rtc->uie_irq_active) {
478 mutex_unlock(&rtc->ops_lock);
479 return rtc_dev_update_irq_enable_emul(rtc, 0);
480 }
481#endif
John Stultz6610e082010-09-23 15:07:34 -0700482 /* make sure we're changing state */
483 if (rtc->uie_rtctimer.enabled == enabled)
484 goto out;
485
John Stultz4a649902012-03-06 17:16:09 -0800486 if (rtc->uie_unsupported) {
487 err = -EINVAL;
488 goto out;
489 }
490
John Stultz6610e082010-09-23 15:07:34 -0700491 if (enabled) {
492 struct rtc_time tm;
493 ktime_t now, onesec;
494
495 __rtc_read_time(rtc, &tm);
496 onesec = ktime_set(1, 0);
497 now = rtc_tm_to_ktime(tm);
498 rtc->uie_rtctimer.node.expires = ktime_add(now, onesec);
499 rtc->uie_rtctimer.period = ktime_set(1, 0);
John Stultzaa0be0f2011-01-20 15:26:12 -0800500 err = rtc_timer_enqueue(rtc, &rtc->uie_rtctimer);
501 } else
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100502 rtc_timer_remove(rtc, &rtc->uie_rtctimer);
Alessandro Zummo099e6572009-01-04 12:00:54 -0800503
John Stultz6610e082010-09-23 15:07:34 -0700504out:
Alessandro Zummo099e6572009-01-04 12:00:54 -0800505 mutex_unlock(&rtc->ops_lock);
John Stultz456d66e2011-02-11 18:15:23 -0800506#ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
507 /*
508 * Enable emulation if the driver did not provide
509 * the update_irq_enable function pointer or if returned
510 * -EINVAL to signal that it has been configured without
511 * interrupts or that are not available at the moment.
512 */
513 if (err == -EINVAL)
514 err = rtc_dev_update_irq_enable_emul(rtc, enabled);
515#endif
Alessandro Zummo099e6572009-01-04 12:00:54 -0800516 return err;
John Stultz6610e082010-09-23 15:07:34 -0700517
Alessandro Zummo099e6572009-01-04 12:00:54 -0800518}
519EXPORT_SYMBOL_GPL(rtc_update_irq_enable);
520
John Stultz6610e082010-09-23 15:07:34 -0700521
David Brownelld728b1e2006-11-25 11:09:28 -0800522/**
John Stultz6610e082010-09-23 15:07:34 -0700523 * rtc_handle_legacy_irq - AIE, UIE and PIE event hook
524 * @rtc: pointer to the rtc device
525 *
526 * This function is called when an AIE, UIE or PIE mode interrupt
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300527 * has occurred (or been emulated).
John Stultz6610e082010-09-23 15:07:34 -0700528 *
529 * Triggers the registered irq_task function callback.
530 */
John Stultz456d66e2011-02-11 18:15:23 -0800531void rtc_handle_legacy_irq(struct rtc_device *rtc, int num, int mode)
John Stultz6610e082010-09-23 15:07:34 -0700532{
533 unsigned long flags;
534
535 /* mark one irq of the appropriate mode */
536 spin_lock_irqsave(&rtc->irq_lock, flags);
537 rtc->irq_data = (rtc->irq_data + (num << 8)) | (RTC_IRQF|mode);
538 spin_unlock_irqrestore(&rtc->irq_lock, flags);
539
540 /* call the task func */
541 spin_lock_irqsave(&rtc->irq_task_lock, flags);
542 if (rtc->irq_task)
543 rtc->irq_task->func(rtc->irq_task->private_data);
544 spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
545
546 wake_up_interruptible(&rtc->irq_queue);
547 kill_fasync(&rtc->async_queue, SIGIO, POLL_IN);
548}
549
550
551/**
552 * rtc_aie_update_irq - AIE mode rtctimer hook
553 * @private: pointer to the rtc_device
554 *
555 * This functions is called when the aie_timer expires.
556 */
557void rtc_aie_update_irq(void *private)
558{
559 struct rtc_device *rtc = (struct rtc_device *)private;
560 rtc_handle_legacy_irq(rtc, 1, RTC_AF);
561}
562
563
564/**
565 * rtc_uie_update_irq - UIE mode rtctimer hook
566 * @private: pointer to the rtc_device
567 *
568 * This functions is called when the uie_timer expires.
569 */
570void rtc_uie_update_irq(void *private)
571{
572 struct rtc_device *rtc = (struct rtc_device *)private;
573 rtc_handle_legacy_irq(rtc, 1, RTC_UF);
574}
575
576
577/**
578 * rtc_pie_update_irq - PIE mode hrtimer hook
579 * @timer: pointer to the pie mode hrtimer
580 *
581 * This function is used to emulate PIE mode interrupts
582 * using an hrtimer. This function is called when the periodic
583 * hrtimer expires.
584 */
585enum hrtimer_restart rtc_pie_update_irq(struct hrtimer *timer)
586{
587 struct rtc_device *rtc;
588 ktime_t period;
589 int count;
590 rtc = container_of(timer, struct rtc_device, pie_timer);
591
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100592 period = NSEC_PER_SEC / rtc->irq_freq;
John Stultz6610e082010-09-23 15:07:34 -0700593 count = hrtimer_forward_now(timer, period);
594
595 rtc_handle_legacy_irq(rtc, count, RTC_PF);
596
597 return HRTIMER_RESTART;
598}
599
600/**
601 * rtc_update_irq - Triggered when a RTC interrupt occurs.
David Brownellab6a2d72007-05-08 00:33:30 -0700602 * @rtc: the rtc device
David Brownelld728b1e2006-11-25 11:09:28 -0800603 * @num: how many irqs are being reported (usually one)
604 * @events: mask of RTC_IRQF with one or more of RTC_PF, RTC_AF, RTC_UF
Atsushi Nemotoe6229bec2009-06-18 16:49:09 -0700605 * Context: any
David Brownelld728b1e2006-11-25 11:09:28 -0800606 */
David Brownellab6a2d72007-05-08 00:33:30 -0700607void rtc_update_irq(struct rtc_device *rtc,
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800608 unsigned long num, unsigned long events)
609{
viresh kumare7cba882015-07-31 16:23:43 +0530610 if (IS_ERR_OR_NULL(rtc))
Alessandro Zummo131c9cc2014-04-03 14:50:09 -0700611 return;
612
NeilBrown7523cee2012-08-05 22:56:20 +0200613 pm_stay_awake(rtc->dev.parent);
John Stultz6610e082010-09-23 15:07:34 -0700614 schedule_work(&rtc->irqwork);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800615}
616EXPORT_SYMBOL_GPL(rtc_update_irq);
617
Michał Mirosław9f3b7952013-02-01 20:40:17 +0100618static int __rtc_match(struct device *dev, const void *data)
Dave Young71da8902008-01-22 14:00:34 +0800619{
Michał Mirosław9f3b7952013-02-01 20:40:17 +0100620 const char *name = data;
Dave Young71da8902008-01-22 14:00:34 +0800621
Kay Sieversd4afc762009-01-06 14:42:11 -0800622 if (strcmp(dev_name(dev), name) == 0)
Dave Young71da8902008-01-22 14:00:34 +0800623 return 1;
624 return 0;
625}
626
Michał Mirosław9f3b7952013-02-01 20:40:17 +0100627struct rtc_device *rtc_class_open(const char *name)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800628{
David Brownellcd966202007-05-08 00:33:40 -0700629 struct device *dev;
David Brownellab6a2d72007-05-08 00:33:30 -0700630 struct rtc_device *rtc = NULL;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800631
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -0400632 dev = class_find_device(rtc_class, NULL, name, __rtc_match);
Dave Young71da8902008-01-22 14:00:34 +0800633 if (dev)
634 rtc = to_rtc_device(dev);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800635
David Brownellab6a2d72007-05-08 00:33:30 -0700636 if (rtc) {
637 if (!try_module_get(rtc->owner)) {
David Brownellcd966202007-05-08 00:33:40 -0700638 put_device(dev);
David Brownellab6a2d72007-05-08 00:33:30 -0700639 rtc = NULL;
640 }
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800641 }
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800642
David Brownellab6a2d72007-05-08 00:33:30 -0700643 return rtc;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800644}
645EXPORT_SYMBOL_GPL(rtc_class_open);
646
David Brownellab6a2d72007-05-08 00:33:30 -0700647void rtc_class_close(struct rtc_device *rtc)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800648{
David Brownellab6a2d72007-05-08 00:33:30 -0700649 module_put(rtc->owner);
David Brownellcd966202007-05-08 00:33:40 -0700650 put_device(&rtc->dev);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800651}
652EXPORT_SYMBOL_GPL(rtc_class_close);
653
David Brownellab6a2d72007-05-08 00:33:30 -0700654int rtc_irq_register(struct rtc_device *rtc, struct rtc_task *task)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800655{
656 int retval = -EBUSY;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800657
658 if (task == NULL || task->func == NULL)
659 return -EINVAL;
660
Alessandro Zummod691eb92007-10-16 01:28:15 -0700661 /* Cannot register while the char dev is in use */
Jiri Kosina372a3022007-12-04 23:45:05 -0800662 if (test_and_set_bit_lock(RTC_DEV_BUSY, &rtc->flags))
Alessandro Zummod691eb92007-10-16 01:28:15 -0700663 return -EBUSY;
664
David Brownelld728b1e2006-11-25 11:09:28 -0800665 spin_lock_irq(&rtc->irq_task_lock);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800666 if (rtc->irq_task == NULL) {
667 rtc->irq_task = task;
668 retval = 0;
669 }
David Brownelld728b1e2006-11-25 11:09:28 -0800670 spin_unlock_irq(&rtc->irq_task_lock);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800671
Jiri Kosina372a3022007-12-04 23:45:05 -0800672 clear_bit_unlock(RTC_DEV_BUSY, &rtc->flags);
Alessandro Zummod691eb92007-10-16 01:28:15 -0700673
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800674 return retval;
675}
676EXPORT_SYMBOL_GPL(rtc_irq_register);
677
David Brownellab6a2d72007-05-08 00:33:30 -0700678void rtc_irq_unregister(struct rtc_device *rtc, struct rtc_task *task)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800679{
David Brownelld728b1e2006-11-25 11:09:28 -0800680 spin_lock_irq(&rtc->irq_task_lock);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800681 if (rtc->irq_task == task)
682 rtc->irq_task = NULL;
David Brownelld728b1e2006-11-25 11:09:28 -0800683 spin_unlock_irq(&rtc->irq_task_lock);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800684}
685EXPORT_SYMBOL_GPL(rtc_irq_unregister);
686
Thomas Gleixner3c8bb90e2011-07-22 09:12:51 +0000687static int rtc_update_hrtimer(struct rtc_device *rtc, int enabled)
688{
689 /*
690 * We always cancel the timer here first, because otherwise
691 * we could run into BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
692 * when we manage to start the timer before the callback
693 * returns HRTIMER_RESTART.
694 *
695 * We cannot use hrtimer_cancel() here as a running callback
696 * could be blocked on rtc->irq_task_lock and hrtimer_cancel()
697 * would spin forever.
698 */
699 if (hrtimer_try_to_cancel(&rtc->pie_timer) < 0)
700 return -1;
701
702 if (enabled) {
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100703 ktime_t period = NSEC_PER_SEC / rtc->irq_freq;
Thomas Gleixner3c8bb90e2011-07-22 09:12:51 +0000704
705 hrtimer_start(&rtc->pie_timer, period, HRTIMER_MODE_REL);
706 }
707 return 0;
708}
709
David Brownell97144c62007-10-16 01:28:16 -0700710/**
711 * rtc_irq_set_state - enable/disable 2^N Hz periodic IRQs
712 * @rtc: the rtc device
713 * @task: currently registered with rtc_irq_register()
714 * @enabled: true to enable periodic IRQs
715 * Context: any
716 *
717 * Note that rtc_irq_set_freq() should previously have been used to
718 * specify the desired frequency of periodic IRQ task->func() callbacks.
719 */
David Brownellab6a2d72007-05-08 00:33:30 -0700720int rtc_irq_set_state(struct rtc_device *rtc, struct rtc_task *task, int enabled)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800721{
722 int err = 0;
723 unsigned long flags;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800724
Thomas Gleixner3c8bb90e2011-07-22 09:12:51 +0000725retry:
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800726 spin_lock_irqsave(&rtc->irq_task_lock, flags);
Alessandro Zummod691eb92007-10-16 01:28:15 -0700727 if (rtc->irq_task != NULL && task == NULL)
728 err = -EBUSY;
Chris Brand0734e272013-07-03 15:07:57 -0700729 else if (rtc->irq_task != task)
Alessandro Zummod691eb92007-10-16 01:28:15 -0700730 err = -EACCES;
Chris Brand0734e272013-07-03 15:07:57 -0700731 else {
Thomas Gleixner3c8bb90e2011-07-22 09:12:51 +0000732 if (rtc_update_hrtimer(rtc, enabled) < 0) {
733 spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
734 cpu_relax();
735 goto retry;
736 }
737 rtc->pie_enabled = enabled;
John Stultz6610e082010-09-23 15:07:34 -0700738 }
John Stultz6610e082010-09-23 15:07:34 -0700739 spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
Baolin Wang29a1f592017-12-14 13:31:43 +0800740
741 trace_rtc_irq_set_state(enabled, err);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800742 return err;
743}
744EXPORT_SYMBOL_GPL(rtc_irq_set_state);
745
David Brownell97144c62007-10-16 01:28:16 -0700746/**
747 * rtc_irq_set_freq - set 2^N Hz periodic IRQ frequency for IRQ
748 * @rtc: the rtc device
749 * @task: currently registered with rtc_irq_register()
750 * @freq: positive frequency with which task->func() will be called
751 * Context: any
752 *
753 * Note that rtc_irq_set_state() is used to enable or disable the
754 * periodic IRQs.
755 */
David Brownellab6a2d72007-05-08 00:33:30 -0700756int rtc_irq_set_freq(struct rtc_device *rtc, struct rtc_task *task, int freq)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800757{
Alessandro Zummo56f10c62006-06-25 05:48:20 -0700758 int err = 0;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800759 unsigned long flags;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800760
Thomas Gleixner6e7a3332011-07-22 09:12:51 +0000761 if (freq <= 0 || freq > RTC_MAX_FREQ)
Marcelo Roberto Jimenez83a06bf2011-02-02 16:04:02 -0200762 return -EINVAL;
Thomas Gleixner3c8bb90e2011-07-22 09:12:51 +0000763retry:
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800764 spin_lock_irqsave(&rtc->irq_task_lock, flags);
Alessandro Zummod691eb92007-10-16 01:28:15 -0700765 if (rtc->irq_task != NULL && task == NULL)
766 err = -EBUSY;
Chris Brand0734e272013-07-03 15:07:57 -0700767 else if (rtc->irq_task != task)
Alessandro Zummod691eb92007-10-16 01:28:15 -0700768 err = -EACCES;
Chris Brand0734e272013-07-03 15:07:57 -0700769 else {
John Stultz6610e082010-09-23 15:07:34 -0700770 rtc->irq_freq = freq;
Thomas Gleixner3c8bb90e2011-07-22 09:12:51 +0000771 if (rtc->pie_enabled && rtc_update_hrtimer(rtc, 1) < 0) {
772 spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
773 cpu_relax();
774 goto retry;
John Stultz6610e082010-09-23 15:07:34 -0700775 }
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800776 }
John Stultz6610e082010-09-23 15:07:34 -0700777 spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
Baolin Wang29a1f592017-12-14 13:31:43 +0800778
779 trace_rtc_irq_set_freq(freq, err);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800780 return err;
781}
David Brownell2601a462006-11-25 11:09:27 -0800782EXPORT_SYMBOL_GPL(rtc_irq_set_freq);
John Stultz6610e082010-09-23 15:07:34 -0700783
784/**
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100785 * rtc_timer_enqueue - Adds a rtc_timer to the rtc_device timerqueue
John Stultz6610e082010-09-23 15:07:34 -0700786 * @rtc rtc device
787 * @timer timer being added.
788 *
789 * Enqueues a timer onto the rtc devices timerqueue and sets
790 * the next alarm event appropriately.
791 *
John Stultzaa0be0f2011-01-20 15:26:12 -0800792 * Sets the enabled bit on the added timer.
793 *
John Stultz6610e082010-09-23 15:07:34 -0700794 * Must hold ops_lock for proper serialization of timerqueue
795 */
John Stultzaa0be0f2011-01-20 15:26:12 -0800796static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer)
John Stultz6610e082010-09-23 15:07:34 -0700797{
Colin Ian King2b2f5ff2016-05-16 17:22:54 +0100798 struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue);
799 struct rtc_time tm;
800 ktime_t now;
801
John Stultzaa0be0f2011-01-20 15:26:12 -0800802 timer->enabled = 1;
Colin Ian King2b2f5ff2016-05-16 17:22:54 +0100803 __rtc_read_time(rtc, &tm);
804 now = rtc_tm_to_ktime(tm);
805
806 /* Skip over expired timers */
807 while (next) {
Thomas Gleixner2456e852016-12-25 11:38:40 +0100808 if (next->expires >= now)
Colin Ian King2b2f5ff2016-05-16 17:22:54 +0100809 break;
810 next = timerqueue_iterate_next(next);
811 }
812
John Stultz6610e082010-09-23 15:07:34 -0700813 timerqueue_add(&rtc->timerqueue, &timer->node);
Baolin Wang29a1f592017-12-14 13:31:43 +0800814 trace_rtc_timer_enqueue(timer);
Alexandre Belloni74717b22017-09-28 13:53:27 +0200815 if (!next || ktime_before(timer->node.expires, next->expires)) {
John Stultz6610e082010-09-23 15:07:34 -0700816 struct rtc_wkalrm alarm;
817 int err;
818 alarm.time = rtc_ktime_to_tm(timer->node.expires);
819 alarm.enabled = 1;
820 err = __rtc_set_alarm(rtc, &alarm);
Zoran Markovic14d0e342013-06-26 16:09:13 -0700821 if (err == -ETIME) {
822 pm_stay_awake(rtc->dev.parent);
John Stultz6610e082010-09-23 15:07:34 -0700823 schedule_work(&rtc->irqwork);
Zoran Markovic14d0e342013-06-26 16:09:13 -0700824 } else if (err) {
John Stultzaa0be0f2011-01-20 15:26:12 -0800825 timerqueue_del(&rtc->timerqueue, &timer->node);
Baolin Wang29a1f592017-12-14 13:31:43 +0800826 trace_rtc_timer_dequeue(timer);
John Stultzaa0be0f2011-01-20 15:26:12 -0800827 timer->enabled = 0;
828 return err;
829 }
John Stultz6610e082010-09-23 15:07:34 -0700830 }
John Stultzaa0be0f2011-01-20 15:26:12 -0800831 return 0;
John Stultz6610e082010-09-23 15:07:34 -0700832}
833
Rabin Vincent41c7f742011-11-22 11:03:14 +0100834static void rtc_alarm_disable(struct rtc_device *rtc)
835{
836 if (!rtc->ops || !rtc->ops->alarm_irq_enable)
837 return;
838
839 rtc->ops->alarm_irq_enable(rtc->dev.parent, false);
Baolin Wang29a1f592017-12-14 13:31:43 +0800840 trace_rtc_alarm_irq_enable(0, 0);
Rabin Vincent41c7f742011-11-22 11:03:14 +0100841}
842
John Stultz6610e082010-09-23 15:07:34 -0700843/**
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100844 * rtc_timer_remove - Removes a rtc_timer from the rtc_device timerqueue
John Stultz6610e082010-09-23 15:07:34 -0700845 * @rtc rtc device
846 * @timer timer being removed.
847 *
848 * Removes a timer onto the rtc devices timerqueue and sets
849 * the next alarm event appropriately.
850 *
John Stultzaa0be0f2011-01-20 15:26:12 -0800851 * Clears the enabled bit on the removed timer.
852 *
John Stultz6610e082010-09-23 15:07:34 -0700853 * Must hold ops_lock for proper serialization of timerqueue
854 */
John Stultzaa0be0f2011-01-20 15:26:12 -0800855static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer)
John Stultz6610e082010-09-23 15:07:34 -0700856{
857 struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue);
858 timerqueue_del(&rtc->timerqueue, &timer->node);
Baolin Wang29a1f592017-12-14 13:31:43 +0800859 trace_rtc_timer_dequeue(timer);
John Stultzaa0be0f2011-01-20 15:26:12 -0800860 timer->enabled = 0;
John Stultz6610e082010-09-23 15:07:34 -0700861 if (next == &timer->node) {
862 struct rtc_wkalrm alarm;
863 int err;
864 next = timerqueue_getnext(&rtc->timerqueue);
Rabin Vincent41c7f742011-11-22 11:03:14 +0100865 if (!next) {
866 rtc_alarm_disable(rtc);
John Stultz6610e082010-09-23 15:07:34 -0700867 return;
Rabin Vincent41c7f742011-11-22 11:03:14 +0100868 }
John Stultz6610e082010-09-23 15:07:34 -0700869 alarm.time = rtc_ktime_to_tm(next->expires);
870 alarm.enabled = 1;
871 err = __rtc_set_alarm(rtc, &alarm);
Zoran Markovic14d0e342013-06-26 16:09:13 -0700872 if (err == -ETIME) {
873 pm_stay_awake(rtc->dev.parent);
John Stultz6610e082010-09-23 15:07:34 -0700874 schedule_work(&rtc->irqwork);
Zoran Markovic14d0e342013-06-26 16:09:13 -0700875 }
John Stultz6610e082010-09-23 15:07:34 -0700876 }
877}
878
879/**
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100880 * rtc_timer_do_work - Expires rtc timers
John Stultz6610e082010-09-23 15:07:34 -0700881 * @rtc rtc device
882 * @timer timer being removed.
883 *
884 * Expires rtc timers. Reprograms next alarm event if needed.
885 * Called via worktask.
886 *
887 * Serializes access to timerqueue via ops_lock mutex
888 */
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100889void rtc_timer_do_work(struct work_struct *work)
John Stultz6610e082010-09-23 15:07:34 -0700890{
891 struct rtc_timer *timer;
892 struct timerqueue_node *next;
893 ktime_t now;
894 struct rtc_time tm;
895
896 struct rtc_device *rtc =
897 container_of(work, struct rtc_device, irqwork);
898
899 mutex_lock(&rtc->ops_lock);
900again:
901 __rtc_read_time(rtc, &tm);
902 now = rtc_tm_to_ktime(tm);
903 while ((next = timerqueue_getnext(&rtc->timerqueue))) {
Thomas Gleixner2456e852016-12-25 11:38:40 +0100904 if (next->expires > now)
John Stultz6610e082010-09-23 15:07:34 -0700905 break;
906
907 /* expire timer */
908 timer = container_of(next, struct rtc_timer, node);
909 timerqueue_del(&rtc->timerqueue, &timer->node);
Baolin Wang29a1f592017-12-14 13:31:43 +0800910 trace_rtc_timer_dequeue(timer);
John Stultz6610e082010-09-23 15:07:34 -0700911 timer->enabled = 0;
912 if (timer->task.func)
913 timer->task.func(timer->task.private_data);
914
Baolin Wang29a1f592017-12-14 13:31:43 +0800915 trace_rtc_timer_fired(timer);
John Stultz6610e082010-09-23 15:07:34 -0700916 /* Re-add/fwd periodic timers */
917 if (ktime_to_ns(timer->period)) {
918 timer->node.expires = ktime_add(timer->node.expires,
919 timer->period);
920 timer->enabled = 1;
921 timerqueue_add(&rtc->timerqueue, &timer->node);
Baolin Wang29a1f592017-12-14 13:31:43 +0800922 trace_rtc_timer_enqueue(timer);
John Stultz6610e082010-09-23 15:07:34 -0700923 }
924 }
925
926 /* Set next alarm */
927 if (next) {
928 struct rtc_wkalrm alarm;
929 int err;
Xunlei Pang6528b882014-12-10 15:54:26 -0800930 int retry = 3;
931
John Stultz6610e082010-09-23 15:07:34 -0700932 alarm.time = rtc_ktime_to_tm(next->expires);
933 alarm.enabled = 1;
Xunlei Pang6528b882014-12-10 15:54:26 -0800934reprogram:
John Stultz6610e082010-09-23 15:07:34 -0700935 err = __rtc_set_alarm(rtc, &alarm);
936 if (err == -ETIME)
937 goto again;
Xunlei Pang6528b882014-12-10 15:54:26 -0800938 else if (err) {
939 if (retry-- > 0)
940 goto reprogram;
941
942 timer = container_of(next, struct rtc_timer, node);
943 timerqueue_del(&rtc->timerqueue, &timer->node);
Baolin Wang29a1f592017-12-14 13:31:43 +0800944 trace_rtc_timer_dequeue(timer);
Xunlei Pang6528b882014-12-10 15:54:26 -0800945 timer->enabled = 0;
946 dev_err(&rtc->dev, "__rtc_set_alarm: err=%d\n", err);
947 goto again;
948 }
Rabin Vincent41c7f742011-11-22 11:03:14 +0100949 } else
950 rtc_alarm_disable(rtc);
John Stultz6610e082010-09-23 15:07:34 -0700951
Zoran Markovic14d0e342013-06-26 16:09:13 -0700952 pm_relax(rtc->dev.parent);
John Stultz6610e082010-09-23 15:07:34 -0700953 mutex_unlock(&rtc->ops_lock);
954}
955
956
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100957/* rtc_timer_init - Initializes an rtc_timer
John Stultz6610e082010-09-23 15:07:34 -0700958 * @timer: timer to be intiialized
959 * @f: function pointer to be called when timer fires
960 * @data: private data passed to function pointer
961 *
962 * Kernel interface to initializing an rtc_timer.
963 */
Sachin Kamat3ff2e132013-07-03 15:05:42 -0700964void rtc_timer_init(struct rtc_timer *timer, void (*f)(void *p), void *data)
John Stultz6610e082010-09-23 15:07:34 -0700965{
966 timerqueue_init(&timer->node);
967 timer->enabled = 0;
968 timer->task.func = f;
969 timer->task.private_data = data;
970}
971
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100972/* rtc_timer_start - Sets an rtc_timer to fire in the future
John Stultz6610e082010-09-23 15:07:34 -0700973 * @ rtc: rtc device to be used
974 * @ timer: timer being set
975 * @ expires: time at which to expire the timer
976 * @ period: period that the timer will recur
977 *
978 * Kernel interface to set an rtc_timer
979 */
Sachin Kamat3ff2e132013-07-03 15:05:42 -0700980int rtc_timer_start(struct rtc_device *rtc, struct rtc_timer *timer,
John Stultz6610e082010-09-23 15:07:34 -0700981 ktime_t expires, ktime_t period)
982{
983 int ret = 0;
984 mutex_lock(&rtc->ops_lock);
985 if (timer->enabled)
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100986 rtc_timer_remove(rtc, timer);
John Stultz6610e082010-09-23 15:07:34 -0700987
988 timer->node.expires = expires;
989 timer->period = period;
990
John Stultzaa0be0f2011-01-20 15:26:12 -0800991 ret = rtc_timer_enqueue(rtc, timer);
John Stultz6610e082010-09-23 15:07:34 -0700992
993 mutex_unlock(&rtc->ops_lock);
994 return ret;
995}
996
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100997/* rtc_timer_cancel - Stops an rtc_timer
John Stultz6610e082010-09-23 15:07:34 -0700998 * @ rtc: rtc device to be used
999 * @ timer: timer being set
1000 *
1001 * Kernel interface to cancel an rtc_timer
1002 */
Krzysztof Kozlowski73744a62015-05-03 18:57:11 +09001003void rtc_timer_cancel(struct rtc_device *rtc, struct rtc_timer *timer)
John Stultz6610e082010-09-23 15:07:34 -07001004{
John Stultz6610e082010-09-23 15:07:34 -07001005 mutex_lock(&rtc->ops_lock);
1006 if (timer->enabled)
Thomas Gleixner96c8f062010-12-13 22:45:48 +01001007 rtc_timer_remove(rtc, timer);
John Stultz6610e082010-09-23 15:07:34 -07001008 mutex_unlock(&rtc->ops_lock);
John Stultz6610e082010-09-23 15:07:34 -07001009}
1010
Joshua Claytonb3967062016-02-05 12:41:11 -08001011/**
1012 * rtc_read_offset - Read the amount of rtc offset in parts per billion
1013 * @ rtc: rtc device to be used
1014 * @ offset: the offset in parts per billion
1015 *
1016 * see below for details.
1017 *
1018 * Kernel interface to read rtc clock offset
1019 * Returns 0 on success, or a negative number on error.
1020 * If read_offset() is not implemented for the rtc, return -EINVAL
1021 */
1022int rtc_read_offset(struct rtc_device *rtc, long *offset)
1023{
1024 int ret;
John Stultz6610e082010-09-23 15:07:34 -07001025
Joshua Claytonb3967062016-02-05 12:41:11 -08001026 if (!rtc->ops)
1027 return -ENODEV;
1028
1029 if (!rtc->ops->read_offset)
1030 return -EINVAL;
1031
1032 mutex_lock(&rtc->ops_lock);
1033 ret = rtc->ops->read_offset(rtc->dev.parent, offset);
1034 mutex_unlock(&rtc->ops_lock);
Baolin Wang29a1f592017-12-14 13:31:43 +08001035
1036 trace_rtc_read_offset(*offset, ret);
Joshua Claytonb3967062016-02-05 12:41:11 -08001037 return ret;
1038}
1039
1040/**
1041 * rtc_set_offset - Adjusts the duration of the average second
1042 * @ rtc: rtc device to be used
1043 * @ offset: the offset in parts per billion
1044 *
1045 * Some rtc's allow an adjustment to the average duration of a second
1046 * to compensate for differences in the actual clock rate due to temperature,
1047 * the crystal, capacitor, etc.
1048 *
Russell King8a25c8f2017-09-29 11:23:25 +01001049 * The adjustment applied is as follows:
1050 * t = t0 * (1 + offset * 1e-9)
1051 * where t0 is the measured length of 1 RTC second with offset = 0
1052 *
Joshua Claytonb3967062016-02-05 12:41:11 -08001053 * Kernel interface to adjust an rtc clock offset.
1054 * Return 0 on success, or a negative number on error.
1055 * If the rtc offset is not setable (or not implemented), return -EINVAL
1056 */
1057int rtc_set_offset(struct rtc_device *rtc, long offset)
1058{
1059 int ret;
1060
1061 if (!rtc->ops)
1062 return -ENODEV;
1063
1064 if (!rtc->ops->set_offset)
1065 return -EINVAL;
1066
1067 mutex_lock(&rtc->ops_lock);
1068 ret = rtc->ops->set_offset(rtc->dev.parent, offset);
1069 mutex_unlock(&rtc->ops_lock);
Baolin Wang29a1f592017-12-14 13:31:43 +08001070
1071 trace_rtc_set_offset(offset, ret);
Joshua Claytonb3967062016-02-05 12:41:11 -08001072 return ret;
1073}