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