From patchwork Fri Aug 25 20:06:15 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Heiner Kallweit X-Patchwork-Id: 806013 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=linux-rtc-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="W0k9aezB"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3xfBy54PBBz9t2y for ; Sat, 26 Aug 2017 06:06:37 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932711AbdHYUGh (ORCPT ); Fri, 25 Aug 2017 16:06:37 -0400 Received: from mail-wm0-f65.google.com ([74.125.82.65]:33681 "EHLO mail-wm0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932771AbdHYUGg (ORCPT ); Fri, 25 Aug 2017 16:06:36 -0400 Received: by mail-wm0-f65.google.com with SMTP id e67so844263wmd.0 for ; Fri, 25 Aug 2017 13:06:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:subject:to:cc:references:message-id:date:user-agent :mime-version:in-reply-to:content-transfer-encoding; bh=NBCsY/bzhooCjg3MG7mdAxpbSyvhKn/q4RGx9lc/tpA=; b=W0k9aezBl8eM8lFuYXWCRgsl0xF2XVcuPoRu2lecPjBvRRXgjmLeVkN19mqoC3hWa0 +6yjUm+titsjkbvLQ6XP0vb0rmOeGzfMCb0FDq8hHaj0YFROcSMByF//IxE3XOsPU4dh UrziK0ncN8p9fDGSqHntYL+yk8aLwMt5H0uXhWSUVeyHpU2uaxF5ibQbaVLExkMzxQIO OYwRPkFqP9EzCbGK7RfLhOwNJAD195UNcgrzoEH33kGfN9svxeQmQn7kvvB8efN4AoU0 9l5POHBIKm8Y0s3IFjN7aEE/4mpqeXIeymCQXPbKdsUrqZxinwoOMMBa956mfjkSDmKG ZxGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:subject:to:cc:references:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding; bh=NBCsY/bzhooCjg3MG7mdAxpbSyvhKn/q4RGx9lc/tpA=; b=lQP8k8jSqN5v/exrSo60xHnxoU4NMh0zsXiGA+bBcqUb4hQvBMLiQq8rpMgjq9IRHA SovvOdHRd3VwRZQyDLuK3MHJYIq/vgL/pJtEF/nm28PyoVi2KpnC7H+wYFSHeigjWSsN jHRUEvhPZ0FpQwOlzZf4PpflXKMKWTvzzec3Xj4guXAp9bEQZvc+CNbnuR5q1mR0M99c FmUmR2QpttzKD+h1z6b3i2IxJ4iyDdPoWfoX7FcOAvc6qlKSNobmk8DdZR+yjAOipIA7 fIrmFWrIO2X3d8hlGTWCDpk+tAP8HqOld3mkWYZt39yqcOkN8bJJ8p/aCT9PayDwyFmO l//Q== X-Gm-Message-State: AHYfb5jUdbc5geIk5GVkHxp9S8cxvlNC6nqiHEFgZkkUcv8WiajX65bP v7NQrXTxtt1/3T96 X-Received: by 10.28.12.208 with SMTP id 199mr339258wmm.48.1503691595456; Fri, 25 Aug 2017 13:06:35 -0700 (PDT) Received: from ?IPv6:2003:ea:8bc7:5e00:6026:1205:9e47:4fa8? (p200300EA8BC75E00602612059E474FA8.dip0.t-ipconnect.de. [2003:ea:8bc7:5e00:6026:1205:9e47:4fa8]) by smtp.googlemail.com with ESMTPSA id y39sm10794575wry.32.2017.08.25.13.06.34 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 25 Aug 2017 13:06:35 -0700 (PDT) From: Heiner Kallweit Subject: [PATCH 3/5] rtc: ds1307: factor out fixing the weekday To: Alexandre Belloni Cc: linux-rtc@vger.kernel.org References: Message-ID: Date: Fri, 25 Aug 2017 22:06:15 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.3.0 MIME-Version: 1.0 In-Reply-To: Sender: linux-rtc-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-rtc@vger.kernel.org Factor out checking and fixing the weekday. In addition fix two issues with the old implementation: - For variable timestamp use correct type time64_t instead of unsigned long which may be just 32bit long. - Updating the weekday register only may be racy, therefore write all timekeeping registers. Signed-off-by: Heiner Kallweit --- drivers/rtc/rtc-ds1307.c | 42 +++++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/drivers/rtc/rtc-ds1307.c b/drivers/rtc/rtc-ds1307.c index ea88e4b3..69f514b6 100644 --- a/drivers/rtc/rtc-ds1307.c +++ b/drivers/rtc/rtc-ds1307.c @@ -1368,6 +1368,29 @@ static void ds1307_trickle_init(struct ds1307 *ds1307) } } +static void ds1307_fix_weekday(struct ds1307 *ds1307) +{ + struct rtc_time tm; + time64_t timestamp; + int wday; + + if (ds1307_get_time(ds1307->dev, &tm)) + return; + + wday = tm.tm_wday; + timestamp = rtc_tm_to_time64(&tm); + rtc_time64_to_tm(timestamp, &tm); + + /* + * Check if reset wday is different from the computed wday + * If different then set the wday which we computed using + * timestamp + * Set not only wday but complete date to avoid potential races. + */ + if (wday != tm.tm_wday) + ds1307_set_time(ds1307->dev, &tm); +} + static const struct regmap_config regmap_config = { .reg_bits = 8, .val_bits = 8, @@ -1378,13 +1401,11 @@ static int ds1307_probe(struct i2c_client *client, { struct ds1307 *ds1307; int err = -ENODEV; - int tmp, wday; + int tmp; const struct chip_desc *chip; bool want_irq; bool ds1307_can_wakeup_device = false; unsigned char *buf; - struct rtc_time tm; - unsigned long timestamp; ds1307 = devm_kzalloc(&client->dev, sizeof(struct ds1307), GFP_KERNEL); if (!ds1307) @@ -1642,20 +1663,7 @@ static int ds1307_probe(struct i2c_client *client, * Some IPs have weekday reset value = 0x1 which might not correct * hence compute the wday using the current date/month/year values */ - ds1307_get_time(ds1307->dev, &tm); - wday = tm.tm_wday; - timestamp = rtc_tm_to_time64(&tm); - rtc_time64_to_tm(timestamp, &tm); - - /* - * Check if reset wday is different from the computed wday - * If different then set the wday which we computed using - * timestamp - */ - if (wday != tm.tm_wday) - regmap_update_bits(ds1307->regmap, MCP794XX_REG_WEEKDAY, - MCP794XX_REG_WEEKDAY_WDAY_MASK, - tm.tm_wday + 1); + ds1307_fix_weekday(ds1307); if (want_irq || ds1307_can_wakeup_device) { device_set_wakeup_capable(ds1307->dev, true);