xref: /openbmc/linux/drivers/rtc/interface.c (revision aa0be0f4)
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/log2.h>
17 #include <linux/workqueue.h>
18 
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 
22 static 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 }
35 
36 int rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm)
37 {
38 	int err;
39 
40 	err = mutex_lock_interruptible(&rtc->ops_lock);
41 	if (err)
42 		return err;
43 
44 	err = __rtc_read_time(rtc, tm);
45 	mutex_unlock(&rtc->ops_lock);
46 	return err;
47 }
48 EXPORT_SYMBOL_GPL(rtc_read_time);
49 
50 int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm)
51 {
52 	int err;
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)
60 		return err;
61 
62 	if (!rtc->ops)
63 		err = -ENODEV;
64 	else if (rtc->ops->set_time)
65 		err = rtc->ops->set_time(rtc->dev.parent, tm);
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;
73 
74 	mutex_unlock(&rtc->ops_lock);
75 	return err;
76 }
77 EXPORT_SYMBOL_GPL(rtc_set_time);
78 
79 int rtc_set_mmss(struct rtc_device *rtc, unsigned long secs)
80 {
81 	int err;
82 
83 	err = mutex_lock_interruptible(&rtc->ops_lock);
84 	if (err)
85 		return err;
86 
87 	if (!rtc->ops)
88 		err = -ENODEV;
89 	else if (rtc->ops->set_mmss)
90 		err = rtc->ops->set_mmss(rtc->dev.parent, secs);
91 	else if (rtc->ops->read_time && rtc->ops->set_time) {
92 		struct rtc_time new, old;
93 
94 		err = rtc->ops->read_time(rtc->dev.parent, &old);
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)))
106 				err = rtc->ops->set_time(rtc->dev.parent,
107 						&new);
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 
119 int rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
120 {
121 	int err;
122 
123 	err = mutex_lock_interruptible(&rtc->ops_lock);
124 	if (err)
125 		return err;
126 	alarm->enabled = rtc->aie_timer.enabled;
127 	if (alarm->enabled)
128 		alarm->time = rtc_ktime_to_tm(rtc->aie_timer.node.expires);
129 	mutex_unlock(&rtc->ops_lock);
130 
131 	return 0;
132 }
133 EXPORT_SYMBOL_GPL(rtc_read_alarm);
134 
135 int __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 
168 int rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
169 {
170 	int err;
171 
172 	err = rtc_valid_tm(&alarm->time);
173 	if (err != 0)
174 		return err;
175 
176 	err = mutex_lock_interruptible(&rtc->ops_lock);
177 	if (err)
178 		return err;
179 	if (rtc->aie_timer.enabled) {
180 		rtc_timer_remove(rtc, &rtc->aie_timer);
181 	}
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) {
185 		err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
186 	}
187 	mutex_unlock(&rtc->ops_lock);
188 	return err;
189 }
190 EXPORT_SYMBOL_GPL(rtc_set_alarm);
191 
192 int 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 
198 	if (rtc->aie_timer.enabled != enabled) {
199 		if (enabled)
200 			err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
201 		else
202 			rtc_timer_remove(rtc, &rtc->aie_timer);
203 	}
204 
205 	if (err)
206 		return err;
207 
208 	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 }
218 EXPORT_SYMBOL_GPL(rtc_alarm_irq_enable);
219 
220 int 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 
226 	/* 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);
239 		err = rtc_timer_enqueue(rtc, &rtc->uie_rtctimer);
240 	} else
241 		rtc_timer_remove(rtc, &rtc->uie_rtctimer);
242 
243 out:
244 	mutex_unlock(&rtc->ops_lock);
245 	return err;
246 
247 }
248 EXPORT_SYMBOL_GPL(rtc_update_irq_enable);
249 
250 
251 /**
252  * 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  */
260 static 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  */
286 void 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  */
299 void 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  */
314 enum 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.
331  * @rtc: the rtc device
332  * @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
334  * Context: any
335  */
336 void rtc_update_irq(struct rtc_device *rtc,
337 		unsigned long num, unsigned long events)
338 {
339 	schedule_work(&rtc->irqwork);
340 }
341 EXPORT_SYMBOL_GPL(rtc_update_irq);
342 
343 static int __rtc_match(struct device *dev, void *data)
344 {
345 	char *name = (char *)data;
346 
347 	if (strcmp(dev_name(dev), name) == 0)
348 		return 1;
349 	return 0;
350 }
351 
352 struct rtc_device *rtc_class_open(char *name)
353 {
354 	struct device *dev;
355 	struct rtc_device *rtc = NULL;
356 
357 	dev = class_find_device(rtc_class, NULL, name, __rtc_match);
358 	if (dev)
359 		rtc = to_rtc_device(dev);
360 
361 	if (rtc) {
362 		if (!try_module_get(rtc->owner)) {
363 			put_device(dev);
364 			rtc = NULL;
365 		}
366 	}
367 
368 	return rtc;
369 }
370 EXPORT_SYMBOL_GPL(rtc_class_open);
371 
372 void rtc_class_close(struct rtc_device *rtc)
373 {
374 	module_put(rtc->owner);
375 	put_device(&rtc->dev);
376 }
377 EXPORT_SYMBOL_GPL(rtc_class_close);
378 
379 int rtc_irq_register(struct rtc_device *rtc, struct rtc_task *task)
380 {
381 	int retval = -EBUSY;
382 
383 	if (task == NULL || task->func == NULL)
384 		return -EINVAL;
385 
386 	/* Cannot register while the char dev is in use */
387 	if (test_and_set_bit_lock(RTC_DEV_BUSY, &rtc->flags))
388 		return -EBUSY;
389 
390 	spin_lock_irq(&rtc->irq_task_lock);
391 	if (rtc->irq_task == NULL) {
392 		rtc->irq_task = task;
393 		retval = 0;
394 	}
395 	spin_unlock_irq(&rtc->irq_task_lock);
396 
397 	clear_bit_unlock(RTC_DEV_BUSY, &rtc->flags);
398 
399 	return retval;
400 }
401 EXPORT_SYMBOL_GPL(rtc_irq_register);
402 
403 void rtc_irq_unregister(struct rtc_device *rtc, struct rtc_task *task)
404 {
405 	spin_lock_irq(&rtc->irq_task_lock);
406 	if (rtc->irq_task == task)
407 		rtc->irq_task = NULL;
408 	spin_unlock_irq(&rtc->irq_task_lock);
409 }
410 EXPORT_SYMBOL_GPL(rtc_irq_unregister);
411 
412 /**
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  */
422 int rtc_irq_set_state(struct rtc_device *rtc, struct rtc_task *task, int enabled)
423 {
424 	int err = 0;
425 	unsigned long flags;
426 
427 	spin_lock_irqsave(&rtc->irq_task_lock, flags);
428 	if (rtc->irq_task != NULL && task == NULL)
429 		err = -EBUSY;
430 	if (rtc->irq_task != task)
431 		err = -EACCES;
432 
433 	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);
441 
442 	return err;
443 }
444 EXPORT_SYMBOL_GPL(rtc_irq_set_state);
445 
446 /**
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  */
456 int rtc_irq_set_freq(struct rtc_device *rtc, struct rtc_task *task, int freq)
457 {
458 	int err = 0;
459 	unsigned long flags;
460 
461 	spin_lock_irqsave(&rtc->irq_task_lock, flags);
462 	if (rtc->irq_task != NULL && task == NULL)
463 		err = -EBUSY;
464 	if (rtc->irq_task != task)
465 		err = -EACCES;
466 	if (err == 0) {
467 		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 		}
475 	}
476 	spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
477 	return err;
478 }
479 EXPORT_SYMBOL_GPL(rtc_irq_set_freq);
480 
481 /**
482  * rtc_timer_enqueue - Adds a rtc_timer to the rtc_device timerqueue
483  * @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  *
489  * Sets the enabled bit on the added timer.
490  *
491  * Must hold ops_lock for proper serialization of timerqueue
492  */
493 static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer)
494 {
495 	timer->enabled = 1;
496 	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);
505 		else if (err) {
506 			timerqueue_del(&rtc->timerqueue, &timer->node);
507 			timer->enabled = 0;
508 			return err;
509 		}
510 	}
511 	return 0;
512 }
513 
514 /**
515  * rtc_timer_remove - Removes a rtc_timer from the rtc_device timerqueue
516  * @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  *
522  * Clears the enabled bit on the removed timer.
523  *
524  * Must hold ops_lock for proper serialization of timerqueue
525  */
526 static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer)
527 {
528 	struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue);
529 	timerqueue_del(&rtc->timerqueue, &timer->node);
530 	timer->enabled = 0;
531 	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 /**
546  * rtc_timer_do_work - Expires rtc timers
547  * @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  */
555 void rtc_timer_do_work(struct work_struct *work)
556 {
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);
566 again:
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 
604 /* rtc_timer_init - Initializes an rtc_timer
605  * @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  */
611 void rtc_timer_init(struct rtc_timer *timer, void (*f)(void* p), void* data)
612 {
613 	timerqueue_init(&timer->node);
614 	timer->enabled = 0;
615 	timer->task.func = f;
616 	timer->task.private_data = data;
617 }
618 
619 /* rtc_timer_start - Sets an rtc_timer to fire in the future
620  * @ 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  */
627 int rtc_timer_start(struct rtc_device *rtc, struct rtc_timer* timer,
628 			ktime_t expires, ktime_t period)
629 {
630 	int ret = 0;
631 	mutex_lock(&rtc->ops_lock);
632 	if (timer->enabled)
633 		rtc_timer_remove(rtc, timer);
634 
635 	timer->node.expires = expires;
636 	timer->period = period;
637 
638 	ret = rtc_timer_enqueue(rtc, timer);
639 
640 	mutex_unlock(&rtc->ops_lock);
641 	return ret;
642 }
643 
644 /* rtc_timer_cancel - Stops an rtc_timer
645  * @ rtc: rtc device to be used
646  * @ timer: timer being set
647  *
648  * Kernel interface to cancel an rtc_timer
649  */
650 int rtc_timer_cancel(struct rtc_device *rtc, struct rtc_timer* timer)
651 {
652 	int ret = 0;
653 	mutex_lock(&rtc->ops_lock);
654 	if (timer->enabled)
655 		rtc_timer_remove(rtc, timer);
656 	mutex_unlock(&rtc->ops_lock);
657 	return ret;
658 }
659 
660 
661