omap_vout.c 57 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 <plat/dma.h>
  45. #include <plat/vrfb.h>
  46. #include <video/omapdss.h>
  47. #include "omap_voutlib.h"
  48. #include "omap_voutdef.h"
  49. #include "omap_vout_vrfb.h"
  50. MODULE_AUTHOR("Texas Instruments");
  51. MODULE_DESCRIPTION("OMAP Video for Linux Video out driver");
  52. MODULE_LICENSE("GPL");
  53. /* Driver Configuration macros */
  54. #define VOUT_NAME "omap_vout"
  55. enum omap_vout_channels {
  56. OMAP_VIDEO1,
  57. OMAP_VIDEO2,
  58. };
  59. static struct videobuf_queue_ops video_vbq_ops;
  60. /* Variables configurable through module params*/
  61. static u32 video1_numbuffers = 3;
  62. static u32 video2_numbuffers = 3;
  63. static u32 video1_bufsize = OMAP_VOUT_MAX_BUF_SIZE;
  64. static u32 video2_bufsize = OMAP_VOUT_MAX_BUF_SIZE;
  65. static u32 vid1_static_vrfb_alloc;
  66. static u32 vid2_static_vrfb_alloc;
  67. static int debug;
  68. /* Module parameters */
  69. module_param(video1_numbuffers, uint, S_IRUGO);
  70. MODULE_PARM_DESC(video1_numbuffers,
  71. "Number of buffers to be allocated at init time for Video1 device.");
  72. module_param(video2_numbuffers, uint, S_IRUGO);
  73. MODULE_PARM_DESC(video2_numbuffers,
  74. "Number of buffers to be allocated at init time for Video2 device.");
  75. module_param(video1_bufsize, uint, S_IRUGO);
  76. MODULE_PARM_DESC(video1_bufsize,
  77. "Size of the buffer to be allocated for video1 device");
  78. module_param(video2_bufsize, uint, S_IRUGO);
  79. MODULE_PARM_DESC(video2_bufsize,
  80. "Size of the buffer to be allocated for video2 device");
  81. module_param(vid1_static_vrfb_alloc, bool, S_IRUGO);
  82. MODULE_PARM_DESC(vid1_static_vrfb_alloc,
  83. "Static allocation of the VRFB buffer for video1 device");
  84. module_param(vid2_static_vrfb_alloc, bool, S_IRUGO);
  85. MODULE_PARM_DESC(vid2_static_vrfb_alloc,
  86. "Static allocation of the VRFB buffer for video2 device");
  87. module_param(debug, bool, S_IRUGO);
  88. MODULE_PARM_DESC(debug, "Debug level (0-1)");
  89. /* list of image formats supported by OMAP2 video pipelines */
  90. static const struct v4l2_fmtdesc omap_formats[] = {
  91. {
  92. /* Note: V4L2 defines RGB565 as:
  93. *
  94. * Byte 0 Byte 1
  95. * g2 g1 g0 r4 r3 r2 r1 r0 b4 b3 b2 b1 b0 g5 g4 g3
  96. *
  97. * We interpret RGB565 as:
  98. *
  99. * Byte 0 Byte 1
  100. * g2 g1 g0 b4 b3 b2 b1 b0 r4 r3 r2 r1 r0 g5 g4 g3
  101. */
  102. .description = "RGB565, le",
  103. .pixelformat = V4L2_PIX_FMT_RGB565,
  104. },
  105. {
  106. /* Note: V4L2 defines RGB32 as: RGB-8-8-8-8 we use
  107. * this for RGB24 unpack mode, the last 8 bits are ignored
  108. * */
  109. .description = "RGB32, le",
  110. .pixelformat = V4L2_PIX_FMT_RGB32,
  111. },
  112. {
  113. /* Note: V4L2 defines RGB24 as: RGB-8-8-8 we use
  114. * this for RGB24 packed mode
  115. *
  116. */
  117. .description = "RGB24, le",
  118. .pixelformat = V4L2_PIX_FMT_RGB24,
  119. },
  120. {
  121. .description = "YUYV (YUV 4:2:2), packed",
  122. .pixelformat = V4L2_PIX_FMT_YUYV,
  123. },
  124. {
  125. .description = "UYVY, packed",
  126. .pixelformat = V4L2_PIX_FMT_UYVY,
  127. },
  128. };
  129. #define NUM_OUTPUT_FORMATS (ARRAY_SIZE(omap_formats))
  130. /*
  131. * Try format
  132. */
  133. static int omap_vout_try_format(struct v4l2_pix_format *pix)
  134. {
  135. int ifmt, bpp = 0;
  136. pix->height = clamp(pix->height, (u32)VID_MIN_HEIGHT,
  137. (u32)VID_MAX_HEIGHT);
  138. pix->width = clamp(pix->width, (u32)VID_MIN_WIDTH, (u32)VID_MAX_WIDTH);
  139. for (ifmt = 0; ifmt < NUM_OUTPUT_FORMATS; ifmt++) {
  140. if (pix->pixelformat == omap_formats[ifmt].pixelformat)
  141. break;
  142. }
  143. if (ifmt == NUM_OUTPUT_FORMATS)
  144. ifmt = 0;
  145. pix->pixelformat = omap_formats[ifmt].pixelformat;
  146. pix->field = V4L2_FIELD_ANY;
  147. pix->priv = 0;
  148. switch (pix->pixelformat) {
  149. case V4L2_PIX_FMT_YUYV:
  150. case V4L2_PIX_FMT_UYVY:
  151. default:
  152. pix->colorspace = V4L2_COLORSPACE_JPEG;
  153. bpp = YUYV_BPP;
  154. break;
  155. case V4L2_PIX_FMT_RGB565:
  156. case V4L2_PIX_FMT_RGB565X:
  157. pix->colorspace = V4L2_COLORSPACE_SRGB;
  158. bpp = RGB565_BPP;
  159. break;
  160. case V4L2_PIX_FMT_RGB24:
  161. pix->colorspace = V4L2_COLORSPACE_SRGB;
  162. bpp = RGB24_BPP;
  163. break;
  164. case V4L2_PIX_FMT_RGB32:
  165. case V4L2_PIX_FMT_BGR32:
  166. pix->colorspace = V4L2_COLORSPACE_SRGB;
  167. bpp = RGB32_BPP;
  168. break;
  169. }
  170. pix->bytesperline = pix->width * bpp;
  171. pix->sizeimage = pix->bytesperline * pix->height;
  172. return bpp;
  173. }
  174. /*
  175. * omap_vout_uservirt_to_phys: This inline function is used to convert user
  176. * space virtual address to physical address.
  177. */
  178. static u32 omap_vout_uservirt_to_phys(u32 virtp)
  179. {
  180. unsigned long physp = 0;
  181. struct vm_area_struct *vma;
  182. struct mm_struct *mm = current->mm;
  183. vma = find_vma(mm, virtp);
  184. /* For kernel direct-mapped memory, take the easy way */
  185. if (virtp >= PAGE_OFFSET) {
  186. physp = virt_to_phys((void *) virtp);
  187. } else 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. } else {
  192. /* otherwise, use get_user_pages() for general userland pages */
  193. int res, nr_pages = 1;
  194. struct page *pages;
  195. down_read(&current->mm->mmap_sem);
  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__, 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 unsigned int omap_vout_poll(struct file *file,
  724. struct poll_table_struct *wait)
  725. {
  726. struct omap_vout_device *vout = file->private_data;
  727. struct videobuf_queue *q = &vout->vbq;
  728. return videobuf_poll_stream(file, q, wait);
  729. }
  730. static void omap_vout_vm_open(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_open [vma=%08lx-%08lx]\n", vma->vm_start, vma->vm_end);
  735. vout->mmap_count++;
  736. }
  737. static void omap_vout_vm_close(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_close [vma=%08lx-%08lx]\n", vma->vm_start, vma->vm_end);
  742. vout->mmap_count--;
  743. }
  744. static struct vm_operations_struct omap_vout_vm_ops = {
  745. .open = omap_vout_vm_open,
  746. .close = omap_vout_vm_close,
  747. };
  748. static int omap_vout_mmap(struct file *file, struct vm_area_struct *vma)
  749. {
  750. int i;
  751. void *pos;
  752. unsigned long start = vma->vm_start;
  753. unsigned long size = (vma->vm_end - vma->vm_start);
  754. struct omap_vout_device *vout = file->private_data;
  755. struct videobuf_queue *q = &vout->vbq;
  756. v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
  757. " %s pgoff=0x%lx, start=0x%lx, end=0x%lx\n", __func__,
  758. vma->vm_pgoff, vma->vm_start, vma->vm_end);
  759. /* look for the buffer to map */
  760. for (i = 0; i < VIDEO_MAX_FRAME; i++) {
  761. if (NULL == q->bufs[i])
  762. continue;
  763. if (V4L2_MEMORY_MMAP != q->bufs[i]->memory)
  764. continue;
  765. if (q->bufs[i]->boff == (vma->vm_pgoff << PAGE_SHIFT))
  766. break;
  767. }
  768. if (VIDEO_MAX_FRAME == i) {
  769. v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev,
  770. "offset invalid [offset=0x%lx]\n",
  771. (vma->vm_pgoff << PAGE_SHIFT));
  772. return -EINVAL;
  773. }
  774. /* Check the size of the buffer */
  775. if (size > vout->buffer_size) {
  776. v4l2_err(&vout->vid_dev->v4l2_dev,
  777. "insufficient memory [%lu] [%u]\n",
  778. size, vout->buffer_size);
  779. return -ENOMEM;
  780. }
  781. q->bufs[i]->baddr = vma->vm_start;
  782. vma->vm_flags |= VM_RESERVED;
  783. vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
  784. vma->vm_ops = &omap_vout_vm_ops;
  785. vma->vm_private_data = (void *) vout;
  786. pos = (void *)vout->buf_virt_addr[i];
  787. vma->vm_pgoff = virt_to_phys((void *)pos) >> PAGE_SHIFT;
  788. while (size > 0) {
  789. unsigned long pfn;
  790. pfn = virt_to_phys((void *) pos) >> PAGE_SHIFT;
  791. if (remap_pfn_range(vma, start, pfn, PAGE_SIZE, PAGE_SHARED))
  792. return -EAGAIN;
  793. start += PAGE_SIZE;
  794. pos += PAGE_SIZE;
  795. size -= PAGE_SIZE;
  796. }
  797. vout->mmap_count++;
  798. v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Exiting %s\n", __func__);
  799. return 0;
  800. }
  801. static int omap_vout_release(struct file *file)
  802. {
  803. unsigned int ret, i;
  804. struct videobuf_queue *q;
  805. struct omapvideo_info *ovid;
  806. struct omap_vout_device *vout = file->private_data;
  807. v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Entering %s\n", __func__);
  808. ovid = &vout->vid_info;
  809. if (!vout)
  810. return 0;
  811. q = &vout->vbq;
  812. /* Disable all the overlay managers connected with this interface */
  813. for (i = 0; i < ovid->num_overlays; i++) {
  814. struct omap_overlay *ovl = ovid->overlays[i];
  815. if (ovl->manager && ovl->manager->device) {
  816. struct omap_overlay_info info;
  817. ovl->get_overlay_info(ovl, &info);
  818. info.enabled = 0;
  819. ovl->set_overlay_info(ovl, &info);
  820. }
  821. }
  822. /* Turn off the pipeline */
  823. ret = omapvid_apply_changes(vout);
  824. if (ret)
  825. v4l2_warn(&vout->vid_dev->v4l2_dev,
  826. "Unable to apply changes\n");
  827. /* Free all buffers */
  828. omap_vout_free_extra_buffers(vout);
  829. /* Free the VRFB buffers only if they are allocated
  830. * during reqbufs. Don't free if init time allocated
  831. */
  832. if (ovid->rotation_type == VOUT_ROT_VRFB) {
  833. if (!vout->vrfb_static_allocation)
  834. omap_vout_free_vrfb_buffers(vout);
  835. }
  836. videobuf_mmap_free(q);
  837. /* Even if apply changes fails we should continue
  838. freeing allocated memory */
  839. if (vout->streaming) {
  840. u32 mask = 0;
  841. mask = DISPC_IRQ_VSYNC | DISPC_IRQ_EVSYNC_EVEN |
  842. DISPC_IRQ_EVSYNC_ODD | DISPC_IRQ_VSYNC2;
  843. omap_dispc_unregister_isr(omap_vout_isr, vout, mask);
  844. vout->streaming = 0;
  845. videobuf_streamoff(q);
  846. videobuf_queue_cancel(q);
  847. }
  848. if (vout->mmap_count != 0)
  849. vout->mmap_count = 0;
  850. vout->opened -= 1;
  851. file->private_data = NULL;
  852. if (vout->buffer_allocated)
  853. videobuf_mmap_free(q);
  854. v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Exiting %s\n", __func__);
  855. return ret;
  856. }
  857. static int omap_vout_open(struct file *file)
  858. {
  859. struct videobuf_queue *q;
  860. struct omap_vout_device *vout = NULL;
  861. vout = video_drvdata(file);
  862. v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Entering %s\n", __func__);
  863. if (vout == NULL)
  864. return -ENODEV;
  865. /* for now, we only support single open */
  866. if (vout->opened)
  867. return -EBUSY;
  868. vout->opened += 1;
  869. file->private_data = vout;
  870. vout->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  871. q = &vout->vbq;
  872. video_vbq_ops.buf_setup = omap_vout_buffer_setup;
  873. video_vbq_ops.buf_prepare = omap_vout_buffer_prepare;
  874. video_vbq_ops.buf_release = omap_vout_buffer_release;
  875. video_vbq_ops.buf_queue = omap_vout_buffer_queue;
  876. spin_lock_init(&vout->vbq_lock);
  877. videobuf_queue_dma_contig_init(q, &video_vbq_ops, q->dev,
  878. &vout->vbq_lock, vout->type, V4L2_FIELD_NONE,
  879. sizeof(struct videobuf_buffer), vout, NULL);
  880. v4l2_dbg(1, debug, &vout->vid_dev->v4l2_dev, "Exiting %s\n", __func__);
  881. return 0;
  882. }
  883. /*
  884. * V4L2 ioctls
  885. */
  886. static int vidioc_querycap(struct file *file, void *fh,
  887. struct v4l2_capability *cap)
  888. {
  889. struct omap_vout_device *vout = fh;
  890. strlcpy(cap->driver, VOUT_NAME, sizeof(cap->driver));
  891. strlcpy(cap->card, vout->vfd->name, sizeof(cap->card));
  892. cap->bus_info[0] = '\0';
  893. cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_OUTPUT;
  894. return 0;
  895. }
  896. static int vidioc_enum_fmt_vid_out(struct file *file, void *fh,
  897. struct v4l2_fmtdesc *fmt)
  898. {
  899. int index = fmt->index;
  900. if (index >= NUM_OUTPUT_FORMATS)
  901. return -EINVAL;
  902. fmt->flags = omap_formats[index].flags;
  903. strlcpy(fmt->description, omap_formats[index].description,
  904. sizeof(fmt->description));
  905. fmt->pixelformat = omap_formats[index].pixelformat;
  906. return 0;
  907. }
  908. static int vidioc_g_fmt_vid_out(struct file *file, void *fh,
  909. struct v4l2_format *f)
  910. {
  911. struct omap_vout_device *vout = fh;
  912. f->fmt.pix = vout->pix;
  913. return 0;
  914. }
  915. static int vidioc_try_fmt_vid_out(struct file *file, void *fh,
  916. struct v4l2_format *f)
  917. {
  918. struct omap_overlay *ovl;
  919. struct omapvideo_info *ovid;
  920. struct omap_video_timings *timing;
  921. struct omap_vout_device *vout = fh;
  922. ovid = &vout->vid_info;
  923. ovl = ovid->overlays[0];
  924. if (!ovl->manager || !ovl->manager->device)
  925. return -EINVAL;
  926. /* get the display device attached to the overlay */
  927. timing = &ovl->manager->device->panel.timings;
  928. vout->fbuf.fmt.height = timing->y_res;
  929. vout->fbuf.fmt.width = timing->x_res;
  930. omap_vout_try_format(&f->fmt.pix);
  931. return 0;
  932. }
  933. static int vidioc_s_fmt_vid_out(struct file *file, void *fh,
  934. struct v4l2_format *f)
  935. {
  936. int ret, bpp;
  937. struct omap_overlay *ovl;
  938. struct omapvideo_info *ovid;
  939. struct omap_video_timings *timing;
  940. struct omap_vout_device *vout = fh;
  941. if (vout->streaming)
  942. return -EBUSY;
  943. mutex_lock(&vout->lock);
  944. ovid = &vout->vid_info;
  945. ovl = ovid->overlays[0];
  946. /* get the display device attached to the overlay */
  947. if (!ovl->manager || !ovl->manager->device) {
  948. ret = -EINVAL;
  949. goto s_fmt_vid_out_exit;
  950. }
  951. timing = &ovl->manager->device->panel.timings;
  952. /* We dont support RGB24-packed mode if vrfb rotation
  953. * is enabled*/
  954. if ((is_rotation_enabled(vout)) &&
  955. f->fmt.pix.pixelformat == V4L2_PIX_FMT_RGB24) {
  956. ret = -EINVAL;
  957. goto s_fmt_vid_out_exit;
  958. }
  959. /* get the framebuffer parameters */
  960. if (is_rotation_90_or_270(vout)) {
  961. vout->fbuf.fmt.height = timing->x_res;
  962. vout->fbuf.fmt.width = timing->y_res;
  963. } else {
  964. vout->fbuf.fmt.height = timing->y_res;
  965. vout->fbuf.fmt.width = timing->x_res;
  966. }
  967. /* change to samller size is OK */
  968. bpp = omap_vout_try_format(&f->fmt.pix);
  969. f->fmt.pix.sizeimage = f->fmt.pix.width * f->fmt.pix.height * bpp;
  970. /* try & set the new output format */
  971. vout->bpp = bpp;
  972. vout->pix = f->fmt.pix;
  973. vout->vrfb_bpp = 1;
  974. /* If YUYV then vrfb bpp is 2, for others its 1 */
  975. if (V4L2_PIX_FMT_YUYV == vout->pix.pixelformat ||
  976. V4L2_PIX_FMT_UYVY == vout->pix.pixelformat)
  977. vout->vrfb_bpp = 2;
  978. /* set default crop and win */
  979. omap_vout_new_format(&vout->pix, &vout->fbuf, &vout->crop, &vout->win);
  980. /* Save the changes in the overlay strcuture */
  981. ret = omapvid_init(vout, 0);
  982. if (ret) {
  983. v4l2_err(&vout->vid_dev->v4l2_dev, "failed to change mode\n");
  984. goto s_fmt_vid_out_exit;
  985. }
  986. ret = 0;
  987. s_fmt_vid_out_exit:
  988. mutex_unlock(&vout->lock);
  989. return ret;
  990. }
  991. static int vidioc_try_fmt_vid_overlay(struct file *file, void *fh,
  992. struct v4l2_format *f)
  993. {
  994. int ret = 0;
  995. struct omap_vout_device *vout = fh;
  996. struct omap_overlay *ovl;
  997. struct omapvideo_info *ovid;
  998. struct v4l2_window *win = &f->fmt.win;
  999. ovid = &vout->vid_info;
  1000. ovl = ovid->overlays[0];
  1001. ret = omap_vout_try_window(&vout->fbuf, win);
  1002. if (!ret) {
  1003. if ((ovl->caps & OMAP_DSS_OVL_CAP_GLOBAL_ALPHA) == 0)
  1004. win->global_alpha = 255;
  1005. else
  1006. win->global_alpha = f->fmt.win.global_alpha;
  1007. }
  1008. return ret;
  1009. }
  1010. static int vidioc_s_fmt_vid_overlay(struct file *file, void *fh,
  1011. struct v4l2_format *f)
  1012. {
  1013. int ret = 0;
  1014. struct omap_overlay *ovl;
  1015. struct omapvideo_info *ovid;
  1016. struct omap_vout_device *vout = fh;
  1017. struct v4l2_window *win = &f->fmt.win;
  1018. mutex_lock(&vout->lock);
  1019. ovid = &vout->vid_info;
  1020. ovl = ovid->overlays[0];
  1021. ret = omap_vout_new_window(&vout->crop, &vout->win, &vout->fbuf, win);
  1022. if (!ret) {
  1023. /* Video1 plane does not support global alpha on OMAP3 */
  1024. if ((ovl->caps & OMAP_DSS_OVL_CAP_GLOBAL_ALPHA) == 0)
  1025. vout->win.global_alpha = 255;
  1026. else
  1027. vout->win.global_alpha = f->fmt.win.global_alpha;
  1028. vout->win.chromakey = f->fmt.win.chromakey;
  1029. }
  1030. mutex_unlock(&vout->lock);
  1031. return ret;
  1032. }
  1033. static int vidioc_enum_fmt_vid_overlay(struct file *file, void *fh,
  1034. struct v4l2_fmtdesc *fmt)
  1035. {
  1036. int index = fmt->index;
  1037. if (index >= NUM_OUTPUT_FORMATS)
  1038. return -EINVAL;
  1039. fmt->flags = omap_formats[index].flags;
  1040. strlcpy(fmt->description, omap_formats[index].description,
  1041. sizeof(fmt->description));
  1042. fmt->pixelformat = omap_formats[index].pixelformat;
  1043. return 0;
  1044. }
  1045. static int vidioc_g_fmt_vid_overlay(struct file *file, void *fh,
  1046. struct v4l2_format *f)
  1047. {
  1048. u32 key_value = 0;
  1049. struct omap_overlay *ovl;
  1050. struct omapvideo_info *ovid;
  1051. struct omap_vout_device *vout = fh;
  1052. struct omap_overlay_manager_info info;
  1053. struct v4l2_window *win = &f->fmt.win;
  1054. ovid = &vout->vid_info;
  1055. ovl = ovid->overlays[0];
  1056. win->w = vout->win.w;
  1057. win->field = vout->win.field;
  1058. win->global_alpha = vout->win.global_alpha;
  1059. if (ovl->manager && ovl->manager->get_manager_info) {
  1060. ovl->manager->get_manager_info(ovl->manager, &info);
  1061. key_value = info.trans_key;
  1062. }
  1063. win->chromakey = key_value;
  1064. return 0;
  1065. }
  1066. static int vidioc_cropcap(struct file *file, void *fh,
  1067. struct v4l2_cropcap *cropcap)
  1068. {
  1069. struct omap_vout_device *vout = fh;
  1070. struct v4l2_pix_format *pix = &vout->pix;
  1071. if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  1072. return -EINVAL;
  1073. /* Width and height are always even */
  1074. cropcap->bounds.width = pix->width & ~1;
  1075. cropcap->bounds.height = pix->height & ~1;
  1076. omap_vout_default_crop(&vout->pix, &vout->fbuf, &cropcap->defrect);
  1077. cropcap->pixelaspect.numerator = 1;
  1078. cropcap->pixelaspect.denominator = 1;
  1079. return 0;
  1080. }
  1081. static int vidioc_g_crop(struct file *file, void *fh, struct v4l2_crop *crop)
  1082. {
  1083. struct omap_vout_device *vout = fh;
  1084. if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  1085. return -EINVAL;
  1086. crop->c = vout->crop;
  1087. return 0;
  1088. }
  1089. static int vidioc_s_crop(struct file *file, void *fh, struct v4l2_crop *crop)
  1090. {
  1091. int ret = -EINVAL;
  1092. struct omap_vout_device *vout = fh;
  1093. struct omapvideo_info *ovid;
  1094. struct omap_overlay *ovl;
  1095. struct omap_video_timings *timing;
  1096. if (vout->streaming)
  1097. return -EBUSY;
  1098. mutex_lock(&vout->lock);
  1099. ovid = &vout->vid_info;
  1100. ovl = ovid->overlays[0];
  1101. if (!ovl->manager || !ovl->manager->device) {
  1102. ret = -EINVAL;
  1103. goto s_crop_err;
  1104. }
  1105. /* get the display device attached to the overlay */
  1106. timing = &ovl->manager->device->panel.timings;
  1107. if (is_rotation_90_or_270(vout)) {
  1108. vout->fbuf.fmt.height = timing->x_res;
  1109. vout->fbuf.fmt.width = timing->y_res;
  1110. } else {
  1111. vout->fbuf.fmt.height = timing->y_res;
  1112. vout->fbuf.fmt.width = timing->x_res;
  1113. }
  1114. if (crop->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
  1115. ret = omap_vout_new_crop(&vout->pix, &vout->crop, &vout->win,
  1116. &vout->fbuf, &crop->c);
  1117. s_crop_err:
  1118. mutex_unlock(&vout->lock);
  1119. return ret;
  1120. }
  1121. static int vidioc_queryctrl(struct file *file, void *fh,
  1122. struct v4l2_queryctrl *ctrl)
  1123. {
  1124. int ret = 0;
  1125. switch (ctrl->id) {
  1126. case V4L2_CID_ROTATE:
  1127. ret = v4l2_ctrl_query_fill(ctrl, 0, 270, 90, 0);
  1128. break;
  1129. case V4L2_CID_BG_COLOR:
  1130. ret = v4l2_ctrl_query_fill(ctrl, 0, 0xFFFFFF, 1, 0);
  1131. break;
  1132. case V4L2_CID_VFLIP:
  1133. ret = v4l2_ctrl_query_fill(ctrl, 0, 1, 1, 0);
  1134. break;
  1135. default:
  1136. ctrl->name[0] = '\0';
  1137. ret = -EINVAL;
  1138. }
  1139. return ret;
  1140. }
  1141. static int vidioc_g_ctrl(struct file *file, void *fh, struct v4l2_control *ctrl)
  1142. {
  1143. int ret = 0;
  1144. struct omap_vout_device *vout = fh;
  1145. switch (ctrl->id) {
  1146. case V4L2_CID_ROTATE:
  1147. ctrl->value = vout->control[0].value;
  1148. break;
  1149. case V4L2_CID_BG_COLOR:
  1150. {
  1151. struct omap_overlay_manager_info info;
  1152. struct omap_overlay *ovl;
  1153. ovl = vout->vid_info.overlays[0];
  1154. if (!ovl->manager || !ovl->manager->get_manager_info) {
  1155. ret = -EINVAL;
  1156. break;
  1157. }
  1158. ovl->manager->get_manager_info(ovl->manager, &info);
  1159. ctrl->value = info.default_color;
  1160. break;
  1161. }
  1162. case V4L2_CID_VFLIP:
  1163. ctrl->value = vout->control[2].value;
  1164. break;
  1165. default:
  1166. ret = -EINVAL;
  1167. }
  1168. return ret;
  1169. }
  1170. static int vidioc_s_ctrl(struct file *file, void *fh, struct v4l2_control *a)
  1171. {
  1172. int ret = 0;
  1173. struct omap_vout_device *vout = fh;
  1174. switch (a->id) {
  1175. case V4L2_CID_ROTATE:
  1176. {
  1177. struct omapvideo_info *ovid;
  1178. int rotation = a->value;
  1179. ovid = &vout->vid_info;
  1180. mutex_lock(&vout->lock);
  1181. if (rotation && ovid->rotation_type == VOUT_ROT_NONE) {
  1182. mutex_unlock(&vout->lock);
  1183. ret = -ERANGE;
  1184. break;
  1185. }
  1186. if (rotation && vout->pix.pixelformat == V4L2_PIX_FMT_RGB24) {
  1187. mutex_unlock(&vout->lock);
  1188. ret = -EINVAL;
  1189. break;
  1190. }
  1191. if (v4l2_rot_to_dss_rot(rotation, &vout->rotation,
  1192. vout->mirror)) {
  1193. mutex_unlock(&vout->lock);
  1194. ret = -EINVAL;
  1195. break;
  1196. }
  1197. vout->control[0].value = rotation;
  1198. mutex_unlock(&vout->lock);
  1199. break;
  1200. }
  1201. case V4L2_CID_BG_COLOR:
  1202. {
  1203. struct omap_overlay *ovl;
  1204. unsigned int color = a->value;
  1205. struct omap_overlay_manager_info info;
  1206. ovl = vout->vid_info.overlays[0];
  1207. mutex_lock(&vout->lock);
  1208. if (!ovl->manager || !ovl->manager->get_manager_info) {
  1209. mutex_unlock(&vout->lock);
  1210. ret = -EINVAL;
  1211. break;
  1212. }
  1213. ovl->manager->get_manager_info(ovl->manager, &info);
  1214. info.default_color = color;
  1215. if (ovl->manager->set_manager_info(ovl->manager, &info)) {
  1216. mutex_unlock(&vout->lock);
  1217. ret = -EINVAL;
  1218. break;
  1219. }
  1220. vout->control[1].value = color;
  1221. mutex_unlock(&vout->lock);
  1222. break;
  1223. }
  1224. case V4L2_CID_VFLIP:
  1225. {
  1226. struct omap_overlay *ovl;
  1227. struct omapvideo_info *ovid;
  1228. unsigned int mirror = a->value;
  1229. ovid = &vout->vid_info;
  1230. ovl = ovid->overlays[0];
  1231. mutex_lock(&vout->lock);
  1232. if (mirror && ovid->rotation_type == VOUT_ROT_NONE) {
  1233. mutex_unlock(&vout->lock);
  1234. ret = -ERANGE;
  1235. break;
  1236. }
  1237. if (mirror && vout->pix.pixelformat == V4L2_PIX_FMT_RGB24) {
  1238. mutex_unlock(&vout->lock);
  1239. ret = -EINVAL;
  1240. break;
  1241. }
  1242. vout->mirror = mirror;
  1243. vout->control[2].value = mirror;
  1244. mutex_unlock(&vout->lock);
  1245. break;
  1246. }
  1247. default:
  1248. ret = -EINVAL;
  1249. }
  1250. return ret;
  1251. }
  1252. static int vidioc_reqbufs(struct file *file, void *fh,
  1253. struct v4l2_requestbuffers *req)
  1254. {
  1255. int ret = 0;
  1256. unsigned int i, num_buffers = 0;
  1257. struct omap_vout_device *vout = fh;
  1258. struct videobuf_queue *q = &vout->vbq;
  1259. if ((req->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) || (req->count < 0))
  1260. return -EINVAL;
  1261. /* if memory is not mmp or userptr
  1262. return error */
  1263. if ((V4L2_MEMORY_MMAP != req->memory) &&
  1264. (V4L2_MEMORY_USERPTR != req->memory))
  1265. return -EINVAL;
  1266. mutex_lock(&vout->lock);
  1267. /* Cannot be requested when streaming is on */
  1268. if (vout->streaming) {
  1269. ret = -EBUSY;
  1270. goto reqbuf_err;
  1271. }
  1272. /* If buffers are already allocated free them */
  1273. if (q->bufs[0] && (V4L2_MEMORY_MMAP == q->bufs[0]->memory)) {
  1274. if (vout->mmap_count) {
  1275. ret = -EBUSY;
  1276. goto reqbuf_err;
  1277. }
  1278. num_buffers = (vout->vid == OMAP_VIDEO1) ?
  1279. video1_numbuffers : video2_numbuffers;
  1280. for (i = num_buffers; i < vout->buffer_allocated; i++) {
  1281. omap_vout_free_buffer(vout->buf_virt_addr[i],
  1282. vout->buffer_size);
  1283. vout->buf_virt_addr[i] = 0;
  1284. vout->buf_phy_addr[i] = 0;
  1285. }
  1286. vout->buffer_allocated = num_buffers;
  1287. videobuf_mmap_free(q);
  1288. } else if (q->bufs[0] && (V4L2_MEMORY_USERPTR == q->bufs[0]->memory)) {
  1289. if (vout->buffer_allocated) {
  1290. videobuf_mmap_free(q);
  1291. for (i = 0; i < vout->buffer_allocated; i++) {
  1292. kfree(q->bufs[i]);
  1293. q->bufs[i] = NULL;
  1294. }
  1295. vout->buffer_allocated = 0;
  1296. }
  1297. }
  1298. /*store the memory type in data structure */
  1299. vout->memory = req->memory;
  1300. INIT_LIST_HEAD(&vout->dma_queue);
  1301. /* call videobuf_reqbufs api */
  1302. ret = videobuf_reqbufs(q, req);
  1303. if (ret < 0)
  1304. goto reqbuf_err;
  1305. vout->buffer_allocated = req->count;
  1306. reqbuf_err:
  1307. mutex_unlock(&vout->lock);
  1308. return ret;
  1309. }
  1310. static int vidioc_querybuf(struct file *file, void *fh,
  1311. struct v4l2_buffer *b)
  1312. {
  1313. struct omap_vout_device *vout = fh;
  1314. return videobuf_querybuf(&vout->vbq, b);
  1315. }
  1316. static int vidioc_qbuf(struct file *file, void *fh,
  1317. struct v4l2_buffer *buffer)
  1318. {
  1319. struct omap_vout_device *vout = fh;
  1320. struct videobuf_queue *q = &vout->vbq;
  1321. if ((V4L2_BUF_TYPE_VIDEO_OUTPUT != buffer->type) ||
  1322. (buffer->index >= vout->buffer_allocated) ||
  1323. (q->bufs[buffer->index]->memory != buffer->memory)) {
  1324. return -EINVAL;
  1325. }
  1326. if (V4L2_MEMORY_USERPTR == buffer->memory) {
  1327. if ((buffer->length < vout->pix.sizeimage) ||
  1328. (0 == buffer->m.userptr)) {
  1329. return -EINVAL;
  1330. }
  1331. }
  1332. if ((is_rotation_enabled(vout)) &&
  1333. vout->vrfb_dma_tx.req_status == DMA_CHAN_NOT_ALLOTED) {
  1334. v4l2_warn(&vout->vid_dev->v4l2_dev,
  1335. "DMA Channel not allocated for Rotation\n");
  1336. return -EINVAL;
  1337. }
  1338. return videobuf_qbuf(q, buffer);
  1339. }
  1340. static int vidioc_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
  1341. {
  1342. struct omap_vout_device *vout = fh;
  1343. struct videobuf_queue *q = &vout->vbq;
  1344. int ret;
  1345. u32 addr;
  1346. unsigned long size;
  1347. struct videobuf_buffer *vb;
  1348. vb = q->bufs[b->index];
  1349. if (!vout->streaming)
  1350. return -EINVAL;
  1351. if (file->f_flags & O_NONBLOCK)
  1352. /* Call videobuf_dqbuf for non blocking mode */
  1353. ret = videobuf_dqbuf(q, (struct v4l2_buffer *)b, 1);
  1354. else
  1355. /* Call videobuf_dqbuf for blocking mode */
  1356. ret = videobuf_dqbuf(q, (struct v4l2_buffer *)b, 0);
  1357. addr = (unsigned long) vout->buf_phy_addr[vb->i];
  1358. size = (unsigned long) vb->size;
  1359. dma_unmap_single(vout->vid_dev->v4l2_dev.dev, addr,
  1360. size, DMA_TO_DEVICE);
  1361. return ret;
  1362. }
  1363. static int vidioc_streamon(struct file *file, void *fh, enum v4l2_buf_type i)
  1364. {
  1365. int ret = 0, j;
  1366. u32 addr = 0, mask = 0;
  1367. struct omap_vout_device *vout = fh;
  1368. struct videobuf_queue *q = &vout->vbq;
  1369. struct omapvideo_info *ovid = &vout->vid_info;
  1370. mutex_lock(&vout->lock);
  1371. if (vout->streaming) {
  1372. ret = -EBUSY;
  1373. goto streamon_err;
  1374. }
  1375. ret = videobuf_streamon(q);
  1376. if (ret)
  1377. goto streamon_err;
  1378. if (list_empty(&vout->dma_queue)) {
  1379. ret = -EIO;
  1380. goto streamon_err1;
  1381. }
  1382. /* Get the next frame from the buffer queue */
  1383. vout->next_frm = vout->cur_frm = list_entry(vout->dma_queue.next,
  1384. struct videobuf_buffer, queue);
  1385. /* Remove buffer from the buffer queue */
  1386. list_del(&vout->cur_frm->queue);
  1387. /* Mark state of the current frame to active */
  1388. vout->cur_frm->state = VIDEOBUF_ACTIVE;
  1389. /* Initialize field_id and started member */
  1390. vout->field_id = 0;
  1391. /* set flag here. Next QBUF will start DMA */
  1392. vout->streaming = 1;
  1393. vout->first_int = 1;
  1394. if (omap_vout_calculate_offset(vout)) {
  1395. ret = -EINVAL;
  1396. goto streamon_err1;
  1397. }
  1398. addr = (unsigned long) vout->queued_buf_addr[vout->cur_frm->i]
  1399. + vout->cropped_offset;
  1400. mask = DISPC_IRQ_VSYNC | DISPC_IRQ_EVSYNC_EVEN | DISPC_IRQ_EVSYNC_ODD
  1401. | DISPC_IRQ_VSYNC2;
  1402. omap_dispc_register_isr(omap_vout_isr, vout, mask);
  1403. for (j = 0; j < ovid->num_overlays; j++) {
  1404. struct omap_overlay *ovl = ovid->overlays[j];
  1405. if (ovl->manager && ovl->manager->device) {
  1406. struct omap_overlay_info info;
  1407. ovl->get_overlay_info(ovl, &info);
  1408. info.enabled = 1;
  1409. info.paddr = addr;
  1410. if (ovl->set_overlay_info(ovl, &info)) {
  1411. ret = -EINVAL;
  1412. goto streamon_err1;
  1413. }
  1414. }
  1415. }
  1416. /* First save the configuration in ovelray structure */
  1417. ret = omapvid_init(vout, addr);
  1418. if (ret)
  1419. v4l2_err(&vout->vid_dev->v4l2_dev,
  1420. "failed to set overlay info\n");
  1421. /* Enable the pipeline and set the Go bit */
  1422. ret = omapvid_apply_changes(vout);
  1423. if (ret)
  1424. v4l2_err(&vout->vid_dev->v4l2_dev, "failed to change mode\n");
  1425. ret = 0;
  1426. streamon_err1:
  1427. if (ret)
  1428. ret = videobuf_streamoff(q);
  1429. streamon_err:
  1430. mutex_unlock(&vout->lock);
  1431. return ret;
  1432. }
  1433. static int vidioc_streamoff(struct file *file, void *fh, enum v4l2_buf_type i)
  1434. {
  1435. u32 mask = 0;
  1436. int ret = 0, j;
  1437. struct omap_vout_device *vout = fh;
  1438. struct omapvideo_info *ovid = &vout->vid_info;
  1439. if (!vout->streaming)
  1440. return -EINVAL;
  1441. vout->streaming = 0;
  1442. mask = DISPC_IRQ_VSYNC | DISPC_IRQ_EVSYNC_EVEN | DISPC_IRQ_EVSYNC_ODD
  1443. | DISPC_IRQ_VSYNC2;
  1444. omap_dispc_unregister_isr(omap_vout_isr, vout, mask);
  1445. for (j = 0; j < ovid->num_overlays; j++) {
  1446. struct omap_overlay *ovl = ovid->overlays[j];
  1447. if (ovl->manager && ovl->manager->device) {
  1448. struct omap_overlay_info info;
  1449. ovl->get_overlay_info(ovl, &info);
  1450. info.enabled = 0;
  1451. ret = ovl->set_overlay_info(ovl, &info);
  1452. if (ret)
  1453. v4l2_err(&vout->vid_dev->v4l2_dev,
  1454. "failed to update overlay info in streamoff\n");
  1455. }
  1456. }
  1457. /* Turn of the pipeline */
  1458. ret = omapvid_apply_changes(vout);
  1459. if (ret)
  1460. v4l2_err(&vout->vid_dev->v4l2_dev, "failed to change mode in"
  1461. " streamoff\n");
  1462. INIT_LIST_HEAD(&vout->dma_queue);
  1463. ret = videobuf_streamoff(&vout->vbq);
  1464. return ret;
  1465. }
  1466. static int vidioc_s_fbuf(struct file *file, void *fh,
  1467. struct v4l2_framebuffer *a)
  1468. {
  1469. int enable = 0;
  1470. struct omap_overlay *ovl;
  1471. struct omapvideo_info *ovid;
  1472. struct omap_vout_device *vout = fh;
  1473. struct omap_overlay_manager_info info;
  1474. enum omap_dss_trans_key_type key_type = OMAP_DSS_COLOR_KEY_GFX_DST;
  1475. ovid = &vout->vid_info;
  1476. ovl = ovid->overlays[0];
  1477. /* OMAP DSS doesn't support Source and Destination color
  1478. key together */
  1479. if ((a->flags & V4L2_FBUF_FLAG_SRC_CHROMAKEY) &&
  1480. (a->flags & V4L2_FBUF_FLAG_CHROMAKEY))
  1481. return -EINVAL;
  1482. /* OMAP DSS Doesn't support the Destination color key
  1483. and alpha blending together */
  1484. if ((a->flags & V4L2_FBUF_FLAG_CHROMAKEY) &&
  1485. (a->flags & V4L2_FBUF_FLAG_LOCAL_ALPHA))
  1486. return -EINVAL;
  1487. if ((a->flags & V4L2_FBUF_FLAG_SRC_CHROMAKEY)) {
  1488. vout->fbuf.flags |= V4L2_FBUF_FLAG_SRC_CHROMAKEY;
  1489. key_type = OMAP_DSS_COLOR_KEY_VID_SRC;
  1490. } else
  1491. vout->fbuf.flags &= ~V4L2_FBUF_FLAG_SRC_CHROMAKEY;
  1492. if ((a->flags & V4L2_FBUF_FLAG_CHROMAKEY)) {
  1493. vout->fbuf.flags |= V4L2_FBUF_FLAG_CHROMAKEY;
  1494. key_type = OMAP_DSS_COLOR_KEY_GFX_DST;
  1495. } else
  1496. vout->fbuf.flags &= ~V4L2_FBUF_FLAG_CHROMAKEY;
  1497. if (a->flags & (V4L2_FBUF_FLAG_CHROMAKEY |
  1498. V4L2_FBUF_FLAG_SRC_CHROMAKEY))
  1499. enable = 1;
  1500. else
  1501. enable = 0;
  1502. if (ovl->manager && ovl->manager->get_manager_info &&
  1503. ovl->manager->set_manager_info) {
  1504. ovl->manager->get_manager_info(ovl->manager, &info);
  1505. info.trans_enabled = enable;
  1506. info.trans_key_type = key_type;
  1507. info.trans_key = vout->win.chromakey;
  1508. if (ovl->manager->set_manager_info(ovl->manager, &info))
  1509. return -EINVAL;
  1510. }
  1511. if (a->flags & V4L2_FBUF_FLAG_LOCAL_ALPHA) {
  1512. vout->fbuf.flags |= V4L2_FBUF_FLAG_LOCAL_ALPHA;
  1513. enable = 1;
  1514. } else {
  1515. vout->fbuf.flags &= ~V4L2_FBUF_FLAG_LOCAL_ALPHA;
  1516. enable = 0;
  1517. }
  1518. if (ovl->manager && ovl->manager->get_manager_info &&
  1519. ovl->manager->set_manager_info) {
  1520. ovl->manager->get_manager_info(ovl->manager, &info);
  1521. /* enable this only if there is no zorder cap */
  1522. if ((ovl->caps & OMAP_DSS_OVL_CAP_ZORDER) == 0)
  1523. info.partial_alpha_enabled = enable;
  1524. if (ovl->manager->set_manager_info(ovl->manager, &info))
  1525. return -EINVAL;
  1526. }
  1527. return 0;
  1528. }
  1529. static int vidioc_g_fbuf(struct file *file, void *fh,
  1530. struct v4l2_framebuffer *a)
  1531. {
  1532. struct omap_overlay *ovl;
  1533. struct omapvideo_info *ovid;
  1534. struct omap_vout_device *vout = fh;
  1535. struct omap_overlay_manager_info info;
  1536. ovid = &vout->vid_info;
  1537. ovl = ovid->overlays[0];
  1538. a->flags = 0x0;
  1539. a->capability = V4L2_FBUF_CAP_LOCAL_ALPHA | V4L2_FBUF_CAP_CHROMAKEY
  1540. | V4L2_FBUF_CAP_SRC_CHROMAKEY;
  1541. if (ovl->manager && ovl->manager->get_manager_info) {
  1542. ovl->manager->get_manager_info(ovl->manager, &info);
  1543. if (info.trans_key_type == OMAP_DSS_COLOR_KEY_VID_SRC)
  1544. a->flags |= V4L2_FBUF_FLAG_SRC_CHROMAKEY;
  1545. if (info.trans_key_type == OMAP_DSS_COLOR_KEY_GFX_DST)
  1546. a->flags |= V4L2_FBUF_FLAG_CHROMAKEY;
  1547. }
  1548. if (ovl->manager && ovl->manager->get_manager_info) {
  1549. ovl->manager->get_manager_info(ovl->manager, &info);
  1550. if (info.partial_alpha_enabled)
  1551. a->flags |= V4L2_FBUF_FLAG_LOCAL_ALPHA;
  1552. }
  1553. return 0;
  1554. }
  1555. static const struct v4l2_ioctl_ops vout_ioctl_ops = {
  1556. .vidioc_querycap = vidioc_querycap,
  1557. .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
  1558. .vidioc_g_fmt_vid_out = vidioc_g_fmt_vid_out,
  1559. .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out,
  1560. .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
  1561. .vidioc_queryctrl = vidioc_queryctrl,
  1562. .vidioc_g_ctrl = vidioc_g_ctrl,
  1563. .vidioc_s_fbuf = vidioc_s_fbuf,
  1564. .vidioc_g_fbuf = vidioc_g_fbuf,
  1565. .vidioc_s_ctrl = vidioc_s_ctrl,
  1566. .vidioc_try_fmt_vid_overlay = vidioc_try_fmt_vid_overlay,
  1567. .vidioc_s_fmt_vid_overlay = vidioc_s_fmt_vid_overlay,
  1568. .vidioc_enum_fmt_vid_overlay = vidioc_enum_fmt_vid_overlay,
  1569. .vidioc_g_fmt_vid_overlay = vidioc_g_fmt_vid_overlay,
  1570. .vidioc_cropcap = vidioc_cropcap,
  1571. .vidioc_g_crop = vidioc_g_crop,
  1572. .vidioc_s_crop = vidioc_s_crop,
  1573. .vidioc_reqbufs = vidioc_reqbufs,
  1574. .vidioc_querybuf = vidioc_querybuf,
  1575. .vidioc_qbuf = vidioc_qbuf,
  1576. .vidioc_dqbuf = vidioc_dqbuf,
  1577. .vidioc_streamon = vidioc_streamon,
  1578. .vidioc_streamoff = vidioc_streamoff,
  1579. };
  1580. static const struct v4l2_file_operations omap_vout_fops = {
  1581. .owner = THIS_MODULE,
  1582. .poll = omap_vout_poll,
  1583. .unlocked_ioctl = video_ioctl2,
  1584. .mmap = omap_vout_mmap,
  1585. .open = omap_vout_open,
  1586. .release = omap_vout_release,
  1587. };
  1588. /* Init functions used during driver initialization */
  1589. /* Initial setup of video_data */
  1590. static int __init omap_vout_setup_video_data(struct omap_vout_device *vout)
  1591. {
  1592. struct video_device *vfd;
  1593. struct v4l2_pix_format *pix;
  1594. struct v4l2_control *control;
  1595. struct omap_dss_device *display =
  1596. vout->vid_info.overlays[0]->manager->device;
  1597. /* set the default pix */
  1598. pix = &vout->pix;
  1599. /* Set the default picture of QVGA */
  1600. pix->width = QQVGA_WIDTH;
  1601. pix->height = QQVGA_HEIGHT;
  1602. /* Default pixel format is RGB 5-6-5 */
  1603. pix->pixelformat = V4L2_PIX_FMT_RGB565;
  1604. pix->field = V4L2_FIELD_ANY;
  1605. pix->bytesperline = pix->width * 2;
  1606. pix->sizeimage = pix->bytesperline * pix->height;
  1607. pix->priv = 0;
  1608. pix->colorspace = V4L2_COLORSPACE_JPEG;
  1609. vout->bpp = RGB565_BPP;
  1610. vout->fbuf.fmt.width = display->panel.timings.x_res;
  1611. vout->fbuf.fmt.height = display->panel.timings.y_res;
  1612. /* Set the data structures for the overlay parameters*/
  1613. vout->win.global_alpha = 255;
  1614. vout->fbuf.flags = 0;
  1615. vout->fbuf.capability = V4L2_FBUF_CAP_LOCAL_ALPHA |
  1616. V4L2_FBUF_CAP_SRC_CHROMAKEY | V4L2_FBUF_CAP_CHROMAKEY;
  1617. vout->win.chromakey = 0;
  1618. omap_vout_new_format(pix, &vout->fbuf, &vout->crop, &vout->win);
  1619. /*Initialize the control variables for
  1620. rotation, flipping and background color. */
  1621. control = vout->control;
  1622. control[0].id = V4L2_CID_ROTATE;
  1623. control[0].value = 0;
  1624. vout->rotation = 0;
  1625. vout->mirror = 0;
  1626. vout->control[2].id = V4L2_CID_HFLIP;
  1627. vout->control[2].value = 0;
  1628. if (vout->vid_info.rotation_type == VOUT_ROT_VRFB)
  1629. vout->vrfb_bpp = 2;
  1630. control[1].id = V4L2_CID_BG_COLOR;
  1631. control[1].value = 0;
  1632. /* initialize the video_device struct */
  1633. vfd = vout->vfd = video_device_alloc();
  1634. if (!vfd) {
  1635. printk(KERN_ERR VOUT_NAME ": could not allocate"
  1636. " video device struct\n");
  1637. return -ENOMEM;
  1638. }
  1639. vfd->release = video_device_release;
  1640. vfd->ioctl_ops = &vout_ioctl_ops;
  1641. strlcpy(vfd->name, VOUT_NAME, sizeof(vfd->name));
  1642. vfd->fops = &omap_vout_fops;
  1643. vfd->v4l2_dev = &vout->vid_dev->v4l2_dev;
  1644. mutex_init(&vout->lock);
  1645. vfd->minor = -1;
  1646. return 0;
  1647. }
  1648. /* Setup video buffers */
  1649. static int __init omap_vout_setup_video_bufs(struct platform_device *pdev,
  1650. int vid_num)
  1651. {
  1652. u32 numbuffers;
  1653. int ret = 0, i;
  1654. struct omapvideo_info *ovid;
  1655. struct omap_vout_device *vout;
  1656. struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
  1657. struct omap2video_device *vid_dev =
  1658. container_of(v4l2_dev, struct omap2video_device, v4l2_dev);
  1659. vout = vid_dev->vouts[vid_num];
  1660. ovid = &vout->vid_info;
  1661. numbuffers = (vid_num == 0) ? video1_numbuffers : video2_numbuffers;
  1662. vout->buffer_size = (vid_num == 0) ? video1_bufsize : video2_bufsize;
  1663. dev_info(&pdev->dev, "Buffer Size = %d\n", vout->buffer_size);
  1664. for (i = 0; i < numbuffers; i++) {
  1665. vout->buf_virt_addr[i] =
  1666. omap_vout_alloc_buffer(vout->buffer_size,
  1667. (u32 *) &vout->buf_phy_addr[i]);
  1668. if (!vout->buf_virt_addr[i]) {
  1669. numbuffers = i;
  1670. ret = -ENOMEM;
  1671. goto free_buffers;
  1672. }
  1673. }
  1674. vout->cropped_offset = 0;
  1675. if (ovid->rotation_type == VOUT_ROT_VRFB) {
  1676. int static_vrfb_allocation = (vid_num == 0) ?
  1677. vid1_static_vrfb_alloc : vid2_static_vrfb_alloc;
  1678. ret = omap_vout_setup_vrfb_bufs(pdev, vid_num,
  1679. static_vrfb_allocation);
  1680. }
  1681. return ret;
  1682. free_buffers:
  1683. for (i = 0; i < numbuffers; i++) {
  1684. omap_vout_free_buffer(vout->buf_virt_addr[i],
  1685. vout->buffer_size);
  1686. vout->buf_virt_addr[i] = 0;
  1687. vout->buf_phy_addr[i] = 0;
  1688. }
  1689. return ret;
  1690. }
  1691. /* Create video out devices */
  1692. static int __init omap_vout_create_video_devices(struct platform_device *pdev)
  1693. {
  1694. int ret = 0, k;
  1695. struct omap_vout_device *vout;
  1696. struct video_device *vfd = NULL;
  1697. struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
  1698. struct omap2video_device *vid_dev = container_of(v4l2_dev,
  1699. struct omap2video_device, v4l2_dev);
  1700. for (k = 0; k < pdev->num_resources; k++) {
  1701. vout = kzalloc(sizeof(struct omap_vout_device), GFP_KERNEL);
  1702. if (!vout) {
  1703. dev_err(&pdev->dev, ": could not allocate memory\n");
  1704. return -ENOMEM;
  1705. }
  1706. vout->vid = k;
  1707. vid_dev->vouts[k] = vout;
  1708. vout->vid_dev = vid_dev;
  1709. /* Select video2 if only 1 overlay is controlled by V4L2 */
  1710. if (pdev->num_resources == 1)
  1711. vout->vid_info.overlays[0] = vid_dev->overlays[k + 2];
  1712. else
  1713. /* Else select video1 and video2 one by one. */
  1714. vout->vid_info.overlays[0] = vid_dev->overlays[k + 1];
  1715. vout->vid_info.num_overlays = 1;
  1716. vout->vid_info.id = k + 1;
  1717. /* Set VRFB as rotation_type for omap2 and omap3 */
  1718. if (cpu_is_omap24xx() || cpu_is_omap34xx())
  1719. vout->vid_info.rotation_type = VOUT_ROT_VRFB;
  1720. /* Setup the default configuration for the video devices
  1721. */
  1722. if (omap_vout_setup_video_data(vout) != 0) {
  1723. ret = -ENOMEM;
  1724. goto error;
  1725. }
  1726. /* Allocate default number of buffers for the video streaming
  1727. * and reserve the VRFB space for rotation
  1728. */
  1729. if (omap_vout_setup_video_bufs(pdev, k) != 0) {
  1730. ret = -ENOMEM;
  1731. goto error1;
  1732. }
  1733. /* Register the Video device with V4L2
  1734. */
  1735. vfd = vout->vfd;
  1736. if (video_register_device(vfd, VFL_TYPE_GRABBER, -1) < 0) {
  1737. dev_err(&pdev->dev, ": Could not register "
  1738. "Video for Linux device\n");
  1739. vfd->minor = -1;
  1740. ret = -ENODEV;
  1741. goto error2;
  1742. }
  1743. video_set_drvdata(vfd, vout);
  1744. /* Configure the overlay structure */
  1745. ret = omapvid_init(vid_dev->vouts[k], 0);
  1746. if (!ret)
  1747. goto success;
  1748. error2:
  1749. if (vout->vid_info.rotation_type == VOUT_ROT_VRFB)
  1750. omap_vout_release_vrfb(vout);
  1751. omap_vout_free_buffers(vout);
  1752. error1:
  1753. video_device_release(vfd);
  1754. error:
  1755. kfree(vout);
  1756. return ret;
  1757. success:
  1758. dev_info(&pdev->dev, ": registered and initialized"
  1759. " video device %d\n", vfd->minor);
  1760. if (k == (pdev->num_resources - 1))
  1761. return 0;
  1762. }
  1763. return -ENODEV;
  1764. }
  1765. /* Driver functions */
  1766. static void omap_vout_cleanup_device(struct omap_vout_device *vout)
  1767. {
  1768. struct video_device *vfd;
  1769. struct omapvideo_info *ovid;
  1770. if (!vout)
  1771. return;
  1772. vfd = vout->vfd;
  1773. ovid = &vout->vid_info;
  1774. if (vfd) {
  1775. if (!video_is_registered(vfd)) {
  1776. /*
  1777. * The device was never registered, so release the
  1778. * video_device struct directly.
  1779. */
  1780. video_device_release(vfd);
  1781. } else {
  1782. /*
  1783. * The unregister function will release the video_device
  1784. * struct as well as unregistering it.
  1785. */
  1786. video_unregister_device(vfd);
  1787. }
  1788. }
  1789. if (ovid->rotation_type == VOUT_ROT_VRFB) {
  1790. omap_vout_release_vrfb(vout);
  1791. /* Free the VRFB buffer if allocated
  1792. * init time
  1793. */
  1794. if (vout->vrfb_static_allocation)
  1795. omap_vout_free_vrfb_buffers(vout);
  1796. }
  1797. omap_vout_free_buffers(vout);
  1798. kfree(vout);
  1799. }
  1800. static int omap_vout_remove(struct platform_device *pdev)
  1801. {
  1802. int k;
  1803. struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
  1804. struct omap2video_device *vid_dev = container_of(v4l2_dev, struct
  1805. omap2video_device, v4l2_dev);
  1806. v4l2_device_unregister(v4l2_dev);
  1807. for (k = 0; k < pdev->num_resources; k++)
  1808. omap_vout_cleanup_device(vid_dev->vouts[k]);
  1809. for (k = 0; k < vid_dev->num_displays; k++) {
  1810. if (vid_dev->displays[k]->state != OMAP_DSS_DISPLAY_DISABLED)
  1811. vid_dev->displays[k]->driver->disable(vid_dev->displays[k]);
  1812. omap_dss_put_device(vid_dev->displays[k]);
  1813. }
  1814. kfree(vid_dev);
  1815. return 0;
  1816. }
  1817. static int __init omap_vout_probe(struct platform_device *pdev)
  1818. {
  1819. int ret = 0, i;
  1820. struct omap_overlay *ovl;
  1821. struct omap_dss_device *dssdev = NULL;
  1822. struct omap_dss_device *def_display;
  1823. struct omap2video_device *vid_dev = NULL;
  1824. if (pdev->num_resources == 0) {
  1825. dev_err(&pdev->dev, "probed for an unknown device\n");
  1826. return -ENODEV;
  1827. }
  1828. vid_dev = kzalloc(sizeof(struct omap2video_device), GFP_KERNEL);
  1829. if (vid_dev == NULL)
  1830. return -ENOMEM;
  1831. vid_dev->num_displays = 0;
  1832. for_each_dss_dev(dssdev) {
  1833. omap_dss_get_device(dssdev);
  1834. if (!dssdev->driver) {
  1835. dev_warn(&pdev->dev, "no driver for display: %s\n",
  1836. dssdev->name);
  1837. omap_dss_put_device(dssdev);
  1838. continue;
  1839. }
  1840. vid_dev->displays[vid_dev->num_displays++] = dssdev;
  1841. }
  1842. if (vid_dev->num_displays == 0) {
  1843. dev_err(&pdev->dev, "no displays\n");
  1844. ret = -EINVAL;
  1845. goto probe_err0;
  1846. }
  1847. vid_dev->num_overlays = omap_dss_get_num_overlays();
  1848. for (i = 0; i < vid_dev->num_overlays; i++)
  1849. vid_dev->overlays[i] = omap_dss_get_overlay(i);
  1850. vid_dev->num_managers = omap_dss_get_num_overlay_managers();
  1851. for (i = 0; i < vid_dev->num_managers; i++)
  1852. vid_dev->managers[i] = omap_dss_get_overlay_manager(i);
  1853. /* Get the Video1 overlay and video2 overlay.
  1854. * Setup the Display attached to that overlays
  1855. */
  1856. for (i = 1; i < vid_dev->num_overlays; i++) {
  1857. ovl = omap_dss_get_overlay(i);
  1858. if (ovl->manager && ovl->manager->device) {
  1859. def_display = ovl->manager->device;
  1860. } else {
  1861. dev_warn(&pdev->dev, "cannot find display\n");
  1862. def_display = NULL;
  1863. }
  1864. if (def_display) {
  1865. struct omap_dss_driver *dssdrv = def_display->driver;
  1866. ret = dssdrv->enable(def_display);
  1867. if (ret) {
  1868. /* Here we are not considering a error
  1869. * as display may be enabled by frame
  1870. * buffer driver
  1871. */
  1872. dev_warn(&pdev->dev,
  1873. "'%s' Display already enabled\n",
  1874. def_display->name);
  1875. }
  1876. }
  1877. }
  1878. if (v4l2_device_register(&pdev->dev, &vid_dev->v4l2_dev) < 0) {
  1879. dev_err(&pdev->dev, "v4l2_device_register failed\n");
  1880. ret = -ENODEV;
  1881. goto probe_err1;
  1882. }
  1883. ret = omap_vout_create_video_devices(pdev);
  1884. if (ret)
  1885. goto probe_err2;
  1886. for (i = 0; i < vid_dev->num_displays; i++) {
  1887. struct omap_dss_device *display = vid_dev->displays[i];
  1888. if (display->driver->update)
  1889. display->driver->update(display, 0, 0,
  1890. display->panel.timings.x_res,
  1891. display->panel.timings.y_res);
  1892. }
  1893. return 0;
  1894. probe_err2:
  1895. v4l2_device_unregister(&vid_dev->v4l2_dev);
  1896. probe_err1:
  1897. for (i = 1; i < vid_dev->num_overlays; i++) {
  1898. def_display = NULL;
  1899. ovl = omap_dss_get_overlay(i);
  1900. if (ovl->manager && ovl->manager->device)
  1901. def_display = ovl->manager->device;
  1902. if (def_display && def_display->driver)
  1903. def_display->driver->disable(def_display);
  1904. }
  1905. probe_err0:
  1906. kfree(vid_dev);
  1907. return ret;
  1908. }
  1909. static struct platform_driver omap_vout_driver = {
  1910. .driver = {
  1911. .name = VOUT_NAME,
  1912. },
  1913. .probe = omap_vout_probe,
  1914. .remove = omap_vout_remove,
  1915. };
  1916. static int __init omap_vout_init(void)
  1917. {
  1918. if (platform_driver_register(&omap_vout_driver) != 0) {
  1919. printk(KERN_ERR VOUT_NAME ":Could not register Video driver\n");
  1920. return -EINVAL;
  1921. }
  1922. return 0;
  1923. }
  1924. static void omap_vout_cleanup(void)
  1925. {
  1926. platform_driver_unregister(&omap_vout_driver);
  1927. }
  1928. late_initcall(omap_vout_init);
  1929. module_exit(omap_vout_cleanup);