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
);
216 else if (!rtc
->ops
->alarm_irq_enable
)
219 err
= rtc
->ops
->alarm_irq_enable(rtc
->dev
.parent
, enabled
);
221 mutex_unlock(&rtc
->ops_lock
);
224 EXPORT_SYMBOL_GPL(rtc_alarm_irq_enable
);
226 int rtc_update_irq_enable(struct rtc_device
*rtc
, unsigned int enabled
)
228 int err
= mutex_lock_interruptible(&rtc
->ops_lock
);
232 /* make sure we're changing state */
233 if (rtc
->uie_rtctimer
.enabled
== enabled
)
240 __rtc_read_time(rtc
, &tm
);
241 onesec
= ktime_set(1, 0);
242 now
= rtc_tm_to_ktime(tm
);
243 rtc
->uie_rtctimer
.node
.expires
= ktime_add(now
, onesec
);
244 rtc
->uie_rtctimer
.period
= ktime_set(1, 0);
245 err
= rtc_timer_enqueue(rtc
, &rtc
->uie_rtctimer
);
247 rtc_timer_remove(rtc
, &rtc
->uie_rtctimer
);
250 mutex_unlock(&rtc
->ops_lock
);
254 EXPORT_SYMBOL_GPL(rtc_update_irq_enable
);
258 * rtc_handle_legacy_irq - AIE, UIE and PIE event hook
259 * @rtc: pointer to the rtc device
261 * This function is called when an AIE, UIE or PIE mode interrupt
262 * has occured (or been emulated).
264 * Triggers the registered irq_task function callback.
266 static void rtc_handle_legacy_irq(struct rtc_device
*rtc
, int num
, int mode
)
270 /* mark one irq of the appropriate mode */
271 spin_lock_irqsave(&rtc
->irq_lock
, flags
);
272 rtc
->irq_data
= (rtc
->irq_data
+ (num
<< 8)) | (RTC_IRQF
|mode
);
273 spin_unlock_irqrestore(&rtc
->irq_lock
, flags
);
275 /* call the task func */
276 spin_lock_irqsave(&rtc
->irq_task_lock
, flags
);
278 rtc
->irq_task
->func(rtc
->irq_task
->private_data
);
279 spin_unlock_irqrestore(&rtc
->irq_task_lock
, flags
);
281 wake_up_interruptible(&rtc
->irq_queue
);
282 kill_fasync(&rtc
->async_queue
, SIGIO
, POLL_IN
);
287 * rtc_aie_update_irq - AIE mode rtctimer hook
288 * @private: pointer to the rtc_device
290 * This functions is called when the aie_timer expires.
292 void rtc_aie_update_irq(void *private)
294 struct rtc_device
*rtc
= (struct rtc_device
*)private;
295 rtc_handle_legacy_irq(rtc
, 1, RTC_AF
);
300 * rtc_uie_update_irq - UIE mode rtctimer hook
301 * @private: pointer to the rtc_device
303 * This functions is called when the uie_timer expires.
305 void rtc_uie_update_irq(void *private)
307 struct rtc_device
*rtc
= (struct rtc_device
*)private;
308 rtc_handle_legacy_irq(rtc
, 1, RTC_UF
);
313 * rtc_pie_update_irq - PIE mode hrtimer hook
314 * @timer: pointer to the pie mode hrtimer
316 * This function is used to emulate PIE mode interrupts
317 * using an hrtimer. This function is called when the periodic
320 enum hrtimer_restart
rtc_pie_update_irq(struct hrtimer
*timer
)
322 struct rtc_device
*rtc
;
325 rtc
= container_of(timer
, struct rtc_device
, pie_timer
);
327 period
= ktime_set(0, NSEC_PER_SEC
/rtc
->irq_freq
);
328 count
= hrtimer_forward_now(timer
, period
);
330 rtc_handle_legacy_irq(rtc
, count
, RTC_PF
);
332 return HRTIMER_RESTART
;
336 * rtc_update_irq - Triggered when a RTC interrupt occurs.
337 * @rtc: the rtc device
338 * @num: how many irqs are being reported (usually one)
339 * @events: mask of RTC_IRQF with one or more of RTC_PF, RTC_AF, RTC_UF
342 void rtc_update_irq(struct rtc_device
*rtc
,
343 unsigned long num
, unsigned long events
)
345 schedule_work(&rtc
->irqwork
);
347 EXPORT_SYMBOL_GPL(rtc_update_irq
);
349 static int __rtc_match(struct device
*dev
, void *data
)
351 char *name
= (char *)data
;
353 if (strcmp(dev_name(dev
), name
) == 0)
358 struct rtc_device
*rtc_class_open(char *name
)
361 struct rtc_device
*rtc
= NULL
;
363 dev
= class_find_device(rtc_class
, NULL
, name
, __rtc_match
);
365 rtc
= to_rtc_device(dev
);
368 if (!try_module_get(rtc
->owner
)) {
376 EXPORT_SYMBOL_GPL(rtc_class_open
);
378 void rtc_class_close(struct rtc_device
*rtc
)
380 module_put(rtc
->owner
);
381 put_device(&rtc
->dev
);
383 EXPORT_SYMBOL_GPL(rtc_class_close
);
385 int rtc_irq_register(struct rtc_device
*rtc
, struct rtc_task
*task
)
389 if (task
== NULL
|| task
->func
== NULL
)
392 /* Cannot register while the char dev is in use */
393 if (test_and_set_bit_lock(RTC_DEV_BUSY
, &rtc
->flags
))
396 spin_lock_irq(&rtc
->irq_task_lock
);
397 if (rtc
->irq_task
== NULL
) {
398 rtc
->irq_task
= task
;
401 spin_unlock_irq(&rtc
->irq_task_lock
);
403 clear_bit_unlock(RTC_DEV_BUSY
, &rtc
->flags
);
407 EXPORT_SYMBOL_GPL(rtc_irq_register
);
409 void rtc_irq_unregister(struct rtc_device
*rtc
, struct rtc_task
*task
)
411 spin_lock_irq(&rtc
->irq_task_lock
);
412 if (rtc
->irq_task
== task
)
413 rtc
->irq_task
= NULL
;
414 spin_unlock_irq(&rtc
->irq_task_lock
);
416 EXPORT_SYMBOL_GPL(rtc_irq_unregister
);
419 * rtc_irq_set_state - enable/disable 2^N Hz periodic IRQs
420 * @rtc: the rtc device
421 * @task: currently registered with rtc_irq_register()
422 * @enabled: true to enable periodic IRQs
425 * Note that rtc_irq_set_freq() should previously have been used to
426 * specify the desired frequency of periodic IRQ task->func() callbacks.
428 int rtc_irq_set_state(struct rtc_device
*rtc
, struct rtc_task
*task
, int enabled
)
433 spin_lock_irqsave(&rtc
->irq_task_lock
, flags
);
434 if (rtc
->irq_task
!= NULL
&& task
== NULL
)
436 if (rtc
->irq_task
!= task
)
440 ktime_t period
= ktime_set(0, NSEC_PER_SEC
/rtc
->irq_freq
);
441 hrtimer_start(&rtc
->pie_timer
, period
, HRTIMER_MODE_REL
);
443 hrtimer_cancel(&rtc
->pie_timer
);
445 rtc
->pie_enabled
= enabled
;
446 spin_unlock_irqrestore(&rtc
->irq_task_lock
, flags
);
450 EXPORT_SYMBOL_GPL(rtc_irq_set_state
);
453 * rtc_irq_set_freq - set 2^N Hz periodic IRQ frequency for IRQ
454 * @rtc: the rtc device
455 * @task: currently registered with rtc_irq_register()
456 * @freq: positive frequency with which task->func() will be called
459 * Note that rtc_irq_set_state() is used to enable or disable the
462 int rtc_irq_set_freq(struct rtc_device
*rtc
, struct rtc_task
*task
, int freq
)
467 spin_lock_irqsave(&rtc
->irq_task_lock
, flags
);
468 if (rtc
->irq_task
!= NULL
&& task
== NULL
)
470 if (rtc
->irq_task
!= task
)
473 rtc
->irq_freq
= freq
;
474 if (rtc
->pie_enabled
) {
476 hrtimer_cancel(&rtc
->pie_timer
);
477 period
= ktime_set(0, NSEC_PER_SEC
/rtc
->irq_freq
);
478 hrtimer_start(&rtc
->pie_timer
, period
,
482 spin_unlock_irqrestore(&rtc
->irq_task_lock
, flags
);
485 EXPORT_SYMBOL_GPL(rtc_irq_set_freq
);
488 * rtc_timer_enqueue - Adds a rtc_timer to the rtc_device timerqueue
490 * @timer timer being added.
492 * Enqueues a timer onto the rtc devices timerqueue and sets
493 * the next alarm event appropriately.
495 * Sets the enabled bit on the added timer.
497 * Must hold ops_lock for proper serialization of timerqueue
499 static int rtc_timer_enqueue(struct rtc_device
*rtc
, struct rtc_timer
*timer
)
502 timerqueue_add(&rtc
->timerqueue
, &timer
->node
);
503 if (&timer
->node
== timerqueue_getnext(&rtc
->timerqueue
)) {
504 struct rtc_wkalrm alarm
;
506 alarm
.time
= rtc_ktime_to_tm(timer
->node
.expires
);
508 err
= __rtc_set_alarm(rtc
, &alarm
);
510 schedule_work(&rtc
->irqwork
);
512 timerqueue_del(&rtc
->timerqueue
, &timer
->node
);
521 * rtc_timer_remove - Removes a rtc_timer from the rtc_device timerqueue
523 * @timer timer being removed.
525 * Removes a timer onto the rtc devices timerqueue and sets
526 * the next alarm event appropriately.
528 * Clears the enabled bit on the removed timer.
530 * Must hold ops_lock for proper serialization of timerqueue
532 static void rtc_timer_remove(struct rtc_device
*rtc
, struct rtc_timer
*timer
)
534 struct timerqueue_node
*next
= timerqueue_getnext(&rtc
->timerqueue
);
535 timerqueue_del(&rtc
->timerqueue
, &timer
->node
);
537 if (next
== &timer
->node
) {
538 struct rtc_wkalrm alarm
;
540 next
= timerqueue_getnext(&rtc
->timerqueue
);
543 alarm
.time
= rtc_ktime_to_tm(next
->expires
);
545 err
= __rtc_set_alarm(rtc
, &alarm
);
547 schedule_work(&rtc
->irqwork
);
552 * rtc_timer_do_work - Expires rtc timers
554 * @timer timer being removed.
556 * Expires rtc timers. Reprograms next alarm event if needed.
557 * Called via worktask.
559 * Serializes access to timerqueue via ops_lock mutex
561 void rtc_timer_do_work(struct work_struct
*work
)
563 struct rtc_timer
*timer
;
564 struct timerqueue_node
*next
;
568 struct rtc_device
*rtc
=
569 container_of(work
, struct rtc_device
, irqwork
);
571 mutex_lock(&rtc
->ops_lock
);
573 __rtc_read_time(rtc
, &tm
);
574 now
= rtc_tm_to_ktime(tm
);
575 while ((next
= timerqueue_getnext(&rtc
->timerqueue
))) {
576 if (next
->expires
.tv64
> now
.tv64
)
580 timer
= container_of(next
, struct rtc_timer
, node
);
581 timerqueue_del(&rtc
->timerqueue
, &timer
->node
);
583 if (timer
->task
.func
)
584 timer
->task
.func(timer
->task
.private_data
);
586 /* Re-add/fwd periodic timers */
587 if (ktime_to_ns(timer
->period
)) {
588 timer
->node
.expires
= ktime_add(timer
->node
.expires
,
591 timerqueue_add(&rtc
->timerqueue
, &timer
->node
);
597 struct rtc_wkalrm alarm
;
599 alarm
.time
= rtc_ktime_to_tm(next
->expires
);
601 err
= __rtc_set_alarm(rtc
, &alarm
);
606 mutex_unlock(&rtc
->ops_lock
);
610 /* rtc_timer_init - Initializes an rtc_timer
611 * @timer: timer to be intiialized
612 * @f: function pointer to be called when timer fires
613 * @data: private data passed to function pointer
615 * Kernel interface to initializing an rtc_timer.
617 void rtc_timer_init(struct rtc_timer
*timer
, void (*f
)(void* p
), void* data
)
619 timerqueue_init(&timer
->node
);
621 timer
->task
.func
= f
;
622 timer
->task
.private_data
= data
;
625 /* rtc_timer_start - Sets an rtc_timer to fire in the future
626 * @ rtc: rtc device to be used
627 * @ timer: timer being set
628 * @ expires: time at which to expire the timer
629 * @ period: period that the timer will recur
631 * Kernel interface to set an rtc_timer
633 int rtc_timer_start(struct rtc_device
*rtc
, struct rtc_timer
* timer
,
634 ktime_t expires
, ktime_t period
)
637 mutex_lock(&rtc
->ops_lock
);
639 rtc_timer_remove(rtc
, timer
);
641 timer
->node
.expires
= expires
;
642 timer
->period
= period
;
644 ret
= rtc_timer_enqueue(rtc
, timer
);
646 mutex_unlock(&rtc
->ops_lock
);
650 /* rtc_timer_cancel - Stops an rtc_timer
651 * @ rtc: rtc device to be used
652 * @ timer: timer being set
654 * Kernel interface to cancel an rtc_timer
656 int rtc_timer_cancel(struct rtc_device
*rtc
, struct rtc_timer
* timer
)
659 mutex_lock(&rtc
->ops_lock
);
661 rtc_timer_remove(rtc
, timer
);
662 mutex_unlock(&rtc
->ops_lock
);