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
;
130 /* The lower level RTC driver may not be capable of filling
131 * in all fields of the rtc_time struct (eg. rtc-cmos),
132 * and so might instead return -1 in some fields.
133 * We deal with that here by grabbing a current RTC timestamp
134 * and using values from that for any missing (-1) values.
136 * But this can be racey, because some fields of the RTC timestamp
137 * may have wrapped in the interval since we read the RTC alarm,
138 * which would lead to us inserting inconsistent values in place
141 * Reading the alarm and timestamp in the reverse sequence
142 * would have the same race condition, and not solve the issue.
144 * So, we must first read the RTC timestamp,
145 * then read the RTC alarm value,
146 * and then read a second RTC timestamp.
148 * If any fields of the second timestamp have changed
149 * when compared with the first timestamp, then we know
150 * our timestamp may be inconsistent with that used by
151 * the low-level rtc_read_alarm_internal() function.
153 * So, when the two timestamps disagree, we just loop and do
154 * the process again to get a fully consistent set of values.
156 * This could all instead be done in the lower level driver,
157 * but since more than one lower level RTC implementation needs it,
158 * then it's probably best best to do it here instead of there..
161 /* Get the "before" timestamp */
162 err
= rtc_read_time(rtc
, &before
);
167 memcpy(&before
, &now
, sizeof(struct rtc_time
));
170 /* get the RTC alarm values, which may be incomplete */
171 err
= rtc_read_alarm_internal(rtc
, alarm
);
177 /* get the "after" timestamp, to detect wrapped fields */
178 err
= rtc_read_time(rtc
, &now
);
182 /* note that tm_sec is a "don't care" value here: */
183 } while ( before
.tm_min
!= now
.tm_min
184 || before
.tm_hour
!= now
.tm_hour
185 || before
.tm_mon
!= now
.tm_mon
186 || before
.tm_year
!= now
.tm_year
187 || before
.tm_isdst
!= now
.tm_isdst
);
189 /* Fill in any missing alarm fields using the timestamp */
190 if (alarm
->time
.tm_sec
== -1)
191 alarm
->time
.tm_sec
= now
.tm_sec
;
192 if (alarm
->time
.tm_min
== -1)
193 alarm
->time
.tm_min
= now
.tm_min
;
194 if (alarm
->time
.tm_hour
== -1)
195 alarm
->time
.tm_hour
= now
.tm_hour
;
196 if (alarm
->time
.tm_mday
== -1)
197 alarm
->time
.tm_mday
= now
.tm_mday
;
198 if (alarm
->time
.tm_mon
== -1)
199 alarm
->time
.tm_mon
= now
.tm_mon
;
200 if (alarm
->time
.tm_year
== -1)
201 alarm
->time
.tm_year
= now
.tm_year
;
204 EXPORT_SYMBOL_GPL(rtc_read_alarm
);
206 int rtc_set_alarm(struct rtc_device
*rtc
, struct rtc_wkalrm
*alarm
)
210 err
= rtc_valid_tm(&alarm
->time
);
214 err
= mutex_lock_interruptible(&rtc
->ops_lock
);
220 else if (!rtc
->ops
->set_alarm
)
223 err
= rtc
->ops
->set_alarm(rtc
->dev
.parent
, alarm
);
225 mutex_unlock(&rtc
->ops_lock
);
228 EXPORT_SYMBOL_GPL(rtc_set_alarm
);
231 * rtc_update_irq - report RTC periodic, alarm, and/or update irqs
232 * @rtc: the rtc device
233 * @num: how many irqs are being reported (usually one)
234 * @events: mask of RTC_IRQF with one or more of RTC_PF, RTC_AF, RTC_UF
235 * Context: in_interrupt(), irqs blocked
237 void rtc_update_irq(struct rtc_device
*rtc
,
238 unsigned long num
, unsigned long events
)
240 spin_lock(&rtc
->irq_lock
);
241 rtc
->irq_data
= (rtc
->irq_data
+ (num
<< 8)) | events
;
242 spin_unlock(&rtc
->irq_lock
);
244 spin_lock(&rtc
->irq_task_lock
);
246 rtc
->irq_task
->func(rtc
->irq_task
->private_data
);
247 spin_unlock(&rtc
->irq_task_lock
);
249 wake_up_interruptible(&rtc
->irq_queue
);
250 kill_fasync(&rtc
->async_queue
, SIGIO
, POLL_IN
);
252 EXPORT_SYMBOL_GPL(rtc_update_irq
);
254 static int __rtc_match(struct device
*dev
, void *data
)
256 char *name
= (char *)data
;
258 if (strncmp(dev
->bus_id
, name
, BUS_ID_SIZE
) == 0)
263 struct rtc_device
*rtc_class_open(char *name
)
266 struct rtc_device
*rtc
= NULL
;
268 dev
= class_find_device(rtc_class
, name
, __rtc_match
);
270 rtc
= to_rtc_device(dev
);
273 if (!try_module_get(rtc
->owner
)) {
281 EXPORT_SYMBOL_GPL(rtc_class_open
);
283 void rtc_class_close(struct rtc_device
*rtc
)
285 module_put(rtc
->owner
);
286 put_device(&rtc
->dev
);
288 EXPORT_SYMBOL_GPL(rtc_class_close
);
290 int rtc_irq_register(struct rtc_device
*rtc
, struct rtc_task
*task
)
294 if (task
== NULL
|| task
->func
== NULL
)
297 /* Cannot register while the char dev is in use */
298 if (test_and_set_bit_lock(RTC_DEV_BUSY
, &rtc
->flags
))
301 spin_lock_irq(&rtc
->irq_task_lock
);
302 if (rtc
->irq_task
== NULL
) {
303 rtc
->irq_task
= task
;
306 spin_unlock_irq(&rtc
->irq_task_lock
);
308 clear_bit_unlock(RTC_DEV_BUSY
, &rtc
->flags
);
312 EXPORT_SYMBOL_GPL(rtc_irq_register
);
314 void rtc_irq_unregister(struct rtc_device
*rtc
, struct rtc_task
*task
)
316 spin_lock_irq(&rtc
->irq_task_lock
);
317 if (rtc
->irq_task
== task
)
318 rtc
->irq_task
= NULL
;
319 spin_unlock_irq(&rtc
->irq_task_lock
);
321 EXPORT_SYMBOL_GPL(rtc_irq_unregister
);
324 * rtc_irq_set_state - enable/disable 2^N Hz periodic IRQs
325 * @rtc: the rtc device
326 * @task: currently registered with rtc_irq_register()
327 * @enabled: true to enable periodic IRQs
330 * Note that rtc_irq_set_freq() should previously have been used to
331 * specify the desired frequency of periodic IRQ task->func() callbacks.
333 int rtc_irq_set_state(struct rtc_device
*rtc
, struct rtc_task
*task
, int enabled
)
338 if (rtc
->ops
->irq_set_state
== NULL
)
341 spin_lock_irqsave(&rtc
->irq_task_lock
, flags
);
342 if (rtc
->irq_task
!= NULL
&& task
== NULL
)
344 if (rtc
->irq_task
!= task
)
346 spin_unlock_irqrestore(&rtc
->irq_task_lock
, flags
);
349 err
= rtc
->ops
->irq_set_state(rtc
->dev
.parent
, enabled
);
353 EXPORT_SYMBOL_GPL(rtc_irq_set_state
);
356 * rtc_irq_set_freq - set 2^N Hz periodic IRQ frequency for IRQ
357 * @rtc: the rtc device
358 * @task: currently registered with rtc_irq_register()
359 * @freq: positive frequency with which task->func() will be called
362 * Note that rtc_irq_set_state() is used to enable or disable the
365 int rtc_irq_set_freq(struct rtc_device
*rtc
, struct rtc_task
*task
, int freq
)
370 if (rtc
->ops
->irq_set_freq
== NULL
)
373 if (!is_power_of_2(freq
))
376 spin_lock_irqsave(&rtc
->irq_task_lock
, flags
);
377 if (rtc
->irq_task
!= NULL
&& task
== NULL
)
379 if (rtc
->irq_task
!= task
)
381 spin_unlock_irqrestore(&rtc
->irq_task_lock
, flags
);
384 err
= rtc
->ops
->irq_set_freq(rtc
->dev
.parent
, freq
);
386 rtc
->irq_freq
= freq
;
390 EXPORT_SYMBOL_GPL(rtc_irq_set_freq
);
This page took 0.037971 seconds and 6 git commands to generate.