blob: 925006d331099699e27b26012050b81cd3acbcce [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 Stultzd5553a52011-01-20 15:26:13 -0800126 if (rtc->ops == NULL)
127 err = -ENODEV;
128 else if (!rtc->ops->read_alarm)
129 err = -EINVAL;
130 else {
131 memset(alarm, 0, sizeof(struct rtc_wkalrm));
132 alarm->enabled = rtc->aie_timer.enabled;
John Stultz6610e082010-09-23 15:07:34 -0700133 alarm->time = rtc_ktime_to_tm(rtc->aie_timer.node.expires);
John Stultzd5553a52011-01-20 15:26:13 -0800134 }
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800135 mutex_unlock(&rtc->ops_lock);
Mark Lord0e36a9a2007-10-16 01:28:21 -0700136
John Stultzd5553a52011-01-20 15:26:13 -0800137 return err;
Mark Lord0e36a9a2007-10-16 01:28:21 -0700138}
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800139EXPORT_SYMBOL_GPL(rtc_read_alarm);
140
John Stultz6610e082010-09-23 15:07:34 -0700141int __rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
142{
143 struct rtc_time tm;
144 long now, scheduled;
145 int err;
146
147 err = rtc_valid_tm(&alarm->time);
148 if (err)
149 return err;
150 rtc_tm_to_time(&alarm->time, &scheduled);
151
152 /* Make sure we're not setting alarms in the past */
153 err = __rtc_read_time(rtc, &tm);
154 rtc_tm_to_time(&tm, &now);
155 if (scheduled <= now)
156 return -ETIME;
157 /*
158 * XXX - We just checked to make sure the alarm time is not
159 * in the past, but there is still a race window where if
160 * the is alarm set for the next second and the second ticks
161 * over right here, before we set the alarm.
162 */
163
164 if (!rtc->ops)
165 err = -ENODEV;
166 else if (!rtc->ops->set_alarm)
167 err = -EINVAL;
168 else
169 err = rtc->ops->set_alarm(rtc->dev.parent, alarm);
170
171 return err;
172}
173
David Brownellab6a2d72007-05-08 00:33:30 -0700174int rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800175{
176 int err;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800177
David Brownellf8245c22007-05-08 00:34:07 -0700178 err = rtc_valid_tm(&alarm->time);
179 if (err != 0)
180 return err;
181
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800182 err = mutex_lock_interruptible(&rtc->ops_lock);
183 if (err)
David Brownellb68bb262008-07-29 22:33:30 -0700184 return err;
John Stultz6610e082010-09-23 15:07:34 -0700185 if (rtc->aie_timer.enabled) {
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100186 rtc_timer_remove(rtc, &rtc->aie_timer);
John Stultz6610e082010-09-23 15:07:34 -0700187 }
188 rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time);
189 rtc->aie_timer.period = ktime_set(0, 0);
190 if (alarm->enabled) {
John Stultzaa0be0f2011-01-20 15:26:12 -0800191 err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
John Stultz6610e082010-09-23 15:07:34 -0700192 }
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800193 mutex_unlock(&rtc->ops_lock);
John Stultzaa0be0f2011-01-20 15:26:12 -0800194 return err;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800195}
196EXPORT_SYMBOL_GPL(rtc_set_alarm);
197
Alessandro Zummo099e6572009-01-04 12:00:54 -0800198int rtc_alarm_irq_enable(struct rtc_device *rtc, unsigned int enabled)
199{
200 int err = mutex_lock_interruptible(&rtc->ops_lock);
201 if (err)
202 return err;
203
John Stultz6610e082010-09-23 15:07:34 -0700204 if (rtc->aie_timer.enabled != enabled) {
John Stultzaa0be0f2011-01-20 15:26:12 -0800205 if (enabled)
206 err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
207 else
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100208 rtc_timer_remove(rtc, &rtc->aie_timer);
John Stultz6610e082010-09-23 15:07:34 -0700209 }
210
John Stultzaa0be0f2011-01-20 15:26:12 -0800211 if (err)
212 return err;
213
Alessandro Zummo099e6572009-01-04 12:00:54 -0800214 if (!rtc->ops)
215 err = -ENODEV;
216 else if (!rtc->ops->alarm_irq_enable)
217 err = -EINVAL;
218 else
219 err = rtc->ops->alarm_irq_enable(rtc->dev.parent, enabled);
220
221 mutex_unlock(&rtc->ops_lock);
222 return err;
223}
224EXPORT_SYMBOL_GPL(rtc_alarm_irq_enable);
225
226int rtc_update_irq_enable(struct rtc_device *rtc, unsigned int enabled)
227{
228 int err = mutex_lock_interruptible(&rtc->ops_lock);
229 if (err)
230 return err;
231
John Stultz6610e082010-09-23 15:07:34 -0700232 /* make sure we're changing state */
233 if (rtc->uie_rtctimer.enabled == enabled)
234 goto out;
235
236 if (enabled) {
237 struct rtc_time tm;
238 ktime_t now, onesec;
239
240 __rtc_read_time(rtc, &tm);
241 onesec = ktime_set(1, 0);
242 now = rtc_tm_to_ktime(tm);
243 rtc->uie_rtctimer.node.expires = ktime_add(now, onesec);
244 rtc->uie_rtctimer.period = ktime_set(1, 0);
John Stultzaa0be0f2011-01-20 15:26:12 -0800245 err = rtc_timer_enqueue(rtc, &rtc->uie_rtctimer);
246 } else
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100247 rtc_timer_remove(rtc, &rtc->uie_rtctimer);
Alessandro Zummo099e6572009-01-04 12:00:54 -0800248
John Stultz6610e082010-09-23 15:07:34 -0700249out:
Alessandro Zummo099e6572009-01-04 12:00:54 -0800250 mutex_unlock(&rtc->ops_lock);
Alessandro Zummo099e6572009-01-04 12:00:54 -0800251 return err;
John Stultz6610e082010-09-23 15:07:34 -0700252
Alessandro Zummo099e6572009-01-04 12:00:54 -0800253}
254EXPORT_SYMBOL_GPL(rtc_update_irq_enable);
255
John Stultz6610e082010-09-23 15:07:34 -0700256
David Brownelld728b1e2006-11-25 11:09:28 -0800257/**
John Stultz6610e082010-09-23 15:07:34 -0700258 * rtc_handle_legacy_irq - AIE, UIE and PIE event hook
259 * @rtc: pointer to the rtc device
260 *
261 * This function is called when an AIE, UIE or PIE mode interrupt
262 * has occured (or been emulated).
263 *
264 * Triggers the registered irq_task function callback.
265 */
266static void rtc_handle_legacy_irq(struct rtc_device *rtc, int num, int mode)
267{
268 unsigned long flags;
269
270 /* mark one irq of the appropriate mode */
271 spin_lock_irqsave(&rtc->irq_lock, flags);
272 rtc->irq_data = (rtc->irq_data + (num << 8)) | (RTC_IRQF|mode);
273 spin_unlock_irqrestore(&rtc->irq_lock, flags);
274
275 /* call the task func */
276 spin_lock_irqsave(&rtc->irq_task_lock, flags);
277 if (rtc->irq_task)
278 rtc->irq_task->func(rtc->irq_task->private_data);
279 spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
280
281 wake_up_interruptible(&rtc->irq_queue);
282 kill_fasync(&rtc->async_queue, SIGIO, POLL_IN);
283}
284
285
286/**
287 * rtc_aie_update_irq - AIE mode rtctimer hook
288 * @private: pointer to the rtc_device
289 *
290 * This functions is called when the aie_timer expires.
291 */
292void rtc_aie_update_irq(void *private)
293{
294 struct rtc_device *rtc = (struct rtc_device *)private;
295 rtc_handle_legacy_irq(rtc, 1, RTC_AF);
296}
297
298
299/**
300 * rtc_uie_update_irq - UIE mode rtctimer hook
301 * @private: pointer to the rtc_device
302 *
303 * This functions is called when the uie_timer expires.
304 */
305void rtc_uie_update_irq(void *private)
306{
307 struct rtc_device *rtc = (struct rtc_device *)private;
308 rtc_handle_legacy_irq(rtc, 1, RTC_UF);
309}
310
311
312/**
313 * rtc_pie_update_irq - PIE mode hrtimer hook
314 * @timer: pointer to the pie mode hrtimer
315 *
316 * This function is used to emulate PIE mode interrupts
317 * using an hrtimer. This function is called when the periodic
318 * hrtimer expires.
319 */
320enum hrtimer_restart rtc_pie_update_irq(struct hrtimer *timer)
321{
322 struct rtc_device *rtc;
323 ktime_t period;
324 int count;
325 rtc = container_of(timer, struct rtc_device, pie_timer);
326
327 period = ktime_set(0, NSEC_PER_SEC/rtc->irq_freq);
328 count = hrtimer_forward_now(timer, period);
329
330 rtc_handle_legacy_irq(rtc, count, RTC_PF);
331
332 return HRTIMER_RESTART;
333}
334
335/**
336 * rtc_update_irq - Triggered when a RTC interrupt occurs.
David Brownellab6a2d72007-05-08 00:33:30 -0700337 * @rtc: the rtc device
David Brownelld728b1e2006-11-25 11:09:28 -0800338 * @num: how many irqs are being reported (usually one)
339 * @events: mask of RTC_IRQF with one or more of RTC_PF, RTC_AF, RTC_UF
Atsushi Nemotoe6229bec2009-06-18 16:49:09 -0700340 * Context: any
David Brownelld728b1e2006-11-25 11:09:28 -0800341 */
David Brownellab6a2d72007-05-08 00:33:30 -0700342void rtc_update_irq(struct rtc_device *rtc,
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800343 unsigned long num, unsigned long events)
344{
John Stultz6610e082010-09-23 15:07:34 -0700345 schedule_work(&rtc->irqwork);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800346}
347EXPORT_SYMBOL_GPL(rtc_update_irq);
348
Dave Young71da8902008-01-22 14:00:34 +0800349static int __rtc_match(struct device *dev, void *data)
350{
351 char *name = (char *)data;
352
Kay Sieversd4afc762009-01-06 14:42:11 -0800353 if (strcmp(dev_name(dev), name) == 0)
Dave Young71da8902008-01-22 14:00:34 +0800354 return 1;
355 return 0;
356}
357
David Brownellab6a2d72007-05-08 00:33:30 -0700358struct rtc_device *rtc_class_open(char *name)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800359{
David Brownellcd966202007-05-08 00:33:40 -0700360 struct device *dev;
David Brownellab6a2d72007-05-08 00:33:30 -0700361 struct rtc_device *rtc = NULL;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800362
Greg Kroah-Hartman695794a2008-05-22 17:21:08 -0400363 dev = class_find_device(rtc_class, NULL, name, __rtc_match);
Dave Young71da8902008-01-22 14:00:34 +0800364 if (dev)
365 rtc = to_rtc_device(dev);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800366
David Brownellab6a2d72007-05-08 00:33:30 -0700367 if (rtc) {
368 if (!try_module_get(rtc->owner)) {
David Brownellcd966202007-05-08 00:33:40 -0700369 put_device(dev);
David Brownellab6a2d72007-05-08 00:33:30 -0700370 rtc = NULL;
371 }
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800372 }
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800373
David Brownellab6a2d72007-05-08 00:33:30 -0700374 return rtc;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800375}
376EXPORT_SYMBOL_GPL(rtc_class_open);
377
David Brownellab6a2d72007-05-08 00:33:30 -0700378void rtc_class_close(struct rtc_device *rtc)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800379{
David Brownellab6a2d72007-05-08 00:33:30 -0700380 module_put(rtc->owner);
David Brownellcd966202007-05-08 00:33:40 -0700381 put_device(&rtc->dev);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800382}
383EXPORT_SYMBOL_GPL(rtc_class_close);
384
David Brownellab6a2d72007-05-08 00:33:30 -0700385int rtc_irq_register(struct rtc_device *rtc, struct rtc_task *task)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800386{
387 int retval = -EBUSY;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800388
389 if (task == NULL || task->func == NULL)
390 return -EINVAL;
391
Alessandro Zummod691eb92007-10-16 01:28:15 -0700392 /* Cannot register while the char dev is in use */
Jiri Kosina372a3022007-12-04 23:45:05 -0800393 if (test_and_set_bit_lock(RTC_DEV_BUSY, &rtc->flags))
Alessandro Zummod691eb92007-10-16 01:28:15 -0700394 return -EBUSY;
395
David Brownelld728b1e2006-11-25 11:09:28 -0800396 spin_lock_irq(&rtc->irq_task_lock);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800397 if (rtc->irq_task == NULL) {
398 rtc->irq_task = task;
399 retval = 0;
400 }
David Brownelld728b1e2006-11-25 11:09:28 -0800401 spin_unlock_irq(&rtc->irq_task_lock);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800402
Jiri Kosina372a3022007-12-04 23:45:05 -0800403 clear_bit_unlock(RTC_DEV_BUSY, &rtc->flags);
Alessandro Zummod691eb92007-10-16 01:28:15 -0700404
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800405 return retval;
406}
407EXPORT_SYMBOL_GPL(rtc_irq_register);
408
David Brownellab6a2d72007-05-08 00:33:30 -0700409void rtc_irq_unregister(struct rtc_device *rtc, struct rtc_task *task)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800410{
David Brownelld728b1e2006-11-25 11:09:28 -0800411 spin_lock_irq(&rtc->irq_task_lock);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800412 if (rtc->irq_task == task)
413 rtc->irq_task = NULL;
David Brownelld728b1e2006-11-25 11:09:28 -0800414 spin_unlock_irq(&rtc->irq_task_lock);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800415}
416EXPORT_SYMBOL_GPL(rtc_irq_unregister);
417
David Brownell97144c62007-10-16 01:28:16 -0700418/**
419 * rtc_irq_set_state - enable/disable 2^N Hz periodic IRQs
420 * @rtc: the rtc device
421 * @task: currently registered with rtc_irq_register()
422 * @enabled: true to enable periodic IRQs
423 * Context: any
424 *
425 * Note that rtc_irq_set_freq() should previously have been used to
426 * specify the desired frequency of periodic IRQ task->func() callbacks.
427 */
David Brownellab6a2d72007-05-08 00:33:30 -0700428int rtc_irq_set_state(struct rtc_device *rtc, struct rtc_task *task, int enabled)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800429{
430 int err = 0;
431 unsigned long flags;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800432
433 spin_lock_irqsave(&rtc->irq_task_lock, flags);
Alessandro Zummod691eb92007-10-16 01:28:15 -0700434 if (rtc->irq_task != NULL && task == NULL)
435 err = -EBUSY;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800436 if (rtc->irq_task != task)
Alessandro Zummod691eb92007-10-16 01:28:15 -0700437 err = -EACCES;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800438
John Stultz6610e082010-09-23 15:07:34 -0700439 if (enabled) {
440 ktime_t period = ktime_set(0, NSEC_PER_SEC/rtc->irq_freq);
441 hrtimer_start(&rtc->pie_timer, period, HRTIMER_MODE_REL);
442 } else {
443 hrtimer_cancel(&rtc->pie_timer);
444 }
445 rtc->pie_enabled = enabled;
446 spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800447
448 return err;
449}
450EXPORT_SYMBOL_GPL(rtc_irq_set_state);
451
David Brownell97144c62007-10-16 01:28:16 -0700452/**
453 * rtc_irq_set_freq - set 2^N Hz periodic IRQ frequency for IRQ
454 * @rtc: the rtc device
455 * @task: currently registered with rtc_irq_register()
456 * @freq: positive frequency with which task->func() will be called
457 * Context: any
458 *
459 * Note that rtc_irq_set_state() is used to enable or disable the
460 * periodic IRQs.
461 */
David Brownellab6a2d72007-05-08 00:33:30 -0700462int rtc_irq_set_freq(struct rtc_device *rtc, struct rtc_task *task, int freq)
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800463{
Alessandro Zummo56f10c62006-06-25 05:48:20 -0700464 int err = 0;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800465 unsigned long flags;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800466
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800467 spin_lock_irqsave(&rtc->irq_task_lock, flags);
Alessandro Zummod691eb92007-10-16 01:28:15 -0700468 if (rtc->irq_task != NULL && task == NULL)
469 err = -EBUSY;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800470 if (rtc->irq_task != task)
Alessandro Zummod691eb92007-10-16 01:28:15 -0700471 err = -EACCES;
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800472 if (err == 0) {
John Stultz6610e082010-09-23 15:07:34 -0700473 rtc->irq_freq = freq;
474 if (rtc->pie_enabled) {
475 ktime_t period;
476 hrtimer_cancel(&rtc->pie_timer);
477 period = ktime_set(0, NSEC_PER_SEC/rtc->irq_freq);
478 hrtimer_start(&rtc->pie_timer, period,
479 HRTIMER_MODE_REL);
480 }
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800481 }
John Stultz6610e082010-09-23 15:07:34 -0700482 spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
Alessandro Zummo0c86edc2006-03-27 01:16:37 -0800483 return err;
484}
David Brownell2601a462006-11-25 11:09:27 -0800485EXPORT_SYMBOL_GPL(rtc_irq_set_freq);
John Stultz6610e082010-09-23 15:07:34 -0700486
487/**
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100488 * rtc_timer_enqueue - Adds a rtc_timer to the rtc_device timerqueue
John Stultz6610e082010-09-23 15:07:34 -0700489 * @rtc rtc device
490 * @timer timer being added.
491 *
492 * Enqueues a timer onto the rtc devices timerqueue and sets
493 * the next alarm event appropriately.
494 *
John Stultzaa0be0f2011-01-20 15:26:12 -0800495 * Sets the enabled bit on the added timer.
496 *
John Stultz6610e082010-09-23 15:07:34 -0700497 * Must hold ops_lock for proper serialization of timerqueue
498 */
John Stultzaa0be0f2011-01-20 15:26:12 -0800499static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer)
John Stultz6610e082010-09-23 15:07:34 -0700500{
John Stultzaa0be0f2011-01-20 15:26:12 -0800501 timer->enabled = 1;
John Stultz6610e082010-09-23 15:07:34 -0700502 timerqueue_add(&rtc->timerqueue, &timer->node);
503 if (&timer->node == timerqueue_getnext(&rtc->timerqueue)) {
504 struct rtc_wkalrm alarm;
505 int err;
506 alarm.time = rtc_ktime_to_tm(timer->node.expires);
507 alarm.enabled = 1;
508 err = __rtc_set_alarm(rtc, &alarm);
509 if (err == -ETIME)
510 schedule_work(&rtc->irqwork);
John Stultzaa0be0f2011-01-20 15:26:12 -0800511 else if (err) {
512 timerqueue_del(&rtc->timerqueue, &timer->node);
513 timer->enabled = 0;
514 return err;
515 }
John Stultz6610e082010-09-23 15:07:34 -0700516 }
John Stultzaa0be0f2011-01-20 15:26:12 -0800517 return 0;
John Stultz6610e082010-09-23 15:07:34 -0700518}
519
520/**
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100521 * rtc_timer_remove - Removes a rtc_timer from the rtc_device timerqueue
John Stultz6610e082010-09-23 15:07:34 -0700522 * @rtc rtc device
523 * @timer timer being removed.
524 *
525 * Removes a timer onto the rtc devices timerqueue and sets
526 * the next alarm event appropriately.
527 *
John Stultzaa0be0f2011-01-20 15:26:12 -0800528 * Clears the enabled bit on the removed timer.
529 *
John Stultz6610e082010-09-23 15:07:34 -0700530 * Must hold ops_lock for proper serialization of timerqueue
531 */
John Stultzaa0be0f2011-01-20 15:26:12 -0800532static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer)
John Stultz6610e082010-09-23 15:07:34 -0700533{
534 struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue);
535 timerqueue_del(&rtc->timerqueue, &timer->node);
John Stultzaa0be0f2011-01-20 15:26:12 -0800536 timer->enabled = 0;
John Stultz6610e082010-09-23 15:07:34 -0700537 if (next == &timer->node) {
538 struct rtc_wkalrm alarm;
539 int err;
540 next = timerqueue_getnext(&rtc->timerqueue);
541 if (!next)
542 return;
543 alarm.time = rtc_ktime_to_tm(next->expires);
544 alarm.enabled = 1;
545 err = __rtc_set_alarm(rtc, &alarm);
546 if (err == -ETIME)
547 schedule_work(&rtc->irqwork);
548 }
549}
550
551/**
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100552 * rtc_timer_do_work - Expires rtc timers
John Stultz6610e082010-09-23 15:07:34 -0700553 * @rtc rtc device
554 * @timer timer being removed.
555 *
556 * Expires rtc timers. Reprograms next alarm event if needed.
557 * Called via worktask.
558 *
559 * Serializes access to timerqueue via ops_lock mutex
560 */
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100561void rtc_timer_do_work(struct work_struct *work)
John Stultz6610e082010-09-23 15:07:34 -0700562{
563 struct rtc_timer *timer;
564 struct timerqueue_node *next;
565 ktime_t now;
566 struct rtc_time tm;
567
568 struct rtc_device *rtc =
569 container_of(work, struct rtc_device, irqwork);
570
571 mutex_lock(&rtc->ops_lock);
572again:
573 __rtc_read_time(rtc, &tm);
574 now = rtc_tm_to_ktime(tm);
575 while ((next = timerqueue_getnext(&rtc->timerqueue))) {
576 if (next->expires.tv64 > now.tv64)
577 break;
578
579 /* expire timer */
580 timer = container_of(next, struct rtc_timer, node);
581 timerqueue_del(&rtc->timerqueue, &timer->node);
582 timer->enabled = 0;
583 if (timer->task.func)
584 timer->task.func(timer->task.private_data);
585
586 /* Re-add/fwd periodic timers */
587 if (ktime_to_ns(timer->period)) {
588 timer->node.expires = ktime_add(timer->node.expires,
589 timer->period);
590 timer->enabled = 1;
591 timerqueue_add(&rtc->timerqueue, &timer->node);
592 }
593 }
594
595 /* Set next alarm */
596 if (next) {
597 struct rtc_wkalrm alarm;
598 int err;
599 alarm.time = rtc_ktime_to_tm(next->expires);
600 alarm.enabled = 1;
601 err = __rtc_set_alarm(rtc, &alarm);
602 if (err == -ETIME)
603 goto again;
604 }
605
606 mutex_unlock(&rtc->ops_lock);
607}
608
609
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100610/* rtc_timer_init - Initializes an rtc_timer
John Stultz6610e082010-09-23 15:07:34 -0700611 * @timer: timer to be intiialized
612 * @f: function pointer to be called when timer fires
613 * @data: private data passed to function pointer
614 *
615 * Kernel interface to initializing an rtc_timer.
616 */
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100617void rtc_timer_init(struct rtc_timer *timer, void (*f)(void* p), void* data)
John Stultz6610e082010-09-23 15:07:34 -0700618{
619 timerqueue_init(&timer->node);
620 timer->enabled = 0;
621 timer->task.func = f;
622 timer->task.private_data = data;
623}
624
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100625/* rtc_timer_start - Sets an rtc_timer to fire in the future
John Stultz6610e082010-09-23 15:07:34 -0700626 * @ rtc: rtc device to be used
627 * @ timer: timer being set
628 * @ expires: time at which to expire the timer
629 * @ period: period that the timer will recur
630 *
631 * Kernel interface to set an rtc_timer
632 */
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100633int rtc_timer_start(struct rtc_device *rtc, struct rtc_timer* timer,
John Stultz6610e082010-09-23 15:07:34 -0700634 ktime_t expires, ktime_t period)
635{
636 int ret = 0;
637 mutex_lock(&rtc->ops_lock);
638 if (timer->enabled)
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100639 rtc_timer_remove(rtc, timer);
John Stultz6610e082010-09-23 15:07:34 -0700640
641 timer->node.expires = expires;
642 timer->period = period;
643
John Stultzaa0be0f2011-01-20 15:26:12 -0800644 ret = rtc_timer_enqueue(rtc, timer);
John Stultz6610e082010-09-23 15:07:34 -0700645
646 mutex_unlock(&rtc->ops_lock);
647 return ret;
648}
649
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100650/* rtc_timer_cancel - Stops an rtc_timer
John Stultz6610e082010-09-23 15:07:34 -0700651 * @ rtc: rtc device to be used
652 * @ timer: timer being set
653 *
654 * Kernel interface to cancel an rtc_timer
655 */
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100656int rtc_timer_cancel(struct rtc_device *rtc, struct rtc_timer* timer)
John Stultz6610e082010-09-23 15:07:34 -0700657{
658 int ret = 0;
659 mutex_lock(&rtc->ops_lock);
660 if (timer->enabled)
Thomas Gleixner96c8f062010-12-13 22:45:48 +0100661 rtc_timer_remove(rtc, timer);
John Stultz6610e082010-09-23 15:07:34 -0700662 mutex_unlock(&rtc->ops_lock);
663 return ret;
664}
665
666