diff mbox series

[2/6] hwmon: (lm95234) Use find_closest to find matching update interval

Message ID 20240718033935.205185-3-linux@roeck-us.net (mailing list archive)
State Accepted
Headers show
Series hwmon: (lm9534) Various improvements | expand

Commit Message

Guenter Roeck July 18, 2024, 3:39 a.m. UTC
Use find_closest() instead of manually coding it to find best update
interval.

Signed-off-by: Guenter Roeck <linux@roeck-us.net>
---
 drivers/hwmon/lm95234.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

Comments

Tzung-Bi Shih July 18, 2024, 4:39 p.m. UTC | #1
On Wed, Jul 17, 2024 at 08:39:31PM -0700, Guenter Roeck wrote:
> @@ -471,10 +472,7 @@ static ssize_t update_interval_store(struct device *dev,
>  	if (ret < 0)
>  		return ret;
>  
> -	for (regval = 0; regval < 3; regval++) {
> -		if (val <= update_intervals[regval])
> -			break;
> -	}
> +	regval = find_closest(val, update_intervals, ARRAY_SIZE(update_intervals));

The behavior changed.

static u16 update_intervals[] = { 143, 364, 1000, 2500 };

If val = 144,
* Originally, regval = 1.
* After applying the patch, regval = 0.
Guenter Roeck July 18, 2024, 5:48 p.m. UTC | #2
On 7/18/24 09:39, Tzung-Bi Shih wrote:
> On Wed, Jul 17, 2024 at 08:39:31PM -0700, Guenter Roeck wrote:
>> @@ -471,10 +472,7 @@ static ssize_t update_interval_store(struct device *dev,
>>   	if (ret < 0)
>>   		return ret;
>>   
>> -	for (regval = 0; regval < 3; regval++) {
>> -		if (val <= update_intervals[regval])
>> -			break;
>> -	}
>> +	regval = find_closest(val, update_intervals, ARRAY_SIZE(update_intervals));
> 
> The behavior changed.
> 
> static u16 update_intervals[] = { 143, 364, 1000, 2500 };
> 
> If val = 144,
> * Originally, regval = 1.
> * After applying the patch, regval = 0.
> 


Yes, find_closest() rounds the value instead of using the lower match.
That was intentional. I'll add an explicit note to the commit message.

Thanks,
Guenter
Guenter Roeck July 18, 2024, 5:53 p.m. UTC | #3
On 7/18/24 10:48, Guenter Roeck wrote:
> On 7/18/24 09:39, Tzung-Bi Shih wrote:
>> On Wed, Jul 17, 2024 at 08:39:31PM -0700, Guenter Roeck wrote:
>>> @@ -471,10 +472,7 @@ static ssize_t update_interval_store(struct device *dev,
>>>       if (ret < 0)
>>>           return ret;
>>> -    for (regval = 0; regval < 3; regval++) {
>>> -        if (val <= update_intervals[regval])
>>> -            break;
>>> -    }
>>> +    regval = find_closest(val, update_intervals, ARRAY_SIZE(update_intervals));
>>
>> The behavior changed.
>>
>> static u16 update_intervals[] = { 143, 364, 1000, 2500 };
>>
>> If val = 144,
>> * Originally, regval = 1.
>> * After applying the patch, regval = 0.
>>
> 
> 
> Yes, find_closest() rounds the value instead of using the lower match.
> That was intentional. I'll add an explicit note to the commit message.
> 

I added this to the commit message:

     Since find_closest() uses rounding to find the best match, the resulting
     update interval will now reflect the update interval that is closest to
     the requested value, not the value that is lower or equal to the requested
     value.

Thanks,
Guenter
Tzung-Bi Shih July 19, 2024, 12:52 a.m. UTC | #4
On Thu, Jul 18, 2024 at 10:53:19AM -0700, Guenter Roeck wrote:
> On 7/18/24 10:48, Guenter Roeck wrote:
> > On 7/18/24 09:39, Tzung-Bi Shih wrote:
> > > On Wed, Jul 17, 2024 at 08:39:31PM -0700, Guenter Roeck wrote:
> > > > @@ -471,10 +472,7 @@ static ssize_t update_interval_store(struct device *dev,
> > > >       if (ret < 0)
> > > >           return ret;
> > > > -    for (regval = 0; regval < 3; regval++) {
> > > > -        if (val <= update_intervals[regval])
> > > > -            break;
> > > > -    }
> > > > +    regval = find_closest(val, update_intervals, ARRAY_SIZE(update_intervals));
> > > 
> > > The behavior changed.
> > > 
> > > static u16 update_intervals[] = { 143, 364, 1000, 2500 };
> > > 
> > > If val = 144,
> > > * Originally, regval = 1.
> > > * After applying the patch, regval = 0.
> > > 
> > 
> > 
> > Yes, find_closest() rounds the value instead of using the lower match.
> > That was intentional. I'll add an explicit note to the commit message.
> > 
> 
> I added this to the commit message:
> 
>     Since find_closest() uses rounding to find the best match, the resulting
>     update interval will now reflect the update interval that is closest to
>     the requested value, not the value that is lower or equal to the requested
>     value.

With that,
Reviewed-by: Tzung-Bi Shih <tzungbi@kernel.org>
diff mbox series

Patch

diff --git a/drivers/hwmon/lm95234.c b/drivers/hwmon/lm95234.c
index 0c509eed6a01..a36fa7824da8 100644
--- a/drivers/hwmon/lm95234.c
+++ b/drivers/hwmon/lm95234.c
@@ -18,6 +18,7 @@ 
 #include <linux/slab.h>
 #include <linux/mutex.h>
 #include <linux/sysfs.h>
+#include <linux/util_macros.h>
 
 #define DRVNAME "lm95234"
 
@@ -471,10 +472,7 @@  static ssize_t update_interval_store(struct device *dev,
 	if (ret < 0)
 		return ret;
 
-	for (regval = 0; regval < 3; regval++) {
-		if (val <= update_intervals[regval])
-			break;
-	}
+	regval = find_closest(val, update_intervals, ARRAY_SIZE(update_intervals));
 
 	mutex_lock(&data->update_lock);
 	data->interval = msecs_to_jiffies(update_intervals[regval]);