s5p_mfc.c 34 KB

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