Lines Matching refs:rtc

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()
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()
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()
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()
90 } else if (!rtc->ops->read_time) { 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()
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()
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()
189 } else if (!test_bit(RTC_FEATURE_ALARM, rtc->features) || !rtc->ops->read_alarm) { 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()
212 int __rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm) in __rtc_read_alarm() argument
263 err = rtc_read_time(rtc, &before); in __rtc_read_alarm()
272 err = rtc_read_alarm_internal(rtc, alarm); in __rtc_read_alarm()
282 err = rtc_read_time(rtc, &now); in __rtc_read_alarm()
338 dev_dbg(&rtc->dev, "alarm rollover: %s\n", "day"); in __rtc_read_alarm()
349 dev_dbg(&rtc->dev, "alarm rollover: %s\n", "month"); in __rtc_read_alarm()
364 dev_dbg(&rtc->dev, "alarm rollover: %s\n", "year"); in __rtc_read_alarm()
372 dev_warn(&rtc->dev, "alarm rollover not handled\n"); in __rtc_read_alarm()
379 dev_warn(&rtc->dev, "invalid alarm value: %ptR\n", 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()
396 } else if (!test_bit(RTC_FEATURE_ALARM, rtc->features)) { 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()
410 static int __rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm) in __rtc_set_alarm() argument
423 err = __rtc_read_time(rtc, &tm); in __rtc_set_alarm()
437 rtc_subtract_offset(rtc, &alarm->time); in __rtc_set_alarm()
439 if (!rtc->ops) in __rtc_set_alarm()
441 else if (!test_bit(RTC_FEATURE_ALARM, rtc->features)) in __rtc_set_alarm()
444 err = rtc->ops->set_alarm(rtc->dev.parent, alarm); 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()
457 else if (!test_bit(RTC_FEATURE_ALARM, rtc->features)) 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()
479 if (test_bit(RTC_FEATURE_ALARM_RES_MINUTE, rtc->features)) 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()
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
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()
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()
545 else if (!test_bit(RTC_FEATURE_ALARM, rtc->features) || !rtc->ops->alarm_irq_enable) 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()
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()
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()
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()
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()
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()
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
734 if (hrtimer_try_to_cancel(&rtc->pie_timer) < 0) 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()
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()
776 int rtc_irq_set_freq(struct rtc_device *rtc, int freq) in rtc_irq_set_freq() argument
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()
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()
824 timerqueue_add(&rtc->timerqueue, &timer->node); in rtc_timer_enqueue()
831 err = __rtc_set_alarm(rtc, &alarm); 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()
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()
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()
877 next = timerqueue_getnext(&rtc->timerqueue); in rtc_timer_remove()
879 rtc_alarm_disable(rtc); in rtc_timer_remove()
884 err = __rtc_set_alarm(rtc, &alarm); in rtc_timer_remove()
886 pm_stay_awake(rtc->dev.parent); in rtc_timer_remove()
887 schedule_work(&rtc->irqwork); in rtc_timer_remove()
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()
921 timerqueue_del(&rtc->timerqueue, &timer->node); in rtc_timer_do_work()
925 timer->func(timer->rtc); in rtc_timer_do_work()
933 timerqueue_add(&rtc->timerqueue, &timer->node); in rtc_timer_do_work()
947 err = __rtc_set_alarm(rtc, &alarm); in rtc_timer_do_work()
955 timerqueue_del(&rtc->timerqueue, &timer->node); 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()
977 struct rtc_device *rtc) in rtc_timer_init() argument
982 timer->rtc = rtc; in rtc_timer_init()
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()
1000 rtc_timer_remove(rtc, timer); 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()
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()
1021 rtc_timer_remove(rtc, timer); in rtc_timer_cancel()
1022 mutex_unlock(&rtc->ops_lock); in rtc_timer_cancel()
1036 int rtc_read_offset(struct rtc_device *rtc, long *offset) in rtc_read_offset() argument
1040 if (!rtc->ops) in rtc_read_offset()
1043 if (!rtc->ops->read_offset) 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()
1071 int rtc_set_offset(struct rtc_device *rtc, long offset) in rtc_set_offset() argument
1075 if (!rtc->ops) in rtc_set_offset()
1078 if (!rtc->ops->set_offset) 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()