Commit | Line | Data |
---|---|---|
0c86edc0 AZ |
1 | /* |
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> | |
d43c36dc | 15 | #include <linux/sched.h> |
97144c67 | 16 | #include <linux/log2.h> |
6610e089 | 17 | #include <linux/workqueue.h> |
0c86edc0 | 18 | |
aa0be0f4 JS |
19 | static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer); |
20 | static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer); | |
21 | ||
6610e089 | 22 | static int __rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm) |
0c86edc0 AZ |
23 | { |
24 | int err; | |
0c86edc0 AZ |
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)); | |
cd966209 | 31 | err = rtc->ops->read_time(rtc->dev.parent, tm); |
0c86edc0 | 32 | } |
6610e089 JS |
33 | return err; |
34 | } | |
35 | ||
36 | int rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm) | |
37 | { | |
38 | int err; | |
0c86edc0 | 39 | |
6610e089 JS |
40 | err = mutex_lock_interruptible(&rtc->ops_lock); |
41 | if (err) | |
42 | return err; | |
43 | ||
44 | err = __rtc_read_time(rtc, tm); | |
0c86edc0 AZ |
45 | mutex_unlock(&rtc->ops_lock); |
46 | return err; | |
47 | } | |
48 | EXPORT_SYMBOL_GPL(rtc_read_time); | |
49 | ||
ab6a2d70 | 50 | int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm) |
0c86edc0 AZ |
51 | { |
52 | int err; | |
0c86edc0 AZ |
53 | |
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) | |
b68bb263 | 60 | return err; |
0c86edc0 AZ |
61 | |
62 | if (!rtc->ops) | |
63 | err = -ENODEV; | |
bbccf83f | 64 | else if (rtc->ops->set_time) |
cd966209 | 65 | err = rtc->ops->set_time(rtc->dev.parent, tm); |
bbccf83f AZ |
66 | 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; | |
0c86edc0 AZ |
73 | |
74 | mutex_unlock(&rtc->ops_lock); | |
75 | return err; | |
76 | } | |
77 | EXPORT_SYMBOL_GPL(rtc_set_time); | |
78 | ||
ab6a2d70 | 79 | int rtc_set_mmss(struct rtc_device *rtc, unsigned long secs) |
0c86edc0 AZ |
80 | { |
81 | int err; | |
0c86edc0 AZ |
82 | |
83 | err = mutex_lock_interruptible(&rtc->ops_lock); | |
84 | if (err) | |
b68bb263 | 85 | return err; |
0c86edc0 AZ |
86 | |
87 | if (!rtc->ops) | |
88 | err = -ENODEV; | |
89 | else if (rtc->ops->set_mmss) | |
cd966209 | 90 | err = rtc->ops->set_mmss(rtc->dev.parent, secs); |
0c86edc0 AZ |
91 | else if (rtc->ops->read_time && rtc->ops->set_time) { |
92 | struct rtc_time new, old; | |
93 | ||
cd966209 | 94 | err = rtc->ops->read_time(rtc->dev.parent, &old); |
0c86edc0 AZ |
95 | 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))) | |
cd966209 | 106 | err = rtc->ops->set_time(rtc->dev.parent, |
ab6a2d70 | 107 | &new); |
0c86edc0 AZ |
108 | } |
109 | } | |
110 | else | |
111 | err = -EINVAL; | |
112 | ||
113 | mutex_unlock(&rtc->ops_lock); | |
114 | ||
115 | return err; | |
116 | } | |
117 | EXPORT_SYMBOL_GPL(rtc_set_mmss); | |
118 | ||
6610e089 | 119 | int rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm) |
0c86edc0 AZ |
120 | { |
121 | int err; | |
0c86edc0 AZ |
122 | |
123 | err = mutex_lock_interruptible(&rtc->ops_lock); | |
124 | if (err) | |
b68bb263 | 125 | return err; |
d5553a55 JS |
126 | 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; | |
6610e089 | 133 | alarm->time = rtc_ktime_to_tm(rtc->aie_timer.node.expires); |
d5553a55 | 134 | } |
0c86edc0 | 135 | mutex_unlock(&rtc->ops_lock); |
6610e089 | 136 | |
d5553a55 | 137 | return err; |
0c86edc0 | 138 | } |
6610e089 | 139 | EXPORT_SYMBOL_GPL(rtc_read_alarm); |
0e36a9a4 | 140 | |
6610e089 | 141 | int __rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm) |
0e36a9a4 | 142 | { |
6610e089 JS |
143 | struct rtc_time tm; |
144 | long now, scheduled; | |
0e36a9a4 | 145 | int err; |
0e36a9a4 | 146 | |
6610e089 JS |
147 | err = rtc_valid_tm(&alarm->time); |
148 | if (err) | |
0e36a9a4 | 149 | return err; |
6610e089 | 150 | rtc_tm_to_time(&alarm->time, &scheduled); |
a01cc657 | 151 | |
6610e089 JS |
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. | |
a01cc657 | 162 | */ |
a01cc657 | 163 | |
6610e089 JS |
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; | |
0e36a9a4 | 172 | } |
0c86edc0 | 173 | |
ab6a2d70 | 174 | int rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm) |
0c86edc0 AZ |
175 | { |
176 | int err; | |
0c86edc0 | 177 | |
f8245c26 DB |
178 | err = rtc_valid_tm(&alarm->time); |
179 | if (err != 0) | |
180 | return err; | |
181 | ||
0c86edc0 AZ |
182 | err = mutex_lock_interruptible(&rtc->ops_lock); |
183 | if (err) | |
b68bb263 | 184 | return err; |
6610e089 | 185 | if (rtc->aie_timer.enabled) { |
96c8f06a | 186 | rtc_timer_remove(rtc, &rtc->aie_timer); |
6610e089 JS |
187 | } |
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) { | |
aa0be0f4 | 191 | err = rtc_timer_enqueue(rtc, &rtc->aie_timer); |
6610e089 | 192 | } |
0c86edc0 | 193 | mutex_unlock(&rtc->ops_lock); |
aa0be0f4 | 194 | return err; |
0c86edc0 AZ |
195 | } |
196 | EXPORT_SYMBOL_GPL(rtc_set_alarm); | |
197 | ||
099e6576 AZ |
198 | int 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 | ||
6610e089 | 204 | if (rtc->aie_timer.enabled != enabled) { |
aa0be0f4 JS |
205 | if (enabled) |
206 | err = rtc_timer_enqueue(rtc, &rtc->aie_timer); | |
207 | else | |
96c8f06a | 208 | rtc_timer_remove(rtc, &rtc->aie_timer); |
6610e089 JS |
209 | } |
210 | ||
aa0be0f4 | 211 | if (err) |
516373b8 UKK |
212 | /* nothing */; |
213 | else if (!rtc->ops) | |
099e6576 AZ |
214 | err = -ENODEV; |
215 | else if (!rtc->ops->alarm_irq_enable) | |
216 | err = -EINVAL; | |
217 | else | |
218 | err = rtc->ops->alarm_irq_enable(rtc->dev.parent, enabled); | |
219 | ||
220 | mutex_unlock(&rtc->ops_lock); | |
221 | return err; | |
222 | } | |
223 | EXPORT_SYMBOL_GPL(rtc_alarm_irq_enable); | |
224 | ||
225 | int rtc_update_irq_enable(struct rtc_device *rtc, unsigned int enabled) | |
226 | { | |
227 | int err = mutex_lock_interruptible(&rtc->ops_lock); | |
228 | if (err) | |
229 | return err; | |
230 | ||
6610e089 JS |
231 | /* make sure we're changing state */ |
232 | if (rtc->uie_rtctimer.enabled == enabled) | |
233 | goto out; | |
234 | ||
235 | if (enabled) { | |
236 | struct rtc_time tm; | |
237 | ktime_t now, onesec; | |
238 | ||
239 | __rtc_read_time(rtc, &tm); | |
240 | onesec = ktime_set(1, 0); | |
241 | now = rtc_tm_to_ktime(tm); | |
242 | rtc->uie_rtctimer.node.expires = ktime_add(now, onesec); | |
243 | rtc->uie_rtctimer.period = ktime_set(1, 0); | |
aa0be0f4 JS |
244 | err = rtc_timer_enqueue(rtc, &rtc->uie_rtctimer); |
245 | } else | |
96c8f06a | 246 | rtc_timer_remove(rtc, &rtc->uie_rtctimer); |
099e6576 | 247 | |
6610e089 | 248 | out: |
099e6576 | 249 | mutex_unlock(&rtc->ops_lock); |
099e6576 | 250 | return err; |
6610e089 | 251 | |
099e6576 AZ |
252 | } |
253 | EXPORT_SYMBOL_GPL(rtc_update_irq_enable); | |
254 | ||
6610e089 | 255 | |
d728b1e6 | 256 | /** |
6610e089 JS |
257 | * rtc_handle_legacy_irq - AIE, UIE and PIE event hook |
258 | * @rtc: pointer to the rtc device | |
259 | * | |
260 | * This function is called when an AIE, UIE or PIE mode interrupt | |
261 | * has occured (or been emulated). | |
262 | * | |
263 | * Triggers the registered irq_task function callback. | |
d728b1e6 | 264 | */ |
6610e089 | 265 | static void rtc_handle_legacy_irq(struct rtc_device *rtc, int num, int mode) |
0c86edc0 | 266 | { |
e6229bec AN |
267 | unsigned long flags; |
268 | ||
6610e089 | 269 | /* mark one irq of the appropriate mode */ |
e6229bec | 270 | spin_lock_irqsave(&rtc->irq_lock, flags); |
6610e089 | 271 | rtc->irq_data = (rtc->irq_data + (num << 8)) | (RTC_IRQF|mode); |
e6229bec | 272 | spin_unlock_irqrestore(&rtc->irq_lock, flags); |
0c86edc0 | 273 | |
6610e089 | 274 | /* call the task func */ |
e6229bec | 275 | spin_lock_irqsave(&rtc->irq_task_lock, flags); |
0c86edc0 AZ |
276 | if (rtc->irq_task) |
277 | rtc->irq_task->func(rtc->irq_task->private_data); | |
e6229bec | 278 | spin_unlock_irqrestore(&rtc->irq_task_lock, flags); |
0c86edc0 AZ |
279 | |
280 | wake_up_interruptible(&rtc->irq_queue); | |
281 | kill_fasync(&rtc->async_queue, SIGIO, POLL_IN); | |
282 | } | |
6610e089 JS |
283 | |
284 | ||
285 | /** | |
286 | * rtc_aie_update_irq - AIE mode rtctimer hook | |
287 | * @private: pointer to the rtc_device | |
288 | * | |
289 | * This functions is called when the aie_timer expires. | |
290 | */ | |
291 | void rtc_aie_update_irq(void *private) | |
292 | { | |
293 | struct rtc_device *rtc = (struct rtc_device *)private; | |
294 | rtc_handle_legacy_irq(rtc, 1, RTC_AF); | |
295 | } | |
296 | ||
297 | ||
298 | /** | |
299 | * rtc_uie_update_irq - UIE mode rtctimer hook | |
300 | * @private: pointer to the rtc_device | |
301 | * | |
302 | * This functions is called when the uie_timer expires. | |
303 | */ | |
304 | void rtc_uie_update_irq(void *private) | |
305 | { | |
306 | struct rtc_device *rtc = (struct rtc_device *)private; | |
307 | rtc_handle_legacy_irq(rtc, 1, RTC_UF); | |
308 | } | |
309 | ||
310 | ||
311 | /** | |
312 | * rtc_pie_update_irq - PIE mode hrtimer hook | |
313 | * @timer: pointer to the pie mode hrtimer | |
314 | * | |
315 | * This function is used to emulate PIE mode interrupts | |
316 | * using an hrtimer. This function is called when the periodic | |
317 | * hrtimer expires. | |
318 | */ | |
319 | enum hrtimer_restart rtc_pie_update_irq(struct hrtimer *timer) | |
320 | { | |
321 | struct rtc_device *rtc; | |
322 | ktime_t period; | |
323 | int count; | |
324 | rtc = container_of(timer, struct rtc_device, pie_timer); | |
325 | ||
326 | period = ktime_set(0, NSEC_PER_SEC/rtc->irq_freq); | |
327 | count = hrtimer_forward_now(timer, period); | |
328 | ||
329 | rtc_handle_legacy_irq(rtc, count, RTC_PF); | |
330 | ||
331 | return HRTIMER_RESTART; | |
332 | } | |
333 | ||
334 | /** | |
335 | * rtc_update_irq - Triggered when a RTC interrupt occurs. | |
336 | * @rtc: the rtc device | |
337 | * @num: how many irqs are being reported (usually one) | |
338 | * @events: mask of RTC_IRQF with one or more of RTC_PF, RTC_AF, RTC_UF | |
339 | * Context: any | |
340 | */ | |
341 | void rtc_update_irq(struct rtc_device *rtc, | |
342 | unsigned long num, unsigned long events) | |
343 | { | |
344 | schedule_work(&rtc->irqwork); | |
345 | } | |
0c86edc0 AZ |
346 | EXPORT_SYMBOL_GPL(rtc_update_irq); |
347 | ||
71da8905 DY |
348 | static int __rtc_match(struct device *dev, void *data) |
349 | { | |
350 | char *name = (char *)data; | |
351 | ||
d4afc76c | 352 | if (strcmp(dev_name(dev), name) == 0) |
71da8905 DY |
353 | return 1; |
354 | return 0; | |
355 | } | |
356 | ||
ab6a2d70 | 357 | struct rtc_device *rtc_class_open(char *name) |
0c86edc0 | 358 | { |
cd966209 | 359 | struct device *dev; |
ab6a2d70 | 360 | struct rtc_device *rtc = NULL; |
0c86edc0 | 361 | |
695794ae | 362 | dev = class_find_device(rtc_class, NULL, name, __rtc_match); |
71da8905 DY |
363 | if (dev) |
364 | rtc = to_rtc_device(dev); | |
0c86edc0 | 365 | |
ab6a2d70 DB |
366 | if (rtc) { |
367 | if (!try_module_get(rtc->owner)) { | |
cd966209 | 368 | put_device(dev); |
ab6a2d70 DB |
369 | rtc = NULL; |
370 | } | |
0c86edc0 | 371 | } |
0c86edc0 | 372 | |
ab6a2d70 | 373 | return rtc; |
0c86edc0 AZ |
374 | } |
375 | EXPORT_SYMBOL_GPL(rtc_class_open); | |
376 | ||
ab6a2d70 | 377 | void rtc_class_close(struct rtc_device *rtc) |
0c86edc0 | 378 | { |
ab6a2d70 | 379 | module_put(rtc->owner); |
cd966209 | 380 | put_device(&rtc->dev); |
0c86edc0 AZ |
381 | } |
382 | EXPORT_SYMBOL_GPL(rtc_class_close); | |
383 | ||
ab6a2d70 | 384 | int rtc_irq_register(struct rtc_device *rtc, struct rtc_task *task) |
0c86edc0 AZ |
385 | { |
386 | int retval = -EBUSY; | |
0c86edc0 AZ |
387 | |
388 | if (task == NULL || task->func == NULL) | |
389 | return -EINVAL; | |
390 | ||
d691eb90 | 391 | /* Cannot register while the char dev is in use */ |
372a302e | 392 | if (test_and_set_bit_lock(RTC_DEV_BUSY, &rtc->flags)) |
d691eb90 AZ |
393 | return -EBUSY; |
394 | ||
d728b1e6 | 395 | spin_lock_irq(&rtc->irq_task_lock); |
0c86edc0 AZ |
396 | if (rtc->irq_task == NULL) { |
397 | rtc->irq_task = task; | |
398 | retval = 0; | |
399 | } | |
d728b1e6 | 400 | spin_unlock_irq(&rtc->irq_task_lock); |
0c86edc0 | 401 | |
372a302e | 402 | clear_bit_unlock(RTC_DEV_BUSY, &rtc->flags); |
d691eb90 | 403 | |
0c86edc0 AZ |
404 | return retval; |
405 | } | |
406 | EXPORT_SYMBOL_GPL(rtc_irq_register); | |
407 | ||
ab6a2d70 | 408 | void rtc_irq_unregister(struct rtc_device *rtc, struct rtc_task *task) |
0c86edc0 | 409 | { |
d728b1e6 | 410 | spin_lock_irq(&rtc->irq_task_lock); |
0c86edc0 AZ |
411 | if (rtc->irq_task == task) |
412 | rtc->irq_task = NULL; | |
d728b1e6 | 413 | spin_unlock_irq(&rtc->irq_task_lock); |
0c86edc0 AZ |
414 | } |
415 | EXPORT_SYMBOL_GPL(rtc_irq_unregister); | |
416 | ||
97144c67 DB |
417 | /** |
418 | * rtc_irq_set_state - enable/disable 2^N Hz periodic IRQs | |
419 | * @rtc: the rtc device | |
420 | * @task: currently registered with rtc_irq_register() | |
421 | * @enabled: true to enable periodic IRQs | |
422 | * Context: any | |
423 | * | |
424 | * Note that rtc_irq_set_freq() should previously have been used to | |
425 | * specify the desired frequency of periodic IRQ task->func() callbacks. | |
426 | */ | |
ab6a2d70 | 427 | int rtc_irq_set_state(struct rtc_device *rtc, struct rtc_task *task, int enabled) |
0c86edc0 AZ |
428 | { |
429 | int err = 0; | |
430 | unsigned long flags; | |
0c86edc0 AZ |
431 | |
432 | spin_lock_irqsave(&rtc->irq_task_lock, flags); | |
d691eb90 AZ |
433 | if (rtc->irq_task != NULL && task == NULL) |
434 | err = -EBUSY; | |
0c86edc0 | 435 | if (rtc->irq_task != task) |
d691eb90 | 436 | err = -EACCES; |
0c86edc0 | 437 | |
6610e089 JS |
438 | if (enabled) { |
439 | ktime_t period = ktime_set(0, NSEC_PER_SEC/rtc->irq_freq); | |
440 | hrtimer_start(&rtc->pie_timer, period, HRTIMER_MODE_REL); | |
441 | } else { | |
442 | hrtimer_cancel(&rtc->pie_timer); | |
443 | } | |
444 | rtc->pie_enabled = enabled; | |
445 | spin_unlock_irqrestore(&rtc->irq_task_lock, flags); | |
0c86edc0 AZ |
446 | |
447 | return err; | |
448 | } | |
449 | EXPORT_SYMBOL_GPL(rtc_irq_set_state); | |
450 | ||
97144c67 DB |
451 | /** |
452 | * rtc_irq_set_freq - set 2^N Hz periodic IRQ frequency for IRQ | |
453 | * @rtc: the rtc device | |
454 | * @task: currently registered with rtc_irq_register() | |
455 | * @freq: positive frequency with which task->func() will be called | |
456 | * Context: any | |
457 | * | |
458 | * Note that rtc_irq_set_state() is used to enable or disable the | |
459 | * periodic IRQs. | |
460 | */ | |
ab6a2d70 | 461 | int rtc_irq_set_freq(struct rtc_device *rtc, struct rtc_task *task, int freq) |
0c86edc0 | 462 | { |
56f10c63 | 463 | int err = 0; |
0c86edc0 | 464 | unsigned long flags; |
0c86edc0 | 465 | |
83a06bf5 MRJ |
466 | if (freq <= 0) |
467 | return -EINVAL; | |
468 | ||
0c86edc0 | 469 | spin_lock_irqsave(&rtc->irq_task_lock, flags); |
d691eb90 AZ |
470 | if (rtc->irq_task != NULL && task == NULL) |
471 | err = -EBUSY; | |
0c86edc0 | 472 | if (rtc->irq_task != task) |
d691eb90 | 473 | err = -EACCES; |
0c86edc0 | 474 | if (err == 0) { |
6610e089 JS |
475 | rtc->irq_freq = freq; |
476 | if (rtc->pie_enabled) { | |
477 | ktime_t period; | |
478 | hrtimer_cancel(&rtc->pie_timer); | |
479 | period = ktime_set(0, NSEC_PER_SEC/rtc->irq_freq); | |
480 | hrtimer_start(&rtc->pie_timer, period, | |
481 | HRTIMER_MODE_REL); | |
482 | } | |
0c86edc0 | 483 | } |
6610e089 | 484 | spin_unlock_irqrestore(&rtc->irq_task_lock, flags); |
0c86edc0 AZ |
485 | return err; |
486 | } | |
2601a464 | 487 | EXPORT_SYMBOL_GPL(rtc_irq_set_freq); |
6610e089 JS |
488 | |
489 | /** | |
96c8f06a | 490 | * rtc_timer_enqueue - Adds a rtc_timer to the rtc_device timerqueue |
6610e089 JS |
491 | * @rtc rtc device |
492 | * @timer timer being added. | |
493 | * | |
494 | * Enqueues a timer onto the rtc devices timerqueue and sets | |
495 | * the next alarm event appropriately. | |
496 | * | |
aa0be0f4 JS |
497 | * Sets the enabled bit on the added timer. |
498 | * | |
6610e089 JS |
499 | * Must hold ops_lock for proper serialization of timerqueue |
500 | */ | |
aa0be0f4 | 501 | static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer) |
6610e089 | 502 | { |
aa0be0f4 | 503 | timer->enabled = 1; |
6610e089 JS |
504 | timerqueue_add(&rtc->timerqueue, &timer->node); |
505 | if (&timer->node == timerqueue_getnext(&rtc->timerqueue)) { | |
506 | struct rtc_wkalrm alarm; | |
507 | int err; | |
508 | alarm.time = rtc_ktime_to_tm(timer->node.expires); | |
509 | alarm.enabled = 1; | |
510 | err = __rtc_set_alarm(rtc, &alarm); | |
511 | if (err == -ETIME) | |
512 | schedule_work(&rtc->irqwork); | |
aa0be0f4 JS |
513 | else if (err) { |
514 | timerqueue_del(&rtc->timerqueue, &timer->node); | |
515 | timer->enabled = 0; | |
516 | return err; | |
517 | } | |
6610e089 | 518 | } |
aa0be0f4 | 519 | return 0; |
6610e089 JS |
520 | } |
521 | ||
522 | /** | |
96c8f06a | 523 | * rtc_timer_remove - Removes a rtc_timer from the rtc_device timerqueue |
6610e089 JS |
524 | * @rtc rtc device |
525 | * @timer timer being removed. | |
526 | * | |
527 | * Removes a timer onto the rtc devices timerqueue and sets | |
528 | * the next alarm event appropriately. | |
529 | * | |
aa0be0f4 JS |
530 | * Clears the enabled bit on the removed timer. |
531 | * | |
6610e089 JS |
532 | * Must hold ops_lock for proper serialization of timerqueue |
533 | */ | |
aa0be0f4 | 534 | static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer) |
6610e089 JS |
535 | { |
536 | struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue); | |
537 | timerqueue_del(&rtc->timerqueue, &timer->node); | |
aa0be0f4 | 538 | timer->enabled = 0; |
6610e089 JS |
539 | if (next == &timer->node) { |
540 | struct rtc_wkalrm alarm; | |
541 | int err; | |
542 | next = timerqueue_getnext(&rtc->timerqueue); | |
543 | if (!next) | |
544 | return; | |
545 | alarm.time = rtc_ktime_to_tm(next->expires); | |
546 | alarm.enabled = 1; | |
547 | err = __rtc_set_alarm(rtc, &alarm); | |
548 | if (err == -ETIME) | |
549 | schedule_work(&rtc->irqwork); | |
550 | } | |
551 | } | |
552 | ||
553 | /** | |
96c8f06a | 554 | * rtc_timer_do_work - Expires rtc timers |
6610e089 JS |
555 | * @rtc rtc device |
556 | * @timer timer being removed. | |
557 | * | |
558 | * Expires rtc timers. Reprograms next alarm event if needed. | |
559 | * Called via worktask. | |
560 | * | |
561 | * Serializes access to timerqueue via ops_lock mutex | |
562 | */ | |
96c8f06a | 563 | void rtc_timer_do_work(struct work_struct *work) |
6610e089 JS |
564 | { |
565 | struct rtc_timer *timer; | |
566 | struct timerqueue_node *next; | |
567 | ktime_t now; | |
568 | struct rtc_time tm; | |
569 | ||
570 | struct rtc_device *rtc = | |
571 | container_of(work, struct rtc_device, irqwork); | |
572 | ||
573 | mutex_lock(&rtc->ops_lock); | |
574 | again: | |
575 | __rtc_read_time(rtc, &tm); | |
576 | now = rtc_tm_to_ktime(tm); | |
577 | while ((next = timerqueue_getnext(&rtc->timerqueue))) { | |
578 | if (next->expires.tv64 > now.tv64) | |
579 | break; | |
580 | ||
581 | /* expire timer */ | |
582 | timer = container_of(next, struct rtc_timer, node); | |
583 | timerqueue_del(&rtc->timerqueue, &timer->node); | |
584 | timer->enabled = 0; | |
585 | if (timer->task.func) | |
586 | timer->task.func(timer->task.private_data); | |
587 | ||
588 | /* Re-add/fwd periodic timers */ | |
589 | if (ktime_to_ns(timer->period)) { | |
590 | timer->node.expires = ktime_add(timer->node.expires, | |
591 | timer->period); | |
592 | timer->enabled = 1; | |
593 | timerqueue_add(&rtc->timerqueue, &timer->node); | |
594 | } | |
595 | } | |
596 | ||
597 | /* Set next alarm */ | |
598 | if (next) { | |
599 | struct rtc_wkalrm alarm; | |
600 | int err; | |
601 | alarm.time = rtc_ktime_to_tm(next->expires); | |
602 | alarm.enabled = 1; | |
603 | err = __rtc_set_alarm(rtc, &alarm); | |
604 | if (err == -ETIME) | |
605 | goto again; | |
606 | } | |
607 | ||
608 | mutex_unlock(&rtc->ops_lock); | |
609 | } | |
610 | ||
611 | ||
96c8f06a | 612 | /* rtc_timer_init - Initializes an rtc_timer |
6610e089 JS |
613 | * @timer: timer to be intiialized |
614 | * @f: function pointer to be called when timer fires | |
615 | * @data: private data passed to function pointer | |
616 | * | |
617 | * Kernel interface to initializing an rtc_timer. | |
618 | */ | |
96c8f06a | 619 | void rtc_timer_init(struct rtc_timer *timer, void (*f)(void* p), void* data) |
6610e089 JS |
620 | { |
621 | timerqueue_init(&timer->node); | |
622 | timer->enabled = 0; | |
623 | timer->task.func = f; | |
624 | timer->task.private_data = data; | |
625 | } | |
626 | ||
96c8f06a | 627 | /* rtc_timer_start - Sets an rtc_timer to fire in the future |
6610e089 JS |
628 | * @ rtc: rtc device to be used |
629 | * @ timer: timer being set | |
630 | * @ expires: time at which to expire the timer | |
631 | * @ period: period that the timer will recur | |
632 | * | |
633 | * Kernel interface to set an rtc_timer | |
634 | */ | |
96c8f06a | 635 | int rtc_timer_start(struct rtc_device *rtc, struct rtc_timer* timer, |
6610e089 JS |
636 | ktime_t expires, ktime_t period) |
637 | { | |
638 | int ret = 0; | |
639 | mutex_lock(&rtc->ops_lock); | |
640 | if (timer->enabled) | |
96c8f06a | 641 | rtc_timer_remove(rtc, timer); |
6610e089 JS |
642 | |
643 | timer->node.expires = expires; | |
644 | timer->period = period; | |
645 | ||
aa0be0f4 | 646 | ret = rtc_timer_enqueue(rtc, timer); |
6610e089 JS |
647 | |
648 | mutex_unlock(&rtc->ops_lock); | |
649 | return ret; | |
650 | } | |
651 | ||
96c8f06a | 652 | /* rtc_timer_cancel - Stops an rtc_timer |
6610e089 JS |
653 | * @ rtc: rtc device to be used |
654 | * @ timer: timer being set | |
655 | * | |
656 | * Kernel interface to cancel an rtc_timer | |
657 | */ | |
96c8f06a | 658 | int rtc_timer_cancel(struct rtc_device *rtc, struct rtc_timer* timer) |
6610e089 JS |
659 | { |
660 | int ret = 0; | |
661 | mutex_lock(&rtc->ops_lock); | |
662 | if (timer->enabled) | |
96c8f06a | 663 | rtc_timer_remove(rtc, timer); |
6610e089 JS |
664 | mutex_unlock(&rtc->ops_lock); |
665 | return ret; | |
666 | } | |
667 | ||
668 |