Re: [PATCH 5/6 v2] rtc: Add support for the RTC in VIA VT8500 and compatibles

From: Alexey Charkov
Date: Fri Nov 12 2010 - 17:53:30 EST


2010/11/7 Alexey Charkov <alchark@xxxxxxxxx>:
> This adds a driver for the RTC devices in VIA and WonderMedia
> Systems-on-Chip. Alarm, 1Hz interrupts, reading and setting time
> are supported.
>
> Signed-off-by: Alexey Charkov <alchark@xxxxxxxxx>
> ---
>
> Please review and (if appropriate) commit to a relevant git tree for
> further integration in 2.6.38.
>
> Compared to the previous submission, this code just contains a rebase
> against the latest changes introduced in 2.6.37 merge window.
>
> Relevant platform definitions are introduced by PATCH 1/6 in this series,
> so one would need that to make use of this code.
>
> Due credits go to the community for providing feedback, advice and
> testing.
>
> Âdrivers/rtc/Kconfig   Â|  Â7 +
> Âdrivers/rtc/Makefile   |  Â1 +
> Âdrivers/rtc/rtc-vt8500.c | Â363 ++++++++++++++++++++++++++++++++++++++++++++++
> Â3 files changed, 371 insertions(+), 0 deletions(-)
> Âcreate mode 100644 drivers/rtc/rtc-vt8500.c
>
> diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig
> index 2883428..27ed129 100644
> --- a/drivers/rtc/Kconfig
> +++ b/drivers/rtc/Kconfig
> @@ -865,6 +865,13 @@ config RTC_DRV_PXA
> Â Â Â Â ÂThis RTC driver uses PXA RTC registers available since pxa27x
> Â Â Â Â Âseries (RDxR, RYxR) instead of legacy RCNR, RTAR.
>
> +config RTC_DRV_VT8500
> + Â Â Â tristate "VIA/WonderMedia 85xx SoC RTC"
> + Â Â Â depends on ARCH_VT8500
> + Â Â Â help
> + Â Â Â Â If you say Y here you will get access to the real time clock
> + Â Â Â Â built into your VIA VT8500 SoC or its relatives.
> +
>
> Âconfig RTC_DRV_SUN4V
> Â Â Â Âbool "SUN4V Hypervisor RTC"
> diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile
> index 4c2832d..1a354e1 100644
> --- a/drivers/rtc/Makefile
> +++ b/drivers/rtc/Makefile
> @@ -97,6 +97,7 @@ obj-$(CONFIG_RTC_DRV_TWL4030) += rtc-twl.o
> Âobj-$(CONFIG_RTC_DRV_TX4939) Â += rtc-tx4939.o
> Âobj-$(CONFIG_RTC_DRV_V3020) Â Â+= rtc-v3020.o
> Âobj-$(CONFIG_RTC_DRV_VR41XX) Â += rtc-vr41xx.o
> +obj-$(CONFIG_RTC_DRV_VT8500) Â += rtc-vt8500.o
> Âobj-$(CONFIG_RTC_DRV_WM831X) Â += rtc-wm831x.o
> Âobj-$(CONFIG_RTC_DRV_WM8350) Â += rtc-wm8350.o
> Âobj-$(CONFIG_RTC_DRV_X1205) Â Â+= rtc-x1205.o
> diff --git a/drivers/rtc/rtc-vt8500.c b/drivers/rtc/rtc-vt8500.c
> new file mode 100644
> index 0000000..7260c95
> --- /dev/null
> +++ b/drivers/rtc/rtc-vt8500.c
> @@ -0,0 +1,363 @@
> +/*
> + * drivers/rtc/rtc-vt8500.c
> + *
> + * ÂCopyright (C) 2010 Alexey Charkov <alchark@xxxxxxxxx>
> + *
> + * Based on rtc-pxa.c
> + *
> + * This software is licensed under the terms of the GNU General Public
> + * License version 2, as published by the Free Software Foundation, and
> + * may be copied, distributed, and modified under those terms.
> + *
> + * 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.
> + */
> +
> +#include <linux/module.h>
> +#include <linux/rtc.h>
> +#include <linux/init.h>
> +#include <linux/interrupt.h>
> +#include <linux/io.h>
> +#include <linux/bcd.h>
> +#include <linux/platform_device.h>
> +#include <linux/slab.h>
> +
> +/*
> + * Register definitions
> + */
> +#define VT8500_RTC_TS Â Â Â Â Â0x00 Â Â/* Time set */
> +#define VT8500_RTC_DS Â Â Â Â Â0x04 Â Â/* Date set */
> +#define VT8500_RTC_AS Â Â Â Â Â0x08 Â Â/* Alarm set */
> +#define VT8500_RTC_CR     Â0x0c  Â/* Control */
> +#define VT8500_RTC_TR Â Â Â Â Â0x10 Â Â/* Time read */
> +#define VT8500_RTC_DR Â Â Â Â Â0x14 Â Â/* Date read */
> +#define VT8500_RTC_WS Â Â Â Â Â0x18 Â Â/* Write status */
> +#define VT8500_RTC_CL Â Â Â Â Â0x20 Â Â/* Calibration */
> +#define VT8500_RTC_IS Â Â Â Â Â0x24 Â Â/* Interrupt status */
> +#define VT8500_RTC_ST Â Â Â Â Â0x28 Â Â/* Status */
> +
> +#define INVALID_TIME_BIT Â Â Â (1 << 31)
> +
> +#define DATE_CENTURY_S Â Â Â Â 19
> +#define DATE_YEAR_S Â Â Â Â Â Â11
> +#define DATE_YEAR_MASK Â Â Â Â (0xff << DATE_YEAR_S)
> +#define DATE_MONTH_S Â Â Â Â Â 6
> +#define DATE_MONTH_MASK Â Â Â Â Â Â Â Â(0x1f << DATE_MONTH_S)
> +#define DATE_DAY_MASK Â Â Â Â Â0x3f
> +
> +#define TIME_DOW_S Â Â Â Â Â Â 20
> +#define TIME_DOW_MASK Â Â Â Â Â(0x07 << TIME_DOW_S)
> +#define TIME_HOUR_S Â Â Â Â Â Â14
> +#define TIME_HOUR_MASK Â Â Â Â (0x3f << TIME_HOUR_S)
> +#define TIME_MIN_S Â Â Â Â Â Â 7
> +#define TIME_MIN_MASK Â Â Â Â Â(0x7f << TIME_MIN_S)
> +#define TIME_SEC_MASK Â Â Â Â Â0x7f
> +
> +#define ALARM_DAY_S Â Â Â Â Â Â20
> +#define ALARM_DAY_MASK Â Â Â Â (0x3f << ALARM_DAY_S)
> +
> +#define ALARM_DAY_BIT Â Â Â Â Â(1 << 29)
> +#define ALARM_HOUR_BIT Â Â Â Â (1 << 28)
> +#define ALARM_MIN_BIT Â Â Â Â Â(1 << 27)
> +#define ALARM_SEC_BIT Â Â Â Â Â(1 << 26)
> +
> +#define ALARM_ENABLE_MASK Â Â Â(ALARM_DAY_BIT \
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â | ALARM_HOUR_BIT \
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â | ALARM_MIN_BIT \
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â | ALARM_SEC_BIT)
> +
> +struct vt8500_rtc {
> +    void __iomem      Â*regbase;
> +    int           irq_alarm;
> +    int           irq_hz;
> +    struct rtc_device    *rtc;
> +    spinlock_t       Âlock;      /* Protects this structure */
> +    struct rtc_time     rtc_alarm;
> +};
> +
> +static irqreturn_t vt8500_rtc_irq(int irq, void *dev_id)
> +{
> + Â Â Â struct platform_device *pdev = to_platform_device(dev_id);
> + Â Â Â struct vt8500_rtc *vt8500_rtc = platform_get_drvdata(pdev);
> + Â Â Â u32 isr;
> + Â Â Â unsigned long events = 0;
> +
> + Â Â Â spin_lock(&vt8500_rtc->lock);
> +
> + Â Â Â /* clear interrupt sources */
> + Â Â Â isr = readl(vt8500_rtc->regbase + VT8500_RTC_IS);
> + Â Â Â writel(isr, vt8500_rtc->regbase + VT8500_RTC_IS);
> +
> + Â Â Â if (isr & 1)
> + Â Â Â Â Â Â Â events |= RTC_AF | RTC_IRQF;
> +
> + Â Â Â /* Only second/minute interrupts are supported */
> + Â Â Â if (isr & 2)
> + Â Â Â Â Â Â Â events |= RTC_UF | RTC_IRQF;
> +
> + Â Â Â rtc_update_irq(vt8500_rtc->rtc, 1, events);
> +
> + Â Â Â spin_unlock(&vt8500_rtc->lock);
> + Â Â Â return IRQ_HANDLED;
> +}
> +
> +static int vt8500_rtc_open(struct device *dev)
> +{
> + Â Â Â struct vt8500_rtc *vt8500_rtc = dev_get_drvdata(dev);
> + Â Â Â int ret;
> +
> + Â Â Â ret = request_irq(vt8500_rtc->irq_hz, vt8500_rtc_irq, IRQF_DISABLED,
> + Â Â Â Â Â Â Â Â Â Â Â Â "rtc 1Hz", dev);
> + Â Â Â if (ret < 0) {
> + Â Â Â Â Â Â Â dev_err(dev, "can't get irq %i, err %d\n", vt8500_rtc->irq_hz,
> + Â Â Â Â Â Â Â Â Â Â Â ret);
> + Â Â Â Â Â Â Â goto err_irq_hz;
> + Â Â Â }
> + Â Â Â ret = request_irq(vt8500_rtc->irq_alarm, vt8500_rtc_irq, IRQF_DISABLED,
> + Â Â Â Â Â Â Â Â Â Â Â Â "rtc alarm", dev);
> + Â Â Â if (ret < 0) {
> + Â Â Â Â Â Â Â dev_err(dev, "can't get irq %i, err %d\n",
> + Â Â Â Â Â Â Â Â Â Â Â vt8500_rtc->irq_alarm, ret);
> + Â Â Â Â Â Â Â goto err_irq_alarm;
> + Â Â Â }
> + Â Â Â return 0;
> +
> +err_irq_alarm:
> + Â Â Â free_irq(vt8500_rtc->irq_hz, dev);
> +err_irq_hz:
> + Â Â Â return ret;
> +}
> +
> +static void vt8500_rtc_release(struct device *dev)
> +{
> + Â Â Â struct vt8500_rtc *vt8500_rtc = dev_get_drvdata(dev);
> +
> + Â Â Â spin_lock_irq(&vt8500_rtc->lock);
> + Â Â Â /* Disable alarm matching */
> + Â Â Â writel(0, vt8500_rtc->regbase + VT8500_RTC_IS);
> + Â Â Â spin_unlock_irq(&vt8500_rtc->lock);
> +
> + Â Â Â free_irq(vt8500_rtc->irq_alarm, dev);
> + Â Â Â free_irq(vt8500_rtc->irq_hz, dev);
> +}
> +
> +static int vt8500_rtc_read_time(struct device *dev, struct rtc_time *tm)
> +{
> + Â Â Â struct vt8500_rtc *vt8500_rtc = dev_get_drvdata(dev);
> + Â Â Â u32 date, time;
> +
> + Â Â Â date = readl(vt8500_rtc->regbase + VT8500_RTC_DR);
> + Â Â Â time = readl(vt8500_rtc->regbase + VT8500_RTC_TR);
> +
> + Â Â Â tm->tm_sec = bcd2bin(time & TIME_SEC_MASK);
> + Â Â Â tm->tm_min = bcd2bin((time & TIME_MIN_MASK) >> TIME_MIN_S);
> + Â Â Â tm->tm_hour = bcd2bin((time & TIME_HOUR_MASK) >> TIME_HOUR_S);
> + Â Â Â tm->tm_mday = bcd2bin(date & DATE_DAY_MASK);
> + Â Â Â tm->tm_mon = bcd2bin((date & DATE_MONTH_MASK) >> DATE_MONTH_S);
> + Â Â Â tm->tm_year = bcd2bin((date & DATE_YEAR_MASK) >> DATE_YEAR_S);
> + Â Â Â tm->tm_wday = (time & TIME_DOW_MASK) >> TIME_DOW_S;
> +
> + Â Â Â return 0;
> +}
> +
> +static int vt8500_rtc_set_time(struct device *dev, struct rtc_time *tm)
> +{
> + Â Â Â struct vt8500_rtc *vt8500_rtc = dev_get_drvdata(dev);
> +
> + Â Â Â writel((bin2bcd(tm->tm_year) << DATE_YEAR_S)
> + Â Â Â Â Â Â Â | (bin2bcd(tm->tm_mon) << DATE_MONTH_S)
> + Â Â Â Â Â Â Â | (bin2bcd(tm->tm_mday)),
> + Â Â Â Â Â Â Â vt8500_rtc->regbase + VT8500_RTC_DS);
> + Â Â Â writel((bin2bcd(tm->tm_wday) << TIME_DOW_S)
> + Â Â Â Â Â Â Â | (bin2bcd(tm->tm_hour) << TIME_HOUR_S)
> + Â Â Â Â Â Â Â | (bin2bcd(tm->tm_min) << TIME_MIN_S)
> + Â Â Â Â Â Â Â | (bin2bcd(tm->tm_sec)),
> + Â Â Â Â Â Â Â vt8500_rtc->regbase + VT8500_RTC_TS);
> +
> + Â Â Â return 0;
> +}
> +
> +static int vt8500_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
> +{
> + Â Â Â struct vt8500_rtc *vt8500_rtc = dev_get_drvdata(dev);
> + Â Â Â u32 isr, alarm;
> +
> + Â Â Â alarm = readl(vt8500_rtc->regbase + VT8500_RTC_AS);
> + Â Â Â isr = readl(vt8500_rtc->regbase + VT8500_RTC_IS);
> +
> + Â Â Â alrm->time.tm_mday = bcd2bin((alarm & ALARM_DAY_MASK) >> ALARM_DAY_S);
> + Â Â Â alrm->time.tm_hour = bcd2bin((alarm & TIME_HOUR_MASK) >> TIME_HOUR_S);
> + Â Â Â alrm->time.tm_min = bcd2bin((alarm & TIME_MIN_MASK) >> TIME_MIN_S);
> + Â Â Â alrm->time.tm_sec = bcd2bin((alarm & TIME_SEC_MASK));
> +
> + Â Â Â alrm->enabled = (alarm & ALARM_ENABLE_MASK) ? 1 : 0;
> +
> + Â Â Â alrm->pending = (isr & 1) ? 1 : 0;
> + Â Â Â return 0;
> +}
> +
> +static int vt8500_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
> +{
> + Â Â Â struct vt8500_rtc *vt8500_rtc = dev_get_drvdata(dev);
> +
> + Â Â Â writel((alrm->enabled ? ALARM_ENABLE_MASK : 0)
> + Â Â Â Â Â Â Â | (bin2bcd(alrm->time.tm_mday) << ALARM_DAY_S)
> + Â Â Â Â Â Â Â | (bin2bcd(alrm->time.tm_hour) << TIME_HOUR_S)
> + Â Â Â Â Â Â Â | (bin2bcd(alrm->time.tm_min) << TIME_MIN_S)
> + Â Â Â Â Â Â Â | (bin2bcd(alrm->time.tm_sec)),
> + Â Â Â Â Â Â Â vt8500_rtc->regbase + VT8500_RTC_AS);
> +
> + Â Â Â return 0;
> +}
> +
> +static int vt8500_rtc_ioctl(struct device *dev, unsigned int cmd,
> + Â Â Â Â Â Â Â unsigned long arg)
> +{
> + Â Â Â struct vt8500_rtc *vt8500_rtc = dev_get_drvdata(dev);
> + Â Â Â int ret = 0;
> + Â Â Â unsigned long tmp;
> +
> + Â Â Â spin_lock_irq(&vt8500_rtc->lock);
> + Â Â Â switch (cmd) {
> + Â Â Â case RTC_AIE_OFF:
> + Â Â Â Â Â Â Â tmp = readl(vt8500_rtc->regbase + VT8500_RTC_AS);
> + Â Â Â Â Â Â Â writel(tmp & ~ALARM_ENABLE_MASK,
> + Â Â Â Â Â Â Â Â Â Â Âvt8500_rtc->regbase + VT8500_RTC_AS);
> + Â Â Â Â Â Â Â break;
> + Â Â Â case RTC_AIE_ON:
> + Â Â Â Â Â Â Â tmp = readl(vt8500_rtc->regbase + VT8500_RTC_AS);
> + Â Â Â Â Â Â Â writel(tmp | ALARM_ENABLE_MASK,
> + Â Â Â Â Â Â Â Â Â Â Âvt8500_rtc->regbase + VT8500_RTC_AS);
> + Â Â Â Â Â Â Â break;
> + Â Â Â case RTC_UIE_OFF:
> + Â Â Â Â Â Â Â tmp = readl(vt8500_rtc->regbase + VT8500_RTC_CR);
> + Â Â Â Â Â Â Â writel(tmp & ~(1 << 2),
> + Â Â Â Â Â Â Â Â Â Â Âvt8500_rtc->regbase + VT8500_RTC_CR);
> + Â Â Â Â Â Â Â break;
> + Â Â Â case RTC_UIE_ON:
> + Â Â Â Â Â Â Â tmp = readl(vt8500_rtc->regbase + VT8500_RTC_CR);
> + Â Â Â Â Â Â Â writel(tmp | ((1 << 3) | (1 << 2)),
> + Â Â Â Â Â Â Â Â Â Â Âvt8500_rtc->regbase + VT8500_RTC_CR);
> + Â Â Â Â Â Â Â break;
> + Â Â Â default:
> + Â Â Â Â Â Â Â ret = -ENOIOCTLCMD;
> + Â Â Â }
> +
> + Â Â Â spin_unlock_irq(&vt8500_rtc->lock);
> + Â Â Â return ret;
> +}
> +
> +static const struct rtc_class_ops vt8500_rtc_ops = {
> + Â Â Â .open = vt8500_rtc_open,
> + Â Â Â .release = vt8500_rtc_release,
> + Â Â Â .ioctl = vt8500_rtc_ioctl,
> + Â Â Â .read_time = vt8500_rtc_read_time,
> + Â Â Â .set_time = vt8500_rtc_set_time,
> + Â Â Â .read_alarm = vt8500_rtc_read_alarm,
> + Â Â Â .set_alarm = vt8500_rtc_set_alarm,
> +};
> +
> +static int __init vt8500_rtc_probe(struct platform_device *pdev)
> +{
> + Â Â Â struct device *dev = &pdev->dev;
> + Â Â Â struct vt8500_rtc *vt8500_rtc;
> + Â Â Â struct resource *res;
> + Â Â Â int ret;
> +
> + Â Â Â vt8500_rtc = kzalloc(sizeof(struct vt8500_rtc), GFP_KERNEL);
> + Â Â Â if (!vt8500_rtc)
> + Â Â Â Â Â Â Â return -ENOMEM;
> +
> + Â Â Â spin_lock_init(&vt8500_rtc->lock);
> + Â Â Â platform_set_drvdata(pdev, vt8500_rtc);
> +
> + Â Â Â ret = -ENXIO;
> + Â Â Â res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
> + Â Â Â if (!res) {
> + Â Â Â Â Â Â Â dev_err(dev, "No I/O memory resource defined\n");
> + Â Â Â Â Â Â Â goto err_map;
> + Â Â Â }
> +
> + Â Â Â vt8500_rtc->irq_alarm = platform_get_irq(pdev, 0);
> + Â Â Â if (vt8500_rtc->irq_alarm < 0) {
> + Â Â Â Â Â Â Â dev_err(dev, "No alarm IRQ resource defined\n");
> + Â Â Â Â Â Â Â goto err_map;
> + Â Â Â }
> +
> + Â Â Â vt8500_rtc->irq_hz = platform_get_irq(pdev, 1);
> + Â Â Â if (vt8500_rtc->irq_hz < 0) {
> + Â Â Â Â Â Â Â dev_err(dev, "No 1Hz IRQ resource defined\n");
> + Â Â Â Â Â Â Â goto err_map;
> + Â Â Â }
> +
> + Â Â Â ret = -ENOMEM;
> + Â Â Â vt8500_rtc->regbase = ioremap(res->start, resource_size(res));
> + Â Â Â if (!vt8500_rtc->regbase) {
> + Â Â Â Â Â Â Â dev_err(&pdev->dev, "Unable to map RTC I/O memory\n");
> + Â Â Â Â Â Â Â goto err_map;
> + Â Â Â }
> +
> + Â Â Â /* Enable the second/minute interrupt generation and enable RTC */
> + Â Â Â writel((1 << 3) | (1 << 2) | 1, vt8500_rtc->regbase + VT8500_RTC_CR);
> +
> + Â Â Â vt8500_rtc->rtc = rtc_device_register("vt8500-rtc", &pdev->dev,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â &vt8500_rtc_ops, THIS_MODULE);
> + Â Â Â ret = PTR_ERR(vt8500_rtc->rtc);
> + Â Â Â if (IS_ERR(vt8500_rtc->rtc)) {
> + Â Â Â Â Â Â Â dev_err(dev, "Failed to register RTC device -> %d\n", ret);
> + Â Â Â Â Â Â Â goto err_rtc_reg;
> + Â Â Â }
> +
> + Â Â Â device_init_wakeup(dev, 1);
> +
> + Â Â Â return 0;
> +
> +err_rtc_reg:
> + Â Â Â iounmap(vt8500_rtc->regbase);
> +err_map:
> + Â Â Â kfree(vt8500_rtc);
> + Â Â Â return ret;
> +}
> +
> +static int __exit vt8500_rtc_remove(struct platform_device *pdev)
> +{
> + Â Â Â struct vt8500_rtc *vt8500_rtc = platform_get_drvdata(pdev);
> +
> + Â Â Â rtc_device_unregister(vt8500_rtc->rtc);
> +
> + Â Â Â spin_lock_irq(&vt8500_rtc->lock);
> + Â Â Â iounmap(vt8500_rtc->regbase);
> + Â Â Â spin_unlock_irq(&vt8500_rtc->lock);
> +
> + Â Â Â kfree(vt8500_rtc);
> +
> + Â Â Â return 0;
> +}
> +
> +static struct platform_driver vt8500_rtc_driver = {
> +    .probe     Â= vt8500_rtc_probe,
> +    .remove     = __exit_p(vt8500_rtc_remove),
> +    .driver     = {
> +        .name  = "vt8500-rtc"
> + Â Â Â },
> +};
> +
> +static int __init vt8500_rtc_init(void)
> +{
> + Â Â Â return platform_driver_register(&vt8500_rtc_driver);
> +}
> +
> +static void __exit vt8500_rtc_exit(void)
> +{
> + Â Â Â platform_driver_unregister(&vt8500_rtc_driver);
> +}
> +
> +module_init(vt8500_rtc_init);
> +module_exit(vt8500_rtc_exit);
> +
> +MODULE_AUTHOR("Alexey Charkov <alchark@xxxxxxxxx>");
> +MODULE_DESCRIPTION("VIA VT8500 SoC Realtime Clock Driver (RTC)");
> +MODULE_LICENSE("GPL");
> +MODULE_ALIAS("platform:vt8500-rtc");
> --
> 1.7.3.2
>
>

Any comments about this?

Thanks,
Alexey
--
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/