rtc_irq_set_freq() requires power-of-two and associated kerneldoc
[linux-2.6/s3c2410-cpufreq.git] / drivers / rtc / interface.c
blob8adcab3c365303b49c9bbe9b91038408e2bbd789
1 /*
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)
19 int err;
21 err = mutex_lock_interruptible(&rtc->ops_lock);
22 if (err)
23 return -EBUSY;
25 if (!rtc->ops)
26 err = -ENODEV;
27 else if (!rtc->ops->read_time)
28 err = -EINVAL;
29 else {
30 memset(tm, 0, sizeof(struct rtc_time));
31 err = rtc->ops->read_time(rtc->dev.parent, tm);
34 mutex_unlock(&rtc->ops_lock);
35 return err;
37 EXPORT_SYMBOL_GPL(rtc_read_time);
39 int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm)
41 int err;
43 err = rtc_valid_tm(tm);
44 if (err != 0)
45 return err;
47 err = mutex_lock_interruptible(&rtc->ops_lock);
48 if (err)
49 return -EBUSY;
51 if (!rtc->ops)
52 err = -ENODEV;
53 else if (!rtc->ops->set_time)
54 err = -EINVAL;
55 else
56 err = rtc->ops->set_time(rtc->dev.parent, tm);
58 mutex_unlock(&rtc->ops_lock);
59 return err;
61 EXPORT_SYMBOL_GPL(rtc_set_time);
63 int rtc_set_mmss(struct rtc_device *rtc, unsigned long secs)
65 int err;
67 err = mutex_lock_interruptible(&rtc->ops_lock);
68 if (err)
69 return -EBUSY;
71 if (!rtc->ops)
72 err = -ENODEV;
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);
79 if (err == 0) {
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,
91 &new);
94 else
95 err = -EINVAL;
97 mutex_unlock(&rtc->ops_lock);
99 return err;
101 EXPORT_SYMBOL_GPL(rtc_set_mmss);
103 int rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
105 int err;
107 err = mutex_lock_interruptible(&rtc->ops_lock);
108 if (err)
109 return -EBUSY;
111 if (rtc->ops == NULL)
112 err = -ENODEV;
113 else if (!rtc->ops->read_alarm)
114 err = -EINVAL;
115 else {
116 memset(alarm, 0, sizeof(struct rtc_wkalrm));
117 err = rtc->ops->read_alarm(rtc->dev.parent, alarm);
120 mutex_unlock(&rtc->ops_lock);
121 return err;
123 EXPORT_SYMBOL_GPL(rtc_read_alarm);
125 int rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
127 int err;
129 err = rtc_valid_tm(&alarm->time);
130 if (err != 0)
131 return err;
133 err = mutex_lock_interruptible(&rtc->ops_lock);
134 if (err)
135 return -EBUSY;
137 if (!rtc->ops)
138 err = -ENODEV;
139 else if (!rtc->ops->set_alarm)
140 err = -EINVAL;
141 else
142 err = rtc->ops->set_alarm(rtc->dev.parent, alarm);
144 mutex_unlock(&rtc->ops_lock);
145 return err;
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);
164 if (rtc->irq_task)
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)
175 struct device *dev;
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);
182 if (dev)
183 rtc = to_rtc_device(dev);
184 break;
188 if (rtc) {
189 if (!try_module_get(rtc->owner)) {
190 put_device(dev);
191 rtc = NULL;
194 up(&rtc_class->sem);
196 return rtc;
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)
209 int retval = -EBUSY;
211 if (task == NULL || task->func == NULL)
212 return -EINVAL;
214 /* Cannot register while the char dev is in use */
215 if (!(mutex_trylock(&rtc->char_lock)))
216 return -EBUSY;
218 spin_lock_irq(&rtc->irq_task_lock);
219 if (rtc->irq_task == NULL) {
220 rtc->irq_task = task;
221 retval = 0;
223 spin_unlock_irq(&rtc->irq_task_lock);
225 mutex_unlock(&rtc->char_lock);
227 return retval;
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
245 * Context: any
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)
252 int err = 0;
253 unsigned long flags;
255 if (rtc->ops->irq_set_state == NULL)
256 return -ENXIO;
258 spin_lock_irqsave(&rtc->irq_task_lock, flags);
259 if (rtc->irq_task != NULL && task == NULL)
260 err = -EBUSY;
261 if (rtc->irq_task != task)
262 err = -EACCES;
263 spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
265 if (err == 0)
266 err = rtc->ops->irq_set_state(rtc->dev.parent, enabled);
268 return err;
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
277 * Context: any
279 * Note that rtc_irq_set_state() is used to enable or disable the
280 * periodic IRQs.
282 int rtc_irq_set_freq(struct rtc_device *rtc, struct rtc_task *task, int freq)
284 int err = 0;
285 unsigned long flags;
287 if (rtc->ops->irq_set_freq == NULL)
288 return -ENXIO;
290 if (!is_power_of_2(freq))
291 return -EINVAL;
293 spin_lock_irqsave(&rtc->irq_task_lock, flags);
294 if (rtc->irq_task != NULL && task == NULL)
295 err = -EBUSY;
296 if (rtc->irq_task != task)
297 err = -EACCES;
298 spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
300 if (err == 0) {
301 err = rtc->ops->irq_set_freq(rtc->dev.parent, freq);
302 if (err == 0)
303 rtc->irq_freq = freq;
305 return err;
307 EXPORT_SYMBOL_GPL(rtc_irq_set_freq);