lpfc: Initiator sends wrong BBCredit value for either FLOGI or FLOGI_ACC
[linux-2.6/btrfs-unstable.git] / drivers / rtc / interface.c
blob37215cf983e92926653d1f3206aa8c4c8842a55a
1 /*
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/module.h>
17 #include <linux/log2.h>
18 #include <linux/workqueue.h>
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);
23 static int __rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm)
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_err(&rtc->dev, "read_time: fail to read\n");
35 return err;
38 err = rtc_valid_tm(tm);
39 if (err < 0)
40 dev_err(&rtc->dev, "read_time: rtc_time isn't valid\n");
42 return err;
45 int rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm)
47 int err;
49 err = mutex_lock_interruptible(&rtc->ops_lock);
50 if (err)
51 return err;
53 err = __rtc_read_time(rtc, tm);
54 mutex_unlock(&rtc->ops_lock);
55 return err;
57 EXPORT_SYMBOL_GPL(rtc_read_time);
59 int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm)
61 int err;
63 err = rtc_valid_tm(tm);
64 if (err != 0)
65 return err;
67 err = mutex_lock_interruptible(&rtc->ops_lock);
68 if (err)
69 return err;
71 if (!rtc->ops)
72 err = -ENODEV;
73 else if (rtc->ops->set_time)
74 err = rtc->ops->set_time(rtc->dev.parent, tm);
75 else if (rtc->ops->set_mmss) {
76 time64_t secs64 = rtc_tm_to_time64(tm);
77 err = rtc->ops->set_mmss(rtc->dev.parent, secs64);
78 } else
79 err = -EINVAL;
81 pm_stay_awake(rtc->dev.parent);
82 mutex_unlock(&rtc->ops_lock);
83 /* A timer might have just expired */
84 schedule_work(&rtc->irqwork);
85 return err;
87 EXPORT_SYMBOL_GPL(rtc_set_time);
89 int rtc_set_mmss(struct rtc_device *rtc, unsigned long secs)
91 int err;
93 err = mutex_lock_interruptible(&rtc->ops_lock);
94 if (err)
95 return err;
97 if (!rtc->ops)
98 err = -ENODEV;
99 else if (rtc->ops->set_mmss)
100 err = rtc->ops->set_mmss(rtc->dev.parent, secs);
101 else if (rtc->ops->read_time && rtc->ops->set_time) {
102 struct rtc_time new, old;
104 err = rtc->ops->read_time(rtc->dev.parent, &old);
105 if (err == 0) {
106 rtc_time64_to_tm(secs, &new);
109 * avoid writing when we're going to change the day of
110 * the month. We will retry in the next minute. This
111 * basically means that if the RTC must not drift
112 * by more than 1 minute in 11 minutes.
114 if (!((old.tm_hour == 23 && old.tm_min == 59) ||
115 (new.tm_hour == 23 && new.tm_min == 59)))
116 err = rtc->ops->set_time(rtc->dev.parent,
117 &new);
119 } else {
120 err = -EINVAL;
123 pm_stay_awake(rtc->dev.parent);
124 mutex_unlock(&rtc->ops_lock);
125 /* A timer might have just expired */
126 schedule_work(&rtc->irqwork);
128 return err;
130 EXPORT_SYMBOL_GPL(rtc_set_mmss);
132 static int rtc_read_alarm_internal(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
134 int err;
136 err = mutex_lock_interruptible(&rtc->ops_lock);
137 if (err)
138 return err;
140 if (rtc->ops == NULL)
141 err = -ENODEV;
142 else if (!rtc->ops->read_alarm)
143 err = -EINVAL;
144 else {
145 memset(alarm, 0, sizeof(struct rtc_wkalrm));
146 err = rtc->ops->read_alarm(rtc->dev.parent, alarm);
149 mutex_unlock(&rtc->ops_lock);
150 return err;
153 int __rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
155 int err;
156 struct rtc_time before, now;
157 int first_time = 1;
158 time64_t t_now, t_alm;
159 enum { none, day, month, year } missing = none;
160 unsigned days;
162 /* The lower level RTC driver may return -1 in some fields,
163 * creating invalid alarm->time values, for reasons like:
165 * - The hardware may not be capable of filling them in;
166 * many alarms match only on time-of-day fields, not
167 * day/month/year calendar data.
169 * - Some hardware uses illegal values as "wildcard" match
170 * values, which non-Linux firmware (like a BIOS) may try
171 * to set up as e.g. "alarm 15 minutes after each hour".
172 * Linux uses only oneshot alarms.
174 * When we see that here, we deal with it by using values from
175 * a current RTC timestamp for any missing (-1) values. The
176 * RTC driver prevents "periodic alarm" modes.
178 * But this can be racey, because some fields of the RTC timestamp
179 * may have wrapped in the interval since we read the RTC alarm,
180 * which would lead to us inserting inconsistent values in place
181 * of the -1 fields.
183 * Reading the alarm and timestamp in the reverse sequence
184 * would have the same race condition, and not solve the issue.
186 * So, we must first read the RTC timestamp,
187 * then read the RTC alarm value,
188 * and then read a second RTC timestamp.
190 * If any fields of the second timestamp have changed
191 * when compared with the first timestamp, then we know
192 * our timestamp may be inconsistent with that used by
193 * the low-level rtc_read_alarm_internal() function.
195 * So, when the two timestamps disagree, we just loop and do
196 * the process again to get a fully consistent set of values.
198 * This could all instead be done in the lower level driver,
199 * but since more than one lower level RTC implementation needs it,
200 * then it's probably best best to do it here instead of there..
203 /* Get the "before" timestamp */
204 err = rtc_read_time(rtc, &before);
205 if (err < 0)
206 return err;
207 do {
208 if (!first_time)
209 memcpy(&before, &now, sizeof(struct rtc_time));
210 first_time = 0;
212 /* get the RTC alarm values, which may be incomplete */
213 err = rtc_read_alarm_internal(rtc, alarm);
214 if (err)
215 return err;
217 /* full-function RTCs won't have such missing fields */
218 if (rtc_valid_tm(&alarm->time) == 0)
219 return 0;
221 /* get the "after" timestamp, to detect wrapped fields */
222 err = rtc_read_time(rtc, &now);
223 if (err < 0)
224 return err;
226 /* note that tm_sec is a "don't care" value here: */
227 } while ( before.tm_min != now.tm_min
228 || before.tm_hour != now.tm_hour
229 || before.tm_mon != now.tm_mon
230 || before.tm_year != now.tm_year);
232 /* Fill in the missing alarm fields using the timestamp; we
233 * know there's at least one since alarm->time is invalid.
235 if (alarm->time.tm_sec == -1)
236 alarm->time.tm_sec = now.tm_sec;
237 if (alarm->time.tm_min == -1)
238 alarm->time.tm_min = now.tm_min;
239 if (alarm->time.tm_hour == -1)
240 alarm->time.tm_hour = now.tm_hour;
242 /* For simplicity, only support date rollover for now */
243 if (alarm->time.tm_mday < 1 || alarm->time.tm_mday > 31) {
244 alarm->time.tm_mday = now.tm_mday;
245 missing = day;
247 if ((unsigned)alarm->time.tm_mon >= 12) {
248 alarm->time.tm_mon = now.tm_mon;
249 if (missing == none)
250 missing = month;
252 if (alarm->time.tm_year == -1) {
253 alarm->time.tm_year = now.tm_year;
254 if (missing == none)
255 missing = year;
258 /* with luck, no rollover is needed */
259 t_now = rtc_tm_to_time64(&now);
260 t_alm = rtc_tm_to_time64(&alarm->time);
261 if (t_now < t_alm)
262 goto done;
264 switch (missing) {
266 /* 24 hour rollover ... if it's now 10am Monday, an alarm that
267 * that will trigger at 5am will do so at 5am Tuesday, which
268 * could also be in the next month or year. This is a common
269 * case, especially for PCs.
271 case day:
272 dev_dbg(&rtc->dev, "alarm rollover: %s\n", "day");
273 t_alm += 24 * 60 * 60;
274 rtc_time64_to_tm(t_alm, &alarm->time);
275 break;
277 /* Month rollover ... if it's the 31th, an alarm on the 3rd will
278 * be next month. An alarm matching on the 30th, 29th, or 28th
279 * may end up in the month after that! Many newer PCs support
280 * this type of alarm.
282 case month:
283 dev_dbg(&rtc->dev, "alarm rollover: %s\n", "month");
284 do {
285 if (alarm->time.tm_mon < 11)
286 alarm->time.tm_mon++;
287 else {
288 alarm->time.tm_mon = 0;
289 alarm->time.tm_year++;
291 days = rtc_month_days(alarm->time.tm_mon,
292 alarm->time.tm_year);
293 } while (days < alarm->time.tm_mday);
294 break;
296 /* Year rollover ... easy except for leap years! */
297 case year:
298 dev_dbg(&rtc->dev, "alarm rollover: %s\n", "year");
299 do {
300 alarm->time.tm_year++;
301 } while (!is_leap_year(alarm->time.tm_year + 1900)
302 && rtc_valid_tm(&alarm->time) != 0);
303 break;
305 default:
306 dev_warn(&rtc->dev, "alarm rollover not handled\n");
309 done:
310 err = rtc_valid_tm(&alarm->time);
312 if (err) {
313 dev_warn(&rtc->dev, "invalid alarm value: %d-%d-%d %d:%d:%d\n",
314 alarm->time.tm_year + 1900, alarm->time.tm_mon + 1,
315 alarm->time.tm_mday, alarm->time.tm_hour, alarm->time.tm_min,
316 alarm->time.tm_sec);
319 return err;
322 int rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
324 int err;
326 err = mutex_lock_interruptible(&rtc->ops_lock);
327 if (err)
328 return err;
329 if (rtc->ops == NULL)
330 err = -ENODEV;
331 else if (!rtc->ops->read_alarm)
332 err = -EINVAL;
333 else {
334 memset(alarm, 0, sizeof(struct rtc_wkalrm));
335 alarm->enabled = rtc->aie_timer.enabled;
336 alarm->time = rtc_ktime_to_tm(rtc->aie_timer.node.expires);
338 mutex_unlock(&rtc->ops_lock);
340 return err;
342 EXPORT_SYMBOL_GPL(rtc_read_alarm);
344 static int __rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
346 struct rtc_time tm;
347 time64_t now, scheduled;
348 int err;
350 err = rtc_valid_tm(&alarm->time);
351 if (err)
352 return err;
353 scheduled = rtc_tm_to_time64(&alarm->time);
355 /* Make sure we're not setting alarms in the past */
356 err = __rtc_read_time(rtc, &tm);
357 if (err)
358 return err;
359 now = rtc_tm_to_time64(&tm);
360 if (scheduled <= now)
361 return -ETIME;
363 * XXX - We just checked to make sure the alarm time is not
364 * in the past, but there is still a race window where if
365 * the is alarm set for the next second and the second ticks
366 * over right here, before we set the alarm.
369 if (!rtc->ops)
370 err = -ENODEV;
371 else if (!rtc->ops->set_alarm)
372 err = -EINVAL;
373 else
374 err = rtc->ops->set_alarm(rtc->dev.parent, alarm);
376 return err;
379 int rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
381 int err;
383 err = rtc_valid_tm(&alarm->time);
384 if (err != 0)
385 return err;
387 err = mutex_lock_interruptible(&rtc->ops_lock);
388 if (err)
389 return err;
390 if (rtc->aie_timer.enabled)
391 rtc_timer_remove(rtc, &rtc->aie_timer);
393 rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time);
394 rtc->aie_timer.period = ktime_set(0, 0);
395 if (alarm->enabled)
396 err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
398 mutex_unlock(&rtc->ops_lock);
399 return err;
401 EXPORT_SYMBOL_GPL(rtc_set_alarm);
403 /* Called once per device from rtc_device_register */
404 int rtc_initialize_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
406 int err;
407 struct rtc_time now;
409 err = rtc_valid_tm(&alarm->time);
410 if (err != 0)
411 return err;
413 err = rtc_read_time(rtc, &now);
414 if (err)
415 return err;
417 err = mutex_lock_interruptible(&rtc->ops_lock);
418 if (err)
419 return err;
421 rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time);
422 rtc->aie_timer.period = ktime_set(0, 0);
424 /* Alarm has to be enabled & in the futrure for us to enqueue it */
425 if (alarm->enabled && (rtc_tm_to_ktime(now).tv64 <
426 rtc->aie_timer.node.expires.tv64)) {
428 rtc->aie_timer.enabled = 1;
429 timerqueue_add(&rtc->timerqueue, &rtc->aie_timer.node);
431 mutex_unlock(&rtc->ops_lock);
432 return err;
434 EXPORT_SYMBOL_GPL(rtc_initialize_alarm);
438 int rtc_alarm_irq_enable(struct rtc_device *rtc, unsigned int enabled)
440 int err = mutex_lock_interruptible(&rtc->ops_lock);
441 if (err)
442 return err;
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);
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);
460 mutex_unlock(&rtc->ops_lock);
461 return err;
463 EXPORT_SYMBOL_GPL(rtc_alarm_irq_enable);
465 int rtc_update_irq_enable(struct rtc_device *rtc, unsigned int enabled)
467 int err = mutex_lock_interruptible(&rtc->ops_lock);
468 if (err)
469 return err;
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);
476 #endif
477 /* make sure we're changing state */
478 if (rtc->uie_rtctimer.enabled == enabled)
479 goto out;
481 if (rtc->uie_unsupported) {
482 err = -EINVAL;
483 goto out;
486 if (enabled) {
487 struct rtc_time tm;
488 ktime_t now, onesec;
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);
499 out:
500 mutex_unlock(&rtc->ops_lock);
501 #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
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.
508 if (err == -EINVAL)
509 err = rtc_dev_update_irq_enable_emul(rtc, enabled);
510 #endif
511 return err;
514 EXPORT_SYMBOL_GPL(rtc_update_irq_enable);
518 * rtc_handle_legacy_irq - AIE, UIE and PIE event hook
519 * @rtc: pointer to the rtc device
521 * This function is called when an AIE, UIE or PIE mode interrupt
522 * has occurred (or been emulated).
524 * Triggers the registered irq_task function callback.
526 void rtc_handle_legacy_irq(struct rtc_device *rtc, int num, int mode)
528 unsigned long flags;
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);
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);
541 wake_up_interruptible(&rtc->irq_queue);
542 kill_fasync(&rtc->async_queue, SIGIO, POLL_IN);
547 * rtc_aie_update_irq - AIE mode rtctimer hook
548 * @private: pointer to the rtc_device
550 * This functions is called when the aie_timer expires.
552 void rtc_aie_update_irq(void *private)
554 struct rtc_device *rtc = (struct rtc_device *)private;
555 rtc_handle_legacy_irq(rtc, 1, RTC_AF);
560 * rtc_uie_update_irq - UIE mode rtctimer hook
561 * @private: pointer to the rtc_device
563 * This functions is called when the uie_timer expires.
565 void rtc_uie_update_irq(void *private)
567 struct rtc_device *rtc = (struct rtc_device *)private;
568 rtc_handle_legacy_irq(rtc, 1, RTC_UF);
573 * rtc_pie_update_irq - PIE mode hrtimer hook
574 * @timer: pointer to the pie mode hrtimer
576 * This function is used to emulate PIE mode interrupts
577 * using an hrtimer. This function is called when the periodic
578 * hrtimer expires.
580 enum hrtimer_restart rtc_pie_update_irq(struct hrtimer *timer)
582 struct rtc_device *rtc;
583 ktime_t period;
584 int count;
585 rtc = container_of(timer, struct rtc_device, pie_timer);
587 period = ktime_set(0, NSEC_PER_SEC/rtc->irq_freq);
588 count = hrtimer_forward_now(timer, period);
590 rtc_handle_legacy_irq(rtc, count, RTC_PF);
592 return HRTIMER_RESTART;
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
602 void rtc_update_irq(struct rtc_device *rtc,
603 unsigned long num, unsigned long events)
605 if (unlikely(IS_ERR_OR_NULL(rtc)))
606 return;
608 pm_stay_awake(rtc->dev.parent);
609 schedule_work(&rtc->irqwork);
611 EXPORT_SYMBOL_GPL(rtc_update_irq);
613 static int __rtc_match(struct device *dev, const void *data)
615 const char *name = data;
617 if (strcmp(dev_name(dev), name) == 0)
618 return 1;
619 return 0;
622 struct rtc_device *rtc_class_open(const char *name)
624 struct device *dev;
625 struct rtc_device *rtc = NULL;
627 dev = class_find_device(rtc_class, NULL, name, __rtc_match);
628 if (dev)
629 rtc = to_rtc_device(dev);
631 if (rtc) {
632 if (!try_module_get(rtc->owner)) {
633 put_device(dev);
634 rtc = NULL;
638 return rtc;
640 EXPORT_SYMBOL_GPL(rtc_class_open);
642 void rtc_class_close(struct rtc_device *rtc)
644 module_put(rtc->owner);
645 put_device(&rtc->dev);
647 EXPORT_SYMBOL_GPL(rtc_class_close);
649 int rtc_irq_register(struct rtc_device *rtc, struct rtc_task *task)
651 int retval = -EBUSY;
653 if (task == NULL || task->func == NULL)
654 return -EINVAL;
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;
660 spin_lock_irq(&rtc->irq_task_lock);
661 if (rtc->irq_task == NULL) {
662 rtc->irq_task = task;
663 retval = 0;
665 spin_unlock_irq(&rtc->irq_task_lock);
667 clear_bit_unlock(RTC_DEV_BUSY, &rtc->flags);
669 return retval;
671 EXPORT_SYMBOL_GPL(rtc_irq_register);
673 void rtc_irq_unregister(struct rtc_device *rtc, struct rtc_task *task)
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);
680 EXPORT_SYMBOL_GPL(rtc_irq_unregister);
682 static int rtc_update_hrtimer(struct rtc_device *rtc, int enabled)
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.
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.
694 if (hrtimer_try_to_cancel(&rtc->pie_timer) < 0)
695 return -1;
697 if (enabled) {
698 ktime_t period = ktime_set(0, NSEC_PER_SEC / rtc->irq_freq);
700 hrtimer_start(&rtc->pie_timer, period, HRTIMER_MODE_REL);
702 return 0;
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
712 * Note that rtc_irq_set_freq() should previously have been used to
713 * specify the desired frequency of periodic IRQ task->func() callbacks.
715 int rtc_irq_set_state(struct rtc_device *rtc, struct rtc_task *task, int enabled)
717 int err = 0;
718 unsigned long flags;
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;
732 rtc->pie_enabled = enabled;
734 spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
735 return err;
737 EXPORT_SYMBOL_GPL(rtc_irq_set_state);
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
746 * Note that rtc_irq_set_state() is used to enable or disable the
747 * periodic IRQs.
749 int rtc_irq_set_freq(struct rtc_device *rtc, struct rtc_task *task, int freq)
751 int err = 0;
752 unsigned long flags;
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;
770 spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
771 return err;
773 EXPORT_SYMBOL_GPL(rtc_irq_set_freq);
776 * rtc_timer_enqueue - Adds a rtc_timer to the rtc_device timerqueue
777 * @rtc rtc device
778 * @timer timer being added.
780 * Enqueues a timer onto the rtc devices timerqueue and sets
781 * the next alarm event appropriately.
783 * Sets the enabled bit on the added timer.
785 * Must hold ops_lock for proper serialization of timerqueue
787 static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer)
789 timer->enabled = 1;
790 timerqueue_add(&rtc->timerqueue, &timer->node);
791 if (&timer->node == timerqueue_getnext(&rtc->timerqueue)) {
792 struct rtc_wkalrm alarm;
793 int err;
794 alarm.time = rtc_ktime_to_tm(timer->node.expires);
795 alarm.enabled = 1;
796 err = __rtc_set_alarm(rtc, &alarm);
797 if (err == -ETIME) {
798 pm_stay_awake(rtc->dev.parent);
799 schedule_work(&rtc->irqwork);
800 } else if (err) {
801 timerqueue_del(&rtc->timerqueue, &timer->node);
802 timer->enabled = 0;
803 return err;
806 return 0;
809 static void rtc_alarm_disable(struct rtc_device *rtc)
811 if (!rtc->ops || !rtc->ops->alarm_irq_enable)
812 return;
814 rtc->ops->alarm_irq_enable(rtc->dev.parent, false);
818 * rtc_timer_remove - Removes a rtc_timer from the rtc_device timerqueue
819 * @rtc rtc device
820 * @timer timer being removed.
822 * Removes a timer onto the rtc devices timerqueue and sets
823 * the next alarm event appropriately.
825 * Clears the enabled bit on the removed timer.
827 * Must hold ops_lock for proper serialization of timerqueue
829 static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer)
831 struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue);
832 timerqueue_del(&rtc->timerqueue, &timer->node);
833 timer->enabled = 0;
834 if (next == &timer->node) {
835 struct rtc_wkalrm alarm;
836 int err;
837 next = timerqueue_getnext(&rtc->timerqueue);
838 if (!next) {
839 rtc_alarm_disable(rtc);
840 return;
842 alarm.time = rtc_ktime_to_tm(next->expires);
843 alarm.enabled = 1;
844 err = __rtc_set_alarm(rtc, &alarm);
845 if (err == -ETIME) {
846 pm_stay_awake(rtc->dev.parent);
847 schedule_work(&rtc->irqwork);
853 * rtc_timer_do_work - Expires rtc timers
854 * @rtc rtc device
855 * @timer timer being removed.
857 * Expires rtc timers. Reprograms next alarm event if needed.
858 * Called via worktask.
860 * Serializes access to timerqueue via ops_lock mutex
862 void rtc_timer_do_work(struct work_struct *work)
864 struct rtc_timer *timer;
865 struct timerqueue_node *next;
866 ktime_t now;
867 struct rtc_time tm;
869 struct rtc_device *rtc =
870 container_of(work, struct rtc_device, irqwork);
872 mutex_lock(&rtc->ops_lock);
873 again:
874 __rtc_read_time(rtc, &tm);
875 now = rtc_tm_to_ktime(tm);
876 while ((next = timerqueue_getnext(&rtc->timerqueue))) {
877 if (next->expires.tv64 > now.tv64)
878 break;
880 /* expire timer */
881 timer = container_of(next, struct rtc_timer, node);
882 timerqueue_del(&rtc->timerqueue, &timer->node);
883 timer->enabled = 0;
884 if (timer->task.func)
885 timer->task.func(timer->task.private_data);
887 /* Re-add/fwd periodic timers */
888 if (ktime_to_ns(timer->period)) {
889 timer->node.expires = ktime_add(timer->node.expires,
890 timer->period);
891 timer->enabled = 1;
892 timerqueue_add(&rtc->timerqueue, &timer->node);
896 /* Set next alarm */
897 if (next) {
898 struct rtc_wkalrm alarm;
899 int err;
900 int retry = 3;
902 alarm.time = rtc_ktime_to_tm(next->expires);
903 alarm.enabled = 1;
904 reprogram:
905 err = __rtc_set_alarm(rtc, &alarm);
906 if (err == -ETIME)
907 goto again;
908 else if (err) {
909 if (retry-- > 0)
910 goto reprogram;
912 timer = container_of(next, struct rtc_timer, node);
913 timerqueue_del(&rtc->timerqueue, &timer->node);
914 timer->enabled = 0;
915 dev_err(&rtc->dev, "__rtc_set_alarm: err=%d\n", err);
916 goto again;
918 } else
919 rtc_alarm_disable(rtc);
921 pm_relax(rtc->dev.parent);
922 mutex_unlock(&rtc->ops_lock);
926 /* rtc_timer_init - Initializes an rtc_timer
927 * @timer: timer to be intiialized
928 * @f: function pointer to be called when timer fires
929 * @data: private data passed to function pointer
931 * Kernel interface to initializing an rtc_timer.
933 void rtc_timer_init(struct rtc_timer *timer, void (*f)(void *p), void *data)
935 timerqueue_init(&timer->node);
936 timer->enabled = 0;
937 timer->task.func = f;
938 timer->task.private_data = data;
941 /* rtc_timer_start - Sets an rtc_timer to fire in the future
942 * @ rtc: rtc device to be used
943 * @ timer: timer being set
944 * @ expires: time at which to expire the timer
945 * @ period: period that the timer will recur
947 * Kernel interface to set an rtc_timer
949 int rtc_timer_start(struct rtc_device *rtc, struct rtc_timer *timer,
950 ktime_t expires, ktime_t period)
952 int ret = 0;
953 mutex_lock(&rtc->ops_lock);
954 if (timer->enabled)
955 rtc_timer_remove(rtc, timer);
957 timer->node.expires = expires;
958 timer->period = period;
960 ret = rtc_timer_enqueue(rtc, timer);
962 mutex_unlock(&rtc->ops_lock);
963 return ret;
966 /* rtc_timer_cancel - Stops an rtc_timer
967 * @ rtc: rtc device to be used
968 * @ timer: timer being set
970 * Kernel interface to cancel an rtc_timer
972 int rtc_timer_cancel(struct rtc_device *rtc, struct rtc_timer *timer)
974 int ret = 0;
975 mutex_lock(&rtc->ops_lock);
976 if (timer->enabled)
977 rtc_timer_remove(rtc, timer);
978 mutex_unlock(&rtc->ops_lock);
979 return ret;