omap_vout.c 57 KB

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