Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp3123636pxb; Mon, 18 Oct 2021 08:39:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxX0v3qO9uZta4/CE33SHerWzGHwEglhkavUtrzjAz7T/WYNs19Or9pMAE2nN5+Lr2qCNtC X-Received: by 2002:a63:36c7:: with SMTP id d190mr15925871pga.86.1634571554370; Mon, 18 Oct 2021 08:39:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634571554; cv=none; d=google.com; s=arc-20160816; b=y7P6P9u3kUXC6RnHcI5eoP1PmkuimBlk+Dp4waSSqYToJ3MSuun1+UdQspK5QrKc+X vLB1vErHgCautw7aBF/NpJEDVW7anZq2pbKGmt5UUsRpb558PSclJI212WFaZ/eJVsSZ u3BpXjlXchMq7tlp8h2ybrVW6ToaX2fVi6p7FSKOvN9lcrv6bJaDN1WKhSHr/ZKIJ4UK f5yLdp2cI16DWIB4avhYJcy6CNYfQEwHGTqVe98wKJ/ecatQXkA7DzdjzxeZE9HBUHyE iH6ItxVXCmLz4VTz0oe77mpNM3CGRBkkfWy5bvtYKbipCkbOX6QEp8buBY04/mijPoRq v2+A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=Fj2exphCL7bqOjP9c4uqdiuIrCa3k5oUrg+x4LkxCjk=; b=FyGqfTlKVfZoRgNZJcIZ+V0IseNZD802hKyBCay7z9M7LQF0dNo42zc7rAmraWgPTS 4D1wdK35fPXqnjoqKpZaU6aluxkRQ3QZ/Tze7PF7IilqEv41P2TLGjnd3FuamglveTm+ 5hUFJ8YldA3PFC1Qmu/PjcwzsrOxbCjAhv2KfQkappLvtfDS55tn4/Pjft19HE7/7TuQ A+7QKxS8J/GmbQtP0jtDxpJtWqj+P6K2Ze3PIePE/vC819oBS9247UV+DFLyTqAQaFb5 tIZ4SMenJjqPHs6MwZ4CQ3/BdUaeb1qAlKyla9VZtDguAmFbobLnlpcE7o59JBsP7N6u uewQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id p18si14296301pgm.554.2021.10.18.08.39.01; Mon, 18 Oct 2021 08:39:14 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233284AbhJRPjt (ORCPT + 99 others); Mon, 18 Oct 2021 11:39:49 -0400 Received: from mslow1.mail.gandi.net ([217.70.178.240]:56831 "EHLO mslow1.mail.gandi.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233233AbhJRPjj (ORCPT ); Mon, 18 Oct 2021 11:39:39 -0400 Received: from relay6-d.mail.gandi.net (unknown [217.70.183.198]) by mslow1.mail.gandi.net (Postfix) with ESMTP id 97579C7127 for ; Mon, 18 Oct 2021 15:37:22 +0000 (UTC) Received: (Authenticated sender: alexandre.belloni@bootlin.com) by relay6-d.mail.gandi.net (Postfix) with ESMTPSA id D6CE7C0009; Mon, 18 Oct 2021 15:36:57 +0000 (UTC) From: Alexandre Belloni To: Alessandro Zummo , Alexandre Belloni Cc: linux-rtc@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 5/5] rtc: pcf8523: add BSM support Date: Mon, 18 Oct 2021 17:36:50 +0200 Message-Id: <20211018153651.82069-5-alexandre.belloni@bootlin.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20211018153651.82069-1-alexandre.belloni@bootlin.com> References: <20211018153651.82069-1-alexandre.belloni@bootlin.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Backup Switch Mode allows to select the strategy to use to switch from the main power supply to the backup power supply. As before, the driver will switch from standby mode to level mode but now only when it has never been set. Signed-off-by: Alexandre Belloni --- drivers/rtc/rtc-pcf8523.c | 103 +++++++++++++++++++++++++++++++++++--- 1 file changed, 97 insertions(+), 6 deletions(-) diff --git a/drivers/rtc/rtc-pcf8523.c b/drivers/rtc/rtc-pcf8523.c index 181c3425d6c7..c93acade7205 100644 --- a/drivers/rtc/rtc-pcf8523.c +++ b/drivers/rtc/rtc-pcf8523.c @@ -4,6 +4,7 @@ */ #include +#include #include #include #include @@ -20,10 +21,8 @@ #define PCF8523_CONTROL2_AF BIT(3) #define PCF8523_REG_CONTROL3 0x02 -#define PCF8523_CONTROL3_PM_BLD BIT(7) /* battery low detection disabled */ -#define PCF8523_CONTROL3_PM_VDD BIT(6) /* switch-over disabled */ -#define PCF8523_CONTROL3_PM_DSM BIT(5) /* direct switching mode */ -#define PCF8523_CONTROL3_PM_MASK 0xe0 +#define PCF8523_CONTROL3_PM GENMASK(7,5) +#define PCF8523_PM_STANDBY 0x7 #define PCF8523_CONTROL3_BLF BIT(2) /* battery low bit, read-only */ #define PCF8523_CONTROL3_BSF BIT(3) @@ -237,6 +236,83 @@ static int pcf8523_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *tm) return 0; } +static int pcf8523_param_get(struct device *dev, struct rtc_param *param) +{ + struct pcf8523 *pcf8523 = dev_get_drvdata(dev); + int ret; + + switch(param->param) { + u32 value; + + case RTC_PARAM_BACKUP_SWITCH_MODE: + ret = regmap_read(pcf8523->regmap, PCF8523_REG_CONTROL3, &value); + if (ret < 0) + return ret; + + value = FIELD_GET(PCF8523_CONTROL3_PM, value); + + switch(value) { + case 0x0: + case 0x4: + param->uvalue = RTC_BSM_LEVEL; + break; + case 0x1: + case 0x5: + param->uvalue = RTC_BSM_DIRECT; + break; + case PCF8523_PM_STANDBY: + param->uvalue = RTC_BSM_STANDBY; + break; + default: + param->uvalue = RTC_BSM_DISABLED; + } + + break; + + default: + return -EINVAL; + } + + return 0; +} + +static int pcf8523_param_set(struct device *dev, struct rtc_param *param) +{ + struct pcf8523 *pcf8523 = dev_get_drvdata(dev); + + switch(param->param) { + u8 mode; + case RTC_PARAM_BACKUP_SWITCH_MODE: + switch (param->uvalue) { + case RTC_BSM_DISABLED: + mode = 0x2; + break; + case RTC_BSM_DIRECT: + mode = 0x1; + break; + case RTC_BSM_LEVEL: + mode = 0x0; + break; + case RTC_BSM_STANDBY: + mode = PCF8523_PM_STANDBY; + break; + default: + return -EINVAL; + } + + return regmap_update_bits(pcf8523->regmap, PCF8523_REG_CONTROL3, + PCF8523_CONTROL3_PM, + FIELD_PREP(PCF8523_CONTROL3_PM, mode)); + + break; + + default: + return -EINVAL; + } + + return 0; +} + static int pcf8523_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg) { @@ -312,6 +388,8 @@ static const struct rtc_class_ops pcf8523_rtc_ops = { .ioctl = pcf8523_rtc_ioctl, .read_offset = pcf8523_rtc_read_offset, .set_offset = pcf8523_rtc_set_offset, + .param_get = pcf8523_param_get, + .param_set = pcf8523_param_set, }; static const struct regmap_config regmap_config = { @@ -326,6 +404,7 @@ static int pcf8523_probe(struct i2c_client *client, struct pcf8523 *pcf8523; struct rtc_device *rtc; bool wakeup_source = false; + u32 value; int err; if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) @@ -351,11 +430,23 @@ static int pcf8523_probe(struct i2c_client *client, dev_warn(&client->dev, "failed to set xtal load capacitance: %d", err); - err = regmap_update_bits(pcf8523->regmap, PCF8523_REG_CONTROL3, - PCF8523_CONTROL3_PM_MASK, 0); + err = regmap_read(pcf8523->regmap, PCF8523_REG_SECONDS, &value); if (err < 0) return err; + if (value & PCF8523_SECONDS_OS) { + err = regmap_read(pcf8523->regmap, PCF8523_REG_CONTROL3, &value); + if (err < 0) + return err; + + if (FIELD_GET(PCF8523_CONTROL3_PM, value) == PCF8523_PM_STANDBY) { + err = regmap_write(pcf8523->regmap, PCF8523_REG_CONTROL3, + value & ~PCF8523_CONTROL3_PM); + if (err < 0) + return err; + } + } + rtc->ops = &pcf8523_rtc_ops; rtc->range_min = RTC_TIMESTAMP_BEGIN_2000; rtc->range_max = RTC_TIMESTAMP_END_2099; -- 2.31.1