iio: light: zopt2201: Remove code duplication in scale write functions
authorBeatriz Viana Costa <beatrizvianacosta16@gmail.com>
Thu, 24 Apr 2025 00:21:44 +0000 (21:21 -0300)
committerJonathan Cameron <Jonathan.Cameron@huawei.com>
Wed, 21 May 2025 13:20:28 +0000 (14:20 +0100)
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>
drivers/iio/light/zopt2201.c

index 604be60e92acae020acdbd274a599726e31983f8..1e5e9bf2935f63f9af4cdbf29f825b218ec477be 100644 (file)
@@ -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;
 }