Skip to contentSkip to conversation

media: Add MIPI CCI register access helper functions

PATCH In review Hans de Goede 5 participants 14 messages Jun 27, 2023 – Aug 15, 2023

Subsystems (7)
lore
Share & tools
lore
#0 lore raw

v4 cover media: Add MIPI CCI register access helper functions

Hi Laurent, Sakari, et al.,

Here is v4 of my MIPI CCI register access helper patches.

New in the CCI register access helpers in v4:
- Add V4L2_CCI_I2C Kconfig and wrap devm_cci_regmap_init_i2c() with if IS_ENABLED(CONFIG_V4L2_CCI_I2C)

New in the CCI register access helpers in v3:
- Use unsigned int instead of int in a couple of places
- Run + fix checkpatch.pl --strict --max-line-length=80
- Rename cci_regmap_init_i2c() to devm_cci_regmap_init_i2c()
- Some small kerneldoc text tweaks

Other changes in v3:
- Some small tweaks to some of the driver conversions, see individual patch changelogs

New in the CCI register access helpers in v2:
- Drop cci_reg_type enum
- Make having an encoded reg-width mandatory rather then using 0 to encode 8 bit width making reg-addresses without an encoded width default to a width of 8
- Add support for 64 bit wide registers
- Introduce a new cci_reg_sequence struct with 64 bit reg values for 64 bit support and without the delay_us field
- Various kerneldoc updates
- Stop supporting delays in cci_multi_reg_write()
- Some includes cleanups
- Disable regmap locking

Other changes in v2:
- Add a patch to convert the ov5693 driver (tested on Surface Go)
- Add a patch to convert the imx290 driver (untested)

Original (v1) cover-letter:

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

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

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

This is based on / the result of our previous discussion on this here:

Patches 2-4 are examples of how these helpers can be used and patch 5 removes the now no longer necessary ov_16bit_addr_reg_helpers.h which was the previous attempt to add common CCI access helpers.

Regards,

Hans

Hans de Goede (5):
media: Add MIPI CCI register access helper functions
media: ov5693: Convert to new CCI register access helpers
media: imx290: Convert to new CCI register access helpers
media: atomisp: ov2680: Convert to new CCI register access helpers
media: Remove ov_16bit_addr_reg_helpers.h

 Documentation/driver-api/media/v4l2-cci.rst   |   5 +
 Documentation/driver-api/media/v4l2-core.rst  |   1 +
 drivers/media/i2c/Kconfig                     |   2 +
 drivers/media/i2c/imx290.c                    | 360 +++++------
 drivers/media/i2c/ov5693.c                    | 585 +++++++-----------
 drivers/media/v4l2-core/Kconfig               |   9 +
 drivers/media/v4l2-core/Makefile              |   1 +
 drivers/media/v4l2-core/v4l2-cci.c            | 166 +++++
 drivers/staging/media/atomisp/i2c/Kconfig     |   1 +
 .../media/atomisp/i2c/atomisp-ov2680.c        | 251 ++++----
 drivers/staging/media/atomisp/i2c/ov2680.h    |  86 +--
 include/media/ov_16bit_addr_reg_helpers.h     |  92 ---
 include/media/v4l2-cci.h                      | 125 ++++
 13 files changed, 807 insertions(+), 877 deletions(-)
 create mode 100644 Documentation/driver-api/media/v4l2-cci.rst
 create mode 100644 drivers/media/v4l2-core/v4l2-cci.c
 delete mode 100644 include/media/ov_16bit_addr_reg_helpers.h
 create mode 100644 include/media/v4l2-cci.h
Signature · 1 line
2.41.0
#1 lore raw

PATCH v4 1/5 media: Add MIPI CCI register access helper functions

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 than 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.

---
Changes in v4:
- Add V4L2_CCI_I2C Kconfig and wrap devm_cci_regmap_init_i2c() with if IS_ENABLED(CONFIG_V4L2_CCI_I2C)

Changes in v3:
- Use unsigned int instead of int in a couple of places
- Run + fix checkpatch.pl --strict --max-line-length=80
- Rename cci_regmap_init_i2c() to devm_cci_regmap_init_i2c()
- Some small kerneldoc text tweaks

Changes in v2:
- Drop cci_reg_type enum
- Make having an encoded reg-width mandatory rather then using 0 to encode 8 bit width making reg-addresses without an encoded width default to a width of 8
- Add support for 64 bit wide registers
- Introduce a new cci_reg_sequence struct with 64 bit reg values for 64 bit support and without the delay_us field
- Various kerneldoc updates
- Stop supporting delays in cci_multi_reg_write()
- Some includes cleanups
- Disable regmap locking

---
 Documentation/driver-api/media/v4l2-cci.rst  |   5 +
 Documentation/driver-api/media/v4l2-core.rst |   1 +
 drivers/media/v4l2-core/Kconfig              |   9 +
 drivers/media/v4l2-core/Makefile             |   1 +
 drivers/media/v4l2-core/v4l2-cci.c           | 166 +++++++++++++++++++
 include/media/v4l2-cci.h                     | 125 ++++++++++++++
 6 files changed, 307 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
