omap_vout.c 57 KB

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