GNU Linux-libre 4.19.286-gnu1
[releases.git] / drivers / media / platform / sti / delta / delta-v4l2.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) STMicroelectronics SA 2015
4  * Authors: Hugues Fruchet <hugues.fruchet@st.com>
5  *          Jean-Christophe Trotin <jean-christophe.trotin@st.com>
6  *          for STMicroelectronics.
7  */
8
9 #include <linux/clk.h>
10 #include <linux/module.h>
11 #include <linux/platform_device.h>
12 #include <linux/pm_runtime.h>
13 #include <linux/slab.h>
14
15 #include <media/v4l2-ioctl.h>
16 #include <media/v4l2-event.h>
17 #include <media/videobuf2-dma-contig.h>
18
19 #include "delta.h"
20 #include "delta-debug.h"
21 #include "delta-ipc.h"
22
23 #define DELTA_NAME      "st-delta"
24
25 #define DELTA_PREFIX "[---:----]"
26
27 #define to_ctx(__fh) container_of(__fh, struct delta_ctx, fh)
28 #define to_au(__vbuf) container_of(__vbuf, struct delta_au, vbuf)
29 #define to_frame(__vbuf) container_of(__vbuf, struct delta_frame, vbuf)
30
31 #define call_dec_op(dec, op, args...)\
32                 ((dec && (dec)->op) ? (dec)->op(args) : 0)
33
34 /* registry of available decoders */
35 static const struct delta_dec *delta_decoders[] = {
36 #ifdef CONFIG_VIDEO_STI_DELTA_MJPEG
37         &mjpegdec,
38 #endif
39 };
40
41 static inline int frame_size(u32 w, u32 h, u32 fmt)
42 {
43         switch (fmt) {
44         case V4L2_PIX_FMT_NV12:
45                 return (w * h * 3) / 2;
46         default:
47                 return 0;
48         }
49 }
50
51 static inline int frame_stride(u32 w, u32 fmt)
52 {
53         switch (fmt) {
54         case V4L2_PIX_FMT_NV12:
55                 return w;
56         default:
57                 return 0;
58         }
59 }
60
61 static void dump_au(struct delta_ctx *ctx, struct delta_au *au)
62 {
63         struct delta_dev *delta = ctx->dev;
64         u32 size = 10;  /* dump first & last 10 bytes */
65         u8 *data = (u8 *)(au->vaddr);
66
67         if (au->size <= (size * 2))
68                 dev_dbg(delta->dev, "%s dump au[%d] dts=%lld size=%d data=%*ph\n",
69                         ctx->name, au->vbuf.vb2_buf.index, au->dts, au->size,
70                         au->size, data);
71         else
72                 dev_dbg(delta->dev, "%s dump au[%d] dts=%lld size=%d data=%*ph..%*ph\n",
73                         ctx->name, au->vbuf.vb2_buf.index, au->dts, au->size,
74                         size, data, size, data + au->size - size);
75 }
76
77 static void dump_frame(struct delta_ctx *ctx, struct delta_frame *frame)
78 {
79         struct delta_dev *delta = ctx->dev;
80         u32 size = 10;  /* dump first 10 bytes */
81         u8 *data = (u8 *)(frame->vaddr);
82
83         dev_dbg(delta->dev, "%s dump frame[%d] dts=%lld type=%s field=%s data=%*ph\n",
84                 ctx->name, frame->index, frame->dts,
85                 frame_type_str(frame->flags),
86                 frame_field_str(frame->field),
87                 size, data);
88 }
89
90 static void delta_au_done(struct delta_ctx *ctx, struct delta_au *au, int err)
91 {
92         struct vb2_v4l2_buffer *vbuf;
93
94         vbuf = &au->vbuf;
95         vbuf->sequence = ctx->au_num++;
96         v4l2_m2m_buf_done(vbuf, err ? VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
97 }
98
99 static void delta_frame_done(struct delta_ctx *ctx, struct delta_frame *frame,
100                              int err)
101 {
102         struct vb2_v4l2_buffer *vbuf;
103
104         dump_frame(ctx, frame);
105
106         /* decoded frame is now output to user */
107         frame->state |= DELTA_FRAME_OUT;
108
109         vbuf = &frame->vbuf;
110         vbuf->sequence = ctx->frame_num++;
111         v4l2_m2m_buf_done(vbuf, err ? VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
112
113         if (frame->info.size) /* ignore EOS */
114                 ctx->output_frames++;
115 }
116
117 static void requeue_free_frames(struct delta_ctx *ctx)
118 {
119         struct vb2_v4l2_buffer *vbuf;
120         struct delta_frame *frame;
121         unsigned int i;
122
123         /* requeue all free frames */
124         for (i = 0; i < ctx->nb_of_frames; i++) {
125                 frame = ctx->frames[i];
126                 if (frame->state == DELTA_FRAME_FREE) {
127                         vbuf = &frame->vbuf;
128                         v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
129                         frame->state = DELTA_FRAME_M2M;
130                 }
131         }
132 }
133
134 static int delta_recycle(struct delta_ctx *ctx, struct delta_frame *frame)
135 {
136         const struct delta_dec *dec = ctx->dec;
137
138         /* recycle frame on decoder side */
139         call_dec_op(dec, recycle, ctx, frame);
140
141         /* this frame is no more output */
142         frame->state &= ~DELTA_FRAME_OUT;
143
144         /* requeue free frame */
145         if (frame->state == DELTA_FRAME_FREE) {
146                 struct vb2_v4l2_buffer *vbuf = &frame->vbuf;
147
148                 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
149                 frame->state = DELTA_FRAME_M2M;
150         }
151
152         /* reset other frame fields */
153         frame->flags = 0;
154         frame->dts = 0;
155
156         return 0;
157 }
158
159 static void delta_push_dts(struct delta_ctx *ctx, u64 val)
160 {
161         struct delta_dts *dts;
162
163         dts = kzalloc(sizeof(*dts), GFP_KERNEL);
164         if (!dts)
165                 return;
166
167         INIT_LIST_HEAD(&dts->list);
168
169         /*
170          * protected by global lock acquired
171          * by V4L2 when calling delta_vb2_au_queue
172          */
173         dts->val = val;
174         list_add_tail(&dts->list, &ctx->dts);
175 }
176
177 static void delta_pop_dts(struct delta_ctx *ctx, u64 *val)
178 {
179         struct delta_dev *delta = ctx->dev;
180         struct delta_dts *dts;
181
182         /*
183          * protected by global lock acquired
184          * by V4L2 when calling delta_vb2_au_queue
185          */
186         if (list_empty(&ctx->dts)) {
187                 dev_warn(delta->dev, "%s no dts to pop ... output dts = 0\n",
188                          ctx->name);
189                 *val = 0;
190                 return;
191         }
192
193         dts = list_first_entry(&ctx->dts, struct delta_dts, list);
194         list_del(&dts->list);
195
196         *val = dts->val;
197
198         kfree(dts);
199 }
200
201 static void delta_flush_dts(struct delta_ctx *ctx)
202 {
203         struct delta_dts *dts;
204         struct delta_dts *next;
205
206         /*
207          * protected by global lock acquired
208          * by V4L2 when calling delta_vb2_au_queue
209          */
210
211         /* free all pending dts */
212         list_for_each_entry_safe(dts, next, &ctx->dts, list)
213                 kfree(dts);
214
215         /* reset list */
216         INIT_LIST_HEAD(&ctx->dts);
217 }
218
219 static inline int frame_alignment(u32 fmt)
220 {
221         switch (fmt) {
222         case V4L2_PIX_FMT_NV12:
223         case V4L2_PIX_FMT_NV21:
224                 /* multiple of 2 */
225                 return 2;
226         default:
227                 return 1;
228         }
229 }
230
231 static inline int estimated_au_size(u32 w, u32 h)
232 {
233         /*
234          * for a MJPEG stream encoded from YUV422 pixel format,
235          * assuming a compression ratio of 2, the maximum size
236          * of an access unit is (width x height x 2) / 2,
237          * so (width x height)
238          */
239         return (w * h);
240 }
241
242 static void set_default_params(struct delta_ctx *ctx)
243 {
244         struct delta_frameinfo *frameinfo = &ctx->frameinfo;
245         struct delta_streaminfo *streaminfo = &ctx->streaminfo;
246
247         memset(frameinfo, 0, sizeof(*frameinfo));
248         frameinfo->pixelformat = V4L2_PIX_FMT_NV12;
249         frameinfo->width = DELTA_DEFAULT_WIDTH;
250         frameinfo->height = DELTA_DEFAULT_HEIGHT;
251         frameinfo->aligned_width = ALIGN(frameinfo->width,
252                                          DELTA_WIDTH_ALIGNMENT);
253         frameinfo->aligned_height = ALIGN(frameinfo->height,
254                                           DELTA_HEIGHT_ALIGNMENT);
255         frameinfo->size = frame_size(frameinfo->aligned_width,
256                                      frameinfo->aligned_height,
257                                      frameinfo->pixelformat);
258         frameinfo->field = V4L2_FIELD_NONE;
259         frameinfo->colorspace = V4L2_COLORSPACE_REC709;
260         frameinfo->xfer_func = V4L2_XFER_FUNC_DEFAULT;
261         frameinfo->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
262         frameinfo->quantization = V4L2_QUANTIZATION_DEFAULT;
263
264         memset(streaminfo, 0, sizeof(*streaminfo));
265         streaminfo->streamformat = DELTA_DEFAULT_STREAMFORMAT;
266         streaminfo->width = DELTA_DEFAULT_WIDTH;
267         streaminfo->height = DELTA_DEFAULT_HEIGHT;
268         streaminfo->field = V4L2_FIELD_NONE;
269         streaminfo->colorspace = V4L2_COLORSPACE_REC709;
270         streaminfo->xfer_func = V4L2_XFER_FUNC_DEFAULT;
271         streaminfo->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
272         streaminfo->quantization = V4L2_QUANTIZATION_DEFAULT;
273
274         ctx->max_au_size = estimated_au_size(streaminfo->width,
275                                              streaminfo->height);
276 }
277
278 static const struct delta_dec *delta_find_decoder(struct delta_ctx *ctx,
279                                                   u32 streamformat,
280                                                   u32 pixelformat)
281 {
282         struct delta_dev *delta = ctx->dev;
283         const struct delta_dec *dec;
284         unsigned int i;
285
286         for (i = 0; i < delta->nb_of_decoders; i++) {
287                 dec = delta->decoders[i];
288                 if ((dec->pixelformat == pixelformat) &&
289                     (dec->streamformat == streamformat))
290                         return dec;
291         }
292
293         return NULL;
294 }
295
296 static void register_format(u32 format, u32 formats[], u32 *nb_of_formats)
297 {
298         u32 i;
299
300         for (i = 0; i < *nb_of_formats; i++) {
301                 if (format == formats[i])
302                         return;
303         }
304
305         formats[(*nb_of_formats)++] = format;
306 }
307
308 static void register_formats(struct delta_dev *delta)
309 {
310         unsigned int i;
311
312         for (i = 0; i < delta->nb_of_decoders; i++) {
313                 register_format(delta->decoders[i]->pixelformat,
314                                 delta->pixelformats,
315                                 &delta->nb_of_pixelformats);
316
317                 register_format(delta->decoders[i]->streamformat,
318                                 delta->streamformats,
319                                 &delta->nb_of_streamformats);
320         }
321 }
322
323 static void register_decoders(struct delta_dev *delta)
324 {
325         unsigned int i;
326
327         for (i = 0; i < ARRAY_SIZE(delta_decoders); i++) {
328                 if (delta->nb_of_decoders >= DELTA_MAX_DECODERS) {
329                         dev_dbg(delta->dev,
330                                 "%s failed to register %s decoder (%d maximum reached)\n",
331                                 DELTA_PREFIX, delta_decoders[i]->name,
332                                 DELTA_MAX_DECODERS);
333                         return;
334                 }
335
336                 delta->decoders[delta->nb_of_decoders++] = delta_decoders[i];
337                 dev_info(delta->dev, "%s %s decoder registered\n",
338                          DELTA_PREFIX, delta_decoders[i]->name);
339         }
340 }
341
342 static int delta_open_decoder(struct delta_ctx *ctx, u32 streamformat,
343                               u32 pixelformat, const struct delta_dec **pdec)
344 {
345         struct delta_dev *delta = ctx->dev;
346         const struct delta_dec *dec;
347         int ret;
348
349         dec = delta_find_decoder(ctx, streamformat, ctx->frameinfo.pixelformat);
350         if (!dec) {
351                 dev_err(delta->dev, "%s no decoder found matching %4.4s => %4.4s\n",
352                         ctx->name, (char *)&streamformat, (char *)&pixelformat);
353                 return -EINVAL;
354         }
355
356         dev_dbg(delta->dev, "%s one decoder matching %4.4s => %4.4s\n",
357                 ctx->name, (char *)&streamformat, (char *)&pixelformat);
358
359         /* update instance name */
360         snprintf(ctx->name, sizeof(ctx->name), "[%3d:%4.4s]",
361                  delta->instance_id, (char *)&streamformat);
362
363         /* open decoder instance */
364         ret = call_dec_op(dec, open, ctx);
365         if (ret) {
366                 dev_err(delta->dev, "%s failed to open decoder instance (%d)\n",
367                         ctx->name, ret);
368                 return ret;
369         }
370
371         dev_dbg(delta->dev, "%s %s decoder opened\n", ctx->name, dec->name);
372
373         *pdec = dec;
374
375         return ret;
376 }
377
378 /*
379  * V4L2 ioctl operations
380  */
381
382 static int delta_querycap(struct file *file, void *priv,
383                           struct v4l2_capability *cap)
384 {
385         struct delta_ctx *ctx = to_ctx(file->private_data);
386         struct delta_dev *delta = ctx->dev;
387
388         strlcpy(cap->driver, DELTA_NAME, sizeof(cap->driver));
389         strlcpy(cap->card, delta->vdev->name, sizeof(cap->card));
390         snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
391                  delta->pdev->name);
392
393         return 0;
394 }
395
396 static int delta_enum_fmt_stream(struct file *file, void *priv,
397                                  struct v4l2_fmtdesc *f)
398 {
399         struct delta_ctx *ctx = to_ctx(file->private_data);
400         struct delta_dev *delta = ctx->dev;
401
402         if (unlikely(f->index >= delta->nb_of_streamformats))
403                 return -EINVAL;
404
405         f->pixelformat = delta->streamformats[f->index];
406
407         return 0;
408 }
409
410 static int delta_enum_fmt_frame(struct file *file, void *priv,
411                                 struct v4l2_fmtdesc *f)
412 {
413         struct delta_ctx *ctx = to_ctx(file->private_data);
414         struct delta_dev *delta = ctx->dev;
415
416         if (unlikely(f->index >= delta->nb_of_pixelformats))
417                 return -EINVAL;
418
419         f->pixelformat = delta->pixelformats[f->index];
420
421         return 0;
422 }
423
424 static int delta_g_fmt_stream(struct file *file, void *fh,
425                               struct v4l2_format *f)
426 {
427         struct delta_ctx *ctx = to_ctx(file->private_data);
428         struct delta_dev *delta = ctx->dev;
429         struct v4l2_pix_format *pix = &f->fmt.pix;
430         struct delta_streaminfo *streaminfo = &ctx->streaminfo;
431         unsigned char str[100] = "";
432
433         if (!(ctx->flags & DELTA_FLAG_STREAMINFO))
434                 dev_dbg(delta->dev,
435                         "%s V4L2 GET_FMT (OUTPUT): no stream information available, default to %s\n",
436                         ctx->name,
437                         delta_streaminfo_str(streaminfo, str, sizeof(str)));
438
439         pix->pixelformat = streaminfo->streamformat;
440         pix->width = streaminfo->width;
441         pix->height = streaminfo->height;
442         pix->field = streaminfo->field;
443         pix->bytesperline = 0;
444         pix->sizeimage = ctx->max_au_size;
445         pix->colorspace = streaminfo->colorspace;
446         pix->xfer_func = streaminfo->xfer_func;
447         pix->ycbcr_enc = streaminfo->ycbcr_enc;
448         pix->quantization = streaminfo->quantization;
449
450         return 0;
451 }
452
453 static int delta_g_fmt_frame(struct file *file, void *fh, struct v4l2_format *f)
454 {
455         struct delta_ctx *ctx = to_ctx(file->private_data);
456         struct delta_dev *delta = ctx->dev;
457         struct v4l2_pix_format *pix = &f->fmt.pix;
458         struct delta_frameinfo *frameinfo = &ctx->frameinfo;
459         struct delta_streaminfo *streaminfo = &ctx->streaminfo;
460         unsigned char str[100] = "";
461
462         if (!(ctx->flags & DELTA_FLAG_FRAMEINFO))
463                 dev_dbg(delta->dev,
464                         "%s V4L2 GET_FMT (CAPTURE): no frame information available, default to %s\n",
465                         ctx->name,
466                         delta_frameinfo_str(frameinfo, str, sizeof(str)));
467
468         pix->pixelformat = frameinfo->pixelformat;
469         pix->width = frameinfo->aligned_width;
470         pix->height = frameinfo->aligned_height;
471         pix->field = frameinfo->field;
472         pix->bytesperline = frame_stride(frameinfo->aligned_width,
473                                                frameinfo->pixelformat);
474         pix->sizeimage = frameinfo->size;
475
476         if (ctx->flags & DELTA_FLAG_STREAMINFO) {
477                 /* align colorspace & friends on stream ones if any set */
478                 frameinfo->colorspace = streaminfo->colorspace;
479                 frameinfo->xfer_func = streaminfo->xfer_func;
480                 frameinfo->ycbcr_enc = streaminfo->ycbcr_enc;
481                 frameinfo->quantization = streaminfo->quantization;
482         }
483         pix->colorspace = frameinfo->colorspace;
484         pix->xfer_func = frameinfo->xfer_func;
485         pix->ycbcr_enc = frameinfo->ycbcr_enc;
486         pix->quantization = frameinfo->quantization;
487
488         return 0;
489 }
490
491 static int delta_try_fmt_stream(struct file *file, void *priv,
492                                 struct v4l2_format *f)
493 {
494         struct delta_ctx *ctx = to_ctx(file->private_data);
495         struct delta_dev *delta = ctx->dev;
496         struct v4l2_pix_format *pix = &f->fmt.pix;
497         u32 streamformat = pix->pixelformat;
498         const struct delta_dec *dec;
499         u32 width, height;
500         u32 au_size;
501
502         dec = delta_find_decoder(ctx, streamformat, ctx->frameinfo.pixelformat);
503         if (!dec) {
504                 dev_dbg(delta->dev,
505                         "%s V4L2 TRY_FMT (OUTPUT): unsupported format %4.4s\n",
506                         ctx->name, (char *)&pix->pixelformat);
507                 return -EINVAL;
508         }
509
510         /* adjust width & height */
511         width = pix->width;
512         height = pix->height;
513         v4l_bound_align_image
514                 (&pix->width,
515                  DELTA_MIN_WIDTH,
516                  dec->max_width ? dec->max_width : DELTA_MAX_WIDTH,
517                  0,
518                  &pix->height,
519                  DELTA_MIN_HEIGHT,
520                  dec->max_height ? dec->max_height : DELTA_MAX_HEIGHT,
521                  0, 0);
522
523         if ((pix->width != width) || (pix->height != height))
524                 dev_dbg(delta->dev,
525                         "%s V4L2 TRY_FMT (OUTPUT): resolution updated %dx%d -> %dx%d to fit min/max/alignment\n",
526                         ctx->name, width, height,
527                         pix->width, pix->height);
528
529         au_size = estimated_au_size(pix->width, pix->height);
530         if (pix->sizeimage < au_size) {
531                 dev_dbg(delta->dev,
532                         "%s V4L2 TRY_FMT (OUTPUT): size updated %d -> %d to fit estimated size\n",
533                         ctx->name, pix->sizeimage, au_size);
534                 pix->sizeimage = au_size;
535         }
536
537         pix->bytesperline = 0;
538
539         if (pix->field == V4L2_FIELD_ANY)
540                 pix->field = V4L2_FIELD_NONE;
541
542         return 0;
543 }
544
545 static int delta_try_fmt_frame(struct file *file, void *priv,
546                                struct v4l2_format *f)
547 {
548         struct delta_ctx *ctx = to_ctx(file->private_data);
549         struct delta_dev *delta = ctx->dev;
550         struct v4l2_pix_format *pix = &f->fmt.pix;
551         u32 pixelformat = pix->pixelformat;
552         const struct delta_dec *dec;
553         u32 width, height;
554
555         dec = delta_find_decoder(ctx, ctx->streaminfo.streamformat,
556                                  pixelformat);
557         if (!dec) {
558                 dev_dbg(delta->dev,
559                         "%s V4L2 TRY_FMT (CAPTURE): unsupported format %4.4s\n",
560                         ctx->name, (char *)&pixelformat);
561                 return -EINVAL;
562         }
563
564         /* adjust width & height */
565         width = pix->width;
566         height = pix->height;
567         v4l_bound_align_image(&pix->width,
568                               DELTA_MIN_WIDTH, DELTA_MAX_WIDTH,
569                               frame_alignment(pixelformat) - 1,
570                               &pix->height,
571                               DELTA_MIN_HEIGHT, DELTA_MAX_HEIGHT,
572                               frame_alignment(pixelformat) - 1, 0);
573
574         if ((pix->width != width) || (pix->height != height))
575                 dev_dbg(delta->dev,
576                         "%s V4L2 TRY_FMT (CAPTURE): resolution updated %dx%d -> %dx%d to fit min/max/alignment\n",
577                         ctx->name, width, height, pix->width, pix->height);
578
579         /* default decoder alignment constraint */
580         width = ALIGN(pix->width, DELTA_WIDTH_ALIGNMENT);
581         height = ALIGN(pix->height, DELTA_HEIGHT_ALIGNMENT);
582         if ((pix->width != width) || (pix->height != height))
583                 dev_dbg(delta->dev,
584                         "%s V4L2 TRY_FMT (CAPTURE): resolution updated %dx%d -> %dx%d to fit decoder alignment\n",
585                         ctx->name, width, height, pix->width, pix->height);
586
587         if (!pix->colorspace) {
588                 pix->colorspace = V4L2_COLORSPACE_REC709;
589                 pix->xfer_func = V4L2_XFER_FUNC_DEFAULT;
590                 pix->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
591                 pix->quantization = V4L2_QUANTIZATION_DEFAULT;
592         }
593
594         pix->width = width;
595         pix->height = height;
596         pix->bytesperline = frame_stride(pix->width, pixelformat);
597         pix->sizeimage = frame_size(pix->width, pix->height, pixelformat);
598
599         if (pix->field == V4L2_FIELD_ANY)
600                 pix->field = V4L2_FIELD_NONE;
601
602         return 0;
603 }
604
605 static int delta_s_fmt_stream(struct file *file, void *fh,
606                               struct v4l2_format *f)
607 {
608         struct delta_ctx *ctx = to_ctx(file->private_data);
609         struct delta_dev *delta = ctx->dev;
610         struct vb2_queue *vq;
611         struct v4l2_pix_format *pix = &f->fmt.pix;
612         int ret;
613
614         ret = delta_try_fmt_stream(file, fh, f);
615         if (ret) {
616                 dev_dbg(delta->dev,
617                         "%s V4L2 S_FMT (OUTPUT): unsupported format %4.4s\n",
618                         ctx->name, (char *)&pix->pixelformat);
619                 return ret;
620         }
621
622         vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
623         if (vb2_is_streaming(vq)) {
624                 dev_dbg(delta->dev, "%s V4L2 S_FMT (OUTPUT): queue busy\n",
625                         ctx->name);
626                 return -EBUSY;
627         }
628
629         ctx->max_au_size = pix->sizeimage;
630         ctx->streaminfo.width = pix->width;
631         ctx->streaminfo.height = pix->height;
632         ctx->streaminfo.streamformat = pix->pixelformat;
633         ctx->streaminfo.colorspace = pix->colorspace;
634         ctx->streaminfo.xfer_func = pix->xfer_func;
635         ctx->streaminfo.ycbcr_enc = pix->ycbcr_enc;
636         ctx->streaminfo.quantization = pix->quantization;
637         ctx->flags |= DELTA_FLAG_STREAMINFO;
638
639         return 0;
640 }
641
642 static int delta_s_fmt_frame(struct file *file, void *fh, struct v4l2_format *f)
643 {
644         struct delta_ctx *ctx = to_ctx(file->private_data);
645         struct delta_dev *delta = ctx->dev;
646         const struct delta_dec *dec = ctx->dec;
647         struct v4l2_pix_format *pix = &f->fmt.pix;
648         struct delta_frameinfo frameinfo;
649         unsigned char str[100] = "";
650         struct vb2_queue *vq;
651         int ret;
652
653         vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
654         if (vb2_is_streaming(vq)) {
655                 dev_dbg(delta->dev, "%s V4L2 S_FMT (CAPTURE): queue busy\n",
656                         ctx->name);
657                 return -EBUSY;
658         }
659
660         if (ctx->state < DELTA_STATE_READY) {
661                 /*
662                  * decoder not yet opened and valid stream header not found,
663                  * could not negotiate format with decoder, check at least
664                  * pixel format & negotiate resolution boundaries
665                  * and alignment...
666                  */
667                 ret = delta_try_fmt_frame(file, fh, f);
668                 if (ret) {
669                         dev_dbg(delta->dev,
670                                 "%s V4L2 S_FMT (CAPTURE): unsupported format %4.4s\n",
671                                 ctx->name, (char *)&pix->pixelformat);
672                         return ret;
673                 }
674
675                 return 0;
676         }
677
678         /* set frame information to decoder */
679         memset(&frameinfo, 0, sizeof(frameinfo));
680         frameinfo.pixelformat = pix->pixelformat;
681         frameinfo.width = pix->width;
682         frameinfo.height = pix->height;
683         frameinfo.aligned_width = pix->width;
684         frameinfo.aligned_height = pix->height;
685         frameinfo.size = pix->sizeimage;
686         frameinfo.field = pix->field;
687         frameinfo.colorspace = pix->colorspace;
688         frameinfo.xfer_func = pix->xfer_func;
689         frameinfo.ycbcr_enc = pix->ycbcr_enc;
690         frameinfo.quantization = pix->quantization;
691         ret = call_dec_op(dec, set_frameinfo, ctx, &frameinfo);
692         if (ret)
693                 return ret;
694
695         /* then get what decoder can really do */
696         ret = call_dec_op(dec, get_frameinfo, ctx, &frameinfo);
697         if (ret)
698                 return ret;
699
700         ctx->flags |= DELTA_FLAG_FRAMEINFO;
701         ctx->frameinfo = frameinfo;
702         dev_dbg(delta->dev,
703                 "%s V4L2 SET_FMT (CAPTURE): frameinfo updated to %s\n",
704                 ctx->name,
705                 delta_frameinfo_str(&frameinfo, str, sizeof(str)));
706
707         pix->pixelformat = frameinfo.pixelformat;
708         pix->width = frameinfo.aligned_width;
709         pix->height = frameinfo.aligned_height;
710         pix->bytesperline = frame_stride(pix->width, pix->pixelformat);
711         pix->sizeimage = frameinfo.size;
712         pix->field = frameinfo.field;
713         pix->colorspace = frameinfo.colorspace;
714         pix->xfer_func = frameinfo.xfer_func;
715         pix->ycbcr_enc = frameinfo.ycbcr_enc;
716         pix->quantization = frameinfo.quantization;
717
718         return 0;
719 }
720
721 static int delta_g_selection(struct file *file, void *fh,
722                              struct v4l2_selection *s)
723 {
724         struct delta_ctx *ctx = to_ctx(fh);
725         struct delta_frameinfo *frameinfo = &ctx->frameinfo;
726         struct v4l2_rect crop;
727
728         if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
729                 return -EINVAL;
730
731         if ((ctx->flags & DELTA_FLAG_FRAMEINFO) &&
732             (frameinfo->flags & DELTA_FRAMEINFO_FLAG_CROP)) {
733                 crop = frameinfo->crop;
734         } else {
735                 /* default to video dimensions */
736                 crop.left = 0;
737                 crop.top = 0;
738                 crop.width = frameinfo->width;
739                 crop.height = frameinfo->height;
740         }
741
742         switch (s->target) {
743         case V4L2_SEL_TGT_COMPOSE:
744         case V4L2_SEL_TGT_COMPOSE_DEFAULT:
745                 /* visible area inside video */
746                 s->r = crop;
747                 break;
748         case V4L2_SEL_TGT_COMPOSE_PADDED:
749         case V4L2_SEL_TGT_COMPOSE_BOUNDS:
750                 /* up to aligned dimensions */
751                 s->r.left = 0;
752                 s->r.top = 0;
753                 s->r.width = frameinfo->aligned_width;
754                 s->r.height = frameinfo->aligned_height;
755                 break;
756         default:
757                 return -EINVAL;
758         }
759
760         return 0;
761 }
762
763 static void delta_complete_eos(struct delta_ctx *ctx,
764                                struct delta_frame *frame)
765 {
766         struct delta_dev *delta = ctx->dev;
767         const struct v4l2_event ev = {.type = V4L2_EVENT_EOS};
768
769         /*
770          * Send EOS to user:
771          * - by returning an empty frame flagged to V4L2_BUF_FLAG_LAST
772          * - and then send EOS event
773          */
774
775         /* empty frame */
776         frame->info.size = 0;
777
778         /* set the last buffer flag */
779         frame->flags |= V4L2_BUF_FLAG_LAST;
780
781         /* release frame to user */
782         delta_frame_done(ctx, frame, 0);
783
784         /* send EOS event */
785         v4l2_event_queue_fh(&ctx->fh, &ev);
786
787         dev_dbg(delta->dev, "%s EOS completed\n", ctx->name);
788 }
789
790 static int delta_try_decoder_cmd(struct file *file, void *fh,
791                                  struct v4l2_decoder_cmd *cmd)
792 {
793         if (cmd->cmd != V4L2_DEC_CMD_STOP)
794                 return -EINVAL;
795
796         if (cmd->flags & V4L2_DEC_CMD_STOP_TO_BLACK)
797                 return -EINVAL;
798
799         if (!(cmd->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) &&
800             (cmd->stop.pts != 0))
801                 return -EINVAL;
802
803         return 0;
804 }
805
806 static int delta_decoder_stop_cmd(struct delta_ctx *ctx, void *fh)
807 {
808         const struct delta_dec *dec = ctx->dec;
809         struct delta_dev *delta = ctx->dev;
810         struct delta_frame *frame = NULL;
811         int ret = 0;
812
813         dev_dbg(delta->dev, "%s EOS received\n", ctx->name);
814
815         if (ctx->state != DELTA_STATE_READY)
816                 return 0;
817
818         /* drain the decoder */
819         call_dec_op(dec, drain, ctx);
820
821         /* release to user drained frames */
822         while (1) {
823                 frame = NULL;
824                 ret = call_dec_op(dec, get_frame, ctx, &frame);
825                 if (ret == -ENODATA) {
826                         /* no more decoded frames */
827                         break;
828                 }
829                 if (frame) {
830                         dev_dbg(delta->dev, "%s drain frame[%d]\n",
831                                 ctx->name, frame->index);
832
833                         /* pop timestamp and mark frame with it */
834                         delta_pop_dts(ctx, &frame->dts);
835
836                         /* release decoded frame to user */
837                         delta_frame_done(ctx, frame, 0);
838                 }
839         }
840
841         /* try to complete EOS */
842         ret = delta_get_free_frame(ctx, &frame);
843         if (ret)
844                 goto delay_eos;
845
846         /* new frame available, EOS can now be completed */
847         delta_complete_eos(ctx, frame);
848
849         ctx->state = DELTA_STATE_EOS;
850
851         return 0;
852
853 delay_eos:
854         /*
855          * EOS completion from driver is delayed because
856          * we don't have a free empty frame available.
857          * EOS completion is so delayed till next frame_queue() call
858          * to be sure to have a free empty frame available.
859          */
860         ctx->state = DELTA_STATE_WF_EOS;
861         dev_dbg(delta->dev, "%s EOS delayed\n", ctx->name);
862
863         return 0;
864 }
865
866 static int delta_decoder_cmd(struct file *file, void *fh,
867                              struct v4l2_decoder_cmd *cmd)
868 {
869         struct delta_ctx *ctx = to_ctx(fh);
870         int ret = 0;
871
872         ret = delta_try_decoder_cmd(file, fh, cmd);
873         if (ret)
874                 return ret;
875
876         return delta_decoder_stop_cmd(ctx, fh);
877 }
878
879 static int delta_subscribe_event(struct v4l2_fh *fh,
880                                  const struct v4l2_event_subscription *sub)
881 {
882         switch (sub->type) {
883         case V4L2_EVENT_EOS:
884                 return v4l2_event_subscribe(fh, sub, 2, NULL);
885         default:
886                 return -EINVAL;
887         }
888
889         return 0;
890 }
891
892 /* v4l2 ioctl ops */
893 static const struct v4l2_ioctl_ops delta_ioctl_ops = {
894         .vidioc_querycap = delta_querycap,
895         .vidioc_enum_fmt_vid_cap = delta_enum_fmt_frame,
896         .vidioc_g_fmt_vid_cap = delta_g_fmt_frame,
897         .vidioc_try_fmt_vid_cap = delta_try_fmt_frame,
898         .vidioc_s_fmt_vid_cap = delta_s_fmt_frame,
899         .vidioc_enum_fmt_vid_out = delta_enum_fmt_stream,
900         .vidioc_g_fmt_vid_out = delta_g_fmt_stream,
901         .vidioc_try_fmt_vid_out = delta_try_fmt_stream,
902         .vidioc_s_fmt_vid_out = delta_s_fmt_stream,
903         .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
904         .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
905         .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
906         .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
907         .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
908         .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
909         .vidioc_streamon = v4l2_m2m_ioctl_streamon,
910         .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
911         .vidioc_g_selection = delta_g_selection,
912         .vidioc_try_decoder_cmd = delta_try_decoder_cmd,
913         .vidioc_decoder_cmd = delta_decoder_cmd,
914         .vidioc_subscribe_event = delta_subscribe_event,
915         .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
916 };
917
918 /*
919  * mem-to-mem operations
920  */
921
922 static void delta_run_work(struct work_struct *work)
923 {
924         struct delta_ctx *ctx = container_of(work, struct delta_ctx, run_work);
925         struct delta_dev *delta = ctx->dev;
926         const struct delta_dec *dec = ctx->dec;
927         struct delta_au *au;
928         struct delta_frame *frame = NULL;
929         int ret = 0;
930         bool discard = false;
931         struct vb2_v4l2_buffer *vbuf;
932
933         if (!dec) {
934                 dev_err(delta->dev, "%s no decoder opened yet\n", ctx->name);
935                 return;
936         }
937
938         /* protect instance against reentrancy */
939         mutex_lock(&ctx->lock);
940
941         vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
942         if (!vbuf) {
943                 dev_err(delta->dev, "%s no buffer to decode\n", ctx->name);
944                 mutex_unlock(&ctx->lock);
945                 return;
946         }
947         au = to_au(vbuf);
948         au->size = vb2_get_plane_payload(&vbuf->vb2_buf, 0);
949         au->dts = vbuf->vb2_buf.timestamp;
950
951         /* dump access unit */
952         dump_au(ctx, au);
953
954         /* enable the hardware */
955         if (!dec->pm) {
956                 ret = delta_get_sync(ctx);
957                 if (ret) {
958                         delta_put_autosuspend(ctx);
959                         goto err;
960                 }
961         }
962
963         /* decode this access unit */
964         ret = call_dec_op(dec, decode, ctx, au);
965
966         /*
967          * if the (-ENODATA) value is returned, it refers to the interlaced
968          * stream case for which 2 access units are needed to get 1 frame.
969          * So, this returned value doesn't mean that the decoding fails, but
970          * indicates that the timestamp information of the access unit shall
971          * not be taken into account, and that the V4L2 buffer associated with
972          * the access unit shall be flagged with V4L2_BUF_FLAG_ERROR to inform
973          * the user of this situation
974          */
975         if (ret == -ENODATA) {
976                 discard = true;
977         } else if (ret) {
978                 dev_err(delta->dev, "%s decoding failed (%d)\n",
979                         ctx->name, ret);
980
981                 /* disable the hardware */
982                 if (!dec->pm)
983                         delta_put_autosuspend(ctx);
984
985                 goto err;
986         }
987
988         /* disable the hardware */
989         if (!dec->pm)
990                 delta_put_autosuspend(ctx);
991
992         /* push au timestamp in FIFO */
993         if (!discard)
994                 delta_push_dts(ctx, au->dts);
995
996         /* get available decoded frames */
997         while (1) {
998                 ret = call_dec_op(dec, get_frame, ctx, &frame);
999                 if (ret == -ENODATA) {
1000                         /* no more decoded frames */
1001                         goto out;
1002                 }
1003                 if (ret) {
1004                         dev_err(delta->dev, "%s  cannot get decoded frame (%d)\n",
1005                                 ctx->name, ret);
1006                         goto out;
1007                 }
1008                 if (!frame) {
1009                         dev_err(delta->dev,
1010                                 "%s  NULL decoded frame\n",
1011                                 ctx->name);
1012                         ret = -EIO;
1013                         goto out;
1014                 }
1015
1016                 /* pop timestamp and mark frame with it */
1017                 delta_pop_dts(ctx, &frame->dts);
1018
1019                 /* release decoded frame to user */
1020                 delta_frame_done(ctx, frame, 0);
1021         }
1022
1023 out:
1024         requeue_free_frames(ctx);
1025         delta_au_done(ctx, au, (discard ? -ENODATA : 0));
1026         mutex_unlock(&ctx->lock);
1027         v4l2_m2m_job_finish(delta->m2m_dev, ctx->fh.m2m_ctx);
1028         return;
1029
1030 err:
1031         requeue_free_frames(ctx);
1032         delta_au_done(ctx, au, ret);
1033         mutex_unlock(&ctx->lock);
1034         v4l2_m2m_job_finish(delta->m2m_dev, ctx->fh.m2m_ctx);
1035 }
1036
1037 static void delta_device_run(void *priv)
1038 {
1039         struct delta_ctx *ctx = priv;
1040         struct delta_dev *delta = ctx->dev;
1041
1042         queue_work(delta->work_queue, &ctx->run_work);
1043 }
1044
1045 static void delta_job_abort(void *priv)
1046 {
1047         struct delta_ctx *ctx = priv;
1048         struct delta_dev *delta = ctx->dev;
1049
1050         dev_dbg(delta->dev, "%s aborting job\n", ctx->name);
1051
1052         ctx->aborting = true;
1053 }
1054
1055 static int delta_job_ready(void *priv)
1056 {
1057         struct delta_ctx *ctx = priv;
1058         struct delta_dev *delta = ctx->dev;
1059         int src_bufs = v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx);
1060
1061         if (!src_bufs) {
1062                 dev_dbg(delta->dev, "%s not ready: not enough video buffers.\n",
1063                         ctx->name);
1064                 return 0;
1065         }
1066
1067         if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) {
1068                 dev_dbg(delta->dev, "%s not ready: not enough video capture buffers.\n",
1069                         ctx->name);
1070                 return 0;
1071         }
1072
1073         if (ctx->aborting) {
1074                 dev_dbg(delta->dev, "%s job not ready: aborting\n", ctx->name);
1075                 return 0;
1076         }
1077
1078         dev_dbg(delta->dev, "%s job ready\n", ctx->name);
1079
1080         return 1;
1081 }
1082
1083 /* mem-to-mem ops */
1084 static const struct v4l2_m2m_ops delta_m2m_ops = {
1085         .device_run     = delta_device_run,
1086         .job_ready      = delta_job_ready,
1087         .job_abort      = delta_job_abort,
1088 };
1089
1090 /*
1091  * VB2 queue operations
1092  */
1093
1094 static int delta_vb2_au_queue_setup(struct vb2_queue *vq,
1095                                     unsigned int *num_buffers,
1096                                     unsigned int *num_planes,
1097                                     unsigned int sizes[],
1098                                     struct device *alloc_devs[])
1099 {
1100         struct delta_ctx *ctx = vb2_get_drv_priv(vq);
1101         unsigned int size = ctx->max_au_size;
1102
1103         if (*num_planes)
1104                 return sizes[0] < size ? -EINVAL : 0;
1105
1106         *num_planes = 1;
1107         if (*num_buffers < 1)
1108                 *num_buffers = 1;
1109         if (*num_buffers > DELTA_MAX_AUS)
1110                 *num_buffers = DELTA_MAX_AUS;
1111
1112         sizes[0] = size;
1113
1114         return 0;
1115 }
1116
1117 static int delta_vb2_au_prepare(struct vb2_buffer *vb)
1118 {
1119         struct vb2_queue *q = vb->vb2_queue;
1120         struct delta_ctx *ctx = vb2_get_drv_priv(q);
1121         struct delta_dev *delta = ctx->dev;
1122         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1123         struct delta_au *au = to_au(vbuf);
1124
1125         if (!au->prepared) {
1126                 /* get memory addresses */
1127                 au->vaddr = vb2_plane_vaddr(&au->vbuf.vb2_buf, 0);
1128                 au->paddr = vb2_dma_contig_plane_dma_addr
1129                                 (&au->vbuf.vb2_buf, 0);
1130                 au->prepared = true;
1131                 dev_dbg(delta->dev, "%s au[%d] prepared; virt=0x%p, phy=0x%pad\n",
1132                         ctx->name, vb->index, au->vaddr, &au->paddr);
1133         }
1134
1135         if (vbuf->field == V4L2_FIELD_ANY)
1136                 vbuf->field = V4L2_FIELD_NONE;
1137
1138         return 0;
1139 }
1140
1141 static int delta_setup_frame(struct delta_ctx *ctx,
1142                              struct delta_frame *frame)
1143 {
1144         struct delta_dev *delta = ctx->dev;
1145         const struct delta_dec *dec = ctx->dec;
1146
1147         if (frame->index >= DELTA_MAX_FRAMES) {
1148                 dev_err(delta->dev,
1149                         "%s frame index=%d exceeds output frame count (%d)\n",
1150                         ctx->name, frame->index, DELTA_MAX_FRAMES);
1151                 return -EINVAL;
1152         }
1153
1154         if (ctx->nb_of_frames >= DELTA_MAX_FRAMES) {
1155                 dev_err(delta->dev,
1156                         "%s number of frames exceeds output frame count (%d > %d)\n",
1157                         ctx->name, ctx->nb_of_frames, DELTA_MAX_FRAMES);
1158                 return -EINVAL;
1159         }
1160
1161         if (frame->index != ctx->nb_of_frames) {
1162                 dev_warn(delta->dev,
1163                          "%s frame index discontinuity detected, expected %d, got %d\n",
1164                          ctx->name, ctx->nb_of_frames, frame->index);
1165         }
1166
1167         frame->state = DELTA_FRAME_FREE;
1168         ctx->frames[ctx->nb_of_frames] = frame;
1169         ctx->nb_of_frames++;
1170
1171         /* setup frame on decoder side */
1172         return call_dec_op(dec, setup_frame, ctx, frame);
1173 }
1174
1175 /*
1176  * default implementation of get_frameinfo decoder ops
1177  * matching frame information from stream information
1178  * & with default pixel format & default alignment.
1179  */
1180 int delta_get_frameinfo_default(struct delta_ctx *ctx,
1181                                 struct delta_frameinfo *frameinfo)
1182 {
1183         struct delta_streaminfo *streaminfo = &ctx->streaminfo;
1184
1185         memset(frameinfo, 0, sizeof(*frameinfo));
1186         frameinfo->pixelformat = V4L2_PIX_FMT_NV12;
1187         frameinfo->width = streaminfo->width;
1188         frameinfo->height = streaminfo->height;
1189         frameinfo->aligned_width = ALIGN(streaminfo->width,
1190                                          DELTA_WIDTH_ALIGNMENT);
1191         frameinfo->aligned_height = ALIGN(streaminfo->height,
1192                                           DELTA_HEIGHT_ALIGNMENT);
1193         frameinfo->size = frame_size(frameinfo->aligned_width,
1194                                      frameinfo->aligned_height,
1195                                      frameinfo->pixelformat);
1196         if (streaminfo->flags & DELTA_STREAMINFO_FLAG_CROP) {
1197                 frameinfo->flags |= DELTA_FRAMEINFO_FLAG_CROP;
1198                 frameinfo->crop = streaminfo->crop;
1199         }
1200         if (streaminfo->flags & DELTA_STREAMINFO_FLAG_PIXELASPECT) {
1201                 frameinfo->flags |= DELTA_FRAMEINFO_FLAG_PIXELASPECT;
1202                 frameinfo->pixelaspect = streaminfo->pixelaspect;
1203         }
1204         frameinfo->field = streaminfo->field;
1205
1206         return 0;
1207 }
1208
1209 /*
1210  * default implementation of recycle decoder ops
1211  * consisting to relax the "decoded" frame state
1212  */
1213 int delta_recycle_default(struct delta_ctx *pctx,
1214                           struct delta_frame *frame)
1215 {
1216         frame->state &= ~DELTA_FRAME_DEC;
1217
1218         return 0;
1219 }
1220
1221 static void dump_frames_status(struct delta_ctx *ctx)
1222 {
1223         struct delta_dev *delta = ctx->dev;
1224         unsigned int i;
1225         struct delta_frame *frame;
1226         unsigned char str[100] = "";
1227
1228         dev_info(delta->dev,
1229                  "%s dumping frames status...\n", ctx->name);
1230
1231         for (i = 0; i < ctx->nb_of_frames; i++) {
1232                 frame = ctx->frames[i];
1233                 dev_info(delta->dev,
1234                          "%s frame[%d] %s\n",
1235                          ctx->name, frame->index,
1236                          frame_state_str(frame->state,
1237                                          str, sizeof(str)));
1238         }
1239 }
1240
1241 int delta_get_free_frame(struct delta_ctx *ctx,
1242                          struct delta_frame **pframe)
1243 {
1244         struct delta_dev *delta = ctx->dev;
1245         struct vb2_v4l2_buffer *vbuf;
1246         struct delta_frame *frame;
1247
1248         *pframe = NULL;
1249
1250         vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
1251         if (!vbuf) {
1252                 dev_err(delta->dev, "%s no frame available",
1253                         ctx->name);
1254                 return -EIO;
1255         }
1256
1257         frame = to_frame(vbuf);
1258         frame->state &= ~DELTA_FRAME_M2M;
1259         if (frame->state != DELTA_FRAME_FREE) {
1260                 dev_err(delta->dev,
1261                         "%s frame[%d] is not free\n",
1262                         ctx->name, frame->index);
1263                 dump_frames_status(ctx);
1264                 return -ENODATA;
1265         }
1266
1267         dev_dbg(delta->dev,
1268                 "%s get free frame[%d]\n", ctx->name, frame->index);
1269
1270         *pframe = frame;
1271         return 0;
1272 }
1273
1274 int delta_get_sync(struct delta_ctx *ctx)
1275 {
1276         struct delta_dev *delta = ctx->dev;
1277         int ret = 0;
1278
1279         /* enable the hardware */
1280         ret = pm_runtime_get_sync(delta->dev);
1281         if (ret < 0) {
1282                 dev_err(delta->dev, "%s pm_runtime_get_sync failed (%d)\n",
1283                         __func__, ret);
1284                 return ret;
1285         }
1286
1287         return 0;
1288 }
1289
1290 void delta_put_autosuspend(struct delta_ctx *ctx)
1291 {
1292         struct delta_dev *delta = ctx->dev;
1293
1294         pm_runtime_put_autosuspend(delta->dev);
1295 }
1296
1297 static void delta_vb2_au_queue(struct vb2_buffer *vb)
1298 {
1299         struct vb2_queue *q = vb->vb2_queue;
1300         struct delta_ctx *ctx = vb2_get_drv_priv(q);
1301         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1302
1303         v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
1304 }
1305
1306 static int delta_vb2_au_start_streaming(struct vb2_queue *q,
1307                                         unsigned int count)
1308 {
1309         struct delta_ctx *ctx = vb2_get_drv_priv(q);
1310         struct delta_dev *delta = ctx->dev;
1311         const struct delta_dec *dec = ctx->dec;
1312         struct delta_au *au;
1313         int ret = 0;
1314         struct vb2_v4l2_buffer *vbuf = NULL;
1315         struct delta_streaminfo *streaminfo = &ctx->streaminfo;
1316         struct delta_frameinfo *frameinfo = &ctx->frameinfo;
1317         unsigned char str1[100] = "";
1318         unsigned char str2[100] = "";
1319
1320         if ((ctx->state != DELTA_STATE_WF_FORMAT) &&
1321             (ctx->state != DELTA_STATE_WF_STREAMINFO))
1322                 return 0;
1323
1324         if (ctx->state == DELTA_STATE_WF_FORMAT) {
1325                 /* open decoder if not yet done */
1326                 ret = delta_open_decoder(ctx,
1327                                          ctx->streaminfo.streamformat,
1328                                          ctx->frameinfo.pixelformat, &dec);
1329                 if (ret)
1330                         goto err;
1331                 ctx->dec = dec;
1332                 ctx->state = DELTA_STATE_WF_STREAMINFO;
1333         }
1334
1335         /*
1336          * first buffer should contain stream header,
1337          * decode it to get the infos related to stream
1338          * such as width, height, dpb, ...
1339          */
1340         vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
1341         if (!vbuf) {
1342                 dev_err(delta->dev, "%s failed to start streaming, no stream header buffer enqueued\n",
1343                         ctx->name);
1344                 ret = -EINVAL;
1345                 goto err;
1346         }
1347         au = to_au(vbuf);
1348         au->size = vb2_get_plane_payload(&vbuf->vb2_buf, 0);
1349         au->dts = vbuf->vb2_buf.timestamp;
1350
1351         delta_push_dts(ctx, au->dts);
1352
1353         /* dump access unit */
1354         dump_au(ctx, au);
1355
1356         /* decode this access unit */
1357         ret = call_dec_op(dec, decode, ctx, au);
1358         if (ret) {
1359                 dev_err(delta->dev, "%s failed to start streaming, header decoding failed (%d)\n",
1360                         ctx->name, ret);
1361                 goto err;
1362         }
1363
1364         ret = call_dec_op(dec, get_streaminfo, ctx, streaminfo);
1365         if (ret) {
1366                 dev_dbg_ratelimited(delta->dev,
1367                                     "%s failed to start streaming, valid stream header not yet decoded\n",
1368                                     ctx->name);
1369                 goto err;
1370         }
1371         ctx->flags |= DELTA_FLAG_STREAMINFO;
1372
1373         ret = call_dec_op(dec, get_frameinfo, ctx, frameinfo);
1374         if (ret)
1375                 goto err;
1376         ctx->flags |= DELTA_FLAG_FRAMEINFO;
1377
1378         ctx->state = DELTA_STATE_READY;
1379
1380         dev_dbg(delta->dev, "%s %s => %s\n", ctx->name,
1381                 delta_streaminfo_str(streaminfo, str1, sizeof(str1)),
1382                 delta_frameinfo_str(frameinfo, str2, sizeof(str2)));
1383
1384         delta_au_done(ctx, au, ret);
1385         return 0;
1386
1387 err:
1388         /*
1389          * return all buffers to vb2 in QUEUED state.
1390          * This will give ownership back to userspace
1391          */
1392         if (vbuf)
1393                 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_QUEUED);
1394
1395         while ((vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
1396                 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_QUEUED);
1397         return ret;
1398 }
1399
1400 static void delta_vb2_au_stop_streaming(struct vb2_queue *q)
1401 {
1402         struct delta_ctx *ctx = vb2_get_drv_priv(q);
1403         struct vb2_v4l2_buffer *vbuf;
1404
1405         delta_flush_dts(ctx);
1406
1407         /* return all buffers to vb2 in ERROR state */
1408         while ((vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
1409                 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
1410
1411         ctx->au_num = 0;
1412
1413         ctx->aborting = false;
1414 }
1415
1416 static int delta_vb2_frame_queue_setup(struct vb2_queue *vq,
1417                                        unsigned int *num_buffers,
1418                                        unsigned int *num_planes,
1419                                        unsigned int sizes[],
1420                                        struct device *alloc_devs[])
1421 {
1422         struct delta_ctx *ctx = vb2_get_drv_priv(vq);
1423         struct delta_dev *delta = ctx->dev;
1424         struct delta_streaminfo *streaminfo = &ctx->streaminfo;
1425         struct delta_frameinfo *frameinfo = &ctx->frameinfo;
1426         unsigned int size = frameinfo->size;
1427
1428         /*
1429          * the number of output buffers needed for decoding =
1430          * user need (*num_buffers given, usually for display pipeline) +
1431          * stream need (streaminfo->dpb) +
1432          * decoding peak smoothing (depends on DELTA IP perf)
1433          */
1434         if (*num_buffers < DELTA_MIN_FRAME_USER) {
1435                 dev_dbg(delta->dev,
1436                         "%s num_buffers too low (%d), increasing to %d\n",
1437                         ctx->name, *num_buffers, DELTA_MIN_FRAME_USER);
1438                 *num_buffers = DELTA_MIN_FRAME_USER;
1439         }
1440
1441         *num_buffers += streaminfo->dpb + DELTA_PEAK_FRAME_SMOOTHING;
1442
1443         if (*num_buffers > DELTA_MAX_FRAMES) {
1444                 dev_dbg(delta->dev,
1445                         "%s output frame count too high (%d), cut to %d\n",
1446                         ctx->name, *num_buffers, DELTA_MAX_FRAMES);
1447                 *num_buffers = DELTA_MAX_FRAMES;
1448         }
1449
1450         if (*num_planes)
1451                 return sizes[0] < size ? -EINVAL : 0;
1452
1453         /* single plane for Y and CbCr */
1454         *num_planes = 1;
1455
1456         sizes[0] = size;
1457
1458         ctx->nb_of_frames = 0;
1459
1460         return 0;
1461 }
1462
1463 static int delta_vb2_frame_prepare(struct vb2_buffer *vb)
1464 {
1465         struct vb2_queue *q = vb->vb2_queue;
1466         struct delta_ctx *ctx = vb2_get_drv_priv(q);
1467         struct delta_dev *delta = ctx->dev;
1468         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1469         struct delta_frame *frame = to_frame(vbuf);
1470         int ret = 0;
1471
1472         if (!frame->prepared) {
1473                 frame->index = vbuf->vb2_buf.index;
1474                 frame->vaddr = vb2_plane_vaddr(&vbuf->vb2_buf, 0);
1475                 frame->paddr = vb2_dma_contig_plane_dma_addr(&vbuf->vb2_buf, 0);
1476                 frame->info = ctx->frameinfo;
1477
1478                 ret = delta_setup_frame(ctx, frame);
1479                 if (ret) {
1480                         dev_err(delta->dev,
1481                                 "%s setup_frame() failed (%d)\n",
1482                                 ctx->name, ret);
1483                         return ret;
1484                 }
1485                 frame->prepared = true;
1486                 dev_dbg(delta->dev,
1487                         "%s frame[%d] prepared; virt=0x%p, phy=0x%pad\n",
1488                         ctx->name, vb->index, frame->vaddr,
1489                         &frame->paddr);
1490         }
1491
1492         frame->flags = vbuf->flags;
1493
1494         return 0;
1495 }
1496
1497 static void delta_vb2_frame_finish(struct vb2_buffer *vb)
1498 {
1499         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1500         struct delta_frame *frame = to_frame(vbuf);
1501
1502         /* update V4L2 fields for user */
1503         vb2_set_plane_payload(&vbuf->vb2_buf, 0, frame->info.size);
1504         vb->timestamp = frame->dts;
1505         vbuf->field = frame->field;
1506         vbuf->flags = frame->flags;
1507 }
1508
1509 static void delta_vb2_frame_queue(struct vb2_buffer *vb)
1510 {
1511         struct vb2_queue *q = vb->vb2_queue;
1512         struct delta_ctx *ctx = vb2_get_drv_priv(q);
1513         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1514         struct delta_frame *frame = to_frame(vbuf);
1515
1516         if (ctx->state == DELTA_STATE_WF_EOS) {
1517                 /* new frame available, EOS can now be completed */
1518                 delta_complete_eos(ctx, frame);
1519
1520                 ctx->state = DELTA_STATE_EOS;
1521
1522                 /* return, no need to recycle this buffer to decoder */
1523                 return;
1524         }
1525
1526         /* recycle this frame */
1527         delta_recycle(ctx, frame);
1528 }
1529
1530 static void delta_vb2_frame_stop_streaming(struct vb2_queue *q)
1531 {
1532         struct delta_ctx *ctx = vb2_get_drv_priv(q);
1533         struct vb2_v4l2_buffer *vbuf;
1534         struct delta_frame *frame;
1535         const struct delta_dec *dec = ctx->dec;
1536         unsigned int i;
1537
1538         delta_flush_dts(ctx);
1539
1540         call_dec_op(dec, flush, ctx);
1541
1542         /*
1543          * return all buffers to vb2 in ERROR state
1544          * & reset each frame state to OUT
1545          */
1546         for (i = 0; i < ctx->nb_of_frames; i++) {
1547                 frame = ctx->frames[i];
1548                 if (!(frame->state & DELTA_FRAME_OUT)) {
1549                         vbuf = &frame->vbuf;
1550                         v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
1551                 }
1552                 frame->state = DELTA_FRAME_OUT;
1553         }
1554
1555         ctx->frame_num = 0;
1556
1557         ctx->aborting = false;
1558 }
1559
1560 /* VB2 queue ops */
1561 static const struct vb2_ops delta_vb2_au_ops = {
1562         .queue_setup = delta_vb2_au_queue_setup,
1563         .buf_prepare = delta_vb2_au_prepare,
1564         .buf_queue = delta_vb2_au_queue,
1565         .wait_prepare = vb2_ops_wait_prepare,
1566         .wait_finish = vb2_ops_wait_finish,
1567         .start_streaming = delta_vb2_au_start_streaming,
1568         .stop_streaming = delta_vb2_au_stop_streaming,
1569 };
1570
1571 static const struct vb2_ops delta_vb2_frame_ops = {
1572         .queue_setup = delta_vb2_frame_queue_setup,
1573         .buf_prepare = delta_vb2_frame_prepare,
1574         .buf_finish = delta_vb2_frame_finish,
1575         .buf_queue = delta_vb2_frame_queue,
1576         .wait_prepare = vb2_ops_wait_prepare,
1577         .wait_finish = vb2_ops_wait_finish,
1578         .stop_streaming = delta_vb2_frame_stop_streaming,
1579 };
1580
1581 /*
1582  * V4L2 file operations
1583  */
1584
1585 static int queue_init(void *priv,
1586                       struct vb2_queue *src_vq, struct vb2_queue *dst_vq)
1587 {
1588         struct vb2_queue *q;
1589         struct delta_ctx *ctx = priv;
1590         struct delta_dev *delta = ctx->dev;
1591         int ret;
1592
1593         /* setup vb2 queue for stream input */
1594         q = src_vq;
1595         q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1596         q->io_modes = VB2_MMAP | VB2_DMABUF;
1597         q->drv_priv = ctx;
1598         /* overload vb2 buf with private au struct */
1599         q->buf_struct_size = sizeof(struct delta_au);
1600         q->ops = &delta_vb2_au_ops;
1601         q->mem_ops = &vb2_dma_contig_memops;
1602         q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1603         q->lock = &delta->lock;
1604         q->dev = delta->dev;
1605
1606         ret = vb2_queue_init(q);
1607         if (ret)
1608                 return ret;
1609
1610         /* setup vb2 queue for frame output */
1611         q = dst_vq;
1612         q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1613         q->io_modes = VB2_MMAP | VB2_DMABUF;
1614         q->drv_priv = ctx;
1615         /* overload vb2 buf with private frame struct */
1616         q->buf_struct_size = sizeof(struct delta_frame)
1617                              + DELTA_MAX_FRAME_PRIV_SIZE;
1618         q->ops = &delta_vb2_frame_ops;
1619         q->mem_ops = &vb2_dma_contig_memops;
1620         q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1621         q->lock = &delta->lock;
1622         q->dev = delta->dev;
1623
1624         return vb2_queue_init(q);
1625 }
1626
1627 static int delta_open(struct file *file)
1628 {
1629         struct delta_dev *delta = video_drvdata(file);
1630         struct delta_ctx *ctx = NULL;
1631         int ret = 0;
1632
1633         mutex_lock(&delta->lock);
1634
1635         ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1636         if (!ctx) {
1637                 ret = -ENOMEM;
1638                 goto err;
1639         }
1640         ctx->dev = delta;
1641
1642         v4l2_fh_init(&ctx->fh, video_devdata(file));
1643         file->private_data = &ctx->fh;
1644         v4l2_fh_add(&ctx->fh);
1645
1646         INIT_WORK(&ctx->run_work, delta_run_work);
1647         mutex_init(&ctx->lock);
1648
1649         ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(delta->m2m_dev, ctx,
1650                                             queue_init);
1651         if (IS_ERR(ctx->fh.m2m_ctx)) {
1652                 ret = PTR_ERR(ctx->fh.m2m_ctx);
1653                 dev_err(delta->dev, "%s failed to initialize m2m context (%d)\n",
1654                         DELTA_PREFIX, ret);
1655                 goto err_fh_del;
1656         }
1657
1658         /*
1659          * wait stream format to determine which
1660          * decoder to open
1661          */
1662         ctx->state = DELTA_STATE_WF_FORMAT;
1663
1664         INIT_LIST_HEAD(&ctx->dts);
1665
1666         /* set the instance name */
1667         delta->instance_id++;
1668         snprintf(ctx->name, sizeof(ctx->name), "[%3d:----]",
1669                  delta->instance_id);
1670
1671         /* default parameters for frame and stream */
1672         set_default_params(ctx);
1673
1674         /* enable ST231 clocks */
1675         if (delta->clk_st231)
1676                 if (clk_prepare_enable(delta->clk_st231))
1677                         dev_warn(delta->dev, "failed to enable st231 clk\n");
1678
1679         /* enable FLASH_PROMIP clock */
1680         if (delta->clk_flash_promip)
1681                 if (clk_prepare_enable(delta->clk_flash_promip))
1682                         dev_warn(delta->dev, "failed to enable delta promip clk\n");
1683
1684         mutex_unlock(&delta->lock);
1685
1686         dev_dbg(delta->dev, "%s decoder instance created\n", ctx->name);
1687
1688         return 0;
1689
1690 err_fh_del:
1691         v4l2_fh_del(&ctx->fh);
1692         v4l2_fh_exit(&ctx->fh);
1693         kfree(ctx);
1694 err:
1695         mutex_unlock(&delta->lock);
1696
1697         return ret;
1698 }
1699
1700 static int delta_release(struct file *file)
1701 {
1702         struct delta_ctx *ctx = to_ctx(file->private_data);
1703         struct delta_dev *delta = ctx->dev;
1704         const struct delta_dec *dec = ctx->dec;
1705
1706         mutex_lock(&delta->lock);
1707
1708         /* close decoder */
1709         call_dec_op(dec, close, ctx);
1710
1711         /*
1712          * trace a summary of instance
1713          * before closing (debug purpose)
1714          */
1715         delta_trace_summary(ctx);
1716
1717         v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
1718
1719         v4l2_fh_del(&ctx->fh);
1720         v4l2_fh_exit(&ctx->fh);
1721
1722         /* disable ST231 clocks */
1723         if (delta->clk_st231)
1724                 clk_disable_unprepare(delta->clk_st231);
1725
1726         /* disable FLASH_PROMIP clock */
1727         if (delta->clk_flash_promip)
1728                 clk_disable_unprepare(delta->clk_flash_promip);
1729
1730         dev_dbg(delta->dev, "%s decoder instance released\n", ctx->name);
1731
1732         kfree(ctx);
1733
1734         mutex_unlock(&delta->lock);
1735         return 0;
1736 }
1737
1738 /* V4L2 file ops */
1739 static const struct v4l2_file_operations delta_fops = {
1740         .owner = THIS_MODULE,
1741         .open = delta_open,
1742         .release = delta_release,
1743         .unlocked_ioctl = video_ioctl2,
1744         .mmap = v4l2_m2m_fop_mmap,
1745         .poll = v4l2_m2m_fop_poll,
1746 };
1747
1748 /*
1749  * Platform device operations
1750  */
1751
1752 static int delta_register_device(struct delta_dev *delta)
1753 {
1754         int ret;
1755         struct video_device *vdev;
1756
1757         if (!delta)
1758                 return -ENODEV;
1759
1760         delta->m2m_dev = v4l2_m2m_init(&delta_m2m_ops);
1761         if (IS_ERR(delta->m2m_dev)) {
1762                 dev_err(delta->dev, "%s failed to initialize v4l2-m2m device\n",
1763                         DELTA_PREFIX);
1764                 ret = PTR_ERR(delta->m2m_dev);
1765                 goto err;
1766         }
1767
1768         vdev = video_device_alloc();
1769         if (!vdev) {
1770                 dev_err(delta->dev, "%s failed to allocate video device\n",
1771                         DELTA_PREFIX);
1772                 ret = -ENOMEM;
1773                 goto err_m2m_release;
1774         }
1775
1776         vdev->fops = &delta_fops;
1777         vdev->ioctl_ops = &delta_ioctl_ops;
1778         vdev->release = video_device_release;
1779         vdev->lock = &delta->lock;
1780         vdev->vfl_dir = VFL_DIR_M2M;
1781         vdev->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_M2M;
1782         vdev->v4l2_dev = &delta->v4l2_dev;
1783         snprintf(vdev->name, sizeof(vdev->name), "%s-%s",
1784                  DELTA_NAME, DELTA_FW_VERSION);
1785
1786         ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
1787         if (ret) {
1788                 dev_err(delta->dev, "%s failed to register video device\n",
1789                         DELTA_PREFIX);
1790                 goto err_vdev_release;
1791         }
1792
1793         delta->vdev = vdev;
1794         video_set_drvdata(vdev, delta);
1795         return 0;
1796
1797 err_vdev_release:
1798         video_device_release(vdev);
1799 err_m2m_release:
1800         v4l2_m2m_release(delta->m2m_dev);
1801 err:
1802         return ret;
1803 }
1804
1805 static void delta_unregister_device(struct delta_dev *delta)
1806 {
1807         if (!delta)
1808                 return;
1809
1810         if (delta->m2m_dev)
1811                 v4l2_m2m_release(delta->m2m_dev);
1812
1813         video_unregister_device(delta->vdev);
1814 }
1815
1816 static int delta_probe(struct platform_device *pdev)
1817 {
1818         struct delta_dev *delta;
1819         struct device *dev = &pdev->dev;
1820         int ret;
1821
1822         delta = devm_kzalloc(dev, sizeof(*delta), GFP_KERNEL);
1823         if (!delta) {
1824                 ret = -ENOMEM;
1825                 goto err;
1826         }
1827
1828         delta->dev = dev;
1829         delta->pdev = pdev;
1830         platform_set_drvdata(pdev, delta);
1831
1832         mutex_init(&delta->lock);
1833
1834         /* get clock resources */
1835         delta->clk_delta = devm_clk_get(dev, "delta");
1836         if (IS_ERR(delta->clk_delta)) {
1837                 dev_dbg(dev, "%s can't get delta clock\n", DELTA_PREFIX);
1838                 delta->clk_delta = NULL;
1839         }
1840
1841         delta->clk_st231 = devm_clk_get(dev, "delta-st231");
1842         if (IS_ERR(delta->clk_st231)) {
1843                 dev_dbg(dev, "%s can't get delta-st231 clock\n", DELTA_PREFIX);
1844                 delta->clk_st231 = NULL;
1845         }
1846
1847         delta->clk_flash_promip = devm_clk_get(dev, "delta-flash-promip");
1848         if (IS_ERR(delta->clk_flash_promip)) {
1849                 dev_dbg(dev, "%s can't get delta-flash-promip clock\n",
1850                         DELTA_PREFIX);
1851                 delta->clk_flash_promip = NULL;
1852         }
1853
1854         /* init pm_runtime used for power management */
1855         pm_runtime_set_autosuspend_delay(dev, DELTA_HW_AUTOSUSPEND_DELAY_MS);
1856         pm_runtime_use_autosuspend(dev);
1857         pm_runtime_set_suspended(dev);
1858         pm_runtime_enable(dev);
1859
1860         /* init firmware ipc channel */
1861         ret = delta_ipc_init(delta);
1862         if (ret) {
1863                 dev_err(delta->dev, "%s failed to initialize firmware ipc channel\n",
1864                         DELTA_PREFIX);
1865                 goto err_pm_disable;
1866         }
1867
1868         /* register all available decoders */
1869         register_decoders(delta);
1870
1871         /* register all supported formats */
1872         register_formats(delta);
1873
1874         /* register on V4L2 */
1875         ret = v4l2_device_register(dev, &delta->v4l2_dev);
1876         if (ret) {
1877                 dev_err(delta->dev, "%s failed to register V4L2 device\n",
1878                         DELTA_PREFIX);
1879                 goto err_pm_disable;
1880         }
1881
1882         delta->work_queue = create_workqueue(DELTA_NAME);
1883         if (!delta->work_queue) {
1884                 dev_err(delta->dev, "%s failed to allocate work queue\n",
1885                         DELTA_PREFIX);
1886                 ret = -ENOMEM;
1887                 goto err_v4l2;
1888         }
1889
1890         /* register device */
1891         ret = delta_register_device(delta);
1892         if (ret)
1893                 goto err_work_queue;
1894
1895         dev_info(dev, "%s %s registered as /dev/video%d\n",
1896                  DELTA_PREFIX, delta->vdev->name, delta->vdev->num);
1897
1898         return 0;
1899
1900 err_work_queue:
1901         destroy_workqueue(delta->work_queue);
1902 err_v4l2:
1903         v4l2_device_unregister(&delta->v4l2_dev);
1904 err_pm_disable:
1905         pm_runtime_disable(dev);
1906 err:
1907         return ret;
1908 }
1909
1910 static int delta_remove(struct platform_device *pdev)
1911 {
1912         struct delta_dev *delta = platform_get_drvdata(pdev);
1913
1914         delta_ipc_exit(delta);
1915
1916         delta_unregister_device(delta);
1917
1918         destroy_workqueue(delta->work_queue);
1919
1920         pm_runtime_put_autosuspend(delta->dev);
1921         pm_runtime_disable(delta->dev);
1922
1923         v4l2_device_unregister(&delta->v4l2_dev);
1924
1925         return 0;
1926 }
1927
1928 static int delta_runtime_suspend(struct device *dev)
1929 {
1930         struct delta_dev *delta = dev_get_drvdata(dev);
1931
1932         if (delta->clk_delta)
1933                 clk_disable_unprepare(delta->clk_delta);
1934
1935         return 0;
1936 }
1937
1938 static int delta_runtime_resume(struct device *dev)
1939 {
1940         struct delta_dev *delta = dev_get_drvdata(dev);
1941
1942         if (delta->clk_delta)
1943                 if (clk_prepare_enable(delta->clk_delta))
1944                         dev_warn(dev, "failed to prepare/enable delta clk\n");
1945
1946         return 0;
1947 }
1948
1949 /* PM ops */
1950 static const struct dev_pm_ops delta_pm_ops = {
1951         .runtime_suspend = delta_runtime_suspend,
1952         .runtime_resume = delta_runtime_resume,
1953 };
1954
1955 static const struct of_device_id delta_match_types[] = {
1956         {
1957          .compatible = "st,st-delta",
1958         },
1959         {
1960          /* end node */
1961         }
1962 };
1963
1964 MODULE_DEVICE_TABLE(of, delta_match_types);
1965
1966 static struct platform_driver delta_driver = {
1967         .probe = delta_probe,
1968         .remove = delta_remove,
1969         .driver = {
1970                    .name = DELTA_NAME,
1971                    .of_match_table = delta_match_types,
1972                    .pm = &delta_pm_ops},
1973 };
1974
1975 module_platform_driver(delta_driver);
1976
1977 MODULE_LICENSE("GPL");
1978 MODULE_AUTHOR("Hugues Fruchet <hugues.fruchet@st.com>");
1979 MODULE_DESCRIPTION("STMicroelectronics DELTA video decoder V4L2 driver");