Documentation/driver-api/media/v4l2-cci.rst new file +5 -0
new file mode 100644
index 000000000000..dd297a40ed20
@@ -0,0 +1,5 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+V4L2 CCI kAPI
+^^^^^^^^^^^^^
+.. kernel-doc:: include/media/v4l2-cci.h
Documentation/driver-api/media/v4l2-core.rst +1 -0
index 1a8c4a5f256b..239045ecc8f4 100644
@@ -22,6 +22,7 @@ Video4Linux devices
     v4l2-mem2mem
     v4l2-async
     v4l2-fwnode
+    v4l2-cci
     v4l2-rect
     v4l2-tuner
     v4l2-common
drivers/media/v4l2-core/Kconfig +9 -0
index 348559bc2468..f77ebd688cde 100644
@@ -74,6 +74,15 @@ config V4L2_FWNODE
 config V4L2_ASYNC
 	tristate
 
+config V4L2_CCI
+	tristate
+
+config V4L2_CCI_I2C
+	tristate
+	depends on I2C
+	select REGMAP_I2C
+	select V4L2_CCI
+
 # Used by drivers that need Videobuf modules
 config VIDEOBUF_GEN
 	tristate
drivers/media/v4l2-core/Makefile +1 -0
index 41d91bd10cf2..be2551705755 100644
@@ -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
drivers/media/v4l2-core/v4l2-cci.c new file +166 -0

166 lines not on this page · raw message

include/media/v4l2-cci.h new file +125 -0

125 lines not on this page · raw message

Signature · 1 line
2.41.0
#2 lore raw

PATCH v4 2/5 media: ov5693: Convert to new CCI register access helpers

In reply to Hans de Goede

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

---
Note for reviewers all the OV5693_REG_?BIT defines in both the register address defines as well as in ov5693_global_regs[] were automatically changed using search replace.
---
Changes in v3:
- Dropped no longer necesary i2c_client member from struct ov5693_device
- Drop unnecessary ret = assignment for cci_update_bits() calls using the err pointer argument
- Fixed a couple of lines > 80 chars

Changes in v2:
- New patch in v2 of this series

---
 drivers/media/i2c/Kconfig  |   1 +
 drivers/media/i2c/ov5693.c | 585 +++++++++++++++----------------------
 2 files changed, 229 insertions(+), 357 deletions(-)
drivers/media/i2c/Kconfig +1 -0
index 9b0bb1d36019..8467f4ce92eb 100644
@@ -576,6 +576,7 @@ config VIDEO_OV5693
 	tristate "OmniVision OV5693 sensor support"
 	depends on I2C && VIDEO_DEV
 	select V4L2_FWNODE
+	select V4L2_CCI_I2C
 	help
 	  This is a Video4Linux2 sensor driver for the OmniVision
 	  OV5693 camera.
drivers/media/i2c/ov5693.c +228 -357

793 lines not on this page · raw message

Signature · 1 line
2.41.0
#3 lore raw

PATCH v4 3/5 media: imx290: Convert to new CCI register access helpers

In reply to Hans de Goede

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

---
Note:
1. This is untested
2. For reviewers: all the IMX290_REG_?BIT defines in both the register address defines as well as in various reg-sequences were automatically changed using search replace.
---
Changes in v3:
- Fixed a couple of lines > 80 chars

Changes in v2:
- New patch in v2 of this series

---
 drivers/media/i2c/Kconfig  |   1 +
 drivers/media/i2c/imx290.c | 360 +++++++++++++++----------------------
 2 files changed, 150 insertions(+), 211 deletions(-)
drivers/media/i2c/Kconfig +1 -0
index 8467f4ce92eb..6365c15bc4d4 100644
@@ -165,6 +165,7 @@ config VIDEO_IMX290
 	select VIDEO_V4L2_SUBDEV_API
 	select REGMAP_I2C
 	select V4L2_FWNODE
+	select V4L2_CCI_I2C
 	help
 	  This is a Video4Linux2 sensor driver for the Sony
 	  IMX290 camera sensor.
drivers/media/i2c/imx290.c +149 -211

566 lines not on this page · raw message

Signature · 1 line
2.41.0
#4 lore raw

Re: [PATCH v4 3/5] media: imx290: Convert to new CCI register access helpers

Hi Hans,

Quoted 21 lines · Hans de Goede

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

---
Note:
1. This is untested
2. For reviewers: all the IMX290_REG_?BIT defines in both the register address defines as well as in various reg-sequences were automatically changed using search replace.
---
Changes in v3:
- Fixed a couple of lines > 80 chars

Changes in v2:
- New patch in v2 of this series

---
 drivers/media/i2c/Kconfig  |   1 +
 drivers/media/i2c/imx290.c | 360 +++++++++++++++----------------------
 2 files changed, 150 insertions(+), 211 deletions(-)

[snip]

Quoted 5 lines
drivers/media/i2c/imx290.c +0 -0
index b3f832e9d7e1..e78c7b91ae72 100644
@@ -21,91 +21,86 @@

[snip]

Quoted 25 lines
drivers/media/i2c/imx290.c +0 -21
@@ -615,63 +605,15 @@ imx290_format_info(const struct imx290 *imx290, u32 code)
 	return NULL;
 }
 
