omap_vout.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285
  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. /* Enable the pipeline and set the Go bit */
  564. ret = omapvid_apply_changes(vout);
  565. if (ret)
  566. printk(KERN_ERR VOUT_NAME "failed to change mode\n");
  567. vout_isr_err:
  568. spin_unlock(&vout->vbq_lock);
  569. }
  570. /* Video buffer call backs */
  571. /*
  572. * Buffer setup function is called by videobuf layer when REQBUF ioctl is
  573. * called. This is used to setup buffers and return size and count of
  574. * buffers allocated. After the call to this buffer, videobuf layer will
  575. * setup buffer queue depending on the size and count of buffers
  576. */
  577. static int omap_vout_buffer_setup(struct videobuf_queue *q, unsigned int *count,
  578. unsigned int *size)
  579. {
  580. int startindex = 0, i, j;
  581. u32 phy_addr = 0, virt_addr = 0;
  582. struct omap_vout_device *vout = q->priv_data;
  583. struct omapvideo_info *ovid = &vout->vid_info;
  584. int vid_max_buf_size;
  585. if (!vout)
  586. return -EINVAL;
  587. vid_max_buf_size = vout->vid == OMAP_VIDEO1 ? video1_bufsize :
  588. video2_bufsize;
  589. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != q->type)
  590. return -EINVAL;
  591. startindex = (vout->vid == OMAP_VIDEO1) ?
  592. video1_numbuffers : video2_numbuffers;
  593. if (V4L2_MEMORY_MMAP == vout->memory && *count < startindex)
  594. *count = startindex;
  595. if (ovid->rotation_type == VOUT_ROT_VRFB) {
  596. if (omap_vout_vrfb_buffer_setup(vout, count, startindex))
  597. return -ENOMEM;
  598. }
  599. if (V4L2_MEMORY_MMAP != vout->memory)
  600. return 0;
  601. /* Now allocated the V4L2 buffers */
  602. *size = PAGE_ALIGN(vout->pix.width * vout->pix.height * vout->bpp);
  603. startindex = (vout->vid == OMAP_VIDEO1) ?
  604. video1_numbuffers : video2_numbuffers;
  605. /* Check the size of the buffer */
  606. if (*size > vid_max_buf_size) {
  607. v4l2_err(&vout->vid_dev->v4l2_dev,
  608. "buffer allocation mismatch [%u] [%u]\n",
  609. *size, vout->buffer_size);
  610. return -ENOMEM;
  611. }
  612. for (i = startindex; i < *count; i++) {
  613. vout->buffer_size = *size;
  614. virt_addr = omap_vout_alloc_buffer(vout->buffer_size,
  615. &phy_addr);
  616. if (!virt_addr) {
  617. if (ovid->rotation_type == VOUT_ROT_NONE) {
  618. break;
  619. } else {
  620. if (!is_rotation_enabled(vout))
  621. break;
  622. /* Free the VRFB buffers if no space for V4L2 buffers */
  623. for (j = i; j < *count; j++) {
  624. omap_vout_free_buffer(
  625. vout->smsshado_virt_addr[j],
  626. vout->smsshado_size);
  627. vout->smsshado_virt_addr[j] = 0;
  628. vout->smsshado_phy_addr[j] = 0;
  629. }
  630. }
  631. }
  632. vout->buf_virt_addr[i] = virt_addr;
  633. vout->buf_phy_addr[i] = phy_addr;
  634. }
  635. *count = vout->buffer_allocated = i;
  636. return 0;
  637. }
  638. /*
  639. * Free the V4L2 buffers additionally allocated than default
  640. * number of buffers
  641. */
  642. static void omap_vout_free_extra_buffers(struct omap_vout_device *vout)
  643. {
  644. int num_buffers = 0, i;
  645. num_buffers = (vout->vid == OMAP_VIDEO1) ?
  646. video1_numbuffers : video2_numbuffers;
  647. for (i = num_buffers; i < vout->buffer_allocated; i++) {
  648. if (vout->buf_virt_addr[i])
  649. omap_vout_free_buffer(vout->buf_virt_addr[i],
  650. vout->buffer_size);
  651. vout->buf_virt_addr[i] = 0;
  652. vout->buf_phy_addr[i] = 0;
  653. }
  654. vout->buffer_allocated = num_buffers;
  655. }
  656. /*
  657. * This function will be called when VIDIOC_QBUF ioctl is called.
  658. * It prepare buffers before give out for the display. This function
  659. * converts user space virtual address into physical address if userptr memory
  660. * exchange mechanism is used. If rotation is enabled, it copies entire
  661. * buffer into VRFB memory space before giving it to the DSS.
  662. */
  663. static int omap_vout_buffer_prepare(struct videobuf_queue *q,
  664. struct videobuf_buffer *vb,
  665. enum v4l2_field field)
  666. {
  667. struct omap_vout_device *vout = q->priv_data;
  668. struct omapvideo_info *ovid = &vout->vid_info;
  669. if (VIDEOBUF_NEEDS_INIT == vb->state) {
  670. vb->width = vout->pix.width;
  671. vb->height = vout->pix.height;
  672. vb->size = vb->width * vb->height * vout->bpp;
  673. vb->field = field;
  674. }
  675. vb->state = VIDEOBUF_PREPARED;
  676. /* if user pointer memory mechanism is used, get the physical
  677. * address of the buffer
  678. */
  679. if (V4L2_MEMORY_USERPTR == vb->memory) {
  680. if (0 == vb->baddr)
  681. return -EINVAL;
  682. /* Physical address */
  683. vout->queued_buf_addr[vb->i] = (u8 *)
  684. omap_vout_uservirt_to_phys(vb->baddr);
  685. } else {
  686. u32 addr, dma_addr;
  687. unsigned long size;
  688. addr = (unsigned long) vout->buf_virt_addr[vb->i];
  689. size = (unsigned long) vb->size;
  690. dma_addr = dma_map_single(vout->vid_dev->v4l2_dev.dev, (void *) addr,
  691. size, DMA_TO_DEVICE);
  692. if (dma_mapping_error(vout->vid_dev->v4l2_dev.dev, dma_addr))
  693. v4l2_err(&vout->vid_dev->v4l2_dev, "dma_map_single failed\n");
  694. vout->queued_buf_addr[vb->i] = (u8 *)vout->buf_phy_addr[vb->i];
  695. }
  696. if (ovid->rotation_type == VOUT_ROT_VRFB)
  697. return omap_vout_prepare_vrfb(vout, vb);
  698. else
  699. return 0;
  700. }
  701. /*
  702. * Buffer queue function will be called from the videobuf layer when _QBUF
  703. * ioctl is called. It is used to enqueue buffer, which is ready to be
  704. * displayed.
  705. */
  706. static void omap_vout_buffer_queue(struct videobuf_queue *q,
  707. struct videobuf_buffer *vb)
  708. {
  709. struct omap_vout_device *vout = q->priv_data;
  710. /* Driver is also maintainig a queue. So enqueue buffer in the driver
  711. * queue */
  712. list_add_tail(&vb->queue, &vout->dma_queue);
  713. vb->state = VIDEOBUF_QUEUED;
  714. }
  715. /*
  716. * Buffer release function is called from videobuf layer to release buffer
  717. * which are already allocated
  718. */
  719. static void omap_vout_buffer_release(struct videobuf_queue *q,
  720. struct videobuf_buffer *vb)
  721. {
  722. struct omap_vout_device *vout = q->priv_data;
  723. vb->state = VIDEOBUF_NEEDS_INIT;
  724. if (V4L2_MEMORY_MMAP != vout->memory)
  725. return;
  726. }
  727. /*
  728. * File operations
  729. */
  730. static unsigned int omap_vout_poll(struct file *file,
  731. struct poll_table_struct *wait)
  732. {
  733. struct omap_vout_device *vout = file->private_data;
  734. struct videobuf_queue *q = &vout->vbq;
  735. return videobuf_poll_stream(file, q, wait);
  736. }
  737. static void omap_vout_vm_open(struct vm_area_struct *vma)
  738. {
  739. struct omap_vout_device *vout = vma->vm_private_data;
  740. v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
  741. "vm_open [vma=%08lx-%08lx]\n", vma->vm_start, vma->vm_end);
  742. vout->mmap_count++;
  743. }
  744. static void omap_vout_vm_close(struct vm_area_struct *vma)
  745. {
  746. struct omap_vout_device *vout = vma->vm_private_data;
  747. v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
  748. "vm_close [vma=%08lx-%08lx]\n", vma->vm_start, vma->vm_end);
  749. vout->mmap_count--;
  750. }
  751. static struct vm_operations_struct omap_vout_vm_ops = {
  752. .open = omap_vout_vm_open,
  753. .close = omap_vout_vm_close,
  754. };
  755. static int omap_vout_mmap(struct file *file, struct vm_area_struct *vma)
  756. {
  757. int i;
  758. void *pos;
  759. unsigned long start = vma->vm_start;
  760. unsigned long size = (vma->vm_end - vma->vm_start);
  761. struct omap_vout_device *vout = file->private_data;
  762. struct videobuf_queue *q = &vout->vbq;
  763. v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
  764. " %s pgoff=0x%lx, start=0x%lx, end=0x%lx\n", __func__,
  765. vma->vm_pgoff, vma->vm_start, vma->vm_end);
  766. /* look for the buffer to map */
  767. for (i = 0; i < VIDEO_MAX_FRAME; i++) {
  768. if (NULL == q->bufs[i])
  769. continue;
  770. if (V4L2_MEMORY_MMAP != q->bufs[i]->memory)
  771. continue;
  772. if (q->bufs[i]->boff == (vma->vm_pgoff << PAGE_SHIFT))
  773. break;
  774. }
  775. if (VIDEO_MAX_FRAME == i) {
  776. v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
  777. "offset invalid [offset=0x%lx]\n",
  778. (vma->vm_pgoff << PAGE_SHIFT));
  779. return -EINVAL;
  780. }
  781. /* Check the size of the buffer */
  782. if (size > vout->buffer_size) {
  783. v4l2_err(&vout->vid_dev->v4l2_dev,
  784. "insufficient memory [%lu] [%u]\n",
  785. size, vout->buffer_size);
  786. return -ENOMEM;
  787. }
  788. q->bufs[i]->baddr = vma->vm_start;
  789. vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
  790. vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
  791. vma->vm_ops = &omap_vout_vm_ops;
  792. vma->vm_private_data = (void *) vout;
  793. pos = (void *)vout->buf_virt_addr[i];
  794. vma->vm_pgoff = virt_to_phys((void *)pos) >> PAGE_SHIFT;
  795. while (size > 0) {
  796. unsigned long pfn;
  797. pfn = virt_to_phys((void *) pos) >> PAGE_SHIFT;
  798. if (remap_pfn_range(vma, start, pfn, PAGE_SIZE, PAGE_SHARED))
  799. return -EAGAIN;
  800. start += PAGE_SIZE;
  801. pos += PAGE_SIZE;
  802. size -= PAGE_SIZE;
  803. }
  804. vout->mmap_count++;
  805. v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Exiting %s\n", __func__);
  806. return 0;
  807. }
  808. static int omap_vout_release(struct file *file)
  809. {
  810. unsigned int ret, i;
  811. struct videobuf_queue *q;
  812. struct omapvideo_info *ovid;
  813. struct omap_vout_device *vout = file->private_data;
  814. v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Entering %s\n", __func__);
  815. ovid = &vout->vid_info;
  816. if (!vout)
  817. return 0;
  818. q = &vout->vbq;
  819. /* Disable all the overlay managers connected with this interface */
  820. for (i = 0; i < ovid->num_overlays; i++) {
  821. struct omap_overlay *ovl = ovid->overlays[i];
  822. struct omap_dss_device *dssdev = ovl->get_device(ovl);
  823. if (dssdev)
  824. ovl->disable(ovl);
  825. }
  826. /* Turn off the pipeline */
  827. ret = omapvid_apply_changes(vout);
  828. if (ret)
  829. v4l2_warn(&vout->vid_dev->v4l2_dev,
  830. "Unable to apply changes\n");
  831. /* Free all buffers */
  832. omap_vout_free_extra_buffers(vout);
  833. /* Free the VRFB buffers only if they are allocated
  834. * during reqbufs. Don't free if init time allocated
  835. */
  836. if (ovid->rotation_type == VOUT_ROT_VRFB) {
  837. if (!vout->vrfb_static_allocation)
  838. omap_vout_free_vrfb_buffers(vout);
  839. }
  840. videobuf_mmap_free(q);
  841. /* Even if apply changes fails we should continue
  842. freeing allocated memory */
  843. if (vout->streaming) {
  844. u32 mask = 0;
  845. mask = DISPC_IRQ_VSYNC | DISPC_IRQ_EVSYNC_EVEN |
  846. DISPC_IRQ_EVSYNC_ODD | DISPC_IRQ_VSYNC2;
  847. omap_dispc_unregister_isr(omap_vout_isr, vout, mask);
  848. vout->streaming = 0;
  849. videobuf_streamoff(q);
  850. videobuf_queue_cancel(q);
  851. }
  852. if (vout->mmap_count != 0)
  853. vout->mmap_count = 0;
  854. vout->opened -= 1;
  855. file->private_data = NULL;
  856. if (vout->buffer_allocated)
  857. videobuf_mmap_free(q);
  858. v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Exiting %s\n", __func__);
  859. return ret;
  860. }
  861. static int omap_vout_open(struct file *file)
  862. {
  863. struct videobuf_queue *q;
  864. struct omap_vout_device *vout = NULL;
  865. vout = video_drvdata(file);
  866. v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Entering %s\n", __func__);
  867. if (vout == NULL)
  868. return -ENODEV;
  869. /* for now, we only support single open */
  870. if (vout->opened)
  871. return -EBUSY;
  872. vout->opened += 1;
  873. file->private_data = vout;
  874. vout->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  875. q = &vout->vbq;
  876. video_vbq_ops.buf_setup = omap_vout_buffer_setup;
  877. video_vbq_ops.buf_prepare = omap_vout_buffer_prepare;
  878. video_vbq_ops.buf_release = omap_vout_buffer_release;
  879. video_vbq_ops.buf_queue = omap_vout_buffer_queue;
  880. spin_lock_init(&vout->vbq_lock);
  881. videobuf_queue_dma_contig_init(q, &video_vbq_ops, q->dev,
  882. &vout->vbq_lock, vout->type, V4L2_FIELD_NONE,
  883. sizeof(struct videobuf_buffer), vout, NULL);
  884. v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Exiting %s\n", __func__);
  885. return 0;
  886. }
  887. /*
  888. * V4L2 ioctls
  889. */
  890. static int vidioc_querycap(struct file *file, void *fh,
  891. struct v4l2_capability *cap)
  892. {
  893. struct omap_vout_device *vout = fh;
  894. strlcpy(cap->driver, VOUT_NAME, sizeof(cap->driver));
  895. strlcpy(cap->card, vout->vfd->name, sizeof(cap->card));
  896. cap->bus_info[0] = '\0';
  897. cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_OUTPUT |
  898. V4L2_CAP_VIDEO_OUTPUT_OVERLAY;
  899. return 0;
  900. }
  901. static int vidioc_enum_fmt_vid_out(struct file *file, void *fh,
  902. struct v4l2_fmtdesc *fmt)
  903. {
  904. int index = fmt->index;
  905. if (index >= NUM_OUTPUT_FORMATS)
  906. return -EINVAL;
  907. fmt->flags = omap_formats[index].flags;
  908. strlcpy(fmt->description, omap_formats[index].description,
  909. sizeof(fmt->description));
  910. fmt->pixelformat = omap_formats[index].pixelformat;
  911. return 0;
  912. }
  913. static int vidioc_g_fmt_vid_out(struct file *file, void *fh,
  914. struct v4l2_format *f)
  915. {
  916. struct omap_vout_device *vout = fh;
  917. f->fmt.pix = vout->pix;
  918. return 0;
  919. }
  920. static int vidioc_try_fmt_vid_out(struct file *file, void *fh,
  921. struct v4l2_format *f)
  922. {
  923. struct omap_overlay *ovl;
  924. struct omapvideo_info *ovid;
  925. struct omap_video_timings *timing;
  926. struct omap_vout_device *vout = fh;
  927. struct omap_dss_device *dssdev;
  928. ovid = &vout->vid_info;
  929. ovl = ovid->overlays[0];
  930. /* get the display device attached to the overlay */
  931. dssdev = ovl->get_device(ovl);
  932. if (!dssdev)
  933. return -EINVAL;
  934. timing = &dssdev->panel.timings;
  935. vout->fbuf.fmt.height = timing->y_res;
  936. vout->fbuf.fmt.width = timing->x_res;
  937. omap_vout_try_format(&f->fmt.pix);
  938. return 0;
  939. }
  940. static int vidioc_s_fmt_vid_out(struct file *file, void *fh,
  941. struct v4l2_format *f)
  942. {
  943. int ret, bpp;
  944. struct omap_overlay *ovl;
  945. struct omapvideo_info *ovid;
  946. struct omap_video_timings *timing;
  947. struct omap_vout_device *vout = fh;
  948. struct omap_dss_device *dssdev;
  949. if (vout->streaming)
  950. return -EBUSY;
  951. mutex_lock(&vout->lock);
  952. ovid = &vout->vid_info;
  953. ovl = ovid->overlays[0];
  954. dssdev = ovl->get_device(ovl);
  955. /* get the display device attached to the overlay */
  956. if (!dssdev) {
  957. ret = -EINVAL;
  958. goto s_fmt_vid_out_exit;
  959. }
  960. timing = &dssdev->panel.timings;
  961. /* We dont support RGB24-packed mode if vrfb rotation
  962. * is enabled*/
  963. if ((is_rotation_enabled(vout)) &&
  964. f->fmt.pix.pixelformat == V4L2_PIX_FMT_RGB24) {
  965. ret = -EINVAL;
  966. goto s_fmt_vid_out_exit;
  967. }
  968. /* get the framebuffer parameters */
  969. if (is_rotation_90_or_270(vout)) {
  970. vout->fbuf.fmt.height = timing->x_res;
  971. vout->fbuf.fmt.width = timing->y_res;
  972. } else {
  973. vout->fbuf.fmt.height = timing->y_res;
  974. vout->fbuf.fmt.width = timing->x_res;
  975. }
  976. /* change to samller size is OK */
  977. bpp = omap_vout_try_format(&f->fmt.pix);
  978. f->fmt.pix.sizeimage = f->fmt.pix.width * f->fmt.pix.height * bpp;
  979. /* try & set the new output format */
  980. vout->bpp = bpp;
  981. vout->pix = f->fmt.pix;
  982. vout->vrfb_bpp = 1;
  983. /* If YUYV then vrfb bpp is 2, for others its 1 */
  984. if (V4L2_PIX_FMT_YUYV == vout->pix.pixelformat ||
  985. V4L2_PIX_FMT_UYVY == vout->pix.pixelformat)
  986. vout->vrfb_bpp = 2;
  987. /* set default crop and win */
  988. omap_vout_new_format(&vout->pix, &vout->fbuf, &vout->crop, &vout->win);
  989. ret = 0;
  990. s_fmt_vid_out_exit:
  991. mutex_unlock(&vout->lock);
  992. return ret;
  993. }
  994. static int vidioc_try_fmt_vid_overlay(struct file *file, void *fh,
  995. struct v4l2_format *f)
  996. {
  997. int ret = 0;
  998. struct omap_vout_device *vout = fh;
  999. struct omap_overlay *ovl;
  1000. struct omapvideo_info *ovid;
  1001. struct v4l2_window *win = &f->fmt.win;
  1002. ovid = &vout->vid_info;
  1003. ovl = ovid->overlays[0];
  1004. ret = omap_vout_try_window(&vout->fbuf, win);
  1005. if (!ret) {
  1006. if ((ovl->caps & OMAP_DSS_OVL_CAP_GLOBAL_ALPHA) == 0)
  1007. win->global_alpha = 255;
  1008. else
  1009. win->global_alpha = f->fmt.win.global_alpha;
  1010. }
  1011. return ret;
  1012. }
  1013. static int vidioc_s_fmt_vid_overlay(struct file *file, void *fh,
  1014. struct v4l2_format *f)
  1015. {
  1016. int ret = 0;
  1017. struct omap_overlay *ovl;
  1018. struct omapvideo_info *ovid;
  1019. struct omap_vout_device *vout = fh;
  1020. struct v4l2_window *win = &f->fmt.win;
  1021. mutex_lock(&vout->lock);
  1022. ovid = &vout->vid_info;
  1023. ovl = ovid->overlays[0];
  1024. ret = omap_vout_new_window(&vout->crop, &vout->win, &vout->fbuf, win);
  1025. if (!ret) {
  1026. /* Video1 plane does not support global alpha on OMAP3 */
  1027. if ((ovl->caps & OMAP_DSS_OVL_CAP_GLOBAL_ALPHA) == 0)
  1028. vout->win.global_alpha = 255;
  1029. else
  1030. vout->win.global_alpha = f->fmt.win.global_alpha;
  1031. vout->win.chromakey = f->fmt.win.chromakey;
  1032. }
  1033. mutex_unlock(&vout->lock);
  1034. return ret;
  1035. }
  1036. static int vidioc_g_fmt_vid_overlay(struct file *file, void *fh,
  1037. struct v4l2_format *f)
  1038. {
  1039. u32 key_value = 0;
  1040. struct omap_overlay *ovl;
  1041. struct omapvideo_info *ovid;
  1042. struct omap_vout_device *vout = fh;
  1043. struct omap_overlay_manager_info info;
  1044. struct v4l2_window *win = &f->fmt.win;
  1045. ovid = &vout->vid_info;
  1046. ovl = ovid->overlays[0];
  1047. win->w = vout->win.w;
  1048. win->field = vout->win.field;
  1049. win->global_alpha = vout->win.global_alpha;
  1050. if (ovl->manager && ovl->manager->get_manager_info) {
  1051. ovl->manager->get_manager_info(ovl->manager, &info);
  1052. key_value = info.trans_key;
  1053. }
  1054. win->chromakey = key_value;
  1055. return 0;
  1056. }
  1057. static int vidioc_cropcap(struct file *file, void *fh,
  1058. struct v4l2_cropcap *cropcap)
  1059. {
  1060. struct omap_vout_device *vout = fh;
  1061. struct v4l2_pix_format *pix = &vout->pix;
  1062. if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  1063. return -EINVAL;
  1064. /* Width and height are always even */
  1065. cropcap->bounds.width = pix->width & ~1;
  1066. cropcap->bounds.height = pix->height & ~1;
  1067. omap_vout_default_crop(&vout->pix, &vout->fbuf, &cropcap->defrect);
  1068. cropcap->pixelaspect.numerator = 1;
  1069. cropcap->pixelaspect.denominator = 1;
  1070. return 0;
  1071. }
  1072. static int vidioc_g_crop(struct file *file, void *fh, struct v4l2_crop *crop)
  1073. {
  1074. struct omap_vout_device *vout = fh;
  1075. if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  1076. return -EINVAL;
  1077. crop->c = vout->crop;
  1078. return 0;
  1079. }
  1080. static int vidioc_s_crop(struct file *file, void *fh, const struct v4l2_crop *crop)
  1081. {
  1082. int ret = -EINVAL;
  1083. struct omap_vout_device *vout = fh;
  1084. struct omapvideo_info *ovid;
  1085. struct omap_overlay *ovl;
  1086. struct omap_video_timings *timing;
  1087. struct omap_dss_device *dssdev;
  1088. if (vout->streaming)
  1089. return -EBUSY;
  1090. mutex_lock(&vout->lock);
  1091. ovid = &vout->vid_info;
  1092. ovl = ovid->overlays[0];
  1093. /* get the display device attached to the overlay */
  1094. dssdev = ovl->get_device(ovl);
  1095. if (!dssdev) {
  1096. ret = -EINVAL;
  1097. goto s_crop_err;
  1098. }
  1099. timing = &dssdev->panel.timings;
  1100. if (is_rotation_90_or_270(vout)) {
  1101. vout->fbuf.fmt.height = timing->x_res;
  1102. vout->fbuf.fmt.width = timing->y_res;
  1103. } else {
  1104. vout->fbuf.fmt.height = timing->y_res;
  1105. vout->fbuf.fmt.width = timing->x_res;
  1106. }
  1107. if (crop->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
  1108. ret = omap_vout_new_crop(&vout->pix, &vout->crop, &vout->win,
  1109. &vout->fbuf, &crop->c);
  1110. s_crop_err:
  1111. mutex_unlock(&vout->lock);
  1112. return ret;
  1113. }
  1114. static int vidioc_queryctrl(struct file *file, void *fh,
  1115. struct v4l2_queryctrl *ctrl)
  1116. {
  1117. int ret = 0;
  1118. switch (ctrl->id) {
  1119. case V4L2_CID_ROTATE:
  1120. ret = v4l2_ctrl_query_fill(ctrl, 0, 270, 90, 0);
  1121. break;
  1122. case V4L2_CID_BG_COLOR:
  1123. ret = v4l2_ctrl_query_fill(ctrl, 0, 0xFFFFFF, 1, 0);
  1124. break;
  1125. case V4L2_CID_VFLIP:
  1126. ret = v4l2_ctrl_query_fill(ctrl, 0, 1, 1, 0);
  1127. break;
  1128. default:
  1129. ctrl->name[0] = '\0';
  1130. ret = -EINVAL;
  1131. }
  1132. return ret;
  1133. }
  1134. static int vidioc_g_ctrl(struct file *file, void *fh, struct v4l2_control *ctrl)
  1135. {
  1136. int ret = 0;
  1137. struct omap_vout_device *vout = fh;
  1138. switch (ctrl->id) {
  1139. case V4L2_CID_ROTATE:
  1140. ctrl->value = vout->control[0].value;
  1141. break;
  1142. case V4L2_CID_BG_COLOR:
  1143. {
  1144. struct omap_overlay_manager_info info;
  1145. struct omap_overlay *ovl;
  1146. ovl = vout->vid_info.overlays[0];
  1147. if (!ovl->manager || !ovl->manager->get_manager_info) {
  1148. ret = -EINVAL;
  1149. break;
  1150. }
  1151. ovl->manager->get_manager_info(ovl->manager, &info);
  1152. ctrl->value = info.default_color;
  1153. break;
  1154. }
  1155. case V4L2_CID_VFLIP:
  1156. ctrl->value = vout->control[2].value;
  1157. break;
  1158. default:
  1159. ret = -EINVAL;
  1160. }
  1161. return ret;
  1162. }
  1163. static int vidioc_s_ctrl(struct file *file, void *fh, struct v4l2_control *a)
  1164. {
  1165. int ret = 0;
  1166. struct omap_vout_device *vout = fh;
  1167. switch (a->id) {
  1168. case V4L2_CID_ROTATE:
  1169. {
  1170. struct omapvideo_info *ovid;
  1171. int rotation = a->value;
  1172. ovid = &vout->vid_info;
  1173. mutex_lock(&vout->lock);
  1174. if (rotation && ovid->rotation_type == VOUT_ROT_NONE) {
  1175. mutex_unlock(&vout->lock);
  1176. ret = -ERANGE;
  1177. break;
  1178. }
  1179. if (rotation && vout->pix.pixelformat == V4L2_PIX_FMT_RGB24) {
  1180. mutex_unlock(&vout->lock);
  1181. ret = -EINVAL;
  1182. break;
  1183. }
  1184. if (v4l2_rot_to_dss_rot(rotation, &vout->rotation,
  1185. vout->mirror)) {
  1186. mutex_unlock(&vout->lock);
  1187. ret = -EINVAL;
  1188. break;
  1189. }
  1190. vout->control[0].value = rotation;
  1191. mutex_unlock(&vout->lock);
  1192. break;
  1193. }
  1194. case V4L2_CID_BG_COLOR:
  1195. {
  1196. struct omap_overlay *ovl;
  1197. unsigned int color = a->value;
  1198. struct omap_overlay_manager_info info;
  1199. ovl = vout->vid_info.overlays[0];
  1200. mutex_lock(&vout->lock);
  1201. if (!ovl->manager || !ovl->manager->get_manager_info) {
  1202. mutex_unlock(&vout->lock);
  1203. ret = -EINVAL;
  1204. break;
  1205. }
  1206. ovl->manager->get_manager_info(ovl->manager, &info);
  1207. info.default_color = color;
  1208. if (ovl->manager->set_manager_info(ovl->manager, &info)) {
  1209. mutex_unlock(&vout->lock);
  1210. ret = -EINVAL;
  1211. break;
  1212. }
  1213. vout->control[1].value = color;
  1214. mutex_unlock(&vout->lock);
  1215. break;
  1216. }
  1217. case V4L2_CID_VFLIP:
  1218. {
  1219. struct omap_overlay *ovl;
  1220. struct omapvideo_info *ovid;
  1221. unsigned int mirror = a->value;
  1222. ovid = &vout->vid_info;
  1223. ovl = ovid->overlays[0];
  1224. mutex_lock(&vout->lock);
  1225. if (mirror && ovid->rotation_type == VOUT_ROT_NONE) {
  1226. mutex_unlock(&vout->lock);
  1227. ret = -ERANGE;
  1228. break;
  1229. }
  1230. if (mirror && vout->pix.pixelformat == V4L2_PIX_FMT_RGB24) {
  1231. mutex_unlock(&vout->lock);
  1232. ret = -EINVAL;
  1233. break;
  1234. }
  1235. vout->mirror = mirror;
  1236. vout->control[2].value = mirror;
  1237. mutex_unlock(&vout->lock);
  1238. break;
  1239. }
  1240. default:
  1241. ret = -EINVAL;
  1242. }
  1243. return ret;
  1244. }
  1245. static int vidioc_reqbufs(struct file *file, void *fh,
  1246. struct v4l2_requestbuffers *req)
  1247. {
  1248. int ret = 0;
  1249. unsigned int i, num_buffers = 0;
  1250. struct omap_vout_device *vout = fh;
  1251. struct videobuf_queue *q = &vout->vbq;
  1252. if ((req->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) || (req->count < 0))
  1253. return -EINVAL;
  1254. /* if memory is not mmp or userptr
  1255. return error */
  1256. if ((V4L2_MEMORY_MMAP != req->memory) &&
  1257. (V4L2_MEMORY_USERPTR != req->memory))
  1258. return -EINVAL;
  1259. mutex_lock(&vout->lock);
  1260. /* Cannot be requested when streaming is on */
  1261. if (vout->streaming) {
  1262. ret = -EBUSY;
  1263. goto reqbuf_err;
  1264. }
  1265. /* If buffers are already allocated free them */
  1266. if (q->bufs[0] && (V4L2_MEMORY_MMAP == q->bufs[0]->memory)) {
  1267. if (vout->mmap_count) {
  1268. ret = -EBUSY;
  1269. goto reqbuf_err;
  1270. }
  1271. num_buffers = (vout->vid == OMAP_VIDEO1) ?
  1272. video1_numbuffers : video2_numbuffers;
  1273. for (i = num_buffers; i < vout->buffer_allocated; i++) {
  1274. omap_vout_free_buffer(vout->buf_virt_addr[i],
  1275. vout->buffer_size);
  1276. vout->buf_virt_addr[i] = 0;
  1277. vout->buf_phy_addr[i] = 0;
  1278. }
  1279. vout->buffer_allocated = num_buffers;
  1280. videobuf_mmap_free(q);
  1281. } else if (q->bufs[0] && (V4L2_MEMORY_USERPTR == q->bufs[0]->memory)) {
  1282. if (vout->buffer_allocated) {
  1283. videobuf_mmap_free(q);
  1284. for (i = 0; i < vout->buffer_allocated; i++) {
  1285. kfree(q->bufs[i]);
  1286. q->bufs[i] = NULL;
  1287. }
  1288. vout->buffer_allocated = 0;
  1289. }
  1290. }
  1291. /*store the memory type in data structure */
  1292. vout->memory = req->memory;
  1293. INIT_LIST_HEAD(&vout->dma_queue);
  1294. /* call videobuf_reqbufs api */
  1295. ret = videobuf_reqbufs(q, req);
  1296. if (ret < 0)
  1297. goto reqbuf_err;
  1298. vout->buffer_allocated = req->count;
  1299. reqbuf_err:
  1300. mutex_unlock(&vout->lock);
  1301. return ret;
  1302. }
  1303. static int vidioc_querybuf(struct file *file, void *fh,
  1304. struct v4l2_buffer *b)
  1305. {
  1306. struct omap_vout_device *vout = fh;
  1307. return videobuf_querybuf(&vout->vbq, b);
  1308. }
  1309. static int vidioc_qbuf(struct file *file, void *fh,
  1310. struct v4l2_buffer *buffer)
  1311. {
  1312. struct omap_vout_device *vout = fh;
  1313. struct videobuf_queue *q = &vout->vbq;
  1314. if ((V4L2_BUF_TYPE_VIDEO_OUTPUT != buffer->type) ||
  1315. (buffer->index >= vout->buffer_allocated) ||
  1316. (q->bufs[buffer->index]->memory != buffer->memory)) {
  1317. return -EINVAL;
  1318. }
  1319. if (V4L2_MEMORY_USERPTR == buffer->memory) {
  1320. if ((buffer->length < vout->pix.sizeimage) ||
  1321. (0 == buffer->m.userptr)) {
  1322. return -EINVAL;
  1323. }
  1324. }
  1325. if ((is_rotation_enabled(vout)) &&
  1326. vout->vrfb_dma_tx.req_status == DMA_CHAN_NOT_ALLOTED) {
  1327. v4l2_warn(&vout->vid_dev->v4l2_dev,
  1328. "DMA Channel not allocated for Rotation\n");
  1329. return -EINVAL;
  1330. }
  1331. return videobuf_qbuf(q, buffer);
  1332. }
  1333. static int vidioc_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
  1334. {
  1335. struct omap_vout_device *vout = fh;
  1336. struct videobuf_queue *q = &vout->vbq;
  1337. int ret;
  1338. u32 addr;
  1339. unsigned long size;
  1340. struct videobuf_buffer *vb;
  1341. vb = q->bufs[b->index];
  1342. if (!vout->streaming)
  1343. return -EINVAL;
  1344. if (file->f_flags & O_NONBLOCK)
  1345. /* Call videobuf_dqbuf for non blocking mode */
  1346. ret = videobuf_dqbuf(q, (struct v4l2_buffer *)b, 1);
  1347. else
  1348. /* Call videobuf_dqbuf for blocking mode */
  1349. ret = videobuf_dqbuf(q, (struct v4l2_buffer *)b, 0);
  1350. addr = (unsigned long) vout->buf_phy_addr[vb->i];
  1351. size = (unsigned long) vb->size;
  1352. dma_unmap_single(vout->vid_dev->v4l2_dev.dev, addr,
  1353. size, DMA_TO_DEVICE);
  1354. return ret;
  1355. }
  1356. static int vidioc_streamon(struct file *file, void *fh, enum v4l2_buf_type i)
  1357. {
  1358. int ret = 0, j;
  1359. u32 addr = 0, mask = 0;
  1360. struct omap_vout_device *vout = fh;
  1361. struct videobuf_queue *q = &vout->vbq;
  1362. struct omapvideo_info *ovid = &vout->vid_info;
  1363. mutex_lock(&vout->lock);
  1364. if (vout->streaming) {
  1365. ret = -EBUSY;
  1366. goto streamon_err;
  1367. }
  1368. ret = videobuf_streamon(q);
  1369. if (ret)
  1370. goto streamon_err;
  1371. if (list_empty(&vout->dma_queue)) {
  1372. ret = -EIO;
  1373. goto streamon_err1;
  1374. }
  1375. /* Get the next frame from the buffer queue */
  1376. vout->next_frm = vout->cur_frm = list_entry(vout->dma_queue.next,
  1377. struct videobuf_buffer, queue);
  1378. /* Remove buffer from the buffer queue */
  1379. list_del(&vout->cur_frm->queue);
  1380. /* Mark state of the current frame to active */
  1381. vout->cur_frm->state = VIDEOBUF_ACTIVE;
  1382. /* Initialize field_id and started member */
  1383. vout->field_id = 0;
  1384. /* set flag here. Next QBUF will start DMA */
  1385. vout->streaming = 1;
  1386. vout->first_int = 1;
  1387. if (omap_vout_calculate_offset(vout)) {
  1388. ret = -EINVAL;
  1389. goto streamon_err1;
  1390. }
  1391. addr = (unsigned long) vout->queued_buf_addr[vout->cur_frm->i]
  1392. + vout->cropped_offset;
  1393. mask = DISPC_IRQ_VSYNC | DISPC_IRQ_EVSYNC_EVEN | DISPC_IRQ_EVSYNC_ODD
  1394. | DISPC_IRQ_VSYNC2;
  1395. omap_dispc_register_isr(omap_vout_isr, vout, mask);
  1396. /* First save the configuration in ovelray structure */
  1397. ret = omapvid_init(vout, addr);
  1398. if (ret)
  1399. v4l2_err(&vout->vid_dev->v4l2_dev,
  1400. "failed to set overlay info\n");
  1401. /* Enable the pipeline and set the Go bit */
  1402. ret = omapvid_apply_changes(vout);
  1403. if (ret)
  1404. v4l2_err(&vout->vid_dev->v4l2_dev, "failed to change mode\n");
  1405. for (j = 0; j < ovid->num_overlays; j++) {
  1406. struct omap_overlay *ovl = ovid->overlays[j];
  1407. struct omap_dss_device *dssdev = ovl->get_device(ovl);
  1408. if (dssdev) {
  1409. ret = ovl->enable(ovl);
  1410. if (ret)
  1411. goto streamon_err1;
  1412. }
  1413. }
  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. struct omap_dss_device *dssdev = ovl->get_device(ovl);
  1437. if (dssdev)
  1438. ovl->disable(ovl);
  1439. }
  1440. /* Turn of the pipeline */
  1441. ret = omapvid_apply_changes(vout);
  1442. if (ret)
  1443. v4l2_err(&vout->vid_dev->v4l2_dev, "failed to change mode in"
  1444. " streamoff\n");
  1445. INIT_LIST_HEAD(&vout->dma_queue);
  1446. ret = videobuf_streamoff(&vout->vbq);
  1447. return ret;
  1448. }
  1449. static int vidioc_s_fbuf(struct file *file, void *fh,
  1450. const struct v4l2_framebuffer *a)
  1451. {
  1452. int enable = 0;
  1453. struct omap_overlay *ovl;
  1454. struct omapvideo_info *ovid;
  1455. struct omap_vout_device *vout = fh;
  1456. struct omap_overlay_manager_info info;
  1457. enum omap_dss_trans_key_type key_type = OMAP_DSS_COLOR_KEY_GFX_DST;
  1458. ovid = &vout->vid_info;
  1459. ovl = ovid->overlays[0];
  1460. /* OMAP DSS doesn't support Source and Destination color
  1461. key together */
  1462. if ((a->flags & V4L2_FBUF_FLAG_SRC_CHROMAKEY) &&
  1463. (a->flags & V4L2_FBUF_FLAG_CHROMAKEY))
  1464. return -EINVAL;
  1465. /* OMAP DSS Doesn't support the Destination color key
  1466. and alpha blending together */
  1467. if ((a->flags & V4L2_FBUF_FLAG_CHROMAKEY) &&
  1468. (a->flags & V4L2_FBUF_FLAG_LOCAL_ALPHA))
  1469. return -EINVAL;
  1470. if ((a->flags & V4L2_FBUF_FLAG_SRC_CHROMAKEY)) {
  1471. vout->fbuf.flags |= V4L2_FBUF_FLAG_SRC_CHROMAKEY;
  1472. key_type = OMAP_DSS_COLOR_KEY_VID_SRC;
  1473. } else
  1474. vout->fbuf.flags &= ~V4L2_FBUF_FLAG_SRC_CHROMAKEY;
  1475. if ((a->flags & V4L2_FBUF_FLAG_CHROMAKEY)) {
  1476. vout->fbuf.flags |= V4L2_FBUF_FLAG_CHROMAKEY;
  1477. key_type = OMAP_DSS_COLOR_KEY_GFX_DST;
  1478. } else
  1479. vout->fbuf.flags &= ~V4L2_FBUF_FLAG_CHROMAKEY;
  1480. if (a->flags & (V4L2_FBUF_FLAG_CHROMAKEY |
  1481. V4L2_FBUF_FLAG_SRC_CHROMAKEY))
  1482. enable = 1;
  1483. else
  1484. enable = 0;
  1485. if (ovl->manager && ovl->manager->get_manager_info &&
  1486. ovl->manager->set_manager_info) {
  1487. ovl->manager->get_manager_info(ovl->manager, &info);
  1488. info.trans_enabled = enable;
  1489. info.trans_key_type = key_type;
  1490. info.trans_key = vout->win.chromakey;
  1491. if (ovl->manager->set_manager_info(ovl->manager, &info))
  1492. return -EINVAL;
  1493. }
  1494. if (a->flags & V4L2_FBUF_FLAG_LOCAL_ALPHA) {
  1495. vout->fbuf.flags |= V4L2_FBUF_FLAG_LOCAL_ALPHA;
  1496. enable = 1;
  1497. } else {
  1498. vout->fbuf.flags &= ~V4L2_FBUF_FLAG_LOCAL_ALPHA;
  1499. enable = 0;
  1500. }
  1501. if (ovl->manager && ovl->manager->get_manager_info &&
  1502. ovl->manager->set_manager_info) {
  1503. ovl->manager->get_manager_info(ovl->manager, &info);
  1504. /* enable this only if there is no zorder cap */
  1505. if ((ovl->caps & OMAP_DSS_OVL_CAP_ZORDER) == 0)
  1506. info.partial_alpha_enabled = enable;
  1507. if (ovl->manager->set_manager_info(ovl->manager, &info))
  1508. return -EINVAL;
  1509. }
  1510. return 0;
  1511. }
  1512. static int vidioc_g_fbuf(struct file *file, void *fh,
  1513. struct v4l2_framebuffer *a)
  1514. {
  1515. struct omap_overlay *ovl;
  1516. struct omapvideo_info *ovid;
  1517. struct omap_vout_device *vout = fh;
  1518. struct omap_overlay_manager_info info;
  1519. ovid = &vout->vid_info;
  1520. ovl = ovid->overlays[0];
  1521. /* The video overlay must stay within the framebuffer and can't be
  1522. positioned independently. */
  1523. a->flags = V4L2_FBUF_FLAG_OVERLAY;
  1524. a->capability = V4L2_FBUF_CAP_LOCAL_ALPHA | V4L2_FBUF_CAP_CHROMAKEY
  1525. | V4L2_FBUF_CAP_SRC_CHROMAKEY;
  1526. if (ovl->manager && ovl->manager->get_manager_info) {
  1527. ovl->manager->get_manager_info(ovl->manager, &info);
  1528. if (info.trans_key_type == OMAP_DSS_COLOR_KEY_VID_SRC)
  1529. a->flags |= V4L2_FBUF_FLAG_SRC_CHROMAKEY;
  1530. if (info.trans_key_type == OMAP_DSS_COLOR_KEY_GFX_DST)
  1531. a->flags |= V4L2_FBUF_FLAG_CHROMAKEY;
  1532. }
  1533. if (ovl->manager && ovl->manager->get_manager_info) {
  1534. ovl->manager->get_manager_info(ovl->manager, &info);
  1535. if (info.partial_alpha_enabled)
  1536. a->flags |= V4L2_FBUF_FLAG_LOCAL_ALPHA;
  1537. }
  1538. return 0;
  1539. }
  1540. static const struct v4l2_ioctl_ops vout_ioctl_ops = {
  1541. .vidioc_querycap = vidioc_querycap,
  1542. .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
  1543. .vidioc_g_fmt_vid_out = vidioc_g_fmt_vid_out,
  1544. .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out,
  1545. .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
  1546. .vidioc_queryctrl = vidioc_queryctrl,
  1547. .vidioc_g_ctrl = vidioc_g_ctrl,
  1548. .vidioc_s_fbuf = vidioc_s_fbuf,
  1549. .vidioc_g_fbuf = vidioc_g_fbuf,
  1550. .vidioc_s_ctrl = vidioc_s_ctrl,
  1551. .vidioc_try_fmt_vid_out_overlay = vidioc_try_fmt_vid_overlay,
  1552. .vidioc_s_fmt_vid_out_overlay = vidioc_s_fmt_vid_overlay,
  1553. .vidioc_g_fmt_vid_out_overlay = vidioc_g_fmt_vid_overlay,
  1554. .vidioc_cropcap = vidioc_cropcap,
  1555. .vidioc_g_crop = vidioc_g_crop,
  1556. .vidioc_s_crop = vidioc_s_crop,
  1557. .vidioc_reqbufs = vidioc_reqbufs,
  1558. .vidioc_querybuf = vidioc_querybuf,
  1559. .vidioc_qbuf = vidioc_qbuf,
  1560. .vidioc_dqbuf = vidioc_dqbuf,
  1561. .vidioc_streamon = vidioc_streamon,
  1562. .vidioc_streamoff = vidioc_streamoff,
  1563. };
  1564. static const struct v4l2_file_operations omap_vout_fops = {
  1565. .owner = THIS_MODULE,
  1566. .poll = omap_vout_poll,
  1567. .unlocked_ioctl = video_ioctl2,
  1568. .mmap = omap_vout_mmap,
  1569. .open = omap_vout_open,
  1570. .release = omap_vout_release,
  1571. };
  1572. /* Init functions used during driver initialization */
  1573. /* Initial setup of video_data */
  1574. static int __init omap_vout_setup_video_data(struct omap_vout_device *vout)
  1575. {
  1576. struct video_device *vfd;
  1577. struct v4l2_pix_format *pix;
  1578. struct v4l2_control *control;
  1579. struct omap_overlay *ovl = vout->vid_info.overlays[0];
  1580. struct omap_dss_device *display = ovl->get_device(ovl);
  1581. /* set the default pix */
  1582. pix = &vout->pix;
  1583. /* Set the default picture of QVGA */
  1584. pix->width = QQVGA_WIDTH;
  1585. pix->height = QQVGA_HEIGHT;
  1586. /* Default pixel format is RGB 5-6-5 */
  1587. pix->pixelformat = V4L2_PIX_FMT_RGB565;
  1588. pix->field = V4L2_FIELD_ANY;
  1589. pix->bytesperline = pix->width * 2;
  1590. pix->sizeimage = pix->bytesperline * pix->height;
  1591. pix->priv = 0;
  1592. pix->colorspace = V4L2_COLORSPACE_JPEG;
  1593. vout->bpp = RGB565_BPP;
  1594. vout->fbuf.fmt.width = display->panel.timings.x_res;
  1595. vout->fbuf.fmt.height = display->panel.timings.y_res;
  1596. /* Set the data structures for the overlay parameters*/
  1597. vout->win.global_alpha = 255;
  1598. vout->fbuf.flags = 0;
  1599. vout->fbuf.capability = V4L2_FBUF_CAP_LOCAL_ALPHA |
  1600. V4L2_FBUF_CAP_SRC_CHROMAKEY | V4L2_FBUF_CAP_CHROMAKEY;
  1601. vout->win.chromakey = 0;
  1602. omap_vout_new_format(pix, &vout->fbuf, &vout->crop, &vout->win);
  1603. /*Initialize the control variables for
  1604. rotation, flipping and background color. */
  1605. control = vout->control;
  1606. control[0].id = V4L2_CID_ROTATE;
  1607. control[0].value = 0;
  1608. vout->rotation = 0;
  1609. vout->mirror = 0;
  1610. vout->control[2].id = V4L2_CID_HFLIP;
  1611. vout->control[2].value = 0;
  1612. if (vout->vid_info.rotation_type == VOUT_ROT_VRFB)
  1613. vout->vrfb_bpp = 2;
  1614. control[1].id = V4L2_CID_BG_COLOR;
  1615. control[1].value = 0;
  1616. /* initialize the video_device struct */
  1617. vfd = vout->vfd = video_device_alloc();
  1618. if (!vfd) {
  1619. printk(KERN_ERR VOUT_NAME ": could not allocate"
  1620. " video device struct\n");
  1621. return -ENOMEM;
  1622. }
  1623. vfd->release = video_device_release;
  1624. vfd->ioctl_ops = &vout_ioctl_ops;
  1625. strlcpy(vfd->name, VOUT_NAME, sizeof(vfd->name));
  1626. vfd->fops = &omap_vout_fops;
  1627. vfd->v4l2_dev = &vout->vid_dev->v4l2_dev;
  1628. vfd->vfl_dir = VFL_DIR_TX;
  1629. mutex_init(&vout->lock);
  1630. vfd->minor = -1;
  1631. return 0;
  1632. }
  1633. /* Setup video buffers */
  1634. static int __init omap_vout_setup_video_bufs(struct platform_device *pdev,
  1635. int vid_num)
  1636. {
  1637. u32 numbuffers;
  1638. int ret = 0, i;
  1639. struct omapvideo_info *ovid;
  1640. struct omap_vout_device *vout;
  1641. struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
  1642. struct omap2video_device *vid_dev =
  1643. container_of(v4l2_dev, struct omap2video_device, v4l2_dev);
  1644. vout = vid_dev->vouts[vid_num];
  1645. ovid = &vout->vid_info;
  1646. numbuffers = (vid_num == 0) ? video1_numbuffers : video2_numbuffers;
  1647. vout->buffer_size = (vid_num == 0) ? video1_bufsize : video2_bufsize;
  1648. dev_info(&pdev->dev, "Buffer Size = %d\n", vout->buffer_size);
  1649. for (i = 0; i < numbuffers; i++) {
  1650. vout->buf_virt_addr[i] =
  1651. omap_vout_alloc_buffer(vout->buffer_size,
  1652. (u32 *) &vout->buf_phy_addr[i]);
  1653. if (!vout->buf_virt_addr[i]) {
  1654. numbuffers = i;
  1655. ret = -ENOMEM;
  1656. goto free_buffers;
  1657. }
  1658. }
  1659. vout->cropped_offset = 0;
  1660. if (ovid->rotation_type == VOUT_ROT_VRFB) {
  1661. int static_vrfb_allocation = (vid_num == 0) ?
  1662. vid1_static_vrfb_alloc : vid2_static_vrfb_alloc;
  1663. ret = omap_vout_setup_vrfb_bufs(pdev, vid_num,
  1664. static_vrfb_allocation);
  1665. }
  1666. return ret;
  1667. free_buffers:
  1668. for (i = 0; i < numbuffers; i++) {
  1669. omap_vout_free_buffer(vout->buf_virt_addr[i],
  1670. vout->buffer_size);
  1671. vout->buf_virt_addr[i] = 0;
  1672. vout->buf_phy_addr[i] = 0;
  1673. }
  1674. return ret;
  1675. }
  1676. /* Create video out devices */
  1677. static int __init omap_vout_create_video_devices(struct platform_device *pdev)
  1678. {
  1679. int ret = 0, k;
  1680. struct omap_vout_device *vout;
  1681. struct video_device *vfd = NULL;
  1682. struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
  1683. struct omap2video_device *vid_dev = container_of(v4l2_dev,
  1684. struct omap2video_device, v4l2_dev);
  1685. for (k = 0; k < pdev->num_resources; k++) {
  1686. vout = kzalloc(sizeof(struct omap_vout_device), GFP_KERNEL);
  1687. if (!vout) {
  1688. dev_err(&pdev->dev, ": could not allocate memory\n");
  1689. return -ENOMEM;
  1690. }
  1691. vout->vid = k;
  1692. vid_dev->vouts[k] = vout;
  1693. vout->vid_dev = vid_dev;
  1694. /* Select video2 if only 1 overlay is controlled by V4L2 */
  1695. if (pdev->num_resources == 1)
  1696. vout->vid_info.overlays[0] = vid_dev->overlays[k + 2];
  1697. else
  1698. /* Else select video1 and video2 one by one. */
  1699. vout->vid_info.overlays[0] = vid_dev->overlays[k + 1];
  1700. vout->vid_info.num_overlays = 1;
  1701. vout->vid_info.id = k + 1;
  1702. /* Set VRFB as rotation_type for omap2 and omap3 */
  1703. if (omap_vout_dss_omap24xx() || omap_vout_dss_omap34xx())
  1704. vout->vid_info.rotation_type = VOUT_ROT_VRFB;
  1705. /* Setup the default configuration for the video devices
  1706. */
  1707. if (omap_vout_setup_video_data(vout) != 0) {
  1708. ret = -ENOMEM;
  1709. goto error;
  1710. }
  1711. /* Allocate default number of buffers for the video streaming
  1712. * and reserve the VRFB space for rotation
  1713. */
  1714. if (omap_vout_setup_video_bufs(pdev, k) != 0) {
  1715. ret = -ENOMEM;
  1716. goto error1;
  1717. }
  1718. /* Register the Video device with V4L2
  1719. */
  1720. vfd = vout->vfd;
  1721. if (video_register_device(vfd, VFL_TYPE_GRABBER, -1) < 0) {
  1722. dev_err(&pdev->dev, ": Could not register "
  1723. "Video for Linux device\n");
  1724. vfd->minor = -1;
  1725. ret = -ENODEV;
  1726. goto error2;
  1727. }
  1728. video_set_drvdata(vfd, vout);
  1729. dev_info(&pdev->dev, ": registered and initialized"
  1730. " video device %d\n", vfd->minor);
  1731. if (k == (pdev->num_resources - 1))
  1732. return 0;
  1733. continue;
  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. }
  1744. return -ENODEV;
  1745. }
  1746. /* Driver functions */
  1747. static void omap_vout_cleanup_device(struct omap_vout_device *vout)
  1748. {
  1749. struct video_device *vfd;
  1750. struct omapvideo_info *ovid;
  1751. if (!vout)
  1752. return;
  1753. vfd = vout->vfd;
  1754. ovid = &vout->vid_info;
  1755. if (vfd) {
  1756. if (!video_is_registered(vfd)) {
  1757. /*
  1758. * The device was never registered, so release the
  1759. * video_device struct directly.
  1760. */
  1761. video_device_release(vfd);
  1762. } else {
  1763. /*
  1764. * The unregister function will release the video_device
  1765. * struct as well as unregistering it.
  1766. */
  1767. video_unregister_device(vfd);
  1768. }
  1769. }
  1770. if (ovid->rotation_type == VOUT_ROT_VRFB) {
  1771. omap_vout_release_vrfb(vout);
  1772. /* Free the VRFB buffer if allocated
  1773. * init time
  1774. */
  1775. if (vout->vrfb_static_allocation)
  1776. omap_vout_free_vrfb_buffers(vout);
  1777. }
  1778. omap_vout_free_buffers(vout);
  1779. kfree(vout);
  1780. }
  1781. static int omap_vout_remove(struct platform_device *pdev)
  1782. {
  1783. int k;
  1784. struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
  1785. struct omap2video_device *vid_dev = container_of(v4l2_dev, struct
  1786. omap2video_device, v4l2_dev);
  1787. v4l2_device_unregister(v4l2_dev);
  1788. for (k = 0; k < pdev->num_resources; k++)
  1789. omap_vout_cleanup_device(vid_dev->vouts[k]);
  1790. for (k = 0; k < vid_dev->num_displays; k++) {
  1791. if (vid_dev->displays[k]->state != OMAP_DSS_DISPLAY_DISABLED)
  1792. vid_dev->displays[k]->driver->disable(vid_dev->displays[k]);
  1793. omap_dss_put_device(vid_dev->displays[k]);
  1794. }
  1795. kfree(vid_dev);
  1796. return 0;
  1797. }
  1798. static int __init omap_vout_probe(struct platform_device *pdev)
  1799. {
  1800. int ret = 0, i;
  1801. struct omap_overlay *ovl;
  1802. struct omap_dss_device *dssdev = NULL;
  1803. struct omap_dss_device *def_display;
  1804. struct omap2video_device *vid_dev = NULL;
  1805. ret = omapdss_compat_init();
  1806. if (ret) {
  1807. dev_err(&pdev->dev, "failed to init dss\n");
  1808. return ret;
  1809. }
  1810. if (pdev->num_resources == 0) {
  1811. dev_err(&pdev->dev, "probed for an unknown device\n");
  1812. ret = -ENODEV;
  1813. goto err_dss_init;
  1814. }
  1815. vid_dev = kzalloc(sizeof(struct omap2video_device), GFP_KERNEL);
  1816. if (vid_dev == NULL) {
  1817. ret = -ENOMEM;
  1818. goto err_dss_init;
  1819. }
  1820. vid_dev->num_displays = 0;
  1821. for_each_dss_dev(dssdev) {
  1822. omap_dss_get_device(dssdev);
  1823. if (!dssdev->driver) {
  1824. dev_warn(&pdev->dev, "no driver for display: %s\n",
  1825. dssdev->name);
  1826. omap_dss_put_device(dssdev);
  1827. continue;
  1828. }
  1829. vid_dev->displays[vid_dev->num_displays++] = dssdev;
  1830. }
  1831. if (vid_dev->num_displays == 0) {
  1832. dev_err(&pdev->dev, "no displays\n");
  1833. ret = -EINVAL;
  1834. goto probe_err0;
  1835. }
  1836. vid_dev->num_overlays = omap_dss_get_num_overlays();
  1837. for (i = 0; i < vid_dev->num_overlays; i++)
  1838. vid_dev->overlays[i] = omap_dss_get_overlay(i);
  1839. vid_dev->num_managers = omap_dss_get_num_overlay_managers();
  1840. for (i = 0; i < vid_dev->num_managers; i++)
  1841. vid_dev->managers[i] = omap_dss_get_overlay_manager(i);
  1842. /* Get the Video1 overlay and video2 overlay.
  1843. * Setup the Display attached to that overlays
  1844. */
  1845. for (i = 1; i < vid_dev->num_overlays; i++) {
  1846. ovl = omap_dss_get_overlay(i);
  1847. dssdev = ovl->get_device(ovl);
  1848. if (dssdev) {
  1849. def_display = dssdev;
  1850. } else {
  1851. dev_warn(&pdev->dev, "cannot find display\n");
  1852. def_display = NULL;
  1853. }
  1854. if (def_display) {
  1855. struct omap_dss_driver *dssdrv = def_display->driver;
  1856. ret = dssdrv->enable(def_display);
  1857. if (ret) {
  1858. /* Here we are not considering a error
  1859. * as display may be enabled by frame
  1860. * buffer driver
  1861. */
  1862. dev_warn(&pdev->dev,
  1863. "'%s' Display already enabled\n",
  1864. def_display->name);
  1865. }
  1866. }
  1867. }
  1868. if (v4l2_device_register(&pdev->dev, &vid_dev->v4l2_dev) < 0) {
  1869. dev_err(&pdev->dev, "v4l2_device_register failed\n");
  1870. ret = -ENODEV;
  1871. goto probe_err1;
  1872. }
  1873. ret = omap_vout_create_video_devices(pdev);
  1874. if (ret)
  1875. goto probe_err2;
  1876. for (i = 0; i < vid_dev->num_displays; i++) {
  1877. struct omap_dss_device *display = vid_dev->displays[i];
  1878. if (display->driver->update)
  1879. display->driver->update(display, 0, 0,
  1880. display->panel.timings.x_res,
  1881. display->panel.timings.y_res);
  1882. }
  1883. return 0;
  1884. probe_err2:
  1885. v4l2_device_unregister(&vid_dev->v4l2_dev);
  1886. probe_err1:
  1887. for (i = 1; i < vid_dev->num_overlays; i++) {
  1888. def_display = NULL;
  1889. ovl = omap_dss_get_overlay(i);
  1890. dssdev = ovl->get_device(ovl);
  1891. if (dssdev)
  1892. def_display = dssdev;
  1893. if (def_display && def_display->driver)
  1894. def_display->driver->disable(def_display);
  1895. }
  1896. probe_err0:
  1897. kfree(vid_dev);
  1898. err_dss_init:
  1899. omapdss_compat_uninit();
  1900. return ret;
  1901. }
  1902. static struct platform_driver omap_vout_driver = {
  1903. .driver = {
  1904. .name = VOUT_NAME,
  1905. },
  1906. .remove = omap_vout_remove,
  1907. };
  1908. static int __init omap_vout_init(void)
  1909. {
  1910. if (platform_driver_probe(&omap_vout_driver, omap_vout_probe) != 0) {
  1911. printk(KERN_ERR VOUT_NAME ":Could not register Video driver\n");
  1912. return -EINVAL;
  1913. }
  1914. return 0;
  1915. }
  1916. static void omap_vout_cleanup(void)
  1917. {
  1918. platform_driver_unregister(&omap_vout_driver);
  1919. }
  1920. late_initcall(omap_vout_init);
  1921. module_exit(omap_vout_cleanup);