omap_vout_vrfb.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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 <media/videobuf-dma-contig.h>
  15. #include <media/v4l2-device.h>
  16. #include <plat/dma.h>
  17. #include <plat/vrfb.h>
  18. #include "omap_voutdef.h"
  19. #include "omap_voutlib.h"
  20. /*
  21. * Function for allocating video buffers
  22. */
  23. static int omap_vout_allocate_vrfb_buffers(struct omap_vout_device *vout,
  24. unsigned int *count, int startindex)
  25. {
  26. int i, j;
  27. for (i = 0; i < *count; i++) {
  28. if (!vout->smsshado_virt_addr[i]) {
  29. vout->smsshado_virt_addr[i] =
  30. omap_vout_alloc_buffer(vout->smsshado_size,
  31. &vout->smsshado_phy_addr[i]);
  32. }
  33. if (!vout->smsshado_virt_addr[i] && startindex != -1) {
  34. if (V4L2_MEMORY_MMAP == vout->memory && i >= startindex)
  35. break;
  36. }
  37. if (!vout->smsshado_virt_addr[i]) {
  38. for (j = 0; j < i; j++) {
  39. omap_vout_free_buffer(
  40. vout->smsshado_virt_addr[j],
  41. vout->smsshado_size);
  42. vout->smsshado_virt_addr[j] = 0;
  43. vout->smsshado_phy_addr[j] = 0;
  44. }
  45. *count = 0;
  46. return -ENOMEM;
  47. }
  48. memset((void *) vout->smsshado_virt_addr[i], 0,
  49. vout->smsshado_size);
  50. }
  51. return 0;
  52. }
  53. /*
  54. * Wakes up the application once the DMA transfer to VRFB space is completed.
  55. */
  56. static void omap_vout_vrfb_dma_tx_callback(int lch, u16 ch_status, void *data)
  57. {
  58. struct vid_vrfb_dma *t = (struct vid_vrfb_dma *) data;
  59. t->tx_status = 1;
  60. wake_up_interruptible(&t->wait);
  61. }
  62. /*
  63. * Free VRFB buffers
  64. */
  65. void omap_vout_free_vrfb_buffers(struct omap_vout_device *vout)
  66. {
  67. int j;
  68. for (j = 0; j < VRFB_NUM_BUFS; j++) {
  69. omap_vout_free_buffer(vout->smsshado_virt_addr[j],
  70. vout->smsshado_size);
  71. vout->smsshado_virt_addr[j] = 0;
  72. vout->smsshado_phy_addr[j] = 0;
  73. }
  74. }
  75. int omap_vout_setup_vrfb_bufs(struct platform_device *pdev, int vid_num,
  76. bool static_vrfb_allocation)
  77. {
  78. int ret = 0, i, j;
  79. struct omap_vout_device *vout;
  80. struct video_device *vfd;
  81. int image_width, image_height;
  82. int vrfb_num_bufs = VRFB_NUM_BUFS;
  83. struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
  84. struct omap2video_device *vid_dev =
  85. container_of(v4l2_dev, struct omap2video_device, v4l2_dev);
  86. vout = vid_dev->vouts[vid_num];
  87. vfd = vout->vfd;
  88. for (i = 0; i < VRFB_NUM_BUFS; i++) {
  89. if (omap_vrfb_request_ctx(&vout->vrfb_context[i])) {
  90. dev_info(&pdev->dev, ": VRFB allocation failed\n");
  91. for (j = 0; j < i; j++)
  92. omap_vrfb_release_ctx(&vout->vrfb_context[j]);
  93. ret = -ENOMEM;
  94. goto free_buffers;
  95. }
  96. }
  97. /* Calculate VRFB memory size */
  98. /* allocate for worst case size */
  99. image_width = VID_MAX_WIDTH / TILE_SIZE;
  100. if (VID_MAX_WIDTH % TILE_SIZE)
  101. image_width++;
  102. image_width = image_width * TILE_SIZE;
  103. image_height = VID_MAX_HEIGHT / TILE_SIZE;
  104. if (VID_MAX_HEIGHT % TILE_SIZE)
  105. image_height++;
  106. image_height = image_height * TILE_SIZE;
  107. vout->smsshado_size = PAGE_ALIGN(image_width * image_height * 2 * 2);
  108. /*
  109. * Request and Initialize DMA, for DMA based VRFB transfer
  110. */
  111. vout->vrfb_dma_tx.dev_id = OMAP_DMA_NO_DEVICE;
  112. vout->vrfb_dma_tx.dma_ch = -1;
  113. vout->vrfb_dma_tx.req_status = DMA_CHAN_ALLOTED;
  114. ret = omap_request_dma(vout->vrfb_dma_tx.dev_id, "VRFB DMA TX",
  115. omap_vout_vrfb_dma_tx_callback,
  116. (void *) &vout->vrfb_dma_tx, &vout->vrfb_dma_tx.dma_ch);
  117. if (ret < 0) {
  118. vout->vrfb_dma_tx.req_status = DMA_CHAN_NOT_ALLOTED;
  119. dev_info(&pdev->dev, ": failed to allocate DMA Channel for"
  120. " video%d\n", vfd->minor);
  121. }
  122. init_waitqueue_head(&vout->vrfb_dma_tx.wait);
  123. /* statically allocated the VRFB buffer is done through
  124. commands line aruments */
  125. if (static_vrfb_allocation) {
  126. if (omap_vout_allocate_vrfb_buffers(vout, &vrfb_num_bufs, -1)) {
  127. ret = -ENOMEM;
  128. goto release_vrfb_ctx;
  129. }
  130. vout->vrfb_static_allocation = 1;
  131. }
  132. return 0;
  133. release_vrfb_ctx:
  134. for (j = 0; j < VRFB_NUM_BUFS; j++)
  135. omap_vrfb_release_ctx(&vout->vrfb_context[j]);
  136. free_buffers:
  137. omap_vout_free_buffers(vout);
  138. return ret;
  139. }
  140. /*
  141. * Release the VRFB context once the module exits
  142. */
  143. void omap_vout_release_vrfb(struct omap_vout_device *vout)
  144. {
  145. int i;
  146. for (i = 0; i < VRFB_NUM_BUFS; i++)
  147. omap_vrfb_release_ctx(&vout->vrfb_context[i]);
  148. if (vout->vrfb_dma_tx.req_status == DMA_CHAN_ALLOTED) {
  149. vout->vrfb_dma_tx.req_status = DMA_CHAN_NOT_ALLOTED;
  150. omap_free_dma(vout->vrfb_dma_tx.dma_ch);
  151. }
  152. }
  153. /*
  154. * Allocate the buffers for the VRFB space. Data is copied from V4L2
  155. * buffers to the VRFB buffers using the DMA engine.
  156. */
  157. int omap_vout_vrfb_buffer_setup(struct omap_vout_device *vout,
  158. unsigned int *count, unsigned int startindex)
  159. {
  160. int i;
  161. bool yuv_mode;
  162. if (!is_rotation_enabled(vout))
  163. return 0;
  164. /* If rotation is enabled, allocate memory for VRFB space also */
  165. *count = *count > VRFB_NUM_BUFS ? VRFB_NUM_BUFS : *count;
  166. /* Allocate the VRFB buffers only if the buffers are not
  167. * allocated during init time.
  168. */
  169. if (!vout->vrfb_static_allocation)
  170. if (omap_vout_allocate_vrfb_buffers(vout, count, startindex))
  171. return -ENOMEM;
  172. if (vout->dss_mode == OMAP_DSS_COLOR_YUV2 ||
  173. vout->dss_mode == OMAP_DSS_COLOR_UYVY)
  174. yuv_mode = true;
  175. else
  176. yuv_mode = false;
  177. for (i = 0; i < *count; i++)
  178. omap_vrfb_setup(&vout->vrfb_context[i],
  179. vout->smsshado_phy_addr[i], vout->pix.width,
  180. vout->pix.height, vout->bpp, yuv_mode);
  181. return 0;
  182. }
  183. int omap_vout_prepare_vrfb(struct omap_vout_device *vout,
  184. struct videobuf_buffer *vb)
  185. {
  186. dma_addr_t dmabuf;
  187. struct vid_vrfb_dma *tx;
  188. enum dss_rotation rotation;
  189. u32 dest_frame_index = 0, src_element_index = 0;
  190. u32 dest_element_index = 0, src_frame_index = 0;
  191. u32 elem_count = 0, frame_count = 0, pixsize = 2;
  192. if (!is_rotation_enabled(vout))
  193. return 0;
  194. dmabuf = vout->buf_phy_addr[vb->i];
  195. /* If rotation is enabled, copy input buffer into VRFB
  196. * memory space using DMA. We are copying input buffer
  197. * into VRFB memory space of desired angle and DSS will
  198. * read image VRFB memory for 0 degree angle
  199. */
  200. pixsize = vout->bpp * vout->vrfb_bpp;
  201. /*
  202. * DMA transfer in double index mode
  203. */
  204. /* Frame index */
  205. dest_frame_index = ((MAX_PIXELS_PER_LINE * pixsize) -
  206. (vout->pix.width * vout->bpp)) + 1;
  207. /* Source and destination parameters */
  208. src_element_index = 0;
  209. src_frame_index = 0;
  210. dest_element_index = 1;
  211. /* Number of elements per frame */
  212. elem_count = vout->pix.width * vout->bpp;
  213. frame_count = vout->pix.height;
  214. tx = &vout->vrfb_dma_tx;
  215. tx->tx_status = 0;
  216. omap_set_dma_transfer_params(tx->dma_ch, OMAP_DMA_DATA_TYPE_S32,
  217. (elem_count / 4), frame_count, OMAP_DMA_SYNC_ELEMENT,
  218. tx->dev_id, 0x0);
  219. /* src_port required only for OMAP1 */
  220. omap_set_dma_src_params(tx->dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
  221. dmabuf, src_element_index, src_frame_index);
  222. /*set dma source burst mode for VRFB */
  223. omap_set_dma_src_burst_mode(tx->dma_ch, OMAP_DMA_DATA_BURST_16);
  224. rotation = calc_rotation(vout);
  225. /* dest_port required only for OMAP1 */
  226. omap_set_dma_dest_params(tx->dma_ch, 0, OMAP_DMA_AMODE_DOUBLE_IDX,
  227. vout->vrfb_context[vb->i].paddr[0], dest_element_index,
  228. dest_frame_index);
  229. /*set dma dest burst mode for VRFB */
  230. omap_set_dma_dest_burst_mode(tx->dma_ch, OMAP_DMA_DATA_BURST_16);
  231. omap_dma_set_global_params(DMA_DEFAULT_ARB_RATE, 0x20, 0);
  232. omap_start_dma(tx->dma_ch);
  233. interruptible_sleep_on_timeout(&tx->wait, VRFB_TX_TIMEOUT);
  234. if (tx->tx_status == 0) {
  235. omap_stop_dma(tx->dma_ch);
  236. return -EINVAL;
  237. }
  238. /* Store buffers physical address into an array. Addresses
  239. * from this array will be used to configure DSS */
  240. vout->queued_buf_addr[vb->i] = (u8 *)
  241. vout->vrfb_context[vb->i].paddr[rotation];
  242. return 0;
  243. }
  244. /*
  245. * Calculate the buffer offsets from which the streaming should
  246. * start. This offset calculation is mainly required because of
  247. * the VRFB 32 pixels alignment with rotation.
  248. */
  249. void omap_vout_calculate_vrfb_offset(struct omap_vout_device *vout)
  250. {
  251. enum dss_rotation rotation;
  252. bool mirroring = vout->mirror;
  253. struct v4l2_rect *crop = &vout->crop;
  254. struct v4l2_pix_format *pix = &vout->pix;
  255. int *cropped_offset = &vout->cropped_offset;
  256. int vr_ps = 1, ps = 2, temp_ps = 2;
  257. int offset = 0, ctop = 0, cleft = 0, line_length = 0;
  258. rotation = calc_rotation(vout);
  259. if (V4L2_PIX_FMT_YUYV == pix->pixelformat ||
  260. V4L2_PIX_FMT_UYVY == pix->pixelformat) {
  261. if (is_rotation_enabled(vout)) {
  262. /*
  263. * ps - Actual pixel size for YUYV/UYVY for
  264. * VRFB/Mirroring is 4 bytes
  265. * vr_ps - Virtually pixel size for YUYV/UYVY is
  266. * 2 bytes
  267. */
  268. ps = 4;
  269. vr_ps = 2;
  270. } else {
  271. ps = 2; /* otherwise the pixel size is 2 byte */
  272. }
  273. } else if (V4L2_PIX_FMT_RGB32 == pix->pixelformat) {
  274. ps = 4;
  275. } else if (V4L2_PIX_FMT_RGB24 == pix->pixelformat) {
  276. ps = 3;
  277. }
  278. vout->ps = ps;
  279. vout->vr_ps = vr_ps;
  280. if (is_rotation_enabled(vout)) {
  281. line_length = MAX_PIXELS_PER_LINE;
  282. ctop = (pix->height - crop->height) - crop->top;
  283. cleft = (pix->width - crop->width) - crop->left;
  284. } else {
  285. line_length = pix->width;
  286. }
  287. vout->line_length = line_length;
  288. switch (rotation) {
  289. case dss_rotation_90_degree:
  290. offset = vout->vrfb_context[0].yoffset *
  291. vout->vrfb_context[0].bytespp;
  292. temp_ps = ps / vr_ps;
  293. if (mirroring == 0) {
  294. *cropped_offset = offset + line_length *
  295. temp_ps * cleft + crop->top * temp_ps;
  296. } else {
  297. *cropped_offset = offset + line_length * temp_ps *
  298. cleft + crop->top * temp_ps + (line_length *
  299. ((crop->width / (vr_ps)) - 1) * ps);
  300. }
  301. break;
  302. case dss_rotation_180_degree:
  303. offset = ((MAX_PIXELS_PER_LINE * vout->vrfb_context[0].yoffset *
  304. vout->vrfb_context[0].bytespp) +
  305. (vout->vrfb_context[0].xoffset *
  306. vout->vrfb_context[0].bytespp));
  307. if (mirroring == 0) {
  308. *cropped_offset = offset + (line_length * ps * ctop) +
  309. (cleft / vr_ps) * ps;
  310. } else {
  311. *cropped_offset = offset + (line_length * ps * ctop) +
  312. (cleft / vr_ps) * ps + (line_length *
  313. (crop->height - 1) * ps);
  314. }
  315. break;
  316. case dss_rotation_270_degree:
  317. offset = MAX_PIXELS_PER_LINE * vout->vrfb_context[0].xoffset *
  318. vout->vrfb_context[0].bytespp;
  319. temp_ps = ps / vr_ps;
  320. if (mirroring == 0) {
  321. *cropped_offset = offset + line_length *
  322. temp_ps * crop->left + ctop * ps;
  323. } else {
  324. *cropped_offset = offset + line_length *
  325. temp_ps * crop->left + ctop * ps +
  326. (line_length * ((crop->width / vr_ps) - 1) *
  327. ps);
  328. }
  329. break;
  330. case dss_rotation_0_degree:
  331. if (mirroring == 0) {
  332. *cropped_offset = (line_length * ps) *
  333. crop->top + (crop->left / vr_ps) * ps;
  334. } else {
  335. *cropped_offset = (line_length * ps) *
  336. crop->top + (crop->left / vr_ps) * ps +
  337. (line_length * (crop->height - 1) * ps);
  338. }
  339. break;
  340. default:
  341. *cropped_offset = (line_length * ps * crop->top) /
  342. vr_ps + (crop->left * ps) / vr_ps +
  343. ((crop->width / vr_ps) - 1) * ps;
  344. break;
  345. }
  346. }