Lines Matching +full:next +full:- +full:mode

1 // SPDX-License-Identifier: GPL-2.0
27 if (!rtc->offset_secs) 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()
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()
240 * of the -1 fields. 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 err = rtc_valid_tm(&alarm->time); 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()
334 * could also be in the next month or year. This is a common 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()
344 * be next month. An alarm matching on the 30th, 29th, or 28th 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()
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()
416 err = rtc_valid_tm(&alarm->time); in __rtc_set_alarm()
420 scheduled = rtc_tm_to_time64(&alarm->time); 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()
433 * the is alarm set for the next second and the second ticks 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()
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()
499 err = rtc_valid_tm(&alarm->time); 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()
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()
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()
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()
581 return -EINVAL; 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
612 * @mode: type of the event, RTC_AF, RTC_UF of RTC_PF
614 * This function is called when an AIE, UIE or PIE mode interrupt
618 void rtc_handle_legacy_irq(struct rtc_device *rtc, int num, int mode) in rtc_handle_legacy_irq() argument
622 /* mark one irq of the appropriate mode */ in rtc_handle_legacy_irq()
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
643 * rtc_uie_update_irq - UIE mode rtctimer hook
654 * rtc_pie_update_irq - PIE mode hrtimer hook
655 * @timer: pointer to the pie mode hrtimer
657 * This function is used to emulate PIE mode interrupts
669 period = NSEC_PER_SEC / rtc->irq_freq; in rtc_pie_update_irq()
678 * rtc_update_irq - Triggered when a RTC interrupt occurs.
690 pm_stay_awake(rtc->dev.parent); in rtc_update_irq()
691 schedule_work(&rtc->irqwork); in rtc_update_irq()
705 if (!try_module_get(rtc->owner)) { in rtc_class_open()
717 module_put(rtc->owner); in rtc_class_close()
718 put_device(&rtc->dev); in rtc_class_close()
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
761 rtc->pie_enabled = enabled; in rtc_irq_set_state()
768 * rtc_irq_set_freq - set 2^N Hz periodic IRQ frequency for IRQ
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
797 * the next alarm event appropriately.
805 struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue); in rtc_timer_enqueue() local
814 timer->enabled = 1; in rtc_timer_enqueue()
818 while (next) { in rtc_timer_enqueue()
819 if (next->expires >= now) in rtc_timer_enqueue()
821 next = timerqueue_iterate_next(next); 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()
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()
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
860 * the next alarm event appropriately.
868 struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue); in rtc_timer_remove() local
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()
878 if (!next) { in rtc_timer_remove()
882 alarm.time = rtc_ktime_to_tm(next->expires); 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.
904 struct timerqueue_node *next; in rtc_timer_do_work() local
912 mutex_lock(&rtc->ops_lock); in rtc_timer_do_work()
916 mutex_unlock(&rtc->ops_lock); in rtc_timer_do_work()
920 while ((next = timerqueue_getnext(&rtc->timerqueue))) { in rtc_timer_do_work()
921 if (next->expires > now) in rtc_timer_do_work()
925 timer = container_of(next, struct rtc_timer, node); in rtc_timer_do_work()
926 timerqueue_del(&rtc->timerqueue, &timer->node); in rtc_timer_do_work()
928 timer->enabled = 0; in rtc_timer_do_work()
929 if (timer->func) in rtc_timer_do_work()
930 timer->func(timer->rtc); in rtc_timer_do_work()
933 /* Re-add/fwd periodic timers */ in rtc_timer_do_work()
934 if (ktime_to_ns(timer->period)) { in rtc_timer_do_work()
935 timer->node.expires = ktime_add(timer->node.expires, in rtc_timer_do_work()
936 timer->period); in rtc_timer_do_work()
937 timer->enabled = 1; in rtc_timer_do_work()
938 timerqueue_add(&rtc->timerqueue, &timer->node); in rtc_timer_do_work()
943 /* Set next alarm */ in rtc_timer_do_work()
944 if (next) { in rtc_timer_do_work()
949 alarm.time = rtc_ktime_to_tm(next->expires); in rtc_timer_do_work()
953 if (err == -ETIME) { in rtc_timer_do_work()
956 if (retry-- > 0) in rtc_timer_do_work()
959 timer = container_of(next, struct rtc_timer, node); in rtc_timer_do_work()
960 timerqueue_del(&rtc->timerqueue, &timer->node); in rtc_timer_do_work()
962 timer->enabled = 0; in rtc_timer_do_work()
963 dev_err(&rtc->dev, "__rtc_set_alarm: err=%d\n", err); in rtc_timer_do_work()
970 pm_relax(rtc->dev.parent); in rtc_timer_do_work()
971 mutex_unlock(&rtc->ops_lock); in rtc_timer_do_work()
974 /* rtc_timer_init - Initializes an rtc_timer
984 timerqueue_init(&timer->node); in rtc_timer_init()
985 timer->enabled = 0; in rtc_timer_init()
986 timer->func = f; in rtc_timer_init()
987 timer->rtc = rtc; in rtc_timer_init()
990 /* rtc_timer_start - Sets an rtc_timer to fire in the future
1003 mutex_lock(&rtc->ops_lock); in rtc_timer_start()
1004 if (timer->enabled) in rtc_timer_start()
1007 timer->node.expires = expires; in rtc_timer_start()
1008 timer->period = period; in rtc_timer_start()
1012 mutex_unlock(&rtc->ops_lock); in rtc_timer_start()
1016 /* rtc_timer_cancel - Stops an rtc_timer
1024 mutex_lock(&rtc->ops_lock); in rtc_timer_cancel()
1025 if (timer->enabled) in rtc_timer_cancel()
1027 mutex_unlock(&rtc->ops_lock); in rtc_timer_cancel()
1031 * rtc_read_offset - Read the amount of rtc offset in parts per billion
1039 * If read_offset() is not implemented for the rtc, return -EINVAL
1045 if (!rtc->ops) in rtc_read_offset()
1046 return -ENODEV; in rtc_read_offset()
1048 if (!rtc->ops->read_offset) in rtc_read_offset()
1049 return -EINVAL; in rtc_read_offset()
1051 mutex_lock(&rtc->ops_lock); in rtc_read_offset()
1052 ret = rtc->ops->read_offset(rtc->dev.parent, offset); in rtc_read_offset()
1053 mutex_unlock(&rtc->ops_lock); in rtc_read_offset()
1060 * rtc_set_offset - Adjusts the duration of the average second
1069 * t = t0 * (1 + offset * 1e-9)
1074 * If the rtc offset is not setable (or not implemented), return -EINVAL
1080 if (!rtc->ops) in rtc_set_offset()
1081 return -ENODEV; in rtc_set_offset()
1083 if (!rtc->ops->set_offset) in rtc_set_offset()
1084 return -EINVAL; in rtc_set_offset()
1086 mutex_lock(&rtc->ops_lock); in rtc_set_offset()
1087 ret = rtc->ops->set_offset(rtc->dev.parent, offset); in rtc_set_offset()
1088 mutex_unlock(&rtc->ops_lock); in rtc_set_offset()