omap_vout_vrfb.c 11 KB

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