]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/char/omap-rtc.c
Merge with /home/tmlind/src/kernel/linux-2.6
[linux-2.6-omap-h63xx.git] / drivers / char / omap-rtc.c
1 /*
2  *      TI OMAP Real Time Clock interface for Linux     
3  *
4  *      Copyright (C) 2003 MontaVista Software, Inc.
5  *      Author: George G. Davis <gdavis@mvista.com> or <source@mvista.com>
6  *
7  *      Initially based on linux-2.4.20/drivers/char/rtc.c
8  *      Copyright (C) 1996 Paul Gortmaker
9  *
10  *      This driver allows use of the real time clock (built into
11  *      nearly all computers) from user space. It exports the /dev/rtc
12  *      interface supporting various ioctl() and also the
13  *      /proc/driver/rtc pseudo-file for status information.
14  *
15  *      The ioctls can be used to set the interrupt behaviour from the
16  *      RTC via IRQs. Then the /dev/rtc interface can be used to make
17  *      use of RTC interrupts, be they time update or alarm based.
18  *
19  *      The /dev/rtc interface will block on reads until an interrupt
20  *      has been received. If a RTC interrupt has already happened,
21  *      it will output an unsigned long and then block. The output value
22  *      contains the interrupt status in the low byte and the number of
23  *      interrupts since the last read in the remaining high bytes. The 
24  *      /dev/rtc interface can also be used with the select(2) call.
25  *
26  *      This program is free software; you can redistribute it and/or
27  *      modify it under the terms of the GNU General Public License
28  *      as published by the Free Software Foundation; either version
29  *      2 of the License, or (at your option) any later version.
30  *
31  *      Based on other minimal char device drivers, like Alan's
32  *      watchdog, Ted's random, etc. etc.
33  *
34  * Change Log :
35  *      v1.0    <gdavis@mvista.com> Initial version based on rtc.c v1.10e
36  *              <ramakrishnan@india.ti.com> Added support for 2.6 kernel, 
37  *                  - changed the return value of the interrupt handler
38  */
39
40 /*
41  *      Note that *all* calls to CMOS_READ and CMOS_WRITE are done with
42  *      interrupts disabled.
43  *      REVISIT: Elaborate on OMAP1510 TRM 15uS BUSY access rule.
44  */
45
46 #include <linux/config.h>
47 #include <linux/module.h>
48 #include <linux/kernel.h>
49 #include <linux/types.h>
50 #include <linux/miscdevice.h>
51 #include <linux/ioport.h>
52 #include <linux/fcntl.h>
53 #include <linux/init.h>
54 #include <linux/poll.h>
55 #include <linux/proc_fs.h>
56 #include <linux/spinlock.h>
57 #include <linux/platform_device.h>
58 #include <linux/interrupt.h>
59 #include <linux/rtc.h>
60 #include <linux/bcd.h>
61
62 #include <asm/io.h>
63 #include <asm/uaccess.h>
64 #include <asm/system.h>
65 #include <asm/hardware.h>
66 #include <asm/irq.h>
67 #include <asm/rtc.h>
68 #include <asm/mach/time.h>
69
70 #include "omap-rtc.h"
71
72 extern spinlock_t rtc_lock;
73
74
75 /* OMAP RTC register access macros: */
76
77 #define CMOS_READ(addr)         omap_readb(addr)
78 #define CMOS_WRITE(val, addr)   omap_writeb(val, addr)
79
80 static struct fasync_struct *rtc_async_queue;
81
82 static DECLARE_WAIT_QUEUE_HEAD(rtc_wait);
83
84 static void get_rtc_time (struct rtc_time *rtc_tm);
85 static void get_rtc_alm_time (struct rtc_time *alm_tm);
86
87 static void set_rtc_irq_bit(unsigned char bit);
88 static void mask_rtc_irq_bit(unsigned char bit);
89
90 static int rtc_read_proc(char *page, char **start, off_t off,
91                          int count, int *eof, void *data);
92
93 /*
94  *      Bits in rtc_status. (7 bits of room for future expansion)
95  */
96
97 #define RTC_IS_OPEN             0x01    /* means /dev/rtc is in use     */
98
99 /*
100  * REVISIT: fix this comment:
101  * rtc_status is never changed by rtc_interrupt, and ioctl/open/close is
102  * protected by the big kernel lock.
103  */
104 static unsigned long rtc_status = 0;    /* bitmapped status byte.       */
105 static unsigned long rtc_irq_data = 0;  /* our output to the world      */
106
107 /*
108  *      If this driver ever becomes modularised, it will be really nice
109  *      to make the epoch retain its value across module reload...
110  */
111
112 static unsigned long epoch = 1900;      /* year corresponding to 0x00   */
113
114 static const unsigned char days_in_mo[] = 
115 {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
116
117 /*
118  *      A very tiny interrupt handler. It runs with SA_INTERRUPT set.
119  */
120
121 static irqreturn_t rtc_interrupt(int irq, void *dev_id, struct pt_regs *regs)
122 {
123         /*
124          *      Either an alarm interrupt or update complete interrupt.
125          *      We store the status in the low byte and the number of
126          *      interrupts received since the last read in the remainder
127          *      of rtc_irq_data.
128          */
129
130         spin_lock (&rtc_lock);
131
132         rtc_irq_data += 0x100;
133         rtc_irq_data &= ~0xff;
134         rtc_irq_data |= CMOS_READ(OMAP_RTC_STATUS_REG);
135
136         if (rtc_irq_data & OMAP_RTC_STATUS_ALARM)
137                 CMOS_WRITE(OMAP_RTC_STATUS_ALARM, OMAP_RTC_STATUS_REG);
138
139         spin_unlock (&rtc_lock);
140
141         /* Now do the rest of the actions */
142         wake_up_interruptible(&rtc_wait);       
143
144         kill_fasync (&rtc_async_queue, SIGIO, POLL_IN);
145         return IRQ_HANDLED;
146 }
147
148 /*
149  *      Now all the various file operations that we export.
150  */
151
152 static ssize_t rtc_read(struct file *file, char __user *buf,
153                         size_t count, loff_t *ppos)
154 {
155         DECLARE_WAITQUEUE(wait, current);
156         unsigned long data;
157         ssize_t retval;
158         
159         if (count < sizeof(unsigned long))
160                 return -EINVAL;
161
162         add_wait_queue(&rtc_wait, &wait);
163         set_current_state(TASK_INTERRUPTIBLE);
164
165         for (;;) {
166                 spin_lock_irq (&rtc_lock);
167                 data = rtc_irq_data;
168                 if (data != 0) {
169                         rtc_irq_data = 0;
170                         break;
171                 }
172                 spin_unlock_irq (&rtc_lock);
173
174                 if (file->f_flags & O_NONBLOCK) {
175                         retval = -EAGAIN;
176                         goto out;
177                 }
178                 if (signal_pending(current)) {
179                         retval = -ERESTARTSYS;
180                         goto out;
181                 }
182                 schedule();
183         }
184
185         spin_unlock_irq (&rtc_lock);
186         retval = put_user(data, (unsigned long __user *)buf);
187         if (!retval)
188                 retval = sizeof(unsigned long); 
189  out:
190         set_current_state(TASK_RUNNING);
191         remove_wait_queue(&rtc_wait, &wait);
192
193         return retval;
194 }
195
196 /* convert from userspace struct to hardware BCD-encoded version,
197  * or return error code
198  */
199 static int utm2bcd(struct rtc_time __user *arg, struct rtc_time *tm)
200 {
201         unsigned char leap_yr;
202
203         if (copy_from_user(tm, arg, sizeof(struct rtc_time)))
204                 return -EFAULT;
205
206         tm->tm_year += 1900;
207         tm->tm_mon++;
208
209         if (tm->tm_year < 1970)
210                 return -EINVAL;
211
212         leap_yr = (!(tm->tm_year % 4) && (tm->tm_year % 100))
213                         || !(tm->tm_year % 400);
214
215         if ((tm->tm_mon > 12) || (tm->tm_mday == 0))
216                 return -EINVAL;
217
218         if (tm->tm_mday > (days_in_mo[tm->tm_mon] + ((tm->tm_mon == 2) && leap_yr)))
219                 return -EINVAL;
220
221         if ((tm->tm_hour >= 24) || (tm->tm_min >= 60) || (tm->tm_sec >= 60))
222                 return -EINVAL;
223
224         if ((tm->tm_year -= epoch) > 255)    /* They are unsigned */
225                 return -EINVAL;
226
227         if (tm->tm_year > 169)
228                 return -EINVAL;
229
230         if (tm->tm_year >= 100)
231                 tm->tm_year -= 100;
232
233         BIN_TO_BCD(tm->tm_sec);
234         BIN_TO_BCD(tm->tm_min);
235         BIN_TO_BCD(tm->tm_hour);
236         BIN_TO_BCD(tm->tm_mday);
237         BIN_TO_BCD(tm->tm_mon);
238         BIN_TO_BCD(tm->tm_year);
239
240         return 0;
241 }
242
243 static int rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
244                      unsigned long arg)
245 {
246         struct rtc_time wtime; 
247         int status = 0;
248         u8 save_control;
249
250         switch (cmd) {
251         case RTC_AIE_OFF:       /* Mask alarm int. enab. bit    */
252                 mask_rtc_irq_bit(OMAP_RTC_INTERRUPTS_IT_ALARM);
253                 break;
254         case RTC_AIE_ON:        /* Allow alarm interrupts.      */
255                 set_rtc_irq_bit(OMAP_RTC_INTERRUPTS_IT_ALARM);
256                 break;
257         case RTC_UIE_OFF:       /* Mask ints from RTC updates.  */
258                 mask_rtc_irq_bit(OMAP_RTC_INTERRUPTS_IT_TIMER);
259                 break;
260         case RTC_UIE_ON:        /* Allow ints for RTC updates.  */
261                 set_rtc_irq_bit(OMAP_RTC_INTERRUPTS_IT_TIMER);
262                 break;
263         case RTC_ALM_READ:      /* Read the present alarm time */
264                 /*
265                  * This returns a struct rtc_time. Reading >= 0xc0
266                  * means "don't care" or "match all". Only the tm_hour,
267                  * tm_min, and tm_sec values are filled in.
268                  */
269                 memset(&wtime, 0, sizeof(struct rtc_time));
270                 get_rtc_alm_time(&wtime);
271                 goto return_wtime;
272         case RTC_ALM_SET:       /* Store a time into the alarm */
273                 status = utm2bcd((void __user *)arg, &wtime);
274                 if (status != 0)
275                         return status;
276
277                 spin_lock_irq(&rtc_lock);
278                 CMOS_WRITE(wtime.tm_year, OMAP_RTC_ALARM_YEARS_REG);
279                 CMOS_WRITE(wtime.tm_mon, OMAP_RTC_ALARM_MONTHS_REG);
280                 CMOS_WRITE(wtime.tm_mday, OMAP_RTC_ALARM_DAYS_REG);
281                 CMOS_WRITE(wtime.tm_hour, OMAP_RTC_ALARM_HOURS_REG);
282                 CMOS_WRITE(wtime.tm_min, OMAP_RTC_ALARM_MINUTES_REG);
283                 CMOS_WRITE(wtime.tm_sec, OMAP_RTC_ALARM_SECONDS_REG);
284                 spin_unlock_irq(&rtc_lock);
285
286                 break;
287         case RTC_RD_TIME:       /* Read the time/date from RTC  */
288                 memset(&wtime, 0, sizeof(struct rtc_time));
289                 get_rtc_time(&wtime);
290                 goto return_wtime;
291         case RTC_SET_TIME:      /* Set the RTC */
292                 if (!capable(CAP_SYS_TIME))
293                         return -EACCES;
294
295                 status = utm2bcd((void __user *)arg, &wtime);
296                 if (status != 0)
297                         return status;
298
299                 spin_lock_irq(&rtc_lock);
300                 save_control = CMOS_READ(OMAP_RTC_CTRL_REG);
301                 CMOS_WRITE((save_control & ~OMAP_RTC_CTRL_STOP),
302                            OMAP_RTC_CTRL_REG);
303                 CMOS_WRITE(wtime.tm_year, OMAP_RTC_YEARS_REG);
304                 CMOS_WRITE(wtime.tm_mon, OMAP_RTC_MONTHS_REG);
305                 CMOS_WRITE(wtime.tm_mday, OMAP_RTC_DAYS_REG);
306                 CMOS_WRITE(wtime.tm_hour, OMAP_RTC_HOURS_REG);
307                 CMOS_WRITE(wtime.tm_min, OMAP_RTC_MINUTES_REG);
308                 CMOS_WRITE(wtime.tm_sec, OMAP_RTC_SECONDS_REG);
309                 CMOS_WRITE((save_control | OMAP_RTC_CTRL_STOP),
310                            OMAP_RTC_CTRL_REG);
311                 spin_unlock_irq(&rtc_lock);
312
313                 break;
314         case RTC_EPOCH_READ:    /* Read the epoch.      */
315                 status = put_user (epoch, (unsigned long  __user *)arg);
316                 break;
317         case RTC_EPOCH_SET:     /* Set the epoch.       */
318                 if (!capable(CAP_SYS_TIME))
319                         return -EACCES;
320
321                 /* 
322                  * There were no RTC clocks before 1900.
323                  */
324                 if (arg < 1900)
325                         status = -EINVAL;
326                 else
327                         epoch = arg;
328                 break;
329         default:
330                 status = -ENOTTY;
331         }
332         return status;
333
334 return_wtime:
335         return copy_to_user((void  __user *)arg, &wtime, sizeof wtime)
336                 ? -EFAULT
337                 : 0;
338 }
339
340 /*
341  *      We enforce only one user at a time here with the open/close.
342  *      Also clear the previous interrupt data on an open, and clean
343  *      up things on a close.
344  */
345
346 /* We use rtc_lock to protect against concurrent opens. So the BKL is not
347  * needed here. Or anywhere else in this driver. */
348 static int rtc_open(struct inode *inode, struct file *file)
349 {
350         spin_lock_irq (&rtc_lock);
351
352         if (rtc_status & RTC_IS_OPEN)
353                 goto out_busy;
354
355         rtc_status |= RTC_IS_OPEN;
356
357         rtc_irq_data = 0;
358         spin_unlock_irq (&rtc_lock);
359         return 0;
360
361 out_busy:
362         spin_unlock_irq (&rtc_lock);
363         return -EBUSY;
364 }
365
366 static int rtc_fasync(int fd, struct file *filp, int on)
367 {
368         return fasync_helper (fd, filp, on, &rtc_async_queue);
369 }
370
371 static int rtc_release(struct inode *inode, struct file *file)
372 {
373         unsigned char tmp;
374
375         /*
376          * Turn off all interrupts once the device is no longer
377          * in use, and clear the data.
378          */
379
380         spin_lock_irq(&rtc_lock);
381         tmp = CMOS_READ(OMAP_RTC_INTERRUPTS_REG);
382         tmp &=  ~OMAP_RTC_INTERRUPTS_IT_ALARM;
383         tmp &=  ~OMAP_RTC_INTERRUPTS_IT_TIMER;
384         CMOS_WRITE(tmp, OMAP_RTC_INTERRUPTS_REG);
385         spin_unlock_irq(&rtc_lock);
386
387         if (file->f_flags & FASYNC) {
388                 rtc_fasync (-1, file, 0);
389         }
390
391         spin_lock_irq (&rtc_lock);
392         rtc_irq_data = 0;
393         spin_unlock_irq (&rtc_lock);
394
395         /* No need for locking -- nobody else can do anything until this rmw
396          * is committed, and we don't implement timer support in omap-rtc.
397          */
398         rtc_status &= ~RTC_IS_OPEN;
399         return 0;
400 }
401
402 /* Called without the kernel lock - fine */
403 static unsigned int rtc_poll(struct file *file, poll_table *wait)
404 {
405         unsigned long l;
406
407         poll_wait(file, &rtc_wait, wait);
408
409         spin_lock_irq (&rtc_lock);
410         l = rtc_irq_data;
411         spin_unlock_irq (&rtc_lock);
412
413         if (l != 0)
414                 return POLLIN | POLLRDNORM;
415         return 0;
416 }
417
418 /*
419  *      The various file operations we support.
420  */
421
422 static struct file_operations rtc_fops = {
423         .owner          = THIS_MODULE,
424         .llseek         = no_llseek,
425         .read           = rtc_read,
426         .poll           = rtc_poll,
427         .ioctl          = rtc_ioctl,
428         .open           = rtc_open,
429         .release        = rtc_release,
430         .fasync         = rtc_fasync,
431 };
432
433 static struct miscdevice rtc_dev = {
434         .minor          = RTC_MINOR,
435         .name           = "rtc",
436         .fops           = &rtc_fops,
437 };
438
439 static int __init omap_rtc_probe(struct device *dev)
440 {
441         struct platform_device  *pdev = to_platform_device(dev);
442         struct resource         *res, *mem;
443
444         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
445         if (res)
446                 mem = request_mem_region(res->start,
447                                 res->end - res->start + 1,
448                                 pdev->name);
449         else
450                 mem = NULL;
451         if (!mem) {
452                 pr_debug("%s: RTC registers at %x are not free.\n",
453                         pdev->name, OMAP_RTC_BASE);
454                 return -EBUSY;
455         }
456         dev_set_drvdata(dev, mem);
457
458         if (CMOS_READ(OMAP_RTC_STATUS_REG) & OMAP_RTC_STATUS_POWER_UP) {
459                 pr_info("%s: RTC power up reset detected.\n",
460                         pdev->name);
461                 /* Clear OMAP_RTC_STATUS_POWER_UP */
462                 CMOS_WRITE(OMAP_RTC_STATUS_POWER_UP, OMAP_RTC_STATUS_REG);
463         }
464
465         if (CMOS_READ(OMAP_RTC_STATUS_REG) & OMAP_RTC_STATUS_ALARM) {
466                 pr_debug("%s: Clearing RTC ALARM interrupt.\n",
467                         pdev->name);
468                 /* Clear OMAP_RTC_STATUS_ALARM */
469                 CMOS_WRITE(OMAP_RTC_STATUS_ALARM, OMAP_RTC_STATUS_REG);
470         }
471
472         if (request_irq(INT_RTC_TIMER, rtc_interrupt, SA_INTERRUPT,
473                         pdev->name, NULL)) {
474                 pr_debug("%s: RTC timer interrupt IRQ%d is not free.\n",
475                         pdev->name, INT_RTC_TIMER);
476                 goto fail;
477         }
478
479         if (request_irq(INT_RTC_ALARM, rtc_interrupt, SA_INTERRUPT,
480                         pdev->name, NULL)) {
481                 pr_debug("%s: RTC alarm interrupt IRQ%d is not free.\n",
482                         pdev->name, INT_RTC_ALARM);
483                 free_irq(INT_RTC_TIMER, NULL);
484                 goto fail;
485         }
486
487         /* On boards with split power, RTC_ON_NOFF resets all but the RTC */
488         if (!(CMOS_READ(OMAP_RTC_CTRL_REG) & OMAP_RTC_CTRL_STOP)) {
489                 pr_info("%s: Enabling RTC.\n", pdev->name);
490                 CMOS_WRITE(OMAP_RTC_CTRL_STOP, OMAP_RTC_CTRL_REG);
491         } else
492                 pr_info("%s: RTC already running.\n", pdev->name);
493
494         spin_lock_init(&rtc_lock);
495         misc_register(&rtc_dev);
496         create_proc_read_entry("driver/rtc", 0, NULL, rtc_read_proc, NULL);
497
498         return 0;
499
500 fail:
501         release_resource(mem);
502         return -EIO;
503 }
504
505 static int __exit omap_rtc_remove(struct device *dev)
506 {
507         free_irq (INT_RTC_TIMER, NULL);
508         free_irq (INT_RTC_ALARM, NULL);
509
510         remove_proc_entry ("driver/rtc", NULL);
511         misc_deregister(&rtc_dev);
512
513         release_resource(dev_get_drvdata(dev));
514         return 0;
515 }
516
517 /*
518  *      Info exported via "/proc/driver/rtc".
519  */
520
521 static int rtc_proc_output (char *buf)
522 {
523 #define YN(value) ((value) ? "yes" : "no")
524         char *p;
525         struct rtc_time tm;
526
527         p = buf;
528
529         get_rtc_time(&tm);
530
531         /*
532          * There is no way to tell if the luser has the RTC set for local
533          * time or for Universal Standard Time (GMT). Probably local though.
534          */
535         p += sprintf(p,
536                      "rtc_time\t: %02d:%02d:%02d\n"
537                      "rtc_date\t: %04d-%02d-%02d\n"
538                      "rtc_epoch\t: %04lu\n",
539                      tm.tm_hour, tm.tm_min, tm.tm_sec,
540                      tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, epoch);
541
542         get_rtc_alm_time(&tm);
543
544         /*
545          * We implicitly assume 24hr mode here. Alarm values >= 0xc0 will
546          * match any value for that particular field. Values that are
547          * greater than a valid time, but less than 0xc0 shouldn't appear.
548          */
549         p += sprintf(p,
550                      "alarm_time\t: %02d:%02d:%02d\n"
551                      "alarm_date\t: %04d-%02d-%02d\n",
552                      tm.tm_hour, tm.tm_min, tm.tm_sec,
553                      tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
554
555         p += sprintf(p,
556                      "BCD\t\t: %s\n"
557                      "24hr\t\t: %s\n"
558                      "alarm_IRQ\t: %s\n"
559                      "update_IRQ\t: %s\n"
560                      "update_rate\t: %ud\n",
561                      YN(1),
562                      YN(1),
563                      YN(CMOS_READ(OMAP_RTC_INTERRUPTS_REG) &
564                         OMAP_RTC_INTERRUPTS_IT_ALARM),
565                      YN(CMOS_READ(OMAP_RTC_INTERRUPTS_REG) &
566                         OMAP_RTC_INTERRUPTS_IT_TIMER),
567                      CMOS_READ(OMAP_RTC_INTERRUPTS_REG) & 3 /* REVISIT */);
568
569         return  p - buf;
570 #undef YN
571 }
572
573 static int rtc_read_proc(char *page, char **start, off_t off,
574                          int count, int *eof, void *data)
575 {
576         int len = rtc_proc_output (page);
577
578         if (len <= off+count)
579                 *eof = 1;
580         *start = page + off;
581         len -= off;
582         if (len > count)
583                 len = count;
584         if (len < 0)
585                 len = 0;
586         return len;
587 }
588
589 /*
590  * Returns true if a clock update is in progress
591  */
592 static inline unsigned char rtc_is_updating(void)
593 {
594         unsigned char uip;
595
596         spin_lock_irq(&rtc_lock);
597         uip = (CMOS_READ(OMAP_RTC_STATUS_REG) & OMAP_RTC_STATUS_BUSY);
598         spin_unlock_irq(&rtc_lock);
599         return uip;
600 }
601
602 static void bcd2tm(struct rtc_time *tm)
603 {
604         BCD_TO_BIN(tm->tm_sec);
605         BCD_TO_BIN(tm->tm_min);
606         BCD_TO_BIN(tm->tm_hour);
607         BCD_TO_BIN(tm->tm_mday);
608         BCD_TO_BIN(tm->tm_mon);
609         BCD_TO_BIN(tm->tm_year);
610
611         /*
612          * Account for differences between how the RTC uses the values
613          * and how they are defined in a struct rtc_time;
614          */
615         if ((tm->tm_year += (epoch - 1900)) <= 69)
616                 tm->tm_year += 100;
617
618         tm->tm_mon--;
619 }
620
621
622 static void get_rtc_time(struct rtc_time *rtc_tm)
623 {
624         unsigned char ctrl;
625
626         /* REVISIT: Fix this comment!!!
627          * read RTC once any update in progress is done. The update
628          * can take just over 2ms. We wait 10 to 20ms. There is no need to
629          * to poll-wait (up to 1s - eeccch) for the falling edge of OMAP_RTC_STATUS_BUSY.
630          * If you need to know *exactly* when a second has started, enable
631          * periodic update complete interrupts, (via ioctl) and then 
632          * immediately read /dev/rtc which will block until you get the IRQ.
633          * Once the read clears, read the RTC time (again via ioctl). Easy.
634          */
635
636 #if     0 /* REVISIT: This need to do as the TRM says. */
637         unsigned long uip_watchdog = jiffies;
638         if (rtc_is_updating() != 0)
639                 while (jiffies - uip_watchdog < 2*HZ/100) {
640                         barrier();
641                         cpu_relax();
642                 }
643 #endif
644
645         /*
646          * Only the values that we read from the RTC are set. We leave
647          * tm_wday, tm_yday and tm_isdst untouched. Even though the
648          * RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated
649          * by the RTC when initially set to a non-zero value.
650          */
651         spin_lock_irq(&rtc_lock);
652         rtc_tm->tm_sec = CMOS_READ(OMAP_RTC_SECONDS_REG);
653         rtc_tm->tm_min = CMOS_READ(OMAP_RTC_MINUTES_REG);
654         rtc_tm->tm_hour = CMOS_READ(OMAP_RTC_HOURS_REG);
655         rtc_tm->tm_mday = CMOS_READ(OMAP_RTC_DAYS_REG);
656         rtc_tm->tm_mon = CMOS_READ(OMAP_RTC_MONTHS_REG);
657         rtc_tm->tm_year = CMOS_READ(OMAP_RTC_YEARS_REG);
658         ctrl = CMOS_READ(OMAP_RTC_CTRL_REG);
659         spin_unlock_irq(&rtc_lock);
660
661         bcd2tm(rtc_tm);
662 }
663
664 static void get_rtc_alm_time(struct rtc_time *alm_tm)
665 {
666         unsigned char ctrl;
667
668         spin_lock_irq(&rtc_lock);
669         alm_tm->tm_sec = CMOS_READ(OMAP_RTC_ALARM_SECONDS_REG);
670         alm_tm->tm_min = CMOS_READ(OMAP_RTC_ALARM_MINUTES_REG);
671         alm_tm->tm_hour = CMOS_READ(OMAP_RTC_ALARM_HOURS_REG);
672         alm_tm->tm_mday = CMOS_READ(OMAP_RTC_ALARM_DAYS_REG);
673         alm_tm->tm_mon = CMOS_READ(OMAP_RTC_ALARM_MONTHS_REG);
674         alm_tm->tm_year = CMOS_READ(OMAP_RTC_ALARM_YEARS_REG);
675         ctrl = CMOS_READ(OMAP_RTC_CTRL_REG);
676         spin_unlock_irq(&rtc_lock);
677
678         bcd2tm(alm_tm);
679 }
680
681 /*
682  * Used to disable/enable UIE and AIE interrupts.
683  */
684
685 static void mask_rtc_irq_bit(unsigned char bit)
686 {
687         unsigned char val;
688
689         spin_lock_irq(&rtc_lock);
690         val = CMOS_READ(OMAP_RTC_INTERRUPTS_REG);
691         val &=  ~bit;
692         CMOS_WRITE(val, OMAP_RTC_INTERRUPTS_REG);
693         rtc_irq_data = 0;
694         spin_unlock_irq(&rtc_lock);
695 }
696
697 static void set_rtc_irq_bit(unsigned char bit)
698 {
699         unsigned char val;
700
701         spin_lock_irq(&rtc_lock);
702         val = CMOS_READ(OMAP_RTC_INTERRUPTS_REG);
703         val |= bit;
704         CMOS_WRITE(val, OMAP_RTC_INTERRUPTS_REG);
705         rtc_irq_data = 0;
706         spin_unlock_irq(&rtc_lock);
707 }
708
709 #ifdef CONFIG_PM
710 static struct timespec rtc_delta;
711
712 static int rtc_suspend(struct device *dev, pm_message_t state)
713 {
714         struct rtc_time rtc_tm;
715         struct timespec time;
716
717         time.tv_nsec = 0;
718         get_rtc_time(&rtc_tm);
719         rtc_tm_to_time(&rtc_tm, &time.tv_sec);
720
721         save_time_delta(&rtc_delta, &time);
722
723         return 0;
724 }
725
726 static int rtc_resume(struct device *dev)
727 {
728         struct rtc_time rtc_tm;
729         struct timespec time;
730
731         time.tv_nsec = 0;
732         get_rtc_time(&rtc_tm);
733         rtc_tm_to_time(&rtc_tm, &time.tv_sec);
734
735         restore_time_delta(&rtc_delta, &time);
736
737         return 0;
738 }
739 #else
740 #define rtc_suspend NULL
741 #define rtc_resume  NULL
742 #endif
743
744 static struct device_driver omap_rtc_driver = {
745         .name           = "omap_rtc",
746         .bus            = &platform_bus_type,
747         .probe          = omap_rtc_probe,
748         .remove         = __exit_p(omap_rtc_remove),
749         .suspend        = rtc_suspend,
750         .resume         = rtc_resume,
751 };
752
753 static int __init rtc_init(void)
754 {
755         return driver_register(&omap_rtc_driver);
756 }
757
758 static void __exit rtc_exit(void)
759 {
760         driver_unregister(&omap_rtc_driver);
761 }
762
763 module_init(rtc_init);
764 module_exit(rtc_exit);
765
766 MODULE_AUTHOR("George G. Davis (and others)");
767 MODULE_LICENSE("GPL");
768 MODULE_ALIAS_MISCDEV(RTC_MINOR);