s5p_mfc_dec.c 34 KB

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