omap_vout.c 56 KB

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