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 int rtc_read_alarm(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
);
123 EXPORT_SYMBOL_GPL(rtc_read_alarm
);
125 int rtc_set_alarm(struct rtc_device
*rtc
, struct rtc_wkalrm
*alarm
)
129 err
= rtc_valid_tm(&alarm
->time
);
133 err
= mutex_lock_interruptible(&rtc
->ops_lock
);
139 else if (!rtc
->ops
->set_alarm
)
142 err
= rtc
->ops
->set_alarm(rtc
->dev
.parent
, alarm
);
144 mutex_unlock(&rtc
->ops_lock
);
147 EXPORT_SYMBOL_GPL(rtc_set_alarm
);
150 * rtc_update_irq - report RTC periodic, alarm, and/or update irqs
151 * @rtc: the rtc device
152 * @num: how many irqs are being reported (usually one)
153 * @events: mask of RTC_IRQF with one or more of RTC_PF, RTC_AF, RTC_UF
154 * Context: in_interrupt(), irqs blocked
156 void rtc_update_irq(struct rtc_device
*rtc
,
157 unsigned long num
, unsigned long events
)
159 spin_lock(&rtc
->irq_lock
);
160 rtc
->irq_data
= (rtc
->irq_data
+ (num
<< 8)) | events
;
161 spin_unlock(&rtc
->irq_lock
);
163 spin_lock(&rtc
->irq_task_lock
);
165 rtc
->irq_task
->func(rtc
->irq_task
->private_data
);
166 spin_unlock(&rtc
->irq_task_lock
);
168 wake_up_interruptible(&rtc
->irq_queue
);
169 kill_fasync(&rtc
->async_queue
, SIGIO
, POLL_IN
);
171 EXPORT_SYMBOL_GPL(rtc_update_irq
);
173 struct rtc_device
*rtc_class_open(char *name
)
176 struct rtc_device
*rtc
= NULL
;
178 down(&rtc_class
->sem
);
179 list_for_each_entry(dev
, &rtc_class
->devices
, node
) {
180 if (strncmp(dev
->bus_id
, name
, BUS_ID_SIZE
) == 0) {
181 dev
= get_device(dev
);
183 rtc
= to_rtc_device(dev
);
189 if (!try_module_get(rtc
->owner
)) {
198 EXPORT_SYMBOL_GPL(rtc_class_open
);
200 void rtc_class_close(struct rtc_device
*rtc
)
202 module_put(rtc
->owner
);
203 put_device(&rtc
->dev
);
205 EXPORT_SYMBOL_GPL(rtc_class_close
);
207 int rtc_irq_register(struct rtc_device
*rtc
, struct rtc_task
*task
)
211 if (task
== NULL
|| task
->func
== NULL
)
214 /* Cannot register while the char dev is in use */
215 if (!(mutex_trylock(&rtc
->char_lock
)))
218 spin_lock_irq(&rtc
->irq_task_lock
);
219 if (rtc
->irq_task
== NULL
) {
220 rtc
->irq_task
= task
;
223 spin_unlock_irq(&rtc
->irq_task_lock
);
225 mutex_unlock(&rtc
->char_lock
);
229 EXPORT_SYMBOL_GPL(rtc_irq_register
);
231 void rtc_irq_unregister(struct rtc_device
*rtc
, struct rtc_task
*task
)
233 spin_lock_irq(&rtc
->irq_task_lock
);
234 if (rtc
->irq_task
== task
)
235 rtc
->irq_task
= NULL
;
236 spin_unlock_irq(&rtc
->irq_task_lock
);
238 EXPORT_SYMBOL_GPL(rtc_irq_unregister
);
241 * rtc_irq_set_state - enable/disable 2^N Hz periodic IRQs
242 * @rtc: the rtc device
243 * @task: currently registered with rtc_irq_register()
244 * @enabled: true to enable periodic IRQs
247 * Note that rtc_irq_set_freq() should previously have been used to
248 * specify the desired frequency of periodic IRQ task->func() callbacks.
250 int rtc_irq_set_state(struct rtc_device
*rtc
, struct rtc_task
*task
, int enabled
)
255 if (rtc
->ops
->irq_set_state
== NULL
)
258 spin_lock_irqsave(&rtc
->irq_task_lock
, flags
);
259 if (rtc
->irq_task
!= NULL
&& task
== NULL
)
261 if (rtc
->irq_task
!= task
)
263 spin_unlock_irqrestore(&rtc
->irq_task_lock
, flags
);
266 err
= rtc
->ops
->irq_set_state(rtc
->dev
.parent
, enabled
);
270 EXPORT_SYMBOL_GPL(rtc_irq_set_state
);
273 * rtc_irq_set_freq - set 2^N Hz periodic IRQ frequency for IRQ
274 * @rtc: the rtc device
275 * @task: currently registered with rtc_irq_register()
276 * @freq: positive frequency with which task->func() will be called
279 * Note that rtc_irq_set_state() is used to enable or disable the
282 int rtc_irq_set_freq(struct rtc_device
*rtc
, struct rtc_task
*task
, int freq
)
287 if (rtc
->ops
->irq_set_freq
== NULL
)
290 if (!is_power_of_2(freq
))
293 spin_lock_irqsave(&rtc
->irq_task_lock
, flags
);
294 if (rtc
->irq_task
!= NULL
&& task
== NULL
)
296 if (rtc
->irq_task
!= task
)
298 spin_unlock_irqrestore(&rtc
->irq_task_lock
, flags
);
301 err
= rtc
->ops
->irq_set_freq(rtc
->dev
.parent
, freq
);
303 rtc
->irq_freq
= freq
;
307 EXPORT_SYMBOL_GPL(rtc_irq_set_freq
);