Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

iio: tsl2583: Use DIV_ROUND_CLOSEST() instead of open-coding it

Use DIV_ROUND_CLOSEST() instead of open-coding it. This documents intent
and makes it more clear what is going on for the casual reviewer.

Generated using the following the Coccinelle semantic patch.

// <smpl>
@r1@
expression x;
constant C1;
constant C2;
@@
((x) + C1) / C2

@script:python@
C1 << r1.C1;
C2 << r1.C2;
@@
try:
if int(C1) * 2 != int(C2):
cocci.include_match(False)
except:
cocci.include_match(False)

@@
expression r1.x;
constant r1.C1;
constant r1.C2;
@@
-(((x) + C1) / C2)
+DIV_ROUND_CLOSEST(x, C2)
// </smpl>

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
Link: https://lore.kernel.org/r/20201227171126.28216-3-lars@metafoo.de
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

authored by

Lars-Peter Clausen and committed by
Jonathan Cameron
9f094829 166549bb

+4 -4
+4 -4
drivers/iio/light/tsl2583.c
··· 285 285 lux64 = lux64 * chip->als_settings.als_gain_trim; 286 286 lux64 >>= 13; 287 287 lux = lux64; 288 - lux = (lux + 500) / 1000; 288 + lux = DIV_ROUND_CLOSEST(lux, 1000); 289 289 290 290 if (lux > TSL2583_LUX_CALC_OVER_FLOW) { /* check for overflow */ 291 291 return_max: ··· 361 361 u8 val; 362 362 363 363 /* determine als integration register */ 364 - als_count = (chip->als_settings.als_time * 100 + 135) / 270; 364 + als_count = DIV_ROUND_CLOSEST(chip->als_settings.als_time * 100, 270); 365 365 if (!als_count) 366 366 als_count = 1; /* ensure at least one cycle */ 367 367 368 368 /* convert back to time (encompasses overrides) */ 369 - als_time = (als_count * 27 + 5) / 10; 369 + als_time = DIV_ROUND_CLOSEST(als_count * 27, 10); 370 370 371 371 val = 256 - als_count; 372 372 ret = i2c_smbus_write_byte_data(chip->client, ··· 380 380 381 381 /* set chip struct re scaling and saturation */ 382 382 chip->als_saturation = als_count * 922; /* 90% of full scale */ 383 - chip->als_time_scale = (als_time + 25) / 50; 383 + chip->als_time_scale = DIV_ROUND_CLOSEST(als_time, 50); 384 384 385 385 return ret; 386 386 }