1 // SPDX-License-Identifier: GPL-2.0
3 * RTC subsystem, interface functions
5 * Copyright (C) 2005 Tower Technologies
6 * Author: Alessandro Zummo <a.zummo@towertech.it>
8 * based on arch/arm/common/rtctime.c
11 #include <linux/rtc.h>
12 #include <linux/sched.h>
13 #include <linux/module.h>
14 #include <linux/log2.h>
15 #include <linux/workqueue.h>
17 #define CREATE_TRACE_POINTS
18 #include <trace/events/rtc.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 void rtc_add_offset(struct rtc_device *rtc, struct rtc_time *tm)
27 if (!rtc->offset_secs)
30 secs = rtc_tm_to_time64(tm);
33 * Since the reading time values from RTC device are always in the RTC
34 * original valid range, but we need to skip the overlapped region
35 * between expanded range and original range, which is no need to add
38 if ((rtc->start_secs > rtc->range_min && secs >= rtc->start_secs) ||
39 (rtc->start_secs < rtc->range_min &&
40 secs <= (rtc->start_secs + rtc->range_max - rtc->range_min)))
43 rtc_time64_to_tm(secs + rtc->offset_secs, tm);
46 static void rtc_subtract_offset(struct rtc_device *rtc, struct rtc_time *tm)
50 if (!rtc->offset_secs)
53 secs = rtc_tm_to_time64(tm);
56 * If the setting time values are in the valid range of RTC hardware
57 * device, then no need to subtract the offset when setting time to RTC
58 * device. Otherwise we need to subtract the offset to make the time
59 * values are valid for RTC hardware device.
61 if (secs >= rtc->range_min && secs <= rtc->range_max)
64 rtc_time64_to_tm(secs - rtc->offset_secs, tm);
67 static int rtc_valid_range(struct rtc_device *rtc, struct rtc_time *tm)
69 if (rtc->range_min != rtc->range_max) {
70 time64_t time = rtc_tm_to_time64(tm);
71 time64_t range_min = rtc->set_start_time ? rtc->start_secs :
73 time64_t range_max = rtc->set_start_time ?
74 (rtc->start_secs + rtc->range_max - rtc->range_min) :
77 if (time < range_min || time > range_max)
84 static int __rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm)
90 } else if (!rtc->ops->read_time) {
93 memset(tm, 0, sizeof(struct rtc_time));
94 err = rtc->ops->read_time(rtc->dev.parent, tm);
96 dev_dbg(&rtc->dev, "read_time: fail to read: %d\n",
101 rtc_add_offset(rtc, tm);
103 err = rtc_valid_tm(tm);
105 dev_dbg(&rtc->dev, "read_time: rtc_time isn't valid\n");
110 int rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm)
114 err = mutex_lock_interruptible(&rtc->ops_lock);
118 err = __rtc_read_time(rtc, tm);
119 mutex_unlock(&rtc->ops_lock);
121 trace_rtc_read_time(rtc_tm_to_time64(tm), err);
124 EXPORT_SYMBOL_GPL(rtc_read_time);
126 int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm)
130 err = rtc_valid_tm(tm);
134 err = rtc_valid_range(rtc, tm);
138 rtc_subtract_offset(rtc, tm);
140 err = mutex_lock_interruptible(&rtc->ops_lock);
146 else if (rtc->ops->set_time)
147 err = rtc->ops->set_time(rtc->dev.parent, tm);
148 else if (rtc->ops->set_mmss64)
149 err = rtc->ops->set_mmss64(rtc->dev.parent,
150 rtc_tm_to_time64(tm));
151 else if (rtc->ops->set_mmss)
152 err = rtc->ops->set_mmss(rtc->dev.parent,
153 rtc_tm_to_time64(tm));
157 pm_stay_awake(rtc->dev.parent);
158 mutex_unlock(&rtc->ops_lock);
159 /* A timer might have just expired */
160 schedule_work(&rtc->irqwork);
162 trace_rtc_set_time(rtc_tm_to_time64(tm), err);
165 EXPORT_SYMBOL_GPL(rtc_set_time);
167 static int rtc_read_alarm_internal(struct rtc_device *rtc,
168 struct rtc_wkalrm *alarm)
172 err = mutex_lock_interruptible(&rtc->ops_lock);
178 } else if (!rtc->ops->read_alarm) {
183 alarm->time.tm_sec = -1;
184 alarm->time.tm_min = -1;
185 alarm->time.tm_hour = -1;
186 alarm->time.tm_mday = -1;
187 alarm->time.tm_mon = -1;
188 alarm->time.tm_year = -1;
189 alarm->time.tm_wday = -1;
190 alarm->time.tm_yday = -1;
191 alarm->time.tm_isdst = -1;
192 err = rtc->ops->read_alarm(rtc->dev.parent, alarm);
195 mutex_unlock(&rtc->ops_lock);
197 trace_rtc_read_alarm(rtc_tm_to_time64(&alarm->time), err);
201 int __rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
204 struct rtc_time before, now;
206 time64_t t_now, t_alm;
207 enum { none, day, month, year } missing = none;
210 /* The lower level RTC driver may return -1 in some fields,
211 * creating invalid alarm->time values, for reasons like:
213 * - The hardware may not be capable of filling them in;
214 * many alarms match only on time-of-day fields, not
215 * day/month/year calendar data.
217 * - Some hardware uses illegal values as "wildcard" match
218 * values, which non-Linux firmware (like a BIOS) may try
219 * to set up as e.g. "alarm 15 minutes after each hour".
220 * Linux uses only oneshot alarms.
222 * When we see that here, we deal with it by using values from
223 * a current RTC timestamp for any missing (-1) values. The
224 * RTC driver prevents "periodic alarm" modes.
226 * But this can be racey, because some fields of the RTC timestamp
227 * may have wrapped in the interval since we read the RTC alarm,
228 * which would lead to us inserting inconsistent values in place
231 * Reading the alarm and timestamp in the reverse sequence
232 * would have the same race condition, and not solve the issue.
234 * So, we must first read the RTC timestamp,
235 * then read the RTC alarm value,
236 * and then read a second RTC timestamp.
238 * If any fields of the second timestamp have changed
239 * when compared with the first timestamp, then we know
240 * our timestamp may be inconsistent with that used by
241 * the low-level rtc_read_alarm_internal() function.
243 * So, when the two timestamps disagree, we just loop and do
244 * the process again to get a fully consistent set of values.
246 * This could all instead be done in the lower level driver,
247 * but since more than one lower level RTC implementation needs it,
248 * then it's probably best best to do it here instead of there..
251 /* Get the "before" timestamp */
252 err = rtc_read_time(rtc, &before);
257 memcpy(&before, &now, sizeof(struct rtc_time));
260 /* get the RTC alarm values, which may be incomplete */
261 err = rtc_read_alarm_internal(rtc, alarm);
265 /* full-function RTCs won't have such missing fields */
266 if (rtc_valid_tm(&alarm->time) == 0) {
267 rtc_add_offset(rtc, &alarm->time);
271 /* get the "after" timestamp, to detect wrapped fields */
272 err = rtc_read_time(rtc, &now);
276 /* note that tm_sec is a "don't care" value here: */
277 } while (before.tm_min != now.tm_min ||
278 before.tm_hour != now.tm_hour ||
279 before.tm_mon != now.tm_mon ||
280 before.tm_year != now.tm_year);
282 /* Fill in the missing alarm fields using the timestamp; we
283 * know there's at least one since alarm->time is invalid.
285 if (alarm->time.tm_sec == -1)
286 alarm->time.tm_sec = now.tm_sec;
287 if (alarm->time.tm_min == -1)
288 alarm->time.tm_min = now.tm_min;
289 if (alarm->time.tm_hour == -1)
290 alarm->time.tm_hour = now.tm_hour;
292 /* For simplicity, only support date rollover for now */
293 if (alarm->time.tm_mday < 1 || alarm->time.tm_mday > 31) {
294 alarm->time.tm_mday = now.tm_mday;
297 if ((unsigned int)alarm->time.tm_mon >= 12) {
298 alarm->time.tm_mon = now.tm_mon;
302 if (alarm->time.tm_year == -1) {
303 alarm->time.tm_year = now.tm_year;
308 /* Can't proceed if alarm is still invalid after replacing
311 err = rtc_valid_tm(&alarm->time);
315 /* with luck, no rollover is needed */
316 t_now = rtc_tm_to_time64(&now);
317 t_alm = rtc_tm_to_time64(&alarm->time);
322 /* 24 hour rollover ... if it's now 10am Monday, an alarm that
323 * that will trigger at 5am will do so at 5am Tuesday, which
324 * could also be in the next month or year. This is a common
325 * case, especially for PCs.
328 dev_dbg(&rtc->dev, "alarm rollover: %s\n", "day");
329 t_alm += 24 * 60 * 60;
330 rtc_time64_to_tm(t_alm, &alarm->time);
333 /* Month rollover ... if it's the 31th, an alarm on the 3rd will
334 * be next month. An alarm matching on the 30th, 29th, or 28th
335 * may end up in the month after that! Many newer PCs support
336 * this type of alarm.
339 dev_dbg(&rtc->dev, "alarm rollover: %s\n", "month");
341 if (alarm->time.tm_mon < 11) {
342 alarm->time.tm_mon++;
344 alarm->time.tm_mon = 0;
345 alarm->time.tm_year++;
347 days = rtc_month_days(alarm->time.tm_mon,
348 alarm->time.tm_year);
349 } while (days < alarm->time.tm_mday);
352 /* Year rollover ... easy except for leap years! */
354 dev_dbg(&rtc->dev, "alarm rollover: %s\n", "year");
356 alarm->time.tm_year++;
357 } while (!is_leap_year(alarm->time.tm_year + 1900) &&
358 rtc_valid_tm(&alarm->time) != 0);
362 dev_warn(&rtc->dev, "alarm rollover not handled\n");
365 err = rtc_valid_tm(&alarm->time);
369 dev_warn(&rtc->dev, "invalid alarm value: %ptR\n",
375 int rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
379 err = mutex_lock_interruptible(&rtc->ops_lock);
384 } else if (!rtc->ops->read_alarm) {
387 memset(alarm, 0, sizeof(struct rtc_wkalrm));
388 alarm->enabled = rtc->aie_timer.enabled;
389 alarm->time = rtc_ktime_to_tm(rtc->aie_timer.node.expires);
391 mutex_unlock(&rtc->ops_lock);
393 trace_rtc_read_alarm(rtc_tm_to_time64(&alarm->time), err);
396 EXPORT_SYMBOL_GPL(rtc_read_alarm);
398 static int __rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
401 time64_t now, scheduled;
404 err = rtc_valid_tm(&alarm->time);
408 scheduled = rtc_tm_to_time64(&alarm->time);
410 /* Make sure we're not setting alarms in the past */
411 err = __rtc_read_time(rtc, &tm);
414 now = rtc_tm_to_time64(&tm);
415 if (scheduled <= now)
418 * XXX - We just checked to make sure the alarm time is not
419 * in the past, but there is still a race window where if
420 * the is alarm set for the next second and the second ticks
421 * over right here, before we set the alarm.
424 rtc_subtract_offset(rtc, &alarm->time);
428 else if (!rtc->ops->set_alarm)
431 err = rtc->ops->set_alarm(rtc->dev.parent, alarm);
433 trace_rtc_set_alarm(rtc_tm_to_time64(&alarm->time), err);
437 int rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
443 else if (!rtc->ops->set_alarm)
446 err = rtc_valid_tm(&alarm->time);
450 err = rtc_valid_range(rtc, &alarm->time);
454 err = mutex_lock_interruptible(&rtc->ops_lock);
457 if (rtc->aie_timer.enabled)
458 rtc_timer_remove(rtc, &rtc->aie_timer);
460 rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time);
461 rtc->aie_timer.period = 0;
463 err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
465 mutex_unlock(&rtc->ops_lock);
469 EXPORT_SYMBOL_GPL(rtc_set_alarm);
471 /* Called once per device from rtc_device_register */
472 int rtc_initialize_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
477 err = rtc_valid_tm(&alarm->time);
481 err = rtc_read_time(rtc, &now);
485 err = mutex_lock_interruptible(&rtc->ops_lock);
489 rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time);
490 rtc->aie_timer.period = 0;
492 /* Alarm has to be enabled & in the future for us to enqueue it */
493 if (alarm->enabled && (rtc_tm_to_ktime(now) <
494 rtc->aie_timer.node.expires)) {
495 rtc->aie_timer.enabled = 1;
496 timerqueue_add(&rtc->timerqueue, &rtc->aie_timer.node);
497 trace_rtc_timer_enqueue(&rtc->aie_timer);
499 mutex_unlock(&rtc->ops_lock);
502 EXPORT_SYMBOL_GPL(rtc_initialize_alarm);
504 int rtc_alarm_irq_enable(struct rtc_device *rtc, unsigned int enabled)
508 err = mutex_lock_interruptible(&rtc->ops_lock);
512 if (rtc->aie_timer.enabled != enabled) {
514 err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
516 rtc_timer_remove(rtc, &rtc->aie_timer);
523 else if (!rtc->ops->alarm_irq_enable)
526 err = rtc->ops->alarm_irq_enable(rtc->dev.parent, enabled);
528 mutex_unlock(&rtc->ops_lock);
530 trace_rtc_alarm_irq_enable(enabled, err);
533 EXPORT_SYMBOL_GPL(rtc_alarm_irq_enable);
535 int rtc_update_irq_enable(struct rtc_device *rtc, unsigned int enabled)
539 err = mutex_lock_interruptible(&rtc->ops_lock);
543 #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
544 if (enabled == 0 && rtc->uie_irq_active) {
545 mutex_unlock(&rtc->ops_lock);
546 return rtc_dev_update_irq_enable_emul(rtc, 0);
549 /* make sure we're changing state */
550 if (rtc->uie_rtctimer.enabled == enabled)
553 if (rtc->uie_unsupported) {
562 __rtc_read_time(rtc, &tm);
563 onesec = ktime_set(1, 0);
564 now = rtc_tm_to_ktime(tm);
565 rtc->uie_rtctimer.node.expires = ktime_add(now, onesec);
566 rtc->uie_rtctimer.period = ktime_set(1, 0);
567 err = rtc_timer_enqueue(rtc, &rtc->uie_rtctimer);
569 rtc_timer_remove(rtc, &rtc->uie_rtctimer);
573 mutex_unlock(&rtc->ops_lock);
574 #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
576 * Enable emulation if the driver returned -EINVAL to signal that it has
577 * been configured without interrupts or they are not available at the
581 err = rtc_dev_update_irq_enable_emul(rtc, enabled);
585 EXPORT_SYMBOL_GPL(rtc_update_irq_enable);
588 * rtc_handle_legacy_irq - AIE, UIE and PIE event hook
589 * @rtc: pointer to the rtc device
591 * This function is called when an AIE, UIE or PIE mode interrupt
592 * has occurred (or been emulated).
595 void rtc_handle_legacy_irq(struct rtc_device *rtc, int num, int mode)
599 /* mark one irq of the appropriate mode */
600 spin_lock_irqsave(&rtc->irq_lock, flags);
601 rtc->irq_data = (rtc->irq_data + (num << 8)) | (RTC_IRQF | mode);
602 spin_unlock_irqrestore(&rtc->irq_lock, flags);
604 wake_up_interruptible(&rtc->irq_queue);
605 kill_fasync(&rtc->async_queue, SIGIO, POLL_IN);
609 * rtc_aie_update_irq - AIE mode rtctimer hook
610 * @rtc: pointer to the rtc_device
612 * This functions is called when the aie_timer expires.
614 void rtc_aie_update_irq(struct rtc_device *rtc)
616 rtc_handle_legacy_irq(rtc, 1, RTC_AF);
620 * rtc_uie_update_irq - UIE mode rtctimer hook
621 * @rtc: pointer to the rtc_device
623 * This functions is called when the uie_timer expires.
625 void rtc_uie_update_irq(struct rtc_device *rtc)
627 rtc_handle_legacy_irq(rtc, 1, RTC_UF);
631 * rtc_pie_update_irq - PIE mode hrtimer hook
632 * @timer: pointer to the pie mode hrtimer
634 * This function is used to emulate PIE mode interrupts
635 * using an hrtimer. This function is called when the periodic
638 enum hrtimer_restart rtc_pie_update_irq(struct hrtimer *timer)
640 struct rtc_device *rtc;
644 rtc = container_of(timer, struct rtc_device, pie_timer);
646 period = NSEC_PER_SEC / rtc->irq_freq;
647 count = hrtimer_forward_now(timer, period);
649 rtc_handle_legacy_irq(rtc, count, RTC_PF);
651 return HRTIMER_RESTART;
655 * rtc_update_irq - Triggered when a RTC interrupt occurs.
656 * @rtc: the rtc device
657 * @num: how many irqs are being reported (usually one)
658 * @events: mask of RTC_IRQF with one or more of RTC_PF, RTC_AF, RTC_UF
661 void rtc_update_irq(struct rtc_device *rtc,
662 unsigned long num, unsigned long events)
664 if (IS_ERR_OR_NULL(rtc))
667 pm_stay_awake(rtc->dev.parent);
668 schedule_work(&rtc->irqwork);
670 EXPORT_SYMBOL_GPL(rtc_update_irq);
672 static int __rtc_match(struct device *dev, const void *data)
674 const char *name = data;
676 if (strcmp(dev_name(dev), name) == 0)
681 struct rtc_device *rtc_class_open(const char *name)
684 struct rtc_device *rtc = NULL;
686 dev = class_find_device(rtc_class, NULL, name, __rtc_match);
688 rtc = to_rtc_device(dev);
691 if (!try_module_get(rtc->owner)) {
699 EXPORT_SYMBOL_GPL(rtc_class_open);
701 void rtc_class_close(struct rtc_device *rtc)
703 module_put(rtc->owner);
704 put_device(&rtc->dev);
706 EXPORT_SYMBOL_GPL(rtc_class_close);
708 static int rtc_update_hrtimer(struct rtc_device *rtc, int enabled)
711 * We always cancel the timer here first, because otherwise
712 * we could run into BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
713 * when we manage to start the timer before the callback
714 * returns HRTIMER_RESTART.
716 * We cannot use hrtimer_cancel() here as a running callback
717 * could be blocked on rtc->irq_task_lock and hrtimer_cancel()
718 * would spin forever.
720 if (hrtimer_try_to_cancel(&rtc->pie_timer) < 0)
724 ktime_t period = NSEC_PER_SEC / rtc->irq_freq;
726 hrtimer_start(&rtc->pie_timer, period, HRTIMER_MODE_REL);
732 * rtc_irq_set_state - enable/disable 2^N Hz periodic IRQs
733 * @rtc: the rtc device
734 * @enabled: true to enable periodic IRQs
737 * Note that rtc_irq_set_freq() should previously have been used to
738 * specify the desired frequency of periodic IRQ.
740 int rtc_irq_set_state(struct rtc_device *rtc, int enabled)
744 while (rtc_update_hrtimer(rtc, enabled) < 0)
747 rtc->pie_enabled = enabled;
749 trace_rtc_irq_set_state(enabled, err);
754 * rtc_irq_set_freq - set 2^N Hz periodic IRQ frequency for IRQ
755 * @rtc: the rtc device
756 * @freq: positive frequency
759 * Note that rtc_irq_set_state() is used to enable or disable the
762 int rtc_irq_set_freq(struct rtc_device *rtc, int freq)
766 if (freq <= 0 || freq > RTC_MAX_FREQ)
769 rtc->irq_freq = freq;
770 while (rtc->pie_enabled && rtc_update_hrtimer(rtc, 1) < 0)
773 trace_rtc_irq_set_freq(freq, err);
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)
791 struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue);
796 __rtc_read_time(rtc, &tm);
797 now = rtc_tm_to_ktime(tm);
799 /* Skip over expired timers */
801 if (next->expires >= now)
803 next = timerqueue_iterate_next(next);
806 timerqueue_add(&rtc->timerqueue, &timer->node);
807 trace_rtc_timer_enqueue(timer);
808 if (!next || ktime_before(timer->node.expires, next->expires)) {
809 struct rtc_wkalrm alarm;
812 alarm.time = rtc_ktime_to_tm(timer->node.expires);
814 err = __rtc_set_alarm(rtc, &alarm);
816 pm_stay_awake(rtc->dev.parent);
817 schedule_work(&rtc->irqwork);
819 timerqueue_del(&rtc->timerqueue, &timer->node);
820 trace_rtc_timer_dequeue(timer);
828 static void rtc_alarm_disable(struct rtc_device *rtc)
830 if (!rtc->ops || !rtc->ops->alarm_irq_enable)
833 rtc->ops->alarm_irq_enable(rtc->dev.parent, false);
834 trace_rtc_alarm_irq_enable(0, 0);
838 * rtc_timer_remove - Removes a rtc_timer from the rtc_device timerqueue
840 * @timer timer being removed.
842 * Removes a timer onto the rtc devices timerqueue and sets
843 * the next alarm event appropriately.
845 * Clears the enabled bit on the removed timer.
847 * Must hold ops_lock for proper serialization of timerqueue
849 static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer)
851 struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue);
853 timerqueue_del(&rtc->timerqueue, &timer->node);
854 trace_rtc_timer_dequeue(timer);
856 if (next == &timer->node) {
857 struct rtc_wkalrm alarm;
860 next = timerqueue_getnext(&rtc->timerqueue);
862 rtc_alarm_disable(rtc);
865 alarm.time = rtc_ktime_to_tm(next->expires);
867 err = __rtc_set_alarm(rtc, &alarm);
869 pm_stay_awake(rtc->dev.parent);
870 schedule_work(&rtc->irqwork);
876 * rtc_timer_do_work - Expires rtc timers
878 * @timer timer being removed.
880 * Expires rtc timers. Reprograms next alarm event if needed.
881 * Called via worktask.
883 * Serializes access to timerqueue via ops_lock mutex
885 void rtc_timer_do_work(struct work_struct *work)
887 struct rtc_timer *timer;
888 struct timerqueue_node *next;
892 struct rtc_device *rtc =
893 container_of(work, struct rtc_device, irqwork);
895 mutex_lock(&rtc->ops_lock);
897 __rtc_read_time(rtc, &tm);
898 now = rtc_tm_to_ktime(tm);
899 while ((next = timerqueue_getnext(&rtc->timerqueue))) {
900 if (next->expires > now)
904 timer = container_of(next, struct rtc_timer, node);
905 timerqueue_del(&rtc->timerqueue, &timer->node);
906 trace_rtc_timer_dequeue(timer);
909 timer->func(timer->rtc);
911 trace_rtc_timer_fired(timer);
912 /* Re-add/fwd periodic timers */
913 if (ktime_to_ns(timer->period)) {
914 timer->node.expires = ktime_add(timer->node.expires,
917 timerqueue_add(&rtc->timerqueue, &timer->node);
918 trace_rtc_timer_enqueue(timer);
924 struct rtc_wkalrm alarm;
928 alarm.time = rtc_ktime_to_tm(next->expires);
931 err = __rtc_set_alarm(rtc, &alarm);
938 timer = container_of(next, struct rtc_timer, node);
939 timerqueue_del(&rtc->timerqueue, &timer->node);
940 trace_rtc_timer_dequeue(timer);
942 dev_err(&rtc->dev, "__rtc_set_alarm: err=%d\n", err);
946 rtc_alarm_disable(rtc);
949 pm_relax(rtc->dev.parent);
950 mutex_unlock(&rtc->ops_lock);
953 /* rtc_timer_init - Initializes an rtc_timer
954 * @timer: timer to be intiialized
955 * @f: function pointer to be called when timer fires
956 * @rtc: pointer to the rtc_device
958 * Kernel interface to initializing an rtc_timer.
960 void rtc_timer_init(struct rtc_timer *timer, void (*f)(struct rtc_device *r),
961 struct rtc_device *rtc)
963 timerqueue_init(&timer->node);
969 /* rtc_timer_start - Sets an rtc_timer to fire in the future
970 * @ rtc: rtc device to be used
971 * @ timer: timer being set
972 * @ expires: time at which to expire the timer
973 * @ period: period that the timer will recur
975 * Kernel interface to set an rtc_timer
977 int rtc_timer_start(struct rtc_device *rtc, struct rtc_timer *timer,
978 ktime_t expires, ktime_t period)
982 mutex_lock(&rtc->ops_lock);
984 rtc_timer_remove(rtc, timer);
986 timer->node.expires = expires;
987 timer->period = period;
989 ret = rtc_timer_enqueue(rtc, timer);
991 mutex_unlock(&rtc->ops_lock);
995 /* rtc_timer_cancel - Stops an rtc_timer
996 * @ rtc: rtc device to be used
997 * @ timer: timer being set
999 * Kernel interface to cancel an rtc_timer
1001 void rtc_timer_cancel(struct rtc_device *rtc, struct rtc_timer *timer)
1003 mutex_lock(&rtc->ops_lock);
1005 rtc_timer_remove(rtc, timer);
1006 mutex_unlock(&rtc->ops_lock);
1010 * rtc_read_offset - Read the amount of rtc offset in parts per billion
1011 * @ rtc: rtc device to be used
1012 * @ offset: the offset in parts per billion
1014 * see below for details.
1016 * Kernel interface to read rtc clock offset
1017 * Returns 0 on success, or a negative number on error.
1018 * If read_offset() is not implemented for the rtc, return -EINVAL
1020 int rtc_read_offset(struct rtc_device *rtc, long *offset)
1027 if (!rtc->ops->read_offset)
1030 mutex_lock(&rtc->ops_lock);
1031 ret = rtc->ops->read_offset(rtc->dev.parent, offset);
1032 mutex_unlock(&rtc->ops_lock);
1034 trace_rtc_read_offset(*offset, ret);
1039 * rtc_set_offset - Adjusts the duration of the average second
1040 * @ rtc: rtc device to be used
1041 * @ offset: the offset in parts per billion
1043 * Some rtc's allow an adjustment to the average duration of a second
1044 * to compensate for differences in the actual clock rate due to temperature,
1045 * the crystal, capacitor, etc.
1047 * The adjustment applied is as follows:
1048 * t = t0 * (1 + offset * 1e-9)
1049 * where t0 is the measured length of 1 RTC second with offset = 0
1051 * Kernel interface to adjust an rtc clock offset.
1052 * Return 0 on success, or a negative number on error.
1053 * If the rtc offset is not setable (or not implemented), return -EINVAL
1055 int rtc_set_offset(struct rtc_device *rtc, long offset)
1062 if (!rtc->ops->set_offset)
1065 mutex_lock(&rtc->ops_lock);
1066 ret = rtc->ops->set_offset(rtc->dev.parent, offset);
1067 mutex_unlock(&rtc->ops_lock);
1069 trace_rtc_set_offset(offset, ret);