string: factorize skip_spaces and export it to be generally available
[safe/jmp/linux-2.6] / drivers / rtc / rtc-sa1100.c
index 83b2bb4..e4a44b6 100644 (file)
@@ -9,7 +9,7 @@
  *
  * Modifications from:
  *   CIH <cih@coventive.com>
- *   Nicolas Pitre <nico@cam.org>
+ *   Nicolas Pitre <nico@fluxnic.net>
  *   Andrew Christian <andrew.christian@hp.com>
  *
  * Converted to the RTC subsystem and Driver Model
 #include <linux/interrupt.h>
 #include <linux/string.h>
 #include <linux/pm.h>
+#include <linux/bitops.h>
 
-#include <asm/bitops.h>
-#include <asm/hardware.h>
+#include <mach/hardware.h>
 #include <asm/irq.h>
-#include <asm/rtc.h>
 
 #ifdef CONFIG_ARCH_PXA
-#include <asm/arch/pxa-regs.h>
+#include <mach/regs-rtc.h>
+#include <mach/regs-ost.h>
 #endif
 
-#define TIMER_FREQ             CLOCK_TICK_RATE
 #define RTC_DEF_DIVIDER                32768 - 1
 #define RTC_DEF_TRIM           0
 
 static unsigned long rtc_freq = 1024;
+static unsigned long timer_freq;
 static struct rtc_time rtc_alarm;
-static spinlock_t sa1100_rtc_lock = SPIN_LOCK_UNLOCKED;
+static DEFINE_SPINLOCK(sa1100_rtc_lock);
+
+static inline int rtc_periodic_alarm(struct rtc_time *tm)
+{
+       return  (tm->tm_year == -1) ||
+               ((unsigned)tm->tm_mon >= 12) ||
+               ((unsigned)(tm->tm_mday - 1) >= 31) ||
+               ((unsigned)tm->tm_hour > 23) ||
+               ((unsigned)tm->tm_min > 59) ||
+               ((unsigned)tm->tm_sec > 59);
+}
+
+/*
+ * Calculate the next alarm time given the requested alarm time mask
+ * and the current time.
+ */
+static void rtc_next_alarm_time(struct rtc_time *next, struct rtc_time *now, struct rtc_time *alrm)
+{
+       unsigned long next_time;
+       unsigned long now_time;
+
+       next->tm_year = now->tm_year;
+       next->tm_mon = now->tm_mon;
+       next->tm_mday = now->tm_mday;
+       next->tm_hour = alrm->tm_hour;
+       next->tm_min = alrm->tm_min;
+       next->tm_sec = alrm->tm_sec;
+
+       rtc_tm_to_time(now, &now_time);
+       rtc_tm_to_time(next, &next_time);
+
+       if (next_time < now_time) {
+               /* Advance one day */
+               next_time += 60 * 60 * 24;
+               rtc_time_to_tm(next_time, next);
+       }
+}
 
 static int rtc_update_alarm(struct rtc_time *alrm)
 {
@@ -68,8 +104,7 @@ static int rtc_update_alarm(struct rtc_time *alrm)
        return ret;
 }
 
