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: light: zopt2201: Remove code duplication in scale write functions

Consolidate duplicated logic from zopt2201_write_scale_als_by_idx() and
zopt2201_write_scale_uvb_by_idx() into a new generic helper function
zopt2201_write_scale_by_idx(). This function takes an additional
parameter: a pointer to a zopt2201_scale array.

To support this, the previously anonymous and duplicated struct used in
the scale arrays was promoted to a named struct: zopt2201_scale.

This change also corrects an incorrect array access that existed in
zopt2201_write_scale_uvb_by_idx().

Signed-off-by: Beatriz Viana Costa <beatrizvianacosta16@gmail.com>
Co-developed-by: Gabriela Victor <gabevictor333@gmail.com>
Signed-off-by: Gabriela Victor <gabevictor333@gmail.com>
Link: https://patch.msgid.link/20250424002144.23260-1-beatrizvianacosta16@gmail.com
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

authored by

Beatriz Viana Costa and committed by
Jonathan Cameron
50ed17cd 52c43d80

+12 -30
+12 -30
drivers/iio/light/zopt2201.c
··· 113 113 { 13, 3125 }, 114 114 }; 115 115 116 - static const struct { 116 + struct zopt2201_scale { 117 117 unsigned int scale, uscale; /* scale factor as integer + micro */ 118 118 u8 gain; /* gain register value */ 119 119 u8 res; /* resolution register value */ 120 - } zopt2201_scale_als[] = { 120 + }; 121 + 122 + static struct zopt2201_scale zopt2201_scale_als[] = { 121 123 { 19, 200000, 0, 5 }, 122 124 { 6, 400000, 1, 5 }, 123 125 { 3, 200000, 2, 5 }, ··· 144 142 { 0, 8333, 4, 0 }, 145 143 }; 146 144 147 - static const struct { 148 - unsigned int scale, uscale; /* scale factor as integer + micro */ 149 - u8 gain; /* gain register value */ 150 - u8 res; /* resolution register value */ 151 - } zopt2201_scale_uvb[] = { 145 + static struct zopt2201_scale zopt2201_scale_uvb[] = { 152 146 { 0, 460800, 0, 5 }, 153 147 { 0, 153600, 1, 5 }, 154 148 { 0, 76800, 2, 5 }, ··· 346 348 return 0; 347 349 } 348 350 349 - static int zopt2201_write_scale_als_by_idx(struct zopt2201_data *data, int idx) 351 + static int zopt2201_write_scale_by_idx(struct zopt2201_data *data, int idx, 352 + struct zopt2201_scale *zopt2201_scale_array) 350 353 { 351 354 int ret; 352 355 353 356 mutex_lock(&data->lock); 354 - ret = zopt2201_set_resolution(data, zopt2201_scale_als[idx].res); 357 + ret = zopt2201_set_resolution(data, zopt2201_scale_array[idx].res); 355 358 if (ret < 0) 356 359 goto unlock; 357 360 358 - ret = zopt2201_set_gain(data, zopt2201_scale_als[idx].gain); 361 + ret = zopt2201_set_gain(data, zopt2201_scale_array[idx].gain); 359 362 360 363 unlock: 361 364 mutex_unlock(&data->lock); ··· 370 371 371 372 for (i = 0; i < ARRAY_SIZE(zopt2201_scale_als); i++) 372 373 if (val == zopt2201_scale_als[i].scale && 373 - val2 == zopt2201_scale_als[i].uscale) { 374 - return zopt2201_write_scale_als_by_idx(data, i); 375 - } 374 + val2 == zopt2201_scale_als[i].uscale) 375 + return zopt2201_write_scale_by_idx(data, i, zopt2201_scale_als); 376 376 377 377 return -EINVAL; 378 - } 379 - 380 - static int zopt2201_write_scale_uvb_by_idx(struct zopt2201_data *data, int idx) 381 - { 382 - int ret; 383 - 384 - mutex_lock(&data->lock); 385 - ret = zopt2201_set_resolution(data, zopt2201_scale_als[idx].res); 386 - if (ret < 0) 387 - goto unlock; 388 - 389 - ret = zopt2201_set_gain(data, zopt2201_scale_als[idx].gain); 390 - 391 - unlock: 392 - mutex_unlock(&data->lock); 393 - return ret; 394 378 } 395 379 396 380 static int zopt2201_write_scale_uvb(struct zopt2201_data *data, ··· 384 402 for (i = 0; i < ARRAY_SIZE(zopt2201_scale_uvb); i++) 385 403 if (val == zopt2201_scale_uvb[i].scale && 386 404 val2 == zopt2201_scale_uvb[i].uscale) 387 - return zopt2201_write_scale_uvb_by_idx(data, i); 405 + return zopt2201_write_scale_by_idx(data, i, zopt2201_scale_uvb); 388 406 389 407 return -EINVAL; 390 408 }