omap_voutlib.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * omap_voutlib.c
  3. *
  4. * Copyright (C) 2005-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. * Based on the OMAP2 camera driver
  11. * Video-for-Linux (Version 2) camera capture driver for
  12. * the OMAP24xx camera controller.
  13. *
  14. * Author: Andy Lowe (source@mvista.com)
  15. *
  16. * Copyright (C) 2004 MontaVista Software, Inc.
  17. * Copyright (C) 2010 Texas Instruments.
  18. *
  19. */
  20. #include <linux/module.h>
  21. #include <linux/errno.h>
  22. #include <linux/kernel.h>
  23. #include <linux/types.h>
  24. #include <linux/videodev2.h>
  25. #include <linux/dma-mapping.h>
  26. #include <video/omapdss.h>
  27. #include "omap_voutlib.h"
  28. MODULE_AUTHOR("Texas Instruments");
  29. MODULE_DESCRIPTION("OMAP Video library");
  30. MODULE_LICENSE("GPL");
  31. /* Return the default overlay cropping rectangle in crop given the image
  32. * size in pix and the video display size in fbuf. The default
  33. * cropping rectangle is the largest rectangle no larger than the capture size
  34. * that will fit on the display. The default cropping rectangle is centered in
  35. * the image. All dimensions and offsets are rounded down to even numbers.
  36. */
  37. void omap_vout_default_crop(struct v4l2_pix_format *pix,
  38. struct v4l2_framebuffer *fbuf, struct v4l2_rect *crop)
  39. {
  40. crop->width = (pix->width < fbuf->fmt.width) ?
  41. pix->width : fbuf->fmt.width;
  42. crop->height = (pix->height < fbuf->fmt.height) ?
  43. pix->height : fbuf->fmt.height;
  44. crop->width &= ~1;
  45. crop->height &= ~1;
  46. crop->left = ((pix->width - crop->width) >> 1) & ~1;
  47. crop->top = ((pix->height - crop->height) >> 1) & ~1;
  48. }
  49. EXPORT_SYMBOL_GPL(omap_vout_default_crop);
  50. /* Given a new render window in new_win, adjust the window to the
  51. * nearest supported configuration. The adjusted window parameters are
  52. * returned in new_win.
  53. * Returns zero if successful, or -EINVAL if the requested window is
  54. * impossible and cannot reasonably be adjusted.
  55. */
  56. int omap_vout_try_window(struct v4l2_framebuffer *fbuf,
  57. struct v4l2_window *new_win)
  58. {
  59. struct v4l2_rect try_win;
  60. /* make a working copy of the new_win rectangle */
  61. try_win = new_win->w;
  62. /* adjust the preview window so it fits on the display by clipping any
  63. * offscreen areas
  64. */
  65. if (try_win.left < 0) {
  66. try_win.width += try_win.left;
  67. try_win.left = 0;
  68. }
  69. if (try_win.top < 0) {
  70. try_win.height += try_win.top;
  71. try_win.top = 0;
  72. }
  73. try_win.width = (try_win.width < fbuf->fmt.width) ?
  74. try_win.width : fbuf->fmt.width;
  75. try_win.height = (try_win.height < fbuf->fmt.height) ?
  76. try_win.height : fbuf->fmt.height;
  77. if (try_win.left + try_win.width > fbuf->fmt.width)
  78. try_win.width = fbuf->fmt.width - try_win.left;
  79. if (try_win.top + try_win.height > fbuf->fmt.height)
  80. try_win.height = fbuf->fmt.height - try_win.top;
  81. try_win.width &= ~1;
  82. try_win.height &= ~1;
  83. if (try_win.width <= 0 || try_win.height <= 0)
  84. return -EINVAL;
  85. /* We now have a valid preview window, so go with it */
  86. new_win->w = try_win;
  87. new_win->field = V4L2_FIELD_ANY;
  88. return 0;
  89. }
  90. EXPORT_SYMBOL_GPL(omap_vout_try_window);
  91. /* Given a new render window in new_win, adjust the window to the
  92. * nearest supported configuration. The image cropping window in crop
  93. * will also be adjusted if necessary. Preference is given to keeping the
  94. * the window as close to the requested configuration as possible. If
  95. * successful, new_win, vout->win, and crop are updated.
  96. * Returns zero if successful, or -EINVAL if the requested preview window is
  97. * impossible and cannot reasonably be adjusted.
  98. */
  99. int omap_vout_new_window(struct v4l2_rect *crop,
  100. struct v4l2_window *win, struct v4l2_framebuffer *fbuf,
  101. struct v4l2_window *new_win)
  102. {
  103. int err;
  104. err = omap_vout_try_window(fbuf, new_win);
  105. if (err)
  106. return err;
  107. /* update our preview window */
  108. win->w = new_win->w;
  109. win->field = new_win->field;
  110. win->chromakey = new_win->chromakey;
  111. /* Adjust the cropping window to allow for resizing limitation */
  112. if (omap_vout_dss_omap24xx()) {
  113. /* For 24xx limit is 8x to 1/2x scaling. */
  114. if ((crop->height/win->w.height) >= 2)
  115. crop->height = win->w.height * 2;
  116. if ((crop->width/win->w.width) >= 2)
  117. crop->width = win->w.width * 2;
  118. if (crop->width > 768) {
  119. /* The OMAP2420 vertical resizing line buffer is 768
  120. * pixels wide. If the cropped image is wider than
  121. * 768 pixels then it cannot be vertically resized.
  122. */
  123. if (crop->height != win->w.height)
  124. crop->width = 768;
  125. }
  126. } else if (omap_vout_dss_omap34xx()) {
  127. /* For 34xx limit is 8x to 1/4x scaling. */
  128. if ((crop->height/win->w.height) >= 4)
  129. crop->height = win->w.height * 4;
  130. if ((crop->width/win->w.width) >= 4)
  131. crop->width = win->w.width * 4;
  132. }
  133. return 0;
  134. }
  135. EXPORT_SYMBOL_GPL(omap_vout_new_window);
  136. /* Given a new cropping rectangle in new_crop, adjust the cropping rectangle to
  137. * the nearest supported configuration. The image render window in win will
  138. * also be adjusted if necessary. The preview window is adjusted such that the
  139. * horizontal and vertical rescaling ratios stay constant. If the render
  140. * window would fall outside the display boundaries, the cropping rectangle
  141. * will also be adjusted to maintain the rescaling ratios. If successful, crop
  142. * and win are updated.
  143. * Returns zero if successful, or -EINVAL if the requested cropping rectangle is
  144. * impossible and cannot reasonably be adjusted.
  145. */
  146. int omap_vout_new_crop(struct v4l2_pix_format *pix,
  147. struct v4l2_rect *crop, struct v4l2_window *win,
  148. struct v4l2_framebuffer *fbuf, const struct v4l2_rect *new_crop)
  149. {
  150. struct v4l2_rect try_crop;
  151. unsigned long vresize, hresize;
  152. /* make a working copy of the new_crop rectangle */
  153. try_crop = *new_crop;
  154. /* adjust the cropping rectangle so it fits in the image */
  155. if (try_crop.left < 0) {
  156. try_crop.width += try_crop.left;
  157. try_crop.left = 0;
  158. }
  159. if (try_crop.top < 0) {
  160. try_crop.height += try_crop.top;
  161. try_crop.top = 0;
  162. }
  163. try_crop.width = (try_crop.width < pix->width) ?
  164. try_crop.width : pix->width;
  165. try_crop.height = (try_crop.height < pix->height) ?
  166. try_crop.height : pix->height;
  167. if (try_crop.left + try_crop.width > pix->width)
  168. try_crop.width = pix->width - try_crop.left;
  169. if (try_crop.top + try_crop.height > pix->height)
  170. try_crop.height = pix->height - try_crop.top;
  171. try_crop.width &= ~1;
  172. try_crop.height &= ~1;
  173. if (try_crop.width <= 0 || try_crop.height <= 0)
  174. return -EINVAL;
  175. if (omap_vout_dss_omap24xx()) {
  176. if (try_crop.height != win->w.height) {
  177. /* If we're resizing vertically, we can't support a
  178. * crop width wider than 768 pixels.
  179. */
  180. if (try_crop.width > 768)
  181. try_crop.width = 768;
  182. }
  183. }
  184. /* vertical resizing */
  185. vresize = (1024 * try_crop.height) / win->w.height;
  186. if (omap_vout_dss_omap24xx() && (vresize > 2048))
  187. vresize = 2048;
  188. else if (omap_vout_dss_omap34xx() && (vresize > 4096))
  189. vresize = 4096;
  190. win->w.height = ((1024 * try_crop.height) / vresize) & ~1;
  191. if (win->w.height == 0)
  192. win->w.height = 2;
  193. if (win->w.height + win->w.top > fbuf->fmt.height) {
  194. /* We made the preview window extend below the bottom of the
  195. * display, so clip it to the display boundary and resize the
  196. * cropping height to maintain the vertical resizing ratio.
  197. */
  198. win->w.height = (fbuf->fmt.height - win->w.top) & ~1;
  199. if (try_crop.height == 0)
  200. try_crop.height = 2;
  201. }
  202. /* horizontal resizing */
  203. hresize = (1024 * try_crop.width) / win->w.width;
  204. if (omap_vout_dss_omap24xx() && (hresize > 2048))
  205. hresize = 2048;
  206. else if (omap_vout_dss_omap34xx() && (hresize > 4096))
  207. hresize = 4096;
  208. win->w.width = ((1024 * try_crop.width) / hresize) & ~1;
  209. if (win->w.width == 0)
  210. win->w.width = 2;
  211. if (win->w.width + win->w.left > fbuf->fmt.width) {
  212. /* We made the preview window extend past the right side of the
  213. * display, so clip it to the display boundary and resize the
  214. * cropping width to maintain the horizontal resizing ratio.
  215. */
  216. win->w.width = (fbuf->fmt.width - win->w.left) & ~1;
  217. if (try_crop.width == 0)
  218. try_crop.width = 2;
  219. }
  220. if (omap_vout_dss_omap24xx()) {
  221. if ((try_crop.height/win->w.height) >= 2)
  222. try_crop.height = win->w.height * 2;
  223. if ((try_crop.width/win->w.width) >= 2)
  224. try_crop.width = win->w.width * 2;
  225. if (try_crop.width > 768) {
  226. /* The OMAP2420 vertical resizing line buffer is
  227. * 768 pixels wide. If the cropped image is wider
  228. * than 768 pixels then it cannot be vertically resized.
  229. */
  230. if (try_crop.height != win->w.height)
  231. try_crop.width = 768;
  232. }
  233. } else if (omap_vout_dss_omap34xx()) {
  234. if ((try_crop.height/win->w.height) >= 4)
  235. try_crop.height = win->w.height * 4;
  236. if ((try_crop.width/win->w.width) >= 4)
  237. try_crop.width = win->w.width * 4;
  238. }
  239. /* update our cropping rectangle and we're done */
  240. *crop = try_crop;
  241. return 0;
  242. }
  243. EXPORT_SYMBOL_GPL(omap_vout_new_crop);
  244. /* Given a new format in pix and fbuf, crop and win
  245. * structures are initialized to default values. crop
  246. * is initialized to the largest window size that will fit on the display. The
  247. * crop window is centered in the image. win is initialized to
  248. * the same size as crop and is centered on the display.
  249. * All sizes and offsets are constrained to be even numbers.
  250. */
  251. void omap_vout_new_format(struct v4l2_pix_format *pix,
  252. struct v4l2_framebuffer *fbuf, struct v4l2_rect *crop,
  253. struct v4l2_window *win)
  254. {
  255. /* crop defines the preview source window in the image capture
  256. * buffer
  257. */
  258. omap_vout_default_crop(pix, fbuf, crop);
  259. /* win defines the preview target window on the display */
  260. win->w.width = crop->width;
  261. win->w.height = crop->height;
  262. win->w.left = ((fbuf->fmt.width - win->w.width) >> 1) & ~1;
  263. win->w.top = ((fbuf->fmt.height - win->w.height) >> 1) & ~1;
  264. }
  265. EXPORT_SYMBOL_GPL(omap_vout_new_format);
  266. /*
  267. * Allocate buffers
  268. */
  269. unsigned long omap_vout_alloc_buffer(u32 buf_size, u32 *phys_addr)
  270. {
  271. u32 order, size;
  272. unsigned long virt_addr, addr;
  273. size = PAGE_ALIGN(buf_size);
  274. order = get_order(size);
  275. virt_addr = __get_free_pages(GFP_KERNEL, order);
  276. addr = virt_addr;
  277. if (virt_addr) {
  278. while (size > 0) {
  279. SetPageReserved(virt_to_page(addr));
  280. addr += PAGE_SIZE;
  281. size -= PAGE_SIZE;
  282. }
  283. }
  284. *phys_addr = (u32) virt_to_phys((void *) virt_addr);
  285. return virt_addr;
  286. }
  287. /*
  288. * Free buffers
  289. */
  290. void omap_vout_free_buffer(unsigned long virtaddr, u32 buf_size)
  291. {
  292. u32 order, size;
  293. unsigned long addr = virtaddr;
  294. size = PAGE_ALIGN(buf_size);
  295. order = get_order(size);
  296. while (size > 0) {
  297. ClearPageReserved(virt_to_page(addr));
  298. addr += PAGE_SIZE;
  299. size -= PAGE_SIZE;
  300. }
  301. free_pages((unsigned long) virtaddr, order);
  302. }
  303. bool omap_vout_dss_omap24xx(void)
  304. {
  305. return omapdss_get_version() == OMAPDSS_VER_OMAP24xx;
  306. }
  307. bool omap_vout_dss_omap34xx(void)
  308. {
  309. switch (omapdss_get_version()) {
  310. case OMAPDSS_VER_OMAP34xx_ES1:
  311. case OMAPDSS_VER_OMAP34xx_ES3:
  312. case OMAPDSS_VER_OMAP3630:
  313. case OMAPDSS_VER_AM35xx:
  314. return true;
  315. default:
  316. return false;
  317. }
  318. }