omap_vout.c 57 KB

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