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>
16 int rtc_read_time(struct class_device
*class_dev
, struct rtc_time
*tm
)
19 struct rtc_device
*rtc
= to_rtc_device(class_dev
);
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(class_dev
->dev
, tm
);
34 mutex_unlock(&rtc
->ops_lock
);
37 EXPORT_SYMBOL_GPL(rtc_read_time
);
39 int rtc_set_time(struct class_device
*class_dev
, struct rtc_time
*tm
)
42 struct rtc_device
*rtc
= to_rtc_device(class_dev
);
44 err
= rtc_valid_tm(tm
);
48 err
= mutex_lock_interruptible(&rtc
->ops_lock
);
54 else if (!rtc
->ops
->set_time
)
57 err
= rtc
->ops
->set_time(class_dev
->dev
, tm
);
59 mutex_unlock(&rtc
->ops_lock
);
62 EXPORT_SYMBOL_GPL(rtc_set_time
);
64 int rtc_set_mmss(struct class_device
*class_dev
, unsigned long secs
)
67 struct rtc_device
*rtc
= to_rtc_device(class_dev
);
69 err
= mutex_lock_interruptible(&rtc
->ops_lock
);
75 else if (rtc
->ops
->set_mmss
)
76 err
= rtc
->ops
->set_mmss(class_dev
->dev
, secs
);
77 else if (rtc
->ops
->read_time
&& rtc
->ops
->set_time
) {
78 struct rtc_time
new, old
;
80 err
= rtc
->ops
->read_time(class_dev
->dev
, &old
);
82 rtc_time_to_tm(secs
, &new);
85 * avoid writing when we're going to change the day of
86 * the month. We will retry in the next minute. This
87 * basically means that if the RTC must not drift
88 * by more than 1 minute in 11 minutes.
90 if (!((old
.tm_hour
== 23 && old
.tm_min
== 59) ||
91 (new.tm_hour
== 23 && new.tm_min
== 59)))
92 err
= rtc
->ops
->set_time(class_dev
->dev
, &new);
98 mutex_unlock(&rtc
->ops_lock
);
102 EXPORT_SYMBOL_GPL(rtc_set_mmss
);
104 int rtc_read_alarm(struct class_device
*class_dev
, struct rtc_wkalrm
*alarm
)
107 struct rtc_device
*rtc
= to_rtc_device(class_dev
);
109 err
= mutex_lock_interruptible(&rtc
->ops_lock
);
113 if (rtc
->ops
== NULL
)
115 else if (!rtc
->ops
->read_alarm
)
118 memset(alarm
, 0, sizeof(struct rtc_wkalrm
));
119 err
= rtc
->ops
->read_alarm(class_dev
->dev
, alarm
);
122 mutex_unlock(&rtc
->ops_lock
);
125 EXPORT_SYMBOL_GPL(rtc_read_alarm
);
127 int rtc_set_alarm(struct class_device
*class_dev
, struct rtc_wkalrm
*alarm
)
130 struct rtc_device
*rtc
= to_rtc_device(class_dev
);
132 err
= mutex_lock_interruptible(&rtc
->ops_lock
);
138 else if (!rtc
->ops
->set_alarm
)
141 err
= rtc
->ops
->set_alarm(class_dev
->dev
, alarm
);
143 mutex_unlock(&rtc
->ops_lock
);
146 EXPORT_SYMBOL_GPL(rtc_set_alarm
);
148 void rtc_update_irq(struct class_device
*class_dev
,
149 unsigned long num
, unsigned long events
)
151 struct rtc_device
*rtc
= to_rtc_device(class_dev
);
153 spin_lock(&rtc
->irq_lock
);
154 rtc
->irq_data
= (rtc
->irq_data
+ (num
<< 8)) | events
;
155 spin_unlock(&rtc
->irq_lock
);
157 spin_lock(&rtc
->irq_task_lock
);
159 rtc
->irq_task
->func(rtc
->irq_task
->private_data
);
160 spin_unlock(&rtc
->irq_task_lock
);
162 wake_up_interruptible(&rtc
->irq_queue
);
163 kill_fasync(&rtc
->async_queue
, SIGIO
, POLL_IN
);
165 EXPORT_SYMBOL_GPL(rtc_update_irq
);
167 struct class_device
*rtc_class_open(char *name
)
169 struct class_device
*class_dev
= NULL
,
172 down(&rtc_class
->sem
);
173 list_for_each_entry(class_dev_tmp
, &rtc_class
->children
, node
) {
174 if (strncmp(class_dev_tmp
->class_id
, name
, BUS_ID_SIZE
) == 0) {
175 class_dev
= class_dev_tmp
;
181 if (!try_module_get(to_rtc_device(class_dev
)->owner
))
188 EXPORT_SYMBOL_GPL(rtc_class_open
);
190 void rtc_class_close(struct class_device
*class_dev
)
192 module_put(to_rtc_device(class_dev
)->owner
);
194 EXPORT_SYMBOL_GPL(rtc_class_close
);
196 int rtc_irq_register(struct class_device
*class_dev
, struct rtc_task
*task
)
199 struct rtc_device
*rtc
= to_rtc_device(class_dev
);
201 if (task
== NULL
|| task
->func
== NULL
)
204 spin_lock(&rtc
->irq_task_lock
);
205 if (rtc
->irq_task
== NULL
) {
206 rtc
->irq_task
= task
;
209 spin_unlock(&rtc
->irq_task_lock
);
213 EXPORT_SYMBOL_GPL(rtc_irq_register
);
215 void rtc_irq_unregister(struct class_device
*class_dev
, struct rtc_task
*task
)
217 struct rtc_device
*rtc
= to_rtc_device(class_dev
);
219 spin_lock(&rtc
->irq_task_lock
);
220 if (rtc
->irq_task
== task
)
221 rtc
->irq_task
= NULL
;
222 spin_unlock(&rtc
->irq_task_lock
);
224 EXPORT_SYMBOL_GPL(rtc_irq_unregister
);
226 int rtc_irq_set_state(struct class_device
*class_dev
, struct rtc_task
*task
, int enabled
)
230 struct rtc_device
*rtc
= to_rtc_device(class_dev
);
232 spin_lock_irqsave(&rtc
->irq_task_lock
, flags
);
233 if (rtc
->irq_task
!= task
)
235 spin_unlock_irqrestore(&rtc
->irq_task_lock
, flags
);
238 err
= rtc
->ops
->irq_set_state(class_dev
->dev
, enabled
);
242 EXPORT_SYMBOL_GPL(rtc_irq_set_state
);
244 int rtc_irq_set_freq(struct class_device
*class_dev
, struct rtc_task
*task
, int freq
)
246 int err
= 0, tmp
= 0;
248 struct rtc_device
*rtc
= to_rtc_device(class_dev
);
250 /* allowed range is 2-8192 */
251 if (freq
< 2 || freq
> 8192)
254 FIXME: this does not belong here, will move where appropriate
255 at a later stage. It cannot hurt right now, trust me :)
256 if ((freq > rtc_max_user_freq) && (!capable(CAP_SYS_RESOURCE)))
259 /* check if freq is a power of 2 */
260 while (freq
> (1 << tmp
))
263 if (freq
!= (1 << tmp
))
266 spin_lock_irqsave(&rtc
->irq_task_lock
, flags
);
267 if (rtc
->irq_task
!= task
)
269 spin_unlock_irqrestore(&rtc
->irq_task_lock
, flags
);
272 err
= rtc
->ops
->irq_set_freq(class_dev
->dev
, freq
);
274 rtc
->irq_freq
= freq
;