Merge branch 'platforms' of git://git.linaro.org/people/rmk/linux-arm
[~shefty/rdma-dev.git] / drivers / rtc / rtc-sa1100.c
index e8f1adde4d821cd6c36df785825c62fd36707a07..4940fa8c4e1002f913e7f3171d8cdc72bd5310fb 100644 (file)
 
 #include <linux/platform_device.h>
 #include <linux/module.h>
+#include <linux/clk.h>
 #include <linux/rtc.h>
 #include <linux/init.h>
 #include <linux/fs.h>
 #include <linux/interrupt.h>
+#include <linux/slab.h>
 #include <linux/string.h>
+#include <linux/of.h>
 #include <linux/pm.h>
 #include <linux/bitops.h>
 
 #include <mach/hardware.h>
 #include <mach/irqs.h>
 
-#ifdef CONFIG_ARCH_PXA
+#if defined(CONFIG_ARCH_PXA) || defined(CONFIG_ARCH_MMP)
 #include <mach/regs-rtc.h>
 #endif
 
 #define RTC_DEF_DIVIDER                (32768 - 1)
 #define RTC_DEF_TRIM           0
-
-static const unsigned long RTC_FREQ = 1024;
-static struct rtc_time rtc_alarm;
-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)
-{
-       struct rtc_time alarm_tm, now_tm;
-       unsigned long now, time;
-       int ret;
-
-       do {
-               now = RCNR;
-               rtc_time_to_tm(now, &now_tm);
-               rtc_next_alarm_time(&alarm_tm, &now_tm, alrm);
-               ret = rtc_tm_to_time(&alarm_tm, &time);
-               if (ret != 0)
-                       break;
-
-               RTSR = RTSR & (RTSR_HZE|RTSR_ALE|RTSR_AL);
-               RTAR = time;
-       } while (now != RCNR);
-
-       return ret;
-}
+#define RTC_FREQ               1024
+
+struct sa1100_rtc {
+       spinlock_t              lock;
+       int                     irq_1hz;
+       int                     irq_alarm;
+       struct rtc_device       *rtc;
+       struct clk              *clk;
+};
 
 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);
+       struct sa1100_rtc *info = dev_get_drvdata(dev_id);
+       struct rtc_device *rtc = info->rtc;
        unsigned int rtsr;
        unsigned long events = 0;
 
-       spin_lock(&sa1100_rtc_lock);
+       spin_lock(&info->lock);
 
        rtsr = RTSR;
        /* clear interrupt sources */
@@ -146,30 +96,28 @@ static irqreturn_t sa1100_rtc_interrupt(int irq, void *dev_id)
 
        rtc_update_irq(rtc, 1, events);
 
