omap_vout.c 54 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, long 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", __func__,
  361. 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. if (!is_rotation_enabled(vout))
  604. break;
  605. /* Free the VRFB buffers if no space for V4L2 buffers */
  606. for (j = i; j < *count; j++) {
  607. omap_vout_free_buffer(vout->smsshado_virt_addr[j],
  608. vout->smsshado_size);
  609. vout->smsshado_virt_addr[j] = 0;
  610. vout->smsshado_phy_addr[j] = 0;
  611. }
  612. }
  613. vout->buf_virt_addr[i] = virt_addr;
  614. vout->buf_phy_addr[i] = phy_addr;
  615. }
  616. *count = vout->buffer_allocated = i;
  617. return 0;
  618. }
  619. /*
  620. * Free the V4L2 buffers additionally allocated than default
  621. * number of buffers
  622. */
  623. static void omap_vout_free_extra_buffers(struct omap_vout_device *vout)
  624. {
  625. int num_buffers = 0, i;
  626. num_buffers = (vout->vid == OMAP_VIDEO1) ?
  627. video1_numbuffers : video2_numbuffers;
  628. for (i = num_buffers; i < vout->buffer_allocated; i++) {
  629. if (vout->buf_virt_addr[i])
  630. omap_vout_free_buffer(vout->buf_virt_addr[i],
  631. vout->buffer_size);
  632. vout->buf_virt_addr[i] = 0;
  633. vout->buf_phy_addr[i] = 0;
  634. }
  635. vout->buffer_allocated = num_buffers;
  636. }
  637. /*
  638. * This function will be called when VIDIOC_QBUF ioctl is called.
  639. * It prepare buffers before give out for the display. This function
  640. * converts user space virtual address into physical address if userptr memory
  641. * exchange mechanism is used. If rotation is enabled, it copies entire
  642. * buffer into VRFB memory space before giving it to the DSS.
  643. */
  644. static int omap_vout_buffer_prepare(struct videobuf_queue *q,
  645. struct videobuf_buffer *vb,
  646. enum v4l2_field field)
  647. {
  648. struct omap_vout_device *vout = q->priv_data;
  649. struct omapvideo_info *ovid = &vout->vid_info;
  650. if (VIDEOBUF_NEEDS_INIT == vb->state) {
  651. vb->width = vout->pix.width;
  652. vb->height = vout->pix.height;
  653. vb->size = vb->width * vb->height * vout->bpp;
  654. vb->field = field;
  655. }
  656. vb->state = VIDEOBUF_PREPARED;
  657. /* if user pointer memory mechanism is used, get the physical
  658. * address of the buffer
  659. */
  660. if (V4L2_MEMORY_USERPTR == vb->memory) {
  661. int ret;
  662. if (0 == vb->baddr)
  663. return -EINVAL;
  664. /* Physical address */
  665. ret = omap_vout_get_userptr(vb, vb->baddr,
  666. (u32 *)&vout->queued_buf_addr[vb->i]);
  667. if (ret < 0)
  668. return ret;
  669. } else {
  670. unsigned long addr, dma_addr;
  671. unsigned long size;
  672. addr = (unsigned long) vout->buf_virt_addr[vb->i];
  673. size = (unsigned long) vb->size;
  674. dma_addr = dma_map_single(vout->vid_dev->v4l2_dev.dev, (void *) addr,
  675. size, DMA_TO_DEVICE);
  676. if (dma_mapping_error(vout->vid_dev->v4l2_dev.dev, dma_addr))
  677. v4l2_err(&vout->vid_dev->v4l2_dev,
  678. "dma_map_single failed\n");
  679. vout->queued_buf_addr[vb->i] = (u8 *)vout->buf_phy_addr[vb->i];
  680. }
  681. if (ovid->rotation_type == VOUT_ROT_VRFB)
  682. return omap_vout_prepare_vrfb(vout, vb);
  683. else
  684. return 0;
  685. }
  686. /*
  687. * Buffer queue function will be called from the videobuf layer when _QBUF
  688. * ioctl is called. It is used to enqueue buffer, which is ready to be
  689. * displayed.
  690. */
  691. static void omap_vout_buffer_queue(struct videobuf_queue *q,
  692. struct videobuf_buffer *vb)
  693. {
  694. struct omap_vout_device *vout = q->priv_data;
  695. /* Driver is also maintainig a queue. So enqueue buffer in the driver
  696. * queue */
  697. list_add_tail(&vb->queue, &vout->dma_queue);
  698. vb->state = VIDEOBUF_QUEUED;
  699. }
  700. /*
  701. * Buffer release function is called from videobuf layer to release buffer
  702. * which are already allocated
  703. */
  704. static void omap_vout_buffer_release(struct videobuf_queue *q,
  705. struct videobuf_buffer *vb)
  706. {
  707. vb->state = VIDEOBUF_NEEDS_INIT;
  708. if (vb->memory == V4L2_MEMORY_USERPTR && vb->priv) {
  709. struct frame_vector *vec = vb->priv;
  710. put_vaddr_frames(vec);
  711. frame_vector_destroy(vec);
  712. }
  713. }
  714. /*
  715. * File operations
  716. */
  717. static __poll_t omap_vout_poll(struct file *file,
  718. struct poll_table_struct *wait)
  719. {
  720. struct omap_vout_device *vout = file->private_data;
  721. struct videobuf_queue *q = &vout->vbq;
  722. return videobuf_poll_stream(file, q, wait);
  723. }
  724. static void omap_vout_vm_open(struct vm_area_struct *vma)
  725. {
  726. struct omap_vout_device *vout = vma->vm_private_data;
  727. v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
  728. "vm_open [vma=%08lx-%08lx]\n", vma->vm_start, vma->vm_end);
  729. vout->mmap_count++;
  730. }
  731. static void omap_vout_vm_close(struct vm_area_struct *vma)
  732. {
  733. struct omap_vout_device *vout = vma->vm_private_data;
  734. v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
  735. "vm_close [vma=%08lx-%08lx]\n", vma->vm_start, vma->vm_end);
  736. vout->mmap_count--;
  737. }
  738. static const struct vm_operations_struct omap_vout_vm_ops = {
  739. .open = omap_vout_vm_open,
  740. .close = omap_vout_vm_close,
  741. };
  742. static int omap_vout_mmap(struct file *file, struct vm_area_struct *vma)
  743. {
  744. int i;
  745. void *pos;
  746. unsigned long start = vma->vm_start;
  747. unsigned long size = (vma->vm_end - vma->vm_start);
  748. struct omap_vout_device *vout = file->private_data;
  749. struct videobuf_queue *q = &vout->vbq;
  750. v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
  751. " %s pgoff=0x%lx, start=0x%lx, end=0x%lx\n", __func__,
  752. vma->vm_pgoff, vma->vm_start, vma->vm_end);
  753. /* look for the buffer to map */
  754. for (i = 0; i < VIDEO_MAX_FRAME; i++) {
  755. if (NULL == q->bufs[i])
  756. continue;
  757. if (V4L2_MEMORY_MMAP != q->bufs[i]->memory)
  758. continue;
  759. if (q->bufs[i]->boff == (vma->vm_pgoff << PAGE_SHIFT))
  760. break;
  761. }
  762. if (VIDEO_MAX_FRAME == i) {
  763. v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
  764. "offset invalid [offset=0x%lx]\n",
  765. (vma->vm_pgoff << PAGE_SHIFT));
  766. return -EINVAL;
  767. }
  768. /* Check the size of the buffer */
  769. if (size > vout->buffer_size) {
  770. v4l2_err(&vout->vid_dev->v4l2_dev,
  771. "insufficient memory [%lu] [%u]\n",
  772. size, vout->buffer_size);
  773. return -ENOMEM;
  774. }
  775. q->bufs[i]->baddr = vma->vm_start;
  776. vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
  777. vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
  778. vma->vm_ops = &omap_vout_vm_ops;
  779. vma->vm_private_data = (void *) vout;
  780. pos = (void *)vout->buf_virt_addr[i];
  781. vma->vm_pgoff = virt_to_phys((void *)pos) >> PAGE_SHIFT;
  782. while (size > 0) {
  783. unsigned long pfn;
  784. pfn = virt_to_phys((void *) pos) >> PAGE_SHIFT;
  785. if (remap_pfn_range(vma, start, pfn, PAGE_SIZE, PAGE_SHARED))
  786. return -EAGAIN;
  787. start += PAGE_SIZE;
  788. pos += PAGE_SIZE;
  789. size -= PAGE_SIZE;
  790. }
  791. vout->mmap_count++;
  792. v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Exiting %s\n", __func__);
  793. return 0;
  794. }
  795. static int omap_vout_release(struct file *file)
  796. {
  797. unsigned int ret, i;
  798. struct videobuf_queue *q;
  799. struct omapvideo_info *ovid;
  800. struct omap_vout_device *vout = file->private_data;
  801. v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Entering %s\n", __func__);
  802. ovid = &vout->vid_info;
  803. if (!vout)
  804. return 0;
  805. q = &vout->vbq;
  806. /* Disable all the overlay managers connected with this interface */
  807. for (i = 0; i < ovid->num_overlays; i++) {
  808. struct omap_overlay *ovl = ovid->overlays[i];
  809. struct omap_dss_device *dssdev = ovl->get_device(ovl);
  810. if (dssdev)
  811. ovl->disable(ovl);
  812. }
  813. /* Turn off the pipeline */
  814. ret = omapvid_apply_changes(vout);
  815. if (ret)
  816. v4l2_warn(&vout->vid_dev->v4l2_dev,
  817. "Unable to apply changes\n");
  818. /* Free all buffers */
  819. omap_vout_free_extra_buffers(vout);
  820. /* Free the VRFB buffers only if they are allocated
  821. * during reqbufs. Don't free if init time allocated
  822. */
  823. if (ovid->rotation_type == VOUT_ROT_VRFB) {
  824. if (!vout->vrfb_static_allocation)
  825. omap_vout_free_vrfb_buffers(vout);
  826. }
  827. videobuf_mmap_free(q);
  828. /* Even if apply changes fails we should continue
  829. freeing allocated memory */
  830. if (vout->streaming) {
  831. u32 mask = 0;
  832. mask = DISPC_IRQ_VSYNC | DISPC_IRQ_EVSYNC_EVEN |
  833. DISPC_IRQ_EVSYNC_ODD | DISPC_IRQ_VSYNC2;
  834. omap_dispc_unregister_isr(omap_vout_isr, vout, mask);
  835. vout->streaming = false;
  836. videobuf_streamoff(q);
  837. videobuf_queue_cancel(q);
  838. }
  839. if (vout->mmap_count != 0)
  840. vout->mmap_count = 0;
  841. vout->opened -= 1;
  842. file->private_data = NULL;
  843. if (vout->buffer_allocated)
  844. videobuf_mmap_free(q);
  845. v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Exiting %s\n", __func__);
  846. return ret;
  847. }
  848. static int omap_vout_open(struct file *file)
  849. {
  850. struct videobuf_queue *q;
  851. struct omap_vout_device *vout = NULL;
  852. vout = video_drvdata(file);
  853. if (vout == NULL)
  854. return -ENODEV;
  855. v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Entering %s\n", __func__);
  856. /* for now, we only support single open */
  857. if (vout->opened)
  858. return -EBUSY;
  859. vout->opened += 1;
  860. file->private_data = vout;
  861. vout->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  862. q = &vout->vbq;
  863. video_vbq_ops.buf_setup = omap_vout_buffer_setup;
  864. video_vbq_ops.buf_prepare = omap_vout_buffer_prepare;
  865. video_vbq_ops.buf_release = omap_vout_buffer_release;
  866. video_vbq_ops.buf_queue = omap_vout_buffer_queue;
  867. spin_lock_init(&vout->vbq_lock);
  868. videobuf_queue_dma_contig_init(q, &video_vbq_ops, q->dev,
  869. &vout->vbq_lock, vout->type, V4L2_FIELD_NONE,
  870. sizeof(struct videobuf_buffer), vout, NULL);
  871. v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Exiting %s\n", __func__);
  872. return 0;
  873. }
  874. /*
  875. * V4L2 ioctls
  876. */
  877. static int vidioc_querycap(struct file *file, void *fh,
  878. struct v4l2_capability *cap)
  879. {
  880. struct omap_vout_device *vout = fh;
  881. strlcpy(cap->driver, VOUT_NAME, sizeof(cap->driver));
  882. strlcpy(cap->card, vout->vfd->name, sizeof(cap->card));
  883. cap->bus_info[0] = '\0';
  884. cap->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_OUTPUT |
  885. V4L2_CAP_VIDEO_OUTPUT_OVERLAY;
  886. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  887. return 0;
  888. }
  889. static int vidioc_enum_fmt_vid_out(struct file *file, void *fh,
  890. struct v4l2_fmtdesc *fmt)
  891. {
  892. int index = fmt->index;
  893. if (index >= NUM_OUTPUT_FORMATS)
  894. return -EINVAL;
  895. fmt->flags = omap_formats[index].flags;
  896. strlcpy(fmt->description, omap_formats[index].description,
  897. sizeof(fmt->description));
  898. fmt->pixelformat = omap_formats[index].pixelformat;
  899. return 0;
  900. }
  901. static int vidioc_g_fmt_vid_out(struct file *file, void *fh,
  902. struct v4l2_format *f)
  903. {
  904. struct omap_vout_device *vout = fh;
  905. f->fmt.pix = vout->pix;
  906. return 0;
  907. }
  908. static int vidioc_try_fmt_vid_out(struct file *file, void *fh,
  909. struct v4l2_format *f)
  910. {
  911. struct omap_overlay *ovl;
  912. struct omapvideo_info *ovid;
  913. struct omap_video_timings *timing;
  914. struct omap_vout_device *vout = fh;
  915. struct omap_dss_device *dssdev;
  916. ovid = &vout->vid_info;
  917. ovl = ovid->overlays[0];
  918. /* get the display device attached to the overlay */
  919. dssdev = ovl->get_device(ovl);
  920. if (!dssdev)
  921. return -EINVAL;
  922. timing = &dssdev->panel.timings;
  923. vout->fbuf.fmt.height = timing->y_res;
  924. vout->fbuf.fmt.width = timing->x_res;
  925. omap_vout_try_format(&f->fmt.pix);
  926. return 0;
  927. }
  928. static int vidioc_s_fmt_vid_out(struct file *file, void *fh,
  929. struct v4l2_format *f)
  930. {
  931. int ret, bpp;
  932. struct omap_overlay *ovl;
  933. struct omapvideo_info *ovid;
  934. struct omap_video_timings *timing;
  935. struct omap_vout_device *vout = fh;
  936. struct omap_dss_device *dssdev;
  937. if (vout->streaming)
  938. return -EBUSY;
  939. mutex_lock(&vout->lock);
  940. ovid = &vout->vid_info;
  941. ovl = ovid->overlays[0];
  942. dssdev = ovl->get_device(ovl);
  943. /* get the display device attached to the overlay */
  944. if (!dssdev) {
  945. ret = -EINVAL;
  946. goto s_fmt_vid_out_exit;
  947. }
  948. timing = &dssdev->panel.timings;
  949. /* We dont support RGB24-packed mode if vrfb rotation
  950. * is enabled*/
  951. if ((is_rotation_enabled(vout)) &&
  952. f->fmt.pix.pixelformat == V4L2_PIX_FMT_RGB24) {
  953. ret = -EINVAL;
  954. goto s_fmt_vid_out_exit;
  955. }
  956. /* get the framebuffer parameters */
  957. if (is_rotation_90_or_270(vout)) {
  958. vout->fbuf.fmt.height = timing->x_res;
  959. vout->fbuf.fmt.width = timing->y_res;
  960. } else {
  961. vout->fbuf.fmt.height = timing->y_res;
  962. vout->fbuf.fmt.width = timing->x_res;
  963. }
  964. /* change to samller size is OK */
  965. bpp = omap_vout_try_format(&f->fmt.pix);
  966. f->fmt.pix.sizeimage = f->fmt.pix.width * f->fmt.pix.height * bpp;
  967. /* try & set the new output format */
  968. vout->bpp = bpp;
  969. vout->pix = f->fmt.pix;
  970. vout->vrfb_bpp = 1;
  971. /* If YUYV then vrfb bpp is 2, for others its 1 */
  972. if (V4L2_PIX_FMT_YUYV == vout->pix.pixelformat ||
  973. V4L2_PIX_FMT_UYVY == vout->pix.pixelformat)
  974. vout->vrfb_bpp = 2;
  975. /* set default crop and win */
  976. omap_vout_new_format(&vout->pix, &vout->fbuf, &vout->crop, &vout->win);
  977. ret = 0;
  978. s_fmt_vid_out_exit:
  979. mutex_unlock(&vout->lock);
  980. return ret;
  981. }
  982. static int vidioc_try_fmt_vid_overlay(struct file *file, void *fh,
  983. struct v4l2_format *f)
  984. {
  985. int ret = 0;
  986. struct omap_vout_device *vout = fh;
  987. struct omap_overlay *ovl;
  988. struct omapvideo_info *ovid;
  989. struct v4l2_window *win = &f->fmt.win;
  990. ovid = &vout->vid_info;
  991. ovl = ovid->overlays[0];
  992. ret = omap_vout_try_window(&vout->fbuf, win);
  993. if (!ret) {
  994. if ((ovl->caps & OMAP_DSS_OVL_CAP_GLOBAL_ALPHA) == 0)
  995. win->global_alpha = 255;
  996. else
  997. win->global_alpha = f->fmt.win.global_alpha;
  998. }
  999. return ret;
  1000. }
  1001. static int vidioc_s_fmt_vid_overlay(struct file *file, void *fh,
  1002. struct v4l2_format *f)
  1003. {
  1004. int ret = 0;
  1005. struct omap_overlay *ovl;
  1006. struct omapvideo_info *ovid;
  1007. struct omap_vout_device *vout = fh;
  1008. struct v4l2_window *win = &f->fmt.win;
  1009. mutex_lock(&vout->lock);
  1010. ovid = &vout->vid_info;
  1011. ovl = ovid->overlays[0];
  1012. ret = omap_vout_new_window(&vout->crop, &vout->win, &vout->fbuf, win);
  1013. if (!ret) {
  1014. /* Video1 plane does not support global alpha on OMAP3 */
  1015. if ((ovl->caps & OMAP_DSS_OVL_CAP_GLOBAL_ALPHA) == 0)
  1016. vout->win.global_alpha = 255;
  1017. else
  1018. vout->win.global_alpha = f->fmt.win.global_alpha;
  1019. vout->win.chromakey = f->fmt.win.chromakey;
  1020. }
  1021. mutex_unlock(&vout->lock);
  1022. return ret;
  1023. }
  1024. static int vidioc_g_fmt_vid_overlay(struct file *file, void *fh,
  1025. struct v4l2_format *f)
  1026. {
  1027. u32 key_value = 0;
  1028. struct omap_overlay *ovl;
  1029. struct omapvideo_info *ovid;
  1030. struct omap_vout_device *vout = fh;
  1031. struct omap_overlay_manager_info info;
  1032. struct v4l2_window *win = &f->fmt.win;
  1033. ovid = &vout->vid_info;
  1034. ovl = ovid->overlays[0];
  1035. win->w = vout->win.w;
  1036. win->field = vout->win.field;
  1037. win->global_alpha = vout->win.global_alpha;
  1038. if (ovl->manager && ovl->manager->get_manager_info) {
  1039. ovl->manager->get_manager_info(ovl->manager, &info);
  1040. key_value = info.trans_key;
  1041. }
  1042. win->chromakey = key_value;
  1043. return 0;
  1044. }
  1045. static int vidioc_g_selection(struct file *file, void *fh, struct v4l2_selection *sel)
  1046. {
  1047. struct omap_vout_device *vout = fh;
  1048. struct v4l2_pix_format *pix = &vout->pix;
  1049. if (sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  1050. return -EINVAL;
  1051. switch (sel->target) {
  1052. case V4L2_SEL_TGT_CROP:
  1053. sel->r = vout->crop;
  1054. break;
  1055. case V4L2_SEL_TGT_CROP_DEFAULT:
  1056. omap_vout_default_crop(&vout->pix, &vout->fbuf, &sel->r);
  1057. break;
  1058. case V4L2_SEL_TGT_CROP_BOUNDS:
  1059. /* Width and height are always even */
  1060. sel->r.width = pix->width & ~1;
  1061. sel->r.height = pix->height & ~1;
  1062. break;
  1063. default:
  1064. return -EINVAL;
  1065. }
  1066. return 0;
  1067. }
  1068. static int vidioc_s_selection(struct file *file, void *fh, struct v4l2_selection *sel)
  1069. {
  1070. int ret = -EINVAL;
  1071. struct omap_vout_device *vout = fh;
  1072. struct omapvideo_info *ovid;
  1073. struct omap_overlay *ovl;
  1074. struct omap_video_timings *timing;
  1075. struct omap_dss_device *dssdev;
  1076. if (sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  1077. return -EINVAL;
  1078. if (sel->target != V4L2_SEL_TGT_CROP)
  1079. return -EINVAL;
  1080. if (vout->streaming)
  1081. return -EBUSY;
  1082. mutex_lock(&vout->lock);
  1083. ovid = &vout->vid_info;
  1084. ovl = ovid->overlays[0];
  1085. /* get the display device attached to the overlay */
  1086. dssdev = ovl->get_device(ovl);
  1087. if (!dssdev) {
  1088. ret = -EINVAL;
  1089. goto s_crop_err;
  1090. }
  1091. timing = &dssdev->panel.timings;
  1092. if (is_rotation_90_or_270(vout)) {
  1093. vout->fbuf.fmt.height = timing->x_res;
  1094. vout->fbuf.fmt.width = timing->y_res;
  1095. } else {
  1096. vout->fbuf.fmt.height = timing->y_res;
  1097. vout->fbuf.fmt.width = timing->x_res;
  1098. }
  1099. ret = omap_vout_new_crop(&vout->pix, &vout->crop, &vout->win,
  1100. &vout->fbuf, &sel->r);
  1101. s_crop_err:
  1102. mutex_unlock(&vout->lock);
  1103. return ret;
  1104. }
  1105. static int omap_vout_s_ctrl(struct v4l2_ctrl *ctrl)
  1106. {
  1107. struct omap_vout_device *vout =
  1108. container_of(ctrl->handler, struct omap_vout_device, ctrl_handler);
  1109. int ret = 0;
  1110. switch (ctrl->id) {
  1111. case V4L2_CID_ROTATE: {
  1112. struct omapvideo_info *ovid;
  1113. int rotation = ctrl->val;
  1114. ovid = &vout->vid_info;
  1115. mutex_lock(&vout->lock);
  1116. if (rotation && ovid->rotation_type == VOUT_ROT_NONE) {
  1117. mutex_unlock(&vout->lock);
  1118. ret = -ERANGE;
  1119. break;
  1120. }
  1121. if (rotation && vout->pix.pixelformat == V4L2_PIX_FMT_RGB24) {
  1122. mutex_unlock(&vout->lock);
  1123. ret = -EINVAL;
  1124. break;
  1125. }
  1126. if (v4l2_rot_to_dss_rot(rotation, &vout->rotation,
  1127. vout->mirror)) {
  1128. mutex_unlock(&vout->lock);
  1129. ret = -EINVAL;
  1130. break;
  1131. }
  1132. mutex_unlock(&vout->lock);
  1133. break;
  1134. }
  1135. case V4L2_CID_BG_COLOR:
  1136. {
  1137. struct omap_overlay *ovl;
  1138. unsigned int color = ctrl->val;
  1139. struct omap_overlay_manager_info info;
  1140. ovl = vout->vid_info.overlays[0];
  1141. mutex_lock(&vout->lock);
  1142. if (!ovl->manager || !ovl->manager->get_manager_info) {
  1143. mutex_unlock(&vout->lock);
  1144. ret = -EINVAL;
  1145. break;
  1146. }
  1147. ovl->manager->get_manager_info(ovl->manager, &info);
  1148. info.default_color = color;
  1149. if (ovl->manager->set_manager_info(ovl->manager, &info)) {
  1150. mutex_unlock(&vout->lock);
  1151. ret = -EINVAL;
  1152. break;
  1153. }
  1154. mutex_unlock(&vout->lock);
  1155. break;
  1156. }
  1157. case V4L2_CID_VFLIP:
  1158. {
  1159. struct omapvideo_info *ovid;
  1160. unsigned int mirror = ctrl->val;
  1161. ovid = &vout->vid_info;
  1162. mutex_lock(&vout->lock);
  1163. if (mirror && ovid->rotation_type == VOUT_ROT_NONE) {
  1164. mutex_unlock(&vout->lock);
  1165. ret = -ERANGE;
  1166. break;
  1167. }
  1168. if (mirror && vout->pix.pixelformat == V4L2_PIX_FMT_RGB24) {
  1169. mutex_unlock(&vout->lock);
  1170. ret = -EINVAL;
  1171. break;
  1172. }
  1173. vout->mirror = mirror;
  1174. mutex_unlock(&vout->lock);
  1175. break;
  1176. }
  1177. default:
  1178. return -EINVAL;
  1179. }
  1180. return ret;
  1181. }
  1182. static const struct v4l2_ctrl_ops omap_vout_ctrl_ops = {
  1183. .s_ctrl = omap_vout_s_ctrl,
  1184. };
  1185. static int vidioc_reqbufs(struct file *file, void *fh,
  1186. struct v4l2_requestbuffers *req)
  1187. {
  1188. int ret = 0;
  1189. unsigned int i, num_buffers = 0;
  1190. struct omap_vout_device *vout = fh;
  1191. struct videobuf_queue *q = &vout->vbq;
  1192. if (req->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  1193. return -EINVAL;
  1194. /* if memory is not mmp or userptr
  1195. return error */
  1196. if ((V4L2_MEMORY_MMAP != req->memory) &&
  1197. (V4L2_MEMORY_USERPTR != req->memory))
  1198. return -EINVAL;
  1199. mutex_lock(&vout->lock);
  1200. /* Cannot be requested when streaming is on */
  1201. if (vout->streaming) {
  1202. ret = -EBUSY;
  1203. goto reqbuf_err;
  1204. }
  1205. /* If buffers are already allocated free them */
  1206. if (q->bufs[0] && (V4L2_MEMORY_MMAP == q->bufs[0]->memory)) {
  1207. if (vout->mmap_count) {
  1208. ret = -EBUSY;
  1209. goto reqbuf_err;
  1210. }
  1211. num_buffers = (vout->vid == OMAP_VIDEO1) ?
  1212. video1_numbuffers : video2_numbuffers;
  1213. for (i = num_buffers; i < vout->buffer_allocated; i++) {
  1214. omap_vout_free_buffer(vout->buf_virt_addr[i],
  1215. vout->buffer_size);
  1216. vout->buf_virt_addr[i] = 0;
  1217. vout->buf_phy_addr[i] = 0;
  1218. }
  1219. vout->buffer_allocated = num_buffers;
  1220. videobuf_mmap_free(q);
  1221. } else if (q->bufs[0] && (V4L2_MEMORY_USERPTR == q->bufs[0]->memory)) {
  1222. if (vout->buffer_allocated) {
  1223. videobuf_mmap_free(q);
  1224. for (i = 0; i < vout->buffer_allocated; i++) {
  1225. kfree(q->bufs[i]);
  1226. q->bufs[i] = NULL;
  1227. }
  1228. vout->buffer_allocated = 0;
  1229. }
  1230. }
  1231. /*store the memory type in data structure */
  1232. vout->memory = req->memory;
  1233. INIT_LIST_HEAD(&vout->dma_queue);
  1234. /* call videobuf_reqbufs api */
  1235. ret = videobuf_reqbufs(q, req);
  1236. if (ret < 0)
  1237. goto reqbuf_err;
  1238. vout->buffer_allocated = req->count;
  1239. reqbuf_err:
  1240. mutex_unlock(&vout->lock);
  1241. return ret;
  1242. }
  1243. static int vidioc_querybuf(struct file *file, void *fh,
  1244. struct v4l2_buffer *b)
  1245. {
  1246. struct omap_vout_device *vout = fh;
  1247. return videobuf_querybuf(&vout->vbq, b);
  1248. }
  1249. static int vidioc_qbuf(struct file *file, void *fh,
  1250. struct v4l2_buffer *buffer)
  1251. {
  1252. struct omap_vout_device *vout = fh;
  1253. struct videobuf_queue *q = &vout->vbq;
  1254. if ((V4L2_BUF_TYPE_VIDEO_OUTPUT != buffer->type) ||
  1255. (buffer->index >= vout->buffer_allocated) ||
  1256. (q->bufs[buffer->index]->memory != buffer->memory)) {
  1257. return -EINVAL;
  1258. }
  1259. if (V4L2_MEMORY_USERPTR == buffer->memory) {
  1260. if ((buffer->length < vout->pix.sizeimage) ||
  1261. (0 == buffer->m.userptr)) {
  1262. return -EINVAL;
  1263. }
  1264. }
  1265. if ((is_rotation_enabled(vout)) &&
  1266. vout->vrfb_dma_tx.req_status == DMA_CHAN_NOT_ALLOTED) {
  1267. v4l2_warn(&vout->vid_dev->v4l2_dev,
  1268. "DMA Channel not allocated for Rotation\n");
  1269. return -EINVAL;
  1270. }
  1271. return videobuf_qbuf(q, buffer);
  1272. }
  1273. static int vidioc_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
  1274. {
  1275. struct omap_vout_device *vout = fh;
  1276. struct videobuf_queue *q = &vout->vbq;
  1277. int ret;
  1278. u32 addr;
  1279. unsigned long size;
  1280. struct videobuf_buffer *vb;
  1281. if (!vout->streaming)
  1282. return -EINVAL;
  1283. ret = videobuf_dqbuf(q, b, !!(file->f_flags & O_NONBLOCK));
  1284. if (ret)
  1285. return ret;
  1286. vb = q->bufs[b->index];
  1287. addr = (unsigned long) vout->buf_phy_addr[vb->i];
  1288. size = (unsigned long) vb->size;
  1289. dma_unmap_single(vout->vid_dev->v4l2_dev.dev, addr,
  1290. size, DMA_TO_DEVICE);
  1291. return 0;
  1292. }
  1293. static int vidioc_streamon(struct file *file, void *fh, enum v4l2_buf_type i)
  1294. {
  1295. int ret = 0, j;
  1296. u32 addr = 0, mask = 0;
  1297. struct omap_vout_device *vout = fh;
  1298. struct videobuf_queue *q = &vout->vbq;
  1299. struct omapvideo_info *ovid = &vout->vid_info;
  1300. mutex_lock(&vout->lock);
  1301. if (vout->streaming) {
  1302. ret = -EBUSY;
  1303. goto streamon_err;
  1304. }
  1305. ret = videobuf_streamon(q);
  1306. if (ret)
  1307. goto streamon_err;
  1308. if (list_empty(&vout->dma_queue)) {
  1309. ret = -EIO;
  1310. goto streamon_err1;
  1311. }
  1312. /* Get the next frame from the buffer queue */
  1313. vout->next_frm = vout->cur_frm = list_entry(vout->dma_queue.next,
  1314. struct videobuf_buffer, queue);
  1315. /* Remove buffer from the buffer queue */
  1316. list_del(&vout->cur_frm->queue);
  1317. /* Mark state of the current frame to active */
  1318. vout->cur_frm->state = VIDEOBUF_ACTIVE;
  1319. /* Initialize field_id and started member */
  1320. vout->field_id = 0;
  1321. /* set flag here. Next QBUF will start DMA */
  1322. vout->streaming = true;
  1323. vout->first_int = 1;
  1324. if (omap_vout_calculate_offset(vout)) {
  1325. ret = -EINVAL;
  1326. goto streamon_err1;
  1327. }
  1328. addr = (unsigned long) vout->queued_buf_addr[vout->cur_frm->i]
  1329. + vout->cropped_offset;
  1330. mask = DISPC_IRQ_VSYNC | DISPC_IRQ_EVSYNC_EVEN | DISPC_IRQ_EVSYNC_ODD
  1331. | DISPC_IRQ_VSYNC2;
  1332. /* First save the configuration in ovelray structure */
  1333. ret = omapvid_init(vout, addr);
  1334. if (ret) {
  1335. v4l2_err(&vout->vid_dev->v4l2_dev,
  1336. "failed to set overlay info\n");
  1337. goto streamon_err1;
  1338. }
  1339. omap_dispc_register_isr(omap_vout_isr, vout, mask);
  1340. /* Enable the pipeline and set the Go bit */
  1341. ret = omapvid_apply_changes(vout);
  1342. if (ret)
  1343. v4l2_err(&vout->vid_dev->v4l2_dev, "failed to change mode\n");
  1344. for (j = 0; j < ovid->num_overlays; j++) {
  1345. struct omap_overlay *ovl = ovid->overlays[j];
  1346. struct omap_dss_device *dssdev = ovl->get_device(ovl);
  1347. if (dssdev) {
  1348. ret = ovl->enable(ovl);
  1349. if (ret)
  1350. goto streamon_err1;
  1351. }
  1352. }
  1353. ret = 0;
  1354. streamon_err1:
  1355. if (ret)
  1356. ret = videobuf_streamoff(q);
  1357. streamon_err:
  1358. mutex_unlock(&vout->lock);
  1359. return ret;
  1360. }
  1361. static int vidioc_streamoff(struct file *file, void *fh, enum v4l2_buf_type i)
  1362. {
  1363. u32 mask = 0;
  1364. int ret = 0, j;
  1365. struct omap_vout_device *vout = fh;
  1366. struct omapvideo_info *ovid = &vout->vid_info;
  1367. if (!vout->streaming)
  1368. return -EINVAL;
  1369. vout->streaming = false;
  1370. mask = DISPC_IRQ_VSYNC | DISPC_IRQ_EVSYNC_EVEN | DISPC_IRQ_EVSYNC_ODD
  1371. | DISPC_IRQ_VSYNC2;
  1372. omap_dispc_unregister_isr(omap_vout_isr, vout, mask);
  1373. for (j = 0; j < ovid->num_overlays; j++) {
  1374. struct omap_overlay *ovl = ovid->overlays[j];
  1375. struct omap_dss_device *dssdev = ovl->get_device(ovl);
  1376. if (dssdev)
  1377. ovl->disable(ovl);
  1378. }
  1379. /* Turn of the pipeline */
  1380. ret = omapvid_apply_changes(vout);
  1381. if (ret)
  1382. v4l2_err(&vout->vid_dev->v4l2_dev,
  1383. "failed to change mode in streamoff\n");
  1384. INIT_LIST_HEAD(&vout->dma_queue);
  1385. ret = videobuf_streamoff(&vout->vbq);
  1386. return ret;
  1387. }
  1388. static int vidioc_s_fbuf(struct file *file, void *fh,
  1389. const struct v4l2_framebuffer *a)
  1390. {
  1391. int enable = 0;
  1392. struct omap_overlay *ovl;
  1393. struct omapvideo_info *ovid;
  1394. struct omap_vout_device *vout = fh;
  1395. struct omap_overlay_manager_info info;
  1396. enum omap_dss_trans_key_type key_type = OMAP_DSS_COLOR_KEY_GFX_DST;
  1397. ovid = &vout->vid_info;
  1398. ovl = ovid->overlays[0];
  1399. /* OMAP DSS doesn't support Source and Destination color
  1400. key together */
  1401. if ((a->flags & V4L2_FBUF_FLAG_SRC_CHROMAKEY) &&
  1402. (a->flags & V4L2_FBUF_FLAG_CHROMAKEY))
  1403. return -EINVAL;
  1404. /* OMAP DSS Doesn't support the Destination color key
  1405. and alpha blending together */
  1406. if ((a->flags & V4L2_FBUF_FLAG_CHROMAKEY) &&
  1407. (a->flags & V4L2_FBUF_FLAG_LOCAL_ALPHA))
  1408. return -EINVAL;
  1409. if ((a->flags & V4L2_FBUF_FLAG_SRC_CHROMAKEY)) {
  1410. vout->fbuf.flags |= V4L2_FBUF_FLAG_SRC_CHROMAKEY;
  1411. key_type = OMAP_DSS_COLOR_KEY_VID_SRC;
  1412. } else
  1413. vout->fbuf.flags &= ~V4L2_FBUF_FLAG_SRC_CHROMAKEY;
  1414. if ((a->flags & V4L2_FBUF_FLAG_CHROMAKEY)) {
  1415. vout->fbuf.flags |= V4L2_FBUF_FLAG_CHROMAKEY;
  1416. key_type = OMAP_DSS_COLOR_KEY_GFX_DST;
  1417. } else
  1418. vout->fbuf.flags &= ~V4L2_FBUF_FLAG_CHROMAKEY;
  1419. if (a->flags & (V4L2_FBUF_FLAG_CHROMAKEY |
  1420. V4L2_FBUF_FLAG_SRC_CHROMAKEY))
  1421. enable = 1;
  1422. else
  1423. enable = 0;
  1424. if (ovl->manager && ovl->manager->get_manager_info &&
  1425. ovl->manager->set_manager_info) {
  1426. ovl->manager->get_manager_info(ovl->manager, &info);
  1427. info.trans_enabled = enable;
  1428. info.trans_key_type = key_type;
  1429. info.trans_key = vout->win.chromakey;
  1430. if (ovl->manager->set_manager_info(ovl->manager, &info))
  1431. return -EINVAL;
  1432. }
  1433. if (a->flags & V4L2_FBUF_FLAG_LOCAL_ALPHA) {
  1434. vout->fbuf.flags |= V4L2_FBUF_FLAG_LOCAL_ALPHA;
  1435. enable = 1;
  1436. } else {
  1437. vout->fbuf.flags &= ~V4L2_FBUF_FLAG_LOCAL_ALPHA;
  1438. enable = 0;
  1439. }
  1440. if (ovl->manager && ovl->manager->get_manager_info &&
  1441. ovl->manager->set_manager_info) {
  1442. ovl->manager->get_manager_info(ovl->manager, &info);
  1443. /* enable this only if there is no zorder cap */
  1444. if ((ovl->caps & OMAP_DSS_OVL_CAP_ZORDER) == 0)
  1445. info.partial_alpha_enabled = enable;
  1446. if (ovl->manager->set_manager_info(ovl->manager, &info))
  1447. return -EINVAL;
  1448. }
  1449. return 0;
  1450. }
  1451. static int vidioc_g_fbuf(struct file *file, void *fh,
  1452. struct v4l2_framebuffer *a)
  1453. {
  1454. struct omap_overlay *ovl;
  1455. struct omapvideo_info *ovid;
  1456. struct omap_vout_device *vout = fh;
  1457. struct omap_overlay_manager_info info;
  1458. ovid = &vout->vid_info;
  1459. ovl = ovid->overlays[0];
  1460. /* The video overlay must stay within the framebuffer and can't be
  1461. positioned independently. */
  1462. a->flags = V4L2_FBUF_FLAG_OVERLAY;
  1463. a->capability = V4L2_FBUF_CAP_LOCAL_ALPHA | V4L2_FBUF_CAP_CHROMAKEY
  1464. | V4L2_FBUF_CAP_SRC_CHROMAKEY;
  1465. if (ovl->manager && ovl->manager->get_manager_info) {
  1466. ovl->manager->get_manager_info(ovl->manager, &info);
  1467. if (info.trans_key_type == OMAP_DSS_COLOR_KEY_VID_SRC)
  1468. a->flags |= V4L2_FBUF_FLAG_SRC_CHROMAKEY;
  1469. if (info.trans_key_type == OMAP_DSS_COLOR_KEY_GFX_DST)
  1470. a->flags |= V4L2_FBUF_FLAG_CHROMAKEY;
  1471. }
  1472. if (ovl->manager && ovl->manager->get_manager_info) {
  1473. ovl->manager->get_manager_info(ovl->manager, &info);
  1474. if (info.partial_alpha_enabled)
  1475. a->flags |= V4L2_FBUF_FLAG_LOCAL_ALPHA;
  1476. }
  1477. return 0;
  1478. }
  1479. static const struct v4l2_ioctl_ops vout_ioctl_ops = {
  1480. .vidioc_querycap = vidioc_querycap,
  1481. .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
  1482. .vidioc_g_fmt_vid_out = vidioc_g_fmt_vid_out,
  1483. .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out,
  1484. .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
  1485. .vidioc_s_fbuf = vidioc_s_fbuf,
  1486. .vidioc_g_fbuf = vidioc_g_fbuf,
  1487. .vidioc_try_fmt_vid_out_overlay = vidioc_try_fmt_vid_overlay,
  1488. .vidioc_s_fmt_vid_out_overlay = vidioc_s_fmt_vid_overlay,
  1489. .vidioc_g_fmt_vid_out_overlay = vidioc_g_fmt_vid_overlay,
  1490. .vidioc_g_selection = vidioc_g_selection,
  1491. .vidioc_s_selection = vidioc_s_selection,
  1492. .vidioc_reqbufs = vidioc_reqbufs,
  1493. .vidioc_querybuf = vidioc_querybuf,
  1494. .vidioc_qbuf = vidioc_qbuf,
  1495. .vidioc_dqbuf = vidioc_dqbuf,
  1496. .vidioc_streamon = vidioc_streamon,
  1497. .vidioc_streamoff = vidioc_streamoff,
  1498. };
  1499. static const struct v4l2_file_operations omap_vout_fops = {
  1500. .owner = THIS_MODULE,
  1501. .poll = omap_vout_poll,
  1502. .unlocked_ioctl = video_ioctl2,
  1503. .mmap = omap_vout_mmap,
  1504. .open = omap_vout_open,
  1505. .release = omap_vout_release,
  1506. };
  1507. /* Init functions used during driver initialization */
  1508. /* Initial setup of video_data */
  1509. static int __init omap_vout_setup_video_data(struct omap_vout_device *vout)
  1510. {
  1511. struct video_device *vfd;
  1512. struct v4l2_pix_format *pix;
  1513. struct omap_overlay *ovl = vout->vid_info.overlays[0];
  1514. struct omap_dss_device *display = ovl->get_device(ovl);
  1515. struct v4l2_ctrl_handler *hdl;
  1516. /* set the default pix */
  1517. pix = &vout->pix;
  1518. /* Set the default picture of QVGA */
  1519. pix->width = QQVGA_WIDTH;
  1520. pix->height = QQVGA_HEIGHT;
  1521. /* Default pixel format is RGB 5-6-5 */
  1522. pix->pixelformat = V4L2_PIX_FMT_RGB565;
  1523. pix->field = V4L2_FIELD_ANY;
  1524. pix->bytesperline = pix->width * 2;
  1525. pix->sizeimage = pix->bytesperline * pix->height;
  1526. pix->colorspace = V4L2_COLORSPACE_JPEG;
  1527. vout->bpp = RGB565_BPP;
  1528. vout->fbuf.fmt.width = display->panel.timings.x_res;
  1529. vout->fbuf.fmt.height = display->panel.timings.y_res;
  1530. /* Set the data structures for the overlay parameters*/
  1531. vout->win.global_alpha = 255;
  1532. vout->fbuf.flags = 0;
  1533. vout->fbuf.capability = V4L2_FBUF_CAP_LOCAL_ALPHA |
  1534. V4L2_FBUF_CAP_SRC_CHROMAKEY | V4L2_FBUF_CAP_CHROMAKEY;
  1535. vout->win.chromakey = 0;
  1536. omap_vout_new_format(pix, &vout->fbuf, &vout->crop, &vout->win);
  1537. hdl = &vout->ctrl_handler;
  1538. v4l2_ctrl_handler_init(hdl, 3);
  1539. v4l2_ctrl_new_std(hdl, &omap_vout_ctrl_ops,
  1540. V4L2_CID_ROTATE, 0, 270, 90, 0);
  1541. v4l2_ctrl_new_std(hdl, &omap_vout_ctrl_ops,
  1542. V4L2_CID_BG_COLOR, 0, 0xffffff, 1, 0);
  1543. v4l2_ctrl_new_std(hdl, &omap_vout_ctrl_ops,
  1544. V4L2_CID_VFLIP, 0, 1, 1, 0);
  1545. if (hdl->error)
  1546. return hdl->error;
  1547. vout->rotation = 0;
  1548. vout->mirror = false;
  1549. if (vout->vid_info.rotation_type == VOUT_ROT_VRFB)
  1550. vout->vrfb_bpp = 2;
  1551. /* initialize the video_device struct */
  1552. vfd = vout->vfd = video_device_alloc();
  1553. if (!vfd) {
  1554. printk(KERN_ERR VOUT_NAME
  1555. ": could not allocate video device struct\n");
  1556. v4l2_ctrl_handler_free(hdl);
  1557. return -ENOMEM;
  1558. }
  1559. vfd->ctrl_handler = hdl;
  1560. vfd->release = video_device_release;
  1561. vfd->ioctl_ops = &vout_ioctl_ops;
  1562. strlcpy(vfd->name, VOUT_NAME, sizeof(vfd->name));
  1563. vfd->fops = &omap_vout_fops;
  1564. vfd->v4l2_dev = &vout->vid_dev->v4l2_dev;
  1565. vfd->vfl_dir = VFL_DIR_TX;
  1566. mutex_init(&vout->lock);
  1567. vfd->minor = -1;
  1568. return 0;
  1569. }
  1570. /* Setup video buffers */
  1571. static int __init omap_vout_setup_video_bufs(struct platform_device *pdev,
  1572. int vid_num)
  1573. {
  1574. u32 numbuffers;
  1575. int ret = 0, i;
  1576. struct omapvideo_info *ovid;
  1577. struct omap_vout_device *vout;
  1578. struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
  1579. struct omap2video_device *vid_dev =
  1580. container_of(v4l2_dev, struct omap2video_device, v4l2_dev);
  1581. vout = vid_dev->vouts[vid_num];
  1582. ovid = &vout->vid_info;
  1583. numbuffers = (vid_num == 0) ? video1_numbuffers : video2_numbuffers;
  1584. vout->buffer_size = (vid_num == 0) ? video1_bufsize : video2_bufsize;
  1585. dev_info(&pdev->dev, "Buffer Size = %d\n", vout->buffer_size);
  1586. for (i = 0; i < numbuffers; i++) {
  1587. vout->buf_virt_addr[i] =
  1588. omap_vout_alloc_buffer(vout->buffer_size,
  1589. (u32 *) &vout->buf_phy_addr[i]);
  1590. if (!vout->buf_virt_addr[i]) {
  1591. numbuffers = i;
  1592. ret = -ENOMEM;
  1593. goto free_buffers;
  1594. }
  1595. }
  1596. vout->cropped_offset = 0;
  1597. if (ovid->rotation_type == VOUT_ROT_VRFB) {
  1598. bool static_vrfb_allocation = (vid_num == 0) ?
  1599. vid1_static_vrfb_alloc : vid2_static_vrfb_alloc;
  1600. ret = omap_vout_setup_vrfb_bufs(pdev, vid_num,
  1601. static_vrfb_allocation);
  1602. }
  1603. return ret;
  1604. free_buffers:
  1605. for (i = 0; i < numbuffers; i++) {
  1606. omap_vout_free_buffer(vout->buf_virt_addr[i],
  1607. vout->buffer_size);
  1608. vout->buf_virt_addr[i] = 0;
  1609. vout->buf_phy_addr[i] = 0;
  1610. }
  1611. return ret;
  1612. }
  1613. /* Create video out devices */
  1614. static int __init omap_vout_create_video_devices(struct platform_device *pdev)
  1615. {
  1616. int ret = 0, k;
  1617. struct omap_vout_device *vout;
  1618. struct video_device *vfd = NULL;
  1619. struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
  1620. struct omap2video_device *vid_dev = container_of(v4l2_dev,
  1621. struct omap2video_device, v4l2_dev);
  1622. for (k = 0; k < pdev->num_resources; k++) {
  1623. vout = kzalloc(sizeof(struct omap_vout_device), GFP_KERNEL);
  1624. if (!vout) {
  1625. dev_err(&pdev->dev, ": could not allocate memory\n");
  1626. return -ENOMEM;
  1627. }
  1628. vout->vid = k;
  1629. vid_dev->vouts[k] = vout;
  1630. vout->vid_dev = vid_dev;
  1631. /* Select video2 if only 1 overlay is controlled by V4L2 */
  1632. if (pdev->num_resources == 1)
  1633. vout->vid_info.overlays[0] = vid_dev->overlays[k + 2];
  1634. else
  1635. /* Else select video1 and video2 one by one. */
  1636. vout->vid_info.overlays[0] = vid_dev->overlays[k + 1];
  1637. vout->vid_info.num_overlays = 1;
  1638. vout->vid_info.id = k + 1;
  1639. /* Set VRFB as rotation_type for omap2 and omap3 */
  1640. if (omap_vout_dss_omap24xx() || omap_vout_dss_omap34xx())
  1641. vout->vid_info.rotation_type = VOUT_ROT_VRFB;
  1642. /* Setup the default configuration for the video devices
  1643. */
  1644. if (omap_vout_setup_video_data(vout) != 0) {
  1645. ret = -ENOMEM;
  1646. goto error;
  1647. }
  1648. /* Allocate default number of buffers for the video streaming
  1649. * and reserve the VRFB space for rotation
  1650. */
  1651. if (omap_vout_setup_video_bufs(pdev, k) != 0) {
  1652. ret = -ENOMEM;
  1653. goto error1;
  1654. }
  1655. /* Register the Video device with V4L2
  1656. */
  1657. vfd = vout->vfd;
  1658. if (video_register_device(vfd, VFL_TYPE_GRABBER, -1) < 0) {
  1659. dev_err(&pdev->dev,
  1660. ": Could not register Video for Linux device\n");
  1661. vfd->minor = -1;
  1662. ret = -ENODEV;
  1663. goto error2;
  1664. }
  1665. video_set_drvdata(vfd, vout);
  1666. dev_info(&pdev->dev,
  1667. ": registered and initialized video device %d\n",
  1668. vfd->minor);
  1669. if (k == (pdev->num_resources - 1))
  1670. return 0;
  1671. continue;
  1672. error2:
  1673. if (vout->vid_info.rotation_type == VOUT_ROT_VRFB)
  1674. omap_vout_release_vrfb(vout);
  1675. omap_vout_free_buffers(vout);
  1676. error1:
  1677. video_device_release(vfd);
  1678. error:
  1679. kfree(vout);
  1680. return ret;
  1681. }
  1682. return -ENODEV;
  1683. }
  1684. /* Driver functions */
  1685. static void omap_vout_cleanup_device(struct omap_vout_device *vout)
  1686. {
  1687. struct video_device *vfd;
  1688. struct omapvideo_info *ovid;
  1689. if (!vout)
  1690. return;
  1691. vfd = vout->vfd;
  1692. ovid = &vout->vid_info;
  1693. if (vfd) {
  1694. if (!video_is_registered(vfd)) {
  1695. /*
  1696. * The device was never registered, so release the
  1697. * video_device struct directly.
  1698. */
  1699. video_device_release(vfd);
  1700. } else {
  1701. /*
  1702. * The unregister function will release the video_device
  1703. * struct as well as unregistering it.
  1704. */
  1705. video_unregister_device(vfd);
  1706. }
  1707. }
  1708. v4l2_ctrl_handler_free(&vout->ctrl_handler);
  1709. if (ovid->rotation_type == VOUT_ROT_VRFB) {
  1710. omap_vout_release_vrfb(vout);
  1711. /* Free the VRFB buffer if allocated
  1712. * init time
  1713. */
  1714. if (vout->vrfb_static_allocation)
  1715. omap_vout_free_vrfb_buffers(vout);
  1716. }
  1717. omap_vout_free_buffers(vout);
  1718. kfree(vout);
  1719. }
  1720. static int omap_vout_remove(struct platform_device *pdev)
  1721. {
  1722. int k;
  1723. struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
  1724. struct omap2video_device *vid_dev = container_of(v4l2_dev, struct
  1725. omap2video_device, v4l2_dev);
  1726. v4l2_device_unregister(v4l2_dev);
  1727. for (k = 0; k < pdev->num_resources; k++)
  1728. omap_vout_cleanup_device(vid_dev->vouts[k]);
  1729. for (k = 0; k < vid_dev->num_displays; k++) {
  1730. if (vid_dev->displays[k]->state != OMAP_DSS_DISPLAY_DISABLED)
  1731. vid_dev->displays[k]->driver->disable(vid_dev->displays[k]);
  1732. omap_dss_put_device(vid_dev->displays[k]);
  1733. }
  1734. kfree(vid_dev);
  1735. return 0;
  1736. }
  1737. static int __init omap_vout_probe(struct platform_device *pdev)
  1738. {
  1739. int ret = 0, i;
  1740. struct omap_overlay *ovl;
  1741. struct omap_dss_device *dssdev = NULL;
  1742. struct omap_dss_device *def_display;
  1743. struct omap2video_device *vid_dev = NULL;
  1744. if (omapdss_is_initialized() == false)
  1745. return -EPROBE_DEFER;
  1746. ret = omapdss_compat_init();
  1747. if (ret) {
  1748. dev_err(&pdev->dev, "failed to init dss\n");
  1749. return ret;
  1750. }
  1751. if (pdev->num_resources == 0) {
  1752. dev_err(&pdev->dev, "probed for an unknown device\n");
  1753. ret = -ENODEV;
  1754. goto err_dss_init;
  1755. }
  1756. vid_dev = kzalloc(sizeof(struct omap2video_device), GFP_KERNEL);
  1757. if (vid_dev == NULL) {
  1758. ret = -ENOMEM;
  1759. goto err_dss_init;
  1760. }
  1761. vid_dev->num_displays = 0;
  1762. for_each_dss_dev(dssdev) {
  1763. omap_dss_get_device(dssdev);
  1764. if (!dssdev->driver) {
  1765. dev_warn(&pdev->dev, "no driver for display: %s\n",
  1766. dssdev->name);
  1767. omap_dss_put_device(dssdev);
  1768. continue;
  1769. }
  1770. vid_dev->displays[vid_dev->num_displays++] = dssdev;
  1771. }
  1772. if (vid_dev->num_displays == 0) {
  1773. dev_err(&pdev->dev, "no displays\n");
  1774. ret = -EINVAL;
  1775. goto probe_err0;
  1776. }
  1777. vid_dev->num_overlays = omap_dss_get_num_overlays();
  1778. for (i = 0; i < vid_dev->num_overlays; i++)
  1779. vid_dev->overlays[i] = omap_dss_get_overlay(i);
  1780. vid_dev->num_managers = omap_dss_get_num_overlay_managers();
  1781. for (i = 0; i < vid_dev->num_managers; i++)
  1782. vid_dev->managers[i] = omap_dss_get_overlay_manager(i);
  1783. /* Get the Video1 overlay and video2 overlay.
  1784. * Setup the Display attached to that overlays
  1785. */
  1786. for (i = 1; i < vid_dev->num_overlays; i++) {
  1787. ovl = omap_dss_get_overlay(i);
  1788. dssdev = ovl->get_device(ovl);
  1789. if (dssdev) {
  1790. def_display = dssdev;
  1791. } else {
  1792. dev_warn(&pdev->dev, "cannot find display\n");
  1793. def_display = NULL;
  1794. }
  1795. if (def_display) {
  1796. struct omap_dss_driver *dssdrv = def_display->driver;
  1797. ret = dssdrv->enable(def_display);
  1798. if (ret) {
  1799. /* Here we are not considering a error
  1800. * as display may be enabled by frame
  1801. * buffer driver
  1802. */
  1803. dev_warn(&pdev->dev,
  1804. "'%s' Display already enabled\n",
  1805. def_display->name);
  1806. }
  1807. }
  1808. }
  1809. if (v4l2_device_register(&pdev->dev, &vid_dev->v4l2_dev) < 0) {
  1810. dev_err(&pdev->dev, "v4l2_device_register failed\n");
  1811. ret = -ENODEV;
  1812. goto probe_err1;
  1813. }
  1814. ret = omap_vout_create_video_devices(pdev);
  1815. if (ret)
  1816. goto probe_err2;
  1817. for (i = 0; i < vid_dev->num_displays; i++) {
  1818. struct omap_dss_device *display = vid_dev->displays[i];
  1819. if (display->driver->update)
  1820. display->driver->update(display, 0, 0,
  1821. display->panel.timings.x_res,
  1822. display->panel.timings.y_res);
  1823. }
  1824. return 0;
  1825. probe_err2:
  1826. v4l2_device_unregister(&vid_dev->v4l2_dev);
  1827. probe_err1:
  1828. for (i = 1; i < vid_dev->num_overlays; i++) {
  1829. def_display = NULL;
  1830. ovl = omap_dss_get_overlay(i);
  1831. dssdev = ovl->get_device(ovl);
  1832. if (dssdev)
  1833. def_display = dssdev;
  1834. if (def_display && def_display->driver)
  1835. def_display->driver->disable(def_display);
  1836. }
  1837. probe_err0:
  1838. kfree(vid_dev);
  1839. err_dss_init:
  1840. omapdss_compat_uninit();
  1841. return ret;
  1842. }
  1843. static struct platform_driver omap_vout_driver = {
  1844. .driver = {
  1845. .name = VOUT_NAME,
  1846. },
  1847. .remove = omap_vout_remove,
  1848. };
  1849. static int __init omap_vout_init(void)
  1850. {
  1851. if (platform_driver_probe(&omap_vout_driver, omap_vout_probe) != 0) {
  1852. printk(KERN_ERR VOUT_NAME ":Could not register Video driver\n");
  1853. return -EINVAL;
  1854. }
  1855. return 0;
  1856. }
  1857. static void omap_vout_cleanup(void)
  1858. {
  1859. platform_driver_unregister(&omap_vout_driver);
  1860. }
  1861. late_initcall(omap_vout_init);
  1862. module_exit(omap_vout_cleanup);