public inbox for linux-media@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH 00/28] media: ov2680: Bugfixes + ACPI + selection(crop-tgt) API support
@ 2023-06-07 16:46 Hans de Goede
  2023-06-07 16:46 ` [PATCH 01/28] media: ov2680: Remove auto-gain and auto-exposure controls Hans de Goede
                   ` (28 more replies)
  0 siblings, 29 replies; 57+ messages in thread
From: Hans de Goede @ 2023-06-07 16:46 UTC (permalink / raw)
  To: Daniel Scally, Laurent Pinchart, Sakari Ailus, Andy Shevchenko
  Cc: Hans de Goede, Mauro Carvalho Chehab, Kate Hsuan, linux-media

Hi All,

During all the work done on the atomisp driver I have mostly been testing
on devices with an ov2680 sensor. As such I have also done a lot of work
on the atomisp-ov2680.c atomisp specific sensor driver.

With the latest atomisp code from:
https://git.kernel.org/pub/scm/linux/kernel/git/hansg/linux.git/tag/?h=media-atomisp-6.5-1

The atomisp code can now work with standard v4l2 sensor drivers using
the selections (crop-tgt) api and v4l2-async sensor driver registration.

This patch series modifies the main drivers/media/i2c/ov2680.c driver
to add bugfixes, ACPI enumeration, selection API support and further
improvments. After this the driver can be used with the atomisp driver
and atomisp-ov2680.c can be dropped.

This also gets the driver much closer to having everything needed for
use with IPU3 / libcamera. I have a Lenovo Miix 510 now with an IPU3 +
ov2680 sensor and I plan to work on this soonish.

This series consist of 3 parts:

1. Patches 0-7 are bugfixes these are put first for backporting

2.1 Patch 8 adds the new CCI register helpers, these are being
reviewed here:
https://lore.kernel.org/linux-media/20230606165808.70751-2-hdegoede@redhat.com/

the intend is to merge that patch separately before this series.
This patch is only here so that the series actually compiles.

2.2 Patch 9 converts the ov2680 driver to the new CCI helpers,
the same has been done in the other series with the atomisp-ov2680
driver and this makes it much easier to sync things up

3. Patches 10 - 28 implement the ACPI enumeration,
selection API support and further improvments.

Regards,

Hans


Hans de Goede (28):
  media: ov2680: Remove auto-gain and auto-exposure controls
  media: ov2680: Fix ov2680_bayer_order()
  media: ov2680: Fix vflip / hflip set functions
  media: ov2680: Use select VIDEO_V4L2_SUBDEV_API
  media: ov2680: Don't take the lock for try_fmt calls
  media: ov2680: Add ov2680_fill_format() helper function
  media: ov2680: Fix ov2680_set_fmt() which == V4L2_SUBDEV_FORMAT_TRY
    not working
  media: Add MIPI CCI register access helper functions
  media: ov2680: Convert to new CCI register access helpers
  media: ov2680: Store dev instead of i2c_client in ov2680_dev
  media: ov2680: Check for "powerdown" GPIO con-id before checking for
    "reset" GPIO con-id
  media: ov2680: Add runtime-pm support
  media: ov2680: Drop is_enabled flag
  media: ov2680: Add support for more clk setups
  media: ov2680: Add support for 19.2 MHz clock
  media: ov2680: Add endpoint matching support
  media: ov2680: Add support for ACPI enumeration
  media: ov2680: Fix ov2680_enum_frame_interval()
  media: ov2680: Annotate the per mode register setting lists
  media: ov2680: Add ov2680_mode struct
  media: ov2680: Make setting the mode algorithm based
  media: ov2680: Add an __ov2680_get_pad_format() helper function
  media: ov2680: Implement selection support
  media: ov2680: Fix exposure and gain ctrls range and default value
  media: ov2680: Add a bunch of register tweaks
  media: ov2680: Add g_skip_frames op support
  media: ov2680: Drop unnecessary pad checks
  media: ov2680: Read and log sensor revision during probe

 Documentation/driver-api/media/v4l2-cci.rst  |    5 +
 Documentation/driver-api/media/v4l2-core.rst |    1 +
 drivers/media/i2c/Kconfig                    |    2 +
 drivers/media/i2c/ov2680.c                   | 1251 +++++++++---------
 drivers/media/v4l2-core/Kconfig              |    5 +
 drivers/media/v4l2-core/Makefile             |    1 +
 drivers/media/v4l2-core/v4l2-cci.c           |  142 ++
 include/media/v4l2-cci.h                     |  109 ++
 8 files changed, 889 insertions(+), 627 deletions(-)
 create mode 100644 Documentation/driver-api/media/v4l2-cci.rst
 create mode 100644 drivers/media/v4l2-core/v4l2-cci.c
 create mode 100644 include/media/v4l2-cci.h

-- 
2.40.1


^ permalink raw reply	[flat|nested] 57+ messages in thread

* [PATCH 01/28] media: ov2680: Remove auto-gain and auto-exposure controls
  2023-06-07 16:46 [PATCH 00/28] media: ov2680: Bugfixes + ACPI + selection(crop-tgt) API support Hans de Goede
@ 2023-06-07 16:46 ` Hans de Goede
  2023-06-08 12:41   ` Sakari Ailus
  2023-06-12  6:53   ` Dan Scally
  2023-06-07 16:46 ` [PATCH 02/28] media: ov2680: Fix ov2680_bayer_order() Hans de Goede
                   ` (27 subsequent siblings)
  28 siblings, 2 replies; 57+ messages in thread
From: Hans de Goede @ 2023-06-07 16:46 UTC (permalink / raw)
  To: Daniel Scally, Laurent Pinchart, Sakari Ailus, Andy Shevchenko
  Cc: Hans de Goede, Mauro Carvalho Chehab, Kate Hsuan, linux-media

Quoting the OV2680 datasheet:

"3.2 exposure and gain control

In the OV2680, the exposure time and gain are set manually from an external
controller. The OV2680 supports manual gain and exposure control only for
normal applications, no auto mode."

And indeed testing with the atomisp_ov2680 fork of ov2680.c has shown that
auto-exposure and auto-gain do not work.

Note that the code setting the auto-exposure flag was broken, callers
of ov2680_exposure_set() were directly passing !!ctrls->auto_exp->val as
"bool auto_exp" value, but ctrls->auto_exp is a menu control with:

enum  v4l2_exposure_auto_type {
        V4L2_EXPOSURE_AUTO = 0,
        V4L2_EXPOSURE_MANUAL = 1,
	...

So instead of passing !!ctrls->auto_exp->val they should have been passing
ctrls->auto_exp->val == V4L2_EXPOSURE_AUTO, iow the passed value was
inverted of what it should have been.

Also remove ov2680_g_volatile_ctrl() since without auto support the gain
and exposure controls are not volatile.

This also fixes the control values not being properly applied in
ov2680_mode_set(). The 800x600 mode register-list also sets gain,
exposure and vflip overriding the last set ctrl values.

ov2680_mode_set() does call ov2680_gain_set() and ov2680_exposure_set()
but did this before writing the mode register-list, so these values
would still be overridden by the mode register-list.

Add a v4l2_ctrl_handler_setup() call after writing the mode register-list
to restore all ctrl values. Also remove the ctrls->gain->is_new check from
ov2680_gain_set() so that the gain always gets restored properly.

Last since ov2680_mode_set() now calls v4l2_ctrl_handler_setup(), remove
the v4l2_ctrl_handler_setup() call after ov2680_mode_restore() since
ov2680_mode_restore() calls ov2680_mode_set().

Fixes: 3ee47cad3e69 ("media: ov2680: Add Omnivision OV2680 sensor driver")
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
---
 drivers/media/i2c/ov2680.c | 162 ++++---------------------------------
 1 file changed, 17 insertions(+), 145 deletions(-)

diff --git a/drivers/media/i2c/ov2680.c b/drivers/media/i2c/ov2680.c
index 54153bf66bdd..02204e185e2f 100644
--- a/drivers/media/i2c/ov2680.c
+++ b/drivers/media/i2c/ov2680.c
@@ -85,15 +85,8 @@ struct ov2680_mode_info {
 
 struct ov2680_ctrls {
 	struct v4l2_ctrl_handler handler;
-	struct {
-		struct v4l2_ctrl *auto_exp;
-		struct v4l2_ctrl *exposure;
-	};
-	struct {
-		struct v4l2_ctrl *auto_gain;
-		struct v4l2_ctrl *gain;
-	};
-
+	struct v4l2_ctrl *exposure;
+	struct v4l2_ctrl *gain;
 	struct v4l2_ctrl *hflip;
 	struct v4l2_ctrl *vflip;
 	struct v4l2_ctrl *test_pattern;
@@ -143,6 +136,7 @@ static const struct reg_value ov2680_setting_30fps_QUXGA_800_600[] = {
 	{0x380e, 0x02}, {0x380f, 0x84}, {0x3811, 0x04}, {0x3813, 0x04},
 	{0x3814, 0x31}, {0x3815, 0x31}, {0x3820, 0xc0}, {0x4008, 0x00},
 	{0x4009, 0x03}, {0x4837, 0x1e}, {0x3501, 0x4e}, {0x3502, 0xe0},
+	{0x3503, 0x03},
 };
 
 static const struct reg_value ov2680_setting_30fps_720P_1280_720[] = {
@@ -405,69 +399,14 @@ static int ov2680_test_pattern_set(struct ov2680_dev *sensor, int value)
 	return 0;
 }
 
-static int ov2680_gain_set(struct ov2680_dev *sensor, bool auto_gain)
+static int ov2680_gain_set(struct ov2680_dev *sensor, u32 gain)
 {
-	struct ov2680_ctrls *ctrls = &sensor->ctrls;
-	u32 gain;
-	int ret;
-
-	ret = ov2680_mod_reg(sensor, OV2680_REG_R_MANUAL, BIT(1),
-			     auto_gain ? 0 : BIT(1));
-	if (ret < 0)
-		return ret;
-
-	if (auto_gain || !ctrls->gain->is_new)
-		return 0;
-
-	gain = ctrls->gain->val;
-
-	ret = ov2680_write_reg16(sensor, OV2680_REG_GAIN_PK, gain);
-
-	return 0;
+	return ov2680_write_reg16(sensor, OV2680_REG_GAIN_PK, gain);
 }
 
-static int ov2680_gain_get(struct ov2680_dev *sensor)
+static int ov2680_exposure_set(struct ov2680_dev *sensor, u32 exp)
 {
-	u32 gain;
-	int ret;
-
-	ret = ov2680_read_reg16(sensor, OV2680_REG_GAIN_PK, &gain);
-	if (ret)
-		return ret;
-
-	return gain;
-}
-
-static int ov2680_exposure_set(struct ov2680_dev *sensor, bool auto_exp)
-{
-	struct ov2680_ctrls *ctrls = &sensor->ctrls;
-	u32 exp;
-	int ret;
-
-	ret = ov2680_mod_reg(sensor, OV2680_REG_R_MANUAL, BIT(0),
-			     auto_exp ? 0 : BIT(0));
-	if (ret < 0)
-		return ret;
-
-	if (auto_exp || !ctrls->exposure->is_new)
-		return 0;
-
-	exp = (u32)ctrls->exposure->val;
-	exp <<= 4;
-
-	return ov2680_write_reg24(sensor, OV2680_REG_EXPOSURE_PK_HIGH, exp);
-}
-
-static int ov2680_exposure_get(struct ov2680_dev *sensor)
-{
-	int ret;
-	u32 exp;
-
-	ret = ov2680_read_reg24(sensor, OV2680_REG_EXPOSURE_PK_HIGH, &exp);
-	if (ret)
-		return ret;
-
-	return exp >> 4;
+	return ov2680_write_reg24(sensor, OV2680_REG_EXPOSURE_PK_HIGH, exp << 4);
 }
 
 static int ov2680_stream_enable(struct ov2680_dev *sensor)
@@ -482,32 +421,16 @@ static int ov2680_stream_disable(struct ov2680_dev *sensor)
 
 static int ov2680_mode_set(struct ov2680_dev *sensor)
 {
-	struct ov2680_ctrls *ctrls = &sensor->ctrls;
 	int ret;
 
-	ret = ov2680_gain_set(sensor, false);
-	if (ret < 0)
-		return ret;
-
-	ret = ov2680_exposure_set(sensor, false);
-	if (ret < 0)
-		return ret;
-
 	ret = ov2680_load_regs(sensor, sensor->current_mode);
 	if (ret < 0)
 		return ret;
 
-	if (ctrls->auto_gain->val) {
-		ret = ov2680_gain_set(sensor, true);
-		if (ret < 0)
-			return ret;
-	}
-
-	if (ctrls->auto_exp->val == V4L2_EXPOSURE_AUTO) {
-		ret = ov2680_exposure_set(sensor, true);
-		if (ret < 0)
-			return ret;
-	}
+	/* Restore value of all ctrls */
+	ret = __v4l2_ctrl_handler_setup(&sensor->ctrls.handler);
+	if (ret < 0)
+		return ret;
 
 	sensor->mode_pending_changes = false;
 
@@ -590,15 +513,10 @@ static int ov2680_s_power(struct v4l2_subdev *sd, int on)
 	else
 		ret = ov2680_power_off(sensor);
 
-	mutex_unlock(&sensor->lock);
-
-	if (on && ret == 0) {
-		ret = v4l2_ctrl_handler_setup(&sensor->ctrls.handler);
-		if (ret < 0)
-			return ret;
-
+	if (on && ret == 0)
 		ret = ov2680_mode_restore(sensor);
-	}
+
+	mutex_unlock(&sensor->lock);
 
 	return ret;
 }
@@ -794,52 +712,19 @@ static int ov2680_enum_frame_interval(struct v4l2_subdev *sd,
 	return 0;
 }
 
-static int ov2680_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
-{
-	struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
-	struct ov2680_dev *sensor = to_ov2680_dev(sd);
-	struct ov2680_ctrls *ctrls = &sensor->ctrls;
-	int val;
-
-	if (!sensor->is_enabled)
-		return 0;
-
-	switch (ctrl->id) {
-	case V4L2_CID_GAIN:
-		val = ov2680_gain_get(sensor);
-		if (val < 0)
-			return val;
-		ctrls->gain->val = val;
-		break;
-	case V4L2_CID_EXPOSURE:
-		val = ov2680_exposure_get(sensor);
-		if (val < 0)
-			return val;
-		ctrls->exposure->val = val;
-		break;
-	}
-
-	return 0;
-}
-
 static int ov2680_s_ctrl(struct v4l2_ctrl *ctrl)
 {
 	struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
 	struct ov2680_dev *sensor = to_ov2680_dev(sd);
-	struct ov2680_ctrls *ctrls = &sensor->ctrls;
 
 	if (!sensor->is_enabled)
 		return 0;
 
 	switch (ctrl->id) {
-	case V4L2_CID_AUTOGAIN:
-		return ov2680_gain_set(sensor, !!ctrl->val);
 	case V4L2_CID_GAIN:
-		return ov2680_gain_set(sensor, !!ctrls->auto_gain->val);
-	case V4L2_CID_EXPOSURE_AUTO:
-		return ov2680_exposure_set(sensor, !!ctrl->val);
+		return ov2680_gain_set(sensor, ctrl->val);
 	case V4L2_CID_EXPOSURE:
-		return ov2680_exposure_set(sensor, !!ctrls->auto_exp->val);
+		return ov2680_exposure_set(sensor, ctrl->val);
 	case V4L2_CID_VFLIP:
 		if (sensor->is_streaming)
 			return -EBUSY;
@@ -864,7 +749,6 @@ static int ov2680_s_ctrl(struct v4l2_ctrl *ctrl)
 }
 
 static const struct v4l2_ctrl_ops ov2680_ctrl_ops = {
-	.g_volatile_ctrl = ov2680_g_volatile_ctrl,
 	.s_ctrl = ov2680_s_ctrl,
 };
 
@@ -936,7 +820,7 @@ static int ov2680_v4l2_register(struct ov2680_dev *sensor)
 	if (ret < 0)
 		return ret;
 
-	v4l2_ctrl_handler_init(hdl, 7);
+	v4l2_ctrl_handler_init(hdl, 5);
 
 	hdl->lock = &sensor->lock;
 
@@ -948,16 +832,9 @@ static int ov2680_v4l2_register(struct ov2680_dev *sensor)
 					ARRAY_SIZE(test_pattern_menu) - 1,
 					0, 0, test_pattern_menu);
 
-	ctrls->auto_exp = v4l2_ctrl_new_std_menu(hdl, ops,
-						 V4L2_CID_EXPOSURE_AUTO,
-						 V4L2_EXPOSURE_MANUAL, 0,
-						 V4L2_EXPOSURE_AUTO);
-
 	ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE,
 					    0, 32767, 1, 0);
 
-	ctrls->auto_gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_AUTOGAIN,
-					     0, 1, 1, 1);
 	ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN, 0, 2047, 1, 0);
 
 	if (hdl->error) {
@@ -965,14 +842,9 @@ static int ov2680_v4l2_register(struct ov2680_dev *sensor)
 		goto cleanup_entity;
 	}
 
-	ctrls->gain->flags |= V4L2_CTRL_FLAG_VOLATILE;
-	ctrls->exposure->flags |= V4L2_CTRL_FLAG_VOLATILE;
 	ctrls->vflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
 	ctrls->hflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
 
-	v4l2_ctrl_auto_cluster(2, &ctrls->auto_gain, 0, true);
-	v4l2_ctrl_auto_cluster(2, &ctrls->auto_exp, 1, true);
-
 	sensor->sd.ctrl_handler = hdl;
 
 	ret = v4l2_async_register_subdev(&sensor->sd);
-- 
2.40.1


^ permalink raw reply related	[flat|nested] 57+ messages in thread

* [PATCH 02/28] media: ov2680: Fix ov2680_bayer_order()
  2023-06-07 16:46 [PATCH 00/28] media: ov2680: Bugfixes + ACPI + selection(crop-tgt) API support Hans de Goede
  2023-06-07 16:46 ` [PATCH 01/28] media: ov2680: Remove auto-gain and auto-exposure controls Hans de Goede
@ 2023-06-07 16:46 ` Hans de Goede
  2023-06-12  7:22   ` Dan Scally
  2023-06-07 16:46 ` [PATCH 03/28] media: ov2680: Fix vflip / hflip set functions Hans de Goede
                   ` (26 subsequent siblings)
  28 siblings, 1 reply; 57+ messages in thread
From: Hans de Goede @ 2023-06-07 16:46 UTC (permalink / raw)
  To: Daniel Scally, Laurent Pinchart, Sakari Ailus, Andy Shevchenko
  Cc: Hans de Goede, Mauro Carvalho Chehab, Kate Hsuan, linux-media

The index into ov2680_hv_flip_bayer_order[] should be 0-3, but
ov2680_bayer_order() was using 0 + BIT(2) + (BIT(2) << 1) as
max index, while the intention was to use: 0 + 1 + 2 as max index.

Fix the index calculation in ov2680_bayer_order(), while at it
also just use the ctrl values rather then reading them back using
a slow i2c-read transaction.

This also allows making the function void, since there now are
no more i2c-reads to error check.

Note the check for the ctrls being NULL is there to allow
adding an ov2680_fill_format() helper later, which will call
ov2680_set_bayer_order() during probe() before the ctrls are created.

Fixes: 3ee47cad3e69 ("media: ov2680: Add Omnivision OV2680 sensor driver")
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
---
 drivers/media/i2c/ov2680.c | 27 ++++++++++-----------------
 1 file changed, 10 insertions(+), 17 deletions(-)

diff --git a/drivers/media/i2c/ov2680.c b/drivers/media/i2c/ov2680.c
index 02204e185e2f..9a9c90c8a949 100644
--- a/drivers/media/i2c/ov2680.c
+++ b/drivers/media/i2c/ov2680.c
@@ -315,26 +315,17 @@ static void ov2680_power_down(struct ov2680_dev *sensor)
 	usleep_range(5000, 10000);
 }
 
-static int ov2680_bayer_order(struct ov2680_dev *sensor)
+static void ov2680_set_bayer_order(struct ov2680_dev *sensor)
 {
-	u32 format1;
-	u32 format2;
-	u32 hv_flip;
-	int ret;
+	int hv_flip = 0;
 
-	ret = ov2680_read_reg(sensor, OV2680_REG_FORMAT1, &format1);
-	if (ret < 0)
-		return ret;
+	if (sensor->ctrls.vflip && sensor->ctrls.vflip->val)
+		hv_flip += 1;
 
-	ret = ov2680_read_reg(sensor, OV2680_REG_FORMAT2, &format2);
-	if (ret < 0)
-		return ret;
-
-	hv_flip = (format2 & BIT(2)  << 1) | (format1 & BIT(2));
+	if (sensor->ctrls.hflip && sensor->ctrls.hflip->val)
+		hv_flip += 2;
 
 	sensor->fmt.code = ov2680_hv_flip_bayer_order[hv_flip];
-
-	return 0;
 }
 
 static int ov2680_vflip_enable(struct ov2680_dev *sensor)
@@ -345,7 +336,8 @@ static int ov2680_vflip_enable(struct ov2680_dev *sensor)
 	if (ret < 0)
 		return ret;
 
-	return ov2680_bayer_order(sensor);
+	ov2680_set_bayer_order(sensor);
+	return 0;
 }
 
 static int ov2680_vflip_disable(struct ov2680_dev *sensor)
@@ -378,7 +370,8 @@ static int ov2680_hflip_disable(struct ov2680_dev *sensor)
 	if (ret < 0)
 		return ret;
 
-	return ov2680_bayer_order(sensor);
+	ov2680_set_bayer_order(sensor);
+	return 0;
 }
 
 static int ov2680_test_pattern_set(struct ov2680_dev *sensor, int value)
-- 
2.40.1


^ permalink raw reply related	[flat|nested] 57+ messages in thread

* [PATCH 03/28] media: ov2680: Fix vflip / hflip set functions
  2023-06-07 16:46 [PATCH 00/28] media: ov2680: Bugfixes + ACPI + selection(crop-tgt) API support Hans de Goede
  2023-06-07 16:46 ` [PATCH 01/28] media: ov2680: Remove auto-gain and auto-exposure controls Hans de Goede
  2023-06-07 16:46 ` [PATCH 02/28] media: ov2680: Fix ov2680_bayer_order() Hans de Goede
@ 2023-06-07 16:46 ` Hans de Goede
  2023-06-12  7:32   ` Dan Scally
  2023-06-07 16:46 ` [PATCH 04/28] media: ov2680: Use select VIDEO_V4L2_SUBDEV_API Hans de Goede
                   ` (25 subsequent siblings)
  28 siblings, 1 reply; 57+ messages in thread
From: Hans de Goede @ 2023-06-07 16:46 UTC (permalink / raw)
  To: Daniel Scally, Laurent Pinchart, Sakari Ailus, Andy Shevchenko
  Cc: Hans de Goede, Mauro Carvalho Chehab, Kate Hsuan, linux-media

ov2680_vflip_disable() / ov2680_hflip_disable() pass BIT(0) instead of
0 as value to ov2680_mod_reg().

While fixing this also:

1. Stop having separate enable/disable functions for hflip / vflip
2. Move the is_streaming check, which is unique to hflip / vflip
   into the ov2680_set_?flip() functions.

for a nice code cleanup.

Fixes: 3ee47cad3e69 ("media: ov2680: Add Omnivision OV2680 sensor driver")
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
---
 drivers/media/i2c/ov2680.c | 48 +++++++++-----------------------------
 1 file changed, 11 insertions(+), 37 deletions(-)

diff --git a/drivers/media/i2c/ov2680.c b/drivers/media/i2c/ov2680.c
index 9a9c90c8a949..c1b23c5b7818 100644
--- a/drivers/media/i2c/ov2680.c
+++ b/drivers/media/i2c/ov2680.c
@@ -328,11 +328,14 @@ static void ov2680_set_bayer_order(struct ov2680_dev *sensor)
 	sensor->fmt.code = ov2680_hv_flip_bayer_order[hv_flip];
 }
 
-static int ov2680_vflip_enable(struct ov2680_dev *sensor)
+static int ov2680_set_vflip(struct ov2680_dev *sensor, s32 val)
 {
 	int ret;
 
-	ret = ov2680_mod_reg(sensor, OV2680_REG_FORMAT1, BIT(2), BIT(2));
+	if (sensor->is_streaming)
+		return -EBUSY;
+
+	ret = ov2680_mod_reg(sensor, OV2680_REG_FORMAT1, BIT(2), val ? BIT(2) : 0);
 	if (ret < 0)
 		return ret;
 
@@ -340,33 +343,14 @@ static int ov2680_vflip_enable(struct ov2680_dev *sensor)
 	return 0;
 }
 
