aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/rtc/interface.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/rtc/interface.c')
-rw-r--r--drivers/rtc/interface.c107
1 files changed, 51 insertions, 56 deletions
diff --git a/drivers/rtc/interface.c b/drivers/rtc/interface.c
index 98d9c87b0d1b..4124f4dd376b 100644
--- a/drivers/rtc/interface.c
+++ b/drivers/rtc/interface.c
@@ -1,3 +1,4 @@
1// SPDX-License-Identifier: GPL-2.0
1/* 2/*
2 * RTC subsystem, interface functions 3 * RTC subsystem, interface functions
3 * 4 *
@@ -5,11 +6,7 @@
5 * Author: Alessandro Zummo <a.zummo@towertech.it> 6 * Author: Alessandro Zummo <a.zummo@towertech.it>
6 * 7 *
7 * based on arch/arm/common/rtctime.c 8 * based on arch/arm/common/rtctime.c
8 * 9 */
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*/
13 10
14#include <linux/rtc.h> 11#include <linux/rtc.h>
15#include <linux/sched.h> 12#include <linux/sched.h>
@@ -87,11 +84,12 @@ static int rtc_valid_range(struct rtc_device *rtc, struct rtc_time *tm)
87static int __rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm) 84static int __rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm)
88{ 85{
89 int err; 86 int err;
90 if (!rtc->ops) 87
88 if (!rtc->ops) {
91 err = -ENODEV; 89 err = -ENODEV;
92 else if (!rtc->ops->read_time) 90 } else if (!rtc->ops->read_time) {
93 err = -EINVAL; 91 err = -EINVAL;
94 else { 92 } else {
95 memset(tm, 0, sizeof(struct rtc_time)); 93 memset(tm, 0, sizeof(struct rtc_time));
96 err = rtc->ops->read_time(rtc->dev.parent, tm); 94 err = rtc->ops->read_time(rtc->dev.parent, tm);
97 if (err < 0) { 95 if (err < 0) {
@@ -147,14 +145,7 @@ int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm)
147 err = -ENODEV; 145 err = -ENODEV;
148 else if (rtc->ops->set_time) 146 else if (rtc->ops->set_time)
149 err = rtc->ops->set_time(rtc->dev.parent, tm); 147 err = rtc->ops->set_time(rtc->dev.parent, tm);
150 else if (rtc->ops->set_mmss64) { 148 else
151 time64_t secs64 = rtc_tm_to_time64(tm);
152
153 err = rtc->ops->set_mmss64(rtc->dev.parent, secs64);
154 } else if (rtc->ops->set_mmss) {
155 time64_t secs64 = rtc_tm_to_time64(tm);
156 err = rtc->ops->set_mmss(rtc->dev.parent, secs64);
157 } else
158 err = -EINVAL; 149 err = -EINVAL;
159 150
160 pm_stay_awake(rtc->dev.parent); 151 pm_stay_awake(rtc->dev.parent);
@@ -167,7 +158,8 @@ int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm)
167} 158}
168EXPORT_SYMBOL_GPL(rtc_set_time); 159EXPORT_SYMBOL_GPL(rtc_set_time);
169 160
170static int rtc_read_alarm_internal(struct rtc_device *rtc, struct rtc_wkalrm *alarm) 161static int rtc_read_alarm_internal(struct rtc_device *rtc,
162 struct rtc_wkalrm *alarm)
171{ 163{
172 int err; 164 int err;
173 165
@@ -175,11 +167,11 @@ static int rtc_read_alarm_internal(struct rtc_device *rtc, struct rtc_wkalrm *al
175 if (err) 167 if (err)
176 return err; 168 return err;
177 169
178 if (rtc->ops == NULL) 170 if (!rtc->ops) {
179 err = -ENODEV; 171 err = -ENODEV;
180 else if (!rtc->ops->read_alarm) 172 } else if (!rtc->ops->read_alarm) {
181 err = -EINVAL; 173 err = -EINVAL;
182 else { 174 } else {
183 alarm->enabled = 0; 175 alarm->enabled = 0;
184 alarm->pending = 0; 176 alarm->pending = 0;
185 alarm->time.tm_sec = -1; 177 alarm->time.tm_sec = -1;
@@ -207,7 +199,7 @@ int __rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
207 int first_time = 1; 199 int first_time = 1;
208 time64_t t_now, t_alm; 200 time64_t t_now, t_alm;
209 enum { none, day, month, year } missing = none; 201 enum { none, day, month, year } missing = none;
210 unsigned days; 202 unsigned int days;
211 203
212 /* The lower level RTC driver may return -1 in some fields, 204 /* The lower level RTC driver may return -1 in some fields,
213 * creating invalid alarm->time values, for reasons like: 205 * creating invalid alarm->time values, for reasons like:
@@ -276,10 +268,10 @@ int __rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
276 return err; 268 return err;
277 269
278 /* note that tm_sec is a "don't care" value here: */ 270 /* note that tm_sec is a "don't care" value here: */
279 } while ( before.tm_min != now.tm_min 271 } while (before.tm_min != now.tm_min ||
280 || before.tm_hour != now.tm_hour 272 before.tm_hour != now.tm_hour ||
281 || before.tm_mon != now.tm_mon 273 before.tm_mon != now.tm_mon ||
282 || before.tm_year != now.tm_year); 274 before.tm_year != now.tm_year);
283 275
284 /* Fill in the missing alarm fields using the timestamp; we 276 /* Fill in the missing alarm fields using the timestamp; we
285 * know there's at least one since alarm->time is invalid. 277 * know there's at least one since alarm->time is invalid.
@@ -296,7 +288,7 @@ int __rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
296 alarm->time.tm_mday = now.tm_mday; 288 alarm->time.tm_mday = now.tm_mday;
297 missing = day; 289 missing = day;
298 } 290 }
299 if ((unsigned)alarm->time.tm_mon >= 12) { 291 if ((unsigned int)alarm->time.tm_mon >= 12) {
300 alarm->time.tm_mon = now.tm_mon; 292 alarm->time.tm_mon = now.tm_mon;
301 if (missing == none) 293 if (missing == none)
302 missing = month; 294 missing = month;
@@ -321,7 +313,6 @@ int __rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
321 goto done; 313 goto done;
322 314
323 switch (missing) { 315 switch (missing) {
324
325 /* 24 hour rollover ... if it's now 10am Monday, an alarm that 316 /* 24 hour rollover ... if it's now 10am Monday, an alarm that
326 * that will trigger at 5am will do so at 5am Tuesday, which 317 * that will trigger at 5am will do so at 5am Tuesday, which
327 * could also be in the next month or year. This is a common 318 * could also be in the next month or year. This is a common
@@ -341,14 +332,14 @@ int __rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
341 case month: 332 case month:
342 dev_dbg(&rtc->dev, "alarm rollover: %s\n", "month"); 333 dev_dbg(&rtc->dev, "alarm rollover: %s\n", "month");
343 do { 334 do {
344 if (alarm->time.tm_mon < 11) 335 if (alarm->time.tm_mon < 11) {
345 alarm->time.tm_mon++; 336 alarm->time.tm_mon++;
346 else { 337 } else {
347 alarm->time.tm_mon = 0; 338 alarm->time.tm_mon = 0;
348 alarm->time.tm_year++; 339 alarm->time.tm_year++;
349 } 340 }
350 days = rtc_month_days(alarm->time.tm_mon, 341 days = rtc_month_days(alarm->time.tm_mon,
351 alarm->time.tm_year); 342 alarm->time.tm_year);
352 } while (days < alarm->time.tm_mday); 343 } while (days < alarm->time.tm_mday);
353 break; 344 break;
354 345
@@ -357,8 +348,8 @@ int __rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
357 dev_dbg(&rtc->dev, "alarm rollover: %s\n", "year"); 348 dev_dbg(&rtc->dev, "alarm rollover: %s\n", "year");
358 do { 349 do {
359 alarm->time.tm_year++; 350 alarm->time.tm_year++;
360 } while (!is_leap_year(alarm->time.tm_year + 1900) 351 } while (!is_leap_year(alarm->time.tm_year + 1900) &&
361 && rtc_valid_tm(&alarm->time) != 0); 352 rtc_valid_tm(&alarm->time) != 0);
362 break; 353 break;
363 354
364 default: 355 default:
@@ -369,7 +360,8 @@ int __rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
369 360
370done: 361done:
371 if (err) 362 if (err)
372 dev_warn(&rtc->dev, "invalid alarm value: %ptR\n", &alarm->time); 363 dev_warn(&rtc->dev, "invalid alarm value: %ptR\n",
364 &alarm->time);
373 365
374 return err; 366 return err;
375} 367}
@@ -381,11 +373,11 @@ int rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
381 err = mutex_lock_interruptible(&rtc->ops_lock); 373 err = mutex_lock_interruptible(&rtc->ops_lock);
382 if (err) 374 if (err)
383 return err; 375 return err;
384 if (rtc->ops == NULL) 376 if (!rtc->ops) {
385 err = -ENODEV; 377 err = -ENODEV;
386 else if (!rtc->ops->read_alarm) 378 } else if (!rtc->ops->read_alarm) {
387 err = -EINVAL; 379 err = -EINVAL;
388 else { 380 } else {
389 memset(alarm, 0, sizeof(struct rtc_wkalrm)); 381 memset(alarm, 0, sizeof(struct rtc_wkalrm));
390 alarm->enabled = rtc->aie_timer.enabled; 382 alarm->enabled = rtc->aie_timer.enabled;
391 alarm->time = rtc_ktime_to_tm(rtc->aie_timer.node.expires); 383 alarm->time = rtc_ktime_to_tm(rtc->aie_timer.node.expires);
@@ -494,7 +486,6 @@ int rtc_initialize_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
494 /* Alarm has to be enabled & in the future for us to enqueue it */ 486 /* Alarm has to be enabled & in the future for us to enqueue it */
495 if (alarm->enabled && (rtc_tm_to_ktime(now) < 487 if (alarm->enabled && (rtc_tm_to_ktime(now) <
496 rtc->aie_timer.node.expires)) { 488 rtc->aie_timer.node.expires)) {
497
498 rtc->aie_timer.enabled = 1; 489 rtc->aie_timer.enabled = 1;
499 timerqueue_add(&rtc->timerqueue, &rtc->aie_timer.node); 490 timerqueue_add(&rtc->timerqueue, &rtc->aie_timer.node);
500 trace_rtc_timer_enqueue(&rtc->aie_timer); 491 trace_rtc_timer_enqueue(&rtc->aie_timer);
@@ -506,7 +497,9 @@ EXPORT_SYMBOL_GPL(rtc_initialize_alarm);
506 497
507int rtc_alarm_irq_enable(struct rtc_device *rtc, unsigned int enabled) 498int rtc_alarm_irq_enable(struct rtc_device *rtc, unsigned int enabled)
508{ 499{
509 int err = mutex_lock_interruptible(&rtc->ops_lock); 500 int err;
501
502 err = mutex_lock_interruptible(&rtc->ops_lock);
510 if (err) 503 if (err)
511 return err; 504 return err;
512 505
@@ -535,7 +528,9 @@ EXPORT_SYMBOL_GPL(rtc_alarm_irq_enable);
535 528
536int rtc_update_irq_enable(struct rtc_device *rtc, unsigned int enabled) 529int rtc_update_irq_enable(struct rtc_device *rtc, unsigned int enabled)
537{ 530{
538 int err = mutex_lock_interruptible(&rtc->ops_lock); 531 int err;
532
533 err = mutex_lock_interruptible(&rtc->ops_lock);
539 if (err) 534 if (err)
540 return err; 535 return err;
541 536
@@ -564,27 +559,25 @@ int rtc_update_irq_enable(struct rtc_device *rtc, unsigned int enabled)
564 rtc->uie_rtctimer.node.expires = ktime_add(now, onesec); 559 rtc->uie_rtctimer.node.expires = ktime_add(now, onesec);
565 rtc->uie_rtctimer.period = ktime_set(1, 0); 560 rtc->uie_rtctimer.period = ktime_set(1, 0);
566 err = rtc_timer_enqueue(rtc, &rtc->uie_rtctimer); 561 err = rtc_timer_enqueue(rtc, &rtc->uie_rtctimer);
567 } else 562 } else {
568 rtc_timer_remove(rtc, &rtc->uie_rtctimer); 563 rtc_timer_remove(rtc, &rtc->uie_rtctimer);
564 }
569 565
570out: 566out:
571 mutex_unlock(&rtc->ops_lock); 567 mutex_unlock(&rtc->ops_lock);
572#ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL 568#ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
573 /* 569 /*
574 * Enable emulation if the driver did not provide 570 * Enable emulation if the driver returned -EINVAL to signal that it has
575 * the update_irq_enable function pointer or if returned 571 * been configured without interrupts or they are not available at the
576 * -EINVAL to signal that it has been configured without 572 * moment.
577 * interrupts or that are not available at the moment.
578 */ 573 */
579 if (err == -EINVAL) 574 if (err == -EINVAL)
580 err = rtc_dev_update_irq_enable_emul(rtc, enabled); 575 err = rtc_dev_update_irq_enable_emul(rtc, enabled);
581#endif 576#endif
582 return err; 577 return err;
583
584} 578}
585EXPORT_SYMBOL_GPL(rtc_update_irq_enable); 579EXPORT_SYMBOL_GPL(rtc_update_irq_enable);
586 580
587
588/** 581/**
589 * rtc_handle_legacy_irq - AIE, UIE and PIE event hook 582 * rtc_handle_legacy_irq - AIE, UIE and PIE event hook
590 * @rtc: pointer to the rtc device 583 * @rtc: pointer to the rtc device
@@ -599,14 +592,13 @@ void rtc_handle_legacy_irq(struct rtc_device *rtc, int num, int mode)
599 592
600 /* mark one irq of the appropriate mode */ 593 /* mark one irq of the appropriate mode */
601 spin_lock_irqsave(&rtc->irq_lock, flags); 594 spin_lock_irqsave(&rtc->irq_lock, flags);
602 rtc->irq_data = (rtc->irq_data + (num << 8)) | (RTC_IRQF|mode); 595 rtc->irq_data = (rtc->irq_data + (num << 8)) | (RTC_IRQF | mode);
603 spin_unlock_irqrestore(&rtc->irq_lock, flags); 596 spin_unlock_irqrestore(&rtc->irq_lock, flags);
604 597
605 wake_up_interruptible(&rtc->irq_queue); 598 wake_up_interruptible(&rtc->irq_queue);
606 kill_fasync(&rtc->async_queue, SIGIO, POLL_IN); 599 kill_fasync(&rtc->async_queue, SIGIO, POLL_IN);
607} 600}
608 601
609
610/** 602/**
611 * rtc_aie_update_irq - AIE mode rtctimer hook 603 * rtc_aie_update_irq - AIE mode rtctimer hook
612 * @rtc: pointer to the rtc_device 604 * @rtc: pointer to the rtc_device
@@ -618,7 +610,6 @@ void rtc_aie_update_irq(struct rtc_device *rtc)
618 rtc_handle_legacy_irq(rtc, 1, RTC_AF); 610 rtc_handle_legacy_irq(rtc, 1, RTC_AF);
619} 611}
620 612
621
622/** 613/**
623 * rtc_uie_update_irq - UIE mode rtctimer hook 614 * rtc_uie_update_irq - UIE mode rtctimer hook
624 * @rtc: pointer to the rtc_device 615 * @rtc: pointer to the rtc_device
@@ -630,7 +621,6 @@ void rtc_uie_update_irq(struct rtc_device *rtc)
630 rtc_handle_legacy_irq(rtc, 1, RTC_UF); 621 rtc_handle_legacy_irq(rtc, 1, RTC_UF);
631} 622}
632 623
633
634/** 624/**
635 * rtc_pie_update_irq - PIE mode hrtimer hook 625 * rtc_pie_update_irq - PIE mode hrtimer hook
636 * @timer: pointer to the pie mode hrtimer 626 * @timer: pointer to the pie mode hrtimer
@@ -644,6 +634,7 @@ enum hrtimer_restart rtc_pie_update_irq(struct hrtimer *timer)
644 struct rtc_device *rtc; 634 struct rtc_device *rtc;
645 ktime_t period; 635 ktime_t period;
646 int count; 636 int count;
637
647 rtc = container_of(timer, struct rtc_device, pie_timer); 638 rtc = container_of(timer, struct rtc_device, pie_timer);
648 639
649 period = NSEC_PER_SEC / rtc->irq_freq; 640 period = NSEC_PER_SEC / rtc->irq_freq;
@@ -662,7 +653,7 @@ enum hrtimer_restart rtc_pie_update_irq(struct hrtimer *timer)
662 * Context: any 653 * Context: any
663 */ 654 */
664void rtc_update_irq(struct rtc_device *rtc, 655void rtc_update_irq(struct rtc_device *rtc,
665 unsigned long num, unsigned long events) 656 unsigned long num, unsigned long events)
666{ 657{
667 if (IS_ERR_OR_NULL(rtc)) 658 if (IS_ERR_OR_NULL(rtc))
668 return; 659 return;
@@ -811,6 +802,7 @@ static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer)
811 if (!next || ktime_before(timer->node.expires, next->expires)) { 802 if (!next || ktime_before(timer->node.expires, next->expires)) {
812 struct rtc_wkalrm alarm; 803 struct rtc_wkalrm alarm;
813 int err; 804 int err;
805
814 alarm.time = rtc_ktime_to_tm(timer->node.expires); 806 alarm.time = rtc_ktime_to_tm(timer->node.expires);
815 alarm.enabled = 1; 807 alarm.enabled = 1;
816 err = __rtc_set_alarm(rtc, &alarm); 808 err = __rtc_set_alarm(rtc, &alarm);
@@ -851,12 +843,14 @@ static void rtc_alarm_disable(struct rtc_device *rtc)
851static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer) 843static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer)
852{ 844{
853 struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue); 845 struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue);
846
854 timerqueue_del(&rtc->timerqueue, &timer->node); 847 timerqueue_del(&rtc->timerqueue, &timer->node);
855 trace_rtc_timer_dequeue(timer); 848 trace_rtc_timer_dequeue(timer);
856 timer->enabled = 0; 849 timer->enabled = 0;
857 if (next == &timer->node) { 850 if (next == &timer->node) {
858 struct rtc_wkalrm alarm; 851 struct rtc_wkalrm alarm;
859 int err; 852 int err;
853
860 next = timerqueue_getnext(&rtc->timerqueue); 854 next = timerqueue_getnext(&rtc->timerqueue);
861 if (!next) { 855 if (!next) {
862 rtc_alarm_disable(rtc); 856 rtc_alarm_disable(rtc);
@@ -929,9 +923,9 @@ again:
929 alarm.enabled = 1; 923 alarm.enabled = 1;
930reprogram: 924reprogram:
931 err = __rtc_set_alarm(rtc, &alarm); 925 err = __rtc_set_alarm(rtc, &alarm);
932 if (err == -ETIME) 926 if (err == -ETIME) {
933 goto again; 927 goto again;
934 else if (err) { 928 } else if (err) {
935 if (retry-- > 0) 929 if (retry-- > 0)
936 goto reprogram; 930 goto reprogram;
937 931
@@ -942,14 +936,14 @@ reprogram:
942 dev_err(&rtc->dev, "__rtc_set_alarm: err=%d\n", err); 936 dev_err(&rtc->dev, "__rtc_set_alarm: err=%d\n", err);
943 goto again; 937 goto again;
944 } 938 }
945 } else 939 } else {
946 rtc_alarm_disable(rtc); 940 rtc_alarm_disable(rtc);
941 }
947 942
948 pm_relax(rtc->dev.parent); 943 pm_relax(rtc->dev.parent);
949 mutex_unlock(&rtc->ops_lock); 944 mutex_unlock(&rtc->ops_lock);
950} 945}
951 946
952
953/* rtc_timer_init - Initializes an rtc_timer 947/* rtc_timer_init - Initializes an rtc_timer
954 * @timer: timer to be intiialized 948 * @timer: timer to be intiialized
955 * @f: function pointer to be called when timer fires 949 * @f: function pointer to be called when timer fires
@@ -975,9 +969,10 @@ void rtc_timer_init(struct rtc_timer *timer, void (*f)(struct rtc_device *r),
975 * Kernel interface to set an rtc_timer 969 * Kernel interface to set an rtc_timer
976 */ 970 */
977int rtc_timer_start(struct rtc_device *rtc, struct rtc_timer *timer, 971int rtc_timer_start(struct rtc_device *rtc, struct rtc_timer *timer,
978 ktime_t expires, ktime_t period) 972 ktime_t expires, ktime_t period)
979{ 973{
980 int ret = 0; 974 int ret = 0;
975
981 mutex_lock(&rtc->ops_lock); 976 mutex_lock(&rtc->ops_lock);
982 if (timer->enabled) 977 if (timer->enabled)
983 rtc_timer_remove(rtc, timer); 978 rtc_timer_remove(rtc, timer);