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>
15 #include <linux/sched.h>
16 #include <linux/module.h>
17 #include <linux/log2.h>
18 #include <linux/workqueue.h>
19
20 static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer);
21 static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer);
22
__rtc_read_time(struct rtc_device * rtc,struct rtc_time * tm)23 static int __rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm)
24 {
25 int err;
26 if (!rtc->ops)
27 err = -ENODEV;
28 else if (!rtc->ops->read_time)
29 err = -EINVAL;
30 else {
31 memset(tm, 0, sizeof(struct rtc_time));
32 err = rtc->ops->read_time(rtc->dev.parent, tm);
33 if (err < 0) {
34 dev_dbg(&rtc->dev, "read_time: fail to read: %d\n",
35 err);
36 return err;
37 }
38
39 err = rtc_valid_tm(tm);
40 if (err < 0)
41 dev_dbg(&rtc->dev, "read_time: rtc_time isn't valid\n");
42 }
43 return err;
44 }
45
rtc_read_time(struct rtc_device * rtc,struct rtc_time * tm)46 int rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm)
47 {
48 int err;
49
50 err = mutex_lock_interruptible(&rtc->ops_lock);
51 if (err)
52 return err;
53
54 err = __rtc_read_time(rtc, tm);
55 mutex_unlock(&rtc->ops_lock);
56 return err;
57 }
58 EXPORT_SYMBOL_GPL(rtc_read_time);
59
rtc_set_time(struct rtc_device * rtc,struct rtc_time * tm)60 int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm)
61 {
62 int err;
63
64 err = rtc_valid_tm(tm);
65 if (err != 0)
66 return err;
67
68 err = mutex_lock_interruptible(&rtc->ops_lock);
69 if (err)
70 return err;
71
72 if (!rtc->ops)
73 err = -ENODEV;
74 else if (rtc->ops->set_time)
75 err = rtc->ops->set_time(rtc->dev.parent, tm);
76 else if (rtc->ops->set_mmss64) {
77 time64_t secs64 = rtc_tm_to_time64(tm);
78
79 err = rtc->ops->set_mmss64(rtc->dev.parent, secs64);
80 } else if (rtc->ops->set_mmss) {
81 time64_t secs64 = rtc_tm_to_time64(tm);
82 err = rtc->ops->set_mmss(rtc->dev.parent, secs64);
83 } else
84 err = -EINVAL;
85
86 pm_stay_awake(rtc->dev.parent);
87 mutex_unlock(&rtc->ops_lock);
88 /* A timer might have just expired */
89 schedule_work(&rtc->irqwork);
90 return err;
91 }
92 EXPORT_SYMBOL_GPL(rtc_set_time);
93
rtc_read_alarm_internal(struct rtc_device * rtc,struct rtc_wkalrm * alarm)94 static int rtc_read_alarm_internal(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
95 {
96 int err;
97
98 err = mutex_lock_interruptible(&rtc->ops_lock);
99 if (err)
100 return err;
101
102 if (rtc->ops == NULL)
103 err = -ENODEV;
104 else if (!rtc->ops->read_alarm)
105 err = -EINVAL;
106 else {
107 memset(alarm, 0, sizeof(struct rtc_wkalrm));
108 err = rtc->ops->read_alarm(rtc->dev.parent, alarm);
109 }
110
111 mutex_unlock(&rtc->ops_lock);
112 return err;
113 }
114
__rtc_read_alarm(struct rtc_device * rtc,struct rtc_wkalrm * alarm)115 int __rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
116 {
117 int err;
118 struct rtc_time before, now;
119 int first_time = 1;
120 time64_t t_now, t_alm;
121 enum { none, day, month, year } missing = none;
122 unsigned days;
123
124 /* The lower level RTC driver may return -1 in some fields,
125 * creating invalid alarm->time values, for reasons like:
126 *
127 * - The hardware may not be capable of filling them in;
128 * many alarms match only on time-of-day fields, not
129 * day/month/year calendar data.
130 *
131 * - Some hardware uses illegal values as "wildcard" match
132 * values, which non-Linux firmware (like a BIOS) may try
133 * to set up as e.g. "alarm 15 minutes after each hour".
134 * Linux uses only oneshot alarms.
135 *
136 * When we see that here, we deal with it by using values from
137 * a current RTC timestamp for any missing (-1) values. The
138 * RTC driver prevents "periodic alarm" modes.
139 *
140 * But this can be racey, because some fields of the RTC timestamp
141 * may have wrapped in the interval since we read the RTC alarm,
142 * which would lead to us inserting inconsistent values in place
143 * of the -1 fields.
144 *
145 * Reading the alarm and timestamp in the reverse sequence
146 * would have the same race condition, and not solve the issue.
147 *
148 * So, we must first read the RTC timestamp,
149 * then read the RTC alarm value,
150 * and then read a second RTC timestamp.
151 *
152 * If any fields of the second timestamp have changed
153 * when compared with the first timestamp, then we know
154 * our timestamp may be inconsistent with that used by
155 * the low-level rtc_read_alarm_internal() function.
156 *
157 * So, when the two timestamps disagree, we just loop and do
158 * the process again to get a fully consistent set of values.
159 *
160 * This could all instead be done in the lower level driver,
161 * but since more than one lower level RTC implementation needs it,
162 * then it's probably best best to do it here instead of there..
163 */
164
165 /* Get the "before" timestamp */
166 err = rtc_read_time(rtc, &before);
167 if (err < 0)
168 return err;
169 do {
170 if (!first_time)
171 memcpy(&before, &now, sizeof(struct rtc_time));
172 first_time = 0;
173
174 /* get the RTC alarm values, which may be incomplete */
175 err = rtc_read_alarm_internal(rtc, alarm);
176 if (err)
177 return err;
178
179 /* full-function RTCs won't have such missing fields */
180 if (rtc_valid_tm(&alarm->time) == 0)
181 return 0;
182
183 /* get the "after" timestamp, to detect wrapped fields */
184 err = rtc_read_time(rtc, &now);
185 if (err < 0)
186 return err;
187
188 /* note that tm_sec is a "don't care" value here: */
189 } while ( before.tm_min != now.tm_min
190 || before.tm_hour != now.tm_hour
191 || before.tm_mon != now.tm_mon
192 || before.tm_year != now.tm_year);
193
194 /* Fill in the missing alarm fields using the timestamp; we
195 * know there's at least one since alarm->time is invalid.
196 */
197 if (alarm->time.tm_sec == -1)
198 alarm->time.tm_sec = now.tm_sec;
199 if (alarm->time.tm_min == -1)
200 alarm->time.tm_min = now.tm_min;
201 if (alarm->time.tm_hour == -1)
202 alarm->time.tm_hour = now.tm_hour;
203
204 /* For simplicity, only support date rollover for now */
205 if (alarm->time.tm_mday < 1 || alarm->time.tm_mday > 31) {
206 alarm->time.tm_mday = now.tm_mday;
207 missing = day;
208 }
209 if ((unsigned)alarm->time.tm_mon >= 12) {
210 alarm->time.tm_mon = now.tm_mon;
211 if (missing == none)
212 missing = month;
213 }
214 if (alarm->time.tm_year == -1) {
215 alarm->time.tm_year = now.tm_year;
216 if (missing == none)
217 missing = year;
218 }
219
220 /* Can't proceed if alarm is still invalid after replacing
221 * missing fields.
222 */
223 err = rtc_valid_tm(&alarm->time);
224 if (err)
225 goto done;
226
227 /* with luck, no rollover is needed */
228 t_now = rtc_tm_to_time64(&now);
229 t_alm = rtc_tm_to_time64(&alarm->time);
230 if (t_now < t_alm)
231 goto done;
232
233 switch (missing) {
234
235 /* 24 hour rollover ... if it's now 10am Monday, an alarm that
236 * that will trigger at 5am will do so at 5am Tuesday, which
237 * could also be in the next month or year. This is a common
238 * case, especially for PCs.
239 */
240 case day:
241 dev_dbg(&rtc->dev, "alarm rollover: %s\n", "day");
242 t_alm += 24 * 60 * 60;
243 rtc_time64_to_tm(t_alm, &alarm->time);
244 break;
245
246 /* Month rollover ... if it's the 31th, an alarm on the 3rd will
247 * be next month. An alarm matching on the 30th, 29th, or 28th
248 * may end up in the month after that! Many newer PCs support
249 * this type of alarm.
250 */
251 case month:
252 dev_dbg(&rtc->dev, "alarm rollover: %s\n", "month");
253 do {
254 if (alarm->time.tm_mon < 11)
255 alarm->time.tm_mon++;
256 else {
257 alarm->time.tm_mon = 0;
258 alarm->time.tm_year++;
259 }
260 days = rtc_month_days(alarm->time.tm_mon,
261 alarm->time.tm_year);
262 } while (days < alarm->time.tm_mday);
263 break;
264
265 /* Year rollover ... easy except for leap years! */
266 case year:
267 dev_dbg(&rtc->dev, "alarm rollover: %s\n", "year");
268 do {
269 alarm->time.tm_year++;
270 } while (!is_leap_year(alarm->time.tm_year + 1900)
271 && rtc_valid_tm(&alarm->time) != 0);
272 break;
273
274 default:
275 dev_warn(&rtc->dev, "alarm rollover not handled\n");
276 }
277
278 err = rtc_valid_tm(&alarm->time);
279
280 done:
281 if (err) {
282 dev_warn(&rtc->dev, "invalid alarm value: %d-%d-%d %d:%d:%d\n",
283 alarm->time.tm_year + 1900, alarm->time.tm_mon + 1,
284 alarm->time.tm_mday, alarm->time.tm_hour, alarm->time.tm_min,
285 alarm->time.tm_sec);
286 }
287
288 return err;
289 }
290
rtc_read_alarm(struct rtc_device * rtc,struct rtc_wkalrm * alarm)291 int rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
292 {
293 int err;
294
295 err = mutex_lock_interruptible(&rtc->ops_lock);
296 if (err)
297 return err;
298 if (rtc->ops == NULL)
299 err = -ENODEV;
300 else if (!rtc->ops->read_alarm)
301 err = -EINVAL;
302 else {
303 memset(alarm, 0, sizeof(struct rtc_wkalrm));
304 alarm->enabled = rtc->aie_timer.enabled;
305 alarm->time = rtc_ktime_to_tm(rtc->aie_timer.node.expires);
306 }
307 mutex_unlock(&rtc->ops_lock);
308
309 return err;
310 }
311 EXPORT_SYMBOL_GPL(rtc_read_alarm);
312
__rtc_set_alarm(struct rtc_device * rtc,struct rtc_wkalrm * alarm)313 static int __rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
314 {
315 struct rtc_time tm;
316 time64_t now, scheduled;
317 int err;
318
319 err = rtc_valid_tm(&alarm->time);
320 if (err)
321 return err;
322 scheduled = rtc_tm_to_time64(&alarm->time);
323
324 /* Make sure we're not setting alarms in the past */
325 err = __rtc_read_time(rtc, &tm);
326 if (err)
327 return err;
328 now = rtc_tm_to_time64(&tm);
329 if (scheduled <= now)
330 return -ETIME;
331 /*
332 * XXX - We just checked to make sure the alarm time is not
333 * in the past, but there is still a race window where if
334 * the is alarm set for the next second and the second ticks
335 * over right here, before we set the alarm.
336 */
337
338 if (!rtc->ops)
339 err = -ENODEV;
340 else if (!rtc->ops->set_alarm)
341 err = -EINVAL;
342 else
343 err = rtc->ops->set_alarm(rtc->dev.parent, alarm);
344
345 return err;
346 }
347
rtc_set_alarm(struct rtc_device * rtc,struct rtc_wkalrm * alarm)348 int rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
349 {
350 int err;
351
352 if (!rtc->ops)
353 return -ENODEV;
354 else if (!rtc->ops->set_alarm)
355 return -EINVAL;
356
357 err = rtc_valid_tm(&alarm->time);
358 if (err != 0)
359 return err;
360
361 err = mutex_lock_interruptible(&rtc->ops_lock);
362 if (err)
363 return err;
364 if (rtc->aie_timer.enabled)
365 rtc_timer_remove(rtc, &rtc->aie_timer);
366
367 rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time);
368 rtc->aie_timer.period = ktime_set(0, 0);
369 if (alarm->enabled)
370 err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
371
372 mutex_unlock(&rtc->ops_lock);
373 return err;
374 }
375 EXPORT_SYMBOL_GPL(rtc_set_alarm);
376
377 /* Called once per device from rtc_device_register */
rtc_initialize_alarm(struct rtc_device * rtc,struct rtc_wkalrm * alarm)378 int rtc_initialize_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
379 {
380 int err;
381 struct rtc_time now;
382
383 err = rtc_valid_tm(&alarm->time);
384 if (err != 0)
385 return err;
386
387 err = rtc_read_time(rtc, &now);
388 if (err)
389 return err;
390
391 err = mutex_lock_interruptible(&rtc->ops_lock);
392 if (err)
393 return err;
394
395 rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time);
396 rtc->aie_timer.period = ktime_set(0, 0);
397
398 /* Alarm has to be enabled & in the futrure for us to enqueue it */
399 if (alarm->enabled && (rtc_tm_to_ktime(now).tv64 <
400 rtc->aie_timer.node.expires.tv64)) {
401
402 rtc->aie_timer.enabled = 1;
403 timerqueue_add(&rtc->timerqueue, &rtc->aie_timer.node);
404 }
405 mutex_unlock(&rtc->ops_lock);
406 return err;
407 }
408 EXPORT_SYMBOL_GPL(rtc_initialize_alarm);
409
410
411
rtc_alarm_irq_enable(struct rtc_device * rtc,unsigned int enabled)412 int rtc_alarm_irq_enable(struct rtc_device *rtc, unsigned int enabled)
413 {
414 int err = mutex_lock_interruptible(&rtc->ops_lock);
415 if (err)
416 return err;
417
418 if (rtc->aie_timer.enabled != enabled) {
419 if (enabled)
420 err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
421 else
422 rtc_timer_remove(rtc, &rtc->aie_timer);
423 }
424
425 if (err)
426 /* nothing */;
427 else if (!rtc->ops)
428 err = -ENODEV;
429 else if (!rtc->ops->alarm_irq_enable)
430 err = -EINVAL;
431 else
432 err = rtc->ops->alarm_irq_enable(rtc->dev.parent, enabled);
433
434 mutex_unlock(&rtc->ops_lock);
435 return err;
436 }
437 EXPORT_SYMBOL_GPL(rtc_alarm_irq_enable);
438
rtc_update_irq_enable(struct rtc_device * rtc,unsigned int enabled)439 int rtc_update_irq_enable(struct rtc_device *rtc, unsigned int enabled)
440 {
441 int err = mutex_lock_interruptible(&rtc->ops_lock);
442 if (err)
443 return err;
444
445 #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
446 if (enabled == 0 && rtc->uie_irq_active) {
447 mutex_unlock(&rtc->ops_lock);
448 return rtc_dev_update_irq_enable_emul(rtc, 0);
449 }
450 #endif
451 /* make sure we're changing state */
452 if (rtc->uie_rtctimer.enabled == enabled)
453 goto out;
454
455 if (rtc->uie_unsupported) {
456 err = -EINVAL;
457 goto out;
458 }
459
460 if (enabled) {
461 struct rtc_time tm;
462 ktime_t now, onesec;
463
464 __rtc_read_time(rtc, &tm);
465 onesec = ktime_set(1, 0);
466 now = rtc_tm_to_ktime(tm);
467 rtc->uie_rtctimer.node.expires = ktime_add(now, onesec);
468 rtc->uie_rtctimer.period = ktime_set(1, 0);
469 err = rtc_timer_enqueue(rtc, &rtc->uie_rtctimer);
470 } else
471 rtc_timer_remove(rtc, &rtc->uie_rtctimer);
472
473 out:
474 mutex_unlock(&rtc->ops_lock);
475 #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
476 /*
477 * Enable emulation if the driver did not provide
478 * the update_irq_enable function pointer or if returned
479 * -EINVAL to signal that it has been configured without
480 * interrupts or that are not available at the moment.
481 */
482 if (err == -EINVAL)
483 err = rtc_dev_update_irq_enable_emul(rtc, enabled);
484 #endif
485 return err;
486
487 }
488 EXPORT_SYMBOL_GPL(rtc_update_irq_enable);
489
490
491 /**
492 * rtc_handle_legacy_irq - AIE, UIE and PIE event hook
493 * @rtc: pointer to the rtc device
494 *
495 * This function is called when an AIE, UIE or PIE mode interrupt
496 * has occurred (or been emulated).
497 *
498 * Triggers the registered irq_task function callback.
499 */
rtc_handle_legacy_irq(struct rtc_device * rtc,int num,int mode)500 void rtc_handle_legacy_irq(struct rtc_device *rtc, int num, int mode)
501 {
502 unsigned long flags;
503
504 /* mark one irq of the appropriate mode */
505 spin_lock_irqsave(&rtc->irq_lock, flags);
506 rtc->irq_data = (rtc->irq_data + (num << 8)) | (RTC_IRQF|mode);
507 spin_unlock_irqrestore(&rtc->irq_lock, flags);
508
509 /* call the task func */
510 spin_lock_irqsave(&rtc->irq_task_lock, flags);
511 if (rtc->irq_task)
512 rtc->irq_task->func(rtc->irq_task->private_data);
513 spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
514
515 wake_up_interruptible(&rtc->irq_queue);
516 kill_fasync(&rtc->async_queue, SIGIO, POLL_IN);
517 }
518
519
520 /**
521 * rtc_aie_update_irq - AIE mode rtctimer hook
522 * @private: pointer to the rtc_device
523 *
524 * This functions is called when the aie_timer expires.
525 */
rtc_aie_update_irq(void * private)526 void rtc_aie_update_irq(void *private)
527 {
528 struct rtc_device *rtc = (struct rtc_device *)private;
529 rtc_handle_legacy_irq(rtc, 1, RTC_AF);
530 }
531
532
533 /**
534 * rtc_uie_update_irq - UIE mode rtctimer hook
535 * @private: pointer to the rtc_device
536 *
537 * This functions is called when the uie_timer expires.
538 */
rtc_uie_update_irq(void * private)539 void rtc_uie_update_irq(void *private)
540 {
541 struct rtc_device *rtc = (struct rtc_device *)private;
542 rtc_handle_legacy_irq(rtc, 1, RTC_UF);
543 }
544
545
546 /**
547 * rtc_pie_update_irq - PIE mode hrtimer hook
548 * @timer: pointer to the pie mode hrtimer
549 *
550 * This function is used to emulate PIE mode interrupts
551 * using an hrtimer. This function is called when the periodic
552 * hrtimer expires.
553 */
rtc_pie_update_irq(struct hrtimer * timer)554 enum hrtimer_restart rtc_pie_update_irq(struct hrtimer *timer)
555 {
556 struct rtc_device *rtc;
557 ktime_t period;
558 int count;
559 rtc = container_of(timer, struct rtc_device, pie_timer);
560
561 period = ktime_set(0, NSEC_PER_SEC/rtc->irq_freq);
562 count = hrtimer_forward_now(timer, period);
563
564 rtc_handle_legacy_irq(rtc, count, RTC_PF);
565
566 return HRTIMER_RESTART;
567 }
568
569 /**
570 * rtc_update_irq - Triggered when a RTC interrupt occurs.
571 * @rtc: the rtc device
572 * @num: how many irqs are being reported (usually one)
573 * @events: mask of RTC_IRQF with one or more of RTC_PF, RTC_AF, RTC_UF
574 * Context: any
575 */
rtc_update_irq(struct rtc_device * rtc,unsigned long num,unsigned long events)576 void rtc_update_irq(struct rtc_device *rtc,
577 unsigned long num, unsigned long events)
578 {
579 if (IS_ERR_OR_NULL(rtc))
580 return;
581
582 pm_stay_awake(rtc->dev.parent);
583 schedule_work(&rtc->irqwork);
584 }
585 EXPORT_SYMBOL_GPL(rtc_update_irq);
586
__rtc_match(struct device * dev,const void * data)587 static int __rtc_match(struct device *dev, const void *data)
588 {
589 const char *name = data;
590
591 if (strcmp(dev_name(dev), name) == 0)
592 return 1;
593 return 0;
594 }
595
rtc_class_open(const char * name)596 struct rtc_device *rtc_class_open(const char *name)
597 {
598 struct device *dev;
599 struct rtc_device *rtc = NULL;
600
601 dev = class_find_device(rtc_class, NULL, name, __rtc_match);
602 if (dev)
603 rtc = to_rtc_device(dev);
604
605 if (rtc) {
606 if (!try_module_get(rtc->owner)) {
607 put_device(dev);
608 rtc = NULL;
609 }
610 }
611
612 return rtc;
613 }
614 EXPORT_SYMBOL_GPL(rtc_class_open);
615
rtc_class_close(struct rtc_device * rtc)616 void rtc_class_close(struct rtc_device *rtc)
617 {
618 module_put(rtc->owner);
619 put_device(&rtc->dev);
620 }
621 EXPORT_SYMBOL_GPL(rtc_class_close);
622
rtc_irq_register(struct rtc_device * rtc,struct rtc_task * task)623 int rtc_irq_register(struct rtc_device *rtc, struct rtc_task *task)
624 {
625 int retval = -EBUSY;
626
627 if (task == NULL || task->func == NULL)
628 return -EINVAL;
629
630 /* Cannot register while the char dev is in use */
631 if (test_and_set_bit_lock(RTC_DEV_BUSY, &rtc->flags))
632 return -EBUSY;
633
634 spin_lock_irq(&rtc->irq_task_lock);
635 if (rtc->irq_task == NULL) {
636 rtc->irq_task = task;
637 retval = 0;
638 }
639 spin_unlock_irq(&rtc->irq_task_lock);
640
641 clear_bit_unlock(RTC_DEV_BUSY, &rtc->flags);
642
643 return retval;
644 }
645 EXPORT_SYMBOL_GPL(rtc_irq_register);
646
rtc_irq_unregister(struct rtc_device * rtc,struct rtc_task * task)647 void rtc_irq_unregister(struct rtc_device *rtc, struct rtc_task *task)
648 {
649 spin_lock_irq(&rtc->irq_task_lock);
650 if (rtc->irq_task == task)
651 rtc->irq_task = NULL;
652 spin_unlock_irq(&rtc->irq_task_lock);
653 }
654 EXPORT_SYMBOL_GPL(rtc_irq_unregister);
655
rtc_update_hrtimer(struct rtc_device * rtc,int enabled)656 static int rtc_update_hrtimer(struct rtc_device *rtc, int enabled)
657 {
658 /*
659 * We always cancel the timer here first, because otherwise
660 * we could run into BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
661 * when we manage to start the timer before the callback
662 * returns HRTIMER_RESTART.
663 *
664 * We cannot use hrtimer_cancel() here as a running callback
665 * could be blocked on rtc->irq_task_lock and hrtimer_cancel()
666 * would spin forever.
667 */
668 if (hrtimer_try_to_cancel(&rtc->pie_timer) < 0)
669 return -1;
670
671 if (enabled) {
672 ktime_t period = ktime_set(0, NSEC_PER_SEC / rtc->irq_freq);
673
674 hrtimer_start(&rtc->pie_timer, period, HRTIMER_MODE_REL);
675 }
676 return 0;
677 }
678
679 /**
680 * rtc_irq_set_state - enable/disable 2^N Hz periodic IRQs
681 * @rtc: the rtc device
682 * @task: currently registered with rtc_irq_register()
683 * @enabled: true to enable periodic IRQs
684 * Context: any
685 *
686 * Note that rtc_irq_set_freq() should previously have been used to
687 * specify the desired frequency of periodic IRQ task->func() callbacks.
688 */
rtc_irq_set_state(struct rtc_device * rtc,struct rtc_task * task,int enabled)689 int rtc_irq_set_state(struct rtc_device *rtc, struct rtc_task *task, int enabled)
690 {
691 int err = 0;
692 unsigned long flags;
693
694 retry:
695 spin_lock_irqsave(&rtc->irq_task_lock, flags);
696 if (rtc->irq_task != NULL && task == NULL)
697 err = -EBUSY;
698 else if (rtc->irq_task != task)
699 err = -EACCES;
700 else {
701 if (rtc_update_hrtimer(rtc, enabled) < 0) {
702 spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
703 cpu_relax();
704 goto retry;
705 }
706 rtc->pie_enabled = enabled;
707 }
708 spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
709 return err;
710 }
711 EXPORT_SYMBOL_GPL(rtc_irq_set_state);
712
713 /**
714 * rtc_irq_set_freq - set 2^N Hz periodic IRQ frequency for IRQ
715 * @rtc: the rtc device
716 * @task: currently registered with rtc_irq_register()
717 * @freq: positive frequency with which task->func() will be called
718 * Context: any
719 *
720 * Note that rtc_irq_set_state() is used to enable or disable the
721 * periodic IRQs.
722 */
rtc_irq_set_freq(struct rtc_device * rtc,struct rtc_task * task,int freq)723 int rtc_irq_set_freq(struct rtc_device *rtc, struct rtc_task *task, int freq)
724 {
725 int err = 0;
726 unsigned long flags;
727
728 if (freq <= 0 || freq > RTC_MAX_FREQ)
729 return -EINVAL;
730 retry:
731 spin_lock_irqsave(&rtc->irq_task_lock, flags);
732 if (rtc->irq_task != NULL && task == NULL)
733 err = -EBUSY;
734 else if (rtc->irq_task != task)
735 err = -EACCES;
736 else {
737 rtc->irq_freq = freq;
738 if (rtc->pie_enabled && rtc_update_hrtimer(rtc, 1) < 0) {
739 spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
740 cpu_relax();
741 goto retry;
742 }
743 }
744 spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
745 return err;
746 }
747 EXPORT_SYMBOL_GPL(rtc_irq_set_freq);
748
749 /**
750 * rtc_timer_enqueue - Adds a rtc_timer to the rtc_device timerqueue
751 * @rtc rtc device
752 * @timer timer being added.
753 *
754 * Enqueues a timer onto the rtc devices timerqueue and sets
755 * the next alarm event appropriately.
756 *
757 * Sets the enabled bit on the added timer.
758 *
759 * Must hold ops_lock for proper serialization of timerqueue
760 */
rtc_timer_enqueue(struct rtc_device * rtc,struct rtc_timer * timer)761 static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer)
762 {
763 struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue);
764 struct rtc_time tm;
765 ktime_t now;
766
767 timer->enabled = 1;
768 __rtc_read_time(rtc, &tm);
769 now = rtc_tm_to_ktime(tm);
770
771 /* Skip over expired timers */
772 while (next) {
773 if (next->expires.tv64 >= now.tv64)
774 break;
775 next = timerqueue_iterate_next(next);
776 }
777
778 timerqueue_add(&rtc->timerqueue, &timer->node);
779 if (!next || ktime_before(timer->node.expires, next->expires)) {
780 struct rtc_wkalrm alarm;
781 int err;
782 alarm.time = rtc_ktime_to_tm(timer->node.expires);
783 alarm.enabled = 1;
784 err = __rtc_set_alarm(rtc, &alarm);
785 if (err == -ETIME) {
786 pm_stay_awake(rtc->dev.parent);
787 schedule_work(&rtc->irqwork);
788 } else if (err) {
789 timerqueue_del(&rtc->timerqueue, &timer->node);
790 timer->enabled = 0;
791 return err;
792 }
793 }
794 return 0;
795 }
796
rtc_alarm_disable(struct rtc_device * rtc)797 static void rtc_alarm_disable(struct rtc_device *rtc)
798 {
799 if (!rtc->ops || !rtc->ops->alarm_irq_enable)
800 return;
801
802 rtc->ops->alarm_irq_enable(rtc->dev.parent, false);
803 }
804
805 /**
806 * rtc_timer_remove - Removes a rtc_timer from the rtc_device timerqueue
807 * @rtc rtc device
808 * @timer timer being removed.
809 *
810 * Removes a timer onto the rtc devices timerqueue and sets
811 * the next alarm event appropriately.
812 *
813 * Clears the enabled bit on the removed timer.
814 *
815 * Must hold ops_lock for proper serialization of timerqueue
816 */
rtc_timer_remove(struct rtc_device * rtc,struct rtc_timer * timer)817 static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer)
818 {
819 struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue);
820 timerqueue_del(&rtc->timerqueue, &timer->node);
821 timer->enabled = 0;
822 if (next == &timer->node) {
823 struct rtc_wkalrm alarm;
824 int err;
825 next = timerqueue_getnext(&rtc->timerqueue);
826 if (!next) {
827 rtc_alarm_disable(rtc);
828 return;
829 }
830 alarm.time = rtc_ktime_to_tm(next->expires);
831 alarm.enabled = 1;
832 err = __rtc_set_alarm(rtc, &alarm);
833 if (err == -ETIME) {
834 pm_stay_awake(rtc->dev.parent);
835 schedule_work(&rtc->irqwork);
836 }
837 }
838 }
839
840 /**
841 * rtc_timer_do_work - Expires rtc timers
842 * @rtc rtc device
843 * @timer timer being removed.
844 *
845 * Expires rtc timers. Reprograms next alarm event if needed.
846 * Called via worktask.
847 *
848 * Serializes access to timerqueue via ops_lock mutex
849 */
rtc_timer_do_work(struct work_struct * work)850 void rtc_timer_do_work(struct work_struct *work)
851 {
852 struct rtc_timer *timer;
853 struct timerqueue_node *next;
854 ktime_t now;
855 struct rtc_time tm;
856
857 struct rtc_device *rtc =
858 container_of(work, struct rtc_device, irqwork);
859
860 mutex_lock(&rtc->ops_lock);
861 again:
862 __rtc_read_time(rtc, &tm);
863 now = rtc_tm_to_ktime(tm);
864 while ((next = timerqueue_getnext(&rtc->timerqueue))) {
865 if (next->expires.tv64 > now.tv64)
866 break;
867
868 /* expire timer */
869 timer = container_of(next, struct rtc_timer, node);
870 timerqueue_del(&rtc->timerqueue, &timer->node);
871 timer->enabled = 0;
872 if (timer->task.func)
873 timer->task.func(timer->task.private_data);
874
875 /* Re-add/fwd periodic timers */
876 if (ktime_to_ns(timer->period)) {
877 timer->node.expires = ktime_add(timer->node.expires,
878 timer->period);
879 timer->enabled = 1;
880 timerqueue_add(&rtc->timerqueue, &timer->node);
881 }
882 }
883
884 /* Set next alarm */
885 if (next) {
886 struct rtc_wkalrm alarm;
887 int err;
888 int retry = 3;
889
890 alarm.time = rtc_ktime_to_tm(next->expires);
891 alarm.enabled = 1;
892 reprogram:
893 err = __rtc_set_alarm(rtc, &alarm);
894 if (err == -ETIME)
895 goto again;
896 else if (err) {
897 if (retry-- > 0)
898 goto reprogram;
899
900 timer = container_of(next, struct rtc_timer, node);
901 timerqueue_del(&rtc->timerqueue, &timer->node);
902 timer->enabled = 0;
903 dev_err(&rtc->dev, "__rtc_set_alarm: err=%d\n", err);
904 goto again;
905 }
906 } else
907 rtc_alarm_disable(rtc);
908
909 pm_relax(rtc->dev.parent);
910 mutex_unlock(&rtc->ops_lock);
911 }
912
913
914 /* rtc_timer_init - Initializes an rtc_timer
915 * @timer: timer to be intiialized
916 * @f: function pointer to be called when timer fires
917 * @data: private data passed to function pointer
918 *
919 * Kernel interface to initializing an rtc_timer.
920 */
rtc_timer_init(struct rtc_timer * timer,void (* f)(void * p),void * data)921 void rtc_timer_init(struct rtc_timer *timer, void (*f)(void *p), void *data)
922 {
923 timerqueue_init(&timer->node);
924 timer->enabled = 0;
925 timer->task.func = f;
926 timer->task.private_data = data;
927 }
928
929 /* rtc_timer_start - Sets an rtc_timer to fire in the future
930 * @ rtc: rtc device to be used
931 * @ timer: timer being set
932 * @ expires: time at which to expire the timer
933 * @ period: period that the timer will recur
934 *
935 * Kernel interface to set an rtc_timer
936 */
rtc_timer_start(struct rtc_device * rtc,struct rtc_timer * timer,ktime_t expires,ktime_t period)937 int rtc_timer_start(struct rtc_device *rtc, struct rtc_timer *timer,
938 ktime_t expires, ktime_t period)
939 {
940 int ret = 0;
941 mutex_lock(&rtc->ops_lock);
942 if (timer->enabled)
943 rtc_timer_remove(rtc, timer);
944
945 timer->node.expires = expires;
946 timer->period = period;
947
948 ret = rtc_timer_enqueue(rtc, timer);
949
950 mutex_unlock(&rtc->ops_lock);
951 return ret;
952 }
953
954 /* rtc_timer_cancel - Stops an rtc_timer
955 * @ rtc: rtc device to be used
956 * @ timer: timer being set
957 *
958 * Kernel interface to cancel an rtc_timer
959 */
rtc_timer_cancel(struct rtc_device * rtc,struct rtc_timer * timer)960 void rtc_timer_cancel(struct rtc_device *rtc, struct rtc_timer *timer)
961 {
962 mutex_lock(&rtc->ops_lock);
963 if (timer->enabled)
964 rtc_timer_remove(rtc, timer);
965 mutex_unlock(&rtc->ops_lock);
966 }
967
968
969