-/* -----------------------------------------------------------------------------
- * Register access
- */
-
-static int __always_unused imx290_read(struct imx290 *imx290, u32 addr, u32 *value)
-{
-	u8 data[3] = { 0, 0, 0 };
-	int ret;
-
-	ret = regmap_raw_read(imx290->regmap, addr & IMX290_REG_ADDR_MASK,
-			      data, (addr >> IMX290_REG_SIZE_SHIFT) & 3);
-	if (ret < 0) {
-		dev_err(imx290->dev, "%u-bit read from 0x%04x failed: %d\n",
-			((addr >> IMX290_REG_SIZE_SHIFT) & 3) * 8,
-			 addr & IMX290_REG_ADDR_MASK, ret);
-		return ret;
-	}
-
-	*value = get_unaligned_le24(data);
-	return 0;
-}

drivers/media/i2c/imx290.c imx290_format_info(const struct imx290 *imx290, u32 code) · line 648

Quoted 10 lines
drivers/media/i2c/imx290.c +0 -10
-
-static int imx290_write(struct imx290 *imx290, u32 addr, u32 value, int *err)
-{
-	u8 data[3];
-	int ret;
-
-	if (err && *err)
-		return *err;
-
-	put_unaligned_le24(value, data);

We seem to be having a problem here, as the CCI helpers unconditionally use big endian for the data :-(

Quoted 14 lines
drivers/media/i2c/imx290.c +0 -14
-
-	ret = regmap_raw_write(imx290->regmap, addr & IMX290_REG_ADDR_MASK,
-			       data, (addr >> IMX290_REG_SIZE_SHIFT) & 3);
-	if (ret < 0) {
-		dev_err(imx290->dev, "%u-bit write to 0x%04x failed: %d\n",
-			((addr >> IMX290_REG_SIZE_SHIFT) & 3) * 8,
-			 addr & IMX290_REG_ADDR_MASK, ret);
-		if (err)
-			*err = ret;
-	}
-
-	return ret;
-}
-

[snip]

Signature · 3 lines
Regards,

Laurent Pinchart
maintainer
#5 lore raw

Re: [PATCH v4 3/5] media: imx290: Convert to new CCI register access helpers

Hi Laurent,

Quoted 65 lines · Laurent Pinchart

Hi Hans,

Quoted 21 lines · Hans de Goede

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

---
Note:
1. This is untested
2. For reviewers: all the IMX290_REG_?BIT defines in both the register address defines as well as in various reg-sequences were automatically changed using search replace.
---
Changes in v3:
- Fixed a couple of lines > 80 chars

Changes in v2:
- New patch in v2 of this series

---
 drivers/media/i2c/Kconfig  |   1 +
 drivers/media/i2c/imx290.c | 360 +++++++++++++++----------------------
 2 files changed, 150 insertions(+), 211 deletions(-)

[snip]

Quoted 5 lines
drivers/media/i2c/imx290.c +0 -0
index b3f832e9d7e1..e78c7b91ae72 100644
@@ -21,91 +21,86 @@

[snip]

Quoted 30 lines
drivers/media/i2c/imx290.c +0 -26
@@ -615,63 +605,15 @@ imx290_format_info(const struct imx290 *imx290, u32 code)
 	return NULL;
 }
 
-/* -----------------------------------------------------------------------------
- * Register access
- */
-
-static int __always_unused imx290_read(struct imx290 *imx290, u32 addr, u32 *value)
-{
-	u8 data[3] = { 0, 0, 0 };
-	int ret;
-
-	ret = regmap_raw_read(imx290->regmap, addr & IMX290_REG_ADDR_MASK,
-			      data, (addr >> IMX290_REG_SIZE_SHIFT) & 3);
-	if (ret < 0) {
-		dev_err(imx290->dev, "%u-bit read from 0x%04x failed: %d\n",
-			((addr >> IMX290_REG_SIZE_SHIFT) & 3) * 8,
-			 addr & IMX290_REG_ADDR_MASK, ret);
-		return ret;
-	}
-
-	*value = get_unaligned_le24(data);
-	return 0;
-}
-
-static int imx290_write(struct imx290 *imx290, u32 addr, u32 value, int *err)
-{
-	u8 data[3];
-	int ret;
Quoted 8 lines

drivers/media/i2c/imx290.c imx290_format_info(const struct imx290 *imx290, u32 code) · line 648

Quoted 5 lines
drivers/media/i2c/imx290.c +0 -5
-
-	if (err && *err)
-		return *err;
-
-	put_unaligned_le24(value, data);

We seem to be having a problem here, as the CCI helpers unconditionally use big endian for the data :-(

Well spotted. This driver needs to address this.

It's a hardware issue though. It's the only sensor ever I've seen to have little endian registers.

Quoted 18 lines
Quoted 15 lines
drivers/media/i2c/imx290.c +0 -14
-
-	ret = regmap_raw_write(imx290->regmap, addr & IMX290_REG_ADDR_MASK,
-			       data, (addr >> IMX290_REG_SIZE_SHIFT) & 3);
-	if (ret < 0) {
-		dev_err(imx290->dev, "%u-bit write to 0x%04x failed: %d\n",
-			((addr >> IMX290_REG_SIZE_SHIFT) & 3) * 8,
-			 addr & IMX290_REG_ADDR_MASK, ret);
-		if (err)
-			*err = ret;
-	}
-
-	return ret;
-}
-

[snip]

Signature · 3 lines
Regards,

Sakari Ailus
#6 lore raw

Re: [PATCH v4 3/5] media: imx290: Convert to new CCI register access helpers

Quoted 73 lines · Sakari Ailus
Quoted 63 lines · Laurent Pinchart
Quoted 21 lines · Hans de Goede

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

---
Note:
1. This is untested
2. For reviewers: all the IMX290_REG_?BIT defines in both the register address defines as well as in various reg-sequences were automatically changed using search replace.
---
Changes in v3:
- Fixed a couple of lines > 80 chars

Changes in v2:
- New patch in v2 of this series

---
 drivers/media/i2c/Kconfig  |   1 +
 drivers/media/i2c/imx290.c | 360 +++++++++++++++----------------------
 2 files changed, 150 insertions(+), 211 deletions(-)

[snip]

Quoted 5 lines
drivers/media/i2c/imx290.c +0 -0
index b3f832e9d7e1..e78c7b91ae72 100644
@@ -21,91 +21,86 @@

[snip]

Quoted 30 lines
drivers/media/i2c/imx290.c +0 -26
@@ -615,63 +605,15 @@ imx290_format_info(const struct imx290 *imx290, u32 code)
 	return NULL;
 }
 
