string: factorize skip_spaces and export it to be generally available
[safe/jmp/linux-2.6] / drivers / rtc / rtc-vr41xx.c
index 4d49fd5..fadddac 100644 (file)
@@ -1,7 +1,7 @@
 /*
  *  Driver for NEC VR4100 series Real Time Clock unit.
  *
- *  Copyright (C) 2003-2006  Yoichi Yuasa <yoichi_yuasa@tripeaks.co.jp>
+ *  Copyright (C) 2003-2008  Yoichi Yuasa <yuasa@linux-mips.org>
  *
  *  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
  *  along with this program; if not, write to the Free Software
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
+#include <linux/err.h>
 #include <linux/fs.h>
 #include <linux/init.h>
 #include <linux/ioport.h>
-#include <linux/irq.h>
+#include <linux/interrupt.h>
 #include <linux/module.h>
 #include <linux/platform_device.h>
 #include <linux/rtc.h>
 #include <linux/spinlock.h>
 #include <linux/types.h>
+#include <linux/log2.h>
 
 #include <asm/div64.h>
 #include <asm/io.h>
 #include <asm/uaccess.h>
-#include <asm/vr41xx/vr41xx.h>
 
-MODULE_AUTHOR("Yoichi Yuasa <yoichi_yuasa@tripeaks.co.jp>");
+MODULE_AUTHOR("Yoichi Yuasa <yuasa@linux-mips.org>");
 MODULE_DESCRIPTION("NEC VR4100 series RTC driver");
-MODULE_LICENSE("GPL");
-
-#define RTC1_TYPE1_START       0x0b0000c0UL
-#define RTC1_TYPE1_END         0x0b0000dfUL
-#define RTC2_TYPE1_START       0x0b0001c0UL
-#define RTC2_TYPE1_END         0x0b0001dfUL
-
-#define RTC1_TYPE2_START       0x0f000100UL
-#define RTC1_TYPE2_END         0x0f00011fUL
-#define RTC2_TYPE2_START       0x0f000120UL
-#define RTC2_TYPE2_END         0x0f00013fUL
-
-#define RTC1_SIZE              0x20
-#define RTC2_SIZE              0x20
+MODULE_LICENSE("GPL v2");
 
 /* RTC 1 registers */
 #define ETIMELREG              0x00
@@ -81,7 +69,6 @@ MODULE_LICENSE("GPL");
 
 #define RTC_FREQUENCY          32768
 #define MAX_PERIODIC_RATE      6553
-#define MAX_USER_PERIODIC_RATE 64
 
 static void __iomem *rtc1_base;
 static void __iomem *rtc2_base;
@@ -94,17 +81,12 @@ static void __iomem *rtc2_base;
 
 static unsigned long epoch = 1970;     /* Jan 1 1970 00:00:00 */
 
-static spinlock_t rtc_lock = SPIN_LOCK_UNLOCKED;
+static DEFINE_SPINLOCK(rtc_lock);
 static char rtc_name[] = "RTC";
-static unsigned long periodic_frequency;
 static unsigned long periodic_count;
-
-struct resource rtc_resource[2] = {
-       {       .name   = rtc_name,
-               .flags  = IORESOURCE_MEM,       },
-       {       .name   = rtc_name,
-               .flags  = IORESOURCE_MEM,       },
-};
+static unsigned int alarm_enabled;
+static int aie_irq;
+static int pie_irq;
 
 static inline unsigned long read_elapsed_second(void)
 {
@@ -150,8 +132,8 @@ static void vr41xx_rtc_release(struct device *dev)
 
        spin_unlock_irq(&rtc_lock);
 
-       disable_irq(ELAPSEDTIME_IRQ);
-       disable_irq(RTCLONG1_IRQ);
+       disable_irq(aie_irq);
+       disable_irq(pie_irq);
 }
 
 static int vr41xx_rtc_read_time(struct device *dev, struct rtc_time *time)
@@ -189,6 +171,7 @@ static int vr41xx_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
        low = rtc1_read(ECMPLREG);
        mid = rtc1_read(ECMPMREG);
        high = rtc1_read(ECMPHREG);
+       wkalrm->enabled = alarm_enabled;
 
        spin_unlock_irq(&rtc_lock);
 
@@ -207,53 +190,73 @@ static int vr41xx_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
 
        spin_lock_irq(&rtc_lock);
 
