omap_vout.c 56 KB

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