linux-media.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jacopo Mondi <jacopo@jmondi.org>
To: linux-media@vger.kernel.org
Cc: mchehab@kernel.org, sakari.ailus@linux.intel.com,
	hverkuil@xs4all.nl, laurent.pinchart@ideasonboard.com,
	roman.kovalivskyi@globallogic.com,
	dafna.hirschfeld@collabora.com, dave.stevenson@raspberrypi.org,
	naush@raspberrypi.com, erosca@de.adit-jv.com,
	Jacopo Mondi <jacopo@jmondi.org>
Subject: [PATCH v4 14/30] media: ov5647: Break out format handling
Date: Thu, 19 Nov 2020 17:32:33 +0100	[thread overview]
Message-ID: <20201119163238.783142-5-jacopo@jmondi.org> (raw)
In-Reply-To: <20201119163238.783142-1-jacopo@jmondi.org>

Break format handling out from the main driver structure.

This commit prepares for the introduction of more sensor formats and
resolutions by instrumenting the existing operation to work on multiple
modes instead of assuming a single supported one.

Signed-off-by: Jacopo Mondi <jacopo@jmondi.org>
---
 drivers/media/i2c/ov5647.c | 91 +++++++++++++++++++++++++++-----------
 1 file changed, 65 insertions(+), 26 deletions(-)

diff --git a/drivers/media/i2c/ov5647.c b/drivers/media/i2c/ov5647.c
index 4c134865cd68d..9a6766410e0ee 100644
--- a/drivers/media/i2c/ov5647.c
+++ b/drivers/media/i2c/ov5647.c
@@ -84,18 +84,28 @@ struct regval_list {
 	u8 data;
 };
 
+struct ov5647_mode {
+	struct v4l2_mbus_framefmt	format;
+	const struct regval_list	*reg_list;
+	unsigned int			num_regs;
+};
+
+struct ov5647_format_list {
+	unsigned int			mbus_code;
+	const struct ov5647_mode	*modes;
+	unsigned int			num_modes;
+};
+
 struct ov5647 {
 	struct v4l2_subdev		sd;
 	struct media_pad		pad;
 	struct mutex			lock;
-	struct v4l2_mbus_framefmt	format;
-	unsigned int			width;
-	unsigned int			height;
 	int				power_count;
 	struct clk			*xclk;
 	struct gpio_desc		*pwdn;
 	bool				clock_ncont;
 	struct v4l2_ctrl_handler	ctrls;
+	const struct ov5647_mode	*mode;
 };
 
 static inline struct ov5647 *to_sensor(struct v4l2_subdev *sd)
@@ -115,7 +125,7 @@ static struct regval_list sensor_oe_enable_regs[] = {
 	{0x3002, 0xe4},
 };
 
