s5p_mfc_dec.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  1. /*
  2. * linux/drivers/media/video/s5p-mfc/s5p_mfc_dec.c
  3. *
  4. * Copyright (C) 2011 Samsung Electronics Co., Ltd.
  5. * http://www.samsung.com/
  6. * Kamil Debski, <k.debski@samsung.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/io.h>
  16. #include <linux/module.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/sched.h>
  19. #include <linux/slab.h>
  20. #include <linux/version.h>
  21. #include <linux/videodev2.h>
  22. #include <linux/workqueue.h>
  23. #include <media/v4l2-ctrls.h>
  24. #include <media/videobuf2-core.h>
  25. #include "regs-mfc.h"
  26. #include "s5p_mfc_common.h"
  27. #include "s5p_mfc_debug.h"
  28. #include "s5p_mfc_dec.h"
  29. #include "s5p_mfc_intr.h"
  30. #include "s5p_mfc_opr.h"
  31. #include "s5p_mfc_pm.h"
  32. #include "s5p_mfc_shm.h"
  33. static struct s5p_mfc_fmt formats[] = {
  34. {
  35. .name = "4:2:0 2 Planes 64x32 Tiles",
  36. .fourcc = V4L2_PIX_FMT_NV12MT,
  37. .codec_mode = S5P_FIMV_CODEC_NONE,
  38. .type = MFC_FMT_RAW,
  39. .num_planes = 2,
  40. },
  41. {
  42. .name = "4:2:0 2 Planes",
  43. .fourcc = V4L2_PIX_FMT_NV12M,
  44. .codec_mode = S5P_FIMV_CODEC_NONE,
  45. .type = MFC_FMT_RAW,
  46. .num_planes = 2,
  47. },
  48. {
  49. .name = "H264 Encoded Stream",
  50. .fourcc = V4L2_PIX_FMT_H264,
  51. .codec_mode = S5P_FIMV_CODEC_H264_DEC,
  52. .type = MFC_FMT_DEC,
  53. .num_planes = 1,
  54. },
  55. {
  56. .name = "H263 Encoded Stream",
  57. .fourcc = V4L2_PIX_FMT_H263,
  58. .codec_mode = S5P_FIMV_CODEC_H263_DEC,
  59. .type = MFC_FMT_DEC,
  60. .num_planes = 1,
  61. },
  62. {
  63. .name = "MPEG1 Encoded Stream",
  64. .fourcc = V4L2_PIX_FMT_MPEG1,
  65. .codec_mode = S5P_FIMV_CODEC_MPEG2_DEC,
  66. .type = MFC_FMT_DEC,
  67. .num_planes = 1,
  68. },
  69. {
  70. .name = "MPEG2 Encoded Stream",
  71. .fourcc = V4L2_PIX_FMT_MPEG2,
  72. .codec_mode = S5P_FIMV_CODEC_MPEG2_DEC,
  73. .type = MFC_FMT_DEC,
  74. .num_planes = 1,
  75. },
  76. {
  77. .name = "MPEG4 Encoded Stream",
  78. .fourcc = V4L2_PIX_FMT_MPEG4,
  79. .codec_mode = S5P_FIMV_CODEC_MPEG4_DEC,
  80. .type = MFC_FMT_DEC,
  81. .num_planes = 1,
  82. },
  83. {
  84. .name = "XviD Encoded Stream",
  85. .fourcc = V4L2_PIX_FMT_XVID,
  86. .codec_mode = S5P_FIMV_CODEC_MPEG4_DEC,
  87. .type = MFC_FMT_DEC,
  88. .num_planes = 1,
  89. },
  90. {
  91. .name = "VC1 Encoded Stream",
  92. .fourcc = V4L2_PIX_FMT_VC1_ANNEX_G,
  93. .codec_mode = S5P_FIMV_CODEC_VC1_DEC,
  94. .type = MFC_FMT_DEC,
  95. .num_planes = 1,
  96. },
  97. {
  98. .name = "VC1 RCV Encoded Stream",
  99. .fourcc = V4L2_PIX_FMT_VC1_ANNEX_L,
  100. .codec_mode = S5P_FIMV_CODEC_VC1RCV_DEC,
  101. .type = MFC_FMT_DEC,
  102. .num_planes = 1,
  103. },
  104. };
  105. #define NUM_FORMATS ARRAY_SIZE(formats)
  106. /* Find selected format description */
  107. static struct s5p_mfc_fmt *find_format(struct v4l2_format *f, unsigned int t)
  108. {
  109. unsigned int i;
  110. for (i = 0; i < NUM_FORMATS; i++) {
  111. if (formats[i].fourcc == f->fmt.pix_mp.pixelformat &&
  112. formats[i].type == t)
  113. return &formats[i];
  114. }
  115. return NULL;
  116. }
  117. static struct mfc_control controls[] = {
  118. {
  119. .id = V4L2_CID_MPEG_MFC51_VIDEO_DECODER_H264_DISPLAY_DELAY,
  120. .type = V4L2_CTRL_TYPE_INTEGER,
  121. .name = "H264 Display Delay",
  122. .minimum = 0,
  123. .maximum = 16383,
  124. .step = 1,
  125. .default_value = 0,
  126. },
  127. {
  128. .id = V4L2_CID_MPEG_MFC51_VIDEO_DECODER_H264_DISPLAY_DELAY_ENABLE,
  129. .type = V4L2_CTRL_TYPE_BOOLEAN,
  130. .name = "H264 Display Delay Enable",
  131. .minimum = 0,
  132. .maximum = 1,
  133. .step = 1,
  134. .default_value = 0,
  135. },
  136. {
  137. .id = V4L2_CID_MPEG_VIDEO_DECODER_MPEG4_DEBLOCK_FILTER,
  138. .type = V4L2_CTRL_TYPE_BOOLEAN,
  139. .name = "Mpeg4 Loop Filter Enable",
  140. .minimum = 0,
  141. .maximum = 1,
  142. .step = 1,
  143. .default_value = 0,
  144. },
  145. {
  146. .id = V4L2_CID_MPEG_VIDEO_DECODER_SLICE_INTERFACE,
  147. .type = V4L2_CTRL_TYPE_BOOLEAN,
  148. .name = "Slice Interface Enable",
  149. .minimum = 0,
  150. .maximum = 1,
  151. .step = 1,
  152. .default_value = 0,
  153. },
  154. {
  155. .id = V4L2_CID_MIN_BUFFERS_FOR_CAPTURE,
  156. .type = V4L2_CTRL_TYPE_INTEGER,
  157. .name = "Minimum number of cap bufs",
  158. .minimum = 1,
  159. .maximum = 32,
  160. .step = 1,
  161. .default_value = 1,
  162. .is_volatile = 1,
  163. },
  164. };
  165. #define NUM_CTRLS ARRAY_SIZE(controls)
  166. /* Check whether a context should be run on hardware */
  167. static int s5p_mfc_ctx_ready(struct s5p_mfc_ctx *ctx)
  168. {
  169. /* Context is to parse header */
  170. if (ctx->src_queue_cnt >= 1 && ctx->state == MFCINST_GOT_INST)
  171. return 1;
  172. /* Context is to decode a frame */
  173. if (ctx->src_queue_cnt >= 1 &&
  174. ctx->state == MFCINST_RUNNING &&
  175. ctx->dst_queue_cnt >= ctx->dpb_count)
  176. return 1;
  177. /* Context is to return last frame */
  178. if (ctx->state == MFCINST_FINISHING &&
  179. ctx->dst_queue_cnt >= ctx->dpb_count)
  180. return 1;
  181. /* Context is to set buffers */
  182. if (ctx->src_queue_cnt >= 1 &&
  183. ctx->state == MFCINST_HEAD_PARSED &&
  184. ctx->capture_state == QUEUE_BUFS_MMAPED)
  185. return 1;
  186. /* Resolution change */
  187. if ((ctx->state == MFCINST_RES_CHANGE_INIT ||
  188. ctx->state == MFCINST_RES_CHANGE_FLUSH) &&
  189. ctx->dst_queue_cnt >= ctx->dpb_count)
  190. return 1;
  191. if (ctx->state == MFCINST_RES_CHANGE_END &&
  192. ctx->src_queue_cnt >= 1)
  193. return 1;
  194. mfc_debug(2, "ctx is not ready\n");
  195. return 0;
  196. }
  197. static struct s5p_mfc_codec_ops decoder_codec_ops = {
  198. .pre_seq_start = NULL,
  199. .post_seq_start = NULL,
  200. .pre_frame_start = NULL,
  201. .post_frame_start = NULL,
  202. };
  203. /* Query capabilities of the device */
  204. static int vidioc_querycap(struct file *file, void *priv,
  205. struct v4l2_capability *cap)
  206. {
  207. struct s5p_mfc_dev *dev = video_drvdata(file);
  208. strncpy(cap->driver, dev->plat_dev->name, sizeof(cap->driver) - 1);
  209. strncpy(cap->card, dev->plat_dev->name, sizeof(cap->card) - 1);
  210. cap->bus_info[0] = 0;
  211. cap->version = KERNEL_VERSION(1, 0, 0);
  212. /*
  213. * This is only a mem-to-mem video device. The capture and output
  214. * device capability flags are left only for backward compatibility
  215. * and are scheduled for removal.
  216. */
  217. cap->capabilities = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING |
  218. V4L2_CAP_VIDEO_CAPTURE_MPLANE |
  219. V4L2_CAP_VIDEO_OUTPUT_MPLANE;
  220. return 0;
  221. }
  222. /* Enumerate format */
  223. static int vidioc_enum_fmt(struct v4l2_fmtdesc *f, bool mplane, bool out)
  224. {
  225. struct s5p_mfc_fmt *fmt;
  226. int i, j = 0;
  227. for (i = 0; i < ARRAY_SIZE(formats); ++i) {
  228. if (mplane && formats[i].num_planes == 1)
  229. continue;
  230. else if (!mplane && formats[i].num_planes > 1)
  231. continue;
  232. if (out && formats[i].type != MFC_FMT_DEC)
  233. continue;
  234. else if (!out && formats[i].type != MFC_FMT_RAW)
  235. continue;
  236. if (j == f->index)
  237. break;
  238. ++j;
  239. }
  240. if (i == ARRAY_SIZE(formats))
  241. return -EINVAL;
  242. fmt = &formats[i];
  243. strlcpy(f->description, fmt->name, sizeof(f->description));
  244. f->pixelformat = fmt->fourcc;
  245. return 0;
  246. }
  247. static int vidioc_enum_fmt_vid_cap(struct file *file, void *pirv,
  248. struct v4l2_fmtdesc *f)
  249. {
  250. return vidioc_enum_fmt(f, false, false);
  251. }
  252. static int vidioc_enum_fmt_vid_cap_mplane(struct file *file, void *pirv,
  253. struct v4l2_fmtdesc *f)
  254. {
  255. return vidioc_enum_fmt(f, true, false);
  256. }
  257. static int vidioc_enum_fmt_vid_out(struct file *file, void *prov,
  258. struct v4l2_fmtdesc *f)
  259. {
  260. return vidioc_enum_fmt(f, false, true);
  261. }
  262. static int vidioc_enum_fmt_vid_out_mplane(struct file *file, void *prov,
  263. struct v4l2_fmtdesc *f)
  264. {
  265. return vidioc_enum_fmt(f, true, true);
  266. }
  267. /* Get format */
  268. static int vidioc_g_fmt(struct file *file, void *priv, struct v4l2_format *f)
  269. {
  270. struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
  271. struct v4l2_pix_format_mplane *pix_mp;
  272. mfc_debug_enter();
  273. pix_mp = &f->fmt.pix_mp;
  274. if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
  275. (ctx->state == MFCINST_GOT_INST || ctx->state ==
  276. MFCINST_RES_CHANGE_END)) {
  277. /* If the MFC is parsing the header,
  278. * so wait until it is finished */
  279. s5p_mfc_clean_ctx_int_flags(ctx);
  280. s5p_mfc_wait_for_done_ctx(ctx, S5P_FIMV_R2H_CMD_SEQ_DONE_RET,
  281. 0);
  282. }
  283. if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
  284. ctx->state >= MFCINST_HEAD_PARSED &&
  285. ctx->state < MFCINST_ABORT) {
  286. /* This is run on CAPTURE (decode output) */
  287. /* Width and height are set to the dimensions
  288. of the movie, the buffer is bigger and
  289. further processing stages should crop to this
  290. rectangle. */
  291. pix_mp->width = ctx->buf_width;
  292. pix_mp->height = ctx->buf_height;
  293. pix_mp->field = V4L2_FIELD_NONE;
  294. pix_mp->num_planes = 2;
  295. /* Set pixelformat to the format in which MFC
  296. outputs the decoded frame */
  297. pix_mp->pixelformat = V4L2_PIX_FMT_NV12MT;
  298. pix_mp->plane_fmt[0].bytesperline = ctx->buf_width;
  299. pix_mp->plane_fmt[0].sizeimage = ctx->luma_size;
  300. pix_mp->plane_fmt[1].bytesperline = ctx->buf_width;
  301. pix_mp->plane_fmt[1].sizeimage = ctx->chroma_size;
  302. } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  303. /* This is run on OUTPUT
  304. The buffer contains compressed image
  305. so width and height have no meaning */
  306. pix_mp->width = 0;
  307. pix_mp->height = 0;
  308. pix_mp->field = V4L2_FIELD_NONE;
  309. pix_mp->plane_fmt[0].bytesperline = ctx->dec_src_buf_size;
  310. pix_mp->plane_fmt[0].sizeimage = ctx->dec_src_buf_size;
  311. pix_mp->pixelformat = ctx->src_fmt->fourcc;
  312. pix_mp->num_planes = ctx->src_fmt->num_planes;
  313. } else {
  314. mfc_err("Format could not be read\n");
  315. mfc_debug(2, "%s-- with error\n", __func__);
  316. return -EINVAL;
  317. }
  318. mfc_debug_leave();
  319. return 0;
  320. }
  321. /* Try format */
  322. static int vidioc_try_fmt(struct file *file, void *priv, struct v4l2_format *f)
  323. {
  324. struct s5p_mfc_fmt *fmt;
  325. if (f->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  326. mfc_err("This node supports decoding only\n");
  327. return -EINVAL;
  328. }
  329. fmt = find_format(f, MFC_FMT_DEC);
  330. if (!fmt) {
  331. mfc_err("Unsupported format\n");
  332. return -EINVAL;
  333. }
  334. if (fmt->type != MFC_FMT_DEC) {
  335. mfc_err("\n");
  336. return -EINVAL;
  337. }
  338. return 0;
  339. }
  340. /* Set format */
  341. static int vidioc_s_fmt(struct file *file, void *priv, struct v4l2_format *f)
  342. {
  343. struct s5p_mfc_dev *dev = video_drvdata(file);
  344. struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
  345. int ret = 0;
  346. struct s5p_mfc_fmt *fmt;
  347. struct v4l2_pix_format_mplane *pix_mp;
  348. mfc_debug_enter();
  349. ret = vidioc_try_fmt(file, priv, f);
  350. pix_mp = &f->fmt.pix_mp;
  351. if (ret)
  352. return ret;
  353. if (ctx->vq_src.streaming || ctx->vq_dst.streaming) {
  354. v4l2_err(&dev->v4l2_dev, "%s queue busy\n", __func__);
  355. ret = -EBUSY;
  356. goto out;
  357. }
  358. fmt = find_format(f, MFC_FMT_DEC);
  359. if (!fmt || fmt->codec_mode == S5P_FIMV_CODEC_NONE) {
  360. mfc_err("Unknown codec\n");
  361. ret = -EINVAL;
  362. goto out;
  363. }
  364. if (fmt->type != MFC_FMT_DEC) {
  365. mfc_err("Wrong format selected, you should choose "
  366. "format for decoding\n");
  367. ret = -EINVAL;
  368. goto out;
  369. }
  370. ctx->src_fmt = fmt;
  371. ctx->codec_mode = fmt->codec_mode;
  372. mfc_debug(2, "The codec number is: %d\n", ctx->codec_mode);
  373. pix_mp->height = 0;
  374. pix_mp->width = 0;
  375. if (pix_mp->plane_fmt[0].sizeimage)
  376. ctx->dec_src_buf_size = pix_mp->plane_fmt[0].sizeimage;
  377. else
  378. pix_mp->plane_fmt[0].sizeimage = ctx->dec_src_buf_size =
  379. DEF_CPB_SIZE;
  380. pix_mp->plane_fmt[0].bytesperline = 0;
  381. ctx->state = MFCINST_INIT;
  382. out:
  383. mfc_debug_leave();
  384. return ret;
  385. }
  386. /* Reqeust buffers */
  387. static int vidioc_reqbufs(struct file *file, void *priv,
  388. struct v4l2_requestbuffers *reqbufs)
  389. {
  390. struct s5p_mfc_dev *dev = video_drvdata(file);
  391. struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
  392. int ret = 0;
  393. unsigned long flags;
  394. if (reqbufs->memory != V4L2_MEMORY_MMAP) {
  395. mfc_err("Only V4L2_MEMORY_MAP is supported\n");
  396. return -EINVAL;
  397. }
  398. if (reqbufs->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  399. /* Can only request buffers after an instance has been opened.*/
  400. if (ctx->state == MFCINST_INIT) {
  401. ctx->src_bufs_cnt = 0;
  402. if (reqbufs->count == 0) {
  403. mfc_debug(2, "Freeing buffers\n");
  404. s5p_mfc_clock_on();
  405. ret = vb2_reqbufs(&ctx->vq_src, reqbufs);
  406. s5p_mfc_clock_off();
  407. return ret;
  408. }
  409. /* Decoding */
  410. if (ctx->output_state != QUEUE_FREE) {
  411. mfc_err("Bufs have already been requested\n");
  412. return -EINVAL;
  413. }
  414. s5p_mfc_clock_on();
  415. ret = vb2_reqbufs(&ctx->vq_src, reqbufs);
  416. s5p_mfc_clock_off();
  417. if (ret) {
  418. mfc_err("vb2_reqbufs on output failed\n");
  419. return ret;
  420. }
  421. mfc_debug(2, "vb2_reqbufs: %d\n", ret);
  422. ctx->output_state = QUEUE_BUFS_REQUESTED;
  423. }
  424. } else if (reqbufs->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  425. ctx->dst_bufs_cnt = 0;
  426. if (reqbufs->count == 0) {
  427. mfc_debug(2, "Freeing buffers\n");
  428. s5p_mfc_clock_on();
  429. ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
  430. s5p_mfc_clock_off();
  431. return ret;
  432. }
  433. if (ctx->capture_state != QUEUE_FREE) {
  434. mfc_err("Bufs have already been requested\n");
  435. return -EINVAL;
  436. }
  437. ctx->capture_state = QUEUE_BUFS_REQUESTED;
  438. s5p_mfc_clock_on();
  439. ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
  440. s5p_mfc_clock_off();
  441. if (ret) {
  442. mfc_err("vb2_reqbufs on capture failed\n");
  443. return ret;
  444. }
  445. if (reqbufs->count < ctx->dpb_count) {
  446. mfc_err("Not enough buffers allocated\n");
  447. reqbufs->count = 0;
  448. s5p_mfc_clock_on();
  449. ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
  450. s5p_mfc_clock_off();
  451. return -ENOMEM;
  452. }
  453. ctx->total_dpb_count = reqbufs->count;
  454. ret = s5p_mfc_alloc_codec_buffers(ctx);
  455. if (ret) {
  456. mfc_err("Failed to allocate decoding buffers\n");
  457. reqbufs->count = 0;
  458. s5p_mfc_clock_on();
  459. ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
  460. s5p_mfc_clock_off();
  461. return -ENOMEM;
  462. }
  463. if (ctx->dst_bufs_cnt == ctx->total_dpb_count) {
  464. ctx->capture_state = QUEUE_BUFS_MMAPED;
  465. } else {
  466. mfc_err("Not all buffers passed to buf_init\n");
  467. reqbufs->count = 0;
  468. s5p_mfc_clock_on();
  469. ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
  470. s5p_mfc_release_codec_buffers(ctx);
  471. s5p_mfc_clock_off();
  472. return -ENOMEM;
  473. }
  474. if (s5p_mfc_ctx_ready(ctx)) {
  475. spin_lock_irqsave(&dev->condlock, flags);
  476. set_bit(ctx->num, &dev->ctx_work_bits);
  477. spin_unlock_irqrestore(&dev->condlock, flags);
  478. }
  479. s5p_mfc_try_run(dev);
  480. s5p_mfc_wait_for_done_ctx(ctx,
  481. S5P_FIMV_R2H_CMD_INIT_BUFFERS_RET, 0);
  482. }
  483. return ret;
  484. }
  485. /* Query buffer */
  486. static int vidioc_querybuf(struct file *file, void *priv,
  487. struct v4l2_buffer *buf)
  488. {
  489. struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
  490. int ret;
  491. int i;
  492. if (buf->memory != V4L2_MEMORY_MMAP) {
  493. mfc_err("Only mmaped buffers can be used\n");
  494. return -EINVAL;
  495. }
  496. mfc_debug(2, "State: %d, buf->type: %d\n", ctx->state, buf->type);
  497. if (ctx->state == MFCINST_INIT &&
  498. buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  499. ret = vb2_querybuf(&ctx->vq_src, buf);
  500. } else if (ctx->state == MFCINST_RUNNING &&
  501. buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  502. ret = vb2_querybuf(&ctx->vq_dst, buf);
  503. for (i = 0; i < buf->length; i++)
  504. buf->m.planes[i].m.mem_offset += DST_QUEUE_OFF_BASE;
  505. } else {
  506. mfc_err("vidioc_querybuf called in an inappropriate state\n");
  507. ret = -EINVAL;
  508. }
  509. mfc_debug_leave();
  510. return ret;
  511. }
  512. /* Queue a buffer */
  513. static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
  514. {
  515. struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
  516. if (ctx->state == MFCINST_ERROR) {
  517. mfc_err("Call on QBUF after unrecoverable error\n");
  518. return -EIO;
  519. }
  520. if (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  521. return vb2_qbuf(&ctx->vq_src, buf);
  522. else if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  523. return vb2_qbuf(&ctx->vq_dst, buf);
  524. return -EINVAL;
  525. }
  526. /* Dequeue a buffer */
  527. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
  528. {
  529. struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
  530. if (ctx->state == MFCINST_ERROR) {
  531. mfc_err("Call on DQBUF after unrecoverable error\n");
  532. return -EIO;
  533. }
  534. if (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  535. return vb2_dqbuf(&ctx->vq_src, buf, file->f_flags & O_NONBLOCK);
  536. else if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  537. return vb2_dqbuf(&ctx->vq_dst, buf, file->f_flags & O_NONBLOCK);
  538. return -EINVAL;
  539. }
  540. /* Stream on */
  541. static int vidioc_streamon(struct file *file, void *priv,
  542. enum v4l2_buf_type type)
  543. {
  544. struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
  545. struct s5p_mfc_dev *dev = ctx->dev;
  546. unsigned long flags;
  547. int ret = -EINVAL;
  548. mfc_debug_enter();
  549. if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  550. if (ctx->state == MFCINST_INIT) {
  551. ctx->dst_bufs_cnt = 0;
  552. ctx->src_bufs_cnt = 0;
  553. ctx->capture_state = QUEUE_FREE;
  554. ctx->output_state = QUEUE_FREE;
  555. s5p_mfc_alloc_instance_buffer(ctx);
  556. s5p_mfc_alloc_dec_temp_buffers(ctx);
  557. spin_lock_irqsave(&dev->condlock, flags);
  558. set_bit(ctx->num, &dev->ctx_work_bits);
  559. spin_unlock_irqrestore(&dev->condlock, flags);
  560. s5p_mfc_clean_ctx_int_flags(ctx);
  561. s5p_mfc_try_run(dev);
  562. if (s5p_mfc_wait_for_done_ctx(ctx,
  563. S5P_FIMV_R2H_CMD_OPEN_INSTANCE_RET, 0)) {
  564. /* Error or timeout */
  565. mfc_err("Error getting instance from hardware\n");
  566. s5p_mfc_release_instance_buffer(ctx);
  567. s5p_mfc_release_dec_desc_buffer(ctx);
  568. return -EIO;
  569. }
  570. mfc_debug(2, "Got instance number: %d\n", ctx->inst_no);
  571. }
  572. ret = vb2_streamon(&ctx->vq_src, type);
  573. }
  574. else if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  575. ret = vb2_streamon(&ctx->vq_dst, type);
  576. mfc_debug_leave();
  577. return ret;
  578. }
  579. /* Stream off, which equals to a pause */
  580. static int vidioc_streamoff(struct file *file, void *priv,
  581. enum v4l2_buf_type type)
  582. {
  583. struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
  584. if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  585. return vb2_streamoff(&ctx->vq_src, type);
  586. else if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  587. return vb2_streamoff(&ctx->vq_dst, type);
  588. return -EINVAL;
  589. }
  590. /* Set controls - v4l2 control framework */
  591. static int s5p_mfc_dec_s_ctrl(struct v4l2_ctrl *ctrl)
  592. {
  593. struct s5p_mfc_ctx *ctx = ctrl_to_ctx(ctrl);
  594. switch (ctrl->id) {
  595. case V4L2_CID_MPEG_MFC51_VIDEO_DECODER_H264_DISPLAY_DELAY:
  596. ctx->display_delay = ctrl->val;
  597. break;
  598. case V4L2_CID_MPEG_MFC51_VIDEO_DECODER_H264_DISPLAY_DELAY_ENABLE:
  599. ctx->display_delay_enable = ctrl->val;
  600. break;
  601. case V4L2_CID_MPEG_VIDEO_DECODER_MPEG4_DEBLOCK_FILTER:
  602. ctx->loop_filter_mpeg4 = ctrl->val;
  603. break;
  604. case V4L2_CID_MPEG_VIDEO_DECODER_SLICE_INTERFACE:
  605. ctx->slice_interface = ctrl->val;
  606. break;
  607. default:
  608. mfc_err("Invalid control 0x%08x\n", ctrl->id);
  609. return -EINVAL;
  610. }
  611. return 0;
  612. }
  613. static int s5p_mfc_dec_g_v_ctrl(struct v4l2_ctrl *ctrl)
  614. {
  615. struct s5p_mfc_ctx *ctx = ctrl_to_ctx(ctrl);
  616. struct s5p_mfc_dev *dev = ctx->dev;
  617. switch (ctrl->id) {
  618. case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE:
  619. if (ctx->state >= MFCINST_HEAD_PARSED &&
  620. ctx->state < MFCINST_ABORT) {
  621. ctrl->val = ctx->dpb_count;
  622. break;
  623. } else if (ctx->state != MFCINST_INIT) {
  624. v4l2_err(&dev->v4l2_dev, "Decoding not initialised\n");
  625. return -EINVAL;
  626. }
  627. /* Should wait for the header to be parsed */
  628. s5p_mfc_clean_ctx_int_flags(ctx);
  629. s5p_mfc_wait_for_done_ctx(ctx,
  630. S5P_FIMV_R2H_CMD_SEQ_DONE_RET, 0);
  631. if (ctx->state >= MFCINST_HEAD_PARSED &&
  632. ctx->state < MFCINST_ABORT) {
  633. ctrl->val = ctx->dpb_count;
  634. } else {
  635. v4l2_err(&dev->v4l2_dev, "Decoding not initialised\n");
  636. return -EINVAL;
  637. }
  638. break;
  639. }
  640. return 0;
  641. }
  642. static const struct v4l2_ctrl_ops s5p_mfc_dec_ctrl_ops = {
  643. .s_ctrl = s5p_mfc_dec_s_ctrl,
  644. .g_volatile_ctrl = s5p_mfc_dec_g_v_ctrl,
  645. };
  646. /* Get cropping information */
  647. static int vidioc_g_crop(struct file *file, void *priv,
  648. struct v4l2_crop *cr)
  649. {
  650. struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
  651. u32 left, right, top, bottom;
  652. if (ctx->state != MFCINST_HEAD_PARSED &&
  653. ctx->state != MFCINST_RUNNING && ctx->state != MFCINST_FINISHING
  654. && ctx->state != MFCINST_FINISHED) {
  655. mfc_err("Cannont set crop\n");
  656. return -EINVAL;
  657. }
  658. if (ctx->src_fmt->fourcc == V4L2_PIX_FMT_H264) {
  659. left = s5p_mfc_read_shm(ctx, CROP_INFO_H);
  660. right = left >> S5P_FIMV_SHARED_CROP_RIGHT_SHIFT;
  661. left = left & S5P_FIMV_SHARED_CROP_LEFT_MASK;
  662. top = s5p_mfc_read_shm(ctx, CROP_INFO_V);
  663. bottom = top >> S5P_FIMV_SHARED_CROP_BOTTOM_SHIFT;
  664. top = top & S5P_FIMV_SHARED_CROP_TOP_MASK;
  665. cr->c.left = left;
  666. cr->c.top = top;
  667. cr->c.width = ctx->img_width - left - right;
  668. cr->c.height = ctx->img_height - top - bottom;
  669. mfc_debug(2, "Cropping info [h264]: l=%d t=%d "
  670. "w=%d h=%d (r=%d b=%d fw=%d fh=%d\n", left, top,
  671. cr->c.width, cr->c.height, right, bottom,
  672. ctx->buf_width, ctx->buf_height);
  673. } else {
  674. cr->c.left = 0;
  675. cr->c.top = 0;
  676. cr->c.width = ctx->img_width;
  677. cr->c.height = ctx->img_height;
  678. mfc_debug(2, "Cropping info: w=%d h=%d fw=%d "
  679. "fh=%d\n", cr->c.width, cr->c.height, ctx->buf_width,
  680. ctx->buf_height);
  681. }
  682. return 0;
  683. }
  684. /* v4l2_ioctl_ops */
  685. static const struct v4l2_ioctl_ops s5p_mfc_dec_ioctl_ops = {
  686. .vidioc_querycap = vidioc_querycap,
  687. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  688. .vidioc_enum_fmt_vid_cap_mplane = vidioc_enum_fmt_vid_cap_mplane,
  689. .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
  690. .vidioc_enum_fmt_vid_out_mplane = vidioc_enum_fmt_vid_out_mplane,
  691. .vidioc_g_fmt_vid_cap_mplane = vidioc_g_fmt,
  692. .vidioc_g_fmt_vid_out_mplane = vidioc_g_fmt,
  693. .vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt,
  694. .vidioc_try_fmt_vid_out_mplane = vidioc_try_fmt,
  695. .vidioc_s_fmt_vid_cap_mplane = vidioc_s_fmt,
  696. .vidioc_s_fmt_vid_out_mplane = vidioc_s_fmt,
  697. .vidioc_reqbufs = vidioc_reqbufs,
  698. .vidioc_querybuf = vidioc_querybuf,
  699. .vidioc_qbuf = vidioc_qbuf,
  700. .vidioc_dqbuf = vidioc_dqbuf,
  701. .vidioc_streamon = vidioc_streamon,
  702. .vidioc_streamoff = vidioc_streamoff,
  703. .vidioc_g_crop = vidioc_g_crop,
  704. };
  705. static int s5p_mfc_queue_setup(struct vb2_queue *vq,
  706. const struct v4l2_format *fmt, unsigned int *buf_count,
  707. unsigned int *plane_count, unsigned int psize[],
  708. void *allocators[])
  709. {
  710. struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
  711. /* Video output for decoding (source)
  712. * this can be set after getting an instance */
  713. if (ctx->state == MFCINST_INIT &&
  714. vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  715. /* A single plane is required for input */
  716. *plane_count = 1;
  717. if (*buf_count < 1)
  718. *buf_count = 1;
  719. if (*buf_count > MFC_MAX_BUFFERS)
  720. *buf_count = MFC_MAX_BUFFERS;
  721. /* Video capture for decoding (destination)
  722. * this can be set after the header was parsed */
  723. } else if (ctx->state == MFCINST_HEAD_PARSED &&
  724. vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  725. /* Output plane count is 2 - one for Y and one for CbCr */
  726. *plane_count = 2;
  727. /* Setup buffer count */
  728. if (*buf_count < ctx->dpb_count)
  729. *buf_count = ctx->dpb_count;
  730. if (*buf_count > ctx->dpb_count + MFC_MAX_EXTRA_DPB)
  731. *buf_count = ctx->dpb_count + MFC_MAX_EXTRA_DPB;
  732. if (*buf_count > MFC_MAX_BUFFERS)
  733. *buf_count = MFC_MAX_BUFFERS;
  734. } else {
  735. mfc_err("State seems invalid. State = %d, vq->type = %d\n",
  736. ctx->state, vq->type);
  737. return -EINVAL;
  738. }
  739. mfc_debug(2, "Buffer count=%d, plane count=%d\n",
  740. *buf_count, *plane_count);
  741. if (ctx->state == MFCINST_HEAD_PARSED &&
  742. vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  743. psize[0] = ctx->luma_size;
  744. psize[1] = ctx->chroma_size;
  745. allocators[0] = ctx->dev->alloc_ctx[MFC_BANK2_ALLOC_CTX];
  746. allocators[1] = ctx->dev->alloc_ctx[MFC_BANK1_ALLOC_CTX];
  747. } else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
  748. ctx->state == MFCINST_INIT) {
  749. psize[0] = ctx->dec_src_buf_size;
  750. allocators[0] = ctx->dev->alloc_ctx[MFC_BANK1_ALLOC_CTX];
  751. } else {
  752. mfc_err("This video node is dedicated to decoding. Decoding not initalised\n");
  753. return -EINVAL;
  754. }
  755. return 0;
  756. }
  757. static void s5p_mfc_unlock(struct vb2_queue *q)
  758. {
  759. struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
  760. struct s5p_mfc_dev *dev = ctx->dev;
  761. mutex_unlock(&dev->mfc_mutex);
  762. }
  763. static void s5p_mfc_lock(struct vb2_queue *q)
  764. {
  765. struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
  766. struct s5p_mfc_dev *dev = ctx->dev;
  767. mutex_lock(&dev->mfc_mutex);
  768. }
  769. static int s5p_mfc_buf_init(struct vb2_buffer *vb)
  770. {
  771. struct vb2_queue *vq = vb->vb2_queue;
  772. struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
  773. unsigned int i;
  774. if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  775. if (ctx->capture_state == QUEUE_BUFS_MMAPED)
  776. return 0;
  777. for (i = 0; i <= ctx->src_fmt->num_planes ; i++) {
  778. if (IS_ERR_OR_NULL(ERR_PTR(
  779. vb2_dma_contig_plane_dma_addr(vb, i)))) {
  780. mfc_err("Plane mem not allocated\n");
  781. return -EINVAL;
  782. }
  783. }
  784. if (vb2_plane_size(vb, 0) < ctx->luma_size ||
  785. vb2_plane_size(vb, 1) < ctx->chroma_size) {
  786. mfc_err("Plane buffer (CAPTURE) is too small\n");
  787. return -EINVAL;
  788. }
  789. i = vb->v4l2_buf.index;
  790. ctx->dst_bufs[i].b = vb;
  791. ctx->dst_bufs[i].cookie.raw.luma =
  792. vb2_dma_contig_plane_dma_addr(vb, 0);
  793. ctx->dst_bufs[i].cookie.raw.chroma =
  794. vb2_dma_contig_plane_dma_addr(vb, 1);
  795. ctx->dst_bufs_cnt++;
  796. } else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  797. if (IS_ERR_OR_NULL(ERR_PTR(
  798. vb2_dma_contig_plane_dma_addr(vb, 0)))) {
  799. mfc_err("Plane memory not allocated\n");
  800. return -EINVAL;
  801. }
  802. if (vb2_plane_size(vb, 0) < ctx->dec_src_buf_size) {
  803. mfc_err("Plane buffer (OUTPUT) is too small\n");
  804. return -EINVAL;
  805. }
  806. i = vb->v4l2_buf.index;
  807. ctx->src_bufs[i].b = vb;
  808. ctx->src_bufs[i].cookie.stream =
  809. vb2_dma_contig_plane_dma_addr(vb, 0);
  810. ctx->src_bufs_cnt++;
  811. } else {
  812. mfc_err("s5p_mfc_buf_init: unknown queue type\n");
  813. return -EINVAL;
  814. }
  815. return 0;
  816. }
  817. static int s5p_mfc_start_streaming(struct vb2_queue *q, unsigned int count)
  818. {
  819. struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
  820. struct s5p_mfc_dev *dev = ctx->dev;
  821. unsigned long flags;
  822. v4l2_ctrl_handler_setup(&ctx->ctrl_handler);
  823. if (ctx->state == MFCINST_FINISHING ||
  824. ctx->state == MFCINST_FINISHED)
  825. ctx->state = MFCINST_RUNNING;
  826. /* If context is ready then dev = work->data;schedule it to run */
  827. if (s5p_mfc_ctx_ready(ctx)) {
  828. spin_lock_irqsave(&dev->condlock, flags);
  829. set_bit(ctx->num, &dev->ctx_work_bits);
  830. spin_unlock_irqrestore(&dev->condlock, flags);
  831. }
  832. s5p_mfc_try_run(dev);
  833. return 0;
  834. }
  835. static int s5p_mfc_stop_streaming(struct vb2_queue *q)
  836. {
  837. unsigned long flags;
  838. struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
  839. struct s5p_mfc_dev *dev = ctx->dev;
  840. int aborted = 0;
  841. if ((ctx->state == MFCINST_FINISHING ||
  842. ctx->state == MFCINST_RUNNING) &&
  843. dev->curr_ctx == ctx->num && dev->hw_lock) {
  844. ctx->state = MFCINST_ABORT;
  845. s5p_mfc_wait_for_done_ctx(ctx,
  846. S5P_FIMV_R2H_CMD_FRAME_DONE_RET, 0);
  847. aborted = 1;
  848. }
  849. spin_lock_irqsave(&dev->irqlock, flags);
  850. if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  851. s5p_mfc_cleanup_queue(&ctx->dst_queue, &ctx->vq_dst);
  852. INIT_LIST_HEAD(&ctx->dst_queue);
  853. ctx->dst_queue_cnt = 0;
  854. ctx->dpb_flush_flag = 1;
  855. ctx->dec_dst_flag = 0;
  856. }
  857. if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  858. s5p_mfc_cleanup_queue(&ctx->src_queue, &ctx->vq_src);
  859. INIT_LIST_HEAD(&ctx->src_queue);
  860. ctx->src_queue_cnt = 0;
  861. }
  862. if (aborted)
  863. ctx->state = MFCINST_RUNNING;
  864. spin_unlock_irqrestore(&dev->irqlock, flags);
  865. return 0;
  866. }
  867. static void s5p_mfc_buf_queue(struct vb2_buffer *vb)
  868. {
  869. struct vb2_queue *vq = vb->vb2_queue;
  870. struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
  871. struct s5p_mfc_dev *dev = ctx->dev;
  872. unsigned long flags;
  873. struct s5p_mfc_buf *mfc_buf;
  874. if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  875. mfc_buf = &ctx->src_bufs[vb->v4l2_buf.index];
  876. mfc_buf->used = 0;
  877. spin_lock_irqsave(&dev->irqlock, flags);
  878. list_add_tail(&mfc_buf->list, &ctx->src_queue);
  879. ctx->src_queue_cnt++;
  880. spin_unlock_irqrestore(&dev->irqlock, flags);
  881. } else if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  882. mfc_buf = &ctx->dst_bufs[vb->v4l2_buf.index];
  883. mfc_buf->used = 0;
  884. /* Mark destination as available for use by MFC */
  885. spin_lock_irqsave(&dev->irqlock, flags);
  886. set_bit(vb->v4l2_buf.index, &ctx->dec_dst_flag);
  887. list_add_tail(&mfc_buf->list, &ctx->dst_queue);
  888. ctx->dst_queue_cnt++;
  889. spin_unlock_irqrestore(&dev->irqlock, flags);
  890. } else {
  891. mfc_err("Unsupported buffer type (%d)\n", vq->type);
  892. }
  893. if (s5p_mfc_ctx_ready(ctx)) {
  894. spin_lock_irqsave(&dev->condlock, flags);
  895. set_bit(ctx->num, &dev->ctx_work_bits);
  896. spin_unlock_irqrestore(&dev->condlock, flags);
  897. }
  898. s5p_mfc_try_run(dev);
  899. }
  900. static struct vb2_ops s5p_mfc_dec_qops = {
  901. .queue_setup = s5p_mfc_queue_setup,
  902. .wait_prepare = s5p_mfc_unlock,
  903. .wait_finish = s5p_mfc_lock,
  904. .buf_init = s5p_mfc_buf_init,
  905. .start_streaming = s5p_mfc_start_streaming,
  906. .stop_streaming = s5p_mfc_stop_streaming,
  907. .buf_queue = s5p_mfc_buf_queue,
  908. };
  909. struct s5p_mfc_codec_ops *get_dec_codec_ops(void)
  910. {
  911. return &decoder_codec_ops;
  912. }
  913. struct vb2_ops *get_dec_queue_ops(void)
  914. {
  915. return &s5p_mfc_dec_qops;
  916. }
  917. const struct v4l2_ioctl_ops *get_dec_v4l2_ioctl_ops(void)
  918. {
  919. return &s5p_mfc_dec_ioctl_ops;
  920. }
  921. #define IS_MFC51_PRIV(x) ((V4L2_CTRL_ID2CLASS(x) == V4L2_CTRL_CLASS_MPEG) \
  922. && V4L2_CTRL_DRIVER_PRIV(x))
  923. int s5p_mfc_dec_ctrls_setup(struct s5p_mfc_ctx *ctx)
  924. {
  925. struct v4l2_ctrl_config cfg;
  926. int i;
  927. v4l2_ctrl_handler_init(&ctx->ctrl_handler, NUM_CTRLS);
  928. if (ctx->ctrl_handler.error) {
  929. mfc_err("v4l2_ctrl_handler_init failed\n");
  930. return ctx->ctrl_handler.error;
  931. }
  932. for (i = 0; i < NUM_CTRLS; i++) {
  933. if (IS_MFC51_PRIV(controls[i].id)) {
  934. memset(&cfg, 0, sizeof(struct v4l2_ctrl_config));
  935. cfg.ops = &s5p_mfc_dec_ctrl_ops;
  936. cfg.id = controls[i].id;
  937. cfg.min = controls[i].minimum;
  938. cfg.max = controls[i].maximum;
  939. cfg.def = controls[i].default_value;
  940. cfg.name = controls[i].name;
  941. cfg.type = controls[i].type;
  942. cfg.step = controls[i].step;
  943. cfg.menu_skip_mask = 0;
  944. ctx->ctrls[i] = v4l2_ctrl_new_custom(&ctx->ctrl_handler,
  945. &cfg, NULL);
  946. } else {
  947. ctx->ctrls[i] = v4l2_ctrl_new_std(&ctx->ctrl_handler,
  948. &s5p_mfc_dec_ctrl_ops,
  949. controls[i].id, controls[i].minimum,
  950. controls[i].maximum, controls[i].step,
  951. controls[i].default_value);
  952. }
  953. if (ctx->ctrl_handler.error) {
  954. mfc_err("Adding control (%d) failed\n", i);
  955. return ctx->ctrl_handler.error;
  956. }
  957. if (controls[i].is_volatile && ctx->ctrls[i])
  958. ctx->ctrls[i]->flags |= V4L2_CTRL_FLAG_VOLATILE;
  959. }
  960. return 0;
  961. }
  962. void s5p_mfc_dec_ctrls_delete(struct s5p_mfc_ctx *ctx)
  963. {
  964. int i;
  965. v4l2_ctrl_handler_free(&ctx->ctrl_handler);
  966. for (i = 0; i < NUM_CTRLS; i++)
  967. ctx->ctrls[i] = NULL;
  968. }