rtc: palmas: clean-up rtc driver
Laxman Dewangan [Tue, 27 Aug 2013 09:53:47 +0000 (14:53 +0530)]
Clean-up and align the Palmas RTC driver to mainline.
Following are the changes:
- Remove intermediate read/write API and directly use the palmas_*
  APIs.
- Simplify the read-time/set-time/read-alarm and set-alarm
  implementation.
- Avoid duplication of code by using function.

Change-Id: I70a3c65312a21da170a5a0558a3cefd7d8f796bd
Signed-off-by: Laxman Dewangan <ldewangan@nvidia.com>
Reviewed-on: http://git-master/r/266677

drivers/rtc/rtc-palmas.c

index 4a20185..ae74754 100644 (file)
@@ -1,8 +1,13 @@
 /*
- * rtc-palmas.c -- Palmas Real Time Clock interface
+ * rtc-palmas.c -- Palmas Real Time Clock driver.
+
+ * RTC driver for TI Palma series devices like TPS65913,
+ * TPS65914 power management IC.
  *
  * Copyright (c) 2012 - 2013, NVIDIA CORPORATION.  All rights reserved.
+ *
  * Author: Kasoju Mallikarjun <mkasoju@nvidia.com>
+ * Author: Laxman Dewangan <ldewangan@nvidia.com>
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License as
@@ -10,7 +15,7 @@
  *
  * This program is distributed "as is" WITHOUT ANY WARRANTY of any kind,
  * whether express or implied; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
  * 02111-1307, USA
  */
 
-#include <linux/kernel.h>
+#include <linux/bcd.h>
 #include <linux/errno.h>
 #include <linux/init.h>
+#include <linux/interrupt.h>
+#include <linux/kernel.h>
+#include <linux/mfd/palmas.h>
 #include <linux/module.h>
-#include <linux/types.h>
+#include <linux/of.h>
 #include <linux/rtc.h>
-#include <linux/bcd.h>
+#include <linux/types.h>
 #include <linux/platform_device.h>
-#include <linux/interrupt.h>
-#include <linux/mfd/palmas.h>
+#include <linux/pm.h>
 
 struct palmas_rtc {
        struct rtc_device       *rtc;
-       /* To store the list of enabled interrupts */
-       unsigned int irqstat;
-       unsigned int irq;
+       struct device           *dev;
+       unsigned int            irq;
 };
 
 /* Total number of RTC registers needed to set time*/
-#define NUM_TIME_REGS  (PALMAS_YEARS_REG - PALMAS_SECONDS_REG + 1)
-
-int palmas_rtc_read(struct palmas *palmas, unsigned int reg,
-                               unsigned int *dest)
-{
-       unsigned int addr;
-       addr = PALMAS_BASE_TO_REG(PALMAS_RTC_BASE, reg);
-
-       return regmap_read(palmas->regmap[RTC_SLAVE], addr, dest);
-}
-
-int palmas_rtc_write(struct palmas *palmas, unsigned int reg,
-                               unsigned int value)
-{
-       unsigned int addr;
-       addr = PALMAS_BASE_TO_REG(PALMAS_RTC_BASE, reg);
-
-       return regmap_write(palmas->regmap[RTC_SLAVE], addr, value);
-}
-
-int palmas_rtc_bulk_read(struct palmas *palmas, unsigned int reg,
-                               void *val, size_t val_count)
-{
-       unsigned int addr;
-       addr = PALMAS_BASE_TO_REG(PALMAS_RTC_BASE, reg);
-
-       return regmap_bulk_read(palmas->regmap[RTC_SLAVE], addr,
-               val, val_count);
-}
-
-int palmas_rtc_bulk_write(struct palmas *palmas, unsigned int reg,
-       const void *val, size_t val_count)
-{
-       unsigned int addr;
-       addr = PALMAS_BASE_TO_REG(PALMAS_RTC_BASE, reg);
-
-       return regmap_bulk_write(palmas->regmap[RTC_SLAVE], addr,
-               val, val_count);
-}
-
-int palmas_rtc_update_bits(struct palmas *palmas, unsigned int reg,
-                               unsigned int mask, unsigned int val)
-{
-       unsigned int addr;
-       addr = PALMAS_BASE_TO_REG(PALMAS_RTC_BASE, reg);
-
-       return regmap_update_bits(palmas->regmap[RTC_SLAVE], addr, mask, val);
-}
+#define PALMAS_NUM_TIME_REGS   (PALMAS_YEARS_REG - PALMAS_SECONDS_REG + 1)
 
