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 *
rkisp1_isp_get_pad_fmt(struct rkisp1_isp * isp,struct v4l2_subdev_state * sd_state,unsigned int pad,u32 which)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 *
rkisp1_isp_get_pad_crop(struct rkisp1_isp * isp,struct v4l2_subdev_state * sd_state,unsigned int pad,u32 which)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 */
rkisp1_config_ism(struct rkisp1_isp * isp)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 */
rkisp1_config_isp(struct rkisp1_isp * isp,enum v4l2_mbus_type mbus_type,u32 mbus_flags)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_SOURCE_VIDEO,
235 V4L2_SUBDEV_FORMAT_ACTIVE);
236 rkisp1_params_pre_configure(&rkisp1->params, sink_fmt->bayer_pat,
237 src_frm->quantization,
238 src_frm->ycbcr_enc);
239 }
240
241 return 0;
242 }
243
244 /* Configure MUX */
rkisp1_config_path(struct rkisp1_isp * isp,enum v4l2_mbus_type mbus_type)245 static void rkisp1_config_path(struct rkisp1_isp *isp,
246 enum v4l2_mbus_type mbus_type)
247 {
248 struct rkisp1_device *rkisp1 = isp->rkisp1;
249 u32 dpcl = rkisp1_read(rkisp1, RKISP1_CIF_VI_DPCL);
250
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;
255
256 rkisp1_write(rkisp1, RKISP1_CIF_VI_DPCL, dpcl);
257 }
258
259 /* Hardware configure Entry */
rkisp1_config_cif(struct rkisp1_isp * isp,enum v4l2_mbus_type mbus_type,u32 mbus_flags)260 static int rkisp1_config_cif(struct rkisp1_isp *isp,
261 enum v4l2_mbus_type mbus_type, u32 mbus_flags)
262 {
263 int ret;
264
265 ret = rkisp1_config_isp(isp, mbus_type, mbus_flags);
266 if (ret)
267 return ret;
268
269 rkisp1_config_path(isp, mbus_type);
270 rkisp1_config_ism(isp);
271
272 return 0;
273 }
274
rkisp1_isp_stop(struct rkisp1_isp * isp)275 static void rkisp1_isp_stop(struct rkisp1_isp *isp)
276 {
277 struct rkisp1_device *rkisp1 = isp->rkisp1;
278 u32 val;
279
280 /*
281 * ISP(mi) stop in mi frame end -> Stop ISP(mipi) ->
282 * Stop ISP(isp) ->wait for ISP isp off
283 */
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);
287
288 rkisp1_write(rkisp1, RKISP1_CIF_MI_IMSC, 0);
289 rkisp1_write(rkisp1, RKISP1_CIF_MI_ICR, ~0);
290
291 /* stop ISP */
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);
296
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);
300
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);
307 }
308
rkisp1_config_clk(struct rkisp1_isp * isp)309 static void rkisp1_config_clk(struct rkisp1_isp *isp)
310 {
311 struct rkisp1_device *rkisp1 = isp->rkisp1;
312
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;
318
319 rkisp1_write(rkisp1, RKISP1_CIF_VI_ICCL, val);
320
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);
328 }
329 }
330
rkisp1_isp_start(struct rkisp1_isp * isp)331 static void rkisp1_isp_start(struct rkisp1_isp *isp)
332 {
333 struct rkisp1_device *rkisp1 = isp->rkisp1;
334 u32 val;
335
336 rkisp1_config_clk(isp);
337
338 /* Activate 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);
344
345 if (isp->src_fmt->pixel_enc != V4L2_PIXEL_ENC_BAYER)
346 rkisp1_params_post_configure(&rkisp1->params);
347 }
348
349 /* ----------------------------------------------------------------------------
350 * Subdev pad operations
351 */
352
to_rkisp1_isp(struct v4l2_subdev * sd)353 static inline struct rkisp1_isp *to_rkisp1_isp(struct v4l2_subdev *sd)
354 {
355 return container_of(sd, struct rkisp1_isp, sd);
356 }
357
rkisp1_isp_enum_mbus_code(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_mbus_code_enum * code)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)
361 {
362 unsigned int i, dir;
363 int pos = 0;
364
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;
369 } else {
370 if (code->index > 0)
371 return -EINVAL;
372 code->code = MEDIA_BUS_FMT_METADATA_FIXED;
373 return 0;
374 }
375
376 for (i = 0; ; i++) {
377 const struct rkisp1_mbus_info *fmt =
378 rkisp1_mbus_info_get_by_index(i);
379
380 if (!fmt)
381 return -EINVAL;
382
383 if (fmt->direction & dir)
384 pos++;
385
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)
390 code->flags =
391 V4L2_SUBDEV_MBUS_CODE_CSC_QUANTIZATION;
392 return 0;
393 }
394 }
395
396 return -EINVAL;
397 }
398
rkisp1_isp_enum_frame_size(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_frame_size_enum * fse)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)
402 {
403 const struct rkisp1_mbus_info *mbus_info;
404
405 if (fse->pad == RKISP1_ISP_PAD_SINK_PARAMS ||
406 fse->pad == RKISP1_ISP_PAD_SOURCE_STATS)
407 return -ENOTTY;
408
409 if (fse->index > 0)
410 return -EINVAL;
411
412 mbus_info = rkisp1_mbus_info_get_by_code(fse->code);
413 if (!mbus_info)
414 return -EINVAL;
415
416 if (!(mbus_info->direction & RKISP1_ISP_SD_SINK) &&
417 fse->pad == RKISP1_ISP_PAD_SINK_VIDEO)
418 return -EINVAL;
419
420 if (!(mbus_info->direction & RKISP1_ISP_SD_SRC) &&
421 fse->pad == RKISP1_ISP_PAD_SOURCE_VIDEO)
422 return -EINVAL;
423
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;
428
429 return 0;
430 }
431
rkisp1_isp_init_config(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state)432 static int rkisp1_isp_init_config(struct v4l2_subdev *sd,
433 struct v4l2_subdev_state *sd_state)
434 {
435 struct v4l2_mbus_framefmt *sink_fmt, *src_fmt;
436 struct v4l2_rect *sink_crop, *src_crop;
437
438 /* Video. */
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;
449
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;
454 sink_crop->left = 0;
455 sink_crop->top = 0;
456
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;
465
466 src_crop = v4l2_subdev_get_try_crop(sd, sd_state,
467 RKISP1_ISP_PAD_SOURCE_VIDEO);
468 *src_crop = *sink_crop;
469
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);
475 sink_fmt->width = 0;
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;
480
481 return 0;
482 }
483
rkisp1_isp_set_src_fmt(struct rkisp1_isp * isp,struct v4l2_subdev_state * sd_state,struct v4l2_mbus_framefmt * format,unsigned int which)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,
487 unsigned int which)
488 {
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;
494 bool set_csc;
495
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);
502
503 /*
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.
507 */
508 sink_info = rkisp1_mbus_info_get_by_code(sink_fmt->code);
509
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);
515 }
516
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;
521 }
522
523 /*
524 * The source width and height must be identical to the source crop
525 * size.
526 */
527 src_fmt->width = src_crop->width;
528 src_fmt->height = src_crop->height;
529
530 /*
531 * Copy the color space for the sink pad. When converting from Bayer to
532 * YUV, default to a limited quantization range.
533 */
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;
537
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;
541 else
542 src_fmt->quantization = sink_fmt->quantization;
543
544 /*
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
549 * modifications.
550 *
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.
559 */
560 set_csc = format->flags & V4L2_MBUS_FRAMEFMT_SET_CSC;
561
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;
570 }
571
572 if (format->quantization != V4L2_QUANTIZATION_DEFAULT)
573 src_fmt->quantization = format->quantization;
574 }
575
576 *format = *src_fmt;
577
578 /*
579 * Restore the SET_CSC flag if it was set to indicate support for the
580 * CSC setting API.
581 */
582 if (set_csc)
583 format->flags |= V4L2_MBUS_FRAMEFMT_SET_CSC;
584
585 /* Store the source format info when setting the active format. */
586 if (which == V4L2_SUBDEV_FORMAT_ACTIVE)
587 isp->src_fmt = src_info;
588 }
589
rkisp1_isp_set_src_crop(struct rkisp1_isp * isp,struct v4l2_subdev_state * sd_state,struct v4l2_rect * r,unsigned int which)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)
593 {
594 struct v4l2_mbus_framefmt *src_fmt;
595 const struct v4l2_rect *sink_crop;
596 struct v4l2_rect *src_crop;
597
598 src_crop = rkisp1_isp_get_pad_crop(isp, sd_state,
599 RKISP1_ISP_PAD_SOURCE_VIDEO,
600 which);
601 sink_crop = rkisp1_isp_get_pad_crop(isp, sd_state,
602 RKISP1_ISP_PAD_SINK_VIDEO,
603 which);
604
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);
610
611 *r = *src_crop;
612
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);
617 }
618
rkisp1_isp_set_sink_crop(struct rkisp1_isp * isp,struct v4l2_subdev_state * sd_state,struct v4l2_rect * r,unsigned int 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)
622 {
623 struct v4l2_rect *sink_crop, *src_crop;
624 const struct v4l2_mbus_framefmt *sink_fmt;
625
626 sink_crop = rkisp1_isp_get_pad_crop(isp, sd_state,
627 RKISP1_ISP_PAD_SINK_VIDEO,
628 which);
629 sink_fmt = rkisp1_isp_get_pad_fmt(isp, sd_state,
630 RKISP1_ISP_PAD_SINK_VIDEO,
631 which);
632
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);
638
639 *r = *sink_crop;
640
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);
645 }
646
rkisp1_isp_set_sink_fmt(struct rkisp1_isp * isp,struct v4l2_subdev_state * sd_state,struct v4l2_mbus_framefmt * format,unsigned int 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,
650 unsigned int which)
651 {
652 const struct rkisp1_mbus_info *mbus_info;
653 struct v4l2_mbus_framefmt *sink_fmt;
654 struct v4l2_rect *sink_crop;
655 bool is_yuv;
656
657 sink_fmt = rkisp1_isp_get_pad_fmt(isp, sd_state,
658 RKISP1_ISP_PAD_SINK_VIDEO,
659 which);
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);
665 }
666 if (which == V4L2_SUBDEV_FORMAT_ACTIVE)
667 isp->sink_fmt = mbus_info;
668
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);
675
676 /*
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
681 * full.
682 */
683 is_yuv = mbus_info->pixel_enc == V4L2_PIXEL_ENC_YUV;
684
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);
690 if (is_yuv) {
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);
696 } else {
697 /*
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.
701 */
702 sink_fmt->ycbcr_enc = V4L2_YCBCR_ENC_601;
703 sink_fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE;
704 }
705
706 *format = *sink_fmt;
707
708 /* Propagate to in crop */
709 sink_crop = rkisp1_isp_get_pad_crop(isp, sd_state,
710 RKISP1_ISP_PAD_SINK_VIDEO,
711 which);
712 rkisp1_isp_set_sink_crop(isp, sd_state, sink_crop, which);
713 }
714
rkisp1_isp_get_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_format * fmt)715 static int rkisp1_isp_get_fmt(struct v4l2_subdev *sd,
716 struct v4l2_subdev_state *sd_state,
717 struct v4l2_subdev_format *fmt)
718 {
719 struct rkisp1_isp *isp = to_rkisp1_isp(sd);
720
721 mutex_lock(&isp->ops_lock);
722 fmt->format = *rkisp1_isp_get_pad_fmt(isp, sd_state, fmt->pad,
723 fmt->which);
724 mutex_unlock(&isp->ops_lock);
725 return 0;
726 }
727
rkisp1_isp_set_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_format * fmt)728 static int rkisp1_isp_set_fmt(struct v4l2_subdev *sd,
729 struct v4l2_subdev_state *sd_state,
730 struct v4l2_subdev_format *fmt)
731 {
732 struct rkisp1_isp *isp = to_rkisp1_isp(sd);
733
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,
737 fmt->which);
738 else if (fmt->pad == RKISP1_ISP_PAD_SOURCE_VIDEO)
739 rkisp1_isp_set_src_fmt(isp, sd_state, &fmt->format,
740 fmt->which);
741 else
742 fmt->format = *rkisp1_isp_get_pad_fmt(isp, sd_state, fmt->pad,
743 fmt->which);
744
745 mutex_unlock(&isp->ops_lock);
746 return 0;
747 }
748
rkisp1_isp_get_selection(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_selection * sel)749 static int rkisp1_isp_get_selection(struct v4l2_subdev *sd,
750 struct v4l2_subdev_state *sd_state,
751 struct v4l2_subdev_selection *sel)
752 {
753 struct rkisp1_isp *isp = to_rkisp1_isp(sd);
754 int ret = 0;
755
756 if (sel->pad != RKISP1_ISP_PAD_SOURCE_VIDEO &&
757 sel->pad != RKISP1_ISP_PAD_SINK_VIDEO)
758 return -EINVAL;
759
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;
765
766 fmt = rkisp1_isp_get_pad_fmt(isp, sd_state, sel->pad,
767 sel->which);
768 sel->r.height = fmt->height;
769 sel->r.width = fmt->width;
770 sel->r.left = 0;
771 sel->r.top = 0;
772 } else {
773 sel->r = *rkisp1_isp_get_pad_crop(isp, sd_state,
774 RKISP1_ISP_PAD_SINK_VIDEO,
775 sel->which);
776 }
777 break;
778 case V4L2_SEL_TGT_CROP:
779 sel->r = *rkisp1_isp_get_pad_crop(isp, sd_state, sel->pad,
780 sel->which);
781 break;
782 default:
783 ret = -EINVAL;
784 }
785 mutex_unlock(&isp->ops_lock);
786 return ret;
787 }
788
rkisp1_isp_set_selection(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_selection * sel)789 static int rkisp1_isp_set_selection(struct v4l2_subdev *sd,
790 struct v4l2_subdev_state *sd_state,
791 struct v4l2_subdev_selection *sel)
792 {
793 struct rkisp1_isp *isp = to_rkisp1_isp(sd);
794 int ret = 0;
795
796 if (sel->target != V4L2_SEL_TGT_CROP)
797 return -EINVAL;
798
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);
806 else
807 ret = -EINVAL;
808
809 mutex_unlock(&isp->ops_lock);
810 return ret;
811 }
812
rkisp1_subdev_link_validate(struct media_link * link)813 static int rkisp1_subdev_link_validate(struct media_link *link)
814 {
815 if (link->sink->index == RKISP1_ISP_PAD_SINK_PARAMS)
816 return 0;
817
818 return v4l2_subdev_link_validate(link);
819 }
820
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,
830 };
831
832 /* ----------------------------------------------------------------------------
833 * Stream operations
834 */
835
rkisp1_isp_s_stream(struct v4l2_subdev * sd,int enable)836 static int rkisp1_isp_s_stream(struct v4l2_subdev *sd, int enable)
837 {
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;
843 u32 mbus_flags;
844 int ret;
845
846 if (!enable) {
847 v4l2_subdev_call(rkisp1->source, video, s_stream, false);
848 rkisp1_isp_stop(isp);
849 return 0;
850 }
851
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));
857 return -EPIPE;
858 }
859
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. */
863 return -EPIPE;
864 }
865
866 if (rkisp1->source == &rkisp1->csi.sd) {
867 mbus_type = V4L2_MBUS_CSI2_DPHY;
868 mbus_flags = 0;
869 } else {
870 const struct rkisp1_sensor_async *asd;
871
872 asd = container_of(rkisp1->source->asd,
873 struct rkisp1_sensor_async, asd);
874
875 mbus_type = asd->mbus_type;
876 mbus_flags = asd->mbus_flags;
877 }
878
879 isp->frame_sequence = -1;
880 mutex_lock(&isp->ops_lock);
881 ret = rkisp1_config_cif(isp, mbus_type, mbus_flags);
882 if (ret)
883 goto mutex_unlock;
884
885 rkisp1_isp_start(isp);
886
887 ret = v4l2_subdev_call(rkisp1->source, video, s_stream, true);
888 if (ret) {
889 rkisp1_isp_stop(isp);
890 goto mutex_unlock;
891 }
892
893 mutex_unlock:
894 mutex_unlock(&isp->ops_lock);
895 return ret;
896 }
897
rkisp1_isp_subs_evt(struct v4l2_subdev * sd,struct v4l2_fh * fh,struct v4l2_event_subscription * sub)898 static int rkisp1_isp_subs_evt(struct v4l2_subdev *sd, struct v4l2_fh *fh,
899 struct v4l2_event_subscription *sub)
900 {
901 if (sub->type != V4L2_EVENT_FRAME_SYNC)
902 return -EINVAL;
903
904 /* V4L2_EVENT_FRAME_SYNC doesn't require an id, so zero should be set */
905 if (sub->id != 0)
906 return -EINVAL;
907
908 return v4l2_event_subscribe(fh, sub, 0, NULL);
909 }
910
911 static const struct media_entity_operations rkisp1_isp_media_ops = {
912 .link_validate = rkisp1_subdev_link_validate,
913 };
914
915 static const struct v4l2_subdev_video_ops rkisp1_isp_video_ops = {
916 .s_stream = rkisp1_isp_s_stream,
917 };
918
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,
922 };
923
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,
928 };
929
rkisp1_isp_register(struct rkisp1_device * rkisp1)930 int rkisp1_isp_register(struct rkisp1_device *rkisp1)
931 {
932 struct v4l2_subdev_state state = {
933 .pads = rkisp1->isp.pad_cfg
934 };
935 struct rkisp1_isp *isp = &rkisp1->isp;
936 struct media_pad *pads = isp->pads;
937 struct v4l2_subdev *sd = &isp->sd;
938 int ret;
939
940 isp->rkisp1 = rkisp1;
941
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));
948
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;
954
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);
957
958 mutex_init(&isp->ops_lock);
959 ret = media_entity_pads_init(&sd->entity, RKISP1_ISP_PAD_MAX, pads);
960 if (ret)
961 goto error;
962
963 ret = v4l2_device_register_subdev(&rkisp1->v4l2_dev, sd);
964 if (ret) {
965 dev_err(rkisp1->dev, "Failed to register isp subdev\n");
966 goto error;
967 }
968
969 rkisp1_isp_init_config(sd, &state);
970
971 return 0;
972
973 error:
974 media_entity_cleanup(&sd->entity);
975 mutex_destroy(&isp->ops_lock);
976 isp->sd.v4l2_dev = NULL;
977 return ret;
978 }
979
rkisp1_isp_unregister(struct rkisp1_device * rkisp1)980 void rkisp1_isp_unregister(struct rkisp1_device *rkisp1)
981 {
982 struct rkisp1_isp *isp = &rkisp1->isp;
983
984 if (!isp->sd.v4l2_dev)
985 return;
986
987 v4l2_device_unregister_subdev(&isp->sd);
988 media_entity_cleanup(&isp->sd.entity);
989 mutex_destroy(&isp->ops_lock);
990 }
991
992 /* ----------------------------------------------------------------------------
993 * Interrupt handlers
994 */
995
rkisp1_isp_queue_event_sof(struct rkisp1_isp * isp)996 static void rkisp1_isp_queue_event_sof(struct rkisp1_isp *isp)
997 {
998 struct v4l2_event event = {
999 .type = V4L2_EVENT_FRAME_SYNC,
1000 };
1001
1002 event.u.frame_sync.frame_sequence = isp->frame_sequence;
1003 v4l2_event_queue(isp->sd.devnode, &event);
1004 }
1005
rkisp1_isp_isr(int irq,void * ctx)1006 irqreturn_t rkisp1_isp_isr(int irq, void *ctx)
1007 {
1008 struct device *dev = ctx;
1009 struct rkisp1_device *rkisp1 = dev_get_drvdata(dev);
1010 u32 status, isp_err;
1011
1012 status = rkisp1_read(rkisp1, RKISP1_CIF_ISP_MIS);
1013 if (!status)
1014 return IRQ_NONE;
1015
1016 rkisp1_write(rkisp1, RKISP1_CIF_ISP_ICR, status);
1017
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++;
1025 }
1026 }
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++;
1040 }
1041
1042 if (status & RKISP1_CIF_ISP_FRAME) {
1043 u32 isp_ris;
1044
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);
1049 /*
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.
1053 */
1054 rkisp1_params_isr(rkisp1);
1055 }
1056
1057 return IRQ_HANDLED;
1058 }
1059