2 * RTC subsystem, interface functions
4 * Copyright (C) 2005 Tower Technologies
5 * Author: Alessandro Zummo <a.zummo@towertech.it>
7 * based on arch/arm/common/rtctime.c
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.
14 #include <linux/rtc.h>
15 #include <linux/sched.h>
16 #include <linux/log2.h>
17 #include <linux/workqueue.h>
19 static int rtc_timer_enqueue(struct rtc_device
*rtc
, struct rtc_timer
*timer
);
20 static void rtc_timer_remove(struct rtc_device
*rtc
, struct rtc_timer
*timer
);
22 static int __rtc_read_time(struct rtc_device
*rtc
, struct rtc_time
*tm
)
27 else if (!rtc
->ops
->read_time
)
30 memset(tm
, 0, sizeof(struct rtc_time
));
31 err
= rtc
->ops
->read_time(rtc
->dev
.parent
, tm
);
36 int rtc_read_time(struct rtc_device
*rtc
, struct rtc_time
*tm
)
40 err
= mutex_lock_interruptible(&rtc
->ops_lock
);
44 err
= __rtc_read_time(rtc
, tm
);
45 mutex_unlock(&rtc
->ops_lock
);
48 EXPORT_SYMBOL_GPL(rtc_read_time
);
50 int rtc_set_time(struct rtc_device
*rtc
, struct rtc_time
*tm
)
54 err
= rtc_valid_tm(tm
);
58 err
= mutex_lock_interruptible(&rtc
->ops_lock
);
64 else if (rtc
->ops
->set_time
)
65 err
= rtc
->ops
->set_time(rtc
->dev
.parent
, tm
);
66 else if (rtc
->ops
->set_mmss
) {
68 err
= rtc_tm_to_time(tm
, &secs
);
70 err
= rtc
->ops
->set_mmss(rtc
->dev
.parent
, secs
);
74 mutex_unlock(&rtc
->ops_lock
);
77 EXPORT_SYMBOL_GPL(rtc_set_time
);
79 int rtc_set_mmss(struct rtc_device
*rtc
, unsigned long secs
)
83 err
= mutex_lock_interruptible(&rtc
->ops_lock
);
89 else if (rtc
->ops
->set_mmss
)
90 err
= rtc
->ops
->set_mmss(rtc
->dev
.parent
, secs
);
91 else if (rtc
->ops
->read_time
&& rtc
->ops
->set_time
) {
92 struct rtc_time
new, old
;
94 err
= rtc
->ops
->read_time(rtc
->dev
.parent
, &old
);
96 rtc_time_to_tm(secs
, &new);
99 * avoid writing when we're going to change the day of
100 * the month. We will retry in the next minute. This
101 * basically means that if the RTC must not drift
102 * by more than 1 minute in 11 minutes.
104 if (!((old
.tm_hour
== 23 && old
.tm_min
== 59) ||
105 (new.tm_hour
== 23 && new.tm_min
== 59)))
106 err
= rtc
->ops
->set_time(rtc
->dev
.parent
,
113 mutex_unlock(&rtc
->ops_lock
);
117 EXPORT_SYMBOL_GPL(rtc_set_mmss
);
119 int rtc_read_alarm(struct rtc_device
*rtc
, struct rtc_wkalrm
*alarm
)
123 err
= mutex_lock_interruptible(&rtc
->ops_lock
);
126 if (rtc
->ops
== NULL
)
128 else if (!rtc
->ops
->read_alarm
)
131 memset(alarm
, 0, sizeof(struct rtc_wkalrm
));
132 alarm
->enabled
= rtc
->aie_timer
.enabled
;
133 alarm
->time
= rtc_ktime_to_tm(rtc
->aie_timer
.node
.expires
);
135 mutex_unlock(&rtc
->ops_lock
);
139 EXPORT_SYMBOL_GPL(rtc_read_alarm
);
141 int __rtc_set_alarm(struct rtc_device
*rtc
, struct rtc_wkalrm
*alarm
)
147 err
= rtc_valid_tm(&alarm
->time
);
150 rtc_tm_to_time(&alarm
->time
, &scheduled
);
152 /* Make sure we're not setting alarms in the past */
153 err
= __rtc_read_time(rtc
, &tm
);
154 rtc_tm_to_time(&tm
, &now
);
155 if (scheduled
<= now
)
158 * XXX - We just checked to make sure the alarm time is not
159 * in the past, but there is still a race window where if
160 * the is alarm set for the next second and the second ticks
161 * over right here, before we set the alarm.
166 else if (!rtc
->ops
->set_alarm
)
169 err
= rtc
->ops
->set_alarm(rtc
->dev
.parent
, alarm
);
174 int rtc_set_alarm(struct rtc_device
*rtc
, struct rtc_wkalrm
*alarm
)
178 err
= rtc_valid_tm(&alarm
->time
);
182 err
= mutex_lock_interruptible(&rtc
->ops_lock
);
185 if (rtc
->aie_timer
.enabled
) {
186 rtc_timer_remove(rtc
, &rtc
->aie_timer
);
188 rtc
->aie_timer
.node
.expires
= rtc_tm_to_ktime(alarm
->time
);
189 rtc
->aie_timer
.period
= ktime_set(0, 0);
190 if (alarm
->enabled
) {
191 err
= rtc_timer_enqueue(rtc
, &rtc
->aie_timer
);
193 mutex_unlock(&rtc
->ops_lock
);
196 EXPORT_SYMBOL_GPL(rtc_set_alarm
);
198 int rtc_alarm_irq_enable(struct rtc_device
*rtc
, unsigned int enabled
)
200 int err
= mutex_lock_interruptible(&rtc
->ops_lock
);
204 if (rtc
->aie_timer
.enabled
!= enabled
) {
206 err
= rtc_timer_enqueue(rtc
, &rtc
->aie_timer
);
208 rtc_timer_remove(rtc
, &rtc
->aie_timer
);
215 else if (!rtc
->ops
->alarm_irq_enable
)
218 err
= rtc
->ops
->alarm_irq_enable(rtc
->dev
.parent
, enabled
);
220 mutex_unlock(&rtc
->ops_lock
);
223 EXPORT_SYMBOL_GPL(rtc_alarm_irq_enable
);
225 int rtc_update_irq_enable(struct rtc_device
*rtc
, unsigned int enabled
)
227 int err
= mutex_lock_interruptible(&rtc
->ops_lock
);
231 #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
232 if (enabled
== 0 && rtc
->uie_irq_active
) {
233 mutex_unlock(&rtc
->ops_lock
);
234 return rtc_dev_update_irq_enable_emul(rtc
, 0);
237 /* make sure we're changing state */
238 if (rtc
->uie_rtctimer
.enabled
== enabled
)
245 __rtc_read_time(rtc
, &tm
);
246 onesec
= ktime_set(1, 0);
247 now
= rtc_tm_to_ktime(tm
);
248 rtc
->uie_rtctimer
.node
.expires
= ktime_add(now
, onesec
);
249 rtc
->uie_rtctimer
.period
= ktime_set(1, 0);
250 err
= rtc_timer_enqueue(rtc
, &rtc
->uie_rtctimer
);
252 rtc_timer_remove(rtc
, &rtc
->uie_rtctimer
);
255 mutex_unlock(&rtc
->ops_lock
);
256 #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
258 * Enable emulation if the driver did not provide
259 * the update_irq_enable function pointer or if returned
260 * -EINVAL to signal that it has been configured without
261 * interrupts or that are not available at the moment.
264 err
= rtc_dev_update_irq_enable_emul(rtc
, enabled
);
269 EXPORT_SYMBOL_GPL(rtc_update_irq_enable
);
273 * rtc_handle_legacy_irq - AIE, UIE and PIE event hook
274 * @rtc: pointer to the rtc device
276 * This function is called when an AIE, UIE or PIE mode interrupt
277 * has occured (or been emulated).
279 * Triggers the registered irq_task function callback.
281 void rtc_handle_legacy_irq(struct rtc_device
*rtc
, int num
, int mode
)
285 /* mark one irq of the appropriate mode */
286 spin_lock_irqsave(&rtc
->irq_lock
, flags
);
287 rtc
->irq_data
= (rtc
->irq_data
+ (num
<< 8)) | (RTC_IRQF
|mode
);
288 spin_unlock_irqrestore(&rtc
->irq_lock
, flags
);
290 /* call the task func */
291 spin_lock_irqsave(&rtc
->irq_task_lock
, flags
);
293 rtc
->irq_task
->func(rtc
->irq_task
->private_data
);
294 spin_unlock_irqrestore(&rtc
->irq_task_lock
, flags
);
296 wake_up_interruptible(&rtc
->irq_queue
);
297 kill_fasync(&rtc
->async_queue
, SIGIO
, POLL_IN
);
302 * rtc_aie_update_irq - AIE mode rtctimer hook
303 * @private: pointer to the rtc_device
305 * This functions is called when the aie_timer expires.
307 void rtc_aie_update_irq(void *private)
309 struct rtc_device
*rtc
= (struct rtc_device
*)private;
310 rtc_handle_legacy_irq(rtc
, 1, RTC_AF
);
315 * rtc_uie_update_irq - UIE mode rtctimer hook
316 * @private: pointer to the rtc_device
318 * This functions is called when the uie_timer expires.
320 void rtc_uie_update_irq(void *private)
322 struct rtc_device
*rtc
= (struct rtc_device
*)private;
323 rtc_handle_legacy_irq(rtc
, 1, RTC_UF
);
328 * rtc_pie_update_irq - PIE mode hrtimer hook
329 * @timer: pointer to the pie mode hrtimer
331 * This function is used to emulate PIE mode interrupts
332 * using an hrtimer. This function is called when the periodic
335 enum hrtimer_restart
rtc_pie_update_irq(struct hrtimer
*timer
)
337 struct rtc_device
*rtc
;
340 rtc
= container_of(timer
, struct rtc_device
, pie_timer
);
342 period
= ktime_set(0, NSEC_PER_SEC
/rtc
->irq_freq
);
343 count
= hrtimer_forward_now(timer
, period
);
345 rtc_handle_legacy_irq(rtc
, count
, RTC_PF
);
347 return HRTIMER_RESTART
;
351 * rtc_update_irq - Triggered when a RTC interrupt occurs.
352 * @rtc: the rtc device
353 * @num: how many irqs are being reported (usually one)
354 * @events: mask of RTC_IRQF with one or more of RTC_PF, RTC_AF, RTC_UF
357 void rtc_update_irq(struct rtc_device
*rtc
,
358 unsigned long num
, unsigned long events
)
360 schedule_work(&rtc
->irqwork
);
362 EXPORT_SYMBOL_GPL(rtc_update_irq
);
364 static int __rtc_match(struct device
*dev
, void *data
)
366 char *name
= (char *)data
;
368 if (strcmp(dev_name(dev
), name
) == 0)
373 struct rtc_device
*rtc_class_open(char *name
)
376 struct rtc_device
*rtc
= NULL
;
378 dev
= class_find_device(rtc_class
, NULL
, name
, __rtc_match
);
380 rtc
= to_rtc_device(dev
);
383 if (!try_module_get(rtc
->owner
)) {
391 EXPORT_SYMBOL_GPL(rtc_class_open
);
393 void rtc_class_close(struct rtc_device
*rtc
)
395 module_put(rtc
->owner
);
396 put_device(&rtc
->dev
);
398 EXPORT_SYMBOL_GPL(rtc_class_close
);
400 int rtc_irq_register(struct rtc_device
*rtc
, struct rtc_task
*task
)
404 if (task
== NULL
|| task
->func
== NULL
)
407 /* Cannot register while the char dev is in use */
408 if (test_and_set_bit_lock(RTC_DEV_BUSY
, &rtc
->flags
))
411 spin_lock_irq(&rtc
->irq_task_lock
);
412 if (rtc
->irq_task
== NULL
) {
413 rtc
->irq_task
= task
;
416 spin_unlock_irq(&rtc
->irq_task_lock
);
418 clear_bit_unlock(RTC_DEV_BUSY
, &rtc
->flags
);
422 EXPORT_SYMBOL_GPL(rtc_irq_register
);
424 void rtc_irq_unregister(struct rtc_device
*rtc
, struct rtc_task
*task
)
426 spin_lock_irq(&rtc
->irq_task_lock
);
427 if (rtc
->irq_task
== task
)
428 rtc
->irq_task
= NULL
;
429 spin_unlock_irq(&rtc
->irq_task_lock
);
431 EXPORT_SYMBOL_GPL(rtc_irq_unregister
);
434 * rtc_irq_set_state - enable/disable 2^N Hz periodic IRQs
435 * @rtc: the rtc device
436 * @task: currently registered with rtc_irq_register()
437 * @enabled: true to enable periodic IRQs
440 * Note that rtc_irq_set_freq() should previously have been used to
441 * specify the desired frequency of periodic IRQ task->func() callbacks.
443 int rtc_irq_set_state(struct rtc_device
*rtc
, struct rtc_task
*task
, int enabled
)
448 spin_lock_irqsave(&rtc
->irq_task_lock
, flags
);
449 if (rtc
->irq_task
!= NULL
&& task
== NULL
)
451 if (rtc
->irq_task
!= task
)
455 ktime_t period
= ktime_set(0, NSEC_PER_SEC
/rtc
->irq_freq
);
456 hrtimer_start(&rtc
->pie_timer
, period
, HRTIMER_MODE_REL
);
458 hrtimer_cancel(&rtc
->pie_timer
);
460 rtc
->pie_enabled
= enabled
;
461 spin_unlock_irqrestore(&rtc
->irq_task_lock
, flags
);
465 EXPORT_SYMBOL_GPL(rtc_irq_set_state
);
468 * rtc_irq_set_freq - set 2^N Hz periodic IRQ frequency for IRQ
469 * @rtc: the rtc device
470 * @task: currently registered with rtc_irq_register()
471 * @freq: positive frequency with which task->func() will be called
474 * Note that rtc_irq_set_state() is used to enable or disable the
477 int rtc_irq_set_freq(struct rtc_device
*rtc
, struct rtc_task
*task
, int freq
)
485 spin_lock_irqsave(&rtc
->irq_task_lock
, flags
);
486 if (rtc
->irq_task
!= NULL
&& task
== NULL
)
488 if (rtc
->irq_task
!= task
)
491 rtc
->irq_freq
= freq
;
492 if (rtc
->pie_enabled
) {
494 hrtimer_cancel(&rtc
->pie_timer
);
495 period
= ktime_set(0, NSEC_PER_SEC
/rtc
->irq_freq
);
496 hrtimer_start(&rtc
->pie_timer
, period
,
500 spin_unlock_irqrestore(&rtc
->irq_task_lock
, flags
);
503 EXPORT_SYMBOL_GPL(rtc_irq_set_freq
);
506 * rtc_timer_enqueue - Adds a rtc_timer to the rtc_device timerqueue
508 * @timer timer being added.
510 * Enqueues a timer onto the rtc devices timerqueue and sets
511 * the next alarm event appropriately.
513 * Sets the enabled bit on the added timer.
515 * Must hold ops_lock for proper serialization of timerqueue
517 static int rtc_timer_enqueue(struct rtc_device
*rtc
, struct rtc_timer
*timer
)
520 timerqueue_add(&rtc
->timerqueue
, &timer
->node
);
521 if (&timer
->node
== timerqueue_getnext(&rtc
->timerqueue
)) {
522 struct rtc_wkalrm alarm
;
524 alarm
.time
= rtc_ktime_to_tm(timer
->node
.expires
);
526 err
= __rtc_set_alarm(rtc
, &alarm
);
528 schedule_work(&rtc
->irqwork
);
530 timerqueue_del(&rtc
->timerqueue
, &timer
->node
);
539 * rtc_timer_remove - Removes a rtc_timer from the rtc_device timerqueue
541 * @timer timer being removed.
543 * Removes a timer onto the rtc devices timerqueue and sets
544 * the next alarm event appropriately.
546 * Clears the enabled bit on the removed timer.
548 * Must hold ops_lock for proper serialization of timerqueue
550 static void rtc_timer_remove(struct rtc_device
*rtc
, struct rtc_timer
*timer
)
552 struct timerqueue_node
*next
= timerqueue_getnext(&rtc
->timerqueue
);
553 timerqueue_del(&rtc
->timerqueue
, &timer
->node
);
555 if (next
== &timer
->node
) {
556 struct rtc_wkalrm alarm
;
558 next
= timerqueue_getnext(&rtc
->timerqueue
);
561 alarm
.time
= rtc_ktime_to_tm(next
->expires
);
563 err
= __rtc_set_alarm(rtc
, &alarm
);
565 schedule_work(&rtc
->irqwork
);
570 * rtc_timer_do_work - Expires rtc timers
572 * @timer timer being removed.
574 * Expires rtc timers. Reprograms next alarm event if needed.
575 * Called via worktask.
577 * Serializes access to timerqueue via ops_lock mutex
579 void rtc_timer_do_work(struct work_struct
*work
)
581 struct rtc_timer
*timer
;
582 struct timerqueue_node
*next
;
586 struct rtc_device
*rtc
=
587 container_of(work
, struct rtc_device
, irqwork
);
589 mutex_lock(&rtc
->ops_lock
);
591 __rtc_read_time(rtc
, &tm
);
592 now
= rtc_tm_to_ktime(tm
);
593 while ((next
= timerqueue_getnext(&rtc
->timerqueue
))) {
594 if (next
->expires
.tv64
> now
.tv64
)
598 timer
= container_of(next
, struct rtc_timer
, node
);
599 timerqueue_del(&rtc
->timerqueue
, &timer
->node
);
601 if (timer
->task
.func
)
602 timer
->task
.func(timer
->task
.private_data
);
604 /* Re-add/fwd periodic timers */
605 if (ktime_to_ns(timer
->period
)) {
606 timer
->node
.expires
= ktime_add(timer
->node
.expires
,
609 timerqueue_add(&rtc
->timerqueue
, &timer
->node
);
615 struct rtc_wkalrm alarm
;
617 alarm
.time
= rtc_ktime_to_tm(next
->expires
);
619 err
= __rtc_set_alarm(rtc
, &alarm
);
624 mutex_unlock(&rtc
->ops_lock
);
628 /* rtc_timer_init - Initializes an rtc_timer
629 * @timer: timer to be intiialized
630 * @f: function pointer to be called when timer fires
631 * @data: private data passed to function pointer
633 * Kernel interface to initializing an rtc_timer.
635 void rtc_timer_init(struct rtc_timer
*timer
, void (*f
)(void* p
), void* data
)
637 timerqueue_init(&timer
->node
);
639 timer
->task
.func
= f
;
640 timer
->task
.private_data
= data
;
643 /* rtc_timer_start - Sets an rtc_timer to fire in the future
644 * @ rtc: rtc device to be used
645 * @ timer: timer being set
646 * @ expires: time at which to expire the timer
647 * @ period: period that the timer will recur
649 * Kernel interface to set an rtc_timer
651 int rtc_timer_start(struct rtc_device
*rtc
, struct rtc_timer
* timer
,
652 ktime_t expires
, ktime_t period
)
655 mutex_lock(&rtc
->ops_lock
);
657 rtc_timer_remove(rtc
, timer
);
659 timer
->node
.expires
= expires
;
660 timer
->period
= period
;
662 ret
= rtc_timer_enqueue(rtc
, timer
);
664 mutex_unlock(&rtc
->ops_lock
);
668 /* rtc_timer_cancel - Stops an rtc_timer
669 * @ rtc: rtc device to be used
670 * @ timer: timer being set
672 * Kernel interface to cancel an rtc_timer
674 int rtc_timer_cancel(struct rtc_device
*rtc
, struct rtc_timer
* timer
)
677 mutex_lock(&rtc
->ops_lock
);
679 rtc_timer_remove(rtc
, timer
);
680 mutex_unlock(&rtc
->ops_lock
);