xref: /linux/drivers/staging/media/omap4iss/iss_ipipeif.c (revision 06d07429858317ded2db7986113a9e0129cd599b)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * TI OMAP4 ISS V4L2 Driver - ISP IPIPEIF module
4  *
5  * Copyright (C) 2012 Texas Instruments, Inc.
6  *
7  * Author: Sergio Aguirre <sergio.a.aguirre@gmail.com>
8  */
9 
10 #include <linux/module.h>
11 #include <linux/uaccess.h>
12 #include <linux/delay.h>
13 #include <linux/device.h>
14 #include <linux/dma-mapping.h>
15 #include <linux/mm.h>
16 #include <linux/sched.h>
17 
18 #include "iss.h"
19 #include "iss_regs.h"
20 #include "iss_ipipeif.h"
21 
22 static const unsigned int ipipeif_fmts[] = {
23 	MEDIA_BUS_FMT_SGRBG10_1X10,
24 	MEDIA_BUS_FMT_SRGGB10_1X10,
25 	MEDIA_BUS_FMT_SBGGR10_1X10,
26 	MEDIA_BUS_FMT_SGBRG10_1X10,
27 	MEDIA_BUS_FMT_UYVY8_1X16,
28 	MEDIA_BUS_FMT_YUYV8_1X16,
29 };
30 
31 /*
32  * ipipeif_print_status - Print current IPIPEIF Module register values.
33  * @ipipeif: Pointer to ISS ISP IPIPEIF device.
34  *
35  * Also prints other debug information stored in the IPIPEIF module.
36  */
37 #define IPIPEIF_PRINT_REGISTER(iss, name)\
38 	dev_dbg(iss->dev, "###IPIPEIF " #name "=0x%08x\n", \
39 		iss_reg_read(iss, OMAP4_ISS_MEM_ISP_IPIPEIF, IPIPEIF_##name))
40 
41 #define ISIF_PRINT_REGISTER(iss, name)\
42 	dev_dbg(iss->dev, "###ISIF " #name "=0x%08x\n", \
43 		iss_reg_read(iss, OMAP4_ISS_MEM_ISP_ISIF, ISIF_##name))
44 
45 #define ISP5_PRINT_REGISTER(iss, name)\
46 	dev_dbg(iss->dev, "###ISP5 " #name "=0x%08x\n", \
47 		iss_reg_read(iss, OMAP4_ISS_MEM_ISP_SYS1, ISP5_##name))
48 
ipipeif_print_status(struct iss_ipipeif_device * ipipeif)49 static void ipipeif_print_status(struct iss_ipipeif_device *ipipeif)
50 {
51 	struct iss_device *iss = to_iss_device(ipipeif);
52 
53 	dev_dbg(iss->dev, "-------------IPIPEIF Register dump-------------\n");
54 
55 	IPIPEIF_PRINT_REGISTER(iss, CFG1);
56 	IPIPEIF_PRINT_REGISTER(iss, CFG2);
57 
58 	ISIF_PRINT_REGISTER(iss, SYNCEN);
59 	ISIF_PRINT_REGISTER(iss, CADU);
60 	ISIF_PRINT_REGISTER(iss, CADL);
61 	ISIF_PRINT_REGISTER(iss, MODESET);
62 	ISIF_PRINT_REGISTER(iss, CCOLP);
63 	ISIF_PRINT_REGISTER(iss, SPH);
64 	ISIF_PRINT_REGISTER(iss, LNH);
65 	ISIF_PRINT_REGISTER(iss, LNV);
66 	ISIF_PRINT_REGISTER(iss, VDINT(0));
67 	ISIF_PRINT_REGISTER(iss, HSIZE);
68 
69 	ISP5_PRINT_REGISTER(iss, SYSCONFIG);
70 	ISP5_PRINT_REGISTER(iss, CTRL);
71 	ISP5_PRINT_REGISTER(iss, IRQSTATUS(0));
72 	ISP5_PRINT_REGISTER(iss, IRQENABLE_SET(0));
73 	ISP5_PRINT_REGISTER(iss, IRQENABLE_CLR(0));
74 
75 	dev_dbg(iss->dev, "-----------------------------------------------\n");
76 }
77 
ipipeif_write_enable(struct iss_ipipeif_device * ipipeif,u8 enable)78 static void ipipeif_write_enable(struct iss_ipipeif_device *ipipeif, u8 enable)
79 {
80 	struct iss_device *iss = to_iss_device(ipipeif);
81 
82 	iss_reg_update(iss, OMAP4_ISS_MEM_ISP_ISIF, ISIF_SYNCEN,
83 		       ISIF_SYNCEN_DWEN, enable ? ISIF_SYNCEN_DWEN : 0);
84 }
85 
86 /*
87  * ipipeif_enable - Enable/Disable IPIPEIF.
88  * @enable: enable flag
89  *
90  */
ipipeif_enable(struct iss_ipipeif_device * ipipeif,u8 enable)91 static void ipipeif_enable(struct iss_ipipeif_device *ipipeif, u8 enable)
92 {
93 	struct iss_device *iss = to_iss_device(ipipeif);
94 
95 	iss_reg_update(iss, OMAP4_ISS_MEM_ISP_ISIF, ISIF_SYNCEN,
96 		       ISIF_SYNCEN_SYEN, enable ? ISIF_SYNCEN_SYEN : 0);
97 }
98 
99 /* -----------------------------------------------------------------------------
100  * Format- and pipeline-related configuration helpers
101  */
102 
103 /*
104  * ipipeif_set_outaddr - Set memory address to save output image
105  * @ipipeif: Pointer to ISP IPIPEIF device.
106  * @addr: 32-bit memory address aligned on 32 byte boundary.
107  *
108  * Sets the memory address where the output will be saved.
109  */
ipipeif_set_outaddr(struct iss_ipipeif_device * ipipeif,u32 addr)110 static void ipipeif_set_outaddr(struct iss_ipipeif_device *ipipeif, u32 addr)
111 {
112 	struct iss_device *iss = to_iss_device(ipipeif);
113 
114 	/* Save address split in Base Address H & L */
115 	iss_reg_write(iss, OMAP4_ISS_MEM_ISP_ISIF, ISIF_CADU,
116 		      (addr >> (16 + 5)) & ISIF_CADU_MASK);
117 	iss_reg_write(iss, OMAP4_ISS_MEM_ISP_ISIF, ISIF_CADL,
118 		      (addr >> 5) & ISIF_CADL_MASK);
119 }
120 
ipipeif_configure(struct iss_ipipeif_device * ipipeif)121 static void ipipeif_configure(struct iss_ipipeif_device *ipipeif)
122 {
123 	struct iss_device *iss = to_iss_device(ipipeif);
124 	const struct iss_format_info *info;
125 	struct v4l2_mbus_framefmt *format;
126 	u32 isif_ccolp = 0;
127 
128 	omap4iss_configure_bridge(iss, ipipeif->input);
129 
130 	/* IPIPEIF_PAD_SINK */
131 	format = &ipipeif->formats[IPIPEIF_PAD_SINK];
132 
133 	/* IPIPEIF with YUV422 input from ISIF */
134 	iss_reg_clr(iss, OMAP4_ISS_MEM_ISP_IPIPEIF, IPIPEIF_CFG1,
135 		    IPIPEIF_CFG1_INPSRC1_MASK | IPIPEIF_CFG1_INPSRC2_MASK);
136 
137 	/* Select ISIF/IPIPEIF input format */
138 	switch (format->code) {
139 	case MEDIA_BUS_FMT_UYVY8_1X16:
140 	case MEDIA_BUS_FMT_YUYV8_1X16:
141 		iss_reg_update(iss, OMAP4_ISS_MEM_ISP_ISIF, ISIF_MODESET,
142 			       ISIF_MODESET_CCDMD | ISIF_MODESET_INPMOD_MASK |
143 			       ISIF_MODESET_CCDW_MASK,
144 			       ISIF_MODESET_INPMOD_YCBCR16);
145 
146 		iss_reg_update(iss, OMAP4_ISS_MEM_ISP_IPIPEIF, IPIPEIF_CFG2,
147 			       IPIPEIF_CFG2_YUV8, IPIPEIF_CFG2_YUV16);
148 
149 		break;
150 	case MEDIA_BUS_FMT_SGRBG10_1X10:
151 		isif_ccolp = ISIF_CCOLP_CP0_F0_GR |
152 			ISIF_CCOLP_CP1_F0_R |
153 			ISIF_CCOLP_CP2_F0_B |
154 			ISIF_CCOLP_CP3_F0_GB;
155 		goto cont_raw;
156 	case MEDIA_BUS_FMT_SRGGB10_1X10:
157 		isif_ccolp = ISIF_CCOLP_CP0_F0_R |
158 			ISIF_CCOLP_CP1_F0_GR |
159 			ISIF_CCOLP_CP2_F0_GB |
160 			ISIF_CCOLP_CP3_F0_B;
161 		goto cont_raw;
162 	case MEDIA_BUS_FMT_SBGGR10_1X10:
163 		isif_ccolp = ISIF_CCOLP_CP0_F0_B |
164 			ISIF_CCOLP_CP1_F0_GB |
165 			ISIF_CCOLP_CP2_F0_GR |
166 			ISIF_CCOLP_CP3_F0_R;
167 		goto cont_raw;
168 	case MEDIA_BUS_FMT_SGBRG10_1X10:
169 		isif_ccolp = ISIF_CCOLP_CP0_F0_GB |
170 			ISIF_CCOLP_CP1_F0_B |
171 			ISIF_CCOLP_CP2_F0_R |
172 			ISIF_CCOLP_CP3_F0_GR;
173 cont_raw:
174 		iss_reg_clr(iss, OMAP4_ISS_MEM_ISP_IPIPEIF, IPIPEIF_CFG2,
175 			    IPIPEIF_CFG2_YUV16);
176 
177 		iss_reg_update(iss, OMAP4_ISS_MEM_ISP_ISIF, ISIF_MODESET,
178 			       ISIF_MODESET_CCDMD | ISIF_MODESET_INPMOD_MASK |
179 			       ISIF_MODESET_CCDW_MASK, ISIF_MODESET_INPMOD_RAW |
180 			       ISIF_MODESET_CCDW_2BIT);
181 
182 		info = omap4iss_video_format_info(format->code);
183 		iss_reg_update(iss, OMAP4_ISS_MEM_ISP_ISIF, ISIF_CGAMMAWD,
184 			       ISIF_CGAMMAWD_GWDI_MASK,
185 			       ISIF_CGAMMAWD_GWDI(info->bpp));
186 
187 		/* Set RAW Bayer pattern */
188 		iss_reg_write(iss, OMAP4_ISS_MEM_ISP_ISIF, ISIF_CCOLP,
189 			      isif_ccolp);
190 		break;
191 	}
192 
193 	iss_reg_write(iss, OMAP4_ISS_MEM_ISP_ISIF, ISIF_SPH, 0 & ISIF_SPH_MASK);
194 	iss_reg_write(iss, OMAP4_ISS_MEM_ISP_ISIF, ISIF_LNH,
195 		      (format->width - 1) & ISIF_LNH_MASK);
196 	iss_reg_write(iss, OMAP4_ISS_MEM_ISP_ISIF, ISIF_LNV,
197 		      (format->height - 1) & ISIF_LNV_MASK);
198 
199 	/* Generate ISIF0 on the last line of the image */
200 	iss_reg_write(iss, OMAP4_ISS_MEM_ISP_ISIF, ISIF_VDINT(0),
201 		      format->height - 1);
202 
203 	/* IPIPEIF_PAD_SOURCE_ISIF_SF */
204 	format = &ipipeif->formats[IPIPEIF_PAD_SOURCE_ISIF_SF];
205 
206 	iss_reg_write(iss, OMAP4_ISS_MEM_ISP_ISIF, ISIF_HSIZE,
207 		      (ipipeif->video_out.bpl_value >> 5) &
208 		      ISIF_HSIZE_HSIZE_MASK);
209 
210 	/* IPIPEIF_PAD_SOURCE_VP */
211 	/* Do nothing? */
212 }
213 
214 /* -----------------------------------------------------------------------------
215  * Interrupt handling
216  */
217 
ipipeif_isr_buffer(struct iss_ipipeif_device * ipipeif)218 static void ipipeif_isr_buffer(struct iss_ipipeif_device *ipipeif)
219 {
220 	struct iss_buffer *buffer;
221 
222 	/* The ISIF generates VD0 interrupts even when writes are disabled.
223 	 * deal with it anyway). Disabling the ISIF when no buffer is available
224 	 * is thus not be enough, we need to handle the situation explicitly.
225 	 */
226 	if (list_empty(&ipipeif->video_out.dmaqueue))
227 		return;
228 
229 	ipipeif_write_enable(ipipeif, 0);
230 
231 	buffer = omap4iss_video_buffer_next(&ipipeif->video_out);
232 	if (!buffer)
233 		return;
234 
235 	ipipeif_set_outaddr(ipipeif, buffer->iss_addr);
236 
237 	ipipeif_write_enable(ipipeif, 1);
238 }
239 
240 /*
241  * omap4iss_ipipeif_isr - Configure ipipeif during interframe time.
242  * @ipipeif: Pointer to ISP IPIPEIF device.
243  * @events: IPIPEIF events
244  */
omap4iss_ipipeif_isr(struct iss_ipipeif_device * ipipeif,u32 events)245 void omap4iss_ipipeif_isr(struct iss_ipipeif_device *ipipeif, u32 events)
246 {
247 	if (omap4iss_module_sync_is_stopping(&ipipeif->wait,
248 					     &ipipeif->stopping))
249 		return;
250 
251 	if ((events & ISP5_IRQ_ISIF_INT(0)) &&
252 	    (ipipeif->output & IPIPEIF_OUTPUT_MEMORY))
253 		ipipeif_isr_buffer(ipipeif);
254 }
255 
256 /* -----------------------------------------------------------------------------
257  * ISP video operations
258  */
259 
ipipeif_video_queue(struct iss_video * video,struct iss_buffer * buffer)260 static int ipipeif_video_queue(struct iss_video *video,
261 			       struct iss_buffer *buffer)
262 {
263 	struct iss_ipipeif_device *ipipeif = container_of(video,
264 				struct iss_ipipeif_device, video_out);
265 
266 	if (!(ipipeif->output & IPIPEIF_OUTPUT_MEMORY))
267 		return -ENODEV;
268 
269 	ipipeif_set_outaddr(ipipeif, buffer->iss_addr);
270 
271 	/*
272 	 * If streaming was enabled before there was a buffer queued
273 	 * or underrun happened in the ISR, the hardware was not enabled
274 	 * and DMA queue flag ISS_VIDEO_DMAQUEUE_UNDERRUN is still set.
275 	 * Enable it now.
276 	 */
277 	if (video->dmaqueue_flags & ISS_VIDEO_DMAQUEUE_UNDERRUN) {
278 		if (ipipeif->output & IPIPEIF_OUTPUT_MEMORY)
279 			ipipeif_write_enable(ipipeif, 1);
280 		ipipeif_enable(ipipeif, 1);
281 		iss_video_dmaqueue_flags_clr(video);
282 	}
283 
284 	return 0;
285 }
286 
287 static const struct iss_video_operations ipipeif_video_ops = {
288 	.queue = ipipeif_video_queue,
289 };
290 
291 /* -----------------------------------------------------------------------------
292  * V4L2 subdev operations
293  */
294 
295 #define IPIPEIF_DRV_SUBCLK_MASK	(OMAP4_ISS_ISP_SUBCLK_IPIPEIF |\
296 				 OMAP4_ISS_ISP_SUBCLK_ISIF)
297 /*
298  * ipipeif_set_stream - Enable/Disable streaming on the IPIPEIF module
299  * @sd: ISP IPIPEIF V4L2 subdevice
300  * @enable: Enable/disable stream
301  */
ipipeif_set_stream(struct v4l2_subdev * sd,int enable)302 static int ipipeif_set_stream(struct v4l2_subdev *sd, int enable)
303 {
304 	struct iss_ipipeif_device *ipipeif = v4l2_get_subdevdata(sd);
305 	struct iss_device *iss = to_iss_device(ipipeif);
306 	struct iss_video *video_out = &ipipeif->video_out;
307 	int ret = 0;
308 
309 	if (ipipeif->state == ISS_PIPELINE_STREAM_STOPPED) {
310 		if (enable == ISS_PIPELINE_STREAM_STOPPED)
311 			return 0;
312 
313 		omap4iss_isp_subclk_enable(iss, IPIPEIF_DRV_SUBCLK_MASK);
314 	}
315 
316 	switch (enable) {
317 	case ISS_PIPELINE_STREAM_CONTINUOUS:
318 
319 		ipipeif_configure(ipipeif);
320 		ipipeif_print_status(ipipeif);
321 
322 		/*
323 		 * When outputting to memory with no buffer available, let the
324 		 * buffer queue handler start the hardware. A DMA queue flag
325 		 * ISS_VIDEO_DMAQUEUE_QUEUED will be set as soon as there is
326 		 * a buffer available.
327 		 */
328 		if (ipipeif->output & IPIPEIF_OUTPUT_MEMORY &&
329 		    !(video_out->dmaqueue_flags & ISS_VIDEO_DMAQUEUE_QUEUED))
330 			break;
331 
332 		atomic_set(&ipipeif->stopping, 0);
333 		if (ipipeif->output & IPIPEIF_OUTPUT_MEMORY)
334 			ipipeif_write_enable(ipipeif, 1);
335 		ipipeif_enable(ipipeif, 1);
336 		iss_video_dmaqueue_flags_clr(video_out);
337 		break;
338 
339 	case ISS_PIPELINE_STREAM_STOPPED:
340 		if (ipipeif->state == ISS_PIPELINE_STREAM_STOPPED)
341 			return 0;
342 		if (omap4iss_module_sync_idle(&sd->entity, &ipipeif->wait,
343 					      &ipipeif->stopping))
344 			ret = -ETIMEDOUT;
345 
346 		if (ipipeif->output & IPIPEIF_OUTPUT_MEMORY)
347 			ipipeif_write_enable(ipipeif, 0);
348 		ipipeif_enable(ipipeif, 0);
349 		omap4iss_isp_subclk_disable(iss, IPIPEIF_DRV_SUBCLK_MASK);
350 		iss_video_dmaqueue_flags_clr(video_out);
351 		break;
352 	}
353 
354 	ipipeif->state = enable;
355 	return ret;
356 }
357 
358 static struct v4l2_mbus_framefmt *
__ipipeif_get_format(struct iss_ipipeif_device * ipipeif,struct v4l2_subdev_state * sd_state,unsigned int pad,enum v4l2_subdev_format_whence which)359 __ipipeif_get_format(struct iss_ipipeif_device *ipipeif,
360 		     struct v4l2_subdev_state *sd_state, unsigned int pad,
361 		     enum v4l2_subdev_format_whence which)
362 {
363 	if (which == V4L2_SUBDEV_FORMAT_TRY)
364 		return v4l2_subdev_state_get_format(sd_state, pad);
365 	return &ipipeif->formats[pad];
366 }
367 
368 /*
369  * ipipeif_try_format - Try video format on a pad
370  * @ipipeif: ISS IPIPEIF device
371  * @sd_state: V4L2 subdev state
372  * @pad: Pad number
373  * @fmt: Format
374  */
375 static void
ipipeif_try_format(struct iss_ipipeif_device * ipipeif,struct v4l2_subdev_state * sd_state,unsigned int pad,struct v4l2_mbus_framefmt * fmt,enum v4l2_subdev_format_whence which)376 ipipeif_try_format(struct iss_ipipeif_device *ipipeif,
377 		   struct v4l2_subdev_state *sd_state, unsigned int pad,
378 		   struct v4l2_mbus_framefmt *fmt,
379 		   enum v4l2_subdev_format_whence which)
380 {
381 	struct v4l2_mbus_framefmt *format;
382 	unsigned int width = fmt->width;
383 	unsigned int height = fmt->height;
384 	unsigned int i;
385 
386 	switch (pad) {
387 	case IPIPEIF_PAD_SINK:
388 		/* TODO: If the IPIPEIF output formatter pad is connected
389 		 * directly to the resizer, only YUV formats can be used.
390 		 */
391 		for (i = 0; i < ARRAY_SIZE(ipipeif_fmts); i++) {
392 			if (fmt->code == ipipeif_fmts[i])
393 				break;
394 		}
395 
396 		/* If not found, use SGRBG10 as default */
397 		if (i >= ARRAY_SIZE(ipipeif_fmts))
398 			fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
399 
400 		/* Clamp the input size. */
401 		fmt->width = clamp_t(u32, width, 1, 8192);
402 		fmt->height = clamp_t(u32, height, 1, 8192);
403 		break;
404 
405 	case IPIPEIF_PAD_SOURCE_ISIF_SF:
406 		format = __ipipeif_get_format(ipipeif, sd_state,
407 					      IPIPEIF_PAD_SINK,
408 					      which);
409 		memcpy(fmt, format, sizeof(*fmt));
410 
411 		/* The data formatter truncates the number of horizontal output
412 		 * pixels to a multiple of 16. To avoid clipping data, allow
413 		 * callers to request an output size bigger than the input size
414 		 * up to the nearest multiple of 16.
415 		 */
416 		fmt->width = clamp_t(u32, width, 32, (fmt->width + 15) & ~15);
417 		fmt->width &= ~15;
418 		fmt->height = clamp_t(u32, height, 32, fmt->height);
419 		break;
420 
421 	case IPIPEIF_PAD_SOURCE_VP:
422 		format = __ipipeif_get_format(ipipeif, sd_state,
423 					      IPIPEIF_PAD_SINK,
424 					      which);
425 		memcpy(fmt, format, sizeof(*fmt));
426 
427 		fmt->width = clamp_t(u32, width, 32, fmt->width);
428 		fmt->height = clamp_t(u32, height, 32, fmt->height);
429 		break;
430 	}
431 
432 	/* Data is written to memory unpacked, each 10-bit or 12-bit pixel is
433 	 * stored on 2 bytes.
434 	 */
435 	fmt->colorspace = V4L2_COLORSPACE_SRGB;
436 	fmt->field = V4L2_FIELD_NONE;
437 }
438 
439 /*
440  * ipipeif_enum_mbus_code - Handle pixel format enumeration
441  * @sd     : pointer to v4l2 subdev structure
442  * @sd_state: V4L2 subdev state
443  * @code   : pointer to v4l2_subdev_mbus_code_enum structure
444  * return -EINVAL or zero on success
445  */
ipipeif_enum_mbus_code(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_mbus_code_enum * code)446 static int ipipeif_enum_mbus_code(struct v4l2_subdev *sd,
447 				  struct v4l2_subdev_state *sd_state,
448 				  struct v4l2_subdev_mbus_code_enum *code)
449 {
450 	struct iss_ipipeif_device *ipipeif = v4l2_get_subdevdata(sd);
451 	struct v4l2_mbus_framefmt *format;
452 
453 	switch (code->pad) {
454 	case IPIPEIF_PAD_SINK:
455 		if (code->index >= ARRAY_SIZE(ipipeif_fmts))
456 			return -EINVAL;
457 
458 		code->code = ipipeif_fmts[code->index];
459 		break;
460 
461 	case IPIPEIF_PAD_SOURCE_ISIF_SF:
462 	case IPIPEIF_PAD_SOURCE_VP:
463 		/* No format conversion inside IPIPEIF */
464 		if (code->index != 0)
465 			return -EINVAL;
466 
467 		format = __ipipeif_get_format(ipipeif, sd_state,
468 					      IPIPEIF_PAD_SINK,
469 					      code->which);
470 
471 		code->code = format->code;
472 		break;
473 
474 	default:
475 		return -EINVAL;
476 	}
477 
478 	return 0;
479 }
480 
ipipeif_enum_frame_size(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_frame_size_enum * fse)481 static int ipipeif_enum_frame_size(struct v4l2_subdev *sd,
482 				   struct v4l2_subdev_state *sd_state,
483 				   struct v4l2_subdev_frame_size_enum *fse)
484 {
485 	struct iss_ipipeif_device *ipipeif = v4l2_get_subdevdata(sd);
486 	struct v4l2_mbus_framefmt format;
487 
488 	if (fse->index != 0)
489 		return -EINVAL;
490 
491 	format.code = fse->code;
492 	format.width = 1;
493 	format.height = 1;
494 	ipipeif_try_format(ipipeif, sd_state, fse->pad, &format, fse->which);
495 	fse->min_width = format.width;
496 	fse->min_height = format.height;
497 
498 	if (format.code != fse->code)
499 		return -EINVAL;
500 
501 	format.code = fse->code;
502 	format.width = -1;
503 	format.height = -1;
504 	ipipeif_try_format(ipipeif, sd_state, fse->pad, &format, fse->which);
505 	fse->max_width = format.width;
506 	fse->max_height = format.height;
507 
508 	return 0;
509 }
510 
511 /*
512  * ipipeif_get_format - Retrieve the video format on a pad
513  * @sd : ISP IPIPEIF V4L2 subdevice
514  * @sd_state: V4L2 subdev state
515  * @fmt: Format
516  *
517  * Return 0 on success or -EINVAL if the pad is invalid or doesn't correspond
518  * to the format type.
519  */
ipipeif_get_format(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_format * fmt)520 static int ipipeif_get_format(struct v4l2_subdev *sd,
521 			      struct v4l2_subdev_state *sd_state,
522 			      struct v4l2_subdev_format *fmt)
523 {
524 	struct iss_ipipeif_device *ipipeif = v4l2_get_subdevdata(sd);
525 	struct v4l2_mbus_framefmt *format;
526 
527 	format = __ipipeif_get_format(ipipeif, sd_state, fmt->pad, fmt->which);
528 	if (!format)
529 		return -EINVAL;
530 
531 	fmt->format = *format;
532 	return 0;
533 }
534 
535 /*
536  * ipipeif_set_format - Set the video format on a pad
537  * @sd : ISP IPIPEIF V4L2 subdevice
538  * @sd_state: V4L2 subdev state
539  * @fmt: Format
540  *
541  * Return 0 on success or -EINVAL if the pad is invalid or doesn't correspond
542  * to the format type.
543  */
ipipeif_set_format(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_format * fmt)544 static int ipipeif_set_format(struct v4l2_subdev *sd,
545 			      struct v4l2_subdev_state *sd_state,
546 			      struct v4l2_subdev_format *fmt)
547 {
548 	struct iss_ipipeif_device *ipipeif = v4l2_get_subdevdata(sd);
549 	struct v4l2_mbus_framefmt *format;
550 
551 	format = __ipipeif_get_format(ipipeif, sd_state, fmt->pad, fmt->which);
552 	if (!format)
553 		return -EINVAL;
554 
555 	ipipeif_try_format(ipipeif, sd_state, fmt->pad, &fmt->format,
556 			   fmt->which);
557 	*format = fmt->format;
558 
559 	/* Propagate the format from sink to source */
560 	if (fmt->pad == IPIPEIF_PAD_SINK) {
561 		format = __ipipeif_get_format(ipipeif, sd_state,
562 					      IPIPEIF_PAD_SOURCE_ISIF_SF,
563 					      fmt->which);
564 		*format = fmt->format;
565 		ipipeif_try_format(ipipeif, sd_state,
566 				   IPIPEIF_PAD_SOURCE_ISIF_SF,
567 				   format, fmt->which);
568 
569 		format = __ipipeif_get_format(ipipeif, sd_state,
570 					      IPIPEIF_PAD_SOURCE_VP,
571 					      fmt->which);
572 		*format = fmt->format;
573 		ipipeif_try_format(ipipeif, sd_state, IPIPEIF_PAD_SOURCE_VP,
574 				   format,
575 				   fmt->which);
576 	}
577 
578 	return 0;
579 }
580 
ipipeif_link_validate(struct v4l2_subdev * sd,struct media_link * link,struct v4l2_subdev_format * source_fmt,struct v4l2_subdev_format * sink_fmt)581 static int ipipeif_link_validate(struct v4l2_subdev *sd,
582 				 struct media_link *link,
583 				 struct v4l2_subdev_format *source_fmt,
584 				 struct v4l2_subdev_format *sink_fmt)
585 {
586 	/* Check if the two ends match */
587 	if (source_fmt->format.width != sink_fmt->format.width ||
588 	    source_fmt->format.height != sink_fmt->format.height)
589 		return -EPIPE;
590 
591 	if (source_fmt->format.code != sink_fmt->format.code)
592 		return -EPIPE;
593 
594 	return 0;
595 }
596 
597 /*
598  * ipipeif_init_formats - Initialize formats on all pads
599  * @sd: ISP IPIPEIF V4L2 subdevice
600  * @fh: V4L2 subdev file handle
601  *
602  * Initialize all pad formats with default values. If fh is not NULL, try
603  * formats are initialized on the file handle. Otherwise active formats are
604  * initialized on the device.
605  */
ipipeif_init_formats(struct v4l2_subdev * sd,struct v4l2_subdev_fh * fh)606 static int ipipeif_init_formats(struct v4l2_subdev *sd,
607 				struct v4l2_subdev_fh *fh)
608 {
609 	struct v4l2_subdev_format format;
610 
611 	memset(&format, 0, sizeof(format));
612 	format.pad = IPIPEIF_PAD_SINK;
613 	format.which = fh ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
614 	format.format.code = MEDIA_BUS_FMT_SGRBG10_1X10;
615 	format.format.width = 4096;
616 	format.format.height = 4096;
617 	ipipeif_set_format(sd, fh ? fh->state : NULL, &format);
618 
619 	return 0;
620 }
621 
622 /* V4L2 subdev video operations */
623 static const struct v4l2_subdev_video_ops ipipeif_v4l2_video_ops = {
624 	.s_stream = ipipeif_set_stream,
625 };
626 
627 /* V4L2 subdev pad operations */
628 static const struct v4l2_subdev_pad_ops ipipeif_v4l2_pad_ops = {
629 	.enum_mbus_code = ipipeif_enum_mbus_code,
630 	.enum_frame_size = ipipeif_enum_frame_size,
631 	.get_fmt = ipipeif_get_format,
632 	.set_fmt = ipipeif_set_format,
633 	.link_validate = ipipeif_link_validate,
634 };
635 
636 /* V4L2 subdev operations */
637 static const struct v4l2_subdev_ops ipipeif_v4l2_ops = {
638 	.video = &ipipeif_v4l2_video_ops,
639 	.pad = &ipipeif_v4l2_pad_ops,
640 };
641 
642 /* V4L2 subdev internal operations */
643 static const struct v4l2_subdev_internal_ops ipipeif_v4l2_internal_ops = {
644 	.open = ipipeif_init_formats,
645 };
646 
647 /* -----------------------------------------------------------------------------
648  * Media entity operations
649  */
650 
651 /*
652  * ipipeif_link_setup - Setup IPIPEIF connections
653  * @entity: IPIPEIF media entity
654  * @local: Pad at the local end of the link
655  * @remote: Pad at the remote end of the link
656  * @flags: Link flags
657  *
658  * return -EINVAL or zero on success
659  */
ipipeif_link_setup(struct media_entity * entity,const struct media_pad * local,const struct media_pad * remote,u32 flags)660 static int ipipeif_link_setup(struct media_entity *entity,
661 			      const struct media_pad *local,
662 			      const struct media_pad *remote, u32 flags)
663 {
664 	struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
665 	struct iss_ipipeif_device *ipipeif = v4l2_get_subdevdata(sd);
666 	struct iss_device *iss = to_iss_device(ipipeif);
667 	unsigned int index = local->index;
668 
669 	/* FIXME: this is actually a hack! */
670 	if (is_media_entity_v4l2_subdev(remote->entity))
671 		index |= 2 << 16;
672 
673 	switch (index) {
674 	case IPIPEIF_PAD_SINK | 2 << 16:
675 		/* Read from the sensor CSI2a or CSI2b. */
676 		if (!(flags & MEDIA_LNK_FL_ENABLED)) {
677 			ipipeif->input = IPIPEIF_INPUT_NONE;
678 			break;
679 		}
680 
681 		if (ipipeif->input != IPIPEIF_INPUT_NONE)
682 			return -EBUSY;
683 
684 		if (remote->entity == &iss->csi2a.subdev.entity)
685 			ipipeif->input = IPIPEIF_INPUT_CSI2A;
686 		else if (remote->entity == &iss->csi2b.subdev.entity)
687 			ipipeif->input = IPIPEIF_INPUT_CSI2B;
688 
689 		break;
690 
691 	case IPIPEIF_PAD_SOURCE_ISIF_SF:
692 		/* Write to memory */
693 		if (flags & MEDIA_LNK_FL_ENABLED) {
694 			if (ipipeif->output & ~IPIPEIF_OUTPUT_MEMORY)
695 				return -EBUSY;
696 			ipipeif->output |= IPIPEIF_OUTPUT_MEMORY;
697 		} else {
698 			ipipeif->output &= ~IPIPEIF_OUTPUT_MEMORY;
699 		}
700 		break;
701 
702 	case IPIPEIF_PAD_SOURCE_VP | 2 << 16:
703 		/* Send to IPIPE/RESIZER */
704 		if (flags & MEDIA_LNK_FL_ENABLED) {
705 			if (ipipeif->output & ~IPIPEIF_OUTPUT_VP)
706 				return -EBUSY;
707 			ipipeif->output |= IPIPEIF_OUTPUT_VP;
708 		} else {
709 			ipipeif->output &= ~IPIPEIF_OUTPUT_VP;
710 		}
711 		break;
712 
713 	default:
714 		return -EINVAL;
715 	}
716 
717 	return 0;
718 }
719 
720 /* media operations */
721 static const struct media_entity_operations ipipeif_media_ops = {
722 	.link_setup = ipipeif_link_setup,
723 	.link_validate = v4l2_subdev_link_validate,
724 };
725 
726 /*
727  * ipipeif_init_entities - Initialize V4L2 subdev and media entity
728  * @ipipeif: ISS ISP IPIPEIF module
729  *
730  * Return 0 on success and a negative error code on failure.
731  */
ipipeif_init_entities(struct iss_ipipeif_device * ipipeif)732 static int ipipeif_init_entities(struct iss_ipipeif_device *ipipeif)
733 {
734 	struct v4l2_subdev *sd = &ipipeif->subdev;
735 	struct media_pad *pads = ipipeif->pads;
736 	struct media_entity *me = &sd->entity;
737 	int ret;
738 
739 	ipipeif->input = IPIPEIF_INPUT_NONE;
740 
741 	v4l2_subdev_init(sd, &ipipeif_v4l2_ops);
742 	sd->internal_ops = &ipipeif_v4l2_internal_ops;
743 	strscpy(sd->name, "OMAP4 ISS ISP IPIPEIF", sizeof(sd->name));
744 	sd->grp_id = BIT(16);	/* group ID for iss subdevs */
745 	v4l2_set_subdevdata(sd, ipipeif);
746 	sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
747 
748 	pads[IPIPEIF_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
749 	pads[IPIPEIF_PAD_SOURCE_ISIF_SF].flags = MEDIA_PAD_FL_SOURCE;
750 	pads[IPIPEIF_PAD_SOURCE_VP].flags = MEDIA_PAD_FL_SOURCE;
751 
752 	me->ops = &ipipeif_media_ops;
753 	ret = media_entity_pads_init(me, IPIPEIF_PADS_NUM, pads);
754 	if (ret < 0)
755 		return ret;
756 
757 	ipipeif_init_formats(sd, NULL);
758 
759 	ipipeif->video_out.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
760 	ipipeif->video_out.ops = &ipipeif_video_ops;
761 	ipipeif->video_out.iss = to_iss_device(ipipeif);
762 	ipipeif->video_out.capture_mem = PAGE_ALIGN(4096 * 4096) * 3;
763 	ipipeif->video_out.bpl_alignment = 32;
764 	ipipeif->video_out.bpl_zero_padding = 1;
765 	ipipeif->video_out.bpl_max = 0x1ffe0;
766 
767 	return omap4iss_video_init(&ipipeif->video_out, "ISP IPIPEIF");
768 }
769 
omap4iss_ipipeif_unregister_entities(struct iss_ipipeif_device * ipipeif)770 void omap4iss_ipipeif_unregister_entities(struct iss_ipipeif_device *ipipeif)
771 {
772 	v4l2_device_unregister_subdev(&ipipeif->subdev);
773 	omap4iss_video_unregister(&ipipeif->video_out);
774 }
775 
omap4iss_ipipeif_register_entities(struct iss_ipipeif_device * ipipeif,struct v4l2_device * vdev)776 int omap4iss_ipipeif_register_entities(struct iss_ipipeif_device *ipipeif,
777 				       struct v4l2_device *vdev)
778 {
779 	int ret;
780 
781 	/* Register the subdev and video node. */
782 	ret = v4l2_device_register_subdev(vdev, &ipipeif->subdev);
783 	if (ret < 0)
784 		goto error;
785 
786 	ret = omap4iss_video_register(&ipipeif->video_out, vdev);
787 	if (ret < 0)
788 		goto error;
789 
790 	return 0;
791 
792 error:
793 	omap4iss_ipipeif_unregister_entities(ipipeif);
794 	return ret;
795 }
796 
797 /* -----------------------------------------------------------------------------
798  * ISP IPIPEIF initialisation and cleanup
799  */
800 
801 /*
802  * omap4iss_ipipeif_init - IPIPEIF module initialization.
803  * @iss: Device pointer specific to the OMAP4 ISS.
804  *
805  * TODO: Get the initialisation values from platform data.
806  *
807  * Return 0 on success or a negative error code otherwise.
808  */
omap4iss_ipipeif_init(struct iss_device * iss)809 int omap4iss_ipipeif_init(struct iss_device *iss)
810 {
811 	struct iss_ipipeif_device *ipipeif = &iss->ipipeif;
812 
813 	ipipeif->state = ISS_PIPELINE_STREAM_STOPPED;
814 	init_waitqueue_head(&ipipeif->wait);
815 
816 	return ipipeif_init_entities(ipipeif);
817 }
818 
819 /*
820  * omap4iss_ipipeif_create_links() - IPIPEIF pads links creation
821  * @iss: Pointer to ISS device
822  *
823  * return negative error code or zero on success
824  */
omap4iss_ipipeif_create_links(struct iss_device * iss)825 int omap4iss_ipipeif_create_links(struct iss_device *iss)
826 {
827 	struct iss_ipipeif_device *ipipeif = &iss->ipipeif;
828 
829 	/* Connect the IPIPEIF subdev to the video node. */
830 	return media_create_pad_link(&ipipeif->subdev.entity,
831 				     IPIPEIF_PAD_SOURCE_ISIF_SF,
832 				     &ipipeif->video_out.video.entity, 0, 0);
833 }
834 
835 /*
836  * omap4iss_ipipeif_cleanup - IPIPEIF module cleanup.
837  * @iss: Device pointer specific to the OMAP4 ISS.
838  */
omap4iss_ipipeif_cleanup(struct iss_device * iss)839 void omap4iss_ipipeif_cleanup(struct iss_device *iss)
840 {
841 	struct iss_ipipeif_device *ipipeif = &iss->ipipeif;
842 
843 	media_entity_cleanup(&ipipeif->subdev.entity);
844 }
845