Lines Matching +full:no +full:- +full:read +full:- +full:rollover
1 // SPDX-License-Identifier: GPL-2.0
27 if (!rtc->offset_secs) in rtc_add_offset()
35 * between expanded range and original range, which is no need to add 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()
50 if (!rtc->offset_secs) in rtc_subtract_offset()
57 * device, then no need to subtract the offset when setting time to RTC 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()
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()
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()
105 dev_dbg(&rtc->dev, "read_time: rtc_time isn't valid\n"); in __rtc_read_time()
114 err = mutex_lock_interruptible(&rtc->ops_lock); in rtc_read_time()
119 mutex_unlock(&rtc->ops_lock); in rtc_read_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()
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()
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()
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()
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()
276 /* full-function RTCs won't have such missing fields */ in __rtc_read_alarm()
277 if (rtc_valid_tm(&alarm->time) == 0) { in __rtc_read_alarm()
278 rtc_add_offset(rtc, &alarm->time); in __rtc_read_alarm()
294 * know there's at least one since alarm->time is invalid. in __rtc_read_alarm()
296 if (alarm->time.tm_sec == -1) in __rtc_read_alarm()
297 alarm->time.tm_sec = now.tm_sec; in __rtc_read_alarm()
298 if (alarm->time.tm_min == -1) in __rtc_read_alarm()
299 alarm->time.tm_min = now.tm_min; in __rtc_read_alarm()
300 if (alarm->time.tm_hour == -1) in __rtc_read_alarm()
301 alarm->time.tm_hour = now.tm_hour; in __rtc_read_alarm()
303 /* For simplicity, only support date rollover for now */ in __rtc_read_alarm()
304 if (alarm->time.tm_mday < 1 || alarm->time.tm_mday > 31) { in __rtc_read_alarm()
305 alarm->time.tm_mday = now.tm_mday; in __rtc_read_alarm()
308 if ((unsigned int)alarm->time.tm_mon >= 12) { in __rtc_read_alarm()
309 alarm->time.tm_mon = now.tm_mon; in __rtc_read_alarm()
313 if (alarm->time.tm_year == -1) { in __rtc_read_alarm()
314 alarm->time.tm_year = now.tm_year; in __rtc_read_alarm()
322 err = rtc_valid_tm(&alarm->time); in __rtc_read_alarm()
326 /* with luck, no rollover is needed */ in __rtc_read_alarm()
328 t_alm = rtc_tm_to_time64(&alarm->time); in __rtc_read_alarm()
333 /* 24 hour rollover ... if it's now 10am Monday, an alarm that in __rtc_read_alarm()
339 dev_dbg(&rtc->dev, "alarm rollover: %s\n", "day"); in __rtc_read_alarm()
341 rtc_time64_to_tm(t_alm, &alarm->time); in __rtc_read_alarm()
344 /* Month rollover ... if it's the 31th, an alarm on the 3rd will in __rtc_read_alarm()
350 dev_dbg(&rtc->dev, "alarm rollover: %s\n", "month"); in __rtc_read_alarm()
352 if (alarm->time.tm_mon < 11) { in __rtc_read_alarm()
353 alarm->time.tm_mon++; in __rtc_read_alarm()
355 alarm->time.tm_mon = 0; in __rtc_read_alarm()
356 alarm->time.tm_year++; in __rtc_read_alarm()
358 days = rtc_month_days(alarm->time.tm_mon, in __rtc_read_alarm()
359 alarm->time.tm_year); in __rtc_read_alarm()
360 } while (days < alarm->time.tm_mday); in __rtc_read_alarm()
363 /* Year rollover ... easy except for leap years! */ in __rtc_read_alarm()
365 dev_dbg(&rtc->dev, "alarm rollover: %s\n", "year"); in __rtc_read_alarm()
367 alarm->time.tm_year++; in __rtc_read_alarm()
368 } while (!is_leap_year(alarm->time.tm_year + 1900) && in __rtc_read_alarm()
369 rtc_valid_tm(&alarm->time) != 0); in __rtc_read_alarm()
373 dev_warn(&rtc->dev, "alarm rollover not handled\n"); in __rtc_read_alarm()
376 err = rtc_valid_tm(&alarm->time); in __rtc_read_alarm()
380 dev_warn(&rtc->dev, "invalid alarm value: %ptR\n", in __rtc_read_alarm()
381 &alarm->time); in __rtc_read_alarm()
390 err = mutex_lock_interruptible(&rtc->ops_lock); in rtc_read_alarm()
393 if (!rtc->ops) { in rtc_read_alarm()
394 err = -ENODEV; in rtc_read_alarm()
395 } else if (!test_bit(RTC_FEATURE_ALARM, rtc->features) || !rtc->ops->read_alarm) { in rtc_read_alarm()
396 err = -EINVAL; in rtc_read_alarm()
399 alarm->enabled = rtc->aie_timer.enabled; in rtc_read_alarm()
400 alarm->time = rtc_ktime_to_tm(rtc->aie_timer.node.expires); in rtc_read_alarm()
402 mutex_unlock(&rtc->ops_lock); in rtc_read_alarm()
404 trace_rtc_read_alarm(rtc_tm_to_time64(&alarm->time), err); in rtc_read_alarm()
415 err = rtc_valid_tm(&alarm->time); in __rtc_set_alarm()
419 scheduled = rtc_tm_to_time64(&alarm->time); in __rtc_set_alarm()
428 return -ETIME; in __rtc_set_alarm()
430 * XXX - We just checked to make sure the alarm time is not in __rtc_set_alarm()
436 rtc_subtract_offset(rtc, &alarm->time); in __rtc_set_alarm()
438 if (!rtc->ops) in __rtc_set_alarm()
439 err = -ENODEV; in __rtc_set_alarm()
440 else if (!test_bit(RTC_FEATURE_ALARM, rtc->features)) in __rtc_set_alarm()
441 err = -EINVAL; in __rtc_set_alarm()
443 err = rtc->ops->set_alarm(rtc->dev.parent, alarm); in __rtc_set_alarm()
445 trace_rtc_set_alarm(rtc_tm_to_time64(&alarm->time), err); in __rtc_set_alarm()
454 if (!rtc->ops) in rtc_set_alarm()
455 return -ENODEV; in rtc_set_alarm()
456 else if (!test_bit(RTC_FEATURE_ALARM, rtc->features)) in rtc_set_alarm()
457 return -EINVAL; in rtc_set_alarm()
459 err = rtc_valid_tm(&alarm->time); in rtc_set_alarm()
463 err = rtc_valid_range(rtc, &alarm->time); in rtc_set_alarm()
467 err = mutex_lock_interruptible(&rtc->ops_lock); in rtc_set_alarm()
470 if (rtc->aie_timer.enabled) in rtc_set_alarm()
471 rtc_timer_remove(rtc, &rtc->aie_timer); in rtc_set_alarm()
473 alarm_time = rtc_tm_to_ktime(alarm->time); in rtc_set_alarm()
478 if (test_bit(RTC_FEATURE_ALARM_RES_MINUTE, rtc->features)) in rtc_set_alarm()
479 alarm_time = ktime_sub_ns(alarm_time, (u64)alarm->time.tm_sec * NSEC_PER_SEC); in rtc_set_alarm()
481 rtc->aie_timer.node.expires = alarm_time; in rtc_set_alarm()
482 rtc->aie_timer.period = 0; in rtc_set_alarm()
483 if (alarm->enabled) in rtc_set_alarm()
484 err = rtc_timer_enqueue(rtc, &rtc->aie_timer); in rtc_set_alarm()
486 mutex_unlock(&rtc->ops_lock); in rtc_set_alarm()
498 err = rtc_valid_tm(&alarm->time); in rtc_initialize_alarm()
506 err = mutex_lock_interruptible(&rtc->ops_lock); in rtc_initialize_alarm()
510 rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time); in rtc_initialize_alarm()
511 rtc->aie_timer.period = 0; in rtc_initialize_alarm()
514 if (alarm->enabled && (rtc_tm_to_ktime(now) < in rtc_initialize_alarm()
515 rtc->aie_timer.node.expires)) { in rtc_initialize_alarm()
516 rtc->aie_timer.enabled = 1; in rtc_initialize_alarm()
517 timerqueue_add(&rtc->timerqueue, &rtc->aie_timer.node); in rtc_initialize_alarm()
518 trace_rtc_timer_enqueue(&rtc->aie_timer); in rtc_initialize_alarm()
520 mutex_unlock(&rtc->ops_lock); in rtc_initialize_alarm()
529 err = mutex_lock_interruptible(&rtc->ops_lock); in rtc_alarm_irq_enable()
533 if (rtc->aie_timer.enabled != enabled) { in rtc_alarm_irq_enable()
535 err = rtc_timer_enqueue(rtc, &rtc->aie_timer); in rtc_alarm_irq_enable()
537 rtc_timer_remove(rtc, &rtc->aie_timer); in rtc_alarm_irq_enable()
542 else if (!rtc->ops) in rtc_alarm_irq_enable()
543 err = -ENODEV; in rtc_alarm_irq_enable()
544 else if (!test_bit(RTC_FEATURE_ALARM, rtc->features) || !rtc->ops->alarm_irq_enable) in rtc_alarm_irq_enable()
545 err = -EINVAL; in rtc_alarm_irq_enable()
547 err = rtc->ops->alarm_irq_enable(rtc->dev.parent, enabled); in rtc_alarm_irq_enable()
549 mutex_unlock(&rtc->ops_lock); in rtc_alarm_irq_enable()
560 err = mutex_lock_interruptible(&rtc->ops_lock); in rtc_update_irq_enable()
565 if (enabled == 0 && rtc->uie_irq_active) { in rtc_update_irq_enable()
566 mutex_unlock(&rtc->ops_lock); in rtc_update_irq_enable()
571 if (rtc->uie_rtctimer.enabled == enabled) in rtc_update_irq_enable()
574 if (!test_bit(RTC_FEATURE_UPDATE_INTERRUPT, rtc->features) || in rtc_update_irq_enable()
575 !test_bit(RTC_FEATURE_ALARM, rtc->features)) { in rtc_update_irq_enable()
576 mutex_unlock(&rtc->ops_lock); in rtc_update_irq_enable()
580 return -EINVAL; in rtc_update_irq_enable()
593 rtc->uie_rtctimer.node.expires = ktime_add(now, onesec); in rtc_update_irq_enable()
594 rtc->uie_rtctimer.period = ktime_set(1, 0); in rtc_update_irq_enable()
595 err = rtc_timer_enqueue(rtc, &rtc->uie_rtctimer); in rtc_update_irq_enable()
597 rtc_timer_remove(rtc, &rtc->uie_rtctimer); in rtc_update_irq_enable()
601 mutex_unlock(&rtc->ops_lock); in rtc_update_irq_enable()
608 * rtc_handle_legacy_irq - AIE, UIE and PIE event hook
622 spin_lock_irqsave(&rtc->irq_lock, flags); in rtc_handle_legacy_irq()
623 rtc->irq_data = (rtc->irq_data + (num << 8)) | (RTC_IRQF | mode); in rtc_handle_legacy_irq()
624 spin_unlock_irqrestore(&rtc->irq_lock, flags); in rtc_handle_legacy_irq()
626 wake_up_interruptible(&rtc->irq_queue); in rtc_handle_legacy_irq()
627 kill_fasync(&rtc->async_queue, SIGIO, POLL_IN); in rtc_handle_legacy_irq()
631 * rtc_aie_update_irq - AIE mode rtctimer hook
642 * rtc_uie_update_irq - UIE mode rtctimer hook
653 * rtc_pie_update_irq - PIE mode hrtimer hook
668 period = NSEC_PER_SEC / rtc->irq_freq; in rtc_pie_update_irq()
677 * rtc_update_irq - Triggered when a RTC interrupt occurs.
689 pm_stay_awake(rtc->dev.parent); in rtc_update_irq()
690 schedule_work(&rtc->irqwork); in rtc_update_irq()
704 if (!try_module_get(rtc->owner)) { in rtc_class_open()
716 module_put(rtc->owner); in rtc_class_close()
717 put_device(&rtc->dev); in rtc_class_close()
725 * we could run into BUG_ON(timer->state != HRTIMER_STATE_CALLBACK); in rtc_update_hrtimer()
730 * could be blocked on rtc->irq_task_lock and hrtimer_cancel() in rtc_update_hrtimer()
733 if (hrtimer_try_to_cancel(&rtc->pie_timer) < 0) in rtc_update_hrtimer()
734 return -1; in rtc_update_hrtimer()
737 ktime_t period = NSEC_PER_SEC / rtc->irq_freq; in rtc_update_hrtimer()
739 hrtimer_start(&rtc->pie_timer, period, HRTIMER_MODE_REL); in rtc_update_hrtimer()
745 * rtc_irq_set_state - enable/disable 2^N Hz periodic IRQs
760 rtc->pie_enabled = enabled; in rtc_irq_set_state()
767 * rtc_irq_set_freq - set 2^N Hz periodic IRQ frequency for IRQ
780 return -EINVAL; in rtc_irq_set_freq()
782 rtc->irq_freq = freq; in rtc_irq_set_freq()
783 while (rtc->pie_enabled && rtc_update_hrtimer(rtc, 1) < 0) in rtc_irq_set_freq()
791 * rtc_timer_enqueue - Adds a rtc_timer to the rtc_device timerqueue
804 struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue); in rtc_timer_enqueue()
813 timer->enabled = 1; in rtc_timer_enqueue()
818 if (next->expires >= now) in rtc_timer_enqueue()
823 timerqueue_add(&rtc->timerqueue, &timer->node); in rtc_timer_enqueue()
825 if (!next || ktime_before(timer->node.expires, next->expires)) { in rtc_timer_enqueue()
828 alarm.time = rtc_ktime_to_tm(timer->node.expires); in rtc_timer_enqueue()
831 if (err == -ETIME) { in rtc_timer_enqueue()
832 pm_stay_awake(rtc->dev.parent); in rtc_timer_enqueue()
833 schedule_work(&rtc->irqwork); in rtc_timer_enqueue()
835 timerqueue_del(&rtc->timerqueue, &timer->node); in rtc_timer_enqueue()
837 timer->enabled = 0; in rtc_timer_enqueue()
846 if (!rtc->ops || !test_bit(RTC_FEATURE_ALARM, rtc->features) || !rtc->ops->alarm_irq_enable) in rtc_alarm_disable()
849 rtc->ops->alarm_irq_enable(rtc->dev.parent, false); in rtc_alarm_disable()
854 * rtc_timer_remove - Removes a rtc_timer from the rtc_device timerqueue
867 struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue); in rtc_timer_remove()
869 timerqueue_del(&rtc->timerqueue, &timer->node); in rtc_timer_remove()
871 timer->enabled = 0; in rtc_timer_remove()
872 if (next == &timer->node) { in rtc_timer_remove()
876 next = timerqueue_getnext(&rtc->timerqueue); in rtc_timer_remove()
881 alarm.time = rtc_ktime_to_tm(next->expires); in rtc_timer_remove()
884 if (err == -ETIME) { in rtc_timer_remove()
885 pm_stay_awake(rtc->dev.parent); in rtc_timer_remove()
886 schedule_work(&rtc->irqwork); in rtc_timer_remove()
892 * rtc_timer_do_work - Expires rtc timers
910 mutex_lock(&rtc->ops_lock); in rtc_timer_do_work()
914 while ((next = timerqueue_getnext(&rtc->timerqueue))) { in rtc_timer_do_work()
915 if (next->expires > now) in rtc_timer_do_work()
920 timerqueue_del(&rtc->timerqueue, &timer->node); in rtc_timer_do_work()
922 timer->enabled = 0; in rtc_timer_do_work()
923 if (timer->func) in rtc_timer_do_work()
924 timer->func(timer->rtc); in rtc_timer_do_work()
927 /* Re-add/fwd periodic timers */ in rtc_timer_do_work()
928 if (ktime_to_ns(timer->period)) { in rtc_timer_do_work()
929 timer->node.expires = ktime_add(timer->node.expires, in rtc_timer_do_work()
930 timer->period); in rtc_timer_do_work()
931 timer->enabled = 1; in rtc_timer_do_work()
932 timerqueue_add(&rtc->timerqueue, &timer->node); in rtc_timer_do_work()
943 alarm.time = rtc_ktime_to_tm(next->expires); in rtc_timer_do_work()
947 if (err == -ETIME) { in rtc_timer_do_work()
950 if (retry-- > 0) in rtc_timer_do_work()
954 timerqueue_del(&rtc->timerqueue, &timer->node); in rtc_timer_do_work()
956 timer->enabled = 0; in rtc_timer_do_work()
957 dev_err(&rtc->dev, "__rtc_set_alarm: err=%d\n", err); in rtc_timer_do_work()
964 pm_relax(rtc->dev.parent); in rtc_timer_do_work()
965 mutex_unlock(&rtc->ops_lock); in rtc_timer_do_work()
968 /* rtc_timer_init - Initializes an rtc_timer
978 timerqueue_init(&timer->node); in rtc_timer_init()
979 timer->enabled = 0; in rtc_timer_init()
980 timer->func = f; in rtc_timer_init()
981 timer->rtc = rtc; in rtc_timer_init()
984 /* rtc_timer_start - Sets an rtc_timer to fire in the future
997 mutex_lock(&rtc->ops_lock); in rtc_timer_start()
998 if (timer->enabled) in rtc_timer_start()
1001 timer->node.expires = expires; in rtc_timer_start()
1002 timer->period = period; in rtc_timer_start()
1006 mutex_unlock(&rtc->ops_lock); in rtc_timer_start()
1010 /* rtc_timer_cancel - Stops an rtc_timer
1018 mutex_lock(&rtc->ops_lock); in rtc_timer_cancel()
1019 if (timer->enabled) in rtc_timer_cancel()
1021 mutex_unlock(&rtc->ops_lock); in rtc_timer_cancel()
1025 * rtc_read_offset - Read the amount of rtc offset in parts per billion
1031 * Kernel interface to read rtc clock offset
1033 * If read_offset() is not implemented for the rtc, return -EINVAL
1039 if (!rtc->ops) in rtc_read_offset()
1040 return -ENODEV; in rtc_read_offset()
1042 if (!rtc->ops->read_offset) in rtc_read_offset()
1043 return -EINVAL; in rtc_read_offset()
1045 mutex_lock(&rtc->ops_lock); in rtc_read_offset()
1046 ret = rtc->ops->read_offset(rtc->dev.parent, offset); in rtc_read_offset()
1047 mutex_unlock(&rtc->ops_lock); in rtc_read_offset()
1054 * rtc_set_offset - Adjusts the duration of the average second
1063 * t = t0 * (1 + offset * 1e-9)
1068 * If the rtc offset is not setable (or not implemented), return -EINVAL
1074 if (!rtc->ops) in rtc_set_offset()
1075 return -ENODEV; in rtc_set_offset()
1077 if (!rtc->ops->set_offset) in rtc_set_offset()
1078 return -EINVAL; in rtc_set_offset()
1080 mutex_lock(&rtc->ops_lock); in rtc_set_offset()
1081 ret = rtc->ops->set_offset(rtc->dev.parent, offset); in rtc_set_offset()
1082 mutex_unlock(&rtc->ops_lock); in rtc_set_offset()