-static irqreturn_t sa1100_rtc_interrupt(int irq, void *dev_id,
-               struct pt_regs *regs)
+static irqreturn_t sa1100_rtc_interrupt(int irq, void *dev_id)
 {
        struct platform_device *pdev = to_platform_device(dev_id);
        struct rtc_device *rtc = platform_get_drvdata(pdev);
@@ -94,7 +129,7 @@ static irqreturn_t sa1100_rtc_interrupt(int irq, void *dev_id,
        if (rtsr & RTSR_HZ)
                events |= RTC_UF | RTC_IRQF;
 
-       rtc_update_irq(&rtc->class_dev, 1, events);
+       rtc_update_irq(rtc, 1, events);
 
        if (rtsr & RTSR_AL && rtc_periodic_alarm(&rtc_alarm))
                rtc_update_alarm(&rtc_alarm);
@@ -106,8 +141,7 @@ static irqreturn_t sa1100_rtc_interrupt(int irq, void *dev_id,
 
 static int rtc_timer1_count;
 
-static irqreturn_t timer1_interrupt(int irq, void *dev_id,
-               struct pt_regs *regs)
+static irqreturn_t timer1_interrupt(int irq, void *dev_id)
 {
        struct platform_device *pdev = to_platform_device(dev_id);
        struct rtc_device *rtc = platform_get_drvdata(pdev);
@@ -121,10 +155,10 @@ static irqreturn_t timer1_interrupt(int irq, void *dev_id,
         */
        OSSR = OSSR_M1; /* clear match on timer1 */
 
-       rtc_update_irq(&rtc->class_dev, rtc_timer1_count, RTC_PF | RTC_IRQF);
+       rtc_update_irq(rtc, rtc_timer1_count, RTC_PF | RTC_IRQF);
 
        if (rtc_timer1_count == 1)
-               rtc_timer1_count = (rtc_freq * ((1<<30)/(TIMER_FREQ>>2)));
+               rtc_timer1_count = (rtc_freq * ((1 << 30) / (timer_freq >> 2)));
 
        return IRQ_HANDLED;
 }
@@ -133,7 +167,7 @@ static int sa1100_rtc_read_callback(struct device *dev, int data)
 {
        if (data & RTC_PF) {
                /* interpolate missed periods and set match for the next */
-               unsigned long period = TIMER_FREQ/rtc_freq;
+               unsigned long period = timer_freq / rtc_freq;
                unsigned long oscr = OSCR;
                unsigned long osmr1 = OSMR1;
                unsigned long missed = (oscr - osmr1)/period;
@@ -157,30 +191,30 @@ static int sa1100_rtc_open(struct device *dev)
 {
        int ret;
 
-       ret = request_irq(IRQ_RTC1Hz, sa1100_rtc_interrupt, SA_INTERRUPT,
+       ret = request_irq(IRQ_RTC1Hz, sa1100_rtc_interrupt, IRQF_DISABLED,
                                "rtc 1Hz", dev);
        if (ret) {
-               printk(KERN_ERR "rtc: IRQ%d already in use.\n", IRQ_RTC1Hz);
+               dev_err(dev, "IRQ %d already in use.\n", IRQ_RTC1Hz);
                goto fail_ui;
        }
-       ret = request_irq(IRQ_RTCAlrm, sa1100_rtc_interrupt, SA_INTERRUPT,
+       ret = request_irq(IRQ_RTCAlrm, sa1100_rtc_interrupt, IRQF_DISABLED,
                                "rtc Alrm", dev);
        if (ret) {
-               printk(KERN_ERR "rtc: IRQ%d already in use.\n", IRQ_RTCAlrm);
+               dev_err(dev, "IRQ %d already in use.\n", IRQ_RTCAlrm);
                goto fail_ai;
        }
-       ret = request_irq(IRQ_OST1, timer1_interrupt, SA_INTERRUPT,
+       ret = request_irq(IRQ_OST1, timer1_interrupt, IRQF_DISABLED,
                                "rtc timer", dev);
        if (ret) {
-               printk(KERN_ERR "rtc: IRQ%d already in use.\n", IRQ_OST1);
+               dev_err(dev, "IRQ %d already in use.\n", IRQ_OST1);
                goto fail_pi;
        }
        return 0;
 
  fail_pi:
-       free_irq(IRQ_RTCAlrm, NULL);
+       free_irq(IRQ_RTCAlrm, dev);
  fail_ai:
-       free_irq(IRQ_RTC1Hz, NULL);
+       free_irq(IRQ_RTC1Hz, dev);
  fail_ui:
        return ret;
 }
@@ -229,10 +263,8 @@ static int sa1100_rtc_ioctl(struct device *dev, unsigned int cmd,
                spin_unlock_irq(&sa1100_rtc_lock);
                return 0;
        case RTC_PIE_ON:
-               if ((rtc_freq > 64) && !capable(CAP_SYS_RESOURCE))
-                       return -EACCES;
                spin_lock_irq(&sa1100_rtc_lock);
-               OSMR1 = TIMER_FREQ/rtc_freq + OSCR;
+               OSMR1 = timer_freq / rtc_freq + OSCR;
                OIER |= OIER_E1;
                rtc_timer1_count = 1;
                spin_unlock_irq(&sa1100_rtc_lock);
@@ -240,14 +272,12 @@ static int sa1100_rtc_ioctl(struct device *dev, unsigned int cmd,
        case RTC_IRQP_READ:
                return put_user(rtc_freq, (unsigned long *)arg);
        case RTC_IRQP_SET:
-               if (arg < 1 || arg > TIMER_FREQ)
+               if (arg < 1 || arg > timer_freq)
                        return -EINVAL;
-               if ((arg > 64) && (!capable(CAP_SYS_RESOURCE)))
-                       return -EACCES;
                rtc_freq = arg;
                return 0;
        }
-       return -EINVAL;
+       return -ENOIOCTLCMD;
 }
 
 static int sa1100_rtc_read_time(struct device *dev, struct rtc_time *tm)
@@ -269,8 +299,12 @@ static int sa1100_rtc_set_time(struct device *dev, struct rtc_time *tm)
 
 static int sa1100_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
 {
+       u32     rtsr;
+
        memcpy(&alrm->time, &rtc_alarm, sizeof(struct rtc_time));
-       alrm->pending = RTSR & RTSR_AL ? 1 : 0;
+       rtsr = RTSR;
+       alrm->enabled = (rtsr & RTSR_ALE) ? 1 : 0;
+       alrm->pending = (rtsr & RTSR_AL) ? 1 : 0;
        return 0;
 }
 
@@ -281,12 +315,10 @@ static int sa1100_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
        spin_lock_irq(&sa1100_rtc_lock);
        ret = rtc_update_alarm(&alrm->time);
        if (ret == 0) {
-               memcpy(&rtc_alarm, &alrm->time, sizeof(struct rtc_time));
-
                if (alrm->enabled)
-                       enable_irq_wake(IRQ_RTCAlrm);
+                       RTSR |= RTSR_ALE;
                else
-                       disable_irq_wake(IRQ_RTCAlrm);
+                       RTSR &= ~RTSR_ALE;
        }
        spin_unlock_irq(&sa1100_rtc_lock);
 
@@ -295,9 +327,7 @@ static int sa1100_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
 
 static int sa1100_rtc_proc(struct device *dev, struct seq_file *seq)
 {
-       seq_printf(seq, "trim/divider\t: 0x%08x\n", RTTR);
-       seq_printf(seq, "alarm_IRQ\t: %s\n",
-                       (RTSR & RTSR_ALE) ? "yes" : "no" );
+       seq_printf(seq, "trim/divider\t: 0x%08x\n", (u32) RTTR);
        seq_printf(seq, "update_IRQ\t: %s\n",
                        (RTSR & RTSR_HZE) ? "yes" : "no");
        seq_printf(seq, "periodic_IRQ\t: %s\n",
@@ -307,7 +337,7 @@ static int sa1100_rtc_proc(struct device *dev, struct seq_file *seq)
        return 0;
 }
 
-static struct rtc_class_ops sa1100_rtc_ops = {
+static const struct rtc_class_ops sa1100_rtc_ops = {
        .open = sa1100_rtc_open,
        .read_callback = sa1100_rtc_read_callback,
        .release = sa1100_rtc_release,
@@ -323,6 +353,8 @@ static int sa1100_rtc_probe(struct platform_device *pdev)
 {
        struct rtc_device *rtc;
 
+       timer_freq = get_clock_tick_rate();
+
        /*
         * According to the manual we should be able to let RTTR be zero
         * and then a default diviser for a 32.768KHz clock is used.
@@ -332,23 +364,21 @@ static int sa1100_rtc_probe(struct platform_device *pdev)
         */
        if (RTTR == 0) {
                RTTR = RTC_DEF_DIVIDER + (RTC_DEF_TRIM << 16);
-               printk(KERN_WARNING "rtc: warning: initializing default clock divider/trim value\n");
+               dev_warn(&pdev->dev, "warning: initializing default clock divider/trim value\n");
                /* The current RTC value probably doesn't make sense either */
                RCNR = 0;
        }
 
+       device_init_wakeup(&pdev->dev, 1);
+
        rtc = rtc_device_register(pdev->name, &pdev->dev, &sa1100_rtc_ops,
                                THIS_MODULE);
 
-       if (IS_ERR(rtc)) {
-               dev_err(&pdev->dev, "Unable to register the RTC device\n");
+       if (IS_ERR(rtc))
                return PTR_ERR(rtc);
-       }
 
        platform_set_drvdata(pdev, rtc);
 
-       dev_info(&pdev->dev, "SA11xx/PXA2xx RTC Registered\n");
-
        return 0;
 }
 
@@ -362,11 +392,35 @@ static int sa1100_rtc_remove(struct platform_device *pdev)
        return 0;
 }
 
+#ifdef CONFIG_PM
+static int sa1100_rtc_suspend(struct device *dev)
+{
+       if (device_may_wakeup(dev))
+               enable_irq_wake(IRQ_RTCAlrm);
+       return 0;
+}
+
+static int sa1100_rtc_resume(struct device *dev)
+{
+       if (device_may_wakeup(dev))
+               disable_irq_wake(IRQ_RTCAlrm);
+       return 0;
+}
+
+static const struct dev_pm_ops sa1100_rtc_pm_ops = {
+       .suspend        = sa1100_rtc_suspend,
+       .resume         = sa1100_rtc_resume,
+};
+#endif
+
 static struct platform_driver sa1100_rtc_driver = {
        .probe          = sa1100_rtc_probe,
        .remove         = sa1100_rtc_remove,
        .driver         = {
-               .name           = "sa1100-rtc",
+               .name   = "sa1100-rtc",
+#ifdef CONFIG_PM
+               .pm     = &sa1100_rtc_pm_ops,
+#endif
        },
 };
 
@@ -386,3 +440,4 @@ module_exit(sa1100_rtc_exit);
 MODULE_AUTHOR("Richard Purdie <rpurdie@rpsys.net>");
 MODULE_DESCRIPTION("SA11x0/PXA2xx Realtime Clock Driver (RTC)");
 MODULE_LICENSE("GPL");
+MODULE_ALIAS("platform:sa1100-rtc");