omap_vout.c 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636
  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 <video/omapdss.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 respectively 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. /* Check the size of the buffer */
  861. if (*size > vout->buffer_size) {
  862. v4l2_err(&vout->vid_dev->v4l2_dev,
  863. "buffer allocation mismatch [%u] [%u]\n",
  864. *size, vout->buffer_size);
  865. return -ENOMEM;
  866. }
  867. for (i = startindex; i < *count; i++) {
  868. vout->buffer_size = *size;
  869. virt_addr = omap_vout_alloc_buffer(vout->buffer_size,
  870. &phy_addr);
  871. if (!virt_addr) {
  872. if (!rotation_enabled(vout))
  873. break;
  874. /* Free the VRFB buffers if no space for V4L2 buffers */
  875. for (j = i; j < *count; j++) {
  876. omap_vout_free_buffer(
  877. vout->smsshado_virt_addr[j],
  878. vout->smsshado_size);
  879. vout->smsshado_virt_addr[j] = 0;
  880. vout->smsshado_phy_addr[j] = 0;
  881. }
  882. }
  883. vout->buf_virt_addr[i] = virt_addr;
  884. vout->buf_phy_addr[i] = phy_addr;
  885. }
  886. *count = vout->buffer_allocated = i;
  887. return 0;
  888. }
  889. /*
  890. * Free the V4L2 buffers additionally allocated than default
  891. * number of buffers and free all the VRFB buffers
  892. */
  893. static void omap_vout_free_allbuffers(struct omap_vout_device *vout)
  894. {
  895. int num_buffers = 0, i;
  896. num_buffers = (vout->vid == OMAP_VIDEO1) ?
  897. video1_numbuffers : video2_numbuffers;
  898. for (i = num_buffers; i < vout->buffer_allocated; i++) {
  899. if (vout->buf_virt_addr[i])
  900. omap_vout_free_buffer(vout->buf_virt_addr[i],
  901. vout->buffer_size);
  902. vout->buf_virt_addr[i] = 0;
  903. vout->buf_phy_addr[i] = 0;
  904. }
  905. /* Free the VRFB buffers only if they are allocated
  906. * during reqbufs. Don't free if init time allocated
  907. */
  908. if (!vout->vrfb_static_allocation) {
  909. for (i = 0; i < VRFB_NUM_BUFS; i++) {
  910. if (vout->smsshado_virt_addr[i]) {
  911. omap_vout_free_buffer(
  912. vout->smsshado_virt_addr[i],
  913. vout->smsshado_size);
  914. vout->smsshado_virt_addr[i] = 0;
  915. vout->smsshado_phy_addr[i] = 0;
  916. }
  917. }
  918. }
  919. vout->buffer_allocated = num_buffers;
  920. }
  921. /*
  922. * This function will be called when VIDIOC_QBUF ioctl is called.
  923. * It prepare buffers before give out for the display. This function
  924. * converts user space virtual address into physical address if userptr memory
  925. * exchange mechanism is used. If rotation is enabled, it copies entire
  926. * buffer into VRFB memory space before giving it to the DSS.
  927. */
  928. static int omap_vout_buffer_prepare(struct videobuf_queue *q,
  929. struct videobuf_buffer *vb,
  930. enum v4l2_field field)
  931. {
  932. dma_addr_t dmabuf;
  933. struct vid_vrfb_dma *tx;
  934. enum dss_rotation rotation;
  935. struct omap_vout_device *vout = q->priv_data;
  936. u32 dest_frame_index = 0, src_element_index = 0;
  937. u32 dest_element_index = 0, src_frame_index = 0;
  938. u32 elem_count = 0, frame_count = 0, pixsize = 2;
  939. if (VIDEOBUF_NEEDS_INIT == vb->state) {
  940. vb->width = vout->pix.width;
  941. vb->height = vout->pix.height;
  942. vb->size = vb->width * vb->height * vout->bpp;
  943. vb->field = field;
  944. }
  945. vb->state = VIDEOBUF_PREPARED;
  946. /* if user pointer memory mechanism is used, get the physical
  947. * address of the buffer
  948. */
  949. if (V4L2_MEMORY_USERPTR == vb->memory) {
  950. if (0 == vb->baddr)
  951. return -EINVAL;
  952. /* Physical address */
  953. vout->queued_buf_addr[vb->i] = (u8 *)
  954. omap_vout_uservirt_to_phys(vb->baddr);
  955. } else {
  956. vout->queued_buf_addr[vb->i] = (u8 *)vout->buf_phy_addr[vb->i];
  957. }
  958. if (!rotation_enabled(vout))
  959. return 0;
  960. dmabuf = vout->buf_phy_addr[vb->i];
  961. /* If rotation is enabled, copy input buffer into VRFB
  962. * memory space using DMA. We are copying input buffer
  963. * into VRFB memory space of desired angle and DSS will
  964. * read image VRFB memory for 0 degree angle
  965. */
  966. pixsize = vout->bpp * vout->vrfb_bpp;
  967. /*
  968. * DMA transfer in double index mode
  969. */
  970. /* Frame index */
  971. dest_frame_index = ((MAX_PIXELS_PER_LINE * pixsize) -
  972. (vout->pix.width * vout->bpp)) + 1;
  973. /* Source and destination parameters */
  974. src_element_index = 0;
  975. src_frame_index = 0;
  976. dest_element_index = 1;
  977. /* Number of elements per frame */
  978. elem_count = vout->pix.width * vout->bpp;
  979. frame_count = vout->pix.height;
  980. tx = &vout->vrfb_dma_tx;
  981. tx->tx_status = 0;
  982. omap_set_dma_transfer_params(tx->dma_ch, OMAP_DMA_DATA_TYPE_S32,
  983. (elem_count / 4), frame_count, OMAP_DMA_SYNC_ELEMENT,
  984. tx->dev_id, 0x0);
  985. /* src_port required only for OMAP1 */
  986. omap_set_dma_src_params(tx->dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
  987. dmabuf, src_element_index, src_frame_index);
  988. /*set dma source burst mode for VRFB */
  989. omap_set_dma_src_burst_mode(tx->dma_ch, OMAP_DMA_DATA_BURST_16);
  990. rotation = calc_rotation(vout);
  991. /* dest_port required only for OMAP1 */
  992. omap_set_dma_dest_params(tx->dma_ch, 0, OMAP_DMA_AMODE_DOUBLE_IDX,
  993. vout->vrfb_context[vb->i].paddr[0], dest_element_index,
  994. dest_frame_index);
  995. /*set dma dest burst mode for VRFB */
  996. omap_set_dma_dest_burst_mode(tx->dma_ch, OMAP_DMA_DATA_BURST_16);
  997. omap_dma_set_global_params(DMA_DEFAULT_ARB_RATE, 0x20, 0);
  998. omap_start_dma(tx->dma_ch);
  999. interruptible_sleep_on_timeout(&tx->wait, VRFB_TX_TIMEOUT);
  1000. if (tx->tx_status == 0) {
  1001. omap_stop_dma(tx->dma_ch);
  1002. return -EINVAL;
  1003. }
  1004. /* Store buffers physical address into an array. Addresses
  1005. * from this array will be used to configure DSS */
  1006. vout->queued_buf_addr[vb->i] = (u8 *)
  1007. vout->vrfb_context[vb->i].paddr[rotation];
  1008. return 0;
  1009. }
  1010. /*
  1011. * Buffer queue function will be called from the videobuf layer when _QBUF
  1012. * ioctl is called. It is used to enqueue buffer, which is ready to be
  1013. * displayed.
  1014. */
  1015. static void omap_vout_buffer_queue(struct videobuf_queue *q,
  1016. struct videobuf_buffer *vb)
  1017. {
  1018. struct omap_vout_device *vout = q->priv_data;
  1019. /* Driver is also maintainig a queue. So enqueue buffer in the driver
  1020. * queue */
  1021. list_add_tail(&vb->queue, &vout->dma_queue);
  1022. vb->state = VIDEOBUF_QUEUED;
  1023. }
  1024. /*
  1025. * Buffer release function is called from videobuf layer to release buffer
  1026. * which are already allocated
  1027. */
  1028. static void omap_vout_buffer_release(struct videobuf_queue *q,
  1029. struct videobuf_buffer *vb)
  1030. {
  1031. struct omap_vout_device *vout = q->priv_data;
  1032. vb->state = VIDEOBUF_NEEDS_INIT;
  1033. if (V4L2_MEMORY_MMAP != vout->memory)
  1034. return;
  1035. }
  1036. /*
  1037. * File operations
  1038. */
  1039. static void omap_vout_vm_open(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_open [vma=%08lx-%08lx]\n", vma->vm_start, vma->vm_end);
  1044. vout->mmap_count++;
  1045. }
  1046. static void omap_vout_vm_close(struct vm_area_struct *vma)
  1047. {
  1048. struct omap_vout_device *vout = vma->vm_private_data;
  1049. v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
  1050. "vm_close [vma=%08lx-%08lx]\n", vma->vm_start, vma->vm_end);
  1051. vout->mmap_count--;
  1052. }
  1053. static struct vm_operations_struct omap_vout_vm_ops = {
  1054. .open = omap_vout_vm_open,
  1055. .close = omap_vout_vm_close,
  1056. };
  1057. static int omap_vout_mmap(struct file *file, struct vm_area_struct *vma)
  1058. {
  1059. int i;
  1060. void *pos;
  1061. unsigned long start = vma->vm_start;
  1062. unsigned long size = (vma->vm_end - vma->vm_start);
  1063. struct omap_vout_device *vout = file->private_data;
  1064. struct videobuf_queue *q = &vout->vbq;
  1065. v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
  1066. " %s pgoff=0x%lx, start=0x%lx, end=0x%lx\n", __func__,
  1067. vma->vm_pgoff, vma->vm_start, vma->vm_end);
  1068. /* look for the buffer to map */
  1069. for (i = 0; i < VIDEO_MAX_FRAME; i++) {
  1070. if (NULL == q->bufs[i])
  1071. continue;
  1072. if (V4L2_MEMORY_MMAP != q->bufs[i]->memory)
  1073. continue;
  1074. if (q->bufs[i]->boff == (vma->vm_pgoff << PAGE_SHIFT))
  1075. break;
  1076. }
  1077. if (VIDEO_MAX_FRAME == i) {
  1078. v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
  1079. "offset invalid [offset=0x%lx]\n",
  1080. (vma->vm_pgoff << PAGE_SHIFT));
  1081. return -EINVAL;
  1082. }
  1083. /* Check the size of the buffer */
  1084. if (size > vout->buffer_size) {
  1085. v4l2_err(&vout->vid_dev->v4l2_dev,
  1086. "insufficient memory [%lu] [%u]\n",
  1087. size, vout->buffer_size);
  1088. return -ENOMEM;
  1089. }
  1090. q->bufs[i]->baddr = vma->vm_start;
  1091. vma->vm_flags |= VM_RESERVED;
  1092. vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
  1093. vma->vm_ops = &omap_vout_vm_ops;
  1094. vma->vm_private_data = (void *) vout;
  1095. pos = (void *)vout->buf_virt_addr[i];
  1096. vma->vm_pgoff = virt_to_phys((void *)pos) >> PAGE_SHIFT;
  1097. while (size > 0) {
  1098. unsigned long pfn;
  1099. pfn = virt_to_phys((void *) pos) >> PAGE_SHIFT;
  1100. if (remap_pfn_range(vma, start, pfn, PAGE_SIZE, PAGE_SHARED))
  1101. return -EAGAIN;
  1102. start += PAGE_SIZE;
  1103. pos += PAGE_SIZE;
  1104. size -= PAGE_SIZE;
  1105. }
  1106. vout->mmap_count++;
  1107. v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Exiting %s\n", __func__);
  1108. return 0;
  1109. }
  1110. static int omap_vout_release(struct file *file)
  1111. {
  1112. unsigned int ret, i;
  1113. struct videobuf_queue *q;
  1114. struct omapvideo_info *ovid;
  1115. struct omap_vout_device *vout = file->private_data;
  1116. v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Entering %s\n", __func__);
  1117. ovid = &vout->vid_info;
  1118. if (!vout)
  1119. return 0;
  1120. q = &vout->vbq;
  1121. /* Disable all the overlay managers connected with this interface */
  1122. for (i = 0; i < ovid->num_overlays; i++) {
  1123. struct omap_overlay *ovl = ovid->overlays[i];
  1124. if (ovl->manager && ovl->manager->device) {
  1125. struct omap_overlay_info info;
  1126. ovl->get_overlay_info(ovl, &info);
  1127. info.enabled = 0;
  1128. ovl->set_overlay_info(ovl, &info);
  1129. }
  1130. }
  1131. /* Turn off the pipeline */
  1132. ret = omapvid_apply_changes(vout);
  1133. if (ret)
  1134. v4l2_warn(&vout->vid_dev->v4l2_dev,
  1135. "Unable to apply changes\n");
  1136. /* Free all buffers */
  1137. omap_vout_free_allbuffers(vout);
  1138. videobuf_mmap_free(q);
  1139. /* Even if apply changes fails we should continue
  1140. freeing allocated memory */
  1141. if (vout->streaming) {
  1142. u32 mask = 0;
  1143. mask = DISPC_IRQ_VSYNC | DISPC_IRQ_EVSYNC_EVEN |
  1144. DISPC_IRQ_EVSYNC_ODD;
  1145. omap_dispc_unregister_isr(omap_vout_isr, vout, mask);
  1146. vout->streaming = 0;
  1147. videobuf_streamoff(q);
  1148. videobuf_queue_cancel(q);
  1149. }
  1150. if (vout->mmap_count != 0)
  1151. vout->mmap_count = 0;
  1152. vout->opened -= 1;
  1153. file->private_data = NULL;
  1154. if (vout->buffer_allocated)
  1155. videobuf_mmap_free(q);
  1156. v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Exiting %s\n", __func__);
  1157. return ret;
  1158. }
  1159. static int omap_vout_open(struct file *file)
  1160. {
  1161. struct videobuf_queue *q;
  1162. struct omap_vout_device *vout = NULL;
  1163. vout = video_drvdata(file);
  1164. v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Entering %s\n", __func__);
  1165. if (vout == NULL)
  1166. return -ENODEV;
  1167. /* for now, we only support single open */
  1168. if (vout->opened)
  1169. return -EBUSY;
  1170. vout->opened += 1;
  1171. file->private_data = vout;
  1172. vout->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  1173. q = &vout->vbq;
  1174. video_vbq_ops.buf_setup = omap_vout_buffer_setup;
  1175. video_vbq_ops.buf_prepare = omap_vout_buffer_prepare;
  1176. video_vbq_ops.buf_release = omap_vout_buffer_release;
  1177. video_vbq_ops.buf_queue = omap_vout_buffer_queue;
  1178. spin_lock_init(&vout->vbq_lock);
  1179. videobuf_queue_dma_contig_init(q, &video_vbq_ops, q->dev,
  1180. &vout->vbq_lock, vout->type, V4L2_FIELD_NONE,
  1181. sizeof(struct videobuf_buffer), vout, NULL);
  1182. v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Exiting %s\n", __func__);
  1183. return 0;
  1184. }
  1185. /*
  1186. * V4L2 ioctls
  1187. */
  1188. static int vidioc_querycap(struct file *file, void *fh,
  1189. struct v4l2_capability *cap)
  1190. {
  1191. struct omap_vout_device *vout = fh;
  1192. strlcpy(cap->driver, VOUT_NAME, sizeof(cap->driver));
  1193. strlcpy(cap->card, vout->vfd->name, sizeof(cap->card));
  1194. cap->bus_info[0] = '\0';
  1195. cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_OUTPUT;
  1196. return 0;
  1197. }
  1198. static int vidioc_enum_fmt_vid_out(struct file *file, void *fh,
  1199. struct v4l2_fmtdesc *fmt)
  1200. {
  1201. int index = fmt->index;
  1202. enum v4l2_buf_type type = fmt->type;
  1203. fmt->index = index;
  1204. fmt->type = type;
  1205. if (index >= NUM_OUTPUT_FORMATS)
  1206. return -EINVAL;
  1207. fmt->flags = omap_formats[index].flags;
  1208. strlcpy(fmt->description, omap_formats[index].description,
  1209. sizeof(fmt->description));
  1210. fmt->pixelformat = omap_formats[index].pixelformat;
  1211. return 0;
  1212. }
  1213. static int vidioc_g_fmt_vid_out(struct file *file, void *fh,
  1214. struct v4l2_format *f)
  1215. {
  1216. struct omap_vout_device *vout = fh;
  1217. f->fmt.pix = vout->pix;
  1218. return 0;
  1219. }
  1220. static int vidioc_try_fmt_vid_out(struct file *file, void *fh,
  1221. struct v4l2_format *f)
  1222. {
  1223. struct omap_overlay *ovl;
  1224. struct omapvideo_info *ovid;
  1225. struct omap_video_timings *timing;
  1226. struct omap_vout_device *vout = fh;
  1227. ovid = &vout->vid_info;
  1228. ovl = ovid->overlays[0];
  1229. if (!ovl->manager || !ovl->manager->device)
  1230. return -EINVAL;
  1231. /* get the display device attached to the overlay */
  1232. timing = &ovl->manager->device->panel.timings;
  1233. vout->fbuf.fmt.height = timing->y_res;
  1234. vout->fbuf.fmt.width = timing->x_res;
  1235. omap_vout_try_format(&f->fmt.pix);
  1236. return 0;
  1237. }
  1238. static int vidioc_s_fmt_vid_out(struct file *file, void *fh,
  1239. struct v4l2_format *f)
  1240. {
  1241. int ret, bpp;
  1242. struct omap_overlay *ovl;
  1243. struct omapvideo_info *ovid;
  1244. struct omap_video_timings *timing;
  1245. struct omap_vout_device *vout = fh;
  1246. if (vout->streaming)
  1247. return -EBUSY;
  1248. mutex_lock(&vout->lock);
  1249. ovid = &vout->vid_info;
  1250. ovl = ovid->overlays[0];
  1251. /* get the display device attached to the overlay */
  1252. if (!ovl->manager || !ovl->manager->device) {
  1253. ret = -EINVAL;
  1254. goto s_fmt_vid_out_exit;
  1255. }
  1256. timing = &ovl->manager->device->panel.timings;
  1257. /* We dont support RGB24-packed mode if vrfb rotation
  1258. * is enabled*/
  1259. if ((rotation_enabled(vout)) &&
  1260. f->fmt.pix.pixelformat == V4L2_PIX_FMT_RGB24) {
  1261. ret = -EINVAL;
  1262. goto s_fmt_vid_out_exit;
  1263. }
  1264. /* get the framebuffer parameters */
  1265. if (rotate_90_or_270(vout)) {
  1266. vout->fbuf.fmt.height = timing->x_res;
  1267. vout->fbuf.fmt.width = timing->y_res;
  1268. } else {
  1269. vout->fbuf.fmt.height = timing->y_res;
  1270. vout->fbuf.fmt.width = timing->x_res;
  1271. }
  1272. /* change to samller size is OK */
  1273. bpp = omap_vout_try_format(&f->fmt.pix);
  1274. f->fmt.pix.sizeimage = f->fmt.pix.width * f->fmt.pix.height * bpp;
  1275. /* try & set the new output format */
  1276. vout->bpp = bpp;
  1277. vout->pix = f->fmt.pix;
  1278. vout->vrfb_bpp = 1;
  1279. /* If YUYV then vrfb bpp is 2, for others its 1 */
  1280. if (V4L2_PIX_FMT_YUYV == vout->pix.pixelformat ||
  1281. V4L2_PIX_FMT_UYVY == vout->pix.pixelformat)
  1282. vout->vrfb_bpp = 2;
  1283. /* set default crop and win */
  1284. omap_vout_new_format(&vout->pix, &vout->fbuf, &vout->crop, &vout->win);
  1285. /* Save the changes in the overlay strcuture */
  1286. ret = omapvid_init(vout, 0);
  1287. if (ret) {
  1288. v4l2_err(&vout->vid_dev->v4l2_dev, "failed to change mode\n");
  1289. goto s_fmt_vid_out_exit;
  1290. }
  1291. ret = 0;
  1292. s_fmt_vid_out_exit:
  1293. mutex_unlock(&vout->lock);
  1294. return ret;
  1295. }
  1296. static int vidioc_try_fmt_vid_overlay(struct file *file, void *fh,
  1297. struct v4l2_format *f)
  1298. {
  1299. int ret = 0;
  1300. struct omap_vout_device *vout = fh;
  1301. struct v4l2_window *win = &f->fmt.win;
  1302. ret = omap_vout_try_window(&vout->fbuf, win);
  1303. if (!ret) {
  1304. if (vout->vid == OMAP_VIDEO1)
  1305. win->global_alpha = 255;
  1306. else
  1307. win->global_alpha = f->fmt.win.global_alpha;
  1308. }
  1309. return ret;
  1310. }
  1311. static int vidioc_s_fmt_vid_overlay(struct file *file, void *fh,
  1312. struct v4l2_format *f)
  1313. {
  1314. int ret = 0;
  1315. struct omap_overlay *ovl;
  1316. struct omapvideo_info *ovid;
  1317. struct omap_vout_device *vout = fh;
  1318. struct v4l2_window *win = &f->fmt.win;
  1319. mutex_lock(&vout->lock);
  1320. ovid = &vout->vid_info;
  1321. ovl = ovid->overlays[0];
  1322. ret = omap_vout_new_window(&vout->crop, &vout->win, &vout->fbuf, win);
  1323. if (!ret) {
  1324. /* Video1 plane does not support global alpha */
  1325. if (ovl->id == OMAP_DSS_VIDEO1)
  1326. vout->win.global_alpha = 255;
  1327. else
  1328. vout->win.global_alpha = f->fmt.win.global_alpha;
  1329. vout->win.chromakey = f->fmt.win.chromakey;
  1330. }
  1331. mutex_unlock(&vout->lock);
  1332. return ret;
  1333. }
  1334. static int vidioc_enum_fmt_vid_overlay(struct file *file, void *fh,
  1335. struct v4l2_fmtdesc *fmt)
  1336. {
  1337. int index = fmt->index;
  1338. enum v4l2_buf_type type = fmt->type;
  1339. fmt->index = index;
  1340. fmt->type = type;
  1341. if (index >= NUM_OUTPUT_FORMATS)
  1342. return -EINVAL;
  1343. fmt->flags = omap_formats[index].flags;
  1344. strlcpy(fmt->description, omap_formats[index].description,
  1345. sizeof(fmt->description));
  1346. fmt->pixelformat = omap_formats[index].pixelformat;
  1347. return 0;
  1348. }
  1349. static int vidioc_g_fmt_vid_overlay(struct file *file, void *fh,
  1350. struct v4l2_format *f)
  1351. {
  1352. u32 key_value = 0;
  1353. struct omap_overlay *ovl;
  1354. struct omapvideo_info *ovid;
  1355. struct omap_vout_device *vout = fh;
  1356. struct omap_overlay_manager_info info;
  1357. struct v4l2_window *win = &f->fmt.win;
  1358. ovid = &vout->vid_info;
  1359. ovl = ovid->overlays[0];
  1360. win->w = vout->win.w;
  1361. win->field = vout->win.field;
  1362. win->global_alpha = vout->win.global_alpha;
  1363. if (ovl->manager && ovl->manager->get_manager_info) {
  1364. ovl->manager->get_manager_info(ovl->manager, &info);
  1365. key_value = info.trans_key;
  1366. }
  1367. win->chromakey = key_value;
  1368. return 0;
  1369. }
  1370. static int vidioc_cropcap(struct file *file, void *fh,
  1371. struct v4l2_cropcap *cropcap)
  1372. {
  1373. struct omap_vout_device *vout = fh;
  1374. struct v4l2_pix_format *pix = &vout->pix;
  1375. if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  1376. return -EINVAL;
  1377. /* Width and height are always even */
  1378. cropcap->bounds.width = pix->width & ~1;
  1379. cropcap->bounds.height = pix->height & ~1;
  1380. omap_vout_default_crop(&vout->pix, &vout->fbuf, &cropcap->defrect);
  1381. cropcap->pixelaspect.numerator = 1;
  1382. cropcap->pixelaspect.denominator = 1;
  1383. return 0;
  1384. }
  1385. static int vidioc_g_crop(struct file *file, void *fh, struct v4l2_crop *crop)
  1386. {
  1387. struct omap_vout_device *vout = fh;
  1388. if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  1389. return -EINVAL;
  1390. crop->c = vout->crop;
  1391. return 0;
  1392. }
  1393. static int vidioc_s_crop(struct file *file, void *fh, struct v4l2_crop *crop)
  1394. {
  1395. int ret = -EINVAL;
  1396. struct omap_vout_device *vout = fh;
  1397. struct omapvideo_info *ovid;
  1398. struct omap_overlay *ovl;
  1399. struct omap_video_timings *timing;
  1400. if (vout->streaming)
  1401. return -EBUSY;
  1402. mutex_lock(&vout->lock);
  1403. ovid = &vout->vid_info;
  1404. ovl = ovid->overlays[0];
  1405. if (!ovl->manager || !ovl->manager->device) {
  1406. ret = -EINVAL;
  1407. goto s_crop_err;
  1408. }
  1409. /* get the display device attached to the overlay */
  1410. timing = &ovl->manager->device->panel.timings;
  1411. if (rotate_90_or_270(vout)) {
  1412. vout->fbuf.fmt.height = timing->x_res;
  1413. vout->fbuf.fmt.width = timing->y_res;
  1414. } else {
  1415. vout->fbuf.fmt.height = timing->y_res;
  1416. vout->fbuf.fmt.width = timing->x_res;
  1417. }
  1418. if (crop->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
  1419. ret = omap_vout_new_crop(&vout->pix, &vout->crop, &vout->win,
  1420. &vout->fbuf, &crop->c);
  1421. s_crop_err:
  1422. mutex_unlock(&vout->lock);
  1423. return ret;
  1424. }
  1425. static int vidioc_queryctrl(struct file *file, void *fh,
  1426. struct v4l2_queryctrl *ctrl)
  1427. {
  1428. int ret = 0;
  1429. switch (ctrl->id) {
  1430. case V4L2_CID_ROTATE:
  1431. ret = v4l2_ctrl_query_fill(ctrl, 0, 270, 90, 0);
  1432. break;
  1433. case V4L2_CID_BG_COLOR:
  1434. ret = v4l2_ctrl_query_fill(ctrl, 0, 0xFFFFFF, 1, 0);
  1435. break;
  1436. case V4L2_CID_VFLIP:
  1437. ret = v4l2_ctrl_query_fill(ctrl, 0, 1, 1, 0);
  1438. break;
  1439. default:
  1440. ctrl->name[0] = '\0';
  1441. ret = -EINVAL;
  1442. }
  1443. return ret;
  1444. }
  1445. static int vidioc_g_ctrl(struct file *file, void *fh, struct v4l2_control *ctrl)
  1446. {
  1447. int ret = 0;
  1448. struct omap_vout_device *vout = fh;
  1449. switch (ctrl->id) {
  1450. case V4L2_CID_ROTATE:
  1451. ctrl->value = vout->control[0].value;
  1452. break;
  1453. case V4L2_CID_BG_COLOR:
  1454. {
  1455. struct omap_overlay_manager_info info;
  1456. struct omap_overlay *ovl;
  1457. ovl = vout->vid_info.overlays[0];
  1458. if (!ovl->manager || !ovl->manager->get_manager_info) {
  1459. ret = -EINVAL;
  1460. break;
  1461. }
  1462. ovl->manager->get_manager_info(ovl->manager, &info);
  1463. ctrl->value = info.default_color;
  1464. break;
  1465. }
  1466. case V4L2_CID_VFLIP:
  1467. ctrl->value = vout->control[2].value;
  1468. break;
  1469. default:
  1470. ret = -EINVAL;
  1471. }
  1472. return ret;
  1473. }
  1474. static int vidioc_s_ctrl(struct file *file, void *fh, struct v4l2_control *a)
  1475. {
  1476. int ret = 0;
  1477. struct omap_vout_device *vout = fh;
  1478. switch (a->id) {
  1479. case V4L2_CID_ROTATE:
  1480. {
  1481. int rotation = a->value;
  1482. mutex_lock(&vout->lock);
  1483. if (rotation && vout->pix.pixelformat == V4L2_PIX_FMT_RGB24) {
  1484. mutex_unlock(&vout->lock);
  1485. ret = -EINVAL;
  1486. break;
  1487. }
  1488. if (v4l2_rot_to_dss_rot(rotation, &vout->rotation,
  1489. vout->mirror)) {
  1490. mutex_unlock(&vout->lock);
  1491. ret = -EINVAL;
  1492. break;
  1493. }
  1494. vout->control[0].value = rotation;
  1495. mutex_unlock(&vout->lock);
  1496. break;
  1497. }
  1498. case V4L2_CID_BG_COLOR:
  1499. {
  1500. struct omap_overlay *ovl;
  1501. unsigned int color = a->value;
  1502. struct omap_overlay_manager_info info;
  1503. ovl = vout->vid_info.overlays[0];
  1504. mutex_lock(&vout->lock);
  1505. if (!ovl->manager || !ovl->manager->get_manager_info) {
  1506. mutex_unlock(&vout->lock);
  1507. ret = -EINVAL;
  1508. break;
  1509. }
  1510. ovl->manager->get_manager_info(ovl->manager, &info);
  1511. info.default_color = color;
  1512. if (ovl->manager->set_manager_info(ovl->manager, &info)) {
  1513. mutex_unlock(&vout->lock);
  1514. ret = -EINVAL;
  1515. break;
  1516. }
  1517. vout->control[1].value = color;
  1518. mutex_unlock(&vout->lock);
  1519. break;
  1520. }
  1521. case V4L2_CID_VFLIP:
  1522. {
  1523. struct omap_overlay *ovl;
  1524. struct omapvideo_info *ovid;
  1525. unsigned int mirror = a->value;
  1526. ovid = &vout->vid_info;
  1527. ovl = ovid->overlays[0];
  1528. mutex_lock(&vout->lock);
  1529. if (mirror && vout->pix.pixelformat == V4L2_PIX_FMT_RGB24) {
  1530. mutex_unlock(&vout->lock);
  1531. ret = -EINVAL;
  1532. break;
  1533. }
  1534. vout->mirror = mirror;
  1535. vout->control[2].value = mirror;
  1536. mutex_unlock(&vout->lock);
  1537. break;
  1538. }
  1539. default:
  1540. ret = -EINVAL;
  1541. }
  1542. return ret;
  1543. }
  1544. static int vidioc_reqbufs(struct file *file, void *fh,
  1545. struct v4l2_requestbuffers *req)
  1546. {
  1547. int ret = 0;
  1548. unsigned int i, num_buffers = 0;
  1549. struct omap_vout_device *vout = fh;
  1550. struct videobuf_queue *q = &vout->vbq;
  1551. if ((req->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) || (req->count < 0))
  1552. return -EINVAL;
  1553. /* if memory is not mmp or userptr
  1554. return error */
  1555. if ((V4L2_MEMORY_MMAP != req->memory) &&
  1556. (V4L2_MEMORY_USERPTR != req->memory))
  1557. return -EINVAL;
  1558. mutex_lock(&vout->lock);
  1559. /* Cannot be requested when streaming is on */
  1560. if (vout->streaming) {
  1561. ret = -EBUSY;
  1562. goto reqbuf_err;
  1563. }
  1564. /* If buffers are already allocated free them */
  1565. if (q->bufs[0] && (V4L2_MEMORY_MMAP == q->bufs[0]->memory)) {
  1566. if (vout->mmap_count) {
  1567. ret = -EBUSY;
  1568. goto reqbuf_err;
  1569. }
  1570. num_buffers = (vout->vid == OMAP_VIDEO1) ?
  1571. video1_numbuffers : video2_numbuffers;
  1572. for (i = num_buffers; i < vout->buffer_allocated; i++) {
  1573. omap_vout_free_buffer(vout->buf_virt_addr[i],
  1574. vout->buffer_size);
  1575. vout->buf_virt_addr[i] = 0;
  1576. vout->buf_phy_addr[i] = 0;
  1577. }
  1578. vout->buffer_allocated = num_buffers;
  1579. videobuf_mmap_free(q);
  1580. } else if (q->bufs[0] && (V4L2_MEMORY_USERPTR == q->bufs[0]->memory)) {
  1581. if (vout->buffer_allocated) {
  1582. videobuf_mmap_free(q);
  1583. for (i = 0; i < vout->buffer_allocated; i++) {
  1584. kfree(q->bufs[i]);
  1585. q->bufs[i] = NULL;
  1586. }
  1587. vout->buffer_allocated = 0;
  1588. }
  1589. }
  1590. /*store the memory type in data structure */
  1591. vout->memory = req->memory;
  1592. INIT_LIST_HEAD(&vout->dma_queue);
  1593. /* call videobuf_reqbufs api */
  1594. ret = videobuf_reqbufs(q, req);
  1595. if (ret < 0)
  1596. goto reqbuf_err;
  1597. vout->buffer_allocated = req->count;
  1598. reqbuf_err:
  1599. mutex_unlock(&vout->lock);
  1600. return ret;
  1601. }
  1602. static int vidioc_querybuf(struct file *file, void *fh,
  1603. struct v4l2_buffer *b)
  1604. {
  1605. struct omap_vout_device *vout = fh;
  1606. return videobuf_querybuf(&vout->vbq, b);
  1607. }
  1608. static int vidioc_qbuf(struct file *file, void *fh,
  1609. struct v4l2_buffer *buffer)
  1610. {
  1611. struct omap_vout_device *vout = fh;
  1612. struct videobuf_queue *q = &vout->vbq;
  1613. if ((V4L2_BUF_TYPE_VIDEO_OUTPUT != buffer->type) ||
  1614. (buffer->index >= vout->buffer_allocated) ||
  1615. (q->bufs[buffer->index]->memory != buffer->memory)) {
  1616. return -EINVAL;
  1617. }
  1618. if (V4L2_MEMORY_USERPTR == buffer->memory) {
  1619. if ((buffer->length < vout->pix.sizeimage) ||
  1620. (0 == buffer->m.userptr)) {
  1621. return -EINVAL;
  1622. }
  1623. }
  1624. if ((rotation_enabled(vout)) &&
  1625. vout->vrfb_dma_tx.req_status == DMA_CHAN_NOT_ALLOTED) {
  1626. v4l2_warn(&vout->vid_dev->v4l2_dev,
  1627. "DMA Channel not allocated for Rotation\n");
  1628. return -EINVAL;
  1629. }
  1630. return videobuf_qbuf(q, buffer);
  1631. }
  1632. static int vidioc_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
  1633. {
  1634. struct omap_vout_device *vout = fh;
  1635. struct videobuf_queue *q = &vout->vbq;
  1636. if (!vout->streaming)
  1637. return -EINVAL;
  1638. if (file->f_flags & O_NONBLOCK)
  1639. /* Call videobuf_dqbuf for non blocking mode */
  1640. return videobuf_dqbuf(q, (struct v4l2_buffer *)b, 1);
  1641. else
  1642. /* Call videobuf_dqbuf for blocking mode */
  1643. return videobuf_dqbuf(q, (struct v4l2_buffer *)b, 0);
  1644. }
  1645. static int vidioc_streamon(struct file *file, void *fh, enum v4l2_buf_type i)
  1646. {
  1647. int ret = 0, j;
  1648. u32 addr = 0, mask = 0;
  1649. struct omap_vout_device *vout = fh;
  1650. struct videobuf_queue *q = &vout->vbq;
  1651. struct omapvideo_info *ovid = &vout->vid_info;
  1652. mutex_lock(&vout->lock);
  1653. if (vout->streaming) {
  1654. ret = -EBUSY;
  1655. goto streamon_err;
  1656. }
  1657. ret = videobuf_streamon(q);
  1658. if (ret)
  1659. goto streamon_err;
  1660. if (list_empty(&vout->dma_queue)) {
  1661. ret = -EIO;
  1662. goto streamon_err1;
  1663. }
  1664. /* Get the next frame from the buffer queue */
  1665. vout->next_frm = vout->cur_frm = list_entry(vout->dma_queue.next,
  1666. struct videobuf_buffer, queue);
  1667. /* Remove buffer from the buffer queue */
  1668. list_del(&vout->cur_frm->queue);
  1669. /* Mark state of the current frame to active */
  1670. vout->cur_frm->state = VIDEOBUF_ACTIVE;
  1671. /* Initialize field_id and started member */
  1672. vout->field_id = 0;
  1673. /* set flag here. Next QBUF will start DMA */
  1674. vout->streaming = 1;
  1675. vout->first_int = 1;
  1676. if (omap_vout_calculate_offset(vout)) {
  1677. ret = -EINVAL;
  1678. goto streamon_err1;
  1679. }
  1680. addr = (unsigned long) vout->queued_buf_addr[vout->cur_frm->i]
  1681. + vout->cropped_offset;
  1682. mask = DISPC_IRQ_VSYNC | DISPC_IRQ_EVSYNC_EVEN | DISPC_IRQ_EVSYNC_ODD;
  1683. omap_dispc_register_isr(omap_vout_isr, vout, mask);
  1684. for (j = 0; j < ovid->num_overlays; j++) {
  1685. struct omap_overlay *ovl = ovid->overlays[j];
  1686. if (ovl->manager && ovl->manager->device) {
  1687. struct omap_overlay_info info;
  1688. ovl->get_overlay_info(ovl, &info);
  1689. info.enabled = 1;
  1690. info.paddr = addr;
  1691. if (ovl->set_overlay_info(ovl, &info)) {
  1692. ret = -EINVAL;
  1693. goto streamon_err1;
  1694. }
  1695. }
  1696. }
  1697. /* First save the configuration in ovelray structure */
  1698. ret = omapvid_init(vout, addr);
  1699. if (ret)
  1700. v4l2_err(&vout->vid_dev->v4l2_dev,
  1701. "failed to set overlay info\n");
  1702. /* Enable the pipeline and set the Go bit */
  1703. ret = omapvid_apply_changes(vout);
  1704. if (ret)
  1705. v4l2_err(&vout->vid_dev->v4l2_dev, "failed to change mode\n");
  1706. ret = 0;
  1707. streamon_err1:
  1708. if (ret)
  1709. ret = videobuf_streamoff(q);
  1710. streamon_err:
  1711. mutex_unlock(&vout->lock);
  1712. return ret;
  1713. }
  1714. static int vidioc_streamoff(struct file *file, void *fh, enum v4l2_buf_type i)
  1715. {
  1716. u32 mask = 0;
  1717. int ret = 0, j;
  1718. struct omap_vout_device *vout = fh;
  1719. struct omapvideo_info *ovid = &vout->vid_info;
  1720. if (!vout->streaming)
  1721. return -EINVAL;
  1722. vout->streaming = 0;
  1723. mask = DISPC_IRQ_VSYNC | DISPC_IRQ_EVSYNC_EVEN | DISPC_IRQ_EVSYNC_ODD;
  1724. omap_dispc_unregister_isr(omap_vout_isr, vout, mask);
  1725. for (j = 0; j < ovid->num_overlays; j++) {
  1726. struct omap_overlay *ovl = ovid->overlays[j];
  1727. if (ovl->manager && ovl->manager->device) {
  1728. struct omap_overlay_info info;
  1729. ovl->get_overlay_info(ovl, &info);
  1730. info.enabled = 0;
  1731. ret = ovl->set_overlay_info(ovl, &info);
  1732. if (ret)
  1733. v4l2_err(&vout->vid_dev->v4l2_dev,
  1734. "failed to update overlay info in streamoff\n");
  1735. }
  1736. }
  1737. /* Turn of the pipeline */
  1738. ret = omapvid_apply_changes(vout);
  1739. if (ret)
  1740. v4l2_err(&vout->vid_dev->v4l2_dev, "failed to change mode in"
  1741. " streamoff\n");
  1742. INIT_LIST_HEAD(&vout->dma_queue);
  1743. ret = videobuf_streamoff(&vout->vbq);
  1744. return ret;
  1745. }
  1746. static int vidioc_s_fbuf(struct file *file, void *fh,
  1747. struct v4l2_framebuffer *a)
  1748. {
  1749. int enable = 0;
  1750. struct omap_overlay *ovl;
  1751. struct omapvideo_info *ovid;
  1752. struct omap_vout_device *vout = fh;
  1753. struct omap_overlay_manager_info info;
  1754. enum omap_dss_trans_key_type key_type = OMAP_DSS_COLOR_KEY_GFX_DST;
  1755. ovid = &vout->vid_info;
  1756. ovl = ovid->overlays[0];
  1757. /* OMAP DSS doesn't support Source and Destination color
  1758. key together */
  1759. if ((a->flags & V4L2_FBUF_FLAG_SRC_CHROMAKEY) &&
  1760. (a->flags & V4L2_FBUF_FLAG_CHROMAKEY))
  1761. return -EINVAL;
  1762. /* OMAP DSS Doesn't support the Destination color key
  1763. and alpha blending together */
  1764. if ((a->flags & V4L2_FBUF_FLAG_CHROMAKEY) &&
  1765. (a->flags & V4L2_FBUF_FLAG_LOCAL_ALPHA))
  1766. return -EINVAL;
  1767. if ((a->flags & V4L2_FBUF_FLAG_SRC_CHROMAKEY)) {
  1768. vout->fbuf.flags |= V4L2_FBUF_FLAG_SRC_CHROMAKEY;
  1769. key_type = OMAP_DSS_COLOR_KEY_VID_SRC;
  1770. } else
  1771. vout->fbuf.flags &= ~V4L2_FBUF_FLAG_SRC_CHROMAKEY;
  1772. if ((a->flags & V4L2_FBUF_FLAG_CHROMAKEY)) {
  1773. vout->fbuf.flags |= V4L2_FBUF_FLAG_CHROMAKEY;
  1774. key_type = OMAP_DSS_COLOR_KEY_GFX_DST;
  1775. } else
  1776. vout->fbuf.flags &= ~V4L2_FBUF_FLAG_CHROMAKEY;
  1777. if (a->flags & (V4L2_FBUF_FLAG_CHROMAKEY |
  1778. V4L2_FBUF_FLAG_SRC_CHROMAKEY))
  1779. enable = 1;
  1780. else
  1781. enable = 0;
  1782. if (ovl->manager && ovl->manager->get_manager_info &&
  1783. ovl->manager->set_manager_info) {
  1784. ovl->manager->get_manager_info(ovl->manager, &info);
  1785. info.trans_enabled = enable;
  1786. info.trans_key_type = key_type;
  1787. info.trans_key = vout->win.chromakey;
  1788. if (ovl->manager->set_manager_info(ovl->manager, &info))
  1789. return -EINVAL;
  1790. }
  1791. if (a->flags & V4L2_FBUF_FLAG_LOCAL_ALPHA) {
  1792. vout->fbuf.flags |= V4L2_FBUF_FLAG_LOCAL_ALPHA;
  1793. enable = 1;
  1794. } else {
  1795. vout->fbuf.flags &= ~V4L2_FBUF_FLAG_LOCAL_ALPHA;
  1796. enable = 0;
  1797. }
  1798. if (ovl->manager && ovl->manager->get_manager_info &&
  1799. ovl->manager->set_manager_info) {
  1800. ovl->manager->get_manager_info(ovl->manager, &info);
  1801. info.alpha_enabled = enable;
  1802. if (ovl->manager->set_manager_info(ovl->manager, &info))
  1803. return -EINVAL;
  1804. }
  1805. return 0;
  1806. }
  1807. static int vidioc_g_fbuf(struct file *file, void *fh,
  1808. struct v4l2_framebuffer *a)
  1809. {
  1810. struct omap_overlay *ovl;
  1811. struct omapvideo_info *ovid;
  1812. struct omap_vout_device *vout = fh;
  1813. struct omap_overlay_manager_info info;
  1814. ovid = &vout->vid_info;
  1815. ovl = ovid->overlays[0];
  1816. a->flags = 0x0;
  1817. a->capability = V4L2_FBUF_CAP_LOCAL_ALPHA | V4L2_FBUF_CAP_CHROMAKEY
  1818. | V4L2_FBUF_CAP_SRC_CHROMAKEY;
  1819. if (ovl->manager && ovl->manager->get_manager_info) {
  1820. ovl->manager->get_manager_info(ovl->manager, &info);
  1821. if (info.trans_key_type == OMAP_DSS_COLOR_KEY_VID_SRC)
  1822. a->flags |= V4L2_FBUF_FLAG_SRC_CHROMAKEY;
  1823. if (info.trans_key_type == OMAP_DSS_COLOR_KEY_GFX_DST)
  1824. a->flags |= V4L2_FBUF_FLAG_CHROMAKEY;
  1825. }
  1826. if (ovl->manager && ovl->manager->get_manager_info) {
  1827. ovl->manager->get_manager_info(ovl->manager, &info);
  1828. if (info.alpha_enabled)
  1829. a->flags |= V4L2_FBUF_FLAG_LOCAL_ALPHA;
  1830. }
  1831. return 0;
  1832. }
  1833. static const struct v4l2_ioctl_ops vout_ioctl_ops = {
  1834. .vidioc_querycap = vidioc_querycap,
  1835. .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
  1836. .vidioc_g_fmt_vid_out = vidioc_g_fmt_vid_out,
  1837. .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out,
  1838. .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
  1839. .vidioc_queryctrl = vidioc_queryctrl,
  1840. .vidioc_g_ctrl = vidioc_g_ctrl,
  1841. .vidioc_s_fbuf = vidioc_s_fbuf,
  1842. .vidioc_g_fbuf = vidioc_g_fbuf,
  1843. .vidioc_s_ctrl = vidioc_s_ctrl,
  1844. .vidioc_try_fmt_vid_overlay = vidioc_try_fmt_vid_overlay,
  1845. .vidioc_s_fmt_vid_overlay = vidioc_s_fmt_vid_overlay,
  1846. .vidioc_enum_fmt_vid_overlay = vidioc_enum_fmt_vid_overlay,
  1847. .vidioc_g_fmt_vid_overlay = vidioc_g_fmt_vid_overlay,
  1848. .vidioc_cropcap = vidioc_cropcap,
  1849. .vidioc_g_crop = vidioc_g_crop,
  1850. .vidioc_s_crop = vidioc_s_crop,
  1851. .vidioc_reqbufs = vidioc_reqbufs,
  1852. .vidioc_querybuf = vidioc_querybuf,
  1853. .vidioc_qbuf = vidioc_qbuf,
  1854. .vidioc_dqbuf = vidioc_dqbuf,
  1855. .vidioc_streamon = vidioc_streamon,
  1856. .vidioc_streamoff = vidioc_streamoff,
  1857. };
  1858. static const struct v4l2_file_operations omap_vout_fops = {
  1859. .owner = THIS_MODULE,
  1860. .unlocked_ioctl = video_ioctl2,
  1861. .mmap = omap_vout_mmap,
  1862. .open = omap_vout_open,
  1863. .release = omap_vout_release,
  1864. };
  1865. /* Init functions used during driver initialization */
  1866. /* Initial setup of video_data */
  1867. static int __init omap_vout_setup_video_data(struct omap_vout_device *vout)
  1868. {
  1869. struct video_device *vfd;
  1870. struct v4l2_pix_format *pix;
  1871. struct v4l2_control *control;
  1872. struct omap_dss_device *display =
  1873. vout->vid_info.overlays[0]->manager->device;
  1874. /* set the default pix */
  1875. pix = &vout->pix;
  1876. /* Set the default picture of QVGA */
  1877. pix->width = QQVGA_WIDTH;
  1878. pix->height = QQVGA_HEIGHT;
  1879. /* Default pixel format is RGB 5-6-5 */
  1880. pix->pixelformat = V4L2_PIX_FMT_RGB565;
  1881. pix->field = V4L2_FIELD_ANY;
  1882. pix->bytesperline = pix->width * 2;
  1883. pix->sizeimage = pix->bytesperline * pix->height;
  1884. pix->priv = 0;
  1885. pix->colorspace = V4L2_COLORSPACE_JPEG;
  1886. vout->bpp = RGB565_BPP;
  1887. vout->fbuf.fmt.width = display->panel.timings.x_res;
  1888. vout->fbuf.fmt.height = display->panel.timings.y_res;
  1889. /* Set the data structures for the overlay parameters*/
  1890. vout->win.global_alpha = 255;
  1891. vout->fbuf.flags = 0;
  1892. vout->fbuf.capability = V4L2_FBUF_CAP_LOCAL_ALPHA |
  1893. V4L2_FBUF_CAP_SRC_CHROMAKEY | V4L2_FBUF_CAP_CHROMAKEY;
  1894. vout->win.chromakey = 0;
  1895. omap_vout_new_format(pix, &vout->fbuf, &vout->crop, &vout->win);
  1896. /*Initialize the control variables for
  1897. rotation, flipping and background color. */
  1898. control = vout->control;
  1899. control[0].id = V4L2_CID_ROTATE;
  1900. control[0].value = 0;
  1901. vout->rotation = 0;
  1902. vout->mirror = 0;
  1903. vout->control[2].id = V4L2_CID_HFLIP;
  1904. vout->control[2].value = 0;
  1905. vout->vrfb_bpp = 2;
  1906. control[1].id = V4L2_CID_BG_COLOR;
  1907. control[1].value = 0;
  1908. /* initialize the video_device struct */
  1909. vfd = vout->vfd = video_device_alloc();
  1910. if (!vfd) {
  1911. printk(KERN_ERR VOUT_NAME ": could not allocate"
  1912. " video device struct\n");
  1913. return -ENOMEM;
  1914. }
  1915. vfd->release = video_device_release;
  1916. vfd->ioctl_ops = &vout_ioctl_ops;
  1917. strlcpy(vfd->name, VOUT_NAME, sizeof(vfd->name));
  1918. vfd->fops = &omap_vout_fops;
  1919. vfd->v4l2_dev = &vout->vid_dev->v4l2_dev;
  1920. mutex_init(&vout->lock);
  1921. vfd->minor = -1;
  1922. return 0;
  1923. }
  1924. /* Setup video buffers */
  1925. static int __init omap_vout_setup_video_bufs(struct platform_device *pdev,
  1926. int vid_num)
  1927. {
  1928. u32 numbuffers;
  1929. int ret = 0, i, j;
  1930. int image_width, image_height;
  1931. struct video_device *vfd;
  1932. struct omap_vout_device *vout;
  1933. int static_vrfb_allocation = 0, vrfb_num_bufs = VRFB_NUM_BUFS;
  1934. struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
  1935. struct omap2video_device *vid_dev =
  1936. container_of(v4l2_dev, struct omap2video_device, v4l2_dev);
  1937. vout = vid_dev->vouts[vid_num];
  1938. vfd = vout->vfd;
  1939. numbuffers = (vid_num == 0) ? video1_numbuffers : video2_numbuffers;
  1940. vout->buffer_size = (vid_num == 0) ? video1_bufsize : video2_bufsize;
  1941. dev_info(&pdev->dev, "Buffer Size = %d\n", vout->buffer_size);
  1942. for (i = 0; i < numbuffers; i++) {
  1943. vout->buf_virt_addr[i] =
  1944. omap_vout_alloc_buffer(vout->buffer_size,
  1945. (u32 *) &vout->buf_phy_addr[i]);
  1946. if (!vout->buf_virt_addr[i]) {
  1947. numbuffers = i;
  1948. ret = -ENOMEM;
  1949. goto free_buffers;
  1950. }
  1951. }
  1952. for (i = 0; i < VRFB_NUM_BUFS; i++) {
  1953. if (omap_vrfb_request_ctx(&vout->vrfb_context[i])) {
  1954. dev_info(&pdev->dev, ": VRFB allocation failed\n");
  1955. for (j = 0; j < i; j++)
  1956. omap_vrfb_release_ctx(&vout->vrfb_context[j]);
  1957. ret = -ENOMEM;
  1958. goto free_buffers;
  1959. }
  1960. }
  1961. vout->cropped_offset = 0;
  1962. /* Calculate VRFB memory size */
  1963. /* allocate for worst case size */
  1964. image_width = VID_MAX_WIDTH / TILE_SIZE;
  1965. if (VID_MAX_WIDTH % TILE_SIZE)
  1966. image_width++;
  1967. image_width = image_width * TILE_SIZE;
  1968. image_height = VID_MAX_HEIGHT / TILE_SIZE;
  1969. if (VID_MAX_HEIGHT % TILE_SIZE)
  1970. image_height++;
  1971. image_height = image_height * TILE_SIZE;
  1972. vout->smsshado_size = PAGE_ALIGN(image_width * image_height * 2 * 2);
  1973. /*
  1974. * Request and Initialize DMA, for DMA based VRFB transfer
  1975. */
  1976. vout->vrfb_dma_tx.dev_id = OMAP_DMA_NO_DEVICE;
  1977. vout->vrfb_dma_tx.dma_ch = -1;
  1978. vout->vrfb_dma_tx.req_status = DMA_CHAN_ALLOTED;
  1979. ret = omap_request_dma(vout->vrfb_dma_tx.dev_id, "VRFB DMA TX",
  1980. omap_vout_vrfb_dma_tx_callback,
  1981. (void *) &vout->vrfb_dma_tx, &vout->vrfb_dma_tx.dma_ch);
  1982. if (ret < 0) {
  1983. vout->vrfb_dma_tx.req_status = DMA_CHAN_NOT_ALLOTED;
  1984. dev_info(&pdev->dev, ": failed to allocate DMA Channel for"
  1985. " video%d\n", vfd->minor);
  1986. }
  1987. init_waitqueue_head(&vout->vrfb_dma_tx.wait);
  1988. /* Allocate VRFB buffers if selected through bootargs */
  1989. static_vrfb_allocation = (vid_num == 0) ?
  1990. vid1_static_vrfb_alloc : vid2_static_vrfb_alloc;
  1991. /* statically allocated the VRFB buffer is done through
  1992. commands line aruments */
  1993. if (static_vrfb_allocation) {
  1994. if (omap_vout_allocate_vrfb_buffers(vout, &vrfb_num_bufs, -1)) {
  1995. ret = -ENOMEM;
  1996. goto release_vrfb_ctx;
  1997. }
  1998. vout->vrfb_static_allocation = 1;
  1999. }
  2000. return 0;
  2001. release_vrfb_ctx:
  2002. for (j = 0; j < VRFB_NUM_BUFS; j++)
  2003. omap_vrfb_release_ctx(&vout->vrfb_context[j]);
  2004. free_buffers:
  2005. for (i = 0; i < numbuffers; i++) {
  2006. omap_vout_free_buffer(vout->buf_virt_addr[i],
  2007. vout->buffer_size);
  2008. vout->buf_virt_addr[i] = 0;
  2009. vout->buf_phy_addr[i] = 0;
  2010. }
  2011. return ret;
  2012. }
  2013. /* Create video out devices */
  2014. static int __init omap_vout_create_video_devices(struct platform_device *pdev)
  2015. {
  2016. int ret = 0, k;
  2017. struct omap_vout_device *vout;
  2018. struct video_device *vfd = NULL;
  2019. struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
  2020. struct omap2video_device *vid_dev = container_of(v4l2_dev,
  2021. struct omap2video_device, v4l2_dev);
  2022. for (k = 0; k < pdev->num_resources; k++) {
  2023. vout = kzalloc(sizeof(struct omap_vout_device), GFP_KERNEL);
  2024. if (!vout) {
  2025. dev_err(&pdev->dev, ": could not allocate memory\n");
  2026. return -ENOMEM;
  2027. }
  2028. vout->vid = k;
  2029. vid_dev->vouts[k] = vout;
  2030. vout->vid_dev = vid_dev;
  2031. /* Select video2 if only 1 overlay is controlled by V4L2 */
  2032. if (pdev->num_resources == 1)
  2033. vout->vid_info.overlays[0] = vid_dev->overlays[k + 2];
  2034. else
  2035. /* Else select video1 and video2 one by one. */
  2036. vout->vid_info.overlays[0] = vid_dev->overlays[k + 1];
  2037. vout->vid_info.num_overlays = 1;
  2038. vout->vid_info.id = k + 1;
  2039. /* Setup the default configuration for the video devices
  2040. */
  2041. if (omap_vout_setup_video_data(vout) != 0) {
  2042. ret = -ENOMEM;
  2043. goto error;
  2044. }
  2045. /* Allocate default number of buffers for the video streaming
  2046. * and reserve the VRFB space for rotation
  2047. */
  2048. if (omap_vout_setup_video_bufs(pdev, k) != 0) {
  2049. ret = -ENOMEM;
  2050. goto error1;
  2051. }
  2052. /* Register the Video device with V4L2
  2053. */
  2054. vfd = vout->vfd;
  2055. if (video_register_device(vfd, VFL_TYPE_GRABBER, -1) < 0) {
  2056. dev_err(&pdev->dev, ": Could not register "
  2057. "Video for Linux device\n");
  2058. vfd->minor = -1;
  2059. ret = -ENODEV;
  2060. goto error2;
  2061. }
  2062. video_set_drvdata(vfd, vout);
  2063. /* Configure the overlay structure */
  2064. ret = omapvid_init(vid_dev->vouts[k], 0);
  2065. if (!ret)
  2066. goto success;
  2067. error2:
  2068. omap_vout_release_vrfb(vout);
  2069. omap_vout_free_buffers(vout);
  2070. error1:
  2071. video_device_release(vfd);
  2072. error:
  2073. kfree(vout);
  2074. return ret;
  2075. success:
  2076. dev_info(&pdev->dev, ": registered and initialized"
  2077. " video device %d\n", vfd->minor);
  2078. if (k == (pdev->num_resources - 1))
  2079. return 0;
  2080. }
  2081. return -ENODEV;
  2082. }
  2083. /* Driver functions */
  2084. static void omap_vout_cleanup_device(struct omap_vout_device *vout)
  2085. {
  2086. struct video_device *vfd;
  2087. if (!vout)
  2088. return;
  2089. vfd = vout->vfd;
  2090. if (vfd) {
  2091. if (!video_is_registered(vfd)) {
  2092. /*
  2093. * The device was never registered, so release the
  2094. * video_device struct directly.
  2095. */
  2096. video_device_release(vfd);
  2097. } else {
  2098. /*
  2099. * The unregister function will release the video_device
  2100. * struct as well as unregistering it.
  2101. */
  2102. video_unregister_device(vfd);
  2103. }
  2104. }
  2105. omap_vout_release_vrfb(vout);
  2106. omap_vout_free_buffers(vout);
  2107. /* Free the VRFB buffer if allocated
  2108. * init time
  2109. */
  2110. if (vout->vrfb_static_allocation)
  2111. omap_vout_free_vrfb_buffers(vout);
  2112. kfree(vout);
  2113. }
  2114. static int omap_vout_remove(struct platform_device *pdev)
  2115. {
  2116. int k;
  2117. struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
  2118. struct omap2video_device *vid_dev = container_of(v4l2_dev, struct
  2119. omap2video_device, v4l2_dev);
  2120. v4l2_device_unregister(v4l2_dev);
  2121. for (k = 0; k < pdev->num_resources; k++)
  2122. omap_vout_cleanup_device(vid_dev->vouts[k]);
  2123. for (k = 0; k < vid_dev->num_displays; k++) {
  2124. if (vid_dev->displays[k]->state != OMAP_DSS_DISPLAY_DISABLED)
  2125. vid_dev->displays[k]->driver->disable(vid_dev->displays[k]);
  2126. omap_dss_put_device(vid_dev->displays[k]);
  2127. }
  2128. kfree(vid_dev);
  2129. return 0;
  2130. }
  2131. static int __init omap_vout_probe(struct platform_device *pdev)
  2132. {
  2133. int ret = 0, i;
  2134. struct omap_overlay *ovl;
  2135. struct omap_dss_device *dssdev = NULL;
  2136. struct omap_dss_device *def_display;
  2137. struct omap2video_device *vid_dev = NULL;
  2138. if (pdev->num_resources == 0) {
  2139. dev_err(&pdev->dev, "probed for an unknown device\n");
  2140. return -ENODEV;
  2141. }
  2142. vid_dev = kzalloc(sizeof(struct omap2video_device), GFP_KERNEL);
  2143. if (vid_dev == NULL)
  2144. return -ENOMEM;
  2145. vid_dev->num_displays = 0;
  2146. for_each_dss_dev(dssdev) {
  2147. omap_dss_get_device(dssdev);
  2148. vid_dev->displays[vid_dev->num_displays++] = dssdev;
  2149. }
  2150. if (vid_dev->num_displays == 0) {
  2151. dev_err(&pdev->dev, "no displays\n");
  2152. ret = -EINVAL;
  2153. goto probe_err0;
  2154. }
  2155. vid_dev->num_overlays = omap_dss_get_num_overlays();
  2156. for (i = 0; i < vid_dev->num_overlays; i++)
  2157. vid_dev->overlays[i] = omap_dss_get_overlay(i);
  2158. vid_dev->num_managers = omap_dss_get_num_overlay_managers();
  2159. for (i = 0; i < vid_dev->num_managers; i++)
  2160. vid_dev->managers[i] = omap_dss_get_overlay_manager(i);
  2161. /* Get the Video1 overlay and video2 overlay.
  2162. * Setup the Display attached to that overlays
  2163. */
  2164. for (i = 1; i < vid_dev->num_overlays; i++) {
  2165. ovl = omap_dss_get_overlay(i);
  2166. if (ovl->manager && ovl->manager->device) {
  2167. def_display = ovl->manager->device;
  2168. } else {
  2169. dev_warn(&pdev->dev, "cannot find display\n");
  2170. def_display = NULL;
  2171. }
  2172. if (def_display) {
  2173. struct omap_dss_driver *dssdrv = def_display->driver;
  2174. ret = dssdrv->enable(def_display);
  2175. if (ret) {
  2176. /* Here we are not considering a error
  2177. * as display may be enabled by frame
  2178. * buffer driver
  2179. */
  2180. dev_warn(&pdev->dev,
  2181. "'%s' Display already enabled\n",
  2182. def_display->name);
  2183. }
  2184. /* set the update mode */
  2185. if (def_display->caps &
  2186. OMAP_DSS_DISPLAY_CAP_MANUAL_UPDATE) {
  2187. if (dssdrv->enable_te)
  2188. dssdrv->enable_te(def_display, 0);
  2189. if (dssdrv->set_update_mode)
  2190. dssdrv->set_update_mode(def_display,
  2191. OMAP_DSS_UPDATE_MANUAL);
  2192. } else {
  2193. if (dssdrv->set_update_mode)
  2194. dssdrv->set_update_mode(def_display,
  2195. OMAP_DSS_UPDATE_AUTO);
  2196. }
  2197. }
  2198. }
  2199. if (v4l2_device_register(&pdev->dev, &vid_dev->v4l2_dev) < 0) {
  2200. dev_err(&pdev->dev, "v4l2_device_register failed\n");
  2201. ret = -ENODEV;
  2202. goto probe_err1;
  2203. }
  2204. ret = omap_vout_create_video_devices(pdev);
  2205. if (ret)
  2206. goto probe_err2;
  2207. for (i = 0; i < vid_dev->num_displays; i++) {
  2208. struct omap_dss_device *display = vid_dev->displays[i];
  2209. if (display->driver->update)
  2210. display->driver->update(display, 0, 0,
  2211. display->panel.timings.x_res,
  2212. display->panel.timings.y_res);
  2213. }
  2214. return 0;
  2215. probe_err2:
  2216. v4l2_device_unregister(&vid_dev->v4l2_dev);
  2217. probe_err1:
  2218. for (i = 1; i < vid_dev->num_overlays; i++) {
  2219. def_display = NULL;
  2220. ovl = omap_dss_get_overlay(i);
  2221. if (ovl->manager && ovl->manager->device)
  2222. def_display = ovl->manager->device;
  2223. if (def_display && def_display->driver)
  2224. def_display->driver->disable(def_display);
  2225. }
  2226. probe_err0:
  2227. kfree(vid_dev);
  2228. return ret;
  2229. }
  2230. static struct platform_driver omap_vout_driver = {
  2231. .driver = {
  2232. .name = VOUT_NAME,
  2233. },
  2234. .probe = omap_vout_probe,
  2235. .remove = omap_vout_remove,
  2236. };
  2237. static int __init omap_vout_init(void)
  2238. {
  2239. if (platform_driver_register(&omap_vout_driver) != 0) {
  2240. printk(KERN_ERR VOUT_NAME ":Could not register Video driver\n");
  2241. return -EINVAL;
  2242. }
  2243. return 0;
  2244. }
  2245. static void omap_vout_cleanup(void)
  2246. {
  2247. platform_driver_unregister(&omap_vout_driver);
  2248. }
  2249. late_initcall(omap_vout_init);
  2250. module_exit(omap_vout_cleanup);