blob: d43ee409a5f29c8ba344232577f56eab6121a6b2 [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);
Xunlei Pang8e4ff1a2015-04-01 20:34:27 -070075 else if (rtc->ops->set_mmss64) {
76 time64_t secs64 = rtc_tm_to_time64(tm);
77
78 err = rtc->ops->set_mmss64(rtc->dev.parent, secs64);
79 } else if (rtc->ops->set_mmss) {
Xunlei Pangbc10aa92015-01-22 02:31:51 +000080 time64_t secs64 = rtc_tm_to_time64(tm);
81 err = rtc->ops->set_mmss(rtc->dev.parent, secs64);
Alessandro Zummobbccf832009-01-06 14:42:21 -080082 } else
83 err = -EINVAL;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080084
Zoran Markovic14d0e342013-06-26 16:09:13 -070085 pm_stay_awake(rtc->dev.parent);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080086 mutex_unlock(&rtc->ops_lock);
NeilBrown5f9679d2011-12-09 09:39:15 +110087 /* A timer might have just expired */
88 schedule_work(&rtc->irqwork);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080089 return err;
90}
91EXPORT_SYMBOL_GPL(rtc_set_time);
92
David Brownellab6a2d72007-05-08 00:33:30 -070093int rtc_set_mmss(struct rtc_device *rtc, unsigned long secs)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080094{
95 int err;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080096
97 err = mutex_lock_interruptible(&rtc->ops_lock);
98 if (err)
David Brownellb68bb262008-07-29 22:33:30 -070099 return err;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800100
101 if (!rtc->ops)
102 err = -ENODEV;
Xunlei Pang8e4ff1a2015-04-01 20:34:27 -0700103 else if (rtc->ops->set_mmss64)
104 err = rtc->ops->set_mmss64(rtc->dev.parent, secs);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800105 else if (rtc->ops->set_mmss)
David Brownellcd966202007-05-08 00:33:40 -0700106 err = rtc->ops->set_mmss(rtc->dev.parent, secs);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800107 else if (rtc->ops->read_time && rtc->ops->set_time) {
108 struct rtc_time new, old;
109
David Brownellcd966202007-05-08 00:33:40 -0700110 err = rtc->ops->read_time(rtc->dev.parent, &old);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800111 if (err == 0) {
Xunlei Pangbc10aa92015-01-22 02:31:51 +0000112 rtc_time64_to_tm(secs, &new);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800113
114 /*
115 * avoid writing when we're going to change the day of
116 * the month. We will retry in the next minute. This
117 * basically means that if the RTC must not drift
118 * by more than 1 minute in 11 minutes.
119 */
120 if (!((old.tm_hour == 23 && old.tm_min == 59) ||
121 (new.tm_hour == 23 && new.tm_min == 59)))
David Brownellcd966202007-05-08 00:33:40 -0700122 err = rtc->ops->set_time(rtc->dev.parent,
David Brownellab6a2d72007-05-08 00:33:30 -0700123 &new);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800124 }
Sachin Kamat3ff2e132013-07-03 15:05:42 -0700125 } else {
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800126 err = -EINVAL;
Sachin Kamat3ff2e132013-07-03 15:05:42 -0700127 }
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800128
Zoran Markovic14d0e342013-06-26 16:09:13 -0700129 pm_stay_awake(rtc->dev.parent);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800130 mutex_unlock(&rtc->ops_lock);
NeilBrown5f9679d2011-12-09 09:39:15 +1100131 /* A timer might have just expired */
132 schedule_work(&rtc->irqwork);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800133
134 return err;
135}
136EXPORT_SYMBOL_GPL(rtc_set_mmss);
137
John Stultzf44f7f92011-02-21 22:58:51 -0800138static int rtc_read_alarm_internal(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
139{
140 int err;
141
142 err = mutex_lock_interruptible(&rtc->ops_lock);
143 if (err)
144 return err;
145
146 if (rtc->ops == NULL)
147 err = -ENODEV;
148 else if (!rtc->ops->read_alarm)
149 err = -EINVAL;
150 else {
151 memset(alarm, 0, sizeof(struct rtc_wkalrm));
152 err = rtc->ops->read_alarm(rtc->dev.parent, alarm);
153 }
154
155 mutex_unlock(&rtc->ops_lock);
156 return err;
157}
158
159int __rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
160{
161 int err;
162 struct rtc_time before, now;
163 int first_time = 1;
Xunlei Pangbc10aa92015-01-22 02:31:51 +0000164 time64_t t_now, t_alm;
John Stultzf44f7f92011-02-21 22:58:51 -0800165 enum { none, day, month, year } missing = none;
166 unsigned days;
167
168 /* The lower level RTC driver may return -1 in some fields,
169 * creating invalid alarm->time values, for reasons like:
170 *
171 * - The hardware may not be capable of filling them in;
172 * many alarms match only on time-of-day fields, not
173 * day/month/year calendar data.
174 *
175 * - Some hardware uses illegal values as "wildcard" match
176 * values, which non-Linux firmware (like a BIOS) may try
177 * to set up as e.g. "alarm 15 minutes after each hour".
178 * Linux uses only oneshot alarms.
179 *
180 * When we see that here, we deal with it by using values from
181 * a current RTC timestamp for any missing (-1) values. The
182 * RTC driver prevents "periodic alarm" modes.
183 *
184 * But this can be racey, because some fields of the RTC timestamp
185 * may have wrapped in the interval since we read the RTC alarm,
186 * which would lead to us inserting inconsistent values in place
187 * of the -1 fields.
188 *
189 * Reading the alarm and timestamp in the reverse sequence
190 * would have the same race condition, and not solve the issue.
191 *
192 * So, we must first read the RTC timestamp,
193 * then read the RTC alarm value,
194 * and then read a second RTC timestamp.
195 *
196 * If any fields of the second timestamp have changed
197 * when compared with the first timestamp, then we know
198 * our timestamp may be inconsistent with that used by
199 * the low-level rtc_read_alarm_internal() function.
200 *
201 * So, when the two timestamps disagree, we just loop and do
202 * the process again to get a fully consistent set of values.
203 *
204 * This could all instead be done in the lower level driver,
205 * but since more than one lower level RTC implementation needs it,
206 * then it's probably best best to do it here instead of there..
207 */
208
209 /* Get the "before" timestamp */
210 err = rtc_read_time(rtc, &before);
211 if (err < 0)
212 return err;
213 do {
214 if (!first_time)
215 memcpy(&before, &now, sizeof(struct rtc_time));
216 first_time = 0;
217
218 /* get the RTC alarm values, which may be incomplete */
219 err = rtc_read_alarm_internal(rtc, alarm);
220 if (err)
221 return err;
222
223 /* full-function RTCs won't have such missing fields */
224 if (rtc_valid_tm(&alarm->time) == 0)
225 return 0;
226
227 /* get the "after" timestamp, to detect wrapped fields */
228 err = rtc_read_time(rtc, &now);
229 if (err < 0)
230 return err;
231
232 /* note that tm_sec is a "don't care" value here: */
233 } while ( before.tm_min != now.tm_min
234 || before.tm_hour != now.tm_hour
235 || before.tm_mon != now.tm_mon
236 || before.tm_year != now.tm_year);
237
238 /* Fill in the missing alarm fields using the timestamp; we
239 * know there's at least one since alarm->time is invalid.
240 */
241 if (alarm->time.tm_sec == -1)
242 alarm->time.tm_sec = now.tm_sec;
243 if (alarm->time.tm_min == -1)
244 alarm->time.tm_min = now.tm_min;
245 if (alarm->time.tm_hour == -1)
246 alarm->time.tm_hour = now.tm_hour;
247
248 /* For simplicity, only support date rollover for now */
Ben Hutchingse74a8f22012-01-10 15:11:02 -0800249 if (alarm->time.tm_mday < 1 || alarm->time.tm_mday > 31) {
John Stultzf44f7f92011-02-21 22:58:51 -0800250 alarm->time.tm_mday = now.tm_mday;
251 missing = day;
252 }
Ben Hutchingse74a8f22012-01-10 15:11:02 -0800253 if ((unsigned)alarm->time.tm_mon >= 12) {
John Stultzf44f7f92011-02-21 22:58:51 -0800254 alarm->time.tm_mon = now.tm_mon;
255 if (missing == none)
256 missing = month;
257 }
258 if (alarm->time.tm_year == -1) {
259 alarm->time.tm_year = now.tm_year;
260 if (missing == none)
261 missing = year;
262 }
263
264 /* with luck, no rollover is needed */
Xunlei Pangbc10aa92015-01-22 02:31:51 +0000265 t_now = rtc_tm_to_time64(&now);
266 t_alm = rtc_tm_to_time64(&alarm->time);
John Stultzf44f7f92011-02-21 22:58:51 -0800267 if (t_now < t_alm)
268 goto done;
269
270 switch (missing) {
271
272 /* 24 hour rollover ... if it's now 10am Monday, an alarm that
273 * that will trigger at 5am will do so at 5am Tuesday, which
274 * could also be in the next month or year. This is a common
275 * case, especially for PCs.
276 */
277 case day:
278 dev_dbg(&rtc->dev, "alarm rollover: %s\n", "day");
279 t_alm += 24 * 60 * 60;
Xunlei Pangbc10aa92015-01-22 02:31:51 +0000280 rtc_time64_to_tm(t_alm, &alarm->time);
John Stultzf44f7f92011-02-21 22:58:51 -0800281 break;
282
283 /* Month rollover ... if it's the 31th, an alarm on the 3rd will
284 * be next month. An alarm matching on the 30th, 29th, or 28th
285 * may end up in the month after that! Many newer PCs support
286 * this type of alarm.
287 */
288 case month:
289 dev_dbg(&rtc->dev, "alarm rollover: %s\n", "month");
290 do {
291 if (alarm->time.tm_mon < 11)
292 alarm->time.tm_mon++;
293 else {
294 alarm->time.tm_mon = 0;
295 alarm->time.tm_year++;
296 }
297 days = rtc_month_days(alarm->time.tm_mon,
298 alarm->time.tm_year);
299 } while (days < alarm->time.tm_mday);
300 break;
301
302 /* Year rollover ... easy except for leap years! */
303 case year:
304 dev_dbg(&rtc->dev, "alarm rollover: %s\n", "year");
305 do {
306 alarm->time.tm_year++;
Ales Novakee1d9012014-06-06 14:35:39 -0700307 } while (!is_leap_year(alarm->time.tm_year + 1900)
308 && rtc_valid_tm(&alarm->time) != 0);
John Stultzf44f7f92011-02-21 22:58:51 -0800309 break;
310
311 default:
312 dev_warn(&rtc->dev, "alarm rollover not handled\n");
313 }
314
315done:
Ales Novakee1d9012014-06-06 14:35:39 -0700316 err = rtc_valid_tm(&alarm->time);
317
318 if (err) {
319 dev_warn(&rtc->dev, "invalid alarm value: %d-%d-%d %d:%d:%d\n",
320 alarm->time.tm_year + 1900, alarm->time.tm_mon + 1,
321 alarm->time.tm_mday, alarm->time.tm_hour, alarm->time.tm_min,
322 alarm->time.tm_sec);
323 }
324
325 return err;
John Stultzf44f7f92011-02-21 22:58:51 -0800326}
327
John Stultz6610e082010-09-23 15:07:34 -0700328int rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800329{
330 int err;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800331
332 err = mutex_lock_interruptible(&rtc->ops_lock);
333 if (err)
David Brownellb68bb262008-07-29 22:33:30 -0700334 return err;
John Stultzd5553a52011-01-20 15:26:13 -0800335 if (rtc->ops == NULL)
336 err = -ENODEV;
337 else if (!rtc->ops->read_alarm)
338 err = -EINVAL;
339 else {
340 memset(alarm, 0, sizeof(struct rtc_wkalrm));
341 alarm->enabled = rtc->aie_timer.enabled;
John Stultz6610e082010-09-23 15:07:34 -0700342 alarm->time = rtc_ktime_to_tm(rtc->aie_timer.node.expires);
John Stultzd5553a52011-01-20 15:26:13 -0800343 }
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800344 mutex_unlock(&rtc->ops_lock);
Mark Lord0e36a9a2007-10-16 01:28:21 -0700345
John Stultzd5553a52011-01-20 15:26:13 -0800346 return err;
Mark Lord0e36a9a2007-10-16 01:28:21 -0700347}
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800348EXPORT_SYMBOL_GPL(rtc_read_alarm);
349
Mark Brownd576fe42011-06-01 11:13:16 +0100350static int __rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
John Stultz6610e082010-09-23 15:07:34 -0700351{
352 struct rtc_time tm;
Xunlei Pangbc10aa92015-01-22 02:31:51 +0000353 time64_t now, scheduled;
John Stultz6610e082010-09-23 15:07:34 -0700354 int err;
355
356 err = rtc_valid_tm(&alarm->time);
357 if (err)
358 return err;
Xunlei Pangbc10aa92015-01-22 02:31:51 +0000359 scheduled = rtc_tm_to_time64(&alarm->time);
John Stultz6610e082010-09-23 15:07:34 -0700360
361 /* Make sure we're not setting alarms in the past */
362 err = __rtc_read_time(rtc, &tm);
Hyogi Gimca6dc2d2014-08-08 14:20:11 -0700363 if (err)
364 return err;
Xunlei Pangbc10aa92015-01-22 02:31:51 +0000365 now = rtc_tm_to_time64(&tm);
John Stultz6610e082010-09-23 15:07:34 -0700366 if (scheduled <= now)
367 return -ETIME;
368 /*
369 * XXX - We just checked to make sure the alarm time is not
370 * in the past, but there is still a race window where if
371 * the is alarm set for the next second and the second ticks
372 * over right here, before we set the alarm.
373 */
374
Linus Torvalds157e8bf2012-01-03 17:32:13 -0800375 if (!rtc->ops)
376 err = -ENODEV;
377 else if (!rtc->ops->set_alarm)
378 err = -EINVAL;
379 else
380 err = rtc->ops->set_alarm(rtc->dev.parent, alarm);
381
382 return err;
John Stultz6610e082010-09-23 15:07:34 -0700383}
384
David Brownellab6a2d72007-05-08 00:33:30 -0700385int rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800386{
387 int err;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800388
David Brownellf8245c22007-05-08 00:34:07 -0700389 err = rtc_valid_tm(&alarm->time);
390 if (err != 0)
391 return err;
392
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800393 err = mutex_lock_interruptible(&rtc->ops_lock);
394 if (err)
David Brownellb68bb262008-07-29 22:33:30 -0700395 return err;
Sachin Kamat3ff2e132013-07-03 15:05:42 -0700396 if (rtc->aie_timer.enabled)
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100397 rtc_timer_remove(rtc, &rtc->aie_timer);
Sachin Kamat3ff2e132013-07-03 15:05:42 -0700398
John Stultz6610e082010-09-23 15:07:34 -0700399 rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time);
400 rtc->aie_timer.period = ktime_set(0, 0);
Sachin Kamat3ff2e132013-07-03 15:05:42 -0700401 if (alarm->enabled)
John Stultzaa0be0f2011-01-20 15:26:12 -0800402 err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
Sachin Kamat3ff2e132013-07-03 15:05:42 -0700403
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800404 mutex_unlock(&rtc->ops_lock);
John Stultzaa0be0f2011-01-20 15:26:12 -0800405 return err;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800406}
407EXPORT_SYMBOL_GPL(rtc_set_alarm);
408
John Stultzf6d5b332011-03-29 18:00:27 -0700409/* Called once per device from rtc_device_register */
410int rtc_initialize_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
411{
412 int err;
John Stultzbd729d72012-01-05 15:21:19 -0800413 struct rtc_time now;
John Stultzf6d5b332011-03-29 18:00:27 -0700414
415 err = rtc_valid_tm(&alarm->time);
416 if (err != 0)
417 return err;
418
John Stultzbd729d72012-01-05 15:21:19 -0800419 err = rtc_read_time(rtc, &now);
420 if (err)
421 return err;
422
John Stultzf6d5b332011-03-29 18:00:27 -0700423 err = mutex_lock_interruptible(&rtc->ops_lock);
424 if (err)
425 return err;
426
427 rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time);
428 rtc->aie_timer.period = ktime_set(0, 0);
John Stultzbd729d72012-01-05 15:21:19 -0800429
430 /* Alarm has to be enabled & in the futrure for us to enqueue it */
431 if (alarm->enabled && (rtc_tm_to_ktime(now).tv64 <
432 rtc->aie_timer.node.expires.tv64)) {
433
John Stultzf6d5b332011-03-29 18:00:27 -0700434 rtc->aie_timer.enabled = 1;
435 timerqueue_add(&rtc->timerqueue, &rtc->aie_timer.node);
436 }
437 mutex_unlock(&rtc->ops_lock);
438 return err;
439}
440EXPORT_SYMBOL_GPL(rtc_initialize_alarm);
441
442
443
Alessandro Zummo099e6572009-01-04 12:00:54 -0800444int rtc_alarm_irq_enable(struct rtc_device *rtc, unsigned int enabled)
445{
446 int err = mutex_lock_interruptible(&rtc->ops_lock);
447 if (err)
448 return err;
449
John Stultz6610e082010-09-23 15:07:34 -0700450 if (rtc->aie_timer.enabled != enabled) {
John Stultzaa0be0f2011-01-20 15:26:12 -0800451 if (enabled)
452 err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
453 else
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100454 rtc_timer_remove(rtc, &rtc->aie_timer);
John Stultz6610e082010-09-23 15:07:34 -0700455 }
456
John Stultzaa0be0f2011-01-20 15:26:12 -0800457 if (err)
Uwe Kleine-König516373b2011-02-14 11:33:17 +0100458 /* nothing */;
459 else if (!rtc->ops)
Alessandro Zummo099e6572009-01-04 12:00:54 -0800460 err = -ENODEV;
461 else if (!rtc->ops->alarm_irq_enable)
462 err = -EINVAL;
463 else
464 err = rtc->ops->alarm_irq_enable(rtc->dev.parent, enabled);
465
466 mutex_unlock(&rtc->ops_lock);
467 return err;
468}
469EXPORT_SYMBOL_GPL(rtc_alarm_irq_enable);
470
471int rtc_update_irq_enable(struct rtc_device *rtc, unsigned int enabled)
472{
473 int err = mutex_lock_interruptible(&rtc->ops_lock);
474 if (err)
475 return err;
476
John Stultz456d66e2011-02-11 18:15:23 -0800477#ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
478 if (enabled == 0 && rtc->uie_irq_active) {
479 mutex_unlock(&rtc->ops_lock);
480 return rtc_dev_update_irq_enable_emul(rtc, 0);
481 }
482#endif
John Stultz6610e082010-09-23 15:07:34 -0700483 /* make sure we're changing state */
484 if (rtc->uie_rtctimer.enabled == enabled)
485 goto out;
486
John Stultz4a649902012-03-06 17:16:09 -0800487 if (rtc->uie_unsupported) {
488 err = -EINVAL;
489 goto out;
490 }
491
John Stultz6610e082010-09-23 15:07:34 -0700492 if (enabled) {
493 struct rtc_time tm;
494 ktime_t now, onesec;
495
496 __rtc_read_time(rtc, &tm);
497 onesec = ktime_set(1, 0);
498 now = rtc_tm_to_ktime(tm);
499 rtc->uie_rtctimer.node.expires = ktime_add(now, onesec);
500 rtc->uie_rtctimer.period = ktime_set(1, 0);
John Stultzaa0be0f2011-01-20 15:26:12 -0800501 err = rtc_timer_enqueue(rtc, &rtc->uie_rtctimer);
502 } else
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100503 rtc_timer_remove(rtc, &rtc->uie_rtctimer);
Alessandro Zummo099e6572009-01-04 12:00:54 -0800504
John Stultz6610e082010-09-23 15:07:34 -0700505out:
Alessandro Zummo099e6572009-01-04 12:00:54 -0800506 mutex_unlock(&rtc->ops_lock);
John Stultz456d66e2011-02-11 18:15:23 -0800507#ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
508 /*
509 * Enable emulation if the driver did not provide
510 * the update_irq_enable function pointer or if returned
511 * -EINVAL to signal that it has been configured without
512 * interrupts or that are not available at the moment.
513 */
514 if (err == -EINVAL)
515 err = rtc_dev_update_irq_enable_emul(rtc, enabled);
516#endif
Alessandro Zummo099e6572009-01-04 12:00:54 -0800517 return err;
John Stultz6610e082010-09-23 15:07:34 -0700518
Alessandro Zummo099e6572009-01-04 12:00:54 -0800519}
520EXPORT_SYMBOL_GPL(rtc_update_irq_enable);
521
John Stultz6610e082010-09-23 15:07:34 -0700522
David Brownelld728b1e2006-11-25 11:09:28 -0800523/**
John Stultz6610e082010-09-23 15:07:34 -0700524 * rtc_handle_legacy_irq - AIE, UIE and PIE event hook
525 * @rtc: pointer to the rtc device
526 *
527 * This function is called when an AIE, UIE or PIE mode interrupt
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300528 * has occurred (or been emulated).
John Stultz6610e082010-09-23 15:07:34 -0700529 *
530 * Triggers the registered irq_task function callback.
531 */
John Stultz456d66e2011-02-11 18:15:23 -0800532void rtc_handle_legacy_irq(struct rtc_device *rtc, int num, int mode)
John Stultz6610e082010-09-23 15:07:34 -0700533{
534 unsigned long flags;
535
536 /* mark one irq of the appropriate mode */
537 spin_lock_irqsave(&rtc->irq_lock, flags);
538 rtc->irq_data = (rtc->irq_data + (num << 8)) | (RTC_IRQF|mode);
539 spin_unlock_irqrestore(&rtc->irq_lock, flags);
540
541 /* call the task func */
542 spin_lock_irqsave(&rtc->irq_task_lock, flags);
543 if (rtc->irq_task)
544 rtc->irq_task->func(rtc->irq_task->private_data);
545 spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
546
547 wake_up_interruptible(&rtc->irq_queue);
548 kill_fasync(&rtc->async_queue, SIGIO, POLL_IN);
549}
550
551
552/**
553 * rtc_aie_update_irq - AIE mode rtctimer hook
554 * @private: pointer to the rtc_device
555 *
556 * This functions is called when the aie_timer expires.
557 */
558void rtc_aie_update_irq(void *private)
559{
560 struct rtc_device *rtc = (struct rtc_device *)private;
561 rtc_handle_legacy_irq(rtc, 1, RTC_AF);
562}
563
564
565/**
566 * rtc_uie_update_irq - UIE mode rtctimer hook
567 * @private: pointer to the rtc_device
568 *
569 * This functions is called when the uie_timer expires.
570 */
571void rtc_uie_update_irq(void *private)
572{
573 struct rtc_device *rtc = (struct rtc_device *)private;
574 rtc_handle_legacy_irq(rtc, 1, RTC_UF);
575}
576
577
578/**
579 * rtc_pie_update_irq - PIE mode hrtimer hook
580 * @timer: pointer to the pie mode hrtimer
581 *
582 * This function is used to emulate PIE mode interrupts
583 * using an hrtimer. This function is called when the periodic
584 * hrtimer expires.
585 */
586enum hrtimer_restart rtc_pie_update_irq(struct hrtimer *timer)
587{
588 struct rtc_device *rtc;
589 ktime_t period;
590 int count;
591 rtc = container_of(timer, struct rtc_device, pie_timer);
592
593 period = ktime_set(0, NSEC_PER_SEC/rtc->irq_freq);
594 count = hrtimer_forward_now(timer, period);
595
596 rtc_handle_legacy_irq(rtc, count, RTC_PF);
597
598 return HRTIMER_RESTART;
599}
600
601/**
602 * rtc_update_irq - Triggered when a RTC interrupt occurs.
David Brownellab6a2d72007-05-08 00:33:30 -0700603 * @rtc: the rtc device
David Brownelld728b1e2006-11-25 11:09:28 -0800604 * @num: how many irqs are being reported (usually one)
605 * @events: mask of RTC_IRQF with one or more of RTC_PF, RTC_AF, RTC_UF
Atsushi Nemotoe6229bec2009-06-18 16:49:09 -0700606 * Context: any
David Brownelld728b1e2006-11-25 11:09:28 -0800607 */
David Brownellab6a2d72007-05-08 00:33:30 -0700608void rtc_update_irq(struct rtc_device *rtc,
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800609 unsigned long num, unsigned long events)
610{
Alessandro Zummo131c9cc2014-04-03 14:50:09 -0700611 if (unlikely(IS_ERR_OR_NULL(rtc)))
612 return;
613
NeilBrown7523cee2012-08-05 22:56:20 +0200614 pm_stay_awake(rtc->dev.parent);
John Stultz6610e082010-09-23 15:07:34 -0700615 schedule_work(&rtc->irqwork);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800616}
617EXPORT_SYMBOL_GPL(rtc_update_irq);
618
Michał Mirosław9f3b7952013-02-01 20:40:17 +0100619static int __rtc_match(struct device *dev, const void *data)
Dave Young71da8902008-01-22 14:00:34 +0800620{
Michał Mirosław9f3b7952013-02-01 20:40:17 +0100621 const char *name = data;
Dave Young71da8902008-01-22 14:00:34 +0800622
Kay Sieversd4afc762009-01-06 14:42:11 -0800623 if (strcmp(dev_name(dev), name) == 0)
Dave Young71da8902008-01-22 14:00:34 +0800624 return 1;
625 return 0;
626}
627
Michał Mirosław9f3b7952013-02-01 20:40:17 +0100628struct rtc_device *rtc_class_open(const char *name)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800629{
David Brownellcd966202007-05-08 00:33:40 -0700630 struct device *dev;
David Brownellab6a2d72007-05-08 00:33:30 -0700631 struct rtc_device *rtc = NULL;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800632
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -0400633 dev = class_find_device(rtc_class, NULL, name, __rtc_match);
Dave Young71da8902008-01-22 14:00:34 +0800634 if (dev)
635 rtc = to_rtc_device(dev);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800636
David Brownellab6a2d72007-05-08 00:33:30 -0700637 if (rtc) {
638 if (!try_module_get(rtc->owner)) {
David Brownellcd966202007-05-08 00:33:40 -0700639 put_device(dev);
David Brownellab6a2d72007-05-08 00:33:30 -0700640 rtc = NULL;
641 }
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800642 }
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800643
David Brownellab6a2d72007-05-08 00:33:30 -0700644 return rtc;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800645}
646EXPORT_SYMBOL_GPL(rtc_class_open);
647
David Brownellab6a2d72007-05-08 00:33:30 -0700648void rtc_class_close(struct rtc_device *rtc)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800649{
David Brownellab6a2d72007-05-08 00:33:30 -0700650 module_put(rtc->owner);
David Brownellcd966202007-05-08 00:33:40 -0700651 put_device(&rtc->dev);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800652}
653EXPORT_SYMBOL_GPL(rtc_class_close);
654
David Brownellab6a2d72007-05-08 00:33:30 -0700655int rtc_irq_register(struct rtc_device *rtc, struct rtc_task *task)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800656{
657 int retval = -EBUSY;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800658
659 if (task == NULL || task->func == NULL)
660 return -EINVAL;
661
Alessandro Zummod691eb92007-10-16 01:28:15 -0700662 /* Cannot register while the char dev is in use */
Jiri Kosina372a3022007-12-04 23:45:05 -0800663 if (test_and_set_bit_lock(RTC_DEV_BUSY, &rtc->flags))
Alessandro Zummod691eb92007-10-16 01:28:15 -0700664 return -EBUSY;
665
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 == NULL) {
668 rtc->irq_task = task;
669 retval = 0;
670 }
David Brownelld728b1e2006-11-25 11:09:28 -0800671 spin_unlock_irq(&rtc->irq_task_lock);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800672
Jiri Kosina372a3022007-12-04 23:45:05 -0800673 clear_bit_unlock(RTC_DEV_BUSY, &rtc->flags);
Alessandro Zummod691eb92007-10-16 01:28:15 -0700674
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800675 return retval;
676}
677EXPORT_SYMBOL_GPL(rtc_irq_register);
678
David Brownellab6a2d72007-05-08 00:33:30 -0700679void rtc_irq_unregister(struct rtc_device *rtc, struct rtc_task *task)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800680{
David Brownelld728b1e2006-11-25 11:09:28 -0800681 spin_lock_irq(&rtc->irq_task_lock);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800682 if (rtc->irq_task == task)
683 rtc->irq_task = NULL;
David Brownelld728b1e2006-11-25 11:09:28 -0800684 spin_unlock_irq(&rtc->irq_task_lock);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800685}
686EXPORT_SYMBOL_GPL(rtc_irq_unregister);
687
Thomas Gleixner3c8bb90e2011-07-22 09:12:51 +0000688static int rtc_update_hrtimer(struct rtc_device *rtc, int enabled)
689{
690 /*
691 * We always cancel the timer here first, because otherwise
692 * we could run into BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
693 * when we manage to start the timer before the callback
694 * returns HRTIMER_RESTART.
695 *
696 * We cannot use hrtimer_cancel() here as a running callback
697 * could be blocked on rtc->irq_task_lock and hrtimer_cancel()
698 * would spin forever.
699 */
700 if (hrtimer_try_to_cancel(&rtc->pie_timer) < 0)
701 return -1;
702
703 if (enabled) {
704 ktime_t period = ktime_set(0, NSEC_PER_SEC / rtc->irq_freq);
705
706 hrtimer_start(&rtc->pie_timer, period, HRTIMER_MODE_REL);
707 }
708 return 0;
709}
710
David Brownell97144c62007-10-16 01:28:16 -0700711/**
712 * rtc_irq_set_state - enable/disable 2^N Hz periodic IRQs
713 * @rtc: the rtc device
714 * @task: currently registered with rtc_irq_register()
715 * @enabled: true to enable periodic IRQs
716 * Context: any
717 *
718 * Note that rtc_irq_set_freq() should previously have been used to
719 * specify the desired frequency of periodic IRQ task->func() callbacks.
720 */
David Brownellab6a2d72007-05-08 00:33:30 -0700721int rtc_irq_set_state(struct rtc_device *rtc, struct rtc_task *task, int enabled)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800722{
723 int err = 0;
724 unsigned long flags;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800725
Thomas Gleixner3c8bb90e2011-07-22 09:12:51 +0000726retry:
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800727 spin_lock_irqsave(&rtc->irq_task_lock, flags);
Alessandro Zummod691eb92007-10-16 01:28:15 -0700728 if (rtc->irq_task != NULL && task == NULL)
729 err = -EBUSY;
Chris Brand0734e272013-07-03 15:07:57 -0700730 else if (rtc->irq_task != task)
Alessandro Zummod691eb92007-10-16 01:28:15 -0700731 err = -EACCES;
Chris Brand0734e272013-07-03 15:07:57 -0700732 else {
Thomas Gleixner3c8bb90e2011-07-22 09:12:51 +0000733 if (rtc_update_hrtimer(rtc, enabled) < 0) {
734 spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
735 cpu_relax();
736 goto retry;
737 }
738 rtc->pie_enabled = enabled;
John Stultz6610e082010-09-23 15:07:34 -0700739 }
John Stultz6610e082010-09-23 15:07:34 -0700740 spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800741 return err;
742}
743EXPORT_SYMBOL_GPL(rtc_irq_set_state);
744
David Brownell97144c62007-10-16 01:28:16 -0700745/**
746 * rtc_irq_set_freq - set 2^N Hz periodic IRQ frequency for IRQ
747 * @rtc: the rtc device
748 * @task: currently registered with rtc_irq_register()
749 * @freq: positive frequency with which task->func() will be called
750 * Context: any
751 *
752 * Note that rtc_irq_set_state() is used to enable or disable the
753 * periodic IRQs.
754 */
David Brownellab6a2d72007-05-08 00:33:30 -0700755int rtc_irq_set_freq(struct rtc_device *rtc, struct rtc_task *task, int freq)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800756{
Alessandro Zummo56f10c62006-06-25 05:48:20 -0700757 int err = 0;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800758 unsigned long flags;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800759
Thomas Gleixner6e7a3332011-07-22 09:12:51 +0000760 if (freq <= 0 || freq > RTC_MAX_FREQ)
Marcelo Roberto Jimenez83a06bf2011-02-02 16:04:02 -0200761 return -EINVAL;
Thomas Gleixner3c8bb90e2011-07-22 09:12:51 +0000762retry:
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800763 spin_lock_irqsave(&rtc->irq_task_lock, flags);
Alessandro Zummod691eb92007-10-16 01:28:15 -0700764 if (rtc->irq_task != NULL && task == NULL)
765 err = -EBUSY;
Chris Brand0734e272013-07-03 15:07:57 -0700766 else if (rtc->irq_task != task)
Alessandro Zummod691eb92007-10-16 01:28:15 -0700767 err = -EACCES;
Chris Brand0734e272013-07-03 15:07:57 -0700768 else {
John Stultz6610e082010-09-23 15:07:34 -0700769 rtc->irq_freq = freq;
Thomas Gleixner3c8bb90e2011-07-22 09:12:51 +0000770 if (rtc->pie_enabled && rtc_update_hrtimer(rtc, 1) < 0) {
771 spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
772 cpu_relax();
773 goto retry;
John Stultz6610e082010-09-23 15:07:34 -0700774 }
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800775 }
John Stultz6610e082010-09-23 15:07:34 -0700776 spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800777 return err;
778}
David Brownell2601a462006-11-25 11:09:27 -0800779EXPORT_SYMBOL_GPL(rtc_irq_set_freq);
John Stultz6610e082010-09-23 15:07:34 -0700780
781/**
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100782 * rtc_timer_enqueue - Adds a rtc_timer to the rtc_device timerqueue
John Stultz6610e082010-09-23 15:07:34 -0700783 * @rtc rtc device
784 * @timer timer being added.
785 *
786 * Enqueues a timer onto the rtc devices timerqueue and sets
787 * the next alarm event appropriately.
788 *
John Stultzaa0be0f2011-01-20 15:26:12 -0800789 * Sets the enabled bit on the added timer.
790 *
John Stultz6610e082010-09-23 15:07:34 -0700791 * Must hold ops_lock for proper serialization of timerqueue
792 */
John Stultzaa0be0f2011-01-20 15:26:12 -0800793static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer)
John Stultz6610e082010-09-23 15:07:34 -0700794{
John Stultzaa0be0f2011-01-20 15:26:12 -0800795 timer->enabled = 1;
John Stultz6610e082010-09-23 15:07:34 -0700796 timerqueue_add(&rtc->timerqueue, &timer->node);
797 if (&timer->node == timerqueue_getnext(&rtc->timerqueue)) {
798 struct rtc_wkalrm alarm;
799 int err;
800 alarm.time = rtc_ktime_to_tm(timer->node.expires);
801 alarm.enabled = 1;
802 err = __rtc_set_alarm(rtc, &alarm);
Zoran Markovic14d0e342013-06-26 16:09:13 -0700803 if (err == -ETIME) {
804 pm_stay_awake(rtc->dev.parent);
John Stultz6610e082010-09-23 15:07:34 -0700805 schedule_work(&rtc->irqwork);
Zoran Markovic14d0e342013-06-26 16:09:13 -0700806 } else if (err) {
John Stultzaa0be0f2011-01-20 15:26:12 -0800807 timerqueue_del(&rtc->timerqueue, &timer->node);
808 timer->enabled = 0;
809 return err;
810 }
John Stultz6610e082010-09-23 15:07:34 -0700811 }
John Stultzaa0be0f2011-01-20 15:26:12 -0800812 return 0;
John Stultz6610e082010-09-23 15:07:34 -0700813}
814
Rabin Vincent41c7f742011-11-22 11:03:14 +0100815static void rtc_alarm_disable(struct rtc_device *rtc)
816{
817 if (!rtc->ops || !rtc->ops->alarm_irq_enable)
818 return;
819
820 rtc->ops->alarm_irq_enable(rtc->dev.parent, false);
821}
822
John Stultz6610e082010-09-23 15:07:34 -0700823/**
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100824 * rtc_timer_remove - Removes a rtc_timer from the rtc_device timerqueue
John Stultz6610e082010-09-23 15:07:34 -0700825 * @rtc rtc device
826 * @timer timer being removed.
827 *
828 * Removes a timer onto the rtc devices timerqueue and sets
829 * the next alarm event appropriately.
830 *
John Stultzaa0be0f2011-01-20 15:26:12 -0800831 * Clears the enabled bit on the removed timer.
832 *
John Stultz6610e082010-09-23 15:07:34 -0700833 * Must hold ops_lock for proper serialization of timerqueue
834 */
John Stultzaa0be0f2011-01-20 15:26:12 -0800835static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer)
John Stultz6610e082010-09-23 15:07:34 -0700836{
837 struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue);
838 timerqueue_del(&rtc->timerqueue, &timer->node);
John Stultzaa0be0f2011-01-20 15:26:12 -0800839 timer->enabled = 0;
John Stultz6610e082010-09-23 15:07:34 -0700840 if (next == &timer->node) {
841 struct rtc_wkalrm alarm;
842 int err;
843 next = timerqueue_getnext(&rtc->timerqueue);
Rabin Vincent41c7f742011-11-22 11:03:14 +0100844 if (!next) {
845 rtc_alarm_disable(rtc);
John Stultz6610e082010-09-23 15:07:34 -0700846 return;
Rabin Vincent41c7f742011-11-22 11:03:14 +0100847 }
John Stultz6610e082010-09-23 15:07:34 -0700848 alarm.time = rtc_ktime_to_tm(next->expires);
849 alarm.enabled = 1;
850 err = __rtc_set_alarm(rtc, &alarm);
Zoran Markovic14d0e342013-06-26 16:09:13 -0700851 if (err == -ETIME) {
852 pm_stay_awake(rtc->dev.parent);
John Stultz6610e082010-09-23 15:07:34 -0700853 schedule_work(&rtc->irqwork);
Zoran Markovic14d0e342013-06-26 16:09:13 -0700854 }
John Stultz6610e082010-09-23 15:07:34 -0700855 }
856}
857
858/**
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100859 * rtc_timer_do_work - Expires rtc timers
John Stultz6610e082010-09-23 15:07:34 -0700860 * @rtc rtc device
861 * @timer timer being removed.
862 *
863 * Expires rtc timers. Reprograms next alarm event if needed.
864 * Called via worktask.
865 *
866 * Serializes access to timerqueue via ops_lock mutex
867 */
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100868void rtc_timer_do_work(struct work_struct *work)
John Stultz6610e082010-09-23 15:07:34 -0700869{
870 struct rtc_timer *timer;
871 struct timerqueue_node *next;
872 ktime_t now;
873 struct rtc_time tm;
874
875 struct rtc_device *rtc =
876 container_of(work, struct rtc_device, irqwork);
877
878 mutex_lock(&rtc->ops_lock);
879again:
880 __rtc_read_time(rtc, &tm);
881 now = rtc_tm_to_ktime(tm);
882 while ((next = timerqueue_getnext(&rtc->timerqueue))) {
883 if (next->expires.tv64 > now.tv64)
884 break;
885
886 /* expire timer */
887 timer = container_of(next, struct rtc_timer, node);
888 timerqueue_del(&rtc->timerqueue, &timer->node);
889 timer->enabled = 0;
890 if (timer->task.func)
891 timer->task.func(timer->task.private_data);
892
893 /* Re-add/fwd periodic timers */
894 if (ktime_to_ns(timer->period)) {
895 timer->node.expires = ktime_add(timer->node.expires,
896 timer->period);
897 timer->enabled = 1;
898 timerqueue_add(&rtc->timerqueue, &timer->node);
899 }
900 }
901
902 /* Set next alarm */
903 if (next) {
904 struct rtc_wkalrm alarm;
905 int err;
Xunlei Pang6528b882014-12-10 15:54:26 -0800906 int retry = 3;
907
John Stultz6610e082010-09-23 15:07:34 -0700908 alarm.time = rtc_ktime_to_tm(next->expires);
909 alarm.enabled = 1;
Xunlei Pang6528b882014-12-10 15:54:26 -0800910reprogram:
John Stultz6610e082010-09-23 15:07:34 -0700911 err = __rtc_set_alarm(rtc, &alarm);
912 if (err == -ETIME)
913 goto again;
Xunlei Pang6528b882014-12-10 15:54:26 -0800914 else if (err) {
915 if (retry-- > 0)
916 goto reprogram;
917
918 timer = container_of(next, struct rtc_timer, node);
919 timerqueue_del(&rtc->timerqueue, &timer->node);
920 timer->enabled = 0;
921 dev_err(&rtc->dev, "__rtc_set_alarm: err=%d\n", err);
922 goto again;
923 }
Rabin Vincent41c7f742011-11-22 11:03:14 +0100924 } else
925 rtc_alarm_disable(rtc);
John Stultz6610e082010-09-23 15:07:34 -0700926
Zoran Markovic14d0e342013-06-26 16:09:13 -0700927 pm_relax(rtc->dev.parent);
John Stultz6610e082010-09-23 15:07:34 -0700928 mutex_unlock(&rtc->ops_lock);
929}
930
931
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100932/* rtc_timer_init - Initializes an rtc_timer
John Stultz6610e082010-09-23 15:07:34 -0700933 * @timer: timer to be intiialized
934 * @f: function pointer to be called when timer fires
935 * @data: private data passed to function pointer
936 *
937 * Kernel interface to initializing an rtc_timer.
938 */
Sachin Kamat3ff2e132013-07-03 15:05:42 -0700939void rtc_timer_init(struct rtc_timer *timer, void (*f)(void *p), void *data)
John Stultz6610e082010-09-23 15:07:34 -0700940{
941 timerqueue_init(&timer->node);
942 timer->enabled = 0;
943 timer->task.func = f;
944 timer->task.private_data = data;
945}
946
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100947/* rtc_timer_start - Sets an rtc_timer to fire in the future
John Stultz6610e082010-09-23 15:07:34 -0700948 * @ rtc: rtc device to be used
949 * @ timer: timer being set
950 * @ expires: time at which to expire the timer
951 * @ period: period that the timer will recur
952 *
953 * Kernel interface to set an rtc_timer
954 */
Sachin Kamat3ff2e132013-07-03 15:05:42 -0700955int rtc_timer_start(struct rtc_device *rtc, struct rtc_timer *timer,
John Stultz6610e082010-09-23 15:07:34 -0700956 ktime_t expires, ktime_t period)
957{
958 int ret = 0;
959 mutex_lock(&rtc->ops_lock);
960 if (timer->enabled)
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100961 rtc_timer_remove(rtc, timer);
John Stultz6610e082010-09-23 15:07:34 -0700962
963 timer->node.expires = expires;
964 timer->period = period;
965
John Stultzaa0be0f2011-01-20 15:26:12 -0800966 ret = rtc_timer_enqueue(rtc, timer);
John Stultz6610e082010-09-23 15:07:34 -0700967
968 mutex_unlock(&rtc->ops_lock);
969 return ret;
970}
971
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100972/* rtc_timer_cancel - Stops an rtc_timer
John Stultz6610e082010-09-23 15:07:34 -0700973 * @ rtc: rtc device to be used
974 * @ timer: timer being set
975 *
976 * Kernel interface to cancel an rtc_timer
977 */
Sachin Kamat3ff2e132013-07-03 15:05:42 -0700978int rtc_timer_cancel(struct rtc_device *rtc, struct rtc_timer *timer)
John Stultz6610e082010-09-23 15:07:34 -0700979{
980 int ret = 0;
981 mutex_lock(&rtc->ops_lock);
982 if (timer->enabled)
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100983 rtc_timer_remove(rtc, timer);
John Stultz6610e082010-09-23 15:07:34 -0700984 mutex_unlock(&rtc->ops_lock);
985 return ret;
986}
987
988