omap_vout.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224
  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/omapfb_dss.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, FOLL_WRITE, 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_g_selection(struct file *file, void *fh, struct v4l2_selection *sel)
  1048. {
  1049. struct omap_vout_device *vout = fh;
  1050. struct v4l2_pix_format *pix = &vout->pix;
  1051. if (sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  1052. return -EINVAL;
  1053. switch (sel->target) {
  1054. case V4L2_SEL_TGT_CROP:
  1055. sel->r = vout->crop;
  1056. break;
  1057. case V4L2_SEL_TGT_CROP_DEFAULT:
  1058. omap_vout_default_crop(&vout->pix, &vout->fbuf, &sel->r);
  1059. break;
  1060. case V4L2_SEL_TGT_CROP_BOUNDS:
  1061. /* Width and height are always even */
  1062. sel->r.width = pix->width & ~1;
  1063. sel->r.height = pix->height & ~1;
  1064. break;
  1065. default:
  1066. return -EINVAL;
  1067. }
  1068. return 0;
  1069. }
  1070. static int vidioc_s_selection(struct file *file, void *fh, struct v4l2_selection *sel)
  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 (sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  1079. return -EINVAL;
  1080. if (sel->target != V4L2_SEL_TGT_CROP)
  1081. return -EINVAL;
  1082. if (vout->streaming)
  1083. return -EBUSY;
  1084. mutex_lock(&vout->lock);
  1085. ovid = &vout->vid_info;
  1086. ovl = ovid->overlays[0];
  1087. /* get the display device attached to the overlay */
  1088. dssdev = ovl->get_device(ovl);
  1089. if (!dssdev) {
  1090. ret = -EINVAL;
  1091. goto s_crop_err;
  1092. }
  1093. timing = &dssdev->panel.timings;
  1094. if (is_rotation_90_or_270(vout)) {
  1095. vout->fbuf.fmt.height = timing->x_res;
  1096. vout->fbuf.fmt.width = timing->y_res;
  1097. } else {
  1098. vout->fbuf.fmt.height = timing->y_res;
  1099. vout->fbuf.fmt.width = timing->x_res;
  1100. }
  1101. ret = omap_vout_new_crop(&vout->pix, &vout->crop, &vout->win,
  1102. &vout->fbuf, &sel->r);
  1103. s_crop_err:
  1104. mutex_unlock(&vout->lock);
  1105. return ret;
  1106. }
  1107. static int omap_vout_s_ctrl(struct v4l2_ctrl *ctrl)
  1108. {
  1109. struct omap_vout_device *vout =
  1110. container_of(ctrl->handler, struct omap_vout_device, ctrl_handler);
  1111. int ret = 0;
  1112. switch (ctrl->id) {
  1113. case V4L2_CID_ROTATE: {
  1114. struct omapvideo_info *ovid;
  1115. int rotation = ctrl->val;
  1116. ovid = &vout->vid_info;
  1117. mutex_lock(&vout->lock);
  1118. if (rotation && ovid->rotation_type == VOUT_ROT_NONE) {
  1119. mutex_unlock(&vout->lock);
  1120. ret = -ERANGE;
  1121. break;
  1122. }
  1123. if (rotation && vout->pix.pixelformat == V4L2_PIX_FMT_RGB24) {
  1124. mutex_unlock(&vout->lock);
  1125. ret = -EINVAL;
  1126. break;
  1127. }
  1128. if (v4l2_rot_to_dss_rot(rotation, &vout->rotation,
  1129. vout->mirror)) {
  1130. mutex_unlock(&vout->lock);
  1131. ret = -EINVAL;
  1132. break;
  1133. }
  1134. mutex_unlock(&vout->lock);
  1135. break;
  1136. }
  1137. case V4L2_CID_BG_COLOR:
  1138. {
  1139. struct omap_overlay *ovl;
  1140. unsigned int color = ctrl->val;
  1141. struct omap_overlay_manager_info info;
  1142. ovl = vout->vid_info.overlays[0];
  1143. mutex_lock(&vout->lock);
  1144. if (!ovl->manager || !ovl->manager->get_manager_info) {
  1145. mutex_unlock(&vout->lock);
  1146. ret = -EINVAL;
  1147. break;
  1148. }
  1149. ovl->manager->get_manager_info(ovl->manager, &info);
  1150. info.default_color = color;
  1151. if (ovl->manager->set_manager_info(ovl->manager, &info)) {
  1152. mutex_unlock(&vout->lock);
  1153. ret = -EINVAL;
  1154. break;
  1155. }
  1156. mutex_unlock(&vout->lock);
  1157. break;
  1158. }
  1159. case V4L2_CID_VFLIP:
  1160. {
  1161. struct omapvideo_info *ovid;
  1162. unsigned int mirror = ctrl->val;
  1163. ovid = &vout->vid_info;
  1164. mutex_lock(&vout->lock);
  1165. if (mirror && ovid->rotation_type == VOUT_ROT_NONE) {
  1166. mutex_unlock(&vout->lock);
  1167. ret = -ERANGE;
  1168. break;
  1169. }
  1170. if (mirror && vout->pix.pixelformat == V4L2_PIX_FMT_RGB24) {
  1171. mutex_unlock(&vout->lock);
  1172. ret = -EINVAL;
  1173. break;
  1174. }
  1175. vout->mirror = mirror;
  1176. mutex_unlock(&vout->lock);
  1177. break;
  1178. }
  1179. default:
  1180. return -EINVAL;
  1181. }
  1182. return ret;
  1183. }
  1184. static const struct v4l2_ctrl_ops omap_vout_ctrl_ops = {
  1185. .s_ctrl = omap_vout_s_ctrl,
  1186. };
  1187. static int vidioc_reqbufs(struct file *file, void *fh,
  1188. struct v4l2_requestbuffers *req)
  1189. {
  1190. int ret = 0;
  1191. unsigned int i, num_buffers = 0;
  1192. struct omap_vout_device *vout = fh;
  1193. struct videobuf_queue *q = &vout->vbq;
  1194. if (req->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  1195. return -EINVAL;
  1196. /* if memory is not mmp or userptr
  1197. return error */
  1198. if ((V4L2_MEMORY_MMAP != req->memory) &&
  1199. (V4L2_MEMORY_USERPTR != req->memory))
  1200. return -EINVAL;
  1201. mutex_lock(&vout->lock);
  1202. /* Cannot be requested when streaming is on */
  1203. if (vout->streaming) {
  1204. ret = -EBUSY;
  1205. goto reqbuf_err;
  1206. }
  1207. /* If buffers are already allocated free them */
  1208. if (q->bufs[0] && (V4L2_MEMORY_MMAP == q->bufs[0]->memory)) {
  1209. if (vout->mmap_count) {
  1210. ret = -EBUSY;
  1211. goto reqbuf_err;
  1212. }
  1213. num_buffers = (vout->vid == OMAP_VIDEO1) ?
  1214. video1_numbuffers : video2_numbuffers;
  1215. for (i = num_buffers; i < vout->buffer_allocated; i++) {
  1216. omap_vout_free_buffer(vout->buf_virt_addr[i],
  1217. vout->buffer_size);
  1218. vout->buf_virt_addr[i] = 0;
  1219. vout->buf_phy_addr[i] = 0;
  1220. }
  1221. vout->buffer_allocated = num_buffers;
  1222. videobuf_mmap_free(q);
  1223. } else if (q->bufs[0] && (V4L2_MEMORY_USERPTR == q->bufs[0]->memory)) {
  1224. if (vout->buffer_allocated) {
  1225. videobuf_mmap_free(q);
  1226. for (i = 0; i < vout->buffer_allocated; i++) {
  1227. kfree(q->bufs[i]);
  1228. q->bufs[i] = NULL;
  1229. }
  1230. vout->buffer_allocated = 0;
  1231. }
  1232. }
  1233. /*store the memory type in data structure */
  1234. vout->memory = req->memory;
  1235. INIT_LIST_HEAD(&vout->dma_queue);
  1236. /* call videobuf_reqbufs api */
  1237. ret = videobuf_reqbufs(q, req);
  1238. if (ret < 0)
  1239. goto reqbuf_err;
  1240. vout->buffer_allocated = req->count;
  1241. reqbuf_err:
  1242. mutex_unlock(&vout->lock);
  1243. return ret;
  1244. }
  1245. static int vidioc_querybuf(struct file *file, void *fh,
  1246. struct v4l2_buffer *b)
  1247. {
  1248. struct omap_vout_device *vout = fh;
  1249. return videobuf_querybuf(&vout->vbq, b);
  1250. }
  1251. static int vidioc_qbuf(struct file *file, void *fh,
  1252. struct v4l2_buffer *buffer)
  1253. {
  1254. struct omap_vout_device *vout = fh;
  1255. struct videobuf_queue *q = &vout->vbq;
  1256. if ((V4L2_BUF_TYPE_VIDEO_OUTPUT != buffer->type) ||
  1257. (buffer->index >= vout->buffer_allocated) ||
  1258. (q->bufs[buffer->index]->memory != buffer->memory)) {
  1259. return -EINVAL;
  1260. }
  1261. if (V4L2_MEMORY_USERPTR == buffer->memory) {
  1262. if ((buffer->length < vout->pix.sizeimage) ||
  1263. (0 == buffer->m.userptr)) {
  1264. return -EINVAL;
  1265. }
  1266. }
  1267. if ((is_rotation_enabled(vout)) &&
  1268. vout->vrfb_dma_tx.req_status == DMA_CHAN_NOT_ALLOTED) {
  1269. v4l2_warn(&vout->vid_dev->v4l2_dev,
  1270. "DMA Channel not allocated for Rotation\n");
  1271. return -EINVAL;
  1272. }
  1273. return videobuf_qbuf(q, buffer);
  1274. }
  1275. static int vidioc_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
  1276. {
  1277. struct omap_vout_device *vout = fh;
  1278. struct videobuf_queue *q = &vout->vbq;
  1279. int ret;
  1280. u32 addr;
  1281. unsigned long size;
  1282. struct videobuf_buffer *vb;
  1283. vb = q->bufs[b->index];
  1284. if (!vout->streaming)
  1285. return -EINVAL;
  1286. if (file->f_flags & O_NONBLOCK)
  1287. /* Call videobuf_dqbuf for non blocking mode */
  1288. ret = videobuf_dqbuf(q, (struct v4l2_buffer *)b, 1);
  1289. else
  1290. /* Call videobuf_dqbuf for blocking mode */
  1291. ret = videobuf_dqbuf(q, (struct v4l2_buffer *)b, 0);
  1292. addr = (unsigned long) vout->buf_phy_addr[vb->i];
  1293. size = (unsigned long) vb->size;
  1294. dma_unmap_single(vout->vid_dev->v4l2_dev.dev, addr,
  1295. size, DMA_TO_DEVICE);
  1296. return ret;
  1297. }
  1298. static int vidioc_streamon(struct file *file, void *fh, enum v4l2_buf_type i)
  1299. {
  1300. int ret = 0, j;
  1301. u32 addr = 0, mask = 0;
  1302. struct omap_vout_device *vout = fh;
  1303. struct videobuf_queue *q = &vout->vbq;
  1304. struct omapvideo_info *ovid = &vout->vid_info;
  1305. mutex_lock(&vout->lock);
  1306. if (vout->streaming) {
  1307. ret = -EBUSY;
  1308. goto streamon_err;
  1309. }
  1310. ret = videobuf_streamon(q);
  1311. if (ret)
  1312. goto streamon_err;
  1313. if (list_empty(&vout->dma_queue)) {
  1314. ret = -EIO;
  1315. goto streamon_err1;
  1316. }
  1317. /* Get the next frame from the buffer queue */
  1318. vout->next_frm = vout->cur_frm = list_entry(vout->dma_queue.next,
  1319. struct videobuf_buffer, queue);
  1320. /* Remove buffer from the buffer queue */
  1321. list_del(&vout->cur_frm->queue);
  1322. /* Mark state of the current frame to active */
  1323. vout->cur_frm->state = VIDEOBUF_ACTIVE;
  1324. /* Initialize field_id and started member */
  1325. vout->field_id = 0;
  1326. /* set flag here. Next QBUF will start DMA */
  1327. vout->streaming = true;
  1328. vout->first_int = 1;
  1329. if (omap_vout_calculate_offset(vout)) {
  1330. ret = -EINVAL;
  1331. goto streamon_err1;
  1332. }
  1333. addr = (unsigned long) vout->queued_buf_addr[vout->cur_frm->i]
  1334. + vout->cropped_offset;
  1335. mask = DISPC_IRQ_VSYNC | DISPC_IRQ_EVSYNC_EVEN | DISPC_IRQ_EVSYNC_ODD
  1336. | DISPC_IRQ_VSYNC2;
  1337. /* First save the configuration in ovelray structure */
  1338. ret = omapvid_init(vout, addr);
  1339. if (ret) {
  1340. v4l2_err(&vout->vid_dev->v4l2_dev,
  1341. "failed to set overlay info\n");
  1342. goto streamon_err1;
  1343. }
  1344. omap_dispc_register_isr(omap_vout_isr, vout, mask);
  1345. /* Enable the pipeline and set the Go bit */
  1346. ret = omapvid_apply_changes(vout);
  1347. if (ret)
  1348. v4l2_err(&vout->vid_dev->v4l2_dev, "failed to change mode\n");
  1349. for (j = 0; j < ovid->num_overlays; j++) {
  1350. struct omap_overlay *ovl = ovid->overlays[j];
  1351. struct omap_dss_device *dssdev = ovl->get_device(ovl);
  1352. if (dssdev) {
  1353. ret = ovl->enable(ovl);
  1354. if (ret)
  1355. goto streamon_err1;
  1356. }
  1357. }
  1358. ret = 0;
  1359. streamon_err1:
  1360. if (ret)
  1361. ret = videobuf_streamoff(q);
  1362. streamon_err:
  1363. mutex_unlock(&vout->lock);
  1364. return ret;
  1365. }
  1366. static int vidioc_streamoff(struct file *file, void *fh, enum v4l2_buf_type i)
  1367. {
  1368. u32 mask = 0;
  1369. int ret = 0, j;
  1370. struct omap_vout_device *vout = fh;
  1371. struct omapvideo_info *ovid = &vout->vid_info;
  1372. if (!vout->streaming)
  1373. return -EINVAL;
  1374. vout->streaming = false;
  1375. mask = DISPC_IRQ_VSYNC | DISPC_IRQ_EVSYNC_EVEN | DISPC_IRQ_EVSYNC_ODD
  1376. | DISPC_IRQ_VSYNC2;
  1377. omap_dispc_unregister_isr(omap_vout_isr, vout, mask);
  1378. for (j = 0; j < ovid->num_overlays; j++) {
  1379. struct omap_overlay *ovl = ovid->overlays[j];
  1380. struct omap_dss_device *dssdev = ovl->get_device(ovl);
  1381. if (dssdev)
  1382. ovl->disable(ovl);
  1383. }
  1384. /* Turn of the pipeline */
  1385. ret = omapvid_apply_changes(vout);
  1386. if (ret)
  1387. v4l2_err(&vout->vid_dev->v4l2_dev, "failed to change mode in"
  1388. " streamoff\n");
  1389. INIT_LIST_HEAD(&vout->dma_queue);
  1390. ret = videobuf_streamoff(&vout->vbq);
  1391. return ret;
  1392. }
  1393. static int vidioc_s_fbuf(struct file *file, void *fh,
  1394. const struct v4l2_framebuffer *a)
  1395. {
  1396. int enable = 0;
  1397. struct omap_overlay *ovl;
  1398. struct omapvideo_info *ovid;
  1399. struct omap_vout_device *vout = fh;
  1400. struct omap_overlay_manager_info info;
  1401. enum omap_dss_trans_key_type key_type = OMAP_DSS_COLOR_KEY_GFX_DST;
  1402. ovid = &vout->vid_info;
  1403. ovl = ovid->overlays[0];
  1404. /* OMAP DSS doesn't support Source and Destination color
  1405. key together */
  1406. if ((a->flags & V4L2_FBUF_FLAG_SRC_CHROMAKEY) &&
  1407. (a->flags & V4L2_FBUF_FLAG_CHROMAKEY))
  1408. return -EINVAL;
  1409. /* OMAP DSS Doesn't support the Destination color key
  1410. and alpha blending together */
  1411. if ((a->flags & V4L2_FBUF_FLAG_CHROMAKEY) &&
  1412. (a->flags & V4L2_FBUF_FLAG_LOCAL_ALPHA))
  1413. return -EINVAL;
  1414. if ((a->flags & V4L2_FBUF_FLAG_SRC_CHROMAKEY)) {
  1415. vout->fbuf.flags |= V4L2_FBUF_FLAG_SRC_CHROMAKEY;
  1416. key_type = OMAP_DSS_COLOR_KEY_VID_SRC;
  1417. } else
  1418. vout->fbuf.flags &= ~V4L2_FBUF_FLAG_SRC_CHROMAKEY;
  1419. if ((a->flags & V4L2_FBUF_FLAG_CHROMAKEY)) {
  1420. vout->fbuf.flags |= V4L2_FBUF_FLAG_CHROMAKEY;
  1421. key_type = OMAP_DSS_COLOR_KEY_GFX_DST;
  1422. } else
  1423. vout->fbuf.flags &= ~V4L2_FBUF_FLAG_CHROMAKEY;
  1424. if (a->flags & (V4L2_FBUF_FLAG_CHROMAKEY |
  1425. V4L2_FBUF_FLAG_SRC_CHROMAKEY))
  1426. enable = 1;
  1427. else
  1428. enable = 0;
  1429. if (ovl->manager && ovl->manager->get_manager_info &&
  1430. ovl->manager->set_manager_info) {
  1431. ovl->manager->get_manager_info(ovl->manager, &info);
  1432. info.trans_enabled = enable;
  1433. info.trans_key_type = key_type;
  1434. info.trans_key = vout->win.chromakey;
  1435. if (ovl->manager->set_manager_info(ovl->manager, &info))
  1436. return -EINVAL;
  1437. }
  1438. if (a->flags & V4L2_FBUF_FLAG_LOCAL_ALPHA) {
  1439. vout->fbuf.flags |= V4L2_FBUF_FLAG_LOCAL_ALPHA;
  1440. enable = 1;
  1441. } else {
  1442. vout->fbuf.flags &= ~V4L2_FBUF_FLAG_LOCAL_ALPHA;
  1443. enable = 0;
  1444. }
  1445. if (ovl->manager && ovl->manager->get_manager_info &&
  1446. ovl->manager->set_manager_info) {
  1447. ovl->manager->get_manager_info(ovl->manager, &info);
  1448. /* enable this only if there is no zorder cap */
  1449. if ((ovl->caps & OMAP_DSS_OVL_CAP_ZORDER) == 0)
  1450. info.partial_alpha_enabled = enable;
  1451. if (ovl->manager->set_manager_info(ovl->manager, &info))
  1452. return -EINVAL;
  1453. }
  1454. return 0;
  1455. }
  1456. static int vidioc_g_fbuf(struct file *file, void *fh,
  1457. struct v4l2_framebuffer *a)
  1458. {
  1459. struct omap_overlay *ovl;
  1460. struct omapvideo_info *ovid;
  1461. struct omap_vout_device *vout = fh;
  1462. struct omap_overlay_manager_info info;
  1463. ovid = &vout->vid_info;
  1464. ovl = ovid->overlays[0];
  1465. /* The video overlay must stay within the framebuffer and can't be
  1466. positioned independently. */
  1467. a->flags = V4L2_FBUF_FLAG_OVERLAY;
  1468. a->capability = V4L2_FBUF_CAP_LOCAL_ALPHA | V4L2_FBUF_CAP_CHROMAKEY
  1469. | V4L2_FBUF_CAP_SRC_CHROMAKEY;
  1470. if (ovl->manager && ovl->manager->get_manager_info) {
  1471. ovl->manager->get_manager_info(ovl->manager, &info);
  1472. if (info.trans_key_type == OMAP_DSS_COLOR_KEY_VID_SRC)
  1473. a->flags |= V4L2_FBUF_FLAG_SRC_CHROMAKEY;
  1474. if (info.trans_key_type == OMAP_DSS_COLOR_KEY_GFX_DST)
  1475. a->flags |= V4L2_FBUF_FLAG_CHROMAKEY;
  1476. }
  1477. if (ovl->manager && ovl->manager->get_manager_info) {
  1478. ovl->manager->get_manager_info(ovl->manager, &info);
  1479. if (info.partial_alpha_enabled)
  1480. a->flags |= V4L2_FBUF_FLAG_LOCAL_ALPHA;
  1481. }
  1482. return 0;
  1483. }
  1484. static const struct v4l2_ioctl_ops vout_ioctl_ops = {
  1485. .vidioc_querycap = vidioc_querycap,
  1486. .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
  1487. .vidioc_g_fmt_vid_out = vidioc_g_fmt_vid_out,
  1488. .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out,
  1489. .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
  1490. .vidioc_s_fbuf = vidioc_s_fbuf,
  1491. .vidioc_g_fbuf = vidioc_g_fbuf,
  1492. .vidioc_try_fmt_vid_out_overlay = vidioc_try_fmt_vid_overlay,
  1493. .vidioc_s_fmt_vid_out_overlay = vidioc_s_fmt_vid_overlay,
  1494. .vidioc_g_fmt_vid_out_overlay = vidioc_g_fmt_vid_overlay,
  1495. .vidioc_g_selection = vidioc_g_selection,
  1496. .vidioc_s_selection = vidioc_s_selection,
  1497. .vidioc_reqbufs = vidioc_reqbufs,
  1498. .vidioc_querybuf = vidioc_querybuf,
  1499. .vidioc_qbuf = vidioc_qbuf,
  1500. .vidioc_dqbuf = vidioc_dqbuf,
  1501. .vidioc_streamon = vidioc_streamon,
  1502. .vidioc_streamoff = vidioc_streamoff,
  1503. };
  1504. static const struct v4l2_file_operations omap_vout_fops = {
  1505. .owner = THIS_MODULE,
  1506. .poll = omap_vout_poll,
  1507. .unlocked_ioctl = video_ioctl2,
  1508. .mmap = omap_vout_mmap,
  1509. .open = omap_vout_open,
  1510. .release = omap_vout_release,
  1511. };
  1512. /* Init functions used during driver initialization */
  1513. /* Initial setup of video_data */
  1514. static int __init omap_vout_setup_video_data(struct omap_vout_device *vout)
  1515. {
  1516. struct video_device *vfd;
  1517. struct v4l2_pix_format *pix;
  1518. struct omap_overlay *ovl = vout->vid_info.overlays[0];
  1519. struct omap_dss_device *display = ovl->get_device(ovl);
  1520. struct v4l2_ctrl_handler *hdl;
  1521. /* set the default pix */
  1522. pix = &vout->pix;
  1523. /* Set the default picture of QVGA */
  1524. pix->width = QQVGA_WIDTH;
  1525. pix->height = QQVGA_HEIGHT;
  1526. /* Default pixel format is RGB 5-6-5 */
  1527. pix->pixelformat = V4L2_PIX_FMT_RGB565;
  1528. pix->field = V4L2_FIELD_ANY;
  1529. pix->bytesperline = pix->width * 2;
  1530. pix->sizeimage = pix->bytesperline * pix->height;
  1531. pix->colorspace = V4L2_COLORSPACE_JPEG;
  1532. vout->bpp = RGB565_BPP;
  1533. vout->fbuf.fmt.width = display->panel.timings.x_res;
  1534. vout->fbuf.fmt.height = display->panel.timings.y_res;
  1535. /* Set the data structures for the overlay parameters*/
  1536. vout->win.global_alpha = 255;
  1537. vout->fbuf.flags = 0;
  1538. vout->fbuf.capability = V4L2_FBUF_CAP_LOCAL_ALPHA |
  1539. V4L2_FBUF_CAP_SRC_CHROMAKEY | V4L2_FBUF_CAP_CHROMAKEY;
  1540. vout->win.chromakey = 0;
  1541. omap_vout_new_format(pix, &vout->fbuf, &vout->crop, &vout->win);
  1542. hdl = &vout->ctrl_handler;
  1543. v4l2_ctrl_handler_init(hdl, 3);
  1544. v4l2_ctrl_new_std(hdl, &omap_vout_ctrl_ops,
  1545. V4L2_CID_ROTATE, 0, 270, 90, 0);
  1546. v4l2_ctrl_new_std(hdl, &omap_vout_ctrl_ops,
  1547. V4L2_CID_BG_COLOR, 0, 0xffffff, 1, 0);
  1548. v4l2_ctrl_new_std(hdl, &omap_vout_ctrl_ops,
  1549. V4L2_CID_VFLIP, 0, 1, 1, 0);
  1550. if (hdl->error)
  1551. return hdl->error;
  1552. vout->rotation = 0;
  1553. vout->mirror = false;
  1554. if (vout->vid_info.rotation_type == VOUT_ROT_VRFB)
  1555. vout->vrfb_bpp = 2;
  1556. /* initialize the video_device struct */
  1557. vfd = vout->vfd = video_device_alloc();
  1558. if (!vfd) {
  1559. printk(KERN_ERR VOUT_NAME ": could not allocate"
  1560. " video device struct\n");
  1561. v4l2_ctrl_handler_free(hdl);
  1562. return -ENOMEM;
  1563. }
  1564. vfd->ctrl_handler = hdl;
  1565. vfd->release = video_device_release;
  1566. vfd->ioctl_ops = &vout_ioctl_ops;
  1567. strlcpy(vfd->name, VOUT_NAME, sizeof(vfd->name));
  1568. vfd->fops = &omap_vout_fops;
  1569. vfd->v4l2_dev = &vout->vid_dev->v4l2_dev;
  1570. vfd->vfl_dir = VFL_DIR_TX;
  1571. mutex_init(&vout->lock);
  1572. vfd->minor = -1;
  1573. return 0;
  1574. }
  1575. /* Setup video buffers */
  1576. static int __init omap_vout_setup_video_bufs(struct platform_device *pdev,
  1577. int vid_num)
  1578. {
  1579. u32 numbuffers;
  1580. int ret = 0, i;
  1581. struct omapvideo_info *ovid;
  1582. struct omap_vout_device *vout;
  1583. struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
  1584. struct omap2video_device *vid_dev =
  1585. container_of(v4l2_dev, struct omap2video_device, v4l2_dev);
  1586. vout = vid_dev->vouts[vid_num];
  1587. ovid = &vout->vid_info;
  1588. numbuffers = (vid_num == 0) ? video1_numbuffers : video2_numbuffers;
  1589. vout->buffer_size = (vid_num == 0) ? video1_bufsize : video2_bufsize;
  1590. dev_info(&pdev->dev, "Buffer Size = %d\n", vout->buffer_size);
  1591. for (i = 0; i < numbuffers; i++) {
  1592. vout->buf_virt_addr[i] =
  1593. omap_vout_alloc_buffer(vout->buffer_size,
  1594. (u32 *) &vout->buf_phy_addr[i]);
  1595. if (!vout->buf_virt_addr[i]) {
  1596. numbuffers = i;
  1597. ret = -ENOMEM;
  1598. goto free_buffers;
  1599. }
  1600. }
  1601. vout->cropped_offset = 0;
  1602. if (ovid->rotation_type == VOUT_ROT_VRFB) {
  1603. bool static_vrfb_allocation = (vid_num == 0) ?
  1604. vid1_static_vrfb_alloc : vid2_static_vrfb_alloc;
  1605. ret = omap_vout_setup_vrfb_bufs(pdev, vid_num,
  1606. static_vrfb_allocation);
  1607. }
  1608. return ret;
  1609. free_buffers:
  1610. for (i = 0; i < numbuffers; i++) {
  1611. omap_vout_free_buffer(vout->buf_virt_addr[i],
  1612. vout->buffer_size);
  1613. vout->buf_virt_addr[i] = 0;
  1614. vout->buf_phy_addr[i] = 0;
  1615. }
  1616. return ret;
  1617. }
  1618. /* Create video out devices */
  1619. static int __init omap_vout_create_video_devices(struct platform_device *pdev)
  1620. {
  1621. int ret = 0, k;
  1622. struct omap_vout_device *vout;
  1623. struct video_device *vfd = NULL;
  1624. struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
  1625. struct omap2video_device *vid_dev = container_of(v4l2_dev,
  1626. struct omap2video_device, v4l2_dev);
  1627. for (k = 0; k < pdev->num_resources; k++) {
  1628. vout = kzalloc(sizeof(struct omap_vout_device), GFP_KERNEL);
  1629. if (!vout) {
  1630. dev_err(&pdev->dev, ": could not allocate memory\n");
  1631. return -ENOMEM;
  1632. }
  1633. vout->vid = k;
  1634. vid_dev->vouts[k] = vout;
  1635. vout->vid_dev = vid_dev;
  1636. /* Select video2 if only 1 overlay is controlled by V4L2 */
  1637. if (pdev->num_resources == 1)
  1638. vout->vid_info.overlays[0] = vid_dev->overlays[k + 2];
  1639. else
  1640. /* Else select video1 and video2 one by one. */
  1641. vout->vid_info.overlays[0] = vid_dev->overlays[k + 1];
  1642. vout->vid_info.num_overlays = 1;
  1643. vout->vid_info.id = k + 1;
  1644. /* Set VRFB as rotation_type for omap2 and omap3 */
  1645. if (omap_vout_dss_omap24xx() || omap_vout_dss_omap34xx())
  1646. vout->vid_info.rotation_type = VOUT_ROT_VRFB;
  1647. /* Setup the default configuration for the video devices
  1648. */
  1649. if (omap_vout_setup_video_data(vout) != 0) {
  1650. ret = -ENOMEM;
  1651. goto error;
  1652. }
  1653. /* Allocate default number of buffers for the video streaming
  1654. * and reserve the VRFB space for rotation
  1655. */
  1656. if (omap_vout_setup_video_bufs(pdev, k) != 0) {
  1657. ret = -ENOMEM;
  1658. goto error1;
  1659. }
  1660. /* Register the Video device with V4L2
  1661. */
  1662. vfd = vout->vfd;
  1663. if (video_register_device(vfd, VFL_TYPE_GRABBER, -1) < 0) {
  1664. dev_err(&pdev->dev, ": Could not register "
  1665. "Video for Linux device\n");
  1666. vfd->minor = -1;
  1667. ret = -ENODEV;
  1668. goto error2;
  1669. }
  1670. video_set_drvdata(vfd, vout);
  1671. dev_info(&pdev->dev, ": registered and initialized"
  1672. " video device %d\n", vfd->minor);
  1673. if (k == (pdev->num_resources - 1))
  1674. return 0;
  1675. continue;
  1676. error2:
  1677. if (vout->vid_info.rotation_type == VOUT_ROT_VRFB)
  1678. omap_vout_release_vrfb(vout);
  1679. omap_vout_free_buffers(vout);
  1680. error1:
  1681. video_device_release(vfd);
  1682. error:
  1683. kfree(vout);
  1684. return ret;
  1685. }
  1686. return -ENODEV;
  1687. }
  1688. /* Driver functions */
  1689. static void omap_vout_cleanup_device(struct omap_vout_device *vout)
  1690. {
  1691. struct video_device *vfd;
  1692. struct omapvideo_info *ovid;
  1693. if (!vout)
  1694. return;
  1695. vfd = vout->vfd;
  1696. ovid = &vout->vid_info;
  1697. if (vfd) {
  1698. if (!video_is_registered(vfd)) {
  1699. /*
  1700. * The device was never registered, so release the
  1701. * video_device struct directly.
  1702. */
  1703. video_device_release(vfd);
  1704. } else {
  1705. /*
  1706. * The unregister function will release the video_device
  1707. * struct as well as unregistering it.
  1708. */
  1709. video_unregister_device(vfd);
  1710. }
  1711. }
  1712. v4l2_ctrl_handler_free(&vout->ctrl_handler);
  1713. if (ovid->rotation_type == VOUT_ROT_VRFB) {
  1714. omap_vout_release_vrfb(vout);
  1715. /* Free the VRFB buffer if allocated
  1716. * init time
  1717. */
  1718. if (vout->vrfb_static_allocation)
  1719. omap_vout_free_vrfb_buffers(vout);
  1720. }
  1721. omap_vout_free_buffers(vout);
  1722. kfree(vout);
  1723. }
  1724. static int omap_vout_remove(struct platform_device *pdev)
  1725. {
  1726. int k;
  1727. struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
  1728. struct omap2video_device *vid_dev = container_of(v4l2_dev, struct
  1729. omap2video_device, v4l2_dev);
  1730. v4l2_device_unregister(v4l2_dev);
  1731. for (k = 0; k < pdev->num_resources; k++)
  1732. omap_vout_cleanup_device(vid_dev->vouts[k]);
  1733. for (k = 0; k < vid_dev->num_displays; k++) {
  1734. if (vid_dev->displays[k]->state != OMAP_DSS_DISPLAY_DISABLED)
  1735. vid_dev->displays[k]->driver->disable(vid_dev->displays[k]);
  1736. omap_dss_put_device(vid_dev->displays[k]);
  1737. }
  1738. kfree(vid_dev);
  1739. return 0;
  1740. }
  1741. static int __init omap_vout_probe(struct platform_device *pdev)
  1742. {
  1743. int ret = 0, i;
  1744. struct omap_overlay *ovl;
  1745. struct omap_dss_device *dssdev = NULL;
  1746. struct omap_dss_device *def_display;
  1747. struct omap2video_device *vid_dev = NULL;
  1748. if (omapdss_is_initialized() == false)
  1749. return -EPROBE_DEFER;
  1750. ret = omapdss_compat_init();
  1751. if (ret) {
  1752. dev_err(&pdev->dev, "failed to init dss\n");
  1753. return ret;
  1754. }
  1755. if (pdev->num_resources == 0) {
  1756. dev_err(&pdev->dev, "probed for an unknown device\n");
  1757. ret = -ENODEV;
  1758. goto err_dss_init;
  1759. }
  1760. vid_dev = kzalloc(sizeof(struct omap2video_device), GFP_KERNEL);
  1761. if (vid_dev == NULL) {
  1762. ret = -ENOMEM;
  1763. goto err_dss_init;
  1764. }
  1765. vid_dev->num_displays = 0;
  1766. for_each_dss_dev(dssdev) {
  1767. omap_dss_get_device(dssdev);
  1768. if (!dssdev->driver) {
  1769. dev_warn(&pdev->dev, "no driver for display: %s\n",
  1770. dssdev->name);
  1771. omap_dss_put_device(dssdev);
  1772. continue;
  1773. }
  1774. vid_dev->displays[vid_dev->num_displays++] = dssdev;
  1775. }
  1776. if (vid_dev->num_displays == 0) {
  1777. dev_err(&pdev->dev, "no displays\n");
  1778. ret = -EINVAL;
  1779. goto probe_err0;
  1780. }
  1781. vid_dev->num_overlays = omap_dss_get_num_overlays();
  1782. for (i = 0; i < vid_dev->num_overlays; i++)
  1783. vid_dev->overlays[i] = omap_dss_get_overlay(i);
  1784. vid_dev->num_managers = omap_dss_get_num_overlay_managers();
  1785. for (i = 0; i < vid_dev->num_managers; i++)
  1786. vid_dev->managers[i] = omap_dss_get_overlay_manager(i);
  1787. /* Get the Video1 overlay and video2 overlay.
  1788. * Setup the Display attached to that overlays
  1789. */
  1790. for (i = 1; i < vid_dev->num_overlays; i++) {
  1791. ovl = omap_dss_get_overlay(i);
  1792. dssdev = ovl->get_device(ovl);
  1793. if (dssdev) {
  1794. def_display = dssdev;
  1795. } else {
  1796. dev_warn(&pdev->dev, "cannot find display\n");
  1797. def_display = NULL;
  1798. }
  1799. if (def_display) {
  1800. struct omap_dss_driver *dssdrv = def_display->driver;
  1801. ret = dssdrv->enable(def_display);
  1802. if (ret) {
  1803. /* Here we are not considering a error
  1804. * as display may be enabled by frame
  1805. * buffer driver
  1806. */
  1807. dev_warn(&pdev->dev,
  1808. "'%s' Display already enabled\n",
  1809. def_display->name);
  1810. }
  1811. }
  1812. }
  1813. if (v4l2_device_register(&pdev->dev, &vid_dev->v4l2_dev) < 0) {
  1814. dev_err(&pdev->dev, "v4l2_device_register failed\n");
  1815. ret = -ENODEV;
  1816. goto probe_err1;
  1817. }
  1818. ret = omap_vout_create_video_devices(pdev);
  1819. if (ret)
  1820. goto probe_err2;
  1821. for (i = 0; i < vid_dev->num_displays; i++) {
  1822. struct omap_dss_device *display = vid_dev->displays[i];
  1823. if (display->driver->update)
  1824. display->driver->update(display, 0, 0,
  1825. display->panel.timings.x_res,
  1826. display->panel.timings.y_res);
  1827. }
  1828. return 0;
  1829. probe_err2:
  1830. v4l2_device_unregister(&vid_dev->v4l2_dev);
  1831. probe_err1:
  1832. for (i = 1; i < vid_dev->num_overlays; i++) {
  1833. def_display = NULL;
  1834. ovl = omap_dss_get_overlay(i);
  1835. dssdev = ovl->get_device(ovl);
  1836. if (dssdev)
  1837. def_display = dssdev;
  1838. if (def_display && def_display->driver)
  1839. def_display->driver->disable(def_display);
  1840. }
  1841. probe_err0:
  1842. kfree(vid_dev);
  1843. err_dss_init:
  1844. omapdss_compat_uninit();
  1845. return ret;
  1846. }
  1847. static struct platform_driver omap_vout_driver = {
  1848. .driver = {
  1849. .name = VOUT_NAME,
  1850. },
  1851. .remove = omap_vout_remove,
  1852. };
  1853. static int __init omap_vout_init(void)
  1854. {
  1855. if (platform_driver_probe(&omap_vout_driver, omap_vout_probe) != 0) {
  1856. printk(KERN_ERR VOUT_NAME ":Could not register Video driver\n");
  1857. return -EINVAL;
  1858. }
  1859. return 0;
  1860. }
  1861. static void omap_vout_cleanup(void)
  1862. {
  1863. platform_driver_unregister(&omap_vout_driver);
  1864. }
  1865. late_initcall(omap_vout_init);
  1866. module_exit(omap_vout_cleanup);