s5p_mfc.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648
  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 <linux/of.h>
  24. #include <linux/of_device.h>
  25. #include <linux/of_reserved_mem.h>
  26. #include <media/videobuf2-v4l2.h>
  27. #include "s5p_mfc_common.h"
  28. #include "s5p_mfc_ctrl.h"
  29. #include "s5p_mfc_debug.h"
  30. #include "s5p_mfc_dec.h"
  31. #include "s5p_mfc_enc.h"
  32. #include "s5p_mfc_intr.h"
  33. #include "s5p_mfc_iommu.h"
  34. #include "s5p_mfc_opr.h"
  35. #include "s5p_mfc_cmd.h"
  36. #include "s5p_mfc_pm.h"
  37. #define S5P_MFC_DEC_NAME "s5p-mfc-dec"
  38. #define S5P_MFC_ENC_NAME "s5p-mfc-enc"
  39. int mfc_debug_level;
  40. module_param_named(debug, mfc_debug_level, int, S_IRUGO | S_IWUSR);
  41. MODULE_PARM_DESC(debug, "Debug level - higher value produces more verbose messages");
  42. static char *mfc_mem_size;
  43. module_param_named(mem, mfc_mem_size, charp, 0644);
  44. MODULE_PARM_DESC(mem, "Preallocated memory size for the firmware and context buffers");
  45. /* Helper functions for interrupt processing */
  46. /* Remove from hw execution round robin */
  47. void clear_work_bit(struct s5p_mfc_ctx *ctx)
  48. {
  49. struct s5p_mfc_dev *dev = ctx->dev;
  50. spin_lock(&dev->condlock);
  51. __clear_bit(ctx->num, &dev->ctx_work_bits);
  52. spin_unlock(&dev->condlock);
  53. }
  54. /* Add to hw execution round robin */
  55. void set_work_bit(struct s5p_mfc_ctx *ctx)
  56. {
  57. struct s5p_mfc_dev *dev = ctx->dev;
  58. spin_lock(&dev->condlock);
  59. __set_bit(ctx->num, &dev->ctx_work_bits);
  60. spin_unlock(&dev->condlock);
  61. }
  62. /* Remove from hw execution round robin */
  63. void clear_work_bit_irqsave(struct s5p_mfc_ctx *ctx)
  64. {
  65. struct s5p_mfc_dev *dev = ctx->dev;
  66. unsigned long flags;
  67. spin_lock_irqsave(&dev->condlock, flags);
  68. __clear_bit(ctx->num, &dev->ctx_work_bits);
  69. spin_unlock_irqrestore(&dev->condlock, flags);
  70. }
  71. /* Add to hw execution round robin */
  72. void set_work_bit_irqsave(struct s5p_mfc_ctx *ctx)
  73. {
  74. struct s5p_mfc_dev *dev = ctx->dev;
  75. unsigned long flags;
  76. spin_lock_irqsave(&dev->condlock, flags);
  77. __set_bit(ctx->num, &dev->ctx_work_bits);
  78. spin_unlock_irqrestore(&dev->condlock, flags);
  79. }
  80. int s5p_mfc_get_new_ctx(struct s5p_mfc_dev *dev)
  81. {
  82. unsigned long flags;
  83. int ctx;
  84. spin_lock_irqsave(&dev->condlock, flags);
  85. ctx = dev->curr_ctx;
  86. do {
  87. ctx = (ctx + 1) % MFC_NUM_CONTEXTS;
  88. if (ctx == dev->curr_ctx) {
  89. if (!test_bit(ctx, &dev->ctx_work_bits))
  90. ctx = -EAGAIN;
  91. break;
  92. }
  93. } while (!test_bit(ctx, &dev->ctx_work_bits));
  94. spin_unlock_irqrestore(&dev->condlock, flags);
  95. return ctx;
  96. }
  97. /* Wake up context wait_queue */
  98. static void wake_up_ctx(struct s5p_mfc_ctx *ctx, unsigned int reason,
  99. unsigned int err)
  100. {
  101. ctx->int_cond = 1;
  102. ctx->int_type = reason;
  103. ctx->int_err = err;
  104. wake_up(&ctx->queue);
  105. }
  106. /* Wake up device wait_queue */
  107. static void wake_up_dev(struct s5p_mfc_dev *dev, unsigned int reason,
  108. unsigned int err)
  109. {
  110. dev->int_cond = 1;
  111. dev->int_type = reason;
  112. dev->int_err = err;
  113. wake_up(&dev->queue);
  114. }
  115. void s5p_mfc_cleanup_queue(struct list_head *lh, struct vb2_queue *vq)
  116. {
  117. struct s5p_mfc_buf *b;
  118. int i;
  119. while (!list_empty(lh)) {
  120. b = list_entry(lh->next, struct s5p_mfc_buf, list);
  121. for (i = 0; i < b->b->vb2_buf.num_planes; i++)
  122. vb2_set_plane_payload(&b->b->vb2_buf, i, 0);
  123. vb2_buffer_done(&b->b->vb2_buf, VB2_BUF_STATE_ERROR);
  124. list_del(&b->list);
  125. }
  126. }
  127. static void s5p_mfc_watchdog(unsigned long arg)
  128. {
  129. struct s5p_mfc_dev *dev = (struct s5p_mfc_dev *)arg;
  130. if (test_bit(0, &dev->hw_lock))
  131. atomic_inc(&dev->watchdog_cnt);
  132. if (atomic_read(&dev->watchdog_cnt) >= MFC_WATCHDOG_CNT) {
  133. /* This means that hw is busy and no interrupts were
  134. * generated by hw for the Nth time of running this
  135. * watchdog timer. This usually means a serious hw
  136. * error. Now it is time to kill all instances and
  137. * reset the MFC. */
  138. mfc_err("Time out during waiting for HW\n");
  139. schedule_work(&dev->watchdog_work);
  140. }
  141. dev->watchdog_timer.expires = jiffies +
  142. msecs_to_jiffies(MFC_WATCHDOG_INTERVAL);
  143. add_timer(&dev->watchdog_timer);
  144. }
  145. static void s5p_mfc_watchdog_worker(struct work_struct *work)
  146. {
  147. struct s5p_mfc_dev *dev;
  148. struct s5p_mfc_ctx *ctx;
  149. unsigned long flags;
  150. int mutex_locked;
  151. int i, ret;
  152. dev = container_of(work, struct s5p_mfc_dev, watchdog_work);
  153. mfc_err("Driver timeout error handling\n");
  154. /* Lock the mutex that protects open and release.
  155. * This is necessary as they may load and unload firmware. */
  156. mutex_locked = mutex_trylock(&dev->mfc_mutex);
  157. if (!mutex_locked)
  158. mfc_err("Error: some instance may be closing/opening\n");
  159. spin_lock_irqsave(&dev->irqlock, flags);
  160. s5p_mfc_clock_off();
  161. for (i = 0; i < MFC_NUM_CONTEXTS; i++) {
  162. ctx = dev->ctx[i];
  163. if (!ctx)
  164. continue;
  165. ctx->state = MFCINST_ERROR;
  166. s5p_mfc_cleanup_queue(&ctx->dst_queue, &ctx->vq_dst);
  167. s5p_mfc_cleanup_queue(&ctx->src_queue, &ctx->vq_src);
  168. clear_work_bit(ctx);
  169. wake_up_ctx(ctx, S5P_MFC_R2H_CMD_ERR_RET, 0);
  170. }
  171. clear_bit(0, &dev->hw_lock);
  172. spin_unlock_irqrestore(&dev->irqlock, flags);
  173. /* De-init MFC */
  174. s5p_mfc_deinit_hw(dev);
  175. /* Double check if there is at least one instance running.
  176. * If no instance is in memory than no firmware should be present */
  177. if (dev->num_inst > 0) {
  178. ret = s5p_mfc_load_firmware(dev);
  179. if (ret) {
  180. mfc_err("Failed to reload FW\n");
  181. goto unlock;
  182. }
  183. s5p_mfc_clock_on();
  184. ret = s5p_mfc_init_hw(dev);
  185. s5p_mfc_clock_off();
  186. if (ret)
  187. mfc_err("Failed to reinit FW\n");
  188. }
  189. unlock:
  190. if (mutex_locked)
  191. mutex_unlock(&dev->mfc_mutex);
  192. }
  193. static void s5p_mfc_handle_frame_all_extracted(struct s5p_mfc_ctx *ctx)
  194. {
  195. struct s5p_mfc_buf *dst_buf;
  196. struct s5p_mfc_dev *dev = ctx->dev;
  197. ctx->state = MFCINST_FINISHED;
  198. ctx->sequence++;
  199. while (!list_empty(&ctx->dst_queue)) {
  200. dst_buf = list_entry(ctx->dst_queue.next,
  201. struct s5p_mfc_buf, list);
  202. mfc_debug(2, "Cleaning up buffer: %d\n",
  203. dst_buf->b->vb2_buf.index);
  204. vb2_set_plane_payload(&dst_buf->b->vb2_buf, 0, 0);
  205. vb2_set_plane_payload(&dst_buf->b->vb2_buf, 1, 0);
  206. list_del(&dst_buf->list);
  207. dst_buf->flags |= MFC_BUF_FLAG_EOS;
  208. ctx->dst_queue_cnt--;
  209. dst_buf->b->sequence = (ctx->sequence++);
  210. if (s5p_mfc_hw_call(dev->mfc_ops, get_pic_type_top, ctx) ==
  211. s5p_mfc_hw_call(dev->mfc_ops, get_pic_type_bot, ctx))
  212. dst_buf->b->field = V4L2_FIELD_NONE;
  213. else
  214. dst_buf->b->field = V4L2_FIELD_INTERLACED;
  215. dst_buf->b->flags |= V4L2_BUF_FLAG_LAST;
  216. ctx->dec_dst_flag &= ~(1 << dst_buf->b->vb2_buf.index);
  217. vb2_buffer_done(&dst_buf->b->vb2_buf, VB2_BUF_STATE_DONE);
  218. }
  219. }
  220. static void s5p_mfc_handle_frame_copy_time(struct s5p_mfc_ctx *ctx)
  221. {
  222. struct s5p_mfc_dev *dev = ctx->dev;
  223. struct s5p_mfc_buf *dst_buf, *src_buf;
  224. size_t dec_y_addr;
  225. unsigned int frame_type;
  226. /* Make sure we actually have a new frame before continuing. */
  227. frame_type = s5p_mfc_hw_call(dev->mfc_ops, get_dec_frame_type, dev);
  228. if (frame_type == S5P_FIMV_DECODE_FRAME_SKIPPED)
  229. return;
  230. dec_y_addr = s5p_mfc_hw_call(dev->mfc_ops, get_dec_y_adr, dev);
  231. /* Copy timestamp / timecode from decoded src to dst and set
  232. appropriate flags. */
  233. src_buf = list_entry(ctx->src_queue.next, struct s5p_mfc_buf, list);
  234. list_for_each_entry(dst_buf, &ctx->dst_queue, list) {
  235. if (vb2_dma_contig_plane_dma_addr(&dst_buf->b->vb2_buf, 0)
  236. == dec_y_addr) {
  237. dst_buf->b->timecode =
  238. src_buf->b->timecode;
  239. dst_buf->b->vb2_buf.timestamp =
  240. src_buf->b->vb2_buf.timestamp;
  241. dst_buf->b->flags &=
  242. ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
  243. dst_buf->b->flags |=
  244. src_buf->b->flags
  245. & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
  246. switch (frame_type) {
  247. case S5P_FIMV_DECODE_FRAME_I_FRAME:
  248. dst_buf->b->flags |=
  249. V4L2_BUF_FLAG_KEYFRAME;
  250. break;
  251. case S5P_FIMV_DECODE_FRAME_P_FRAME:
  252. dst_buf->b->flags |=
  253. V4L2_BUF_FLAG_PFRAME;
  254. break;
  255. case S5P_FIMV_DECODE_FRAME_B_FRAME:
  256. dst_buf->b->flags |=
  257. V4L2_BUF_FLAG_BFRAME;
  258. break;
  259. default:
  260. /* Don't know how to handle
  261. S5P_FIMV_DECODE_FRAME_OTHER_FRAME. */
  262. mfc_debug(2, "Unexpected frame type: %d\n",
  263. frame_type);
  264. }
  265. break;
  266. }
  267. }
  268. }
  269. static void s5p_mfc_handle_frame_new(struct s5p_mfc_ctx *ctx, unsigned int err)
  270. {
  271. struct s5p_mfc_dev *dev = ctx->dev;
  272. struct s5p_mfc_buf *dst_buf;
  273. size_t dspl_y_addr;
  274. unsigned int frame_type;
  275. dspl_y_addr = s5p_mfc_hw_call(dev->mfc_ops, get_dspl_y_adr, dev);
  276. if (IS_MFCV6_PLUS(dev))
  277. frame_type = s5p_mfc_hw_call(dev->mfc_ops,
  278. get_disp_frame_type, ctx);
  279. else
  280. frame_type = s5p_mfc_hw_call(dev->mfc_ops,
  281. get_dec_frame_type, dev);
  282. /* If frame is same as previous then skip and do not dequeue */
  283. if (frame_type == S5P_FIMV_DECODE_FRAME_SKIPPED) {
  284. if (!ctx->after_packed_pb)
  285. ctx->sequence++;
  286. ctx->after_packed_pb = 0;
  287. return;
  288. }
  289. ctx->sequence++;
  290. /* The MFC returns address of the buffer, now we have to
  291. * check which videobuf does it correspond to */
  292. list_for_each_entry(dst_buf, &ctx->dst_queue, list) {
  293. /* Check if this is the buffer we're looking for */
  294. if (vb2_dma_contig_plane_dma_addr(&dst_buf->b->vb2_buf, 0)
  295. == dspl_y_addr) {
  296. list_del(&dst_buf->list);
  297. ctx->dst_queue_cnt--;
  298. dst_buf->b->sequence = ctx->sequence;
  299. if (s5p_mfc_hw_call(dev->mfc_ops,
  300. get_pic_type_top, ctx) ==
  301. s5p_mfc_hw_call(dev->mfc_ops,
  302. get_pic_type_bot, ctx))
  303. dst_buf->b->field = V4L2_FIELD_NONE;
  304. else
  305. dst_buf->b->field =
  306. V4L2_FIELD_INTERLACED;
  307. vb2_set_plane_payload(&dst_buf->b->vb2_buf, 0,
  308. ctx->luma_size);
  309. vb2_set_plane_payload(&dst_buf->b->vb2_buf, 1,
  310. ctx->chroma_size);
  311. clear_bit(dst_buf->b->vb2_buf.index,
  312. &ctx->dec_dst_flag);
  313. vb2_buffer_done(&dst_buf->b->vb2_buf, err ?
  314. VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
  315. break;
  316. }
  317. }
  318. }
  319. /* Handle frame decoding interrupt */
  320. static void s5p_mfc_handle_frame(struct s5p_mfc_ctx *ctx,
  321. unsigned int reason, unsigned int err)
  322. {
  323. struct s5p_mfc_dev *dev = ctx->dev;
  324. unsigned int dst_frame_status;
  325. unsigned int dec_frame_status;
  326. struct s5p_mfc_buf *src_buf;
  327. unsigned int res_change;
  328. dst_frame_status = s5p_mfc_hw_call(dev->mfc_ops, get_dspl_status, dev)
  329. & S5P_FIMV_DEC_STATUS_DECODING_STATUS_MASK;
  330. dec_frame_status = s5p_mfc_hw_call(dev->mfc_ops, get_dec_status, dev)
  331. & S5P_FIMV_DEC_STATUS_DECODING_STATUS_MASK;
  332. res_change = (s5p_mfc_hw_call(dev->mfc_ops, get_dspl_status, dev)
  333. & S5P_FIMV_DEC_STATUS_RESOLUTION_MASK)
  334. >> S5P_FIMV_DEC_STATUS_RESOLUTION_SHIFT;
  335. mfc_debug(2, "Frame Status: %x\n", dst_frame_status);
  336. if (ctx->state == MFCINST_RES_CHANGE_INIT)
  337. ctx->state = MFCINST_RES_CHANGE_FLUSH;
  338. if (res_change == S5P_FIMV_RES_INCREASE ||
  339. res_change == S5P_FIMV_RES_DECREASE) {
  340. ctx->state = MFCINST_RES_CHANGE_INIT;
  341. s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
  342. wake_up_ctx(ctx, reason, err);
  343. WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
  344. s5p_mfc_clock_off();
  345. s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
  346. return;
  347. }
  348. if (ctx->dpb_flush_flag)
  349. ctx->dpb_flush_flag = 0;
  350. /* All frames remaining in the buffer have been extracted */
  351. if (dst_frame_status == S5P_FIMV_DEC_STATUS_DECODING_EMPTY) {
  352. if (ctx->state == MFCINST_RES_CHANGE_FLUSH) {
  353. static const struct v4l2_event ev_src_ch = {
  354. .type = V4L2_EVENT_SOURCE_CHANGE,
  355. .u.src_change.changes =
  356. V4L2_EVENT_SRC_CH_RESOLUTION,
  357. };
  358. s5p_mfc_handle_frame_all_extracted(ctx);
  359. ctx->state = MFCINST_RES_CHANGE_END;
  360. v4l2_event_queue_fh(&ctx->fh, &ev_src_ch);
  361. goto leave_handle_frame;
  362. } else {
  363. s5p_mfc_handle_frame_all_extracted(ctx);
  364. }
  365. }
  366. if (dec_frame_status == S5P_FIMV_DEC_STATUS_DECODING_DISPLAY)
  367. s5p_mfc_handle_frame_copy_time(ctx);
  368. /* A frame has been decoded and is in the buffer */
  369. if (dst_frame_status == S5P_FIMV_DEC_STATUS_DISPLAY_ONLY ||
  370. dst_frame_status == S5P_FIMV_DEC_STATUS_DECODING_DISPLAY) {
  371. s5p_mfc_handle_frame_new(ctx, err);
  372. } else {
  373. mfc_debug(2, "No frame decode\n");
  374. }
  375. /* Mark source buffer as complete */
  376. if (dst_frame_status != S5P_FIMV_DEC_STATUS_DISPLAY_ONLY
  377. && !list_empty(&ctx->src_queue)) {
  378. src_buf = list_entry(ctx->src_queue.next, struct s5p_mfc_buf,
  379. list);
  380. ctx->consumed_stream += s5p_mfc_hw_call(dev->mfc_ops,
  381. get_consumed_stream, dev);
  382. if (ctx->codec_mode != S5P_MFC_CODEC_H264_DEC &&
  383. ctx->codec_mode != S5P_MFC_CODEC_VP8_DEC &&
  384. ctx->consumed_stream + STUFF_BYTE <
  385. src_buf->b->vb2_buf.planes[0].bytesused) {
  386. /* Run MFC again on the same buffer */
  387. mfc_debug(2, "Running again the same buffer\n");
  388. ctx->after_packed_pb = 1;
  389. } else {
  390. mfc_debug(2, "MFC needs next buffer\n");
  391. ctx->consumed_stream = 0;
  392. if (src_buf->flags & MFC_BUF_FLAG_EOS)
  393. ctx->state = MFCINST_FINISHING;
  394. list_del(&src_buf->list);
  395. ctx->src_queue_cnt--;
  396. if (s5p_mfc_hw_call(dev->mfc_ops, err_dec, err) > 0)
  397. vb2_buffer_done(&src_buf->b->vb2_buf,
  398. VB2_BUF_STATE_ERROR);
  399. else
  400. vb2_buffer_done(&src_buf->b->vb2_buf,
  401. VB2_BUF_STATE_DONE);
  402. }
  403. }
  404. leave_handle_frame:
  405. if ((ctx->src_queue_cnt == 0 && ctx->state != MFCINST_FINISHING)
  406. || ctx->dst_queue_cnt < ctx->pb_count)
  407. clear_work_bit(ctx);
  408. s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
  409. wake_up_ctx(ctx, reason, err);
  410. WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
  411. s5p_mfc_clock_off();
  412. /* if suspending, wake up device and do not try_run again*/
  413. if (test_bit(0, &dev->enter_suspend))
  414. wake_up_dev(dev, reason, err);
  415. else
  416. s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
  417. }
  418. /* Error handling for interrupt */
  419. static void s5p_mfc_handle_error(struct s5p_mfc_dev *dev,
  420. struct s5p_mfc_ctx *ctx, unsigned int reason, unsigned int err)
  421. {
  422. mfc_err("Interrupt Error: %08x\n", err);
  423. if (ctx != NULL) {
  424. /* Error recovery is dependent on the state of context */
  425. switch (ctx->state) {
  426. case MFCINST_RES_CHANGE_INIT:
  427. case MFCINST_RES_CHANGE_FLUSH:
  428. case MFCINST_RES_CHANGE_END:
  429. case MFCINST_FINISHING:
  430. case MFCINST_FINISHED:
  431. case MFCINST_RUNNING:
  432. /* It is highly probable that an error occurred
  433. * while decoding a frame */
  434. clear_work_bit(ctx);
  435. ctx->state = MFCINST_ERROR;
  436. /* Mark all dst buffers as having an error */
  437. s5p_mfc_cleanup_queue(&ctx->dst_queue, &ctx->vq_dst);
  438. /* Mark all src buffers as having an error */
  439. s5p_mfc_cleanup_queue(&ctx->src_queue, &ctx->vq_src);
  440. wake_up_ctx(ctx, reason, err);
  441. break;
  442. default:
  443. clear_work_bit(ctx);
  444. ctx->state = MFCINST_ERROR;
  445. wake_up_ctx(ctx, reason, err);
  446. break;
  447. }
  448. }
  449. WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
  450. s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
  451. s5p_mfc_clock_off();
  452. wake_up_dev(dev, reason, err);
  453. }
  454. /* Header parsing interrupt handling */
  455. static void s5p_mfc_handle_seq_done(struct s5p_mfc_ctx *ctx,
  456. unsigned int reason, unsigned int err)
  457. {
  458. struct s5p_mfc_dev *dev;
  459. if (ctx == NULL)
  460. return;
  461. dev = ctx->dev;
  462. if (ctx->c_ops->post_seq_start) {
  463. if (ctx->c_ops->post_seq_start(ctx))
  464. mfc_err("post_seq_start() failed\n");
  465. } else {
  466. ctx->img_width = s5p_mfc_hw_call(dev->mfc_ops, get_img_width,
  467. dev);
  468. ctx->img_height = s5p_mfc_hw_call(dev->mfc_ops, get_img_height,
  469. dev);
  470. s5p_mfc_hw_call(dev->mfc_ops, dec_calc_dpb_size, ctx);
  471. ctx->pb_count = s5p_mfc_hw_call(dev->mfc_ops, get_dpb_count,
  472. dev);
  473. ctx->mv_count = s5p_mfc_hw_call(dev->mfc_ops, get_mv_count,
  474. dev);
  475. if (ctx->img_width == 0 || ctx->img_height == 0)
  476. ctx->state = MFCINST_ERROR;
  477. else
  478. ctx->state = MFCINST_HEAD_PARSED;
  479. if ((ctx->codec_mode == S5P_MFC_CODEC_H264_DEC ||
  480. ctx->codec_mode == S5P_MFC_CODEC_H264_MVC_DEC) &&
  481. !list_empty(&ctx->src_queue)) {
  482. struct s5p_mfc_buf *src_buf;
  483. src_buf = list_entry(ctx->src_queue.next,
  484. struct s5p_mfc_buf, list);
  485. if (s5p_mfc_hw_call(dev->mfc_ops, get_consumed_stream,
  486. dev) <
  487. src_buf->b->vb2_buf.planes[0].bytesused)
  488. ctx->head_processed = 0;
  489. else
  490. ctx->head_processed = 1;
  491. } else {
  492. ctx->head_processed = 1;
  493. }
  494. }
  495. s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
  496. clear_work_bit(ctx);
  497. WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
  498. s5p_mfc_clock_off();
  499. s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
  500. wake_up_ctx(ctx, reason, err);
  501. }
  502. /* Header parsing interrupt handling */
  503. static void s5p_mfc_handle_init_buffers(struct s5p_mfc_ctx *ctx,
  504. unsigned int reason, unsigned int err)
  505. {
  506. struct s5p_mfc_buf *src_buf;
  507. struct s5p_mfc_dev *dev;
  508. if (ctx == NULL)
  509. return;
  510. dev = ctx->dev;
  511. s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
  512. ctx->int_type = reason;
  513. ctx->int_err = err;
  514. ctx->int_cond = 1;
  515. clear_work_bit(ctx);
  516. if (err == 0) {
  517. ctx->state = MFCINST_RUNNING;
  518. if (!ctx->dpb_flush_flag && ctx->head_processed) {
  519. if (!list_empty(&ctx->src_queue)) {
  520. src_buf = list_entry(ctx->src_queue.next,
  521. struct s5p_mfc_buf, list);
  522. list_del(&src_buf->list);
  523. ctx->src_queue_cnt--;
  524. vb2_buffer_done(&src_buf->b->vb2_buf,
  525. VB2_BUF_STATE_DONE);
  526. }
  527. } else {
  528. ctx->dpb_flush_flag = 0;
  529. }
  530. WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
  531. s5p_mfc_clock_off();
  532. wake_up(&ctx->queue);
  533. s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
  534. } else {
  535. WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
  536. s5p_mfc_clock_off();
  537. wake_up(&ctx->queue);
  538. }
  539. }
  540. static void s5p_mfc_handle_stream_complete(struct s5p_mfc_ctx *ctx)
  541. {
  542. struct s5p_mfc_dev *dev = ctx->dev;
  543. struct s5p_mfc_buf *mb_entry;
  544. mfc_debug(2, "Stream completed\n");
  545. ctx->state = MFCINST_FINISHED;
  546. if (!list_empty(&ctx->dst_queue)) {
  547. mb_entry = list_entry(ctx->dst_queue.next, struct s5p_mfc_buf,
  548. list);
  549. list_del(&mb_entry->list);
  550. ctx->dst_queue_cnt--;
  551. vb2_set_plane_payload(&mb_entry->b->vb2_buf, 0, 0);
  552. vb2_buffer_done(&mb_entry->b->vb2_buf, VB2_BUF_STATE_DONE);
  553. }
  554. clear_work_bit(ctx);
  555. WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
  556. s5p_mfc_clock_off();
  557. wake_up(&ctx->queue);
  558. s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
  559. }
  560. /* Interrupt processing */
  561. static irqreturn_t s5p_mfc_irq(int irq, void *priv)
  562. {
  563. struct s5p_mfc_dev *dev = priv;
  564. struct s5p_mfc_ctx *ctx;
  565. unsigned int reason;
  566. unsigned int err;
  567. mfc_debug_enter();
  568. /* Reset the timeout watchdog */
  569. atomic_set(&dev->watchdog_cnt, 0);
  570. spin_lock(&dev->irqlock);
  571. ctx = dev->ctx[dev->curr_ctx];
  572. /* Get the reason of interrupt and the error code */
  573. reason = s5p_mfc_hw_call(dev->mfc_ops, get_int_reason, dev);
  574. err = s5p_mfc_hw_call(dev->mfc_ops, get_int_err, dev);
  575. mfc_debug(1, "Int reason: %d (err: %08x)\n", reason, err);
  576. switch (reason) {
  577. case S5P_MFC_R2H_CMD_ERR_RET:
  578. /* An error has occurred */
  579. if (ctx->state == MFCINST_RUNNING &&
  580. (s5p_mfc_hw_call(dev->mfc_ops, err_dec, err) >=
  581. dev->warn_start ||
  582. err == S5P_FIMV_ERR_NO_VALID_SEQ_HDR ||
  583. err == S5P_FIMV_ERR_INCOMPLETE_FRAME ||
  584. err == S5P_FIMV_ERR_TIMEOUT))
  585. s5p_mfc_handle_frame(ctx, reason, err);
  586. else
  587. s5p_mfc_handle_error(dev, ctx, reason, err);
  588. clear_bit(0, &dev->enter_suspend);
  589. break;
  590. case S5P_MFC_R2H_CMD_SLICE_DONE_RET:
  591. case S5P_MFC_R2H_CMD_FIELD_DONE_RET:
  592. case S5P_MFC_R2H_CMD_FRAME_DONE_RET:
  593. if (ctx->c_ops->post_frame_start) {
  594. if (ctx->c_ops->post_frame_start(ctx))
  595. mfc_err("post_frame_start() failed\n");
  596. if (ctx->state == MFCINST_FINISHING &&
  597. list_empty(&ctx->ref_queue)) {
  598. s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
  599. s5p_mfc_handle_stream_complete(ctx);
  600. break;
  601. }
  602. s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
  603. WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
  604. s5p_mfc_clock_off();
  605. wake_up_ctx(ctx, reason, err);
  606. s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
  607. } else {
  608. s5p_mfc_handle_frame(ctx, reason, err);
  609. }
  610. break;
  611. case S5P_MFC_R2H_CMD_SEQ_DONE_RET:
  612. s5p_mfc_handle_seq_done(ctx, reason, err);
  613. break;
  614. case S5P_MFC_R2H_CMD_OPEN_INSTANCE_RET:
  615. ctx->inst_no = s5p_mfc_hw_call(dev->mfc_ops, get_inst_no, dev);
  616. ctx->state = MFCINST_GOT_INST;
  617. goto irq_cleanup_hw;
  618. case S5P_MFC_R2H_CMD_CLOSE_INSTANCE_RET:
  619. ctx->inst_no = MFC_NO_INSTANCE_SET;
  620. ctx->state = MFCINST_FREE;
  621. goto irq_cleanup_hw;
  622. case S5P_MFC_R2H_CMD_SYS_INIT_RET:
  623. case S5P_MFC_R2H_CMD_FW_STATUS_RET:
  624. case S5P_MFC_R2H_CMD_SLEEP_RET:
  625. case S5P_MFC_R2H_CMD_WAKEUP_RET:
  626. if (ctx)
  627. clear_work_bit(ctx);
  628. s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
  629. clear_bit(0, &dev->hw_lock);
  630. clear_bit(0, &dev->enter_suspend);
  631. wake_up_dev(dev, reason, err);
  632. break;
  633. case S5P_MFC_R2H_CMD_INIT_BUFFERS_RET:
  634. s5p_mfc_handle_init_buffers(ctx, reason, err);
  635. break;
  636. case S5P_MFC_R2H_CMD_COMPLETE_SEQ_RET:
  637. s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
  638. ctx->int_type = reason;
  639. ctx->int_err = err;
  640. s5p_mfc_handle_stream_complete(ctx);
  641. break;
  642. case S5P_MFC_R2H_CMD_DPB_FLUSH_RET:
  643. ctx->state = MFCINST_RUNNING;
  644. goto irq_cleanup_hw;
  645. default:
  646. mfc_debug(2, "Unknown int reason\n");
  647. s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
  648. }
  649. spin_unlock(&dev->irqlock);
  650. mfc_debug_leave();
  651. return IRQ_HANDLED;
  652. irq_cleanup_hw:
  653. s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
  654. ctx->int_type = reason;
  655. ctx->int_err = err;
  656. ctx->int_cond = 1;
  657. if (test_and_clear_bit(0, &dev->hw_lock) == 0)
  658. mfc_err("Failed to unlock hw\n");
  659. s5p_mfc_clock_off();
  660. clear_work_bit(ctx);
  661. wake_up(&ctx->queue);
  662. s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
  663. spin_unlock(&dev->irqlock);
  664. mfc_debug(2, "Exit via irq_cleanup_hw\n");
  665. return IRQ_HANDLED;
  666. }
  667. /* Open an MFC node */
  668. static int s5p_mfc_open(struct file *file)
  669. {
  670. struct video_device *vdev = video_devdata(file);
  671. struct s5p_mfc_dev *dev = video_drvdata(file);
  672. struct s5p_mfc_ctx *ctx = NULL;
  673. struct vb2_queue *q;
  674. int ret = 0;
  675. mfc_debug_enter();
  676. if (mutex_lock_interruptible(&dev->mfc_mutex))
  677. return -ERESTARTSYS;
  678. dev->num_inst++; /* It is guarded by mfc_mutex in vfd */
  679. /* Allocate memory for context */
  680. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  681. if (!ctx) {
  682. ret = -ENOMEM;
  683. goto err_alloc;
  684. }
  685. init_waitqueue_head(&ctx->queue);
  686. v4l2_fh_init(&ctx->fh, vdev);
  687. file->private_data = &ctx->fh;
  688. v4l2_fh_add(&ctx->fh);
  689. ctx->dev = dev;
  690. INIT_LIST_HEAD(&ctx->src_queue);
  691. INIT_LIST_HEAD(&ctx->dst_queue);
  692. ctx->src_queue_cnt = 0;
  693. ctx->dst_queue_cnt = 0;
  694. /* Get context number */
  695. ctx->num = 0;
  696. while (dev->ctx[ctx->num]) {
  697. ctx->num++;
  698. if (ctx->num >= MFC_NUM_CONTEXTS) {
  699. mfc_debug(2, "Too many open contexts\n");
  700. ret = -EBUSY;
  701. goto err_no_ctx;
  702. }
  703. }
  704. /* Mark context as idle */
  705. clear_work_bit_irqsave(ctx);
  706. dev->ctx[ctx->num] = ctx;
  707. if (vdev == dev->vfd_dec) {
  708. ctx->type = MFCINST_DECODER;
  709. ctx->c_ops = get_dec_codec_ops();
  710. s5p_mfc_dec_init(ctx);
  711. /* Setup ctrl handler */
  712. ret = s5p_mfc_dec_ctrls_setup(ctx);
  713. if (ret) {
  714. mfc_err("Failed to setup mfc controls\n");
  715. goto err_ctrls_setup;
  716. }
  717. } else if (vdev == dev->vfd_enc) {
  718. ctx->type = MFCINST_ENCODER;
  719. ctx->c_ops = get_enc_codec_ops();
  720. /* only for encoder */
  721. INIT_LIST_HEAD(&ctx->ref_queue);
  722. ctx->ref_queue_cnt = 0;
  723. s5p_mfc_enc_init(ctx);
  724. /* Setup ctrl handler */
  725. ret = s5p_mfc_enc_ctrls_setup(ctx);
  726. if (ret) {
  727. mfc_err("Failed to setup mfc controls\n");
  728. goto err_ctrls_setup;
  729. }
  730. } else {
  731. ret = -ENOENT;
  732. goto err_bad_node;
  733. }
  734. ctx->fh.ctrl_handler = &ctx->ctrl_handler;
  735. ctx->inst_no = MFC_NO_INSTANCE_SET;
  736. /* Load firmware if this is the first instance */
  737. if (dev->num_inst == 1) {
  738. dev->watchdog_timer.expires = jiffies +
  739. msecs_to_jiffies(MFC_WATCHDOG_INTERVAL);
  740. add_timer(&dev->watchdog_timer);
  741. ret = s5p_mfc_power_on();
  742. if (ret < 0) {
  743. mfc_err("power on failed\n");
  744. goto err_pwr_enable;
  745. }
  746. s5p_mfc_clock_on();
  747. ret = s5p_mfc_load_firmware(dev);
  748. if (ret) {
  749. s5p_mfc_clock_off();
  750. goto err_load_fw;
  751. }
  752. /* Init the FW */
  753. ret = s5p_mfc_init_hw(dev);
  754. s5p_mfc_clock_off();
  755. if (ret)
  756. goto err_init_hw;
  757. }
  758. /* Init videobuf2 queue for CAPTURE */
  759. q = &ctx->vq_dst;
  760. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  761. q->drv_priv = &ctx->fh;
  762. q->lock = &dev->mfc_mutex;
  763. if (vdev == dev->vfd_dec) {
  764. q->io_modes = VB2_MMAP;
  765. q->ops = get_dec_queue_ops();
  766. } else if (vdev == dev->vfd_enc) {
  767. q->io_modes = VB2_MMAP | VB2_USERPTR;
  768. q->ops = get_enc_queue_ops();
  769. } else {
  770. ret = -ENOENT;
  771. goto err_queue_init;
  772. }
  773. /*
  774. * We'll do mostly sequential access, so sacrifice TLB efficiency for
  775. * faster allocation.
  776. */
  777. q->dma_attrs = DMA_ATTR_ALLOC_SINGLE_PAGES;
  778. q->mem_ops = &vb2_dma_contig_memops;
  779. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  780. ret = vb2_queue_init(q);
  781. if (ret) {
  782. mfc_err("Failed to initialize videobuf2 queue(capture)\n");
  783. goto err_queue_init;
  784. }
  785. /* Init videobuf2 queue for OUTPUT */
  786. q = &ctx->vq_src;
  787. q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
  788. q->drv_priv = &ctx->fh;
  789. q->lock = &dev->mfc_mutex;
  790. if (vdev == dev->vfd_dec) {
  791. q->io_modes = VB2_MMAP;
  792. q->ops = get_dec_queue_ops();
  793. } else if (vdev == dev->vfd_enc) {
  794. q->io_modes = VB2_MMAP | VB2_USERPTR;
  795. q->ops = get_enc_queue_ops();
  796. } else {
  797. ret = -ENOENT;
  798. goto err_queue_init;
  799. }
  800. /* One way to indicate end-of-stream for MFC is to set the
  801. * bytesused == 0. However by default videobuf2 handles bytesused
  802. * equal to 0 as a special case and changes its value to the size
  803. * of the buffer. Set the allow_zero_bytesused flag so that videobuf2
  804. * will keep the value of bytesused intact.
  805. */
  806. q->allow_zero_bytesused = 1;
  807. /*
  808. * We'll do mostly sequential access, so sacrifice TLB efficiency for
  809. * faster allocation.
  810. */
  811. q->dma_attrs = DMA_ATTR_ALLOC_SINGLE_PAGES;
  812. q->mem_ops = &vb2_dma_contig_memops;
  813. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  814. ret = vb2_queue_init(q);
  815. if (ret) {
  816. mfc_err("Failed to initialize videobuf2 queue(output)\n");
  817. goto err_queue_init;
  818. }
  819. mutex_unlock(&dev->mfc_mutex);
  820. mfc_debug_leave();
  821. return ret;
  822. /* Deinit when failure occurred */
  823. err_queue_init:
  824. if (dev->num_inst == 1)
  825. s5p_mfc_deinit_hw(dev);
  826. err_init_hw:
  827. err_load_fw:
  828. err_pwr_enable:
  829. if (dev->num_inst == 1) {
  830. if (s5p_mfc_power_off() < 0)
  831. mfc_err("power off failed\n");
  832. del_timer_sync(&dev->watchdog_timer);
  833. }
  834. err_ctrls_setup:
  835. s5p_mfc_dec_ctrls_delete(ctx);
  836. err_bad_node:
  837. dev->ctx[ctx->num] = NULL;
  838. err_no_ctx:
  839. v4l2_fh_del(&ctx->fh);
  840. v4l2_fh_exit(&ctx->fh);
  841. kfree(ctx);
  842. err_alloc:
  843. dev->num_inst--;
  844. mutex_unlock(&dev->mfc_mutex);
  845. mfc_debug_leave();
  846. return ret;
  847. }
  848. /* Release MFC context */
  849. static int s5p_mfc_release(struct file *file)
  850. {
  851. struct s5p_mfc_ctx *ctx = fh_to_ctx(file->private_data);
  852. struct s5p_mfc_dev *dev = ctx->dev;
  853. /* if dev is null, do cleanup that doesn't need dev */
  854. mfc_debug_enter();
  855. if (dev)
  856. mutex_lock(&dev->mfc_mutex);
  857. vb2_queue_release(&ctx->vq_src);
  858. vb2_queue_release(&ctx->vq_dst);
  859. if (dev) {
  860. s5p_mfc_clock_on();
  861. /* Mark context as idle */
  862. clear_work_bit_irqsave(ctx);
  863. /*
  864. * If instance was initialised and not yet freed,
  865. * return instance and free resources
  866. */
  867. if (ctx->state != MFCINST_FREE && ctx->state != MFCINST_INIT) {
  868. mfc_debug(2, "Has to free instance\n");
  869. s5p_mfc_close_mfc_inst(dev, ctx);
  870. }
  871. /* hardware locking scheme */
  872. if (dev->curr_ctx == ctx->num)
  873. clear_bit(0, &dev->hw_lock);
  874. dev->num_inst--;
  875. if (dev->num_inst == 0) {
  876. mfc_debug(2, "Last instance\n");
  877. s5p_mfc_deinit_hw(dev);
  878. del_timer_sync(&dev->watchdog_timer);
  879. s5p_mfc_clock_off();
  880. if (s5p_mfc_power_off() < 0)
  881. mfc_err("Power off failed\n");
  882. } else {
  883. mfc_debug(2, "Shutting down clock\n");
  884. s5p_mfc_clock_off();
  885. }
  886. }
  887. if (dev)
  888. dev->ctx[ctx->num] = NULL;
  889. s5p_mfc_dec_ctrls_delete(ctx);
  890. v4l2_fh_del(&ctx->fh);
  891. /* vdev is gone if dev is null */
  892. if (dev)
  893. v4l2_fh_exit(&ctx->fh);
  894. kfree(ctx);
  895. mfc_debug_leave();
  896. if (dev)
  897. mutex_unlock(&dev->mfc_mutex);
  898. return 0;
  899. }
  900. /* Poll */
  901. static unsigned int s5p_mfc_poll(struct file *file,
  902. struct poll_table_struct *wait)
  903. {
  904. struct s5p_mfc_ctx *ctx = fh_to_ctx(file->private_data);
  905. struct s5p_mfc_dev *dev = ctx->dev;
  906. struct vb2_queue *src_q, *dst_q;
  907. struct vb2_buffer *src_vb = NULL, *dst_vb = NULL;
  908. unsigned int rc = 0;
  909. unsigned long flags;
  910. mutex_lock(&dev->mfc_mutex);
  911. src_q = &ctx->vq_src;
  912. dst_q = &ctx->vq_dst;
  913. /*
  914. * There has to be at least one buffer queued on each queued_list, which
  915. * means either in driver already or waiting for driver to claim it
  916. * and start processing.
  917. */
  918. if ((!src_q->streaming || list_empty(&src_q->queued_list))
  919. && (!dst_q->streaming || list_empty(&dst_q->queued_list))) {
  920. rc = POLLERR;
  921. goto end;
  922. }
  923. mutex_unlock(&dev->mfc_mutex);
  924. poll_wait(file, &ctx->fh.wait, wait);
  925. poll_wait(file, &src_q->done_wq, wait);
  926. poll_wait(file, &dst_q->done_wq, wait);
  927. mutex_lock(&dev->mfc_mutex);
  928. if (v4l2_event_pending(&ctx->fh))
  929. rc |= POLLPRI;
  930. spin_lock_irqsave(&src_q->done_lock, flags);
  931. if (!list_empty(&src_q->done_list))
  932. src_vb = list_first_entry(&src_q->done_list, struct vb2_buffer,
  933. done_entry);
  934. if (src_vb && (src_vb->state == VB2_BUF_STATE_DONE
  935. || src_vb->state == VB2_BUF_STATE_ERROR))
  936. rc |= POLLOUT | POLLWRNORM;
  937. spin_unlock_irqrestore(&src_q->done_lock, flags);
  938. spin_lock_irqsave(&dst_q->done_lock, flags);
  939. if (!list_empty(&dst_q->done_list))
  940. dst_vb = list_first_entry(&dst_q->done_list, struct vb2_buffer,
  941. done_entry);
  942. if (dst_vb && (dst_vb->state == VB2_BUF_STATE_DONE
  943. || dst_vb->state == VB2_BUF_STATE_ERROR))
  944. rc |= POLLIN | POLLRDNORM;
  945. spin_unlock_irqrestore(&dst_q->done_lock, flags);
  946. end:
  947. mutex_unlock(&dev->mfc_mutex);
  948. return rc;
  949. }
  950. /* Mmap */
  951. static int s5p_mfc_mmap(struct file *file, struct vm_area_struct *vma)
  952. {
  953. struct s5p_mfc_ctx *ctx = fh_to_ctx(file->private_data);
  954. struct s5p_mfc_dev *dev = ctx->dev;
  955. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  956. int ret;
  957. if (mutex_lock_interruptible(&dev->mfc_mutex))
  958. return -ERESTARTSYS;
  959. if (offset < DST_QUEUE_OFF_BASE) {
  960. mfc_debug(2, "mmaping source\n");
  961. ret = vb2_mmap(&ctx->vq_src, vma);
  962. } else { /* capture */
  963. mfc_debug(2, "mmaping destination\n");
  964. vma->vm_pgoff -= (DST_QUEUE_OFF_BASE >> PAGE_SHIFT);
  965. ret = vb2_mmap(&ctx->vq_dst, vma);
  966. }
  967. mutex_unlock(&dev->mfc_mutex);
  968. return ret;
  969. }
  970. /* v4l2 ops */
  971. static const struct v4l2_file_operations s5p_mfc_fops = {
  972. .owner = THIS_MODULE,
  973. .open = s5p_mfc_open,
  974. .release = s5p_mfc_release,
  975. .poll = s5p_mfc_poll,
  976. .unlocked_ioctl = video_ioctl2,
  977. .mmap = s5p_mfc_mmap,
  978. };
  979. /* DMA memory related helper functions */
  980. static void s5p_mfc_memdev_release(struct device *dev)
  981. {
  982. of_reserved_mem_device_release(dev);
  983. }
  984. static struct device *s5p_mfc_alloc_memdev(struct device *dev,
  985. const char *name, unsigned int idx)
  986. {
  987. struct device *child;
  988. int ret;
  989. child = devm_kzalloc(dev, sizeof(struct device), GFP_KERNEL);
  990. if (!child)
  991. return NULL;
  992. device_initialize(child);
  993. dev_set_name(child, "%s:%s", dev_name(dev), name);
  994. child->parent = dev;
  995. child->bus = dev->bus;
  996. child->coherent_dma_mask = dev->coherent_dma_mask;
  997. child->dma_mask = dev->dma_mask;
  998. child->release = s5p_mfc_memdev_release;
  999. if (device_add(child) == 0) {
  1000. ret = of_reserved_mem_device_init_by_idx(child, dev->of_node,
  1001. idx);
  1002. if (ret == 0)
  1003. return child;
  1004. device_del(child);
  1005. }
  1006. put_device(child);
  1007. return NULL;
  1008. }
  1009. static int s5p_mfc_configure_2port_memory(struct s5p_mfc_dev *mfc_dev)
  1010. {
  1011. struct device *dev = &mfc_dev->plat_dev->dev;
  1012. void *bank2_virt;
  1013. dma_addr_t bank2_dma_addr;
  1014. unsigned long align_size = 1 << MFC_BASE_ALIGN_ORDER;
  1015. int ret;
  1016. /*
  1017. * Create and initialize virtual devices for accessing
  1018. * reserved memory regions.
  1019. */
  1020. mfc_dev->mem_dev[BANK_L_CTX] = s5p_mfc_alloc_memdev(dev, "left",
  1021. BANK_L_CTX);
  1022. if (!mfc_dev->mem_dev[BANK_L_CTX])
  1023. return -ENODEV;
  1024. mfc_dev->mem_dev[BANK_R_CTX] = s5p_mfc_alloc_memdev(dev, "right",
  1025. BANK_R_CTX);
  1026. if (!mfc_dev->mem_dev[BANK_R_CTX]) {
  1027. device_unregister(mfc_dev->mem_dev[BANK_L_CTX]);
  1028. return -ENODEV;
  1029. }
  1030. /* Allocate memory for firmware and initialize both banks addresses */
  1031. ret = s5p_mfc_alloc_firmware(mfc_dev);
  1032. if (ret) {
  1033. device_unregister(mfc_dev->mem_dev[BANK_R_CTX]);
  1034. device_unregister(mfc_dev->mem_dev[BANK_L_CTX]);
  1035. return ret;
  1036. }
  1037. mfc_dev->dma_base[BANK_L_CTX] = mfc_dev->fw_buf.dma;
  1038. bank2_virt = dma_alloc_coherent(mfc_dev->mem_dev[BANK_R_CTX],
  1039. align_size, &bank2_dma_addr, GFP_KERNEL);
  1040. if (!bank2_virt) {
  1041. mfc_err("Allocating bank2 base failed\n");
  1042. s5p_mfc_release_firmware(mfc_dev);
  1043. device_unregister(mfc_dev->mem_dev[BANK_R_CTX]);
  1044. device_unregister(mfc_dev->mem_dev[BANK_L_CTX]);
  1045. return -ENOMEM;
  1046. }
  1047. /* Valid buffers passed to MFC encoder with LAST_FRAME command
  1048. * should not have address of bank2 - MFC will treat it as a null frame.
  1049. * To avoid such situation we set bank2 address below the pool address.
  1050. */
  1051. mfc_dev->dma_base[BANK_R_CTX] = bank2_dma_addr - align_size;
  1052. dma_free_coherent(mfc_dev->mem_dev[BANK_R_CTX], align_size, bank2_virt,
  1053. bank2_dma_addr);
  1054. vb2_dma_contig_set_max_seg_size(mfc_dev->mem_dev[BANK_L_CTX],
  1055. DMA_BIT_MASK(32));
  1056. vb2_dma_contig_set_max_seg_size(mfc_dev->mem_dev[BANK_R_CTX],
  1057. DMA_BIT_MASK(32));
  1058. return 0;
  1059. }
  1060. static void s5p_mfc_unconfigure_2port_memory(struct s5p_mfc_dev *mfc_dev)
  1061. {
  1062. device_unregister(mfc_dev->mem_dev[BANK_L_CTX]);
  1063. device_unregister(mfc_dev->mem_dev[BANK_R_CTX]);
  1064. vb2_dma_contig_clear_max_seg_size(mfc_dev->mem_dev[BANK_L_CTX]);
  1065. vb2_dma_contig_clear_max_seg_size(mfc_dev->mem_dev[BANK_R_CTX]);
  1066. }
  1067. static int s5p_mfc_configure_common_memory(struct s5p_mfc_dev *mfc_dev)
  1068. {
  1069. struct device *dev = &mfc_dev->plat_dev->dev;
  1070. unsigned long mem_size = SZ_4M;
  1071. unsigned int bitmap_size;
  1072. if (IS_ENABLED(CONFIG_DMA_CMA) || exynos_is_iommu_available(dev))
  1073. mem_size = SZ_8M;
  1074. if (mfc_mem_size)
  1075. mem_size = memparse(mfc_mem_size, NULL);
  1076. bitmap_size = BITS_TO_LONGS(mem_size >> PAGE_SHIFT) * sizeof(long);
  1077. mfc_dev->mem_bitmap = kzalloc(bitmap_size, GFP_KERNEL);
  1078. if (!mfc_dev->mem_bitmap)
  1079. return -ENOMEM;
  1080. mfc_dev->mem_virt = dma_alloc_coherent(dev, mem_size,
  1081. &mfc_dev->mem_base, GFP_KERNEL);
  1082. if (!mfc_dev->mem_virt) {
  1083. kfree(mfc_dev->mem_bitmap);
  1084. dev_err(dev, "failed to preallocate %ld MiB for the firmware and context buffers\n",
  1085. (mem_size / SZ_1M));
  1086. return -ENOMEM;
  1087. }
  1088. mfc_dev->mem_size = mem_size;
  1089. mfc_dev->dma_base[BANK_L_CTX] = mfc_dev->mem_base;
  1090. mfc_dev->dma_base[BANK_R_CTX] = mfc_dev->mem_base;
  1091. /*
  1092. * MFC hardware cannot handle 0 as a base address, so mark first 128K
  1093. * as used (to keep required base alignment) and adjust base address
  1094. */
  1095. if (mfc_dev->mem_base == (dma_addr_t)0) {
  1096. unsigned int offset = 1 << MFC_BASE_ALIGN_ORDER;
  1097. bitmap_set(mfc_dev->mem_bitmap, 0, offset >> PAGE_SHIFT);
  1098. mfc_dev->dma_base[BANK_L_CTX] += offset;
  1099. mfc_dev->dma_base[BANK_R_CTX] += offset;
  1100. }
  1101. /* Firmware allocation cannot fail in this case */
  1102. s5p_mfc_alloc_firmware(mfc_dev);
  1103. mfc_dev->mem_dev[BANK_L_CTX] = mfc_dev->mem_dev[BANK_R_CTX] = dev;
  1104. vb2_dma_contig_set_max_seg_size(dev, DMA_BIT_MASK(32));
  1105. dev_info(dev, "preallocated %ld MiB buffer for the firmware and context buffers\n",
  1106. (mem_size / SZ_1M));
  1107. return 0;
  1108. }
  1109. static void s5p_mfc_unconfigure_common_memory(struct s5p_mfc_dev *mfc_dev)
  1110. {
  1111. struct device *dev = &mfc_dev->plat_dev->dev;
  1112. dma_free_coherent(dev, mfc_dev->mem_size, mfc_dev->mem_virt,
  1113. mfc_dev->mem_base);
  1114. kfree(mfc_dev->mem_bitmap);
  1115. vb2_dma_contig_clear_max_seg_size(dev);
  1116. }
  1117. static int s5p_mfc_configure_dma_memory(struct s5p_mfc_dev *mfc_dev)
  1118. {
  1119. struct device *dev = &mfc_dev->plat_dev->dev;
  1120. if (exynos_is_iommu_available(dev) || !IS_TWOPORT(mfc_dev))
  1121. return s5p_mfc_configure_common_memory(mfc_dev);
  1122. else
  1123. return s5p_mfc_configure_2port_memory(mfc_dev);
  1124. }
  1125. static void s5p_mfc_unconfigure_dma_memory(struct s5p_mfc_dev *mfc_dev)
  1126. {
  1127. struct device *dev = &mfc_dev->plat_dev->dev;
  1128. s5p_mfc_release_firmware(mfc_dev);
  1129. if (exynos_is_iommu_available(dev) || !IS_TWOPORT(mfc_dev))
  1130. s5p_mfc_unconfigure_common_memory(mfc_dev);
  1131. else
  1132. s5p_mfc_unconfigure_2port_memory(mfc_dev);
  1133. }
  1134. /* MFC probe function */
  1135. static int s5p_mfc_probe(struct platform_device *pdev)
  1136. {
  1137. struct s5p_mfc_dev *dev;
  1138. struct video_device *vfd;
  1139. struct resource *res;
  1140. int ret;
  1141. pr_debug("%s++\n", __func__);
  1142. dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
  1143. if (!dev) {
  1144. dev_err(&pdev->dev, "Not enough memory for MFC device\n");
  1145. return -ENOMEM;
  1146. }
  1147. spin_lock_init(&dev->irqlock);
  1148. spin_lock_init(&dev->condlock);
  1149. dev->plat_dev = pdev;
  1150. if (!dev->plat_dev) {
  1151. dev_err(&pdev->dev, "No platform data specified\n");
  1152. return -ENODEV;
  1153. }
  1154. dev->variant = of_device_get_match_data(&pdev->dev);
  1155. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1156. dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
  1157. if (IS_ERR(dev->regs_base))
  1158. return PTR_ERR(dev->regs_base);
  1159. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1160. if (res == NULL) {
  1161. dev_err(&pdev->dev, "failed to get irq resource\n");
  1162. return -ENOENT;
  1163. }
  1164. dev->irq = res->start;
  1165. ret = devm_request_irq(&pdev->dev, dev->irq, s5p_mfc_irq,
  1166. 0, pdev->name, dev);
  1167. if (ret) {
  1168. dev_err(&pdev->dev, "Failed to install irq (%d)\n", ret);
  1169. return ret;
  1170. }
  1171. ret = s5p_mfc_configure_dma_memory(dev);
  1172. if (ret < 0) {
  1173. dev_err(&pdev->dev, "failed to configure DMA memory\n");
  1174. return ret;
  1175. }
  1176. ret = s5p_mfc_init_pm(dev);
  1177. if (ret < 0) {
  1178. dev_err(&pdev->dev, "failed to get mfc clock source\n");
  1179. goto err_dma;
  1180. }
  1181. mutex_init(&dev->mfc_mutex);
  1182. init_waitqueue_head(&dev->queue);
  1183. dev->hw_lock = 0;
  1184. INIT_WORK(&dev->watchdog_work, s5p_mfc_watchdog_worker);
  1185. atomic_set(&dev->watchdog_cnt, 0);
  1186. init_timer(&dev->watchdog_timer);
  1187. dev->watchdog_timer.data = (unsigned long)dev;
  1188. dev->watchdog_timer.function = s5p_mfc_watchdog;
  1189. ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
  1190. if (ret)
  1191. goto err_v4l2_dev_reg;
  1192. /* decoder */
  1193. vfd = video_device_alloc();
  1194. if (!vfd) {
  1195. v4l2_err(&dev->v4l2_dev, "Failed to allocate video device\n");
  1196. ret = -ENOMEM;
  1197. goto err_dec_alloc;
  1198. }
  1199. vfd->fops = &s5p_mfc_fops;
  1200. vfd->ioctl_ops = get_dec_v4l2_ioctl_ops();
  1201. vfd->release = video_device_release;
  1202. vfd->lock = &dev->mfc_mutex;
  1203. vfd->v4l2_dev = &dev->v4l2_dev;
  1204. vfd->vfl_dir = VFL_DIR_M2M;
  1205. snprintf(vfd->name, sizeof(vfd->name), "%s", S5P_MFC_DEC_NAME);
  1206. dev->vfd_dec = vfd;
  1207. video_set_drvdata(vfd, dev);
  1208. /* encoder */
  1209. vfd = video_device_alloc();
  1210. if (!vfd) {
  1211. v4l2_err(&dev->v4l2_dev, "Failed to allocate video device\n");
  1212. ret = -ENOMEM;
  1213. goto err_enc_alloc;
  1214. }
  1215. vfd->fops = &s5p_mfc_fops;
  1216. vfd->ioctl_ops = get_enc_v4l2_ioctl_ops();
  1217. vfd->release = video_device_release;
  1218. vfd->lock = &dev->mfc_mutex;
  1219. vfd->v4l2_dev = &dev->v4l2_dev;
  1220. vfd->vfl_dir = VFL_DIR_M2M;
  1221. snprintf(vfd->name, sizeof(vfd->name), "%s", S5P_MFC_ENC_NAME);
  1222. dev->vfd_enc = vfd;
  1223. video_set_drvdata(vfd, dev);
  1224. platform_set_drvdata(pdev, dev);
  1225. /* Initialize HW ops and commands based on MFC version */
  1226. s5p_mfc_init_hw_ops(dev);
  1227. s5p_mfc_init_hw_cmds(dev);
  1228. s5p_mfc_init_regs(dev);
  1229. /* Register decoder and encoder */
  1230. ret = video_register_device(dev->vfd_dec, VFL_TYPE_GRABBER, 0);
  1231. if (ret) {
  1232. v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
  1233. goto err_dec_reg;
  1234. }
  1235. v4l2_info(&dev->v4l2_dev,
  1236. "decoder registered as /dev/video%d\n", dev->vfd_dec->num);
  1237. ret = video_register_device(dev->vfd_enc, VFL_TYPE_GRABBER, 0);
  1238. if (ret) {
  1239. v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
  1240. goto err_enc_reg;
  1241. }
  1242. v4l2_info(&dev->v4l2_dev,
  1243. "encoder registered as /dev/video%d\n", dev->vfd_enc->num);
  1244. pr_debug("%s--\n", __func__);
  1245. return 0;
  1246. /* Deinit MFC if probe had failed */
  1247. err_enc_reg:
  1248. video_unregister_device(dev->vfd_dec);
  1249. err_dec_reg:
  1250. video_device_release(dev->vfd_enc);
  1251. err_enc_alloc:
  1252. video_device_release(dev->vfd_dec);
  1253. err_dec_alloc:
  1254. v4l2_device_unregister(&dev->v4l2_dev);
  1255. err_v4l2_dev_reg:
  1256. s5p_mfc_final_pm(dev);
  1257. err_dma:
  1258. s5p_mfc_unconfigure_dma_memory(dev);
  1259. pr_debug("%s-- with error\n", __func__);
  1260. return ret;
  1261. }
  1262. /* Remove the driver */
  1263. static int s5p_mfc_remove(struct platform_device *pdev)
  1264. {
  1265. struct s5p_mfc_dev *dev = platform_get_drvdata(pdev);
  1266. struct s5p_mfc_ctx *ctx;
  1267. int i;
  1268. v4l2_info(&dev->v4l2_dev, "Removing %s\n", pdev->name);
  1269. /*
  1270. * Clear ctx dev pointer to avoid races between s5p_mfc_remove()
  1271. * and s5p_mfc_release() and s5p_mfc_release() accessing ctx->dev
  1272. * after s5p_mfc_remove() is run during unbind.
  1273. */
  1274. mutex_lock(&dev->mfc_mutex);
  1275. for (i = 0; i < MFC_NUM_CONTEXTS; i++) {
  1276. ctx = dev->ctx[i];
  1277. if (!ctx)
  1278. continue;
  1279. /* clear ctx->dev */
  1280. ctx->dev = NULL;
  1281. }
  1282. mutex_unlock(&dev->mfc_mutex);
  1283. del_timer_sync(&dev->watchdog_timer);
  1284. flush_work(&dev->watchdog_work);
  1285. video_unregister_device(dev->vfd_enc);
  1286. video_unregister_device(dev->vfd_dec);
  1287. video_device_release(dev->vfd_enc);
  1288. video_device_release(dev->vfd_dec);
  1289. v4l2_device_unregister(&dev->v4l2_dev);
  1290. s5p_mfc_unconfigure_dma_memory(dev);
  1291. s5p_mfc_final_pm(dev);
  1292. return 0;
  1293. }
  1294. #ifdef CONFIG_PM_SLEEP
  1295. static int s5p_mfc_suspend(struct device *dev)
  1296. {
  1297. struct platform_device *pdev = to_platform_device(dev);
  1298. struct s5p_mfc_dev *m_dev = platform_get_drvdata(pdev);
  1299. int ret;
  1300. if (m_dev->num_inst == 0)
  1301. return 0;
  1302. if (test_and_set_bit(0, &m_dev->enter_suspend) != 0) {
  1303. mfc_err("Error: going to suspend for a second time\n");
  1304. return -EIO;
  1305. }
  1306. /* Check if we're processing then wait if it necessary. */
  1307. while (test_and_set_bit(0, &m_dev->hw_lock) != 0) {
  1308. /* Try and lock the HW */
  1309. /* Wait on the interrupt waitqueue */
  1310. ret = wait_event_interruptible_timeout(m_dev->queue,
  1311. m_dev->int_cond, msecs_to_jiffies(MFC_INT_TIMEOUT));
  1312. if (ret == 0) {
  1313. mfc_err("Waiting for hardware to finish timed out\n");
  1314. clear_bit(0, &m_dev->enter_suspend);
  1315. return -EIO;
  1316. }
  1317. }
  1318. ret = s5p_mfc_sleep(m_dev);
  1319. if (ret) {
  1320. clear_bit(0, &m_dev->enter_suspend);
  1321. clear_bit(0, &m_dev->hw_lock);
  1322. }
  1323. return ret;
  1324. }
  1325. static int s5p_mfc_resume(struct device *dev)
  1326. {
  1327. struct platform_device *pdev = to_platform_device(dev);
  1328. struct s5p_mfc_dev *m_dev = platform_get_drvdata(pdev);
  1329. if (m_dev->num_inst == 0)
  1330. return 0;
  1331. return s5p_mfc_wakeup(m_dev);
  1332. }
  1333. #endif
  1334. /* Power management */
  1335. static const struct dev_pm_ops s5p_mfc_pm_ops = {
  1336. SET_SYSTEM_SLEEP_PM_OPS(s5p_mfc_suspend, s5p_mfc_resume)
  1337. };
  1338. static struct s5p_mfc_buf_size_v5 mfc_buf_size_v5 = {
  1339. .h264_ctx = MFC_H264_CTX_BUF_SIZE,
  1340. .non_h264_ctx = MFC_CTX_BUF_SIZE,
  1341. .dsc = DESC_BUF_SIZE,
  1342. .shm = SHARED_BUF_SIZE,
  1343. };
  1344. static struct s5p_mfc_buf_size buf_size_v5 = {
  1345. .fw = MAX_FW_SIZE,
  1346. .cpb = MAX_CPB_SIZE,
  1347. .priv = &mfc_buf_size_v5,
  1348. };
  1349. static struct s5p_mfc_variant mfc_drvdata_v5 = {
  1350. .version = MFC_VERSION,
  1351. .version_bit = MFC_V5_BIT,
  1352. .port_num = MFC_NUM_PORTS,
  1353. .buf_size = &buf_size_v5,
  1354. .fw_name[0] = "s5p-mfc.fw",
  1355. .clk_names = {"mfc", "sclk_mfc"},
  1356. .num_clocks = 2,
  1357. .use_clock_gating = true,
  1358. };
  1359. static struct s5p_mfc_buf_size_v6 mfc_buf_size_v6 = {
  1360. .dev_ctx = MFC_CTX_BUF_SIZE_V6,
  1361. .h264_dec_ctx = MFC_H264_DEC_CTX_BUF_SIZE_V6,
  1362. .other_dec_ctx = MFC_OTHER_DEC_CTX_BUF_SIZE_V6,
  1363. .h264_enc_ctx = MFC_H264_ENC_CTX_BUF_SIZE_V6,
  1364. .other_enc_ctx = MFC_OTHER_ENC_CTX_BUF_SIZE_V6,
  1365. };
  1366. static struct s5p_mfc_buf_size buf_size_v6 = {
  1367. .fw = MAX_FW_SIZE_V6,
  1368. .cpb = MAX_CPB_SIZE_V6,
  1369. .priv = &mfc_buf_size_v6,
  1370. };
  1371. static struct s5p_mfc_variant mfc_drvdata_v6 = {
  1372. .version = MFC_VERSION_V6,
  1373. .version_bit = MFC_V6_BIT,
  1374. .port_num = MFC_NUM_PORTS_V6,
  1375. .buf_size = &buf_size_v6,
  1376. .fw_name[0] = "s5p-mfc-v6.fw",
  1377. /*
  1378. * v6-v2 firmware contains bug fixes and interface change
  1379. * for init buffer command
  1380. */
  1381. .fw_name[1] = "s5p-mfc-v6-v2.fw",
  1382. .clk_names = {"mfc"},
  1383. .num_clocks = 1,
  1384. };
  1385. static struct s5p_mfc_buf_size_v6 mfc_buf_size_v7 = {
  1386. .dev_ctx = MFC_CTX_BUF_SIZE_V7,
  1387. .h264_dec_ctx = MFC_H264_DEC_CTX_BUF_SIZE_V7,
  1388. .other_dec_ctx = MFC_OTHER_DEC_CTX_BUF_SIZE_V7,
  1389. .h264_enc_ctx = MFC_H264_ENC_CTX_BUF_SIZE_V7,
  1390. .other_enc_ctx = MFC_OTHER_ENC_CTX_BUF_SIZE_V7,
  1391. };
  1392. static struct s5p_mfc_buf_size buf_size_v7 = {
  1393. .fw = MAX_FW_SIZE_V7,
  1394. .cpb = MAX_CPB_SIZE_V7,
  1395. .priv = &mfc_buf_size_v7,
  1396. };
  1397. static struct s5p_mfc_variant mfc_drvdata_v7 = {
  1398. .version = MFC_VERSION_V7,
  1399. .version_bit = MFC_V7_BIT,
  1400. .port_num = MFC_NUM_PORTS_V7,
  1401. .buf_size = &buf_size_v7,
  1402. .fw_name[0] = "s5p-mfc-v7.fw",
  1403. .clk_names = {"mfc", "sclk_mfc"},
  1404. .num_clocks = 2,
  1405. };
  1406. static struct s5p_mfc_buf_size_v6 mfc_buf_size_v8 = {
  1407. .dev_ctx = MFC_CTX_BUF_SIZE_V8,
  1408. .h264_dec_ctx = MFC_H264_DEC_CTX_BUF_SIZE_V8,
  1409. .other_dec_ctx = MFC_OTHER_DEC_CTX_BUF_SIZE_V8,
  1410. .h264_enc_ctx = MFC_H264_ENC_CTX_BUF_SIZE_V8,
  1411. .other_enc_ctx = MFC_OTHER_ENC_CTX_BUF_SIZE_V8,
  1412. };
  1413. static struct s5p_mfc_buf_size buf_size_v8 = {
  1414. .fw = MAX_FW_SIZE_V8,
  1415. .cpb = MAX_CPB_SIZE_V8,
  1416. .priv = &mfc_buf_size_v8,
  1417. };
  1418. static struct s5p_mfc_variant mfc_drvdata_v8 = {
  1419. .version = MFC_VERSION_V8,
  1420. .version_bit = MFC_V8_BIT,
  1421. .port_num = MFC_NUM_PORTS_V8,
  1422. .buf_size = &buf_size_v8,
  1423. .fw_name[0] = "s5p-mfc-v8.fw",
  1424. .clk_names = {"mfc"},
  1425. .num_clocks = 1,
  1426. };
  1427. static struct s5p_mfc_variant mfc_drvdata_v8_5433 = {
  1428. .version = MFC_VERSION_V8,
  1429. .version_bit = MFC_V8_BIT,
  1430. .port_num = MFC_NUM_PORTS_V8,
  1431. .buf_size = &buf_size_v8,
  1432. .fw_name[0] = "s5p-mfc-v8.fw",
  1433. .clk_names = {"pclk", "aclk", "aclk_xiu"},
  1434. .num_clocks = 3,
  1435. };
  1436. static const struct of_device_id exynos_mfc_match[] = {
  1437. {
  1438. .compatible = "samsung,mfc-v5",
  1439. .data = &mfc_drvdata_v5,
  1440. }, {
  1441. .compatible = "samsung,mfc-v6",
  1442. .data = &mfc_drvdata_v6,
  1443. }, {
  1444. .compatible = "samsung,mfc-v7",
  1445. .data = &mfc_drvdata_v7,
  1446. }, {
  1447. .compatible = "samsung,mfc-v8",
  1448. .data = &mfc_drvdata_v8,
  1449. }, {
  1450. .compatible = "samsung,exynos5433-mfc",
  1451. .data = &mfc_drvdata_v8_5433,
  1452. },
  1453. {},
  1454. };
  1455. MODULE_DEVICE_TABLE(of, exynos_mfc_match);
  1456. static struct platform_driver s5p_mfc_driver = {
  1457. .probe = s5p_mfc_probe,
  1458. .remove = s5p_mfc_remove,
  1459. .driver = {
  1460. .name = S5P_MFC_NAME,
  1461. .pm = &s5p_mfc_pm_ops,
  1462. .of_match_table = exynos_mfc_match,
  1463. },
  1464. };
  1465. module_platform_driver(s5p_mfc_driver);
  1466. MODULE_LICENSE("GPL");
  1467. MODULE_AUTHOR("Kamil Debski <k.debski@samsung.com>");
  1468. MODULE_DESCRIPTION("Samsung S5P Multi Format Codec V4L2 driver");