On Wed, Jan 25, 2017 at 11:53 AM, Guenter Roeck <linux@xxxxxxxxxxxx> wrote: > On 01/25/2017 01:51 AM, Andrea Merello wrote: >> >> On Tue, Jan 24, 2017 at 9:18 PM, Guenter Roeck <linux@xxxxxxxxxxxx> wrote: >>> >>> On Tue, Jan 24, 2017 at 04:02:20PM +0100, Andrea Merello wrote: >>>> >>>> This patch adds a HWMON driver for ST Microelectronics STTS751 >>>> temperature sensors. >>>> >>>> It does support manual-triggered conversions as well as automatic >>>> conversions. The latter is used when the "event" or "therm" function >>>> is present (declaring the physical wire is attached in the DT). >>>> >>>> Thanks-to: LABBE Corentin [for suggestions] >>>> Signed-off-by: Andrea Merello <andrea.merello@xxxxxxxxx> >>>> Cc: LABBE Corentin <clabbe.montjoie@xxxxxxxxx> >>>> Cc: Guenter Roeck <linux@xxxxxxxxxxxx> >>>> Cc: Jean Delvare <jdelvare@xxxxxxxx> >>>> --- >>> >>> > > [ ... ] > >>>> + >>>> +static s32 stts751_to_hw(int val) >>>> +{ >>>> + s32 hw_val; >>>> + >>>> + if (val < 0) >>>> + hw_val = (val - 62) / 125 * 32; >>>> + else >>>> + hw_val = (val + 62) / 125 * 32; >>>> + >>> >>> >>> How about "return DIV_ROUND_CLOSEST(val, 125) * 32;" ? >> >> >> I admit that I stale this from lm90 without caring too much.. Now, >> looking at it, I realized that we could also improve the calculation >> precision by performing the multiplication before the division: >> >> DIV_ROUND_CLOSEST(val * 32, 125); >> >> (I think doing in this way makes rounding almost useless. Maybe it has >> a slight effect.) >> > > The purpose of doing the multiplication afterwards is to make sure > that the lower 5 bits are 0 (or, rather, to shift the result 5 bits > to the left). Doing it first defeats that purpose, and would actually > mess up the rounding (because the lower bits will be ignored by the > hardware). For example, writing 0xff would yield 0xe0, while what > you would probably really want is to write 0x1e0 in that situation. Ah, right. Sorry, while looking at the calculations I lost focus on the specific context (hw details). > Guenter > -- To unsubscribe from this list: send the line "unsubscribe linux-hwmon" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html