2 * linux/arch/arm/common/rtctime.c
4 * Copyright (C) 2003 Deep Blue Solutions Ltd.
5 * Based on sa1100-rtc.c, Nils Faerber, CIH, Nicolas Pitre.
6 * Based on rtc.c by Paul Gortmaker
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/time.h>
15 #include <linux/rtc.h>
16 #include <linux/poll.h>
17 #include <linux/proc_fs.h>
18 #include <linux/miscdevice.h>
19 #include <linux/spinlock.h>
20 #include <linux/capability.h>
21 #include <linux/device.h>
22 #include <linux/mutex.h>
25 #include <asm/semaphore.h>
27 static DECLARE_WAIT_QUEUE_HEAD(rtc_wait);
28 static struct fasync_struct *rtc_async_queue;
31 * rtc_lock protects rtc_irq_data
33 static DEFINE_SPINLOCK(rtc_lock);
34 static unsigned long rtc_irq_data;
37 * rtc_sem protects rtc_inuse and rtc_ops
39 static DEFINE_MUTEX(rtc_mutex);
40 static unsigned long rtc_inuse;
41 static struct rtc_ops *rtc_ops;
43 #define rtc_epoch 1900UL
45 static const unsigned char days_in_month[] = {
46 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
49 #define LEAPS_THRU_END_OF(y) ((y)/4 - (y)/100 + (y)/400)
50 #define LEAP_YEAR(year) ((!(year % 4) && (year % 100)) || !(year % 400))
52 static int month_days(unsigned int month, unsigned int year)
54 return days_in_month[month] + (LEAP_YEAR(year) && month == 1);
58 * Convert seconds since 01-01-1970 00:00:00 to Gregorian date.
60 void rtc_time_to_tm(unsigned long time, struct rtc_time *tm)
62 int days, month, year;
67 tm->tm_wday = (days + 4) % 7;
69 year = 1970 + days / 365;
70 days -= (year - 1970) * 365
71 + LEAPS_THRU_END_OF(year - 1)
72 - LEAPS_THRU_END_OF(1970 - 1);
75 days += 365 + LEAP_YEAR(year);
77 tm->tm_year = year - 1900;
78 tm->tm_yday = days + 1;
80 for (month = 0; month < 11; month++) {
83 newdays = days - month_days(month, year);
89 tm->tm_mday = days + 1;
91 tm->tm_hour = time / 3600;
92 time -= tm->tm_hour * 3600;
93 tm->tm_min = time / 60;
94 tm->tm_sec = time - tm->tm_min * 60;
96 EXPORT_SYMBOL(rtc_time_to_tm);
99 * Does the rtc_time represent a valid date/time?
101 int rtc_valid_tm(struct rtc_time *tm)
103 if (tm->tm_year < 70 ||
106 tm->tm_mday > month_days(tm->tm_mon, tm->tm_year + 1900) ||
114 EXPORT_SYMBOL(rtc_valid_tm);
117 * Convert Gregorian date to seconds since 01-01-1970 00:00:00.
119 int rtc_tm_to_time(struct rtc_time *tm, unsigned long *time)
121 *time = mktime(tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
122 tm->tm_hour, tm->tm_min, tm->tm_sec);
126 EXPORT_SYMBOL(rtc_tm_to_time);
129 * Calculate the next alarm time given the requested alarm time mask
130 * and the current time.
132 void rtc_next_alarm_time(struct rtc_time *next, struct rtc_time *now, struct rtc_time *alrm)
134 unsigned long next_time;
135 unsigned long now_time;
137 next->tm_year = now->tm_year;
138 next->tm_mon = now->tm_mon;
139 next->tm_mday = now->tm_mday;
140 next->tm_hour = alrm->tm_hour;
141 next->tm_min = alrm->tm_min;
142 next->tm_sec = alrm->tm_sec;
144 rtc_tm_to_time(now, &now_time);
145 rtc_tm_to_time(next, &next_time);
147 if (next_time < now_time) {
148 /* Advance one day */
149 next_time += 60 * 60 * 24;
150 rtc_time_to_tm(next_time, next);
154 static inline int rtc_read_time(struct rtc_ops *ops, struct rtc_time *tm)
156 memset(tm, 0, sizeof(struct rtc_time));
157 return ops->read_time(tm);
160 static inline int rtc_set_time(struct rtc_ops *ops, struct rtc_time *tm)
164 ret = rtc_valid_tm(tm);
166 ret = ops->set_time(tm);
171 static inline int rtc_read_alarm(struct rtc_ops *ops, struct rtc_wkalrm *alrm)
174 if (ops->read_alarm) {
175 memset(alrm, 0, sizeof(struct rtc_wkalrm));
176 ret = ops->read_alarm(alrm);
181 static inline int rtc_set_alarm(struct rtc_ops *ops, struct rtc_wkalrm *alrm)
185 ret = ops->set_alarm(alrm);
189 void rtc_update(unsigned long num, unsigned long events)
191 spin_lock(&rtc_lock);
192 rtc_irq_data = (rtc_irq_data + (num << 8)) | events;
193 spin_unlock(&rtc_lock);
195 wake_up_interruptible(&rtc_wait);
196 kill_fasync(&rtc_async_queue, SIGIO, POLL_IN);
198 EXPORT_SYMBOL(rtc_update);
202 rtc_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
204 DECLARE_WAITQUEUE(wait, current);
208 if (count < sizeof(unsigned long))
211 add_wait_queue(&rtc_wait, &wait);
213 __set_current_state(TASK_INTERRUPTIBLE);
215 spin_lock_irq(&rtc_lock);
218 spin_unlock_irq(&rtc_lock);
224 if (file->f_flags & O_NONBLOCK) {
228 if (signal_pending(current)) {
234 set_current_state(TASK_RUNNING);
235 remove_wait_queue(&rtc_wait, &wait);
238 ret = put_user(data, (unsigned long __user *)buf);
240 ret = sizeof(unsigned long);
245 static unsigned int rtc_poll(struct file *file, poll_table *wait)
249 poll_wait(file, &rtc_wait, wait);
251 spin_lock_irq(&rtc_lock);
253 spin_unlock_irq(&rtc_lock);
255 return data != 0 ? POLLIN | POLLRDNORM : 0;
258 static int rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
261 struct rtc_ops *ops = file->private_data;
263 struct rtc_wkalrm alrm;
264 void __user *uarg = (void __user *)arg;
269 ret = rtc_read_alarm(ops, &alrm);
272 ret = copy_to_user(uarg, &alrm.time, sizeof(tm));
278 ret = copy_from_user(&alrm.time, uarg, sizeof(tm));
285 alrm.time.tm_mday = -1;
286 alrm.time.tm_mon = -1;
287 alrm.time.tm_year = -1;
288 alrm.time.tm_wday = -1;
289 alrm.time.tm_yday = -1;
290 alrm.time.tm_isdst = -1;
291 ret = rtc_set_alarm(ops, &alrm);
295 ret = rtc_read_time(ops, &tm);
298 ret = copy_to_user(uarg, &tm, sizeof(tm));
304 if (!capable(CAP_SYS_TIME)) {
308 ret = copy_from_user(&tm, uarg, sizeof(tm));
313 ret = rtc_set_time(ops, &tm);
319 * There were no RTC clocks before 1900.
325 if (!capable(CAP_SYS_TIME)) {
335 ret = put_user(rtc_epoch, (unsigned long __user *)uarg);
339 ret = copy_from_user(&alrm, uarg, sizeof(alrm));
344 ret = rtc_set_alarm(ops, &alrm);
348 ret = rtc_read_alarm(ops, &alrm);
351 ret = copy_to_user(uarg, &alrm, sizeof(alrm));
358 ret = ops->ioctl(cmd, arg);
364 static int rtc_open(struct inode *inode, struct file *file)
368 mutex_lock(&rtc_mutex);
372 } else if (!rtc_ops || !try_module_get(rtc_ops->owner)) {
375 file->private_data = rtc_ops;
377 ret = rtc_ops->open ? rtc_ops->open() : 0;
379 spin_lock_irq(&rtc_lock);
381 spin_unlock_irq(&rtc_lock);
386 mutex_unlock(&rtc_mutex);
391 static int rtc_release(struct inode *inode, struct file *file)
393 struct rtc_ops *ops = file->private_data;
398 spin_lock_irq(&rtc_lock);
400 spin_unlock_irq(&rtc_lock);
402 module_put(rtc_ops->owner);
408 static int rtc_fasync(int fd, struct file *file, int on)
410 return fasync_helper(fd, file, on, &rtc_async_queue);
413 static struct file_operations rtc_fops = {
414 .owner = THIS_MODULE,
420 .release = rtc_release,
421 .fasync = rtc_fasync,
424 static struct miscdevice rtc_miscdev = {
431 static int rtc_read_proc(char *page, char **start, off_t off, int count, int *eof, void *data)
433 struct rtc_ops *ops = data;
434 struct rtc_wkalrm alrm;
438 if (rtc_read_time(ops, &tm) == 0) {
440 "rtc_time\t: %02d:%02d:%02d\n"
441 "rtc_date\t: %04d-%02d-%02d\n"
442 "rtc_epoch\t: %04lu\n",
443 tm.tm_hour, tm.tm_min, tm.tm_sec,
444 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
448 if (rtc_read_alarm(ops, &alrm) == 0) {
449 p += sprintf(p, "alrm_time\t: ");
450 if ((unsigned int)alrm.time.tm_hour <= 24)
451 p += sprintf(p, "%02d:", alrm.time.tm_hour);
453 p += sprintf(p, "**:");
454 if ((unsigned int)alrm.time.tm_min <= 59)
455 p += sprintf(p, "%02d:", alrm.time.tm_min);
457 p += sprintf(p, "**:");
458 if ((unsigned int)alrm.time.tm_sec <= 59)
459 p += sprintf(p, "%02d\n", alrm.time.tm_sec);
461 p += sprintf(p, "**\n");
463 p += sprintf(p, "alrm_date\t: ");
464 if ((unsigned int)alrm.time.tm_year <= 200)
465 p += sprintf(p, "%04d-", alrm.time.tm_year + 1900);
467 p += sprintf(p, "****-");
468 if ((unsigned int)alrm.time.tm_mon <= 11)
469 p += sprintf(p, "%02d-", alrm.time.tm_mon + 1);
471 p += sprintf(p, "**-");
472 if ((unsigned int)alrm.time.tm_mday <= 31)
473 p += sprintf(p, "%02d\n", alrm.time.tm_mday);
475 p += sprintf(p, "**\n");
476 p += sprintf(p, "alrm_wakeup\t: %s\n",
477 alrm.enabled ? "yes" : "no");
478 p += sprintf(p, "alrm_pending\t: %s\n",
479 alrm.pending ? "yes" : "no");
488 int register_rtc(struct rtc_ops *ops)
492 mutex_lock(&rtc_mutex);
493 if (rtc_ops == NULL) {
496 ret = misc_register(&rtc_miscdev);
498 create_proc_read_entry("driver/rtc", 0, NULL,
501 mutex_unlock(&rtc_mutex);
505 EXPORT_SYMBOL(register_rtc);
507 void unregister_rtc(struct rtc_ops *rtc)
509 mutex_lock(&rtc_mutex);
510 if (rtc == rtc_ops) {
511 remove_proc_entry("driver/rtc", NULL);
512 misc_deregister(&rtc_miscdev);
515 mutex_unlock(&rtc_mutex);
517 EXPORT_SYMBOL(unregister_rtc);