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_read_time(struct rtc_device
*rtc
, struct rtc_time
*tm
)
24 else if (!rtc
->ops
->read_time
)
27 memset(tm
, 0, sizeof(struct rtc_time
));
28 err
= rtc
->ops
->read_time(rtc
->dev
.parent
, tm
);
33 int rtc_read_time(struct rtc_device
*rtc
, struct rtc_time
*tm
)
37 err
= mutex_lock_interruptible(&rtc
->ops_lock
);
41 err
= __rtc_read_time(rtc
, tm
);
42 mutex_unlock(&rtc
->ops_lock
);
45 EXPORT_SYMBOL_GPL(rtc_read_time
);
47 int rtc_set_time(struct rtc_device
*rtc
, struct rtc_time
*tm
)
51 err
= rtc_valid_tm(tm
);
55 err
= mutex_lock_interruptible(&rtc
->ops_lock
);
61 else if (rtc
->ops
->set_time
)
62 err
= rtc
->ops
->set_time(rtc
->dev
.parent
, tm
);
63 else if (rtc
->ops
->set_mmss
) {
65 err
= rtc_tm_to_time(tm
, &secs
);
67 err
= rtc
->ops
->set_mmss(rtc
->dev
.parent
, secs
);
71 mutex_unlock(&rtc
->ops_lock
);
74 EXPORT_SYMBOL_GPL(rtc_set_time
);
76 int rtc_set_mmss(struct rtc_device
*rtc
, unsigned long secs
)
80 err
= mutex_lock_interruptible(&rtc
->ops_lock
);
86 else if (rtc
->ops
->set_mmss
)
87 err
= rtc
->ops
->set_mmss(rtc
->dev
.parent
, secs
);
88 else if (rtc
->ops
->read_time
&& rtc
->ops
->set_time
) {
89 struct rtc_time
new, old
;
91 err
= rtc
->ops
->read_time(rtc
->dev
.parent
, &old
);
93 rtc_time_to_tm(secs
, &new);
96 * avoid writing when we're going to change the day of
97 * the month. We will retry in the next minute. This
98 * basically means that if the RTC must not drift
99 * by more than 1 minute in 11 minutes.
101 if (!((old
.tm_hour
== 23 && old
.tm_min
== 59) ||
102 (new.tm_hour
== 23 && new.tm_min
== 59)))
103 err
= rtc
->ops
->set_time(rtc
->dev
.parent
,
110 mutex_unlock(&rtc
->ops_lock
);
114 EXPORT_SYMBOL_GPL(rtc_set_mmss
);
116 int rtc_read_alarm(struct rtc_device
*rtc
, struct rtc_wkalrm
*alarm
)
120 err
= mutex_lock_interruptible(&rtc
->ops_lock
);
123 alarm
->enabled
= rtc
->aie_timer
.enabled
;
125 alarm
->time
= rtc_ktime_to_tm(rtc
->aie_timer
.node
.expires
);
126 mutex_unlock(&rtc
->ops_lock
);
130 EXPORT_SYMBOL_GPL(rtc_read_alarm
);
132 int __rtc_set_alarm(struct rtc_device
*rtc
, struct rtc_wkalrm
*alarm
)
138 err
= rtc_valid_tm(&alarm
->time
);
141 rtc_tm_to_time(&alarm
->time
, &scheduled
);
143 /* Make sure we're not setting alarms in the past */
144 err
= __rtc_read_time(rtc
, &tm
);
145 rtc_tm_to_time(&tm
, &now
);
146 if (scheduled
<= now
)
149 * XXX - We just checked to make sure the alarm time is not
150 * in the past, but there is still a race window where if
151 * the is alarm set for the next second and the second ticks
152 * over right here, before we set the alarm.
157 else if (!rtc
->ops
->set_alarm
)
160 err
= rtc
->ops
->set_alarm(rtc
->dev
.parent
, alarm
);
165 int rtc_set_alarm(struct rtc_device
*rtc
, struct rtc_wkalrm
*alarm
)
169 err
= rtc_valid_tm(&alarm
->time
);
173 err
= mutex_lock_interruptible(&rtc
->ops_lock
);
176 if (rtc
->aie_timer
.enabled
) {
177 rtc_timer_remove(rtc
, &rtc
->aie_timer
);
178 rtc
->aie_timer
.enabled
= 0;
180 rtc
->aie_timer
.node
.expires
= rtc_tm_to_ktime(alarm
->time
);
181 rtc
->aie_timer
.period
= ktime_set(0, 0);
182 if (alarm
->enabled
) {
183 rtc
->aie_timer
.enabled
= 1;
184 rtc_timer_enqueue(rtc
, &rtc
->aie_timer
);
186 mutex_unlock(&rtc
->ops_lock
);
189 EXPORT_SYMBOL_GPL(rtc_set_alarm
);
191 int rtc_alarm_irq_enable(struct rtc_device
*rtc
, unsigned int enabled
)
193 int err
= mutex_lock_interruptible(&rtc
->ops_lock
);
197 if (rtc
->aie_timer
.enabled
!= enabled
) {
199 rtc
->aie_timer
.enabled
= 1;
200 rtc_timer_enqueue(rtc
, &rtc
->aie_timer
);
202 rtc_timer_remove(rtc
, &rtc
->aie_timer
);
203 rtc
->aie_timer
.enabled
= 0;
209 else if (!rtc
->ops
->alarm_irq_enable
)
212 err
= rtc
->ops
->alarm_irq_enable(rtc
->dev
.parent
, enabled
);
214 mutex_unlock(&rtc
->ops_lock
);
217 EXPORT_SYMBOL_GPL(rtc_alarm_irq_enable
);
219 int rtc_update_irq_enable(struct rtc_device
*rtc
, unsigned int enabled
)
221 int err
= mutex_lock_interruptible(&rtc
->ops_lock
);
225 /* make sure we're changing state */
226 if (rtc
->uie_rtctimer
.enabled
== enabled
)
233 __rtc_read_time(rtc
, &tm
);
234 onesec
= ktime_set(1, 0);
235 now
= rtc_tm_to_ktime(tm
);
236 rtc
->uie_rtctimer
.node
.expires
= ktime_add(now
, onesec
);
237 rtc
->uie_rtctimer
.period
= ktime_set(1, 0);
238 rtc
->uie_rtctimer
.enabled
= 1;
239 rtc_timer_enqueue(rtc
, &rtc
->uie_rtctimer
);
241 rtc_timer_remove(rtc
, &rtc
->uie_rtctimer
);
242 rtc
->uie_rtctimer
.enabled
= 0;
246 mutex_unlock(&rtc
->ops_lock
);
250 EXPORT_SYMBOL_GPL(rtc_update_irq_enable
);
254 * rtc_handle_legacy_irq - AIE, UIE and PIE event hook
255 * @rtc: pointer to the rtc device
257 * This function is called when an AIE, UIE or PIE mode interrupt
258 * has occured (or been emulated).
260 * Triggers the registered irq_task function callback.
262 static void rtc_handle_legacy_irq(struct rtc_device
*rtc
, int num
, int mode
)
266 /* mark one irq of the appropriate mode */
267 spin_lock_irqsave(&rtc
->irq_lock
, flags
);
268 rtc
->irq_data
= (rtc
->irq_data
+ (num
<< 8)) | (RTC_IRQF
|mode
);
269 spin_unlock_irqrestore(&rtc
->irq_lock
, flags
);
271 /* call the task func */
272 spin_lock_irqsave(&rtc
->irq_task_lock
, flags
);
274 rtc
->irq_task
->func(rtc
->irq_task
->private_data
);
275 spin_unlock_irqrestore(&rtc
->irq_task_lock
, flags
);
277 wake_up_interruptible(&rtc
->irq_queue
);
278 kill_fasync(&rtc
->async_queue
, SIGIO
, POLL_IN
);
283 * rtc_aie_update_irq - AIE mode rtctimer hook
284 * @private: pointer to the rtc_device
286 * This functions is called when the aie_timer expires.
288 void rtc_aie_update_irq(void *private)
290 struct rtc_device
*rtc
= (struct rtc_device
*)private;
291 rtc_handle_legacy_irq(rtc
, 1, RTC_AF
);
296 * rtc_uie_update_irq - UIE mode rtctimer hook
297 * @private: pointer to the rtc_device
299 * This functions is called when the uie_timer expires.
301 void rtc_uie_update_irq(void *private)
303 struct rtc_device
*rtc
= (struct rtc_device
*)private;
304 rtc_handle_legacy_irq(rtc
, 1, RTC_UF
);
309 * rtc_pie_update_irq - PIE mode hrtimer hook
310 * @timer: pointer to the pie mode hrtimer
312 * This function is used to emulate PIE mode interrupts
313 * using an hrtimer. This function is called when the periodic
316 enum hrtimer_restart
rtc_pie_update_irq(struct hrtimer
*timer
)
318 struct rtc_device
*rtc
;
321 rtc
= container_of(timer
, struct rtc_device
, pie_timer
);
323 period
= ktime_set(0, NSEC_PER_SEC
/rtc
->irq_freq
);
324 count
= hrtimer_forward_now(timer
, period
);
326 rtc_handle_legacy_irq(rtc
, count
, RTC_PF
);
328 return HRTIMER_RESTART
;
332 * rtc_update_irq - Triggered when a RTC interrupt occurs.
333 * @rtc: the rtc device
334 * @num: how many irqs are being reported (usually one)
335 * @events: mask of RTC_IRQF with one or more of RTC_PF, RTC_AF, RTC_UF
338 void rtc_update_irq(struct rtc_device
*rtc
,
339 unsigned long num
, unsigned long events
)
341 schedule_work(&rtc
->irqwork
);
343 EXPORT_SYMBOL_GPL(rtc_update_irq
);
345 static int __rtc_match(struct device
*dev
, void *data
)
347 char *name
= (char *)data
;
349 if (strcmp(dev_name(dev
), name
) == 0)
354 struct rtc_device
*rtc_class_open(char *name
)
357 struct rtc_device
*rtc
= NULL
;
359 dev
= class_find_device(rtc_class
, NULL
, name
, __rtc_match
);
361 rtc
= to_rtc_device(dev
);
364 if (!try_module_get(rtc
->owner
)) {
372 EXPORT_SYMBOL_GPL(rtc_class_open
);
374 void rtc_class_close(struct rtc_device
*rtc
)
376 module_put(rtc
->owner
);
377 put_device(&rtc
->dev
);
379 EXPORT_SYMBOL_GPL(rtc_class_close
);
381 int rtc_irq_register(struct rtc_device
*rtc
, struct rtc_task
*task
)
385 if (task
== NULL
|| task
->func
== NULL
)
388 /* Cannot register while the char dev is in use */
389 if (test_and_set_bit_lock(RTC_DEV_BUSY
, &rtc
->flags
))
392 spin_lock_irq(&rtc
->irq_task_lock
);
393 if (rtc
->irq_task
== NULL
) {
394 rtc
->irq_task
= task
;
397 spin_unlock_irq(&rtc
->irq_task_lock
);
399 clear_bit_unlock(RTC_DEV_BUSY
, &rtc
->flags
);
403 EXPORT_SYMBOL_GPL(rtc_irq_register
);
405 void rtc_irq_unregister(struct rtc_device
*rtc
, struct rtc_task
*task
)
407 spin_lock_irq(&rtc
->irq_task_lock
);
408 if (rtc
->irq_task
== task
)
409 rtc
->irq_task
= NULL
;
410 spin_unlock_irq(&rtc
->irq_task_lock
);
412 EXPORT_SYMBOL_GPL(rtc_irq_unregister
);
415 * rtc_irq_set_state - enable/disable 2^N Hz periodic IRQs
416 * @rtc: the rtc device
417 * @task: currently registered with rtc_irq_register()
418 * @enabled: true to enable periodic IRQs
421 * Note that rtc_irq_set_freq() should previously have been used to
422 * specify the desired frequency of periodic IRQ task->func() callbacks.
424 int rtc_irq_set_state(struct rtc_device
*rtc
, struct rtc_task
*task
, int enabled
)
429 spin_lock_irqsave(&rtc
->irq_task_lock
, flags
);
430 if (rtc
->irq_task
!= NULL
&& task
== NULL
)
432 if (rtc
->irq_task
!= task
)
436 ktime_t period
= ktime_set(0, NSEC_PER_SEC
/rtc
->irq_freq
);
437 hrtimer_start(&rtc
->pie_timer
, period
, HRTIMER_MODE_REL
);
439 hrtimer_cancel(&rtc
->pie_timer
);
441 rtc
->pie_enabled
= enabled
;
442 spin_unlock_irqrestore(&rtc
->irq_task_lock
, flags
);
446 EXPORT_SYMBOL_GPL(rtc_irq_set_state
);
449 * rtc_irq_set_freq - set 2^N Hz periodic IRQ frequency for IRQ
450 * @rtc: the rtc device
451 * @task: currently registered with rtc_irq_register()
452 * @freq: positive frequency with which task->func() will be called
455 * Note that rtc_irq_set_state() is used to enable or disable the
458 int rtc_irq_set_freq(struct rtc_device
*rtc
, struct rtc_task
*task
, int freq
)
463 spin_lock_irqsave(&rtc
->irq_task_lock
, flags
);
464 if (rtc
->irq_task
!= NULL
&& task
== NULL
)
466 if (rtc
->irq_task
!= task
)
469 rtc
->irq_freq
= freq
;
470 if (rtc
->pie_enabled
) {
472 hrtimer_cancel(&rtc
->pie_timer
);
473 period
= ktime_set(0, NSEC_PER_SEC
/rtc
->irq_freq
);
474 hrtimer_start(&rtc
->pie_timer
, period
,
478 spin_unlock_irqrestore(&rtc
->irq_task_lock
, flags
);
481 EXPORT_SYMBOL_GPL(rtc_irq_set_freq
);
484 * rtc_timer_enqueue - Adds a rtc_timer to the rtc_device timerqueue
486 * @timer timer being added.
488 * Enqueues a timer onto the rtc devices timerqueue and sets
489 * the next alarm event appropriately.
491 * Must hold ops_lock for proper serialization of timerqueue
493 void rtc_timer_enqueue(struct rtc_device
*rtc
, struct rtc_timer
*timer
)
495 timerqueue_add(&rtc
->timerqueue
, &timer
->node
);
496 if (&timer
->node
== timerqueue_getnext(&rtc
->timerqueue
)) {
497 struct rtc_wkalrm alarm
;
499 alarm
.time
= rtc_ktime_to_tm(timer
->node
.expires
);
501 err
= __rtc_set_alarm(rtc
, &alarm
);
503 schedule_work(&rtc
->irqwork
);
508 * rtc_timer_remove - Removes a rtc_timer from the rtc_device timerqueue
510 * @timer timer being removed.
512 * Removes a timer onto the rtc devices timerqueue and sets
513 * the next alarm event appropriately.
515 * Must hold ops_lock for proper serialization of timerqueue
517 void rtc_timer_remove(struct rtc_device
*rtc
, struct rtc_timer
*timer
)
519 struct timerqueue_node
*next
= timerqueue_getnext(&rtc
->timerqueue
);
520 timerqueue_del(&rtc
->timerqueue
, &timer
->node
);
522 if (next
== &timer
->node
) {
523 struct rtc_wkalrm alarm
;
525 next
= timerqueue_getnext(&rtc
->timerqueue
);
528 alarm
.time
= rtc_ktime_to_tm(next
->expires
);
530 err
= __rtc_set_alarm(rtc
, &alarm
);
532 schedule_work(&rtc
->irqwork
);
537 * rtc_timer_do_work - Expires rtc timers
539 * @timer timer being removed.
541 * Expires rtc timers. Reprograms next alarm event if needed.
542 * Called via worktask.
544 * Serializes access to timerqueue via ops_lock mutex
546 void rtc_timer_do_work(struct work_struct
*work
)
548 struct rtc_timer
*timer
;
549 struct timerqueue_node
*next
;
553 struct rtc_device
*rtc
=
554 container_of(work
, struct rtc_device
, irqwork
);
556 mutex_lock(&rtc
->ops_lock
);
558 __rtc_read_time(rtc
, &tm
);
559 now
= rtc_tm_to_ktime(tm
);
560 while ((next
= timerqueue_getnext(&rtc
->timerqueue
))) {
561 if (next
->expires
.tv64
> now
.tv64
)
565 timer
= container_of(next
, struct rtc_timer
, node
);
566 timerqueue_del(&rtc
->timerqueue
, &timer
->node
);
568 if (timer
->task
.func
)
569 timer
->task
.func(timer
->task
.private_data
);
571 /* Re-add/fwd periodic timers */
572 if (ktime_to_ns(timer
->period
)) {
573 timer
->node
.expires
= ktime_add(timer
->node
.expires
,
576 timerqueue_add(&rtc
->timerqueue
, &timer
->node
);
582 struct rtc_wkalrm alarm
;
584 alarm
.time
= rtc_ktime_to_tm(next
->expires
);
586 err
= __rtc_set_alarm(rtc
, &alarm
);
591 mutex_unlock(&rtc
->ops_lock
);
595 /* rtc_timer_init - Initializes an rtc_timer
596 * @timer: timer to be intiialized
597 * @f: function pointer to be called when timer fires
598 * @data: private data passed to function pointer
600 * Kernel interface to initializing an rtc_timer.
602 void rtc_timer_init(struct rtc_timer
*timer
, void (*f
)(void* p
), void* data
)
604 timerqueue_init(&timer
->node
);
606 timer
->task
.func
= f
;
607 timer
->task
.private_data
= data
;
610 /* rtc_timer_start - Sets an rtc_timer to fire in the future
611 * @ rtc: rtc device to be used
612 * @ timer: timer being set
613 * @ expires: time at which to expire the timer
614 * @ period: period that the timer will recur
616 * Kernel interface to set an rtc_timer
618 int rtc_timer_start(struct rtc_device
*rtc
, struct rtc_timer
* timer
,
619 ktime_t expires
, ktime_t period
)
622 mutex_lock(&rtc
->ops_lock
);
624 rtc_timer_remove(rtc
, timer
);
626 timer
->node
.expires
= expires
;
627 timer
->period
= period
;
630 rtc_timer_enqueue(rtc
, timer
);
632 mutex_unlock(&rtc
->ops_lock
);
636 /* rtc_timer_cancel - Stops an rtc_timer
637 * @ rtc: rtc device to be used
638 * @ timer: timer being set
640 * Kernel interface to cancel an rtc_timer
642 int rtc_timer_cancel(struct rtc_device
*rtc
, struct rtc_timer
* timer
)
645 mutex_lock(&rtc
->ops_lock
);
647 rtc_timer_remove(rtc
, timer
);
649 mutex_unlock(&rtc
->ops_lock
);
This page took 0.066491 seconds and 6 git commands to generate.