Linux RTC
 help / color / mirror / Atom feed
From: Herve Codina <herve.codina@bootlin.com>
To: sashiko-bot@kernel.org
Cc: sashiko-reviews@lists.linux.dev, linux-rtc@vger.kernel.org,
	devicetree@vger.kernel.org, robh@kernel.org, conor+dt@kernel.org,
	"Alexandre Belloni" <alexandre.belloni@bootlin.com>
Subject: Re: [PATCH 2/3] rtc: pcf2127: Add support for the clkout clock
Date: Mon, 2 Nov 2026 14:49:56 +0100	[thread overview]
Message-ID: <20261102144956.57f9bbf3@bootlin.com> (raw)
In-Reply-To: <sashiko-outbox-193475@kernel.org>

On Mon, 02 Nov 2026 13:13:46 +0000
sashiko-bot@kernel.org wrote:

> Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
> - [Medium] rtc: pcf2127: rate 0 in pcf2127_clkout_rate2cof() sets COF to HiZ
> --
> 
> commit fd3d38a37715a1ab67448b3adb80c6b0ee46a7e2
> Author: Herve Codina <herve.codina@bootlin.com>
> 
> rtc: pcf2127: Add support for the clkout clock
> 
> This commit adds common clock framework support for the CLKOUT output clock
> on PCF2127, PCF2129, PCA2129, and PCF2131 RTC devices, allowing configuration
> of frequencies from 1 Hz to 32768 Hz and disabling the output into HiZ.
> 
> > diff --git a/drivers/rtc/rtc-pcf2127.c b/drivers/rtc/rtc-pcf2127.c
> > index ff2023908f5a3..2c442770ebc52 100644
> > --- a/drivers/rtc/rtc-pcf2127.c
> > +++ b/drivers/rtc/rtc-pcf2127.c  
> 
> [ ... ]
> 
> > @@ -626,6 +634,180 @@ static int pcf2127_watchdog_init(struct device *dev, struct pcf2127 *pcf2127)
> >  	return devm_watchdog_register_device(dev, &pcf2127->wdd);
> >  }
> >  
> > +#ifdef CONFIG_COMMON_CLK
> > +#define clkout_hw_to_pcf2127(_hw) container_of(_hw, struct pcf2127, clkout_hw)
> > +
> > +/* Rates indexed by COF values */
> > +static int pcf2127_clkout_rates[] = {
> > +	32768,
> > +	16384,
> > +	8192,
> > +	4096,
> > +	2048,
> > +	1024,
> > +	1,
> > +	0, /* 0Hz (COF 0b111) will put the clockout signal to HiZ */
> > +};
> > +
> > +static u32 pcf2127_clkout_rate2cof(unsigned long rate)
> > +{
> > +	unsigned int i;
> > +
> > +	for (i = 0; i < ARRAY_SIZE(pcf2127_clkout_rates); i++) {
> > +		if (pcf2127_clkout_rates[i] <= rate)
> > +			return i;
> > +	}
> > +
> > +	return 0; /* Maximum output clock rate */
> > +}
> > +
> > +static int pcf2127_clkout_cof2rate(u32 cof)
> > +{
> > +	return pcf2127_clkout_rates[cof];
> > +}
> > +
> > +static unsigned long pcf2127_clkout_recalc_rate(struct clk_hw *hw,
> > +						unsigned long parent_rate)
> > +{
> > +	struct pcf2127 *pcf2127 = clkout_hw_to_pcf2127(hw);
> > +
> > +	return pcf2127_clkout_cof2rate(pcf2127->clkout_cof);
> > +}
> > +
> > +static int pcf2127_clkout_determine_rate(struct clk_hw *hw,
> > +					 struct clk_rate_request *req)
> > +{
> > +	u32 cof;
> > +
> > +	cof = pcf2127_clkout_rate2cof(req->rate);
> > +	req->rate = pcf2127_clkout_cof2rate(cof);
> > +
> > +	return 0;
> > +}
> > +
> > +static int pcf2127_clkout_set_rate(struct clk_hw *hw, unsigned long rate,
> > +				   unsigned long parent_rate)
> > +{
> > +	struct pcf2127 *pcf2127 = clkout_hw_to_pcf2127(hw);
> > +	u32 cof;
> > +	int ret;
> > +
> > +	cof = pcf2127_clkout_rate2cof(rate);
> > +
> > +	/*
> > +	 * Setting COF will enable the clock (i.e. output not in HiZ anymore)
> > +	 * Perform the operation only if already enabled.
> > +	 * Otherwise, do nothing. The expected COF will be set on prepare()
> > +	 */
> > +	if (pcf2127->clkout_enabled) {
> > +		ret = regmap_update_bits(pcf2127->regmap, pcf2127->cfg->reg_clkout,
> > +					 PCF2127_CLKOUT_COF,
> > +					 FIELD_PREP(PCF2127_CLKOUT_COF, cof));
> > +		if (ret)
> > +			return ret;
> > +	}
> > +
> > +	pcf2127->clkout_cof = cof;
> > +	return 0;
> > +}  
> 
> [Severity: Medium]
> Since rate is an unsigned long and pcf2127_clkout_rates[7] is 0, the condition
> pcf2127_clkout_rates[i] <= rate in pcf2127_clkout_rate2cof() will always match
> at index 7 (PCF2127_CLKOUT_COF_HIZ) when rate is 0, making the fallback
> return 0 unreachable.
> 
> If a consumer passes rate == 0 to pcf2127_clkout_determine_rate() or
> pcf2127_clkout_set_rate(), pcf2127_clkout_rate2cof(0) returns 7
> (PCF2127_CLKOUT_COF_HIZ). In pcf2127_clkout_set_rate(), this writes
> PCF2127_CLKOUT_COF_HIZ to reg_clkout if pcf2127->clkout_enabled is true
> (putting the CLKOUT pin into HiZ while pcf2127->clkout_enabled remains true),
> and caches PCF2127_CLKOUT_COF_HIZ in pcf2127->clkout_cof so subsequent calls
> to pcf2127_clkout_prepare() -> pcf2127_clkout_control(hw, true) will also
> leave the pin in HiZ.

