xref: /linux/drivers/media/platform/ti/vpe/vip.c (revision fab183d632628381b466a41479489541ac0e29a0)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * TI VIP capture driver
4  *
5  * Copyright (C) 2025 Texas Instruments Incorporated -  http://www.ti.com/
6  * David Griego, <dagriego@biglakesoftware.com>
7  * Dale Farnsworth, <dale@farnsworth.org>
8  * Yemike Abhilash Chandra, <y-abhilashchandra@ti.com>
9  */
10 
11 #include <linux/clk.h>
12 #include <linux/cleanup.h>
13 #include <linux/delay.h>
14 #include <linux/dma-mapping.h>
15 #include <linux/err.h>
16 #include <linux/interrupt.h>
17 #include <linux/module.h>
18 #include <linux/workqueue.h>
19 #include <linux/platform_device.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/sched.h>
22 #include <linux/mfd/syscon.h>
23 #include <linux/regmap.h>
24 
25 #include <linux/pinctrl/consumer.h>
26 #include <linux/of_device.h>
27 #include <linux/of_graph.h>
28 
29 #include "vip.h"
30 
31 #define VIP_MODULE_NAME "vip"
32 
33 static int debug;
34 module_param(debug, int, 0644);
35 MODULE_PARM_DESC(debug, "debug level (0-8)");
36 
37 /*
38  * Minimum and maximum frame sizes
39  */
40 #define MIN_W		128
41 #define MIN_H		128
42 #define MAX_W		2048
43 #define MAX_H		1536
44 
45 /*
46  * Required alignments
47  */
48 #define S_ALIGN		0 /* multiple of 1 */
49 #define H_ALIGN		1 /* multiple of 2 */
50 #define W_ALIGN		1 /* multiple of 2 */
51 
52 /*
53  * Need a descriptor entry for each of up to 15 outputs,
54  * and up to 2 control transfers.
55  */
56 #define VIP_DESC_LIST_SIZE	(17 * sizeof(struct vpdma_dtd))
57 
58 /*
59  * port flag bits
60  */
61 #define FLAG_INTERLACED	BIT(4)
62 #define FLAG_MULT_PORT		BIT(6)
63 #define FLAG_MULT_ANC		BIT(7)
64 
65 #define VIP_VPDMA_FIFO_SIZE	2
66 #define VIP_DROPQ_SIZE		3
67 
68 /*
69  * Define indices into the srce_info tables
70  */
71 
72 #define VIP_SRCE_MULT_PORT	0
73 #define VIP_SRCE_MULT_ANC	1
74 #define VIP_SRCE_LUMA		2
75 #define VIP_SRCE_CHROMA	3
76 #define VIP_SRCE_RGB		4
77 
78 #define reg_read(dev, offset) ioread32((dev)->base + (offset))
79 #define reg_write(dev, offset, val) iowrite32((val), (dev)->base + (offset))
80 
81 #define GET_OFFSET_TOP(port, obj, reg)	\
82 	((obj)->base - (port)->dev->base + (reg))
83 
84 #define VIP_SET_MMR_ADB_HDR(port, hdr, regs, offset_a)	\
85 	VPDMA_SET_MMR_ADB_HDR((port)->mmr_adb, vip_mmr_adb, hdr, regs, offset_a)
86 
87 /*
88  * These represent the module resets bit for slice 1
89  * Upon detecting slice2 we simply left shift by 1
90  */
91 #define VIP_DP_RST	BIT(16)
92 #define VIP_CSC_RST	BIT(20)
93 #define VIP_SC_RST	BIT(22)
94 
95 #define VIP_PARSER_PORT(p)		(VIP_PARSER_PORTA_0 + ((p) * 0x8U))
96 #define VIP_PARSER_CROP_H_PORT(p) \
97 		(VIP_PARSER_PORTA_EXTRA4 + ((p) * 0x10U))
98 #define VIP_PARSER_CROP_V_PORT(p) \
99 		(VIP_PARSER_PORTA_EXTRA5 + ((p) * 0x10U))
100 #define VIP_PARSER_STOP_IMM_PORT(p)	(VIP_PARSER_PORTA_EXTRA6 + ((p) * 0x4U))
101 
102 #define PARSER_IRQ_MASK (VIP_PORTA_OUTPUT_FIFO_YUV | \
103 			 VIP_PORTB_OUTPUT_FIFO_YUV)
104 
105 /*
106  * The srce_info structure contains per-srce data.
107  */
108 struct vip_srce_info {
109 	u8	base_channel;	/* the VPDMA channel number */
110 	u8	vb_index;	/* input frame f, f-1, f-2 index */
111 	u8	vb_part;	/* identifies section of co-planar formats */
112 };
113 
114 static struct vip_srce_info srce_info[5] = {
115 	[VIP_SRCE_MULT_PORT] = {
116 		.base_channel	= VIP1_CHAN_NUM_MULT_PORT_A_SRC0,
117 		.vb_index	= 0,
118 		.vb_part	= VIP_CHROMA,
119 	},
120 	[VIP_SRCE_MULT_ANC] = {
121 		.base_channel	= VIP1_CHAN_NUM_MULT_ANC_A_SRC0,
122 		.vb_index	= 0,
123 		.vb_part	= VIP_LUMA,
124 	},
125 	[VIP_SRCE_LUMA] = {
126 		.base_channel	= VIP1_CHAN_NUM_PORT_A_LUMA,
127 		.vb_index	= 1,
128 		.vb_part	= VIP_LUMA,
129 	},
130 	[VIP_SRCE_CHROMA] = {
131 		.base_channel	= VIP1_CHAN_NUM_PORT_A_CHROMA,
132 		.vb_index	= 1,
133 		.vb_part	= VIP_CHROMA,
134 	},
135 	[VIP_SRCE_RGB] = {
136 		.base_channel	= VIP1_CHAN_NUM_PORT_A_RGB,
137 		.vb_part	= VIP_LUMA,
138 	},
139 };
140 
141 static struct vip_fmt vip_formats[VIP_MAX_ACTIVE_FMT] = {
142 	{
143 		.fourcc		= V4L2_PIX_FMT_NV12,
144 		.code		= MEDIA_BUS_FMT_UYVY8_2X8,
145 		.coplanar	= 1,
146 		.vpdma_fmt	= { &vpdma_yuv_fmts[VPDMA_DATA_FMT_Y420],
147 				    &vpdma_yuv_fmts[VPDMA_DATA_FMT_C420],
148 				  },
149 	},
150 	{
151 		.fourcc		= V4L2_PIX_FMT_UYVY,
152 		.code		= MEDIA_BUS_FMT_UYVY8_2X8,
153 		.coplanar	= 0,
154 		.vpdma_fmt	= { &vpdma_yuv_fmts[VPDMA_DATA_FMT_CBY422],
155 				  },
156 	},
157 	{
158 		.fourcc		= V4L2_PIX_FMT_YUYV,
159 		.code		= MEDIA_BUS_FMT_UYVY8_2X8,
160 		.coplanar	= 0,
161 		.vpdma_fmt	= { &vpdma_yuv_fmts[VPDMA_DATA_FMT_YCB422],
162 				  },
163 	},
164 	{
165 		.fourcc		= V4L2_PIX_FMT_VYUY,
166 		.code		= MEDIA_BUS_FMT_UYVY8_2X8,
167 		.coplanar	= 0,
168 		.vpdma_fmt	= { &vpdma_yuv_fmts[VPDMA_DATA_FMT_CRY422],
169 				  },
170 	},
171 	{
172 		.fourcc		= V4L2_PIX_FMT_YVYU,
173 		.code		= MEDIA_BUS_FMT_UYVY8_2X8,
174 		.coplanar	= 0,
175 		.vpdma_fmt	= { &vpdma_yuv_fmts[VPDMA_DATA_FMT_YCR422],
176 				  },
177 	},
178 	{
179 		.fourcc		= V4L2_PIX_FMT_RGB24,
180 		.code		= MEDIA_BUS_FMT_UYVY8_2X8,
181 		.coplanar	= 0,
182 		.vpdma_fmt	= { &vpdma_rgb_fmts[VPDMA_DATA_FMT_RGB24],
183 				  },
184 	},
185 	{
186 		.fourcc		= V4L2_PIX_FMT_RGB32,
187 		.code		= MEDIA_BUS_FMT_UYVY8_2X8,
188 		.coplanar	= 0,
189 		.vpdma_fmt	= { &vpdma_rgb_fmts[VPDMA_DATA_FMT_ARGB32],
190 				  },
191 	},
192 	{
193 		.fourcc		= V4L2_PIX_FMT_BGR24,
194 		.code		= MEDIA_BUS_FMT_UYVY8_2X8,
195 		.coplanar	= 0,
196 		.vpdma_fmt	= { &vpdma_rgb_fmts[VPDMA_DATA_FMT_BGR24],
197 				  },
198 	},
199 	{
200 		.fourcc		= V4L2_PIX_FMT_BGR32,
201 		.code		= MEDIA_BUS_FMT_UYVY8_2X8,
202 		.coplanar	= 0,
203 		.vpdma_fmt	= { &vpdma_rgb_fmts[VPDMA_DATA_FMT_ABGR32],
204 				  },
205 	},
206 	{
207 		.fourcc		= V4L2_PIX_FMT_RGB24,
208 		.code		= MEDIA_BUS_FMT_RGB888_1X24,
209 		.coplanar	= 0,
210 		.vpdma_fmt	= { &vpdma_rgb_fmts[VPDMA_DATA_FMT_RGB24],
211 				  },
212 	},
213 	{
214 		.fourcc		= V4L2_PIX_FMT_RGB32,
215 		.code		= MEDIA_BUS_FMT_ARGB8888_1X32,
216 		.coplanar	= 0,
217 		.vpdma_fmt	= { &vpdma_rgb_fmts[VPDMA_DATA_FMT_ARGB32],
218 				  },
219 	},
220 	{
221 		.fourcc		= V4L2_PIX_FMT_SBGGR8,
222 		.code		= MEDIA_BUS_FMT_SBGGR8_1X8,
223 		.coplanar	= 0,
224 		.vpdma_fmt	= { &vpdma_raw_fmts[VPDMA_DATA_FMT_RAW8],
225 				  },
226 	},
227 	{
228 		.fourcc		= V4L2_PIX_FMT_SGBRG8,
229 		.code		= MEDIA_BUS_FMT_SGBRG8_1X8,
230 		.coplanar	= 0,
231 		.vpdma_fmt	= { &vpdma_raw_fmts[VPDMA_DATA_FMT_RAW8],
232 				  },
233 	},
234 	{
235 		.fourcc		= V4L2_PIX_FMT_SGRBG8,
236 		.code		= MEDIA_BUS_FMT_SGRBG8_1X8,
237 		.coplanar	= 0,
238 		.vpdma_fmt	= { &vpdma_raw_fmts[VPDMA_DATA_FMT_RAW8],
239 				  },
240 	},
241 	{
242 		.fourcc		= V4L2_PIX_FMT_SRGGB8,
243 		.code		= MEDIA_BUS_FMT_SRGGB8_1X8,
244 		.coplanar	= 0,
245 		.vpdma_fmt	= { &vpdma_raw_fmts[VPDMA_DATA_FMT_RAW8],
246 				  },
247 	},
248 	{
249 		/* V4L2 currently only defines one 16 bit variant */
250 		.fourcc		= V4L2_PIX_FMT_SBGGR16,
251 		.code		= MEDIA_BUS_FMT_SBGGR16_1X16,
252 		.coplanar	= 0,
253 		.vpdma_fmt	= { &vpdma_raw_fmts[VPDMA_DATA_FMT_RAW16],
254 				  },
255 	},
256 };
257 
258 /*
259  * DMA address/data block for the shadow registers
260  */
261 struct vip_mmr_adb {
262 	struct vpdma_adb_hdr	sc_hdr0;
263 	u32			sc_regs0[7];
264 	u32			sc_pad0[1];
265 	struct vpdma_adb_hdr	sc_hdr8;
266 	u32			sc_regs8[6];
267 	u32			sc_pad8[2];
268 	struct vpdma_adb_hdr	sc_hdr17;
269 	u32			sc_regs17[9];
270 	u32			sc_pad17[3];
271 	struct vpdma_adb_hdr	csc_hdr;
272 	u32			csc_regs[6];
273 	u32			csc_pad[2];
274 };
275 
276 /*
277  * Function prototype declarations
278  */
279 static int alloc_port(struct vip_dev *, int);
280 static void free_port(struct vip_port *);
281 static int vip_setup_parser(struct vip_port *port);
282 static int vip_setup_scaler(struct vip_stream *stream);
283 static void vip_enable_parser(struct vip_port *port, bool on);
284 static void vip_reset_parser(struct vip_port *port, bool on);
285 static void vip_parser_stop_imm(struct vip_port *port, bool on);
286 static void stop_dma(struct vip_stream *stream, bool clear_list);
287 static int vip_load_vpdma_list_fifo(struct vip_stream *stream);
288 static inline bool is_scaler_available(struct vip_port *port);
289 static inline bool allocate_scaler(struct vip_port *port);
290 static inline void free_scaler(struct vip_port *port);
291 static bool is_csc_available(struct vip_port *port);
292 static bool allocate_csc(struct vip_port *port,
293 			 enum vip_csc_state csc_direction);
294 static void free_csc(struct vip_port *port);
295 
296 /* initialize  v4l2_format_info member in vip_formats array */
vip_init_format_info(struct device * dev)297 static void vip_init_format_info(struct device *dev)
298 {
299 	struct vip_fmt *fmt;
300 	int i;
301 
302 	for (i = 0; i < ARRAY_SIZE(vip_formats); i++) {
303 		fmt = &vip_formats[i];
304 		fmt->finfo = v4l2_format_info(fmt->fourcc);
305 	}
306 }
307 
308 /*  Print Four-character-code (FOURCC) */
fourcc_to_str(u32 fmt)309 static char *fourcc_to_str(u32 fmt)
310 {
311 	static char code[5];
312 
313 	code[0] = (unsigned char)(fmt & 0xff);
314 	code[1] = (unsigned char)((fmt >> 8) & 0xff);
315 	code[2] = (unsigned char)((fmt >> 16) & 0xff);
316 	code[3] = (unsigned char)((fmt >> 24) & 0xff);
317 	code[4] = '\0';
318 
319 	return code;
320 }
321 
322 /*
323  * Find our format description corresponding to the passed v4l2_format
324  */
find_port_format_by_pix(struct vip_port * port,u32 pixelformat)325 static struct vip_fmt *find_port_format_by_pix(struct vip_port *port,
326 					       u32 pixelformat)
327 {
328 	struct vip_fmt *fmt;
329 	unsigned int index;
330 
331 	for (index = 0; index < port->num_active_fmt; index++) {
332 		fmt = port->active_fmt[index];
333 		if (fmt->fourcc == pixelformat)
334 			return fmt;
335 	}
336 
337 	return NULL;
338 }
339 
find_port_format_by_code(struct vip_port * port,u32 code)340 static struct vip_fmt *find_port_format_by_code(struct vip_port *port,
341 						u32 code)
342 {
343 	struct vip_fmt *fmt;
344 	unsigned int index;
345 
346 	for (index = 0; index < port->num_active_fmt; index++) {
347 		fmt = port->active_fmt[index];
348 		if (fmt->code == code)
349 			return fmt;
350 	}
351 
352 	return NULL;
353 }
354 
notifier_to_vip_port(struct v4l2_async_notifier * n)355 inline struct vip_port *notifier_to_vip_port(struct v4l2_async_notifier *n)
356 {
357 	return container_of(n, struct vip_port, notifier);
358 }
359 
vip_is_mbuscode_yuv(u32 code)360 static bool vip_is_mbuscode_yuv(u32 code)
361 {
362 	return ((code & 0xff00) == 0x2000);
363 }
364 
vip_is_mbuscode_rgb(u32 code)365 static bool vip_is_mbuscode_rgb(u32 code)
366 {
367 	return ((code & 0xff00) == 0x1000);
368 }
369 
vip_is_mbuscode_raw(u32 code)370 static bool vip_is_mbuscode_raw(u32 code)
371 {
372 	return ((code & 0xff00) == 0x3000);
373 }
374 
375 /*
376  * This is not an accurate conversion but it is only used to
377  * assess if color conversion is needed.
378  */
vip_mbus_code_to_fourcc(u32 code)379 static u32 vip_mbus_code_to_fourcc(u32 code)
380 {
381 	if (vip_is_mbuscode_rgb(code))
382 		return V4L2_PIX_FMT_RGB24;
383 
384 	if (vip_is_mbuscode_yuv(code))
385 		return V4L2_PIX_FMT_UYVY;
386 
387 	return V4L2_PIX_FMT_SBGGR8;
388 }
389 
390 static enum vip_csc_state
vip_csc_direction(u32 src_code,const struct v4l2_format_info * dfinfo)391 vip_csc_direction(u32 src_code, const struct v4l2_format_info *dfinfo)
392 {
393 	if (vip_is_mbuscode_yuv(src_code) && v4l2_is_format_rgb(dfinfo))
394 		return VIP_CSC_Y2R;
395 	else if (vip_is_mbuscode_rgb(src_code) && v4l2_is_format_yuv(dfinfo))
396 		return VIP_CSC_R2Y;
397 	else
398 		return VIP_CSC_NA;
399 }
400 
401 /*
402  * Insert a masked field into a 32-bit field
403  */
insert_field(u32 * valp,u32 field,u32 mask,int shift)404 static void insert_field(u32 *valp, u32 field, u32 mask, int shift)
405 {
406 	u32 val = *valp;
407 
408 	val &= ~(mask << shift);
409 	val |= (field & mask) << shift;
410 	*valp = val;
411 }
412 
413 /*
414  * Set the headers for all of the address/data block structures.
415  */
init_adb_hdrs(struct vip_port * port)416 static void init_adb_hdrs(struct vip_port *port)
417 {
418 	VIP_SET_MMR_ADB_HDR(port, sc_hdr0, sc_regs0,
419 			    GET_OFFSET_TOP(port, port->dev->sc, CFG_SC0));
420 	VIP_SET_MMR_ADB_HDR(port, sc_hdr8, sc_regs8,
421 			    GET_OFFSET_TOP(port, port->dev->sc, CFG_SC8));
422 	VIP_SET_MMR_ADB_HDR(port, sc_hdr17, sc_regs17,
423 			    GET_OFFSET_TOP(port, port->dev->sc, CFG_SC17));
424 	VIP_SET_MMR_ADB_HDR(port, csc_hdr, csc_regs,
425 			    GET_OFFSET_TOP(port, port->dev->csc, CSC_CSC00));
426 
427 };
428 
vip_module_toggle(struct vip_dev * dev,uint32_t module,bool on)429 static void vip_module_toggle(struct vip_dev *dev, uint32_t module, bool on)
430 {
431 	u32 val = 0;
432 
433 	val = reg_read(dev, VIP_CLK_RESET);
434 
435 	if (dev->slice_id == VIP_SLICE2)
436 		module <<= 1;
437 
438 	if (on)
439 		val |= module;
440 	else
441 		val &= ~module;
442 
443 	reg_write(dev, VIP_CLK_RESET, val);
444 }
445 
446 /*
447  * Enable or disable the VIP clocks
448  */
vip_set_clock_enable(struct vip_dev * dev,bool on)449 static void vip_set_clock_enable(struct vip_dev *dev, bool on)
450 {
451 	u32 val = 0;
452 
453 	val = reg_read(dev, VIP_CLK_ENABLE);
454 	if (on) {
455 		val |= VIP_VPDMA_CLK_ENABLE;
456 		if (dev->slice_id == VIP_SLICE1)
457 			val |= VIP_VIP1_DATA_PATH_CLK_ENABLE;
458 		else
459 			val |= VIP_VIP2_DATA_PATH_CLK_ENABLE;
460 	} else {
461 		if (dev->slice_id == VIP_SLICE1)
462 			val &= ~VIP_VIP1_DATA_PATH_CLK_ENABLE;
463 		else
464 			val &= ~VIP_VIP2_DATA_PATH_CLK_ENABLE;
465 
466 		/* Both VIP are disabled then shutdown VPDMA also */
467 		if (!(val & (VIP_VIP1_DATA_PATH_CLK_ENABLE |
468 			     VIP_VIP2_DATA_PATH_CLK_ENABLE)))
469 			val = 0;
470 	}
471 
472 	reg_write(dev, VIP_CLK_ENABLE, val);
473 }
474 
475 /* This helper function is used to enable the clock early on to
476  * enable vpdma firmware loading before the slice device are created
477  */
vip_shared_set_clock_enable(struct vip_shared * shared,bool on)478 static void vip_shared_set_clock_enable(struct vip_shared *shared, bool on)
479 {
480 	u32 val = 0;
481 
482 	val = VIP_VIP1_DATA_PATH_CLK_ENABLE | VIP_VPDMA_CLK_ENABLE;
483 
484 	reg_write(shared, VIP_CLK_ENABLE, val);
485 }
486 
vip_top_reset(struct vip_dev * dev)487 static void vip_top_reset(struct vip_dev *dev)
488 {
489 	u32 val = 0;
490 
491 	val = reg_read(dev, VIP_CLK_RESET);
492 
493 	if (dev->slice_id == VIP_SLICE1)
494 		insert_field(&val, 1, VIP_DATA_PATH_CLK_RESET_MASK,
495 			     VIP_VIP1_DATA_PATH_RESET_SHIFT);
496 	else
497 		insert_field(&val, 1, VIP_DATA_PATH_CLK_RESET_MASK,
498 			     VIP_VIP2_DATA_PATH_RESET_SHIFT);
499 
500 	reg_write(dev, VIP_CLK_RESET, val);
501 
502 	usleep_range(200, 250);
503 
504 	val = reg_read(dev, VIP_CLK_RESET);
505 
506 	if (dev->slice_id == VIP_SLICE1)
507 		insert_field(&val, 0, VIP_DATA_PATH_CLK_RESET_MASK,
508 			     VIP_VIP1_DATA_PATH_RESET_SHIFT);
509 	else
510 		insert_field(&val, 0, VIP_DATA_PATH_CLK_RESET_MASK,
511 			     VIP_VIP2_DATA_PATH_RESET_SHIFT);
512 	reg_write(dev, VIP_CLK_RESET, val);
513 }
514 
vip_top_vpdma_reset(struct vip_shared * shared)515 static void vip_top_vpdma_reset(struct vip_shared *shared)
516 {
517 	u32 val;
518 
519 	val = reg_read(shared, VIP_CLK_RESET);
520 	insert_field(&val, 1, VIP_VPDMA_CLK_RESET_MASK,
521 		     VIP_VPDMA_CLK_RESET_SHIFT);
522 	reg_write(shared, VIP_CLK_RESET, val);
523 
524 	usleep_range(200, 250);
525 
526 	val = reg_read(shared, VIP_CLK_RESET);
527 	insert_field(&val, 0, VIP_VPDMA_CLK_RESET_MASK,
528 		     VIP_VPDMA_CLK_RESET_SHIFT);
529 	reg_write(shared, VIP_CLK_RESET, val);
530 }
531 
vip_set_pclk_invert(struct vip_port * port)532 static void vip_set_pclk_invert(struct vip_port *port)
533 {
534 	struct vip_ctrl_module *ctrl = port->dev->syscon;
535 	struct vip_dev *dev = port->dev;
536 	u32 index;
537 	/*
538 	 * When the VIP parser is configured to so that the pixel clock
539 	 * is to be sampled at falling edge, the pixel clock needs to be
540 	 * inverted before it is given to the VIP module. This is done
541 	 * by setting a bit in the CTRL_CORE_SMA_SW1 register.
542 	 */
543 
544 	index = 2 * port->dev->slice_id + port->port_id;
545 
546 	v4l2_dbg(3, debug, &dev->v4l2_dev, "%s: slice%d:port%d -> index: %d\n", __func__,
547 		 port->dev->slice_id, port->port_id, index);
548 
549 	if (ctrl->syscon_pol)
550 		regmap_update_bits(ctrl->syscon_pol,
551 				   ctrl->syscon_offset,
552 				   ctrl->syscon_bit_field[index],
553 				   ctrl->syscon_bit_field[index]);
554 }
555 
vip_set_data_interface(struct vip_port * port,enum data_interface_modes mode)556 static void vip_set_data_interface(struct vip_port *port,
557 				   enum data_interface_modes mode)
558 {
559 	u32 val = 0;
560 
561 	insert_field(&val, mode, VIP_DATA_INTERFACE_MODE_MASK,
562 		     VIP_DATA_INTERFACE_MODE_SHFT);
563 
564 	reg_write(port->dev->parser, VIP_PARSER_MAIN_CFG, val);
565 }
566 
vip_set_slice_path(struct vip_dev * dev,enum data_path_select data_path,u32 path_val)567 static void vip_set_slice_path(struct vip_dev *dev,
568 			       enum data_path_select data_path, u32 path_val)
569 {
570 	u32 val = 0;
571 	int data_path_reg;
572 
573 	data_path_reg = VIP_VIP1_DATA_PATH_SELECT + 4 * dev->slice_id;
574 
575 	switch (data_path) {
576 	case ALL_FIELDS_DATA_SELECT:
577 		val |= path_val;
578 		break;
579 	case VIP_CSC_SRC_DATA_SELECT:
580 		insert_field(&val, path_val, VIP_CSC_SRC_SELECT_MASK,
581 			     VIP_CSC_SRC_SELECT_SHFT);
582 		break;
583 	case VIP_SC_SRC_DATA_SELECT:
584 		insert_field(&val, path_val, VIP_SC_SRC_SELECT_MASK,
585 			     VIP_SC_SRC_SELECT_SHFT);
586 		break;
587 	case VIP_RGB_SRC_DATA_SELECT:
588 		val |= (path_val) ? VIP_RGB_SRC_SELECT : 0;
589 		break;
590 	case VIP_RGB_OUT_LO_DATA_SELECT:
591 		val |= (path_val) ? VIP_RGB_OUT_LO_SRC_SELECT : 0;
592 		break;
593 	case VIP_RGB_OUT_HI_DATA_SELECT:
594 		val |= (path_val) ? VIP_RGB_OUT_HI_SRC_SELECT : 0;
595 		break;
596 	case VIP_CHR_DS_1_SRC_DATA_SELECT:
597 		insert_field(&val, path_val, VIP_DS1_SRC_SELECT_MASK,
598 			     VIP_DS1_SRC_SELECT_SHFT);
599 		break;
600 	case VIP_CHR_DS_2_SRC_DATA_SELECT:
601 		insert_field(&val, path_val, VIP_DS2_SRC_SELECT_MASK,
602 			     VIP_DS2_SRC_SELECT_SHFT);
603 		break;
604 	case VIP_MULTI_CHANNEL_DATA_SELECT:
605 		val |= (path_val) ? VIP_MULTI_CHANNEL_SELECT : 0;
606 		break;
607 	case VIP_CHR_DS_1_DATA_BYPASS:
608 		val |= (path_val) ? VIP_DS1_BYPASS : 0;
609 		break;
610 	case VIP_CHR_DS_2_DATA_BYPASS:
611 		val |= (path_val) ? VIP_DS2_BYPASS : 0;
612 		break;
613 	default:
614 		v4l2_err(&dev->v4l2_dev, "%s: data_path 0x%x is not valid\n",
615 			 __func__, data_path);
616 		return;
617 	}
618 	insert_field(&val, data_path, VIP_DATAPATH_SELECT_MASK,
619 		     VIP_DATAPATH_SELECT_SHFT);
620 	reg_write(dev, data_path_reg, val);
621 	v4l2_dbg(3, debug, &dev->v4l2_dev, "%s: DATA_PATH_SELECT(%08X): %08X\n", __func__,
622 		 data_path_reg, reg_read(dev, data_path_reg));
623 }
624 
625 /*
626  * Return the vip_stream structure for a given struct file
627  */
file2stream(struct file * file)628 static inline struct vip_stream *file2stream(struct file *file)
629 {
630 	return video_drvdata(file);
631 }
632 
633 /*
634  * Append a destination descriptor to the current descriptor list,
635  * setting up dma to the given srce.
636  */
add_out_dtd(struct vip_stream * stream,int srce_type)637 static int add_out_dtd(struct vip_stream *stream, int srce_type)
638 {
639 	struct vip_port *port = stream->port;
640 	struct vip_dev *dev = port->dev;
641 	struct vip_srce_info *sinfo = &srce_info[srce_type];
642 	struct v4l2_rect *c_rect = &port->c_rect;
643 	struct vip_fmt *fmt = port->fmt;
644 	int channel, plane = 0;
645 	int max_width, max_height;
646 	dma_addr_t dma_addr = 0;
647 	u32 flags;
648 	u32 width = stream->width;
649 
650 	channel = sinfo->base_channel;
651 
652 	switch (srce_type) {
653 	case VIP_SRCE_MULT_PORT:
654 	case VIP_SRCE_MULT_ANC:
655 		if (port->port_id == VIP_PORTB)
656 			channel += VIP_CHAN_MULT_PORTB_OFFSET;
657 		channel += stream->stream_id;
658 		flags = 0;
659 		break;
660 	case VIP_SRCE_CHROMA:
661 		plane = 1;
662 		fallthrough;
663 	case VIP_SRCE_LUMA:
664 		if (port->port_id == VIP_PORTB) {
665 			if (port->scaler && !port->fmt->coplanar)
666 				/*
667 				 * In this case Port A Chroma channel
668 				 * is used to carry Port B scaled YUV422
669 				 */
670 				channel += 1;
671 			else
672 				channel += VIP_CHAN_YUV_PORTB_OFFSET;
673 		}
674 		flags = port->flags;
675 		break;
676 	case VIP_SRCE_RGB:
677 		if (port->port_id == VIP_PORTB ||
678 		    (port->port_id == VIP_PORTA &&
679 		     port->csc == VIP_CSC_NA &&
680 		     v4l2_is_format_rgb(port->fmt->finfo)))
681 			/*
682 			 * RGB sensor only connect to Y_LO
683 			 * channel i.e. port B channel.
684 			 */
685 			channel += VIP_CHAN_RGB_PORTB_OFFSET;
686 		flags = port->flags;
687 		break;
688 	default:
689 		v4l2_err(&dev->v4l2_dev, "%s: srce_type 0x%x is not valid\n",
690 			 __func__, srce_type);
691 		return -1;
692 	}
693 
694 	if (dev->slice_id == VIP_SLICE2)
695 		channel += VIP_CHAN_VIP2_OFFSET;
696 
697 	if (port->fmt->vpdma_fmt[0] == &vpdma_raw_fmts[VPDMA_DATA_FMT_RAW8]) {
698 		/*
699 		 * Special case since we are faking a YUV422 16bit format
700 		 * to have the vpdma perform the needed byte swap
701 		 * we need to adjust the pixel width accordingly
702 		 * otherwise the parser will attempt to collect more pixels
703 		 * then available and the vpdma transfer will exceed the
704 		 * allocated frame buffer.
705 		 */
706 		width >>= 1;
707 		v4l2_dbg(1, debug, &dev->v4l2_dev, "%s: 8 bit raw detected, adjusting width to %d\n",
708 			 __func__, width);
709 	}
710 
711 	/*
712 	 * Use VPDMA_MAX_SIZE1 or VPDMA_MAX_SIZE2 register for slice0/1
713 	 */
714 
715 	if (dev->slice_id == VIP_SLICE1) {
716 		vpdma_set_max_size(dev->shared->vpdma, VPDMA_MAX_SIZE1,
717 				   width, stream->height);
718 
719 		max_width = MAX_OUT_WIDTH_REG1;
720 		max_height = MAX_OUT_HEIGHT_REG1;
721 	} else {
722 		vpdma_set_max_size(dev->shared->vpdma, VPDMA_MAX_SIZE2,
723 				   width, stream->height);
724 
725 		max_width = MAX_OUT_WIDTH_REG2;
726 		max_height = MAX_OUT_HEIGHT_REG2;
727 	}
728 
729 	/*
730 	 * Mark this channel to be cleared while cleaning up resources
731 	 * This will make sure that an abort descriptor for this channel
732 	 * would be submitted to VPDMA causing any ongoing  transaction to be
733 	 * aborted and cleanup the VPDMA FSM for this channel
734 	 */
735 	stream->vpdma_channels[channel] = 1;
736 
737 	vpdma_rawchan_add_out_dtd(&stream->desc_list, c_rect->width,
738 				  stream->bytesperline, c_rect,
739 				  fmt->vpdma_fmt[plane], dma_addr,
740 				  max_width, max_height, channel, flags);
741 	return 0;
742 }
743 
744 /*
745  * add_stream_dtds - prepares and starts DMA for pending transfers
746  */
add_stream_dtds(struct vip_stream * stream)747 static void add_stream_dtds(struct vip_stream *stream)
748 {
749 	struct vip_port *port = stream->port;
750 	int srce_type;
751 
752 	if (port->flags & FLAG_MULT_PORT)
753 		srce_type = VIP_SRCE_MULT_PORT;
754 	else if (port->flags & FLAG_MULT_ANC)
755 		srce_type = VIP_SRCE_MULT_ANC;
756 	else if (v4l2_is_format_rgb(port->fmt->finfo))
757 		srce_type = VIP_SRCE_RGB;
758 	else
759 		srce_type = VIP_SRCE_LUMA;
760 
761 	add_out_dtd(stream, srce_type);
762 
763 	if (srce_type == VIP_SRCE_LUMA && port->fmt->coplanar)
764 		add_out_dtd(stream, VIP_SRCE_CHROMA);
765 }
766 
enable_irqs(struct vip_dev * dev,int irq_num,int list_num)767 static void enable_irqs(struct vip_dev *dev, int irq_num, int list_num)
768 {
769 	struct vip_parser_data *parser = dev->parser;
770 	u32 reg_addr = VIP_INT0_ENABLE0_SET +
771 			VIP_INTC_INTX_OFFSET * irq_num;
772 	u32 irq_val = (1 << (list_num * 2)) |
773 		      (VIP_VIP1_PARSER_INT << (irq_num * 1));
774 
775 	/* Enable Parser Interrupt */
776 	reg_write(parser, VIP_PARSER_FIQ_MASK, (u32)~PARSER_IRQ_MASK);
777 
778 	reg_write(dev->shared, reg_addr, irq_val);
779 
780 	vpdma_enable_list_complete_irq(dev->shared->vpdma,
781 				       irq_num, list_num, true);
782 }
783 
disable_irqs(struct vip_dev * dev,int irq_num,int list_num)784 static void disable_irqs(struct vip_dev *dev, int irq_num, int list_num)
785 {
786 	struct vip_parser_data *parser = dev->parser;
787 	u32 reg_addr = VIP_INT0_ENABLE0_CLR +
788 			VIP_INTC_INTX_OFFSET * irq_num;
789 	u32 irq_val = (1 << (list_num * 2)) |
790 		      (VIP_VIP1_PARSER_INT << (irq_num * 1));
791 
792 	/* Disable all Parser Interrupt */
793 	reg_write(parser, VIP_PARSER_FIQ_MASK, 0xffffffff);
794 
795 	reg_write(dev->shared, reg_addr, irq_val);
796 
797 	vpdma_enable_list_complete_irq(dev->shared->vpdma,
798 				       irq_num, list_num, false);
799 }
800 
clear_irqs(struct vip_dev * dev,int irq_num,int list_num)801 static void clear_irqs(struct vip_dev *dev, int irq_num, int list_num)
802 {
803 	struct vip_parser_data *parser = dev->parser;
804 	u32 reg_addr = VIP_INT0_STATUS0_CLR +
805 			VIP_INTC_INTX_OFFSET * irq_num;
806 	u32 irq_val = (1 << (list_num * 2)) |
807 		      (VIP_VIP1_PARSER_INT << (irq_num * 1));
808 
809 	/* Clear all Parser Interrupt */
810 	reg_write(parser, VIP_PARSER_FIQ_CLR, 0xffffffff);
811 	reg_write(parser, VIP_PARSER_FIQ_CLR, 0x0);
812 
813 	reg_write(dev->shared, reg_addr, irq_val);
814 
815 	vpdma_clear_list_stat(dev->shared->vpdma, irq_num, dev->slice_id);
816 }
817 
818 /*
819  * Quiesce recovery work and per-list IRQs before releasing stream resources.
820  * disable_work_sync() prevents the overflow handler from requeueing recovery
821  * work. Mask and synchronize IRQs afterwards because a running worker may
822  * have re-enabled them before exiting.
823  */
vip_quiesce_stream(struct vip_stream * stream)824 static void vip_quiesce_stream(struct vip_stream *stream)
825 {
826 	struct vip_dev *dev = stream->port->dev;
827 
828 	disable_work_sync(&stream->recovery_work);
829 	disable_irqs(dev, dev->slice_id, stream->list_num);
830 	clear_irqs(dev, dev->slice_id, stream->list_num);
831 	synchronize_irq(dev->irq);
832 }
833 
populate_desc_list(struct vip_stream * stream)834 static void populate_desc_list(struct vip_stream *stream)
835 {
836 	struct vip_port *port = stream->port;
837 	struct vip_dev *dev = port->dev;
838 
839 	stream->desc_next = stream->desc_list.buf.addr;
840 	add_stream_dtds(stream);
841 
842 	vpdma_map_desc_buf(dev->shared->vpdma, &stream->desc_list.buf);
843 }
844 
845 /*
846  * start_dma - adds descriptors to the dma list and submits them.
847  * Should be called after a new vb is queued and on a vpdma list
848  * completion interrupt.
849  */
start_dma(struct vip_stream * stream,struct vip_buffer * buf)850 static void start_dma(struct vip_stream *stream, struct vip_buffer *buf)
851 {
852 	struct vip_dev *dev = stream->port->dev;
853 	struct vpdma_data *vpdma = dev->shared->vpdma;
854 	int list_num = stream->list_num;
855 	dma_addr_t dma_addr;
856 	int drop_data;
857 
858 	if (vpdma_list_busy(vpdma, list_num)) {
859 		v4l2_err(&dev->v4l2_dev, "vpdma list busy, cannot post\n");
860 		return;				/* nothing to do */
861 	}
862 
863 	if (buf) {
864 		dma_addr = vb2_dma_contig_plane_dma_addr(&buf->vb.vb2_buf, 0);
865 		drop_data = 0;
866 		v4l2_dbg(4, debug, &dev->v4l2_dev, "%s: vb2 buf idx:%d, dma_addr:%pad\n",
867 			 __func__, buf->vb.vb2_buf.index, &dma_addr);
868 	} else {
869 		dma_addr = 0;
870 		drop_data = 1;
871 		v4l2_dbg(4, debug, &dev->v4l2_dev, "%s: dropped\n", __func__);
872 	}
873 
874 	vpdma_update_dma_addr(dev->shared->vpdma, &stream->desc_list,
875 			      dma_addr, stream->write_desc, drop_data, 0);
876 
877 	if (stream->port->fmt->coplanar) {
878 		dma_addr += stream->bytesperline * stream->height;
879 		vpdma_update_dma_addr(dev->shared->vpdma, &stream->desc_list,
880 				      dma_addr, stream->write_desc + 1,
881 				      drop_data, 1);
882 	}
883 
884 	vpdma_submit_descs(dev->shared->vpdma,
885 			   &stream->desc_list, stream->list_num);
886 }
887 
vip_schedule_next_buffer(struct vip_stream * stream)888 static void vip_schedule_next_buffer(struct vip_stream *stream)
889 {
890 	struct vip_dev *dev = stream->port->dev;
891 	struct vip_buffer *buf;
892 	unsigned long flags;
893 
894 	spin_lock_irqsave(&dev->slock, flags);
895 	if (list_empty(&stream->vidq)) {
896 		v4l2_dbg(4, debug, &dev->v4l2_dev, "Dropping frame\n");
897 		if (list_empty(&stream->dropq)) {
898 			v4l2_err(&dev->v4l2_dev, "No dropq buffer left!");
899 			spin_unlock_irqrestore(&dev->slock, flags);
900 			return;
901 		}
902 		buf = list_entry(stream->dropq.next,
903 				 struct vip_buffer, list);
904 
905 		buf->drop = true;
906 		list_move_tail(&buf->list, &stream->post_bufs);
907 		buf = NULL;
908 	} else {
909 		buf = list_entry(stream->vidq.next,
910 				 struct vip_buffer, list);
911 		buf->drop = false;
912 		list_move_tail(&buf->list, &stream->post_bufs);
913 		v4l2_dbg(4, debug, &dev->v4l2_dev, "added next buffer\n");
914 	}
915 
916 	spin_unlock_irqrestore(&dev->slock, flags);
917 	start_dma(stream, buf);
918 }
919 
vip_process_buffer_complete(struct vip_stream * stream)920 static void vip_process_buffer_complete(struct vip_stream *stream)
921 {
922 	struct vip_dev *dev = stream->port->dev;
923 	struct vip_buffer *buf;
924 	struct vb2_v4l2_buffer *vb = NULL;
925 	unsigned long flags, fld;
926 
927 	buf = list_first_entry_or_null(&stream->post_bufs, struct vip_buffer, list);
928 
929 	if (stream->port->flags & FLAG_INTERLACED) {
930 		vpdma_unmap_desc_buf(dev->shared->vpdma,
931 				     &stream->desc_list.buf);
932 
933 		fld = dtd_get_field(stream->write_desc);
934 		stream->field = fld ? V4L2_FIELD_BOTTOM : V4L2_FIELD_TOP;
935 
936 		vpdma_map_desc_buf(dev->shared->vpdma, &stream->desc_list.buf);
937 	}
938 
939 	if (buf) {
940 		vb = &buf->vb;
941 		vb->sequence = stream->sequence;
942 		vb->vb2_buf.timestamp = ktime_get_ns();
943 		vb->field = V4L2_FIELD_NONE;
944 
945 		if (buf->drop) {
946 			spin_lock_irqsave(&dev->slock, flags);
947 			list_move_tail(&buf->list, &stream->dropq);
948 			spin_unlock_irqrestore(&dev->slock, flags);
949 		} else {
950 			spin_lock_irqsave(&dev->slock, flags);
951 			list_del(&buf->list);
952 			spin_unlock_irqrestore(&dev->slock, flags);
953 			vb2_buffer_done(&vb->vb2_buf, VB2_BUF_STATE_DONE);
954 		}
955 	} else {
956 		v4l2_err(&dev->v4l2_dev, "%s: buf is null!!!\n", __func__);
957 		return;
958 	}
959 
960 	stream->sequence++;
961 }
962 
vip_reset_vpdma(struct vip_stream * stream)963 static int vip_reset_vpdma(struct vip_stream *stream)
964 {
965 	struct vip_port *port = stream->port;
966 	struct vip_dev *dev = port->dev;
967 	struct vip_buffer *buf;
968 	unsigned long flags;
969 
970 	stop_dma(stream, false);
971 
972 	spin_lock_irqsave(&dev->slock, flags);
973 	/* requeue all active buffers in the opposite order */
974 	while (!list_empty(&stream->post_bufs)) {
975 		buf = list_last_entry(&stream->post_bufs,
976 				      struct vip_buffer, list);
977 		list_del(&buf->list);
978 		if (buf->drop == 1) {
979 			list_add_tail(&buf->list, &stream->dropq);
980 			v4l2_dbg(4, debug, &dev->v4l2_dev, "requeueing drop buffer on dropq\n");
981 		} else {
982 			list_add(&buf->list, &stream->vidq);
983 			v4l2_dbg(4, debug, &dev->v4l2_dev, "requeueing vb2 buf idx:%d on vidq\n",
984 				 buf->vb.vb2_buf.index);
985 		}
986 	}
987 	spin_unlock_irqrestore(&dev->slock, flags);
988 
989 	/* Make sure the desc_list is unmapped */
990 	vpdma_unmap_desc_buf(dev->shared->vpdma, &stream->desc_list.buf);
991 
992 	return 0;
993 }
994 
vip_overflow_recovery_work(struct work_struct * work)995 static void vip_overflow_recovery_work(struct work_struct *work)
996 {
997 	struct vip_stream *stream = container_of(work, struct vip_stream,
998 						 recovery_work);
999 	struct vip_port *port = stream->port;
1000 	struct vip_dev *dev = port->dev;
1001 
1002 	v4l2_err(&dev->v4l2_dev, "%s: Port %c\n", __func__,
1003 		 port->port_id == VIP_PORTA ? 'A' : 'B');
1004 
1005 	disable_irqs(dev, dev->slice_id, stream->list_num);
1006 	clear_irqs(dev, dev->slice_id, stream->list_num);
1007 
1008 	/* 1.	Set VIP_XTRA6_PORT_A[31:16] YUV_SRCNUM_STOP_IMMEDIATELY */
1009 	/* 2.	Set VIP_XTRA6_PORT_A[15:0] ANC_SRCNUM_STOP_IMMEDIATELY */
1010 	vip_parser_stop_imm(port, 1);
1011 
1012 	/* 3.	Clear VIP_PORT_A[8] ENABLE */
1013 	/*
1014 	 * 4.	Set VIP_PORT_A[7] CLR_ASYNC_FIFO_RD
1015 	 *      Set VIP_PORT_A[6] CLR_ASYNC_FIFO_WR
1016 	 */
1017 	vip_enable_parser(port, false);
1018 
1019 	/* 5.	Set VIP_PORT_A[23] SW_RESET */
1020 	vip_reset_parser(port, 1);
1021 
1022 	/*
1023 	 * 6.	Reset other VIP modules
1024 	 *	For each module used downstream of VIP_PARSER, write 1 to the
1025 	 *      bit location of the VIP_CLKC_RST register which is connected
1026 	 *      to VIP_PARSER
1027 	 */
1028 	vip_module_toggle(dev, VIP_DP_RST, true);
1029 
1030 	usleep_range(200, 250);
1031 
1032 	/*
1033 	 * 7.	Abort VPDMA channels
1034 	 *	Write to list attribute to stop list 0
1035 	 *	Write to list address register location of abort list
1036 	 *	Write to list attribute register list 0 and size of abort list
1037 	 */
1038 	vip_reset_vpdma(stream);
1039 
1040 	/* 8.	Clear VIP_PORT_A[23] SW_RESET */
1041 	vip_reset_parser(port, 0);
1042 
1043 	/*
1044 	 * 9.	Un-reset other VIP modules
1045 	 *	For each module used downstream of VIP_PARSER, write 0 to
1046 	 *	the bit location of the VIP_CLKC_RST register which is
1047 	 *	connected to VIP_PARSER
1048 	 */
1049 	vip_module_toggle(dev, VIP_DP_RST, false);
1050 
1051 	/* 10.	(Delay) */
1052 	/* 11.	SC coeff downloaded (if VIP_SCALER is being used) */
1053 	vip_setup_scaler(stream);
1054 
1055 	/* 12.	(Delay) */
1056 		/* the above are not needed here yet */
1057 
1058 	populate_desc_list(stream);
1059 	stream->num_recovery++;
1060 	if (stream->num_recovery < 5) {
1061 		/* Reload the vpdma */
1062 		vip_load_vpdma_list_fifo(stream);
1063 
1064 		enable_irqs(dev, dev->slice_id, stream->list_num);
1065 		vip_schedule_next_buffer(stream);
1066 
1067 		/* 13.	Clear VIP_XTRA6_PORT_A[31:16] YUV_SRCNUM_STOP_IMM */
1068 		/* 14.	Clear VIP_XTRA6_PORT_A[15:0] ANC_SRCNUM_STOP_IMM */
1069 
1070 		vip_parser_stop_imm(port, 0);
1071 
1072 		/* 15.	Set VIP_PORT_A[8] ENABLE */
1073 		/*
1074 		 * 16.	Clear VIP_PORT_A[7] CLR_ASYNC_FIFO_RD
1075 		 *	Clear VIP_PORT_A[6] CLR_ASYNC_FIFO_WR
1076 		 */
1077 		vip_enable_parser(port, true);
1078 	} else {
1079 		v4l2_err(&dev->v4l2_dev, "%s: num_recovery limit exceeded leaving disabled\n",
1080 			 __func__);
1081 	}
1082 }
1083 
handle_parser_irqs(struct vip_dev * dev)1084 static void handle_parser_irqs(struct vip_dev *dev)
1085 {
1086 	struct vip_parser_data *parser = dev->parser;
1087 	struct vip_port *porta = dev->ports[VIP_PORTA];
1088 	struct vip_port *portb = dev->ports[VIP_PORTB];
1089 	struct vip_stream *stream = NULL;
1090 	u32 vip_irq_stat = reg_read(parser, VIP_PARSER_FIQ_STATUS);
1091 	int i;
1092 
1093 	v4l2_dbg(3, debug, &dev->v4l2_dev, "%s: FIQ_STATUS: 0x%08x\n", __func__, vip_irq_stat);
1094 
1095 	/* Clear all Parser Interrupt */
1096 	reg_write(parser, VIP_PARSER_FIQ_CLR, vip_irq_stat);
1097 	reg_write(parser, VIP_PARSER_FIQ_CLR, 0x0);
1098 
1099 	#ifdef DEBUG
1100 	if (vip_irq_stat & VIP_PORTA_VDET)
1101 		v4l2_dbg(3, debug, &dev->v4l2_dev, "VIP_PORTA_VDET\n");
1102 	if (vip_irq_stat & VIP_PORTB_VDET)
1103 		v4l2_dbg(3, debug, &dev->v4l2_dev, "VIP_PORTB_VDET\n");
1104 	if (vip_irq_stat & VIP_PORTA_ASYNC_FIFO_OF)
1105 		v4l2_err(&dev->v4l2_dev, "VIP_PORTA_ASYNC_FIFO_OF\n");
1106 	if (vip_irq_stat & VIP_PORTB_ASYNC_FIFO_OF)
1107 		v4l2_err(&dev->v4l2_dev, "VIP_PORTB_ASYNC_FIFO_OF\n");
1108 	if (vip_irq_stat & VIP_PORTA_OUTPUT_FIFO_YUV)
1109 		v4l2_err(&dev->v4l2_dev, "VIP_PORTA_OUTPUT_FIFO_YUV\n");
1110 	if (vip_irq_stat & VIP_PORTA_OUTPUT_FIFO_ANC)
1111 		v4l2_err(&dev->v4l2_dev, "VIP_PORTA_OUTPUT_FIFO_ANC\n");
1112 	if (vip_irq_stat & VIP_PORTB_OUTPUT_FIFO_YUV)
1113 		v4l2_err(&dev->v4l2_dev, "VIP_PORTB_OUTPUT_FIFO_YUV\n");
1114 	if (vip_irq_stat & VIP_PORTB_OUTPUT_FIFO_ANC)
1115 		v4l2_err(&dev->v4l2_dev, "VIP_PORTB_OUTPUT_FIFO_ANC\n");
1116 	if (vip_irq_stat & VIP_PORTA_CONN)
1117 		v4l2_dbg(3, debug, &dev->v4l2_dev, "VIP_PORTA_CONN\n");
1118 	if (vip_irq_stat & VIP_PORTA_DISCONN)
1119 		v4l2_dbg(3, debug, &dev->v4l2_dev, "VIP_PORTA_DISCONN\n");
1120 	if (vip_irq_stat & VIP_PORTB_CONN)
1121 		v4l2_dbg(3, debug, &dev->v4l2_dev, "VIP_PORTB_CONN\n");
1122 	if (vip_irq_stat & VIP_PORTB_DISCONN)
1123 		v4l2_dbg(3, debug, &dev->v4l2_dev, "VIP_PORTB_DISCONN\n");
1124 	if (vip_irq_stat & VIP_PORTA_SRC0_SIZE)
1125 		v4l2_dbg(3, debug, &dev->v4l2_dev, "VIP_PORTA_SRC0_SIZE\n");
1126 	if (vip_irq_stat & VIP_PORTB_SRC0_SIZE)
1127 		v4l2_dbg(3, debug, &dev->v4l2_dev, "VIP_PORTB_SRC0_SIZE\n");
1128 	if (vip_irq_stat & VIP_PORTA_YUV_PROTO_VIOLATION)
1129 		v4l2_dbg(3, debug, &dev->v4l2_dev, "VIP_PORTA_YUV_PROTO_VIOLATION\n");
1130 	if (vip_irq_stat & VIP_PORTA_ANC_PROTO_VIOLATION)
1131 		v4l2_dbg(3, debug, &dev->v4l2_dev, "VIP_PORTA_ANC_PROTO_VIOLATION\n");
1132 	if (vip_irq_stat & VIP_PORTB_YUV_PROTO_VIOLATION)
1133 		v4l2_dbg(3, debug, &dev->v4l2_dev, "VIP_PORTB_YUV_PROTO_VIOLATION\n");
1134 	if (vip_irq_stat & VIP_PORTB_ANC_PROTO_VIOLATION)
1135 		v4l2_dbg(3, debug, &dev->v4l2_dev, "VIP_PORTB_ANC_PROTO_VIOLATION\n");
1136 	if (vip_irq_stat & VIP_PORTA_CFG_DISABLE_COMPLETE)
1137 		v4l2_dbg(3, debug, &dev->v4l2_dev, "VIP_PORTA_CFG_DISABLE_COMPLETE\n");
1138 	if (vip_irq_stat & VIP_PORTB_CFG_DISABLE_COMPLETE)
1139 		v4l2_dbg(3, debug, &dev->v4l2_dev, "VIP_PORTB_CFG_DISABLE_COMPLETE\n");
1140 	#endif
1141 
1142 	if (vip_irq_stat & (VIP_PORTA_ASYNC_FIFO_OF |
1143 			VIP_PORTA_OUTPUT_FIFO_YUV |
1144 			VIP_PORTA_OUTPUT_FIFO_ANC)) {
1145 		for (i = 0; i < VIP_CAP_STREAMS_PER_PORT; i++) {
1146 			if (porta->cap_streams[i] &&
1147 			    porta->cap_streams[i]->port->port_id ==
1148 			    porta->port_id) {
1149 				stream = porta->cap_streams[i];
1150 				break;
1151 			}
1152 		}
1153 		if (stream) {
1154 			disable_irqs(dev, dev->slice_id,
1155 				     stream->list_num);
1156 			schedule_work(&stream->recovery_work);
1157 			return;
1158 		}
1159 	}
1160 	if (vip_irq_stat & (VIP_PORTB_ASYNC_FIFO_OF |
1161 			VIP_PORTB_OUTPUT_FIFO_YUV |
1162 			VIP_PORTB_OUTPUT_FIFO_ANC)) {
1163 		for (i = 0; i < VIP_CAP_STREAMS_PER_PORT; i++) {
1164 			if (portb->cap_streams[i] &&
1165 			    portb->cap_streams[i]->port->port_id ==
1166 			    portb->port_id) {
1167 				stream = portb->cap_streams[i];
1168 				break;
1169 			}
1170 		}
1171 		if (stream) {
1172 			disable_irqs(dev, dev->slice_id,
1173 				     stream->list_num);
1174 			schedule_work(&stream->recovery_work);
1175 			return;
1176 		}
1177 	}
1178 }
1179 
vip_irq(int irq_vip,void * data)1180 static irqreturn_t vip_irq(int irq_vip, void *data)
1181 {
1182 	struct vip_dev *dev = (struct vip_dev *)data;
1183 	struct vpdma_data *vpdma;
1184 	struct vip_stream *stream;
1185 	int list_num;
1186 	int irq_num = dev->slice_id;
1187 	u32 irqst, irqst_saved, reg_addr;
1188 
1189 	if (!dev->shared)
1190 		return IRQ_HANDLED;
1191 
1192 	vpdma = dev->shared->vpdma;
1193 	reg_addr = VIP_INT0_STATUS0 +
1194 			VIP_INTC_INTX_OFFSET * irq_num;
1195 	irqst_saved = reg_read(dev->shared, reg_addr);
1196 	irqst = irqst_saved;
1197 
1198 	v4l2_dbg(8, debug, &dev->v4l2_dev, "IRQ %d VIP_INT%d_STATUS0 0x%x\n",
1199 		 irq_vip, irq_num, irqst);
1200 	if (irqst) {
1201 		if (irqst & (VIP_VIP1_PARSER_INT << (irq_num * 1))) {
1202 			irqst &= ~(VIP_VIP1_PARSER_INT << (irq_num * 1));
1203 			handle_parser_irqs(dev);
1204 		}
1205 
1206 		for (list_num = 0; irqst && (list_num < 8);  list_num++) {
1207 			/* Check for LIST_COMPLETE IRQ */
1208 			if (!(irqst & (1 << list_num * 2)))
1209 				continue;
1210 
1211 			v4l2_dbg(8, debug, &dev->v4l2_dev, "IRQ %d: handling LIST%d_COMPLETE\n",
1212 				 irq_num, list_num);
1213 
1214 			stream = vpdma_hwlist_get_priv(vpdma, list_num);
1215 			if (!stream || stream->list_num != list_num) {
1216 				v4l2_err(&dev->v4l2_dev, "IRQ occurred for unused list");
1217 				continue;
1218 			}
1219 
1220 			vpdma_clear_list_stat(vpdma, irq_num, list_num);
1221 
1222 			vip_process_buffer_complete(stream);
1223 
1224 			vip_schedule_next_buffer(stream);
1225 
1226 			irqst &= ~((1 << list_num * 2));
1227 		}
1228 	}
1229 
1230 	/* Acknowledge that we are done with all interrupts */
1231 	reg_write(dev->shared, VIP_INTC_E0I, 1 << irq_num);
1232 
1233 	/* Clear handled events from status register */
1234 	reg_addr = VIP_INT0_STATUS0_CLR +
1235 		   VIP_INTC_INTX_OFFSET * irq_num;
1236 	reg_write(dev->shared, reg_addr, irqst_saved);
1237 
1238 	return IRQ_HANDLED;
1239 }
1240 
1241 /*
1242  * video ioctls
1243  */
vip_querycap(struct file * file,void * priv,struct v4l2_capability * cap)1244 static int vip_querycap(struct file *file, void *priv,
1245 			struct v4l2_capability *cap)
1246 {
1247 	strscpy(cap->driver, VIP_MODULE_NAME, sizeof(cap->driver));
1248 	strscpy(cap->card, VIP_MODULE_NAME, sizeof(cap->card));
1249 	return 0;
1250 }
1251 
vip_enuminput(struct file * file,void * priv,struct v4l2_input * inp)1252 static int vip_enuminput(struct file *file, void *priv,
1253 			 struct v4l2_input *inp)
1254 {
1255 	struct vip_stream *stream = file2stream(file);
1256 
1257 	if (inp->index)
1258 		return -EINVAL;
1259 
1260 	inp->type = V4L2_INPUT_TYPE_CAMERA;
1261 	inp->std = stream->vfd->tvnorms;
1262 	sprintf(inp->name, "Camera %u", stream->vfd->num);
1263 
1264 	return 0;
1265 }
1266 
vip_g_input(struct file * file,void * priv,unsigned int * i)1267 static int vip_g_input(struct file *file, void *priv, unsigned int *i)
1268 {
1269 	*i = 0;
1270 	return 0;
1271 }
1272 
vip_s_input(struct file * file,void * priv,unsigned int i)1273 static int vip_s_input(struct file *file, void *priv, unsigned int i)
1274 {
1275 	if (i != 0)
1276 		return -EINVAL;
1277 	return 0;
1278 }
1279 
vip_querystd(struct file * file,void * fh,v4l2_std_id * std)1280 static int vip_querystd(struct file *file, void *fh, v4l2_std_id *std)
1281 {
1282 	struct vip_stream *stream = file2stream(file);
1283 	struct vip_port *port = stream->port;
1284 
1285 	*std = stream->vfd->tvnorms;
1286 	v4l2_subdev_call(port->subdev, video, querystd, std);
1287 	return 0;
1288 }
1289 
vip_g_std(struct file * file,void * fh,v4l2_std_id * std)1290 static int vip_g_std(struct file *file, void *fh, v4l2_std_id *std)
1291 {
1292 	struct vip_stream *stream = file2stream(file);
1293 	struct vip_port *port = stream->port;
1294 
1295 	*std = stream->vfd->tvnorms;
1296 	v4l2_subdev_call(port->subdev, video, g_std, std);
1297 
1298 	return 0;
1299 }
1300 
vip_s_std(struct file * file,void * fh,v4l2_std_id std)1301 static int vip_s_std(struct file *file, void *fh, v4l2_std_id std)
1302 {
1303 	struct vip_stream *stream = file2stream(file);
1304 	struct vip_port *port = stream->port;
1305 
1306 	if (stream->vfd->tvnorms == std)
1307 		return 0;
1308 
1309 	if (!(std & stream->vfd->tvnorms))
1310 		return -EINVAL;
1311 
1312 	if (vb2_is_busy(&stream->vb_vidq))
1313 		return -EBUSY;
1314 
1315 	v4l2_subdev_call(port->subdev, video, s_std, std);
1316 	return 0;
1317 }
1318 
vip_enum_fmt_vid_cap(struct file * file,void * priv,struct v4l2_fmtdesc * f)1319 static int vip_enum_fmt_vid_cap(struct file *file, void *priv,
1320 				struct v4l2_fmtdesc *f)
1321 {
1322 	struct vip_stream *stream = file2stream(file);
1323 	struct vip_port *port = stream->port;
1324 	struct vip_fmt *fmt;
1325 
1326 	if (f->index >= port->num_active_fmt)
1327 		return -EINVAL;
1328 
1329 	fmt = port->active_fmt[f->index];
1330 	f->pixelformat = fmt->fourcc;
1331 
1332 	return 0;
1333 }
1334 
vip_enum_framesizes(struct file * file,void * priv,struct v4l2_frmsizeenum * f)1335 static int vip_enum_framesizes(struct file *file, void *priv,
1336 			       struct v4l2_frmsizeenum *f)
1337 {
1338 	struct vip_stream *stream = file2stream(file);
1339 	struct vip_port *port = stream->port;
1340 	struct vip_dev *dev = port->dev;
1341 	struct vip_fmt *fmt;
1342 	int ret;
1343 	struct v4l2_subdev_frame_size_enum fse = {
1344 		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
1345 		.pad = 0,
1346 	};
1347 
1348 	fmt = find_port_format_by_pix(port, f->pixel_format);
1349 	if (!fmt)
1350 		return -EINVAL;
1351 
1352 	fse.index = f->index;
1353 	fse.code = fmt->code;
1354 	ret = v4l2_subdev_call(port->subdev, pad, enum_frame_size, NULL, &fse);
1355 	if (ret)
1356 		return -EINVAL;
1357 
1358 	v4l2_dbg(1, debug, &dev->v4l2_dev, "%s: index: %d code: %x W:[%d,%d] H:[%d,%d]\n",
1359 		 __func__, fse.index, fse.code, fse.min_width, fse.max_width,
1360 		 fse.min_height, fse.max_height);
1361 
1362 	f->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1363 	f->discrete.width = fse.max_width;
1364 	f->discrete.height = fse.max_height;
1365 
1366 	return 0;
1367 }
1368 
vip_enum_frameintervals(struct file * file,void * priv,struct v4l2_frmivalenum * f)1369 static int vip_enum_frameintervals(struct file *file, void *priv,
1370 				   struct v4l2_frmivalenum *f)
1371 {
1372 	struct vip_stream *stream = file2stream(file);
1373 	struct vip_port *port = stream->port;
1374 	struct vip_fmt *fmt;
1375 	struct v4l2_subdev_frame_interval_enum fie = {
1376 		.index = f->index,
1377 		.width = f->width,
1378 		.height = f->height,
1379 		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
1380 	};
1381 	int ret;
1382 
1383 	fmt = find_port_format_by_pix(port, f->pixel_format);
1384 	if (!fmt)
1385 		return -EINVAL;
1386 
1387 	fie.code = fmt->code;
1388 	ret = v4l2_subdev_call(port->subdev, pad, enum_frame_interval,
1389 			       NULL, &fie);
1390 	if (ret)
1391 		return ret;
1392 	f->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1393 	f->discrete = fie.interval;
1394 
1395 	return 0;
1396 }
1397 
vip_g_parm(struct file * file,void * fh,struct v4l2_streamparm * a)1398 static int vip_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
1399 {
1400 	struct vip_stream *stream = video_drvdata(file);
1401 	struct vip_port *port = stream->port;
1402 
1403 	return v4l2_g_parm_cap(video_devdata(file), port->subdev, a);
1404 }
1405 
vip_s_parm(struct file * file,void * fh,struct v4l2_streamparm * a)1406 static int vip_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
1407 {
1408 	struct vip_stream *stream = video_drvdata(file);
1409 	struct vip_port *port = stream->port;
1410 
1411 	return v4l2_s_parm_cap(video_devdata(file), port->subdev, a);
1412 }
1413 
vip_calc_format_size(struct vip_port * port,struct vip_fmt * fmt,struct v4l2_format * f)1414 static int vip_calc_format_size(struct vip_port *port,
1415 				struct vip_fmt *fmt,
1416 				struct v4l2_format *f)
1417 {
1418 	enum v4l2_field *field;
1419 	unsigned int stride;
1420 	struct vip_dev *dev = port->dev;
1421 
1422 	if (!fmt) {
1423 		v4l2_dbg(2, debug, &dev->v4l2_dev,
1424 			 "no vip_fmt format provided!\n");
1425 		return -EINVAL;
1426 	}
1427 
1428 	field = &f->fmt.pix.field;
1429 	if (*field == V4L2_FIELD_ANY)
1430 		*field = V4L2_FIELD_NONE;
1431 	else if (V4L2_FIELD_NONE != *field && V4L2_FIELD_ALTERNATE != *field)
1432 		return -EINVAL;
1433 
1434 	v4l_bound_align_image(&f->fmt.pix.width, MIN_W, MAX_W, W_ALIGN,
1435 			      &f->fmt.pix.height, MIN_H, MAX_H, H_ALIGN,
1436 			      S_ALIGN);
1437 
1438 	stride = f->fmt.pix.width * (fmt->vpdma_fmt[0]->depth >> 3);
1439 	f->fmt.pix.bytesperline = ALIGN(stride, VPDMA_STRIDE_ALIGN);
1440 
1441 	f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
1442 	if (fmt->coplanar) {
1443 		f->fmt.pix.sizeimage += f->fmt.pix.height *
1444 					f->fmt.pix.bytesperline *
1445 					fmt->vpdma_fmt[VIP_CHROMA]->depth >> 3;
1446 	}
1447 
1448 	f->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
1449 
1450 	v4l2_dbg(3, debug, &dev->v4l2_dev, "calc_format_size: fourcc:%s size: %dx%d bpl:%d img_size:%d\n",
1451 		 fourcc_to_str(f->fmt.pix.pixelformat),
1452 		 f->fmt.pix.width, f->fmt.pix.height,
1453 		 f->fmt.pix.bytesperline, f->fmt.pix.sizeimage);
1454 
1455 	return 0;
1456 }
1457 
vip_is_size_dma_aligned(u32 bpp,u32 width)1458 static inline bool vip_is_size_dma_aligned(u32 bpp, u32 width)
1459 {
1460 	return ((width * bpp) == ALIGN(width * bpp, VPDMA_STRIDE_ALIGN));
1461 }
1462 
vip_try_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)1463 static int vip_try_fmt_vid_cap(struct file *file, void *priv,
1464 			       struct v4l2_format *f)
1465 {
1466 	struct vip_stream *stream = file2stream(file);
1467 	struct vip_port *port = stream->port;
1468 	struct vip_dev *dev = port->dev;
1469 	struct vip_fmt *fmt;
1470 	u32 best_width, best_height, largest_width, largest_height;
1471 	int ret, found;
1472 	enum vip_csc_state csc_direction;
1473 	struct v4l2_subdev_frame_size_enum fse = {
1474 		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
1475 		.pad = 0,
1476 	};
1477 
1478 	fmt = find_port_format_by_pix(port, f->fmt.pix.pixelformat);
1479 	if (!fmt) {
1480 		/* Just get the first one enumerated */
1481 		fmt = port->active_fmt[0];
1482 		f->fmt.pix.pixelformat = fmt->fourcc;
1483 	}
1484 
1485 	csc_direction =  vip_csc_direction(fmt->code, fmt->finfo);
1486 	if (csc_direction != VIP_CSC_NA) {
1487 		if (!is_csc_available(port)) {
1488 			v4l2_dbg(2, debug, &dev->v4l2_dev,
1489 				 "CSC not available for Fourcc format (0x%08x).\n",
1490 				 f->fmt.pix.pixelformat);
1491 
1492 			/* Just get the first one enumerated */
1493 			fmt = port->active_fmt[0];
1494 			f->fmt.pix.pixelformat = fmt->fourcc;
1495 			/* re-evaluate the csc_direction here */
1496 			csc_direction =  vip_csc_direction(fmt->code,
1497 							   fmt->finfo);
1498 		} else {
1499 			v4l2_dbg(3, debug, &dev->v4l2_dev, "CSC active on Port %c: going %s\n",
1500 				 port->port_id == VIP_PORTA ? 'A' : 'B',
1501 				 (csc_direction == VIP_CSC_Y2R) ? "Y2R" : "R2Y");
1502 		}
1503 	}
1504 
1505 	/*
1506 	 * Given that sensors might support multiple mbus code we need
1507 	 * to use the one that matches the requested pixel format
1508 	 */
1509 	port->try_mbus_framefmt = port->mbus_framefmt;
1510 	port->try_mbus_framefmt.code = fmt->code;
1511 
1512 	/* check for/find a valid width/height */
1513 	ret = 0;
1514 	found = false;
1515 	best_width = 0;
1516 	best_height = 0;
1517 	largest_width = 0;
1518 	largest_height = 0;
1519 
1520 	fse.code = fmt->code;
1521 	for (fse.index = 0; ; fse.index++) {
1522 		u32 bpp = fmt->vpdma_fmt[0]->depth >> 3;
1523 
1524 		ret = v4l2_subdev_call(port->subdev, pad,
1525 				       enum_frame_size, NULL, &fse);
1526 		if (ret)
1527 			break;
1528 
1529 		if (!vip_is_size_dma_aligned(bpp, fse.max_width))
1530 			continue;
1531 
1532 		if (fse.max_width >= largest_width &&
1533 		    fse.max_height >= largest_height) {
1534 			v4l2_dbg(3, debug, &dev->v4l2_dev, "try_fmt loop:%d found new larger: %dx%d\n",
1535 				 fse.index, fse.max_width, fse.max_height);
1536 			largest_width = fse.max_width;
1537 			largest_height = fse.max_height;
1538 		}
1539 
1540 		if (fse.max_width >= f->fmt.pix.width &&
1541 		    fse.max_height >= f->fmt.pix.height) {
1542 			v4l2_dbg(3, debug, &dev->v4l2_dev, "try_fmt loop:%d found at least larger: %dx%d\n",
1543 				 fse.index, fse.max_width, fse.max_height);
1544 
1545 			if (!best_width ||
1546 			    ((abs(best_width - f->fmt.pix.width) >=
1547 			      abs(fse.max_width - f->fmt.pix.width)) &&
1548 			     (abs(best_height - f->fmt.pix.height) >=
1549 			      abs(fse.max_height - f->fmt.pix.height)))) {
1550 				best_width = fse.max_width;
1551 				best_height = fse.max_height;
1552 				v4l2_dbg(3, debug, &dev->v4l2_dev, "try_fmt loop:%d found new best: %dx%d\n",
1553 					 fse.index, fse.max_width,
1554 					 fse.max_height);
1555 			}
1556 		}
1557 
1558 		if (f->fmt.pix.width == fse.max_width &&
1559 		    f->fmt.pix.height == fse.max_height) {
1560 			found = true;
1561 			v4l2_dbg(3, debug, &dev->v4l2_dev, "try_fmt loop:%d found direct match: %dx%d\n",
1562 				 fse.index, fse.max_width,
1563 				 fse.max_height);
1564 			break;
1565 		}
1566 
1567 		if (f->fmt.pix.width >= fse.min_width &&
1568 		    f->fmt.pix.width <= fse.max_width &&
1569 		    f->fmt.pix.height >= fse.min_height &&
1570 		    f->fmt.pix.height <= fse.max_height) {
1571 			found = true;
1572 			v4l2_dbg(3, debug, &dev->v4l2_dev, "try_fmt loop:%d found direct range match: %dx%d\n",
1573 				 fse.index, fse.max_width,
1574 				 fse.max_height);
1575 			break;
1576 		}
1577 	}
1578 
1579 	if (found) {
1580 		port->try_mbus_framefmt.width = f->fmt.pix.width;
1581 		port->try_mbus_framefmt.height = f->fmt.pix.height;
1582 		/* No need to check for scaling */
1583 		goto calc_size;
1584 	} else if (f->fmt.pix.width > largest_width) {
1585 		port->try_mbus_framefmt.width = largest_width;
1586 		port->try_mbus_framefmt.height = largest_height;
1587 	} else if (best_width) {
1588 		port->try_mbus_framefmt.width = best_width;
1589 		port->try_mbus_framefmt.height = best_height;
1590 	} else {
1591 		/* use existing values as default */
1592 	}
1593 
1594 	v4l2_dbg(3, debug, &dev->v4l2_dev, "try_fmt best subdev size: %dx%d\n",
1595 		 port->try_mbus_framefmt.width,
1596 		 port->try_mbus_framefmt.height);
1597 
1598 	if (is_scaler_available(port) &&
1599 	    csc_direction != VIP_CSC_Y2R &&
1600 	    !vip_is_mbuscode_raw(fmt->code) &&
1601 	    f->fmt.pix.height <= port->try_mbus_framefmt.height &&
1602 	    port->try_mbus_framefmt.height <= SC_MAX_PIXEL_HEIGHT &&
1603 	    port->try_mbus_framefmt.width <= SC_MAX_PIXEL_WIDTH) {
1604 		/*
1605 		 * Scaler is only accessible if the dst colorspace is YUV.
1606 		 * As the input to the scaler must be in YUV mode only.
1607 		 *
1608 		 * Scaling up is allowed only horizontally.
1609 		 */
1610 		unsigned int hratio, vratio, width_align, height_align;
1611 		u32 bpp = fmt->vpdma_fmt[0]->depth >> 3;
1612 
1613 		v4l2_dbg(3, debug, &dev->v4l2_dev, "Scaler active on Port %c: requesting %dx%d\n",
1614 			 port->port_id == VIP_PORTA ? 'A' : 'B',
1615 			 f->fmt.pix.width, f->fmt.pix.height);
1616 
1617 		/* Just make sure everything is properly aligned */
1618 		width_align = ALIGN(f->fmt.pix.width * bpp, VPDMA_STRIDE_ALIGN);
1619 		width_align /= bpp;
1620 		height_align = ALIGN(f->fmt.pix.height, 2);
1621 
1622 		f->fmt.pix.width = width_align;
1623 		f->fmt.pix.height = height_align;
1624 
1625 		hratio = f->fmt.pix.width * 1000 /
1626 			 port->try_mbus_framefmt.width;
1627 		vratio = f->fmt.pix.height * 1000 /
1628 			 port->try_mbus_framefmt.height;
1629 		if (hratio < 125) {
1630 			f->fmt.pix.width = port->try_mbus_framefmt.width / 8;
1631 			v4l2_dbg(3, debug, &dev->v4l2_dev, "Horizontal scaling ratio out of range adjusting -> %d\n",
1632 				 f->fmt.pix.width);
1633 		}
1634 
1635 		if (vratio < 188) {
1636 			f->fmt.pix.height = port->try_mbus_framefmt.height / 4;
1637 			v4l2_dbg(3, debug, &dev->v4l2_dev, "Vertical scaling ratio out of range adjusting -> %d\n",
1638 				 f->fmt.pix.height);
1639 		}
1640 		v4l2_dbg(3, debug, &dev->v4l2_dev, "Scaler: got %dx%d\n",
1641 			 f->fmt.pix.width, f->fmt.pix.height);
1642 	} else {
1643 		/* use existing values as default */
1644 		f->fmt.pix.width = port->try_mbus_framefmt.width;
1645 		f->fmt.pix.height = port->try_mbus_framefmt.height;
1646 	}
1647 
1648 calc_size:
1649 	/* That we have a fmt calculate imagesize and bytesperline */
1650 	return vip_calc_format_size(port, fmt, f);
1651 }
1652 
vip_g_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)1653 static int vip_g_fmt_vid_cap(struct file *file, void *priv,
1654 			     struct v4l2_format *f)
1655 {
1656 	struct vip_stream *stream = file2stream(file);
1657 	struct vip_port *port = stream->port;
1658 
1659 	/* Use last known values or defaults */
1660 	f->fmt.pix.width	= stream->width;
1661 	f->fmt.pix.height	= stream->height;
1662 	f->fmt.pix.pixelformat	= port->fmt->fourcc;
1663 	f->fmt.pix.field	= stream->sup_field;
1664 	f->fmt.pix.colorspace   = V4L2_COLORSPACE_SRGB;
1665 	f->fmt.pix.bytesperline	= stream->bytesperline;
1666 	f->fmt.pix.sizeimage	= stream->sizeimage;
1667 
1668 	return 0;
1669 }
1670 
vip_s_fmt_vid_cap(struct file * file,void * priv,struct v4l2_format * f)1671 static int vip_s_fmt_vid_cap(struct file *file, void *priv,
1672 			     struct v4l2_format *f)
1673 {
1674 	struct vip_stream *stream = file2stream(file);
1675 	struct vip_port *port = stream->port;
1676 	struct vip_dev *dev = port->dev;
1677 	struct v4l2_subdev_format sfmt;
1678 	struct v4l2_mbus_framefmt *mf;
1679 	enum vip_csc_state csc_direction;
1680 	int ret;
1681 
1682 	ret = vip_try_fmt_vid_cap(file, priv, f);
1683 	if (ret)
1684 		return ret;
1685 
1686 	if (vb2_is_busy(&stream->vb_vidq)) {
1687 		v4l2_err(&dev->v4l2_dev, "%s queue busy\n", __func__);
1688 		return -EBUSY;
1689 	}
1690 
1691 	/*
1692 	 * Check if we need the scaler or not
1693 	 *
1694 	 * Since on previous S_FMT call the scaler might have been
1695 	 * allocated if it is not needed in this instance we will
1696 	 * attempt to free it just in case.
1697 	 *
1698 	 * free_scaler() is harmless unless the current port
1699 	 * allocated it.
1700 	 */
1701 	if (f->fmt.pix.width == port->try_mbus_framefmt.width &&
1702 	    f->fmt.pix.height == port->try_mbus_framefmt.height)
1703 		free_scaler(port);
1704 	else
1705 		allocate_scaler(port);
1706 
1707 	port->fmt = find_port_format_by_pix(port,
1708 					    f->fmt.pix.pixelformat);
1709 	stream->width		= f->fmt.pix.width;
1710 	stream->height		= f->fmt.pix.height;
1711 	stream->bytesperline	= f->fmt.pix.bytesperline;
1712 	stream->sizeimage	= f->fmt.pix.sizeimage;
1713 	stream->sup_field	= f->fmt.pix.field;
1714 	stream->field		= f->fmt.pix.field;
1715 
1716 	port->c_rect.left	= 0;
1717 	port->c_rect.top	= 0;
1718 	port->c_rect.width	= stream->width;
1719 	port->c_rect.height	= stream->height;
1720 
1721 	/*
1722 	 * Check if we need the csc unit or not
1723 	 *
1724 	 * Since on previous S_FMT call, the csc might have been
1725 	 * allocated if it is not needed in this instance we will
1726 	 * attempt to free it just in case.
1727 	 *
1728 	 * free_csc() is harmless unless the current port
1729 	 * allocated it.
1730 	 */
1731 	csc_direction =  vip_csc_direction(port->fmt->code, port->fmt->finfo);
1732 	if (csc_direction == VIP_CSC_NA)
1733 		free_csc(port);
1734 	else
1735 		allocate_csc(port, csc_direction);
1736 
1737 	if (stream->sup_field == V4L2_FIELD_ALTERNATE)
1738 		port->flags |= FLAG_INTERLACED;
1739 	else
1740 		port->flags &= ~FLAG_INTERLACED;
1741 
1742 	memset(&sfmt, 0, sizeof(sfmt));
1743 	mf = &sfmt.format;
1744 	v4l2_fill_mbus_format(mf, &f->fmt.pix, port->fmt->code);
1745 	/* Make sure to use the subdev size found in the try_fmt */
1746 	mf->width = port->try_mbus_framefmt.width;
1747 	mf->height = port->try_mbus_framefmt.height;
1748 
1749 	sfmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1750 	sfmt.pad = 0;
1751 	ret = v4l2_subdev_call(port->subdev, pad, set_fmt, NULL, &sfmt);
1752 	if (ret) {
1753 		v4l2_dbg(1, debug, &dev->v4l2_dev, "set_fmt failed in subdev\n");
1754 		return ret;
1755 	}
1756 
1757 	/* Save it */
1758 	port->mbus_framefmt = *mf;
1759 
1760 	v4l2_dbg(3, debug, &dev->v4l2_dev, "s_fmt subdev fmt mbus_code: %04X size: %dx%d\n",
1761 		 port->mbus_framefmt.code,
1762 		 port->mbus_framefmt.width, port->mbus_framefmt.height);
1763 
1764 	return 0;
1765 }
1766 
vip_unset_csc_y2r(struct vip_dev * dev,struct vip_port * port)1767 static void vip_unset_csc_y2r(struct vip_dev *dev, struct vip_port *port)
1768 {
1769 	if (port->port_id == VIP_PORTA) {
1770 		vip_set_slice_path(dev, VIP_CSC_SRC_DATA_SELECT, 0);
1771 		vip_set_slice_path(dev, VIP_MULTI_CHANNEL_DATA_SELECT, 0);
1772 		vip_set_slice_path(dev, VIP_RGB_OUT_HI_DATA_SELECT, 0);
1773 		vip_set_slice_path(dev, VIP_RGB_SRC_DATA_SELECT, 0);
1774 	} else {
1775 		vip_set_slice_path(dev, VIP_CSC_SRC_DATA_SELECT, 0);
1776 		vip_set_slice_path(dev, VIP_MULTI_CHANNEL_DATA_SELECT, 0);
1777 		vip_set_slice_path(dev, VIP_RGB_OUT_LO_DATA_SELECT, 0);
1778 	}
1779 }
1780 
vip_unset_csc_r2y(struct vip_dev * dev,struct vip_port * port)1781 static void vip_unset_csc_r2y(struct vip_dev *dev, struct vip_port *port)
1782 {
1783 	if (port->port_id != VIP_PORTA) {
1784 		v4l2_err(&dev->v4l2_dev, "RGB sensor can only be on Port A\n");
1785 		return;
1786 	}
1787 
1788 	if (port->scaler && port->fmt->coplanar) {
1789 		vip_set_slice_path(dev, VIP_CSC_SRC_DATA_SELECT, 0);
1790 		vip_set_slice_path(dev, VIP_SC_SRC_DATA_SELECT, 0);
1791 		vip_set_slice_path(dev, VIP_CHR_DS_1_SRC_DATA_SELECT, 0);
1792 		vip_set_slice_path(dev, VIP_CHR_DS_1_DATA_BYPASS, 0);
1793 		vip_set_slice_path(dev, VIP_RGB_OUT_HI_DATA_SELECT, 0);
1794 	} else if (port->scaler) {
1795 		vip_set_slice_path(dev, VIP_CSC_SRC_DATA_SELECT, 0);
1796 		vip_set_slice_path(dev, VIP_SC_SRC_DATA_SELECT, 0);
1797 		vip_set_slice_path(dev, VIP_CHR_DS_1_SRC_DATA_SELECT, 0);
1798 		vip_set_slice_path(dev, VIP_CHR_DS_1_DATA_BYPASS, 0);
1799 		vip_set_slice_path(dev, VIP_RGB_OUT_HI_DATA_SELECT, 0);
1800 	} else if (port->fmt->coplanar) {
1801 		vip_set_slice_path(dev, VIP_CSC_SRC_DATA_SELECT, 0);
1802 		vip_set_slice_path(dev, VIP_CHR_DS_1_SRC_DATA_SELECT, 0);
1803 		vip_set_slice_path(dev, VIP_CHR_DS_1_DATA_BYPASS, 0);
1804 		vip_set_slice_path(dev, VIP_RGB_OUT_HI_DATA_SELECT, 0);
1805 	}
1806 }
1807 
vip_unset_rgb(struct vip_dev * dev,struct vip_port * port)1808 static void vip_unset_rgb(struct vip_dev *dev, struct vip_port *port)
1809 {
1810 	if (port->port_id != VIP_PORTA) {
1811 		v4l2_err(&dev->v4l2_dev, "RGB sensor can only be on Port A\n");
1812 		return;
1813 	}
1814 
1815 	vip_set_slice_path(dev, VIP_MULTI_CHANNEL_DATA_SELECT, 0);
1816 	vip_set_slice_path(dev, VIP_RGB_OUT_LO_DATA_SELECT, 0);
1817 }
1818 
vip_unset_yuv(struct vip_dev * dev,struct vip_port * port)1819 static void vip_unset_yuv(struct vip_dev *dev, struct vip_port *port)
1820 {
1821 	if (port->scaler && port->fmt->coplanar) {
1822 		if (port->port_id == VIP_PORTA) {
1823 			vip_set_slice_path(dev, VIP_SC_SRC_DATA_SELECT, 0);
1824 			vip_set_slice_path(dev, VIP_CHR_DS_1_SRC_DATA_SELECT, 0);
1825 			vip_set_slice_path(dev, VIP_CHR_DS_1_DATA_BYPASS, 0);
1826 			vip_set_slice_path(dev, VIP_RGB_OUT_HI_DATA_SELECT, 0);
1827 		} else {
1828 			vip_set_slice_path(dev, VIP_SC_SRC_DATA_SELECT, 0);
1829 			vip_set_slice_path(dev, VIP_CHR_DS_2_SRC_DATA_SELECT, 0);
1830 			vip_set_slice_path(dev, VIP_CHR_DS_1_DATA_BYPASS, 0);
1831 			vip_set_slice_path(dev, VIP_RGB_OUT_LO_DATA_SELECT, 0);
1832 			vip_set_slice_path(dev, VIP_MULTI_CHANNEL_DATA_SELECT, 0);
1833 		}
1834 	} else if (port->scaler) {
1835 		if (port->port_id == VIP_PORTA) {
1836 			vip_set_slice_path(dev, VIP_SC_SRC_DATA_SELECT, 0);
1837 			vip_set_slice_path(dev, VIP_CHR_DS_1_SRC_DATA_SELECT, 0);
1838 			vip_set_slice_path(dev, VIP_CHR_DS_1_DATA_BYPASS, 0);
1839 			vip_set_slice_path(dev, VIP_RGB_OUT_HI_DATA_SELECT, 0);
1840 		} else {
1841 			vip_set_slice_path(dev, VIP_SC_SRC_DATA_SELECT, 0);
1842 			vip_set_slice_path(dev, VIP_CHR_DS_2_SRC_DATA_SELECT, 0);
1843 			vip_set_slice_path(dev, VIP_CHR_DS_1_DATA_BYPASS, 0);
1844 			vip_set_slice_path(dev, VIP_CHR_DS_2_DATA_BYPASS, 0);
1845 			vip_set_slice_path(dev, VIP_RGB_OUT_HI_DATA_SELECT, 0);
1846 		}
1847 	} else if (port->fmt->coplanar) {
1848 		if (port->port_id == VIP_PORTA) {
1849 			vip_set_slice_path(dev, VIP_CHR_DS_1_SRC_DATA_SELECT, 0);
1850 			vip_set_slice_path(dev, VIP_CHR_DS_1_DATA_BYPASS, 0);
1851 			vip_set_slice_path(dev, VIP_RGB_OUT_HI_DATA_SELECT, 0);
1852 		} else {
1853 			vip_set_slice_path(dev, VIP_CHR_DS_2_SRC_DATA_SELECT, 0);
1854 			vip_set_slice_path(dev, VIP_CHR_DS_2_DATA_BYPASS, 0);
1855 			vip_set_slice_path(dev, VIP_MULTI_CHANNEL_DATA_SELECT, 0);
1856 			vip_set_slice_path(dev, VIP_RGB_OUT_LO_DATA_SELECT, 0);
1857 		}
1858 	} else {
1859 		/*
1860 		 * We undo all data path setting except for the multi
1861 		 * stream case.
1862 		 * Because we cannot disrupt other on-going capture if only
1863 		 * one stream is terminated the other might still be going
1864 		 */
1865 		vip_set_slice_path(dev, VIP_MULTI_CHANNEL_DATA_SELECT, 1);
1866 		vip_set_slice_path(dev, VIP_RGB_OUT_LO_DATA_SELECT, 0);
1867 	}
1868 }
1869 
1870 /*
1871  * Does the exact opposite of set_fmt_params
1872  * It makes sure the DataPath register is sane after tear down
1873  */
unset_fmt_params(struct vip_stream * stream)1874 static void unset_fmt_params(struct vip_stream *stream)
1875 {
1876 	struct vip_port *port = stream->port;
1877 	struct vip_dev *dev = port->dev;
1878 
1879 	stream->sequence = 0;
1880 	stream->field = V4L2_FIELD_TOP;
1881 
1882 	/* Undo CSC Y2R routing */
1883 	if (port->csc == VIP_CSC_Y2R) {
1884 		vip_unset_csc_y2r(dev, port);
1885 	/* Undo CSC R2Y routing */
1886 	} else if (port->csc == VIP_CSC_R2Y) {
1887 		vip_unset_csc_r2y(dev, port);
1888 	/* Undo RGB output routing (no CSC) */
1889 	} else if (v4l2_is_format_rgb(port->fmt->finfo)) {
1890 		vip_unset_rgb(dev, port);
1891 	/* Undo YUV routing with no CSC */
1892 	} else {
1893 		vip_unset_yuv(dev, port);
1894 	}
1895 }
1896 
vip_config_csc_y2r(struct vip_dev * dev,struct vip_port * port)1897 static void vip_config_csc_y2r(struct vip_dev *dev, struct vip_port *port)
1898 {
1899 	port->flags &= ~FLAG_MULT_PORT;
1900 
1901 	/* Set alpha component in background color */
1902 	vpdma_set_bg_color(dev->shared->vpdma,
1903 			   (struct vpdma_data_format *)
1904 			   port->fmt->vpdma_fmt[0],
1905 			   0xff);
1906 
1907 	if (port->port_id == VIP_PORTA) {
1908 		vip_set_slice_path(dev, VIP_CSC_SRC_DATA_SELECT, 1);
1909 		vip_set_slice_path(dev, VIP_MULTI_CHANNEL_DATA_SELECT, 0);
1910 		vip_set_slice_path(dev, VIP_RGB_OUT_HI_DATA_SELECT, 1);
1911 		vip_set_slice_path(dev, VIP_RGB_SRC_DATA_SELECT, 1);
1912 	} else {
1913 		vip_set_slice_path(dev, VIP_CSC_SRC_DATA_SELECT, 2);
1914 		vip_set_slice_path(dev, VIP_MULTI_CHANNEL_DATA_SELECT, 0);
1915 		vip_set_slice_path(dev, VIP_RGB_OUT_LO_DATA_SELECT, 1);
1916 	}
1917 }
1918 
vip_config_csc_r2y(struct vip_dev * dev,struct vip_port * port)1919 static void vip_config_csc_r2y(struct vip_dev *dev, struct vip_port *port)
1920 {
1921 	if (port->port_id != VIP_PORTA) {
1922 		v4l2_err(&dev->v4l2_dev, "RGB sensor can only be on Port A\n");
1923 		return;
1924 	}
1925 
1926 	port->flags &= ~FLAG_MULT_PORT;
1927 
1928 	if (port->scaler && port->fmt->coplanar) {
1929 		vip_set_slice_path(dev, VIP_CSC_SRC_DATA_SELECT, 4);
1930 		vip_set_slice_path(dev, VIP_SC_SRC_DATA_SELECT, 1);
1931 		vip_set_slice_path(dev, VIP_CHR_DS_1_SRC_DATA_SELECT, 1);
1932 		vip_set_slice_path(dev, VIP_CHR_DS_1_DATA_BYPASS, 0);
1933 		vip_set_slice_path(dev, VIP_RGB_OUT_HI_DATA_SELECT, 0);
1934 	} else if (port->scaler) {
1935 		vip_set_slice_path(dev, VIP_CSC_SRC_DATA_SELECT, 4);
1936 		vip_set_slice_path(dev, VIP_SC_SRC_DATA_SELECT, 1);
1937 		vip_set_slice_path(dev, VIP_CHR_DS_1_SRC_DATA_SELECT, 1);
1938 		vip_set_slice_path(dev, VIP_CHR_DS_1_DATA_BYPASS, 1);
1939 		vip_set_slice_path(dev, VIP_RGB_OUT_HI_DATA_SELECT, 0);
1940 	} else if (port->fmt->coplanar) {
1941 		vip_set_slice_path(dev, VIP_CSC_SRC_DATA_SELECT, 4);
1942 		vip_set_slice_path(dev, VIP_CHR_DS_1_SRC_DATA_SELECT, 2);
1943 		vip_set_slice_path(dev, VIP_CHR_DS_1_DATA_BYPASS, 0);
1944 		vip_set_slice_path(dev, VIP_RGB_OUT_HI_DATA_SELECT, 0);
1945 	} else {
1946 		vip_set_slice_path(dev, VIP_CSC_SRC_DATA_SELECT, 4);
1947 		vip_set_slice_path(dev, VIP_CHR_DS_1_SRC_DATA_SELECT, 2);
1948 		vip_set_slice_path(dev, VIP_CHR_DS_1_DATA_BYPASS, 1);
1949 		vip_set_slice_path(dev, VIP_RGB_OUT_HI_DATA_SELECT, 0);
1950 	}
1951 }
1952 
vip_config_rgb(struct vip_dev * dev,struct vip_port * port)1953 static void vip_config_rgb(struct vip_dev *dev, struct vip_port *port)
1954 {
1955 	if (port->port_id != VIP_PORTA) {
1956 		v4l2_err(&dev->v4l2_dev, "RGB sensor can only be on Port A\n");
1957 		return;
1958 	}
1959 
1960 	port->flags &= ~FLAG_MULT_PORT;
1961 
1962 	/* Set alpha component in background color */
1963 	vpdma_set_bg_color(dev->shared->vpdma,
1964 			   (struct vpdma_data_format *)
1965 			   port->fmt->vpdma_fmt[0],
1966 			   0xff);
1967 
1968 	vip_set_slice_path(dev, VIP_MULTI_CHANNEL_DATA_SELECT, 1);
1969 	vip_set_slice_path(dev, VIP_RGB_OUT_LO_DATA_SELECT, 1);
1970 }
1971 
vip_config_yuv(struct vip_dev * dev,struct vip_port * port)1972 static void vip_config_yuv(struct vip_dev *dev, struct vip_port *port)
1973 {
1974 	if (port->scaler && port->fmt->coplanar) {
1975 		port->flags &= ~FLAG_MULT_PORT;
1976 		if (port->port_id == VIP_PORTA) {
1977 			vip_set_slice_path(dev, VIP_SC_SRC_DATA_SELECT, 2);
1978 			vip_set_slice_path(dev, VIP_CHR_DS_1_SRC_DATA_SELECT, 1);
1979 			vip_set_slice_path(dev, VIP_CHR_DS_1_DATA_BYPASS, 0);
1980 			vip_set_slice_path(dev, VIP_RGB_OUT_HI_DATA_SELECT, 0);
1981 		} else {
1982 			vip_set_slice_path(dev, VIP_SC_SRC_DATA_SELECT, 3);
1983 			vip_set_slice_path(dev, VIP_CHR_DS_2_SRC_DATA_SELECT, 1);
1984 			vip_set_slice_path(dev, VIP_CHR_DS_1_DATA_BYPASS, 0);
1985 			vip_set_slice_path(dev, VIP_RGB_OUT_LO_DATA_SELECT, 0);
1986 			vip_set_slice_path(dev, VIP_MULTI_CHANNEL_DATA_SELECT, 0);
1987 		}
1988 	} else if (port->scaler) {
1989 		port->flags &= ~FLAG_MULT_PORT;
1990 		if (port->port_id == VIP_PORTA) {
1991 			vip_set_slice_path(dev, VIP_SC_SRC_DATA_SELECT, 2);
1992 			vip_set_slice_path(dev, VIP_CHR_DS_1_SRC_DATA_SELECT, 1);
1993 			vip_set_slice_path(dev, VIP_CHR_DS_1_DATA_BYPASS, 1);
1994 			vip_set_slice_path(dev, VIP_RGB_OUT_HI_DATA_SELECT, 0);
1995 		} else {
1996 			vip_set_slice_path(dev, VIP_SC_SRC_DATA_SELECT, 3);
1997 			vip_set_slice_path(dev, VIP_CHR_DS_2_SRC_DATA_SELECT, 1);
1998 			vip_set_slice_path(dev, VIP_CHR_DS_1_DATA_BYPASS, 1);
1999 			vip_set_slice_path(dev, VIP_CHR_DS_2_DATA_BYPASS, 1);
2000 			vip_set_slice_path(dev, VIP_RGB_OUT_HI_DATA_SELECT, 0);
2001 		}
2002 	} else if (port->fmt->coplanar) {
2003 		port->flags &= ~FLAG_MULT_PORT;
2004 		if (port->port_id == VIP_PORTA) {
2005 			vip_set_slice_path(dev, VIP_CHR_DS_1_SRC_DATA_SELECT, 3);
2006 			vip_set_slice_path(dev, VIP_CHR_DS_1_DATA_BYPASS, 0);
2007 			vip_set_slice_path(dev, VIP_RGB_OUT_HI_DATA_SELECT, 0);
2008 		} else {
2009 			vip_set_slice_path(dev, VIP_CHR_DS_2_SRC_DATA_SELECT, 4);
2010 			vip_set_slice_path(dev, VIP_CHR_DS_2_DATA_BYPASS, 0);
2011 			vip_set_slice_path(dev, VIP_MULTI_CHANNEL_DATA_SELECT, 0);
2012 			vip_set_slice_path(dev, VIP_RGB_OUT_LO_DATA_SELECT, 0);
2013 		}
2014 	} else {
2015 		port->flags |= FLAG_MULT_PORT;
2016 		vip_set_slice_path(dev, VIP_MULTI_CHANNEL_DATA_SELECT, 1);
2017 		vip_set_slice_path(dev, VIP_RGB_OUT_LO_DATA_SELECT, 0);
2018 	}
2019 }
2020 
2021 /*
2022  * Set the registers that are modified when the video format changes.
2023  */
set_fmt_params(struct vip_stream * stream)2024 static void set_fmt_params(struct vip_stream *stream)
2025 {
2026 	struct vip_port *port = stream->port;
2027 	struct vip_dev *dev = port->dev;
2028 
2029 	stream->sequence = 0;
2030 	stream->field = V4L2_FIELD_TOP;
2031 
2032 	/* YUV input, RGB output using CSC (Y2R) */
2033 	if (port->csc == VIP_CSC_Y2R) {
2034 		vip_config_csc_y2r(dev, port);
2035 	/* RGB input, YUV output using CSC (R2Y) */
2036 	} else if (port->csc == VIP_CSC_R2Y) {
2037 		vip_config_csc_r2y(dev, port);
2038 	/* RGB output without CSC */
2039 	} else if (v4l2_is_format_rgb(port->fmt->finfo)) {
2040 		vip_config_rgb(dev, port);
2041 	/* YUV output without CSC */
2042 	} else {
2043 		vip_config_yuv(dev, port);
2044 	}
2045 }
2046 
vip_g_selection(struct file * file,void * fh,struct v4l2_selection * s)2047 static int vip_g_selection(struct file *file, void *fh,
2048 			   struct v4l2_selection *s)
2049 {
2050 	if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2051 		return -EINVAL;
2052 
2053 	struct vip_stream *stream = file2stream(file);
2054 
2055 	switch (s->target) {
2056 	case V4L2_SEL_TGT_CROP_BOUNDS:
2057 	case V4L2_SEL_TGT_CROP_DEFAULT:
2058 		s->r.left = 0;
2059 		s->r.top = 0;
2060 		s->r.width = stream->width;
2061 		s->r.height = stream->height;
2062 		return 0;
2063 
2064 	case V4L2_SEL_TGT_CROP:
2065 		s->r = stream->port->c_rect;
2066 		return 0;
2067 	}
2068 
2069 	return -EINVAL;
2070 }
2071 
enclosed_rectangle(struct v4l2_rect * a,struct v4l2_rect * b)2072 static int enclosed_rectangle(struct v4l2_rect *a, struct v4l2_rect *b)
2073 {
2074 	if (a->left < b->left || a->top < b->top)
2075 		return 0;
2076 	if (a->left + a->width > b->left + b->width)
2077 		return 0;
2078 	if (a->top + a->height > b->top + b->height)
2079 		return 0;
2080 
2081 	return 1;
2082 }
2083 
vip_s_selection(struct file * file,void * fh,struct v4l2_selection * s)2084 static int vip_s_selection(struct file *file, void *fh,
2085 			   struct v4l2_selection *s)
2086 {
2087 	struct vip_stream *stream = file2stream(file);
2088 	struct vip_port *port = stream->port;
2089 	struct vip_dev *dev = port->dev;
2090 	struct v4l2_rect r = s->r;
2091 
2092 	if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2093 		return -EINVAL;
2094 
2095 	if (s->target != V4L2_SEL_TGT_CROP)
2096 		return -EINVAL;
2097 
2098 	if (vb2_is_busy(&stream->vb_vidq))
2099 		return -EBUSY;
2100 
2101 	v4l_bound_align_image(&r.width, 0, stream->width, 0,
2102 			      &r.height, 0, stream->height, 0, 0);
2103 
2104 	r.left = clamp_t(unsigned int, r.left, 0, stream->width - r.width);
2105 	r.top  = clamp_t(unsigned int, r.top, 0, stream->height - r.height);
2106 
2107 	if (s->flags & V4L2_SEL_FLAG_LE && !enclosed_rectangle(&r, &s->r))
2108 		return -ERANGE;
2109 
2110 	if (s->flags & V4L2_SEL_FLAG_GE && !enclosed_rectangle(&s->r, &r))
2111 		return -ERANGE;
2112 
2113 	s->r = r;
2114 	stream->port->c_rect = r;
2115 
2116 	v4l2_dbg(1, debug, &dev->v4l2_dev, "cropped (%d,%d)/%dx%d of %dx%d\n",
2117 		 r.left, r.top, r.width, r.height,
2118 		 stream->width, stream->height);
2119 
2120 	return 0;
2121 }
2122 
2123 static const struct v4l2_ioctl_ops vip_ioctl_ops = {
2124 	.vidioc_querycap	= vip_querycap,
2125 	.vidioc_enum_input	= vip_enuminput,
2126 	.vidioc_g_input		= vip_g_input,
2127 	.vidioc_s_input		= vip_s_input,
2128 
2129 	.vidioc_querystd	= vip_querystd,
2130 	.vidioc_g_std		= vip_g_std,
2131 	.vidioc_s_std		= vip_s_std,
2132 
2133 	.vidioc_enum_fmt_vid_cap = vip_enum_fmt_vid_cap,
2134 	.vidioc_g_fmt_vid_cap	= vip_g_fmt_vid_cap,
2135 	.vidioc_try_fmt_vid_cap	= vip_try_fmt_vid_cap,
2136 	.vidioc_s_fmt_vid_cap	= vip_s_fmt_vid_cap,
2137 
2138 	.vidioc_enum_frameintervals	= vip_enum_frameintervals,
2139 	.vidioc_enum_framesizes		= vip_enum_framesizes,
2140 	.vidioc_s_parm			= vip_s_parm,
2141 	.vidioc_g_parm			= vip_g_parm,
2142 	.vidioc_g_selection	= vip_g_selection,
2143 	.vidioc_s_selection	= vip_s_selection,
2144 	.vidioc_reqbufs		= vb2_ioctl_reqbufs,
2145 	.vidioc_create_bufs	= vb2_ioctl_create_bufs,
2146 	.vidioc_prepare_buf	= vb2_ioctl_prepare_buf,
2147 	.vidioc_querybuf	= vb2_ioctl_querybuf,
2148 	.vidioc_qbuf		= vb2_ioctl_qbuf,
2149 	.vidioc_dqbuf		= vb2_ioctl_dqbuf,
2150 	.vidioc_expbuf		= vb2_ioctl_expbuf,
2151 
2152 	.vidioc_streamon	= vb2_ioctl_streamon,
2153 	.vidioc_streamoff	= vb2_ioctl_streamoff,
2154 	.vidioc_log_status	= v4l2_ctrl_log_status,
2155 	.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
2156 	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
2157 };
2158 
2159 /*
2160  * Videobuf operations
2161  */
vip_queue_setup(struct vb2_queue * vq,unsigned int * nbuffers,unsigned int * nplanes,unsigned int sizes[],struct device * alloc_devs[])2162 static int vip_queue_setup(struct vb2_queue *vq,
2163 			   unsigned int *nbuffers, unsigned int *nplanes,
2164 			   unsigned int sizes[], struct device *alloc_devs[])
2165 {
2166 	struct vip_stream *stream = vb2_get_drv_priv(vq);
2167 	struct vip_port *port = stream->port;
2168 	struct vip_dev *dev = port->dev;
2169 	unsigned int size = stream->sizeimage;
2170 
2171 	if (*nplanes)
2172 		return sizes[0] < size ? -EINVAL : 0;
2173 
2174 	*nplanes = 1;
2175 	sizes[0] = size;
2176 
2177 	v4l2_dbg(1, debug, &dev->v4l2_dev, "get %d buffer(s) of size %d each.\n",
2178 		 *nbuffers, sizes[0]);
2179 
2180 	return 0;
2181 }
2182 
vip_buf_prepare(struct vb2_buffer * vb)2183 static int vip_buf_prepare(struct vb2_buffer *vb)
2184 {
2185 	struct vip_stream *stream = vb2_get_drv_priv(vb->vb2_queue);
2186 	struct vip_port *port = stream->port;
2187 	struct vip_dev *dev = port->dev;
2188 
2189 	if (vb2_plane_size(vb, 0) < stream->sizeimage) {
2190 		v4l2_dbg(1, debug, &dev->v4l2_dev,
2191 			 "%s data will not fit into plane (%lu < %lu)\n",
2192 			 __func__, vb2_plane_size(vb, 0),
2193 			(long)stream->sizeimage);
2194 		return -EINVAL;
2195 	}
2196 
2197 	vb2_set_plane_payload(vb, 0, stream->sizeimage);
2198 
2199 	return 0;
2200 }
2201 
vip_buf_queue(struct vb2_buffer * vb)2202 static void vip_buf_queue(struct vb2_buffer *vb)
2203 {
2204 	struct vip_stream *stream = vb2_get_drv_priv(vb->vb2_queue);
2205 	struct vip_dev *dev = stream->port->dev;
2206 	struct vip_buffer *buf = container_of(vb, struct vip_buffer,
2207 					      vb.vb2_buf);
2208 	unsigned long flags;
2209 
2210 	spin_lock_irqsave(&dev->slock, flags);
2211 	list_add_tail(&buf->list, &stream->vidq);
2212 	spin_unlock_irqrestore(&dev->slock, flags);
2213 }
2214 
return_buffers(struct vb2_queue * vq,int state)2215 static void return_buffers(struct vb2_queue *vq, int state)
2216 {
2217 	struct vip_stream *stream = vb2_get_drv_priv(vq);
2218 	struct vip_dev *dev = stream->port->dev;
2219 	struct vip_buffer *buf;
2220 	unsigned long flags;
2221 
2222 	spin_lock_irqsave(&dev->slock, flags);
2223 
2224 	/* release all active buffers */
2225 	while (!list_empty(&stream->post_bufs)) {
2226 		buf = list_entry(stream->post_bufs.next,
2227 				 struct vip_buffer, list);
2228 		list_del(&buf->list);
2229 		if (buf->drop == 1)
2230 			list_add_tail(&buf->list, &stream->dropq);
2231 		else
2232 			vb2_buffer_done(&buf->vb.vb2_buf, state);
2233 	}
2234 	while (!list_empty(&stream->vidq)) {
2235 		buf = list_entry(stream->vidq.next, struct vip_buffer, list);
2236 		list_del(&buf->list);
2237 		vb2_buffer_done(&buf->vb.vb2_buf, state);
2238 	}
2239 
2240 	INIT_LIST_HEAD(&stream->post_bufs);
2241 	INIT_LIST_HEAD(&stream->vidq);
2242 
2243 	spin_unlock_irqrestore(&dev->slock, flags);
2244 }
2245 
vip_setup_scaler(struct vip_stream * stream)2246 static int vip_setup_scaler(struct vip_stream *stream)
2247 {
2248 	struct vip_port *port = stream->port;
2249 	struct vip_dev *dev = port->dev;
2250 	struct sc_data *sc = dev->sc;
2251 	struct csc_data *csc = dev->csc;
2252 	struct vpdma_data *vpdma = dev->shared->vpdma;
2253 	struct vip_mmr_adb *mmr_adb = port->mmr_adb.addr;
2254 	int list_num = stream->list_num;
2255 	int timeout = 500;
2256 	struct v4l2_format dst_f;
2257 	struct v4l2_format src_f;
2258 
2259 	memset(&src_f, 0, sizeof(src_f));
2260 	src_f.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2261 	v4l2_fill_pix_format(&src_f.fmt.pix, &port->mbus_framefmt);
2262 	src_f.fmt.pix.pixelformat = vip_mbus_code_to_fourcc(port->fmt->code);
2263 
2264 	dst_f = src_f;
2265 	dst_f.fmt.pix.pixelformat = port->fmt->fourcc;
2266 	dst_f.fmt.pix.width = stream->width;
2267 	dst_f.fmt.pix.height = stream->height;
2268 
2269 	/* if scaler not associated with this port then skip */
2270 	if (port->scaler) {
2271 		sc_set_hs_coeffs(sc, port->sc_coeff_h.addr,
2272 				 port->mbus_framefmt.width,
2273 				 port->c_rect.width);
2274 		sc_set_vs_coeffs(sc, port->sc_coeff_v.addr,
2275 				 port->mbus_framefmt.height,
2276 				 port->c_rect.height);
2277 		sc_config_scaler(sc, &mmr_adb->sc_regs0[0],
2278 				 &mmr_adb->sc_regs8[0], &mmr_adb->sc_regs17[0],
2279 				 port->mbus_framefmt.width,
2280 				 port->mbus_framefmt.height,
2281 				 port->c_rect.width,
2282 				 port->c_rect.height);
2283 		port->load_mmrs = true;
2284 	}
2285 
2286 	/* if csc not associated with this port then skip */
2287 	if (port->csc) {
2288 		csc_set_coeff(csc, &mmr_adb->csc_regs[0],
2289 			      &src_f, &dst_f);
2290 
2291 		port->load_mmrs = true;
2292 	}
2293 
2294 	/* If coeff are already loaded then skip */
2295 	if (!sc->load_coeff_v && !sc->load_coeff_h && !port->load_mmrs)
2296 		return 0;
2297 
2298 	if (vpdma_list_busy(vpdma, list_num)) {
2299 		v4l2_dbg(3, debug, &dev->v4l2_dev, "%s: List %d is busy\n",
2300 			 __func__, list_num);
2301 	}
2302 
2303 	/* Make sure we start with a clean list */
2304 	vpdma_reset_desc_list(&stream->desc_list);
2305 
2306 	/* config descriptors */
2307 	if (port->load_mmrs) {
2308 		vpdma_map_desc_buf(vpdma, &port->mmr_adb);
2309 		vpdma_add_cfd_adb(&stream->desc_list, CFD_MMR_CLIENT,
2310 				  &port->mmr_adb);
2311 
2312 		port->load_mmrs = false;
2313 		v4l2_dbg(3, debug, &dev->v4l2_dev, "Added mmr_adb config desc\n");
2314 	}
2315 
2316 	if (sc->loaded_coeff_h != port->sc_coeff_h.dma_addr ||
2317 	    sc->load_coeff_h) {
2318 		vpdma_map_desc_buf(vpdma, &port->sc_coeff_h);
2319 		vpdma_add_cfd_block(&stream->desc_list,
2320 				    VIP_SLICE1_CFD_SC_CLIENT + dev->slice_id,
2321 				    &port->sc_coeff_h, 0);
2322 
2323 		sc->loaded_coeff_h = port->sc_coeff_h.dma_addr;
2324 		sc->load_coeff_h = false;
2325 		v4l2_dbg(3, debug, &dev->v4l2_dev, "Added sc_coeff_h config desc\n");
2326 	}
2327 
2328 	if (sc->loaded_coeff_v != port->sc_coeff_v.dma_addr ||
2329 	    sc->load_coeff_v) {
2330 		vpdma_map_desc_buf(vpdma, &port->sc_coeff_v);
2331 		vpdma_add_cfd_block(&stream->desc_list,
2332 				    VIP_SLICE1_CFD_SC_CLIENT + dev->slice_id,
2333 				    &port->sc_coeff_v, SC_COEF_SRAM_SIZE >> 4);
2334 
2335 		sc->loaded_coeff_v = port->sc_coeff_v.dma_addr;
2336 		sc->load_coeff_v = false;
2337 		v4l2_dbg(3, debug, &dev->v4l2_dev, "Added sc_coeff_v config desc\n");
2338 	}
2339 	v4l2_dbg(3, debug, stream, "CFD_SC_CLIENT %d slice_id: %d\n",
2340 		 VIP_SLICE1_CFD_SC_CLIENT + dev->slice_id, dev->slice_id);
2341 
2342 	vpdma_map_desc_buf(vpdma, &stream->desc_list.buf);
2343 	v4l2_dbg(3, debug, &dev->v4l2_dev, "Submitting desc on list# %d\n", list_num);
2344 	vpdma_submit_descs(vpdma, &stream->desc_list, list_num);
2345 
2346 	while (vpdma_list_busy(vpdma, list_num) && timeout--)
2347 		usleep_range(1000, 1100);
2348 
2349 	vpdma_unmap_desc_buf(dev->shared->vpdma, &port->mmr_adb);
2350 	vpdma_unmap_desc_buf(dev->shared->vpdma, &port->sc_coeff_h);
2351 	vpdma_unmap_desc_buf(dev->shared->vpdma, &port->sc_coeff_v);
2352 	vpdma_unmap_desc_buf(dev->shared->vpdma, &stream->desc_list.buf);
2353 
2354 	vpdma_reset_desc_list(&stream->desc_list);
2355 
2356 	if (timeout <= 0) {
2357 		v4l2_err(&dev->v4l2_dev, "Timed out setting up scaler through VPDMA list\n");
2358 		return -EBUSY;
2359 	}
2360 
2361 	return 0;
2362 }
2363 
vip_load_vpdma_list_fifo(struct vip_stream * stream)2364 static int vip_load_vpdma_list_fifo(struct vip_stream *stream)
2365 {
2366 	struct vip_port *port = stream->port;
2367 	struct vip_dev *dev = port->dev;
2368 	struct vpdma_data *vpdma = dev->shared->vpdma;
2369 	int list_num = stream->list_num;
2370 	struct vip_buffer *buf;
2371 	unsigned long flags;
2372 	int timeout, i;
2373 
2374 	if (vpdma_list_busy(dev->shared->vpdma, stream->list_num))
2375 		return -EBUSY;
2376 
2377 	for (i = 0; i < VIP_VPDMA_FIFO_SIZE; i++) {
2378 		spin_lock_irqsave(&dev->slock, flags);
2379 		if (list_empty(&stream->vidq)) {
2380 			v4l2_err(&dev->v4l2_dev, "No buffer left!");
2381 			spin_unlock_irqrestore(&dev->slock, flags);
2382 			return -EINVAL;
2383 		}
2384 
2385 		buf = list_entry(stream->vidq.next,
2386 				 struct vip_buffer, list);
2387 		buf->drop = false;
2388 
2389 		list_move_tail(&buf->list, &stream->post_bufs);
2390 		spin_unlock_irqrestore(&dev->slock, flags);
2391 
2392 		v4l2_dbg(2, debug, &dev->v4l2_dev, "%s: start_dma vb2 buf idx:%d\n",
2393 			 __func__, buf->vb.vb2_buf.index);
2394 		start_dma(stream, buf);
2395 
2396 		timeout = 500;
2397 		while (vpdma_list_busy(vpdma, list_num) && timeout--)
2398 			usleep_range(1000, 1100);
2399 
2400 		if (timeout <= 0) {
2401 			v4l2_err(&dev->v4l2_dev, "Timed out loading VPDMA list fifo\n");
2402 			return -EBUSY;
2403 		}
2404 	}
2405 	return 0;
2406 }
2407 
vip_start_streaming(struct vb2_queue * vq,unsigned int count)2408 static int vip_start_streaming(struct vb2_queue *vq, unsigned int count)
2409 {
2410 	struct vip_stream *stream = vb2_get_drv_priv(vq);
2411 	struct vip_port *port = stream->port;
2412 	struct vip_dev *dev = port->dev;
2413 	int ret;
2414 
2415 	ret = vip_setup_scaler(stream);
2416 	if (ret)
2417 		goto err;
2418 
2419 	/*
2420 	 * Make sure the scaler is configured before the datapath is
2421 	 * enabled. The scaler can only load the coefficient
2422 	 * parameters when it is idle. If the scaler path is enabled
2423 	 * and video data is being received then the VPDMA transfer will
2424 	 * stall indefinitely.
2425 	 */
2426 	set_fmt_params(stream);
2427 	ret = vip_setup_parser(port);
2428 	if (ret)
2429 		goto err;
2430 
2431 	if (port->subdev) {
2432 		ret = v4l2_subdev_call(port->subdev, video, s_stream, 1);
2433 		if (ret) {
2434 			v4l2_err(&dev->v4l2_dev, "stream on failed in subdev\n");
2435 			goto err;
2436 		}
2437 	}
2438 
2439 	stream->sequence = 0;
2440 	stream->field = V4L2_FIELD_TOP;
2441 	populate_desc_list(stream);
2442 
2443 	ret = vip_load_vpdma_list_fifo(stream);
2444 	if (ret)
2445 		goto err;
2446 
2447 	stream->num_recovery = 0;
2448 	enable_work(&stream->recovery_work);
2449 
2450 	clear_irqs(dev, dev->slice_id, stream->list_num);
2451 	enable_irqs(dev, dev->slice_id, stream->list_num);
2452 	vip_schedule_next_buffer(stream);
2453 	vip_parser_stop_imm(port, false);
2454 	vip_enable_parser(port, true);
2455 
2456 	return 0;
2457 
2458 err:
2459 	return_buffers(vq, VB2_BUF_STATE_QUEUED);
2460 	return ret;
2461 }
2462 
2463 /*
2464  * Abort streaming and wait for last buffer
2465  */
vip_stop_streaming(struct vb2_queue * vq)2466 static void vip_stop_streaming(struct vb2_queue *vq)
2467 {
2468 	struct vip_stream *stream = vb2_get_drv_priv(vq);
2469 	struct vip_port *port = stream->port;
2470 	struct vip_dev *dev = port->dev;
2471 	int ret;
2472 
2473 	/*
2474 	 * A running recovery worker may re-enable the parser, so quiesce it
2475 	 * and its IRQ handler before stopping the parser or releasing the
2476 	 * descriptor list.
2477 	 */
2478 	vip_quiesce_stream(stream);
2479 
2480 	vip_parser_stop_imm(port, true);
2481 	vip_enable_parser(port, false);
2482 	unset_fmt_params(stream);
2483 
2484 	if (port->subdev) {
2485 		ret = v4l2_subdev_call(port->subdev, video, s_stream, 0);
2486 		if (ret)
2487 			v4l2_err(&dev->v4l2_dev, "Failed to stop subdev stream");
2488 	}
2489 
2490 	stop_dma(stream, true);
2491 
2492 	return_buffers(vq, VB2_BUF_STATE_ERROR);
2493 
2494 	vpdma_unmap_desc_buf(dev->shared->vpdma, &stream->desc_list.buf);
2495 	vpdma_reset_desc_list(&stream->desc_list);
2496 }
2497 
2498 static const struct vb2_ops vip_video_qops = {
2499 	.queue_setup		= vip_queue_setup,
2500 	.buf_prepare		= vip_buf_prepare,
2501 	.buf_queue		= vip_buf_queue,
2502 	.start_streaming	= vip_start_streaming,
2503 	.stop_streaming		= vip_stop_streaming,
2504 };
2505 
vip_init_dev(struct vip_dev * dev)2506 static int vip_init_dev(struct vip_dev *dev)
2507 {
2508 	if (dev->num_ports != 0)
2509 		goto done;
2510 
2511 	vip_set_clock_enable(dev, 1);
2512 	vip_module_toggle(dev, VIP_SC_RST, false);
2513 	vip_module_toggle(dev, VIP_CSC_RST, false);
2514 done:
2515 	dev->num_ports++;
2516 
2517 	return 0;
2518 }
2519 
is_scaler_available(struct vip_port * port)2520 static inline bool is_scaler_available(struct vip_port *port)
2521 {
2522 	if (port->endpoint.bus_type == V4L2_MBUS_PARALLEL)
2523 		if (port->dev->sc_assigned == VIP_NOT_ASSIGNED ||
2524 		    port->dev->sc_assigned == port->port_id)
2525 			return true;
2526 	return false;
2527 }
2528 
allocate_scaler(struct vip_port * port)2529 static inline bool allocate_scaler(struct vip_port *port)
2530 {
2531 	if (is_scaler_available(port)) {
2532 		if (port->dev->sc_assigned == VIP_NOT_ASSIGNED ||
2533 		    port->dev->sc_assigned == port->port_id) {
2534 			port->dev->sc_assigned = port->port_id;
2535 			port->scaler = true;
2536 			return true;
2537 		}
2538 	}
2539 	return false;
2540 }
2541 
free_scaler(struct vip_port * port)2542 static inline void free_scaler(struct vip_port *port)
2543 {
2544 	if (port->dev->sc_assigned == port->port_id) {
2545 		port->dev->sc_assigned = VIP_NOT_ASSIGNED;
2546 		port->scaler = false;
2547 	}
2548 }
2549 
is_csc_available(struct vip_port * port)2550 static bool is_csc_available(struct vip_port *port)
2551 {
2552 	if (port->endpoint.bus_type == V4L2_MBUS_PARALLEL)
2553 		if (port->dev->csc_assigned == VIP_NOT_ASSIGNED ||
2554 		    port->dev->csc_assigned == port->port_id)
2555 			return true;
2556 	return false;
2557 }
2558 
allocate_csc(struct vip_port * port,enum vip_csc_state csc_direction)2559 static bool allocate_csc(struct vip_port *port,
2560 			 enum vip_csc_state csc_direction)
2561 {
2562 	struct vip_dev *dev = port->dev;
2563 	/* Is CSC needed? */
2564 	if (csc_direction != VIP_CSC_NA) {
2565 		if (is_csc_available(port)) {
2566 			port->dev->csc_assigned = port->port_id;
2567 			port->csc = csc_direction;
2568 			v4l2_dbg(1, debug, &dev->v4l2_dev, "%s: csc allocated: dir: %d\n",
2569 				 __func__, csc_direction);
2570 			return true;
2571 		}
2572 	}
2573 	return false;
2574 }
2575 
free_csc(struct vip_port * port)2576 static void free_csc(struct vip_port *port)
2577 {
2578 	struct vip_dev *dev = port->dev;
2579 
2580 	if (port->dev->csc_assigned == port->port_id) {
2581 		port->dev->csc_assigned = VIP_NOT_ASSIGNED;
2582 		port->csc = VIP_CSC_NA;
2583 		v4l2_dbg(1, debug, &dev->v4l2_dev, "%s: csc freed\n",
2584 			 __func__);
2585 	}
2586 }
2587 
vip_init_port(struct vip_port * port)2588 static int vip_init_port(struct vip_port *port)
2589 {
2590 	struct vip_dev *dev = port->dev;
2591 	int ret;
2592 	struct vip_fmt *fmt;
2593 	struct v4l2_subdev_format sd_fmt = {
2594 		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
2595 		.pad = 0,
2596 	};
2597 	struct v4l2_mbus_framefmt *mbus_fmt = &sd_fmt.format;
2598 
2599 	if (port->num_streams != 0)
2600 		goto done;
2601 
2602 	ret = vip_init_dev(port->dev);
2603 	if (ret)
2604 		goto done;
2605 
2606 	/* Get subdevice current frame format */
2607 	ret = v4l2_subdev_call(port->subdev, pad, get_fmt, NULL, &sd_fmt);
2608 	if (ret)
2609 		v4l2_dbg(1, debug, &dev->v4l2_dev, "init_port get_fmt failed in subdev: (%d)\n",
2610 			 ret);
2611 
2612 	/* try to find one that matches */
2613 	fmt = find_port_format_by_code(port, mbus_fmt->code);
2614 	if (!fmt) {
2615 		v4l2_dbg(1, debug, &dev->v4l2_dev, "subdev default mbus_fmt %04x is not matched.\n",
2616 			 mbus_fmt->code);
2617 		/* if all else fails just pick the first one */
2618 		fmt = port->active_fmt[0];
2619 
2620 		mbus_fmt->code = fmt->code;
2621 		sd_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
2622 		sd_fmt.pad = 0;
2623 		ret = v4l2_subdev_call(port->subdev, pad, set_fmt,
2624 				       NULL, &sd_fmt);
2625 		if (ret)
2626 			v4l2_dbg(1, debug, &dev->v4l2_dev, "init_port set_fmt failed in subdev: (%d)\n",
2627 				 ret);
2628 	}
2629 
2630 	/* Assign current format */
2631 	port->fmt = fmt;
2632 	port->mbus_framefmt = *mbus_fmt;
2633 
2634 	v4l2_dbg(3, debug, &dev->v4l2_dev, "%s: g_mbus_fmt subdev mbus_code: %04X fourcc:%s size: %dx%d\n",
2635 		 __func__, fmt->code,
2636 		fourcc_to_str(fmt->fourcc),
2637 		mbus_fmt->width, mbus_fmt->height);
2638 
2639 	if (mbus_fmt->field == V4L2_FIELD_ALTERNATE)
2640 		port->flags |= FLAG_INTERLACED;
2641 	else
2642 		port->flags &= ~FLAG_INTERLACED;
2643 
2644 	port->c_rect.left	= 0;
2645 	port->c_rect.top	= 0;
2646 	port->c_rect.width	= mbus_fmt->width;
2647 	port->c_rect.height	= mbus_fmt->height;
2648 
2649 	ret = vpdma_alloc_desc_buf(&port->sc_coeff_h, SC_COEF_SRAM_SIZE);
2650 	if (ret != 0)
2651 		return ret;
2652 
2653 	ret = vpdma_alloc_desc_buf(&port->sc_coeff_v, SC_COEF_SRAM_SIZE);
2654 	if (ret != 0)
2655 		goto free_sc_h;
2656 
2657 	ret = vpdma_alloc_desc_buf(&port->mmr_adb, sizeof(struct vip_mmr_adb));
2658 	if (ret != 0)
2659 		goto free_sc_v;
2660 
2661 	init_adb_hdrs(port);
2662 
2663 	vip_enable_parser(port, false);
2664 done:
2665 	port->num_streams++;
2666 	return 0;
2667 
2668 free_sc_v:
2669 	vpdma_free_desc_buf(&port->sc_coeff_v);
2670 free_sc_h:
2671 	vpdma_free_desc_buf(&port->sc_coeff_h);
2672 	return ret;
2673 }
2674 
vip_init_stream(struct vip_stream * stream)2675 static int vip_init_stream(struct vip_stream *stream)
2676 {
2677 	struct vip_port *port = stream->port;
2678 	struct vip_dev *dev = port->dev;
2679 	struct vip_fmt *fmt;
2680 	struct v4l2_mbus_framefmt *mbus_fmt;
2681 	struct v4l2_format f;
2682 	int ret;
2683 
2684 	ret = vip_init_port(port);
2685 	if (ret != 0)
2686 		return ret;
2687 
2688 	fmt = port->fmt;
2689 	mbus_fmt = &port->mbus_framefmt;
2690 
2691 	memset(&f, 0, sizeof(f));
2692 
2693 	/* Properly calculate the sizeimage and bytesperline values. */
2694 	v4l2_fill_pix_format(&f.fmt.pix, mbus_fmt);
2695 	f.fmt.pix.pixelformat = fmt->fourcc;
2696 	ret = vip_calc_format_size(port, fmt, &f);
2697 	if (ret)
2698 		return ret;
2699 
2700 	stream->width = f.fmt.pix.width;
2701 	stream->height = f.fmt.pix.height;
2702 	stream->sup_field = f.fmt.pix.field;
2703 	stream->bytesperline = f.fmt.pix.bytesperline;
2704 	stream->sizeimage = f.fmt.pix.sizeimage;
2705 
2706 	v4l2_dbg(3, debug, &dev->v4l2_dev, "init_stream fourcc:%s size: %dx%d bpl:%d img_size:%d\n",
2707 		 fourcc_to_str(f.fmt.pix.pixelformat),
2708 		 f.fmt.pix.width, f.fmt.pix.height,
2709 		 f.fmt.pix.bytesperline, f.fmt.pix.sizeimage);
2710 	v4l2_dbg(3, debug, &dev->v4l2_dev, "init_stream vpdma data type: 0x%02X\n",
2711 		 port->fmt->vpdma_fmt[0]->data_type);
2712 
2713 	ret = vpdma_create_desc_list(&stream->desc_list, VIP_DESC_LIST_SIZE,
2714 				     VPDMA_LIST_TYPE_NORMAL);
2715 
2716 	if (ret != 0)
2717 		return ret;
2718 
2719 	stream->write_desc = (struct vpdma_dtd *)stream->desc_list.buf.addr
2720 				+ 15;
2721 
2722 	v4l2_dbg(1, debug, &dev->v4l2_dev, "%s: stream instance %pa\n",
2723 		 __func__, &stream);
2724 
2725 	return 0;
2726 }
2727 
vip_release_dev(struct vip_dev * dev)2728 static void vip_release_dev(struct vip_dev *dev)
2729 {
2730 	/*
2731 	 * On last close, disable clocks to conserve power
2732 	 */
2733 
2734 	if (--dev->num_ports == 0) {
2735 		/* reset the scaler module */
2736 		vip_module_toggle(dev, VIP_SC_RST, true);
2737 		vip_module_toggle(dev, VIP_CSC_RST, true);
2738 		vip_set_clock_enable(dev, 0);
2739 	}
2740 }
2741 
vip_set_crop_parser(struct vip_port * port)2742 static int vip_set_crop_parser(struct vip_port *port)
2743 {
2744 	struct vip_dev *dev = port->dev;
2745 	struct vip_parser_data *parser = dev->parser;
2746 	u32 hcrop = 0, vcrop = 0;
2747 	u32 width = port->mbus_framefmt.width;
2748 
2749 	if (port->fmt->vpdma_fmt[0] == &vpdma_raw_fmts[VPDMA_DATA_FMT_RAW8]) {
2750 		/*
2751 		 * Special case since we are faking a YUV422 16bit format
2752 		 * to have the vpdma perform the needed byte swap
2753 		 * we need to adjust the pixel width accordingly
2754 		 * otherwise the parser will attempt to collect more pixels
2755 		 * then available and the vpdma transfer will exceed the
2756 		 * allocated frame buffer.
2757 		 */
2758 		width >>= 1;
2759 		v4l2_dbg(1, debug, &dev->v4l2_dev, "%s: 8 bit raw detected, adjusting width to %d\n",
2760 			 __func__, width);
2761 	}
2762 
2763 	/*
2764 	 * Set Parser Crop parameters to source size otherwise
2765 	 * scaler and colorspace converter will yield garbage.
2766 	 */
2767 	hcrop = VIP_ACT_BYPASS;
2768 	insert_field(&hcrop, 0, VIP_ACT_SKIP_NUMPIX_MASK,
2769 		     VIP_ACT_SKIP_NUMPIX_SHFT);
2770 	insert_field(&hcrop, width,
2771 		     VIP_ACT_USE_NUMPIX_MASK, VIP_ACT_USE_NUMPIX_SHFT);
2772 	reg_write(parser, VIP_PARSER_CROP_H_PORT(port->port_id), hcrop);
2773 
2774 	insert_field(&vcrop, 0, VIP_ACT_SKIP_NUMLINES_MASK,
2775 		     VIP_ACT_SKIP_NUMLINES_SHFT);
2776 	insert_field(&vcrop, port->mbus_framefmt.height,
2777 		     VIP_ACT_USE_NUMLINES_MASK, VIP_ACT_USE_NUMLINES_SHFT);
2778 	reg_write(parser, VIP_PARSER_CROP_V_PORT(port->port_id), vcrop);
2779 
2780 	return 0;
2781 }
2782 
vip_setup_parser(struct vip_port * port)2783 static int vip_setup_parser(struct vip_port *port)
2784 {
2785 	struct vip_dev *dev = port->dev;
2786 	struct vip_parser_data *parser = dev->parser;
2787 	struct v4l2_fwnode_endpoint *endpoint = &port->endpoint;
2788 	int iface, sync_type;
2789 	u32 flags = 0, config0;
2790 
2791 	/* Reset the port */
2792 	vip_reset_parser(port, true);
2793 	usleep_range(200, 250);
2794 	vip_reset_parser(port, false);
2795 
2796 	config0 = reg_read(parser, VIP_PARSER_PORT(port->port_id));
2797 
2798 	if (endpoint->bus_type == V4L2_MBUS_BT656) {
2799 		flags = endpoint->bus.parallel.flags;
2800 		iface = DUAL_8B_INTERFACE;
2801 
2802 		/*
2803 		 * Ideally, this should come from subdev
2804 		 * port->fmt can be anything once CSC is enabled
2805 		 */
2806 		if (vip_is_mbuscode_rgb(port->fmt->code))
2807 			sync_type = EMBEDDED_SYNC_SINGLE_RGB_OR_YUV444;
2808 		else
2809 			sync_type = EMBEDDED_SYNC_LINE_MULTIPLEXED_YUV422;
2810 
2811 	} else if (endpoint->bus_type == V4L2_MBUS_PARALLEL) {
2812 		flags = endpoint->bus.parallel.flags;
2813 
2814 		switch (endpoint->bus.parallel.bus_width) {
2815 		case 24:
2816 			iface = SINGLE_24B_INTERFACE;
2817 		break;
2818 		case 16:
2819 			iface = SINGLE_16B_INTERFACE;
2820 		break;
2821 		case 8:
2822 		default:
2823 			iface = DUAL_8B_INTERFACE;
2824 		}
2825 
2826 		if (vip_is_mbuscode_rgb(port->fmt->code))
2827 			sync_type = DISCRETE_SYNC_SINGLE_RGB_24B;
2828 		else
2829 			sync_type = DISCRETE_SYNC_SINGLE_YUV422;
2830 
2831 		if (flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
2832 			config0 |= VIP_HSYNC_POLARITY;
2833 		else if (flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)
2834 			config0 &= ~VIP_HSYNC_POLARITY;
2835 
2836 		if (flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH)
2837 			config0 |= VIP_VSYNC_POLARITY;
2838 		else if (flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)
2839 			config0 &= ~VIP_VSYNC_POLARITY;
2840 
2841 		config0 &= ~VIP_USE_ACTVID_HSYNC_ONLY;
2842 		config0 |= VIP_ACTVID_POLARITY;
2843 		config0 |= VIP_DISCRETE_BASIC_MODE;
2844 
2845 	} else {
2846 		v4l2_err(&dev->v4l2_dev, "Device doesn't support CSI2");
2847 		return -EINVAL;
2848 	}
2849 
2850 	if (flags & V4L2_MBUS_PCLK_SAMPLE_FALLING) {
2851 		vip_set_pclk_invert(port);
2852 		config0 |= VIP_PIXCLK_EDGE_POLARITY;
2853 	} else {
2854 		config0 &= ~VIP_PIXCLK_EDGE_POLARITY;
2855 	}
2856 
2857 	config0 |= ((sync_type & VIP_SYNC_TYPE_MASK) << VIP_SYNC_TYPE_SHFT);
2858 
2859 	reg_write(parser, VIP_PARSER_PORT(port->port_id), config0);
2860 
2861 	vip_set_data_interface(port, iface);
2862 	vip_set_crop_parser(port);
2863 
2864 	return 0;
2865 }
2866 
vip_enable_parser(struct vip_port * port,bool on)2867 static void vip_enable_parser(struct vip_port *port, bool on)
2868 {
2869 	u32 config0;
2870 	struct vip_dev *dev = port->dev;
2871 	struct vip_parser_data *parser = dev->parser;
2872 
2873 	config0 = reg_read(parser, VIP_PARSER_PORT(port->port_id));
2874 
2875 	if (on) {
2876 		config0 |= VIP_PORT_ENABLE;
2877 		config0 &= ~(VIP_ASYNC_FIFO_RD | VIP_ASYNC_FIFO_WR);
2878 	} else {
2879 		config0 &= ~VIP_PORT_ENABLE;
2880 		config0 |= (VIP_ASYNC_FIFO_RD | VIP_ASYNC_FIFO_WR);
2881 	}
2882 	reg_write(parser, VIP_PARSER_PORT(port->port_id), config0);
2883 }
2884 
vip_reset_parser(struct vip_port * port,bool on)2885 static void vip_reset_parser(struct vip_port *port, bool on)
2886 {
2887 	u32 config0;
2888 	struct vip_dev *dev = port->dev;
2889 	struct vip_parser_data *parser = dev->parser;
2890 
2891 	config0 = reg_read(parser, VIP_PARSER_PORT(port->port_id));
2892 
2893 	if (on)
2894 		config0 |= VIP_SW_RESET;
2895 	else
2896 		config0 &= ~VIP_SW_RESET;
2897 
2898 	reg_write(parser, VIP_PARSER_PORT(port->port_id), config0);
2899 }
2900 
vip_parser_stop_imm(struct vip_port * port,bool on)2901 static void vip_parser_stop_imm(struct vip_port *port, bool on)
2902 {
2903 	u32 config0;
2904 	struct vip_dev *dev = port->dev;
2905 	struct vip_parser_data *parser = dev->parser;
2906 
2907 	config0 = reg_read(parser, VIP_PARSER_STOP_IMM_PORT(port->port_id));
2908 
2909 	if (on)
2910 		config0 = 0xffffffff;
2911 	else
2912 		config0 = 0;
2913 
2914 	reg_write(parser, VIP_PARSER_STOP_IMM_PORT(port->port_id), config0);
2915 }
2916 
vip_release_stream(struct vip_stream * stream)2917 static void vip_release_stream(struct vip_stream *stream)
2918 {
2919 	struct vip_dev *dev = stream->port->dev;
2920 
2921 	v4l2_dbg(1, debug, &dev->v4l2_dev, "%s: stream instance %pa\n",
2922 		 __func__, &stream);
2923 
2924 	vpdma_unmap_desc_buf(dev->shared->vpdma, &stream->desc_list.buf);
2925 	vpdma_free_desc_buf(&stream->desc_list.buf);
2926 	vpdma_free_desc_list(&stream->desc_list);
2927 }
2928 
vip_release_port(struct vip_port * port)2929 static void vip_release_port(struct vip_port *port)
2930 {
2931 	struct vip_dev *dev = port->dev;
2932 
2933 	v4l2_dbg(1, debug, &dev->v4l2_dev, "%s: port instance %pa\n",
2934 		 __func__, &port);
2935 
2936 	vpdma_free_desc_buf(&port->mmr_adb);
2937 	vpdma_free_desc_buf(&port->sc_coeff_h);
2938 	vpdma_free_desc_buf(&port->sc_coeff_v);
2939 }
2940 
stop_dma(struct vip_stream * stream,bool clear_list)2941 static void stop_dma(struct vip_stream *stream, bool clear_list)
2942 {
2943 	struct vip_dev *dev = stream->port->dev;
2944 	int ch, size = 0;
2945 
2946 	/* Create a list of channels to be cleared */
2947 	for (ch = 0; ch < VPDMA_MAX_CHANNELS; ch++) {
2948 		if (stream->vpdma_channels[ch] == 1) {
2949 			stream->vpdma_channels_to_abort[size++] = ch;
2950 			v4l2_dbg(2, debug, &dev->v4l2_dev, "Clear channel no: %d\n", ch);
2951 		}
2952 	}
2953 
2954 	/* Clear all the used channels for the list */
2955 	vpdma_list_cleanup(dev->shared->vpdma, stream->list_num,
2956 			   stream->vpdma_channels_to_abort, size);
2957 
2958 	if (clear_list)
2959 		for (ch = 0; ch < VPDMA_MAX_CHANNELS; ch++)
2960 			stream->vpdma_channels[ch] = 0;
2961 }
2962 
vip_open(struct file * file)2963 static int vip_open(struct file *file)
2964 {
2965 	struct vip_stream *stream = video_drvdata(file);
2966 	struct vip_port *port = stream->port;
2967 	struct vip_dev *dev = port->dev;
2968 	int ret = 0;
2969 
2970 	mutex_lock(&dev->mutex);
2971 
2972 	ret = v4l2_fh_open(file);
2973 	if (ret) {
2974 		v4l2_err(&dev->v4l2_dev, "v4l2_fh_open failed\n");
2975 		goto unlock;
2976 	}
2977 
2978 	/*
2979 	 * If this is the first open file.
2980 	 * Then initialize hw module.
2981 	 */
2982 	if (!v4l2_fh_is_singular_file(file))
2983 		goto unlock;
2984 
2985 	if (vip_init_stream(stream))
2986 		ret = -ENODEV;
2987 unlock:
2988 	mutex_unlock(&dev->mutex);
2989 	return ret;
2990 }
2991 
vip_release(struct file * file)2992 static int vip_release(struct file *file)
2993 {
2994 	struct vip_stream *stream = video_drvdata(file);
2995 	struct vip_port *port = stream->port;
2996 	struct vip_dev *dev = port->dev;
2997 	bool fh_singular;
2998 	int ret;
2999 
3000 	mutex_lock(&dev->mutex);
3001 
3002 	/* Save the singular status before we call the clean-up helper */
3003 	fh_singular = v4l2_fh_is_singular_file(file);
3004 
3005 	/* the release helper will cleanup any on-going streaming */
3006 	ret = _vb2_fop_release(file, NULL);
3007 
3008 	free_csc(port);
3009 	free_scaler(port);
3010 
3011 	/*
3012 	 * If this is the last open file.
3013 	 * Then de-initialize hw module.
3014 	 */
3015 	if (fh_singular) {
3016 		vip_release_stream(stream);
3017 
3018 		if (--port->num_streams == 0) {
3019 			vip_release_port(port);
3020 			vip_release_dev(port->dev);
3021 		}
3022 	}
3023 
3024 	mutex_unlock(&dev->mutex);
3025 
3026 	return ret;
3027 }
3028 
3029 /*
3030  * File operations
3031  */
3032 static const struct v4l2_file_operations vip_fops = {
3033 	.owner		= THIS_MODULE,
3034 	.open		= vip_open,
3035 	.release	= vip_release,
3036 	.poll		= vb2_fop_poll,
3037 	.unlocked_ioctl	= video_ioctl2,
3038 	.mmap		= vb2_fop_mmap,
3039 };
3040 
3041 static struct video_device vip_videodev = {
3042 	.name		= VIP_MODULE_NAME,
3043 	.fops		= &vip_fops,
3044 	.ioctl_ops	= &vip_ioctl_ops,
3045 	.minor		= -1,
3046 	.release	= video_device_release,
3047 	.tvnorms	= V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM,
3048 	.device_caps	= V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE,
3049 };
3050 
alloc_stream(struct vip_port * port,int stream_id,int vfl_type)3051 static int alloc_stream(struct vip_port *port, int stream_id, int vfl_type)
3052 {
3053 	struct vip_stream *stream;
3054 	struct vip_dev *dev = port->dev;
3055 	struct vb2_queue *q;
3056 	struct video_device *vfd;
3057 	struct vip_buffer *buf;
3058 	struct list_head *pos, *tmp;
3059 	int ret, i;
3060 
3061 	stream = kzalloc_obj(*stream);
3062 	if (!stream)
3063 		return -ENOMEM;
3064 
3065 	stream->port = port;
3066 	stream->stream_id = stream_id;
3067 	stream->vfl_type = vfl_type;
3068 	port->cap_streams[stream_id] = stream;
3069 
3070 	stream->list_num = vpdma_hwlist_alloc(dev->shared->vpdma, stream);
3071 	if (stream->list_num < 0) {
3072 		v4l2_err(&dev->v4l2_dev, "Could not get VPDMA hwlist");
3073 		ret = -ENODEV;
3074 		goto do_free_stream;
3075 	}
3076 
3077 	INIT_LIST_HEAD(&stream->post_bufs);
3078 
3079 	/*
3080 	 * Initialize queue
3081 	 */
3082 	q = &stream->vb_vidq;
3083 	q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
3084 	q->io_modes = VB2_MMAP | VB2_DMABUF;
3085 	q->drv_priv = stream;
3086 	q->buf_struct_size = sizeof(struct vip_buffer);
3087 	q->ops = &vip_video_qops;
3088 	q->mem_ops = &vb2_dma_contig_memops;
3089 	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
3090 	q->lock = &dev->mutex;
3091 	q->min_queued_buffers = 2;
3092 	q->dev = dev->v4l2_dev.dev;
3093 
3094 	ret = vb2_queue_init(q);
3095 	if (ret)
3096 		goto do_free_hwlist;
3097 
3098 	INIT_WORK(&stream->recovery_work, vip_overflow_recovery_work);
3099 	/* Start disabled; vip_start_streaming() enables it before IRQs. */
3100 	disable_work(&stream->recovery_work);
3101 
3102 	INIT_LIST_HEAD(&stream->vidq);
3103 
3104 	/* Allocate/populate Drop queue entries */
3105 	INIT_LIST_HEAD(&stream->dropq);
3106 	for (i = 0; i < VIP_DROPQ_SIZE; i++) {
3107 		buf = kzalloc_obj(*buf, GFP_ATOMIC);
3108 		if (!buf) {
3109 			ret = -ENOMEM;
3110 			goto do_free_dropq;
3111 		}
3112 		buf->drop = true;
3113 		list_add(&buf->list, &stream->dropq);
3114 	}
3115 
3116 	vfd = video_device_alloc();
3117 	if (!vfd) {
3118 		ret = -ENOMEM;
3119 		goto do_free_dropq;
3120 	}
3121 	*vfd = vip_videodev;
3122 	vfd->v4l2_dev = &dev->v4l2_dev;
3123 	vfd->queue = q;
3124 
3125 	vfd->lock = &dev->mutex;
3126 	video_set_drvdata(vfd, stream);
3127 	stream->vfd = vfd;
3128 
3129 	ret = video_register_device(vfd, vfl_type, -1);
3130 	if (ret) {
3131 		v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
3132 		goto do_free_vfd;
3133 	}
3134 
3135 	v4l2_info(&dev->v4l2_dev, "device registered as %s\n",
3136 		  video_device_node_name(vfd));
3137 	return 0;
3138 
3139 do_free_vfd:
3140 	video_device_release(vfd);
3141 do_free_dropq:
3142 	list_for_each_safe(pos, tmp, &stream->dropq) {
3143 		buf = list_entry(pos,
3144 				 struct vip_buffer, list);
3145 		v4l2_dbg(1, debug, &dev->v4l2_dev, "dropq buffer\n");
3146 		list_del(pos);
3147 		kfree(buf);
3148 	}
3149 do_free_hwlist:
3150 	vpdma_hwlist_release(dev->shared->vpdma, stream->list_num);
3151 do_free_stream:
3152 	kfree(stream);
3153 	return ret;
3154 }
3155 
free_stream(struct vip_stream * stream)3156 static void free_stream(struct vip_stream *stream)
3157 {
3158 	struct vip_dev *dev;
3159 	struct vip_buffer *buf;
3160 	struct list_head *pos, *q;
3161 
3162 	if (!stream)
3163 		return;
3164 
3165 	dev = stream->port->dev;
3166 	/*
3167 	 * Quiesce the IRQ handler and recovery worker, then drop the stream
3168 	 * from cap_streams[], before releasing stream-owned resources.
3169 	 */
3170 	vip_quiesce_stream(stream);
3171 	stream->port->cap_streams[stream->stream_id] = NULL;
3172 
3173 	/* Free up the Drop queue */
3174 	list_for_each_safe(pos, q, &stream->dropq) {
3175 		buf = list_entry(pos,
3176 				 struct vip_buffer, list);
3177 		v4l2_dbg(1, debug, &dev->v4l2_dev, "dropq buffer\n");
3178 		list_del(pos);
3179 		kfree(buf);
3180 	}
3181 
3182 	video_unregister_device(stream->vfd);
3183 	vpdma_hwlist_release(dev->shared->vpdma, stream->list_num);
3184 	kfree(stream);
3185 }
3186 
get_subdev_active_format(struct vip_port * port,struct v4l2_subdev * subdev)3187 static int get_subdev_active_format(struct vip_port *port,
3188 				    struct v4l2_subdev *subdev)
3189 {
3190 	struct vip_fmt *fmt;
3191 	struct vip_dev *dev = port->dev;
3192 	struct v4l2_subdev_mbus_code_enum mbus_code;
3193 	int ret = 0;
3194 	unsigned int k, i, j;
3195 	enum vip_csc_state csc;
3196 
3197 	/* Enumerate sub device formats and enable all matching local formats */
3198 	port->num_active_fmt = 0;
3199 	for (k = 0, i = 0; (ret != -EINVAL); k++) {
3200 		memset(&mbus_code, 0, sizeof(mbus_code));
3201 		mbus_code.index = k;
3202 		mbus_code.which = V4L2_SUBDEV_FORMAT_ACTIVE;
3203 		ret = v4l2_subdev_call(subdev, pad, enum_mbus_code,
3204 				       NULL, &mbus_code);
3205 		if (ret)
3206 			continue;
3207 
3208 		v4l2_dbg(2, debug, &dev->v4l2_dev,
3209 			 "subdev %s: code: %04x idx: %d\n",
3210 			 subdev->name, mbus_code.code, k);
3211 
3212 		for (j = 0; j < ARRAY_SIZE(vip_formats); j++) {
3213 			fmt = &vip_formats[j];
3214 			if (mbus_code.code != fmt->code)
3215 				continue;
3216 
3217 			/*
3218 			 * When the port is configured for BT656
3219 			 * then none of the downstream unit can be used.
3220 			 * So here we need to skip all format requiring
3221 			 * either CSC or CHR_DS
3222 			 */
3223 			csc = vip_csc_direction(fmt->code, fmt->finfo);
3224 			if (port->endpoint.bus_type == V4L2_MBUS_BT656 &&
3225 			    (csc != VIP_CSC_NA || fmt->coplanar))
3226 				continue;
3227 
3228 			port->active_fmt[i] = fmt;
3229 			v4l2_dbg(2, debug, &dev->v4l2_dev,
3230 				 "matched fourcc: %s: code: %04x idx: %d\n",
3231 				 fourcc_to_str(fmt->fourcc), fmt->code, i);
3232 			port->num_active_fmt = ++i;
3233 		}
3234 	}
3235 
3236 	if (i == 0) {
3237 		v4l2_err(&dev->v4l2_dev, "No suitable format reported by subdev %s\n",
3238 			 subdev->name);
3239 		return -EINVAL;
3240 	}
3241 	return 0;
3242 }
3243 
alloc_port(struct vip_dev * dev,int id)3244 static int alloc_port(struct vip_dev *dev, int id)
3245 {
3246 	struct vip_port *port;
3247 
3248 	if (dev->ports[id])
3249 		return -EINVAL;
3250 
3251 	port = devm_kzalloc(&dev->pdev->dev, sizeof(*port), GFP_KERNEL);
3252 	if (!port)
3253 		return -ENOMEM;
3254 
3255 	dev->ports[id] = port;
3256 	port->dev = dev;
3257 	port->port_id = id;
3258 	port->num_streams = 0;
3259 	return 0;
3260 }
3261 
free_port(struct vip_port * port)3262 static void free_port(struct vip_port *port)
3263 {
3264 	if (!port)
3265 		return;
3266 
3267 	v4l2_async_nf_unregister(&port->notifier);
3268 	v4l2_async_nf_cleanup(&port->notifier);
3269 	free_stream(port->cap_streams[0]);
3270 }
3271 
get_field(u32 value,u32 mask,int shift)3272 static int get_field(u32 value, u32 mask, int shift)
3273 {
3274 	return (value & (mask << shift)) >> shift;
3275 }
3276 
3277 static int vip_probe_complete(struct platform_device *pdev);
vip_vpdma_fw_cb(struct platform_device * pdev)3278 static void vip_vpdma_fw_cb(struct platform_device *pdev)
3279 {
3280 	dev_info(&pdev->dev, "VPDMA firmware loaded\n");
3281 
3282 	if (pdev->dev.of_node)
3283 		vip_probe_complete(pdev);
3284 }
3285 
vip_create_streams(struct vip_port * port,struct v4l2_subdev * subdev)3286 static int vip_create_streams(struct vip_port *port,
3287 			      struct v4l2_subdev *subdev)
3288 {
3289 	int i;
3290 
3291 	for (i = 0; i < VIP_CAP_STREAMS_PER_PORT; i++)
3292 		free_stream(port->cap_streams[i]);
3293 
3294 	if (get_subdev_active_format(port, subdev))
3295 		return -ENODEV;
3296 
3297 	port->subdev = subdev;
3298 
3299 	if (port->endpoint.bus_type == V4L2_MBUS_PARALLEL) {
3300 		port->flags |= FLAG_MULT_PORT;
3301 		port->num_streams_configured = 1;
3302 		alloc_stream(port, 0, VFL_TYPE_VIDEO);
3303 	} else if (port->endpoint.bus_type == V4L2_MBUS_BT656) {
3304 		port->flags |= FLAG_MULT_PORT;
3305 		port->num_streams_configured = 1;
3306 		alloc_stream(port, 0, VFL_TYPE_VIDEO);
3307 	}
3308 	return 0;
3309 }
3310 
vip_async_bound(struct v4l2_async_notifier * notifier,struct v4l2_subdev * subdev,struct v4l2_async_connection * asd)3311 static int vip_async_bound(struct v4l2_async_notifier *notifier,
3312 			   struct v4l2_subdev *subdev,
3313 			   struct v4l2_async_connection *asd)
3314 {
3315 	struct vip_port *port = notifier_to_vip_port(notifier);
3316 	int ret;
3317 
3318 	if (port->subdev) {
3319 		v4l2_info(&port->dev->v4l2_dev, "Rejecting subdev %s (Already set!!)",
3320 			  subdev->name);
3321 		return 0;
3322 	}
3323 
3324 	v4l2_info(&port->dev->v4l2_dev, "Port %c: Using subdev %s for capture\n",
3325 		  port->port_id == VIP_PORTA ? 'A' : 'B', subdev->name);
3326 
3327 	ret = vip_create_streams(port, subdev);
3328 	if (ret)
3329 		return ret;
3330 
3331 	return 0;
3332 }
3333 
vip_async_complete(struct v4l2_async_notifier * notifier)3334 static int vip_async_complete(struct v4l2_async_notifier *notifier)
3335 {
3336 	return 0;
3337 }
3338 
3339 static const struct v4l2_async_notifier_operations vip_async_ops = {
3340 	.bound = vip_async_bound,
3341 	.complete = vip_async_complete,
3342 };
3343 
3344 static struct fwnode_handle *
fwnode_graph_get_next_endpoint_by_regs(const struct fwnode_handle * fwnode,int port_reg,int reg)3345 fwnode_graph_get_next_endpoint_by_regs(const struct fwnode_handle *fwnode,
3346 				       int port_reg, int reg)
3347 {
3348 	return of_fwnode_handle(of_graph_get_endpoint_by_regs(to_of_node(fwnode),
3349 							      port_reg, reg));
3350 }
3351 
vip_register_subdev_notify(struct vip_port * port,struct fwnode_handle * ep)3352 static int vip_register_subdev_notify(struct vip_port *port,
3353 				      struct fwnode_handle *ep)
3354 {
3355 	struct v4l2_async_notifier *notifier = &port->notifier;
3356 	struct fwnode_handle *subdev;
3357 	struct v4l2_fwnode_endpoint *vep;
3358 	struct v4l2_async_connection *asd;
3359 	int ret;
3360 	struct vip_dev *dev = port->dev;
3361 
3362 	vep = &port->endpoint;
3363 
3364 	subdev = fwnode_graph_get_remote_port_parent(ep);
3365 	if (!subdev) {
3366 		v4l2_dbg(3, debug, &dev->v4l2_dev, "can't get remote parent\n");
3367 		return -EINVAL;
3368 	}
3369 
3370 	ret = v4l2_fwnode_endpoint_parse(ep, vep);
3371 	if (ret) {
3372 		v4l2_dbg(3, debug, &dev->v4l2_dev, "Failed to parse endpoint:\n");
3373 		fwnode_handle_put(subdev);
3374 		return -EINVAL;
3375 	}
3376 
3377 	v4l2_async_nf_init(notifier, &port->dev->shared->v4l2_dev);
3378 
3379 	asd = v4l2_async_nf_add_fwnode(notifier, subdev, struct v4l2_async_connection);
3380 	if (IS_ERR(asd)) {
3381 		v4l2_dbg(1, debug, &dev->v4l2_dev, "Error adding asd\n");
3382 		fwnode_handle_put(subdev);
3383 		v4l2_async_nf_cleanup(notifier);
3384 		return -EINVAL;
3385 	}
3386 
3387 	notifier->ops = &vip_async_ops;
3388 	ret = v4l2_async_nf_register(notifier);
3389 	if (ret) {
3390 		v4l2_dbg(1, debug, &dev->v4l2_dev, "Error registering async notifier\n");
3391 		v4l2_async_nf_cleanup(notifier);
3392 		ret = -EINVAL;
3393 	}
3394 
3395 	return ret;
3396 }
3397 
vip_endpoint_scan(struct platform_device * pdev)3398 static int vip_endpoint_scan(struct platform_device *pdev)
3399 {
3400 	struct device_node *parent = pdev->dev.of_node;
3401 	struct device_node *ep = NULL;
3402 	int count = 0, p;
3403 
3404 	for (p = 0; p < (VIP_NUM_PORTS * VIP_NUM_SLICES); p++) {
3405 		ep = of_graph_get_endpoint_by_regs(parent, p, 0);
3406 		if (!ep)
3407 			continue;
3408 
3409 		count++;
3410 		of_node_put(ep);
3411 	}
3412 
3413 	return count;
3414 }
3415 
vip_probe_complete(struct platform_device * pdev)3416 static int vip_probe_complete(struct platform_device *pdev)
3417 {
3418 	struct vip_shared *shared = platform_get_drvdata(pdev);
3419 	struct vip_ctrl_module *ctrl = NULL;
3420 	struct vip_port *port;
3421 	struct vip_dev *dev;
3422 	struct device_node *parent = pdev->dev.of_node;
3423 	unsigned int syscon_args[5];
3424 	int ret, i, slice_id, port_id, p;
3425 
3426 	/* Allocate ctrl before using it */
3427 	ctrl = devm_kzalloc(&pdev->dev, sizeof(*ctrl), GFP_KERNEL);
3428 	if (!ctrl)
3429 		return -ENOMEM;
3430 
3431 	ctrl->syscon_pol = syscon_regmap_lookup_by_phandle_args(parent, "ti,ctrl-module",
3432 								5, syscon_args);
3433 
3434 	if (IS_ERR(ctrl->syscon_pol))
3435 		return dev_err_probe(&pdev->dev, PTR_ERR(ctrl->syscon_pol),
3436 				     "Failed to get ti,ctrl-module\n");
3437 
3438 	ctrl->syscon_offset = syscon_args[0];
3439 
3440 	for (i = 0; i < ARRAY_SIZE(ctrl->syscon_bit_field); i++)
3441 		ctrl->syscon_bit_field[i] = syscon_args[i + 1];
3442 
3443 	for (p = 0; p < (VIP_NUM_PORTS * VIP_NUM_SLICES); p++) {
3444 		struct fwnode_handle *ep __free(fwnode_handle) =
3445 			fwnode_graph_get_next_endpoint_by_regs(
3446 				of_fwnode_handle(parent), p, 0);
3447 		if (!ep)
3448 			continue;
3449 
3450 		switch (p) {
3451 		case 0:
3452 			slice_id = VIP_SLICE1;
3453 			port_id = VIP_PORTA;
3454 			break;
3455 		case 1:
3456 			slice_id = VIP_SLICE2;
3457 			port_id = VIP_PORTA;
3458 			break;
3459 		case 2:
3460 			slice_id = VIP_SLICE1;
3461 			port_id = VIP_PORTB;
3462 			break;
3463 		case 3:
3464 			slice_id = VIP_SLICE2;
3465 			port_id = VIP_PORTB;
3466 			break;
3467 		default:
3468 			dev_err(&pdev->dev, "Unknown port reg=<%d>\n", p);
3469 			continue;
3470 		}
3471 
3472 		ret = alloc_port(shared->devs[slice_id], port_id);
3473 		if (ret < 0)
3474 			continue;
3475 
3476 		dev = shared->devs[slice_id];
3477 		dev->syscon = ctrl;
3478 		port = dev->ports[port_id];
3479 
3480 		vip_register_subdev_notify(port, ep);
3481 	}
3482 	return 0;
3483 }
3484 
vip_probe_slice(struct platform_device * pdev,int slice)3485 static int vip_probe_slice(struct platform_device *pdev, int slice)
3486 {
3487 	struct vip_shared *shared = platform_get_drvdata(pdev);
3488 	struct vip_dev *dev;
3489 	struct vip_parser_data *parser;
3490 	struct sc_data *sc;
3491 	struct csc_data *csc;
3492 	int ret;
3493 
3494 	dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
3495 	if (!dev)
3496 		return -ENOMEM;
3497 
3498 	dev->irq = platform_get_irq(pdev, slice);
3499 	if (dev->irq < 0)
3500 		return dev->irq;
3501 
3502 	ret = devm_request_irq(&pdev->dev, dev->irq, vip_irq,
3503 			       0, VIP_MODULE_NAME, dev);
3504 	if (ret < 0)
3505 		return ret;
3506 
3507 	spin_lock_init(&dev->slock);
3508 	mutex_init(&dev->mutex);
3509 
3510 	dev->slice_id = slice;
3511 	dev->pdev = pdev;
3512 	dev->base = shared->base;
3513 	dev->v4l2_dev = shared->v4l2_dev;
3514 
3515 	dev->shared = shared;
3516 	shared->devs[slice] = dev;
3517 
3518 	vip_top_reset(dev);
3519 	vip_set_slice_path(dev, VIP_MULTI_CHANNEL_DATA_SELECT, 1);
3520 
3521 	parser = devm_kzalloc(&pdev->dev, sizeof(*dev->parser), GFP_KERNEL);
3522 	if (!parser)
3523 		return -ENOMEM;
3524 
3525 	parser->base = dev->base + (slice ? VIP_SLICE1_PARSER : VIP_SLICE0_PARSER);
3526 	if (IS_ERR(parser->base))
3527 		return PTR_ERR(parser->base);
3528 
3529 	parser->pdev = pdev;
3530 	dev->parser = parser;
3531 
3532 	dev->sc_assigned = VIP_NOT_ASSIGNED;
3533 	sc = devm_kzalloc(&pdev->dev, sizeof(*dev->sc), GFP_KERNEL);
3534 	if (!sc)
3535 		return -ENOMEM;
3536 
3537 	sc->base = dev->base + (slice ? VIP_SLICE1_SC : VIP_SLICE0_SC);
3538 	if (IS_ERR(sc->base))
3539 		return PTR_ERR(sc->base);
3540 
3541 	sc->pdev = pdev;
3542 	dev->sc = sc;
3543 
3544 	dev->csc_assigned = VIP_NOT_ASSIGNED;
3545 	csc = devm_kzalloc(&pdev->dev, sizeof(*dev->csc), GFP_KERNEL);
3546 	if (!csc)
3547 		return -ENOMEM;
3548 
3549 	csc->base = dev->base + (slice ? VIP_SLICE1_CSC : VIP_SLICE0_CSC);
3550 	if (IS_ERR(csc->base))
3551 		return PTR_ERR(csc->base);
3552 
3553 	csc->pdev = pdev;
3554 	dev->csc = csc;
3555 
3556 	return 0;
3557 }
3558 
vip_probe(struct platform_device * pdev)3559 static int vip_probe(struct platform_device *pdev)
3560 {
3561 	struct vip_shared *shared;
3562 	int ret, slice = VIP_SLICE1;
3563 	u32 tmp, pid;
3564 
3565 	/* If there are no endpoint defined there is nothing to do */
3566 	if (!vip_endpoint_scan(pdev)) {
3567 		dev_err(&pdev->dev, "%s: No sensor", __func__);
3568 		return -ENODEV;
3569 	}
3570 
3571 	ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
3572 	if (ret) {
3573 		dev_err(&pdev->dev,
3574 			"32-bit consistent DMA enable failed\n");
3575 		return ret;
3576 	}
3577 
3578 	shared = devm_kzalloc(&pdev->dev, sizeof(*shared), GFP_KERNEL);
3579 	if (!shared)
3580 		return -ENOMEM;
3581 
3582 	shared->base = devm_platform_ioremap_resource(pdev, 0);
3583 	if (IS_ERR(shared->base))
3584 		return PTR_ERR(shared->base);
3585 
3586 	vip_init_format_info(&pdev->dev);
3587 
3588 	pm_runtime_enable(&pdev->dev);
3589 
3590 	ret = pm_runtime_get_sync(&pdev->dev);
3591 	if (ret < 0)
3592 		goto err_runtime_disable;
3593 
3594 	/* Make sure H/W module has the right functionality */
3595 	pid = reg_read(shared, VIP_PID);
3596 	tmp = get_field(pid, VIP_PID_FUNC_MASK, VIP_PID_FUNC_SHIFT);
3597 
3598 	if (tmp != VIP_PID_FUNC) {
3599 		dev_info(&pdev->dev, "vip: unexpected PID function: 0x%x\n",
3600 			 tmp);
3601 		ret = -ENODEV;
3602 		goto err_runtime_put;
3603 	}
3604 
3605 	ret = v4l2_device_register(&pdev->dev, &shared->v4l2_dev);
3606 	if (ret)
3607 		goto err_runtime_put;
3608 
3609 	/* enable clocks, so the firmware will load properly */
3610 	vip_shared_set_clock_enable(shared, 1);
3611 	vip_top_vpdma_reset(shared);
3612 
3613 	platform_set_drvdata(pdev, shared);
3614 
3615 	v4l2_ctrl_handler_init(&shared->ctrl_handler, 11);
3616 	shared->v4l2_dev.ctrl_handler = &shared->ctrl_handler;
3617 
3618 	for (slice = VIP_SLICE1; slice < VIP_NUM_SLICES; slice++) {
3619 		ret = vip_probe_slice(pdev, slice);
3620 		if (ret) {
3621 			dev_err(&pdev->dev, "Creating slice failed");
3622 			goto err_dev_unreg;
3623 		}
3624 	}
3625 
3626 	shared->vpdma = &shared->vpdma_data;
3627 
3628 	shared->vpdma->pdev = pdev;
3629 	shared->vpdma->cb = vip_vpdma_fw_cb;
3630 	spin_lock_init(&shared->vpdma->lock);
3631 
3632 	shared->vpdma->base = shared->base + VIP_VPDMA_BASE;
3633 	if (!shared->vpdma->base) {
3634 		dev_err(&pdev->dev, "failed to ioremap\n");
3635 		ret = -ENOMEM;
3636 		goto err_dev_unreg;
3637 	}
3638 
3639 	ret = vpdma_load_firmware(shared->vpdma);
3640 	if (ret) {
3641 		dev_err(&pdev->dev, "Creating VPDMA failed");
3642 		goto err_dev_unreg;
3643 	}
3644 
3645 	return 0;
3646 
3647 err_dev_unreg:
3648 	v4l2_ctrl_handler_free(&shared->ctrl_handler);
3649 	v4l2_device_unregister(&shared->v4l2_dev);
3650 err_runtime_put:
3651 	pm_runtime_put_sync(&pdev->dev);
3652 err_runtime_disable:
3653 	pm_runtime_disable(&pdev->dev);
3654 
3655 	return ret;
3656 }
3657 
vip_remove(struct platform_device * pdev)3658 static void vip_remove(struct platform_device *pdev)
3659 {
3660 	struct vip_shared *shared = platform_get_drvdata(pdev);
3661 	struct vip_dev *dev;
3662 	int slice;
3663 
3664 	for (slice = 0; slice < VIP_NUM_SLICES; slice++) {
3665 		dev = shared->devs[slice];
3666 		if (!dev)
3667 			continue;
3668 
3669 		free_port(dev->ports[VIP_PORTA]);
3670 		free_port(dev->ports[VIP_PORTB]);
3671 	}
3672 
3673 	v4l2_ctrl_handler_free(&shared->ctrl_handler);
3674 	v4l2_device_unregister(&shared->v4l2_dev);
3675 
3676 	pm_runtime_put_sync(&pdev->dev);
3677 	pm_runtime_disable(&pdev->dev);
3678 }
3679 
3680 #if defined(CONFIG_OF)
3681 static const struct of_device_id vip_of_match[] = {
3682 	{
3683 		.compatible = "ti,dra7-vip",
3684 	},
3685 	{},
3686 };
3687 
3688 MODULE_DEVICE_TABLE(of, vip_of_match);
3689 #endif
3690 
3691 static struct platform_driver vip_pdrv = {
3692 	.probe		= vip_probe,
3693 	.remove		= vip_remove,
3694 	.driver		= {
3695 		.name	= VIP_MODULE_NAME,
3696 		.of_match_table = of_match_ptr(vip_of_match),
3697 	},
3698 };
3699 
3700 module_platform_driver(vip_pdrv);
3701 
3702 MODULE_DESCRIPTION("TI VIP driver");
3703 MODULE_AUTHOR("Texas Instruments");
3704 MODULE_LICENSE("GPL");
3705