diff mbox

[1/2] rtc: rtc-lpc32xx: Introduce RTC driver for the LPC32XX SoC

Message ID 1281370650-29520-2-git-send-email-wellsk40@gmail.com
State Superseded
Headers show

Commit Message

wellsk40@gmail.com Aug. 9, 2010, 4:17 p.m. UTC
From: Kevin Wells <wellsk40@gmail.com>

This patch contains the RTC driver for the built-in RTC in
the LPC32XX SoC.

Signed-off-by: Kevin Wells <wellsk40@gmail.com>
Signed-off-by: Durgesh Pattamatta <durgesh.pattamatta@nxp.com>
---
 drivers/rtc/rtc-lpc32xx.c |  391 +++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 391 insertions(+), 0 deletions(-)
 create mode 100644 drivers/rtc/rtc-lpc32xx.c

Comments

Wan ZongShun Aug. 10, 2010, 1:55 a.m. UTC | #1
Hi Kevin ,

This is really a natty rtc patch:).


2010/8/10  <wellsk40@gmail.com>:
> From: Kevin Wells <wellsk40@gmail.com>
>
> This patch contains the RTC driver for the built-in RTC in
> the LPC32XX SoC.
>
> Signed-off-by: Kevin Wells <wellsk40@gmail.com>
> Signed-off-by: Durgesh Pattamatta <durgesh.pattamatta@nxp.com>
> ---
>  drivers/rtc/rtc-lpc32xx.c |  391 +++++++++++++++++++++++++++++++++++++++++++++
>  1 files changed, 391 insertions(+), 0 deletions(-)
>  create mode 100644 drivers/rtc/rtc-lpc32xx.c
>
> diff --git a/drivers/rtc/rtc-lpc32xx.c b/drivers/rtc/rtc-lpc32xx.c
> new file mode 100644
> index 0000000..7803c68
> --- /dev/null
> +++ b/drivers/rtc/rtc-lpc32xx.c
> @@ -0,0 +1,391 @@
> +/*
> + * Copyright (C) 2010 NXP Semiconductors
> + *
> + * This program is free software; you can redistribute it and/or modify
> + * it under the terms of the GNU General Public License as published by
> + * the Free Software Foundation; either version 2 of the License, or
> + * (at your option) any later version.
> + *
> + *  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/kernel.h>
> +#include <linux/module.h>
> +#include <linux/init.h>
> +#include <linux/platform_device.h>
> +#include <linux/spinlock.h>
> +#include <linux/rtc.h>
> +#include <linux/slab.h>
> +#include <linux/io.h>
> +
> +/*
> + * Clock and Power control register offsets
> + */
> +#define RTC_UCOUNT             0x00
> +#define RTC_DCOUNT             0x04
> +#define RTC_MATCH0             0x08
> +#define RTC_MATCH1             0x0C
> +#define RTC_CTRL               0x10
> +#define RTC_INTSTAT            0x14
> +#define RTC_KEY                        0x18
> +#define RTC_SRAM               0x80
> +
> +#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
> +
> +#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;
> +       unsigned int irq;
> +       int alarm_enabled;
> +       struct rtc_device *rtc;
> +       spinlock_t lock;
> +};
> +
> +static int lpc32xx_rtc_read_time(struct device *dev, struct rtc_time *time)
> +{
> +       unsigned long elapsed_sec;
> +       struct lpc32xx_rtc *rtc = dev_get_drvdata(dev);
> +
> +       elapsed_sec = rtc_readl(rtc, RTC_UCOUNT);
> +       rtc_time_to_tm(elapsed_sec, time);
> +
> +       return rtc_valid_tm(time);
> +}
> +
> +static int lpc32xx_rtc_set_mmss(struct device *dev, unsigned long secs)
> +{
> +       struct lpc32xx_rtc *rtc = dev_get_drvdata(dev);
> +       u32 tmp;
> +
> +       spin_lock_irq(&rtc->lock);
> +
> +       /* RTC must be disabled during count update */
> +       tmp = rtc_readl(rtc, RTC_CTRL);
> +       rtc_writel(rtc, RTC_CTRL, tmp | RTC_CNTR_DIS);
> +       rtc_writel(rtc, RTC_UCOUNT, secs);
> +       rtc_writel(rtc, RTC_DCOUNT, 0xFFFFFFFF - secs);
> +       rtc_writel(rtc, RTC_CTRL, tmp &= ~RTC_CNTR_DIS);
> +
> +       spin_unlock_irq(&rtc->lock);
> +
> +       return 0;
> +}
> +
> +static int lpc32xx_rtc_read_alarm(struct device *dev,
> +       struct rtc_wkalrm *wkalrm)
> +{
> +       struct lpc32xx_rtc *rtc = dev_get_drvdata(dev);
> +
> +       rtc_time_to_tm(rtc_readl(rtc, RTC_MATCH0), &wkalrm->time);
> +       wkalrm->enabled = rtc->alarm_enabled;
> +
> +       return rtc_valid_tm(&wkalrm->time);
> +}
> +
> +static int lpc32xx_rtc_set_alarm(struct device *dev,
> +       struct rtc_wkalrm *wkalrm)
> +{
> +       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);
> +               return ret;
> +       }
> +
> +       spin_lock_irq(&rtc->lock);
> +
> +       /* Disable alarm during update */
> +       tmp = rtc_readl(rtc, RTC_CTRL);
> +       rtc_writel(rtc, RTC_CTRL, tmp & ~RTC_MATCH0_EN);
> +
> +       rtc->alarm_enabled = wkalrm->enabled = 1;
> +       if (wkalrm->enabled) {
> +               rtc_writel(rtc, RTC_MATCH0, alarmsecs);
> +               rtc_writel(rtc, RTC_INTSTAT, RTC_MATCH0_INT_STS);
> +               rtc_writel(rtc, RTC_CTRL, tmp | RTC_MATCH0_EN);
> +       }

I think this 'wkalrm->enabled ' will always be set '1', so 'if'
condition will be alway true?

