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 #define CREATE_TRACE_POINTS
21 #include <trace/events/rtc.h>
23 static int rtc_timer_enqueue(struct rtc_device
*rtc
, struct rtc_timer
*timer
);
24 static void rtc_timer_remove(struct rtc_device
*rtc
, struct rtc_timer
*timer
);
26 static void rtc_add_offset(struct rtc_device
*rtc
, struct rtc_time
*tm
)
30 if (!rtc
->offset_secs
)
33 secs
= rtc_tm_to_time64(tm
);
36 * Since the reading time values from RTC device are always in the RTC
37 * original valid range, but we need to skip the overlapped region
38 * between expanded range and original range, which is no need to add
41 if ((rtc
->start_secs
> rtc
->range_min
&& secs
>= rtc
->start_secs
) ||
42 (rtc
->start_secs
< rtc
->range_min
&&
43 secs
<= (rtc
->start_secs
+ rtc
->range_max
- rtc
->range_min
)))
46 rtc_time64_to_tm(secs
+ rtc
->offset_secs
, tm
);
49 static void rtc_subtract_offset(struct rtc_device
*rtc
, struct rtc_time
*tm
)
53 if (!rtc
->offset_secs
)
56 secs
= rtc_tm_to_time64(tm
);
59 * If the setting time values are in the valid range of RTC hardware
60 * device, then no need to subtract the offset when setting time to RTC
61 * device. Otherwise we need to subtract the offset to make the time
62 * values are valid for RTC hardware device.
64 if (secs
>= rtc
->range_min
&& secs
<= rtc
->range_max
)
67 rtc_time64_to_tm(secs
- rtc
->offset_secs
, tm
);
70 static int rtc_valid_range(struct rtc_device
*rtc
, struct rtc_time
*tm
)
72 if (rtc
->range_min
!= rtc
->range_max
) {
73 time64_t time
= rtc_tm_to_time64(tm
);
74 time64_t range_min
= rtc
->set_start_time
? rtc
->start_secs
:
76 time64_t range_max
= rtc
->set_start_time
?
77 (rtc
->start_secs
+ rtc
->range_max
- rtc
->range_min
) :
80 if (time
< range_min
|| time
> range_max
)
87 static int __rtc_read_time(struct rtc_device
*rtc
, struct rtc_time
*tm
)
92 else if (!rtc
->ops
->read_time
)
95 memset(tm
, 0, sizeof(struct rtc_time
));
96 err
= rtc
->ops
->read_time(rtc
->dev
.parent
, tm
);
98 dev_dbg(&rtc
->dev
, "read_time: fail to read: %d\n",
103 rtc_add_offset(rtc
, tm
);
105 err
= rtc_valid_tm(tm
);
107 dev_dbg(&rtc
->dev
, "read_time: rtc_time isn't valid\n");
112 int rtc_read_time(struct rtc_device
*rtc
, struct rtc_time
*tm
)
116 err
= mutex_lock_interruptible(&rtc
->ops_lock
);
120 err
= __rtc_read_time(rtc
, tm
);
121 mutex_unlock(&rtc
->ops_lock
);
123 trace_rtc_read_time(rtc_tm_to_time64(tm
), err
);
126 EXPORT_SYMBOL_GPL(rtc_read_time
);
128 int rtc_set_time(struct rtc_device
*rtc
, struct rtc_time
*tm
)
132 err
= rtc_valid_tm(tm
);
136 err
= rtc_valid_range(rtc
, tm
);
140 rtc_subtract_offset(rtc
, tm
);
142 #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
143 uie
= rtc
->uie_rtctimer
.enabled
|| rtc
->uie_irq_active
;
145 uie
= rtc
->uie_rtctimer
.enabled
;
148 err
= rtc_update_irq_enable(rtc
, 0);
153 err
= mutex_lock_interruptible(&rtc
->ops_lock
);
159 else if (rtc
->ops
->set_time
)
160 err
= rtc
->ops
->set_time(rtc
->dev
.parent
, tm
);
161 else if (rtc
->ops
->set_mmss64
) {
162 time64_t secs64
= rtc_tm_to_time64(tm
);
164 err
= rtc
->ops
->set_mmss64(rtc
->dev
.parent
, secs64
);
165 } else if (rtc
->ops
->set_mmss
) {
166 time64_t secs64
= rtc_tm_to_time64(tm
);
167 err
= rtc
->ops
->set_mmss(rtc
->dev
.parent
, secs64
);
171 pm_stay_awake(rtc
->dev
.parent
);
172 mutex_unlock(&rtc
->ops_lock
);
173 /* A timer might have just expired */
174 schedule_work(&rtc
->irqwork
);
177 err
= rtc_update_irq_enable(rtc
, 1);
182 trace_rtc_set_time(rtc_tm_to_time64(tm
), err
);
185 EXPORT_SYMBOL_GPL(rtc_set_time
);
187 static int rtc_read_alarm_internal(struct rtc_device
*rtc
, struct rtc_wkalrm
*alarm
)
191 err
= mutex_lock_interruptible(&rtc
->ops_lock
);
195 if (rtc
->ops
== NULL
)
197 else if (!rtc
->ops
->read_alarm
)
202 alarm
->time
.tm_sec
= -1;
203 alarm
->time
.tm_min
= -1;
204 alarm
->time
.tm_hour
= -1;
205 alarm
->time
.tm_mday
= -1;
206 alarm
->time
.tm_mon
= -1;
207 alarm
->time
.tm_year
= -1;
208 alarm
->time
.tm_wday
= -1;
209 alarm
->time
.tm_yday
= -1;
210 alarm
->time
.tm_isdst
= -1;
211 err
= rtc
->ops
->read_alarm(rtc
->dev
.parent
, alarm
);
214 mutex_unlock(&rtc
->ops_lock
);
216 trace_rtc_read_alarm(rtc_tm_to_time64(&alarm
->time
), err
);
220 int __rtc_read_alarm(struct rtc_device
*rtc
, struct rtc_wkalrm
*alarm
)
223 struct rtc_time before
, now
;
225 time64_t t_now
, t_alm
;
226 enum { none
, day
, month
, year
} missing
= none
;
229 /* The lower level RTC driver may return -1 in some fields,
230 * creating invalid alarm->time values, for reasons like:
232 * - The hardware may not be capable of filling them in;
233 * many alarms match only on time-of-day fields, not
234 * day/month/year calendar data.
236 * - Some hardware uses illegal values as "wildcard" match
237 * values, which non-Linux firmware (like a BIOS) may try
238 * to set up as e.g. "alarm 15 minutes after each hour".
239 * Linux uses only oneshot alarms.
241 * When we see that here, we deal with it by using values from
242 * a current RTC timestamp for any missing (-1) values. The
243 * RTC driver prevents "periodic alarm" modes.
245 * But this can be racey, because some fields of the RTC timestamp
246 * may have wrapped in the interval since we read the RTC alarm,
247 * which would lead to us inserting inconsistent values in place
250 * Reading the alarm and timestamp in the reverse sequence
251 * would have the same race condition, and not solve the issue.
253 * So, we must first read the RTC timestamp,
254 * then read the RTC alarm value,
255 * and then read a second RTC timestamp.
257 * If any fields of the second timestamp have changed
258 * when compared with the first timestamp, then we know
259 * our timestamp may be inconsistent with that used by
260 * the low-level rtc_read_alarm_internal() function.
262 * So, when the two timestamps disagree, we just loop and do
263 * the process again to get a fully consistent set of values.
265 * This could all instead be done in the lower level driver,
266 * but since more than one lower level RTC implementation needs it,
267 * then it's probably best best to do it here instead of there..
270 /* Get the "before" timestamp */
271 err
= rtc_read_time(rtc
, &before
);
276 memcpy(&before
, &now
, sizeof(struct rtc_time
));
279 /* get the RTC alarm values, which may be incomplete */
280 err
= rtc_read_alarm_internal(rtc
, alarm
);
284 /* full-function RTCs won't have such missing fields */
285 if (rtc_valid_tm(&alarm
->time
) == 0) {
286 rtc_add_offset(rtc
, &alarm
->time
);
290 /* get the "after" timestamp, to detect wrapped fields */
291 err
= rtc_read_time(rtc
, &now
);
295 /* note that tm_sec is a "don't care" value here: */
296 } while ( before
.tm_min
!= now
.tm_min
297 || before
.tm_hour
!= now
.tm_hour
298 || before
.tm_mon
!= now
.tm_mon
299 || before
.tm_year
!= now
.tm_year
);
301 /* Fill in the missing alarm fields using the timestamp; we
302 * know there's at least one since alarm->time is invalid.
304 if (alarm
->time
.tm_sec
== -1)
305 alarm
->time
.tm_sec
= now
.tm_sec
;
306 if (alarm
->time
.tm_min
== -1)
307 alarm
->time
.tm_min
= now
.tm_min
;
308 if (alarm
->time
.tm_hour
== -1)
309 alarm
->time
.tm_hour
= now
.tm_hour
;
311 /* For simplicity, only support date rollover for now */
312 if (alarm
->time
.tm_mday
< 1 || alarm
->time
.tm_mday
> 31) {
313 alarm
->time
.tm_mday
= now
.tm_mday
;
316 if ((unsigned)alarm
->time
.tm_mon
>= 12) {
317 alarm
->time
.tm_mon
= now
.tm_mon
;
321 if (alarm
->time
.tm_year
== -1) {
322 alarm
->time
.tm_year
= now
.tm_year
;
327 /* Can't proceed if alarm is still invalid after replacing
330 err
= rtc_valid_tm(&alarm
->time
);
334 /* with luck, no rollover is needed */
335 t_now
= rtc_tm_to_time64(&now
);
336 t_alm
= rtc_tm_to_time64(&alarm
->time
);
342 /* 24 hour rollover ... if it's now 10am Monday, an alarm that
343 * that will trigger at 5am will do so at 5am Tuesday, which
344 * could also be in the next month or year. This is a common
345 * case, especially for PCs.
348 dev_dbg(&rtc
->dev
, "alarm rollover: %s\n", "day");
349 t_alm
+= 24 * 60 * 60;
350 rtc_time64_to_tm(t_alm
, &alarm
->time
);
353 /* Month rollover ... if it's the 31th, an alarm on the 3rd will
354 * be next month. An alarm matching on the 30th, 29th, or 28th
355 * may end up in the month after that! Many newer PCs support
356 * this type of alarm.
359 dev_dbg(&rtc
->dev
, "alarm rollover: %s\n", "month");
361 if (alarm
->time
.tm_mon
< 11)
362 alarm
->time
.tm_mon
++;
364 alarm
->time
.tm_mon
= 0;
365 alarm
->time
.tm_year
++;
367 days
= rtc_month_days(alarm
->time
.tm_mon
,
368 alarm
->time
.tm_year
);
369 } while (days
< alarm
->time
.tm_mday
);
372 /* Year rollover ... easy except for leap years! */
374 dev_dbg(&rtc
->dev
, "alarm rollover: %s\n", "year");
376 alarm
->time
.tm_year
++;
377 } while (!is_leap_year(alarm
->time
.tm_year
+ 1900)
378 && rtc_valid_tm(&alarm
->time
) != 0);
382 dev_warn(&rtc
->dev
, "alarm rollover not handled\n");
385 err
= rtc_valid_tm(&alarm
->time
);
389 dev_warn(&rtc
->dev
, "invalid alarm value: %d-%d-%d %d:%d:%d\n",
390 alarm
->time
.tm_year
+ 1900, alarm
->time
.tm_mon
+ 1,
391 alarm
->time
.tm_mday
, alarm
->time
.tm_hour
, alarm
->time
.tm_min
,
398 int rtc_read_alarm(struct rtc_device
*rtc
, struct rtc_wkalrm
*alarm
)
402 err
= mutex_lock_interruptible(&rtc
->ops_lock
);
405 if (rtc
->ops
== NULL
)
407 else if (!rtc
->ops
->read_alarm
)
410 memset(alarm
, 0, sizeof(struct rtc_wkalrm
));
411 alarm
->enabled
= rtc
->aie_timer
.enabled
;
412 alarm
->time
= rtc_ktime_to_tm(rtc
->aie_timer
.node
.expires
);
414 mutex_unlock(&rtc
->ops_lock
);
416 trace_rtc_read_alarm(rtc_tm_to_time64(&alarm
->time
), err
);
419 EXPORT_SYMBOL_GPL(rtc_read_alarm
);
421 static int __rtc_set_alarm(struct rtc_device
*rtc
, struct rtc_wkalrm
*alarm
)
424 time64_t now
, scheduled
;
427 err
= rtc_valid_tm(&alarm
->time
);
431 scheduled
= rtc_tm_to_time64(&alarm
->time
);
433 /* Make sure we're not setting alarms in the past */
434 err
= __rtc_read_time(rtc
, &tm
);
437 now
= rtc_tm_to_time64(&tm
);
438 if (scheduled
<= now
)
441 * XXX - We just checked to make sure the alarm time is not
442 * in the past, but there is still a race window where if
443 * the is alarm set for the next second and the second ticks
444 * over right here, before we set the alarm.
447 rtc_subtract_offset(rtc
, &alarm
->time
);
451 else if (!rtc
->ops
->set_alarm
)
454 err
= rtc
->ops
->set_alarm(rtc
->dev
.parent
, alarm
);
456 trace_rtc_set_alarm(rtc_tm_to_time64(&alarm
->time
), err
);
460 int rtc_set_alarm(struct rtc_device
*rtc
, struct rtc_wkalrm
*alarm
)
466 else if (!rtc
->ops
->set_alarm
)
469 err
= rtc_valid_tm(&alarm
->time
);
473 err
= rtc_valid_range(rtc
, &alarm
->time
);
477 err
= mutex_lock_interruptible(&rtc
->ops_lock
);
480 if (rtc
->aie_timer
.enabled
)
481 rtc_timer_remove(rtc
, &rtc
->aie_timer
);
483 rtc
->aie_timer
.node
.expires
= rtc_tm_to_ktime(alarm
->time
);
484 rtc
->aie_timer
.period
= 0;
486 err
= rtc_timer_enqueue(rtc
, &rtc
->aie_timer
);
488 mutex_unlock(&rtc
->ops_lock
);
492 EXPORT_SYMBOL_GPL(rtc_set_alarm
);
494 /* Called once per device from rtc_device_register */
495 int rtc_initialize_alarm(struct rtc_device
*rtc
, struct rtc_wkalrm
*alarm
)
500 err
= rtc_valid_tm(&alarm
->time
);
504 err
= rtc_read_time(rtc
, &now
);
508 err
= mutex_lock_interruptible(&rtc
->ops_lock
);
512 rtc
->aie_timer
.node
.expires
= rtc_tm_to_ktime(alarm
->time
);
513 rtc
->aie_timer
.period
= 0;
515 /* Alarm has to be enabled & in the future for us to enqueue it */
516 if (alarm
->enabled
&& (rtc_tm_to_ktime(now
) <
517 rtc
->aie_timer
.node
.expires
)) {
519 rtc
->aie_timer
.enabled
= 1;
520 timerqueue_add(&rtc
->timerqueue
, &rtc
->aie_timer
.node
);
521 trace_rtc_timer_enqueue(&rtc
->aie_timer
);
523 mutex_unlock(&rtc
->ops_lock
);
526 EXPORT_SYMBOL_GPL(rtc_initialize_alarm
);
528 int rtc_alarm_irq_enable(struct rtc_device
*rtc
, unsigned int enabled
)
530 int err
= mutex_lock_interruptible(&rtc
->ops_lock
);
534 if (rtc
->aie_timer
.enabled
!= enabled
) {
536 err
= rtc_timer_enqueue(rtc
, &rtc
->aie_timer
);
538 rtc_timer_remove(rtc
, &rtc
->aie_timer
);
545 else if (!rtc
->ops
->alarm_irq_enable
)
548 err
= rtc
->ops
->alarm_irq_enable(rtc
->dev
.parent
, enabled
);
550 mutex_unlock(&rtc
->ops_lock
);
552 trace_rtc_alarm_irq_enable(enabled
, err
);
555 EXPORT_SYMBOL_GPL(rtc_alarm_irq_enable
);
557 int rtc_update_irq_enable(struct rtc_device
*rtc
, unsigned int enabled
)
559 int err
= mutex_lock_interruptible(&rtc
->ops_lock
);
563 #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
564 if (enabled
== 0 && rtc
->uie_irq_active
) {
565 mutex_unlock(&rtc
->ops_lock
);
566 return rtc_dev_update_irq_enable_emul(rtc
, 0);
569 /* make sure we're changing state */
570 if (rtc
->uie_rtctimer
.enabled
== enabled
)
573 if (rtc
->uie_unsupported
) {
582 __rtc_read_time(rtc
, &tm
);
583 onesec
= ktime_set(1, 0);
584 now
= rtc_tm_to_ktime(tm
);
585 rtc
->uie_rtctimer
.node
.expires
= ktime_add(now
, onesec
);
586 rtc
->uie_rtctimer
.period
= ktime_set(1, 0);
587 err
= rtc_timer_enqueue(rtc
, &rtc
->uie_rtctimer
);
589 rtc_timer_remove(rtc
, &rtc
->uie_rtctimer
);
592 mutex_unlock(&rtc
->ops_lock
);
593 #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
595 * Enable emulation if the driver did not provide
596 * the update_irq_enable function pointer or if returned
597 * -EINVAL to signal that it has been configured without
598 * interrupts or that are not available at the moment.
601 err
= rtc_dev_update_irq_enable_emul(rtc
, enabled
);
606 EXPORT_SYMBOL_GPL(rtc_update_irq_enable
);
610 * rtc_handle_legacy_irq - AIE, UIE and PIE event hook
611 * @rtc: pointer to the rtc device
613 * This function is called when an AIE, UIE or PIE mode interrupt
614 * has occurred (or been emulated).
616 * Triggers the registered irq_task function callback.
618 void rtc_handle_legacy_irq(struct rtc_device
*rtc
, int num
, int mode
)
622 /* mark one irq of the appropriate mode */
623 spin_lock_irqsave(&rtc
->irq_lock
, flags
);
624 rtc
->irq_data
= (rtc
->irq_data
+ (num
<< 8)) | (RTC_IRQF
|mode
);
625 spin_unlock_irqrestore(&rtc
->irq_lock
, flags
);
627 wake_up_interruptible(&rtc
->irq_queue
);
628 kill_fasync(&rtc
->async_queue
, SIGIO
, POLL_IN
);
633 * rtc_aie_update_irq - AIE mode rtctimer hook
634 * @private: pointer to the rtc_device
636 * This functions is called when the aie_timer expires.
638 void rtc_aie_update_irq(void *private)
640 struct rtc_device
*rtc
= (struct rtc_device
*)private;
641 rtc_handle_legacy_irq(rtc
, 1, RTC_AF
);
646 * rtc_uie_update_irq - UIE mode rtctimer hook
647 * @private: pointer to the rtc_device
649 * This functions is called when the uie_timer expires.
651 void rtc_uie_update_irq(void *private)
653 struct rtc_device
*rtc
= (struct rtc_device
*)private;
654 rtc_handle_legacy_irq(rtc
, 1, RTC_UF
);
659 * rtc_pie_update_irq - PIE mode hrtimer hook
660 * @timer: pointer to the pie mode hrtimer
662 * This function is used to emulate PIE mode interrupts
663 * using an hrtimer. This function is called when the periodic
666 enum hrtimer_restart
rtc_pie_update_irq(struct hrtimer
*timer
)
668 struct rtc_device
*rtc
;
671 rtc
= container_of(timer
, struct rtc_device
, pie_timer
);
673 period
= NSEC_PER_SEC
/ rtc
->irq_freq
;
674 count
= hrtimer_forward_now(timer
, period
);
676 rtc_handle_legacy_irq(rtc
, count
, RTC_PF
);
678 return HRTIMER_RESTART
;
682 * rtc_update_irq - Triggered when a RTC interrupt occurs.
683 * @rtc: the rtc device
684 * @num: how many irqs are being reported (usually one)
685 * @events: mask of RTC_IRQF with one or more of RTC_PF, RTC_AF, RTC_UF
688 void rtc_update_irq(struct rtc_device
*rtc
,
689 unsigned long num
, unsigned long events
)
691 if (IS_ERR_OR_NULL(rtc
))
694 pm_stay_awake(rtc
->dev
.parent
);
695 schedule_work(&rtc
->irqwork
);
697 EXPORT_SYMBOL_GPL(rtc_update_irq
);
699 static int __rtc_match(struct device
*dev
, const void *data
)
701 const char *name
= data
;
703 if (strcmp(dev_name(dev
), name
) == 0)
708 struct rtc_device
*rtc_class_open(const char *name
)
711 struct rtc_device
*rtc
= NULL
;
713 dev
= class_find_device(rtc_class
, NULL
, name
, __rtc_match
);
715 rtc
= to_rtc_device(dev
);
718 if (!try_module_get(rtc
->owner
)) {
726 EXPORT_SYMBOL_GPL(rtc_class_open
);
728 void rtc_class_close(struct rtc_device
*rtc
)
730 module_put(rtc
->owner
);
731 put_device(&rtc
->dev
);
733 EXPORT_SYMBOL_GPL(rtc_class_close
);
735 static int rtc_update_hrtimer(struct rtc_device
*rtc
, int enabled
)
738 * We always cancel the timer here first, because otherwise
739 * we could run into BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
740 * when we manage to start the timer before the callback
741 * returns HRTIMER_RESTART.
743 * We cannot use hrtimer_cancel() here as a running callback
744 * could be blocked on rtc->irq_task_lock and hrtimer_cancel()
745 * would spin forever.
747 if (hrtimer_try_to_cancel(&rtc
->pie_timer
) < 0)
751 ktime_t period
= NSEC_PER_SEC
/ rtc
->irq_freq
;
753 hrtimer_start(&rtc
->pie_timer
, period
, HRTIMER_MODE_REL
);
759 * rtc_irq_set_state - enable/disable 2^N Hz periodic IRQs
760 * @rtc: the rtc device
761 * @task: currently registered with rtc_irq_register()
762 * @enabled: true to enable periodic IRQs
765 * Note that rtc_irq_set_freq() should previously have been used to
766 * specify the desired frequency of periodic IRQ.
768 int rtc_irq_set_state(struct rtc_device
*rtc
, int enabled
)
772 while (rtc_update_hrtimer(rtc
, enabled
) < 0)
775 rtc
->pie_enabled
= enabled
;
777 trace_rtc_irq_set_state(enabled
, err
);
782 * rtc_irq_set_freq - set 2^N Hz periodic IRQ frequency for IRQ
783 * @rtc: the rtc device
784 * @task: currently registered with rtc_irq_register()
785 * @freq: positive frequency
788 * Note that rtc_irq_set_state() is used to enable or disable the
791 int rtc_irq_set_freq(struct rtc_device
*rtc
, int freq
)
795 if (freq
<= 0 || freq
> RTC_MAX_FREQ
)
798 rtc
->irq_freq
= freq
;
799 while (rtc
->pie_enabled
&& rtc_update_hrtimer(rtc
, 1) < 0)
802 trace_rtc_irq_set_freq(freq
, err
);
807 * rtc_timer_enqueue - Adds a rtc_timer to the rtc_device timerqueue
809 * @timer timer being added.
811 * Enqueues a timer onto the rtc devices timerqueue and sets
812 * the next alarm event appropriately.
814 * Sets the enabled bit on the added timer.
816 * Must hold ops_lock for proper serialization of timerqueue
818 static int rtc_timer_enqueue(struct rtc_device
*rtc
, struct rtc_timer
*timer
)
820 struct timerqueue_node
*next
= timerqueue_getnext(&rtc
->timerqueue
);
825 __rtc_read_time(rtc
, &tm
);
826 now
= rtc_tm_to_ktime(tm
);
828 /* Skip over expired timers */
830 if (next
->expires
>= now
)
832 next
= timerqueue_iterate_next(next
);
835 timerqueue_add(&rtc
->timerqueue
, &timer
->node
);
836 trace_rtc_timer_enqueue(timer
);
837 if (!next
|| ktime_before(timer
->node
.expires
, next
->expires
)) {
838 struct rtc_wkalrm alarm
;
840 alarm
.time
= rtc_ktime_to_tm(timer
->node
.expires
);
842 err
= __rtc_set_alarm(rtc
, &alarm
);
844 pm_stay_awake(rtc
->dev
.parent
);
845 schedule_work(&rtc
->irqwork
);
847 timerqueue_del(&rtc
->timerqueue
, &timer
->node
);
848 trace_rtc_timer_dequeue(timer
);
856 static void rtc_alarm_disable(struct rtc_device
*rtc
)
858 if (!rtc
->ops
|| !rtc
->ops
->alarm_irq_enable
)
861 rtc
->ops
->alarm_irq_enable(rtc
->dev
.parent
, false);
862 trace_rtc_alarm_irq_enable(0, 0);
866 * rtc_timer_remove - Removes a rtc_timer from the rtc_device timerqueue
868 * @timer timer being removed.
870 * Removes a timer onto the rtc devices timerqueue and sets
871 * the next alarm event appropriately.
873 * Clears the enabled bit on the removed timer.
875 * Must hold ops_lock for proper serialization of timerqueue
877 static void rtc_timer_remove(struct rtc_device
*rtc
, struct rtc_timer
*timer
)
879 struct timerqueue_node
*next
= timerqueue_getnext(&rtc
->timerqueue
);
880 timerqueue_del(&rtc
->timerqueue
, &timer
->node
);
881 trace_rtc_timer_dequeue(timer
);
883 if (next
== &timer
->node
) {
884 struct rtc_wkalrm alarm
;
886 next
= timerqueue_getnext(&rtc
->timerqueue
);
888 rtc_alarm_disable(rtc
);
891 alarm
.time
= rtc_ktime_to_tm(next
->expires
);
893 err
= __rtc_set_alarm(rtc
, &alarm
);
895 pm_stay_awake(rtc
->dev
.parent
);
896 schedule_work(&rtc
->irqwork
);
902 * rtc_timer_do_work - Expires rtc timers
904 * @timer timer being removed.
906 * Expires rtc timers. Reprograms next alarm event if needed.
907 * Called via worktask.
909 * Serializes access to timerqueue via ops_lock mutex
911 void rtc_timer_do_work(struct work_struct
*work
)
913 struct rtc_timer
*timer
;
914 struct timerqueue_node
*next
;
918 struct rtc_device
*rtc
=
919 container_of(work
, struct rtc_device
, irqwork
);
921 mutex_lock(&rtc
->ops_lock
);
923 __rtc_read_time(rtc
, &tm
);
924 now
= rtc_tm_to_ktime(tm
);
925 while ((next
= timerqueue_getnext(&rtc
->timerqueue
))) {
926 if (next
->expires
> now
)
930 timer
= container_of(next
, struct rtc_timer
, node
);
931 timerqueue_del(&rtc
->timerqueue
, &timer
->node
);
932 trace_rtc_timer_dequeue(timer
);
935 timer
->func(timer
->private_data
);
937 trace_rtc_timer_fired(timer
);
938 /* Re-add/fwd periodic timers */
939 if (ktime_to_ns(timer
->period
)) {
940 timer
->node
.expires
= ktime_add(timer
->node
.expires
,
943 timerqueue_add(&rtc
->timerqueue
, &timer
->node
);
944 trace_rtc_timer_enqueue(timer
);
950 struct rtc_wkalrm alarm
;
954 alarm
.time
= rtc_ktime_to_tm(next
->expires
);
957 err
= __rtc_set_alarm(rtc
, &alarm
);
964 timer
= container_of(next
, struct rtc_timer
, node
);
965 timerqueue_del(&rtc
->timerqueue
, &timer
->node
);
966 trace_rtc_timer_dequeue(timer
);
968 dev_err(&rtc
->dev
, "__rtc_set_alarm: err=%d\n", err
);
972 rtc_alarm_disable(rtc
);
974 pm_relax(rtc
->dev
.parent
);
975 mutex_unlock(&rtc
->ops_lock
);
979 /* rtc_timer_init - Initializes an rtc_timer
980 * @timer: timer to be intiialized
981 * @f: function pointer to be called when timer fires
982 * @data: private data passed to function pointer
984 * Kernel interface to initializing an rtc_timer.
986 void rtc_timer_init(struct rtc_timer
*timer
, void (*f
)(void *p
), void *data
)
988 timerqueue_init(&timer
->node
);
991 timer
->private_data
= data
;
994 /* rtc_timer_start - Sets an rtc_timer to fire in the future
995 * @ rtc: rtc device to be used
996 * @ timer: timer being set
997 * @ expires: time at which to expire the timer
998 * @ period: period that the timer will recur
1000 * Kernel interface to set an rtc_timer
1002 int rtc_timer_start(struct rtc_device
*rtc
, struct rtc_timer
*timer
,
1003 ktime_t expires
, ktime_t period
)
1006 mutex_lock(&rtc
->ops_lock
);
1008 rtc_timer_remove(rtc
, timer
);
1010 timer
->node
.expires
= expires
;
1011 timer
->period
= period
;
1013 ret
= rtc_timer_enqueue(rtc
, timer
);
1015 mutex_unlock(&rtc
->ops_lock
);
1019 /* rtc_timer_cancel - Stops an rtc_timer
1020 * @ rtc: rtc device to be used
1021 * @ timer: timer being set
1023 * Kernel interface to cancel an rtc_timer
1025 void rtc_timer_cancel(struct rtc_device
*rtc
, struct rtc_timer
*timer
)
1027 mutex_lock(&rtc
->ops_lock
);
1029 rtc_timer_remove(rtc
, timer
);
1030 mutex_unlock(&rtc
->ops_lock
);
1034 * rtc_read_offset - Read the amount of rtc offset in parts per billion
1035 * @ rtc: rtc device to be used
1036 * @ offset: the offset in parts per billion
1038 * see below for details.
1040 * Kernel interface to read rtc clock offset
1041 * Returns 0 on success, or a negative number on error.
1042 * If read_offset() is not implemented for the rtc, return -EINVAL
1044 int rtc_read_offset(struct rtc_device
*rtc
, long *offset
)
1051 if (!rtc
->ops
->read_offset
)
1054 mutex_lock(&rtc
->ops_lock
);
1055 ret
= rtc
->ops
->read_offset(rtc
->dev
.parent
, offset
);
1056 mutex_unlock(&rtc
->ops_lock
);
1058 trace_rtc_read_offset(*offset
, ret
);
1063 * rtc_set_offset - Adjusts the duration of the average second
1064 * @ rtc: rtc device to be used
1065 * @ offset: the offset in parts per billion
1067 * Some rtc's allow an adjustment to the average duration of a second
1068 * to compensate for differences in the actual clock rate due to temperature,
1069 * the crystal, capacitor, etc.
1071 * The adjustment applied is as follows:
1072 * t = t0 * (1 + offset * 1e-9)
1073 * where t0 is the measured length of 1 RTC second with offset = 0
1075 * Kernel interface to adjust an rtc clock offset.
1076 * Return 0 on success, or a negative number on error.
1077 * If the rtc offset is not setable (or not implemented), return -EINVAL
1079 int rtc_set_offset(struct rtc_device
*rtc
, long offset
)
1086 if (!rtc
->ops
->set_offset
)
1089 mutex_lock(&rtc
->ops_lock
);
1090 ret
= rtc
->ops
->set_offset(rtc
->dev
.parent
, offset
);
1091 mutex_unlock(&rtc
->ops_lock
);
1093 trace_rtc_set_offset(offset
, ret
);