From nobody Wed Sep 17 22:40:52 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id CEDBCC4332F for ; Thu, 15 Dec 2022 15:18:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230002AbiLOPS0 (ORCPT ); Thu, 15 Dec 2022 10:18:26 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46084 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229866AbiLOPSN (ORCPT ); Thu, 15 Dec 2022 10:18:13 -0500 Received: from mail.hugovil.com (mail.hugovil.com [162.243.120.170]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A0CEABDD; Thu, 15 Dec 2022 07:18:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=hugovil.com ; s=x; h=Subject:Content-Transfer-Encoding:MIME-Version:References: In-Reply-To:Message-Id:Date:Cc:To:From:Sender:Reply-To:Content-Type: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=AKT7vMuFa4zuixGZfWOqdH1QYl+ynLT/9uMz3IdzAig=; b=E3KhdBp2dJQM6xxnqzweZErHl8 ueeMXliyhTb61Zp7AZNtIzLIHAzeu2okuHmhHcJ8QMhAbvvA59DvdTZBLJabHJeJODyTQy9Ba4VhU ZornaZnXS+FnniZUkQnOP+d1ijQFvFTe+X1m9k2NmjiIn6AdrR2bI8ruMp+b7SUVcnQ4=; Received: from modemcable168.174-80-70.mc.videotron.ca ([70.80.174.168]:48102 helo=pettiford.lan) by mail.hugovil.com with esmtpa (Exim 4.92) (envelope-from ) id 1p5pmO-0000EC-FM; Thu, 15 Dec 2022 10:04:13 -0500 From: Hugo Villeneuve To: a.zummo@towertech.it, alexandre.belloni@bootlin.com, robh+dt@kernel.org, krzysztof.kozlowski+dt@linaro.org Cc: linux-rtc@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, hugo@hugovil.com, Hugo Villeneuve Date: Thu, 15 Dec 2022 10:02:13 -0500 Message-Id: <20221215150214.1109074-13-hugo@hugovil.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20221215150214.1109074-1-hugo@hugovil.com> References: <20221215150214.1109074-1-hugo@hugovil.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-SA-Exim-Connect-IP: 70.80.174.168 X-SA-Exim-Mail-From: hugo@hugovil.com Subject: [PATCH v3 12/14] rtc: pcf2127: support generic watchdog timing configuration X-SA-Exim-Version: 4.2.1 (built Wed, 08 May 2019 21:11:16 +0000) X-SA-Exim-Scanned: Yes (on mail.hugovil.com) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Hugo Villeneuve Introduce in the configuration structure two new values to hold the watchdog clock source and the min_hw_heartbeat_ms value. The minimum and maximum timeout values are automatically computed from the watchdog clock source value for each variant. The PCF2131 has no 1Hz watchdog clock source, as is the case for PCF2127/29. The next best choice is using a 1/4Hz clock, giving a watchdog timeout range between 4 and 1016s. By using the same register configuration as for the PCF2127/29, the 1/4Hz clock source is selected. Note: the PCF2127 datasheet gives a min/max range between 1 and 255s, but it should be between 2 and 254s, because the watchdog is triggered when the timer value reaches 1, not 0. Signed-off-by: Hugo Villeneuve --- drivers/rtc/rtc-pcf2127.c | 56 +++++++++++++++++++++++++++++++++------ 1 file changed, 48 insertions(+), 8 deletions(-) diff --git a/drivers/rtc/rtc-pcf2127.c b/drivers/rtc/rtc-pcf2127.c index 11fbdab6bf01..3fd2fee4978b 100644 --- a/drivers/rtc/rtc-pcf2127.c +++ b/drivers/rtc/rtc-pcf2127.c @@ -157,9 +157,29 @@ =20 /* 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 +#define PCF2127_WD_VAL_DEFAULT 60 /* In seconds. */ +/* PCF2127/29 watchdog timer value constants */ +#define PCF2127_WD_CLOCK_HZ_X1000 1000 /* 1Hz */ +#define PCF2127_WD_MIN_HW_HEARTBEAT_MS 500 +/* PCF2131 watchdog timer value constants */ +#define PCF2131_WD_CLOCK_HZ_X1000 250 /* 1/4Hz */ +#define PCF2131_WD_MIN_HW_HEARTBEAT_MS 4000 +/* + * Compute watchdog period, t, in seconds, from the WATCHDG_TIM_VAL regist= er + * value, n, and the clock frequency, f, in Hz. + * + * The PCF2127/29 datasheet gives t as: + * t =3D n / f + * The PCF2131 datasheet gives t as: + * t =3D (n - 1) / f + * For both variants, the watchdog is triggered when the WATCHDG_TIM_VAL r= eaches + * the value 1, and not zero. Consequently, the equation from the PCF2131 + * datasheet seems to be the correct one for both variants. + */ +#define WD_PERIOD_S(_n_, _f1000_) ((1000 * ((_n_) - 1)) / (_f1000_)) + +/* Compute value of WATCHDG_TIM_VAL to obtain period t, in seconds. */ +#define WD_COUNTER(_t_, _f1000_) ((((_t_) * (_f1000_)) / 1000) + 1) =20 /* Mask for currently enabled interrupts */ #define PCF2127_CTRL1_IRQ_MASK (PCF2127_BIT_CTRL1_TSF1) @@ -202,6 +222,11 @@ struct pcf21xx_config { u8 reg_wd_val; /* Watchdog value register. */ u8 reg_clkout; /* Clkout register. */ u8 reg_reset; /* Reset register if available. */ + + /* Watchdog configuration. */ + int wdd_clock_hz_x1000; /* Value in Hz multiplicated by 1000 */ + int wdd_min_hw_heartbeat_ms; + unsigned int ts_count; struct pcf21xx_ts_config ts[4]; struct attribute_group attribute_group; @@ -496,10 +521,19 @@ static int pcf2127_watchdog_init(struct device *dev, = struct pcf2127 *pcf2127) pcf2127->wdd.parent =3D dev; pcf2127->wdd.info =3D &pcf2127_wdt_info; pcf2127->wdd.ops =3D &pcf2127_watchdog_ops; - pcf2127->wdd.min_timeout =3D PCF2127_WD_VAL_MIN; - pcf2127->wdd.max_timeout =3D PCF2127_WD_VAL_MAX; - pcf2127->wdd.timeout =3D PCF2127_WD_VAL_DEFAULT; - pcf2127->wdd.min_hw_heartbeat_ms =3D 500; + + pcf2127->wdd.min_timeout =3D + WD_PERIOD_S(2, pcf2127->cfg->wdd_clock_hz_x1000); + pcf2127->wdd.max_timeout =3D + WD_PERIOD_S(255, pcf2127->cfg->wdd_clock_hz_x1000); + pcf2127->wdd.timeout =3D WD_COUNTER(PCF2127_WD_VAL_DEFAULT, + pcf2127->cfg->wdd_clock_hz_x1000); + + dev_dbg(dev, "%s min =3D %ds\n", __func__, pcf2127->wdd.min_timeout); + dev_dbg(dev, "%s max =3D %ds\n", __func__, pcf2127->wdd.max_timeout); + dev_dbg(dev, "%s def =3D %d\n", __func__, pcf2127->wdd.timeout); + + pcf2127->wdd.min_hw_heartbeat_ms =3D pcf2127->cfg->wdd_min_hw_heartbeat_m= s; pcf2127->wdd.status =3D WATCHDOG_NOWAYOUT_INIT_STATUS; =20 watchdog_set_drvdata(&pcf2127->wdd, pcf2127); @@ -926,6 +960,8 @@ static struct pcf21xx_config pcf21xx_cfg[] =3D { .reg_wd_ctl =3D PCF2127_REG_WD_CTL, .reg_wd_val =3D PCF2127_REG_WD_VAL, .reg_clkout =3D PCF2127_REG_CLKOUT, + .wdd_clock_hz_x1000 =3D PCF2127_WD_CLOCK_HZ_X1000, + .wdd_min_hw_heartbeat_ms =3D PCF2127_WD_MIN_HW_HEARTBEAT_MS, .ts_count =3D 1, .ts[0] =3D { .regs_base =3D PCF2127_REG_TS1_BASE, @@ -951,6 +987,8 @@ static struct pcf21xx_config pcf21xx_cfg[] =3D { .reg_wd_ctl =3D PCF2127_REG_WD_CTL, .reg_wd_val =3D PCF2127_REG_WD_VAL, .reg_clkout =3D PCF2127_REG_CLKOUT, + .wdd_clock_hz_x1000 =3D PCF2127_WD_CLOCK_HZ_X1000, + .wdd_min_hw_heartbeat_ms =3D PCF2127_WD_MIN_HW_HEARTBEAT_MS, .ts_count =3D 1, .ts[0] =3D { .regs_base =3D PCF2127_REG_TS1_BASE, @@ -977,6 +1015,8 @@ static struct pcf21xx_config pcf21xx_cfg[] =3D { .reg_wd_val =3D PCF2131_REG_WD_VAL, .reg_clkout =3D PCF2131_REG_CLKOUT, .reg_reset =3D PCF2131_REG_SR_RESET, + .wdd_clock_hz_x1000 =3D PCF2131_WD_CLOCK_HZ_X1000, + .wdd_min_hw_heartbeat_ms =3D PCF2131_WD_MIN_HW_HEARTBEAT_MS, .ts_count =3D 4, .ts[0] =3D { .regs_base =3D PCF2131_REG_TS1_BASE, @@ -1215,7 +1255,7 @@ static int pcf2127_probe(struct device *dev, struct r= egmap *regmap, =20 /* * Watchdog timer enabled and reset pin /RST activated when timed out. - * Select 1Hz clock source for watchdog timer. + * Select 1Hz clock source for watchdog timer (1/4Hz for PCF2131). * Note: Countdown timer disabled and not available. * For pca2129, pcf2129 and pcf2131, only bit[7] is for Symbol WD_CD * of register watchdg_tim_ctl. The bit[6] is labeled --=20 2.30.2