> +
> +       spin_unlock_irq(&rtc->lock);
> +
> +       return 0;
> +}
> +
> +static int lpc32xx_rtc_alarm_irq_enable(struct device *dev,
> +       unsigned int enabled)
> +{
> +       struct lpc32xx_rtc *rtc = dev_get_drvdata(dev);
> +       u32 tmp;
> +
> +       spin_lock_irq(&rtc->lock);
> +       rtc->alarm_enabled = (int) enabled;
> +       tmp = rtc_readl(rtc, RTC_CTRL);
> +
> +       if (enabled)
> +               tmp |= RTC_MATCH0_EN;
> +       else
> +               tmp &= ~RTC_MATCH0_EN;
> +
> +       rtc_writel(rtc, RTC_CTRL, tmp);
> +       spin_unlock_irq(&rtc->lock);
> +
> +       return 0;
> +}
> +
> +static irqreturn_t lpc32xx_rtc_alarm_interrupt(int irq, void *dev)
> +{
> +       struct lpc32xx_rtc *rtc = (struct lpc32xx_rtc *) dev;
> +
> +       spin_lock(&rtc->lock);
> +
> +       /* Disable alarm interrupt */
> +       rtc_writel(rtc, RTC_CTRL,
> +               rtc_readl(rtc, RTC_CTRL) & ~RTC_MATCH0_EN);
> +       rtc->alarm_enabled = 0;
> +
> +       /*
> +        * Write a large value to the match value so the RTC won't
> +        * keep firing the match status
> +        */
> +       rtc_writel(rtc, RTC_MATCH0, 0xFFFFFFFF);
> +       rtc_writel(rtc, RTC_INTSTAT, RTC_MATCH0_INT_STS);
> +
> +       spin_unlock(&rtc->lock);
> +
> +       rtc_update_irq(rtc->rtc, 1, RTC_IRQF | RTC_AF);
> +
> +       return IRQ_HANDLED;
> +}
> +
> +static const struct rtc_class_ops lpc32xx_rtc_ops = {
> +       .read_time              = lpc32xx_rtc_read_time,
> +       .set_mmss               = lpc32xx_rtc_set_mmss,
> +       .read_alarm             = lpc32xx_rtc_read_alarm,
> +       .set_alarm              = lpc32xx_rtc_set_alarm,
> +       .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 *rtc = NULL;
> +       int rtcirq, retval;
> +       u32 tmp;
> +
> +       res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
> +       if (!res) {
> +               dev_err(&pdev->dev, "Can't get memory resource\n");
> +               return -ENOENT;
> +       }
> +
> +       rtcirq = platform_get_irq(pdev, 0);
> +       if ((rtcirq < 0) || (rtcirq >= NR_IRQS)) {
> +               dev_err(&pdev->dev, "Can't get interrupt resource\n");
> +               return -ENOENT;
> +       }
> +
> +       rtc = kzalloc(sizeof(struct lpc32xx_rtc), GFP_KERNEL);
> +       if (unlikely(!rtc)) {
> +               dev_err(&pdev->dev, "Can't allocate memory\n");
> +               return -ENOMEM;
> +       }
> +       rtc->irq = rtcirq;
> +
> +       mem = request_mem_region(res->start, resource_size(res), pdev->name);
> +       if (!mem) {
> +               dev_err(&pdev->dev, "RTC registers are not free\n");
> +               retval = -EBUSY;
> +               goto err_reqmem;
> +       }
> +
> +       rtc->rtc_base = ioremap(res->start, res->end - res->start + 1);

Use resource_size(res) here.

> +       if (!rtc->rtc_base) {
> +               dev_err(&pdev->dev, "Can't map memory\n");
> +               retval = -EIO;
> +               goto err_noremap;
> +       }
> +
> +       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.
> +        */
> +       if (rtc_readl(rtc, RTC_KEY) == RTC_KEY_ONSW_LOADVAL) {
> +               tmp = rtc_readl(rtc, RTC_CTRL);
> +               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);
> +               rtc_writel(rtc, RTC_CTRL, tmp);
> +
> +               /* Clear latched interrupt states */
> +               rtc_writel(rtc, RTC_MATCH0, 0xFFFFFFFF);
> +               rtc_writel(rtc, RTC_INTSTAT, RTC_MATCH0_INT_STS |
> +                       RTC_MATCH1_INT_STS | RTC_ONSW_INT_STS);
> +
> +               /* Write key value to RTC so it won't reload on reset */
> +               rtc_writel(rtc, RTC_KEY, RTC_KEY_ONSW_LOADVAL);
> +       } else if (rtc_readl(rtc, RTC_CTRL) & RTC_MATCH0_EN)
> +               rtc->alarm_enabled = 1;
> +
> +       platform_set_drvdata(pdev, rtc);
> +
> +       device_init_wakeup(&pdev->dev, 1);
> +       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(rtc->rtc);
> +               goto err_noreg;
> +       }
> +
> +       retval = request_irq(rtc->irq, lpc32xx_rtc_alarm_interrupt,
> +               IRQF_DISABLED, "rtcalarm", rtc);
> +       if (retval < 0) {
> +               dev_err(&pdev->dev, "Can't request interrupt\n");
> +               goto err_free_irq;
> +       }
> +
> +       return 0;
> +
> +err_free_irq:
> +       rtc_device_unregister(rtc->rtc);
> +err_noreg:
> +       iounmap(rtc->rtc_base);
> +err_noremap:
> +       release_resource(mem);
> +err_reqmem:
> +       kfree(rtc);
> +
> +       return retval;
> +}
> +
> +static int __devexit lpc32xx_rtc_remove(struct platform_device *pdev)
> +{
> +       struct lpc32xx_rtc *rtc = platform_get_drvdata(pdev);
> +
> +       free_irq(rtc->irq, pdev);
> +       rtc_device_unregister(rtc->rtc);
> +       iounmap(rtc->rtc_base);
> +       release_resource(dev_get_drvdata(&rtc->rtc->dev));
> +       kfree(rtc);
> +
> +       return 0;
> +}
> +
> +#ifdef CONFIG_PM
> +static int lpc32xx_rtc_suspend(struct device *dev)
> +{
> +       struct platform_device *pdev = to_platform_device(dev);
> +       struct lpc32xx_rtc *rtc = platform_get_drvdata(pdev);
> +
> +       if (device_may_wakeup(&pdev->dev))
> +               enable_irq_wake(rtc->irq);
> +       else
> +               disable_irq_wake(rtc->irq);
> +
> +       return 0;
> +}
> +
> +static int lpc32xx_rtc_resume(struct device *dev)
> +{
> +       struct platform_device *pdev = to_platform_device(dev);
> +       struct lpc32xx_rtc *rtc = platform_get_drvdata(pdev);
> +
> +       if (device_may_wakeup(&pdev->dev))
> +               disable_irq_wake(rtc->irq);
> +
> +       return 0;
> +}
> +
> +/* Unconditionally disable the alarm */
> +static int lpc32xx_rtc_freeze(struct device *dev)
> +{
> +       struct platform_device *pdev = to_platform_device(dev);
> +       struct lpc32xx_rtc *rtc = platform_get_drvdata(pdev);
> +
> +       spin_lock_irq(&rtc->lock);
> +
> +       rtc_writel(rtc, RTC_CTRL,
> +               rtc_readl(rtc, RTC_CTRL) & ~RTC_MATCH0_EN);
> +
> +       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)
> +               rtc_writel(rtc, RTC_CTRL,
> +                       rtc_readl(rtc, RTC_CTRL) | RTC_MATCH0_EN);
> +
> +       return 0;
> +}
> +
> +#else
> +#define lpc32xx_rtc_suspend NULL
> +#define lpc32xx_rtc_resume NULL
> +#define lpc32xx_rtc_freeze NULL
> +#define lpc32xx_rtc_thaw 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_thaw,
> +       .restore = lpc32xx_rtc_resume
> +};
> +
> +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,
> +       },
> +};
> +
> +static int __init lpc32xx_rtc_init(void)
> +{
> +       return platform_driver_register(&lpc32xx_rtc_driver);
> +}
> +
> +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 <wellsk40@gmail.com");
> +MODULE_DESCRIPTION("RTC driver for the LPC32xx SoC");
> +MODULE_LICENSE("GPL");
> --
> 1.7.1.1
>
> --
> You received this message because you are subscribed to "rtc-linux".
> Membership options at http://groups.google.com/group/rtc-linux .
> Please read http://groups.google.com/group/rtc-linux/web/checklist
> before submitting a driver.
Wolfram Sang Aug. 10, 2010, 10:25 a.m. UTC | #2
Hi Kevin,

