omap_vout.c 56 KB

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