omap_vout.c 56 KB

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