]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/media/platform/m2m-deinterlace.c
Merge remote-tracking branch 'v4l-dvb/master'
[karo-tx-linux.git] / drivers / media / platform / m2m-deinterlace.c
1 /*
2  * V4L2 deinterlacing support.
3  *
4  * Copyright (c) 2012 Vista Silicon S.L.
5  * Javier Martin <javier.martin@vista-silicon.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by the
9  * Free Software Foundation; either version 2 of the
10  * License, or (at your option) any later version
11  */
12
13 #include <linux/module.h>
14 #include <linux/slab.h>
15 #include <linux/interrupt.h>
16 #include <linux/dmaengine.h>
17 #include <linux/platform_device.h>
18
19 #include <media/v4l2-mem2mem.h>
20 #include <media/v4l2-device.h>
21 #include <media/v4l2-ioctl.h>
22 #include <media/videobuf2-dma-contig.h>
23
24 #define MEM2MEM_TEST_MODULE_NAME "mem2mem-deinterlace"
25
26 MODULE_DESCRIPTION("mem2mem device which supports deinterlacing using dmaengine");
27 MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com");
28 MODULE_LICENSE("GPL");
29 MODULE_VERSION("0.0.1");
30
31 static bool debug;
32 module_param(debug, bool, 0644);
33
34 /* Flags that indicate a format can be used for capture/output */
35 #define MEM2MEM_CAPTURE (1 << 0)
36 #define MEM2MEM_OUTPUT  (1 << 1)
37
38 #define MEM2MEM_NAME            "m2m-deinterlace"
39
40 #define dprintk(dev, fmt, arg...) \
41         v4l2_dbg(1, debug, &dev->v4l2_dev, "%s: " fmt, __func__, ## arg)
42
43 struct deinterlace_fmt {
44         char    *name;
45         u32     fourcc;
46         /* Types the format can be used for */
47         u32     types;
48 };
49
50 static struct deinterlace_fmt formats[] = {
51         {
52                 .name   = "YUV 4:2:0 Planar",
53                 .fourcc = V4L2_PIX_FMT_YUV420,
54                 .types  = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
55         },
56         {
57                 .name   = "YUYV 4:2:2",
58                 .fourcc = V4L2_PIX_FMT_YUYV,
59                 .types  = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
60         },
61 };
62
63 #define NUM_FORMATS ARRAY_SIZE(formats)
64
65 /* Per-queue, driver-specific private data */
66 struct deinterlace_q_data {
67         unsigned int            width;
68         unsigned int            height;
69         unsigned int            sizeimage;
70         struct deinterlace_fmt  *fmt;
71         enum v4l2_field         field;
72 };
73
74 enum {
75         V4L2_M2M_SRC = 0,
76         V4L2_M2M_DST = 1,
77 };
78
79 enum {
80         YUV420_DMA_Y_ODD,
81         YUV420_DMA_Y_EVEN,
82         YUV420_DMA_U_ODD,
83         YUV420_DMA_U_EVEN,
84         YUV420_DMA_V_ODD,
85         YUV420_DMA_V_EVEN,
86         YUV420_DMA_Y_ODD_DOUBLING,
87         YUV420_DMA_U_ODD_DOUBLING,
88         YUV420_DMA_V_ODD_DOUBLING,
89         YUYV_DMA_ODD,
90         YUYV_DMA_EVEN,
91         YUYV_DMA_EVEN_DOUBLING,
92 };
93
94 /* Source and destination queue data */
95 static struct deinterlace_q_data q_data[2];
96
97 static struct deinterlace_q_data *get_q_data(enum v4l2_buf_type type)
98 {
99         switch (type) {
100         case V4L2_BUF_TYPE_VIDEO_OUTPUT:
101                 return &q_data[V4L2_M2M_SRC];
102         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
103                 return &q_data[V4L2_M2M_DST];
104         default:
105                 BUG();
106         }
107         return NULL;
108 }
109
110 static struct deinterlace_fmt *find_format(struct v4l2_format *f)
111 {
112         struct deinterlace_fmt *fmt;
113         unsigned int k;
114
115         for (k = 0; k < NUM_FORMATS; k++) {
116                 fmt = &formats[k];
117                 if ((fmt->types & f->type) &&
118                         (fmt->fourcc == f->fmt.pix.pixelformat))
119                         break;
120         }
121
122         if (k == NUM_FORMATS)
123                 return NULL;
124
125         return &formats[k];
126 }
127
128 struct deinterlace_dev {
129         struct v4l2_device      v4l2_dev;
130         struct video_device     vfd;
131
132         atomic_t                busy;
133         struct mutex            dev_mutex;
134         spinlock_t              irqlock;
135
136         struct dma_chan         *dma_chan;
137
138         struct v4l2_m2m_dev     *m2m_dev;
139         struct vb2_alloc_ctx    *alloc_ctx;
140 };
141
142 struct deinterlace_ctx {
143         struct deinterlace_dev  *dev;
144
145         /* Abort requested by m2m */
146         int                     aborting;
147         enum v4l2_colorspace    colorspace;
148         dma_cookie_t            cookie;
149         struct v4l2_m2m_ctx     *m2m_ctx;
150         struct dma_interleaved_template *xt;
151 };
152
153 /*
154  * mem2mem callbacks
155  */
156 static int deinterlace_job_ready(void *priv)
157 {
158         struct deinterlace_ctx *ctx = priv;
159         struct deinterlace_dev *pcdev = ctx->dev;
160
161         if ((v4l2_m2m_num_src_bufs_ready(ctx->m2m_ctx) > 0)
162             && (v4l2_m2m_num_dst_bufs_ready(ctx->m2m_ctx) > 0)
163             && (atomic_read(&ctx->dev->busy) == 0)) {
164                 dprintk(pcdev, "Task ready\n");
165                 return 1;
166         }
167
168         dprintk(pcdev, "Task not ready to run\n");
169
170         return 0;
171 }
172
173 static void deinterlace_job_abort(void *priv)
174 {
175         struct deinterlace_ctx *ctx = priv;
176         struct deinterlace_dev *pcdev = ctx->dev;
177
178         ctx->aborting = 1;
179
180         dprintk(pcdev, "Aborting task\n");
181
182         v4l2_m2m_job_finish(pcdev->m2m_dev, ctx->m2m_ctx);
183 }
184
185 static void deinterlace_lock(void *priv)
186 {
187         struct deinterlace_ctx *ctx = priv;
188         struct deinterlace_dev *pcdev = ctx->dev;
189         mutex_lock(&pcdev->dev_mutex);
190 }
191
192 static void deinterlace_unlock(void *priv)
193 {
194         struct deinterlace_ctx *ctx = priv;
195         struct deinterlace_dev *pcdev = ctx->dev;
196         mutex_unlock(&pcdev->dev_mutex);
197 }
198
199 static void dma_callback(void *data)
200 {
201         struct deinterlace_ctx *curr_ctx = data;
202         struct deinterlace_dev *pcdev = curr_ctx->dev;
203         struct vb2_v4l2_buffer *src_vb, *dst_vb;
204
205         atomic_set(&pcdev->busy, 0);
206
207         src_vb = v4l2_m2m_src_buf_remove(curr_ctx->m2m_ctx);
208         dst_vb = v4l2_m2m_dst_buf_remove(curr_ctx->m2m_ctx);
209
210         dst_vb->timestamp = src_vb->timestamp;
211         dst_vb->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
212         dst_vb->flags |=
213                 src_vb->flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
214         dst_vb->timecode = src_vb->timecode;
215
216         v4l2_m2m_buf_done(src_vb, VB2_BUF_STATE_DONE);
217         v4l2_m2m_buf_done(dst_vb, VB2_BUF_STATE_DONE);
218
219         v4l2_m2m_job_finish(pcdev->m2m_dev, curr_ctx->m2m_ctx);
220
221         dprintk(pcdev, "dma transfers completed.\n");
222 }
223
224 static void deinterlace_issue_dma(struct deinterlace_ctx *ctx, int op,
225                                   int do_callback)
226 {
227         struct deinterlace_q_data *s_q_data;
228         struct vb2_v4l2_buffer *src_buf, *dst_buf;
229         struct deinterlace_dev *pcdev = ctx->dev;
230         struct dma_chan *chan = pcdev->dma_chan;
231         struct dma_device *dmadev = chan->device;
232         struct dma_async_tx_descriptor *tx;
233         unsigned int s_width, s_height;
234         unsigned int s_size;
235         dma_addr_t p_in, p_out;
236         enum dma_ctrl_flags flags;
237
238         src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
239         dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
240
241         s_q_data = get_q_data(V4L2_BUF_TYPE_VIDEO_OUTPUT);
242         s_width = s_q_data->width;
243         s_height = s_q_data->height;
244         s_size = s_width * s_height;
245
246         p_in = (dma_addr_t)vb2_dma_contig_plane_dma_addr(&src_buf->vb2_buf, 0);
247         p_out = (dma_addr_t)vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf,
248                                                           0);
249         if (!p_in || !p_out) {
250                 v4l2_err(&pcdev->v4l2_dev,
251                          "Acquiring kernel pointers to buffers failed\n");
252                 return;
253         }
254
255         switch (op) {
256         case YUV420_DMA_Y_ODD:
257                 ctx->xt->numf = s_height / 2;
258                 ctx->xt->sgl[0].size = s_width;
259                 ctx->xt->sgl[0].icg = s_width;
260                 ctx->xt->src_start = p_in;
261                 ctx->xt->dst_start = p_out;
262                 break;
263         case YUV420_DMA_Y_EVEN:
264                 ctx->xt->numf = s_height / 2;
265                 ctx->xt->sgl[0].size = s_width;
266                 ctx->xt->sgl[0].icg = s_width;
267                 ctx->xt->src_start = p_in + s_size / 2;
268                 ctx->xt->dst_start = p_out + s_width;
269                 break;
270         case YUV420_DMA_U_ODD:
271                 ctx->xt->numf = s_height / 4;
272                 ctx->xt->sgl[0].size = s_width / 2;
273                 ctx->xt->sgl[0].icg = s_width / 2;
274                 ctx->xt->src_start = p_in + s_size;
275                 ctx->xt->dst_start = p_out + s_size;
276                 break;
277         case YUV420_DMA_U_EVEN:
278                 ctx->xt->numf = s_height / 4;
279                 ctx->xt->sgl[0].size = s_width / 2;
280                 ctx->xt->sgl[0].icg = s_width / 2;
281                 ctx->xt->src_start = p_in + (9 * s_size) / 8;
282                 ctx->xt->dst_start = p_out + s_size + s_width / 2;
283                 break;
284         case YUV420_DMA_V_ODD:
285                 ctx->xt->numf = s_height / 4;
286                 ctx->xt->sgl[0].size = s_width / 2;
287                 ctx->xt->sgl[0].icg = s_width / 2;
288                 ctx->xt->src_start = p_in + (5 * s_size) / 4;
289                 ctx->xt->dst_start = p_out + (5 * s_size) / 4;
290                 break;
291         case YUV420_DMA_V_EVEN:
292                 ctx->xt->numf = s_height / 4;
293                 ctx->xt->sgl[0].size = s_width / 2;
294                 ctx->xt->sgl[0].icg = s_width / 2;
295                 ctx->xt->src_start = p_in + (11 * s_size) / 8;
296                 ctx->xt->dst_start = p_out + (5 * s_size) / 4 + s_width / 2;
297                 break;
298         case YUV420_DMA_Y_ODD_DOUBLING:
299                 ctx->xt->numf = s_height / 2;
300                 ctx->xt->sgl[0].size = s_width;
301                 ctx->xt->sgl[0].icg = s_width;
302                 ctx->xt->src_start = p_in;
303                 ctx->xt->dst_start = p_out + s_width;
304                 break;
305         case YUV420_DMA_U_ODD_DOUBLING:
306                 ctx->xt->numf = s_height / 4;
307                 ctx->xt->sgl[0].size = s_width / 2;
308                 ctx->xt->sgl[0].icg = s_width / 2;
309                 ctx->xt->src_start = p_in + s_size;
310                 ctx->xt->dst_start = p_out + s_size + s_width / 2;
311                 break;
312         case YUV420_DMA_V_ODD_DOUBLING:
313                 ctx->xt->numf = s_height / 4;
314                 ctx->xt->sgl[0].size = s_width / 2;
315                 ctx->xt->sgl[0].icg = s_width / 2;
316                 ctx->xt->src_start = p_in + (5 * s_size) / 4;
317                 ctx->xt->dst_start = p_out + (5 * s_size) / 4 + s_width / 2;
318                 break;
319         case YUYV_DMA_ODD:
320                 ctx->xt->numf = s_height / 2;
321                 ctx->xt->sgl[0].size = s_width * 2;
322                 ctx->xt->sgl[0].icg = s_width * 2;
323                 ctx->xt->src_start = p_in;
324                 ctx->xt->dst_start = p_out;
325                 break;
326         case YUYV_DMA_EVEN:
327                 ctx->xt->numf = s_height / 2;
328                 ctx->xt->sgl[0].size = s_width * 2;
329                 ctx->xt->sgl[0].icg = s_width * 2;
330                 ctx->xt->src_start = p_in + s_size;
331                 ctx->xt->dst_start = p_out + s_width * 2;
332                 break;
333         case YUYV_DMA_EVEN_DOUBLING:
334         default:
335                 ctx->xt->numf = s_height / 2;
336                 ctx->xt->sgl[0].size = s_width * 2;
337                 ctx->xt->sgl[0].icg = s_width * 2;
338                 ctx->xt->src_start = p_in;
339                 ctx->xt->dst_start = p_out + s_width * 2;
340                 break;
341         }
342
343         /* Common parameters for al transfers */
344         ctx->xt->frame_size = 1;
345         ctx->xt->dir = DMA_MEM_TO_MEM;
346         ctx->xt->src_sgl = false;
347         ctx->xt->dst_sgl = true;
348         flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT;
349
350         tx = dmadev->device_prep_interleaved_dma(chan, ctx->xt, flags);
351         if (tx == NULL) {
352                 v4l2_warn(&pcdev->v4l2_dev, "DMA interleaved prep error\n");
353                 return;
354         }
355
356         if (do_callback) {
357                 tx->callback = dma_callback;
358                 tx->callback_param = ctx;
359         }
360
361         ctx->cookie = dmaengine_submit(tx);
362         if (dma_submit_error(ctx->cookie)) {
363                 v4l2_warn(&pcdev->v4l2_dev,
364                           "DMA submit error %d with src=0x%x dst=0x%x len=0x%x\n",
365                           ctx->cookie, (unsigned)p_in, (unsigned)p_out,
366                           s_size * 3/2);
367                 return;
368         }
369
370         dma_async_issue_pending(chan);
371 }
372
373 static void deinterlace_device_run(void *priv)
374 {
375         struct deinterlace_ctx *ctx = priv;
376         struct deinterlace_q_data *dst_q_data;
377
378         atomic_set(&ctx->dev->busy, 1);
379
380         dprintk(ctx->dev, "%s: DMA try issue.\n", __func__);
381
382         dst_q_data = get_q_data(V4L2_BUF_TYPE_VIDEO_CAPTURE);
383
384         /*
385          * 4 possible field conversions are possible at the moment:
386          *  V4L2_FIELD_SEQ_TB --> V4L2_FIELD_INTERLACED_TB:
387          *      two separate fields in the same input buffer are interlaced
388          *      in the output buffer using weaving. Top field comes first.
389          *  V4L2_FIELD_SEQ_TB --> V4L2_FIELD_NONE:
390          *      top field from the input buffer is copied to the output buffer
391          *      using line doubling. Bottom field from the input buffer is discarded.
392          * V4L2_FIELD_SEQ_BT --> V4L2_FIELD_INTERLACED_BT:
393          *      two separate fields in the same input buffer are interlaced
394          *      in the output buffer using weaving. Bottom field comes first.
395          * V4L2_FIELD_SEQ_BT --> V4L2_FIELD_NONE:
396          *      bottom field from the input buffer is copied to the output buffer
397          *      using line doubling. Top field from the input buffer is discarded.
398          */
399         switch (dst_q_data->fmt->fourcc) {
400         case V4L2_PIX_FMT_YUV420:
401                 switch (dst_q_data->field) {
402                 case V4L2_FIELD_INTERLACED_TB:
403                 case V4L2_FIELD_INTERLACED_BT:
404                         dprintk(ctx->dev, "%s: yuv420 interlaced tb.\n",
405                                 __func__);
406                         deinterlace_issue_dma(ctx, YUV420_DMA_Y_ODD, 0);
407                         deinterlace_issue_dma(ctx, YUV420_DMA_Y_EVEN, 0);
408                         deinterlace_issue_dma(ctx, YUV420_DMA_U_ODD, 0);
409                         deinterlace_issue_dma(ctx, YUV420_DMA_U_EVEN, 0);
410                         deinterlace_issue_dma(ctx, YUV420_DMA_V_ODD, 0);
411                         deinterlace_issue_dma(ctx, YUV420_DMA_V_EVEN, 1);
412                         break;
413                 case V4L2_FIELD_NONE:
414                 default:
415                         dprintk(ctx->dev, "%s: yuv420 interlaced line doubling.\n",
416                                 __func__);
417                         deinterlace_issue_dma(ctx, YUV420_DMA_Y_ODD, 0);
418                         deinterlace_issue_dma(ctx, YUV420_DMA_Y_ODD_DOUBLING, 0);
419                         deinterlace_issue_dma(ctx, YUV420_DMA_U_ODD, 0);
420                         deinterlace_issue_dma(ctx, YUV420_DMA_U_ODD_DOUBLING, 0);
421                         deinterlace_issue_dma(ctx, YUV420_DMA_V_ODD, 0);
422                         deinterlace_issue_dma(ctx, YUV420_DMA_V_ODD_DOUBLING, 1);
423                         break;
424                 }
425                 break;
426         case V4L2_PIX_FMT_YUYV:
427         default:
428                 switch (dst_q_data->field) {
429                 case V4L2_FIELD_INTERLACED_TB:
430                 case V4L2_FIELD_INTERLACED_BT:
431                         dprintk(ctx->dev, "%s: yuyv interlaced_tb.\n",
432                                 __func__);
433                         deinterlace_issue_dma(ctx, YUYV_DMA_ODD, 0);
434                         deinterlace_issue_dma(ctx, YUYV_DMA_EVEN, 1);
435                         break;
436                 case V4L2_FIELD_NONE:
437                 default:
438                         dprintk(ctx->dev, "%s: yuyv interlaced line doubling.\n",
439                                 __func__);
440                         deinterlace_issue_dma(ctx, YUYV_DMA_ODD, 0);
441                         deinterlace_issue_dma(ctx, YUYV_DMA_EVEN_DOUBLING, 1);
442                         break;
443                 }
444                 break;
445         }
446
447         dprintk(ctx->dev, "%s: DMA issue done.\n", __func__);
448 }
449
450 /*
451  * video ioctls
452  */
453 static int vidioc_querycap(struct file *file, void *priv,
454                            struct v4l2_capability *cap)
455 {
456         strlcpy(cap->driver, MEM2MEM_NAME, sizeof(cap->driver));
457         strlcpy(cap->card, MEM2MEM_NAME, sizeof(cap->card));
458         strlcpy(cap->bus_info, MEM2MEM_NAME, sizeof(cap->card));
459         /*
460          * This is only a mem-to-mem video device. The capture and output
461          * device capability flags are left only for backward compatibility
462          * and are scheduled for removal.
463          */
464         cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT |
465                            V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
466         cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
467
468         return 0;
469 }
470
471 static int enum_fmt(struct v4l2_fmtdesc *f, u32 type)
472 {
473         int i, num;
474         struct deinterlace_fmt *fmt;
475
476         num = 0;
477
478         for (i = 0; i < NUM_FORMATS; ++i) {
479                 if (formats[i].types & type) {
480                         /* index-th format of type type found ? */
481                         if (num == f->index)
482                                 break;
483                         /* Correct type but haven't reached our index yet,
484                          * just increment per-type index */
485                         ++num;
486                 }
487         }
488
489         if (i < NUM_FORMATS) {
490                 /* Format found */
491                 fmt = &formats[i];
492                 strlcpy(f->description, fmt->name, sizeof(f->description));
493                 f->pixelformat = fmt->fourcc;
494                 return 0;
495         }
496
497         /* Format not found */
498         return -EINVAL;
499 }
500
501 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
502                                    struct v4l2_fmtdesc *f)
503 {
504         return enum_fmt(f, MEM2MEM_CAPTURE);
505 }
506
507 static int vidioc_enum_fmt_vid_out(struct file *file, void *priv,
508                                    struct v4l2_fmtdesc *f)
509 {
510         return enum_fmt(f, MEM2MEM_OUTPUT);
511 }
512
513 static int vidioc_g_fmt(struct deinterlace_ctx *ctx, struct v4l2_format *f)
514 {
515         struct vb2_queue *vq;
516         struct deinterlace_q_data *q_data;
517
518         vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
519         if (!vq)
520                 return -EINVAL;
521
522         q_data = get_q_data(f->type);
523
524         f->fmt.pix.width        = q_data->width;
525         f->fmt.pix.height       = q_data->height;
526         f->fmt.pix.field        = q_data->field;
527         f->fmt.pix.pixelformat  = q_data->fmt->fourcc;
528
529         switch (q_data->fmt->fourcc) {
530         case V4L2_PIX_FMT_YUV420:
531                 f->fmt.pix.bytesperline = q_data->width * 3 / 2;
532                 break;
533         case V4L2_PIX_FMT_YUYV:
534         default:
535                 f->fmt.pix.bytesperline = q_data->width * 2;
536         }
537
538         f->fmt.pix.sizeimage    = q_data->sizeimage;
539         f->fmt.pix.colorspace   = ctx->colorspace;
540
541         return 0;
542 }
543
544 static int vidioc_g_fmt_vid_out(struct file *file, void *priv,
545                                 struct v4l2_format *f)
546 {
547         return vidioc_g_fmt(priv, f);
548 }
549
550 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
551                                 struct v4l2_format *f)
552 {
553         return vidioc_g_fmt(priv, f);
554 }
555
556 static int vidioc_try_fmt(struct v4l2_format *f, struct deinterlace_fmt *fmt)
557 {
558         switch (f->fmt.pix.pixelformat) {
559         case V4L2_PIX_FMT_YUV420:
560                 f->fmt.pix.bytesperline = f->fmt.pix.width * 3 / 2;
561                 break;
562         case V4L2_PIX_FMT_YUYV:
563         default:
564                 f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
565         }
566         f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
567
568         return 0;
569 }
570
571 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
572                                   struct v4l2_format *f)
573 {
574         struct deinterlace_fmt *fmt;
575         struct deinterlace_ctx *ctx = priv;
576
577         fmt = find_format(f);
578         if (!fmt || !(fmt->types & MEM2MEM_CAPTURE))
579                 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUV420;
580
581         f->fmt.pix.colorspace = ctx->colorspace;
582
583         if (f->fmt.pix.field != V4L2_FIELD_INTERLACED_TB &&
584             f->fmt.pix.field != V4L2_FIELD_INTERLACED_BT &&
585             f->fmt.pix.field != V4L2_FIELD_NONE)
586                 f->fmt.pix.field = V4L2_FIELD_INTERLACED_TB;
587
588         return vidioc_try_fmt(f, fmt);
589 }
590
591 static int vidioc_try_fmt_vid_out(struct file *file, void *priv,
592                                   struct v4l2_format *f)
593 {
594         struct deinterlace_fmt *fmt;
595
596         fmt = find_format(f);
597         if (!fmt || !(fmt->types & MEM2MEM_OUTPUT))
598                 f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUV420;
599
600         if (!f->fmt.pix.colorspace)
601                 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
602
603         if (f->fmt.pix.field != V4L2_FIELD_SEQ_TB &&
604             f->fmt.pix.field != V4L2_FIELD_SEQ_BT)
605                 f->fmt.pix.field = V4L2_FIELD_SEQ_TB;
606
607         return vidioc_try_fmt(f, fmt);
608 }
609
610 static int vidioc_s_fmt(struct deinterlace_ctx *ctx, struct v4l2_format *f)
611 {
612         struct deinterlace_q_data *q_data;
613         struct vb2_queue *vq;
614
615         vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
616         if (!vq)
617                 return -EINVAL;
618
619         q_data = get_q_data(f->type);
620         if (!q_data)
621                 return -EINVAL;
622
623         if (vb2_is_busy(vq)) {
624                 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
625                 return -EBUSY;
626         }
627
628         q_data->fmt = find_format(f);
629         if (!q_data->fmt) {
630                 v4l2_err(&ctx->dev->v4l2_dev,
631                          "Couldn't set format type %d, wxh: %dx%d. fmt: %d, field: %d\n",
632                         f->type, f->fmt.pix.width, f->fmt.pix.height,
633                         f->fmt.pix.pixelformat, f->fmt.pix.field);
634                 return -EINVAL;
635         }
636
637         q_data->width           = f->fmt.pix.width;
638         q_data->height          = f->fmt.pix.height;
639         q_data->field           = f->fmt.pix.field;
640
641         switch (f->fmt.pix.pixelformat) {
642         case V4L2_PIX_FMT_YUV420:
643                 f->fmt.pix.bytesperline = f->fmt.pix.width * 3 / 2;
644                 q_data->sizeimage = (q_data->width * q_data->height * 3) / 2;
645                 break;
646         case V4L2_PIX_FMT_YUYV:
647         default:
648                 f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
649                 q_data->sizeimage = q_data->width * q_data->height * 2;
650         }
651
652         dprintk(ctx->dev,
653                 "Setting format for type %d, wxh: %dx%d, fmt: %d, field: %d\n",
654                 f->type, q_data->width, q_data->height, q_data->fmt->fourcc,
655                 q_data->field);
656
657         return 0;
658 }
659
660 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
661                                 struct v4l2_format *f)
662 {
663         int ret;
664
665         ret = vidioc_try_fmt_vid_cap(file, priv, f);
666         if (ret)
667                 return ret;
668         return vidioc_s_fmt(priv, f);
669 }
670
671 static int vidioc_s_fmt_vid_out(struct file *file, void *priv,
672                                 struct v4l2_format *f)
673 {
674         struct deinterlace_ctx *ctx = priv;
675         int ret;
676
677         ret = vidioc_try_fmt_vid_out(file, priv, f);
678         if (ret)
679                 return ret;
680
681         ret = vidioc_s_fmt(priv, f);
682         if (!ret)
683                 ctx->colorspace = f->fmt.pix.colorspace;
684
685         return ret;
686 }
687
688 static int vidioc_reqbufs(struct file *file, void *priv,
689                           struct v4l2_requestbuffers *reqbufs)
690 {
691         struct deinterlace_ctx *ctx = priv;
692
693         return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
694 }
695
696 static int vidioc_querybuf(struct file *file, void *priv,
697                            struct v4l2_buffer *buf)
698 {
699         struct deinterlace_ctx *ctx = priv;
700
701         return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
702 }
703
704 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
705 {
706         struct deinterlace_ctx *ctx = priv;
707
708         return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
709 }
710
711 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
712 {
713         struct deinterlace_ctx *ctx = priv;
714
715         return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
716 }
717
718 static int vidioc_streamon(struct file *file, void *priv,
719                            enum v4l2_buf_type type)
720 {
721         struct deinterlace_q_data *s_q_data, *d_q_data;
722         struct deinterlace_ctx *ctx = priv;
723
724         s_q_data = get_q_data(V4L2_BUF_TYPE_VIDEO_OUTPUT);
725         d_q_data = get_q_data(V4L2_BUF_TYPE_VIDEO_CAPTURE);
726
727         /* Check that src and dst queues have the same pix format */
728         if (s_q_data->fmt->fourcc != d_q_data->fmt->fourcc) {
729                 v4l2_err(&ctx->dev->v4l2_dev,
730                          "src and dst formats don't match.\n");
731                 return -EINVAL;
732         }
733
734         /* Check that input and output deinterlacing types are compatible */
735         switch (s_q_data->field) {
736         case V4L2_FIELD_SEQ_BT:
737                 if (d_q_data->field != V4L2_FIELD_NONE &&
738                         d_q_data->field != V4L2_FIELD_INTERLACED_BT) {
739                         v4l2_err(&ctx->dev->v4l2_dev,
740                          "src and dst field conversion [(%d)->(%d)] not supported.\n",
741                                 s_q_data->field, d_q_data->field);
742                         return -EINVAL;
743                 }
744                 break;
745         case V4L2_FIELD_SEQ_TB:
746                 if (d_q_data->field != V4L2_FIELD_NONE &&
747                         d_q_data->field != V4L2_FIELD_INTERLACED_TB) {
748                         v4l2_err(&ctx->dev->v4l2_dev,
749                          "src and dst field conversion [(%d)->(%d)] not supported.\n",
750                                 s_q_data->field, d_q_data->field);
751                         return -EINVAL;
752                 }
753                 break;
754         default:
755                 return -EINVAL;
756         }
757
758         return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
759 }
760
761 static int vidioc_streamoff(struct file *file, void *priv,
762                             enum v4l2_buf_type type)
763 {
764         struct deinterlace_ctx *ctx = priv;
765
766         return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
767 }
768
769 static const struct v4l2_ioctl_ops deinterlace_ioctl_ops = {
770         .vidioc_querycap        = vidioc_querycap,
771
772         .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
773         .vidioc_g_fmt_vid_cap   = vidioc_g_fmt_vid_cap,
774         .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
775         .vidioc_s_fmt_vid_cap   = vidioc_s_fmt_vid_cap,
776
777         .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
778         .vidioc_g_fmt_vid_out   = vidioc_g_fmt_vid_out,
779         .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out,
780         .vidioc_s_fmt_vid_out   = vidioc_s_fmt_vid_out,
781
782         .vidioc_reqbufs         = vidioc_reqbufs,
783         .vidioc_querybuf        = vidioc_querybuf,
784
785         .vidioc_qbuf            = vidioc_qbuf,
786         .vidioc_dqbuf           = vidioc_dqbuf,
787
788         .vidioc_streamon        = vidioc_streamon,
789         .vidioc_streamoff       = vidioc_streamoff,
790 };
791
792
793 /*
794  * Queue operations
795  */
796 struct vb2_dc_conf {
797         struct device           *dev;
798 };
799
800 static int deinterlace_queue_setup(struct vb2_queue *vq,
801                                 const void *parg,
802                                 unsigned int *nbuffers, unsigned int *nplanes,
803                                 unsigned int sizes[], void *alloc_ctxs[])
804 {
805         struct deinterlace_ctx *ctx = vb2_get_drv_priv(vq);
806         struct deinterlace_q_data *q_data;
807         unsigned int size, count = *nbuffers;
808
809         q_data = get_q_data(vq->type);
810
811         switch (q_data->fmt->fourcc) {
812         case V4L2_PIX_FMT_YUV420:
813                 size = q_data->width * q_data->height * 3 / 2;
814                 break;
815         case V4L2_PIX_FMT_YUYV:
816         default:
817                 size = q_data->width * q_data->height * 2;
818         }
819
820         *nplanes = 1;
821         *nbuffers = count;
822         sizes[0] = size;
823
824         alloc_ctxs[0] = ctx->dev->alloc_ctx;
825
826         dprintk(ctx->dev, "get %d buffer(s) of size %d each.\n", count, size);
827
828         return 0;
829 }
830
831 static int deinterlace_buf_prepare(struct vb2_buffer *vb)
832 {
833         struct deinterlace_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
834         struct deinterlace_q_data *q_data;
835
836         dprintk(ctx->dev, "type: %d\n", vb->vb2_queue->type);
837
838         q_data = get_q_data(vb->vb2_queue->type);
839
840         if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
841                 dprintk(ctx->dev, "%s data will not fit into plane (%lu < %lu)\n",
842                         __func__, vb2_plane_size(vb, 0), (long)q_data->sizeimage);
843                 return -EINVAL;
844         }
845
846         vb2_set_plane_payload(vb, 0, q_data->sizeimage);
847
848         return 0;
849 }
850
851 static void deinterlace_buf_queue(struct vb2_buffer *vb)
852 {
853         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
854         struct deinterlace_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
855
856         v4l2_m2m_buf_queue(ctx->m2m_ctx, vbuf);
857 }
858
859 static struct vb2_ops deinterlace_qops = {
860         .queue_setup     = deinterlace_queue_setup,
861         .buf_prepare     = deinterlace_buf_prepare,
862         .buf_queue       = deinterlace_buf_queue,
863 };
864
865 static int queue_init(void *priv, struct vb2_queue *src_vq,
866                       struct vb2_queue *dst_vq)
867 {
868         struct deinterlace_ctx *ctx = priv;
869         int ret;
870
871         src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
872         src_vq->io_modes = VB2_MMAP | VB2_USERPTR;
873         src_vq->drv_priv = ctx;
874         src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
875         src_vq->ops = &deinterlace_qops;
876         src_vq->mem_ops = &vb2_dma_contig_memops;
877         src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
878         q_data[V4L2_M2M_SRC].fmt = &formats[0];
879         q_data[V4L2_M2M_SRC].width = 640;
880         q_data[V4L2_M2M_SRC].height = 480;
881         q_data[V4L2_M2M_SRC].sizeimage = (640 * 480 * 3) / 2;
882         q_data[V4L2_M2M_SRC].field = V4L2_FIELD_SEQ_TB;
883
884         ret = vb2_queue_init(src_vq);
885         if (ret)
886                 return ret;
887
888         dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
889         dst_vq->io_modes = VB2_MMAP | VB2_USERPTR;
890         dst_vq->drv_priv = ctx;
891         dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
892         dst_vq->ops = &deinterlace_qops;
893         dst_vq->mem_ops = &vb2_dma_contig_memops;
894         dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
895         q_data[V4L2_M2M_DST].fmt = &formats[0];
896         q_data[V4L2_M2M_DST].width = 640;
897         q_data[V4L2_M2M_DST].height = 480;
898         q_data[V4L2_M2M_DST].sizeimage = (640 * 480 * 3) / 2;
899         q_data[V4L2_M2M_SRC].field = V4L2_FIELD_INTERLACED_TB;
900
901         return vb2_queue_init(dst_vq);
902 }
903
904 /*
905  * File operations
906  */
907 static int deinterlace_open(struct file *file)
908 {
909         struct deinterlace_dev *pcdev = video_drvdata(file);
910         struct deinterlace_ctx *ctx = NULL;
911
912         ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
913         if (!ctx)
914                 return -ENOMEM;
915
916         file->private_data = ctx;
917         ctx->dev = pcdev;
918
919         ctx->m2m_ctx = v4l2_m2m_ctx_init(pcdev->m2m_dev, ctx, &queue_init);
920         if (IS_ERR(ctx->m2m_ctx)) {
921                 int ret = PTR_ERR(ctx->m2m_ctx);
922
923                 kfree(ctx);
924                 return ret;
925         }
926
927         ctx->xt = kzalloc(sizeof(struct dma_interleaved_template) +
928                                 sizeof(struct data_chunk), GFP_KERNEL);
929         if (!ctx->xt) {
930                 kfree(ctx);
931                 return -ENOMEM;
932         }
933
934         ctx->colorspace = V4L2_COLORSPACE_REC709;
935
936         dprintk(pcdev, "Created instance %p, m2m_ctx: %p\n", ctx, ctx->m2m_ctx);
937
938         return 0;
939 }
940
941 static int deinterlace_release(struct file *file)
942 {
943         struct deinterlace_dev *pcdev = video_drvdata(file);
944         struct deinterlace_ctx *ctx = file->private_data;
945
946         dprintk(pcdev, "Releasing instance %p\n", ctx);
947
948         v4l2_m2m_ctx_release(ctx->m2m_ctx);
949         kfree(ctx->xt);
950         kfree(ctx);
951
952         return 0;
953 }
954
955 static unsigned int deinterlace_poll(struct file *file,
956                                  struct poll_table_struct *wait)
957 {
958         struct deinterlace_ctx *ctx = file->private_data;
959         int ret;
960
961         deinterlace_lock(ctx);
962         ret = v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
963         deinterlace_unlock(ctx);
964
965         return ret;
966 }
967
968 static int deinterlace_mmap(struct file *file, struct vm_area_struct *vma)
969 {
970         struct deinterlace_ctx *ctx = file->private_data;
971
972         return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
973 }
974
975 static const struct v4l2_file_operations deinterlace_fops = {
976         .owner          = THIS_MODULE,
977         .open           = deinterlace_open,
978         .release        = deinterlace_release,
979         .poll           = deinterlace_poll,
980         .unlocked_ioctl = video_ioctl2,
981         .mmap           = deinterlace_mmap,
982 };
983
984 static struct video_device deinterlace_videodev = {
985         .name           = MEM2MEM_NAME,
986         .fops           = &deinterlace_fops,
987         .ioctl_ops      = &deinterlace_ioctl_ops,
988         .minor          = -1,
989         .release        = video_device_release_empty,
990         .vfl_dir        = VFL_DIR_M2M,
991 };
992
993 static struct v4l2_m2m_ops m2m_ops = {
994         .device_run     = deinterlace_device_run,
995         .job_ready      = deinterlace_job_ready,
996         .job_abort      = deinterlace_job_abort,
997         .lock           = deinterlace_lock,
998         .unlock         = deinterlace_unlock,
999 };
1000
1001 static int deinterlace_probe(struct platform_device *pdev)
1002 {
1003         struct deinterlace_dev *pcdev;
1004         struct video_device *vfd;
1005         dma_cap_mask_t mask;
1006         int ret = 0;
1007
1008         pcdev = devm_kzalloc(&pdev->dev, sizeof(*pcdev), GFP_KERNEL);
1009         if (!pcdev)
1010                 return -ENOMEM;
1011
1012         spin_lock_init(&pcdev->irqlock);
1013
1014         dma_cap_zero(mask);
1015         dma_cap_set(DMA_INTERLEAVE, mask);
1016         pcdev->dma_chan = dma_request_channel(mask, NULL, pcdev);
1017         if (!pcdev->dma_chan)
1018                 return -ENODEV;
1019
1020         if (!dma_has_cap(DMA_INTERLEAVE, pcdev->dma_chan->device->cap_mask)) {
1021                 v4l2_err(&pcdev->v4l2_dev, "DMA does not support INTERLEAVE\n");
1022                 goto rel_dma;
1023         }
1024
1025         ret = v4l2_device_register(&pdev->dev, &pcdev->v4l2_dev);
1026         if (ret)
1027                 goto rel_dma;
1028
1029         atomic_set(&pcdev->busy, 0);
1030         mutex_init(&pcdev->dev_mutex);
1031
1032         vfd = &pcdev->vfd;
1033         *vfd = deinterlace_videodev;
1034         vfd->lock = &pcdev->dev_mutex;
1035         vfd->v4l2_dev = &pcdev->v4l2_dev;
1036
1037         ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
1038         if (ret) {
1039                 v4l2_err(&pcdev->v4l2_dev, "Failed to register video device\n");
1040                 goto unreg_dev;
1041         }
1042
1043         video_set_drvdata(vfd, pcdev);
1044         snprintf(vfd->name, sizeof(vfd->name), "%s", deinterlace_videodev.name);
1045         v4l2_info(&pcdev->v4l2_dev, MEM2MEM_TEST_MODULE_NAME
1046                         " Device registered as /dev/video%d\n", vfd->num);
1047
1048         platform_set_drvdata(pdev, pcdev);
1049
1050         pcdev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
1051         if (IS_ERR(pcdev->alloc_ctx)) {
1052                 v4l2_err(&pcdev->v4l2_dev, "Failed to alloc vb2 context\n");
1053                 ret = PTR_ERR(pcdev->alloc_ctx);
1054                 goto err_ctx;
1055         }
1056
1057         pcdev->m2m_dev = v4l2_m2m_init(&m2m_ops);
1058         if (IS_ERR(pcdev->m2m_dev)) {
1059                 v4l2_err(&pcdev->v4l2_dev, "Failed to init mem2mem device\n");
1060                 ret = PTR_ERR(pcdev->m2m_dev);
1061                 goto err_m2m;
1062         }
1063
1064         return 0;
1065
1066 err_m2m:
1067         video_unregister_device(&pcdev->vfd);
1068 err_ctx:
1069         vb2_dma_contig_cleanup_ctx(pcdev->alloc_ctx);
1070 unreg_dev:
1071         v4l2_device_unregister(&pcdev->v4l2_dev);
1072 rel_dma:
1073         dma_release_channel(pcdev->dma_chan);
1074
1075         return ret;
1076 }
1077
1078 static int deinterlace_remove(struct platform_device *pdev)
1079 {
1080         struct deinterlace_dev *pcdev = platform_get_drvdata(pdev);
1081
1082         v4l2_info(&pcdev->v4l2_dev, "Removing " MEM2MEM_TEST_MODULE_NAME);
1083         v4l2_m2m_release(pcdev->m2m_dev);
1084         video_unregister_device(&pcdev->vfd);
1085         v4l2_device_unregister(&pcdev->v4l2_dev);
1086         vb2_dma_contig_cleanup_ctx(pcdev->alloc_ctx);
1087         dma_release_channel(pcdev->dma_chan);
1088
1089         return 0;
1090 }
1091
1092 static struct platform_driver deinterlace_pdrv = {
1093         .probe          = deinterlace_probe,
1094         .remove         = deinterlace_remove,
1095         .driver         = {
1096                 .name   = MEM2MEM_NAME,
1097         },
1098 };
1099 module_platform_driver(deinterlace_pdrv);
1100