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/log2.h>
17 int rtc_read_time(struct rtc_device
*rtc
, struct rtc_time
*tm
)
21 err
= mutex_lock_interruptible(&rtc
->ops_lock
);
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
);
34 mutex_unlock(&rtc
->ops_lock
);
37 EXPORT_SYMBOL_GPL(rtc_read_time
);
39 int rtc_set_time(struct rtc_device
*rtc
, struct rtc_time
*tm
)
43 err
= rtc_valid_tm(tm
);
47 err
= mutex_lock_interruptible(&rtc
->ops_lock
);
53 else if (rtc
->ops
->set_time
)
54 err
= rtc
->ops
->set_time(rtc
->dev
.parent
, tm
);
55 else if (rtc
->ops
->set_mmss
) {
57 err
= rtc_tm_to_time(tm
, &secs
);
59 err
= rtc
->ops
->set_mmss(rtc
->dev
.parent
, secs
);
63 mutex_unlock(&rtc
->ops_lock
);
66 EXPORT_SYMBOL_GPL(rtc_set_time
);
68 int rtc_set_mmss(struct rtc_device
*rtc
, unsigned long secs
)
72 err
= mutex_lock_interruptible(&rtc
->ops_lock
);
78 else if (rtc
->ops
->set_mmss
)
79 err
= rtc
->ops
->set_mmss(rtc
->dev
.parent
, secs
);
80 else if (rtc
->ops
->read_time
&& rtc
->ops
->set_time
) {
81 struct rtc_time
new, old
;
83 err
= rtc
->ops
->read_time(rtc
->dev
.parent
, &old
);
85 rtc_time_to_tm(secs
, &new);
88 * avoid writing when we're going to change the day of
89 * the month. We will retry in the next minute. This
90 * basically means that if the RTC must not drift
91 * by more than 1 minute in 11 minutes.
93 if (!((old
.tm_hour
== 23 && old
.tm_min
== 59) ||
94 (new.tm_hour
== 23 && new.tm_min
== 59)))
95 err
= rtc
->ops
->set_time(rtc
->dev
.parent
,
102 mutex_unlock(&rtc
->ops_lock
);
106 EXPORT_SYMBOL_GPL(rtc_set_mmss
);
108 static int rtc_read_alarm_internal(struct rtc_device
*rtc
, struct rtc_wkalrm
*alarm
)
112 err
= mutex_lock_interruptible(&rtc
->ops_lock
);
116 if (rtc
->ops
== NULL
)
118 else if (!rtc
->ops
->read_alarm
)
121 memset(alarm
, 0, sizeof(struct rtc_wkalrm
));
122 err
= rtc
->ops
->read_alarm(rtc
->dev
.parent
, alarm
);
125 mutex_unlock(&rtc
->ops_lock
);
129 int rtc_read_alarm(struct rtc_device
*rtc
, struct rtc_wkalrm
*alarm
)
132 struct rtc_time before
, now
;
134 unsigned long t_now
, t_alm
;
135 enum { none
, day
, month
, year
} missing
= none
;
138 /* The lower level RTC driver may return -1 in some fields,
139 * creating invalid alarm->time values, for reasons like:
141 * - The hardware may not be capable of filling them in;
142 * many alarms match only on time-of-day fields, not
143 * day/month/year calendar data.
145 * - Some hardware uses illegal values as "wildcard" match
146 * values, which non-Linux firmware (like a BIOS) may try
147 * to set up as e.g. "alarm 15 minutes after each hour".
148 * Linux uses only oneshot alarms.
150 * When we see that here, we deal with it by using values from
151 * a current RTC timestamp for any missing (-1) values. The
152 * RTC driver prevents "periodic alarm" modes.
154 * But this can be racey, because some fields of the RTC timestamp
155 * may have wrapped in the interval since we read the RTC alarm,
156 * which would lead to us inserting inconsistent values in place
159 * Reading the alarm and timestamp in the reverse sequence
160 * would have the same race condition, and not solve the issue.
162 * So, we must first read the RTC timestamp,
163 * then read the RTC alarm value,
164 * and then read a second RTC timestamp.
166 * If any fields of the second timestamp have changed
167 * when compared with the first timestamp, then we know
168 * our timestamp may be inconsistent with that used by
169 * the low-level rtc_read_alarm_internal() function.
171 * So, when the two timestamps disagree, we just loop and do
172 * the process again to get a fully consistent set of values.
174 * This could all instead be done in the lower level driver,
175 * but since more than one lower level RTC implementation needs it,
176 * then it's probably best best to do it here instead of there..
179 /* Get the "before" timestamp */
180 err
= rtc_read_time(rtc
, &before
);
185 memcpy(&before
, &now
, sizeof(struct rtc_time
));
188 /* get the RTC alarm values, which may be incomplete */
189 err
= rtc_read_alarm_internal(rtc
, alarm
);
195 /* full-function RTCs won't have such missing fields */
196 if (rtc_valid_tm(&alarm
->time
) == 0)
199 /* get the "after" timestamp, to detect wrapped fields */
200 err
= rtc_read_time(rtc
, &now
);
204 /* note that tm_sec is a "don't care" value here: */
205 } while ( before
.tm_min
!= now
.tm_min
206 || before
.tm_hour
!= now
.tm_hour
207 || before
.tm_mon
!= now
.tm_mon
208 || before
.tm_year
!= now
.tm_year
);
210 /* Fill in the missing alarm fields using the timestamp; we
211 * know there's at least one since alarm->time is invalid.
213 if (alarm
->time
.tm_sec
== -1)
214 alarm
->time
.tm_sec
= now
.tm_sec
;
215 if (alarm
->time
.tm_min
== -1)
216 alarm
->time
.tm_min
= now
.tm_min
;
217 if (alarm
->time
.tm_hour
== -1)
218 alarm
->time
.tm_hour
= now
.tm_hour
;
220 /* For simplicity, only support date rollover for now */
221 if (alarm
->time
.tm_mday
== -1) {
222 alarm
->time
.tm_mday
= now
.tm_mday
;
225 if (alarm
->time
.tm_mon
== -1) {
226 alarm
->time
.tm_mon
= now
.tm_mon
;
230 if (alarm
->time
.tm_year
== -1) {
231 alarm
->time
.tm_year
= now
.tm_year
;
236 /* with luck, no rollover is needed */
237 rtc_tm_to_time(&now
, &t_now
);
238 rtc_tm_to_time(&alarm
->time
, &t_alm
);
244 /* 24 hour rollover ... if it's now 10am Monday, an alarm that
245 * that will trigger at 5am will do so at 5am Tuesday, which
246 * could also be in the next month or year. This is a common
247 * case, especially for PCs.
250 dev_dbg(&rtc
->dev
, "alarm rollover: %s\n", "day");
251 t_alm
+= 24 * 60 * 60;
252 rtc_time_to_tm(t_alm
, &alarm
->time
);
255 /* Month rollover ... if it's the 31th, an alarm on the 3rd will
256 * be next month. An alarm matching on the 30th, 29th, or 28th
257 * may end up in the month after that! Many newer PCs support
258 * this type of alarm.
261 dev_dbg(&rtc
->dev
, "alarm rollover: %s\n", "month");
263 if (alarm
->time
.tm_mon
< 11)
264 alarm
->time
.tm_mon
++;
266 alarm
->time
.tm_mon
= 0;
267 alarm
->time
.tm_year
++;
269 days
= rtc_month_days(alarm
->time
.tm_mon
,
270 alarm
->time
.tm_year
);
271 } while (days
< alarm
->time
.tm_mday
);
274 /* Year rollover ... easy except for leap years! */
276 dev_dbg(&rtc
->dev
, "alarm rollover: %s\n", "year");
278 alarm
->time
.tm_year
++;
279 } while (rtc_valid_tm(&alarm
->time
) != 0);
283 dev_warn(&rtc
->dev
, "alarm rollover not handled\n");
289 EXPORT_SYMBOL_GPL(rtc_read_alarm
);
291 int rtc_set_alarm(struct rtc_device
*rtc
, struct rtc_wkalrm
*alarm
)
295 err
= rtc_valid_tm(&alarm
->time
);
299 err
= mutex_lock_interruptible(&rtc
->ops_lock
);
305 else if (!rtc
->ops
->set_alarm
)
308 err
= rtc
->ops
->set_alarm(rtc
->dev
.parent
, alarm
);
310 mutex_unlock(&rtc
->ops_lock
);
313 EXPORT_SYMBOL_GPL(rtc_set_alarm
);
315 int rtc_alarm_irq_enable(struct rtc_device
*rtc
, unsigned int enabled
)
317 int err
= mutex_lock_interruptible(&rtc
->ops_lock
);
323 else if (!rtc
->ops
->alarm_irq_enable
)
326 err
= rtc
->ops
->alarm_irq_enable(rtc
->dev
.parent
, enabled
);
328 mutex_unlock(&rtc
->ops_lock
);
331 EXPORT_SYMBOL_GPL(rtc_alarm_irq_enable
);
333 int rtc_update_irq_enable(struct rtc_device
*rtc
, unsigned int enabled
)
335 int err
= mutex_lock_interruptible(&rtc
->ops_lock
);
339 #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
340 if (enabled
== 0 && rtc
->uie_irq_active
) {
341 mutex_unlock(&rtc
->ops_lock
);
342 return rtc_dev_update_irq_enable_emul(rtc
, enabled
);
348 else if (!rtc
->ops
->update_irq_enable
)
351 err
= rtc
->ops
->update_irq_enable(rtc
->dev
.parent
, enabled
);
353 mutex_unlock(&rtc
->ops_lock
);
355 #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
357 * Enable emulation if the driver did not provide
358 * the update_irq_enable function pointer or if returned
359 * -EINVAL to signal that it has been configured without
360 * interrupts or that are not available at the moment.
363 err
= rtc_dev_update_irq_enable_emul(rtc
, enabled
);
367 EXPORT_SYMBOL_GPL(rtc_update_irq_enable
);
370 * rtc_update_irq - report RTC periodic, alarm, and/or update irqs
371 * @rtc: the rtc device
372 * @num: how many irqs are being reported (usually one)
373 * @events: mask of RTC_IRQF with one or more of RTC_PF, RTC_AF, RTC_UF
376 void rtc_update_irq(struct rtc_device
*rtc
,
377 unsigned long num
, unsigned long events
)
381 spin_lock_irqsave(&rtc
->irq_lock
, flags
);
382 rtc
->irq_data
= (rtc
->irq_data
+ (num
<< 8)) | events
;
383 spin_unlock_irqrestore(&rtc
->irq_lock
, flags
);
385 spin_lock_irqsave(&rtc
->irq_task_lock
, flags
);
387 rtc
->irq_task
->func(rtc
->irq_task
->private_data
);
388 spin_unlock_irqrestore(&rtc
->irq_task_lock
, flags
);
390 wake_up_interruptible(&rtc
->irq_queue
);
391 kill_fasync(&rtc
->async_queue
, SIGIO
, POLL_IN
);
393 EXPORT_SYMBOL_GPL(rtc_update_irq
);
395 static int __rtc_match(struct device
*dev
, void *data
)
397 char *name
= (char *)data
;
399 if (strcmp(dev_name(dev
), name
) == 0)
404 struct rtc_device
*rtc_class_open(char *name
)
407 struct rtc_device
*rtc
= NULL
;
409 dev
= class_find_device(rtc_class
, NULL
, name
, __rtc_match
);
411 rtc
= to_rtc_device(dev
);
414 if (!try_module_get(rtc
->owner
)) {
422 EXPORT_SYMBOL_GPL(rtc_class_open
);
424 void rtc_class_close(struct rtc_device
*rtc
)
426 module_put(rtc
->owner
);
427 put_device(&rtc
->dev
);
429 EXPORT_SYMBOL_GPL(rtc_class_close
);
431 int rtc_irq_register(struct rtc_device
*rtc
, struct rtc_task
*task
)
435 if (task
== NULL
|| task
->func
== NULL
)
438 /* Cannot register while the char dev is in use */
439 if (test_and_set_bit_lock(RTC_DEV_BUSY
, &rtc
->flags
))
442 spin_lock_irq(&rtc
->irq_task_lock
);
443 if (rtc
->irq_task
== NULL
) {
444 rtc
->irq_task
= task
;
447 spin_unlock_irq(&rtc
->irq_task_lock
);
449 clear_bit_unlock(RTC_DEV_BUSY
, &rtc
->flags
);
453 EXPORT_SYMBOL_GPL(rtc_irq_register
);
455 void rtc_irq_unregister(struct rtc_device
*rtc
, struct rtc_task
*task
)
457 spin_lock_irq(&rtc
->irq_task_lock
);
458 if (rtc
->irq_task
== task
)
459 rtc
->irq_task
= NULL
;
460 spin_unlock_irq(&rtc
->irq_task_lock
);
462 EXPORT_SYMBOL_GPL(rtc_irq_unregister
);
465 * rtc_irq_set_state - enable/disable 2^N Hz periodic IRQs
466 * @rtc: the rtc device
467 * @task: currently registered with rtc_irq_register()
468 * @enabled: true to enable periodic IRQs
471 * Note that rtc_irq_set_freq() should previously have been used to
472 * specify the desired frequency of periodic IRQ task->func() callbacks.
474 int rtc_irq_set_state(struct rtc_device
*rtc
, struct rtc_task
*task
, int enabled
)
479 if (rtc
->ops
->irq_set_state
== NULL
)
482 spin_lock_irqsave(&rtc
->irq_task_lock
, flags
);
483 if (rtc
->irq_task
!= NULL
&& task
== NULL
)
485 if (rtc
->irq_task
!= task
)
487 spin_unlock_irqrestore(&rtc
->irq_task_lock
, flags
);
490 err
= rtc
->ops
->irq_set_state(rtc
->dev
.parent
, enabled
);
494 EXPORT_SYMBOL_GPL(rtc_irq_set_state
);
497 * rtc_irq_set_freq - set 2^N Hz periodic IRQ frequency for IRQ
498 * @rtc: the rtc device
499 * @task: currently registered with rtc_irq_register()
500 * @freq: positive frequency with which task->func() will be called
503 * Note that rtc_irq_set_state() is used to enable or disable the
506 int rtc_irq_set_freq(struct rtc_device
*rtc
, struct rtc_task
*task
, int freq
)
511 if (rtc
->ops
->irq_set_freq
== NULL
)
514 spin_lock_irqsave(&rtc
->irq_task_lock
, flags
);
515 if (rtc
->irq_task
!= NULL
&& task
== NULL
)
517 if (rtc
->irq_task
!= task
)
519 spin_unlock_irqrestore(&rtc
->irq_task_lock
, flags
);
522 err
= rtc
->ops
->irq_set_freq(rtc
->dev
.parent
, freq
);
524 rtc
->irq_freq
= freq
;
528 EXPORT_SYMBOL_GPL(rtc_irq_set_freq
);