This is exactly what it is expected.

return 0 is unreachable but pcf2127_clkout_rate2cof() returns a u32.

Without the last return 0 (even if unreachable) or any other u32 returned value,
GCC obviously complied
   error: control reaches end of non-void function

> 
> Should the 0 entry be removed from pcf2127_clkout_rates[] (or skipped in
> pcf2127_clkout_rate2cof()) so that pcf2127->clkout_cof only ever holds active
> clock divider values (0..6)?
> 

No.

All rates should be present in pcf2127_clkout_rates[]. The 0Hz rate value is
a valid rate even if the pin will be set in HiZ in that case.

pcf2127->clkout_cof should hold the value related to OHz rate.

A user can ask a 0Hz rate and call clk_prepare(). This is a valid use case.

Best regards,
Hervé

  reply	other threads:[~2026-11-02 13:50 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-02 13:07 [PATCH 0/3] rtc: pcf2127: Add support for output clock and disable the pcf2131 100th counter Herve Codina
2026-11-02 13:07 ` [PATCH 1/3] dt-bindings: rtc: pcf2127: Add support for clkout clock Herve Codina
2026-11-02 13:09   ` sashiko-bot
2026-11-02 19:32   ` Conor Dooley
2026-11-02 13:07 ` [PATCH 2/3] rtc: pcf2127: Add support for the " Herve Codina
2026-11-02 13:13   ` sashiko-bot
2026-11-02 13:49     ` Herve Codina [this message]
2026-11-02 13:07 ` [PATCH 3/3] rtc: pcf2127: Disable the 100th counter for PCF2131 Herve Codina
2026-11-02 13:10   ` sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261102144956.57f9bbf3@bootlin.com \
    --to=herve.codina@bootlin.com \
    --cc=alexandre.belloni@bootlin.com \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=linux-rtc@vger.kernel.org \
    --cc=robh@kernel.org \
    --cc=sashiko-bot@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox