From patchwork Fri Aug 13 20:35:22 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Retanubun X-Patchwork-Id: 61709 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from mail-gy0-f184.google.com (mail-gy0-f184.google.com [209.85.160.184]) by ozlabs.org (Postfix) with ESMTP id C0F56B70D0 for ; Sat, 14 Aug 2010 06:35:27 +1000 (EST) Received: by gyh4 with SMTP id 4sf5164184gyh.11 for ; Fri, 13 Aug 2010 13:35:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlegroups.com; s=beta; h=domainkey-signature:received:x-beenthere:received:received:received :received:received-spf:received:received:received:message-id:date :from:organization:user-agent:mime-version:to:cc:subject :x-original-sender:x-original-authentication-results:reply-to :precedence:mailing-list:list-id:list-post:list-help:list-archive :sender:list-subscribe:list-unsubscribe:content-type; bh=iNIvcOgevNsji65B0o7Gj4CibUoDXXW2cN2cjc8jPkM=; b=qiYDwpPnLHsOE0VRin1YHBCzvilUHfcdyId5Nb1eXZLnqe/hoUDCAd5L79CZAxDqnp h+UIlWMppKkTTLyE3pmcCwR7EsF7PXxTll9LJEhA9JH2K4BKakO1g6wr0MuNKV1JiRNn D9ziZlP4vELy0fm5/DHcnzcc69xfqS+7OFbKA= DomainKey-Signature: a=rsa-sha1; c=nofws; d=googlegroups.com; s=beta; h=x-beenthere:received-spf:message-id:date:from:organization :user-agent:mime-version:to:cc:subject:x-original-sender :x-original-authentication-results:reply-to:precedence:mailing-list :list-id:list-post:list-help:list-archive:sender:list-subscribe :list-unsubscribe:content-type; b=JggRAH9fM4qSJ1kQgQHmG2m7ZK48DHmk17YwDAFMDtr2E9KTmHytFW9Z6/c0ml04TN 7sBRZ/Avm7+LUQO5bdTXFBgZY5f+2TscFRUHIrtZJMtquJy9TXJHp1aVKS3GFHPAWdMg Ry4jhEMOte5HJob5RtSPy4nGDixyLq/+xFOOE= Received: by 10.90.81.13 with SMTP id e13mr359115agb.22.1281731724847; Fri, 13 Aug 2010 13:35:24 -0700 (PDT) X-BeenThere: rtc-linux@googlegroups.com Received: by 10.231.55.74 with SMTP id t10ls363124ibg.3.p; Fri, 13 Aug 2010 13:35:24 -0700 (PDT) Received: by 10.231.183.71 with SMTP id cf7mr86196ibb.17.1281731724365; Fri, 13 Aug 2010 13:35:24 -0700 (PDT) Received: by 10.231.183.71 with SMTP id cf7mr86195ibb.17.1281731724329; Fri, 13 Aug 2010 13:35:24 -0700 (PDT) Received: from mail2.ruggedcom.com (mail2.ruggedcom.com [204.50.148.11]) by gmr-mx.google.com with ESMTP id z11si994681ibb.4.2010.08.13.13.35.24; Fri, 13 Aug 2010 13:35:24 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of RichardRetanubun@ruggedcom.com designates 204.50.148.11 as permitted sender) client-ip=204.50.148.11; Received: from rceng01.eng.lan (10.200.2.11) by RCexchangesvr1 (10.200.2.21) with Microsoft SMTP Server id 8.2.176.0; Fri, 13 Aug 2010 16:35:37 -0400 Received: from richardretanubun.eng.lan (unknown [10.200.20.26]) by rceng01.eng.lan (Postfix) with ESMTP id 754B114E7F8; Fri, 13 Aug 2010 16:35:23 -0400 (EDT) Received: from localhost ([127.0.0.1] ident=richardretanubun) by richardretanubun.eng.lan with esmtp (Exim 4.72) (envelope-from ) id 1Ok0yJ-00016X-5Y; Fri, 13 Aug 2010 16:35:23 -0400 Message-ID: <4C65AC8A.1070505@RuggedCom.com> Date: Fri, 13 Aug 2010 16:35:22 -0400 From: Richard Retanubun Organization: RuggedCom User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.10) Gecko/20100619 Iceowl/1.0b1 Icedove/3.0.5 MIME-Version: 1.0 To: a.zummo@towertech.it CC: rtc-linux@googlegroups.com Subject: [rtc-linux] Subject: [PATCH] rtc-m41t80: Check stored time validitiy during device probe. X-Original-Sender: richardretanubun@ruggedcom.com X-Original-Authentication-Results: gmr-mx.google.com; spf=pass (google.com: best guess record for domain of RichardRetanubun@ruggedcom.com designates 204.50.148.11 as permitted sender) smtp.mail=RichardRetanubun@ruggedcom.com Reply-To: rtc-linux@googlegroups.com Precedence: list Mailing-list: list rtc-linux@googlegroups.com; contact rtc-linux+owners@googlegroups.com List-ID: List-Post: , List-Help: , List-Archive: Sender: rtc-linux@googlegroups.com List-Subscribe: , List-Unsubscribe: , From 57e07fc70021602ea02c0842817ba1a4e06c0ee7 Mon Sep 17 00:00:00 2001 From: Richard Retanubun Date: Fri, 13 Aug 2010 14:45:16 -0400 Subject: [PATCH] rtc-m41t80: Check stored time validitiy during device probe. When the battery is gone on rtc-m41t80, data integrity is always in question. Check the validity of the returned date time. In addition, if the time is found to be invalid during device probe, initialize the date time to a sane default value. --- The checking validity during reads are already mainlined with commit b485fe5ea1008db02abff9ef15be4f31b52df4f7 My change simply adds a typecast to remove compiler warning for m41t80_rtc_read_alarm The valid time check during device probe is what I am proposing, what the default time should be is probably up for debate I made it a #def to make it easy to customize. drivers/rtc/rtc-m41t80.c | 49 +++++++++++++++++++++++++++++++++++++-------- 1 files changed, 40 insertions(+), 9 deletions(-) -- 1.7.1 diff --git a/drivers/rtc/rtc-m41t80.c b/drivers/rtc/rtc-m41t80.c index 60fe266..9ac82b1 100644 --- a/drivers/rtc/rtc-m41t80.c +++ b/drivers/rtc/rtc-m41t80.c @@ -66,6 +66,8 @@ #define M41T80_FEATURE_WD (1 << 3) /* Extra watchdog resolution */ #define M41T80_FEATURE_SQ_ALT (1 << 4) /* RSx bits are in reg 4 */ +#define M41T80_DEFAULT_TIME 1262304000 /* 2010-01-01 00:00:00 UTC */ + #define DRV_VERSION "0.05" static const struct i2c_device_id m41t80_id[] = { @@ -121,7 +123,7 @@ static int m41t80_get_datetime(struct i2c_client *client, /* assume 20YY not 19YY, and ignore the Century Bit */ tm->tm_year = bcd2bin(buf[M41T80_REG_YEAR]) + 100; - return 0; + return rtc_valid_tm(tm); } /* Sets the given date and time to the real time clock. */ @@ -364,7 +366,7 @@ static int m41t80_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *t) t->time.tm_isdst = -1; t->enabled = !!(reg[M41T80_REG_ALARM_MON] & M41T80_ALMON_AFE); t->pending = !!(reg[M41T80_REG_FLAGS] & M41T80_FLAGS_AF); - return 0; + return rtc_valid_tm((struct rtc_time *)t); } static struct rtc_class_ops m41t80_rtc_ops = { @@ -806,21 +808,50 @@ static int m41t80_probe(struct i2c_client *client, if (rc & M41T80_ALHOUR_HT) { if (clientdata->features & M41T80_FEATURE_HT) { - m41t80_get_datetime(client, &tm); dev_info(&client->dev, "HT bit was set!\n"); - dev_info(&client->dev, - "Power Down at " - "%04i-%02i-%02i %02i:%02i:%02i\n", - tm.tm_year + 1900, - tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, - tm.tm_min, tm.tm_sec); + + /* Show power down time only if it is valid */ + if (m41t80_get_datetime(client, &tm) == 0) { + dev_info(&client->dev, + "Power Down at " + "%04i-%02i-%02i %02i:%02i:%02i\n", + tm.tm_year + 1900, + tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, + tm.tm_min, tm.tm_sec); + } } + /* Clear the HT bit */ if (i2c_smbus_write_byte_data(client, M41T80_REG_ALARM_HOUR, rc & ~M41T80_ALHOUR_HT) < 0) goto ht_err; } + /* + * check if RTC time is valid, if it is not valid, set it to known + * default. The check is done here to make sure HT bit is + * already checked and cleared before time is set. + */ + if (m41t80_get_datetime(client, &tm) != 0) { + dev_err(&client->dev, + "Invalid date %04i-%02i-%02i %02i:%02i:%02i\n", + tm.tm_year + 1900, + tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, + tm.tm_min, tm.tm_sec); + + rtc_time_to_tm(M41T80_DEFAULT_TIME, &tm); + + dev_err(&client->dev, + "Date defaulted to %04i-%02i-%02i %02i:%02i:%02i\n", + tm.tm_year + 1900, + tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, + tm.tm_min, tm.tm_sec); + + if (m41t80_set_datetime(client, &tm) != 0) { + goto exit; + } + } + /* Make sure ST (stop) bit is cleared */ rc = i2c_smbus_read_byte_data(client, M41T80_REG_SEC); if (rc < 0)