-/* -----------------------------------------------------------------------------
- * Register access
- */
-
-static int __always_unused imx290_read(struct imx290 *imx290, u32 addr, u32 *value)
-{
-	u8 data[3] = { 0, 0, 0 };
-	int ret;
-
-	ret = regmap_raw_read(imx290->regmap, addr & IMX290_REG_ADDR_MASK,
-			      data, (addr >> IMX290_REG_SIZE_SHIFT) & 3);
-	if (ret < 0) {
-		dev_err(imx290->dev, "%u-bit read from 0x%04x failed: %d\n",
-			((addr >> IMX290_REG_SIZE_SHIFT) & 3) * 8,
-			 addr & IMX290_REG_ADDR_MASK, ret);
-		return ret;
-	}
-
-	*value = get_unaligned_le24(data);
-	return 0;
-}
-
-static int imx290_write(struct imx290 *imx290, u32 addr, u32 value, int *err)
-{
-	u8 data[3];
-	int ret;
Quoted 8 lines

drivers/media/i2c/imx290.c imx290_format_info(const struct imx290 *imx290, u32 code) · line 648

Quoted 5 lines
drivers/media/i2c/imx290.c +0 -5
-
-	if (err && *err)
-		return *err;
-
-	put_unaligned_le24(value, data);

We seem to be having a problem here, as the CCI helpers unconditionally use big endian for the data :-(

Quoted 4 lines

Well spotted. This driver needs to address this.

It's a hardware issue though. It's the only sensor ever I've seen to have little endian registers.

I'm not sure I would call that a hardware issue. It may be a peculiarity of this sensor, but is it really the only one ?

How would you like to see this addressed ? We could add CCI_REG*_LE macros, but I'm not sure to like that. Setting a flag at init time would be nicer, but there's nowhere to store it.

Quoted 16 lines
Quoted 16 lines
Quoted 14 lines
drivers/media/i2c/imx290.c +0 -14
-
-	ret = regmap_raw_write(imx290->regmap, addr & IMX290_REG_ADDR_MASK,
-			       data, (addr >> IMX290_REG_SIZE_SHIFT) & 3);
-	if (ret < 0) {
-		dev_err(imx290->dev, "%u-bit write to 0x%04x failed: %d\n",
-			((addr >> IMX290_REG_SIZE_SHIFT) & 3) * 8,
-			 addr & IMX290_REG_ADDR_MASK, ret);
-		if (err)
-			*err = ret;
-	}
-
-	return ret;
-}
-

[snip]

Signature · 3 lines
Regards,

Laurent Pinchart
#7 lore raw

Re: [PATCH v4 3/5] media: imx290: Convert to new CCI register access helpers

In reply to Sakari Ailus

Am Dienstag, 15. August 2023, 15:31:55 CEST schrieb Sakari Ailus:

Quoted 53 lines

********************
Achtung externe E-Mail: Öffnen Sie Anhänge und Links nur, wenn Sie wissen, dass diese aus einer sicheren Quelle stammen und sicher sind. Leiten Sie die E-Mail im Zweifelsfall zur Prüfung an den IT-Helpdesk weiter. Attention external email: Open attachments and links only if you know that they are from a secure source and are safe. In doubt forward the email to the IT-Helpdesk to check it. ********************

Hi Laurent,

Quoted 42 lines · Laurent Pinchart

Hi Hans,

Quoted 22 lines · Hans de Goede

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

---
Note:
1. This is untested
2. For reviewers: all the IMX290_REG_?BIT defines in both the register address defines as well as in various reg-sequences were automatically changed using search replace.
---
Changes in v3:
- Fixed a couple of lines > 80 chars

Changes in v2:
- New patch in v2 of this series
---

 drivers/media/i2c/Kconfig  |   1 +
 drivers/media/i2c/imx290.c | 360 +++++++++++++++----------------------
 2 files changed, 150 insertions(+), 211 deletions(-)

[snip]

Quoted 5 lines
drivers/media/i2c/imx290.c +0 -0
index b3f832e9d7e1..e78c7b91ae72 100644
@@ -21,91 +21,86 @@

[snip]

Quoted 6 lines
drivers/media/i2c/imx290.c +0 -0
@@ -615,63 +605,15 @@ imx290_format_info(const struct imx290 *imx290,

u32 code)> >
return NULL;

}

