Lines Matching refs:rtc

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)
27 if (!rtc->offset_secs)
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)
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) {
71 time64_t range_min = rtc->set_start_time ? rtc->start_secs :
72 rtc->range_min;
73 timeu64_t range_max = rtc->set_start_time ?
74 (rtc->start_secs + rtc->range_max - rtc->range_min) :
75 rtc->range_max;
84 static int __rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm)
88 if (!rtc->ops) {
90 } else if (!rtc->ops->read_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);
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);
126 int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm)
134 err = rtc_valid_range(rtc, tm);
138 rtc_subtract_offset(rtc, tm);
141 uie = rtc->uie_rtctimer.enabled || rtc->uie_irq_active;
143 uie = rtc->uie_rtctimer.enabled;
146 err = rtc_update_irq_enable(rtc, 0);
151 err = mutex_lock_interruptible(&rtc->ops_lock);
155 if (!rtc->ops)
157 else if (rtc->ops->set_time)
158 err = rtc->ops->set_time(rtc->dev.parent, tm);
162 pm_stay_awake(rtc->dev.parent);
163 mutex_unlock(&rtc->ops_lock);
165 schedule_work(&rtc->irqwork);
168 err = rtc_update_irq_enable(rtc, 1);
178 static int rtc_read_alarm_internal(struct rtc_device *rtc,
183 err = mutex_lock_interruptible(&rtc->ops_lock);
187 if (!rtc->ops) {
189 } else if (!test_bit(RTC_FEATURE_ALARM, rtc->features) || !rtc->ops->read_alarm) {
203 err = rtc->ops->read_alarm(rtc->dev.parent, alarm);
206 mutex_unlock(&rtc->ops_lock);
212 int __rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
263 err = rtc_read_time(rtc, &before);
272 err = rtc_read_alarm_internal(rtc, alarm);
278 rtc_add_offset(rtc, &alarm->time);
283 err = rtc_read_time(rtc, &now);
339 dev_dbg(&rtc->dev, "alarm rollover: %s\n", "day");
350 dev_dbg(&rtc->dev, "alarm rollover: %s\n", "month");
365 dev_dbg(&rtc->dev, "alarm rollover: %s\n", "year");
373 dev_warn(&rtc->dev, "alarm rollover not handled\n");
380 dev_warn(&rtc->dev, "invalid alarm value: %ptR\n",
386 int rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
390 err = mutex_lock_interruptible(&rtc->ops_lock);
393 if (!rtc->ops) {
395 } else if (!test_bit(RTC_FEATURE_ALARM, rtc->features)) {
399 alarm->enabled = rtc->aie_timer.enabled;
400 alarm->time = rtc_ktime_to_tm(rtc->aie_timer.node.expires);
402 mutex_unlock(&rtc->ops_lock);
409 static int __rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
422 err = __rtc_read_time(rtc, &tm);
436 rtc_subtract_offset(rtc, &alarm->time);
438 if (!rtc->ops)
440 else if (!test_bit(RTC_FEATURE_ALARM, rtc->features))
443 err = rtc->ops->set_alarm(rtc->dev.parent, alarm);
449 int rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
454 if (!rtc->ops)
456 else if (!test_bit(RTC_FEATURE_ALARM, rtc->features))
463 err = rtc_valid_range(rtc, &alarm->time);
467 err = mutex_lock_interruptible(&rtc->ops_lock);
470 if (rtc->aie_timer.enabled)
471 rtc_timer_remove(rtc, &rtc->aie_timer);
478 if (test_bit(RTC_FEATURE_ALARM_RES_MINUTE, rtc->features))
481 rtc->aie_timer.node.expires = alarm_time;
482 rtc->aie_timer.period = 0;
484 err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
486 mutex_unlock(&rtc->ops_lock);
493 int rtc_initialize_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
502 err = rtc_read_time(rtc, &now);
506 err = mutex_lock_interruptible(&rtc->ops_lock);
510 rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time);
511 rtc->aie_timer.period = 0;
515 rtc->aie_timer.node.expires)) {
516 rtc->aie_timer.enabled = 1;
517 timerqueue_add(&rtc->timerqueue, &rtc->aie_timer.node);
518 trace_rtc_timer_enqueue(&rtc->aie_timer);
520 mutex_unlock(&rtc->ops_lock);
525 int rtc_alarm_irq_enable(struct rtc_device *rtc, unsigned int enabled)
529 err = mutex_lock_interruptible(&rtc->ops_lock);
533 if (rtc->aie_timer.enabled != enabled) {
535 err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
537 rtc_timer_remove(rtc, &rtc->aie_timer);
542 else if (!rtc->ops)
544 else if (!test_bit(RTC_FEATURE_ALARM, rtc->features) || !rtc->ops->alarm_irq_enable)
547 err = rtc->ops->alarm_irq_enable(rtc->dev.parent, enabled);
549 mutex_unlock(&rtc->ops_lock);
556 int rtc_update_irq_enable(struct rtc_device *rtc, unsigned int enabled)
560 err = mutex_lock_interruptible(&rtc->ops_lock);
565 if (enabled == 0 && rtc->uie_irq_active) {
566 mutex_unlock(&rtc->ops_lock);
567 return rtc_dev_update_irq_enable_emul(rtc, 0);
571 if (rtc->uie_rtctimer.enabled == enabled)
574 if (!test_bit(RTC_FEATURE_UPDATE_INTERRUPT, rtc->features) ||
575 !test_bit(RTC_FEATURE_ALARM, rtc->features)) {
576 mutex_unlock(&rtc->ops_lock);
578 return rtc_dev_update_irq_enable_emul(rtc, enabled);
588 err = __rtc_read_time(rtc, &tm);
593 rtc->uie_rtctimer.node.expires = ktime_add(now, onesec);
594 rtc->uie_rtctimer.period = ktime_set(1, 0);
595 err = rtc_timer_enqueue(rtc, &rtc->uie_rtctimer);
597 rtc_timer_remove(rtc, &rtc->uie_rtctimer);
601 mutex_unlock(&rtc->ops_lock);
609 * @rtc: pointer to the rtc device
617 void rtc_handle_legacy_irq(struct rtc_device *rtc, int num, int mode)
622 spin_lock_irqsave(&rtc->irq_lock, flags);
623 rtc->irq_data = (rtc->irq_data + (num << 8)) | (RTC_IRQF | mode);
624 spin_unlock_irqrestore(&rtc->irq_lock, flags);
626 wake_up_interruptible(&rtc->irq_queue);
627 kill_fasync(&rtc->async_queue, SIGIO, POLL_IN);
632 * @rtc: pointer to the rtc_device
636 void rtc_aie_update_irq(struct rtc_device *rtc)
638 rtc_handle_legacy_irq(rtc, 1, RTC_AF);
643 * @rtc: pointer to the rtc_device
647 void rtc_uie_update_irq(struct rtc_device *rtc)
649 rtc_handle_legacy_irq(rtc, 1, RTC_UF);
662 struct rtc_device *rtc;
666 rtc = container_of(timer, struct rtc_device, pie_timer);
668 period = NSEC_PER_SEC / rtc->irq_freq;
671 rtc_handle_legacy_irq(rtc, count, RTC_PF);
678 * @rtc: the rtc device
683 void rtc_update_irq(struct rtc_device *rtc,
686 if (IS_ERR_OR_NULL(rtc))
689 pm_stay_awake(rtc->dev.parent);
690 schedule_work(&rtc->irqwork);
697 struct rtc_device *rtc = NULL;
701 rtc = to_rtc_device(dev);
703 if (rtc) {
704 if (!try_module_get(rtc->owner)) {
706 rtc = NULL;
710 return rtc;
714 void rtc_class_close(struct rtc_device *rtc)
716 module_put(rtc->owner);
717 put_device(&rtc->dev);
721 static int rtc_update_hrtimer(struct rtc_device *rtc, int enabled)
730 * could be blocked on rtc->irq_task_lock and hrtimer_cancel()
733 if (hrtimer_try_to_cancel(&rtc->pie_timer) < 0)
737 ktime_t period = NSEC_PER_SEC / rtc->irq_freq;
739 hrtimer_start(&rtc->pie_timer, period, HRTIMER_MODE_REL);
746 * @rtc: the rtc device
753 int rtc_irq_set_state(struct rtc_device *rtc, int enabled)
757 while (rtc_update_hrtimer(rtc, enabled) < 0)
760 rtc->pie_enabled = enabled;
768 * @rtc: the rtc device
775 int rtc_irq_set_freq(struct rtc_device *rtc, int freq)
782 rtc->irq_freq = freq;
783 while (rtc->pie_enabled && rtc_update_hrtimer(rtc, 1) < 0)
792 * @rtc: rtc device
795 * Enqueues a timer onto the rtc devices timerqueue and sets
802 static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer)
804 struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue);
809 err = __rtc_read_time(rtc, &tm);
823 timerqueue_add(&rtc->timerqueue, &timer->node);
830 err = __rtc_set_alarm(rtc, &alarm);
832 pm_stay_awake(rtc->dev.parent);
833 schedule_work(&rtc->irqwork);
835 timerqueue_del(&rtc->timerqueue, &timer->node);
844 static void rtc_alarm_disable(struct rtc_device *rtc)
846 if (!rtc->ops || !test_bit(RTC_FEATURE_ALARM, rtc->features) || !rtc->ops->alarm_irq_enable)
849 rtc->ops->alarm_irq_enable(rtc->dev.parent, false);
855 * @rtc: rtc device
858 * Removes a timer onto the rtc devices timerqueue and sets
865 static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer)
867 struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue);
869 timerqueue_del(&rtc->timerqueue, &timer->node);
876 next = timerqueue_getnext(&rtc->timerqueue);
878 rtc_alarm_disable(rtc);
883 err = __rtc_set_alarm(rtc, &alarm);
885 pm_stay_awake(rtc->dev.parent);
886 schedule_work(&rtc->irqwork);
892 * rtc_timer_do_work - Expires rtc timers
895 * Expires rtc timers. Reprograms next alarm event if needed.
907 struct rtc_device *rtc =
910 mutex_lock(&rtc->ops_lock);
912 __rtc_read_time(rtc, &tm);
914 while ((next = timerqueue_getnext(&rtc->timerqueue))) {
920 timerqueue_del(&rtc->timerqueue, &timer->node);
924 timer->func(timer->rtc);
932 timerqueue_add(&rtc->timerqueue, &timer->node);
946 err = __rtc_set_alarm(rtc, &alarm);
954 timerqueue_del(&rtc->timerqueue, &timer->node);
957 dev_err(&rtc->dev, "__rtc_set_alarm: err=%d\n", err);
961 rtc_alarm_disable(rtc);
964 pm_relax(rtc->dev.parent);
965 mutex_unlock(&rtc->ops_lock);
971 * @rtc: pointer to the rtc_device
976 struct rtc_device *rtc)
981 timer->rtc = rtc;
985 * @ rtc: rtc device to be used
992 int rtc_timer_start(struct rtc_device *rtc, struct rtc_timer *timer,
997 mutex_lock(&rtc->ops_lock);
999 rtc_timer_remove(rtc, timer);
1004 ret = rtc_timer_enqueue(rtc, timer);
1006 mutex_unlock(&rtc->ops_lock);
1011 * @ rtc: rtc device to be used
1016 void rtc_timer_cancel(struct rtc_device *rtc, struct rtc_timer *timer)
1018 mutex_lock(&rtc->ops_lock);
1020 rtc_timer_remove(rtc, timer);
1021 mutex_unlock(&rtc->ops_lock);
1025 * rtc_read_offset - Read the amount of rtc offset in parts per billion
1026 * @rtc: rtc device to be used
1031 * Kernel interface to read rtc clock offset
1033 * If read_offset() is not implemented for the rtc, return -EINVAL
1035 int rtc_read_offset(struct rtc_device *rtc, long *offset)
1039 if (!rtc->ops)
1042 if (!rtc->ops->read_offset)
1045 mutex_lock(&rtc->ops_lock);
1046 ret = rtc->ops->read_offset(rtc->dev.parent, offset);
1047 mutex_unlock(&rtc->ops_lock);
1055 * @rtc: rtc device to be used
1058 * Some rtc's allow an adjustment to the average duration of a second
1066 * Kernel interface to adjust an rtc clock offset.
1068 * If the rtc offset is not setable (or not implemented), return -EINVAL
1070 int rtc_set_offset(struct rtc_device *rtc, long offset)
1074 if (!rtc->ops)
1077 if (!rtc->ops->set_offset)
1080 mutex_lock(&rtc->ops_lock);
1081 ret = rtc->ops->set_offset(rtc->dev.parent, offset);
1082 mutex_unlock(&rtc->ops_lock);