UPSTREAM Revert 3.9 "rtc: palmas: Add RTC driver Palmas series PMIC"
[linux-3.10.git] / drivers / rtc / rtc-cmos.c
index de632e7..f1cb706 100644 (file)
@@ -37,6 +37,8 @@
 #include <linux/mod_devicetable.h>
 #include <linux/log2.h>
 #include <linux/pm.h>
+#include <linux/of.h>
+#include <linux/of_platform.h>
 
 /* this is for "generic access to PC-style RTC" using CMOS_READ/CMOS_WRITE */
 #include <asm-generic/rtc.h>
@@ -162,7 +164,7 @@ static inline unsigned char cmos_read_bank2(unsigned char addr)
 static inline void cmos_write_bank2(unsigned char val, unsigned char addr)
 {
        outb(addr, RTC_PORT(2));
-       outb(val, RTC_PORT(2));
+       outb(val, RTC_PORT(3));
 }
 
 #else
@@ -375,31 +377,6 @@ static int cmos_set_alarm(struct device *dev, struct rtc_wkalrm *t)
        return 0;
 }
 
-static int cmos_irq_set_freq(struct device *dev, int freq)
-{
-       struct cmos_rtc *cmos = dev_get_drvdata(dev);
-       int             f;
-       unsigned long   flags;
-
-       if (!is_valid_irq(cmos->irq))
-               return -ENXIO;
-
-       if (!is_power_of_2(freq))
-               return -EINVAL;
-       /* 0 = no irqs; 1 = 2^15 Hz ... 15 = 2^0 Hz */
-       f = ffs(freq);
-       if (f-- > 16)
-               return -EINVAL;
-       f = 16 - f;
-
-       spin_lock_irqsave(&rtc_lock, flags);
-       hpet_set_periodic_freq(freq);
-       CMOS_WRITE(RTC_REF_CLCK_32KHZ | f, RTC_FREQ_SELECT);
-       spin_unlock_irqrestore(&rtc_lock, flags);
-
-       return 0;
-}
-
 static int cmos_alarm_irq_enable(struct device *dev, unsigned int enabled)
 {
        struct cmos_rtc *cmos = dev_get_drvdata(dev);
@@ -419,25 +396,6 @@ static int cmos_alarm_irq_enable(struct device *dev, unsigned int enabled)
        return 0;
 }
 
-static int cmos_update_irq_enable(struct device *dev, unsigned int enabled)
-{
-       struct cmos_rtc *cmos = dev_get_drvdata(dev);
-       unsigned long   flags;
-
-       if (!is_valid_irq(cmos->irq))
-               return -EINVAL;
-
-       spin_lock_irqsave(&rtc_lock, flags);
-
-       if (enabled)
-               cmos_irq_enable(cmos, RTC_UIE);
-       else
-               cmos_irq_disable(cmos, RTC_UIE);
-
-       spin_unlock_irqrestore(&rtc_lock, flags);
-       return 0;
-}
-
 #if defined(CONFIG_RTC_INTF_PROC) || defined(CONFIG_RTC_INTF_PROC_MODULE)
 
 static int cmos_procfs(struct device *dev, struct seq_file *seq)
@@ -482,9 +440,7 @@ static const struct rtc_class_ops cmos_rtc_ops = {
        .read_alarm             = cmos_read_alarm,
        .set_alarm              = cmos_set_alarm,
        .proc                   = cmos_procfs,
-       .irq_set_freq           = cmos_irq_set_freq,
        .alarm_irq_enable       = cmos_alarm_irq_enable,
-       .update_irq_enable      = cmos_update_irq_enable,
 };
 
 /*----------------------------------------------------------------*/