driver seems to work fine here, a few comments though.

On Mon, Aug 09, 2010 at 09:17:29AM -0700, wellsk40@gmail.com wrote:
> From: Kevin Wells <wellsk40@gmail.com>
> 
> This patch contains the RTC driver for the built-in RTC in
> the LPC32XX SoC.
> 
> Signed-off-by: Kevin Wells <wellsk40@gmail.com>
> Signed-off-by: Durgesh Pattamatta <durgesh.pattamatta@nxp.com>
> ---
>  drivers/rtc/rtc-lpc32xx.c |  391 +++++++++++++++++++++++++++++++++++++++++++++
>  1 files changed, 391 insertions(+), 0 deletions(-)
>  create mode 100644 drivers/rtc/rtc-lpc32xx.c
> 
> diff --git a/drivers/rtc/rtc-lpc32xx.c b/drivers/rtc/rtc-lpc32xx.c
> new file mode 100644
> index 0000000..7803c68
> --- /dev/null
> +++ b/drivers/rtc/rtc-lpc32xx.c
> @@ -0,0 +1,391 @@
> +/*
> + * Copyright (C) 2010 NXP Semiconductors
> + *
> + * This program is free software; you can redistribute it and/or modify
> + * it under the terms of the GNU General Public License as published by
> + * the Free Software Foundation; either version 2 of the License, or
> + * (at your option) any later version.
> + *
> + *  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/kernel.h>
> +#include <linux/module.h>
> +#include <linux/init.h>
> +#include <linux/platform_device.h>
> +#include <linux/spinlock.h>
> +#include <linux/rtc.h>
> +#include <linux/slab.h>
> +#include <linux/io.h>
> +
> +/*
> + * Clock and Power control register offsets
> + */
> +#define RTC_UCOUNT		0x00
> +#define RTC_DCOUNT		0x04
> +#define RTC_MATCH0		0x08
> +#define RTC_MATCH1		0x0C
> +#define RTC_CTRL		0x10
> +#define RTC_INTSTAT		0x14
> +#define RTC_KEY			0x18
> +#define RTC_SRAM		0x80
> +
> +#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
> +
> +#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;
> +	unsigned int irq;
> +	int alarm_enabled;
> +	struct rtc_device *rtc;
> +	spinlock_t lock;
> +};
> +
> +static int lpc32xx_rtc_read_time(struct device *dev, struct rtc_time *time)
> +{
> +	unsigned long elapsed_sec;
> +	struct lpc32xx_rtc *rtc = dev_get_drvdata(dev);
> +
> +	elapsed_sec = rtc_readl(rtc, RTC_UCOUNT);
> +	rtc_time_to_tm(elapsed_sec, time);
> +
> +	return rtc_valid_tm(time);
> +}
> +
> +static int lpc32xx_rtc_set_mmss(struct device *dev, unsigned long secs)
> +{
> +	struct lpc32xx_rtc *rtc = dev_get_drvdata(dev);
> +	u32 tmp;
> +
> +	spin_lock_irq(&rtc->lock);
> +
> +	/* RTC must be disabled during count update */
> +	tmp = rtc_readl(rtc, RTC_CTRL);
> +	rtc_writel(rtc, RTC_CTRL, tmp | RTC_CNTR_DIS);
> +	rtc_writel(rtc, RTC_UCOUNT, secs);
> +	rtc_writel(rtc, RTC_DCOUNT, 0xFFFFFFFF - secs);
> +	rtc_writel(rtc, RTC_CTRL, tmp &= ~RTC_CNTR_DIS);
> +
> +	spin_unlock_irq(&rtc->lock);
> +
> +	return 0;
> +}
> +
> +static int lpc32xx_rtc_read_alarm(struct device *dev,
> +	struct rtc_wkalrm *wkalrm)
> +{
> +	struct lpc32xx_rtc *rtc = dev_get_drvdata(dev);
> +
> +	rtc_time_to_tm(rtc_readl(rtc, RTC_MATCH0), &wkalrm->time);
> +	wkalrm->enabled = rtc->alarm_enabled;
> +
> +	return rtc_valid_tm(&wkalrm->time);
> +}
> +
> +static int lpc32xx_rtc_set_alarm(struct device *dev,
> +	struct rtc_wkalrm *wkalrm)
> +{
> +	struct lpc32xx_rtc *rtc = dev_get_drvdata(dev);
> +	unsigned long alarmsecs;
> +	u32 tmp;
> +	int ret;

Empty line to seperate variables from the code

> +	ret = rtc_tm_to_time(&wkalrm->time, &alarmsecs);
> +	if (ret < 0) {
> +		dev_err(dev, "Failed to convert time: %d\n", ret);
> +		return ret;
> +	}
> +
> +	spin_lock_irq(&rtc->lock);
> +
> +	/* Disable alarm during update */
> +	tmp = rtc_readl(rtc, RTC_CTRL);
> +	rtc_writel(rtc, RTC_CTRL, tmp & ~RTC_MATCH0_EN);
> +
> +	rtc->alarm_enabled = wkalrm->enabled = 1;
> +	if (wkalrm->enabled) {
> +		rtc_writel(rtc, RTC_MATCH0, alarmsecs);
> +		rtc_writel(rtc, RTC_INTSTAT, RTC_MATCH0_INT_STS);
> +		rtc_writel(rtc, RTC_CTRL, tmp | RTC_MATCH0_EN);
> +	}
> +
> +	spin_unlock_irq(&rtc->lock);
> +
> +	return 0;
> +}
> +
> +static int lpc32xx_rtc_alarm_irq_enable(struct device *dev,
> +	unsigned int enabled)
> +{
> +	struct lpc32xx_rtc *rtc = dev_get_drvdata(dev);
> +	u32 tmp;
> +
> +	spin_lock_irq(&rtc->lock);
> +	rtc->alarm_enabled = (int) enabled;
> +	tmp = rtc_readl(rtc, RTC_CTRL);
> +
> +	if (enabled)
> +		tmp |= RTC_MATCH0_EN;
> +	else
> +		tmp &= ~RTC_MATCH0_EN;
> +
> +	rtc_writel(rtc, RTC_CTRL, tmp);
> +	spin_unlock_irq(&rtc->lock);
> +
> +	return 0;
> +}
> +
> +static irqreturn_t lpc32xx_rtc_alarm_interrupt(int irq, void *dev)
> +{
> +	struct lpc32xx_rtc *rtc = (struct lpc32xx_rtc *) dev;

You can drop this cast.

> +
> +	spin_lock(&rtc->lock);
> +
> +	/* Disable alarm interrupt */
> +	rtc_writel(rtc, RTC_CTRL,
> +		rtc_readl(rtc, RTC_CTRL) & ~RTC_MATCH0_EN);
> +	rtc->alarm_enabled = 0;
> +
> +	/*
> +	 * Write a large value to the match value so the RTC won't
> +	 * keep firing the match status
> +	 */
> +	rtc_writel(rtc, RTC_MATCH0, 0xFFFFFFFF);
> +	rtc_writel(rtc, RTC_INTSTAT, RTC_MATCH0_INT_STS);
> +
> +	spin_unlock(&rtc->lock);
> +
> +	rtc_update_irq(rtc->rtc, 1, RTC_IRQF | RTC_AF);
> +
> +	return IRQ_HANDLED;
> +}
> +
> +static const struct rtc_class_ops lpc32xx_rtc_ops = {
> +	.read_time		= lpc32xx_rtc_read_time,
> +	.set_mmss		= lpc32xx_rtc_set_mmss,
> +	.read_alarm		= lpc32xx_rtc_read_alarm,
> +	.set_alarm		= lpc32xx_rtc_set_alarm,
> +	.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 *rtc = NULL;
> +	int rtcirq, retval;
> +	u32 tmp;
> +
> +	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
> +	if (!res) {
> +		dev_err(&pdev->dev, "Can't get memory resource\n");
> +		return -ENOENT;
> +	}
> +
> +	rtcirq = platform_get_irq(pdev, 0);
> +	if ((rtcirq < 0) || (rtcirq >= NR_IRQS)) {
> +		dev_err(&pdev->dev, "Can't get interrupt resource\n");
> +		return -ENOENT;
> +	}
> +
> +	rtc = kzalloc(sizeof(struct lpc32xx_rtc), GFP_KERNEL);
> +	if (unlikely(!rtc)) {
> +		dev_err(&pdev->dev, "Can't allocate memory\n");
> +		return -ENOMEM;
> +	}
> +	rtc->irq = rtcirq;
> +
> +	mem = request_mem_region(res->start, resource_size(res), pdev->name);
> +	if (!mem) {
> +		dev_err(&pdev->dev, "RTC registers are not free\n");
> +		retval = -EBUSY;
> +		goto err_reqmem;
> +	}
> +
> +	rtc->rtc_base = ioremap(res->start, res->end - res->start + 1);

How about using managed resources (devm_*)? They usually simplify the
error paths and make the probe more readable.

> +	if (!rtc->rtc_base) {
> +		dev_err(&pdev->dev, "Can't map memory\n");
> +		retval = -EIO;
> +		goto err_noremap;
> +	}
> +
> +	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.
> +	 */
> +	if (rtc_readl(rtc, RTC_KEY) == RTC_KEY_ONSW_LOADVAL) {
> +		tmp = rtc_readl(rtc, RTC_CTRL);
> +		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);
> +		rtc_writel(rtc, RTC_CTRL, tmp);
> +
> +		/* Clear latched interrupt states */
> +		rtc_writel(rtc, RTC_MATCH0, 0xFFFFFFFF);
> +		rtc_writel(rtc, RTC_INTSTAT, RTC_MATCH0_INT_STS |
> +			RTC_MATCH1_INT_STS | RTC_ONSW_INT_STS);
> +
> +		/* Write key value to RTC so it won't reload on reset */
> +		rtc_writel(rtc, RTC_KEY, RTC_KEY_ONSW_LOADVAL);
> +	} else if (rtc_readl(rtc, RTC_CTRL) & RTC_MATCH0_EN)
> +		rtc->alarm_enabled = 1;
> +
> +	platform_set_drvdata(pdev, rtc);
> +
> +	device_init_wakeup(&pdev->dev, 1);
> +	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(rtc->rtc);
> +		goto err_noreg;
> +	}
> +
> +	retval = request_irq(rtc->irq, lpc32xx_rtc_alarm_interrupt,
> +		IRQF_DISABLED, "rtcalarm", rtc);
> +	if (retval < 0) {
> +		dev_err(&pdev->dev, "Can't request interrupt\n");
> +		goto err_free_irq;
> +	}

