imx-vdoa.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*
  2. * i.MX6 Video Data Order Adapter (VDOA)
  3. *
  4. * Copyright (C) 2014 Philipp Zabel
  5. * Copyright (C) 2016 Pengutronix, Michael Tretter <kernel@pengutronix.de>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * version 2, as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/clk.h>
  17. #include <linux/device.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/module.h>
  20. #include <linux/dma-mapping.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/videodev2.h>
  23. #include <linux/slab.h>
  24. #include "imx-vdoa.h"
  25. #define VDOA_NAME "imx-vdoa"
  26. #define VDOAC 0x00
  27. #define VDOASRR 0x04
  28. #define VDOAIE 0x08
  29. #define VDOAIST 0x0c
  30. #define VDOAFP 0x10
  31. #define VDOAIEBA00 0x14
  32. #define VDOAIEBA01 0x18
  33. #define VDOAIEBA02 0x1c
  34. #define VDOAIEBA10 0x20
  35. #define VDOAIEBA11 0x24
  36. #define VDOAIEBA12 0x28
  37. #define VDOASL 0x2c
  38. #define VDOAIUBO 0x30
  39. #define VDOAVEBA0 0x34
  40. #define VDOAVEBA1 0x38
  41. #define VDOAVEBA2 0x3c
  42. #define VDOAVUBO 0x40
  43. #define VDOASR 0x44
  44. #define VDOAC_ISEL BIT(6)
  45. #define VDOAC_PFS BIT(5)
  46. #define VDOAC_SO BIT(4)
  47. #define VDOAC_SYNC BIT(3)
  48. #define VDOAC_NF BIT(2)
  49. #define VDOAC_BNDM_MASK 0x3
  50. #define VDOAC_BAND_HEIGHT_8 0x0
  51. #define VDOAC_BAND_HEIGHT_16 0x1
  52. #define VDOAC_BAND_HEIGHT_32 0x2
  53. #define VDOASRR_START BIT(1)
  54. #define VDOASRR_SWRST BIT(0)
  55. #define VDOAIE_EITERR BIT(1)
  56. #define VDOAIE_EIEOT BIT(0)
  57. #define VDOAIST_TERR BIT(1)
  58. #define VDOAIST_EOT BIT(0)
  59. #define VDOAFP_FH_MASK (0x1fff << 16)
  60. #define VDOAFP_FW_MASK (0x3fff)
  61. #define VDOASL_VSLY_MASK (0x3fff << 16)
  62. #define VDOASL_ISLY_MASK (0x7fff)
  63. #define VDOASR_ERRW BIT(4)
  64. #define VDOASR_EOB BIT(3)
  65. #define VDOASR_CURRENT_FRAME (0x3 << 1)
  66. #define VDOASR_CURRENT_BUFFER BIT(1)
  67. enum {
  68. V4L2_M2M_SRC = 0,
  69. V4L2_M2M_DST = 1,
  70. };
  71. struct vdoa_data {
  72. struct vdoa_ctx *curr_ctx;
  73. struct device *dev;
  74. struct clk *vdoa_clk;
  75. void __iomem *regs;
  76. int irq;
  77. };
  78. struct vdoa_q_data {
  79. unsigned int width;
  80. unsigned int height;
  81. unsigned int bytesperline;
  82. unsigned int sizeimage;
  83. u32 pixelformat;
  84. };
  85. struct vdoa_ctx {
  86. struct vdoa_data *vdoa;
  87. struct completion completion;
  88. struct vdoa_q_data q_data[2];
  89. };
  90. static irqreturn_t vdoa_irq_handler(int irq, void *data)
  91. {
  92. struct vdoa_data *vdoa = data;
  93. struct vdoa_ctx *curr_ctx;
  94. u32 val;
  95. /* Disable interrupts */
  96. writel(0, vdoa->regs + VDOAIE);
  97. curr_ctx = vdoa->curr_ctx;
  98. if (!curr_ctx) {
  99. dev_dbg(vdoa->dev,
  100. "Instance released before the end of transaction\n");
  101. return IRQ_HANDLED;
  102. }
  103. val = readl(vdoa->regs + VDOAIST);
  104. writel(val, vdoa->regs + VDOAIST);
  105. if (val & VDOAIST_TERR) {
  106. val = readl(vdoa->regs + VDOASR) & VDOASR_ERRW;
  107. dev_err(vdoa->dev, "AXI %s error\n", val ? "write" : "read");
  108. } else if (!(val & VDOAIST_EOT)) {
  109. dev_warn(vdoa->dev, "Spurious interrupt\n");
  110. }
  111. complete(&curr_ctx->completion);
  112. return IRQ_HANDLED;
  113. }
  114. void vdoa_device_run(struct vdoa_ctx *ctx, dma_addr_t dst, dma_addr_t src)
  115. {
  116. struct vdoa_q_data *src_q_data, *dst_q_data;
  117. struct vdoa_data *vdoa = ctx->vdoa;
  118. u32 val;
  119. vdoa->curr_ctx = ctx;
  120. src_q_data = &ctx->q_data[V4L2_M2M_SRC];
  121. dst_q_data = &ctx->q_data[V4L2_M2M_DST];
  122. /* Progressive, no sync, 1 frame per run */
  123. if (dst_q_data->pixelformat == V4L2_PIX_FMT_YUYV)
  124. val = VDOAC_PFS;
  125. else
  126. val = 0;
  127. writel(val, vdoa->regs + VDOAC);
  128. writel(dst_q_data->height << 16 | dst_q_data->width,
  129. vdoa->regs + VDOAFP);
  130. val = dst;
  131. writel(val, vdoa->regs + VDOAIEBA00);
  132. writel(src_q_data->bytesperline << 16 | dst_q_data->bytesperline,
  133. vdoa->regs + VDOASL);
  134. if (dst_q_data->pixelformat == V4L2_PIX_FMT_NV12 ||
  135. dst_q_data->pixelformat == V4L2_PIX_FMT_NV21)
  136. val = dst_q_data->bytesperline * dst_q_data->height;
  137. else
  138. val = 0;
  139. writel(val, vdoa->regs + VDOAIUBO);
  140. val = src;
  141. writel(val, vdoa->regs + VDOAVEBA0);
  142. val = round_up(src_q_data->bytesperline * src_q_data->height, 4096);
  143. writel(val, vdoa->regs + VDOAVUBO);
  144. /* Enable interrupts and start transfer */
  145. writel(VDOAIE_EITERR | VDOAIE_EIEOT, vdoa->regs + VDOAIE);
  146. writel(VDOASRR_START, vdoa->regs + VDOASRR);
  147. }
  148. EXPORT_SYMBOL(vdoa_device_run);
  149. int vdoa_wait_for_completion(struct vdoa_ctx *ctx)
  150. {
  151. struct vdoa_data *vdoa = ctx->vdoa;
  152. if (!wait_for_completion_timeout(&ctx->completion,
  153. msecs_to_jiffies(300))) {
  154. dev_err(vdoa->dev,
  155. "Timeout waiting for transfer result\n");
  156. return -ETIMEDOUT;
  157. }
  158. return 0;
  159. }
  160. EXPORT_SYMBOL(vdoa_wait_for_completion);
  161. struct vdoa_ctx *vdoa_context_create(struct vdoa_data *vdoa)
  162. {
  163. struct vdoa_ctx *ctx;
  164. int err;
  165. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  166. if (!ctx)
  167. return NULL;
  168. err = clk_prepare_enable(vdoa->vdoa_clk);
  169. if (err) {
  170. kfree(ctx);
  171. return NULL;
  172. }
  173. init_completion(&ctx->completion);
  174. ctx->vdoa = vdoa;
  175. return ctx;
  176. }
  177. EXPORT_SYMBOL(vdoa_context_create);
  178. void vdoa_context_destroy(struct vdoa_ctx *ctx)
  179. {
  180. struct vdoa_data *vdoa = ctx->vdoa;
  181. clk_disable_unprepare(vdoa->vdoa_clk);
  182. kfree(ctx);
  183. }
  184. EXPORT_SYMBOL(vdoa_context_destroy);
  185. int vdoa_context_configure(struct vdoa_ctx *ctx,
  186. unsigned int width, unsigned int height,
  187. u32 pixelformat)
  188. {
  189. struct vdoa_q_data *src_q_data;
  190. struct vdoa_q_data *dst_q_data;
  191. if (width < 16 || width > 8192 || width % 16 != 0 ||
  192. height < 16 || height > 4096 || height % 16 != 0)
  193. return -EINVAL;
  194. if (pixelformat != V4L2_PIX_FMT_YUYV &&
  195. pixelformat != V4L2_PIX_FMT_NV12)
  196. return -EINVAL;
  197. /* If no context is passed, only check if the format is valid */
  198. if (!ctx)
  199. return 0;
  200. src_q_data = &ctx->q_data[V4L2_M2M_SRC];
  201. dst_q_data = &ctx->q_data[V4L2_M2M_DST];
  202. src_q_data->width = width;
  203. src_q_data->height = height;
  204. src_q_data->bytesperline = width;
  205. src_q_data->sizeimage =
  206. round_up(src_q_data->bytesperline * height, 4096) +
  207. src_q_data->bytesperline * height / 2;
  208. dst_q_data->width = width;
  209. dst_q_data->height = height;
  210. dst_q_data->pixelformat = pixelformat;
  211. switch (pixelformat) {
  212. case V4L2_PIX_FMT_YUYV:
  213. dst_q_data->bytesperline = width * 2;
  214. dst_q_data->sizeimage = dst_q_data->bytesperline * height;
  215. break;
  216. case V4L2_PIX_FMT_NV12:
  217. default:
  218. dst_q_data->bytesperline = width;
  219. dst_q_data->sizeimage =
  220. dst_q_data->bytesperline * height * 3 / 2;
  221. break;
  222. }
  223. return 0;
  224. }
  225. EXPORT_SYMBOL(vdoa_context_configure);
  226. static int vdoa_probe(struct platform_device *pdev)
  227. {
  228. struct vdoa_data *vdoa;
  229. struct resource *res;
  230. dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
  231. vdoa = devm_kzalloc(&pdev->dev, sizeof(*vdoa), GFP_KERNEL);
  232. if (!vdoa)
  233. return -ENOMEM;
  234. vdoa->dev = &pdev->dev;
  235. vdoa->vdoa_clk = devm_clk_get(vdoa->dev, NULL);
  236. if (IS_ERR(vdoa->vdoa_clk)) {
  237. dev_err(vdoa->dev, "Failed to get clock\n");
  238. return PTR_ERR(vdoa->vdoa_clk);
  239. }
  240. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  241. vdoa->regs = devm_ioremap_resource(vdoa->dev, res);
  242. if (IS_ERR(vdoa->regs))
  243. return PTR_ERR(vdoa->regs);
  244. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  245. vdoa->irq = devm_request_threaded_irq(&pdev->dev, res->start, NULL,
  246. vdoa_irq_handler, IRQF_ONESHOT,
  247. "vdoa", vdoa);
  248. if (vdoa->irq < 0) {
  249. dev_err(vdoa->dev, "Failed to get irq\n");
  250. return vdoa->irq;
  251. }
  252. platform_set_drvdata(pdev, vdoa);
  253. return 0;
  254. }
  255. static int vdoa_remove(struct platform_device *pdev)
  256. {
  257. return 0;
  258. }
  259. static const struct of_device_id vdoa_dt_ids[] = {
  260. { .compatible = "fsl,imx6q-vdoa" },
  261. {}
  262. };
  263. MODULE_DEVICE_TABLE(of, vdoa_dt_ids);
  264. static struct platform_driver vdoa_driver = {
  265. .probe = vdoa_probe,
  266. .remove = vdoa_remove,
  267. .driver = {
  268. .name = VDOA_NAME,
  269. .of_match_table = vdoa_dt_ids,
  270. },
  271. };
  272. module_platform_driver(vdoa_driver);
  273. MODULE_DESCRIPTION("Video Data Order Adapter");
  274. MODULE_AUTHOR("Philipp Zabel <philipp.zabel@gmail.com>");
  275. MODULE_ALIAS("platform:imx-vdoa");
  276. MODULE_LICENSE("GPL");