GNU Linux-libre 4.19.245-gnu1
[releases.git] / drivers / media / platform / qcom / venus / venc.c
1 /*
2  * Copyright (c) 2012-2016, The Linux Foundation. All rights reserved.
3  * Copyright (C) 2017 Linaro Ltd.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 and
7  * only version 2 as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  */
15 #include <linux/clk.h>
16 #include <linux/module.h>
17 #include <linux/mod_devicetable.h>
18 #include <linux/platform_device.h>
19 #include <linux/pm_runtime.h>
20 #include <linux/slab.h>
21 #include <media/v4l2-mem2mem.h>
22 #include <media/videobuf2-dma-sg.h>
23 #include <media/v4l2-ioctl.h>
24 #include <media/v4l2-event.h>
25 #include <media/v4l2-ctrls.h>
26
27 #include "hfi_venus_io.h"
28 #include "hfi_parser.h"
29 #include "core.h"
30 #include "helpers.h"
31 #include "venc.h"
32
33 #define NUM_B_FRAMES_MAX        4
34
35 /*
36  * Three resons to keep MPLANE formats (despite that the number of planes
37  * currently is one):
38  * - the MPLANE formats allow only one plane to be used
39  * - the downstream driver use MPLANE formats too
40  * - future firmware versions could add support for >1 planes
41  */
42 static const struct venus_format venc_formats[] = {
43         {
44                 .pixfmt = V4L2_PIX_FMT_NV12,
45                 .num_planes = 1,
46                 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
47         }, {
48                 .pixfmt = V4L2_PIX_FMT_MPEG4,
49                 .num_planes = 1,
50                 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
51         }, {
52                 .pixfmt = V4L2_PIX_FMT_H263,
53                 .num_planes = 1,
54                 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
55         }, {
56                 .pixfmt = V4L2_PIX_FMT_H264,
57                 .num_planes = 1,
58                 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
59         }, {
60                 .pixfmt = V4L2_PIX_FMT_VP8,
61                 .num_planes = 1,
62                 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
63         }, {
64                 .pixfmt = V4L2_PIX_FMT_HEVC,
65                 .num_planes = 1,
66                 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
67         },
68 };
69
70 static const struct venus_format *
71 find_format(struct venus_inst *inst, u32 pixfmt, u32 type)
72 {
73         const struct venus_format *fmt = venc_formats;
74         unsigned int size = ARRAY_SIZE(venc_formats);
75         unsigned int i;
76
77         for (i = 0; i < size; i++) {
78                 if (fmt[i].pixfmt == pixfmt)
79                         break;
80         }
81
82         if (i == size || fmt[i].type != type)
83                 return NULL;
84
85         if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
86             !venus_helper_check_codec(inst, fmt[i].pixfmt))
87                 return NULL;
88
89         return &fmt[i];
90 }
91
92 static const struct venus_format *
93 find_format_by_index(struct venus_inst *inst, unsigned int index, u32 type)
94 {
95         const struct venus_format *fmt = venc_formats;
96         unsigned int size = ARRAY_SIZE(venc_formats);
97         unsigned int i, k = 0;
98
99         if (index > size)
100                 return NULL;
101
102         for (i = 0; i < size; i++) {
103                 bool valid;
104
105                 if (fmt[i].type != type)
106                         continue;
107                 valid = type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ||
108                         venus_helper_check_codec(inst, fmt[i].pixfmt);
109                 if (k == index && valid)
110                         break;
111                 if (valid)
112                         k++;
113         }
114
115         if (i == size)
116                 return NULL;
117
118         return &fmt[i];
119 }
120
121 static int venc_v4l2_to_hfi(int id, int value)
122 {
123         switch (id) {
124         case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL:
125                 switch (value) {
126                 case V4L2_MPEG_VIDEO_MPEG4_LEVEL_0:
127                 default:
128                         return HFI_MPEG4_LEVEL_0;
129                 case V4L2_MPEG_VIDEO_MPEG4_LEVEL_0B:
130                         return HFI_MPEG4_LEVEL_0b;
131                 case V4L2_MPEG_VIDEO_MPEG4_LEVEL_1:
132                         return HFI_MPEG4_LEVEL_1;
133                 case V4L2_MPEG_VIDEO_MPEG4_LEVEL_2:
134                         return HFI_MPEG4_LEVEL_2;
135                 case V4L2_MPEG_VIDEO_MPEG4_LEVEL_3:
136                         return HFI_MPEG4_LEVEL_3;
137                 case V4L2_MPEG_VIDEO_MPEG4_LEVEL_4:
138                         return HFI_MPEG4_LEVEL_4;
139                 case V4L2_MPEG_VIDEO_MPEG4_LEVEL_5:
140                         return HFI_MPEG4_LEVEL_5;
141                 }
142         case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE:
143                 switch (value) {
144                 case V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE:
145                 default:
146                         return HFI_MPEG4_PROFILE_SIMPLE;
147                 case V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_SIMPLE:
148                         return HFI_MPEG4_PROFILE_ADVANCEDSIMPLE;
149                 }
150         case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
151                 switch (value) {
152                 case V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE:
153                         return HFI_H264_PROFILE_BASELINE;
154                 case V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE:
155                         return HFI_H264_PROFILE_CONSTRAINED_BASE;
156                 case V4L2_MPEG_VIDEO_H264_PROFILE_MAIN:
157                         return HFI_H264_PROFILE_MAIN;
158                 case V4L2_MPEG_VIDEO_H264_PROFILE_HIGH:
159                 default:
160                         return HFI_H264_PROFILE_HIGH;
161                 }
162         case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
163                 switch (value) {
164                 case V4L2_MPEG_VIDEO_H264_LEVEL_1_0:
165                         return HFI_H264_LEVEL_1;
166                 case V4L2_MPEG_VIDEO_H264_LEVEL_1B:
167                         return HFI_H264_LEVEL_1b;
168                 case V4L2_MPEG_VIDEO_H264_LEVEL_1_1:
169                         return HFI_H264_LEVEL_11;
170                 case V4L2_MPEG_VIDEO_H264_LEVEL_1_2:
171                         return HFI_H264_LEVEL_12;
172                 case V4L2_MPEG_VIDEO_H264_LEVEL_1_3:
173                         return HFI_H264_LEVEL_13;
174                 case V4L2_MPEG_VIDEO_H264_LEVEL_2_0:
175                         return HFI_H264_LEVEL_2;
176                 case V4L2_MPEG_VIDEO_H264_LEVEL_2_1:
177                         return HFI_H264_LEVEL_21;
178                 case V4L2_MPEG_VIDEO_H264_LEVEL_2_2:
179                         return HFI_H264_LEVEL_22;
180                 case V4L2_MPEG_VIDEO_H264_LEVEL_3_0:
181                         return HFI_H264_LEVEL_3;
182                 case V4L2_MPEG_VIDEO_H264_LEVEL_3_1:
183                         return HFI_H264_LEVEL_31;
184                 case V4L2_MPEG_VIDEO_H264_LEVEL_3_2:
185                         return HFI_H264_LEVEL_32;
186                 case V4L2_MPEG_VIDEO_H264_LEVEL_4_0:
187                         return HFI_H264_LEVEL_4;
188                 case V4L2_MPEG_VIDEO_H264_LEVEL_4_1:
189                         return HFI_H264_LEVEL_41;
190                 case V4L2_MPEG_VIDEO_H264_LEVEL_4_2:
191                         return HFI_H264_LEVEL_42;
192                 case V4L2_MPEG_VIDEO_H264_LEVEL_5_0:
193                 default:
194                         return HFI_H264_LEVEL_5;
195                 case V4L2_MPEG_VIDEO_H264_LEVEL_5_1:
196                         return HFI_H264_LEVEL_51;
197                 }
198         case V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE:
199                 switch (value) {
200                 case V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CAVLC:
201                 default:
202                         return HFI_H264_ENTROPY_CAVLC;
203                 case V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CABAC:
204                         return HFI_H264_ENTROPY_CABAC;
205                 }
206         case V4L2_CID_MPEG_VIDEO_VP8_PROFILE:
207                 switch (value) {
208                 case 0:
209                 default:
210                         return HFI_VPX_PROFILE_VERSION_0;
211                 case 1:
212                         return HFI_VPX_PROFILE_VERSION_1;
213                 case 2:
214                         return HFI_VPX_PROFILE_VERSION_2;
215                 case 3:
216                         return HFI_VPX_PROFILE_VERSION_3;
217                 }
218         case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
219                 switch (value) {
220                 case V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED:
221                 default:
222                         return HFI_H264_DB_MODE_ALL_BOUNDARY;
223                 case V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED:
224                         return HFI_H264_DB_MODE_DISABLE;
225                 case V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED_AT_SLICE_BOUNDARY:
226                         return HFI_H264_DB_MODE_SKIP_SLICE_BOUNDARY;
227                 }
228         case V4L2_CID_MPEG_VIDEO_HEVC_PROFILE:
229                 switch (value) {
230                 case V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN:
231                 default:
232                         return HFI_HEVC_PROFILE_MAIN;
233                 case V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_STILL_PICTURE:
234                         return HFI_HEVC_PROFILE_MAIN_STILL_PIC;
235                 case V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_10:
236                         return HFI_HEVC_PROFILE_MAIN10;
237                 }
238         case V4L2_CID_MPEG_VIDEO_HEVC_LEVEL:
239                 switch (value) {
240                 case V4L2_MPEG_VIDEO_HEVC_LEVEL_1:
241                 default:
242                         return HFI_HEVC_LEVEL_1;
243                 case V4L2_MPEG_VIDEO_HEVC_LEVEL_2:
244                         return HFI_HEVC_LEVEL_2;
245                 case V4L2_MPEG_VIDEO_HEVC_LEVEL_2_1:
246                         return HFI_HEVC_LEVEL_21;
247                 case V4L2_MPEG_VIDEO_HEVC_LEVEL_3:
248                         return HFI_HEVC_LEVEL_3;
249                 case V4L2_MPEG_VIDEO_HEVC_LEVEL_3_1:
250                         return HFI_HEVC_LEVEL_31;
251                 case V4L2_MPEG_VIDEO_HEVC_LEVEL_4:
252                         return HFI_HEVC_LEVEL_4;
253                 case V4L2_MPEG_VIDEO_HEVC_LEVEL_4_1:
254                         return HFI_HEVC_LEVEL_41;
255                 case V4L2_MPEG_VIDEO_HEVC_LEVEL_5:
256                         return HFI_HEVC_LEVEL_5;
257                 case V4L2_MPEG_VIDEO_HEVC_LEVEL_5_1:
258                         return HFI_HEVC_LEVEL_51;
259                 case V4L2_MPEG_VIDEO_HEVC_LEVEL_5_2:
260                         return HFI_HEVC_LEVEL_52;
261                 case V4L2_MPEG_VIDEO_HEVC_LEVEL_6:
262                         return HFI_HEVC_LEVEL_6;
263                 case V4L2_MPEG_VIDEO_HEVC_LEVEL_6_1:
264                         return HFI_HEVC_LEVEL_61;
265                 case V4L2_MPEG_VIDEO_HEVC_LEVEL_6_2:
266                         return HFI_HEVC_LEVEL_62;
267                 }
268         }
269
270         return 0;
271 }
272
273 static int
274 venc_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
275 {
276         strlcpy(cap->driver, "qcom-venus", sizeof(cap->driver));
277         strlcpy(cap->card, "Qualcomm Venus video encoder", sizeof(cap->card));
278         strlcpy(cap->bus_info, "platform:qcom-venus", sizeof(cap->bus_info));
279
280         return 0;
281 }
282
283 static int venc_enum_fmt(struct file *file, void *fh, struct v4l2_fmtdesc *f)
284 {
285         struct venus_inst *inst = to_inst(file);
286         const struct venus_format *fmt;
287
288         fmt = find_format_by_index(inst, f->index, f->type);
289
290         memset(f->reserved, 0, sizeof(f->reserved));
291
292         if (!fmt)
293                 return -EINVAL;
294
295         f->pixelformat = fmt->pixfmt;
296
297         return 0;
298 }
299
300 static const struct venus_format *
301 venc_try_fmt_common(struct venus_inst *inst, struct v4l2_format *f)
302 {
303         struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
304         struct v4l2_plane_pix_format *pfmt = pixmp->plane_fmt;
305         const struct venus_format *fmt;
306
307         memset(pfmt[0].reserved, 0, sizeof(pfmt[0].reserved));
308         memset(pixmp->reserved, 0, sizeof(pixmp->reserved));
309
310         fmt = find_format(inst, pixmp->pixelformat, f->type);
311         if (!fmt) {
312                 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
313                         pixmp->pixelformat = V4L2_PIX_FMT_H264;
314                 else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
315                         pixmp->pixelformat = V4L2_PIX_FMT_NV12;
316                 else
317                         return NULL;
318                 fmt = find_format(inst, pixmp->pixelformat, f->type);
319                 if (!fmt)
320                         return NULL;
321         }
322
323         pixmp->width = clamp(pixmp->width, frame_width_min(inst),
324                              frame_width_max(inst));
325         pixmp->height = clamp(pixmp->height, frame_height_min(inst),
326                               frame_height_max(inst));
327
328         if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
329                 pixmp->height = ALIGN(pixmp->height, 32);
330
331         pixmp->width = ALIGN(pixmp->width, 2);
332         pixmp->height = ALIGN(pixmp->height, 2);
333
334         if (pixmp->field == V4L2_FIELD_ANY)
335                 pixmp->field = V4L2_FIELD_NONE;
336         pixmp->num_planes = fmt->num_planes;
337         pixmp->flags = 0;
338
339         pfmt[0].sizeimage = venus_helper_get_framesz(pixmp->pixelformat,
340                                                      pixmp->width,
341                                                      pixmp->height);
342
343         if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
344                 pfmt[0].bytesperline = ALIGN(pixmp->width, 128);
345         else
346                 pfmt[0].bytesperline = 0;
347
348         return fmt;
349 }
350
351 static int venc_try_fmt(struct file *file, void *fh, struct v4l2_format *f)
352 {
353         struct venus_inst *inst = to_inst(file);
354
355         venc_try_fmt_common(inst, f);
356
357         return 0;
358 }
359
360 static int venc_s_fmt(struct file *file, void *fh, struct v4l2_format *f)
361 {
362         struct venus_inst *inst = to_inst(file);
363         struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
364         struct v4l2_pix_format_mplane orig_pixmp;
365         const struct venus_format *fmt;
366         struct v4l2_format format;
367         u32 pixfmt_out = 0, pixfmt_cap = 0;
368
369         orig_pixmp = *pixmp;
370
371         fmt = venc_try_fmt_common(inst, f);
372         if (!fmt)
373                 return -EINVAL;
374
375         if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
376                 pixfmt_out = pixmp->pixelformat;
377                 pixfmt_cap = inst->fmt_cap->pixfmt;
378         } else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
379                 pixfmt_cap = pixmp->pixelformat;
380                 pixfmt_out = inst->fmt_out->pixfmt;
381         }
382
383         memset(&format, 0, sizeof(format));
384
385         format.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
386         format.fmt.pix_mp.pixelformat = pixfmt_out;
387         format.fmt.pix_mp.width = orig_pixmp.width;
388         format.fmt.pix_mp.height = orig_pixmp.height;
389         venc_try_fmt_common(inst, &format);
390
391         if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
392                 inst->out_width = format.fmt.pix_mp.width;
393                 inst->out_height = format.fmt.pix_mp.height;
394                 inst->colorspace = pixmp->colorspace;
395                 inst->ycbcr_enc = pixmp->ycbcr_enc;
396                 inst->quantization = pixmp->quantization;
397                 inst->xfer_func = pixmp->xfer_func;
398         }
399
400         memset(&format, 0, sizeof(format));
401
402         format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
403         format.fmt.pix_mp.pixelformat = pixfmt_cap;
404         format.fmt.pix_mp.width = orig_pixmp.width;
405         format.fmt.pix_mp.height = orig_pixmp.height;
406         venc_try_fmt_common(inst, &format);
407
408         inst->width = format.fmt.pix_mp.width;
409         inst->height = format.fmt.pix_mp.height;
410
411         if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
412                 inst->fmt_out = fmt;
413         else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
414                 inst->fmt_cap = fmt;
415
416         return 0;
417 }
418
419 static int venc_g_fmt(struct file *file, void *fh, struct v4l2_format *f)
420 {
421         struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
422         struct venus_inst *inst = to_inst(file);
423         const struct venus_format *fmt;
424
425         if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
426                 fmt = inst->fmt_cap;
427         else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
428                 fmt = inst->fmt_out;
429         else
430                 return -EINVAL;
431
432         pixmp->pixelformat = fmt->pixfmt;
433
434         if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
435                 pixmp->width = inst->width;
436                 pixmp->height = inst->height;
437                 pixmp->colorspace = inst->colorspace;
438                 pixmp->ycbcr_enc = inst->ycbcr_enc;
439                 pixmp->quantization = inst->quantization;
440                 pixmp->xfer_func = inst->xfer_func;
441         } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
442                 pixmp->width = inst->out_width;
443                 pixmp->height = inst->out_height;
444         }
445
446         venc_try_fmt_common(inst, f);
447
448         return 0;
449 }
450
451 static int
452 venc_g_selection(struct file *file, void *fh, struct v4l2_selection *s)
453 {
454         struct venus_inst *inst = to_inst(file);
455
456         if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
457                 return -EINVAL;
458
459         switch (s->target) {
460         case V4L2_SEL_TGT_CROP_DEFAULT:
461         case V4L2_SEL_TGT_CROP_BOUNDS:
462                 s->r.width = inst->width;
463                 s->r.height = inst->height;
464                 break;
465         case V4L2_SEL_TGT_CROP:
466                 s->r.width = inst->out_width;
467                 s->r.height = inst->out_height;
468                 break;
469         default:
470                 return -EINVAL;
471         }
472
473         s->r.top = 0;
474         s->r.left = 0;
475
476         return 0;
477 }
478
479 static int
480 venc_s_selection(struct file *file, void *fh, struct v4l2_selection *s)
481 {
482         struct venus_inst *inst = to_inst(file);
483
484         if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
485                 return -EINVAL;
486
487         switch (s->target) {
488         case V4L2_SEL_TGT_CROP:
489                 if (s->r.width != inst->out_width ||
490                     s->r.height != inst->out_height ||
491                     s->r.top != 0 || s->r.left != 0)
492                         return -EINVAL;
493                 break;
494         default:
495                 return -EINVAL;
496         }
497
498         return 0;
499 }
500
501 static int venc_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
502 {
503         struct venus_inst *inst = to_inst(file);
504         struct v4l2_outputparm *out = &a->parm.output;
505         struct v4l2_fract *timeperframe = &out->timeperframe;
506         u64 us_per_frame, fps;
507
508         if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
509             a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
510                 return -EINVAL;
511
512         memset(out->reserved, 0, sizeof(out->reserved));
513
514         if (!timeperframe->denominator)
515                 timeperframe->denominator = inst->timeperframe.denominator;
516         if (!timeperframe->numerator)
517                 timeperframe->numerator = inst->timeperframe.numerator;
518
519         out->capability = V4L2_CAP_TIMEPERFRAME;
520
521         us_per_frame = timeperframe->numerator * (u64)USEC_PER_SEC;
522         do_div(us_per_frame, timeperframe->denominator);
523
524         if (!us_per_frame)
525                 return -EINVAL;
526
527         fps = (u64)USEC_PER_SEC;
528         do_div(fps, us_per_frame);
529
530         inst->timeperframe = *timeperframe;
531         inst->fps = fps;
532
533         return 0;
534 }
535
536 static int venc_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
537 {
538         struct venus_inst *inst = to_inst(file);
539
540         if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
541             a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
542                 return -EINVAL;
543
544         a->parm.output.capability |= V4L2_CAP_TIMEPERFRAME;
545         a->parm.output.timeperframe = inst->timeperframe;
546
547         return 0;
548 }
549
550 static int venc_enum_framesizes(struct file *file, void *fh,
551                                 struct v4l2_frmsizeenum *fsize)
552 {
553         struct venus_inst *inst = to_inst(file);
554         const struct venus_format *fmt;
555
556         fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
557
558         fmt = find_format(inst, fsize->pixel_format,
559                           V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
560         if (!fmt) {
561                 fmt = find_format(inst, fsize->pixel_format,
562                                   V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
563                 if (!fmt)
564                         return -EINVAL;
565         }
566
567         if (fsize->index)
568                 return -EINVAL;
569
570         fsize->stepwise.min_width = frame_width_min(inst);
571         fsize->stepwise.max_width = frame_width_max(inst);
572         fsize->stepwise.step_width = frame_width_step(inst);
573         fsize->stepwise.min_height = frame_height_min(inst);
574         fsize->stepwise.max_height = frame_height_max(inst);
575         fsize->stepwise.step_height = frame_height_step(inst);
576
577         return 0;
578 }
579
580 static int venc_enum_frameintervals(struct file *file, void *fh,
581                                     struct v4l2_frmivalenum *fival)
582 {
583         struct venus_inst *inst = to_inst(file);
584         const struct venus_format *fmt;
585
586         fival->type = V4L2_FRMIVAL_TYPE_STEPWISE;
587
588         fmt = find_format(inst, fival->pixel_format,
589                           V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
590         if (!fmt) {
591                 fmt = find_format(inst, fival->pixel_format,
592                                   V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
593                 if (!fmt)
594                         return -EINVAL;
595         }
596
597         if (fival->index)
598                 return -EINVAL;
599
600         if (!fival->width || !fival->height)
601                 return -EINVAL;
602
603         if (fival->width > frame_width_max(inst) ||
604             fival->width < frame_width_min(inst) ||
605             fival->height > frame_height_max(inst) ||
606             fival->height < frame_height_min(inst))
607                 return -EINVAL;
608
609         fival->stepwise.min.numerator = 1;
610         fival->stepwise.min.denominator = frate_max(inst);
611         fival->stepwise.max.numerator = 1;
612         fival->stepwise.max.denominator = frate_min(inst);
613         fival->stepwise.step.numerator = 1;
614         fival->stepwise.step.denominator = frate_max(inst);
615
616         return 0;
617 }
618
619 static const struct v4l2_ioctl_ops venc_ioctl_ops = {
620         .vidioc_querycap = venc_querycap,
621         .vidioc_enum_fmt_vid_cap_mplane = venc_enum_fmt,
622         .vidioc_enum_fmt_vid_out_mplane = venc_enum_fmt,
623         .vidioc_s_fmt_vid_cap_mplane = venc_s_fmt,
624         .vidioc_s_fmt_vid_out_mplane = venc_s_fmt,
625         .vidioc_g_fmt_vid_cap_mplane = venc_g_fmt,
626         .vidioc_g_fmt_vid_out_mplane = venc_g_fmt,
627         .vidioc_try_fmt_vid_cap_mplane = venc_try_fmt,
628         .vidioc_try_fmt_vid_out_mplane = venc_try_fmt,
629         .vidioc_g_selection = venc_g_selection,
630         .vidioc_s_selection = venc_s_selection,
631         .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
632         .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
633         .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
634         .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
635         .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
636         .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
637         .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
638         .vidioc_streamon = v4l2_m2m_ioctl_streamon,
639         .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
640         .vidioc_s_parm = venc_s_parm,
641         .vidioc_g_parm = venc_g_parm,
642         .vidioc_enum_framesizes = venc_enum_framesizes,
643         .vidioc_enum_frameintervals = venc_enum_frameintervals,
644         .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
645         .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
646 };
647
648 static int venc_set_properties(struct venus_inst *inst)
649 {
650         struct venc_controls *ctr = &inst->controls.enc;
651         struct hfi_intra_period intra_period;
652         struct hfi_profile_level pl;
653         struct hfi_framerate frate;
654         struct hfi_bitrate brate;
655         struct hfi_idr_period idrp;
656         u32 ptype, rate_control, bitrate, profile = 0, level = 0;
657         int ret;
658
659         ret = venus_helper_set_work_mode(inst, VIDC_WORK_MODE_2);
660         if (ret)
661                 return ret;
662
663         ret = venus_helper_set_core_usage(inst, VIDC_CORE_ID_2);
664         if (ret)
665                 return ret;
666
667         ptype = HFI_PROPERTY_CONFIG_FRAME_RATE;
668         frate.buffer_type = HFI_BUFFER_OUTPUT;
669         frate.framerate = inst->fps * (1 << 16);
670
671         ret = hfi_session_set_property(inst, ptype, &frate);
672         if (ret)
673                 return ret;
674
675         if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_H264) {
676                 struct hfi_h264_vui_timing_info info;
677                 struct hfi_h264_entropy_control entropy;
678                 struct hfi_h264_db_control deblock;
679
680                 ptype = HFI_PROPERTY_PARAM_VENC_H264_VUI_TIMING_INFO;
681                 info.enable = 1;
682                 info.fixed_framerate = 1;
683                 info.time_scale = NSEC_PER_SEC;
684
685                 ret = hfi_session_set_property(inst, ptype, &info);
686                 if (ret)
687                         return ret;
688
689                 ptype = HFI_PROPERTY_PARAM_VENC_H264_ENTROPY_CONTROL;
690                 entropy.entropy_mode = venc_v4l2_to_hfi(
691                                           V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE,
692                                           ctr->h264_entropy_mode);
693                 entropy.cabac_model = HFI_H264_CABAC_MODEL_0;
694
695                 ret = hfi_session_set_property(inst, ptype, &entropy);
696                 if (ret)
697                         return ret;
698
699                 ptype = HFI_PROPERTY_PARAM_VENC_H264_DEBLOCK_CONTROL;
700                 deblock.mode = venc_v4l2_to_hfi(
701                                       V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
702                                       ctr->h264_loop_filter_mode);
703                 deblock.slice_alpha_offset = ctr->h264_loop_filter_alpha;
704                 deblock.slice_beta_offset = ctr->h264_loop_filter_beta;
705
706                 ret = hfi_session_set_property(inst, ptype, &deblock);
707                 if (ret)
708                         return ret;
709         }
710
711         /* IDR periodicity, n:
712          * n = 0 - only the first I-frame is IDR frame
713          * n = 1 - all I-frames will be IDR frames
714          * n > 1 - every n-th I-frame will be IDR frame
715          */
716         ptype = HFI_PROPERTY_CONFIG_VENC_IDR_PERIOD;
717         idrp.idr_period = 0;
718         ret = hfi_session_set_property(inst, ptype, &idrp);
719         if (ret)
720                 return ret;
721
722         if (ctr->num_b_frames) {
723                 u32 max_num_b_frames = NUM_B_FRAMES_MAX;
724
725                 ptype = HFI_PROPERTY_PARAM_VENC_MAX_NUM_B_FRAMES;
726                 ret = hfi_session_set_property(inst, ptype, &max_num_b_frames);
727                 if (ret)
728                         return ret;
729         }
730
731         ptype = HFI_PROPERTY_CONFIG_VENC_INTRA_PERIOD;
732         intra_period.pframes = ctr->num_p_frames;
733         intra_period.bframes = ctr->num_b_frames;
734
735         ret = hfi_session_set_property(inst, ptype, &intra_period);
736         if (ret)
737                 return ret;
738
739         if (ctr->bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR)
740                 rate_control = HFI_RATE_CONTROL_VBR_CFR;
741         else
742                 rate_control = HFI_RATE_CONTROL_CBR_CFR;
743
744         ptype = HFI_PROPERTY_PARAM_VENC_RATE_CONTROL;
745         ret = hfi_session_set_property(inst, ptype, &rate_control);
746         if (ret)
747                 return ret;
748
749         if (!ctr->bitrate)
750                 bitrate = 64000;
751         else
752                 bitrate = ctr->bitrate;
753
754         ptype = HFI_PROPERTY_CONFIG_VENC_TARGET_BITRATE;
755         brate.bitrate = bitrate;
756         brate.layer_id = 0;
757
758         ret = hfi_session_set_property(inst, ptype, &brate);
759         if (ret)
760                 return ret;
761
762         if (!ctr->bitrate_peak)
763                 bitrate *= 2;
764         else
765                 bitrate = ctr->bitrate_peak;
766
767         ptype = HFI_PROPERTY_CONFIG_VENC_MAX_BITRATE;
768         brate.bitrate = bitrate;
769         brate.layer_id = 0;
770
771         ret = hfi_session_set_property(inst, ptype, &brate);
772         if (ret)
773                 return ret;
774
775         if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_H264) {
776                 profile = venc_v4l2_to_hfi(V4L2_CID_MPEG_VIDEO_H264_PROFILE,
777                                            ctr->profile.h264);
778                 level = venc_v4l2_to_hfi(V4L2_CID_MPEG_VIDEO_H264_LEVEL,
779                                          ctr->level.h264);
780         } else if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_VP8) {
781                 profile = venc_v4l2_to_hfi(V4L2_CID_MPEG_VIDEO_VP8_PROFILE,
782                                            ctr->profile.vpx);
783                 level = 0;
784         } else if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_MPEG4) {
785                 profile = venc_v4l2_to_hfi(V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE,
786                                            ctr->profile.mpeg4);
787                 level = venc_v4l2_to_hfi(V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL,
788                                          ctr->level.mpeg4);
789         } else if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_H263) {
790                 profile = 0;
791                 level = 0;
792         } else if (inst->fmt_cap->pixfmt == V4L2_PIX_FMT_HEVC) {
793                 profile = venc_v4l2_to_hfi(V4L2_CID_MPEG_VIDEO_HEVC_PROFILE,
794                                            ctr->profile.hevc);
795                 level = venc_v4l2_to_hfi(V4L2_CID_MPEG_VIDEO_HEVC_LEVEL,
796                                          ctr->level.hevc);
797         }
798
799         ptype = HFI_PROPERTY_PARAM_PROFILE_LEVEL_CURRENT;
800         pl.profile = profile;
801         pl.level = level;
802
803         ret = hfi_session_set_property(inst, ptype, &pl);
804         if (ret)
805                 return ret;
806
807         return 0;
808 }
809
810 static int venc_init_session(struct venus_inst *inst)
811 {
812         int ret;
813
814         ret = hfi_session_init(inst, inst->fmt_cap->pixfmt);
815         if (ret)
816                 return ret;
817
818         ret = venus_helper_set_input_resolution(inst, inst->width,
819                                                 inst->height);
820         if (ret)
821                 goto deinit;
822
823         ret = venus_helper_set_output_resolution(inst, inst->width,
824                                                  inst->height,
825                                                  HFI_BUFFER_OUTPUT);
826         if (ret)
827                 goto deinit;
828
829         ret = venus_helper_set_color_format(inst, inst->fmt_out->pixfmt);
830         if (ret)
831                 goto deinit;
832
833         ret = venc_set_properties(inst);
834         if (ret)
835                 goto deinit;
836
837         return 0;
838 deinit:
839         hfi_session_deinit(inst);
840         return ret;
841 }
842
843 static int venc_out_num_buffers(struct venus_inst *inst, unsigned int *num)
844 {
845         struct hfi_buffer_requirements bufreq;
846         int ret;
847
848         ret = venc_init_session(inst);
849         if (ret)
850                 return ret;
851
852         ret = venus_helper_get_bufreq(inst, HFI_BUFFER_INPUT, &bufreq);
853
854         *num = bufreq.count_actual;
855
856         hfi_session_deinit(inst);
857
858         return ret;
859 }
860
861 static int venc_queue_setup(struct vb2_queue *q,
862                             unsigned int *num_buffers, unsigned int *num_planes,
863                             unsigned int sizes[], struct device *alloc_devs[])
864 {
865         struct venus_inst *inst = vb2_get_drv_priv(q);
866         unsigned int num, min = 4;
867         int ret = 0;
868
869         if (*num_planes) {
870                 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
871                     *num_planes != inst->fmt_out->num_planes)
872                         return -EINVAL;
873
874                 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
875                     *num_planes != inst->fmt_cap->num_planes)
876                         return -EINVAL;
877
878                 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
879                     sizes[0] < inst->input_buf_size)
880                         return -EINVAL;
881
882                 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
883                     sizes[0] < inst->output_buf_size)
884                         return -EINVAL;
885
886                 return 0;
887         }
888
889         switch (q->type) {
890         case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
891                 *num_planes = inst->fmt_out->num_planes;
892
893                 ret = venc_out_num_buffers(inst, &num);
894                 if (ret)
895                         break;
896
897                 num = max(num, min);
898                 *num_buffers = max(*num_buffers, num);
899                 inst->num_input_bufs = *num_buffers;
900
901                 sizes[0] = venus_helper_get_framesz(inst->fmt_out->pixfmt,
902                                                     inst->width,
903                                                     inst->height);
904                 inst->input_buf_size = sizes[0];
905                 break;
906         case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
907                 *num_planes = inst->fmt_cap->num_planes;
908                 *num_buffers = max(*num_buffers, min);
909                 inst->num_output_bufs = *num_buffers;
910                 sizes[0] = venus_helper_get_framesz(inst->fmt_cap->pixfmt,
911                                                     inst->width,
912                                                     inst->height);
913                 inst->output_buf_size = sizes[0];
914                 break;
915         default:
916                 ret = -EINVAL;
917                 break;
918         }
919
920         return ret;
921 }
922
923 static int venc_verify_conf(struct venus_inst *inst)
924 {
925         enum hfi_version ver = inst->core->res->hfi_version;
926         struct hfi_buffer_requirements bufreq;
927         int ret;
928
929         if (!inst->num_input_bufs || !inst->num_output_bufs)
930                 return -EINVAL;
931
932         ret = venus_helper_get_bufreq(inst, HFI_BUFFER_OUTPUT, &bufreq);
933         if (ret)
934                 return ret;
935
936         if (inst->num_output_bufs < bufreq.count_actual ||
937             inst->num_output_bufs < HFI_BUFREQ_COUNT_MIN(&bufreq, ver))
938                 return -EINVAL;
939
940         ret = venus_helper_get_bufreq(inst, HFI_BUFFER_INPUT, &bufreq);
941         if (ret)
942                 return ret;
943
944         if (inst->num_input_bufs < bufreq.count_actual ||
945             inst->num_input_bufs < HFI_BUFREQ_COUNT_MIN(&bufreq, ver))
946                 return -EINVAL;
947
948         return 0;
949 }
950
951 static int venc_start_streaming(struct vb2_queue *q, unsigned int count)
952 {
953         struct venus_inst *inst = vb2_get_drv_priv(q);
954         int ret;
955
956         mutex_lock(&inst->lock);
957
958         if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
959                 inst->streamon_out = 1;
960         else
961                 inst->streamon_cap = 1;
962
963         if (!(inst->streamon_out & inst->streamon_cap)) {
964                 mutex_unlock(&inst->lock);
965                 return 0;
966         }
967
968         venus_helper_init_instance(inst);
969
970         inst->sequence_cap = 0;
971         inst->sequence_out = 0;
972
973         ret = venc_init_session(inst);
974         if (ret)
975                 goto bufs_done;
976
977         ret = venc_set_properties(inst);
978         if (ret)
979                 goto deinit_sess;
980
981         ret = venc_verify_conf(inst);
982         if (ret)
983                 goto deinit_sess;
984
985         ret = venus_helper_set_num_bufs(inst, inst->num_input_bufs,
986                                         inst->num_output_bufs, 0);
987         if (ret)
988                 goto deinit_sess;
989
990         ret = venus_helper_vb2_start_streaming(inst);
991         if (ret)
992                 goto deinit_sess;
993
994         mutex_unlock(&inst->lock);
995
996         return 0;
997
998 deinit_sess:
999         hfi_session_deinit(inst);
1000 bufs_done:
1001         venus_helper_buffers_done(inst, VB2_BUF_STATE_QUEUED);
1002         if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
1003                 inst->streamon_out = 0;
1004         else
1005                 inst->streamon_cap = 0;
1006         mutex_unlock(&inst->lock);
1007         return ret;
1008 }
1009
1010 static const struct vb2_ops venc_vb2_ops = {
1011         .queue_setup = venc_queue_setup,
1012         .buf_init = venus_helper_vb2_buf_init,
1013         .buf_prepare = venus_helper_vb2_buf_prepare,
1014         .start_streaming = venc_start_streaming,
1015         .stop_streaming = venus_helper_vb2_stop_streaming,
1016         .buf_queue = venus_helper_vb2_buf_queue,
1017 };
1018
1019 static void venc_buf_done(struct venus_inst *inst, unsigned int buf_type,
1020                           u32 tag, u32 bytesused, u32 data_offset, u32 flags,
1021                           u32 hfi_flags, u64 timestamp_us)
1022 {
1023         struct vb2_v4l2_buffer *vbuf;
1024         struct vb2_buffer *vb;
1025         unsigned int type;
1026
1027         if (buf_type == HFI_BUFFER_INPUT)
1028                 type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1029         else
1030                 type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1031
1032         vbuf = venus_helper_find_buf(inst, type, tag);
1033         if (!vbuf)
1034                 return;
1035
1036         vbuf->flags = flags;
1037
1038         if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1039                 vb = &vbuf->vb2_buf;
1040                 vb2_set_plane_payload(vb, 0, bytesused + data_offset);
1041                 vb->planes[0].data_offset = data_offset;
1042                 vb->timestamp = timestamp_us * NSEC_PER_USEC;
1043                 vbuf->sequence = inst->sequence_cap++;
1044         } else {
1045                 vbuf->sequence = inst->sequence_out++;
1046         }
1047
1048         v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_DONE);
1049 }
1050
1051 static void venc_event_notify(struct venus_inst *inst, u32 event,
1052                               struct hfi_event_data *data)
1053 {
1054         struct device *dev = inst->core->dev_enc;
1055
1056         if (event == EVT_SESSION_ERROR) {
1057                 inst->session_error = true;
1058                 dev_err(dev, "enc: event session error %x\n", inst->error);
1059         }
1060 }
1061
1062 static const struct hfi_inst_ops venc_hfi_ops = {
1063         .buf_done = venc_buf_done,
1064         .event_notify = venc_event_notify,
1065 };
1066
1067 static const struct v4l2_m2m_ops venc_m2m_ops = {
1068         .device_run = venus_helper_m2m_device_run,
1069         .job_abort = venus_helper_m2m_job_abort,
1070 };
1071
1072 static int m2m_queue_init(void *priv, struct vb2_queue *src_vq,
1073                           struct vb2_queue *dst_vq)
1074 {
1075         struct venus_inst *inst = priv;
1076         int ret;
1077
1078         src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1079         src_vq->io_modes = VB2_MMAP | VB2_DMABUF;
1080         src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1081         src_vq->ops = &venc_vb2_ops;
1082         src_vq->mem_ops = &vb2_dma_sg_memops;
1083         src_vq->drv_priv = inst;
1084         src_vq->buf_struct_size = sizeof(struct venus_buffer);
1085         src_vq->allow_zero_bytesused = 1;
1086         src_vq->min_buffers_needed = 1;
1087         src_vq->dev = inst->core->dev;
1088         if (inst->core->res->hfi_version == HFI_VERSION_1XX)
1089                 src_vq->bidirectional = 1;
1090         ret = vb2_queue_init(src_vq);
1091         if (ret)
1092                 return ret;
1093
1094         dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1095         dst_vq->io_modes = VB2_MMAP | VB2_DMABUF;
1096         dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1097         dst_vq->ops = &venc_vb2_ops;
1098         dst_vq->mem_ops = &vb2_dma_sg_memops;
1099         dst_vq->drv_priv = inst;
1100         dst_vq->buf_struct_size = sizeof(struct venus_buffer);
1101         dst_vq->allow_zero_bytesused = 1;
1102         dst_vq->min_buffers_needed = 1;
1103         dst_vq->dev = inst->core->dev;
1104         ret = vb2_queue_init(dst_vq);
1105         if (ret) {
1106                 vb2_queue_release(src_vq);
1107                 return ret;
1108         }
1109
1110         return 0;
1111 }
1112
1113 static void venc_inst_init(struct venus_inst *inst)
1114 {
1115         inst->fmt_cap = &venc_formats[2];
1116         inst->fmt_out = &venc_formats[0];
1117         inst->width = 1280;
1118         inst->height = ALIGN(720, 32);
1119         inst->out_width = 1280;
1120         inst->out_height = 720;
1121         inst->fps = 15;
1122         inst->timeperframe.numerator = 1;
1123         inst->timeperframe.denominator = 15;
1124         inst->hfi_codec = HFI_VIDEO_CODEC_H264;
1125 }
1126
1127 static int venc_open(struct file *file)
1128 {
1129         struct venus_core *core = video_drvdata(file);
1130         struct venus_inst *inst;
1131         int ret;
1132
1133         inst = kzalloc(sizeof(*inst), GFP_KERNEL);
1134         if (!inst)
1135                 return -ENOMEM;
1136
1137         INIT_LIST_HEAD(&inst->dpbbufs);
1138         INIT_LIST_HEAD(&inst->registeredbufs);
1139         INIT_LIST_HEAD(&inst->internalbufs);
1140         INIT_LIST_HEAD(&inst->list);
1141         mutex_init(&inst->lock);
1142
1143         inst->core = core;
1144         inst->session_type = VIDC_SESSION_TYPE_ENC;
1145
1146         venus_helper_init_instance(inst);
1147
1148         ret = pm_runtime_get_sync(core->dev_enc);
1149         if (ret < 0)
1150                 goto err_free_inst;
1151
1152         ret = venc_ctrl_init(inst);
1153         if (ret)
1154                 goto err_put_sync;
1155
1156         ret = hfi_session_create(inst, &venc_hfi_ops);
1157         if (ret)
1158                 goto err_ctrl_deinit;
1159
1160         venc_inst_init(inst);
1161
1162         /*
1163          * create m2m device for every instance, the m2m context scheduling
1164          * is made by firmware side so we do not need to care about.
1165          */
1166         inst->m2m_dev = v4l2_m2m_init(&venc_m2m_ops);
1167         if (IS_ERR(inst->m2m_dev)) {
1168                 ret = PTR_ERR(inst->m2m_dev);
1169                 goto err_session_destroy;
1170         }
1171
1172         inst->m2m_ctx = v4l2_m2m_ctx_init(inst->m2m_dev, inst, m2m_queue_init);
1173         if (IS_ERR(inst->m2m_ctx)) {
1174                 ret = PTR_ERR(inst->m2m_ctx);
1175                 goto err_m2m_release;
1176         }
1177
1178         v4l2_fh_init(&inst->fh, core->vdev_enc);
1179
1180         inst->fh.ctrl_handler = &inst->ctrl_handler;
1181         v4l2_fh_add(&inst->fh);
1182         inst->fh.m2m_ctx = inst->m2m_ctx;
1183         file->private_data = &inst->fh;
1184
1185         return 0;
1186
1187 err_m2m_release:
1188         v4l2_m2m_release(inst->m2m_dev);
1189 err_session_destroy:
1190         hfi_session_destroy(inst);
1191 err_ctrl_deinit:
1192         venc_ctrl_deinit(inst);
1193 err_put_sync:
1194         pm_runtime_put_sync(core->dev_enc);
1195 err_free_inst:
1196         kfree(inst);
1197         return ret;
1198 }
1199
1200 static int venc_close(struct file *file)
1201 {
1202         struct venus_inst *inst = to_inst(file);
1203
1204         v4l2_m2m_ctx_release(inst->m2m_ctx);
1205         v4l2_m2m_release(inst->m2m_dev);
1206         venc_ctrl_deinit(inst);
1207         hfi_session_destroy(inst);
1208         mutex_destroy(&inst->lock);
1209         v4l2_fh_del(&inst->fh);
1210         v4l2_fh_exit(&inst->fh);
1211
1212         pm_runtime_put_sync(inst->core->dev_enc);
1213
1214         kfree(inst);
1215         return 0;
1216 }
1217
1218 static const struct v4l2_file_operations venc_fops = {
1219         .owner = THIS_MODULE,
1220         .open = venc_open,
1221         .release = venc_close,
1222         .unlocked_ioctl = video_ioctl2,
1223         .poll = v4l2_m2m_fop_poll,
1224         .mmap = v4l2_m2m_fop_mmap,
1225 };
1226
1227 static int venc_probe(struct platform_device *pdev)
1228 {
1229         struct device *dev = &pdev->dev;
1230         struct video_device *vdev;
1231         struct venus_core *core;
1232         int ret;
1233
1234         if (!dev->parent)
1235                 return -EPROBE_DEFER;
1236
1237         core = dev_get_drvdata(dev->parent);
1238         if (!core)
1239                 return -EPROBE_DEFER;
1240
1241         if (IS_V3(core) || IS_V4(core)) {
1242                 core->core1_clk = devm_clk_get(dev, "core");
1243                 if (IS_ERR(core->core1_clk))
1244                         return PTR_ERR(core->core1_clk);
1245         }
1246
1247         if (IS_V4(core)) {
1248                 core->core1_bus_clk = devm_clk_get(dev, "bus");
1249                 if (IS_ERR(core->core1_bus_clk))
1250                         return PTR_ERR(core->core1_bus_clk);
1251         }
1252
1253         platform_set_drvdata(pdev, core);
1254
1255         vdev = video_device_alloc();
1256         if (!vdev)
1257                 return -ENOMEM;
1258
1259         strlcpy(vdev->name, "qcom-venus-encoder", sizeof(vdev->name));
1260         vdev->release = video_device_release;
1261         vdev->fops = &venc_fops;
1262         vdev->ioctl_ops = &venc_ioctl_ops;
1263         vdev->vfl_dir = VFL_DIR_M2M;
1264         vdev->v4l2_dev = &core->v4l2_dev;
1265         vdev->device_caps = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING;
1266
1267         ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
1268         if (ret)
1269                 goto err_vdev_release;
1270
1271         core->vdev_enc = vdev;
1272         core->dev_enc = dev;
1273
1274         video_set_drvdata(vdev, core);
1275         pm_runtime_enable(dev);
1276
1277         return 0;
1278
1279 err_vdev_release:
1280         video_device_release(vdev);
1281         return ret;
1282 }
1283
1284 static int venc_remove(struct platform_device *pdev)
1285 {
1286         struct venus_core *core = dev_get_drvdata(pdev->dev.parent);
1287
1288         video_unregister_device(core->vdev_enc);
1289         pm_runtime_disable(core->dev_enc);
1290
1291         return 0;
1292 }
1293
1294 static __maybe_unused int venc_runtime_suspend(struct device *dev)
1295 {
1296         struct venus_core *core = dev_get_drvdata(dev);
1297         int ret;
1298
1299         if (IS_V1(core))
1300                 return 0;
1301
1302         ret = venus_helper_power_enable(core, VIDC_SESSION_TYPE_ENC, true);
1303         if (ret)
1304                 return ret;
1305
1306         if (IS_V4(core))
1307                 clk_disable_unprepare(core->core1_bus_clk);
1308
1309         clk_disable_unprepare(core->core1_clk);
1310
1311         return venus_helper_power_enable(core, VIDC_SESSION_TYPE_ENC, false);
1312 }
1313
1314 static __maybe_unused int venc_runtime_resume(struct device *dev)
1315 {
1316         struct venus_core *core = dev_get_drvdata(dev);
1317         int ret;
1318
1319         if (IS_V1(core))
1320                 return 0;
1321
1322         ret = venus_helper_power_enable(core, VIDC_SESSION_TYPE_ENC, true);
1323         if (ret)
1324                 return ret;
1325
1326         ret = clk_prepare_enable(core->core1_clk);
1327         if (ret)
1328                 goto err_power_disable;
1329
1330         if (IS_V4(core))
1331                 ret = clk_prepare_enable(core->core1_bus_clk);
1332
1333         if (ret)
1334                 goto err_unprepare_core1;
1335
1336         return venus_helper_power_enable(core, VIDC_SESSION_TYPE_ENC, false);
1337
1338 err_unprepare_core1:
1339         clk_disable_unprepare(core->core1_clk);
1340 err_power_disable:
1341         venus_helper_power_enable(core, VIDC_SESSION_TYPE_ENC, false);
1342         return ret;
1343 }
1344
1345 static const struct dev_pm_ops venc_pm_ops = {
1346         SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1347                                 pm_runtime_force_resume)
1348         SET_RUNTIME_PM_OPS(venc_runtime_suspend, venc_runtime_resume, NULL)
1349 };
1350
1351 static const struct of_device_id venc_dt_match[] = {
1352         { .compatible = "venus-encoder" },
1353         { }
1354 };
1355 MODULE_DEVICE_TABLE(of, venc_dt_match);
1356
1357 static struct platform_driver qcom_venus_enc_driver = {
1358         .probe = venc_probe,
1359         .remove = venc_remove,
1360         .driver = {
1361                 .name = "qcom-venus-encoder",
1362                 .of_match_table = venc_dt_match,
1363                 .pm = &venc_pm_ops,
1364         },
1365 };
1366 module_platform_driver(qcom_venus_enc_driver);
1367
1368 MODULE_ALIAS("platform:qcom-venus-encoder");
1369 MODULE_DESCRIPTION("Qualcomm Venus video encoder driver");
1370 MODULE_LICENSE("GPL v2");