Subject: [PATCH] media: i2c: tw9906: Use ARRAY_SIZE instead of manual checking

this driver currently uses a terminator(0x00, 0x00) to end the list
of reg-vals, instead a struct array with ARRAY_SIZE macro from
linux/kernel.h can be used to obtain the length of the array.

Signed-off-by: Moses Christopher Bollavarapu <[email protected]>
---
drivers/media/i2c/tw9906.c | 110 ++++++++++++++++++++-----------------
1 file changed, 60 insertions(+), 50 deletions(-)

diff --git a/drivers/media/i2c/tw9906.c b/drivers/media/i2c/tw9906.c
index c528eb01fed0..318f87ae63f2 100644
--- a/drivers/media/i2c/tw9906.c
+++ b/drivers/media/i2c/tw9906.c
@@ -6,6 +6,7 @@
#include <linux/module.h>
#include <linux/init.h>
#include <linux/i2c.h>
+#include <linux/kernel.h>
#include <linux/videodev2.h>
#include <linux/ioctl.h>
#include <linux/slab.h>
@@ -26,36 +27,40 @@ static inline struct tw9906 *to_state(struct v4l2_subdev *sd)
return container_of(sd, struct tw9906, sd);
}

-static const u8 initial_registers[] = {
- 0x02, 0x40, /* input 0, composite */
- 0x03, 0xa2, /* correct digital format */
- 0x05, 0x81, /* or 0x01 for PAL */
- 0x07, 0x02, /* window */
- 0x08, 0x14, /* window */
- 0x09, 0xf0, /* window */
- 0x0a, 0x10, /* window */
- 0x0b, 0xd0, /* window */
- 0x0d, 0x00, /* scaling */
- 0x0e, 0x11, /* scaling */
- 0x0f, 0x00, /* scaling */
- 0x10, 0x00, /* brightness */
- 0x11, 0x60, /* contrast */
- 0x12, 0x11, /* sharpness */
- 0x13, 0x7e, /* U gain */
- 0x14, 0x7e, /* V gain */
- 0x15, 0x00, /* hue */
- 0x19, 0x57, /* vbi */
- 0x1a, 0x0f,
- 0x1b, 0x40,
- 0x29, 0x03,
- 0x55, 0x00,
- 0x6b, 0x26,
- 0x6c, 0x36,
- 0x6d, 0xf0,
- 0x6e, 0x41,
- 0x6f, 0x13,
- 0xad, 0x70,
- 0x00, 0x00, /* Terminator (reg 0x00 is read-only) */
+struct reg_val {
+ u8 reg;
+ u8 val;
+};
+
+static const struct reg_val init_regs[] = {
+ {0x02, 0x40}, /* input 0, composite */
+ {0x03, 0xa2}, /* correct digital format */
+ {0x05, 0x81}, /* or 0x01 for PAL */
+ {0x07, 0x02}, /* window */
+ {0x08, 0x14}, /* window */
+ {0x09, 0xf0}, /* window */
+ {0x0a, 0x10}, /* window */
+ {0x0b, 0xd0}, /* window */
+ {0x0d, 0x00}, /* scaling */
+ {0x0e, 0x11}, /* scaling */
+ {0x0f, 0x00}, /* scaling */
+ {0x10, 0x00}, /* brightness */
+ {0x11, 0x60}, /* contrast */
+ {0x12, 0x11}, /* sharpness */
+ {0x13, 0x7e}, /* U gain */
+ {0x14, 0x7e}, /* V gain */
+ {0x15, 0x00}, /* hue */
+ {0x19, 0x57}, /* vbi */
+ {0x1a, 0x0f},
+ {0x1b, 0x40},
+ {0x29, 0x03},
+ {0x55, 0x00},
+ {0x6b, 0x26},
+ {0x6c, 0x36},
+ {0x6d, 0xf0},
+ {0x6e, 0x41},
+ {0x6f, 0x13},
+ {0xad, 0x70},
};

static int write_reg(struct v4l2_subdev *sd, u8 reg, u8 value)
@@ -65,13 +70,14 @@ static int write_reg(struct v4l2_subdev *sd, u8 reg, u8 value)
return i2c_smbus_write_byte_data(client, reg, value);
}

-static int write_regs(struct v4l2_subdev *sd, const u8 *regs)
+static int write_regs(struct v4l2_subdev *sd,
+ const struct reg_val *rv, int len)
{
- int i;
-
- for (i = 0; regs[i] != 0x00; i += 2)
- if (write_reg(sd, regs[i], regs[i + 1]) < 0)
+ while (--len >= 0) {
+ if (write_reg(sd, rv->reg, rv->val) < 0)
return -1;
+ rv++;
+ }
return 0;
}

@@ -86,24 +92,28 @@ static int tw9906_s_std(struct v4l2_subdev *sd, v4l2_std_id norm)
{
struct tw9906 *dec = to_state(sd);
bool is_60hz = norm & V4L2_STD_525_60;
- static const u8 config_60hz[] = {
- 0x05, 0x81,
- 0x07, 0x02,
- 0x08, 0x14,
- 0x09, 0xf0,
- 0, 0,
+ int ret;
+
+ static const struct reg_val config_60hz[] = {
+ {0x05, 0x81},
+ {0x07, 0x02},
+ {0x08, 0x14},
+ {0x09, 0xf0},
};
- static const u8 config_50hz[] = {
- 0x05, 0x01,
- 0x07, 0x12,
- 0x08, 0x18,
- 0x09, 0x20,
- 0, 0,
+ static const struct reg_val config_50hz[] = {
+ {0x05, 0x01},
+ {0x07, 0x12},
+ {0x08, 0x18},
+ {0x09, 0x20},
};

- write_regs(sd, is_60hz ? config_60hz : config_50hz);
+ if (is_60hz)
+ ret = write_regs(sd, config_60hz, ARRAY_SIZE(config_60hz));
+ else
+ ret = write_regs(sd, config_50hz, ARRAY_SIZE(config_50hz));
+
dec->norm = norm;
- return 0;
+ return ret;
}

static int tw9906_s_ctrl(struct v4l2_ctrl *ctrl)
@@ -195,7 +205,7 @@ static int tw9906_probe(struct i2c_client *client,
/* Initialize tw9906 */
dec->norm = V4L2_STD_NTSC;

- if (write_regs(sd, initial_registers) < 0) {
+ if (write_regs(sd, init_regs, ARRAY_SIZE(init_regs)) < 0) {
v4l2_err(client, "error initializing TW9906\n");
return -EINVAL;
}
--
2.30.2