summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBeatriz Viana Costa <beatrizvianacosta16@gmail.com>2025-04-23 21:21:44 -0300
committerJonathan Cameron <Jonathan.Cameron@huawei.com>2025-05-21 14:20:28 +0100
commit50ed17cdfd7f32f1b0f87da0aaabcd5e8dd23f19 (patch)
tree92c94a0a06d480ad5842bb0a1c2c29e8db410070
parent52c43d80fa8370eb877fc63b1fc1eec67e1b1410 (diff)
downloadlinux-50ed17cdfd7f32f1b0f87da0aaabcd5e8dd23f19.tar.gz
linux-50ed17cdfd7f32f1b0f87da0aaabcd5e8dd23f19.zip
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>
-rw-r--r--drivers/iio/light/zopt2201.c42
1 files changed, 12 insertions, 30 deletions
diff --git a/drivers/iio/light/zopt2201.c b/drivers/iio/light/zopt2201.c
index 604be60e92ac..1e5e9bf2935f 100644
--- a/drivers/iio/light/zopt2201.c
+++ b/drivers/iio/light/zopt2201.c
@@ -113,11 +113,13 @@ static const struct {
{ 13, 3125 },
};
-static const struct {
+struct zopt2201_scale {
unsigned int scale, uscale; /* scale factor as integer + micro */
u8 gain; /* gain register value */
u8 res; /* resolution register value */
-} zopt2201_scale_als[] = {
+};
+
+static struct zopt2201_scale zopt2201_scale_als[] = {
{ 19, 200000, 0, 5 },
{ 6, 400000, 1, 5 },
{ 3, 200000, 2, 5 },
@@ -142,11 +144,7 @@ static const struct {
{ 0, 8333, 4, 0 },
};
-static const struct {
- unsigned int scale, uscale; /* scale factor as integer + micro */
- u8 gain; /* gain register value */
- u8 res; /* resolution register value */
-} zopt2201_scale_uvb[] = {
+static struct zopt2201_scale zopt2201_scale_uvb[] = {
{ 0, 460800, 0, 5 },
{ 0, 153600, 1, 5 },
{ 0, 76800, 2, 5 },
@@ -348,16 +346,17 @@ static int zopt2201_set_gain(struct zopt2201_data *data, u8 gain)
return 0;
}
-static int zopt2201_write_scale_als_by_idx(struct zopt2201_data *data, int idx)
+static int zopt2201_write_scale_by_idx(struct zopt2201_data *data, int idx,
+ struct zopt2201_scale *zopt2201_scale_array)
{
int ret;
mutex_lock(&data->lock);
- ret = zopt2201_set_resolution(data, zopt2201_scale_als[idx].res);
+ ret = zopt2201_set_resolution(data, zopt2201_scale_array[idx].res);
if (ret < 0)
goto unlock;
- ret = zopt2201_set_gain(data, zopt2201_scale_als[idx].gain);
+ ret = zopt2201_set_gain(data, zopt2201_scale_array[idx].gain);
unlock:
mutex_unlock(&data->lock);
@@ -371,29 +370,12 @@ static int zopt2201_write_scale_als(struct zopt2201_data *data,
for (i = 0; i < ARRAY_SIZE(zopt2201_scale_als); i++)
if (val == zopt2201_scale_als[i].scale &&
- val2 == zopt2201_scale_als[i].uscale) {
- return zopt2201_write_scale_als_by_idx(data, i);
- }
+ val2 == zopt2201_scale_als[i].uscale)
+ return zopt2201_write_scale_by_idx(data, i, zopt2201_scale_als);
return -EINVAL;
}
-static int zopt2201_write_scale_uvb_by_idx(struct zopt2201_data *data, int idx)
-{
- int ret;
-
- mutex_lock(&data->lock);
- ret = zopt2201_set_resolution(data, zopt2201_scale_als[idx].res);
- if (ret < 0)
- goto unlock;
-
- ret = zopt2201_set_gain(data, zopt2201_scale_als[idx].gain);
-
-unlock:
- mutex_unlock(&data->lock);
- return ret;
-}
-
static int zopt2201_write_scale_uvb(struct zopt2201_data *data,
int val, int val2)
{
@@ -402,7 +384,7 @@ static int zopt2201_write_scale_uvb(struct zopt2201_data *data,
for (i = 0; i < ARRAY_SIZE(zopt2201_scale_uvb); i++)
if (val == zopt2201_scale_uvb[i].scale &&
val2 == zopt2201_scale_uvb[i].uscale)
- return zopt2201_write_scale_uvb_by_idx(data, i);
+ return zopt2201_write_scale_by_idx(data, i, zopt2201_scale_uvb);
return -EINVAL;
}