s5p_mfc_dec.c 33 KB

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