omap_vout.c 55 KB

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