blob: 7e253be19ba7e8f066776e623b187012175dc3c3 [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
73 err = mutex_lock_interruptible(&rtc->ops_lock);
74 if (err)
David Brownellb68bb262008-07-29 22:33:30 -070075 return err;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080076
77 if (!rtc->ops)
78 err = -ENODEV;
Alessandro Zummobbccf832009-01-06 14:42:21 -080079 else if (rtc->ops->set_time)
David Brownellcd966202007-05-08 00:33:40 -070080 err = rtc->ops->set_time(rtc->dev.parent, tm);
Xunlei Pang8e4ff1a2015-04-01 20:34:27 -070081 else if (rtc->ops->set_mmss64) {
82 time64_t secs64 = rtc_tm_to_time64(tm);
83
84 err = rtc->ops->set_mmss64(rtc->dev.parent, secs64);
85 } else if (rtc->ops->set_mmss) {
Xunlei Pangbc10aa92015-01-22 02:31:51 +000086 time64_t secs64 = rtc_tm_to_time64(tm);
87 err = rtc->ops->set_mmss(rtc->dev.parent, secs64);
Alessandro Zummobbccf832009-01-06 14:42:21 -080088 } else
89 err = -EINVAL;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080090
Zoran Markovic14d0e342013-06-26 16:09:13 -070091 pm_stay_awake(rtc->dev.parent);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080092 mutex_unlock(&rtc->ops_lock);
NeilBrown5f9679d2011-12-09 09:39:15 +110093 /* A timer might have just expired */
94 schedule_work(&rtc->irqwork);
Baolin Wang29a1f592017-12-14 13:31:43 +080095
96 trace_rtc_set_time(rtc_tm_to_time64(tm), err);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080097 return err;
98}
99EXPORT_SYMBOL_GPL(rtc_set_time);
100
John Stultzf44f7f92011-02-21 22:58:51 -0800101static int rtc_read_alarm_internal(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
102{
103 int err;
104
105 err = mutex_lock_interruptible(&rtc->ops_lock);
106 if (err)
107 return err;
108
109 if (rtc->ops == NULL)
110 err = -ENODEV;
111 else if (!rtc->ops->read_alarm)
112 err = -EINVAL;
113 else {
Uwe Kleine-Königd68778b2016-05-11 09:11:23 +0200114 alarm->enabled = 0;
115 alarm->pending = 0;
116 alarm->time.tm_sec = -1;
117 alarm->time.tm_min = -1;
118 alarm->time.tm_hour = -1;
119 alarm->time.tm_mday = -1;
120 alarm->time.tm_mon = -1;
121 alarm->time.tm_year = -1;
122 alarm->time.tm_wday = -1;
123 alarm->time.tm_yday = -1;
124 alarm->time.tm_isdst = -1;
John Stultzf44f7f92011-02-21 22:58:51 -0800125 err = rtc->ops->read_alarm(rtc->dev.parent, alarm);
126 }
127
128 mutex_unlock(&rtc->ops_lock);
Baolin Wang29a1f592017-12-14 13:31:43 +0800129
130 trace_rtc_read_alarm(rtc_tm_to_time64(&alarm->time), err);
John Stultzf44f7f92011-02-21 22:58:51 -0800131 return err;
132}
133
134int __rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
135{
136 int err;
137 struct rtc_time before, now;
138 int first_time = 1;
Xunlei Pangbc10aa92015-01-22 02:31:51 +0000139 time64_t t_now, t_alm;
John Stultzf44f7f92011-02-21 22:58:51 -0800140 enum { none, day, month, year } missing = none;
141 unsigned days;
142
143 /* The lower level RTC driver may return -1 in some fields,
144 * creating invalid alarm->time values, for reasons like:
145 *
146 * - The hardware may not be capable of filling them in;
147 * many alarms match only on time-of-day fields, not
148 * day/month/year calendar data.
149 *
150 * - Some hardware uses illegal values as "wildcard" match
151 * values, which non-Linux firmware (like a BIOS) may try
152 * to set up as e.g. "alarm 15 minutes after each hour".
153 * Linux uses only oneshot alarms.
154 *
155 * When we see that here, we deal with it by using values from
156 * a current RTC timestamp for any missing (-1) values. The
157 * RTC driver prevents "periodic alarm" modes.
158 *
159 * But this can be racey, because some fields of the RTC timestamp
160 * may have wrapped in the interval since we read the RTC alarm,
161 * which would lead to us inserting inconsistent values in place
162 * of the -1 fields.
163 *
164 * Reading the alarm and timestamp in the reverse sequence
165 * would have the same race condition, and not solve the issue.
166 *
167 * So, we must first read the RTC timestamp,
168 * then read the RTC alarm value,
169 * and then read a second RTC timestamp.
170 *
171 * If any fields of the second timestamp have changed
172 * when compared with the first timestamp, then we know
173 * our timestamp may be inconsistent with that used by
174 * the low-level rtc_read_alarm_internal() function.
175 *
176 * So, when the two timestamps disagree, we just loop and do
177 * the process again to get a fully consistent set of values.
178 *
179 * This could all instead be done in the lower level driver,
180 * but since more than one lower level RTC implementation needs it,
181 * then it's probably best best to do it here instead of there..
182 */
183
184 /* Get the "before" timestamp */
185 err = rtc_read_time(rtc, &before);
186 if (err < 0)
187 return err;
188 do {
189 if (!first_time)
190 memcpy(&before, &now, sizeof(struct rtc_time));
191 first_time = 0;
192
193 /* get the RTC alarm values, which may be incomplete */
194 err = rtc_read_alarm_internal(rtc, alarm);
195 if (err)
196 return err;
197
198 /* full-function RTCs won't have such missing fields */
199 if (rtc_valid_tm(&alarm->time) == 0)
200 return 0;
201
202 /* get the "after" timestamp, to detect wrapped fields */
203 err = rtc_read_time(rtc, &now);
204 if (err < 0)
205 return err;
206
207 /* note that tm_sec is a "don't care" value here: */
208 } while ( before.tm_min != now.tm_min
209 || before.tm_hour != now.tm_hour
210 || before.tm_mon != now.tm_mon
211 || before.tm_year != now.tm_year);
212
213 /* Fill in the missing alarm fields using the timestamp; we
214 * know there's at least one since alarm->time is invalid.
215 */
216 if (alarm->time.tm_sec == -1)
217 alarm->time.tm_sec = now.tm_sec;
218 if (alarm->time.tm_min == -1)
219 alarm->time.tm_min = now.tm_min;
220 if (alarm->time.tm_hour == -1)
221 alarm->time.tm_hour = now.tm_hour;
222
223 /* For simplicity, only support date rollover for now */
Ben Hutchingse74a8f22012-01-10 15:11:02 -0800224 if (alarm->time.tm_mday < 1 || alarm->time.tm_mday > 31) {
John Stultzf44f7f92011-02-21 22:58:51 -0800225 alarm->time.tm_mday = now.tm_mday;
226 missing = day;
227 }
Ben Hutchingse74a8f22012-01-10 15:11:02 -0800228 if ((unsigned)alarm->time.tm_mon >= 12) {
John Stultzf44f7f92011-02-21 22:58:51 -0800229 alarm->time.tm_mon = now.tm_mon;
230 if (missing == none)
231 missing = month;
232 }
233 if (alarm->time.tm_year == -1) {
234 alarm->time.tm_year = now.tm_year;
235 if (missing == none)
236 missing = year;
237 }
238
Vaibhav Jainda96aea2017-05-19 22:18:55 +0530239 /* Can't proceed if alarm is still invalid after replacing
240 * missing fields.
241 */
242 err = rtc_valid_tm(&alarm->time);
243 if (err)
244 goto done;
245
John Stultzf44f7f92011-02-21 22:58:51 -0800246 /* with luck, no rollover is needed */
Xunlei Pangbc10aa92015-01-22 02:31:51 +0000247 t_now = rtc_tm_to_time64(&now);
248 t_alm = rtc_tm_to_time64(&alarm->time);
John Stultzf44f7f92011-02-21 22:58:51 -0800249 if (t_now < t_alm)
250 goto done;
251
252 switch (missing) {
253
254 /* 24 hour rollover ... if it's now 10am Monday, an alarm that
255 * that will trigger at 5am will do so at 5am Tuesday, which
256 * could also be in the next month or year. This is a common
257 * case, especially for PCs.
258 */
259 case day:
260 dev_dbg(&rtc->dev, "alarm rollover: %s\n", "day");
261 t_alm += 24 * 60 * 60;
Xunlei Pangbc10aa92015-01-22 02:31:51 +0000262 rtc_time64_to_tm(t_alm, &alarm->time);
John Stultzf44f7f92011-02-21 22:58:51 -0800263 break;
264
265 /* Month rollover ... if it's the 31th, an alarm on the 3rd will
266 * be next month. An alarm matching on the 30th, 29th, or 28th
267 * may end up in the month after that! Many newer PCs support
268 * this type of alarm.
269 */
270 case month:
271 dev_dbg(&rtc->dev, "alarm rollover: %s\n", "month");
272 do {
273 if (alarm->time.tm_mon < 11)
274 alarm->time.tm_mon++;
275 else {
276 alarm->time.tm_mon = 0;
277 alarm->time.tm_year++;
278 }
279 days = rtc_month_days(alarm->time.tm_mon,
280 alarm->time.tm_year);
281 } while (days < alarm->time.tm_mday);
282 break;
283
284 /* Year rollover ... easy except for leap years! */
285 case year:
286 dev_dbg(&rtc->dev, "alarm rollover: %s\n", "year");
287 do {
288 alarm->time.tm_year++;
Ales Novakee1d9012014-06-06 14:35:39 -0700289 } while (!is_leap_year(alarm->time.tm_year + 1900)
290 && rtc_valid_tm(&alarm->time) != 0);
John Stultzf44f7f92011-02-21 22:58:51 -0800291 break;
292
293 default:
294 dev_warn(&rtc->dev, "alarm rollover not handled\n");
295 }
296
Ales Novakee1d9012014-06-06 14:35:39 -0700297 err = rtc_valid_tm(&alarm->time);
298
Vaibhav Jainda96aea2017-05-19 22:18:55 +0530299done:
Ales Novakee1d9012014-06-06 14:35:39 -0700300 if (err) {
301 dev_warn(&rtc->dev, "invalid alarm value: %d-%d-%d %d:%d:%d\n",
302 alarm->time.tm_year + 1900, alarm->time.tm_mon + 1,
303 alarm->time.tm_mday, alarm->time.tm_hour, alarm->time.tm_min,
304 alarm->time.tm_sec);
305 }
306
307 return err;
John Stultzf44f7f92011-02-21 22:58:51 -0800308}
309
John Stultz6610e082010-09-23 15:07:34 -0700310int rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800311{
312 int err;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800313
314 err = mutex_lock_interruptible(&rtc->ops_lock);
315 if (err)
David Brownellb68bb262008-07-29 22:33:30 -0700316 return err;
John Stultzd5553a52011-01-20 15:26:13 -0800317 if (rtc->ops == NULL)
318 err = -ENODEV;
319 else if (!rtc->ops->read_alarm)
320 err = -EINVAL;
321 else {
322 memset(alarm, 0, sizeof(struct rtc_wkalrm));
323 alarm->enabled = rtc->aie_timer.enabled;
John Stultz6610e082010-09-23 15:07:34 -0700324 alarm->time = rtc_ktime_to_tm(rtc->aie_timer.node.expires);
John Stultzd5553a52011-01-20 15:26:13 -0800325 }
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800326 mutex_unlock(&rtc->ops_lock);
Mark Lord0e36a9a2007-10-16 01:28:21 -0700327
Baolin Wang29a1f592017-12-14 13:31:43 +0800328 trace_rtc_read_alarm(rtc_tm_to_time64(&alarm->time), err);
John Stultzd5553a52011-01-20 15:26:13 -0800329 return err;
Mark Lord0e36a9a2007-10-16 01:28:21 -0700330}
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800331EXPORT_SYMBOL_GPL(rtc_read_alarm);
332
Mark Brownd576fe42011-06-01 11:13:16 +0100333static int __rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
John Stultz6610e082010-09-23 15:07:34 -0700334{
335 struct rtc_time tm;
Xunlei Pangbc10aa92015-01-22 02:31:51 +0000336 time64_t now, scheduled;
John Stultz6610e082010-09-23 15:07:34 -0700337 int err;
338
339 err = rtc_valid_tm(&alarm->time);
340 if (err)
341 return err;
Xunlei Pangbc10aa92015-01-22 02:31:51 +0000342 scheduled = rtc_tm_to_time64(&alarm->time);
John Stultz6610e082010-09-23 15:07:34 -0700343
344 /* Make sure we're not setting alarms in the past */
345 err = __rtc_read_time(rtc, &tm);
Hyogi Gimca6dc2d2014-08-08 14:20:11 -0700346 if (err)
347 return err;
Xunlei Pangbc10aa92015-01-22 02:31:51 +0000348 now = rtc_tm_to_time64(&tm);
John Stultz6610e082010-09-23 15:07:34 -0700349 if (scheduled <= now)
350 return -ETIME;
351 /*
352 * XXX - We just checked to make sure the alarm time is not
353 * in the past, but there is still a race window where if
354 * the is alarm set for the next second and the second ticks
355 * over right here, before we set the alarm.
356 */
357
Linus Torvalds157e8bf2012-01-03 17:32:13 -0800358 if (!rtc->ops)
359 err = -ENODEV;
360 else if (!rtc->ops->set_alarm)
361 err = -EINVAL;
362 else
363 err = rtc->ops->set_alarm(rtc->dev.parent, alarm);
364
Baolin Wang29a1f592017-12-14 13:31:43 +0800365 trace_rtc_set_alarm(rtc_tm_to_time64(&alarm->time), err);
Linus Torvalds157e8bf2012-01-03 17:32:13 -0800366 return err;
John Stultz6610e082010-09-23 15:07:34 -0700367}
368
David Brownellab6a2d72007-05-08 00:33:30 -0700369int rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800370{
371 int err;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800372
David Brownellf8245c22007-05-08 00:34:07 -0700373 err = rtc_valid_tm(&alarm->time);
374 if (err != 0)
375 return err;
376
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800377 err = mutex_lock_interruptible(&rtc->ops_lock);
378 if (err)
David Brownellb68bb262008-07-29 22:33:30 -0700379 return err;
Sachin Kamat3ff2e132013-07-03 15:05:42 -0700380 if (rtc->aie_timer.enabled)
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100381 rtc_timer_remove(rtc, &rtc->aie_timer);
Sachin Kamat3ff2e132013-07-03 15:05:42 -0700382
John Stultz6610e082010-09-23 15:07:34 -0700383 rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time);
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100384 rtc->aie_timer.period = 0;
Sachin Kamat3ff2e132013-07-03 15:05:42 -0700385 if (alarm->enabled)
John Stultzaa0be0f2011-01-20 15:26:12 -0800386 err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
Sachin Kamat3ff2e132013-07-03 15:05:42 -0700387
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800388 mutex_unlock(&rtc->ops_lock);
John Stultzaa0be0f2011-01-20 15:26:12 -0800389 return err;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800390}
391EXPORT_SYMBOL_GPL(rtc_set_alarm);
392
John Stultzf6d5b332011-03-29 18:00:27 -0700393/* Called once per device from rtc_device_register */
394int rtc_initialize_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
395{
396 int err;
John Stultzbd729d72012-01-05 15:21:19 -0800397 struct rtc_time now;
John Stultzf6d5b332011-03-29 18:00:27 -0700398
399 err = rtc_valid_tm(&alarm->time);
400 if (err != 0)
401 return err;
402
John Stultzbd729d72012-01-05 15:21:19 -0800403 err = rtc_read_time(rtc, &now);
404 if (err)
405 return err;
406
John Stultzf6d5b332011-03-29 18:00:27 -0700407 err = mutex_lock_interruptible(&rtc->ops_lock);
408 if (err)
409 return err;
410
411 rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time);
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100412 rtc->aie_timer.period = 0;
John Stultzbd729d72012-01-05 15:21:19 -0800413
Uwe Kleine-König6785b3b2016-07-02 17:28:12 +0200414 /* Alarm has to be enabled & in the future for us to enqueue it */
Thomas Gleixner2456e852016-12-25 11:38:40 +0100415 if (alarm->enabled && (rtc_tm_to_ktime(now) <
416 rtc->aie_timer.node.expires)) {
John Stultzbd729d72012-01-05 15:21:19 -0800417
John Stultzf6d5b332011-03-29 18:00:27 -0700418 rtc->aie_timer.enabled = 1;
419 timerqueue_add(&rtc->timerqueue, &rtc->aie_timer.node);
Baolin Wang29a1f592017-12-14 13:31:43 +0800420 trace_rtc_timer_enqueue(&rtc->aie_timer);
John Stultzf6d5b332011-03-29 18:00:27 -0700421 }
422 mutex_unlock(&rtc->ops_lock);
423 return err;
424}
425EXPORT_SYMBOL_GPL(rtc_initialize_alarm);
426
Alessandro Zummo099e6572009-01-04 12:00:54 -0800427int rtc_alarm_irq_enable(struct rtc_device *rtc, unsigned int enabled)
428{
429 int err = mutex_lock_interruptible(&rtc->ops_lock);
430 if (err)
431 return err;
432
John Stultz6610e082010-09-23 15:07:34 -0700433 if (rtc->aie_timer.enabled != enabled) {
John Stultzaa0be0f2011-01-20 15:26:12 -0800434 if (enabled)
435 err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
436 else
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100437 rtc_timer_remove(rtc, &rtc->aie_timer);
John Stultz6610e082010-09-23 15:07:34 -0700438 }
439
John Stultzaa0be0f2011-01-20 15:26:12 -0800440 if (err)
Uwe Kleine-König516373b2011-02-14 11:33:17 +0100441 /* nothing */;
442 else if (!rtc->ops)
Alessandro Zummo099e6572009-01-04 12:00:54 -0800443 err = -ENODEV;
444 else if (!rtc->ops->alarm_irq_enable)
445 err = -EINVAL;
446 else
447 err = rtc->ops->alarm_irq_enable(rtc->dev.parent, enabled);
448
449 mutex_unlock(&rtc->ops_lock);
Baolin Wang29a1f592017-12-14 13:31:43 +0800450
451 trace_rtc_alarm_irq_enable(enabled, err);
Alessandro Zummo099e6572009-01-04 12:00:54 -0800452 return err;
453}
454EXPORT_SYMBOL_GPL(rtc_alarm_irq_enable);
455
456int rtc_update_irq_enable(struct rtc_device *rtc, unsigned int enabled)
457{
458 int err = mutex_lock_interruptible(&rtc->ops_lock);
459 if (err)
460 return err;
461
John Stultz456d66e2011-02-11 18:15:23 -0800462#ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
463 if (enabled == 0 && rtc->uie_irq_active) {
464 mutex_unlock(&rtc->ops_lock);
465 return rtc_dev_update_irq_enable_emul(rtc, 0);
466 }
467#endif
John Stultz6610e082010-09-23 15:07:34 -0700468 /* make sure we're changing state */
469 if (rtc->uie_rtctimer.enabled == enabled)
470 goto out;
471
John Stultz4a649902012-03-06 17:16:09 -0800472 if (rtc->uie_unsupported) {
473 err = -EINVAL;
474 goto out;
475 }
476
John Stultz6610e082010-09-23 15:07:34 -0700477 if (enabled) {
478 struct rtc_time tm;
479 ktime_t now, onesec;
480
481 __rtc_read_time(rtc, &tm);
482 onesec = ktime_set(1, 0);
483 now = rtc_tm_to_ktime(tm);
484 rtc->uie_rtctimer.node.expires = ktime_add(now, onesec);
485 rtc->uie_rtctimer.period = ktime_set(1, 0);
John Stultzaa0be0f2011-01-20 15:26:12 -0800486 err = rtc_timer_enqueue(rtc, &rtc->uie_rtctimer);
487 } else
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100488 rtc_timer_remove(rtc, &rtc->uie_rtctimer);
Alessandro Zummo099e6572009-01-04 12:00:54 -0800489
John Stultz6610e082010-09-23 15:07:34 -0700490out:
Alessandro Zummo099e6572009-01-04 12:00:54 -0800491 mutex_unlock(&rtc->ops_lock);
John Stultz456d66e2011-02-11 18:15:23 -0800492#ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
493 /*
494 * Enable emulation if the driver did not provide
495 * the update_irq_enable function pointer or if returned
496 * -EINVAL to signal that it has been configured without
497 * interrupts or that are not available at the moment.
498 */
499 if (err == -EINVAL)
500 err = rtc_dev_update_irq_enable_emul(rtc, enabled);
501#endif
Alessandro Zummo099e6572009-01-04 12:00:54 -0800502 return err;
John Stultz6610e082010-09-23 15:07:34 -0700503
Alessandro Zummo099e6572009-01-04 12:00:54 -0800504}
505EXPORT_SYMBOL_GPL(rtc_update_irq_enable);
506
John Stultz6610e082010-09-23 15:07:34 -0700507
David Brownelld728b1e2006-11-25 11:09:28 -0800508/**
John Stultz6610e082010-09-23 15:07:34 -0700509 * rtc_handle_legacy_irq - AIE, UIE and PIE event hook
510 * @rtc: pointer to the rtc device
511 *
512 * This function is called when an AIE, UIE or PIE mode interrupt
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300513 * has occurred (or been emulated).
John Stultz6610e082010-09-23 15:07:34 -0700514 *
515 * Triggers the registered irq_task function callback.
516 */
John Stultz456d66e2011-02-11 18:15:23 -0800517void rtc_handle_legacy_irq(struct rtc_device *rtc, int num, int mode)
John Stultz6610e082010-09-23 15:07:34 -0700518{
519 unsigned long flags;
520
521 /* mark one irq of the appropriate mode */
522 spin_lock_irqsave(&rtc->irq_lock, flags);
523 rtc->irq_data = (rtc->irq_data + (num << 8)) | (RTC_IRQF|mode);
524 spin_unlock_irqrestore(&rtc->irq_lock, flags);
525
526 /* call the task func */
527 spin_lock_irqsave(&rtc->irq_task_lock, flags);
528 if (rtc->irq_task)
529 rtc->irq_task->func(rtc->irq_task->private_data);
530 spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
531
532 wake_up_interruptible(&rtc->irq_queue);
533 kill_fasync(&rtc->async_queue, SIGIO, POLL_IN);
534}
535
536
537/**
538 * rtc_aie_update_irq - AIE mode rtctimer hook
539 * @private: pointer to the rtc_device
540 *
541 * This functions is called when the aie_timer expires.
542 */
543void rtc_aie_update_irq(void *private)
544{
545 struct rtc_device *rtc = (struct rtc_device *)private;
546 rtc_handle_legacy_irq(rtc, 1, RTC_AF);
547}
548
549
550/**
551 * rtc_uie_update_irq - UIE mode rtctimer hook
552 * @private: pointer to the rtc_device
553 *
554 * This functions is called when the uie_timer expires.
555 */
556void rtc_uie_update_irq(void *private)
557{
558 struct rtc_device *rtc = (struct rtc_device *)private;
559 rtc_handle_legacy_irq(rtc, 1, RTC_UF);
560}
561
562
563/**
564 * rtc_pie_update_irq - PIE mode hrtimer hook
565 * @timer: pointer to the pie mode hrtimer
566 *
567 * This function is used to emulate PIE mode interrupts
568 * using an hrtimer. This function is called when the periodic
569 * hrtimer expires.
570 */
571enum hrtimer_restart rtc_pie_update_irq(struct hrtimer *timer)
572{
573 struct rtc_device *rtc;
574 ktime_t period;
575 int count;
576 rtc = container_of(timer, struct rtc_device, pie_timer);
577
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100578 period = NSEC_PER_SEC / rtc->irq_freq;
John Stultz6610e082010-09-23 15:07:34 -0700579 count = hrtimer_forward_now(timer, period);
580
581 rtc_handle_legacy_irq(rtc, count, RTC_PF);
582
583 return HRTIMER_RESTART;
584}
585
586/**
587 * rtc_update_irq - Triggered when a RTC interrupt occurs.
David Brownellab6a2d72007-05-08 00:33:30 -0700588 * @rtc: the rtc device
David Brownelld728b1e2006-11-25 11:09:28 -0800589 * @num: how many irqs are being reported (usually one)
590 * @events: mask of RTC_IRQF with one or more of RTC_PF, RTC_AF, RTC_UF
Atsushi Nemotoe6229bec2009-06-18 16:49:09 -0700591 * Context: any
David Brownelld728b1e2006-11-25 11:09:28 -0800592 */
David Brownellab6a2d72007-05-08 00:33:30 -0700593void rtc_update_irq(struct rtc_device *rtc,
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800594 unsigned long num, unsigned long events)
595{
viresh kumare7cba882015-07-31 16:23:43 +0530596 if (IS_ERR_OR_NULL(rtc))
Alessandro Zummo131c9cc2014-04-03 14:50:09 -0700597 return;
598
NeilBrown7523cee2012-08-05 22:56:20 +0200599 pm_stay_awake(rtc->dev.parent);
John Stultz6610e082010-09-23 15:07:34 -0700600 schedule_work(&rtc->irqwork);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800601}
602EXPORT_SYMBOL_GPL(rtc_update_irq);
603
Michał Mirosław9f3b7952013-02-01 20:40:17 +0100604static int __rtc_match(struct device *dev, const void *data)
Dave Young71da8902008-01-22 14:00:34 +0800605{
Michał Mirosław9f3b7952013-02-01 20:40:17 +0100606 const char *name = data;
Dave Young71da8902008-01-22 14:00:34 +0800607
Kay Sieversd4afc762009-01-06 14:42:11 -0800608 if (strcmp(dev_name(dev), name) == 0)
Dave Young71da8902008-01-22 14:00:34 +0800609 return 1;
610 return 0;
611}
612
Michał Mirosław9f3b7952013-02-01 20:40:17 +0100613struct rtc_device *rtc_class_open(const char *name)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800614{
David Brownellcd966202007-05-08 00:33:40 -0700615 struct device *dev;
David Brownellab6a2d72007-05-08 00:33:30 -0700616 struct rtc_device *rtc = NULL;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800617
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -0400618 dev = class_find_device(rtc_class, NULL, name, __rtc_match);
Dave Young71da8902008-01-22 14:00:34 +0800619 if (dev)
620 rtc = to_rtc_device(dev);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800621
David Brownellab6a2d72007-05-08 00:33:30 -0700622 if (rtc) {
623 if (!try_module_get(rtc->owner)) {
David Brownellcd966202007-05-08 00:33:40 -0700624 put_device(dev);
David Brownellab6a2d72007-05-08 00:33:30 -0700625 rtc = NULL;
626 }
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800627 }
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800628
David Brownellab6a2d72007-05-08 00:33:30 -0700629 return rtc;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800630}
631EXPORT_SYMBOL_GPL(rtc_class_open);
632
David Brownellab6a2d72007-05-08 00:33:30 -0700633void rtc_class_close(struct rtc_device *rtc)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800634{
David Brownellab6a2d72007-05-08 00:33:30 -0700635 module_put(rtc->owner);
David Brownellcd966202007-05-08 00:33:40 -0700636 put_device(&rtc->dev);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800637}
638EXPORT_SYMBOL_GPL(rtc_class_close);
639
David Brownellab6a2d72007-05-08 00:33:30 -0700640int rtc_irq_register(struct rtc_device *rtc, struct rtc_task *task)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800641{
642 int retval = -EBUSY;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800643
644 if (task == NULL || task->func == NULL)
645 return -EINVAL;
646
Alessandro Zummod691eb92007-10-16 01:28:15 -0700647 /* Cannot register while the char dev is in use */
Jiri Kosina372a3022007-12-04 23:45:05 -0800648 if (test_and_set_bit_lock(RTC_DEV_BUSY, &rtc->flags))
Alessandro Zummod691eb92007-10-16 01:28:15 -0700649 return -EBUSY;
650
David Brownelld728b1e2006-11-25 11:09:28 -0800651 spin_lock_irq(&rtc->irq_task_lock);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800652 if (rtc->irq_task == NULL) {
653 rtc->irq_task = task;
654 retval = 0;
655 }
David Brownelld728b1e2006-11-25 11:09:28 -0800656 spin_unlock_irq(&rtc->irq_task_lock);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800657
Jiri Kosina372a3022007-12-04 23:45:05 -0800658 clear_bit_unlock(RTC_DEV_BUSY, &rtc->flags);
Alessandro Zummod691eb92007-10-16 01:28:15 -0700659
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800660 return retval;
661}
662EXPORT_SYMBOL_GPL(rtc_irq_register);
663
David Brownellab6a2d72007-05-08 00:33:30 -0700664void rtc_irq_unregister(struct rtc_device *rtc, struct rtc_task *task)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800665{
David Brownelld728b1e2006-11-25 11:09:28 -0800666 spin_lock_irq(&rtc->irq_task_lock);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800667 if (rtc->irq_task == task)
668 rtc->irq_task = NULL;
David Brownelld728b1e2006-11-25 11:09:28 -0800669 spin_unlock_irq(&rtc->irq_task_lock);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800670}
671EXPORT_SYMBOL_GPL(rtc_irq_unregister);
672
Thomas Gleixner3c8bb902011-07-22 09:12:51 +0000673static int rtc_update_hrtimer(struct rtc_device *rtc, int enabled)
674{
675 /*
676 * We always cancel the timer here first, because otherwise
677 * we could run into BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
678 * when we manage to start the timer before the callback
679 * returns HRTIMER_RESTART.
680 *
681 * We cannot use hrtimer_cancel() here as a running callback
682 * could be blocked on rtc->irq_task_lock and hrtimer_cancel()
683 * would spin forever.
684 */
685 if (hrtimer_try_to_cancel(&rtc->pie_timer) < 0)
686 return -1;
687
688 if (enabled) {
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100689 ktime_t period = NSEC_PER_SEC / rtc->irq_freq;
Thomas Gleixner3c8bb902011-07-22 09:12:51 +0000690
691 hrtimer_start(&rtc->pie_timer, period, HRTIMER_MODE_REL);
692 }
693 return 0;
694}
695
David Brownell97144c62007-10-16 01:28:16 -0700696/**
697 * rtc_irq_set_state - enable/disable 2^N Hz periodic IRQs
698 * @rtc: the rtc device
699 * @task: currently registered with rtc_irq_register()
700 * @enabled: true to enable periodic IRQs
701 * Context: any
702 *
703 * Note that rtc_irq_set_freq() should previously have been used to
704 * specify the desired frequency of periodic IRQ task->func() callbacks.
705 */
David Brownellab6a2d72007-05-08 00:33:30 -0700706int rtc_irq_set_state(struct rtc_device *rtc, struct rtc_task *task, int enabled)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800707{
708 int err = 0;
709 unsigned long flags;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800710
Thomas Gleixner3c8bb902011-07-22 09:12:51 +0000711retry:
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800712 spin_lock_irqsave(&rtc->irq_task_lock, flags);
Alessandro Zummod691eb92007-10-16 01:28:15 -0700713 if (rtc->irq_task != NULL && task == NULL)
714 err = -EBUSY;
Chris Brand0734e272013-07-03 15:07:57 -0700715 else if (rtc->irq_task != task)
Alessandro Zummod691eb92007-10-16 01:28:15 -0700716 err = -EACCES;
Chris Brand0734e272013-07-03 15:07:57 -0700717 else {
Thomas Gleixner3c8bb902011-07-22 09:12:51 +0000718 if (rtc_update_hrtimer(rtc, enabled) < 0) {
719 spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
720 cpu_relax();
721 goto retry;
722 }
723 rtc->pie_enabled = enabled;
John Stultz6610e082010-09-23 15:07:34 -0700724 }
John Stultz6610e082010-09-23 15:07:34 -0700725 spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
Baolin Wang29a1f592017-12-14 13:31:43 +0800726
727 trace_rtc_irq_set_state(enabled, err);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800728 return err;
729}
730EXPORT_SYMBOL_GPL(rtc_irq_set_state);
731
David Brownell97144c62007-10-16 01:28:16 -0700732/**
733 * rtc_irq_set_freq - set 2^N Hz periodic IRQ frequency for IRQ
734 * @rtc: the rtc device
735 * @task: currently registered with rtc_irq_register()
736 * @freq: positive frequency with which task->func() will be called
737 * Context: any
738 *
739 * Note that rtc_irq_set_state() is used to enable or disable the
740 * periodic IRQs.
741 */
David Brownellab6a2d72007-05-08 00:33:30 -0700742int rtc_irq_set_freq(struct rtc_device *rtc, struct rtc_task *task, int freq)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800743{
Alessandro Zummo56f10c62006-06-25 05:48:20 -0700744 int err = 0;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800745 unsigned long flags;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800746
Thomas Gleixner6e7a3332011-07-22 09:12:51 +0000747 if (freq <= 0 || freq > RTC_MAX_FREQ)
Marcelo Roberto Jimenez83a06bf2011-02-02 16:04:02 -0200748 return -EINVAL;
Thomas Gleixner3c8bb902011-07-22 09:12:51 +0000749retry:
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800750 spin_lock_irqsave(&rtc->irq_task_lock, flags);
Alessandro Zummod691eb92007-10-16 01:28:15 -0700751 if (rtc->irq_task != NULL && task == NULL)
752 err = -EBUSY;
Chris Brand0734e272013-07-03 15:07:57 -0700753 else if (rtc->irq_task != task)
Alessandro Zummod691eb92007-10-16 01:28:15 -0700754 err = -EACCES;
Chris Brand0734e272013-07-03 15:07:57 -0700755 else {
John Stultz6610e082010-09-23 15:07:34 -0700756 rtc->irq_freq = freq;
Thomas Gleixner3c8bb902011-07-22 09:12:51 +0000757 if (rtc->pie_enabled && rtc_update_hrtimer(rtc, 1) < 0) {
758 spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
759 cpu_relax();
760 goto retry;
John Stultz6610e082010-09-23 15:07:34 -0700761 }
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800762 }
John Stultz6610e082010-09-23 15:07:34 -0700763 spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
Baolin Wang29a1f592017-12-14 13:31:43 +0800764
765 trace_rtc_irq_set_freq(freq, err);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800766 return err;
767}
David Brownell2601a462006-11-25 11:09:27 -0800768EXPORT_SYMBOL_GPL(rtc_irq_set_freq);
John Stultz6610e082010-09-23 15:07:34 -0700769
770/**
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100771 * rtc_timer_enqueue - Adds a rtc_timer to the rtc_device timerqueue
John Stultz6610e082010-09-23 15:07:34 -0700772 * @rtc rtc device
773 * @timer timer being added.
774 *
775 * Enqueues a timer onto the rtc devices timerqueue and sets
776 * the next alarm event appropriately.
777 *
John Stultzaa0be0f2011-01-20 15:26:12 -0800778 * Sets the enabled bit on the added timer.
779 *
John Stultz6610e082010-09-23 15:07:34 -0700780 * Must hold ops_lock for proper serialization of timerqueue
781 */
John Stultzaa0be0f2011-01-20 15:26:12 -0800782static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer)
John Stultz6610e082010-09-23 15:07:34 -0700783{
Colin Ian King2b2f5ff2016-05-16 17:22:54 +0100784 struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue);
785 struct rtc_time tm;
786 ktime_t now;
787
John Stultzaa0be0f2011-01-20 15:26:12 -0800788 timer->enabled = 1;
Colin Ian King2b2f5ff2016-05-16 17:22:54 +0100789 __rtc_read_time(rtc, &tm);
790 now = rtc_tm_to_ktime(tm);
791
792 /* Skip over expired timers */
793 while (next) {
Thomas Gleixner2456e852016-12-25 11:38:40 +0100794 if (next->expires >= now)
Colin Ian King2b2f5ff2016-05-16 17:22:54 +0100795 break;
796 next = timerqueue_iterate_next(next);
797 }
798
John Stultz6610e082010-09-23 15:07:34 -0700799 timerqueue_add(&rtc->timerqueue, &timer->node);
Baolin Wang29a1f592017-12-14 13:31:43 +0800800 trace_rtc_timer_enqueue(timer);
Alexandre Belloni74717b22017-09-28 13:53:27 +0200801 if (!next || ktime_before(timer->node.expires, next->expires)) {
John Stultz6610e082010-09-23 15:07:34 -0700802 struct rtc_wkalrm alarm;
803 int err;
804 alarm.time = rtc_ktime_to_tm(timer->node.expires);
805 alarm.enabled = 1;
806 err = __rtc_set_alarm(rtc, &alarm);
Zoran Markovic14d0e342013-06-26 16:09:13 -0700807 if (err == -ETIME) {
808 pm_stay_awake(rtc->dev.parent);
John Stultz6610e082010-09-23 15:07:34 -0700809 schedule_work(&rtc->irqwork);
Zoran Markovic14d0e342013-06-26 16:09:13 -0700810 } else if (err) {
John Stultzaa0be0f2011-01-20 15:26:12 -0800811 timerqueue_del(&rtc->timerqueue, &timer->node);
Baolin Wang29a1f592017-12-14 13:31:43 +0800812 trace_rtc_timer_dequeue(timer);
John Stultzaa0be0f2011-01-20 15:26:12 -0800813 timer->enabled = 0;
814 return err;
815 }
John Stultz6610e082010-09-23 15:07:34 -0700816 }
John Stultzaa0be0f2011-01-20 15:26:12 -0800817 return 0;
John Stultz6610e082010-09-23 15:07:34 -0700818}
819
Rabin Vincent41c7f742011-11-22 11:03:14 +0100820static void rtc_alarm_disable(struct rtc_device *rtc)
821{
822 if (!rtc->ops || !rtc->ops->alarm_irq_enable)
823 return;
824
825 rtc->ops->alarm_irq_enable(rtc->dev.parent, false);
Baolin Wang29a1f592017-12-14 13:31:43 +0800826 trace_rtc_alarm_irq_enable(0, 0);
Rabin Vincent41c7f742011-11-22 11:03:14 +0100827}
828
John Stultz6610e082010-09-23 15:07:34 -0700829/**
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100830 * rtc_timer_remove - Removes a rtc_timer from the rtc_device timerqueue
John Stultz6610e082010-09-23 15:07:34 -0700831 * @rtc rtc device
832 * @timer timer being removed.
833 *
834 * Removes a timer onto the rtc devices timerqueue and sets
835 * the next alarm event appropriately.
836 *
John Stultzaa0be0f2011-01-20 15:26:12 -0800837 * Clears the enabled bit on the removed timer.
838 *
John Stultz6610e082010-09-23 15:07:34 -0700839 * Must hold ops_lock for proper serialization of timerqueue
840 */
John Stultzaa0be0f2011-01-20 15:26:12 -0800841static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer)
John Stultz6610e082010-09-23 15:07:34 -0700842{
843 struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue);
844 timerqueue_del(&rtc->timerqueue, &timer->node);
Baolin Wang29a1f592017-12-14 13:31:43 +0800845 trace_rtc_timer_dequeue(timer);
John Stultzaa0be0f2011-01-20 15:26:12 -0800846 timer->enabled = 0;
John Stultz6610e082010-09-23 15:07:34 -0700847 if (next == &timer->node) {
848 struct rtc_wkalrm alarm;
849 int err;
850 next = timerqueue_getnext(&rtc->timerqueue);
Rabin Vincent41c7f742011-11-22 11:03:14 +0100851 if (!next) {
852 rtc_alarm_disable(rtc);
John Stultz6610e082010-09-23 15:07:34 -0700853 return;
Rabin Vincent41c7f742011-11-22 11:03:14 +0100854 }
John Stultz6610e082010-09-23 15:07:34 -0700855 alarm.time = rtc_ktime_to_tm(next->expires);
856 alarm.enabled = 1;
857 err = __rtc_set_alarm(rtc, &alarm);
Zoran Markovic14d0e342013-06-26 16:09:13 -0700858 if (err == -ETIME) {
859 pm_stay_awake(rtc->dev.parent);
John Stultz6610e082010-09-23 15:07:34 -0700860 schedule_work(&rtc->irqwork);
Zoran Markovic14d0e342013-06-26 16:09:13 -0700861 }
John Stultz6610e082010-09-23 15:07:34 -0700862 }
863}
864
865/**
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100866 * rtc_timer_do_work - Expires rtc timers
John Stultz6610e082010-09-23 15:07:34 -0700867 * @rtc rtc device
868 * @timer timer being removed.
869 *
870 * Expires rtc timers. Reprograms next alarm event if needed.
871 * Called via worktask.
872 *
873 * Serializes access to timerqueue via ops_lock mutex
874 */
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100875void rtc_timer_do_work(struct work_struct *work)
John Stultz6610e082010-09-23 15:07:34 -0700876{
877 struct rtc_timer *timer;
878 struct timerqueue_node *next;
879 ktime_t now;
880 struct rtc_time tm;
881
882 struct rtc_device *rtc =
883 container_of(work, struct rtc_device, irqwork);
884
885 mutex_lock(&rtc->ops_lock);
886again:
887 __rtc_read_time(rtc, &tm);
888 now = rtc_tm_to_ktime(tm);
889 while ((next = timerqueue_getnext(&rtc->timerqueue))) {
Thomas Gleixner2456e852016-12-25 11:38:40 +0100890 if (next->expires > now)
John Stultz6610e082010-09-23 15:07:34 -0700891 break;
892
893 /* expire timer */
894 timer = container_of(next, struct rtc_timer, node);
895 timerqueue_del(&rtc->timerqueue, &timer->node);
Baolin Wang29a1f592017-12-14 13:31:43 +0800896 trace_rtc_timer_dequeue(timer);
John Stultz6610e082010-09-23 15:07:34 -0700897 timer->enabled = 0;
898 if (timer->task.func)
899 timer->task.func(timer->task.private_data);
900
Baolin Wang29a1f592017-12-14 13:31:43 +0800901 trace_rtc_timer_fired(timer);
John Stultz6610e082010-09-23 15:07:34 -0700902 /* Re-add/fwd periodic timers */
903 if (ktime_to_ns(timer->period)) {
904 timer->node.expires = ktime_add(timer->node.expires,
905 timer->period);
906 timer->enabled = 1;
907 timerqueue_add(&rtc->timerqueue, &timer->node);
Baolin Wang29a1f592017-12-14 13:31:43 +0800908 trace_rtc_timer_enqueue(timer);
John Stultz6610e082010-09-23 15:07:34 -0700909 }
910 }
911
912 /* Set next alarm */
913 if (next) {
914 struct rtc_wkalrm alarm;
915 int err;
Xunlei Pang6528b882014-12-10 15:54:26 -0800916 int retry = 3;
917
John Stultz6610e082010-09-23 15:07:34 -0700918 alarm.time = rtc_ktime_to_tm(next->expires);
919 alarm.enabled = 1;
Xunlei Pang6528b882014-12-10 15:54:26 -0800920reprogram:
John Stultz6610e082010-09-23 15:07:34 -0700921 err = __rtc_set_alarm(rtc, &alarm);
922 if (err == -ETIME)
923 goto again;
Xunlei Pang6528b882014-12-10 15:54:26 -0800924 else if (err) {
925 if (retry-- > 0)
926 goto reprogram;
927
928 timer = container_of(next, struct rtc_timer, node);
929 timerqueue_del(&rtc->timerqueue, &timer->node);
Baolin Wang29a1f592017-12-14 13:31:43 +0800930 trace_rtc_timer_dequeue(timer);
Xunlei Pang6528b882014-12-10 15:54:26 -0800931 timer->enabled = 0;
932 dev_err(&rtc->dev, "__rtc_set_alarm: err=%d\n", err);
933 goto again;
934 }
Rabin Vincent41c7f742011-11-22 11:03:14 +0100935 } else
936 rtc_alarm_disable(rtc);
John Stultz6610e082010-09-23 15:07:34 -0700937
Zoran Markovic14d0e342013-06-26 16:09:13 -0700938 pm_relax(rtc->dev.parent);
John Stultz6610e082010-09-23 15:07:34 -0700939 mutex_unlock(&rtc->ops_lock);
940}
941
942
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100943/* rtc_timer_init - Initializes an rtc_timer
John Stultz6610e082010-09-23 15:07:34 -0700944 * @timer: timer to be intiialized
945 * @f: function pointer to be called when timer fires
946 * @data: private data passed to function pointer
947 *
948 * Kernel interface to initializing an rtc_timer.
949 */
Sachin Kamat3ff2e132013-07-03 15:05:42 -0700950void rtc_timer_init(struct rtc_timer *timer, void (*f)(void *p), void *data)
John Stultz6610e082010-09-23 15:07:34 -0700951{
952 timerqueue_init(&timer->node);
953 timer->enabled = 0;
954 timer->task.func = f;
955 timer->task.private_data = data;
956}
957
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100958/* rtc_timer_start - Sets an rtc_timer to fire in the future
John Stultz6610e082010-09-23 15:07:34 -0700959 * @ rtc: rtc device to be used
960 * @ timer: timer being set
961 * @ expires: time at which to expire the timer
962 * @ period: period that the timer will recur
963 *
964 * Kernel interface to set an rtc_timer
965 */
Sachin Kamat3ff2e132013-07-03 15:05:42 -0700966int rtc_timer_start(struct rtc_device *rtc, struct rtc_timer *timer,
John Stultz6610e082010-09-23 15:07:34 -0700967 ktime_t expires, ktime_t period)
968{
969 int ret = 0;
970 mutex_lock(&rtc->ops_lock);
971 if (timer->enabled)
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100972 rtc_timer_remove(rtc, timer);
John Stultz6610e082010-09-23 15:07:34 -0700973
974 timer->node.expires = expires;
975 timer->period = period;
976
John Stultzaa0be0f2011-01-20 15:26:12 -0800977 ret = rtc_timer_enqueue(rtc, timer);
John Stultz6610e082010-09-23 15:07:34 -0700978
979 mutex_unlock(&rtc->ops_lock);
980 return ret;
981}
982
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100983/* rtc_timer_cancel - Stops an rtc_timer
John Stultz6610e082010-09-23 15:07:34 -0700984 * @ rtc: rtc device to be used
985 * @ timer: timer being set
986 *
987 * Kernel interface to cancel an rtc_timer
988 */
Krzysztof Kozlowski73744a62015-05-03 18:57:11 +0900989void rtc_timer_cancel(struct rtc_device *rtc, struct rtc_timer *timer)
John Stultz6610e082010-09-23 15:07:34 -0700990{
John Stultz6610e082010-09-23 15:07:34 -0700991 mutex_lock(&rtc->ops_lock);
992 if (timer->enabled)
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100993 rtc_timer_remove(rtc, timer);
John Stultz6610e082010-09-23 15:07:34 -0700994 mutex_unlock(&rtc->ops_lock);
John Stultz6610e082010-09-23 15:07:34 -0700995}
996
Joshua Claytonb3967062016-02-05 12:41:11 -0800997/**
998 * rtc_read_offset - Read the amount of rtc offset in parts per billion
999 * @ rtc: rtc device to be used
1000 * @ offset: the offset in parts per billion
1001 *
1002 * see below for details.
1003 *
1004 * Kernel interface to read rtc clock offset
1005 * Returns 0 on success, or a negative number on error.
1006 * If read_offset() is not implemented for the rtc, return -EINVAL
1007 */
1008int rtc_read_offset(struct rtc_device *rtc, long *offset)
1009{
1010 int ret;
John Stultz6610e082010-09-23 15:07:34 -07001011
Joshua Claytonb3967062016-02-05 12:41:11 -08001012 if (!rtc->ops)
1013 return -ENODEV;
1014
1015 if (!rtc->ops->read_offset)
1016 return -EINVAL;
1017
1018 mutex_lock(&rtc->ops_lock);
1019 ret = rtc->ops->read_offset(rtc->dev.parent, offset);
1020 mutex_unlock(&rtc->ops_lock);
Baolin Wang29a1f592017-12-14 13:31:43 +08001021
1022 trace_rtc_read_offset(*offset, ret);
Joshua Claytonb3967062016-02-05 12:41:11 -08001023 return ret;
1024}
1025
1026/**
1027 * rtc_set_offset - Adjusts the duration of the average second
1028 * @ rtc: rtc device to be used
1029 * @ offset: the offset in parts per billion
1030 *
1031 * Some rtc's allow an adjustment to the average duration of a second
1032 * to compensate for differences in the actual clock rate due to temperature,
1033 * the crystal, capacitor, etc.
1034 *
Russell King8a25c8f2017-09-29 11:23:25 +01001035 * The adjustment applied is as follows:
1036 * t = t0 * (1 + offset * 1e-9)
1037 * where t0 is the measured length of 1 RTC second with offset = 0
1038 *
Joshua Claytonb3967062016-02-05 12:41:11 -08001039 * Kernel interface to adjust an rtc clock offset.
1040 * Return 0 on success, or a negative number on error.
1041 * If the rtc offset is not setable (or not implemented), return -EINVAL
1042 */
1043int rtc_set_offset(struct rtc_device *rtc, long offset)
1044{
1045 int ret;
1046
1047 if (!rtc->ops)
1048 return -ENODEV;
1049
1050 if (!rtc->ops->set_offset)
1051 return -EINVAL;
1052
1053 mutex_lock(&rtc->ops_lock);
1054 ret = rtc->ops->set_offset(rtc->dev.parent, offset);
1055 mutex_unlock(&rtc->ops_lock);
Baolin Wang29a1f592017-12-14 13:31:43 +08001056
1057 trace_rtc_set_offset(offset, ret);
Joshua Claytonb3967062016-02-05 12:41:11 -08001058 return ret;
1059}