@@ -650,7 +606,7 @@ cmos_do_probe(struct device *dev, struct resource *ports, int rtc_irq)
         * (needing ioremap etc), not i/o space resources like this ...
         */
        ports = request_region(ports->start,
-                       ports->end + 1 - ports->start,
+                       resource_size(ports),
                        driver_name);
        if (!ports) {
                dev_dbg(dev, "i/o registers already in use\n");
@@ -750,7 +706,7 @@ cmos_do_probe(struct device *dev, struct resource *ports, int rtc_irq)
                        rtc_cmos_int_handler = hpet_rtc_interrupt;
                        err = hpet_register_irq_handler(cmos_interrupt);
                        if (err != 0) {
-                               printk(KERN_WARNING "hpet_register_irq_handler "
+                               dev_warn(dev, "hpet_register_irq_handler "
                                                " failed in rtc_init().");
                                goto cleanup1;
                        }
@@ -758,7 +714,7 @@ cmos_do_probe(struct device *dev, struct resource *ports, int rtc_irq)
                        rtc_cmos_int_handler = cmos_interrupt;
 
                retval = request_irq(rtc_irq, rtc_cmos_int_handler,
-                               IRQF_DISABLED, dev_name(&cmos_rtc.rtc->dev),
+                               0, dev_name(&cmos_rtc.rtc->dev),
                                cmos_rtc.rtc);
                if (retval < 0) {
                        dev_dbg(dev, "IRQ %d is already in use\n", rtc_irq);
@@ -775,8 +731,7 @@ cmos_do_probe(struct device *dev, struct resource *ports, int rtc_irq)
                goto cleanup2;
        }
 
-       pr_info("%s: %s%s, %zd bytes nvram%s\n",
-               dev_name(&cmos_rtc.rtc->dev),
+       dev_info(dev, "%s%s, %zd bytes nvram%s\n",
                !is_valid_irq(rtc_irq) ? "no alarms" :
                        cmos_rtc.mon_alrm ? "alarms up to one year" :
                        cmos_rtc.day_alrm ? "alarms up to one month" :
@@ -794,7 +749,7 @@ cleanup1:
        cmos_rtc.dev = NULL;
        rtc_device_unregister(cmos_rtc.rtc);
 cleanup0:
-       release_region(ports->start, ports->end + 1 - ports->start);
+       release_region(ports->start, resource_size(ports));
        return retval;
 }
 
@@ -823,7 +778,7 @@ static void __exit cmos_do_remove(struct device *dev)
        cmos->rtc = NULL;
 
        ports = cmos->iomem;
-       release_region(ports->start, ports->end + 1 - ports->start);
+       release_region(ports->start, resource_size(ports));
        cmos->iomem = NULL;
 
        cmos->dev = NULL;
@@ -849,9 +804,8 @@ static int cmos_suspend(struct device *dev)
                        mask = RTC_IRQMASK;
                tmp &= ~mask;
                CMOS_WRITE(tmp, RTC_CONTROL);
+               hpet_mask_rtc_irq_bit(mask);
 
-               /* shut down hpet emulation - we don't need it for alarm */
-               hpet_mask_rtc_irq_bit(RTC_PIE|RTC_AIE|RTC_UIE);
                cmos_checkintr(cmos, tmp);
        }
        spin_unlock_irq(&rtc_lock);
@@ -864,8 +818,7 @@ static int cmos_suspend(struct device *dev)
                        enable_irq_wake(cmos->irq);
        }
 
-       pr_debug("%s: suspend%s, ctrl %02x\n",
-                       dev_name(&cmos_rtc.rtc->dev),
+       dev_dbg(dev, "suspend%s, ctrl %02x\n",
                        (tmp & RTC_AIE) ? ", alarm may wake" : "",
                        tmp);
 
@@ -901,6 +854,9 @@ static int cmos_resume(struct device *dev)
                }
 
                spin_lock_irq(&rtc_lock);
+               if (device_may_wakeup(dev))
+                       hpet_rtc_timer_init();
+
                do {
                        CMOS_WRITE(tmp, RTC_CONTROL);
                        hpet_set_rtc_irq_bit(tmp & RTC_IRQMASK);
@@ -920,9 +876,7 @@ static int cmos_resume(struct device *dev)
                spin_unlock_irq(&rtc_lock);
        }
 
-       pr_debug("%s: resume, ctrl %02x\n",
-                       dev_name(&cmos_rtc.rtc->dev),
-                       tmp);
+       dev_dbg(dev, "resume, ctrl %02x\n", tmp);
 
        return 0;
 }
