s5p_mfc_dec.c 32 KB

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