s5p_mfc_dec.c 34 KB

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