1 // SPDX-License-Identifier: (GPL-2.0+ OR MIT) 2 /* 3 * Rockchip ISP1 Driver - ISP Subdevice 4 * 5 * Copyright (C) 2019 Collabora, Ltd. 6 * 7 * Based on Rockchip ISP1 driver by Rockchip Electronics Co., Ltd. 8 * Copyright (C) 2017 Rockchip Electronics Co., Ltd. 9 */ 10 11 #include <linux/iopoll.h> 12 #include <linux/pm_runtime.h> 13 #include <linux/videodev2.h> 14 #include <linux/vmalloc.h> 15 16 #include <media/v4l2-event.h> 17 18 #include "rkisp1-common.h" 19 20 #define RKISP1_DEF_SINK_PAD_FMT MEDIA_BUS_FMT_SRGGB10_1X10 21 #define RKISP1_DEF_SRC_PAD_FMT MEDIA_BUS_FMT_YUYV8_2X8 22 23 #define RKISP1_ISP_DEV_NAME RKISP1_DRIVER_NAME "_isp" 24 25 /* 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. 29 */ 30 31 /* 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) 36 * 37 * Cropping regions of ISP 38 * 39 * +---------------------------------------------------------+ 40 * | Sensor image | 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 * +---------------------------------------------------------+ 54 */ 55 56 /* ---------------------------------------------------------------------------- 57 * Helpers 58 */ 59 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) 64 { 65 struct v4l2_subdev_state state = { 66 .pads = isp->pad_cfg 67 }; 68 69 if (which == V4L2_SUBDEV_FORMAT_TRY) 70 return v4l2_subdev_get_try_format(&isp->sd, sd_state, pad); 71 else 72 return v4l2_subdev_get_try_format(&isp->sd, &state, pad); 73 } 74 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) 79 { 80 struct v4l2_subdev_state state = { 81 .pads = isp->pad_cfg 82 }; 83 84 if (which == V4L2_SUBDEV_FORMAT_TRY) 85 return v4l2_subdev_get_try_crop(&isp->sd, sd_state, pad); 86 else 87 return v4l2_subdev_get_try_crop(&isp->sd, &state, pad); 88 } 89 90 /* ---------------------------------------------------------------------------- 91 * Camera Interface registers configurations 92 */ 93 94 /* 95 * Image Stabilization. 96 * This should only be called when configuring CIF 97 * or at the frame end interrupt 98 */ 99 static void rkisp1_config_ism(struct rkisp1_isp *isp) 100 { 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; 106 u32 val; 107 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); 116 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); 122 } 123 124 /* 125 * configure ISP blocks with input format, size...... 126 */ 127 static int rkisp1_config_isp(struct rkisp1_isp *isp, 128 enum v4l2_mbus_type mbus_type, u32 mbus_flags) 129 { 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; 136 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); 143 144 if (sink_fmt->pixel_enc == V4L2_PIXEL_ENC_BAYER) { 145 acq_mult = 1; 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; 149 else 150 isp_ctrl = RKISP1_CIF_ISP_CTRL_ISP_MODE_RAW_PICT; 151 } else { 152 rkisp1_write(rkisp1, RKISP1_CIF_ISP_DEMOSAIC, 153 RKISP1_CIF_ISP_DEMOSAIC_TH(0xc)); 154 155 if (mbus_type == V4L2_MBUS_BT656) 156 isp_ctrl = RKISP1_CIF_ISP_CTRL_ISP_MODE_BAYER_ITU656; 157 else 158 isp_ctrl = RKISP1_CIF_ISP_CTRL_ISP_MODE_BAYER_ITU601; 159 } 160 } else if (sink_fmt->pixel_enc == V4L2_PIXEL_ENC_YUV) { 161 acq_mult = 2; 162 if (mbus_type == V4L2_MBUS_CSI2_DPHY) { 163 isp_ctrl = RKISP1_CIF_ISP_CTRL_ISP_MODE_ITU601; 164 } else { 165 if (mbus_type == V4L2_MBUS_BT656) 166 isp_ctrl = RKISP1_CIF_ISP_CTRL_ISP_MODE_ITU656; 167 else 168 isp_ctrl = RKISP1_CIF_ISP_CTRL_ISP_MODE_ITU601; 169 } 170 171 irq_mask |= RKISP1_CIF_ISP_DATA_LOSS; 172 } 173 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; 178 179 switch (sink_fmt->bus_width) { 180 case 8: 181 acq_prop |= RKISP1_CIF_ISP_ACQ_PROP_IN_SEL_8B_ZERO; 182 break; 183 case 10: 184 acq_prop |= RKISP1_CIF_ISP_ACQ_PROP_IN_SEL_10B_ZERO; 185 break; 186 case 12: 187 acq_prop |= RKISP1_CIF_ISP_ACQ_PROP_IN_SEL_12B; 188 break; 189 default: 190 dev_err(rkisp1->dev, "Invalid bus width %u\n", 191 sink_fmt->bus_width); 192 return -EINVAL; 193 } 194 } 195 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; 199 200 if (mbus_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW) 201 acq_prop |= RKISP1_CIF_ISP_ACQ_PROP_HSYNC_LOW; 202 } 203 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); 210 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); 217 218 /* ISP Out Area */ 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); 223 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); 227 228 if (src_fmt->pixel_enc == V4L2_PIXEL_ENC_BAYER) { 229 rkisp1_params_disable(&rkisp1->params); 230 } else { 231 struct v4l2_mbus_framefmt *src_frm; 232 233 src_frm = rkisp1_isp_get_pad_fmt(isp, NULL, 234 RKISP1_ISP_PAD_SINK_VIDEO, 235 V4L2_SUBDEV_FORMAT_ACTIVE); 236 rkisp1_params_configure(&rkisp1->params, sink_fmt->bayer_pat, 237 src_frm->quantization); 238 } 239 240 return 0; 241 } 242 243 /* Configure MUX */ 244 static void rkisp1_config_path(struct rkisp1_isp *isp, 245 enum v4l2_mbus_type mbus_type) 246 { 247 struct rkisp1_device *rkisp1 = isp->rkisp1; 248 u32 dpcl = rkisp1_read(rkisp1, RKISP1_CIF_VI_DPCL); 249 250 if (mbus_type == V4L2_MBUS_BT656 || mbus_type == V4L2_MBUS_PARALLEL) 251 dpcl |= RKISP1_CIF_VI_DPCL_IF_SEL_PARALLEL; 252 else if (mbus_type == V4L2_MBUS_CSI2_DPHY) 253 dpcl |= RKISP1_CIF_VI_DPCL_IF_SEL_MIPI; 254 255 rkisp1_write(rkisp1, RKISP1_CIF_VI_DPCL, dpcl); 256 } 257 258 /* Hardware configure Entry */ 259 static int rkisp1_config_cif(struct rkisp1_isp *isp, 260 enum v4l2_mbus_type mbus_type, u32 mbus_flags) 261 { 262 int ret; 263 264 ret = rkisp1_config_isp(isp, mbus_type, mbus_flags); 265 if (ret) 266 return ret; 267 268 rkisp1_config_path(isp, mbus_type); 269 rkisp1_config_ism(isp); 270 271 return 0; 272 } 273 274 static void rkisp1_isp_stop(struct rkisp1_isp *isp) 275 { 276 struct rkisp1_device *rkisp1 = isp->rkisp1; 277 u32 val; 278 279 /* 280 * ISP(mi) stop in mi frame end -> Stop ISP(mipi) -> 281 * Stop ISP(isp) ->wait for ISP isp off 282 */ 283 /* stop and clear MI and ISP interrupts */ 284 rkisp1_write(rkisp1, RKISP1_CIF_ISP_IMSC, 0); 285 rkisp1_write(rkisp1, RKISP1_CIF_ISP_ICR, ~0); 286 287 rkisp1_write(rkisp1, RKISP1_CIF_MI_IMSC, 0); 288 rkisp1_write(rkisp1, RKISP1_CIF_MI_ICR, ~0); 289 290 /* stop ISP */ 291 val = rkisp1_read(rkisp1, RKISP1_CIF_ISP_CTRL); 292 val &= ~(RKISP1_CIF_ISP_CTRL_ISP_INFORM_ENABLE | 293 RKISP1_CIF_ISP_CTRL_ISP_ENABLE); 294 rkisp1_write(rkisp1, RKISP1_CIF_ISP_CTRL, val); 295 296 val = rkisp1_read(rkisp1, RKISP1_CIF_ISP_CTRL); 297 rkisp1_write(rkisp1, RKISP1_CIF_ISP_CTRL, 298 val | RKISP1_CIF_ISP_CTRL_ISP_CFG_UPD); 299 300 readx_poll_timeout(readl, rkisp1->base_addr + RKISP1_CIF_ISP_RIS, 301 val, val & RKISP1_CIF_ISP_OFF, 20, 100); 302 rkisp1_write(rkisp1, RKISP1_CIF_VI_IRCL, 303 RKISP1_CIF_VI_IRCL_MIPI_SW_RST | 304 RKISP1_CIF_VI_IRCL_ISP_SW_RST); 305 rkisp1_write(rkisp1, RKISP1_CIF_VI_IRCL, 0x0); 306 } 307 308 static void rkisp1_config_clk(struct rkisp1_isp *isp) 309 { 310 struct rkisp1_device *rkisp1 = isp->rkisp1; 311 312 u32 val = RKISP1_CIF_VI_ICCL_ISP_CLK | RKISP1_CIF_VI_ICCL_CP_CLK | 313 RKISP1_CIF_VI_ICCL_MRSZ_CLK | RKISP1_CIF_VI_ICCL_SRSZ_CLK | 314 RKISP1_CIF_VI_ICCL_JPEG_CLK | RKISP1_CIF_VI_ICCL_MI_CLK | 315 RKISP1_CIF_VI_ICCL_IE_CLK | RKISP1_CIF_VI_ICCL_MIPI_CLK | 316 RKISP1_CIF_VI_ICCL_DCROP_CLK; 317 318 rkisp1_write(rkisp1, RKISP1_CIF_VI_ICCL, val); 319 320 /* ensure sp and mp can run at the same time in V12 */ 321 if (rkisp1->info->isp_ver == RKISP1_V12) { 322 val = RKISP1_CIF_CLK_CTRL_MI_Y12 | RKISP1_CIF_CLK_CTRL_MI_SP | 323 RKISP1_CIF_CLK_CTRL_MI_RAW0 | RKISP1_CIF_CLK_CTRL_MI_RAW1 | 324 RKISP1_CIF_CLK_CTRL_MI_READ | RKISP1_CIF_CLK_CTRL_MI_RAWRD | 325 RKISP1_CIF_CLK_CTRL_CP | RKISP1_CIF_CLK_CTRL_IE; 326 rkisp1_write(rkisp1, RKISP1_CIF_VI_ISP_CLK_CTRL_V12, val); 327 } 328 } 329 330 static void rkisp1_isp_start(struct rkisp1_isp *isp) 331 { 332 struct rkisp1_device *rkisp1 = isp->rkisp1; 333 u32 val; 334 335 rkisp1_config_clk(isp); 336 337 /* Activate ISP */ 338 val = rkisp1_read(rkisp1, RKISP1_CIF_ISP_CTRL); 339 val |= RKISP1_CIF_ISP_CTRL_ISP_CFG_UPD | 340 RKISP1_CIF_ISP_CTRL_ISP_ENABLE | 341 RKISP1_CIF_ISP_CTRL_ISP_INFORM_ENABLE; 342 rkisp1_write(rkisp1, RKISP1_CIF_ISP_CTRL, val); 343 } 344 345 /* ---------------------------------------------------------------------------- 346 * Subdev pad operations 347 */ 348 349 static inline struct rkisp1_isp *to_rkisp1_isp(struct v4l2_subdev *sd) 350 { 351 return container_of(sd, struct rkisp1_isp, sd); 352 } 353 354 static int rkisp1_isp_enum_mbus_code(struct v4l2_subdev *sd, 355 struct v4l2_subdev_state *sd_state, 356 struct v4l2_subdev_mbus_code_enum *code) 357 { 358 unsigned int i, dir; 359 int pos = 0; 360 361 if (code->pad == RKISP1_ISP_PAD_SINK_VIDEO) { 362 dir = RKISP1_ISP_SD_SINK; 363 } else if (code->pad == RKISP1_ISP_PAD_SOURCE_VIDEO) { 364 dir = RKISP1_ISP_SD_SRC; 365 } else { 366 if (code->index > 0) 367 return -EINVAL; 368 code->code = MEDIA_BUS_FMT_METADATA_FIXED; 369 return 0; 370 } 371 372 for (i = 0; ; i++) { 373 const struct rkisp1_mbus_info *fmt = 374 rkisp1_mbus_info_get_by_index(i); 375 376 if (!fmt) 377 return -EINVAL; 378 379 if (fmt->direction & dir) 380 pos++; 381 382 if (code->index == pos - 1) { 383 code->code = fmt->mbus_code; 384 if (fmt->pixel_enc == V4L2_PIXEL_ENC_YUV && 385 dir == RKISP1_ISP_SD_SRC) 386 code->flags = 387 V4L2_SUBDEV_MBUS_CODE_CSC_QUANTIZATION; 388 return 0; 389 } 390 } 391 392 return -EINVAL; 393 } 394 395 static int rkisp1_isp_enum_frame_size(struct v4l2_subdev *sd, 396 struct v4l2_subdev_state *sd_state, 397 struct v4l2_subdev_frame_size_enum *fse) 398 { 399 const struct rkisp1_mbus_info *mbus_info; 400 401 if (fse->pad == RKISP1_ISP_PAD_SINK_PARAMS || 402 fse->pad == RKISP1_ISP_PAD_SOURCE_STATS) 403 return -ENOTTY; 404 405 if (fse->index > 0) 406 return -EINVAL; 407 408 mbus_info = rkisp1_mbus_info_get_by_code(fse->code); 409 if (!mbus_info) 410 return -EINVAL; 411 412 if (!(mbus_info->direction & RKISP1_ISP_SD_SINK) && 413 fse->pad == RKISP1_ISP_PAD_SINK_VIDEO) 414 return -EINVAL; 415 416 if (!(mbus_info->direction & RKISP1_ISP_SD_SRC) && 417 fse->pad == RKISP1_ISP_PAD_SOURCE_VIDEO) 418 return -EINVAL; 419 420 fse->min_width = RKISP1_ISP_MIN_WIDTH; 421 fse->max_width = RKISP1_ISP_MAX_WIDTH; 422 fse->min_height = RKISP1_ISP_MIN_HEIGHT; 423 fse->max_height = RKISP1_ISP_MAX_HEIGHT; 424 425 return 0; 426 } 427 428 static int rkisp1_isp_init_config(struct v4l2_subdev *sd, 429 struct v4l2_subdev_state *sd_state) 430 { 431 struct v4l2_mbus_framefmt *sink_fmt, *src_fmt; 432 struct v4l2_rect *sink_crop, *src_crop; 433 434 sink_fmt = v4l2_subdev_get_try_format(sd, sd_state, 435 RKISP1_ISP_PAD_SINK_VIDEO); 436 sink_fmt->width = RKISP1_DEFAULT_WIDTH; 437 sink_fmt->height = RKISP1_DEFAULT_HEIGHT; 438 sink_fmt->field = V4L2_FIELD_NONE; 439 sink_fmt->code = RKISP1_DEF_SINK_PAD_FMT; 440 441 sink_crop = v4l2_subdev_get_try_crop(sd, sd_state, 442 RKISP1_ISP_PAD_SINK_VIDEO); 443 sink_crop->width = RKISP1_DEFAULT_WIDTH; 444 sink_crop->height = RKISP1_DEFAULT_HEIGHT; 445 sink_crop->left = 0; 446 sink_crop->top = 0; 447 448 src_fmt = v4l2_subdev_get_try_format(sd, sd_state, 449 RKISP1_ISP_PAD_SOURCE_VIDEO); 450 *src_fmt = *sink_fmt; 451 src_fmt->code = RKISP1_DEF_SRC_PAD_FMT; 452 453 src_crop = v4l2_subdev_get_try_crop(sd, sd_state, 454 RKISP1_ISP_PAD_SOURCE_VIDEO); 455 *src_crop = *sink_crop; 456 457 sink_fmt = v4l2_subdev_get_try_format(sd, sd_state, 458 RKISP1_ISP_PAD_SINK_PARAMS); 459 src_fmt = v4l2_subdev_get_try_format(sd, sd_state, 460 RKISP1_ISP_PAD_SOURCE_STATS); 461 sink_fmt->width = 0; 462 sink_fmt->height = 0; 463 sink_fmt->field = V4L2_FIELD_NONE; 464 sink_fmt->code = MEDIA_BUS_FMT_METADATA_FIXED; 465 *src_fmt = *sink_fmt; 466 467 return 0; 468 } 469 470 static void rkisp1_isp_set_src_fmt(struct rkisp1_isp *isp, 471 struct v4l2_subdev_state *sd_state, 472 struct v4l2_mbus_framefmt *format, 473 unsigned int which) 474 { 475 const struct rkisp1_mbus_info *mbus_info; 476 struct v4l2_mbus_framefmt *src_fmt; 477 const struct v4l2_rect *src_crop; 478 479 src_fmt = rkisp1_isp_get_pad_fmt(isp, sd_state, 480 RKISP1_ISP_PAD_SOURCE_VIDEO, which); 481 src_crop = rkisp1_isp_get_pad_crop(isp, sd_state, 482 RKISP1_ISP_PAD_SOURCE_VIDEO, which); 483 484 src_fmt->code = format->code; 485 mbus_info = rkisp1_mbus_info_get_by_code(src_fmt->code); 486 if (!mbus_info || !(mbus_info->direction & RKISP1_ISP_SD_SRC)) { 487 src_fmt->code = RKISP1_DEF_SRC_PAD_FMT; 488 mbus_info = rkisp1_mbus_info_get_by_code(src_fmt->code); 489 } 490 if (which == V4L2_SUBDEV_FORMAT_ACTIVE) 491 isp->src_fmt = mbus_info; 492 src_fmt->width = src_crop->width; 493 src_fmt->height = src_crop->height; 494 495 /* 496 * The CSC API is used to allow userspace to force full 497 * quantization on YUV formats. 498 */ 499 if (format->flags & V4L2_MBUS_FRAMEFMT_SET_CSC && 500 format->quantization == V4L2_QUANTIZATION_FULL_RANGE && 501 mbus_info->pixel_enc == V4L2_PIXEL_ENC_YUV) 502 src_fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE; 503 else if (mbus_info->pixel_enc == V4L2_PIXEL_ENC_YUV) 504 src_fmt->quantization = V4L2_QUANTIZATION_LIM_RANGE; 505 else 506 src_fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE; 507 508 *format = *src_fmt; 509 } 510 511 static void rkisp1_isp_set_src_crop(struct rkisp1_isp *isp, 512 struct v4l2_subdev_state *sd_state, 513 struct v4l2_rect *r, unsigned int which) 514 { 515 struct v4l2_mbus_framefmt *src_fmt; 516 const struct v4l2_rect *sink_crop; 517 struct v4l2_rect *src_crop; 518 519 src_crop = rkisp1_isp_get_pad_crop(isp, sd_state, 520 RKISP1_ISP_PAD_SOURCE_VIDEO, 521 which); 522 sink_crop = rkisp1_isp_get_pad_crop(isp, sd_state, 523 RKISP1_ISP_PAD_SINK_VIDEO, 524 which); 525 526 src_crop->left = ALIGN(r->left, 2); 527 src_crop->width = ALIGN(r->width, 2); 528 src_crop->top = r->top; 529 src_crop->height = r->height; 530 rkisp1_sd_adjust_crop_rect(src_crop, sink_crop); 531 532 *r = *src_crop; 533 534 /* Propagate to out format */ 535 src_fmt = rkisp1_isp_get_pad_fmt(isp, sd_state, 536 RKISP1_ISP_PAD_SOURCE_VIDEO, which); 537 rkisp1_isp_set_src_fmt(isp, sd_state, src_fmt, which); 538 } 539 540 static void rkisp1_isp_set_sink_crop(struct rkisp1_isp *isp, 541 struct v4l2_subdev_state *sd_state, 542 struct v4l2_rect *r, unsigned int which) 543 { 544 struct v4l2_rect *sink_crop, *src_crop; 545 const struct v4l2_mbus_framefmt *sink_fmt; 546 547 sink_crop = rkisp1_isp_get_pad_crop(isp, sd_state, 548 RKISP1_ISP_PAD_SINK_VIDEO, 549 which); 550 sink_fmt = rkisp1_isp_get_pad_fmt(isp, sd_state, 551 RKISP1_ISP_PAD_SINK_VIDEO, 552 which); 553 554 sink_crop->left = ALIGN(r->left, 2); 555 sink_crop->width = ALIGN(r->width, 2); 556 sink_crop->top = r->top; 557 sink_crop->height = r->height; 558 rkisp1_sd_adjust_crop(sink_crop, sink_fmt); 559 560 *r = *sink_crop; 561 562 /* Propagate to out crop */ 563 src_crop = rkisp1_isp_get_pad_crop(isp, sd_state, 564 RKISP1_ISP_PAD_SOURCE_VIDEO, which); 565 rkisp1_isp_set_src_crop(isp, sd_state, src_crop, which); 566 } 567 568 static void rkisp1_isp_set_sink_fmt(struct rkisp1_isp *isp, 569 struct v4l2_subdev_state *sd_state, 570 struct v4l2_mbus_framefmt *format, 571 unsigned int which) 572 { 573 const struct rkisp1_mbus_info *mbus_info; 574 struct v4l2_mbus_framefmt *sink_fmt; 575 struct v4l2_rect *sink_crop; 576 577 sink_fmt = rkisp1_isp_get_pad_fmt(isp, sd_state, 578 RKISP1_ISP_PAD_SINK_VIDEO, 579 which); 580 sink_fmt->code = format->code; 581 mbus_info = rkisp1_mbus_info_get_by_code(sink_fmt->code); 582 if (!mbus_info || !(mbus_info->direction & RKISP1_ISP_SD_SINK)) { 583 sink_fmt->code = RKISP1_DEF_SINK_PAD_FMT; 584 mbus_info = rkisp1_mbus_info_get_by_code(sink_fmt->code); 585 } 586 if (which == V4L2_SUBDEV_FORMAT_ACTIVE) 587 isp->sink_fmt = mbus_info; 588 589 sink_fmt->width = clamp_t(u32, format->width, 590 RKISP1_ISP_MIN_WIDTH, 591 RKISP1_ISP_MAX_WIDTH); 592 sink_fmt->height = clamp_t(u32, format->height, 593 RKISP1_ISP_MIN_HEIGHT, 594 RKISP1_ISP_MAX_HEIGHT); 595 596 *format = *sink_fmt; 597 598 /* Propagate to in crop */ 599 sink_crop = rkisp1_isp_get_pad_crop(isp, sd_state, 600 RKISP1_ISP_PAD_SINK_VIDEO, 601 which); 602 rkisp1_isp_set_sink_crop(isp, sd_state, sink_crop, which); 603 } 604 605 static int rkisp1_isp_get_fmt(struct v4l2_subdev *sd, 606 struct v4l2_subdev_state *sd_state, 607 struct v4l2_subdev_format *fmt) 608 { 609 struct rkisp1_isp *isp = to_rkisp1_isp(sd); 610 611 mutex_lock(&isp->ops_lock); 612 fmt->format = *rkisp1_isp_get_pad_fmt(isp, sd_state, fmt->pad, 613 fmt->which); 614 mutex_unlock(&isp->ops_lock); 615 return 0; 616 } 617 618 static int rkisp1_isp_set_fmt(struct v4l2_subdev *sd, 619 struct v4l2_subdev_state *sd_state, 620 struct v4l2_subdev_format *fmt) 621 { 622 struct rkisp1_isp *isp = to_rkisp1_isp(sd); 623 624 mutex_lock(&isp->ops_lock); 625 if (fmt->pad == RKISP1_ISP_PAD_SINK_VIDEO) 626 rkisp1_isp_set_sink_fmt(isp, sd_state, &fmt->format, 627 fmt->which); 628 else if (fmt->pad == RKISP1_ISP_PAD_SOURCE_VIDEO) 629 rkisp1_isp_set_src_fmt(isp, sd_state, &fmt->format, 630 fmt->which); 631 else 632 fmt->format = *rkisp1_isp_get_pad_fmt(isp, sd_state, fmt->pad, 633 fmt->which); 634 635 mutex_unlock(&isp->ops_lock); 636 return 0; 637 } 638 639 static int rkisp1_isp_get_selection(struct v4l2_subdev *sd, 640 struct v4l2_subdev_state *sd_state, 641 struct v4l2_subdev_selection *sel) 642 { 643 struct rkisp1_isp *isp = to_rkisp1_isp(sd); 644 int ret = 0; 645 646 if (sel->pad != RKISP1_ISP_PAD_SOURCE_VIDEO && 647 sel->pad != RKISP1_ISP_PAD_SINK_VIDEO) 648 return -EINVAL; 649 650 mutex_lock(&isp->ops_lock); 651 switch (sel->target) { 652 case V4L2_SEL_TGT_CROP_BOUNDS: 653 if (sel->pad == RKISP1_ISP_PAD_SINK_VIDEO) { 654 struct v4l2_mbus_framefmt *fmt; 655 656 fmt = rkisp1_isp_get_pad_fmt(isp, sd_state, sel->pad, 657 sel->which); 658 sel->r.height = fmt->height; 659 sel->r.width = fmt->width; 660 sel->r.left = 0; 661 sel->r.top = 0; 662 } else { 663 sel->r = *rkisp1_isp_get_pad_crop(isp, sd_state, 664 RKISP1_ISP_PAD_SINK_VIDEO, 665 sel->which); 666 } 667 break; 668 case V4L2_SEL_TGT_CROP: 669 sel->r = *rkisp1_isp_get_pad_crop(isp, sd_state, sel->pad, 670 sel->which); 671 break; 672 default: 673 ret = -EINVAL; 674 } 675 mutex_unlock(&isp->ops_lock); 676 return ret; 677 } 678 679 static int rkisp1_isp_set_selection(struct v4l2_subdev *sd, 680 struct v4l2_subdev_state *sd_state, 681 struct v4l2_subdev_selection *sel) 682 { 683 struct rkisp1_isp *isp = to_rkisp1_isp(sd); 684 int ret = 0; 685 686 if (sel->target != V4L2_SEL_TGT_CROP) 687 return -EINVAL; 688 689 dev_dbg(isp->rkisp1->dev, "%s: pad: %d sel(%d,%d)/%dx%d\n", __func__, 690 sel->pad, sel->r.left, sel->r.top, sel->r.width, sel->r.height); 691 mutex_lock(&isp->ops_lock); 692 if (sel->pad == RKISP1_ISP_PAD_SINK_VIDEO) 693 rkisp1_isp_set_sink_crop(isp, sd_state, &sel->r, sel->which); 694 else if (sel->pad == RKISP1_ISP_PAD_SOURCE_VIDEO) 695 rkisp1_isp_set_src_crop(isp, sd_state, &sel->r, sel->which); 696 else 697 ret = -EINVAL; 698 699 mutex_unlock(&isp->ops_lock); 700 return ret; 701 } 702 703 static int rkisp1_subdev_link_validate(struct media_link *link) 704 { 705 if (link->sink->index == RKISP1_ISP_PAD_SINK_PARAMS) 706 return 0; 707 708 return v4l2_subdev_link_validate(link); 709 } 710 711 static const struct v4l2_subdev_pad_ops rkisp1_isp_pad_ops = { 712 .enum_mbus_code = rkisp1_isp_enum_mbus_code, 713 .enum_frame_size = rkisp1_isp_enum_frame_size, 714 .get_selection = rkisp1_isp_get_selection, 715 .set_selection = rkisp1_isp_set_selection, 716 .init_cfg = rkisp1_isp_init_config, 717 .get_fmt = rkisp1_isp_get_fmt, 718 .set_fmt = rkisp1_isp_set_fmt, 719 .link_validate = v4l2_subdev_link_validate_default, 720 }; 721 722 /* ---------------------------------------------------------------------------- 723 * Stream operations 724 */ 725 726 static int rkisp1_isp_s_stream(struct v4l2_subdev *sd, int enable) 727 { 728 struct rkisp1_isp *isp = to_rkisp1_isp(sd); 729 struct rkisp1_device *rkisp1 = isp->rkisp1; 730 struct media_pad *source_pad; 731 struct media_pad *sink_pad; 732 enum v4l2_mbus_type mbus_type; 733 u32 mbus_flags; 734 int ret; 735 736 if (!enable) { 737 v4l2_subdev_call(rkisp1->source, video, s_stream, false); 738 rkisp1_isp_stop(isp); 739 return 0; 740 } 741 742 sink_pad = &isp->pads[RKISP1_ISP_PAD_SINK_VIDEO]; 743 source_pad = media_pad_remote_pad_unique(sink_pad); 744 if (IS_ERR(source_pad)) { 745 dev_dbg(rkisp1->dev, "Failed to get source for ISP: %ld\n", 746 PTR_ERR(source_pad)); 747 return -EPIPE; 748 } 749 750 rkisp1->source = media_entity_to_v4l2_subdev(source_pad->entity); 751 if (!rkisp1->source) { 752 /* This should really not happen, so is not worth a message. */ 753 return -EPIPE; 754 } 755 756 if (rkisp1->source == &rkisp1->csi.sd) { 757 mbus_type = V4L2_MBUS_CSI2_DPHY; 758 mbus_flags = 0; 759 } else { 760 const struct rkisp1_sensor_async *asd; 761 762 asd = container_of(rkisp1->source->asd, 763 struct rkisp1_sensor_async, asd); 764 765 mbus_type = asd->mbus_type; 766 mbus_flags = asd->mbus_flags; 767 } 768 769 isp->frame_sequence = -1; 770 mutex_lock(&isp->ops_lock); 771 ret = rkisp1_config_cif(isp, mbus_type, mbus_flags); 772 if (ret) 773 goto mutex_unlock; 774 775 rkisp1_isp_start(isp); 776 777 ret = v4l2_subdev_call(rkisp1->source, video, s_stream, true); 778 if (ret) { 779 rkisp1_isp_stop(isp); 780 goto mutex_unlock; 781 } 782 783 mutex_unlock: 784 mutex_unlock(&isp->ops_lock); 785 return ret; 786 } 787 788 static int rkisp1_isp_subs_evt(struct v4l2_subdev *sd, struct v4l2_fh *fh, 789 struct v4l2_event_subscription *sub) 790 { 791 if (sub->type != V4L2_EVENT_FRAME_SYNC) 792 return -EINVAL; 793 794 /* V4L2_EVENT_FRAME_SYNC doesn't require an id, so zero should be set */ 795 if (sub->id != 0) 796 return -EINVAL; 797 798 return v4l2_event_subscribe(fh, sub, 0, NULL); 799 } 800 801 static const struct media_entity_operations rkisp1_isp_media_ops = { 802 .link_validate = rkisp1_subdev_link_validate, 803 }; 804 805 static const struct v4l2_subdev_video_ops rkisp1_isp_video_ops = { 806 .s_stream = rkisp1_isp_s_stream, 807 }; 808 809 static const struct v4l2_subdev_core_ops rkisp1_isp_core_ops = { 810 .subscribe_event = rkisp1_isp_subs_evt, 811 .unsubscribe_event = v4l2_event_subdev_unsubscribe, 812 }; 813 814 static const struct v4l2_subdev_ops rkisp1_isp_ops = { 815 .core = &rkisp1_isp_core_ops, 816 .video = &rkisp1_isp_video_ops, 817 .pad = &rkisp1_isp_pad_ops, 818 }; 819 820 int rkisp1_isp_register(struct rkisp1_device *rkisp1) 821 { 822 struct v4l2_subdev_state state = { 823 .pads = rkisp1->isp.pad_cfg 824 }; 825 struct rkisp1_isp *isp = &rkisp1->isp; 826 struct media_pad *pads = isp->pads; 827 struct v4l2_subdev *sd = &isp->sd; 828 int ret; 829 830 isp->rkisp1 = rkisp1; 831 832 v4l2_subdev_init(sd, &rkisp1_isp_ops); 833 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS; 834 sd->entity.ops = &rkisp1_isp_media_ops; 835 sd->entity.function = MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER; 836 sd->owner = THIS_MODULE; 837 strscpy(sd->name, RKISP1_ISP_DEV_NAME, sizeof(sd->name)); 838 839 pads[RKISP1_ISP_PAD_SINK_VIDEO].flags = MEDIA_PAD_FL_SINK | 840 MEDIA_PAD_FL_MUST_CONNECT; 841 pads[RKISP1_ISP_PAD_SINK_PARAMS].flags = MEDIA_PAD_FL_SINK; 842 pads[RKISP1_ISP_PAD_SOURCE_VIDEO].flags = MEDIA_PAD_FL_SOURCE; 843 pads[RKISP1_ISP_PAD_SOURCE_STATS].flags = MEDIA_PAD_FL_SOURCE; 844 845 isp->sink_fmt = rkisp1_mbus_info_get_by_code(RKISP1_DEF_SINK_PAD_FMT); 846 isp->src_fmt = rkisp1_mbus_info_get_by_code(RKISP1_DEF_SRC_PAD_FMT); 847 848 mutex_init(&isp->ops_lock); 849 ret = media_entity_pads_init(&sd->entity, RKISP1_ISP_PAD_MAX, pads); 850 if (ret) 851 goto error; 852 853 ret = v4l2_device_register_subdev(&rkisp1->v4l2_dev, sd); 854 if (ret) { 855 dev_err(rkisp1->dev, "Failed to register isp subdev\n"); 856 goto error; 857 } 858 859 rkisp1_isp_init_config(sd, &state); 860 861 return 0; 862 863 error: 864 media_entity_cleanup(&sd->entity); 865 mutex_destroy(&isp->ops_lock); 866 isp->sd.v4l2_dev = NULL; 867 return ret; 868 } 869 870 void rkisp1_isp_unregister(struct rkisp1_device *rkisp1) 871 { 872 struct rkisp1_isp *isp = &rkisp1->isp; 873 874 if (!isp->sd.v4l2_dev) 875 return; 876 877 v4l2_device_unregister_subdev(&isp->sd); 878 media_entity_cleanup(&isp->sd.entity); 879 mutex_destroy(&isp->ops_lock); 880 } 881 882 /* ---------------------------------------------------------------------------- 883 * Interrupt handlers 884 */ 885 886 static void rkisp1_isp_queue_event_sof(struct rkisp1_isp *isp) 887 { 888 struct v4l2_event event = { 889 .type = V4L2_EVENT_FRAME_SYNC, 890 }; 891 892 event.u.frame_sync.frame_sequence = isp->frame_sequence; 893 v4l2_event_queue(isp->sd.devnode, &event); 894 } 895 896 irqreturn_t rkisp1_isp_isr(int irq, void *ctx) 897 { 898 struct device *dev = ctx; 899 struct rkisp1_device *rkisp1 = dev_get_drvdata(dev); 900 u32 status, isp_err; 901 902 status = rkisp1_read(rkisp1, RKISP1_CIF_ISP_MIS); 903 if (!status) 904 return IRQ_NONE; 905 906 rkisp1_write(rkisp1, RKISP1_CIF_ISP_ICR, status); 907 908 /* Vertical sync signal, starting generating new frame */ 909 if (status & RKISP1_CIF_ISP_V_START) { 910 rkisp1->isp.frame_sequence++; 911 rkisp1_isp_queue_event_sof(&rkisp1->isp); 912 if (status & RKISP1_CIF_ISP_FRAME) { 913 WARN_ONCE(1, "irq delay is too long, buffers might not be in sync\n"); 914 rkisp1->debug.irq_delay++; 915 } 916 } 917 if (status & RKISP1_CIF_ISP_PIC_SIZE_ERROR) { 918 /* Clear pic_size_error */ 919 isp_err = rkisp1_read(rkisp1, RKISP1_CIF_ISP_ERR); 920 if (isp_err & RKISP1_CIF_ISP_ERR_INFORM_SIZE) 921 rkisp1->debug.inform_size_error++; 922 if (isp_err & RKISP1_CIF_ISP_ERR_IS_SIZE) 923 rkisp1->debug.img_stabilization_size_error++; 924 if (isp_err & RKISP1_CIF_ISP_ERR_OUTFORM_SIZE) 925 rkisp1->debug.outform_size_error++; 926 rkisp1_write(rkisp1, RKISP1_CIF_ISP_ERR_CLR, isp_err); 927 } else if (status & RKISP1_CIF_ISP_DATA_LOSS) { 928 /* keep track of data_loss in debugfs */ 929 rkisp1->debug.data_loss++; 930 } 931 932 if (status & RKISP1_CIF_ISP_FRAME) { 933 u32 isp_ris; 934 935 /* New frame from the sensor received */ 936 isp_ris = rkisp1_read(rkisp1, RKISP1_CIF_ISP_RIS); 937 if (isp_ris & RKISP1_STATS_MEAS_MASK) 938 rkisp1_stats_isr(&rkisp1->stats, isp_ris); 939 /* 940 * Then update changed configs. Some of them involve 941 * lot of register writes. Do those only one per frame. 942 * Do the updates in the order of the processing flow. 943 */ 944 rkisp1_params_isr(rkisp1); 945 } 946 947 return IRQ_HANDLED; 948 } 949