Re: [rtc-linux] [RFC][PATCH 15/26] RTC: Add JZ4740 RTC driver

From: Wan ZongShun
Date: Sat Jun 05 2010 - 11:48:25 EST


2010/6/3 Lars-Peter Clausen <lars@xxxxxxxxxx>:
> This patch adds support for the RTC unit on JZ4740 SoCs.
>
> Signed-off-by: Lars-Peter Clausen <lars@xxxxxxxxxx>
> Cc: Alessandro Zummo <a.zummo@xxxxxxxxxxxx>
> Cc: Paul Gortmaker <p_gortmaker@xxxxxxxxx>
> Cc: rtc-linux@xxxxxxxxxxxxxxxx
> ---
> Âdrivers/rtc/Kconfig   Â|  11 ++
> Âdrivers/rtc/Makefile   |  Â1 +
> Âdrivers/rtc/rtc-jz4740.c | Â344 ++++++++++++++++++++++++++++++++++++++++++++++
> Â3 files changed, 356 insertions(+), 0 deletions(-)
> Âcreate mode 100644 drivers/rtc/rtc-jz4740.c
>
> diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig
> index 10ba12c..d0ed7e6 100644
> --- a/drivers/rtc/Kconfig
> +++ b/drivers/rtc/Kconfig
> @@ -905,4 +905,15 @@ config RTC_DRV_MPC5121
> Â Â Â Â ÂThis driver can also be built as a module. If so, the module
> Â Â Â Â Âwill be called rtc-mpc5121.
>
> +config RTC_DRV_JZ4740
> + Â Â Â tristate "Ingenic JZ4740 SoC"
> + Â Â Â depends on RTC_CLASS
> + Â Â Â depends on MACH_JZ4740
> + Â Â Â help
> + Â Â Â Â If you say yes here you get support for the Ingenic JZ4740 SoC RTC
> + Â Â Â Â controller.
> +
> + Â Â Â Â This driver can also be buillt as a module. If so, the module
> + Â Â Â Â will be called rtc-jz4740.
> +
> Âendif # RTC_CLASS
> diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile
> index 5adbba7..fedf9bb 100644
> --- a/drivers/rtc/Makefile
> +++ b/drivers/rtc/Makefile
> @@ -47,6 +47,7 @@ obj-$(CONFIG_RTC_DRV_EP93XX) Â+= rtc-ep93xx.o
> Âobj-$(CONFIG_RTC_DRV_FM3130) Â += rtc-fm3130.o
> Âobj-$(CONFIG_RTC_DRV_GENERIC) Â+= rtc-generic.o
> Âobj-$(CONFIG_RTC_DRV_ISL1208) Â+= rtc-isl1208.o
> +obj-$(CONFIG_RTC_DRV_JZ4740) Â += rtc-jz4740.o
> Âobj-$(CONFIG_RTC_DRV_M41T80) Â += rtc-m41t80.o
> Âobj-$(CONFIG_RTC_DRV_M41T94) Â += rtc-m41t94.o
> Âobj-$(CONFIG_RTC_DRV_M48T35) Â += rtc-m48t35.o
> diff --git a/drivers/rtc/rtc-jz4740.c b/drivers/rtc/rtc-jz4740.c
> new file mode 100644
> index 0000000..41ab78f
> --- /dev/null
> +++ b/drivers/rtc/rtc-jz4740.c
> @@ -0,0 +1,344 @@
> +/*
> + * ÂCopyright (C) 2009-2010, Lars-Peter Clausen <lars@xxxxxxxxxx>
> + * Â Â Â Â Â Â JZ4740 SoC RTC driver
> + *
> + * Â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/platform_device.h>
> +#include <linux/rtc.h>
> +#include <linux/slab.h>
> +#include <linux/spinlock.h>
> +
> +#define JZ_REG_RTC_CTRL Â Â Â Â Â Â Â Â0x00
> +#define JZ_REG_RTC_SEC Â Â Â Â 0x04
> +#define JZ_REG_RTC_SEC_ALARM Â 0x08
> +#define JZ_REG_RTC_REGULATOR Â 0x0C
> +#define JZ_REG_RTC_HIBERNATE Â 0x20
> +#define JZ_REG_RTC_SCRATCHPAD Â0x34
> +
> +#define JZ_RTC_CTRL_WRDY Â Â Â BIT(7)
> +#define JZ_RTC_CTRL_1HZ Â Â Â Â Â Â Â ÂBIT(6)
> +#define JZ_RTC_CTRL_1HZ_IRQ Â ÂBIT(5)
> +#define JZ_RTC_CTRL_AF Â Â Â Â BIT(4)
> +#define JZ_RTC_CTRL_AF_IRQ Â Â BIT(3)
> +#define JZ_RTC_CTRL_AE Â Â Â Â BIT(2)
> +#define JZ_RTC_CTRL_ENABLE Â Â BIT(0)
> +
> +struct jz4740_rtc {
> + Â Â Â struct resource *mem;
> + Â Â Â void __iomem *base;
> +
> + Â Â Â struct rtc_device *rtc;
> +
> + Â Â Â unsigned int irq;
> +
> + Â Â Â spinlock_t lock;
> +};
> +
> +static inline uint32_t jz4740_rtc_reg_read(struct jz4740_rtc *rtc, size_t reg)
> +{
> + Â Â Â return readl(rtc->base + reg);
> +}
> +
> +static inline void jz4740_rtc_wait_write_ready(struct jz4740_rtc *rtc)
> +{
> + Â Â Â uint32_t ctrl;
> + Â Â Â do {
> + Â Â Â Â Â Â Â ctrl = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_CTRL);
> + Â Â Â } while (!(ctrl & JZ_RTC_CTRL_WRDY));
> +}
> +
> +
> +static inline void jz4740_rtc_reg_write(struct jz4740_rtc *rtc, size_t reg,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â uint32_t val)
> +{
> + Â Â Â jz4740_rtc_wait_write_ready(rtc);
> + Â Â Â writel(val, rtc->base + reg);
> +}
> +
> +static void jz4740_rtc_ctrl_set_bits(struct jz4740_rtc *rtc, uint32_t mask,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â uint32_t val)
> +{
> + Â Â Â unsigned long flags;
> + Â Â Â uint32_t ctrl;
> +
> + Â Â Â spin_lock_irqsave(&rtc->lock, flags);
> +
> + Â Â Â ctrl = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_CTRL);
> +
> + Â Â Â /* Don't clear interrupt flags by accident */
> + Â Â Â ctrl |= JZ_RTC_CTRL_1HZ | JZ_RTC_CTRL_AF;
> +
> + Â Â Â ctrl &= ~mask;
> + Â Â Â ctrl |= val;
> +
> + Â Â Â jz4740_rtc_reg_write(rtc, JZ_REG_RTC_CTRL, ctrl);
> +
> + Â Â Â spin_unlock_irqrestore(&rtc->lock, flags);
> +}
> +
> +static inline struct jz4740_rtc *dev_to_rtc(struct device *dev)
> +{
> + Â Â Â return dev_get_drvdata(dev);
> +}
> +

