Lines Matching +full:rtc +full:-
1 // SPDX-License-Identifier: GPL-2.0
3 * RTC subsystem, interface functions
11 #include <linux/rtc.h>
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) in rtc_add_offset() argument
27 if (!rtc->offset_secs) in rtc_add_offset()
33 * Since the reading time values from RTC device are always in the RTC in rtc_add_offset()
38 if ((rtc->start_secs > rtc->range_min && secs >= rtc->start_secs) || in rtc_add_offset()
39 (rtc->start_secs < rtc->range_min && in rtc_add_offset()
40 secs <= (rtc->start_secs + rtc->range_max - rtc->range_min))) in rtc_add_offset()
43 rtc_time64_to_tm(secs + rtc->offset_secs, tm); in rtc_add_offset()
46 static void rtc_subtract_offset(struct rtc_device *rtc, struct rtc_time *tm) in rtc_subtract_offset() argument
50 if (!rtc->offset_secs) in rtc_subtract_offset()
56 * If the setting time values are in the valid range of RTC hardware in rtc_subtract_offset()
57 * device, then no need to subtract the offset when setting time to RTC in rtc_subtract_offset()
59 * values are valid for RTC hardware device. in rtc_subtract_offset()
61 if (secs >= rtc->range_min && secs <= rtc->range_max) in rtc_subtract_offset()
64 rtc_time64_to_tm(secs - rtc->offset_secs, tm); in rtc_subtract_offset()
67 static int rtc_valid_range(struct rtc_device *rtc, struct rtc_time *tm) in rtc_valid_range() argument
69 if (rtc->range_min != rtc->range_max) { in rtc_valid_range()
71 time64_t range_min = rtc->set_start_time ? rtc->start_secs : in rtc_valid_range()
72 rtc->range_min; in rtc_valid_range()
73 timeu64_t range_max = rtc->set_start_time ? in rtc_valid_range()
74 (rtc->start_secs + rtc->range_max - rtc->range_min) : in rtc_valid_range()
75 rtc->range_max; in rtc_valid_range()
78 return -ERANGE; in rtc_valid_range()
84 static int __rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm) in __rtc_read_time() argument
88 if (!rtc->ops) { in __rtc_read_time()
89 err = -ENODEV; in __rtc_read_time()
90 } else if (!rtc->ops->read_time) { in __rtc_read_time()
91 err = -EINVAL; in __rtc_read_time()
94 err = rtc->ops->read_time(rtc->dev.parent, tm); in __rtc_read_time()
96 dev_dbg(&rtc->dev, "read_time: fail to read: %d\n", in __rtc_read_time()
101 rtc_add_offset(rtc, tm); in __rtc_read_time()
105 dev_dbg(&rtc->dev, "read_time: rtc_time isn't valid\n"); in __rtc_read_time()
110 int rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm) in rtc_read_time() argument
114 err = mutex_lock_interruptible(&rtc->ops_lock); in rtc_read_time()
118 err = __rtc_read_time(rtc, tm); in rtc_read_time()
119 mutex_unlock(&rtc->ops_lock); in rtc_read_time()
126 int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm) in rtc_set_time() argument
134 err = rtc_valid_range(rtc, tm); in rtc_set_time()
138 rtc_subtract_offset(rtc, tm); in rtc_set_time()
141 uie = rtc->uie_rtctimer.enabled || rtc->uie_irq_active; in rtc_set_time()
143 uie = rtc->uie_rtctimer.enabled; in rtc_set_time()
146 err = rtc_update_irq_enable(rtc, 0); in rtc_set_time()
151 err = mutex_lock_interruptible(&rtc->ops_lock); in rtc_set_time()
155 if (!rtc->ops) in rtc_set_time()
156 err = -ENODEV; in rtc_set_time()
157 else if (rtc->ops->set_time) in rtc_set_time()
158 err = rtc->ops->set_time(rtc->dev.parent, tm); in rtc_set_time()
160 err = -EINVAL; in rtc_set_time()
162 pm_stay_awake(rtc->dev.parent); in rtc_set_time()
163 mutex_unlock(&rtc->ops_lock); in rtc_set_time()
165 schedule_work(&rtc->irqwork); in rtc_set_time()
168 err = rtc_update_irq_enable(rtc, 1); in rtc_set_time()
178 static int rtc_read_alarm_internal(struct rtc_device *rtc, in rtc_read_alarm_internal() argument
183 err = mutex_lock_interruptible(&rtc->ops_lock); in rtc_read_alarm_internal()
187 if (!rtc->ops) { in rtc_read_alarm_internal()
188 err = -ENODEV; in rtc_read_alarm_internal()
189 } else if (!test_bit(RTC_FEATURE_ALARM, rtc->features) || !rtc->ops->read_alarm) { in rtc_read_alarm_internal()
190 err = -EINVAL; in rtc_read_alarm_internal()
192 alarm->enabled = 0; in rtc_read_alarm_internal()
193 alarm->pending = 0; in rtc_read_alarm_internal()
194 alarm->time.tm_sec = -1; in rtc_read_alarm_internal()
195 alarm->time.tm_min = -1; in rtc_read_alarm_internal()
196 alarm->time.tm_hour = -1; in rtc_read_alarm_internal()
197 alarm->time.tm_mday = -1; in rtc_read_alarm_internal()
198 alarm->time.tm_mon = -1; in rtc_read_alarm_internal()
199 alarm->time.tm_year = -1; in rtc_read_alarm_internal()
200 alarm->time.tm_wday = -1; in rtc_read_alarm_internal()
201 alarm->time.tm_yday = -1; in rtc_read_alarm_internal()
202 alarm->time.tm_isdst = -1; in rtc_read_alarm_internal()
203 err = rtc->ops->read_alarm(rtc->dev.parent, alarm); in rtc_read_alarm_internal()
206 mutex_unlock(&rtc->ops_lock); in rtc_read_alarm_internal()
208 trace_rtc_read_alarm(rtc_tm_to_time64(&alarm->time), err); in rtc_read_alarm_internal()
212 int __rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm) in __rtc_read_alarm() argument
221 /* The lower level RTC driver may return -1 in some fields, in __rtc_read_alarm()
222 * creating invalid alarm->time values, for reasons like: in __rtc_read_alarm()
224 * - The hardware may not be capable of filling them in; in __rtc_read_alarm()
225 * many alarms match only on time-of-day fields, not in __rtc_read_alarm()
228 * - Some hardware uses illegal values as "wildcard" match in __rtc_read_alarm()
229 * values, which non-Linux firmware (like a BIOS) may try in __rtc_read_alarm()
234 * a current RTC timestamp for any missing (-1) values. The in __rtc_read_alarm()
235 * RTC driver prevents "periodic alarm" modes. in __rtc_read_alarm()
237 * But this can be racey, because some fields of the RTC timestamp in __rtc_read_alarm()
238 * may have wrapped in the interval since we read the RTC alarm, in __rtc_read_alarm()
240 * of the -1 fields. in __rtc_read_alarm()
245 * So, we must first read the RTC timestamp, in __rtc_read_alarm()
246 * then read the RTC alarm value, in __rtc_read_alarm()
247 * and then read a second RTC timestamp. in __rtc_read_alarm()
252 * the low-level rtc_read_alarm_internal() function. in __rtc_read_alarm()
258 * but since more than one lower level RTC implementation needs it, in __rtc_read_alarm()
263 err = rtc_read_time(rtc, &before); in __rtc_read_alarm()
271 /* get the RTC alarm values, which may be incomplete */ in __rtc_read_alarm()
272 err = rtc_read_alarm_internal(rtc, alarm); in __rtc_read_alarm()
276 /* full-function RTCs won't have such missing fields */ in __rtc_read_alarm()
277 err = rtc_valid_tm(&alarm->time); in __rtc_read_alarm()
282 err = rtc_read_time(rtc, &now); in __rtc_read_alarm()
293 * know there's at least one since alarm->time is invalid. in __rtc_read_alarm()
295 if (alarm->time.tm_sec == -1) in __rtc_read_alarm()
296 alarm->time.tm_sec = now.tm_sec; in __rtc_read_alarm()
297 if (alarm->time.tm_min == -1) in __rtc_read_alarm()
298 alarm->time.tm_min = now.tm_min; in __rtc_read_alarm()
299 if (alarm->time.tm_hour == -1) in __rtc_read_alarm()
300 alarm->time.tm_hour = now.tm_hour; in __rtc_read_alarm()
303 if (alarm->time.tm_mday < 1 || alarm->time.tm_mday > 31) { in __rtc_read_alarm()
304 alarm->time.tm_mday = now.tm_mday; in __rtc_read_alarm()
307 if ((unsigned int)alarm->time.tm_mon >= 12) { in __rtc_read_alarm()
308 alarm->time.tm_mon = now.tm_mon; in __rtc_read_alarm()
312 if (alarm->time.tm_year == -1) { in __rtc_read_alarm()
313 alarm->time.tm_year = now.tm_year; in __rtc_read_alarm()
321 err = rtc_valid_tm(&alarm->time); in __rtc_read_alarm()
327 t_alm = rtc_tm_to_time64(&alarm->time); in __rtc_read_alarm()
338 dev_dbg(&rtc->dev, "alarm rollover: %s\n", "day"); in __rtc_read_alarm()
340 rtc_time64_to_tm(t_alm, &alarm->time); in __rtc_read_alarm()
349 dev_dbg(&rtc->dev, "alarm rollover: %s\n", "month"); in __rtc_read_alarm()
351 if (alarm->time.tm_mon < 11) { in __rtc_read_alarm()
352 alarm->time.tm_mon++; in __rtc_read_alarm()
354 alarm->time.tm_mon = 0; in __rtc_read_alarm()
355 alarm->time.tm_year++; in __rtc_read_alarm()
357 days = rtc_month_days(alarm->time.tm_mon, in __rtc_read_alarm()
358 alarm->time.tm_year); in __rtc_read_alarm()
359 } while (days < alarm->time.tm_mday); in __rtc_read_alarm()
364 dev_dbg(&rtc->dev, "alarm rollover: %s\n", "year"); in __rtc_read_alarm()
366 alarm->time.tm_year++; in __rtc_read_alarm()
367 } while (!is_leap_year(alarm->time.tm_year + 1900) && in __rtc_read_alarm()
368 rtc_valid_tm(&alarm->time) != 0); in __rtc_read_alarm()
372 dev_warn(&rtc->dev, "alarm rollover not handled\n"); in __rtc_read_alarm()
375 err = rtc_valid_tm(&alarm->time); in __rtc_read_alarm()
378 if (err && alarm->enabled) in __rtc_read_alarm()
379 dev_warn(&rtc->dev, "invalid alarm value: %ptR\n", in __rtc_read_alarm()
380 &alarm->time); in __rtc_read_alarm()
382 rtc_add_offset(rtc, &alarm->time); in __rtc_read_alarm()
387 int rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm) in rtc_read_alarm() argument
391 err = mutex_lock_interruptible(&rtc->ops_lock); in rtc_read_alarm()
394 if (!rtc->ops) { in rtc_read_alarm()
395 err = -ENODEV; in rtc_read_alarm()
396 } else if (!test_bit(RTC_FEATURE_ALARM, rtc->features)) { in rtc_read_alarm()
397 err = -EINVAL; in rtc_read_alarm()
400 alarm->enabled = rtc->aie_timer.enabled; in rtc_read_alarm()
401 alarm->time = rtc_ktime_to_tm(rtc->aie_timer.node.expires); in rtc_read_alarm()
403 mutex_unlock(&rtc->ops_lock); in rtc_read_alarm()
405 trace_rtc_read_alarm(rtc_tm_to_time64(&alarm->time), err); in rtc_read_alarm()
410 static int __rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm) in __rtc_set_alarm() argument
416 err = rtc_valid_tm(&alarm->time); in __rtc_set_alarm()
420 scheduled = rtc_tm_to_time64(&alarm->time); in __rtc_set_alarm()
423 err = __rtc_read_time(rtc, &tm); in __rtc_set_alarm()
429 return -ETIME; in __rtc_set_alarm()
431 * XXX - We just checked to make sure the alarm time is not in __rtc_set_alarm()
437 rtc_subtract_offset(rtc, &alarm->time); in __rtc_set_alarm()
439 if (!rtc->ops) in __rtc_set_alarm()
440 err = -ENODEV; in __rtc_set_alarm()
441 else if (!test_bit(RTC_FEATURE_ALARM, rtc->features)) in __rtc_set_alarm()
442 err = -EINVAL; in __rtc_set_alarm()
444 err = rtc->ops->set_alarm(rtc->dev.parent, alarm); in __rtc_set_alarm()
446 trace_rtc_set_alarm(rtc_tm_to_time64(&alarm->time), err); in __rtc_set_alarm()
450 int rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm) in rtc_set_alarm() argument
455 if (!rtc->ops) in rtc_set_alarm()
456 return -ENODEV; in rtc_set_alarm()
457 else if (!test_bit(RTC_FEATURE_ALARM, rtc->features)) in rtc_set_alarm()
458 return -EINVAL; in rtc_set_alarm()
460 err = rtc_valid_tm(&alarm->time); in rtc_set_alarm()
464 err = rtc_valid_range(rtc, &alarm->time); in rtc_set_alarm()
468 err = mutex_lock_interruptible(&rtc->ops_lock); in rtc_set_alarm()
471 if (rtc->aie_timer.enabled) in rtc_set_alarm()
472 rtc_timer_remove(rtc, &rtc->aie_timer); in rtc_set_alarm()
474 alarm_time = rtc_tm_to_ktime(alarm->time); in rtc_set_alarm()
479 if (test_bit(RTC_FEATURE_ALARM_RES_MINUTE, rtc->features)) in rtc_set_alarm()
480 alarm_time = ktime_sub_ns(alarm_time, (u64)alarm->time.tm_sec * NSEC_PER_SEC); in rtc_set_alarm()
482 rtc->aie_timer.node.expires = alarm_time; in rtc_set_alarm()
483 rtc->aie_timer.period = 0; in rtc_set_alarm()
484 if (alarm->enabled) in rtc_set_alarm()
485 err = rtc_timer_enqueue(rtc, &rtc->aie_timer); in rtc_set_alarm()
487 mutex_unlock(&rtc->ops_lock); in rtc_set_alarm()
494 int rtc_initialize_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm) in rtc_initialize_alarm() argument
499 err = rtc_valid_tm(&alarm->time); in rtc_initialize_alarm()
503 err = rtc_read_time(rtc, &now); in rtc_initialize_alarm()
507 err = mutex_lock_interruptible(&rtc->ops_lock); in rtc_initialize_alarm()
511 rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time); in rtc_initialize_alarm()
512 rtc->aie_timer.period = 0; in rtc_initialize_alarm()
515 if (alarm->enabled && (rtc_tm_to_ktime(now) < in rtc_initialize_alarm()
516 rtc->aie_timer.node.expires)) { in rtc_initialize_alarm()
517 rtc->aie_timer.enabled = 1; in rtc_initialize_alarm()
518 timerqueue_add(&rtc->timerqueue, &rtc->aie_timer.node); in rtc_initialize_alarm()
519 trace_rtc_timer_enqueue(&rtc->aie_timer); in rtc_initialize_alarm()
521 mutex_unlock(&rtc->ops_lock); in rtc_initialize_alarm()
526 int rtc_alarm_irq_enable(struct rtc_device *rtc, unsigned int enabled) in rtc_alarm_irq_enable() argument
530 err = mutex_lock_interruptible(&rtc->ops_lock); in rtc_alarm_irq_enable()
534 if (rtc->aie_timer.enabled != enabled) { in rtc_alarm_irq_enable()
536 err = rtc_timer_enqueue(rtc, &rtc->aie_timer); in rtc_alarm_irq_enable()
538 rtc_timer_remove(rtc, &rtc->aie_timer); in rtc_alarm_irq_enable()
543 else if (!rtc->ops) in rtc_alarm_irq_enable()
544 err = -ENODEV; in rtc_alarm_irq_enable()
545 else if (!test_bit(RTC_FEATURE_ALARM, rtc->features) || !rtc->ops->alarm_irq_enable) in rtc_alarm_irq_enable()
546 err = -EINVAL; in rtc_alarm_irq_enable()
548 err = rtc->ops->alarm_irq_enable(rtc->dev.parent, enabled); in rtc_alarm_irq_enable()
550 mutex_unlock(&rtc->ops_lock); in rtc_alarm_irq_enable()
557 int rtc_update_irq_enable(struct rtc_device *rtc, unsigned int enabled) in rtc_update_irq_enable() argument
561 err = mutex_lock_interruptible(&rtc->ops_lock); in rtc_update_irq_enable()
566 if (enabled == 0 && rtc->uie_irq_active) { in rtc_update_irq_enable()
567 mutex_unlock(&rtc->ops_lock); in rtc_update_irq_enable()
568 return rtc_dev_update_irq_enable_emul(rtc, 0); in rtc_update_irq_enable()
572 if (rtc->uie_rtctimer.enabled == enabled) in rtc_update_irq_enable()
575 if (!test_bit(RTC_FEATURE_UPDATE_INTERRUPT, rtc->features) || in rtc_update_irq_enable()
576 !test_bit(RTC_FEATURE_ALARM, rtc->features)) { in rtc_update_irq_enable()
577 mutex_unlock(&rtc->ops_lock); in rtc_update_irq_enable()
579 return rtc_dev_update_irq_enable_emul(rtc, enabled); in rtc_update_irq_enable()
581 return -EINVAL; in rtc_update_irq_enable()
589 err = __rtc_read_time(rtc, &tm); in rtc_update_irq_enable()
594 rtc->uie_rtctimer.node.expires = ktime_add(now, onesec); in rtc_update_irq_enable()
595 rtc->uie_rtctimer.period = ktime_set(1, 0); in rtc_update_irq_enable()
596 err = rtc_timer_enqueue(rtc, &rtc->uie_rtctimer); in rtc_update_irq_enable()
598 rtc_timer_remove(rtc, &rtc->uie_rtctimer); in rtc_update_irq_enable()
602 mutex_unlock(&rtc->ops_lock); in rtc_update_irq_enable()
609 * rtc_handle_legacy_irq - AIE, UIE and PIE event hook
610 * @rtc: pointer to the rtc device
618 void rtc_handle_legacy_irq(struct rtc_device *rtc, int num, int mode) in rtc_handle_legacy_irq() argument
623 spin_lock_irqsave(&rtc->irq_lock, flags); in rtc_handle_legacy_irq()
624 rtc->irq_data = (rtc->irq_data + (num << 8)) | (RTC_IRQF | mode); in rtc_handle_legacy_irq()
625 spin_unlock_irqrestore(&rtc->irq_lock, flags); in rtc_handle_legacy_irq()
627 wake_up_interruptible(&rtc->irq_queue); in rtc_handle_legacy_irq()
628 kill_fasync(&rtc->async_queue, SIGIO, POLL_IN); in rtc_handle_legacy_irq()
632 * rtc_aie_update_irq - AIE mode rtctimer hook
633 * @rtc: pointer to the rtc_device
637 void rtc_aie_update_irq(struct rtc_device *rtc) in rtc_aie_update_irq() argument
639 rtc_handle_legacy_irq(rtc, 1, RTC_AF); in rtc_aie_update_irq()
643 * rtc_uie_update_irq - UIE mode rtctimer hook
644 * @rtc: pointer to the rtc_device
648 void rtc_uie_update_irq(struct rtc_device *rtc) in rtc_uie_update_irq() argument
650 rtc_handle_legacy_irq(rtc, 1, RTC_UF); in rtc_uie_update_irq()
654 * rtc_pie_update_irq - PIE mode hrtimer hook
663 struct rtc_device *rtc; in rtc_pie_update_irq() local
667 rtc = container_of(timer, struct rtc_device, pie_timer); in rtc_pie_update_irq()
669 period = NSEC_PER_SEC / rtc->irq_freq; in rtc_pie_update_irq()
672 rtc_handle_legacy_irq(rtc, count, RTC_PF); in rtc_pie_update_irq()
678 * rtc_update_irq - Triggered when a RTC interrupt occurs.
679 * @rtc: the rtc device
684 void rtc_update_irq(struct rtc_device *rtc, in rtc_update_irq() argument
687 if (IS_ERR_OR_NULL(rtc)) in rtc_update_irq()
690 pm_stay_awake(rtc->dev.parent); in rtc_update_irq()
691 schedule_work(&rtc->irqwork); in rtc_update_irq()
698 struct rtc_device *rtc = NULL; in rtc_class_open() local
702 rtc = to_rtc_device(dev); in rtc_class_open()
704 if (rtc) { in rtc_class_open()
705 if (!try_module_get(rtc->owner)) { in rtc_class_open()
707 rtc = NULL; in rtc_class_open()
711 return rtc; in rtc_class_open()
715 void rtc_class_close(struct rtc_device *rtc) in rtc_class_close() argument
717 module_put(rtc->owner); in rtc_class_close()
718 put_device(&rtc->dev); in rtc_class_close()
722 static int rtc_update_hrtimer(struct rtc_device *rtc, int enabled) in rtc_update_hrtimer() argument
726 * we could run into BUG_ON(timer->state != HRTIMER_STATE_CALLBACK); in rtc_update_hrtimer()
731 * could be blocked on rtc->irq_task_lock and hrtimer_cancel() in rtc_update_hrtimer()
734 if (hrtimer_try_to_cancel(&rtc->pie_timer) < 0) in rtc_update_hrtimer()
735 return -1; in rtc_update_hrtimer()
738 ktime_t period = NSEC_PER_SEC / rtc->irq_freq; in rtc_update_hrtimer()
740 hrtimer_start(&rtc->pie_timer, period, HRTIMER_MODE_REL); in rtc_update_hrtimer()
746 * rtc_irq_set_state - enable/disable 2^N Hz periodic IRQs
747 * @rtc: the rtc device
754 int rtc_irq_set_state(struct rtc_device *rtc, int enabled) in rtc_irq_set_state() argument
758 while (rtc_update_hrtimer(rtc, enabled) < 0) in rtc_irq_set_state()
761 rtc->pie_enabled = enabled; in rtc_irq_set_state()
768 * rtc_irq_set_freq - set 2^N Hz periodic IRQ frequency for IRQ
769 * @rtc: the rtc device
776 int rtc_irq_set_freq(struct rtc_device *rtc, int freq) in rtc_irq_set_freq() argument
781 return -EINVAL; in rtc_irq_set_freq()
783 rtc->irq_freq = freq; in rtc_irq_set_freq()
784 while (rtc->pie_enabled && rtc_update_hrtimer(rtc, 1) < 0) in rtc_irq_set_freq()
792 * rtc_timer_enqueue - Adds a rtc_timer to the rtc_device timerqueue
793 * @rtc: rtc device
796 * Enqueues a timer onto the rtc devices timerqueue and sets
803 static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer) in rtc_timer_enqueue() argument
805 struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue); in rtc_timer_enqueue()
810 err = __rtc_read_time(rtc, &tm); in rtc_timer_enqueue()
814 timer->enabled = 1; in rtc_timer_enqueue()
819 if (next->expires >= now) in rtc_timer_enqueue()
824 timerqueue_add(&rtc->timerqueue, &timer->node); in rtc_timer_enqueue()
826 if (!next || ktime_before(timer->node.expires, next->expires)) { in rtc_timer_enqueue()
829 alarm.time = rtc_ktime_to_tm(timer->node.expires); in rtc_timer_enqueue()
831 err = __rtc_set_alarm(rtc, &alarm); in rtc_timer_enqueue()
832 if (err == -ETIME) { in rtc_timer_enqueue()
833 pm_stay_awake(rtc->dev.parent); in rtc_timer_enqueue()
834 schedule_work(&rtc->irqwork); in rtc_timer_enqueue()
836 timerqueue_del(&rtc->timerqueue, &timer->node); in rtc_timer_enqueue()
838 timer->enabled = 0; in rtc_timer_enqueue()
845 static void rtc_alarm_disable(struct rtc_device *rtc) in rtc_alarm_disable() argument
847 if (!rtc->ops || !test_bit(RTC_FEATURE_ALARM, rtc->features) || !rtc->ops->alarm_irq_enable) in rtc_alarm_disable()
850 rtc->ops->alarm_irq_enable(rtc->dev.parent, false); in rtc_alarm_disable()
855 * rtc_timer_remove - Removes a rtc_timer from the rtc_device timerqueue
856 * @rtc: rtc device
859 * Removes a timer onto the rtc devices timerqueue and sets
866 static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer) in rtc_timer_remove() argument
868 struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue); in rtc_timer_remove()
870 timerqueue_del(&rtc->timerqueue, &timer->node); in rtc_timer_remove()
872 timer->enabled = 0; in rtc_timer_remove()
873 if (next == &timer->node) { in rtc_timer_remove()
877 next = timerqueue_getnext(&rtc->timerqueue); in rtc_timer_remove()
879 rtc_alarm_disable(rtc); in rtc_timer_remove()
882 alarm.time = rtc_ktime_to_tm(next->expires); in rtc_timer_remove()
884 err = __rtc_set_alarm(rtc, &alarm); in rtc_timer_remove()
885 if (err == -ETIME) { in rtc_timer_remove()
886 pm_stay_awake(rtc->dev.parent); in rtc_timer_remove()
887 schedule_work(&rtc->irqwork); in rtc_timer_remove()
893 * rtc_timer_do_work - Expires rtc timers
896 * Expires rtc timers. Reprograms next alarm event if needed.
908 struct rtc_device *rtc = in rtc_timer_do_work() local
911 mutex_lock(&rtc->ops_lock); in rtc_timer_do_work()
913 __rtc_read_time(rtc, &tm); in rtc_timer_do_work()
915 while ((next = timerqueue_getnext(&rtc->timerqueue))) { in rtc_timer_do_work()
916 if (next->expires > now) in rtc_timer_do_work()
921 timerqueue_del(&rtc->timerqueue, &timer->node); in rtc_timer_do_work()
923 timer->enabled = 0; in rtc_timer_do_work()
924 if (timer->func) in rtc_timer_do_work()
925 timer->func(timer->rtc); in rtc_timer_do_work()
928 /* Re-add/fwd periodic timers */ in rtc_timer_do_work()
929 if (ktime_to_ns(timer->period)) { in rtc_timer_do_work()
930 timer->node.expires = ktime_add(timer->node.expires, in rtc_timer_do_work()
931 timer->period); in rtc_timer_do_work()
932 timer->enabled = 1; in rtc_timer_do_work()
933 timerqueue_add(&rtc->timerqueue, &timer->node); in rtc_timer_do_work()
944 alarm.time = rtc_ktime_to_tm(next->expires); in rtc_timer_do_work()
947 err = __rtc_set_alarm(rtc, &alarm); in rtc_timer_do_work()
948 if (err == -ETIME) { in rtc_timer_do_work()
951 if (retry-- > 0) in rtc_timer_do_work()
955 timerqueue_del(&rtc->timerqueue, &timer->node); in rtc_timer_do_work()
957 timer->enabled = 0; in rtc_timer_do_work()
958 dev_err(&rtc->dev, "__rtc_set_alarm: err=%d\n", err); in rtc_timer_do_work()
962 rtc_alarm_disable(rtc); in rtc_timer_do_work()
965 pm_relax(rtc->dev.parent); in rtc_timer_do_work()
966 mutex_unlock(&rtc->ops_lock); in rtc_timer_do_work()
969 /* rtc_timer_init - Initializes an rtc_timer
972 * @rtc: pointer to the rtc_device
977 struct rtc_device *rtc) in rtc_timer_init() argument
979 timerqueue_init(&timer->node); in rtc_timer_init()
980 timer->enabled = 0; in rtc_timer_init()
981 timer->func = f; in rtc_timer_init()
982 timer->rtc = rtc; in rtc_timer_init()
985 /* rtc_timer_start - Sets an rtc_timer to fire in the future
986 * @ rtc: rtc device to be used
993 int rtc_timer_start(struct rtc_device *rtc, struct rtc_timer *timer, in rtc_timer_start() argument
998 mutex_lock(&rtc->ops_lock); in rtc_timer_start()
999 if (timer->enabled) in rtc_timer_start()
1000 rtc_timer_remove(rtc, timer); in rtc_timer_start()
1002 timer->node.expires = expires; in rtc_timer_start()
1003 timer->period = period; in rtc_timer_start()
1005 ret = rtc_timer_enqueue(rtc, timer); in rtc_timer_start()
1007 mutex_unlock(&rtc->ops_lock); in rtc_timer_start()
1011 /* rtc_timer_cancel - Stops an rtc_timer
1012 * @ rtc: rtc device to be used
1017 void rtc_timer_cancel(struct rtc_device *rtc, struct rtc_timer *timer) in rtc_timer_cancel() argument
1019 mutex_lock(&rtc->ops_lock); in rtc_timer_cancel()
1020 if (timer->enabled) in rtc_timer_cancel()
1021 rtc_timer_remove(rtc, timer); in rtc_timer_cancel()
1022 mutex_unlock(&rtc->ops_lock); in rtc_timer_cancel()
1026 * rtc_read_offset - Read the amount of rtc offset in parts per billion
1027 * @rtc: rtc device to be used
1032 * Kernel interface to read rtc clock offset
1034 * If read_offset() is not implemented for the rtc, return -EINVAL
1036 int rtc_read_offset(struct rtc_device *rtc, long *offset) in rtc_read_offset() argument
1040 if (!rtc->ops) in rtc_read_offset()
1041 return -ENODEV; in rtc_read_offset()
1043 if (!rtc->ops->read_offset) in rtc_read_offset()
1044 return -EINVAL; in rtc_read_offset()
1046 mutex_lock(&rtc->ops_lock); in rtc_read_offset()
1047 ret = rtc->ops->read_offset(rtc->dev.parent, offset); in rtc_read_offset()
1048 mutex_unlock(&rtc->ops_lock); in rtc_read_offset()
1055 * rtc_set_offset - Adjusts the duration of the average second
1056 * @rtc: rtc device to be used
1059 * Some rtc's allow an adjustment to the average duration of a second
1064 * t = t0 * (1 + offset * 1e-9)
1065 * where t0 is the measured length of 1 RTC second with offset = 0
1067 * Kernel interface to adjust an rtc clock offset.
1069 * If the rtc offset is not setable (or not implemented), return -EINVAL
1071 int rtc_set_offset(struct rtc_device *rtc, long offset) in rtc_set_offset() argument
1075 if (!rtc->ops) in rtc_set_offset()
1076 return -ENODEV; in rtc_set_offset()
1078 if (!rtc->ops->set_offset) in rtc_set_offset()
1079 return -EINVAL; in rtc_set_offset()
1081 mutex_lock(&rtc->ops_lock); in rtc_set_offset()
1082 ret = rtc->ops->set_offset(rtc->dev.parent, offset); in rtc_set_offset()
1083 mutex_unlock(&rtc->ops_lock); in rtc_set_offset()