-       if (rtsr & RTSR_AL && rtc_periodic_alarm(&rtc_alarm))
-               rtc_update_alarm(&rtc_alarm);
-
-       spin_unlock(&sa1100_rtc_lock);
+       spin_unlock(&info->lock);
 
        return IRQ_HANDLED;
 }
 
 static int sa1100_rtc_open(struct device *dev)
 {
+       struct sa1100_rtc *info = dev_get_drvdata(dev);
+       struct rtc_device *rtc = info->rtc;
        int ret;
-       struct platform_device *plat_dev = to_platform_device(dev);
-       struct rtc_device *rtc = platform_get_drvdata(plat_dev);
 
-       ret = request_irq(IRQ_RTC1Hz, sa1100_rtc_interrupt, IRQF_DISABLED,
-               "rtc 1Hz", dev);
+       ret = clk_prepare_enable(info->clk);
+       if (ret)
+               goto fail_clk;
+       ret = request_irq(info->irq_1hz, sa1100_rtc_interrupt, 0, "rtc 1Hz", dev);
        if (ret) {
-               dev_err(dev, "IRQ %d already in use.\n", IRQ_RTC1Hz);
+               dev_err(dev, "IRQ %d already in use.\n", info->irq_1hz);
                goto fail_ui;
        }
-       ret = request_irq(IRQ_RTCAlrm, sa1100_rtc_interrupt, IRQF_DISABLED,
-               "rtc Alrm", dev);
+       ret = request_irq(info->irq_alarm, sa1100_rtc_interrupt, 0, "rtc Alrm", dev);
        if (ret) {
-               dev_err(dev, "IRQ %d already in use.\n", IRQ_RTCAlrm);
+               dev_err(dev, "IRQ %d already in use.\n", info->irq_alarm);
                goto fail_ai;
        }
        rtc->max_user_freq = RTC_FREQ;
@@ -178,29 +126,36 @@ static int sa1100_rtc_open(struct device *dev)
        return 0;
 
  fail_ai:
-       free_irq(IRQ_RTC1Hz, dev);
+       free_irq(info->irq_1hz, dev);
  fail_ui:
+       clk_disable_unprepare(info->clk);
+ fail_clk:
        return ret;
 }
 
 static void sa1100_rtc_release(struct device *dev)
 {
-       spin_lock_irq(&sa1100_rtc_lock);
+       struct sa1100_rtc *info = dev_get_drvdata(dev);
+
+       spin_lock_irq(&info->lock);
        RTSR = 0;
-       spin_unlock_irq(&sa1100_rtc_lock);
+       spin_unlock_irq(&info->lock);
 
-       free_irq(IRQ_RTCAlrm, dev);
-       free_irq(IRQ_RTC1Hz, dev);
+       free_irq(info->irq_alarm, dev);
+       free_irq(info->irq_1hz, dev);
+       clk_disable_unprepare(info->clk);
 }
 
 static int sa1100_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
 {
-       spin_lock_irq(&sa1100_rtc_lock);
+       struct sa1100_rtc *info = dev_get_drvdata(dev);
+
+       spin_lock_irq(&info->lock);
        if (enabled)
                RTSR |= RTSR_ALE;
        else
                RTSR &= ~RTSR_ALE;
-       spin_unlock_irq(&sa1100_rtc_lock);
+       spin_unlock_irq(&info->lock);
        return 0;
 }
 
