omap_vout.c 56 KB

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