Quoted 12 lines
Quoted 4 lines
Quoted 4 lines

-/*
-----------------------------------------------------------------------
------ - * Register access
- */

Quoted 8 lines

drivers/media/i2c/imx290.c imx290_format_info(const struct imx290 *imx290, u32 code)

Quoted 8 lines
drivers/media/i2c/imx290.c +0 -2
-
-static int __always_unused imx290_read(struct imx290 *imx290, u32 addr,

u32 *value) -{

drivers/media/i2c/imx290.c +0 -5
-	u8 data[3] = { 0, 0, 0 };
-	int ret;
-
-	ret = regmap_raw_read(imx290->regmap, addr & IMX290_REG_ADDR_MASK,
-			      data, (addr >> IMX290_REG_SIZE_SHIFT) & 

3);

Quoted 2 lines
Quoted 2 lines

drivers/media/i2c/imx290.c

Quoted 2 lines
drivers/media/i2c/imx290.c +0 -2
-	if (ret < 0) {
-		dev_err(imx290->dev, "%u-bit read from 0x%04x failed: 

%d\n",

Quoted 22 lines
Quoted 13 lines
Quoted 13 lines
drivers/media/i2c/imx290.c +0 -10
-			((addr >> IMX290_REG_SIZE_SHIFT) & 3) * 8,
-			 addr & IMX290_REG_ADDR_MASK, ret);
-		return ret;
-	}
-
-	*value = get_unaligned_le24(data);
-	return 0;
-}
-
-static int imx290_write(struct imx290 *imx290, u32 addr, u32 value, int

*err) -{

drivers/media/i2c/imx290.c +0 -2
-	u8 data[3];
-	int ret;
Quoted 8 lines

drivers/media/i2c/imx290.c imx290_format_info(const struct imx290 *imx290, u32 code) · line 648

Quoted 5 lines
drivers/media/i2c/imx290.c +0 -5
-
-	if (err && *err)
-		return *err;
-
-	put_unaligned_le24(value, data);

We seem to be having a problem here, as the CCI helpers unconditionally use big endian for the data :-(

Quoted 4 lines

Well spotted. This driver needs to address this.

It's a hardware issue though. It's the only sensor ever I've seen to have little endian registers.

It's getting even worse: There are cameras using this sensor when bulk read is broken (VC MIPI IMX327 C, Laurent knows about this peculiar hardware), so regmap_config.use_single_read has to be set to true. This is currently not possible anymore insode this driver.

Best regards,
Alexander

Quoted 3 lines
Quoted 3 lines

drivers/media/i2c/imx290.c imx290_format_info(const struct imx290 *imx290, u32 code)

Quoted 3 lines
drivers/media/i2c/imx290.c +0 -3
-
-	ret = regmap_raw_write(imx290->regmap, addr & IMX290_REG_ADDR_MASK,
-			       data, (addr >> IMX290_REG_SIZE_SHIFT) & 

3);

Quoted 2 lines
Quoted 2 lines

drivers/media/i2c/imx290.c

Quoted 2 lines
drivers/media/i2c/imx290.c +0 -2
-	if (ret < 0) {
-		dev_err(imx290->dev, "%u-bit write to 0x%04x failed: 

%d\n",

Quoted 11 lines
Quoted 11 lines
Quoted 9 lines
drivers/media/i2c/imx290.c +0 -9
-			((addr >> IMX290_REG_SIZE_SHIFT) & 3) * 8,
-			 addr & IMX290_REG_ADDR_MASK, ret);
-		if (err)
-			*err = ret;
-	}
-
-	return ret;
-}
-

[snip]

Signature · 4 lines
TQ-Systems GmbH | Mühlstraße 2, Gut Delling | 82229 Seefeld, Germany
Amtsgericht München, HRB 105018
Geschäftsführer: Detlef Schneider, Rüdiger Stahl, Stefan Schneider
http://www.tq-group.com/
#8 lore raw

Re: [PATCH v4 3/5] media: imx290: Convert to new CCI register access helpers

Quoted 81 lines · Alexander Stein

Am Dienstag, 15. August 2023, 15:31:55 CEST schrieb Sakari Ailus:

Quoted 80 lines · Laurent Pinchart
Quoted 66 lines · Hans de Goede
Quoted 22 lines

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

---
Note:
1. This is untested
2. For reviewers: all the IMX290_REG_?BIT defines in both the register address defines as well as in various reg-sequences were automatically changed using search replace.
---
Changes in v3:
- Fixed a couple of lines > 80 chars

Changes in v2:
- New patch in v2 of this series
---

 drivers/media/i2c/Kconfig  |   1 +
 drivers/media/i2c/imx290.c | 360 +++++++++++++++----------------------
 2 files changed, 150 insertions(+), 211 deletions(-)

[snip]

Quoted 5 lines
drivers/media/i2c/imx290.c +0 -0
index b3f832e9d7e1..e78c7b91ae72 100644
@@ -21,91 +21,86 @@

[snip]

Quoted 32 lines
drivers/media/i2c/imx290.c +0 -26
@@ -615,63 +605,15 @@ imx290_format_info(const struct imx290 *imx290, u32 code)
 
 	return NULL;
 
 }
 
