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_mmss
) {
76 time64_t secs64
= rtc_tm_to_time64(tm
);
77 err
= rtc
->ops
->set_mmss(rtc
->dev
.parent
, secs64
);
81 pm_stay_awake(rtc
->dev
.parent
);
82 mutex_unlock(&rtc
->ops_lock
);
83 /* A timer might have just expired */
84 schedule_work(&rtc
->irqwork
);
87 EXPORT_SYMBOL_GPL(rtc_set_time
);
89 int rtc_set_mmss(struct rtc_device
*rtc
, unsigned long secs
)
93 err
= mutex_lock_interruptible(&rtc
->ops_lock
);
99 else if (rtc
->ops
->set_mmss
)
100 err
= rtc
->ops
->set_mmss(rtc
->dev
.parent
, secs
);
101 else if (rtc
->ops
->read_time
&& rtc
->ops
->set_time
) {
102 struct rtc_time
new, old
;
104 err
= rtc
->ops
->read_time(rtc
->dev
.parent
, &old
);
106 rtc_time64_to_tm(secs
, &new);
109 * avoid writing when we're going to change the day of
110 * the month. We will retry in the next minute. This
111 * basically means that if the RTC must not drift
112 * by more than 1 minute in 11 minutes.
114 if (!((old
.tm_hour
== 23 && old
.tm_min
== 59) ||
115 (new.tm_hour
== 23 && new.tm_min
== 59)))
116 err
= rtc
->ops
->set_time(rtc
->dev
.parent
,
123 pm_stay_awake(rtc
->dev
.parent
);
124 mutex_unlock(&rtc
->ops_lock
);
125 /* A timer might have just expired */
126 schedule_work(&rtc
->irqwork
);
130 EXPORT_SYMBOL_GPL(rtc_set_mmss
);
132 static int rtc_read_alarm_internal(struct rtc_device
*rtc
, struct rtc_wkalrm
*alarm
)
136 err
= mutex_lock_interruptible(&rtc
->ops_lock
);
140 if (rtc
->ops
== NULL
)
142 else if (!rtc
->ops
->read_alarm
)
145 memset(alarm
, 0, sizeof(struct rtc_wkalrm
));
146 err
= rtc
->ops
->read_alarm(rtc
->dev
.parent
, alarm
);
149 mutex_unlock(&rtc
->ops_lock
);
153 int __rtc_read_alarm(struct rtc_device
*rtc
, struct rtc_wkalrm
*alarm
)
156 struct rtc_time before
, now
;
158 time64_t t_now
, t_alm
;
159 enum { none
, day
, month
, year
} missing
= none
;
162 /* The lower level RTC driver may return -1 in some fields,
163 * creating invalid alarm->time values, for reasons like:
165 * - The hardware may not be capable of filling them in;
166 * many alarms match only on time-of-day fields, not
167 * day/month/year calendar data.
169 * - Some hardware uses illegal values as "wildcard" match
170 * values, which non-Linux firmware (like a BIOS) may try
171 * to set up as e.g. "alarm 15 minutes after each hour".
172 * Linux uses only oneshot alarms.
174 * When we see that here, we deal with it by using values from
175 * a current RTC timestamp for any missing (-1) values. The
176 * RTC driver prevents "periodic alarm" modes.
178 * But this can be racey, because some fields of the RTC timestamp
179 * may have wrapped in the interval since we read the RTC alarm,
180 * which would lead to us inserting inconsistent values in place
183 * Reading the alarm and timestamp in the reverse sequence
184 * would have the same race condition, and not solve the issue.
186 * So, we must first read the RTC timestamp,
187 * then read the RTC alarm value,
188 * and then read a second RTC timestamp.
190 * If any fields of the second timestamp have changed
191 * when compared with the first timestamp, then we know
192 * our timestamp may be inconsistent with that used by
193 * the low-level rtc_read_alarm_internal() function.
195 * So, when the two timestamps disagree, we just loop and do
196 * the process again to get a fully consistent set of values.
198 * This could all instead be done in the lower level driver,
199 * but since more than one lower level RTC implementation needs it,
200 * then it's probably best best to do it here instead of there..
203 /* Get the "before" timestamp */
204 err
= rtc_read_time(rtc
, &before
);
209 memcpy(&before
, &now
, sizeof(struct rtc_time
));
212 /* get the RTC alarm values, which may be incomplete */
213 err
= rtc_read_alarm_internal(rtc
, alarm
);
217 /* full-function RTCs won't have such missing fields */
218 if (rtc_valid_tm(&alarm
->time
) == 0)
221 /* get the "after" timestamp, to detect wrapped fields */
222 err
= rtc_read_time(rtc
, &now
);
226 /* note that tm_sec is a "don't care" value here: */
227 } while ( before
.tm_min
!= now
.tm_min
228 || before
.tm_hour
!= now
.tm_hour
229 || before
.tm_mon
!= now
.tm_mon
230 || before
.tm_year
!= now
.tm_year
);
232 /* Fill in the missing alarm fields using the timestamp; we
233 * know there's at least one since alarm->time is invalid.
235 if (alarm
->time
.tm_sec
== -1)
236 alarm
->time
.tm_sec
= now
.tm_sec
;
237 if (alarm
->time
.tm_min
== -1)
238 alarm
->time
.tm_min
= now
.tm_min
;
239 if (alarm
->time
.tm_hour
== -1)
240 alarm
->time
.tm_hour
= now
.tm_hour
;
242 /* For simplicity, only support date rollover for now */
243 if (alarm
->time
.tm_mday
< 1 || alarm
->time
.tm_mday
> 31) {
244 alarm
->time
.tm_mday
= now
.tm_mday
;
247 if ((unsigned)alarm
->time
.tm_mon
>= 12) {
248 alarm
->time
.tm_mon
= now
.tm_mon
;
252 if (alarm
->time
.tm_year
== -1) {
253 alarm
->time
.tm_year
= now
.tm_year
;
258 /* with luck, no rollover is needed */
259 t_now
= rtc_tm_to_time64(&now
);
260 t_alm
= rtc_tm_to_time64(&alarm
->time
);
266 /* 24 hour rollover ... if it's now 10am Monday, an alarm that
267 * that will trigger at 5am will do so at 5am Tuesday, which
268 * could also be in the next month or year. This is a common
269 * case, especially for PCs.
272 dev_dbg(&rtc
->dev
, "alarm rollover: %s\n", "day");
273 t_alm
+= 24 * 60 * 60;
274 rtc_time64_to_tm(t_alm
, &alarm
->time
);
277 /* Month rollover ... if it's the 31th, an alarm on the 3rd will
278 * be next month. An alarm matching on the 30th, 29th, or 28th
279 * may end up in the month after that! Many newer PCs support
280 * this type of alarm.
283 dev_dbg(&rtc
->dev
, "alarm rollover: %s\n", "month");
285 if (alarm
->time
.tm_mon
< 11)
286 alarm
->time
.tm_mon
++;
288 alarm
->time
.tm_mon
= 0;
289 alarm
->time
.tm_year
++;
291 days
= rtc_month_days(alarm
->time
.tm_mon
,
292 alarm
->time
.tm_year
);
293 } while (days
< alarm
->time
.tm_mday
);
296 /* Year rollover ... easy except for leap years! */
298 dev_dbg(&rtc
->dev
, "alarm rollover: %s\n", "year");
300 alarm
->time
.tm_year
++;
301 } while (!is_leap_year(alarm
->time
.tm_year
+ 1900)
302 && rtc_valid_tm(&alarm
->time
) != 0);
306 dev_warn(&rtc
->dev
, "alarm rollover not handled\n");
310 err
= rtc_valid_tm(&alarm
->time
);
313 dev_warn(&rtc
->dev
, "invalid alarm value: %d-%d-%d %d:%d:%d\n",
314 alarm
->time
.tm_year
+ 1900, alarm
->time
.tm_mon
+ 1,
315 alarm
->time
.tm_mday
, alarm
->time
.tm_hour
, alarm
->time
.tm_min
,
322 int rtc_read_alarm(struct rtc_device
*rtc
, struct rtc_wkalrm
*alarm
)
326 err
= mutex_lock_interruptible(&rtc
->ops_lock
);
329 if (rtc
->ops
== NULL
)
331 else if (!rtc
->ops
->read_alarm
)
334 memset(alarm
, 0, sizeof(struct rtc_wkalrm
));
335 alarm
->enabled
= rtc
->aie_timer
.enabled
;
336 alarm
->time
= rtc_ktime_to_tm(rtc
->aie_timer
.node
.expires
);
338 mutex_unlock(&rtc
->ops_lock
);
342 EXPORT_SYMBOL_GPL(rtc_read_alarm
);
344 static int __rtc_set_alarm(struct rtc_device
*rtc
, struct rtc_wkalrm
*alarm
)
347 time64_t now
, scheduled
;
350 err
= rtc_valid_tm(&alarm
->time
);
353 scheduled
= rtc_tm_to_time64(&alarm
->time
);
355 /* Make sure we're not setting alarms in the past */
356 err
= __rtc_read_time(rtc
, &tm
);
359 now
= rtc_tm_to_time64(&tm
);
360 if (scheduled
<= now
)
363 * XXX - We just checked to make sure the alarm time is not
364 * in the past, but there is still a race window where if
365 * the is alarm set for the next second and the second ticks
366 * over right here, before we set the alarm.
371 else if (!rtc
->ops
->set_alarm
)
374 err
= rtc
->ops
->set_alarm(rtc
->dev
.parent
, alarm
);
379 int rtc_set_alarm(struct rtc_device
*rtc
, struct rtc_wkalrm
*alarm
)
383 err
= rtc_valid_tm(&alarm
->time
);
387 err
= mutex_lock_interruptible(&rtc
->ops_lock
);
390 if (rtc
->aie_timer
.enabled
)
391 rtc_timer_remove(rtc
, &rtc
->aie_timer
);
393 rtc
->aie_timer
.node
.expires
= rtc_tm_to_ktime(alarm
->time
);
394 rtc
->aie_timer
.period
= ktime_set(0, 0);
396 err
= rtc_timer_enqueue(rtc
, &rtc
->aie_timer
);
398 mutex_unlock(&rtc
->ops_lock
);
401 EXPORT_SYMBOL_GPL(rtc_set_alarm
);
403 /* Called once per device from rtc_device_register */
404 int rtc_initialize_alarm(struct rtc_device
*rtc
, struct rtc_wkalrm
*alarm
)
409 err
= rtc_valid_tm(&alarm
->time
);
413 err
= rtc_read_time(rtc
, &now
);
417 err
= mutex_lock_interruptible(&rtc
->ops_lock
);
421 rtc
->aie_timer
.node
.expires
= rtc_tm_to_ktime(alarm
->time
);
422 rtc
->aie_timer
.period
= ktime_set(0, 0);
424 /* Alarm has to be enabled & in the futrure for us to enqueue it */
425 if (alarm
->enabled
&& (rtc_tm_to_ktime(now
).tv64
<
426 rtc
->aie_timer
.node
.expires
.tv64
)) {
428 rtc
->aie_timer
.enabled
= 1;
429 timerqueue_add(&rtc
->timerqueue
, &rtc
->aie_timer
.node
);
431 mutex_unlock(&rtc
->ops_lock
);
434 EXPORT_SYMBOL_GPL(rtc_initialize_alarm
);
438 int rtc_alarm_irq_enable(struct rtc_device
*rtc
, unsigned int enabled
)
440 int err
= mutex_lock_interruptible(&rtc
->ops_lock
);
444 if (rtc
->aie_timer
.enabled
!= enabled
) {
446 err
= rtc_timer_enqueue(rtc
, &rtc
->aie_timer
);
448 rtc_timer_remove(rtc
, &rtc
->aie_timer
);
455 else if (!rtc
->ops
->alarm_irq_enable
)
458 err
= rtc
->ops
->alarm_irq_enable(rtc
->dev
.parent
, enabled
);
460 mutex_unlock(&rtc
->ops_lock
);
463 EXPORT_SYMBOL_GPL(rtc_alarm_irq_enable
);
465 int rtc_update_irq_enable(struct rtc_device
*rtc
, unsigned int enabled
)
467 int err
= mutex_lock_interruptible(&rtc
->ops_lock
);
471 #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
472 if (enabled
== 0 && rtc
->uie_irq_active
) {
473 mutex_unlock(&rtc
->ops_lock
);
474 return rtc_dev_update_irq_enable_emul(rtc
, 0);
477 /* make sure we're changing state */
478 if (rtc
->uie_rtctimer
.enabled
== enabled
)
481 if (rtc
->uie_unsupported
) {
490 __rtc_read_time(rtc
, &tm
);
491 onesec
= ktime_set(1, 0);
492 now
= rtc_tm_to_ktime(tm
);
493 rtc
->uie_rtctimer
.node
.expires
= ktime_add(now
, onesec
);
494 rtc
->uie_rtctimer
.period
= ktime_set(1, 0);
495 err
= rtc_timer_enqueue(rtc
, &rtc
->uie_rtctimer
);
497 rtc_timer_remove(rtc
, &rtc
->uie_rtctimer
);
500 mutex_unlock(&rtc
->ops_lock
);
501 #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
503 * Enable emulation if the driver did not provide
504 * the update_irq_enable function pointer or if returned
505 * -EINVAL to signal that it has been configured without
506 * interrupts or that are not available at the moment.
509 err
= rtc_dev_update_irq_enable_emul(rtc
, enabled
);
514 EXPORT_SYMBOL_GPL(rtc_update_irq_enable
);
518 * rtc_handle_legacy_irq - AIE, UIE and PIE event hook
519 * @rtc: pointer to the rtc device
521 * This function is called when an AIE, UIE or PIE mode interrupt
522 * has occurred (or been emulated).
524 * Triggers the registered irq_task function callback.
526 void rtc_handle_legacy_irq(struct rtc_device
*rtc
, int num
, int mode
)
530 /* mark one irq of the appropriate mode */
531 spin_lock_irqsave(&rtc
->irq_lock
, flags
);
532 rtc
->irq_data
= (rtc
->irq_data
+ (num
<< 8)) | (RTC_IRQF
|mode
);
533 spin_unlock_irqrestore(&rtc
->irq_lock
, flags
);
535 /* call the task func */
536 spin_lock_irqsave(&rtc
->irq_task_lock
, flags
);
538 rtc
->irq_task
->func(rtc
->irq_task
->private_data
);
539 spin_unlock_irqrestore(&rtc
->irq_task_lock
, flags
);
541 wake_up_interruptible(&rtc
->irq_queue
);
542 kill_fasync(&rtc
->async_queue
, SIGIO
, POLL_IN
);
547 * rtc_aie_update_irq - AIE mode rtctimer hook
548 * @private: pointer to the rtc_device
550 * This functions is called when the aie_timer expires.
552 void rtc_aie_update_irq(void *private)
554 struct rtc_device
*rtc
= (struct rtc_device
*)private;
555 rtc_handle_legacy_irq(rtc
, 1, RTC_AF
);
560 * rtc_uie_update_irq - UIE mode rtctimer hook
561 * @private: pointer to the rtc_device
563 * This functions is called when the uie_timer expires.
565 void rtc_uie_update_irq(void *private)
567 struct rtc_device
*rtc
= (struct rtc_device
*)private;
568 rtc_handle_legacy_irq(rtc
, 1, RTC_UF
);
573 * rtc_pie_update_irq - PIE mode hrtimer hook
574 * @timer: pointer to the pie mode hrtimer
576 * This function is used to emulate PIE mode interrupts
577 * using an hrtimer. This function is called when the periodic
580 enum hrtimer_restart
rtc_pie_update_irq(struct hrtimer
*timer
)
582 struct rtc_device
*rtc
;
585 rtc
= container_of(timer
, struct rtc_device
, pie_timer
);
587 period
= ktime_set(0, NSEC_PER_SEC
/rtc
->irq_freq
);
588 count
= hrtimer_forward_now(timer
, period
);
590 rtc_handle_legacy_irq(rtc
, count
, RTC_PF
);
592 return HRTIMER_RESTART
;
596 * rtc_update_irq - Triggered when a RTC interrupt occurs.
597 * @rtc: the rtc device
598 * @num: how many irqs are being reported (usually one)
599 * @events: mask of RTC_IRQF with one or more of RTC_PF, RTC_AF, RTC_UF
602 void rtc_update_irq(struct rtc_device
*rtc
,
603 unsigned long num
, unsigned long events
)
605 if (unlikely(IS_ERR_OR_NULL(rtc
)))
608 pm_stay_awake(rtc
->dev
.parent
);
609 schedule_work(&rtc
->irqwork
);
611 EXPORT_SYMBOL_GPL(rtc_update_irq
);
613 static int __rtc_match(struct device
*dev
, const void *data
)
615 const char *name
= data
;
617 if (strcmp(dev_name(dev
), name
) == 0)
622 struct rtc_device
*rtc_class_open(const char *name
)
625 struct rtc_device
*rtc
= NULL
;
627 dev
= class_find_device(rtc_class
, NULL
, name
, __rtc_match
);
629 rtc
= to_rtc_device(dev
);
632 if (!try_module_get(rtc
->owner
)) {
640 EXPORT_SYMBOL_GPL(rtc_class_open
);
642 void rtc_class_close(struct rtc_device
*rtc
)
644 module_put(rtc
->owner
);
645 put_device(&rtc
->dev
);
647 EXPORT_SYMBOL_GPL(rtc_class_close
);
649 int rtc_irq_register(struct rtc_device
*rtc
, struct rtc_task
*task
)
653 if (task
== NULL
|| task
->func
== NULL
)
656 /* Cannot register while the char dev is in use */
657 if (test_and_set_bit_lock(RTC_DEV_BUSY
, &rtc
->flags
))
660 spin_lock_irq(&rtc
->irq_task_lock
);
661 if (rtc
->irq_task
== NULL
) {
662 rtc
->irq_task
= task
;
665 spin_unlock_irq(&rtc
->irq_task_lock
);
667 clear_bit_unlock(RTC_DEV_BUSY
, &rtc
->flags
);
671 EXPORT_SYMBOL_GPL(rtc_irq_register
);
673 void rtc_irq_unregister(struct rtc_device
*rtc
, struct rtc_task
*task
)
675 spin_lock_irq(&rtc
->irq_task_lock
);
676 if (rtc
->irq_task
== task
)
677 rtc
->irq_task
= NULL
;
678 spin_unlock_irq(&rtc
->irq_task_lock
);
680 EXPORT_SYMBOL_GPL(rtc_irq_unregister
);
682 static int rtc_update_hrtimer(struct rtc_device
*rtc
, int enabled
)
685 * We always cancel the timer here first, because otherwise
686 * we could run into BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
687 * when we manage to start the timer before the callback
688 * returns HRTIMER_RESTART.
690 * We cannot use hrtimer_cancel() here as a running callback
691 * could be blocked on rtc->irq_task_lock and hrtimer_cancel()
692 * would spin forever.
694 if (hrtimer_try_to_cancel(&rtc
->pie_timer
) < 0)
698 ktime_t period
= ktime_set(0, NSEC_PER_SEC
/ rtc
->irq_freq
);
700 hrtimer_start(&rtc
->pie_timer
, period
, HRTIMER_MODE_REL
);
706 * rtc_irq_set_state - enable/disable 2^N Hz periodic IRQs
707 * @rtc: the rtc device
708 * @task: currently registered with rtc_irq_register()
709 * @enabled: true to enable periodic IRQs
712 * Note that rtc_irq_set_freq() should previously have been used to
713 * specify the desired frequency of periodic IRQ task->func() callbacks.
715 int rtc_irq_set_state(struct rtc_device
*rtc
, struct rtc_task
*task
, int enabled
)
721 spin_lock_irqsave(&rtc
->irq_task_lock
, flags
);
722 if (rtc
->irq_task
!= NULL
&& task
== NULL
)
724 else if (rtc
->irq_task
!= task
)
727 if (rtc_update_hrtimer(rtc
, enabled
) < 0) {
728 spin_unlock_irqrestore(&rtc
->irq_task_lock
, flags
);
732 rtc
->pie_enabled
= enabled
;
734 spin_unlock_irqrestore(&rtc
->irq_task_lock
, flags
);
737 EXPORT_SYMBOL_GPL(rtc_irq_set_state
);
740 * rtc_irq_set_freq - set 2^N Hz periodic IRQ frequency for IRQ
741 * @rtc: the rtc device
742 * @task: currently registered with rtc_irq_register()
743 * @freq: positive frequency with which task->func() will be called
746 * Note that rtc_irq_set_state() is used to enable or disable the
749 int rtc_irq_set_freq(struct rtc_device
*rtc
, struct rtc_task
*task
, int freq
)
754 if (freq
<= 0 || freq
> RTC_MAX_FREQ
)
757 spin_lock_irqsave(&rtc
->irq_task_lock
, flags
);
758 if (rtc
->irq_task
!= NULL
&& task
== NULL
)
760 else if (rtc
->irq_task
!= task
)
763 rtc
->irq_freq
= freq
;
764 if (rtc
->pie_enabled
&& rtc_update_hrtimer(rtc
, 1) < 0) {
765 spin_unlock_irqrestore(&rtc
->irq_task_lock
, flags
);
770 spin_unlock_irqrestore(&rtc
->irq_task_lock
, flags
);
773 EXPORT_SYMBOL_GPL(rtc_irq_set_freq
);
776 * rtc_timer_enqueue - Adds a rtc_timer to the rtc_device timerqueue
778 * @timer timer being added.
780 * Enqueues a timer onto the rtc devices timerqueue and sets
781 * the next alarm event appropriately.
783 * Sets the enabled bit on the added timer.
785 * Must hold ops_lock for proper serialization of timerqueue
787 static int rtc_timer_enqueue(struct rtc_device
*rtc
, struct rtc_timer
*timer
)
790 timerqueue_add(&rtc
->timerqueue
, &timer
->node
);
791 if (&timer
->node
== timerqueue_getnext(&rtc
->timerqueue
)) {
792 struct rtc_wkalrm alarm
;
794 alarm
.time
= rtc_ktime_to_tm(timer
->node
.expires
);
796 err
= __rtc_set_alarm(rtc
, &alarm
);
798 pm_stay_awake(rtc
->dev
.parent
);
799 schedule_work(&rtc
->irqwork
);
801 timerqueue_del(&rtc
->timerqueue
, &timer
->node
);
809 static void rtc_alarm_disable(struct rtc_device
*rtc
)
811 if (!rtc
->ops
|| !rtc
->ops
->alarm_irq_enable
)
814 rtc
->ops
->alarm_irq_enable(rtc
->dev
.parent
, false);
818 * rtc_timer_remove - Removes a rtc_timer from the rtc_device timerqueue
820 * @timer timer being removed.
822 * Removes a timer onto the rtc devices timerqueue and sets
823 * the next alarm event appropriately.
825 * Clears the enabled bit on the removed timer.
827 * Must hold ops_lock for proper serialization of timerqueue
829 static void rtc_timer_remove(struct rtc_device
*rtc
, struct rtc_timer
*timer
)
831 struct timerqueue_node
*next
= timerqueue_getnext(&rtc
->timerqueue
);
832 timerqueue_del(&rtc
->timerqueue
, &timer
->node
);
834 if (next
== &timer
->node
) {
835 struct rtc_wkalrm alarm
;
837 next
= timerqueue_getnext(&rtc
->timerqueue
);
839 rtc_alarm_disable(rtc
);
842 alarm
.time
= rtc_ktime_to_tm(next
->expires
);
844 err
= __rtc_set_alarm(rtc
, &alarm
);
846 pm_stay_awake(rtc
->dev
.parent
);
847 schedule_work(&rtc
->irqwork
);
853 * rtc_timer_do_work - Expires rtc timers
855 * @timer timer being removed.
857 * Expires rtc timers. Reprograms next alarm event if needed.
858 * Called via worktask.
860 * Serializes access to timerqueue via ops_lock mutex
862 void rtc_timer_do_work(struct work_struct
*work
)
864 struct rtc_timer
*timer
;
865 struct timerqueue_node
*next
;
869 struct rtc_device
*rtc
=
870 container_of(work
, struct rtc_device
, irqwork
);
872 mutex_lock(&rtc
->ops_lock
);
874 __rtc_read_time(rtc
, &tm
);
875 now
= rtc_tm_to_ktime(tm
);
876 while ((next
= timerqueue_getnext(&rtc
->timerqueue
))) {
877 if (next
->expires
.tv64
> now
.tv64
)
881 timer
= container_of(next
, struct rtc_timer
, node
);
882 timerqueue_del(&rtc
->timerqueue
, &timer
->node
);
884 if (timer
->task
.func
)
885 timer
->task
.func(timer
->task
.private_data
);
887 /* Re-add/fwd periodic timers */
888 if (ktime_to_ns(timer
->period
)) {
889 timer
->node
.expires
= ktime_add(timer
->node
.expires
,
892 timerqueue_add(&rtc
->timerqueue
, &timer
->node
);
898 struct rtc_wkalrm alarm
;
902 alarm
.time
= rtc_ktime_to_tm(next
->expires
);
905 err
= __rtc_set_alarm(rtc
, &alarm
);
912 timer
= container_of(next
, struct rtc_timer
, node
);
913 timerqueue_del(&rtc
->timerqueue
, &timer
->node
);
915 dev_err(&rtc
->dev
, "__rtc_set_alarm: err=%d\n", err
);
919 rtc_alarm_disable(rtc
);
921 pm_relax(rtc
->dev
.parent
);
922 mutex_unlock(&rtc
->ops_lock
);
926 /* rtc_timer_init - Initializes an rtc_timer
927 * @timer: timer to be intiialized
928 * @f: function pointer to be called when timer fires
929 * @data: private data passed to function pointer
931 * Kernel interface to initializing an rtc_timer.
933 void rtc_timer_init(struct rtc_timer
*timer
, void (*f
)(void *p
), void *data
)
935 timerqueue_init(&timer
->node
);
937 timer
->task
.func
= f
;
938 timer
->task
.private_data
= data
;
941 /* rtc_timer_start - Sets an rtc_timer to fire in the future
942 * @ rtc: rtc device to be used
943 * @ timer: timer being set
944 * @ expires: time at which to expire the timer
945 * @ period: period that the timer will recur
947 * Kernel interface to set an rtc_timer
949 int rtc_timer_start(struct rtc_device
*rtc
, struct rtc_timer
*timer
,
950 ktime_t expires
, ktime_t period
)
953 mutex_lock(&rtc
->ops_lock
);
955 rtc_timer_remove(rtc
, timer
);
957 timer
->node
.expires
= expires
;
958 timer
->period
= period
;
960 ret
= rtc_timer_enqueue(rtc
, timer
);
962 mutex_unlock(&rtc
->ops_lock
);
966 /* rtc_timer_cancel - Stops an rtc_timer
967 * @ rtc: rtc device to be used
968 * @ timer: timer being set
970 * Kernel interface to cancel an rtc_timer
972 int rtc_timer_cancel(struct rtc_device
*rtc
, struct rtc_timer
*timer
)
975 mutex_lock(&rtc
->ops_lock
);
977 rtc_timer_remove(rtc
, timer
);
978 mutex_unlock(&rtc
->ops_lock
);