From patchwork Wed Dec 17 16:45:27 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lee Jones X-Patchwork-Id: 42391 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-wg0-f72.google.com (mail-wg0-f72.google.com [74.125.82.72]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 1779B26C8B for ; Wed, 17 Dec 2014 16:46:32 +0000 (UTC) Received: by mail-wg0-f72.google.com with SMTP id y19sf10271668wgg.3 for ; Wed, 17 Dec 2014 08:46:31 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:delivered-to:from:to:cc:subject :date:message-id:in-reply-to:references:sender:precedence:list-id :x-original-sender:x-original-authentication-results:mailing-list :list-post:list-help:list-archive:list-unsubscribe; bh=We4WZR/kpzCarMEAwUBwMtrS3zO6aEaMTLrRdMQFlQA=; b=cZ9SLf+3bHsvSuefC9wbb++TKxVCUTfstx+sujRNzWOMUPNjVlnxhBpO9iYwkec7Fo +Jg7p+FxVfBbG2owyxrKpFzVndgCYl0B01WK0Z3vewH6IuS5Vqwm/B3Fe4PtLzfUfVjc hGWUgtM9A1xcjonDsFVywgd0nSFt06YFVYrGNJ3pL/YX9q6s46BM5+WPYpsBJdrNbNAR NJMrgg9THPrYWG35vTWFAondueghTDWhYfbx5Ma1KF3pk+XtzIan00NJl1sWTMZkHPK6 sFvQh4NYkohX7KSM1nr8XVSaU6exRpl6AFCdiOcE7/XM5uPb7MG1iI8zJ5EamlqH6xA/ ApMg== X-Gm-Message-State: ALoCoQl595vXifbZOcmYbsX4stp0qbFfTTp9oM1AlXAOSOEZjik/pN/Ss429IT3ERqVb+7rDNPot X-Received: by 10.180.105.97 with SMTP id gl1mr1501366wib.7.1418834791323; Wed, 17 Dec 2014 08:46:31 -0800 (PST) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.43.1 with SMTP id s1ls1146072lal.23.gmail; Wed, 17 Dec 2014 08:46:31 -0800 (PST) X-Received: by 10.152.87.142 with SMTP id ay14mr20067122lab.45.1418834791034; Wed, 17 Dec 2014 08:46:31 -0800 (PST) Received: from mail-lb0-f179.google.com (mail-lb0-f179.google.com. [209.85.217.179]) by mx.google.com with ESMTPS id ul1si4425242lbb.29.2014.12.17.08.46.31 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 17 Dec 2014 08:46:31 -0800 (PST) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.179 as permitted sender) client-ip=209.85.217.179; Received: by mail-lb0-f179.google.com with SMTP id z11so12964812lbi.10 for ; Wed, 17 Dec 2014 08:46:31 -0800 (PST) X-Received: by 10.112.170.36 with SMTP id aj4mr41918371lbc.3.1418834790923; Wed, 17 Dec 2014 08:46:30 -0800 (PST) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.112.142.69 with SMTP id ru5csp1422158lbb; Wed, 17 Dec 2014 08:46:29 -0800 (PST) X-Received: by 10.69.20.74 with SMTP id ha10mr71241644pbd.122.1418834772744; Wed, 17 Dec 2014 08:46:12 -0800 (PST) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id qu3si6461469pbc.58.2014.12.17.08.46.11 for ; Wed, 17 Dec 2014 08:46:12 -0800 (PST) Received-SPF: none (google.com: linux-watchdog-owner@vger.kernel.org does not designate permitted sender hosts) client-ip=209.132.180.67; Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751347AbaLQQqG (ORCPT ); Wed, 17 Dec 2014 11:46:06 -0500 Received: from mail-yk0-f174.google.com ([209.85.160.174]:52816 "EHLO mail-yk0-f174.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751412AbaLQQqE (ORCPT ); Wed, 17 Dec 2014 11:46:04 -0500 Received: by mail-yk0-f174.google.com with SMTP id 10so7007546ykt.5 for ; Wed, 17 Dec 2014 08:46:03 -0800 (PST) X-Received: by 10.236.230.40 with SMTP id i38mr31189378yhq.34.1418834763339; Wed, 17 Dec 2014 08:46:03 -0800 (PST) Received: from localhost.localdomain (host109-148-232-11.range109-148.btcentralplus.com. [109.148.232.11]) by mx.google.com with ESMTPSA id t3sm2460839yhp.19.2014.12.17.08.46.00 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 17 Dec 2014 08:46:02 -0800 (PST) From: Lee Jones To: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Cc: lee.jones@linaro.org, kernel@stlinux.com, a.zummo@towertech.it, rtc-linux@googlegroups.com, wim@iguana.be, linux-watchdog@vger.kernel.org, devicetree@vger.kernel.org, linux@roeck-us.net, arnd@arndb.de Subject: [PATCH 8/8] rtc: st: add new driver for ST's LPC RTC Date: Wed, 17 Dec 2014 16:45:27 +0000 Message-Id: <1418834727-1602-9-git-send-email-lee.jones@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1418834727-1602-1-git-send-email-lee.jones@linaro.org> References: <1418834727-1602-1-git-send-email-lee.jones@linaro.org> Sender: linux-watchdog-owner@vger.kernel.org Precedence: list List-ID: X-Mailing-List: linux-watchdog@vger.kernel.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: lee.jones@linaro.org X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.179 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , ST's Low Power Controller (LPC) controls two devices; watchdog and RTC. Only one of the devices can be used at any one time. This is enforced by the correlating MFD driver. This portion of the driver-set controls the Real Time Clock. Signed-off-by: Lee Jones --- drivers/rtc/Kconfig | 13 ++ drivers/rtc/Makefile | 1 + drivers/rtc/rtc-st-lpc.c | 354 ++++++++++++++++++++++++++++++++++++++++++++++ drivers/watchdog/st_wdt.c | 8 +- 4 files changed, 375 insertions(+), 1 deletion(-) create mode 100644 drivers/rtc/rtc-st-lpc.c diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig index a168e96..aa4bd90 100644 --- a/drivers/rtc/Kconfig +++ b/drivers/rtc/Kconfig @@ -1355,6 +1355,18 @@ config RTC_DRV_SIRFSOC Say "yes" here to support the real time clock on SiRF SOC chips. This driver can also be built as a module called rtc-sirfsoc. +config RTC_DRV_ST_LPC + tristate "STMicroelectronics LPC RTC" + depends on ARCH_STI + depends on OF + select MFD_ST_LPC + help + Say Y here to include STMicroelectronics Low Power Controller + (LPC) based RTC support. + + To compile this driver as a module, choose M here: the + module will be called rtc-st-lpc. + config RTC_DRV_MOXART tristate "MOXA ART RTC" depends on ARCH_MOXART || COMPILE_TEST @@ -1390,4 +1402,5 @@ config RTC_DRV_HID_SENSOR_TIME rtc-hid-sensor-time. + endif # RTC_CLASS diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile index 56f061c..ce5860b 100644 --- a/drivers/rtc/Makefile +++ b/drivers/rtc/Makefile @@ -145,4 +145,5 @@ obj-$(CONFIG_RTC_DRV_WM8350) += rtc-wm8350.o obj-$(CONFIG_RTC_DRV_X1205) += rtc-x1205.o obj-$(CONFIG_RTC_DRV_XGENE) += rtc-xgene.o obj-$(CONFIG_RTC_DRV_SIRFSOC) += rtc-sirfsoc.o +obj-$(CONFIG_RTC_DRV_ST_LPC) += rtc-st-lpc.o obj-$(CONFIG_RTC_DRV_MOXART) += rtc-moxart.o diff --git a/drivers/rtc/rtc-st-lpc.c b/drivers/rtc/rtc-st-lpc.c new file mode 100644 index 0000000..de28aca --- /dev/null +++ b/drivers/rtc/rtc-st-lpc.c @@ -0,0 +1,354 @@ +/* + * rtc-st-lpc.c - ST's LPC RTC, powered by the Low Power Timer + * + * Copyright (C) 2014 STMicroelectronics Limited + * + * Author: David Paris for STMicroelectronics + * Lee Jones for STMicroelectronics + * + * Based on the original driver written by Stuart Menefy. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public Licence + * as published by the Free Software Foundation; either version + * 2 of the Licence, or (at your option) any later version. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +/* Low Power Timer */ +#define LPC_LPT_LSB_OFF 0x400 +#define LPC_LPT_MSB_OFF 0x404 +#define LPC_LPT_START_OFF 0x408 + +/* Low Power Alarm */ +#define LPC_LPA_LSB_OFF 0x410 +#define LPC_LPA_MSB_OFF 0x414 +#define LPC_LPA_START_OFF 0x418 + +/* LPC as WDT */ +#define LPC_WDT_OFF 0x510 +#define LPC_WDT_FLAG_OFF 0x514 + +struct st_rtc { + struct rtc_device *rtc_dev; + struct rtc_wkalrm alarm; + struct resource *res; + struct clk *clk; + unsigned long clkrate; + void __iomem *ioaddr; + bool irq_enabled:1; + spinlock_t lock; + short irq; +}; + +static void st_rtc_set_hw_alarm(struct st_rtc *rtc, + unsigned long msb, unsigned long lsb) +{ + unsigned long flags; + + spin_lock_irqsave(&rtc->lock, flags); + + writel(1, rtc->ioaddr + LPC_WDT_OFF); + + writel(msb, rtc->ioaddr + LPC_LPA_MSB_OFF); + writel(lsb, rtc->ioaddr + LPC_LPA_LSB_OFF); + writel(1, rtc->ioaddr + LPC_LPA_START_OFF); + + writel(0, rtc->ioaddr + LPC_WDT_OFF); + + spin_unlock_irqrestore(&rtc->lock, flags); +} + +static irqreturn_t st_rtc_handler(int this_irq, void *data) +{ + struct st_rtc *rtc = (struct st_rtc *)data; + + rtc_update_irq(rtc->rtc_dev, 1, RTC_AF); + + return IRQ_HANDLED; +} + +static int st_rtc_read_time(struct device *dev, struct rtc_time *tm) +{ + struct st_rtc *rtc = dev_get_drvdata(dev); + unsigned long lpt_lsb, lpt_msb; + unsigned long long lpt; + unsigned long flags; + + spin_lock_irqsave(&rtc->lock, flags); + + do { + lpt_msb = readl(rtc->ioaddr + LPC_LPT_MSB_OFF); + lpt_lsb = readl(rtc->ioaddr + LPC_LPT_LSB_OFF); + } while (readl(rtc->ioaddr + LPC_LPT_MSB_OFF) != lpt_msb); + + spin_unlock_irqrestore(&rtc->lock, flags); + + lpt = ((unsigned long long)lpt_msb << 32) | lpt_lsb; + do_div(lpt, rtc->clkrate); + rtc_time_to_tm(lpt, tm); + + return 0; +} + +static int st_rtc_set_time(struct device *dev, struct rtc_time *tm) +{ + struct st_rtc *rtc = dev_get_drvdata(dev); + unsigned long long lpt; + unsigned long secs, flags; + int ret; + + ret = rtc_tm_to_time(tm, &secs); + if (ret) + return ret; + + lpt = (unsigned long long)secs * rtc->clkrate; + + spin_lock_irqsave(&rtc->lock, flags); + + writel(lpt >> 32, rtc->ioaddr + LPC_LPT_MSB_OFF); + writel(lpt, rtc->ioaddr + LPC_LPT_LSB_OFF); + writel(1, rtc->ioaddr + LPC_LPT_START_OFF); + + spin_unlock_irqrestore(&rtc->lock, flags); + + return 0; +} + +static int st_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *wkalrm) +{ + struct st_rtc *rtc = dev_get_drvdata(dev); + unsigned long flags; + + spin_lock_irqsave(&rtc->lock, flags); + + memcpy(wkalrm, &rtc->alarm, sizeof(struct rtc_wkalrm)); + + spin_unlock_irqrestore(&rtc->lock, flags); + + return 0; +} + +static int st_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled) +{ + struct st_rtc *rtc = dev_get_drvdata(dev); + + if (enabled && !rtc->irq_enabled) { + enable_irq(rtc->irq); + rtc->irq_enabled = true; + } else if (!enabled && rtc->irq_enabled) { + disable_irq(rtc->irq); + rtc->irq_enabled = false; + } + + return 0; +} + +static int st_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *t) +{ + struct st_rtc *rtc = dev_get_drvdata(dev); + struct rtc_time now; + unsigned long now_secs; + unsigned long alarm_secs; + unsigned long long lpa; + + st_rtc_read_time(dev, &now); + rtc_tm_to_time(&now, &now_secs); + rtc_tm_to_time(&t->time, &alarm_secs); + + /* Invalid alarm time */ + if (now_secs > alarm_secs) + return -EINVAL; + + memcpy(&rtc->alarm, t, sizeof(struct rtc_wkalrm)); + + /* Now many secs to fire */ + alarm_secs -= now_secs; + lpa = (unsigned long long)alarm_secs * rtc->clkrate; + + st_rtc_set_hw_alarm(rtc, lpa >> 32, lpa); + st_rtc_alarm_irq_enable(dev, t->enabled); + + return 0; +} + +static struct rtc_class_ops st_rtc_ops = { + .read_time = st_rtc_read_time, + .set_time = st_rtc_set_time, + .read_alarm = st_rtc_read_alarm, + .set_alarm = st_rtc_set_alarm, + .alarm_irq_enable = st_rtc_alarm_irq_enable, +}; + +static int st_rtc_probe(struct platform_device *pdev) +{ + struct device_node *np = pdev->dev.of_node; + struct st_rtc *rtc; + struct resource *res; + struct rtc_time tm_check; + uint32_t mode; + int ret = 0; + + ret = of_property_read_u32(np, "st,lpc-mode", &mode); + if (ret) { + dev_err(&pdev->dev, "An LPC mode must be provided\n"); + return -EINVAL; + } + + /* LPC can either run in RTC or WDT mode */ + if (mode != ST_LPC_MODE_RTC) + return -ENODEV; + + rtc = devm_kzalloc(&pdev->dev, sizeof(struct st_rtc), GFP_KERNEL); + if (!rtc) + return -ENOMEM; + + spin_lock_init(&rtc->lock); + + res = platform_get_resource(pdev, IORESOURCE_MEM, 0); + rtc->ioaddr = devm_ioremap_resource(&pdev->dev, res); + if (IS_ERR(rtc->ioaddr)) + return PTR_ERR(rtc->ioaddr); + + rtc->irq = irq_of_parse_and_map(np, 0); + if (!rtc->irq) { + dev_err(&pdev->dev, "IRQ missing or invalid\n"); + return -EINVAL; + } + + ret = devm_request_irq(&pdev->dev, rtc->irq, st_rtc_handler, 0, + pdev->name, rtc); + if (ret) { + dev_err(&pdev->dev, "Failed to request irq %i\n", rtc->irq); + return ret; + } + + enable_irq_wake(rtc->irq); + disable_irq(rtc->irq); + + rtc->clk = clk_get(&pdev->dev, NULL); + if (IS_ERR(rtc->clk)) { + dev_err(&pdev->dev, "Unable to request clock\n"); + return PTR_ERR(rtc->clk); + } + + clk_prepare_enable(rtc->clk); + + rtc->clkrate = clk_get_rate(rtc->clk); + if (!rtc->clkrate) { + dev_err(&pdev->dev, "Unable to fetch clock rate\n"); + return -EINVAL; + } + + device_set_wakeup_capable(&pdev->dev, 1); + + platform_set_drvdata(pdev, rtc); + + /* + * The RTC-LPC is able to manage date.year > 2038 + * but currently the kernel can not manage this date! + * If the RTC-LPC has a date.year > 2038 then + * it's set to the epoch "Jan 1st 2000" + */ + st_rtc_read_time(&pdev->dev, &tm_check); + + if (tm_check.tm_year >= (2038 - 1900)) { + memset(&tm_check, 0, sizeof(tm_check)); + tm_check.tm_year = 100; + tm_check.tm_mday = 1; + st_rtc_set_time(&pdev->dev, &tm_check); + } + + rtc->rtc_dev = rtc_device_register("st-lpc-rtc", &pdev->dev, + &st_rtc_ops, THIS_MODULE); + if (IS_ERR(rtc->rtc_dev)) { + clk_disable_unprepare(rtc->clk); + return PTR_ERR(rtc->rtc_dev); + } + + return 0; +} + +static int st_rtc_remove(struct platform_device *pdev) +{ + struct st_rtc *rtc = platform_get_drvdata(pdev); + + if (likely(rtc->rtc_dev)) + rtc_device_unregister(rtc->rtc_dev); + + return 0; +} + +#ifdef CONFIG_PM +static int st_rtc_suspend(struct device *dev) +{ + struct st_rtc *rtc = dev_get_drvdata(dev); + + if (device_may_wakeup(dev)) + return 0; + + writel(1, rtc->ioaddr + LPC_WDT_OFF); + writel(0, rtc->ioaddr + LPC_LPA_START_OFF); + writel(0, rtc->ioaddr + LPC_WDT_OFF); + + return 0; +} + +static int st_rtc_resume(struct device *dev) +{ + struct st_rtc *rtc = dev_get_drvdata(dev); + + rtc_alarm_irq_enable(rtc->rtc_dev, 0); + + /* + * clean 'rtc->alarm' to allow a new + * .set_alarm to the upper RTC layer + */ + memset(&rtc->alarm, 0, sizeof(struct rtc_wkalrm)); + + writel(0, rtc->ioaddr + LPC_LPA_MSB_OFF); + writel(0, rtc->ioaddr + LPC_LPA_LSB_OFF); + writel(1, rtc->ioaddr + LPC_WDT_OFF); + writel(1, rtc->ioaddr + LPC_LPA_START_OFF); + writel(0, rtc->ioaddr + LPC_WDT_OFF); + + return 0; +} +#endif + +static SIMPLE_DEV_PM_OPS(st_rtc_pm_ops, st_rtc_suspend, st_rtc_resume); + +static struct of_device_id st_rtc_match[] = { + { .compatible = "st,stih407-lpc" }, + {} +}; +MODULE_DEVICE_TABLE(of, st_wdog_match); + +static struct platform_driver st_rtc_platform_driver = { + .driver = { + .name = "st-lpc-rtc", + .pm = &st_rtc_pm_ops, + .of_match_table = st_rtc_match, + }, + .probe = st_rtc_probe, + .remove = st_rtc_remove, +}; + +module_platform_driver(st_rtc_platform_driver); + +MODULE_DESCRIPTION("STMicroelectronics LPC RTC driver"); +MODULE_AUTHOR("David Paris "); +MODULE_LICENSE("GPLv2"); diff --git a/drivers/watchdog/st_wdt.c b/drivers/watchdog/st_wdt.c index cf3a4f7..166b57c 100644 --- a/drivers/watchdog/st_wdt.c +++ b/drivers/watchdog/st_wdt.c @@ -281,12 +281,16 @@ static int st_wdog_remove(struct platform_device *pdev) return 0; } -#ifdef CONFIG_PM +#ifdef CONFIG_PM_SLEEP static int st_wdog_suspend(struct device *dev) { + struct st_wdog *st_wdog = watchdog_get_drvdata(&st_wdog_dev); + if (watchdog_active(&st_wdog_dev)) st_wdog_stop(&st_wdog_dev); + st_wdog_setup(st_wdog, false); + return 0; } @@ -294,6 +298,8 @@ static int st_wdog_resume(struct device *dev) { struct st_wdog *st_wdog = watchdog_get_drvdata(&st_wdog_dev); + st_wdog_setup(st_wdog, true); + if (watchdog_active(&st_wdog_dev)) { st_wdog_load_timer(st_wdog, st_wdog_dev.timeout); st_wdog_start(&st_wdog_dev);