<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">From 1f00b84d993e1f8de17ef936e00f4264266cb5d1 Mon Sep 17 00:00:00 2001
From: Lad Prabhakar &lt;prabhakar.mahadev-lad.rj@bp.renesas.com&gt;
Date: Tue, 10 Mar 2020 14:17:08 +0100
Subject: [PATCH] media: i2c: imx219: Add support for RAW8 bit bayer
 format

Commit 22da1d56e982151e0bdfafe9de6fe94098a51356 upstream.

IMX219 sensor is capable for RAW8/RAW10 modes. This commit adds support
for RAW8 bayer format.

Signed-off-by: Lad Prabhakar &lt;prabhakar.mahadev-lad.rj@bp.renesas.com&gt;
Signed-off-by: Dave Stevenson &lt;dave.stevenson@raspberrypi.com&gt;
Signed-off-by: Sakari Ailus &lt;sakari.ailus@linux.intel.com&gt;
Signed-off-by: Mauro Carvalho Chehab &lt;mchehab+huawei@kernel.org&gt;
---
 drivers/media/i2c/imx219.c | 148 +++++++++++++++++++++++++++++--------
 1 file changed, 116 insertions(+), 32 deletions(-)

--- a/drivers/media/i2c/imx219.c
+++ b/drivers/media/i2c/imx219.c
@@ -168,15 +168,12 @@ static const struct imx219_reg mode_3280
 	{0x0171, 0x01},
 	{0x0174, 0x00},
 	{0x0175, 0x00},
-	{0x018c, 0x0a},
-	{0x018d, 0x0a},
 	{0x0301, 0x05},
 	{0x0303, 0x01},
 	{0x0304, 0x03},
 	{0x0305, 0x03},
 	{0x0306, 0x00},
 	{0x0307, 0x39},
-	{0x0309, 0x0a},
 	{0x030b, 0x01},
 	{0x030c, 0x00},
 	{0x030d, 0x72},
@@ -230,15 +227,12 @@ static const struct imx219_reg mode_1920
 	{0x0171, 0x01},
 	{0x0174, 0x00},
 	{0x0175, 0x00},
-	{0x018c, 0x0a},
-	{0x018d, 0x0a},
 	{0x0301, 0x05},
 	{0x0303, 0x01},
 	{0x0304, 0x03},
 	{0x0305, 0x03},
 	{0x0306, 0x00},
 	{0x0307, 0x39},
-	{0x0309, 0x0a},
 	{0x030b, 0x01},
 	{0x030c, 0x00},
 	{0x030d, 0x72},
@@ -290,15 +284,12 @@ static const struct imx219_reg mode_1640
 	{0x0171, 0x01},
 	{0x0174, 0x01},
 	{0x0175, 0x01},
-	{0x018c, 0x0a},
-	{0x018d, 0x0a},
 	{0x0301, 0x05},
 	{0x0303, 0x01},
 	{0x0304, 0x03},
 	{0x0305, 0x03},
 	{0x0306, 0x00},
 	{0x0307, 0x39},
-	{0x0309, 0x0a},
 	{0x030b, 0x01},
 	{0x030c, 0x00},
 	{0x030d, 0x72},
@@ -322,6 +313,18 @@ static const struct imx219_reg mode_1640
 	{0x0163, 0x78},
 };
 