-/* -----------------------------------------------------------------------------
- * Register access
- */
-
-static int __always_unused imx290_read(struct imx290 *imx290, u32 addr, u32 *value)
-{
-	u8 data[3] = { 0, 0, 0 };
-	int ret;
-
-	ret = regmap_raw_read(imx290->regmap, addr & IMX290_REG_ADDR_MASK,
-			      data, (addr >> IMX290_REG_SIZE_SHIFT) & 3);
-	if (ret < 0) {
-		dev_err(imx290->dev, "%u-bit read from 0x%04x failed: %d\n",
-			((addr >> IMX290_REG_SIZE_SHIFT) & 3) * 8,
-			 addr & IMX290_REG_ADDR_MASK, ret);
-		return ret;
-	}
-
-	*value = get_unaligned_le24(data);
-	return 0;
-}
-
-static int imx290_write(struct imx290 *imx290, u32 addr, u32 value, int *err)
-{
-	u8 data[3];
-	int ret;
Quoted 8 lines

drivers/media/i2c/imx290.c imx290_format_info(const struct imx290 *imx290, u32 code) · line 648

Quoted 5 lines
drivers/media/i2c/imx290.c +0 -5
-
-	if (err && *err)
-		return *err;
-
-	put_unaligned_le24(value, data);

We seem to be having a problem here, as the CCI helpers unconditionally use big endian for the data :-(

Well spotted. This driver needs to address this.

It's a hardware issue though. It's the only sensor ever I've seen to have little endian registers.

The IMX296 and IMX297 seem to also have little-endian register values.

Quoted 4 lines

It's getting even worse: There are cameras using this sensor when bulk read is broken (VC MIPI IMX327 C, Laurent knows about this peculiar hardware), so regmap_config.use_single_read has to be set to true. This is currently not possible anymore insode this driver.

I think this could be fixed by passing a flag to the init function. Or, possibly better, the init function could read a DT property and set the flag automatically.

Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 14 lines
drivers/media/i2c/imx290.c +0 -14
-
-	ret = regmap_raw_write(imx290->regmap, addr & IMX290_REG_ADDR_MASK,
-			       data, (addr >> IMX290_REG_SIZE_SHIFT) & 3);
-	if (ret < 0) {
-		dev_err(imx290->dev, "%u-bit write to 0x%04x failed: %d\n",
-			((addr >> IMX290_REG_SIZE_SHIFT) & 3) * 8,
-			 addr & IMX290_REG_ADDR_MASK, ret);
-		if (err)
-			*err = ret;
-	}
-
-	return ret;
-}
-

[snip]

Signature · 3 lines
Regards,

Laurent Pinchart
#9 lore raw

Re: [PATCH v4 3/5] media: imx290: Convert to new CCI register access helpers

In reply to Laurent Pinchart

Hi,

Quoted 65 lines · Laurent Pinchart

Hi Hans,

Quoted 21 lines · Hans de Goede

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

---
Note:
1. This is untested
2. For reviewers: all the IMX290_REG_?BIT defines in both the register address defines as well as in various reg-sequences were automatically changed using search replace.
---
Changes in v3:
- Fixed a couple of lines > 80 chars

Changes in v2:
- New patch in v2 of this series

---
 drivers/media/i2c/Kconfig  |   1 +
 drivers/media/i2c/imx290.c | 360 +++++++++++++++----------------------
 2 files changed, 150 insertions(+), 211 deletions(-)

[snip]

Quoted 5 lines
drivers/media/i2c/imx290.c +0 -0
index b3f832e9d7e1..e78c7b91ae72 100644
@@ -21,91 +21,86 @@

[snip]

Quoted 30 lines
drivers/media/i2c/imx290.c +0 -26
@@ -615,63 +605,15 @@ imx290_format_info(const struct imx290 *imx290, u32 code)
 	return NULL;
 }
 
