rtc: Add max77660 rtc driver
[linux-3.10.git] / drivers / rtc / rtc-ds1553.c
index ecafbad..8c6c952 100644 (file)
 #include <linux/bcd.h>
 #include <linux/init.h>
 #include <linux/kernel.h>
+#include <linux/gfp.h>
 #include <linux/delay.h>
 #include <linux/jiffies.h>
 #include <linux/interrupt.h>
 #include <linux/rtc.h>
 #include <linux/platform_device.h>
 #include <linux/io.h>
+#include <linux/module.h>
 
-#define DRV_VERSION "0.1"
+#define DRV_VERSION "0.3"
 
 #define RTC_REG_SIZE           0x2000
 #define RTC_OFFSET             0x1ff0
@@ -61,7 +63,6 @@
 struct rtc_plat_data {
        struct rtc_device *rtc;
        void __iomem *ioaddr;
-       unsigned long baseaddr;
        unsigned long last_jiffies;
        int irq;
        unsigned int irqen;
@@ -69,6 +70,7 @@ struct rtc_plat_data {
        int alrm_min;
        int alrm_hour;
        int alrm_mday;
+       spinlock_t lock;
 };
 
 static int ds1553_rtc_set_time(struct device *dev, struct rtc_time *tm)
@@ -78,17 +80,17 @@ static int ds1553_rtc_set_time(struct device *dev, struct rtc_time *tm)
        void __iomem *ioaddr = pdata->ioaddr;
        u8 century;
 
-       century = BIN2BCD((tm->tm_year + 1900) / 100);
+       century = bin2bcd((tm->tm_year + 1900) / 100);
 
        writeb(RTC_WRITE, pdata->ioaddr + RTC_CONTROL);
 
-       writeb(BIN2BCD(tm->tm_year % 100), ioaddr + RTC_YEAR);
-       writeb(BIN2BCD(tm->tm_mon + 1), ioaddr + RTC_MONTH);
-       writeb(BIN2BCD(tm->tm_wday) & RTC_DAY_MASK, ioaddr + RTC_DAY);
-       writeb(BIN2BCD(tm->tm_mday), ioaddr + RTC_DATE);
-       writeb(BIN2BCD(tm->tm_hour), ioaddr + RTC_HOURS);
-       writeb(BIN2BCD(tm->tm_min), ioaddr + RTC_MINUTES);
-       writeb(BIN2BCD(tm->tm_sec) & RTC_SECONDS_MASK, ioaddr + RTC_SECONDS);
+       writeb(bin2bcd(tm->tm_year % 100), ioaddr + RTC_YEAR);
+       writeb(bin2bcd(tm->tm_mon + 1), ioaddr + RTC_MONTH);
+       writeb(bin2bcd(tm->tm_wday) & RTC_DAY_MASK, ioaddr + RTC_DAY);
+       writeb(bin2bcd(tm->tm_mday), ioaddr + RTC_DATE);
+       writeb(bin2bcd(tm->tm_hour), ioaddr + RTC_HOURS);
+       writeb(bin2bcd(tm->tm_min), ioaddr + RTC_MINUTES);
+       writeb(bin2bcd(tm->tm_sec) & RTC_SECONDS_MASK, ioaddr + RTC_SECONDS);
 
        /* RTC_CENTURY and RTC_CONTROL share same register */
        writeb(RTC_WRITE | (century & RTC_CENTURY_MASK), ioaddr + RTC_CENTURY);
@@ -118,14 +120,14 @@ static int ds1553_rtc_read_time(struct device *dev, struct rtc_time *tm)
        year = readb(ioaddr + RTC_YEAR);
        century = readb(ioaddr + RTC_CENTURY) & RTC_CENTURY_MASK;
        writeb(0, ioaddr + RTC_CONTROL);
-       tm->tm_sec = BCD2BIN(second);
-       tm->tm_min = BCD2BIN(minute);
-       tm->tm_hour = BCD2BIN(hour);
-       tm->tm_mday = BCD2BIN(day);
-       tm->tm_wday = BCD2BIN(week);
-       tm->tm_mon = BCD2BIN(month) - 1;
+       tm->tm_sec = bcd2bin(second);
+       tm->tm_min = bcd2bin(minute);
+       tm->tm_hour = bcd2bin(hour);
+       tm->tm_mday = bcd2bin(day);
+       tm->tm_wday = bcd2bin(week);
+       tm->tm_mon = bcd2bin(month) - 1;
        /* year is 1900 + tm->tm_year */
-       tm->tm_year = BCD2BIN(year) + BCD2BIN(century) * 100 - 1900;
+       tm->tm_year = bcd2bin(year) + bcd2bin(century) * 100 - 1900;
 
        if (rtc_valid_tm(tm) < 0) {
                dev_err(dev, "retrieved date/time is not valid.\n");
@@ -139,22 +141,22 @@ static void ds1553_rtc_update_alarm(struct rtc_plat_data *pdata)
        void __iomem *ioaddr = pdata->ioaddr;
        unsigned long flags;
 
-       spin_lock_irqsave(&pdata->rtc->irq_lock, flags);
+       spin_lock_irqsave(&pdata->lock, flags);
        writeb(pdata->alrm_mday < 0 || (pdata->irqen & RTC_UF) ?
-              0x80 : BIN2BCD(pdata->alrm_mday),
+              0x80 : bin2bcd(pdata->alrm_mday),
               ioaddr + RTC_DATE_ALARM);
        writeb(pdata->alrm_hour < 0 || (pdata->irqen & RTC_UF) ?
-              0x80 : BIN2BCD(pdata->alrm_hour),
+              0x80 : bin2bcd(pdata->alrm_hour),
               ioaddr + RTC_HOURS_ALARM);
        writeb(pdata->alrm_min < 0 || (pdata->irqen & RTC_UF) ?
-              0x80 : BIN2BCD(pdata->alrm_min),
+              0x80 : bin2bcd(pdata->alrm_min),
               ioaddr + RTC_MINUTES_ALARM);
        writeb(pdata->alrm_sec < 0 || (pdata->irqen & RTC_UF) ?
-              0x80 : BIN2BCD(pdata->alrm_sec),
+              0x80 : bin2bcd(pdata->alrm_sec),
               ioaddr + RTC_SECONDS_ALARM);
        writeb(pdata->irqen ? RTC_INTS_AE : 0, ioaddr + RTC_INTERRUPTS);
        readb(ioaddr + RTC_FLAGS);      /* clear interrupts */
-       spin_unlock_irqrestore(&pdata->rtc->irq_lock, flags);
+       spin_unlock_irqrestore(&pdata->lock, flags);
 }
 
 static int ds1553_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
@@ -162,7 +164,7 @@ static int ds1553_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
        struct platform_device *pdev = to_platform_device(dev);
        struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
 
-       if (pdata->irq < 0)
+       if (pdata->irq <= 0)
                return -EINVAL;
        pdata->alrm_mday = alrm->time.tm_mday;
        pdata->alrm_hour = alrm->time.tm_hour;
@@ -179,7 +181,7 @@ static int ds1553_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
        struct platform_device *pdev = to_platform_device(dev);
        struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
 
-       if (pdata->irq < 0)
+       if (pdata->irq <= 0)
                return -EINVAL;
        alrm->time.tm_mday = pdata->alrm_mday < 0 ? 0 : pdata->alrm_mday;
        alrm->time.tm_hour = pdata->alrm_hour < 0 ? 0 : pdata->alrm_hour;
@@ -189,81 +191,57 @@ static int ds1553_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
        return 0;
 }
 
