On Tue, Sep 07, 2021 at 11:25:43PM +0200, Ansuel Smith wrote: > The function can loop and lock the system if for whatever reason the bit > for the target sensor is NEVER valid. This is the case if a sensor is > disabled by the factory and the valid bit is never reported as actually > valid. Add a timeout check and exit if a timeout occurs. As this is > a very rare condition, handle the timeout only if the first read fails. > While at it also rework the function to improve readability. > > Signed-off-by: Ansuel Smith <ansuelsmth@xxxxxxxxx> > --- > drivers/thermal/qcom/tsens.c | 40 +++++++++++++++++++++++------------- > 1 file changed, 26 insertions(+), 14 deletions(-) > > diff --git a/drivers/thermal/qcom/tsens.c b/drivers/thermal/qcom/tsens.c > index b1162e566a70..1ff244176beb 100644 > --- a/drivers/thermal/qcom/tsens.c > +++ b/drivers/thermal/qcom/tsens.c > @@ -599,26 +599,38 @@ int get_temp_tsens_valid(const struct tsens_sensor *s, int *temp) > int hw_id = s->hw_id; > u32 temp_idx = LAST_TEMP_0 + hw_id; > u32 valid_idx = VALID_0 + hw_id; > + unsigned long timeout; > u32 valid; > int ret; > > /* VER_0 doesn't have VALID bit */ > - if (tsens_version(priv) >= VER_0_1) { > + if (tsens_version(priv) == VER_0) > + goto get_temp; > + > + ret = regmap_field_read(priv->rf[valid_idx], &valid); > + if (ret || valid) > + goto check_valid; > + > + timeout = jiffies + msecs_to_jiffies(20); > + do { > + /* Valid bit is 0 for 6 AHB clock cycles. > + * At 19.2MHz, 1 AHB clock is ~60ns. > + * We should enter this loop very, very rarely. > + */ > + ndelay(400); > ret = regmap_field_read(priv->rf[valid_idx], &valid); > - if (ret) > - return ret; > - while (!valid) { > - /* Valid bit is 0 for 6 AHB clock cycles. > - * At 19.2MHz, 1 AHB clock is ~60ns. > - * We should enter this loop very, very rarely. > - */ > - ndelay(400); > - ret = regmap_field_read(priv->rf[valid_idx], &valid); > - if (ret) > - return ret; > - } > - } > + if (ret || valid) > + goto check_valid; > + } while (time_before(jiffies, timeout)); > + > + return -ETIMEDOUT; > + > +check_valid: > + /* Check ret of valid bit read */ > + if (ret) > + return ret; > > +get_temp: > /* Valid bit is set, OK to read the temperature */ > *temp = tsens_hw_to_mC(s, temp_idx); I still think that something like this would be clearer than the multiple jumps to 'check_valid': ret = regmap_field_read(priv->rf[valid_idx], &valid); if (ret) return ret; timeout = jiffies + msecs_to_jiffies(20); while (!valid) { /* Valid bit is 0 for 6 AHB clock cycles. * At 19.2MHz, 1 AHB clock is ~60ns. * We should enter this loop very, very rarely. */ ndelay(400); ret = regmap_field_read(priv->rf[valid_idx], &valid); if (ret) return ret; if (time_after(jiffies, timeout)) return -ETIMEDOUT; }; That said, I'm just a random dude, so my opinion isn't really important, as long as the maintainers are happy with the change :)