s5p_mfc.c 35 KB

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