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 /* ----------------------------------------------------------------------------
60 static struct v4l2_mbus_framefmt *
61 rkisp1_isp_get_pad_fmt(struct rkisp1_isp *isp,
62 struct v4l2_subdev_state *sd_state,
63 unsigned int pad, u32 which)
65 struct v4l2_subdev_state state = {
69 if (which == V4L2_SUBDEV_FORMAT_TRY)
70 return v4l2_subdev_get_try_format(&isp->sd, sd_state, pad);
72 return v4l2_subdev_get_try_format(&isp->sd, &state, pad);
75 static struct v4l2_rect *
76 rkisp1_isp_get_pad_crop(struct rkisp1_isp *isp,
77 struct v4l2_subdev_state *sd_state,
78 unsigned int pad, u32 which)
80 struct v4l2_subdev_state state = {
84 if (which == V4L2_SUBDEV_FORMAT_TRY)
85 return v4l2_subdev_get_try_crop(&isp->sd, sd_state, pad);
87 return v4l2_subdev_get_try_crop(&isp->sd, &state, pad);
90 /* ----------------------------------------------------------------------------
91 * Camera Interface registers configurations
95 * Image Stabilization.
96 * This should only be called when configuring CIF
97 * or at the frame end interrupt
99 static void rkisp1_config_ism(struct rkisp1_isp *isp)
101 const struct v4l2_rect *src_crop =
102 rkisp1_isp_get_pad_crop(isp, NULL,
103 RKISP1_ISP_PAD_SOURCE_VIDEO,
104 V4L2_SUBDEV_FORMAT_ACTIVE);
105 struct rkisp1_device *rkisp1 = isp->rkisp1;
108 rkisp1_write(rkisp1, RKISP1_CIF_ISP_IS_RECENTER, 0);
109 rkisp1_write(rkisp1, RKISP1_CIF_ISP_IS_MAX_DX, 0);
110 rkisp1_write(rkisp1, RKISP1_CIF_ISP_IS_MAX_DY, 0);
111 rkisp1_write(rkisp1, RKISP1_CIF_ISP_IS_DISPLACE, 0);
112 rkisp1_write(rkisp1, RKISP1_CIF_ISP_IS_H_OFFS, src_crop->left);
113 rkisp1_write(rkisp1, RKISP1_CIF_ISP_IS_V_OFFS, src_crop->top);
114 rkisp1_write(rkisp1, RKISP1_CIF_ISP_IS_H_SIZE, src_crop->width);
115 rkisp1_write(rkisp1, RKISP1_CIF_ISP_IS_V_SIZE, src_crop->height);
117 /* IS(Image Stabilization) is always on, working as output crop */
118 rkisp1_write(rkisp1, RKISP1_CIF_ISP_IS_CTRL, 1);
119 val = rkisp1_read(rkisp1, RKISP1_CIF_ISP_CTRL);
120 val |= RKISP1_CIF_ISP_CTRL_ISP_CFG_UPD;
121 rkisp1_write(rkisp1, RKISP1_CIF_ISP_CTRL, val);
125 * configure ISP blocks with input format, size......
127 static int rkisp1_config_isp(struct rkisp1_isp *isp,
128 enum v4l2_mbus_type mbus_type, u32 mbus_flags)
130 struct rkisp1_device *rkisp1 = isp->rkisp1;
131 u32 isp_ctrl = 0, irq_mask = 0, acq_mult = 0, acq_prop = 0;
132 const struct rkisp1_mbus_info *sink_fmt = isp->sink_fmt;
133 const struct rkisp1_mbus_info *src_fmt = isp->src_fmt;
134 const struct v4l2_mbus_framefmt *sink_frm;
135 const struct v4l2_rect *sink_crop;
137 sink_frm = rkisp1_isp_get_pad_fmt(isp, NULL,
138 RKISP1_ISP_PAD_SINK_VIDEO,
139 V4L2_SUBDEV_FORMAT_ACTIVE);
140 sink_crop = rkisp1_isp_get_pad_crop(isp, NULL,
141 RKISP1_ISP_PAD_SINK_VIDEO,
142 V4L2_SUBDEV_FORMAT_ACTIVE);
144 if (sink_fmt->pixel_enc == V4L2_PIXEL_ENC_BAYER) {
146 if (src_fmt->pixel_enc == V4L2_PIXEL_ENC_BAYER) {
147 if (mbus_type == V4L2_MBUS_BT656)
148 isp_ctrl = RKISP1_CIF_ISP_CTRL_ISP_MODE_RAW_PICT_ITU656;
150 isp_ctrl = RKISP1_CIF_ISP_CTRL_ISP_MODE_RAW_PICT;
152 rkisp1_write(rkisp1, RKISP1_CIF_ISP_DEMOSAIC,
153 RKISP1_CIF_ISP_DEMOSAIC_TH(0xc));
155 if (mbus_type == V4L2_MBUS_BT656)
156 isp_ctrl = RKISP1_CIF_ISP_CTRL_ISP_MODE_BAYER_ITU656;
158 isp_ctrl = RKISP1_CIF_ISP_CTRL_ISP_MODE_BAYER_ITU601;
160 } else if (sink_fmt->pixel_enc == V4L2_PIXEL_ENC_YUV) {
162 if (mbus_type == V4L2_MBUS_CSI2_DPHY) {
163 isp_ctrl = RKISP1_CIF_ISP_CTRL_ISP_MODE_ITU601;
165 if (mbus_type == V4L2_MBUS_BT656)
166 isp_ctrl = RKISP1_CIF_ISP_CTRL_ISP_MODE_ITU656;
168 isp_ctrl = RKISP1_CIF_ISP_CTRL_ISP_MODE_ITU601;
171 irq_mask |= RKISP1_CIF_ISP_DATA_LOSS;
174 /* Set up input acquisition properties */
175 if (mbus_type == V4L2_MBUS_BT656 || mbus_type == V4L2_MBUS_PARALLEL) {
176 if (mbus_flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
177 acq_prop |= RKISP1_CIF_ISP_ACQ_PROP_POS_EDGE;
179 switch (sink_fmt->bus_width) {
181 acq_prop |= RKISP1_CIF_ISP_ACQ_PROP_IN_SEL_8B_ZERO;
184 acq_prop |= RKISP1_CIF_ISP_ACQ_PROP_IN_SEL_10B_ZERO;
187 acq_prop |= RKISP1_CIF_ISP_ACQ_PROP_IN_SEL_12B;
190 dev_err(rkisp1->dev, "Invalid bus width %u\n",
191 sink_fmt->bus_width);
196 if (mbus_type == V4L2_MBUS_PARALLEL) {
197 if (mbus_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)
198 acq_prop |= RKISP1_CIF_ISP_ACQ_PROP_VSYNC_LOW;
200 if (mbus_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)
201 acq_prop |= RKISP1_CIF_ISP_ACQ_PROP_HSYNC_LOW;
204 rkisp1_write(rkisp1, RKISP1_CIF_ISP_CTRL, isp_ctrl);
205 rkisp1_write(rkisp1, RKISP1_CIF_ISP_ACQ_PROP,
206 acq_prop | sink_fmt->yuv_seq |
207 RKISP1_CIF_ISP_ACQ_PROP_BAYER_PAT(sink_fmt->bayer_pat) |
208 RKISP1_CIF_ISP_ACQ_PROP_FIELD_SEL_ALL);
209 rkisp1_write(rkisp1, RKISP1_CIF_ISP_ACQ_NR_FRAMES, 0);
211 /* Acquisition Size */
212 rkisp1_write(rkisp1, RKISP1_CIF_ISP_ACQ_H_OFFS, 0);
213 rkisp1_write(rkisp1, RKISP1_CIF_ISP_ACQ_V_OFFS, 0);
214 rkisp1_write(rkisp1, RKISP1_CIF_ISP_ACQ_H_SIZE,
215 acq_mult * sink_frm->width);
216 rkisp1_write(rkisp1, RKISP1_CIF_ISP_ACQ_V_SIZE, sink_frm->height);
219 rkisp1_write(rkisp1, RKISP1_CIF_ISP_OUT_H_OFFS, sink_crop->left);
220 rkisp1_write(rkisp1, RKISP1_CIF_ISP_OUT_V_OFFS, sink_crop->top);
221 rkisp1_write(rkisp1, RKISP1_CIF_ISP_OUT_H_SIZE, sink_crop->width);
222 rkisp1_write(rkisp1, RKISP1_CIF_ISP_OUT_V_SIZE, sink_crop->height);
224 irq_mask |= RKISP1_CIF_ISP_FRAME | RKISP1_CIF_ISP_V_START |
225 RKISP1_CIF_ISP_PIC_SIZE_ERROR;
226 rkisp1_write(rkisp1, RKISP1_CIF_ISP_IMSC, irq_mask);
228 if (src_fmt->pixel_enc == V4L2_PIXEL_ENC_BAYER) {
229 rkisp1_params_disable(&rkisp1->params);
231 struct v4l2_mbus_framefmt *src_frm;
233 src_frm = rkisp1_isp_get_pad_fmt(isp, NULL,
234 RKISP1_ISP_PAD_SOURCE_VIDEO,
235 V4L2_SUBDEV_FORMAT_ACTIVE);
236 rkisp1_params_pre_configure(&rkisp1->params, sink_fmt->bayer_pat,
237 src_frm->quantization,
245 static void rkisp1_config_path(struct rkisp1_isp *isp,
246 enum v4l2_mbus_type mbus_type)
248 struct rkisp1_device *rkisp1 = isp->rkisp1;
249 u32 dpcl = rkisp1_read(rkisp1, RKISP1_CIF_VI_DPCL);
251 if (mbus_type == V4L2_MBUS_BT656 || mbus_type == V4L2_MBUS_PARALLEL)
252 dpcl |= RKISP1_CIF_VI_DPCL_IF_SEL_PARALLEL;
253 else if (mbus_type == V4L2_MBUS_CSI2_DPHY)
254 dpcl |= RKISP1_CIF_VI_DPCL_IF_SEL_MIPI;
256 rkisp1_write(rkisp1, RKISP1_CIF_VI_DPCL, dpcl);
259 /* Hardware configure Entry */
260 static int rkisp1_config_cif(struct rkisp1_isp *isp,
261 enum v4l2_mbus_type mbus_type, u32 mbus_flags)
265 ret = rkisp1_config_isp(isp, mbus_type, mbus_flags);
269 rkisp1_config_path(isp, mbus_type);
270 rkisp1_config_ism(isp);
275 static void rkisp1_isp_stop(struct rkisp1_isp *isp)
277 struct rkisp1_device *rkisp1 = isp->rkisp1;
281 * ISP(mi) stop in mi frame end -> Stop ISP(mipi) ->
282 * Stop ISP(isp) ->wait for ISP isp off
284 /* stop and clear MI and ISP interrupts */
285 rkisp1_write(rkisp1, RKISP1_CIF_ISP_IMSC, 0);
286 rkisp1_write(rkisp1, RKISP1_CIF_ISP_ICR, ~0);
288 rkisp1_write(rkisp1, RKISP1_CIF_MI_IMSC, 0);
289 rkisp1_write(rkisp1, RKISP1_CIF_MI_ICR, ~0);
292 val = rkisp1_read(rkisp1, RKISP1_CIF_ISP_CTRL);
293 val &= ~(RKISP1_CIF_ISP_CTRL_ISP_INFORM_ENABLE |
294 RKISP1_CIF_ISP_CTRL_ISP_ENABLE);
295 rkisp1_write(rkisp1, RKISP1_CIF_ISP_CTRL, val);
297 val = rkisp1_read(rkisp1, RKISP1_CIF_ISP_CTRL);
298 rkisp1_write(rkisp1, RKISP1_CIF_ISP_CTRL,
299 val | RKISP1_CIF_ISP_CTRL_ISP_CFG_UPD);
301 readx_poll_timeout(readl, rkisp1->base_addr + RKISP1_CIF_ISP_RIS,
302 val, val & RKISP1_CIF_ISP_OFF, 20, 100);
303 rkisp1_write(rkisp1, RKISP1_CIF_VI_IRCL,
304 RKISP1_CIF_VI_IRCL_MIPI_SW_RST |
305 RKISP1_CIF_VI_IRCL_ISP_SW_RST);
306 rkisp1_write(rkisp1, RKISP1_CIF_VI_IRCL, 0x0);
309 static void rkisp1_config_clk(struct rkisp1_isp *isp)
311 struct rkisp1_device *rkisp1 = isp->rkisp1;
313 u32 val = RKISP1_CIF_VI_ICCL_ISP_CLK | RKISP1_CIF_VI_ICCL_CP_CLK |
314 RKISP1_CIF_VI_ICCL_MRSZ_CLK | RKISP1_CIF_VI_ICCL_SRSZ_CLK |
315 RKISP1_CIF_VI_ICCL_JPEG_CLK | RKISP1_CIF_VI_ICCL_MI_CLK |
316 RKISP1_CIF_VI_ICCL_IE_CLK | RKISP1_CIF_VI_ICCL_MIPI_CLK |
317 RKISP1_CIF_VI_ICCL_DCROP_CLK;
319 rkisp1_write(rkisp1, RKISP1_CIF_VI_ICCL, val);
321 /* ensure sp and mp can run at the same time in V12 */
322 if (rkisp1->info->isp_ver == RKISP1_V12) {
323 val = RKISP1_CIF_CLK_CTRL_MI_Y12 | RKISP1_CIF_CLK_CTRL_MI_SP |
324 RKISP1_CIF_CLK_CTRL_MI_RAW0 | RKISP1_CIF_CLK_CTRL_MI_RAW1 |
325 RKISP1_CIF_CLK_CTRL_MI_READ | RKISP1_CIF_CLK_CTRL_MI_RAWRD |
326 RKISP1_CIF_CLK_CTRL_CP | RKISP1_CIF_CLK_CTRL_IE;
327 rkisp1_write(rkisp1, RKISP1_CIF_VI_ISP_CLK_CTRL_V12, val);
331 static void rkisp1_isp_start(struct rkisp1_isp *isp)
333 struct rkisp1_device *rkisp1 = isp->rkisp1;
336 rkisp1_config_clk(isp);
339 val = rkisp1_read(rkisp1, RKISP1_CIF_ISP_CTRL);
340 val |= RKISP1_CIF_ISP_CTRL_ISP_CFG_UPD |
341 RKISP1_CIF_ISP_CTRL_ISP_ENABLE |
342 RKISP1_CIF_ISP_CTRL_ISP_INFORM_ENABLE;
343 rkisp1_write(rkisp1, RKISP1_CIF_ISP_CTRL, val);
345 if (isp->src_fmt->pixel_enc != V4L2_PIXEL_ENC_BAYER)
346 rkisp1_params_post_configure(&rkisp1->params);
349 /* ----------------------------------------------------------------------------
350 * Subdev pad operations
353 static inline struct rkisp1_isp *to_rkisp1_isp(struct v4l2_subdev *sd)
355 return container_of(sd, struct rkisp1_isp, sd);
358 static int rkisp1_isp_enum_mbus_code(struct v4l2_subdev *sd,
359 struct v4l2_subdev_state *sd_state,
360 struct v4l2_subdev_mbus_code_enum *code)
365 if (code->pad == RKISP1_ISP_PAD_SINK_VIDEO) {
366 dir = RKISP1_ISP_SD_SINK;
367 } else if (code->pad == RKISP1_ISP_PAD_SOURCE_VIDEO) {
368 dir = RKISP1_ISP_SD_SRC;
372 code->code = MEDIA_BUS_FMT_METADATA_FIXED;
377 const struct rkisp1_mbus_info *fmt =
378 rkisp1_mbus_info_get_by_index(i);
383 if (fmt->direction & dir)
386 if (code->index == pos - 1) {
387 code->code = fmt->mbus_code;
388 if (fmt->pixel_enc == V4L2_PIXEL_ENC_YUV &&
389 dir == RKISP1_ISP_SD_SRC)
391 V4L2_SUBDEV_MBUS_CODE_CSC_QUANTIZATION;
399 static int rkisp1_isp_enum_frame_size(struct v4l2_subdev *sd,
400 struct v4l2_subdev_state *sd_state,
401 struct v4l2_subdev_frame_size_enum *fse)
403 const struct rkisp1_mbus_info *mbus_info;
405 if (fse->pad == RKISP1_ISP_PAD_SINK_PARAMS ||
406 fse->pad == RKISP1_ISP_PAD_SOURCE_STATS)
412 mbus_info = rkisp1_mbus_info_get_by_code(fse->code);
416 if (!(mbus_info->direction & RKISP1_ISP_SD_SINK) &&
417 fse->pad == RKISP1_ISP_PAD_SINK_VIDEO)
420 if (!(mbus_info->direction & RKISP1_ISP_SD_SRC) &&
421 fse->pad == RKISP1_ISP_PAD_SOURCE_VIDEO)
424 fse->min_width = RKISP1_ISP_MIN_WIDTH;
425 fse->max_width = RKISP1_ISP_MAX_WIDTH;
426 fse->min_height = RKISP1_ISP_MIN_HEIGHT;
427 fse->max_height = RKISP1_ISP_MAX_HEIGHT;
432 static int rkisp1_isp_init_config(struct v4l2_subdev *sd,
433 struct v4l2_subdev_state *sd_state)
435 struct v4l2_mbus_framefmt *sink_fmt, *src_fmt;
436 struct v4l2_rect *sink_crop, *src_crop;
439 sink_fmt = v4l2_subdev_get_try_format(sd, sd_state,
440 RKISP1_ISP_PAD_SINK_VIDEO);
441 sink_fmt->width = RKISP1_DEFAULT_WIDTH;
442 sink_fmt->height = RKISP1_DEFAULT_HEIGHT;
443 sink_fmt->field = V4L2_FIELD_NONE;
444 sink_fmt->code = RKISP1_DEF_SINK_PAD_FMT;
445 sink_fmt->colorspace = V4L2_COLORSPACE_RAW;
446 sink_fmt->xfer_func = V4L2_XFER_FUNC_NONE;
447 sink_fmt->ycbcr_enc = V4L2_YCBCR_ENC_601;
448 sink_fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE;
450 sink_crop = v4l2_subdev_get_try_crop(sd, sd_state,
451 RKISP1_ISP_PAD_SINK_VIDEO);
452 sink_crop->width = RKISP1_DEFAULT_WIDTH;
453 sink_crop->height = RKISP1_DEFAULT_HEIGHT;
457 src_fmt = v4l2_subdev_get_try_format(sd, sd_state,
458 RKISP1_ISP_PAD_SOURCE_VIDEO);
459 *src_fmt = *sink_fmt;
460 src_fmt->code = RKISP1_DEF_SRC_PAD_FMT;
461 src_fmt->colorspace = V4L2_COLORSPACE_SRGB;
462 src_fmt->xfer_func = V4L2_XFER_FUNC_SRGB;
463 src_fmt->ycbcr_enc = V4L2_YCBCR_ENC_601;
464 src_fmt->quantization = V4L2_QUANTIZATION_LIM_RANGE;
466 src_crop = v4l2_subdev_get_try_crop(sd, sd_state,
467 RKISP1_ISP_PAD_SOURCE_VIDEO);
468 *src_crop = *sink_crop;
470 /* Parameters and statistics. */
471 sink_fmt = v4l2_subdev_get_try_format(sd, sd_state,
472 RKISP1_ISP_PAD_SINK_PARAMS);
473 src_fmt = v4l2_subdev_get_try_format(sd, sd_state,
474 RKISP1_ISP_PAD_SOURCE_STATS);
476 sink_fmt->height = 0;
477 sink_fmt->field = V4L2_FIELD_NONE;
478 sink_fmt->code = MEDIA_BUS_FMT_METADATA_FIXED;
479 *src_fmt = *sink_fmt;
484 static void rkisp1_isp_set_src_fmt(struct rkisp1_isp *isp,
485 struct v4l2_subdev_state *sd_state,
486 struct v4l2_mbus_framefmt *format,
489 const struct rkisp1_mbus_info *sink_info;
490 const struct rkisp1_mbus_info *src_info;
491 struct v4l2_mbus_framefmt *sink_fmt;
492 struct v4l2_mbus_framefmt *src_fmt;
493 const struct v4l2_rect *src_crop;
496 sink_fmt = rkisp1_isp_get_pad_fmt(isp, sd_state,
497 RKISP1_ISP_PAD_SINK_VIDEO, which);
498 src_fmt = rkisp1_isp_get_pad_fmt(isp, sd_state,
499 RKISP1_ISP_PAD_SOURCE_VIDEO, which);
500 src_crop = rkisp1_isp_get_pad_crop(isp, sd_state,
501 RKISP1_ISP_PAD_SOURCE_VIDEO, which);
504 * Media bus code. The ISP can operate in pass-through mode (Bayer in,
505 * Bayer out or YUV in, YUV out) or process Bayer data to YUV, but
506 * can't convert from YUV to Bayer.
508 sink_info = rkisp1_mbus_info_get_by_code(sink_fmt->code);
510 src_fmt->code = format->code;
511 src_info = rkisp1_mbus_info_get_by_code(src_fmt->code);
512 if (!src_info || !(src_info->direction & RKISP1_ISP_SD_SRC)) {
513 src_fmt->code = RKISP1_DEF_SRC_PAD_FMT;
514 src_info = rkisp1_mbus_info_get_by_code(src_fmt->code);
517 if (sink_info->pixel_enc == V4L2_PIXEL_ENC_YUV &&
518 src_info->pixel_enc == V4L2_PIXEL_ENC_BAYER) {
519 src_fmt->code = sink_fmt->code;
520 src_info = sink_info;
524 * The source width and height must be identical to the source crop
527 src_fmt->width = src_crop->width;
528 src_fmt->height = src_crop->height;
531 * Copy the color space for the sink pad. When converting from Bayer to
532 * YUV, default to a limited quantization range.
534 src_fmt->colorspace = sink_fmt->colorspace;
535 src_fmt->xfer_func = sink_fmt->xfer_func;
536 src_fmt->ycbcr_enc = sink_fmt->ycbcr_enc;
538 if (sink_info->pixel_enc == V4L2_PIXEL_ENC_BAYER &&
539 src_info->pixel_enc == V4L2_PIXEL_ENC_YUV)
540 src_fmt->quantization = V4L2_QUANTIZATION_LIM_RANGE;
542 src_fmt->quantization = sink_fmt->quantization;
545 * Allow setting the source color space fields when the SET_CSC flag is
546 * set and the source format is YUV. If the sink format is YUV, don't
547 * set the color primaries, transfer function or YCbCr encoding as the
548 * ISP is bypassed in that case and passes YUV data through without
551 * The color primaries and transfer function are configured through the
552 * cross-talk matrix and tone curve respectively. Settings for those
553 * hardware blocks are conveyed through the ISP parameters buffer, as
554 * they need to combine color space information with other image tuning
555 * characteristics and can't thus be computed by the kernel based on the
556 * color space. The source pad colorspace and xfer_func fields are thus
557 * ignored by the driver, but can be set by userspace to propagate
558 * accurate color space information down the pipeline.
560 set_csc = format->flags & V4L2_MBUS_FRAMEFMT_SET_CSC;
562 if (set_csc && src_info->pixel_enc == V4L2_PIXEL_ENC_YUV) {
563 if (sink_info->pixel_enc == V4L2_PIXEL_ENC_BAYER) {
564 if (format->colorspace != V4L2_COLORSPACE_DEFAULT)
565 src_fmt->colorspace = format->colorspace;
566 if (format->xfer_func != V4L2_XFER_FUNC_DEFAULT)
567 src_fmt->xfer_func = format->xfer_func;
568 if (format->ycbcr_enc != V4L2_YCBCR_ENC_DEFAULT)
569 src_fmt->ycbcr_enc = format->ycbcr_enc;
572 if (format->quantization != V4L2_QUANTIZATION_DEFAULT)
573 src_fmt->quantization = format->quantization;
579 * Restore the SET_CSC flag if it was set to indicate support for the
583 format->flags |= V4L2_MBUS_FRAMEFMT_SET_CSC;
585 /* Store the source format info when setting the active format. */
586 if (which == V4L2_SUBDEV_FORMAT_ACTIVE)
587 isp->src_fmt = src_info;
590 static void rkisp1_isp_set_src_crop(struct rkisp1_isp *isp,
591 struct v4l2_subdev_state *sd_state,
592 struct v4l2_rect *r, unsigned int which)
594 struct v4l2_mbus_framefmt *src_fmt;
595 const struct v4l2_rect *sink_crop;
596 struct v4l2_rect *src_crop;
598 src_crop = rkisp1_isp_get_pad_crop(isp, sd_state,
599 RKISP1_ISP_PAD_SOURCE_VIDEO,
601 sink_crop = rkisp1_isp_get_pad_crop(isp, sd_state,
602 RKISP1_ISP_PAD_SINK_VIDEO,
605 src_crop->left = ALIGN(r->left, 2);
606 src_crop->width = ALIGN(r->width, 2);
607 src_crop->top = r->top;
608 src_crop->height = r->height;
609 rkisp1_sd_adjust_crop_rect(src_crop, sink_crop);
613 /* Propagate to out format */
614 src_fmt = rkisp1_isp_get_pad_fmt(isp, sd_state,
615 RKISP1_ISP_PAD_SOURCE_VIDEO, which);
616 rkisp1_isp_set_src_fmt(isp, sd_state, src_fmt, which);
619 static void rkisp1_isp_set_sink_crop(struct rkisp1_isp *isp,
620 struct v4l2_subdev_state *sd_state,
621 struct v4l2_rect *r, unsigned int which)
623 struct v4l2_rect *sink_crop, *src_crop;
624 const struct v4l2_mbus_framefmt *sink_fmt;
626 sink_crop = rkisp1_isp_get_pad_crop(isp, sd_state,
627 RKISP1_ISP_PAD_SINK_VIDEO,
629 sink_fmt = rkisp1_isp_get_pad_fmt(isp, sd_state,
630 RKISP1_ISP_PAD_SINK_VIDEO,
633 sink_crop->left = ALIGN(r->left, 2);
634 sink_crop->width = ALIGN(r->width, 2);
635 sink_crop->top = r->top;
636 sink_crop->height = r->height;
637 rkisp1_sd_adjust_crop(sink_crop, sink_fmt);
641 /* Propagate to out crop */
642 src_crop = rkisp1_isp_get_pad_crop(isp, sd_state,
643 RKISP1_ISP_PAD_SOURCE_VIDEO, which);
644 rkisp1_isp_set_src_crop(isp, sd_state, src_crop, which);
647 static void rkisp1_isp_set_sink_fmt(struct rkisp1_isp *isp,
648 struct v4l2_subdev_state *sd_state,
649 struct v4l2_mbus_framefmt *format,
652 const struct rkisp1_mbus_info *mbus_info;
653 struct v4l2_mbus_framefmt *sink_fmt;
654 struct v4l2_rect *sink_crop;
657 sink_fmt = rkisp1_isp_get_pad_fmt(isp, sd_state,
658 RKISP1_ISP_PAD_SINK_VIDEO,
660 sink_fmt->code = format->code;
661 mbus_info = rkisp1_mbus_info_get_by_code(sink_fmt->code);
662 if (!mbus_info || !(mbus_info->direction & RKISP1_ISP_SD_SINK)) {
663 sink_fmt->code = RKISP1_DEF_SINK_PAD_FMT;
664 mbus_info = rkisp1_mbus_info_get_by_code(sink_fmt->code);
666 if (which == V4L2_SUBDEV_FORMAT_ACTIVE)
667 isp->sink_fmt = mbus_info;
669 sink_fmt->width = clamp_t(u32, format->width,
670 RKISP1_ISP_MIN_WIDTH,
671 RKISP1_ISP_MAX_WIDTH);
672 sink_fmt->height = clamp_t(u32, format->height,
673 RKISP1_ISP_MIN_HEIGHT,
674 RKISP1_ISP_MAX_HEIGHT);
677 * Adjust the color space fields. Accept any color primaries and
678 * transfer function for both YUV and Bayer. For YUV any YCbCr encoding
679 * and quantization range is also accepted. For Bayer formats, the YCbCr
680 * encoding isn't applicable, and the quantization range can only be
683 is_yuv = mbus_info->pixel_enc == V4L2_PIXEL_ENC_YUV;
685 sink_fmt->colorspace = format->colorspace ? :
686 (is_yuv ? V4L2_COLORSPACE_SRGB :
687 V4L2_COLORSPACE_RAW);
688 sink_fmt->xfer_func = format->xfer_func ? :
689 V4L2_MAP_XFER_FUNC_DEFAULT(sink_fmt->colorspace);
691 sink_fmt->ycbcr_enc = format->ycbcr_enc ? :
692 V4L2_MAP_YCBCR_ENC_DEFAULT(sink_fmt->colorspace);
693 sink_fmt->quantization = format->quantization ? :
694 V4L2_MAP_QUANTIZATION_DEFAULT(false, sink_fmt->colorspace,
695 sink_fmt->ycbcr_enc);
698 * The YCbCr encoding isn't applicable for non-YUV formats, but
699 * V4L2 has no "no encoding" value. Hardcode it to Rec. 601, it
700 * should be ignored by userspace.
702 sink_fmt->ycbcr_enc = V4L2_YCBCR_ENC_601;
703 sink_fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE;
708 /* Propagate to in crop */
709 sink_crop = rkisp1_isp_get_pad_crop(isp, sd_state,
710 RKISP1_ISP_PAD_SINK_VIDEO,
712 rkisp1_isp_set_sink_crop(isp, sd_state, sink_crop, which);
715 static int rkisp1_isp_get_fmt(struct v4l2_subdev *sd,
716 struct v4l2_subdev_state *sd_state,
717 struct v4l2_subdev_format *fmt)
719 struct rkisp1_isp *isp = to_rkisp1_isp(sd);
721 mutex_lock(&isp->ops_lock);
722 fmt->format = *rkisp1_isp_get_pad_fmt(isp, sd_state, fmt->pad,
724 mutex_unlock(&isp->ops_lock);
728 static int rkisp1_isp_set_fmt(struct v4l2_subdev *sd,
729 struct v4l2_subdev_state *sd_state,
730 struct v4l2_subdev_format *fmt)
732 struct rkisp1_isp *isp = to_rkisp1_isp(sd);
734 mutex_lock(&isp->ops_lock);
735 if (fmt->pad == RKISP1_ISP_PAD_SINK_VIDEO)
736 rkisp1_isp_set_sink_fmt(isp, sd_state, &fmt->format,
738 else if (fmt->pad == RKISP1_ISP_PAD_SOURCE_VIDEO)
739 rkisp1_isp_set_src_fmt(isp, sd_state, &fmt->format,
742 fmt->format = *rkisp1_isp_get_pad_fmt(isp, sd_state, fmt->pad,
745 mutex_unlock(&isp->ops_lock);
749 static int rkisp1_isp_get_selection(struct v4l2_subdev *sd,
750 struct v4l2_subdev_state *sd_state,
751 struct v4l2_subdev_selection *sel)
753 struct rkisp1_isp *isp = to_rkisp1_isp(sd);
756 if (sel->pad != RKISP1_ISP_PAD_SOURCE_VIDEO &&
757 sel->pad != RKISP1_ISP_PAD_SINK_VIDEO)
760 mutex_lock(&isp->ops_lock);
761 switch (sel->target) {
762 case V4L2_SEL_TGT_CROP_BOUNDS:
763 if (sel->pad == RKISP1_ISP_PAD_SINK_VIDEO) {
764 struct v4l2_mbus_framefmt *fmt;
766 fmt = rkisp1_isp_get_pad_fmt(isp, sd_state, sel->pad,
768 sel->r.height = fmt->height;
769 sel->r.width = fmt->width;
773 sel->r = *rkisp1_isp_get_pad_crop(isp, sd_state,
774 RKISP1_ISP_PAD_SINK_VIDEO,
778 case V4L2_SEL_TGT_CROP:
779 sel->r = *rkisp1_isp_get_pad_crop(isp, sd_state, sel->pad,
785 mutex_unlock(&isp->ops_lock);
789 static int rkisp1_isp_set_selection(struct v4l2_subdev *sd,
790 struct v4l2_subdev_state *sd_state,
791 struct v4l2_subdev_selection *sel)
793 struct rkisp1_isp *isp = to_rkisp1_isp(sd);
796 if (sel->target != V4L2_SEL_TGT_CROP)
799 dev_dbg(isp->rkisp1->dev, "%s: pad: %d sel(%d,%d)/%dx%d\n", __func__,
800 sel->pad, sel->r.left, sel->r.top, sel->r.width, sel->r.height);
801 mutex_lock(&isp->ops_lock);
802 if (sel->pad == RKISP1_ISP_PAD_SINK_VIDEO)
803 rkisp1_isp_set_sink_crop(isp, sd_state, &sel->r, sel->which);
804 else if (sel->pad == RKISP1_ISP_PAD_SOURCE_VIDEO)
805 rkisp1_isp_set_src_crop(isp, sd_state, &sel->r, sel->which);
809 mutex_unlock(&isp->ops_lock);
813 static int rkisp1_subdev_link_validate(struct media_link *link)
815 if (link->sink->index == RKISP1_ISP_PAD_SINK_PARAMS)
818 return v4l2_subdev_link_validate(link);
821 static const struct v4l2_subdev_pad_ops rkisp1_isp_pad_ops = {
822 .enum_mbus_code = rkisp1_isp_enum_mbus_code,
823 .enum_frame_size = rkisp1_isp_enum_frame_size,
824 .get_selection = rkisp1_isp_get_selection,
825 .set_selection = rkisp1_isp_set_selection,
826 .init_cfg = rkisp1_isp_init_config,
827 .get_fmt = rkisp1_isp_get_fmt,
828 .set_fmt = rkisp1_isp_set_fmt,
829 .link_validate = v4l2_subdev_link_validate_default,
832 /* ----------------------------------------------------------------------------
836 static int rkisp1_isp_s_stream(struct v4l2_subdev *sd, int enable)
838 struct rkisp1_isp *isp = to_rkisp1_isp(sd);
839 struct rkisp1_device *rkisp1 = isp->rkisp1;
840 struct media_pad *source_pad;
841 struct media_pad *sink_pad;
842 enum v4l2_mbus_type mbus_type;
847 v4l2_subdev_call(rkisp1->source, video, s_stream, false);
848 rkisp1_isp_stop(isp);
852 sink_pad = &isp->pads[RKISP1_ISP_PAD_SINK_VIDEO];
853 source_pad = media_pad_remote_pad_unique(sink_pad);
854 if (IS_ERR(source_pad)) {
855 dev_dbg(rkisp1->dev, "Failed to get source for ISP: %ld\n",
856 PTR_ERR(source_pad));
860 rkisp1->source = media_entity_to_v4l2_subdev(source_pad->entity);
861 if (!rkisp1->source) {
862 /* This should really not happen, so is not worth a message. */
866 if (rkisp1->source == &rkisp1->csi.sd) {
867 mbus_type = V4L2_MBUS_CSI2_DPHY;
870 const struct rkisp1_sensor_async *asd;
872 asd = container_of(rkisp1->source->asd,
873 struct rkisp1_sensor_async, asd);
875 mbus_type = asd->mbus_type;
876 mbus_flags = asd->mbus_flags;
879 isp->frame_sequence = -1;
880 mutex_lock(&isp->ops_lock);
881 ret = rkisp1_config_cif(isp, mbus_type, mbus_flags);
885 rkisp1_isp_start(isp);
887 ret = v4l2_subdev_call(rkisp1->source, video, s_stream, true);
889 rkisp1_isp_stop(isp);
894 mutex_unlock(&isp->ops_lock);
898 static int rkisp1_isp_subs_evt(struct v4l2_subdev *sd, struct v4l2_fh *fh,
899 struct v4l2_event_subscription *sub)
901 if (sub->type != V4L2_EVENT_FRAME_SYNC)
904 /* V4L2_EVENT_FRAME_SYNC doesn't require an id, so zero should be set */
908 return v4l2_event_subscribe(fh, sub, 0, NULL);
911 static const struct media_entity_operations rkisp1_isp_media_ops = {
912 .link_validate = rkisp1_subdev_link_validate,
915 static const struct v4l2_subdev_video_ops rkisp1_isp_video_ops = {
916 .s_stream = rkisp1_isp_s_stream,
919 static const struct v4l2_subdev_core_ops rkisp1_isp_core_ops = {
920 .subscribe_event = rkisp1_isp_subs_evt,
921 .unsubscribe_event = v4l2_event_subdev_unsubscribe,
924 static const struct v4l2_subdev_ops rkisp1_isp_ops = {
925 .core = &rkisp1_isp_core_ops,
926 .video = &rkisp1_isp_video_ops,
927 .pad = &rkisp1_isp_pad_ops,
930 int rkisp1_isp_register(struct rkisp1_device *rkisp1)
932 struct v4l2_subdev_state state = {
933 .pads = rkisp1->isp.pad_cfg
935 struct rkisp1_isp *isp = &rkisp1->isp;
936 struct media_pad *pads = isp->pads;
937 struct v4l2_subdev *sd = &isp->sd;
940 isp->rkisp1 = rkisp1;
942 v4l2_subdev_init(sd, &rkisp1_isp_ops);
943 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
944 sd->entity.ops = &rkisp1_isp_media_ops;
945 sd->entity.function = MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER;
946 sd->owner = THIS_MODULE;
947 strscpy(sd->name, RKISP1_ISP_DEV_NAME, sizeof(sd->name));
949 pads[RKISP1_ISP_PAD_SINK_VIDEO].flags = MEDIA_PAD_FL_SINK |
950 MEDIA_PAD_FL_MUST_CONNECT;
951 pads[RKISP1_ISP_PAD_SINK_PARAMS].flags = MEDIA_PAD_FL_SINK;
952 pads[RKISP1_ISP_PAD_SOURCE_VIDEO].flags = MEDIA_PAD_FL_SOURCE;
953 pads[RKISP1_ISP_PAD_SOURCE_STATS].flags = MEDIA_PAD_FL_SOURCE;
955 isp->sink_fmt = rkisp1_mbus_info_get_by_code(RKISP1_DEF_SINK_PAD_FMT);
956 isp->src_fmt = rkisp1_mbus_info_get_by_code(RKISP1_DEF_SRC_PAD_FMT);
958 mutex_init(&isp->ops_lock);
959 ret = media_entity_pads_init(&sd->entity, RKISP1_ISP_PAD_MAX, pads);
963 ret = v4l2_device_register_subdev(&rkisp1->v4l2_dev, sd);
965 dev_err(rkisp1->dev, "Failed to register isp subdev\n");
969 rkisp1_isp_init_config(sd, &state);
974 media_entity_cleanup(&sd->entity);
975 mutex_destroy(&isp->ops_lock);
976 isp->sd.v4l2_dev = NULL;
980 void rkisp1_isp_unregister(struct rkisp1_device *rkisp1)
982 struct rkisp1_isp *isp = &rkisp1->isp;
984 if (!isp->sd.v4l2_dev)
987 v4l2_device_unregister_subdev(&isp->sd);
988 media_entity_cleanup(&isp->sd.entity);
989 mutex_destroy(&isp->ops_lock);
992 /* ----------------------------------------------------------------------------
996 static void rkisp1_isp_queue_event_sof(struct rkisp1_isp *isp)
998 struct v4l2_event event = {
999 .type = V4L2_EVENT_FRAME_SYNC,
1002 event.u.frame_sync.frame_sequence = isp->frame_sequence;
1003 v4l2_event_queue(isp->sd.devnode, &event);
1006 irqreturn_t rkisp1_isp_isr(int irq, void *ctx)
1008 struct device *dev = ctx;
1009 struct rkisp1_device *rkisp1 = dev_get_drvdata(dev);
1010 u32 status, isp_err;
1012 status = rkisp1_read(rkisp1, RKISP1_CIF_ISP_MIS);
1016 rkisp1_write(rkisp1, RKISP1_CIF_ISP_ICR, status);
1018 /* Vertical sync signal, starting generating new frame */
1019 if (status & RKISP1_CIF_ISP_V_START) {
1020 rkisp1->isp.frame_sequence++;
1021 rkisp1_isp_queue_event_sof(&rkisp1->isp);
1022 if (status & RKISP1_CIF_ISP_FRAME) {
1023 WARN_ONCE(1, "irq delay is too long, buffers might not be in sync\n");
1024 rkisp1->debug.irq_delay++;
1027 if (status & RKISP1_CIF_ISP_PIC_SIZE_ERROR) {
1028 /* Clear pic_size_error */
1029 isp_err = rkisp1_read(rkisp1, RKISP1_CIF_ISP_ERR);
1030 if (isp_err & RKISP1_CIF_ISP_ERR_INFORM_SIZE)
1031 rkisp1->debug.inform_size_error++;
1032 if (isp_err & RKISP1_CIF_ISP_ERR_IS_SIZE)
1033 rkisp1->debug.img_stabilization_size_error++;
1034 if (isp_err & RKISP1_CIF_ISP_ERR_OUTFORM_SIZE)
1035 rkisp1->debug.outform_size_error++;
1036 rkisp1_write(rkisp1, RKISP1_CIF_ISP_ERR_CLR, isp_err);
1037 } else if (status & RKISP1_CIF_ISP_DATA_LOSS) {
1038 /* keep track of data_loss in debugfs */
1039 rkisp1->debug.data_loss++;
1042 if (status & RKISP1_CIF_ISP_FRAME) {
1045 /* New frame from the sensor received */
1046 isp_ris = rkisp1_read(rkisp1, RKISP1_CIF_ISP_RIS);
1047 if (isp_ris & RKISP1_STATS_MEAS_MASK)
1048 rkisp1_stats_isr(&rkisp1->stats, isp_ris);
1050 * Then update changed configs. Some of them involve
1051 * lot of register writes. Do those only one per frame.
1052 * Do the updates in the order of the processing flow.
1054 rkisp1_params_isr(rkisp1);