[PATCH] rtc: spacemit: handle regmap_test_bits() error return

Alexandre Belloni alexandre.belloni at bootlin.com
Fri Jul 24 06:39:30 PDT 2026


On 24/07/2026 08:32:13-0500, Alex Elder wrote:
> On 7/23/26 9:42 PM, kr494167 at gmail.com wrote:
> > From: Surendra Singh Chouhan <kr494167 at gmail.com>
> > 
> > p1_rtc_read_time() called if (!regmap_test_bits(regmap, RTC_CTRL, RTC_EN))
> > to check if the RTC was enabled.
> > 
> > regmap_test_bits() returns 1 if the bit is set, 0 if not set, and a
> > negative error code (e.g. -EIO) if reading the control register fails.
> > Using !regmap_test_bits(...) evaluates a negative error code as boolean
> > false, causing I2C/regmap read failures to be ignored and incorrectly
> > proceeding to read time registers from a failing device.
> > 
> > Fix this by capturing the return value of regmap_test_bits() and returning
> > the error code if negative, or -EINVAL if the RTC is disabled.
> > 
> > Fixes: a6de182daa2b ("rtc: spacemit: support the SpacemiT P1 RTC")
> > Signed-off-by: Surendra Singh Chouhan <kr494167 at gmail.com>
> 
> This is an I2C-based regmap, so yes, we do need to test
> for errors when reading or writing registers.
> 
> I think this is a good fix.  I have a minor suggestion or
> two below but either way:
> 
> Reviewed-by: Alex Elder <elder at riscstar.com>
> 
> > ---
> >   drivers/rtc/rtc-spacemit-p1.c | 5 +++--
> >   1 file changed, 3 insertions(+), 2 deletions(-)
> > 
> > diff --git a/drivers/rtc/rtc-spacemit-p1.c b/drivers/rtc/rtc-spacemit-p1.c
> > index 43ab62494bb4..c1820c920ba3 100644
> > --- a/drivers/rtc/rtc-spacemit-p1.c
> > +++ b/drivers/rtc/rtc-spacemit-p1.c
> > @@ -57,8 +57,9 @@ static int p1_rtc_read_time(struct device *dev, struct rtc_time *t)
> >   	u8 time[6];
> >   	int ret;
> > -	if (!regmap_test_bits(regmap, RTC_CTRL, RTC_EN))
> > -		return -EINVAL;		/* RTC is disabled */
> > +	ret = regmap_test_bits(regmap, RTC_CTRL, RTC_EN);
> > +	if (ret <= 0)
> > +		return ret ? ret : -EINVAL;	/* RTC is disabled or error */
> 
> Since regmap_test_bits() can return -EINVAL, I wonder if a
> different error code would be better to indicate "disabled".
> It can return -EBUSY and -EIO too, as well as anything any
> regmap->reg_read() can return.  Scanning through the list of
> basic error codes I don't have a good suggestion, unfortunately.
> 

No, EINVAL is standard across all the drivers. I considered changing
this to ETIME or ENODATA but that would break userspace too much.

> Otherwise, perhaps just:
> 
> 		return ret ? : -EINVAL;  /* comment if you like */
> 
> >   	ret = regmap_bulk_read(regmap, RTC_TIME, time, sizeof(time));
> >   	if (ret)
> 

-- 
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com



More information about the linux-riscv mailing list