s5p_mfc.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231
  1. /*
  2. * Samsung S5P Multi Format Codec v 5.1
  3. *
  4. * Copyright (c) 2011 Samsung Electronics Co., Ltd.
  5. * Kamil Debski, <k.debski@samsung.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. */
  12. #include <linux/clk.h>
  13. #include <linux/delay.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/videobuf2-core.h>
  23. #include "regs-mfc.h"
  24. #include "s5p_mfc_ctrl.h"
  25. #include "s5p_mfc_debug.h"
  26. #include "s5p_mfc_dec.h"
  27. #include "s5p_mfc_enc.h"
  28. #include "s5p_mfc_intr.h"
  29. #include "s5p_mfc_opr.h"
  30. #include "s5p_mfc_pm.h"
  31. #include "s5p_mfc_shm.h"
  32. #define S5P_MFC_NAME "s5p-mfc"
  33. #define S5P_MFC_DEC_NAME "s5p-mfc-dec"
  34. #define S5P_MFC_ENC_NAME "s5p-mfc-enc"
  35. int debug;
  36. module_param(debug, int, S_IRUGO | S_IWUSR);
  37. MODULE_PARM_DESC(debug, "Debug level - higher value produces more verbose messages");
  38. /* Helper functions for interrupt processing */
  39. /* Remove from hw execution round robin */
  40. static void clear_work_bit(struct s5p_mfc_ctx *ctx)
  41. {
  42. struct s5p_mfc_dev *dev = ctx->dev;
  43. spin_lock(&dev->condlock);
  44. clear_bit(ctx->num, &dev->ctx_work_bits);
  45. spin_unlock(&dev->condlock);
  46. }
  47. /* Wake up context wait_queue */
  48. static void wake_up_ctx(struct s5p_mfc_ctx *ctx, unsigned int reason,
  49. unsigned int err)
  50. {
  51. ctx->int_cond = 1;
  52. ctx->int_type = reason;
  53. ctx->int_err = err;
  54. wake_up(&ctx->queue);
  55. }
  56. /* Wake up device wait_queue */
  57. static void wake_up_dev(struct s5p_mfc_dev *dev, unsigned int reason,
  58. unsigned int err)
  59. {
  60. dev->int_cond = 1;
  61. dev->int_type = reason;
  62. dev->int_err = err;
  63. wake_up(&dev->queue);
  64. }
  65. static void s5p_mfc_watchdog(unsigned long arg)
  66. {
  67. struct s5p_mfc_dev *dev = (struct s5p_mfc_dev *)arg;
  68. if (test_bit(0, &dev->hw_lock))
  69. atomic_inc(&dev->watchdog_cnt);
  70. if (atomic_read(&dev->watchdog_cnt) >= MFC_WATCHDOG_CNT) {
  71. /* This means that hw is busy and no interrupts were
  72. * generated by hw for the Nth time of running this
  73. * watchdog timer. This usually means a serious hw
  74. * error. Now it is time to kill all instances and
  75. * reset the MFC. */
  76. mfc_err("Time out during waiting for HW\n");
  77. queue_work(dev->watchdog_workqueue, &dev->watchdog_work);
  78. }
  79. dev->watchdog_timer.expires = jiffies +
  80. msecs_to_jiffies(MFC_WATCHDOG_INTERVAL);
  81. add_timer(&dev->watchdog_timer);
  82. }
  83. static void s5p_mfc_watchdog_worker(struct work_struct *work)
  84. {
  85. struct s5p_mfc_dev *dev;
  86. struct s5p_mfc_ctx *ctx;
  87. unsigned long flags;
  88. int mutex_locked;
  89. int i, ret;
  90. dev = container_of(work, struct s5p_mfc_dev, watchdog_work);
  91. mfc_err("Driver timeout error handling\n");
  92. /* Lock the mutex that protects open and release.
  93. * This is necessary as they may load and unload firmware. */
  94. mutex_locked = mutex_trylock(&dev->mfc_mutex);
  95. if (!mutex_locked)
  96. mfc_err("Error: some instance may be closing/opening\n");
  97. spin_lock_irqsave(&dev->irqlock, flags);
  98. s5p_mfc_clock_off();
  99. for (i = 0; i < MFC_NUM_CONTEXTS; i++) {
  100. ctx = dev->ctx[i];
  101. if (!ctx)
  102. continue;
  103. ctx->state = MFCINST_ERROR;
  104. s5p_mfc_cleanup_queue(&ctx->dst_queue, &ctx->vq_dst);
  105. s5p_mfc_cleanup_queue(&ctx->src_queue, &ctx->vq_src);
  106. clear_work_bit(ctx);
  107. wake_up_ctx(ctx, S5P_FIMV_R2H_CMD_ERR_RET, 0);
  108. }
  109. clear_bit(0, &dev->hw_lock);
  110. spin_unlock_irqrestore(&dev->irqlock, flags);
  111. /* Double check if there is at least one instance running.
  112. * If no instance is in memory than no firmware should be present */
  113. if (dev->num_inst > 0) {
  114. ret = s5p_mfc_reload_firmware(dev);
  115. if (ret) {
  116. mfc_err("Failed to reload FW\n");
  117. goto unlock;
  118. }
  119. s5p_mfc_clock_on();
  120. ret = s5p_mfc_init_hw(dev);
  121. if (ret)
  122. mfc_err("Failed to reinit FW\n");
  123. }
  124. unlock:
  125. if (mutex_locked)
  126. mutex_unlock(&dev->mfc_mutex);
  127. }
  128. static enum s5p_mfc_node_type s5p_mfc_get_node_type(struct file *file)
  129. {
  130. struct video_device *vdev = video_devdata(file);
  131. if (!vdev) {
  132. mfc_err("failed to get video_device");
  133. return MFCNODE_INVALID;
  134. }
  135. if (vdev->index == 0)
  136. return MFCNODE_DECODER;
  137. else if (vdev->index == 1)
  138. return MFCNODE_ENCODER;
  139. return MFCNODE_INVALID;
  140. }
  141. static void s5p_mfc_clear_int_flags(struct s5p_mfc_dev *dev)
  142. {
  143. mfc_write(dev, 0, S5P_FIMV_RISC_HOST_INT);
  144. mfc_write(dev, 0, S5P_FIMV_RISC2HOST_CMD);
  145. mfc_write(dev, 0xffff, S5P_FIMV_SI_RTN_CHID);
  146. }
  147. static void s5p_mfc_handle_frame_all_extracted(struct s5p_mfc_ctx *ctx)
  148. {
  149. struct s5p_mfc_buf *dst_buf;
  150. ctx->state = MFCINST_FINISHED;
  151. ctx->sequence++;
  152. while (!list_empty(&ctx->dst_queue)) {
  153. dst_buf = list_entry(ctx->dst_queue.next,
  154. struct s5p_mfc_buf, list);
  155. mfc_debug(2, "Cleaning up buffer: %d\n",
  156. dst_buf->b->v4l2_buf.index);
  157. vb2_set_plane_payload(dst_buf->b, 0, 0);
  158. vb2_set_plane_payload(dst_buf->b, 1, 0);
  159. list_del(&dst_buf->list);
  160. ctx->dst_queue_cnt--;
  161. dst_buf->b->v4l2_buf.sequence = (ctx->sequence++);
  162. if (s5p_mfc_read_shm(ctx, PIC_TIME_TOP) ==
  163. s5p_mfc_read_shm(ctx, PIC_TIME_BOT))
  164. dst_buf->b->v4l2_buf.field = V4L2_FIELD_NONE;
  165. else
  166. dst_buf->b->v4l2_buf.field = V4L2_FIELD_INTERLACED;
  167. ctx->dec_dst_flag &= ~(1 << dst_buf->b->v4l2_buf.index);
  168. vb2_buffer_done(dst_buf->b, VB2_BUF_STATE_DONE);
  169. }
  170. }
  171. static void s5p_mfc_handle_frame_copy_time(struct s5p_mfc_ctx *ctx)
  172. {
  173. struct s5p_mfc_dev *dev = ctx->dev;
  174. struct s5p_mfc_buf *dst_buf, *src_buf;
  175. size_t dec_y_addr = s5p_mfc_get_dec_y_adr();
  176. unsigned int frame_type = s5p_mfc_get_frame_type();
  177. /* Copy timestamp / timecode from decoded src to dst and set
  178. appropraite flags */
  179. src_buf = list_entry(ctx->src_queue.next, struct s5p_mfc_buf, list);
  180. list_for_each_entry(dst_buf, &ctx->dst_queue, list) {
  181. if (vb2_dma_contig_plane_dma_addr(dst_buf->b, 0) == dec_y_addr) {
  182. memcpy(&dst_buf->b->v4l2_buf.timecode,
  183. &src_buf->b->v4l2_buf.timecode,
  184. sizeof(struct v4l2_timecode));
  185. memcpy(&dst_buf->b->v4l2_buf.timestamp,
  186. &src_buf->b->v4l2_buf.timestamp,
  187. sizeof(struct timeval));
  188. switch (frame_type) {
  189. case S5P_FIMV_DECODE_FRAME_I_FRAME:
  190. dst_buf->b->v4l2_buf.flags |=
  191. V4L2_BUF_FLAG_KEYFRAME;
  192. break;
  193. case S5P_FIMV_DECODE_FRAME_P_FRAME:
  194. dst_buf->b->v4l2_buf.flags |=
  195. V4L2_BUF_FLAG_PFRAME;
  196. break;
  197. case S5P_FIMV_DECODE_FRAME_B_FRAME:
  198. dst_buf->b->v4l2_buf.flags |=
  199. V4L2_BUF_FLAG_BFRAME;
  200. break;
  201. }
  202. break;
  203. }
  204. }
  205. }
  206. static void s5p_mfc_handle_frame_new(struct s5p_mfc_ctx *ctx, unsigned int err)
  207. {
  208. struct s5p_mfc_dev *dev = ctx->dev;
  209. struct s5p_mfc_buf *dst_buf;
  210. size_t dspl_y_addr = s5p_mfc_get_dspl_y_adr();
  211. unsigned int frame_type = s5p_mfc_get_frame_type();
  212. unsigned int index;
  213. /* If frame is same as previous then skip and do not dequeue */
  214. if (frame_type == S5P_FIMV_DECODE_FRAME_SKIPPED) {
  215. if (!ctx->after_packed_pb)
  216. ctx->sequence++;
  217. ctx->after_packed_pb = 0;
  218. return;
  219. }
  220. ctx->sequence++;
  221. /* The MFC returns address of the buffer, now we have to
  222. * check which videobuf does it correspond to */
  223. list_for_each_entry(dst_buf, &ctx->dst_queue, list) {
  224. /* Check if this is the buffer we're looking for */
  225. if (vb2_dma_contig_plane_dma_addr(dst_buf->b, 0) == dspl_y_addr) {
  226. list_del(&dst_buf->list);
  227. ctx->dst_queue_cnt--;
  228. dst_buf->b->v4l2_buf.sequence = ctx->sequence;
  229. if (s5p_mfc_read_shm(ctx, PIC_TIME_TOP) ==
  230. s5p_mfc_read_shm(ctx, PIC_TIME_BOT))
  231. dst_buf->b->v4l2_buf.field = V4L2_FIELD_NONE;
  232. else
  233. dst_buf->b->v4l2_buf.field =
  234. V4L2_FIELD_INTERLACED;
  235. vb2_set_plane_payload(dst_buf->b, 0, ctx->luma_size);
  236. vb2_set_plane_payload(dst_buf->b, 1, ctx->chroma_size);
  237. clear_bit(dst_buf->b->v4l2_buf.index,
  238. &ctx->dec_dst_flag);
  239. vb2_buffer_done(dst_buf->b,
  240. err ? VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
  241. index = dst_buf->b->v4l2_buf.index;
  242. break;
  243. }
  244. }
  245. }
  246. /* Handle frame decoding interrupt */
  247. static void s5p_mfc_handle_frame(struct s5p_mfc_ctx *ctx,
  248. unsigned int reason, unsigned int err)
  249. {
  250. struct s5p_mfc_dev *dev = ctx->dev;
  251. unsigned int dst_frame_status;
  252. struct s5p_mfc_buf *src_buf;
  253. unsigned long flags;
  254. unsigned int res_change;
  255. unsigned int index;
  256. dst_frame_status = s5p_mfc_get_dspl_status()
  257. & S5P_FIMV_DEC_STATUS_DECODING_STATUS_MASK;
  258. res_change = s5p_mfc_get_dspl_status()
  259. & S5P_FIMV_DEC_STATUS_RESOLUTION_MASK;
  260. mfc_debug(2, "Frame Status: %x\n", dst_frame_status);
  261. if (ctx->state == MFCINST_RES_CHANGE_INIT)
  262. ctx->state = MFCINST_RES_CHANGE_FLUSH;
  263. if (res_change) {
  264. ctx->state = MFCINST_RES_CHANGE_INIT;
  265. s5p_mfc_clear_int_flags(dev);
  266. wake_up_ctx(ctx, reason, err);
  267. if (test_and_clear_bit(0, &dev->hw_lock) == 0)
  268. BUG();
  269. s5p_mfc_clock_off();
  270. s5p_mfc_try_run(dev);
  271. return;
  272. }
  273. if (ctx->dpb_flush_flag)
  274. ctx->dpb_flush_flag = 0;
  275. spin_lock_irqsave(&dev->irqlock, flags);
  276. /* All frames remaining in the buffer have been extracted */
  277. if (dst_frame_status == S5P_FIMV_DEC_STATUS_DECODING_EMPTY) {
  278. if (ctx->state == MFCINST_RES_CHANGE_FLUSH) {
  279. s5p_mfc_handle_frame_all_extracted(ctx);
  280. ctx->state = MFCINST_RES_CHANGE_END;
  281. goto leave_handle_frame;
  282. } else {
  283. s5p_mfc_handle_frame_all_extracted(ctx);
  284. }
  285. }
  286. if (dst_frame_status == S5P_FIMV_DEC_STATUS_DECODING_DISPLAY ||
  287. dst_frame_status == S5P_FIMV_DEC_STATUS_DECODING_ONLY)
  288. s5p_mfc_handle_frame_copy_time(ctx);
  289. /* A frame has been decoded and is in the buffer */
  290. if (dst_frame_status == S5P_FIMV_DEC_STATUS_DISPLAY_ONLY ||
  291. dst_frame_status == S5P_FIMV_DEC_STATUS_DECODING_DISPLAY) {
  292. s5p_mfc_handle_frame_new(ctx, err);
  293. } else {
  294. mfc_debug(2, "No frame decode\n");
  295. }
  296. /* Mark source buffer as complete */
  297. if (dst_frame_status != S5P_FIMV_DEC_STATUS_DISPLAY_ONLY
  298. && !list_empty(&ctx->src_queue)) {
  299. src_buf = list_entry(ctx->src_queue.next, struct s5p_mfc_buf,
  300. list);
  301. ctx->consumed_stream += s5p_mfc_get_consumed_stream();
  302. if (ctx->codec_mode != S5P_FIMV_CODEC_H264_DEC &&
  303. s5p_mfc_get_frame_type() == S5P_FIMV_DECODE_FRAME_P_FRAME
  304. && ctx->consumed_stream + STUFF_BYTE <
  305. src_buf->b->v4l2_planes[0].bytesused) {
  306. /* Run MFC again on the same buffer */
  307. mfc_debug(2, "Running again the same buffer\n");
  308. ctx->after_packed_pb = 1;
  309. } else {
  310. index = src_buf->b->v4l2_buf.index;
  311. mfc_debug(2, "MFC needs next buffer\n");
  312. ctx->consumed_stream = 0;
  313. list_del(&src_buf->list);
  314. ctx->src_queue_cnt--;
  315. if (s5p_mfc_err_dec(err) > 0)
  316. vb2_buffer_done(src_buf->b, VB2_BUF_STATE_ERROR);
  317. else
  318. vb2_buffer_done(src_buf->b, VB2_BUF_STATE_DONE);
  319. }
  320. }
  321. leave_handle_frame:
  322. spin_unlock_irqrestore(&dev->irqlock, flags);
  323. if ((ctx->src_queue_cnt == 0 && ctx->state != MFCINST_FINISHING)
  324. || ctx->dst_queue_cnt < ctx->dpb_count)
  325. clear_work_bit(ctx);
  326. s5p_mfc_clear_int_flags(dev);
  327. wake_up_ctx(ctx, reason, err);
  328. if (test_and_clear_bit(0, &dev->hw_lock) == 0)
  329. BUG();
  330. s5p_mfc_clock_off();
  331. s5p_mfc_try_run(dev);
  332. }
  333. /* Error handling for interrupt */
  334. static void s5p_mfc_handle_error(struct s5p_mfc_ctx *ctx,
  335. unsigned int reason, unsigned int err)
  336. {
  337. struct s5p_mfc_dev *dev;
  338. unsigned long flags;
  339. /* If no context is available then all necessary
  340. * processing has been done. */
  341. if (ctx == NULL)
  342. return;
  343. dev = ctx->dev;
  344. mfc_err("Interrupt Error: %08x\n", err);
  345. s5p_mfc_clear_int_flags(dev);
  346. wake_up_dev(dev, reason, err);
  347. /* Error recovery is dependent on the state of context */
  348. switch (ctx->state) {
  349. case MFCINST_INIT:
  350. /* This error had to happen while acquireing instance */
  351. case MFCINST_GOT_INST:
  352. /* This error had to happen while parsing the header */
  353. case MFCINST_HEAD_PARSED:
  354. /* This error had to happen while setting dst buffers */
  355. case MFCINST_RETURN_INST:
  356. /* This error had to happen while releasing instance */
  357. clear_work_bit(ctx);
  358. wake_up_ctx(ctx, reason, err);
  359. if (test_and_clear_bit(0, &dev->hw_lock) == 0)
  360. BUG();
  361. s5p_mfc_clock_off();
  362. ctx->state = MFCINST_ERROR;
  363. break;
  364. case MFCINST_FINISHING:
  365. case MFCINST_FINISHED:
  366. case MFCINST_RUNNING:
  367. /* It is higly probable that an error occured
  368. * while decoding a frame */
  369. clear_work_bit(ctx);
  370. ctx->state = MFCINST_ERROR;
  371. /* Mark all dst buffers as having an error */
  372. spin_lock_irqsave(&dev->irqlock, flags);
  373. s5p_mfc_cleanup_queue(&ctx->dst_queue, &ctx->vq_dst);
  374. /* Mark all src buffers as having an error */
  375. s5p_mfc_cleanup_queue(&ctx->src_queue, &ctx->vq_src);
  376. spin_unlock_irqrestore(&dev->irqlock, flags);
  377. if (test_and_clear_bit(0, &dev->hw_lock) == 0)
  378. BUG();
  379. s5p_mfc_clock_off();
  380. break;
  381. default:
  382. mfc_err("Encountered an error interrupt which had not been handled\n");
  383. break;
  384. }
  385. return;
  386. }
  387. /* Header parsing interrupt handling */
  388. static void s5p_mfc_handle_seq_done(struct s5p_mfc_ctx *ctx,
  389. unsigned int reason, unsigned int err)
  390. {
  391. struct s5p_mfc_dev *dev;
  392. unsigned int guard_width, guard_height;
  393. if (ctx == NULL)
  394. return;
  395. dev = ctx->dev;
  396. if (ctx->c_ops->post_seq_start) {
  397. if (ctx->c_ops->post_seq_start(ctx))
  398. mfc_err("post_seq_start() failed\n");
  399. } else {
  400. ctx->img_width = s5p_mfc_get_img_width();
  401. ctx->img_height = s5p_mfc_get_img_height();
  402. ctx->buf_width = ALIGN(ctx->img_width,
  403. S5P_FIMV_NV12MT_HALIGN);
  404. ctx->buf_height = ALIGN(ctx->img_height,
  405. S5P_FIMV_NV12MT_VALIGN);
  406. mfc_debug(2, "SEQ Done: Movie dimensions %dx%d, "
  407. "buffer dimensions: %dx%d\n", ctx->img_width,
  408. ctx->img_height, ctx->buf_width,
  409. ctx->buf_height);
  410. if (ctx->codec_mode == S5P_FIMV_CODEC_H264_DEC) {
  411. ctx->luma_size = ALIGN(ctx->buf_width *
  412. ctx->buf_height, S5P_FIMV_DEC_BUF_ALIGN);
  413. ctx->chroma_size = ALIGN(ctx->buf_width *
  414. ALIGN((ctx->img_height >> 1),
  415. S5P_FIMV_NV12MT_VALIGN),
  416. S5P_FIMV_DEC_BUF_ALIGN);
  417. ctx->mv_size = ALIGN(ctx->buf_width *
  418. ALIGN((ctx->buf_height >> 2),
  419. S5P_FIMV_NV12MT_VALIGN),
  420. S5P_FIMV_DEC_BUF_ALIGN);
  421. } else {
  422. guard_width = ALIGN(ctx->img_width + 24,
  423. S5P_FIMV_NV12MT_HALIGN);
  424. guard_height = ALIGN(ctx->img_height + 16,
  425. S5P_FIMV_NV12MT_VALIGN);
  426. ctx->luma_size = ALIGN(guard_width *
  427. guard_height, S5P_FIMV_DEC_BUF_ALIGN);
  428. guard_width = ALIGN(ctx->img_width + 16,
  429. S5P_FIMV_NV12MT_HALIGN);
  430. guard_height = ALIGN((ctx->img_height >> 1) + 4,
  431. S5P_FIMV_NV12MT_VALIGN);
  432. ctx->chroma_size = ALIGN(guard_width *
  433. guard_height, S5P_FIMV_DEC_BUF_ALIGN);
  434. ctx->mv_size = 0;
  435. }
  436. ctx->dpb_count = s5p_mfc_get_dpb_count();
  437. if (ctx->img_width == 0 || ctx->img_height == 0)
  438. ctx->state = MFCINST_ERROR;
  439. else
  440. ctx->state = MFCINST_HEAD_PARSED;
  441. }
  442. s5p_mfc_clear_int_flags(dev);
  443. clear_work_bit(ctx);
  444. if (test_and_clear_bit(0, &dev->hw_lock) == 0)
  445. BUG();
  446. s5p_mfc_clock_off();
  447. s5p_mfc_try_run(dev);
  448. wake_up_ctx(ctx, reason, err);
  449. }
  450. /* Header parsing interrupt handling */
  451. static void s5p_mfc_handle_init_buffers(struct s5p_mfc_ctx *ctx,
  452. unsigned int reason, unsigned int err)
  453. {
  454. struct s5p_mfc_buf *src_buf;
  455. struct s5p_mfc_dev *dev;
  456. unsigned long flags;
  457. if (ctx == NULL)
  458. return;
  459. dev = ctx->dev;
  460. s5p_mfc_clear_int_flags(dev);
  461. ctx->int_type = reason;
  462. ctx->int_err = err;
  463. ctx->int_cond = 1;
  464. spin_lock(&dev->condlock);
  465. clear_bit(ctx->num, &dev->ctx_work_bits);
  466. spin_unlock(&dev->condlock);
  467. if (err == 0) {
  468. ctx->state = MFCINST_RUNNING;
  469. if (!ctx->dpb_flush_flag) {
  470. spin_lock_irqsave(&dev->irqlock, flags);
  471. if (!list_empty(&ctx->src_queue)) {
  472. src_buf = list_entry(ctx->src_queue.next,
  473. struct s5p_mfc_buf, list);
  474. list_del(&src_buf->list);
  475. ctx->src_queue_cnt--;
  476. vb2_buffer_done(src_buf->b,
  477. VB2_BUF_STATE_DONE);
  478. }
  479. spin_unlock_irqrestore(&dev->irqlock, flags);
  480. } else {
  481. ctx->dpb_flush_flag = 0;
  482. }
  483. if (test_and_clear_bit(0, &dev->hw_lock) == 0)
  484. BUG();
  485. s5p_mfc_clock_off();
  486. wake_up(&ctx->queue);
  487. s5p_mfc_try_run(dev);
  488. } else {
  489. if (test_and_clear_bit(0, &dev->hw_lock) == 0)
  490. BUG();
  491. s5p_mfc_clock_off();
  492. wake_up(&ctx->queue);
  493. }
  494. }
  495. /* Interrupt processing */
  496. static irqreturn_t s5p_mfc_irq(int irq, void *priv)
  497. {
  498. struct s5p_mfc_dev *dev = priv;
  499. struct s5p_mfc_ctx *ctx;
  500. unsigned int reason;
  501. unsigned int err;
  502. mfc_debug_enter();
  503. /* Reset the timeout watchdog */
  504. atomic_set(&dev->watchdog_cnt, 0);
  505. ctx = dev->ctx[dev->curr_ctx];
  506. /* Get the reason of interrupt and the error code */
  507. reason = s5p_mfc_get_int_reason();
  508. err = s5p_mfc_get_int_err();
  509. mfc_debug(1, "Int reason: %d (err: %08x)\n", reason, err);
  510. switch (reason) {
  511. case S5P_FIMV_R2H_CMD_ERR_RET:
  512. /* An error has occured */
  513. if (ctx->state == MFCINST_RUNNING &&
  514. s5p_mfc_err_dec(err) >= S5P_FIMV_ERR_WARNINGS_START)
  515. s5p_mfc_handle_frame(ctx, reason, err);
  516. else
  517. s5p_mfc_handle_error(ctx, reason, err);
  518. clear_bit(0, &dev->enter_suspend);
  519. break;
  520. case S5P_FIMV_R2H_CMD_SLICE_DONE_RET:
  521. case S5P_FIMV_R2H_CMD_FRAME_DONE_RET:
  522. if (ctx->c_ops->post_frame_start) {
  523. if (ctx->c_ops->post_frame_start(ctx))
  524. mfc_err("post_frame_start() failed\n");
  525. s5p_mfc_clear_int_flags(dev);
  526. wake_up_ctx(ctx, reason, err);
  527. if (test_and_clear_bit(0, &dev->hw_lock) == 0)
  528. BUG();
  529. s5p_mfc_clock_off();
  530. s5p_mfc_try_run(dev);
  531. } else {
  532. s5p_mfc_handle_frame(ctx, reason, err);
  533. }
  534. break;
  535. case S5P_FIMV_R2H_CMD_SEQ_DONE_RET:
  536. s5p_mfc_handle_seq_done(ctx, reason, err);
  537. break;
  538. case S5P_FIMV_R2H_CMD_OPEN_INSTANCE_RET:
  539. ctx->inst_no = s5p_mfc_get_inst_no();
  540. ctx->state = MFCINST_GOT_INST;
  541. clear_work_bit(ctx);
  542. wake_up(&ctx->queue);
  543. goto irq_cleanup_hw;
  544. case S5P_FIMV_R2H_CMD_CLOSE_INSTANCE_RET:
  545. clear_work_bit(ctx);
  546. ctx->state = MFCINST_FREE;
  547. wake_up(&ctx->queue);
  548. goto irq_cleanup_hw;
  549. case S5P_FIMV_R2H_CMD_SYS_INIT_RET:
  550. case S5P_FIMV_R2H_CMD_FW_STATUS_RET:
  551. case S5P_FIMV_R2H_CMD_SLEEP_RET:
  552. case S5P_FIMV_R2H_CMD_WAKEUP_RET:
  553. if (ctx)
  554. clear_work_bit(ctx);
  555. s5p_mfc_clear_int_flags(dev);
  556. wake_up_dev(dev, reason, err);
  557. clear_bit(0, &dev->hw_lock);
  558. clear_bit(0, &dev->enter_suspend);
  559. break;
  560. case S5P_FIMV_R2H_CMD_INIT_BUFFERS_RET:
  561. s5p_mfc_handle_init_buffers(ctx, reason, err);
  562. break;
  563. default:
  564. mfc_debug(2, "Unknown int reason\n");
  565. s5p_mfc_clear_int_flags(dev);
  566. }
  567. mfc_debug_leave();
  568. return IRQ_HANDLED;
  569. irq_cleanup_hw:
  570. s5p_mfc_clear_int_flags(dev);
  571. ctx->int_type = reason;
  572. ctx->int_err = err;
  573. ctx->int_cond = 1;
  574. if (test_and_clear_bit(0, &dev->hw_lock) == 0)
  575. mfc_err("Failed to unlock hw\n");
  576. s5p_mfc_clock_off();
  577. s5p_mfc_try_run(dev);
  578. mfc_debug(2, "Exit via irq_cleanup_hw\n");
  579. return IRQ_HANDLED;
  580. }
  581. /* Open an MFC node */
  582. static int s5p_mfc_open(struct file *file)
  583. {
  584. struct s5p_mfc_dev *dev = video_drvdata(file);
  585. struct s5p_mfc_ctx *ctx = NULL;
  586. struct vb2_queue *q;
  587. unsigned long flags;
  588. int ret = 0;
  589. mfc_debug_enter();
  590. if (mutex_lock_interruptible(&dev->mfc_mutex))
  591. return -ERESTARTSYS;
  592. dev->num_inst++; /* It is guarded by mfc_mutex in vfd */
  593. /* Allocate memory for context */
  594. ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
  595. if (!ctx) {
  596. mfc_err("Not enough memory\n");
  597. ret = -ENOMEM;
  598. goto err_alloc;
  599. }
  600. v4l2_fh_init(&ctx->fh, video_devdata(file));
  601. file->private_data = &ctx->fh;
  602. v4l2_fh_add(&ctx->fh);
  603. ctx->dev = dev;
  604. INIT_LIST_HEAD(&ctx->src_queue);
  605. INIT_LIST_HEAD(&ctx->dst_queue);
  606. ctx->src_queue_cnt = 0;
  607. ctx->dst_queue_cnt = 0;
  608. /* Get context number */
  609. ctx->num = 0;
  610. while (dev->ctx[ctx->num]) {
  611. ctx->num++;
  612. if (ctx->num >= MFC_NUM_CONTEXTS) {
  613. mfc_err("Too many open contexts\n");
  614. ret = -EBUSY;
  615. goto err_no_ctx;
  616. }
  617. }
  618. /* Mark context as idle */
  619. spin_lock_irqsave(&dev->condlock, flags);
  620. clear_bit(ctx->num, &dev->ctx_work_bits);
  621. spin_unlock_irqrestore(&dev->condlock, flags);
  622. dev->ctx[ctx->num] = ctx;
  623. if (s5p_mfc_get_node_type(file) == MFCNODE_DECODER) {
  624. ctx->type = MFCINST_DECODER;
  625. ctx->c_ops = get_dec_codec_ops();
  626. /* Setup ctrl handler */
  627. ret = s5p_mfc_dec_ctrls_setup(ctx);
  628. if (ret) {
  629. mfc_err("Failed to setup mfc controls\n");
  630. goto err_ctrls_setup;
  631. }
  632. } else if (s5p_mfc_get_node_type(file) == MFCNODE_ENCODER) {
  633. ctx->type = MFCINST_ENCODER;
  634. ctx->c_ops = get_enc_codec_ops();
  635. /* only for encoder */
  636. INIT_LIST_HEAD(&ctx->ref_queue);
  637. ctx->ref_queue_cnt = 0;
  638. /* Setup ctrl handler */
  639. ret = s5p_mfc_enc_ctrls_setup(ctx);
  640. if (ret) {
  641. mfc_err("Failed to setup mfc controls\n");
  642. goto err_ctrls_setup;
  643. }
  644. } else {
  645. ret = -ENOENT;
  646. goto err_bad_node;
  647. }
  648. ctx->fh.ctrl_handler = &ctx->ctrl_handler;
  649. ctx->inst_no = -1;
  650. /* Load firmware if this is the first instance */
  651. if (dev->num_inst == 1) {
  652. dev->watchdog_timer.expires = jiffies +
  653. msecs_to_jiffies(MFC_WATCHDOG_INTERVAL);
  654. add_timer(&dev->watchdog_timer);
  655. ret = s5p_mfc_power_on();
  656. if (ret < 0) {
  657. mfc_err("power on failed\n");
  658. goto err_pwr_enable;
  659. }
  660. s5p_mfc_clock_on();
  661. ret = s5p_mfc_alloc_and_load_firmware(dev);
  662. if (ret)
  663. goto err_alloc_fw;
  664. /* Init the FW */
  665. ret = s5p_mfc_init_hw(dev);
  666. if (ret)
  667. goto err_init_hw;
  668. s5p_mfc_clock_off();
  669. }
  670. /* Init videobuf2 queue for CAPTURE */
  671. q = &ctx->vq_dst;
  672. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  673. q->drv_priv = &ctx->fh;
  674. if (s5p_mfc_get_node_type(file) == MFCNODE_DECODER) {
  675. q->io_modes = VB2_MMAP;
  676. q->ops = get_dec_queue_ops();
  677. } else if (s5p_mfc_get_node_type(file) == MFCNODE_ENCODER) {
  678. q->io_modes = VB2_MMAP | VB2_USERPTR;
  679. q->ops = get_enc_queue_ops();
  680. } else {
  681. ret = -ENOENT;
  682. goto err_queue_init;
  683. }
  684. q->mem_ops = (struct vb2_mem_ops *)&vb2_dma_contig_memops;
  685. ret = vb2_queue_init(q);
  686. if (ret) {
  687. mfc_err("Failed to initialize videobuf2 queue(capture)\n");
  688. goto err_queue_init;
  689. }
  690. /* Init videobuf2 queue for OUTPUT */
  691. q = &ctx->vq_src;
  692. q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
  693. q->io_modes = VB2_MMAP;
  694. q->drv_priv = &ctx->fh;
  695. if (s5p_mfc_get_node_type(file) == MFCNODE_DECODER) {
  696. q->io_modes = VB2_MMAP;
  697. q->ops = get_dec_queue_ops();
  698. } else if (s5p_mfc_get_node_type(file) == MFCNODE_ENCODER) {
  699. q->io_modes = VB2_MMAP | VB2_USERPTR;
  700. q->ops = get_enc_queue_ops();
  701. } else {
  702. ret = -ENOENT;
  703. goto err_queue_init;
  704. }
  705. q->mem_ops = (struct vb2_mem_ops *)&vb2_dma_contig_memops;
  706. ret = vb2_queue_init(q);
  707. if (ret) {
  708. mfc_err("Failed to initialize videobuf2 queue(output)\n");
  709. goto err_queue_init;
  710. }
  711. init_waitqueue_head(&ctx->queue);
  712. mutex_unlock(&dev->mfc_mutex);
  713. mfc_debug_leave();
  714. return ret;
  715. /* Deinit when failure occured */
  716. err_queue_init:
  717. err_init_hw:
  718. s5p_mfc_release_firmware(dev);
  719. err_alloc_fw:
  720. dev->ctx[ctx->num] = NULL;
  721. del_timer_sync(&dev->watchdog_timer);
  722. s5p_mfc_clock_off();
  723. err_pwr_enable:
  724. if (dev->num_inst == 1) {
  725. if (s5p_mfc_power_off() < 0)
  726. mfc_err("power off failed\n");
  727. s5p_mfc_release_firmware(dev);
  728. }
  729. err_ctrls_setup:
  730. s5p_mfc_dec_ctrls_delete(ctx);
  731. err_bad_node:
  732. err_no_ctx:
  733. v4l2_fh_del(&ctx->fh);
  734. v4l2_fh_exit(&ctx->fh);
  735. kfree(ctx);
  736. err_alloc:
  737. dev->num_inst--;
  738. mutex_unlock(&dev->mfc_mutex);
  739. mfc_debug_leave();
  740. return ret;
  741. }
  742. /* Release MFC context */
  743. static int s5p_mfc_release(struct file *file)
  744. {
  745. struct s5p_mfc_ctx *ctx = fh_to_ctx(file->private_data);
  746. struct s5p_mfc_dev *dev = ctx->dev;
  747. unsigned long flags;
  748. mfc_debug_enter();
  749. mutex_lock(&dev->mfc_mutex);
  750. s5p_mfc_clock_on();
  751. vb2_queue_release(&ctx->vq_src);
  752. vb2_queue_release(&ctx->vq_dst);
  753. /* Mark context as idle */
  754. spin_lock_irqsave(&dev->condlock, flags);
  755. clear_bit(ctx->num, &dev->ctx_work_bits);
  756. spin_unlock_irqrestore(&dev->condlock, flags);
  757. /* If instance was initialised then
  758. * return instance and free reosurces */
  759. if (ctx->inst_no != MFC_NO_INSTANCE_SET) {
  760. mfc_debug(2, "Has to free instance\n");
  761. ctx->state = MFCINST_RETURN_INST;
  762. spin_lock_irqsave(&dev->condlock, flags);
  763. set_bit(ctx->num, &dev->ctx_work_bits);
  764. spin_unlock_irqrestore(&dev->condlock, flags);
  765. s5p_mfc_clean_ctx_int_flags(ctx);
  766. s5p_mfc_try_run(dev);
  767. /* Wait until instance is returned or timeout occured */
  768. if (s5p_mfc_wait_for_done_ctx
  769. (ctx, S5P_FIMV_R2H_CMD_CLOSE_INSTANCE_RET, 0)) {
  770. s5p_mfc_clock_off();
  771. mfc_err("Err returning instance\n");
  772. }
  773. mfc_debug(2, "After free instance\n");
  774. /* Free resources */
  775. s5p_mfc_release_codec_buffers(ctx);
  776. s5p_mfc_release_instance_buffer(ctx);
  777. if (ctx->type == MFCINST_DECODER)
  778. s5p_mfc_release_dec_desc_buffer(ctx);
  779. ctx->inst_no = MFC_NO_INSTANCE_SET;
  780. }
  781. /* hardware locking scheme */
  782. if (dev->curr_ctx == ctx->num)
  783. clear_bit(0, &dev->hw_lock);
  784. dev->num_inst--;
  785. if (dev->num_inst == 0) {
  786. mfc_debug(2, "Last instance - release firmware\n");
  787. /* reset <-> F/W release */
  788. s5p_mfc_reset(dev);
  789. s5p_mfc_release_firmware(dev);
  790. del_timer_sync(&dev->watchdog_timer);
  791. if (s5p_mfc_power_off() < 0)
  792. mfc_err("Power off failed\n");
  793. }
  794. mfc_debug(2, "Shutting down clock\n");
  795. s5p_mfc_clock_off();
  796. dev->ctx[ctx->num] = NULL;
  797. s5p_mfc_dec_ctrls_delete(ctx);
  798. v4l2_fh_del(&ctx->fh);
  799. v4l2_fh_exit(&ctx->fh);
  800. kfree(ctx);
  801. mfc_debug_leave();
  802. mutex_unlock(&dev->mfc_mutex);
  803. return 0;
  804. }
  805. /* Poll */
  806. static unsigned int s5p_mfc_poll(struct file *file,
  807. struct poll_table_struct *wait)
  808. {
  809. struct s5p_mfc_ctx *ctx = fh_to_ctx(file->private_data);
  810. struct s5p_mfc_dev *dev = ctx->dev;
  811. struct vb2_queue *src_q, *dst_q;
  812. struct vb2_buffer *src_vb = NULL, *dst_vb = NULL;
  813. unsigned int rc = 0;
  814. unsigned long flags;
  815. mutex_lock(&dev->mfc_mutex);
  816. src_q = &ctx->vq_src;
  817. dst_q = &ctx->vq_dst;
  818. /*
  819. * There has to be at least one buffer queued on each queued_list, which
  820. * means either in driver already or waiting for driver to claim it
  821. * and start processing.
  822. */
  823. if ((!src_q->streaming || list_empty(&src_q->queued_list))
  824. && (!dst_q->streaming || list_empty(&dst_q->queued_list))) {
  825. rc = POLLERR;
  826. goto end;
  827. }
  828. mutex_unlock(&dev->mfc_mutex);
  829. poll_wait(file, &src_q->done_wq, wait);
  830. poll_wait(file, &dst_q->done_wq, wait);
  831. mutex_lock(&dev->mfc_mutex);
  832. spin_lock_irqsave(&src_q->done_lock, flags);
  833. if (!list_empty(&src_q->done_list))
  834. src_vb = list_first_entry(&src_q->done_list, struct vb2_buffer,
  835. done_entry);
  836. if (src_vb && (src_vb->state == VB2_BUF_STATE_DONE
  837. || src_vb->state == VB2_BUF_STATE_ERROR))
  838. rc |= POLLOUT | POLLWRNORM;
  839. spin_unlock_irqrestore(&src_q->done_lock, flags);
  840. spin_lock_irqsave(&dst_q->done_lock, flags);
  841. if (!list_empty(&dst_q->done_list))
  842. dst_vb = list_first_entry(&dst_q->done_list, struct vb2_buffer,
  843. done_entry);
  844. if (dst_vb && (dst_vb->state == VB2_BUF_STATE_DONE
  845. || dst_vb->state == VB2_BUF_STATE_ERROR))
  846. rc |= POLLIN | POLLRDNORM;
  847. spin_unlock_irqrestore(&dst_q->done_lock, flags);
  848. end:
  849. mutex_unlock(&dev->mfc_mutex);
  850. return rc;
  851. }
  852. /* Mmap */
  853. static int s5p_mfc_mmap(struct file *file, struct vm_area_struct *vma)
  854. {
  855. struct s5p_mfc_ctx *ctx = fh_to_ctx(file->private_data);
  856. struct s5p_mfc_dev *dev = ctx->dev;
  857. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  858. int ret;
  859. if (mutex_lock_interruptible(&dev->mfc_mutex))
  860. return -ERESTARTSYS;
  861. if (offset < DST_QUEUE_OFF_BASE) {
  862. mfc_debug(2, "mmaping source\n");
  863. ret = vb2_mmap(&ctx->vq_src, vma);
  864. } else { /* capture */
  865. mfc_debug(2, "mmaping destination\n");
  866. vma->vm_pgoff -= (DST_QUEUE_OFF_BASE >> PAGE_SHIFT);
  867. ret = vb2_mmap(&ctx->vq_dst, vma);
  868. }
  869. mutex_unlock(&dev->mfc_mutex);
  870. return ret;
  871. }
  872. /* v4l2 ops */
  873. static const struct v4l2_file_operations s5p_mfc_fops = {
  874. .owner = THIS_MODULE,
  875. .open = s5p_mfc_open,
  876. .release = s5p_mfc_release,
  877. .poll = s5p_mfc_poll,
  878. .unlocked_ioctl = video_ioctl2,
  879. .mmap = s5p_mfc_mmap,
  880. };
  881. static int match_child(struct device *dev, void *data)
  882. {
  883. if (!dev_name(dev))
  884. return 0;
  885. return !strcmp(dev_name(dev), (char *)data);
  886. }
  887. /* MFC probe function */
  888. static int s5p_mfc_probe(struct platform_device *pdev)
  889. {
  890. struct s5p_mfc_dev *dev;
  891. struct video_device *vfd;
  892. struct resource *res;
  893. int ret;
  894. pr_debug("%s++\n", __func__);
  895. dev = devm_kzalloc(&pdev->dev, sizeof *dev, GFP_KERNEL);
  896. if (!dev) {
  897. dev_err(&pdev->dev, "Not enough memory for MFC device\n");
  898. return -ENOMEM;
  899. }
  900. spin_lock_init(&dev->irqlock);
  901. spin_lock_init(&dev->condlock);
  902. dev->plat_dev = pdev;
  903. if (!dev->plat_dev) {
  904. dev_err(&pdev->dev, "No platform data specified\n");
  905. return -ENODEV;
  906. }
  907. ret = s5p_mfc_init_pm(dev);
  908. if (ret < 0) {
  909. dev_err(&pdev->dev, "failed to get mfc clock source\n");
  910. return ret;
  911. }
  912. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  913. dev->regs_base = devm_request_and_ioremap(&pdev->dev, res);
  914. if (dev->regs_base == NULL) {
  915. dev_err(&pdev->dev, "Failed to obtain io memory\n");
  916. return -ENOENT;
  917. }
  918. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  919. if (res == NULL) {
  920. dev_err(&pdev->dev, "failed to get irq resource\n");
  921. ret = -ENOENT;
  922. goto err_res;
  923. }
  924. dev->irq = res->start;
  925. ret = devm_request_irq(&pdev->dev, dev->irq, s5p_mfc_irq,
  926. IRQF_DISABLED, pdev->name, dev);
  927. if (ret) {
  928. dev_err(&pdev->dev, "Failed to install irq (%d)\n", ret);
  929. goto err_res;
  930. }
  931. dev->mem_dev_l = device_find_child(&dev->plat_dev->dev, "s5p-mfc-l",
  932. match_child);
  933. if (!dev->mem_dev_l) {
  934. mfc_err("Mem child (L) device get failed\n");
  935. ret = -ENODEV;
  936. goto err_res;
  937. }
  938. dev->mem_dev_r = device_find_child(&dev->plat_dev->dev, "s5p-mfc-r",
  939. match_child);
  940. if (!dev->mem_dev_r) {
  941. mfc_err("Mem child (R) device get failed\n");
  942. ret = -ENODEV;
  943. goto err_res;
  944. }
  945. dev->alloc_ctx[0] = vb2_dma_contig_init_ctx(dev->mem_dev_l);
  946. if (IS_ERR_OR_NULL(dev->alloc_ctx[0])) {
  947. ret = PTR_ERR(dev->alloc_ctx[0]);
  948. goto err_res;
  949. }
  950. dev->alloc_ctx[1] = vb2_dma_contig_init_ctx(dev->mem_dev_r);
  951. if (IS_ERR_OR_NULL(dev->alloc_ctx[1])) {
  952. ret = PTR_ERR(dev->alloc_ctx[1]);
  953. goto err_mem_init_ctx_1;
  954. }
  955. mutex_init(&dev->mfc_mutex);
  956. ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
  957. if (ret)
  958. goto err_v4l2_dev_reg;
  959. init_waitqueue_head(&dev->queue);
  960. /* decoder */
  961. vfd = video_device_alloc();
  962. if (!vfd) {
  963. v4l2_err(&dev->v4l2_dev, "Failed to allocate video device\n");
  964. ret = -ENOMEM;
  965. goto err_dec_alloc;
  966. }
  967. vfd->fops = &s5p_mfc_fops,
  968. vfd->ioctl_ops = get_dec_v4l2_ioctl_ops();
  969. vfd->release = video_device_release,
  970. vfd->lock = &dev->mfc_mutex;
  971. vfd->v4l2_dev = &dev->v4l2_dev;
  972. vfd->vfl_dir = VFL_DIR_M2M;
  973. snprintf(vfd->name, sizeof(vfd->name), "%s", S5P_MFC_DEC_NAME);
  974. dev->vfd_dec = vfd;
  975. ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
  976. if (ret) {
  977. v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
  978. video_device_release(vfd);
  979. goto err_dec_reg;
  980. }
  981. v4l2_info(&dev->v4l2_dev,
  982. "decoder registered as /dev/video%d\n", vfd->num);
  983. video_set_drvdata(vfd, dev);
  984. /* encoder */
  985. vfd = video_device_alloc();
  986. if (!vfd) {
  987. v4l2_err(&dev->v4l2_dev, "Failed to allocate video device\n");
  988. ret = -ENOMEM;
  989. goto err_enc_alloc;
  990. }
  991. vfd->fops = &s5p_mfc_fops,
  992. vfd->ioctl_ops = get_enc_v4l2_ioctl_ops();
  993. vfd->release = video_device_release,
  994. vfd->lock = &dev->mfc_mutex;
  995. vfd->v4l2_dev = &dev->v4l2_dev;
  996. snprintf(vfd->name, sizeof(vfd->name), "%s", S5P_MFC_ENC_NAME);
  997. dev->vfd_enc = vfd;
  998. ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
  999. if (ret) {
  1000. v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
  1001. video_device_release(vfd);
  1002. goto err_enc_reg;
  1003. }
  1004. v4l2_info(&dev->v4l2_dev,
  1005. "encoder registered as /dev/video%d\n", vfd->num);
  1006. video_set_drvdata(vfd, dev);
  1007. platform_set_drvdata(pdev, dev);
  1008. dev->hw_lock = 0;
  1009. dev->watchdog_workqueue = create_singlethread_workqueue(S5P_MFC_NAME);
  1010. INIT_WORK(&dev->watchdog_work, s5p_mfc_watchdog_worker);
  1011. atomic_set(&dev->watchdog_cnt, 0);
  1012. init_timer(&dev->watchdog_timer);
  1013. dev->watchdog_timer.data = (unsigned long)dev;
  1014. dev->watchdog_timer.function = s5p_mfc_watchdog;
  1015. pr_debug("%s--\n", __func__);
  1016. return 0;
  1017. /* Deinit MFC if probe had failed */
  1018. err_enc_reg:
  1019. video_device_release(dev->vfd_enc);
  1020. err_enc_alloc:
  1021. video_unregister_device(dev->vfd_dec);
  1022. err_dec_reg:
  1023. video_device_release(dev->vfd_dec);
  1024. err_dec_alloc:
  1025. v4l2_device_unregister(&dev->v4l2_dev);
  1026. err_v4l2_dev_reg:
  1027. vb2_dma_contig_cleanup_ctx(dev->alloc_ctx[1]);
  1028. err_mem_init_ctx_1:
  1029. vb2_dma_contig_cleanup_ctx(dev->alloc_ctx[0]);
  1030. err_res:
  1031. s5p_mfc_final_pm(dev);
  1032. pr_debug("%s-- with error\n", __func__);
  1033. return ret;
  1034. }
  1035. /* Remove the driver */
  1036. static int __devexit s5p_mfc_remove(struct platform_device *pdev)
  1037. {
  1038. struct s5p_mfc_dev *dev = platform_get_drvdata(pdev);
  1039. v4l2_info(&dev->v4l2_dev, "Removing %s\n", pdev->name);
  1040. del_timer_sync(&dev->watchdog_timer);
  1041. flush_workqueue(dev->watchdog_workqueue);
  1042. destroy_workqueue(dev->watchdog_workqueue);
  1043. video_unregister_device(dev->vfd_enc);
  1044. video_unregister_device(dev->vfd_dec);
  1045. v4l2_device_unregister(&dev->v4l2_dev);
  1046. vb2_dma_contig_cleanup_ctx(dev->alloc_ctx[0]);
  1047. vb2_dma_contig_cleanup_ctx(dev->alloc_ctx[1]);
  1048. s5p_mfc_final_pm(dev);
  1049. return 0;
  1050. }
  1051. #ifdef CONFIG_PM_SLEEP
  1052. static int s5p_mfc_suspend(struct device *dev)
  1053. {
  1054. struct platform_device *pdev = to_platform_device(dev);
  1055. struct s5p_mfc_dev *m_dev = platform_get_drvdata(pdev);
  1056. int ret;
  1057. if (m_dev->num_inst == 0)
  1058. return 0;
  1059. return s5p_mfc_sleep(m_dev);
  1060. if (test_and_set_bit(0, &m_dev->enter_suspend) != 0) {
  1061. mfc_err("Error: going to suspend for a second time\n");
  1062. return -EIO;
  1063. }
  1064. /* Check if we're processing then wait if it necessary. */
  1065. while (test_and_set_bit(0, &m_dev->hw_lock) != 0) {
  1066. /* Try and lock the HW */
  1067. /* Wait on the interrupt waitqueue */
  1068. ret = wait_event_interruptible_timeout(m_dev->queue,
  1069. m_dev->int_cond || m_dev->ctx[m_dev->curr_ctx]->int_cond,
  1070. msecs_to_jiffies(MFC_INT_TIMEOUT));
  1071. if (ret == 0) {
  1072. mfc_err("Waiting for hardware to finish timed out\n");
  1073. return -EIO;
  1074. }
  1075. }
  1076. return 0;
  1077. }
  1078. static int s5p_mfc_resume(struct device *dev)
  1079. {
  1080. struct platform_device *pdev = to_platform_device(dev);
  1081. struct s5p_mfc_dev *m_dev = platform_get_drvdata(pdev);
  1082. if (m_dev->num_inst == 0)
  1083. return 0;
  1084. return s5p_mfc_wakeup(m_dev);
  1085. }
  1086. #endif
  1087. #ifdef CONFIG_PM_RUNTIME
  1088. static int s5p_mfc_runtime_suspend(struct device *dev)
  1089. {
  1090. struct platform_device *pdev = to_platform_device(dev);
  1091. struct s5p_mfc_dev *m_dev = platform_get_drvdata(pdev);
  1092. atomic_set(&m_dev->pm.power, 0);
  1093. return 0;
  1094. }
  1095. static int s5p_mfc_runtime_resume(struct device *dev)
  1096. {
  1097. struct platform_device *pdev = to_platform_device(dev);
  1098. struct s5p_mfc_dev *m_dev = platform_get_drvdata(pdev);
  1099. int pre_power;
  1100. if (!m_dev->alloc_ctx)
  1101. return 0;
  1102. pre_power = atomic_read(&m_dev->pm.power);
  1103. atomic_set(&m_dev->pm.power, 1);
  1104. return 0;
  1105. }
  1106. #endif
  1107. /* Power management */
  1108. static const struct dev_pm_ops s5p_mfc_pm_ops = {
  1109. SET_SYSTEM_SLEEP_PM_OPS(s5p_mfc_suspend, s5p_mfc_resume)
  1110. SET_RUNTIME_PM_OPS(s5p_mfc_runtime_suspend, s5p_mfc_runtime_resume,
  1111. NULL)
  1112. };
  1113. static struct platform_driver s5p_mfc_driver = {
  1114. .probe = s5p_mfc_probe,
  1115. .remove = __devexit_p(s5p_mfc_remove),
  1116. .driver = {
  1117. .name = S5P_MFC_NAME,
  1118. .owner = THIS_MODULE,
  1119. .pm = &s5p_mfc_pm_ops
  1120. },
  1121. };
  1122. module_platform_driver(s5p_mfc_driver);
  1123. MODULE_LICENSE("GPL");
  1124. MODULE_AUTHOR("Kamil Debski <k.debski@samsung.com>");
  1125. MODULE_DESCRIPTION("Samsung S5P Multi Format Codec V4L2 driver");