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 (!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 (!rtc->ops->read_alarm) {
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);
435 rtc_subtract_offset(rtc, &alarm->time);
437 if (!rtc->ops)
439 else if (!rtc->ops->set_alarm)
442 err = rtc->ops->set_alarm(rtc->dev.parent, alarm);
448 int rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
452 if (!rtc->ops)
454 else if (!rtc->ops->set_alarm)
461 err = rtc_valid_range(rtc, &alarm->time);
465 err = mutex_lock_interruptible(&rtc->ops_lock);
468 if (rtc->aie_timer.enabled)
469 rtc_timer_remove(rtc, &rtc->aie_timer);
471 rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time);
472 rtc->aie_timer.period = 0;
474 err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
476 mutex_unlock(&rtc->ops_lock);
483 int rtc_initialize_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
492 err = rtc_read_time(rtc, &now);
496 err = mutex_lock_interruptible(&rtc->ops_lock);
500 rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time);
501 rtc->aie_timer.period = 0;
505 rtc->aie_timer.node.expires)) {
506 rtc->aie_timer.enabled = 1;
507 timerqueue_add(&rtc->timerqueue, &rtc->aie_timer.node);
508 trace_rtc_timer_enqueue(&rtc->aie_timer);
510 mutex_unlock(&rtc->ops_lock);
515 int rtc_alarm_irq_enable(struct rtc_device *rtc, unsigned int enabled)
519 err = mutex_lock_interruptible(&rtc->ops_lock);
523 if (rtc->aie_timer.enabled != enabled) {
525 err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
527 rtc_timer_remove(rtc, &rtc->aie_timer);
532 else if (!rtc->ops)
534 else if (!rtc->ops->alarm_irq_enable)
537 err = rtc->ops->alarm_irq_enable(rtc->dev.parent, enabled);
539 mutex_unlock(&rtc->ops_lock);
546 int rtc_update_irq_enable(struct rtc_device *rtc, unsigned int enabled)
550 err = mutex_lock_interruptible(&rtc->ops_lock);
555 if (enabled == 0 && rtc->uie_irq_active) {
556 mutex_unlock(&rtc->ops_lock);
557 return rtc_dev_update_irq_enable_emul(rtc, 0);
561 if (rtc->uie_rtctimer.enabled == enabled)
564 if (rtc->uie_unsupported) {
573 rc = __rtc_read_time(rtc, &tm);
578 rtc->uie_rtctimer.node.expires = ktime_add(now, onesec);
579 rtc->uie_rtctimer.period = ktime_set(1, 0);
580 err = rtc_timer_enqueue(rtc, &rtc->uie_rtctimer);
582 rtc_timer_remove(rtc, &rtc->uie_rtctimer);
586 mutex_unlock(&rtc->ops_lock);
604 err = rtc_dev_update_irq_enable_emul(rtc, enabled);
612 * @rtc: pointer to the rtc device
620 void rtc_handle_legacy_irq(struct rtc_device *rtc, int num, int mode)
625 spin_lock_irqsave(&rtc->irq_lock, flags);
626 rtc->irq_data = (rtc->irq_data + (num << 8)) | (RTC_IRQF | mode);
627 spin_unlock_irqrestore(&rtc->irq_lock, flags);
629 wake_up_interruptible(&rtc->irq_queue);
630 kill_fasync(&rtc->async_queue, SIGIO, POLL_IN);
635 * @rtc: pointer to the rtc_device
639 void rtc_aie_update_irq(struct rtc_device *rtc)
641 rtc_handle_legacy_irq(rtc, 1, RTC_AF);
646 * @rtc: pointer to the rtc_device
650 void rtc_uie_update_irq(struct rtc_device *rtc)
652 rtc_handle_legacy_irq(rtc, 1, RTC_UF);
665 struct rtc_device *rtc;
669 rtc = container_of(timer, struct rtc_device, pie_timer);
671 period = NSEC_PER_SEC / rtc->irq_freq;
674 rtc_handle_legacy_irq(rtc, count, RTC_PF);
681 * @rtc: the rtc device
686 void rtc_update_irq(struct rtc_device *rtc,
689 if (IS_ERR_OR_NULL(rtc))
692 pm_stay_awake(rtc->dev.parent);
693 schedule_work(&rtc->irqwork);
700 struct rtc_device *rtc = NULL;
704 rtc = to_rtc_device(dev);
706 if (rtc) {
707 if (!try_module_get(rtc->owner)) {
709 rtc = NULL;
713 return rtc;
717 void rtc_class_close(struct rtc_device *rtc)
719 module_put(rtc->owner);
720 put_device(&rtc->dev);
724 static int rtc_update_hrtimer(struct rtc_device *rtc, int enabled)
733 * could be blocked on rtc->irq_task_lock and hrtimer_cancel()
736 if (hrtimer_try_to_cancel(&rtc->pie_timer) < 0)
740 ktime_t period = NSEC_PER_SEC / rtc->irq_freq;
742 hrtimer_start(&rtc->pie_timer, period, HRTIMER_MODE_REL);
749 * @rtc: the rtc device
756 int rtc_irq_set_state(struct rtc_device *rtc, int enabled)
760 while (rtc_update_hrtimer(rtc, enabled) < 0)
763 rtc->pie_enabled = enabled;
771 * @rtc: the rtc device
778 int rtc_irq_set_freq(struct rtc_device *rtc, int freq)
785 rtc->irq_freq = freq;
786 while (rtc->pie_enabled && rtc_update_hrtimer(rtc, 1) < 0)
795 * @rtc: rtc device
798 * Enqueues a timer onto the rtc devices timerqueue and sets
805 static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer)
807 struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue);
812 err = __rtc_read_time(rtc, &tm);
826 timerqueue_add(&rtc->timerqueue, &timer->node);
833 err = __rtc_set_alarm(rtc, &alarm);
835 pm_stay_awake(rtc->dev.parent);
836 schedule_work(&rtc->irqwork);
838 timerqueue_del(&rtc->timerqueue, &timer->node);
847 static void rtc_alarm_disable(struct rtc_device *rtc)
849 if (!rtc->ops || !rtc->ops->alarm_irq_enable)
852 rtc->ops->alarm_irq_enable(rtc->dev.parent, false);
858 * @rtc: rtc device
861 * Removes a timer onto the rtc devices timerqueue and sets
868 static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer)
870 struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue);
872 timerqueue_del(&rtc->timerqueue, &timer->node);
879 next = timerqueue_getnext(&rtc->timerqueue);
881 rtc_alarm_disable(rtc);
886 err = __rtc_set_alarm(rtc, &alarm);
888 pm_stay_awake(rtc->dev.parent);
889 schedule_work(&rtc->irqwork);
895 * rtc_timer_do_work - Expires rtc timers
898 * Expires rtc timers. Reprograms next alarm event if needed.
910 struct rtc_device *rtc =
913 mutex_lock(&rtc->ops_lock);
915 __rtc_read_time(rtc, &tm);
917 while ((next = timerqueue_getnext(&rtc->timerqueue))) {
923 timerqueue_del(&rtc->timerqueue, &timer->node);
927 timer->func(timer->rtc);
935 timerqueue_add(&rtc->timerqueue, &timer->node);
949 err = __rtc_set_alarm(rtc, &alarm);
957 timerqueue_del(&rtc->timerqueue, &timer->node);
960 dev_err(&rtc->dev, "__rtc_set_alarm: err=%d\n", err);
964 rtc_alarm_disable(rtc);
967 pm_relax(rtc->dev.parent);
968 mutex_unlock(&rtc->ops_lock);
974 * @rtc: pointer to the rtc_device
979 struct rtc_device *rtc)
984 timer->rtc = rtc;
988 * @ rtc: rtc device to be used
995 int rtc_timer_start(struct rtc_device *rtc, struct rtc_timer *timer,
1000 mutex_lock(&rtc->ops_lock);
1002 rtc_timer_remove(rtc, timer);
1007 ret = rtc_timer_enqueue(rtc, timer);
1009 mutex_unlock(&rtc->ops_lock);
1014 * @ rtc: rtc device to be used
1019 void rtc_timer_cancel(struct rtc_device *rtc, struct rtc_timer *timer)
1021 mutex_lock(&rtc->ops_lock);
1023 rtc_timer_remove(rtc, timer);
1024 mutex_unlock(&rtc->ops_lock);
1028 * rtc_read_offset - Read the amount of rtc offset in parts per billion
1029 * @rtc: rtc device to be used
1034 * Kernel interface to read rtc clock offset
1036 * If read_offset() is not implemented for the rtc, return -EINVAL
1038 int rtc_read_offset(struct rtc_device *rtc, long *offset)
1042 if (!rtc->ops)
1045 if (!rtc->ops->read_offset)
1048 mutex_lock(&rtc->ops_lock);
1049 ret = rtc->ops->read_offset(rtc->dev.parent, offset);
1050 mutex_unlock(&rtc->ops_lock);
1058 * @rtc: rtc device to be used
1061 * Some rtc's allow an adjustment to the average duration of a second
1069 * Kernel interface to adjust an rtc clock offset.
1071 * If the rtc offset is not setable (or not implemented), return -EINVAL
1073 int rtc_set_offset(struct rtc_device *rtc, long offset)
1077 if (!rtc->ops)
1080 if (!rtc->ops->set_offset)
1083 mutex_lock(&rtc->ops_lock);
1084 ret = rtc->ops->set_offset(rtc->dev.parent, offset);
1085 mutex_unlock(&rtc->ops_lock);