omap_vout.c 57 KB

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