omap_vout.c 56 KB

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