Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * DS1286 Real Time Clock interface for Linux |
| 3 | * |
| 4 | * Copyright (C) 1998, 1999, 2000 Ralf Baechle |
| 5 | * |
| 6 | * Based on code written by Paul Gortmaker. |
| 7 | * |
| 8 | * This driver allows use of the real time clock (built into nearly all |
| 9 | * computers) from user space. It exports the /dev/rtc interface supporting |
| 10 | * various ioctl() and also the /proc/rtc pseudo-file for status |
| 11 | * information. |
| 12 | * |
| 13 | * The ioctls can be used to set the interrupt behaviour and generation rate |
| 14 | * from the RTC via IRQ 8. Then the /dev/rtc interface can be used to make |
| 15 | * use of these timer interrupts, be they interval or alarm based. |
| 16 | * |
| 17 | * The /dev/rtc interface will block on reads until an interrupt has been |
| 18 | * received. If a RTC interrupt has already happened, it will output an |
| 19 | * unsigned long and then block. The output value contains the interrupt |
| 20 | * status in the low byte and the number of interrupts since the last read |
| 21 | * in the remaining high bytes. The /dev/rtc interface can also be used with |
| 22 | * the select(2) call. |
| 23 | * |
| 24 | * This program is free software; you can redistribute it and/or modify it |
| 25 | * under the terms of the GNU General Public License as published by the |
| 26 | * Free Software Foundation; either version 2 of the License, or (at your |
| 27 | * option) any later version. |
| 28 | */ |
| 29 | #include <linux/ds1286.h> |
| 30 | #include <linux/types.h> |
| 31 | #include <linux/errno.h> |
| 32 | #include <linux/miscdevice.h> |
| 33 | #include <linux/slab.h> |
| 34 | #include <linux/ioport.h> |
| 35 | #include <linux/fcntl.h> |
| 36 | #include <linux/init.h> |
| 37 | #include <linux/poll.h> |
| 38 | #include <linux/rtc.h> |
| 39 | #include <linux/spinlock.h> |
| 40 | #include <linux/bcd.h> |
| 41 | #include <linux/proc_fs.h> |
Julia Lawall | ac0e4b7 | 2008-04-30 00:53:58 -0700 | [diff] [blame] | 42 | #include <linux/jiffies.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
| 44 | #include <asm/uaccess.h> |
| 45 | #include <asm/system.h> |
| 46 | |
| 47 | #define DS1286_VERSION "1.0" |
| 48 | |
| 49 | /* |
| 50 | * We sponge a minor off of the misc major. No need slurping |
| 51 | * up another valuable major dev number for this. If you add |
| 52 | * an ioctl, make sure you don't conflict with SPARC's RTC |
| 53 | * ioctls. |
| 54 | */ |
| 55 | |
| 56 | static DECLARE_WAIT_QUEUE_HEAD(ds1286_wait); |
| 57 | |
| 58 | static ssize_t ds1286_read(struct file *file, char *buf, |
| 59 | size_t count, loff_t *ppos); |
| 60 | |
| 61 | static int ds1286_ioctl(struct inode *inode, struct file *file, |
| 62 | unsigned int cmd, unsigned long arg); |
| 63 | |
| 64 | static unsigned int ds1286_poll(struct file *file, poll_table *wait); |
| 65 | |
| 66 | static void ds1286_get_alm_time (struct rtc_time *alm_tm); |
| 67 | static void ds1286_get_time(struct rtc_time *rtc_tm); |
| 68 | static int ds1286_set_time(struct rtc_time *rtc_tm); |
| 69 | |
| 70 | static inline unsigned char ds1286_is_updating(void); |
| 71 | |
| 72 | static DEFINE_SPINLOCK(ds1286_lock); |
| 73 | |
| 74 | static int ds1286_read_proc(char *page, char **start, off_t off, |
| 75 | int count, int *eof, void *data); |
| 76 | |
| 77 | /* |
| 78 | * Bits in rtc_status. (7 bits of room for future expansion) |
| 79 | */ |
| 80 | |
| 81 | #define RTC_IS_OPEN 0x01 /* means /dev/rtc is in use */ |
| 82 | #define RTC_TIMER_ON 0x02 /* missed irq timer active */ |
| 83 | |
| 84 | static unsigned char ds1286_status; /* bitmapped status byte. */ |
| 85 | |
| 86 | static unsigned char days_in_mo[] = { |
| 87 | 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 |
| 88 | }; |
| 89 | |
| 90 | /* |
| 91 | * Now all the various file operations that we export. |
| 92 | */ |
| 93 | |
| 94 | static ssize_t ds1286_read(struct file *file, char *buf, |
| 95 | size_t count, loff_t *ppos) |
| 96 | { |
| 97 | return -EIO; |
| 98 | } |
| 99 | |
| 100 | static int ds1286_ioctl(struct inode *inode, struct file *file, |
| 101 | unsigned int cmd, unsigned long arg) |
| 102 | { |
| 103 | struct rtc_time wtime; |
| 104 | |
| 105 | switch (cmd) { |
| 106 | case RTC_AIE_OFF: /* Mask alarm int. enab. bit */ |
| 107 | { |
Alexey Dobriyan | c53421b | 2006-09-30 23:27:37 -0700 | [diff] [blame] | 108 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | unsigned char val; |
| 110 | |
| 111 | if (!capable(CAP_SYS_TIME)) |
| 112 | return -EACCES; |
| 113 | |
| 114 | spin_lock_irqsave(&ds1286_lock, flags); |
| 115 | val = rtc_read(RTC_CMD); |
| 116 | val |= RTC_TDM; |
| 117 | rtc_write(val, RTC_CMD); |
| 118 | spin_unlock_irqrestore(&ds1286_lock, flags); |
| 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | case RTC_AIE_ON: /* Allow alarm interrupts. */ |
| 123 | { |
Alexey Dobriyan | c53421b | 2006-09-30 23:27:37 -0700 | [diff] [blame] | 124 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | unsigned char val; |
| 126 | |
| 127 | if (!capable(CAP_SYS_TIME)) |
| 128 | return -EACCES; |
| 129 | |
| 130 | spin_lock_irqsave(&ds1286_lock, flags); |
| 131 | val = rtc_read(RTC_CMD); |
| 132 | val &= ~RTC_TDM; |
| 133 | rtc_write(val, RTC_CMD); |
| 134 | spin_unlock_irqrestore(&ds1286_lock, flags); |
| 135 | |
| 136 | return 0; |
| 137 | } |
| 138 | case RTC_WIE_OFF: /* Mask watchdog int. enab. bit */ |
| 139 | { |
Alexey Dobriyan | c53421b | 2006-09-30 23:27:37 -0700 | [diff] [blame] | 140 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | unsigned char val; |
| 142 | |
| 143 | if (!capable(CAP_SYS_TIME)) |
| 144 | return -EACCES; |
| 145 | |
| 146 | spin_lock_irqsave(&ds1286_lock, flags); |
| 147 | val = rtc_read(RTC_CMD); |
| 148 | val |= RTC_WAM; |
| 149 | rtc_write(val, RTC_CMD); |
| 150 | spin_unlock_irqrestore(&ds1286_lock, flags); |
| 151 | |
| 152 | return 0; |
| 153 | } |
| 154 | case RTC_WIE_ON: /* Allow watchdog interrupts. */ |
| 155 | { |
Alexey Dobriyan | c53421b | 2006-09-30 23:27:37 -0700 | [diff] [blame] | 156 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | unsigned char val; |
| 158 | |
| 159 | if (!capable(CAP_SYS_TIME)) |
| 160 | return -EACCES; |
| 161 | |
| 162 | spin_lock_irqsave(&ds1286_lock, flags); |
| 163 | val = rtc_read(RTC_CMD); |
| 164 | val &= ~RTC_WAM; |
| 165 | rtc_write(val, RTC_CMD); |
| 166 | spin_unlock_irqrestore(&ds1286_lock, flags); |
| 167 | |
| 168 | return 0; |
| 169 | } |
| 170 | case RTC_ALM_READ: /* Read the present alarm time */ |
| 171 | { |
| 172 | /* |
| 173 | * This returns a struct rtc_time. Reading >= 0xc0 |
| 174 | * means "don't care" or "match all". Only the tm_hour, |
| 175 | * tm_min, and tm_sec values are filled in. |
| 176 | */ |
| 177 | |
| 178 | memset(&wtime, 0, sizeof(wtime)); |
| 179 | ds1286_get_alm_time(&wtime); |
| 180 | break; |
| 181 | } |
| 182 | case RTC_ALM_SET: /* Store a time into the alarm */ |
| 183 | { |
| 184 | /* |
| 185 | * This expects a struct rtc_time. Writing 0xff means |
| 186 | * "don't care" or "match all". Only the tm_hour, |
| 187 | * tm_min and tm_sec are used. |
| 188 | */ |
| 189 | unsigned char hrs, min, sec; |
| 190 | struct rtc_time alm_tm; |
| 191 | |
| 192 | if (!capable(CAP_SYS_TIME)) |
| 193 | return -EACCES; |
| 194 | |
| 195 | if (copy_from_user(&alm_tm, (struct rtc_time*)arg, |
| 196 | sizeof(struct rtc_time))) |
| 197 | return -EFAULT; |
| 198 | |
| 199 | hrs = alm_tm.tm_hour; |
| 200 | min = alm_tm.tm_min; |
Ralf Baechle | b35692e | 2007-03-08 00:14:25 +0000 | [diff] [blame] | 201 | sec = alm_tm.tm_sec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | |
| 203 | if (hrs >= 24) |
| 204 | hrs = 0xff; |
| 205 | |
| 206 | if (min >= 60) |
| 207 | min = 0xff; |
| 208 | |
Ralf Baechle | b35692e | 2007-03-08 00:14:25 +0000 | [diff] [blame] | 209 | if (sec != 0) |
| 210 | return -EINVAL; |
| 211 | |
| 212 | min = BIN2BCD(min); |
| 213 | min = BIN2BCD(hrs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | |
| 215 | spin_lock(&ds1286_lock); |
| 216 | rtc_write(hrs, RTC_HOURS_ALARM); |
| 217 | rtc_write(min, RTC_MINUTES_ALARM); |
| 218 | spin_unlock(&ds1286_lock); |
| 219 | |
| 220 | return 0; |
| 221 | } |
| 222 | case RTC_RD_TIME: /* Read the time/date from RTC */ |
| 223 | { |
| 224 | memset(&wtime, 0, sizeof(wtime)); |
| 225 | ds1286_get_time(&wtime); |
| 226 | break; |
| 227 | } |
| 228 | case RTC_SET_TIME: /* Set the RTC */ |
| 229 | { |
| 230 | struct rtc_time rtc_tm; |
| 231 | |
| 232 | if (!capable(CAP_SYS_TIME)) |
| 233 | return -EACCES; |
| 234 | |
| 235 | if (copy_from_user(&rtc_tm, (struct rtc_time*)arg, |
| 236 | sizeof(struct rtc_time))) |
| 237 | return -EFAULT; |
| 238 | |
| 239 | return ds1286_set_time(&rtc_tm); |
| 240 | } |
| 241 | default: |
| 242 | return -EINVAL; |
| 243 | } |
| 244 | return copy_to_user((void *)arg, &wtime, sizeof wtime) ? -EFAULT : 0; |
| 245 | } |
| 246 | |
| 247 | /* |
| 248 | * We enforce only one user at a time here with the open/close. |
| 249 | * Also clear the previous interrupt data on an open, and clean |
| 250 | * up things on a close. |
| 251 | */ |
| 252 | |
| 253 | static int ds1286_open(struct inode *inode, struct file *file) |
| 254 | { |
| 255 | spin_lock_irq(&ds1286_lock); |
| 256 | |
| 257 | if (ds1286_status & RTC_IS_OPEN) |
| 258 | goto out_busy; |
| 259 | |
| 260 | ds1286_status |= RTC_IS_OPEN; |
| 261 | |
| 262 | spin_unlock_irq(&ds1286_lock); |
| 263 | return 0; |
| 264 | |
| 265 | out_busy: |
| 266 | spin_lock_irq(&ds1286_lock); |
| 267 | return -EBUSY; |
| 268 | } |
| 269 | |
| 270 | static int ds1286_release(struct inode *inode, struct file *file) |
| 271 | { |
| 272 | ds1286_status &= ~RTC_IS_OPEN; |
| 273 | |
| 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | static unsigned int ds1286_poll(struct file *file, poll_table *wait) |
| 278 | { |
| 279 | poll_wait(file, &ds1286_wait, wait); |
| 280 | |
| 281 | return 0; |
| 282 | } |
| 283 | |
| 284 | /* |
| 285 | * The various file operations we support. |
| 286 | */ |
| 287 | |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 288 | static const struct file_operations ds1286_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | .llseek = no_llseek, |
| 290 | .read = ds1286_read, |
| 291 | .poll = ds1286_poll, |
| 292 | .ioctl = ds1286_ioctl, |
| 293 | .open = ds1286_open, |
| 294 | .release = ds1286_release, |
| 295 | }; |
| 296 | |
| 297 | static struct miscdevice ds1286_dev= |
| 298 | { |
| 299 | .minor = RTC_MINOR, |
| 300 | .name = "rtc", |
| 301 | .fops = &ds1286_fops, |
| 302 | }; |
| 303 | |
| 304 | static int __init ds1286_init(void) |
| 305 | { |
| 306 | int err; |
| 307 | |
| 308 | printk(KERN_INFO "DS1286 Real Time Clock Driver v%s\n", DS1286_VERSION); |
| 309 | |
| 310 | err = misc_register(&ds1286_dev); |
| 311 | if (err) |
| 312 | goto out; |
| 313 | |
| 314 | if (!create_proc_read_entry("driver/rtc", 0, 0, ds1286_read_proc, NULL)) { |
| 315 | err = -ENOMEM; |
| 316 | |
| 317 | goto out_deregister; |
| 318 | } |
| 319 | |
| 320 | return 0; |
| 321 | |
| 322 | out_deregister: |
| 323 | misc_deregister(&ds1286_dev); |
| 324 | |
| 325 | out: |
| 326 | return err; |
| 327 | } |
| 328 | |
| 329 | static void __exit ds1286_exit(void) |
| 330 | { |
| 331 | remove_proc_entry("driver/rtc", NULL); |
| 332 | misc_deregister(&ds1286_dev); |
| 333 | } |
| 334 | |
| 335 | static char *days[] = { |
| 336 | "***", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" |
| 337 | }; |
| 338 | |
| 339 | /* |
| 340 | * Info exported via "/proc/rtc". |
| 341 | */ |
| 342 | static int ds1286_proc_output(char *buf) |
| 343 | { |
| 344 | char *p, *s; |
| 345 | struct rtc_time tm; |
| 346 | unsigned char hundredth, month, cmd, amode; |
| 347 | |
| 348 | p = buf; |
| 349 | |
| 350 | ds1286_get_time(&tm); |
| 351 | hundredth = rtc_read(RTC_HUNDREDTH_SECOND); |
| 352 | BCD_TO_BIN(hundredth); |
| 353 | |
| 354 | p += sprintf(p, |
| 355 | "rtc_time\t: %02d:%02d:%02d.%02d\n" |
| 356 | "rtc_date\t: %04d-%02d-%02d\n", |
| 357 | tm.tm_hour, tm.tm_min, tm.tm_sec, hundredth, |
| 358 | tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday); |
| 359 | |
| 360 | /* |
| 361 | * We implicitly assume 24hr mode here. Alarm values >= 0xc0 will |
| 362 | * match any value for that particular field. Values that are |
| 363 | * greater than a valid time, but less than 0xc0 shouldn't appear. |
| 364 | */ |
| 365 | ds1286_get_alm_time(&tm); |
| 366 | p += sprintf(p, "alarm\t\t: %s ", days[tm.tm_wday]); |
| 367 | if (tm.tm_hour <= 24) |
| 368 | p += sprintf(p, "%02d:", tm.tm_hour); |
| 369 | else |
| 370 | p += sprintf(p, "**:"); |
| 371 | |
| 372 | if (tm.tm_min <= 59) |
| 373 | p += sprintf(p, "%02d\n", tm.tm_min); |
| 374 | else |
| 375 | p += sprintf(p, "**\n"); |
| 376 | |
| 377 | month = rtc_read(RTC_MONTH); |
| 378 | p += sprintf(p, |
| 379 | "oscillator\t: %s\n" |
| 380 | "square_wave\t: %s\n", |
| 381 | (month & RTC_EOSC) ? "disabled" : "enabled", |
| 382 | (month & RTC_ESQW) ? "disabled" : "enabled"); |
| 383 | |
| 384 | amode = ((rtc_read(RTC_MINUTES_ALARM) & 0x80) >> 5) | |
| 385 | ((rtc_read(RTC_HOURS_ALARM) & 0x80) >> 6) | |
| 386 | ((rtc_read(RTC_DAY_ALARM) & 0x80) >> 7); |
| 387 | if (amode == 7) s = "each minute"; |
| 388 | else if (amode == 3) s = "minutes match"; |
| 389 | else if (amode == 1) s = "hours and minutes match"; |
| 390 | else if (amode == 0) s = "days, hours and minutes match"; |
| 391 | else s = "invalid"; |
| 392 | p += sprintf(p, "alarm_mode\t: %s\n", s); |
| 393 | |
| 394 | cmd = rtc_read(RTC_CMD); |
| 395 | p += sprintf(p, |
| 396 | "alarm_enable\t: %s\n" |
| 397 | "wdog_alarm\t: %s\n" |
| 398 | "alarm_mask\t: %s\n" |
| 399 | "wdog_alarm_mask\t: %s\n" |
| 400 | "interrupt_mode\t: %s\n" |
| 401 | "INTB_mode\t: %s_active\n" |
| 402 | "interrupt_pins\t: %s\n", |
| 403 | (cmd & RTC_TDF) ? "yes" : "no", |
| 404 | (cmd & RTC_WAF) ? "yes" : "no", |
| 405 | (cmd & RTC_TDM) ? "disabled" : "enabled", |
| 406 | (cmd & RTC_WAM) ? "disabled" : "enabled", |
| 407 | (cmd & RTC_PU_LVL) ? "pulse" : "level", |
| 408 | (cmd & RTC_IBH_LO) ? "low" : "high", |
| 409 | (cmd & RTC_IPSW) ? "unswapped" : "swapped"); |
| 410 | |
| 411 | return p - buf; |
| 412 | } |
| 413 | |
| 414 | static int ds1286_read_proc(char *page, char **start, off_t off, |
| 415 | int count, int *eof, void *data) |
| 416 | { |
| 417 | int len = ds1286_proc_output (page); |
| 418 | if (len <= off+count) *eof = 1; |
| 419 | *start = page + off; |
| 420 | len -= off; |
| 421 | if (len>count) |
| 422 | len = count; |
| 423 | if (len<0) |
| 424 | len = 0; |
| 425 | |
| 426 | return len; |
| 427 | } |
| 428 | |
| 429 | /* |
| 430 | * Returns true if a clock update is in progress |
| 431 | */ |
| 432 | static inline unsigned char ds1286_is_updating(void) |
| 433 | { |
| 434 | return rtc_read(RTC_CMD) & RTC_TE; |
| 435 | } |
| 436 | |
| 437 | |
| 438 | static void ds1286_get_time(struct rtc_time *rtc_tm) |
| 439 | { |
| 440 | unsigned char save_control; |
Alexey Dobriyan | c53421b | 2006-09-30 23:27:37 -0700 | [diff] [blame] | 441 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | unsigned long uip_watchdog = jiffies; |
| 443 | |
| 444 | /* |
| 445 | * read RTC once any update in progress is done. The update |
| 446 | * can take just over 2ms. We wait 10 to 20ms. There is no need to |
| 447 | * to poll-wait (up to 1s - eeccch) for the falling edge of RTC_UIP. |
| 448 | * If you need to know *exactly* when a second has started, enable |
| 449 | * periodic update complete interrupts, (via ioctl) and then |
| 450 | * immediately read /dev/rtc which will block until you get the IRQ. |
| 451 | * Once the read clears, read the RTC time (again via ioctl). Easy. |
| 452 | */ |
| 453 | |
| 454 | if (ds1286_is_updating() != 0) |
Julia Lawall | ac0e4b7 | 2008-04-30 00:53:58 -0700 | [diff] [blame] | 455 | while (time_before(jiffies, uip_watchdog + 2*HZ/100)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | barrier(); |
| 457 | |
| 458 | /* |
| 459 | * Only the values that we read from the RTC are set. We leave |
| 460 | * tm_wday, tm_yday and tm_isdst untouched. Even though the |
| 461 | * RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated |
| 462 | * by the RTC when initially set to a non-zero value. |
| 463 | */ |
| 464 | spin_lock_irqsave(&ds1286_lock, flags); |
| 465 | save_control = rtc_read(RTC_CMD); |
| 466 | rtc_write((save_control|RTC_TE), RTC_CMD); |
| 467 | |
| 468 | rtc_tm->tm_sec = rtc_read(RTC_SECONDS); |
| 469 | rtc_tm->tm_min = rtc_read(RTC_MINUTES); |
| 470 | rtc_tm->tm_hour = rtc_read(RTC_HOURS) & 0x3f; |
| 471 | rtc_tm->tm_mday = rtc_read(RTC_DATE); |
| 472 | rtc_tm->tm_mon = rtc_read(RTC_MONTH) & 0x1f; |
| 473 | rtc_tm->tm_year = rtc_read(RTC_YEAR); |
| 474 | |
| 475 | rtc_write(save_control, RTC_CMD); |
| 476 | spin_unlock_irqrestore(&ds1286_lock, flags); |
| 477 | |
| 478 | BCD_TO_BIN(rtc_tm->tm_sec); |
| 479 | BCD_TO_BIN(rtc_tm->tm_min); |
| 480 | BCD_TO_BIN(rtc_tm->tm_hour); |
| 481 | BCD_TO_BIN(rtc_tm->tm_mday); |
| 482 | BCD_TO_BIN(rtc_tm->tm_mon); |
| 483 | BCD_TO_BIN(rtc_tm->tm_year); |
| 484 | |
| 485 | /* |
| 486 | * Account for differences between how the RTC uses the values |
| 487 | * and how they are defined in a struct rtc_time; |
| 488 | */ |
| 489 | if (rtc_tm->tm_year < 45) |
| 490 | rtc_tm->tm_year += 30; |
| 491 | if ((rtc_tm->tm_year += 40) < 70) |
| 492 | rtc_tm->tm_year += 100; |
| 493 | |
| 494 | rtc_tm->tm_mon--; |
| 495 | } |
| 496 | |
| 497 | static int ds1286_set_time(struct rtc_time *rtc_tm) |
| 498 | { |
| 499 | unsigned char mon, day, hrs, min, sec, leap_yr; |
| 500 | unsigned char save_control; |
Alexey Dobriyan | c53421b | 2006-09-30 23:27:37 -0700 | [diff] [blame] | 501 | unsigned int yrs; |
| 502 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | |
| 504 | |
| 505 | yrs = rtc_tm->tm_year + 1900; |
| 506 | mon = rtc_tm->tm_mon + 1; /* tm_mon starts at zero */ |
| 507 | day = rtc_tm->tm_mday; |
| 508 | hrs = rtc_tm->tm_hour; |
| 509 | min = rtc_tm->tm_min; |
| 510 | sec = rtc_tm->tm_sec; |
| 511 | |
| 512 | if (yrs < 1970) |
| 513 | return -EINVAL; |
| 514 | |
| 515 | leap_yr = ((!(yrs % 4) && (yrs % 100)) || !(yrs % 400)); |
| 516 | |
| 517 | if ((mon > 12) || (day == 0)) |
| 518 | return -EINVAL; |
| 519 | |
| 520 | if (day > (days_in_mo[mon] + ((mon == 2) && leap_yr))) |
| 521 | return -EINVAL; |
| 522 | |
| 523 | if ((hrs >= 24) || (min >= 60) || (sec >= 60)) |
| 524 | return -EINVAL; |
| 525 | |
| 526 | if ((yrs -= 1940) > 255) /* They are unsigned */ |
| 527 | return -EINVAL; |
| 528 | |
| 529 | if (yrs >= 100) |
| 530 | yrs -= 100; |
| 531 | |
| 532 | BIN_TO_BCD(sec); |
| 533 | BIN_TO_BCD(min); |
| 534 | BIN_TO_BCD(hrs); |
| 535 | BIN_TO_BCD(day); |
| 536 | BIN_TO_BCD(mon); |
| 537 | BIN_TO_BCD(yrs); |
| 538 | |
| 539 | spin_lock_irqsave(&ds1286_lock, flags); |
| 540 | save_control = rtc_read(RTC_CMD); |
| 541 | rtc_write((save_control|RTC_TE), RTC_CMD); |
| 542 | |
| 543 | rtc_write(yrs, RTC_YEAR); |
| 544 | rtc_write(mon, RTC_MONTH); |
| 545 | rtc_write(day, RTC_DATE); |
| 546 | rtc_write(hrs, RTC_HOURS); |
| 547 | rtc_write(min, RTC_MINUTES); |
| 548 | rtc_write(sec, RTC_SECONDS); |
| 549 | rtc_write(0, RTC_HUNDREDTH_SECOND); |
| 550 | |
| 551 | rtc_write(save_control, RTC_CMD); |
| 552 | spin_unlock_irqrestore(&ds1286_lock, flags); |
| 553 | |
| 554 | return 0; |
| 555 | } |
| 556 | |
| 557 | static void ds1286_get_alm_time(struct rtc_time *alm_tm) |
| 558 | { |
| 559 | unsigned char cmd; |
Alexey Dobriyan | c53421b | 2006-09-30 23:27:37 -0700 | [diff] [blame] | 560 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | |
| 562 | /* |
| 563 | * Only the values that we read from the RTC are set. That |
| 564 | * means only tm_wday, tm_hour, tm_min. |
| 565 | */ |
| 566 | spin_lock_irqsave(&ds1286_lock, flags); |
| 567 | alm_tm->tm_min = rtc_read(RTC_MINUTES_ALARM) & 0x7f; |
| 568 | alm_tm->tm_hour = rtc_read(RTC_HOURS_ALARM) & 0x1f; |
| 569 | alm_tm->tm_wday = rtc_read(RTC_DAY_ALARM) & 0x07; |
| 570 | cmd = rtc_read(RTC_CMD); |
| 571 | spin_unlock_irqrestore(&ds1286_lock, flags); |
| 572 | |
| 573 | BCD_TO_BIN(alm_tm->tm_min); |
| 574 | BCD_TO_BIN(alm_tm->tm_hour); |
| 575 | alm_tm->tm_sec = 0; |
| 576 | } |
| 577 | |
| 578 | module_init(ds1286_init); |
| 579 | module_exit(ds1286_exit); |
| 580 | |
| 581 | MODULE_AUTHOR("Ralf Baechle"); |
| 582 | MODULE_LICENSE("GPL"); |
| 583 | MODULE_ALIAS_MISCDEV(RTC_MINOR); |