-static int palmas_rtc_alarm_irq_enable(struct device *dev, unsigned enabled)
-{
-       struct palmas *palmas = dev_get_drvdata(dev->parent);
-       u8 val = 0;
-
-       dev_dbg(dev, "%s(): enabled %u\n", __func__, enabled);
-
-       if (enabled)
-               val = PALMAS_RTC_INTERRUPTS_REG_IT_ALARM;
-
-       return palmas_rtc_write(palmas,
-               PALMAS_RTC_INTERRUPTS_REG, val);
-}
-
-/*
- * Gets current palmas RTC time and date parameters.
- *
- * The RTC's time/alarm representation is not what gmtime(3) requires
- * Linux to use:
- *
- *  - Months are 1..12 vs Linux 0-11
- *  - Years are 0..99 vs Linux 1900..N (we assume 21st century)
- */
 static int palmas_rtc_read_time(struct device *dev, struct rtc_time *tm)
 {
-       unsigned char rtc_data[NUM_TIME_REGS];
+       unsigned char rtc_data[PALMAS_NUM_TIME_REGS];
        struct palmas *palmas = dev_get_drvdata(dev->parent);
        int ret;
 
        /* Copy RTC counting registers to static registers or latches */
-       ret = palmas_rtc_update_bits(palmas, PALMAS_RTC_CTRL_REG,
+       ret = palmas_update_bits(palmas, PALMAS_RTC_BASE, PALMAS_RTC_CTRL_REG,
                PALMAS_RTC_CTRL_REG_GET_TIME, PALMAS_RTC_CTRL_REG_GET_TIME);
        if (ret < 0) {
-               dev_err(dev, "RTC CTRL reg update failed with err:%d\n", ret);
+               dev_err(dev, "RTC CTRL reg update failed, err: %d\n", ret);
                return ret;
        }
 
-       ret = palmas_rtc_bulk_read(palmas, PALMAS_SECONDS_REG,
-               rtc_data, NUM_TIME_REGS);
+       ret = palmas_bulk_read(palmas, PALMAS_RTC_BASE, PALMAS_SECONDS_REG,
+                       rtc_data, PALMAS_NUM_TIME_REGS);
        if (ret < 0) {
-               dev_err(dev, "reading from RTC failed with err:%d\n", ret);
+               dev_err(dev, "RTC_SECONDS reg read failed, err = %d\n", ret);
                return ret;
        }
 
@@ -146,7 +82,7 @@ static int palmas_rtc_read_time(struct device *dev, struct rtc_time *tm)
 
 static int palmas_rtc_set_time(struct device *dev, struct rtc_time *tm)
 {
-       unsigned char rtc_data[NUM_TIME_REGS];
+       unsigned char rtc_data[PALMAS_NUM_TIME_REGS];
        struct palmas *palmas = dev_get_drvdata(dev->parent);
        int ret;
 
@@ -162,44 +98,50 @@ static int palmas_rtc_set_time(struct device *dev, struct rtc_time *tm)
                tm->tm_hour, tm->tm_min, tm->tm_sec);
 
        /* Stop RTC while updating the RTC time registers */
-       ret = palmas_rtc_update_bits(palmas, PALMAS_RTC_CTRL_REG,
+       ret = palmas_update_bits(palmas, PALMAS_RTC_BASE, PALMAS_RTC_CTRL_REG,
                PALMAS_RTC_CTRL_REG_STOP_RTC, 0);
        if (ret < 0) {
-               dev_err(dev, "RTC stop failed with err:%d\n", ret);
+               dev_err(dev, "RTC stop failed, err = %d\n", ret);
                return ret;
        }
 
-       /* update all the time registers in one shot */
-       ret = palmas_rtc_bulk_write(palmas, PALMAS_SECONDS_REG,
-               rtc_data, NUM_TIME_REGS);
+       ret = palmas_bulk_write(palmas, PALMAS_RTC_BASE, PALMAS_SECONDS_REG,
+               rtc_data, PALMAS_NUM_TIME_REGS);
        if (ret < 0) {
-               dev_err(dev, "rtc_set_time error %d\n", ret);
+               dev_err(dev, "RTC_SECONDS reg write failed, err = %d\n", ret);
                return ret;
        }
 
        /* Start back RTC */
-       ret = palmas_rtc_update_bits(palmas, PALMAS_RTC_CTRL_REG,
-               PALMAS_RTC_CTRL_REG_STOP_RTC, 1);
+       ret = palmas_update_bits(palmas, PALMAS_RTC_BASE, PALMAS_RTC_CTRL_REG,
+               PALMAS_RTC_CTRL_REG_STOP_RTC, PALMAS_RTC_CTRL_REG_STOP_RTC);
        if (ret < 0)
-               dev_err(dev, "RTC start failed with err:%d\n", ret);
-
+               dev_err(dev, "RTC start failed, err = %d\n", ret);
        return ret;
 }
 
-/*
- * Gets current palmas RTC alarm time.
- */
+static int palmas_rtc_alarm_irq_enable(struct device *dev, unsigned enabled)
+{
+       struct palmas *palmas = dev_get_drvdata(dev->parent);
+       u8 val;
+
+       val = enabled ? PALMAS_RTC_INTERRUPTS_REG_IT_ALARM : 0;
+       return palmas_write(palmas, PALMAS_RTC_BASE,
+               PALMAS_RTC_INTERRUPTS_REG, val);
+}
+
 static int palmas_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
 {
-       unsigned char alarm_data[NUM_TIME_REGS];
+       unsigned char alarm_data[PALMAS_NUM_TIME_REGS];
        u32 int_val;
        struct palmas *palmas = dev_get_drvdata(dev->parent);
        int ret;
 
-       ret = palmas_rtc_bulk_read(palmas, PALMAS_ALARM_SECONDS_REG,
-               alarm_data, NUM_TIME_REGS);
+       ret = palmas_bulk_read(palmas, PALMAS_RTC_BASE,
+                       PALMAS_ALARM_SECONDS_REG,
+                       alarm_data, PALMAS_NUM_TIME_REGS);
        if (ret < 0) {
-               dev_err(dev, "rtc_read_alarm error %d\n", ret);
+               dev_err(dev, "RTC_ALARM_SECONDS read failed, err = %d\n", ret);
                return ret;
        }
 
@@ -214,27 +156,29 @@ static int palmas_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
                alm->time.tm_year, alm->time.tm_mon, alm->time.tm_mday,
                alm->time.tm_hour, alm->time.tm_min, alm->time.tm_sec);
 
-       ret = palmas_rtc_read(palmas, PALMAS_RTC_INTERRUPTS_REG,
-               &int_val);
-       if (ret < 0)
+       ret = palmas_read(palmas, PALMAS_RTC_BASE, PALMAS_RTC_INTERRUPTS_REG,
+                       &int_val);
+       if (ret < 0) {
+               dev_err(dev, "RTC_INTERRUPTS reg read failed, err = %d\n", ret);
                return ret;
+       }
 
        if (int_val & PALMAS_RTC_INTERRUPTS_REG_IT_ALARM)
                alm->enabled = 1;
-
        return ret;
 }
 
 static int palmas_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
 {
-       unsigned char alarm_data[NUM_TIME_REGS];
+       unsigned char alarm_data[PALMAS_NUM_TIME_REGS];
        struct palmas *palmas = dev_get_drvdata(dev->parent);
        int ret;
 
-       dev_dbg(dev, "%s()\n", __func__);
        ret = palmas_rtc_alarm_irq_enable(dev, 0);
-       if (ret)
+       if (ret < 0) {
+               dev_err(dev, "Disable RTC alarm failed\n");
                return ret;
+       }
 
        alarm_data[0] = bin2bcd(alm->time.tm_sec);
        alarm_data[1] = bin2bcd(alm->time.tm_min);
@@ -247,49 +191,53 @@ static int palmas_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
                alm->time.tm_year, alm->time.tm_mon, alm->time.tm_mday,
                alm->time.tm_hour, alm->time.tm_min, alm->time.tm_sec);
 
-       /* update all the alarm registers in one shot */
-       ret = palmas_rtc_bulk_write(palmas,
-               PALMAS_ALARM_SECONDS_REG, alarm_data, NUM_TIME_REGS);
-       if (ret) {
-               dev_err(dev, "rtc_set_alarm error %d\n", ret);
+       ret = palmas_bulk_write(palmas, PALMAS_RTC_BASE,
+               PALMAS_ALARM_SECONDS_REG, alarm_data, PALMAS_NUM_TIME_REGS);
+       if (ret < 0) {
+               dev_err(dev, "ALARM_SECONDS_REG write failed, err = %d\n", ret);
                return ret;
        }
 
        if (alm->enabled)
                ret = palmas_rtc_alarm_irq_enable(dev, 1);
-
        return ret;
 }
 
-static irqreturn_t palmas_rtc_interrupt(int irq, void *rtc)
+static int palmas_clear_interrupts(struct device *dev)
 {
-       struct device *dev = rtc;
-       unsigned long events = 0;
        struct palmas *palmas = dev_get_drvdata(dev->parent);
-       struct palmas_rtc *palmas_rtc = dev_get_drvdata(dev);
+       unsigned int rtc_reg;
        int ret;
-       u32 rtc_reg;
 
-       dev_dbg(dev, "RTC ISR\n");
-
-       ret = palmas_rtc_read(palmas, PALMAS_RTC_STATUS_REG,
-               &rtc_reg);
-       if (ret)
-               return IRQ_NONE;
+       ret = palmas_read(palmas, PALMAS_RTC_BASE, PALMAS_RTC_STATUS_REG,
+                               &rtc_reg);
+       if (ret < 0) {
+               dev_err(dev, "RTC_STATUS read failed, err = %d\n", ret);
+               return ret;
+       }
 
-       dev_dbg(dev, "RTC ISR status 0x%02x\n", rtc_reg);
+       ret = palmas_write(palmas, PALMAS_RTC_BASE, PALMAS_RTC_STATUS_REG,
+                       rtc_reg);
+       if (ret < 0) {
+               dev_err(dev, "RTC_STATUS write failed, err = %d\n", ret);
+               return ret;
+       }
+       return 0;
+}
 
-       if (rtc_reg & PALMAS_RTC_STATUS_REG_ALARM)
-               events = RTC_IRQF | RTC_AF;
+static irqreturn_t palmas_rtc_interrupt(int irq, void *context)
+{
+       struct palmas_rtc *palmas_rtc = context;
+       struct device *dev = palmas_rtc->dev;
+       int ret;
 
-       ret = palmas_rtc_write(palmas, PALMAS_RTC_STATUS_REG,
-               rtc_reg);
-       if (ret)
+       ret = palmas_clear_interrupts(dev);
+       if (ret < 0) {
+               dev_err(dev, "RTC interrupt clear failed, err = %d\n", ret);
                return IRQ_NONE;
+       }
 
-       /* Notify RTC core on event */
-       rtc_update_irq(palmas_rtc->rtc, 1, events);
-
+       rtc_update_irq(palmas_rtc->rtc, 1, RTC_IRQF | RTC_AF);
        return IRQ_HANDLED;
 }
 
@@ -303,14 +251,28 @@ static struct rtc_class_ops palmas_rtc_ops = {
 
 static int palmas_rtc_probe(struct platform_device *pdev)
 {
-       struct palmas *palmas = NULL;
+       struct palmas *palmas = dev_get_drvdata(pdev->dev.parent);
        struct palmas_rtc *palmas_rtc = NULL;
        struct palmas_platform_data *palmas_pdata;
        struct palmas_rtc_platform_data *rtc_pdata = NULL;
        int ret;
-       u32 rtc_reg;
+       bool enable_bb_charging = false;
+       bool high_bb_charging;
 
-       palmas = dev_get_drvdata(pdev->dev.parent);
+
+       palmas_pdata = dev_get_platdata(pdev->dev.parent);
+       if (palmas_pdata)
+               rtc_pdata = palmas_pdata->rtc_pdata;
+
+       if (rtc_pdata) {
+               enable_bb_charging = rtc_pdata->backup_battery_chargeable;
+               high_bb_charging = rtc_pdata->backup_battery_charge_high_current;
+       } else if (pdev->dev.of_node) {
+               enable_bb_charging = of_property_read_bool(pdev->dev.of_node,
+                                       "ti,backup-battery-chargeable");
+               high_bb_charging = of_property_read_bool(pdev->dev.of_node,
+                                       "ti,backup-battery-charge-high-current");
+       }
 
        palmas_rtc = devm_kzalloc(&pdev->dev, sizeof(struct palmas_rtc),
                        GFP_KERNEL);
@@ -319,125 +281,106 @@ static int palmas_rtc_probe(struct platform_device *pdev)
                return -ENOMEM;
        }
 
-       palmas_pdata = dev_get_platdata(pdev->dev.parent);
-       if (palmas_pdata)
-               rtc_pdata = palmas_pdata->rtc_pdata;
+       /* Clear pending interrupts */
+       ret = palmas_clear_interrupts(&pdev->dev);
+       if (ret < 0) {
+               dev_err(&pdev->dev, "clear RTC int failed, err = %d\n", ret);
+               return ret;
+       }
 
        palmas->rtc = palmas_rtc;
-       if (rtc_pdata && rtc_pdata->backup_battery_chargeable) {
-               int slave;
-               unsigned int addr;
-               int reg = 0;
+       palmas_rtc->dev = &pdev->dev;
+       platform_set_drvdata(pdev, palmas_rtc);
 
-               addr = PALMAS_BASE_TO_REG(PALMAS_PMU_CONTROL_BASE,
-                               PALMAS_BACKUP_BATTERY_CTRL);
-               slave = PALMAS_BASE_TO_SLAVE(PALMAS_PMU_CONTROL_BASE);
+       if (enable_bb_charging) {
+               unsigned reg = PALMAS_BACKUP_BATTERY_CTRL_BBS_BBC_LOW_ICHRG;
 
-               if (!rtc_pdata->backup_battery_charge_high_current)
-                       reg = PALMAS_BACKUP_BATTERY_CTRL_BBS_BBC_LOW_ICHRG;
+               if (high_bb_charging)
+                       reg = 0;
 
-               ret = regmap_update_bits(palmas->regmap[slave], addr,
-                               PALMAS_BACKUP_BATTERY_CTRL_BBS_BBC_LOW_ICHRG,
-                               reg);
+               ret = palmas_update_bits(palmas, PALMAS_PMU_CONTROL_BASE,
+                       PALMAS_BACKUP_BATTERY_CTRL,
+                       PALMAS_BACKUP_BATTERY_CTRL_BBS_BBC_LOW_ICHRG, reg);
                if (ret < 0) {
                        dev_err(&pdev->dev,
-                               "Battery backup control failed, e %d\n", ret);
+                               "BACKUP_BATTERY_CTRL update failed, %d\n", ret);
                        return ret;
-               };
+               }
 
-               ret = regmap_update_bits(palmas->regmap[slave], addr,
-                               PALMAS_BACKUP_BATTERY_CTRL_BB_CHG_EN,
-                               PALMAS_BACKUP_BATTERY_CTRL_BB_CHG_EN);
+               ret = palmas_update_bits(palmas, PALMAS_PMU_CONTROL_BASE,
+                       PALMAS_BACKUP_BATTERY_CTRL,
+                       PALMAS_BACKUP_BATTERY_CTRL_BB_CHG_EN,
+                       PALMAS_BACKUP_BATTERY_CTRL_BB_CHG_EN);
                if (ret < 0) {
                        dev_err(&pdev->dev,
-                               "Battery backup charging enable failed, e %d\n",
-                               ret);
+                               "BACKUP_BATTERY_CTRL update failed, %d\n", ret);
                        return ret;
                }
        }
 
