mtk_vcodec_util.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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/module.h>
  16. #include "mtk_vcodec_drv.h"
  17. #include "mtk_vcodec_util.h"
  18. #include "mtk_vpu.h"
  19. /* For encoder, this will enable logs in venc/*/
  20. bool mtk_vcodec_dbg;
  21. EXPORT_SYMBOL(mtk_vcodec_dbg);
  22. /* The log level of v4l2 encoder or decoder driver.
  23. * That is, files under mtk-vcodec/.
  24. */
  25. int mtk_v4l2_dbg_level;
  26. EXPORT_SYMBOL(mtk_v4l2_dbg_level);
  27. void __iomem *mtk_vcodec_get_reg_addr(struct mtk_vcodec_ctx *data,
  28. unsigned int reg_idx)
  29. {
  30. struct mtk_vcodec_ctx *ctx = (struct mtk_vcodec_ctx *)data;
  31. if (!data || reg_idx >= NUM_MAX_VCODEC_REG_BASE) {
  32. mtk_v4l2_err("Invalid arguments, reg_idx=%d", reg_idx);
  33. return NULL;
  34. }
  35. return ctx->dev->reg_base[reg_idx];
  36. }
  37. EXPORT_SYMBOL(mtk_vcodec_get_reg_addr);
  38. int mtk_vcodec_mem_alloc(struct mtk_vcodec_ctx *data,
  39. struct mtk_vcodec_mem *mem)
  40. {
  41. unsigned long size = mem->size;
  42. struct mtk_vcodec_ctx *ctx = (struct mtk_vcodec_ctx *)data;
  43. struct device *dev = &ctx->dev->plat_dev->dev;
  44. mem->va = dma_alloc_coherent(dev, size, &mem->dma_addr, GFP_KERNEL);
  45. if (!mem->va) {
  46. mtk_v4l2_err("%s dma_alloc size=%ld failed!", dev_name(dev),
  47. size);
  48. return -ENOMEM;
  49. }
  50. memset(mem->va, 0, size);
  51. mtk_v4l2_debug(3, "[%d] - va = %p", ctx->id, mem->va);
  52. mtk_v4l2_debug(3, "[%d] - dma = 0x%lx", ctx->id,
  53. (unsigned long)mem->dma_addr);
  54. mtk_v4l2_debug(3, "[%d] size = 0x%lx", ctx->id, size);
  55. return 0;
  56. }
  57. EXPORT_SYMBOL(mtk_vcodec_mem_alloc);
  58. void mtk_vcodec_mem_free(struct mtk_vcodec_ctx *data,
  59. struct mtk_vcodec_mem *mem)
  60. {
  61. unsigned long size = mem->size;
  62. struct mtk_vcodec_ctx *ctx = (struct mtk_vcodec_ctx *)data;
  63. struct device *dev = &ctx->dev->plat_dev->dev;
  64. if (!mem->va) {
  65. mtk_v4l2_err("%s dma_free size=%ld failed!", dev_name(dev),
  66. size);
  67. return;
  68. }
  69. mtk_v4l2_debug(3, "[%d] - va = %p", ctx->id, mem->va);
  70. mtk_v4l2_debug(3, "[%d] - dma = 0x%lx", ctx->id,
  71. (unsigned long)mem->dma_addr);
  72. mtk_v4l2_debug(3, "[%d] size = 0x%lx", ctx->id, size);
  73. dma_free_coherent(dev, size, mem->va, mem->dma_addr);
  74. mem->va = NULL;
  75. mem->dma_addr = 0;
  76. mem->size = 0;
  77. }
  78. EXPORT_SYMBOL(mtk_vcodec_mem_free);
  79. void mtk_vcodec_set_curr_ctx(struct mtk_vcodec_dev *dev,
  80. struct mtk_vcodec_ctx *ctx)
  81. {
  82. unsigned long flags;
  83. spin_lock_irqsave(&dev->irqlock, flags);
  84. dev->curr_ctx = ctx;
  85. spin_unlock_irqrestore(&dev->irqlock, flags);
  86. }
  87. EXPORT_SYMBOL(mtk_vcodec_set_curr_ctx);
  88. struct mtk_vcodec_ctx *mtk_vcodec_get_curr_ctx(struct mtk_vcodec_dev *dev)
  89. {
  90. unsigned long flags;
  91. struct mtk_vcodec_ctx *ctx;
  92. spin_lock_irqsave(&dev->irqlock, flags);
  93. ctx = dev->curr_ctx;
  94. spin_unlock_irqrestore(&dev->irqlock, flags);
  95. return ctx;
  96. }
  97. EXPORT_SYMBOL(mtk_vcodec_get_curr_ctx);
  98. MODULE_LICENSE("GPL v2");
  99. MODULE_DESCRIPTION("Mediatek video codec driver");