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