omap_vout.c 56 KB

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