[PATCH v11 12/14] media: atomisp: Allow raw Bayer capture

From: Maurizio Casciano

Date: Sat Nov 21 2026 - 08:33:35 EST


AtomISP currently rejects raw formats and substitutes YUV420. Expose
the supported raw Bayer formats so userspace can select unprocessed
sensor frames with VIDIOC_S_FMT.

Enumerate the sensor's media-bus codes and pass the requested RAW code
to discrete frame-size enumeration. Crop-capable sensors report their
native RAW sizes without conversion padding. Match TRY_FMT to S_FMT
when sensor negotiation falls back to native processed output.

RAW formats expose the sensor transport frame for the ISP2401 copy
pipeline. Processed formats keep the existing padding rules.

Sashiko identified the negotiation and enumeration cases. Codex drafted
the follow-up and exercised it with simulated sensor replies.

Link: https://linuxtv.org/mailman3/hyperkitty/list/media-ci@xxxxxxxxxxx/message/TJOXYBYPVBPWJS47TNZF33YCWBAP2WZL/
Signed-off-by: Maurizio Casciano <mauriziocasciano7@xxxxxxxxx>
---
.../staging/media/atomisp/pci/atomisp_cmd.c | 36 ++++--
.../staging/media/atomisp/pci/atomisp_ioctl.c | 106 +++++++++++++-----
2 files changed, 106 insertions(+), 36 deletions(-)