-       ret = palmas_rtc_write(palmas, PALMAS_RTC_INTERRUPTS_REG, 0);
+       ret = palmas_write(palmas, PALMAS_RTC_BASE,
+                               PALMAS_RTC_INTERRUPTS_REG, 0);
        if (ret < 0) {
                dev_err(&pdev->dev, "RTC_INTERRUPTS_REG write failed: %d\n",
                                ret);
                return ret;
        }
 
-       /* Clear pending interrupts */
-       ret = palmas_rtc_read(palmas, PALMAS_RTC_STATUS_REG,
-               &rtc_reg);
-       if (ret < 0) {
-               dev_err(&pdev->dev, "rtc_read_status error %d\n", ret);
-               return ret;
-       }
-
-       ret = palmas_rtc_write(palmas, PALMAS_RTC_STATUS_REG,
-               rtc_reg);
-       if (ret < 0) {
-               dev_err(&pdev->dev, "rtc_clear_interupt error %d\n", ret);
-               return ret;
-       }
-
-       dev_dbg(&pdev->dev, "Enabling palmas-RTC.\n");
-       rtc_reg = PALMAS_RTC_CTRL_REG_STOP_RTC;
-       ret = palmas_rtc_write(palmas, PALMAS_RTC_CTRL_REG,
-               rtc_reg);
+       /* Start RTC */
+       ret = palmas_update_bits(palmas, PALMAS_RTC_BASE, PALMAS_RTC_CTRL_REG,
+                       PALMAS_RTC_CTRL_REG_STOP_RTC,
+                       PALMAS_RTC_CTRL_REG_STOP_RTC);
        if (ret < 0) {
-               dev_err(&pdev->dev, "rtc_enable error %d\n", ret);
+               dev_err(&pdev->dev, "RTC_CTRL write failed, err = %d\n", ret);
                return ret;
        }
 
        palmas_rtc->irq = palmas_irq_get_virq(palmas, PALMAS_RTC_ALARM_IRQ);
        dev_dbg(&pdev->dev, "RTC interrupt %d\n", palmas_rtc->irq);
