omap_vout_vrfb.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /*
  2. * omap_vout_vrfb.c
  3. *
  4. * Copyright (C) 2010 Texas Instruments.
  5. *
  6. * This file is licensed under the terms of the GNU General Public License
  7. * version 2. This program is licensed "as is" without any warranty of any
  8. * kind, whether express or implied.
  9. *
  10. */
  11. #include <linux/sched.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/videodev2.h>
  14. #include <linux/slab.h>
  15. #include <media/videobuf-dma-contig.h>
  16. #include <media/v4l2-device.h>
  17. #include <video/omapvrfb.h>
  18. #include "omap_voutdef.h"
  19. #include "omap_voutlib.h"
  20. #include "omap_vout_vrfb.h"
  21. #define OMAP_DMA_NO_DEVICE 0
  22. /*
  23. * Function for allocating video buffers
  24. */
  25. static int omap_vout_allocate_vrfb_buffers(struct omap_vout_device *vout,
  26. unsigned int *count, int startindex)
  27. {
  28. int i, j;
  29. for (i = 0; i < *count; i++) {
  30. if (!vout->smsshado_virt_addr[i]) {
  31. vout->smsshado_virt_addr[i] =
  32. omap_vout_alloc_buffer(vout->smsshado_size,
  33. &vout->smsshado_phy_addr[i]);
  34. }
  35. if (!vout->smsshado_virt_addr[i] && startindex != -1) {
  36. if (V4L2_MEMORY_MMAP == vout->memory && i >= startindex)
  37. break;
  38. }
  39. if (!vout->smsshado_virt_addr[i]) {
  40. for (j = 0; j < i; j++) {
  41. omap_vout_free_buffer(
  42. vout->smsshado_virt_addr[j],
  43. vout->smsshado_size);
  44. vout->smsshado_virt_addr[j] = 0;
  45. vout->smsshado_phy_addr[j] = 0;
  46. }
  47. *count = 0;
  48. return -ENOMEM;
  49. }
  50. memset((void *) vout->smsshado_virt_addr[i], 0,
  51. vout->smsshado_size);
  52. }
  53. return 0;
  54. }
  55. /*
  56. * Wakes up the application once the DMA transfer to VRFB space is completed.
  57. */
  58. static void omap_vout_vrfb_dma_tx_callback(void *data)
  59. {
  60. struct vid_vrfb_dma *t = (struct vid_vrfb_dma *) data;
  61. t->tx_status = 1;
  62. wake_up_interruptible(&t->wait);
  63. }
  64. /*
  65. * Free VRFB buffers
  66. */
  67. void omap_vout_free_vrfb_buffers(struct omap_vout_device *vout)
  68. {
  69. int j;
  70. for (j = 0; j < VRFB_NUM_BUFS; j++) {
  71. if (vout->smsshado_virt_addr[j]) {
  72. omap_vout_free_buffer(vout->smsshado_virt_addr[j],
  73. vout->smsshado_size);
  74. vout->smsshado_virt_addr[j] = 0;
  75. vout->smsshado_phy_addr[j] = 0;
  76. }
  77. }
  78. }
  79. int omap_vout_setup_vrfb_bufs(struct platform_device *pdev, int vid_num,
  80. bool static_vrfb_allocation)
  81. {
  82. int ret = 0, i, j;
  83. struct omap_vout_device *vout;
  84. struct video_device *vfd;
  85. dma_cap_mask_t mask;
  86. int image_width, image_height;
  87. int vrfb_num_bufs = VRFB_NUM_BUFS;
  88. struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
  89. struct omap2video_device *vid_dev =
  90. container_of(v4l2_dev, struct omap2video_device, v4l2_dev);
  91. vout = vid_dev->vouts[vid_num];
  92. vfd = vout->vfd;
  93. for (i = 0; i < VRFB_NUM_BUFS; i++) {
  94. if (omap_vrfb_request_ctx(&vout->vrfb_context[i])) {
  95. dev_info(&pdev->dev, ": VRFB allocation failed\n");
  96. for (j = 0; j < i; j++)
  97. omap_vrfb_release_ctx(&vout->vrfb_context[j]);
  98. ret = -ENOMEM;
  99. goto free_buffers;
  100. }
  101. }
  102. /* Calculate VRFB memory size */
  103. /* allocate for worst case size */
  104. image_width = VID_MAX_WIDTH / TILE_SIZE;
  105. if (VID_MAX_WIDTH % TILE_SIZE)
  106. image_width++;
  107. image_width = image_width * TILE_SIZE;
  108. image_height = VID_MAX_HEIGHT / TILE_SIZE;
  109. if (VID_MAX_HEIGHT % TILE_SIZE)
  110. image_height++;
  111. image_height = image_height * TILE_SIZE;
  112. vout->smsshado_size = PAGE_ALIGN(image_width * image_height * 2 * 2);
  113. /*
  114. * Request and Initialize DMA, for DMA based VRFB transfer
  115. */
  116. dma_cap_zero(mask);
  117. dma_cap_set(DMA_INTERLEAVE, mask);
  118. vout->vrfb_dma_tx.chan = dma_request_chan_by_mask(&mask);
  119. if (IS_ERR(vout->vrfb_dma_tx.chan)) {
  120. vout->vrfb_dma_tx.req_status = DMA_CHAN_NOT_ALLOTED;
  121. } else {
  122. size_t xt_size = sizeof(struct dma_interleaved_template) +
  123. sizeof(struct data_chunk);
  124. vout->vrfb_dma_tx.xt = kzalloc(xt_size, GFP_KERNEL);
  125. if (!vout->vrfb_dma_tx.xt) {
  126. dma_release_channel(vout->vrfb_dma_tx.chan);
  127. vout->vrfb_dma_tx.req_status = DMA_CHAN_NOT_ALLOTED;
  128. }
  129. }
  130. if (vout->vrfb_dma_tx.req_status == DMA_CHAN_NOT_ALLOTED)
  131. dev_info(&pdev->dev,
  132. ": failed to allocate DMA Channel for video%d\n",
  133. vfd->minor);
  134. init_waitqueue_head(&vout->vrfb_dma_tx.wait);
  135. /* statically allocated the VRFB buffer is done through
  136. commands line aruments */
  137. if (static_vrfb_allocation) {
  138. if (omap_vout_allocate_vrfb_buffers(vout, &vrfb_num_bufs, -1)) {
  139. ret = -ENOMEM;
  140. goto release_vrfb_ctx;
  141. }
  142. vout->vrfb_static_allocation = true;
  143. }
  144. return 0;
  145. release_vrfb_ctx:
  146. for (j = 0; j < VRFB_NUM_BUFS; j++)
  147. omap_vrfb_release_ctx(&vout->vrfb_context[j]);
  148. free_buffers:
  149. omap_vout_free_buffers(vout);
  150. return ret;
  151. }
  152. /*
  153. * Release the VRFB context once the module exits
  154. */
  155. void omap_vout_release_vrfb(struct omap_vout_device *vout)
  156. {
  157. int i;
  158. for (i = 0; i < VRFB_NUM_BUFS; i++)
  159. omap_vrfb_release_ctx(&vout->vrfb_context[i]);
  160. if (vout->vrfb_dma_tx.req_status == DMA_CHAN_ALLOTED) {
  161. vout->vrfb_dma_tx.req_status = DMA_CHAN_NOT_ALLOTED;
  162. kfree(vout->vrfb_dma_tx.xt);
  163. dmaengine_terminate_sync(vout->vrfb_dma_tx.chan);
  164. dma_release_channel(vout->vrfb_dma_tx.chan);
  165. }
  166. }
  167. /*
  168. * Allocate the buffers for the VRFB space. Data is copied from V4L2
  169. * buffers to the VRFB buffers using the DMA engine.
  170. */
  171. int omap_vout_vrfb_buffer_setup(struct omap_vout_device *vout,
  172. unsigned int *count, unsigned int startindex)
  173. {
  174. int i;
  175. bool yuv_mode;
  176. if (!is_rotation_enabled(vout))
  177. return 0;
  178. /* If rotation is enabled, allocate memory for VRFB space also */
  179. *count = *count > VRFB_NUM_BUFS ? VRFB_NUM_BUFS : *count;
  180. /* Allocate the VRFB buffers only if the buffers are not
  181. * allocated during init time.
  182. */
  183. if (!vout->vrfb_static_allocation)
  184. if (omap_vout_allocate_vrfb_buffers(vout, count, startindex))
  185. return -ENOMEM;
  186. if (vout->dss_mode == OMAP_DSS_COLOR_YUV2 ||
  187. vout->dss_mode == OMAP_DSS_COLOR_UYVY)
  188. yuv_mode = true;
  189. else
  190. yuv_mode = false;
  191. for (i = 0; i < *count; i++)
  192. omap_vrfb_setup(&vout->vrfb_context[i],
  193. vout->smsshado_phy_addr[i], vout->pix.width,
  194. vout->pix.height, vout->bpp, yuv_mode);
  195. return 0;
  196. }
  197. int omap_vout_prepare_vrfb(struct omap_vout_device *vout,
  198. struct videobuf_buffer *vb)
  199. {
  200. struct dma_async_tx_descriptor *tx;
  201. enum dma_ctrl_flags flags = DMA_PREP_INTERRUPT | DMA_CTRL_ACK;
  202. struct dma_chan *chan = vout->vrfb_dma_tx.chan;
  203. struct dma_device *dmadev = chan->device;
  204. struct dma_interleaved_template *xt = vout->vrfb_dma_tx.xt;
  205. dma_cookie_t cookie;
  206. enum dma_status status;
  207. enum dss_rotation rotation;
  208. size_t dst_icg;
  209. u32 pixsize;
  210. if (!is_rotation_enabled(vout))
  211. return 0;
  212. /* If rotation is enabled, copy input buffer into VRFB
  213. * memory space using DMA. We are copying input buffer
  214. * into VRFB memory space of desired angle and DSS will
  215. * read image VRFB memory for 0 degree angle
  216. */
  217. pixsize = vout->bpp * vout->vrfb_bpp;
  218. dst_icg = ((MAX_PIXELS_PER_LINE * pixsize) -
  219. (vout->pix.width * vout->bpp)) + 1;
  220. xt->src_start = vout->buf_phy_addr[vb->i];
  221. xt->dst_start = vout->vrfb_context[vb->i].paddr[0];
  222. xt->numf = vout->pix.height;
  223. xt->frame_size = 1;
  224. xt->sgl[0].size = vout->pix.width * vout->bpp;
  225. xt->sgl[0].icg = dst_icg;
  226. xt->dir = DMA_MEM_TO_MEM;
  227. xt->src_sgl = false;
  228. xt->src_inc = true;
  229. xt->dst_sgl = true;
  230. xt->dst_inc = true;
  231. tx = dmadev->device_prep_interleaved_dma(chan, xt, flags);
  232. if (tx == NULL) {
  233. pr_err("%s: DMA interleaved prep error\n", __func__);
  234. return -EINVAL;
  235. }
  236. tx->callback = omap_vout_vrfb_dma_tx_callback;
  237. tx->callback_param = &vout->vrfb_dma_tx;
  238. cookie = dmaengine_submit(tx);
  239. if (dma_submit_error(cookie)) {
  240. pr_err("%s: dmaengine_submit failed (%d)\n", __func__, cookie);
  241. return -EINVAL;
  242. }
  243. vout->vrfb_dma_tx.tx_status = 0;
  244. dma_async_issue_pending(chan);
  245. wait_event_interruptible_timeout(vout->vrfb_dma_tx.wait,
  246. vout->vrfb_dma_tx.tx_status == 1,
  247. VRFB_TX_TIMEOUT);
  248. status = dma_async_is_tx_complete(chan, cookie, NULL, NULL);
  249. if (vout->vrfb_dma_tx.tx_status == 0) {
  250. pr_err("%s: Timeout while waiting for DMA\n", __func__);
  251. dmaengine_terminate_sync(chan);
  252. return -EINVAL;
  253. } else if (status != DMA_COMPLETE) {
  254. pr_err("%s: DMA completion %s status\n", __func__,
  255. status == DMA_ERROR ? "error" : "busy");
  256. dmaengine_terminate_sync(chan);
  257. return -EINVAL;
  258. }
  259. /* Store buffers physical address into an array. Addresses
  260. * from this array will be used to configure DSS */
  261. rotation = calc_rotation(vout);
  262. vout->queued_buf_addr[vb->i] = (u8 *)
  263. vout->vrfb_context[vb->i].paddr[rotation];
  264. return 0;
  265. }
  266. /*
  267. * Calculate the buffer offsets from which the streaming should
  268. * start. This offset calculation is mainly required because of
  269. * the VRFB 32 pixels alignment with rotation.
  270. */
  271. void omap_vout_calculate_vrfb_offset(struct omap_vout_device *vout)
  272. {
  273. enum dss_rotation rotation;
  274. bool mirroring = vout->mirror;
  275. struct v4l2_rect *crop = &vout->crop;
  276. struct v4l2_pix_format *pix = &vout->pix;
  277. int *cropped_offset = &vout->cropped_offset;
  278. int vr_ps = 1, ps = 2, temp_ps = 2;
  279. int offset = 0, ctop = 0, cleft = 0, line_length = 0;
  280. rotation = calc_rotation(vout);
  281. if (V4L2_PIX_FMT_YUYV == pix->pixelformat ||
  282. V4L2_PIX_FMT_UYVY == pix->pixelformat) {
  283. if (is_rotation_enabled(vout)) {
  284. /*
  285. * ps - Actual pixel size for YUYV/UYVY for
  286. * VRFB/Mirroring is 4 bytes
  287. * vr_ps - Virtually pixel size for YUYV/UYVY is
  288. * 2 bytes
  289. */
  290. ps = 4;
  291. vr_ps = 2;
  292. } else {
  293. ps = 2; /* otherwise the pixel size is 2 byte */
  294. }
  295. } else if (V4L2_PIX_FMT_RGB32 == pix->pixelformat) {
  296. ps = 4;
  297. } else if (V4L2_PIX_FMT_RGB24 == pix->pixelformat) {
  298. ps = 3;
  299. }
  300. vout->ps = ps;
  301. vout->vr_ps = vr_ps;
  302. if (is_rotation_enabled(vout)) {
  303. line_length = MAX_PIXELS_PER_LINE;
  304. ctop = (pix->height - crop->height) - crop->top;
  305. cleft = (pix->width - crop->width) - crop->left;
  306. } else {
  307. line_length = pix->width;
  308. }
  309. vout->line_length = line_length;
  310. switch (rotation) {
  311. case dss_rotation_90_degree:
  312. offset = vout->vrfb_context[0].yoffset *
  313. vout->vrfb_context[0].bytespp;
  314. temp_ps = ps / vr_ps;
  315. if (!mirroring) {
  316. *cropped_offset = offset + line_length *
  317. temp_ps * cleft + crop->top * temp_ps;
  318. } else {
  319. *cropped_offset = offset + line_length * temp_ps *
  320. cleft + crop->top * temp_ps + (line_length *
  321. ((crop->width / (vr_ps)) - 1) * ps);
  322. }
  323. break;
  324. case dss_rotation_180_degree:
  325. offset = ((MAX_PIXELS_PER_LINE * vout->vrfb_context[0].yoffset *
  326. vout->vrfb_context[0].bytespp) +
  327. (vout->vrfb_context[0].xoffset *
  328. vout->vrfb_context[0].bytespp));
  329. if (!mirroring) {
  330. *cropped_offset = offset + (line_length * ps * ctop) +
  331. (cleft / vr_ps) * ps;
  332. } else {
  333. *cropped_offset = offset + (line_length * ps * ctop) +
  334. (cleft / vr_ps) * ps + (line_length *
  335. (crop->height - 1) * ps);
  336. }
  337. break;
  338. case dss_rotation_270_degree:
  339. offset = MAX_PIXELS_PER_LINE * vout->vrfb_context[0].xoffset *
  340. vout->vrfb_context[0].bytespp;
  341. temp_ps = ps / vr_ps;
  342. if (!mirroring) {
  343. *cropped_offset = offset + line_length *
  344. temp_ps * crop->left + ctop * ps;
  345. } else {
  346. *cropped_offset = offset + line_length *
  347. temp_ps * crop->left + ctop * ps +
  348. (line_length * ((crop->width / vr_ps) - 1) *
  349. ps);
  350. }
  351. break;
  352. case dss_rotation_0_degree:
  353. if (!mirroring) {
  354. *cropped_offset = (line_length * ps) *
  355. crop->top + (crop->left / vr_ps) * ps;
  356. } else {
  357. *cropped_offset = (line_length * ps) *
  358. crop->top + (crop->left / vr_ps) * ps +
  359. (line_length * (crop->height - 1) * ps);
  360. }
  361. break;
  362. default:
  363. *cropped_offset = (line_length * ps * crop->top) /
  364. vr_ps + (crop->left * ps) / vr_ps +
  365. ((crop->width / vr_ps) - 1) * ps;
  366. break;
  367. }
  368. }