s5p_mfc_dec.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201
  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_PLUS(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_PLUS(dev) &&
  379. (fmt->fourcc == V4L2_PIX_FMT_NV12MT)) {
  380. mfc_err("Not supported format.\n");
  381. return -EINVAL;
  382. } else if (!IS_MFCV6_PLUS(dev) &&
  383. (fmt->fourcc != V4L2_PIX_FMT_NV12MT)) {
  384. mfc_err("Not supported format.\n");
  385. return -EINVAL;
  386. }
  387. }
  388. return 0;
  389. }
  390. /* Set format */
  391. static int vidioc_s_fmt(struct file *file, void *priv, struct v4l2_format *f)
  392. {
  393. struct s5p_mfc_dev *dev = video_drvdata(file);
  394. struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
  395. int ret = 0;
  396. struct v4l2_pix_format_mplane *pix_mp;
  397. mfc_debug_enter();
  398. ret = vidioc_try_fmt(file, priv, f);
  399. pix_mp = &f->fmt.pix_mp;
  400. if (ret)
  401. return ret;
  402. if (ctx->vq_src.streaming || ctx->vq_dst.streaming) {
  403. v4l2_err(&dev->v4l2_dev, "%s queue busy\n", __func__);
  404. ret = -EBUSY;
  405. goto out;
  406. }
  407. if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  408. /* dst_fmt is validated by call to vidioc_try_fmt */
  409. ctx->dst_fmt = find_format(f, MFC_FMT_RAW);
  410. ret = 0;
  411. goto out;
  412. } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  413. /* src_fmt is validated by call to vidioc_try_fmt */
  414. ctx->src_fmt = find_format(f, MFC_FMT_DEC);
  415. ctx->codec_mode = ctx->src_fmt->codec_mode;
  416. mfc_debug(2, "The codec number is: %d\n", ctx->codec_mode);
  417. pix_mp->height = 0;
  418. pix_mp->width = 0;
  419. if (pix_mp->plane_fmt[0].sizeimage)
  420. ctx->dec_src_buf_size = pix_mp->plane_fmt[0].sizeimage;
  421. else
  422. pix_mp->plane_fmt[0].sizeimage = ctx->dec_src_buf_size =
  423. DEF_CPB_SIZE;
  424. pix_mp->plane_fmt[0].bytesperline = 0;
  425. ctx->state = MFCINST_INIT;
  426. ret = 0;
  427. goto out;
  428. } else {
  429. mfc_err("Wrong type error for S_FMT : %d", f->type);
  430. ret = -EINVAL;
  431. goto out;
  432. }
  433. out:
  434. mfc_debug_leave();
  435. return ret;
  436. }
  437. /* Reqeust buffers */
  438. static int vidioc_reqbufs(struct file *file, void *priv,
  439. struct v4l2_requestbuffers *reqbufs)
  440. {
  441. struct s5p_mfc_dev *dev = video_drvdata(file);
  442. struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
  443. int ret = 0;
  444. if (reqbufs->memory != V4L2_MEMORY_MMAP) {
  445. mfc_err("Only V4L2_MEMORY_MAP is supported\n");
  446. return -EINVAL;
  447. }
  448. if (reqbufs->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  449. /* Can only request buffers after an instance has been opened.*/
  450. if (ctx->state == MFCINST_INIT) {
  451. ctx->src_bufs_cnt = 0;
  452. if (reqbufs->count == 0) {
  453. mfc_debug(2, "Freeing buffers\n");
  454. s5p_mfc_clock_on();
  455. ret = vb2_reqbufs(&ctx->vq_src, reqbufs);
  456. s5p_mfc_clock_off();
  457. return ret;
  458. }
  459. /* Decoding */
  460. if (ctx->output_state != QUEUE_FREE) {
  461. mfc_err("Bufs have already been requested\n");
  462. return -EINVAL;
  463. }
  464. s5p_mfc_clock_on();
  465. ret = vb2_reqbufs(&ctx->vq_src, reqbufs);
  466. s5p_mfc_clock_off();
  467. if (ret) {
  468. mfc_err("vb2_reqbufs on output failed\n");
  469. return ret;
  470. }
  471. mfc_debug(2, "vb2_reqbufs: %d\n", ret);
  472. ctx->output_state = QUEUE_BUFS_REQUESTED;
  473. }
  474. } else if (reqbufs->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  475. ctx->dst_bufs_cnt = 0;
  476. if (reqbufs->count == 0) {
  477. mfc_debug(2, "Freeing buffers\n");
  478. s5p_mfc_clock_on();
  479. ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
  480. s5p_mfc_clock_off();
  481. return ret;
  482. }
  483. if (ctx->capture_state != QUEUE_FREE) {
  484. mfc_err("Bufs have already been requested\n");
  485. return -EINVAL;
  486. }
  487. ctx->capture_state = QUEUE_BUFS_REQUESTED;
  488. s5p_mfc_clock_on();
  489. ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
  490. s5p_mfc_clock_off();
  491. if (ret) {
  492. mfc_err("vb2_reqbufs on capture failed\n");
  493. return ret;
  494. }
  495. if (reqbufs->count < ctx->pb_count) {
  496. mfc_err("Not enough buffers allocated\n");
  497. reqbufs->count = 0;
  498. s5p_mfc_clock_on();
  499. ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
  500. s5p_mfc_clock_off();
  501. return -ENOMEM;
  502. }
  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. s5p_mfc_clock_on();
  509. ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
  510. s5p_mfc_clock_off();
  511. return -ENOMEM;
  512. }
  513. if (ctx->dst_bufs_cnt == ctx->total_dpb_count) {
  514. ctx->capture_state = QUEUE_BUFS_MMAPED;
  515. } else {
  516. mfc_err("Not all buffers passed to buf_init\n");
  517. reqbufs->count = 0;
  518. s5p_mfc_clock_on();
  519. ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
  520. s5p_mfc_hw_call(dev->mfc_ops, release_codec_buffers,
  521. ctx);
  522. s5p_mfc_clock_off();
  523. return -ENOMEM;
  524. }
  525. if (s5p_mfc_ctx_ready(ctx))
  526. set_work_bit_irqsave(ctx);
  527. s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
  528. s5p_mfc_wait_for_done_ctx(ctx,
  529. S5P_MFC_R2H_CMD_INIT_BUFFERS_RET, 0);
  530. }
  531. return ret;
  532. }
  533. /* Query buffer */
  534. static int vidioc_querybuf(struct file *file, void *priv,
  535. struct v4l2_buffer *buf)
  536. {
  537. struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
  538. int ret;
  539. int i;
  540. if (buf->memory != V4L2_MEMORY_MMAP) {
  541. mfc_err("Only mmaped buffers can be used\n");
  542. return -EINVAL;
  543. }
  544. mfc_debug(2, "State: %d, buf->type: %d\n", ctx->state, buf->type);
  545. if (ctx->state == MFCINST_INIT &&
  546. buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  547. ret = vb2_querybuf(&ctx->vq_src, buf);
  548. } else if (ctx->state == MFCINST_RUNNING &&
  549. buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  550. ret = vb2_querybuf(&ctx->vq_dst, buf);
  551. for (i = 0; i < buf->length; i++)
  552. buf->m.planes[i].m.mem_offset += DST_QUEUE_OFF_BASE;
  553. } else {
  554. mfc_err("vidioc_querybuf called in an inappropriate state\n");
  555. ret = -EINVAL;
  556. }
  557. mfc_debug_leave();
  558. return ret;
  559. }
  560. /* Queue a buffer */
  561. static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
  562. {
  563. struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
  564. if (ctx->state == MFCINST_ERROR) {
  565. mfc_err("Call on QBUF after unrecoverable error\n");
  566. return -EIO;
  567. }
  568. if (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  569. return vb2_qbuf(&ctx->vq_src, buf);
  570. else if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  571. return vb2_qbuf(&ctx->vq_dst, buf);
  572. return -EINVAL;
  573. }
  574. /* Dequeue a buffer */
  575. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
  576. {
  577. const struct v4l2_event ev = {
  578. .type = V4L2_EVENT_EOS
  579. };
  580. struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
  581. int ret;
  582. if (ctx->state == MFCINST_ERROR) {
  583. mfc_err("Call on DQBUF after unrecoverable error\n");
  584. return -EIO;
  585. }
  586. if (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  587. ret = vb2_dqbuf(&ctx->vq_src, buf, file->f_flags & O_NONBLOCK);
  588. else if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  589. ret = vb2_dqbuf(&ctx->vq_dst, buf, file->f_flags & O_NONBLOCK);
  590. if (ret == 0 && ctx->state == MFCINST_FINISHED &&
  591. list_empty(&ctx->vq_dst.done_list))
  592. v4l2_event_queue_fh(&ctx->fh, &ev);
  593. } else {
  594. ret = -EINVAL;
  595. }
  596. return ret;
  597. }
  598. /* Export DMA buffer */
  599. static int vidioc_expbuf(struct file *file, void *priv,
  600. struct v4l2_exportbuffer *eb)
  601. {
  602. struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
  603. if (eb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  604. return vb2_expbuf(&ctx->vq_src, eb);
  605. if (eb->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  606. return vb2_expbuf(&ctx->vq_dst, eb);
  607. return -EINVAL;
  608. }
  609. /* Stream on */
  610. static int vidioc_streamon(struct file *file, void *priv,
  611. enum v4l2_buf_type type)
  612. {
  613. struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
  614. struct s5p_mfc_dev *dev = ctx->dev;
  615. int ret = -EINVAL;
  616. mfc_debug_enter();
  617. if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  618. if (ctx->state == MFCINST_INIT) {
  619. ctx->dst_bufs_cnt = 0;
  620. ctx->src_bufs_cnt = 0;
  621. ctx->capture_state = QUEUE_FREE;
  622. ctx->output_state = QUEUE_FREE;
  623. s5p_mfc_hw_call(dev->mfc_ops, alloc_instance_buffer,
  624. ctx);
  625. s5p_mfc_hw_call(dev->mfc_ops, alloc_dec_temp_buffers,
  626. ctx);
  627. set_work_bit_irqsave(ctx);
  628. s5p_mfc_clean_ctx_int_flags(ctx);
  629. s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
  630. if (s5p_mfc_wait_for_done_ctx(ctx,
  631. S5P_MFC_R2H_CMD_OPEN_INSTANCE_RET, 0)) {
  632. /* Error or timeout */
  633. mfc_err("Error getting instance from hardware\n");
  634. s5p_mfc_hw_call(dev->mfc_ops,
  635. release_instance_buffer, ctx);
  636. s5p_mfc_hw_call(dev->mfc_ops,
  637. release_dec_desc_buffer, ctx);
  638. return -EIO;
  639. }
  640. mfc_debug(2, "Got instance number: %d\n", ctx->inst_no);
  641. }
  642. ret = vb2_streamon(&ctx->vq_src, type);
  643. }
  644. else if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  645. ret = vb2_streamon(&ctx->vq_dst, type);
  646. mfc_debug_leave();
  647. return ret;
  648. }
  649. /* Stream off, which equals to a pause */
  650. static int vidioc_streamoff(struct file *file, void *priv,
  651. enum v4l2_buf_type type)
  652. {
  653. struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
  654. if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
  655. return vb2_streamoff(&ctx->vq_src, type);
  656. else if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  657. return vb2_streamoff(&ctx->vq_dst, type);
  658. return -EINVAL;
  659. }
  660. /* Set controls - v4l2 control framework */
  661. static int s5p_mfc_dec_s_ctrl(struct v4l2_ctrl *ctrl)
  662. {
  663. struct s5p_mfc_ctx *ctx = ctrl_to_ctx(ctrl);
  664. switch (ctrl->id) {
  665. case V4L2_CID_MPEG_MFC51_VIDEO_DECODER_H264_DISPLAY_DELAY:
  666. ctx->display_delay = ctrl->val;
  667. break;
  668. case V4L2_CID_MPEG_MFC51_VIDEO_DECODER_H264_DISPLAY_DELAY_ENABLE:
  669. ctx->display_delay_enable = ctrl->val;
  670. break;
  671. case V4L2_CID_MPEG_VIDEO_DECODER_MPEG4_DEBLOCK_FILTER:
  672. ctx->loop_filter_mpeg4 = ctrl->val;
  673. break;
  674. case V4L2_CID_MPEG_VIDEO_DECODER_SLICE_INTERFACE:
  675. ctx->slice_interface = ctrl->val;
  676. break;
  677. default:
  678. mfc_err("Invalid control 0x%08x\n", ctrl->id);
  679. return -EINVAL;
  680. }
  681. return 0;
  682. }
  683. static int s5p_mfc_dec_g_v_ctrl(struct v4l2_ctrl *ctrl)
  684. {
  685. struct s5p_mfc_ctx *ctx = ctrl_to_ctx(ctrl);
  686. struct s5p_mfc_dev *dev = ctx->dev;
  687. switch (ctrl->id) {
  688. case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE:
  689. if (ctx->state >= MFCINST_HEAD_PARSED &&
  690. ctx->state < MFCINST_ABORT) {
  691. ctrl->val = ctx->pb_count;
  692. break;
  693. } else if (ctx->state != MFCINST_INIT) {
  694. v4l2_err(&dev->v4l2_dev, "Decoding not initialised\n");
  695. return -EINVAL;
  696. }
  697. /* Should wait for the header to be parsed */
  698. s5p_mfc_clean_ctx_int_flags(ctx);
  699. s5p_mfc_wait_for_done_ctx(ctx,
  700. S5P_MFC_R2H_CMD_SEQ_DONE_RET, 0);
  701. if (ctx->state >= MFCINST_HEAD_PARSED &&
  702. ctx->state < MFCINST_ABORT) {
  703. ctrl->val = ctx->pb_count;
  704. } else {
  705. v4l2_err(&dev->v4l2_dev, "Decoding not initialised\n");
  706. return -EINVAL;
  707. }
  708. break;
  709. }
  710. return 0;
  711. }
  712. static const struct v4l2_ctrl_ops s5p_mfc_dec_ctrl_ops = {
  713. .s_ctrl = s5p_mfc_dec_s_ctrl,
  714. .g_volatile_ctrl = s5p_mfc_dec_g_v_ctrl,
  715. };
  716. /* Get cropping information */
  717. static int vidioc_g_crop(struct file *file, void *priv,
  718. struct v4l2_crop *cr)
  719. {
  720. struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
  721. struct s5p_mfc_dev *dev = ctx->dev;
  722. u32 left, right, top, bottom;
  723. if (ctx->state != MFCINST_HEAD_PARSED &&
  724. ctx->state != MFCINST_RUNNING && ctx->state != MFCINST_FINISHING
  725. && ctx->state != MFCINST_FINISHED) {
  726. mfc_err("Cannont set crop\n");
  727. return -EINVAL;
  728. }
  729. if (ctx->src_fmt->fourcc == V4L2_PIX_FMT_H264) {
  730. left = s5p_mfc_hw_call(dev->mfc_ops, get_crop_info_h, ctx);
  731. right = left >> S5P_FIMV_SHARED_CROP_RIGHT_SHIFT;
  732. left = left & S5P_FIMV_SHARED_CROP_LEFT_MASK;
  733. top = s5p_mfc_hw_call(dev->mfc_ops, get_crop_info_v, ctx);
  734. bottom = top >> S5P_FIMV_SHARED_CROP_BOTTOM_SHIFT;
  735. top = top & S5P_FIMV_SHARED_CROP_TOP_MASK;
  736. cr->c.left = left;
  737. cr->c.top = top;
  738. cr->c.width = ctx->img_width - left - right;
  739. cr->c.height = ctx->img_height - top - bottom;
  740. mfc_debug(2, "Cropping info [h264]: l=%d t=%d "
  741. "w=%d h=%d (r=%d b=%d fw=%d fh=%d\n", left, top,
  742. cr->c.width, cr->c.height, right, bottom,
  743. ctx->buf_width, ctx->buf_height);
  744. } else {
  745. cr->c.left = 0;
  746. cr->c.top = 0;
  747. cr->c.width = ctx->img_width;
  748. cr->c.height = ctx->img_height;
  749. mfc_debug(2, "Cropping info: w=%d h=%d fw=%d "
  750. "fh=%d\n", cr->c.width, cr->c.height, ctx->buf_width,
  751. ctx->buf_height);
  752. }
  753. return 0;
  754. }
  755. int vidioc_decoder_cmd(struct file *file, void *priv,
  756. struct v4l2_decoder_cmd *cmd)
  757. {
  758. struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
  759. struct s5p_mfc_dev *dev = ctx->dev;
  760. struct s5p_mfc_buf *buf;
  761. unsigned long flags;
  762. switch (cmd->cmd) {
  763. case V4L2_ENC_CMD_STOP:
  764. if (cmd->flags != 0)
  765. return -EINVAL;
  766. if (!ctx->vq_src.streaming)
  767. return -EINVAL;
  768. spin_lock_irqsave(&dev->irqlock, flags);
  769. if (list_empty(&ctx->src_queue)) {
  770. mfc_err("EOS: empty src queue, entering finishing state");
  771. ctx->state = MFCINST_FINISHING;
  772. if (s5p_mfc_ctx_ready(ctx))
  773. set_work_bit_irqsave(ctx);
  774. spin_unlock_irqrestore(&dev->irqlock, flags);
  775. s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
  776. } else {
  777. mfc_err("EOS: marking last buffer of stream");
  778. buf = list_entry(ctx->src_queue.prev,
  779. struct s5p_mfc_buf, list);
  780. if (buf->flags & MFC_BUF_FLAG_USED)
  781. ctx->state = MFCINST_FINISHING;
  782. else
  783. buf->flags |= MFC_BUF_FLAG_EOS;
  784. spin_unlock_irqrestore(&dev->irqlock, flags);
  785. }
  786. break;
  787. default:
  788. return -EINVAL;
  789. }
  790. return 0;
  791. }
  792. static int vidioc_subscribe_event(struct v4l2_fh *fh,
  793. const struct v4l2_event_subscription *sub)
  794. {
  795. switch (sub->type) {
  796. case V4L2_EVENT_EOS:
  797. return v4l2_event_subscribe(fh, sub, 2, NULL);
  798. default:
  799. return -EINVAL;
  800. }
  801. }
  802. /* v4l2_ioctl_ops */
  803. static const struct v4l2_ioctl_ops s5p_mfc_dec_ioctl_ops = {
  804. .vidioc_querycap = vidioc_querycap,
  805. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  806. .vidioc_enum_fmt_vid_cap_mplane = vidioc_enum_fmt_vid_cap_mplane,
  807. .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
  808. .vidioc_enum_fmt_vid_out_mplane = vidioc_enum_fmt_vid_out_mplane,
  809. .vidioc_g_fmt_vid_cap_mplane = vidioc_g_fmt,
  810. .vidioc_g_fmt_vid_out_mplane = vidioc_g_fmt,
  811. .vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt,
  812. .vidioc_try_fmt_vid_out_mplane = vidioc_try_fmt,
  813. .vidioc_s_fmt_vid_cap_mplane = vidioc_s_fmt,
  814. .vidioc_s_fmt_vid_out_mplane = vidioc_s_fmt,
  815. .vidioc_reqbufs = vidioc_reqbufs,
  816. .vidioc_querybuf = vidioc_querybuf,
  817. .vidioc_qbuf = vidioc_qbuf,
  818. .vidioc_dqbuf = vidioc_dqbuf,
  819. .vidioc_expbuf = vidioc_expbuf,
  820. .vidioc_streamon = vidioc_streamon,
  821. .vidioc_streamoff = vidioc_streamoff,
  822. .vidioc_g_crop = vidioc_g_crop,
  823. .vidioc_decoder_cmd = vidioc_decoder_cmd,
  824. .vidioc_subscribe_event = vidioc_subscribe_event,
  825. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  826. };
  827. static int s5p_mfc_queue_setup(struct vb2_queue *vq,
  828. const struct v4l2_format *fmt, unsigned int *buf_count,
  829. unsigned int *plane_count, unsigned int psize[],
  830. void *allocators[])
  831. {
  832. struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
  833. struct s5p_mfc_dev *dev = ctx->dev;
  834. /* Video output for decoding (source)
  835. * this can be set after getting an instance */
  836. if (ctx->state == MFCINST_INIT &&
  837. vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  838. /* A single plane is required for input */
  839. *plane_count = 1;
  840. if (*buf_count < 1)
  841. *buf_count = 1;
  842. if (*buf_count > MFC_MAX_BUFFERS)
  843. *buf_count = MFC_MAX_BUFFERS;
  844. /* Video capture for decoding (destination)
  845. * this can be set after the header was parsed */
  846. } else if (ctx->state == MFCINST_HEAD_PARSED &&
  847. vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  848. /* Output plane count is 2 - one for Y and one for CbCr */
  849. *plane_count = 2;
  850. /* Setup buffer count */
  851. if (*buf_count < ctx->pb_count)
  852. *buf_count = ctx->pb_count;
  853. if (*buf_count > ctx->pb_count + MFC_MAX_EXTRA_DPB)
  854. *buf_count = ctx->pb_count + MFC_MAX_EXTRA_DPB;
  855. if (*buf_count > MFC_MAX_BUFFERS)
  856. *buf_count = MFC_MAX_BUFFERS;
  857. } else {
  858. mfc_err("State seems invalid. State = %d, vq->type = %d\n",
  859. ctx->state, vq->type);
  860. return -EINVAL;
  861. }
  862. mfc_debug(2, "Buffer count=%d, plane count=%d\n",
  863. *buf_count, *plane_count);
  864. if (ctx->state == MFCINST_HEAD_PARSED &&
  865. vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  866. psize[0] = ctx->luma_size;
  867. psize[1] = ctx->chroma_size;
  868. if (IS_MFCV6_PLUS(dev))
  869. allocators[0] =
  870. ctx->dev->alloc_ctx[MFC_BANK1_ALLOC_CTX];
  871. else
  872. allocators[0] =
  873. ctx->dev->alloc_ctx[MFC_BANK2_ALLOC_CTX];
  874. allocators[1] = ctx->dev->alloc_ctx[MFC_BANK1_ALLOC_CTX];
  875. } else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
  876. ctx->state == MFCINST_INIT) {
  877. psize[0] = ctx->dec_src_buf_size;
  878. allocators[0] = ctx->dev->alloc_ctx[MFC_BANK1_ALLOC_CTX];
  879. } else {
  880. mfc_err("This video node is dedicated to decoding. Decoding not initialized\n");
  881. return -EINVAL;
  882. }
  883. return 0;
  884. }
  885. static void s5p_mfc_unlock(struct vb2_queue *q)
  886. {
  887. struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
  888. struct s5p_mfc_dev *dev = ctx->dev;
  889. mutex_unlock(&dev->mfc_mutex);
  890. }
  891. static void s5p_mfc_lock(struct vb2_queue *q)
  892. {
  893. struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
  894. struct s5p_mfc_dev *dev = ctx->dev;
  895. mutex_lock(&dev->mfc_mutex);
  896. }
  897. static int s5p_mfc_buf_init(struct vb2_buffer *vb)
  898. {
  899. struct vb2_queue *vq = vb->vb2_queue;
  900. struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
  901. unsigned int i;
  902. if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  903. if (ctx->capture_state == QUEUE_BUFS_MMAPED)
  904. return 0;
  905. for (i = 0; i <= ctx->src_fmt->num_planes ; i++) {
  906. if (IS_ERR_OR_NULL(ERR_PTR(
  907. vb2_dma_contig_plane_dma_addr(vb, i)))) {
  908. mfc_err("Plane mem not allocated\n");
  909. return -EINVAL;
  910. }
  911. }
  912. if (vb2_plane_size(vb, 0) < ctx->luma_size ||
  913. vb2_plane_size(vb, 1) < ctx->chroma_size) {
  914. mfc_err("Plane buffer (CAPTURE) is too small\n");
  915. return -EINVAL;
  916. }
  917. i = vb->v4l2_buf.index;
  918. ctx->dst_bufs[i].b = vb;
  919. ctx->dst_bufs[i].cookie.raw.luma =
  920. vb2_dma_contig_plane_dma_addr(vb, 0);
  921. ctx->dst_bufs[i].cookie.raw.chroma =
  922. vb2_dma_contig_plane_dma_addr(vb, 1);
  923. ctx->dst_bufs_cnt++;
  924. } else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  925. if (IS_ERR_OR_NULL(ERR_PTR(
  926. vb2_dma_contig_plane_dma_addr(vb, 0)))) {
  927. mfc_err("Plane memory not allocated\n");
  928. return -EINVAL;
  929. }
  930. if (vb2_plane_size(vb, 0) < ctx->dec_src_buf_size) {
  931. mfc_err("Plane buffer (OUTPUT) is too small\n");
  932. return -EINVAL;
  933. }
  934. i = vb->v4l2_buf.index;
  935. ctx->src_bufs[i].b = vb;
  936. ctx->src_bufs[i].cookie.stream =
  937. vb2_dma_contig_plane_dma_addr(vb, 0);
  938. ctx->src_bufs_cnt++;
  939. } else {
  940. mfc_err("s5p_mfc_buf_init: unknown queue type\n");
  941. return -EINVAL;
  942. }
  943. return 0;
  944. }
  945. static int s5p_mfc_start_streaming(struct vb2_queue *q, unsigned int count)
  946. {
  947. struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
  948. struct s5p_mfc_dev *dev = ctx->dev;
  949. v4l2_ctrl_handler_setup(&ctx->ctrl_handler);
  950. if (ctx->state == MFCINST_FINISHING ||
  951. ctx->state == MFCINST_FINISHED)
  952. ctx->state = MFCINST_RUNNING;
  953. /* If context is ready then dev = work->data;schedule it to run */
  954. if (s5p_mfc_ctx_ready(ctx))
  955. set_work_bit_irqsave(ctx);
  956. s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
  957. return 0;
  958. }
  959. static int s5p_mfc_stop_streaming(struct vb2_queue *q)
  960. {
  961. unsigned long flags;
  962. struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
  963. struct s5p_mfc_dev *dev = ctx->dev;
  964. int aborted = 0;
  965. if ((ctx->state == MFCINST_FINISHING ||
  966. ctx->state == MFCINST_RUNNING) &&
  967. dev->curr_ctx == ctx->num && dev->hw_lock) {
  968. ctx->state = MFCINST_ABORT;
  969. s5p_mfc_wait_for_done_ctx(ctx,
  970. S5P_MFC_R2H_CMD_FRAME_DONE_RET, 0);
  971. aborted = 1;
  972. }
  973. if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  974. spin_lock_irqsave(&dev->irqlock, flags);
  975. s5p_mfc_hw_call(dev->mfc_ops, cleanup_queue, &ctx->dst_queue,
  976. &ctx->vq_dst);
  977. INIT_LIST_HEAD(&ctx->dst_queue);
  978. ctx->dst_queue_cnt = 0;
  979. ctx->dpb_flush_flag = 1;
  980. ctx->dec_dst_flag = 0;
  981. spin_unlock_irqrestore(&dev->irqlock, flags);
  982. if (IS_MFCV6_PLUS(dev) && (ctx->state == MFCINST_RUNNING)) {
  983. ctx->state = MFCINST_FLUSH;
  984. set_work_bit_irqsave(ctx);
  985. s5p_mfc_clean_ctx_int_flags(ctx);
  986. s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
  987. if (s5p_mfc_wait_for_done_ctx(ctx,
  988. S5P_MFC_R2H_CMD_DPB_FLUSH_RET, 0))
  989. mfc_err("Err flushing buffers\n");
  990. }
  991. }
  992. if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  993. spin_lock_irqsave(&dev->irqlock, flags);
  994. s5p_mfc_hw_call(dev->mfc_ops, cleanup_queue, &ctx->src_queue,
  995. &ctx->vq_src);
  996. INIT_LIST_HEAD(&ctx->src_queue);
  997. ctx->src_queue_cnt = 0;
  998. spin_unlock_irqrestore(&dev->irqlock, flags);
  999. }
  1000. if (aborted)
  1001. ctx->state = MFCINST_RUNNING;
  1002. return 0;
  1003. }
  1004. static void s5p_mfc_buf_queue(struct vb2_buffer *vb)
  1005. {
  1006. struct vb2_queue *vq = vb->vb2_queue;
  1007. struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
  1008. struct s5p_mfc_dev *dev = ctx->dev;
  1009. unsigned long flags;
  1010. struct s5p_mfc_buf *mfc_buf;
  1011. if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  1012. mfc_buf = &ctx->src_bufs[vb->v4l2_buf.index];
  1013. mfc_buf->flags &= ~MFC_BUF_FLAG_USED;
  1014. spin_lock_irqsave(&dev->irqlock, flags);
  1015. list_add_tail(&mfc_buf->list, &ctx->src_queue);
  1016. ctx->src_queue_cnt++;
  1017. spin_unlock_irqrestore(&dev->irqlock, flags);
  1018. } else if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
  1019. mfc_buf = &ctx->dst_bufs[vb->v4l2_buf.index];
  1020. mfc_buf->flags &= ~MFC_BUF_FLAG_USED;
  1021. /* Mark destination as available for use by MFC */
  1022. spin_lock_irqsave(&dev->irqlock, flags);
  1023. set_bit(vb->v4l2_buf.index, &ctx->dec_dst_flag);
  1024. list_add_tail(&mfc_buf->list, &ctx->dst_queue);
  1025. ctx->dst_queue_cnt++;
  1026. spin_unlock_irqrestore(&dev->irqlock, flags);
  1027. } else {
  1028. mfc_err("Unsupported buffer type (%d)\n", vq->type);
  1029. }
  1030. if (s5p_mfc_ctx_ready(ctx))
  1031. set_work_bit_irqsave(ctx);
  1032. s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
  1033. }
  1034. static struct vb2_ops s5p_mfc_dec_qops = {
  1035. .queue_setup = s5p_mfc_queue_setup,
  1036. .wait_prepare = s5p_mfc_unlock,
  1037. .wait_finish = s5p_mfc_lock,
  1038. .buf_init = s5p_mfc_buf_init,
  1039. .start_streaming = s5p_mfc_start_streaming,
  1040. .stop_streaming = s5p_mfc_stop_streaming,
  1041. .buf_queue = s5p_mfc_buf_queue,
  1042. };
  1043. struct s5p_mfc_codec_ops *get_dec_codec_ops(void)
  1044. {
  1045. return &decoder_codec_ops;
  1046. }
  1047. struct vb2_ops *get_dec_queue_ops(void)
  1048. {
  1049. return &s5p_mfc_dec_qops;
  1050. }
  1051. const struct v4l2_ioctl_ops *get_dec_v4l2_ioctl_ops(void)
  1052. {
  1053. return &s5p_mfc_dec_ioctl_ops;
  1054. }
  1055. #define IS_MFC51_PRIV(x) ((V4L2_CTRL_ID2CLASS(x) == V4L2_CTRL_CLASS_MPEG) \
  1056. && V4L2_CTRL_DRIVER_PRIV(x))
  1057. int s5p_mfc_dec_ctrls_setup(struct s5p_mfc_ctx *ctx)
  1058. {
  1059. struct v4l2_ctrl_config cfg;
  1060. int i;
  1061. v4l2_ctrl_handler_init(&ctx->ctrl_handler, NUM_CTRLS);
  1062. if (ctx->ctrl_handler.error) {
  1063. mfc_err("v4l2_ctrl_handler_init failed\n");
  1064. return ctx->ctrl_handler.error;
  1065. }
  1066. for (i = 0; i < NUM_CTRLS; i++) {
  1067. if (IS_MFC51_PRIV(controls[i].id)) {
  1068. memset(&cfg, 0, sizeof(struct v4l2_ctrl_config));
  1069. cfg.ops = &s5p_mfc_dec_ctrl_ops;
  1070. cfg.id = controls[i].id;
  1071. cfg.min = controls[i].minimum;
  1072. cfg.max = controls[i].maximum;
  1073. cfg.def = controls[i].default_value;
  1074. cfg.name = controls[i].name;
  1075. cfg.type = controls[i].type;
  1076. cfg.step = controls[i].step;
  1077. cfg.menu_skip_mask = 0;
  1078. ctx->ctrls[i] = v4l2_ctrl_new_custom(&ctx->ctrl_handler,
  1079. &cfg, NULL);
  1080. } else {
  1081. ctx->ctrls[i] = v4l2_ctrl_new_std(&ctx->ctrl_handler,
  1082. &s5p_mfc_dec_ctrl_ops,
  1083. controls[i].id, controls[i].minimum,
  1084. controls[i].maximum, controls[i].step,
  1085. controls[i].default_value);
  1086. }
  1087. if (ctx->ctrl_handler.error) {
  1088. mfc_err("Adding control (%d) failed\n", i);
  1089. return ctx->ctrl_handler.error;
  1090. }
  1091. if (controls[i].is_volatile && ctx->ctrls[i])
  1092. ctx->ctrls[i]->flags |= V4L2_CTRL_FLAG_VOLATILE;
  1093. }
  1094. return 0;
  1095. }
  1096. void s5p_mfc_dec_ctrls_delete(struct s5p_mfc_ctx *ctx)
  1097. {
  1098. int i;
  1099. v4l2_ctrl_handler_free(&ctx->ctrl_handler);
  1100. for (i = 0; i < NUM_CTRLS; i++)
  1101. ctx->ctrls[i] = NULL;
  1102. }
  1103. void s5p_mfc_dec_init(struct s5p_mfc_ctx *ctx)
  1104. {
  1105. struct v4l2_format f;
  1106. f.fmt.pix_mp.pixelformat = DEF_SRC_FMT_DEC;
  1107. ctx->src_fmt = find_format(&f, MFC_FMT_DEC);
  1108. f.fmt.pix_mp.pixelformat = DEF_DST_FMT_DEC;
  1109. ctx->dst_fmt = find_format(&f, MFC_FMT_RAW);
  1110. mfc_debug(2, "Default src_fmt is %x, dest_fmt is %x\n",
  1111. (unsigned int)ctx->src_fmt, (unsigned int)ctx->dst_fmt);
  1112. }