s5p_mfc_dec.c 33 KB

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