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
) {
77 err
= rtc_tm_to_time(tm
, &secs
);
79 err
= rtc
->ops
->set_mmss(rtc
->dev
.parent
, secs
);
83 pm_stay_awake(rtc
->dev
.parent
);
84 mutex_unlock(&rtc
->ops_lock
);
85 /* A timer might have just expired */
86 schedule_work(&rtc
->irqwork
);
89 EXPORT_SYMBOL_GPL(rtc_set_time
);
91 int rtc_set_mmss(struct rtc_device
*rtc
, unsigned long secs
)
95 err
= mutex_lock_interruptible(&rtc
->ops_lock
);
101 else if (rtc
->ops
->set_mmss
)
102 err
= rtc
->ops
->set_mmss(rtc
->dev
.parent
, secs
);
103 else if (rtc
->ops
->read_time
&& rtc
->ops
->set_time
) {
104 struct rtc_time
new, old
;
106 err
= rtc
->ops
->read_time(rtc
->dev
.parent
, &old
);
108 rtc_time_to_tm(secs
, &new);
111 * avoid writing when we're going to change the day of
112 * the month. We will retry in the next minute. This
113 * basically means that if the RTC must not drift
114 * by more than 1 minute in 11 minutes.
116 if (!((old
.tm_hour
== 23 && old
.tm_min
== 59) ||
117 (new.tm_hour
== 23 && new.tm_min
== 59)))
118 err
= rtc
->ops
->set_time(rtc
->dev
.parent
,
125 pm_stay_awake(rtc
->dev
.parent
);
126 mutex_unlock(&rtc
->ops_lock
);
127 /* A timer might have just expired */
128 schedule_work(&rtc
->irqwork
);
132 EXPORT_SYMBOL_GPL(rtc_set_mmss
);
134 static int rtc_read_alarm_internal(struct rtc_device
*rtc
, struct rtc_wkalrm
*alarm
)
138 err
= mutex_lock_interruptible(&rtc
->ops_lock
);
142 if (rtc
->ops
== NULL
)
144 else if (!rtc
->ops
->read_alarm
)
147 memset(alarm
, 0, sizeof(struct rtc_wkalrm
));
148 err
= rtc
->ops
->read_alarm(rtc
->dev
.parent
, alarm
);
151 mutex_unlock(&rtc
->ops_lock
);
155 int __rtc_read_alarm(struct rtc_device
*rtc
, struct rtc_wkalrm
*alarm
)
158 struct rtc_time before
, now
;
160 unsigned long t_now
, t_alm
;
161 enum { none
, day
, month
, year
} missing
= none
;
164 /* The lower level RTC driver may return -1 in some fields,
165 * creating invalid alarm->time values, for reasons like:
167 * - The hardware may not be capable of filling them in;
168 * many alarms match only on time-of-day fields, not
169 * day/month/year calendar data.
171 * - Some hardware uses illegal values as "wildcard" match
172 * values, which non-Linux firmware (like a BIOS) may try
173 * to set up as e.g. "alarm 15 minutes after each hour".
174 * Linux uses only oneshot alarms.
176 * When we see that here, we deal with it by using values from
177 * a current RTC timestamp for any missing (-1) values. The
178 * RTC driver prevents "periodic alarm" modes.
180 * But this can be racey, because some fields of the RTC timestamp
181 * may have wrapped in the interval since we read the RTC alarm,
182 * which would lead to us inserting inconsistent values in place
185 * Reading the alarm and timestamp in the reverse sequence
186 * would have the same race condition, and not solve the issue.
188 * So, we must first read the RTC timestamp,
189 * then read the RTC alarm value,
190 * and then read a second RTC timestamp.
192 * If any fields of the second timestamp have changed
193 * when compared with the first timestamp, then we know
194 * our timestamp may be inconsistent with that used by
195 * the low-level rtc_read_alarm_internal() function.
197 * So, when the two timestamps disagree, we just loop and do
198 * the process again to get a fully consistent set of values.
200 * This could all instead be done in the lower level driver,
201 * but since more than one lower level RTC implementation needs it,
202 * then it's probably best best to do it here instead of there..
205 /* Get the "before" timestamp */
206 err
= rtc_read_time(rtc
, &before
);
211 memcpy(&before
, &now
, sizeof(struct rtc_time
));
214 /* get the RTC alarm values, which may be incomplete */
215 err
= rtc_read_alarm_internal(rtc
, alarm
);
219 /* full-function RTCs won't have such missing fields */
220 if (rtc_valid_tm(&alarm
->time
) == 0)
223 /* get the "after" timestamp, to detect wrapped fields */
224 err
= rtc_read_time(rtc
, &now
);
228 /* note that tm_sec is a "don't care" value here: */
229 } while ( before
.tm_min
!= now
.tm_min
230 || before
.tm_hour
!= now
.tm_hour
231 || before
.tm_mon
!= now
.tm_mon
232 || before
.tm_year
!= now
.tm_year
);
234 /* Fill in the missing alarm fields using the timestamp; we
235 * know there's at least one since alarm->time is invalid.
237 if (alarm
->time
.tm_sec
== -1)
238 alarm
->time
.tm_sec
= now
.tm_sec
;
239 if (alarm
->time
.tm_min
== -1)
240 alarm
->time
.tm_min
= now
.tm_min
;
241 if (alarm
->time
.tm_hour
== -1)
242 alarm
->time
.tm_hour
= now
.tm_hour
;
244 /* For simplicity, only support date rollover for now */
245 if (alarm
->time
.tm_mday
< 1 || alarm
->time
.tm_mday
> 31) {
246 alarm
->time
.tm_mday
= now
.tm_mday
;
249 if ((unsigned)alarm
->time
.tm_mon
>= 12) {
250 alarm
->time
.tm_mon
= now
.tm_mon
;
254 if (alarm
->time
.tm_year
== -1) {
255 alarm
->time
.tm_year
= now
.tm_year
;
260 /* with luck, no rollover is needed */
261 rtc_tm_to_time(&now
, &t_now
);
262 rtc_tm_to_time(&alarm
->time
, &t_alm
);
268 /* 24 hour rollover ... if it's now 10am Monday, an alarm that
269 * that will trigger at 5am will do so at 5am Tuesday, which
270 * could also be in the next month or year. This is a common
271 * case, especially for PCs.
274 dev_dbg(&rtc
->dev
, "alarm rollover: %s\n", "day");
275 t_alm
+= 24 * 60 * 60;
276 rtc_time_to_tm(t_alm
, &alarm
->time
);
279 /* Month rollover ... if it's the 31th, an alarm on the 3rd will
280 * be next month. An alarm matching on the 30th, 29th, or 28th
281 * may end up in the month after that! Many newer PCs support
282 * this type of alarm.
285 dev_dbg(&rtc
->dev
, "alarm rollover: %s\n", "month");
287 if (alarm
->time
.tm_mon
< 11)
288 alarm
->time
.tm_mon
++;
290 alarm
->time
.tm_mon
= 0;
291 alarm
->time
.tm_year
++;
293 days
= rtc_month_days(alarm
->time
.tm_mon
,
294 alarm
->time
.tm_year
);
295 } while (days
< alarm
->time
.tm_mday
);
298 /* Year rollover ... easy except for leap years! */
300 dev_dbg(&rtc
->dev
, "alarm rollover: %s\n", "year");
302 alarm
->time
.tm_year
++;
303 } while (!is_leap_year(alarm
->time
.tm_year
+ 1900)
304 && rtc_valid_tm(&alarm
->time
) != 0);
308 dev_warn(&rtc
->dev
, "alarm rollover not handled\n");
312 err
= rtc_valid_tm(&alarm
->time
);
315 dev_warn(&rtc
->dev
, "invalid alarm value: %d-%d-%d %d:%d:%d\n",
316 alarm
->time
.tm_year
+ 1900, alarm
->time
.tm_mon
+ 1,
317 alarm
->time
.tm_mday
, alarm
->time
.tm_hour
, alarm
->time
.tm_min
,
324 int rtc_read_alarm(struct rtc_device
*rtc
, struct rtc_wkalrm
*alarm
)
328 err
= mutex_lock_interruptible(&rtc
->ops_lock
);
331 if (rtc
->ops
== NULL
)
333 else if (!rtc
->ops
->read_alarm
)
336 memset(alarm
, 0, sizeof(struct rtc_wkalrm
));
337 alarm
->enabled
= rtc
->aie_timer
.enabled
;
338 alarm
->time
= rtc_ktime_to_tm(rtc
->aie_timer
.node
.expires
);
340 mutex_unlock(&rtc
->ops_lock
);
344 EXPORT_SYMBOL_GPL(rtc_read_alarm
);
346 static int __rtc_set_alarm(struct rtc_device
*rtc
, struct rtc_wkalrm
*alarm
)
352 err
= rtc_valid_tm(&alarm
->time
);
355 rtc_tm_to_time(&alarm
->time
, &scheduled
);
357 /* Make sure we're not setting alarms in the past */
358 err
= __rtc_read_time(rtc
, &tm
);
361 rtc_tm_to_time(&tm
, &now
);
362 if (scheduled
<= now
)
365 * XXX - We just checked to make sure the alarm time is not
366 * in the past, but there is still a race window where if
367 * the is alarm set for the next second and the second ticks
368 * over right here, before we set the alarm.
373 else if (!rtc
->ops
->set_alarm
)
376 err
= rtc
->ops
->set_alarm(rtc
->dev
.parent
, alarm
);
381 int rtc_set_alarm(struct rtc_device
*rtc
, struct rtc_wkalrm
*alarm
)
385 err
= rtc_valid_tm(&alarm
->time
);
389 err
= mutex_lock_interruptible(&rtc
->ops_lock
);
392 if (rtc
->aie_timer
.enabled
)
393 rtc_timer_remove(rtc
, &rtc
->aie_timer
);
395 rtc
->aie_timer
.node
.expires
= rtc_tm_to_ktime(alarm
->time
);
396 rtc
->aie_timer
.period
= ktime_set(0, 0);
398 err
= rtc_timer_enqueue(rtc
, &rtc
->aie_timer
);
400 mutex_unlock(&rtc
->ops_lock
);
403 EXPORT_SYMBOL_GPL(rtc_set_alarm
);
405 /* Called once per device from rtc_device_register */
406 int rtc_initialize_alarm(struct rtc_device
*rtc
, struct rtc_wkalrm
*alarm
)
411 err
= rtc_valid_tm(&alarm
->time
);
415 err
= rtc_read_time(rtc
, &now
);
419 err
= mutex_lock_interruptible(&rtc
->ops_lock
);
423 rtc
->aie_timer
.node
.expires
= rtc_tm_to_ktime(alarm
->time
);
424 rtc
->aie_timer
.period
= ktime_set(0, 0);
426 /* Alarm has to be enabled & in the futrure for us to enqueue it */
427 if (alarm
->enabled
&& (rtc_tm_to_ktime(now
).tv64
<
428 rtc
->aie_timer
.node
.expires
.tv64
)) {
430 rtc
->aie_timer
.enabled
= 1;
431 timerqueue_add(&rtc
->timerqueue
, &rtc
->aie_timer
.node
);
433 mutex_unlock(&rtc
->ops_lock
);
436 EXPORT_SYMBOL_GPL(rtc_initialize_alarm
);
440 int rtc_alarm_irq_enable(struct rtc_device
*rtc
, unsigned int enabled
)
442 int err
= mutex_lock_interruptible(&rtc
->ops_lock
);
446 if (rtc
->aie_timer
.enabled
!= enabled
) {
448 err
= rtc_timer_enqueue(rtc
, &rtc
->aie_timer
);
450 rtc_timer_remove(rtc
, &rtc
->aie_timer
);
457 else if (!rtc
->ops
->alarm_irq_enable
)
460 err
= rtc
->ops
->alarm_irq_enable(rtc
->dev
.parent
, enabled
);
462 mutex_unlock(&rtc
->ops_lock
);
465 EXPORT_SYMBOL_GPL(rtc_alarm_irq_enable
);
467 int rtc_update_irq_enable(struct rtc_device
*rtc
, unsigned int enabled
)
469 int err
= mutex_lock_interruptible(&rtc
->ops_lock
);
473 #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
474 if (enabled
== 0 && rtc
->uie_irq_active
) {
475 mutex_unlock(&rtc
->ops_lock
);
476 return rtc_dev_update_irq_enable_emul(rtc
, 0);
479 /* make sure we're changing state */
480 if (rtc
->uie_rtctimer
.enabled
== enabled
)
483 if (rtc
->uie_unsupported
) {
492 __rtc_read_time(rtc
, &tm
);
493 onesec
= ktime_set(1, 0);
494 now
= rtc_tm_to_ktime(tm
);
495 rtc
->uie_rtctimer
.node
.expires
= ktime_add(now
, onesec
);
496 rtc
->uie_rtctimer
.period
= ktime_set(1, 0);
497 err
= rtc_timer_enqueue(rtc
, &rtc
->uie_rtctimer
);
499 rtc_timer_remove(rtc
, &rtc
->uie_rtctimer
);
502 mutex_unlock(&rtc
->ops_lock
);
503 #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
505 * Enable emulation if the driver did not provide
506 * the update_irq_enable function pointer or if returned
507 * -EINVAL to signal that it has been configured without
508 * interrupts or that are not available at the moment.
511 err
= rtc_dev_update_irq_enable_emul(rtc
, enabled
);
516 EXPORT_SYMBOL_GPL(rtc_update_irq_enable
);
520 * rtc_handle_legacy_irq - AIE, UIE and PIE event hook
521 * @rtc: pointer to the rtc device
523 * This function is called when an AIE, UIE or PIE mode interrupt
524 * has occurred (or been emulated).
526 * Triggers the registered irq_task function callback.
528 void rtc_handle_legacy_irq(struct rtc_device
*rtc
, int num
, int mode
)
532 /* mark one irq of the appropriate mode */
533 spin_lock_irqsave(&rtc
->irq_lock
, flags
);
534 rtc
->irq_data
= (rtc
->irq_data
+ (num
<< 8)) | (RTC_IRQF
|mode
);
535 spin_unlock_irqrestore(&rtc
->irq_lock
, flags
);
537 /* call the task func */
538 spin_lock_irqsave(&rtc
->irq_task_lock
, flags
);
540 rtc
->irq_task
->func(rtc
->irq_task
->private_data
);
541 spin_unlock_irqrestore(&rtc
->irq_task_lock
, flags
);
543 wake_up_interruptible(&rtc
->irq_queue
);
544 kill_fasync(&rtc
->async_queue
, SIGIO
, POLL_IN
);
549 * rtc_aie_update_irq - AIE mode rtctimer hook
550 * @private: pointer to the rtc_device
552 * This functions is called when the aie_timer expires.
554 void rtc_aie_update_irq(void *private)
556 struct rtc_device
*rtc
= (struct rtc_device
*)private;
557 rtc_handle_legacy_irq(rtc
, 1, RTC_AF
);
562 * rtc_uie_update_irq - UIE mode rtctimer hook
563 * @private: pointer to the rtc_device
565 * This functions is called when the uie_timer expires.
567 void rtc_uie_update_irq(void *private)
569 struct rtc_device
*rtc
= (struct rtc_device
*)private;
570 rtc_handle_legacy_irq(rtc
, 1, RTC_UF
);
575 * rtc_pie_update_irq - PIE mode hrtimer hook
576 * @timer: pointer to the pie mode hrtimer
578 * This function is used to emulate PIE mode interrupts
579 * using an hrtimer. This function is called when the periodic
582 enum hrtimer_restart
rtc_pie_update_irq(struct hrtimer
*timer
)
584 struct rtc_device
*rtc
;
587 rtc
= container_of(timer
, struct rtc_device
, pie_timer
);
589 period
= ktime_set(0, NSEC_PER_SEC
/rtc
->irq_freq
);
590 count
= hrtimer_forward_now(timer
, period
);
592 rtc_handle_legacy_irq(rtc
, count
, RTC_PF
);
594 return HRTIMER_RESTART
;
598 * rtc_update_irq - Triggered when a RTC interrupt occurs.
599 * @rtc: the rtc device
600 * @num: how many irqs are being reported (usually one)
601 * @events: mask of RTC_IRQF with one or more of RTC_PF, RTC_AF, RTC_UF
604 void rtc_update_irq(struct rtc_device
*rtc
,
605 unsigned long num
, unsigned long events
)
607 if (unlikely(IS_ERR_OR_NULL(rtc
)))
610 pm_stay_awake(rtc
->dev
.parent
);
611 schedule_work(&rtc
->irqwork
);
613 EXPORT_SYMBOL_GPL(rtc_update_irq
);
615 static int __rtc_match(struct device
*dev
, const void *data
)
617 const char *name
= data
;
619 if (strcmp(dev_name(dev
), name
) == 0)
624 struct rtc_device
*rtc_class_open(const char *name
)
627 struct rtc_device
*rtc
= NULL
;
629 dev
= class_find_device(rtc_class
, NULL
, name
, __rtc_match
);
631 rtc
= to_rtc_device(dev
);
634 if (!try_module_get(rtc
->owner
)) {
642 EXPORT_SYMBOL_GPL(rtc_class_open
);
644 void rtc_class_close(struct rtc_device
*rtc
)
646 module_put(rtc
->owner
);
647 put_device(&rtc
->dev
);
649 EXPORT_SYMBOL_GPL(rtc_class_close
);
651 int rtc_irq_register(struct rtc_device
*rtc
, struct rtc_task
*task
)
655 if (task
== NULL
|| task
->func
== NULL
)
658 /* Cannot register while the char dev is in use */
659 if (test_and_set_bit_lock(RTC_DEV_BUSY
, &rtc
->flags
))
662 spin_lock_irq(&rtc
->irq_task_lock
);
663 if (rtc
->irq_task
== NULL
) {
664 rtc
->irq_task
= task
;
667 spin_unlock_irq(&rtc
->irq_task_lock
);
669 clear_bit_unlock(RTC_DEV_BUSY
, &rtc
->flags
);
673 EXPORT_SYMBOL_GPL(rtc_irq_register
);
675 void rtc_irq_unregister(struct rtc_device
*rtc
, struct rtc_task
*task
)
677 spin_lock_irq(&rtc
->irq_task_lock
);
678 if (rtc
->irq_task
== task
)
679 rtc
->irq_task
= NULL
;
680 spin_unlock_irq(&rtc
->irq_task_lock
);
682 EXPORT_SYMBOL_GPL(rtc_irq_unregister
);
684 static int rtc_update_hrtimer(struct rtc_device
*rtc
, int enabled
)
687 * We always cancel the timer here first, because otherwise
688 * we could run into BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
689 * when we manage to start the timer before the callback
690 * returns HRTIMER_RESTART.
692 * We cannot use hrtimer_cancel() here as a running callback
693 * could be blocked on rtc->irq_task_lock and hrtimer_cancel()
694 * would spin forever.
696 if (hrtimer_try_to_cancel(&rtc
->pie_timer
) < 0)
700 ktime_t period
= ktime_set(0, NSEC_PER_SEC
/ rtc
->irq_freq
);
702 hrtimer_start(&rtc
->pie_timer
, period
, HRTIMER_MODE_REL
);
708 * rtc_irq_set_state - enable/disable 2^N Hz periodic IRQs
709 * @rtc: the rtc device
710 * @task: currently registered with rtc_irq_register()
711 * @enabled: true to enable periodic IRQs
714 * Note that rtc_irq_set_freq() should previously have been used to
715 * specify the desired frequency of periodic IRQ task->func() callbacks.
717 int rtc_irq_set_state(struct rtc_device
*rtc
, struct rtc_task
*task
, int enabled
)
723 spin_lock_irqsave(&rtc
->irq_task_lock
, flags
);
724 if (rtc
->irq_task
!= NULL
&& task
== NULL
)
726 else if (rtc
->irq_task
!= task
)
729 if (rtc_update_hrtimer(rtc
, enabled
) < 0) {
730 spin_unlock_irqrestore(&rtc
->irq_task_lock
, flags
);
734 rtc
->pie_enabled
= enabled
;
736 spin_unlock_irqrestore(&rtc
->irq_task_lock
, flags
);
739 EXPORT_SYMBOL_GPL(rtc_irq_set_state
);
742 * rtc_irq_set_freq - set 2^N Hz periodic IRQ frequency for IRQ
743 * @rtc: the rtc device
744 * @task: currently registered with rtc_irq_register()
745 * @freq: positive frequency with which task->func() will be called
748 * Note that rtc_irq_set_state() is used to enable or disable the
751 int rtc_irq_set_freq(struct rtc_device
*rtc
, struct rtc_task
*task
, int freq
)
756 if (freq
<= 0 || freq
> RTC_MAX_FREQ
)
759 spin_lock_irqsave(&rtc
->irq_task_lock
, flags
);
760 if (rtc
->irq_task
!= NULL
&& task
== NULL
)
762 else if (rtc
->irq_task
!= task
)
765 rtc
->irq_freq
= freq
;
766 if (rtc
->pie_enabled
&& rtc_update_hrtimer(rtc
, 1) < 0) {
767 spin_unlock_irqrestore(&rtc
->irq_task_lock
, flags
);
772 spin_unlock_irqrestore(&rtc
->irq_task_lock
, flags
);
775 EXPORT_SYMBOL_GPL(rtc_irq_set_freq
);
778 * rtc_timer_enqueue - Adds a rtc_timer to the rtc_device timerqueue
780 * @timer timer being added.
782 * Enqueues a timer onto the rtc devices timerqueue and sets
783 * the next alarm event appropriately.
785 * Sets the enabled bit on the added timer.
787 * Must hold ops_lock for proper serialization of timerqueue
789 static int rtc_timer_enqueue(struct rtc_device
*rtc
, struct rtc_timer
*timer
)
792 timerqueue_add(&rtc
->timerqueue
, &timer
->node
);
793 if (&timer
->node
== timerqueue_getnext(&rtc
->timerqueue
)) {
794 struct rtc_wkalrm alarm
;
796 alarm
.time
= rtc_ktime_to_tm(timer
->node
.expires
);
798 err
= __rtc_set_alarm(rtc
, &alarm
);
800 pm_stay_awake(rtc
->dev
.parent
);
801 schedule_work(&rtc
->irqwork
);
803 timerqueue_del(&rtc
->timerqueue
, &timer
->node
);
811 static void rtc_alarm_disable(struct rtc_device
*rtc
)
813 if (!rtc
->ops
|| !rtc
->ops
->alarm_irq_enable
)
816 rtc
->ops
->alarm_irq_enable(rtc
->dev
.parent
, false);
820 * rtc_timer_remove - Removes a rtc_timer from the rtc_device timerqueue
822 * @timer timer being removed.
824 * Removes a timer onto the rtc devices timerqueue and sets
825 * the next alarm event appropriately.
827 * Clears the enabled bit on the removed timer.
829 * Must hold ops_lock for proper serialization of timerqueue
831 static void rtc_timer_remove(struct rtc_device
*rtc
, struct rtc_timer
*timer
)
833 struct timerqueue_node
*next
= timerqueue_getnext(&rtc
->timerqueue
);
834 timerqueue_del(&rtc
->timerqueue
, &timer
->node
);
836 if (next
== &timer
->node
) {
837 struct rtc_wkalrm alarm
;
839 next
= timerqueue_getnext(&rtc
->timerqueue
);
841 rtc_alarm_disable(rtc
);
844 alarm
.time
= rtc_ktime_to_tm(next
->expires
);
846 err
= __rtc_set_alarm(rtc
, &alarm
);
848 pm_stay_awake(rtc
->dev
.parent
);
849 schedule_work(&rtc
->irqwork
);
855 * rtc_timer_do_work - Expires rtc timers
857 * @timer timer being removed.
859 * Expires rtc timers. Reprograms next alarm event if needed.
860 * Called via worktask.
862 * Serializes access to timerqueue via ops_lock mutex
864 void rtc_timer_do_work(struct work_struct
*work
)
866 struct rtc_timer
*timer
;
867 struct timerqueue_node
*next
;
871 struct rtc_device
*rtc
=
872 container_of(work
, struct rtc_device
, irqwork
);
874 mutex_lock(&rtc
->ops_lock
);
876 __rtc_read_time(rtc
, &tm
);
877 now
= rtc_tm_to_ktime(tm
);
878 while ((next
= timerqueue_getnext(&rtc
->timerqueue
))) {
879 if (next
->expires
.tv64
> now
.tv64
)
883 timer
= container_of(next
, struct rtc_timer
, node
);
884 timerqueue_del(&rtc
->timerqueue
, &timer
->node
);
886 if (timer
->task
.func
)
887 timer
->task
.func(timer
->task
.private_data
);
889 /* Re-add/fwd periodic timers */
890 if (ktime_to_ns(timer
->period
)) {
891 timer
->node
.expires
= ktime_add(timer
->node
.expires
,
894 timerqueue_add(&rtc
->timerqueue
, &timer
->node
);
900 struct rtc_wkalrm alarm
;
904 alarm
.time
= rtc_ktime_to_tm(next
->expires
);
907 err
= __rtc_set_alarm(rtc
, &alarm
);
914 timer
= container_of(next
, struct rtc_timer
, node
);
915 timerqueue_del(&rtc
->timerqueue
, &timer
->node
);
917 dev_err(&rtc
->dev
, "__rtc_set_alarm: err=%d\n", err
);
921 rtc_alarm_disable(rtc
);
923 pm_relax(rtc
->dev
.parent
);
924 mutex_unlock(&rtc
->ops_lock
);
928 /* rtc_timer_init - Initializes an rtc_timer
929 * @timer: timer to be intiialized
930 * @f: function pointer to be called when timer fires
931 * @data: private data passed to function pointer
933 * Kernel interface to initializing an rtc_timer.
935 void rtc_timer_init(struct rtc_timer
*timer
, void (*f
)(void *p
), void *data
)
937 timerqueue_init(&timer
->node
);
939 timer
->task
.func
= f
;
940 timer
->task
.private_data
= data
;
943 /* rtc_timer_start - Sets an rtc_timer to fire in the future
944 * @ rtc: rtc device to be used
945 * @ timer: timer being set
946 * @ expires: time at which to expire the timer
947 * @ period: period that the timer will recur
949 * Kernel interface to set an rtc_timer
951 int rtc_timer_start(struct rtc_device
*rtc
, struct rtc_timer
*timer
,
952 ktime_t expires
, ktime_t period
)
955 mutex_lock(&rtc
->ops_lock
);
957 rtc_timer_remove(rtc
, timer
);
959 timer
->node
.expires
= expires
;
960 timer
->period
= period
;
962 ret
= rtc_timer_enqueue(rtc
, timer
);
964 mutex_unlock(&rtc
->ops_lock
);
968 /* rtc_timer_cancel - Stops an rtc_timer
969 * @ rtc: rtc device to be used
970 * @ timer: timer being set
972 * Kernel interface to cancel an rtc_timer
974 int rtc_timer_cancel(struct rtc_device
*rtc
, struct rtc_timer
*timer
)
977 mutex_lock(&rtc
->ops_lock
);
979 rtc_timer_remove(rtc
, timer
);
980 mutex_unlock(&rtc
->ops_lock
);