s5p_mfc.c 37 KB

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