omap_vout_vrfb.c 11 KB

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