+static const struct imx219_reg raw8_framefmt_regs[] = {
+	{0x018c, 0x08},
+	{0x018d, 0x08},
+	{0x0309, 0x08},
+};
+
+static const struct imx219_reg raw10_framefmt_regs[] = {
+	{0x018c, 0x0a},
+	{0x018d, 0x0a},
+	{0x0309, 0x0a},
+};
+
 static const char * const imx219_test_pattern_menu[] = {
 	"Disabled",
 	"Color Bars",
@@ -349,6 +352,27 @@ static const char * const imx219_supply_
 #define IMX219_NUM_SUPPLIES ARRAY_SIZE(imx219_supply_name)
 
 /*
+ * The supported formats.
+ * This table MUST contain 4 entries per format, to cover the various flip
+ * combinations in the order
+ * - no flip
+ * - h flip
+ * - v flip
+ * - h&amp;v flips
+ */
+static const u32 codes[] = {
+	MEDIA_BUS_FMT_SRGGB10_1X10,
+	MEDIA_BUS_FMT_SGRBG10_1X10,
+	MEDIA_BUS_FMT_SGBRG10_1X10,
+	MEDIA_BUS_FMT_SBGGR10_1X10,
+
+	MEDIA_BUS_FMT_SRGGB8_1X8,
+	MEDIA_BUS_FMT_SGRBG8_1X8,
+	MEDIA_BUS_FMT_SGBRG8_1X8,
+	MEDIA_BUS_FMT_SBGGR8_1X8,
+};
+
+/*
  * Initialisation delay between XCLR low-&gt;high and the moment when the sensor
  * can start capture (i.e. can leave software stanby) must be not less than:
  *   t4 + max(t5, t6 + &lt;time to initialize the sensor register over I2C&gt;)
@@ -413,6 +437,8 @@ struct imx219 {
 	struct v4l2_subdev sd;
 	struct media_pad pad;
 
+	struct v4l2_mbus_framefmt fmt;
+
 	struct clk *xclk; /* system clock to IMX219 */
 	u32 xclk_freq;
 
@@ -519,19 +545,40 @@ static int imx219_write_regs(struct imx2
 }
 
 /* Get bayer order based on flip setting. */
-static u32 imx219_get_format_code(struct imx219 *imx219)
+static u32 imx219_get_format_code(struct imx219 *imx219, u32 code)
 {
-	/*
-	 * Only one bayer order is supported.
-	 * It depends on the flip settings.
-	 */
-	static const u32 codes[2][2] = {
-		{ MEDIA_BUS_FMT_SRGGB10_1X10, MEDIA_BUS_FMT_SGRBG10_1X10, },
-		{ MEDIA_BUS_FMT_SGBRG10_1X10, MEDIA_BUS_FMT_SBGGR10_1X10, },
-	};
+	unsigned int i;
 
 	lockdep_assert_held(&amp;imx219-&gt;mutex);
-	return codes[imx219-&gt;vflip-&gt;val][imx219-&gt;hflip-&gt;val];
+
+	for (i = 0; i &lt; ARRAY_SIZE(codes); i++)
+		if (codes[i] == code)
+			break;
+
+	if (i &gt;= ARRAY_SIZE(codes))
+		i = 0;
+
+	i = (i &amp; ~3) | (imx219-&gt;vflip-&gt;val ? 2 : 0) |
+	    (imx219-&gt;hflip-&gt;val ? 1 : 0);
+
+	return codes[i];
+}
+
+static void imx219_set_default_format(struct imx219 *imx219)
+{
+	struct v4l2_mbus_framefmt *fmt;
+
+	fmt = &amp;imx219-&gt;fmt;
+	fmt-&gt;code = MEDIA_BUS_FMT_SRGGB10_1X10;
+	fmt-&gt;colorspace = V4L2_COLORSPACE_SRGB;
+	fmt-&gt;ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt-&gt;colorspace);
+	fmt-&gt;quantization = V4L2_MAP_QUANTIZATION_DEFAULT(true,
+							  fmt-&gt;colorspace,
+							  fmt-&gt;ycbcr_enc);
+	fmt-&gt;xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt-&gt;colorspace);
+	fmt-&gt;width = supported_modes[0].width;
+	fmt-&gt;height = supported_modes[0].height;
+	fmt-&gt;field = V4L2_FIELD_NONE;
 }
 
 static int imx219_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
