From patchwork Thu Aug 22 13:19:32 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bruno Thomsen X-Patchwork-Id: 11109279 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 29A11912 for ; Thu, 22 Aug 2019 13:19:52 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 04951233A0 for ; Thu, 22 Aug 2019 13:19:52 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="KW7054+X" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732323AbfHVNTv (ORCPT ); Thu, 22 Aug 2019 09:19:51 -0400 Received: from mail-wm1-f68.google.com ([209.85.128.68]:51764 "EHLO mail-wm1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730621AbfHVNTv (ORCPT ); Thu, 22 Aug 2019 09:19:51 -0400 Received: by mail-wm1-f68.google.com with SMTP id k1so5626645wmi.1; Thu, 22 Aug 2019 06:19:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=ddD9APKRjXleO36Xs3naLTEd/IAvAOuyDBxcB3/l6RE=; b=KW7054+XQ7ewqXyjahG/huCwZ5HAssdkIt/hab63CWZoC89MOHcnpHYU0X6yODmlp2 N380G9NR9JjrEVP1ZgimWq4Q0rRsGqorTbhC8dvY4qDfIN51DzOaSOhYII3sL7E2de0T obwNVydsqICf5LBE5EFU4kwA9GrUK+BRYBJJuKJmzteXtGYwKSma1n2JCTNicJNIg8pk 8pDY5R6VtLcx1kp4RCjZ8x00qflilBvrs4OEXBzpN2xr7yG60Y0GLo4hgbtIO/API7PU iDJXMaKAt6LfI7rNtS/RAp61B00gemAcyDPfelr/4q15SCpiTUSAq/0bmfOBKXrg/ji7 V1pw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=ddD9APKRjXleO36Xs3naLTEd/IAvAOuyDBxcB3/l6RE=; b=Gc033YMTfIrukOQ2kqkdblva1igAA9lBhk5vmnSfLUKpc/oZtIKj5ykhuxD7rvzWQ/ 1+UCFo+zzOUXMJtnMQAggoWeTOmgYgAktiRa2xrvXRbhhUKYG/rv0pMyEv7OIQlkYI70 TlcVUQiOOESgmtuiv2UYW4ifXDs6/OXcw8nGt/wsndAdG5UqNLN3j1g+dGLZNm4ygcvk 7sPWIbLpqq7Ex/qxj12s3zL6ZjnT30VyCvK0P0t1ip6769tajm4UEZ0sb5y66sdg6CWj 5a0qa1ofAn23No4RH6KvZVnp1mnnegQUjn1n8LyIII1S9qRBoXC2suCBlY2xIQshQYzy rAEw== X-Gm-Message-State: APjAAAWynQ5nnca31zCNLIQpdMP5VA0ZYZQPt0AF9RgBOANEvJG0CU8G Ftznmnc6G2fBx9YvgP6VcIP+n5JghrV76w== X-Google-Smtp-Source: APXvYqxZL7YUSunqPC7/AS91BwMbMyqOjmZhe4tKxkizMwfLl2FJD+RCl/trAs1BIYNzqeHrD8BP8A== X-Received: by 2002:a1c:9a95:: with SMTP id c143mr6364700wme.2.1566479989188; Thu, 22 Aug 2019 06:19:49 -0700 (PDT) Received: from localhost.localdomain (3e6b1cc1.rev.stofanet.dk. [62.107.28.193]) by smtp.googlemail.com with ESMTPSA id d19sm34644045wrb.7.2019.08.22.06.19.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Aug 2019 06:19:48 -0700 (PDT) From: Bruno Thomsen To: linux-rtc@vger.kernel.org, linux-watchdog@vger.kernel.org Cc: alexandre.belloni@bootlin.com, a.zummo@towertech.it, wim@linux-watchdog.org, linux@roeck-us.net, u.kleine-koenig@pengutronix.de, bth@kamstrup.com, bruno.thomsen@gmail.com Subject: [PATCH v3 1/5] rtc: pcf2127: convert to devm_rtc_allocate_device Date: Thu, 22 Aug 2019 15:19:32 +0200 Message-Id: <20190822131936.18772-1-bruno.thomsen@gmail.com> X-Mailer: git-send-email 2.21.0 MIME-Version: 1.0 Sender: linux-watchdog-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-watchdog@vger.kernel.org This allows further improvement of the driver. Signed-off-by: Bruno Thomsen --- v3: no change. v2: no change. drivers/rtc/rtc-pcf2127.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/drivers/rtc/rtc-pcf2127.c b/drivers/rtc/rtc-pcf2127.c index 8632f58fed43..58eb96506e4b 100644 --- a/drivers/rtc/rtc-pcf2127.c +++ b/drivers/rtc/rtc-pcf2127.c @@ -237,11 +237,12 @@ static int pcf2127_probe(struct device *dev, struct regmap *regmap, dev_set_drvdata(dev, pcf2127); - pcf2127->rtc = devm_rtc_device_register(dev, name, &pcf2127_rtc_ops, - THIS_MODULE); + pcf2127->rtc = devm_rtc_allocate_device(dev); if (IS_ERR(pcf2127->rtc)) return PTR_ERR(pcf2127->rtc); + pcf2127->rtc->ops = &pcf2127_rtc_ops; + if (has_nvmem) { struct nvmem_config nvmem_cfg = { .priv = pcf2127, @@ -253,7 +254,7 @@ static int pcf2127_probe(struct device *dev, struct regmap *regmap, ret = rtc_nvmem_register(pcf2127->rtc, &nvmem_cfg); } - return ret; + return rtc_register_device(pcf2127->rtc); } #ifdef CONFIG_OF From patchwork Thu Aug 22 13:19:33 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bruno Thomsen X-Patchwork-Id: 11109281 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id ED23C13B1 for ; Thu, 22 Aug 2019 13:19:53 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id C14F1233A0 for ; Thu, 22 Aug 2019 13:19:53 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="R/dOtmqZ" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732333AbfHVNTx (ORCPT ); Thu, 22 Aug 2019 09:19:53 -0400 Received: from mail-wr1-f66.google.com ([209.85.221.66]:41516 "EHLO mail-wr1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732317AbfHVNTx (ORCPT ); Thu, 22 Aug 2019 09:19:53 -0400 Received: by mail-wr1-f66.google.com with SMTP id j16so5401505wrr.8; Thu, 22 Aug 2019 06:19:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Q+0GMcqTHAEC+PuCJzygw3HvGYKmBoMy49VZ9nky2OE=; b=R/dOtmqZ0cEogDXNW5NFt+5Ea5bFYFZNpKYgQuNBheXezcy6KkdHF09bbUXxnIP69O 9BQnD6yh5xokO5yEGlD/cA5MoNhNR+nEls3uW8wwKLoGAZScUGEVSGmFXDUmupHY4v/S lF+hhUFJuvuAiDcH2Y1nTFjnK8aY/4bcWhgmLxAW7f24ddUev/7K6FXykvU5PhrmFKYI A1Ut25Mij0SNumgAVeKp71pzf7XiFYAtqgzrxMbZ6mWW6Cv8iJHFYiDLjAqIRY2quvw9 GKZyHHsQcF4ehDYRs1SJhqpZai3j6snvBIHPmQWSwb+498dirczKvsVGQd/EVQpVhIWn 5NyQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Q+0GMcqTHAEC+PuCJzygw3HvGYKmBoMy49VZ9nky2OE=; b=SG+QRsAfGOlJsOu8p0oa3QEZxhr3Hw24t3OCgqOjzSK6ucdTkTgiL1ua3i2s/y48NI oR86867zDHbis/BIorUwBLAGEfz7+YbfUqEelcJP50BDdPrePzGQaulrDvZZ8FJsOfWF cudn0IdW1AV4roOqbJ6BuuQYrJwOQ+xcujFd/yYltZ8ajAGYAsuZT9N8cOyopS5wH6tv nxcBT6q09VMv9DXz/5FFkU8gH+kSqzBckTfoBoFChMxaY0ZR/ji0ypFJXqnSv0eYjL7w cwsYQcHIWII+iw6x6EghXICpnlCl1Ifjsdj+GSYqoRbYjq4tUiNvp03B2Ah4UlKqdr6K tXOQ== X-Gm-Message-State: APjAAAU7UeuZPVu67vJwKzYYbODMWvZ/vyfjRBOg77NV2y7JPoMrAQmS KuwlC8VUsI5BoNPoCP5rhvtGqQoRsICdJQ== X-Google-Smtp-Source: APXvYqw2wCLyD+oOzRy7pfTTINejF54Dl9dtIHNp4PWG43M68gjz9s49AxEVRrAgKwkrA7Dj34OzLQ== X-Received: by 2002:adf:fd82:: with SMTP id d2mr45331963wrr.194.1566479990431; Thu, 22 Aug 2019 06:19:50 -0700 (PDT) Received: from localhost.localdomain (3e6b1cc1.rev.stofanet.dk. [62.107.28.193]) by smtp.googlemail.com with ESMTPSA id d19sm34644045wrb.7.2019.08.22.06.19.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Aug 2019 06:19:49 -0700 (PDT) From: Bruno Thomsen To: linux-rtc@vger.kernel.org, linux-watchdog@vger.kernel.org Cc: alexandre.belloni@bootlin.com, a.zummo@towertech.it, wim@linux-watchdog.org, linux@roeck-us.net, u.kleine-koenig@pengutronix.de, bth@kamstrup.com, bruno.thomsen@gmail.com Subject: [PATCH v3 2/5] rtc: pcf2127: cleanup register and bit defines Date: Thu, 22 Aug 2019 15:19:33 +0200 Message-Id: <20190822131936.18772-2-bruno.thomsen@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190822131936.18772-1-bruno.thomsen@gmail.com> References: <20190822131936.18772-1-bruno.thomsen@gmail.com> MIME-Version: 1.0 Sender: linux-watchdog-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-watchdog@vger.kernel.org Cleanup of defines to follow kernel coding style and increase code readability by using same register and bit define style. Change PCF2127_REG_RAM_{addr_MSB,wrt_cmd,rd_cmd} to upper case as kernel coding guide section 12 'Macros, Enums and RTL' states "Names of macros defining constants and labels in enums are capitalized". Improve readability of RAM register comment by making whole sentences. Remove parentheses from register defines as they are only used for expressions and not constants. As there are no clear style for name of registers and bits in the kernel drivers, I suggest the following for at least this driver, but hopefully also other RTC drivers. Register name should follow this convention: [chip]_REG_[reg name] 0xXX Bit name should follow this convention, so it clearly states which chip register it's part of: [chip]_BIT_[reg name]_[bit name] BIT(X) Additionally I suggest bit defines are always placed right below its corresponding register define and using an extra tab indentation for the BIT(X) part. This will visualt make it easy to see that bit defines are part of the complete register definition. Rename PCF2127_OSF to PCF2127_BIT_SC_OSF and move it right below PCF2127_REG_SC. This will improve readability of bit checks as it's easy to verify that it uses the correct register. Move end of line comments above register defines as it's more like a heading for 1 register define and up to 8 bit defines or a collection of registers that are close related like timestamp split across 6 registers. Signed-off-by: Bruno Thomsen --- v3: no change. v2: updated commit message. drivers/rtc/rtc-pcf2127.c | 59 ++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 26 deletions(-) diff --git a/drivers/rtc/rtc-pcf2127.c b/drivers/rtc/rtc-pcf2127.c index 58eb96506e4b..cd8def79b379 100644 --- a/drivers/rtc/rtc-pcf2127.c +++ b/drivers/rtc/rtc-pcf2127.c @@ -19,26 +19,32 @@ #include #include -#define PCF2127_REG_CTRL1 (0x00) /* Control Register 1 */ -#define PCF2127_REG_CTRL2 (0x01) /* Control Register 2 */ - -#define PCF2127_REG_CTRL3 (0x02) /* Control Register 3 */ -#define PCF2127_REG_CTRL3_BLF BIT(2) - -#define PCF2127_REG_SC (0x03) /* datetime */ -#define PCF2127_REG_MN (0x04) -#define PCF2127_REG_HR (0x05) -#define PCF2127_REG_DM (0x06) -#define PCF2127_REG_DW (0x07) -#define PCF2127_REG_MO (0x08) -#define PCF2127_REG_YR (0x09) - -/* the pcf2127 has 512 bytes nvmem, pcf2129 doesn't */ -#define PCF2127_REG_RAM_addr_MSB 0x1a -#define PCF2127_REG_RAM_wrt_cmd 0x1c -#define PCF2127_REG_RAM_rd_cmd 0x1d +/* Control register 1 */ +#define PCF2127_REG_CTRL1 0x00 +/* Control register 2 */ +#define PCF2127_REG_CTRL2 0x01 +/* Control register 3 */ +#define PCF2127_REG_CTRL3 0x02 +#define PCF2127_BIT_CTRL3_BLF BIT(2) +/* Time and date registers */ +#define PCF2127_REG_SC 0x03 +#define PCF2127_BIT_SC_OSF BIT(7) +#define PCF2127_REG_MN 0x04 +#define PCF2127_REG_HR 0x05 +#define PCF2127_REG_DM 0x06 +#define PCF2127_REG_DW 0x07 +#define PCF2127_REG_MO 0x08 +#define PCF2127_REG_YR 0x09 +/* + * RAM registers + * PCF2127 has 512 bytes general-purpose static RAM (SRAM) that is + * battery backed and can survive a power outage. + * PCF2129 doesn't have this feature. + */ +#define PCF2127_REG_RAM_ADDR_MSB 0x1A +#define PCF2127_REG_RAM_WRT_CMD 0x1C +#define PCF2127_REG_RAM_RD_CMD 0x1D -#define PCF2127_OSF BIT(7) /* Oscillator Fail flag */ struct pcf2127 { struct rtc_device *rtc; @@ -73,11 +79,12 @@ static int pcf2127_rtc_read_time(struct device *dev, struct rtc_time *tm) return ret; } - if (buf[PCF2127_REG_CTRL3] & PCF2127_REG_CTRL3_BLF) + if (buf[PCF2127_REG_CTRL3] & PCF2127_BIT_CTRL3_BLF) dev_info(dev, "low voltage detected, check/replace RTC battery.\n"); - if (buf[PCF2127_REG_SC] & PCF2127_OSF) { + /* Clock integrity is not guaranteed when OSF flag is set. */ + if (buf[PCF2127_REG_SC] & PCF2127_BIT_SC_OSF) { /* * no need clear the flag here, * it will be cleared once the new date is saved @@ -166,7 +173,7 @@ static int pcf2127_rtc_ioctl(struct device *dev, if (ret) return ret; - touser = touser & PCF2127_REG_CTRL3_BLF ? 1 : 0; + touser = touser & PCF2127_BIT_CTRL3_BLF ? 1 : 0; if (copy_to_user((void __user *)arg, &touser, sizeof(int))) return -EFAULT; @@ -192,12 +199,12 @@ static int pcf2127_nvmem_read(void *priv, unsigned int offset, int ret; unsigned char offsetbuf[] = { offset >> 8, offset }; - ret = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_RAM_addr_MSB, + ret = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_RAM_ADDR_MSB, offsetbuf, 2); if (ret) return ret; - ret = regmap_bulk_read(pcf2127->regmap, PCF2127_REG_RAM_rd_cmd, + ret = regmap_bulk_read(pcf2127->regmap, PCF2127_REG_RAM_RD_CMD, val, bytes); return ret ?: bytes; @@ -210,12 +217,12 @@ static int pcf2127_nvmem_write(void *priv, unsigned int offset, int ret; unsigned char offsetbuf[] = { offset >> 8, offset }; - ret = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_RAM_addr_MSB, + ret = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_RAM_ADDR_MSB, offsetbuf, 2); if (ret) return ret; - ret = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_RAM_wrt_cmd, + ret = regmap_bulk_write(pcf2127->regmap, PCF2127_REG_RAM_WRT_CMD, val, bytes); return ret ?: bytes; From patchwork Thu Aug 22 13:19:34 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bruno Thomsen X-Patchwork-Id: 11109283 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id CEB5613B1 for ; Thu, 22 Aug 2019 13:19:54 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id ACBB82173E for ; Thu, 22 Aug 2019 13:19:54 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="LNFVTHkw" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732355AbfHVNTy (ORCPT ); Thu, 22 Aug 2019 09:19:54 -0400 Received: from mail-wr1-f68.google.com ([209.85.221.68]:42879 "EHLO mail-wr1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730621AbfHVNTy (ORCPT ); Thu, 22 Aug 2019 09:19:54 -0400 Received: by mail-wr1-f68.google.com with SMTP id b16so5387719wrq.9; Thu, 22 Aug 2019 06:19:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=YtEgS3a59qGIbOV1HTWE1e/qiDN5d44GwGNe2nrAAsw=; b=LNFVTHkwXpEC27df6ulXj/LaWk+0z4YZWALXX7JmDoiZnpIKjPd9Me2QAbyxUJy0PM J1wf8pygoANIe7z60eS9IY6DgTs3Rn+MiJgFu1dbpTFyeMyjz2SBsKNv/4KwtDPB60EP IaTCZTwdW1F0mL3+ytqSpdk5gOv3aQFmniDN4eshIZjd6BTRwjFKAJ7MN6tEJImrojtS YwF5I1lryOOlZenqUuh3oAr3cLzAqT2/xhkUIaP1FjlUdkWTanrzb4iRghS1kyl7pnbb +Q0Ah0hg6oczBN+ZEeYq+G2xUwV0h3GCxUwVLUVtRtTIdazujgldoCnNad44n9ziIvMj R4iQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=YtEgS3a59qGIbOV1HTWE1e/qiDN5d44GwGNe2nrAAsw=; b=r7tsjaBaeV6wPZzQqJR80ZRd1n1fbgt6mncajGJNJc3B5+zG2K9hubPt4jsd1w21kW 1Ihua2thgsLXzyLbMf5dZRMJ4Aa7MNmIiIvTMq89L5UmcNZqL4pzQ/tOqI7XkK92yRlc wPKsUUUcWARVt4aOLveYfVcCOr7yAQxOauMc2n82ZgkNqjg7aV/Z656uvf2RMJnWvwro UwLX3atsVCKur+oQDRgbrXH1TuNpEhwNqD98tLn9kUiu1Cct5lxbfXZsNwof7PFVrYzm jvhDVoJsyd+PmcBgm5IieHi/QvF46dGY+Ylo//k3HlfzsppxREcYqfPQ3+YhtaBB1Jur UD+Q== X-Gm-Message-State: APjAAAVB0LIutu8psNDrbI3/CeYGh3rd/GLu4ArHy/Twq9QWOeNrBGKt ZaIF3waD6Rupa5yFsYD/4WwzWQMNenwaiw== X-Google-Smtp-Source: APXvYqwWtJafFn1aPf+tu5KDwOKO46F0H3k234BVWqRCS4gww4+FA8asmbYIjtkb4o9AcSDHCuw52Q== X-Received: by 2002:adf:f3d1:: with SMTP id g17mr46506253wrp.38.1566479991762; Thu, 22 Aug 2019 06:19:51 -0700 (PDT) Received: from localhost.localdomain (3e6b1cc1.rev.stofanet.dk. [62.107.28.193]) by smtp.googlemail.com with ESMTPSA id d19sm34644045wrb.7.2019.08.22.06.19.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Aug 2019 06:19:51 -0700 (PDT) From: Bruno Thomsen To: linux-rtc@vger.kernel.org, linux-watchdog@vger.kernel.org Cc: alexandre.belloni@bootlin.com, a.zummo@towertech.it, wim@linux-watchdog.org, linux@roeck-us.net, u.kleine-koenig@pengutronix.de, bth@kamstrup.com, bruno.thomsen@gmail.com Subject: [PATCH v3 3/5] rtc: pcf2127: bugfix: read rtc disables watchdog Date: Thu, 22 Aug 2019 15:19:34 +0200 Message-Id: <20190822131936.18772-3-bruno.thomsen@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190822131936.18772-1-bruno.thomsen@gmail.com> References: <20190822131936.18772-1-bruno.thomsen@gmail.com> MIME-Version: 1.0 Sender: linux-watchdog-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-watchdog@vger.kernel.org The previous fix listed bulk read of registers as root cause of accendential disabling of watchdog, since the watchdog counter register (WD_VAL) was zeroed. Fixes: 3769a375ab83 rtc: pcf2127: bulk read only date and time registers. Tested with the same PCF2127 chip as Sean reveled root cause of WD_VAL register value zeroing was caused by reading CTRL2 register which is one of the watchdog feature control registers. So the solution is to not read the first two control registers (CTRL1 and CTRL2) in pcf2127_rtc_read_time as they are not needed anyway. Size of local buf variable is kept to allow easy usage of register defines to improve readability of code. Debug trace line was updated after CTRL1 and CTRL2 are no longer read from the chip. Also replaced magic numbers in buf access with register defines. Signed-off-by: Bruno Thomsen --- v3: no change. v2: new bugfix, not in v1. drivers/rtc/rtc-pcf2127.c | 32 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/drivers/rtc/rtc-pcf2127.c b/drivers/rtc/rtc-pcf2127.c index cd8def79b379..ee4921e4a47c 100644 --- a/drivers/rtc/rtc-pcf2127.c +++ b/drivers/rtc/rtc-pcf2127.c @@ -60,20 +60,14 @@ static int pcf2127_rtc_read_time(struct device *dev, struct rtc_time *tm) struct pcf2127 *pcf2127 = dev_get_drvdata(dev); unsigned char buf[10]; int ret; - int i; - for (i = 0; i <= PCF2127_REG_CTRL3; i++) { - ret = regmap_read(pcf2127->regmap, PCF2127_REG_CTRL1 + i, - (unsigned int *)(buf + i)); - if (ret) { - dev_err(dev, "%s: read error\n", __func__); - return ret; - } - } - - ret = regmap_bulk_read(pcf2127->regmap, PCF2127_REG_SC, - (buf + PCF2127_REG_SC), - ARRAY_SIZE(buf) - PCF2127_REG_SC); + /* + * Avoid reading CTRL2 register as it causes WD_VAL register + * value to reset to 0 which means watchdog is stopped. + */ + ret = regmap_bulk_read(pcf2127->regmap, PCF2127_REG_CTRL3, + (buf + PCF2127_REG_CTRL3), + ARRAY_SIZE(buf) - PCF2127_REG_CTRL3); if (ret) { dev_err(dev, "%s: read error\n", __func__); return ret; @@ -95,14 +89,12 @@ static int pcf2127_rtc_read_time(struct device *dev, struct rtc_time *tm) } dev_dbg(dev, - "%s: raw data is cr1=%02x, cr2=%02x, cr3=%02x, " - "sec=%02x, min=%02x, hr=%02x, " + "%s: raw data is cr3=%02x, sec=%02x, min=%02x, hr=%02x, " "mday=%02x, wday=%02x, mon=%02x, year=%02x\n", - __func__, - buf[0], buf[1], buf[2], - buf[3], buf[4], buf[5], - buf[6], buf[7], buf[8], buf[9]); - + __func__, buf[PCF2127_REG_CTRL3], buf[PCF2127_REG_SC], + buf[PCF2127_REG_MN], buf[PCF2127_REG_HR], + buf[PCF2127_REG_DM], buf[PCF2127_REG_DW], + buf[PCF2127_REG_MO], buf[PCF2127_REG_YR]); tm->tm_sec = bcd2bin(buf[PCF2127_REG_SC] & 0x7F); tm->tm_min = bcd2bin(buf[PCF2127_REG_MN] & 0x7F); From patchwork Thu Aug 22 13:19:35 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bruno Thomsen X-Patchwork-Id: 11109285 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 178A5912 for ; Thu, 22 Aug 2019 13:19:57 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id E0D29233FE for ; Thu, 22 Aug 2019 13:19:56 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Cfc83J0v" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732938AbfHVNT4 (ORCPT ); Thu, 22 Aug 2019 09:19:56 -0400 Received: from mail-wm1-f66.google.com ([209.85.128.66]:37610 "EHLO mail-wm1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732317AbfHVNT4 (ORCPT ); Thu, 22 Aug 2019 09:19:56 -0400 Received: by mail-wm1-f66.google.com with SMTP id d16so5767951wme.2; Thu, 22 Aug 2019 06:19:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=92nMDvehPAphgE3W+p66OZbjPeMAsNp+BOuLVquGoGQ=; b=Cfc83J0v/Ew0hWKOMmfBXZss2fc7lThi1Ga1cootvccELcM5Q8ltoBOxm+ThqND2bw jQf7/niuivQqOUyG0aH4lnIFCOQDtRD96lmkDQs4ZcLHVkWofj9K2gzEh0WvlKLdI4fQ oM89CpYcbw6ujFtHWQUkDeU5UvHOyFqEajFpEZSRZXsKQSnGGkVOPNcj3Hf1yVxE0zMJ JZPPUSxKJrceM9M9w8LHBhY4sLw7x0FuSe3Ym4kFf2DG0jsQkbkzAmpFDXtRHEVPnOiz +B6/xMxzlnMq6gMTbPPF6sb4ohcE3CaDtgxvQk+RK/5wBI6bsKMskne0WMHpXFQi3ZHq rhNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=92nMDvehPAphgE3W+p66OZbjPeMAsNp+BOuLVquGoGQ=; b=q8+w0ndcu4YN8W1VEVnxzip3usjaNk06ImpcOk4lnTt0tb/ZoL64Zg3lgyhLFEkNpv bcTVxJ6aTYqPZb/7rsnBs6O48mY4FU7zQLwojvl09fh0IClbiLKUI5bnJHB27FBV7O7l 6na/nKKk7DHIG6ZQQvywWD3Vbj4bO/14bPvZNMjVAU2ND/6M1zDc57ESqobJLYHu5VG8 e6/YWLs7JxLyQm7hu7Z1z9mHdQUjj/88HWoiYwP4Z3sZcrbFgheoSw+IffvGAET4CxFH PLXNKuHTAgQ0qRhYRksBN2pO80usPVelvxjzDtQ0ADlTNc/tEKs0W4JcO4yKCiB+93dQ Bfcg== X-Gm-Message-State: APjAAAVRQSzS/SxdEBJTc6E+luvCbx5AHazNDaAxszMwbRvI/a0YzGKW f76SYP7q/mUkCpKld+ZhN/94brXeKcbomA== X-Google-Smtp-Source: APXvYqwLAHhGOvCky81U0xxNvcb/DWdmGy9cyXq9bf7LCgz3Tb/sK5fjhC0S+pY914Zt+MBo/lD7UQ== X-Received: by 2002:a05:600c:da:: with SMTP id u26mr6279036wmm.70.1566479992925; Thu, 22 Aug 2019 06:19:52 -0700 (PDT) Received: from localhost.localdomain (3e6b1cc1.rev.stofanet.dk. [62.107.28.193]) by smtp.googlemail.com with ESMTPSA id d19sm34644045wrb.7.2019.08.22.06.19.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Aug 2019 06:19:52 -0700 (PDT) From: Bruno Thomsen To: linux-rtc@vger.kernel.org, linux-watchdog@vger.kernel.org Cc: alexandre.belloni@bootlin.com, a.zummo@towertech.it, wim@linux-watchdog.org, linux@roeck-us.net, u.kleine-koenig@pengutronix.de, bth@kamstrup.com, bruno.thomsen@gmail.com Subject: [PATCH v3 4/5] rtc: pcf2127: add watchdog feature support Date: Thu, 22 Aug 2019 15:19:35 +0200 Message-Id: <20190822131936.18772-4-bruno.thomsen@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190822131936.18772-1-bruno.thomsen@gmail.com> References: <20190822131936.18772-1-bruno.thomsen@gmail.com> MIME-Version: 1.0 Sender: linux-watchdog-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-watchdog@vger.kernel.org Add partial support for the watchdog functionality of both PCF2127 and PCF2129 chips. The programmable watchdog timer is currently using a fixed clock source of 1Hz. This result in a selectable range of 1-255 seconds, which covers most embedded Linux use-cases. Clock sources of 4096Hz, 64Hz and 1/60Hz is mostly useful in MCU use-cases. Countdown timer not available when using watchdog feature. Signed-off-by: Bruno Thomsen Acked-by: Guenter Roeck --- v3: removed 2 x dev_info() and 1 x dev_err() traces. lowered dev_info() to dbg_info() in pcf2127_wdt_set_timeout. removed unneeded ret variable in pcf2127_wdt_set_timeout. v2: use new watchdog api, e.g. devm_watchdog_register_device. remove watchdog Kconfig option. update existing Kconfig option with additional information. drivers/rtc/Kconfig | 7 ++- drivers/rtc/rtc-pcf2127.c | 118 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 124 insertions(+), 1 deletion(-) diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig index e72f65b61176..a3bb58a08879 100644 --- a/drivers/rtc/Kconfig +++ b/drivers/rtc/Kconfig @@ -876,7 +876,12 @@ config RTC_DRV_PCF2127 depends on RTC_I2C_AND_SPI help If you say yes here you get support for the NXP PCF2127/29 RTC - chips. + chips with integrated quartz crystal for industrial applications. + Both chips also have watchdog timer and tamper switch detection + features. + + PCF2127 has an additional feature of 512 bytes battery backed + memory that's accessible using nvmem interface. This driver can also be built as a module. If so, the module will be called rtc-pcf2127. diff --git a/drivers/rtc/rtc-pcf2127.c b/drivers/rtc/rtc-pcf2127.c index ee4921e4a47c..8d6eda455d81 100644 --- a/drivers/rtc/rtc-pcf2127.c +++ b/drivers/rtc/rtc-pcf2127.c @@ -5,6 +5,9 @@ * * Author: Renaud Cerrato * + * Watchdog and tamper functions + * Author: Bruno Thomsen + * * based on the other drivers in this same directory. * * Datasheet: http://cache.nxp.com/documents/data_sheet/PCF2127.pdf @@ -18,6 +21,7 @@ #include #include #include +#include /* Control register 1 */ #define PCF2127_REG_CTRL1 0x00 @@ -35,6 +39,13 @@ #define PCF2127_REG_DW 0x07 #define PCF2127_REG_MO 0x08 #define PCF2127_REG_YR 0x09 +/* Watchdog registers */ +#define PCF2127_REG_WD_CTL 0x10 +#define PCF2127_BIT_WD_CTL_TF0 BIT(0) +#define PCF2127_BIT_WD_CTL_TF1 BIT(1) +#define PCF2127_BIT_WD_CTL_CD0 BIT(6) +#define PCF2127_BIT_WD_CTL_CD1 BIT(7) +#define PCF2127_REG_WD_VAL 0x11 /* * RAM registers * PCF2127 has 512 bytes general-purpose static RAM (SRAM) that is @@ -45,9 +56,15 @@ #define PCF2127_REG_RAM_WRT_CMD 0x1C #define PCF2127_REG_RAM_RD_CMD 0x1D +/* Watchdog timer value constants */ +#define PCF2127_WD_VAL_STOP 0 +#define PCF2127_WD_VAL_MIN 2 +#define PCF2127_WD_VAL_MAX 255 +#define PCF2127_WD_VAL_DEFAULT 60 struct pcf2127 { struct rtc_device *rtc; + struct watchdog_device wdd; struct regmap *regmap; }; @@ -220,6 +237,74 @@ static int pcf2127_nvmem_write(void *priv, unsigned int offset, return ret ?: bytes; } +/* watchdog driver */ + +static int pcf2127_wdt_ping(struct watchdog_device *wdd) +{ + struct pcf2127 *pcf2127 = watchdog_get_drvdata(wdd); + + return regmap_write(pcf2127->regmap, PCF2127_REG_WD_VAL, wdd->timeout); +} + +/* + * Restart watchdog timer if feature is active. + * + * Note: Reading CTRL2 register causes watchdog to stop which is unfortunate, + * since register also contain control/status flags for other features. + * Always call this function after reading CTRL2 register. + */ +static int pcf2127_wdt_active_ping(struct watchdog_device *wdd) +{ + int ret = 0; + + if (watchdog_active(wdd)) { + ret = pcf2127_wdt_ping(wdd); + if (ret) + dev_err(wdd->parent, + "%s: watchdog restart failed, ret=%d\n", + __func__, ret); + } + + return ret; +} + +static int pcf2127_wdt_start(struct watchdog_device *wdd) +{ + return pcf2127_wdt_ping(wdd); +} + +static int pcf2127_wdt_stop(struct watchdog_device *wdd) +{ + struct pcf2127 *pcf2127 = watchdog_get_drvdata(wdd); + + return regmap_write(pcf2127->regmap, PCF2127_REG_WD_VAL, + PCF2127_WD_VAL_STOP); +} + +static int pcf2127_wdt_set_timeout(struct watchdog_device *wdd, + unsigned int new_timeout) +{ + dev_dbg(wdd->parent, "new watchdog timeout: %is (old: %is)\n", + new_timeout, wdd->timeout); + + wdd->timeout = new_timeout; + + return pcf2127_wdt_active_ping(wdd); +} + +static const struct watchdog_info pcf2127_wdt_info = { + .identity = "NXP PCF2127/PCF2129 Watchdog", + .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT, +}; + +static const struct watchdog_ops pcf2127_watchdog_ops = { + .owner = THIS_MODULE, + .start = pcf2127_wdt_start, + .stop = pcf2127_wdt_stop, + .ping = pcf2127_wdt_ping, + .set_timeout = pcf2127_wdt_set_timeout, +}; + static int pcf2127_probe(struct device *dev, struct regmap *regmap, const char *name, bool has_nvmem) { @@ -242,6 +327,16 @@ static int pcf2127_probe(struct device *dev, struct regmap *regmap, pcf2127->rtc->ops = &pcf2127_rtc_ops; + pcf2127->wdd.parent = dev; + pcf2127->wdd.info = &pcf2127_wdt_info; + pcf2127->wdd.ops = &pcf2127_watchdog_ops; + pcf2127->wdd.min_timeout = PCF2127_WD_VAL_MIN; + pcf2127->wdd.max_timeout = PCF2127_WD_VAL_MAX; + pcf2127->wdd.timeout = PCF2127_WD_VAL_DEFAULT; + pcf2127->wdd.min_hw_heartbeat_ms = 500; + + watchdog_set_drvdata(&pcf2127->wdd, pcf2127); + if (has_nvmem) { struct nvmem_config nvmem_cfg = { .priv = pcf2127, @@ -253,6 +348,29 @@ static int pcf2127_probe(struct device *dev, struct regmap *regmap, ret = rtc_nvmem_register(pcf2127->rtc, &nvmem_cfg); } + /* + * Watchdog timer enabled and reset pin /RST activated when timed out. + * Select 1Hz clock source for watchdog timer. + * Timer is not started until WD_VAL is loaded with a valid value. + * Note: Countdown timer disabled and not available. + */ + ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_WD_CTL, + PCF2127_BIT_WD_CTL_CD1 | + PCF2127_BIT_WD_CTL_CD0 | + PCF2127_BIT_WD_CTL_TF1 | + PCF2127_BIT_WD_CTL_TF0, + PCF2127_BIT_WD_CTL_CD1 | + PCF2127_BIT_WD_CTL_CD0 | + PCF2127_BIT_WD_CTL_TF1); + if (ret) { + dev_err(dev, "%s: watchdog config (wd_ctl) failed\n", __func__); + return ret; + } + + ret = devm_watchdog_register_device(dev, &pcf2127->wdd); + if (ret) + return ret; + return rtc_register_device(pcf2127->rtc); } From patchwork Thu Aug 22 13:19:36 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bruno Thomsen X-Patchwork-Id: 11109287 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 8429113B1 for ; Thu, 22 Aug 2019 13:19:58 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 59254233A0 for ; Thu, 22 Aug 2019 13:19:58 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="mz61uVpt" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732317AbfHVNT6 (ORCPT ); Thu, 22 Aug 2019 09:19:58 -0400 Received: from mail-wr1-f67.google.com ([209.85.221.67]:38996 "EHLO mail-wr1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730621AbfHVNT5 (ORCPT ); Thu, 22 Aug 2019 09:19:57 -0400 Received: by mail-wr1-f67.google.com with SMTP id t16so5409962wra.6; Thu, 22 Aug 2019 06:19:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=8YzNQFejHcUxRGGGHyCklNkZB6Rv3yiFGAIZAAgoOvo=; b=mz61uVpt8xGUHMFran+MeTmkZJ/zLONa/gyVZgB8L52h4b7TPpGRYbRqnCl00O6maa eyydiWuT3agYR9sF+a+BCQtXGlFEVOhazQV0+QWP0WRQ5Oekt6hCjlNj3mtw/gleVBNC DRAUMmu8XNtqqxHklRBJwcPXWa3pxQ2VVU68EvNJQCHkEOm2MPOWBHM7+kvNATZJU5CW jGAdYR01jtiWbvLUcUR1i8w+aNomi2l0KcvJfMCc0PJ7pTpS0oImsEShBSQB5vEK8wP3 qxDOOrDe5vB8I2PpPyig5TRsvTH+L8nowPrS6dHyZ+c7J1yFqNMh+jdwgrDyZ1g5vw44 GlUw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=8YzNQFejHcUxRGGGHyCklNkZB6Rv3yiFGAIZAAgoOvo=; b=SK795YVYQf1ct5X0OPKhW9zhJfkWcfjqdDNqW4GcnVf5CHS3htKfu61oH7V9zvIYcI qmsHirOtYWdGYamYark4+mrO0zTCer0jyYYvbklFB+VM5LSQbA8fo7HjUCkuIL8eZ7wx 31RuMD4GqczLk18XDf6wvq38YT8rkUq3Vxshj5QhnAlEdw6ZhGvuYGoDG5BGTRVbkhhL xEvlaXLCcV3tVQQf38Vofyo5lgffItjmVBB+ZqmssKc5lWOtKGq7JfJPs3B/+n/yZVMi wv+uF4LNTpR7a2DS9pqK1WrCnll+7cMQ5acnQvdbn7bJEvVOlqFmVyWj4TCrJnV17Ut0 VpGw== X-Gm-Message-State: APjAAAWDoC/Hc3uq5C7YB5bC06uVdhNMHzqSjEzMNGj1liRvYUEXI866 PwrGUcDLa9PnHg5yJa6BDHp4w+M2BNGDAA== X-Google-Smtp-Source: APXvYqxawesr4/ade+dpooRHdXcn2CsBAKF3KJyVQYDgaaGxsU6+RvY7bGpd4JW/n7klWZONGMKUuQ== X-Received: by 2002:adf:fe12:: with SMTP id n18mr47027619wrr.105.1566479994040; Thu, 22 Aug 2019 06:19:54 -0700 (PDT) Received: from localhost.localdomain (3e6b1cc1.rev.stofanet.dk. [62.107.28.193]) by smtp.googlemail.com with ESMTPSA id d19sm34644045wrb.7.2019.08.22.06.19.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Aug 2019 06:19:53 -0700 (PDT) From: Bruno Thomsen To: linux-rtc@vger.kernel.org, linux-watchdog@vger.kernel.org Cc: alexandre.belloni@bootlin.com, a.zummo@towertech.it, wim@linux-watchdog.org, linux@roeck-us.net, u.kleine-koenig@pengutronix.de, bth@kamstrup.com, bruno.thomsen@gmail.com Subject: [PATCH v3 5/5] rtc: pcf2127: add tamper detection support Date: Thu, 22 Aug 2019 15:19:36 +0200 Message-Id: <20190822131936.18772-5-bruno.thomsen@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190822131936.18772-1-bruno.thomsen@gmail.com> References: <20190822131936.18772-1-bruno.thomsen@gmail.com> MIME-Version: 1.0 Sender: linux-watchdog-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-watchdog@vger.kernel.org Add support for integrated tamper detection function in both PCF2127 and PCF2129 chips. This patch implements the feature by adding an additional timestamp0 file to sysfs device path. This file contains seconds since epoch, if an event occurred, or is empty, if none occurred. Interface should match ISL1208 and RV3028 RTC drivers. Signed-off-by: Bruno Thomsen --- v3: no change. v2: call pcf2127_wdt_active_ping after CTRL2 register read. add dev_dbg() trace in timestamp0_show(). minor regmap dev_err() text update in pcf2127_probe(). drivers/rtc/rtc-pcf2127.c | 160 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 160 insertions(+) diff --git a/drivers/rtc/rtc-pcf2127.c b/drivers/rtc/rtc-pcf2127.c index 8d6eda455d81..3ec87d320766 100644 --- a/drivers/rtc/rtc-pcf2127.c +++ b/drivers/rtc/rtc-pcf2127.c @@ -25,11 +25,18 @@ /* Control register 1 */ #define PCF2127_REG_CTRL1 0x00 +#define PCF2127_BIT_CTRL1_TSF1 BIT(4) /* Control register 2 */ #define PCF2127_REG_CTRL2 0x01 +#define PCF2127_BIT_CTRL2_TSIE BIT(2) +#define PCF2127_BIT_CTRL2_TSF2 BIT(5) /* Control register 3 */ #define PCF2127_REG_CTRL3 0x02 +#define PCF2127_BIT_CTRL3_BLIE BIT(0) +#define PCF2127_BIT_CTRL3_BIE BIT(1) #define PCF2127_BIT_CTRL3_BLF BIT(2) +#define PCF2127_BIT_CTRL3_BF BIT(3) +#define PCF2127_BIT_CTRL3_BTSE BIT(4) /* Time and date registers */ #define PCF2127_REG_SC 0x03 #define PCF2127_BIT_SC_OSF BIT(7) @@ -46,6 +53,16 @@ #define PCF2127_BIT_WD_CTL_CD0 BIT(6) #define PCF2127_BIT_WD_CTL_CD1 BIT(7) #define PCF2127_REG_WD_VAL 0x11 +/* Tamper timestamp registers */ +#define PCF2127_REG_TS_CTRL 0x12 +#define PCF2127_BIT_TS_CTRL_TSOFF BIT(6) +#define PCF2127_BIT_TS_CTRL_TSM BIT(7) +#define PCF2127_REG_TS_SC 0x13 +#define PCF2127_REG_TS_MN 0x14 +#define PCF2127_REG_TS_HR 0x15 +#define PCF2127_REG_TS_DM 0x16 +#define PCF2127_REG_TS_MO 0x17 +#define PCF2127_REG_TS_YR 0x18 /* * RAM registers * PCF2127 has 512 bytes general-purpose static RAM (SRAM) that is @@ -305,6 +322,97 @@ static const struct watchdog_ops pcf2127_watchdog_ops = { .set_timeout = pcf2127_wdt_set_timeout, }; +/* sysfs interface */ + +static ssize_t timestamp0_store(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t count) +{ + struct pcf2127 *pcf2127 = dev_get_drvdata(dev->parent); + int ret; + + ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL1, + PCF2127_BIT_CTRL1_TSF1, 0); + if (ret) { + dev_err(dev, "%s: update ctrl1 ret=%d\n", __func__, ret); + return ret; + } + + ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL2, + PCF2127_BIT_CTRL2_TSF2, 0); + if (ret) { + dev_err(dev, "%s: update ctrl2 ret=%d\n", __func__, ret); + return ret; + } + + ret = pcf2127_wdt_active_ping(&pcf2127->wdd); + if (ret) + return ret; + + return count; +}; + +static ssize_t timestamp0_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct pcf2127 *pcf2127 = dev_get_drvdata(dev->parent); + struct rtc_time tm; + int ret; + unsigned char data[25]; + + ret = regmap_bulk_read(pcf2127->regmap, PCF2127_REG_CTRL1, data, + sizeof(data)); + if (ret) { + dev_err(dev, "%s: read error ret=%d\n", __func__, ret); + return ret; + } + + dev_dbg(dev, + "%s: raw data is cr1=%02x, cr2=%02x, cr3=%02x, ts_sc=%02x, " + "ts_mn=%02x, ts_hr=%02x, ts_dm=%02x, ts_mo=%02x, ts_yr=%02x\n", + __func__, data[PCF2127_REG_CTRL1], data[PCF2127_REG_CTRL2], + data[PCF2127_REG_CTRL3], data[PCF2127_REG_TS_SC], + data[PCF2127_REG_TS_MN], data[PCF2127_REG_TS_HR], + data[PCF2127_REG_TS_DM], data[PCF2127_REG_TS_MO], + data[PCF2127_REG_TS_YR]); + + ret = pcf2127_wdt_active_ping(&pcf2127->wdd); + if (ret) + return ret; + + if (!(data[PCF2127_REG_CTRL1] & PCF2127_BIT_CTRL1_TSF1) && + !(data[PCF2127_REG_CTRL2] & PCF2127_BIT_CTRL2_TSF2)) + return 0; + + tm.tm_sec = bcd2bin(data[PCF2127_REG_TS_SC] & 0x7F); + tm.tm_min = bcd2bin(data[PCF2127_REG_TS_MN] & 0x7F); + tm.tm_hour = bcd2bin(data[PCF2127_REG_TS_HR] & 0x3F); + tm.tm_mday = bcd2bin(data[PCF2127_REG_TS_DM] & 0x3F); + /* TS_MO register (month) value range: 1-12 */ + tm.tm_mon = bcd2bin(data[PCF2127_REG_TS_MO] & 0x1F) - 1; + tm.tm_year = bcd2bin(data[PCF2127_REG_TS_YR]); + if (tm.tm_year < 70) + tm.tm_year += 100; /* assume we are in 1970...2069 */ + + ret = rtc_valid_tm(&tm); + if (ret) + return ret; + + return sprintf(buf, "%llu\n", + (unsigned long long)rtc_tm_to_time64(&tm)); +}; + +static DEVICE_ATTR_RW(timestamp0); + +static struct attribute *pcf2127_attrs[] = { + &dev_attr_timestamp0.attr, + NULL +}; + +static const struct attribute_group pcf2127_attr_group = { + .attrs = pcf2127_attrs, +}; + static int pcf2127_probe(struct device *dev, struct regmap *regmap, const char *name, bool has_nvmem) { @@ -371,6 +479,58 @@ static int pcf2127_probe(struct device *dev, struct regmap *regmap, if (ret) return ret; + /* + * Disable battery low/switch-over timestamp and interrupts. + * Clear battery interrupt flags which can block new trigger events. + * Note: This is the default chip behaviour but added to ensure + * correct tamper timestamp and interrupt function. + */ + ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL3, + PCF2127_BIT_CTRL3_BTSE | + PCF2127_BIT_CTRL3_BF | + PCF2127_BIT_CTRL3_BIE | + PCF2127_BIT_CTRL3_BLIE, 0); + if (ret) { + dev_err(dev, "%s: interrupt config (ctrl3) failed\n", + __func__); + return ret; + } + + /* + * Enable timestamp function and store timestamp of first trigger + * event until TSF1 and TFS2 interrupt flags are cleared. + */ + ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_TS_CTRL, + PCF2127_BIT_TS_CTRL_TSOFF | + PCF2127_BIT_TS_CTRL_TSM, + PCF2127_BIT_TS_CTRL_TSM); + if (ret) { + dev_err(dev, "%s: tamper detection config (ts_ctrl) failed\n", + __func__); + return ret; + } + + /* + * Enable interrupt generation when TSF1 or TSF2 timestamp flags + * are set. Interrupt signal is an open-drain output and can be + * left floating if unused. + */ + ret = regmap_update_bits(pcf2127->regmap, PCF2127_REG_CTRL2, + PCF2127_BIT_CTRL2_TSIE, + PCF2127_BIT_CTRL2_TSIE); + if (ret) { + dev_err(dev, "%s: tamper detection config (ctrl2) failed\n", + __func__); + return ret; + } + + ret = rtc_add_group(pcf2127->rtc, &pcf2127_attr_group); + if (ret) { + dev_err(dev, "%s: tamper sysfs registering failed\n", + __func__); + return ret; + } + return rtc_register_device(pcf2127->rtc); }