From patchwork Tue Jun 16 09:39:47 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: rnd4@dave-tech.it X-Patchwork-Id: 484888 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from mail-lb0-x23e.google.com (mail-lb0-x23e.google.com [IPv6:2a00:1450:4010:c04::23e]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 094D41402A5 for ; Tue, 16 Jun 2015 19:39:57 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=googlegroups.com header.i=@googlegroups.com header.b=vvIxb3d4; dkim-atps=neutral Received: by lbiv13 with SMTP id v13sf2049346lbi.0 for ; Tue, 16 Jun 2015 02:39:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlegroups.com; s=20120806; h=mime-version:from:to:cc:subject:date:message-id:in-reply-to :references:x-original-sender:x-original-authentication-results :reply-to:content-type:precedence:mailing-list:list-id :x-spam-checked-in-group:list-post:list-help:list-archive:sender :list-subscribe:list-unsubscribe; bh=hbJ7kqakeSeMbki0QDK+qmAWE7h7PucNOxTnSHOitaU=; b=vvIxb3d4qce3cNkTW5nedxuukknKbjn6X2bzNm3LExbKZTlEHbBhXYA1BsqnGP/Swq HxXsW3+cJ68hQWcoRp6L+RXP/3+YiUM2fgi9CE4Sy9sZfisO0VRfEe2EnahOjPTRYdMB QxCkW+HFzfZpQLln3GOHSqAf7WVWndwrkVtgsWF+SsaTp7UsbGPmQUMXwcRCgcDQXRPO bGGbv2W6/Tjo07p7xy21K0uwfTW/46zsKoRiX8+dGteJEQsFmg6n8CBaI5AEhkkq+peX lDbE9f1+5R8+2UECgSb0Zh04QW0mlqu/NRg4JD7dKQgc2uqpvV+u4bNGP2V87BWikjUW +eiA== X-Received: by 10.152.9.201 with SMTP id c9mr342710lab.37.1434447592692; Tue, 16 Jun 2015 02:39:52 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: rtc-linux@googlegroups.com Received: by 10.152.179.39 with SMTP id dd7ls67771lac.60.gmail; Tue, 16 Jun 2015 02:39:51 -0700 (PDT) X-Received: by 10.112.171.41 with SMTP id ar9mr4764887lbc.24.1434447591722; Tue, 16 Jun 2015 02:39:51 -0700 (PDT) Received: from mx.dave-tech.it (mx.dave-tech.it. [2.229.21.40]) by gmr-mx.google.com with ESMTP id gt9si61812wib.2.2015.06.16.02.39.51 for ; Tue, 16 Jun 2015 02:39:51 -0700 (PDT) Received-SPF: neutral (google.com: 2.229.21.40 is neither permitted nor denied by best guess record for domain of amon@linuxserver2.dave-lan) client-ip=2.229.21.40; Received: from linuxserver2.dave-lan (unknown [192.168.0.23]) by mx.dave-tech.it (Postfix) with ESMTP id 3924BA4; Tue, 16 Jun 2015 11:39:51 +0200 (CEST) Received: by linuxserver2.dave-lan (Postfix, from userid 500) id 30ABA405A2; Tue, 16 Jun 2015 11:39:51 +0200 (CEST) From: rnd4@dave-tech.it To: alexandre.belloni@free-electrons.com Cc: a.zummo@towertech.it, rtc-linux@googlegroups.com, linux-kernel@vger.kernel.org, Andrea Scian Subject: [rtc-linux] [PATCH v2] driver: rtc: pcf2127: use OFS flag to detect unreliable date and warn the user Date: Tue, 16 Jun 2015 11:39:47 +0200 Message-Id: <1434447587-23992-1-git-send-email-rnd4@dave-tech.it> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <557EF5DD.9070300@dave-tech.it> References: <557EF5DD.9070300@dave-tech.it> X-Original-Sender: rnd4@dave-tech.it X-Original-Authentication-Results: gmr-mx.google.com; spf=neutral (google.com: 2.229.21.40 is neither permitted nor denied by best guess record for domain of amon@linuxserver2.dave-lan) smtp.mail=amon@linuxserver2.dave-lan Reply-To: rtc-linux@googlegroups.com Precedence: list Mailing-list: list rtc-linux@googlegroups.com; contact rtc-linux+owners@googlegroups.com List-ID: X-Spam-Checked-In-Group: rtc-linux@googlegroups.com X-Google-Group-Id: 712029733259 List-Post: , List-Help: , List-Archive: , List-Unsubscribe: , From: Andrea Scian Signed-off-by: Andrea Scian --- Changes since v1: * use BIT() to select the right bitfield instead of hardcode value * return -EINVAL if OFS is detected * cache OSF into driver structure to warn the user even later drivers/rtc/rtc-pcf2127.c | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/drivers/rtc/rtc-pcf2127.c b/drivers/rtc/rtc-pcf2127.c index 7f803b2..aecee0b 100644 --- a/drivers/rtc/rtc-pcf2127.c +++ b/drivers/rtc/rtc-pcf2127.c @@ -33,11 +33,14 @@ #define PCF2127_REG_MO (0x08) #define PCF2127_REG_YR (0x09) +#define PCF2127_OSF BIT(7) /* Oscillator Fail flag */ + static struct i2c_driver pcf2127_driver; struct pcf2127 { struct rtc_device *rtc; int voltage_low; /* indicates if a low_voltage was detected */ + int oscillator_failed; /* OSF was detected and date in unreliable */ }; /* @@ -59,7 +62,18 @@ static int pcf2127_get_datetime(struct i2c_client *client, struct rtc_time *tm) if (buf[PCF2127_REG_CTRL3] & 0x04) { pcf2127->voltage_low = 1; dev_info(&client->dev, - "low voltage detected, date/time is not reliable.\n"); + "low voltage detected, check/replace RTC battery.\n"); + } + + if (buf[PCF2127_REG_SC] & PCF2127_OSF) { + /* + * no need clear the flag here, + * it will be cleared once the new date is saved + */ + pcf2127->oscillator_failed = 1; + dev_warn(&client->dev, + "oscillator stop detected, date/time is not reliable\n"); + return -EINVAL; } dev_dbg(&client->dev, @@ -93,6 +107,7 @@ static int pcf2127_get_datetime(struct i2c_client *client, struct rtc_time *tm) static int pcf2127_set_datetime(struct i2c_client *client, struct rtc_time *tm) { + struct pcf2127 *pcf2127 = i2c_get_clientdata(client); unsigned char buf[8]; int i = 0, err; @@ -106,7 +121,7 @@ static int pcf2127_set_datetime(struct i2c_client *client, struct rtc_time *tm) buf[i++] = PCF2127_REG_SC; /* hours, minutes and seconds */ - buf[i++] = bin2bcd(tm->tm_sec); + buf[i++] = bin2bcd(tm->tm_sec); /* this will also clear OFS flag */ buf[i++] = bin2bcd(tm->tm_min); buf[i++] = bin2bcd(tm->tm_hour); buf[i++] = bin2bcd(tm->tm_mday); @@ -126,6 +141,9 @@ static int pcf2127_set_datetime(struct i2c_client *client, struct rtc_time *tm) return -EIO; } + /* clear OSF flag in client data */ + pcf2127->oscillator_failed = 0; + return 0; } @@ -138,7 +156,9 @@ static int pcf2127_rtc_ioctl(struct device *dev, switch (cmd) { case RTC_VL_READ: if (pcf2127->voltage_low) - dev_info(dev, "low voltage detected, date/time is not reliable.\n"); + dev_info(dev, "low voltage detected, check/replace battery\n"); + if (pcf2127->oscillator_failed) + dev_info(dev, "oscillator stop detected, date/time is not reliable\n"); if (copy_to_user((void __user *)arg, &pcf2127->voltage_low, sizeof(int)))