Merge 2.6.38-rc5 into staging-next
[deliverable/linux.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/log2.h>
17 #include <linux/workqueue.h>
18
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);
21
22 static int __rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm)
23 {
24 int err;
25 if (!rtc->ops)
26 err = -ENODEV;
27 else if (!rtc->ops->read_time)
28 err = -EINVAL;
29 else {
30 memset(tm, 0, sizeof(struct rtc_time));
31 err = rtc->ops->read_time(rtc->dev.parent, tm);
32 }
33 return err;
34 }
35
36 int rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm)
37 {
38 int err;
39
40 err = mutex_lock_interruptible(&rtc->ops_lock);
41 if (err)
42 return err;
43
44 err = __rtc_read_time(rtc, tm);
45 mutex_unlock(&rtc->ops_lock);
46 return err;
47 }
48 EXPORT_SYMBOL_GPL(rtc_read_time);
49
50 int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm)
51 {
52 int err;
53
54 err = rtc_valid_tm(tm);
55 if (err != 0)
56 return err;
57
58 err = mutex_lock_interruptible(&rtc->ops_lock);
59 if (err)
60 return err;
61
62 if (!rtc->ops)
63 err = -ENODEV;
64 else if (rtc->ops->set_time)
65 err = rtc->ops->set_time(rtc->dev.parent, tm);
66 else if (rtc->ops->set_mmss) {
67 unsigned long secs;
68 err = rtc_tm_to_time(tm, &secs);
69 if (err == 0)
70 err = rtc->ops->set_mmss(rtc->dev.parent, secs);
71 } else
72 err = -EINVAL;
73
74 mutex_unlock(&rtc->ops_lock);
75 return err;
76 }
77 EXPORT_SYMBOL_GPL(rtc_set_time);
78
79 int rtc_set_mmss(struct rtc_device *rtc, unsigned long secs)
80 {
81 int err;
82
83 err = mutex_lock_interruptible(&rtc->ops_lock);
84 if (err)
85 return err;
86
87 if (!rtc->ops)
88 err = -ENODEV;
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;
93
94 err = rtc->ops->read_time(rtc->dev.parent, &old);
95 if (err == 0) {
96 rtc_time_to_tm(secs, &new);
97
98 /*
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.
103 */
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,
107 &new);
108 }
109 }
110 else
111 err = -EINVAL;
112
113 mutex_unlock(&rtc->ops_lock);
114
115 return err;
116 }
117 EXPORT_SYMBOL_GPL(rtc_set_mmss);
118
119 int rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
120 {
121 int err;
122
123 err = mutex_lock_interruptible(&rtc->ops_lock);
124 if (err)
125 return err;
126 if (rtc->ops == NULL)
127 err = -ENODEV;
128 else if (!rtc->ops->read_alarm)
129 err = -EINVAL;
130 else {
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);
134 }
135 mutex_unlock(&rtc->ops_lock);
136
137 return err;
138 }
139 EXPORT_SYMBOL_GPL(rtc_read_alarm);
140
141 int __rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
142 {
143 struct rtc_time tm;
144 long now, scheduled;
145 int err;
146
147 err = rtc_valid_tm(&alarm->time);
148 if (err)
149 return err;
150 rtc_tm_to_time(&alarm->time, &scheduled);
151
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)
156 return -ETIME;
157 /*
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.
162 */
163
164 if (!rtc->ops)
165 err = -ENODEV;
166 else if (!rtc->ops->set_alarm)
167 err = -EINVAL;
168 else
169 err = rtc->ops->set_alarm(rtc->dev.parent, alarm);
170
171 return err;
172 }
173
174 int rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
175 {
176 int err;
177
178 err = rtc_valid_tm(&alarm->time);
179 if (err != 0)
180 return err;
181
182 err = mutex_lock_interruptible(&rtc->ops_lock);
183 if (err)
184 return err;
185 if (rtc->aie_timer.enabled) {
186 rtc_timer_remove(rtc, &rtc->aie_timer);
187 }
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);
192 }
193 mutex_unlock(&rtc->ops_lock);
194 return err;
195 }
196 EXPORT_SYMBOL_GPL(rtc_set_alarm);
197
198 int rtc_alarm_irq_enable(struct rtc_device *rtc, unsigned int enabled)
199 {
200 int err = mutex_lock_interruptible(&rtc->ops_lock);
201 if (err)
202 return err;
203
204 if (rtc->aie_timer.enabled != enabled) {
205 if (enabled)
206 err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
207 else
208 rtc_timer_remove(rtc, &rtc->aie_timer);
209 }
210
211 if (err)
212 return err;
213
214 if (!rtc->ops)
215 err = -ENODEV;
216 else if (!rtc->ops->alarm_irq_enable)
217 err = -EINVAL;
218 else
219 err = rtc->ops->alarm_irq_enable(rtc->dev.parent, enabled);
220
221 mutex_unlock(&rtc->ops_lock);
222 return err;
223 }
224 EXPORT_SYMBOL_GPL(rtc_alarm_irq_enable);
225
226 int rtc_update_irq_enable(struct rtc_device *rtc, unsigned int enabled)
227 {
228 int err = mutex_lock_interruptible(&rtc->ops_lock);
229 if (err)
230 return err;
231
232 /* make sure we're changing state */
233 if (rtc->uie_rtctimer.enabled == enabled)
234 goto out;
235
236 if (enabled) {
237 struct rtc_time tm;
238 ktime_t now, onesec;
239
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);
246 } else
247 rtc_timer_remove(rtc, &rtc->uie_rtctimer);
248
249 out:
250 mutex_unlock(&rtc->ops_lock);
251 return err;
252
253 }
254 EXPORT_SYMBOL_GPL(rtc_update_irq_enable);
255
256
257 /**
258 * rtc_handle_legacy_irq - AIE, UIE and PIE event hook
259 * @rtc: pointer to the rtc device
260 *
261 * This function is called when an AIE, UIE or PIE mode interrupt
262 * has occured (or been emulated).
263 *
264 * Triggers the registered irq_task function callback.
265 */
266 static void rtc_handle_legacy_irq(struct rtc_device *rtc, int num, int mode)
267 {
268 unsigned long flags;
269
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);
274
275 /* call the task func */
276 spin_lock_irqsave(&rtc->irq_task_lock, flags);
277 if (rtc->irq_task)
278 rtc->irq_task->func(rtc->irq_task->private_data);
279 spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
280
281 wake_up_interruptible(&rtc->irq_queue);
282 kill_fasync(&rtc->async_queue, SIGIO, POLL_IN);
283 }
284
285
286 /**
287 * rtc_aie_update_irq - AIE mode rtctimer hook
288 * @private: pointer to the rtc_device
289 *
290 * This functions is called when the aie_timer expires.
291 */
292 void rtc_aie_update_irq(void *private)
293 {
294 struct rtc_device *rtc = (struct rtc_device *)private;
295 rtc_handle_legacy_irq(rtc, 1, RTC_AF);
296 }
297
298
299 /**
300 * rtc_uie_update_irq - UIE mode rtctimer hook
301 * @private: pointer to the rtc_device
302 *
303 * This functions is called when the uie_timer expires.
304 */
305 void rtc_uie_update_irq(void *private)
306 {
307 struct rtc_device *rtc = (struct rtc_device *)private;
308 rtc_handle_legacy_irq(rtc, 1, RTC_UF);
309 }
310
311
312 /**
313 * rtc_pie_update_irq - PIE mode hrtimer hook
314 * @timer: pointer to the pie mode hrtimer
315 *
316 * This function is used to emulate PIE mode interrupts
317 * using an hrtimer. This function is called when the periodic
318 * hrtimer expires.
319 */
320 enum hrtimer_restart rtc_pie_update_irq(struct hrtimer *timer)
321 {
322 struct rtc_device *rtc;
323 ktime_t period;
324 int count;
325 rtc = container_of(timer, struct rtc_device, pie_timer);
326
327 period = ktime_set(0, NSEC_PER_SEC/rtc->irq_freq);
328 count = hrtimer_forward_now(timer, period);
329
330 rtc_handle_legacy_irq(rtc, count, RTC_PF);
331
332 return HRTIMER_RESTART;
333 }
334
335 /**
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
340 * Context: any
341 */
342 void rtc_update_irq(struct rtc_device *rtc,
343 unsigned long num, unsigned long events)
344 {
345 schedule_work(&rtc->irqwork);
346 }
347 EXPORT_SYMBOL_GPL(rtc_update_irq);
348
349 static int __rtc_match(struct device *dev, void *data)
350 {
351 char *name = (char *)data;
352
353 if (strcmp(dev_name(dev), name) == 0)
354 return 1;
355 return 0;
356 }
357
358 struct rtc_device *rtc_class_open(char *name)
359 {
360 struct device *dev;
361 struct rtc_device *rtc = NULL;
362
363 dev = class_find_device(rtc_class, NULL, name, __rtc_match);
364 if (dev)
365 rtc = to_rtc_device(dev);
366
367 if (rtc) {
368 if (!try_module_get(rtc->owner)) {
369 put_device(dev);
370 rtc = NULL;
371 }
372 }
373
374 return rtc;
375 }
376 EXPORT_SYMBOL_GPL(rtc_class_open);
377
378 void rtc_class_close(struct rtc_device *rtc)
379 {
380 module_put(rtc->owner);
381 put_device(&rtc->dev);
382 }
383 EXPORT_SYMBOL_GPL(rtc_class_close);
384
385 int rtc_irq_register(struct rtc_device *rtc, struct rtc_task *task)
386 {
387 int retval = -EBUSY;
388
389 if (task == NULL || task->func == NULL)
390 return -EINVAL;
391
392 /* Cannot register while the char dev is in use */
393 if (test_and_set_bit_lock(RTC_DEV_BUSY, &rtc->flags))
394 return -EBUSY;
395
396 spin_lock_irq(&rtc->irq_task_lock);
397 if (rtc->irq_task == NULL) {
398 rtc->irq_task = task;
399 retval = 0;
400 }
401 spin_unlock_irq(&rtc->irq_task_lock);
402
403 clear_bit_unlock(RTC_DEV_BUSY, &rtc->flags);
404
405 return retval;
406 }
407 EXPORT_SYMBOL_GPL(rtc_irq_register);
408
409 void rtc_irq_unregister(struct rtc_device *rtc, struct rtc_task *task)
410 {
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);
415 }
416 EXPORT_SYMBOL_GPL(rtc_irq_unregister);
417
418 /**
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
423 * Context: any
424 *
425 * Note that rtc_irq_set_freq() should previously have been used to
426 * specify the desired frequency of periodic IRQ task->func() callbacks.
427 */
428 int rtc_irq_set_state(struct rtc_device *rtc, struct rtc_task *task, int enabled)
429 {
430 int err = 0;
431 unsigned long flags;
432
433 spin_lock_irqsave(&rtc->irq_task_lock, flags);
434 if (rtc->irq_task != NULL && task == NULL)
435 err = -EBUSY;
436 if (rtc->irq_task != task)
437 err = -EACCES;
438
439 if (enabled) {
440 ktime_t period = ktime_set(0, NSEC_PER_SEC/rtc->irq_freq);
441 hrtimer_start(&rtc->pie_timer, period, HRTIMER_MODE_REL);
442 } else {
443 hrtimer_cancel(&rtc->pie_timer);
444 }
445 rtc->pie_enabled = enabled;
446 spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
447
448 return err;
449 }
450 EXPORT_SYMBOL_GPL(rtc_irq_set_state);
451
452 /**
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
457 * Context: any
458 *
459 * Note that rtc_irq_set_state() is used to enable or disable the
460 * periodic IRQs.
461 */
462 int rtc_irq_set_freq(struct rtc_device *rtc, struct rtc_task *task, int freq)
463 {
464 int err = 0;
465 unsigned long flags;
466
467 if (freq <= 0)
468 return -EINVAL;
469
470 spin_lock_irqsave(&rtc->irq_task_lock, flags);
471 if (rtc->irq_task != NULL && task == NULL)
472 err = -EBUSY;
473 if (rtc->irq_task != task)
474 err = -EACCES;
475 if (err == 0) {
476 rtc->irq_freq = freq;
477 if (rtc->pie_enabled) {
478 ktime_t period;
479 hrtimer_cancel(&rtc->pie_timer);
480 period = ktime_set(0, NSEC_PER_SEC/rtc->irq_freq);
481 hrtimer_start(&rtc->pie_timer, period,
482 HRTIMER_MODE_REL);
483 }
484 }
485 spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
486 return err;
487 }
488 EXPORT_SYMBOL_GPL(rtc_irq_set_freq);
489
490 /**
491 * rtc_timer_enqueue - Adds a rtc_timer to the rtc_device timerqueue
492 * @rtc rtc device
493 * @timer timer being added.
494 *
495 * Enqueues a timer onto the rtc devices timerqueue and sets
496 * the next alarm event appropriately.
497 *
498 * Sets the enabled bit on the added timer.
499 *
500 * Must hold ops_lock for proper serialization of timerqueue
501 */
502 static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer)
503 {
504 timer->enabled = 1;
505 timerqueue_add(&rtc->timerqueue, &timer->node);
506 if (&timer->node == timerqueue_getnext(&rtc->timerqueue)) {
507 struct rtc_wkalrm alarm;
508 int err;
509 alarm.time = rtc_ktime_to_tm(timer->node.expires);
510 alarm.enabled = 1;
511 err = __rtc_set_alarm(rtc, &alarm);
512 if (err == -ETIME)
513 schedule_work(&rtc->irqwork);
514 else if (err) {
515 timerqueue_del(&rtc->timerqueue, &timer->node);
516 timer->enabled = 0;
517 return err;
518 }
519 }
520 return 0;
521 }
522
523 /**
524 * rtc_timer_remove - Removes a rtc_timer from the rtc_device timerqueue
525 * @rtc rtc device
526 * @timer timer being removed.
527 *
528 * Removes a timer onto the rtc devices timerqueue and sets
529 * the next alarm event appropriately.
530 *
531 * Clears the enabled bit on the removed timer.
532 *
533 * Must hold ops_lock for proper serialization of timerqueue
534 */
535 static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer)
536 {
537 struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue);
538 timerqueue_del(&rtc->timerqueue, &timer->node);
539 timer->enabled = 0;
540 if (next == &timer->node) {
541 struct rtc_wkalrm alarm;
542 int err;
543 next = timerqueue_getnext(&rtc->timerqueue);
544 if (!next)
545 return;
546 alarm.time = rtc_ktime_to_tm(next->expires);
547 alarm.enabled = 1;
548 err = __rtc_set_alarm(rtc, &alarm);
549 if (err == -ETIME)
550 schedule_work(&rtc->irqwork);
551 }
552 }
553
554 /**
555 * rtc_timer_do_work - Expires rtc timers
556 * @rtc rtc device
557 * @timer timer being removed.
558 *
559 * Expires rtc timers. Reprograms next alarm event if needed.
560 * Called via worktask.
561 *
562 * Serializes access to timerqueue via ops_lock mutex
563 */
564 void rtc_timer_do_work(struct work_struct *work)
565 {
566 struct rtc_timer *timer;
567 struct timerqueue_node *next;
568 ktime_t now;
569 struct rtc_time tm;
570
571 struct rtc_device *rtc =
572 container_of(work, struct rtc_device, irqwork);
573
574 mutex_lock(&rtc->ops_lock);
575 again:
576 __rtc_read_time(rtc, &tm);
577 now = rtc_tm_to_ktime(tm);
578 while ((next = timerqueue_getnext(&rtc->timerqueue))) {
579 if (next->expires.tv64 > now.tv64)
580 break;
581
582 /* expire timer */
583 timer = container_of(next, struct rtc_timer, node);
584 timerqueue_del(&rtc->timerqueue, &timer->node);
585 timer->enabled = 0;
586 if (timer->task.func)
587 timer->task.func(timer->task.private_data);
588
589 /* Re-add/fwd periodic timers */
590 if (ktime_to_ns(timer->period)) {
591 timer->node.expires = ktime_add(timer->node.expires,
592 timer->period);
593 timer->enabled = 1;
594 timerqueue_add(&rtc->timerqueue, &timer->node);
595 }
596 }
597
598 /* Set next alarm */
599 if (next) {
600 struct rtc_wkalrm alarm;
601 int err;
602 alarm.time = rtc_ktime_to_tm(next->expires);
603 alarm.enabled = 1;
604 err = __rtc_set_alarm(rtc, &alarm);
605 if (err == -ETIME)
606 goto again;
607 }
608
609 mutex_unlock(&rtc->ops_lock);
610 }
611
612
613 /* rtc_timer_init - Initializes an rtc_timer
614 * @timer: timer to be intiialized
615 * @f: function pointer to be called when timer fires
616 * @data: private data passed to function pointer
617 *
618 * Kernel interface to initializing an rtc_timer.
619 */
620 void rtc_timer_init(struct rtc_timer *timer, void (*f)(void* p), void* data)
621 {
622 timerqueue_init(&timer->node);
623 timer->enabled = 0;
624 timer->task.func = f;
625 timer->task.private_data = data;
626 }
627
628 /* rtc_timer_start - Sets an rtc_timer to fire in the future
629 * @ rtc: rtc device to be used
630 * @ timer: timer being set
631 * @ expires: time at which to expire the timer
632 * @ period: period that the timer will recur
633 *
634 * Kernel interface to set an rtc_timer
635 */
636 int rtc_timer_start(struct rtc_device *rtc, struct rtc_timer* timer,
637 ktime_t expires, ktime_t period)
638 {
639 int ret = 0;
640 mutex_lock(&rtc->ops_lock);
641 if (timer->enabled)
642 rtc_timer_remove(rtc, timer);
643
644 timer->node.expires = expires;
645 timer->period = period;
646
647 ret = rtc_timer_enqueue(rtc, timer);
648
649 mutex_unlock(&rtc->ops_lock);
650 return ret;
651 }
652
653 /* rtc_timer_cancel - Stops an rtc_timer
654 * @ rtc: rtc device to be used
655 * @ timer: timer being set
656 *
657 * Kernel interface to cancel an rtc_timer
658 */
659 int rtc_timer_cancel(struct rtc_device *rtc, struct rtc_timer* timer)
660 {
661 int ret = 0;
662 mutex_lock(&rtc->ops_lock);
663 if (timer->enabled)
664 rtc_timer_remove(rtc, timer);
665 mutex_unlock(&rtc->ops_lock);
666 return ret;
667 }
668
669
This page took 0.045676 seconds and 5 git commands to generate.