s5p_mfc.c 35 KB

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