These changes are the raw update to linux-4.4.6-rt14. Kernel sources
[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 split 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)
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,
522                               struct v4l2_subdev_pad_config *cfg,
523                               struct v4l2_subdev_format *fmt)
524 {
525         struct iss_ipipeif_device *ipipeif = v4l2_get_subdevdata(sd);
526         struct v4l2_mbus_framefmt *format;
527
528         format = __ipipeif_get_format(ipipeif, cfg, fmt->pad, fmt->which);
529         if (!format)
530                 return -EINVAL;
531
532         fmt->format = *format;
533         return 0;
534 }
535
536 /*
537  * ipipeif_set_format - Set the video format on a pad
538  * @sd : ISP IPIPEIF V4L2 subdevice
539  * @cfg: V4L2 subdev pad config
540  * @fmt: Format
541  *
542  * Return 0 on success or -EINVAL if the pad is invalid or doesn't correspond
543  * to the format type.
544  */
545 static int ipipeif_set_format(struct v4l2_subdev *sd,
546                               struct v4l2_subdev_pad_config *cfg,
547                               struct v4l2_subdev_format *fmt)
548 {
549         struct iss_ipipeif_device *ipipeif = v4l2_get_subdevdata(sd);
550         struct v4l2_mbus_framefmt *format;
551
552         format = __ipipeif_get_format(ipipeif, cfg, fmt->pad, fmt->which);
553         if (!format)
554                 return -EINVAL;
555
556         ipipeif_try_format(ipipeif, cfg, fmt->pad, &fmt->format, fmt->which);
557         *format = fmt->format;
558
559         /* Propagate the format from sink to source */
560         if (fmt->pad == IPIPEIF_PAD_SINK) {
561                 format = __ipipeif_get_format(ipipeif, cfg,
562                                               IPIPEIF_PAD_SOURCE_ISIF_SF,
563                                               fmt->which);
564                 *format = fmt->format;
565                 ipipeif_try_format(ipipeif, cfg, IPIPEIF_PAD_SOURCE_ISIF_SF,
566                                    format, fmt->which);
567
568                 format = __ipipeif_get_format(ipipeif, cfg,
569                                               IPIPEIF_PAD_SOURCE_VP,
570                                               fmt->which);
571                 *format = fmt->format;
572                 ipipeif_try_format(ipipeif, cfg, IPIPEIF_PAD_SOURCE_VP, format,
573                                    fmt->which);
574         }
575
576         return 0;
577 }
578
579 static int ipipeif_link_validate(struct v4l2_subdev *sd,
580                                  struct media_link *link,
581                                  struct v4l2_subdev_format *source_fmt,
582                                  struct v4l2_subdev_format *sink_fmt)
583 {
584         /* Check if the two ends match */
585         if (source_fmt->format.width != sink_fmt->format.width ||
586             source_fmt->format.height != sink_fmt->format.height)
587                 return -EPIPE;
588
589         if (source_fmt->format.code != sink_fmt->format.code)
590                 return -EPIPE;
591
592         return 0;
593 }
594
595 /*
596  * ipipeif_init_formats - Initialize formats on all pads
597  * @sd: ISP IPIPEIF V4L2 subdevice
598  * @fh: V4L2 subdev file handle
599  *
600  * Initialize all pad formats with default values. If fh is not NULL, try
601  * formats are initialized on the file handle. Otherwise active formats are
602  * initialized on the device.
603  */
604 static int ipipeif_init_formats(struct v4l2_subdev *sd,
605                                 struct v4l2_subdev_fh *fh)
606 {
607         struct v4l2_subdev_format format;
608
609         memset(&format, 0, sizeof(format));
610         format.pad = IPIPEIF_PAD_SINK;
611         format.which = fh ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
612         format.format.code = MEDIA_BUS_FMT_SGRBG10_1X10;
613         format.format.width = 4096;
614         format.format.height = 4096;
615         ipipeif_set_format(sd, fh ? fh->pad : NULL, &format);
616
617         return 0;
618 }
619
620 /* V4L2 subdev video operations */
621 static const struct v4l2_subdev_video_ops ipipeif_v4l2_video_ops = {
622         .s_stream = ipipeif_set_stream,
623 };
624
625 /* V4L2 subdev pad operations */
626 static const struct v4l2_subdev_pad_ops ipipeif_v4l2_pad_ops = {
627         .enum_mbus_code = ipipeif_enum_mbus_code,
628         .enum_frame_size = ipipeif_enum_frame_size,
629         .get_fmt = ipipeif_get_format,
630         .set_fmt = ipipeif_set_format,
631         .link_validate = ipipeif_link_validate,
632 };
633
634 /* V4L2 subdev operations */
635 static const struct v4l2_subdev_ops ipipeif_v4l2_ops = {
636         .video = &ipipeif_v4l2_video_ops,
637         .pad = &ipipeif_v4l2_pad_ops,
638 };
639
640 /* V4L2 subdev internal operations */
641 static const struct v4l2_subdev_internal_ops ipipeif_v4l2_internal_ops = {
642         .open = ipipeif_init_formats,
643 };
644
645 /* -----------------------------------------------------------------------------
646  * Media entity operations
647  */
648
649 /*
650  * ipipeif_link_setup - Setup IPIPEIF connections
651  * @entity: IPIPEIF media entity
652  * @local: Pad at the local end of the link
653  * @remote: Pad at the remote end of the link
654  * @flags: Link flags
655  *
656  * return -EINVAL or zero on success
657  */
658 static int ipipeif_link_setup(struct media_entity *entity,
659                               const struct media_pad *local,
660                               const struct media_pad *remote, u32 flags)
661 {
662         struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
663         struct iss_ipipeif_device *ipipeif = v4l2_get_subdevdata(sd);
664         struct iss_device *iss = to_iss_device(ipipeif);
665
666         switch (local->index | media_entity_type(remote->entity)) {
667         case IPIPEIF_PAD_SINK | MEDIA_ENT_T_V4L2_SUBDEV:
668                 /* Read from the sensor CSI2a or CSI2b. */
669                 if (!(flags & MEDIA_LNK_FL_ENABLED)) {
670                         ipipeif->input = IPIPEIF_INPUT_NONE;
671                         break;
672                 }
673
674                 if (ipipeif->input != IPIPEIF_INPUT_NONE)
675                         return -EBUSY;
676
677                 if (remote->entity == &iss->csi2a.subdev.entity)
678                         ipipeif->input = IPIPEIF_INPUT_CSI2A;
679                 else if (remote->entity == &iss->csi2b.subdev.entity)
680                         ipipeif->input = IPIPEIF_INPUT_CSI2B;
681
682                 break;
683
684         case IPIPEIF_PAD_SOURCE_ISIF_SF | MEDIA_ENT_T_DEVNODE:
685                 /* Write to memory */
686                 if (flags & MEDIA_LNK_FL_ENABLED) {
687                         if (ipipeif->output & ~IPIPEIF_OUTPUT_MEMORY)
688                                 return -EBUSY;
689                         ipipeif->output |= IPIPEIF_OUTPUT_MEMORY;
690                 } else {
691                         ipipeif->output &= ~IPIPEIF_OUTPUT_MEMORY;
692                 }
693                 break;
694
695         case IPIPEIF_PAD_SOURCE_VP | MEDIA_ENT_T_V4L2_SUBDEV:
696                 /* Send to IPIPE/RESIZER */
697                 if (flags & MEDIA_LNK_FL_ENABLED) {
698                         if (ipipeif->output & ~IPIPEIF_OUTPUT_VP)
699                                 return -EBUSY;
700                         ipipeif->output |= IPIPEIF_OUTPUT_VP;
701                 } else {
702                         ipipeif->output &= ~IPIPEIF_OUTPUT_VP;
703                 }
704                 break;
705
706         default:
707                 return -EINVAL;
708         }
709
710         return 0;
711 }
712
713 /* media operations */
714 static const struct media_entity_operations ipipeif_media_ops = {
715         .link_setup = ipipeif_link_setup,
716         .link_validate = v4l2_subdev_link_validate,
717 };
718
719 /*
720  * ipipeif_init_entities - Initialize V4L2 subdev and media entity
721  * @ipipeif: ISS ISP IPIPEIF module
722  *
723  * Return 0 on success and a negative error code on failure.
724  */
725 static int ipipeif_init_entities(struct iss_ipipeif_device *ipipeif)
726 {
727         struct v4l2_subdev *sd = &ipipeif->subdev;
728         struct media_pad *pads = ipipeif->pads;
729         struct media_entity *me = &sd->entity;
730         int ret;
731
732         ipipeif->input = IPIPEIF_INPUT_NONE;
733
734         v4l2_subdev_init(sd, &ipipeif_v4l2_ops);
735         sd->internal_ops = &ipipeif_v4l2_internal_ops;
736         strlcpy(sd->name, "OMAP4 ISS ISP IPIPEIF", sizeof(sd->name));
737         sd->grp_id = 1 << 16;   /* group ID for iss subdevs */
738         v4l2_set_subdevdata(sd, ipipeif);
739         sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
740
741         pads[IPIPEIF_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
742         pads[IPIPEIF_PAD_SOURCE_ISIF_SF].flags = MEDIA_PAD_FL_SOURCE;
743         pads[IPIPEIF_PAD_SOURCE_VP].flags = MEDIA_PAD_FL_SOURCE;
744
745         me->ops = &ipipeif_media_ops;
746         ret = media_entity_init(me, IPIPEIF_PADS_NUM, pads, 0);
747         if (ret < 0)
748                 return ret;
749
750         ipipeif_init_formats(sd, NULL);
751
752         ipipeif->video_out.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
753         ipipeif->video_out.ops = &ipipeif_video_ops;
754         ipipeif->video_out.iss = to_iss_device(ipipeif);
755         ipipeif->video_out.capture_mem = PAGE_ALIGN(4096 * 4096) * 3;
756         ipipeif->video_out.bpl_alignment = 32;
757         ipipeif->video_out.bpl_zero_padding = 1;
758         ipipeif->video_out.bpl_max = 0x1ffe0;
759
760         ret = omap4iss_video_init(&ipipeif->video_out, "ISP IPIPEIF");
761         if (ret < 0)
762                 return ret;
763
764         /* Connect the IPIPEIF subdev to the video node. */
765         ret = media_entity_create_link(&ipipeif->subdev.entity,
766                                        IPIPEIF_PAD_SOURCE_ISIF_SF,
767                                        &ipipeif->video_out.video.entity, 0, 0);
768         if (ret < 0)
769                 return ret;
770
771         return 0;
772 }
773
774 void omap4iss_ipipeif_unregister_entities(struct iss_ipipeif_device *ipipeif)
775 {
776         v4l2_device_unregister_subdev(&ipipeif->subdev);
777         omap4iss_video_unregister(&ipipeif->video_out);
778 }
779
780 int omap4iss_ipipeif_register_entities(struct iss_ipipeif_device *ipipeif,
781                                        struct v4l2_device *vdev)
782 {
783         int ret;
784
785         /* Register the subdev and video node. */
786         ret = v4l2_device_register_subdev(vdev, &ipipeif->subdev);
787         if (ret < 0)
788                 goto error;
789
790         ret = omap4iss_video_register(&ipipeif->video_out, vdev);
791         if (ret < 0)
792                 goto error;
793
794         return 0;
795
796 error:
797         omap4iss_ipipeif_unregister_entities(ipipeif);
798         return ret;
799 }
800
801 /* -----------------------------------------------------------------------------
802  * ISP IPIPEIF initialisation and cleanup
803  */
804
805 /*
806  * omap4iss_ipipeif_init - IPIPEIF module initialization.
807  * @iss: Device pointer specific to the OMAP4 ISS.
808  *
809  * TODO: Get the initialisation values from platform data.
810  *
811  * Return 0 on success or a negative error code otherwise.
812  */
813 int omap4iss_ipipeif_init(struct iss_device *iss)
814 {
815         struct iss_ipipeif_device *ipipeif = &iss->ipipeif;
816
817         ipipeif->state = ISS_PIPELINE_STREAM_STOPPED;
818         init_waitqueue_head(&ipipeif->wait);
819
820         return ipipeif_init_entities(ipipeif);
821 }
822
823 /*
824  * omap4iss_ipipeif_cleanup - IPIPEIF module cleanup.
825  * @iss: Device pointer specific to the OMAP4 ISS.
826  */
827 void omap4iss_ipipeif_cleanup(struct iss_device *iss)
828 {
829         struct iss_ipipeif_device *ipipeif = &iss->ipipeif;
830
831         media_entity_cleanup(&ipipeif->subdev.entity);
832 }