iss_ipipeif.c revision 11abbfd30f74f79fe78d9ff79cc3fcfa86a975c5
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	omap4iss_isp_enable_interrupts(iss);
218}
219
220/* -----------------------------------------------------------------------------
221 * Interrupt handling
222 */
223
224static void ipipeif_isr_buffer(struct iss_ipipeif_device *ipipeif)
225{
226	struct iss_buffer *buffer;
227
228	/* The ISIF generates VD0 interrupts even when writes are disabled.
229	 * deal with it anyway). Disabling the ISIF when no buffer is available
230	 * is thus not be enough, we need to handle the situation explicitly.
231	 */
232	if (list_empty(&ipipeif->video_out.dmaqueue))
233		return;
234
235	ipipeif_write_enable(ipipeif, 0);
236
237	buffer = omap4iss_video_buffer_next(&ipipeif->video_out);
238	if (buffer == NULL)
239		return;
240
241	ipipeif_set_outaddr(ipipeif, buffer->iss_addr);
242
243	ipipeif_write_enable(ipipeif, 1);
244}
245
246/*
247 * ipipeif_isif0_isr - Handle ISIF0 event
248 * @ipipeif: Pointer to ISP IPIPEIF device.
249 *
250 * Executes LSC deferred enablement before next frame starts.
251 */
252static void ipipeif_isif0_isr(struct iss_ipipeif_device *ipipeif)
253{
254	struct iss_pipeline *pipe =
255			     to_iss_pipeline(&ipipeif->subdev.entity);
256	if (pipe->do_propagation)
257		atomic_inc(&pipe->frame_number);
258
259	if (ipipeif->output & IPIPEIF_OUTPUT_MEMORY)
260		ipipeif_isr_buffer(ipipeif);
261}
262
263/*
264 * omap4iss_ipipeif_isr - Configure ipipeif during interframe time.
265 * @ipipeif: Pointer to ISP IPIPEIF device.
266 * @events: IPIPEIF events
267 */
268void omap4iss_ipipeif_isr(struct iss_ipipeif_device *ipipeif, u32 events)
269{
270	if (omap4iss_module_sync_is_stopping(&ipipeif->wait,
271					     &ipipeif->stopping))
272		return;
273
274	if (events & ISP5_IRQ_ISIF_INT(0))
275		ipipeif_isif0_isr(ipipeif);
276}
277
278/* -----------------------------------------------------------------------------
279 * ISP video operations
280 */
281
282static int ipipeif_video_queue(struct iss_video *video,
283			       struct iss_buffer *buffer)
284{
285	struct iss_ipipeif_device *ipipeif = container_of(video,
286				struct iss_ipipeif_device, video_out);
287
288	if (!(ipipeif->output & IPIPEIF_OUTPUT_MEMORY))
289		return -ENODEV;
290
291	ipipeif_set_outaddr(ipipeif, buffer->iss_addr);
292
293	/*
294	 * If streaming was enabled before there was a buffer queued
295	 * or underrun happened in the ISR, the hardware was not enabled
296	 * and DMA queue flag ISS_VIDEO_DMAQUEUE_UNDERRUN is still set.
297	 * Enable it now.
298	 */
299	if (video->dmaqueue_flags & ISS_VIDEO_DMAQUEUE_UNDERRUN) {
300		if (ipipeif->output & IPIPEIF_OUTPUT_MEMORY)
301			ipipeif_write_enable(ipipeif, 1);
302		ipipeif_enable(ipipeif, 1);
303		iss_video_dmaqueue_flags_clr(video);
304	}
305
306	return 0;
307}
308
309static const struct iss_video_operations ipipeif_video_ops = {
310	.queue = ipipeif_video_queue,
311};
312
313/* -----------------------------------------------------------------------------
314 * V4L2 subdev operations
315 */
316
317#define IPIPEIF_DRV_SUBCLK_MASK	(OMAP4_ISS_ISP_SUBCLK_IPIPEIF |\
318				 OMAP4_ISS_ISP_SUBCLK_ISIF)
319/*
320 * ipipeif_set_stream - Enable/Disable streaming on the IPIPEIF module
321 * @sd: ISP IPIPEIF V4L2 subdevice
322 * @enable: Enable/disable stream
323 */
324static int ipipeif_set_stream(struct v4l2_subdev *sd, int enable)
325{
326	struct iss_ipipeif_device *ipipeif = v4l2_get_subdevdata(sd);
327	struct iss_device *iss = to_iss_device(ipipeif);
328	struct iss_video *video_out = &ipipeif->video_out;
329	int ret = 0;
330
331	if (ipipeif->state == ISS_PIPELINE_STREAM_STOPPED) {
332		if (enable == ISS_PIPELINE_STREAM_STOPPED)
333			return 0;
334
335		omap4iss_isp_subclk_enable(iss, IPIPEIF_DRV_SUBCLK_MASK);
336	}
337
338	switch (enable) {
339	case ISS_PIPELINE_STREAM_CONTINUOUS:
340
341		ipipeif_configure(ipipeif);
342		ipipeif_print_status(ipipeif);
343
344		/*
345		 * When outputting to memory with no buffer available, let the
346		 * buffer queue handler start the hardware. A DMA queue flag
347		 * ISS_VIDEO_DMAQUEUE_QUEUED will be set as soon as there is
348		 * a buffer available.
349		 */
350		if (ipipeif->output & IPIPEIF_OUTPUT_MEMORY &&
351		    !(video_out->dmaqueue_flags & ISS_VIDEO_DMAQUEUE_QUEUED))
352			break;
353
354		atomic_set(&ipipeif->stopping, 0);
355		if (ipipeif->output & IPIPEIF_OUTPUT_MEMORY)
356			ipipeif_write_enable(ipipeif, 1);
357		ipipeif_enable(ipipeif, 1);
358		iss_video_dmaqueue_flags_clr(video_out);
359		break;
360
361	case ISS_PIPELINE_STREAM_STOPPED:
362		if (ipipeif->state == ISS_PIPELINE_STREAM_STOPPED)
363			return 0;
364		if (omap4iss_module_sync_idle(&sd->entity, &ipipeif->wait,
365					      &ipipeif->stopping))
366			dev_dbg(iss->dev, "%s: module stop timeout.\n",
367				sd->name);
368
369		if (ipipeif->output & IPIPEIF_OUTPUT_MEMORY)
370			ipipeif_write_enable(ipipeif, 0);
371		ipipeif_enable(ipipeif, 0);
372		omap4iss_isp_disable_interrupts(iss);
373		omap4iss_isp_subclk_disable(iss, IPIPEIF_DRV_SUBCLK_MASK);
374		iss_video_dmaqueue_flags_clr(video_out);
375		break;
376	}
377
378	ipipeif->state = enable;
379	return ret;
380}
381
382static struct v4l2_mbus_framefmt *
383__ipipeif_get_format(struct iss_ipipeif_device *ipipeif,
384		     struct v4l2_subdev_fh *fh, unsigned int pad,
385		     enum v4l2_subdev_format_whence which)
386{
387	if (which == V4L2_SUBDEV_FORMAT_TRY)
388		return v4l2_subdev_get_try_format(fh, pad);
389	else
390		return &ipipeif->formats[pad];
391}
392
393/*
394 * ipipeif_try_format - Try video format on a pad
395 * @ipipeif: ISS IPIPEIF device
396 * @fh : V4L2 subdev file handle
397 * @pad: Pad number
398 * @fmt: Format
399 */
400static void
401ipipeif_try_format(struct iss_ipipeif_device *ipipeif,
402		   struct v4l2_subdev_fh *fh, unsigned int pad,
403		   struct v4l2_mbus_framefmt *fmt,
404		   enum v4l2_subdev_format_whence which)
405{
406	struct v4l2_mbus_framefmt *format;
407	unsigned int width = fmt->width;
408	unsigned int height = fmt->height;
409	unsigned int i;
410
411	switch (pad) {
412	case IPIPEIF_PAD_SINK:
413		/* TODO: If the IPIPEIF output formatter pad is connected
414		 * directly to the resizer, only YUV formats can be used.
415		 */
416		for (i = 0; i < ARRAY_SIZE(ipipeif_fmts); i++) {
417			if (fmt->code == ipipeif_fmts[i])
418				break;
419		}
420
421		/* If not found, use SGRBG10 as default */
422		if (i >= ARRAY_SIZE(ipipeif_fmts))
423			fmt->code = V4L2_MBUS_FMT_SGRBG10_1X10;
424
425		/* Clamp the input size. */
426		fmt->width = clamp_t(u32, width, 1, 8192);
427		fmt->height = clamp_t(u32, height, 1, 8192);
428		break;
429
430	case IPIPEIF_PAD_SOURCE_ISIF_SF:
431		format = __ipipeif_get_format(ipipeif, fh, IPIPEIF_PAD_SINK,
432					      which);
433		memcpy(fmt, format, sizeof(*fmt));
434
435		/* The data formatter truncates the number of horizontal output
436		 * pixels to a multiple of 16. To avoid clipping data, allow
437		 * callers to request an output size bigger than the input size
438		 * up to the nearest multiple of 16.
439		 */
440		fmt->width = clamp_t(u32, width, 32, (fmt->width + 15) & ~15);
441		fmt->width &= ~15;
442		fmt->height = clamp_t(u32, height, 32, fmt->height);
443		break;
444
445	case IPIPEIF_PAD_SOURCE_VP:
446		format = __ipipeif_get_format(ipipeif, fh, IPIPEIF_PAD_SINK,
447					      which);
448		memcpy(fmt, format, sizeof(*fmt));
449
450		fmt->width = clamp_t(u32, width, 32, fmt->width);
451		fmt->height = clamp_t(u32, height, 32, fmt->height);
452		break;
453	}
454
455	/* Data is written to memory unpacked, each 10-bit or 12-bit pixel is
456	 * stored on 2 bytes.
457	 */
458	fmt->colorspace = V4L2_COLORSPACE_SRGB;
459	fmt->field = V4L2_FIELD_NONE;
460}
461
462/*
463 * ipipeif_enum_mbus_code - Handle pixel format enumeration
464 * @sd     : pointer to v4l2 subdev structure
465 * @fh : V4L2 subdev file handle
466 * @code   : pointer to v4l2_subdev_mbus_code_enum structure
467 * return -EINVAL or zero on success
468 */
469static int ipipeif_enum_mbus_code(struct v4l2_subdev *sd,
470			       struct v4l2_subdev_fh *fh,
471			       struct v4l2_subdev_mbus_code_enum *code)
472{
473	struct iss_ipipeif_device *ipipeif = v4l2_get_subdevdata(sd);
474	struct v4l2_mbus_framefmt *format;
475
476	switch (code->pad) {
477	case IPIPEIF_PAD_SINK:
478		if (code->index >= ARRAY_SIZE(ipipeif_fmts))
479			return -EINVAL;
480
481		code->code = ipipeif_fmts[code->index];
482		break;
483
484	case IPIPEIF_PAD_SOURCE_ISIF_SF:
485	case IPIPEIF_PAD_SOURCE_VP:
486		/* No format conversion inside IPIPEIF */
487		if (code->index != 0)
488			return -EINVAL;
489
490		format = __ipipeif_get_format(ipipeif, fh, IPIPEIF_PAD_SINK,
491					      V4L2_SUBDEV_FORMAT_TRY);
492
493		code->code = format->code;
494		break;
495
496	default:
497		return -EINVAL;
498	}
499
500	return 0;
501}
502
503static int ipipeif_enum_frame_size(struct v4l2_subdev *sd,
504				struct v4l2_subdev_fh *fh,
505				struct v4l2_subdev_frame_size_enum *fse)
506{
507	struct iss_ipipeif_device *ipipeif = v4l2_get_subdevdata(sd);
508	struct v4l2_mbus_framefmt format;
509
510	if (fse->index != 0)
511		return -EINVAL;
512
513	format.code = fse->code;
514	format.width = 1;
515	format.height = 1;
516	ipipeif_try_format(ipipeif, fh, fse->pad, &format,
517			   V4L2_SUBDEV_FORMAT_TRY);
518	fse->min_width = format.width;
519	fse->min_height = format.height;
520
521	if (format.code != fse->code)
522		return -EINVAL;
523
524	format.code = fse->code;
525	format.width = -1;
526	format.height = -1;
527	ipipeif_try_format(ipipeif, fh, fse->pad, &format,
528			   V4L2_SUBDEV_FORMAT_TRY);
529	fse->max_width = format.width;
530	fse->max_height = format.height;
531
532	return 0;
533}
534
535/*
536 * ipipeif_get_format - Retrieve the video format on a pad
537 * @sd : ISP IPIPEIF V4L2 subdevice
538 * @fh : V4L2 subdev file handle
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 */
544static int ipipeif_get_format(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
545			   struct v4l2_subdev_format *fmt)
546{
547	struct iss_ipipeif_device *ipipeif = v4l2_get_subdevdata(sd);
548	struct v4l2_mbus_framefmt *format;
549
550	format = __ipipeif_get_format(ipipeif, fh, fmt->pad, fmt->which);
551	if (format == NULL)
552		return -EINVAL;
553
554	fmt->format = *format;
555	return 0;
556}
557
558/*
559 * ipipeif_set_format - Set the video format on a pad
560 * @sd : ISP IPIPEIF V4L2 subdevice
561 * @fh : V4L2 subdev file handle
562 * @fmt: Format
563 *
564 * Return 0 on success or -EINVAL if the pad is invalid or doesn't correspond
565 * to the format type.
566 */
567static int ipipeif_set_format(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
568			   struct v4l2_subdev_format *fmt)
569{
570	struct iss_ipipeif_device *ipipeif = v4l2_get_subdevdata(sd);
571	struct v4l2_mbus_framefmt *format;
572
573	format = __ipipeif_get_format(ipipeif, fh, fmt->pad, fmt->which);
574	if (format == NULL)
575		return -EINVAL;
576
577	ipipeif_try_format(ipipeif, fh, fmt->pad, &fmt->format, fmt->which);
578	*format = fmt->format;
579
580	/* Propagate the format from sink to source */
581	if (fmt->pad == IPIPEIF_PAD_SINK) {
582		format = __ipipeif_get_format(ipipeif, fh,
583					      IPIPEIF_PAD_SOURCE_ISIF_SF,
584					      fmt->which);
585		*format = fmt->format;
586		ipipeif_try_format(ipipeif, fh, IPIPEIF_PAD_SOURCE_ISIF_SF,
587				   format, fmt->which);
588
589		format = __ipipeif_get_format(ipipeif, fh,
590					      IPIPEIF_PAD_SOURCE_VP,
591					      fmt->which);
592		*format = fmt->format;
593		ipipeif_try_format(ipipeif, fh, IPIPEIF_PAD_SOURCE_VP, format,
594				fmt->which);
595	}
596
597	return 0;
598}
599
600static int ipipeif_link_validate(struct v4l2_subdev *sd,
601				 struct media_link *link,
602				 struct v4l2_subdev_format *source_fmt,
603				 struct v4l2_subdev_format *sink_fmt)
604{
605	/* Check if the two ends match */
606	if (source_fmt->format.width != sink_fmt->format.width ||
607	    source_fmt->format.height != sink_fmt->format.height)
608		return -EPIPE;
609
610	if (source_fmt->format.code != sink_fmt->format.code)
611		return -EPIPE;
612
613	return 0;
614}
615
616/*
617 * ipipeif_init_formats - Initialize formats on all pads
618 * @sd: ISP IPIPEIF V4L2 subdevice
619 * @fh: V4L2 subdev file handle
620 *
621 * Initialize all pad formats with default values. If fh is not NULL, try
622 * formats are initialized on the file handle. Otherwise active formats are
623 * initialized on the device.
624 */
625static int ipipeif_init_formats(struct v4l2_subdev *sd,
626				struct v4l2_subdev_fh *fh)
627{
628	struct v4l2_subdev_format format;
629
630	memset(&format, 0, sizeof(format));
631	format.pad = IPIPEIF_PAD_SINK;
632	format.which = fh ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
633	format.format.code = V4L2_MBUS_FMT_SGRBG10_1X10;
634	format.format.width = 4096;
635	format.format.height = 4096;
636	ipipeif_set_format(sd, fh, &format);
637
638	return 0;
639}
640
641/* V4L2 subdev video operations */
642static const struct v4l2_subdev_video_ops ipipeif_v4l2_video_ops = {
643	.s_stream = ipipeif_set_stream,
644};
645
646/* V4L2 subdev pad operations */
647static const struct v4l2_subdev_pad_ops ipipeif_v4l2_pad_ops = {
648	.enum_mbus_code = ipipeif_enum_mbus_code,
649	.enum_frame_size = ipipeif_enum_frame_size,
650	.get_fmt = ipipeif_get_format,
651	.set_fmt = ipipeif_set_format,
652	.link_validate = ipipeif_link_validate,
653};
654
655/* V4L2 subdev operations */
656static const struct v4l2_subdev_ops ipipeif_v4l2_ops = {
657	.video = &ipipeif_v4l2_video_ops,
658	.pad = &ipipeif_v4l2_pad_ops,
659};
660
661/* V4L2 subdev internal operations */
662static const struct v4l2_subdev_internal_ops ipipeif_v4l2_internal_ops = {
663	.open = ipipeif_init_formats,
664};
665
666/* -----------------------------------------------------------------------------
667 * Media entity operations
668 */
669
670/*
671 * ipipeif_link_setup - Setup IPIPEIF connections
672 * @entity: IPIPEIF media entity
673 * @local: Pad at the local end of the link
674 * @remote: Pad at the remote end of the link
675 * @flags: Link flags
676 *
677 * return -EINVAL or zero on success
678 */
679static int ipipeif_link_setup(struct media_entity *entity,
680			   const struct media_pad *local,
681			   const struct media_pad *remote, u32 flags)
682{
683	struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
684	struct iss_ipipeif_device *ipipeif = v4l2_get_subdevdata(sd);
685	struct iss_device *iss = to_iss_device(ipipeif);
686
687	switch (local->index | media_entity_type(remote->entity)) {
688	case IPIPEIF_PAD_SINK | MEDIA_ENT_T_V4L2_SUBDEV:
689		/* Read from the sensor CSI2a or CSI2b. */
690		if (!(flags & MEDIA_LNK_FL_ENABLED)) {
691			ipipeif->input = IPIPEIF_INPUT_NONE;
692			break;
693		}
694
695		if (ipipeif->input != IPIPEIF_INPUT_NONE)
696			return -EBUSY;
697
698		if (remote->entity == &iss->csi2a.subdev.entity)
699			ipipeif->input = IPIPEIF_INPUT_CSI2A;
700		else if (remote->entity == &iss->csi2b.subdev.entity)
701			ipipeif->input = IPIPEIF_INPUT_CSI2B;
702
703		break;
704
705	case IPIPEIF_PAD_SOURCE_ISIF_SF | MEDIA_ENT_T_DEVNODE:
706		/* Write to memory */
707		if (flags & MEDIA_LNK_FL_ENABLED) {
708			if (ipipeif->output & ~IPIPEIF_OUTPUT_MEMORY)
709				return -EBUSY;
710			ipipeif->output |= IPIPEIF_OUTPUT_MEMORY;
711		} else {
712			ipipeif->output &= ~IPIPEIF_OUTPUT_MEMORY;
713		}
714		break;
715
716	case IPIPEIF_PAD_SOURCE_VP | MEDIA_ENT_T_V4L2_SUBDEV:
717		/* Send to IPIPE/RESIZER */
718		if (flags & MEDIA_LNK_FL_ENABLED) {
719			if (ipipeif->output & ~IPIPEIF_OUTPUT_VP)
720				return -EBUSY;
721			ipipeif->output |= IPIPEIF_OUTPUT_VP;
722		} else {
723			ipipeif->output &= ~IPIPEIF_OUTPUT_VP;
724		}
725		break;
726
727	default:
728		return -EINVAL;
729	}
730
731	return 0;
732}
733
734/* media operations */
735static const struct media_entity_operations ipipeif_media_ops = {
736	.link_setup = ipipeif_link_setup,
737	.link_validate = v4l2_subdev_link_validate,
738};
739
740/*
741 * ipipeif_init_entities - Initialize V4L2 subdev and media entity
742 * @ipipeif: ISS ISP IPIPEIF module
743 *
744 * Return 0 on success and a negative error code on failure.
745 */
746static int ipipeif_init_entities(struct iss_ipipeif_device *ipipeif)
747{
748	struct v4l2_subdev *sd = &ipipeif->subdev;
749	struct media_pad *pads = ipipeif->pads;
750	struct media_entity *me = &sd->entity;
751	int ret;
752
753	ipipeif->input = IPIPEIF_INPUT_NONE;
754
755	v4l2_subdev_init(sd, &ipipeif_v4l2_ops);
756	sd->internal_ops = &ipipeif_v4l2_internal_ops;
757	strlcpy(sd->name, "OMAP4 ISS ISP IPIPEIF", sizeof(sd->name));
758	sd->grp_id = 1 << 16;	/* group ID for iss subdevs */
759	v4l2_set_subdevdata(sd, ipipeif);
760	sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
761
762	pads[IPIPEIF_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
763	pads[IPIPEIF_PAD_SOURCE_ISIF_SF].flags = MEDIA_PAD_FL_SOURCE;
764	pads[IPIPEIF_PAD_SOURCE_VP].flags = MEDIA_PAD_FL_SOURCE;
765
766	me->ops = &ipipeif_media_ops;
767	ret = media_entity_init(me, IPIPEIF_PADS_NUM, pads, 0);
768	if (ret < 0)
769		return ret;
770
771	ipipeif_init_formats(sd, NULL);
772
773	ipipeif->video_out.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
774	ipipeif->video_out.ops = &ipipeif_video_ops;
775	ipipeif->video_out.iss = to_iss_device(ipipeif);
776	ipipeif->video_out.capture_mem = PAGE_ALIGN(4096 * 4096) * 3;
777	ipipeif->video_out.bpl_alignment = 32;
778	ipipeif->video_out.bpl_zero_padding = 1;
779	ipipeif->video_out.bpl_max = 0x1ffe0;
780
781	ret = omap4iss_video_init(&ipipeif->video_out, "ISP IPIPEIF");
782	if (ret < 0)
783		return ret;
784
785	/* Connect the IPIPEIF subdev to the video node. */
786	ret = media_entity_create_link(&ipipeif->subdev.entity,
787				       IPIPEIF_PAD_SOURCE_ISIF_SF,
788				       &ipipeif->video_out.video.entity, 0, 0);
789	if (ret < 0)
790		return ret;
791
792	return 0;
793}
794
795void omap4iss_ipipeif_unregister_entities(struct iss_ipipeif_device *ipipeif)
796{
797	media_entity_cleanup(&ipipeif->subdev.entity);
798
799	v4l2_device_unregister_subdev(&ipipeif->subdev);
800	omap4iss_video_unregister(&ipipeif->video_out);
801}
802
803int omap4iss_ipipeif_register_entities(struct iss_ipipeif_device *ipipeif,
804	struct v4l2_device *vdev)
805{
806	int ret;
807
808	/* Register the subdev and video node. */
809	ret = v4l2_device_register_subdev(vdev, &ipipeif->subdev);
810	if (ret < 0)
811		goto error;
812
813	ret = omap4iss_video_register(&ipipeif->video_out, vdev);
814	if (ret < 0)
815		goto error;
816
817	return 0;
818
819error:
820	omap4iss_ipipeif_unregister_entities(ipipeif);
821	return ret;
822}
823
824/* -----------------------------------------------------------------------------
825 * ISP IPIPEIF initialisation and cleanup
826 */
827
828/*
829 * omap4iss_ipipeif_init - IPIPEIF module initialization.
830 * @iss: Device pointer specific to the OMAP4 ISS.
831 *
832 * TODO: Get the initialisation values from platform data.
833 *
834 * Return 0 on success or a negative error code otherwise.
835 */
836int omap4iss_ipipeif_init(struct iss_device *iss)
837{
838	struct iss_ipipeif_device *ipipeif = &iss->ipipeif;
839
840	ipipeif->state = ISS_PIPELINE_STREAM_STOPPED;
841	init_waitqueue_head(&ipipeif->wait);
842
843	return ipipeif_init_entities(ipipeif);
844}
845
846/*
847 * omap4iss_ipipeif_cleanup - IPIPEIF module cleanup.
848 * @iss: Device pointer specific to the OMAP4 ISS.
849 */
850void omap4iss_ipipeif_cleanup(struct iss_device *iss)
851{
852	/* FIXME: are you sure there's nothing to do? */
853}
854