GNU Linux-libre 4.19.211-gnu1
[releases.git] / drivers / media / platform / sti / hva / hva-v4l2.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) STMicroelectronics SA 2015
4  * Authors: Yannick Fertre <yannick.fertre@st.com>
5  *          Hugues Fruchet <hugues.fruchet@st.com>
6  */
7
8 #include <linux/module.h>
9 #include <linux/mod_devicetable.h>
10 #include <linux/platform_device.h>
11 #include <linux/slab.h>
12 #include <media/v4l2-event.h>
13 #include <media/v4l2-ioctl.h>
14 #include <media/videobuf2-dma-contig.h>
15
16 #include "hva.h"
17 #include "hva-hw.h"
18
19 #define MIN_FRAMES      1
20 #define MIN_STREAMS     1
21
22 #define HVA_MIN_WIDTH   32
23 #define HVA_MAX_WIDTH   1920
24 #define HVA_MIN_HEIGHT  32
25 #define HVA_MAX_HEIGHT  1920
26
27 /* HVA requires a 16x16 pixels alignment for frames */
28 #define HVA_WIDTH_ALIGNMENT     16
29 #define HVA_HEIGHT_ALIGNMENT    16
30
31 #define HVA_DEFAULT_WIDTH       HVA_MIN_WIDTH
32 #define HVA_DEFAULT_HEIGHT      HVA_MIN_HEIGHT
33 #define HVA_DEFAULT_FRAME_NUM   1
34 #define HVA_DEFAULT_FRAME_DEN   30
35
36 #define to_type_str(type) (type == V4L2_BUF_TYPE_VIDEO_OUTPUT ? \
37                            "frame" : "stream")
38
39 #define fh_to_ctx(f)    (container_of(f, struct hva_ctx, fh))
40
41 /* registry of available encoders */
42 static const struct hva_enc *hva_encoders[] = {
43         &nv12h264enc,
44         &nv21h264enc,
45 };
46
47 static inline int frame_size(u32 w, u32 h, u32 fmt)
48 {
49         switch (fmt) {
50         case V4L2_PIX_FMT_NV12:
51         case V4L2_PIX_FMT_NV21:
52                 return (w * h * 3) / 2;
53         default:
54                 return 0;
55         }
56 }
57
58 static inline int frame_stride(u32 w, u32 fmt)
59 {
60         switch (fmt) {
61         case V4L2_PIX_FMT_NV12:
62         case V4L2_PIX_FMT_NV21:
63                 return w;
64         default:
65                 return 0;
66         }
67 }
68
69 static inline int frame_alignment(u32 fmt)
70 {
71         switch (fmt) {
72         case V4L2_PIX_FMT_NV12:
73         case V4L2_PIX_FMT_NV21:
74                 /* multiple of 2 */
75                 return 2;
76         default:
77                 return 1;
78         }
79 }
80
81 static inline int estimated_stream_size(u32 w, u32 h)
82 {
83         /*
84          * HVA only encodes in YUV420 format, whatever the frame format.
85          * A compression ratio of 2 is assumed: thus, the maximum size
86          * of a stream is estimated to ((width x height x 3 / 2) / 2)
87          */
88         return (w * h * 3) / 4;
89 }
90
91 static void set_default_params(struct hva_ctx *ctx)
92 {
93         struct hva_frameinfo *frameinfo = &ctx->frameinfo;
94         struct hva_streaminfo *streaminfo = &ctx->streaminfo;
95
96         frameinfo->pixelformat = V4L2_PIX_FMT_NV12;
97         frameinfo->width = HVA_DEFAULT_WIDTH;
98         frameinfo->height = HVA_DEFAULT_HEIGHT;
99         frameinfo->aligned_width = ALIGN(frameinfo->width,
100                                          HVA_WIDTH_ALIGNMENT);
101         frameinfo->aligned_height = ALIGN(frameinfo->height,
102                                           HVA_HEIGHT_ALIGNMENT);
103         frameinfo->size = frame_size(frameinfo->aligned_width,
104                                      frameinfo->aligned_height,
105                                      frameinfo->pixelformat);
106
107         streaminfo->streamformat = V4L2_PIX_FMT_H264;
108         streaminfo->width = HVA_DEFAULT_WIDTH;
109         streaminfo->height = HVA_DEFAULT_HEIGHT;
110
111         ctx->colorspace = V4L2_COLORSPACE_REC709;
112         ctx->xfer_func = V4L2_XFER_FUNC_DEFAULT;
113         ctx->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
114         ctx->quantization = V4L2_QUANTIZATION_DEFAULT;
115
116         ctx->max_stream_size = estimated_stream_size(streaminfo->width,
117                                                      streaminfo->height);
118 }
119
120 static const struct hva_enc *hva_find_encoder(struct hva_ctx *ctx,
121                                               u32 pixelformat,
122                                               u32 streamformat)
123 {
124         struct hva_dev *hva = ctx_to_hdev(ctx);
125         const struct hva_enc *enc;
126         unsigned int i;
127
128         for (i = 0; i < hva->nb_of_encoders; i++) {
129                 enc = hva->encoders[i];
130                 if ((enc->pixelformat == pixelformat) &&
131                     (enc->streamformat == streamformat))
132                         return enc;
133         }
134
135         return NULL;
136 }
137
138 static void register_format(u32 format, u32 formats[], u32 *nb_of_formats)
139 {
140         u32 i;
141         bool found = false;
142
143         for (i = 0; i < *nb_of_formats; i++) {
144                 if (format == formats[i]) {
145                         found = true;
146                         break;
147                 }
148         }
149
150         if (!found)
151                 formats[(*nb_of_formats)++] = format;
152 }
153
154 static void register_formats(struct hva_dev *hva)
155 {
156         unsigned int i;
157
158         for (i = 0; i < hva->nb_of_encoders; i++) {
159                 register_format(hva->encoders[i]->pixelformat,
160                                 hva->pixelformats,
161                                 &hva->nb_of_pixelformats);
162
163                 register_format(hva->encoders[i]->streamformat,
164                                 hva->streamformats,
165                                 &hva->nb_of_streamformats);
166         }
167 }
168
169 static void register_encoders(struct hva_dev *hva)
170 {
171         struct device *dev = hva_to_dev(hva);
172         unsigned int i;
173
174         for (i = 0; i < ARRAY_SIZE(hva_encoders); i++) {
175                 if (hva->nb_of_encoders >= HVA_MAX_ENCODERS) {
176                         dev_dbg(dev,
177                                 "%s failed to register %s encoder (%d maximum reached)\n",
178                                 HVA_PREFIX, hva_encoders[i]->name,
179                                 HVA_MAX_ENCODERS);
180                         return;
181                 }
182
183                 hva->encoders[hva->nb_of_encoders++] = hva_encoders[i];
184                 dev_info(dev, "%s %s encoder registered\n", HVA_PREFIX,
185                          hva_encoders[i]->name);
186         }
187 }
188
189 static int hva_open_encoder(struct hva_ctx *ctx, u32 streamformat,
190                             u32 pixelformat, struct hva_enc **penc)
191 {
192         struct hva_dev *hva = ctx_to_hdev(ctx);
193         struct device *dev = ctx_to_dev(ctx);
194         struct hva_enc *enc;
195         int ret;
196
197         /* find an encoder which can deal with these formats */
198         enc = (struct hva_enc *)hva_find_encoder(ctx, pixelformat,
199                                                  streamformat);
200         if (!enc) {
201                 dev_err(dev, "%s no encoder found matching %4.4s => %4.4s\n",
202                         ctx->name, (char *)&pixelformat, (char *)&streamformat);
203                 return -EINVAL;
204         }
205
206         dev_dbg(dev, "%s one encoder matching %4.4s => %4.4s\n",
207                 ctx->name, (char *)&pixelformat, (char *)&streamformat);
208
209         /* update instance name */
210         snprintf(ctx->name, sizeof(ctx->name), "[%3d:%4.4s]",
211                  hva->instance_id, (char *)&streamformat);
212
213         /* open encoder instance */
214         ret = enc->open(ctx);
215         if (ret) {
216                 dev_err(dev, "%s failed to open encoder instance (%d)\n",
217                         ctx->name, ret);
218                 return ret;
219         }
220
221         dev_dbg(dev, "%s %s encoder opened\n", ctx->name, enc->name);
222
223         *penc = enc;
224
225         return ret;
226 }
227
228 static void hva_dbg_summary(struct hva_ctx *ctx)
229 {
230         struct device *dev = ctx_to_dev(ctx);
231         struct hva_streaminfo *stream = &ctx->streaminfo;
232         struct hva_frameinfo *frame = &ctx->frameinfo;
233
234         if (!(ctx->flags & HVA_FLAG_STREAMINFO))
235                 return;
236
237         dev_dbg(dev, "%s %4.4s %dx%d > %4.4s %dx%d %s %s: %d frames encoded, %d system errors, %d encoding errors, %d frame errors\n",
238                 ctx->name,
239                 (char *)&frame->pixelformat,
240                 frame->aligned_width, frame->aligned_height,
241                 (char *)&stream->streamformat,
242                 stream->width, stream->height,
243                 stream->profile, stream->level,
244                 ctx->encoded_frames,
245                 ctx->sys_errors,
246                 ctx->encode_errors,
247                 ctx->frame_errors);
248 }
249
250 /*
251  * V4L2 ioctl operations
252  */
253
254 static int hva_querycap(struct file *file, void *priv,
255                         struct v4l2_capability *cap)
256 {
257         struct hva_ctx *ctx = fh_to_ctx(file->private_data);
258         struct hva_dev *hva = ctx_to_hdev(ctx);
259
260         strlcpy(cap->driver, HVA_NAME, sizeof(cap->driver));
261         strlcpy(cap->card, hva->vdev->name, sizeof(cap->card));
262         snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
263                  hva->pdev->name);
264
265         return 0;
266 }
267
268 static int hva_enum_fmt_stream(struct file *file, void *priv,
269                                struct v4l2_fmtdesc *f)
270 {
271         struct hva_ctx *ctx = fh_to_ctx(file->private_data);
272         struct hva_dev *hva = ctx_to_hdev(ctx);
273
274         if (unlikely(f->index >= hva->nb_of_streamformats))
275                 return -EINVAL;
276
277         f->pixelformat = hva->streamformats[f->index];
278
279         return 0;
280 }
281
282 static int hva_enum_fmt_frame(struct file *file, void *priv,
283                               struct v4l2_fmtdesc *f)
284 {
285         struct hva_ctx *ctx = fh_to_ctx(file->private_data);
286         struct hva_dev *hva = ctx_to_hdev(ctx);
287
288         if (unlikely(f->index >= hva->nb_of_pixelformats))
289                 return -EINVAL;
290
291         f->pixelformat = hva->pixelformats[f->index];
292
293         return 0;
294 }
295
296 static int hva_g_fmt_stream(struct file *file, void *fh, struct v4l2_format *f)
297 {
298         struct hva_ctx *ctx = fh_to_ctx(file->private_data);
299         struct hva_streaminfo *streaminfo = &ctx->streaminfo;
300
301         f->fmt.pix.width = streaminfo->width;
302         f->fmt.pix.height = streaminfo->height;
303         f->fmt.pix.field = V4L2_FIELD_NONE;
304         f->fmt.pix.colorspace = ctx->colorspace;
305         f->fmt.pix.xfer_func = ctx->xfer_func;
306         f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc;
307         f->fmt.pix.quantization = ctx->quantization;
308         f->fmt.pix.pixelformat = streaminfo->streamformat;
309         f->fmt.pix.bytesperline = 0;
310         f->fmt.pix.sizeimage = ctx->max_stream_size;
311
312         return 0;
313 }
314
315 static int hva_g_fmt_frame(struct file *file, void *fh, struct v4l2_format *f)
316 {
317         struct hva_ctx *ctx = fh_to_ctx(file->private_data);
318         struct hva_frameinfo *frameinfo = &ctx->frameinfo;
319
320         f->fmt.pix.width = frameinfo->width;
321         f->fmt.pix.height = frameinfo->height;
322         f->fmt.pix.field = V4L2_FIELD_NONE;
323         f->fmt.pix.colorspace = ctx->colorspace;
324         f->fmt.pix.xfer_func = ctx->xfer_func;
325         f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc;
326         f->fmt.pix.quantization = ctx->quantization;
327         f->fmt.pix.pixelformat = frameinfo->pixelformat;
328         f->fmt.pix.bytesperline = frame_stride(frameinfo->aligned_width,
329                                                frameinfo->pixelformat);
330         f->fmt.pix.sizeimage = frameinfo->size;
331
332         return 0;
333 }
334
335 static int hva_try_fmt_stream(struct file *file, void *priv,
336                               struct v4l2_format *f)
337 {
338         struct hva_ctx *ctx = fh_to_ctx(file->private_data);
339         struct device *dev = ctx_to_dev(ctx);
340         struct v4l2_pix_format *pix = &f->fmt.pix;
341         u32 streamformat = pix->pixelformat;
342         const struct hva_enc *enc;
343         u32 width, height;
344         u32 stream_size;
345
346         enc = hva_find_encoder(ctx, ctx->frameinfo.pixelformat, streamformat);
347         if (!enc) {
348                 dev_dbg(dev,
349                         "%s V4L2 TRY_FMT (CAPTURE): unsupported format %.4s\n",
350                         ctx->name, (char *)&pix->pixelformat);
351                 return -EINVAL;
352         }
353
354         width = pix->width;
355         height = pix->height;
356         if (ctx->flags & HVA_FLAG_FRAMEINFO) {
357                 /*
358                  * if the frame resolution is already fixed, only allow the
359                  * same stream resolution
360                  */
361                 pix->width = ctx->frameinfo.width;
362                 pix->height = ctx->frameinfo.height;
363                 if ((pix->width != width) || (pix->height != height))
364                         dev_dbg(dev,
365                                 "%s V4L2 TRY_FMT (CAPTURE): resolution updated %dx%d -> %dx%d to fit frame resolution\n",
366                                 ctx->name, width, height,
367                                 pix->width, pix->height);
368         } else {
369                 /* adjust width & height */
370                 v4l_bound_align_image(&pix->width,
371                                       HVA_MIN_WIDTH, enc->max_width,
372                                       0,
373                                       &pix->height,
374                                       HVA_MIN_HEIGHT, enc->max_height,
375                                       0,
376                                       0);
377
378                 if ((pix->width != width) || (pix->height != height))
379                         dev_dbg(dev,
380                                 "%s V4L2 TRY_FMT (CAPTURE): resolution updated %dx%d -> %dx%d to fit min/max/alignment\n",
381                                 ctx->name, width, height,
382                                 pix->width, pix->height);
383         }
384
385         stream_size = estimated_stream_size(pix->width, pix->height);
386         if (pix->sizeimage < stream_size)
387                 pix->sizeimage = stream_size;
388
389         pix->bytesperline = 0;
390         pix->colorspace = ctx->colorspace;
391         pix->xfer_func = ctx->xfer_func;
392         pix->ycbcr_enc = ctx->ycbcr_enc;
393         pix->quantization = ctx->quantization;
394         pix->field = V4L2_FIELD_NONE;
395
396         return 0;
397 }
398
399 static int hva_try_fmt_frame(struct file *file, void *priv,
400                              struct v4l2_format *f)
401 {
402         struct hva_ctx *ctx = fh_to_ctx(file->private_data);
403         struct device *dev = ctx_to_dev(ctx);
404         struct v4l2_pix_format *pix = &f->fmt.pix;
405         u32 pixelformat = pix->pixelformat;
406         const struct hva_enc *enc;
407         u32 width, height;
408
409         enc = hva_find_encoder(ctx, pixelformat, ctx->streaminfo.streamformat);
410         if (!enc) {
411                 dev_dbg(dev,
412                         "%s V4L2 TRY_FMT (OUTPUT): unsupported format %.4s\n",
413                         ctx->name, (char *)&pixelformat);
414                 return -EINVAL;
415         }
416
417         /* adjust width & height */
418         width = pix->width;
419         height = pix->height;
420         v4l_bound_align_image(&pix->width,
421                               HVA_MIN_WIDTH, HVA_MAX_WIDTH,
422                               frame_alignment(pixelformat) - 1,
423                               &pix->height,
424                               HVA_MIN_HEIGHT, HVA_MAX_HEIGHT,
425                               frame_alignment(pixelformat) - 1,
426                               0);
427
428         if ((pix->width != width) || (pix->height != height))
429                 dev_dbg(dev,
430                         "%s V4L2 TRY_FMT (OUTPUT): resolution updated %dx%d -> %dx%d to fit min/max/alignment\n",
431                         ctx->name, width, height, pix->width, pix->height);
432
433         width = ALIGN(pix->width, HVA_WIDTH_ALIGNMENT);
434         height = ALIGN(pix->height, HVA_HEIGHT_ALIGNMENT);
435
436         if (!pix->colorspace) {
437                 pix->colorspace = V4L2_COLORSPACE_REC709;
438                 pix->xfer_func = V4L2_XFER_FUNC_DEFAULT;
439                 pix->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
440                 pix->quantization = V4L2_QUANTIZATION_DEFAULT;
441         }
442
443         pix->bytesperline = frame_stride(width, pixelformat);
444         pix->sizeimage = frame_size(width, height, pixelformat);
445         pix->field = V4L2_FIELD_NONE;
446
447         return 0;
448 }
449
450 static int hva_s_fmt_stream(struct file *file, void *fh, struct v4l2_format *f)
451 {
452         struct hva_ctx *ctx = fh_to_ctx(file->private_data);
453         struct device *dev = ctx_to_dev(ctx);
454         struct vb2_queue *vq;
455         int ret;
456
457         ret = hva_try_fmt_stream(file, fh, f);
458         if (ret) {
459                 dev_dbg(dev, "%s V4L2 S_FMT (CAPTURE): unsupported format %.4s\n",
460                         ctx->name, (char *)&f->fmt.pix.pixelformat);
461                 return ret;
462         }
463
464         vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
465         if (vb2_is_streaming(vq)) {
466                 dev_dbg(dev, "%s V4L2 S_FMT (CAPTURE): queue busy\n",
467                         ctx->name);
468                 return -EBUSY;
469         }
470
471         ctx->max_stream_size = f->fmt.pix.sizeimage;
472         ctx->streaminfo.width = f->fmt.pix.width;
473         ctx->streaminfo.height = f->fmt.pix.height;
474         ctx->streaminfo.streamformat = f->fmt.pix.pixelformat;
475         ctx->flags |= HVA_FLAG_STREAMINFO;
476
477         return 0;
478 }
479
480 static int hva_s_fmt_frame(struct file *file, void *fh, struct v4l2_format *f)
481 {
482         struct hva_ctx *ctx = fh_to_ctx(file->private_data);
483         struct device *dev = ctx_to_dev(ctx);
484         struct v4l2_pix_format *pix = &f->fmt.pix;
485         struct vb2_queue *vq;
486         int ret;
487
488         ret = hva_try_fmt_frame(file, fh, f);
489         if (ret) {
490                 dev_dbg(dev, "%s V4L2 S_FMT (OUTPUT): unsupported format %.4s\n",
491                         ctx->name, (char *)&pix->pixelformat);
492                 return ret;
493         }
494
495         vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
496         if (vb2_is_streaming(vq)) {
497                 dev_dbg(dev, "%s V4L2 S_FMT (OUTPUT): queue busy\n", ctx->name);
498                 return -EBUSY;
499         }
500
501         ctx->colorspace = pix->colorspace;
502         ctx->xfer_func = pix->xfer_func;
503         ctx->ycbcr_enc = pix->ycbcr_enc;
504         ctx->quantization = pix->quantization;
505
506         ctx->frameinfo.aligned_width = ALIGN(pix->width, HVA_WIDTH_ALIGNMENT);
507         ctx->frameinfo.aligned_height = ALIGN(pix->height,
508                                               HVA_HEIGHT_ALIGNMENT);
509         ctx->frameinfo.size = pix->sizeimage;
510         ctx->frameinfo.pixelformat = pix->pixelformat;
511         ctx->frameinfo.width = pix->width;
512         ctx->frameinfo.height = pix->height;
513         ctx->flags |= HVA_FLAG_FRAMEINFO;
514
515         return 0;
516 }
517
518 static int hva_g_parm(struct file *file, void *fh, struct v4l2_streamparm *sp)
519 {
520         struct hva_ctx *ctx = fh_to_ctx(file->private_data);
521         struct v4l2_fract *time_per_frame = &ctx->ctrls.time_per_frame;
522
523         if (sp->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
524                 return -EINVAL;
525
526         sp->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
527         sp->parm.output.timeperframe.numerator = time_per_frame->numerator;
528         sp->parm.output.timeperframe.denominator =
529                 time_per_frame->denominator;
530
531         return 0;
532 }
533
534 static int hva_s_parm(struct file *file, void *fh, struct v4l2_streamparm *sp)
535 {
536         struct hva_ctx *ctx = fh_to_ctx(file->private_data);
537         struct v4l2_fract *time_per_frame = &ctx->ctrls.time_per_frame;
538
539         if (sp->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
540                 return -EINVAL;
541
542         if (!sp->parm.output.timeperframe.numerator ||
543             !sp->parm.output.timeperframe.denominator)
544                 return hva_g_parm(file, fh, sp);
545
546         sp->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
547         time_per_frame->numerator = sp->parm.output.timeperframe.numerator;
548         time_per_frame->denominator =
549                 sp->parm.output.timeperframe.denominator;
550
551         return 0;
552 }
553
554 static int hva_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
555 {
556         struct hva_ctx *ctx = fh_to_ctx(file->private_data);
557         struct device *dev = ctx_to_dev(ctx);
558
559         if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
560                 /*
561                  * depending on the targeted compressed video format, the
562                  * capture buffer might contain headers (e.g. H.264 SPS/PPS)
563                  * filled in by the driver client; the size of these data is
564                  * copied from the bytesused field of the V4L2 buffer in the
565                  * payload field of the hva stream buffer
566                  */
567                 struct vb2_queue *vq;
568                 struct hva_stream *stream;
569
570                 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, buf->type);
571
572                 if (buf->index >= vq->num_buffers) {
573                         dev_dbg(dev, "%s buffer index %d out of range (%d)\n",
574                                 ctx->name, buf->index, vq->num_buffers);
575                         return -EINVAL;
576                 }
577
578                 stream = (struct hva_stream *)vq->bufs[buf->index];
579                 stream->bytesused = buf->bytesused;
580         }
581
582         return v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf);
583 }
584
585 /* V4L2 ioctl ops */
586 static const struct v4l2_ioctl_ops hva_ioctl_ops = {
587         .vidioc_querycap                = hva_querycap,
588         .vidioc_enum_fmt_vid_cap        = hva_enum_fmt_stream,
589         .vidioc_enum_fmt_vid_out        = hva_enum_fmt_frame,
590         .vidioc_g_fmt_vid_cap           = hva_g_fmt_stream,
591         .vidioc_g_fmt_vid_out           = hva_g_fmt_frame,
592         .vidioc_try_fmt_vid_cap         = hva_try_fmt_stream,
593         .vidioc_try_fmt_vid_out         = hva_try_fmt_frame,
594         .vidioc_s_fmt_vid_cap           = hva_s_fmt_stream,
595         .vidioc_s_fmt_vid_out           = hva_s_fmt_frame,
596         .vidioc_g_parm                  = hva_g_parm,
597         .vidioc_s_parm                  = hva_s_parm,
598         .vidioc_reqbufs                 = v4l2_m2m_ioctl_reqbufs,
599         .vidioc_create_bufs             = v4l2_m2m_ioctl_create_bufs,
600         .vidioc_querybuf                = v4l2_m2m_ioctl_querybuf,
601         .vidioc_expbuf                  = v4l2_m2m_ioctl_expbuf,
602         .vidioc_qbuf                    = hva_qbuf,
603         .vidioc_dqbuf                   = v4l2_m2m_ioctl_dqbuf,
604         .vidioc_streamon                = v4l2_m2m_ioctl_streamon,
605         .vidioc_streamoff               = v4l2_m2m_ioctl_streamoff,
606         .vidioc_subscribe_event         = v4l2_ctrl_subscribe_event,
607         .vidioc_unsubscribe_event       = v4l2_event_unsubscribe,
608 };
609
610 /*
611  * V4L2 control operations
612  */
613
614 static int hva_s_ctrl(struct v4l2_ctrl *ctrl)
615 {
616         struct hva_ctx *ctx = container_of(ctrl->handler, struct hva_ctx,
617                                            ctrl_handler);
618         struct device *dev = ctx_to_dev(ctx);
619
620         dev_dbg(dev, "%s S_CTRL: id = %d, val = %d\n", ctx->name,
621                 ctrl->id, ctrl->val);
622
623         switch (ctrl->id) {
624         case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
625                 ctx->ctrls.bitrate_mode = ctrl->val;
626                 break;
627         case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
628                 ctx->ctrls.gop_size = ctrl->val;
629                 break;
630         case V4L2_CID_MPEG_VIDEO_BITRATE:
631                 ctx->ctrls.bitrate = ctrl->val;
632                 break;
633         case V4L2_CID_MPEG_VIDEO_ASPECT:
634                 ctx->ctrls.aspect = ctrl->val;
635                 break;
636         case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
637                 ctx->ctrls.profile = ctrl->val;
638                 snprintf(ctx->streaminfo.profile,
639                          sizeof(ctx->streaminfo.profile),
640                          "%s profile",
641                          v4l2_ctrl_get_menu(ctrl->id)[ctrl->val]);
642                 break;
643         case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
644                 ctx->ctrls.level = ctrl->val;
645                 snprintf(ctx->streaminfo.level,
646                          sizeof(ctx->streaminfo.level),
647                          "level %s",
648                          v4l2_ctrl_get_menu(ctrl->id)[ctrl->val]);
649                 break;
650         case V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE:
651                 ctx->ctrls.entropy_mode = ctrl->val;
652                 break;
653         case V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE:
654                 ctx->ctrls.cpb_size = ctrl->val;
655                 break;
656         case V4L2_CID_MPEG_VIDEO_H264_8X8_TRANSFORM:
657                 ctx->ctrls.dct8x8 = ctrl->val;
658                 break;
659         case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
660                 ctx->ctrls.qpmin = ctrl->val;
661                 break;
662         case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
663                 ctx->ctrls.qpmax = ctrl->val;
664                 break;
665         case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE:
666                 ctx->ctrls.vui_sar = ctrl->val;
667                 break;
668         case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC:
669                 ctx->ctrls.vui_sar_idc = ctrl->val;
670                 break;
671         case V4L2_CID_MPEG_VIDEO_H264_SEI_FRAME_PACKING:
672                 ctx->ctrls.sei_fp = ctrl->val;
673                 break;
674         case V4L2_CID_MPEG_VIDEO_H264_SEI_FP_ARRANGEMENT_TYPE:
675                 ctx->ctrls.sei_fp_type = ctrl->val;
676                 break;
677         default:
678                 dev_dbg(dev, "%s S_CTRL: invalid control (id = %d)\n",
679                         ctx->name, ctrl->id);
680                 return -EINVAL;
681         }
682
683         return 0;
684 }
685
686 /* V4L2 control ops */
687 static const struct v4l2_ctrl_ops hva_ctrl_ops = {
688         .s_ctrl = hva_s_ctrl,
689 };
690
691 static int hva_ctrls_setup(struct hva_ctx *ctx)
692 {
693         struct device *dev = ctx_to_dev(ctx);
694         u64 mask;
695         enum v4l2_mpeg_video_h264_sei_fp_arrangement_type sei_fp_type =
696                 V4L2_MPEG_VIDEO_H264_SEI_FP_ARRANGEMENT_TYPE_TOP_BOTTOM;
697
698         v4l2_ctrl_handler_init(&ctx->ctrl_handler, 15);
699
700         v4l2_ctrl_new_std_menu(&ctx->ctrl_handler, &hva_ctrl_ops,
701                                V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
702                                V4L2_MPEG_VIDEO_BITRATE_MODE_CBR,
703                                0,
704                                V4L2_MPEG_VIDEO_BITRATE_MODE_CBR);
705
706         v4l2_ctrl_new_std(&ctx->ctrl_handler, &hva_ctrl_ops,
707                           V4L2_CID_MPEG_VIDEO_GOP_SIZE,
708                           1, 60, 1, 16);
709
710         v4l2_ctrl_new_std(&ctx->ctrl_handler, &hva_ctrl_ops,
711                           V4L2_CID_MPEG_VIDEO_BITRATE,
712                           1000, 60000000, 1000, 20000000);
713
714         mask = ~(1 << V4L2_MPEG_VIDEO_ASPECT_1x1);
715         v4l2_ctrl_new_std_menu(&ctx->ctrl_handler, &hva_ctrl_ops,
716                                V4L2_CID_MPEG_VIDEO_ASPECT,
717                                V4L2_MPEG_VIDEO_ASPECT_1x1,
718                                mask,
719                                V4L2_MPEG_VIDEO_ASPECT_1x1);
720
721         mask = ~((1 << V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE) |
722                  (1 << V4L2_MPEG_VIDEO_H264_PROFILE_MAIN) |
723                  (1 << V4L2_MPEG_VIDEO_H264_PROFILE_HIGH) |
724                  (1 << V4L2_MPEG_VIDEO_H264_PROFILE_STEREO_HIGH));
725         v4l2_ctrl_new_std_menu(&ctx->ctrl_handler, &hva_ctrl_ops,
726                                V4L2_CID_MPEG_VIDEO_H264_PROFILE,
727                                V4L2_MPEG_VIDEO_H264_PROFILE_STEREO_HIGH,
728                                mask,
729                                V4L2_MPEG_VIDEO_H264_PROFILE_HIGH);
730
731         v4l2_ctrl_new_std_menu(&ctx->ctrl_handler, &hva_ctrl_ops,
732                                V4L2_CID_MPEG_VIDEO_H264_LEVEL,
733                                V4L2_MPEG_VIDEO_H264_LEVEL_4_2,
734                                0,
735                                V4L2_MPEG_VIDEO_H264_LEVEL_4_0);
736
737         v4l2_ctrl_new_std_menu(&ctx->ctrl_handler, &hva_ctrl_ops,
738                                V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE,
739                                V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CABAC,
740                                0,
741                                V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CAVLC);
742
743         v4l2_ctrl_new_std(&ctx->ctrl_handler, &hva_ctrl_ops,
744                           V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE,
745                           1, 10000, 1, 3000);
746
747         v4l2_ctrl_new_std(&ctx->ctrl_handler, &hva_ctrl_ops,
748                           V4L2_CID_MPEG_VIDEO_H264_8X8_TRANSFORM,
749                           0, 1, 1, 0);
750
751         v4l2_ctrl_new_std(&ctx->ctrl_handler, &hva_ctrl_ops,
752                           V4L2_CID_MPEG_VIDEO_H264_MIN_QP,
753                           0, 51, 1, 5);
754
755         v4l2_ctrl_new_std(&ctx->ctrl_handler, &hva_ctrl_ops,
756                           V4L2_CID_MPEG_VIDEO_H264_MAX_QP,
757                           0, 51, 1, 51);
758
759         v4l2_ctrl_new_std(&ctx->ctrl_handler, &hva_ctrl_ops,
760                           V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE,
761                           0, 1, 1, 1);
762
763         mask = ~(1 << V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_1x1);
764         v4l2_ctrl_new_std_menu(&ctx->ctrl_handler, &hva_ctrl_ops,
765                                V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC,
766                                V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_1x1,
767                                mask,
768                                V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_1x1);
769
770         v4l2_ctrl_new_std(&ctx->ctrl_handler, &hva_ctrl_ops,
771                           V4L2_CID_MPEG_VIDEO_H264_SEI_FRAME_PACKING,
772                           0, 1, 1, 0);
773
774         mask = ~(1 << sei_fp_type);
775         v4l2_ctrl_new_std_menu(&ctx->ctrl_handler, &hva_ctrl_ops,
776                                V4L2_CID_MPEG_VIDEO_H264_SEI_FP_ARRANGEMENT_TYPE,
777                                sei_fp_type,
778                                mask,
779                                sei_fp_type);
780
781         if (ctx->ctrl_handler.error) {
782                 int err = ctx->ctrl_handler.error;
783
784                 dev_dbg(dev, "%s controls setup failed (%d)\n",
785                         ctx->name, err);
786                 v4l2_ctrl_handler_free(&ctx->ctrl_handler);
787                 return err;
788         }
789
790         v4l2_ctrl_handler_setup(&ctx->ctrl_handler);
791
792         /* set default time per frame */
793         ctx->ctrls.time_per_frame.numerator = HVA_DEFAULT_FRAME_NUM;
794         ctx->ctrls.time_per_frame.denominator = HVA_DEFAULT_FRAME_DEN;
795
796         return 0;
797 }
798
799 /*
800  * mem-to-mem operations
801  */
802
803 static void hva_run_work(struct work_struct *work)
804 {
805         struct hva_ctx *ctx = container_of(work, struct hva_ctx, run_work);
806         struct vb2_v4l2_buffer *src_buf, *dst_buf;
807         const struct hva_enc *enc = ctx->enc;
808         struct hva_frame *frame;
809         struct hva_stream *stream;
810         int ret;
811
812         /* protect instance against reentrancy */
813         mutex_lock(&ctx->lock);
814
815 #ifdef CONFIG_VIDEO_STI_HVA_DEBUGFS
816         hva_dbg_perf_begin(ctx);
817 #endif
818
819         src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
820         dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
821
822         frame = to_hva_frame(src_buf);
823         stream = to_hva_stream(dst_buf);
824         frame->vbuf.sequence = ctx->frame_num++;
825
826         ret = enc->encode(ctx, frame, stream);
827
828         vb2_set_plane_payload(&dst_buf->vb2_buf, 0, stream->bytesused);
829         if (ret) {
830                 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_ERROR);
831                 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_ERROR);
832         } else {
833                 /* propagate frame timestamp */
834                 dst_buf->vb2_buf.timestamp = src_buf->vb2_buf.timestamp;
835                 dst_buf->field = V4L2_FIELD_NONE;
836                 dst_buf->sequence = ctx->stream_num - 1;
837
838                 ctx->encoded_frames++;
839
840 #ifdef CONFIG_VIDEO_STI_HVA_DEBUGFS
841                 hva_dbg_perf_end(ctx, stream);
842 #endif
843
844                 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
845                 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
846         }
847
848         mutex_unlock(&ctx->lock);
849
850         v4l2_m2m_job_finish(ctx->hva_dev->m2m_dev, ctx->fh.m2m_ctx);
851 }
852
853 static void hva_device_run(void *priv)
854 {
855         struct hva_ctx *ctx = priv;
856         struct hva_dev *hva = ctx_to_hdev(ctx);
857
858         queue_work(hva->work_queue, &ctx->run_work);
859 }
860
861 static void hva_job_abort(void *priv)
862 {
863         struct hva_ctx *ctx = priv;
864         struct device *dev = ctx_to_dev(ctx);
865
866         dev_dbg(dev, "%s aborting job\n", ctx->name);
867
868         ctx->aborting = true;
869 }
870
871 static int hva_job_ready(void *priv)
872 {
873         struct hva_ctx *ctx = priv;
874         struct device *dev = ctx_to_dev(ctx);
875
876         if (!v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx)) {
877                 dev_dbg(dev, "%s job not ready: no frame buffers\n",
878                         ctx->name);
879                 return 0;
880         }
881
882         if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) {
883                 dev_dbg(dev, "%s job not ready: no stream buffers\n",
884                         ctx->name);
885                 return 0;
886         }
887
888         if (ctx->aborting) {
889                 dev_dbg(dev, "%s job not ready: aborting\n", ctx->name);
890                 return 0;
891         }
892
893         return 1;
894 }
895
896 /* mem-to-mem ops */
897 static const struct v4l2_m2m_ops hva_m2m_ops = {
898         .device_run     = hva_device_run,
899         .job_abort      = hva_job_abort,
900         .job_ready      = hva_job_ready,
901 };
902
903 /*
904  * VB2 queue operations
905  */
906
907 static int hva_queue_setup(struct vb2_queue *vq,
908                            unsigned int *num_buffers, unsigned int *num_planes,
909                            unsigned int sizes[], struct device *alloc_devs[])
910 {
911         struct hva_ctx *ctx = vb2_get_drv_priv(vq);
912         struct device *dev = ctx_to_dev(ctx);
913         unsigned int size;
914
915         dev_dbg(dev, "%s %s queue setup: num_buffers %d\n", ctx->name,
916                 to_type_str(vq->type), *num_buffers);
917
918         size = vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT ?
919                 ctx->frameinfo.size : ctx->max_stream_size;
920
921         if (*num_planes)
922                 return sizes[0] < size ? -EINVAL : 0;
923
924         /* only one plane supported */
925         *num_planes = 1;
926         sizes[0] = size;
927
928         return 0;
929 }
930
931 static int hva_buf_prepare(struct vb2_buffer *vb)
932 {
933         struct hva_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
934         struct device *dev = ctx_to_dev(ctx);
935         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
936
937         if (vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
938                 struct hva_frame *frame = to_hva_frame(vbuf);
939
940                 if (vbuf->field == V4L2_FIELD_ANY)
941                         vbuf->field = V4L2_FIELD_NONE;
942                 if (vbuf->field != V4L2_FIELD_NONE) {
943                         dev_dbg(dev,
944                                 "%s frame[%d] prepare: %d field not supported\n",
945                                 ctx->name, vb->index, vbuf->field);
946                         return -EINVAL;
947                 }
948
949                 if (!frame->prepared) {
950                         /* get memory addresses */
951                         frame->vaddr = vb2_plane_vaddr(&vbuf->vb2_buf, 0);
952                         frame->paddr = vb2_dma_contig_plane_dma_addr(
953                                         &vbuf->vb2_buf, 0);
954                         frame->info = ctx->frameinfo;
955                         frame->prepared = true;
956
957                         dev_dbg(dev,
958                                 "%s frame[%d] prepared; virt=%p, phy=%pad\n",
959                                 ctx->name, vb->index,
960                                 frame->vaddr, &frame->paddr);
961                 }
962         } else {
963                 struct hva_stream *stream = to_hva_stream(vbuf);
964
965                 if (!stream->prepared) {
966                         /* get memory addresses */
967                         stream->vaddr = vb2_plane_vaddr(&vbuf->vb2_buf, 0);
968                         stream->paddr = vb2_dma_contig_plane_dma_addr(
969                                         &vbuf->vb2_buf, 0);
970                         stream->size = vb2_plane_size(&vbuf->vb2_buf, 0);
971                         stream->prepared = true;
972
973                         dev_dbg(dev,
974                                 "%s stream[%d] prepared; virt=%p, phy=%pad\n",
975                                 ctx->name, vb->index,
976                                 stream->vaddr, &stream->paddr);
977                 }
978         }
979
980         return 0;
981 }
982
983 static void hva_buf_queue(struct vb2_buffer *vb)
984 {
985         struct hva_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
986         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
987
988         if (ctx->fh.m2m_ctx)
989                 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
990 }
991
992 static int hva_start_streaming(struct vb2_queue *vq, unsigned int count)
993 {
994         struct hva_ctx *ctx = vb2_get_drv_priv(vq);
995         struct hva_dev *hva = ctx_to_hdev(ctx);
996         struct device *dev = ctx_to_dev(ctx);
997         struct vb2_v4l2_buffer *vbuf;
998         int ret;
999         unsigned int i;
1000         bool found = false;
1001
1002         dev_dbg(dev, "%s %s start streaming\n", ctx->name,
1003                 to_type_str(vq->type));
1004
1005         /* open encoder when both start_streaming have been called */
1006         if (V4L2_TYPE_IS_OUTPUT(vq->type)) {
1007                 if (!vb2_start_streaming_called(&ctx->fh.m2m_ctx->cap_q_ctx.q))
1008                         return 0;
1009         } else {
1010                 if (!vb2_start_streaming_called(&ctx->fh.m2m_ctx->out_q_ctx.q))
1011                         return 0;
1012         }
1013
1014         /* store the instance context in the instances array */
1015         for (i = 0; i < HVA_MAX_INSTANCES; i++) {
1016                 if (!hva->instances[i]) {
1017                         hva->instances[i] = ctx;
1018                         /* save the context identifier in the context */
1019                         ctx->id = i;
1020                         found = true;
1021                         break;
1022                 }
1023         }
1024
1025         if (!found) {
1026                 dev_err(dev, "%s maximum instances reached\n", ctx->name);
1027                 ret = -ENOMEM;
1028                 goto err;
1029         }
1030
1031         hva->nb_of_instances++;
1032
1033         if (!ctx->enc) {
1034                 ret = hva_open_encoder(ctx,
1035                                        ctx->streaminfo.streamformat,
1036                                        ctx->frameinfo.pixelformat,
1037                                        &ctx->enc);
1038                 if (ret < 0)
1039                         goto err_ctx;
1040         }
1041
1042         return 0;
1043
1044 err_ctx:
1045         hva->instances[ctx->id] = NULL;
1046         hva->nb_of_instances--;
1047 err:
1048         if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1049                 /* return of all pending buffers to vb2 (in queued state) */
1050                 while ((vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
1051                         v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_QUEUED);
1052         } else {
1053                 /* return of all pending buffers to vb2 (in queued state) */
1054                 while ((vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
1055                         v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_QUEUED);
1056         }
1057
1058         ctx->sys_errors++;
1059
1060         return ret;
1061 }
1062
1063 static void hva_stop_streaming(struct vb2_queue *vq)
1064 {
1065         struct hva_ctx *ctx = vb2_get_drv_priv(vq);
1066         struct hva_dev *hva = ctx_to_hdev(ctx);
1067         struct device *dev = ctx_to_dev(ctx);
1068         const struct hva_enc *enc = ctx->enc;
1069         struct vb2_v4l2_buffer *vbuf;
1070
1071         dev_dbg(dev, "%s %s stop streaming\n", ctx->name,
1072                 to_type_str(vq->type));
1073
1074         if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1075                 /* return of all pending buffers to vb2 (in error state) */
1076                 ctx->frame_num = 0;
1077                 while ((vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
1078                         v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
1079         } else {
1080                 /* return of all pending buffers to vb2 (in error state) */
1081                 ctx->stream_num = 0;
1082                 while ((vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
1083                         v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
1084         }
1085
1086         if ((V4L2_TYPE_IS_OUTPUT(vq->type) &&
1087              vb2_is_streaming(&ctx->fh.m2m_ctx->cap_q_ctx.q)) ||
1088             (!V4L2_TYPE_IS_OUTPUT(vq->type) &&
1089              vb2_is_streaming(&ctx->fh.m2m_ctx->out_q_ctx.q))) {
1090                 dev_dbg(dev, "%s %s out=%d cap=%d\n",
1091                         ctx->name, to_type_str(vq->type),
1092                         vb2_is_streaming(&ctx->fh.m2m_ctx->out_q_ctx.q),
1093                         vb2_is_streaming(&ctx->fh.m2m_ctx->cap_q_ctx.q));
1094                 return;
1095         }
1096
1097         /* close encoder when both stop_streaming have been called */
1098         if (enc) {
1099                 dev_dbg(dev, "%s %s encoder closed\n", ctx->name, enc->name);
1100                 enc->close(ctx);
1101                 ctx->enc = NULL;
1102
1103                 /* clear instance context in instances array */
1104                 hva->instances[ctx->id] = NULL;
1105                 hva->nb_of_instances--;
1106         }
1107
1108         ctx->aborting = false;
1109 }
1110
1111 /* VB2 queue ops */
1112 static const struct vb2_ops hva_qops = {
1113         .queue_setup            = hva_queue_setup,
1114         .buf_prepare            = hva_buf_prepare,
1115         .buf_queue              = hva_buf_queue,
1116         .start_streaming        = hva_start_streaming,
1117         .stop_streaming         = hva_stop_streaming,
1118         .wait_prepare           = vb2_ops_wait_prepare,
1119         .wait_finish            = vb2_ops_wait_finish,
1120 };
1121
1122 /*
1123  * V4L2 file operations
1124  */
1125
1126 static int queue_init(struct hva_ctx *ctx, struct vb2_queue *vq)
1127 {
1128         vq->io_modes = VB2_MMAP | VB2_DMABUF;
1129         vq->drv_priv = ctx;
1130         vq->ops = &hva_qops;
1131         vq->mem_ops = &vb2_dma_contig_memops;
1132         vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1133         vq->lock = &ctx->hva_dev->lock;
1134
1135         return vb2_queue_init(vq);
1136 }
1137
1138 static int hva_queue_init(void *priv, struct vb2_queue *src_vq,
1139                           struct vb2_queue *dst_vq)
1140 {
1141         struct hva_ctx *ctx = priv;
1142         int ret;
1143
1144         src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1145         src_vq->buf_struct_size = sizeof(struct hva_frame);
1146         src_vq->min_buffers_needed = MIN_FRAMES;
1147         src_vq->dev = ctx->hva_dev->dev;
1148
1149         ret = queue_init(ctx, src_vq);
1150         if (ret)
1151                 return ret;
1152
1153         dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1154         dst_vq->buf_struct_size = sizeof(struct hva_stream);
1155         dst_vq->min_buffers_needed = MIN_STREAMS;
1156         dst_vq->dev = ctx->hva_dev->dev;
1157
1158         return queue_init(ctx, dst_vq);
1159 }
1160
1161 static int hva_open(struct file *file)
1162 {
1163         struct hva_dev *hva = video_drvdata(file);
1164         struct device *dev = hva_to_dev(hva);
1165         struct hva_ctx *ctx;
1166         int ret;
1167
1168         ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1169         if (!ctx) {
1170                 ret = -ENOMEM;
1171                 goto out;
1172         }
1173         ctx->hva_dev = hva;
1174
1175         INIT_WORK(&ctx->run_work, hva_run_work);
1176         v4l2_fh_init(&ctx->fh, video_devdata(file));
1177         file->private_data = &ctx->fh;
1178         v4l2_fh_add(&ctx->fh);
1179
1180         ret = hva_ctrls_setup(ctx);
1181         if (ret) {
1182                 dev_err(dev, "%s [x:x] failed to setup controls\n",
1183                         HVA_PREFIX);
1184                 ctx->sys_errors++;
1185                 goto err_fh;
1186         }
1187         ctx->fh.ctrl_handler = &ctx->ctrl_handler;
1188
1189         mutex_init(&ctx->lock);
1190
1191         ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(hva->m2m_dev, ctx,
1192                                             &hva_queue_init);
1193         if (IS_ERR(ctx->fh.m2m_ctx)) {
1194                 ret = PTR_ERR(ctx->fh.m2m_ctx);
1195                 dev_err(dev, "%s failed to initialize m2m context (%d)\n",
1196                         HVA_PREFIX, ret);
1197                 ctx->sys_errors++;
1198                 goto err_ctrls;
1199         }
1200
1201         /* set the instance name */
1202         mutex_lock(&hva->lock);
1203         hva->instance_id++;
1204         snprintf(ctx->name, sizeof(ctx->name), "[%3d:----]",
1205                  hva->instance_id);
1206         mutex_unlock(&hva->lock);
1207
1208         /* default parameters for frame and stream */
1209         set_default_params(ctx);
1210
1211 #ifdef CONFIG_VIDEO_STI_HVA_DEBUGFS
1212         hva_dbg_ctx_create(ctx);
1213 #endif
1214
1215         dev_info(dev, "%s encoder instance created\n", ctx->name);
1216
1217         return 0;
1218
1219 err_ctrls:
1220         v4l2_ctrl_handler_free(&ctx->ctrl_handler);
1221 err_fh:
1222         v4l2_fh_del(&ctx->fh);
1223         v4l2_fh_exit(&ctx->fh);
1224         kfree(ctx);
1225 out:
1226         return ret;
1227 }
1228
1229 static int hva_release(struct file *file)
1230 {
1231         struct hva_ctx *ctx = fh_to_ctx(file->private_data);
1232         struct hva_dev *hva = ctx_to_hdev(ctx);
1233         struct device *dev = ctx_to_dev(ctx);
1234         const struct hva_enc *enc = ctx->enc;
1235
1236         if (enc) {
1237                 dev_dbg(dev, "%s %s encoder closed\n", ctx->name, enc->name);
1238                 enc->close(ctx);
1239                 ctx->enc = NULL;
1240
1241                 /* clear instance context in instances array */
1242                 hva->instances[ctx->id] = NULL;
1243                 hva->nb_of_instances--;
1244         }
1245
1246         /* trace a summary of instance before closing (debug purpose) */
1247         hva_dbg_summary(ctx);
1248
1249         v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
1250
1251         v4l2_ctrl_handler_free(&ctx->ctrl_handler);
1252
1253         v4l2_fh_del(&ctx->fh);
1254         v4l2_fh_exit(&ctx->fh);
1255
1256 #ifdef CONFIG_VIDEO_STI_HVA_DEBUGFS
1257         hva_dbg_ctx_remove(ctx);
1258 #endif
1259
1260         dev_info(dev, "%s encoder instance released\n", ctx->name);
1261
1262         kfree(ctx);
1263
1264         return 0;
1265 }
1266
1267 /* V4L2 file ops */
1268 static const struct v4l2_file_operations hva_fops = {
1269         .owner                  = THIS_MODULE,
1270         .open                   = hva_open,
1271         .release                = hva_release,
1272         .unlocked_ioctl         = video_ioctl2,
1273         .mmap                   = v4l2_m2m_fop_mmap,
1274         .poll                   = v4l2_m2m_fop_poll,
1275 };
1276
1277 /*
1278  * Platform device operations
1279  */
1280
1281 static int hva_register_device(struct hva_dev *hva)
1282 {
1283         int ret;
1284         struct video_device *vdev;
1285         struct device *dev;
1286
1287         if (!hva)
1288                 return -ENODEV;
1289         dev = hva_to_dev(hva);
1290
1291         hva->m2m_dev = v4l2_m2m_init(&hva_m2m_ops);
1292         if (IS_ERR(hva->m2m_dev)) {
1293                 dev_err(dev, "%s failed to initialize v4l2-m2m device\n",
1294                         HVA_PREFIX);
1295                 ret = PTR_ERR(hva->m2m_dev);
1296                 goto err;
1297         }
1298
1299         vdev = video_device_alloc();
1300         if (!vdev) {
1301                 dev_err(dev, "%s failed to allocate video device\n",
1302                         HVA_PREFIX);
1303                 ret = -ENOMEM;
1304                 goto err_m2m_release;
1305         }
1306
1307         vdev->fops = &hva_fops;
1308         vdev->ioctl_ops = &hva_ioctl_ops;
1309         vdev->release = video_device_release;
1310         vdev->lock = &hva->lock;
1311         vdev->vfl_dir = VFL_DIR_M2M;
1312         vdev->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_M2M;
1313         vdev->v4l2_dev = &hva->v4l2_dev;
1314         snprintf(vdev->name, sizeof(vdev->name), "%s%lx", HVA_NAME,
1315                  hva->ip_version);
1316
1317         ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
1318         if (ret) {
1319                 dev_err(dev, "%s failed to register video device\n",
1320                         HVA_PREFIX);
1321                 goto err_vdev_release;
1322         }
1323
1324         hva->vdev = vdev;
1325         video_set_drvdata(vdev, hva);
1326         return 0;
1327
1328 err_vdev_release:
1329         video_device_release(vdev);
1330 err_m2m_release:
1331         v4l2_m2m_release(hva->m2m_dev);
1332 err:
1333         return ret;
1334 }
1335
1336 static void hva_unregister_device(struct hva_dev *hva)
1337 {
1338         if (!hva)
1339                 return;
1340
1341         if (hva->m2m_dev)
1342                 v4l2_m2m_release(hva->m2m_dev);
1343
1344         video_unregister_device(hva->vdev);
1345 }
1346
1347 static int hva_probe(struct platform_device *pdev)
1348 {
1349         struct hva_dev *hva;
1350         struct device *dev = &pdev->dev;
1351         int ret;
1352
1353         hva = devm_kzalloc(dev, sizeof(*hva), GFP_KERNEL);
1354         if (!hva) {
1355                 ret = -ENOMEM;
1356                 goto err;
1357         }
1358
1359         ret = dma_coerce_mask_and_coherent(dev, DMA_BIT_MASK(32));
1360         if (ret)
1361                 return ret;
1362
1363         hva->dev = dev;
1364         hva->pdev = pdev;
1365         platform_set_drvdata(pdev, hva);
1366
1367         mutex_init(&hva->lock);
1368
1369         /* probe hardware */
1370         ret = hva_hw_probe(pdev, hva);
1371         if (ret)
1372                 goto err;
1373
1374         /* register all available encoders */
1375         register_encoders(hva);
1376
1377         /* register all supported formats */
1378         register_formats(hva);
1379
1380         /* register on V4L2 */
1381         ret = v4l2_device_register(dev, &hva->v4l2_dev);
1382         if (ret) {
1383                 dev_err(dev, "%s %s failed to register V4L2 device\n",
1384                         HVA_PREFIX, HVA_NAME);
1385                 goto err_hw;
1386         }
1387
1388 #ifdef CONFIG_VIDEO_STI_HVA_DEBUGFS
1389         hva_debugfs_create(hva);
1390 #endif
1391
1392         hva->work_queue = create_workqueue(HVA_NAME);
1393         if (!hva->work_queue) {
1394                 dev_err(dev, "%s %s failed to allocate work queue\n",
1395                         HVA_PREFIX, HVA_NAME);
1396                 ret = -ENOMEM;
1397                 goto err_v4l2;
1398         }
1399
1400         /* register device */
1401         ret = hva_register_device(hva);
1402         if (ret)
1403                 goto err_work_queue;
1404
1405         dev_info(dev, "%s %s registered as /dev/video%d\n", HVA_PREFIX,
1406                  HVA_NAME, hva->vdev->num);
1407
1408         return 0;
1409
1410 err_work_queue:
1411         destroy_workqueue(hva->work_queue);
1412 err_v4l2:
1413 #ifdef CONFIG_VIDEO_STI_HVA_DEBUGFS
1414         hva_debugfs_remove(hva);
1415 #endif
1416         v4l2_device_unregister(&hva->v4l2_dev);
1417 err_hw:
1418         hva_hw_remove(hva);
1419 err:
1420         return ret;
1421 }
1422
1423 static int hva_remove(struct platform_device *pdev)
1424 {
1425         struct hva_dev *hva = platform_get_drvdata(pdev);
1426         struct device *dev = hva_to_dev(hva);
1427
1428         hva_unregister_device(hva);
1429
1430         destroy_workqueue(hva->work_queue);
1431
1432         hva_hw_remove(hva);
1433
1434 #ifdef CONFIG_VIDEO_STI_HVA_DEBUGFS
1435         hva_debugfs_remove(hva);
1436 #endif
1437
1438         v4l2_device_unregister(&hva->v4l2_dev);
1439
1440         dev_info(dev, "%s %s removed\n", HVA_PREFIX, pdev->name);
1441
1442         return 0;
1443 }
1444
1445 /* PM ops */
1446 static const struct dev_pm_ops hva_pm_ops = {
1447         .runtime_suspend        = hva_hw_runtime_suspend,
1448         .runtime_resume         = hva_hw_runtime_resume,
1449 };
1450
1451 static const struct of_device_id hva_match_types[] = {
1452         {
1453          .compatible = "st,st-hva",
1454         },
1455         { /* end node */ }
1456 };
1457
1458 MODULE_DEVICE_TABLE(of, hva_match_types);
1459
1460 static struct platform_driver hva_driver = {
1461         .probe  = hva_probe,
1462         .remove = hva_remove,
1463         .driver = {
1464                 .name           = HVA_NAME,
1465                 .of_match_table = hva_match_types,
1466                 .pm             = &hva_pm_ops,
1467                 },
1468 };
1469
1470 module_platform_driver(hva_driver);
1471
1472 MODULE_LICENSE("GPL");
1473 MODULE_AUTHOR("Yannick Fertre <yannick.fertre@st.com>");
1474 MODULE_DESCRIPTION("STMicroelectronics HVA video encoder V4L2 driver");