omap_vout.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299
  1. /*
  2. * omap_vout.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. * Leveraged code from 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. * History:
  20. * 20-APR-2006 Khasim Modified VRFB based Rotation,
  21. * The image data is always read from 0 degree
  22. * view and written
  23. * to the virtual space of desired rotation angle
  24. * 4-DEC-2006 Jian Changed to support better memory management
  25. *
  26. * 17-Nov-2008 Hardik Changed driver to use video_ioctl2
  27. *
  28. * 23-Feb-2010 Vaibhav H Modified to use new DSS2 interface
  29. *
  30. */
  31. #include <linux/init.h>
  32. #include <linux/module.h>
  33. #include <linux/vmalloc.h>
  34. #include <linux/sched.h>
  35. #include <linux/types.h>
  36. #include <linux/platform_device.h>
  37. #include <linux/irq.h>
  38. #include <linux/videodev2.h>
  39. #include <linux/dma-mapping.h>
  40. #include <linux/slab.h>
  41. #include <media/videobuf-dma-contig.h>
  42. #include <media/v4l2-device.h>
  43. #include <media/v4l2-ioctl.h>
  44. #include <video/omapvrfb.h>
  45. #include <video/omapdss.h>
  46. #include "omap_voutlib.h"
  47. #include "omap_voutdef.h"
  48. #include "omap_vout_vrfb.h"
  49. MODULE_AUTHOR("Texas Instruments");
  50. MODULE_DESCRIPTION("OMAP Video for Linux Video out driver");
  51. MODULE_LICENSE("GPL");
  52. /* Driver Configuration macros */
  53. #define VOUT_NAME "omap_vout"
  54. enum omap_vout_channels {
  55. OMAP_VIDEO1,
  56. OMAP_VIDEO2,
  57. };
  58. static struct videobuf_queue_ops video_vbq_ops;
  59. /* Variables configurable through module params*/
  60. static u32 video1_numbuffers = 3;
  61. static u32 video2_numbuffers = 3;
  62. static u32 video1_bufsize = OMAP_VOUT_MAX_BUF_SIZE;
  63. static u32 video2_bufsize = OMAP_VOUT_MAX_BUF_SIZE;
  64. static bool vid1_static_vrfb_alloc;
  65. static bool vid2_static_vrfb_alloc;
  66. static bool debug;
  67. /* Module parameters */
  68. module_param(video1_numbuffers, uint, S_IRUGO);
  69. MODULE_PARM_DESC(video1_numbuffers,
  70. "Number of buffers to be allocated at init time for Video1 device.");
  71. module_param(video2_numbuffers, uint, S_IRUGO);
  72. MODULE_PARM_DESC(video2_numbuffers,
  73. "Number of buffers to be allocated at init time for Video2 device.");
  74. module_param(video1_bufsize, uint, S_IRUGO);
  75. MODULE_PARM_DESC(video1_bufsize,
  76. "Size of the buffer to be allocated for video1 device");
  77. module_param(video2_bufsize, uint, S_IRUGO);
  78. MODULE_PARM_DESC(video2_bufsize,
  79. "Size of the buffer to be allocated for video2 device");
  80. module_param(vid1_static_vrfb_alloc, bool, S_IRUGO);
  81. MODULE_PARM_DESC(vid1_static_vrfb_alloc,
  82. "Static allocation of the VRFB buffer for video1 device");
  83. module_param(vid2_static_vrfb_alloc, bool, S_IRUGO);
  84. MODULE_PARM_DESC(vid2_static_vrfb_alloc,
  85. "Static allocation of the VRFB buffer for video2 device");
  86. module_param(debug, bool, S_IRUGO);
  87. MODULE_PARM_DESC(debug, "Debug level (0-1)");
  88. /* list of image formats supported by OMAP2 video pipelines */
  89. static const struct v4l2_fmtdesc omap_formats[] = {
  90. {
  91. /* Note: V4L2 defines RGB565 as:
  92. *
  93. * Byte 0 Byte 1
  94. * g2 g1 g0 r4 r3 r2 r1 r0 b4 b3 b2 b1 b0 g5 g4 g3
  95. *
  96. * We interpret RGB565 as:
  97. *
  98. * Byte 0 Byte 1
  99. * g2 g1 g0 b4 b3 b2 b1 b0 r4 r3 r2 r1 r0 g5 g4 g3
  100. */
  101. .description = "RGB565, le",
  102. .pixelformat = V4L2_PIX_FMT_RGB565,
  103. },
  104. {
  105. /* Note: V4L2 defines RGB32 as: RGB-8-8-8-8 we use
  106. * this for RGB24 unpack mode, the last 8 bits are ignored
  107. * */
  108. .description = "RGB32, le",
  109. .pixelformat = V4L2_PIX_FMT_RGB32,
  110. },
  111. {
  112. /* Note: V4L2 defines RGB24 as: RGB-8-8-8 we use
  113. * this for RGB24 packed mode
  114. *
  115. */
  116. .description = "RGB24, le",
  117. .pixelformat = V4L2_PIX_FMT_RGB24,
  118. },
  119. {
  120. .description = "YUYV (YUV 4:2:2), packed",
  121. .pixelformat = V4L2_PIX_FMT_YUYV,
  122. },
  123. {
  124. .description = "UYVY, packed",
  125. .pixelformat = V4L2_PIX_FMT_UYVY,
  126. },
  127. };
  128. #define NUM_OUTPUT_FORMATS (ARRAY_SIZE(omap_formats))
  129. /*
  130. * Try format
  131. */
  132. static int omap_vout_try_format(struct v4l2_pix_format *pix)
  133. {
  134. int ifmt, bpp = 0;
  135. pix->height = clamp(pix->height, (u32)VID_MIN_HEIGHT,
  136. (u32)VID_MAX_HEIGHT);
  137. pix->width = clamp(pix->width, (u32)VID_MIN_WIDTH, (u32)VID_MAX_WIDTH);
  138. for (ifmt = 0; ifmt < NUM_OUTPUT_FORMATS; ifmt++) {
  139. if (pix->pixelformat == omap_formats[ifmt].pixelformat)
  140. break;
  141. }
  142. if (ifmt == NUM_OUTPUT_FORMATS)
  143. ifmt = 0;
  144. pix->pixelformat = omap_formats[ifmt].pixelformat;
  145. pix->field = V4L2_FIELD_ANY;
  146. pix->priv = 0;
  147. switch (pix->pixelformat) {
  148. case V4L2_PIX_FMT_YUYV:
  149. case V4L2_PIX_FMT_UYVY:
  150. default:
  151. pix->colorspace = V4L2_COLORSPACE_JPEG;
  152. bpp = YUYV_BPP;
  153. break;
  154. case V4L2_PIX_FMT_RGB565:
  155. case V4L2_PIX_FMT_RGB565X:
  156. pix->colorspace = V4L2_COLORSPACE_SRGB;
  157. bpp = RGB565_BPP;
  158. break;
  159. case V4L2_PIX_FMT_RGB24:
  160. pix->colorspace = V4L2_COLORSPACE_SRGB;
  161. bpp = RGB24_BPP;
  162. break;
  163. case V4L2_PIX_FMT_RGB32:
  164. case V4L2_PIX_FMT_BGR32:
  165. pix->colorspace = V4L2_COLORSPACE_SRGB;
  166. bpp = RGB32_BPP;
  167. break;
  168. }
  169. pix->bytesperline = pix->width * bpp;
  170. pix->sizeimage = pix->bytesperline * pix->height;
  171. return bpp;
  172. }
  173. /*
  174. * omap_vout_uservirt_to_phys: This inline function is used to convert user
  175. * space virtual address to physical address.
  176. */
  177. static u32 omap_vout_uservirt_to_phys(u32 virtp)
  178. {
  179. unsigned long physp = 0;
  180. struct vm_area_struct *vma;
  181. struct mm_struct *mm = current->mm;
  182. vma = find_vma(mm, virtp);
  183. /* For kernel direct-mapped memory, take the easy way */
  184. if (virtp >= PAGE_OFFSET) {
  185. physp = virt_to_phys((void *) virtp);
  186. } else if (vma && (vma->vm_flags & VM_IO) && vma->vm_pgoff) {
  187. /* this will catch, kernel-allocated, mmaped-to-usermode
  188. addresses */
  189. physp = (vma->vm_pgoff << PAGE_SHIFT) + (virtp - vma->vm_start);
  190. } else {
  191. /* otherwise, use get_user_pages() for general userland pages */
  192. int res, nr_pages = 1;
  193. struct page *pages;
  194. down_read(&current->mm->mmap_sem);
  195. res = get_user_pages(current, current->mm, virtp, nr_pages, 1,
  196. 0, &pages, NULL);
  197. up_read(&current->mm->mmap_sem);
  198. if (res == nr_pages) {
  199. physp = __pa(page_address(&pages[0]) +
  200. (virtp & ~PAGE_MASK));
  201. } else {
  202. printk(KERN_WARNING VOUT_NAME
  203. "get_user_pages failed\n");
  204. return 0;
  205. }
  206. }
  207. return physp;
  208. }
  209. /*
  210. * Free the V4L2 buffers
  211. */
  212. void omap_vout_free_buffers(struct omap_vout_device *vout)
  213. {
  214. int i, numbuffers;
  215. /* Allocate memory for the buffers */
  216. numbuffers = (vout->vid) ? video2_numbuffers : video1_numbuffers;
  217. vout->buffer_size = (vout->vid) ? video2_bufsize : video1_bufsize;
  218. for (i = 0; i < numbuffers; i++) {
  219. omap_vout_free_buffer(vout->buf_virt_addr[i],
  220. vout->buffer_size);
  221. vout->buf_phy_addr[i] = 0;
  222. vout->buf_virt_addr[i] = 0;
  223. }
  224. }
  225. /*
  226. * Convert V4L2 rotation to DSS rotation
  227. * V4L2 understand 0, 90, 180, 270.
  228. * Convert to 0, 1, 2 and 3 respectively for DSS
  229. */
  230. static int v4l2_rot_to_dss_rot(int v4l2_rotation,
  231. enum dss_rotation *rotation, bool mirror)
  232. {
  233. int ret = 0;
  234. switch (v4l2_rotation) {
  235. case 90:
  236. *rotation = dss_rotation_90_degree;
  237. break;
  238. case 180:
  239. *rotation = dss_rotation_180_degree;
  240. break;
  241. case 270:
  242. *rotation = dss_rotation_270_degree;
  243. break;
  244. case 0:
  245. *rotation = dss_rotation_0_degree;
  246. break;
  247. default:
  248. ret = -EINVAL;
  249. }
  250. return ret;
  251. }
  252. static int omap_vout_calculate_offset(struct omap_vout_device *vout)
  253. {
  254. struct omapvideo_info *ovid;
  255. struct v4l2_rect *crop = &vout->crop;
  256. struct v4l2_pix_format *pix = &vout->pix;
  257. int *cropped_offset = &vout->cropped_offset;
  258. int ps = 2, line_length = 0;
  259. ovid = &vout->vid_info;
  260. if (ovid->rotation_type == VOUT_ROT_VRFB) {
  261. omap_vout_calculate_vrfb_offset(vout);
  262. } else {
  263. vout->line_length = line_length = pix->width;
  264. if (V4L2_PIX_FMT_YUYV == pix->pixelformat ||
  265. V4L2_PIX_FMT_UYVY == pix->pixelformat)
  266. ps = 2;
  267. else if (V4L2_PIX_FMT_RGB32 == pix->pixelformat)
  268. ps = 4;
  269. else if (V4L2_PIX_FMT_RGB24 == pix->pixelformat)
  270. ps = 3;
  271. vout->ps = ps;
  272. *cropped_offset = (line_length * ps) *
  273. crop->top + crop->left * ps;
  274. }
  275. v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "%s Offset:%x\n",
  276. __func__, vout->cropped_offset);
  277. return 0;
  278. }
  279. /*
  280. * Convert V4L2 pixel format to DSS pixel format
  281. */
  282. static int video_mode_to_dss_mode(struct omap_vout_device *vout)
  283. {
  284. struct omap_overlay *ovl;
  285. struct omapvideo_info *ovid;
  286. struct v4l2_pix_format *pix = &vout->pix;
  287. enum omap_color_mode mode;
  288. ovid = &vout->vid_info;
  289. ovl = ovid->overlays[0];
  290. switch (pix->pixelformat) {
  291. case 0:
  292. break;
  293. case V4L2_PIX_FMT_YUYV:
  294. mode = OMAP_DSS_COLOR_YUV2;
  295. break;
  296. case V4L2_PIX_FMT_UYVY:
  297. mode = OMAP_DSS_COLOR_UYVY;
  298. break;
  299. case V4L2_PIX_FMT_RGB565:
  300. mode = OMAP_DSS_COLOR_RGB16;
  301. break;
  302. case V4L2_PIX_FMT_RGB24:
  303. mode = OMAP_DSS_COLOR_RGB24P;
  304. break;
  305. case V4L2_PIX_FMT_RGB32:
  306. mode = (ovl->id == OMAP_DSS_VIDEO1) ?
  307. OMAP_DSS_COLOR_RGB24U : OMAP_DSS_COLOR_ARGB32;
  308. break;
  309. case V4L2_PIX_FMT_BGR32:
  310. mode = OMAP_DSS_COLOR_RGBX32;
  311. break;
  312. default:
  313. mode = -EINVAL;
  314. }
  315. return mode;
  316. }
  317. /*
  318. * Setup the overlay
  319. */
  320. static int omapvid_setup_overlay(struct omap_vout_device *vout,
  321. struct omap_overlay *ovl, int posx, int posy, int outw,
  322. int outh, u32 addr)
  323. {
  324. int ret = 0;
  325. struct omap_overlay_info info;
  326. int cropheight, cropwidth, pixheight, pixwidth;
  327. if ((ovl->caps & OMAP_DSS_OVL_CAP_SCALE) == 0 &&
  328. (outw != vout->pix.width || outh != vout->pix.height)) {
  329. ret = -EINVAL;
  330. goto setup_ovl_err;
  331. }
  332. vout->dss_mode = video_mode_to_dss_mode(vout);
  333. if (vout->dss_mode == -EINVAL) {
  334. ret = -EINVAL;
  335. goto setup_ovl_err;
  336. }
  337. /* Setup the input plane parameters according to
  338. * rotation value selected.
  339. */
  340. if (is_rotation_90_or_270(vout)) {
  341. cropheight = vout->crop.width;
  342. cropwidth = vout->crop.height;
  343. pixheight = vout->pix.width;
  344. pixwidth = vout->pix.height;
  345. } else {
  346. cropheight = vout->crop.height;
  347. cropwidth = vout->crop.width;
  348. pixheight = vout->pix.height;
  349. pixwidth = vout->pix.width;
  350. }
  351. ovl->get_overlay_info(ovl, &info);
  352. info.paddr = addr;
  353. info.width = cropwidth;
  354. info.height = cropheight;
  355. info.color_mode = vout->dss_mode;
  356. info.mirror = vout->mirror;
  357. info.pos_x = posx;
  358. info.pos_y = posy;
  359. info.out_width = outw;
  360. info.out_height = outh;
  361. info.global_alpha = vout->win.global_alpha;
  362. if (!is_rotation_enabled(vout)) {
  363. info.rotation = 0;
  364. info.rotation_type = OMAP_DSS_ROT_DMA;
  365. info.screen_width = pixwidth;
  366. } else {
  367. info.rotation = vout->rotation;
  368. info.rotation_type = OMAP_DSS_ROT_VRFB;
  369. info.screen_width = 2048;
  370. }
  371. v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
  372. "%s enable=%d addr=%x width=%d\n height=%d color_mode=%d\n"
  373. "rotation=%d mirror=%d posx=%d posy=%d out_width = %d \n"
  374. "out_height=%d rotation_type=%d screen_width=%d\n",
  375. __func__, ovl->is_enabled(ovl), info.paddr, info.width, info.height,
  376. info.color_mode, info.rotation, info.mirror, info.pos_x,
  377. info.pos_y, info.out_width, info.out_height, info.rotation_type,
  378. info.screen_width);
  379. ret = ovl->set_overlay_info(ovl, &info);
  380. if (ret)
  381. goto setup_ovl_err;
  382. return 0;
  383. setup_ovl_err:
  384. v4l2_warn(&vout->vid_dev->v4l2_dev, "setup_overlay failed\n");
  385. return ret;
  386. }
  387. /*
  388. * Initialize the overlay structure
  389. */
  390. static int omapvid_init(struct omap_vout_device *vout, u32 addr)
  391. {
  392. int ret = 0, i;
  393. struct v4l2_window *win;
  394. struct omap_overlay *ovl;
  395. int posx, posy, outw, outh, temp;
  396. struct omap_video_timings *timing;
  397. struct omapvideo_info *ovid = &vout->vid_info;
  398. win = &vout->win;
  399. for (i = 0; i < ovid->num_overlays; i++) {
  400. struct omap_dss_device *dssdev;
  401. ovl = ovid->overlays[i];
  402. dssdev = ovl->get_device(ovl);
  403. if (!dssdev)
  404. return -EINVAL;
  405. timing = &dssdev->panel.timings;
  406. outw = win->w.width;
  407. outh = win->w.height;
  408. switch (vout->rotation) {
  409. case dss_rotation_90_degree:
  410. /* Invert the height and width for 90
  411. * and 270 degree rotation
  412. */
  413. temp = outw;
  414. outw = outh;
  415. outh = temp;
  416. posy = (timing->y_res - win->w.width) - win->w.left;
  417. posx = win->w.top;
  418. break;
  419. case dss_rotation_180_degree:
  420. posx = (timing->x_res - win->w.width) - win->w.left;
  421. posy = (timing->y_res - win->w.height) - win->w.top;
  422. break;
  423. case dss_rotation_270_degree:
  424. temp = outw;
  425. outw = outh;
  426. outh = temp;
  427. posy = win->w.left;
  428. posx = (timing->x_res - win->w.height) - win->w.top;
  429. break;
  430. default:
  431. posx = win->w.left;
  432. posy = win->w.top;
  433. break;
  434. }
  435. ret = omapvid_setup_overlay(vout, ovl, posx, posy,
  436. outw, outh, addr);
  437. if (ret)
  438. goto omapvid_init_err;
  439. }
  440. return 0;
  441. omapvid_init_err:
  442. v4l2_warn(&vout->vid_dev->v4l2_dev, "apply_changes failed\n");
  443. return ret;
  444. }
  445. /*
  446. * Apply the changes set the go bit of DSS
  447. */
  448. static int omapvid_apply_changes(struct omap_vout_device *vout)
  449. {
  450. int i;
  451. struct omap_overlay *ovl;
  452. struct omapvideo_info *ovid = &vout->vid_info;
  453. for (i = 0; i < ovid->num_overlays; i++) {
  454. struct omap_dss_device *dssdev;
  455. ovl = ovid->overlays[i];
  456. dssdev = ovl->get_device(ovl);
  457. if (!dssdev)
  458. return -EINVAL;
  459. ovl->manager->apply(ovl->manager);
  460. }
  461. return 0;
  462. }
  463. static int omapvid_handle_interlace_display(struct omap_vout_device *vout,
  464. unsigned int irqstatus, struct timeval timevalue)
  465. {
  466. u32 fid;
  467. if (vout->first_int) {
  468. vout->first_int = 0;
  469. goto err;
  470. }
  471. if (irqstatus & DISPC_IRQ_EVSYNC_ODD)
  472. fid = 1;
  473. else if (irqstatus & DISPC_IRQ_EVSYNC_EVEN)
  474. fid = 0;
  475. else
  476. goto err;
  477. vout->field_id ^= 1;
  478. if (fid != vout->field_id) {
  479. if (fid == 0)
  480. vout->field_id = fid;
  481. } else if (0 == fid) {
  482. if (vout->cur_frm == vout->next_frm)
  483. goto err;
  484. vout->cur_frm->ts = timevalue;
  485. vout->cur_frm->state = VIDEOBUF_DONE;
  486. wake_up_interruptible(&vout->cur_frm->done);
  487. vout->cur_frm = vout->next_frm;
  488. } else {
  489. if (list_empty(&vout->dma_queue) ||
  490. (vout->cur_frm != vout->next_frm))
  491. goto err;
  492. }
  493. return vout->field_id;
  494. err:
  495. return 0;
  496. }
  497. static void omap_vout_isr(void *arg, unsigned int irqstatus)
  498. {
  499. int ret, fid, mgr_id;
  500. u32 addr, irq;
  501. struct omap_overlay *ovl;
  502. struct timeval timevalue;
  503. struct omapvideo_info *ovid;
  504. struct omap_dss_device *cur_display;
  505. struct omap_vout_device *vout = (struct omap_vout_device *)arg;
  506. if (!vout->streaming)
  507. return;
  508. ovid = &vout->vid_info;
  509. ovl = ovid->overlays[0];
  510. mgr_id = ovl->manager->id;
  511. /* get the display device attached to the overlay */
  512. cur_display = ovl->get_device(ovl);
  513. if (!cur_display)
  514. return;
  515. spin_lock(&vout->vbq_lock);
  516. do_gettimeofday(&timevalue);
  517. switch (cur_display->type) {
  518. case OMAP_DISPLAY_TYPE_DSI:
  519. case OMAP_DISPLAY_TYPE_DPI:
  520. if (mgr_id == OMAP_DSS_CHANNEL_LCD)
  521. irq = DISPC_IRQ_VSYNC;
  522. else if (mgr_id == OMAP_DSS_CHANNEL_LCD2)
  523. irq = DISPC_IRQ_VSYNC2;
  524. else
  525. goto vout_isr_err;
  526. if (!(irqstatus & irq))
  527. goto vout_isr_err;
  528. break;
  529. case OMAP_DISPLAY_TYPE_VENC:
  530. fid = omapvid_handle_interlace_display(vout, irqstatus,
  531. timevalue);
  532. if (!fid)
  533. goto vout_isr_err;
  534. break;
  535. case OMAP_DISPLAY_TYPE_HDMI:
  536. if (!(irqstatus & DISPC_IRQ_EVSYNC_EVEN))
  537. goto vout_isr_err;
  538. break;
  539. default:
  540. goto vout_isr_err;
  541. }
  542. if (!vout->first_int && (vout->cur_frm != vout->next_frm)) {
  543. vout->cur_frm->ts = timevalue;
  544. vout->cur_frm->state = VIDEOBUF_DONE;
  545. wake_up_interruptible(&vout->cur_frm->done);
  546. vout->cur_frm = vout->next_frm;
  547. }
  548. vout->first_int = 0;
  549. if (list_empty(&vout->dma_queue))
  550. goto vout_isr_err;
  551. vout->next_frm = list_entry(vout->dma_queue.next,
  552. struct videobuf_buffer, queue);
  553. list_del(&vout->next_frm->queue);
  554. vout->next_frm->state = VIDEOBUF_ACTIVE;
  555. addr = (unsigned long) vout->queued_buf_addr[vout->next_frm->i]
  556. + vout->cropped_offset;
  557. /* First save the configuration in ovelray structure */
  558. ret = omapvid_init(vout, addr);
  559. if (ret)
  560. printk(KERN_ERR VOUT_NAME
  561. "failed to set overlay info\n");
  562. /* Enable the pipeline and set the Go bit */
  563. ret = omapvid_apply_changes(vout);
  564. if (ret)
  565. printk(KERN_ERR VOUT_NAME "failed to change mode\n");
  566. vout_isr_err:
  567. spin_unlock(&vout->vbq_lock);
  568. }
  569. /* Video buffer call backs */
  570. /*
  571. * Buffer setup function is called by videobuf layer when REQBUF ioctl is
  572. * called. This is used to setup buffers and return size and count of
  573. * buffers allocated. After the call to this buffer, videobuf layer will
  574. * setup buffer queue depending on the size and count of buffers
  575. */
  576. static int omap_vout_buffer_setup(struct videobuf_queue *q, unsigned int *count,
  577. unsigned int *size)
  578. {
  579. int startindex = 0, i, j;
  580. u32 phy_addr = 0, virt_addr = 0;
  581. struct omap_vout_device *vout = q->priv_data;
  582. struct omapvideo_info *ovid = &vout->vid_info;
  583. int vid_max_buf_size;
  584. if (!vout)
  585. return -EINVAL;
  586. vid_max_buf_size = vout->vid == OMAP_VIDEO1 ? video1_bufsize :
  587. video2_bufsize;
  588. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != q->type)
  589. return -EINVAL;
  590. startindex = (vout->vid == OMAP_VIDEO1) ?
  591. video1_numbuffers : video2_numbuffers;
  592. if (V4L2_MEMORY_MMAP == vout->memory && *count < startindex)
  593. *count = startindex;
  594. if (ovid->rotation_type == VOUT_ROT_VRFB) {
  595. if (omap_vout_vrfb_buffer_setup(vout, count, startindex))
  596. return -ENOMEM;
  597. }
  598. if (V4L2_MEMORY_MMAP != vout->memory)
  599. return 0;
  600. /* Now allocated the V4L2 buffers */
  601. *size = PAGE_ALIGN(vout->pix.width * vout->pix.height * vout->bpp);
  602. startindex = (vout->vid == OMAP_VIDEO1) ?
  603. video1_numbuffers : video2_numbuffers;
  604. /* Check the size of the buffer */
  605. if (*size > vid_max_buf_size) {
  606. v4l2_err(&vout->vid_dev->v4l2_dev,
  607. "buffer allocation mismatch [%u] [%u]\n",
  608. *size, vout->buffer_size);
  609. return -ENOMEM;
  610. }
  611. for (i = startindex; i < *count; i++) {
  612. vout->buffer_size = *size;
  613. virt_addr = omap_vout_alloc_buffer(vout->buffer_size,
  614. &phy_addr);
  615. if (!virt_addr) {
  616. if (ovid->rotation_type == VOUT_ROT_NONE) {
  617. break;
  618. } else {
  619. if (!is_rotation_enabled(vout))
  620. break;
  621. /* Free the VRFB buffers if no space for V4L2 buffers */
  622. for (j = i; j < *count; j++) {
  623. omap_vout_free_buffer(
  624. vout->smsshado_virt_addr[j],
  625. vout->smsshado_size);
  626. vout->smsshado_virt_addr[j] = 0;
  627. vout->smsshado_phy_addr[j] = 0;
  628. }
  629. }
  630. }
  631. vout->buf_virt_addr[i] = virt_addr;
  632. vout->buf_phy_addr[i] = phy_addr;
  633. }
  634. *count = vout->buffer_allocated = i;
  635. return 0;
  636. }
  637. /*
  638. * Free the V4L2 buffers additionally allocated than default
  639. * number of buffers
  640. */
  641. static void omap_vout_free_extra_buffers(struct omap_vout_device *vout)
  642. {
  643. int num_buffers = 0, i;
  644. num_buffers = (vout->vid == OMAP_VIDEO1) ?
  645. video1_numbuffers : video2_numbuffers;
  646. for (i = num_buffers; i < vout->buffer_allocated; i++) {
  647. if (vout->buf_virt_addr[i])
  648. omap_vout_free_buffer(vout->buf_virt_addr[i],
  649. vout->buffer_size);
  650. vout->buf_virt_addr[i] = 0;
  651. vout->buf_phy_addr[i] = 0;
  652. }
  653. vout->buffer_allocated = num_buffers;
  654. }
  655. /*
  656. * This function will be called when VIDIOC_QBUF ioctl is called.
  657. * It prepare buffers before give out for the display. This function
  658. * converts user space virtual address into physical address if userptr memory
  659. * exchange mechanism is used. If rotation is enabled, it copies entire
  660. * buffer into VRFB memory space before giving it to the DSS.
  661. */
  662. static int omap_vout_buffer_prepare(struct videobuf_queue *q,
  663. struct videobuf_buffer *vb,
  664. enum v4l2_field field)
  665. {
  666. struct omap_vout_device *vout = q->priv_data;
  667. struct omapvideo_info *ovid = &vout->vid_info;
  668. if (VIDEOBUF_NEEDS_INIT == vb->state) {
  669. vb->width = vout->pix.width;
  670. vb->height = vout->pix.height;
  671. vb->size = vb->width * vb->height * vout->bpp;
  672. vb->field = field;
  673. }
  674. vb->state = VIDEOBUF_PREPARED;
  675. /* if user pointer memory mechanism is used, get the physical
  676. * address of the buffer
  677. */
  678. if (V4L2_MEMORY_USERPTR == vb->memory) {
  679. if (0 == vb->baddr)
  680. return -EINVAL;
  681. /* Physical address */
  682. vout->queued_buf_addr[vb->i] = (u8 *)
  683. omap_vout_uservirt_to_phys(vb->baddr);
  684. } else {
  685. u32 addr, dma_addr;
  686. unsigned long size;
  687. addr = (unsigned long) vout->buf_virt_addr[vb->i];
  688. size = (unsigned long) vb->size;
  689. dma_addr = dma_map_single(vout->vid_dev->v4l2_dev.dev, (void *) addr,
  690. size, DMA_TO_DEVICE);
  691. if (dma_mapping_error(vout->vid_dev->v4l2_dev.dev, dma_addr))
  692. v4l2_err(&vout->vid_dev->v4l2_dev, "dma_map_single failed\n");
  693. vout->queued_buf_addr[vb->i] = (u8 *)vout->buf_phy_addr[vb->i];
  694. }
  695. if (ovid->rotation_type == VOUT_ROT_VRFB)
  696. return omap_vout_prepare_vrfb(vout, vb);
  697. else
  698. return 0;
  699. }
  700. /*
  701. * Buffer queue function will be called from the videobuf layer when _QBUF
  702. * ioctl is called. It is used to enqueue buffer, which is ready to be
  703. * displayed.
  704. */
  705. static void omap_vout_buffer_queue(struct videobuf_queue *q,
  706. struct videobuf_buffer *vb)
  707. {
  708. struct omap_vout_device *vout = q->priv_data;
  709. /* Driver is also maintainig a queue. So enqueue buffer in the driver
  710. * queue */
  711. list_add_tail(&vb->queue, &vout->dma_queue);
  712. vb->state = VIDEOBUF_QUEUED;
  713. }
  714. /*
  715. * Buffer release function is called from videobuf layer to release buffer
  716. * which are already allocated
  717. */
  718. static void omap_vout_buffer_release(struct videobuf_queue *q,
  719. struct videobuf_buffer *vb)
  720. {
  721. struct omap_vout_device *vout = q->priv_data;
  722. vb->state = VIDEOBUF_NEEDS_INIT;
  723. if (V4L2_MEMORY_MMAP != vout->memory)
  724. return;
  725. }
  726. /*
  727. * File operations
  728. */
  729. static unsigned int omap_vout_poll(struct file *file,
  730. struct poll_table_struct *wait)
  731. {
  732. struct omap_vout_device *vout = file->private_data;
  733. struct videobuf_queue *q = &vout->vbq;
  734. return videobuf_poll_stream(file, q, wait);
  735. }
  736. static void omap_vout_vm_open(struct vm_area_struct *vma)
  737. {
  738. struct omap_vout_device *vout = vma->vm_private_data;
  739. v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
  740. "vm_open [vma=%08lx-%08lx]\n", vma->vm_start, vma->vm_end);
  741. vout->mmap_count++;
  742. }
  743. static void omap_vout_vm_close(struct vm_area_struct *vma)
  744. {
  745. struct omap_vout_device *vout = vma->vm_private_data;
  746. v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
  747. "vm_close [vma=%08lx-%08lx]\n", vma->vm_start, vma->vm_end);
  748. vout->mmap_count--;
  749. }
  750. static struct vm_operations_struct omap_vout_vm_ops = {
  751. .open = omap_vout_vm_open,
  752. .close = omap_vout_vm_close,
  753. };
  754. static int omap_vout_mmap(struct file *file, struct vm_area_struct *vma)
  755. {
  756. int i;
  757. void *pos;
  758. unsigned long start = vma->vm_start;
  759. unsigned long size = (vma->vm_end - vma->vm_start);
  760. struct omap_vout_device *vout = file->private_data;
  761. struct videobuf_queue *q = &vout->vbq;
  762. v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
  763. " %s pgoff=0x%lx, start=0x%lx, end=0x%lx\n", __func__,
  764. vma->vm_pgoff, vma->vm_start, vma->vm_end);
  765. /* look for the buffer to map */
  766. for (i = 0; i < VIDEO_MAX_FRAME; i++) {
  767. if (NULL == q->bufs[i])
  768. continue;
  769. if (V4L2_MEMORY_MMAP != q->bufs[i]->memory)
  770. continue;
  771. if (q->bufs[i]->boff == (vma->vm_pgoff << PAGE_SHIFT))
  772. break;
  773. }
  774. if (VIDEO_MAX_FRAME == i) {
  775. v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
  776. "offset invalid [offset=0x%lx]\n",
  777. (vma->vm_pgoff << PAGE_SHIFT));
  778. return -EINVAL;
  779. }
  780. /* Check the size of the buffer */
  781. if (size > vout->buffer_size) {
  782. v4l2_err(&vout->vid_dev->v4l2_dev,
  783. "insufficient memory [%lu] [%u]\n",
  784. size, vout->buffer_size);
  785. return -ENOMEM;
  786. }
  787. q->bufs[i]->baddr = vma->vm_start;
  788. vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
  789. vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
  790. vma->vm_ops = &omap_vout_vm_ops;
  791. vma->vm_private_data = (void *) vout;
  792. pos = (void *)vout->buf_virt_addr[i];
  793. vma->vm_pgoff = virt_to_phys((void *)pos) >> PAGE_SHIFT;
  794. while (size > 0) {
  795. unsigned long pfn;
  796. pfn = virt_to_phys((void *) pos) >> PAGE_SHIFT;
  797. if (remap_pfn_range(vma, start, pfn, PAGE_SIZE, PAGE_SHARED))
  798. return -EAGAIN;
  799. start += PAGE_SIZE;
  800. pos += PAGE_SIZE;
  801. size -= PAGE_SIZE;
  802. }
  803. vout->mmap_count++;
  804. v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Exiting %s\n", __func__);
  805. return 0;
  806. }
  807. static int omap_vout_release(struct file *file)
  808. {
  809. unsigned int ret, i;
  810. struct videobuf_queue *q;
  811. struct omapvideo_info *ovid;
  812. struct omap_vout_device *vout = file->private_data;
  813. v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Entering %s\n", __func__);
  814. ovid = &vout->vid_info;
  815. if (!vout)
  816. return 0;
  817. q = &vout->vbq;
  818. /* Disable all the overlay managers connected with this interface */
  819. for (i = 0; i < ovid->num_overlays; i++) {
  820. struct omap_overlay *ovl = ovid->overlays[i];
  821. struct omap_dss_device *dssdev = ovl->get_device(ovl);
  822. if (dssdev)
  823. ovl->disable(ovl);
  824. }
  825. /* Turn off the pipeline */
  826. ret = omapvid_apply_changes(vout);
  827. if (ret)
  828. v4l2_warn(&vout->vid_dev->v4l2_dev,
  829. "Unable to apply changes\n");
  830. /* Free all buffers */
  831. omap_vout_free_extra_buffers(vout);
  832. /* Free the VRFB buffers only if they are allocated
  833. * during reqbufs. Don't free if init time allocated
  834. */
  835. if (ovid->rotation_type == VOUT_ROT_VRFB) {
  836. if (!vout->vrfb_static_allocation)
  837. omap_vout_free_vrfb_buffers(vout);
  838. }
  839. videobuf_mmap_free(q);
  840. /* Even if apply changes fails we should continue
  841. freeing allocated memory */
  842. if (vout->streaming) {
  843. u32 mask = 0;
  844. mask = DISPC_IRQ_VSYNC | DISPC_IRQ_EVSYNC_EVEN |
  845. DISPC_IRQ_EVSYNC_ODD | DISPC_IRQ_VSYNC2;
  846. omap_dispc_unregister_isr(omap_vout_isr, vout, mask);
  847. vout->streaming = 0;
  848. videobuf_streamoff(q);
  849. videobuf_queue_cancel(q);
  850. }
  851. if (vout->mmap_count != 0)
  852. vout->mmap_count = 0;
  853. vout->opened -= 1;
  854. file->private_data = NULL;
  855. if (vout->buffer_allocated)
  856. videobuf_mmap_free(q);
  857. v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Exiting %s\n", __func__);
  858. return ret;
  859. }
  860. static int omap_vout_open(struct file *file)
  861. {
  862. struct videobuf_queue *q;
  863. struct omap_vout_device *vout = NULL;
  864. vout = video_drvdata(file);
  865. v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Entering %s\n", __func__);
  866. if (vout == NULL)
  867. return -ENODEV;
  868. /* for now, we only support single open */
  869. if (vout->opened)
  870. return -EBUSY;
  871. vout->opened += 1;
  872. file->private_data = vout;
  873. vout->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  874. q = &vout->vbq;
  875. video_vbq_ops.buf_setup = omap_vout_buffer_setup;
  876. video_vbq_ops.buf_prepare = omap_vout_buffer_prepare;
  877. video_vbq_ops.buf_release = omap_vout_buffer_release;
  878. video_vbq_ops.buf_queue = omap_vout_buffer_queue;
  879. spin_lock_init(&vout->vbq_lock);
  880. videobuf_queue_dma_contig_init(q, &video_vbq_ops, q->dev,
  881. &vout->vbq_lock, vout->type, V4L2_FIELD_NONE,
  882. sizeof(struct videobuf_buffer), vout, NULL);
  883. v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Exiting %s\n", __func__);
  884. return 0;
  885. }
  886. /*
  887. * V4L2 ioctls
  888. */
  889. static int vidioc_querycap(struct file *file, void *fh,
  890. struct v4l2_capability *cap)
  891. {
  892. struct omap_vout_device *vout = fh;
  893. strlcpy(cap->driver, VOUT_NAME, sizeof(cap->driver));
  894. strlcpy(cap->card, vout->vfd->name, sizeof(cap->card));
  895. cap->bus_info[0] = '\0';
  896. cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_OUTPUT |
  897. V4L2_CAP_VIDEO_OUTPUT_OVERLAY;
  898. return 0;
  899. }
  900. static int vidioc_enum_fmt_vid_out(struct file *file, void *fh,
  901. struct v4l2_fmtdesc *fmt)
  902. {
  903. int index = fmt->index;
  904. if (index >= NUM_OUTPUT_FORMATS)
  905. return -EINVAL;
  906. fmt->flags = omap_formats[index].flags;
  907. strlcpy(fmt->description, omap_formats[index].description,
  908. sizeof(fmt->description));
  909. fmt->pixelformat = omap_formats[index].pixelformat;
  910. return 0;
  911. }
  912. static int vidioc_g_fmt_vid_out(struct file *file, void *fh,
  913. struct v4l2_format *f)
  914. {
  915. struct omap_vout_device *vout = fh;
  916. f->fmt.pix = vout->pix;
  917. return 0;
  918. }
  919. static int vidioc_try_fmt_vid_out(struct file *file, void *fh,
  920. struct v4l2_format *f)
  921. {
  922. struct omap_overlay *ovl;
  923. struct omapvideo_info *ovid;
  924. struct omap_video_timings *timing;
  925. struct omap_vout_device *vout = fh;
  926. struct omap_dss_device *dssdev;
  927. ovid = &vout->vid_info;
  928. ovl = ovid->overlays[0];
  929. /* get the display device attached to the overlay */
  930. dssdev = ovl->get_device(ovl);
  931. if (!dssdev)
  932. return -EINVAL;
  933. timing = &dssdev->panel.timings;
  934. vout->fbuf.fmt.height = timing->y_res;
  935. vout->fbuf.fmt.width = timing->x_res;
  936. omap_vout_try_format(&f->fmt.pix);
  937. return 0;
  938. }
  939. static int vidioc_s_fmt_vid_out(struct file *file, void *fh,
  940. struct v4l2_format *f)
  941. {
  942. int ret, bpp;
  943. struct omap_overlay *ovl;
  944. struct omapvideo_info *ovid;
  945. struct omap_video_timings *timing;
  946. struct omap_vout_device *vout = fh;
  947. struct omap_dss_device *dssdev;
  948. if (vout->streaming)
  949. return -EBUSY;
  950. mutex_lock(&vout->lock);
  951. ovid = &vout->vid_info;
  952. ovl = ovid->overlays[0];
  953. dssdev = ovl->get_device(ovl);
  954. /* get the display device attached to the overlay */
  955. if (!dssdev) {
  956. ret = -EINVAL;
  957. goto s_fmt_vid_out_exit;
  958. }
  959. timing = &dssdev->panel.timings;
  960. /* We dont support RGB24-packed mode if vrfb rotation
  961. * is enabled*/
  962. if ((is_rotation_enabled(vout)) &&
  963. f->fmt.pix.pixelformat == V4L2_PIX_FMT_RGB24) {
  964. ret = -EINVAL;
  965. goto s_fmt_vid_out_exit;
  966. }
  967. /* get the framebuffer parameters */
  968. if (is_rotation_90_or_270(vout)) {
  969. vout->fbuf.fmt.height = timing->x_res;
  970. vout->fbuf.fmt.width = timing->y_res;
  971. } else {
  972. vout->fbuf.fmt.height = timing->y_res;
  973. vout->fbuf.fmt.width = timing->x_res;
  974. }
  975. /* change to samller size is OK */
  976. bpp = omap_vout_try_format(&f->fmt.pix);
  977. f->fmt.pix.sizeimage = f->fmt.pix.width * f->fmt.pix.height * bpp;
  978. /* try & set the new output format */
  979. vout->bpp = bpp;
  980. vout->pix = f->fmt.pix;
  981. vout->vrfb_bpp = 1;
  982. /* If YUYV then vrfb bpp is 2, for others its 1 */
  983. if (V4L2_PIX_FMT_YUYV == vout->pix.pixelformat ||
  984. V4L2_PIX_FMT_UYVY == vout->pix.pixelformat)
  985. vout->vrfb_bpp = 2;
  986. /* set default crop and win */
  987. omap_vout_new_format(&vout->pix, &vout->fbuf, &vout->crop, &vout->win);
  988. ret = 0;
  989. s_fmt_vid_out_exit:
  990. mutex_unlock(&vout->lock);
  991. return ret;
  992. }
  993. static int vidioc_try_fmt_vid_overlay(struct file *file, void *fh,
  994. struct v4l2_format *f)
  995. {
  996. int ret = 0;
  997. struct omap_vout_device *vout = fh;
  998. struct omap_overlay *ovl;
  999. struct omapvideo_info *ovid;
  1000. struct v4l2_window *win = &f->fmt.win;
  1001. ovid = &vout->vid_info;
  1002. ovl = ovid->overlays[0];
  1003. ret = omap_vout_try_window(&vout->fbuf, win);
  1004. if (!ret) {
  1005. if ((ovl->caps & OMAP_DSS_OVL_CAP_GLOBAL_ALPHA) == 0)
  1006. win->global_alpha = 255;
  1007. else
  1008. win->global_alpha = f->fmt.win.global_alpha;
  1009. }
  1010. return ret;
  1011. }
  1012. static int vidioc_s_fmt_vid_overlay(struct file *file, void *fh,
  1013. struct v4l2_format *f)
  1014. {
  1015. int ret = 0;
  1016. struct omap_overlay *ovl;
  1017. struct omapvideo_info *ovid;
  1018. struct omap_vout_device *vout = fh;
  1019. struct v4l2_window *win = &f->fmt.win;
  1020. mutex_lock(&vout->lock);
  1021. ovid = &vout->vid_info;
  1022. ovl = ovid->overlays[0];
  1023. ret = omap_vout_new_window(&vout->crop, &vout->win, &vout->fbuf, win);
  1024. if (!ret) {
  1025. /* Video1 plane does not support global alpha on OMAP3 */
  1026. if ((ovl->caps & OMAP_DSS_OVL_CAP_GLOBAL_ALPHA) == 0)
  1027. vout->win.global_alpha = 255;
  1028. else
  1029. vout->win.global_alpha = f->fmt.win.global_alpha;
  1030. vout->win.chromakey = f->fmt.win.chromakey;
  1031. }
  1032. mutex_unlock(&vout->lock);
  1033. return ret;
  1034. }
  1035. static int vidioc_enum_fmt_vid_overlay(struct file *file, void *fh,
  1036. struct v4l2_fmtdesc *fmt)
  1037. {
  1038. int index = fmt->index;
  1039. if (index >= NUM_OUTPUT_FORMATS)
  1040. return -EINVAL;
  1041. fmt->flags = omap_formats[index].flags;
  1042. strlcpy(fmt->description, omap_formats[index].description,
  1043. sizeof(fmt->description));
  1044. fmt->pixelformat = omap_formats[index].pixelformat;
  1045. return 0;
  1046. }
  1047. static int vidioc_g_fmt_vid_overlay(struct file *file, void *fh,
  1048. struct v4l2_format *f)
  1049. {
  1050. u32 key_value = 0;
  1051. struct omap_overlay *ovl;
  1052. struct omapvideo_info *ovid;
  1053. struct omap_vout_device *vout = fh;
  1054. struct omap_overlay_manager_info info;
  1055. struct v4l2_window *win = &f->fmt.win;
  1056. ovid = &vout->vid_info;
  1057. ovl = ovid->overlays[0];
  1058. win->w = vout->win.w;
  1059. win->field = vout->win.field;
  1060. win->global_alpha = vout->win.global_alpha;
  1061. if (ovl->manager && ovl->manager->get_manager_info) {
  1062. ovl->manager->get_manager_info(ovl->manager, &info);
  1063. key_value = info.trans_key;
  1064. }
  1065. win->chromakey = key_value;
  1066. return 0;
  1067. }
  1068. static int vidioc_cropcap(struct file *file, void *fh,
  1069. struct v4l2_cropcap *cropcap)
  1070. {
  1071. struct omap_vout_device *vout = fh;
  1072. struct v4l2_pix_format *pix = &vout->pix;
  1073. if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  1074. return -EINVAL;
  1075. /* Width and height are always even */
  1076. cropcap->bounds.width = pix->width & ~1;
  1077. cropcap->bounds.height = pix->height & ~1;
  1078. omap_vout_default_crop(&vout->pix, &vout->fbuf, &cropcap->defrect);
  1079. cropcap->pixelaspect.numerator = 1;
  1080. cropcap->pixelaspect.denominator = 1;
  1081. return 0;
  1082. }
  1083. static int vidioc_g_crop(struct file *file, void *fh, struct v4l2_crop *crop)
  1084. {
  1085. struct omap_vout_device *vout = fh;
  1086. if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  1087. return -EINVAL;
  1088. crop->c = vout->crop;
  1089. return 0;
  1090. }
  1091. static int vidioc_s_crop(struct file *file, void *fh, const struct v4l2_crop *crop)
  1092. {
  1093. int ret = -EINVAL;
  1094. struct omap_vout_device *vout = fh;
  1095. struct omapvideo_info *ovid;
  1096. struct omap_overlay *ovl;
  1097. struct omap_video_timings *timing;
  1098. struct omap_dss_device *dssdev;
  1099. if (vout->streaming)
  1100. return -EBUSY;
  1101. mutex_lock(&vout->lock);
  1102. ovid = &vout->vid_info;
  1103. ovl = ovid->overlays[0];
  1104. /* get the display device attached to the overlay */
  1105. dssdev = ovl->get_device(ovl);
  1106. if (!dssdev) {
  1107. ret = -EINVAL;
  1108. goto s_crop_err;
  1109. }
  1110. timing = &dssdev->panel.timings;
  1111. if (is_rotation_90_or_270(vout)) {
  1112. vout->fbuf.fmt.height = timing->x_res;
  1113. vout->fbuf.fmt.width = timing->y_res;
  1114. } else {
  1115. vout->fbuf.fmt.height = timing->y_res;
  1116. vout->fbuf.fmt.width = timing->x_res;
  1117. }
  1118. if (crop->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
  1119. ret = omap_vout_new_crop(&vout->pix, &vout->crop, &vout->win,
  1120. &vout->fbuf, &crop->c);
  1121. s_crop_err:
  1122. mutex_unlock(&vout->lock);
  1123. return ret;
  1124. }
  1125. static int vidioc_queryctrl(struct file *file, void *fh,
  1126. struct v4l2_queryctrl *ctrl)
  1127. {
  1128. int ret = 0;
  1129. switch (ctrl->id) {
  1130. case V4L2_CID_ROTATE:
  1131. ret = v4l2_ctrl_query_fill(ctrl, 0, 270, 90, 0);
  1132. break;
  1133. case V4L2_CID_BG_COLOR:
  1134. ret = v4l2_ctrl_query_fill(ctrl, 0, 0xFFFFFF, 1, 0);
  1135. break;
  1136. case V4L2_CID_VFLIP:
  1137. ret = v4l2_ctrl_query_fill(ctrl, 0, 1, 1, 0);
  1138. break;
  1139. default:
  1140. ctrl->name[0] = '\0';
  1141. ret = -EINVAL;
  1142. }
  1143. return ret;
  1144. }
  1145. static int vidioc_g_ctrl(struct file *file, void *fh, struct v4l2_control *ctrl)
  1146. {
  1147. int ret = 0;
  1148. struct omap_vout_device *vout = fh;
  1149. switch (ctrl->id) {
  1150. case V4L2_CID_ROTATE:
  1151. ctrl->value = vout->control[0].value;
  1152. break;
  1153. case V4L2_CID_BG_COLOR:
  1154. {
  1155. struct omap_overlay_manager_info info;
  1156. struct omap_overlay *ovl;
  1157. ovl = vout->vid_info.overlays[0];
  1158. if (!ovl->manager || !ovl->manager->get_manager_info) {
  1159. ret = -EINVAL;
  1160. break;
  1161. }
  1162. ovl->manager->get_manager_info(ovl->manager, &info);
  1163. ctrl->value = info.default_color;
  1164. break;
  1165. }
  1166. case V4L2_CID_VFLIP:
  1167. ctrl->value = vout->control[2].value;
  1168. break;
  1169. default:
  1170. ret = -EINVAL;
  1171. }
  1172. return ret;
  1173. }
  1174. static int vidioc_s_ctrl(struct file *file, void *fh, struct v4l2_control *a)
  1175. {
  1176. int ret = 0;
  1177. struct omap_vout_device *vout = fh;
  1178. switch (a->id) {
  1179. case V4L2_CID_ROTATE:
  1180. {
  1181. struct omapvideo_info *ovid;
  1182. int rotation = a->value;
  1183. ovid = &vout->vid_info;
  1184. mutex_lock(&vout->lock);
  1185. if (rotation && ovid->rotation_type == VOUT_ROT_NONE) {
  1186. mutex_unlock(&vout->lock);
  1187. ret = -ERANGE;
  1188. break;
  1189. }
  1190. if (rotation && vout->pix.pixelformat == V4L2_PIX_FMT_RGB24) {
  1191. mutex_unlock(&vout->lock);
  1192. ret = -EINVAL;
  1193. break;
  1194. }
  1195. if (v4l2_rot_to_dss_rot(rotation, &vout->rotation,
  1196. vout->mirror)) {
  1197. mutex_unlock(&vout->lock);
  1198. ret = -EINVAL;
  1199. break;
  1200. }
  1201. vout->control[0].value = rotation;
  1202. mutex_unlock(&vout->lock);
  1203. break;
  1204. }
  1205. case V4L2_CID_BG_COLOR:
  1206. {
  1207. struct omap_overlay *ovl;
  1208. unsigned int color = a->value;
  1209. struct omap_overlay_manager_info info;
  1210. ovl = vout->vid_info.overlays[0];
  1211. mutex_lock(&vout->lock);
  1212. if (!ovl->manager || !ovl->manager->get_manager_info) {
  1213. mutex_unlock(&vout->lock);
  1214. ret = -EINVAL;
  1215. break;
  1216. }
  1217. ovl->manager->get_manager_info(ovl->manager, &info);
  1218. info.default_color = color;
  1219. if (ovl->manager->set_manager_info(ovl->manager, &info)) {
  1220. mutex_unlock(&vout->lock);
  1221. ret = -EINVAL;
  1222. break;
  1223. }
  1224. vout->control[1].value = color;
  1225. mutex_unlock(&vout->lock);
  1226. break;
  1227. }
  1228. case V4L2_CID_VFLIP:
  1229. {
  1230. struct omap_overlay *ovl;
  1231. struct omapvideo_info *ovid;
  1232. unsigned int mirror = a->value;
  1233. ovid = &vout->vid_info;
  1234. ovl = ovid->overlays[0];
  1235. mutex_lock(&vout->lock);
  1236. if (mirror && ovid->rotation_type == VOUT_ROT_NONE) {
  1237. mutex_unlock(&vout->lock);
  1238. ret = -ERANGE;
  1239. break;
  1240. }
  1241. if (mirror && vout->pix.pixelformat == V4L2_PIX_FMT_RGB24) {
  1242. mutex_unlock(&vout->lock);
  1243. ret = -EINVAL;
  1244. break;
  1245. }
  1246. vout->mirror = mirror;
  1247. vout->control[2].value = mirror;
  1248. mutex_unlock(&vout->lock);
  1249. break;
  1250. }
  1251. default:
  1252. ret = -EINVAL;
  1253. }
  1254. return ret;
  1255. }
  1256. static int vidioc_reqbufs(struct file *file, void *fh,
  1257. struct v4l2_requestbuffers *req)
  1258. {
  1259. int ret = 0;
  1260. unsigned int i, num_buffers = 0;
  1261. struct omap_vout_device *vout = fh;
  1262. struct videobuf_queue *q = &vout->vbq;
  1263. if ((req->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) || (req->count < 0))
  1264. return -EINVAL;
  1265. /* if memory is not mmp or userptr
  1266. return error */
  1267. if ((V4L2_MEMORY_MMAP != req->memory) &&
  1268. (V4L2_MEMORY_USERPTR != req->memory))
  1269. return -EINVAL;
  1270. mutex_lock(&vout->lock);
  1271. /* Cannot be requested when streaming is on */
  1272. if (vout->streaming) {
  1273. ret = -EBUSY;
  1274. goto reqbuf_err;
  1275. }
  1276. /* If buffers are already allocated free them */
  1277. if (q->bufs[0] && (V4L2_MEMORY_MMAP == q->bufs[0]->memory)) {
  1278. if (vout->mmap_count) {
  1279. ret = -EBUSY;
  1280. goto reqbuf_err;
  1281. }
  1282. num_buffers = (vout->vid == OMAP_VIDEO1) ?
  1283. video1_numbuffers : video2_numbuffers;
  1284. for (i = num_buffers; i < vout->buffer_allocated; i++) {
  1285. omap_vout_free_buffer(vout->buf_virt_addr[i],
  1286. vout->buffer_size);
  1287. vout->buf_virt_addr[i] = 0;
  1288. vout->buf_phy_addr[i] = 0;
  1289. }
  1290. vout->buffer_allocated = num_buffers;
  1291. videobuf_mmap_free(q);
  1292. } else if (q->bufs[0] && (V4L2_MEMORY_USERPTR == q->bufs[0]->memory)) {
  1293. if (vout->buffer_allocated) {
  1294. videobuf_mmap_free(q);
  1295. for (i = 0; i < vout->buffer_allocated; i++) {
  1296. kfree(q->bufs[i]);
  1297. q->bufs[i] = NULL;
  1298. }
  1299. vout->buffer_allocated = 0;
  1300. }
  1301. }
  1302. /*store the memory type in data structure */
  1303. vout->memory = req->memory;
  1304. INIT_LIST_HEAD(&vout->dma_queue);
  1305. /* call videobuf_reqbufs api */
  1306. ret = videobuf_reqbufs(q, req);
  1307. if (ret < 0)
  1308. goto reqbuf_err;
  1309. vout->buffer_allocated = req->count;
  1310. reqbuf_err:
  1311. mutex_unlock(&vout->lock);
  1312. return ret;
  1313. }
  1314. static int vidioc_querybuf(struct file *file, void *fh,
  1315. struct v4l2_buffer *b)
  1316. {
  1317. struct omap_vout_device *vout = fh;
  1318. return videobuf_querybuf(&vout->vbq, b);
  1319. }
  1320. static int vidioc_qbuf(struct file *file, void *fh,
  1321. struct v4l2_buffer *buffer)
  1322. {
  1323. struct omap_vout_device *vout = fh;
  1324. struct videobuf_queue *q = &vout->vbq;
  1325. if ((V4L2_BUF_TYPE_VIDEO_OUTPUT != buffer->type) ||
  1326. (buffer->index >= vout->buffer_allocated) ||
  1327. (q->bufs[buffer->index]->memory != buffer->memory)) {
  1328. return -EINVAL;
  1329. }
  1330. if (V4L2_MEMORY_USERPTR == buffer->memory) {
  1331. if ((buffer->length < vout->pix.sizeimage) ||
  1332. (0 == buffer->m.userptr)) {
  1333. return -EINVAL;
  1334. }
  1335. }
  1336. if ((is_rotation_enabled(vout)) &&
  1337. vout->vrfb_dma_tx.req_status == DMA_CHAN_NOT_ALLOTED) {
  1338. v4l2_warn(&vout->vid_dev->v4l2_dev,
  1339. "DMA Channel not allocated for Rotation\n");
  1340. return -EINVAL;
  1341. }
  1342. return videobuf_qbuf(q, buffer);
  1343. }
  1344. static int vidioc_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
  1345. {
  1346. struct omap_vout_device *vout = fh;
  1347. struct videobuf_queue *q = &vout->vbq;
  1348. int ret;
  1349. u32 addr;
  1350. unsigned long size;
  1351. struct videobuf_buffer *vb;
  1352. vb = q->bufs[b->index];
  1353. if (!vout->streaming)
  1354. return -EINVAL;
  1355. if (file->f_flags & O_NONBLOCK)
  1356. /* Call videobuf_dqbuf for non blocking mode */
  1357. ret = videobuf_dqbuf(q, (struct v4l2_buffer *)b, 1);
  1358. else
  1359. /* Call videobuf_dqbuf for blocking mode */
  1360. ret = videobuf_dqbuf(q, (struct v4l2_buffer *)b, 0);
  1361. addr = (unsigned long) vout->buf_phy_addr[vb->i];
  1362. size = (unsigned long) vb->size;
  1363. dma_unmap_single(vout->vid_dev->v4l2_dev.dev, addr,
  1364. size, DMA_TO_DEVICE);
  1365. return ret;
  1366. }
  1367. static int vidioc_streamon(struct file *file, void *fh, enum v4l2_buf_type i)
  1368. {
  1369. int ret = 0, j;
  1370. u32 addr = 0, mask = 0;
  1371. struct omap_vout_device *vout = fh;
  1372. struct videobuf_queue *q = &vout->vbq;
  1373. struct omapvideo_info *ovid = &vout->vid_info;
  1374. mutex_lock(&vout->lock);
  1375. if (vout->streaming) {
  1376. ret = -EBUSY;
  1377. goto streamon_err;
  1378. }
  1379. ret = videobuf_streamon(q);
  1380. if (ret)
  1381. goto streamon_err;
  1382. if (list_empty(&vout->dma_queue)) {
  1383. ret = -EIO;
  1384. goto streamon_err1;
  1385. }
  1386. /* Get the next frame from the buffer queue */
  1387. vout->next_frm = vout->cur_frm = list_entry(vout->dma_queue.next,
  1388. struct videobuf_buffer, queue);
  1389. /* Remove buffer from the buffer queue */
  1390. list_del(&vout->cur_frm->queue);
  1391. /* Mark state of the current frame to active */
  1392. vout->cur_frm->state = VIDEOBUF_ACTIVE;
  1393. /* Initialize field_id and started member */
  1394. vout->field_id = 0;
  1395. /* set flag here. Next QBUF will start DMA */
  1396. vout->streaming = 1;
  1397. vout->first_int = 1;
  1398. if (omap_vout_calculate_offset(vout)) {
  1399. ret = -EINVAL;
  1400. goto streamon_err1;
  1401. }
  1402. addr = (unsigned long) vout->queued_buf_addr[vout->cur_frm->i]
  1403. + vout->cropped_offset;
  1404. mask = DISPC_IRQ_VSYNC | DISPC_IRQ_EVSYNC_EVEN | DISPC_IRQ_EVSYNC_ODD
  1405. | DISPC_IRQ_VSYNC2;
  1406. omap_dispc_register_isr(omap_vout_isr, vout, mask);
  1407. /* First save the configuration in ovelray structure */
  1408. ret = omapvid_init(vout, addr);
  1409. if (ret)
  1410. v4l2_err(&vout->vid_dev->v4l2_dev,
  1411. "failed to set overlay info\n");
  1412. /* Enable the pipeline and set the Go bit */
  1413. ret = omapvid_apply_changes(vout);
  1414. if (ret)
  1415. v4l2_err(&vout->vid_dev->v4l2_dev, "failed to change mode\n");
  1416. for (j = 0; j < ovid->num_overlays; j++) {
  1417. struct omap_overlay *ovl = ovid->overlays[j];
  1418. struct omap_dss_device *dssdev = ovl->get_device(ovl);
  1419. if (dssdev) {
  1420. ret = ovl->enable(ovl);
  1421. if (ret)
  1422. goto streamon_err1;
  1423. }
  1424. }
  1425. ret = 0;
  1426. streamon_err1:
  1427. if (ret)
  1428. ret = videobuf_streamoff(q);
  1429. streamon_err:
  1430. mutex_unlock(&vout->lock);
  1431. return ret;
  1432. }
  1433. static int vidioc_streamoff(struct file *file, void *fh, enum v4l2_buf_type i)
  1434. {
  1435. u32 mask = 0;
  1436. int ret = 0, j;
  1437. struct omap_vout_device *vout = fh;
  1438. struct omapvideo_info *ovid = &vout->vid_info;
  1439. if (!vout->streaming)
  1440. return -EINVAL;
  1441. vout->streaming = 0;
  1442. mask = DISPC_IRQ_VSYNC | DISPC_IRQ_EVSYNC_EVEN | DISPC_IRQ_EVSYNC_ODD
  1443. | DISPC_IRQ_VSYNC2;
  1444. omap_dispc_unregister_isr(omap_vout_isr, vout, mask);
  1445. for (j = 0; j < ovid->num_overlays; j++) {
  1446. struct omap_overlay *ovl = ovid->overlays[j];
  1447. struct omap_dss_device *dssdev = ovl->get_device(ovl);
  1448. if (dssdev)
  1449. ovl->disable(ovl);
  1450. }
  1451. /* Turn of the pipeline */
  1452. ret = omapvid_apply_changes(vout);
  1453. if (ret)
  1454. v4l2_err(&vout->vid_dev->v4l2_dev, "failed to change mode in"
  1455. " streamoff\n");
  1456. INIT_LIST_HEAD(&vout->dma_queue);
  1457. ret = videobuf_streamoff(&vout->vbq);
  1458. return ret;
  1459. }
  1460. static int vidioc_s_fbuf(struct file *file, void *fh,
  1461. const struct v4l2_framebuffer *a)
  1462. {
  1463. int enable = 0;
  1464. struct omap_overlay *ovl;
  1465. struct omapvideo_info *ovid;
  1466. struct omap_vout_device *vout = fh;
  1467. struct omap_overlay_manager_info info;
  1468. enum omap_dss_trans_key_type key_type = OMAP_DSS_COLOR_KEY_GFX_DST;
  1469. ovid = &vout->vid_info;
  1470. ovl = ovid->overlays[0];
  1471. /* OMAP DSS doesn't support Source and Destination color
  1472. key together */
  1473. if ((a->flags & V4L2_FBUF_FLAG_SRC_CHROMAKEY) &&
  1474. (a->flags & V4L2_FBUF_FLAG_CHROMAKEY))
  1475. return -EINVAL;
  1476. /* OMAP DSS Doesn't support the Destination color key
  1477. and alpha blending together */
  1478. if ((a->flags & V4L2_FBUF_FLAG_CHROMAKEY) &&
  1479. (a->flags & V4L2_FBUF_FLAG_LOCAL_ALPHA))
  1480. return -EINVAL;
  1481. if ((a->flags & V4L2_FBUF_FLAG_SRC_CHROMAKEY)) {
  1482. vout->fbuf.flags |= V4L2_FBUF_FLAG_SRC_CHROMAKEY;
  1483. key_type = OMAP_DSS_COLOR_KEY_VID_SRC;
  1484. } else
  1485. vout->fbuf.flags &= ~V4L2_FBUF_FLAG_SRC_CHROMAKEY;
  1486. if ((a->flags & V4L2_FBUF_FLAG_CHROMAKEY)) {
  1487. vout->fbuf.flags |= V4L2_FBUF_FLAG_CHROMAKEY;
  1488. key_type = OMAP_DSS_COLOR_KEY_GFX_DST;
  1489. } else
  1490. vout->fbuf.flags &= ~V4L2_FBUF_FLAG_CHROMAKEY;
  1491. if (a->flags & (V4L2_FBUF_FLAG_CHROMAKEY |
  1492. V4L2_FBUF_FLAG_SRC_CHROMAKEY))
  1493. enable = 1;
  1494. else
  1495. enable = 0;
  1496. if (ovl->manager && ovl->manager->get_manager_info &&
  1497. ovl->manager->set_manager_info) {
  1498. ovl->manager->get_manager_info(ovl->manager, &info);
  1499. info.trans_enabled = enable;
  1500. info.trans_key_type = key_type;
  1501. info.trans_key = vout->win.chromakey;
  1502. if (ovl->manager->set_manager_info(ovl->manager, &info))
  1503. return -EINVAL;
  1504. }
  1505. if (a->flags & V4L2_FBUF_FLAG_LOCAL_ALPHA) {
  1506. vout->fbuf.flags |= V4L2_FBUF_FLAG_LOCAL_ALPHA;
  1507. enable = 1;
  1508. } else {
  1509. vout->fbuf.flags &= ~V4L2_FBUF_FLAG_LOCAL_ALPHA;
  1510. enable = 0;
  1511. }
  1512. if (ovl->manager && ovl->manager->get_manager_info &&
  1513. ovl->manager->set_manager_info) {
  1514. ovl->manager->get_manager_info(ovl->manager, &info);
  1515. /* enable this only if there is no zorder cap */
  1516. if ((ovl->caps & OMAP_DSS_OVL_CAP_ZORDER) == 0)
  1517. info.partial_alpha_enabled = enable;
  1518. if (ovl->manager->set_manager_info(ovl->manager, &info))
  1519. return -EINVAL;
  1520. }
  1521. return 0;
  1522. }
  1523. static int vidioc_g_fbuf(struct file *file, void *fh,
  1524. struct v4l2_framebuffer *a)
  1525. {
  1526. struct omap_overlay *ovl;
  1527. struct omapvideo_info *ovid;
  1528. struct omap_vout_device *vout = fh;
  1529. struct omap_overlay_manager_info info;
  1530. ovid = &vout->vid_info;
  1531. ovl = ovid->overlays[0];
  1532. /* The video overlay must stay within the framebuffer and can't be
  1533. positioned independently. */
  1534. a->flags = V4L2_FBUF_FLAG_OVERLAY;
  1535. a->capability = V4L2_FBUF_CAP_LOCAL_ALPHA | V4L2_FBUF_CAP_CHROMAKEY
  1536. | V4L2_FBUF_CAP_SRC_CHROMAKEY;
  1537. if (ovl->manager && ovl->manager->get_manager_info) {
  1538. ovl->manager->get_manager_info(ovl->manager, &info);
  1539. if (info.trans_key_type == OMAP_DSS_COLOR_KEY_VID_SRC)
  1540. a->flags |= V4L2_FBUF_FLAG_SRC_CHROMAKEY;
  1541. if (info.trans_key_type == OMAP_DSS_COLOR_KEY_GFX_DST)
  1542. a->flags |= V4L2_FBUF_FLAG_CHROMAKEY;
  1543. }
  1544. if (ovl->manager && ovl->manager->get_manager_info) {
  1545. ovl->manager->get_manager_info(ovl->manager, &info);
  1546. if (info.partial_alpha_enabled)
  1547. a->flags |= V4L2_FBUF_FLAG_LOCAL_ALPHA;
  1548. }
  1549. return 0;
  1550. }
  1551. static const struct v4l2_ioctl_ops vout_ioctl_ops = {
  1552. .vidioc_querycap = vidioc_querycap,
  1553. .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
  1554. .vidioc_g_fmt_vid_out = vidioc_g_fmt_vid_out,
  1555. .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out,
  1556. .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
  1557. .vidioc_queryctrl = vidioc_queryctrl,
  1558. .vidioc_g_ctrl = vidioc_g_ctrl,
  1559. .vidioc_s_fbuf = vidioc_s_fbuf,
  1560. .vidioc_g_fbuf = vidioc_g_fbuf,
  1561. .vidioc_s_ctrl = vidioc_s_ctrl,
  1562. .vidioc_try_fmt_vid_overlay = vidioc_try_fmt_vid_overlay,
  1563. .vidioc_s_fmt_vid_overlay = vidioc_s_fmt_vid_overlay,
  1564. .vidioc_enum_fmt_vid_overlay = vidioc_enum_fmt_vid_overlay,
  1565. .vidioc_g_fmt_vid_overlay = vidioc_g_fmt_vid_overlay,
  1566. .vidioc_cropcap = vidioc_cropcap,
  1567. .vidioc_g_crop = vidioc_g_crop,
  1568. .vidioc_s_crop = vidioc_s_crop,
  1569. .vidioc_reqbufs = vidioc_reqbufs,
  1570. .vidioc_querybuf = vidioc_querybuf,
  1571. .vidioc_qbuf = vidioc_qbuf,
  1572. .vidioc_dqbuf = vidioc_dqbuf,
  1573. .vidioc_streamon = vidioc_streamon,
  1574. .vidioc_streamoff = vidioc_streamoff,
  1575. };
  1576. static const struct v4l2_file_operations omap_vout_fops = {
  1577. .owner = THIS_MODULE,
  1578. .poll = omap_vout_poll,
  1579. .unlocked_ioctl = video_ioctl2,
  1580. .mmap = omap_vout_mmap,
  1581. .open = omap_vout_open,
  1582. .release = omap_vout_release,
  1583. };
  1584. /* Init functions used during driver initialization */
  1585. /* Initial setup of video_data */
  1586. static int __init omap_vout_setup_video_data(struct omap_vout_device *vout)
  1587. {
  1588. struct video_device *vfd;
  1589. struct v4l2_pix_format *pix;
  1590. struct v4l2_control *control;
  1591. struct omap_overlay *ovl = vout->vid_info.overlays[0];
  1592. struct omap_dss_device *display = ovl->get_device(ovl);
  1593. /* set the default pix */
  1594. pix = &vout->pix;
  1595. /* Set the default picture of QVGA */
  1596. pix->width = QQVGA_WIDTH;
  1597. pix->height = QQVGA_HEIGHT;
  1598. /* Default pixel format is RGB 5-6-5 */
  1599. pix->pixelformat = V4L2_PIX_FMT_RGB565;
  1600. pix->field = V4L2_FIELD_ANY;
  1601. pix->bytesperline = pix->width * 2;
  1602. pix->sizeimage = pix->bytesperline * pix->height;
  1603. pix->priv = 0;
  1604. pix->colorspace = V4L2_COLORSPACE_JPEG;
  1605. vout->bpp = RGB565_BPP;
  1606. vout->fbuf.fmt.width = display->panel.timings.x_res;
  1607. vout->fbuf.fmt.height = display->panel.timings.y_res;
  1608. /* Set the data structures for the overlay parameters*/
  1609. vout->win.global_alpha = 255;
  1610. vout->fbuf.flags = 0;
  1611. vout->fbuf.capability = V4L2_FBUF_CAP_LOCAL_ALPHA |
  1612. V4L2_FBUF_CAP_SRC_CHROMAKEY | V4L2_FBUF_CAP_CHROMAKEY;
  1613. vout->win.chromakey = 0;
  1614. omap_vout_new_format(pix, &vout->fbuf, &vout->crop, &vout->win);
  1615. /*Initialize the control variables for
  1616. rotation, flipping and background color. */
  1617. control = vout->control;
  1618. control[0].id = V4L2_CID_ROTATE;
  1619. control[0].value = 0;
  1620. vout->rotation = 0;
  1621. vout->mirror = 0;
  1622. vout->control[2].id = V4L2_CID_HFLIP;
  1623. vout->control[2].value = 0;
  1624. if (vout->vid_info.rotation_type == VOUT_ROT_VRFB)
  1625. vout->vrfb_bpp = 2;
  1626. control[1].id = V4L2_CID_BG_COLOR;
  1627. control[1].value = 0;
  1628. /* initialize the video_device struct */
  1629. vfd = vout->vfd = video_device_alloc();
  1630. if (!vfd) {
  1631. printk(KERN_ERR VOUT_NAME ": could not allocate"
  1632. " video device struct\n");
  1633. return -ENOMEM;
  1634. }
  1635. vfd->release = video_device_release;
  1636. vfd->ioctl_ops = &vout_ioctl_ops;
  1637. strlcpy(vfd->name, VOUT_NAME, sizeof(vfd->name));
  1638. vfd->fops = &omap_vout_fops;
  1639. vfd->v4l2_dev = &vout->vid_dev->v4l2_dev;
  1640. vfd->vfl_dir = VFL_DIR_TX;
  1641. mutex_init(&vout->lock);
  1642. vfd->minor = -1;
  1643. return 0;
  1644. }
  1645. /* Setup video buffers */
  1646. static int __init omap_vout_setup_video_bufs(struct platform_device *pdev,
  1647. int vid_num)
  1648. {
  1649. u32 numbuffers;
  1650. int ret = 0, i;
  1651. struct omapvideo_info *ovid;
  1652. struct omap_vout_device *vout;
  1653. struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
  1654. struct omap2video_device *vid_dev =
  1655. container_of(v4l2_dev, struct omap2video_device, v4l2_dev);
  1656. vout = vid_dev->vouts[vid_num];
  1657. ovid = &vout->vid_info;
  1658. numbuffers = (vid_num == 0) ? video1_numbuffers : video2_numbuffers;
  1659. vout->buffer_size = (vid_num == 0) ? video1_bufsize : video2_bufsize;
  1660. dev_info(&pdev->dev, "Buffer Size = %d\n", vout->buffer_size);
  1661. for (i = 0; i < numbuffers; i++) {
  1662. vout->buf_virt_addr[i] =
  1663. omap_vout_alloc_buffer(vout->buffer_size,
  1664. (u32 *) &vout->buf_phy_addr[i]);
  1665. if (!vout->buf_virt_addr[i]) {
  1666. numbuffers = i;
  1667. ret = -ENOMEM;
  1668. goto free_buffers;
  1669. }
  1670. }
  1671. vout->cropped_offset = 0;
  1672. if (ovid->rotation_type == VOUT_ROT_VRFB) {
  1673. int static_vrfb_allocation = (vid_num == 0) ?
  1674. vid1_static_vrfb_alloc : vid2_static_vrfb_alloc;
  1675. ret = omap_vout_setup_vrfb_bufs(pdev, vid_num,
  1676. static_vrfb_allocation);
  1677. }
  1678. return ret;
  1679. free_buffers:
  1680. for (i = 0; i < numbuffers; i++) {
  1681. omap_vout_free_buffer(vout->buf_virt_addr[i],
  1682. vout->buffer_size);
  1683. vout->buf_virt_addr[i] = 0;
  1684. vout->buf_phy_addr[i] = 0;
  1685. }
  1686. return ret;
  1687. }
  1688. /* Create video out devices */
  1689. static int __init omap_vout_create_video_devices(struct platform_device *pdev)
  1690. {
  1691. int ret = 0, k;
  1692. struct omap_vout_device *vout;
  1693. struct video_device *vfd = NULL;
  1694. struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
  1695. struct omap2video_device *vid_dev = container_of(v4l2_dev,
  1696. struct omap2video_device, v4l2_dev);
  1697. for (k = 0; k < pdev->num_resources; k++) {
  1698. vout = kzalloc(sizeof(struct omap_vout_device), GFP_KERNEL);
  1699. if (!vout) {
  1700. dev_err(&pdev->dev, ": could not allocate memory\n");
  1701. return -ENOMEM;
  1702. }
  1703. vout->vid = k;
  1704. vid_dev->vouts[k] = vout;
  1705. vout->vid_dev = vid_dev;
  1706. /* Select video2 if only 1 overlay is controlled by V4L2 */
  1707. if (pdev->num_resources == 1)
  1708. vout->vid_info.overlays[0] = vid_dev->overlays[k + 2];
  1709. else
  1710. /* Else select video1 and video2 one by one. */
  1711. vout->vid_info.overlays[0] = vid_dev->overlays[k + 1];
  1712. vout->vid_info.num_overlays = 1;
  1713. vout->vid_info.id = k + 1;
  1714. /* Set VRFB as rotation_type for omap2 and omap3 */
  1715. if (omap_vout_dss_omap24xx() || omap_vout_dss_omap34xx())
  1716. vout->vid_info.rotation_type = VOUT_ROT_VRFB;
  1717. /* Setup the default configuration for the video devices
  1718. */
  1719. if (omap_vout_setup_video_data(vout) != 0) {
  1720. ret = -ENOMEM;
  1721. goto error;
  1722. }
  1723. /* Allocate default number of buffers for the video streaming
  1724. * and reserve the VRFB space for rotation
  1725. */
  1726. if (omap_vout_setup_video_bufs(pdev, k) != 0) {
  1727. ret = -ENOMEM;
  1728. goto error1;
  1729. }
  1730. /* Register the Video device with V4L2
  1731. */
  1732. vfd = vout->vfd;
  1733. if (video_register_device(vfd, VFL_TYPE_GRABBER, -1) < 0) {
  1734. dev_err(&pdev->dev, ": Could not register "
  1735. "Video for Linux device\n");
  1736. vfd->minor = -1;
  1737. ret = -ENODEV;
  1738. goto error2;
  1739. }
  1740. video_set_drvdata(vfd, vout);
  1741. dev_info(&pdev->dev, ": registered and initialized"
  1742. " video device %d\n", vfd->minor);
  1743. if (k == (pdev->num_resources - 1))
  1744. return 0;
  1745. continue;
  1746. error2:
  1747. if (vout->vid_info.rotation_type == VOUT_ROT_VRFB)
  1748. omap_vout_release_vrfb(vout);
  1749. omap_vout_free_buffers(vout);
  1750. error1:
  1751. video_device_release(vfd);
  1752. error:
  1753. kfree(vout);
  1754. return ret;
  1755. }
  1756. return -ENODEV;
  1757. }
  1758. /* Driver functions */
  1759. static void omap_vout_cleanup_device(struct omap_vout_device *vout)
  1760. {
  1761. struct video_device *vfd;
  1762. struct omapvideo_info *ovid;
  1763. if (!vout)
  1764. return;
  1765. vfd = vout->vfd;
  1766. ovid = &vout->vid_info;
  1767. if (vfd) {
  1768. if (!video_is_registered(vfd)) {
  1769. /*
  1770. * The device was never registered, so release the
  1771. * video_device struct directly.
  1772. */
  1773. video_device_release(vfd);
  1774. } else {
  1775. /*
  1776. * The unregister function will release the video_device
  1777. * struct as well as unregistering it.
  1778. */
  1779. video_unregister_device(vfd);
  1780. }
  1781. }
  1782. if (ovid->rotation_type == VOUT_ROT_VRFB) {
  1783. omap_vout_release_vrfb(vout);
  1784. /* Free the VRFB buffer if allocated
  1785. * init time
  1786. */
  1787. if (vout->vrfb_static_allocation)
  1788. omap_vout_free_vrfb_buffers(vout);
  1789. }
  1790. omap_vout_free_buffers(vout);
  1791. kfree(vout);
  1792. }
  1793. static int omap_vout_remove(struct platform_device *pdev)
  1794. {
  1795. int k;
  1796. struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
  1797. struct omap2video_device *vid_dev = container_of(v4l2_dev, struct
  1798. omap2video_device, v4l2_dev);
  1799. v4l2_device_unregister(v4l2_dev);
  1800. for (k = 0; k < pdev->num_resources; k++)
  1801. omap_vout_cleanup_device(vid_dev->vouts[k]);
  1802. for (k = 0; k < vid_dev->num_displays; k++) {
  1803. if (vid_dev->displays[k]->state != OMAP_DSS_DISPLAY_DISABLED)
  1804. vid_dev->displays[k]->driver->disable(vid_dev->displays[k]);
  1805. omap_dss_put_device(vid_dev->displays[k]);
  1806. }
  1807. kfree(vid_dev);
  1808. return 0;
  1809. }
  1810. static int __init omap_vout_probe(struct platform_device *pdev)
  1811. {
  1812. int ret = 0, i;
  1813. struct omap_overlay *ovl;
  1814. struct omap_dss_device *dssdev = NULL;
  1815. struct omap_dss_device *def_display;
  1816. struct omap2video_device *vid_dev = NULL;
  1817. ret = omapdss_compat_init();
  1818. if (ret) {
  1819. dev_err(&pdev->dev, "failed to init dss\n");
  1820. return ret;
  1821. }
  1822. if (pdev->num_resources == 0) {
  1823. dev_err(&pdev->dev, "probed for an unknown device\n");
  1824. ret = -ENODEV;
  1825. goto err_dss_init;
  1826. }
  1827. vid_dev = kzalloc(sizeof(struct omap2video_device), GFP_KERNEL);
  1828. if (vid_dev == NULL) {
  1829. ret = -ENOMEM;
  1830. goto err_dss_init;
  1831. }
  1832. vid_dev->num_displays = 0;
  1833. for_each_dss_dev(dssdev) {
  1834. omap_dss_get_device(dssdev);
  1835. if (!dssdev->driver) {
  1836. dev_warn(&pdev->dev, "no driver for display: %s\n",
  1837. dssdev->name);
  1838. omap_dss_put_device(dssdev);
  1839. continue;
  1840. }
  1841. vid_dev->displays[vid_dev->num_displays++] = dssdev;
  1842. }
  1843. if (vid_dev->num_displays == 0) {
  1844. dev_err(&pdev->dev, "no displays\n");
  1845. ret = -EINVAL;
  1846. goto probe_err0;
  1847. }
  1848. vid_dev->num_overlays = omap_dss_get_num_overlays();
  1849. for (i = 0; i < vid_dev->num_overlays; i++)
  1850. vid_dev->overlays[i] = omap_dss_get_overlay(i);
  1851. vid_dev->num_managers = omap_dss_get_num_overlay_managers();
  1852. for (i = 0; i < vid_dev->num_managers; i++)
  1853. vid_dev->managers[i] = omap_dss_get_overlay_manager(i);
  1854. /* Get the Video1 overlay and video2 overlay.
  1855. * Setup the Display attached to that overlays
  1856. */
  1857. for (i = 1; i < vid_dev->num_overlays; i++) {
  1858. ovl = omap_dss_get_overlay(i);
  1859. dssdev = ovl->get_device(ovl);
  1860. if (dssdev) {
  1861. def_display = dssdev;
  1862. } else {
  1863. dev_warn(&pdev->dev, "cannot find display\n");
  1864. def_display = NULL;
  1865. }
  1866. if (def_display) {
  1867. struct omap_dss_driver *dssdrv = def_display->driver;
  1868. ret = dssdrv->enable(def_display);
  1869. if (ret) {
  1870. /* Here we are not considering a error
  1871. * as display may be enabled by frame
  1872. * buffer driver
  1873. */
  1874. dev_warn(&pdev->dev,
  1875. "'%s' Display already enabled\n",
  1876. def_display->name);
  1877. }
  1878. }
  1879. }
  1880. if (v4l2_device_register(&pdev->dev, &vid_dev->v4l2_dev) < 0) {
  1881. dev_err(&pdev->dev, "v4l2_device_register failed\n");
  1882. ret = -ENODEV;
  1883. goto probe_err1;
  1884. }
  1885. ret = omap_vout_create_video_devices(pdev);
  1886. if (ret)
  1887. goto probe_err2;
  1888. for (i = 0; i < vid_dev->num_displays; i++) {
  1889. struct omap_dss_device *display = vid_dev->displays[i];
  1890. if (display->driver->update)
  1891. display->driver->update(display, 0, 0,
  1892. display->panel.timings.x_res,
  1893. display->panel.timings.y_res);
  1894. }
  1895. return 0;
  1896. probe_err2:
  1897. v4l2_device_unregister(&vid_dev->v4l2_dev);
  1898. probe_err1:
  1899. for (i = 1; i < vid_dev->num_overlays; i++) {
  1900. def_display = NULL;
  1901. ovl = omap_dss_get_overlay(i);
  1902. dssdev = ovl->get_device(ovl);
  1903. if (dssdev)
  1904. def_display = dssdev;
  1905. if (def_display && def_display->driver)
  1906. def_display->driver->disable(def_display);
  1907. }
  1908. probe_err0:
  1909. kfree(vid_dev);
  1910. err_dss_init:
  1911. omapdss_compat_uninit();
  1912. return ret;
  1913. }
  1914. static struct platform_driver omap_vout_driver = {
  1915. .driver = {
  1916. .name = VOUT_NAME,
  1917. },
  1918. .remove = omap_vout_remove,
  1919. };
  1920. static int __init omap_vout_init(void)
  1921. {
  1922. if (platform_driver_probe(&omap_vout_driver, omap_vout_probe) != 0) {
  1923. printk(KERN_ERR VOUT_NAME ":Could not register Video driver\n");
  1924. return -EINVAL;
  1925. }
  1926. return 0;
  1927. }
  1928. static void omap_vout_cleanup(void)
  1929. {
  1930. platform_driver_unregister(&omap_vout_driver);
  1931. }
  1932. late_initcall(omap_vout_init);
  1933. module_exit(omap_vout_cleanup);