From patchwork Thu Apr 2 19:17:40 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lee Jones X-Patchwork-Id: 228223 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.8 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 87F54C2BA18 for ; Thu, 2 Apr 2020 19:17:13 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 60DAF206F8 for ; Thu, 2 Apr 2020 19:17:13 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="jFjnihIq" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388902AbgDBTRN (ORCPT ); Thu, 2 Apr 2020 15:17:13 -0400 Received: from mail-wm1-f67.google.com ([209.85.128.67]:37761 "EHLO mail-wm1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388921AbgDBTRM (ORCPT ); Thu, 2 Apr 2020 15:17:12 -0400 Received: by mail-wm1-f67.google.com with SMTP id j19so4952283wmi.2 for ; Thu, 02 Apr 2020 12:17:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=8Z20wcAGJt+rsOO0qFIXrokKyfVoYrM8wAvpmSP5OEA=; b=jFjnihIqkOTp7NEEDsK3bcVJKV31J4IIwIFLiYOMbK8xdvjsHFXdpDyd57EMzzPQVr C9iY2LfVjRf7/Af8ITbIZUrQoJlLTrLQ28c1hfLE+gZuS3zlZMuaEmYIRY1lL38cxeWv WqTw/YZCASt5Qr9xZeQq415QokcM06f+2BM3MEtJxUXcpl8gIqQHmTHeFlZNFR+57Qkq zJFtkVsrceA4xqfDSUgQCdEuC2oblVlehb0Aj+I4tYMjtxp60Sm55VGWzdNWZ9ZLAlqP PPnAWuNN1zRX0hgFICxSY+jnIrxL/X9k6+enz6gUYcnL8pwOGg8arZ/KQa4phZs+Fb3Y vA6Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=8Z20wcAGJt+rsOO0qFIXrokKyfVoYrM8wAvpmSP5OEA=; b=QJCGRoAF4MHPcpVBykji39Rm3OcSoxJgt0HvWXSfOheIM6xgIVT5zmYtlnNDsyOsCO R/SBLq8edLCL+WMWzBScPrOrEurFIRajQmrsn72QUiKmrIHKJvFH19khWSFojdTazC5o foQlfv4zBJM5cwDcm5Y9n2ADynh2A3gwtAGBDkrMhADV0TUsa47J0BcYbSoue1mz3clB BT52QocFJwbsgWdOwKH59zqkVi+GBOsFC1LgJsErZttzKo41y3VAflLa+r/kUugPbPLv vnw5pSTIMuCIW4AGT0aCQBZuLbvhVH/qgr9TywdfrWEX645e/QyqScVYtAXxgY+Rh4Yi ka4A== X-Gm-Message-State: AGi0PuZFT7eV+bUtNNYTVUxpu4ROJn6HdSUdrQjBjrIADA/y3T+AR3b+ 9oMrgr8w/cM0/pNiYFFIzDtgXSW4bBxsig== X-Google-Smtp-Source: APiQypKFWqn0/RGCnvFk2nkGihtA1fZ4oVsezYMjHZUvkU8+wLpyi9tKjd9jAECNE1oCAxHcUucI4g== X-Received: by 2002:a1c:6a14:: with SMTP id f20mr4597118wmc.125.1585855030890; Thu, 02 Apr 2020 12:17:10 -0700 (PDT) Received: from localhost.localdomain ([95.149.164.95]) by smtp.gmail.com with ESMTPSA id y1sm879050wmd.14.2020.04.02.12.17.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Apr 2020 12:17:10 -0700 (PDT) From: Lee Jones To: stable@vger.kernel.org Subject: [PATCH 4.9 17/24] rtc: pm8xxx: Fix issue in RTC write path Date: Thu, 2 Apr 2020 20:17:40 +0100 Message-Id: <20200402191747.789097-17-lee.jones@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200402191747.789097-1-lee.jones@linaro.org> References: <20200402191747.789097-1-lee.jones@linaro.org> MIME-Version: 1.0 Sender: stable-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org From: Mohit Aggarwal [ Upstream commit 83220bf38b77a830f8e62ab1a0d0408304f9b966 ] In order to set time in rtc, need to disable rtc hw before writing into rtc registers. Also fixes disabling of alarm while setting rtc time. Signed-off-by: Mohit Aggarwal Signed-off-by: Alexandre Belloni Signed-off-by: Lee Jones --- drivers/rtc/rtc-pm8xxx.c | 49 +++++++++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 11 deletions(-) diff --git a/drivers/rtc/rtc-pm8xxx.c b/drivers/rtc/rtc-pm8xxx.c index a1b4b0ed1f196..3b619b7b2c530 100644 --- a/drivers/rtc/rtc-pm8xxx.c +++ b/drivers/rtc/rtc-pm8xxx.c @@ -74,16 +74,18 @@ struct pm8xxx_rtc { /* * Steps to write the RTC registers. * 1. Disable alarm if enabled. - * 2. Write 0x00 to LSB. - * 3. Write Byte[1], Byte[2], Byte[3] then Byte[0]. - * 4. Enable alarm if disabled in step 1. + * 2. Disable rtc if enabled. + * 3. Write 0x00 to LSB. + * 4. Write Byte[1], Byte[2], Byte[3] then Byte[0]. + * 5. Enable rtc if disabled in step 2. + * 6. Enable alarm if disabled in step 1. */ static int pm8xxx_rtc_set_time(struct device *dev, struct rtc_time *tm) { int rc, i; unsigned long secs, irq_flags; - u8 value[NUM_8_BIT_RTC_REGS], alarm_enabled = 0; - unsigned int ctrl_reg; + u8 value[NUM_8_BIT_RTC_REGS], alarm_enabled = 0, rtc_disabled = 0; + unsigned int ctrl_reg, rtc_ctrl_reg; struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev); const struct pm8xxx_rtc_regs *regs = rtc_dd->regs; @@ -92,23 +94,38 @@ static int pm8xxx_rtc_set_time(struct device *dev, struct rtc_time *tm) rtc_tm_to_time(tm, &secs); + dev_dbg(dev, "Seconds value to be written to RTC = %lu\n", secs); + for (i = 0; i < NUM_8_BIT_RTC_REGS; i++) { value[i] = secs & 0xFF; secs >>= 8; } - dev_dbg(dev, "Seconds value to be written to RTC = %lu\n", secs); - spin_lock_irqsave(&rtc_dd->ctrl_reg_lock, irq_flags); - rc = regmap_read(rtc_dd->regmap, regs->ctrl, &ctrl_reg); + rc = regmap_read(rtc_dd->regmap, regs->alarm_ctrl, &ctrl_reg); if (rc) goto rtc_rw_fail; if (ctrl_reg & regs->alarm_en) { alarm_enabled = 1; ctrl_reg &= ~regs->alarm_en; - rc = regmap_write(rtc_dd->regmap, regs->ctrl, ctrl_reg); + rc = regmap_write(rtc_dd->regmap, regs->alarm_ctrl, ctrl_reg); + if (rc) { + dev_err(dev, "Write to RTC Alarm control register failed\n"); + goto rtc_rw_fail; + } + } + + /* Disable RTC H/w before writing on RTC register */ + rc = regmap_read(rtc_dd->regmap, regs->ctrl, &rtc_ctrl_reg); + if (rc) + goto rtc_rw_fail; + + if (rtc_ctrl_reg & PM8xxx_RTC_ENABLE) { + rtc_disabled = 1; + rtc_ctrl_reg &= ~PM8xxx_RTC_ENABLE; + rc = regmap_write(rtc_dd->regmap, regs->ctrl, rtc_ctrl_reg); if (rc) { dev_err(dev, "Write to RTC control register failed\n"); goto rtc_rw_fail; @@ -137,11 +154,21 @@ static int pm8xxx_rtc_set_time(struct device *dev, struct rtc_time *tm) goto rtc_rw_fail; } + /* Enable RTC H/w after writing on RTC register */ + if (rtc_disabled) { + rtc_ctrl_reg |= PM8xxx_RTC_ENABLE; + rc = regmap_write(rtc_dd->regmap, regs->ctrl, rtc_ctrl_reg); + if (rc) { + dev_err(dev, "Write to RTC control register failed\n"); + goto rtc_rw_fail; + } + } + if (alarm_enabled) { ctrl_reg |= regs->alarm_en; - rc = regmap_write(rtc_dd->regmap, regs->ctrl, ctrl_reg); + rc = regmap_write(rtc_dd->regmap, regs->alarm_ctrl, ctrl_reg); if (rc) { - dev_err(dev, "Write to RTC control register failed\n"); + dev_err(dev, "Write to RTC Alarm control register failed\n"); goto rtc_rw_fail; } }