omap_vout.c 66 KB

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