]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/rtc/interface.c
rtc: pcf8563: Fix interrupt trigger method
[mirror_ubuntu-bionic-kernel.git] / drivers / rtc / interface.c
1 /*
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 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);
22
23 static int __rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm)
24 {
25 int err;
26 if (!rtc->ops)
27 err = -ENODEV;
28 else if (!rtc->ops->read_time)
29 err = -EINVAL;
30 else {
31 memset(tm, 0, sizeof(struct rtc_time));
32 err = rtc->ops->read_time(rtc->dev.parent, tm);
33 if (err < 0) {
34 dev_dbg(&rtc->dev, "read_time: fail to read: %d\n",
35 err);
36 return err;
37 }
38
39 err = rtc_valid_tm(tm);
40 if (err < 0)
41 dev_dbg(&rtc->dev, "read_time: rtc_time isn't valid\n");
42 }
43 return err;
44 }
45
46 int rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm)
47 {
48 int err;
49
50 err = mutex_lock_interruptible(&rtc->ops_lock);
51 if (err)
52 return err;
53
54 err = __rtc_read_time(rtc, tm);
55 mutex_unlock(&rtc->ops_lock);
56 return err;
57 }
58 EXPORT_SYMBOL_GPL(rtc_read_time);
59
60 int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm)
61 {
62 int err, uie;
63
64 err = rtc_valid_tm(tm);
65 if (err != 0)
66 return err;
67
68 #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
69 uie = rtc->uie_rtctimer.enabled || rtc->uie_irq_active;
70 #else
71 uie = rtc->uie_rtctimer.enabled;
72 #endif
73 if (uie) {
74 err = rtc_update_irq_enable(rtc, 0);
75 if (err)
76 return err;
77 }
78
79 err = mutex_lock_interruptible(&rtc->ops_lock);
80 if (err)
81 return err;
82
83 if (!rtc->ops)
84 err = -ENODEV;
85 else if (rtc->ops->set_time)
86 err = rtc->ops->set_time(rtc->dev.parent, tm);
87 else if (rtc->ops->set_mmss64) {
88 time64_t secs64 = rtc_tm_to_time64(tm);
89
90 err = rtc->ops->set_mmss64(rtc->dev.parent, secs64);
91 } else if (rtc->ops->set_mmss) {
92 time64_t secs64 = rtc_tm_to_time64(tm);
93 err = rtc->ops->set_mmss(rtc->dev.parent, secs64);
94 } else
95 err = -EINVAL;
96
97 pm_stay_awake(rtc->dev.parent);
98 mutex_unlock(&rtc->ops_lock);
99 /* A timer might have just expired */
100 schedule_work(&rtc->irqwork);
101
102 if (uie) {
103 err = rtc_update_irq_enable(rtc, 1);
104 if (err)
105 return err;
106 }
107
108 return err;
109 }
110 EXPORT_SYMBOL_GPL(rtc_set_time);
111
112 static int rtc_read_alarm_internal(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
113 {
114 int err;
115
116 err = mutex_lock_interruptible(&rtc->ops_lock);
117 if (err)
118 return err;
119
120 if (rtc->ops == NULL)
121 err = -ENODEV;
122 else if (!rtc->ops->read_alarm)
123 err = -EINVAL;
124 else {
125 alarm->enabled = 0;
126 alarm->pending = 0;
127 alarm->time.tm_sec = -1;
128 alarm->time.tm_min = -1;
129 alarm->time.tm_hour = -1;
130 alarm->time.tm_mday = -1;
131 alarm->time.tm_mon = -1;
132 alarm->time.tm_year = -1;
133 alarm->time.tm_wday = -1;
134 alarm->time.tm_yday = -1;
135 alarm->time.tm_isdst = -1;
136 err = rtc->ops->read_alarm(rtc->dev.parent, alarm);
137 }
138
139 mutex_unlock(&rtc->ops_lock);
140 return err;
141 }
142
143 int __rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
144 {
145 int err;
146 struct rtc_time before, now;
147 int first_time = 1;
148 time64_t t_now, t_alm;
149 enum { none, day, month, year } missing = none;
150 unsigned days;
151
152 /* The lower level RTC driver may return -1 in some fields,
153 * creating invalid alarm->time values, for reasons like:
154 *
155 * - The hardware may not be capable of filling them in;
156 * many alarms match only on time-of-day fields, not
157 * day/month/year calendar data.
158 *
159 * - Some hardware uses illegal values as "wildcard" match
160 * values, which non-Linux firmware (like a BIOS) may try
161 * to set up as e.g. "alarm 15 minutes after each hour".
162 * Linux uses only oneshot alarms.
163 *
164 * When we see that here, we deal with it by using values from
165 * a current RTC timestamp for any missing (-1) values. The
166 * RTC driver prevents "periodic alarm" modes.
167 *
168 * But this can be racey, because some fields of the RTC timestamp
169 * may have wrapped in the interval since we read the RTC alarm,
170 * which would lead to us inserting inconsistent values in place
171 * of the -1 fields.
172 *
173 * Reading the alarm and timestamp in the reverse sequence
174 * would have the same race condition, and not solve the issue.
175 *
176 * So, we must first read the RTC timestamp,
177 * then read the RTC alarm value,
178 * and then read a second RTC timestamp.
179 *
180 * If any fields of the second timestamp have changed
181 * when compared with the first timestamp, then we know
182 * our timestamp may be inconsistent with that used by
183 * the low-level rtc_read_alarm_internal() function.
184 *
185 * So, when the two timestamps disagree, we just loop and do
186 * the process again to get a fully consistent set of values.
187 *
188 * This could all instead be done in the lower level driver,
189 * but since more than one lower level RTC implementation needs it,
190 * then it's probably best best to do it here instead of there..
191 */
192
193 /* Get the "before" timestamp */
194 err = rtc_read_time(rtc, &before);
195 if (err < 0)
196 return err;
197 do {
198 if (!first_time)
199 memcpy(&before, &now, sizeof(struct rtc_time));
200 first_time = 0;
201
202 /* get the RTC alarm values, which may be incomplete */
203 err = rtc_read_alarm_internal(rtc, alarm);
204 if (err)
205 return err;
206
207 /* full-function RTCs won't have such missing fields */
208 if (rtc_valid_tm(&alarm->time) == 0)
209 return 0;
210
211 /* get the "after" timestamp, to detect wrapped fields */
212 err = rtc_read_time(rtc, &now);
213 if (err < 0)
214 return err;
215
216 /* note that tm_sec is a "don't care" value here: */
217 } while ( before.tm_min != now.tm_min
218 || before.tm_hour != now.tm_hour
219 || before.tm_mon != now.tm_mon
220 || before.tm_year != now.tm_year);
221
222 /* Fill in the missing alarm fields using the timestamp; we
223 * know there's at least one since alarm->time is invalid.
224 */
225 if (alarm->time.tm_sec == -1)
226 alarm->time.tm_sec = now.tm_sec;
227 if (alarm->time.tm_min == -1)
228 alarm->time.tm_min = now.tm_min;
229 if (alarm->time.tm_hour == -1)
230 alarm->time.tm_hour = now.tm_hour;
231
232 /* For simplicity, only support date rollover for now */
233 if (alarm->time.tm_mday < 1 || alarm->time.tm_mday > 31) {
234 alarm->time.tm_mday = now.tm_mday;
235 missing = day;
236 }
237 if ((unsigned)alarm->time.tm_mon >= 12) {
238 alarm->time.tm_mon = now.tm_mon;
239 if (missing == none)
240 missing = month;
241 }
242 if (alarm->time.tm_year == -1) {
243 alarm->time.tm_year = now.tm_year;
244 if (missing == none)
245 missing = year;
246 }
247
248 /* Can't proceed if alarm is still invalid after replacing
249 * missing fields.
250 */
251 err = rtc_valid_tm(&alarm->time);
252 if (err)
253 goto done;
254
255 /* with luck, no rollover is needed */
256 t_now = rtc_tm_to_time64(&now);
257 t_alm = rtc_tm_to_time64(&alarm->time);
258 if (t_now < t_alm)
259 goto done;
260
261 switch (missing) {
262
263 /* 24 hour rollover ... if it's now 10am Monday, an alarm that
264 * that will trigger at 5am will do so at 5am Tuesday, which
265 * could also be in the next month or year. This is a common
266 * case, especially for PCs.
267 */
268 case day:
269 dev_dbg(&rtc->dev, "alarm rollover: %s\n", "day");
270 t_alm += 24 * 60 * 60;
271 rtc_time64_to_tm(t_alm, &alarm->time);
272 break;
273
274 /* Month rollover ... if it's the 31th, an alarm on the 3rd will
275 * be next month. An alarm matching on the 30th, 29th, or 28th
276 * may end up in the month after that! Many newer PCs support
277 * this type of alarm.
278 */
279 case month:
280 dev_dbg(&rtc->dev, "alarm rollover: %s\n", "month");
281 do {
282 if (alarm->time.tm_mon < 11)
283 alarm->time.tm_mon++;
284 else {
285 alarm->time.tm_mon = 0;
286 alarm->time.tm_year++;
287 }
288 days = rtc_month_days(alarm->time.tm_mon,
289 alarm->time.tm_year);
290 } while (days < alarm->time.tm_mday);
291 break;
292
293 /* Year rollover ... easy except for leap years! */
294 case year:
295 dev_dbg(&rtc->dev, "alarm rollover: %s\n", "year");
296 do {
297 alarm->time.tm_year++;
298 } while (!is_leap_year(alarm->time.tm_year + 1900)
299 && rtc_valid_tm(&alarm->time) != 0);
300 break;
301
302 default:
303 dev_warn(&rtc->dev, "alarm rollover not handled\n");
304 }
305
306 err = rtc_valid_tm(&alarm->time);
307
308 done:
309 if (err) {
310 dev_warn(&rtc->dev, "invalid alarm value: %d-%d-%d %d:%d:%d\n",
311 alarm->time.tm_year + 1900, alarm->time.tm_mon + 1,
312 alarm->time.tm_mday, alarm->time.tm_hour, alarm->time.tm_min,
313 alarm->time.tm_sec);
314 }
315
316 return err;
317 }
318
319 int rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
320 {
321 int err;
322
323 err = mutex_lock_interruptible(&rtc->ops_lock);
324 if (err)
325 return err;
326 if (rtc->ops == NULL)
327 err = -ENODEV;
328 else if (!rtc->ops->read_alarm)
329 err = -EINVAL;
330 else {
331 memset(alarm, 0, sizeof(struct rtc_wkalrm));
332 alarm->enabled = rtc->aie_timer.enabled;
333 alarm->time = rtc_ktime_to_tm(rtc->aie_timer.node.expires);
334 }
335 mutex_unlock(&rtc->ops_lock);
336
337 return err;
338 }
339 EXPORT_SYMBOL_GPL(rtc_read_alarm);
340
341 static int __rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
342 {
343 struct rtc_time tm;
344 time64_t now, scheduled;
345 int err;
346
347 err = rtc_valid_tm(&alarm->time);
348 if (err)
349 return err;
350 scheduled = rtc_tm_to_time64(&alarm->time);
351
352 /* Make sure we're not setting alarms in the past */
353 err = __rtc_read_time(rtc, &tm);
354 if (err)
355 return err;
356 now = rtc_tm_to_time64(&tm);
357 if (scheduled <= now)
358 return -ETIME;
359 /*
360 * XXX - We just checked to make sure the alarm time is not
361 * in the past, but there is still a race window where if
362 * the is alarm set for the next second and the second ticks
363 * over right here, before we set the alarm.
364 */
365
366 if (!rtc->ops)
367 err = -ENODEV;
368 else if (!rtc->ops->set_alarm)
369 err = -EINVAL;
370 else
371 err = rtc->ops->set_alarm(rtc->dev.parent, alarm);
372
373 return err;
374 }
375
376 int rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
377 {
378 int err;
379
380 if (!rtc->ops)
381 return -ENODEV;
382 else if (!rtc->ops->set_alarm)
383 return -EINVAL;
384
385 err = rtc_valid_tm(&alarm->time);
386 if (err != 0)
387 return err;
388
389 err = mutex_lock_interruptible(&rtc->ops_lock);
390 if (err)
391 return err;
392 if (rtc->aie_timer.enabled)
393 rtc_timer_remove(rtc, &rtc->aie_timer);
394
395 rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time);
396 rtc->aie_timer.period = 0;
397 if (alarm->enabled)
398 err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
399
400 mutex_unlock(&rtc->ops_lock);
401 return err;
402 }
403 EXPORT_SYMBOL_GPL(rtc_set_alarm);
404
405 /* Called once per device from rtc_device_register */
406 int rtc_initialize_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
407 {
408 int err;
409 struct rtc_time now;
410
411 err = rtc_valid_tm(&alarm->time);
412 if (err != 0)
413 return err;
414
415 err = rtc_read_time(rtc, &now);
416 if (err)
417 return err;
418
419 err = mutex_lock_interruptible(&rtc->ops_lock);
420 if (err)
421 return err;
422
423 rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time);
424 rtc->aie_timer.period = 0;
425
426 /* Alarm has to be enabled & in the future for us to enqueue it */
427 if (alarm->enabled && (rtc_tm_to_ktime(now) <
428 rtc->aie_timer.node.expires)) {
429
430 rtc->aie_timer.enabled = 1;
431 timerqueue_add(&rtc->timerqueue, &rtc->aie_timer.node);
432 }
433 mutex_unlock(&rtc->ops_lock);
434 return err;
435 }
436 EXPORT_SYMBOL_GPL(rtc_initialize_alarm);
437
438 int rtc_alarm_irq_enable(struct rtc_device *rtc, unsigned int enabled)
439 {
440 int err = mutex_lock_interruptible(&rtc->ops_lock);
441 if (err)
442 return err;
443
444 if (rtc->aie_timer.enabled != enabled) {
445 if (enabled)
446 err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
447 else
448 rtc_timer_remove(rtc, &rtc->aie_timer);
449 }
450
451 if (err)
452 /* nothing */;
453 else if (!rtc->ops)
454 err = -ENODEV;
455 else if (!rtc->ops->alarm_irq_enable)
456 err = -EINVAL;
457 else
458 err = rtc->ops->alarm_irq_enable(rtc->dev.parent, enabled);
459
460 mutex_unlock(&rtc->ops_lock);
461 return err;
462 }
463 EXPORT_SYMBOL_GPL(rtc_alarm_irq_enable);
464
465 int rtc_update_irq_enable(struct rtc_device *rtc, unsigned int enabled)
466 {
467 int err = mutex_lock_interruptible(&rtc->ops_lock);
468 if (err)
469 return err;
470
471 #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
472 if (enabled == 0 && rtc->uie_irq_active) {
473 mutex_unlock(&rtc->ops_lock);
474 return rtc_dev_update_irq_enable_emul(rtc, 0);
475 }
476 #endif
477 /* make sure we're changing state */
478 if (rtc->uie_rtctimer.enabled == enabled)
479 goto out;
480
481 if (rtc->uie_unsupported) {
482 err = -EINVAL;
483 goto out;
484 }
485
486 if (enabled) {
487 struct rtc_time tm;
488 ktime_t now, onesec;
489
490 __rtc_read_time(rtc, &tm);
491 onesec = ktime_set(1, 0);
492 now = rtc_tm_to_ktime(tm);
493 rtc->uie_rtctimer.node.expires = ktime_add(now, onesec);
494 rtc->uie_rtctimer.period = ktime_set(1, 0);
495 err = rtc_timer_enqueue(rtc, &rtc->uie_rtctimer);
496 } else
497 rtc_timer_remove(rtc, &rtc->uie_rtctimer);
498
499 out:
500 mutex_unlock(&rtc->ops_lock);
501 #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
502 /*
503 * Enable emulation if the driver did not provide
504 * the update_irq_enable function pointer or if returned
505 * -EINVAL to signal that it has been configured without
506 * interrupts or that are not available at the moment.
507 */
508 if (err == -EINVAL)
509 err = rtc_dev_update_irq_enable_emul(rtc, enabled);
510 #endif
511 return err;
512
513 }
514 EXPORT_SYMBOL_GPL(rtc_update_irq_enable);
515
516
517 /**
518 * rtc_handle_legacy_irq - AIE, UIE and PIE event hook
519 * @rtc: pointer to the rtc device
520 *
521 * This function is called when an AIE, UIE or PIE mode interrupt
522 * has occurred (or been emulated).
523 *
524 * Triggers the registered irq_task function callback.
525 */
526 void rtc_handle_legacy_irq(struct rtc_device *rtc, int num, int mode)
527 {
528 unsigned long flags;
529
530 /* mark one irq of the appropriate mode */
531 spin_lock_irqsave(&rtc->irq_lock, flags);
532 rtc->irq_data = (rtc->irq_data + (num << 8)) | (RTC_IRQF|mode);
533 spin_unlock_irqrestore(&rtc->irq_lock, flags);
534
535 /* call the task func */
536 spin_lock_irqsave(&rtc->irq_task_lock, flags);
537 if (rtc->irq_task)
538 rtc->irq_task->func(rtc->irq_task->private_data);
539 spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
540
541 wake_up_interruptible(&rtc->irq_queue);
542 kill_fasync(&rtc->async_queue, SIGIO, POLL_IN);
543 }
544
545
546 /**
547 * rtc_aie_update_irq - AIE mode rtctimer hook
548 * @private: pointer to the rtc_device
549 *
550 * This functions is called when the aie_timer expires.
551 */
552 void rtc_aie_update_irq(void *private)
553 {
554 struct rtc_device *rtc = (struct rtc_device *)private;
555 rtc_handle_legacy_irq(rtc, 1, RTC_AF);
556 }
557
558
559 /**
560 * rtc_uie_update_irq - UIE mode rtctimer hook
561 * @private: pointer to the rtc_device
562 *
563 * This functions is called when the uie_timer expires.
564 */
565 void rtc_uie_update_irq(void *private)
566 {
567 struct rtc_device *rtc = (struct rtc_device *)private;
568 rtc_handle_legacy_irq(rtc, 1, RTC_UF);
569 }
570
571
572 /**
573 * rtc_pie_update_irq - PIE mode hrtimer hook
574 * @timer: pointer to the pie mode hrtimer
575 *
576 * This function is used to emulate PIE mode interrupts
577 * using an hrtimer. This function is called when the periodic
578 * hrtimer expires.
579 */
580 enum hrtimer_restart rtc_pie_update_irq(struct hrtimer *timer)
581 {
582 struct rtc_device *rtc;
583 ktime_t period;
584 int count;
585 rtc = container_of(timer, struct rtc_device, pie_timer);
586
587 period = NSEC_PER_SEC / rtc->irq_freq;
588 count = hrtimer_forward_now(timer, period);
589
590 rtc_handle_legacy_irq(rtc, count, RTC_PF);
591
592 return HRTIMER_RESTART;
593 }
594
595 /**
596 * rtc_update_irq - Triggered when a RTC interrupt occurs.
597 * @rtc: the rtc device
598 * @num: how many irqs are being reported (usually one)
599 * @events: mask of RTC_IRQF with one or more of RTC_PF, RTC_AF, RTC_UF
600 * Context: any
601 */
602 void rtc_update_irq(struct rtc_device *rtc,
603 unsigned long num, unsigned long events)
604 {
605 if (IS_ERR_OR_NULL(rtc))
606 return;
607
608 pm_stay_awake(rtc->dev.parent);
609 schedule_work(&rtc->irqwork);
610 }
611 EXPORT_SYMBOL_GPL(rtc_update_irq);
612
613 static int __rtc_match(struct device *dev, const void *data)
614 {
615 const char *name = data;
616
617 if (strcmp(dev_name(dev), name) == 0)
618 return 1;
619 return 0;
620 }
621
622 struct rtc_device *rtc_class_open(const char *name)
623 {
624 struct device *dev;
625 struct rtc_device *rtc = NULL;
626
627 dev = class_find_device(rtc_class, NULL, name, __rtc_match);
628 if (dev)
629 rtc = to_rtc_device(dev);
630
631 if (rtc) {
632 if (!try_module_get(rtc->owner)) {
633 put_device(dev);
634 rtc = NULL;
635 }
636 }
637
638 return rtc;
639 }
640 EXPORT_SYMBOL_GPL(rtc_class_open);
641
642 void rtc_class_close(struct rtc_device *rtc)
643 {
644 module_put(rtc->owner);
645 put_device(&rtc->dev);
646 }
647 EXPORT_SYMBOL_GPL(rtc_class_close);
648
649 int rtc_irq_register(struct rtc_device *rtc, struct rtc_task *task)
650 {
651 int retval = -EBUSY;
652
653 if (task == NULL || task->func == NULL)
654 return -EINVAL;
655
656 /* Cannot register while the char dev is in use */
657 if (test_and_set_bit_lock(RTC_DEV_BUSY, &rtc->flags))
658 return -EBUSY;
659
660 spin_lock_irq(&rtc->irq_task_lock);
661 if (rtc->irq_task == NULL) {
662 rtc->irq_task = task;
663 retval = 0;
664 }
665 spin_unlock_irq(&rtc->irq_task_lock);
666
667 clear_bit_unlock(RTC_DEV_BUSY, &rtc->flags);
668
669 return retval;
670 }
671 EXPORT_SYMBOL_GPL(rtc_irq_register);
672
673 void rtc_irq_unregister(struct rtc_device *rtc, struct rtc_task *task)
674 {
675 spin_lock_irq(&rtc->irq_task_lock);
676 if (rtc->irq_task == task)
677 rtc->irq_task = NULL;
678 spin_unlock_irq(&rtc->irq_task_lock);
679 }
680 EXPORT_SYMBOL_GPL(rtc_irq_unregister);
681
682 static int rtc_update_hrtimer(struct rtc_device *rtc, int enabled)
683 {
684 /*
685 * We always cancel the timer here first, because otherwise
686 * we could run into BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
687 * when we manage to start the timer before the callback
688 * returns HRTIMER_RESTART.
689 *
690 * We cannot use hrtimer_cancel() here as a running callback
691 * could be blocked on rtc->irq_task_lock and hrtimer_cancel()
692 * would spin forever.
693 */
694 if (hrtimer_try_to_cancel(&rtc->pie_timer) < 0)
695 return -1;
696
697 if (enabled) {
698 ktime_t period = NSEC_PER_SEC / rtc->irq_freq;
699
700 hrtimer_start(&rtc->pie_timer, period, HRTIMER_MODE_REL);
701 }
702 return 0;
703 }
704
705 /**
706 * rtc_irq_set_state - enable/disable 2^N Hz periodic IRQs
707 * @rtc: the rtc device
708 * @task: currently registered with rtc_irq_register()
709 * @enabled: true to enable periodic IRQs
710 * Context: any
711 *
712 * Note that rtc_irq_set_freq() should previously have been used to
713 * specify the desired frequency of periodic IRQ task->func() callbacks.
714 */
715 int rtc_irq_set_state(struct rtc_device *rtc, struct rtc_task *task, int enabled)
716 {
717 int err = 0;
718 unsigned long flags;
719
720 retry:
721 spin_lock_irqsave(&rtc->irq_task_lock, flags);
722 if (rtc->irq_task != NULL && task == NULL)
723 err = -EBUSY;
724 else if (rtc->irq_task != task)
725 err = -EACCES;
726 else {
727 if (rtc_update_hrtimer(rtc, enabled) < 0) {
728 spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
729 cpu_relax();
730 goto retry;
731 }
732 rtc->pie_enabled = enabled;
733 }
734 spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
735 return err;
736 }
737 EXPORT_SYMBOL_GPL(rtc_irq_set_state);
738
739 /**
740 * rtc_irq_set_freq - set 2^N Hz periodic IRQ frequency for IRQ
741 * @rtc: the rtc device
742 * @task: currently registered with rtc_irq_register()
743 * @freq: positive frequency with which task->func() will be called
744 * Context: any
745 *
746 * Note that rtc_irq_set_state() is used to enable or disable the
747 * periodic IRQs.
748 */
749 int rtc_irq_set_freq(struct rtc_device *rtc, struct rtc_task *task, int freq)
750 {
751 int err = 0;
752 unsigned long flags;
753
754 if (freq <= 0 || freq > RTC_MAX_FREQ)
755 return -EINVAL;
756 retry:
757 spin_lock_irqsave(&rtc->irq_task_lock, flags);
758 if (rtc->irq_task != NULL && task == NULL)
759 err = -EBUSY;
760 else if (rtc->irq_task != task)
761 err = -EACCES;
762 else {
763 rtc->irq_freq = freq;
764 if (rtc->pie_enabled && rtc_update_hrtimer(rtc, 1) < 0) {
765 spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
766 cpu_relax();
767 goto retry;
768 }
769 }
770 spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
771 return err;
772 }
773 EXPORT_SYMBOL_GPL(rtc_irq_set_freq);
774
775 /**
776 * rtc_timer_enqueue - Adds a rtc_timer to the rtc_device timerqueue
777 * @rtc rtc device
778 * @timer timer being added.
779 *
780 * Enqueues a timer onto the rtc devices timerqueue and sets
781 * the next alarm event appropriately.
782 *
783 * Sets the enabled bit on the added timer.
784 *
785 * Must hold ops_lock for proper serialization of timerqueue
786 */
787 static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer)
788 {
789 struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue);
790 struct rtc_time tm;
791 ktime_t now;
792
793 timer->enabled = 1;
794 __rtc_read_time(rtc, &tm);
795 now = rtc_tm_to_ktime(tm);
796
797 /* Skip over expired timers */
798 while (next) {
799 if (next->expires >= now)
800 break;
801 next = timerqueue_iterate_next(next);
802 }
803
804 timerqueue_add(&rtc->timerqueue, &timer->node);
805 if (!next || ktime_before(timer->node.expires, next->expires)) {
806 struct rtc_wkalrm alarm;
807 int err;
808 alarm.time = rtc_ktime_to_tm(timer->node.expires);
809 alarm.enabled = 1;
810 err = __rtc_set_alarm(rtc, &alarm);
811 if (err == -ETIME) {
812 pm_stay_awake(rtc->dev.parent);
813 schedule_work(&rtc->irqwork);
814 } else if (err) {
815 timerqueue_del(&rtc->timerqueue, &timer->node);
816 timer->enabled = 0;
817 return err;
818 }
819 }
820 return 0;
821 }
822
823 static void rtc_alarm_disable(struct rtc_device *rtc)
824 {
825 if (!rtc->ops || !rtc->ops->alarm_irq_enable)
826 return;
827
828 rtc->ops->alarm_irq_enable(rtc->dev.parent, false);
829 }
830
831 /**
832 * rtc_timer_remove - Removes a rtc_timer from the rtc_device timerqueue
833 * @rtc rtc device
834 * @timer timer being removed.
835 *
836 * Removes a timer onto the rtc devices timerqueue and sets
837 * the next alarm event appropriately.
838 *
839 * Clears the enabled bit on the removed timer.
840 *
841 * Must hold ops_lock for proper serialization of timerqueue
842 */
843 static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer)
844 {
845 struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue);
846 timerqueue_del(&rtc->timerqueue, &timer->node);
847 timer->enabled = 0;
848 if (next == &timer->node) {
849 struct rtc_wkalrm alarm;
850 int err;
851 next = timerqueue_getnext(&rtc->timerqueue);
852 if (!next) {
853 rtc_alarm_disable(rtc);
854 return;
855 }
856 alarm.time = rtc_ktime_to_tm(next->expires);
857 alarm.enabled = 1;
858 err = __rtc_set_alarm(rtc, &alarm);
859 if (err == -ETIME) {
860 pm_stay_awake(rtc->dev.parent);
861 schedule_work(&rtc->irqwork);
862 }
863 }
864 }
865
866 /**
867 * rtc_timer_do_work - Expires rtc timers
868 * @rtc rtc device
869 * @timer timer being removed.
870 *
871 * Expires rtc timers. Reprograms next alarm event if needed.
872 * Called via worktask.
873 *
874 * Serializes access to timerqueue via ops_lock mutex
875 */
876 void rtc_timer_do_work(struct work_struct *work)
877 {
878 struct rtc_timer *timer;
879 struct timerqueue_node *next;
880 ktime_t now;
881 struct rtc_time tm;
882
883 struct rtc_device *rtc =
884 container_of(work, struct rtc_device, irqwork);
885
886 mutex_lock(&rtc->ops_lock);
887 again:
888 __rtc_read_time(rtc, &tm);
889 now = rtc_tm_to_ktime(tm);
890 while ((next = timerqueue_getnext(&rtc->timerqueue))) {
891 if (next->expires > now)
892 break;
893
894 /* expire timer */
895 timer = container_of(next, struct rtc_timer, node);
896 timerqueue_del(&rtc->timerqueue, &timer->node);
897 timer->enabled = 0;
898 if (timer->task.func)
899 timer->task.func(timer->task.private_data);
900
901 /* Re-add/fwd periodic timers */
902 if (ktime_to_ns(timer->period)) {
903 timer->node.expires = ktime_add(timer->node.expires,
904 timer->period);
905 timer->enabled = 1;
906 timerqueue_add(&rtc->timerqueue, &timer->node);
907 }
908 }
909
910 /* Set next alarm */
911 if (next) {
912 struct rtc_wkalrm alarm;
913 int err;
914 int retry = 3;
915
916 alarm.time = rtc_ktime_to_tm(next->expires);
917 alarm.enabled = 1;
918 reprogram:
919 err = __rtc_set_alarm(rtc, &alarm);
920 if (err == -ETIME)
921 goto again;
922 else if (err) {
923 if (retry-- > 0)
924 goto reprogram;
925
926 timer = container_of(next, struct rtc_timer, node);
927 timerqueue_del(&rtc->timerqueue, &timer->node);
928 timer->enabled = 0;
929 dev_err(&rtc->dev, "__rtc_set_alarm: err=%d\n", err);
930 goto again;
931 }
932 } else
933 rtc_alarm_disable(rtc);
934
935 pm_relax(rtc->dev.parent);
936 mutex_unlock(&rtc->ops_lock);
937 }
938
939
940 /* rtc_timer_init - Initializes an rtc_timer
941 * @timer: timer to be intiialized
942 * @f: function pointer to be called when timer fires
943 * @data: private data passed to function pointer
944 *
945 * Kernel interface to initializing an rtc_timer.
946 */
947 void rtc_timer_init(struct rtc_timer *timer, void (*f)(void *p), void *data)
948 {
949 timerqueue_init(&timer->node);
950 timer->enabled = 0;
951 timer->task.func = f;
952 timer->task.private_data = data;
953 }
954
955 /* rtc_timer_start - Sets an rtc_timer to fire in the future
956 * @ rtc: rtc device to be used
957 * @ timer: timer being set
958 * @ expires: time at which to expire the timer
959 * @ period: period that the timer will recur
960 *
961 * Kernel interface to set an rtc_timer
962 */
963 int rtc_timer_start(struct rtc_device *rtc, struct rtc_timer *timer,
964 ktime_t expires, ktime_t period)
965 {
966 int ret = 0;
967 mutex_lock(&rtc->ops_lock);
968 if (timer->enabled)
969 rtc_timer_remove(rtc, timer);
970
971 timer->node.expires = expires;
972 timer->period = period;
973
974 ret = rtc_timer_enqueue(rtc, timer);
975
976 mutex_unlock(&rtc->ops_lock);
977 return ret;
978 }
979
980 /* rtc_timer_cancel - Stops an rtc_timer
981 * @ rtc: rtc device to be used
982 * @ timer: timer being set
983 *
984 * Kernel interface to cancel an rtc_timer
985 */
986 void rtc_timer_cancel(struct rtc_device *rtc, struct rtc_timer *timer)
987 {
988 mutex_lock(&rtc->ops_lock);
989 if (timer->enabled)
990 rtc_timer_remove(rtc, timer);
991 mutex_unlock(&rtc->ops_lock);
992 }
993
994 /**
995 * rtc_read_offset - Read the amount of rtc offset in parts per billion
996 * @ rtc: rtc device to be used
997 * @ offset: the offset in parts per billion
998 *
999 * see below for details.
1000 *
1001 * Kernel interface to read rtc clock offset
1002 * Returns 0 on success, or a negative number on error.
1003 * If read_offset() is not implemented for the rtc, return -EINVAL
1004 */
1005 int rtc_read_offset(struct rtc_device *rtc, long *offset)
1006 {
1007 int ret;
1008
1009 if (!rtc->ops)
1010 return -ENODEV;
1011
1012 if (!rtc->ops->read_offset)
1013 return -EINVAL;
1014
1015 mutex_lock(&rtc->ops_lock);
1016 ret = rtc->ops->read_offset(rtc->dev.parent, offset);
1017 mutex_unlock(&rtc->ops_lock);
1018 return ret;
1019 }
1020
1021 /**
1022 * rtc_set_offset - Adjusts the duration of the average second
1023 * @ rtc: rtc device to be used
1024 * @ offset: the offset in parts per billion
1025 *
1026 * Some rtc's allow an adjustment to the average duration of a second
1027 * to compensate for differences in the actual clock rate due to temperature,
1028 * the crystal, capacitor, etc.
1029 *
1030 * The adjustment applied is as follows:
1031 * t = t0 * (1 + offset * 1e-9)
1032 * where t0 is the measured length of 1 RTC second with offset = 0
1033 *
1034 * Kernel interface to adjust an rtc clock offset.
1035 * Return 0 on success, or a negative number on error.
1036 * If the rtc offset is not setable (or not implemented), return -EINVAL
1037 */
1038 int rtc_set_offset(struct rtc_device *rtc, long offset)
1039 {
1040 int ret;
1041
1042 if (!rtc->ops)
1043 return -ENODEV;
1044
1045 if (!rtc->ops->set_offset)
1046 return -EINVAL;
1047
1048 mutex_lock(&rtc->ops_lock);
1049 ret = rtc->ops->set_offset(rtc->dev.parent, offset);
1050 mutex_unlock(&rtc->ops_lock);
1051 return ret;
1052 }