-static struct regval_list ov5647_640x480[] = {
+static const struct regval_list ov5647_640x480[] = {
 	{0x0100, 0x00},
 	{0x0103, 0x01},
 	{0x3034, 0x08},
@@ -205,6 +215,33 @@ static struct regval_list ov5647_640x480[] = {
 	{0x0100, 0x01},
 };
 
+static const struct ov5647_mode ov5647_8bit_modes[] = {
+	{
+		.format	= {
+			.code		= MEDIA_BUS_FMT_SBGGR8_1X8,
+			.colorspace	= V4L2_COLORSPACE_SRGB,
+			.field		= V4L2_FIELD_NONE,
+			.width		= 640,
+			.height		= 480
+		},
+		.reg_list	= ov5647_640x480,
+		.num_regs	= ARRAY_SIZE(ov5647_640x480)
+	},
+};
+
+static const struct ov5647_format_list ov5647_formats[] = {
+	{
+		.mbus_code	= MEDIA_BUS_FMT_SBGGR8_1X8,
+		.modes		= ov5647_8bit_modes,
+		.num_modes	= ARRAY_SIZE(ov5647_8bit_modes),
+	},
+};
+
+#define OV5647_NUM_FORMATS	(ARRAY_SIZE(ov5647_formats))
+
+#define OV5647_DEFAULT_MODE	(&ov5647_formats[0].modes[0])
+#define OV5647_DEFAULT_FORMAT	(ov5647_formats[0].modes[0].format)
+
 static int ov5647_write(struct v4l2_subdev *sd, u16 reg, u8 val)
 {
 	unsigned char data[3] = { reg >> 8, reg & 0xff, val};
@@ -245,7 +282,7 @@ static int ov5647_read(struct v4l2_subdev *sd, u16 reg, u8 *val)
 }
 
 static int ov5647_write_array(struct v4l2_subdev *sd,
-			      struct regval_list *regs, int array_size)
+			      const struct regval_list *regs, int array_size)
 {
 	int i, ret;
 
@@ -276,6 +313,7 @@ static int ov5647_set_virtual_channel(struct v4l2_subdev *sd, int channel)
 static int ov5647_set_mode(struct v4l2_subdev *sd)
 {
 	struct i2c_client *client = v4l2_get_subdevdata(sd);
+	struct ov5647 *sensor = to_sensor(sd);
 	u8 resetval, rdval;
 	int ret;
 
@@ -283,8 +321,8 @@ static int ov5647_set_mode(struct v4l2_subdev *sd)
 	if (ret < 0)
 		return ret;
 
-	ret = ov5647_write_array(sd, ov5647_640x480,
-				 ARRAY_SIZE(ov5647_640x480));
+	ret = ov5647_write_array(sd, sensor->mode->reg_list,
+				 sensor->mode->num_regs);
 	if (ret < 0) {
 		dev_err(&client->dev, "write sensor default regs error\n");
 		return ret;
@@ -496,10 +534,10 @@ static int ov5647_enum_mbus_code(struct v4l2_subdev *sd,
 				 struct v4l2_subdev_pad_config *cfg,
 				 struct v4l2_subdev_mbus_code_enum *code)
 {
-	if (code->index > 0)
+	if (code->index >= OV5647_NUM_FORMATS)
 		return -EINVAL;
 
-	code->code = MEDIA_BUS_FMT_SBGGR8_1X8;
+	code->code = ov5647_formats[code->index].mbus_code;
 
 	return 0;
 }
@@ -508,16 +546,24 @@ static int ov5647_enum_frame_size(struct v4l2_subdev *sd,
 				  struct v4l2_subdev_pad_config *cfg,
 				  struct v4l2_subdev_frame_size_enum *fse)
 {
-	if (fse->index)
+	const struct v4l2_mbus_framefmt *fmt;
+	unsigned int i = 0;
+
+	for (; i < OV5647_NUM_FORMATS; ++i) {
+		if (ov5647_formats[i].mbus_code == fse->code)
+			break;
+	}
+	if (i == OV5647_NUM_FORMATS)
 		return -EINVAL;
 
-	if (fse->code != MEDIA_BUS_FMT_SBGGR8_1X8)
+	if (fse->index >= ov5647_formats[i].num_modes)
 		return -EINVAL;
 
-	fse->min_width = 640;
-	fse->max_width = 640;
-	fse->min_height = 480;
-	fse->max_height = 480;
+	fmt = &ov5647_formats[i].modes[fse->index].format;
+	fse->min_width = fmt->width;
+	fse->max_width = fmt->width;
+	fse->min_height = fmt->height;
+	fse->max_height = fmt->height;
 
 	return 0;
 }
@@ -529,12 +575,7 @@ static int ov5647_set_get_fmt(struct v4l2_subdev *sd,
 	struct v4l2_mbus_framefmt *fmt = &format->format;
 
 	/* Only one format is supported, so return that. */
-	memset(fmt, 0, sizeof(*fmt));
-	fmt->code = MEDIA_BUS_FMT_SBGGR8_1X8;
-	fmt->colorspace = V4L2_COLORSPACE_SRGB;
-	fmt->field = V4L2_FIELD_NONE;
-	fmt->width = 640;
-	fmt->height = 480;
+	*fmt = OV5647_DEFAULT_FORMAT;
 
 	return 0;
 }
@@ -594,11 +635,7 @@ static int ov5647_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
 	crop->width = OV5647_WINDOW_WIDTH_DEF;
 	crop->height = OV5647_WINDOW_HEIGHT_DEF;
 
-	format->code = MEDIA_BUS_FMT_SBGGR8_1X8;
-	format->width = 640;
-	format->height = 480;
-	format->field = V4L2_FIELD_NONE;
-	format->colorspace = V4L2_COLORSPACE_SRGB;
+	*format = OV5647_DEFAULT_FORMAT;
 
 	return 0;
 }
@@ -822,6 +859,8 @@ static int ov5647_probe(struct i2c_client *client)
 
 	mutex_init(&sensor->lock);
 
+	sensor->mode = OV5647_DEFAULT_MODE;
+
 	ret = ov5647_init_controls(sensor);
 	if (ret)
 		goto mutex_destroy;
-- 
2.29.1


  parent reply	other threads:[~2020-11-19 16:34 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-19 16:19 [PATCH v4 00/30] media: ov5647: Support RaspberryPi Camera Module v1 Jacopo Mondi
2020-11-19 16:19 ` [PATCH v4 01/30] dt-bindings: media: i2c: Rename ov5647.yaml Jacopo Mondi
2020-11-25  9:15   ` Sakari Ailus
2020-12-21 17:34     ` Jacopo Mondi
2020-12-22  9:43       ` Sakari Ailus
2020-12-21 20:23     ` Rob Herring
2020-12-22  9:50       ` Sakari Ailus
2021-01-14 20:20         ` Rob Herring
2021-01-15  9:03           ` Jacopo Mondi
2021-01-15  9:26             ` Sakari Ailus
2020-11-19 16:19 ` [PATCH v4 02/30] media: ov5647: Add support for PWDN GPIO Jacopo Mondi
2020-11-19 16:19 ` [PATCH v4 03/30] media: ov5647: Add support for non-continuous clock mode Jacopo Mondi
2020-11-19 16:19 ` [PATCH v4 04/30] media: ov5647: Add set_fmt and get_fmt calls Jacopo Mondi
2020-11-19 16:19 ` [PATCH v4 05/30] media: ov5647: Fix format initialization Jacopo Mondi
2020-11-19 16:19 ` [PATCH v4 06/30] media: ov5647: Fix style issues Jacopo Mondi
2020-11-19 16:19 ` [PATCH v4 07/30] media: ov5647: Replace license with SPDX identifier Jacopo Mondi
2020-11-19 16:19 ` [PATCH v4 08/30] media: ov5647: Fix return value from read/write Jacopo Mondi
2020-11-19 16:19 ` [PATCH v4 09/30] media: ov5647: Program mode at s_stream(1) time Jacopo Mondi
2020-11-19 16:32 ` [PATCH v4 10/30] media: ov5647: Implement enum_frame_size() Jacopo Mondi
2020-11-19 16:32   ` [PATCH v4 11/30] media: ov5647: Protect s_stream() with mutex Jacopo Mondi
2020-11-19 16:32   ` [PATCH v4 12/30] media: ov5647: Support gain, exposure and AWB controls Jacopo Mondi
2020-11-19 16:32   ` [PATCH v4 13/30] media: ov5647: Rationalize driver structure name Jacopo Mondi
2020-11-19 16:32   ` Jacopo Mondi [this message]
2020-11-19 16:32   ` [PATCH v4 15/30] media: ov5647: Add support for get_selection() Jacopo Mondi
2020-11-19 16:32   ` [PATCH v4 16/30] media: ov5647: Rename SBGGR8 VGA mode Jacopo Mondi
2020-11-19 16:32   ` [PATCH v4 17/30] media: ov5647: Add SGGBR10_1X10 modes Jacopo Mondi
2020-11-19 16:32   ` [PATCH v4 18/30] media: ov5647: Use SBGGR10_1X10 640x480 as default Jacopo Mondi
2020-11-19 16:32   ` [PATCH v4 19/30] media: ov5647: Implement set_fmt pad operation Jacopo Mondi
2020-11-19 16:35 ` [PATCH v4 20/30] media: ov5647: Set V4L2_SUBDEV_FL_HAS_EVENTS flag Jacopo Mondi
2020-11-19 16:35   ` [PATCH v4 21/30] media: ov5647: Support V4L2_CID_PIXEL_RATE Jacopo Mondi
2020-11-19 16:35   ` [PATCH v4 22/30] media: ov5647: Support V4L2_CID_HBLANK control Jacopo Mondi
2020-11-19 16:35   ` [PATCH v4 23/30] media: ov5647: Support V4L2_CID_VBLANK control Jacopo Mondi
2020-11-19 16:35   ` [PATCH v4 24/30] media: ov5647: Advertise the correct exposure range Jacopo Mondi
2020-11-19 16:35   ` [PATCH v4 25/30] media: ov5647: Use pm_runtime infrastructure Jacopo Mondi
2020-11-19 16:35   ` [PATCH v4 26/30] media: ov5647: Rework s_stream() operation Jacopo Mondi
2020-11-19 16:35   ` [PATCH v4 27/30] media: ov5647: Apply controls only when powered Jacopo Mondi
2020-11-19 16:35   ` [PATCH v4 28/30] media: ov5647: Constify oe_enable/disable reglist Jacopo Mondi
2020-11-19 16:35   ` [PATCH v4 29/30] media: ov5647: Support VIDIOC_SUBSCRIBE_EVENT Jacopo Mondi
2020-11-19 16:37 ` [PATCH v4 30/30] media: ov5647: Remove 640x480 SBGGR8 mode Jacopo Mondi

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=20201119163238.783142-5-jacopo@jmondi.org \
    --to=jacopo@jmondi.org \
    --cc=dafna.hirschfeld@collabora.com \
    --cc=dave.stevenson@raspberrypi.org \
    --cc=erosca@de.adit-jv.com \
    --cc=hverkuil@xs4all.nl \
    --cc=laurent.pinchart@ideasonboard.com \
    --cc=linux-media@vger.kernel.org \
    --cc=mchehab@kernel.org \
    --cc=naush@raspberrypi.com \
    --cc=roman.kovalivskyi@globallogic.com \
    --cc=sakari.ailus@linux.intel.com \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).