-/* -----------------------------------------------------------------------------
- * Register access
- */
-
-static int __always_unused imx290_read(struct imx290 *imx290, u32 addr, u32 *value)
-{
-	u8 data[3] = { 0, 0, 0 };
-	int ret;
-
-	ret = regmap_raw_read(imx290->regmap, addr & IMX290_REG_ADDR_MASK,
-			      data, (addr >> IMX290_REG_SIZE_SHIFT) & 3);
-	if (ret < 0) {
-		dev_err(imx290->dev, "%u-bit read from 0x%04x failed: %d\n",
-			((addr >> IMX290_REG_SIZE_SHIFT) & 3) * 8,
-			 addr & IMX290_REG_ADDR_MASK, ret);
-		return ret;
-	}
-
-	*value = get_unaligned_le24(data);
-	return 0;
-}
-
-static int imx290_write(struct imx290 *imx290, u32 addr, u32 value, int *err)
-{
-	u8 data[3];
-	int ret;
Quoted 8 lines

drivers/media/i2c/imx290.c imx290_format_info(const struct imx290 *imx290, u32 code) · line 648

Quoted 5 lines
drivers/media/i2c/imx290.c +0 -5
-
-	if (err && *err)
-		return *err;
-
-	put_unaligned_le24(value, data);

We seem to be having a problem here, as the CCI helpers unconditionally use big endian for the data :-(

That is because that is what the specification says, from the MIPI CSI spec:

"""
6.3.2 The Transmission Byte Order for Multi-byte Register Values

This is a normative section.

The first byte of a CCI message is always the MS byte of a multi-byte register and the last byte is always the LS byte. """

So it seems that the IMX sensors are special here and it might be best to just revert the conversion to the CCI helpers?

Alternative would be to make devm_cci_regmap_init_i2c() return a newly allocated struct
which contains both a struct regmap * and a long flags and make the helpers take a pointer to that struct, combined with adding an endianess flag to the flags member.

Regards,

Hans

Quoted 18 lines
Quoted 15 lines
drivers/media/i2c/imx290.c +0 -14
-
-	ret = regmap_raw_write(imx290->regmap, addr & IMX290_REG_ADDR_MASK,
-			       data, (addr >> IMX290_REG_SIZE_SHIFT) & 3);
-	if (ret < 0) {
-		dev_err(imx290->dev, "%u-bit write to 0x%04x failed: %d\n",
-			((addr >> IMX290_REG_SIZE_SHIFT) & 3) * 8,
-			 addr & IMX290_REG_ADDR_MASK, ret);
-		if (err)
-			*err = ret;
-	}
-
-	return ret;
-}
-

[snip]

#10 lore raw

Re: [PATCH v4 3/5] media: imx290: Convert to new CCI register access helpers

Hi Hans,

Quoted 78 lines · Hans de Goede
Quoted 63 lines · Laurent Pinchart
Quoted 21 lines · Hans de Goede

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

---
Note:
1. This is untested
2. For reviewers: all the IMX290_REG_?BIT defines in both the register address defines as well as in various reg-sequences were automatically changed using search replace.
---
Changes in v3:
- Fixed a couple of lines > 80 chars

Changes in v2:
- New patch in v2 of this series

---
 drivers/media/i2c/Kconfig  |   1 +
 drivers/media/i2c/imx290.c | 360 +++++++++++++++----------------------
 2 files changed, 150 insertions(+), 211 deletions(-)

[snip]

Quoted 5 lines
drivers/media/i2c/imx290.c +0 -0
index b3f832e9d7e1..e78c7b91ae72 100644
@@ -21,91 +21,86 @@

[snip]

Quoted 30 lines
drivers/media/i2c/imx290.c +0 -26
@@ -615,63 +605,15 @@ imx290_format_info(const struct imx290 *imx290, u32 code)
 	return NULL;
 }
 
