diff options
author | venu byravarasu <vbyravarasu@nvidia.com> | 2011-12-14 17:14:15 +0530 |
---|---|---|
committer | Dan Willemsen <dwillemsen@nvidia.com> | 2013-09-14 01:10:10 -0700 |
commit | df89c52f34616b120764b1d8cb81bdb1b8183cb9 (patch) | |
tree | 9ff17db4b62001d0a13a36f8821413c6f5c8103e | |
parent | 2c6bfcb89d70b17e2455e1c27ca5e4bf6e7f4d00 (diff) |
rtc: ricoh583: add ricoh583 PMU RTC driver
Add Ricoh583 PMU RTC driver along with make files.
bug 902137
Change-Id: If9a62ca897e4d41735c7638fff17c7b7570bc3c6
Signed-off-by: venu byravarasu <vbyravarasu@nvidia.com>
Reviewed-on: http://git-master/r/70017
Reviewed-by: Automatic_Commit_Validation_User
Reviewed-by: Laxman Dewangan <ldewangan@nvidia.com>
Rebase-Id: Rd6ca9940d644994ee111f2a15a3bc0b57adbfd27
-rw-r--r-- | drivers/rtc/Kconfig | 10 | ||||
-rw-r--r-- | drivers/rtc/Makefile | 1 | ||||
-rw-r--r-- | drivers/rtc/rtc-ricoh583.c | 394 |
3 files changed, 405 insertions, 0 deletions
diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig index c8ded466b56b..b1524a16a5cd 100644 --- a/drivers/rtc/Kconfig +++ b/drivers/rtc/Kconfig @@ -1241,6 +1241,16 @@ config RTC_DRV_LOONGSON1 This driver can also be built as a module. If so, the module will be called rtc-ls1x. +config RTC_DRV_RC5T583 + tristate "RICOH RC5T583 PMU RTC driver" + depends on MFD_RICOH583 + default n + help + If you say yes here you get support for the RICOH RC5T583 RTC module. + + This driver can also be built as a module. If so, the module + will be called rtc-rc5t583. + config RTC_DRV_MXC tristate "Freescale MXC Real Time Clock" depends on ARCH_MXC diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile index 0ce8194bf9f1..7cf9d56ba5cd 100644 --- a/drivers/rtc/Makefile +++ b/drivers/rtc/Makefile @@ -120,6 +120,7 @@ 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_RC5T583) += rtc-ricoh583.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-ricoh583.c b/drivers/rtc/rtc-ricoh583.c new file mode 100644 index 000000000000..6b4767aa6884 --- /dev/null +++ b/drivers/rtc/rtc-ricoh583.c @@ -0,0 +1,394 @@ +/* + * drivers/rtc/rtc_ricoh583.c + * + * rtc driver for ricoh rc5t583 pmu + * + * 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/ricoh583.h> +#include <linux/platform_device.h> +#include <linux/rtc.h> +#include <linux/slab.h> +#include <linux/module.h> + +#define rtc_ctrl1 0xED +#define rtc_ctrl2 0xEE +#define rtc_seconds_reg 0xE0 +#define rtc_alarm_y 0xF0 +#define rtc_adjust 0xE7 + +/* +linux rtc driver refers 1900 as base year in many calculations. +(e.g. refer drivers/rtc/rtc-lib.c) +*/ +#define os_ref_year 1900 + +/* + pmu rtc have only 2 nibbles to store year information, so using an offset + of 100 to set the base year as 2000 for our driver. +*/ +#define rtc_year_offset 100 + +struct ricoh583_rtc { + unsigned long epoch_start; + int irq; + struct rtc_device *rtc; + bool irq_en; +}; + +static int ricoh583_read_regs(struct device *dev, int reg, int len, + uint8_t *val) +{ + int ret; + + ret = ricoh583_bulk_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; +} + +static int ricoh583_write_regs(struct device *dev, int reg, int len, + uint8_t *val) +{ + int ret; + ret = ricoh583_bulk_writes(dev->parent, reg, len, val); + if (ret < 0) { + dev_err(dev->parent, "\n %s failed writing\n", __func__); + WARN_ON(1); + } + + return ret; +} + +static int ricoh583_rtc_valid_tm(struct rtc_time *tm) +{ + if (tm->tm_year >= (rtc_year_offset + 99) + || tm->tm_mon >= 12 + || tm->tm_mday < 1 + || tm->tm_mday > rtc_month_days(tm->tm_mon, tm->tm_year + os_ref_year) + || tm->tm_hour >= 24 + || tm->tm_min >= 60 + || 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 device *dev, struct rtc_time *tm) +{ + dev_info(dev, "rtc-time : %d/%d/%d %d:%d\n", + (tm->tm_mon + 1), tm->tm_mday, (tm->tm_year + os_ref_year), + tm->tm_hour, tm->tm_min); +} + +static int ricoh583_rtc_read_time(struct device *dev, struct rtc_time *tm) +{ + u8 buff[7]; + int err; + err = ricoh583_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_wday = buff[3]; + tm->tm_mday = buff[4]; + tm->tm_mon = buff[5]; + tm->tm_year = buff[6]; + print_time(dev, tm); + return ricoh583_rtc_valid_tm(tm); +} + +static int ricoh583_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_wday; + buff[4] = tm->tm_mday; + buff[5] = tm->tm_mon; + buff[6] = tm->tm_year; + + print_time(dev, tm); + + convert_decimal_to_bcd(buff, sizeof(buff)); + err = ricoh583_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; + } + + return 0; +} +static int ricoh583_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm); + +static int ricoh583_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) +{ + struct ricoh583_rtc *rtc = dev_get_drvdata(dev); + unsigned long seconds; + u8 buff[5]; + 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(dev->parent, &alrm->time); + rtc_tm_to_time(&alrm->time, &seconds); + ricoh583_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) { + rtc->irq_en = true; + } else if (!alrm->enabled && rtc->irq_en) { + rtc->irq_en = false; + } + + 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; + convert_decimal_to_bcd(buff, sizeof(buff)); + err = ricoh583_write_regs(dev, rtc_alarm_y, sizeof(buff), buff); + if (err){ + dev_err(dev->parent, "\n unable to set alarm\n"); + return -EBUSY; + } + buff[0] = 0x25; /* to enable alarm_y */ + buff[1] = 0x20; /* to enable 24-hour format */ + err = ricoh583_write_regs(dev, rtc_ctrl1, 2, buff); + if (err) { + dev_err(dev, "failed programming rtc ctrl regs\n"); + return -EBUSY; + } +return err; +} + +static int ricoh583_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm) +{ + u8 buff[5]; + int err; + + err = ricoh583_read_regs(dev, rtc_alarm_y, sizeof(buff), buff); + if (err) + return err; + convert_bcd_to_decimal(buff, sizeof(buff)); + + alrm->time.tm_min = buff[0]; + alrm->time.tm_hour = buff[1]; + alrm->time.tm_mday = buff[2]; + alrm->time.tm_mon = buff[3]; + alrm->time.tm_year = buff[4]; + + dev_info(dev->parent, "\n getting alarm time::\n"); + print_time(dev, &alrm->time); + + return 0; +} + +static const struct rtc_class_ops ricoh583_rtc_ops = { + .read_time = ricoh583_rtc_read_time, + .set_time = ricoh583_rtc_set_time, + .set_alarm = ricoh583_rtc_set_alarm, + .read_alarm = ricoh583_rtc_read_alarm, +}; + +static irqreturn_t ricoh583_rtc_irq(int irq, void *data) +{ + struct device *dev = data; + struct ricoh583_rtc *rtc = dev_get_drvdata(dev); + u8 reg; + int err; + + /* clear alarm-Y status bits.*/ + err = ricoh583_read_regs(dev, rtc_ctrl2, 1, ®); + if (err) { + dev_err(dev->parent, "unable to read rtc_ctrl2 reg\n"); + return -EBUSY; + } + reg &= ~0x8; + err = ricoh583_write_regs(dev, rtc_ctrl2, 1, ®); + 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 ricoh583_rtc_probe(struct platform_device *pdev) +{ + struct ricoh583_rtc_platform_data *pdata = pdev->dev.platform_data; + struct ricoh583_rtc *rtc; + struct rtc_time tm; + int err; + u8 reg[2]; + 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); + device_init_wakeup(&pdev->dev, 1); + rtc->rtc = rtc_device_register(pdev->name, &pdev->dev, + &ricoh583_rtc_ops, THIS_MODULE); + + if (IS_ERR(rtc->rtc)) { + err = PTR_ERR(rtc->rtc); + goto fail; + } + reg[0] = 0; /* clearing RTC Adjust register */ + err = ricoh583_write_regs(&pdev->dev, rtc_adjust, 1, reg); + if (err) { + dev_err(&pdev->dev, "unable to program rtc_adjust reg\n"); + return -EBUSY; + } + + reg[0] = 0x25; /* to enable alarm_y */ + reg[1] = 0x20; /* to enable 24-hour format */ + err = ricoh583_write_regs(&pdev->dev, rtc_ctrl1, 2, reg); + if (err) { + dev_err(&pdev->dev, "failed rtc setup\n"); + return -EBUSY; + } + + ricoh583_rtc_read_time(&pdev->dev, &tm); + if ((tm.tm_year < rtc_year_offset || tm.tm_year > (rtc_year_offset + 99))){ + if (pdata->time.tm_year < 2000 || pdata->time.tm_year > 2100) { + memset(&pdata->time, 0, sizeof(pdata->time)); + pdata->time.tm_year = rtc_year_offset; + pdata->time.tm_mday = 1; + } else + pdata->time.tm_year -= os_ref_year; + ricoh583_rtc_set_time(&pdev->dev, &pdata->time); + } + if (pdata && (pdata->irq >= 0)) { + rtc->irq = pdata->irq; + err = request_threaded_irq(pdata->irq, NULL, ricoh583_rtc_irq, + IRQF_ONESHOT, "rtc_ricoh583", + &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); + } + } + return 0; + +fail: + if (!IS_ERR_OR_NULL(rtc->rtc)) + rtc_device_unregister(rtc->rtc); + kfree(rtc); + return err; +} + +static int ricoh583_rtc_remove(struct platform_device *pdev) +{ + struct ricoh583_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 ricoh583_rtc_driver = { + .driver = { + .name = "rtc_ricoh583", + .owner = THIS_MODULE, + }, + .probe = ricoh583_rtc_probe, + .remove = ricoh583_rtc_remove, +}; + +static int __init ricoh583_rtc_init(void) +{ + return platform_driver_register(&ricoh583_rtc_driver); +} +module_init(ricoh583_rtc_init); + +static void __exit ricoh583_rtc_exit(void) +{ + platform_driver_unregister(&ricoh583_rtc_driver); +} +module_exit(ricoh583_rtc_exit); + +MODULE_DESCRIPTION("RICOH PMU ricoh583 RTC driver"); +MODULE_AUTHOR("NVIDIA Corporation"); +MODULE_LICENSE("GPL"); +MODULE_ALIAS("platform:rtc_ricoh583"); |