-       ret = request_threaded_irq(palmas_rtc->irq, NULL,
-               palmas_rtc_interrupt, IRQF_TRIGGER_LOW | IRQF_ONESHOT |
-               IRQF_EARLY_RESUME,
-               "palmas-rtc", &pdev->dev);
-       if (ret < 0) {
-               dev_err(&pdev->dev, "IRQ is not free.\n");
-               return ret;
-       }
-       device_init_wakeup(&pdev->dev, 1);
-       platform_set_drvdata(pdev, palmas_rtc);
 
+       device_init_wakeup(&pdev->dev, 1);
        palmas_rtc->rtc = rtc_device_register(pdev->name, &pdev->dev,
                &palmas_rtc_ops, THIS_MODULE);
        if (IS_ERR(palmas_rtc->rtc)) {
                ret = PTR_ERR(palmas_rtc->rtc);
-               free_irq(palmas_rtc->irq, &pdev->dev);
-               dev_err(&pdev->dev, "RTC device register: err %d\n", ret);
+               dev_err(&pdev->dev, "RTC register failed, err = %d\n", ret);
+               return ret;
+       }
+
+       ret = request_threaded_irq(palmas_rtc->irq, NULL,
+                       palmas_rtc_interrupt,
+                       IRQF_TRIGGER_LOW | IRQF_ONESHOT |
+                       IRQF_EARLY_RESUME,
+                       dev_name(&pdev->dev), palmas_rtc);
+       if (ret < 0) {
+               dev_err(&pdev->dev, "IRQ request failed, err = %d\n", ret);
+               rtc_device_unregister(palmas_rtc->rtc);
                return ret;
        }
 
        return 0;
 }
 