-/* -----------------------------------------------------------------------------
- * Register access
- */
-
-static int __always_unused imx290_read(struct imx290 *imx290, u32 addr, u32 *value)
-{
-	u8 data[3] = { 0, 0, 0 };
-	int ret;
-
-	ret = regmap_raw_read(imx290->regmap, addr & IMX290_REG_ADDR_MASK,
-			      data, (addr >> IMX290_REG_SIZE_SHIFT) & 3);
-	if (ret < 0) {
-		dev_err(imx290->dev, "%u-bit read from 0x%04x failed: %d\n",
-			((addr >> IMX290_REG_SIZE_SHIFT) & 3) * 8,
-			 addr & IMX290_REG_ADDR_MASK, ret);
-		return ret;
-	}
-
-	*value = get_unaligned_le24(data);
-	return 0;
-}
-
-static int imx290_write(struct imx290 *imx290, u32 addr, u32 value, int *err)
-{
-	u8 data[3];
-	int ret;
Quoted 8 lines

drivers/media/i2c/imx290.c imx290_format_info(const struct imx290 *imx290, u32 code) · line 648

Quoted 5 lines
drivers/media/i2c/imx290.c +0 -5
-
-	if (err && *err)
-		return *err;
-
-	put_unaligned_le24(value, data);

We seem to be having a problem here, as the CCI helpers unconditionally use big endian for the data :-(

That is because that is what the specification says, from the MIPI CSI spec:

"""
6.3.2 The Transmission Byte Order for Multi-byte Register Values

Quoted 8 lines

This is a normative section.

The first byte of a CCI message is always the MS byte of a multi-byte register and the last byte is always the LS byte.
"""

So it seems that the IMX sensors are special here and it might be best to just revert the conversion to the CCI helpers?

I'm fine with reverting for v6.6, as we're close to the release of v6.5.

Quoted 4 lines

Alternative would be to make devm_cci_regmap_init_i2c() return a newly allocated struct which contains both a struct regmap * and a long flags and make the helpers take a pointer to that struct, combined with adding an endianess flag to the flags member.

I think I like the idea, it will probably help adding support for various device-specific quirks in the future.

Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
drivers/media/i2c/imx290.c +0 -14
-
-	ret = regmap_raw_write(imx290->regmap, addr & IMX290_REG_ADDR_MASK,
-			       data, (addr >> IMX290_REG_SIZE_SHIFT) & 3);
-	if (ret < 0) {
-		dev_err(imx290->dev, "%u-bit write to 0x%04x failed: %d\n",
-			((addr >> IMX290_REG_SIZE_SHIFT) & 3) * 8,
-			 addr & IMX290_REG_ADDR_MASK, ret);
-		if (err)
-			*err = ret;
-	}
-
-	return ret;
-}
-
Signature · 3 lines
Regards,

Laurent Pinchart
#11 lore raw

PATCH v4 4/5 media: atomisp: ov2680: Convert to new CCI register access helpers

In reply to Hans de Goede

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

While at it also switch to using the same register address defines as the standard drivers/media/i2c/ov2680.c driver to make merging the 2 drivers simpler.

---
Note atomisp-ov2680.c is going away. The main atomisp driver now supports standard v4l2 drivers and a patch series is pending for the main drivers/media/i2c/ov2680.c to get it into shape to replace atomisp-ov2680.c .

The only reason to upstream this patch is because much of the work landing in the main ov2680.c is copy -pasted from the state of atomisp-ov2680.c *after this patch* , so having this in git history before deleting atomisp-ov2680.c is helpful in case someone ever finds the need to compare the code.

---
 drivers/staging/media/atomisp/i2c/Kconfig     |   1 +
 .../media/atomisp/i2c/atomisp-ov2680.c        | 251 ++++++++----------
 drivers/staging/media/atomisp/i2c/ov2680.h    |  86 +-----
 3 files changed, 121 insertions(+), 217 deletions(-)
drivers/staging/media/atomisp/i2c/Kconfig +1 -0
index 16b6b808d4a7..5aa9d367cebb 100644
@@ -53,6 +53,7 @@ config VIDEO_ATOMISP_OV2680
 	tristate "Omnivision OV2680 sensor support"
 	depends on ACPI
 	depends on I2C && VIDEO_DEV
+	select V4L2_CCI_I2C
 	help
 	  This is a Video4Linux2 sensor-level driver for the Omnivision
 	  OV2680 raw camera.
drivers/staging/media/atomisp/i2c/atomisp-ov2680.c +115 -136

378 lines not on this page · raw message

drivers/staging/media/atomisp/i2c/ov2680.h +5 -81

127 lines not on this page · raw message

Signature · 1 line
2.41.0
#12 lore raw

PATCH v4 5/5 media: Remove ov_16bit_addr_reg_helpers.h

In reply to Hans de Goede

The helpers in this header are not used anywhere anymore, they have been superseded by the new CCI register access helpers.

---
 include/media/ov_16bit_addr_reg_helpers.h | 92 -----------------------
 1 file changed, 92 deletions(-)
 delete mode 100644 include/media/ov_16bit_addr_reg_helpers.h
include/media/ov_16bit_addr_reg_helpers.h deleted +0 -92
deleted file mode 100644
index 1c60a50bd795..000000000000
@@ -1,92 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0 */
-/*
- * I2C register access helpers for Omnivision OVxxxx image sensors which expect
- * a 16 bit register address in big-endian format and which have 1-3 byte
- * wide registers, in big-endian format (for the higher width registers).
- *
- * Based on the register helpers from drivers/media/i2c/ov2680.c which is:
- * Copyright (C) 2018 Linaro Ltd
- */
-#ifndef __OV_16BIT_ADDR_REG_HELPERS_H
-#define __OV_16BIT_ADDR_REG_HELPERS_H
-
-#include <asm/unaligned.h>
-#include <linux/dev_printk.h>
-#include <linux/i2c.h>
-
-static inline int ov_read_reg(struct i2c_client *client, u16 reg,
-				  unsigned int len, u32 *val)
-{
-	u8 addr_buf[2], data_buf[4] = { };
-	struct i2c_msg msgs[2];
-	int ret;
-
-	if (len > 4)
-		return -EINVAL;
-
-	put_unaligned_be16(reg, addr_buf);
-
-	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 ov_read_reg8(s, r, v)	ov_read_reg(s, r, 1, v)
-#define ov_read_reg16(s, r, v)	ov_read_reg(s, r, 2, v)
-#define ov_read_reg24(s, r, v)	ov_read_reg(s, r, 3, v)
-
-static inline int ov_write_reg(struct i2c_client *client, u16 reg,
-				   unsigned int len, u32 val)
-{
-	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 ov_write_reg8(s, r, v)	ov_write_reg(s, r, 1, v)
-#define ov_write_reg16(s, r, v)	ov_write_reg(s, r, 2, v)
-#define ov_write_reg24(s, r, v)	ov_write_reg(s, r, 3, v)
-
-static inline int ov_update_reg(struct i2c_client *client, u16 reg, u8 mask, u8 val)
-{
-	u32 readval;
-	int ret;
-
-	ret = ov_read_reg8(client, reg, &readval);
-	if (ret < 0)
-		return ret;
-
-	val = (readval & ~mask) | (val & mask);
-
-	return ov_write_reg8(client, reg, val);
-}
-
-#endif
Signature · 1 line
2.41.0
#13 lore raw
In reply to Hans de Goede

Hi Hans, Laurent,

Quoted 7 lines · Hans de Goede

Hi Laurent, Sakari, et al.,

Here is v4 of my MIPI CCI register access helper patches.

New in the CCI register access helpers in v4:
- Add V4L2_CCI_I2C Kconfig and wrap devm_cci_regmap_init_i2c() with if IS_ENABLED(CONFIG_V4L2_CCI_I2C)

Laurent: are you fine with this version? I see you've acked all but the first patch.

I'll pick this to my tree then.

Signature · 3 lines
Kind regards,

Sakari Ailus