omap_vout.c 57 KB

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