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