omap_vout.c 57 KB

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