Merge branch 'x86-cpu-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-block.git] / drivers / rtc / interface.c
1 /*
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>
15 #include <linux/sched.h>
16 #include <linux/module.h>
17 #include <linux/log2.h>
18 #include <linux/workqueue.h>
19
20 static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer);
21 static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer);
22
23 static 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);
33                 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");
41         }
42         return err;
43 }
44
45 int rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm)
46 {
47         int err;
48
49         err = mutex_lock_interruptible(&rtc->ops_lock);
50         if (err)
51                 return err;
52
53         err = __rtc_read_time(rtc, tm);
54         mutex_unlock(&rtc->ops_lock);
55         return err;
56 }
57 EXPORT_SYMBOL_GPL(rtc_read_time);
58
59 int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm)
60 {
61         int err;
62
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)
69                 return err;
70
71         if (!rtc->ops)
72                 err = -ENODEV;
73         else if (rtc->ops->set_time)
74                 err = rtc->ops->set_time(rtc->dev.parent, tm);
75         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) {
80                 time64_t secs64 = rtc_tm_to_time64(tm);
81                 err = rtc->ops->set_mmss(rtc->dev.parent, secs64);
82         } else
83                 err = -EINVAL;
84
85         pm_stay_awake(rtc->dev.parent);
86         mutex_unlock(&rtc->ops_lock);
87         /* A timer might have just expired */
88         schedule_work(&rtc->irqwork);
89         return err;
90 }
91 EXPORT_SYMBOL_GPL(rtc_set_time);
92
93 int rtc_set_mmss(struct rtc_device *rtc, unsigned long secs)
94 {
95         int err;
96
97         err = mutex_lock_interruptible(&rtc->ops_lock);
98         if (err)
99                 return err;
100
101         if (!rtc->ops)
102                 err = -ENODEV;
103         else if (rtc->ops->set_mmss64)
104                 err = rtc->ops->set_mmss64(rtc->dev.parent, secs);
105         else if (rtc->ops->set_mmss)
106                 err = rtc->ops->set_mmss(rtc->dev.parent, secs);
107         else if (rtc->ops->read_time && rtc->ops->set_time) {
108                 struct rtc_time new, old;
109
110                 err = rtc->ops->read_time(rtc->dev.parent, &old);
111                 if (err == 0) {
112                         rtc_time64_to_tm(secs, &new);
113
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)))
122                                 err = rtc->ops->set_time(rtc->dev.parent,
123                                                 &new);
124                 }
125         } else {
126                 err = -EINVAL;
127         }
128
129         pm_stay_awake(rtc->dev.parent);
130         mutex_unlock(&rtc->ops_lock);
131         /* A timer might have just expired */
132         schedule_work(&rtc->irqwork);
133
134         return err;
135 }
136 EXPORT_SYMBOL_GPL(rtc_set_mmss);
137
138 static 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
159 int __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;
164         time64_t t_now, t_alm;
165         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 */
249         if (alarm->time.tm_mday < 1 || alarm->time.tm_mday > 31) {
250                 alarm->time.tm_mday = now.tm_mday;
251                 missing = day;
252         }
253         if ((unsigned)alarm->time.tm_mon >= 12) {
254                 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 */
265         t_now = rtc_tm_to_time64(&now);
266         t_alm = rtc_tm_to_time64(&alarm->time);
267         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;
280                 rtc_time64_to_tm(t_alm, &alarm->time);
281                 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++;
307                 } while (!is_leap_year(alarm->time.tm_year + 1900)
308                         && rtc_valid_tm(&alarm->time) != 0);
309                 break;
310
311         default:
312                 dev_warn(&rtc->dev, "alarm rollover not handled\n");
313         }
314
315 done:
316         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;
326 }
327
328 int rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
329 {
330         int err;
331
332         err = mutex_lock_interruptible(&rtc->ops_lock);
333         if (err)
334                 return err;
335         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;
342                 alarm->time = rtc_ktime_to_tm(rtc->aie_timer.node.expires);
343         }
344         mutex_unlock(&rtc->ops_lock);
345
346         return err;
347 }
348 EXPORT_SYMBOL_GPL(rtc_read_alarm);
349
350 static int __rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
351 {
352         struct rtc_time tm;
353         time64_t now, scheduled;
354         int err;
355
356         err = rtc_valid_tm(&alarm->time);
357         if (err)
358                 return err;
359         scheduled = rtc_tm_to_time64(&alarm->time);
360
361         /* Make sure we're not setting alarms in the past */
362         err = __rtc_read_time(rtc, &tm);
363         if (err)
364                 return err;
365         now = rtc_tm_to_time64(&tm);
366         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
375         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;
383 }
384
385 int rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
386 {
387         int err;
388
389         err = rtc_valid_tm(&alarm->time);
390         if (err != 0)
391                 return err;
392
393         err = mutex_lock_interruptible(&rtc->ops_lock);
394         if (err)
395                 return err;
396         if (rtc->aie_timer.enabled)
397                 rtc_timer_remove(rtc, &rtc->aie_timer);
398
399         rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time);
400         rtc->aie_timer.period = ktime_set(0, 0);
401         if (alarm->enabled)
402                 err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
403
404         mutex_unlock(&rtc->ops_lock);
405         return err;
406 }
407 EXPORT_SYMBOL_GPL(rtc_set_alarm);
408
409 /* Called once per device from rtc_device_register */
410 int rtc_initialize_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
411 {
412         int err;
413         struct rtc_time now;
414
415         err = rtc_valid_tm(&alarm->time);
416         if (err != 0)
417                 return err;
418
419         err = rtc_read_time(rtc, &now);
420         if (err)
421                 return err;
422
423         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);
429
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
434                 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 }
440 EXPORT_SYMBOL_GPL(rtc_initialize_alarm);
441
442
443
444 int 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
450         if (rtc->aie_timer.enabled != enabled) {
451                 if (enabled)
452                         err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
453                 else
454                         rtc_timer_remove(rtc, &rtc->aie_timer);
455         }
456
457         if (err)
458                 /* nothing */;
459         else if (!rtc->ops)
460                 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 }
469 EXPORT_SYMBOL_GPL(rtc_alarm_irq_enable);
470
471 int 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
477 #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
483         /* make sure we're changing state */
484         if (rtc->uie_rtctimer.enabled == enabled)
485                 goto out;
486
487         if (rtc->uie_unsupported) {
488                 err = -EINVAL;
489                 goto out;
490         }
491
492         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);
501                 err = rtc_timer_enqueue(rtc, &rtc->uie_rtctimer);
502         } else
503                 rtc_timer_remove(rtc, &rtc->uie_rtctimer);
504
505 out:
506         mutex_unlock(&rtc->ops_lock);
507 #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
517         return err;
518
519 }
520 EXPORT_SYMBOL_GPL(rtc_update_irq_enable);
521
522
523 /**
524  * 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
528  * has occurred (or been emulated).
529  *
530  * Triggers the registered irq_task function callback.
531  */
532 void rtc_handle_legacy_irq(struct rtc_device *rtc, int num, int mode)
533 {
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  */
558 void 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  */
571 void 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  */
586 enum 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.
603  * @rtc: the rtc device
604  * @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
606  * Context: any
607  */
608 void rtc_update_irq(struct rtc_device *rtc,
609                 unsigned long num, unsigned long events)
610 {
611         if (unlikely(IS_ERR_OR_NULL(rtc)))
612                 return;
613
614         pm_stay_awake(rtc->dev.parent);
615         schedule_work(&rtc->irqwork);
616 }
617 EXPORT_SYMBOL_GPL(rtc_update_irq);
618
619 static int __rtc_match(struct device *dev, const void *data)
620 {
621         const char *name = data;
622
623         if (strcmp(dev_name(dev), name) == 0)
624                 return 1;
625         return 0;
626 }
627
628 struct rtc_device *rtc_class_open(const char *name)
629 {
630         struct device *dev;
631         struct rtc_device *rtc = NULL;
632
633         dev = class_find_device(rtc_class, NULL, name, __rtc_match);
634         if (dev)
635                 rtc = to_rtc_device(dev);
636
637         if (rtc) {
638                 if (!try_module_get(rtc->owner)) {
639                         put_device(dev);
640                         rtc = NULL;
641                 }
642         }
643
644         return rtc;
645 }
646 EXPORT_SYMBOL_GPL(rtc_class_open);
647
648 void rtc_class_close(struct rtc_device *rtc)
649 {
650         module_put(rtc->owner);
651         put_device(&rtc->dev);
652 }
653 EXPORT_SYMBOL_GPL(rtc_class_close);
654
655 int rtc_irq_register(struct rtc_device *rtc, struct rtc_task *task)
656 {
657         int retval = -EBUSY;
658
659         if (task == NULL || task->func == NULL)
660                 return -EINVAL;
661
662         /* Cannot register while the char dev is in use */
663         if (test_and_set_bit_lock(RTC_DEV_BUSY, &rtc->flags))
664                 return -EBUSY;
665
666         spin_lock_irq(&rtc->irq_task_lock);
667         if (rtc->irq_task == NULL) {
668                 rtc->irq_task = task;
669                 retval = 0;
670         }
671         spin_unlock_irq(&rtc->irq_task_lock);
672
673         clear_bit_unlock(RTC_DEV_BUSY, &rtc->flags);
674
675         return retval;
676 }
677 EXPORT_SYMBOL_GPL(rtc_irq_register);
678
679 void rtc_irq_unregister(struct rtc_device *rtc, struct rtc_task *task)
680 {
681         spin_lock_irq(&rtc->irq_task_lock);
682         if (rtc->irq_task == task)
683                 rtc->irq_task = NULL;
684         spin_unlock_irq(&rtc->irq_task_lock);
685 }
686 EXPORT_SYMBOL_GPL(rtc_irq_unregister);
687
688 static 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
711 /**
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  */
721 int rtc_irq_set_state(struct rtc_device *rtc, struct rtc_task *task, int enabled)
722 {
723         int err = 0;
724         unsigned long flags;
725
726 retry:
727         spin_lock_irqsave(&rtc->irq_task_lock, flags);
728         if (rtc->irq_task != NULL && task == NULL)
729                 err = -EBUSY;
730         else if (rtc->irq_task != task)
731                 err = -EACCES;
732         else {
733                 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;
739         }
740         spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
741         return err;
742 }
743 EXPORT_SYMBOL_GPL(rtc_irq_set_state);
744
745 /**
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  */
755 int rtc_irq_set_freq(struct rtc_device *rtc, struct rtc_task *task, int freq)
756 {
757         int err = 0;
758         unsigned long flags;
759
760         if (freq <= 0 || freq > RTC_MAX_FREQ)
761                 return -EINVAL;
762 retry:
763         spin_lock_irqsave(&rtc->irq_task_lock, flags);
764         if (rtc->irq_task != NULL && task == NULL)
765                 err = -EBUSY;
766         else if (rtc->irq_task != task)
767                 err = -EACCES;
768         else {
769                 rtc->irq_freq = freq;
770                 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;
774                 }
775         }
776         spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
777         return err;
778 }
779 EXPORT_SYMBOL_GPL(rtc_irq_set_freq);
780
781 /**
782  * rtc_timer_enqueue - Adds a rtc_timer to the rtc_device timerqueue
783  * @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  *
789  * Sets the enabled bit on the added timer.
790  *
791  * Must hold ops_lock for proper serialization of timerqueue
792  */
793 static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer)
794 {
795         timer->enabled = 1;
796         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);
803                 if (err == -ETIME) {
804                         pm_stay_awake(rtc->dev.parent);
805                         schedule_work(&rtc->irqwork);
806                 } else if (err) {
807                         timerqueue_del(&rtc->timerqueue, &timer->node);
808                         timer->enabled = 0;
809                         return err;
810                 }
811         }
812         return 0;
813 }
814
815 static 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
823 /**
824  * rtc_timer_remove - Removes a rtc_timer from the rtc_device timerqueue
825  * @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  *
831  * Clears the enabled bit on the removed timer.
832  *
833  * Must hold ops_lock for proper serialization of timerqueue
834  */
835 static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer)
836 {
837         struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue);
838         timerqueue_del(&rtc->timerqueue, &timer->node);
839         timer->enabled = 0;
840         if (next == &timer->node) {
841                 struct rtc_wkalrm alarm;
842                 int err;
843                 next = timerqueue_getnext(&rtc->timerqueue);
844                 if (!next) {
845                         rtc_alarm_disable(rtc);
846                         return;
847                 }
848                 alarm.time = rtc_ktime_to_tm(next->expires);
849                 alarm.enabled = 1;
850                 err = __rtc_set_alarm(rtc, &alarm);
851                 if (err == -ETIME) {
852                         pm_stay_awake(rtc->dev.parent);
853                         schedule_work(&rtc->irqwork);
854                 }
855         }
856 }
857
858 /**
859  * rtc_timer_do_work - Expires rtc timers
860  * @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  */
868 void rtc_timer_do_work(struct work_struct *work)
869 {
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);
879 again:
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;
906                 int retry = 3;
907
908                 alarm.time = rtc_ktime_to_tm(next->expires);
909                 alarm.enabled = 1;
910 reprogram:
911                 err = __rtc_set_alarm(rtc, &alarm);
912                 if (err == -ETIME)
913                         goto again;
914                 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                 }
924         } else
925                 rtc_alarm_disable(rtc);
926
927         pm_relax(rtc->dev.parent);
928         mutex_unlock(&rtc->ops_lock);
929 }
930
931
932 /* rtc_timer_init - Initializes an rtc_timer
933  * @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  */
939 void rtc_timer_init(struct rtc_timer *timer, void (*f)(void *p), void *data)
940 {
941         timerqueue_init(&timer->node);
942         timer->enabled = 0;
943         timer->task.func = f;
944         timer->task.private_data = data;
945 }
946
947 /* rtc_timer_start - Sets an rtc_timer to fire in the future
948  * @ 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  */
955 int rtc_timer_start(struct rtc_device *rtc, struct rtc_timer *timer,
956                         ktime_t expires, ktime_t period)
957 {
958         int ret = 0;
959         mutex_lock(&rtc->ops_lock);
960         if (timer->enabled)
961                 rtc_timer_remove(rtc, timer);
962
963         timer->node.expires = expires;
964         timer->period = period;
965
966         ret = rtc_timer_enqueue(rtc, timer);
967
968         mutex_unlock(&rtc->ops_lock);
969         return ret;
970 }
971
972 /* rtc_timer_cancel - Stops an rtc_timer
973  * @ rtc: rtc device to be used
974  * @ timer: timer being set
975  *
976  * Kernel interface to cancel an rtc_timer
977  */
978 int rtc_timer_cancel(struct rtc_device *rtc, struct rtc_timer *timer)
979 {
980         int ret = 0;
981         mutex_lock(&rtc->ops_lock);
982         if (timer->enabled)
983                 rtc_timer_remove(rtc, timer);
984         mutex_unlock(&rtc->ops_lock);
985         return ret;
986 }
987
988