diff --git a/drivers/staging/media/atomisp/pci/atomisp_cmd.c b/drivers/staging/media/atomisp/pci/atomisp_cmd.c
index c2978e587..bada0eec6 100644
--- a/drivers/staging/media/atomisp/pci/atomisp_cmd.c
+++ b/drivers/staging/media/atomisp/pci/atomisp_cmd.c
@@ -3818,11 +3818,11 @@ int atomisp_try_fmt(struct atomisp_device *isp, struct v4l2_pix_format *f,
struct atomisp_sub_device *asd = &isp->asd;
struct v4l2_mbus_framefmt ffmt = { };
struct v4l2_area padding;
+ bool retried = false;
int ret;

fmt = atomisp_get_format_bridge(f->pixelformat);
- /* Currently, raw formats are broken!!! */
- if (!fmt || fmt->sh_fmt == IA_CSS_FRAME_FORMAT_RAW) {
+ if (!fmt) {
f->pixelformat = V4L2_PIX_FMT_YUV420;

fmt = atomisp_get_format_bridge(f->pixelformat);
@@ -3840,23 +3840,24 @@ int atomisp_try_fmt(struct atomisp_device *isp, struct v4l2_pix_format *f,
}

/*
- * atomisp_set_fmt() will set the sensor resolution to the requested
- * resolution + padding. Add padding here and remove it again after
- * the set_fmt call, like atomisp_set_fmt_to_snr() does.
+ * Match S_FMT: only Bayer-to-processed conversion needs padding.
+ * Start with the requested format and retry without padding if the
+ * sensor supplies a native processed format.
*/
- atomisp_get_pix_padding(isp, f, &padding);
+ if (fmt->sh_fmt == IA_CSS_FRAME_FORMAT_RAW)
+ padding = (struct v4l2_area) { };
+ else
+ atomisp_get_pix_padding(isp, f, &padding);
+
+retry:
v4l2_fill_mbus_format(&ffmt, f, fmt->mbus_code);
ffmt.width += padding.width;
ffmt.height += padding.height;

- dev_dbg(isp->dev, "try_mbus_fmt: try %ux%u\n", ffmt.width, ffmt.height);
-
ret = atomisp_set_sensor_crop_and_fmt(isp, &ffmt, V4L2_SUBDEV_FORMAT_TRY);
if (ret)
return ret;

- dev_dbg(isp->dev, "try_mbus_fmt: got %ux%u\n", ffmt.width, ffmt.height);
-
snr_fmt = atomisp_get_format_bridge_from_mbus(ffmt.code);
if (!snr_fmt) {
dev_err(isp->dev, "unknown sensor format 0x%8.8x\n",
@@ -3864,6 +3865,21 @@ int atomisp_try_fmt(struct atomisp_device *isp, struct v4l2_pix_format *f,
return -EINVAL;
}

+ if (fmt->sh_fmt == IA_CSS_FRAME_FORMAT_RAW) {
+ fmt = snr_fmt;
+ f->pixelformat = fmt->pixelformat;
+ } else if (!atomisp_is_mbuscode_raw(ffmt.code) &&
+ (padding.width || padding.height)) {
+ padding = (struct v4l2_area) { };
+ retried = true;
+ goto retry;
+ } else if (retried && atomisp_is_mbuscode_raw(ffmt.code)) {
+ return -EINVAL;
+ }
+
+ if (ffmt.width < padding.width || ffmt.height < padding.height)
+ return -EINVAL;
+
f->width = ffmt.width - padding.width;
f->height = ffmt.height - padding.height;

diff --git a/drivers/staging/media/atomisp/pci/atomisp_ioctl.c b/drivers/staging/media/atomisp/pci/atomisp_ioctl.c
index 87c29a940..948efa320 100644
--- a/drivers/staging/media/atomisp/pci/atomisp_ioctl.c
+++ b/drivers/staging/media/atomisp/pci/atomisp_ioctl.c
@@ -7,6 +7,7 @@
* Copyright (c) 2010 Silicon Hive www.siliconhive.com.
*/

+#include <linux/bitmap.h>
#include <linux/delay.h>
#include <linux/pci.h>

@@ -447,6 +448,36 @@ static int atomisp_s_input(struct file *file, void *fh, unsigned int input)
return ret;
}

+static int atomisp_enum_raw_formats(struct v4l2_subdev *sensor,
+ unsigned long *raw_formats)
+{
+ struct v4l2_subdev_mbus_code_enum code = {
+ .which = V4L2_SUBDEV_FORMAT_ACTIVE,
+ };
+ struct v4l2_subdev_state *state;
+ unsigned int i;
+ int ret;
+
+ state = v4l2_subdev_lock_and_get_active_state(sensor);
+ for (;; code.index++) {
+ ret = v4l2_subdev_call(sensor, pad, enum_mbus_code, state, &code);
+ if (ret)
+ break;
+
+ for (i = 0; i < ARRAY_SIZE(atomisp_output_fmts); i++) {
+ const struct atomisp_format_bridge *fmt = &atomisp_output_fmts[i];
+
+ if (fmt->sh_fmt == IA_CSS_FRAME_FORMAT_RAW &&
+ fmt->mbus_code == code.code)
+ set_bit(i, raw_formats);
+ }
+ }
+ if (state)
+ v4l2_subdev_unlock_state(state);
+
+ return ret == -EINVAL || ret == -ENOIOCTLCMD ? 0 : ret;
+}
+
/*
* With crop any framesize <= sensor-size can be made, give
* userspace a list of sizes to choice from.
@@ -455,7 +486,7 @@ static int atomisp_enum_framesizes_crop_inner(struct atomisp_device *isp,
struct v4l2_frmsizeenum *fsize,
const struct v4l2_rect *active,
const struct v4l2_rect *native,
- int *valid_sizes)
+ bool raw, int *valid_sizes)
{
static const struct v4l2_area frame_sizes[] = {
{ 1920, 1440 },
@@ -472,10 +503,22 @@ static int atomisp_enum_framesizes_crop_inner(struct atomisp_device *isp,
};
int i;

+ if (raw && (*valid_sizes)++ == fsize->index) {
+ fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
+ fsize->discrete.width = native->width;
+ fsize->discrete.height = native->height;
+ return 0;
+ }
+
for (i = 0; i < ARRAY_SIZE(frame_sizes); i++) {
- struct v4l2_area padding;
+ struct v4l2_area padding = { };
+
+ if (raw && frame_sizes[i].width == native->width &&
+ frame_sizes[i].height == native->height)
+ continue;

- atomisp_get_padding(isp, frame_sizes[i], &padding);
+ if (!raw)
+ atomisp_get_padding(isp, frame_sizes[i], &padding);

if ((frame_sizes[i].width + padding.width) > native->width ||
(frame_sizes[i].height + padding.height) > native->height)
@@ -503,14 +546,14 @@ static int atomisp_enum_framesizes_crop_inner(struct atomisp_device *isp,
}

static int atomisp_enum_framesizes_crop(struct atomisp_device *isp,
- struct v4l2_frmsizeenum *fsize)
+ struct v4l2_frmsizeenum *fsize, bool raw)
{
struct atomisp_input_subdev *input = &isp->inputs[isp->asd.input_curr];
struct v4l2_rect active = input->active_rect;
struct v4l2_rect native = input->native_rect;
int ret, valid_sizes = 0;

- ret = atomisp_enum_framesizes_crop_inner(isp, fsize, &active, &native, &valid_sizes);
+ ret = atomisp_enum_framesizes_crop_inner(isp, fsize, &active, &native, raw, &valid_sizes);
if (ret == 0)
return 0;

@@ -522,7 +565,7 @@ static int atomisp_enum_framesizes_crop(struct atomisp_device *isp,
native.width /= 2;
native.height /= 2;

- return atomisp_enum_framesizes_crop_inner(isp, fsize, &active, &native, &valid_sizes);
+ return atomisp_enum_framesizes_crop_inner(isp, fsize, &active, &native, raw, &valid_sizes);
}

static int atomisp_enum_framesizes(struct file *file, void *priv,
@@ -537,14 +580,33 @@ static int atomisp_enum_framesizes(struct file *file, void *priv,
.which = V4L2_SUBDEV_FORMAT_ACTIVE,
.code = input->code,
};
+ const struct atomisp_format_bridge *format;
struct v4l2_subdev_state *act_sd_state;
int ret;

if (!input->sensor)
return -EINVAL;

+ format = atomisp_get_format_bridge(fsize->pixel_format);
+ if (!format)
+ return -EINVAL;
+
+ if (format->sh_fmt == IA_CSS_FRAME_FORMAT_RAW) {
+ fse.code = format->mbus_code;
+ if (input->crop_support) {
+ DECLARE_BITMAP(raw_formats, ARRAY_SIZE(atomisp_output_fmts)) = { };
+
+ ret = atomisp_enum_raw_formats(input->sensor, raw_formats);
+ if (ret)
+ return ret;
+ if (!test_bit(format - atomisp_output_fmts, raw_formats))
+ return -EINVAL;
+ }
+ }
+
if (input->crop_support)
- return atomisp_enum_framesizes_crop(isp, fsize);
+ return atomisp_enum_framesizes_crop(isp, fsize,
+ format->sh_fmt == IA_CSS_FRAME_FORMAT_RAW);

act_sd_state = v4l2_subdev_lock_and_get_active_state(input->sensor);
ret = v4l2_subdev_call(input->sensor, pad, enum_frame_size,
@@ -555,8 +617,13 @@ static int atomisp_enum_framesizes(struct file *file, void *priv,
return ret;

fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
- fsize->discrete.width = fse.max_width - pad_w;
- fsize->discrete.height = fse.max_height - pad_h;
+ if (format->sh_fmt == IA_CSS_FRAME_FORMAT_RAW) {
+ fsize->discrete.width = fse.max_width;
+ fsize->discrete.height = fse.max_height;
+ } else {
+ fsize->discrete.width = fse.max_width - pad_w;
+ fsize->discrete.height = fse.max_height - pad_h;
+ }

return 0;
}
@@ -602,35 +669,22 @@ static int atomisp_enum_fmt_cap(struct file *file, void *fh,
struct atomisp_device *isp = video_get_drvdata(vdev);
struct atomisp_sub_device *asd = atomisp_to_video_pipe(vdev)->asd;
struct atomisp_input_subdev *input = &isp->inputs[asd->input_curr];
- struct v4l2_subdev_mbus_code_enum code = {
- .which = V4L2_SUBDEV_FORMAT_ACTIVE,
- };
+ DECLARE_BITMAP(raw_formats, ARRAY_SIZE(atomisp_output_fmts)) = { };
const struct atomisp_format_bridge *format;
- struct v4l2_subdev_state *act_sd_state;
unsigned int i, fi = 0;
int ret;

if (!input->sensor)
return -EINVAL;

- act_sd_state = v4l2_subdev_lock_and_get_active_state(input->sensor);
- ret = v4l2_subdev_call(input->sensor, pad, enum_mbus_code,
- act_sd_state, &code);
- if (act_sd_state)
- v4l2_subdev_unlock_state(act_sd_state);
+ ret = atomisp_enum_raw_formats(input->sensor, raw_formats);
if (ret)
return ret;

for (i = 0; i < ARRAY_SIZE(atomisp_output_fmts); i++) {
format = &atomisp_output_fmts[i];
-
- /*
- * Is the atomisp-supported format is valid for the
- * sensor (configuration)? If not, skip it.
- *
- * FIXME: fix the pipeline to allow sensor format too.
- */
- if (format->sh_fmt == IA_CSS_FRAME_FORMAT_RAW)
+ if (format->sh_fmt == IA_CSS_FRAME_FORMAT_RAW &&
+ !test_bit(i, raw_formats))
continue;

/* Found a match. Now let's pick f->index'th one. */
--
2.53.0