@@ -225,7 +180,6 @@ static int sa1100_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
 {
        u32     rtsr;
 
-       memcpy(&alrm->time, &rtc_alarm, sizeof(struct rtc_time));
        rtsr = RTSR;
        alrm->enabled = (rtsr & RTSR_ALE) ? 1 : 0;
        alrm->pending = (rtsr & RTSR_AL) ? 1 : 0;
@@ -234,17 +188,22 @@ static int sa1100_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
 
 static int sa1100_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
 {
+       struct sa1100_rtc *info = dev_get_drvdata(dev);
+       unsigned long time;
        int ret;
 
-       spin_lock_irq(&sa1100_rtc_lock);
-       ret = rtc_update_alarm(&alrm->time);
-       if (ret == 0) {
-               if (alrm->enabled)
-                       RTSR |= RTSR_ALE;
-               else
-                       RTSR &= ~RTSR_ALE;
-       }
-       spin_unlock_irq(&sa1100_rtc_lock);
+       spin_lock_irq(&info->lock);
+       ret = rtc_tm_to_time(&alrm->time, &time);
+       if (ret != 0)
+               goto out;
+       RTSR = RTSR & (RTSR_HZE|RTSR_ALE|RTSR_AL);
+       RTAR = time;
+       if (alrm->enabled)
+               RTSR |= RTSR_ALE;
+       else
+               RTSR &= ~RTSR_ALE;
+out:
+       spin_unlock_irq(&info->lock);
 
        return ret;
 }
@@ -271,6 +230,27 @@ static const struct rtc_class_ops sa1100_rtc_ops = {
 static int sa1100_rtc_probe(struct platform_device *pdev)
 {
        struct rtc_device *rtc;
+       struct sa1100_rtc *info;
+       int irq_1hz, irq_alarm, ret = 0;
+
+       irq_1hz = platform_get_irq_byname(pdev, "rtc 1Hz");
+       irq_alarm = platform_get_irq_byname(pdev, "rtc alarm");
+       if (irq_1hz < 0 || irq_alarm < 0)
+               return -ENODEV;
+
+       info = kzalloc(sizeof(struct sa1100_rtc), GFP_KERNEL);
+       if (!info)
+               return -ENOMEM;
+       info->clk = clk_get(&pdev->dev, NULL);
+       if (IS_ERR(info->clk)) {
+               dev_err(&pdev->dev, "failed to find rtc clock source\n");
+               ret = PTR_ERR(info->clk);
+               goto err_clk;
+       }
+       info->irq_1hz = irq_1hz;
+       info->irq_alarm = irq_alarm;
+       spin_lock_init(&info->lock);
+       platform_set_drvdata(pdev, info);
 
        /*
         * According to the manual we should be able to let RTTR be zero
@@ -292,10 +272,11 @@ static int sa1100_rtc_probe(struct platform_device *pdev)
        rtc = rtc_device_register(pdev->name, &pdev->dev, &sa1100_rtc_ops,
                THIS_MODULE);
 
-       if (IS_ERR(rtc))
-               return PTR_ERR(rtc);
-
-       platform_set_drvdata(pdev, rtc);
+       if (IS_ERR(rtc)) {
+               ret = PTR_ERR(rtc);
+               goto err_dev;
+       }
+       info->rtc = rtc;
 
        /* Fix for a nasty initialization problem the in SA11xx RTSR register.
         * See also the comments in sa1100_rtc_interrupt().
@@ -322,14 +303,24 @@ static int sa1100_rtc_probe(struct platform_device *pdev)
        RTSR = RTSR_AL | RTSR_HZ;
 
        return 0;
+err_dev:
+       platform_set_drvdata(pdev, NULL);
+       clk_put(info->clk);
+err_clk:
+       kfree(info);
+       return ret;
 }
 
 static int sa1100_rtc_remove(struct platform_device *pdev)
 {
-       struct rtc_device *rtc = platform_get_drvdata(pdev);
+       struct sa1100_rtc *info = platform_get_drvdata(pdev);
 
-       if (rtc)
-               rtc_device_unregister(rtc);
+       if (info) {
+               rtc_device_unregister(info->rtc);
+               clk_put(info->clk);
+               platform_set_drvdata(pdev, NULL);
+               kfree(info);
+       }
 
        return 0;
 }
@@ -337,15 +328,17 @@ static int sa1100_rtc_remove(struct platform_device *pdev)
 #ifdef CONFIG_PM
 static int sa1100_rtc_suspend(struct device *dev)
 {
+       struct sa1100_rtc *info = dev_get_drvdata(dev);
        if (device_may_wakeup(dev))
-               enable_irq_wake(IRQ_RTCAlrm);
+               enable_irq_wake(info->irq_alarm);
        return 0;
 }
 
 static int sa1100_rtc_resume(struct device *dev)
 {
+       struct sa1100_rtc *info = dev_get_drvdata(dev);
        if (device_may_wakeup(dev))
-               disable_irq_wake(IRQ_RTCAlrm);
+               disable_irq_wake(info->irq_alarm);
        return 0;
 }
 
@@ -355,6 +348,13 @@ static const struct dev_pm_ops sa1100_rtc_pm_ops = {
 };
 #endif
 
+static struct of_device_id sa1100_rtc_dt_ids[] = {
+       { .compatible = "mrvl,sa1100-rtc", },
+       { .compatible = "mrvl,mmp-rtc", },
+       {}
+};
+MODULE_DEVICE_TABLE(of, sa1100_rtc_dt_ids);
+
 static struct platform_driver sa1100_rtc_driver = {
        .probe          = sa1100_rtc_probe,
        .remove         = sa1100_rtc_remove,
@@ -363,6 +363,7 @@ static struct platform_driver sa1100_rtc_driver = {
 #ifdef CONFIG_PM
                .pm     = &sa1100_rtc_pm_ops,
 #endif
+               .of_match_table = sa1100_rtc_dt_ids,
        },
 };