-static irqreturn_t ds1553_rtc_interrupt(int irq, void *dev_id,
-                                       struct pt_regs *regs)
+static irqreturn_t ds1553_rtc_interrupt(int irq, void *dev_id)
 {
        struct platform_device *pdev = dev_id;
        struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
        void __iomem *ioaddr = pdata->ioaddr;
-       unsigned long events = RTC_IRQF;
+       unsigned long events = 0;
 
+       spin_lock(&pdata->lock);
        /* read and clear interrupt */
-       if (!(readb(ioaddr + RTC_FLAGS) & RTC_FLAGS_AF))
-               return IRQ_NONE;
-       if (readb(ioaddr + RTC_SECONDS_ALARM) & 0x80)
-               events |= RTC_UF;
-       else
-               events |= RTC_AF;
-       rtc_update_irq(&pdata->rtc->class_dev, 1, events);
-       return IRQ_HANDLED;
-}
-
-static void ds1553_rtc_release(struct device *dev)
-{
-       struct platform_device *pdev = to_platform_device(dev);
-       struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
-
-       if (pdata->irq >= 0) {
-               pdata->irqen = 0;
-               ds1553_rtc_update_alarm(pdata);
+       if (readb(ioaddr + RTC_FLAGS) & RTC_FLAGS_AF) {
+               events = RTC_IRQF;
+               if (readb(ioaddr + RTC_SECONDS_ALARM) & 0x80)
+                       events |= RTC_UF;
+               else
+                       events |= RTC_AF;
+               if (likely(pdata->rtc))
+                       rtc_update_irq(pdata->rtc, 1, events);
        }
+       spin_unlock(&pdata->lock);
+       return events ? IRQ_HANDLED : IRQ_NONE;
 }
 
-static int ds1553_rtc_ioctl(struct device *dev, unsigned int cmd,
-                           unsigned long arg)
+static int ds1553_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
 {
        struct platform_device *pdev = to_platform_device(dev);
        struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
 
-       if (pdata->irq < 0)
-               return -ENOIOCTLCMD;
-       switch (cmd) {
-       case RTC_AIE_OFF:
-               pdata->irqen &= ~RTC_AF;
-               ds1553_rtc_update_alarm(pdata);
-               break;
-       case RTC_AIE_ON:
+       if (pdata->irq <= 0)
+               return -EINVAL;
+       if (enabled)
                pdata->irqen |= RTC_AF;
-               ds1553_rtc_update_alarm(pdata);
-               break;
-       case RTC_UIE_OFF:
-               pdata->irqen &= ~RTC_UF;
-               ds1553_rtc_update_alarm(pdata);
-               break;
-       case RTC_UIE_ON:
-               pdata->irqen |= RTC_UF;
-               ds1553_rtc_update_alarm(pdata);
-               break;
-       default:
-               return -ENOIOCTLCMD;
-       }
+       else
+               pdata->irqen &= ~RTC_AF;
+       ds1553_rtc_update_alarm(pdata);
        return 0;
 }
 
-static struct rtc_class_ops ds1553_rtc_ops = {
-       .read_time      = ds1553_rtc_read_time,
-       .set_time       = ds1553_rtc_set_time,
-       .read_alarm     = ds1553_rtc_read_alarm,
-       .set_alarm      = ds1553_rtc_set_alarm,
-       .release        = ds1553_rtc_release,
-       .ioctl          = ds1553_rtc_ioctl,
+static const struct rtc_class_ops ds1553_rtc_ops = {
+       .read_time              = ds1553_rtc_read_time,
+       .set_time               = ds1553_rtc_set_time,
+       .read_alarm             = ds1553_rtc_read_alarm,
+       .set_alarm              = ds1553_rtc_set_alarm,
+       .alarm_irq_enable       = ds1553_rtc_alarm_irq_enable,
 };
 
-static ssize_t ds1553_nvram_read(struct kobject *kobj, char *buf,
-                                loff_t pos, size_t size)
+static ssize_t ds1553_nvram_read(struct file *filp, struct kobject *kobj,
+                                struct bin_attribute *bin_attr,
+                                char *buf, loff_t pos, size_t size)
 {
-       struct platform_device *pdev =
-               to_platform_device(container_of(kobj, struct device, kobj));
+       struct device *dev = container_of(kobj, struct device, kobj);
+       struct platform_device *pdev = to_platform_device(dev);
        struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
        void __iomem *ioaddr = pdata->ioaddr;
        ssize_t count;
@@ -273,11 +251,12 @@ static ssize_t ds1553_nvram_read(struct kobject *kobj, char *buf,
        return count;
 }
 
-static ssize_t ds1553_nvram_write(struct kobject *kobj, char *buf,
-                                 loff_t pos, size_t size)
+static ssize_t ds1553_nvram_write(struct file *filp, struct kobject *kobj,
+                                 struct bin_attribute *bin_attr,
+                                 char *buf, loff_t pos, size_t size)
 {
-       struct platform_device *pdev =
-               to_platform_device(container_of(kobj, struct device, kobj));
+       struct device *dev = container_of(kobj, struct device, kobj);
+       struct platform_device *pdev = to_platform_device(dev);
        struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
        void __iomem *ioaddr = pdata->ioaddr;
        ssize_t count;
@@ -290,40 +269,35 @@ static ssize_t ds1553_nvram_write(struct kobject *kobj, char *buf,
 static struct bin_attribute ds1553_nvram_attr = {
        .attr = {
                .name = "nvram",
-               .mode = S_IRUGO | S_IWUGO,
-               .owner = THIS_MODULE,
+               .mode = S_IRUGO | S_IWUSR,
        },
        .size = RTC_OFFSET,
        .read = ds1553_nvram_read,
        .write = ds1553_nvram_write,
 };
 
-static int __init ds1553_rtc_probe(struct platform_device *pdev)
+static int ds1553_rtc_probe(struct platform_device *pdev)
 {
        struct rtc_device *rtc;
        struct resource *res;
        unsigned int cen, sec;
-       struct rtc_plat_data *pdata = NULL;
-       void __iomem *ioaddr = NULL;
+       struct rtc_plat_data *pdata;
+       void __iomem *ioaddr;
        int ret = 0;
 
        res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
        if (!res)
                return -ENODEV;
-       pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
+       pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
        if (!pdata)
                return -ENOMEM;
-       pdata->irq = -1;
-       if (!request_mem_region(res->start, RTC_REG_SIZE, pdev->name)) {
-               ret = -EBUSY;
-               goto out;
-       }
-       pdata->baseaddr = res->start;
-       ioaddr = ioremap(pdata->baseaddr, RTC_REG_SIZE);
-       if (!ioaddr) {
-               ret = -ENOMEM;
-               goto out;
-       }
+       if (!devm_request_mem_region(&pdev->dev, res->start, RTC_REG_SIZE,
+                       pdev->name))
+               return -EBUSY;
+
+       ioaddr = devm_ioremap(&pdev->dev, res->start, RTC_REG_SIZE);
+       if (!ioaddr)
+               return -ENOMEM;
        pdata->ioaddr = ioaddr;
        pdata->irq = platform_get_irq(pdev, 0);
 
@@ -339,74 +313,53 @@ static int __init ds1553_rtc_probe(struct platform_device *pdev)
        if (readb(ioaddr + RTC_FLAGS) & RTC_FLAGS_BLF)
                dev_warn(&pdev->dev, "voltage-low detected.\n");
 
-       if (pdata->irq >= 0) {
+       spin_lock_init(&pdata->lock);
+       pdata->last_jiffies = jiffies;
+       platform_set_drvdata(pdev, pdata);
+       if (pdata->irq > 0) {
                writeb(0, ioaddr + RTC_INTERRUPTS);
-               if (request_irq(pdata->irq, ds1553_rtc_interrupt, SA_SHIRQ,
-                               pdev->name, pdev) < 0) {
+               if (devm_request_irq(&pdev->dev, pdata->irq,
+                               ds1553_rtc_interrupt,
+                               0, pdev->name, pdev) < 0) {
                        dev_warn(&pdev->dev, "interrupt not available.\n");
-                       pdata->irq = -1;
+                       pdata->irq = 0;
                }
        }
 
-       rtc = rtc_device_register(pdev->name, &pdev->dev,
+       rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
                                  &ds1553_rtc_ops, THIS_MODULE);
-       if (IS_ERR(rtc)) {
-               ret = PTR_ERR(rtc);
-               goto out;
-       }
+       if (IS_ERR(rtc))
+               return PTR_ERR(rtc);
        pdata->rtc = rtc;
-       pdata->last_jiffies = jiffies;
-       platform_set_drvdata(pdev, pdata);
-       sysfs_create_bin_file(&pdev->dev.kobj, &ds1553_nvram_attr);
-       return 0;
- out:
-       if (pdata->irq >= 0)
-               free_irq(pdata->irq, pdev);
-       if (ioaddr)
-               iounmap(ioaddr);
-       if (pdata->baseaddr)
-               release_mem_region(pdata->baseaddr, RTC_REG_SIZE);
-       kfree(pdata);
+
+       ret = sysfs_create_bin_file(&pdev->dev.kobj, &ds1553_nvram_attr);
+
        return ret;
 }
 
-static int __devexit ds1553_rtc_remove(struct platform_device *pdev)
+static int ds1553_rtc_remove(struct platform_device *pdev)
 {
        struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
 
        sysfs_remove_bin_file(&pdev->dev.kobj, &ds1553_nvram_attr);
-       rtc_device_unregister(pdata->rtc);
-       if (pdata->irq >= 0) {
+       if (pdata->irq > 0)
                writeb(0, pdata->ioaddr + RTC_INTERRUPTS);
-               free_irq(pdata->irq, pdev);
-       }
-       iounmap(pdata->ioaddr);
-       release_mem_region(pdata->baseaddr, RTC_REG_SIZE);
-       kfree(pdata);
        return 0;
 }
 
+/* work with hotplug and coldplug */
+MODULE_ALIAS("platform:rtc-ds1553");
+
 static struct platform_driver ds1553_rtc_driver = {
        .probe          = ds1553_rtc_probe,
-       .remove         = __devexit_p(ds1553_rtc_remove),
+       .remove         = ds1553_rtc_remove,
        .driver         = {
-               .name   = "ds1553",
+               .name   = "rtc-ds1553",
                .owner  = THIS_MODULE,
        },
 };
 
-static __init int ds1553_init(void)
-{
-       return platform_driver_register(&ds1553_rtc_driver);
-}
-
-static __exit void ds1553_exit(void)
-{
-       return platform_driver_unregister(&ds1553_rtc_driver);
-}
-
-module_init(ds1553_init);
-module_exit(ds1553_exit);
+module_platform_driver(ds1553_rtc_driver);
 
 MODULE_AUTHOR("Atsushi Nemoto <anemo@mba.ocn.ne.jp>");
 MODULE_DESCRIPTION("Dallas DS1553 RTC driver");