-static int ov2680_vflip_disable(struct ov2680_dev *sensor)
+static int ov2680_set_hflip(struct ov2680_dev *sensor, s32 val)
 {
 	int ret;
 
-	ret = ov2680_mod_reg(sensor, OV2680_REG_FORMAT1, BIT(2), BIT(0));
-	if (ret < 0)
-		return ret;
+	if (sensor->is_streaming)
+		return -EBUSY;
 
-	return ov2680_bayer_order(sensor);
-}
-
-static int ov2680_hflip_enable(struct ov2680_dev *sensor)
-{
-	int ret;
-
-	ret = ov2680_mod_reg(sensor, OV2680_REG_FORMAT2, BIT(2), BIT(2));
-	if (ret < 0)
-		return ret;
-
-	return ov2680_bayer_order(sensor);
-}
-
-static int ov2680_hflip_disable(struct ov2680_dev *sensor)
-{
-	int ret;
-
-	ret = ov2680_mod_reg(sensor, OV2680_REG_FORMAT2, BIT(2), BIT(0));
+	ret = ov2680_mod_reg(sensor, OV2680_REG_FORMAT2, BIT(2), val ? BIT(2) : 0);
 	if (ret < 0)
 		return ret;
 
@@ -719,19 +703,9 @@ static int ov2680_s_ctrl(struct v4l2_ctrl *ctrl)
 	case V4L2_CID_EXPOSURE:
 		return ov2680_exposure_set(sensor, ctrl->val);
 	case V4L2_CID_VFLIP:
-		if (sensor->is_streaming)
-			return -EBUSY;
-		if (ctrl->val)
-			return ov2680_vflip_enable(sensor);
-		else
-			return ov2680_vflip_disable(sensor);
+		return ov2680_set_vflip(sensor, ctrl->val);
 	case V4L2_CID_HFLIP:
-		if (sensor->is_streaming)
-			return -EBUSY;
-		if (ctrl->val)
-			return ov2680_hflip_enable(sensor);
-		else
-			return ov2680_hflip_disable(sensor);
+		return ov2680_set_hflip(sensor, ctrl->val);
 	case V4L2_CID_TEST_PATTERN:
 		return ov2680_test_pattern_set(sensor, ctrl->val);
 	default:
-- 
2.40.1


^ permalink raw reply related	[flat|nested] 57+ messages in thread

* [PATCH 04/28] media: ov2680: Use select VIDEO_V4L2_SUBDEV_API
  2023-06-07 16:46 [PATCH 00/28] media: ov2680: Bugfixes + ACPI + selection(crop-tgt) API support Hans de Goede
                   ` (2 preceding siblings ...)
  2023-06-07 16:46 ` [PATCH 03/28] media: ov2680: Fix vflip / hflip set functions Hans de Goede
@ 2023-06-07 16:46 ` Hans de Goede
  2023-06-12  8:20   ` Dan Scally
  2023-06-07 16:46 ` [PATCH 05/28] media: ov2680: Don't take the lock for try_fmt calls Hans de Goede
                   ` (24 subsequent siblings)
  28 siblings, 1 reply; 57+ messages in thread
From: Hans de Goede @ 2023-06-07 16:46 UTC (permalink / raw)
  To: Daniel Scally, Laurent Pinchart, Sakari Ailus, Andy Shevchenko
  Cc: Hans de Goede, Mauro Carvalho Chehab, Kate Hsuan, linux-media

Select VIDEO_V4L2_SUBDEV_API in Kconfig and drop the
ifdef CONFIG_VIDEO_V4L2_SUBDEV_API checks, like other callers
of v4l2_subdev_get_try_format() do.

This is a preparation patch for fixing ov2680_set_fmt()
which == V4L2_SUBDEV_FORMAT_TRY calls not properly filling in
the passed in v4l2_mbus_framefmt struct.

Fixes: 3ee47cad3e69 ("media: ov2680: Add Omnivision OV2680 sensor driver")
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
---
 drivers/media/i2c/Kconfig  |  1 +
 drivers/media/i2c/ov2680.c | 16 ++--------------
 2 files changed, 3 insertions(+), 14 deletions(-)

diff --git a/drivers/media/i2c/Kconfig b/drivers/media/i2c/Kconfig
index 8f55155afe67..791473fcbad3 100644
--- a/drivers/media/i2c/Kconfig
+++ b/drivers/media/i2c/Kconfig
@@ -433,6 +433,7 @@ config VIDEO_OV2680
 	tristate "OmniVision OV2680 sensor support"
 	depends on VIDEO_DEV && I2C
 	select MEDIA_CONTROLLER
+	select VIDEO_V4L2_SUBDEV_API
 	select V4L2_FWNODE
 	help
 	  This is a Video4Linux2 sensor driver for the OmniVision
diff --git a/drivers/media/i2c/ov2680.c b/drivers/media/i2c/ov2680.c
index c1b23c5b7818..d90bbca6e913 100644
--- a/drivers/media/i2c/ov2680.c
+++ b/drivers/media/i2c/ov2680.c
@@ -559,7 +559,6 @@ static int ov2680_get_fmt(struct v4l2_subdev *sd,
 {
 	struct ov2680_dev *sensor = to_ov2680_dev(sd);
 	struct v4l2_mbus_framefmt *fmt = NULL;
-	int ret = 0;
 
 	if (format->pad != 0)
 		return -EINVAL;
@@ -567,22 +566,17 @@ static int ov2680_get_fmt(struct v4l2_subdev *sd,
 	mutex_lock(&sensor->lock);
 
 	if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
-#ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
 		fmt = v4l2_subdev_get_try_format(&sensor->sd, sd_state,
 						 format->pad);
-#else
-		ret = -EINVAL;
-#endif
 	} else {
 		fmt = &sensor->fmt;
 	}
 
-	if (fmt)
-		format->format = *fmt;
+	format->format = *fmt;
 
 	mutex_unlock(&sensor->lock);
 
-	return ret;
+	return 0;
 }
 
 static int ov2680_set_fmt(struct v4l2_subdev *sd,
@@ -591,9 +585,7 @@ static int ov2680_set_fmt(struct v4l2_subdev *sd,
 {
 	struct ov2680_dev *sensor = to_ov2680_dev(sd);
 	struct v4l2_mbus_framefmt *fmt = &format->format;
-#ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
 	struct v4l2_mbus_framefmt *try_fmt;
-#endif
 	const struct ov2680_mode_info *mode;
 	int ret = 0;
 
@@ -616,10 +608,8 @@ static int ov2680_set_fmt(struct v4l2_subdev *sd,
 	}
 
 	if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
-#ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
 		try_fmt = v4l2_subdev_get_try_format(sd, sd_state, 0);
 		format->format = *try_fmt;
-#endif
 		goto unlock;
 	}
 
@@ -777,9 +767,7 @@ static int ov2680_v4l2_register(struct ov2680_dev *sensor)
 	v4l2_i2c_subdev_init(&sensor->sd, sensor->i2c_client,
 			     &ov2680_subdev_ops);
 
-#ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
 	sensor->sd.flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
-#endif
 	sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
 	sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
 
-- 
2.40.1


^ permalink raw reply related	[flat|nested] 57+ messages in thread

* [PATCH 05/28] media: ov2680: Don't take the lock for try_fmt calls
  2023-06-07 16:46 [PATCH 00/28] media: ov2680: Bugfixes + ACPI + selection(crop-tgt) API support Hans de Goede
                   ` (3 preceding siblings ...)
  2023-06-07 16:46 ` [PATCH 04/28] media: ov2680: Use select VIDEO_V4L2_SUBDEV_API Hans de Goede
@ 2023-06-07 16:46 ` Hans de Goede
  2023-06-08 12:44   ` Sakari Ailus
  2023-06-07 16:46 ` [PATCH 06/28] media: ov2680: Add ov2680_fill_format() helper function Hans de Goede
                   ` (23 subsequent siblings)
  28 siblings, 1 reply; 57+ messages in thread
From: Hans de Goede @ 2023-06-07 16:46 UTC (permalink / raw)
  To: Daniel Scally, Laurent Pinchart, Sakari Ailus, Andy Shevchenko
  Cc: Hans de Goede, Mauro Carvalho Chehab, Kate Hsuan, linux-media

On ov2680_set_fmt() calls with format->which == V4L2_SUBDEV_FORMAT_TRY,
ov2680_set_fmt() does not talk to the sensor.

So in this case there is no need to lock the sensor->lock mutex or
to check that the sensor is streaming.

Fixes: 3ee47cad3e69 ("media: ov2680: Add Omnivision OV2680 sensor driver")
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
---
 drivers/media/i2c/ov2680.c | 20 +++++++++-----------
 1 file changed, 9 insertions(+), 11 deletions(-)

diff --git a/drivers/media/i2c/ov2680.c b/drivers/media/i2c/ov2680.c
index d90bbca6e913..a26a6f18f4f1 100644
--- a/drivers/media/i2c/ov2680.c
+++ b/drivers/media/i2c/ov2680.c
@@ -592,24 +592,22 @@ static int ov2680_set_fmt(struct v4l2_subdev *sd,
 	if (format->pad != 0)
 		return -EINVAL;
 
-	mutex_lock(&sensor->lock);
-
-	if (sensor->is_streaming) {
-		ret = -EBUSY;
-		goto unlock;
-	}
-
 	mode = v4l2_find_nearest_size(ov2680_mode_data,
 				      ARRAY_SIZE(ov2680_mode_data), width,
 				      height, fmt->width, fmt->height);
-	if (!mode) {
-		ret = -EINVAL;
-		goto unlock;
-	}
+	if (!mode)
+		return -EINVAL;
 
 	if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
 		try_fmt = v4l2_subdev_get_try_format(sd, sd_state, 0);
 		format->format = *try_fmt;
+		return 0;
+	}
+
+	mutex_lock(&sensor->lock);
+
+	if (sensor->is_streaming) {
+		ret = -EBUSY;
 		goto unlock;
 	}
 
-- 
2.40.1


^ permalink raw reply related	[flat|nested] 57+ messages in thread

* [PATCH 06/28] media: ov2680: Add ov2680_fill_format() helper function
  2023-06-07 16:46 [PATCH 00/28] media: ov2680: Bugfixes + ACPI + selection(crop-tgt) API support Hans de Goede
                   ` (4 preceding siblings ...)
  2023-06-07 16:46 ` [PATCH 05/28] media: ov2680: Don't take the lock for try_fmt calls Hans de Goede
@ 2023-06-07 16:46 ` Hans de Goede
  2023-06-07 16:46 ` [PATCH 07/28] media: ov2680: Fix ov2680_set_fmt() which == V4L2_SUBDEV_FORMAT_TRY not working Hans de Goede
                   ` (22 subsequent siblings)
  28 siblings, 0 replies; 57+ messages in thread
From: Hans de Goede @ 2023-06-07 16:46 UTC (permalink / raw)
  To: Daniel Scally, Laurent Pinchart, Sakari Ailus, Andy Shevchenko
  Cc: Hans de Goede, Mauro Carvalho Chehab, Kate Hsuan, linux-media

Add a ov2680_fill_format() helper function and use this everywhere were
a v4l2_mbus_framefmt struct needs to be filled in so that the driver always
fills it consistently.

This is a preparation patch for fixing ov2680_set_fmt()
which == V4L2_SUBDEV_FORMAT_TRY calls not properly filling in
the passed in v4l2_mbus_framefmt struct.

Note that for ov2680_init_cfg() this now simply always fills
the try_fmt struct of the passed in sd_state. This is correct because
ov2680_init_cfg() is never called with a NULL sd_state so the old
sd_state check is not necessary.

Fixes: 3ee47cad3e69 ("media: ov2680: Add Omnivision OV2680 sensor driver")
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
---
 drivers/media/i2c/ov2680.c | 47 ++++++++++++++++++++------------------
 1 file changed, 25 insertions(+), 22 deletions(-)

diff --git a/drivers/media/i2c/ov2680.c b/drivers/media/i2c/ov2680.c
index a26a6f18f4f1..9d26437a1297 100644
--- a/drivers/media/i2c/ov2680.c
+++ b/drivers/media/i2c/ov2680.c
@@ -54,6 +54,9 @@
 #define OV2680_WIDTH_MAX		1600
 #define OV2680_HEIGHT_MAX		1200
 
+#define OV2680_DEFAULT_WIDTH			800
+#define OV2680_DEFAULT_HEIGHT			600
+
 enum ov2680_mode_id {
 	OV2680_MODE_QUXGA_800_600,
 	OV2680_MODE_720P_1280_720,
@@ -315,7 +318,7 @@ static void ov2680_power_down(struct ov2680_dev *sensor)
 	usleep_range(5000, 10000);
 }
 
-static void ov2680_set_bayer_order(struct ov2680_dev *sensor)
+static void ov2680_set_bayer_order(struct ov2680_dev *sensor, struct v4l2_mbus_framefmt *fmt)
 {
 	int hv_flip = 0;
 
@@ -325,7 +328,19 @@ static void ov2680_set_bayer_order(struct ov2680_dev *sensor)
 	if (sensor->ctrls.hflip && sensor->ctrls.hflip->val)
 		hv_flip += 2;
 
-	sensor->fmt.code = ov2680_hv_flip_bayer_order[hv_flip];
+	fmt->code = ov2680_hv_flip_bayer_order[hv_flip];
+}
+
+static void ov2680_fill_format(struct ov2680_dev *sensor,
+			       struct v4l2_mbus_framefmt *fmt,
+			       unsigned int width, unsigned int height)
+{
+	memset(fmt, 0, sizeof(*fmt));
+	fmt->width = width;
+	fmt->height = height;
+	fmt->field = V4L2_FIELD_NONE;
+	fmt->colorspace = V4L2_COLORSPACE_SRGB;
+	ov2680_set_bayer_order(sensor, fmt);
 }
 
 static int ov2680_set_vflip(struct ov2680_dev *sensor, s32 val)
@@ -339,7 +354,7 @@ static int ov2680_set_vflip(struct ov2680_dev *sensor, s32 val)
 	if (ret < 0)
 		return ret;
 
-	ov2680_set_bayer_order(sensor);
+	ov2680_set_bayer_order(sensor, &sensor->fmt);
 	return 0;
 }
 
@@ -354,7 +369,7 @@ static int ov2680_set_hflip(struct ov2680_dev *sensor, s32 val)
 	if (ret < 0)
 		return ret;
 
-	ov2680_set_bayer_order(sensor);
+	ov2680_set_bayer_order(sensor, &sensor->fmt);
 	return 0;
 }
 
@@ -611,10 +626,7 @@ static int ov2680_set_fmt(struct v4l2_subdev *sd,
 		goto unlock;
 	}
 
-	fmt->width = mode->width;
-	fmt->height = mode->height;
-	fmt->code = sensor->fmt.code;
-	fmt->colorspace = sensor->fmt.colorspace;
+	ov2680_fill_format(sensor, fmt, mode->width, mode->height);
 
 	sensor->current_mode = mode;
 	sensor->fmt = format->format;
@@ -629,16 +641,11 @@ static int ov2680_set_fmt(struct v4l2_subdev *sd,
 static int ov2680_init_cfg(struct v4l2_subdev *sd,
 			   struct v4l2_subdev_state *sd_state)
 {
-	struct v4l2_subdev_format fmt = {
-		.which = sd_state ? V4L2_SUBDEV_FORMAT_TRY
-		: V4L2_SUBDEV_FORMAT_ACTIVE,
-		.format = {
-			.width = 800,
-			.height = 600,
-		}
-	};
+	struct ov2680_dev *sensor = to_ov2680_dev(sd);
 
-	return ov2680_set_fmt(sd, sd_state, &fmt);
+	ov2680_fill_format(sensor, &sd_state->pads[0].try_fmt,
+			   OV2680_DEFAULT_WIDTH, OV2680_DEFAULT_HEIGHT);
+	return 0;
 }
 
 static int ov2680_enum_frame_size(struct v4l2_subdev *sd,
@@ -737,11 +744,7 @@ static int ov2680_mode_init(struct ov2680_dev *sensor)
 	const struct ov2680_mode_info *init_mode;
 
 	/* set initial mode */
-	sensor->fmt.code = MEDIA_BUS_FMT_SBGGR10_1X10;
-	sensor->fmt.width = 800;
-	sensor->fmt.height = 600;
-	sensor->fmt.field = V4L2_FIELD_NONE;
-	sensor->fmt.colorspace = V4L2_COLORSPACE_SRGB;
+	ov2680_fill_format(sensor, &sensor->fmt, OV2680_DEFAULT_WIDTH, OV2680_DEFAULT_HEIGHT);
 
 	sensor->frame_interval.denominator = OV2680_FRAME_RATE;
 	sensor->frame_interval.numerator = 1;
-- 
2.40.1


^ permalink raw reply related	[flat|nested] 57+ messages in thread

* [PATCH 07/28] media: ov2680: Fix ov2680_set_fmt() which == V4L2_SUBDEV_FORMAT_TRY not working
  2023-06-07 16:46 [PATCH 00/28] media: ov2680: Bugfixes + ACPI + selection(crop-tgt) API support Hans de Goede
                   ` (5 preceding siblings ...)
  2023-06-07 16:46 ` [PATCH 06/28] media: ov2680: Add ov2680_fill_format() helper function Hans de Goede
@ 2023-06-07 16:46 ` Hans de Goede
  2023-06-07 16:46 ` [PATCH 08/28] media: Add MIPI CCI register access helper functions Hans de Goede
                   ` (21 subsequent siblings)
  28 siblings, 0 replies; 57+ messages in thread
From: Hans de Goede @ 2023-06-07 16:46 UTC (permalink / raw)
  To: Daniel Scally, Laurent Pinchart, Sakari Ailus, Andy Shevchenko
  Cc: Hans de Goede, Mauro Carvalho Chehab, Kate Hsuan, linux-media

ov2680_set_fmt() which == V4L2_SUBDEV_FORMAT_TRY was getting
the try_fmt v4l2_mbus_framefmt struct from the passed in sd_state
and then storing the contents of that into the return by reference
format->format struct.

While the right thing to do would be filling format->format based on
the just looked up mode and then store the results of that in
sd_state->pads[0].try_fmt .

Before the previous change introducing ov2680_fill_format() this
resulted in ov2680_set_fmt() which == V4L2_SUBDEV_FORMAT_TRY always
returning the zero-ed out sd_state->pads[0].try_fmt in format->format
breaking callers using this.

After the introduction of ov2680_fill_format() which at least
initializes sd_state->pads[0].try_fmt properly, format->format
is now always being filled with the default 800x600 mode set by
ov2680_init_cfg() independent of the actual requested mode.

Move the filling of format->format with ov2680_fill_format() to
before the if (which == V4L2_SUBDEV_FORMAT_TRY) and then store
the filled in format->format in sd_state->pads[0].try_fmt to
fix this.

Note this removes the fmt local variable because IMHO having a local
variable which points to a sub-struct of one of the function arguments
just leads to confusion when reading the code.

Fixes: 3ee47cad3e69 ("media: ov2680: Add Omnivision OV2680 sensor driver")
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
---
 drivers/media/i2c/ov2680.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

diff --git a/drivers/media/i2c/ov2680.c b/drivers/media/i2c/ov2680.c
index 9d26437a1297..ffdc7c06a361 100644
--- a/drivers/media/i2c/ov2680.c
+++ b/drivers/media/i2c/ov2680.c
@@ -599,7 +599,6 @@ static int ov2680_set_fmt(struct v4l2_subdev *sd,
 			  struct v4l2_subdev_format *format)
 {
 	struct ov2680_dev *sensor = to_ov2680_dev(sd);
-	struct v4l2_mbus_framefmt *fmt = &format->format;
 	struct v4l2_mbus_framefmt *try_fmt;
 	const struct ov2680_mode_info *mode;
 	int ret = 0;
@@ -608,14 +607,16 @@ static int ov2680_set_fmt(struct v4l2_subdev *sd,
 		return -EINVAL;
 
 	mode = v4l2_find_nearest_size(ov2680_mode_data,
-				      ARRAY_SIZE(ov2680_mode_data), width,
-				      height, fmt->width, fmt->height);
+				      ARRAY_SIZE(ov2680_mode_data), width, height,
+				      format->format.width, format->format.height);
 	if (!mode)
 		return -EINVAL;
 
+	ov2680_fill_format(sensor, &format->format, mode->width, mode->height);
+
 	if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
 		try_fmt = v4l2_subdev_get_try_format(sd, sd_state, 0);
-		format->format = *try_fmt;
+		*try_fmt = format->format;
 		return 0;
 	}
 
@@ -626,8 +627,6 @@ static int ov2680_set_fmt(struct v4l2_subdev *sd,
 		goto unlock;
 	}
 
-	ov2680_fill_format(sensor, fmt, mode->width, mode->height);
-
 	sensor->current_mode = mode;
 	sensor->fmt = format->format;
 	sensor->mode_pending_changes = true;
-- 
2.40.1


^ permalink raw reply related	[flat|nested] 57+ messages in thread

* [PATCH 08/28] media: Add MIPI CCI register access helper functions
  2023-06-07 16:46 [PATCH 00/28] media: ov2680: Bugfixes + ACPI + selection(crop-tgt) API support Hans de Goede
                   ` (6 preceding siblings ...)
  2023-06-07 16:46 ` [PATCH 07/28] media: ov2680: Fix ov2680_set_fmt() which == V4L2_SUBDEV_FORMAT_TRY not working Hans de Goede
@ 2023-06-07 16:46 ` Hans de Goede
  2023-06-07 20:22   ` Andy Shevchenko
  2023-06-07 16:46 ` [PATCH 09/28] media: ov2680: Convert to new CCI register access helpers Hans de Goede
                   ` (20 subsequent siblings)
  28 siblings, 1 reply; 57+ messages in thread
From: Hans de Goede @ 2023-06-07 16:46 UTC (permalink / raw)
  To: Daniel Scally, Laurent Pinchart, Sakari Ailus, Andy Shevchenko
  Cc: Hans de Goede, Mauro Carvalho Chehab, Kate Hsuan, linux-media

The CSI2 specification specifies a standard method to access camera sensor
registers called "Camera Control Interface (CCI)".

This uses either 8 or 16 bit (big-endian wire order) register addresses
and supports 8, 16, 24 or 32 bit (big-endian wire order) register widths.

Currently a lot of Linux camera sensor drivers all have their own custom
helpers for this, often copy and pasted from other drivers.

Add a set of generic helpers for this so that all sensor drivers can
switch to a single common implementation.

These helpers take an extra optional "int *err" function parameter,
this can be used to chain a bunch of register accesses together with
only a single error check at the end, rather then needing to error
check each individual register access. The first failing call will
set the contents of err to a non 0 value and all other calls will
then become no-ops.

Link: https://lore.kernel.org/linux-media/59aefa7f-7bf9-6736-6040-39551329cd0a@redhat.com/
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
---
 Documentation/driver-api/media/v4l2-cci.rst  |   5 +
 Documentation/driver-api/media/v4l2-core.rst |   1 +
 drivers/media/v4l2-core/Kconfig              |   5 +
 drivers/media/v4l2-core/Makefile             |   1 +
 drivers/media/v4l2-core/v4l2-cci.c           | 142 +++++++++++++++++++
 include/media/v4l2-cci.h                     | 109 ++++++++++++++
 6 files changed, 263 insertions(+)
 create mode 100644 Documentation/driver-api/media/v4l2-cci.rst
 create mode 100644 drivers/media/v4l2-core/v4l2-cci.c
 create mode 100644 include/media/v4l2-cci.h

diff --git a/Documentation/driver-api/media/v4l2-cci.rst b/Documentation/driver-api/media/v4l2-cci.rst
new file mode 100644
index 000000000000..dd297a40ed20
--- /dev/null
+++ b/Documentation/driver-api/media/v4l2-cci.rst
@@ -0,0 +1,5 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+V4L2 CCI kAPI
+^^^^^^^^^^^^^
+.. kernel-doc:: include/media/v4l2-cci.h
diff --git a/Documentation/driver-api/media/v4l2-core.rst b/Documentation/driver-api/media/v4l2-core.rst
index 1a8c4a5f256b..239045ecc8f4 100644
--- a/Documentation/driver-api/media/v4l2-core.rst
+++ b/Documentation/driver-api/media/v4l2-core.rst
@@ -22,6 +22,7 @@ Video4Linux devices
     v4l2-mem2mem
     v4l2-async
     v4l2-fwnode
+    v4l2-cci
     v4l2-rect
     v4l2-tuner
     v4l2-common
diff --git a/drivers/media/v4l2-core/Kconfig b/drivers/media/v4l2-core/Kconfig
index 348559bc2468..523ba243261d 100644
--- a/drivers/media/v4l2-core/Kconfig
+++ b/drivers/media/v4l2-core/Kconfig
@@ -74,6 +74,11 @@ config V4L2_FWNODE
 config V4L2_ASYNC
 	tristate
 
+config V4L2_CCI
+	tristate
+	depends on I2C
+	select REGMAP_I2C
+
 # Used by drivers that need Videobuf modules
 config VIDEOBUF_GEN
 	tristate
diff --git a/drivers/media/v4l2-core/Makefile b/drivers/media/v4l2-core/Makefile
index 41d91bd10cf2..be2551705755 100644
--- a/drivers/media/v4l2-core/Makefile
+++ b/drivers/media/v4l2-core/Makefile
@@ -25,6 +25,7 @@ videodev-$(CONFIG_VIDEO_V4L2_I2C) += v4l2-i2c.o
 # (e. g. LC_ALL=C sort Makefile)
 
 obj-$(CONFIG_V4L2_ASYNC) += v4l2-async.o
+obj-$(CONFIG_V4L2_CCI) += v4l2-cci.o
 obj-$(CONFIG_V4L2_FLASH_LED_CLASS) += v4l2-flash-led-class.o
 obj-$(CONFIG_V4L2_FWNODE) += v4l2-fwnode.o
 obj-$(CONFIG_V4L2_H264) += v4l2-h264.o
diff --git a/drivers/media/v4l2-core/v4l2-cci.c b/drivers/media/v4l2-core/v4l2-cci.c
new file mode 100644
index 000000000000..21207d137dbe
--- /dev/null
+++ b/drivers/media/v4l2-core/v4l2-cci.c
@@ -0,0 +1,142 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * MIPI Camera Control Interface (CCI) register access helpers.
+ *
+ * Copyright (C) 2023 Hans de Goede <hansg@kernel.org>
+ */
+
+#include <linux/delay.h>
+#include <linux/dev_printk.h>
+#include <linux/module.h>
+#include <linux/regmap.h>
+
+#include <media/v4l2-cci.h>
+
+int cci_read(struct regmap *map, u32 reg, u32 *val, int *err)
+{
+	int i, len, ret;
+	u8 buf[4];
+
+	if (err && *err)
+		return *err;
+
+	/* Set len to register width in bytes */
+	len = ((reg & CCI_REG_WIDTH_MASK) >> CCI_REG_WIDTH_SHIFT) + 1;
+	reg &= CCI_REG_ADDR_MASK;
+
+	ret = regmap_bulk_read(map, reg, buf, len);
+	if (ret) {
+		dev_err(regmap_get_device(map), "Error reading reg 0x%4x: %d\n", reg, ret);
+		if (err)
+			*err = ret;
+
+		return ret;
+	}
+
+	*val = 0;
+	for (i = 0; i < len; i++) {
+		*val <<= 8;
+		*val |= buf[i];
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(cci_read);
+
+int cci_write(struct regmap *map, u32 reg, u32 val, int *err)
+{
+	int i, len, ret;
+	u8 buf[4];
+
+	if (err && *err)
+		return *err;
+
+	/* Set len to register width in bytes */
+	len = ((reg & CCI_REG_WIDTH_MASK) >> CCI_REG_WIDTH_SHIFT) + 1;
+	reg &= CCI_REG_ADDR_MASK;
+
+	for (i = 0; i < len; i++) {
+		buf[len - i - 1] = val & 0xff;
+		val >>= 8;
+	}
+
+	ret = regmap_bulk_write(map, reg, buf, len);
+	if (ret) {
+		dev_err(regmap_get_device(map), "Error writing reg 0x%4x: %d\n", reg, ret);
+		if (err)
+			*err = ret;
+	}
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(cci_write);
+
+int cci_update_bits(struct regmap *map, u32 reg, u32 mask, u32 val, int *err)
+{
+	int width, ret;
+	u32 readval;
+
+	if (err && *err)
+		return *err;
+
+	/*
+	 * For single byte updates use regmap_update_bits(), this uses
+	 * the regmap-lock to protect against other read-modify-writes racing.
+	 */
+	width = (reg & CCI_REG_WIDTH_MASK) >> CCI_REG_WIDTH_SHIFT;
+	if (width == cci_reg_8) {
+		reg &= CCI_REG_ADDR_MASK;
+		ret = regmap_update_bits(map, reg, mask, val);
+		if (ret) {
+			dev_err(regmap_get_device(map), "Error updating reg 0x%4x: %d\n", reg, ret);
+			if (err)
+				*err = ret;
+		}
+
+		return ret;
+	}
+
+	ret = cci_read(map, reg, &readval, err);
+	if (ret)
+		return ret;
+
+	val = (readval & ~mask) | (val & mask);
+
+	return cci_write(map, reg, val, err);
+}
+EXPORT_SYMBOL_GPL(cci_update_bits);
+
+int cci_multi_reg_write(struct regmap *map, const struct reg_sequence *regs, int num_regs, int *err)
+{
+	int i, ret;
+
+	if (err && *err)
+		return *err;
+
+	for (i = 0; i < num_regs; i++) {
+		ret = cci_write(map, regs[i].reg, regs[i].def, err);
+		if (ret)
+			return ret;
+
+		if (regs[i].delay_us)
+			fsleep(regs[i].delay_us);
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(cci_multi_reg_write);
+
+struct regmap *cci_regmap_init_i2c(struct i2c_client *client, int reg_addr_bits)
+{
+	struct regmap_config config = {
+		.reg_bits = reg_addr_bits,
+		.val_bits = 8,
+		.reg_format_endian = REGMAP_ENDIAN_BIG,
+	};
+
+	return devm_regmap_init_i2c(client, &config);
+}
+EXPORT_SYMBOL_GPL(cci_regmap_init_i2c);
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Hans de Goede <hansg@kernel.org>");
diff --git a/include/media/v4l2-cci.h b/include/media/v4l2-cci.h
new file mode 100644
index 000000000000..69b8a7c4a013
--- /dev/null
+++ b/include/media/v4l2-cci.h
@@ -0,0 +1,109 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * MIPI Camera Control Interface (CCI) register access helpers.
+ *
+ * Copyright (C) 2023 Hans de Goede <hansg@kernel.org>
+ */
+#ifndef _V4L2_CCI_H
+#define _V4L2_CCI_H
+
+#include <linux/regmap.h>
+#include <linux/types.h>
+
+/*
+ * Note cci_reg_8 deliberately is 0, not 1, so that raw
+ * (not wrapped in a CCI_REG*() macro) register addresses
+ * do 8 bit wide accesses. This allows unchanged use of register
+ * initialization lists of raw address, value pairs which only
+ * do 8 bit width accesses. Which makes porting drivers easier.
+ */
+enum cci_reg_type {
+	cci_reg_8 = 0,
+	cci_reg_16,
+	cci_reg_24,
+	cci_reg_32,
+};
+
+/*
+ * Macros to define register address with the register width encoded
+ * into the higher bits. CCI_REG8() is a no-op so its use is optional.
+ */
+#define CCI_REG_ADDR_MASK		GENMASK(15, 0)
+#define CCI_REG_WIDTH_SHIFT		16
+#define CCI_REG_WIDTH_MASK		GENMASK(17, 16)
+
+#define CCI_REG8(x)			((cci_reg_8 << CCI_REG_WIDTH_SHIFT) | (x))
+#define CCI_REG16(x)			((cci_reg_16 << CCI_REG_WIDTH_SHIFT) | (x))
+#define CCI_REG24(x)			((cci_reg_24 << CCI_REG_WIDTH_SHIFT) | (x))
+#define CCI_REG32(x)			((cci_reg_32 << CCI_REG_WIDTH_SHIFT) | (x))
+
+/**
+ * cci_read() - Read a value from a single CCI register
+ *
+ * @map: Register map to write to
+ * @reg: Register address to write, use CCI_REG#() macros to encode reg width
+ * @val: Pointer to store read value
+ * @err: optional pointer to store errors, if a previous error is set the write will be skipped
+ *
+ * Return: %0 on success or a negative error code on failure.
+ */
+int cci_read(struct regmap *map, u32 reg, u32 *val, int *err);
+
+/**
+ * cci_write() - Write a value to a single CCI register
+ *
+ * @map: Register map to write to
+ * @reg: Register address to write, use CCI_REG#() macros to encode reg width
+ * @val: Value to be written
+ * @err: optional pointer to store errors, if a previous error is set the write will be skipped
+ *
+ * Return: %0 on success or a negative error code on failure.
+ */
+int cci_write(struct regmap *map, u32 reg, u32 val, int *err);
+
+/**
+ * cci_update_bits() - Perform a read/modify/write cycle on a single CCI register
+ *
+ * @map: Register map to write to
+ * @reg: Register address to write, use CCI_REG#() macros to encode reg width
+ * @mask: Bitmask to change
+ * @val: New value for bitmask
+ * @err: optional pointer to store errors, if a previous error is set the update will be skipped
+ *
+ * For 8 bit width registers this is guaranteed to be atomic wrt other
+ * cci_*() register access functions. For multi-byte width registers
+ * atomicity is NOT guaranteed.
+ *
+ * Return: %0 on success or a negative error code on failure.
+ */
+int cci_update_bits(struct regmap *map, u32 reg, u32 mask, u32 val, int *err);
+
+/**
+ * cci_multi_reg_write() - Write multiple registers to the device
+ *
+ * @map: Register map to write to
+ * @regs: Array of structures containing register-address, value pairs to be written
+ *        register-addresses use CCI_REG#() macros to encode reg width
+ * @num_regs: Number of registers to write
+ * @err: optional pointer to store errors, if a previous error is set the update will be skipped
+ *
+ * Write multiple registers to the device where the set of register, value
+ * pairs are supplied in any order, possibly not all in a single range.
+ *
+ * Return: %0 on success or a negative error code on failure.
+ */
+int cci_multi_reg_write(struct regmap *map, const struct reg_sequence *regs, int num_regs, int *err);
+
+/**
+ * cci_regmap_init_i2c() - Create regmap to use with cci_*() register access functions
+ *
+ * @client: i2c_client to create the regmap for
+ * @reg_addr_bits: register address width to use (8 or 16)
+ *
+ * Note the memory for the created regmap is devm() managed, tied to the client.
+ *
+ * Return: %0 on success or a negative error code on failure.
+ */
+struct regmap *cci_regmap_init_i2c(struct i2c_client *client, int reg_addr_bits);
+
+#endif
-- 
2.40.1


^ permalink raw reply related	[flat|nested] 57+ messages in thread

* [PATCH 09/28] media: ov2680: Convert to new CCI register access helpers
  2023-06-07 16:46 [PATCH 00/28] media: ov2680: Bugfixes + ACPI + selection(crop-tgt) API support Hans de Goede
                   ` (7 preceding siblings ...)
  2023-06-07 16:46 ` [PATCH 08/28] media: Add MIPI CCI register access helper functions Hans de Goede
@ 2023-06-07 16:46 ` Hans de Goede
  2023-06-07 16:46 ` [PATCH 10/28] media: ov2680: Store dev instead of i2c_client in ov2680_dev Hans de Goede
                   ` (19 subsequent siblings)
  28 siblings, 0 replies; 57+ messages in thread
From: Hans de Goede @ 2023-06-07 16:46 UTC (permalink / raw)
  To: Daniel Scally, Laurent Pinchart, Sakari Ailus, Andy Shevchenko
  Cc: Hans de Goede, Mauro Carvalho Chehab, Kate Hsuan, linux-media

Use the new comon CCI register access helpers to replace the private
register access helpers in the ov2680 driver.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
---
 drivers/media/i2c/Kconfig  |   1 +
 drivers/media/i2c/ov2680.c | 207 +++++++++----------------------------
 2 files changed, 47 insertions(+), 161 deletions(-)

diff --git a/drivers/media/i2c/Kconfig b/drivers/media/i2c/Kconfig
index 791473fcbad3..d17b52e390e1 100644
--- a/drivers/media/i2c/Kconfig
+++ b/drivers/media/i2c/Kconfig
@@ -435,6 +435,7 @@ config VIDEO_OV2680
 	select MEDIA_CONTROLLER
 	select VIDEO_V4L2_SUBDEV_API
 	select V4L2_FWNODE
+	select V4L2_CCI
 	help
 	  This is a Video4Linux2 sensor driver for the OmniVision
 	  OV2680 camera.
diff --git a/drivers/media/i2c/ov2680.c b/drivers/media/i2c/ov2680.c
index ffdc7c06a361..dc57052336b3 100644
--- a/drivers/media/i2c/ov2680.c
+++ b/drivers/media/i2c/ov2680.c
@@ -10,7 +10,6 @@
  *
  */
 
-#include <asm/unaligned.h>
 #include <linux/clk.h>
 #include <linux/delay.h>
 #include <linux/err.h>
@@ -21,38 +20,34 @@
 #include <linux/gpio/consumer.h>
 #include <linux/regulator/consumer.h>
 
+#include <media/v4l2-cci.h>
 #include <media/v4l2-common.h>
 #include <media/v4l2-ctrls.h>
 #include <media/v4l2-subdev.h>
 
-#define OV2680_XVCLK_VALUE	24000000
+#define OV2680_XVCLK_VALUE			24000000
 
-#define OV2680_CHIP_ID		0x2680
+#define OV2680_CHIP_ID				0x2680
 
-#define OV2680_REG_STREAM_CTRL		0x0100
-#define OV2680_REG_SOFT_RESET		0x0103
+#define OV2680_REG_STREAM_CTRL			CCI_REG8(0x0100)
+#define OV2680_REG_SOFT_RESET			CCI_REG8(0x0103)
 
-#define OV2680_REG_CHIP_ID_HIGH		0x300a
-#define OV2680_REG_CHIP_ID_LOW		0x300b
+#define OV2680_REG_CHIP_ID			CCI_REG16(0x300a)
 
-#define OV2680_REG_R_MANUAL		0x3503
-#define OV2680_REG_GAIN_PK		0x350a
-#define OV2680_REG_EXPOSURE_PK_HIGH	0x3500
-#define OV2680_REG_TIMING_HTS		0x380c
-#define OV2680_REG_TIMING_VTS		0x380e
-#define OV2680_REG_FORMAT1		0x3820
-#define OV2680_REG_FORMAT2		0x3821
+#define OV2680_REG_EXPOSURE_PK			CCI_REG24(0x3500)
+#define OV2680_REG_R_MANUAL			CCI_REG8(0x3503)
+#define OV2680_REG_GAIN_PK			CCI_REG16(0x350a)
+#define OV2680_REG_TIMING_HTS			CCI_REG16(0x380c)
+#define OV2680_REG_TIMING_VTS			CCI_REG16(0x380e)
+#define OV2680_REG_FORMAT1			CCI_REG8(0x3820)
+#define OV2680_REG_FORMAT2			CCI_REG8(0x3821)
 
-#define OV2680_REG_ISP_CTRL00		0x5080
+#define OV2680_REG_ISP_CTRL00			CCI_REG8(0x5080)
 
-#define OV2680_FRAME_RATE		30
+#define OV2680_FRAME_RATE			30
 
-#define OV2680_REG_VALUE_8BIT		1
-#define OV2680_REG_VALUE_16BIT		2
-#define OV2680_REG_VALUE_24BIT		3
-
-#define OV2680_WIDTH_MAX		1600
-#define OV2680_HEIGHT_MAX		1200
+#define OV2680_WIDTH_MAX			1600
+#define OV2680_HEIGHT_MAX			1200
 
 #define OV2680_DEFAULT_WIDTH			800
 #define OV2680_DEFAULT_HEIGHT			600
@@ -64,11 +59,6 @@ enum ov2680_mode_id {
 	OV2680_MODE_MAX,
 };
 
-struct reg_value {
-	u16 reg_addr;
-	u8 val;
-};
-
 static const char * const ov2680_supply_name[] = {
 	"DOVDD",
 	"DVDD",
@@ -82,7 +72,7 @@ struct ov2680_mode_info {
 	enum ov2680_mode_id id;
 	u32 width;
 	u32 height;
-	const struct reg_value *reg_data;
+	const struct reg_sequence *reg_data;
 	u32 reg_data_size;
 };
 
@@ -97,6 +87,7 @@ struct ov2680_ctrls {
 
 struct ov2680_dev {
 	struct i2c_client		*i2c_client;
+	struct regmap			*regmap;
 	struct v4l2_subdev		sd;
 
 	struct media_pad		pad;
@@ -133,7 +124,7 @@ static const int ov2680_hv_flip_bayer_order[] = {
 	MEDIA_BUS_FMT_SRGGB10_1X10,
 };
 
-static const struct reg_value ov2680_setting_30fps_QUXGA_800_600[] = {
+static const struct reg_sequence ov2680_setting_30fps_QUXGA_800_600[] = {
 	{0x3086, 0x01}, {0x370a, 0x23}, {0x3808, 0x03}, {0x3809, 0x20},
 	{0x380a, 0x02}, {0x380b, 0x58}, {0x380c, 0x06}, {0x380d, 0xac},
 	{0x380e, 0x02}, {0x380f, 0x84}, {0x3811, 0x04}, {0x3813, 0x04},
@@ -142,14 +133,14 @@ static const struct reg_value ov2680_setting_30fps_QUXGA_800_600[] = {
 	{0x3503, 0x03},
 };
 
-static const struct reg_value ov2680_setting_30fps_720P_1280_720[] = {
+static const struct reg_sequence ov2680_setting_30fps_720P_1280_720[] = {
 	{0x3086, 0x00}, {0x3808, 0x05}, {0x3809, 0x00}, {0x380a, 0x02},
 	{0x380b, 0xd0}, {0x380c, 0x06}, {0x380d, 0xa8}, {0x380e, 0x05},
 	{0x380f, 0x0e}, {0x3811, 0x08}, {0x3813, 0x06}, {0x3814, 0x11},
 	{0x3815, 0x11}, {0x3820, 0xc0}, {0x4008, 0x00},
 };
 
-static const struct reg_value ov2680_setting_30fps_UXGA_1600_1200[] = {
+static const struct reg_sequence ov2680_setting_30fps_UXGA_1600_1200[] = {
 	{0x3086, 0x00}, {0x3501, 0x4e}, {0x3502, 0xe0}, {0x3808, 0x06},
 	{0x3809, 0x40}, {0x380a, 0x04}, {0x380b, 0xb0}, {0x380c, 0x06},
 	{0x380d, 0xa8}, {0x380e, 0x05}, {0x380f, 0x0e}, {0x3811, 0x00},
@@ -191,115 +182,6 @@ static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl)
 			     ctrls.handler)->sd;
 }
 
-static int __ov2680_write_reg(struct ov2680_dev *sensor, u16 reg,
-			      unsigned int len, u32 val)
-{
-	struct i2c_client *client = sensor->i2c_client;
-	u8 buf[6];
-	int ret;
-
-	if (len > 4)
-		return -EINVAL;
-
-	put_unaligned_be16(reg, buf);
-	put_unaligned_be32(val << (8 * (4 - len)), buf + 2);
-	ret = i2c_master_send(client, buf, len + 2);
-	if (ret != len + 2) {
-		dev_err(&client->dev, "write error: reg=0x%4x: %d\n", reg, ret);
-		return -EIO;
-	}
-
-	return 0;
-}
-
-#define ov2680_write_reg(s, r, v) \
-	__ov2680_write_reg(s, r, OV2680_REG_VALUE_8BIT, v)
-
-#define ov2680_write_reg16(s, r, v) \
-	__ov2680_write_reg(s, r, OV2680_REG_VALUE_16BIT, v)
-
-#define ov2680_write_reg24(s, r, v) \
-	__ov2680_write_reg(s, r, OV2680_REG_VALUE_24BIT, v)
-
-static int __ov2680_read_reg(struct ov2680_dev *sensor, u16 reg,
-			     unsigned int len, u32 *val)
-{
-	struct i2c_client *client = sensor->i2c_client;
-	struct i2c_msg msgs[2];
-	u8 addr_buf[2] = { reg >> 8, reg & 0xff };
-	u8 data_buf[4] = { 0, };
-	int ret;
-
-	if (len > 4)
-		return -EINVAL;
-
-	msgs[0].addr = client->addr;
-	msgs[0].flags = 0;
-	msgs[0].len = ARRAY_SIZE(addr_buf);
-	msgs[0].buf = addr_buf;
-
-	msgs[1].addr = client->addr;
-	msgs[1].flags = I2C_M_RD;
-	msgs[1].len = len;
-	msgs[1].buf = &data_buf[4 - len];
-
-	ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
-	if (ret != ARRAY_SIZE(msgs)) {
-		dev_err(&client->dev, "read error: reg=0x%4x: %d\n", reg, ret);
-		return -EIO;
-	}
-
-	*val = get_unaligned_be32(data_buf);
-
-	return 0;
-}
-
-#define ov2680_read_reg(s, r, v) \
-	__ov2680_read_reg(s, r, OV2680_REG_VALUE_8BIT, v)
-
-#define ov2680_read_reg16(s, r, v) \
-	__ov2680_read_reg(s, r, OV2680_REG_VALUE_16BIT, v)
-
-#define ov2680_read_reg24(s, r, v) \
-	__ov2680_read_reg(s, r, OV2680_REG_VALUE_24BIT, v)
-
-static int ov2680_mod_reg(struct ov2680_dev *sensor, u16 reg, u8 mask, u8 val)
-{
-	u32 readval;
-	int ret;
-
-	ret = ov2680_read_reg(sensor, reg, &readval);
-	if (ret < 0)
-		return ret;
-
-	readval &= ~mask;
-	val &= mask;
-	val |= readval;
-
-	return ov2680_write_reg(sensor, reg, val);
-}
-
-static int ov2680_load_regs(struct ov2680_dev *sensor,
-			    const struct ov2680_mode_info *mode)
-{
-	const struct reg_value *regs = mode->reg_data;
-	unsigned int i;
-	int ret = 0;
-	u16 reg_addr;
-	u8 val;
-
-	for (i = 0; i < mode->reg_data_size; ++i, ++regs) {
-		reg_addr = regs->reg_addr;
-		val = regs->val;
-
-		ret = ov2680_write_reg(sensor, reg_addr, val);
-		if (ret)
-			break;
-	}
-
-	return ret;
-}
-
 static void ov2680_power_up(struct ov2680_dev *sensor)
 {
 	if (!sensor->reset_gpio)
@@ -350,7 +232,8 @@ static int ov2680_set_vflip(struct ov2680_dev *sensor, s32 val)
 	if (sensor->is_streaming)
 		return -EBUSY;
 
-	ret = ov2680_mod_reg(sensor, OV2680_REG_FORMAT1, BIT(2), val ? BIT(2) : 0);
+	ret = cci_update_bits(sensor->regmap, OV2680_REG_FORMAT1, BIT(2),
+			      val ? BIT(2) : 0, NULL);
 	if (ret < 0)
 		return ret;
 
@@ -365,7 +248,8 @@ static int ov2680_set_hflip(struct ov2680_dev *sensor, s32 val)
 	if (sensor->is_streaming)
 		return -EBUSY;
 
-	ret = ov2680_mod_reg(sensor, OV2680_REG_FORMAT2, BIT(2), val ? BIT(2) : 0);
+	ret = cci_update_bits(sensor->regmap, OV2680_REG_FORMAT2, BIT(2),
+			      val ? BIT(2) : 0, NULL);
 	if (ret < 0)
 		return ret;
 
@@ -375,47 +259,43 @@ static int ov2680_set_hflip(struct ov2680_dev *sensor, s32 val)
 
 static int ov2680_test_pattern_set(struct ov2680_dev *sensor, int value)
 {
-	int ret;
+	int ret = 0;
 
 	if (!value)
-		return ov2680_mod_reg(sensor, OV2680_REG_ISP_CTRL00, BIT(7), 0);
+		return cci_update_bits(sensor->regmap, OV2680_REG_ISP_CTRL00, BIT(7), 0, NULL);
 
-	ret = ov2680_mod_reg(sensor, OV2680_REG_ISP_CTRL00, 0x03, value - 1);
-	if (ret < 0)
-		return ret;
+	cci_update_bits(sensor->regmap, OV2680_REG_ISP_CTRL00, 0x03, value - 1, &ret);
+	cci_update_bits(sensor->regmap, OV2680_REG_ISP_CTRL00, BIT(7), BIT(7), &ret);
 
-	ret = ov2680_mod_reg(sensor, OV2680_REG_ISP_CTRL00, BIT(7), BIT(7));
-	if (ret < 0)
-		return ret;
-
-	return 0;
+	return ret;
 }
 
 static int ov2680_gain_set(struct ov2680_dev *sensor, u32 gain)
 {
-	return ov2680_write_reg16(sensor, OV2680_REG_GAIN_PK, gain);
+	return cci_write(sensor->regmap, OV2680_REG_GAIN_PK, gain, NULL);
 }
 
 static int ov2680_exposure_set(struct ov2680_dev *sensor, u32 exp)
 {
-	return ov2680_write_reg24(sensor, OV2680_REG_EXPOSURE_PK_HIGH, exp << 4);
+	return cci_write(sensor->regmap, OV2680_REG_EXPOSURE_PK, exp << 4, NULL);
 }
 
 static int ov2680_stream_enable(struct ov2680_dev *sensor)
 {
-	return ov2680_write_reg(sensor, OV2680_REG_STREAM_CTRL, 1);
+	return cci_write(sensor->regmap, OV2680_REG_STREAM_CTRL, 1, NULL);
 }
 
 static int ov2680_stream_disable(struct ov2680_dev *sensor)
 {
-	return ov2680_write_reg(sensor, OV2680_REG_STREAM_CTRL, 0);
+	return cci_write(sensor->regmap, OV2680_REG_STREAM_CTRL, 0, NULL);
 }
 
 static int ov2680_mode_set(struct ov2680_dev *sensor)
 {
 	int ret;
 
-	ret = ov2680_load_regs(sensor, sensor->current_mode);
+	ret = cci_multi_reg_write(sensor->regmap, sensor->current_mode->reg_data,
+				  sensor->current_mode->reg_data_size, NULL);
 	if (ret < 0)
 		return ret;
 
@@ -433,7 +313,8 @@ static int ov2680_mode_restore(struct ov2680_dev *sensor)
 {
 	int ret;
 
-	ret = ov2680_load_regs(sensor, &ov2680_mode_init_data);
+	ret = cci_multi_reg_write(sensor->regmap, ov2680_mode_init_data.reg_data,
+				  ov2680_mode_init_data.reg_data_size, NULL);
 	if (ret < 0)
 		return ret;
 
@@ -468,7 +349,7 @@ static int ov2680_power_on(struct ov2680_dev *sensor)
 	}
 
 	if (!sensor->reset_gpio) {
-		ret = ov2680_write_reg(sensor, OV2680_REG_SOFT_RESET, 0x01);
+		ret = cci_write(sensor->regmap, OV2680_REG_SOFT_RESET, 0x01, NULL);
 		if (ret != 0) {
 			dev_err(dev, "sensor soft reset failed\n");
 			return ret;
@@ -835,9 +716,9 @@ static int ov2680_check_id(struct ov2680_dev *sensor)
 
 	ov2680_power_on(sensor);
 
-	ret = ov2680_read_reg16(sensor, OV2680_REG_CHIP_ID_HIGH, &chip_id);
+	ret = cci_read(sensor->regmap, OV2680_REG_CHIP_ID, &chip_id, NULL);
 	if (ret < 0) {
-		dev_err(dev, "failed to read chip id high\n");
+		dev_err(dev, "failed to read chip id\n");
 		return -ENODEV;
 	}
 
@@ -891,6 +772,10 @@ static int ov2680_probe(struct i2c_client *client)
 
 	sensor->i2c_client = client;
 
+	sensor->regmap = cci_regmap_init_i2c(client, 16);
+	if (IS_ERR(sensor->regmap))
+		return PTR_ERR(sensor->regmap);
+
 	ret = ov2680_parse_dt(sensor);
 	if (ret < 0)
 		return -EINVAL;
-- 
2.40.1


^ permalink raw reply related	[flat|nested] 57+ messages in thread

* [PATCH 10/28] media: ov2680: Store dev instead of i2c_client in ov2680_dev
  2023-06-07 16:46 [PATCH 00/28] media: ov2680: Bugfixes + ACPI + selection(crop-tgt) API support Hans de Goede
                   ` (8 preceding siblings ...)
  2023-06-07 16:46 ` [PATCH 09/28] media: ov2680: Convert to new CCI register access helpers Hans de Goede
@ 2023-06-07 16:46 ` Hans de Goede
  2023-06-07 20:43   ` Andy Shevchenko
  2023-06-07 16:46 ` [PATCH 11/28] media: ov2680: Check for "powerdown" GPIO con-id before checking for "reset" GPIO con-id Hans de Goede
                   ` (18 subsequent siblings)
  28 siblings, 1 reply; 57+ messages in thread
From: Hans de Goede @ 2023-06-07 16:46 UTC (permalink / raw)
  To: Daniel Scally, Laurent Pinchart, Sakari Ailus, Andy Shevchenko
  Cc: Hans de Goede, Mauro Carvalho Chehab, Kate Hsuan, linux-media

Now that the cci_* register access helpers are used access to
the i2c_client after probe() is no longer necessary.

Directly store a struct device *dev pointing to &client->dev inside
ov2680_dev to make the code simpler.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
---
 drivers/media/i2c/ov2680.c | 32 +++++++++++---------------------
 1 file changed, 11 insertions(+), 21 deletions(-)

diff --git a/drivers/media/i2c/ov2680.c b/drivers/media/i2c/ov2680.c
index dc57052336b3..e43028b1e939 100644
--- a/drivers/media/i2c/ov2680.c
+++ b/drivers/media/i2c/ov2680.c
@@ -86,7 +86,7 @@ struct ov2680_ctrls {
 };
 
 struct ov2680_dev {
-	struct i2c_client		*i2c_client;
+	struct device			*dev;
 	struct regmap			*regmap;
 	struct v4l2_subdev		sd;
 
@@ -171,11 +171,6 @@ static struct ov2680_dev *to_ov2680_dev(struct v4l2_subdev *sd)
 	return container_of(sd, struct ov2680_dev, sd);
 }
 
-static struct device *ov2680_to_dev(struct ov2680_dev *sensor)
-{
-	return &sensor->i2c_client->dev;
-}
-
 static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl)
 {
 	return &container_of(ctrl->handler, struct ov2680_dev,
@@ -336,7 +331,6 @@ static int ov2680_power_off(struct ov2680_dev *sensor)
 
 static int ov2680_power_on(struct ov2680_dev *sensor)
 {
-	struct device *dev = ov2680_to_dev(sensor);
 	int ret;
 
 	if (sensor->is_enabled)
@@ -344,14 +338,14 @@ static int ov2680_power_on(struct ov2680_dev *sensor)
 
 	ret = regulator_bulk_enable(OV2680_NUM_SUPPLIES, sensor->supplies);
 	if (ret < 0) {
-		dev_err(dev, "failed to enable regulators: %d\n", ret);
+		dev_err(sensor->dev, "failed to enable regulators: %d\n", ret);
 		return ret;
 	}
 
 	if (!sensor->reset_gpio) {
 		ret = cci_write(sensor->regmap, OV2680_REG_SOFT_RESET, 0x01, NULL);
 		if (ret != 0) {
-			dev_err(dev, "sensor soft reset failed\n");
+			dev_err(sensor->dev, "sensor soft reset failed\n");
 			return ret;
 		}
 		usleep_range(1000, 2000);
@@ -638,15 +632,14 @@ static int ov2680_mode_init(struct ov2680_dev *sensor)
 	return 0;
 }
 
-static int ov2680_v4l2_register(struct ov2680_dev *sensor)
+static int ov2680_v4l2_register(struct ov2680_dev *sensor, struct i2c_client *client)
 {
 	const struct v4l2_ctrl_ops *ops = &ov2680_ctrl_ops;
 	struct ov2680_ctrls *ctrls = &sensor->ctrls;
 	struct v4l2_ctrl_handler *hdl = &ctrls->handler;
 	int ret = 0;
 
-	v4l2_i2c_subdev_init(&sensor->sd, sensor->i2c_client,
-			     &ov2680_subdev_ops);
+	v4l2_i2c_subdev_init(&sensor->sd, client, &ov2680_subdev_ops);
 
 	sensor->sd.flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
 	sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
@@ -703,14 +696,11 @@ static int ov2680_get_regulators(struct ov2680_dev *sensor)
 	for (i = 0; i < OV2680_NUM_SUPPLIES; i++)
 		sensor->supplies[i].supply = ov2680_supply_name[i];
 
-	return devm_regulator_bulk_get(&sensor->i2c_client->dev,
-				       OV2680_NUM_SUPPLIES,
-				       sensor->supplies);
+	return devm_regulator_bulk_get(sensor->dev, OV2680_NUM_SUPPLIES, sensor->supplies);
 }
 
 static int ov2680_check_id(struct ov2680_dev *sensor)
 {
-	struct device *dev = ov2680_to_dev(sensor);
 	u32 chip_id;
 	int ret;
 
@@ -718,12 +708,12 @@ static int ov2680_check_id(struct ov2680_dev *sensor)
 
 	ret = cci_read(sensor->regmap, OV2680_REG_CHIP_ID, &chip_id, NULL);
 	if (ret < 0) {
-		dev_err(dev, "failed to read chip id\n");
+		dev_err(sensor->dev, "failed to read chip id\n");
 		return -ENODEV;
 	}
 
 	if (chip_id != OV2680_CHIP_ID) {
-		dev_err(dev, "chip id: 0x%04x does not match expected 0x%04x\n",
+		dev_err(sensor->dev, "chip id: 0x%04x does not match expected 0x%04x\n",
 			chip_id, OV2680_CHIP_ID);
 		return -ENODEV;
 	}
@@ -733,7 +723,7 @@ static int ov2680_check_id(struct ov2680_dev *sensor)
 
 static int ov2680_parse_dt(struct ov2680_dev *sensor)
 {
-	struct device *dev = ov2680_to_dev(sensor);
+	struct device *dev = sensor->dev;
 	int ret;
 
 	sensor->reset_gpio = devm_gpiod_get_optional(dev, "reset",
@@ -770,7 +760,7 @@ static int ov2680_probe(struct i2c_client *client)
 	if (!sensor)
 		return -ENOMEM;
 
-	sensor->i2c_client = client;
+	sensor->dev = &client->dev;
 
 	sensor->regmap = cci_regmap_init_i2c(client, 16);
 	if (IS_ERR(sensor->regmap))
@@ -796,7 +786,7 @@ static int ov2680_probe(struct i2c_client *client)
 	if (ret < 0)
 		goto lock_destroy;
 
-	ret = ov2680_v4l2_register(sensor);
+	ret = ov2680_v4l2_register(sensor, client);
 	if (ret < 0)
 		goto lock_destroy;
 
-- 
2.40.1


^ permalink raw reply related	[flat|nested] 57+ messages in thread

* [PATCH 11/28] media: ov2680: Check for "powerdown" GPIO con-id before checking for "reset" GPIO con-id
  2023-06-07 16:46 [PATCH 00/28] media: ov2680: Bugfixes + ACPI + selection(crop-tgt) API support Hans de Goede
                   ` (9 preceding siblings ...)
  2023-06-07 16:46 ` [PATCH 10/28] media: ov2680: Store dev instead of i2c_client in ov2680_dev Hans de Goede
@ 2023-06-07 16:46 ` Hans de Goede
  2023-06-07 16:46 ` [PATCH 12/28] media: ov2680: Add runtime-pm support Hans de Goede
                   ` (17 subsequent siblings)
  28 siblings, 0 replies; 57+ messages in thread
From: Hans de Goede @ 2023-06-07 16:46 UTC (permalink / raw)
  To: Daniel Scally, Laurent Pinchart, Sakari Ailus, Andy Shevchenko
  Cc: Hans de Goede, Mauro Carvalho Chehab, Kate Hsuan, linux-media

The datasheet of the OV2680 labels the single GPIO to put the sensor in
powersaving mode as XSHUTDN aka shutdown, _not_ reset.

This is important because some boards have standardized sensor connectors
which allow connecting various sensor modules. These connectors have both
reset and powerdown signals and the powerdown signal is routed to
the OV2680's XSHUTDN pin.

On x86/ACPI multiple Bay Trail, Cherry Trail, Sky Lake and Kaby Lake models
have an OV2680 connected to the ISP2 / IPU3. On these devices the GPIOS are
not described in DT instead the GPIOs are described with an Intel specific
DSM which labels them as either powerdown or reset. Often this DSM returns
both reset and powerdown pins even though the OV2680 has only 1 such pin.

For the ov2680 driver to work on these devices it must use the GPIO with
"powerdown" as con-id, matching the XSHUTDN name from the datasheet.

As for why "powerdown" vs say "shutdown" the ACPI DSM -> con-id mapping
code is shared, so we must use standardized names and currently all of
the following sensor drivers already use "powerdown":
adv7180, gc0310, isl7998x, ov02a10, ov2659, ov5640, ov5648, ov5670,
ov5693, ov7670, ov772x, ov7740, ov8858, ov8865 and ov9650 .

Where as the hi846 driver is the lonely standout using "shutdown".

Try the "powerdown" con-id first to make things work, falling back to
"reset" to keep existing DT setups working.

Cc: Dan Scally <dan.scally@ideasonboard.com>
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
---
 drivers/media/i2c/ov2680.c | 26 +++++++++++++++++---------
 1 file changed, 17 insertions(+), 9 deletions(-)

diff --git a/drivers/media/i2c/ov2680.c b/drivers/media/i2c/ov2680.c
index e43028b1e939..bfc65c712b84 100644
--- a/drivers/media/i2c/ov2680.c
+++ b/drivers/media/i2c/ov2680.c
@@ -95,7 +95,7 @@ struct ov2680_dev {
 	u32				xvclk_freq;
 	struct regulator_bulk_data	supplies[OV2680_NUM_SUPPLIES];
 
-	struct gpio_desc		*reset_gpio;
+	struct gpio_desc		*pwdn_gpio;
 	struct mutex			lock; /* protect members */
 
 	bool				mode_pending_changes;
@@ -179,19 +179,19 @@ static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl)
 
 static void ov2680_power_up(struct ov2680_dev *sensor)
 {
-	if (!sensor->reset_gpio)
+	if (!sensor->pwdn_gpio)
 		return;
 
-	gpiod_set_value(sensor->reset_gpio, 0);
+	gpiod_set_value(sensor->pwdn_gpio, 0);
 	usleep_range(5000, 10000);
 }
 
 static void ov2680_power_down(struct ov2680_dev *sensor)
 {
-	if (!sensor->reset_gpio)
+	if (!sensor->pwdn_gpio)
 		return;
 
-	gpiod_set_value(sensor->reset_gpio, 1);
+	gpiod_set_value(sensor->pwdn_gpio, 1);
 	usleep_range(5000, 10000);
 }
 
@@ -342,7 +342,7 @@ static int ov2680_power_on(struct ov2680_dev *sensor)
 		return ret;
 	}
 
-	if (!sensor->reset_gpio) {
+	if (!sensor->pwdn_gpio) {
 		ret = cci_write(sensor->regmap, OV2680_REG_SOFT_RESET, 0x01, NULL);
 		if (ret != 0) {
 			dev_err(sensor->dev, "sensor soft reset failed\n");
@@ -726,9 +726,17 @@ static int ov2680_parse_dt(struct ov2680_dev *sensor)
 	struct device *dev = sensor->dev;
 	int ret;
 
-	sensor->reset_gpio = devm_gpiod_get_optional(dev, "reset",
-						     GPIOD_OUT_HIGH);
-	ret = PTR_ERR_OR_ZERO(sensor->reset_gpio);
+	/*
+	 * The pin we want is named XSHUTDN in the datasheet. Linux sensor
+	 * drivers have standardized on using "powerdown" as con-id name
+	 * for powerdown or shutdown pins. Older DTB files use "reset",
+	 * so fallback to that if there is no "powerdown" pin.
+	 */
+	sensor->pwdn_gpio = devm_gpiod_get_optional(dev, "powerdown", GPIOD_OUT_HIGH);
+	if (!sensor->pwdn_gpio)
+		sensor->pwdn_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
+
+	ret = PTR_ERR_OR_ZERO(sensor->pwdn_gpio);
 	if (ret < 0) {
 		dev_dbg(dev, "error while getting reset gpio: %d\n", ret);
 		return ret;
-- 
2.40.1


^ permalink raw reply related	[flat|nested] 57+ messages in thread

* [PATCH 12/28] media: ov2680: Add runtime-pm support
  2023-06-07 16:46 [PATCH 00/28] media: ov2680: Bugfixes + ACPI + selection(crop-tgt) API support Hans de Goede
                   ` (10 preceding siblings ...)
  2023-06-07 16:46 ` [PATCH 11/28] media: ov2680: Check for "powerdown" GPIO con-id before checking for "reset" GPIO con-id Hans de Goede
@ 2023-06-07 16:46 ` Hans de Goede
  2023-06-07 16:46 ` [PATCH 13/28] media: ov2680: Drop is_enabled flag Hans de Goede
                   ` (16 subsequent siblings)
  28 siblings, 0 replies; 57+ messages in thread
From: Hans de Goede @ 2023-06-07 16:46 UTC (permalink / raw)
  To: Daniel Scally, Laurent Pinchart, Sakari Ailus, Andy Shevchenko
  Cc: Hans de Goede, Mauro Carvalho Chehab, Kate Hsuan, linux-media

Remove the obsolete s_power() callback and instead use runtime-pm +
autosuspend, powering-on the sensor on s_stream(1) and releasing
the runtime-pm reference on s_stream(0).

This also removes the need for ov2680_mode_restore() instead
ov2680_stream_enable() now takes care of all sensor initalization
after power-on.

This is a preparation patch for adding ACPI support.

Note this also removes putting the clock lane into LP-11 state from
ov2680_power_on() since now streaming will start immediately after
powering on the sensor there is no need to put the clock lane
in a low power state.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
---
 drivers/media/i2c/ov2680.c | 134 ++++++++++++++++---------------------
 1 file changed, 59 insertions(+), 75 deletions(-)

diff --git a/drivers/media/i2c/ov2680.c b/drivers/media/i2c/ov2680.c
index bfc65c712b84..8a95be552633 100644
--- a/drivers/media/i2c/ov2680.c
+++ b/drivers/media/i2c/ov2680.c
@@ -17,6 +17,7 @@
 #include <linux/init.h>
 #include <linux/module.h>
 #include <linux/of_device.h>
+#include <linux/pm_runtime.h>
 #include <linux/gpio/consumer.h>
 #include <linux/regulator/consumer.h>
 
@@ -98,7 +99,6 @@ struct ov2680_dev {
 	struct gpio_desc		*pwdn_gpio;
 	struct mutex			lock; /* protect members */
 
-	bool				mode_pending_changes;
 	bool				is_enabled;
 	bool				is_streaming;
 
@@ -277,20 +277,12 @@ static int ov2680_exposure_set(struct ov2680_dev *sensor, u32 exp)
 
 static int ov2680_stream_enable(struct ov2680_dev *sensor)
 {
-	return cci_write(sensor->regmap, OV2680_REG_STREAM_CTRL, 1, NULL);
-}
+	int ret = 0;
 
-static int ov2680_stream_disable(struct ov2680_dev *sensor)
-{
-	return cci_write(sensor->regmap, OV2680_REG_STREAM_CTRL, 0, NULL);
-}
-
-static int ov2680_mode_set(struct ov2680_dev *sensor)
-{
-	int ret;
-
-	ret = cci_multi_reg_write(sensor->regmap, sensor->current_mode->reg_data,
-				  sensor->current_mode->reg_data_size, NULL);
+	cci_multi_reg_write(sensor->regmap, ov2680_mode_init_data.reg_data,
+			    ov2680_mode_init_data.reg_data_size, &ret);
+	cci_multi_reg_write(sensor->regmap, sensor->current_mode->reg_data,
+			    sensor->current_mode->reg_data_size, &ret);
 	if (ret < 0)
 		return ret;
 
@@ -299,21 +291,12 @@ static int ov2680_mode_set(struct ov2680_dev *sensor)
 	if (ret < 0)
 		return ret;
 
-	sensor->mode_pending_changes = false;
-
-	return 0;
+	return cci_write(sensor->regmap, OV2680_REG_STREAM_CTRL, 1, NULL);
 }
 
-static int ov2680_mode_restore(struct ov2680_dev *sensor)
+static int ov2680_stream_disable(struct ov2680_dev *sensor)
 {
-	int ret;
-
-	ret = cci_multi_reg_write(sensor->regmap, ov2680_mode_init_data.reg_data,
-				  ov2680_mode_init_data.reg_data_size, NULL);
-	if (ret < 0)
-		return ret;
-
-	return ov2680_mode_set(sensor);
+	return cci_write(sensor->regmap, OV2680_REG_STREAM_CTRL, 0, NULL);
 }
 
 static int ov2680_power_off(struct ov2680_dev *sensor)
@@ -360,34 +343,9 @@ static int ov2680_power_on(struct ov2680_dev *sensor)
 
 	sensor->is_enabled = true;
 
-	/* Set clock lane into LP-11 state */
-	ov2680_stream_enable(sensor);
-	usleep_range(1000, 2000);
-	ov2680_stream_disable(sensor);
-
 	return 0;
 }
 
-static int ov2680_s_power(struct v4l2_subdev *sd, int on)
-{
-	struct ov2680_dev *sensor = to_ov2680_dev(sd);
-	int ret = 0;
-
-	mutex_lock(&sensor->lock);
-
-	if (on)
-		ret = ov2680_power_on(sensor);
-	else
-		ret = ov2680_power_off(sensor);
-
-	if (on && ret == 0)
-		ret = ov2680_mode_restore(sensor);
-
-	mutex_unlock(&sensor->lock);
-
-	return ret;
-}
-
 static int ov2680_s_g_frame_interval(struct v4l2_subdev *sd,
 				     struct v4l2_subdev_frame_interval *fi)
 {
@@ -410,16 +368,20 @@ static int ov2680_s_stream(struct v4l2_subdev *sd, int enable)
 	if (sensor->is_streaming == !!enable)
 		goto unlock;
 
-	if (enable && sensor->mode_pending_changes) {
-		ret = ov2680_mode_set(sensor);
+	if (enable) {
+		ret = pm_runtime_resume_and_get(sensor->sd.dev);
 		if (ret < 0)
 			goto unlock;
-	}
 
-	if (enable)
 		ret = ov2680_stream_enable(sensor);
-	else
+		if (ret < 0) {
+			pm_runtime_put(sensor->sd.dev);
+			goto unlock;
+		}
+	} else {
 		ret = ov2680_stream_disable(sensor);
+		pm_runtime_put(sensor->sd.dev);
+	}
 
 	sensor->is_streaming = !!enable;
 
@@ -504,7 +466,6 @@ static int ov2680_set_fmt(struct v4l2_subdev *sd,
 
 	sensor->current_mode = mode;
 	sensor->fmt = format->format;
-	sensor->mode_pending_changes = true;
 
 unlock:
 	mutex_unlock(&sensor->lock);
@@ -588,10 +549,6 @@ static const struct v4l2_ctrl_ops ov2680_ctrl_ops = {
 	.s_ctrl = ov2680_s_ctrl,
 };
 
-static const struct v4l2_subdev_core_ops ov2680_core_ops = {
-	.s_power = ov2680_s_power,
-};
-
 static const struct v4l2_subdev_video_ops ov2680_video_ops = {
 	.g_frame_interval	= ov2680_s_g_frame_interval,
 	.s_frame_interval	= ov2680_s_g_frame_interval,
@@ -608,7 +565,6 @@ static const struct v4l2_subdev_pad_ops ov2680_pad_ops = {
 };
 
 static const struct v4l2_subdev_ops ov2680_subdev_ops = {
-	.core	= &ov2680_core_ops,
 	.video	= &ov2680_video_ops,
 	.pad	= &ov2680_pad_ops,
 };
@@ -627,8 +583,6 @@ static int ov2680_mode_init(struct ov2680_dev *sensor)
 
 	sensor->current_mode = init_mode;
 
-	sensor->mode_pending_changes = true;
-
 	return 0;
 }
 
@@ -704,8 +658,6 @@ static int ov2680_check_id(struct ov2680_dev *sensor)
 	u32 chip_id;
 	int ret;
 
-	ov2680_power_on(sensor);
-
 	ret = cci_read(sensor->regmap, OV2680_REG_CHIP_ID, &chip_id, NULL);
 	if (ret < 0) {
 		dev_err(sensor->dev, "failed to read chip id\n");
@@ -790,18 +742,39 @@ static int ov2680_probe(struct i2c_client *client)
 
 	mutex_init(&sensor->lock);
 
-	ret = ov2680_check_id(sensor);
+	/*
+	 * Power up and verify the chip now, so that if runtime pm is
+	 * disabled the chip is left on and streaming will work.
+	 */
+	ret = ov2680_power_on(sensor);
 	if (ret < 0)
 		goto lock_destroy;
 
+	ret = ov2680_check_id(sensor);
+	if (ret < 0)
+		goto err_powerdown;
+
+	pm_runtime_set_active(&client->dev);
+	pm_runtime_get_noresume(&client->dev);
+	pm_runtime_enable(&client->dev);
+
 	ret = ov2680_v4l2_register(sensor, client);
 	if (ret < 0)
-		goto lock_destroy;
+		goto err_pm_runtime;
+
+	pm_runtime_set_autosuspend_delay(&client->dev, 1000);
+	pm_runtime_use_autosuspend(&client->dev);
+	pm_runtime_put_autosuspend(&client->dev);
 
 	dev_info(dev, "ov2680 init correctly\n");
 
 	return 0;
 
+err_pm_runtime:
+	pm_runtime_disable(&client->dev);
+	pm_runtime_put_noidle(&client->dev);
+err_powerdown:
+	ov2680_power_off(sensor);
 lock_destroy:
 	dev_err(dev, "ov2680 init fail: %d\n", ret);
 	mutex_destroy(&sensor->lock);
@@ -818,9 +791,18 @@ static void ov2680_remove(struct i2c_client *client)
 	mutex_destroy(&sensor->lock);
 	media_entity_cleanup(&sensor->sd.entity);
 	v4l2_ctrl_handler_free(&sensor->ctrls.handler);
+
+	/*
+	 * Disable runtime PM. In case runtime PM is disabled in the kernel,
+	 * make sure to turn power off manually.
+	 */
+	pm_runtime_disable(&client->dev);
+	if (!pm_runtime_status_suspended(&client->dev))
+		ov2680_power_off(sensor);
+	pm_runtime_set_suspended(&client->dev);
 }
 
-static int __maybe_unused ov2680_suspend(struct device *dev)
+static int ov2680_suspend(struct device *dev)
 {
 	struct v4l2_subdev *sd = dev_get_drvdata(dev);
 	struct ov2680_dev *sensor = to_ov2680_dev(sd);
@@ -828,15 +810,19 @@ static int __maybe_unused ov2680_suspend(struct device *dev)
 	if (sensor->is_streaming)
 		ov2680_stream_disable(sensor);
 
-	return 0;
+	return ov2680_power_off(sensor);
 }
 
-static int __maybe_unused ov2680_resume(struct device *dev)
+static int ov2680_resume(struct device *dev)
 {
 	struct v4l2_subdev *sd = dev_get_drvdata(dev);
 	struct ov2680_dev *sensor = to_ov2680_dev(sd);
 	int ret;
 
+	ret = ov2680_power_on(sensor);
+	if (ret < 0)
+		goto stream_disable;
+
 	if (sensor->is_streaming) {
 		ret = ov2680_stream_enable(sensor);
 		if (ret < 0)
@@ -852,9 +838,7 @@ static int __maybe_unused ov2680_resume(struct device *dev)
 	return ret;
 }
 
-static const struct dev_pm_ops ov2680_pm_ops = {
-	SET_SYSTEM_SLEEP_PM_OPS(ov2680_suspend, ov2680_resume)
-};
+static DEFINE_RUNTIME_DEV_PM_OPS(ov2680_pm_ops, ov2680_suspend, ov2680_resume, NULL);
 
 static const struct of_device_id ov2680_dt_ids[] = {
 	{ .compatible = "ovti,ov2680" },
@@ -865,7 +849,7 @@ MODULE_DEVICE_TABLE(of, ov2680_dt_ids);
 static struct i2c_driver ov2680_i2c_driver = {
 	.driver = {
 		.name  = "ov2680",
-		.pm = &ov2680_pm_ops,
+		.pm = pm_sleep_ptr(&ov2680_pm_ops),
 		.of_match_table	= of_match_ptr(ov2680_dt_ids),
 	},
 	.probe_new	= ov2680_probe,
-- 
2.40.1


^ permalink raw reply related	[flat|nested] 57+ messages in thread

* [PATCH 13/28] media: ov2680: Drop is_enabled flag
  2023-06-07 16:46 [PATCH 00/28] media: ov2680: Bugfixes + ACPI + selection(crop-tgt) API support Hans de Goede
                   ` (11 preceding siblings ...)
  2023-06-07 16:46 ` [PATCH 12/28] media: ov2680: Add runtime-pm support Hans de Goede
@ 2023-06-07 16:46 ` Hans de Goede
  2023-06-07 16:46 ` [PATCH 14/28] media: ov2680: Add support for more clk setups Hans de Goede
                   ` (15 subsequent siblings)
  28 siblings, 0 replies; 57+ messages in thread
From: Hans de Goede @ 2023-06-07 16:46 UTC (permalink / raw)
  To: Daniel Scally, Laurent Pinchart, Sakari Ailus, Andy Shevchenko
  Cc: Hans de Goede, Mauro Carvalho Chehab, Kate Hsuan, linux-media

With runtime-pm it is guaranteed that ov2680_power_on() and
ov2680_power_off() will always be called in a balanced way;
and the is_enabled check in ov2680_s_ctrl() can be replaced
by checking the runtime-suspend state.

So there is no more need for the is_enabled flag, remove it.

While at it also make sure that flip control changes while
suspended still lead to the bayer-order getting updated so
that get_fmt returns the correct bayer-order.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
---
 drivers/media/i2c/ov2680.c | 36 ++++++++++++++++++------------------
 1 file changed, 18 insertions(+), 18 deletions(-)

diff --git a/drivers/media/i2c/ov2680.c b/drivers/media/i2c/ov2680.c
index 8a95be552633..d04336d9c3ce 100644
--- a/drivers/media/i2c/ov2680.c
+++ b/drivers/media/i2c/ov2680.c
@@ -99,7 +99,6 @@ struct ov2680_dev {
 	struct gpio_desc		*pwdn_gpio;
 	struct mutex			lock; /* protect members */
 
-	bool				is_enabled;
 	bool				is_streaming;
 
 	struct ov2680_ctrls		ctrls;
@@ -301,14 +300,9 @@ static int ov2680_stream_disable(struct ov2680_dev *sensor)
 
 static int ov2680_power_off(struct ov2680_dev *sensor)
 {
-	if (!sensor->is_enabled)
-		return 0;
-
 	clk_disable_unprepare(sensor->xvclk);
 	ov2680_power_down(sensor);
 	regulator_bulk_disable(OV2680_NUM_SUPPLIES, sensor->supplies);
-	sensor->is_enabled = false;
-
 	return 0;
 }
 
@@ -316,9 +310,6 @@ static int ov2680_power_on(struct ov2680_dev *sensor)
 {
 	int ret;
 
-	if (sensor->is_enabled)
-		return 0;
-
 	ret = regulator_bulk_enable(OV2680_NUM_SUPPLIES, sensor->supplies);
 	if (ret < 0) {
 		dev_err(sensor->dev, "failed to enable regulators: %d\n", ret);
@@ -341,8 +332,6 @@ static int ov2680_power_on(struct ov2680_dev *sensor)
 	if (ret < 0)
 		return ret;
 
-	sensor->is_enabled = true;
-
 	return 0;
 }
 
@@ -523,26 +512,37 @@ static int ov2680_s_ctrl(struct v4l2_ctrl *ctrl)
 {
 	struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
 	struct ov2680_dev *sensor = to_ov2680_dev(sd);
+	int ret;
 
-	if (!sensor->is_enabled)
+	/* Only apply changes to the controls if the device is powered up */
+	if (!pm_runtime_get_if_in_use(sensor->sd.dev)) {
+		ov2680_set_bayer_order(sensor, &sensor->fmt);
 		return 0;
+	}
 
 	switch (ctrl->id) {
 	case V4L2_CID_GAIN:
-		return ov2680_gain_set(sensor, ctrl->val);
+		ret = ov2680_gain_set(sensor, ctrl->val);
+		break;
 	case V4L2_CID_EXPOSURE:
-		return ov2680_exposure_set(sensor, ctrl->val);
+		ret = ov2680_exposure_set(sensor, ctrl->val);
+		break;
 	case V4L2_CID_VFLIP:
-		return ov2680_set_vflip(sensor, ctrl->val);
+		ret = ov2680_set_vflip(sensor, ctrl->val);
+		break;
 	case V4L2_CID_HFLIP:
-		return ov2680_set_hflip(sensor, ctrl->val);
+		ret = ov2680_set_hflip(sensor, ctrl->val);
+		break;
 	case V4L2_CID_TEST_PATTERN:
-		return ov2680_test_pattern_set(sensor, ctrl->val);
+		ret = ov2680_test_pattern_set(sensor, ctrl->val);
+		break;
 	default:
+		ret = -EINVAL;
 		break;
 	}
 
-	return -EINVAL;
+	pm_runtime_put(sensor->sd.dev);
+	return ret;
 }
 
 static const struct v4l2_ctrl_ops ov2680_ctrl_ops = {
-- 
2.40.1


^ permalink raw reply related	[flat|nested] 57+ messages in thread

* [PATCH 14/28] media: ov2680: Add support for more clk setups
  2023-06-07 16:46 [PATCH 00/28] media: ov2680: Bugfixes + ACPI + selection(crop-tgt) API support Hans de Goede
                   ` (12 preceding siblings ...)
  2023-06-07 16:46 ` [PATCH 13/28] media: ov2680: Drop is_enabled flag Hans de Goede
@ 2023-06-07 16:46 ` Hans de Goede
  2023-06-07 20:51   ` Andy Shevchenko
  2023-06-07 16:46 ` [PATCH 15/28] media: ov2680: Add support for 19.2 MHz clock Hans de Goede
                   ` (14 subsequent siblings)
  28 siblings, 1 reply; 57+ messages in thread
From: Hans de Goede @ 2023-06-07 16:46 UTC (permalink / raw)
  To: Daniel Scally, Laurent Pinchart, Sakari Ailus, Andy Shevchenko
  Cc: Hans de Goede, Mauro Carvalho Chehab, Kate Hsuan, linux-media

On ACPI systems the following 2 scenarios are possible:

1. The xvclk is fully controlled by ACPI powermanagement, so there
   is no "xvclk" for the driver to get (since it is abstracted away).
   In this case there will be a "clock-frequency" device property
   to tell the driver the xvclk rate.

2. There is a xvclk modelled in the clk framework for the driver,
   but the clk-generator may not be set to the right frequency
   yet. In this case there will also be a "clock-frequency" device
   property and the driver is expected to set the rate of the xvclk
   through this frequency through the clk framework.

Handle both these scenarios by switching to devm_clk_get_optional()
and checking for a "clock-frequency" device property.

This is modelled after how this same issues was fixed for the ov8865 in
commit 73dcffeb2ff9 ("media: i2c: Support 19.2MHz input clock in ov8865").

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
---
 drivers/media/i2c/ov2680.c | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)

diff --git a/drivers/media/i2c/ov2680.c b/drivers/media/i2c/ov2680.c
index d04336d9c3ce..81d59206f901 100644
--- a/drivers/media/i2c/ov2680.c
+++ b/drivers/media/i2c/ov2680.c
@@ -676,6 +676,7 @@ static int ov2680_check_id(struct ov2680_dev *sensor)
 static int ov2680_parse_dt(struct ov2680_dev *sensor)
 {
 	struct device *dev = sensor->dev;
+	unsigned int rate = 0;
 	int ret;
 
 	/*
@@ -694,13 +695,31 @@ static int ov2680_parse_dt(struct ov2680_dev *sensor)
 		return ret;
 	}
 
-	sensor->xvclk = devm_clk_get(dev, "xvclk");
+	sensor->xvclk = devm_clk_get_optional(dev, "xvclk");
 	if (IS_ERR(sensor->xvclk)) {
 		dev_err(dev, "xvclk clock missing or invalid\n");
 		return PTR_ERR(sensor->xvclk);
 	}
 
-	sensor->xvclk_freq = clk_get_rate(sensor->xvclk);
+	/*
+	 * We could have either a 24MHz or 19.2MHz clock rate from either dt or
+	 * ACPI...but we also need to support the weird IPU3 case which will
+	 * have an external clock AND a clock-frequency property. Check for the
+	 * clock-frequency property and if found, set that rate if we managed
+	 * to acquire a clock. This should cover the ACPI case. If the system
+	 * uses devicetree then the configured rate should already be set, so
+	 * we can just read it.
+	 */
+	ret = fwnode_property_read_u32(dev_fwnode(dev), "clock-frequency", &rate);
+	if (!ret && sensor->xvclk) {
+		ret = clk_set_rate(sensor->xvclk, rate);
+		if (ret)
+			return dev_err_probe(dev, ret, "failed to set clock rate\n");
+	} else if (ret && !sensor->xvclk) {
+		return dev_err_probe(dev, ret, "invalid clock config\n");
+	}
+
+	sensor->xvclk_freq = rate ? rate : clk_get_rate(sensor->xvclk);
 	if (sensor->xvclk_freq != OV2680_XVCLK_VALUE) {
 		dev_err(dev, "wrong xvclk frequency %d HZ, expected: %d Hz\n",
 			sensor->xvclk_freq, OV2680_XVCLK_VALUE);
-- 
2.40.1


^ permalink raw reply related	[flat|nested] 57+ messages in thread

* [PATCH 15/28] media: ov2680: Add support for 19.2 MHz clock
  2023-06-07 16:46 [PATCH 00/28] media: ov2680: Bugfixes + ACPI + selection(crop-tgt) API support Hans de Goede
                   ` (13 preceding siblings ...)
  2023-06-07 16:46 ` [PATCH 14/28] media: ov2680: Add support for more clk setups Hans de Goede
@ 2023-06-07 16:46 ` Hans de Goede
  2023-06-07 20:53   ` Andy Shevchenko
  2023-06-07 16:47 ` [PATCH 16/28] media: ov2680: Add endpoint matching support Hans de Goede
                   ` (13 subsequent siblings)
  28 siblings, 1 reply; 57+ messages in thread
From: Hans de Goede @ 2023-06-07 16:46 UTC (permalink / raw)
  To: Daniel Scally, Laurent Pinchart, Sakari Ailus, Andy Shevchenko
  Cc: Hans de Goede, Mauro Carvalho Chehab, Kate Hsuan, linux-media

Most x86/ACPI boards use the ov2680 with a 19.2 MHz xvclk,
rather then the expected 24MHz, add support for this.

Compensate for the lower clk by setting a higher PLL multiplier
of 69 when using 19.2 MHz vs the default multiplier of 55 for
a 24MHz xvclk.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
---
 drivers/media/i2c/ov2680.c | 35 +++++++++++++++++++++++++++++------
 1 file changed, 29 insertions(+), 6 deletions(-)

diff --git a/drivers/media/i2c/ov2680.c b/drivers/media/i2c/ov2680.c
index 81d59206f901..b3a792b28467 100644
--- a/drivers/media/i2c/ov2680.c
+++ b/drivers/media/i2c/ov2680.c
@@ -26,14 +26,13 @@
 #include <media/v4l2-ctrls.h>
 #include <media/v4l2-subdev.h>
 
-#define OV2680_XVCLK_VALUE			24000000
-
 #define OV2680_CHIP_ID				0x2680
 
 #define OV2680_REG_STREAM_CTRL			CCI_REG8(0x0100)
 #define OV2680_REG_SOFT_RESET			CCI_REG8(0x0103)
 
 #define OV2680_REG_CHIP_ID			CCI_REG16(0x300a)
+#define OV2680_REG_PLL_MULTIPLIER		CCI_REG16(0x3081)
 
 #define OV2680_REG_EXPOSURE_PK			CCI_REG24(0x3500)
 #define OV2680_REG_R_MANUAL			CCI_REG8(0x3503)
@@ -68,6 +67,21 @@ static const char * const ov2680_supply_name[] = {
 
 #define OV2680_NUM_SUPPLIES ARRAY_SIZE(ov2680_supply_name)
 
+enum {
+	OV2680_19_2_MHZ,
+	OV2680_24_MHZ,
+};
+
+static const unsigned long ov2680_xvclk_freqs[] = {
+	[OV2680_19_2_MHZ] = 19200000,
+	[OV2680_24_MHZ] = 24000000,
+};
+
+static const u8 ov2680_pll_multipliers[] = {
+	[OV2680_19_2_MHZ] = 69,
+	[OV2680_24_MHZ] = 55,
+};
+
 struct ov2680_mode_info {
 	const char *name;
 	enum ov2680_mode_id id;
@@ -94,6 +108,7 @@ struct ov2680_dev {
 	struct media_pad		pad;
 	struct clk			*xvclk;
 	u32				xvclk_freq;
+	u8				pll_mult;
 	struct regulator_bulk_data	supplies[OV2680_NUM_SUPPLIES];
 
 	struct gpio_desc		*pwdn_gpio;
@@ -278,6 +293,7 @@ static int ov2680_stream_enable(struct ov2680_dev *sensor)
 {
 	int ret = 0;
 
+	cci_write(sensor->regmap, OV2680_REG_PLL_MULTIPLIER, sensor->pll_mult, &ret);
 	cci_multi_reg_write(sensor->regmap, ov2680_mode_init_data.reg_data,
 			    ov2680_mode_init_data.reg_data_size, &ret);
 	cci_multi_reg_write(sensor->regmap, sensor->current_mode->reg_data,
@@ -677,7 +693,7 @@ static int ov2680_parse_dt(struct ov2680_dev *sensor)
 {
 	struct device *dev = sensor->dev;
 	unsigned int rate = 0;
-	int ret;
+	int i, ret;
 
 	/*
 	 * The pin we want is named XSHUTDN in the datasheet. Linux sensor
@@ -720,12 +736,19 @@ static int ov2680_parse_dt(struct ov2680_dev *sensor)
 	}
 
 	sensor->xvclk_freq = rate ? rate : clk_get_rate(sensor->xvclk);
-	if (sensor->xvclk_freq != OV2680_XVCLK_VALUE) {
-		dev_err(dev, "wrong xvclk frequency %d HZ, expected: %d Hz\n",
-			sensor->xvclk_freq, OV2680_XVCLK_VALUE);
+
+	for (i = 0; i < ARRAY_SIZE(ov2680_xvclk_freqs); i++) {
+		if (sensor->xvclk_freq == ov2680_xvclk_freqs[i])
+			break;
+	}
+
+	if (i == ARRAY_SIZE(ov2680_xvclk_freqs)) {
+		dev_err(dev, "unsupported xvclk frequency %d Hz\n", sensor->xvclk_freq);
 		return -EINVAL;
 	}
 
+	sensor->pll_mult = ov2680_pll_multipliers[i];
+
 	return 0;
 }
 
-- 
2.40.1


^ permalink raw reply related	[flat|nested] 57+ messages in thread

* [PATCH 16/28] media: ov2680: Add endpoint matching support
  2023-06-07 16:46 [PATCH 00/28] media: ov2680: Bugfixes + ACPI + selection(crop-tgt) API support Hans de Goede
                   ` (14 preceding siblings ...)
  2023-06-07 16:46 ` [PATCH 15/28] media: ov2680: Add support for 19.2 MHz clock Hans de Goede
@ 2023-06-07 16:47 ` Hans de Goede
  2023-06-07 16:47 ` [PATCH 17/28] media: ov2680: Add support for ACPI enumeration Hans de Goede
                   ` (12 subsequent siblings)
  28 siblings, 0 replies; 57+ messages in thread
From: Hans de Goede @ 2023-06-07 16:47 UTC (permalink / raw)
  To: Daniel Scally, Laurent Pinchart, Sakari Ailus, Andy Shevchenko
  Cc: Hans de Goede, Mauro Carvalho Chehab, Kate Hsuan, linux-media

Add endpoint matching support and defer probe() until
the endpoint fwnode is available. This is necessary on ACPI
platforms where the bridge code creating the fwnodes may also e.g.
set the "clock-frequency" device property and add GPIO mappings.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
---
 drivers/media/i2c/ov2680.c | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)

diff --git a/drivers/media/i2c/ov2680.c b/drivers/media/i2c/ov2680.c
index b3a792b28467..9fa92b4f1307 100644
--- a/drivers/media/i2c/ov2680.c
+++ b/drivers/media/i2c/ov2680.c
@@ -102,6 +102,7 @@ struct ov2680_ctrls {
 
 struct ov2680_dev {
 	struct device			*dev;
+	struct fwnode_handle		*ep_fwnode;
 	struct regmap			*regmap;
 	struct v4l2_subdev		sd;
 
@@ -614,6 +615,7 @@ static int ov2680_v4l2_register(struct ov2680_dev *sensor, struct i2c_client *cl
 	sensor->sd.flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
 	sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
 	sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
+	sensor->sd.fwnode = sensor->ep_fwnode;
 
 	ret = media_entity_pads_init(&sensor->sd.entity, 1, &sensor->pad);
 	if (ret < 0)
@@ -768,29 +770,37 @@ static int ov2680_probe(struct i2c_client *client)
 	if (IS_ERR(sensor->regmap))
 		return PTR_ERR(sensor->regmap);
 
+	/*
+	 * Sometimes the fwnode graph is initialized by the bridge driver.
+	 * Bridge drivers doing this may also add GPIO mappings, wait for this.
+	 */
+	sensor->ep_fwnode = fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL);
+	if (!sensor->ep_fwnode)
+		return dev_err_probe(dev, -EPROBE_DEFER, "waiting for fwnode graph endpoint\n");
+
+	mutex_init(&sensor->lock);
+
 	ret = ov2680_parse_dt(sensor);
 	if (ret < 0)
-		return -EINVAL;
+		goto err_fwnode_put;
 
 	ret = ov2680_mode_init(sensor);
 	if (ret < 0)
-		return ret;
+		goto err_fwnode_put;
 
 	ret = ov2680_get_regulators(sensor);
 	if (ret < 0) {
 		dev_err(dev, "failed to get regulators\n");
-		return ret;
+		goto err_fwnode_put;
 	}
 
-	mutex_init(&sensor->lock);
-
 	/*
 	 * Power up and verify the chip now, so that if runtime pm is
 	 * disabled the chip is left on and streaming will work.
 	 */
 	ret = ov2680_power_on(sensor);
 	if (ret < 0)
-		goto lock_destroy;
+		goto err_fwnode_put;
 
 	ret = ov2680_check_id(sensor);
 	if (ret < 0)
@@ -817,9 +827,10 @@ static int ov2680_probe(struct i2c_client *client)
 	pm_runtime_put_noidle(&client->dev);
 err_powerdown:
 	ov2680_power_off(sensor);
-lock_destroy:
+err_fwnode_put:
 	dev_err(dev, "ov2680 init fail: %d\n", ret);
 	mutex_destroy(&sensor->lock);
+	fwnode_handle_put(sensor->ep_fwnode);
 
 	return ret;
 }
@@ -833,6 +844,7 @@ static void ov2680_remove(struct i2c_client *client)
 	mutex_destroy(&sensor->lock);
 	media_entity_cleanup(&sensor->sd.entity);
 	v4l2_ctrl_handler_free(&sensor->ctrls.handler);
+	fwnode_handle_put(sensor->ep_fwnode);
 
 	/*
 	 * Disable runtime PM. In case runtime PM is disabled in the kernel,
-- 
2.40.1


^ permalink raw reply related	[flat|nested] 57+ messages in thread

* [PATCH 17/28] media: ov2680: Add support for ACPI enumeration
  2023-06-07 16:46 [PATCH 00/28] media: ov2680: Bugfixes + ACPI + selection(crop-tgt) API support Hans de Goede
                   ` (15 preceding siblings ...)
  2023-06-07 16:47 ` [PATCH 16/28] media: ov2680: Add endpoint matching support Hans de Goede
@ 2023-06-07 16:47 ` Hans de Goede
  2023-06-07 20:57   ` Andy Shevchenko
  2023-06-07 16:47 ` [PATCH 18/28] media: ov2680: Fix ov2680_enum_frame_interval() Hans de Goede
                   ` (11 subsequent siblings)
  28 siblings, 1 reply; 57+ messages in thread
From: Hans de Goede @ 2023-06-07 16:47 UTC (permalink / raw)
  To: Daniel Scally, Laurent Pinchart, Sakari Ailus, Andy Shevchenko
  Cc: Hans de Goede, Mauro Carvalho Chehab, Kate Hsuan, linux-media

Add an acpi_match_table now that all the other bits necessary for
ACPI support are in place.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
---
 drivers/media/i2c/ov2680.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/drivers/media/i2c/ov2680.c b/drivers/media/i2c/ov2680.c
index 9fa92b4f1307..190c58caae11 100644
--- a/drivers/media/i2c/ov2680.c
+++ b/drivers/media/i2c/ov2680.c
@@ -900,11 +900,18 @@ static const struct of_device_id ov2680_dt_ids[] = {
 };
 MODULE_DEVICE_TABLE(of, ov2680_dt_ids);
 
+static const struct acpi_device_id ov2680_acpi_ids[] = {
+	{ "OVTI2680" },
+	{ /* sentinel */ },
+};
+MODULE_DEVICE_TABLE(acpi, ov2680_acpi_ids);
+
 static struct i2c_driver ov2680_i2c_driver = {
 	.driver = {
 		.name  = "ov2680",
 		.pm = pm_sleep_ptr(&ov2680_pm_ops),
 		.of_match_table	= of_match_ptr(ov2680_dt_ids),
+		.acpi_match_table = ov2680_acpi_ids,
 	},
 	.probe_new	= ov2680_probe,
 	.remove		= ov2680_remove,
-- 
2.40.1


^ permalink raw reply related	[flat|nested] 57+ messages in thread

* [PATCH 18/28] media: ov2680: Fix ov2680_enum_frame_interval()
  2023-06-07 16:46 [PATCH 00/28] media: ov2680: Bugfixes + ACPI + selection(crop-tgt) API support Hans de Goede
                   ` (16 preceding siblings ...)
  2023-06-07 16:47 ` [PATCH 17/28] media: ov2680: Add support for ACPI enumeration Hans de Goede
@ 2023-06-07 16:47 ` Hans de Goede
  2023-06-07 16:47 ` [PATCH 19/28] media: ov2680: Annotate the per mode register setting lists Hans de Goede
                   ` (10 subsequent siblings)
  28 siblings, 0 replies; 57+ messages in thread
From: Hans de Goede @ 2023-06-07 16:47 UTC (permalink / raw)
  To: Daniel Scally, Laurent Pinchart, Sakari Ailus, Andy Shevchenko
  Cc: Hans de Goede, Mauro Carvalho Chehab, Kate Hsuan, linux-media

Fix and simplify ov2680_enum_frame_interval(), the index is not
an index into ov2680_mode_data[], so using OV2680_MODE_MAX is wrong.

Instead it is an index indexing the different framerates for
the resolution specified in fie->width, fie->height.

Since the ov2680 code only supports a single fixed framerate,
index must always be 0 and we don't need to check the other
fie input values.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
---
 drivers/media/i2c/ov2680.c | 16 ++++++----------
 1 file changed, 6 insertions(+), 10 deletions(-)

diff --git a/drivers/media/i2c/ov2680.c b/drivers/media/i2c/ov2680.c
index 190c58caae11..bc76ebee4854 100644
--- a/drivers/media/i2c/ov2680.c
+++ b/drivers/media/i2c/ov2680.c
@@ -507,20 +507,16 @@ static int ov2680_enum_frame_size(struct v4l2_subdev *sd,
 }
 
 static int ov2680_enum_frame_interval(struct v4l2_subdev *sd,
-			      struct v4l2_subdev_state *sd_state,
-			      struct v4l2_subdev_frame_interval_enum *fie)
+				      struct v4l2_subdev_state *sd_state,
+				      struct v4l2_subdev_frame_interval_enum *fie)
 {
-	struct v4l2_fract tpf;
+	struct ov2680_dev *sensor = to_ov2680_dev(sd);
 
-	if (fie->index >= OV2680_MODE_MAX || fie->width > OV2680_WIDTH_MAX ||
-	    fie->height > OV2680_HEIGHT_MAX ||
-	    fie->which > V4L2_SUBDEV_FORMAT_ACTIVE)
+	/* Only 1 framerate */
+	if (fie->index)
 		return -EINVAL;
 
-	tpf.denominator = OV2680_FRAME_RATE;
-	tpf.numerator = 1;
-
-	fie->interval = tpf;
+	fie->interval = sensor->frame_interval;
 
 	return 0;
 }
-- 
2.40.1


^ permalink raw reply related	[flat|nested] 57+ messages in thread

* [PATCH 19/28] media: ov2680: Annotate the per mode register setting lists
  2023-06-07 16:46 [PATCH 00/28] media: ov2680: Bugfixes + ACPI + selection(crop-tgt) API support Hans de Goede
                   ` (17 preceding siblings ...)
  2023-06-07 16:47 ` [PATCH 18/28] media: ov2680: Fix ov2680_enum_frame_interval() Hans de Goede
@ 2023-06-07 16:47 ` Hans de Goede
  2023-06-07 16:47 ` [PATCH 20/28] media: ov2680: Add ov2680_mode struct Hans de Goede
                   ` (9 subsequent siblings)
  28 siblings, 0 replies; 57+ messages in thread
From: Hans de Goede @ 2023-06-07 16:47 UTC (permalink / raw)
  To: Daniel Scally, Laurent Pinchart, Sakari Ailus, Andy Shevchenko
  Cc: Hans de Goede, Mauro Carvalho Chehab, Kate Hsuan, linux-media

Annotate the per mode register setting lists.

This is a preparation patch for moving to calculating the per mode
settings, allowing to set any mode through cropping.

The annotations make it easier to see which registers are mode
dependent and which are fixed.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
---
 drivers/media/i2c/ov2680.c | 118 ++++++++++++++++++++++++++++++++-----
 1 file changed, 104 insertions(+), 14 deletions(-)

diff --git a/drivers/media/i2c/ov2680.c b/drivers/media/i2c/ov2680.c
index bc76ebee4854..cb6ec2272376 100644
--- a/drivers/media/i2c/ov2680.c
+++ b/drivers/media/i2c/ov2680.c
@@ -140,27 +140,117 @@ static const int ov2680_hv_flip_bayer_order[] = {
 };
 
 static const struct reg_sequence ov2680_setting_30fps_QUXGA_800_600[] = {
-	{0x3086, 0x01}, {0x370a, 0x23}, {0x3808, 0x03}, {0x3809, 0x20},
-	{0x380a, 0x02}, {0x380b, 0x58}, {0x380c, 0x06}, {0x380d, 0xac},
-	{0x380e, 0x02}, {0x380f, 0x84}, {0x3811, 0x04}, {0x3813, 0x04},
-	{0x3814, 0x31}, {0x3815, 0x31}, {0x3820, 0xc0}, {0x4008, 0x00},
-	{0x4009, 0x03}, {0x4837, 0x1e}, {0x3501, 0x4e}, {0x3502, 0xe0},
+	/* Set PLL SP DIV to 1 for binning mode */
+	{0x3086, 0x01},
+
+	/* Sensor control register 0x0a to 0x23 for binning mode */
+	{0x370a, 0x23},
+
+	/* Set X and Y output size to 800x600 */
+	{0x3808, 0x03},
+	{0x3809, 0x20},
+	{0x380a, 0x02},
+	{0x380b, 0x58},
+
+	/* Set HTS + VTS to 1708x644 */
+	{0x380c, 0x06},
+	{0x380d, 0xac},
+	{0x380e, 0x02},
+	{0x380f, 0x84},
+
+	/* Set ISP WIN X and Y start to 4x4 */
+	{0x3811, 0x04},
+	{0x3813, 0x04},
+
+	/* Set X INC and Y INC for binning */
+	{0x3814, 0x31},
+	{0x3815, 0x31},
+
+	/* Initialize FORMAT1 to default/reset value (vflip disabled) */
+	{0x3820, 0xc0},
+
+	/* Set black level compensation range to 0 - 3 (default 0 - 11) */
+	{0x4008, 0x00},
+	{0x4009, 0x03},
+
+	/* Set MIPI pclk period to 0x1e (default/reset is 0x18) */
+	{0x4837, 0x1e},
+
+	/* Initialize exposure to 0x4ee (immediately overridden by the ctrl, drop this */
+	{0x3501, 0x4e},
+	{0x3502, 0xe0},
+
+	/* R MANUAL set exposure (0x01) and gain (0x02) to manual (hw does not do auto) */
 	{0x3503, 0x03},
 };
 
 static const struct reg_sequence ov2680_setting_30fps_720P_1280_720[] = {
-	{0x3086, 0x00}, {0x3808, 0x05}, {0x3809, 0x00}, {0x380a, 0x02},
-	{0x380b, 0xd0}, {0x380c, 0x06}, {0x380d, 0xa8}, {0x380e, 0x05},
-	{0x380f, 0x0e}, {0x3811, 0x08}, {0x3813, 0x06}, {0x3814, 0x11},
-	{0x3815, 0x11}, {0x3820, 0xc0}, {0x4008, 0x00},
+	/* Set PLL SP DIV to 0 for not binning mode */
+	{0x3086, 0x00},
+
+	/* Set X and Y output size to 1280x720 */
+	{0x3808, 0x05},
+	{0x3809, 0x00},
+	{0x380a, 0x02},
+	{0x380b, 0xd0},
+
+	/* Set HTS + VTS to 1704x1294 */
+	{0x380c, 0x06},
+	{0x380d, 0xa8},
+	{0x380e, 0x05},
+	{0x380f, 0x0e},
+
+	/* Set ISP WIN X and Y start to 8x6 */
+	{0x3811, 0x08},
+	{0x3813, 0x06},
+
+	/* Set X INC and Y INC for non binning */
+	{0x3814, 0x11},
+	{0x3815, 0x11},
+
+	/* Initialize FORMAT1 to default/reset value (vflip disabled) */
+	{0x3820, 0xc0},
+
+	/* Set backlight compensation range start to 0 */
+	{0x4008, 0x00},
 };
 
 static const struct reg_sequence ov2680_setting_30fps_UXGA_1600_1200[] = {
-	{0x3086, 0x00}, {0x3501, 0x4e}, {0x3502, 0xe0}, {0x3808, 0x06},
-	{0x3809, 0x40}, {0x380a, 0x04}, {0x380b, 0xb0}, {0x380c, 0x06},
-	{0x380d, 0xa8}, {0x380e, 0x05}, {0x380f, 0x0e}, {0x3811, 0x00},
-	{0x3813, 0x00}, {0x3814, 0x11}, {0x3815, 0x11}, {0x3820, 0xc0},
-	{0x4008, 0x00}, {0x4837, 0x18}
+	/* Set PLL SP DIV to 0 for not binning mode */
+	{0x3086, 0x00},
+
+	/* Initialize exposure to 0x4ee (immediately overridden by the ctrl, drop this */
+	{0x3501, 0x4e},
+	{0x3502, 0xe0},
+
+	/* Set X and Y output size to 1600x1200 */
+	{0x3808, 0x06},
+	{0x3809, 0x40},
+	{0x380a, 0x04},
+	{0x380b, 0xb0},
+
+	/* Set HTS + VTS to 1704x1294 */
+	{0x380c, 0x06},
+	{0x380d, 0xa8},
+	{0x380e, 0x05},
+	{0x380f, 0x0e},
+
+	/* Set ISP WIN X and Y start to 0x0 */
+	{0x3811, 0x00},
+	{0x3813, 0x00},
+
+	/* Set X INC and Y INC for non binning */
+	{0x3814, 0x11},
+	{0x3815, 0x11},
+
+	/* Initialize FORMAT1 to default/reset value (vflip disabled) */
+	{0x3820, 0xc0},
+
+	/* Set backlight compensation range start to 0 */
+	{0x4008, 0x00},
+
+	/* Set MIPI pclk period to default/reset value of 0x18 */
+	{0x4837, 0x18}
 };
 
 static const struct ov2680_mode_info ov2680_mode_init_data = {
-- 
2.40.1


^ permalink raw reply related	[flat|nested] 57+ messages in thread

* [PATCH 20/28] media: ov2680: Add ov2680_mode struct
  2023-06-07 16:46 [PATCH 00/28] media: ov2680: Bugfixes + ACPI + selection(crop-tgt) API support Hans de Goede
                   ` (18 preceding siblings ...)
  2023-06-07 16:47 ` [PATCH 19/28] media: ov2680: Annotate the per mode register setting lists Hans de Goede
@ 2023-06-07 16:47 ` Hans de Goede
  2023-06-07 16:47 ` [PATCH 21/28] media: ov2680: Make setting the mode algorithm based Hans de Goede
                   ` (8 subsequent siblings)
  28 siblings, 0 replies; 57+ messages in thread
From: Hans de Goede @ 2023-06-07 16:47 UTC (permalink / raw)
  To: Daniel Scally, Laurent Pinchart, Sakari Ailus, Andy Shevchenko
  Cc: Hans de Goede, Mauro Carvalho Chehab, Kate Hsuan, linux-media

Add an ov2680_mode struct to group together mode related state.

For now this only containst the v4l2_mbus_framefmt and
the frame_interval.

This is a preparation patch for moving to calculating the per mode
settings, which will store more info in the new ov2680_mode struct.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
---
 drivers/media/i2c/ov2680.c | 30 +++++++++++++++++-------------
 1 file changed, 17 insertions(+), 13 deletions(-)

diff --git a/drivers/media/i2c/ov2680.c b/drivers/media/i2c/ov2680.c
index cb6ec2272376..563b6645fea1 100644
--- a/drivers/media/i2c/ov2680.c
+++ b/drivers/media/i2c/ov2680.c
@@ -100,6 +100,11 @@ struct ov2680_ctrls {
 	struct v4l2_ctrl *test_pattern;
 };
 
+struct ov2680_mode {
+	struct v4l2_mbus_framefmt	fmt;
+	struct v4l2_fract		frame_interval;
+};
+
 struct ov2680_dev {
 	struct device			*dev;
 	struct fwnode_handle		*ep_fwnode;
@@ -118,8 +123,7 @@ struct ov2680_dev {
 	bool				is_streaming;
 
 	struct ov2680_ctrls		ctrls;
-	struct v4l2_mbus_framefmt	fmt;
-	struct v4l2_fract		frame_interval;
+	struct ov2680_mode		mode;
 
 	const struct ov2680_mode_info	*current_mode;
 };
@@ -337,7 +341,7 @@ static int ov2680_set_vflip(struct ov2680_dev *sensor, s32 val)
 	if (ret < 0)
 		return ret;
 
-	ov2680_set_bayer_order(sensor, &sensor->fmt);
+	ov2680_set_bayer_order(sensor, &sensor->mode.fmt);
 	return 0;
 }
 
@@ -353,7 +357,7 @@ static int ov2680_set_hflip(struct ov2680_dev *sensor, s32 val)
 	if (ret < 0)
 		return ret;
 
-	ov2680_set_bayer_order(sensor, &sensor->fmt);
+	ov2680_set_bayer_order(sensor, &sensor->mode.fmt);
 	return 0;
 }
 
@@ -448,7 +452,7 @@ static int ov2680_s_g_frame_interval(struct v4l2_subdev *sd,
 	struct ov2680_dev *sensor = to_ov2680_dev(sd);
 
 	mutex_lock(&sensor->lock);
-	fi->interval = sensor->frame_interval;
+	fi->interval = sensor->mode.frame_interval;
 	mutex_unlock(&sensor->lock);
 
 	return 0;
@@ -496,7 +500,7 @@ static int ov2680_enum_mbus_code(struct v4l2_subdev *sd,
 	if (code->pad != 0 || code->index != 0)
 		return -EINVAL;
 
-	code->code = sensor->fmt.code;
+	code->code = sensor->mode.fmt.code;
 
 	return 0;
 }
@@ -517,7 +521,7 @@ static int ov2680_get_fmt(struct v4l2_subdev *sd,
 		fmt = v4l2_subdev_get_try_format(&sensor->sd, sd_state,
 						 format->pad);
 	} else {
-		fmt = &sensor->fmt;
+		fmt = &sensor->mode.fmt;
 	}
 
 	format->format = *fmt;
@@ -561,7 +565,7 @@ static int ov2680_set_fmt(struct v4l2_subdev *sd,
 	}
 
 	sensor->current_mode = mode;
-	sensor->fmt = format->format;
+	sensor->mode.fmt = format->format;
 
 unlock:
 	mutex_unlock(&sensor->lock);
@@ -606,7 +610,7 @@ static int ov2680_enum_frame_interval(struct v4l2_subdev *sd,
 	if (fie->index)
 		return -EINVAL;
 
-	fie->interval = sensor->frame_interval;
+	fie->interval = sensor->mode.frame_interval;
 
 	return 0;
 }
@@ -619,7 +623,7 @@ static int ov2680_s_ctrl(struct v4l2_ctrl *ctrl)
 
 	/* Only apply changes to the controls if the device is powered up */
 	if (!pm_runtime_get_if_in_use(sensor->sd.dev)) {
-		ov2680_set_bayer_order(sensor, &sensor->fmt);
+		ov2680_set_bayer_order(sensor, &sensor->mode.fmt);
 		return 0;
 	}
 
@@ -677,10 +681,10 @@ static int ov2680_mode_init(struct ov2680_dev *sensor)
 	const struct ov2680_mode_info *init_mode;
 
 	/* set initial mode */
-	ov2680_fill_format(sensor, &sensor->fmt, OV2680_DEFAULT_WIDTH, OV2680_DEFAULT_HEIGHT);
+	ov2680_fill_format(sensor, &sensor->mode.fmt, OV2680_DEFAULT_WIDTH, OV2680_DEFAULT_HEIGHT);
 
-	sensor->frame_interval.denominator = OV2680_FRAME_RATE;
-	sensor->frame_interval.numerator = 1;
+	sensor->mode.frame_interval.denominator = OV2680_FRAME_RATE;
+	sensor->mode.frame_interval.numerator = 1;
 
 	init_mode = &ov2680_mode_init_data;
 
-- 
2.40.1


^ permalink raw reply related	[flat|nested] 57+ messages in thread

* [PATCH 21/28] media: ov2680: Make setting the mode algorithm based
  2023-06-07 16:46 [PATCH 00/28] media: ov2680: Bugfixes + ACPI + selection(crop-tgt) API support Hans de Goede
                   ` (19 preceding siblings ...)
  2023-06-07 16:47 ` [PATCH 20/28] media: ov2680: Add ov2680_mode struct Hans de Goede
@ 2023-06-07 16:47 ` Hans de Goede
  2023-06-07 16:47 ` [PATCH 22/28] media: ov2680: Add an __ov2680_get_pad_format() helper function Hans de Goede
                   ` (7 subsequent siblings)
  28 siblings, 0 replies; 57+ messages in thread
From: Hans de Goede @ 2023-06-07 16:47 UTC (permalink / raw)
  To: Daniel Scally, Laurent Pinchart, Sakari Ailus, Andy Shevchenko
  Cc: Hans de Goede, Mauro Carvalho Chehab, Kate Hsuan, linux-media

Instead of using a long fixed register settings list for each resolution,
calculate the register settings based on the requested width + height.

This is based on atomisp-ov2680 commit 0611888592df ("media: atomisp:
ov2680: Make setting the modes algorithm based").

This will allow future enhancements like adding hblank and vblank controls
and adding selection support.

This also adds properly prgramming the ISP window and setting
the manual ISP window control bit in register 0x5708, this is
necessary for the hflip and vflip conrols to work properly.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
---
 drivers/media/i2c/ov2680.c | 302 ++++++++++++++++---------------------
 1 file changed, 133 insertions(+), 169 deletions(-)

diff --git a/drivers/media/i2c/ov2680.c b/drivers/media/i2c/ov2680.c
index 563b6645fea1..b0a8322696ee 100644
--- a/drivers/media/i2c/ov2680.c
+++ b/drivers/media/i2c/ov2680.c
@@ -37,28 +37,44 @@
 #define OV2680_REG_EXPOSURE_PK			CCI_REG24(0x3500)
 #define OV2680_REG_R_MANUAL			CCI_REG8(0x3503)
 #define OV2680_REG_GAIN_PK			CCI_REG16(0x350a)
+
+#define OV2680_REG_SENSOR_CTRL_0A		CCI_REG8(0x370a)
+
+#define OV2680_REG_HORIZONTAL_START		CCI_REG16(0x3800)
+#define OV2680_REG_VERTICAL_START		CCI_REG16(0x3802)
+#define OV2680_REG_HORIZONTAL_END		CCI_REG16(0x3804)
+#define OV2680_REG_VERTICAL_END			CCI_REG16(0x3806)
+#define OV2680_REG_HORIZONTAL_OUTPUT_SIZE	CCI_REG16(0x3808)
+#define OV2680_REG_VERTICAL_OUTPUT_SIZE		CCI_REG16(0x380a)
 #define OV2680_REG_TIMING_HTS			CCI_REG16(0x380c)
 #define OV2680_REG_TIMING_VTS			CCI_REG16(0x380e)
+#define OV2680_REG_ISP_X_WIN			CCI_REG16(0x3810)
+#define OV2680_REG_ISP_Y_WIN			CCI_REG16(0x3812)
+#define OV2680_REG_X_INC			CCI_REG8(0x3814)
+#define OV2680_REG_Y_INC			CCI_REG8(0x3815)
 #define OV2680_REG_FORMAT1			CCI_REG8(0x3820)
 #define OV2680_REG_FORMAT2			CCI_REG8(0x3821)
 
 #define OV2680_REG_ISP_CTRL00			CCI_REG8(0x5080)
 
+#define OV2680_REG_X_WIN			CCI_REG16(0x5704)
+#define OV2680_REG_Y_WIN			CCI_REG16(0x5706)
+
 #define OV2680_FRAME_RATE			30
 
-#define OV2680_WIDTH_MAX			1600
-#define OV2680_HEIGHT_MAX			1200
+#define OV2680_NATIVE_WIDTH			1616
+#define OV2680_NATIVE_HEIGHT			1216
+
+/* 66MHz pixel clock: 66MHz / 1704 * 1294 = 30fps */
+#define OV2680_PIXELS_PER_LINE			1704
+#define OV2680_LINES_PER_FRAME			1294
+
+/* If possible send 16 extra rows / lines to the ISP as padding */
+#define OV2680_END_MARGIN			16
 
 #define OV2680_DEFAULT_WIDTH			800
 #define OV2680_DEFAULT_HEIGHT			600
 
-enum ov2680_mode_id {
-	OV2680_MODE_QUXGA_800_600,
-	OV2680_MODE_720P_1280_720,
-	OV2680_MODE_UXGA_1600_1200,
-	OV2680_MODE_MAX,
-};
-
 static const char * const ov2680_supply_name[] = {
 	"DOVDD",
 	"DVDD",
@@ -82,15 +98,6 @@ static const u8 ov2680_pll_multipliers[] = {
 	[OV2680_24_MHZ] = 55,
 };
 
-struct ov2680_mode_info {
-	const char *name;
-	enum ov2680_mode_id id;
-	u32 width;
-	u32 height;
-	const struct reg_sequence *reg_data;
-	u32 reg_data_size;
-};
-
 struct ov2680_ctrls {
 	struct v4l2_ctrl_handler handler;
 	struct v4l2_ctrl *exposure;
@@ -103,6 +110,15 @@ struct ov2680_ctrls {
 struct ov2680_mode {
 	struct v4l2_mbus_framefmt	fmt;
 	struct v4l2_fract		frame_interval;
+	bool				binning;
+	u16				h_start;
+	u16				v_start;
+	u16				h_end;
+	u16				v_end;
+	u16				h_output_size;
+	u16				v_output_size;
+	u16				hts;
+	u16				vts;
 };
 
 struct ov2680_dev {
@@ -124,8 +140,6 @@ struct ov2680_dev {
 
 	struct ov2680_ctrls		ctrls;
 	struct ov2680_mode		mode;
-
-	const struct ov2680_mode_info	*current_mode;
 };
 
 static const char * const test_pattern_menu[] = {
@@ -143,136 +157,19 @@ static const int ov2680_hv_flip_bayer_order[] = {
 	MEDIA_BUS_FMT_SRGGB10_1X10,
 };
 
-static const struct reg_sequence ov2680_setting_30fps_QUXGA_800_600[] = {
-	/* Set PLL SP DIV to 1 for binning mode */
-	{0x3086, 0x01},
-
-	/* Sensor control register 0x0a to 0x23 for binning mode */
-	{0x370a, 0x23},
-
-	/* Set X and Y output size to 800x600 */
-	{0x3808, 0x03},
-	{0x3809, 0x20},
-	{0x380a, 0x02},
-	{0x380b, 0x58},
-
-	/* Set HTS + VTS to 1708x644 */
-	{0x380c, 0x06},
-	{0x380d, 0xac},
-	{0x380e, 0x02},
-	{0x380f, 0x84},
-
-	/* Set ISP WIN X and Y start to 4x4 */
-	{0x3811, 0x04},
-	{0x3813, 0x04},
-
-	/* Set X INC and Y INC for binning */
-	{0x3814, 0x31},
-	{0x3815, 0x31},
-
-	/* Initialize FORMAT1 to default/reset value (vflip disabled) */
-	{0x3820, 0xc0},
+static const struct reg_sequence ov2680_global_setting[] = {
+	/* R MANUAL set exposure (0x01) and gain (0x02) to manual (hw does not do auto) */
+	{0x3503, 0x03},
 
 	/* Set black level compensation range to 0 - 3 (default 0 - 11) */
 	{0x4008, 0x00},
 	{0x4009, 0x03},
 
-	/* Set MIPI pclk period to 0x1e (default/reset is 0x18) */
-	{0x4837, 0x1e},
-
-	/* Initialize exposure to 0x4ee (immediately overridden by the ctrl, drop this */
-	{0x3501, 0x4e},
-	{0x3502, 0xe0},
-
-	/* R MANUAL set exposure (0x01) and gain (0x02) to manual (hw does not do auto) */
-	{0x3503, 0x03},
-};
-
-static const struct reg_sequence ov2680_setting_30fps_720P_1280_720[] = {
-	/* Set PLL SP DIV to 0 for not binning mode */
-	{0x3086, 0x00},
-
-	/* Set X and Y output size to 1280x720 */
-	{0x3808, 0x05},
-	{0x3809, 0x00},
-	{0x380a, 0x02},
-	{0x380b, 0xd0},
-
-	/* Set HTS + VTS to 1704x1294 */
-	{0x380c, 0x06},
-	{0x380d, 0xa8},
-	{0x380e, 0x05},
-	{0x380f, 0x0e},
-
-	/* Set ISP WIN X and Y start to 8x6 */
-	{0x3811, 0x08},
-	{0x3813, 0x06},
-
-	/* Set X INC and Y INC for non binning */
-	{0x3814, 0x11},
-	{0x3815, 0x11},
-
-	/* Initialize FORMAT1 to default/reset value (vflip disabled) */
-	{0x3820, 0xc0},
-
-	/* Set backlight compensation range start to 0 */
-	{0x4008, 0x00},
-};
-
-static const struct reg_sequence ov2680_setting_30fps_UXGA_1600_1200[] = {
-	/* Set PLL SP DIV to 0 for not binning mode */
-	{0x3086, 0x00},
-
-	/* Initialize exposure to 0x4ee (immediately overridden by the ctrl, drop this */
-	{0x3501, 0x4e},
-	{0x3502, 0xe0},
-
-	/* Set X and Y output size to 1600x1200 */
-	{0x3808, 0x06},
-	{0x3809, 0x40},
-	{0x380a, 0x04},
-	{0x380b, 0xb0},
-
-	/* Set HTS + VTS to 1704x1294 */
-	{0x380c, 0x06},
-	{0x380d, 0xa8},
-	{0x380e, 0x05},
-	{0x380f, 0x0e},
-
-	/* Set ISP WIN X and Y start to 0x0 */
-	{0x3811, 0x00},
-	{0x3813, 0x00},
-
-	/* Set X INC and Y INC for non binning */
-	{0x3814, 0x11},
-	{0x3815, 0x11},
-
-	/* Initialize FORMAT1 to default/reset value (vflip disabled) */
-	{0x3820, 0xc0},
-
-	/* Set backlight compensation range start to 0 */
-	{0x4008, 0x00},
-
-	/* Set MIPI pclk period to default/reset value of 0x18 */
-	{0x4837, 0x18}
-};
-
-static const struct ov2680_mode_info ov2680_mode_init_data = {
-	"mode_quxga_800_600", OV2680_MODE_QUXGA_800_600, 800, 600,
-	ov2680_setting_30fps_QUXGA_800_600,
-	ARRAY_SIZE(ov2680_setting_30fps_QUXGA_800_600),
-};
-
-static const struct ov2680_mode_info ov2680_mode_data[OV2680_MODE_MAX] = {
-	{"mode_quxga_800_600", OV2680_MODE_QUXGA_800_600,
-	 800, 600, ov2680_setting_30fps_QUXGA_800_600,
-	 ARRAY_SIZE(ov2680_setting_30fps_QUXGA_800_600)},
-	{"mode_720p_1280_720", OV2680_MODE_720P_1280_720,
-	 1280, 720, ov2680_setting_30fps_720P_1280_720,
-	 ARRAY_SIZE(ov2680_setting_30fps_720P_1280_720)},
-	{"mode_uxga_1600_1200", OV2680_MODE_UXGA_1600_1200,
-	 1600, 1200, ov2680_setting_30fps_UXGA_1600_1200,
-	 ARRAY_SIZE(ov2680_setting_30fps_UXGA_1600_1200)},
+	/*
+	 * Window CONTROL 0x00 -> 0x01, enable manual window control,
+	 * this is necessary for full size flip and mirror support.
+	 */
+	{0x5708, 0x01},
 };
 
 static struct ov2680_dev *to_ov2680_dev(struct v4l2_subdev *sd)
@@ -329,6 +226,74 @@ static void ov2680_fill_format(struct ov2680_dev *sensor,
 	ov2680_set_bayer_order(sensor, fmt);
 }
 
+static void ov2680_calc_mode(struct ov2680_dev *sensor)
+{
+	int width = sensor->mode.fmt.width;
+	int height = sensor->mode.fmt.height;
+	int orig_width = width;
+	int orig_height = height;
+
+	if (width  <= (OV2680_NATIVE_WIDTH / 2) &&
+	    height <= (OV2680_NATIVE_HEIGHT / 2)) {
+		sensor->mode.binning = true;
+		width *= 2;
+		height *= 2;
+	} else {
+		sensor->mode.binning = false;
+	}
+
+	sensor->mode.h_start = ((OV2680_NATIVE_WIDTH - width) / 2) & ~1;
+	sensor->mode.v_start = ((OV2680_NATIVE_HEIGHT - height) / 2) & ~1;
+	sensor->mode.h_end = min(sensor->mode.h_start + width + OV2680_END_MARGIN - 1,
+				 OV2680_NATIVE_WIDTH - 1);
+	sensor->mode.v_end = min(sensor->mode.v_start + height + OV2680_END_MARGIN - 1,
+				 OV2680_NATIVE_HEIGHT - 1);
+	sensor->mode.h_output_size = orig_width;
+	sensor->mode.v_output_size = orig_height;
+	sensor->mode.hts = OV2680_PIXELS_PER_LINE;
+	sensor->mode.vts = OV2680_LINES_PER_FRAME;
+}
+
+static int ov2680_set_mode(struct ov2680_dev *sensor)
+{
+	u8 sensor_ctrl_0a, inc, fmt1, fmt2;
+	int ret = 0;
+
+	if (sensor->mode.binning) {
+		sensor_ctrl_0a = 0x23;
+		inc = 0x31;
+		fmt1 = 0xc2;
+		fmt2 = 0x01;
+	} else {
+		sensor_ctrl_0a = 0x21;
+		inc = 0x11;
+		fmt1 = 0xc0;
+		fmt2 = 0x00;
+	}
+
+	cci_write(sensor->regmap, OV2680_REG_SENSOR_CTRL_0A, sensor_ctrl_0a, &ret);
+	cci_write(sensor->regmap, OV2680_REG_HORIZONTAL_START, sensor->mode.h_start, &ret);
+	cci_write(sensor->regmap, OV2680_REG_VERTICAL_START, sensor->mode.v_start, &ret);
+	cci_write(sensor->regmap, OV2680_REG_HORIZONTAL_END, sensor->mode.h_end, &ret);
+	cci_write(sensor->regmap, OV2680_REG_VERTICAL_END, sensor->mode.v_end, &ret);
+	cci_write(sensor->regmap, OV2680_REG_HORIZONTAL_OUTPUT_SIZE,
+		  sensor->mode.h_output_size, &ret);
+	cci_write(sensor->regmap, OV2680_REG_VERTICAL_OUTPUT_SIZE,
+		  sensor->mode.v_output_size, &ret);
+	cci_write(sensor->regmap, OV2680_REG_TIMING_HTS, sensor->mode.hts, &ret);
+	cci_write(sensor->regmap, OV2680_REG_TIMING_VTS, sensor->mode.vts, &ret);
+	cci_write(sensor->regmap, OV2680_REG_ISP_X_WIN, 0, &ret);
+	cci_write(sensor->regmap, OV2680_REG_ISP_Y_WIN, 0, &ret);
+	cci_write(sensor->regmap, OV2680_REG_X_INC, inc, &ret);
+	cci_write(sensor->regmap, OV2680_REG_Y_INC, inc, &ret);
+	cci_write(sensor->regmap, OV2680_REG_X_WIN, sensor->mode.h_output_size, &ret);
+	cci_write(sensor->regmap, OV2680_REG_Y_WIN, sensor->mode.v_output_size, &ret);
+	cci_write(sensor->regmap, OV2680_REG_FORMAT1, fmt1, &ret);
+	cci_write(sensor->regmap, OV2680_REG_FORMAT2, fmt2, &ret);
+
+	return ret;
+}
+
 static int ov2680_set_vflip(struct ov2680_dev *sensor, s32 val)
 {
 	int ret;
@@ -389,10 +354,12 @@ static int ov2680_stream_enable(struct ov2680_dev *sensor)
 	int ret = 0;
 
 	cci_write(sensor->regmap, OV2680_REG_PLL_MULTIPLIER, sensor->pll_mult, &ret);
-	cci_multi_reg_write(sensor->regmap, ov2680_mode_init_data.reg_data,
-			    ov2680_mode_init_data.reg_data_size, &ret);
-	cci_multi_reg_write(sensor->regmap, sensor->current_mode->reg_data,
-			    sensor->current_mode->reg_data_size, &ret);
+	cci_multi_reg_write(sensor->regmap, ov2680_global_setting,
+			    ARRAY_SIZE(ov2680_global_setting), &ret);
+	if (ret < 0)
+		return ret;
+
+	ret = ov2680_set_mode(sensor);
 	if (ret < 0)
 		return ret;
 
@@ -537,19 +504,16 @@ static int ov2680_set_fmt(struct v4l2_subdev *sd,
 {
 	struct ov2680_dev *sensor = to_ov2680_dev(sd);
 	struct v4l2_mbus_framefmt *try_fmt;
-	const struct ov2680_mode_info *mode;
+	unsigned int width, height;
 	int ret = 0;
 
 	if (format->pad != 0)
 		return -EINVAL;
 
-	mode = v4l2_find_nearest_size(ov2680_mode_data,
-				      ARRAY_SIZE(ov2680_mode_data), width, height,
-				      format->format.width, format->format.height);
-	if (!mode)
-		return -EINVAL;
+	width = min_t(unsigned int, ALIGN(format->format.width, 2), OV2680_NATIVE_WIDTH);
+	height = min_t(unsigned int, ALIGN(format->format.height, 2), OV2680_NATIVE_HEIGHT);
 
-	ov2680_fill_format(sensor, &format->format, mode->width, mode->height);
+	ov2680_fill_format(sensor, &format->format, width, height);
 
 	if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
 		try_fmt = v4l2_subdev_get_try_format(sd, sd_state, 0);
@@ -564,8 +528,8 @@ static int ov2680_set_fmt(struct v4l2_subdev *sd,
 		goto unlock;
 	}
 
-	sensor->current_mode = mode;
 	sensor->mode.fmt = format->format;
+	ov2680_calc_mode(sensor);
 
 unlock:
 	mutex_unlock(&sensor->lock);
@@ -587,15 +551,20 @@ static int ov2680_enum_frame_size(struct v4l2_subdev *sd,
 				  struct v4l2_subdev_state *sd_state,
 				  struct v4l2_subdev_frame_size_enum *fse)
 {
-	int index = fse->index;
+	static const struct v4l2_frmsize_discrete ov2680_frame_sizes[] = {
+		{ 1600, 1200 },
+		{ 1280,  720 },
+		{  800,  600 },
+	};
+	u32 index = fse->index;
 
-	if (index >= OV2680_MODE_MAX || index < 0)
+	if (index >= ARRAY_SIZE(ov2680_frame_sizes))
 		return -EINVAL;
 
-	fse->min_width = ov2680_mode_data[index].width;
-	fse->min_height = ov2680_mode_data[index].height;
-	fse->max_width = ov2680_mode_data[index].width;
-	fse->max_height = ov2680_mode_data[index].height;
+	fse->min_width = ov2680_frame_sizes[index].width;
+	fse->min_height = ov2680_frame_sizes[index].height;
+	fse->max_width = ov2680_frame_sizes[index].width;
+	fse->max_height = ov2680_frame_sizes[index].height;
 
 	return 0;
 }
@@ -678,18 +647,13 @@ static const struct v4l2_subdev_ops ov2680_subdev_ops = {
 
 static int ov2680_mode_init(struct ov2680_dev *sensor)
 {
-	const struct ov2680_mode_info *init_mode;
-
 	/* set initial mode */
 	ov2680_fill_format(sensor, &sensor->mode.fmt, OV2680_DEFAULT_WIDTH, OV2680_DEFAULT_HEIGHT);
+	ov2680_calc_mode(sensor);
 
 	sensor->mode.frame_interval.denominator = OV2680_FRAME_RATE;
 	sensor->mode.frame_interval.numerator = 1;
 
-	init_mode = &ov2680_mode_init_data;
-
-	sensor->current_mode = init_mode;
-
 	return 0;
 }
 
-- 
2.40.1


^ permalink raw reply related	[flat|nested] 57+ messages in thread

* [PATCH 22/28] media: ov2680: Add an __ov2680_get_pad_format() helper function
  2023-06-07 16:46 [PATCH 00/28] media: ov2680: Bugfixes + ACPI + selection(crop-tgt) API support Hans de Goede
                   ` (20 preceding siblings ...)
  2023-06-07 16:47 ` [PATCH 21/28] media: ov2680: Make setting the mode algorithm based Hans de Goede
@ 2023-06-07 16:47 ` Hans de Goede
  2023-06-07 16:47 ` [PATCH 23/28] media: ov2680: Implement selection support Hans de Goede
                   ` (6 subsequent siblings)
  28 siblings, 0 replies; 57+ messages in thread
From: Hans de Goede @ 2023-06-07 16:47 UTC (permalink / raw)
  To: Daniel Scally, Laurent Pinchart, Sakari Ailus, Andy Shevchenko
  Cc: Hans de Goede, Mauro Carvalho Chehab, Kate Hsuan, linux-media

Add an __ov2680_get_pad_format() helper function.

This is a preparation patch for adding selections support.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
---
 drivers/media/i2c/ov2680.c | 23 +++++++++++++----------
 1 file changed, 13 insertions(+), 10 deletions(-)

diff --git a/drivers/media/i2c/ov2680.c b/drivers/media/i2c/ov2680.c
index b0a8322696ee..7f1990b9a3fb 100644
--- a/drivers/media/i2c/ov2680.c
+++ b/drivers/media/i2c/ov2680.c
@@ -214,6 +214,16 @@ static void ov2680_set_bayer_order(struct ov2680_dev *sensor, struct v4l2_mbus_f
 	fmt->code = ov2680_hv_flip_bayer_order[hv_flip];
 }
 
+static struct v4l2_mbus_framefmt *
+__ov2680_get_pad_format(struct ov2680_dev *sensor, struct v4l2_subdev_state *state,
+			unsigned int pad, enum v4l2_subdev_format_whence which)
+{
+	if (which == V4L2_SUBDEV_FORMAT_TRY)
+		return v4l2_subdev_get_try_format(&sensor->sd, state, pad);
+
+	return &sensor->mode.fmt;
+}
+
 static void ov2680_fill_format(struct ov2680_dev *sensor,
 			       struct v4l2_mbus_framefmt *fmt,
 			       unsigned int width, unsigned int height)
@@ -477,22 +487,15 @@ static int ov2680_get_fmt(struct v4l2_subdev *sd,
 			  struct v4l2_subdev_format *format)
 {
 	struct ov2680_dev *sensor = to_ov2680_dev(sd);
-	struct v4l2_mbus_framefmt *fmt = NULL;
+	struct v4l2_mbus_framefmt *fmt;
 
 	if (format->pad != 0)
 		return -EINVAL;
 
+	fmt = __ov2680_get_pad_format(sensor, sd_state, format->pad, format->which);
+
 	mutex_lock(&sensor->lock);
-
-	if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
-		fmt = v4l2_subdev_get_try_format(&sensor->sd, sd_state,
-						 format->pad);
-	} else {
-		fmt = &sensor->mode.fmt;
-	}
-
 	format->format = *fmt;
-
 	mutex_unlock(&sensor->lock);
 
 	return 0;
-- 
2.40.1


^ permalink raw reply related	[flat|nested] 57+ messages in thread

* [PATCH 23/28] media: ov2680: Implement selection support
  2023-06-07 16:46 [PATCH 00/28] media: ov2680: Bugfixes + ACPI + selection(crop-tgt) API support Hans de Goede
                   ` (21 preceding siblings ...)
  2023-06-07 16:47 ` [PATCH 22/28] media: ov2680: Add an __ov2680_get_pad_format() helper function Hans de Goede
@ 2023-06-07 16:47 ` Hans de Goede
  2023-06-07 21:01   ` Andy Shevchenko
  2023-06-07 21:04   ` Andy Shevchenko
  2023-06-07 16:47 ` [PATCH 24/28] media: ov2680: Fix exposure and gain ctrls range and default value Hans de Goede
                   ` (5 subsequent siblings)
  28 siblings, 2 replies; 57+ messages in thread
From: Hans de Goede @ 2023-06-07 16:47 UTC (permalink / raw)
  To: Daniel Scally, Laurent Pinchart, Sakari Ailus, Andy Shevchenko
  Cc: Hans de Goede, Mauro Carvalho Chehab, Kate Hsuan, linux-media

Implement selection support. Modelled after ov5693 selection support,
but allow setting sizes smaller than crop-size through set_fmt since
that was already allowed.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
---
 drivers/media/i2c/ov2680.c | 135 ++++++++++++++++++++++++++++++++++---
 1 file changed, 127 insertions(+), 8 deletions(-)

diff --git a/drivers/media/i2c/ov2680.c b/drivers/media/i2c/ov2680.c
index 7f1990b9a3fb..4705a901c513 100644
--- a/drivers/media/i2c/ov2680.c
+++ b/drivers/media/i2c/ov2680.c
@@ -64,6 +64,14 @@
 
 #define OV2680_NATIVE_WIDTH			1616
 #define OV2680_NATIVE_HEIGHT			1216
+#define OV2680_NATIVE_START_LEFT		0
+#define OV2680_NATIVE_START_TOP			0
+#define OV2680_ACTIVE_WIDTH			1600
+#define OV2680_ACTIVE_HEIGHT			1200
+#define OV2680_ACTIVE_START_LEFT		8
+#define OV2680_ACTIVE_START_TOP			8
+#define OV2680_MIN_CROP_WIDTH			2
+#define OV2680_MIN_CROP_HEIGHT			2
 
 /* 66MHz pixel clock: 66MHz / 1704 * 1294 = 30fps */
 #define OV2680_PIXELS_PER_LINE			1704
@@ -108,6 +116,7 @@ struct ov2680_ctrls {
 };
 
 struct ov2680_mode {
+	struct v4l2_rect		crop;
 	struct v4l2_mbus_framefmt	fmt;
 	struct v4l2_fract		frame_interval;
 	bool				binning;
@@ -142,6 +151,13 @@ struct ov2680_dev {
 	struct ov2680_mode		mode;
 };
 
+static const struct v4l2_rect ov2680_default_crop = {
+	.left = OV2680_ACTIVE_START_LEFT,
+	.top = OV2680_ACTIVE_START_TOP,
+	.width = OV2680_ACTIVE_WIDTH,
+	.height = OV2680_ACTIVE_HEIGHT,
+};
+
 static const char * const test_pattern_menu[] = {
 	"Disabled",
 	"Color Bars",
@@ -224,6 +240,16 @@ __ov2680_get_pad_format(struct ov2680_dev *sensor, struct v4l2_subdev_state *sta
 	return &sensor->mode.fmt;
 }
 
+static struct v4l2_rect *
+__ov2680_get_pad_crop(struct ov2680_dev *sensor, struct v4l2_subdev_state *state,
+		      unsigned int pad, enum v4l2_subdev_format_whence which)
+{
+	if (which == V4L2_SUBDEV_FORMAT_TRY)
+		return v4l2_subdev_get_try_crop(&sensor->sd, state, pad);
+
+	return &sensor->mode.crop;
+}
+
 static void ov2680_fill_format(struct ov2680_dev *sensor,
 			       struct v4l2_mbus_framefmt *fmt,
 			       unsigned int width, unsigned int height)
@@ -243,8 +269,8 @@ static void ov2680_calc_mode(struct ov2680_dev *sensor)
 	int orig_width = width;
 	int orig_height = height;
 
-	if (width  <= (OV2680_NATIVE_WIDTH / 2) &&
-	    height <= (OV2680_NATIVE_HEIGHT / 2)) {
+	if (width  <= (sensor->mode.crop.width / 2) &&
+	    height <= (sensor->mode.crop.height / 2)) {
 		sensor->mode.binning = true;
 		width *= 2;
 		height *= 2;
@@ -252,8 +278,10 @@ static void ov2680_calc_mode(struct ov2680_dev *sensor)
 		sensor->mode.binning = false;
 	}
 
-	sensor->mode.h_start = ((OV2680_NATIVE_WIDTH - width) / 2) & ~1;
-	sensor->mode.v_start = ((OV2680_NATIVE_HEIGHT - height) / 2) & ~1;
+	sensor->mode.h_start =
+		(sensor->mode.crop.left + (sensor->mode.crop.width - width) / 2) & ~1;
+	sensor->mode.v_start =
+		(sensor->mode.crop.top + (sensor->mode.crop.height - height) / 2) & ~1;
 	sensor->mode.h_end = min(sensor->mode.h_start + width + OV2680_END_MARGIN - 1,
 				 OV2680_NATIVE_WIDTH - 1);
 	sensor->mode.v_end = min(sensor->mode.v_start + height + OV2680_END_MARGIN - 1,
@@ -507,14 +535,20 @@ static int ov2680_set_fmt(struct v4l2_subdev *sd,
 {
 	struct ov2680_dev *sensor = to_ov2680_dev(sd);
 	struct v4l2_mbus_framefmt *try_fmt;
+	const struct v4l2_rect *crop;
 	unsigned int width, height;
 	int ret = 0;
 
 	if (format->pad != 0)
 		return -EINVAL;
 
-	width = min_t(unsigned int, ALIGN(format->format.width, 2), OV2680_NATIVE_WIDTH);
-	height = min_t(unsigned int, ALIGN(format->format.height, 2), OV2680_NATIVE_HEIGHT);
+	crop = __ov2680_get_pad_crop(sensor, sd_state, format->pad, format->which);
+
+	/* Limit set_fmt max size to crop width / height */
+	width = clamp_t(unsigned int, ALIGN(format->format.width, 2),
+			OV2680_MIN_CROP_WIDTH, crop->width);
+	height = clamp_t(unsigned int, ALIGN(format->format.height, 2),
+			 OV2680_MIN_CROP_HEIGHT, crop->height);
 
 	ov2680_fill_format(sensor, &format->format, width, height);
 
@@ -540,11 +574,93 @@ static int ov2680_set_fmt(struct v4l2_subdev *sd,
 	return ret;
 }
 
+static int ov2680_get_selection(struct v4l2_subdev *sd,
+				struct v4l2_subdev_state *state,
+				struct v4l2_subdev_selection *sel)
+{
+	struct ov2680_dev *sensor = to_ov2680_dev(sd);
+
+	switch (sel->target) {
+	case V4L2_SEL_TGT_CROP:
+		mutex_lock(&sensor->lock);
+		sel->r = *__ov2680_get_pad_crop(sensor, state, sel->pad, sel->which);
+		mutex_unlock(&sensor->lock);
+		break;
+	case V4L2_SEL_TGT_NATIVE_SIZE:
+	case V4L2_SEL_TGT_CROP_BOUNDS:
+		sel->r.top = 0;
+		sel->r.left = 0;
+		sel->r.width = OV2680_NATIVE_WIDTH;
+		sel->r.height = OV2680_NATIVE_HEIGHT;
+		break;
+	case V4L2_SEL_TGT_CROP_DEFAULT:
+		sel->r = ov2680_default_crop;
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+static int ov2680_set_selection(struct v4l2_subdev *sd,
+				struct v4l2_subdev_state *state,
+				struct v4l2_subdev_selection *sel)
+{
+	struct ov2680_dev *sensor = to_ov2680_dev(sd);
+	struct v4l2_mbus_framefmt *format;
+	struct v4l2_rect *crop;
+	struct v4l2_rect rect;
+
+	if (sel->target != V4L2_SEL_TGT_CROP)
+		return -EINVAL;
+
+	/*
+	 * Clamp the boundaries of the crop rectangle to the size of the sensor
+	 * pixel array. Align to multiples of 2 to ensure Bayer pattern isn't
+	 * disrupted.
+	 */
+	rect.left = clamp(ALIGN(sel->r.left, 2), OV2680_NATIVE_START_LEFT,
+			  OV2680_NATIVE_WIDTH);
+	rect.top = clamp(ALIGN(sel->r.top, 2), OV2680_NATIVE_START_TOP,
+			 OV2680_NATIVE_HEIGHT);
+	rect.width = clamp_t(unsigned int, ALIGN(sel->r.width, 2),
+			     OV2680_MIN_CROP_WIDTH, OV2680_NATIVE_WIDTH);
+	rect.height = clamp_t(unsigned int, ALIGN(sel->r.height, 2),
+			      OV2680_MIN_CROP_HEIGHT, OV2680_NATIVE_HEIGHT);
+
+	/* Make sure the crop rectangle isn't outside the bounds of the array */
+	rect.width = min_t(unsigned int, rect.width, OV2680_NATIVE_WIDTH - rect.left);
+	rect.height = min_t(unsigned int, rect.height, OV2680_NATIVE_HEIGHT - rect.top);
+
+	crop = __ov2680_get_pad_crop(sensor, state, sel->pad, sel->which);
+
+	mutex_lock(&sensor->lock);
+	if (rect.width != crop->width || rect.height != crop->height) {
+		/*
+		 * Reset the output image size if the crop rectangle size has
+		 * been modified.
+		 */
+		format = __ov2680_get_pad_format(sensor, state, sel->pad, sel->which);
+		format->width = rect.width;
+		format->height = rect.height;
+	}
+
+	*crop = rect;
+	mutex_unlock(&sensor->lock);
+
+	sel->r = rect;
+
+	return 0;
+}
+
 static int ov2680_init_cfg(struct v4l2_subdev *sd,
 			   struct v4l2_subdev_state *sd_state)
 {
 	struct ov2680_dev *sensor = to_ov2680_dev(sd);
 
+	sd_state->pads[0].try_crop = ov2680_default_crop;
+
 	ov2680_fill_format(sensor, &sd_state->pads[0].try_fmt,
 			   OV2680_DEFAULT_WIDTH, OV2680_DEFAULT_HEIGHT);
 	return 0;
@@ -637,10 +753,12 @@ static const struct v4l2_subdev_video_ops ov2680_video_ops = {
 static const struct v4l2_subdev_pad_ops ov2680_pad_ops = {
 	.init_cfg		= ov2680_init_cfg,
 	.enum_mbus_code		= ov2680_enum_mbus_code,
-	.get_fmt		= ov2680_get_fmt,
-	.set_fmt		= ov2680_set_fmt,
 	.enum_frame_size	= ov2680_enum_frame_size,
 	.enum_frame_interval	= ov2680_enum_frame_interval,
+	.get_fmt		= ov2680_get_fmt,
+	.set_fmt		= ov2680_set_fmt,
+	.get_selection		= ov2680_get_selection,
+	.set_selection		= ov2680_set_selection,
 };
 
 static const struct v4l2_subdev_ops ov2680_subdev_ops = {
@@ -651,6 +769,7 @@ static const struct v4l2_subdev_ops ov2680_subdev_ops = {
 static int ov2680_mode_init(struct ov2680_dev *sensor)
 {
 	/* set initial mode */
+	sensor->mode.crop = ov2680_default_crop;
 	ov2680_fill_format(sensor, &sensor->mode.fmt, OV2680_DEFAULT_WIDTH, OV2680_DEFAULT_HEIGHT);
 	ov2680_calc_mode(sensor);
 
-- 
2.40.1


^ permalink raw reply related	[flat|nested] 57+ messages in thread

* [PATCH 24/28] media: ov2680: Fix exposure and gain ctrls range and default value
  2023-06-07 16:46 [PATCH 00/28] media: ov2680: Bugfixes + ACPI + selection(crop-tgt) API support Hans de Goede
                   ` (22 preceding siblings ...)
  2023-06-07 16:47 ` [PATCH 23/28] media: ov2680: Implement selection support Hans de Goede
@ 2023-06-07 16:47 ` Hans de Goede
  2023-06-07 16:47 ` [PATCH 25/28] media: ov2680: Add a bunch of register tweaks Hans de Goede
                   ` (4 subsequent siblings)
  28 siblings, 0 replies; 57+ messages in thread
From: Hans de Goede @ 2023-06-07 16:47 UTC (permalink / raw)
  To: Daniel Scally, Laurent Pinchart, Sakari Ailus, Andy Shevchenko
  Cc: Hans de Goede, Mauro Carvalho Chehab, Kate Hsuan, linux-media

The exposure control's max effective value is VTS - 8, set the control
range to match this. Thas means that if/when a future commit makes VTS
configurable as a control itself the exposure range needs to be
updated dynamically to match the VTS value.

The gain control goes from 0 - 1023, higher values wrap around to
the lowest gain setting.

Also stop setting 0 as default for both controls this leads to
a totally black picture which is no good. This is esp. important
for tests of the sensor driver without (userspace driven)
auto exposure / gain.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
---
 drivers/media/i2c/ov2680.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/drivers/media/i2c/ov2680.c b/drivers/media/i2c/ov2680.c
index 4705a901c513..d1fd47f10a17 100644
--- a/drivers/media/i2c/ov2680.c
+++ b/drivers/media/i2c/ov2680.c
@@ -80,6 +80,9 @@
 /* If possible send 16 extra rows / lines to the ISP as padding */
 #define OV2680_END_MARGIN			16
 
+/* Max exposure time is VTS - 8 */
+#define OV2680_INTEGRATION_TIME_MARGIN		8
+
 #define OV2680_DEFAULT_WIDTH			800
 #define OV2680_DEFAULT_HEIGHT			600
 
@@ -784,6 +787,7 @@ static int ov2680_v4l2_register(struct ov2680_dev *sensor, struct i2c_client *cl
 	const struct v4l2_ctrl_ops *ops = &ov2680_ctrl_ops;
 	struct ov2680_ctrls *ctrls = &sensor->ctrls;
 	struct v4l2_ctrl_handler *hdl = &ctrls->handler;
+	int exp_max = OV2680_LINES_PER_FRAME - OV2680_INTEGRATION_TIME_MARGIN;
 	int ret = 0;
 
 	v4l2_i2c_subdev_init(&sensor->sd, client, &ov2680_subdev_ops);
@@ -810,9 +814,9 @@ static int ov2680_v4l2_register(struct ov2680_dev *sensor, struct i2c_client *cl
 					0, 0, test_pattern_menu);
 
 	ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE,
-					    0, 32767, 1, 0);
+					    0, exp_max, 1, exp_max);
 
-	ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN, 0, 2047, 1, 0);
+	ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN, 0, 1023, 1, 250);
 
 	if (hdl->error) {
 		ret = hdl->error;
-- 
2.40.1


^ permalink raw reply related	[flat|nested] 57+ messages in thread

* [PATCH 25/28] media: ov2680: Add a bunch of register tweaks
  2023-06-07 16:46 [PATCH 00/28] media: ov2680: Bugfixes + ACPI + selection(crop-tgt) API support Hans de Goede
                   ` (23 preceding siblings ...)
  2023-06-07 16:47 ` [PATCH 24/28] media: ov2680: Fix exposure and gain ctrls range and default value Hans de Goede
@ 2023-06-07 16:47 ` Hans de Goede
  2023-06-07 16:47 ` [PATCH 26/28] media: ov2680: Add g_skip_frames op support Hans de Goede
                   ` (3 subsequent siblings)
  28 siblings, 0 replies; 57+ messages in thread
From: Hans de Goede @ 2023-06-07 16:47 UTC (permalink / raw)
  To: Daniel Scally, Laurent Pinchart, Sakari Ailus, Andy Shevchenko
  Cc: Hans de Goede, Mauro Carvalho Chehab, Kate Hsuan, linux-media

Usually when developing a sensor driver with help from the vendor
the vendor will provide a bunch of register tweaks for optimal
performance of the sensor.

The atomisp-ov2680.c driver was (presumably) developed by Intel with
help from OmniVision and indeed contains a bunch of register tweaks.

Add these register tweaks to the "main" ov2680.c driver.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
---
 drivers/media/i2c/ov2680.c | 53 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 53 insertions(+)

diff --git a/drivers/media/i2c/ov2680.c b/drivers/media/i2c/ov2680.c
index d1fd47f10a17..867df602ee53 100644
--- a/drivers/media/i2c/ov2680.c
+++ b/drivers/media/i2c/ov2680.c
@@ -177,18 +177,71 @@ static const int ov2680_hv_flip_bayer_order[] = {
 };
 
 static const struct reg_sequence ov2680_global_setting[] = {
+	/* MIPI PHY, 0x10 -> 0x1c enable bp_c_hs_en_lat and bp_d_hs_en_lat */
+	{0x3016, 0x1c},
+
 	/* R MANUAL set exposure (0x01) and gain (0x02) to manual (hw does not do auto) */
 	{0x3503, 0x03},
 
+	/* Analog control register tweaks */
+	{0x3603, 0x39}, /* Reset value 0x99 */
+	{0x3604, 0x24}, /* Reset value 0x74 */
+	{0x3621, 0x37}, /* Reset value 0x44 */
+
+	/* Sensor control register tweaks */
+	{0x3701, 0x64}, /* Reset value 0x61 */
+	{0x3705, 0x3c}, /* Reset value 0x21 */
+	{0x370c, 0x50}, /* Reset value 0x10 */
+	{0x370d, 0xc0}, /* Reset value 0x00 */
+	{0x3718, 0x88}, /* Reset value 0x80 */
+
+	/* PSRAM tweaks */
+	{0x3781, 0x80}, /* Reset value 0x00 */
+	{0x3784, 0x0c}, /* Reset value 0x00, based on OV2680_R1A_AM10.ovt */
+	{0x3789, 0x60}, /* Reset value 0x50 */
+
+	/* BLC CTRL00 0x01 -> 0x81 set avg_weight to 8 */
+	{0x4000, 0x81},
+
 	/* Set black level compensation range to 0 - 3 (default 0 - 11) */
 	{0x4008, 0x00},
 	{0x4009, 0x03},
 
+	/* VFIFO R2 0x00 -> 0x02 set Frame reset enable */
+	{0x4602, 0x02},
+
+	/* MIPI ctrl CLK PREPARE MIN change from 0x26 (38) -> 0x36 (54) */
+	{0x481f, 0x36},
+
+	/* MIPI ctrl CLK LPX P MIN change from 0x32 (50) -> 0x36 (54) */
+	{0x4825, 0x36},
+
+	/* R ISP CTRL2 0x20 -> 0x30, set sof_sel bit */
+	{0x5002, 0x30},
+
 	/*
 	 * Window CONTROL 0x00 -> 0x01, enable manual window control,
 	 * this is necessary for full size flip and mirror support.
 	 */
 	{0x5708, 0x01},
+
+	/*
+	 * DPC CTRL0 0x14 -> 0x3e, set enable_tail, enable_3x3_cluster
+	 * and enable_general_tail bits based OV2680_R1A_AM10.ovt.
+	 */
+	{0x5780, 0x3e},
+
+	/* DPC MORE CONNECTION CASE THRE 0x0c (12) -> 0x02 (2) */
+	{0x5788, 0x02},
+
+	/* DPC GAIN LIST1 0x0f (15) -> 0x08 (8) */
+	{0x578e, 0x08},
+
+	/* DPC GAIN LIST2 0x3f (63) -> 0x0c (12) */
+	{0x578f, 0x0c},
+
+	/* DPC THRE RATIO 0x04 (4) -> 0x00 (0) */
+	{0x5792, 0x00},
 };
 
 static struct ov2680_dev *to_ov2680_dev(struct v4l2_subdev *sd)
-- 
2.40.1


^ permalink raw reply related	[flat|nested] 57+ messages in thread

* [PATCH 26/28] media: ov2680: Add g_skip_frames op support
  2023-06-07 16:46 [PATCH 00/28] media: ov2680: Bugfixes + ACPI + selection(crop-tgt) API support Hans de Goede
                   ` (24 preceding siblings ...)
  2023-06-07 16:47 ` [PATCH 25/28] media: ov2680: Add a bunch of register tweaks Hans de Goede
@ 2023-06-07 16:47 ` Hans de Goede
  2023-06-07 17:04   ` Laurent Pinchart
  2023-06-07 16:47 ` [PATCH 27/28] media: ov2680: Drop unnecessary pad checks Hans de Goede
                   ` (2 subsequent siblings)
  28 siblings, 1 reply; 57+ messages in thread
From: Hans de Goede @ 2023-06-07 16:47 UTC (permalink / raw)
  To: Daniel Scally, Laurent Pinchart, Sakari Ailus, Andy Shevchenko
  Cc: Hans de Goede, Mauro Carvalho Chehab, Kate Hsuan, linux-media

Add support for v4l2_subdev_sensor_ops.g_skip_frames.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
---
 drivers/media/i2c/ov2680.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/drivers/media/i2c/ov2680.c b/drivers/media/i2c/ov2680.c
index 867df602ee53..9e4f0bcbe979 100644
--- a/drivers/media/i2c/ov2680.c
+++ b/drivers/media/i2c/ov2680.c
@@ -62,6 +62,8 @@
 
 #define OV2680_FRAME_RATE			30
 
+#define OV2680_SKIP_FRAMES			3
+
 #define OV2680_NATIVE_WIDTH			1616
 #define OV2680_NATIVE_HEIGHT			1216
 #define OV2680_NATIVE_START_LEFT		0
@@ -759,6 +761,12 @@ static int ov2680_enum_frame_interval(struct v4l2_subdev *sd,
 	return 0;
 }
 
+static int ov2680_g_skip_frames(struct v4l2_subdev *sd, u32 *frames)
+{
+	*frames = OV2680_SKIP_FRAMES;
+	return 0;
+}
+
 static int ov2680_s_ctrl(struct v4l2_ctrl *ctrl)
 {
 	struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
@@ -806,6 +814,10 @@ static const struct v4l2_subdev_video_ops ov2680_video_ops = {
 	.s_stream		= ov2680_s_stream,
 };
 
+static const struct v4l2_subdev_sensor_ops ov2680_sensor_ops = {
+	.g_skip_frames	= ov2680_g_skip_frames,
+};
+
 static const struct v4l2_subdev_pad_ops ov2680_pad_ops = {
 	.init_cfg		= ov2680_init_cfg,
 	.enum_mbus_code		= ov2680_enum_mbus_code,
@@ -820,6 +832,7 @@ static const struct v4l2_subdev_pad_ops ov2680_pad_ops = {
 static const struct v4l2_subdev_ops ov2680_subdev_ops = {
 	.video	= &ov2680_video_ops,
 	.pad	= &ov2680_pad_ops,
+	.sensor = &ov2680_sensor_ops,
 };
 
 static int ov2680_mode_init(struct ov2680_dev *sensor)
-- 
2.40.1


^ permalink raw reply related	[flat|nested] 57+ messages in thread

* [PATCH 27/28] media: ov2680: Drop unnecessary pad checks
  2023-06-07 16:46 [PATCH 00/28] media: ov2680: Bugfixes + ACPI + selection(crop-tgt) API support Hans de Goede
                   ` (25 preceding siblings ...)
  2023-06-07 16:47 ` [PATCH 26/28] media: ov2680: Add g_skip_frames op support Hans de Goede
@ 2023-06-07 16:47 ` Hans de Goede
  2023-06-07 16:47 ` [PATCH 28/28] media: ov2680: Read and log sensor revision during probe Hans de Goede
  2023-06-09  9:37 ` [PATCH 00/28] media: ov2680: Bugfixes + ACPI + selection(crop-tgt) API support Sakari Ailus
  28 siblings, 0 replies; 57+ messages in thread
From: Hans de Goede @ 2023-06-07 16:47 UTC (permalink / raw)
  To: Daniel Scally, Laurent Pinchart, Sakari Ailus, Andy Shevchenko
  Cc: Hans de Goede, Mauro Carvalho Chehab, Kate Hsuan, linux-media

Drop unnecessary pad checks in enum_mbus_code, get_fmt, set_fmt
this is already checked by check_pad() from
drivers/media/v4l2-core/v4l2-subdev.c.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
---
 drivers/media/i2c/ov2680.c | 8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)

diff --git a/drivers/media/i2c/ov2680.c b/drivers/media/i2c/ov2680.c
index 9e4f0bcbe979..8872898ff2c4 100644
--- a/drivers/media/i2c/ov2680.c
+++ b/drivers/media/i2c/ov2680.c
@@ -560,7 +560,7 @@ static int ov2680_enum_mbus_code(struct v4l2_subdev *sd,
 {
 	struct ov2680_dev *sensor = to_ov2680_dev(sd);
 
-	if (code->pad != 0 || code->index != 0)
+	if (code->index != 0)
 		return -EINVAL;
 
 	code->code = sensor->mode.fmt.code;
@@ -575,9 +575,6 @@ static int ov2680_get_fmt(struct v4l2_subdev *sd,
 	struct ov2680_dev *sensor = to_ov2680_dev(sd);
 	struct v4l2_mbus_framefmt *fmt;
 
-	if (format->pad != 0)
-		return -EINVAL;
-
 	fmt = __ov2680_get_pad_format(sensor, sd_state, format->pad, format->which);
 
 	mutex_lock(&sensor->lock);
@@ -597,9 +594,6 @@ static int ov2680_set_fmt(struct v4l2_subdev *sd,
 	unsigned int width, height;
 	int ret = 0;
 
-	if (format->pad != 0)
-		return -EINVAL;
-
 	crop = __ov2680_get_pad_crop(sensor, sd_state, format->pad, format->which);
 
 	/* Limit set_fmt max size to crop width / height */
-- 
2.40.1


^ permalink raw reply related	[flat|nested] 57+ messages in thread

* [PATCH 28/28] media: ov2680: Read and log sensor revision during probe
  2023-06-07 16:46 [PATCH 00/28] media: ov2680: Bugfixes + ACPI + selection(crop-tgt) API support Hans de Goede
                   ` (26 preceding siblings ...)
  2023-06-07 16:47 ` [PATCH 27/28] media: ov2680: Drop unnecessary pad checks Hans de Goede
@ 2023-06-07 16:47 ` Hans de Goede
  2023-06-09  9:37 ` [PATCH 00/28] media: ov2680: Bugfixes + ACPI + selection(crop-tgt) API support Sakari Ailus
  28 siblings, 0 replies; 57+ messages in thread
From: Hans de Goede @ 2023-06-07 16:47 UTC (permalink / raw)
  To: Daniel Scally, Laurent Pinchart, Sakari Ailus, Andy Shevchenko
  Cc: Hans de Goede, Mauro Carvalho Chehab, Kate Hsuan, linux-media

Read and log sensor revision during probe.

Since this means that the driver will now already log a message on
successful probe drop the "ov2680 init correctly" log message.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
---
 drivers/media/i2c/ov2680.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/drivers/media/i2c/ov2680.c b/drivers/media/i2c/ov2680.c
index 8872898ff2c4..399959cccd44 100644
--- a/drivers/media/i2c/ov2680.c
+++ b/drivers/media/i2c/ov2680.c
@@ -32,6 +32,7 @@
 #define OV2680_REG_SOFT_RESET			CCI_REG8(0x0103)
 
 #define OV2680_REG_CHIP_ID			CCI_REG16(0x300a)
+#define OV2680_REG_SC_CMMN_SUB_ID		CCI_REG8(0x302a)
 #define OV2680_REG_PLL_MULTIPLIER		CCI_REG16(0x3081)
 
 #define OV2680_REG_EXPOSURE_PK			CCI_REG24(0x3500)
@@ -913,10 +914,11 @@ static int ov2680_get_regulators(struct ov2680_dev *sensor)
 
 static int ov2680_check_id(struct ov2680_dev *sensor)
 {
-	u32 chip_id;
-	int ret;
+	u32 chip_id, rev;
+	int ret = 0;
 
-	ret = cci_read(sensor->regmap, OV2680_REG_CHIP_ID, &chip_id, NULL);
+	cci_read(sensor->regmap, OV2680_REG_CHIP_ID, &chip_id, &ret);
+	cci_read(sensor->regmap, OV2680_REG_SC_CMMN_SUB_ID, &rev, &ret);
 	if (ret < 0) {
 		dev_err(sensor->dev, "failed to read chip id\n");
 		return -ENODEV;
@@ -928,6 +930,8 @@ static int ov2680_check_id(struct ov2680_dev *sensor)
 		return -ENODEV;
 	}
 
+	dev_info(sensor->dev, "sensor_revision id = 0x%x, rev= %d\n", chip_id, rev & 0x0f);
+
 	return 0;
 }
 
@@ -1058,8 +1062,6 @@ static int ov2680_probe(struct i2c_client *client)
 	pm_runtime_use_autosuspend(&client->dev);
 	pm_runtime_put_autosuspend(&client->dev);
 
-	dev_info(dev, "ov2680 init correctly\n");
-
 	return 0;
 
 err_pm_runtime:
-- 
2.40.1


^ permalink raw reply related	[flat|nested] 57+ messages in thread

* Re: [PATCH 26/28] media: ov2680: Add g_skip_frames op support
  2023-06-07 16:47 ` [PATCH 26/28] media: ov2680: Add g_skip_frames op support Hans de Goede
@ 2023-06-07 17:04   ` Laurent Pinchart
  2023-06-07 17:49     ` Hans de Goede
  0 siblings, 1 reply; 57+ messages in thread
From: Laurent Pinchart @ 2023-06-07 17:04 UTC (permalink / raw)
  To: Hans de Goede
  Cc: Daniel Scally, Sakari Ailus, Andy Shevchenko,
	Mauro Carvalho Chehab, Kate Hsuan, linux-media

Hi Hans,

Thank you for the patch.

On Wed, Jun 07, 2023 at 06:47:10PM +0200, Hans de Goede wrote:
> Add support for v4l2_subdev_sensor_ops.g_skip_frames.

The .g_skip_frames() subdev operation was a mistake in hindsight, and
should probably not be used in new drivers (or existing drivers that
don't use it yet). Why do you need it ?

> Signed-off-by: Hans de Goede <hdegoede@redhat.com>
> ---
>  drivers/media/i2c/ov2680.c | 13 +++++++++++++
>  1 file changed, 13 insertions(+)
> 
> diff --git a/drivers/media/i2c/ov2680.c b/drivers/media/i2c/ov2680.c
> index 867df602ee53..9e4f0bcbe979 100644
> --- a/drivers/media/i2c/ov2680.c
> +++ b/drivers/media/i2c/ov2680.c
> @@ -62,6 +62,8 @@
>  
>  #define OV2680_FRAME_RATE			30
>  
> +#define OV2680_SKIP_FRAMES			3
> +
>  #define OV2680_NATIVE_WIDTH			1616
>  #define OV2680_NATIVE_HEIGHT			1216
>  #define OV2680_NATIVE_START_LEFT		0
> @@ -759,6 +761,12 @@ static int ov2680_enum_frame_interval(struct v4l2_subdev *sd,
>  	return 0;
>  }
>  
> +static int ov2680_g_skip_frames(struct v4l2_subdev *sd, u32 *frames)
> +{
> +	*frames = OV2680_SKIP_FRAMES;
> +	return 0;
> +}
> +
>  static int ov2680_s_ctrl(struct v4l2_ctrl *ctrl)
>  {
>  	struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
> @@ -806,6 +814,10 @@ static const struct v4l2_subdev_video_ops ov2680_video_ops = {
>  	.s_stream		= ov2680_s_stream,
>  };
>  
> +static const struct v4l2_subdev_sensor_ops ov2680_sensor_ops = {
> +	.g_skip_frames	= ov2680_g_skip_frames,
> +};
> +
>  static const struct v4l2_subdev_pad_ops ov2680_pad_ops = {
>  	.init_cfg		= ov2680_init_cfg,
>  	.enum_mbus_code		= ov2680_enum_mbus_code,
> @@ -820,6 +832,7 @@ static const struct v4l2_subdev_pad_ops ov2680_pad_ops = {
>  static const struct v4l2_subdev_ops ov2680_subdev_ops = {
>  	.video	= &ov2680_video_ops,
>  	.pad	= &ov2680_pad_ops,
> +	.sensor = &ov2680_sensor_ops,
>  };
>  
>  static int ov2680_mode_init(struct ov2680_dev *sensor)

-- 
Regards,

Laurent Pinchart

^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH 26/28] media: ov2680: Add g_skip_frames op support
  2023-06-07 17:04   ` Laurent Pinchart
@ 2023-06-07 17:49     ` Hans de Goede
  0 siblings, 0 replies; 57+ messages in thread
From: Hans de Goede @ 2023-06-07 17:49 UTC (permalink / raw)
  To: Laurent Pinchart
  Cc: Daniel Scally, Sakari Ailus, Andy Shevchenko,
	Mauro Carvalho Chehab, Kate Hsuan, linux-media

Hi Laurent,

On 6/7/23 19:04, Laurent Pinchart wrote:
> Hi Hans,
> 
> Thank you for the patch.
> 
> On Wed, Jun 07, 2023 at 06:47:10PM +0200, Hans de Goede wrote:
>> Add support for v4l2_subdev_sensor_ops.g_skip_frames.
> 
> The .g_skip_frames() subdev operation was a mistake in hindsight, and
> should probably not be used in new drivers (or existing drivers that
> don't use it yet). Why do you need it ?

I don't need it. The atomisp-ov2680.c version of the ov2680
driver had it and I thought it would be useful to preserve
that functionality.

Since you say it is deprecated this can be dropped.

Regards,

Hans






>> Signed-off-by: Hans de Goede <hdegoede@redhat.com>
>> ---
>>  drivers/media/i2c/ov2680.c | 13 +++++++++++++
>>  1 file changed, 13 insertions(+)
>>
>> diff --git a/drivers/media/i2c/ov2680.c b/drivers/media/i2c/ov2680.c
>> index 867df602ee53..9e4f0bcbe979 100644
>> --- a/drivers/media/i2c/ov2680.c
>> +++ b/drivers/media/i2c/ov2680.c
>> @@ -62,6 +62,8 @@
>>  
>>  #define OV2680_FRAME_RATE			30
>>  
>> +#define OV2680_SKIP_FRAMES			3
>> +
>>  #define OV2680_NATIVE_WIDTH			1616
>>  #define OV2680_NATIVE_HEIGHT			1216
>>  #define OV2680_NATIVE_START_LEFT		0
>> @@ -759,6 +761,12 @@ static int ov2680_enum_frame_interval(struct v4l2_subdev *sd,
>>  	return 0;
>>  }
>>  
>> +static int ov2680_g_skip_frames(struct v4l2_subdev *sd, u32 *frames)
>> +{
>> +	*frames = OV2680_SKIP_FRAMES;
>> +	return 0;
>> +}
>> +
>>  static int ov2680_s_ctrl(struct v4l2_ctrl *ctrl)
>>  {
>>  	struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
>> @@ -806,6 +814,10 @@ static const struct v4l2_subdev_video_ops ov2680_video_ops = {
>>  	.s_stream		= ov2680_s_stream,
>>  };
>>  
>> +static const struct v4l2_subdev_sensor_ops ov2680_sensor_ops = {
>> +	.g_skip_frames	= ov2680_g_skip_frames,
>> +};
>> +
>>  static const struct v4l2_subdev_pad_ops ov2680_pad_ops = {
>>  	.init_cfg		= ov2680_init_cfg,
>>  	.enum_mbus_code		= ov2680_enum_mbus_code,
>> @@ -820,6 +832,7 @@ static const struct v4l2_subdev_pad_ops ov2680_pad_ops = {
>>  static const struct v4l2_subdev_ops ov2680_subdev_ops = {
>>  	.video	= &ov2680_video_ops,
>>  	.pad	= &ov2680_pad_ops,
>> +	.sensor = &ov2680_sensor_ops,
>>  };
>>  
>>  static int ov2680_mode_init(struct ov2680_dev *sensor)
> 


^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH 08/28] media: Add MIPI CCI register access helper functions
  2023-06-07 16:46 ` [PATCH 08/28] media: Add MIPI CCI register access helper functions Hans de Goede
@ 2023-06-07 20:22   ` Andy Shevchenko
  2023-06-07 20:23     ` Andy Shevchenko
  0 siblings, 1 reply; 57+ messages in thread
From: Andy Shevchenko @ 2023-06-07 20:22 UTC (permalink / raw)
  To: Hans de Goede
  Cc: Daniel Scally, Laurent Pinchart, Sakari Ailus,
	Mauro Carvalho Chehab, Kate Hsuan, linux-media

On Wed, Jun 07, 2023 at 06:46:52PM +0200, Hans de Goede wrote:
> The CSI2 specification specifies a standard method to access camera sensor
> registers called "Camera Control Interface (CCI)".
> 
> This uses either 8 or 16 bit (big-endian wire order) register addresses
> and supports 8, 16, 24 or 32 bit (big-endian wire order) register widths.
> 
> Currently a lot of Linux camera sensor drivers all have their own custom
> helpers for this, often copy and pasted from other drivers.
> 
> Add a set of generic helpers for this so that all sensor drivers can
> switch to a single common implementation.
> 
> These helpers take an extra optional "int *err" function parameter,
> this can be used to chain a bunch of register accesses together with
> only a single error check at the end, rather then needing to error
> check each individual register access. The first failing call will
> set the contents of err to a non 0 value and all other calls will
> then become no-ops.

So, this version doesn't have updates we discussed previously. Is it only for
reference here?

-- 
With Best Regards,
Andy Shevchenko



^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH 08/28] media: Add MIPI CCI register access helper functions
  2023-06-07 20:22   ` Andy Shevchenko
@ 2023-06-07 20:23     ` Andy Shevchenko
  0 siblings, 0 replies; 57+ messages in thread
From: Andy Shevchenko @ 2023-06-07 20:23 UTC (permalink / raw)
  To: Hans de Goede
  Cc: Daniel Scally, Laurent Pinchart, Sakari Ailus,
	Mauro Carvalho Chehab, Kate Hsuan, linux-media

On Wed, Jun 07, 2023 at 11:22:48PM +0300, Andy Shevchenko wrote:
> On Wed, Jun 07, 2023 at 06:46:52PM +0200, Hans de Goede wrote:
> > The CSI2 specification specifies a standard method to access camera sensor
> > registers called "Camera Control Interface (CCI)".
> > 
> > This uses either 8 or 16 bit (big-endian wire order) register addresses
> > and supports 8, 16, 24 or 32 bit (big-endian wire order) register widths.
> > 
> > Currently a lot of Linux camera sensor drivers all have their own custom
> > helpers for this, often copy and pasted from other drivers.
> > 
> > Add a set of generic helpers for this so that all sensor drivers can
> > switch to a single common implementation.
> > 
> > These helpers take an extra optional "int *err" function parameter,
> > this can be used to chain a bunch of register accesses together with
> > only a single error check at the end, rather then needing to error
> > check each individual register access. The first failing call will
> > set the contents of err to a non 0 value and all other calls will
> > then become no-ops.
> 
> So, this version doesn't have updates we discussed previously. Is it only for
> reference here?

Okay, I found the answer in the cover letter.

-- 
With Best Regards,
Andy Shevchenko



^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH 10/28] media: ov2680: Store dev instead of i2c_client in ov2680_dev
  2023-06-07 16:46 ` [PATCH 10/28] media: ov2680: Store dev instead of i2c_client in ov2680_dev Hans de Goede
@ 2023-06-07 20:43   ` Andy Shevchenko
  0 siblings, 0 replies; 57+ messages in thread
From: Andy Shevchenko @ 2023-06-07 20:43 UTC (permalink / raw)
  To: Hans de Goede
  Cc: Daniel Scally, Laurent Pinchart, Sakari Ailus,
	Mauro Carvalho Chehab, Kate Hsuan, linux-media

On Wed, Jun 07, 2023 at 06:46:54PM +0200, Hans de Goede wrote:
> Now that the cci_* register access helpers are used access to
> the i2c_client after probe() is no longer necessary.

And even if needed can be retrieved from the dev anyway.

> Directly store a struct device *dev pointing to &client->dev inside
> ov2680_dev to make the code simpler.

...

> +static int ov2680_v4l2_register(struct ov2680_dev *sensor, struct i2c_client *client)
>  {
>  	const struct v4l2_ctrl_ops *ops = &ov2680_ctrl_ops;
>  	struct ov2680_ctrls *ctrls = &sensor->ctrls;
>  	struct v4l2_ctrl_handler *hdl = &ctrls->handler;
>  	int ret = 0;
>  
> -	v4l2_i2c_subdev_init(&sensor->sd, sensor->i2c_client,
> -			     &ov2680_subdev_ops);
> +	v4l2_i2c_subdev_init(&sensor->sd, client, &ov2680_subdev_ops);

So, not sure if you prefer

	struct i2c_client *client = to_i2c_client(sensor->dev);

here instead of adding a new parameter.

-- 
With Best Regards,
Andy Shevchenko



^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH 14/28] media: ov2680: Add support for more clk setups
  2023-06-07 16:46 ` [PATCH 14/28] media: ov2680: Add support for more clk setups Hans de Goede
@ 2023-06-07 20:51   ` Andy Shevchenko
  0 siblings, 0 replies; 57+ messages in thread
From: Andy Shevchenko @ 2023-06-07 20:51 UTC (permalink / raw)
  To: Hans de Goede
  Cc: Daniel Scally, Laurent Pinchart, Sakari Ailus,
	Mauro Carvalho Chehab, Kate Hsuan, linux-media

On Wed, Jun 07, 2023 at 06:46:58PM +0200, Hans de Goede wrote:
> On ACPI systems the following 2 scenarios are possible:
> 
> 1. The xvclk is fully controlled by ACPI powermanagement, so there
>    is no "xvclk" for the driver to get (since it is abstracted away).
>    In this case there will be a "clock-frequency" device property
>    to tell the driver the xvclk rate.
> 
> 2. There is a xvclk modelled in the clk framework for the driver,
>    but the clk-generator may not be set to the right frequency
>    yet. In this case there will also be a "clock-frequency" device
>    property and the driver is expected to set the rate of the xvclk
>    through this frequency through the clk framework.
> 
> Handle both these scenarios by switching to devm_clk_get_optional()
> and checking for a "clock-frequency" device property.
> 
> This is modelled after how this same issues was fixed for the ov8865 in

this --> the

> commit 73dcffeb2ff9 ("media: i2c: Support 19.2MHz input clock in ov8865").

...

> +	/*
> +	 * We could have either a 24MHz or 19.2MHz clock rate from either dt or

DT

> +	 * ACPI...but we also need to support the weird IPU3 case which will

ACPI... but

> +	 * have an external clock AND a clock-frequency property. Check for the
> +	 * clock-frequency property and if found, set that rate if we managed
> +	 * to acquire a clock. This should cover the ACPI case. If the system
> +	 * uses devicetree then the configured rate should already be set, so
> +	 * we can just read it.
> +	 */
> +	ret = fwnode_property_read_u32(dev_fwnode(dev), "clock-frequency", &rate);

	if (ret && !sensor->xvclk)
		return dev_err_probe(dev, ret, "invalid clock config\n");

('else' is redundant)

> +	if (!ret && sensor->xvclk) {
> +		ret = clk_set_rate(sensor->xvclk, rate);
> +		if (ret)
> +			return dev_err_probe(dev, ret, "failed to set clock rate\n");
> +	} else if (ret && !sensor->xvclk) {
> +		return dev_err_probe(dev, ret, "invalid clock config\n");
> +	}

...

> +	sensor->xvclk_freq = rate ? rate : clk_get_rate(sensor->xvclk);

Elvis can be used:

	sensor->xvclk_freq = rate ?: clk_get_rate(sensor->xvclk);

-- 
With Best Regards,
Andy Shevchenko



^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH 15/28] media: ov2680: Add support for 19.2 MHz clock
  2023-06-07 16:46 ` [PATCH 15/28] media: ov2680: Add support for 19.2 MHz clock Hans de Goede
@ 2023-06-07 20:53   ` Andy Shevchenko
  0 siblings, 0 replies; 57+ messages in thread
From: Andy Shevchenko @ 2023-06-07 20:53 UTC (permalink / raw)
  To: Hans de Goede
  Cc: Daniel Scally, Laurent Pinchart, Sakari Ailus,
	Mauro Carvalho Chehab, Kate Hsuan, linux-media

On Wed, Jun 07, 2023 at 06:46:59PM +0200, Hans de Goede wrote:
> Most x86/ACPI boards use the ov2680 with a 19.2 MHz xvclk,
> rather then the expected 24MHz, add support for this.
> 
> Compensate for the lower clk by setting a higher PLL multiplier
> of 69 when using 19.2 MHz vs the default multiplier of 55 for
> a 24MHz xvclk.

...

> @@ -720,12 +736,19 @@ static int ov2680_parse_dt(struct ov2680_dev *sensor)

I believe this call is solely the part of the ->probe() flow, hence...

...

> +	if (i == ARRAY_SIZE(ov2680_xvclk_freqs)) {
> +		dev_err(dev, "unsupported xvclk frequency %d Hz\n", sensor->xvclk_freq);
>  		return -EINVAL;

	return dev_err_probe(...);

?

>  	}

-- 
With Best Regards,
Andy Shevchenko



^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH 17/28] media: ov2680: Add support for ACPI enumeration
  2023-06-07 16:47 ` [PATCH 17/28] media: ov2680: Add support for ACPI enumeration Hans de Goede
@ 2023-06-07 20:57   ` Andy Shevchenko
  0 siblings, 0 replies; 57+ messages in thread
From: Andy Shevchenko @ 2023-06-07 20:57 UTC (permalink / raw)
  To: Hans de Goede
  Cc: Daniel Scally, Laurent Pinchart, Sakari Ailus,
	Mauro Carvalho Chehab, Kate Hsuan, linux-media

On Wed, Jun 07, 2023 at 06:47:01PM +0200, Hans de Goede wrote:
> Add an acpi_match_table now that all the other bits necessary for
> ACPI support are in place.

...

>  		.name  = "ov2680",
>  		.pm = pm_sleep_ptr(&ov2680_pm_ops),

>  		.of_match_table	= of_match_ptr(ov2680_dt_ids),

Side note. If we don't have OF dependency, this may provoke defined but not
used. That's why I eagerly remove of_match_ptr() from the ID tables.

Besides that, however might not be applicable right now, this will allow
to use PRP0001 ACPI HID.

> +		.acpi_match_table = ov2680_acpi_ids,
>  	},
>  	.probe_new	= ov2680_probe,
>  	.remove		= ov2680_remove,

-- 
With Best Regards,
Andy Shevchenko



^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH 23/28] media: ov2680: Implement selection support
  2023-06-07 16:47 ` [PATCH 23/28] media: ov2680: Implement selection support Hans de Goede
@ 2023-06-07 21:01   ` Andy Shevchenko
  2023-06-07 21:04   ` Andy Shevchenko
  1 sibling, 0 replies; 57+ messages in thread
From: Andy Shevchenko @ 2023-06-07 21:01 UTC (permalink / raw)
  To: Hans de Goede
  Cc: Daniel Scally, Laurent Pinchart, Sakari Ailus,
	Mauro Carvalho Chehab, Kate Hsuan, linux-media

On Wed, Jun 07, 2023 at 06:47:07PM +0200, Hans de Goede wrote:
> Implement selection support. Modelled after ov5693 selection support,
> but allow setting sizes smaller than crop-size through set_fmt since

->set_fmt()

> that was already allowed.

-- 
With Best Regards,
Andy Shevchenko



^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH 23/28] media: ov2680: Implement selection support
  2023-06-07 16:47 ` [PATCH 23/28] media: ov2680: Implement selection support Hans de Goede
  2023-06-07 21:01   ` Andy Shevchenko
@ 2023-06-07 21:04   ` Andy Shevchenko
  1 sibling, 0 replies; 57+ messages in thread
From: Andy Shevchenko @ 2023-06-07 21:04 UTC (permalink / raw)
  To: Hans de Goede
  Cc: Daniel Scally, Laurent Pinchart, Sakari Ailus,
	Mauro Carvalho Chehab, Kate Hsuan, linux-media

On Wed, Jun 07, 2023 at 06:47:07PM +0200, Hans de Goede wrote:
> Implement selection support. Modelled after ov5693 selection support,
> but allow setting sizes smaller than crop-size through set_fmt since
> that was already allowed.

...

> +	/* Limit set_fmt max size to crop width / height */
> +	width = clamp_t(unsigned int, ALIGN(format->format.width, 2),
> +			OV2680_MIN_CROP_WIDTH, crop->width);
> +	height = clamp_t(unsigned int, ALIGN(format->format.height, 2),
> +			 OV2680_MIN_CROP_HEIGHT, crop->height);

I'm wondering if clamp_val() would work here.

...

> +	rect.left = clamp(ALIGN(sel->r.left, 2), OV2680_NATIVE_START_LEFT,
> +			  OV2680_NATIVE_WIDTH);
> +	rect.top = clamp(ALIGN(sel->r.top, 2), OV2680_NATIVE_START_TOP,
> +			 OV2680_NATIVE_HEIGHT);
> +	rect.width = clamp_t(unsigned int, ALIGN(sel->r.width, 2),
> +			     OV2680_MIN_CROP_WIDTH, OV2680_NATIVE_WIDTH);
> +	rect.height = clamp_t(unsigned int, ALIGN(sel->r.height, 2),
> +			      OV2680_MIN_CROP_HEIGHT, OV2680_NATIVE_HEIGHT);

Ditto.

-- 
With Best Regards,
Andy Shevchenko



^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH 01/28] media: ov2680: Remove auto-gain and auto-exposure controls
  2023-06-07 16:46 ` [PATCH 01/28] media: ov2680: Remove auto-gain and auto-exposure controls Hans de Goede
@ 2023-06-08 12:41   ` Sakari Ailus
  2023-06-12  6:53   ` Dan Scally
  1 sibling, 0 replies; 57+ messages in thread
From: Sakari Ailus @ 2023-06-08 12:41 UTC (permalink / raw)
  To: Hans de Goede
  Cc: Daniel Scally, Laurent Pinchart, Andy Shevchenko,
	Mauro Carvalho Chehab, Kate Hsuan, linux-media

Hi Hans,

On Wed, Jun 07, 2023 at 06:46:45PM +0200, Hans de Goede wrote:
> Quoting the OV2680 datasheet:
> 
> "3.2 exposure and gain control
> 
> In the OV2680, the exposure time and gain are set manually from an external
> controller. The OV2680 supports manual gain and exposure control only for
> normal applications, no auto mode."
> 
> And indeed testing with the atomisp_ov2680 fork of ov2680.c has shown that
> auto-exposure and auto-gain do not work.
> 
> Note that the code setting the auto-exposure flag was broken, callers
> of ov2680_exposure_set() were directly passing !!ctrls->auto_exp->val as
> "bool auto_exp" value, but ctrls->auto_exp is a menu control with:
> 
> enum  v4l2_exposure_auto_type {
>         V4L2_EXPOSURE_AUTO = 0,
>         V4L2_EXPOSURE_MANUAL = 1,
> 	...
> 
> So instead of passing !!ctrls->auto_exp->val they should have been passing
> ctrls->auto_exp->val == V4L2_EXPOSURE_AUTO, iow the passed value was
> inverted of what it should have been.
> 
> Also remove ov2680_g_volatile_ctrl() since without auto support the gain
> and exposure controls are not volatile.
> 
> This also fixes the control values not being properly applied in
> ov2680_mode_set(). The 800x600 mode register-list also sets gain,
> exposure and vflip overriding the last set ctrl values.
> 
> ov2680_mode_set() does call ov2680_gain_set() and ov2680_exposure_set()
> but did this before writing the mode register-list, so these values
> would still be overridden by the mode register-list.
> 
> Add a v4l2_ctrl_handler_setup() call after writing the mode register-list
> to restore all ctrl values. Also remove the ctrls->gain->is_new check from
> ov2680_gain_set() so that the gain always gets restored properly.
> 
> Last since ov2680_mode_set() now calls v4l2_ctrl_handler_setup(), remove
> the v4l2_ctrl_handler_setup() call after ov2680_mode_restore() since
> ov2680_mode_restore() calls ov2680_mode_set().
> 
> Fixes: 3ee47cad3e69 ("media: ov2680: Add Omnivision OV2680 sensor driver")
> Signed-off-by: Hans de Goede <hdegoede@redhat.com>

It is interesting, to say at least, to see this and the few following
patches. No-one has previously complained. Makes me also wonder how the
original driver was tested.

Could you also cc the original author who is also listed as the current
maintainter of the driver for v2?

-- 
Kind regards,

Sakari Ailus

^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH 05/28] media: ov2680: Don't take the lock for try_fmt calls
  2023-06-07 16:46 ` [PATCH 05/28] media: ov2680: Don't take the lock for try_fmt calls Hans de Goede
@ 2023-06-08 12:44   ` Sakari Ailus
  2023-06-08 12:48     ` Laurent Pinchart
  0 siblings, 1 reply; 57+ messages in thread
From: Sakari Ailus @ 2023-06-08 12:44 UTC (permalink / raw)
  To: Hans de Goede
  Cc: Daniel Scally, Laurent Pinchart, Andy Shevchenko,
	Mauro Carvalho Chehab, Kate Hsuan, linux-media

Hi Hans,

On Wed, Jun 07, 2023 at 06:46:49PM +0200, Hans de Goede wrote:
> On ov2680_set_fmt() calls with format->which == V4L2_SUBDEV_FORMAT_TRY,
> ov2680_set_fmt() does not talk to the sensor.
> 
> So in this case there is no need to lock the sensor->lock mutex or
> to check that the sensor is streaming.
> 
> Fixes: 3ee47cad3e69 ("media: ov2680: Add Omnivision OV2680 sensor driver")
> Signed-off-by: Hans de Goede <hdegoede@redhat.com>
> ---
>  drivers/media/i2c/ov2680.c | 20 +++++++++-----------
>  1 file changed, 9 insertions(+), 11 deletions(-)
> 
> diff --git a/drivers/media/i2c/ov2680.c b/drivers/media/i2c/ov2680.c
> index d90bbca6e913..a26a6f18f4f1 100644
> --- a/drivers/media/i2c/ov2680.c
> +++ b/drivers/media/i2c/ov2680.c
> @@ -592,24 +592,22 @@ static int ov2680_set_fmt(struct v4l2_subdev *sd,
>  	if (format->pad != 0)
>  		return -EINVAL;
>  
> -	mutex_lock(&sensor->lock);
> -
> -	if (sensor->is_streaming) {
> -		ret = -EBUSY;
> -		goto unlock;
> -	}
> -
>  	mode = v4l2_find_nearest_size(ov2680_mode_data,
>  				      ARRAY_SIZE(ov2680_mode_data), width,
>  				      height, fmt->width, fmt->height);
> -	if (!mode) {
> -		ret = -EINVAL;
> -		goto unlock;
> -	}
> +	if (!mode)
> +		return -EINVAL;
>  
>  	if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
>  		try_fmt = v4l2_subdev_get_try_format(sd, sd_state, 0);
>  		format->format = *try_fmt;

Access to sd_state needs to be serialised, so mutex should be held here.

> +		return 0;
> +	}
> +
> +	mutex_lock(&sensor->lock);
> +
> +	if (sensor->is_streaming) {
> +		ret = -EBUSY;
>  		goto unlock;
>  	}
>  

-- 
Kind regards,

Sakari Ailus

^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH 05/28] media: ov2680: Don't take the lock for try_fmt calls
  2023-06-08 12:44   ` Sakari Ailus
@ 2023-06-08 12:48     ` Laurent Pinchart
  2023-06-08 13:15       ` Sakari Ailus
  2023-06-08 13:17       ` Hans de Goede
  0 siblings, 2 replies; 57+ messages in thread
From: Laurent Pinchart @ 2023-06-08 12:48 UTC (permalink / raw)
  To: Sakari Ailus
  Cc: Hans de Goede, Daniel Scally, Andy Shevchenko,
	Mauro Carvalho Chehab, Kate Hsuan, linux-media

Hi Sakari,

On Thu, Jun 08, 2023 at 12:44:51PM +0000, Sakari Ailus wrote:
> On Wed, Jun 07, 2023 at 06:46:49PM +0200, Hans de Goede wrote:
> > On ov2680_set_fmt() calls with format->which == V4L2_SUBDEV_FORMAT_TRY,
> > ov2680_set_fmt() does not talk to the sensor.
> > 
> > So in this case there is no need to lock the sensor->lock mutex or
> > to check that the sensor is streaming.
> > 
> > Fixes: 3ee47cad3e69 ("media: ov2680: Add Omnivision OV2680 sensor driver")
> > Signed-off-by: Hans de Goede <hdegoede@redhat.com>
> > ---
> >  drivers/media/i2c/ov2680.c | 20 +++++++++-----------
> >  1 file changed, 9 insertions(+), 11 deletions(-)
> > 
> > diff --git a/drivers/media/i2c/ov2680.c b/drivers/media/i2c/ov2680.c
> > index d90bbca6e913..a26a6f18f4f1 100644
> > --- a/drivers/media/i2c/ov2680.c
> > +++ b/drivers/media/i2c/ov2680.c
> > @@ -592,24 +592,22 @@ static int ov2680_set_fmt(struct v4l2_subdev *sd,
> >  	if (format->pad != 0)
> >  		return -EINVAL;
> >  
> > -	mutex_lock(&sensor->lock);
> > -
> > -	if (sensor->is_streaming) {
> > -		ret = -EBUSY;
> > -		goto unlock;
> > -	}
> > -
> >  	mode = v4l2_find_nearest_size(ov2680_mode_data,
> >  				      ARRAY_SIZE(ov2680_mode_data), width,
> >  				      height, fmt->width, fmt->height);
> > -	if (!mode) {
> > -		ret = -EINVAL;
> > -		goto unlock;
> > -	}
> > +	if (!mode)
> > +		return -EINVAL;
> >  
> >  	if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
> >  		try_fmt = v4l2_subdev_get_try_format(sd, sd_state, 0);
> >  		format->format = *try_fmt;
> 
> Access to sd_state needs to be serialised, so mutex should be held here.

This operation should be called with the state lock held already, so I
don't think any extra locking is needed.

> > +		return 0;
> > +	}
> > +
> > +	mutex_lock(&sensor->lock);
> > +
> > +	if (sensor->is_streaming) {
> > +		ret = -EBUSY;
> >  		goto unlock;
> >  	}
> >  

-- 
Regards,

Laurent Pinchart

^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH 05/28] media: ov2680: Don't take the lock for try_fmt calls
  2023-06-08 12:48     ` Laurent Pinchart
@ 2023-06-08 13:15       ` Sakari Ailus
  2023-06-08 13:17       ` Hans de Goede
  1 sibling, 0 replies; 57+ messages in thread
From: Sakari Ailus @ 2023-06-08 13:15 UTC (permalink / raw)
  To: Laurent Pinchart
  Cc: Hans de Goede, Daniel Scally, Andy Shevchenko,
	Mauro Carvalho Chehab, Kate Hsuan, linux-media

Hi Laurent,

On Thu, Jun 08, 2023 at 03:48:30PM +0300, Laurent Pinchart wrote:
> Hi Sakari,
> 
> On Thu, Jun 08, 2023 at 12:44:51PM +0000, Sakari Ailus wrote:
> > On Wed, Jun 07, 2023 at 06:46:49PM +0200, Hans de Goede wrote:
> > > On ov2680_set_fmt() calls with format->which == V4L2_SUBDEV_FORMAT_TRY,
> > > ov2680_set_fmt() does not talk to the sensor.
> > > 
> > > So in this case there is no need to lock the sensor->lock mutex or
> > > to check that the sensor is streaming.
> > > 
> > > Fixes: 3ee47cad3e69 ("media: ov2680: Add Omnivision OV2680 sensor driver")
> > > Signed-off-by: Hans de Goede <hdegoede@redhat.com>
> > > ---
> > >  drivers/media/i2c/ov2680.c | 20 +++++++++-----------
> > >  1 file changed, 9 insertions(+), 11 deletions(-)
> > > 
> > > diff --git a/drivers/media/i2c/ov2680.c b/drivers/media/i2c/ov2680.c
> > > index d90bbca6e913..a26a6f18f4f1 100644
> > > --- a/drivers/media/i2c/ov2680.c
> > > +++ b/drivers/media/i2c/ov2680.c
> > > @@ -592,24 +592,22 @@ static int ov2680_set_fmt(struct v4l2_subdev *sd,
> > >  	if (format->pad != 0)
> > >  		return -EINVAL;
> > >  
> > > -	mutex_lock(&sensor->lock);
> > > -
> > > -	if (sensor->is_streaming) {
> > > -		ret = -EBUSY;
> > > -		goto unlock;
> > > -	}
> > > -
> > >  	mode = v4l2_find_nearest_size(ov2680_mode_data,
> > >  				      ARRAY_SIZE(ov2680_mode_data), width,
> > >  				      height, fmt->width, fmt->height);
> > > -	if (!mode) {
> > > -		ret = -EINVAL;
> > > -		goto unlock;
> > > -	}
> > > +	if (!mode)
> > > +		return -EINVAL;
> > >  
> > >  	if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
> > >  		try_fmt = v4l2_subdev_get_try_format(sd, sd_state, 0);
> > >  		format->format = *try_fmt;
> > 
> > Access to sd_state needs to be serialised, so mutex should be held here.
> 
> This operation should be called with the state lock held already, so I
> don't think any extra locking is needed.

Ah, you're right indeed. I suppose lock here is redundant but controls need
some care.

-- 
Regards,

Sakari Ailus

^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH 05/28] media: ov2680: Don't take the lock for try_fmt calls
  2023-06-08 12:48     ` Laurent Pinchart
  2023-06-08 13:15       ` Sakari Ailus
@ 2023-06-08 13:17       ` Hans de Goede
  1 sibling, 0 replies; 57+ messages in thread
From: Hans de Goede @ 2023-06-08 13:17 UTC (permalink / raw)
  To: Laurent Pinchart, Sakari Ailus
  Cc: Daniel Scally, Andy Shevchenko, Mauro Carvalho Chehab, Kate Hsuan,
	linux-media

Hi,

On 6/8/23 14:48, Laurent Pinchart wrote:
> Hi Sakari,
> 
> On Thu, Jun 08, 2023 at 12:44:51PM +0000, Sakari Ailus wrote:
>> On Wed, Jun 07, 2023 at 06:46:49PM +0200, Hans de Goede wrote:
>>> On ov2680_set_fmt() calls with format->which == V4L2_SUBDEV_FORMAT_TRY,
>>> ov2680_set_fmt() does not talk to the sensor.
>>>
>>> So in this case there is no need to lock the sensor->lock mutex or
>>> to check that the sensor is streaming.
>>>
>>> Fixes: 3ee47cad3e69 ("media: ov2680: Add Omnivision OV2680 sensor driver")
>>> Signed-off-by: Hans de Goede <hdegoede@redhat.com>
>>> ---
>>>  drivers/media/i2c/ov2680.c | 20 +++++++++-----------
>>>  1 file changed, 9 insertions(+), 11 deletions(-)
>>>
>>> diff --git a/drivers/media/i2c/ov2680.c b/drivers/media/i2c/ov2680.c
>>> index d90bbca6e913..a26a6f18f4f1 100644
>>> --- a/drivers/media/i2c/ov2680.c
>>> +++ b/drivers/media/i2c/ov2680.c
>>> @@ -592,24 +592,22 @@ static int ov2680_set_fmt(struct v4l2_subdev *sd,
>>>  	if (format->pad != 0)
>>>  		return -EINVAL;
>>>  
>>> -	mutex_lock(&sensor->lock);
>>> -
>>> -	if (sensor->is_streaming) {
>>> -		ret = -EBUSY;
>>> -		goto unlock;
>>> -	}
>>> -
>>>  	mode = v4l2_find_nearest_size(ov2680_mode_data,
>>>  				      ARRAY_SIZE(ov2680_mode_data), width,
>>>  				      height, fmt->width, fmt->height);
>>> -	if (!mode) {
>>> -		ret = -EINVAL;
>>> -		goto unlock;
>>> -	}
>>> +	if (!mode)
>>> +		return -EINVAL;
>>>  
>>>  	if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
>>>  		try_fmt = v4l2_subdev_get_try_format(sd, sd_state, 0);
>>>  		format->format = *try_fmt;
>>
>> Access to sd_state needs to be serialised, so mutex should be held here.
> 
> This operation should be called with the state lock held already, so I
> don't think any extra locking is needed.

Right sd_state is allocated per v4l2-subdev fd so I would expect
the caller to take care of any necessary locking here.

Regards,

Hans




> 
>>> +		return 0;
>>> +	}
>>> +
>>> +	mutex_lock(&sensor->lock);
>>> +
>>> +	if (sensor->is_streaming) {
>>> +		ret = -EBUSY;
>>>  		goto unlock;
>>>  	}
>>>  
> 


^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH 00/28] media: ov2680: Bugfixes + ACPI + selection(crop-tgt) API support
  2023-06-07 16:46 [PATCH 00/28] media: ov2680: Bugfixes + ACPI + selection(crop-tgt) API support Hans de Goede
                   ` (27 preceding siblings ...)
  2023-06-07 16:47 ` [PATCH 28/28] media: ov2680: Read and log sensor revision during probe Hans de Goede
@ 2023-06-09  9:37 ` Sakari Ailus
  2023-06-09 14:28   ` Hans de Goede
  28 siblings, 1 reply; 57+ messages in thread
From: Sakari Ailus @ 2023-06-09  9:37 UTC (permalink / raw)
  To: Hans de Goede
  Cc: Daniel Scally, Laurent Pinchart, Andy Shevchenko,
	Mauro Carvalho Chehab, Kate Hsuan, linux-media

Hi Hans,

On Wed, Jun 07, 2023 at 06:46:44PM +0200, Hans de Goede wrote:
> Hi All,
> 
> During all the work done on the atomisp driver I have mostly been testing
> on devices with an ov2680 sensor. As such I have also done a lot of work
> on the atomisp-ov2680.c atomisp specific sensor driver.

Could you wrap the lines at or before 80 characters, unless there are
reason (generally other coding style rules) to keep them longer?

-- 
Kind regards,

Sakari Ailus

^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH 00/28] media: ov2680: Bugfixes + ACPI + selection(crop-tgt) API support
  2023-06-09  9:37 ` [PATCH 00/28] media: ov2680: Bugfixes + ACPI + selection(crop-tgt) API support Sakari Ailus
@ 2023-06-09 14:28   ` Hans de Goede
  0 siblings, 0 replies; 57+ messages in thread
From: Hans de Goede @ 2023-06-09 14:28 UTC (permalink / raw)
  To: Sakari Ailus
  Cc: Daniel Scally, Laurent Pinchart, Andy Shevchenko,
	Mauro Carvalho Chehab, Kate Hsuan, linux-media

Hi Sakari,

On 6/9/23 11:37, Sakari Ailus wrote:
> Hi Hans,
> 
> On Wed, Jun 07, 2023 at 06:46:44PM +0200, Hans de Goede wrote:
>> Hi All,
>>
>> During all the work done on the atomisp driver I have mostly been testing
>> on devices with an ov2680 sensor. As such I have also done a lot of work
>> on the atomisp-ov2680.c atomisp specific sensor driver.
> 
> Could you wrap the lines at or before 80 characters, unless there are
> reason (generally other coding style rules) to keep them longer?

I can certainly do that. But the kernel has stopped requiring this now
and I often find that sticking within the new official limit of
100 chars leads to IMHO better readable code the needlessly breaking
the lines in 2.

Are there any specific reasons why you want to keep enforcing
the old and obsolete 80 chars limit ?

I must say that inconsistent enforcing of a 80 vs 100 chars limit
across the kernel is quite confusing for contributors.

E.g. I'm pretty sure that if I had stuck to 80 chars with this
patch-set from the start that Andy would then have pointed out
several places where I had needlessly broken a lone in 2.

So having 2 different limits leads to reviewers contradicting
each other which is really not helpful.

SO IMHO if drivers/media is going to keep enforcing a 80 chars
limit then this needs to be clearly documented (with rationale)
somewhere. Or did I miss this already being documented somewhere ?

Regards,

Hans





^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH 01/28] media: ov2680: Remove auto-gain and auto-exposure controls
  2023-06-07 16:46 ` [PATCH 01/28] media: ov2680: Remove auto-gain and auto-exposure controls Hans de Goede
  2023-06-08 12:41   ` Sakari Ailus
@ 2023-06-12  6:53   ` Dan Scally
  2023-06-12  7:44     ` Hans de Goede
  1 sibling, 1 reply; 57+ messages in thread
From: Dan Scally @ 2023-06-12  6:53 UTC (permalink / raw)
  To: Hans de Goede, Laurent Pinchart, Sakari Ailus, Andy Shevchenko
  Cc: Mauro Carvalho Chehab, Kate Hsuan, linux-media

Morning Hans - thanks for the set

On 07/06/2023 17:46, Hans de Goede wrote:
> Quoting the OV2680 datasheet:
>
> "3.2 exposure and gain control
>
> In the OV2680, the exposure time and gain are set manually from an external
> controller. The OV2680 supports manual gain and exposure control only for
> normal applications, no auto mode."
>
> And indeed testing with the atomisp_ov2680 fork of ov2680.c has shown that
> auto-exposure and auto-gain do not work.
>
> Note that the code setting the auto-exposure flag was broken, callers
> of ov2680_exposure_set() were directly passing !!ctrls->auto_exp->val as
> "bool auto_exp" value, but ctrls->auto_exp is a menu control with:
>
> enum  v4l2_exposure_auto_type {
>          V4L2_EXPOSURE_AUTO = 0,
>          V4L2_EXPOSURE_MANUAL = 1,
> 	...
>
> So instead of passing !!ctrls->auto_exp->val they should have been passing
> ctrls->auto_exp->val == V4L2_EXPOSURE_AUTO, iow the passed value was
> inverted of what it should have been.
>
> Also remove ov2680_g_volatile_ctrl() since without auto support the gain
> and exposure controls are not volatile.
>
> This also fixes the control values not being properly applied in
> ov2680_mode_set(). The 800x600 mode register-list also sets gain,
> exposure and vflip overriding the last set ctrl values.
>
> ov2680_mode_set() does call ov2680_gain_set() and ov2680_exposure_set()
> but did this before writing the mode register-list, so these values
> would still be overridden by the mode register-list.
>
> Add a v4l2_ctrl_handler_setup() call after writing the mode register-list
> to restore all ctrl values. Also remove the ctrls->gain->is_new check from
> ov2680_gain_set() so that the gain always gets restored properly.
>
> Last since ov2680_mode_set() now calls v4l2_ctrl_handler_setup(), remove
> the v4l2_ctrl_handler_setup() call after ov2680_mode_restore() since
> ov2680_mode_restore() calls ov2680_mode_set().
>
> Fixes: 3ee47cad3e69 ("media: ov2680: Add Omnivision OV2680 sensor driver")
> Signed-off-by: Hans de Goede <hdegoede@redhat.com>
> ---
>   drivers/media/i2c/ov2680.c | 162 ++++---------------------------------
>   1 file changed, 17 insertions(+), 145 deletions(-)
>
> diff --git a/drivers/media/i2c/ov2680.c b/drivers/media/i2c/ov2680.c
> index 54153bf66bdd..02204e185e2f 100644
> --- a/drivers/media/i2c/ov2680.c
> +++ b/drivers/media/i2c/ov2680.c
> @@ -85,15 +85,8 @@ struct ov2680_mode_info {
>   
>   struct ov2680_ctrls {
>   	struct v4l2_ctrl_handler handler;
> -	struct {
> -		struct v4l2_ctrl *auto_exp;
> -		struct v4l2_ctrl *exposure;
> -	};
> -	struct {
> -		struct v4l2_ctrl *auto_gain;
> -		struct v4l2_ctrl *gain;
> -	};
> -
> +	struct v4l2_ctrl *exposure;
> +	struct v4l2_ctrl *gain;
>   	struct v4l2_ctrl *hflip;
>   	struct v4l2_ctrl *vflip;
>   	struct v4l2_ctrl *test_pattern;
> @@ -143,6 +136,7 @@ static const struct reg_value ov2680_setting_30fps_QUXGA_800_600[] = {
>   	{0x380e, 0x02}, {0x380f, 0x84}, {0x3811, 0x04}, {0x3813, 0x04},
>   	{0x3814, 0x31}, {0x3815, 0x31}, {0x3820, 0xc0}, {0x4008, 0x00},
>   	{0x4009, 0x03}, {0x4837, 0x1e}, {0x3501, 0x4e}, {0x3502, 0xe0},
> +	{0x3503, 0x03},


I'm confused why this was added to this mode but not the other two; according to the datasheet it's 
setting the AGC/AEC into manual but I can't see why it's only needed here?

>   };
>   
>   static const struct reg_value ov2680_setting_30fps_720P_1280_720[] = {
> @@ -405,69 +399,14 @@ static int ov2680_test_pattern_set(struct ov2680_dev *sensor, int value)
>   	return 0;
>   }
>   
> -static int ov2680_gain_set(struct ov2680_dev *sensor, bool auto_gain)
> +static int ov2680_gain_set(struct ov2680_dev *sensor, u32 gain)
>   {
> -	struct ov2680_ctrls *ctrls = &sensor->ctrls;
> -	u32 gain;
> -	int ret;
> -
> -	ret = ov2680_mod_reg(sensor, OV2680_REG_R_MANUAL, BIT(1),
> -			     auto_gain ? 0 : BIT(1));
> -	if (ret < 0)
> -		return ret;
> -
> -	if (auto_gain || !ctrls->gain->is_new)
> -		return 0;
> -
> -	gain = ctrls->gain->val;
> -
> -	ret = ov2680_write_reg16(sensor, OV2680_REG_GAIN_PK, gain);
> -
> -	return 0;
> +	return ov2680_write_reg16(sensor, OV2680_REG_GAIN_PK, gain);
>   }
>   
> -static int ov2680_gain_get(struct ov2680_dev *sensor)
> +static int ov2680_exposure_set(struct ov2680_dev *sensor, u32 exp)
>   {
> -	u32 gain;
> -	int ret;
> -
> -	ret = ov2680_read_reg16(sensor, OV2680_REG_GAIN_PK, &gain);
> -	if (ret)
> -		return ret;
> -
> -	return gain;
> -}
> -
> -static int ov2680_exposure_set(struct ov2680_dev *sensor, bool auto_exp)
> -{
> -	struct ov2680_ctrls *ctrls = &sensor->ctrls;
> -	u32 exp;
> -	int ret;
> -
> -	ret = ov2680_mod_reg(sensor, OV2680_REG_R_MANUAL, BIT(0),
> -			     auto_exp ? 0 : BIT(0));
> -	if (ret < 0)
> -		return ret;
> -
> -	if (auto_exp || !ctrls->exposure->is_new)
> -		return 0;
> -
> -	exp = (u32)ctrls->exposure->val;
> -	exp <<= 4;
> -
> -	return ov2680_write_reg24(sensor, OV2680_REG_EXPOSURE_PK_HIGH, exp);
> -}
> -
> -static int ov2680_exposure_get(struct ov2680_dev *sensor)
> -{
> -	int ret;
> -	u32 exp;
> -
> -	ret = ov2680_read_reg24(sensor, OV2680_REG_EXPOSURE_PK_HIGH, &exp);
> -	if (ret)
> -		return ret;
> -
> -	return exp >> 4;
> +	return ov2680_write_reg24(sensor, OV2680_REG_EXPOSURE_PK_HIGH, exp << 4);
>   }
>   
>   static int ov2680_stream_enable(struct ov2680_dev *sensor)
> @@ -482,32 +421,16 @@ static int ov2680_stream_disable(struct ov2680_dev *sensor)
>   
>   static int ov2680_mode_set(struct ov2680_dev *sensor)
>   {
> -	struct ov2680_ctrls *ctrls = &sensor->ctrls;
>   	int ret;
>   
> -	ret = ov2680_gain_set(sensor, false);
> -	if (ret < 0)
> -		return ret;
> -
> -	ret = ov2680_exposure_set(sensor, false);
> -	if (ret < 0)
> -		return ret;
> -
>   	ret = ov2680_load_regs(sensor, sensor->current_mode);
>   	if (ret < 0)
>   		return ret;
>   
> -	if (ctrls->auto_gain->val) {
> -		ret = ov2680_gain_set(sensor, true);
> -		if (ret < 0)
> -			return ret;
> -	}
> -
> -	if (ctrls->auto_exp->val == V4L2_EXPOSURE_AUTO) {
> -		ret = ov2680_exposure_set(sensor, true);
> -		if (ret < 0)
> -			return ret;
> -	}
> +	/* Restore value of all ctrls */
> +	ret = __v4l2_ctrl_handler_setup(&sensor->ctrls.handler);
> +	if (ret < 0)
> +		return ret;
>   
>   	sensor->mode_pending_changes = false;
>   
> @@ -590,15 +513,10 @@ static int ov2680_s_power(struct v4l2_subdev *sd, int on)
>   	else
>   		ret = ov2680_power_off(sensor);
>   
> -	mutex_unlock(&sensor->lock);
> -
> -	if (on && ret == 0) {
> -		ret = v4l2_ctrl_handler_setup(&sensor->ctrls.handler);
> -		if (ret < 0)
> -			return ret;
> -
> +	if (on && ret == 0)
>   		ret = ov2680_mode_restore(sensor);
> -	}
> +
> +	mutex_unlock(&sensor->lock);
>   
>   	return ret;
>   }
> @@ -794,52 +712,19 @@ static int ov2680_enum_frame_interval(struct v4l2_subdev *sd,
>   	return 0;
>   }
>   
> -static int ov2680_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
> -{
> -	struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
> -	struct ov2680_dev *sensor = to_ov2680_dev(sd);
> -	struct ov2680_ctrls *ctrls = &sensor->ctrls;
> -	int val;
> -
> -	if (!sensor->is_enabled)
> -		return 0;
> -
> -	switch (ctrl->id) {
> -	case V4L2_CID_GAIN:
> -		val = ov2680_gain_get(sensor);
> -		if (val < 0)
> -			return val;
> -		ctrls->gain->val = val;
> -		break;
> -	case V4L2_CID_EXPOSURE:
> -		val = ov2680_exposure_get(sensor);
> -		if (val < 0)
> -			return val;
> -		ctrls->exposure->val = val;
> -		break;
> -	}
> -
> -	return 0;
> -}
> -
>   static int ov2680_s_ctrl(struct v4l2_ctrl *ctrl)
>   {
>   	struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
>   	struct ov2680_dev *sensor = to_ov2680_dev(sd);
> -	struct ov2680_ctrls *ctrls = &sensor->ctrls;
>   
>   	if (!sensor->is_enabled)
>   		return 0;
>   
>   	switch (ctrl->id) {
> -	case V4L2_CID_AUTOGAIN:
> -		return ov2680_gain_set(sensor, !!ctrl->val);
>   	case V4L2_CID_GAIN:
> -		return ov2680_gain_set(sensor, !!ctrls->auto_gain->val);
> -	case V4L2_CID_EXPOSURE_AUTO:
> -		return ov2680_exposure_set(sensor, !!ctrl->val);
> +		return ov2680_gain_set(sensor, ctrl->val);
>   	case V4L2_CID_EXPOSURE:
> -		return ov2680_exposure_set(sensor, !!ctrls->auto_exp->val);
> +		return ov2680_exposure_set(sensor, ctrl->val);
>   	case V4L2_CID_VFLIP:
>   		if (sensor->is_streaming)
>   			return -EBUSY;
> @@ -864,7 +749,6 @@ static int ov2680_s_ctrl(struct v4l2_ctrl *ctrl)
>   }
>   
>   static const struct v4l2_ctrl_ops ov2680_ctrl_ops = {
> -	.g_volatile_ctrl = ov2680_g_volatile_ctrl,
>   	.s_ctrl = ov2680_s_ctrl,
>   };
>   
> @@ -936,7 +820,7 @@ static int ov2680_v4l2_register(struct ov2680_dev *sensor)
>   	if (ret < 0)
>   		return ret;
>   
> -	v4l2_ctrl_handler_init(hdl, 7);
> +	v4l2_ctrl_handler_init(hdl, 5);
>   
>   	hdl->lock = &sensor->lock;
>   
> @@ -948,16 +832,9 @@ static int ov2680_v4l2_register(struct ov2680_dev *sensor)
>   					ARRAY_SIZE(test_pattern_menu) - 1,
>   					0, 0, test_pattern_menu);
>   
> -	ctrls->auto_exp = v4l2_ctrl_new_std_menu(hdl, ops,
> -						 V4L2_CID_EXPOSURE_AUTO,
> -						 V4L2_EXPOSURE_MANUAL, 0,
> -						 V4L2_EXPOSURE_AUTO);
> -
>   	ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE,
>   					    0, 32767, 1, 0);
>   
> -	ctrls->auto_gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_AUTOGAIN,
> -					     0, 1, 1, 1);
>   	ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN, 0, 2047, 1, 0);
>   
>   	if (hdl->error) {
> @@ -965,14 +842,9 @@ static int ov2680_v4l2_register(struct ov2680_dev *sensor)
>   		goto cleanup_entity;
>   	}
>   
> -	ctrls->gain->flags |= V4L2_CTRL_FLAG_VOLATILE;
> -	ctrls->exposure->flags |= V4L2_CTRL_FLAG_VOLATILE;
>   	ctrls->vflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
>   	ctrls->hflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
>   
> -	v4l2_ctrl_auto_cluster(2, &ctrls->auto_gain, 0, true);
> -	v4l2_ctrl_auto_cluster(2, &ctrls->auto_exp, 1, true);
> -
>   	sensor->sd.ctrl_handler = hdl;
>   
>   	ret = v4l2_async_register_subdev(&sensor->sd);

^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH 02/28] media: ov2680: Fix ov2680_bayer_order()
  2023-06-07 16:46 ` [PATCH 02/28] media: ov2680: Fix ov2680_bayer_order() Hans de Goede
@ 2023-06-12  7:22   ` Dan Scally
  0 siblings, 0 replies; 57+ messages in thread
From: Dan Scally @ 2023-06-12  7:22 UTC (permalink / raw)
  To: Hans de Goede, Laurent Pinchart, Sakari Ailus, Andy Shevchenko
  Cc: Mauro Carvalho Chehab, Kate Hsuan, linux-media

Hi Hans

On 07/06/2023 17:46, Hans de Goede wrote:
> The index into ov2680_hv_flip_bayer_order[] should be 0-3, but
> ov2680_bayer_order() was using 0 + BIT(2) + (BIT(2) << 1) as
> max index, while the intention was to use: 0 + 1 + 2 as max index.
>
> Fix the index calculation in ov2680_bayer_order(), while at it
> also just use the ctrl values rather then reading them back using
> a slow i2c-read transaction.
>
> This also allows making the function void, since there now are
> no more i2c-reads to error check.
>
> Note the check for the ctrls being NULL is there to allow
> adding an ov2680_fill_format() helper later, which will call
> ov2680_set_bayer_order() during probe() before the ctrls are created.
>
> Fixes: 3ee47cad3e69 ("media: ov2680: Add Omnivision OV2680 sensor driver")
> Signed-off-by: Hans de Goede <hdegoede@redhat.com>
> ---


Reviewed-by: Daniel Scally <dan.scally@ideasonboard.com>

>   drivers/media/i2c/ov2680.c | 27 ++++++++++-----------------
>   1 file changed, 10 insertions(+), 17 deletions(-)
>
> diff --git a/drivers/media/i2c/ov2680.c b/drivers/media/i2c/ov2680.c
> index 02204e185e2f..9a9c90c8a949 100644
> --- a/drivers/media/i2c/ov2680.c
> +++ b/drivers/media/i2c/ov2680.c
> @@ -315,26 +315,17 @@ static void ov2680_power_down(struct ov2680_dev *sensor)
>   	usleep_range(5000, 10000);
>   }
>   
> -static int ov2680_bayer_order(struct ov2680_dev *sensor)
> +static void ov2680_set_bayer_order(struct ov2680_dev *sensor)
>   {
> -	u32 format1;
> -	u32 format2;
> -	u32 hv_flip;
> -	int ret;
> +	int hv_flip = 0;
>   
> -	ret = ov2680_read_reg(sensor, OV2680_REG_FORMAT1, &format1);
> -	if (ret < 0)
> -		return ret;
> +	if (sensor->ctrls.vflip && sensor->ctrls.vflip->val)
> +		hv_flip += 1;
>   
> -	ret = ov2680_read_reg(sensor, OV2680_REG_FORMAT2, &format2);
> -	if (ret < 0)
> -		return ret;
> -
> -	hv_flip = (format2 & BIT(2)  << 1) | (format1 & BIT(2));
> +	if (sensor->ctrls.hflip && sensor->ctrls.hflip->val)
> +		hv_flip += 2;
>   
>   	sensor->fmt.code = ov2680_hv_flip_bayer_order[hv_flip];
> -
> -	return 0;
>   }
>   
>   static int ov2680_vflip_enable(struct ov2680_dev *sensor)
> @@ -345,7 +336,8 @@ static int ov2680_vflip_enable(struct ov2680_dev *sensor)
>   	if (ret < 0)
>   		return ret;
>   
> -	return ov2680_bayer_order(sensor);
> +	ov2680_set_bayer_order(sensor);
> +	return 0;
>   }
>   
>   static int ov2680_vflip_disable(struct ov2680_dev *sensor)
> @@ -378,7 +370,8 @@ static int ov2680_hflip_disable(struct ov2680_dev *sensor)
>   	if (ret < 0)
>   		return ret;
>   
> -	return ov2680_bayer_order(sensor);
> +	ov2680_set_bayer_order(sensor);
> +	return 0;
>   }
>   
>   static int ov2680_test_pattern_set(struct ov2680_dev *sensor, int value)

^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH 03/28] media: ov2680: Fix vflip / hflip set functions
  2023-06-07 16:46 ` [PATCH 03/28] media: ov2680: Fix vflip / hflip set functions Hans de Goede
@ 2023-06-12  7:32   ` Dan Scally
  2023-06-12  8:06     ` Hans de Goede
  0 siblings, 1 reply; 57+ messages in thread
From: Dan Scally @ 2023-06-12  7:32 UTC (permalink / raw)
  To: Hans de Goede, Laurent Pinchart, Sakari Ailus, Andy Shevchenko
  Cc: Mauro Carvalho Chehab, Kate Hsuan, linux-media

Hello Hans (and everybody else)

On 07/06/2023 17:46, Hans de Goede wrote:
> ov2680_vflip_disable() / ov2680_hflip_disable() pass BIT(0) instead of
> 0 as value to ov2680_mod_reg().
>
> While fixing this also:
>
> 1. Stop having separate enable/disable functions for hflip / vflip
> 2. Move the is_streaming check, which is unique to hflip / vflip
>     into the ov2680_set_?flip() functions.


I think the code in the patch is good, but I wonder about the right way to deal with the changed 
pixel ordering following a flip/mirror. None of the other Omnivision sensor drivers I've looked at 
disallow a flip/mirror whilst streaming, but of course doing so wrecks the colours. I don't think 
that there's any reason we couldn't make the equivalent calculation to ov2680_set_bayer_order() 
within libcamera though and configure the ipu3-imgu appropriately, and so properly support a 
flip-whilst-streaming...but quite possibly I'm missing something. So I'm leaning towards "let's just 
drop the is_streaming check", but I'm interested in what everyone else thinks.

> for a nice code cleanup.
>
> Fixes: 3ee47cad3e69 ("media: ov2680: Add Omnivision OV2680 sensor driver")
> Signed-off-by: Hans de Goede <hdegoede@redhat.com>
> ---
>   drivers/media/i2c/ov2680.c | 48 +++++++++-----------------------------
>   1 file changed, 11 insertions(+), 37 deletions(-)
>
> diff --git a/drivers/media/i2c/ov2680.c b/drivers/media/i2c/ov2680.c
> index 9a9c90c8a949..c1b23c5b7818 100644
> --- a/drivers/media/i2c/ov2680.c
> +++ b/drivers/media/i2c/ov2680.c
> @@ -328,11 +328,14 @@ static void ov2680_set_bayer_order(struct ov2680_dev *sensor)
>   	sensor->fmt.code = ov2680_hv_flip_bayer_order[hv_flip];
>   }
>   
> -static int ov2680_vflip_enable(struct ov2680_dev *sensor)
> +static int ov2680_set_vflip(struct ov2680_dev *sensor, s32 val)
>   {
>   	int ret;
>   
> -	ret = ov2680_mod_reg(sensor, OV2680_REG_FORMAT1, BIT(2), BIT(2));
> +	if (sensor->is_streaming)
> +		return -EBUSY;
> +
> +	ret = ov2680_mod_reg(sensor, OV2680_REG_FORMAT1, BIT(2), val ? BIT(2) : 0);
>   	if (ret < 0)
>   		return ret;
>   
> @@ -340,33 +343,14 @@ static int ov2680_vflip_enable(struct ov2680_dev *sensor)
>   	return 0;
>   }
>   
> -static int ov2680_vflip_disable(struct ov2680_dev *sensor)
> +static int ov2680_set_hflip(struct ov2680_dev *sensor, s32 val)
>   {
>   	int ret;
>   
> -	ret = ov2680_mod_reg(sensor, OV2680_REG_FORMAT1, BIT(2), BIT(0));
> -	if (ret < 0)
> -		return ret;
> +	if (sensor->is_streaming)
> +		return -EBUSY;
>   
> -	return ov2680_bayer_order(sensor);
> -}
> -
> -static int ov2680_hflip_enable(struct ov2680_dev *sensor)
> -{
> -	int ret;
> -
> -	ret = ov2680_mod_reg(sensor, OV2680_REG_FORMAT2, BIT(2), BIT(2));
> -	if (ret < 0)
> -		return ret;
> -
> -	return ov2680_bayer_order(sensor);
> -}
> -
> -static int ov2680_hflip_disable(struct ov2680_dev *sensor)
> -{
> -	int ret;
> -
> -	ret = ov2680_mod_reg(sensor, OV2680_REG_FORMAT2, BIT(2), BIT(0));
> +	ret = ov2680_mod_reg(sensor, OV2680_REG_FORMAT2, BIT(2), val ? BIT(2) : 0);
>   	if (ret < 0)
>   		return ret;
>   
> @@ -719,19 +703,9 @@ static int ov2680_s_ctrl(struct v4l2_ctrl *ctrl)
>   	case V4L2_CID_EXPOSURE:
>   		return ov2680_exposure_set(sensor, ctrl->val);
>   	case V4L2_CID_VFLIP:
> -		if (sensor->is_streaming)
> -			return -EBUSY;
> -		if (ctrl->val)
> -			return ov2680_vflip_enable(sensor);
> -		else
> -			return ov2680_vflip_disable(sensor);
> +		return ov2680_set_vflip(sensor, ctrl->val);
>   	case V4L2_CID_HFLIP:
> -		if (sensor->is_streaming)
> -			return -EBUSY;
> -		if (ctrl->val)
> -			return ov2680_hflip_enable(sensor);
> -		else
> -			return ov2680_hflip_disable(sensor);
> +		return ov2680_set_hflip(sensor, ctrl->val);
>   	case V4L2_CID_TEST_PATTERN:
>   		return ov2680_test_pattern_set(sensor, ctrl->val);
>   	default:

^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH 01/28] media: ov2680: Remove auto-gain and auto-exposure controls
  2023-06-12  6:53   ` Dan Scally
@ 2023-06-12  7:44     ` Hans de Goede
  2023-06-12  7:59       ` Dan Scally
  0 siblings, 1 reply; 57+ messages in thread
From: Hans de Goede @ 2023-06-12  7:44 UTC (permalink / raw)
  To: Dan Scally, Laurent Pinchart, Sakari Ailus, Andy Shevchenko
  Cc: Mauro Carvalho Chehab, Kate Hsuan, linux-media

Hi Dan,

On 6/12/23 08:53, Dan Scally wrote:
> Morning Hans - thanks for the set
> 
> On 07/06/2023 17:46, Hans de Goede wrote:
>> Quoting the OV2680 datasheet:
>>
>> "3.2 exposure and gain control
>>
>> In the OV2680, the exposure time and gain are set manually from an external
>> controller. The OV2680 supports manual gain and exposure control only for
>> normal applications, no auto mode."
>>
>> And indeed testing with the atomisp_ov2680 fork of ov2680.c has shown that
>> auto-exposure and auto-gain do not work.
>>
>> Note that the code setting the auto-exposure flag was broken, callers
>> of ov2680_exposure_set() were directly passing !!ctrls->auto_exp->val as
>> "bool auto_exp" value, but ctrls->auto_exp is a menu control with:
>>
>> enum  v4l2_exposure_auto_type {
>>          V4L2_EXPOSURE_AUTO = 0,
>>          V4L2_EXPOSURE_MANUAL = 1,
>>     ...
>>
>> So instead of passing !!ctrls->auto_exp->val they should have been passing
>> ctrls->auto_exp->val == V4L2_EXPOSURE_AUTO, iow the passed value was
>> inverted of what it should have been.
>>
>> Also remove ov2680_g_volatile_ctrl() since without auto support the gain
>> and exposure controls are not volatile.
>>
>> This also fixes the control values not being properly applied in
>> ov2680_mode_set(). The 800x600 mode register-list also sets gain,
>> exposure and vflip overriding the last set ctrl values.
>>
>> ov2680_mode_set() does call ov2680_gain_set() and ov2680_exposure_set()
>> but did this before writing the mode register-list, so these values
>> would still be overridden by the mode register-list.
>>
>> Add a v4l2_ctrl_handler_setup() call after writing the mode register-list
>> to restore all ctrl values. Also remove the ctrls->gain->is_new check from
>> ov2680_gain_set() so that the gain always gets restored properly.
>>
>> Last since ov2680_mode_set() now calls v4l2_ctrl_handler_setup(), remove
>> the v4l2_ctrl_handler_setup() call after ov2680_mode_restore() since
>> ov2680_mode_restore() calls ov2680_mode_set().
>>
>> Fixes: 3ee47cad3e69 ("media: ov2680: Add Omnivision OV2680 sensor driver")
>> Signed-off-by: Hans de Goede <hdegoede@redhat.com>
>> ---
>>   drivers/media/i2c/ov2680.c | 162 ++++---------------------------------
>>   1 file changed, 17 insertions(+), 145 deletions(-)
>>
>> diff --git a/drivers/media/i2c/ov2680.c b/drivers/media/i2c/ov2680.c
>> index 54153bf66bdd..02204e185e2f 100644
>> --- a/drivers/media/i2c/ov2680.c
>> +++ b/drivers/media/i2c/ov2680.c
>> @@ -85,15 +85,8 @@ struct ov2680_mode_info {
>>     struct ov2680_ctrls {
>>       struct v4l2_ctrl_handler handler;
>> -    struct {
>> -        struct v4l2_ctrl *auto_exp;
>> -        struct v4l2_ctrl *exposure;
>> -    };
>> -    struct {
>> -        struct v4l2_ctrl *auto_gain;
>> -        struct v4l2_ctrl *gain;
>> -    };
>> -
>> +    struct v4l2_ctrl *exposure;
>> +    struct v4l2_ctrl *gain;
>>       struct v4l2_ctrl *hflip;
>>       struct v4l2_ctrl *vflip;
>>       struct v4l2_ctrl *test_pattern;
>> @@ -143,6 +136,7 @@ static const struct reg_value ov2680_setting_30fps_QUXGA_800_600[] = {
>>       {0x380e, 0x02}, {0x380f, 0x84}, {0x3811, 0x04}, {0x3813, 0x04},
>>       {0x3814, 0x31}, {0x3815, 0x31}, {0x3820, 0xc0}, {0x4008, 0x00},
>>       {0x4009, 0x03}, {0x4837, 0x1e}, {0x3501, 0x4e}, {0x3502, 0xe0},
>> +    {0x3503, 0x03},
> 
> 
> I'm confused why this was added to this mode but not the other two; according to the datasheet it's setting the AGC/AEC into manual but I can't see why it's only needed here?

The original driver uses the 800x600 mode to also store "init_mode" registers,
no matter what mode is set, first all 800x600 registers are written and then
the other mode's registers are written over that.

This ugliness goes away later in the series when this gets renamed to
ov2680_global_setting[] and all the mode specific registers are dropped
because they are calculated based on crop + binning.

When switching to calculating the mode registers the register poke also gets some docs :)

        /* R MANUAL set exposure (0x01) and gain (0x02) to manual (hw does not do auto) */
        {0x3503, 0x03},

Regards,

Hans


^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH 01/28] media: ov2680: Remove auto-gain and auto-exposure controls
  2023-06-12  7:44     ` Hans de Goede
@ 2023-06-12  7:59       ` Dan Scally
  2023-06-12  9:57         ` Andy Shevchenko
  0 siblings, 1 reply; 57+ messages in thread
From: Dan Scally @ 2023-06-12  7:59 UTC (permalink / raw)
  To: Hans de Goede, Laurent Pinchart, Sakari Ailus, Andy Shevchenko
  Cc: Mauro Carvalho Chehab, Kate Hsuan, linux-media


On 12/06/2023 08:44, Hans de Goede wrote:
> Hi Dan,
>
> On 6/12/23 08:53, Dan Scally wrote:
>> Morning Hans - thanks for the set
>>
>> On 07/06/2023 17:46, Hans de Goede wrote:
>>> Quoting the OV2680 datasheet:
>>>
>>> "3.2 exposure and gain control
>>>
>>> In the OV2680, the exposure time and gain are set manually from an external
>>> controller. The OV2680 supports manual gain and exposure control only for
>>> normal applications, no auto mode."
>>>
>>> And indeed testing with the atomisp_ov2680 fork of ov2680.c has shown that
>>> auto-exposure and auto-gain do not work.
>>>
>>> Note that the code setting the auto-exposure flag was broken, callers
>>> of ov2680_exposure_set() were directly passing !!ctrls->auto_exp->val as
>>> "bool auto_exp" value, but ctrls->auto_exp is a menu control with:
>>>
>>> enum  v4l2_exposure_auto_type {
>>>           V4L2_EXPOSURE_AUTO = 0,
>>>           V4L2_EXPOSURE_MANUAL = 1,
>>>      ...
>>>
>>> So instead of passing !!ctrls->auto_exp->val they should have been passing
>>> ctrls->auto_exp->val == V4L2_EXPOSURE_AUTO, iow the passed value was
>>> inverted of what it should have been.
>>>
>>> Also remove ov2680_g_volatile_ctrl() since without auto support the gain
>>> and exposure controls are not volatile.
>>>
>>> This also fixes the control values not being properly applied in
>>> ov2680_mode_set(). The 800x600 mode register-list also sets gain,
>>> exposure and vflip overriding the last set ctrl values.
>>>
>>> ov2680_mode_set() does call ov2680_gain_set() and ov2680_exposure_set()
>>> but did this before writing the mode register-list, so these values
>>> would still be overridden by the mode register-list.
>>>
>>> Add a v4l2_ctrl_handler_setup() call after writing the mode register-list
>>> to restore all ctrl values. Also remove the ctrls->gain->is_new check from
>>> ov2680_gain_set() so that the gain always gets restored properly.
>>>
>>> Last since ov2680_mode_set() now calls v4l2_ctrl_handler_setup(), remove
>>> the v4l2_ctrl_handler_setup() call after ov2680_mode_restore() since
>>> ov2680_mode_restore() calls ov2680_mode_set().
>>>
>>> Fixes: 3ee47cad3e69 ("media: ov2680: Add Omnivision OV2680 sensor driver")
>>> Signed-off-by: Hans de Goede <hdegoede@redhat.com>
>>> ---
>>>    drivers/media/i2c/ov2680.c | 162 ++++---------------------------------
>>>    1 file changed, 17 insertions(+), 145 deletions(-)
>>>
>>> diff --git a/drivers/media/i2c/ov2680.c b/drivers/media/i2c/ov2680.c
>>> index 54153bf66bdd..02204e185e2f 100644
>>> --- a/drivers/media/i2c/ov2680.c
>>> +++ b/drivers/media/i2c/ov2680.c
>>> @@ -85,15 +85,8 @@ struct ov2680_mode_info {
>>>      struct ov2680_ctrls {
>>>        struct v4l2_ctrl_handler handler;
>>> -    struct {
>>> -        struct v4l2_ctrl *auto_exp;
>>> -        struct v4l2_ctrl *exposure;
>>> -    };
>>> -    struct {
>>> -        struct v4l2_ctrl *auto_gain;
>>> -        struct v4l2_ctrl *gain;
>>> -    };
>>> -
>>> +    struct v4l2_ctrl *exposure;
>>> +    struct v4l2_ctrl *gain;
>>>        struct v4l2_ctrl *hflip;
>>>        struct v4l2_ctrl *vflip;
>>>        struct v4l2_ctrl *test_pattern;
>>> @@ -143,6 +136,7 @@ static const struct reg_value ov2680_setting_30fps_QUXGA_800_600[] = {
>>>        {0x380e, 0x02}, {0x380f, 0x84}, {0x3811, 0x04}, {0x3813, 0x04},
>>>        {0x3814, 0x31}, {0x3815, 0x31}, {0x3820, 0xc0}, {0x4008, 0x00},
>>>        {0x4009, 0x03}, {0x4837, 0x1e}, {0x3501, 0x4e}, {0x3502, 0xe0},
>>> +    {0x3503, 0x03},
>>
>> I'm confused why this was added to this mode but not the other two; according to the datasheet it's setting the AGC/AEC into manual but I can't see why it's only needed here?
> The original driver uses the 800x600 mode to also store "init_mode" registers,
> no matter what mode is set, first all 800x600 registers are written and then
> the other mode's registers are written over that.


Ah yeah I see it now. Looks good to me then: Reviewed-by: Daniel Scally <dan.scally@ideasonboard.com>

>
> This ugliness goes away later in the series when this gets renamed to
> ov2680_global_setting[] and all the mode specific registers are dropped
> because they are calculated based on crop + binning.
>
> When switching to calculating the mode registers the register poke also gets some docs :)
>
>          /* R MANUAL set exposure (0x01) and gain (0x02) to manual (hw does not do auto) */
>          {0x3503, 0x03},


That sounds like it will be much better

>
> Regards,
>
> Hans
>

^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH 03/28] media: ov2680: Fix vflip / hflip set functions
  2023-06-12  7:32   ` Dan Scally
@ 2023-06-12  8:06     ` Hans de Goede
  2023-06-12  9:00       ` Laurent Pinchart
  0 siblings, 1 reply; 57+ messages in thread
From: Hans de Goede @ 2023-06-12  8:06 UTC (permalink / raw)
  To: Dan Scally, Laurent Pinchart, Sakari Ailus, Andy Shevchenko
  Cc: Mauro Carvalho Chehab, Kate Hsuan, linux-media

Hi,

On 6/12/23 09:32, Dan Scally wrote:
> Hello Hans (and everybody else)
> 
> On 07/06/2023 17:46, Hans de Goede wrote:
>> ov2680_vflip_disable() / ov2680_hflip_disable() pass BIT(0) instead of
>> 0 as value to ov2680_mod_reg().
>>
>> While fixing this also:
>>
>> 1. Stop having separate enable/disable functions for hflip / vflip
>> 2. Move the is_streaming check, which is unique to hflip / vflip
>>     into the ov2680_set_?flip() functions.
> 
> 
> I think the code in the patch is good, but I wonder about the right way to deal with the changed pixel ordering following a flip/mirror. None of the other Omnivision sensor drivers I've looked at disallow a flip/mirror whilst streaming, but of course doing so wrecks the colours. I don't think that there's any reason we couldn't make the equivalent calculation to ov2680_set_bayer_order() within libcamera though and configure the ipu3-imgu appropriately, and so properly support a flip-whilst-streaming...but quite possibly I'm missing something. So I'm leaning towards "let's just drop the is_streaming check", but I'm interested in what everyone else thinks.

So the story wrt flipping on the ov2680 is complicated. I have done quite a bit
of testing wrt the flipping with the atomisp-ov2680.c version of the code and
I have made the following observations:

1. By default the ISP window is set to auto which means the ISP will set it so that
the bayer order does NOT change when flipping. Except when trying to use the full
native sensor resolution because then the ISP has no "room" to adjust the window to
fixup the bayer order. So the whole ov2680_set_bayer_order() helper is only relevant
when either using manual ISP settings or when using the full native resolution.
Note the ISP here is the sensor's internal ISP (which has very little functionality
on the ov2680).

2. Independent of auto vs manual ISP window settings changing flipping during mid
stream does not work. Instead of the colors getting messed up the sensor simply just
stops streaming when changing the flipping mid stream.

Because of 2. I have decided to keep ov2680_set_bayer_order() and add support
for manually setting the ISP window so that the full native size can be used.

Having the full native size available is important for the atomisp ISP since
that expects the resolution outputted by the sensor to have some padding
(minimum 12 / 14 pixels depending on bayer order) vs the resolution being
output to userspace.

Manually setting the ISP window makes things actually work as the code
expected before doing this (the manually setting the ISP window is also done
in the commit adding the mode-dependent registers calculation).

2. Also means we need to keep the is_streaming checks. Changing flipping
while streaming simply does not work on this sensor (or at least I don't know
how to make it work).

As Sakari already mentioned it seems that the current mainline driver has
not seen much testing of things like the flip controls.

Regards,

Hans



> 
>> for a nice code cleanup.
>>
>> Fixes: 3ee47cad3e69 ("media: ov2680: Add Omnivision OV2680 sensor driver")
>> Signed-off-by: Hans de Goede <hdegoede@redhat.com>
>> ---
>>   drivers/media/i2c/ov2680.c | 48 +++++++++-----------------------------
>>   1 file changed, 11 insertions(+), 37 deletions(-)
>>
>> diff --git a/drivers/media/i2c/ov2680.c b/drivers/media/i2c/ov2680.c
>> index 9a9c90c8a949..c1b23c5b7818 100644
>> --- a/drivers/media/i2c/ov2680.c
>> +++ b/drivers/media/i2c/ov2680.c
>> @@ -328,11 +328,14 @@ static void ov2680_set_bayer_order(struct ov2680_dev *sensor)
>>       sensor->fmt.code = ov2680_hv_flip_bayer_order[hv_flip];
>>   }
>>   -static int ov2680_vflip_enable(struct ov2680_dev *sensor)
>> +static int ov2680_set_vflip(struct ov2680_dev *sensor, s32 val)
>>   {
>>       int ret;
>>   -    ret = ov2680_mod_reg(sensor, OV2680_REG_FORMAT1, BIT(2), BIT(2));
>> +    if (sensor->is_streaming)
>> +        return -EBUSY;
>> +
>> +    ret = ov2680_mod_reg(sensor, OV2680_REG_FORMAT1, BIT(2), val ? BIT(2) : 0);
>>       if (ret < 0)
>>           return ret;
>>   @@ -340,33 +343,14 @@ static int ov2680_vflip_enable(struct ov2680_dev *sensor)
>>       return 0;
>>   }
>>   -static int ov2680_vflip_disable(struct ov2680_dev *sensor)
>> +static int ov2680_set_hflip(struct ov2680_dev *sensor, s32 val)
>>   {
>>       int ret;
>>   -    ret = ov2680_mod_reg(sensor, OV2680_REG_FORMAT1, BIT(2), BIT(0));
>> -    if (ret < 0)
>> -        return ret;
>> +    if (sensor->is_streaming)
>> +        return -EBUSY;
>>   -    return ov2680_bayer_order(sensor);
>> -}
>> -
>> -static int ov2680_hflip_enable(struct ov2680_dev *sensor)
>> -{
>> -    int ret;
>> -
>> -    ret = ov2680_mod_reg(sensor, OV2680_REG_FORMAT2, BIT(2), BIT(2));
>> -    if (ret < 0)
>> -        return ret;
>> -
>> -    return ov2680_bayer_order(sensor);
>> -}
>> -
>> -static int ov2680_hflip_disable(struct ov2680_dev *sensor)
>> -{
>> -    int ret;
>> -
>> -    ret = ov2680_mod_reg(sensor, OV2680_REG_FORMAT2, BIT(2), BIT(0));
>> +    ret = ov2680_mod_reg(sensor, OV2680_REG_FORMAT2, BIT(2), val ? BIT(2) : 0);
>>       if (ret < 0)
>>           return ret;
>>   @@ -719,19 +703,9 @@ static int ov2680_s_ctrl(struct v4l2_ctrl *ctrl)
>>       case V4L2_CID_EXPOSURE:
>>           return ov2680_exposure_set(sensor, ctrl->val);
>>       case V4L2_CID_VFLIP:
>> -        if (sensor->is_streaming)
>> -            return -EBUSY;
>> -        if (ctrl->val)
>> -            return ov2680_vflip_enable(sensor);
>> -        else
>> -            return ov2680_vflip_disable(sensor);
>> +        return ov2680_set_vflip(sensor, ctrl->val);
>>       case V4L2_CID_HFLIP:
>> -        if (sensor->is_streaming)
>> -            return -EBUSY;
>> -        if (ctrl->val)
>> -            return ov2680_hflip_enable(sensor);
>> -        else
>> -            return ov2680_hflip_disable(sensor);
>> +        return ov2680_set_hflip(sensor, ctrl->val);
>>       case V4L2_CID_TEST_PATTERN:
>>           return ov2680_test_pattern_set(sensor, ctrl->val);
>>       default:
> 


^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH 04/28] media: ov2680: Use select VIDEO_V4L2_SUBDEV_API
  2023-06-07 16:46 ` [PATCH 04/28] media: ov2680: Use select VIDEO_V4L2_SUBDEV_API Hans de Goede
@ 2023-06-12  8:20   ` Dan Scally
  2023-06-12  8:57     ` Hans de Goede
  0 siblings, 1 reply; 57+ messages in thread
From: Dan Scally @ 2023-06-12  8:20 UTC (permalink / raw)
  To: Hans de Goede, Laurent Pinchart, Sakari Ailus, Andy Shevchenko
  Cc: Mauro Carvalho Chehab, Kate Hsuan, linux-media


On 07/06/2023 17:46, Hans de Goede wrote:
> Select VIDEO_V4L2_SUBDEV_API in Kconfig and drop the
> ifdef CONFIG_VIDEO_V4L2_SUBDEV_API checks, like other callers
> of v4l2_subdev_get_try_format() do.
>
> This is a preparation patch for fixing ov2680_set_fmt()
> which == V4L2_SUBDEV_FORMAT_TRY calls not properly filling in
> the passed in v4l2_mbus_framefmt struct.
>
> Fixes: 3ee47cad3e69 ("media: ov2680: Add Omnivision OV2680 sensor driver")
Not sure about Fixes on this one as I don't think it was necessarily broken before, but either way 
Reviewed-by: Daniel Scally <dan.scally@ideasonboard.com>
> Signed-off-by: Hans de Goede <hdegoede@redhat.com>
> ---
>   drivers/media/i2c/Kconfig  |  1 +
>   drivers/media/i2c/ov2680.c | 16 ++--------------
>   2 files changed, 3 insertions(+), 14 deletions(-)
>
> diff --git a/drivers/media/i2c/Kconfig b/drivers/media/i2c/Kconfig
> index 8f55155afe67..791473fcbad3 100644
> --- a/drivers/media/i2c/Kconfig
> +++ b/drivers/media/i2c/Kconfig
> @@ -433,6 +433,7 @@ config VIDEO_OV2680
>   	tristate "OmniVision OV2680 sensor support"
>   	depends on VIDEO_DEV && I2C
>   	select MEDIA_CONTROLLER
> +	select VIDEO_V4L2_SUBDEV_API
>   	select V4L2_FWNODE
>   	help
>   	  This is a Video4Linux2 sensor driver for the OmniVision
> diff --git a/drivers/media/i2c/ov2680.c b/drivers/media/i2c/ov2680.c
> index c1b23c5b7818..d90bbca6e913 100644
> --- a/drivers/media/i2c/ov2680.c
> +++ b/drivers/media/i2c/ov2680.c
> @@ -559,7 +559,6 @@ static int ov2680_get_fmt(struct v4l2_subdev *sd,
>   {
>   	struct ov2680_dev *sensor = to_ov2680_dev(sd);
>   	struct v4l2_mbus_framefmt *fmt = NULL;
> -	int ret = 0;
>   
>   	if (format->pad != 0)
>   		return -EINVAL;
> @@ -567,22 +566,17 @@ static int ov2680_get_fmt(struct v4l2_subdev *sd,
>   	mutex_lock(&sensor->lock);
>   
>   	if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
> -#ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
>   		fmt = v4l2_subdev_get_try_format(&sensor->sd, sd_state,
>   						 format->pad);
> -#else
> -		ret = -EINVAL;
> -#endif
>   	} else {
>   		fmt = &sensor->fmt;
>   	}
>   
> -	if (fmt)
> -		format->format = *fmt;
> +	format->format = *fmt;
>   
>   	mutex_unlock(&sensor->lock);
>   
> -	return ret;
> +	return 0;
>   }
>   
>   static int ov2680_set_fmt(struct v4l2_subdev *sd,
> @@ -591,9 +585,7 @@ static int ov2680_set_fmt(struct v4l2_subdev *sd,
>   {
>   	struct ov2680_dev *sensor = to_ov2680_dev(sd);
>   	struct v4l2_mbus_framefmt *fmt = &format->format;
> -#ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
>   	struct v4l2_mbus_framefmt *try_fmt;
> -#endif
>   	const struct ov2680_mode_info *mode;
>   	int ret = 0;
>   
> @@ -616,10 +608,8 @@ static int ov2680_set_fmt(struct v4l2_subdev *sd,
>   	}
>   
>   	if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
> -#ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
>   		try_fmt = v4l2_subdev_get_try_format(sd, sd_state, 0);
>   		format->format = *try_fmt;
> -#endif
>   		goto unlock;
>   	}
>   
> @@ -777,9 +767,7 @@ static int ov2680_v4l2_register(struct ov2680_dev *sensor)
>   	v4l2_i2c_subdev_init(&sensor->sd, sensor->i2c_client,
>   			     &ov2680_subdev_ops);
>   
> -#ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
>   	sensor->sd.flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
> -#endif
>   	sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
>   	sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
>   

^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH 04/28] media: ov2680: Use select VIDEO_V4L2_SUBDEV_API
  2023-06-12  8:20   ` Dan Scally
@ 2023-06-12  8:57     ` Hans de Goede
  0 siblings, 0 replies; 57+ messages in thread
From: Hans de Goede @ 2023-06-12  8:57 UTC (permalink / raw)
  To: Dan Scally, Laurent Pinchart, Sakari Ailus, Andy Shevchenko
  Cc: Mauro Carvalho Chehab, Kate Hsuan, linux-media

Hi,

On 6/12/23 10:20, Dan Scally wrote:
> 
> On 07/06/2023 17:46, Hans de Goede wrote:
>> Select VIDEO_V4L2_SUBDEV_API in Kconfig and drop the
>> ifdef CONFIG_VIDEO_V4L2_SUBDEV_API checks, like other callers
>> of v4l2_subdev_get_try_format() do.
>>
>> This is a preparation patch for fixing ov2680_set_fmt()
>> which == V4L2_SUBDEV_FORMAT_TRY calls not properly filling in
>> the passed in v4l2_mbus_framefmt struct.
>>
>> Fixes: 3ee47cad3e69 ("media: ov2680: Add Omnivision OV2680 sensor driver")
> Not sure about Fixes on this one as I don't think it was necessarily broken before, but either way 
Ah right, I should have added a note after a cut line that the Fixes tag is there because this is a pre-requisite for further fixes in the series.

> Reviewed-by: Daniel Scally <dan.scally@ideasonboard.com>

Thank you,

Regards,

Hans



>> Signed-off-by: Hans de Goede <hdegoede@redhat.com>
>> ---
>>   drivers/media/i2c/Kconfig  |  1 +
>>   drivers/media/i2c/ov2680.c | 16 ++--------------
>>   2 files changed, 3 insertions(+), 14 deletions(-)
>>
>> diff --git a/drivers/media/i2c/Kconfig b/drivers/media/i2c/Kconfig
>> index 8f55155afe67..791473fcbad3 100644
>> --- a/drivers/media/i2c/Kconfig
>> +++ b/drivers/media/i2c/Kconfig
>> @@ -433,6 +433,7 @@ config VIDEO_OV2680
>>       tristate "OmniVision OV2680 sensor support"
>>       depends on VIDEO_DEV && I2C
>>       select MEDIA_CONTROLLER
>> +    select VIDEO_V4L2_SUBDEV_API
>>       select V4L2_FWNODE
>>       help
>>         This is a Video4Linux2 sensor driver for the OmniVision
>> diff --git a/drivers/media/i2c/ov2680.c b/drivers/media/i2c/ov2680.c
>> index c1b23c5b7818..d90bbca6e913 100644
>> --- a/drivers/media/i2c/ov2680.c
>> +++ b/drivers/media/i2c/ov2680.c
>> @@ -559,7 +559,6 @@ static int ov2680_get_fmt(struct v4l2_subdev *sd,
>>   {
>>       struct ov2680_dev *sensor = to_ov2680_dev(sd);
>>       struct v4l2_mbus_framefmt *fmt = NULL;
>> -    int ret = 0;
>>         if (format->pad != 0)
>>           return -EINVAL;
>> @@ -567,22 +566,17 @@ static int ov2680_get_fmt(struct v4l2_subdev *sd,
>>       mutex_lock(&sensor->lock);
>>         if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
>> -#ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
>>           fmt = v4l2_subdev_get_try_format(&sensor->sd, sd_state,
>>                            format->pad);
>> -#else
>> -        ret = -EINVAL;
>> -#endif
>>       } else {
>>           fmt = &sensor->fmt;
>>       }
>>   -    if (fmt)
>> -        format->format = *fmt;
>> +    format->format = *fmt;
>>         mutex_unlock(&sensor->lock);
>>   -    return ret;
>> +    return 0;
>>   }
>>     static int ov2680_set_fmt(struct v4l2_subdev *sd,
>> @@ -591,9 +585,7 @@ static int ov2680_set_fmt(struct v4l2_subdev *sd,
>>   {
>>       struct ov2680_dev *sensor = to_ov2680_dev(sd);
>>       struct v4l2_mbus_framefmt *fmt = &format->format;
>> -#ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
>>       struct v4l2_mbus_framefmt *try_fmt;
>> -#endif
>>       const struct ov2680_mode_info *mode;
>>       int ret = 0;
>>   @@ -616,10 +608,8 @@ static int ov2680_set_fmt(struct v4l2_subdev *sd,
>>       }
>>         if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
>> -#ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
>>           try_fmt = v4l2_subdev_get_try_format(sd, sd_state, 0);
>>           format->format = *try_fmt;
>> -#endif
>>           goto unlock;
>>       }
>>   @@ -777,9 +767,7 @@ static int ov2680_v4l2_register(struct ov2680_dev *sensor)
>>       v4l2_i2c_subdev_init(&sensor->sd, sensor->i2c_client,
>>                    &ov2680_subdev_ops);
>>   -#ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
>>       sensor->sd.flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
>> -#endif
>>       sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
>>       sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
>>   
> 


^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH 03/28] media: ov2680: Fix vflip / hflip set functions
  2023-06-12  8:06     ` Hans de Goede
@ 2023-06-12  9:00       ` Laurent Pinchart
  0 siblings, 0 replies; 57+ messages in thread
From: Laurent Pinchart @ 2023-06-12  9:00 UTC (permalink / raw)
  To: Hans de Goede
  Cc: Dan Scally, Sakari Ailus, Andy Shevchenko, Mauro Carvalho Chehab,
	Kate Hsuan, linux-media

On Mon, Jun 12, 2023 at 10:06:09AM +0200, Hans de Goede wrote:
> On 6/12/23 09:32, Dan Scally wrote:
> > Hello Hans (and everybody else)
> > 
> > On 07/06/2023 17:46, Hans de Goede wrote:
> >> ov2680_vflip_disable() / ov2680_hflip_disable() pass BIT(0) instead of
> >> 0 as value to ov2680_mod_reg().
> >>
> >> While fixing this also:
> >>
> >> 1. Stop having separate enable/disable functions for hflip / vflip
> >> 2. Move the is_streaming check, which is unique to hflip / vflip
> >>     into the ov2680_set_?flip() functions.
> > 
> > I think the code in the patch is good, but I wonder about the right
> > way to deal with the changed pixel ordering following a flip/mirror.
> > None of the other Omnivision sensor drivers I've looked at disallow
> > a flip/mirror whilst streaming, but of course doing so wrecks the
> > colours. I don't think that there's any reason we couldn't make the
> > equivalent calculation to ov2680_set_bayer_order() within libcamera
> > though and configure the ipu3-imgu appropriately, and so properly
> > support a flip-whilst-streaming...but quite possibly I'm missing
> > something. So I'm leaning towards "let's just drop the is_streaming
> > check", but I'm interested in what everyone else thinks.
> 
> So the story wrt flipping on the ov2680 is complicated.

The story of flipping is complicated, period :-)

We made a mistake in V4L2 when adding support for Bayer formats. We
should have created RAW{8,10,12,...} media bus codes and pixel formats,
regardless of the CFA pattern, and conveyed the pattern information
out-of-band. This would have allowed changing the CFA pattern during
streaming.

As a result of this bad decision, various sensor drivers have decided to
move the crop rectangle by one pixel when flipping, in order to preserve
the CFA pattern. In some cases, this crop rectangle adjustement is even
performed by the camera sensor itself (and I've seen OV sensors where
this behaviour is configurable, you can disable the auto-adjustment
through a register). Other drivers change their reported CFA pattern
when flipping, and disable flipping changes during streaming.

TL;DR: It's a mess. It's probably not too late to fix that, and move
towards RAW media bus codes, but it won't be easy.

> I have done quite a bit
> of testing wrt the flipping with the atomisp-ov2680.c version of the code and
> I have made the following observations:
> 
> 1. By default the ISP window is set to auto which means the ISP will set it so that
> the bayer order does NOT change when flipping. Except when trying to use the full
> native sensor resolution because then the ISP has no "room" to adjust the window to
> fixup the bayer order. So the whole ov2680_set_bayer_order() helper is only relevant
> when either using manual ISP settings or when using the full native resolution.
> Note the ISP here is the sensor's internal ISP (which has very little functionality
> on the ov2680).
> 
> 2. Independent of auto vs manual ISP window settings changing flipping during mid
> stream does not work. Instead of the colors getting messed up the sensor simply just
> stops streaming when changing the flipping mid stream.
> 
> Because of 2. I have decided to keep ov2680_set_bayer_order() and add support
> for manually setting the ISP window so that the full native size can be used.
> 
> Having the full native size available is important for the atomisp ISP since
> that expects the resolution outputted by the sensor to have some padding
> (minimum 12 / 14 pixels depending on bayer order) vs the resolution being
> output to userspace.
> 
> Manually setting the ISP window makes things actually work as the code
> expected before doing this (the manually setting the ISP window is also done
> in the commit adding the mode-dependent registers calculation).
> 
> 2. Also means we need to keep the is_streaming checks. Changing flipping
> while streaming simply does not work on this sensor (or at least I don't know
> how to make it work).
> 
> As Sakari already mentioned it seems that the current mainline driver has
> not seen much testing of things like the flip controls.
> 
> >> for a nice code cleanup.
> >>
> >> Fixes: 3ee47cad3e69 ("media: ov2680: Add Omnivision OV2680 sensor driver")
> >> Signed-off-by: Hans de Goede <hdegoede@redhat.com>
> >> ---
> >>   drivers/media/i2c/ov2680.c | 48 +++++++++-----------------------------
> >>   1 file changed, 11 insertions(+), 37 deletions(-)
> >>
> >> diff --git a/drivers/media/i2c/ov2680.c b/drivers/media/i2c/ov2680.c
> >> index 9a9c90c8a949..c1b23c5b7818 100644
> >> --- a/drivers/media/i2c/ov2680.c
> >> +++ b/drivers/media/i2c/ov2680.c
> >> @@ -328,11 +328,14 @@ static void ov2680_set_bayer_order(struct ov2680_dev *sensor)
> >>       sensor->fmt.code = ov2680_hv_flip_bayer_order[hv_flip];
> >>   }
> >>   -static int ov2680_vflip_enable(struct ov2680_dev *sensor)
> >> +static int ov2680_set_vflip(struct ov2680_dev *sensor, s32 val)
> >>   {
> >>       int ret;
> >>   -    ret = ov2680_mod_reg(sensor, OV2680_REG_FORMAT1, BIT(2), BIT(2));
> >> +    if (sensor->is_streaming)
> >> +        return -EBUSY;
> >> +
> >> +    ret = ov2680_mod_reg(sensor, OV2680_REG_FORMAT1, BIT(2), val ? BIT(2) : 0);
> >>       if (ret < 0)
> >>           return ret;
> >>   @@ -340,33 +343,14 @@ static int ov2680_vflip_enable(struct ov2680_dev *sensor)
> >>       return 0;
> >>   }
> >>   -static int ov2680_vflip_disable(struct ov2680_dev *sensor)
> >> +static int ov2680_set_hflip(struct ov2680_dev *sensor, s32 val)
> >>   {
> >>       int ret;
> >>   -    ret = ov2680_mod_reg(sensor, OV2680_REG_FORMAT1, BIT(2), BIT(0));
> >> -    if (ret < 0)
> >> -        return ret;
> >> +    if (sensor->is_streaming)
> >> +        return -EBUSY;
> >>   -    return ov2680_bayer_order(sensor);
> >> -}
> >> -
> >> -static int ov2680_hflip_enable(struct ov2680_dev *sensor)
> >> -{
> >> -    int ret;
> >> -
> >> -    ret = ov2680_mod_reg(sensor, OV2680_REG_FORMAT2, BIT(2), BIT(2));
> >> -    if (ret < 0)
> >> -        return ret;
> >> -
> >> -    return ov2680_bayer_order(sensor);
> >> -}
> >> -
> >> -static int ov2680_hflip_disable(struct ov2680_dev *sensor)
> >> -{
> >> -    int ret;
> >> -
> >> -    ret = ov2680_mod_reg(sensor, OV2680_REG_FORMAT2, BIT(2), BIT(0));
> >> +    ret = ov2680_mod_reg(sensor, OV2680_REG_FORMAT2, BIT(2), val ? BIT(2) : 0);
> >>       if (ret < 0)
> >>           return ret;
> >>   @@ -719,19 +703,9 @@ static int ov2680_s_ctrl(struct v4l2_ctrl *ctrl)
> >>       case V4L2_CID_EXPOSURE:
> >>           return ov2680_exposure_set(sensor, ctrl->val);
> >>       case V4L2_CID_VFLIP:
> >> -        if (sensor->is_streaming)
> >> -            return -EBUSY;
> >> -        if (ctrl->val)
> >> -            return ov2680_vflip_enable(sensor);
> >> -        else
> >> -            return ov2680_vflip_disable(sensor);
> >> +        return ov2680_set_vflip(sensor, ctrl->val);
> >>       case V4L2_CID_HFLIP:
> >> -        if (sensor->is_streaming)
> >> -            return -EBUSY;
> >> -        if (ctrl->val)
> >> -            return ov2680_hflip_enable(sensor);
> >> -        else
> >> -            return ov2680_hflip_disable(sensor);
> >> +        return ov2680_set_hflip(sensor, ctrl->val);
> >>       case V4L2_CID_TEST_PATTERN:
> >>           return ov2680_test_pattern_set(sensor, ctrl->val);
> >>       default:

-- 
Regards,

Laurent Pinchart

^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH 01/28] media: ov2680: Remove auto-gain and auto-exposure controls
  2023-06-12  7:59       ` Dan Scally
@ 2023-06-12  9:57         ` Andy Shevchenko
  2023-06-12 11:30           ` Dan Scally
  0 siblings, 1 reply; 57+ messages in thread
From: Andy Shevchenko @ 2023-06-12  9:57 UTC (permalink / raw)
  To: Dan Scally
  Cc: Hans de Goede, Laurent Pinchart, Sakari Ailus, Andy Shevchenko,
	Mauro Carvalho Chehab, Kate Hsuan, linux-media

On Mon, Jun 12, 2023 at 10:59 AM Dan Scally <dan.scally@ideasonboard.com> wrote:
> On 12/06/2023 08:44, Hans de Goede wrote:

...

> Ah yeah I see it now. Looks good to me then: Reviewed-by: Daniel Scally <dan.scally@ideasonboard.com>

Do not forget to put tags on a separate line, so tools, such as b4,
may pick them up.




-- 
With Best Regards,
Andy Shevchenko

^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH 01/28] media: ov2680: Remove auto-gain and auto-exposure controls
  2023-06-12  9:57         ` Andy Shevchenko
@ 2023-06-12 11:30           ` Dan Scally
  0 siblings, 0 replies; 57+ messages in thread
From: Dan Scally @ 2023-06-12 11:30 UTC (permalink / raw)
  To: Andy Shevchenko
  Cc: Hans de Goede, Laurent Pinchart, Sakari Ailus, Andy Shevchenko,
	Mauro Carvalho Chehab, Kate Hsuan, linux-media


On 12/06/2023 10:57, Andy Shevchenko wrote:
> On Mon, Jun 12, 2023 at 10:59 AM Dan Scally <dan.scally@ideasonboard.com> wrote:
>> On 12/06/2023 08:44, Hans de Goede wrote:
> ...
>
>> Ah yeah I see it now. Looks good to me then: Reviewed-by: Daniel Scally <dan.scally@ideasonboard.com>
> Do not forget to put tags on a separate line, so tools, such as b4,
> may pick them up.


Ah - my bad, I'll do that in future.

>
>
>

^ permalink raw reply	[flat|nested] 57+ messages in thread

end of thread, other threads:[~2023-06-12 11:39 UTC | newest]

Thread overview: 57+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-06-07 16:46 [PATCH 00/28] media: ov2680: Bugfixes + ACPI + selection(crop-tgt) API support Hans de Goede
2023-06-07 16:46 ` [PATCH 01/28] media: ov2680: Remove auto-gain and auto-exposure controls Hans de Goede
2023-06-08 12:41   ` Sakari Ailus
2023-06-12  6:53   ` Dan Scally
2023-06-12  7:44     ` Hans de Goede
2023-06-12  7:59       ` Dan Scally
2023-06-12  9:57         ` Andy Shevchenko
2023-06-12 11:30           ` Dan Scally
2023-06-07 16:46 ` [PATCH 02/28] media: ov2680: Fix ov2680_bayer_order() Hans de Goede
2023-06-12  7:22   ` Dan Scally
2023-06-07 16:46 ` [PATCH 03/28] media: ov2680: Fix vflip / hflip set functions Hans de Goede
2023-06-12  7:32   ` Dan Scally
2023-06-12  8:06     ` Hans de Goede
2023-06-12  9:00       ` Laurent Pinchart
2023-06-07 16:46 ` [PATCH 04/28] media: ov2680: Use select VIDEO_V4L2_SUBDEV_API Hans de Goede
2023-06-12  8:20   ` Dan Scally
2023-06-12  8:57     ` Hans de Goede
2023-06-07 16:46 ` [PATCH 05/28] media: ov2680: Don't take the lock for try_fmt calls Hans de Goede
2023-06-08 12:44   ` Sakari Ailus
2023-06-08 12:48     ` Laurent Pinchart
2023-06-08 13:15       ` Sakari Ailus
2023-06-08 13:17       ` Hans de Goede
2023-06-07 16:46 ` [PATCH 06/28] media: ov2680: Add ov2680_fill_format() helper function Hans de Goede
2023-06-07 16:46 ` [PATCH 07/28] media: ov2680: Fix ov2680_set_fmt() which == V4L2_SUBDEV_FORMAT_TRY not working Hans de Goede
2023-06-07 16:46 ` [PATCH 08/28] media: Add MIPI CCI register access helper functions Hans de Goede
2023-06-07 20:22   ` Andy Shevchenko
2023-06-07 20:23     ` Andy Shevchenko
2023-06-07 16:46 ` [PATCH 09/28] media: ov2680: Convert to new CCI register access helpers Hans de Goede
2023-06-07 16:46 ` [PATCH 10/28] media: ov2680: Store dev instead of i2c_client in ov2680_dev Hans de Goede
2023-06-07 20:43   ` Andy Shevchenko
2023-06-07 16:46 ` [PATCH 11/28] media: ov2680: Check for "powerdown" GPIO con-id before checking for "reset" GPIO con-id Hans de Goede
2023-06-07 16:46 ` [PATCH 12/28] media: ov2680: Add runtime-pm support Hans de Goede
2023-06-07 16:46 ` [PATCH 13/28] media: ov2680: Drop is_enabled flag Hans de Goede
2023-06-07 16:46 ` [PATCH 14/28] media: ov2680: Add support for more clk setups Hans de Goede
2023-06-07 20:51   ` Andy Shevchenko
2023-06-07 16:46 ` [PATCH 15/28] media: ov2680: Add support for 19.2 MHz clock Hans de Goede
2023-06-07 20:53   ` Andy Shevchenko
2023-06-07 16:47 ` [PATCH 16/28] media: ov2680: Add endpoint matching support Hans de Goede
2023-06-07 16:47 ` [PATCH 17/28] media: ov2680: Add support for ACPI enumeration Hans de Goede
2023-06-07 20:57   ` Andy Shevchenko
2023-06-07 16:47 ` [PATCH 18/28] media: ov2680: Fix ov2680_enum_frame_interval() Hans de Goede
2023-06-07 16:47 ` [PATCH 19/28] media: ov2680: Annotate the per mode register setting lists Hans de Goede
2023-06-07 16:47 ` [PATCH 20/28] media: ov2680: Add ov2680_mode struct Hans de Goede
2023-06-07 16:47 ` [PATCH 21/28] media: ov2680: Make setting the mode algorithm based Hans de Goede
2023-06-07 16:47 ` [PATCH 22/28] media: ov2680: Add an __ov2680_get_pad_format() helper function Hans de Goede
2023-06-07 16:47 ` [PATCH 23/28] media: ov2680: Implement selection support Hans de Goede
2023-06-07 21:01   ` Andy Shevchenko
2023-06-07 21:04   ` Andy Shevchenko
2023-06-07 16:47 ` [PATCH 24/28] media: ov2680: Fix exposure and gain ctrls range and default value Hans de Goede
2023-06-07 16:47 ` [PATCH 25/28] media: ov2680: Add a bunch of register tweaks Hans de Goede
2023-06-07 16:47 ` [PATCH 26/28] media: ov2680: Add g_skip_frames op support Hans de Goede
2023-06-07 17:04   ` Laurent Pinchart
2023-06-07 17:49     ` Hans de Goede
2023-06-07 16:47 ` [PATCH 27/28] media: ov2680: Drop unnecessary pad checks Hans de Goede
2023-06-07 16:47 ` [PATCH 28/28] media: ov2680: Read and log sensor revision during probe Hans de Goede
2023-06-09  9:37 ` [PATCH 00/28] media: ov2680: Bugfixes + ACPI + selection(crop-tgt) API support Sakari Ailus
2023-06-09 14:28   ` Hans de Goede

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox