omap_vout.c 66 KB

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