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 
285 	/* Mask MI and ISP interrupts */
286 	rkisp1_write(rkisp1, RKISP1_CIF_ISP_IMSC, 0);
287 	rkisp1_write(rkisp1, RKISP1_CIF_MI_IMSC, 0);
288 
289 	/* Flush posted writes */
290 	rkisp1_read(rkisp1, RKISP1_CIF_MI_IMSC);
291 
292 	/*
293 	 * Wait until the IRQ handler has ended. The IRQ handler may get called
294 	 * even after this, but it will return immediately as the MI and ISP
295 	 * interrupts have been masked.
296 	 */
297 	synchronize_irq(rkisp1->irqs[RKISP1_IRQ_ISP]);
298 	if (rkisp1->irqs[RKISP1_IRQ_ISP] != rkisp1->irqs[RKISP1_IRQ_MI])
299 		synchronize_irq(rkisp1->irqs[RKISP1_IRQ_MI]);
300 
301 	/* Clear MI and ISP interrupt status */
302 	rkisp1_write(rkisp1, RKISP1_CIF_ISP_ICR, ~0);
303 	rkisp1_write(rkisp1, RKISP1_CIF_MI_ICR, ~0);
304 
305 	/* stop ISP */
306 	val = rkisp1_read(rkisp1, RKISP1_CIF_ISP_CTRL);
307 	val &= ~(RKISP1_CIF_ISP_CTRL_ISP_INFORM_ENABLE |
308 		 RKISP1_CIF_ISP_CTRL_ISP_ENABLE);
309 	rkisp1_write(rkisp1, RKISP1_CIF_ISP_CTRL, val);
310 
311 	val = rkisp1_read(rkisp1,	RKISP1_CIF_ISP_CTRL);
312 	rkisp1_write(rkisp1, RKISP1_CIF_ISP_CTRL,
313 		     val | RKISP1_CIF_ISP_CTRL_ISP_CFG_UPD);
314 
315 	readx_poll_timeout(readl, rkisp1->base_addr + RKISP1_CIF_ISP_RIS,
316 			   val, val & RKISP1_CIF_ISP_OFF, 20, 100);
317 	rkisp1_write(rkisp1, RKISP1_CIF_VI_IRCL,
318 		     RKISP1_CIF_VI_IRCL_MIPI_SW_RST |
319 		     RKISP1_CIF_VI_IRCL_ISP_SW_RST);
320 	rkisp1_write(rkisp1, RKISP1_CIF_VI_IRCL, 0x0);
321 }
322 
rkisp1_config_clk(struct rkisp1_isp * isp)323 static void rkisp1_config_clk(struct rkisp1_isp *isp)
324 {
325 	struct rkisp1_device *rkisp1 = isp->rkisp1;
326 
327 	u32 val = RKISP1_CIF_VI_ICCL_ISP_CLK | RKISP1_CIF_VI_ICCL_CP_CLK |
328 		  RKISP1_CIF_VI_ICCL_MRSZ_CLK | RKISP1_CIF_VI_ICCL_SRSZ_CLK |
329 		  RKISP1_CIF_VI_ICCL_JPEG_CLK | RKISP1_CIF_VI_ICCL_MI_CLK |
330 		  RKISP1_CIF_VI_ICCL_IE_CLK | RKISP1_CIF_VI_ICCL_MIPI_CLK |
331 		  RKISP1_CIF_VI_ICCL_DCROP_CLK;
332 
333 	rkisp1_write(rkisp1, RKISP1_CIF_VI_ICCL, val);
334 
335 	/* ensure sp and mp can run at the same time in V12 */
336 	if (rkisp1->info->isp_ver == RKISP1_V12) {
337 		val = RKISP1_CIF_CLK_CTRL_MI_Y12 | RKISP1_CIF_CLK_CTRL_MI_SP |
338 		      RKISP1_CIF_CLK_CTRL_MI_RAW0 | RKISP1_CIF_CLK_CTRL_MI_RAW1 |
339 		      RKISP1_CIF_CLK_CTRL_MI_READ | RKISP1_CIF_CLK_CTRL_MI_RAWRD |
340 		      RKISP1_CIF_CLK_CTRL_CP | RKISP1_CIF_CLK_CTRL_IE;
341 		rkisp1_write(rkisp1, RKISP1_CIF_VI_ISP_CLK_CTRL_V12, val);
342 	}
343 }
344 
rkisp1_isp_start(struct rkisp1_isp * isp)345 static void rkisp1_isp_start(struct rkisp1_isp *isp)
346 {
347 	struct rkisp1_device *rkisp1 = isp->rkisp1;
348 	u32 val;
349 
350 	rkisp1_config_clk(isp);
351 
352 	/* Activate ISP */
353 	val = rkisp1_read(rkisp1, RKISP1_CIF_ISP_CTRL);
354 	val |= RKISP1_CIF_ISP_CTRL_ISP_CFG_UPD |
355 	       RKISP1_CIF_ISP_CTRL_ISP_ENABLE |
356 	       RKISP1_CIF_ISP_CTRL_ISP_INFORM_ENABLE;
357 	rkisp1_write(rkisp1, RKISP1_CIF_ISP_CTRL, val);
358 
359 	if (isp->src_fmt->pixel_enc != V4L2_PIXEL_ENC_BAYER)
360 		rkisp1_params_post_configure(&rkisp1->params);
361 }
362 
363 /* ----------------------------------------------------------------------------
364  * Subdev pad operations
365  */
366 
to_rkisp1_isp(struct v4l2_subdev * sd)367 static inline struct rkisp1_isp *to_rkisp1_isp(struct v4l2_subdev *sd)
368 {
369 	return container_of(sd, struct rkisp1_isp, sd);
370 }
371 
rkisp1_isp_enum_mbus_code(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_mbus_code_enum * code)372 static int rkisp1_isp_enum_mbus_code(struct v4l2_subdev *sd,
373 				     struct v4l2_subdev_state *sd_state,
374 				     struct v4l2_subdev_mbus_code_enum *code)
375 {
376 	unsigned int i, dir;
377 	int pos = 0;
378 
379 	if (code->pad == RKISP1_ISP_PAD_SINK_VIDEO) {
380 		dir = RKISP1_ISP_SD_SINK;
381 	} else if (code->pad == RKISP1_ISP_PAD_SOURCE_VIDEO) {
382 		dir = RKISP1_ISP_SD_SRC;
383 	} else {
384 		if (code->index > 0)
385 			return -EINVAL;
386 		code->code = MEDIA_BUS_FMT_METADATA_FIXED;
387 		return 0;
388 	}
389 
390 	for (i = 0; ; i++) {
391 		const struct rkisp1_mbus_info *fmt =
392 			rkisp1_mbus_info_get_by_index(i);
393 
394 		if (!fmt)
395 			return -EINVAL;
396 
397 		if (fmt->direction & dir)
398 			pos++;
399 
400 		if (code->index == pos - 1) {
401 			code->code = fmt->mbus_code;
402 			if (fmt->pixel_enc == V4L2_PIXEL_ENC_YUV &&
403 			    dir == RKISP1_ISP_SD_SRC)
404 				code->flags =
405 					V4L2_SUBDEV_MBUS_CODE_CSC_QUANTIZATION;
406 			return 0;
407 		}
408 	}
409 
410 	return -EINVAL;
411 }
412 
rkisp1_isp_enum_frame_size(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_frame_size_enum * fse)413 static int rkisp1_isp_enum_frame_size(struct v4l2_subdev *sd,
414 				      struct v4l2_subdev_state *sd_state,
415 				      struct v4l2_subdev_frame_size_enum *fse)
416 {
417 	const struct rkisp1_mbus_info *mbus_info;
418 
419 	if (fse->pad == RKISP1_ISP_PAD_SINK_PARAMS ||
420 	    fse->pad == RKISP1_ISP_PAD_SOURCE_STATS)
421 		return -ENOTTY;
422 
423 	if (fse->index > 0)
424 		return -EINVAL;
425 
426 	mbus_info = rkisp1_mbus_info_get_by_code(fse->code);
427 	if (!mbus_info)
428 		return -EINVAL;
429 
430 	if (!(mbus_info->direction & RKISP1_ISP_SD_SINK) &&
431 	    fse->pad == RKISP1_ISP_PAD_SINK_VIDEO)
432 		return -EINVAL;
433 
434 	if (!(mbus_info->direction & RKISP1_ISP_SD_SRC) &&
435 	    fse->pad == RKISP1_ISP_PAD_SOURCE_VIDEO)
436 		return -EINVAL;
437 
438 	fse->min_width = RKISP1_ISP_MIN_WIDTH;
439 	fse->max_width = RKISP1_ISP_MAX_WIDTH;
440 	fse->min_height = RKISP1_ISP_MIN_HEIGHT;
441 	fse->max_height = RKISP1_ISP_MAX_HEIGHT;
442 
443 	return 0;
444 }
445 
rkisp1_isp_init_config(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state)446 static int rkisp1_isp_init_config(struct v4l2_subdev *sd,
447 				  struct v4l2_subdev_state *sd_state)
448 {
449 	struct v4l2_mbus_framefmt *sink_fmt, *src_fmt;
450 	struct v4l2_rect *sink_crop, *src_crop;
451 
452 	/* Video. */
453 	sink_fmt = v4l2_subdev_get_try_format(sd, sd_state,
454 					      RKISP1_ISP_PAD_SINK_VIDEO);
455 	sink_fmt->width = RKISP1_DEFAULT_WIDTH;
456 	sink_fmt->height = RKISP1_DEFAULT_HEIGHT;
457 	sink_fmt->field = V4L2_FIELD_NONE;
458 	sink_fmt->code = RKISP1_DEF_SINK_PAD_FMT;
459 	sink_fmt->colorspace = V4L2_COLORSPACE_RAW;
460 	sink_fmt->xfer_func = V4L2_XFER_FUNC_NONE;
461 	sink_fmt->ycbcr_enc = V4L2_YCBCR_ENC_601;
462 	sink_fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE;
463 
464 	sink_crop = v4l2_subdev_get_try_crop(sd, sd_state,
465 					     RKISP1_ISP_PAD_SINK_VIDEO);
466 	sink_crop->width = RKISP1_DEFAULT_WIDTH;
467 	sink_crop->height = RKISP1_DEFAULT_HEIGHT;
468 	sink_crop->left = 0;
469 	sink_crop->top = 0;
470 
471 	src_fmt = v4l2_subdev_get_try_format(sd, sd_state,
472 					     RKISP1_ISP_PAD_SOURCE_VIDEO);
473 	*src_fmt = *sink_fmt;
474 	src_fmt->code = RKISP1_DEF_SRC_PAD_FMT;
475 	src_fmt->colorspace = V4L2_COLORSPACE_SRGB;
476 	src_fmt->xfer_func = V4L2_XFER_FUNC_SRGB;
477 	src_fmt->ycbcr_enc = V4L2_YCBCR_ENC_601;
478 	src_fmt->quantization = V4L2_QUANTIZATION_LIM_RANGE;
479 
480 	src_crop = v4l2_subdev_get_try_crop(sd, sd_state,
481 					    RKISP1_ISP_PAD_SOURCE_VIDEO);
482 	*src_crop = *sink_crop;
483 
484 	/* Parameters and statistics. */
485 	sink_fmt = v4l2_subdev_get_try_format(sd, sd_state,
486 					      RKISP1_ISP_PAD_SINK_PARAMS);
487 	src_fmt = v4l2_subdev_get_try_format(sd, sd_state,
488 					     RKISP1_ISP_PAD_SOURCE_STATS);
489 	sink_fmt->width = 0;
490 	sink_fmt->height = 0;
491 	sink_fmt->field = V4L2_FIELD_NONE;
492 	sink_fmt->code = MEDIA_BUS_FMT_METADATA_FIXED;
493 	*src_fmt = *sink_fmt;
494 
495 	return 0;
496 }
497 
rkisp1_isp_set_src_fmt(struct rkisp1_isp * isp,struct v4l2_subdev_state * sd_state,struct v4l2_mbus_framefmt * format,unsigned int which)498 static void rkisp1_isp_set_src_fmt(struct rkisp1_isp *isp,
499 				   struct v4l2_subdev_state *sd_state,
500 				   struct v4l2_mbus_framefmt *format,
501 				   unsigned int which)
502 {
503 	const struct rkisp1_mbus_info *sink_info;
504 	const struct rkisp1_mbus_info *src_info;
505 	struct v4l2_mbus_framefmt *sink_fmt;
506 	struct v4l2_mbus_framefmt *src_fmt;
507 	const struct v4l2_rect *src_crop;
508 	bool set_csc;
509 
510 	sink_fmt = rkisp1_isp_get_pad_fmt(isp, sd_state,
511 					  RKISP1_ISP_PAD_SINK_VIDEO, which);
512 	src_fmt = rkisp1_isp_get_pad_fmt(isp, sd_state,
513 					 RKISP1_ISP_PAD_SOURCE_VIDEO, which);
514 	src_crop = rkisp1_isp_get_pad_crop(isp, sd_state,
515 					   RKISP1_ISP_PAD_SOURCE_VIDEO, which);
516 
517 	/*
518 	 * Media bus code. The ISP can operate in pass-through mode (Bayer in,
519 	 * Bayer out or YUV in, YUV out) or process Bayer data to YUV, but
520 	 * can't convert from YUV to Bayer.
521 	 */
522 	sink_info = rkisp1_mbus_info_get_by_code(sink_fmt->code);
523 
524 	src_fmt->code = format->code;
525 	src_info = rkisp1_mbus_info_get_by_code(src_fmt->code);
526 	if (!src_info || !(src_info->direction & RKISP1_ISP_SD_SRC)) {
527 		src_fmt->code = RKISP1_DEF_SRC_PAD_FMT;
528 		src_info = rkisp1_mbus_info_get_by_code(src_fmt->code);
529 	}
530 
531 	if (sink_info->pixel_enc == V4L2_PIXEL_ENC_YUV &&
532 	    src_info->pixel_enc == V4L2_PIXEL_ENC_BAYER) {
533 		src_fmt->code = sink_fmt->code;
534 		src_info = sink_info;
535 	}
536 
537 	/*
538 	 * The source width and height must be identical to the source crop
539 	 * size.
540 	 */
541 	src_fmt->width  = src_crop->width;
542 	src_fmt->height = src_crop->height;
543 
544 	/*
545 	 * Copy the color space for the sink pad. When converting from Bayer to
546 	 * YUV, default to a limited quantization range.
547 	 */
548 	src_fmt->colorspace = sink_fmt->colorspace;
549 	src_fmt->xfer_func = sink_fmt->xfer_func;
550 	src_fmt->ycbcr_enc = sink_fmt->ycbcr_enc;
551 
552 	if (sink_info->pixel_enc == V4L2_PIXEL_ENC_BAYER &&
553 	    src_info->pixel_enc == V4L2_PIXEL_ENC_YUV)
554 		src_fmt->quantization = V4L2_QUANTIZATION_LIM_RANGE;
555 	else
556 		src_fmt->quantization = sink_fmt->quantization;
557 
558 	/*
559 	 * Allow setting the source color space fields when the SET_CSC flag is
560 	 * set and the source format is YUV. If the sink format is YUV, don't
561 	 * set the color primaries, transfer function or YCbCr encoding as the
562 	 * ISP is bypassed in that case and passes YUV data through without
563 	 * modifications.
564 	 *
565 	 * The color primaries and transfer function are configured through the
566 	 * cross-talk matrix and tone curve respectively. Settings for those
567 	 * hardware blocks are conveyed through the ISP parameters buffer, as
568 	 * they need to combine color space information with other image tuning
569 	 * characteristics and can't thus be computed by the kernel based on the
570 	 * color space. The source pad colorspace and xfer_func fields are thus
571 	 * ignored by the driver, but can be set by userspace to propagate
572 	 * accurate color space information down the pipeline.
573 	 */
574 	set_csc = format->flags & V4L2_MBUS_FRAMEFMT_SET_CSC;
575 
576 	if (set_csc && src_info->pixel_enc == V4L2_PIXEL_ENC_YUV) {
577 		if (sink_info->pixel_enc == V4L2_PIXEL_ENC_BAYER) {
578 			if (format->colorspace != V4L2_COLORSPACE_DEFAULT)
579 				src_fmt->colorspace = format->colorspace;
580 			if (format->xfer_func != V4L2_XFER_FUNC_DEFAULT)
581 				src_fmt->xfer_func = format->xfer_func;
582 			if (format->ycbcr_enc != V4L2_YCBCR_ENC_DEFAULT)
583 				src_fmt->ycbcr_enc = format->ycbcr_enc;
584 		}
585 
586 		if (format->quantization != V4L2_QUANTIZATION_DEFAULT)
587 			src_fmt->quantization = format->quantization;
588 	}
589 
590 	*format = *src_fmt;
591 
592 	/*
593 	 * Restore the SET_CSC flag if it was set to indicate support for the
594 	 * CSC setting API.
595 	 */
596 	if (set_csc)
597 		format->flags |= V4L2_MBUS_FRAMEFMT_SET_CSC;
598 
599 	/* Store the source format info when setting the active format. */
600 	if (which == V4L2_SUBDEV_FORMAT_ACTIVE)
601 		isp->src_fmt = src_info;
602 }
603 
rkisp1_isp_set_src_crop(struct rkisp1_isp * isp,struct v4l2_subdev_state * sd_state,struct v4l2_rect * r,unsigned int which)604 static void rkisp1_isp_set_src_crop(struct rkisp1_isp *isp,
605 				    struct v4l2_subdev_state *sd_state,
606 				    struct v4l2_rect *r, unsigned int which)
607 {
608 	struct v4l2_mbus_framefmt *src_fmt;
609 	const struct v4l2_rect *sink_crop;
610 	struct v4l2_rect *src_crop;
611 
612 	src_crop = rkisp1_isp_get_pad_crop(isp, sd_state,
613 					   RKISP1_ISP_PAD_SOURCE_VIDEO,
614 					   which);
615 	sink_crop = rkisp1_isp_get_pad_crop(isp, sd_state,
616 					    RKISP1_ISP_PAD_SINK_VIDEO,
617 					    which);
618 
619 	src_crop->left = ALIGN(r->left, 2);
620 	src_crop->width = ALIGN(r->width, 2);
621 	src_crop->top = r->top;
622 	src_crop->height = r->height;
623 	rkisp1_sd_adjust_crop_rect(src_crop, sink_crop);
624 
625 	*r = *src_crop;
626 
627 	/* Propagate to out format */
628 	src_fmt = rkisp1_isp_get_pad_fmt(isp, sd_state,
629 					 RKISP1_ISP_PAD_SOURCE_VIDEO, which);
630 	rkisp1_isp_set_src_fmt(isp, sd_state, src_fmt, which);
631 }
632 
rkisp1_isp_set_sink_crop(struct rkisp1_isp * isp,struct v4l2_subdev_state * sd_state,struct v4l2_rect * r,unsigned int which)633 static void rkisp1_isp_set_sink_crop(struct rkisp1_isp *isp,
634 				     struct v4l2_subdev_state *sd_state,
635 				     struct v4l2_rect *r, unsigned int which)
636 {
637 	struct v4l2_rect *sink_crop, *src_crop;
638 	const struct v4l2_mbus_framefmt *sink_fmt;
639 
640 	sink_crop = rkisp1_isp_get_pad_crop(isp, sd_state,
641 					    RKISP1_ISP_PAD_SINK_VIDEO,
642 					    which);
643 	sink_fmt = rkisp1_isp_get_pad_fmt(isp, sd_state,
644 					  RKISP1_ISP_PAD_SINK_VIDEO,
645 					  which);
646 
647 	sink_crop->left = ALIGN(r->left, 2);
648 	sink_crop->width = ALIGN(r->width, 2);
649 	sink_crop->top = r->top;
650 	sink_crop->height = r->height;
651 	rkisp1_sd_adjust_crop(sink_crop, sink_fmt);
652 
653 	*r = *sink_crop;
654 
655 	/* Propagate to out crop */
656 	src_crop = rkisp1_isp_get_pad_crop(isp, sd_state,
657 					   RKISP1_ISP_PAD_SOURCE_VIDEO, which);
658 	rkisp1_isp_set_src_crop(isp, sd_state, src_crop, which);
659 }
660 
rkisp1_isp_set_sink_fmt(struct rkisp1_isp * isp,struct v4l2_subdev_state * sd_state,struct v4l2_mbus_framefmt * format,unsigned int which)661 static void rkisp1_isp_set_sink_fmt(struct rkisp1_isp *isp,
662 				    struct v4l2_subdev_state *sd_state,
663 				    struct v4l2_mbus_framefmt *format,
664 				    unsigned int which)
665 {
666 	const struct rkisp1_mbus_info *mbus_info;
667 	struct v4l2_mbus_framefmt *sink_fmt;
668 	struct v4l2_rect *sink_crop;
669 	bool is_yuv;
670 
671 	sink_fmt = rkisp1_isp_get_pad_fmt(isp, sd_state,
672 					  RKISP1_ISP_PAD_SINK_VIDEO,
673 					  which);
674 	sink_fmt->code = format->code;
675 	mbus_info = rkisp1_mbus_info_get_by_code(sink_fmt->code);
676 	if (!mbus_info || !(mbus_info->direction & RKISP1_ISP_SD_SINK)) {
677 		sink_fmt->code = RKISP1_DEF_SINK_PAD_FMT;
678 		mbus_info = rkisp1_mbus_info_get_by_code(sink_fmt->code);
679 	}
680 	if (which == V4L2_SUBDEV_FORMAT_ACTIVE)
681 		isp->sink_fmt = mbus_info;
682 
683 	sink_fmt->width = clamp_t(u32, format->width,
684 				  RKISP1_ISP_MIN_WIDTH,
685 				  RKISP1_ISP_MAX_WIDTH);
686 	sink_fmt->height = clamp_t(u32, format->height,
687 				   RKISP1_ISP_MIN_HEIGHT,
688 				   RKISP1_ISP_MAX_HEIGHT);
689 
690 	/*
691 	 * Adjust the color space fields. Accept any color primaries and
692 	 * transfer function for both YUV and Bayer. For YUV any YCbCr encoding
693 	 * and quantization range is also accepted. For Bayer formats, the YCbCr
694 	 * encoding isn't applicable, and the quantization range can only be
695 	 * full.
696 	 */
697 	is_yuv = mbus_info->pixel_enc == V4L2_PIXEL_ENC_YUV;
698 
699 	sink_fmt->colorspace = format->colorspace ? :
700 			       (is_yuv ? V4L2_COLORSPACE_SRGB :
701 				V4L2_COLORSPACE_RAW);
702 	sink_fmt->xfer_func = format->xfer_func ? :
703 			      V4L2_MAP_XFER_FUNC_DEFAULT(sink_fmt->colorspace);
704 	if (is_yuv) {
705 		sink_fmt->ycbcr_enc = format->ycbcr_enc ? :
706 			V4L2_MAP_YCBCR_ENC_DEFAULT(sink_fmt->colorspace);
707 		sink_fmt->quantization = format->quantization ? :
708 			V4L2_MAP_QUANTIZATION_DEFAULT(false, sink_fmt->colorspace,
709 						      sink_fmt->ycbcr_enc);
710 	} else {
711 		/*
712 		 * The YCbCr encoding isn't applicable for non-YUV formats, but
713 		 * V4L2 has no "no encoding" value. Hardcode it to Rec. 601, it
714 		 * should be ignored by userspace.
715 		 */
716 		sink_fmt->ycbcr_enc = V4L2_YCBCR_ENC_601;
717 		sink_fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE;
718 	}
719 
720 	*format = *sink_fmt;
721 
722 	/* Propagate to in crop */
723 	sink_crop = rkisp1_isp_get_pad_crop(isp, sd_state,
724 					    RKISP1_ISP_PAD_SINK_VIDEO,
725 					    which);
726 	rkisp1_isp_set_sink_crop(isp, sd_state, sink_crop, which);
727 }
728 
rkisp1_isp_get_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_format * fmt)729 static int rkisp1_isp_get_fmt(struct v4l2_subdev *sd,
730 			      struct v4l2_subdev_state *sd_state,
731 			      struct v4l2_subdev_format *fmt)
732 {
733 	struct rkisp1_isp *isp = to_rkisp1_isp(sd);
734 
735 	mutex_lock(&isp->ops_lock);
736 	fmt->format = *rkisp1_isp_get_pad_fmt(isp, sd_state, fmt->pad,
737 					      fmt->which);
738 	mutex_unlock(&isp->ops_lock);
739 	return 0;
740 }
741 
rkisp1_isp_set_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_format * fmt)742 static int rkisp1_isp_set_fmt(struct v4l2_subdev *sd,
743 			      struct v4l2_subdev_state *sd_state,
744 			      struct v4l2_subdev_format *fmt)
745 {
746 	struct rkisp1_isp *isp = to_rkisp1_isp(sd);
747 
748 	mutex_lock(&isp->ops_lock);
749 	if (fmt->pad == RKISP1_ISP_PAD_SINK_VIDEO)
750 		rkisp1_isp_set_sink_fmt(isp, sd_state, &fmt->format,
751 					fmt->which);
752 	else if (fmt->pad == RKISP1_ISP_PAD_SOURCE_VIDEO)
753 		rkisp1_isp_set_src_fmt(isp, sd_state, &fmt->format,
754 				       fmt->which);
755 	else
756 		fmt->format = *rkisp1_isp_get_pad_fmt(isp, sd_state, fmt->pad,
757 						      fmt->which);
758 
759 	mutex_unlock(&isp->ops_lock);
760 	return 0;
761 }
762 
rkisp1_isp_get_selection(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_selection * sel)763 static int rkisp1_isp_get_selection(struct v4l2_subdev *sd,
764 				    struct v4l2_subdev_state *sd_state,
765 				    struct v4l2_subdev_selection *sel)
766 {
767 	struct rkisp1_isp *isp = to_rkisp1_isp(sd);
768 	int ret = 0;
769 
770 	if (sel->pad != RKISP1_ISP_PAD_SOURCE_VIDEO &&
771 	    sel->pad != RKISP1_ISP_PAD_SINK_VIDEO)
772 		return -EINVAL;
773 
774 	mutex_lock(&isp->ops_lock);
775 	switch (sel->target) {
776 	case V4L2_SEL_TGT_CROP_BOUNDS:
777 		if (sel->pad == RKISP1_ISP_PAD_SINK_VIDEO) {
778 			struct v4l2_mbus_framefmt *fmt;
779 
780 			fmt = rkisp1_isp_get_pad_fmt(isp, sd_state, sel->pad,
781 						     sel->which);
782 			sel->r.height = fmt->height;
783 			sel->r.width = fmt->width;
784 			sel->r.left = 0;
785 			sel->r.top = 0;
786 		} else {
787 			sel->r = *rkisp1_isp_get_pad_crop(isp, sd_state,
788 							  RKISP1_ISP_PAD_SINK_VIDEO,
789 							  sel->which);
790 		}
791 		break;
792 	case V4L2_SEL_TGT_CROP:
793 		sel->r = *rkisp1_isp_get_pad_crop(isp, sd_state, sel->pad,
794 						  sel->which);
795 		break;
796 	default:
797 		ret = -EINVAL;
798 	}
799 	mutex_unlock(&isp->ops_lock);
800 	return ret;
801 }
802 
rkisp1_isp_set_selection(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_selection * sel)803 static int rkisp1_isp_set_selection(struct v4l2_subdev *sd,
804 				    struct v4l2_subdev_state *sd_state,
805 				    struct v4l2_subdev_selection *sel)
806 {
807 	struct rkisp1_isp *isp = to_rkisp1_isp(sd);
808 	int ret = 0;
809 
810 	if (sel->target != V4L2_SEL_TGT_CROP)
811 		return -EINVAL;
812 
813 	dev_dbg(isp->rkisp1->dev, "%s: pad: %d sel(%d,%d)/%dx%d\n", __func__,
814 		sel->pad, sel->r.left, sel->r.top, sel->r.width, sel->r.height);
815 	mutex_lock(&isp->ops_lock);
816 	if (sel->pad == RKISP1_ISP_PAD_SINK_VIDEO)
817 		rkisp1_isp_set_sink_crop(isp, sd_state, &sel->r, sel->which);
818 	else if (sel->pad == RKISP1_ISP_PAD_SOURCE_VIDEO)
819 		rkisp1_isp_set_src_crop(isp, sd_state, &sel->r, sel->which);
820 	else
821 		ret = -EINVAL;
822 
823 	mutex_unlock(&isp->ops_lock);
824 	return ret;
825 }
826 
rkisp1_subdev_link_validate(struct media_link * link)827 static int rkisp1_subdev_link_validate(struct media_link *link)
828 {
829 	if (link->sink->index == RKISP1_ISP_PAD_SINK_PARAMS)
830 		return 0;
831 
832 	return v4l2_subdev_link_validate(link);
833 }
834 
835 static const struct v4l2_subdev_pad_ops rkisp1_isp_pad_ops = {
836 	.enum_mbus_code = rkisp1_isp_enum_mbus_code,
837 	.enum_frame_size = rkisp1_isp_enum_frame_size,
838 	.get_selection = rkisp1_isp_get_selection,
839 	.set_selection = rkisp1_isp_set_selection,
840 	.init_cfg = rkisp1_isp_init_config,
841 	.get_fmt = rkisp1_isp_get_fmt,
842 	.set_fmt = rkisp1_isp_set_fmt,
843 	.link_validate = v4l2_subdev_link_validate_default,
844 };
845 
846 /* ----------------------------------------------------------------------------
847  * Stream operations
848  */
849 
rkisp1_isp_s_stream(struct v4l2_subdev * sd,int enable)850 static int rkisp1_isp_s_stream(struct v4l2_subdev *sd, int enable)
851 {
852 	struct rkisp1_isp *isp = to_rkisp1_isp(sd);
853 	struct rkisp1_device *rkisp1 = isp->rkisp1;
854 	struct media_pad *source_pad;
855 	struct media_pad *sink_pad;
856 	enum v4l2_mbus_type mbus_type;
857 	u32 mbus_flags;
858 	int ret;
859 
860 	if (!enable) {
861 		v4l2_subdev_call(rkisp1->source, video, s_stream, false);
862 		rkisp1_isp_stop(isp);
863 		return 0;
864 	}
865 
866 	sink_pad = &isp->pads[RKISP1_ISP_PAD_SINK_VIDEO];
867 	source_pad = media_pad_remote_pad_unique(sink_pad);
868 	if (IS_ERR(source_pad)) {
869 		dev_dbg(rkisp1->dev, "Failed to get source for ISP: %ld\n",
870 			PTR_ERR(source_pad));
871 		return -EPIPE;
872 	}
873 
874 	rkisp1->source = media_entity_to_v4l2_subdev(source_pad->entity);
875 	if (!rkisp1->source) {
876 		/* This should really not happen, so is not worth a message. */
877 		return -EPIPE;
878 	}
879 
880 	if (rkisp1->source == &rkisp1->csi.sd) {
881 		mbus_type = V4L2_MBUS_CSI2_DPHY;
882 		mbus_flags = 0;
883 	} else {
884 		const struct rkisp1_sensor_async *asd;
885 		struct v4l2_async_connection *asc;
886 
887 		asc = v4l2_async_connection_unique(rkisp1->source);
888 		if (!asc)
889 			return -EPIPE;
890 
891 		asd = container_of(asc, struct rkisp1_sensor_async, asd);
892 
893 		mbus_type = asd->mbus_type;
894 		mbus_flags = asd->mbus_flags;
895 	}
896 
897 	isp->frame_sequence = -1;
898 	mutex_lock(&isp->ops_lock);
899 	ret = rkisp1_config_cif(isp, mbus_type, mbus_flags);
900 	if (ret)
901 		goto mutex_unlock;
902 
903 	rkisp1_isp_start(isp);
904 
905 	ret = v4l2_subdev_call(rkisp1->source, video, s_stream, true);
906 	if (ret) {
907 		rkisp1_isp_stop(isp);
908 		goto mutex_unlock;
909 	}
910 
911 mutex_unlock:
912 	mutex_unlock(&isp->ops_lock);
913 	return ret;
914 }
915 
rkisp1_isp_subs_evt(struct v4l2_subdev * sd,struct v4l2_fh * fh,struct v4l2_event_subscription * sub)916 static int rkisp1_isp_subs_evt(struct v4l2_subdev *sd, struct v4l2_fh *fh,
917 			       struct v4l2_event_subscription *sub)
918 {
919 	if (sub->type != V4L2_EVENT_FRAME_SYNC)
920 		return -EINVAL;
921 
922 	/* V4L2_EVENT_FRAME_SYNC doesn't require an id, so zero should be set */
923 	if (sub->id != 0)
924 		return -EINVAL;
925 
926 	return v4l2_event_subscribe(fh, sub, 0, NULL);
927 }
928 
929 static const struct media_entity_operations rkisp1_isp_media_ops = {
930 	.link_validate = rkisp1_subdev_link_validate,
931 };
932 
933 static const struct v4l2_subdev_video_ops rkisp1_isp_video_ops = {
934 	.s_stream = rkisp1_isp_s_stream,
935 };
936 
937 static const struct v4l2_subdev_core_ops rkisp1_isp_core_ops = {
938 	.subscribe_event = rkisp1_isp_subs_evt,
939 	.unsubscribe_event = v4l2_event_subdev_unsubscribe,
940 };
941 
942 static const struct v4l2_subdev_ops rkisp1_isp_ops = {
943 	.core = &rkisp1_isp_core_ops,
944 	.video = &rkisp1_isp_video_ops,
945 	.pad = &rkisp1_isp_pad_ops,
946 };
947 
rkisp1_isp_register(struct rkisp1_device * rkisp1)948 int rkisp1_isp_register(struct rkisp1_device *rkisp1)
949 {
950 	struct v4l2_subdev_state state = {
951 		.pads = rkisp1->isp.pad_cfg
952 	};
953 	struct rkisp1_isp *isp = &rkisp1->isp;
954 	struct media_pad *pads = isp->pads;
955 	struct v4l2_subdev *sd = &isp->sd;
956 	int ret;
957 
958 	isp->rkisp1 = rkisp1;
959 
960 	v4l2_subdev_init(sd, &rkisp1_isp_ops);
961 	sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
962 	sd->entity.ops = &rkisp1_isp_media_ops;
963 	sd->entity.function = MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER;
964 	sd->owner = THIS_MODULE;
965 	strscpy(sd->name, RKISP1_ISP_DEV_NAME, sizeof(sd->name));
966 
967 	pads[RKISP1_ISP_PAD_SINK_VIDEO].flags = MEDIA_PAD_FL_SINK |
968 						MEDIA_PAD_FL_MUST_CONNECT;
969 	pads[RKISP1_ISP_PAD_SINK_PARAMS].flags = MEDIA_PAD_FL_SINK;
970 	pads[RKISP1_ISP_PAD_SOURCE_VIDEO].flags = MEDIA_PAD_FL_SOURCE;
971 	pads[RKISP1_ISP_PAD_SOURCE_STATS].flags = MEDIA_PAD_FL_SOURCE;
972 
973 	isp->sink_fmt = rkisp1_mbus_info_get_by_code(RKISP1_DEF_SINK_PAD_FMT);
974 	isp->src_fmt = rkisp1_mbus_info_get_by_code(RKISP1_DEF_SRC_PAD_FMT);
975 
976 	mutex_init(&isp->ops_lock);
977 	ret = media_entity_pads_init(&sd->entity, RKISP1_ISP_PAD_MAX, pads);
978 	if (ret)
979 		goto error;
980 
981 	ret = v4l2_device_register_subdev(&rkisp1->v4l2_dev, sd);
982 	if (ret) {
983 		dev_err(rkisp1->dev, "Failed to register isp subdev\n");
984 		goto error;
985 	}
986 
987 	rkisp1_isp_init_config(sd, &state);
988 
989 	return 0;
990 
991 error:
992 	media_entity_cleanup(&sd->entity);
993 	mutex_destroy(&isp->ops_lock);
994 	isp->sd.v4l2_dev = NULL;
995 	return ret;
996 }
997 
rkisp1_isp_unregister(struct rkisp1_device * rkisp1)998 void rkisp1_isp_unregister(struct rkisp1_device *rkisp1)
999 {
1000 	struct rkisp1_isp *isp = &rkisp1->isp;
1001 
1002 	if (!isp->sd.v4l2_dev)
1003 		return;
1004 
1005 	v4l2_device_unregister_subdev(&isp->sd);
1006 	media_entity_cleanup(&isp->sd.entity);
1007 	mutex_destroy(&isp->ops_lock);
1008 }
1009 
1010 /* ----------------------------------------------------------------------------
1011  * Interrupt handlers
1012  */
1013 
rkisp1_isp_queue_event_sof(struct rkisp1_isp * isp)1014 static void rkisp1_isp_queue_event_sof(struct rkisp1_isp *isp)
1015 {
1016 	struct v4l2_event event = {
1017 		.type = V4L2_EVENT_FRAME_SYNC,
1018 	};
1019 
1020 	event.u.frame_sync.frame_sequence = isp->frame_sequence;
1021 	v4l2_event_queue(isp->sd.devnode, &event);
1022 }
1023 
rkisp1_isp_isr(int irq,void * ctx)1024 irqreturn_t rkisp1_isp_isr(int irq, void *ctx)
1025 {
1026 	struct device *dev = ctx;
1027 	struct rkisp1_device *rkisp1 = dev_get_drvdata(dev);
1028 	u32 status, isp_err;
1029 
1030 	if (!rkisp1->irqs_enabled)
1031 		return IRQ_NONE;
1032 
1033 	status = rkisp1_read(rkisp1, RKISP1_CIF_ISP_MIS);
1034 	if (!status)
1035 		return IRQ_NONE;
1036 
1037 	rkisp1_write(rkisp1, RKISP1_CIF_ISP_ICR, status);
1038 
1039 	/* Vertical sync signal, starting generating new frame */
1040 	if (status & RKISP1_CIF_ISP_V_START) {
1041 		rkisp1->isp.frame_sequence++;
1042 		rkisp1_isp_queue_event_sof(&rkisp1->isp);
1043 		if (status & RKISP1_CIF_ISP_FRAME) {
1044 			WARN_ONCE(1, "irq delay is too long, buffers might not be in sync\n");
1045 			rkisp1->debug.irq_delay++;
1046 		}
1047 	}
1048 	if (status & RKISP1_CIF_ISP_PIC_SIZE_ERROR) {
1049 		/* Clear pic_size_error */
1050 		isp_err = rkisp1_read(rkisp1, RKISP1_CIF_ISP_ERR);
1051 		if (isp_err & RKISP1_CIF_ISP_ERR_INFORM_SIZE)
1052 			rkisp1->debug.inform_size_error++;
1053 		if (isp_err & RKISP1_CIF_ISP_ERR_IS_SIZE)
1054 			rkisp1->debug.img_stabilization_size_error++;
1055 		if (isp_err & RKISP1_CIF_ISP_ERR_OUTFORM_SIZE)
1056 			rkisp1->debug.outform_size_error++;
1057 		rkisp1_write(rkisp1, RKISP1_CIF_ISP_ERR_CLR, isp_err);
1058 	} else if (status & RKISP1_CIF_ISP_DATA_LOSS) {
1059 		/* keep track of data_loss in debugfs */
1060 		rkisp1->debug.data_loss++;
1061 	}
1062 
1063 	if (status & RKISP1_CIF_ISP_FRAME) {
1064 		u32 isp_ris;
1065 
1066 		/* New frame from the sensor received */
1067 		isp_ris = rkisp1_read(rkisp1, RKISP1_CIF_ISP_RIS);
1068 		if (isp_ris & RKISP1_STATS_MEAS_MASK)
1069 			rkisp1_stats_isr(&rkisp1->stats, isp_ris);
1070 		/*
1071 		 * Then update changed configs. Some of them involve
1072 		 * lot of register writes. Do those only one per frame.
1073 		 * Do the updates in the order of the processing flow.
1074 		 */
1075 		rkisp1_params_isr(rkisp1);
1076 	}
1077 
1078 	return IRQ_HANDLED;
1079 }
1080