From: Moses Christopher Bollavarapu <mosescb.dev@gmail.com>
To: mchehab@kernel.org, linux-media@vger.kernel.org,
linux-kernel@vger.kernel.org
Cc: Moses Christopher Bollavarapu <mosescb.dev@gmail.com>
Subject: [PATCH] media: i2c: tw2804: Use ARRAY_SIZE instead of manual checking
Date: Sun, 17 Apr 2022 14:09:59 +0200 [thread overview]
Message-ID: <20220417120959.16977-1-mosescb.dev@gmail.com> (raw)
this driver currently uses a terminator(0xFF, 0xFF) to check for the
end of a reg-val list, 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 <mosescb.dev@gmail.com>
---
drivers/media/i2c/tw2804.c | 177 +++++++++++++++++++------------------
1 file changed, 91 insertions(+), 86 deletions(-)
diff --git a/drivers/media/i2c/tw2804.c b/drivers/media/i2c/tw2804.c
index cd05f1ff504d..87842d178361 100644
--- a/drivers/media/i2c/tw2804.c
+++ b/drivers/media/i2c/tw2804.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>
@@ -32,75 +33,77 @@ struct tw2804 {
int norm;
};
-static const u8 global_registers[] = {
- 0x39, 0x00,
- 0x3a, 0xff,
- 0x3b, 0x84,
- 0x3c, 0x80,
- 0x3d, 0x80,
- 0x3e, 0x82,
- 0x3f, 0x82,
- 0x78, 0x00,
- 0xff, 0xff, /* Terminator (reg 0xff does not exist) */
+struct reg_val {
+ u8 reg;
+ u8 val;
+};
+static const struct reg_val global_registers[] = {
+ {0x39, 0x00},
+ {0x3a, 0xff},
+ {0x3b, 0x84},
+ {0x3c, 0x80},
+ {0x3d, 0x80},
+ {0x3e, 0x82},
+ {0x3f, 0x82},
+ {0x78, 0x00},
};
-static const u8 channel_registers[] = {
- 0x01, 0xc4,
- 0x02, 0xa5,
- 0x03, 0x20,
- 0x04, 0xd0,
- 0x05, 0x20,
- 0x06, 0xd0,
- 0x07, 0x88,
- 0x08, 0x20,
- 0x09, 0x07,
- 0x0a, 0xf0,
- 0x0b, 0x07,
- 0x0c, 0xf0,
- 0x0d, 0x40,
- 0x0e, 0xd2,
- 0x0f, 0x80,
- 0x10, 0x80,
- 0x11, 0x80,
- 0x12, 0x80,
- 0x13, 0x1f,
- 0x14, 0x00,
- 0x15, 0x00,
- 0x16, 0x00,
- 0x17, 0x00,
- 0x18, 0xff,
- 0x19, 0xff,
- 0x1a, 0xff,
- 0x1b, 0xff,
- 0x1c, 0xff,
- 0x1d, 0xff,
- 0x1e, 0xff,
- 0x1f, 0xff,
- 0x20, 0x07,
- 0x21, 0x07,
- 0x22, 0x00,
- 0x23, 0x91,
- 0x24, 0x51,
- 0x25, 0x03,
- 0x26, 0x00,
- 0x27, 0x00,
- 0x28, 0x00,
- 0x29, 0x00,
- 0x2a, 0x00,
- 0x2b, 0x00,
- 0x2c, 0x00,
- 0x2d, 0x00,
- 0x2e, 0x00,
- 0x2f, 0x00,
- 0x30, 0x00,
- 0x31, 0x00,
- 0x32, 0x00,
- 0x33, 0x00,
- 0x34, 0x00,
- 0x35, 0x00,
- 0x36, 0x00,
- 0x37, 0x00,
- 0xff, 0xff, /* Terminator (reg 0xff does not exist) */
+static const struct reg_val channel_registers[] = {
+ {0x01, 0xc4},
+ {0x02, 0xa5},
+ {0x03, 0x20},
+ {0x04, 0xd0},
+ {0x05, 0x20},
+ {0x06, 0xd0},
+ {0x07, 0x88},
+ {0x08, 0x20},
+ {0x09, 0x07},
+ {0x0a, 0xf0},
+ {0x0b, 0x07},
+ {0x0c, 0xf0},
+ {0x0d, 0x40},
+ {0x0e, 0xd2},
+ {0x0f, 0x80},
+ {0x10, 0x80},
+ {0x11, 0x80},
+ {0x12, 0x80},
+ {0x13, 0x1f},
+ {0x14, 0x00},
+ {0x15, 0x00},
+ {0x16, 0x00},
+ {0x17, 0x00},
+ {0x18, 0xff},
+ {0x19, 0xff},
+ {0x1a, 0xff},
+ {0x1b, 0xff},
+ {0x1c, 0xff},
+ {0x1d, 0xff},
+ {0x1e, 0xff},
+ {0x1f, 0xff},
+ {0x20, 0x07},
+ {0x21, 0x07},
+ {0x22, 0x00},
+ {0x23, 0x91},
+ {0x24, 0x51},
+ {0x25, 0x03},
+ {0x26, 0x00},
+ {0x27, 0x00},
+ {0x28, 0x00},
+ {0x29, 0x00},
+ {0x2a, 0x00},
+ {0x2b, 0x00},
+ {0x2c, 0x00},
+ {0x2d, 0x00},
+ {0x2e, 0x00},
+ {0x2f, 0x00},
+ {0x30, 0x00},
+ {0x31, 0x00},
+ {0x32, 0x00},
+ {0x33, 0x00},
+ {0x34, 0x00},
+ {0x35, 0x00},
+ {0x36, 0x00},
+ {0x37, 0x00},
};
static int write_reg(struct i2c_client *client, u8 reg, u8 value, u8 channel)
@@ -108,16 +111,17 @@ static int write_reg(struct i2c_client *client, u8 reg, u8 value, u8 channel)
return i2c_smbus_write_byte_data(client, reg | (channel << 6), value);
}
-static int write_regs(struct i2c_client *client, const u8 *regs, u8 channel)
+static int write_regs(struct i2c_client *client, const struct reg_val *rv,
+ int len, u8 channel)
{
int ret;
- int i;
- for (i = 0; regs[i] != 0xff; i += 2) {
+ while (--len >= 0) {
ret = i2c_smbus_write_byte_data(client,
- regs[i] | (channel << 6), regs[i + 1]);
+ rv->reg | (channel << 6), rv->val);
if (ret < 0)
return ret;
+ rv++;
}
return 0;
}
@@ -252,21 +256,20 @@ static int tw2804_s_std(struct v4l2_subdev *sd, v4l2_std_id norm)
struct tw2804 *dec = to_state(sd);
struct i2c_client *client = v4l2_get_subdevdata(sd);
bool is_60hz = norm & V4L2_STD_525_60;
- u8 regs[] = {
- 0x01, is_60hz ? 0xc4 : 0x84,
- 0x09, is_60hz ? 0x07 : 0x04,
- 0x0a, is_60hz ? 0xf0 : 0x20,
- 0x0b, is_60hz ? 0x07 : 0x04,
- 0x0c, is_60hz ? 0xf0 : 0x20,
- 0x0d, is_60hz ? 0x40 : 0x4a,
- 0x16, is_60hz ? 0x00 : 0x40,
- 0x17, is_60hz ? 0x00 : 0x40,
- 0x20, is_60hz ? 0x07 : 0x0f,
- 0x21, is_60hz ? 0x07 : 0x0f,
- 0xff, 0xff,
+ struct reg_val regs[] = {
+ {0x01, is_60hz ? 0xc4 : 0x84},
+ {0x09, is_60hz ? 0x07 : 0x04},
+ {0x0a, is_60hz ? 0xf0 : 0x20},
+ {0x0b, is_60hz ? 0x07 : 0x04},
+ {0x0c, is_60hz ? 0xf0 : 0x20},
+ {0x0d, is_60hz ? 0x40 : 0x4a},
+ {0x16, is_60hz ? 0x00 : 0x40},
+ {0x17, is_60hz ? 0x00 : 0x40},
+ {0x20, is_60hz ? 0x07 : 0x0f},
+ {0x21, is_60hz ? 0x07 : 0x0f},
};
- write_regs(client, regs, dec->channel);
+ write_regs(client, regs, ARRAY_SIZE(regs), dec->channel);
dec->norm = norm;
return 0;
}
@@ -288,12 +291,14 @@ static int tw2804_s_video_routing(struct v4l2_subdev *sd, u32 input, u32 output,
dev_dbg(&client->dev, "initializing TW2804 channel %d\n",
dec->channel);
if (dec->channel == 0 &&
- write_regs(client, global_registers, 0) < 0) {
+ write_regs(client, global_registers,
+ ARRAY_SIZE(global_registers), 0) < 0) {
dev_err(&client->dev,
"error initializing TW2804 global registers\n");
return -EIO;
}
- if (write_regs(client, channel_registers, dec->channel) < 0) {
+ if (write_regs(client, channel_registers,
+ ARRAY_SIZE(channel_registers), dec->channel) < 0) {
dev_err(&client->dev,
"error initializing TW2804 channel %d\n",
dec->channel);
--
2.30.2
reply other threads:[~2022-04-17 12:10 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220417120959.16977-1-mosescb.dev@gmail.com \
--to=mosescb.dev@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-media@vger.kernel.org \
--cc=mchehab@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.