vpbe_display.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897
  1. /*
  2. * Copyright (C) 2010 Texas Instruments Incorporated - http://www.ti.com/
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation version 2.
  7. *
  8. * This program is distributed WITHOUT ANY WARRANTY of any
  9. * kind, whether express or implied; without even the implied warranty
  10. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/errno.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/string.h>
  19. #include <linux/wait.h>
  20. #include <linux/time.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/irq.h>
  23. #include <linux/mm.h>
  24. #include <linux/mutex.h>
  25. #include <linux/videodev2.h>
  26. #include <linux/slab.h>
  27. #include <asm/pgtable.h>
  28. #include <mach/cputype.h>
  29. #include <media/v4l2-dev.h>
  30. #include <media/v4l2-common.h>
  31. #include <media/v4l2-ioctl.h>
  32. #include <media/v4l2-device.h>
  33. #include <media/davinci/vpbe_display.h>
  34. #include <media/davinci/vpbe_types.h>
  35. #include <media/davinci/vpbe.h>
  36. #include <media/davinci/vpbe_venc.h>
  37. #include <media/davinci/vpbe_osd.h>
  38. #include "vpbe_venc_regs.h"
  39. #define VPBE_DISPLAY_DRIVER "vpbe-v4l2"
  40. static int debug;
  41. #define VPBE_DEFAULT_NUM_BUFS 3
  42. module_param(debug, int, 0644);
  43. static int vpbe_set_osd_display_params(struct vpbe_display *disp_dev,
  44. struct vpbe_layer *layer);
  45. static int venc_is_second_field(struct vpbe_display *disp_dev)
  46. {
  47. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  48. int ret;
  49. int val;
  50. ret = v4l2_subdev_call(vpbe_dev->venc,
  51. core,
  52. ioctl,
  53. VENC_GET_FLD,
  54. &val);
  55. if (ret < 0) {
  56. v4l2_err(&vpbe_dev->v4l2_dev,
  57. "Error in getting Field ID 0\n");
  58. }
  59. return val;
  60. }
  61. static void vpbe_isr_even_field(struct vpbe_display *disp_obj,
  62. struct vpbe_layer *layer)
  63. {
  64. struct timespec timevalue;
  65. if (layer->cur_frm == layer->next_frm)
  66. return;
  67. ktime_get_ts(&timevalue);
  68. layer->cur_frm->vb.v4l2_buf.timestamp.tv_sec =
  69. timevalue.tv_sec;
  70. layer->cur_frm->vb.v4l2_buf.timestamp.tv_usec =
  71. timevalue.tv_nsec / NSEC_PER_USEC;
  72. vb2_buffer_done(&layer->cur_frm->vb, VB2_BUF_STATE_DONE);
  73. /* Make cur_frm pointing to next_frm */
  74. layer->cur_frm = layer->next_frm;
  75. }
  76. static void vpbe_isr_odd_field(struct vpbe_display *disp_obj,
  77. struct vpbe_layer *layer)
  78. {
  79. struct osd_state *osd_device = disp_obj->osd_device;
  80. unsigned long addr;
  81. spin_lock(&disp_obj->dma_queue_lock);
  82. if (list_empty(&layer->dma_queue) ||
  83. (layer->cur_frm != layer->next_frm)) {
  84. spin_unlock(&disp_obj->dma_queue_lock);
  85. return;
  86. }
  87. /*
  88. * one field is displayed configure
  89. * the next frame if it is available
  90. * otherwise hold on current frame
  91. * Get next from the buffer queue
  92. */
  93. layer->next_frm = list_entry(layer->dma_queue.next,
  94. struct vpbe_disp_buffer, list);
  95. /* Remove that from the buffer queue */
  96. list_del(&layer->next_frm->list);
  97. spin_unlock(&disp_obj->dma_queue_lock);
  98. /* Mark state of the frame to active */
  99. layer->next_frm->vb.state = VB2_BUF_STATE_ACTIVE;
  100. addr = vb2_dma_contig_plane_dma_addr(&layer->next_frm->vb, 0);
  101. osd_device->ops.start_layer(osd_device,
  102. layer->layer_info.id,
  103. addr,
  104. disp_obj->cbcr_ofst);
  105. }
  106. /* interrupt service routine */
  107. static irqreturn_t venc_isr(int irq, void *arg)
  108. {
  109. struct vpbe_display *disp_dev = (struct vpbe_display *)arg;
  110. struct vpbe_layer *layer;
  111. static unsigned last_event;
  112. unsigned event = 0;
  113. int fid;
  114. int i;
  115. if ((NULL == arg) || (NULL == disp_dev->dev[0]))
  116. return IRQ_HANDLED;
  117. if (venc_is_second_field(disp_dev))
  118. event |= VENC_SECOND_FIELD;
  119. else
  120. event |= VENC_FIRST_FIELD;
  121. if (event == (last_event & ~VENC_END_OF_FRAME)) {
  122. /*
  123. * If the display is non-interlaced, then we need to flag the
  124. * end-of-frame event at every interrupt regardless of the
  125. * value of the FIDST bit. We can conclude that the display is
  126. * non-interlaced if the value of the FIDST bit is unchanged
  127. * from the previous interrupt.
  128. */
  129. event |= VENC_END_OF_FRAME;
  130. } else if (event == VENC_SECOND_FIELD) {
  131. /* end-of-frame for interlaced display */
  132. event |= VENC_END_OF_FRAME;
  133. }
  134. last_event = event;
  135. for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
  136. layer = disp_dev->dev[i];
  137. /* If streaming is started in this layer */
  138. if (!layer->started)
  139. continue;
  140. if (layer->layer_first_int) {
  141. layer->layer_first_int = 0;
  142. continue;
  143. }
  144. /* Check the field format */
  145. if ((V4L2_FIELD_NONE == layer->pix_fmt.field) &&
  146. (event & VENC_END_OF_FRAME)) {
  147. /* Progressive mode */
  148. vpbe_isr_even_field(disp_dev, layer);
  149. vpbe_isr_odd_field(disp_dev, layer);
  150. } else {
  151. /* Interlaced mode */
  152. layer->field_id ^= 1;
  153. if (event & VENC_FIRST_FIELD)
  154. fid = 0;
  155. else
  156. fid = 1;
  157. /*
  158. * If field id does not match with store
  159. * field id
  160. */
  161. if (fid != layer->field_id) {
  162. /* Make them in sync */
  163. layer->field_id = fid;
  164. continue;
  165. }
  166. /*
  167. * device field id and local field id are
  168. * in sync. If this is even field
  169. */
  170. if (0 == fid)
  171. vpbe_isr_even_field(disp_dev, layer);
  172. else /* odd field */
  173. vpbe_isr_odd_field(disp_dev, layer);
  174. }
  175. }
  176. return IRQ_HANDLED;
  177. }
  178. /*
  179. * vpbe_buffer_prepare()
  180. * This is the callback function called from vb2_qbuf() function
  181. * the buffer is prepared and user space virtual address is converted into
  182. * physical address
  183. */
  184. static int vpbe_buffer_prepare(struct vb2_buffer *vb)
  185. {
  186. struct vpbe_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
  187. struct vb2_queue *q = vb->vb2_queue;
  188. struct vpbe_layer *layer = fh->layer;
  189. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  190. unsigned long addr;
  191. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  192. "vpbe_buffer_prepare\n");
  193. if (vb->state != VB2_BUF_STATE_ACTIVE &&
  194. vb->state != VB2_BUF_STATE_PREPARED) {
  195. vb2_set_plane_payload(vb, 0, layer->pix_fmt.sizeimage);
  196. if (vb2_plane_vaddr(vb, 0) &&
  197. vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
  198. return -EINVAL;
  199. addr = vb2_dma_contig_plane_dma_addr(vb, 0);
  200. if (q->streaming) {
  201. if (!IS_ALIGNED(addr, 8)) {
  202. v4l2_err(&vpbe_dev->v4l2_dev,
  203. "buffer_prepare:offset is \
  204. not aligned to 32 bytes\n");
  205. return -EINVAL;
  206. }
  207. }
  208. }
  209. return 0;
  210. }
  211. /*
  212. * vpbe_buffer_setup()
  213. * This function allocates memory for the buffers
  214. */
  215. static int
  216. vpbe_buffer_queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
  217. unsigned int *nbuffers, unsigned int *nplanes,
  218. unsigned int sizes[], void *alloc_ctxs[])
  219. {
  220. /* Get the file handle object and layer object */
  221. struct vpbe_fh *fh = vb2_get_drv_priv(vq);
  222. struct vpbe_layer *layer = fh->layer;
  223. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  224. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_buffer_setup\n");
  225. /* Store number of buffers allocated in numbuffer member */
  226. if (*nbuffers < VPBE_DEFAULT_NUM_BUFS)
  227. *nbuffers = layer->numbuffers = VPBE_DEFAULT_NUM_BUFS;
  228. *nplanes = 1;
  229. sizes[0] = layer->pix_fmt.sizeimage;
  230. alloc_ctxs[0] = layer->alloc_ctx;
  231. return 0;
  232. }
  233. /*
  234. * vpbe_buffer_queue()
  235. * This function adds the buffer to DMA queue
  236. */
  237. static void vpbe_buffer_queue(struct vb2_buffer *vb)
  238. {
  239. /* Get the file handle object and layer object */
  240. struct vpbe_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
  241. struct vpbe_disp_buffer *buf = container_of(vb,
  242. struct vpbe_disp_buffer, vb);
  243. struct vpbe_layer *layer = fh->layer;
  244. struct vpbe_display *disp = fh->disp_dev;
  245. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  246. unsigned long flags;
  247. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  248. "vpbe_buffer_queue\n");
  249. /* add the buffer to the DMA queue */
  250. spin_lock_irqsave(&disp->dma_queue_lock, flags);
  251. list_add_tail(&buf->list, &layer->dma_queue);
  252. spin_unlock_irqrestore(&disp->dma_queue_lock, flags);
  253. }
  254. /*
  255. * vpbe_buf_cleanup()
  256. * This function is called from the vb2 layer to free memory allocated to
  257. * the buffers
  258. */
  259. static void vpbe_buf_cleanup(struct vb2_buffer *vb)
  260. {
  261. /* Get the file handle object and layer object */
  262. struct vpbe_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
  263. struct vpbe_layer *layer = fh->layer;
  264. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  265. struct vpbe_disp_buffer *buf = container_of(vb,
  266. struct vpbe_disp_buffer, vb);
  267. unsigned long flags;
  268. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  269. "vpbe_buf_cleanup\n");
  270. spin_lock_irqsave(&layer->irqlock, flags);
  271. if (vb->state == VB2_BUF_STATE_ACTIVE)
  272. list_del_init(&buf->list);
  273. spin_unlock_irqrestore(&layer->irqlock, flags);
  274. }
  275. static void vpbe_wait_prepare(struct vb2_queue *vq)
  276. {
  277. struct vpbe_fh *fh = vb2_get_drv_priv(vq);
  278. struct vpbe_layer *layer = fh->layer;
  279. mutex_unlock(&layer->opslock);
  280. }
  281. static void vpbe_wait_finish(struct vb2_queue *vq)
  282. {
  283. struct vpbe_fh *fh = vb2_get_drv_priv(vq);
  284. struct vpbe_layer *layer = fh->layer;
  285. mutex_lock(&layer->opslock);
  286. }
  287. static int vpbe_buffer_init(struct vb2_buffer *vb)
  288. {
  289. struct vpbe_disp_buffer *buf = container_of(vb,
  290. struct vpbe_disp_buffer, vb);
  291. INIT_LIST_HEAD(&buf->list);
  292. return 0;
  293. }
  294. static int vpbe_start_streaming(struct vb2_queue *vq, unsigned int count)
  295. {
  296. struct vpbe_fh *fh = vb2_get_drv_priv(vq);
  297. struct vpbe_layer *layer = fh->layer;
  298. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  299. int ret;
  300. /* If buffer queue is empty, return error */
  301. if (list_empty(&layer->dma_queue)) {
  302. v4l2_err(&vpbe_dev->v4l2_dev, "buffer queue is empty\n");
  303. return -EINVAL;
  304. }
  305. /* Get the next frame from the buffer queue */
  306. layer->next_frm = layer->cur_frm = list_entry(layer->dma_queue.next,
  307. struct vpbe_disp_buffer, list);
  308. /* Remove buffer from the buffer queue */
  309. list_del(&layer->cur_frm->list);
  310. /* Mark state of the current frame to active */
  311. layer->cur_frm->vb.state = VB2_BUF_STATE_ACTIVE;
  312. /* Initialize field_id and started member */
  313. layer->field_id = 0;
  314. /* Set parameters in OSD and VENC */
  315. ret = vpbe_set_osd_display_params(fh->disp_dev, layer);
  316. if (ret < 0)
  317. return ret;
  318. /*
  319. * if request format is yuv420 semiplanar, need to
  320. * enable both video windows
  321. */
  322. layer->started = 1;
  323. layer->layer_first_int = 1;
  324. return ret;
  325. }
  326. static int vpbe_stop_streaming(struct vb2_queue *vq)
  327. {
  328. struct vpbe_fh *fh = vb2_get_drv_priv(vq);
  329. struct vpbe_layer *layer = fh->layer;
  330. if (!vb2_is_streaming(vq))
  331. return 0;
  332. /* release all active buffers */
  333. while (!list_empty(&layer->dma_queue)) {
  334. layer->next_frm = list_entry(layer->dma_queue.next,
  335. struct vpbe_disp_buffer, list);
  336. list_del(&layer->next_frm->list);
  337. vb2_buffer_done(&layer->next_frm->vb, VB2_BUF_STATE_ERROR);
  338. }
  339. return 0;
  340. }
  341. static struct vb2_ops video_qops = {
  342. .queue_setup = vpbe_buffer_queue_setup,
  343. .wait_prepare = vpbe_wait_prepare,
  344. .wait_finish = vpbe_wait_finish,
  345. .buf_init = vpbe_buffer_init,
  346. .buf_prepare = vpbe_buffer_prepare,
  347. .start_streaming = vpbe_start_streaming,
  348. .stop_streaming = vpbe_stop_streaming,
  349. .buf_cleanup = vpbe_buf_cleanup,
  350. .buf_queue = vpbe_buffer_queue,
  351. };
  352. static
  353. struct vpbe_layer*
  354. _vpbe_display_get_other_win_layer(struct vpbe_display *disp_dev,
  355. struct vpbe_layer *layer)
  356. {
  357. enum vpbe_display_device_id thiswin, otherwin;
  358. thiswin = layer->device_id;
  359. otherwin = (thiswin == VPBE_DISPLAY_DEVICE_0) ?
  360. VPBE_DISPLAY_DEVICE_1 : VPBE_DISPLAY_DEVICE_0;
  361. return disp_dev->dev[otherwin];
  362. }
  363. static int vpbe_set_osd_display_params(struct vpbe_display *disp_dev,
  364. struct vpbe_layer *layer)
  365. {
  366. struct osd_layer_config *cfg = &layer->layer_info.config;
  367. struct osd_state *osd_device = disp_dev->osd_device;
  368. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  369. unsigned long addr;
  370. int ret;
  371. addr = vb2_dma_contig_plane_dma_addr(&layer->cur_frm->vb, 0);
  372. /* Set address in the display registers */
  373. osd_device->ops.start_layer(osd_device,
  374. layer->layer_info.id,
  375. addr,
  376. disp_dev->cbcr_ofst);
  377. ret = osd_device->ops.enable_layer(osd_device,
  378. layer->layer_info.id, 0);
  379. if (ret < 0) {
  380. v4l2_err(&vpbe_dev->v4l2_dev,
  381. "Error in enabling osd window layer 0\n");
  382. return -1;
  383. }
  384. /* Enable the window */
  385. layer->layer_info.enable = 1;
  386. if (cfg->pixfmt == PIXFMT_NV12) {
  387. struct vpbe_layer *otherlayer =
  388. _vpbe_display_get_other_win_layer(disp_dev, layer);
  389. ret = osd_device->ops.enable_layer(osd_device,
  390. otherlayer->layer_info.id, 1);
  391. if (ret < 0) {
  392. v4l2_err(&vpbe_dev->v4l2_dev,
  393. "Error in enabling osd window layer 1\n");
  394. return -1;
  395. }
  396. otherlayer->layer_info.enable = 1;
  397. }
  398. return 0;
  399. }
  400. static void
  401. vpbe_disp_calculate_scale_factor(struct vpbe_display *disp_dev,
  402. struct vpbe_layer *layer,
  403. int expected_xsize, int expected_ysize)
  404. {
  405. struct display_layer_info *layer_info = &layer->layer_info;
  406. struct v4l2_pix_format *pixfmt = &layer->pix_fmt;
  407. struct osd_layer_config *cfg = &layer->layer_info.config;
  408. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  409. int calculated_xsize;
  410. int h_exp = 0;
  411. int v_exp = 0;
  412. int h_scale;
  413. int v_scale;
  414. v4l2_std_id standard_id = vpbe_dev->current_timings.std_id;
  415. /*
  416. * Application initially set the image format. Current display
  417. * size is obtained from the vpbe display controller. expected_xsize
  418. * and expected_ysize are set through S_CROP ioctl. Based on this,
  419. * driver will calculate the scale factors for vertical and
  420. * horizontal direction so that the image is displayed scaled
  421. * and expanded. Application uses expansion to display the image
  422. * in a square pixel. Otherwise it is displayed using displays
  423. * pixel aspect ratio.It is expected that application chooses
  424. * the crop coordinates for cropped or scaled display. if crop
  425. * size is less than the image size, it is displayed cropped or
  426. * it is displayed scaled and/or expanded.
  427. *
  428. * to begin with, set the crop window same as expected. Later we
  429. * will override with scaled window size
  430. */
  431. cfg->xsize = pixfmt->width;
  432. cfg->ysize = pixfmt->height;
  433. layer_info->h_zoom = ZOOM_X1; /* no horizontal zoom */
  434. layer_info->v_zoom = ZOOM_X1; /* no horizontal zoom */
  435. layer_info->h_exp = H_EXP_OFF; /* no horizontal zoom */
  436. layer_info->v_exp = V_EXP_OFF; /* no horizontal zoom */
  437. if (pixfmt->width < expected_xsize) {
  438. h_scale = vpbe_dev->current_timings.xres / pixfmt->width;
  439. if (h_scale < 2)
  440. h_scale = 1;
  441. else if (h_scale >= 4)
  442. h_scale = 4;
  443. else
  444. h_scale = 2;
  445. cfg->xsize *= h_scale;
  446. if (cfg->xsize < expected_xsize) {
  447. if ((standard_id & V4L2_STD_525_60) ||
  448. (standard_id & V4L2_STD_625_50)) {
  449. calculated_xsize = (cfg->xsize *
  450. VPBE_DISPLAY_H_EXP_RATIO_N) /
  451. VPBE_DISPLAY_H_EXP_RATIO_D;
  452. if (calculated_xsize <= expected_xsize) {
  453. h_exp = 1;
  454. cfg->xsize = calculated_xsize;
  455. }
  456. }
  457. }
  458. if (h_scale == 2)
  459. layer_info->h_zoom = ZOOM_X2;
  460. else if (h_scale == 4)
  461. layer_info->h_zoom = ZOOM_X4;
  462. if (h_exp)
  463. layer_info->h_exp = H_EXP_9_OVER_8;
  464. } else {
  465. /* no scaling, only cropping. Set display area to crop area */
  466. cfg->xsize = expected_xsize;
  467. }
  468. if (pixfmt->height < expected_ysize) {
  469. v_scale = expected_ysize / pixfmt->height;
  470. if (v_scale < 2)
  471. v_scale = 1;
  472. else if (v_scale >= 4)
  473. v_scale = 4;
  474. else
  475. v_scale = 2;
  476. cfg->ysize *= v_scale;
  477. if (cfg->ysize < expected_ysize) {
  478. if ((standard_id & V4L2_STD_625_50)) {
  479. calculated_xsize = (cfg->ysize *
  480. VPBE_DISPLAY_V_EXP_RATIO_N) /
  481. VPBE_DISPLAY_V_EXP_RATIO_D;
  482. if (calculated_xsize <= expected_ysize) {
  483. v_exp = 1;
  484. cfg->ysize = calculated_xsize;
  485. }
  486. }
  487. }
  488. if (v_scale == 2)
  489. layer_info->v_zoom = ZOOM_X2;
  490. else if (v_scale == 4)
  491. layer_info->v_zoom = ZOOM_X4;
  492. if (v_exp)
  493. layer_info->h_exp = V_EXP_6_OVER_5;
  494. } else {
  495. /* no scaling, only cropping. Set display area to crop area */
  496. cfg->ysize = expected_ysize;
  497. }
  498. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  499. "crop display xsize = %d, ysize = %d\n",
  500. cfg->xsize, cfg->ysize);
  501. }
  502. static void vpbe_disp_adj_position(struct vpbe_display *disp_dev,
  503. struct vpbe_layer *layer,
  504. int top, int left)
  505. {
  506. struct osd_layer_config *cfg = &layer->layer_info.config;
  507. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  508. cfg->xpos = min((unsigned int)left,
  509. vpbe_dev->current_timings.xres - cfg->xsize);
  510. cfg->ypos = min((unsigned int)top,
  511. vpbe_dev->current_timings.yres - cfg->ysize);
  512. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  513. "new xpos = %d, ypos = %d\n",
  514. cfg->xpos, cfg->ypos);
  515. }
  516. static void vpbe_disp_check_window_params(struct vpbe_display *disp_dev,
  517. struct v4l2_rect *c)
  518. {
  519. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  520. if ((c->width == 0) ||
  521. ((c->width + c->left) > vpbe_dev->current_timings.xres))
  522. c->width = vpbe_dev->current_timings.xres - c->left;
  523. if ((c->height == 0) || ((c->height + c->top) >
  524. vpbe_dev->current_timings.yres))
  525. c->height = vpbe_dev->current_timings.yres - c->top;
  526. /* window height must be even for interlaced display */
  527. if (vpbe_dev->current_timings.interlaced)
  528. c->height &= (~0x01);
  529. }
  530. /**
  531. * vpbe_try_format()
  532. * If user application provides width and height, and have bytesperline set
  533. * to zero, driver calculates bytesperline and sizeimage based on hardware
  534. * limits.
  535. */
  536. static int vpbe_try_format(struct vpbe_display *disp_dev,
  537. struct v4l2_pix_format *pixfmt, int check)
  538. {
  539. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  540. int min_height = 1;
  541. int min_width = 32;
  542. int max_height;
  543. int max_width;
  544. int bpp;
  545. if ((pixfmt->pixelformat != V4L2_PIX_FMT_UYVY) &&
  546. (pixfmt->pixelformat != V4L2_PIX_FMT_NV12))
  547. /* choose default as V4L2_PIX_FMT_UYVY */
  548. pixfmt->pixelformat = V4L2_PIX_FMT_UYVY;
  549. /* Check the field format */
  550. if ((pixfmt->field != V4L2_FIELD_INTERLACED) &&
  551. (pixfmt->field != V4L2_FIELD_NONE)) {
  552. if (vpbe_dev->current_timings.interlaced)
  553. pixfmt->field = V4L2_FIELD_INTERLACED;
  554. else
  555. pixfmt->field = V4L2_FIELD_NONE;
  556. }
  557. if (pixfmt->field == V4L2_FIELD_INTERLACED)
  558. min_height = 2;
  559. if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12)
  560. bpp = 1;
  561. else
  562. bpp = 2;
  563. max_width = vpbe_dev->current_timings.xres;
  564. max_height = vpbe_dev->current_timings.yres;
  565. min_width /= bpp;
  566. if (!pixfmt->width || (pixfmt->width < min_width) ||
  567. (pixfmt->width > max_width)) {
  568. pixfmt->width = vpbe_dev->current_timings.xres;
  569. }
  570. if (!pixfmt->height || (pixfmt->height < min_height) ||
  571. (pixfmt->height > max_height)) {
  572. pixfmt->height = vpbe_dev->current_timings.yres;
  573. }
  574. if (pixfmt->bytesperline < (pixfmt->width * bpp))
  575. pixfmt->bytesperline = pixfmt->width * bpp;
  576. /* Make the bytesperline 32 byte aligned */
  577. pixfmt->bytesperline = ((pixfmt->width * bpp + 31) & ~31);
  578. if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12)
  579. pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height +
  580. (pixfmt->bytesperline * pixfmt->height >> 1);
  581. else
  582. pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height;
  583. return 0;
  584. }
  585. static int vpbe_display_g_priority(struct file *file, void *priv,
  586. enum v4l2_priority *p)
  587. {
  588. struct vpbe_fh *fh = file->private_data;
  589. struct vpbe_layer *layer = fh->layer;
  590. *p = v4l2_prio_max(&layer->prio);
  591. return 0;
  592. }
  593. static int vpbe_display_s_priority(struct file *file, void *priv,
  594. enum v4l2_priority p)
  595. {
  596. struct vpbe_fh *fh = file->private_data;
  597. struct vpbe_layer *layer = fh->layer;
  598. int ret;
  599. ret = v4l2_prio_change(&layer->prio, &fh->prio, p);
  600. return ret;
  601. }
  602. static int vpbe_display_querycap(struct file *file, void *priv,
  603. struct v4l2_capability *cap)
  604. {
  605. struct vpbe_fh *fh = file->private_data;
  606. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  607. cap->version = VPBE_DISPLAY_VERSION_CODE;
  608. cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
  609. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  610. snprintf(cap->driver, sizeof(cap->driver), "%s",
  611. dev_name(vpbe_dev->pdev));
  612. snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
  613. dev_name(vpbe_dev->pdev));
  614. strlcpy(cap->card, vpbe_dev->cfg->module_name, sizeof(cap->card));
  615. return 0;
  616. }
  617. static int vpbe_display_s_crop(struct file *file, void *priv,
  618. const struct v4l2_crop *crop)
  619. {
  620. struct vpbe_fh *fh = file->private_data;
  621. struct vpbe_layer *layer = fh->layer;
  622. struct vpbe_display *disp_dev = fh->disp_dev;
  623. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  624. struct osd_layer_config *cfg = &layer->layer_info.config;
  625. struct osd_state *osd_device = disp_dev->osd_device;
  626. struct v4l2_rect rect = crop->c;
  627. int ret;
  628. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  629. "VIDIOC_S_CROP, layer id = %d\n", layer->device_id);
  630. if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  631. v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buf type\n");
  632. return -EINVAL;
  633. }
  634. if (rect.top < 0)
  635. rect.top = 0;
  636. if (rect.left < 0)
  637. rect.left = 0;
  638. vpbe_disp_check_window_params(disp_dev, &rect);
  639. osd_device->ops.get_layer_config(osd_device,
  640. layer->layer_info.id, cfg);
  641. vpbe_disp_calculate_scale_factor(disp_dev, layer,
  642. rect.width,
  643. rect.height);
  644. vpbe_disp_adj_position(disp_dev, layer, rect.top,
  645. rect.left);
  646. ret = osd_device->ops.set_layer_config(osd_device,
  647. layer->layer_info.id, cfg);
  648. if (ret < 0) {
  649. v4l2_err(&vpbe_dev->v4l2_dev,
  650. "Error in set layer config:\n");
  651. return -EINVAL;
  652. }
  653. /* apply zooming and h or v expansion */
  654. osd_device->ops.set_zoom(osd_device,
  655. layer->layer_info.id,
  656. layer->layer_info.h_zoom,
  657. layer->layer_info.v_zoom);
  658. ret = osd_device->ops.set_vid_expansion(osd_device,
  659. layer->layer_info.h_exp,
  660. layer->layer_info.v_exp);
  661. if (ret < 0) {
  662. v4l2_err(&vpbe_dev->v4l2_dev,
  663. "Error in set vid expansion:\n");
  664. return -EINVAL;
  665. }
  666. if ((layer->layer_info.h_zoom != ZOOM_X1) ||
  667. (layer->layer_info.v_zoom != ZOOM_X1) ||
  668. (layer->layer_info.h_exp != H_EXP_OFF) ||
  669. (layer->layer_info.v_exp != V_EXP_OFF))
  670. /* Enable expansion filter */
  671. osd_device->ops.set_interpolation_filter(osd_device, 1);
  672. else
  673. osd_device->ops.set_interpolation_filter(osd_device, 0);
  674. return 0;
  675. }
  676. static int vpbe_display_g_crop(struct file *file, void *priv,
  677. struct v4l2_crop *crop)
  678. {
  679. struct vpbe_fh *fh = file->private_data;
  680. struct vpbe_layer *layer = fh->layer;
  681. struct osd_layer_config *cfg = &layer->layer_info.config;
  682. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  683. struct osd_state *osd_device = fh->disp_dev->osd_device;
  684. struct v4l2_rect *rect = &crop->c;
  685. int ret;
  686. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  687. "VIDIOC_G_CROP, layer id = %d\n",
  688. layer->device_id);
  689. if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  690. v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buf type\n");
  691. ret = -EINVAL;
  692. }
  693. osd_device->ops.get_layer_config(osd_device,
  694. layer->layer_info.id, cfg);
  695. rect->top = cfg->ypos;
  696. rect->left = cfg->xpos;
  697. rect->width = cfg->xsize;
  698. rect->height = cfg->ysize;
  699. return 0;
  700. }
  701. static int vpbe_display_cropcap(struct file *file, void *priv,
  702. struct v4l2_cropcap *cropcap)
  703. {
  704. struct vpbe_fh *fh = file->private_data;
  705. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  706. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_CROPCAP ioctl\n");
  707. cropcap->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  708. cropcap->bounds.left = 0;
  709. cropcap->bounds.top = 0;
  710. cropcap->bounds.width = vpbe_dev->current_timings.xres;
  711. cropcap->bounds.height = vpbe_dev->current_timings.yres;
  712. cropcap->pixelaspect = vpbe_dev->current_timings.aspect;
  713. cropcap->defrect = cropcap->bounds;
  714. return 0;
  715. }
  716. static int vpbe_display_g_fmt(struct file *file, void *priv,
  717. struct v4l2_format *fmt)
  718. {
  719. struct vpbe_fh *fh = file->private_data;
  720. struct vpbe_layer *layer = fh->layer;
  721. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  722. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  723. "VIDIOC_G_FMT, layer id = %d\n",
  724. layer->device_id);
  725. /* If buffer type is video output */
  726. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != fmt->type) {
  727. v4l2_err(&vpbe_dev->v4l2_dev, "invalid type\n");
  728. return -EINVAL;
  729. }
  730. /* Fill in the information about format */
  731. fmt->fmt.pix = layer->pix_fmt;
  732. return 0;
  733. }
  734. static int vpbe_display_enum_fmt(struct file *file, void *priv,
  735. struct v4l2_fmtdesc *fmt)
  736. {
  737. struct vpbe_fh *fh = file->private_data;
  738. struct vpbe_layer *layer = fh->layer;
  739. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  740. unsigned int index = 0;
  741. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  742. "VIDIOC_ENUM_FMT, layer id = %d\n",
  743. layer->device_id);
  744. if (fmt->index > 1) {
  745. v4l2_err(&vpbe_dev->v4l2_dev, "Invalid format index\n");
  746. return -EINVAL;
  747. }
  748. /* Fill in the information about format */
  749. index = fmt->index;
  750. memset(fmt, 0, sizeof(*fmt));
  751. fmt->index = index;
  752. fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  753. if (index == 0) {
  754. strcpy(fmt->description, "YUV 4:2:2 - UYVY");
  755. fmt->pixelformat = V4L2_PIX_FMT_UYVY;
  756. } else {
  757. strcpy(fmt->description, "Y/CbCr 4:2:0");
  758. fmt->pixelformat = V4L2_PIX_FMT_NV12;
  759. }
  760. return 0;
  761. }
  762. static int vpbe_display_s_fmt(struct file *file, void *priv,
  763. struct v4l2_format *fmt)
  764. {
  765. struct vpbe_fh *fh = file->private_data;
  766. struct vpbe_layer *layer = fh->layer;
  767. struct vpbe_display *disp_dev = fh->disp_dev;
  768. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  769. struct osd_layer_config *cfg = &layer->layer_info.config;
  770. struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
  771. struct osd_state *osd_device = disp_dev->osd_device;
  772. int ret;
  773. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  774. "VIDIOC_S_FMT, layer id = %d\n",
  775. layer->device_id);
  776. /* If streaming is started, return error */
  777. if (layer->started) {
  778. v4l2_err(&vpbe_dev->v4l2_dev, "Streaming is started\n");
  779. return -EBUSY;
  780. }
  781. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != fmt->type) {
  782. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "invalid type\n");
  783. return -EINVAL;
  784. }
  785. /* Check for valid pixel format */
  786. ret = vpbe_try_format(disp_dev, pixfmt, 1);
  787. if (ret)
  788. return ret;
  789. /* YUV420 is requested, check availability of the
  790. other video window */
  791. layer->pix_fmt = *pixfmt;
  792. /* Get osd layer config */
  793. osd_device->ops.get_layer_config(osd_device,
  794. layer->layer_info.id, cfg);
  795. /* Store the pixel format in the layer object */
  796. cfg->xsize = pixfmt->width;
  797. cfg->ysize = pixfmt->height;
  798. cfg->line_length = pixfmt->bytesperline;
  799. cfg->ypos = 0;
  800. cfg->xpos = 0;
  801. cfg->interlaced = vpbe_dev->current_timings.interlaced;
  802. if (V4L2_PIX_FMT_UYVY == pixfmt->pixelformat)
  803. cfg->pixfmt = PIXFMT_YCbCrI;
  804. /* Change of the default pixel format for both video windows */
  805. if (V4L2_PIX_FMT_NV12 == pixfmt->pixelformat) {
  806. struct vpbe_layer *otherlayer;
  807. cfg->pixfmt = PIXFMT_NV12;
  808. otherlayer = _vpbe_display_get_other_win_layer(disp_dev,
  809. layer);
  810. otherlayer->layer_info.config.pixfmt = PIXFMT_NV12;
  811. }
  812. /* Set the layer config in the osd window */
  813. ret = osd_device->ops.set_layer_config(osd_device,
  814. layer->layer_info.id, cfg);
  815. if (ret < 0) {
  816. v4l2_err(&vpbe_dev->v4l2_dev,
  817. "Error in S_FMT params:\n");
  818. return -EINVAL;
  819. }
  820. /* Readback and fill the local copy of current pix format */
  821. osd_device->ops.get_layer_config(osd_device,
  822. layer->layer_info.id, cfg);
  823. return 0;
  824. }
  825. static int vpbe_display_try_fmt(struct file *file, void *priv,
  826. struct v4l2_format *fmt)
  827. {
  828. struct vpbe_fh *fh = file->private_data;
  829. struct vpbe_display *disp_dev = fh->disp_dev;
  830. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  831. struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
  832. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_TRY_FMT\n");
  833. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != fmt->type) {
  834. v4l2_err(&vpbe_dev->v4l2_dev, "invalid type\n");
  835. return -EINVAL;
  836. }
  837. /* Check for valid field format */
  838. return vpbe_try_format(disp_dev, pixfmt, 0);
  839. }
  840. /**
  841. * vpbe_display_s_std - Set the given standard in the encoder
  842. *
  843. * Sets the standard if supported by the current encoder. Return the status.
  844. * 0 - success & -EINVAL on error
  845. */
  846. static int vpbe_display_s_std(struct file *file, void *priv,
  847. v4l2_std_id *std_id)
  848. {
  849. struct vpbe_fh *fh = priv;
  850. struct vpbe_layer *layer = fh->layer;
  851. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  852. int ret;
  853. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_S_STD\n");
  854. /* If streaming is started, return error */
  855. if (layer->started) {
  856. v4l2_err(&vpbe_dev->v4l2_dev, "Streaming is started\n");
  857. return -EBUSY;
  858. }
  859. if (NULL != vpbe_dev->ops.s_std) {
  860. ret = vpbe_dev->ops.s_std(vpbe_dev, std_id);
  861. if (ret) {
  862. v4l2_err(&vpbe_dev->v4l2_dev,
  863. "Failed to set standard for sub devices\n");
  864. return -EINVAL;
  865. }
  866. } else {
  867. return -EINVAL;
  868. }
  869. return 0;
  870. }
  871. /**
  872. * vpbe_display_g_std - Get the standard in the current encoder
  873. *
  874. * Get the standard in the current encoder. Return the status. 0 - success
  875. * -EINVAL on error
  876. */
  877. static int vpbe_display_g_std(struct file *file, void *priv,
  878. v4l2_std_id *std_id)
  879. {
  880. struct vpbe_fh *fh = priv;
  881. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  882. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_G_STD\n");
  883. /* Get the standard from the current encoder */
  884. if (vpbe_dev->current_timings.timings_type & VPBE_ENC_STD) {
  885. *std_id = vpbe_dev->current_timings.std_id;
  886. return 0;
  887. }
  888. return -EINVAL;
  889. }
  890. /**
  891. * vpbe_display_enum_output - enumerate outputs
  892. *
  893. * Enumerates the outputs available at the vpbe display
  894. * returns the status, -EINVAL if end of output list
  895. */
  896. static int vpbe_display_enum_output(struct file *file, void *priv,
  897. struct v4l2_output *output)
  898. {
  899. struct vpbe_fh *fh = priv;
  900. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  901. int ret;
  902. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_ENUM_OUTPUT\n");
  903. /* Enumerate outputs */
  904. if (NULL == vpbe_dev->ops.enum_outputs)
  905. return -EINVAL;
  906. ret = vpbe_dev->ops.enum_outputs(vpbe_dev, output);
  907. if (ret) {
  908. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  909. "Failed to enumerate outputs\n");
  910. return -EINVAL;
  911. }
  912. return 0;
  913. }
  914. /**
  915. * vpbe_display_s_output - Set output to
  916. * the output specified by the index
  917. */
  918. static int vpbe_display_s_output(struct file *file, void *priv,
  919. unsigned int i)
  920. {
  921. struct vpbe_fh *fh = priv;
  922. struct vpbe_layer *layer = fh->layer;
  923. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  924. int ret;
  925. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_S_OUTPUT\n");
  926. /* If streaming is started, return error */
  927. if (layer->started) {
  928. v4l2_err(&vpbe_dev->v4l2_dev, "Streaming is started\n");
  929. return -EBUSY;
  930. }
  931. if (NULL == vpbe_dev->ops.set_output)
  932. return -EINVAL;
  933. ret = vpbe_dev->ops.set_output(vpbe_dev, i);
  934. if (ret) {
  935. v4l2_err(&vpbe_dev->v4l2_dev,
  936. "Failed to set output for sub devices\n");
  937. return -EINVAL;
  938. }
  939. return 0;
  940. }
  941. /**
  942. * vpbe_display_g_output - Get output from subdevice
  943. * for a given by the index
  944. */
  945. static int vpbe_display_g_output(struct file *file, void *priv,
  946. unsigned int *i)
  947. {
  948. struct vpbe_fh *fh = priv;
  949. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  950. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_G_OUTPUT\n");
  951. /* Get the standard from the current encoder */
  952. *i = vpbe_dev->current_out_index;
  953. return 0;
  954. }
  955. /**
  956. * vpbe_display_enum_dv_timings - Enumerate the dv timings
  957. *
  958. * enum the timings in the current encoder. Return the status. 0 - success
  959. * -EINVAL on error
  960. */
  961. static int
  962. vpbe_display_enum_dv_timings(struct file *file, void *priv,
  963. struct v4l2_enum_dv_timings *timings)
  964. {
  965. struct vpbe_fh *fh = priv;
  966. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  967. int ret;
  968. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_ENUM_DV_TIMINGS\n");
  969. /* Enumerate outputs */
  970. if (NULL == vpbe_dev->ops.enum_dv_timings)
  971. return -EINVAL;
  972. ret = vpbe_dev->ops.enum_dv_timings(vpbe_dev, timings);
  973. if (ret) {
  974. v4l2_err(&vpbe_dev->v4l2_dev,
  975. "Failed to enumerate dv timings info\n");
  976. return -EINVAL;
  977. }
  978. return 0;
  979. }
  980. /**
  981. * vpbe_display_s_dv_timings - Set the dv timings
  982. *
  983. * Set the timings in the current encoder. Return the status. 0 - success
  984. * -EINVAL on error
  985. */
  986. static int
  987. vpbe_display_s_dv_timings(struct file *file, void *priv,
  988. struct v4l2_dv_timings *timings)
  989. {
  990. struct vpbe_fh *fh = priv;
  991. struct vpbe_layer *layer = fh->layer;
  992. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  993. int ret;
  994. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_S_DV_TIMINGS\n");
  995. /* If streaming is started, return error */
  996. if (layer->started) {
  997. v4l2_err(&vpbe_dev->v4l2_dev, "Streaming is started\n");
  998. return -EBUSY;
  999. }
  1000. /* Set the given standard in the encoder */
  1001. if (!vpbe_dev->ops.s_dv_timings)
  1002. return -EINVAL;
  1003. ret = vpbe_dev->ops.s_dv_timings(vpbe_dev, timings);
  1004. if (ret) {
  1005. v4l2_err(&vpbe_dev->v4l2_dev,
  1006. "Failed to set the dv timings info\n");
  1007. return -EINVAL;
  1008. }
  1009. /* set the current norm to zero to be consistent. If STD is used
  1010. * v4l2 layer will set the norm properly on successful s_std call
  1011. */
  1012. layer->video_dev.current_norm = 0;
  1013. return 0;
  1014. }
  1015. /**
  1016. * vpbe_display_g_dv_timings - Set the dv timings
  1017. *
  1018. * Get the timings in the current encoder. Return the status. 0 - success
  1019. * -EINVAL on error
  1020. */
  1021. static int
  1022. vpbe_display_g_dv_timings(struct file *file, void *priv,
  1023. struct v4l2_dv_timings *dv_timings)
  1024. {
  1025. struct vpbe_fh *fh = priv;
  1026. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  1027. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_G_DV_TIMINGS\n");
  1028. /* Get the given standard in the encoder */
  1029. if (vpbe_dev->current_timings.timings_type &
  1030. VPBE_ENC_CUSTOM_TIMINGS) {
  1031. *dv_timings = vpbe_dev->current_timings.dv_timings;
  1032. } else {
  1033. return -EINVAL;
  1034. }
  1035. return 0;
  1036. }
  1037. static int vpbe_display_streamoff(struct file *file, void *priv,
  1038. enum v4l2_buf_type buf_type)
  1039. {
  1040. struct vpbe_fh *fh = file->private_data;
  1041. struct vpbe_layer *layer = fh->layer;
  1042. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  1043. struct osd_state *osd_device = fh->disp_dev->osd_device;
  1044. int ret;
  1045. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  1046. "VIDIOC_STREAMOFF,layer id = %d\n",
  1047. layer->device_id);
  1048. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != buf_type) {
  1049. v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
  1050. return -EINVAL;
  1051. }
  1052. /* If io is allowed for this file handle, return error */
  1053. if (!fh->io_allowed) {
  1054. v4l2_err(&vpbe_dev->v4l2_dev, "No io_allowed\n");
  1055. return -EACCES;
  1056. }
  1057. /* If streaming is not started, return error */
  1058. if (!layer->started) {
  1059. v4l2_err(&vpbe_dev->v4l2_dev, "streaming not started in layer"
  1060. " id = %d\n", layer->device_id);
  1061. return -EINVAL;
  1062. }
  1063. osd_device->ops.disable_layer(osd_device,
  1064. layer->layer_info.id);
  1065. layer->started = 0;
  1066. ret = vb2_streamoff(&layer->buffer_queue, buf_type);
  1067. return ret;
  1068. }
  1069. static int vpbe_display_streamon(struct file *file, void *priv,
  1070. enum v4l2_buf_type buf_type)
  1071. {
  1072. struct vpbe_fh *fh = file->private_data;
  1073. struct vpbe_layer *layer = fh->layer;
  1074. struct vpbe_display *disp_dev = fh->disp_dev;
  1075. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  1076. struct osd_state *osd_device = disp_dev->osd_device;
  1077. int ret;
  1078. osd_device->ops.disable_layer(osd_device,
  1079. layer->layer_info.id);
  1080. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_STREAMON, layerid=%d\n",
  1081. layer->device_id);
  1082. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != buf_type) {
  1083. v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
  1084. return -EINVAL;
  1085. }
  1086. /* If file handle is not allowed IO, return error */
  1087. if (!fh->io_allowed) {
  1088. v4l2_err(&vpbe_dev->v4l2_dev, "No io_allowed\n");
  1089. return -EACCES;
  1090. }
  1091. /* If Streaming is already started, return error */
  1092. if (layer->started) {
  1093. v4l2_err(&vpbe_dev->v4l2_dev, "layer is already streaming\n");
  1094. return -EBUSY;
  1095. }
  1096. /*
  1097. * Call vb2_streamon to start streaming
  1098. * in videobuf
  1099. */
  1100. ret = vb2_streamon(&layer->buffer_queue, buf_type);
  1101. if (ret) {
  1102. v4l2_err(&vpbe_dev->v4l2_dev,
  1103. "error in vb2_streamon\n");
  1104. return ret;
  1105. }
  1106. return ret;
  1107. }
  1108. static int vpbe_display_dqbuf(struct file *file, void *priv,
  1109. struct v4l2_buffer *buf)
  1110. {
  1111. struct vpbe_fh *fh = file->private_data;
  1112. struct vpbe_layer *layer = fh->layer;
  1113. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  1114. int ret;
  1115. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  1116. "VIDIOC_DQBUF, layer id = %d\n",
  1117. layer->device_id);
  1118. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != buf->type) {
  1119. v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
  1120. return -EINVAL;
  1121. }
  1122. /* If this file handle is not allowed to do IO, return error */
  1123. if (!fh->io_allowed) {
  1124. v4l2_err(&vpbe_dev->v4l2_dev, "No io_allowed\n");
  1125. return -EACCES;
  1126. }
  1127. if (file->f_flags & O_NONBLOCK)
  1128. /* Call videobuf_dqbuf for non blocking mode */
  1129. ret = vb2_dqbuf(&layer->buffer_queue, buf, 1);
  1130. else
  1131. /* Call videobuf_dqbuf for blocking mode */
  1132. ret = vb2_dqbuf(&layer->buffer_queue, buf, 0);
  1133. return ret;
  1134. }
  1135. static int vpbe_display_qbuf(struct file *file, void *priv,
  1136. struct v4l2_buffer *p)
  1137. {
  1138. struct vpbe_fh *fh = file->private_data;
  1139. struct vpbe_layer *layer = fh->layer;
  1140. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  1141. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  1142. "VIDIOC_QBUF, layer id = %d\n",
  1143. layer->device_id);
  1144. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != p->type) {
  1145. v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
  1146. return -EINVAL;
  1147. }
  1148. /* If this file handle is not allowed to do IO, return error */
  1149. if (!fh->io_allowed) {
  1150. v4l2_err(&vpbe_dev->v4l2_dev, "No io_allowed\n");
  1151. return -EACCES;
  1152. }
  1153. return vb2_qbuf(&layer->buffer_queue, p);
  1154. }
  1155. static int vpbe_display_querybuf(struct file *file, void *priv,
  1156. struct v4l2_buffer *buf)
  1157. {
  1158. struct vpbe_fh *fh = file->private_data;
  1159. struct vpbe_layer *layer = fh->layer;
  1160. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  1161. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  1162. "VIDIOC_QUERYBUF, layer id = %d\n",
  1163. layer->device_id);
  1164. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != buf->type) {
  1165. v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
  1166. return -EINVAL;
  1167. }
  1168. /* Call vb2_querybuf to get information */
  1169. return vb2_querybuf(&layer->buffer_queue, buf);
  1170. }
  1171. static int vpbe_display_reqbufs(struct file *file, void *priv,
  1172. struct v4l2_requestbuffers *req_buf)
  1173. {
  1174. struct vpbe_fh *fh = file->private_data;
  1175. struct vpbe_layer *layer = fh->layer;
  1176. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  1177. struct vb2_queue *q;
  1178. int ret;
  1179. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_reqbufs\n");
  1180. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != req_buf->type) {
  1181. v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
  1182. return -EINVAL;
  1183. }
  1184. /* If io users of the layer is not zero, return error */
  1185. if (0 != layer->io_usrs) {
  1186. v4l2_err(&vpbe_dev->v4l2_dev, "not IO user\n");
  1187. return -EBUSY;
  1188. }
  1189. /* Initialize videobuf queue as per the buffer type */
  1190. layer->alloc_ctx = vb2_dma_contig_init_ctx(vpbe_dev->pdev);
  1191. if (!layer->alloc_ctx) {
  1192. v4l2_err(&vpbe_dev->v4l2_dev, "Failed to get the context\n");
  1193. return -EINVAL;
  1194. }
  1195. q = &layer->buffer_queue;
  1196. memset(q, 0, sizeof(*q));
  1197. q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  1198. q->io_modes = VB2_MMAP | VB2_USERPTR;
  1199. q->drv_priv = fh;
  1200. q->ops = &video_qops;
  1201. q->mem_ops = &vb2_dma_contig_memops;
  1202. q->buf_struct_size = sizeof(struct vpbe_disp_buffer);
  1203. ret = vb2_queue_init(q);
  1204. if (ret) {
  1205. v4l2_err(&vpbe_dev->v4l2_dev, "vb2_queue_init() failed\n");
  1206. vb2_dma_contig_cleanup_ctx(layer->alloc_ctx);
  1207. return ret;
  1208. }
  1209. /* Set io allowed member of file handle to TRUE */
  1210. fh->io_allowed = 1;
  1211. /* Increment io usrs member of layer object to 1 */
  1212. layer->io_usrs = 1;
  1213. /* Store type of memory requested in layer object */
  1214. layer->memory = req_buf->memory;
  1215. /* Initialize buffer queue */
  1216. INIT_LIST_HEAD(&layer->dma_queue);
  1217. /* Allocate buffers */
  1218. return vb2_reqbufs(q, req_buf);
  1219. }
  1220. /*
  1221. * vpbe_display_mmap()
  1222. * It is used to map kernel space buffers into user spaces
  1223. */
  1224. static int vpbe_display_mmap(struct file *filep, struct vm_area_struct *vma)
  1225. {
  1226. /* Get the layer object and file handle object */
  1227. struct vpbe_fh *fh = filep->private_data;
  1228. struct vpbe_layer *layer = fh->layer;
  1229. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  1230. int ret;
  1231. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_mmap\n");
  1232. if (mutex_lock_interruptible(&layer->opslock))
  1233. return -ERESTARTSYS;
  1234. ret = vb2_mmap(&layer->buffer_queue, vma);
  1235. mutex_unlock(&layer->opslock);
  1236. return ret;
  1237. }
  1238. /* vpbe_display_poll(): It is used for select/poll system call
  1239. */
  1240. static unsigned int vpbe_display_poll(struct file *filep, poll_table *wait)
  1241. {
  1242. struct vpbe_fh *fh = filep->private_data;
  1243. struct vpbe_layer *layer = fh->layer;
  1244. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  1245. unsigned int err = 0;
  1246. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_poll\n");
  1247. if (layer->started) {
  1248. mutex_lock(&layer->opslock);
  1249. err = vb2_poll(&layer->buffer_queue, filep, wait);
  1250. mutex_unlock(&layer->opslock);
  1251. }
  1252. return err;
  1253. }
  1254. /*
  1255. * vpbe_display_open()
  1256. * It creates object of file handle structure and stores it in private_data
  1257. * member of filepointer
  1258. */
  1259. static int vpbe_display_open(struct file *file)
  1260. {
  1261. struct vpbe_fh *fh = NULL;
  1262. struct vpbe_layer *layer = video_drvdata(file);
  1263. struct vpbe_display *disp_dev = layer->disp_dev;
  1264. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  1265. struct osd_state *osd_device = disp_dev->osd_device;
  1266. int err;
  1267. /* Allocate memory for the file handle object */
  1268. fh = kmalloc(sizeof(struct vpbe_fh), GFP_KERNEL);
  1269. if (fh == NULL) {
  1270. v4l2_err(&vpbe_dev->v4l2_dev,
  1271. "unable to allocate memory for file handle object\n");
  1272. return -ENOMEM;
  1273. }
  1274. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  1275. "vpbe display open plane = %d\n",
  1276. layer->device_id);
  1277. /* store pointer to fh in private_data member of filep */
  1278. file->private_data = fh;
  1279. fh->layer = layer;
  1280. fh->disp_dev = disp_dev;
  1281. if (!layer->usrs) {
  1282. if (mutex_lock_interruptible(&layer->opslock))
  1283. return -ERESTARTSYS;
  1284. /* First claim the layer for this device */
  1285. err = osd_device->ops.request_layer(osd_device,
  1286. layer->layer_info.id);
  1287. mutex_unlock(&layer->opslock);
  1288. if (err < 0) {
  1289. /* Couldn't get layer */
  1290. v4l2_err(&vpbe_dev->v4l2_dev,
  1291. "Display Manager failed to allocate layer\n");
  1292. kfree(fh);
  1293. return -EINVAL;
  1294. }
  1295. }
  1296. /* Increment layer usrs counter */
  1297. layer->usrs++;
  1298. /* Set io_allowed member to false */
  1299. fh->io_allowed = 0;
  1300. /* Initialize priority of this instance to default priority */
  1301. fh->prio = V4L2_PRIORITY_UNSET;
  1302. v4l2_prio_open(&layer->prio, &fh->prio);
  1303. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  1304. "vpbe display device opened successfully\n");
  1305. return 0;
  1306. }
  1307. /*
  1308. * vpbe_display_release()
  1309. * This function deletes buffer queue, frees the buffers and the davinci
  1310. * display file * handle
  1311. */
  1312. static int vpbe_display_release(struct file *file)
  1313. {
  1314. /* Get the layer object and file handle object */
  1315. struct vpbe_fh *fh = file->private_data;
  1316. struct vpbe_layer *layer = fh->layer;
  1317. struct osd_layer_config *cfg = &layer->layer_info.config;
  1318. struct vpbe_display *disp_dev = fh->disp_dev;
  1319. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  1320. struct osd_state *osd_device = disp_dev->osd_device;
  1321. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_release\n");
  1322. mutex_lock(&layer->opslock);
  1323. /* if this instance is doing IO */
  1324. if (fh->io_allowed) {
  1325. /* Reset io_usrs member of layer object */
  1326. layer->io_usrs = 0;
  1327. osd_device->ops.disable_layer(osd_device,
  1328. layer->layer_info.id);
  1329. layer->started = 0;
  1330. /* Free buffers allocated */
  1331. vb2_queue_release(&layer->buffer_queue);
  1332. vb2_dma_contig_cleanup_ctx(&layer->buffer_queue);
  1333. }
  1334. /* Decrement layer usrs counter */
  1335. layer->usrs--;
  1336. /* If this file handle has initialize encoder device, reset it */
  1337. if (!layer->usrs) {
  1338. if (cfg->pixfmt == PIXFMT_NV12) {
  1339. struct vpbe_layer *otherlayer;
  1340. otherlayer =
  1341. _vpbe_display_get_other_win_layer(disp_dev, layer);
  1342. osd_device->ops.disable_layer(osd_device,
  1343. otherlayer->layer_info.id);
  1344. osd_device->ops.release_layer(osd_device,
  1345. otherlayer->layer_info.id);
  1346. }
  1347. osd_device->ops.disable_layer(osd_device,
  1348. layer->layer_info.id);
  1349. osd_device->ops.release_layer(osd_device,
  1350. layer->layer_info.id);
  1351. }
  1352. /* Close the priority */
  1353. v4l2_prio_close(&layer->prio, fh->prio);
  1354. file->private_data = NULL;
  1355. mutex_unlock(&layer->opslock);
  1356. /* Free memory allocated to file handle object */
  1357. kfree(fh);
  1358. disp_dev->cbcr_ofst = 0;
  1359. return 0;
  1360. }
  1361. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1362. static int vpbe_display_g_register(struct file *file, void *priv,
  1363. struct v4l2_dbg_register *reg)
  1364. {
  1365. struct v4l2_dbg_match *match = &reg->match;
  1366. struct vpbe_fh *fh = file->private_data;
  1367. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  1368. if (match->type >= 2) {
  1369. v4l2_subdev_call(vpbe_dev->venc,
  1370. core,
  1371. g_register,
  1372. reg);
  1373. }
  1374. return 0;
  1375. }
  1376. static int vpbe_display_s_register(struct file *file, void *priv,
  1377. struct v4l2_dbg_register *reg)
  1378. {
  1379. return 0;
  1380. }
  1381. #endif
  1382. /* vpbe capture ioctl operations */
  1383. static const struct v4l2_ioctl_ops vpbe_ioctl_ops = {
  1384. .vidioc_querycap = vpbe_display_querycap,
  1385. .vidioc_g_fmt_vid_out = vpbe_display_g_fmt,
  1386. .vidioc_enum_fmt_vid_out = vpbe_display_enum_fmt,
  1387. .vidioc_s_fmt_vid_out = vpbe_display_s_fmt,
  1388. .vidioc_try_fmt_vid_out = vpbe_display_try_fmt,
  1389. .vidioc_reqbufs = vpbe_display_reqbufs,
  1390. .vidioc_querybuf = vpbe_display_querybuf,
  1391. .vidioc_qbuf = vpbe_display_qbuf,
  1392. .vidioc_dqbuf = vpbe_display_dqbuf,
  1393. .vidioc_streamon = vpbe_display_streamon,
  1394. .vidioc_streamoff = vpbe_display_streamoff,
  1395. .vidioc_cropcap = vpbe_display_cropcap,
  1396. .vidioc_g_crop = vpbe_display_g_crop,
  1397. .vidioc_s_crop = vpbe_display_s_crop,
  1398. .vidioc_g_priority = vpbe_display_g_priority,
  1399. .vidioc_s_priority = vpbe_display_s_priority,
  1400. .vidioc_s_std = vpbe_display_s_std,
  1401. .vidioc_g_std = vpbe_display_g_std,
  1402. .vidioc_enum_output = vpbe_display_enum_output,
  1403. .vidioc_s_output = vpbe_display_s_output,
  1404. .vidioc_g_output = vpbe_display_g_output,
  1405. .vidioc_s_dv_timings = vpbe_display_s_dv_timings,
  1406. .vidioc_g_dv_timings = vpbe_display_g_dv_timings,
  1407. .vidioc_enum_dv_timings = vpbe_display_enum_dv_timings,
  1408. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1409. .vidioc_g_register = vpbe_display_g_register,
  1410. .vidioc_s_register = vpbe_display_s_register,
  1411. #endif
  1412. };
  1413. static struct v4l2_file_operations vpbe_fops = {
  1414. .owner = THIS_MODULE,
  1415. .open = vpbe_display_open,
  1416. .release = vpbe_display_release,
  1417. .unlocked_ioctl = video_ioctl2,
  1418. .mmap = vpbe_display_mmap,
  1419. .poll = vpbe_display_poll
  1420. };
  1421. static int vpbe_device_get(struct device *dev, void *data)
  1422. {
  1423. struct platform_device *pdev = to_platform_device(dev);
  1424. struct vpbe_display *vpbe_disp = data;
  1425. if (strcmp("vpbe_controller", pdev->name) == 0)
  1426. vpbe_disp->vpbe_dev = platform_get_drvdata(pdev);
  1427. if (strcmp("vpbe-osd", pdev->name) == 0)
  1428. vpbe_disp->osd_device = platform_get_drvdata(pdev);
  1429. return 0;
  1430. }
  1431. static int init_vpbe_layer(int i, struct vpbe_display *disp_dev,
  1432. struct platform_device *pdev)
  1433. {
  1434. struct vpbe_layer *vpbe_display_layer = NULL;
  1435. struct video_device *vbd = NULL;
  1436. /* Allocate memory for four plane display objects */
  1437. disp_dev->dev[i] =
  1438. kzalloc(sizeof(struct vpbe_layer), GFP_KERNEL);
  1439. /* If memory allocation fails, return error */
  1440. if (!disp_dev->dev[i]) {
  1441. printk(KERN_ERR "ran out of memory\n");
  1442. return -ENOMEM;
  1443. }
  1444. spin_lock_init(&disp_dev->dev[i]->irqlock);
  1445. mutex_init(&disp_dev->dev[i]->opslock);
  1446. /* Get the pointer to the layer object */
  1447. vpbe_display_layer = disp_dev->dev[i];
  1448. vbd = &vpbe_display_layer->video_dev;
  1449. /* Initialize field of video device */
  1450. vbd->release = video_device_release_empty;
  1451. vbd->fops = &vpbe_fops;
  1452. vbd->ioctl_ops = &vpbe_ioctl_ops;
  1453. vbd->minor = -1;
  1454. vbd->v4l2_dev = &disp_dev->vpbe_dev->v4l2_dev;
  1455. vbd->lock = &vpbe_display_layer->opslock;
  1456. vbd->vfl_dir = VFL_DIR_TX;
  1457. if (disp_dev->vpbe_dev->current_timings.timings_type &
  1458. VPBE_ENC_STD) {
  1459. vbd->tvnorms = (V4L2_STD_525_60 | V4L2_STD_625_50);
  1460. vbd->current_norm =
  1461. disp_dev->vpbe_dev->current_timings.std_id;
  1462. } else
  1463. vbd->current_norm = 0;
  1464. snprintf(vbd->name, sizeof(vbd->name),
  1465. "DaVinci_VPBE Display_DRIVER_V%d.%d.%d",
  1466. (VPBE_DISPLAY_VERSION_CODE >> 16) & 0xff,
  1467. (VPBE_DISPLAY_VERSION_CODE >> 8) & 0xff,
  1468. (VPBE_DISPLAY_VERSION_CODE) & 0xff);
  1469. vpbe_display_layer->device_id = i;
  1470. vpbe_display_layer->layer_info.id =
  1471. ((i == VPBE_DISPLAY_DEVICE_0) ? WIN_VID0 : WIN_VID1);
  1472. /* Initialize prio member of layer object */
  1473. v4l2_prio_init(&vpbe_display_layer->prio);
  1474. return 0;
  1475. }
  1476. static int register_device(struct vpbe_layer *vpbe_display_layer,
  1477. struct vpbe_display *disp_dev,
  1478. struct platform_device *pdev)
  1479. {
  1480. int err;
  1481. v4l2_info(&disp_dev->vpbe_dev->v4l2_dev,
  1482. "Trying to register VPBE display device.\n");
  1483. v4l2_info(&disp_dev->vpbe_dev->v4l2_dev,
  1484. "layer=%x,layer->video_dev=%x\n",
  1485. (int)vpbe_display_layer,
  1486. (int)&vpbe_display_layer->video_dev);
  1487. err = video_register_device(&vpbe_display_layer->video_dev,
  1488. VFL_TYPE_GRABBER,
  1489. -1);
  1490. if (err)
  1491. return -ENODEV;
  1492. vpbe_display_layer->disp_dev = disp_dev;
  1493. /* set the driver data in platform device */
  1494. platform_set_drvdata(pdev, disp_dev);
  1495. video_set_drvdata(&vpbe_display_layer->video_dev,
  1496. vpbe_display_layer);
  1497. return 0;
  1498. }
  1499. /*
  1500. * vpbe_display_probe()
  1501. * This function creates device entries by register itself to the V4L2 driver
  1502. * and initializes fields of each layer objects
  1503. */
  1504. static int vpbe_display_probe(struct platform_device *pdev)
  1505. {
  1506. struct vpbe_layer *vpbe_display_layer;
  1507. struct vpbe_display *disp_dev;
  1508. struct resource *res = NULL;
  1509. int k;
  1510. int i;
  1511. int err;
  1512. int irq;
  1513. printk(KERN_DEBUG "vpbe_display_probe\n");
  1514. /* Allocate memory for vpbe_display */
  1515. disp_dev = kzalloc(sizeof(struct vpbe_display), GFP_KERNEL);
  1516. if (!disp_dev) {
  1517. printk(KERN_ERR "ran out of memory\n");
  1518. return -ENOMEM;
  1519. }
  1520. spin_lock_init(&disp_dev->dma_queue_lock);
  1521. /*
  1522. * Scan all the platform devices to find the vpbe
  1523. * controller device and get the vpbe_dev object
  1524. */
  1525. err = bus_for_each_dev(&platform_bus_type, NULL, disp_dev,
  1526. vpbe_device_get);
  1527. if (err < 0)
  1528. return err;
  1529. /* Initialize the vpbe display controller */
  1530. if (NULL != disp_dev->vpbe_dev->ops.initialize) {
  1531. err = disp_dev->vpbe_dev->ops.initialize(&pdev->dev,
  1532. disp_dev->vpbe_dev);
  1533. if (err) {
  1534. v4l2_err(&disp_dev->vpbe_dev->v4l2_dev,
  1535. "Error initing vpbe\n");
  1536. err = -ENOMEM;
  1537. goto probe_out;
  1538. }
  1539. }
  1540. for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
  1541. if (init_vpbe_layer(i, disp_dev, pdev)) {
  1542. err = -ENODEV;
  1543. goto probe_out;
  1544. }
  1545. }
  1546. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1547. if (!res) {
  1548. v4l2_err(&disp_dev->vpbe_dev->v4l2_dev,
  1549. "Unable to get VENC interrupt resource\n");
  1550. err = -ENODEV;
  1551. goto probe_out;
  1552. }
  1553. irq = res->start;
  1554. if (request_irq(irq, venc_isr, IRQF_DISABLED, VPBE_DISPLAY_DRIVER,
  1555. disp_dev)) {
  1556. v4l2_err(&disp_dev->vpbe_dev->v4l2_dev,
  1557. "Unable to request interrupt\n");
  1558. err = -ENODEV;
  1559. goto probe_out;
  1560. }
  1561. for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
  1562. if (register_device(disp_dev->dev[i], disp_dev, pdev)) {
  1563. err = -ENODEV;
  1564. goto probe_out_irq;
  1565. }
  1566. }
  1567. printk(KERN_DEBUG "Successfully completed the probing of vpbe v4l2 device\n");
  1568. return 0;
  1569. probe_out_irq:
  1570. free_irq(res->start, disp_dev);
  1571. probe_out:
  1572. for (k = 0; k < VPBE_DISPLAY_MAX_DEVICES; k++) {
  1573. /* Get the pointer to the layer object */
  1574. vpbe_display_layer = disp_dev->dev[k];
  1575. /* Unregister video device */
  1576. if (vpbe_display_layer) {
  1577. video_unregister_device(
  1578. &vpbe_display_layer->video_dev);
  1579. kfree(disp_dev->dev[k]);
  1580. }
  1581. }
  1582. kfree(disp_dev);
  1583. return err;
  1584. }
  1585. /*
  1586. * vpbe_display_remove()
  1587. * It un-register hardware layer from V4L2 driver
  1588. */
  1589. static int vpbe_display_remove(struct platform_device *pdev)
  1590. {
  1591. struct vpbe_layer *vpbe_display_layer;
  1592. struct vpbe_display *disp_dev = platform_get_drvdata(pdev);
  1593. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  1594. struct resource *res;
  1595. int i;
  1596. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_remove\n");
  1597. /* unregister irq */
  1598. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1599. free_irq(res->start, disp_dev);
  1600. /* deinitialize the vpbe display controller */
  1601. if (NULL != vpbe_dev->ops.deinitialize)
  1602. vpbe_dev->ops.deinitialize(&pdev->dev, vpbe_dev);
  1603. /* un-register device */
  1604. for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
  1605. /* Get the pointer to the layer object */
  1606. vpbe_display_layer = disp_dev->dev[i];
  1607. /* Unregister video device */
  1608. video_unregister_device(&vpbe_display_layer->video_dev);
  1609. }
  1610. for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
  1611. kfree(disp_dev->dev[i]);
  1612. disp_dev->dev[i] = NULL;
  1613. }
  1614. return 0;
  1615. }
  1616. static struct platform_driver vpbe_display_driver = {
  1617. .driver = {
  1618. .name = VPBE_DISPLAY_DRIVER,
  1619. .owner = THIS_MODULE,
  1620. .bus = &platform_bus_type,
  1621. },
  1622. .probe = vpbe_display_probe,
  1623. .remove = vpbe_display_remove,
  1624. };
  1625. module_platform_driver(vpbe_display_driver);
  1626. MODULE_DESCRIPTION("TI DM644x/DM355/DM365 VPBE Display controller");
  1627. MODULE_LICENSE("GPL");
  1628. MODULE_AUTHOR("Texas Instruments");