@@ -545,7 +592,8 @@ static int imx219_open(struct v4l2_subde
 	/* Initialize try_fmt */
 	try_fmt-&gt;width = supported_modes[0].width;
 	try_fmt-&gt;height = supported_modes[0].height;
-	try_fmt-&gt;code = imx219_get_format_code(imx219);
+	try_fmt-&gt;code = imx219_get_format_code(imx219,
+					       MEDIA_BUS_FMT_SRGGB10_1X10);
 	try_fmt-&gt;field = V4L2_FIELD_NONE;
 
 	mutex_unlock(&amp;imx219-&gt;mutex);
@@ -648,14 +696,10 @@ static int imx219_enum_mbus_code(struct
 {
 	struct imx219 *imx219 = to_imx219(sd);
 
-	/*
-	 * Only one bayer order is supported (though it depends on the flip
-	 * settings)
-	 */
-	if (code-&gt;index &gt; 0)
+	if (code-&gt;index &gt;= (ARRAY_SIZE(codes) / 4))
 		return -EINVAL;
 
-	code-&gt;code = imx219_get_format_code(imx219);
+	code-&gt;code = imx219_get_format_code(imx219, codes[code-&gt;index * 4]);
 
 	return 0;
 }
@@ -669,7 +713,7 @@ static int imx219_enum_frame_size(struct
 	if (fse-&gt;index &gt;= ARRAY_SIZE(supported_modes))
 		return -EINVAL;
 
-	if (fse-&gt;code != imx219_get_format_code(imx219))
+	if (fse-&gt;code != imx219_get_format_code(imx219, imx219-&gt;fmt.code))
 		return -EINVAL;
 
 	fse-&gt;min_width = supported_modes[fse-&gt;index].width;
@@ -696,9 +740,7 @@ static void imx219_update_pad_format(str
 {
 	fmt-&gt;format.width = mode-&gt;width;
 	fmt-&gt;format.height = mode-&gt;height;
-	fmt-&gt;format.code = imx219_get_format_code(imx219);
 	fmt-&gt;format.field = V4L2_FIELD_NONE;
-
 	imx219_reset_colorspace(&amp;fmt-&gt;format);
 }
 
@@ -710,10 +752,12 @@ static int __imx219_get_pad_format(struc
 		struct v4l2_mbus_framefmt *try_fmt =
 			v4l2_subdev_get_try_format(&amp;imx219-&gt;sd, cfg, fmt-&gt;pad);
 		/* update the code which could change due to vflip or hflip: */
-		try_fmt-&gt;code = imx219_get_format_code(imx219);
+		try_fmt-&gt;code = imx219_get_format_code(imx219, try_fmt-&gt;code);
 		fmt-&gt;format = *try_fmt;
 	} else {
 		imx219_update_pad_format(imx219, imx219-&gt;mode, fmt);
+		fmt-&gt;format.code = imx219_get_format_code(imx219,
+							  imx219-&gt;fmt.code);
 	}
 
 	return 0;
@@ -741,11 +785,18 @@ static int imx219_set_pad_format(struct
 	const struct imx219_mode *mode;
 	struct v4l2_mbus_framefmt *framefmt;
 	int exposure_max, exposure_def, hblank;
+	unsigned int i;
 
 	mutex_lock(&amp;imx219-&gt;mutex);
 
+	for (i = 0; i &lt; ARRAY_SIZE(codes); i++)
+		if (codes[i] == fmt-&gt;format.code)
+			break;
+	if (i &gt;= ARRAY_SIZE(codes))
+		i = 0;
+
 	/* Bayer order varies with flips */
-	fmt-&gt;format.code = imx219_get_format_code(imx219);
+	fmt-&gt;format.code = imx219_get_format_code(imx219, codes[i]);
 
 	mode = v4l2_find_nearest_size(supported_modes,
 				      ARRAY_SIZE(supported_modes),
@@ -755,7 +806,9 @@ static int imx219_set_pad_format(struct
 	if (fmt-&gt;which == V4L2_SUBDEV_FORMAT_TRY) {
 		framefmt = v4l2_subdev_get_try_format(sd, cfg, fmt-&gt;pad);
 		*framefmt = fmt-&gt;format;
-	} else if (imx219-&gt;mode != mode) {
+	} else if (imx219-&gt;mode != mode ||
+		   imx219-&gt;fmt.code != fmt-&gt;format.code) {
+		imx219-&gt;fmt = fmt-&gt;format;
 		imx219-&gt;mode = mode;
 		/* Update limits and set FPS to default */
 		__v4l2_ctrl_modify_range(imx219-&gt;vblank, IMX219_VBLANK_MIN,
@@ -786,6 +839,27 @@ static int imx219_set_pad_format(struct
 	return 0;
 }
 
+static int imx219_set_framefmt(struct imx219 *imx219)
+{
+	switch (imx219-&gt;fmt.code) {
+	case MEDIA_BUS_FMT_SRGGB8_1X8:
+	case MEDIA_BUS_FMT_SGRBG8_1X8:
+	case MEDIA_BUS_FMT_SGBRG8_1X8:
+	case MEDIA_BUS_FMT_SBGGR8_1X8:
+		return imx219_write_regs(imx219, raw8_framefmt_regs,
+					ARRAY_SIZE(raw8_framefmt_regs));
+
+	case MEDIA_BUS_FMT_SRGGB10_1X10:
+	case MEDIA_BUS_FMT_SGRBG10_1X10:
+	case MEDIA_BUS_FMT_SGBRG10_1X10:
+	case MEDIA_BUS_FMT_SBGGR10_1X10:
+		return imx219_write_regs(imx219, raw10_framefmt_regs,
+					ARRAY_SIZE(raw10_framefmt_regs));
+	}
+
+	return -EINVAL;
+}
+
 static int imx219_start_streaming(struct imx219 *imx219)
 {
 	struct i2c_client *client = v4l2_get_subdevdata(&amp;imx219-&gt;sd);
@@ -800,6 +874,13 @@ static int imx219_start_streaming(struct
 		return ret;
 	}
 
+	ret = imx219_set_framefmt(imx219);
+	if (ret) {
+		dev_err(&amp;client-&gt;dev, "%s failed to set frame format: %d\n",
+			__func__, ret);
+		return ret;
+	}
+
 	/* Apply customized values from user */
 	ret =  __v4l2_ctrl_handler_setup(imx219-&gt;sd.ctrl_handler);
 	if (ret)
@@ -1253,6 +1334,9 @@ static int imx219_probe(struct i2c_clien
 	/* Initialize source pad */
 	imx219-&gt;pad.flags = MEDIA_PAD_FL_SOURCE;
 
+	/* Initialize default format */
+	imx219_set_default_format(imx219);
+
 	ret = media_entity_pads_init(&amp;imx219-&gt;sd.entity, 1, &amp;imx219-&gt;pad);
 	if (ret) {
 		dev_err(dev, "failed to init entity pads: %d\n", ret);
</pre></body></html>