s5p_mfc_dec.c 33 KB

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