mtk_vcodec_dec_drv.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /*
  2. * Copyright (c) 2016 MediaTek Inc.
  3. * Author: PC Chen <pc.chen@mediatek.com>
  4. * Tiffany Lin <tiffany.lin@mediatek.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/slab.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/irq.h>
  18. #include <linux/module.h>
  19. #include <linux/of_device.h>
  20. #include <linux/of.h>
  21. #include <media/v4l2-event.h>
  22. #include <media/v4l2-mem2mem.h>
  23. #include <media/videobuf2-dma-contig.h>
  24. #include "mtk_vcodec_drv.h"
  25. #include "mtk_vcodec_dec.h"
  26. #include "mtk_vcodec_dec_pm.h"
  27. #include "mtk_vcodec_intr.h"
  28. #include "mtk_vcodec_util.h"
  29. #include "mtk_vpu.h"
  30. #define VDEC_HW_ACTIVE 0x10
  31. #define VDEC_IRQ_CFG 0x11
  32. #define VDEC_IRQ_CLR 0x10
  33. #define VDEC_IRQ_CFG_REG 0xa4
  34. module_param(mtk_v4l2_dbg_level, int, 0644);
  35. module_param(mtk_vcodec_dbg, bool, 0644);
  36. /* Wake up context wait_queue */
  37. static void wake_up_ctx(struct mtk_vcodec_ctx *ctx)
  38. {
  39. ctx->int_cond = 1;
  40. wake_up_interruptible(&ctx->queue);
  41. }
  42. static irqreturn_t mtk_vcodec_dec_irq_handler(int irq, void *priv)
  43. {
  44. struct mtk_vcodec_dev *dev = priv;
  45. struct mtk_vcodec_ctx *ctx;
  46. u32 cg_status = 0;
  47. unsigned int dec_done_status = 0;
  48. void __iomem *vdec_misc_addr = dev->reg_base[VDEC_MISC] +
  49. VDEC_IRQ_CFG_REG;
  50. ctx = mtk_vcodec_get_curr_ctx(dev);
  51. /* check if HW active or not */
  52. cg_status = readl(dev->reg_base[0]);
  53. if ((cg_status & VDEC_HW_ACTIVE) != 0) {
  54. mtk_v4l2_err("DEC ISR, VDEC active is not 0x0 (0x%08x)",
  55. cg_status);
  56. return IRQ_HANDLED;
  57. }
  58. dec_done_status = readl(vdec_misc_addr);
  59. ctx->irq_status = dec_done_status;
  60. if ((dec_done_status & MTK_VDEC_IRQ_STATUS_DEC_SUCCESS) !=
  61. MTK_VDEC_IRQ_STATUS_DEC_SUCCESS)
  62. return IRQ_HANDLED;
  63. /* clear interrupt */
  64. writel((readl(vdec_misc_addr) | VDEC_IRQ_CFG),
  65. dev->reg_base[VDEC_MISC] + VDEC_IRQ_CFG_REG);
  66. writel((readl(vdec_misc_addr) & ~VDEC_IRQ_CLR),
  67. dev->reg_base[VDEC_MISC] + VDEC_IRQ_CFG_REG);
  68. wake_up_ctx(ctx);
  69. mtk_v4l2_debug(3,
  70. "mtk_vcodec_dec_irq_handler :wake up ctx %d, dec_done_status=%x",
  71. ctx->id, dec_done_status);
  72. return IRQ_HANDLED;
  73. }
  74. static void mtk_vcodec_dec_reset_handler(void *priv)
  75. {
  76. struct mtk_vcodec_dev *dev = priv;
  77. struct mtk_vcodec_ctx *ctx;
  78. mtk_v4l2_err("Watchdog timeout!!");
  79. mutex_lock(&dev->dev_mutex);
  80. list_for_each_entry(ctx, &dev->ctx_list, list) {
  81. ctx->state = MTK_STATE_ABORT;
  82. mtk_v4l2_debug(0, "[%d] Change to state MTK_STATE_ERROR",
  83. ctx->id);
  84. }
  85. mutex_unlock(&dev->dev_mutex);
  86. }
  87. static int fops_vcodec_open(struct file *file)
  88. {
  89. struct mtk_vcodec_dev *dev = video_drvdata(file);
  90. struct mtk_vcodec_ctx *ctx = NULL;
  91. struct mtk_video_dec_buf *mtk_buf = NULL;
  92. int ret = 0;
  93. struct vb2_queue *src_vq;
  94. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  95. if (!ctx)
  96. return -ENOMEM;
  97. mtk_buf = kzalloc(sizeof(*mtk_buf), GFP_KERNEL);
  98. if (!mtk_buf) {
  99. kfree(ctx);
  100. return -ENOMEM;
  101. }
  102. mutex_lock(&dev->dev_mutex);
  103. ctx->empty_flush_buf = mtk_buf;
  104. ctx->id = dev->id_counter++;
  105. v4l2_fh_init(&ctx->fh, video_devdata(file));
  106. file->private_data = &ctx->fh;
  107. v4l2_fh_add(&ctx->fh);
  108. INIT_LIST_HEAD(&ctx->list);
  109. ctx->dev = dev;
  110. init_waitqueue_head(&ctx->queue);
  111. mutex_init(&ctx->lock);
  112. ctx->type = MTK_INST_DECODER;
  113. ret = mtk_vcodec_dec_ctrls_setup(ctx);
  114. if (ret) {
  115. mtk_v4l2_err("Failed to setup mt vcodec controls");
  116. goto err_ctrls_setup;
  117. }
  118. ctx->m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev_dec, ctx,
  119. &mtk_vcodec_dec_queue_init);
  120. if (IS_ERR((__force void *)ctx->m2m_ctx)) {
  121. ret = PTR_ERR((__force void *)ctx->m2m_ctx);
  122. mtk_v4l2_err("Failed to v4l2_m2m_ctx_init() (%d)",
  123. ret);
  124. goto err_m2m_ctx_init;
  125. }
  126. src_vq = v4l2_m2m_get_vq(ctx->m2m_ctx,
  127. V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
  128. ctx->empty_flush_buf->vb.vb2_buf.vb2_queue = src_vq;
  129. ctx->empty_flush_buf->lastframe = true;
  130. mtk_vcodec_dec_set_default_params(ctx);
  131. if (v4l2_fh_is_singular(&ctx->fh)) {
  132. mtk_vcodec_dec_pw_on(&dev->pm);
  133. /*
  134. * vpu_load_firmware checks if it was loaded already and
  135. * does nothing in that case
  136. */
  137. ret = vpu_load_firmware(dev->vpu_plat_dev);
  138. if (ret < 0) {
  139. /*
  140. * Return 0 if downloading firmware successfully,
  141. * otherwise it is failed
  142. */
  143. mtk_v4l2_err("vpu_load_firmware failed!");
  144. goto err_load_fw;
  145. }
  146. dev->dec_capability =
  147. vpu_get_vdec_hw_capa(dev->vpu_plat_dev);
  148. mtk_v4l2_debug(0, "decoder capability %x", dev->dec_capability);
  149. }
  150. list_add(&ctx->list, &dev->ctx_list);
  151. mutex_unlock(&dev->dev_mutex);
  152. mtk_v4l2_debug(0, "%s decoder [%d]", dev_name(&dev->plat_dev->dev),
  153. ctx->id);
  154. return ret;
  155. /* Deinit when failure occurred */
  156. err_load_fw:
  157. v4l2_m2m_ctx_release(ctx->m2m_ctx);
  158. err_m2m_ctx_init:
  159. v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
  160. err_ctrls_setup:
  161. v4l2_fh_del(&ctx->fh);
  162. v4l2_fh_exit(&ctx->fh);
  163. kfree(ctx->empty_flush_buf);
  164. kfree(ctx);
  165. mutex_unlock(&dev->dev_mutex);
  166. return ret;
  167. }
  168. static int fops_vcodec_release(struct file *file)
  169. {
  170. struct mtk_vcodec_dev *dev = video_drvdata(file);
  171. struct mtk_vcodec_ctx *ctx = fh_to_ctx(file->private_data);
  172. mtk_v4l2_debug(0, "[%d] decoder", ctx->id);
  173. mutex_lock(&dev->dev_mutex);
  174. /*
  175. * Call v4l2_m2m_ctx_release before mtk_vcodec_dec_release. First, it
  176. * makes sure the worker thread is not running after vdec_if_deinit.
  177. * Second, the decoder will be flushed and all the buffers will be
  178. * returned in stop_streaming.
  179. */
  180. v4l2_m2m_ctx_release(ctx->m2m_ctx);
  181. mtk_vcodec_dec_release(ctx);
  182. if (v4l2_fh_is_singular(&ctx->fh))
  183. mtk_vcodec_dec_pw_off(&dev->pm);
  184. v4l2_fh_del(&ctx->fh);
  185. v4l2_fh_exit(&ctx->fh);
  186. v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
  187. list_del_init(&ctx->list);
  188. kfree(ctx->empty_flush_buf);
  189. kfree(ctx);
  190. mutex_unlock(&dev->dev_mutex);
  191. return 0;
  192. }
  193. static const struct v4l2_file_operations mtk_vcodec_fops = {
  194. .owner = THIS_MODULE,
  195. .open = fops_vcodec_open,
  196. .release = fops_vcodec_release,
  197. .poll = v4l2_m2m_fop_poll,
  198. .unlocked_ioctl = video_ioctl2,
  199. .mmap = v4l2_m2m_fop_mmap,
  200. };
  201. static int mtk_vcodec_probe(struct platform_device *pdev)
  202. {
  203. struct mtk_vcodec_dev *dev;
  204. struct video_device *vfd_dec;
  205. struct resource *res;
  206. int i, ret;
  207. dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
  208. if (!dev)
  209. return -ENOMEM;
  210. INIT_LIST_HEAD(&dev->ctx_list);
  211. dev->plat_dev = pdev;
  212. dev->vpu_plat_dev = vpu_get_plat_device(dev->plat_dev);
  213. if (dev->vpu_plat_dev == NULL) {
  214. mtk_v4l2_err("[VPU] vpu device in not ready");
  215. return -EPROBE_DEFER;
  216. }
  217. vpu_wdt_reg_handler(dev->vpu_plat_dev, mtk_vcodec_dec_reset_handler,
  218. dev, VPU_RST_DEC);
  219. ret = mtk_vcodec_init_dec_pm(dev);
  220. if (ret < 0) {
  221. dev_err(&pdev->dev, "Failed to get mt vcodec clock source");
  222. return ret;
  223. }
  224. for (i = 0; i < NUM_MAX_VDEC_REG_BASE; i++) {
  225. res = platform_get_resource(pdev, IORESOURCE_MEM, i);
  226. if (res == NULL) {
  227. dev_err(&pdev->dev, "get memory resource failed.");
  228. ret = -ENXIO;
  229. goto err_res;
  230. }
  231. dev->reg_base[i] = devm_ioremap_resource(&pdev->dev, res);
  232. if (IS_ERR((__force void *)dev->reg_base[i])) {
  233. ret = PTR_ERR((__force void *)dev->reg_base[i]);
  234. goto err_res;
  235. }
  236. mtk_v4l2_debug(2, "reg[%d] base=%p", i, dev->reg_base[i]);
  237. }
  238. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  239. if (res == NULL) {
  240. dev_err(&pdev->dev, "failed to get irq resource");
  241. ret = -ENOENT;
  242. goto err_res;
  243. }
  244. dev->dec_irq = platform_get_irq(pdev, 0);
  245. ret = devm_request_irq(&pdev->dev, dev->dec_irq,
  246. mtk_vcodec_dec_irq_handler, 0, pdev->name, dev);
  247. if (ret) {
  248. dev_err(&pdev->dev, "Failed to install dev->dec_irq %d (%d)",
  249. dev->dec_irq,
  250. ret);
  251. goto err_res;
  252. }
  253. disable_irq(dev->dec_irq);
  254. mutex_init(&dev->dec_mutex);
  255. mutex_init(&dev->dev_mutex);
  256. spin_lock_init(&dev->irqlock);
  257. snprintf(dev->v4l2_dev.name, sizeof(dev->v4l2_dev.name), "%s",
  258. "[/MTK_V4L2_VDEC]");
  259. ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
  260. if (ret) {
  261. mtk_v4l2_err("v4l2_device_register err=%d", ret);
  262. goto err_res;
  263. }
  264. init_waitqueue_head(&dev->queue);
  265. vfd_dec = video_device_alloc();
  266. if (!vfd_dec) {
  267. mtk_v4l2_err("Failed to allocate video device");
  268. ret = -ENOMEM;
  269. goto err_dec_alloc;
  270. }
  271. vfd_dec->fops = &mtk_vcodec_fops;
  272. vfd_dec->ioctl_ops = &mtk_vdec_ioctl_ops;
  273. vfd_dec->release = video_device_release;
  274. vfd_dec->lock = &dev->dev_mutex;
  275. vfd_dec->v4l2_dev = &dev->v4l2_dev;
  276. vfd_dec->vfl_dir = VFL_DIR_M2M;
  277. vfd_dec->device_caps = V4L2_CAP_VIDEO_M2M_MPLANE |
  278. V4L2_CAP_STREAMING;
  279. snprintf(vfd_dec->name, sizeof(vfd_dec->name), "%s",
  280. MTK_VCODEC_DEC_NAME);
  281. video_set_drvdata(vfd_dec, dev);
  282. dev->vfd_dec = vfd_dec;
  283. platform_set_drvdata(pdev, dev);
  284. dev->m2m_dev_dec = v4l2_m2m_init(&mtk_vdec_m2m_ops);
  285. if (IS_ERR((__force void *)dev->m2m_dev_dec)) {
  286. mtk_v4l2_err("Failed to init mem2mem dec device");
  287. ret = PTR_ERR((__force void *)dev->m2m_dev_dec);
  288. goto err_dec_mem_init;
  289. }
  290. dev->decode_workqueue =
  291. alloc_ordered_workqueue(MTK_VCODEC_DEC_NAME,
  292. WQ_MEM_RECLAIM | WQ_FREEZABLE);
  293. if (!dev->decode_workqueue) {
  294. mtk_v4l2_err("Failed to create decode workqueue");
  295. ret = -EINVAL;
  296. goto err_event_workq;
  297. }
  298. ret = video_register_device(vfd_dec, VFL_TYPE_GRABBER, 0);
  299. if (ret) {
  300. mtk_v4l2_err("Failed to register video device");
  301. goto err_dec_reg;
  302. }
  303. mtk_v4l2_debug(0, "decoder registered as /dev/video%d",
  304. vfd_dec->num);
  305. return 0;
  306. err_dec_reg:
  307. destroy_workqueue(dev->decode_workqueue);
  308. err_event_workq:
  309. v4l2_m2m_release(dev->m2m_dev_dec);
  310. err_dec_mem_init:
  311. video_unregister_device(vfd_dec);
  312. err_dec_alloc:
  313. v4l2_device_unregister(&dev->v4l2_dev);
  314. err_res:
  315. mtk_vcodec_release_dec_pm(dev);
  316. return ret;
  317. }
  318. static const struct of_device_id mtk_vcodec_match[] = {
  319. {.compatible = "mediatek,mt8173-vcodec-dec",},
  320. {},
  321. };
  322. MODULE_DEVICE_TABLE(of, mtk_vcodec_match);
  323. static int mtk_vcodec_dec_remove(struct platform_device *pdev)
  324. {
  325. struct mtk_vcodec_dev *dev = platform_get_drvdata(pdev);
  326. flush_workqueue(dev->decode_workqueue);
  327. destroy_workqueue(dev->decode_workqueue);
  328. if (dev->m2m_dev_dec)
  329. v4l2_m2m_release(dev->m2m_dev_dec);
  330. if (dev->vfd_dec)
  331. video_unregister_device(dev->vfd_dec);
  332. v4l2_device_unregister(&dev->v4l2_dev);
  333. mtk_vcodec_release_dec_pm(dev);
  334. return 0;
  335. }
  336. static struct platform_driver mtk_vcodec_dec_driver = {
  337. .probe = mtk_vcodec_probe,
  338. .remove = mtk_vcodec_dec_remove,
  339. .driver = {
  340. .name = MTK_VCODEC_DEC_NAME,
  341. .of_match_table = mtk_vcodec_match,
  342. },
  343. };
  344. module_platform_driver(mtk_vcodec_dec_driver);
  345. MODULE_LICENSE("GPL v2");
  346. MODULE_DESCRIPTION("Mediatek video codec V4L2 decoder driver");