Major rewrite of RTC driver with review comments from RTC subgroup
authorKevin Wells <wellsk40@gmail.com>
Wed, 18 Aug 2010 18:27:32 +0000 (11:27 -0700)
committerKevin Wells <wellsk40@gmail.com>
Wed, 18 Aug 2010 18:27:32 +0000 (11:27 -0700)
Based on v4 RCT release to mainline

drivers/rtc/rtc-lpc32xx.c

index 4c331b4..ec8701c 100644 (file)
@@ -1,8 +1,4 @@
 /*
- * drivers/rtc/rtc-lpc32xx.c
- *
- * Author: Kevin Wells <kevin.wells@nxp.com>
- *
  * Copyright (C) 2010 NXP Semiconductors
  *
  * This program is free software; you can redistribute it and/or modify
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
  *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  675 Mass Ave, Cambridge, MA 02139, USA.
  */
 
-#include <linux/err.h>
-#include <linux/fs.h>
-#include <linux/interrupt.h>
+#include <linux/kernel.h>
 #include <linux/module.h>
+#include <linux/init.h>
 #include <linux/platform_device.h>
-#include <linux/rtc.h>
 #include <linux/spinlock.h>
-#include <linux/types.h>
-#include <linux/io.h>
+#include <linux/rtc.h>
 #include <linux/slab.h>
+#include <linux/io.h>
 
 /*
  * Clock and Power control register offsets
  */