-/*
- * Disable all palmas RTC module interrupts.
- * Sets status flag to free.
- */
 static int palmas_rtc_remove(struct platform_device *pdev)
 {
-       /* leave rtc running, but disable irqs */
-       struct palmas *palmas = dev_get_drvdata(pdev->dev.parent);
+       struct palmas_rtc *palmas_rtc = platform_get_drvdata(pdev);
 
-       palmas_rtc_alarm_irq_enable(&palmas->rtc->rtc->dev, 0);
-       free_irq(palmas->irq, &pdev->dev);
-       rtc_device_unregister(palmas->rtc->rtc);
+       palmas_rtc_alarm_irq_enable(&pdev->dev, 0);
+       free_irq(palmas_rtc->irq, palmas_rtc);
+       rtc_device_unregister(palmas_rtc->rtc);
        return 0;
 }
 
 #ifdef CONFIG_PM_SLEEP
 static int palmas_rtc_suspend(struct device *dev)
 {
-       struct platform_device *pdev = to_platform_device(dev);
-       struct palmas *palmas = dev_get_drvdata(pdev->dev.parent);
+       struct palmas_rtc *palmas_rtc = dev_get_drvdata(dev);
 
        if (device_may_wakeup(dev)) {
                int ret;
                struct rtc_wkalrm alm;
 
-               enable_irq_wake(palmas->rtc->irq);
+               enable_irq_wake(palmas_rtc->irq);
                ret = palmas_rtc_read_alarm(dev, &alm);
                if (!ret)
                        dev_info(dev, "%s() alrm %d time %d %d %d %d %d %d\n",
@@ -452,14 +395,13 @@ static int palmas_rtc_suspend(struct device *dev)
 
 static int palmas_rtc_resume(struct device *dev)
 {
-       struct platform_device *pdev = to_platform_device(dev);
-       struct palmas *palmas = dev_get_drvdata(pdev->dev.parent);
+       struct palmas_rtc *palmas_rtc = dev_get_drvdata(dev);
 
        if (device_may_wakeup(dev)) {
                struct rtc_time tm;
                int ret;
 
-               disable_irq_wake(palmas->rtc->irq);
+               disable_irq_wake(palmas_rtc->irq);
                ret = palmas_rtc_read_time(dev, &tm);
                if (!ret)
                        dev_info(dev, "%s() %d %d %d %d %d %d\n",
@@ -469,15 +411,18 @@ static int palmas_rtc_resume(struct device *dev)
 
        return 0;
 }
+#endif
 
 static const struct dev_pm_ops palmas_rtc_pm_ops = {
-       .suspend        = palmas_rtc_suspend,
-       .resume         = palmas_rtc_resume,
+       SET_SYSTEM_SLEEP_PM_OPS(palmas_rtc_suspend, palmas_rtc_resume)
 };
 
-#define DEV_PM_OPS     (&palmas_rtc_pm_ops)
-#else
-#define DEV_PM_OPS     NULL
+#ifdef CONFIG_OF
+static struct of_device_id of_palmas_rtc_match[] = {
+       { .compatible = "ti,palmas-rtc"},
+       { },
+};
+MODULE_DEVICE_TABLE(of, of_palmas_rtc_match);
 #endif
 
 static struct platform_driver palmas_rtc_driver = {
@@ -486,11 +431,15 @@ static struct platform_driver palmas_rtc_driver = {
        .driver         = {
                .owner  = THIS_MODULE,
                .name   = "palmas-rtc",
-               .pm     = DEV_PM_OPS,
+               .pm     = &palmas_rtc_pm_ops,
+               .of_match_table = of_match_ptr(of_palmas_rtc_match),
        },
 };
 
 module_platform_driver(palmas_rtc_driver);
+
 MODULE_ALIAS("platform:palmas_rtc");
+MODULE_DESCRIPTION("TI PALMAS series RTC driver");
 MODULE_AUTHOR("Kasoju Mallikarjun <mkasoju@nvidia.com>");
+MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
 MODULE_LICENSE("GPL v2");