Why do you need to re-implement the 'dev_to_rtc' instead of using
'platform_get_drvdata' provided by Linux own.'

> +static int jz4740_rtc_read_time(struct device *dev, struct rtc_time *time)
> +{
> + Â Â Â struct jz4740_rtc *rtc = dev_to_rtc(dev);
> + Â Â Â uint32_t secs, secs2;
> +
> + Â Â Â secs = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_SEC);
> + Â Â Â secs2 = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_SEC);
> +
> + Â Â Â while (secs != secs2) {
> + Â Â Â Â Â Â Â secs = secs2;
> + Â Â Â Â Â Â Â secs2 = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_SEC);
> + Â Â Â }
> +
> + Â Â Â rtc_time_to_tm(secs, time);
> +
> + Â Â Â return rtc_valid_tm(time);
> +}
> +
> +static int jz4740_rtc_set_mmss(struct device *dev, unsigned long secs)
> +{
> + Â Â Â struct jz4740_rtc *rtc = dev_to_rtc(dev);
> +
> + Â Â Â if ((uint32_t)secs != secs)
> + Â Â Â Â Â Â Â return -EINVAL;
> +
> + Â Â Â jz4740_rtc_reg_write(rtc, JZ_REG_RTC_SEC, secs);
> +
> + Â Â Â return 0;
> +}
> +
> +static int jz4740_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
> +{
> + Â Â Â struct jz4740_rtc *rtc = dev_to_rtc(dev);
> + Â Â Â uint32_t secs, secs2;
> + Â Â Â uint32_t ctrl;
> +
> + Â Â Â secs = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_SEC_ALARM);
> + Â Â Â secs2 = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_SEC_ALARM);
> +
> + Â Â Â while (secs != secs2) {
> + Â Â Â Â Â Â Â secs = secs2;
> + Â Â Â Â Â Â Â secs2 = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_SEC_ALARM);
> + Â Â Â }
> +
> + Â Â Â ctrl = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_CTRL);
> +
> + Â Â Â alrm->enabled = !!(ctrl & JZ_RTC_CTRL_AE);
> + Â Â Â alrm->pending = !!(ctrl & JZ_RTC_CTRL_AF);
> +
> + Â Â Â rtc_time_to_tm(secs, &alrm->time);
> +
> + Â Â Â return rtc_valid_tm(&alrm->time);
> +}
> +
> +static int jz4740_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
> +{
> + Â Â Â struct jz4740_rtc *rtc = dev_to_rtc(dev);
> + Â Â Â unsigned long secs;
> +
> + Â Â Â rtc_tm_to_time(&alrm->time, &secs);
> +
> + Â Â Â if ((uint32_t)secs != secs)
> + Â Â Â Â Â Â Â return -EINVAL;
> +
> + Â Â Â jz4740_rtc_reg_write(rtc, JZ_REG_RTC_SEC_ALARM, (uint32_t)secs);
> + Â Â Â jz4740_rtc_ctrl_set_bits(rtc, JZ_RTC_CTRL_AE,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â alrm->enabled ? JZ_RTC_CTRL_AE : 0);
> +
> + Â Â Â return 0;
> +}
> +
> +static int jz4740_rtc_update_irq_enable(struct device *dev, unsigned int enable)
> +{
> + Â Â Â struct jz4740_rtc *rtc = dev_to_rtc(dev);
> + Â Â Â jz4740_rtc_ctrl_set_bits(rtc, JZ_RTC_CTRL_1HZ_IRQ,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â enable ? JZ_RTC_CTRL_1HZ_IRQ : 0);
> + Â Â Â return 0;
> +}
> +
> +
> +static int jz4740_rtc_alarm_irq_enable(struct device *dev, unsigned int enable)
> +{
> + Â Â Â struct jz4740_rtc *rtc = dev_to_rtc(dev);
> + Â Â Â jz4740_rtc_ctrl_set_bits(rtc, JZ_RTC_CTRL_AF_IRQ,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â enable ? JZ_RTC_CTRL_AF_IRQ : 0);
> + Â Â Â return 0;
> +}
> +
> +static struct rtc_class_ops jz4740_rtc_ops = {
> +    .read_time   Â= jz4740_rtc_read_time,
> +    .set_mmss    = jz4740_rtc_set_mmss,
> +    .read_alarm   = jz4740_rtc_read_alarm,
> +    .set_alarm   Â= jz4740_rtc_set_alarm,
> + Â Â Â .update_irq_enable = jz4740_rtc_update_irq_enable,
> + Â Â Â .alarm_irq_enable = jz4740_rtc_alarm_irq_enable,
> +};
> +
> +static irqreturn_t jz4740_rtc_irq(int irq, void *data)
> +{
> + Â Â Â struct jz4740_rtc *rtc = data;
> + Â Â Â uint32_t ctrl;
> + Â Â Â unsigned long events = 0;
> + Â Â Â ctrl = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_CTRL);
> +
> + Â Â Â if (ctrl & JZ_RTC_CTRL_1HZ)
> + Â Â Â Â Â Â Â events |= (RTC_UF | RTC_IRQF);
> +
> + Â Â Â if (ctrl & JZ_RTC_CTRL_AF)
> + Â Â Â Â Â Â Â events |= (RTC_AF | RTC_IRQF);
> +
> + Â Â Â rtc_update_irq(rtc->rtc, 1, events);
> +
> + Â Â Â jz4740_rtc_ctrl_set_bits(rtc, JZ_RTC_CTRL_1HZ | JZ_RTC_CTRL_AF, 0);
> +
> + Â Â Â return IRQ_HANDLED;
> +}
> +
> +void jz4740_rtc_poweroff(struct device *dev)
> +{
> + Â Â Â struct jz4740_rtc *rtc = dev_get_drvdata(dev);
> + Â Â Â jz4740_rtc_reg_write(rtc, JZ_REG_RTC_HIBERNATE, 1);
> +}
> +EXPORT_SYMBOL_GPL(jz4740_rtc_poweroff);
> +
> +static int __devinit jz4740_rtc_probe(struct platform_device *pdev)
> +{
> + Â Â Â int ret;
> + Â Â Â struct jz4740_rtc *rtc;
> + Â Â Â uint32_t scratchpad;
> +
> + Â Â Â rtc = kmalloc(sizeof(*rtc), GFP_KERNEL);
> +
> + Â Â Â rtc->irq = platform_get_irq(pdev, 0);
> +
> + Â Â Â if (rtc->irq < 0) {
> + Â Â Â Â Â Â Â ret = -ENOENT;
> + Â Â Â Â Â Â Â dev_err(&pdev->dev, "Failed to get platform irq\n");
> + Â Â Â Â Â Â Â goto err_free;
> + Â Â Â }
> +
> + Â Â Â rtc->mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
> + Â Â Â if (!rtc->mem) {
> + Â Â Â Â Â Â Â ret = -ENOENT;
> + Â Â Â Â Â Â Â dev_err(&pdev->dev, "Failed to get platform mmio memory\n");
> + Â Â Â Â Â Â Â goto err_free;
> + Â Â Â }
> +
> + Â Â Â rtc->mem = request_mem_region(rtc->mem->start, resource_size(rtc->mem),
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â pdev->name);
> +
> + Â Â Â if (!rtc->mem) {
> + Â Â Â Â Â Â Â ret = -EBUSY;
> + Â Â Â Â Â Â Â dev_err(&pdev->dev, "Failed to request mmio memory region\n");
> + Â Â Â Â Â Â Â goto err_free;
> + Â Â Â }
> +
> + Â Â Â rtc->base = ioremap_nocache(rtc->mem->start, resource_size(rtc->mem));
> +
> + Â Â Â if (!rtc->base) {
> + Â Â Â Â Â Â Â ret = -EBUSY;
> + Â Â Â Â Â Â Â dev_err(&pdev->dev, "Failed to ioremap mmio memory\n");
> + Â Â Â Â Â Â Â goto err_release_mem_region;
> + Â Â Â }
> +
> + Â Â Â spin_lock_init(&rtc->lock);
> +
> + Â Â Â platform_set_drvdata(pdev, rtc);
> +
> + Â Â Â rtc->rtc = rtc_device_register(pdev->name, &pdev->dev, &jz4740_rtc_ops,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â THIS_MODULE);
> +
> + Â Â Â if (IS_ERR(rtc->rtc)) {
> + Â Â Â Â Â Â Â ret = PTR_ERR(rtc->rtc);
> + Â Â Â Â Â Â Â dev_err(&pdev->dev, "Failed to register rtc device: %d\n", ret);
> + Â Â Â Â Â Â Â goto err_iounmap;
> + Â Â Â }
> +
> + Â Â Â ret = request_irq(rtc->irq, jz4740_rtc_irq, 0,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â pdev->name, rtc);
> +
> + Â Â Â if (ret) {
> + Â Â Â Â Â Â Â dev_err(&pdev->dev, "Failed to request rtc irq: %d\n", ret);
> + Â Â Â Â Â Â Â goto err_unregister_rtc;
> + Â Â Â }
> +
> + Â Â Â scratchpad = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_SCRATCHPAD);
> + Â Â Â if (scratchpad != 0x12345678) {
> + Â Â Â Â Â Â Â jz4740_rtc_reg_write(rtc, JZ_REG_RTC_SCRATCHPAD, 0x12345678);
> + Â Â Â Â Â Â Â jz4740_rtc_reg_write(rtc, JZ_REG_RTC_SEC, 0);
> + Â Â Â }
> +
> + Â Â Â return 0;
> +
> +err_unregister_rtc:
> + Â Â Â rtc_device_unregister(rtc->rtc);
> +err_iounmap:
> + Â Â Â platform_set_drvdata(pdev, NULL);
> + Â Â Â iounmap(rtc->base);
> +err_release_mem_region:
> + Â Â Â release_mem_region(rtc->mem->start, resource_size(rtc->mem));
> +err_free:
> + Â Â Â kfree(rtc);
> +
> + Â Â Â return ret;
> +}
> +
> +static int __devexit jz4740_rtc_remove(struct platform_device *pdev)
> +{
> + Â Â Â struct jz4740_rtc *rtc = platform_get_drvdata(pdev);
> +
> + Â Â Â free_irq(rtc->irq, rtc);
> +
> + Â Â Â rtc_device_unregister(rtc->rtc);
> +
> + Â Â Â iounmap(rtc->base);
> + Â Â Â release_mem_region(rtc->mem->start, resource_size(rtc->mem));
> +
> + Â Â Â kfree(rtc);
> +
> + Â Â Â platform_set_drvdata(pdev, NULL);
> +
> + Â Â Â return 0;
> +}
> +
> +struct platform_driver jz4740_rtc_driver = {
> + Â Â Â .probe = jz4740_rtc_probe,
> + Â Â Â .remove = __devexit_p(jz4740_rtc_remove),
> + Â Â Â .driver = {
> + Â Â Â Â Â Â Â .name = "jz4740-rtc",
> + Â Â Â Â Â Â Â .owner = THIS_MODULE,
> + Â Â Â },
> +};
> +
> +static int __init jz4740_rtc_init(void)
> +{
> + Â Â Â return platform_driver_register(&jz4740_rtc_driver);

platform_driver_probe is much better here.

> +}
> +module_init(jz4740_rtc_init);
> +
> +static void __exit jz4740_rtc_exit(void)
> +{
> + Â Â Â platform_driver_unregister(&jz4740_rtc_driver);
> +}
> +module_exit(jz4740_rtc_exit);
> +
> +MODULE_AUTHOR("Lars-Peter Clausen <lars@xxxxxxxxxx>");
> +MODULE_LICENSE("GPL");
> +MODULE_DESCRIPTION("RTC driver for the JZ4740 SoC\n");
> +MODULE_ALIAS("platform:jz4740-rtc");
> --
> 1.5.6.5
>
> --
> 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/