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)
56 err = rtc->ops->set_time(rtc->dev.parent, tm);
58 mutex_unlock(&rtc->ops_lock);
61 EXPORT_SYMBOL_GPL(rtc_set_time);
63 int rtc_set_mmss(struct rtc_device *rtc, unsigned long secs)
67 err = mutex_lock_interruptible(&rtc->ops_lock);
73 else if (rtc->ops->set_mmss)
74 err = rtc->ops->set_mmss(rtc->dev.parent, secs);
75 else if (rtc->ops->read_time && rtc->ops->set_time) {
76 struct rtc_time new, old;
78 err = rtc->ops->read_time(rtc->dev.parent, &old);
80 rtc_time_to_tm(secs, &new);
83 * avoid writing when we're going to change the day of
84 * the month. We will retry in the next minute. This
85 * basically means that if the RTC must not drift
86 * by more than 1 minute in 11 minutes.
88 if (!((old.tm_hour == 23 && old.tm_min == 59) ||
89 (new.tm_hour == 23 && new.tm_min == 59)))
90 err = rtc->ops->set_time(rtc->dev.parent,
97 mutex_unlock(&rtc->ops_lock);
101 EXPORT_SYMBOL_GPL(rtc_set_mmss);
103 static int rtc_read_alarm_internal(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
107 err = mutex_lock_interruptible(&rtc->ops_lock);
111 if (rtc->ops == NULL)
113 else if (!rtc->ops->read_alarm)
116 memset(alarm, 0, sizeof(struct rtc_wkalrm));
117 err = rtc->ops->read_alarm(rtc->dev.parent, alarm);
120 mutex_unlock(&rtc->ops_lock);
124 int rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
127 struct rtc_time before, now;
129 unsigned long t_now, t_alm;
130 enum { none, day, month, year } missing = none;
133 /* The lower level RTC driver may return -1 in some fields,
134 * creating invalid alarm->time values, for reasons like:
136 * - The hardware may not be capable of filling them in;
137 * many alarms match only on time-of-day fields, not
138 * day/month/year calendar data.
140 * - Some hardware uses illegal values as "wildcard" match
141 * values, which non-Linux firmware (like a BIOS) may try
142 * to set up as e.g. "alarm 15 minutes after each hour".
143 * Linux uses only oneshot alarms.
145 * When we see that here, we deal with it by using values from
146 * a current RTC timestamp for any missing (-1) values. The
147 * RTC driver prevents "periodic alarm" modes.
149 * But this can be racey, because some fields of the RTC timestamp
150 * may have wrapped in the interval since we read the RTC alarm,
151 * which would lead to us inserting inconsistent values in place
154 * Reading the alarm and timestamp in the reverse sequence
155 * would have the same race condition, and not solve the issue.
157 * So, we must first read the RTC timestamp,
158 * then read the RTC alarm value,
159 * and then read a second RTC timestamp.
161 * If any fields of the second timestamp have changed
162 * when compared with the first timestamp, then we know
163 * our timestamp may be inconsistent with that used by
164 * the low-level rtc_read_alarm_internal() function.
166 * So, when the two timestamps disagree, we just loop and do
167 * the process again to get a fully consistent set of values.
169 * This could all instead be done in the lower level driver,
170 * but since more than one lower level RTC implementation needs it,
171 * then it's probably best best to do it here instead of there..
174 /* Get the "before" timestamp */
175 err = rtc_read_time(rtc, &before);
180 memcpy(&before, &now, sizeof(struct rtc_time));
183 /* get the RTC alarm values, which may be incomplete */
184 err = rtc_read_alarm_internal(rtc, alarm);
190 /* full-function RTCs won't have such missing fields */
191 if (rtc_valid_tm(&alarm->time) == 0)
194 /* get the "after" timestamp, to detect wrapped fields */
195 err = rtc_read_time(rtc, &now);
199 /* note that tm_sec is a "don't care" value here: */
200 } while ( before.tm_min != now.tm_min
201 || before.tm_hour != now.tm_hour
202 || before.tm_mon != now.tm_mon
203 || before.tm_year != now.tm_year);
205 /* Fill in the missing alarm fields using the timestamp; we
206 * know there's at least one since alarm->time is invalid.
208 if (alarm->time.tm_sec == -1)
209 alarm->time.tm_sec = now.tm_sec;
210 if (alarm->time.tm_min == -1)
211 alarm->time.tm_min = now.tm_min;
212 if (alarm->time.tm_hour == -1)
213 alarm->time.tm_hour = now.tm_hour;
215 /* For simplicity, only support date rollover for now */
216 if (alarm->time.tm_mday == -1) {
217 alarm->time.tm_mday = now.tm_mday;
220 if (alarm->time.tm_mon == -1) {
221 alarm->time.tm_mon = now.tm_mon;
225 if (alarm->time.tm_year == -1) {
226 alarm->time.tm_year = now.tm_year;
231 /* with luck, no rollover is needed */
232 rtc_tm_to_time(&now, &t_now);
233 rtc_tm_to_time(&alarm->time, &t_alm);
239 /* 24 hour rollover ... if it's now 10am Monday, an alarm that
240 * that will trigger at 5am will do so at 5am Tuesday, which
241 * could also be in the next month or year. This is a common
242 * case, especially for PCs.
245 dev_dbg(&rtc->dev, "alarm rollover: %s\n", "day");
246 t_alm += 24 * 60 * 60;
247 rtc_time_to_tm(t_alm, &alarm->time);
250 /* Month rollover ... if it's the 31th, an alarm on the 3rd will
251 * be next month. An alarm matching on the 30th, 29th, or 28th
252 * may end up in the month after that! Many newer PCs support
253 * this type of alarm.
256 dev_dbg(&rtc->dev, "alarm rollover: %s\n", "month");
258 if (alarm->time.tm_mon < 11)
259 alarm->time.tm_mon++;
261 alarm->time.tm_mon = 0;
262 alarm->time.tm_year++;
264 days = rtc_month_days(alarm->time.tm_mon,
265 alarm->time.tm_year);
266 } while (days < alarm->time.tm_mday);
269 /* Year rollover ... easy except for leap years! */
271 dev_dbg(&rtc->dev, "alarm rollover: %s\n", "year");
273 alarm->time.tm_year++;
274 } while (!rtc_valid_tm(&alarm->time));
278 dev_warn(&rtc->dev, "alarm rollover not handled\n");
284 EXPORT_SYMBOL_GPL(rtc_read_alarm);
286 int rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
290 err = rtc_valid_tm(&alarm->time);
294 err = mutex_lock_interruptible(&rtc->ops_lock);
300 else if (!rtc->ops->set_alarm)
303 err = rtc->ops->set_alarm(rtc->dev.parent, alarm);
305 mutex_unlock(&rtc->ops_lock);
308 EXPORT_SYMBOL_GPL(rtc_set_alarm);
311 * rtc_update_irq - report RTC periodic, alarm, and/or update irqs
312 * @rtc: the rtc device
313 * @num: how many irqs are being reported (usually one)
314 * @events: mask of RTC_IRQF with one or more of RTC_PF, RTC_AF, RTC_UF
315 * Context: in_interrupt(), irqs blocked
317 void rtc_update_irq(struct rtc_device *rtc,
318 unsigned long num, unsigned long events)
320 spin_lock(&rtc->irq_lock);
321 rtc->irq_data = (rtc->irq_data + (num << 8)) | events;
322 spin_unlock(&rtc->irq_lock);
324 spin_lock(&rtc->irq_task_lock);
326 rtc->irq_task->func(rtc->irq_task->private_data);
327 spin_unlock(&rtc->irq_task_lock);
329 wake_up_interruptible(&rtc->irq_queue);
330 kill_fasync(&rtc->async_queue, SIGIO, POLL_IN);
332 EXPORT_SYMBOL_GPL(rtc_update_irq);
334 static int __rtc_match(struct device *dev, void *data)
336 char *name = (char *)data;
338 if (strncmp(dev->bus_id, name, BUS_ID_SIZE) == 0)
343 struct rtc_device *rtc_class_open(char *name)
346 struct rtc_device *rtc = NULL;
348 dev = class_find_device(rtc_class, name, __rtc_match);
350 rtc = to_rtc_device(dev);
353 if (!try_module_get(rtc->owner)) {
361 EXPORT_SYMBOL_GPL(rtc_class_open);
363 void rtc_class_close(struct rtc_device *rtc)
365 module_put(rtc->owner);
366 put_device(&rtc->dev);
368 EXPORT_SYMBOL_GPL(rtc_class_close);
370 int rtc_irq_register(struct rtc_device *rtc, struct rtc_task *task)
374 if (task == NULL || task->func == NULL)
377 /* Cannot register while the char dev is in use */
378 if (test_and_set_bit_lock(RTC_DEV_BUSY, &rtc->flags))
381 spin_lock_irq(&rtc->irq_task_lock);
382 if (rtc->irq_task == NULL) {
383 rtc->irq_task = task;
386 spin_unlock_irq(&rtc->irq_task_lock);
388 clear_bit_unlock(RTC_DEV_BUSY, &rtc->flags);
392 EXPORT_SYMBOL_GPL(rtc_irq_register);
394 void rtc_irq_unregister(struct rtc_device *rtc, struct rtc_task *task)
396 spin_lock_irq(&rtc->irq_task_lock);
397 if (rtc->irq_task == task)
398 rtc->irq_task = NULL;
399 spin_unlock_irq(&rtc->irq_task_lock);
401 EXPORT_SYMBOL_GPL(rtc_irq_unregister);
404 * rtc_irq_set_state - enable/disable 2^N Hz periodic IRQs
405 * @rtc: the rtc device
406 * @task: currently registered with rtc_irq_register()
407 * @enabled: true to enable periodic IRQs
410 * Note that rtc_irq_set_freq() should previously have been used to
411 * specify the desired frequency of periodic IRQ task->func() callbacks.
413 int rtc_irq_set_state(struct rtc_device *rtc, struct rtc_task *task, int enabled)
418 if (rtc->ops->irq_set_state == NULL)
421 spin_lock_irqsave(&rtc->irq_task_lock, flags);
422 if (rtc->irq_task != NULL && task == NULL)
424 if (rtc->irq_task != task)
426 spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
429 err = rtc->ops->irq_set_state(rtc->dev.parent, enabled);
433 EXPORT_SYMBOL_GPL(rtc_irq_set_state);
436 * rtc_irq_set_freq - set 2^N Hz periodic IRQ frequency for IRQ
437 * @rtc: the rtc device
438 * @task: currently registered with rtc_irq_register()
439 * @freq: positive frequency with which task->func() will be called
442 * Note that rtc_irq_set_state() is used to enable or disable the
445 int rtc_irq_set_freq(struct rtc_device *rtc, struct rtc_task *task, int freq)
450 if (rtc->ops->irq_set_freq == NULL)
453 if (!is_power_of_2(freq))
456 spin_lock_irqsave(&rtc->irq_task_lock, flags);
457 if (rtc->irq_task != NULL && task == NULL)
459 if (rtc->irq_task != task)
461 spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
464 err = rtc->ops->irq_set_freq(rtc->dev.parent, freq);
466 rtc->irq_freq = freq;
470 EXPORT_SYMBOL_GPL(rtc_irq_set_freq);