s5p_mfc_dec.c 33 KB

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