blob: 37215cf983e92926653d1f3206aa8c4c8842a55a [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
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
John Stultz6610e082010-09-23 15:07:34 -070023static int __rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm)
24{
25 int err;
26 if (!rtc->ops)
27 err = -ENODEV;
28 else if (!rtc->ops->read_time)
29 err = -EINVAL;
30 else {
31 memset(tm, 0, sizeof(struct rtc_time));
32 err = rtc->ops->read_time(rtc->dev.parent, tm);
Hyogi Gim16682c862014-12-10 15:52:27 -080033 if (err < 0) {
34 dev_err(&rtc->dev, "read_time: fail to read\n");
35 return err;
36 }
37
38 err = rtc_valid_tm(tm);
39 if (err < 0)
40 dev_err(&rtc->dev, "read_time: rtc_time isn't valid\n");
John Stultz6610e082010-09-23 15:07:34 -070041 }
42 return err;
43}
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080044
David Brownellab6a2d72007-05-08 00:33:30 -070045int rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080046{
47 int err;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080048
49 err = mutex_lock_interruptible(&rtc->ops_lock);
50 if (err)
David Brownellb68bb262008-07-29 22:33:30 -070051 return err;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080052
John Stultz6610e082010-09-23 15:07:34 -070053 err = __rtc_read_time(rtc, tm);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080054 mutex_unlock(&rtc->ops_lock);
55 return err;
56}
57EXPORT_SYMBOL_GPL(rtc_read_time);
58
David Brownellab6a2d72007-05-08 00:33:30 -070059int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080060{
61 int err;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080062
63 err = rtc_valid_tm(tm);
64 if (err != 0)
65 return err;
66
67 err = mutex_lock_interruptible(&rtc->ops_lock);
68 if (err)
David Brownellb68bb262008-07-29 22:33:30 -070069 return err;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080070
71 if (!rtc->ops)
72 err = -ENODEV;
Alessandro Zummobbccf832009-01-06 14:42:21 -080073 else if (rtc->ops->set_time)
David Brownellcd966202007-05-08 00:33:40 -070074 err = rtc->ops->set_time(rtc->dev.parent, tm);
Alessandro Zummobbccf832009-01-06 14:42:21 -080075 else if (rtc->ops->set_mmss) {
Xunlei Pangbc10aa92015-01-22 02:31:51 +000076 time64_t secs64 = rtc_tm_to_time64(tm);
77 err = rtc->ops->set_mmss(rtc->dev.parent, secs64);
Alessandro Zummobbccf832009-01-06 14:42:21 -080078 } else
79 err = -EINVAL;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080080
Zoran Markovic14d0e342013-06-26 16:09:13 -070081 pm_stay_awake(rtc->dev.parent);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080082 mutex_unlock(&rtc->ops_lock);
NeilBrown5f9679d2011-12-09 09:39:15 +110083 /* A timer might have just expired */
84 schedule_work(&rtc->irqwork);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080085 return err;
86}
87EXPORT_SYMBOL_GPL(rtc_set_time);
88
David Brownellab6a2d72007-05-08 00:33:30 -070089int rtc_set_mmss(struct rtc_device *rtc, unsigned long secs)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080090{
91 int err;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080092
93 err = mutex_lock_interruptible(&rtc->ops_lock);
94 if (err)
David Brownellb68bb262008-07-29 22:33:30 -070095 return err;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080096
97 if (!rtc->ops)
98 err = -ENODEV;
99 else if (rtc->ops->set_mmss)
David Brownellcd966202007-05-08 00:33:40 -0700100 err = rtc->ops->set_mmss(rtc->dev.parent, secs);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800101 else if (rtc->ops->read_time && rtc->ops->set_time) {
102 struct rtc_time new, old;
103
David Brownellcd966202007-05-08 00:33:40 -0700104 err = rtc->ops->read_time(rtc->dev.parent, &old);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800105 if (err == 0) {
Xunlei Pangbc10aa92015-01-22 02:31:51 +0000106 rtc_time64_to_tm(secs, &new);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800107
108 /*
109 * avoid writing when we're going to change the day of
110 * the month. We will retry in the next minute. This
111 * basically means that if the RTC must not drift
112 * by more than 1 minute in 11 minutes.
113 */
114 if (!((old.tm_hour == 23 && old.tm_min == 59) ||
115 (new.tm_hour == 23 && new.tm_min == 59)))
David Brownellcd966202007-05-08 00:33:40 -0700116 err = rtc->ops->set_time(rtc->dev.parent,
David Brownellab6a2d72007-05-08 00:33:30 -0700117 &new);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800118 }
Sachin Kamat3ff2e132013-07-03 15:05:42 -0700119 } else {
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800120 err = -EINVAL;
Sachin Kamat3ff2e132013-07-03 15:05:42 -0700121 }
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800122
Zoran Markovic14d0e342013-06-26 16:09:13 -0700123 pm_stay_awake(rtc->dev.parent);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800124 mutex_unlock(&rtc->ops_lock);
NeilBrown5f9679d2011-12-09 09:39:15 +1100125 /* A timer might have just expired */
126 schedule_work(&rtc->irqwork);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800127
128 return err;
129}
130EXPORT_SYMBOL_GPL(rtc_set_mmss);
131
John Stultzf44f7f92011-02-21 22:58:51 -0800132static int rtc_read_alarm_internal(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
133{
134 int err;
135
136 err = mutex_lock_interruptible(&rtc->ops_lock);
137 if (err)
138 return err;
139
140 if (rtc->ops == NULL)
141 err = -ENODEV;
142 else if (!rtc->ops->read_alarm)
143 err = -EINVAL;
144 else {
145 memset(alarm, 0, sizeof(struct rtc_wkalrm));
146 err = rtc->ops->read_alarm(rtc->dev.parent, alarm);
147 }
148
149 mutex_unlock(&rtc->ops_lock);
150 return err;
151}
152
153int __rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
154{
155 int err;
156 struct rtc_time before, now;
157 int first_time = 1;
Xunlei Pangbc10aa92015-01-22 02:31:51 +0000158 time64_t t_now, t_alm;
John Stultzf44f7f92011-02-21 22:58:51 -0800159 enum { none, day, month, year } missing = none;
160 unsigned days;
161
162 /* The lower level RTC driver may return -1 in some fields,
163 * creating invalid alarm->time values, for reasons like:
164 *
165 * - The hardware may not be capable of filling them in;
166 * many alarms match only on time-of-day fields, not
167 * day/month/year calendar data.
168 *
169 * - Some hardware uses illegal values as "wildcard" match
170 * values, which non-Linux firmware (like a BIOS) may try
171 * to set up as e.g. "alarm 15 minutes after each hour".
172 * Linux uses only oneshot alarms.
173 *
174 * When we see that here, we deal with it by using values from
175 * a current RTC timestamp for any missing (-1) values. The
176 * RTC driver prevents "periodic alarm" modes.
177 *
178 * But this can be racey, because some fields of the RTC timestamp
179 * may have wrapped in the interval since we read the RTC alarm,
180 * which would lead to us inserting inconsistent values in place
181 * of the -1 fields.
182 *
183 * Reading the alarm and timestamp in the reverse sequence
184 * would have the same race condition, and not solve the issue.
185 *
186 * So, we must first read the RTC timestamp,
187 * then read the RTC alarm value,
188 * and then read a second RTC timestamp.
189 *
190 * If any fields of the second timestamp have changed
191 * when compared with the first timestamp, then we know
192 * our timestamp may be inconsistent with that used by
193 * the low-level rtc_read_alarm_internal() function.
194 *
195 * So, when the two timestamps disagree, we just loop and do
196 * the process again to get a fully consistent set of values.
197 *
198 * This could all instead be done in the lower level driver,
199 * but since more than one lower level RTC implementation needs it,
200 * then it's probably best best to do it here instead of there..
201 */
202
203 /* Get the "before" timestamp */
204 err = rtc_read_time(rtc, &before);
205 if (err < 0)
206 return err;
207 do {
208 if (!first_time)
209 memcpy(&before, &now, sizeof(struct rtc_time));
210 first_time = 0;
211
212 /* get the RTC alarm values, which may be incomplete */
213 err = rtc_read_alarm_internal(rtc, alarm);
214 if (err)
215 return err;
216
217 /* full-function RTCs won't have such missing fields */
218 if (rtc_valid_tm(&alarm->time) == 0)
219 return 0;
220
221 /* get the "after" timestamp, to detect wrapped fields */
222 err = rtc_read_time(rtc, &now);
223 if (err < 0)
224 return err;
225
226 /* note that tm_sec is a "don't care" value here: */
227 } while ( before.tm_min != now.tm_min
228 || before.tm_hour != now.tm_hour
229 || before.tm_mon != now.tm_mon
230 || before.tm_year != now.tm_year);
231
232 /* Fill in the missing alarm fields using the timestamp; we
233 * know there's at least one since alarm->time is invalid.
234 */
235 if (alarm->time.tm_sec == -1)
236 alarm->time.tm_sec = now.tm_sec;
237 if (alarm->time.tm_min == -1)
238 alarm->time.tm_min = now.tm_min;
239 if (alarm->time.tm_hour == -1)
240 alarm->time.tm_hour = now.tm_hour;
241
242 /* For simplicity, only support date rollover for now */
Ben Hutchingse74a8f22012-01-10 15:11:02 -0800243 if (alarm->time.tm_mday < 1 || alarm->time.tm_mday > 31) {
John Stultzf44f7f92011-02-21 22:58:51 -0800244 alarm->time.tm_mday = now.tm_mday;
245 missing = day;
246 }
Ben Hutchingse74a8f22012-01-10 15:11:02 -0800247 if ((unsigned)alarm->time.tm_mon >= 12) {
John Stultzf44f7f92011-02-21 22:58:51 -0800248 alarm->time.tm_mon = now.tm_mon;
249 if (missing == none)
250 missing = month;
251 }
252 if (alarm->time.tm_year == -1) {
253 alarm->time.tm_year = now.tm_year;
254 if (missing == none)
255 missing = year;
256 }
257
258 /* with luck, no rollover is needed */
Xunlei Pangbc10aa92015-01-22 02:31:51 +0000259 t_now = rtc_tm_to_time64(&now);
260 t_alm = rtc_tm_to_time64(&alarm->time);
John Stultzf44f7f92011-02-21 22:58:51 -0800261 if (t_now < t_alm)
262 goto done;
263
264 switch (missing) {
265
266 /* 24 hour rollover ... if it's now 10am Monday, an alarm that
267 * that will trigger at 5am will do so at 5am Tuesday, which
268 * could also be in the next month or year. This is a common
269 * case, especially for PCs.
270 */
271 case day:
272 dev_dbg(&rtc->dev, "alarm rollover: %s\n", "day");
273 t_alm += 24 * 60 * 60;
Xunlei Pangbc10aa92015-01-22 02:31:51 +0000274 rtc_time64_to_tm(t_alm, &alarm->time);
John Stultzf44f7f92011-02-21 22:58:51 -0800275 break;
276
277 /* Month rollover ... if it's the 31th, an alarm on the 3rd will
278 * be next month. An alarm matching on the 30th, 29th, or 28th
279 * may end up in the month after that! Many newer PCs support
280 * this type of alarm.
281 */
282 case month:
283 dev_dbg(&rtc->dev, "alarm rollover: %s\n", "month");
284 do {
285 if (alarm->time.tm_mon < 11)
286 alarm->time.tm_mon++;
287 else {
288 alarm->time.tm_mon = 0;
289 alarm->time.tm_year++;
290 }
291 days = rtc_month_days(alarm->time.tm_mon,
292 alarm->time.tm_year);
293 } while (days < alarm->time.tm_mday);
294 break;
295
296 /* Year rollover ... easy except for leap years! */
297 case year:
298 dev_dbg(&rtc->dev, "alarm rollover: %s\n", "year");
299 do {
300 alarm->time.tm_year++;
Ales Novakee1d9012014-06-06 14:35:39 -0700301 } while (!is_leap_year(alarm->time.tm_year + 1900)
302 && rtc_valid_tm(&alarm->time) != 0);
John Stultzf44f7f92011-02-21 22:58:51 -0800303 break;
304
305 default:
306 dev_warn(&rtc->dev, "alarm rollover not handled\n");
307 }
308
309done:
Ales Novakee1d9012014-06-06 14:35:39 -0700310 err = rtc_valid_tm(&alarm->time);
311
312 if (err) {
313 dev_warn(&rtc->dev, "invalid alarm value: %d-%d-%d %d:%d:%d\n",
314 alarm->time.tm_year + 1900, alarm->time.tm_mon + 1,
315 alarm->time.tm_mday, alarm->time.tm_hour, alarm->time.tm_min,
316 alarm->time.tm_sec);
317 }
318
319 return err;
John Stultzf44f7f92011-02-21 22:58:51 -0800320}
321
John Stultz6610e082010-09-23 15:07:34 -0700322int rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800323{
324 int err;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800325
326 err = mutex_lock_interruptible(&rtc->ops_lock);
327 if (err)
David Brownellb68bb262008-07-29 22:33:30 -0700328 return err;
John Stultzd5553a52011-01-20 15:26:13 -0800329 if (rtc->ops == NULL)
330 err = -ENODEV;
331 else if (!rtc->ops->read_alarm)
332 err = -EINVAL;
333 else {
334 memset(alarm, 0, sizeof(struct rtc_wkalrm));
335 alarm->enabled = rtc->aie_timer.enabled;
John Stultz6610e082010-09-23 15:07:34 -0700336 alarm->time = rtc_ktime_to_tm(rtc->aie_timer.node.expires);
John Stultzd5553a52011-01-20 15:26:13 -0800337 }
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800338 mutex_unlock(&rtc->ops_lock);
Mark Lord0e36a9a2007-10-16 01:28:21 -0700339
John Stultzd5553a52011-01-20 15:26:13 -0800340 return err;
Mark Lord0e36a9a2007-10-16 01:28:21 -0700341}
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800342EXPORT_SYMBOL_GPL(rtc_read_alarm);
343
Mark Brownd576fe42011-06-01 11:13:16 +0100344static int __rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
John Stultz6610e082010-09-23 15:07:34 -0700345{
346 struct rtc_time tm;
Xunlei Pangbc10aa92015-01-22 02:31:51 +0000347 time64_t now, scheduled;
John Stultz6610e082010-09-23 15:07:34 -0700348 int err;
349
350 err = rtc_valid_tm(&alarm->time);
351 if (err)
352 return err;
Xunlei Pangbc10aa92015-01-22 02:31:51 +0000353 scheduled = rtc_tm_to_time64(&alarm->time);
John Stultz6610e082010-09-23 15:07:34 -0700354
355 /* Make sure we're not setting alarms in the past */
356 err = __rtc_read_time(rtc, &tm);
Hyogi Gimca6dc2d2014-08-08 14:20:11 -0700357 if (err)
358 return err;
Xunlei Pangbc10aa92015-01-22 02:31:51 +0000359 now = rtc_tm_to_time64(&tm);
John Stultz6610e082010-09-23 15:07:34 -0700360 if (scheduled <= now)
361 return -ETIME;
362 /*
363 * XXX - We just checked to make sure the alarm time is not
364 * in the past, but there is still a race window where if
365 * the is alarm set for the next second and the second ticks
366 * over right here, before we set the alarm.
367 */
368
Linus Torvalds157e8bf2012-01-03 17:32:13 -0800369 if (!rtc->ops)
370 err = -ENODEV;
371 else if (!rtc->ops->set_alarm)
372 err = -EINVAL;
373 else
374 err = rtc->ops->set_alarm(rtc->dev.parent, alarm);
375
376 return err;
John Stultz6610e082010-09-23 15:07:34 -0700377}
378
David Brownellab6a2d72007-05-08 00:33:30 -0700379int rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800380{
381 int err;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800382
David Brownellf8245c22007-05-08 00:34:07 -0700383 err = rtc_valid_tm(&alarm->time);
384 if (err != 0)
385 return err;
386
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800387 err = mutex_lock_interruptible(&rtc->ops_lock);
388 if (err)
David Brownellb68bb262008-07-29 22:33:30 -0700389 return err;
Sachin Kamat3ff2e132013-07-03 15:05:42 -0700390 if (rtc->aie_timer.enabled)
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100391 rtc_timer_remove(rtc, &rtc->aie_timer);
Sachin Kamat3ff2e132013-07-03 15:05:42 -0700392
John Stultz6610e082010-09-23 15:07:34 -0700393 rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time);
394 rtc->aie_timer.period = ktime_set(0, 0);
Sachin Kamat3ff2e132013-07-03 15:05:42 -0700395 if (alarm->enabled)
John Stultzaa0be0f2011-01-20 15:26:12 -0800396 err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
Sachin Kamat3ff2e132013-07-03 15:05:42 -0700397
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800398 mutex_unlock(&rtc->ops_lock);
John Stultzaa0be0f2011-01-20 15:26:12 -0800399 return err;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800400}
401EXPORT_SYMBOL_GPL(rtc_set_alarm);
402
John Stultzf6d5b332011-03-29 18:00:27 -0700403/* Called once per device from rtc_device_register */
404int rtc_initialize_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
405{
406 int err;
John Stultzbd729d72012-01-05 15:21:19 -0800407 struct rtc_time now;
John Stultzf6d5b332011-03-29 18:00:27 -0700408
409 err = rtc_valid_tm(&alarm->time);
410 if (err != 0)
411 return err;
412
John Stultzbd729d72012-01-05 15:21:19 -0800413 err = rtc_read_time(rtc, &now);
414 if (err)
415 return err;
416
John Stultzf6d5b332011-03-29 18:00:27 -0700417 err = mutex_lock_interruptible(&rtc->ops_lock);
418 if (err)
419 return err;
420
421 rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time);
422 rtc->aie_timer.period = ktime_set(0, 0);
John Stultzbd729d72012-01-05 15:21:19 -0800423
424 /* Alarm has to be enabled & in the futrure for us to enqueue it */
425 if (alarm->enabled && (rtc_tm_to_ktime(now).tv64 <
426 rtc->aie_timer.node.expires.tv64)) {
427
John Stultzf6d5b332011-03-29 18:00:27 -0700428 rtc->aie_timer.enabled = 1;
429 timerqueue_add(&rtc->timerqueue, &rtc->aie_timer.node);
430 }
431 mutex_unlock(&rtc->ops_lock);
432 return err;
433}
434EXPORT_SYMBOL_GPL(rtc_initialize_alarm);
435
436
437
Alessandro Zummo099e6572009-01-04 12:00:54 -0800438int rtc_alarm_irq_enable(struct rtc_device *rtc, unsigned int enabled)
439{
440 int err = mutex_lock_interruptible(&rtc->ops_lock);
441 if (err)
442 return err;
443
John Stultz6610e082010-09-23 15:07:34 -0700444 if (rtc->aie_timer.enabled != enabled) {
John Stultzaa0be0f2011-01-20 15:26:12 -0800445 if (enabled)
446 err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
447 else
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100448 rtc_timer_remove(rtc, &rtc->aie_timer);
John Stultz6610e082010-09-23 15:07:34 -0700449 }
450
John Stultzaa0be0f2011-01-20 15:26:12 -0800451 if (err)
Uwe Kleine-König516373b2011-02-14 11:33:17 +0100452 /* nothing */;
453 else if (!rtc->ops)
Alessandro Zummo099e6572009-01-04 12:00:54 -0800454 err = -ENODEV;
455 else if (!rtc->ops->alarm_irq_enable)
456 err = -EINVAL;
457 else
458 err = rtc->ops->alarm_irq_enable(rtc->dev.parent, enabled);
459
460 mutex_unlock(&rtc->ops_lock);
461 return err;
462}
463EXPORT_SYMBOL_GPL(rtc_alarm_irq_enable);
464
465int rtc_update_irq_enable(struct rtc_device *rtc, unsigned int enabled)
466{
467 int err = mutex_lock_interruptible(&rtc->ops_lock);
468 if (err)
469 return err;
470
John Stultz456d66e2011-02-11 18:15:23 -0800471#ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
472 if (enabled == 0 && rtc->uie_irq_active) {
473 mutex_unlock(&rtc->ops_lock);
474 return rtc_dev_update_irq_enable_emul(rtc, 0);
475 }
476#endif
John Stultz6610e082010-09-23 15:07:34 -0700477 /* make sure we're changing state */
478 if (rtc->uie_rtctimer.enabled == enabled)
479 goto out;
480
John Stultz4a649902012-03-06 17:16:09 -0800481 if (rtc->uie_unsupported) {
482 err = -EINVAL;
483 goto out;
484 }
485
John Stultz6610e082010-09-23 15:07:34 -0700486 if (enabled) {
487 struct rtc_time tm;
488 ktime_t now, onesec;
489
490 __rtc_read_time(rtc, &tm);
491 onesec = ktime_set(1, 0);
492 now = rtc_tm_to_ktime(tm);
493 rtc->uie_rtctimer.node.expires = ktime_add(now, onesec);
494 rtc->uie_rtctimer.period = ktime_set(1, 0);
John Stultzaa0be0f2011-01-20 15:26:12 -0800495 err = rtc_timer_enqueue(rtc, &rtc->uie_rtctimer);
496 } else
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100497 rtc_timer_remove(rtc, &rtc->uie_rtctimer);
Alessandro Zummo099e6572009-01-04 12:00:54 -0800498
John Stultz6610e082010-09-23 15:07:34 -0700499out:
Alessandro Zummo099e6572009-01-04 12:00:54 -0800500 mutex_unlock(&rtc->ops_lock);
John Stultz456d66e2011-02-11 18:15:23 -0800501#ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
502 /*
503 * Enable emulation if the driver did not provide
504 * the update_irq_enable function pointer or if returned
505 * -EINVAL to signal that it has been configured without
506 * interrupts or that are not available at the moment.
507 */
508 if (err == -EINVAL)
509 err = rtc_dev_update_irq_enable_emul(rtc, enabled);
510#endif
Alessandro Zummo099e6572009-01-04 12:00:54 -0800511 return err;
John Stultz6610e082010-09-23 15:07:34 -0700512
Alessandro Zummo099e6572009-01-04 12:00:54 -0800513}
514EXPORT_SYMBOL_GPL(rtc_update_irq_enable);
515
John Stultz6610e082010-09-23 15:07:34 -0700516
David Brownelld728b1e2006-11-25 11:09:28 -0800517/**
John Stultz6610e082010-09-23 15:07:34 -0700518 * rtc_handle_legacy_irq - AIE, UIE and PIE event hook
519 * @rtc: pointer to the rtc device
520 *
521 * This function is called when an AIE, UIE or PIE mode interrupt
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300522 * has occurred (or been emulated).
John Stultz6610e082010-09-23 15:07:34 -0700523 *
524 * Triggers the registered irq_task function callback.
525 */
John Stultz456d66e2011-02-11 18:15:23 -0800526void rtc_handle_legacy_irq(struct rtc_device *rtc, int num, int mode)
John Stultz6610e082010-09-23 15:07:34 -0700527{
528 unsigned long flags;
529
530 /* mark one irq of the appropriate mode */
531 spin_lock_irqsave(&rtc->irq_lock, flags);
532 rtc->irq_data = (rtc->irq_data + (num << 8)) | (RTC_IRQF|mode);
533 spin_unlock_irqrestore(&rtc->irq_lock, flags);
534
535 /* call the task func */
536 spin_lock_irqsave(&rtc->irq_task_lock, flags);
537 if (rtc->irq_task)
538 rtc->irq_task->func(rtc->irq_task->private_data);
539 spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
540
541 wake_up_interruptible(&rtc->irq_queue);
542 kill_fasync(&rtc->async_queue, SIGIO, POLL_IN);
543}
544
545
546/**
547 * rtc_aie_update_irq - AIE mode rtctimer hook
548 * @private: pointer to the rtc_device
549 *
550 * This functions is called when the aie_timer expires.
551 */
552void rtc_aie_update_irq(void *private)
553{
554 struct rtc_device *rtc = (struct rtc_device *)private;
555 rtc_handle_legacy_irq(rtc, 1, RTC_AF);
556}
557
558
559/**
560 * rtc_uie_update_irq - UIE mode rtctimer hook
561 * @private: pointer to the rtc_device
562 *
563 * This functions is called when the uie_timer expires.
564 */
565void rtc_uie_update_irq(void *private)
566{
567 struct rtc_device *rtc = (struct rtc_device *)private;
568 rtc_handle_legacy_irq(rtc, 1, RTC_UF);
569}
570
571
572/**
573 * rtc_pie_update_irq - PIE mode hrtimer hook
574 * @timer: pointer to the pie mode hrtimer
575 *
576 * This function is used to emulate PIE mode interrupts
577 * using an hrtimer. This function is called when the periodic
578 * hrtimer expires.
579 */
580enum hrtimer_restart rtc_pie_update_irq(struct hrtimer *timer)
581{
582 struct rtc_device *rtc;
583 ktime_t period;
584 int count;
585 rtc = container_of(timer, struct rtc_device, pie_timer);
586
587 period = ktime_set(0, NSEC_PER_SEC/rtc->irq_freq);
588 count = hrtimer_forward_now(timer, period);
589
590 rtc_handle_legacy_irq(rtc, count, RTC_PF);
591
592 return HRTIMER_RESTART;
593}
594
595/**
596 * rtc_update_irq - Triggered when a RTC interrupt occurs.
David Brownellab6a2d72007-05-08 00:33:30 -0700597 * @rtc: the rtc device
David Brownelld728b1e2006-11-25 11:09:28 -0800598 * @num: how many irqs are being reported (usually one)
599 * @events: mask of RTC_IRQF with one or more of RTC_PF, RTC_AF, RTC_UF
Atsushi Nemotoe6229bec2009-06-18 16:49:09 -0700600 * Context: any
David Brownelld728b1e2006-11-25 11:09:28 -0800601 */
David Brownellab6a2d72007-05-08 00:33:30 -0700602void rtc_update_irq(struct rtc_device *rtc,
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800603 unsigned long num, unsigned long events)
604{
Alessandro Zummo131c9cc2014-04-03 14:50:09 -0700605 if (unlikely(IS_ERR_OR_NULL(rtc)))
606 return;
607
NeilBrown7523cee2012-08-05 22:56:20 +0200608 pm_stay_awake(rtc->dev.parent);
John Stultz6610e082010-09-23 15:07:34 -0700609 schedule_work(&rtc->irqwork);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800610}
611EXPORT_SYMBOL_GPL(rtc_update_irq);
612
Michał Mirosław9f3b7952013-02-01 20:40:17 +0100613static int __rtc_match(struct device *dev, const void *data)
Dave Young71da8902008-01-22 14:00:34 +0800614{
Michał Mirosław9f3b7952013-02-01 20:40:17 +0100615 const char *name = data;
Dave Young71da8902008-01-22 14:00:34 +0800616
Kay Sieversd4afc762009-01-06 14:42:11 -0800617 if (strcmp(dev_name(dev), name) == 0)
Dave Young71da8902008-01-22 14:00:34 +0800618 return 1;
619 return 0;
620}
621
Michał Mirosław9f3b7952013-02-01 20:40:17 +0100622struct rtc_device *rtc_class_open(const char *name)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800623{
David Brownellcd966202007-05-08 00:33:40 -0700624 struct device *dev;
David Brownellab6a2d72007-05-08 00:33:30 -0700625 struct rtc_device *rtc = NULL;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800626
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -0400627 dev = class_find_device(rtc_class, NULL, name, __rtc_match);
Dave Young71da8902008-01-22 14:00:34 +0800628 if (dev)
629 rtc = to_rtc_device(dev);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800630
David Brownellab6a2d72007-05-08 00:33:30 -0700631 if (rtc) {
632 if (!try_module_get(rtc->owner)) {
David Brownellcd966202007-05-08 00:33:40 -0700633 put_device(dev);
David Brownellab6a2d72007-05-08 00:33:30 -0700634 rtc = NULL;
635 }
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800636 }
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800637
David Brownellab6a2d72007-05-08 00:33:30 -0700638 return rtc;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800639}
640EXPORT_SYMBOL_GPL(rtc_class_open);
641
David Brownellab6a2d72007-05-08 00:33:30 -0700642void rtc_class_close(struct rtc_device *rtc)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800643{
David Brownellab6a2d72007-05-08 00:33:30 -0700644 module_put(rtc->owner);
David Brownellcd966202007-05-08 00:33:40 -0700645 put_device(&rtc->dev);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800646}
647EXPORT_SYMBOL_GPL(rtc_class_close);
648
David Brownellab6a2d72007-05-08 00:33:30 -0700649int rtc_irq_register(struct rtc_device *rtc, struct rtc_task *task)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800650{
651 int retval = -EBUSY;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800652
653 if (task == NULL || task->func == NULL)
654 return -EINVAL;
655
Alessandro Zummod691eb92007-10-16 01:28:15 -0700656 /* Cannot register while the char dev is in use */
Jiri Kosina372a3022007-12-04 23:45:05 -0800657 if (test_and_set_bit_lock(RTC_DEV_BUSY, &rtc->flags))
Alessandro Zummod691eb92007-10-16 01:28:15 -0700658 return -EBUSY;
659
David Brownelld728b1e2006-11-25 11:09:28 -0800660 spin_lock_irq(&rtc->irq_task_lock);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800661 if (rtc->irq_task == NULL) {
662 rtc->irq_task = task;
663 retval = 0;
664 }
David Brownelld728b1e2006-11-25 11:09:28 -0800665 spin_unlock_irq(&rtc->irq_task_lock);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800666
Jiri Kosina372a3022007-12-04 23:45:05 -0800667 clear_bit_unlock(RTC_DEV_BUSY, &rtc->flags);
Alessandro Zummod691eb92007-10-16 01:28:15 -0700668
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800669 return retval;
670}
671EXPORT_SYMBOL_GPL(rtc_irq_register);
672
David Brownellab6a2d72007-05-08 00:33:30 -0700673void rtc_irq_unregister(struct rtc_device *rtc, struct rtc_task *task)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800674{
David Brownelld728b1e2006-11-25 11:09:28 -0800675 spin_lock_irq(&rtc->irq_task_lock);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800676 if (rtc->irq_task == task)
677 rtc->irq_task = NULL;
David Brownelld728b1e2006-11-25 11:09:28 -0800678 spin_unlock_irq(&rtc->irq_task_lock);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800679}
680EXPORT_SYMBOL_GPL(rtc_irq_unregister);
681
Thomas Gleixner3c8bb90e2011-07-22 09:12:51 +0000682static int rtc_update_hrtimer(struct rtc_device *rtc, int enabled)
683{
684 /*
685 * We always cancel the timer here first, because otherwise
686 * we could run into BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
687 * when we manage to start the timer before the callback
688 * returns HRTIMER_RESTART.
689 *
690 * We cannot use hrtimer_cancel() here as a running callback
691 * could be blocked on rtc->irq_task_lock and hrtimer_cancel()
692 * would spin forever.
693 */
694 if (hrtimer_try_to_cancel(&rtc->pie_timer) < 0)
695 return -1;
696
697 if (enabled) {
698 ktime_t period = ktime_set(0, NSEC_PER_SEC / rtc->irq_freq);
699
700 hrtimer_start(&rtc->pie_timer, period, HRTIMER_MODE_REL);
701 }
702 return 0;
703}
704
David Brownell97144c62007-10-16 01:28:16 -0700705/**
706 * rtc_irq_set_state - enable/disable 2^N Hz periodic IRQs
707 * @rtc: the rtc device
708 * @task: currently registered with rtc_irq_register()
709 * @enabled: true to enable periodic IRQs
710 * Context: any
711 *
712 * Note that rtc_irq_set_freq() should previously have been used to
713 * specify the desired frequency of periodic IRQ task->func() callbacks.
714 */
David Brownellab6a2d72007-05-08 00:33:30 -0700715int rtc_irq_set_state(struct rtc_device *rtc, struct rtc_task *task, int enabled)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800716{
717 int err = 0;
718 unsigned long flags;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800719
Thomas Gleixner3c8bb90e2011-07-22 09:12:51 +0000720retry:
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800721 spin_lock_irqsave(&rtc->irq_task_lock, flags);
Alessandro Zummod691eb92007-10-16 01:28:15 -0700722 if (rtc->irq_task != NULL && task == NULL)
723 err = -EBUSY;
Chris Brand0734e272013-07-03 15:07:57 -0700724 else if (rtc->irq_task != task)
Alessandro Zummod691eb92007-10-16 01:28:15 -0700725 err = -EACCES;
Chris Brand0734e272013-07-03 15:07:57 -0700726 else {
Thomas Gleixner3c8bb90e2011-07-22 09:12:51 +0000727 if (rtc_update_hrtimer(rtc, enabled) < 0) {
728 spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
729 cpu_relax();
730 goto retry;
731 }
732 rtc->pie_enabled = enabled;
John Stultz6610e082010-09-23 15:07:34 -0700733 }
John Stultz6610e082010-09-23 15:07:34 -0700734 spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800735 return err;
736}
737EXPORT_SYMBOL_GPL(rtc_irq_set_state);
738
David Brownell97144c62007-10-16 01:28:16 -0700739/**
740 * rtc_irq_set_freq - set 2^N Hz periodic IRQ frequency for IRQ
741 * @rtc: the rtc device
742 * @task: currently registered with rtc_irq_register()
743 * @freq: positive frequency with which task->func() will be called
744 * Context: any
745 *
746 * Note that rtc_irq_set_state() is used to enable or disable the
747 * periodic IRQs.
748 */
David Brownellab6a2d72007-05-08 00:33:30 -0700749int rtc_irq_set_freq(struct rtc_device *rtc, struct rtc_task *task, int freq)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800750{
Alessandro Zummo56f10c62006-06-25 05:48:20 -0700751 int err = 0;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800752 unsigned long flags;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800753
Thomas Gleixner6e7a3332011-07-22 09:12:51 +0000754 if (freq <= 0 || freq > RTC_MAX_FREQ)
Marcelo Roberto Jimenez83a06bf2011-02-02 16:04:02 -0200755 return -EINVAL;
Thomas Gleixner3c8bb90e2011-07-22 09:12:51 +0000756retry:
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800757 spin_lock_irqsave(&rtc->irq_task_lock, flags);
Alessandro Zummod691eb92007-10-16 01:28:15 -0700758 if (rtc->irq_task != NULL && task == NULL)
759 err = -EBUSY;
Chris Brand0734e272013-07-03 15:07:57 -0700760 else if (rtc->irq_task != task)
Alessandro Zummod691eb92007-10-16 01:28:15 -0700761 err = -EACCES;
Chris Brand0734e272013-07-03 15:07:57 -0700762 else {
John Stultz6610e082010-09-23 15:07:34 -0700763 rtc->irq_freq = freq;
Thomas Gleixner3c8bb90e2011-07-22 09:12:51 +0000764 if (rtc->pie_enabled && rtc_update_hrtimer(rtc, 1) < 0) {
765 spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
766 cpu_relax();
767 goto retry;
John Stultz6610e082010-09-23 15:07:34 -0700768 }
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800769 }
John Stultz6610e082010-09-23 15:07:34 -0700770 spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800771 return err;
772}
David Brownell2601a462006-11-25 11:09:27 -0800773EXPORT_SYMBOL_GPL(rtc_irq_set_freq);
John Stultz6610e082010-09-23 15:07:34 -0700774
775/**
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100776 * rtc_timer_enqueue - Adds a rtc_timer to the rtc_device timerqueue
John Stultz6610e082010-09-23 15:07:34 -0700777 * @rtc rtc device
778 * @timer timer being added.
779 *
780 * Enqueues a timer onto the rtc devices timerqueue and sets
781 * the next alarm event appropriately.
782 *
John Stultzaa0be0f2011-01-20 15:26:12 -0800783 * Sets the enabled bit on the added timer.
784 *
John Stultz6610e082010-09-23 15:07:34 -0700785 * Must hold ops_lock for proper serialization of timerqueue
786 */
John Stultzaa0be0f2011-01-20 15:26:12 -0800787static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer)
John Stultz6610e082010-09-23 15:07:34 -0700788{
John Stultzaa0be0f2011-01-20 15:26:12 -0800789 timer->enabled = 1;
John Stultz6610e082010-09-23 15:07:34 -0700790 timerqueue_add(&rtc->timerqueue, &timer->node);
791 if (&timer->node == timerqueue_getnext(&rtc->timerqueue)) {
792 struct rtc_wkalrm alarm;
793 int err;
794 alarm.time = rtc_ktime_to_tm(timer->node.expires);
795 alarm.enabled = 1;
796 err = __rtc_set_alarm(rtc, &alarm);
Zoran Markovic14d0e342013-06-26 16:09:13 -0700797 if (err == -ETIME) {
798 pm_stay_awake(rtc->dev.parent);
John Stultz6610e082010-09-23 15:07:34 -0700799 schedule_work(&rtc->irqwork);
Zoran Markovic14d0e342013-06-26 16:09:13 -0700800 } else if (err) {
John Stultzaa0be0f2011-01-20 15:26:12 -0800801 timerqueue_del(&rtc->timerqueue, &timer->node);
802 timer->enabled = 0;
803 return err;
804 }
John Stultz6610e082010-09-23 15:07:34 -0700805 }
John Stultzaa0be0f2011-01-20 15:26:12 -0800806 return 0;
John Stultz6610e082010-09-23 15:07:34 -0700807}
808
Rabin Vincent41c7f742011-11-22 11:03:14 +0100809static void rtc_alarm_disable(struct rtc_device *rtc)
810{
811 if (!rtc->ops || !rtc->ops->alarm_irq_enable)
812 return;
813
814 rtc->ops->alarm_irq_enable(rtc->dev.parent, false);
815}
816
John Stultz6610e082010-09-23 15:07:34 -0700817/**
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100818 * rtc_timer_remove - Removes a rtc_timer from the rtc_device timerqueue
John Stultz6610e082010-09-23 15:07:34 -0700819 * @rtc rtc device
820 * @timer timer being removed.
821 *
822 * Removes a timer onto the rtc devices timerqueue and sets
823 * the next alarm event appropriately.
824 *
John Stultzaa0be0f2011-01-20 15:26:12 -0800825 * Clears the enabled bit on the removed timer.
826 *
John Stultz6610e082010-09-23 15:07:34 -0700827 * Must hold ops_lock for proper serialization of timerqueue
828 */
John Stultzaa0be0f2011-01-20 15:26:12 -0800829static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer)
John Stultz6610e082010-09-23 15:07:34 -0700830{
831 struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue);
832 timerqueue_del(&rtc->timerqueue, &timer->node);
John Stultzaa0be0f2011-01-20 15:26:12 -0800833 timer->enabled = 0;
John Stultz6610e082010-09-23 15:07:34 -0700834 if (next == &timer->node) {
835 struct rtc_wkalrm alarm;
836 int err;
837 next = timerqueue_getnext(&rtc->timerqueue);
Rabin Vincent41c7f742011-11-22 11:03:14 +0100838 if (!next) {
839 rtc_alarm_disable(rtc);
John Stultz6610e082010-09-23 15:07:34 -0700840 return;
Rabin Vincent41c7f742011-11-22 11:03:14 +0100841 }
John Stultz6610e082010-09-23 15:07:34 -0700842 alarm.time = rtc_ktime_to_tm(next->expires);
843 alarm.enabled = 1;
844 err = __rtc_set_alarm(rtc, &alarm);
Zoran Markovic14d0e342013-06-26 16:09:13 -0700845 if (err == -ETIME) {
846 pm_stay_awake(rtc->dev.parent);
John Stultz6610e082010-09-23 15:07:34 -0700847 schedule_work(&rtc->irqwork);
Zoran Markovic14d0e342013-06-26 16:09:13 -0700848 }
John Stultz6610e082010-09-23 15:07:34 -0700849 }
850}
851
852/**
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100853 * rtc_timer_do_work - Expires rtc timers
John Stultz6610e082010-09-23 15:07:34 -0700854 * @rtc rtc device
855 * @timer timer being removed.
856 *
857 * Expires rtc timers. Reprograms next alarm event if needed.
858 * Called via worktask.
859 *
860 * Serializes access to timerqueue via ops_lock mutex
861 */
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100862void rtc_timer_do_work(struct work_struct *work)
John Stultz6610e082010-09-23 15:07:34 -0700863{
864 struct rtc_timer *timer;
865 struct timerqueue_node *next;
866 ktime_t now;
867 struct rtc_time tm;
868
869 struct rtc_device *rtc =
870 container_of(work, struct rtc_device, irqwork);
871
872 mutex_lock(&rtc->ops_lock);
873again:
874 __rtc_read_time(rtc, &tm);
875 now = rtc_tm_to_ktime(tm);
876 while ((next = timerqueue_getnext(&rtc->timerqueue))) {
877 if (next->expires.tv64 > now.tv64)
878 break;
879
880 /* expire timer */
881 timer = container_of(next, struct rtc_timer, node);
882 timerqueue_del(&rtc->timerqueue, &timer->node);
883 timer->enabled = 0;
884 if (timer->task.func)
885 timer->task.func(timer->task.private_data);
886
887 /* Re-add/fwd periodic timers */
888 if (ktime_to_ns(timer->period)) {
889 timer->node.expires = ktime_add(timer->node.expires,
890 timer->period);
891 timer->enabled = 1;
892 timerqueue_add(&rtc->timerqueue, &timer->node);
893 }
894 }
895
896 /* Set next alarm */
897 if (next) {
898 struct rtc_wkalrm alarm;
899 int err;
Xunlei Pang6528b882014-12-10 15:54:26 -0800900 int retry = 3;
901
John Stultz6610e082010-09-23 15:07:34 -0700902 alarm.time = rtc_ktime_to_tm(next->expires);
903 alarm.enabled = 1;
Xunlei Pang6528b882014-12-10 15:54:26 -0800904reprogram:
John Stultz6610e082010-09-23 15:07:34 -0700905 err = __rtc_set_alarm(rtc, &alarm);
906 if (err == -ETIME)
907 goto again;
Xunlei Pang6528b882014-12-10 15:54:26 -0800908 else if (err) {
909 if (retry-- > 0)
910 goto reprogram;
911
912 timer = container_of(next, struct rtc_timer, node);
913 timerqueue_del(&rtc->timerqueue, &timer->node);
914 timer->enabled = 0;
915 dev_err(&rtc->dev, "__rtc_set_alarm: err=%d\n", err);
916 goto again;
917 }
Rabin Vincent41c7f742011-11-22 11:03:14 +0100918 } else
919 rtc_alarm_disable(rtc);
John Stultz6610e082010-09-23 15:07:34 -0700920
Zoran Markovic14d0e342013-06-26 16:09:13 -0700921 pm_relax(rtc->dev.parent);
John Stultz6610e082010-09-23 15:07:34 -0700922 mutex_unlock(&rtc->ops_lock);
923}
924
925
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100926/* rtc_timer_init - Initializes an rtc_timer
John Stultz6610e082010-09-23 15:07:34 -0700927 * @timer: timer to be intiialized
928 * @f: function pointer to be called when timer fires
929 * @data: private data passed to function pointer
930 *
931 * Kernel interface to initializing an rtc_timer.
932 */
Sachin Kamat3ff2e132013-07-03 15:05:42 -0700933void rtc_timer_init(struct rtc_timer *timer, void (*f)(void *p), void *data)
John Stultz6610e082010-09-23 15:07:34 -0700934{
935 timerqueue_init(&timer->node);
936 timer->enabled = 0;
937 timer->task.func = f;
938 timer->task.private_data = data;
939}
940
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100941/* rtc_timer_start - Sets an rtc_timer to fire in the future
John Stultz6610e082010-09-23 15:07:34 -0700942 * @ rtc: rtc device to be used
943 * @ timer: timer being set
944 * @ expires: time at which to expire the timer
945 * @ period: period that the timer will recur
946 *
947 * Kernel interface to set an rtc_timer
948 */
Sachin Kamat3ff2e132013-07-03 15:05:42 -0700949int rtc_timer_start(struct rtc_device *rtc, struct rtc_timer *timer,
John Stultz6610e082010-09-23 15:07:34 -0700950 ktime_t expires, ktime_t period)
951{
952 int ret = 0;
953 mutex_lock(&rtc->ops_lock);
954 if (timer->enabled)
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100955 rtc_timer_remove(rtc, timer);
John Stultz6610e082010-09-23 15:07:34 -0700956
957 timer->node.expires = expires;
958 timer->period = period;
959
John Stultzaa0be0f2011-01-20 15:26:12 -0800960 ret = rtc_timer_enqueue(rtc, timer);
John Stultz6610e082010-09-23 15:07:34 -0700961
962 mutex_unlock(&rtc->ops_lock);
963 return ret;
964}
965
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100966/* rtc_timer_cancel - Stops an rtc_timer
John Stultz6610e082010-09-23 15:07:34 -0700967 * @ rtc: rtc device to be used
968 * @ timer: timer being set
969 *
970 * Kernel interface to cancel an rtc_timer
971 */
Sachin Kamat3ff2e132013-07-03 15:05:42 -0700972int rtc_timer_cancel(struct rtc_device *rtc, struct rtc_timer *timer)
John Stultz6610e082010-09-23 15:07:34 -0700973{
974 int ret = 0;
975 mutex_lock(&rtc->ops_lock);
976 if (timer->enabled)
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100977 rtc_timer_remove(rtc, timer);
John Stultz6610e082010-09-23 15:07:34 -0700978 mutex_unlock(&rtc->ops_lock);
979 return ret;
980}
981
982