imx-vdoa.c 8.2 KB

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