]>
git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/rtc/interface.c
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_dbg(&rtc
->dev
, "read_time: fail to read: %d\n",
39 err
= rtc_valid_tm(tm
);
41 dev_dbg(&rtc
->dev
, "read_time: rtc_time isn't valid\n");
46 int rtc_read_time(struct rtc_device
*rtc
, struct rtc_time
*tm
)
50 err
= mutex_lock_interruptible(&rtc
->ops_lock
);
54 err
= __rtc_read_time(rtc
, tm
);
55 mutex_unlock(&rtc
->ops_lock
);
58 EXPORT_SYMBOL_GPL(rtc_read_time
);
60 int rtc_set_time(struct rtc_device
*rtc
, struct rtc_time
*tm
)
64 err
= rtc_valid_tm(tm
);
68 #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
69 uie
= rtc
->uie_rtctimer
.enabled
|| rtc
->uie_irq_active
;
71 uie
= rtc
->uie_rtctimer
.enabled
;
74 err
= rtc_update_irq_enable(rtc
, 0);
79 err
= mutex_lock_interruptible(&rtc
->ops_lock
);
85 else if (rtc
->ops
->set_time
)
86 err
= rtc
->ops
->set_time(rtc
->dev
.parent
, tm
);
87 else if (rtc
->ops
->set_mmss64
) {
88 time64_t secs64
= rtc_tm_to_time64(tm
);
90 err
= rtc
->ops
->set_mmss64(rtc
->dev
.parent
, secs64
);
91 } else if (rtc
->ops
->set_mmss
) {
92 time64_t secs64
= rtc_tm_to_time64(tm
);
93 err
= rtc
->ops
->set_mmss(rtc
->dev
.parent
, secs64
);
97 pm_stay_awake(rtc
->dev
.parent
);
98 mutex_unlock(&rtc
->ops_lock
);
99 /* A timer might have just expired */
100 schedule_work(&rtc
->irqwork
);
103 err
= rtc_update_irq_enable(rtc
, 1);
110 EXPORT_SYMBOL_GPL(rtc_set_time
);
112 static int rtc_read_alarm_internal(struct rtc_device
*rtc
, struct rtc_wkalrm
*alarm
)
116 err
= mutex_lock_interruptible(&rtc
->ops_lock
);
120 if (rtc
->ops
== NULL
)
122 else if (!rtc
->ops
->read_alarm
)
127 alarm
->time
.tm_sec
= -1;
128 alarm
->time
.tm_min
= -1;
129 alarm
->time
.tm_hour
= -1;
130 alarm
->time
.tm_mday
= -1;
131 alarm
->time
.tm_mon
= -1;
132 alarm
->time
.tm_year
= -1;
133 alarm
->time
.tm_wday
= -1;
134 alarm
->time
.tm_yday
= -1;
135 alarm
->time
.tm_isdst
= -1;
136 err
= rtc
->ops
->read_alarm(rtc
->dev
.parent
, alarm
);
139 mutex_unlock(&rtc
->ops_lock
);
143 int __rtc_read_alarm(struct rtc_device
*rtc
, struct rtc_wkalrm
*alarm
)
146 struct rtc_time before
, now
;
148 time64_t t_now
, t_alm
;
149 enum { none
, day
, month
, year
} missing
= none
;
152 /* The lower level RTC driver may return -1 in some fields,
153 * creating invalid alarm->time values, for reasons like:
155 * - The hardware may not be capable of filling them in;
156 * many alarms match only on time-of-day fields, not
157 * day/month/year calendar data.
159 * - Some hardware uses illegal values as "wildcard" match
160 * values, which non-Linux firmware (like a BIOS) may try
161 * to set up as e.g. "alarm 15 minutes after each hour".
162 * Linux uses only oneshot alarms.
164 * When we see that here, we deal with it by using values from
165 * a current RTC timestamp for any missing (-1) values. The
166 * RTC driver prevents "periodic alarm" modes.
168 * But this can be racey, because some fields of the RTC timestamp
169 * may have wrapped in the interval since we read the RTC alarm,
170 * which would lead to us inserting inconsistent values in place
173 * Reading the alarm and timestamp in the reverse sequence
174 * would have the same race condition, and not solve the issue.
176 * So, we must first read the RTC timestamp,
177 * then read the RTC alarm value,
178 * and then read a second RTC timestamp.
180 * If any fields of the second timestamp have changed
181 * when compared with the first timestamp, then we know
182 * our timestamp may be inconsistent with that used by
183 * the low-level rtc_read_alarm_internal() function.
185 * So, when the two timestamps disagree, we just loop and do
186 * the process again to get a fully consistent set of values.
188 * This could all instead be done in the lower level driver,
189 * but since more than one lower level RTC implementation needs it,
190 * then it's probably best best to do it here instead of there..
193 /* Get the "before" timestamp */
194 err
= rtc_read_time(rtc
, &before
);
199 memcpy(&before
, &now
, sizeof(struct rtc_time
));
202 /* get the RTC alarm values, which may be incomplete */
203 err
= rtc_read_alarm_internal(rtc
, alarm
);
207 /* full-function RTCs won't have such missing fields */
208 if (rtc_valid_tm(&alarm
->time
) == 0)
211 /* get the "after" timestamp, to detect wrapped fields */
212 err
= rtc_read_time(rtc
, &now
);
216 /* note that tm_sec is a "don't care" value here: */
217 } while ( before
.tm_min
!= now
.tm_min
218 || before
.tm_hour
!= now
.tm_hour
219 || before
.tm_mon
!= now
.tm_mon
220 || before
.tm_year
!= now
.tm_year
);
222 /* Fill in the missing alarm fields using the timestamp; we
223 * know there's at least one since alarm->time is invalid.
225 if (alarm
->time
.tm_sec
== -1)
226 alarm
->time
.tm_sec
= now
.tm_sec
;
227 if (alarm
->time
.tm_min
== -1)
228 alarm
->time
.tm_min
= now
.tm_min
;
229 if (alarm
->time
.tm_hour
== -1)
230 alarm
->time
.tm_hour
= now
.tm_hour
;
232 /* For simplicity, only support date rollover for now */
233 if (alarm
->time
.tm_mday
< 1 || alarm
->time
.tm_mday
> 31) {
234 alarm
->time
.tm_mday
= now
.tm_mday
;
237 if ((unsigned)alarm
->time
.tm_mon
>= 12) {
238 alarm
->time
.tm_mon
= now
.tm_mon
;
242 if (alarm
->time
.tm_year
== -1) {
243 alarm
->time
.tm_year
= now
.tm_year
;
248 /* Can't proceed if alarm is still invalid after replacing
251 err
= rtc_valid_tm(&alarm
->time
);
255 /* with luck, no rollover is needed */
256 t_now
= rtc_tm_to_time64(&now
);
257 t_alm
= rtc_tm_to_time64(&alarm
->time
);
263 /* 24 hour rollover ... if it's now 10am Monday, an alarm that
264 * that will trigger at 5am will do so at 5am Tuesday, which
265 * could also be in the next month or year. This is a common
266 * case, especially for PCs.
269 dev_dbg(&rtc
->dev
, "alarm rollover: %s\n", "day");
270 t_alm
+= 24 * 60 * 60;
271 rtc_time64_to_tm(t_alm
, &alarm
->time
);
274 /* Month rollover ... if it's the 31th, an alarm on the 3rd will
275 * be next month. An alarm matching on the 30th, 29th, or 28th
276 * may end up in the month after that! Many newer PCs support
277 * this type of alarm.
280 dev_dbg(&rtc
->dev
, "alarm rollover: %s\n", "month");
282 if (alarm
->time
.tm_mon
< 11)
283 alarm
->time
.tm_mon
++;
285 alarm
->time
.tm_mon
= 0;
286 alarm
->time
.tm_year
++;
288 days
= rtc_month_days(alarm
->time
.tm_mon
,
289 alarm
->time
.tm_year
);
290 } while (days
< alarm
->time
.tm_mday
);
293 /* Year rollover ... easy except for leap years! */
295 dev_dbg(&rtc
->dev
, "alarm rollover: %s\n", "year");
297 alarm
->time
.tm_year
++;
298 } while (!is_leap_year(alarm
->time
.tm_year
+ 1900)
299 && rtc_valid_tm(&alarm
->time
) != 0);
303 dev_warn(&rtc
->dev
, "alarm rollover not handled\n");
306 err
= rtc_valid_tm(&alarm
->time
);
310 dev_warn(&rtc
->dev
, "invalid alarm value: %d-%d-%d %d:%d:%d\n",
311 alarm
->time
.tm_year
+ 1900, alarm
->time
.tm_mon
+ 1,
312 alarm
->time
.tm_mday
, alarm
->time
.tm_hour
, alarm
->time
.tm_min
,
319 int rtc_read_alarm(struct rtc_device
*rtc
, struct rtc_wkalrm
*alarm
)
323 err
= mutex_lock_interruptible(&rtc
->ops_lock
);
326 if (rtc
->ops
== NULL
)
328 else if (!rtc
->ops
->read_alarm
)
331 memset(alarm
, 0, sizeof(struct rtc_wkalrm
));
332 alarm
->enabled
= rtc
->aie_timer
.enabled
;
333 alarm
->time
= rtc_ktime_to_tm(rtc
->aie_timer
.node
.expires
);
335 mutex_unlock(&rtc
->ops_lock
);
339 EXPORT_SYMBOL_GPL(rtc_read_alarm
);
341 static int __rtc_set_alarm(struct rtc_device
*rtc
, struct rtc_wkalrm
*alarm
)
344 time64_t now
, scheduled
;
347 err
= rtc_valid_tm(&alarm
->time
);
350 scheduled
= rtc_tm_to_time64(&alarm
->time
);
352 /* Make sure we're not setting alarms in the past */
353 err
= __rtc_read_time(rtc
, &tm
);
356 now
= rtc_tm_to_time64(&tm
);
357 if (scheduled
<= now
)
360 * XXX - We just checked to make sure the alarm time is not
361 * in the past, but there is still a race window where if
362 * the is alarm set for the next second and the second ticks
363 * over right here, before we set the alarm.
368 else if (!rtc
->ops
->set_alarm
)
371 err
= rtc
->ops
->set_alarm(rtc
->dev
.parent
, alarm
);
376 int rtc_set_alarm(struct rtc_device
*rtc
, struct rtc_wkalrm
*alarm
)
382 else if (!rtc
->ops
->set_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
= 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
= 0;
426 /* Alarm has to be enabled & in the future for us to enqueue it */
427 if (alarm
->enabled
&& (rtc_tm_to_ktime(now
) <
428 rtc
->aie_timer
.node
.expires
)) {
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
);
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
= 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 (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
= 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
)
789 struct timerqueue_node
*next
= timerqueue_getnext(&rtc
->timerqueue
);
794 __rtc_read_time(rtc
, &tm
);
795 now
= rtc_tm_to_ktime(tm
);
797 /* Skip over expired timers */
799 if (next
->expires
>= now
)
801 next
= timerqueue_iterate_next(next
);
804 timerqueue_add(&rtc
->timerqueue
, &timer
->node
);
805 if (!next
|| ktime_before(timer
->node
.expires
, next
->expires
)) {
806 struct rtc_wkalrm alarm
;
808 alarm
.time
= rtc_ktime_to_tm(timer
->node
.expires
);
810 err
= __rtc_set_alarm(rtc
, &alarm
);
812 pm_stay_awake(rtc
->dev
.parent
);
813 schedule_work(&rtc
->irqwork
);
815 timerqueue_del(&rtc
->timerqueue
, &timer
->node
);
823 static void rtc_alarm_disable(struct rtc_device
*rtc
)
825 if (!rtc
->ops
|| !rtc
->ops
->alarm_irq_enable
)
828 rtc
->ops
->alarm_irq_enable(rtc
->dev
.parent
, false);
832 * rtc_timer_remove - Removes a rtc_timer from the rtc_device timerqueue
834 * @timer timer being removed.
836 * Removes a timer onto the rtc devices timerqueue and sets
837 * the next alarm event appropriately.
839 * Clears the enabled bit on the removed timer.
841 * Must hold ops_lock for proper serialization of timerqueue
843 static void rtc_timer_remove(struct rtc_device
*rtc
, struct rtc_timer
*timer
)
845 struct timerqueue_node
*next
= timerqueue_getnext(&rtc
->timerqueue
);
846 timerqueue_del(&rtc
->timerqueue
, &timer
->node
);
848 if (next
== &timer
->node
) {
849 struct rtc_wkalrm alarm
;
851 next
= timerqueue_getnext(&rtc
->timerqueue
);
853 rtc_alarm_disable(rtc
);
856 alarm
.time
= rtc_ktime_to_tm(next
->expires
);
858 err
= __rtc_set_alarm(rtc
, &alarm
);
860 pm_stay_awake(rtc
->dev
.parent
);
861 schedule_work(&rtc
->irqwork
);
867 * rtc_timer_do_work - Expires rtc timers
869 * @timer timer being removed.
871 * Expires rtc timers. Reprograms next alarm event if needed.
872 * Called via worktask.
874 * Serializes access to timerqueue via ops_lock mutex
876 void rtc_timer_do_work(struct work_struct
*work
)
878 struct rtc_timer
*timer
;
879 struct timerqueue_node
*next
;
883 struct rtc_device
*rtc
=
884 container_of(work
, struct rtc_device
, irqwork
);
886 mutex_lock(&rtc
->ops_lock
);
888 __rtc_read_time(rtc
, &tm
);
889 now
= rtc_tm_to_ktime(tm
);
890 while ((next
= timerqueue_getnext(&rtc
->timerqueue
))) {
891 if (next
->expires
> now
)
895 timer
= container_of(next
, struct rtc_timer
, node
);
896 timerqueue_del(&rtc
->timerqueue
, &timer
->node
);
898 if (timer
->task
.func
)
899 timer
->task
.func(timer
->task
.private_data
);
901 /* Re-add/fwd periodic timers */
902 if (ktime_to_ns(timer
->period
)) {
903 timer
->node
.expires
= ktime_add(timer
->node
.expires
,
906 timerqueue_add(&rtc
->timerqueue
, &timer
->node
);
912 struct rtc_wkalrm alarm
;
916 alarm
.time
= rtc_ktime_to_tm(next
->expires
);
919 err
= __rtc_set_alarm(rtc
, &alarm
);
926 timer
= container_of(next
, struct rtc_timer
, node
);
927 timerqueue_del(&rtc
->timerqueue
, &timer
->node
);
929 dev_err(&rtc
->dev
, "__rtc_set_alarm: err=%d\n", err
);
933 rtc_alarm_disable(rtc
);
935 pm_relax(rtc
->dev
.parent
);
936 mutex_unlock(&rtc
->ops_lock
);
940 /* rtc_timer_init - Initializes an rtc_timer
941 * @timer: timer to be intiialized
942 * @f: function pointer to be called when timer fires
943 * @data: private data passed to function pointer
945 * Kernel interface to initializing an rtc_timer.
947 void rtc_timer_init(struct rtc_timer
*timer
, void (*f
)(void *p
), void *data
)
949 timerqueue_init(&timer
->node
);
951 timer
->task
.func
= f
;
952 timer
->task
.private_data
= data
;
955 /* rtc_timer_start - Sets an rtc_timer to fire in the future
956 * @ rtc: rtc device to be used
957 * @ timer: timer being set
958 * @ expires: time at which to expire the timer
959 * @ period: period that the timer will recur
961 * Kernel interface to set an rtc_timer
963 int rtc_timer_start(struct rtc_device
*rtc
, struct rtc_timer
*timer
,
964 ktime_t expires
, ktime_t period
)
967 mutex_lock(&rtc
->ops_lock
);
969 rtc_timer_remove(rtc
, timer
);
971 timer
->node
.expires
= expires
;
972 timer
->period
= period
;
974 ret
= rtc_timer_enqueue(rtc
, timer
);
976 mutex_unlock(&rtc
->ops_lock
);
980 /* rtc_timer_cancel - Stops an rtc_timer
981 * @ rtc: rtc device to be used
982 * @ timer: timer being set
984 * Kernel interface to cancel an rtc_timer
986 void rtc_timer_cancel(struct rtc_device
*rtc
, struct rtc_timer
*timer
)
988 mutex_lock(&rtc
->ops_lock
);
990 rtc_timer_remove(rtc
, timer
);
991 mutex_unlock(&rtc
->ops_lock
);
995 * rtc_read_offset - Read the amount of rtc offset in parts per billion
996 * @ rtc: rtc device to be used
997 * @ offset: the offset in parts per billion
999 * see below for details.
1001 * Kernel interface to read rtc clock offset
1002 * Returns 0 on success, or a negative number on error.
1003 * If read_offset() is not implemented for the rtc, return -EINVAL
1005 int rtc_read_offset(struct rtc_device
*rtc
, long *offset
)
1012 if (!rtc
->ops
->read_offset
)
1015 mutex_lock(&rtc
->ops_lock
);
1016 ret
= rtc
->ops
->read_offset(rtc
->dev
.parent
, offset
);
1017 mutex_unlock(&rtc
->ops_lock
);
1022 * rtc_set_offset - Adjusts the duration of the average second
1023 * @ rtc: rtc device to be used
1024 * @ offset: the offset in parts per billion
1026 * Some rtc's allow an adjustment to the average duration of a second
1027 * to compensate for differences in the actual clock rate due to temperature,
1028 * the crystal, capacitor, etc.
1030 * The adjustment applied is as follows:
1031 * t = t0 * (1 + offset * 1e-9)
1032 * where t0 is the measured length of 1 RTC second with offset = 0
1034 * Kernel interface to adjust an rtc clock offset.
1035 * Return 0 on success, or a negative number on error.
1036 * If the rtc offset is not setable (or not implemented), return -EINVAL
1038 int rtc_set_offset(struct rtc_device
*rtc
, long offset
)
1045 if (!rtc
->ops
->set_offset
)
1048 mutex_lock(&rtc
->ops_lock
);
1049 ret
= rtc
->ops
->set_offset(rtc
->dev
.parent
, offset
);
1050 mutex_unlock(&rtc
->ops_lock
);