s5p_mfc_dec.c 29 KB

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