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