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