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