s5p_mfc_dec.c 33 KB

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