hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
kernel/drivers/rtc/interface.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0
12 /*
23 * RTC subsystem, interface functions
34 *
....@@ -5,11 +6,7 @@
56 * Author: Alessandro Zummo <a.zummo@towertech.it>
67 *
78 * based on arch/arm/common/rtctime.c
8
- *
9
- * This program is free software; you can redistribute it and/or modify
10
- * it under the terms of the GNU General Public License version 2 as
11
- * published by the Free Software Foundation.
12
-*/
9
+ */
1310
1411 #include <linux/rtc.h>
1512 #include <linux/sched.h>
....@@ -73,7 +70,7 @@
7370 time64_t time = rtc_tm_to_time64(tm);
7471 time64_t range_min = rtc->set_start_time ? rtc->start_secs :
7572 rtc->range_min;
76
- time64_t range_max = rtc->set_start_time ?
73
+ timeu64_t range_max = rtc->set_start_time ?
7774 (rtc->start_secs + rtc->range_max - rtc->range_min) :
7875 rtc->range_max;
7976
....@@ -87,11 +84,12 @@
8784 static int __rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm)
8885 {
8986 int err;
90
- if (!rtc->ops)
87
+
88
+ if (!rtc->ops) {
9189 err = -ENODEV;
92
- else if (!rtc->ops->read_time)
90
+ } else if (!rtc->ops->read_time) {
9391 err = -EINVAL;
94
- else {
92
+ } else {
9593 memset(tm, 0, sizeof(struct rtc_time));
9694 err = rtc->ops->read_time(rtc->dev.parent, tm);
9795 if (err < 0) {
....@@ -158,14 +156,7 @@
158156 err = -ENODEV;
159157 else if (rtc->ops->set_time)
160158 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
159
+ else
169160 err = -EINVAL;
170161
171162 pm_stay_awake(rtc->dev.parent);
....@@ -184,7 +175,8 @@
184175 }
185176 EXPORT_SYMBOL_GPL(rtc_set_time);
186177
187
-static int rtc_read_alarm_internal(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
178
+static int rtc_read_alarm_internal(struct rtc_device *rtc,
179
+ struct rtc_wkalrm *alarm)
188180 {
189181 int err;
190182
....@@ -192,11 +184,11 @@
192184 if (err)
193185 return err;
194186
195
- if (rtc->ops == NULL)
187
+ if (!rtc->ops) {
196188 err = -ENODEV;
197
- else if (!rtc->ops->read_alarm)
189
+ } else if (!rtc->ops->read_alarm) {
198190 err = -EINVAL;
199
- else {
191
+ } else {
200192 alarm->enabled = 0;
201193 alarm->pending = 0;
202194 alarm->time.tm_sec = -1;
....@@ -224,7 +216,7 @@
224216 int first_time = 1;
225217 time64_t t_now, t_alm;
226218 enum { none, day, month, year } missing = none;
227
- unsigned days;
219
+ unsigned int days;
228220
229221 /* The lower level RTC driver may return -1 in some fields,
230222 * creating invalid alarm->time values, for reasons like:
....@@ -293,10 +285,10 @@
293285 return err;
294286
295287 /* note that tm_sec is a "don't care" value here: */
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);
288
+ } while (before.tm_min != now.tm_min ||
289
+ before.tm_hour != now.tm_hour ||
290
+ before.tm_mon != now.tm_mon ||
291
+ before.tm_year != now.tm_year);
300292
301293 /* Fill in the missing alarm fields using the timestamp; we
302294 * know there's at least one since alarm->time is invalid.
....@@ -313,7 +305,7 @@
313305 alarm->time.tm_mday = now.tm_mday;
314306 missing = day;
315307 }
316
- if ((unsigned)alarm->time.tm_mon >= 12) {
308
+ if ((unsigned int)alarm->time.tm_mon >= 12) {
317309 alarm->time.tm_mon = now.tm_mon;
318310 if (missing == none)
319311 missing = month;
....@@ -338,7 +330,6 @@
338330 goto done;
339331
340332 switch (missing) {
341
-
342333 /* 24 hour rollover ... if it's now 10am Monday, an alarm that
343334 * that will trigger at 5am will do so at 5am Tuesday, which
344335 * could also be in the next month or year. This is a common
....@@ -358,14 +349,14 @@
358349 case month:
359350 dev_dbg(&rtc->dev, "alarm rollover: %s\n", "month");
360351 do {
361
- if (alarm->time.tm_mon < 11)
352
+ if (alarm->time.tm_mon < 11) {
362353 alarm->time.tm_mon++;
363
- else {
354
+ } else {
364355 alarm->time.tm_mon = 0;
365356 alarm->time.tm_year++;
366357 }
367358 days = rtc_month_days(alarm->time.tm_mon,
368
- alarm->time.tm_year);
359
+ alarm->time.tm_year);
369360 } while (days < alarm->time.tm_mday);
370361 break;
371362
....@@ -374,8 +365,8 @@
374365 dev_dbg(&rtc->dev, "alarm rollover: %s\n", "year");
375366 do {
376367 alarm->time.tm_year++;
377
- } while (!is_leap_year(alarm->time.tm_year + 1900)
378
- && rtc_valid_tm(&alarm->time) != 0);
368
+ } while (!is_leap_year(alarm->time.tm_year + 1900) &&
369
+ rtc_valid_tm(&alarm->time) != 0);
379370 break;
380371
381372 default:
....@@ -385,12 +376,9 @@
385376 err = rtc_valid_tm(&alarm->time);
386377
387378 done:
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
- }
379
+ if (err)
380
+ dev_warn(&rtc->dev, "invalid alarm value: %ptR\n",
381
+ &alarm->time);
394382
395383 return err;
396384 }
....@@ -402,11 +390,11 @@
402390 err = mutex_lock_interruptible(&rtc->ops_lock);
403391 if (err)
404392 return err;
405
- if (rtc->ops == NULL)
393
+ if (!rtc->ops) {
406394 err = -ENODEV;
407
- else if (!rtc->ops->read_alarm)
395
+ } else if (!rtc->ops->read_alarm) {
408396 err = -EINVAL;
409
- else {
397
+ } else {
410398 memset(alarm, 0, sizeof(struct rtc_wkalrm));
411399 alarm->enabled = rtc->aie_timer.enabled;
412400 alarm->time = rtc_ktime_to_tm(rtc->aie_timer.node.expires);
....@@ -515,7 +503,6 @@
515503 /* Alarm has to be enabled & in the future for us to enqueue it */
516504 if (alarm->enabled && (rtc_tm_to_ktime(now) <
517505 rtc->aie_timer.node.expires)) {
518
-
519506 rtc->aie_timer.enabled = 1;
520507 timerqueue_add(&rtc->timerqueue, &rtc->aie_timer.node);
521508 trace_rtc_timer_enqueue(&rtc->aie_timer);
....@@ -527,7 +514,9 @@
527514
528515 int rtc_alarm_irq_enable(struct rtc_device *rtc, unsigned int enabled)
529516 {
530
- int err = mutex_lock_interruptible(&rtc->ops_lock);
517
+ int err;
518
+
519
+ err = mutex_lock_interruptible(&rtc->ops_lock);
531520 if (err)
532521 return err;
533522
....@@ -556,7 +545,9 @@
556545
557546 int rtc_update_irq_enable(struct rtc_device *rtc, unsigned int enabled)
558547 {
559
- int err = mutex_lock_interruptible(&rtc->ops_lock);
548
+ int rc = 0, err;
549
+
550
+ err = mutex_lock_interruptible(&rtc->ops_lock);
560551 if (err)
561552 return err;
562553
....@@ -579,41 +570,52 @@
579570 struct rtc_time tm;
580571 ktime_t now, onesec;
581572
582
- __rtc_read_time(rtc, &tm);
573
+ rc = __rtc_read_time(rtc, &tm);
574
+ if (rc)
575
+ goto out;
583576 onesec = ktime_set(1, 0);
584577 now = rtc_tm_to_ktime(tm);
585578 rtc->uie_rtctimer.node.expires = ktime_add(now, onesec);
586579 rtc->uie_rtctimer.period = ktime_set(1, 0);
587580 err = rtc_timer_enqueue(rtc, &rtc->uie_rtctimer);
588
- } else
581
+ } else {
589582 rtc_timer_remove(rtc, &rtc->uie_rtctimer);
583
+ }
590584
591585 out:
592586 mutex_unlock(&rtc->ops_lock);
587
+
588
+ /*
589
+ * __rtc_read_time() failed, this probably means that the RTC time has
590
+ * never been set or less probably there is a transient error on the
591
+ * bus. In any case, avoid enabling emulation has this will fail when
592
+ * reading the time too.
593
+ */
594
+ if (rc)
595
+ return rc;
596
+
593597 #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
594598 /*
595
- * Enable emulation if the driver did not provide
596
- * the update_irq_enable function pointer or if returned
597
- * -EINVAL to signal that it has been configured without
598
- * interrupts or that are not available at the moment.
599
+ * Enable emulation if the driver returned -EINVAL to signal that it has
600
+ * been configured without interrupts or they are not available at the
601
+ * moment.
599602 */
600603 if (err == -EINVAL)
601604 err = rtc_dev_update_irq_enable_emul(rtc, enabled);
602605 #endif
603606 return err;
604
-
605607 }
606608 EXPORT_SYMBOL_GPL(rtc_update_irq_enable);
607
-
608609
609610 /**
610611 * rtc_handle_legacy_irq - AIE, UIE and PIE event hook
611612 * @rtc: pointer to the rtc device
613
+ * @num: number of occurence of the event
614
+ * @mode: type of the event, RTC_AF, RTC_UF of RTC_PF
612615 *
613616 * This function is called when an AIE, UIE or PIE mode interrupt
614617 * has occurred (or been emulated).
615618 *
616
- * Triggers the registered irq_task function callback.
617619 */
618620 void rtc_handle_legacy_irq(struct rtc_device *rtc, int num, int mode)
619621 {
....@@ -621,39 +623,34 @@
621623
622624 /* mark one irq of the appropriate mode */
623625 spin_lock_irqsave(&rtc->irq_lock, flags);
624
- rtc->irq_data = (rtc->irq_data + (num << 8)) | (RTC_IRQF|mode);
626
+ rtc->irq_data = (rtc->irq_data + (num << 8)) | (RTC_IRQF | mode);
625627 spin_unlock_irqrestore(&rtc->irq_lock, flags);
626628
627629 wake_up_interruptible(&rtc->irq_queue);
628630 kill_fasync(&rtc->async_queue, SIGIO, POLL_IN);
629631 }
630632
631
-
632633 /**
633634 * rtc_aie_update_irq - AIE mode rtctimer hook
634
- * @private: pointer to the rtc_device
635
+ * @rtc: pointer to the rtc_device
635636 *
636637 * This functions is called when the aie_timer expires.
637638 */
638
-void rtc_aie_update_irq(void *private)
639
+void rtc_aie_update_irq(struct rtc_device *rtc)
639640 {
640
- struct rtc_device *rtc = (struct rtc_device *)private;
641641 rtc_handle_legacy_irq(rtc, 1, RTC_AF);
642642 }
643643
644
-
645644 /**
646645 * rtc_uie_update_irq - UIE mode rtctimer hook
647
- * @private: pointer to the rtc_device
646
+ * @rtc: pointer to the rtc_device
648647 *
649648 * This functions is called when the uie_timer expires.
650649 */
651
-void rtc_uie_update_irq(void *private)
650
+void rtc_uie_update_irq(struct rtc_device *rtc)
652651 {
653
- struct rtc_device *rtc = (struct rtc_device *)private;
654652 rtc_handle_legacy_irq(rtc, 1, RTC_UF);
655653 }
656
-
657654
658655 /**
659656 * rtc_pie_update_irq - PIE mode hrtimer hook
....@@ -667,7 +664,8 @@
667664 {
668665 struct rtc_device *rtc;
669666 ktime_t period;
670
- int count;
667
+ u64 count;
668
+
671669 rtc = container_of(timer, struct rtc_device, pie_timer);
672670
673671 period = NSEC_PER_SEC / rtc->irq_freq;
....@@ -686,7 +684,7 @@
686684 * Context: any
687685 */
688686 void rtc_update_irq(struct rtc_device *rtc,
689
- unsigned long num, unsigned long events)
687
+ unsigned long num, unsigned long events)
690688 {
691689 if (IS_ERR_OR_NULL(rtc))
692690 return;
....@@ -696,21 +694,12 @@
696694 }
697695 EXPORT_SYMBOL_GPL(rtc_update_irq);
698696
699
-static int __rtc_match(struct device *dev, const void *data)
700
-{
701
- const char *name = data;
702
-
703
- if (strcmp(dev_name(dev), name) == 0)
704
- return 1;
705
- return 0;
706
-}
707
-
708697 struct rtc_device *rtc_class_open(const char *name)
709698 {
710699 struct device *dev;
711700 struct rtc_device *rtc = NULL;
712701
713
- dev = class_find_device(rtc_class, NULL, name, __rtc_match);
702
+ dev = class_find_device_by_name(rtc_class, name);
714703 if (dev)
715704 rtc = to_rtc_device(dev);
716705
....@@ -758,7 +747,6 @@
758747 /**
759748 * rtc_irq_set_state - enable/disable 2^N Hz periodic IRQs
760749 * @rtc: the rtc device
761
- * @task: currently registered with rtc_irq_register()
762750 * @enabled: true to enable periodic IRQs
763751 * Context: any
764752 *
....@@ -781,7 +769,6 @@
781769 /**
782770 * rtc_irq_set_freq - set 2^N Hz periodic IRQ frequency for IRQ
783771 * @rtc: the rtc device
784
- * @task: currently registered with rtc_irq_register()
785772 * @freq: positive frequency
786773 * Context: any
787774 *
....@@ -805,8 +792,8 @@
805792
806793 /**
807794 * rtc_timer_enqueue - Adds a rtc_timer to the rtc_device timerqueue
808
- * @rtc rtc device
809
- * @timer timer being added.
795
+ * @rtc: rtc device
796
+ * @timer: timer being added.
810797 *
811798 * Enqueues a timer onto the rtc devices timerqueue and sets
812799 * the next alarm event appropriately.
....@@ -820,9 +807,13 @@
820807 struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue);
821808 struct rtc_time tm;
822809 ktime_t now;
810
+ int err;
811
+
812
+ err = __rtc_read_time(rtc, &tm);
813
+ if (err)
814
+ return err;
823815
824816 timer->enabled = 1;
825
- __rtc_read_time(rtc, &tm);
826817 now = rtc_tm_to_ktime(tm);
827818
828819 /* Skip over expired timers */
....@@ -836,7 +827,7 @@
836827 trace_rtc_timer_enqueue(timer);
837828 if (!next || ktime_before(timer->node.expires, next->expires)) {
838829 struct rtc_wkalrm alarm;
839
- int err;
830
+
840831 alarm.time = rtc_ktime_to_tm(timer->node.expires);
841832 alarm.enabled = 1;
842833 err = __rtc_set_alarm(rtc, &alarm);
....@@ -864,8 +855,8 @@
864855
865856 /**
866857 * rtc_timer_remove - Removes a rtc_timer from the rtc_device timerqueue
867
- * @rtc rtc device
868
- * @timer timer being removed.
858
+ * @rtc: rtc device
859
+ * @timer: timer being removed.
869860 *
870861 * Removes a timer onto the rtc devices timerqueue and sets
871862 * the next alarm event appropriately.
....@@ -877,12 +868,14 @@
877868 static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer)
878869 {
879870 struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue);
871
+
880872 timerqueue_del(&rtc->timerqueue, &timer->node);
881873 trace_rtc_timer_dequeue(timer);
882874 timer->enabled = 0;
883875 if (next == &timer->node) {
884876 struct rtc_wkalrm alarm;
885877 int err;
878
+
886879 next = timerqueue_getnext(&rtc->timerqueue);
887880 if (!next) {
888881 rtc_alarm_disable(rtc);
....@@ -900,8 +893,7 @@
900893
901894 /**
902895 * rtc_timer_do_work - Expires rtc timers
903
- * @rtc rtc device
904
- * @timer timer being removed.
896
+ * @work: work item
905897 *
906898 * Expires rtc timers. Reprograms next alarm event if needed.
907899 * Called via worktask.
....@@ -932,7 +924,7 @@
932924 trace_rtc_timer_dequeue(timer);
933925 timer->enabled = 0;
934926 if (timer->func)
935
- timer->func(timer->private_data);
927
+ timer->func(timer->rtc);
936928
937929 trace_rtc_timer_fired(timer);
938930 /* Re-add/fwd periodic timers */
....@@ -955,9 +947,9 @@
955947 alarm.enabled = 1;
956948 reprogram:
957949 err = __rtc_set_alarm(rtc, &alarm);
958
- if (err == -ETIME)
950
+ if (err == -ETIME) {
959951 goto again;
960
- else if (err) {
952
+ } else if (err) {
961953 if (retry-- > 0)
962954 goto reprogram;
963955
....@@ -968,27 +960,28 @@
968960 dev_err(&rtc->dev, "__rtc_set_alarm: err=%d\n", err);
969961 goto again;
970962 }
971
- } else
963
+ } else {
972964 rtc_alarm_disable(rtc);
965
+ }
973966
974967 pm_relax(rtc->dev.parent);
975968 mutex_unlock(&rtc->ops_lock);
976969 }
977970
978
-
979971 /* rtc_timer_init - Initializes an rtc_timer
980972 * @timer: timer to be intiialized
981973 * @f: function pointer to be called when timer fires
982
- * @data: private data passed to function pointer
974
+ * @rtc: pointer to the rtc_device
983975 *
984976 * Kernel interface to initializing an rtc_timer.
985977 */
986
-void rtc_timer_init(struct rtc_timer *timer, void (*f)(void *p), void *data)
978
+void rtc_timer_init(struct rtc_timer *timer, void (*f)(struct rtc_device *r),
979
+ struct rtc_device *rtc)
987980 {
988981 timerqueue_init(&timer->node);
989982 timer->enabled = 0;
990983 timer->func = f;
991
- timer->private_data = data;
984
+ timer->rtc = rtc;
992985 }
993986
994987 /* rtc_timer_start - Sets an rtc_timer to fire in the future
....@@ -1000,9 +993,10 @@
1000993 * Kernel interface to set an rtc_timer
1001994 */
1002995 int rtc_timer_start(struct rtc_device *rtc, struct rtc_timer *timer,
1003
- ktime_t expires, ktime_t period)
996
+ ktime_t expires, ktime_t period)
1004997 {
1005998 int ret = 0;
999
+
10061000 mutex_lock(&rtc->ops_lock);
10071001 if (timer->enabled)
10081002 rtc_timer_remove(rtc, timer);
....@@ -1032,8 +1026,8 @@
10321026
10331027 /**
10341028 * rtc_read_offset - Read the amount of rtc offset in parts per billion
1035
- * @ rtc: rtc device to be used
1036
- * @ offset: the offset in parts per billion
1029
+ * @rtc: rtc device to be used
1030
+ * @offset: the offset in parts per billion
10371031 *
10381032 * see below for details.
10391033 *
....@@ -1061,8 +1055,8 @@
10611055
10621056 /**
10631057 * rtc_set_offset - Adjusts the duration of the average second
1064
- * @ rtc: rtc device to be used
1065
- * @ offset: the offset in parts per billion
1058
+ * @rtc: rtc device to be used
1059
+ * @offset: the offset in parts per billion
10661060 *
10671061 * Some rtc's allow an adjustment to the average duration of a second
10681062 * to compensate for differences in the actual clock rate due to temperature,