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

From: Wan ZongShun
Date: Mon Aug 09 2010 - 21:55:24 EST


Hi Kevin ,

This is really a natty rtc patch:).


2010/8/10 <wellsk40@xxxxxxxxx>:
> From: Kevin Wells <wellsk40@xxxxxxxxx>
>
> This patch contains the RTC driver for the built-in RTC in
> the LPC32XX SoC.
>
> Signed-off-by: Kevin Wells <wellsk40@xxxxxxxxx>
> Signed-off-by: Durgesh Pattamatta <durgesh.pattamatta@xxxxxxx>
> ---
> Â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@xxxxxxxxx");
> +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.



--
*linux-arm-kernel mailing list
mail addr:linux-arm-kernel@xxxxxxxxxxxxxxxxxxx
you can subscribe by:
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

* linux-arm-NUC900 mailing list
mail addr:NUC900@xxxxxxxxxxxxxxxx
main web: https://groups.google.com/group/NUC900
you can subscribe it by sending me mail:
mcuos.com@xxxxxxxxx
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/