1
2
3
4
5
6
7
8
9
10
11
12
13
14#include <linux/rtc.h>
15#include <linux/sched.h>
16#include <linux/module.h>
17#include <linux/log2.h>
18#include <linux/workqueue.h>
19
20#define CREATE_TRACE_POINTS
21#include <trace/events/rtc.h>
22
23static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer);
24static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer);
25
26static void rtc_add_offset(struct rtc_device *rtc, struct rtc_time *tm)
27{
28 time64_t secs;
29
30 if (!rtc->offset_secs)
31 return;
32
33 secs = rtc_tm_to_time64(tm);
34
35
36
37
38
39
40
41 if ((rtc->start_secs > rtc->range_min && secs >= rtc->start_secs) ||
42 (rtc->start_secs < rtc->range_min &&
43 secs <= (rtc->start_secs + rtc->range_max - rtc->range_min)))
44 return;
45
46 rtc_time64_to_tm(secs + rtc->offset_secs, tm);
47}
48
49static void rtc_subtract_offset(struct rtc_device *rtc, struct rtc_time *tm)
50{
51 time64_t secs;
52
53 if (!rtc->offset_secs)
54 return;
55
56 secs = rtc_tm_to_time64(tm);
57
58
59
60
61
62
63
64 if (secs >= rtc->range_min && secs <= rtc->range_max)
65 return;
66
67 rtc_time64_to_tm(secs - rtc->offset_secs, tm);
68}
69
70static int rtc_valid_range(struct rtc_device *rtc, struct rtc_time *tm)
71{
72 if (rtc->range_min != rtc->range_max) {
73 time64_t time = rtc_tm_to_time64(tm);
74 time64_t range_min = rtc->set_start_time ? rtc->start_secs :
75 rtc->range_min;
76 time64_t range_max = rtc->set_start_time ?
77 (rtc->start_secs + rtc->range_max - rtc->range_min) :
78 rtc->range_max;
79
80 if (time < range_min || time > range_max)
81 return -ERANGE;
82 }
83
84 return 0;
85}
86
87static int __rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm)
88{
89 int err;
90 if (!rtc->ops)
91 err = -ENODEV;
92 else if (!rtc->ops->read_time)
93 err = -EINVAL;
94 else {
95 memset(tm, 0, sizeof(struct rtc_time));
96 err = rtc->ops->read_time(rtc->dev.parent, tm);
97 if (err < 0) {
98 dev_dbg(&rtc->dev, "read_time: fail to read: %d\n",
99 err);
100 return err;
101 }
102
103 rtc_add_offset(rtc, tm);
104
105 err = rtc_valid_tm(tm);
106 if (err < 0)
107 dev_dbg(&rtc->dev, "read_time: rtc_time isn't valid\n");
108 }
109 return err;
110}
111
112int rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm)
113{
114 int err;
115
116 err = mutex_lock_interruptible(&rtc->ops_lock);
117 if (err)
118 return err;
119
120 err = __rtc_read_time(rtc, tm);
121 mutex_unlock(&rtc->ops_lock);
122
123 trace_rtc_read_time(rtc_tm_to_time64(tm), err);
124 return err;
125}
126EXPORT_SYMBOL_GPL(rtc_read_time);
127
128int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm)
129{
130 int err, uie;
131
132 err = rtc_valid_tm(tm);
133 if (err != 0)
134 return err;
135
136 err = rtc_valid_range(rtc, tm);
137 if (err)
138 return err;
139
140 rtc_subtract_offset(rtc, tm);
141
142#ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
143 uie = rtc->uie_rtctimer.enabled || rtc->uie_irq_active;
144#else
145 uie = rtc->uie_rtctimer.enabled;
146#endif
147 if (uie) {
148 err = rtc_update_irq_enable(rtc, 0);
149 if (err)
150 return err;
151 }
152
153 err = mutex_lock_interruptible(&rtc->ops_lock);
154 if (err)
155 return err;
156
157 if (!rtc->ops)
158 err = -ENODEV;
159 else if (rtc->ops->set_time)
160 err = rtc->ops->set_time(rtc->dev.parent, tm);
161 else if (rtc->ops->set_mmss64) {
162 time64_t secs64 = rtc_tm_to_time64(tm);
163
164 err = rtc->ops->set_mmss64(rtc->dev.parent, secs64);
165 } else if (rtc->ops->set_mmss) {
166 time64_t secs64 = rtc_tm_to_time64(tm);
167 err = rtc->ops->set_mmss(rtc->dev.parent, secs64);
168 } else
169 err = -EINVAL;
170
171 pm_stay_awake(rtc->dev.parent);
172 mutex_unlock(&rtc->ops_lock);
173
174 schedule_work(&rtc->irqwork);
175
176 if (uie) {
177 err = rtc_update_irq_enable(rtc, 1);
178 if (err)
179 return err;
180 }
181
182 trace_rtc_set_time(rtc_tm_to_time64(tm), err);
183 return err;
184}
185EXPORT_SYMBOL_GPL(rtc_set_time);
186
187static int rtc_read_alarm_internal(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
188{
189 int err;
190
191 err = mutex_lock_interruptible(&rtc->ops_lock);
192 if (err)
193 return err;
194
195 if (rtc->ops == NULL)
196 err = -ENODEV;
197 else if (!rtc->ops->read_alarm)
198 err = -EINVAL;
199 else {
200 alarm->enabled = 0;
201 alarm->pending = 0;
202 alarm->time.tm_sec = -1;
203 alarm->time.tm_min = -1;
204 alarm->time.tm_hour = -1;
205 alarm->time.tm_mday = -1;
206 alarm->time.tm_mon = -1;
207 alarm->time.tm_year = -1;
208 alarm->time.tm_wday = -1;
209 alarm->time.tm_yday = -1;
210 alarm->time.tm_isdst = -1;
211 err = rtc->ops->read_alarm(rtc->dev.parent, alarm);
212 }
213
214 mutex_unlock(&rtc->ops_lock);
215
216 trace_rtc_read_alarm(rtc_tm_to_time64(&alarm->time), err);
217 return err;
218}
219
220int __rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
221{
222 int err;
223 struct rtc_time before, now;
224 int first_time = 1;
225 time64_t t_now, t_alm;
226 enum { none, day, month, year } missing = none;
227 unsigned days;
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271 err = rtc_read_time(rtc, &before);
272 if (err < 0)
273 return err;
274 do {
275 if (!first_time)
276 memcpy(&before, &now, sizeof(struct rtc_time));
277 first_time = 0;
278
279
280 err = rtc_read_alarm_internal(rtc, alarm);
281 if (err)
282 return err;
283
284
285 if (rtc_valid_tm(&alarm->time) == 0) {
286 rtc_add_offset(rtc, &alarm->time);
287 return 0;
288 }
289
290
291 err = rtc_read_time(rtc, &now);
292 if (err < 0)
293 return err;
294
295
296 } while ( before.tm_min != now.tm_min
297 || before.tm_hour != now.tm_hour
298 || before.tm_mon != now.tm_mon
299 || before.tm_year != now.tm_year);
300
301
302
303
304 if (alarm->time.tm_sec == -1)
305 alarm->time.tm_sec = now.tm_sec;
306 if (alarm->time.tm_min == -1)
307 alarm->time.tm_min = now.tm_min;
308 if (alarm->time.tm_hour == -1)
309 alarm->time.tm_hour = now.tm_hour;
310
311
312 if (alarm->time.tm_mday < 1 || alarm->time.tm_mday > 31) {
313 alarm->time.tm_mday = now.tm_mday;
314 missing = day;
315 }
316 if ((unsigned)alarm->time.tm_mon >= 12) {
317 alarm->time.tm_mon = now.tm_mon;
318 if (missing == none)
319 missing = month;
320 }
321 if (alarm->time.tm_year == -1) {
322 alarm->time.tm_year = now.tm_year;
323 if (missing == none)
324 missing = year;
325 }
326
327
328
329
330 err = rtc_valid_tm(&alarm->time);
331 if (err)
332 goto done;
333
334
335 t_now = rtc_tm_to_time64(&now);
336 t_alm = rtc_tm_to_time64(&alarm->time);
337 if (t_now < t_alm)
338 goto done;
339
340 switch (missing) {
341
342
343
344
345
346
347 case day:
348 dev_dbg(&rtc->dev, "alarm rollover: %s\n", "day");
349 t_alm += 24 * 60 * 60;
350 rtc_time64_to_tm(t_alm, &alarm->time);
351 break;
352
353
354
355
356
357
358 case month:
359 dev_dbg(&rtc->dev, "alarm rollover: %s\n", "month");
360 do {
361 if (alarm->time.tm_mon < 11)
362 alarm->time.tm_mon++;
363 else {
364 alarm->time.tm_mon = 0;
365 alarm->time.tm_year++;
366 }
367 days = rtc_month_days(alarm->time.tm_mon,
368 alarm->time.tm_year);
369 } while (days < alarm->time.tm_mday);
370 break;
371
372
373 case year:
374 dev_dbg(&rtc->dev, "alarm rollover: %s\n", "year");
375 do {
376 alarm->time.tm_year++;
377 } while (!is_leap_year(alarm->time.tm_year + 1900)
378 && rtc_valid_tm(&alarm->time) != 0);
379 break;
380
381 default:
382 dev_warn(&rtc->dev, "alarm rollover not handled\n");
383 }
384
385 err = rtc_valid_tm(&alarm->time);
386
387done:
388 if (err) {
389 dev_warn(&rtc->dev, "invalid alarm value: %d-%d-%d %d:%d:%d\n",
390 alarm->time.tm_year + 1900, alarm->time.tm_mon + 1,
391 alarm->time.tm_mday, alarm->time.tm_hour, alarm->time.tm_min,
392 alarm->time.tm_sec);
393 }
394
395 return err;
396}
397
398int rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
399{
400 int err;
401
402 err = mutex_lock_interruptible(&rtc->ops_lock);
403 if (err)
404 return err;
405 if (rtc->ops == NULL)
406 err = -ENODEV;
407 else if (!rtc->ops->read_alarm)
408 err = -EINVAL;
409 else {
410 memset(alarm, 0, sizeof(struct rtc_wkalrm));
411 alarm->enabled = rtc->aie_timer.enabled;
412 alarm->time = rtc_ktime_to_tm(rtc->aie_timer.node.expires);
413 }
414 mutex_unlock(&rtc->ops_lock);
415
416 trace_rtc_read_alarm(rtc_tm_to_time64(&alarm->time), err);
417 return err;
418}
419EXPORT_SYMBOL_GPL(rtc_read_alarm);
420
421static int __rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
422{
423 struct rtc_time tm;
424 time64_t now, scheduled;
425 int err;
426
427 err = rtc_valid_tm(&alarm->time);
428 if (err)
429 return err;
430
431 scheduled = rtc_tm_to_time64(&alarm->time);
432
433
434 err = __rtc_read_time(rtc, &tm);
435 if (err)
436 return err;
437 now = rtc_tm_to_time64(&tm);
438 if (scheduled <= now)
439 return -ETIME;
440
441
442
443
444
445
446
447 rtc_subtract_offset(rtc, &alarm->time);
448
449 if (!rtc->ops)
450 err = -ENODEV;
451 else if (!rtc->ops->set_alarm)
452 err = -EINVAL;
453 else
454 err = rtc->ops->set_alarm(rtc->dev.parent, alarm);
455
456 trace_rtc_set_alarm(rtc_tm_to_time64(&alarm->time), err);
457 return err;
458}
459
460int rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
461{
462 int err;
463
464 if (!rtc->ops)
465 return -ENODEV;
466 else if (!rtc->ops->set_alarm)
467 return -EINVAL;
468
469 err = rtc_valid_tm(&alarm->time);
470 if (err != 0)
471 return err;
472
473 err = rtc_valid_range(rtc, &alarm->time);
474 if (err)
475 return err;
476
477 err = mutex_lock_interruptible(&rtc->ops_lock);
478 if (err)
479 return err;
480 if (rtc->aie_timer.enabled)
481 rtc_timer_remove(rtc, &rtc->aie_timer);
482
483 rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time);
484 rtc->aie_timer.period = 0;
485 if (alarm->enabled)
486 err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
487
488 mutex_unlock(&rtc->ops_lock);
489
490 return err;
491}
492EXPORT_SYMBOL_GPL(rtc_set_alarm);
493
494
495int rtc_initialize_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
496{
497 int err;
498 struct rtc_time now;
499
500 err = rtc_valid_tm(&alarm->time);
501 if (err != 0)
502 return err;
503
504 err = rtc_read_time(rtc, &now);
505 if (err)
506 return err;
507
508 err = mutex_lock_interruptible(&rtc->ops_lock);
509 if (err)
510 return err;
511
512 rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time);
513 rtc->aie_timer.period = 0;
514
515
516 if (alarm->enabled && (rtc_tm_to_ktime(now) <
517 rtc->aie_timer.node.expires)) {
518
519 rtc->aie_timer.enabled = 1;
520 timerqueue_add(&rtc->timerqueue, &rtc->aie_timer.node);
521 trace_rtc_timer_enqueue(&rtc->aie_timer);
522 }
523 mutex_unlock(&rtc->ops_lock);
524 return err;
525}
526EXPORT_SYMBOL_GPL(rtc_initialize_alarm);
527
528int rtc_alarm_irq_enable(struct rtc_device *rtc, unsigned int enabled)
529{
530 int err = mutex_lock_interruptible(&rtc->ops_lock);
531 if (err)
532 return err;
533
534 if (rtc->aie_timer.enabled != enabled) {
535 if (enabled)
536 err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
537 else
538 rtc_timer_remove(rtc, &rtc->aie_timer);
539 }
540
541 if (err)
542 ;
543 else if (!rtc->ops)
544 err = -ENODEV;
545 else if (!rtc->ops->alarm_irq_enable)
546 err = -EINVAL;
547 else
548 err = rtc->ops->alarm_irq_enable(rtc->dev.parent, enabled);
549
550 mutex_unlock(&rtc->ops_lock);
551
552 trace_rtc_alarm_irq_enable(enabled, err);
553 return err;
554}
555EXPORT_SYMBOL_GPL(rtc_alarm_irq_enable);
556
557int rtc_update_irq_enable(struct rtc_device *rtc, unsigned int enabled)
558{
559 int rc = 0, err;
560
561 err = mutex_lock_interruptible(&rtc->ops_lock);
562 if (err)
563 return err;
564
565#ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
566 if (enabled == 0 && rtc->uie_irq_active) {
567 mutex_unlock(&rtc->ops_lock);
568 return rtc_dev_update_irq_enable_emul(rtc, 0);
569 }
570#endif
571
572 if (rtc->uie_rtctimer.enabled == enabled)
573 goto out;
574
575 if (rtc->uie_unsupported) {
576 err = -EINVAL;
577 goto out;
578 }
579
580 if (enabled) {
581 struct rtc_time tm;
582 ktime_t now, onesec;
583
584 rc = __rtc_read_time(rtc, &tm);
585 if (rc)
586 goto out;
587 onesec = ktime_set(1, 0);
588 now = rtc_tm_to_ktime(tm);
589 rtc->uie_rtctimer.node.expires = ktime_add(now, onesec);
590 rtc->uie_rtctimer.period = ktime_set(1, 0);
591 err = rtc_timer_enqueue(rtc, &rtc->uie_rtctimer);
592 } else
593 rtc_timer_remove(rtc, &rtc->uie_rtctimer);
594
595out:
596 mutex_unlock(&rtc->ops_lock);
597
598
599
600
601
602
603
604 if (rc)
605 return rc;
606
607#ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
608
609
610
611
612
613 if (err == -EINVAL)
614 err = rtc_dev_update_irq_enable_emul(rtc, enabled);
615#endif
616 return err;
617
618}
619EXPORT_SYMBOL_GPL(rtc_update_irq_enable);
620
621
622
623
624
625
626
627
628
629
630
631void rtc_handle_legacy_irq(struct rtc_device *rtc, int num, int mode)
632{
633 unsigned long flags;
634
635
636 spin_lock_irqsave(&rtc->irq_lock, flags);
637 rtc->irq_data = (rtc->irq_data + (num << 8)) | (RTC_IRQF|mode);
638 spin_unlock_irqrestore(&rtc->irq_lock, flags);
639
640
641 spin_lock_irqsave(&rtc->irq_task_lock, flags);
642 if (rtc->irq_task)
643 rtc->irq_task->func(rtc->irq_task->private_data);
644 spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
645
646 wake_up_interruptible(&rtc->irq_queue);
647 kill_fasync(&rtc->async_queue, SIGIO, POLL_IN);
648}
649
650
651
652
653
654
655
656
657void rtc_aie_update_irq(void *private)
658{
659 struct rtc_device *rtc = (struct rtc_device *)private;
660 rtc_handle_legacy_irq(rtc, 1, RTC_AF);
661}
662
663
664
665
666
667
668
669
670void rtc_uie_update_irq(void *private)
671{
672 struct rtc_device *rtc = (struct rtc_device *)private;
673 rtc_handle_legacy_irq(rtc, 1, RTC_UF);
674}
675
676
677
678
679
680
681
682
683
684
685enum hrtimer_restart rtc_pie_update_irq(struct hrtimer *timer)
686{
687 struct rtc_device *rtc;
688 ktime_t period;
689 int count;
690 rtc = container_of(timer, struct rtc_device, pie_timer);
691
692 period = NSEC_PER_SEC / rtc->irq_freq;
693 count = hrtimer_forward_now(timer, period);
694
695 rtc_handle_legacy_irq(rtc, count, RTC_PF);
696
697 return HRTIMER_RESTART;
698}
699
700
701
702
703
704
705
706
707void rtc_update_irq(struct rtc_device *rtc,
708 unsigned long num, unsigned long events)
709{
710 if (IS_ERR_OR_NULL(rtc))
711 return;
712
713 pm_stay_awake(rtc->dev.parent);
714 schedule_work(&rtc->irqwork);
715}
716EXPORT_SYMBOL_GPL(rtc_update_irq);
717
718struct rtc_device *rtc_class_open(const char *name)
719{
720 struct device *dev;
721 struct rtc_device *rtc = NULL;
722
723 dev = class_find_device_by_name(rtc_class, name);
724 if (dev)
725 rtc = to_rtc_device(dev);
726
727 if (rtc) {
728 if (!try_module_get(rtc->owner)) {
729 put_device(dev);
730 rtc = NULL;
731 }
732 }
733
734 return rtc;
735}
736EXPORT_SYMBOL_GPL(rtc_class_open);
737
738void rtc_class_close(struct rtc_device *rtc)
739{
740 module_put(rtc->owner);
741 put_device(&rtc->dev);
742}
743EXPORT_SYMBOL_GPL(rtc_class_close);
744
745int rtc_irq_register(struct rtc_device *rtc, struct rtc_task *task)
746{
747 int retval = -EBUSY;
748
749 if (task == NULL || task->func == NULL)
750 return -EINVAL;
751
752
753 if (test_and_set_bit_lock(RTC_DEV_BUSY, &rtc->flags))
754 return -EBUSY;
755
756 spin_lock_irq(&rtc->irq_task_lock);
757 if (rtc->irq_task == NULL) {
758 rtc->irq_task = task;
759 retval = 0;
760 }
761 spin_unlock_irq(&rtc->irq_task_lock);
762
763 clear_bit_unlock(RTC_DEV_BUSY, &rtc->flags);
764
765 return retval;
766}
767EXPORT_SYMBOL_GPL(rtc_irq_register);
768
769void rtc_irq_unregister(struct rtc_device *rtc, struct rtc_task *task)
770{
771 spin_lock_irq(&rtc->irq_task_lock);
772 if (rtc->irq_task == task)
773 rtc->irq_task = NULL;
774 spin_unlock_irq(&rtc->irq_task_lock);
775}
776EXPORT_SYMBOL_GPL(rtc_irq_unregister);
777
778static int rtc_update_hrtimer(struct rtc_device *rtc, int enabled)
779{
780
781
782
783
784
785
786
787
788
789
790 if (hrtimer_try_to_cancel(&rtc->pie_timer) < 0)
791 return -1;
792
793 if (enabled) {
794 ktime_t period = NSEC_PER_SEC / rtc->irq_freq;
795
796 hrtimer_start(&rtc->pie_timer, period, HRTIMER_MODE_REL);
797 }
798 return 0;
799}
800
801
802
803
804
805
806
807
808
809
810
811int rtc_irq_set_state(struct rtc_device *rtc, struct rtc_task *task, int enabled)
812{
813 int err = 0;
814 unsigned long flags;
815
816retry:
817 spin_lock_irqsave(&rtc->irq_task_lock, flags);
818 if (rtc->irq_task != NULL && task == NULL)
819 err = -EBUSY;
820 else if (rtc->irq_task != task)
821 err = -EACCES;
822 else {
823 if (rtc_update_hrtimer(rtc, enabled) < 0) {
824 spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
825 cpu_relax();
826 goto retry;
827 }
828 rtc->pie_enabled = enabled;
829 }
830 spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
831
832 trace_rtc_irq_set_state(enabled, err);
833 return err;
834}
835EXPORT_SYMBOL_GPL(rtc_irq_set_state);
836
837
838
839
840
841
842
843
844
845
846
847int rtc_irq_set_freq(struct rtc_device *rtc, struct rtc_task *task, int freq)
848{
849 int err = 0;
850 unsigned long flags;
851
852 if (freq <= 0 || freq > RTC_MAX_FREQ)
853 return -EINVAL;
854retry:
855 spin_lock_irqsave(&rtc->irq_task_lock, flags);
856 if (rtc->irq_task != NULL && task == NULL)
857 err = -EBUSY;
858 else if (rtc->irq_task != task)
859 err = -EACCES;
860 else {
861 rtc->irq_freq = freq;
862 if (rtc->pie_enabled && rtc_update_hrtimer(rtc, 1) < 0) {
863 spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
864 cpu_relax();
865 goto retry;
866 }
867 }
868 spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
869
870 trace_rtc_irq_set_freq(freq, err);
871 return err;
872}
873EXPORT_SYMBOL_GPL(rtc_irq_set_freq);
874
875
876
877
878
879
880
881
882
883
884
885
886
887static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer)
888{
889 struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue);
890 struct rtc_time tm;
891 ktime_t now;
892
893 timer->enabled = 1;
894 __rtc_read_time(rtc, &tm);
895 now = rtc_tm_to_ktime(tm);
896
897
898 while (next) {
899 if (next->expires >= now)
900 break;
901 next = timerqueue_iterate_next(next);
902 }
903
904 timerqueue_add(&rtc->timerqueue, &timer->node);
905 trace_rtc_timer_enqueue(timer);
906 if (!next || ktime_before(timer->node.expires, next->expires)) {
907 struct rtc_wkalrm alarm;
908 int err;
909 alarm.time = rtc_ktime_to_tm(timer->node.expires);
910 alarm.enabled = 1;
911 err = __rtc_set_alarm(rtc, &alarm);
912 if (err == -ETIME) {
913 pm_stay_awake(rtc->dev.parent);
914 schedule_work(&rtc->irqwork);
915 } else if (err) {
916 timerqueue_del(&rtc->timerqueue, &timer->node);
917 trace_rtc_timer_dequeue(timer);
918 timer->enabled = 0;
919 return err;
920 }
921 }
922 return 0;
923}
924
925static void rtc_alarm_disable(struct rtc_device *rtc)
926{
927 if (!rtc->ops || !rtc->ops->alarm_irq_enable)
928 return;
929
930 rtc->ops->alarm_irq_enable(rtc->dev.parent, false);
931 trace_rtc_alarm_irq_enable(0, 0);
932}
933
934
935
936
937
938
939
940
941
942
943
944
945
946static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer)
947{
948 struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue);
949 timerqueue_del(&rtc->timerqueue, &timer->node);
950 trace_rtc_timer_dequeue(timer);
951 timer->enabled = 0;
952 if (next == &timer->node) {
953 struct rtc_wkalrm alarm;
954 int err;
955 next = timerqueue_getnext(&rtc->timerqueue);
956 if (!next) {
957 rtc_alarm_disable(rtc);
958 return;
959 }
960 alarm.time = rtc_ktime_to_tm(next->expires);
961 alarm.enabled = 1;
962 err = __rtc_set_alarm(rtc, &alarm);
963 if (err == -ETIME) {
964 pm_stay_awake(rtc->dev.parent);
965 schedule_work(&rtc->irqwork);
966 }
967 }
968}
969
970
971
972
973
974
975
976
977
978
979
980void rtc_timer_do_work(struct work_struct *work)
981{
982 struct rtc_timer *timer;
983 struct timerqueue_node *next;
984 ktime_t now;
985 struct rtc_time tm;
986
987 struct rtc_device *rtc =
988 container_of(work, struct rtc_device, irqwork);
989
990 mutex_lock(&rtc->ops_lock);
991again:
992 __rtc_read_time(rtc, &tm);
993 now = rtc_tm_to_ktime(tm);
994 while ((next = timerqueue_getnext(&rtc->timerqueue))) {
995 if (next->expires > now)
996 break;
997
998
999 timer = container_of(next, struct rtc_timer, node);
1000 timerqueue_del(&rtc->timerqueue, &timer->node);
1001 trace_rtc_timer_dequeue(timer);
1002 timer->enabled = 0;
1003 if (timer->task.func)
1004 timer->task.func(timer->task.private_data);
1005
1006 trace_rtc_timer_fired(timer);
1007
1008 if (ktime_to_ns(timer->period)) {
1009 timer->node.expires = ktime_add(timer->node.expires,
1010 timer->period);
1011 timer->enabled = 1;
1012 timerqueue_add(&rtc->timerqueue, &timer->node);
1013 trace_rtc_timer_enqueue(timer);
1014 }
1015 }
1016
1017
1018 if (next) {
1019 struct rtc_wkalrm alarm;
1020 int err;
1021 int retry = 3;
1022
1023 alarm.time = rtc_ktime_to_tm(next->expires);
1024 alarm.enabled = 1;
1025reprogram:
1026 err = __rtc_set_alarm(rtc, &alarm);
1027 if (err == -ETIME)
1028 goto again;
1029 else if (err) {
1030 if (retry-- > 0)
1031 goto reprogram;
1032
1033 timer = container_of(next, struct rtc_timer, node);
1034 timerqueue_del(&rtc->timerqueue, &timer->node);
1035 trace_rtc_timer_dequeue(timer);
1036 timer->enabled = 0;
1037 dev_err(&rtc->dev, "__rtc_set_alarm: err=%d\n", err);
1038 goto again;
1039 }
1040 } else
1041 rtc_alarm_disable(rtc);
1042
1043 pm_relax(rtc->dev.parent);
1044 mutex_unlock(&rtc->ops_lock);
1045}
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055void rtc_timer_init(struct rtc_timer *timer, void (*f)(void *p), void *data)
1056{
1057 timerqueue_init(&timer->node);
1058 timer->enabled = 0;
1059 timer->task.func = f;
1060 timer->task.private_data = data;
1061}
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071int rtc_timer_start(struct rtc_device *rtc, struct rtc_timer *timer,
1072 ktime_t expires, ktime_t period)
1073{
1074 int ret = 0;
1075 mutex_lock(&rtc->ops_lock);
1076 if (timer->enabled)
1077 rtc_timer_remove(rtc, timer);
1078
1079 timer->node.expires = expires;
1080 timer->period = period;
1081
1082 ret = rtc_timer_enqueue(rtc, timer);
1083
1084 mutex_unlock(&rtc->ops_lock);
1085 return ret;
1086}
1087
1088
1089
1090
1091
1092
1093
1094void rtc_timer_cancel(struct rtc_device *rtc, struct rtc_timer *timer)
1095{
1096 mutex_lock(&rtc->ops_lock);
1097 if (timer->enabled)
1098 rtc_timer_remove(rtc, timer);
1099 mutex_unlock(&rtc->ops_lock);
1100}
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113int rtc_read_offset(struct rtc_device *rtc, long *offset)
1114{
1115 int ret;
1116
1117 if (!rtc->ops)
1118 return -ENODEV;
1119
1120 if (!rtc->ops->read_offset)
1121 return -EINVAL;
1122
1123 mutex_lock(&rtc->ops_lock);
1124 ret = rtc->ops->read_offset(rtc->dev.parent, offset);
1125 mutex_unlock(&rtc->ops_lock);
1126
1127 trace_rtc_read_offset(*offset, ret);
1128 return ret;
1129}
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148int rtc_set_offset(struct rtc_device *rtc, long offset)
1149{
1150 int ret;
1151
1152 if (!rtc->ops)
1153 return -ENODEV;
1154
1155 if (!rtc->ops->set_offset)
1156 return -EINVAL;
1157
1158 mutex_lock(&rtc->ops_lock);
1159 ret = rtc->ops->set_offset(rtc->dev.parent, offset);
1160 mutex_unlock(&rtc->ops_lock);
1161
1162 trace_rtc_set_offset(offset, ret);
1163 return ret;
1164}
1165