-#define RTC_UCOUNT(x)          ((x) + 0x00)
-#define RTC_DCOUNT(x)          ((x) + 0x04)
-#define RTC_MATCH0(x)          ((x) + 0x08)
-#define RTC_MATCH1(x)          ((x) + 0x0C)
-#define RTC_CTRL(x)            ((x) + 0x10)
-#define RTC_INTSTAT(x)         ((x) + 0x14)
-#define RTC_KEY(x)             ((x) + 0x18)
-#define RTC_SRAM(x, y)         ((x) + 0x80 + ((y) * 4))
-
-#define RTC_MATCH0_EN          (1 << 0)
-#define RTC_MATCH1_EN          (1 << 1)
-#define RTC_ONSW_MATCH0_EN     (1 << 2)
-#define RTC_ONSW_MATCH1_EN     (1 << 3)
-#define RTC_SW_RESET           (1 << 4)
-#define RTC_CNTR_DIS           (1 << 6)
-#define RTC_ONSW_FORCE_HIGH    (1 << 7)
-
-#define RTC_MATCH0_INT_STS     (1 << 0)
-#define RTC_MATCH1_INT_STS     (1 << 1)
-#define RTC_ONSW_INT_STS       (1 << 2)
-
-#define RTC_KEY_ONSW_LOADVAL   0xB5C13F27
-
-static const char rtc_name[] = "rtc-lpc32xx";
-
-struct lpc32xx_rtc_priv {
+#define LPC32XX_RTC_UCOUNT             0x00
+#define LPC32XX_RTC_DCOUNT             0x04
+#define LPC32XX_RTC_MATCH0             0x08
+#define LPC32XX_RTC_MATCH1             0x0C
+#define LPC32XX_RTC_CTRL               0x10
+#define LPC32XX_RTC_INTSTAT            0x14
+#define LPC32XX_RTC_KEY                        0x18
+#define LPC32XX_RTC_SRAM               0x80
+
+#define LPC32XX_RTC_CTRL_MATCH0                (1 << 0)
+#define LPC32XX_RTC_CTRL_MATCH1                (1 << 1)
+#define LPC32XX_RTC_CTRL_ONSW_MATCH0   (1 << 2)
+#define LPC32XX_RTC_CTRL_ONSW_MATCH1   (1 << 3)
+#define LPC32XX_RTC_CTRL_SW_RESET      (1 << 4)
+#define LPC32XX_RTC_CTRL_CNTR_DIS      (1 << 6)
+#define LPC32XX_RTC_CTRL_ONSW_FORCE_HI (1 << 7)
+
+#define LPC32XX_RTC_INTSTAT_MATCH0     (1 << 0)
+#define LPC32XX_RTC_INTSTAT_MATCH1     (1 << 1)
+#define LPC32XX_RTC_INTSTAT_ONSW       (1 << 2)
+
+#define LPC32XX_RTC_KEY_ONSW_LOADVAL   0xB5C13F27
+
+#define RTC_NAME "rtc-lpc32xx"
+
+#define rtc_readl(dev, reg) \
+       __raw_readl((dev)->rtc_base + (reg))
+#define rtc_writel(dev, reg, val) \
+       __raw_writel((val), (dev)->rtc_base + (reg))
+
+struct lpc32xx_rtc {
        void __iomem *rtc_base;
        int irq;
-       int alarm_enabled;
+       unsigned char alarm_enabled;
        struct rtc_device *rtc;
        spinlock_t lock;
 };
 
-static inline void write_seconds(u32 iobase, unsigned long secs)
-{
-       u32 tmp;
-
-       tmp = readl(RTC_CTRL(iobase));
-       tmp |= RTC_CNTR_DIS;
-       writel(tmp, RTC_CTRL(iobase));
-
-       writel(secs, RTC_UCOUNT(iobase));
-       writel((0xFFFFFFFF - secs), RTC_DCOUNT(iobase));
-
-       tmp &= ~RTC_CNTR_DIS;
-       writel(tmp, RTC_CTRL(iobase));
-}
-
 static int lpc32xx_rtc_read_time(struct device *dev, struct rtc_time *time)
 {
        unsigned long elapsed_sec;
-       struct lpc32xx_rtc_priv *lpc32xx_rtc_dat = dev_get_drvdata(dev);
-
-       elapsed_sec = readl(RTC_UCOUNT(lpc32xx_rtc_dat->rtc_base));
+       struct lpc32xx_rtc *rtc = dev_get_drvdata(dev);
 
+       elapsed_sec = rtc_readl(rtc, LPC32XX_RTC_UCOUNT);
        rtc_time_to_tm(elapsed_sec, time);
 
-       return 0;
+       return rtc_valid_tm(time);
 }
 
 static int lpc32xx_rtc_set_mmss(struct device *dev, unsigned long secs)
 {
-       struct lpc32xx_rtc_priv *lpc32xx_rtc_dat = dev_get_drvdata(dev);
+       struct lpc32xx_rtc *rtc = dev_get_drvdata(dev);
+       u32 tmp;
+
+       spin_lock_irq(&rtc->lock);
 
-       spin_lock_irq(&lpc32xx_rtc_dat->lock);
-       write_seconds((u32) lpc32xx_rtc_dat->rtc_base, secs);
-       spin_unlock_irq(&lpc32xx_rtc_dat->lock);
+       /* RTC must be disabled during count update */
+       tmp = rtc_readl(rtc, LPC32XX_RTC_CTRL);
+       rtc_writel(rtc, LPC32XX_RTC_CTRL, tmp | LPC32XX_RTC_CTRL_CNTR_DIS);
+       rtc_writel(rtc, LPC32XX_RTC_UCOUNT, secs);
+       rtc_writel(rtc, LPC32XX_RTC_DCOUNT, 0xFFFFFFFF - secs);
+       rtc_writel(rtc, LPC32XX_RTC_CTRL, tmp &= ~LPC32XX_RTC_CTRL_CNTR_DIS);
+
+       spin_unlock_irq(&rtc->lock);
 
        return 0;
 }
@@ -104,62 +94,47 @@ static int lpc32xx_rtc_set_mmss(struct device *dev, unsigned long secs)
 static int lpc32xx_rtc_read_alarm(struct device *dev,
        struct rtc_wkalrm *wkalrm)
 {
-       struct lpc32xx_rtc_priv *lpc32xx_rtc_dat = dev_get_drvdata(dev);
-       unsigned long alarmsecs;
-
-       alarmsecs = readl(RTC_MATCH0(lpc32xx_rtc_dat->rtc_base));
-       wkalrm->enabled = lpc32xx_rtc_dat->alarm_enabled;
+       struct lpc32xx_rtc *rtc = dev_get_drvdata(dev);
 
-       rtc_time_to_tm(alarmsecs, &wkalrm->time);
+       rtc_time_to_tm(rtc_readl(rtc, LPC32XX_RTC_MATCH0), &wkalrm->time);
+       wkalrm->enabled = rtc->alarm_enabled;
+       wkalrm->pending = !!(rtc_readl(rtc, LPC32XX_RTC_INTSTAT) &
+               LPC32XX_RTC_INTSTAT_MATCH0);
 
-       return 0;
+       return rtc_valid_tm(&wkalrm->time);
 }
 
 static int lpc32xx_rtc_set_alarm(struct device *dev,
        struct rtc_wkalrm *wkalrm)
 {
-       struct lpc32xx_rtc_priv *lpc32xx_rtc_dat = dev_get_drvdata(dev);
+       struct lpc32xx_rtc *rtc = dev_get_drvdata(dev);
        unsigned long alarmsecs;
+       u32 tmp;
        int ret;
 
        ret = rtc_tm_to_time(&wkalrm->time, &alarmsecs);
        if (ret < 0) {
-               dev_err(dev, "Failed to convert time: %d\n", ret);
+               dev_warn(dev, "Failed to convert time: %d\n", ret);
                return ret;
        }
 
-       spin_lock_irq(&lpc32xx_rtc_dat->lock);
+       spin_lock_irq(&rtc->lock);
 
-       if (lpc32xx_rtc_dat->alarm_enabled)
-               writel(~RTC_MATCH0_EN &
-                       readl(RTC_CTRL(lpc32xx_rtc_dat->rtc_base)),
-                       RTC_CTRL(lpc32xx_rtc_dat->rtc_base));
+       /* Disable alarm during update */
+       tmp = rtc_readl(rtc, LPC32XX_RTC_CTRL);
+       rtc_writel(rtc, LPC32XX_RTC_CTRL, tmp & ~LPC32XX_RTC_CTRL_MATCH0);
 
-       writel(alarmsecs, RTC_MATCH0(lpc32xx_rtc_dat->rtc_base));
-       lpc32xx_rtc_dat->alarm_enabled = wkalrm->enabled;
+       rtc_writel(rtc, LPC32XX_RTC_MATCH0, alarmsecs);
 
+       rtc->alarm_enabled = wkalrm->enabled;
        if (wkalrm->enabled) {
-               writel(RTC_MATCH0_INT_STS,
-                       RTC_INTSTAT(lpc32xx_rtc_dat->rtc_base));
-
-               writel(RTC_MATCH0_EN |
-                       readl(RTC_CTRL(lpc32xx_rtc_dat->rtc_base)),
-                       RTC_CTRL(lpc32xx_rtc_dat->rtc_base));
+               rtc_writel(rtc, LPC32XX_RTC_INTSTAT,
+                          LPC32XX_RTC_INTSTAT_MATCH0);
+               rtc_writel(rtc, LPC32XX_RTC_CTRL, tmp |
+                          LPC32XX_RTC_CTRL_MATCH0);
        }
 
-       spin_unlock_irq(&lpc32xx_rtc_dat->lock);
-
-       return 0;
-}
-
-static int lpc32xx_rtc_proc(struct device *dev, struct seq_file *seq)
-{
-       struct lpc32xx_rtc_priv *lpc32xx_rtc_dat = dev_get_drvdata(dev);
-       u32 tmp;
-
-       tmp = readl(RTC_CTRL(lpc32xx_rtc_dat->rtc_base));
-       seq_printf(seq, "Alarm_IRQ\t: %s\n",
-                  (tmp & RTC_MATCH0_EN) ? "yes" : "no");
+       spin_unlock_irq(&rtc->lock);
 
        return 0;
 }
@@ -167,50 +142,48 @@ static int lpc32xx_rtc_proc(struct device *dev, struct seq_file *seq)
 static int lpc32xx_rtc_alarm_irq_enable(struct device *dev,
        unsigned int enabled)
 {
-       struct lpc32xx_rtc_priv *lpc32xx_rtc_dat = dev_get_drvdata(dev);
+       struct lpc32xx_rtc *rtc = dev_get_drvdata(dev);
+       u32 tmp;
+
+       spin_lock_irq(&rtc->lock);
+       tmp = rtc_readl(rtc, LPC32XX_RTC_CTRL);
 
        if (enabled) {
-               if (!lpc32xx_rtc_dat->alarm_enabled) {
-                       spin_lock_irq(&lpc32xx_rtc_dat->lock);
-                       lpc32xx_rtc_dat->alarm_enabled = 1;
-                       writel(RTC_MATCH0_EN |
-                               readl(RTC_CTRL(lpc32xx_rtc_dat->rtc_base)),
-                               RTC_CTRL(lpc32xx_rtc_dat->rtc_base));
-                       spin_unlock_irq(&lpc32xx_rtc_dat->lock);
-               }
-       } else if (lpc32xx_rtc_dat->alarm_enabled) {
-               spin_lock_irq(&lpc32xx_rtc_dat->lock);
-               lpc32xx_rtc_dat->alarm_enabled = 0;
-               writel(~RTC_MATCH0_EN &
-                       readl(RTC_CTRL(lpc32xx_rtc_dat->rtc_base)),
-                       RTC_CTRL(lpc32xx_rtc_dat->rtc_base));
-               spin_unlock_irq(&lpc32xx_rtc_dat->lock);
+               rtc->alarm_enabled = 1;
+               tmp |= LPC32XX_RTC_CTRL_MATCH0;
+       } else {
+               rtc->alarm_enabled = 0;
+               tmp &= ~LPC32XX_RTC_CTRL_MATCH0;
        }
 
+       rtc_writel(rtc, LPC32XX_RTC_CTRL, tmp);
+       spin_unlock_irq(&rtc->lock);
+
        return 0;
 }
 
 static irqreturn_t lpc32xx_rtc_alarm_interrupt(int irq, void *dev)
 {
-       struct lpc32xx_rtc_priv *lpc32xx_rtc_dat =
-               (struct lpc32xx_rtc_priv *) dev;
+       struct lpc32xx_rtc *rtc = dev;
 
-       spin_lock(&lpc32xx_rtc_dat->lock);
+       spin_lock(&rtc->lock);
 
-       /* If the alarm isn't disabled, the match will keep occuring, so
-               disable it now */
-       writel(readl(RTC_CTRL(lpc32xx_rtc_dat->rtc_base)) &
-               ~RTC_MATCH0_EN, RTC_CTRL(lpc32xx_rtc_dat->rtc_base));
+       /* Disable alarm interrupt */
+       rtc_writel(rtc, LPC32XX_RTC_CTRL,
+               rtc_readl(rtc, LPC32XX_RTC_CTRL) &
+                         ~LPC32XX_RTC_CTRL_MATCH0);
+       rtc->alarm_enabled = 0;
 
-       /* Write a large value to the match value so the RTC won't
-          keep firing the match status */
-       writel(0xFFFFFFFF, RTC_MATCH0(lpc32xx_rtc_dat->rtc_base));
-       writel(RTC_MATCH0_INT_STS,
-               RTC_INTSTAT(lpc32xx_rtc_dat->rtc_base));
+       /*
+        * Write a large value to the match value so the RTC won't
+        * keep firing the match status
+        */
+       rtc_writel(rtc, LPC32XX_RTC_MATCH0, 0xFFFFFFFF);
+       rtc_writel(rtc, LPC32XX_RTC_INTSTAT, LPC32XX_RTC_INTSTAT_MATCH0);
 
-       rtc_update_irq(lpc32xx_rtc_dat->rtc, 1, RTC_IRQF | RTC_AF);
+       spin_unlock(&rtc->lock);
 
-       spin_unlock(&lpc32xx_rtc_dat->lock);
+       rtc_update_irq(rtc->rtc, 1, RTC_IRQF | RTC_AF);
 
        return IRQ_HANDLED;
 }
@@ -220,15 +193,15 @@ static const struct rtc_class_ops lpc32xx_rtc_ops = {
        .set_mmss               = lpc32xx_rtc_set_mmss,
        .read_alarm             = lpc32xx_rtc_read_alarm,
        .set_alarm              = lpc32xx_rtc_set_alarm,
-       .proc                   = lpc32xx_rtc_proc,
        .alarm_irq_enable       = lpc32xx_rtc_alarm_irq_enable,
 };
 
 static int __devinit lpc32xx_rtc_probe(struct platform_device *pdev)
 {
-       struct resource *res, *mem = NULL;
-       struct lpc32xx_rtc_priv *lpc32xx_rtc_dat = NULL;
-       int rtcirq, retval;
+       struct resource *res;
+       struct lpc32xx_rtc *rtc;
+       resource_size_t size;
+       int rtcirq;
        u32 tmp;
 
        res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
@@ -238,141 +211,129 @@ static int __devinit lpc32xx_rtc_probe(struct platform_device *pdev)
        }
 
        rtcirq = platform_get_irq(pdev, 0);
-       if ((rtcirq < 0) || (rtcirq >= NR_IRQS)) {
-               dev_err(&pdev->dev, "Can't get interrupt resource\n");
-               return -ENOENT;
+       if (rtcirq < 0 || rtcirq >= NR_IRQS) {
+               dev_warn(&pdev->dev, "Can't get interrupt resource\n");
+               rtcirq = -1;
        }
 
-       lpc32xx_rtc_dat = kzalloc(sizeof(struct lpc32xx_rtc_priv),
-               GFP_KERNEL);
-       if (unlikely(!lpc32xx_rtc_dat)) {
+       rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
+       if (unlikely(!rtc)) {
                dev_err(&pdev->dev, "Can't allocate memory\n");
                return -ENOMEM;
        }
-       lpc32xx_rtc_dat->irq = rtcirq;
+       rtc->irq = rtcirq;
+
+       size = resource_size(res);
 
-       mem = request_mem_region(res->start, resource_size(res), pdev->name);
-       if (!mem) {
+       if (!devm_request_mem_region(&pdev->dev, res->start, size,
+                                    pdev->name)) {
                dev_err(&pdev->dev, "RTC registers are not free\n");
-               retval = -EBUSY;
-               goto err_reqmem;
+               return -EBUSY;
        }
 
-       lpc32xx_rtc_dat->rtc_base = ioremap(res->start,
-               res->end - res->start + 1);
-       if (!lpc32xx_rtc_dat->rtc_base) {
+       rtc->rtc_base = devm_ioremap(&pdev->dev, res->start, size);
+       if (!rtc->rtc_base) {
                dev_err(&pdev->dev, "Can't map memory\n");
-               retval = -EIO;
-               goto err_noremap;
+               return -ENOMEM;
        }
 
-       spin_lock_init(&lpc32xx_rtc_dat->lock);
-
-       /* If RTC has never been previously setup, then set it up
-          now */
-       if (readl(RTC_KEY(lpc32xx_rtc_dat->rtc_base)) !=
-               RTC_KEY_ONSW_LOADVAL) {
-               spin_lock_irq(&lpc32xx_rtc_dat->lock);
-               tmp = readl(RTC_CTRL(lpc32xx_rtc_dat->rtc_base));
-               tmp &= ~(RTC_SW_RESET | RTC_CNTR_DIS | RTC_MATCH0_EN |
-                       RTC_MATCH1_EN | RTC_ONSW_MATCH0_EN |
-                       RTC_ONSW_MATCH1_EN | RTC_ONSW_FORCE_HIGH);
-               writel(tmp, RTC_CTRL(lpc32xx_rtc_dat->rtc_base));
+       spin_lock_init(&rtc->lock);
+
+       /*
+        * The RTC is on a seperate power domain and can keep it's state
+        * across a chip power cycle. If the RTC has never been previously
+        * setup, then set it up now for the first time.
+        */
+       tmp = rtc_readl(rtc, LPC32XX_RTC_CTRL);
+       if (rtc_readl(rtc, LPC32XX_RTC_KEY) != LPC32XX_RTC_KEY_ONSW_LOADVAL) {
+               tmp &= ~(LPC32XX_RTC_CTRL_SW_RESET |
+                       LPC32XX_RTC_CTRL_CNTR_DIS |
+                       LPC32XX_RTC_CTRL_MATCH0 |
+                       LPC32XX_RTC_CTRL_MATCH1 |
+                       LPC32XX_RTC_CTRL_ONSW_MATCH0 |
+                       LPC32XX_RTC_CTRL_ONSW_MATCH1 |
+                       LPC32XX_RTC_CTRL_ONSW_FORCE_HI);
+               rtc_writel(rtc, LPC32XX_RTC_CTRL, tmp);
 
                /* Clear latched interrupt states */
-               writel(0xFFFFFFFF, RTC_MATCH0(lpc32xx_rtc_dat->rtc_base));
-               writel(RTC_MATCH0_INT_STS | RTC_MATCH1_INT_STS |
-                       RTC_ONSW_INT_STS,
-                       RTC_INTSTAT(lpc32xx_rtc_dat->rtc_base));
+               rtc_writel(rtc, LPC32XX_RTC_MATCH0, 0xFFFFFFFF);
+               rtc_writel(rtc, LPC32XX_RTC_INTSTAT,
+                          LPC32XX_RTC_INTSTAT_MATCH0 |
+                          LPC32XX_RTC_INTSTAT_MATCH1 |
+                          LPC32XX_RTC_INTSTAT_ONSW);
 
                /* Write key value to RTC so it won't reload on reset */
-               writel(RTC_KEY_ONSW_LOADVAL,
-                       RTC_KEY(lpc32xx_rtc_dat->rtc_base));
-
-               spin_unlock_irq(&lpc32xx_rtc_dat->lock);
-       } else if (readl(RTC_CTRL(lpc32xx_rtc_dat->rtc_base)) & RTC_MATCH0_EN)
-               lpc32xx_rtc_dat->alarm_enabled = 1;
+               rtc_writel(rtc, LPC32XX_RTC_KEY,
+                          LPC32XX_RTC_KEY_ONSW_LOADVAL);
+       } else {
+               rtc_writel(rtc, LPC32XX_RTC_CTRL,
+                          tmp & ~LPC32XX_RTC_CTRL_MATCH0);
+       }
 
-       platform_set_drvdata(pdev, lpc32xx_rtc_dat);
+       platform_set_drvdata(pdev, rtc);
 
-       device_init_wakeup(&pdev->dev, 1);
-       lpc32xx_rtc_dat->rtc = rtc_device_register(rtc_name, &pdev->dev,
-               &lpc32xx_rtc_ops, THIS_MODULE);
-       if (IS_ERR(lpc32xx_rtc_dat->rtc)) {
+       rtc->rtc = rtc_device_register(RTC_NAME, &pdev->dev, &lpc32xx_rtc_ops,
+               THIS_MODULE);
+       if (IS_ERR(rtc->rtc)) {
                dev_err(&pdev->dev, "Can't get RTC\n");
-               retval = PTR_ERR(lpc32xx_rtc_dat->rtc);
-               goto err_noreg;
+               platform_set_drvdata(pdev, NULL);
+               return PTR_ERR(rtc->rtc);
        }
 
-       retval = request_irq(lpc32xx_rtc_dat->irq, lpc32xx_rtc_alarm_interrupt,
-               IRQF_DISABLED, "rtcalarm", lpc32xx_rtc_dat);
-       if (retval < 0) {
-               dev_err(&pdev->dev, "Can't request interrupt\n");
-               goto err_free_irq;
+       /*
+        * IRQ is enabled after device registration in case alarm IRQ
+        * is pending upon suspend exit.
+        */
+       if (rtc->irq >= 0) {
+               if (devm_request_irq(&pdev->dev, rtc->irq,
+                                    lpc32xx_rtc_alarm_interrupt,
+                                    IRQF_DISABLED, pdev->name, rtc) < 0) {
+                       dev_warn(&pdev->dev, "Can't request interrupt.\n");
+                       rtc->irq = -1;
+               } else {
+                       device_init_wakeup(&pdev->dev, 1);
+               }
        }
 
        return 0;
-
-err_free_irq:
-       rtc_device_unregister(lpc32xx_rtc_dat->rtc);
-err_noreg:
-       iounmap(lpc32xx_rtc_dat->rtc_base);
-err_noremap:
-       release_resource(mem);
-err_reqmem:
-       kfree(lpc32xx_rtc_dat);
-
-       return retval;
 }
 
 static int __devexit lpc32xx_rtc_remove(struct platform_device *pdev)
 {
-       struct lpc32xx_rtc_priv *lpc32xx_rtc_dat =
-               platform_get_drvdata(pdev);
+       struct lpc32xx_rtc *rtc = platform_get_drvdata(pdev);
 
-       free_irq(lpc32xx_rtc_dat->irq, pdev);
-       rtc_device_unregister(lpc32xx_rtc_dat->rtc);
-       iounmap(lpc32xx_rtc_dat->rtc_base);
-       release_resource(dev_get_drvdata(&lpc32xx_rtc_dat->rtc->dev));
-       kfree(lpc32xx_rtc_dat);
+       if (rtc->irq >= 0)
+               device_init_wakeup(&pdev->dev, 0);
+
+       platform_set_drvdata(pdev, NULL);
+       rtc_device_unregister(rtc->rtc);
 
        return 0;
 }
 
 #ifdef CONFIG_PM
-/* Turn off the alarm if it should not be a wake source. */
 static int lpc32xx_rtc_suspend(struct device *dev)
 {
        struct platform_device *pdev = to_platform_device(dev);
-       struct lpc32xx_rtc_priv *lpc32xx_rtc_dat =
-               platform_get_drvdata(pdev);
-
-       if (device_may_wakeup(&pdev->dev))
-               enable_irq_wake(lpc32xx_rtc_dat->irq);
-       else
-               disable_irq_wake(lpc32xx_rtc_dat->irq);
+       struct lpc32xx_rtc *rtc = platform_get_drvdata(pdev);
 
+       if (rtc->irq >= 0) {
+               if (device_may_wakeup(&pdev->dev))
+                       enable_irq_wake(rtc->irq);
+               else
+                       disable_irq_wake(rtc->irq);
+       }
 
        return 0;
 }
 
-/* Enable the alarm if it should be enabled (in case it was disabled to
- * prevent use as a wake source).
- */
 static int lpc32xx_rtc_resume(struct device *dev)
 {
        struct platform_device *pdev = to_platform_device(dev);
-       struct lpc32xx_rtc_priv *lpc32xx_rtc_dat =
-               platform_get_drvdata(pdev);
+       struct lpc32xx_rtc *rtc = platform_get_drvdata(pdev);
 
-       if (lpc32xx_rtc_dat->alarm_enabled) {
-               spin_lock_irq(&lpc32xx_rtc_dat->lock);
-
-               writel(RTC_MATCH0_EN |
-                       readl(RTC_CTRL(lpc32xx_rtc_dat->rtc_base)),
-                       RTC_CTRL(lpc32xx_rtc_dat->rtc_base));
-               spin_unlock_irq(&lpc32xx_rtc_dat->lock);
-       }
+       if (rtc->irq >= 0 && device_may_wakeup(&pdev->dev))
+               disable_irq_wake(rtc->irq);
 
        return 0;
 }
@@ -381,37 +342,57 @@ static int lpc32xx_rtc_resume(struct device *dev)
 static int lpc32xx_rtc_freeze(struct device *dev)
 {
        struct platform_device *pdev = to_platform_device(dev);
-       struct lpc32xx_rtc_priv *lpc32xx_rtc_dat =
-               platform_get_drvdata(pdev);
+       struct lpc32xx_rtc *rtc = platform_get_drvdata(pdev);
 
-       spin_lock_irq(&lpc32xx_rtc_dat->lock);
+       spin_lock_irq(&rtc->lock);
 
-       writel(~RTC_MATCH0_EN &
-               readl(RTC_CTRL(lpc32xx_rtc_dat->rtc_base)),
-               RTC_CTRL(lpc32xx_rtc_dat->rtc_base));
+       rtc_writel(rtc, LPC32XX_RTC_CTRL,
+               rtc_readl(rtc, LPC32XX_RTC_CTRL) &
+                         ~LPC32XX_RTC_CTRL_MATCH0);
+
+       spin_unlock_irq(&rtc->lock);
+
+       return 0;
+}
+
+static int lpc32xx_rtc_thaw(struct device *dev)
+{
+       struct platform_device *pdev = to_platform_device(dev);
+       struct lpc32xx_rtc *rtc = platform_get_drvdata(pdev);
+
+       if (rtc->alarm_enabled) {
+               spin_lock_irq(&rtc->lock);
+
+               rtc_writel(rtc, LPC32XX_RTC_CTRL,
+                          rtc_readl(rtc, LPC32XX_RTC_CTRL) |
+                          LPC32XX_RTC_CTRL_MATCH0);
+
+               spin_unlock_irq(&rtc->lock);
+       }
 
        return 0;
 }
-#else
-#define lpc32xx_rtc_suspend NULL
-#define lpc32xx_rtc_resume NULL
-#define lpc32xx_rtc_freeze NULL
-#endif
 
 static const struct dev_pm_ops lpc32xx_rtc_pm_ops = {
        .suspend = lpc32xx_rtc_suspend,
        .resume = lpc32xx_rtc_resume,
        .freeze = lpc32xx_rtc_freeze,
-       .thaw = lpc32xx_rtc_resume,
+       .thaw = lpc32xx_rtc_thaw,
        .restore = lpc32xx_rtc_resume
 };
 
+#define LPC32XX_RTC_PM_OPS (&lpc32xx_rtc_pm_ops)
+#else
+#define LPC32XX_RTC_PM_OPS NULL
+#endif
+
 static struct platform_driver lpc32xx_rtc_driver = {
        .probe          = lpc32xx_rtc_probe,
        .remove         = __devexit_p(lpc32xx_rtc_remove),
        .driver = {
-               .name = rtc_name,
-               .pm = &lpc32xx_rtc_pm_ops,
+               .name   = RTC_NAME,
+               .owner  = THIS_MODULE,
+               .pm     = LPC32XX_RTC_PM_OPS
        },
 };
 
@@ -419,15 +400,15 @@ static int __init lpc32xx_rtc_init(void)
 {
        return platform_driver_register(&lpc32xx_rtc_driver);
 }
+module_init(lpc32xx_rtc_init);
 
 static void __exit lpc32xx_rtc_exit(void)
 {
        platform_driver_unregister(&lpc32xx_rtc_driver);
 }
-
-module_init(lpc32xx_rtc_init);
 module_exit(lpc32xx_rtc_exit);
 
-MODULE_AUTHOR("Kevin Wells <kevin.wells@nxp.com");
-MODULE_DESCRIPTION("LPC32XX RTC Driver");
+MODULE_AUTHOR("Kevin Wells <wellsk40@gmail.com");
+MODULE_DESCRIPTION("RTC driver for the LPC32xx SoC");
 MODULE_LICENSE("GPL");
+MODULE_ALIAS("platform:rtc-lpc32xx");