omap_vout.c 57 KB

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