I saw that a number of rtc-drivers register their irq after they
register the device. I wonder if this is OK here? Couldn't it happen
that after rtc_device_register() there is a preemption and another
process could set the alarm? Then there is a race between interrupts
already enabled and no handler available, no?

> +
> +	return 0;
> +
> +err_free_irq:
> +	rtc_device_unregister(rtc->rtc);
> +err_noreg:
> +	iounmap(rtc->rtc_base);
> +err_noremap:
> +	release_resource(mem);
> +err_reqmem:
> +	kfree(rtc);
> +
> +	return retval;
> +}
> +
> +static int __devexit lpc32xx_rtc_remove(struct platform_device *pdev)
> +{
> +	struct lpc32xx_rtc *rtc = platform_get_drvdata(pdev);
> +
> +	free_irq(rtc->irq, pdev);
> +	rtc_device_unregister(rtc->rtc);
> +	iounmap(rtc->rtc_base);
> +	release_resource(dev_get_drvdata(&rtc->rtc->dev));
> +	kfree(rtc);
> +
> +	return 0;
> +}
> +
> +#ifdef CONFIG_PM
> +static int lpc32xx_rtc_suspend(struct device *dev)
> +{
> +	struct platform_device *pdev = to_platform_device(dev);
> +	struct lpc32xx_rtc *rtc = platform_get_drvdata(pdev);
> +
> +	if (device_may_wakeup(&pdev->dev))
> +		enable_irq_wake(rtc->irq);
> +	else
> +		disable_irq_wake(rtc->irq);
> +
> +	return 0;
> +}
> +
> +static int lpc32xx_rtc_resume(struct device *dev)
> +{
> +	struct platform_device *pdev = to_platform_device(dev);
> +	struct lpc32xx_rtc *rtc = platform_get_drvdata(pdev);
> +
> +	if (device_may_wakeup(&pdev->dev))
> +		disable_irq_wake(rtc->irq);
> +
> +	return 0;
> +}
> +
> +/* Unconditionally disable the alarm */
> +static int lpc32xx_rtc_freeze(struct device *dev)
> +{
> +	struct platform_device *pdev = to_platform_device(dev);
> +	struct lpc32xx_rtc *rtc = platform_get_drvdata(pdev);
> +
> +	spin_lock_irq(&rtc->lock);
> +
> +	rtc_writel(rtc, RTC_CTRL,
> +		rtc_readl(rtc, RTC_CTRL) & ~RTC_MATCH0_EN);
> +
> +	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)
> +		rtc_writel(rtc, RTC_CTRL,
> +			rtc_readl(rtc, RTC_CTRL) | RTC_MATCH0_EN);
> +
> +	return 0;
> +}
> +
> +#else
> +#define lpc32xx_rtc_suspend NULL
> +#define lpc32xx_rtc_resume NULL
> +#define lpc32xx_rtc_freeze NULL
> +#define lpc32xx_rtc_thaw 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_thaw,
> +	.restore = lpc32xx_rtc_resume
> +};
> +
> +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,
> +	},
> +};
> +
> +static int __init lpc32xx_rtc_init(void)
> +{
> +	return platform_driver_register(&lpc32xx_rtc_driver);
> +}
> +
> +static void __exit lpc32xx_rtc_exit(void)
> +{
> +	platform_driver_unregister(&lpc32xx_rtc_driver);
> +}
> +
> +module_init(lpc32xx_rtc_init);
> +module_exit(lpc32xx_rtc_exit);

