omap_vout.c 67 KB

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