1 // SPDX-License-Identifier: (GPL-2.0+ OR MIT)
3 * Rockchip ISP1 Driver - ISP Subdevice
5 * Copyright (C) 2019 Collabora, Ltd.
7 * Based on Rockchip ISP1 driver by Rockchip Electronics Co., Ltd.
8 * Copyright (C) 2017 Rockchip Electronics Co., Ltd.
11 #include <linux/iopoll.h>
12 #include <linux/pm_runtime.h>
13 #include <linux/videodev2.h>
14 #include <linux/vmalloc.h>
16 #include <media/v4l2-event.h>
18 #include "rkisp1-common.h"
20 #define RKISP1_DEF_SINK_PAD_FMT MEDIA_BUS_FMT_SRGGB10_1X10
21 #define RKISP1_DEF_SRC_PAD_FMT MEDIA_BUS_FMT_YUYV8_2X8
23 #define RKISP1_ISP_DEV_NAME RKISP1_DRIVER_NAME "_isp"
26 * NOTE: MIPI controller and input MUX are also configured in this file.
27 * This is because ISP Subdev describes not only ISP submodule (input size,
28 * format, output size, format), but also a virtual route device.
32 * There are many variables named with format/frame in below code,
33 * please see here for their meaning.
34 * Cropping in the sink pad defines the image region from the sensor.
35 * Cropping in the source pad defines the region for the Image Stabilizer (IS)
37 * Cropping regions of ISP
39 * +---------------------------------------------------------+
41 * | +---------------------------------------------------+ |
42 * | | CIF_ISP_ACQ (for black level) | |
43 * | | sink pad format | |
44 * | | +--------------------------------------------+ | |
45 * | | | CIF_ISP_OUT | | |
46 * | | | sink pad crop | | |
47 * | | | +---------------------------------+ | | |
48 * | | | | CIF_ISP_IS | | | |
49 * | | | | source pad crop and format | | | |
50 * | | | +---------------------------------+ | | |
51 * | | +--------------------------------------------+ | |
52 * | +---------------------------------------------------+ |
53 * +---------------------------------------------------------+
56 /* ----------------------------------------------------------------------------
57 * Camera Interface registers configurations
61 * Image Stabilization.
62 * This should only be called when configuring CIF
63 * or at the frame end interrupt
65 static void rkisp1_config_ism(struct rkisp1_isp *isp,
66 struct v4l2_subdev_state *sd_state)
68 const struct v4l2_rect *src_crop =
69 v4l2_subdev_state_get_crop(sd_state,
70 RKISP1_ISP_PAD_SOURCE_VIDEO);
71 struct rkisp1_device *rkisp1 = isp->rkisp1;
74 rkisp1_write(rkisp1, RKISP1_CIF_ISP_IS_RECENTER, 0);
75 rkisp1_write(rkisp1, RKISP1_CIF_ISP_IS_MAX_DX, 0);
76 rkisp1_write(rkisp1, RKISP1_CIF_ISP_IS_MAX_DY, 0);
77 rkisp1_write(rkisp1, RKISP1_CIF_ISP_IS_DISPLACE, 0);
78 rkisp1_write(rkisp1, RKISP1_CIF_ISP_IS_H_OFFS, src_crop->left);
79 rkisp1_write(rkisp1, RKISP1_CIF_ISP_IS_V_OFFS, src_crop->top);
80 rkisp1_write(rkisp1, RKISP1_CIF_ISP_IS_H_SIZE, src_crop->width);
81 rkisp1_write(rkisp1, RKISP1_CIF_ISP_IS_V_SIZE, src_crop->height);
83 /* IS(Image Stabilization) is always on, working as output crop */
84 rkisp1_write(rkisp1, RKISP1_CIF_ISP_IS_CTRL, 1);
85 val = rkisp1_read(rkisp1, RKISP1_CIF_ISP_CTRL);
86 val |= RKISP1_CIF_ISP_CTRL_ISP_CFG_UPD;
87 rkisp1_write(rkisp1, RKISP1_CIF_ISP_CTRL, val);
91 * configure ISP blocks with input format, size......
93 static int rkisp1_config_isp(struct rkisp1_isp *isp,
94 struct v4l2_subdev_state *sd_state,
95 enum v4l2_mbus_type mbus_type, u32 mbus_flags)
97 struct rkisp1_device *rkisp1 = isp->rkisp1;
98 u32 isp_ctrl = 0, irq_mask = 0, acq_mult = 0, acq_prop = 0;
99 const struct rkisp1_mbus_info *sink_fmt;
100 const struct rkisp1_mbus_info *src_fmt;
101 const struct v4l2_mbus_framefmt *src_frm;
102 const struct v4l2_mbus_framefmt *sink_frm;
103 const struct v4l2_rect *sink_crop;
105 sink_frm = v4l2_subdev_state_get_format(sd_state,
106 RKISP1_ISP_PAD_SINK_VIDEO);
107 sink_crop = v4l2_subdev_state_get_crop(sd_state,
108 RKISP1_ISP_PAD_SINK_VIDEO);
109 src_frm = v4l2_subdev_state_get_format(sd_state,
110 RKISP1_ISP_PAD_SOURCE_VIDEO);
112 sink_fmt = rkisp1_mbus_info_get_by_code(sink_frm->code);
113 src_fmt = rkisp1_mbus_info_get_by_code(src_frm->code);
115 if (sink_fmt->pixel_enc == V4L2_PIXEL_ENC_BAYER) {
117 if (src_fmt->pixel_enc == V4L2_PIXEL_ENC_BAYER) {
118 if (mbus_type == V4L2_MBUS_BT656)
119 isp_ctrl = RKISP1_CIF_ISP_CTRL_ISP_MODE_RAW_PICT_ITU656;
121 isp_ctrl = RKISP1_CIF_ISP_CTRL_ISP_MODE_RAW_PICT;
123 rkisp1_write(rkisp1, RKISP1_CIF_ISP_DEMOSAIC,
124 RKISP1_CIF_ISP_DEMOSAIC_TH(0xc));
126 if (mbus_type == V4L2_MBUS_BT656)
127 isp_ctrl = RKISP1_CIF_ISP_CTRL_ISP_MODE_BAYER_ITU656;
129 isp_ctrl = RKISP1_CIF_ISP_CTRL_ISP_MODE_BAYER_ITU601;
131 } else if (sink_fmt->pixel_enc == V4L2_PIXEL_ENC_YUV) {
133 if (mbus_type == V4L2_MBUS_CSI2_DPHY) {
134 isp_ctrl = RKISP1_CIF_ISP_CTRL_ISP_MODE_ITU601;
136 if (mbus_type == V4L2_MBUS_BT656)
137 isp_ctrl = RKISP1_CIF_ISP_CTRL_ISP_MODE_ITU656;
139 isp_ctrl = RKISP1_CIF_ISP_CTRL_ISP_MODE_ITU601;
142 irq_mask |= RKISP1_CIF_ISP_DATA_LOSS;
145 /* Set up input acquisition properties */
146 if (mbus_type == V4L2_MBUS_BT656 || mbus_type == V4L2_MBUS_PARALLEL) {
147 if (mbus_flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
148 acq_prop |= RKISP1_CIF_ISP_ACQ_PROP_POS_EDGE;
150 switch (sink_fmt->bus_width) {
152 acq_prop |= RKISP1_CIF_ISP_ACQ_PROP_IN_SEL_8B_ZERO;
155 acq_prop |= RKISP1_CIF_ISP_ACQ_PROP_IN_SEL_10B_ZERO;
158 acq_prop |= RKISP1_CIF_ISP_ACQ_PROP_IN_SEL_12B;
161 dev_err(rkisp1->dev, "Invalid bus width %u\n",
162 sink_fmt->bus_width);
167 if (mbus_type == V4L2_MBUS_PARALLEL) {
168 if (mbus_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)
169 acq_prop |= RKISP1_CIF_ISP_ACQ_PROP_VSYNC_LOW;
171 if (mbus_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)
172 acq_prop |= RKISP1_CIF_ISP_ACQ_PROP_HSYNC_LOW;
175 rkisp1_write(rkisp1, RKISP1_CIF_ISP_CTRL, isp_ctrl);
176 rkisp1_write(rkisp1, RKISP1_CIF_ISP_ACQ_PROP,
177 acq_prop | sink_fmt->yuv_seq |
178 RKISP1_CIF_ISP_ACQ_PROP_BAYER_PAT(sink_fmt->bayer_pat) |
179 RKISP1_CIF_ISP_ACQ_PROP_FIELD_SEL_ALL);
180 rkisp1_write(rkisp1, RKISP1_CIF_ISP_ACQ_NR_FRAMES, 0);
182 /* Acquisition Size */
183 rkisp1_write(rkisp1, RKISP1_CIF_ISP_ACQ_H_OFFS, 0);
184 rkisp1_write(rkisp1, RKISP1_CIF_ISP_ACQ_V_OFFS, 0);
185 rkisp1_write(rkisp1, RKISP1_CIF_ISP_ACQ_H_SIZE,
186 acq_mult * sink_frm->width);
187 rkisp1_write(rkisp1, RKISP1_CIF_ISP_ACQ_V_SIZE, sink_frm->height);
190 rkisp1_write(rkisp1, RKISP1_CIF_ISP_OUT_H_OFFS, sink_crop->left);
191 rkisp1_write(rkisp1, RKISP1_CIF_ISP_OUT_V_OFFS, sink_crop->top);
192 rkisp1_write(rkisp1, RKISP1_CIF_ISP_OUT_H_SIZE, sink_crop->width);
193 rkisp1_write(rkisp1, RKISP1_CIF_ISP_OUT_V_SIZE, sink_crop->height);
195 irq_mask |= RKISP1_CIF_ISP_FRAME | RKISP1_CIF_ISP_V_START |
196 RKISP1_CIF_ISP_PIC_SIZE_ERROR;
197 rkisp1_write(rkisp1, RKISP1_CIF_ISP_IMSC, irq_mask);
199 if (src_fmt->pixel_enc == V4L2_PIXEL_ENC_BAYER) {
200 rkisp1_params_disable(&rkisp1->params);
202 struct v4l2_mbus_framefmt *src_frm;
204 src_frm = v4l2_subdev_state_get_format(sd_state,
205 RKISP1_ISP_PAD_SOURCE_VIDEO);
206 rkisp1_params_pre_configure(&rkisp1->params, sink_fmt->bayer_pat,
207 src_frm->quantization,
211 isp->sink_fmt = sink_fmt;
217 static void rkisp1_config_path(struct rkisp1_isp *isp,
218 enum v4l2_mbus_type mbus_type)
220 struct rkisp1_device *rkisp1 = isp->rkisp1;
221 u32 dpcl = rkisp1_read(rkisp1, RKISP1_CIF_VI_DPCL);
223 if (mbus_type == V4L2_MBUS_BT656 || mbus_type == V4L2_MBUS_PARALLEL)
224 dpcl |= RKISP1_CIF_VI_DPCL_IF_SEL_PARALLEL;
225 else if (mbus_type == V4L2_MBUS_CSI2_DPHY)
226 dpcl |= RKISP1_CIF_VI_DPCL_IF_SEL_MIPI;
228 rkisp1_write(rkisp1, RKISP1_CIF_VI_DPCL, dpcl);
231 /* Hardware configure Entry */
232 static int rkisp1_config_cif(struct rkisp1_isp *isp,
233 struct v4l2_subdev_state *sd_state,
234 enum v4l2_mbus_type mbus_type, u32 mbus_flags)
238 ret = rkisp1_config_isp(isp, sd_state, mbus_type, mbus_flags);
242 rkisp1_config_path(isp, mbus_type);
243 rkisp1_config_ism(isp, sd_state);
248 static void rkisp1_isp_stop(struct rkisp1_isp *isp)
250 struct rkisp1_device *rkisp1 = isp->rkisp1;
254 * ISP(mi) stop in mi frame end -> Stop ISP(mipi) ->
255 * Stop ISP(isp) ->wait for ISP isp off
257 /* stop and clear MI and ISP interrupts */
258 rkisp1_write(rkisp1, RKISP1_CIF_ISP_IMSC, 0);
259 rkisp1_write(rkisp1, RKISP1_CIF_ISP_ICR, ~0);
261 rkisp1_write(rkisp1, RKISP1_CIF_MI_IMSC, 0);
262 rkisp1_write(rkisp1, RKISP1_CIF_MI_ICR, ~0);
265 val = rkisp1_read(rkisp1, RKISP1_CIF_ISP_CTRL);
266 val &= ~(RKISP1_CIF_ISP_CTRL_ISP_INFORM_ENABLE |
267 RKISP1_CIF_ISP_CTRL_ISP_ENABLE);
268 rkisp1_write(rkisp1, RKISP1_CIF_ISP_CTRL, val);
270 val = rkisp1_read(rkisp1, RKISP1_CIF_ISP_CTRL);
271 rkisp1_write(rkisp1, RKISP1_CIF_ISP_CTRL,
272 val | RKISP1_CIF_ISP_CTRL_ISP_CFG_UPD);
274 readx_poll_timeout(readl, rkisp1->base_addr + RKISP1_CIF_ISP_RIS,
275 val, val & RKISP1_CIF_ISP_OFF, 20, 100);
276 rkisp1_write(rkisp1, RKISP1_CIF_VI_IRCL,
277 RKISP1_CIF_VI_IRCL_MIPI_SW_RST |
278 RKISP1_CIF_VI_IRCL_ISP_SW_RST);
279 rkisp1_write(rkisp1, RKISP1_CIF_VI_IRCL, 0x0);
282 static void rkisp1_config_clk(struct rkisp1_isp *isp)
284 struct rkisp1_device *rkisp1 = isp->rkisp1;
286 u32 val = RKISP1_CIF_VI_ICCL_ISP_CLK | RKISP1_CIF_VI_ICCL_CP_CLK |
287 RKISP1_CIF_VI_ICCL_MRSZ_CLK | RKISP1_CIF_VI_ICCL_SRSZ_CLK |
288 RKISP1_CIF_VI_ICCL_JPEG_CLK | RKISP1_CIF_VI_ICCL_MI_CLK |
289 RKISP1_CIF_VI_ICCL_IE_CLK | RKISP1_CIF_VI_ICCL_MIPI_CLK |
290 RKISP1_CIF_VI_ICCL_DCROP_CLK;
292 rkisp1_write(rkisp1, RKISP1_CIF_VI_ICCL, val);
294 /* ensure sp and mp can run at the same time in V12 */
295 if (rkisp1->info->isp_ver == RKISP1_V12) {
296 val = RKISP1_CIF_CLK_CTRL_MI_Y12 | RKISP1_CIF_CLK_CTRL_MI_SP |
297 RKISP1_CIF_CLK_CTRL_MI_RAW0 | RKISP1_CIF_CLK_CTRL_MI_RAW1 |
298 RKISP1_CIF_CLK_CTRL_MI_READ | RKISP1_CIF_CLK_CTRL_MI_RAWRD |
299 RKISP1_CIF_CLK_CTRL_CP | RKISP1_CIF_CLK_CTRL_IE;
300 rkisp1_write(rkisp1, RKISP1_CIF_VI_ISP_CLK_CTRL_V12, val);
304 static void rkisp1_isp_start(struct rkisp1_isp *isp,
305 struct v4l2_subdev_state *sd_state)
307 struct rkisp1_device *rkisp1 = isp->rkisp1;
308 const struct v4l2_mbus_framefmt *src_fmt;
309 const struct rkisp1_mbus_info *src_info;
312 rkisp1_config_clk(isp);
315 val = rkisp1_read(rkisp1, RKISP1_CIF_ISP_CTRL);
316 val |= RKISP1_CIF_ISP_CTRL_ISP_CFG_UPD |
317 RKISP1_CIF_ISP_CTRL_ISP_ENABLE |
318 RKISP1_CIF_ISP_CTRL_ISP_INFORM_ENABLE;
319 rkisp1_write(rkisp1, RKISP1_CIF_ISP_CTRL, val);
321 src_fmt = v4l2_subdev_state_get_format(sd_state,
322 RKISP1_ISP_PAD_SOURCE_VIDEO);
323 src_info = rkisp1_mbus_info_get_by_code(src_fmt->code);
325 if (src_info->pixel_enc != V4L2_PIXEL_ENC_BAYER)
326 rkisp1_params_post_configure(&rkisp1->params);
329 /* ----------------------------------------------------------------------------
330 * Subdev pad operations
333 static inline struct rkisp1_isp *to_rkisp1_isp(struct v4l2_subdev *sd)
335 return container_of(sd, struct rkisp1_isp, sd);
338 static int rkisp1_isp_enum_mbus_code(struct v4l2_subdev *sd,
339 struct v4l2_subdev_state *sd_state,
340 struct v4l2_subdev_mbus_code_enum *code)
345 if (code->pad == RKISP1_ISP_PAD_SINK_VIDEO) {
346 dir = RKISP1_ISP_SD_SINK;
347 } else if (code->pad == RKISP1_ISP_PAD_SOURCE_VIDEO) {
348 dir = RKISP1_ISP_SD_SRC;
352 code->code = MEDIA_BUS_FMT_METADATA_FIXED;
357 const struct rkisp1_mbus_info *fmt =
358 rkisp1_mbus_info_get_by_index(i);
363 if (fmt->direction & dir)
366 if (code->index == pos - 1) {
367 code->code = fmt->mbus_code;
368 if (fmt->pixel_enc == V4L2_PIXEL_ENC_YUV &&
369 dir == RKISP1_ISP_SD_SRC)
371 V4L2_SUBDEV_MBUS_CODE_CSC_QUANTIZATION;
379 static int rkisp1_isp_enum_frame_size(struct v4l2_subdev *sd,
380 struct v4l2_subdev_state *sd_state,
381 struct v4l2_subdev_frame_size_enum *fse)
383 const struct rkisp1_mbus_info *mbus_info;
385 if (fse->pad == RKISP1_ISP_PAD_SINK_PARAMS ||
386 fse->pad == RKISP1_ISP_PAD_SOURCE_STATS)
392 mbus_info = rkisp1_mbus_info_get_by_code(fse->code);
396 if (!(mbus_info->direction & RKISP1_ISP_SD_SINK) &&
397 fse->pad == RKISP1_ISP_PAD_SINK_VIDEO)
400 if (!(mbus_info->direction & RKISP1_ISP_SD_SRC) &&
401 fse->pad == RKISP1_ISP_PAD_SOURCE_VIDEO)
404 fse->min_width = RKISP1_ISP_MIN_WIDTH;
405 fse->max_width = RKISP1_ISP_MAX_WIDTH;
406 fse->min_height = RKISP1_ISP_MIN_HEIGHT;
407 fse->max_height = RKISP1_ISP_MAX_HEIGHT;
412 static int rkisp1_isp_init_config(struct v4l2_subdev *sd,
413 struct v4l2_subdev_state *sd_state)
415 struct v4l2_mbus_framefmt *sink_fmt, *src_fmt;
416 struct v4l2_rect *sink_crop, *src_crop;
419 sink_fmt = v4l2_subdev_state_get_format(sd_state,
420 RKISP1_ISP_PAD_SINK_VIDEO);
421 sink_fmt->width = RKISP1_DEFAULT_WIDTH;
422 sink_fmt->height = RKISP1_DEFAULT_HEIGHT;
423 sink_fmt->field = V4L2_FIELD_NONE;
424 sink_fmt->code = RKISP1_DEF_SINK_PAD_FMT;
425 sink_fmt->colorspace = V4L2_COLORSPACE_RAW;
426 sink_fmt->xfer_func = V4L2_XFER_FUNC_NONE;
427 sink_fmt->ycbcr_enc = V4L2_YCBCR_ENC_601;
428 sink_fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE;
430 sink_crop = v4l2_subdev_state_get_crop(sd_state,
431 RKISP1_ISP_PAD_SINK_VIDEO);
432 sink_crop->width = RKISP1_DEFAULT_WIDTH;
433 sink_crop->height = RKISP1_DEFAULT_HEIGHT;
437 src_fmt = v4l2_subdev_state_get_format(sd_state,
438 RKISP1_ISP_PAD_SOURCE_VIDEO);
439 *src_fmt = *sink_fmt;
440 src_fmt->code = RKISP1_DEF_SRC_PAD_FMT;
441 src_fmt->colorspace = V4L2_COLORSPACE_SRGB;
442 src_fmt->xfer_func = V4L2_XFER_FUNC_SRGB;
443 src_fmt->ycbcr_enc = V4L2_YCBCR_ENC_601;
444 src_fmt->quantization = V4L2_QUANTIZATION_LIM_RANGE;
446 src_crop = v4l2_subdev_state_get_crop(sd_state,
447 RKISP1_ISP_PAD_SOURCE_VIDEO);
448 *src_crop = *sink_crop;
450 /* Parameters and statistics. */
451 sink_fmt = v4l2_subdev_state_get_format(sd_state,
452 RKISP1_ISP_PAD_SINK_PARAMS);
453 src_fmt = v4l2_subdev_state_get_format(sd_state,
454 RKISP1_ISP_PAD_SOURCE_STATS);
456 sink_fmt->height = 0;
457 sink_fmt->field = V4L2_FIELD_NONE;
458 sink_fmt->code = MEDIA_BUS_FMT_METADATA_FIXED;
459 *src_fmt = *sink_fmt;
464 static void rkisp1_isp_set_src_fmt(struct rkisp1_isp *isp,
465 struct v4l2_subdev_state *sd_state,
466 struct v4l2_mbus_framefmt *format)
468 const struct rkisp1_mbus_info *sink_info;
469 const struct rkisp1_mbus_info *src_info;
470 struct v4l2_mbus_framefmt *sink_fmt;
471 struct v4l2_mbus_framefmt *src_fmt;
472 const struct v4l2_rect *src_crop;
475 sink_fmt = v4l2_subdev_state_get_format(sd_state,
476 RKISP1_ISP_PAD_SINK_VIDEO);
477 src_fmt = v4l2_subdev_state_get_format(sd_state,
478 RKISP1_ISP_PAD_SOURCE_VIDEO);
479 src_crop = v4l2_subdev_state_get_crop(sd_state,
480 RKISP1_ISP_PAD_SOURCE_VIDEO);
483 * Media bus code. The ISP can operate in pass-through mode (Bayer in,
484 * Bayer out or YUV in, YUV out) or process Bayer data to YUV, but
485 * can't convert from YUV to Bayer.
487 sink_info = rkisp1_mbus_info_get_by_code(sink_fmt->code);
489 src_fmt->code = format->code;
490 src_info = rkisp1_mbus_info_get_by_code(src_fmt->code);
491 if (!src_info || !(src_info->direction & RKISP1_ISP_SD_SRC)) {
492 src_fmt->code = RKISP1_DEF_SRC_PAD_FMT;
493 src_info = rkisp1_mbus_info_get_by_code(src_fmt->code);
496 if (sink_info->pixel_enc == V4L2_PIXEL_ENC_YUV &&
497 src_info->pixel_enc == V4L2_PIXEL_ENC_BAYER) {
498 src_fmt->code = sink_fmt->code;
499 src_info = sink_info;
503 * The source width and height must be identical to the source crop
506 src_fmt->width = src_crop->width;
507 src_fmt->height = src_crop->height;
510 * Copy the color space for the sink pad. When converting from Bayer to
511 * YUV, default to a limited quantization range.
513 src_fmt->colorspace = sink_fmt->colorspace;
514 src_fmt->xfer_func = sink_fmt->xfer_func;
515 src_fmt->ycbcr_enc = sink_fmt->ycbcr_enc;
517 if (sink_info->pixel_enc == V4L2_PIXEL_ENC_BAYER &&
518 src_info->pixel_enc == V4L2_PIXEL_ENC_YUV)
519 src_fmt->quantization = V4L2_QUANTIZATION_LIM_RANGE;
521 src_fmt->quantization = sink_fmt->quantization;
524 * Allow setting the source color space fields when the SET_CSC flag is
525 * set and the source format is YUV. If the sink format is YUV, don't
526 * set the color primaries, transfer function or YCbCr encoding as the
527 * ISP is bypassed in that case and passes YUV data through without
530 * The color primaries and transfer function are configured through the
531 * cross-talk matrix and tone curve respectively. Settings for those
532 * hardware blocks are conveyed through the ISP parameters buffer, as
533 * they need to combine color space information with other image tuning
534 * characteristics and can't thus be computed by the kernel based on the
535 * color space. The source pad colorspace and xfer_func fields are thus
536 * ignored by the driver, but can be set by userspace to propagate
537 * accurate color space information down the pipeline.
539 set_csc = format->flags & V4L2_MBUS_FRAMEFMT_SET_CSC;
541 if (set_csc && src_info->pixel_enc == V4L2_PIXEL_ENC_YUV) {
542 if (sink_info->pixel_enc == V4L2_PIXEL_ENC_BAYER) {
543 if (format->colorspace != V4L2_COLORSPACE_DEFAULT)
544 src_fmt->colorspace = format->colorspace;
545 if (format->xfer_func != V4L2_XFER_FUNC_DEFAULT)
546 src_fmt->xfer_func = format->xfer_func;
547 if (format->ycbcr_enc != V4L2_YCBCR_ENC_DEFAULT)
548 src_fmt->ycbcr_enc = format->ycbcr_enc;
551 if (format->quantization != V4L2_QUANTIZATION_DEFAULT)
552 src_fmt->quantization = format->quantization;
558 * Restore the SET_CSC flag if it was set to indicate support for the
562 format->flags |= V4L2_MBUS_FRAMEFMT_SET_CSC;
565 static void rkisp1_isp_set_src_crop(struct rkisp1_isp *isp,
566 struct v4l2_subdev_state *sd_state,
569 struct v4l2_mbus_framefmt *src_fmt;
570 const struct v4l2_rect *sink_crop;
571 struct v4l2_rect *src_crop;
573 src_crop = v4l2_subdev_state_get_crop(sd_state,
574 RKISP1_ISP_PAD_SOURCE_VIDEO);
575 sink_crop = v4l2_subdev_state_get_crop(sd_state,
576 RKISP1_ISP_PAD_SINK_VIDEO);
578 src_crop->left = ALIGN(r->left, 2);
579 src_crop->width = ALIGN(r->width, 2);
580 src_crop->top = r->top;
581 src_crop->height = r->height;
582 rkisp1_sd_adjust_crop_rect(src_crop, sink_crop);
586 /* Propagate to out format */
587 src_fmt = v4l2_subdev_state_get_format(sd_state,
588 RKISP1_ISP_PAD_SOURCE_VIDEO);
589 rkisp1_isp_set_src_fmt(isp, sd_state, src_fmt);
592 static void rkisp1_isp_set_sink_crop(struct rkisp1_isp *isp,
593 struct v4l2_subdev_state *sd_state,
596 struct v4l2_rect *sink_crop, *src_crop;
597 const struct v4l2_mbus_framefmt *sink_fmt;
599 sink_crop = v4l2_subdev_state_get_crop(sd_state,
600 RKISP1_ISP_PAD_SINK_VIDEO);
601 sink_fmt = v4l2_subdev_state_get_format(sd_state,
602 RKISP1_ISP_PAD_SINK_VIDEO);
604 sink_crop->left = ALIGN(r->left, 2);
605 sink_crop->width = ALIGN(r->width, 2);
606 sink_crop->top = r->top;
607 sink_crop->height = r->height;
608 rkisp1_sd_adjust_crop(sink_crop, sink_fmt);
612 /* Propagate to out crop */
613 src_crop = v4l2_subdev_state_get_crop(sd_state,
614 RKISP1_ISP_PAD_SOURCE_VIDEO);
615 rkisp1_isp_set_src_crop(isp, sd_state, src_crop);
618 static void rkisp1_isp_set_sink_fmt(struct rkisp1_isp *isp,
619 struct v4l2_subdev_state *sd_state,
620 struct v4l2_mbus_framefmt *format)
622 const struct rkisp1_mbus_info *mbus_info;
623 struct v4l2_mbus_framefmt *sink_fmt;
624 struct v4l2_rect *sink_crop;
627 sink_fmt = v4l2_subdev_state_get_format(sd_state,
628 RKISP1_ISP_PAD_SINK_VIDEO);
629 sink_fmt->code = format->code;
630 mbus_info = rkisp1_mbus_info_get_by_code(sink_fmt->code);
631 if (!mbus_info || !(mbus_info->direction & RKISP1_ISP_SD_SINK)) {
632 sink_fmt->code = RKISP1_DEF_SINK_PAD_FMT;
633 mbus_info = rkisp1_mbus_info_get_by_code(sink_fmt->code);
636 sink_fmt->width = clamp_t(u32, format->width,
637 RKISP1_ISP_MIN_WIDTH,
638 RKISP1_ISP_MAX_WIDTH);
639 sink_fmt->height = clamp_t(u32, format->height,
640 RKISP1_ISP_MIN_HEIGHT,
641 RKISP1_ISP_MAX_HEIGHT);
644 * Adjust the color space fields. Accept any color primaries and
645 * transfer function for both YUV and Bayer. For YUV any YCbCr encoding
646 * and quantization range is also accepted. For Bayer formats, the YCbCr
647 * encoding isn't applicable, and the quantization range can only be
650 is_yuv = mbus_info->pixel_enc == V4L2_PIXEL_ENC_YUV;
652 sink_fmt->colorspace = format->colorspace ? :
653 (is_yuv ? V4L2_COLORSPACE_SRGB :
654 V4L2_COLORSPACE_RAW);
655 sink_fmt->xfer_func = format->xfer_func ? :
656 V4L2_MAP_XFER_FUNC_DEFAULT(sink_fmt->colorspace);
658 sink_fmt->ycbcr_enc = format->ycbcr_enc ? :
659 V4L2_MAP_YCBCR_ENC_DEFAULT(sink_fmt->colorspace);
660 sink_fmt->quantization = format->quantization ? :
661 V4L2_MAP_QUANTIZATION_DEFAULT(false, sink_fmt->colorspace,
662 sink_fmt->ycbcr_enc);
665 * The YCbCr encoding isn't applicable for non-YUV formats, but
666 * V4L2 has no "no encoding" value. Hardcode it to Rec. 601, it
667 * should be ignored by userspace.
669 sink_fmt->ycbcr_enc = V4L2_YCBCR_ENC_601;
670 sink_fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE;
675 /* Propagate to in crop */
676 sink_crop = v4l2_subdev_state_get_crop(sd_state,
677 RKISP1_ISP_PAD_SINK_VIDEO);
678 rkisp1_isp_set_sink_crop(isp, sd_state, sink_crop);
681 static int rkisp1_isp_set_fmt(struct v4l2_subdev *sd,
682 struct v4l2_subdev_state *sd_state,
683 struct v4l2_subdev_format *fmt)
685 struct rkisp1_isp *isp = to_rkisp1_isp(sd);
687 if (fmt->pad == RKISP1_ISP_PAD_SINK_VIDEO)
688 rkisp1_isp_set_sink_fmt(isp, sd_state, &fmt->format);
689 else if (fmt->pad == RKISP1_ISP_PAD_SOURCE_VIDEO)
690 rkisp1_isp_set_src_fmt(isp, sd_state, &fmt->format);
692 fmt->format = *v4l2_subdev_state_get_format(sd_state,
698 static int rkisp1_isp_get_selection(struct v4l2_subdev *sd,
699 struct v4l2_subdev_state *sd_state,
700 struct v4l2_subdev_selection *sel)
704 if (sel->pad != RKISP1_ISP_PAD_SOURCE_VIDEO &&
705 sel->pad != RKISP1_ISP_PAD_SINK_VIDEO)
708 switch (sel->target) {
709 case V4L2_SEL_TGT_CROP_BOUNDS:
710 if (sel->pad == RKISP1_ISP_PAD_SINK_VIDEO) {
711 struct v4l2_mbus_framefmt *fmt;
713 fmt = v4l2_subdev_state_get_format(sd_state, sel->pad);
714 sel->r.height = fmt->height;
715 sel->r.width = fmt->width;
719 sel->r = *v4l2_subdev_state_get_crop(sd_state,
720 RKISP1_ISP_PAD_SINK_VIDEO);
724 case V4L2_SEL_TGT_CROP:
725 sel->r = *v4l2_subdev_state_get_crop(sd_state, sel->pad);
736 static int rkisp1_isp_set_selection(struct v4l2_subdev *sd,
737 struct v4l2_subdev_state *sd_state,
738 struct v4l2_subdev_selection *sel)
740 struct rkisp1_isp *isp = to_rkisp1_isp(sd);
743 if (sel->target != V4L2_SEL_TGT_CROP)
746 dev_dbg(isp->rkisp1->dev, "%s: pad: %d sel(%d,%d)/%dx%d\n", __func__,
747 sel->pad, sel->r.left, sel->r.top, sel->r.width, sel->r.height);
749 if (sel->pad == RKISP1_ISP_PAD_SINK_VIDEO)
750 rkisp1_isp_set_sink_crop(isp, sd_state, &sel->r);
751 else if (sel->pad == RKISP1_ISP_PAD_SOURCE_VIDEO)
752 rkisp1_isp_set_src_crop(isp, sd_state, &sel->r);
759 static int rkisp1_subdev_link_validate(struct media_link *link)
761 if (link->sink->index == RKISP1_ISP_PAD_SINK_PARAMS)
764 return v4l2_subdev_link_validate(link);
767 static const struct v4l2_subdev_pad_ops rkisp1_isp_pad_ops = {
768 .enum_mbus_code = rkisp1_isp_enum_mbus_code,
769 .enum_frame_size = rkisp1_isp_enum_frame_size,
770 .get_selection = rkisp1_isp_get_selection,
771 .set_selection = rkisp1_isp_set_selection,
772 .init_cfg = rkisp1_isp_init_config,
773 .get_fmt = v4l2_subdev_get_fmt,
774 .set_fmt = rkisp1_isp_set_fmt,
775 .link_validate = v4l2_subdev_link_validate_default,
778 /* ----------------------------------------------------------------------------
782 static int rkisp1_isp_s_stream(struct v4l2_subdev *sd, int enable)
784 struct rkisp1_isp *isp = to_rkisp1_isp(sd);
785 struct rkisp1_device *rkisp1 = isp->rkisp1;
786 struct v4l2_subdev_state *sd_state;
787 struct media_pad *source_pad;
788 struct media_pad *sink_pad;
789 enum v4l2_mbus_type mbus_type;
794 v4l2_subdev_call(rkisp1->source, video, s_stream, false);
795 rkisp1_isp_stop(isp);
799 sink_pad = &isp->pads[RKISP1_ISP_PAD_SINK_VIDEO];
800 source_pad = media_pad_remote_pad_unique(sink_pad);
801 if (IS_ERR(source_pad)) {
802 dev_dbg(rkisp1->dev, "Failed to get source for ISP: %ld\n",
803 PTR_ERR(source_pad));
807 rkisp1->source = media_entity_to_v4l2_subdev(source_pad->entity);
808 if (!rkisp1->source) {
809 /* This should really not happen, so is not worth a message. */
813 if (rkisp1->source == &rkisp1->csi.sd) {
814 mbus_type = V4L2_MBUS_CSI2_DPHY;
817 const struct rkisp1_sensor_async *asd;
818 struct v4l2_async_connection *asc;
820 asc = v4l2_async_connection_unique(rkisp1->source);
824 asd = container_of(asc, struct rkisp1_sensor_async, asd);
826 mbus_type = asd->mbus_type;
827 mbus_flags = asd->mbus_flags;
830 isp->frame_sequence = -1;
832 sd_state = v4l2_subdev_lock_and_get_active_state(sd);
834 ret = rkisp1_config_cif(isp, sd_state, mbus_type, mbus_flags);
838 rkisp1_isp_start(isp, sd_state);
840 ret = v4l2_subdev_call(rkisp1->source, video, s_stream, true);
842 rkisp1_isp_stop(isp);
847 v4l2_subdev_unlock_state(sd_state);
851 static int rkisp1_isp_subs_evt(struct v4l2_subdev *sd, struct v4l2_fh *fh,
852 struct v4l2_event_subscription *sub)
854 if (sub->type != V4L2_EVENT_FRAME_SYNC)
857 /* V4L2_EVENT_FRAME_SYNC doesn't require an id, so zero should be set */
861 return v4l2_event_subscribe(fh, sub, 0, NULL);
864 static const struct media_entity_operations rkisp1_isp_media_ops = {
865 .link_validate = rkisp1_subdev_link_validate,
868 static const struct v4l2_subdev_video_ops rkisp1_isp_video_ops = {
869 .s_stream = rkisp1_isp_s_stream,
872 static const struct v4l2_subdev_core_ops rkisp1_isp_core_ops = {
873 .subscribe_event = rkisp1_isp_subs_evt,
874 .unsubscribe_event = v4l2_event_subdev_unsubscribe,
877 static const struct v4l2_subdev_ops rkisp1_isp_ops = {
878 .core = &rkisp1_isp_core_ops,
879 .video = &rkisp1_isp_video_ops,
880 .pad = &rkisp1_isp_pad_ops,
883 int rkisp1_isp_register(struct rkisp1_device *rkisp1)
885 struct rkisp1_isp *isp = &rkisp1->isp;
886 struct media_pad *pads = isp->pads;
887 struct v4l2_subdev *sd = &isp->sd;
890 isp->rkisp1 = rkisp1;
892 v4l2_subdev_init(sd, &rkisp1_isp_ops);
893 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
894 sd->entity.ops = &rkisp1_isp_media_ops;
895 sd->entity.function = MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER;
896 sd->owner = THIS_MODULE;
897 strscpy(sd->name, RKISP1_ISP_DEV_NAME, sizeof(sd->name));
899 pads[RKISP1_ISP_PAD_SINK_VIDEO].flags = MEDIA_PAD_FL_SINK |
900 MEDIA_PAD_FL_MUST_CONNECT;
901 pads[RKISP1_ISP_PAD_SINK_PARAMS].flags = MEDIA_PAD_FL_SINK;
902 pads[RKISP1_ISP_PAD_SOURCE_VIDEO].flags = MEDIA_PAD_FL_SOURCE;
903 pads[RKISP1_ISP_PAD_SOURCE_STATS].flags = MEDIA_PAD_FL_SOURCE;
905 ret = media_entity_pads_init(&sd->entity, RKISP1_ISP_PAD_MAX, pads);
907 goto err_entity_cleanup;
909 ret = v4l2_subdev_init_finalize(sd);
911 goto err_subdev_cleanup;
913 ret = v4l2_device_register_subdev(&rkisp1->v4l2_dev, sd);
915 dev_err(rkisp1->dev, "Failed to register isp subdev\n");
916 goto err_subdev_cleanup;
922 v4l2_subdev_cleanup(sd);
924 media_entity_cleanup(&sd->entity);
925 isp->sd.v4l2_dev = NULL;
929 void rkisp1_isp_unregister(struct rkisp1_device *rkisp1)
931 struct rkisp1_isp *isp = &rkisp1->isp;
933 if (!isp->sd.v4l2_dev)
936 v4l2_device_unregister_subdev(&isp->sd);
937 media_entity_cleanup(&isp->sd.entity);
940 /* ----------------------------------------------------------------------------
944 static void rkisp1_isp_queue_event_sof(struct rkisp1_isp *isp)
946 struct v4l2_event event = {
947 .type = V4L2_EVENT_FRAME_SYNC,
950 event.u.frame_sync.frame_sequence = isp->frame_sequence;
951 v4l2_event_queue(isp->sd.devnode, &event);
954 irqreturn_t rkisp1_isp_isr(int irq, void *ctx)
956 struct device *dev = ctx;
957 struct rkisp1_device *rkisp1 = dev_get_drvdata(dev);
960 status = rkisp1_read(rkisp1, RKISP1_CIF_ISP_MIS);
964 rkisp1_write(rkisp1, RKISP1_CIF_ISP_ICR, status);
966 /* Vertical sync signal, starting generating new frame */
967 if (status & RKISP1_CIF_ISP_V_START) {
968 rkisp1->isp.frame_sequence++;
969 rkisp1_isp_queue_event_sof(&rkisp1->isp);
970 if (status & RKISP1_CIF_ISP_FRAME) {
971 WARN_ONCE(1, "irq delay is too long, buffers might not be in sync\n");
972 rkisp1->debug.irq_delay++;
975 if (status & RKISP1_CIF_ISP_PIC_SIZE_ERROR) {
976 /* Clear pic_size_error */
977 isp_err = rkisp1_read(rkisp1, RKISP1_CIF_ISP_ERR);
978 if (isp_err & RKISP1_CIF_ISP_ERR_INFORM_SIZE)
979 rkisp1->debug.inform_size_error++;
980 if (isp_err & RKISP1_CIF_ISP_ERR_IS_SIZE)
981 rkisp1->debug.img_stabilization_size_error++;
982 if (isp_err & RKISP1_CIF_ISP_ERR_OUTFORM_SIZE)
983 rkisp1->debug.outform_size_error++;
984 rkisp1_write(rkisp1, RKISP1_CIF_ISP_ERR_CLR, isp_err);
985 } else if (status & RKISP1_CIF_ISP_DATA_LOSS) {
986 /* keep track of data_loss in debugfs */
987 rkisp1->debug.data_loss++;
990 if (status & RKISP1_CIF_ISP_FRAME) {
993 /* New frame from the sensor received */
994 isp_ris = rkisp1_read(rkisp1, RKISP1_CIF_ISP_RIS);
995 if (isp_ris & RKISP1_STATS_MEAS_MASK)
996 rkisp1_stats_isr(&rkisp1->stats, isp_ris);
998 * Then update changed configs. Some of them involve
999 * lot of register writes. Do those only one per frame.
1000 * Do the updates in the order of the processing flow.
1002 rkisp1_params_isr(rkisp1);