blob: f1ba2c696528c79d5793b28f71bfe17c0f3d5ba3 [file] [log] [blame]
Alessandro Zummo0c86edc2006-03-27 01:16:37 -08001/*
2 * RTC subsystem, interface functions
3 *
4 * Copyright (C) 2005 Tower Technologies
5 * Author: Alessandro Zummo <a.zummo@towertech.it>
6 *
7 * based on arch/arm/common/rtctime.c
8 *
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.
12*/
13
14#include <linux/rtc.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040015#include <linux/sched.h>
David Brownell97144c62007-10-16 01:28:16 -070016#include <linux/log2.h>
John Stultz6610e082010-09-23 15:07:34 -070017#include <linux/workqueue.h>
18
John Stultzaa0be0f2011-01-20 15:26:12 -080019static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer);
20static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer);
21
John Stultz6610e082010-09-23 15:07:34 -070022static int __rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm)
23{
24 int err;
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);
32 }
33 return err;
34}
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080035
David Brownellab6a2d72007-05-08 00:33:30 -070036int rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080037{
38 int err;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080039
40 err = mutex_lock_interruptible(&rtc->ops_lock);
41 if (err)
David Brownellb68bb262008-07-29 22:33:30 -070042 return err;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080043
John Stultz6610e082010-09-23 15:07:34 -070044 err = __rtc_read_time(rtc, tm);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080045 mutex_unlock(&rtc->ops_lock);
46 return err;
47}
48EXPORT_SYMBOL_GPL(rtc_read_time);
49
David Brownellab6a2d72007-05-08 00:33:30 -070050int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080051{
52 int err;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080053
54 err = rtc_valid_tm(tm);
55 if (err != 0)
56 return err;
57
58 err = mutex_lock_interruptible(&rtc->ops_lock);
59 if (err)
David Brownellb68bb262008-07-29 22:33:30 -070060 return err;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080061
62 if (!rtc->ops)
63 err = -ENODEV;
Alessandro Zummobbccf832009-01-06 14:42:21 -080064 else if (rtc->ops->set_time)
David Brownellcd966202007-05-08 00:33:40 -070065 err = rtc->ops->set_time(rtc->dev.parent, tm);
Alessandro Zummobbccf832009-01-06 14:42:21 -080066 else if (rtc->ops->set_mmss) {
67 unsigned long secs;
68 err = rtc_tm_to_time(tm, &secs);
69 if (err == 0)
70 err = rtc->ops->set_mmss(rtc->dev.parent, secs);
71 } else
72 err = -EINVAL;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080073
74 mutex_unlock(&rtc->ops_lock);
75 return err;
76}
77EXPORT_SYMBOL_GPL(rtc_set_time);
78
David Brownellab6a2d72007-05-08 00:33:30 -070079int rtc_set_mmss(struct rtc_device *rtc, unsigned long secs)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080080{
81 int err;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080082
83 err = mutex_lock_interruptible(&rtc->ops_lock);
84 if (err)
David Brownellb68bb262008-07-29 22:33:30 -070085 return err;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080086
87 if (!rtc->ops)
88 err = -ENODEV;
89 else if (rtc->ops->set_mmss)
David Brownellcd966202007-05-08 00:33:40 -070090 err = rtc->ops->set_mmss(rtc->dev.parent, secs);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080091 else if (rtc->ops->read_time && rtc->ops->set_time) {
92 struct rtc_time new, old;
93
David Brownellcd966202007-05-08 00:33:40 -070094 err = rtc->ops->read_time(rtc->dev.parent, &old);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -080095 if (err == 0) {
96 rtc_time_to_tm(secs, &new);
97
98 /*
99 * avoid writing when we're going to change the day of
100 * the month. We will retry in the next minute. This
101 * basically means that if the RTC must not drift
102 * by more than 1 minute in 11 minutes.
103 */
104 if (!((old.tm_hour == 23 && old.tm_min == 59) ||
105 (new.tm_hour == 23 && new.tm_min == 59)))
David Brownellcd966202007-05-08 00:33:40 -0700106 err = rtc->ops->set_time(rtc->dev.parent,
David Brownellab6a2d72007-05-08 00:33:30 -0700107 &new);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800108 }
109 }
110 else
111 err = -EINVAL;
112
113 mutex_unlock(&rtc->ops_lock);
114
115 return err;
116}
117EXPORT_SYMBOL_GPL(rtc_set_mmss);
118
John Stultz6610e082010-09-23 15:07:34 -0700119int rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800120{
121 int err;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800122
123 err = mutex_lock_interruptible(&rtc->ops_lock);
124 if (err)
David Brownellb68bb262008-07-29 22:33:30 -0700125 return err;
John Stultz6610e082010-09-23 15:07:34 -0700126 alarm->enabled = rtc->aie_timer.enabled;
127 if (alarm->enabled)
128 alarm->time = rtc_ktime_to_tm(rtc->aie_timer.node.expires);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800129 mutex_unlock(&rtc->ops_lock);
Mark Lord0e36a9a2007-10-16 01:28:21 -0700130
Mark Lord0e36a9a2007-10-16 01:28:21 -0700131 return 0;
132}
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800133EXPORT_SYMBOL_GPL(rtc_read_alarm);
134
John Stultz6610e082010-09-23 15:07:34 -0700135int __rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
136{
137 struct rtc_time tm;
138 long now, scheduled;
139 int err;
140
141 err = rtc_valid_tm(&alarm->time);
142 if (err)
143 return err;
144 rtc_tm_to_time(&alarm->time, &scheduled);
145
146 /* Make sure we're not setting alarms in the past */
147 err = __rtc_read_time(rtc, &tm);
148 rtc_tm_to_time(&tm, &now);
149 if (scheduled <= now)
150 return -ETIME;
151 /*
152 * XXX - We just checked to make sure the alarm time is not
153 * in the past, but there is still a race window where if
154 * the is alarm set for the next second and the second ticks
155 * over right here, before we set the alarm.
156 */
157
158 if (!rtc->ops)
159 err = -ENODEV;
160 else if (!rtc->ops->set_alarm)
161 err = -EINVAL;
162 else
163 err = rtc->ops->set_alarm(rtc->dev.parent, alarm);
164
165 return err;
166}
167
David Brownellab6a2d72007-05-08 00:33:30 -0700168int rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800169{
170 int err;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800171
David Brownellf8245c22007-05-08 00:34:07 -0700172 err = rtc_valid_tm(&alarm->time);
173 if (err != 0)
174 return err;
175
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800176 err = mutex_lock_interruptible(&rtc->ops_lock);
177 if (err)
David Brownellb68bb262008-07-29 22:33:30 -0700178 return err;
John Stultz6610e082010-09-23 15:07:34 -0700179 if (rtc->aie_timer.enabled) {
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100180 rtc_timer_remove(rtc, &rtc->aie_timer);
John Stultz6610e082010-09-23 15:07:34 -0700181 }
182 rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time);
183 rtc->aie_timer.period = ktime_set(0, 0);
184 if (alarm->enabled) {
John Stultzaa0be0f2011-01-20 15:26:12 -0800185 err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
John Stultz6610e082010-09-23 15:07:34 -0700186 }
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800187 mutex_unlock(&rtc->ops_lock);
John Stultzaa0be0f2011-01-20 15:26:12 -0800188 return err;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800189}
190EXPORT_SYMBOL_GPL(rtc_set_alarm);
191
Alessandro Zummo099e6572009-01-04 12:00:54 -0800192int rtc_alarm_irq_enable(struct rtc_device *rtc, unsigned int enabled)
193{
194 int err = mutex_lock_interruptible(&rtc->ops_lock);
195 if (err)
196 return err;
197
John Stultz6610e082010-09-23 15:07:34 -0700198 if (rtc->aie_timer.enabled != enabled) {
John Stultzaa0be0f2011-01-20 15:26:12 -0800199 if (enabled)
200 err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
201 else
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100202 rtc_timer_remove(rtc, &rtc->aie_timer);
John Stultz6610e082010-09-23 15:07:34 -0700203 }
204
John Stultzaa0be0f2011-01-20 15:26:12 -0800205 if (err)
206 return err;
207
Alessandro Zummo099e6572009-01-04 12:00:54 -0800208 if (!rtc->ops)
209 err = -ENODEV;
210 else if (!rtc->ops->alarm_irq_enable)
211 err = -EINVAL;
212 else
213 err = rtc->ops->alarm_irq_enable(rtc->dev.parent, enabled);
214
215 mutex_unlock(&rtc->ops_lock);
216 return err;
217}
218EXPORT_SYMBOL_GPL(rtc_alarm_irq_enable);
219
220int rtc_update_irq_enable(struct rtc_device *rtc, unsigned int enabled)
221{
222 int err = mutex_lock_interruptible(&rtc->ops_lock);
223 if (err)
224 return err;
225
John Stultz6610e082010-09-23 15:07:34 -0700226 /* make sure we're changing state */
227 if (rtc->uie_rtctimer.enabled == enabled)
228 goto out;
229
230 if (enabled) {
231 struct rtc_time tm;
232 ktime_t now, onesec;
233
234 __rtc_read_time(rtc, &tm);
235 onesec = ktime_set(1, 0);
236 now = rtc_tm_to_ktime(tm);
237 rtc->uie_rtctimer.node.expires = ktime_add(now, onesec);
238 rtc->uie_rtctimer.period = ktime_set(1, 0);
John Stultzaa0be0f2011-01-20 15:26:12 -0800239 err = rtc_timer_enqueue(rtc, &rtc->uie_rtctimer);
240 } else
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100241 rtc_timer_remove(rtc, &rtc->uie_rtctimer);
Alessandro Zummo099e6572009-01-04 12:00:54 -0800242
John Stultz6610e082010-09-23 15:07:34 -0700243out:
Alessandro Zummo099e6572009-01-04 12:00:54 -0800244 mutex_unlock(&rtc->ops_lock);
Alessandro Zummo099e6572009-01-04 12:00:54 -0800245 return err;
John Stultz6610e082010-09-23 15:07:34 -0700246
Alessandro Zummo099e6572009-01-04 12:00:54 -0800247}
248EXPORT_SYMBOL_GPL(rtc_update_irq_enable);
249
John Stultz6610e082010-09-23 15:07:34 -0700250
David Brownelld728b1e2006-11-25 11:09:28 -0800251/**
John Stultz6610e082010-09-23 15:07:34 -0700252 * rtc_handle_legacy_irq - AIE, UIE and PIE event hook
253 * @rtc: pointer to the rtc device
254 *
255 * This function is called when an AIE, UIE or PIE mode interrupt
256 * has occured (or been emulated).
257 *
258 * Triggers the registered irq_task function callback.
259 */
260static void rtc_handle_legacy_irq(struct rtc_device *rtc, int num, int mode)
261{
262 unsigned long flags;
263
264 /* mark one irq of the appropriate mode */
265 spin_lock_irqsave(&rtc->irq_lock, flags);
266 rtc->irq_data = (rtc->irq_data + (num << 8)) | (RTC_IRQF|mode);
267 spin_unlock_irqrestore(&rtc->irq_lock, flags);
268
269 /* call the task func */
270 spin_lock_irqsave(&rtc->irq_task_lock, flags);
271 if (rtc->irq_task)
272 rtc->irq_task->func(rtc->irq_task->private_data);
273 spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
274
275 wake_up_interruptible(&rtc->irq_queue);
276 kill_fasync(&rtc->async_queue, SIGIO, POLL_IN);
277}
278
279
280/**
281 * rtc_aie_update_irq - AIE mode rtctimer hook
282 * @private: pointer to the rtc_device
283 *
284 * This functions is called when the aie_timer expires.
285 */
286void rtc_aie_update_irq(void *private)
287{
288 struct rtc_device *rtc = (struct rtc_device *)private;
289 rtc_handle_legacy_irq(rtc, 1, RTC_AF);
290}
291
292
293/**
294 * rtc_uie_update_irq - UIE mode rtctimer hook
295 * @private: pointer to the rtc_device
296 *
297 * This functions is called when the uie_timer expires.
298 */
299void rtc_uie_update_irq(void *private)
300{
301 struct rtc_device *rtc = (struct rtc_device *)private;
302 rtc_handle_legacy_irq(rtc, 1, RTC_UF);
303}
304
305
306/**
307 * rtc_pie_update_irq - PIE mode hrtimer hook
308 * @timer: pointer to the pie mode hrtimer
309 *
310 * This function is used to emulate PIE mode interrupts
311 * using an hrtimer. This function is called when the periodic
312 * hrtimer expires.
313 */
314enum hrtimer_restart rtc_pie_update_irq(struct hrtimer *timer)
315{
316 struct rtc_device *rtc;
317 ktime_t period;
318 int count;
319 rtc = container_of(timer, struct rtc_device, pie_timer);
320
321 period = ktime_set(0, NSEC_PER_SEC/rtc->irq_freq);
322 count = hrtimer_forward_now(timer, period);
323
324 rtc_handle_legacy_irq(rtc, count, RTC_PF);
325
326 return HRTIMER_RESTART;
327}
328
329/**
330 * rtc_update_irq - Triggered when a RTC interrupt occurs.
David Brownellab6a2d72007-05-08 00:33:30 -0700331 * @rtc: the rtc device
David Brownelld728b1e2006-11-25 11:09:28 -0800332 * @num: how many irqs are being reported (usually one)
333 * @events: mask of RTC_IRQF with one or more of RTC_PF, RTC_AF, RTC_UF
Atsushi Nemotoe6229be2009-06-18 16:49:09 -0700334 * Context: any
David Brownelld728b1e2006-11-25 11:09:28 -0800335 */
David Brownellab6a2d72007-05-08 00:33:30 -0700336void rtc_update_irq(struct rtc_device *rtc,
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800337 unsigned long num, unsigned long events)
338{
John Stultz6610e082010-09-23 15:07:34 -0700339 schedule_work(&rtc->irqwork);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800340}
341EXPORT_SYMBOL_GPL(rtc_update_irq);
342
Dave Young71da8902008-01-22 14:00:34 +0800343static int __rtc_match(struct device *dev, void *data)
344{
345 char *name = (char *)data;
346
Kay Sieversd4afc762009-01-06 14:42:11 -0800347 if (strcmp(dev_name(dev), name) == 0)
Dave Young71da8902008-01-22 14:00:34 +0800348 return 1;
349 return 0;
350}
351
David Brownellab6a2d72007-05-08 00:33:30 -0700352struct rtc_device *rtc_class_open(char *name)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800353{
David Brownellcd966202007-05-08 00:33:40 -0700354 struct device *dev;
David Brownellab6a2d72007-05-08 00:33:30 -0700355 struct rtc_device *rtc = NULL;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800356
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -0400357 dev = class_find_device(rtc_class, NULL, name, __rtc_match);
Dave Young71da8902008-01-22 14:00:34 +0800358 if (dev)
359 rtc = to_rtc_device(dev);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800360
David Brownellab6a2d72007-05-08 00:33:30 -0700361 if (rtc) {
362 if (!try_module_get(rtc->owner)) {
David Brownellcd966202007-05-08 00:33:40 -0700363 put_device(dev);
David Brownellab6a2d72007-05-08 00:33:30 -0700364 rtc = NULL;
365 }
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800366 }
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800367
David Brownellab6a2d72007-05-08 00:33:30 -0700368 return rtc;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800369}
370EXPORT_SYMBOL_GPL(rtc_class_open);
371
David Brownellab6a2d72007-05-08 00:33:30 -0700372void rtc_class_close(struct rtc_device *rtc)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800373{
David Brownellab6a2d72007-05-08 00:33:30 -0700374 module_put(rtc->owner);
David Brownellcd966202007-05-08 00:33:40 -0700375 put_device(&rtc->dev);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800376}
377EXPORT_SYMBOL_GPL(rtc_class_close);
378
David Brownellab6a2d72007-05-08 00:33:30 -0700379int rtc_irq_register(struct rtc_device *rtc, struct rtc_task *task)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800380{
381 int retval = -EBUSY;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800382
383 if (task == NULL || task->func == NULL)
384 return -EINVAL;
385
Alessandro Zummod691eb92007-10-16 01:28:15 -0700386 /* Cannot register while the char dev is in use */
Jiri Kosina372a3022007-12-04 23:45:05 -0800387 if (test_and_set_bit_lock(RTC_DEV_BUSY, &rtc->flags))
Alessandro Zummod691eb92007-10-16 01:28:15 -0700388 return -EBUSY;
389
David Brownelld728b1e2006-11-25 11:09:28 -0800390 spin_lock_irq(&rtc->irq_task_lock);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800391 if (rtc->irq_task == NULL) {
392 rtc->irq_task = task;
393 retval = 0;
394 }
David Brownelld728b1e2006-11-25 11:09:28 -0800395 spin_unlock_irq(&rtc->irq_task_lock);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800396
Jiri Kosina372a3022007-12-04 23:45:05 -0800397 clear_bit_unlock(RTC_DEV_BUSY, &rtc->flags);
Alessandro Zummod691eb92007-10-16 01:28:15 -0700398
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800399 return retval;
400}
401EXPORT_SYMBOL_GPL(rtc_irq_register);
402
David Brownellab6a2d72007-05-08 00:33:30 -0700403void rtc_irq_unregister(struct rtc_device *rtc, struct rtc_task *task)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800404{
David Brownelld728b1e2006-11-25 11:09:28 -0800405 spin_lock_irq(&rtc->irq_task_lock);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800406 if (rtc->irq_task == task)
407 rtc->irq_task = NULL;
David Brownelld728b1e2006-11-25 11:09:28 -0800408 spin_unlock_irq(&rtc->irq_task_lock);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800409}
410EXPORT_SYMBOL_GPL(rtc_irq_unregister);
411
David Brownell97144c62007-10-16 01:28:16 -0700412/**
413 * rtc_irq_set_state - enable/disable 2^N Hz periodic IRQs
414 * @rtc: the rtc device
415 * @task: currently registered with rtc_irq_register()
416 * @enabled: true to enable periodic IRQs
417 * Context: any
418 *
419 * Note that rtc_irq_set_freq() should previously have been used to
420 * specify the desired frequency of periodic IRQ task->func() callbacks.
421 */
David Brownellab6a2d72007-05-08 00:33:30 -0700422int rtc_irq_set_state(struct rtc_device *rtc, struct rtc_task *task, int enabled)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800423{
424 int err = 0;
425 unsigned long flags;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800426
427 spin_lock_irqsave(&rtc->irq_task_lock, flags);
Alessandro Zummod691eb92007-10-16 01:28:15 -0700428 if (rtc->irq_task != NULL && task == NULL)
429 err = -EBUSY;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800430 if (rtc->irq_task != task)
Alessandro Zummod691eb92007-10-16 01:28:15 -0700431 err = -EACCES;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800432
John Stultz6610e082010-09-23 15:07:34 -0700433 if (enabled) {
434 ktime_t period = ktime_set(0, NSEC_PER_SEC/rtc->irq_freq);
435 hrtimer_start(&rtc->pie_timer, period, HRTIMER_MODE_REL);
436 } else {
437 hrtimer_cancel(&rtc->pie_timer);
438 }
439 rtc->pie_enabled = enabled;
440 spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800441
442 return err;
443}
444EXPORT_SYMBOL_GPL(rtc_irq_set_state);
445
David Brownell97144c62007-10-16 01:28:16 -0700446/**
447 * rtc_irq_set_freq - set 2^N Hz periodic IRQ frequency for IRQ
448 * @rtc: the rtc device
449 * @task: currently registered with rtc_irq_register()
450 * @freq: positive frequency with which task->func() will be called
451 * Context: any
452 *
453 * Note that rtc_irq_set_state() is used to enable or disable the
454 * periodic IRQs.
455 */
David Brownellab6a2d72007-05-08 00:33:30 -0700456int rtc_irq_set_freq(struct rtc_device *rtc, struct rtc_task *task, int freq)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800457{
Alessandro Zummo56f10c62006-06-25 05:48:20 -0700458 int err = 0;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800459 unsigned long flags;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800460
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800461 spin_lock_irqsave(&rtc->irq_task_lock, flags);
Alessandro Zummod691eb92007-10-16 01:28:15 -0700462 if (rtc->irq_task != NULL && task == NULL)
463 err = -EBUSY;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800464 if (rtc->irq_task != task)
Alessandro Zummod691eb92007-10-16 01:28:15 -0700465 err = -EACCES;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800466 if (err == 0) {
John Stultz6610e082010-09-23 15:07:34 -0700467 rtc->irq_freq = freq;
468 if (rtc->pie_enabled) {
469 ktime_t period;
470 hrtimer_cancel(&rtc->pie_timer);
471 period = ktime_set(0, NSEC_PER_SEC/rtc->irq_freq);
472 hrtimer_start(&rtc->pie_timer, period,
473 HRTIMER_MODE_REL);
474 }
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800475 }
John Stultz6610e082010-09-23 15:07:34 -0700476 spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800477 return err;
478}
David Brownell2601a462006-11-25 11:09:27 -0800479EXPORT_SYMBOL_GPL(rtc_irq_set_freq);
John Stultz6610e082010-09-23 15:07:34 -0700480
481/**
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100482 * rtc_timer_enqueue - Adds a rtc_timer to the rtc_device timerqueue
John Stultz6610e082010-09-23 15:07:34 -0700483 * @rtc rtc device
484 * @timer timer being added.
485 *
486 * Enqueues a timer onto the rtc devices timerqueue and sets
487 * the next alarm event appropriately.
488 *
John Stultzaa0be0f2011-01-20 15:26:12 -0800489 * Sets the enabled bit on the added timer.
490 *
John Stultz6610e082010-09-23 15:07:34 -0700491 * Must hold ops_lock for proper serialization of timerqueue
492 */
John Stultzaa0be0f2011-01-20 15:26:12 -0800493static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer)
John Stultz6610e082010-09-23 15:07:34 -0700494{
John Stultzaa0be0f2011-01-20 15:26:12 -0800495 timer->enabled = 1;
John Stultz6610e082010-09-23 15:07:34 -0700496 timerqueue_add(&rtc->timerqueue, &timer->node);
497 if (&timer->node == timerqueue_getnext(&rtc->timerqueue)) {
498 struct rtc_wkalrm alarm;
499 int err;
500 alarm.time = rtc_ktime_to_tm(timer->node.expires);
501 alarm.enabled = 1;
502 err = __rtc_set_alarm(rtc, &alarm);
503 if (err == -ETIME)
504 schedule_work(&rtc->irqwork);
John Stultzaa0be0f2011-01-20 15:26:12 -0800505 else if (err) {
506 timerqueue_del(&rtc->timerqueue, &timer->node);
507 timer->enabled = 0;
508 return err;
509 }
John Stultz6610e082010-09-23 15:07:34 -0700510 }
John Stultzaa0be0f2011-01-20 15:26:12 -0800511 return 0;
John Stultz6610e082010-09-23 15:07:34 -0700512}
513
514/**
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100515 * rtc_timer_remove - Removes a rtc_timer from the rtc_device timerqueue
John Stultz6610e082010-09-23 15:07:34 -0700516 * @rtc rtc device
517 * @timer timer being removed.
518 *
519 * Removes a timer onto the rtc devices timerqueue and sets
520 * the next alarm event appropriately.
521 *
John Stultzaa0be0f2011-01-20 15:26:12 -0800522 * Clears the enabled bit on the removed timer.
523 *
John Stultz6610e082010-09-23 15:07:34 -0700524 * Must hold ops_lock for proper serialization of timerqueue
525 */
John Stultzaa0be0f2011-01-20 15:26:12 -0800526static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer)
John Stultz6610e082010-09-23 15:07:34 -0700527{
528 struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue);
529 timerqueue_del(&rtc->timerqueue, &timer->node);
John Stultzaa0be0f2011-01-20 15:26:12 -0800530 timer->enabled = 0;
John Stultz6610e082010-09-23 15:07:34 -0700531 if (next == &timer->node) {
532 struct rtc_wkalrm alarm;
533 int err;
534 next = timerqueue_getnext(&rtc->timerqueue);
535 if (!next)
536 return;
537 alarm.time = rtc_ktime_to_tm(next->expires);
538 alarm.enabled = 1;
539 err = __rtc_set_alarm(rtc, &alarm);
540 if (err == -ETIME)
541 schedule_work(&rtc->irqwork);
542 }
543}
544
545/**
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100546 * rtc_timer_do_work - Expires rtc timers
John Stultz6610e082010-09-23 15:07:34 -0700547 * @rtc rtc device
548 * @timer timer being removed.
549 *
550 * Expires rtc timers. Reprograms next alarm event if needed.
551 * Called via worktask.
552 *
553 * Serializes access to timerqueue via ops_lock mutex
554 */
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100555void rtc_timer_do_work(struct work_struct *work)
John Stultz6610e082010-09-23 15:07:34 -0700556{
557 struct rtc_timer *timer;
558 struct timerqueue_node *next;
559 ktime_t now;
560 struct rtc_time tm;
561
562 struct rtc_device *rtc =
563 container_of(work, struct rtc_device, irqwork);
564
565 mutex_lock(&rtc->ops_lock);
566again:
567 __rtc_read_time(rtc, &tm);
568 now = rtc_tm_to_ktime(tm);
569 while ((next = timerqueue_getnext(&rtc->timerqueue))) {
570 if (next->expires.tv64 > now.tv64)
571 break;
572
573 /* expire timer */
574 timer = container_of(next, struct rtc_timer, node);
575 timerqueue_del(&rtc->timerqueue, &timer->node);
576 timer->enabled = 0;
577 if (timer->task.func)
578 timer->task.func(timer->task.private_data);
579
580 /* Re-add/fwd periodic timers */
581 if (ktime_to_ns(timer->period)) {
582 timer->node.expires = ktime_add(timer->node.expires,
583 timer->period);
584 timer->enabled = 1;
585 timerqueue_add(&rtc->timerqueue, &timer->node);
586 }
587 }
588
589 /* Set next alarm */
590 if (next) {
591 struct rtc_wkalrm alarm;
592 int err;
593 alarm.time = rtc_ktime_to_tm(next->expires);
594 alarm.enabled = 1;
595 err = __rtc_set_alarm(rtc, &alarm);
596 if (err == -ETIME)
597 goto again;
598 }
599
600 mutex_unlock(&rtc->ops_lock);
601}
602
603
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100604/* rtc_timer_init - Initializes an rtc_timer
John Stultz6610e082010-09-23 15:07:34 -0700605 * @timer: timer to be intiialized
606 * @f: function pointer to be called when timer fires
607 * @data: private data passed to function pointer
608 *
609 * Kernel interface to initializing an rtc_timer.
610 */
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100611void rtc_timer_init(struct rtc_timer *timer, void (*f)(void* p), void* data)
John Stultz6610e082010-09-23 15:07:34 -0700612{
613 timerqueue_init(&timer->node);
614 timer->enabled = 0;
615 timer->task.func = f;
616 timer->task.private_data = data;
617}
618
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100619/* rtc_timer_start - Sets an rtc_timer to fire in the future
John Stultz6610e082010-09-23 15:07:34 -0700620 * @ rtc: rtc device to be used
621 * @ timer: timer being set
622 * @ expires: time at which to expire the timer
623 * @ period: period that the timer will recur
624 *
625 * Kernel interface to set an rtc_timer
626 */
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100627int rtc_timer_start(struct rtc_device *rtc, struct rtc_timer* timer,
John Stultz6610e082010-09-23 15:07:34 -0700628 ktime_t expires, ktime_t period)
629{
630 int ret = 0;
631 mutex_lock(&rtc->ops_lock);
632 if (timer->enabled)
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100633 rtc_timer_remove(rtc, timer);
John Stultz6610e082010-09-23 15:07:34 -0700634
635 timer->node.expires = expires;
636 timer->period = period;
637
John Stultzaa0be0f2011-01-20 15:26:12 -0800638 ret = rtc_timer_enqueue(rtc, timer);
John Stultz6610e082010-09-23 15:07:34 -0700639
640 mutex_unlock(&rtc->ops_lock);
641 return ret;
642}
643
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100644/* rtc_timer_cancel - Stops an rtc_timer
John Stultz6610e082010-09-23 15:07:34 -0700645 * @ rtc: rtc device to be used
646 * @ timer: timer being set
647 *
648 * Kernel interface to cancel an rtc_timer
649 */
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100650int rtc_timer_cancel(struct rtc_device *rtc, struct rtc_timer* timer)
John Stultz6610e082010-09-23 15:07:34 -0700651{
652 int ret = 0;
653 mutex_lock(&rtc->ops_lock);
654 if (timer->enabled)
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100655 rtc_timer_remove(rtc, timer);
John Stultz6610e082010-09-23 15:07:34 -0700656 mutex_unlock(&rtc->ops_lock);
657 return ret;
658}
659
660