+       if (alarm_enabled)
+               disable_irq(aie_irq);
+
        rtc1_write(ECMPLREG, (uint16_t)(alarm_sec << 15));
        rtc1_write(ECMPMREG, (uint16_t)(alarm_sec >> 1));
        rtc1_write(ECMPHREG, (uint16_t)(alarm_sec >> 17));
 
+       if (wkalrm->enabled)
+               enable_irq(aie_irq);
+
+       alarm_enabled = wkalrm->enabled;
+
        spin_unlock_irq(&rtc_lock);
 
        return 0;
 }
 
-static int vr41xx_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
+static int vr41xx_rtc_irq_set_freq(struct device *dev, int freq)
 {
-       unsigned long count;
+       u64 count;
 
-       switch (cmd) {
-       case RTC_AIE_ON:
-               enable_irq(ELAPSEDTIME_IRQ);
-               break;
-       case RTC_AIE_OFF:
-               disable_irq(ELAPSEDTIME_IRQ);
-               break;
-       case RTC_PIE_ON:
-               enable_irq(RTCLONG1_IRQ);
-               break;
-       case RTC_PIE_OFF:
-               disable_irq(RTCLONG1_IRQ);
-               break;
-       case RTC_IRQP_READ:
-               return put_user(periodic_frequency, (unsigned long __user *)arg);
-               break;
-       case RTC_IRQP_SET:
-               if (arg > MAX_PERIODIC_RATE)
-                       return -EINVAL;
+       if (!is_power_of_2(freq))
+               return -EINVAL;
+       count = RTC_FREQUENCY;
+       do_div(count, freq);
+
+       spin_lock_irq(&rtc_lock);
+
+       periodic_count = count;
+       rtc1_write(RTCL1LREG, periodic_count);
+       rtc1_write(RTCL1HREG, periodic_count >> 16);
+
+       spin_unlock_irq(&rtc_lock);
+
+       return 0;
+}
 
-               if (arg > MAX_USER_PERIODIC_RATE && capable(CAP_SYS_RESOURCE) == 0)
-                       return -EACCES;
+static int vr41xx_rtc_irq_set_state(struct device *dev, int enabled)
+{
+       if (enabled)
+               enable_irq(pie_irq);
+       else
+               disable_irq(pie_irq);
 
-               periodic_frequency = arg;
+       return 0;
+}
 
-               count = RTC_FREQUENCY;
-               do_div(count, arg);
+static int vr41xx_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
+{
+       switch (cmd) {
+       case RTC_AIE_ON:
+               spin_lock_irq(&rtc_lock);
 
-               periodic_count = count;
+               if (!alarm_enabled) {
+                       enable_irq(aie_irq);
+                       alarm_enabled = 1;
+               }
 
+               spin_unlock_irq(&rtc_lock);
+               break;
+       case RTC_AIE_OFF:
                spin_lock_irq(&rtc_lock);
 
-               rtc1_write(RTCL1LREG, count);
-               rtc1_write(RTCL1HREG, count >> 16);
+               if (alarm_enabled) {
+                       disable_irq(aie_irq);
+                       alarm_enabled = 0;
+               }
 
                spin_unlock_irq(&rtc_lock);
                break;
@@ -263,32 +266,28 @@ static int vr41xx_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long
                /* Doesn't support before 1900 */
                if (arg < 1900)
                        return -EINVAL;
-
-               if (capable(CAP_SYS_TIME) == 0)
-                       return -EACCES;
-
                epoch = arg;
                break;
        default:
-               return -EINVAL;
+               return -ENOIOCTLCMD;
        }
 
        return 0;
 }
 
-static irqreturn_t elapsedtime_interrupt(int irq, void *dev_id, struct pt_regs *regs)
+static irqreturn_t elapsedtime_interrupt(int irq, void *dev_id)
 {
        struct platform_device *pdev = (struct platform_device *)dev_id;
        struct rtc_device *rtc = platform_get_drvdata(pdev);
 
        rtc2_write(RTCINTREG, ELAPSEDTIME_INT);
 
-       rtc_update_irq(&rtc->class_dev, 1, RTC_AF);
+       rtc_update_irq(rtc, 1, RTC_AF);
 
        return IRQ_HANDLED;
 }
 
-static irqreturn_t rtclong1_interrupt(int irq, void *dev_id, struct pt_regs *regs)
+static irqreturn_t rtclong1_interrupt(int irq, void *dev_id)
 {
        struct platform_device *pdev = (struct platform_device *)dev_id;
        struct rtc_device *rtc = platform_get_drvdata(pdev);
@@ -299,49 +298,59 @@ static irqreturn_t rtclong1_interrupt(int irq, void *dev_id, struct pt_regs *reg
        rtc1_write(RTCL1LREG, count);
        rtc1_write(RTCL1HREG, count >> 16);
 
-       rtc_update_irq(&rtc->class_dev, 1, RTC_PF);
+       rtc_update_irq(rtc, 1, RTC_PF);
 
        return IRQ_HANDLED;
 }
 
-static struct rtc_class_ops vr41xx_rtc_ops = {
+static const struct rtc_class_ops vr41xx_rtc_ops = {
        .release        = vr41xx_rtc_release,
        .ioctl          = vr41xx_rtc_ioctl,
        .read_time      = vr41xx_rtc_read_time,
        .set_time       = vr41xx_rtc_set_time,
        .read_alarm     = vr41xx_rtc_read_alarm,
        .set_alarm      = vr41xx_rtc_set_alarm,
+       .irq_set_freq   = vr41xx_rtc_irq_set_freq,
+       .irq_set_state  = vr41xx_rtc_irq_set_state,
 };
 
 static int __devinit rtc_probe(struct platform_device *pdev)
 {
+       struct resource *res;
        struct rtc_device *rtc;
-       unsigned int irq;
        int retval;
 
-       if (pdev->num_resources != 2)
+       if (pdev->num_resources != 4)
                return -EBUSY;
 
-       rtc1_base = ioremap(pdev->resource[0].start, RTC1_SIZE);
-       if (rtc1_base == NULL)
+       res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+       if (!res)
                return -EBUSY;
 
-       rtc2_base = ioremap(pdev->resource[1].start, RTC2_SIZE);
-       if (rtc2_base == NULL) {
-               iounmap(rtc1_base);
-               rtc1_base = NULL;
+       rtc1_base = ioremap(res->start, res->end - res->start + 1);
+       if (!rtc1_base)
                return -EBUSY;
+
+       res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
+       if (!res) {
+               retval = -EBUSY;
+               goto err_rtc1_iounmap;
+       }
+
+       rtc2_base = ioremap(res->start, res->end - res->start + 1);
+       if (!rtc2_base) {
+               retval = -EBUSY;
+               goto err_rtc1_iounmap;
        }
 
        rtc = rtc_device_register(rtc_name, &pdev->dev, &vr41xx_rtc_ops, THIS_MODULE);
        if (IS_ERR(rtc)) {
-               iounmap(rtc1_base);
-               iounmap(rtc2_base);
-               rtc1_base = NULL;
-               rtc2_base = NULL;
-               return PTR_ERR(rtc);
+               retval = PTR_ERR(rtc);
+               goto err_iounmap_all;
        }
 
+       rtc->max_user_freq = MAX_PERIODIC_RATE;
+
        spin_lock_irq(&rtc_lock);
 
        rtc1_write(ECMPLREG, 0);
@@ -352,35 +361,50 @@ static int __devinit rtc_probe(struct platform_device *pdev)
 
        spin_unlock_irq(&rtc_lock);
 
-       irq = ELAPSEDTIME_IRQ;
-       retval = request_irq(irq, elapsedtime_interrupt, SA_INTERRUPT,
-                            "elapsed_time", pdev);
-       if (retval == 0) {
-               irq = RTCLONG1_IRQ;
-               retval = request_irq(irq, rtclong1_interrupt, SA_INTERRUPT,
-                                    "rtclong1", pdev);
+       aie_irq = platform_get_irq(pdev, 0);
+       if (aie_irq <= 0) {
+               retval = -EBUSY;
+               goto err_device_unregister;
        }
 
-       if (retval < 0) {
-               printk(KERN_ERR "rtc: IRQ%d is busy\n", irq);
-               rtc_device_unregister(rtc);
-               if (irq == RTCLONG1_IRQ)
-                       free_irq(ELAPSEDTIME_IRQ, NULL);
-               iounmap(rtc1_base);
-               iounmap(rtc2_base);
-               rtc1_base = NULL;
-               rtc2_base = NULL;
-               return retval;
-       }
+       retval = request_irq(aie_irq, elapsedtime_interrupt, IRQF_DISABLED,
+                            "elapsed_time", pdev);
+       if (retval < 0)
+               goto err_device_unregister;
+
+       pie_irq = platform_get_irq(pdev, 1);
+       if (pie_irq <= 0)
+               goto err_free_irq;
+
+       retval = request_irq(pie_irq, rtclong1_interrupt, IRQF_DISABLED,
+                            "rtclong1", pdev);
+       if (retval < 0)
+               goto err_free_irq;
 
        platform_set_drvdata(pdev, rtc);
 
-       disable_irq(ELAPSEDTIME_IRQ);
-       disable_irq(RTCLONG1_IRQ);
+       disable_irq(aie_irq);
+       disable_irq(pie_irq);
 
        printk(KERN_INFO "rtc: Real Time Clock of NEC VR4100 series\n");
 
        return 0;
+
+err_free_irq:
+       free_irq(aie_irq, pdev);
+
+err_device_unregister:
+       rtc_device_unregister(rtc);
+
+err_iounmap_all:
+       iounmap(rtc2_base);
+       rtc2_base = NULL;
+
+err_rtc1_iounmap:
+       iounmap(rtc1_base);
+       rtc1_base = NULL;
+
+       return retval;
 }
 
 static int __devexit rtc_remove(struct platform_device *pdev)
@@ -388,22 +412,23 @@ static int __devexit rtc_remove(struct platform_device *pdev)
        struct rtc_device *rtc;
 
        rtc = platform_get_drvdata(pdev);
-       if (rtc != NULL)
+       if (rtc)
                rtc_device_unregister(rtc);
 
        platform_set_drvdata(pdev, NULL);
 
-       free_irq(ELAPSEDTIME_IRQ, NULL);
-       free_irq(RTCLONG1_IRQ, NULL);
-       if (rtc1_base != NULL)
+       free_irq(aie_irq, pdev);
+       free_irq(pie_irq, pdev);
+       if (rtc1_base)
                iounmap(rtc1_base);
-       if (rtc2_base != NULL)
+       if (rtc2_base)
                iounmap(rtc2_base);
 
        return 0;
 }
 
-static struct platform_device *rtc_platform_device;
+/* work with hotplug and coldplug */
+MODULE_ALIAS("platform:RTC");
 
 static struct platform_driver rtc_platform_driver = {
        .probe          = rtc_probe,
@@ -416,55 +441,12 @@ static struct platform_driver rtc_platform_driver = {
 
 static int __init vr41xx_rtc_init(void)
 {
-       int retval;
-
-       switch (current_cpu_data.cputype) {
-       case CPU_VR4111:
-       case CPU_VR4121:
-               rtc_resource[0].start = RTC1_TYPE1_START;
-               rtc_resource[0].end = RTC1_TYPE1_END;
-               rtc_resource[1].start = RTC2_TYPE1_START;
-               rtc_resource[1].end = RTC2_TYPE1_END;
-               break;
-       case CPU_VR4122:
-       case CPU_VR4131:
-       case CPU_VR4133:
-               rtc_resource[0].start = RTC1_TYPE2_START;
-               rtc_resource[0].end = RTC1_TYPE2_END;
-               rtc_resource[1].start = RTC2_TYPE2_START;
-               rtc_resource[1].end = RTC2_TYPE2_END;
-               break;
-       default:
-               return -ENODEV;
-               break;
-       }
-
-       rtc_platform_device = platform_device_alloc("RTC", -1);
-       if (rtc_platform_device == NULL)
-               return -ENOMEM;
-
-       retval = platform_device_add_resources(rtc_platform_device,
-                               rtc_resource, ARRAY_SIZE(rtc_resource));
-
-       if (retval == 0)
-               retval = platform_device_add(rtc_platform_device);
-
-       if (retval < 0) {
-               platform_device_put(rtc_platform_device);
-               return retval;
-       }
-
-       retval = platform_driver_register(&rtc_platform_driver);
-       if (retval < 0)
-               platform_device_unregister(rtc_platform_device);
-
-       return retval;
+       return platform_driver_register(&rtc_platform_driver);
 }
 
 static void __exit vr41xx_rtc_exit(void)
 {
        platform_driver_unregister(&rtc_platform_driver);
-       platform_device_unregister(rtc_platform_device);
 }
 
 module_init(vr41xx_rtc_init);