2 * RTC subsystem, interface functions
4 * Copyright (C) 2005 Tower Technologies
5 * Author: Alessandro Zummo <a.zummo@towertech.it>
7 * based on arch/arm/common/rtctime.c
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.
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>
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
);
23 static int __rtc_read_time(struct rtc_device
*rtc
, struct rtc_time
*tm
)
28 else if (!rtc
->ops
->read_time
)
31 memset(tm
, 0, sizeof(struct rtc_time
));
32 err
= rtc
->ops
->read_time(rtc
->dev
.parent
, tm
);
34 dev_err(&rtc
->dev
, "read_time: fail to read\n");
38 err
= rtc_valid_tm(tm
);
40 dev_err(&rtc
->dev
, "read_time: rtc_time isn't valid\n");
45 int rtc_read_time(struct rtc_device
*rtc
, struct rtc_time
*tm
)
49 err
= mutex_lock_interruptible(&rtc
->ops_lock
);
53 err
= __rtc_read_time(rtc
, tm
);
54 mutex_unlock(&rtc
->ops_lock
);
57 EXPORT_SYMBOL_GPL(rtc_read_time
);
59 int rtc_set_time(struct rtc_device
*rtc
, struct rtc_time
*tm
)
63 err
= rtc_valid_tm(tm
);
67 err
= mutex_lock_interruptible(&rtc
->ops_lock
);
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
);
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
);
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
);
91 EXPORT_SYMBOL_GPL(rtc_set_time
);
93 int rtc_set_mmss(struct rtc_device
*rtc
, unsigned long secs
)
97 err
= mutex_lock_interruptible(&rtc
->ops_lock
);
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
;
110 err
= rtc
->ops
->read_time(rtc
->dev
.parent
, &old
);
112 rtc_time64_to_tm(secs
, &new);
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.
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
,
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
);
136 EXPORT_SYMBOL_GPL(rtc_set_mmss
);
138 static int rtc_read_alarm_internal(struct rtc_device
*rtc
, struct rtc_wkalrm
*alarm
)
142 err
= mutex_lock_interruptible(&rtc
->ops_lock
);
146 if (rtc
->ops
== NULL
)
148 else if (!rtc
->ops
->read_alarm
)
151 memset(alarm
, 0, sizeof(struct rtc_wkalrm
));
152 err
= rtc
->ops
->read_alarm(rtc
->dev
.parent
, alarm
);
155 mutex_unlock(&rtc
->ops_lock
);
159 int __rtc_read_alarm(struct rtc_device
*rtc
, struct rtc_wkalrm
*alarm
)
162 struct rtc_time before
, now
;
164 time64_t t_now
, t_alm
;
165 enum { none
, day
, month
, year
} missing
= none
;
168 /* The lower level RTC driver may return -1 in some fields,
169 * creating invalid alarm->time values, for reasons like:
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.
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.
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.
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
189 * Reading the alarm and timestamp in the reverse sequence
190 * would have the same race condition, and not solve the issue.
192 * So, we must first read the RTC timestamp,
193 * then read the RTC alarm value,
194 * and then read a second RTC timestamp.
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.
201 * So, when the two timestamps disagree, we just loop and do
202 * the process again to get a fully consistent set of values.
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..
209 /* Get the "before" timestamp */
210 err
= rtc_read_time(rtc
, &before
);
215 memcpy(&before
, &now
, sizeof(struct rtc_time
));
218 /* get the RTC alarm values, which may be incomplete */
219 err
= rtc_read_alarm_internal(rtc
, alarm
);
223 /* full-function RTCs won't have such missing fields */
224 if (rtc_valid_tm(&alarm
->time
) == 0)
227 /* get the "after" timestamp, to detect wrapped fields */
228 err
= rtc_read_time(rtc
, &now
);
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
);
238 /* Fill in the missing alarm fields using the timestamp; we
239 * know there's at least one since alarm->time is invalid.
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
;
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
;
253 if ((unsigned)alarm
->time
.tm_mon
>= 12) {
254 alarm
->time
.tm_mon
= now
.tm_mon
;
258 if (alarm
->time
.tm_year
== -1) {
259 alarm
->time
.tm_year
= now
.tm_year
;
264 /* with luck, no rollover is needed */
265 t_now
= rtc_tm_to_time64(&now
);
266 t_alm
= rtc_tm_to_time64(&alarm
->time
);
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.
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
);
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.
289 dev_dbg(&rtc
->dev
, "alarm rollover: %s\n", "month");
291 if (alarm
->time
.tm_mon
< 11)
292 alarm
->time
.tm_mon
++;
294 alarm
->time
.tm_mon
= 0;
295 alarm
->time
.tm_year
++;
297 days
= rtc_month_days(alarm
->time
.tm_mon
,
298 alarm
->time
.tm_year
);
299 } while (days
< alarm
->time
.tm_mday
);
302 /* Year rollover ... easy except for leap years! */
304 dev_dbg(&rtc
->dev
, "alarm rollover: %s\n", "year");
306 alarm
->time
.tm_year
++;
307 } while (!is_leap_year(alarm
->time
.tm_year
+ 1900)
308 && rtc_valid_tm(&alarm
->time
) != 0);
312 dev_warn(&rtc
->dev
, "alarm rollover not handled\n");
316 err
= rtc_valid_tm(&alarm
->time
);
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
,
328 int rtc_read_alarm(struct rtc_device
*rtc
, struct rtc_wkalrm
*alarm
)
332 err
= mutex_lock_interruptible(&rtc
->ops_lock
);
335 if (rtc
->ops
== NULL
)
337 else if (!rtc
->ops
->read_alarm
)
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
);
344 mutex_unlock(&rtc
->ops_lock
);
348 EXPORT_SYMBOL_GPL(rtc_read_alarm
);
350 static int __rtc_set_alarm(struct rtc_device
*rtc
, struct rtc_wkalrm
*alarm
)
353 time64_t now
, scheduled
;
356 err
= rtc_valid_tm(&alarm
->time
);
359 scheduled
= rtc_tm_to_time64(&alarm
->time
);
361 /* Make sure we're not setting alarms in the past */
362 err
= __rtc_read_time(rtc
, &tm
);
365 now
= rtc_tm_to_time64(&tm
);
366 if (scheduled
<= now
)
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.
377 else if (!rtc
->ops
->set_alarm
)
380 err
= rtc
->ops
->set_alarm(rtc
->dev
.parent
, alarm
);
385 int rtc_set_alarm(struct rtc_device
*rtc
, struct rtc_wkalrm
*alarm
)
389 err
= rtc_valid_tm(&alarm
->time
);
393 err
= mutex_lock_interruptible(&rtc
->ops_lock
);
396 if (rtc
->aie_timer
.enabled
)
397 rtc_timer_remove(rtc
, &rtc
->aie_timer
);
399 rtc
->aie_timer
.node
.expires
= rtc_tm_to_ktime(alarm
->time
);
400 rtc
->aie_timer
.period
= ktime_set(0, 0);
402 err
= rtc_timer_enqueue(rtc
, &rtc
->aie_timer
);
404 mutex_unlock(&rtc
->ops_lock
);
407 EXPORT_SYMBOL_GPL(rtc_set_alarm
);
409 /* Called once per device from rtc_device_register */
410 int rtc_initialize_alarm(struct rtc_device
*rtc
, struct rtc_wkalrm
*alarm
)
415 err
= rtc_valid_tm(&alarm
->time
);
419 err
= rtc_read_time(rtc
, &now
);
423 err
= mutex_lock_interruptible(&rtc
->ops_lock
);
427 rtc
->aie_timer
.node
.expires
= rtc_tm_to_ktime(alarm
->time
);
428 rtc
->aie_timer
.period
= ktime_set(0, 0);
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
)) {
434 rtc
->aie_timer
.enabled
= 1;
435 timerqueue_add(&rtc
->timerqueue
, &rtc
->aie_timer
.node
);
437 mutex_unlock(&rtc
->ops_lock
);
440 EXPORT_SYMBOL_GPL(rtc_initialize_alarm
);
444 int rtc_alarm_irq_enable(struct rtc_device
*rtc
, unsigned int enabled
)
446 int err
= mutex_lock_interruptible(&rtc
->ops_lock
);
450 if (rtc
->aie_timer
.enabled
!= enabled
) {
452 err
= rtc_timer_enqueue(rtc
, &rtc
->aie_timer
);
454 rtc_timer_remove(rtc
, &rtc
->aie_timer
);
461 else if (!rtc
->ops
->alarm_irq_enable
)
464 err
= rtc
->ops
->alarm_irq_enable(rtc
->dev
.parent
, enabled
);
466 mutex_unlock(&rtc
->ops_lock
);
469 EXPORT_SYMBOL_GPL(rtc_alarm_irq_enable
);
471 int rtc_update_irq_enable(struct rtc_device
*rtc
, unsigned int enabled
)
473 int err
= mutex_lock_interruptible(&rtc
->ops_lock
);
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);
483 /* make sure we're changing state */
484 if (rtc
->uie_rtctimer
.enabled
== enabled
)
487 if (rtc
->uie_unsupported
) {
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
);
503 rtc_timer_remove(rtc
, &rtc
->uie_rtctimer
);
506 mutex_unlock(&rtc
->ops_lock
);
507 #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
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.
515 err
= rtc_dev_update_irq_enable_emul(rtc
, enabled
);
520 EXPORT_SYMBOL_GPL(rtc_update_irq_enable
);
524 * rtc_handle_legacy_irq - AIE, UIE and PIE event hook
525 * @rtc: pointer to the rtc device
527 * This function is called when an AIE, UIE or PIE mode interrupt
528 * has occurred (or been emulated).
530 * Triggers the registered irq_task function callback.
532 void rtc_handle_legacy_irq(struct rtc_device
*rtc
, int num
, int mode
)
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
);
541 /* call the task func */
542 spin_lock_irqsave(&rtc
->irq_task_lock
, flags
);
544 rtc
->irq_task
->func(rtc
->irq_task
->private_data
);
545 spin_unlock_irqrestore(&rtc
->irq_task_lock
, flags
);
547 wake_up_interruptible(&rtc
->irq_queue
);
548 kill_fasync(&rtc
->async_queue
, SIGIO
, POLL_IN
);
553 * rtc_aie_update_irq - AIE mode rtctimer hook
554 * @private: pointer to the rtc_device
556 * This functions is called when the aie_timer expires.
558 void rtc_aie_update_irq(void *private)
560 struct rtc_device
*rtc
= (struct rtc_device
*)private;
561 rtc_handle_legacy_irq(rtc
, 1, RTC_AF
);
566 * rtc_uie_update_irq - UIE mode rtctimer hook
567 * @private: pointer to the rtc_device
569 * This functions is called when the uie_timer expires.
571 void rtc_uie_update_irq(void *private)
573 struct rtc_device
*rtc
= (struct rtc_device
*)private;
574 rtc_handle_legacy_irq(rtc
, 1, RTC_UF
);
579 * rtc_pie_update_irq - PIE mode hrtimer hook
580 * @timer: pointer to the pie mode hrtimer
582 * This function is used to emulate PIE mode interrupts
583 * using an hrtimer. This function is called when the periodic
586 enum hrtimer_restart
rtc_pie_update_irq(struct hrtimer
*timer
)
588 struct rtc_device
*rtc
;
591 rtc
= container_of(timer
, struct rtc_device
, pie_timer
);
593 period
= ktime_set(0, NSEC_PER_SEC
/rtc
->irq_freq
);
594 count
= hrtimer_forward_now(timer
, period
);
596 rtc_handle_legacy_irq(rtc
, count
, RTC_PF
);
598 return HRTIMER_RESTART
;
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
608 void rtc_update_irq(struct rtc_device
*rtc
,
609 unsigned long num
, unsigned long events
)
611 if (unlikely(IS_ERR_OR_NULL(rtc
)))
614 pm_stay_awake(rtc
->dev
.parent
);
615 schedule_work(&rtc
->irqwork
);
617 EXPORT_SYMBOL_GPL(rtc_update_irq
);
619 static int __rtc_match(struct device
*dev
, const void *data
)
621 const char *name
= data
;
623 if (strcmp(dev_name(dev
), name
) == 0)
628 struct rtc_device
*rtc_class_open(const char *name
)
631 struct rtc_device
*rtc
= NULL
;
633 dev
= class_find_device(rtc_class
, NULL
, name
, __rtc_match
);
635 rtc
= to_rtc_device(dev
);
638 if (!try_module_get(rtc
->owner
)) {
646 EXPORT_SYMBOL_GPL(rtc_class_open
);
648 void rtc_class_close(struct rtc_device
*rtc
)
650 module_put(rtc
->owner
);
651 put_device(&rtc
->dev
);
653 EXPORT_SYMBOL_GPL(rtc_class_close
);
655 int rtc_irq_register(struct rtc_device
*rtc
, struct rtc_task
*task
)
659 if (task
== NULL
|| task
->func
== NULL
)
662 /* Cannot register while the char dev is in use */
663 if (test_and_set_bit_lock(RTC_DEV_BUSY
, &rtc
->flags
))
666 spin_lock_irq(&rtc
->irq_task_lock
);
667 if (rtc
->irq_task
== NULL
) {
668 rtc
->irq_task
= task
;
671 spin_unlock_irq(&rtc
->irq_task_lock
);
673 clear_bit_unlock(RTC_DEV_BUSY
, &rtc
->flags
);
677 EXPORT_SYMBOL_GPL(rtc_irq_register
);
679 void rtc_irq_unregister(struct rtc_device
*rtc
, struct rtc_task
*task
)
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
);
686 EXPORT_SYMBOL_GPL(rtc_irq_unregister
);
688 static int rtc_update_hrtimer(struct rtc_device
*rtc
, int enabled
)
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.
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.
700 if (hrtimer_try_to_cancel(&rtc
->pie_timer
) < 0)
704 ktime_t period
= ktime_set(0, NSEC_PER_SEC
/ rtc
->irq_freq
);
706 hrtimer_start(&rtc
->pie_timer
, period
, HRTIMER_MODE_REL
);
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
718 * Note that rtc_irq_set_freq() should previously have been used to
719 * specify the desired frequency of periodic IRQ task->func() callbacks.
721 int rtc_irq_set_state(struct rtc_device
*rtc
, struct rtc_task
*task
, int enabled
)
727 spin_lock_irqsave(&rtc
->irq_task_lock
, flags
);
728 if (rtc
->irq_task
!= NULL
&& task
== NULL
)
730 else if (rtc
->irq_task
!= task
)
733 if (rtc_update_hrtimer(rtc
, enabled
) < 0) {
734 spin_unlock_irqrestore(&rtc
->irq_task_lock
, flags
);
738 rtc
->pie_enabled
= enabled
;
740 spin_unlock_irqrestore(&rtc
->irq_task_lock
, flags
);
743 EXPORT_SYMBOL_GPL(rtc_irq_set_state
);
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
752 * Note that rtc_irq_set_state() is used to enable or disable the
755 int rtc_irq_set_freq(struct rtc_device
*rtc
, struct rtc_task
*task
, int freq
)
760 if (freq
<= 0 || freq
> RTC_MAX_FREQ
)
763 spin_lock_irqsave(&rtc
->irq_task_lock
, flags
);
764 if (rtc
->irq_task
!= NULL
&& task
== NULL
)
766 else if (rtc
->irq_task
!= task
)
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
);
776 spin_unlock_irqrestore(&rtc
->irq_task_lock
, flags
);
779 EXPORT_SYMBOL_GPL(rtc_irq_set_freq
);
782 * rtc_timer_enqueue - Adds a rtc_timer to the rtc_device timerqueue
784 * @timer timer being added.
786 * Enqueues a timer onto the rtc devices timerqueue and sets
787 * the next alarm event appropriately.
789 * Sets the enabled bit on the added timer.
791 * Must hold ops_lock for proper serialization of timerqueue
793 static int rtc_timer_enqueue(struct rtc_device
*rtc
, struct rtc_timer
*timer
)
796 timerqueue_add(&rtc
->timerqueue
, &timer
->node
);
797 if (&timer
->node
== timerqueue_getnext(&rtc
->timerqueue
)) {
798 struct rtc_wkalrm alarm
;
800 alarm
.time
= rtc_ktime_to_tm(timer
->node
.expires
);
802 err
= __rtc_set_alarm(rtc
, &alarm
);
804 pm_stay_awake(rtc
->dev
.parent
);
805 schedule_work(&rtc
->irqwork
);
807 timerqueue_del(&rtc
->timerqueue
, &timer
->node
);
815 static void rtc_alarm_disable(struct rtc_device
*rtc
)
817 if (!rtc
->ops
|| !rtc
->ops
->alarm_irq_enable
)
820 rtc
->ops
->alarm_irq_enable(rtc
->dev
.parent
, false);
824 * rtc_timer_remove - Removes a rtc_timer from the rtc_device timerqueue
826 * @timer timer being removed.
828 * Removes a timer onto the rtc devices timerqueue and sets
829 * the next alarm event appropriately.
831 * Clears the enabled bit on the removed timer.
833 * Must hold ops_lock for proper serialization of timerqueue
835 static void rtc_timer_remove(struct rtc_device
*rtc
, struct rtc_timer
*timer
)
837 struct timerqueue_node
*next
= timerqueue_getnext(&rtc
->timerqueue
);
838 timerqueue_del(&rtc
->timerqueue
, &timer
->node
);
840 if (next
== &timer
->node
) {
841 struct rtc_wkalrm alarm
;
843 next
= timerqueue_getnext(&rtc
->timerqueue
);
845 rtc_alarm_disable(rtc
);
848 alarm
.time
= rtc_ktime_to_tm(next
->expires
);
850 err
= __rtc_set_alarm(rtc
, &alarm
);
852 pm_stay_awake(rtc
->dev
.parent
);
853 schedule_work(&rtc
->irqwork
);
859 * rtc_timer_do_work - Expires rtc timers
861 * @timer timer being removed.
863 * Expires rtc timers. Reprograms next alarm event if needed.
864 * Called via worktask.
866 * Serializes access to timerqueue via ops_lock mutex
868 void rtc_timer_do_work(struct work_struct
*work
)
870 struct rtc_timer
*timer
;
871 struct timerqueue_node
*next
;
875 struct rtc_device
*rtc
=
876 container_of(work
, struct rtc_device
, irqwork
);
878 mutex_lock(&rtc
->ops_lock
);
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
)
887 timer
= container_of(next
, struct rtc_timer
, node
);
888 timerqueue_del(&rtc
->timerqueue
, &timer
->node
);
890 if (timer
->task
.func
)
891 timer
->task
.func(timer
->task
.private_data
);
893 /* Re-add/fwd periodic timers */
894 if (ktime_to_ns(timer
->period
)) {
895 timer
->node
.expires
= ktime_add(timer
->node
.expires
,
898 timerqueue_add(&rtc
->timerqueue
, &timer
->node
);
904 struct rtc_wkalrm alarm
;
908 alarm
.time
= rtc_ktime_to_tm(next
->expires
);
911 err
= __rtc_set_alarm(rtc
, &alarm
);
918 timer
= container_of(next
, struct rtc_timer
, node
);
919 timerqueue_del(&rtc
->timerqueue
, &timer
->node
);
921 dev_err(&rtc
->dev
, "__rtc_set_alarm: err=%d\n", err
);
925 rtc_alarm_disable(rtc
);
927 pm_relax(rtc
->dev
.parent
);
928 mutex_unlock(&rtc
->ops_lock
);
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
937 * Kernel interface to initializing an rtc_timer.
939 void rtc_timer_init(struct rtc_timer
*timer
, void (*f
)(void *p
), void *data
)
941 timerqueue_init(&timer
->node
);
943 timer
->task
.func
= f
;
944 timer
->task
.private_data
= data
;
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
953 * Kernel interface to set an rtc_timer
955 int rtc_timer_start(struct rtc_device
*rtc
, struct rtc_timer
*timer
,
956 ktime_t expires
, ktime_t period
)
959 mutex_lock(&rtc
->ops_lock
);
961 rtc_timer_remove(rtc
, timer
);
963 timer
->node
.expires
= expires
;
964 timer
->period
= period
;
966 ret
= rtc_timer_enqueue(rtc
, timer
);
968 mutex_unlock(&rtc
->ops_lock
);
972 /* rtc_timer_cancel - Stops an rtc_timer
973 * @ rtc: rtc device to be used
974 * @ timer: timer being set
976 * Kernel interface to cancel an rtc_timer
978 int rtc_timer_cancel(struct rtc_device
*rtc
, struct rtc_timer
*timer
)
981 mutex_lock(&rtc
->ops_lock
);
983 rtc_timer_remove(rtc
, timer
);
984 mutex_unlock(&rtc
->ops_lock
);
This page took 0.080093 seconds and 6 git commands to generate.