iss_ipipeif.c revision 714148260d0585aedf72bd4d6d0a909886b0e9e1
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 struct v4l2_mbus_framefmt *
27__ipipeif_get_format(struct iss_ipipeif_device *ipipeif, struct v4l2_subdev_fh *fh,
28		  unsigned int pad, enum v4l2_subdev_format_whence which);
29
30static const unsigned int ipipeif_fmts[] = {
31	V4L2_MBUS_FMT_SGRBG10_1X10,
32	V4L2_MBUS_FMT_SRGGB10_1X10,
33	V4L2_MBUS_FMT_SBGGR10_1X10,
34	V4L2_MBUS_FMT_SGBRG10_1X10,
35	V4L2_MBUS_FMT_UYVY8_1X16,
36	V4L2_MBUS_FMT_YUYV8_1X16,
37};
38
39/*
40 * ipipeif_print_status - Print current IPIPEIF Module register values.
41 * @ipipeif: Pointer to ISS ISP IPIPEIF device.
42 *
43 * Also prints other debug information stored in the IPIPEIF module.
44 */
45#define IPIPEIF_PRINT_REGISTER(iss, name)\
46	dev_dbg(iss->dev, "###IPIPEIF " #name "=0x%08x\n", \
47		readl(iss->regs[OMAP4_ISS_MEM_ISP_IPIPEIF] + IPIPEIF_##name))
48
49#define ISIF_PRINT_REGISTER(iss, name)\
50	dev_dbg(iss->dev, "###ISIF " #name "=0x%08x\n", \
51		readl(iss->regs[OMAP4_ISS_MEM_ISP_ISIF] + ISIF_##name))
52
53#define ISP5_PRINT_REGISTER(iss, name)\
54	dev_dbg(iss->dev, "###ISP5 " #name "=0x%08x\n", \
55		readl(iss->regs[OMAP4_ISS_MEM_ISP_SYS1] + ISP5_##name))
56
57static void ipipeif_print_status(struct iss_ipipeif_device *ipipeif)
58{
59	struct iss_device *iss = to_iss_device(ipipeif);
60
61	dev_dbg(iss->dev, "-------------IPIPEIF Register dump-------------\n");
62
63	IPIPEIF_PRINT_REGISTER(iss, CFG1);
64	IPIPEIF_PRINT_REGISTER(iss, CFG2);
65
66	ISIF_PRINT_REGISTER(iss, SYNCEN);
67	ISIF_PRINT_REGISTER(iss, CADU);
68	ISIF_PRINT_REGISTER(iss, CADL);
69	ISIF_PRINT_REGISTER(iss, MODESET);
70	ISIF_PRINT_REGISTER(iss, CCOLP);
71	ISIF_PRINT_REGISTER(iss, SPH);
72	ISIF_PRINT_REGISTER(iss, LNH);
73	ISIF_PRINT_REGISTER(iss, LNV);
74	ISIF_PRINT_REGISTER(iss, VDINT0);
75	ISIF_PRINT_REGISTER(iss, HSIZE);
76
77	ISP5_PRINT_REGISTER(iss, SYSCONFIG);
78	ISP5_PRINT_REGISTER(iss, CTRL);
79	ISP5_PRINT_REGISTER(iss, IRQSTATUS(0));
80	ISP5_PRINT_REGISTER(iss, IRQENABLE_SET(0));
81	ISP5_PRINT_REGISTER(iss, IRQENABLE_CLR(0));
82
83	dev_dbg(iss->dev, "-----------------------------------------------\n");
84}
85
86static void ipipeif_write_enable(struct iss_ipipeif_device *ipipeif, u8 enable)
87{
88	struct iss_device *iss = to_iss_device(ipipeif);
89
90	writel((readl(iss->regs[OMAP4_ISS_MEM_ISP_ISIF] + ISIF_SYNCEN) &
91		~ISIF_SYNCEN_DWEN) |
92		enable ? ISIF_SYNCEN_DWEN : 0,
93		iss->regs[OMAP4_ISS_MEM_ISP_ISIF] + ISIF_SYNCEN);
94}
95
96/*
97 * ipipeif_enable - Enable/Disable IPIPEIF.
98 * @enable: enable flag
99 *
100 */
101static void ipipeif_enable(struct iss_ipipeif_device *ipipeif, u8 enable)
102{
103	struct iss_device *iss = to_iss_device(ipipeif);
104
105	writel((readl(iss->regs[OMAP4_ISS_MEM_ISP_ISIF] + ISIF_SYNCEN) &
106		~ISIF_SYNCEN_SYEN) |
107		enable ? ISIF_SYNCEN_SYEN : 0,
108		iss->regs[OMAP4_ISS_MEM_ISP_ISIF] + ISIF_SYNCEN);
109}
110
111/* -----------------------------------------------------------------------------
112 * Format- and pipeline-related configuration helpers
113 */
114
115/*
116 * ipipeif_set_outaddr - Set memory address to save output image
117 * @ipipeif: Pointer to ISP IPIPEIF device.
118 * @addr: 32-bit memory address aligned on 32 byte boundary.
119 *
120 * Sets the memory address where the output will be saved.
121 */
122static void ipipeif_set_outaddr(struct iss_ipipeif_device *ipipeif, u32 addr)
123{
124	struct iss_device *iss = to_iss_device(ipipeif);
125
126	/* Save address splitted in Base Address H & L */
127	writel((addr >> (16 + 5)) & ISIF_CADU_MASK,
128		iss->regs[OMAP4_ISS_MEM_ISP_ISIF] + ISIF_CADU);
129	writel((addr >> 5) & ISIF_CADL_MASK,
130		iss->regs[OMAP4_ISS_MEM_ISP_ISIF] + ISIF_CADL);
131}
132
133static void ipipeif_configure(struct iss_ipipeif_device *ipipeif)
134{
135	struct iss_device *iss = to_iss_device(ipipeif);
136	struct v4l2_mbus_framefmt *format;
137	u32 isif_ccolp = 0;
138
139	omap4iss_configure_bridge(iss, ipipeif->input);
140
141	/* IPIPEIF_PAD_SINK */
142	format = &ipipeif->formats[IPIPEIF_PAD_SINK];
143
144	/* IPIPEIF with YUV422 input from ISIF */
145	writel(readl(iss->regs[OMAP4_ISS_MEM_ISP_IPIPEIF] + IPIPEIF_CFG1) &
146		~(IPIPEIF_CFG1_INPSRC1_MASK | IPIPEIF_CFG1_INPSRC2_MASK),
147		iss->regs[OMAP4_ISS_MEM_ISP_IPIPEIF] + IPIPEIF_CFG1);
148
149	/* Select ISIF/IPIPEIF input format */
150	switch (format->code) {
151	case V4L2_MBUS_FMT_UYVY8_1X16:
152	case V4L2_MBUS_FMT_YUYV8_1X16:
153		writel((readl(iss->regs[OMAP4_ISS_MEM_ISP_ISIF] + ISIF_MODESET) &
154			~(ISIF_MODESET_CCDMD |
155			  ISIF_MODESET_INPMOD_MASK |
156			  ISIF_MODESET_CCDW_MASK)) |
157			ISIF_MODESET_INPMOD_YCBCR16,
158			iss->regs[OMAP4_ISS_MEM_ISP_ISIF] + ISIF_MODESET);
159
160		writel((readl(iss->regs[OMAP4_ISS_MEM_ISP_IPIPEIF] + IPIPEIF_CFG2) &
161			~IPIPEIF_CFG2_YUV8) |
162			IPIPEIF_CFG2_YUV16,
163			iss->regs[OMAP4_ISS_MEM_ISP_IPIPEIF] + IPIPEIF_CFG2);
164
165		break;
166	case V4L2_MBUS_FMT_SGRBG10_1X10:
167		isif_ccolp = ISIF_CCOLP_CP0_F0_GR |
168			ISIF_CCOLP_CP1_F0_R |
169			ISIF_CCOLP_CP2_F0_B |
170			ISIF_CCOLP_CP3_F0_GB;
171		goto cont_raw;
172	case V4L2_MBUS_FMT_SRGGB10_1X10:
173		isif_ccolp = ISIF_CCOLP_CP0_F0_R |
174			ISIF_CCOLP_CP1_F0_GR |
175			ISIF_CCOLP_CP2_F0_GB |
176			ISIF_CCOLP_CP3_F0_B;
177		goto cont_raw;
178	case V4L2_MBUS_FMT_SBGGR10_1X10:
179		isif_ccolp = ISIF_CCOLP_CP0_F0_B |
180			ISIF_CCOLP_CP1_F0_GB |
181			ISIF_CCOLP_CP2_F0_GR |
182			ISIF_CCOLP_CP3_F0_R;
183		goto cont_raw;
184	case V4L2_MBUS_FMT_SGBRG10_1X10:
185		isif_ccolp = ISIF_CCOLP_CP0_F0_GB |
186			ISIF_CCOLP_CP1_F0_B |
187			ISIF_CCOLP_CP2_F0_R |
188			ISIF_CCOLP_CP3_F0_GR;
189cont_raw:
190		writel((readl(iss->regs[OMAP4_ISS_MEM_ISP_IPIPEIF] + IPIPEIF_CFG2) &
191			~IPIPEIF_CFG2_YUV16),
192			iss->regs[OMAP4_ISS_MEM_ISP_IPIPEIF] + IPIPEIF_CFG2);
193
194		writel((readl(iss->regs[OMAP4_ISS_MEM_ISP_ISIF] + ISIF_MODESET) &
195			~(ISIF_MODESET_CCDMD |
196			  ISIF_MODESET_INPMOD_MASK |
197			  ISIF_MODESET_CCDW_MASK)) |
198			ISIF_MODESET_INPMOD_RAW | ISIF_MODESET_CCDW_2BIT,
199			iss->regs[OMAP4_ISS_MEM_ISP_ISIF] + ISIF_MODESET);
200
201		writel((readl(iss->regs[OMAP4_ISS_MEM_ISP_ISIF] + ISIF_CGAMMAWD) &
202			~ISIF_CGAMMAWD_GWDI_MASK) |
203			ISIF_CGAMMAWD_GWDI_BIT11,
204			iss->regs[OMAP4_ISS_MEM_ISP_ISIF] + ISIF_CGAMMAWD);
205
206		/* Set RAW Bayer pattern */
207		writel(isif_ccolp,
208			iss->regs[OMAP4_ISS_MEM_ISP_ISIF] + ISIF_CCOLP);
209		break;
210	}
211
212	writel(0 & ISIF_SPH_MASK, iss->regs[OMAP4_ISS_MEM_ISP_ISIF] + ISIF_SPH);
213	writel((format->width - 1) & ISIF_LNH_MASK,
214		iss->regs[OMAP4_ISS_MEM_ISP_ISIF] + ISIF_LNH);
215	writel((format->height - 1) & ISIF_LNV_MASK,
216		iss->regs[OMAP4_ISS_MEM_ISP_ISIF] + ISIF_LNV);
217
218	/* Generate ISIF0 on the last line of the image */
219	writel(format->height - 1,
220		iss->regs[OMAP4_ISS_MEM_ISP_ISIF] + ISIF_VDINT0);
221
222	/* IPIPEIF_PAD_SOURCE_ISIF_SF */
223	format = &ipipeif->formats[IPIPEIF_PAD_SOURCE_ISIF_SF];
224
225	writel((ipipeif->video_out.bpl_value >> 5) & ISIF_HSIZE_HSIZE_MASK,
226		iss->regs[OMAP4_ISS_MEM_ISP_ISIF] + ISIF_HSIZE);
227
228	/* IPIPEIF_PAD_SOURCE_VP */
229	/* Do nothing? */
230
231	omap4iss_isp_enable_interrupts(iss);
232}
233
234/* -----------------------------------------------------------------------------
235 * Interrupt handling
236 */
237
238static void ipipeif_isr_buffer(struct iss_ipipeif_device *ipipeif)
239{
240	struct iss_buffer *buffer;
241
242	ipipeif_write_enable(ipipeif, 0);
243
244	buffer = omap4iss_video_buffer_next(&ipipeif->video_out);
245	if (buffer == NULL)
246		return;
247
248	ipipeif_set_outaddr(ipipeif, buffer->iss_addr);
249
250	ipipeif_write_enable(ipipeif, 1);
251}
252
253/*
254 * ipipeif_isif0_isr - Handle ISIF0 event
255 * @ipipeif: Pointer to ISP IPIPEIF device.
256 *
257 * Executes LSC deferred enablement before next frame starts.
258 */
259static void ipipeif_isif0_isr(struct iss_ipipeif_device *ipipeif)
260{
261	struct iss_pipeline *pipe =
262			     to_iss_pipeline(&ipipeif->subdev.entity);
263	if (pipe->do_propagation)
264		atomic_inc(&pipe->frame_number);
265
266	if (ipipeif->output & IPIPEIF_OUTPUT_MEMORY)
267		ipipeif_isr_buffer(ipipeif);
268}
269
270/*
271 * omap4iss_ipipeif_isr - Configure ipipeif during interframe time.
272 * @ipipeif: Pointer to ISP IPIPEIF device.
273 * @events: IPIPEIF events
274 */
275void omap4iss_ipipeif_isr(struct iss_ipipeif_device *ipipeif, u32 events)
276{
277	if (omap4iss_module_sync_is_stopping(&ipipeif->wait, &ipipeif->stopping))
278		return;
279
280	if (events & ISP5_IRQ_ISIF0)
281		ipipeif_isif0_isr(ipipeif);
282}
283
284/* -----------------------------------------------------------------------------
285 * ISP video operations
286 */
287
288static int ipipeif_video_queue(struct iss_video *video, struct iss_buffer *buffer)
289{
290	struct iss_ipipeif_device *ipipeif = container_of(video,
291				struct iss_ipipeif_device, video_out);
292
293	if (!(ipipeif->output & IPIPEIF_OUTPUT_MEMORY))
294		return -ENODEV;
295
296	ipipeif_set_outaddr(ipipeif, buffer->iss_addr);
297
298	/*
299	 * If streaming was enabled before there was a buffer queued
300	 * or underrun happened in the ISR, the hardware was not enabled
301	 * and DMA queue flag ISS_VIDEO_DMAQUEUE_UNDERRUN is still set.
302	 * Enable it now.
303	 */
304	if (video->dmaqueue_flags & ISS_VIDEO_DMAQUEUE_UNDERRUN) {
305		if (ipipeif->output & IPIPEIF_OUTPUT_MEMORY)
306			ipipeif_write_enable(ipipeif, 1);
307		ipipeif_enable(ipipeif, 1);
308		iss_video_dmaqueue_flags_clr(video);
309	}
310
311	return 0;
312}
313
314static const struct iss_video_operations ipipeif_video_ops = {
315	.queue = ipipeif_video_queue,
316};
317
318/* -----------------------------------------------------------------------------
319 * V4L2 subdev operations
320 */
321
322#define IPIPEIF_DRV_SUBCLK_MASK	(OMAP4_ISS_ISP_SUBCLK_IPIPEIF |\
323				 OMAP4_ISS_ISP_SUBCLK_ISIF)
324/*
325 * ipipeif_set_stream - Enable/Disable streaming on the IPIPEIF module
326 * @sd: ISP IPIPEIF V4L2 subdevice
327 * @enable: Enable/disable stream
328 */
329static int ipipeif_set_stream(struct v4l2_subdev *sd, int enable)
330{
331	struct iss_ipipeif_device *ipipeif = v4l2_get_subdevdata(sd);
332	struct iss_device *iss = to_iss_device(ipipeif);
333	struct iss_video *video_out = &ipipeif->video_out;
334	int ret = 0;
335
336	if (ipipeif->state == ISS_PIPELINE_STREAM_STOPPED) {
337		if (enable == ISS_PIPELINE_STREAM_STOPPED)
338			return 0;
339
340		omap4iss_isp_subclk_enable(iss, IPIPEIF_DRV_SUBCLK_MASK);
341	}
342
343	switch (enable) {
344	case ISS_PIPELINE_STREAM_CONTINUOUS:
345
346		ipipeif_configure(ipipeif);
347		ipipeif_print_status(ipipeif);
348
349		/*
350		 * When outputting to memory with no buffer available, let the
351		 * buffer queue handler start the hardware. A DMA queue flag
352		 * ISS_VIDEO_DMAQUEUE_QUEUED will be set as soon as there is
353		 * a buffer available.
354		 */
355		if (ipipeif->output & IPIPEIF_OUTPUT_MEMORY &&
356		    !(video_out->dmaqueue_flags & ISS_VIDEO_DMAQUEUE_QUEUED))
357			break;
358
359		atomic_set(&ipipeif->stopping, 0);
360		if (ipipeif->output & IPIPEIF_OUTPUT_MEMORY)
361			ipipeif_write_enable(ipipeif, 1);
362		ipipeif_enable(ipipeif, 1);
363		iss_video_dmaqueue_flags_clr(video_out);
364		break;
365
366	case ISS_PIPELINE_STREAM_STOPPED:
367		if (ipipeif->state == ISS_PIPELINE_STREAM_STOPPED)
368			return 0;
369		if (omap4iss_module_sync_idle(&sd->entity, &ipipeif->wait,
370					      &ipipeif->stopping))
371			dev_dbg(iss->dev, "%s: module stop timeout.\n",
372				sd->name);
373
374		if (ipipeif->output & IPIPEIF_OUTPUT_MEMORY)
375			ipipeif_write_enable(ipipeif, 0);
376		ipipeif_enable(ipipeif, 0);
377		omap4iss_isp_disable_interrupts(iss);
378		omap4iss_isp_subclk_disable(iss, IPIPEIF_DRV_SUBCLK_MASK);
379		iss_video_dmaqueue_flags_clr(video_out);
380		break;
381	}
382
383	ipipeif->state = enable;
384	return ret;
385}
386
387static struct v4l2_mbus_framefmt *
388__ipipeif_get_format(struct iss_ipipeif_device *ipipeif, struct v4l2_subdev_fh *fh,
389		  unsigned int pad, enum v4l2_subdev_format_whence which)
390{
391	if (which == V4L2_SUBDEV_FORMAT_TRY)
392		return v4l2_subdev_get_try_format(fh, pad);
393	else
394		return &ipipeif->formats[pad];
395}
396
397/*
398 * ipipeif_try_format - Try video format on a pad
399 * @ipipeif: ISS IPIPEIF device
400 * @fh : V4L2 subdev file handle
401 * @pad: Pad number
402 * @fmt: Format
403 */
404static void
405ipipeif_try_format(struct iss_ipipeif_device *ipipeif, struct v4l2_subdev_fh *fh,
406		unsigned int pad, struct v4l2_mbus_framefmt *fmt,
407		enum v4l2_subdev_format_whence which)
408{
409	struct v4l2_mbus_framefmt *format;
410	unsigned int width = fmt->width;
411	unsigned int height = fmt->height;
412	unsigned int i;
413
414	switch (pad) {
415	case IPIPEIF_PAD_SINK:
416		/* TODO: If the IPIPEIF output formatter pad is connected directly
417		 * to the resizer, only YUV formats can be used.
418		 */
419		for (i = 0; i < ARRAY_SIZE(ipipeif_fmts); i++) {
420			if (fmt->code == ipipeif_fmts[i])
421				break;
422		}
423
424		/* If not found, use SGRBG10 as default */
425		if (i >= ARRAY_SIZE(ipipeif_fmts))
426			fmt->code = V4L2_MBUS_FMT_SGRBG10_1X10;
427
428		/* Clamp the input size. */
429		fmt->width = clamp_t(u32, width, 1, 8192);
430		fmt->height = clamp_t(u32, height, 1, 8192);
431		break;
432
433	case IPIPEIF_PAD_SOURCE_ISIF_SF:
434		format = __ipipeif_get_format(ipipeif, fh, IPIPEIF_PAD_SINK, which);
435		memcpy(fmt, format, sizeof(*fmt));
436
437		/* The data formatter truncates the number of horizontal output
438		 * pixels to a multiple of 16. To avoid clipping data, allow
439		 * callers to request an output size bigger than the input size
440		 * up to the nearest multiple of 16.
441		 */
442		fmt->width = clamp_t(u32, width, 32, (fmt->width + 15) & ~15);
443		fmt->width &= ~15;
444		fmt->height = clamp_t(u32, height, 32, fmt->height);
445		break;
446
447	case IPIPEIF_PAD_SOURCE_VP:
448		format = __ipipeif_get_format(ipipeif, fh, IPIPEIF_PAD_SINK, which);
449		memcpy(fmt, format, sizeof(*fmt));
450
451		fmt->width = clamp_t(u32, width, 32, fmt->width);
452		fmt->height = clamp_t(u32, height, 32, fmt->height);
453		break;
454	}
455
456	/* Data is written to memory unpacked, each 10-bit or 12-bit pixel is
457	 * stored on 2 bytes.
458	 */
459	fmt->colorspace = V4L2_COLORSPACE_SRGB;
460	fmt->field = V4L2_FIELD_NONE;
461}
462
463/*
464 * ipipeif_enum_mbus_code - Handle pixel format enumeration
465 * @sd     : pointer to v4l2 subdev structure
466 * @fh : V4L2 subdev file handle
467 * @code   : pointer to v4l2_subdev_mbus_code_enum structure
468 * return -EINVAL or zero on success
469 */
470static int ipipeif_enum_mbus_code(struct v4l2_subdev *sd,
471			       struct v4l2_subdev_fh *fh,
472			       struct v4l2_subdev_mbus_code_enum *code)
473{
474	struct iss_ipipeif_device *ipipeif = v4l2_get_subdevdata(sd);
475	struct v4l2_mbus_framefmt *format;
476
477	switch (code->pad) {
478	case IPIPEIF_PAD_SINK:
479		if (code->index >= ARRAY_SIZE(ipipeif_fmts))
480			return -EINVAL;
481
482		code->code = ipipeif_fmts[code->index];
483		break;
484
485	case IPIPEIF_PAD_SOURCE_ISIF_SF:
486	case IPIPEIF_PAD_SOURCE_VP:
487		/* No format conversion inside IPIPEIF */
488		if (code->index != 0)
489			return -EINVAL;
490
491		format = __ipipeif_get_format(ipipeif, fh, IPIPEIF_PAD_SINK,
492					      V4L2_SUBDEV_FORMAT_TRY);
493
494		code->code = format->code;
495		break;
496
497	default:
498		return -EINVAL;
499	}
500
501	return 0;
502}
503
504static int ipipeif_enum_frame_size(struct v4l2_subdev *sd,
505				struct v4l2_subdev_fh *fh,
506				struct v4l2_subdev_frame_size_enum *fse)
507{
508	struct iss_ipipeif_device *ipipeif = v4l2_get_subdevdata(sd);
509	struct v4l2_mbus_framefmt format;
510
511	if (fse->index != 0)
512		return -EINVAL;
513
514	format.code = fse->code;
515	format.width = 1;
516	format.height = 1;
517	ipipeif_try_format(ipipeif, fh, fse->pad, &format, 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, V4L2_SUBDEV_FORMAT_TRY);
528	fse->max_width = format.width;
529	fse->max_height = format.height;
530
531	return 0;
532}
533
534/*
535 * ipipeif_get_format - Retrieve the video format on a pad
536 * @sd : ISP IPIPEIF V4L2 subdevice
537 * @fh : V4L2 subdev file handle
538 * @fmt: Format
539 *
540 * Return 0 on success or -EINVAL if the pad is invalid or doesn't correspond
541 * to the format type.
542 */
543static int ipipeif_get_format(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
544			   struct v4l2_subdev_format *fmt)
545{
546	struct iss_ipipeif_device *ipipeif = v4l2_get_subdevdata(sd);
547	struct v4l2_mbus_framefmt *format;
548
549	format = __ipipeif_get_format(ipipeif, fh, fmt->pad, fmt->which);
550	if (format == NULL)
551		return -EINVAL;
552
553	fmt->format = *format;
554	return 0;
555}
556
557/*
558 * ipipeif_set_format - Set the video format on a pad
559 * @sd : ISP IPIPEIF V4L2 subdevice
560 * @fh : V4L2 subdev file handle
561 * @fmt: Format
562 *
563 * Return 0 on success or -EINVAL if the pad is invalid or doesn't correspond
564 * to the format type.
565 */
566static int ipipeif_set_format(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
567			   struct v4l2_subdev_format *fmt)
568{
569	struct iss_ipipeif_device *ipipeif = v4l2_get_subdevdata(sd);
570	struct v4l2_mbus_framefmt *format;
571
572	format = __ipipeif_get_format(ipipeif, fh, fmt->pad, fmt->which);
573	if (format == NULL)
574		return -EINVAL;
575
576	ipipeif_try_format(ipipeif, fh, fmt->pad, &fmt->format, fmt->which);
577	*format = fmt->format;
578
579	/* Propagate the format from sink to source */
580	if (fmt->pad == IPIPEIF_PAD_SINK) {
581		format = __ipipeif_get_format(ipipeif, fh, IPIPEIF_PAD_SOURCE_ISIF_SF,
582					   fmt->which);
583		*format = fmt->format;
584		ipipeif_try_format(ipipeif, fh, IPIPEIF_PAD_SOURCE_ISIF_SF, format,
585				fmt->which);
586
587		format = __ipipeif_get_format(ipipeif, fh, IPIPEIF_PAD_SOURCE_VP,
588					   fmt->which);
589		*format = fmt->format;
590		ipipeif_try_format(ipipeif, fh, IPIPEIF_PAD_SOURCE_VP, format,
591				fmt->which);
592	}
593
594	return 0;
595}
596
597static int ipipeif_link_validate(struct v4l2_subdev *sd, struct media_link *link,
598				 struct v4l2_subdev_format *source_fmt,
599				 struct v4l2_subdev_format *sink_fmt)
600{
601	/* Check if the two ends match */
602	if (source_fmt->format.width != sink_fmt->format.width ||
603	    source_fmt->format.height != sink_fmt->format.height)
604		return -EPIPE;
605
606	if (source_fmt->format.code != sink_fmt->format.code)
607		return -EPIPE;
608
609	return 0;
610}
611
612/*
613 * ipipeif_init_formats - Initialize formats on all pads
614 * @sd: ISP IPIPEIF V4L2 subdevice
615 * @fh: V4L2 subdev file handle
616 *
617 * Initialize all pad formats with default values. If fh is not NULL, try
618 * formats are initialized on the file handle. Otherwise active formats are
619 * initialized on the device.
620 */
621static int ipipeif_init_formats(struct v4l2_subdev *sd, 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, IPIPEIF_PAD_SOURCE_ISIF_SF,
782			&ipipeif->video_out.video.entity, 0, 0);
783	if (ret < 0)
784		return ret;
785
786	return 0;
787}
788
789void omap4iss_ipipeif_unregister_entities(struct iss_ipipeif_device *ipipeif)
790{
791	media_entity_cleanup(&ipipeif->subdev.entity);
792
793	v4l2_device_unregister_subdev(&ipipeif->subdev);
794	omap4iss_video_unregister(&ipipeif->video_out);
795}
796
797int omap4iss_ipipeif_register_entities(struct iss_ipipeif_device *ipipeif,
798	struct v4l2_device *vdev)
799{
800	int ret;
801
802	/* Register the subdev and video node. */
803	ret = v4l2_device_register_subdev(vdev, &ipipeif->subdev);
804	if (ret < 0)
805		goto error;
806
807	ret = omap4iss_video_register(&ipipeif->video_out, vdev);
808	if (ret < 0)
809		goto error;
810
811	return 0;
812
813error:
814	omap4iss_ipipeif_unregister_entities(ipipeif);
815	return ret;
816}
817
818/* -----------------------------------------------------------------------------
819 * ISP IPIPEIF initialisation and cleanup
820 */
821
822/*
823 * omap4iss_ipipeif_init - IPIPEIF module initialization.
824 * @iss: Device pointer specific to the OMAP4 ISS.
825 *
826 * TODO: Get the initialisation values from platform data.
827 *
828 * Return 0 on success or a negative error code otherwise.
829 */
830int omap4iss_ipipeif_init(struct iss_device *iss)
831{
832	struct iss_ipipeif_device *ipipeif = &iss->ipipeif;
833
834	ipipeif->state = ISS_PIPELINE_STREAM_STOPPED;
835	init_waitqueue_head(&ipipeif->wait);
836
837	return ipipeif_init_entities(ipipeif);
838}
839
840/*
841 * omap4iss_ipipeif_cleanup - IPIPEIF module cleanup.
842 * @iss: Device pointer specific to the OMAP4 ISS.
843 */
844void omap4iss_ipipeif_cleanup(struct iss_device *iss)
845{
846	/* FIXME: are you sure there's nothing to do? */
847}
848