Minor nitpick: The trend seems to be to put those lines directly after
the function.

> +
> +MODULE_AUTHOR("Kevin Wells <wellsk40@gmail.com");
> +MODULE_DESCRIPTION("RTC driver for the LPC32xx SoC");
> +MODULE_LICENSE("GPL");
> -- 
> 1.7.1.1
> 
> -- 
> You received this message because you are subscribed to "rtc-linux".
> Membership options at http://groups.google.com/group/rtc-linux .
> Please read http://groups.google.com/group/rtc-linux/web/checklist
> before submitting a driver.

Regards,

   Wolfram
Uwe Kleine-König Aug. 10, 2010, noon UTC | #3
Hi Wolfram,

> > +	retval = request_irq(rtc->irq, lpc32xx_rtc_alarm_interrupt,
> > +		IRQF_DISABLED, "rtcalarm", rtc);
> > +	if (retval < 0) {
> > +		dev_err(&pdev->dev, "Can't request interrupt\n");
> > +		goto err_free_irq;
> > +	}
> 
> I saw that a number of rtc-drivers register their irq after they
> register the device. I wonder if this is OK here? Couldn't it happen
> that after rtc_device_register() there is a preemption and another
> process could set the alarm? Then there is a race between interrupts
> already enabled and no handler available, no?
If you do it the other way around the irq might trigger and the handler
reports an irq for a device that doesn't exist yet.
 
Best regards
Uwe
Alessandro Zummo Aug. 10, 2010, 1:34 p.m. UTC | #4
On Mon,  9 Aug 2010 09:17:29 -0700
wellsk40@gmail.com wrote:

> +
> +	retval = request_irq(rtc->irq, lpc32xx_rtc_alarm_interrupt,
> +		IRQF_DISABLED, "rtcalarm", rtc);
> +	if (retval < 0) {
> +		dev_err(&pdev->dev, "Can't request interrupt\n");
> +		goto err_free_irq;
> +	}

 if the driver can work without the alarm irq you
 can convert this to a non fatal warning.

 having an rtc without alarms is much better than having none.
