omap_vout_vrfb.c 11 KB

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