From patchwork Fri Mar 18 11:20:45 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wei Ni X-Patchwork-Id: 87513 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from mail-yi0-f56.google.com (mail-yi0-f56.google.com [209.85.218.56]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) (Client CN "smtp.gmail.com", Issuer "Google Internet Authority" (verified OK)) by ozlabs.org (Postfix) with ESMTPS id 94483B6FDF for ; Fri, 18 Mar 2011 22:21:06 +1100 (EST) Received: by yib17 with SMTP id 17sf3017178yib.11 for ; Fri, 18 Mar 2011 04:21:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlegroups.com; s=beta; h=domainkey-signature:mime-version:x-beenthere:received-spf :x-pgp-universal:from:to:cc:subject:date:message-id:x-mailer :x-original-sender:x-original-authentication-results:reply-to :precedence:mailing-list:list-id:x-google-group-id:list-post :list-help:list-archive:sender:list-subscribe:list-unsubscribe :content-type; bh=F2B9cPSWLyE3wRhO4v3iSdsDxuJuQYVM17f6xF6ZXg8=; b=Disy+n2ge0lRPrzWOu1NjARtcE0RQ06+wsn+o1pZRskCb3bH8vRVgKTI8g4kTg90em /MWyWB+9IpPHJr9wVrB+sCJQPsKkEa4fZrC4w6dwTiKaOdSZ8W3BP8Vtlx7ujlhTX+BZ zfUqncOAvdVmisIPrbcHB90ee1aA7sINcXHUk= DomainKey-Signature: a=rsa-sha1; c=nofws; d=googlegroups.com; s=beta; h=mime-version:x-beenthere:received-spf:x-pgp-universal:from:to:cc :subject:date:message-id:x-mailer:x-original-sender :x-original-authentication-results:reply-to:precedence:mailing-list :list-id:x-google-group-id:list-post:list-help:list-archive:sender :list-subscribe:list-unsubscribe:content-type; b=AailEpxIOuYgUp/+ZIMF9OHwEByECoAXAVUL0Sz2H8XDswF6MkxvdBKBo3+smuix4G MRLJIt2Oe+Z/QCR/z6AQkyfUiT569Pyi7oKwDWy70zXDeZ79SI4h0BYq/Hga/EadprjX pyVD5maM2iXcHLnmNwapBS0QPl8jO0yNhwbr4= Received: by 10.90.38.20 with SMTP id l20mr146259agl.52.1300447262624; Fri, 18 Mar 2011 04:21:02 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: rtc-linux@googlegroups.com Received: by 10.231.176.130 with SMTP id be2ls90712ibb.1.p; Fri, 18 Mar 2011 04:21:02 -0700 (PDT) Received: by 10.43.54.200 with SMTP id vv8mr163546icb.35.1300447262208; Fri, 18 Mar 2011 04:21:02 -0700 (PDT) Received: by 10.43.54.200 with SMTP id vv8mr163545icb.35.1300447262193; Fri, 18 Mar 2011 04:21:02 -0700 (PDT) Received: from hqemgate04.nvidia.com (hqemgate04.nvidia.com [216.228.121.35]) by gmr-mx.google.com with ESMTPS id xk8si830987icb.5.2011.03.18.04.21.01 (version=TLSv1/SSLv3 cipher=OTHER); Fri, 18 Mar 2011 04:21:02 -0700 (PDT) Received-SPF: pass (google.com: domain of wni@nvidia.com designates 216.228.121.35 as permitted sender) client-ip=216.228.121.35; Received: from hqnvupgp07.nvidia.com (Not Verified[216.228.121.13]) by hqemgate04.nvidia.com id ; Fri, 18 Mar 2011 04:18:55 -0700 Received: from hqnvemgw01.nvidia.com ([172.17.108.22]) by hqnvupgp07.nvidia.com (PGP Universal service); Fri, 18 Mar 2011 04:20:59 -0700 X-PGP-Universal: processed; by hqnvupgp07.nvidia.com on Fri, 18 Mar 2011 04:20:59 -0700 Received: from daphne.nvidia.com (Not Verified[172.16.212.96]) by hqnvemgw01.nvidia.com with MailMarshal (v6, 7, 2, 8378) id ; Fri, 18 Mar 2011 04:21:10 -0700 Received: from localhost.localdomain (dhcp-10-19-174-149.nvidia.com [10.19.174.149]) by daphne.nvidia.com (8.13.8+Sun/8.8.8) with ESMTP id p2IBKt1s004766; Fri, 18 Mar 2011 04:20:58 -0700 (PDT) From: wni@nvidia.com To: a.zummo@towertech.it, rtc-linux@googlegroups.com Cc: linux-tegra@vger.kernel.org, Wei Ni Subject: [rtc-linux] [PATCH] mfd: tps6586x: add RTC driver for TI TPS6586x Date: Fri, 18 Mar 2011 19:20:45 +0800 Message-Id: <1300447245-24906-1-git-send-email-wni@nvidia.com> X-Mailer: git-send-email 1.7.0 X-Original-Sender: wni@nvidia.com X-Original-Authentication-Results: gmr-mx.google.com; spf=pass (google.com: domain of wni@nvidia.com designates 216.228.121.35 as permitted sender) smtp.mail=wni@nvidia.com Reply-To: rtc-linux@googlegroups.com Precedence: list Mailing-list: list rtc-linux@googlegroups.com; contact rtc-linux+owners@googlegroups.com List-ID: X-Google-Group-Id: 712029733259 List-Post: , List-Help: , List-Archive: Sender: rtc-linux@googlegroups.com List-Subscribe: , List-Unsubscribe: , From: Wei Ni this driver supports setting of alarms, and reading/setting of time. Signed-off-by: Wei Ni --- drivers/rtc/Kconfig | 6 + drivers/rtc/rtc-tps6586x.c | 380 ++++++++++++++++++++++++++++++++++++++++++ include/linux/mfd/tps6586x.h | 14 ++ 3 files changed, 400 insertions(+), 0 deletions(-) create mode 100644 drivers/rtc/rtc-tps6586x.c diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig index 4941cad..89159e0 100644 --- a/drivers/rtc/Kconfig +++ b/drivers/rtc/Kconfig @@ -297,6 +297,12 @@ config RTC_DRV_DM355EVM help Supports the RTC firmware in the MSP430 on the DM355 EVM. +config RTC_DRV_TPS6586X + tristate "TI TPS6586X RTC" + depends on MFD_TPS6586X + help + This driver supports TPS6586X RTC. + config RTC_DRV_TWL92330 boolean "TI TWL92330/Menelaus" depends on MENELAUS diff --git a/drivers/rtc/rtc-tps6586x.c b/drivers/rtc/rtc-tps6586x.c new file mode 100644 index 0000000..df8109e --- /dev/null +++ b/drivers/rtc/rtc-tps6586x.c @@ -0,0 +1,380 @@ +/* + * drivers/rtc/rtc-tps6586x.c + * + * RTC driver for TI TPS6586x + * + * Copyright (c) 2010, 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. + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#define RTC_CTRL 0xc0 +#define OSC_SRC_SEL BIT(6) /* select internal or external clock */ +#define RTC_ENABLE BIT(5) /* enables alarm */ +#define RTC_HIRES BIT(4) /* 1Khz or 32Khz updates */ +#define RTC_ALARM1_HI 0xc1 +#define RTC_COUNT4 0xc6 + +struct tps6586x_rtc { + unsigned long epoch_start; + int irq; + struct rtc_device *rtc; + bool irq_en; +}; + +static inline struct device *to_tps6586x_dev(struct device *dev) +{ + return dev->parent; +} + +static int tps6586x_rtc_read_time(struct device *dev, struct rtc_time *tm) +{ + struct tps6586x_rtc *rtc = dev_get_drvdata(dev); + struct device *tps_dev = to_tps6586x_dev(dev); + unsigned long long ticks = 0; + unsigned long seconds; + u8 buff[5]; + int err; + int i; + + err = tps6586x_reads(tps_dev, RTC_COUNT4, sizeof(buff), buff); + if (err < 0) { + dev_err(dev, "failed to read counter\n"); + return err; + } + + for (i = 0; i < sizeof(buff); i++) { + ticks <<= 8; + ticks |= buff[i]; + } + + seconds = ticks >> 10; + + seconds += rtc->epoch_start; + rtc_time_to_tm(seconds, tm); + return rtc_valid_tm(tm); +} + +static int tps6586x_rtc_set_time(struct device *dev, struct rtc_time *tm) +{ + struct tps6586x_rtc *rtc = dev_get_drvdata(dev); + struct device *tps_dev = to_tps6586x_dev(dev); + unsigned long long ticks; + unsigned long seconds; + u8 buff[5]; + int err; + + rtc_tm_to_time(tm, &seconds); + + if (WARN_ON(seconds < rtc->epoch_start)) { + dev_err(dev, "requested time unsupported\n"); + return -EINVAL; + } + + seconds -= rtc->epoch_start; + + ticks = (unsigned long long)seconds << 10; + buff[0] = (ticks >> 32) & 0xff; + buff[1] = (ticks >> 24) & 0xff; + buff[2] = (ticks >> 16) & 0xff; + buff[3] = (ticks >> 8) & 0xff; + buff[4] = ticks & 0xff; + + err = tps6586x_clr_bits(tps_dev, RTC_CTRL, RTC_ENABLE); + if (err < 0) { + dev_err(dev, "failed to clear RTC_ENABLE\n"); + return err; + } + + err = tps6586x_writes(tps_dev, RTC_COUNT4, sizeof(buff), buff); + if (err < 0) { + dev_err(dev, "failed to program new time\n"); + return err; + } + + err = tps6586x_set_bits(tps_dev, RTC_CTRL, RTC_ENABLE); + if (err < 0) { + dev_err(dev, "failed to set RTC_ENABLE\n"); + return err; + } + + return 0; +} + +static int tps6586x_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) +{ + struct tps6586x_rtc *rtc = dev_get_drvdata(dev); + struct device *tps_dev = to_tps6586x_dev(dev); + unsigned long seconds; + unsigned long ticks; + u8 buff[3]; + int err; + + if (rtc->irq == -1) + return -EIO; + + rtc_tm_to_time(&alrm->time, &seconds); + + if (WARN_ON(alrm->enabled && (seconds < rtc->epoch_start))) { + dev_err(dev, "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; + } + + seconds -= rtc->epoch_start; + ticks = (unsigned long long)seconds << 10; + + buff[0] = (ticks >> 16) & 0xff; + buff[1] = (ticks >> 8) & 0xff; + buff[2] = ticks & 0xff; + + err = tps6586x_writes(tps_dev, RTC_ALARM1_HI, sizeof(buff), buff); + if (err) + dev_err(tps_dev, "unable to program alarm\n"); + + return err; +} + +static int tps6586x_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm) +{ + struct tps6586x_rtc *rtc = dev_get_drvdata(dev); + struct device *tps_dev = to_tps6586x_dev(dev); + unsigned long ticks; + unsigned long seconds; + u8 buff[3]; + int err; + + err = tps6586x_reads(tps_dev, RTC_ALARM1_HI, sizeof(buff), buff); + if (err) + return err; + + ticks = (buff[0] << 16) | (buff[1] << 8) | buff[2]; + seconds = ticks >> 10; + seconds += rtc->epoch_start; + + rtc_time_to_tm(seconds, &alrm->time); + alrm->enabled = rtc->irq_en; + + return 0; +} + +static int tps6586x_rtc_alarm_irq_enable(struct device *dev, + unsigned int enabled) +{ + struct tps6586x_rtc *rtc = dev_get_drvdata(dev); + struct device *tps_dev = to_tps6586x_dev(dev); + u8 buff; + int err; + + if (rtc->irq == -1) + return -EIO; + + err = tps6586x_read(tps_dev, RTC_CTRL, &buff); + if (err < 0) { + dev_err(dev, "failed to read RTC_CTRL\n"); + return err; + } + + if ((enabled && (buff & RTC_ENABLE)) || + (!enabled && !(buff & RTC_ENABLE))) + return 0; + + if (enabled) { + err = tps6586x_set_bits(tps_dev, RTC_CTRL, RTC_ENABLE); + if (err < 0) { + dev_err(dev, "failed to set RTC_ENABLE\n"); + return err; + } + if (!rtc->irq_en) { + enable_irq(rtc->irq); + rtc->irq_en = true; + } + } else { + err = tps6586x_clr_bits(tps_dev, RTC_CTRL, RTC_ENABLE); + if (err < 0) { + dev_err(dev, "failed to clear RTC_ENABLE\n"); + return err; + } + if (!rtc->irq_en) { + disable_irq(rtc->irq); + rtc->irq_en = false; + } + } + + return 0; +} + +static const struct rtc_class_ops tps6586x_rtc_ops = { + .read_time = tps6586x_rtc_read_time, + .set_time = tps6586x_rtc_set_time, + .set_alarm = tps6586x_rtc_set_alarm, + .read_alarm = tps6586x_rtc_read_alarm, + .alarm_irq_enable = tps6586x_rtc_alarm_irq_enable, +}; + +static irqreturn_t tps6586x_rtc_irq(int irq, void *data) +{ + struct device *dev = data; + struct tps6586x_rtc *rtc = dev_get_drvdata(dev); + + rtc_update_irq(rtc->rtc, 1, RTC_IRQF | RTC_AF); + return IRQ_HANDLED; +} + +static int __devinit tps6586x_rtc_probe(struct platform_device *pdev) +{ + struct tps6586x_rtc_platform_data *pdata = pdev->dev.platform_data; + struct device *tps_dev = to_tps6586x_dev(&pdev->dev); + struct tps6586x_rtc *rtc; + int err; + struct tps6586x_epoch_start *epoch; + + 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_warn(&pdev->dev, "no IRQ specified, wakeup is disabled\n"); + + epoch = &pdata->start; + rtc->epoch_start = mktime(epoch->year, epoch->month, epoch->day, + epoch->hour, epoch->min, epoch->sec); + + device_init_wakeup(&pdev->dev, 1); + + rtc->rtc = rtc_device_register("tps6586x-rtc", &pdev->dev, + &tps6586x_rtc_ops, THIS_MODULE); + + if (IS_ERR(rtc->rtc)) { + err = PTR_ERR(rtc->rtc); + goto fail; + } + + /* disable high-res mode, enable tick counting */ + err = tps6586x_update(tps_dev, RTC_CTRL, + (RTC_ENABLE | OSC_SRC_SEL), + (RTC_ENABLE | OSC_SRC_SEL)); + + if (err < 0) { + dev_err(&pdev->dev, "unable to start counter\n"); + goto fail; + } + + dev_set_drvdata(&pdev->dev, rtc); + if (pdata && (pdata->irq >= 0)) { + rtc->irq = pdata->irq; + err = request_threaded_irq(pdata->irq, NULL, tps6586x_rtc_irq, + IRQF_ONESHOT, "tps6586x-rtc", + &pdev->dev); + if (err) { + dev_warn(&pdev->dev, "unable to request IRQ(%d)\n", + rtc->irq); + rtc->irq = -1; + } else { + disable_irq(rtc->irq); + rtc->irq_en = false; + } + } + + return 0; + +fail: + if (!IS_ERR_OR_NULL(rtc->rtc)) + rtc_device_unregister(rtc->rtc); + kfree(rtc); + return err; +} + +static int __devexit tps6586x_rtc_remove(struct platform_device *pdev) +{ + struct tps6586x_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 int tps6586x_rtc_suspend(struct platform_device *pdev, + pm_message_t state) +{ + struct tps6586x_rtc *rtc = dev_get_drvdata(&pdev->dev); + + if (device_may_wakeup(pdev)) + enable_irq_wake(rtc->irq); + return 0; +} + +static int tps6586x_rtc_resume(struct platform_device *pdev) +{ + struct tps6586x_rtc *rtc = dev_get_drvdata(&pdev->dev); + + if (device_may_wakeup(pdev)) + disable_irq_wake(rtc->irq); + return 0; +} + +static struct platform_driver tps6586x_rtc_driver = { + .driver = { + .name = "tps6586x-rtc", + .owner = THIS_MODULE, + }, + .probe = tps6586x_rtc_probe, + .remove = __devexit_p(tps6586x_rtc_remove), + .suspend = tps6586x_rtc_suspend, + .resume = tps6586x_rtc_resume, +}; + +static int __init tps6586x_rtc_init(void) +{ + return platform_driver_register(&tps6586x_rtc_driver); +} +module_init(tps6586x_rtc_init); + +static void __exit tps6586x_rtc_exit(void) +{ + platform_driver_unregister(&tps6586x_rtc_driver); +} +module_exit(tps6586x_rtc_exit); + +MODULE_DESCRIPTION("TI TPS6586x RTC driver"); +MODULE_AUTHOR("NVIDIA Corporation"); +MODULE_LICENSE("GPL"); +MODULE_ALIAS("platform:rtc-tps6586x"); diff --git a/include/linux/mfd/tps6586x.h b/include/linux/mfd/tps6586x.h index b6bab1b..eb5389c 100644 --- a/include/linux/mfd/tps6586x.h +++ b/include/linux/mfd/tps6586x.h @@ -54,6 +54,20 @@ struct tps6586x_subdev_info { void *platform_data; }; +struct tps6586x_epoch_start { + int year; + int month; + int day; + int hour; + int min; + int sec; +}; + +struct tps6586x_rtc_platform_data { + int irq; + struct tps6586x_epoch_start start; +}; + struct tps6586x_platform_data { int num_subdevs; struct tps6586x_subdev_info *subdevs;