diff mbox series

[v2,11/16] hwmon: (mr75203) add protection for negative voltage value

Message ID 20220817054321.6519-12-farbere@amazon.com (mailing list archive)
State Changes Requested
Headers show
Series Variety of fixes and new features for mr75203 driver | expand

Commit Message

Farber, Eliav Aug. 17, 2022, 5:43 a.m. UTC
This change makes sure the returned voltage vlaue is 0 or positive.

Signed-off-by: Eliav Farber <farbere@amazon.com>
---
 drivers/hwmon/mr75203.c | 7 +++++++
 1 file changed, 7 insertions(+)

Comments

Guenter Roeck Aug. 18, 2022, 8:13 p.m. UTC | #1
On Wed, Aug 17, 2022 at 05:43:16AM +0000, Eliav Farber wrote:
> This change makes sure the returned voltage vlaue is 0 or positive.
> 
> Signed-off-by: Eliav Farber <farbere@amazon.com>
> ---
>  drivers/hwmon/mr75203.c | 7 +++++++
>  1 file changed, 7 insertions(+)
> 
> diff --git a/drivers/hwmon/mr75203.c b/drivers/hwmon/mr75203.c
> index 24a00339cfd8..e3191f590167 100644
> --- a/drivers/hwmon/mr75203.c
> +++ b/drivers/hwmon/mr75203.c
> @@ -218,6 +218,13 @@ static int pvt_read_in(struct device *dev, u32 attr, int channel, long *val)
>  			return ret;
>  
>  		n &= SAMPLE_DATA_MSK;
> +
> +		/* Voltage can't be negative */

Who says, and what does that mean ? Under which conditions would
the value be negative, and why would that be a problem / bug ?
After all, negative voltages do exist.

Guenter

> +		if (PVT_N_CONST * n < PVT_R_CONST) {
> +			*val = 0;
> +			return 0;
> +		}
> +
>  		/* Convert the N bitstream count into voltage */
>  		*val = pvt->vd[channel].pre_scaler;
>  		*val *= (PVT_N_CONST * n - PVT_R_CONST);
diff mbox series

Patch

diff --git a/drivers/hwmon/mr75203.c b/drivers/hwmon/mr75203.c
index 24a00339cfd8..e3191f590167 100644
--- a/drivers/hwmon/mr75203.c
+++ b/drivers/hwmon/mr75203.c
@@ -218,6 +218,13 @@  static int pvt_read_in(struct device *dev, u32 attr, int channel, long *val)
 			return ret;
 
 		n &= SAMPLE_DATA_MSK;
+
+		/* Voltage can't be negative */
+		if (PVT_N_CONST * n < PVT_R_CONST) {
+			*val = 0;
+			return 0;
+		}
+
 		/* Convert the N bitstream count into voltage */
 		*val = pvt->vd[channel].pre_scaler;
 		*val *= (PVT_N_CONST * n - PVT_R_CONST);