@@ -954,14 +908,17 @@ static inline int cmos_poweroff(struct device *dev)
 
 static u32 rtc_handler(void *context)
 {
+       struct device *dev = context;
+
+       pm_wakeup_event(dev, 0);
        acpi_clear_event(ACPI_EVENT_RTC);
        acpi_disable_event(ACPI_EVENT_RTC, 0);
        return ACPI_INTERRUPT_HANDLED;
 }
 
-static inline void rtc_wake_setup(void)
+static inline void rtc_wake_setup(struct device *dev)
 {
-       acpi_install_fixed_event_handler(ACPI_EVENT_RTC, rtc_handler, NULL);
+       acpi_install_fixed_event_handler(ACPI_EVENT_RTC, rtc_handler, dev);
        /*
         * After the RTC handler is installed, the Fixed_RTC event should
         * be disabled. Only when the RTC alarm is set will it be enabled.
@@ -988,13 +945,12 @@ static void rtc_wake_off(struct device *dev)
  */
 static struct cmos_rtc_board_info acpi_rtc_info;
 
-static void __devinit
-cmos_wake_setup(struct device *dev)
+static void cmos_wake_setup(struct device *dev)
 {
        if (acpi_disabled)
                return;
 
-       rtc_wake_setup();
+       rtc_wake_setup(dev);
        acpi_rtc_info.wake_on = rtc_wake_on;
        acpi_rtc_info.wake_off = rtc_wake_off;
 
@@ -1021,8 +977,7 @@ cmos_wake_setup(struct device *dev)
 
 #else
 
-static void __devinit
-cmos_wake_setup(struct device *dev)
+static void cmos_wake_setup(struct device *dev)
 {
 }
 
@@ -1032,8 +987,7 @@ cmos_wake_setup(struct device *dev)
 
 #include <linux/pnp.h>
 
-static int __devinit
-cmos_pnp_probe(struct pnp_dev *pnp, const struct pnp_device_id *id)
+static int cmos_pnp_probe(struct pnp_dev *pnp, const struct pnp_device_id *id)
 {
        cmos_wake_setup(&pnp->dev);
 
@@ -1103,6 +1057,46 @@ static struct pnp_driver cmos_pnp_driver = {
 
 #endif /* CONFIG_PNP */
 
+#ifdef CONFIG_OF
+static const struct of_device_id of_cmos_match[] = {
+       {
+               .compatible = "motorola,mc146818",
+       },
+       { },
+};
+MODULE_DEVICE_TABLE(of, of_cmos_match);
+
+static __init void cmos_of_init(struct platform_device *pdev)
+{
+       struct device_node *node = pdev->dev.of_node;
+       struct rtc_time time;
+       int ret;
+       const __be32 *val;
+
+       if (!node)
+               return;
+
+       val = of_get_property(node, "ctrl-reg", NULL);
+       if (val)
+               CMOS_WRITE(be32_to_cpup(val), RTC_CONTROL);
+
+       val = of_get_property(node, "freq-reg", NULL);
+       if (val)
+               CMOS_WRITE(be32_to_cpup(val), RTC_FREQ_SELECT);
+
+       get_rtc_time(&time);
+       ret = rtc_valid_tm(&time);
+       if (ret) {
+               struct rtc_time def_time = {
+                       .tm_year = 1,
+                       .tm_mday = 1,
+               };
+               set_rtc_time(&def_time);
+       }
+}
+#else
+static inline void cmos_of_init(struct platform_device *pdev) {}
+#endif
 /*----------------------------------------------------------------*/
 
 /* Platform setup should have set up an RTC device, when PNP is
@@ -1111,6 +1105,7 @@ static struct pnp_driver cmos_pnp_driver = {
 
 static int __init cmos_platform_probe(struct platform_device *pdev)
 {
+       cmos_of_init(pdev);
        cmos_wake_setup(&pdev->dev);
        return cmos_do_probe(&pdev->dev,
                        platform_get_resource(pdev, IORESOURCE_IO, 0),
@@ -1142,6 +1137,7 @@ static struct platform_driver cmos_platform_driver = {
 #ifdef CONFIG_PM
                .pm             = &cmos_pm_ops,
 #endif
+               .of_match_table = of_match_ptr(of_cmos_match),
        }
 };