summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorvenu byravarasu <vbyravarasu@nvidia.com>2011-03-03 17:05:36 +0530
committerDan Willemsen <dwillemsen@nvidia.com>2013-09-14 01:10:01 -0700
commit6a48bfd6e9c0efe034e4d8e36384fb0f7132c0ff (patch)
tree38d82ef82520bbd1a2a966e3570045301007050a
parente0120ba71e62001295149a0fab6a6db8311f5ccc (diff)
rtc: RTC driver for tps6591x
With this check in native driver for TPS6591x RTC is added to linux kernel. Original-Change-Id: I267cc11b1451b8e2a4117cdff0b808f9f8fcc42a Reviewed-on: http://git-master/r/20754 Tested-by: Venu Byravarasu <vbyravarasu@nvidia.com> Reviewed-by: Laxman Dewangan <ldewangan@nvidia.com> Original-Change-Id: If55c1d1d7ebdedace4f0dea0b19bbda088a6cb1e Rebase-Id: Rdd5b5c4b3c14b247608dffb5ee017051e802e5a1
-rw-r--r--drivers/rtc/Kconfig9
-rw-r--r--drivers/rtc/Makefile1
-rw-r--r--drivers/rtc/rtc-tps6591x.c500
3 files changed, 510 insertions, 0 deletions
diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig
index 0de99d04ef97..13deabef2598 100644
--- a/drivers/rtc/Kconfig
+++ b/drivers/rtc/Kconfig
@@ -1202,6 +1202,15 @@ config RTC_DRV_TILE
Enable support for the Linux driver side of the Tilera
hypervisor's real-time clock interface.
+config RTC_DRV_TPS6591x
+ tristate "TPS6591x RTC driver"
+ default RTC_CLASS
+ help
+ If you say yes here you get support for the TPS6591x RTC module.
+
+ This driver can also be built as a module. If so, the module
+ will be called rtc-tps6591x.
+
config RTC_DRV_PUV3
tristate "PKUnity v3 RTC support"
depends on ARCH_PUV3
diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile
index e608e8b098b8..b92a589cb79a 100644
--- a/drivers/rtc/Makefile
+++ b/drivers/rtc/Makefile
@@ -118,6 +118,7 @@ obj-$(CONFIG_RTC_DRV_SUN4V) += rtc-sun4v.o
obj-$(CONFIG_RTC_DRV_TEGRA) += rtc-tegra.o
obj-$(CONFIG_RTC_DRV_TEST) += rtc-test.o
obj-$(CONFIG_RTC_DRV_TILE) += rtc-tile.o
+obj-$(CONFIG_RTC_DRV_TPS6591x) += rtc-tps6591x.o
obj-$(CONFIG_RTC_DRV_TWL4030) += rtc-twl.o
obj-$(CONFIG_RTC_DRV_TPS6586X) += rtc-tps6586x.o
obj-$(CONFIG_RTC_DRV_TPS65910) += rtc-tps65910.o
diff --git a/drivers/rtc/rtc-tps6591x.c b/drivers/rtc/rtc-tps6591x.c
new file mode 100644
index 000000000000..5fba5cfe0812
--- /dev/null
+++ b/drivers/rtc/rtc-tps6591x.c
@@ -0,0 +1,500 @@
+/*
+ * drivers/rtc/rtc-tps6591x.c
+ *
+ * RTC driver for TI TPS6591x
+ *
+ * Copyright (c) 2011, NVIDIA Corporation.
+ *
+ * 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
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * 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.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+/* #define DEBUG 1 */
+/* #define VERBOSE_DEBUG 1 */
+
+#include <linux/device.h>
+#include <linux/err.h>
+#include <linux/init.h>
+#include <linux/kernel.h>
+#include <linux/mfd/tps6591x.h>
+#include <linux/platform_device.h>
+#include <linux/rtc.h>
+#include <linux/slab.h>
+#include <linux/module.h>
+
+#define RTC_CTRL 0x10
+#define RTC_STATUS 0x11
+#define RTC_SECONDS_REG 0x0
+#define RTC_ALARM 0x8
+#define RTC_INT 0x12
+#define RTC_INT_MASK 0x51
+#define RTC_RESET_STATUS 0x16
+
+struct tps6591x_rtc {
+ unsigned long epoch_start;
+ int irq;
+ struct rtc_device *rtc;
+ bool irq_en;
+};
+
+static int tps6591x_read_regs(struct device *dev, int reg, int len,
+ uint8_t *val)
+{
+ int ret;
+ ret = tps6591x_reads(dev->parent, reg, len, val);
+ if (ret < 0) {
+ dev_err(dev->parent, "\n %s failed reading from 0x%02x\n",
+ __func__, reg);
+ WARN_ON(1);
+ return ret;
+ }
+ return 0;
+}
+
+static int tps6591x_write_regs(struct device *dev, int reg, int len,
+ uint8_t *val)
+{
+ int ret;
+ ret = tps6591x_writes(dev->parent, reg, len, val);
+ if (ret < 0) {
+ dev_err(dev->parent, "\n %s failed writing\n", __func__);
+ WARN_ON(1);
+ return ret;
+ }
+
+ return 0;
+}
+
+static int tps6591x_rtc_valid_tm(struct rtc_time *tm)
+{
+ if (tm->tm_year <= 99
+ || ((unsigned)tm->tm_mon) >= 12
+ || tm->tm_mday < 1
+ || tm->tm_mday > rtc_month_days(tm->tm_mon, tm->tm_year + 1900)
+ || ((unsigned)tm->tm_hour) >= 24
+ || ((unsigned)tm->tm_min) >= 60
+ || ((unsigned)tm->tm_sec) >= 60)
+ return -EINVAL;
+ return 0;
+}
+
+static u8 dec2bcd(u8 dec)
+{
+ return ((dec/10)<<4)+(dec%10);
+}
+
+static u8 bcd2dec(u8 bcd)
+{
+ return (bcd >> 4)*10+(bcd & 0xF);
+}
+
+static void convert_bcd_to_decimal(u8 *buf, u8 len)
+{
+ int i = 0;
+ for (i = 0; i < len; i++)
+ buf[i] = bcd2dec(buf[i]);
+}
+
+static void convert_decimal_to_bcd(u8 *buf, u8 len)
+{
+ int i = 0;
+ for (i = 0; i < len; i++)
+ buf[i] = dec2bcd(buf[i]);
+}
+
+static void print_time(struct rtc_time *tm)
+{
+ printk(KERN_INFO "\n TPS6591x RTC Time : %d-%d-%d %d:%d:%d\n",
+ tm->tm_mday, tm->tm_mon, tm->tm_year,
+ tm->tm_hour, tm->tm_min , tm->tm_sec);
+}
+
+static int tps6591x_rtc_read_time(struct device *dev, struct rtc_time *tm)
+{
+ u8 buff[7];
+ int err;
+ err = tps6591x_read_regs(dev, RTC_SECONDS_REG, sizeof(buff), buff);
+ if (err < 0) {
+ dev_err(dev, "\n %s :: failed to read time\n", __FILE__);
+ return err;
+ }
+ convert_bcd_to_decimal(buff, sizeof(buff));
+ tm->tm_sec = buff[0];
+ tm->tm_min = buff[1];
+ tm->tm_hour = buff[2];
+ tm->tm_mday = buff[3];
+ tm->tm_mon = buff[4];
+ tm->tm_year = buff[5];
+ tm->tm_wday = buff[6];
+ print_time(tm);
+ return tps6591x_rtc_valid_tm(tm);
+}
+
+static int tps6591x_rtc_stop(struct device *dev)
+{
+ u8 reg = 0;
+ u8 retries = 0;
+ int err;
+ do {
+ err = tps6591x_read_regs(dev, RTC_CTRL, 1, &reg);
+ if (err < 0) {
+ dev_err(dev->parent, "\n failed to read RTC_CTRL reg\n");
+ return err;
+ }
+
+ /* clear STOP bit alone */
+ reg &= ~0x1;
+
+ err = tps6591x_write_regs(dev, RTC_CTRL, 1, &reg);
+ if (err < 0) {
+ dev_err(dev->parent, "\n failed to program RTC_CTRL reg\n");
+ return err;
+ }
+
+ err = tps6591x_read_regs(dev, RTC_STATUS, 1, &reg);
+ if (err < 0) {
+ dev_err(dev->parent, "\n failed to read RTC_CTRL reg\n");
+ return err;
+ }
+ /* FixMe: Is allowing up to 5 retries sufficient?? */
+ if (retries++ == 5) {
+ dev_err(dev->parent, "\n failed to stop RTC\n");
+ return -EBUSY;
+ }
+ } while (reg & 2);
+ return 0;
+}
+
+static int tps6591x_rtc_start(struct device *dev)
+{
+ u8 reg = 0;
+ u8 retries = 0;
+ int err;
+
+ do {
+ err = tps6591x_read_regs(dev, RTC_CTRL, 1, &reg);
+ if (err < 0) {
+ dev_err(dev->parent, "\n failed to read RTC_CTRL reg\n");
+ return err;
+ }
+
+ /* set STOP bit alone */
+ reg |= 0x1;
+
+ err = tps6591x_write_regs(dev, RTC_CTRL, 1, &reg);
+ if (err < 0) {
+ dev_err(dev->parent, "\n failed to program RTC_CTRL reg\n");
+ return err;
+ }
+
+ err = tps6591x_read_regs(dev, RTC_STATUS, 1, &reg);
+ if (err < 0) {
+ dev_err(dev->parent, "\n failed to read RTC_CTRL reg\n");
+ return err;
+ }
+ /* FixMe: Is allowing up to 5 retries sufficient?? */
+ if (retries++ == 5) {
+ dev_err(dev->parent, "\n failed to stop RTC\n");
+ return -EBUSY;
+ }
+ } while (!(reg & 2));
+ return 0;
+}
+
+
+static int tps6591x_rtc_set_time(struct device *dev, struct rtc_time *tm)
+{
+ u8 buff[7];
+ int err;
+
+ buff[0] = tm->tm_sec;
+ buff[1] = tm->tm_min;
+ buff[2] = tm->tm_hour;
+ buff[3] = tm->tm_mday;
+ buff[4] = tm->tm_mon;
+ buff[5] = tm->tm_year;
+ buff[6] = tm->tm_wday;
+
+ print_time(tm);
+ convert_decimal_to_bcd(buff, sizeof(buff));
+ err = tps6591x_rtc_stop(dev);
+ if (err < 0) {
+ dev_err(dev->parent, "\n failed to clear RTC_ENABLE\n");
+ return err;
+ }
+
+ err = tps6591x_write_regs(dev, RTC_SECONDS_REG, sizeof(buff), buff);
+ if (err < 0) {
+ dev_err(dev->parent, "\n failed to program new time\n");
+ return err;
+ }
+
+ err = tps6591x_rtc_start(dev);
+ if (err < 0) {
+ dev_err(dev->parent, "\n failed to set RTC_ENABLE\n");
+ return err;
+ }
+
+ return 0;
+}
+
+static int tps6591x_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
+{
+ struct tps6591x_rtc *rtc = dev_get_drvdata(dev);
+ unsigned long seconds;
+ u8 buff[6];
+ int err;
+ struct rtc_time tm;
+
+ if (rtc->irq == -1)
+ return -EIO;
+
+ dev_info(dev->parent, "\n setting alarm to requested time::\n");
+ print_time(&alrm->time);
+ rtc_tm_to_time(&alrm->time, &seconds);
+ tps6591x_rtc_read_time(dev, &tm);
+ rtc_tm_to_time(&tm, &rtc->epoch_start);
+
+ if (WARN_ON(alrm->enabled && (seconds < rtc->epoch_start))) {
+ dev_err(dev->parent, "\n can't set alarm to requested time\n");
+ return -EINVAL;
+ }
+
+ if (alrm->enabled && !rtc->irq_en) {
+ enable_irq(rtc->irq);
+ rtc->irq_en = true;
+ } else if (!alrm->enabled && rtc->irq_en) {
+ disable_irq(rtc->irq);
+ rtc->irq_en = false;
+ }
+
+ buff[0] = alrm->time.tm_sec;
+ buff[1] = alrm->time.tm_min;
+ buff[2] = alrm->time.tm_hour;
+ buff[3] = alrm->time.tm_mday;
+ buff[4] = alrm->time.tm_mon;
+ buff[5] = alrm->time.tm_year;
+ convert_decimal_to_bcd(buff, sizeof(buff));
+ err = tps6591x_write_regs(dev, RTC_ALARM, sizeof(buff), buff);
+ if (err)
+ dev_err(dev->parent, "\n unable to program alarm\n");
+
+ return err;
+}
+
+static int tps6591x_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
+{
+ u8 buff[6];
+ int err;
+
+ err = tps6591x_read_regs(dev, RTC_ALARM, sizeof(buff), buff);
+ if (err)
+ return err;
+ convert_bcd_to_decimal(buff, sizeof(buff));
+
+ alrm->time.tm_sec = buff[0];
+ alrm->time.tm_min = buff[1];
+ alrm->time.tm_hour = buff[2];
+ alrm->time.tm_mday = buff[3];
+ alrm->time.tm_mon = buff[4];
+ alrm->time.tm_year = buff[5];
+
+ dev_info(dev->parent, "\n getting alarm time::\n");
+ print_time(&alrm->time);
+
+ return 0;
+}
+
+static int tps6591x_rtc_alarm_irq_enable(struct device *dev,
+ unsigned int enable)
+{
+ struct tps6591x_rtc *rtc = dev_get_drvdata(dev);
+ u8 reg;
+ int err;
+
+ if (rtc->irq == -1)
+ return -EIO;
+
+ if (enable) {
+ if (rtc->irq_en == true)
+ return 0;
+ err = tps6591x_read_regs(dev, RTC_INT, 1, &reg);
+ if (err)
+ return err;
+ reg |= 0x8;
+ err = tps6591x_write_regs(dev, RTC_INT, 1, &reg);
+ if (err)
+ return err;
+ rtc->irq_en = true;
+ } else {
+ if (rtc->irq_en == false)
+ return 0;
+ err = tps6591x_read_regs(dev, RTC_INT, 1, &reg);
+ if (err)
+ return err;
+ reg &= ~0x8;
+ err = tps6591x_write_regs(dev, RTC_INT, 1, &reg);
+ if (err)
+ return err;
+ rtc->irq_en = false;
+ }
+ return 0;
+}
+
+static const struct rtc_class_ops tps6591x_rtc_ops = {
+ .read_time = tps6591x_rtc_read_time,
+ .set_time = tps6591x_rtc_set_time,
+ .set_alarm = tps6591x_rtc_set_alarm,
+ .read_alarm = tps6591x_rtc_read_alarm,
+ .alarm_irq_enable = tps6591x_rtc_alarm_irq_enable,
+};
+
+static irqreturn_t tps6591x_rtc_irq(int irq, void *data)
+{
+ struct device *dev = data;
+ struct tps6591x_rtc *rtc = dev_get_drvdata(dev);
+ u8 reg;
+ int err;
+
+ /* clear Alarm stray interrupt and Power_up status bits.*/
+ reg = 0xC0;
+ err = tps6591x_write_regs(dev, RTC_STATUS, 1, &reg);
+ if (err) {
+ dev_err(dev->parent, "unable to program RTC_STATUS reg\n");
+ return -EBUSY;
+ }
+
+ rtc_update_irq(rtc->rtc, 1, RTC_IRQF | RTC_AF);
+ return IRQ_HANDLED;
+}
+
+static int tps6591x_rtc_probe(struct platform_device *pdev)
+{
+ struct tps6591x_rtc_platform_data *pdata = pdev->dev.platform_data;
+ struct tps6591x_rtc *rtc;
+ int err;
+ u8 reg;
+
+ rtc = kzalloc(sizeof(*rtc), GFP_KERNEL);
+
+ if (!rtc)
+ return -ENOMEM;
+
+ rtc->irq = -1;
+
+ if (!pdata) {
+ dev_err(&pdev->dev, "no platform_data specified\n");
+ return -EINVAL;
+ }
+
+ if (pdata->irq < 0)
+ dev_err(&pdev->dev, "\n no IRQ specified, wakeup is disabled\n");
+
+ dev_set_drvdata(&pdev->dev, rtc);
+ rtc->rtc = rtc_device_register("rtc-tps6591x", &pdev->dev,
+ &tps6591x_rtc_ops, THIS_MODULE);
+
+ if (IS_ERR(rtc->rtc)) {
+ err = PTR_ERR(rtc->rtc);
+ goto fail;
+ }
+
+ if ((int)pdev && (int)&pdev->dev)
+ err = tps6591x_read_regs(&pdev->dev, RTC_STATUS, 1, &reg);
+ else {
+ dev_err(&pdev->dev, "\n %s Input params incorrect\n", __func__);
+ return -EBUSY;
+ }
+ if (err) {
+ dev_err(&pdev->dev, "\n %s unable to read status\n", __func__);
+ return -EBUSY;
+ }
+ if (reg & 0x80)
+ dev_info(&pdev->dev, "\n %s RTC reset occured\n", __func__);
+
+ /* cear Alarm stray interrupt and Power_up status bits.*/
+ reg = 0xC0;
+ tps6591x_write_regs(&pdev->dev, RTC_STATUS, 1, &reg);
+ if (err) {
+ dev_err(&pdev->dev, "unable to program RTC_STATUS reg\n");
+ return -EBUSY;
+ }
+
+ /* enable ALARM INT only */
+ reg = 0xBF;
+ tps6591x_write_regs(&pdev->dev, RTC_INT_MASK, 1, &reg);
+ if (err) {
+ dev_err(&pdev->dev, "unable to program Interrupt Mask reg\n");
+ return -EBUSY;
+ }
+
+ if (pdata && (pdata->irq >= 0)) {
+ rtc->irq = pdata->irq;
+ err = request_threaded_irq(pdata->irq, NULL, tps6591x_rtc_irq,
+ IRQF_ONESHOT, "rtc-tps6591x",
+ &pdev->dev);
+ if (err) {
+ dev_err(&pdev->dev, "request IRQ:%d fail\n", rtc->irq);
+ rtc->irq = -1;
+ } else {
+ device_init_wakeup(&pdev->dev, 1);
+ enable_irq_wake(rtc->irq);
+ disable_irq(rtc->irq);
+ }
+ }
+ return 0;
+
+fail:
+ if (!IS_ERR_OR_NULL(rtc->rtc))
+ rtc_device_unregister(rtc->rtc);
+ kfree(rtc);
+ return err;
+}
+
+static int tps6591x_rtc_remove(struct platform_device *pdev)
+{
+ struct tps6591x_rtc *rtc = dev_get_drvdata(&pdev->dev);
+
+ if (rtc->irq != -1)
+ free_irq(rtc->irq, rtc);
+ rtc_device_unregister(rtc->rtc);
+ kfree(rtc);
+ return 0;
+}
+
+static struct platform_driver tps6591x_rtc_driver = {
+ .driver = {
+ .name = "rtc-tps6591x",
+ .owner = THIS_MODULE,
+ },
+ .probe = tps6591x_rtc_probe,
+ .remove = tps6591x_rtc_remove,
+};
+
+static int __init tps6591x_rtc_init(void)
+{
+ return platform_driver_register(&tps6591x_rtc_driver);
+}
+module_init(tps6591x_rtc_init);
+
+static void __exit tps6591x_rtc_exit(void)
+{
+ platform_driver_unregister(&tps6591x_rtc_driver);
+}
+module_exit(tps6591x_rtc_exit);
+
+MODULE_DESCRIPTION("TI TPS6591x RTC driver");
+MODULE_AUTHOR("NVIDIA Corporation");
+MODULE_LICENSE("GPL");
+MODULE_ALIAS("platform:rtc-tps6591x");