Wolfram Sang Aug. 10, 2010, 1:40 p.m. UTC | #5
On Tue, Aug 10, 2010 at 02:00:55PM +0200, Uwe Kleine-König wrote:
> Hi Wolfram,
> 
> > > +	retval = request_irq(rtc->irq, lpc32xx_rtc_alarm_interrupt,
> > > +		IRQF_DISABLED, "rtcalarm", rtc);
> > > +	if (retval < 0) {
> > > +		dev_err(&pdev->dev, "Can't request interrupt\n");
> > > +		goto err_free_irq;
> > > +	}
> > 
> > I saw that a number of rtc-drivers register their irq after they
> > register the device. I wonder if this is OK here? Couldn't it happen
> > that after rtc_device_register() there is a preemption and another
> > process could set the alarm? Then there is a race between interrupts
> > already enabled and no handler available, no?
> If you do it the other way around the irq might trigger and the handler
> reports an irq for a device that doesn't exist yet.

Well, I was assuming that you initially have all interrupts disabled...
Kevin Wells Aug. 10, 2010, 7:08 p.m. UTC | #6
Hi Wolfram,

Thanks for helping review this.
I'll get your suggestions installed and an update posted in a few days.

> > +
> > +	device_init_wakeup(&pdev->dev, 1);
> > +	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(rtc->rtc);
> > +		goto err_noreg;
> > +	}
> > +
> > +	retval = request_irq(rtc->irq, lpc32xx_rtc_alarm_interrupt,
> > +		IRQF_DISABLED, "rtcalarm", rtc);
> > +	if (retval < 0) {
> > +		dev_err(&pdev->dev, "Can't request interrupt\n");
> > +		goto err_free_irq;
> > +	}
> 
> I saw that a number of rtc-drivers register their irq after they register the
> device. I wonder if this is OK here? Couldn't it happen that after
> rtc_device_register() there is a preemption and another process could set the
> alarm? Then there is a race between interrupts already enabled and no handler
> available, no?
> 

The 32xx RTC registers can save states across power or reset
cycles and are only initialized if they have never been previously
initialized. When waking up from suspend, power on, or reset - the
alarm IRQ may have fired and may be enabled from a previous
initialization, so the RTC device needs to be registered first.

You bring up a good point though, I wonder if going through probe
requires that the alarm be disabled (ie, suspend won't go via probe,
but a hard system reset will). I'll review this a bit more.
Kevin Wells Aug. 10, 2010, 7:08 p.m. UTC | #7
Hi Zongshun,

Thanks for helping review this.

> Subject: Re: [rtc-linux] [PATCH 1/2] rtc: rtc-lpc32xx: Introduce RTC driver
> for the LPC32XX SoC
> 
> Hi Kevin ,
> 
> This is really a natty rtc patch:).

From the urban dictionary...
"natty"
1) originating from rastafarian culture. meaning 'good','cool' and
most importantly, 'elite'.
2) A slang for Natural Light beer, a favorite of poor college students
the world over.

I hope it's the first one, but I guess the 2nd isn't that bad if
you’re a college student o)
I'll merge both patches into a single patch for v2 and repost in a
few days.

> > +       /* Disable alarm during update */
> > +       tmp = rtc_readl(rtc, RTC_CTRL);
> > +       rtc_writel(rtc, RTC_CTRL, tmp & ~RTC_MATCH0_EN);
> > +
> > +       rtc->alarm_enabled = wkalrm->enabled = 1;
> > +       if (wkalrm->enabled) {
> > +               rtc_writel(rtc, RTC_MATCH0, alarmsecs);
> > +               rtc_writel(rtc, RTC_INTSTAT, RTC_MATCH0_INT_STS);
> > +               rtc_writel(rtc, RTC_CTRL, tmp | RTC_MATCH0_EN);
> > +       }
> 
> I think this 'wkalrm->enabled ' will always be set '1', so 'if'
> condition will be alway true?

Thanks for catching this. The '= 1' assignment shouldn't be there.
Kevin Wells Aug. 10, 2010, 7:08 p.m. UTC | #8
Hi Alessandro,

Thanks for helping to review this.

> 
> > +
> > +	retval = request_irq(rtc->irq, lpc32xx_rtc_alarm_interrupt,
> > +		IRQF_DISABLED, "rtcalarm", rtc);
> > +	if (retval < 0) {
> > +		dev_err(&pdev->dev, "Can't request interrupt\n");
> > +		goto err_free_irq;
> > +	}
> 
>  if the driver can work without the alarm irq you
>  can convert this to a non fatal warning.
> 
>  having an rtc without alarms is much better than having none.
> 

Good suggestion. I'll change this.

> --
> 
>  Best regards,
> 
>  Alessandro Zummo,
>   Tower Technologies - Torino, Italy
> 
>   http://www.towertech.it
>
Wan ZongShun Aug. 11, 2010, 1:40 a.m. UTC | #9
2010/8/11 Kevin Wells <kevin.wells@nxp.com>:
> Hi Zongshun,
>
> Thanks for helping review this.
>
>> Subject: Re: [rtc-linux] [PATCH 1/2] rtc: rtc-lpc32xx: Introduce RTC driver
>> for the LPC32XX SoC
>>
>> Hi Kevin ,
>>
>> This is really a natty rtc patch:).
>
> From the urban dictionary...
> "natty"
> 1) originating from rastafarian culture. meaning 'good','cool' and
> most importantly, 'elite'.
> 2) A slang for Natural Light beer, a favorite of poor college students
> the world over.
>

Hmm,I means the first, this patch looks very good to me. :)

