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/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. 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. static 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 == NULL)
  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 == NULL)
  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_height == 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 == NULL)
  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. static void s5p_mfc_handle_stream_complete(struct s5p_mfc_ctx *ctx,
  497. unsigned int reason, unsigned int err)
  498. {
  499. struct s5p_mfc_dev *dev = ctx->dev;
  500. struct s5p_mfc_buf *mb_entry;
  501. mfc_debug(2, "Stream completed");
  502. s5p_mfc_clear_int_flags(dev);
  503. ctx->int_type = reason;
  504. ctx->int_err = err;
  505. ctx->state = MFCINST_FINISHED;
  506. spin_lock(&dev->irqlock);
  507. if (!list_empty(&ctx->dst_queue)) {
  508. mb_entry = list_entry(ctx->dst_queue.next, struct s5p_mfc_buf,
  509. list);
  510. list_del(&mb_entry->list);
  511. ctx->dst_queue_cnt--;
  512. vb2_set_plane_payload(mb_entry->b, 0, 0);
  513. vb2_buffer_done(mb_entry->b, VB2_BUF_STATE_DONE);
  514. }
  515. spin_unlock(&dev->irqlock);
  516. clear_work_bit(ctx);
  517. if (test_and_clear_bit(0, &dev->hw_lock) == 0)
  518. WARN_ON(1);
  519. s5p_mfc_clock_off();
  520. wake_up(&ctx->queue);
  521. s5p_mfc_try_run(dev);
  522. }
  523. /* Interrupt processing */
  524. static irqreturn_t s5p_mfc_irq(int irq, void *priv)
  525. {
  526. struct s5p_mfc_dev *dev = priv;
  527. struct s5p_mfc_ctx *ctx;
  528. unsigned int reason;
  529. unsigned int err;
  530. mfc_debug_enter();
  531. /* Reset the timeout watchdog */
  532. atomic_set(&dev->watchdog_cnt, 0);
  533. ctx = dev->ctx[dev->curr_ctx];
  534. /* Get the reason of interrupt and the error code */
  535. reason = s5p_mfc_get_int_reason();
  536. err = s5p_mfc_get_int_err();
  537. mfc_debug(1, "Int reason: %d (err: %08x)\n", reason, err);
  538. switch (reason) {
  539. case S5P_FIMV_R2H_CMD_ERR_RET:
  540. /* An error has occured */
  541. if (ctx->state == MFCINST_RUNNING &&
  542. s5p_mfc_err_dec(err) >= S5P_FIMV_ERR_WARNINGS_START)
  543. s5p_mfc_handle_frame(ctx, reason, err);
  544. else
  545. s5p_mfc_handle_error(ctx, reason, err);
  546. clear_bit(0, &dev->enter_suspend);
  547. break;
  548. case S5P_FIMV_R2H_CMD_SLICE_DONE_RET:
  549. case S5P_FIMV_R2H_CMD_FRAME_DONE_RET:
  550. if (ctx->c_ops->post_frame_start) {
  551. if (ctx->c_ops->post_frame_start(ctx))
  552. mfc_err("post_frame_start() failed\n");
  553. s5p_mfc_clear_int_flags(dev);
  554. wake_up_ctx(ctx, reason, err);
  555. if (test_and_clear_bit(0, &dev->hw_lock) == 0)
  556. BUG();
  557. s5p_mfc_clock_off();
  558. s5p_mfc_try_run(dev);
  559. } else {
  560. s5p_mfc_handle_frame(ctx, reason, err);
  561. }
  562. break;
  563. case S5P_FIMV_R2H_CMD_SEQ_DONE_RET:
  564. s5p_mfc_handle_seq_done(ctx, reason, err);
  565. break;
  566. case S5P_FIMV_R2H_CMD_OPEN_INSTANCE_RET:
  567. ctx->inst_no = s5p_mfc_get_inst_no();
  568. ctx->state = MFCINST_GOT_INST;
  569. clear_work_bit(ctx);
  570. wake_up(&ctx->queue);
  571. goto irq_cleanup_hw;
  572. case S5P_FIMV_R2H_CMD_CLOSE_INSTANCE_RET:
  573. clear_work_bit(ctx);
  574. ctx->state = MFCINST_FREE;
  575. wake_up(&ctx->queue);
  576. goto irq_cleanup_hw;
  577. case S5P_FIMV_R2H_CMD_SYS_INIT_RET:
  578. case S5P_FIMV_R2H_CMD_FW_STATUS_RET:
  579. case S5P_FIMV_R2H_CMD_SLEEP_RET:
  580. case S5P_FIMV_R2H_CMD_WAKEUP_RET:
  581. if (ctx)
  582. clear_work_bit(ctx);
  583. s5p_mfc_clear_int_flags(dev);
  584. wake_up_dev(dev, reason, err);
  585. clear_bit(0, &dev->hw_lock);
  586. clear_bit(0, &dev->enter_suspend);
  587. break;
  588. case S5P_FIMV_R2H_CMD_INIT_BUFFERS_RET:
  589. s5p_mfc_handle_init_buffers(ctx, reason, err);
  590. break;
  591. case S5P_FIMV_R2H_CMD_ENC_COMPLETE_RET:
  592. s5p_mfc_handle_stream_complete(ctx, reason, err);
  593. break;
  594. default:
  595. mfc_debug(2, "Unknown int reason\n");
  596. s5p_mfc_clear_int_flags(dev);
  597. }
  598. mfc_debug_leave();
  599. return IRQ_HANDLED;
  600. irq_cleanup_hw:
  601. s5p_mfc_clear_int_flags(dev);
  602. ctx->int_type = reason;
  603. ctx->int_err = err;
  604. ctx->int_cond = 1;
  605. if (test_and_clear_bit(0, &dev->hw_lock) == 0)
  606. mfc_err("Failed to unlock hw\n");
  607. s5p_mfc_clock_off();
  608. s5p_mfc_try_run(dev);
  609. mfc_debug(2, "Exit via irq_cleanup_hw\n");
  610. return IRQ_HANDLED;
  611. }
  612. /* Open an MFC node */
  613. static int s5p_mfc_open(struct file *file)
  614. {
  615. struct s5p_mfc_dev *dev = video_drvdata(file);
  616. struct s5p_mfc_ctx *ctx = NULL;
  617. struct vb2_queue *q;
  618. unsigned long flags;
  619. int ret = 0;
  620. mfc_debug_enter();
  621. if (mutex_lock_interruptible(&dev->mfc_mutex))
  622. return -ERESTARTSYS;
  623. dev->num_inst++; /* It is guarded by mfc_mutex in vfd */
  624. /* Allocate memory for context */
  625. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  626. if (!ctx) {
  627. mfc_err("Not enough memory\n");
  628. ret = -ENOMEM;
  629. goto err_alloc;
  630. }
  631. v4l2_fh_init(&ctx->fh, video_devdata(file));
  632. file->private_data = &ctx->fh;
  633. v4l2_fh_add(&ctx->fh);
  634. ctx->dev = dev;
  635. INIT_LIST_HEAD(&ctx->src_queue);
  636. INIT_LIST_HEAD(&ctx->dst_queue);
  637. ctx->src_queue_cnt = 0;
  638. ctx->dst_queue_cnt = 0;
  639. /* Get context number */
  640. ctx->num = 0;
  641. while (dev->ctx[ctx->num]) {
  642. ctx->num++;
  643. if (ctx->num >= MFC_NUM_CONTEXTS) {
  644. mfc_err("Too many open contexts\n");
  645. ret = -EBUSY;
  646. goto err_no_ctx;
  647. }
  648. }
  649. /* Mark context as idle */
  650. spin_lock_irqsave(&dev->condlock, flags);
  651. clear_bit(ctx->num, &dev->ctx_work_bits);
  652. spin_unlock_irqrestore(&dev->condlock, flags);
  653. dev->ctx[ctx->num] = ctx;
  654. if (s5p_mfc_get_node_type(file) == MFCNODE_DECODER) {
  655. ctx->type = MFCINST_DECODER;
  656. ctx->c_ops = get_dec_codec_ops();
  657. /* Setup ctrl handler */
  658. ret = s5p_mfc_dec_ctrls_setup(ctx);
  659. if (ret) {
  660. mfc_err("Failed to setup mfc controls\n");
  661. goto err_ctrls_setup;
  662. }
  663. } else if (s5p_mfc_get_node_type(file) == MFCNODE_ENCODER) {
  664. ctx->type = MFCINST_ENCODER;
  665. ctx->c_ops = get_enc_codec_ops();
  666. /* only for encoder */
  667. INIT_LIST_HEAD(&ctx->ref_queue);
  668. ctx->ref_queue_cnt = 0;
  669. /* Setup ctrl handler */
  670. ret = s5p_mfc_enc_ctrls_setup(ctx);
  671. if (ret) {
  672. mfc_err("Failed to setup mfc controls\n");
  673. goto err_ctrls_setup;
  674. }
  675. } else {
  676. ret = -ENOENT;
  677. goto err_bad_node;
  678. }
  679. ctx->fh.ctrl_handler = &ctx->ctrl_handler;
  680. ctx->inst_no = -1;
  681. /* Load firmware if this is the first instance */
  682. if (dev->num_inst == 1) {
  683. dev->watchdog_timer.expires = jiffies +
  684. msecs_to_jiffies(MFC_WATCHDOG_INTERVAL);
  685. add_timer(&dev->watchdog_timer);
  686. ret = s5p_mfc_power_on();
  687. if (ret < 0) {
  688. mfc_err("power on failed\n");
  689. goto err_pwr_enable;
  690. }
  691. s5p_mfc_clock_on();
  692. ret = s5p_mfc_alloc_and_load_firmware(dev);
  693. if (ret)
  694. goto err_alloc_fw;
  695. /* Init the FW */
  696. ret = s5p_mfc_init_hw(dev);
  697. if (ret)
  698. goto err_init_hw;
  699. s5p_mfc_clock_off();
  700. }
  701. /* Init videobuf2 queue for CAPTURE */
  702. q = &ctx->vq_dst;
  703. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  704. q->drv_priv = &ctx->fh;
  705. if (s5p_mfc_get_node_type(file) == MFCNODE_DECODER) {
  706. q->io_modes = VB2_MMAP;
  707. q->ops = get_dec_queue_ops();
  708. } else if (s5p_mfc_get_node_type(file) == MFCNODE_ENCODER) {
  709. q->io_modes = VB2_MMAP | VB2_USERPTR;
  710. q->ops = get_enc_queue_ops();
  711. } else {
  712. ret = -ENOENT;
  713. goto err_queue_init;
  714. }
  715. q->mem_ops = (struct vb2_mem_ops *)&vb2_dma_contig_memops;
  716. ret = vb2_queue_init(q);
  717. if (ret) {
  718. mfc_err("Failed to initialize videobuf2 queue(capture)\n");
  719. goto err_queue_init;
  720. }
  721. /* Init videobuf2 queue for OUTPUT */
  722. q = &ctx->vq_src;
  723. q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
  724. q->io_modes = VB2_MMAP;
  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(output)\n");
  740. goto err_queue_init;
  741. }
  742. init_waitqueue_head(&ctx->queue);
  743. mutex_unlock(&dev->mfc_mutex);
  744. mfc_debug_leave();
  745. return ret;
  746. /* Deinit when failure occured */
  747. err_queue_init:
  748. err_init_hw:
  749. s5p_mfc_release_firmware(dev);
  750. err_alloc_fw:
  751. dev->ctx[ctx->num] = NULL;
  752. del_timer_sync(&dev->watchdog_timer);
  753. s5p_mfc_clock_off();
  754. err_pwr_enable:
  755. if (dev->num_inst == 1) {
  756. if (s5p_mfc_power_off() < 0)
  757. mfc_err("power off failed\n");
  758. s5p_mfc_release_firmware(dev);
  759. }
  760. err_ctrls_setup:
  761. s5p_mfc_dec_ctrls_delete(ctx);
  762. err_bad_node:
  763. err_no_ctx:
  764. v4l2_fh_del(&ctx->fh);
  765. v4l2_fh_exit(&ctx->fh);
  766. kfree(ctx);
  767. err_alloc:
  768. dev->num_inst--;
  769. mutex_unlock(&dev->mfc_mutex);
  770. mfc_debug_leave();
  771. return ret;
  772. }
  773. /* Release MFC context */
  774. static int s5p_mfc_release(struct file *file)
  775. {
  776. struct s5p_mfc_ctx *ctx = fh_to_ctx(file->private_data);
  777. struct s5p_mfc_dev *dev = ctx->dev;
  778. unsigned long flags;
  779. mfc_debug_enter();
  780. mutex_lock(&dev->mfc_mutex);
  781. s5p_mfc_clock_on();
  782. vb2_queue_release(&ctx->vq_src);
  783. vb2_queue_release(&ctx->vq_dst);
  784. /* Mark context as idle */
  785. spin_lock_irqsave(&dev->condlock, flags);
  786. clear_bit(ctx->num, &dev->ctx_work_bits);
  787. spin_unlock_irqrestore(&dev->condlock, flags);
  788. /* If instance was initialised then
  789. * return instance and free reosurces */
  790. if (ctx->inst_no != MFC_NO_INSTANCE_SET) {
  791. mfc_debug(2, "Has to free instance\n");
  792. ctx->state = MFCINST_RETURN_INST;
  793. spin_lock_irqsave(&dev->condlock, flags);
  794. set_bit(ctx->num, &dev->ctx_work_bits);
  795. spin_unlock_irqrestore(&dev->condlock, flags);
  796. s5p_mfc_clean_ctx_int_flags(ctx);
  797. s5p_mfc_try_run(dev);
  798. /* Wait until instance is returned or timeout occured */
  799. if (s5p_mfc_wait_for_done_ctx
  800. (ctx, S5P_FIMV_R2H_CMD_CLOSE_INSTANCE_RET, 0)) {
  801. s5p_mfc_clock_off();
  802. mfc_err("Err returning instance\n");
  803. }
  804. mfc_debug(2, "After free instance\n");
  805. /* Free resources */
  806. s5p_mfc_release_codec_buffers(ctx);
  807. s5p_mfc_release_instance_buffer(ctx);
  808. if (ctx->type == MFCINST_DECODER)
  809. s5p_mfc_release_dec_desc_buffer(ctx);
  810. ctx->inst_no = MFC_NO_INSTANCE_SET;
  811. }
  812. /* hardware locking scheme */
  813. if (dev->curr_ctx == ctx->num)
  814. clear_bit(0, &dev->hw_lock);
  815. dev->num_inst--;
  816. if (dev->num_inst == 0) {
  817. mfc_debug(2, "Last instance - release firmware\n");
  818. /* reset <-> F/W release */
  819. s5p_mfc_reset(dev);
  820. s5p_mfc_release_firmware(dev);
  821. del_timer_sync(&dev->watchdog_timer);
  822. if (s5p_mfc_power_off() < 0)
  823. mfc_err("Power off failed\n");
  824. }
  825. mfc_debug(2, "Shutting down clock\n");
  826. s5p_mfc_clock_off();
  827. dev->ctx[ctx->num] = NULL;
  828. s5p_mfc_dec_ctrls_delete(ctx);
  829. v4l2_fh_del(&ctx->fh);
  830. v4l2_fh_exit(&ctx->fh);
  831. kfree(ctx);
  832. mfc_debug_leave();
  833. mutex_unlock(&dev->mfc_mutex);
  834. return 0;
  835. }
  836. /* Poll */
  837. static unsigned int s5p_mfc_poll(struct file *file,
  838. struct poll_table_struct *wait)
  839. {
  840. struct s5p_mfc_ctx *ctx = fh_to_ctx(file->private_data);
  841. struct s5p_mfc_dev *dev = ctx->dev;
  842. struct vb2_queue *src_q, *dst_q;
  843. struct vb2_buffer *src_vb = NULL, *dst_vb = NULL;
  844. unsigned int rc = 0;
  845. unsigned long flags;
  846. mutex_lock(&dev->mfc_mutex);
  847. src_q = &ctx->vq_src;
  848. dst_q = &ctx->vq_dst;
  849. /*
  850. * There has to be at least one buffer queued on each queued_list, which
  851. * means either in driver already or waiting for driver to claim it
  852. * and start processing.
  853. */
  854. if ((!src_q->streaming || list_empty(&src_q->queued_list))
  855. && (!dst_q->streaming || list_empty(&dst_q->queued_list))) {
  856. rc = POLLERR;
  857. goto end;
  858. }
  859. mutex_unlock(&dev->mfc_mutex);
  860. poll_wait(file, &ctx->fh.wait, wait);
  861. poll_wait(file, &src_q->done_wq, wait);
  862. poll_wait(file, &dst_q->done_wq, wait);
  863. mutex_lock(&dev->mfc_mutex);
  864. if (v4l2_event_pending(&ctx->fh))
  865. rc |= POLLPRI;
  866. spin_lock_irqsave(&src_q->done_lock, flags);
  867. if (!list_empty(&src_q->done_list))
  868. src_vb = list_first_entry(&src_q->done_list, struct vb2_buffer,
  869. done_entry);
  870. if (src_vb && (src_vb->state == VB2_BUF_STATE_DONE
  871. || src_vb->state == VB2_BUF_STATE_ERROR))
  872. rc |= POLLOUT | POLLWRNORM;
  873. spin_unlock_irqrestore(&src_q->done_lock, flags);
  874. spin_lock_irqsave(&dst_q->done_lock, flags);
  875. if (!list_empty(&dst_q->done_list))
  876. dst_vb = list_first_entry(&dst_q->done_list, struct vb2_buffer,
  877. done_entry);
  878. if (dst_vb && (dst_vb->state == VB2_BUF_STATE_DONE
  879. || dst_vb->state == VB2_BUF_STATE_ERROR))
  880. rc |= POLLIN | POLLRDNORM;
  881. spin_unlock_irqrestore(&dst_q->done_lock, flags);
  882. end:
  883. mutex_unlock(&dev->mfc_mutex);
  884. return rc;
  885. }
  886. /* Mmap */
  887. static int s5p_mfc_mmap(struct file *file, struct vm_area_struct *vma)
  888. {
  889. struct s5p_mfc_ctx *ctx = fh_to_ctx(file->private_data);
  890. struct s5p_mfc_dev *dev = ctx->dev;
  891. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  892. int ret;
  893. if (mutex_lock_interruptible(&dev->mfc_mutex))
  894. return -ERESTARTSYS;
  895. if (offset < DST_QUEUE_OFF_BASE) {
  896. mfc_debug(2, "mmaping source\n");
  897. ret = vb2_mmap(&ctx->vq_src, vma);
  898. } else { /* capture */
  899. mfc_debug(2, "mmaping destination\n");
  900. vma->vm_pgoff -= (DST_QUEUE_OFF_BASE >> PAGE_SHIFT);
  901. ret = vb2_mmap(&ctx->vq_dst, vma);
  902. }
  903. mutex_unlock(&dev->mfc_mutex);
  904. return ret;
  905. }
  906. /* v4l2 ops */
  907. static const struct v4l2_file_operations s5p_mfc_fops = {
  908. .owner = THIS_MODULE,
  909. .open = s5p_mfc_open,
  910. .release = s5p_mfc_release,
  911. .poll = s5p_mfc_poll,
  912. .unlocked_ioctl = video_ioctl2,
  913. .mmap = s5p_mfc_mmap,
  914. };
  915. static int match_child(struct device *dev, void *data)
  916. {
  917. if (!dev_name(dev))
  918. return 0;
  919. return !strcmp(dev_name(dev), (char *)data);
  920. }
  921. /* MFC probe function */
  922. static int s5p_mfc_probe(struct platform_device *pdev)
  923. {
  924. struct s5p_mfc_dev *dev;
  925. struct video_device *vfd;
  926. struct resource *res;
  927. int ret;
  928. pr_debug("%s++\n", __func__);
  929. dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
  930. if (!dev) {
  931. dev_err(&pdev->dev, "Not enough memory for MFC device\n");
  932. return -ENOMEM;
  933. }
  934. spin_lock_init(&dev->irqlock);
  935. spin_lock_init(&dev->condlock);
  936. dev->plat_dev = pdev;
  937. if (!dev->plat_dev) {
  938. dev_err(&pdev->dev, "No platform data specified\n");
  939. return -ENODEV;
  940. }
  941. ret = s5p_mfc_init_pm(dev);
  942. if (ret < 0) {
  943. dev_err(&pdev->dev, "failed to get mfc clock source\n");
  944. return ret;
  945. }
  946. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  947. dev->regs_base = devm_request_and_ioremap(&pdev->dev, res);
  948. if (dev->regs_base == NULL) {
  949. dev_err(&pdev->dev, "Failed to obtain io memory\n");
  950. return -ENOENT;
  951. }
  952. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  953. if (res == NULL) {
  954. dev_err(&pdev->dev, "failed to get irq resource\n");
  955. ret = -ENOENT;
  956. goto err_res;
  957. }
  958. dev->irq = res->start;
  959. ret = devm_request_irq(&pdev->dev, dev->irq, s5p_mfc_irq,
  960. IRQF_DISABLED, pdev->name, dev);
  961. if (ret) {
  962. dev_err(&pdev->dev, "Failed to install irq (%d)\n", ret);
  963. goto err_res;
  964. }
  965. dev->mem_dev_l = device_find_child(&dev->plat_dev->dev, "s5p-mfc-l",
  966. match_child);
  967. if (!dev->mem_dev_l) {
  968. mfc_err("Mem child (L) device get failed\n");
  969. ret = -ENODEV;
  970. goto err_res;
  971. }
  972. dev->mem_dev_r = device_find_child(&dev->plat_dev->dev, "s5p-mfc-r",
  973. match_child);
  974. if (!dev->mem_dev_r) {
  975. mfc_err("Mem child (R) device get failed\n");
  976. ret = -ENODEV;
  977. goto err_res;
  978. }
  979. dev->alloc_ctx[0] = vb2_dma_contig_init_ctx(dev->mem_dev_l);
  980. if (IS_ERR_OR_NULL(dev->alloc_ctx[0])) {
  981. ret = PTR_ERR(dev->alloc_ctx[0]);
  982. goto err_res;
  983. }
  984. dev->alloc_ctx[1] = vb2_dma_contig_init_ctx(dev->mem_dev_r);
  985. if (IS_ERR_OR_NULL(dev->alloc_ctx[1])) {
  986. ret = PTR_ERR(dev->alloc_ctx[1]);
  987. goto err_mem_init_ctx_1;
  988. }
  989. mutex_init(&dev->mfc_mutex);
  990. ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
  991. if (ret)
  992. goto err_v4l2_dev_reg;
  993. init_waitqueue_head(&dev->queue);
  994. /* decoder */
  995. vfd = video_device_alloc();
  996. if (!vfd) {
  997. v4l2_err(&dev->v4l2_dev, "Failed to allocate video device\n");
  998. ret = -ENOMEM;
  999. goto err_dec_alloc;
  1000. }
  1001. vfd->fops = &s5p_mfc_fops,
  1002. vfd->ioctl_ops = get_dec_v4l2_ioctl_ops();
  1003. vfd->release = video_device_release,
  1004. vfd->lock = &dev->mfc_mutex;
  1005. vfd->v4l2_dev = &dev->v4l2_dev;
  1006. vfd->vfl_dir = VFL_DIR_M2M;
  1007. snprintf(vfd->name, sizeof(vfd->name), "%s", S5P_MFC_DEC_NAME);
  1008. dev->vfd_dec = vfd;
  1009. ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
  1010. if (ret) {
  1011. v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
  1012. video_device_release(vfd);
  1013. goto err_dec_reg;
  1014. }
  1015. v4l2_info(&dev->v4l2_dev,
  1016. "decoder registered as /dev/video%d\n", vfd->num);
  1017. video_set_drvdata(vfd, dev);
  1018. /* encoder */
  1019. vfd = video_device_alloc();
  1020. if (!vfd) {
  1021. v4l2_err(&dev->v4l2_dev, "Failed to allocate video device\n");
  1022. ret = -ENOMEM;
  1023. goto err_enc_alloc;
  1024. }
  1025. vfd->fops = &s5p_mfc_fops,
  1026. vfd->ioctl_ops = get_enc_v4l2_ioctl_ops();
  1027. vfd->release = video_device_release,
  1028. vfd->lock = &dev->mfc_mutex;
  1029. vfd->v4l2_dev = &dev->v4l2_dev;
  1030. snprintf(vfd->name, sizeof(vfd->name), "%s", S5P_MFC_ENC_NAME);
  1031. dev->vfd_enc = vfd;
  1032. ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
  1033. if (ret) {
  1034. v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
  1035. video_device_release(vfd);
  1036. goto err_enc_reg;
  1037. }
  1038. v4l2_info(&dev->v4l2_dev,
  1039. "encoder registered as /dev/video%d\n", vfd->num);
  1040. video_set_drvdata(vfd, dev);
  1041. platform_set_drvdata(pdev, dev);
  1042. dev->hw_lock = 0;
  1043. dev->watchdog_workqueue = create_singlethread_workqueue(S5P_MFC_NAME);
  1044. INIT_WORK(&dev->watchdog_work, s5p_mfc_watchdog_worker);
  1045. atomic_set(&dev->watchdog_cnt, 0);
  1046. init_timer(&dev->watchdog_timer);
  1047. dev->watchdog_timer.data = (unsigned long)dev;
  1048. dev->watchdog_timer.function = s5p_mfc_watchdog;
  1049. pr_debug("%s--\n", __func__);
  1050. return 0;
  1051. /* Deinit MFC if probe had failed */
  1052. err_enc_reg:
  1053. video_device_release(dev->vfd_enc);
  1054. err_enc_alloc:
  1055. video_unregister_device(dev->vfd_dec);
  1056. err_dec_reg:
  1057. video_device_release(dev->vfd_dec);
  1058. err_dec_alloc:
  1059. v4l2_device_unregister(&dev->v4l2_dev);
  1060. err_v4l2_dev_reg:
  1061. vb2_dma_contig_cleanup_ctx(dev->alloc_ctx[1]);
  1062. err_mem_init_ctx_1:
  1063. vb2_dma_contig_cleanup_ctx(dev->alloc_ctx[0]);
  1064. err_res:
  1065. s5p_mfc_final_pm(dev);
  1066. pr_debug("%s-- with error\n", __func__);
  1067. return ret;
  1068. }
  1069. /* Remove the driver */
  1070. static int __devexit s5p_mfc_remove(struct platform_device *pdev)
  1071. {
  1072. struct s5p_mfc_dev *dev = platform_get_drvdata(pdev);
  1073. v4l2_info(&dev->v4l2_dev, "Removing %s\n", pdev->name);
  1074. del_timer_sync(&dev->watchdog_timer);
  1075. flush_workqueue(dev->watchdog_workqueue);
  1076. destroy_workqueue(dev->watchdog_workqueue);
  1077. video_unregister_device(dev->vfd_enc);
  1078. video_unregister_device(dev->vfd_dec);
  1079. v4l2_device_unregister(&dev->v4l2_dev);
  1080. vb2_dma_contig_cleanup_ctx(dev->alloc_ctx[0]);
  1081. vb2_dma_contig_cleanup_ctx(dev->alloc_ctx[1]);
  1082. s5p_mfc_final_pm(dev);
  1083. return 0;
  1084. }
  1085. #ifdef CONFIG_PM_SLEEP
  1086. static int s5p_mfc_suspend(struct device *dev)
  1087. {
  1088. struct platform_device *pdev = to_platform_device(dev);
  1089. struct s5p_mfc_dev *m_dev = platform_get_drvdata(pdev);
  1090. int ret;
  1091. if (m_dev->num_inst == 0)
  1092. return 0;
  1093. return s5p_mfc_sleep(m_dev);
  1094. if (test_and_set_bit(0, &m_dev->enter_suspend) != 0) {
  1095. mfc_err("Error: going to suspend for a second time\n");
  1096. return -EIO;
  1097. }
  1098. /* Check if we're processing then wait if it necessary. */
  1099. while (test_and_set_bit(0, &m_dev->hw_lock) != 0) {
  1100. /* Try and lock the HW */
  1101. /* Wait on the interrupt waitqueue */
  1102. ret = wait_event_interruptible_timeout(m_dev->queue,
  1103. m_dev->int_cond || m_dev->ctx[m_dev->curr_ctx]->int_cond,
  1104. msecs_to_jiffies(MFC_INT_TIMEOUT));
  1105. if (ret == 0) {
  1106. mfc_err("Waiting for hardware to finish timed out\n");
  1107. return -EIO;
  1108. }
  1109. }
  1110. return 0;
  1111. }
  1112. static int s5p_mfc_resume(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. if (m_dev->num_inst == 0)
  1117. return 0;
  1118. return s5p_mfc_wakeup(m_dev);
  1119. }
  1120. #endif
  1121. #ifdef CONFIG_PM_RUNTIME
  1122. static int s5p_mfc_runtime_suspend(struct device *dev)
  1123. {
  1124. struct platform_device *pdev = to_platform_device(dev);
  1125. struct s5p_mfc_dev *m_dev = platform_get_drvdata(pdev);
  1126. atomic_set(&m_dev->pm.power, 0);
  1127. return 0;
  1128. }
  1129. static int s5p_mfc_runtime_resume(struct device *dev)
  1130. {
  1131. struct platform_device *pdev = to_platform_device(dev);
  1132. struct s5p_mfc_dev *m_dev = platform_get_drvdata(pdev);
  1133. int pre_power;
  1134. if (!m_dev->alloc_ctx)
  1135. return 0;
  1136. pre_power = atomic_read(&m_dev->pm.power);
  1137. atomic_set(&m_dev->pm.power, 1);
  1138. return 0;
  1139. }
  1140. #endif
  1141. /* Power management */
  1142. static const struct dev_pm_ops s5p_mfc_pm_ops = {
  1143. SET_SYSTEM_SLEEP_PM_OPS(s5p_mfc_suspend, s5p_mfc_resume)
  1144. SET_RUNTIME_PM_OPS(s5p_mfc_runtime_suspend, s5p_mfc_runtime_resume,
  1145. NULL)
  1146. };
  1147. static struct platform_driver s5p_mfc_driver = {
  1148. .probe = s5p_mfc_probe,
  1149. .remove = __devexit_p(s5p_mfc_remove),
  1150. .driver = {
  1151. .name = S5P_MFC_NAME,
  1152. .owner = THIS_MODULE,
  1153. .pm = &s5p_mfc_pm_ops
  1154. },
  1155. };
  1156. module_platform_driver(s5p_mfc_driver);
  1157. MODULE_LICENSE("GPL");
  1158. MODULE_AUTHOR("Kamil Debski <k.debski@samsung.com>");
  1159. MODULE_DESCRIPTION("Samsung S5P Multi Format Codec V4L2 driver");