vpbe_display.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858
  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. int ret;
  299. /* Get the next frame from the buffer queue */
  300. layer->next_frm = layer->cur_frm = list_entry(layer->dma_queue.next,
  301. struct vpbe_disp_buffer, list);
  302. /* Remove buffer from the buffer queue */
  303. list_del(&layer->cur_frm->list);
  304. /* Mark state of the current frame to active */
  305. layer->cur_frm->vb.state = VB2_BUF_STATE_ACTIVE;
  306. /* Initialize field_id and started member */
  307. layer->field_id = 0;
  308. /* Set parameters in OSD and VENC */
  309. ret = vpbe_set_osd_display_params(fh->disp_dev, layer);
  310. if (ret < 0) {
  311. struct vpbe_disp_buffer *buf, *tmp;
  312. vb2_buffer_done(&layer->cur_frm->vb, VB2_BUF_STATE_QUEUED);
  313. list_for_each_entry_safe(buf, tmp, &layer->dma_queue, list) {
  314. list_del(&buf->list);
  315. vb2_buffer_done(&buf->vb, VB2_BUF_STATE_QUEUED);
  316. }
  317. return ret;
  318. }
  319. /*
  320. * if request format is yuv420 semiplanar, need to
  321. * enable both video windows
  322. */
  323. layer->started = 1;
  324. layer->layer_first_int = 1;
  325. return ret;
  326. }
  327. static void vpbe_stop_streaming(struct vb2_queue *vq)
  328. {
  329. struct vpbe_fh *fh = vb2_get_drv_priv(vq);
  330. struct vpbe_layer *layer = fh->layer;
  331. struct vpbe_display *disp = fh->disp_dev;
  332. unsigned long flags;
  333. if (!vb2_is_streaming(vq))
  334. return;
  335. /* release all active buffers */
  336. spin_lock_irqsave(&disp->dma_queue_lock, flags);
  337. if (layer->cur_frm == layer->next_frm) {
  338. vb2_buffer_done(&layer->cur_frm->vb, VB2_BUF_STATE_ERROR);
  339. } else {
  340. if (layer->cur_frm != NULL)
  341. vb2_buffer_done(&layer->cur_frm->vb,
  342. VB2_BUF_STATE_ERROR);
  343. if (layer->next_frm != NULL)
  344. vb2_buffer_done(&layer->next_frm->vb,
  345. VB2_BUF_STATE_ERROR);
  346. }
  347. while (!list_empty(&layer->dma_queue)) {
  348. layer->next_frm = list_entry(layer->dma_queue.next,
  349. struct vpbe_disp_buffer, list);
  350. list_del(&layer->next_frm->list);
  351. vb2_buffer_done(&layer->next_frm->vb, VB2_BUF_STATE_ERROR);
  352. }
  353. spin_unlock_irqrestore(&disp->dma_queue_lock, flags);
  354. }
  355. static struct vb2_ops video_qops = {
  356. .queue_setup = vpbe_buffer_queue_setup,
  357. .wait_prepare = vpbe_wait_prepare,
  358. .wait_finish = vpbe_wait_finish,
  359. .buf_init = vpbe_buffer_init,
  360. .buf_prepare = vpbe_buffer_prepare,
  361. .start_streaming = vpbe_start_streaming,
  362. .stop_streaming = vpbe_stop_streaming,
  363. .buf_cleanup = vpbe_buf_cleanup,
  364. .buf_queue = vpbe_buffer_queue,
  365. };
  366. static
  367. struct vpbe_layer*
  368. _vpbe_display_get_other_win_layer(struct vpbe_display *disp_dev,
  369. struct vpbe_layer *layer)
  370. {
  371. enum vpbe_display_device_id thiswin, otherwin;
  372. thiswin = layer->device_id;
  373. otherwin = (thiswin == VPBE_DISPLAY_DEVICE_0) ?
  374. VPBE_DISPLAY_DEVICE_1 : VPBE_DISPLAY_DEVICE_0;
  375. return disp_dev->dev[otherwin];
  376. }
  377. static int vpbe_set_osd_display_params(struct vpbe_display *disp_dev,
  378. struct vpbe_layer *layer)
  379. {
  380. struct osd_layer_config *cfg = &layer->layer_info.config;
  381. struct osd_state *osd_device = disp_dev->osd_device;
  382. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  383. unsigned long addr;
  384. int ret;
  385. addr = vb2_dma_contig_plane_dma_addr(&layer->cur_frm->vb, 0);
  386. /* Set address in the display registers */
  387. osd_device->ops.start_layer(osd_device,
  388. layer->layer_info.id,
  389. addr,
  390. disp_dev->cbcr_ofst);
  391. ret = osd_device->ops.enable_layer(osd_device,
  392. layer->layer_info.id, 0);
  393. if (ret < 0) {
  394. v4l2_err(&vpbe_dev->v4l2_dev,
  395. "Error in enabling osd window layer 0\n");
  396. return -1;
  397. }
  398. /* Enable the window */
  399. layer->layer_info.enable = 1;
  400. if (cfg->pixfmt == PIXFMT_NV12) {
  401. struct vpbe_layer *otherlayer =
  402. _vpbe_display_get_other_win_layer(disp_dev, layer);
  403. ret = osd_device->ops.enable_layer(osd_device,
  404. otherlayer->layer_info.id, 1);
  405. if (ret < 0) {
  406. v4l2_err(&vpbe_dev->v4l2_dev,
  407. "Error in enabling osd window layer 1\n");
  408. return -1;
  409. }
  410. otherlayer->layer_info.enable = 1;
  411. }
  412. return 0;
  413. }
  414. static void
  415. vpbe_disp_calculate_scale_factor(struct vpbe_display *disp_dev,
  416. struct vpbe_layer *layer,
  417. int expected_xsize, int expected_ysize)
  418. {
  419. struct display_layer_info *layer_info = &layer->layer_info;
  420. struct v4l2_pix_format *pixfmt = &layer->pix_fmt;
  421. struct osd_layer_config *cfg = &layer->layer_info.config;
  422. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  423. int calculated_xsize;
  424. int h_exp = 0;
  425. int v_exp = 0;
  426. int h_scale;
  427. int v_scale;
  428. v4l2_std_id standard_id = vpbe_dev->current_timings.std_id;
  429. /*
  430. * Application initially set the image format. Current display
  431. * size is obtained from the vpbe display controller. expected_xsize
  432. * and expected_ysize are set through S_CROP ioctl. Based on this,
  433. * driver will calculate the scale factors for vertical and
  434. * horizontal direction so that the image is displayed scaled
  435. * and expanded. Application uses expansion to display the image
  436. * in a square pixel. Otherwise it is displayed using displays
  437. * pixel aspect ratio.It is expected that application chooses
  438. * the crop coordinates for cropped or scaled display. if crop
  439. * size is less than the image size, it is displayed cropped or
  440. * it is displayed scaled and/or expanded.
  441. *
  442. * to begin with, set the crop window same as expected. Later we
  443. * will override with scaled window size
  444. */
  445. cfg->xsize = pixfmt->width;
  446. cfg->ysize = pixfmt->height;
  447. layer_info->h_zoom = ZOOM_X1; /* no horizontal zoom */
  448. layer_info->v_zoom = ZOOM_X1; /* no horizontal zoom */
  449. layer_info->h_exp = H_EXP_OFF; /* no horizontal zoom */
  450. layer_info->v_exp = V_EXP_OFF; /* no horizontal zoom */
  451. if (pixfmt->width < expected_xsize) {
  452. h_scale = vpbe_dev->current_timings.xres / pixfmt->width;
  453. if (h_scale < 2)
  454. h_scale = 1;
  455. else if (h_scale >= 4)
  456. h_scale = 4;
  457. else
  458. h_scale = 2;
  459. cfg->xsize *= h_scale;
  460. if (cfg->xsize < expected_xsize) {
  461. if ((standard_id & V4L2_STD_525_60) ||
  462. (standard_id & V4L2_STD_625_50)) {
  463. calculated_xsize = (cfg->xsize *
  464. VPBE_DISPLAY_H_EXP_RATIO_N) /
  465. VPBE_DISPLAY_H_EXP_RATIO_D;
  466. if (calculated_xsize <= expected_xsize) {
  467. h_exp = 1;
  468. cfg->xsize = calculated_xsize;
  469. }
  470. }
  471. }
  472. if (h_scale == 2)
  473. layer_info->h_zoom = ZOOM_X2;
  474. else if (h_scale == 4)
  475. layer_info->h_zoom = ZOOM_X4;
  476. if (h_exp)
  477. layer_info->h_exp = H_EXP_9_OVER_8;
  478. } else {
  479. /* no scaling, only cropping. Set display area to crop area */
  480. cfg->xsize = expected_xsize;
  481. }
  482. if (pixfmt->height < expected_ysize) {
  483. v_scale = expected_ysize / pixfmt->height;
  484. if (v_scale < 2)
  485. v_scale = 1;
  486. else if (v_scale >= 4)
  487. v_scale = 4;
  488. else
  489. v_scale = 2;
  490. cfg->ysize *= v_scale;
  491. if (cfg->ysize < expected_ysize) {
  492. if ((standard_id & V4L2_STD_625_50)) {
  493. calculated_xsize = (cfg->ysize *
  494. VPBE_DISPLAY_V_EXP_RATIO_N) /
  495. VPBE_DISPLAY_V_EXP_RATIO_D;
  496. if (calculated_xsize <= expected_ysize) {
  497. v_exp = 1;
  498. cfg->ysize = calculated_xsize;
  499. }
  500. }
  501. }
  502. if (v_scale == 2)
  503. layer_info->v_zoom = ZOOM_X2;
  504. else if (v_scale == 4)
  505. layer_info->v_zoom = ZOOM_X4;
  506. if (v_exp)
  507. layer_info->h_exp = V_EXP_6_OVER_5;
  508. } else {
  509. /* no scaling, only cropping. Set display area to crop area */
  510. cfg->ysize = expected_ysize;
  511. }
  512. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  513. "crop display xsize = %d, ysize = %d\n",
  514. cfg->xsize, cfg->ysize);
  515. }
  516. static void vpbe_disp_adj_position(struct vpbe_display *disp_dev,
  517. struct vpbe_layer *layer,
  518. int top, int left)
  519. {
  520. struct osd_layer_config *cfg = &layer->layer_info.config;
  521. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  522. cfg->xpos = min((unsigned int)left,
  523. vpbe_dev->current_timings.xres - cfg->xsize);
  524. cfg->ypos = min((unsigned int)top,
  525. vpbe_dev->current_timings.yres - cfg->ysize);
  526. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  527. "new xpos = %d, ypos = %d\n",
  528. cfg->xpos, cfg->ypos);
  529. }
  530. static void vpbe_disp_check_window_params(struct vpbe_display *disp_dev,
  531. struct v4l2_rect *c)
  532. {
  533. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  534. if ((c->width == 0) ||
  535. ((c->width + c->left) > vpbe_dev->current_timings.xres))
  536. c->width = vpbe_dev->current_timings.xres - c->left;
  537. if ((c->height == 0) || ((c->height + c->top) >
  538. vpbe_dev->current_timings.yres))
  539. c->height = vpbe_dev->current_timings.yres - c->top;
  540. /* window height must be even for interlaced display */
  541. if (vpbe_dev->current_timings.interlaced)
  542. c->height &= (~0x01);
  543. }
  544. /**
  545. * vpbe_try_format()
  546. * If user application provides width and height, and have bytesperline set
  547. * to zero, driver calculates bytesperline and sizeimage based on hardware
  548. * limits.
  549. */
  550. static int vpbe_try_format(struct vpbe_display *disp_dev,
  551. struct v4l2_pix_format *pixfmt, int check)
  552. {
  553. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  554. int min_height = 1;
  555. int min_width = 32;
  556. int max_height;
  557. int max_width;
  558. int bpp;
  559. if ((pixfmt->pixelformat != V4L2_PIX_FMT_UYVY) &&
  560. (pixfmt->pixelformat != V4L2_PIX_FMT_NV12))
  561. /* choose default as V4L2_PIX_FMT_UYVY */
  562. pixfmt->pixelformat = V4L2_PIX_FMT_UYVY;
  563. /* Check the field format */
  564. if ((pixfmt->field != V4L2_FIELD_INTERLACED) &&
  565. (pixfmt->field != V4L2_FIELD_NONE)) {
  566. if (vpbe_dev->current_timings.interlaced)
  567. pixfmt->field = V4L2_FIELD_INTERLACED;
  568. else
  569. pixfmt->field = V4L2_FIELD_NONE;
  570. }
  571. if (pixfmt->field == V4L2_FIELD_INTERLACED)
  572. min_height = 2;
  573. if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12)
  574. bpp = 1;
  575. else
  576. bpp = 2;
  577. max_width = vpbe_dev->current_timings.xres;
  578. max_height = vpbe_dev->current_timings.yres;
  579. min_width /= bpp;
  580. if (!pixfmt->width || (pixfmt->width < min_width) ||
  581. (pixfmt->width > max_width)) {
  582. pixfmt->width = vpbe_dev->current_timings.xres;
  583. }
  584. if (!pixfmt->height || (pixfmt->height < min_height) ||
  585. (pixfmt->height > max_height)) {
  586. pixfmt->height = vpbe_dev->current_timings.yres;
  587. }
  588. if (pixfmt->bytesperline < (pixfmt->width * bpp))
  589. pixfmt->bytesperline = pixfmt->width * bpp;
  590. /* Make the bytesperline 32 byte aligned */
  591. pixfmt->bytesperline = ((pixfmt->width * bpp + 31) & ~31);
  592. if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12)
  593. pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height +
  594. (pixfmt->bytesperline * pixfmt->height >> 1);
  595. else
  596. pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height;
  597. return 0;
  598. }
  599. static int vpbe_display_querycap(struct file *file, void *priv,
  600. struct v4l2_capability *cap)
  601. {
  602. struct vpbe_fh *fh = file->private_data;
  603. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  604. cap->version = VPBE_DISPLAY_VERSION_CODE;
  605. cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
  606. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  607. snprintf(cap->driver, sizeof(cap->driver), "%s",
  608. dev_name(vpbe_dev->pdev));
  609. snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
  610. dev_name(vpbe_dev->pdev));
  611. strlcpy(cap->card, vpbe_dev->cfg->module_name, sizeof(cap->card));
  612. return 0;
  613. }
  614. static int vpbe_display_s_crop(struct file *file, void *priv,
  615. const struct v4l2_crop *crop)
  616. {
  617. struct vpbe_fh *fh = file->private_data;
  618. struct vpbe_layer *layer = fh->layer;
  619. struct vpbe_display *disp_dev = fh->disp_dev;
  620. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  621. struct osd_layer_config *cfg = &layer->layer_info.config;
  622. struct osd_state *osd_device = disp_dev->osd_device;
  623. struct v4l2_rect rect = crop->c;
  624. int ret;
  625. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  626. "VIDIOC_S_CROP, layer id = %d\n", layer->device_id);
  627. if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  628. v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buf type\n");
  629. return -EINVAL;
  630. }
  631. if (rect.top < 0)
  632. rect.top = 0;
  633. if (rect.left < 0)
  634. rect.left = 0;
  635. vpbe_disp_check_window_params(disp_dev, &rect);
  636. osd_device->ops.get_layer_config(osd_device,
  637. layer->layer_info.id, cfg);
  638. vpbe_disp_calculate_scale_factor(disp_dev, layer,
  639. rect.width,
  640. rect.height);
  641. vpbe_disp_adj_position(disp_dev, layer, rect.top,
  642. rect.left);
  643. ret = osd_device->ops.set_layer_config(osd_device,
  644. layer->layer_info.id, cfg);
  645. if (ret < 0) {
  646. v4l2_err(&vpbe_dev->v4l2_dev,
  647. "Error in set layer config:\n");
  648. return -EINVAL;
  649. }
  650. /* apply zooming and h or v expansion */
  651. osd_device->ops.set_zoom(osd_device,
  652. layer->layer_info.id,
  653. layer->layer_info.h_zoom,
  654. layer->layer_info.v_zoom);
  655. ret = osd_device->ops.set_vid_expansion(osd_device,
  656. layer->layer_info.h_exp,
  657. layer->layer_info.v_exp);
  658. if (ret < 0) {
  659. v4l2_err(&vpbe_dev->v4l2_dev,
  660. "Error in set vid expansion:\n");
  661. return -EINVAL;
  662. }
  663. if ((layer->layer_info.h_zoom != ZOOM_X1) ||
  664. (layer->layer_info.v_zoom != ZOOM_X1) ||
  665. (layer->layer_info.h_exp != H_EXP_OFF) ||
  666. (layer->layer_info.v_exp != V_EXP_OFF))
  667. /* Enable expansion filter */
  668. osd_device->ops.set_interpolation_filter(osd_device, 1);
  669. else
  670. osd_device->ops.set_interpolation_filter(osd_device, 0);
  671. return 0;
  672. }
  673. static int vpbe_display_g_crop(struct file *file, void *priv,
  674. struct v4l2_crop *crop)
  675. {
  676. struct vpbe_fh *fh = file->private_data;
  677. struct vpbe_layer *layer = fh->layer;
  678. struct osd_layer_config *cfg = &layer->layer_info.config;
  679. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  680. struct osd_state *osd_device = fh->disp_dev->osd_device;
  681. struct v4l2_rect *rect = &crop->c;
  682. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  683. "VIDIOC_G_CROP, layer id = %d\n",
  684. layer->device_id);
  685. if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  686. v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buf type\n");
  687. return -EINVAL;
  688. }
  689. osd_device->ops.get_layer_config(osd_device,
  690. layer->layer_info.id, cfg);
  691. rect->top = cfg->ypos;
  692. rect->left = cfg->xpos;
  693. rect->width = cfg->xsize;
  694. rect->height = cfg->ysize;
  695. return 0;
  696. }
  697. static int vpbe_display_cropcap(struct file *file, void *priv,
  698. struct v4l2_cropcap *cropcap)
  699. {
  700. struct vpbe_fh *fh = file->private_data;
  701. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  702. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_CROPCAP ioctl\n");
  703. cropcap->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  704. cropcap->bounds.left = 0;
  705. cropcap->bounds.top = 0;
  706. cropcap->bounds.width = vpbe_dev->current_timings.xres;
  707. cropcap->bounds.height = vpbe_dev->current_timings.yres;
  708. cropcap->pixelaspect = vpbe_dev->current_timings.aspect;
  709. cropcap->defrect = cropcap->bounds;
  710. return 0;
  711. }
  712. static int vpbe_display_g_fmt(struct file *file, void *priv,
  713. struct v4l2_format *fmt)
  714. {
  715. struct vpbe_fh *fh = file->private_data;
  716. struct vpbe_layer *layer = fh->layer;
  717. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  718. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  719. "VIDIOC_G_FMT, layer id = %d\n",
  720. layer->device_id);
  721. /* If buffer type is video output */
  722. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != fmt->type) {
  723. v4l2_err(&vpbe_dev->v4l2_dev, "invalid type\n");
  724. return -EINVAL;
  725. }
  726. /* Fill in the information about format */
  727. fmt->fmt.pix = layer->pix_fmt;
  728. return 0;
  729. }
  730. static int vpbe_display_enum_fmt(struct file *file, void *priv,
  731. struct v4l2_fmtdesc *fmt)
  732. {
  733. struct vpbe_fh *fh = file->private_data;
  734. struct vpbe_layer *layer = fh->layer;
  735. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  736. unsigned int index = 0;
  737. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  738. "VIDIOC_ENUM_FMT, layer id = %d\n",
  739. layer->device_id);
  740. if (fmt->index > 1) {
  741. v4l2_err(&vpbe_dev->v4l2_dev, "Invalid format index\n");
  742. return -EINVAL;
  743. }
  744. /* Fill in the information about format */
  745. index = fmt->index;
  746. memset(fmt, 0, sizeof(*fmt));
  747. fmt->index = index;
  748. fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  749. if (index == 0) {
  750. strcpy(fmt->description, "YUV 4:2:2 - UYVY");
  751. fmt->pixelformat = V4L2_PIX_FMT_UYVY;
  752. } else {
  753. strcpy(fmt->description, "Y/CbCr 4:2:0");
  754. fmt->pixelformat = V4L2_PIX_FMT_NV12;
  755. }
  756. return 0;
  757. }
  758. static int vpbe_display_s_fmt(struct file *file, void *priv,
  759. struct v4l2_format *fmt)
  760. {
  761. struct vpbe_fh *fh = file->private_data;
  762. struct vpbe_layer *layer = fh->layer;
  763. struct vpbe_display *disp_dev = fh->disp_dev;
  764. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  765. struct osd_layer_config *cfg = &layer->layer_info.config;
  766. struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
  767. struct osd_state *osd_device = disp_dev->osd_device;
  768. int ret;
  769. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  770. "VIDIOC_S_FMT, layer id = %d\n",
  771. layer->device_id);
  772. /* If streaming is started, return error */
  773. if (layer->started) {
  774. v4l2_err(&vpbe_dev->v4l2_dev, "Streaming is started\n");
  775. return -EBUSY;
  776. }
  777. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != fmt->type) {
  778. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "invalid type\n");
  779. return -EINVAL;
  780. }
  781. /* Check for valid pixel format */
  782. ret = vpbe_try_format(disp_dev, pixfmt, 1);
  783. if (ret)
  784. return ret;
  785. /* YUV420 is requested, check availability of the
  786. other video window */
  787. layer->pix_fmt = *pixfmt;
  788. if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12) {
  789. struct vpbe_layer *otherlayer;
  790. otherlayer = _vpbe_display_get_other_win_layer(disp_dev, layer);
  791. /* if other layer is available, only
  792. * claim it, do not configure it
  793. */
  794. ret = osd_device->ops.request_layer(osd_device,
  795. otherlayer->layer_info.id);
  796. if (ret < 0) {
  797. v4l2_err(&vpbe_dev->v4l2_dev,
  798. "Display Manager failed to allocate layer\n");
  799. return -EBUSY;
  800. }
  801. }
  802. /* Get osd layer config */
  803. osd_device->ops.get_layer_config(osd_device,
  804. layer->layer_info.id, cfg);
  805. /* Store the pixel format in the layer object */
  806. cfg->xsize = pixfmt->width;
  807. cfg->ysize = pixfmt->height;
  808. cfg->line_length = pixfmt->bytesperline;
  809. cfg->ypos = 0;
  810. cfg->xpos = 0;
  811. cfg->interlaced = vpbe_dev->current_timings.interlaced;
  812. if (V4L2_PIX_FMT_UYVY == pixfmt->pixelformat)
  813. cfg->pixfmt = PIXFMT_YCBCRI;
  814. /* Change of the default pixel format for both video windows */
  815. if (V4L2_PIX_FMT_NV12 == pixfmt->pixelformat) {
  816. struct vpbe_layer *otherlayer;
  817. cfg->pixfmt = PIXFMT_NV12;
  818. otherlayer = _vpbe_display_get_other_win_layer(disp_dev,
  819. layer);
  820. otherlayer->layer_info.config.pixfmt = PIXFMT_NV12;
  821. }
  822. /* Set the layer config in the osd window */
  823. ret = osd_device->ops.set_layer_config(osd_device,
  824. layer->layer_info.id, cfg);
  825. if (ret < 0) {
  826. v4l2_err(&vpbe_dev->v4l2_dev,
  827. "Error in S_FMT params:\n");
  828. return -EINVAL;
  829. }
  830. /* Readback and fill the local copy of current pix format */
  831. osd_device->ops.get_layer_config(osd_device,
  832. layer->layer_info.id, cfg);
  833. return 0;
  834. }
  835. static int vpbe_display_try_fmt(struct file *file, void *priv,
  836. struct v4l2_format *fmt)
  837. {
  838. struct vpbe_fh *fh = file->private_data;
  839. struct vpbe_display *disp_dev = fh->disp_dev;
  840. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  841. struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
  842. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_TRY_FMT\n");
  843. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != fmt->type) {
  844. v4l2_err(&vpbe_dev->v4l2_dev, "invalid type\n");
  845. return -EINVAL;
  846. }
  847. /* Check for valid field format */
  848. return vpbe_try_format(disp_dev, pixfmt, 0);
  849. }
  850. /**
  851. * vpbe_display_s_std - Set the given standard in the encoder
  852. *
  853. * Sets the standard if supported by the current encoder. Return the status.
  854. * 0 - success & -EINVAL on error
  855. */
  856. static int vpbe_display_s_std(struct file *file, void *priv,
  857. v4l2_std_id std_id)
  858. {
  859. struct vpbe_fh *fh = priv;
  860. struct vpbe_layer *layer = fh->layer;
  861. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  862. int ret;
  863. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_S_STD\n");
  864. /* If streaming is started, return error */
  865. if (layer->started) {
  866. v4l2_err(&vpbe_dev->v4l2_dev, "Streaming is started\n");
  867. return -EBUSY;
  868. }
  869. if (NULL != vpbe_dev->ops.s_std) {
  870. ret = vpbe_dev->ops.s_std(vpbe_dev, std_id);
  871. if (ret) {
  872. v4l2_err(&vpbe_dev->v4l2_dev,
  873. "Failed to set standard for sub devices\n");
  874. return -EINVAL;
  875. }
  876. } else {
  877. return -EINVAL;
  878. }
  879. return 0;
  880. }
  881. /**
  882. * vpbe_display_g_std - Get the standard in the current encoder
  883. *
  884. * Get the standard in the current encoder. Return the status. 0 - success
  885. * -EINVAL on error
  886. */
  887. static int vpbe_display_g_std(struct file *file, void *priv,
  888. v4l2_std_id *std_id)
  889. {
  890. struct vpbe_fh *fh = priv;
  891. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  892. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_G_STD\n");
  893. /* Get the standard from the current encoder */
  894. if (vpbe_dev->current_timings.timings_type & VPBE_ENC_STD) {
  895. *std_id = vpbe_dev->current_timings.std_id;
  896. return 0;
  897. }
  898. return -EINVAL;
  899. }
  900. /**
  901. * vpbe_display_enum_output - enumerate outputs
  902. *
  903. * Enumerates the outputs available at the vpbe display
  904. * returns the status, -EINVAL if end of output list
  905. */
  906. static int vpbe_display_enum_output(struct file *file, void *priv,
  907. struct v4l2_output *output)
  908. {
  909. struct vpbe_fh *fh = priv;
  910. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  911. int ret;
  912. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_ENUM_OUTPUT\n");
  913. /* Enumerate outputs */
  914. if (NULL == vpbe_dev->ops.enum_outputs)
  915. return -EINVAL;
  916. ret = vpbe_dev->ops.enum_outputs(vpbe_dev, output);
  917. if (ret) {
  918. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  919. "Failed to enumerate outputs\n");
  920. return -EINVAL;
  921. }
  922. return 0;
  923. }
  924. /**
  925. * vpbe_display_s_output - Set output to
  926. * the output specified by the index
  927. */
  928. static int vpbe_display_s_output(struct file *file, void *priv,
  929. unsigned int i)
  930. {
  931. struct vpbe_fh *fh = priv;
  932. struct vpbe_layer *layer = fh->layer;
  933. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  934. int ret;
  935. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_S_OUTPUT\n");
  936. /* If streaming is started, return error */
  937. if (layer->started) {
  938. v4l2_err(&vpbe_dev->v4l2_dev, "Streaming is started\n");
  939. return -EBUSY;
  940. }
  941. if (NULL == vpbe_dev->ops.set_output)
  942. return -EINVAL;
  943. ret = vpbe_dev->ops.set_output(vpbe_dev, i);
  944. if (ret) {
  945. v4l2_err(&vpbe_dev->v4l2_dev,
  946. "Failed to set output for sub devices\n");
  947. return -EINVAL;
  948. }
  949. return 0;
  950. }
  951. /**
  952. * vpbe_display_g_output - Get output from subdevice
  953. * for a given by the index
  954. */
  955. static int vpbe_display_g_output(struct file *file, void *priv,
  956. unsigned int *i)
  957. {
  958. struct vpbe_fh *fh = priv;
  959. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  960. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_G_OUTPUT\n");
  961. /* Get the standard from the current encoder */
  962. *i = vpbe_dev->current_out_index;
  963. return 0;
  964. }
  965. /**
  966. * vpbe_display_enum_dv_timings - Enumerate the dv timings
  967. *
  968. * enum the timings in the current encoder. Return the status. 0 - success
  969. * -EINVAL on error
  970. */
  971. static int
  972. vpbe_display_enum_dv_timings(struct file *file, void *priv,
  973. struct v4l2_enum_dv_timings *timings)
  974. {
  975. struct vpbe_fh *fh = priv;
  976. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  977. int ret;
  978. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_ENUM_DV_TIMINGS\n");
  979. /* Enumerate outputs */
  980. if (NULL == vpbe_dev->ops.enum_dv_timings)
  981. return -EINVAL;
  982. ret = vpbe_dev->ops.enum_dv_timings(vpbe_dev, timings);
  983. if (ret) {
  984. v4l2_err(&vpbe_dev->v4l2_dev,
  985. "Failed to enumerate dv timings info\n");
  986. return -EINVAL;
  987. }
  988. return 0;
  989. }
  990. /**
  991. * vpbe_display_s_dv_timings - Set the dv timings
  992. *
  993. * Set the timings in the current encoder. Return the status. 0 - success
  994. * -EINVAL on error
  995. */
  996. static int
  997. vpbe_display_s_dv_timings(struct file *file, void *priv,
  998. struct v4l2_dv_timings *timings)
  999. {
  1000. struct vpbe_fh *fh = priv;
  1001. struct vpbe_layer *layer = fh->layer;
  1002. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  1003. int ret;
  1004. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_S_DV_TIMINGS\n");
  1005. /* If streaming is started, return error */
  1006. if (layer->started) {
  1007. v4l2_err(&vpbe_dev->v4l2_dev, "Streaming is started\n");
  1008. return -EBUSY;
  1009. }
  1010. /* Set the given standard in the encoder */
  1011. if (!vpbe_dev->ops.s_dv_timings)
  1012. return -EINVAL;
  1013. ret = vpbe_dev->ops.s_dv_timings(vpbe_dev, timings);
  1014. if (ret) {
  1015. v4l2_err(&vpbe_dev->v4l2_dev,
  1016. "Failed to set the dv timings info\n");
  1017. return -EINVAL;
  1018. }
  1019. return 0;
  1020. }
  1021. /**
  1022. * vpbe_display_g_dv_timings - Set the dv timings
  1023. *
  1024. * Get the timings in the current encoder. Return the status. 0 - success
  1025. * -EINVAL on error
  1026. */
  1027. static int
  1028. vpbe_display_g_dv_timings(struct file *file, void *priv,
  1029. struct v4l2_dv_timings *dv_timings)
  1030. {
  1031. struct vpbe_fh *fh = priv;
  1032. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  1033. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_G_DV_TIMINGS\n");
  1034. /* Get the given standard in the encoder */
  1035. if (vpbe_dev->current_timings.timings_type &
  1036. VPBE_ENC_DV_TIMINGS) {
  1037. *dv_timings = vpbe_dev->current_timings.dv_timings;
  1038. } else {
  1039. return -EINVAL;
  1040. }
  1041. return 0;
  1042. }
  1043. static int vpbe_display_streamoff(struct file *file, void *priv,
  1044. enum v4l2_buf_type buf_type)
  1045. {
  1046. struct vpbe_fh *fh = file->private_data;
  1047. struct vpbe_layer *layer = fh->layer;
  1048. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  1049. struct osd_state *osd_device = fh->disp_dev->osd_device;
  1050. int ret;
  1051. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  1052. "VIDIOC_STREAMOFF,layer id = %d\n",
  1053. layer->device_id);
  1054. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != buf_type) {
  1055. v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
  1056. return -EINVAL;
  1057. }
  1058. /* If io is allowed for this file handle, return error */
  1059. if (!fh->io_allowed) {
  1060. v4l2_err(&vpbe_dev->v4l2_dev, "No io_allowed\n");
  1061. return -EACCES;
  1062. }
  1063. /* If streaming is not started, return error */
  1064. if (!layer->started) {
  1065. v4l2_err(&vpbe_dev->v4l2_dev, "streaming not started in layer"
  1066. " id = %d\n", layer->device_id);
  1067. return -EINVAL;
  1068. }
  1069. osd_device->ops.disable_layer(osd_device,
  1070. layer->layer_info.id);
  1071. layer->started = 0;
  1072. ret = vb2_streamoff(&layer->buffer_queue, buf_type);
  1073. return ret;
  1074. }
  1075. static int vpbe_display_streamon(struct file *file, void *priv,
  1076. enum v4l2_buf_type buf_type)
  1077. {
  1078. struct vpbe_fh *fh = file->private_data;
  1079. struct vpbe_layer *layer = fh->layer;
  1080. struct vpbe_display *disp_dev = fh->disp_dev;
  1081. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  1082. struct osd_state *osd_device = disp_dev->osd_device;
  1083. int ret;
  1084. osd_device->ops.disable_layer(osd_device,
  1085. layer->layer_info.id);
  1086. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_STREAMON, layerid=%d\n",
  1087. layer->device_id);
  1088. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != buf_type) {
  1089. v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
  1090. return -EINVAL;
  1091. }
  1092. /* If file handle is not allowed IO, return error */
  1093. if (!fh->io_allowed) {
  1094. v4l2_err(&vpbe_dev->v4l2_dev, "No io_allowed\n");
  1095. return -EACCES;
  1096. }
  1097. /* If Streaming is already started, return error */
  1098. if (layer->started) {
  1099. v4l2_err(&vpbe_dev->v4l2_dev, "layer is already streaming\n");
  1100. return -EBUSY;
  1101. }
  1102. /*
  1103. * Call vb2_streamon to start streaming
  1104. * in videobuf
  1105. */
  1106. ret = vb2_streamon(&layer->buffer_queue, buf_type);
  1107. if (ret) {
  1108. v4l2_err(&vpbe_dev->v4l2_dev,
  1109. "error in vb2_streamon\n");
  1110. return ret;
  1111. }
  1112. return ret;
  1113. }
  1114. static int vpbe_display_dqbuf(struct file *file, void *priv,
  1115. struct v4l2_buffer *buf)
  1116. {
  1117. struct vpbe_fh *fh = file->private_data;
  1118. struct vpbe_layer *layer = fh->layer;
  1119. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  1120. int ret;
  1121. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  1122. "VIDIOC_DQBUF, layer id = %d\n",
  1123. layer->device_id);
  1124. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != buf->type) {
  1125. v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
  1126. return -EINVAL;
  1127. }
  1128. /* If this file handle is not allowed to do IO, return error */
  1129. if (!fh->io_allowed) {
  1130. v4l2_err(&vpbe_dev->v4l2_dev, "No io_allowed\n");
  1131. return -EACCES;
  1132. }
  1133. if (file->f_flags & O_NONBLOCK)
  1134. /* Call videobuf_dqbuf for non blocking mode */
  1135. ret = vb2_dqbuf(&layer->buffer_queue, buf, 1);
  1136. else
  1137. /* Call videobuf_dqbuf for blocking mode */
  1138. ret = vb2_dqbuf(&layer->buffer_queue, buf, 0);
  1139. return ret;
  1140. }
  1141. static int vpbe_display_qbuf(struct file *file, void *priv,
  1142. struct v4l2_buffer *p)
  1143. {
  1144. struct vpbe_fh *fh = file->private_data;
  1145. struct vpbe_layer *layer = fh->layer;
  1146. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  1147. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  1148. "VIDIOC_QBUF, layer id = %d\n",
  1149. layer->device_id);
  1150. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != p->type) {
  1151. v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
  1152. return -EINVAL;
  1153. }
  1154. /* If this file handle is not allowed to do IO, return error */
  1155. if (!fh->io_allowed) {
  1156. v4l2_err(&vpbe_dev->v4l2_dev, "No io_allowed\n");
  1157. return -EACCES;
  1158. }
  1159. return vb2_qbuf(&layer->buffer_queue, p);
  1160. }
  1161. static int vpbe_display_querybuf(struct file *file, void *priv,
  1162. struct v4l2_buffer *buf)
  1163. {
  1164. struct vpbe_fh *fh = file->private_data;
  1165. struct vpbe_layer *layer = fh->layer;
  1166. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  1167. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  1168. "VIDIOC_QUERYBUF, layer id = %d\n",
  1169. layer->device_id);
  1170. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != buf->type) {
  1171. v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
  1172. return -EINVAL;
  1173. }
  1174. /* Call vb2_querybuf to get information */
  1175. return vb2_querybuf(&layer->buffer_queue, buf);
  1176. }
  1177. static int vpbe_display_reqbufs(struct file *file, void *priv,
  1178. struct v4l2_requestbuffers *req_buf)
  1179. {
  1180. struct vpbe_fh *fh = file->private_data;
  1181. struct vpbe_layer *layer = fh->layer;
  1182. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  1183. struct vb2_queue *q;
  1184. int ret;
  1185. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_reqbufs\n");
  1186. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != req_buf->type) {
  1187. v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buffer type\n");
  1188. return -EINVAL;
  1189. }
  1190. /* If io users of the layer is not zero, return error */
  1191. if (0 != layer->io_usrs) {
  1192. v4l2_err(&vpbe_dev->v4l2_dev, "not IO user\n");
  1193. return -EBUSY;
  1194. }
  1195. /* Initialize videobuf queue as per the buffer type */
  1196. layer->alloc_ctx = vb2_dma_contig_init_ctx(vpbe_dev->pdev);
  1197. if (IS_ERR(layer->alloc_ctx)) {
  1198. v4l2_err(&vpbe_dev->v4l2_dev, "Failed to get the context\n");
  1199. return PTR_ERR(layer->alloc_ctx);
  1200. }
  1201. q = &layer->buffer_queue;
  1202. memset(q, 0, sizeof(*q));
  1203. q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  1204. q->io_modes = VB2_MMAP | VB2_USERPTR;
  1205. q->drv_priv = fh;
  1206. q->ops = &video_qops;
  1207. q->mem_ops = &vb2_dma_contig_memops;
  1208. q->buf_struct_size = sizeof(struct vpbe_disp_buffer);
  1209. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1210. q->min_buffers_needed = 1;
  1211. ret = vb2_queue_init(q);
  1212. if (ret) {
  1213. v4l2_err(&vpbe_dev->v4l2_dev, "vb2_queue_init() failed\n");
  1214. vb2_dma_contig_cleanup_ctx(layer->alloc_ctx);
  1215. return ret;
  1216. }
  1217. /* Set io allowed member of file handle to TRUE */
  1218. fh->io_allowed = 1;
  1219. /* Increment io usrs member of layer object to 1 */
  1220. layer->io_usrs = 1;
  1221. /* Store type of memory requested in layer object */
  1222. layer->memory = req_buf->memory;
  1223. /* Initialize buffer queue */
  1224. INIT_LIST_HEAD(&layer->dma_queue);
  1225. /* Allocate buffers */
  1226. return vb2_reqbufs(q, req_buf);
  1227. }
  1228. /*
  1229. * vpbe_display_mmap()
  1230. * It is used to map kernel space buffers into user spaces
  1231. */
  1232. static int vpbe_display_mmap(struct file *filep, struct vm_area_struct *vma)
  1233. {
  1234. /* Get the layer object and file handle object */
  1235. struct vpbe_fh *fh = filep->private_data;
  1236. struct vpbe_layer *layer = fh->layer;
  1237. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  1238. int ret;
  1239. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_mmap\n");
  1240. if (mutex_lock_interruptible(&layer->opslock))
  1241. return -ERESTARTSYS;
  1242. ret = vb2_mmap(&layer->buffer_queue, vma);
  1243. mutex_unlock(&layer->opslock);
  1244. return ret;
  1245. }
  1246. /* vpbe_display_poll(): It is used for select/poll system call
  1247. */
  1248. static unsigned int vpbe_display_poll(struct file *filep, poll_table *wait)
  1249. {
  1250. struct vpbe_fh *fh = filep->private_data;
  1251. struct vpbe_layer *layer = fh->layer;
  1252. struct vpbe_device *vpbe_dev = fh->disp_dev->vpbe_dev;
  1253. unsigned int err = 0;
  1254. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_poll\n");
  1255. if (layer->started) {
  1256. mutex_lock(&layer->opslock);
  1257. err = vb2_poll(&layer->buffer_queue, filep, wait);
  1258. mutex_unlock(&layer->opslock);
  1259. }
  1260. return err;
  1261. }
  1262. /*
  1263. * vpbe_display_open()
  1264. * It creates object of file handle structure and stores it in private_data
  1265. * member of filepointer
  1266. */
  1267. static int vpbe_display_open(struct file *file)
  1268. {
  1269. struct vpbe_fh *fh = NULL;
  1270. struct vpbe_layer *layer = video_drvdata(file);
  1271. struct video_device *vdev = video_devdata(file);
  1272. struct vpbe_display *disp_dev = layer->disp_dev;
  1273. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  1274. struct osd_state *osd_device = disp_dev->osd_device;
  1275. int err;
  1276. /* Allocate memory for the file handle object */
  1277. fh = kmalloc(sizeof(struct vpbe_fh), GFP_KERNEL);
  1278. if (fh == NULL) {
  1279. v4l2_err(&vpbe_dev->v4l2_dev,
  1280. "unable to allocate memory for file handle object\n");
  1281. return -ENOMEM;
  1282. }
  1283. v4l2_fh_init(&fh->fh, vdev);
  1284. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  1285. "vpbe display open plane = %d\n",
  1286. layer->device_id);
  1287. /* store pointer to fh in private_data member of filep */
  1288. file->private_data = fh;
  1289. fh->layer = layer;
  1290. fh->disp_dev = disp_dev;
  1291. if (!layer->usrs) {
  1292. if (mutex_lock_interruptible(&layer->opslock))
  1293. return -ERESTARTSYS;
  1294. /* First claim the layer for this device */
  1295. err = osd_device->ops.request_layer(osd_device,
  1296. layer->layer_info.id);
  1297. mutex_unlock(&layer->opslock);
  1298. if (err < 0) {
  1299. /* Couldn't get layer */
  1300. v4l2_err(&vpbe_dev->v4l2_dev,
  1301. "Display Manager failed to allocate layer\n");
  1302. kfree(fh);
  1303. return -EINVAL;
  1304. }
  1305. }
  1306. /* Increment layer usrs counter */
  1307. layer->usrs++;
  1308. /* Set io_allowed member to false */
  1309. fh->io_allowed = 0;
  1310. v4l2_fh_add(&fh->fh);
  1311. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  1312. "vpbe display device opened successfully\n");
  1313. return 0;
  1314. }
  1315. /*
  1316. * vpbe_display_release()
  1317. * This function deletes buffer queue, frees the buffers and the davinci
  1318. * display file * handle
  1319. */
  1320. static int vpbe_display_release(struct file *file)
  1321. {
  1322. /* Get the layer object and file handle object */
  1323. struct vpbe_fh *fh = file->private_data;
  1324. struct vpbe_layer *layer = fh->layer;
  1325. struct osd_layer_config *cfg = &layer->layer_info.config;
  1326. struct vpbe_display *disp_dev = fh->disp_dev;
  1327. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  1328. struct osd_state *osd_device = disp_dev->osd_device;
  1329. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_release\n");
  1330. mutex_lock(&layer->opslock);
  1331. /* if this instance is doing IO */
  1332. if (fh->io_allowed) {
  1333. /* Reset io_usrs member of layer object */
  1334. layer->io_usrs = 0;
  1335. osd_device->ops.disable_layer(osd_device,
  1336. layer->layer_info.id);
  1337. layer->started = 0;
  1338. /* Free buffers allocated */
  1339. vb2_queue_release(&layer->buffer_queue);
  1340. vb2_dma_contig_cleanup_ctx(&layer->buffer_queue);
  1341. }
  1342. /* Decrement layer usrs counter */
  1343. layer->usrs--;
  1344. /* If this file handle has initialize encoder device, reset it */
  1345. if (!layer->usrs) {
  1346. if (cfg->pixfmt == PIXFMT_NV12) {
  1347. struct vpbe_layer *otherlayer;
  1348. otherlayer =
  1349. _vpbe_display_get_other_win_layer(disp_dev, layer);
  1350. osd_device->ops.disable_layer(osd_device,
  1351. otherlayer->layer_info.id);
  1352. osd_device->ops.release_layer(osd_device,
  1353. otherlayer->layer_info.id);
  1354. }
  1355. osd_device->ops.disable_layer(osd_device,
  1356. layer->layer_info.id);
  1357. osd_device->ops.release_layer(osd_device,
  1358. layer->layer_info.id);
  1359. }
  1360. v4l2_fh_del(&fh->fh);
  1361. v4l2_fh_exit(&fh->fh);
  1362. file->private_data = NULL;
  1363. mutex_unlock(&layer->opslock);
  1364. /* Free memory allocated to file handle object */
  1365. kfree(fh);
  1366. disp_dev->cbcr_ofst = 0;
  1367. return 0;
  1368. }
  1369. /* vpbe capture ioctl operations */
  1370. static const struct v4l2_ioctl_ops vpbe_ioctl_ops = {
  1371. .vidioc_querycap = vpbe_display_querycap,
  1372. .vidioc_g_fmt_vid_out = vpbe_display_g_fmt,
  1373. .vidioc_enum_fmt_vid_out = vpbe_display_enum_fmt,
  1374. .vidioc_s_fmt_vid_out = vpbe_display_s_fmt,
  1375. .vidioc_try_fmt_vid_out = vpbe_display_try_fmt,
  1376. .vidioc_reqbufs = vpbe_display_reqbufs,
  1377. .vidioc_querybuf = vpbe_display_querybuf,
  1378. .vidioc_qbuf = vpbe_display_qbuf,
  1379. .vidioc_dqbuf = vpbe_display_dqbuf,
  1380. .vidioc_streamon = vpbe_display_streamon,
  1381. .vidioc_streamoff = vpbe_display_streamoff,
  1382. .vidioc_cropcap = vpbe_display_cropcap,
  1383. .vidioc_g_crop = vpbe_display_g_crop,
  1384. .vidioc_s_crop = vpbe_display_s_crop,
  1385. .vidioc_s_std = vpbe_display_s_std,
  1386. .vidioc_g_std = vpbe_display_g_std,
  1387. .vidioc_enum_output = vpbe_display_enum_output,
  1388. .vidioc_s_output = vpbe_display_s_output,
  1389. .vidioc_g_output = vpbe_display_g_output,
  1390. .vidioc_s_dv_timings = vpbe_display_s_dv_timings,
  1391. .vidioc_g_dv_timings = vpbe_display_g_dv_timings,
  1392. .vidioc_enum_dv_timings = vpbe_display_enum_dv_timings,
  1393. };
  1394. static struct v4l2_file_operations vpbe_fops = {
  1395. .owner = THIS_MODULE,
  1396. .open = vpbe_display_open,
  1397. .release = vpbe_display_release,
  1398. .unlocked_ioctl = video_ioctl2,
  1399. .mmap = vpbe_display_mmap,
  1400. .poll = vpbe_display_poll
  1401. };
  1402. static int vpbe_device_get(struct device *dev, void *data)
  1403. {
  1404. struct platform_device *pdev = to_platform_device(dev);
  1405. struct vpbe_display *vpbe_disp = data;
  1406. if (strcmp("vpbe_controller", pdev->name) == 0)
  1407. vpbe_disp->vpbe_dev = platform_get_drvdata(pdev);
  1408. if (strstr(pdev->name, "vpbe-osd") != NULL)
  1409. vpbe_disp->osd_device = platform_get_drvdata(pdev);
  1410. return 0;
  1411. }
  1412. static int init_vpbe_layer(int i, struct vpbe_display *disp_dev,
  1413. struct platform_device *pdev)
  1414. {
  1415. struct vpbe_layer *vpbe_display_layer = NULL;
  1416. struct video_device *vbd = NULL;
  1417. /* Allocate memory for four plane display objects */
  1418. disp_dev->dev[i] =
  1419. kzalloc(sizeof(struct vpbe_layer), GFP_KERNEL);
  1420. /* If memory allocation fails, return error */
  1421. if (!disp_dev->dev[i]) {
  1422. printk(KERN_ERR "ran out of memory\n");
  1423. return -ENOMEM;
  1424. }
  1425. spin_lock_init(&disp_dev->dev[i]->irqlock);
  1426. mutex_init(&disp_dev->dev[i]->opslock);
  1427. /* Get the pointer to the layer object */
  1428. vpbe_display_layer = disp_dev->dev[i];
  1429. vbd = &vpbe_display_layer->video_dev;
  1430. /* Initialize field of video device */
  1431. vbd->release = video_device_release_empty;
  1432. vbd->fops = &vpbe_fops;
  1433. vbd->ioctl_ops = &vpbe_ioctl_ops;
  1434. vbd->minor = -1;
  1435. vbd->v4l2_dev = &disp_dev->vpbe_dev->v4l2_dev;
  1436. vbd->lock = &vpbe_display_layer->opslock;
  1437. vbd->vfl_dir = VFL_DIR_TX;
  1438. if (disp_dev->vpbe_dev->current_timings.timings_type &
  1439. VPBE_ENC_STD)
  1440. vbd->tvnorms = (V4L2_STD_525_60 | V4L2_STD_625_50);
  1441. snprintf(vbd->name, sizeof(vbd->name),
  1442. "DaVinci_VPBE Display_DRIVER_V%d.%d.%d",
  1443. (VPBE_DISPLAY_VERSION_CODE >> 16) & 0xff,
  1444. (VPBE_DISPLAY_VERSION_CODE >> 8) & 0xff,
  1445. (VPBE_DISPLAY_VERSION_CODE) & 0xff);
  1446. vpbe_display_layer->device_id = i;
  1447. vpbe_display_layer->layer_info.id =
  1448. ((i == VPBE_DISPLAY_DEVICE_0) ? WIN_VID0 : WIN_VID1);
  1449. return 0;
  1450. }
  1451. static int register_device(struct vpbe_layer *vpbe_display_layer,
  1452. struct vpbe_display *disp_dev,
  1453. struct platform_device *pdev)
  1454. {
  1455. int err;
  1456. v4l2_info(&disp_dev->vpbe_dev->v4l2_dev,
  1457. "Trying to register VPBE display device.\n");
  1458. v4l2_info(&disp_dev->vpbe_dev->v4l2_dev,
  1459. "layer=%x,layer->video_dev=%x\n",
  1460. (int)vpbe_display_layer,
  1461. (int)&vpbe_display_layer->video_dev);
  1462. err = video_register_device(&vpbe_display_layer->video_dev,
  1463. VFL_TYPE_GRABBER,
  1464. -1);
  1465. if (err)
  1466. return -ENODEV;
  1467. vpbe_display_layer->disp_dev = disp_dev;
  1468. /* set the driver data in platform device */
  1469. platform_set_drvdata(pdev, disp_dev);
  1470. set_bit(V4L2_FL_USE_FH_PRIO, &vpbe_display_layer->video_dev.flags);
  1471. video_set_drvdata(&vpbe_display_layer->video_dev,
  1472. vpbe_display_layer);
  1473. return 0;
  1474. }
  1475. /*
  1476. * vpbe_display_probe()
  1477. * This function creates device entries by register itself to the V4L2 driver
  1478. * and initializes fields of each layer objects
  1479. */
  1480. static int vpbe_display_probe(struct platform_device *pdev)
  1481. {
  1482. struct vpbe_layer *vpbe_display_layer;
  1483. struct vpbe_display *disp_dev;
  1484. struct resource *res = NULL;
  1485. int k;
  1486. int i;
  1487. int err;
  1488. int irq;
  1489. printk(KERN_DEBUG "vpbe_display_probe\n");
  1490. /* Allocate memory for vpbe_display */
  1491. disp_dev = devm_kzalloc(&pdev->dev, sizeof(struct vpbe_display),
  1492. GFP_KERNEL);
  1493. if (!disp_dev)
  1494. return -ENOMEM;
  1495. spin_lock_init(&disp_dev->dma_queue_lock);
  1496. /*
  1497. * Scan all the platform devices to find the vpbe
  1498. * controller device and get the vpbe_dev object
  1499. */
  1500. err = bus_for_each_dev(&platform_bus_type, NULL, disp_dev,
  1501. vpbe_device_get);
  1502. if (err < 0)
  1503. return err;
  1504. /* Initialize the vpbe display controller */
  1505. if (NULL != disp_dev->vpbe_dev->ops.initialize) {
  1506. err = disp_dev->vpbe_dev->ops.initialize(&pdev->dev,
  1507. disp_dev->vpbe_dev);
  1508. if (err) {
  1509. v4l2_err(&disp_dev->vpbe_dev->v4l2_dev,
  1510. "Error initing vpbe\n");
  1511. err = -ENOMEM;
  1512. goto probe_out;
  1513. }
  1514. }
  1515. for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
  1516. if (init_vpbe_layer(i, disp_dev, pdev)) {
  1517. err = -ENODEV;
  1518. goto probe_out;
  1519. }
  1520. }
  1521. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1522. if (!res) {
  1523. v4l2_err(&disp_dev->vpbe_dev->v4l2_dev,
  1524. "Unable to get VENC interrupt resource\n");
  1525. err = -ENODEV;
  1526. goto probe_out;
  1527. }
  1528. irq = res->start;
  1529. err = devm_request_irq(&pdev->dev, irq, venc_isr, 0,
  1530. VPBE_DISPLAY_DRIVER, disp_dev);
  1531. if (err) {
  1532. v4l2_err(&disp_dev->vpbe_dev->v4l2_dev,
  1533. "Unable to request interrupt\n");
  1534. goto probe_out;
  1535. }
  1536. for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
  1537. if (register_device(disp_dev->dev[i], disp_dev, pdev)) {
  1538. err = -ENODEV;
  1539. goto probe_out;
  1540. }
  1541. }
  1542. printk(KERN_DEBUG "Successfully completed the probing of vpbe v4l2 device\n");
  1543. return 0;
  1544. probe_out:
  1545. for (k = 0; k < VPBE_DISPLAY_MAX_DEVICES; k++) {
  1546. /* Get the pointer to the layer object */
  1547. vpbe_display_layer = disp_dev->dev[k];
  1548. /* Unregister video device */
  1549. if (vpbe_display_layer) {
  1550. video_unregister_device(
  1551. &vpbe_display_layer->video_dev);
  1552. kfree(disp_dev->dev[k]);
  1553. }
  1554. }
  1555. return err;
  1556. }
  1557. /*
  1558. * vpbe_display_remove()
  1559. * It un-register hardware layer from V4L2 driver
  1560. */
  1561. static int vpbe_display_remove(struct platform_device *pdev)
  1562. {
  1563. struct vpbe_layer *vpbe_display_layer;
  1564. struct vpbe_display *disp_dev = platform_get_drvdata(pdev);
  1565. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  1566. int i;
  1567. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_remove\n");
  1568. /* deinitialize the vpbe display controller */
  1569. if (NULL != vpbe_dev->ops.deinitialize)
  1570. vpbe_dev->ops.deinitialize(&pdev->dev, vpbe_dev);
  1571. /* un-register device */
  1572. for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
  1573. /* Get the pointer to the layer object */
  1574. vpbe_display_layer = disp_dev->dev[i];
  1575. /* Unregister video device */
  1576. video_unregister_device(&vpbe_display_layer->video_dev);
  1577. }
  1578. for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
  1579. kfree(disp_dev->dev[i]);
  1580. disp_dev->dev[i] = NULL;
  1581. }
  1582. return 0;
  1583. }
  1584. static struct platform_driver vpbe_display_driver = {
  1585. .driver = {
  1586. .name = VPBE_DISPLAY_DRIVER,
  1587. .owner = THIS_MODULE,
  1588. .bus = &platform_bus_type,
  1589. },
  1590. .probe = vpbe_display_probe,
  1591. .remove = vpbe_display_remove,
  1592. };
  1593. module_platform_driver(vpbe_display_driver);
  1594. MODULE_DESCRIPTION("TI DM644x/DM355/DM365 VPBE Display controller");
  1595. MODULE_LICENSE("GPL");
  1596. MODULE_AUTHOR("Texas Instruments");