> I hope it's the first one, but I guess the 2nd isn't that bad if
> you’re a college student o)
> I'll merge both patches into a single patch for v2 and repost in a
> few days.
>
>> > +       /* Disable alarm during update */
>> > +       tmp = rtc_readl(rtc, RTC_CTRL);
>> > +       rtc_writel(rtc, RTC_CTRL, tmp & ~RTC_MATCH0_EN);
>> > +
>> > +       rtc->alarm_enabled = wkalrm->enabled = 1;
>> > +       if (wkalrm->enabled) {
>> > +               rtc_writel(rtc, RTC_MATCH0, alarmsecs);
>> > +               rtc_writel(rtc, RTC_INTSTAT, RTC_MATCH0_INT_STS);
>> > +               rtc_writel(rtc, RTC_CTRL, tmp | RTC_MATCH0_EN);
>> > +       }
>>
>> I think this 'wkalrm->enabled ' will always be set '1', so 'if'
>> condition will be alway true?
>
> Thanks for catching this. The '= 1' assignment shouldn't be there.
>
diff mbox

Patch

diff --git a/drivers/rtc/rtc-lpc32xx.c b/drivers/rtc/rtc-lpc32xx.c
new file mode 100644
index 0000000..7803c68
--- /dev/null
+++ b/drivers/rtc/rtc-lpc32xx.c
@@ -0,0 +1,391 @@ 
+/*
+ * Copyright (C) 2010 NXP Semiconductors
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ *  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/kernel.h>
+#include <linux/module.h>
+#include <linux/init.h>
+#include <linux/platform_device.h>
+#include <linux/spinlock.h>
+#include <linux/rtc.h>
+#include <linux/slab.h>
+#include <linux/io.h>
+
+/*
+ * Clock and Power control register offsets
+ */
+#define RTC_UCOUNT		0x00
+#define RTC_DCOUNT		0x04
+#define RTC_MATCH0		0x08
+#define RTC_MATCH1		0x0C
+#define RTC_CTRL		0x10
+#define RTC_INTSTAT		0x14
+#define RTC_KEY			0x18
+#define RTC_SRAM		0x80
+
+#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
+
+#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;
+	unsigned int irq;
+	int alarm_enabled;
+	struct rtc_device *rtc;
+	spinlock_t lock;
+};
+
+static int lpc32xx_rtc_read_time(struct device *dev, struct rtc_time *time)
+{
+	unsigned long elapsed_sec;
+	struct lpc32xx_rtc *rtc = dev_get_drvdata(dev);
+
+	elapsed_sec = rtc_readl(rtc, RTC_UCOUNT);
+	rtc_time_to_tm(elapsed_sec, time);
+
+	return rtc_valid_tm(time);
+}
+
+static int lpc32xx_rtc_set_mmss(struct device *dev, unsigned long secs)
+{
+	struct lpc32xx_rtc *rtc = dev_get_drvdata(dev);
+	u32 tmp;
+
+	spin_lock_irq(&rtc->lock);
+
+	/* RTC must be disabled during count update */
+	tmp = rtc_readl(rtc, RTC_CTRL);
+	rtc_writel(rtc, RTC_CTRL, tmp | RTC_CNTR_DIS);
+	rtc_writel(rtc, RTC_UCOUNT, secs);
+	rtc_writel(rtc, RTC_DCOUNT, 0xFFFFFFFF - secs);
+	rtc_writel(rtc, RTC_CTRL, tmp &= ~RTC_CNTR_DIS);
+
+	spin_unlock_irq(&rtc->lock);
+
+	return 0;
+}
+
+static int lpc32xx_rtc_read_alarm(struct device *dev,
+	struct rtc_wkalrm *wkalrm)
+{
+	struct lpc32xx_rtc *rtc = dev_get_drvdata(dev);
+
+	rtc_time_to_tm(rtc_readl(rtc, RTC_MATCH0), &wkalrm->time);
+	wkalrm->enabled = rtc->alarm_enabled;
+
+	return rtc_valid_tm(&wkalrm->time);
+}
+
+static int lpc32xx_rtc_set_alarm(struct device *dev,
+	struct rtc_wkalrm *wkalrm)
+{
+	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);
+		return ret;
+	}
+
+	spin_lock_irq(&rtc->lock);
+
+	/* Disable alarm during update */
+	tmp = rtc_readl(rtc, RTC_CTRL);
+	rtc_writel(rtc, RTC_CTRL, tmp & ~RTC_MATCH0_EN);
+
+	rtc->alarm_enabled = wkalrm->enabled = 1;
+	if (wkalrm->enabled) {
+		rtc_writel(rtc, RTC_MATCH0, alarmsecs);
+		rtc_writel(rtc, RTC_INTSTAT, RTC_MATCH0_INT_STS);
+		rtc_writel(rtc, RTC_CTRL, tmp | RTC_MATCH0_EN);
+	}
+
+	spin_unlock_irq(&rtc->lock);
+
+	return 0;
+}
+
+static int lpc32xx_rtc_alarm_irq_enable(struct device *dev,
+	unsigned int enabled)
+{
+	struct lpc32xx_rtc *rtc = dev_get_drvdata(dev);
+	u32 tmp;
+
+	spin_lock_irq(&rtc->lock);
+	rtc->alarm_enabled = (int) enabled;
+	tmp = rtc_readl(rtc, RTC_CTRL);
+
+	if (enabled)
+		tmp |= RTC_MATCH0_EN;
+	else
+		tmp &= ~RTC_MATCH0_EN;
+
+	rtc_writel(rtc, RTC_CTRL, tmp);
+	spin_unlock_irq(&rtc->lock);
+
+	return 0;
+}
+
+static irqreturn_t lpc32xx_rtc_alarm_interrupt(int irq, void *dev)
+{
+	struct lpc32xx_rtc *rtc = (struct lpc32xx_rtc *) dev;
+
+	spin_lock(&rtc->lock);
+
+	/* Disable alarm interrupt */
+	rtc_writel(rtc, RTC_CTRL,
+		rtc_readl(rtc, RTC_CTRL) & ~RTC_MATCH0_EN);
+	rtc->alarm_enabled = 0;
+
+	/*
+	 * Write a large value to the match value so the RTC won't
+	 * keep firing the match status
+	 */
+	rtc_writel(rtc, RTC_MATCH0, 0xFFFFFFFF);
+	rtc_writel(rtc, RTC_INTSTAT, RTC_MATCH0_INT_STS);
+
+	spin_unlock(&rtc->lock);
+
+	rtc_update_irq(rtc->rtc, 1, RTC_IRQF | RTC_AF);
+
+	return IRQ_HANDLED;
+}
+
+static const struct rtc_class_ops lpc32xx_rtc_ops = {
+	.read_time		= lpc32xx_rtc_read_time,
+	.set_mmss		= lpc32xx_rtc_set_mmss,
+	.read_alarm		= lpc32xx_rtc_read_alarm,
+	.set_alarm		= lpc32xx_rtc_set_alarm,
+	.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 *rtc = NULL;
+	int rtcirq, retval;
+	u32 tmp;
+
+	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	if (!res) {
+		dev_err(&pdev->dev, "Can't get memory resource\n");
+		return -ENOENT;
+	}
+
+	rtcirq = platform_get_irq(pdev, 0);
+	if ((rtcirq < 0) || (rtcirq >= NR_IRQS)) {
+		dev_err(&pdev->dev, "Can't get interrupt resource\n");
+		return -ENOENT;
+	}
+
+	rtc = kzalloc(sizeof(struct lpc32xx_rtc), GFP_KERNEL);
+	if (unlikely(!rtc)) {
+		dev_err(&pdev->dev, "Can't allocate memory\n");
+		return -ENOMEM;
+	}
+	rtc->irq = rtcirq;
+
+	mem = request_mem_region(res->start, resource_size(res), pdev->name);
+	if (!mem) {
+		dev_err(&pdev->dev, "RTC registers are not free\n");
+		retval = -EBUSY;
+		goto err_reqmem;
+	}
+
+	rtc->rtc_base = ioremap(res->start, res->end - res->start + 1);
+	if (!rtc->rtc_base) {
+		dev_err(&pdev->dev, "Can't map memory\n");
+		retval = -EIO;
+		goto err_noremap;
+	}
+
+	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.
+	 */
+	if (rtc_readl(rtc, RTC_KEY) == RTC_KEY_ONSW_LOADVAL) {
+		tmp = rtc_readl(rtc, RTC_CTRL);
+		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);
+		rtc_writel(rtc, RTC_CTRL, tmp);
+
+		/* Clear latched interrupt states */
+		rtc_writel(rtc, RTC_MATCH0, 0xFFFFFFFF);
+		rtc_writel(rtc, RTC_INTSTAT, RTC_MATCH0_INT_STS |
+			RTC_MATCH1_INT_STS | RTC_ONSW_INT_STS);
+
+		/* Write key value to RTC so it won't reload on reset */
+		rtc_writel(rtc, RTC_KEY, RTC_KEY_ONSW_LOADVAL);
+	} else if (rtc_readl(rtc, RTC_CTRL) & RTC_MATCH0_EN)
+		rtc->alarm_enabled = 1;
+
+	platform_set_drvdata(pdev, rtc);
+
+	device_init_wakeup(&pdev->dev, 1);
+	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(rtc->rtc);
+		goto err_noreg;
+	}
+
+	retval = request_irq(rtc->irq, lpc32xx_rtc_alarm_interrupt,
+		IRQF_DISABLED, "rtcalarm", rtc);
+	if (retval < 0) {
+		dev_err(&pdev->dev, "Can't request interrupt\n");
+		goto err_free_irq;
+	}
+
+	return 0;
+
+err_free_irq:
+	rtc_device_unregister(rtc->rtc);
+err_noreg:
+	iounmap(rtc->rtc_base);
+err_noremap:
+	release_resource(mem);
+err_reqmem:
+	kfree(rtc);
+
+	return retval;
+}
+
+static int __devexit lpc32xx_rtc_remove(struct platform_device *pdev)
+{
+	struct lpc32xx_rtc *rtc = platform_get_drvdata(pdev);
+
+	free_irq(rtc->irq, pdev);
+	rtc_device_unregister(rtc->rtc);
+	iounmap(rtc->rtc_base);
+	release_resource(dev_get_drvdata(&rtc->rtc->dev));
+	kfree(rtc);
+
+	return 0;
+}
+
+#ifdef CONFIG_PM
+static int lpc32xx_rtc_suspend(struct device *dev)
+{
+	struct platform_device *pdev = to_platform_device(dev);
+	struct lpc32xx_rtc *rtc = platform_get_drvdata(pdev);
+
+	if (device_may_wakeup(&pdev->dev))
+		enable_irq_wake(rtc->irq);
+	else
+		disable_irq_wake(rtc->irq);
+
+	return 0;
+}
+
+static int lpc32xx_rtc_resume(struct device *dev)
+{
+	struct platform_device *pdev = to_platform_device(dev);
+	struct lpc32xx_rtc *rtc = platform_get_drvdata(pdev);
+
+	if (device_may_wakeup(&pdev->dev))
+		disable_irq_wake(rtc->irq);
+
+	return 0;
+}
+
+/* Unconditionally disable the alarm */
+static int lpc32xx_rtc_freeze(struct device *dev)
+{
+	struct platform_device *pdev = to_platform_device(dev);
+	struct lpc32xx_rtc *rtc = platform_get_drvdata(pdev);
+
+	spin_lock_irq(&rtc->lock);
+
+	rtc_writel(rtc, RTC_CTRL,
+		rtc_readl(rtc, RTC_CTRL) & ~RTC_MATCH0_EN);
+
+	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)
+		rtc_writel(rtc, RTC_CTRL,
+			rtc_readl(rtc, RTC_CTRL) | RTC_MATCH0_EN);
+
+	return 0;
+}
+
+#else
+#define lpc32xx_rtc_suspend NULL
+#define lpc32xx_rtc_resume NULL
+#define lpc32xx_rtc_freeze NULL
+#define lpc32xx_rtc_thaw 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_thaw,
+	.restore = lpc32xx_rtc_resume
+};
+
+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,
+	},
+};
+
+static int __init lpc32xx_rtc_init(void)
+{
+	return platform_driver_register(&lpc32xx_rtc_driver);
+}
+
+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 <wellsk40@gmail.com");
+MODULE_DESCRIPTION("RTC driver for the LPC32xx SoC");
+MODULE_LICENSE("GPL");