blob: be88a0d6a5d59b23480715da221514651838a78f [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001/*
2 * RTC subsystem, interface functions
3 *
4 * Copyright (C) 2005 Tower Technologies
5 * Author: Alessandro Zummo <a.zummo@towertech.it>
6 *
7 * 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*/
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 * Since the reading time values from RTC device are always in the RTC
37 * original valid range, but we need to skip the overlapped region
38 * between expanded range and original range, which is no need to add
39 * the offset.
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 * If the setting time values are in the valid range of RTC hardware
60 * device, then no need to subtract the offset when setting time to RTC
61 * device. Otherwise we need to subtract the offset to make the time
62 * values are valid for RTC hardware device.
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 /* A timer might have just expired */
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 /* The lower level RTC driver may return -1 in some fields,
230 * creating invalid alarm->time values, for reasons like:
231 *
232 * - The hardware may not be capable of filling them in;
233 * many alarms match only on time-of-day fields, not
234 * day/month/year calendar data.
235 *
236 * - Some hardware uses illegal values as "wildcard" match
237 * values, which non-Linux firmware (like a BIOS) may try
238 * to set up as e.g. "alarm 15 minutes after each hour".
239 * Linux uses only oneshot alarms.
240 *
241 * When we see that here, we deal with it by using values from
242 * a current RTC timestamp for any missing (-1) values. The
243 * RTC driver prevents "periodic alarm" modes.
244 *
245 * But this can be racey, because some fields of the RTC timestamp
246 * may have wrapped in the interval since we read the RTC alarm,
247 * which would lead to us inserting inconsistent values in place
248 * of the -1 fields.
249 *
250 * Reading the alarm and timestamp in the reverse sequence
251 * would have the same race condition, and not solve the issue.
252 *
253 * So, we must first read the RTC timestamp,
254 * then read the RTC alarm value,
255 * and then read a second RTC timestamp.
256 *
257 * If any fields of the second timestamp have changed
258 * when compared with the first timestamp, then we know
259 * our timestamp may be inconsistent with that used by
260 * the low-level rtc_read_alarm_internal() function.
261 *
262 * So, when the two timestamps disagree, we just loop and do
263 * the process again to get a fully consistent set of values.
264 *
265 * This could all instead be done in the lower level driver,
266 * but since more than one lower level RTC implementation needs it,
267 * then it's probably best best to do it here instead of there..
268 */
269
270 /* Get the "before" timestamp */
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 /* get the RTC alarm values, which may be incomplete */
280 err = rtc_read_alarm_internal(rtc, alarm);
281 if (err)
282 return err;
283
284 /* full-function RTCs won't have such missing fields */
285 if (rtc_valid_tm(&alarm->time) == 0) {
286 rtc_add_offset(rtc, &alarm->time);
287 return 0;
288 }
289
290 /* get the "after" timestamp, to detect wrapped fields */
291 err = rtc_read_time(rtc, &now);
292 if (err < 0)
293 return err;
294
295 /* 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);
300
301 /* Fill in the missing alarm fields using the timestamp; we
302 * know there's at least one since alarm->time is invalid.
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 /* For simplicity, only support date rollover for now */
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 /* Can't proceed if alarm is still invalid after replacing
328 * missing fields.
329 */
330 err = rtc_valid_tm(&alarm->time);
331 if (err)
332 goto done;
333
334 /* with luck, no rollover is needed */
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 /* 24 hour rollover ... if it's now 10am Monday, an alarm that
343 * that will trigger at 5am will do so at 5am Tuesday, which
344 * could also be in the next month or year. This is a common
345 * case, especially for PCs.
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 /* Month rollover ... if it's the 31th, an alarm on the 3rd will
354 * be next month. An alarm matching on the 30th, 29th, or 28th
355 * may end up in the month after that! Many newer PCs support
356 * this type of alarm.
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 /* Year rollover ... easy except for leap years! */
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 /* Make sure we're not setting alarms in the past */
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 * XXX - We just checked to make sure the alarm time is not
442 * in the past, but there is still a race window where if
443 * the is alarm set for the next second and the second ticks
444 * over right here, before we set the alarm.
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;
xj79176bd2022-05-26 14:45:57 +0800485 rtc->aie_timer.enabled = alarm->enabled;
xjb04a4022021-11-25 15:01:52 +0800486 if (alarm->enabled)
487 err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
488
489 mutex_unlock(&rtc->ops_lock);
490
491 return err;
492}
493EXPORT_SYMBOL_GPL(rtc_set_alarm);
494
495/* Called once per device from rtc_device_register */
496int rtc_initialize_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
497{
498 int err;
499 struct rtc_time now;
500
501 err = rtc_valid_tm(&alarm->time);
502 if (err != 0)
503 return err;
504
505 err = rtc_read_time(rtc, &now);
506 if (err)
507 return err;
508
509 err = mutex_lock_interruptible(&rtc->ops_lock);
510 if (err)
511 return err;
512
513 rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time);
514 rtc->aie_timer.period = 0;
515
516 /* Alarm has to be enabled & in the future for us to enqueue it */
517 if (alarm->enabled && (rtc_tm_to_ktime(now) <
518 rtc->aie_timer.node.expires)) {
519
520 rtc->aie_timer.enabled = 1;
521 timerqueue_add(&rtc->timerqueue, &rtc->aie_timer.node);
522 trace_rtc_timer_enqueue(&rtc->aie_timer);
523 }
524 mutex_unlock(&rtc->ops_lock);
525 return err;
526}
527EXPORT_SYMBOL_GPL(rtc_initialize_alarm);
528
529int rtc_alarm_irq_enable(struct rtc_device *rtc, unsigned int enabled)
530{
531 int err = mutex_lock_interruptible(&rtc->ops_lock);
532 if (err)
533 return err;
534
535 if (rtc->aie_timer.enabled != enabled) {
536 if (enabled)
537 err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
538 else
539 rtc_timer_remove(rtc, &rtc->aie_timer);
540 }
541
542 if (err)
543 /* nothing */;
544 else if (!rtc->ops)
545 err = -ENODEV;
546 else if (!rtc->ops->alarm_irq_enable)
547 err = -EINVAL;
548 else
549 err = rtc->ops->alarm_irq_enable(rtc->dev.parent, enabled);
550
551 mutex_unlock(&rtc->ops_lock);
552
553 trace_rtc_alarm_irq_enable(enabled, err);
554 return err;
555}
556EXPORT_SYMBOL_GPL(rtc_alarm_irq_enable);
557
558int rtc_update_irq_enable(struct rtc_device *rtc, unsigned int enabled)
559{
560 int err = mutex_lock_interruptible(&rtc->ops_lock);
561 if (err)
562 return err;
563
564#ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
565 if (enabled == 0 && rtc->uie_irq_active) {
566 mutex_unlock(&rtc->ops_lock);
567 return rtc_dev_update_irq_enable_emul(rtc, 0);
568 }
569#endif
570 /* make sure we're changing state */
571 if (rtc->uie_rtctimer.enabled == enabled)
572 goto out;
573
574 if (rtc->uie_unsupported) {
575 err = -EINVAL;
576 goto out;
577 }
578
579 if (enabled) {
580 struct rtc_time tm;
581 ktime_t now, onesec;
582
583 __rtc_read_time(rtc, &tm);
584 onesec = ktime_set(1, 0);
585 now = rtc_tm_to_ktime(tm);
586 rtc->uie_rtctimer.node.expires = ktime_add(now, onesec);
587 rtc->uie_rtctimer.period = ktime_set(1, 0);
588 err = rtc_timer_enqueue(rtc, &rtc->uie_rtctimer);
589 } else
590 rtc_timer_remove(rtc, &rtc->uie_rtctimer);
591
592out:
593 mutex_unlock(&rtc->ops_lock);
594#ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
595 /*
596 * Enable emulation if the driver did not provide
597 * the update_irq_enable function pointer or if returned
598 * -EINVAL to signal that it has been configured without
599 * interrupts or that are not available at the moment.
600 */
601 if (err == -EINVAL)
602 err = rtc_dev_update_irq_enable_emul(rtc, enabled);
603#endif
604 return err;
605
606}
607EXPORT_SYMBOL_GPL(rtc_update_irq_enable);
608
609
610/**
611 * rtc_handle_legacy_irq - AIE, UIE and PIE event hook
612 * @rtc: pointer to the rtc device
613 *
614 * This function is called when an AIE, UIE or PIE mode interrupt
615 * has occurred (or been emulated).
616 *
617 * Triggers the registered irq_task function callback.
618 */
619void rtc_handle_legacy_irq(struct rtc_device *rtc, int num, int mode)
620{
621 unsigned long flags;
622
623 /* mark one irq of the appropriate mode */
624 spin_lock_irqsave(&rtc->irq_lock, flags);
625 rtc->irq_data = (rtc->irq_data + (num << 8)) | (RTC_IRQF|mode);
626 spin_unlock_irqrestore(&rtc->irq_lock, flags);
627
628 wake_up_interruptible(&rtc->irq_queue);
629 kill_fasync(&rtc->async_queue, SIGIO, POLL_IN);
630}
631
632
633/**
634 * rtc_aie_update_irq - AIE mode rtctimer hook
635 * @private: pointer to the rtc_device
636 *
637 * This functions is called when the aie_timer expires.
638 */
639void rtc_aie_update_irq(void *private)
640{
641 struct rtc_device *rtc = (struct rtc_device *)private;
642 rtc_handle_legacy_irq(rtc, 1, RTC_AF);
643}
644
645
646/**
647 * rtc_uie_update_irq - UIE mode rtctimer hook
648 * @private: pointer to the rtc_device
649 *
650 * This functions is called when the uie_timer expires.
651 */
652void rtc_uie_update_irq(void *private)
653{
654 struct rtc_device *rtc = (struct rtc_device *)private;
655 rtc_handle_legacy_irq(rtc, 1, RTC_UF);
656}
657
658
659/**
660 * rtc_pie_update_irq - PIE mode hrtimer hook
661 * @timer: pointer to the pie mode hrtimer
662 *
663 * This function is used to emulate PIE mode interrupts
664 * using an hrtimer. This function is called when the periodic
665 * hrtimer expires.
666 */
667enum hrtimer_restart rtc_pie_update_irq(struct hrtimer *timer)
668{
669 struct rtc_device *rtc;
670 ktime_t period;
671 int count;
672 rtc = container_of(timer, struct rtc_device, pie_timer);
673
674 period = NSEC_PER_SEC / rtc->irq_freq;
675 count = hrtimer_forward_now(timer, period);
676
677 rtc_handle_legacy_irq(rtc, count, RTC_PF);
678
679 return HRTIMER_RESTART;
680}
681
682/**
683 * rtc_update_irq - Triggered when a RTC interrupt occurs.
684 * @rtc: the rtc device
685 * @num: how many irqs are being reported (usually one)
686 * @events: mask of RTC_IRQF with one or more of RTC_PF, RTC_AF, RTC_UF
687 * Context: any
688 */
689void rtc_update_irq(struct rtc_device *rtc,
690 unsigned long num, unsigned long events)
691{
692 if (IS_ERR_OR_NULL(rtc))
693 return;
694
695 pm_stay_awake(rtc->dev.parent);
696 schedule_work(&rtc->irqwork);
697}
698EXPORT_SYMBOL_GPL(rtc_update_irq);
699
700static int __rtc_match(struct device *dev, const void *data)
701{
702 const char *name = data;
703
704 if (strcmp(dev_name(dev), name) == 0)
705 return 1;
706 return 0;
707}
708
709struct rtc_device *rtc_class_open(const char *name)
710{
711 struct device *dev;
712 struct rtc_device *rtc = NULL;
713
714 dev = class_find_device(rtc_class, NULL, name, __rtc_match);
715 if (dev)
716 rtc = to_rtc_device(dev);
717
718 if (rtc) {
719 if (!try_module_get(rtc->owner)) {
720 put_device(dev);
721 rtc = NULL;
722 }
723 }
724
725 return rtc;
726}
727EXPORT_SYMBOL_GPL(rtc_class_open);
728
729void rtc_class_close(struct rtc_device *rtc)
730{
731 module_put(rtc->owner);
732 put_device(&rtc->dev);
733}
734EXPORT_SYMBOL_GPL(rtc_class_close);
735
736static int rtc_update_hrtimer(struct rtc_device *rtc, int enabled)
737{
738 /*
739 * We always cancel the timer here first, because otherwise
740 * we could run into BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
741 * when we manage to start the timer before the callback
742 * returns HRTIMER_RESTART.
743 *
744 * We cannot use hrtimer_cancel() here as a running callback
745 * could be blocked on rtc->irq_task_lock and hrtimer_cancel()
746 * would spin forever.
747 */
748 if (hrtimer_try_to_cancel(&rtc->pie_timer) < 0)
749 return -1;
750
751 if (enabled) {
752 ktime_t period = NSEC_PER_SEC / rtc->irq_freq;
753
754 hrtimer_start(&rtc->pie_timer, period, HRTIMER_MODE_REL);
755 }
756 return 0;
757}
758
759/**
760 * rtc_irq_set_state - enable/disable 2^N Hz periodic IRQs
761 * @rtc: the rtc device
762 * @task: currently registered with rtc_irq_register()
763 * @enabled: true to enable periodic IRQs
764 * Context: any
765 *
766 * Note that rtc_irq_set_freq() should previously have been used to
767 * specify the desired frequency of periodic IRQ.
768 */
769int rtc_irq_set_state(struct rtc_device *rtc, int enabled)
770{
771 int err = 0;
772
773 while (rtc_update_hrtimer(rtc, enabled) < 0)
774 cpu_relax();
775
776 rtc->pie_enabled = enabled;
777
778 trace_rtc_irq_set_state(enabled, err);
779 return err;
780}
781
782/**
783 * rtc_irq_set_freq - set 2^N Hz periodic IRQ frequency for IRQ
784 * @rtc: the rtc device
785 * @task: currently registered with rtc_irq_register()
786 * @freq: positive frequency
787 * Context: any
788 *
789 * Note that rtc_irq_set_state() is used to enable or disable the
790 * periodic IRQs.
791 */
792int rtc_irq_set_freq(struct rtc_device *rtc, int freq)
793{
794 int err = 0;
795
796 if (freq <= 0 || freq > RTC_MAX_FREQ)
797 return -EINVAL;
798
799 rtc->irq_freq = freq;
800 while (rtc->pie_enabled && rtc_update_hrtimer(rtc, 1) < 0)
801 cpu_relax();
802
803 trace_rtc_irq_set_freq(freq, err);
804 return err;
805}
806
807/**
808 * rtc_timer_enqueue - Adds a rtc_timer to the rtc_device timerqueue
809 * @rtc rtc device
810 * @timer timer being added.
811 *
812 * Enqueues a timer onto the rtc devices timerqueue and sets
813 * the next alarm event appropriately.
814 *
815 * Sets the enabled bit on the added timer.
816 *
817 * Must hold ops_lock for proper serialization of timerqueue
818 */
819static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer)
820{
821 struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue);
822 struct rtc_time tm;
823 ktime_t now;
824
lh0d3f4db2022-09-17 00:16:39 -0700825 if (timer->enabled == 0)
xj79176bd2022-05-26 14:45:57 +0800826 timer->enabled = 1;
xjb04a4022021-11-25 15:01:52 +0800827 __rtc_read_time(rtc, &tm);
828 now = rtc_tm_to_ktime(tm);
829
830 /* Skip over expired timers */
831 while (next) {
832 if (next->expires >= now)
833 break;
834 next = timerqueue_iterate_next(next);
835 }
836
837 timerqueue_add(&rtc->timerqueue, &timer->node);
838 trace_rtc_timer_enqueue(timer);
839 if (!next || ktime_before(timer->node.expires, next->expires)) {
840 struct rtc_wkalrm alarm;
841 int err;
842 alarm.time = rtc_ktime_to_tm(timer->node.expires);
xj79176bd2022-05-26 14:45:57 +0800843 alarm.enabled = timer->enabled;
xjb04a4022021-11-25 15:01:52 +0800844 err = __rtc_set_alarm(rtc, &alarm);
845 if (err == -ETIME) {
846 pm_stay_awake(rtc->dev.parent);
847 schedule_work(&rtc->irqwork);
848 } else if (err) {
849 timerqueue_del(&rtc->timerqueue, &timer->node);
850 trace_rtc_timer_dequeue(timer);
851 timer->enabled = 0;
852 return err;
853 }
854 }
855 return 0;
856}
857
858static void rtc_alarm_disable(struct rtc_device *rtc)
859{
860 if (!rtc->ops || !rtc->ops->alarm_irq_enable)
861 return;
862
863 rtc->ops->alarm_irq_enable(rtc->dev.parent, false);
864 trace_rtc_alarm_irq_enable(0, 0);
865}
866
867/**
868 * rtc_timer_remove - Removes a rtc_timer from the rtc_device timerqueue
869 * @rtc rtc device
870 * @timer timer being removed.
871 *
872 * Removes a timer onto the rtc devices timerqueue and sets
873 * the next alarm event appropriately.
874 *
875 * Clears the enabled bit on the removed timer.
876 *
877 * Must hold ops_lock for proper serialization of timerqueue
878 */
879static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer)
880{
881 struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue);
882 timerqueue_del(&rtc->timerqueue, &timer->node);
883 trace_rtc_timer_dequeue(timer);
884 timer->enabled = 0;
885 if (next == &timer->node) {
886 struct rtc_wkalrm alarm;
887 int err;
888 next = timerqueue_getnext(&rtc->timerqueue);
889 if (!next) {
890 rtc_alarm_disable(rtc);
891 return;
892 }
893 alarm.time = rtc_ktime_to_tm(next->expires);
894 alarm.enabled = 1;
895 err = __rtc_set_alarm(rtc, &alarm);
896 if (err == -ETIME) {
897 pm_stay_awake(rtc->dev.parent);
898 schedule_work(&rtc->irqwork);
899 }
900 }
901}
902
903/**
904 * rtc_timer_do_work - Expires rtc timers
905 * @rtc rtc device
906 * @timer timer being removed.
907 *
908 * Expires rtc timers. Reprograms next alarm event if needed.
909 * Called via worktask.
910 *
911 * Serializes access to timerqueue via ops_lock mutex
912 */
913void rtc_timer_do_work(struct work_struct *work)
914{
915 struct rtc_timer *timer;
916 struct timerqueue_node *next;
917 ktime_t now;
918 struct rtc_time tm;
919
920 struct rtc_device *rtc =
921 container_of(work, struct rtc_device, irqwork);
922
923 mutex_lock(&rtc->ops_lock);
924again:
925 __rtc_read_time(rtc, &tm);
926 now = rtc_tm_to_ktime(tm);
927 while ((next = timerqueue_getnext(&rtc->timerqueue))) {
928 if (next->expires > now)
929 break;
930
931 /* expire timer */
932 timer = container_of(next, struct rtc_timer, node);
933 timerqueue_del(&rtc->timerqueue, &timer->node);
934 trace_rtc_timer_dequeue(timer);
935 timer->enabled = 0;
936 if (timer->func)
937 timer->func(timer->private_data);
938
939 trace_rtc_timer_fired(timer);
940 /* Re-add/fwd periodic timers */
941 if (ktime_to_ns(timer->period)) {
942 timer->node.expires = ktime_add(timer->node.expires,
943 timer->period);
944 timer->enabled = 1;
945 timerqueue_add(&rtc->timerqueue, &timer->node);
946 trace_rtc_timer_enqueue(timer);
947 }
948 }
949
950 /* Set next alarm */
951 if (next) {
952 struct rtc_wkalrm alarm;
953 int err;
954 int retry = 3;
955
956 alarm.time = rtc_ktime_to_tm(next->expires);
957 alarm.enabled = 1;
958reprogram:
959 err = __rtc_set_alarm(rtc, &alarm);
960 if (err == -ETIME)
961 goto again;
962 else if (err) {
963 if (retry-- > 0)
964 goto reprogram;
965
966 timer = container_of(next, struct rtc_timer, node);
967 timerqueue_del(&rtc->timerqueue, &timer->node);
968 trace_rtc_timer_dequeue(timer);
969 timer->enabled = 0;
970 dev_err(&rtc->dev, "__rtc_set_alarm: err=%d\n", err);
971 goto again;
972 }
973 } else
974 rtc_alarm_disable(rtc);
975
976 pm_relax(rtc->dev.parent);
977 mutex_unlock(&rtc->ops_lock);
978}
979
980
981/* rtc_timer_init - Initializes an rtc_timer
982 * @timer: timer to be intiialized
983 * @f: function pointer to be called when timer fires
984 * @data: private data passed to function pointer
985 *
986 * Kernel interface to initializing an rtc_timer.
987 */
988void rtc_timer_init(struct rtc_timer *timer, void (*f)(void *p), void *data)
989{
990 timerqueue_init(&timer->node);
991 timer->enabled = 0;
992 timer->func = f;
993 timer->private_data = data;
994}
995
996/* rtc_timer_start - Sets an rtc_timer to fire in the future
997 * @ rtc: rtc device to be used
998 * @ timer: timer being set
999 * @ expires: time at which to expire the timer
1000 * @ period: period that the timer will recur
1001 *
1002 * Kernel interface to set an rtc_timer
1003 */
1004int rtc_timer_start(struct rtc_device *rtc, struct rtc_timer *timer,
1005 ktime_t expires, ktime_t period)
1006{
1007 int ret = 0;
1008 mutex_lock(&rtc->ops_lock);
1009 if (timer->enabled)
1010 rtc_timer_remove(rtc, timer);
1011
1012 timer->node.expires = expires;
1013 timer->period = period;
1014
1015 ret = rtc_timer_enqueue(rtc, timer);
1016
1017 mutex_unlock(&rtc->ops_lock);
1018 return ret;
1019}
1020
1021/* rtc_timer_cancel - Stops an rtc_timer
1022 * @ rtc: rtc device to be used
1023 * @ timer: timer being set
1024 *
1025 * Kernel interface to cancel an rtc_timer
1026 */
1027void rtc_timer_cancel(struct rtc_device *rtc, struct rtc_timer *timer)
1028{
1029 mutex_lock(&rtc->ops_lock);
1030 if (timer->enabled)
1031 rtc_timer_remove(rtc, timer);
1032 mutex_unlock(&rtc->ops_lock);
1033}
1034
1035/**
1036 * rtc_read_offset - Read the amount of rtc offset in parts per billion
1037 * @ rtc: rtc device to be used
1038 * @ offset: the offset in parts per billion
1039 *
1040 * see below for details.
1041 *
1042 * Kernel interface to read rtc clock offset
1043 * Returns 0 on success, or a negative number on error.
1044 * If read_offset() is not implemented for the rtc, return -EINVAL
1045 */
1046int rtc_read_offset(struct rtc_device *rtc, long *offset)
1047{
1048 int ret;
1049
1050 if (!rtc->ops)
1051 return -ENODEV;
1052
1053 if (!rtc->ops->read_offset)
1054 return -EINVAL;
1055
1056 mutex_lock(&rtc->ops_lock);
1057 ret = rtc->ops->read_offset(rtc->dev.parent, offset);
1058 mutex_unlock(&rtc->ops_lock);
1059
1060 trace_rtc_read_offset(*offset, ret);
1061 return ret;
1062}
1063
1064/**
1065 * rtc_set_offset - Adjusts the duration of the average second
1066 * @ rtc: rtc device to be used
1067 * @ offset: the offset in parts per billion
1068 *
1069 * Some rtc's allow an adjustment to the average duration of a second
1070 * to compensate for differences in the actual clock rate due to temperature,
1071 * the crystal, capacitor, etc.
1072 *
1073 * The adjustment applied is as follows:
1074 * t = t0 * (1 + offset * 1e-9)
1075 * where t0 is the measured length of 1 RTC second with offset = 0
1076 *
1077 * Kernel interface to adjust an rtc clock offset.
1078 * Return 0 on success, or a negative number on error.
1079 * If the rtc offset is not setable (or not implemented), return -EINVAL
1080 */
1081int rtc_set_offset(struct rtc_device *rtc, long offset)
1082{
1083 int ret;
1084
1085 if (!rtc->ops)
1086 return -ENODEV;
1087
1088 if (!rtc->ops->set_offset)
1089 return -EINVAL;
1090
1091 mutex_lock(&rtc->ops_lock);
1092 ret = rtc->ops->set_offset(rtc->dev.parent, offset);
1093 mutex_unlock(&rtc->ops_lock);
1094
1095 trace_rtc_set_offset(offset, ret);
1096 return ret;
1097}