vpbe_display.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534
  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. if (layer->cur_frm == layer->next_frm)
  65. return;
  66. v4l2_get_timestamp(&layer->cur_frm->vb.v4l2_buf.timestamp);
  67. vb2_buffer_done(&layer->cur_frm->vb, VB2_BUF_STATE_DONE);
  68. /* Make cur_frm pointing to next_frm */
  69. layer->cur_frm = layer->next_frm;
  70. }
  71. static void vpbe_isr_odd_field(struct vpbe_display *disp_obj,
  72. struct vpbe_layer *layer)
  73. {
  74. struct osd_state *osd_device = disp_obj->osd_device;
  75. unsigned long addr;
  76. spin_lock(&disp_obj->dma_queue_lock);
  77. if (list_empty(&layer->dma_queue) ||
  78. (layer->cur_frm != layer->next_frm)) {
  79. spin_unlock(&disp_obj->dma_queue_lock);
  80. return;
  81. }
  82. /*
  83. * one field is displayed configure
  84. * the next frame if it is available
  85. * otherwise hold on current frame
  86. * Get next from the buffer queue
  87. */
  88. layer->next_frm = list_entry(layer->dma_queue.next,
  89. struct vpbe_disp_buffer, list);
  90. /* Remove that from the buffer queue */
  91. list_del(&layer->next_frm->list);
  92. spin_unlock(&disp_obj->dma_queue_lock);
  93. /* Mark state of the frame to active */
  94. layer->next_frm->vb.state = VB2_BUF_STATE_ACTIVE;
  95. addr = vb2_dma_contig_plane_dma_addr(&layer->next_frm->vb, 0);
  96. osd_device->ops.start_layer(osd_device,
  97. layer->layer_info.id,
  98. addr,
  99. disp_obj->cbcr_ofst);
  100. }
  101. /* interrupt service routine */
  102. static irqreturn_t venc_isr(int irq, void *arg)
  103. {
  104. struct vpbe_display *disp_dev = (struct vpbe_display *)arg;
  105. struct vpbe_layer *layer;
  106. static unsigned last_event;
  107. unsigned event = 0;
  108. int fid;
  109. int i;
  110. if ((NULL == arg) || (NULL == disp_dev->dev[0]))
  111. return IRQ_HANDLED;
  112. if (venc_is_second_field(disp_dev))
  113. event |= VENC_SECOND_FIELD;
  114. else
  115. event |= VENC_FIRST_FIELD;
  116. if (event == (last_event & ~VENC_END_OF_FRAME)) {
  117. /*
  118. * If the display is non-interlaced, then we need to flag the
  119. * end-of-frame event at every interrupt regardless of the
  120. * value of the FIDST bit. We can conclude that the display is
  121. * non-interlaced if the value of the FIDST bit is unchanged
  122. * from the previous interrupt.
  123. */
  124. event |= VENC_END_OF_FRAME;
  125. } else if (event == VENC_SECOND_FIELD) {
  126. /* end-of-frame for interlaced display */
  127. event |= VENC_END_OF_FRAME;
  128. }
  129. last_event = event;
  130. for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
  131. layer = disp_dev->dev[i];
  132. if (!vb2_start_streaming_called(&layer->buffer_queue))
  133. continue;
  134. if (layer->layer_first_int) {
  135. layer->layer_first_int = 0;
  136. continue;
  137. }
  138. /* Check the field format */
  139. if ((V4L2_FIELD_NONE == layer->pix_fmt.field) &&
  140. (event & VENC_END_OF_FRAME)) {
  141. /* Progressive mode */
  142. vpbe_isr_even_field(disp_dev, layer);
  143. vpbe_isr_odd_field(disp_dev, layer);
  144. } else {
  145. /* Interlaced mode */
  146. layer->field_id ^= 1;
  147. if (event & VENC_FIRST_FIELD)
  148. fid = 0;
  149. else
  150. fid = 1;
  151. /*
  152. * If field id does not match with store
  153. * field id
  154. */
  155. if (fid != layer->field_id) {
  156. /* Make them in sync */
  157. layer->field_id = fid;
  158. continue;
  159. }
  160. /*
  161. * device field id and local field id are
  162. * in sync. If this is even field
  163. */
  164. if (0 == fid)
  165. vpbe_isr_even_field(disp_dev, layer);
  166. else /* odd field */
  167. vpbe_isr_odd_field(disp_dev, layer);
  168. }
  169. }
  170. return IRQ_HANDLED;
  171. }
  172. /*
  173. * vpbe_buffer_prepare()
  174. * This is the callback function called from vb2_qbuf() function
  175. * the buffer is prepared and user space virtual address is converted into
  176. * physical address
  177. */
  178. static int vpbe_buffer_prepare(struct vb2_buffer *vb)
  179. {
  180. struct vb2_queue *q = vb->vb2_queue;
  181. struct vpbe_layer *layer = vb2_get_drv_priv(q);
  182. struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
  183. unsigned long addr;
  184. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  185. "vpbe_buffer_prepare\n");
  186. vb2_set_plane_payload(vb, 0, layer->pix_fmt.sizeimage);
  187. if (vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
  188. return -EINVAL;
  189. addr = vb2_dma_contig_plane_dma_addr(vb, 0);
  190. if (!IS_ALIGNED(addr, 8)) {
  191. v4l2_err(&vpbe_dev->v4l2_dev,
  192. "buffer_prepare:offset is not aligned to 32 bytes\n");
  193. return -EINVAL;
  194. }
  195. return 0;
  196. }
  197. /*
  198. * vpbe_buffer_setup()
  199. * This function allocates memory for the buffers
  200. */
  201. static int
  202. vpbe_buffer_queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
  203. unsigned int *nbuffers, unsigned int *nplanes,
  204. unsigned int sizes[], void *alloc_ctxs[])
  205. {
  206. /* Get the file handle object and layer object */
  207. struct vpbe_layer *layer = vb2_get_drv_priv(vq);
  208. struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
  209. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_buffer_setup\n");
  210. if (fmt && fmt->fmt.pix.sizeimage < layer->pix_fmt.sizeimage)
  211. return -EINVAL;
  212. /* Store number of buffers allocated in numbuffer member */
  213. if (vq->num_buffers + *nbuffers < VPBE_DEFAULT_NUM_BUFS)
  214. *nbuffers = VPBE_DEFAULT_NUM_BUFS - vq->num_buffers;
  215. *nplanes = 1;
  216. sizes[0] = fmt ? fmt->fmt.pix.sizeimage : layer->pix_fmt.sizeimage;
  217. alloc_ctxs[0] = layer->alloc_ctx;
  218. return 0;
  219. }
  220. /*
  221. * vpbe_buffer_queue()
  222. * This function adds the buffer to DMA queue
  223. */
  224. static void vpbe_buffer_queue(struct vb2_buffer *vb)
  225. {
  226. /* Get the file handle object and layer object */
  227. struct vpbe_disp_buffer *buf = container_of(vb,
  228. struct vpbe_disp_buffer, vb);
  229. struct vpbe_layer *layer = vb2_get_drv_priv(vb->vb2_queue);
  230. struct vpbe_display *disp = layer->disp_dev;
  231. struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
  232. unsigned long flags;
  233. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  234. "vpbe_buffer_queue\n");
  235. /* add the buffer to the DMA queue */
  236. spin_lock_irqsave(&disp->dma_queue_lock, flags);
  237. list_add_tail(&buf->list, &layer->dma_queue);
  238. spin_unlock_irqrestore(&disp->dma_queue_lock, flags);
  239. }
  240. static int vpbe_start_streaming(struct vb2_queue *vq, unsigned int count)
  241. {
  242. struct vpbe_layer *layer = vb2_get_drv_priv(vq);
  243. struct osd_state *osd_device = layer->disp_dev->osd_device;
  244. int ret;
  245. osd_device->ops.disable_layer(osd_device, layer->layer_info.id);
  246. /* Get the next frame from the buffer queue */
  247. layer->next_frm = layer->cur_frm = list_entry(layer->dma_queue.next,
  248. struct vpbe_disp_buffer, list);
  249. /* Remove buffer from the buffer queue */
  250. list_del(&layer->cur_frm->list);
  251. /* Mark state of the current frame to active */
  252. layer->cur_frm->vb.state = VB2_BUF_STATE_ACTIVE;
  253. /* Initialize field_id and started member */
  254. layer->field_id = 0;
  255. /* Set parameters in OSD and VENC */
  256. ret = vpbe_set_osd_display_params(layer->disp_dev, layer);
  257. if (ret < 0) {
  258. struct vpbe_disp_buffer *buf, *tmp;
  259. vb2_buffer_done(&layer->cur_frm->vb, VB2_BUF_STATE_QUEUED);
  260. list_for_each_entry_safe(buf, tmp, &layer->dma_queue, list) {
  261. list_del(&buf->list);
  262. vb2_buffer_done(&buf->vb, VB2_BUF_STATE_QUEUED);
  263. }
  264. return ret;
  265. }
  266. /*
  267. * if request format is yuv420 semiplanar, need to
  268. * enable both video windows
  269. */
  270. layer->layer_first_int = 1;
  271. return ret;
  272. }
  273. static void vpbe_stop_streaming(struct vb2_queue *vq)
  274. {
  275. struct vpbe_layer *layer = vb2_get_drv_priv(vq);
  276. struct osd_state *osd_device = layer->disp_dev->osd_device;
  277. struct vpbe_display *disp = layer->disp_dev;
  278. unsigned long flags;
  279. if (!vb2_is_streaming(vq))
  280. return;
  281. osd_device->ops.disable_layer(osd_device, layer->layer_info.id);
  282. /* release all active buffers */
  283. spin_lock_irqsave(&disp->dma_queue_lock, flags);
  284. if (layer->cur_frm == layer->next_frm) {
  285. vb2_buffer_done(&layer->cur_frm->vb, VB2_BUF_STATE_ERROR);
  286. } else {
  287. if (layer->cur_frm != NULL)
  288. vb2_buffer_done(&layer->cur_frm->vb,
  289. VB2_BUF_STATE_ERROR);
  290. if (layer->next_frm != NULL)
  291. vb2_buffer_done(&layer->next_frm->vb,
  292. VB2_BUF_STATE_ERROR);
  293. }
  294. while (!list_empty(&layer->dma_queue)) {
  295. layer->next_frm = list_entry(layer->dma_queue.next,
  296. struct vpbe_disp_buffer, list);
  297. list_del(&layer->next_frm->list);
  298. vb2_buffer_done(&layer->next_frm->vb, VB2_BUF_STATE_ERROR);
  299. }
  300. spin_unlock_irqrestore(&disp->dma_queue_lock, flags);
  301. }
  302. static struct vb2_ops video_qops = {
  303. .queue_setup = vpbe_buffer_queue_setup,
  304. .wait_prepare = vb2_ops_wait_prepare,
  305. .wait_finish = vb2_ops_wait_finish,
  306. .buf_prepare = vpbe_buffer_prepare,
  307. .start_streaming = vpbe_start_streaming,
  308. .stop_streaming = vpbe_stop_streaming,
  309. .buf_queue = vpbe_buffer_queue,
  310. };
  311. static
  312. struct vpbe_layer*
  313. _vpbe_display_get_other_win_layer(struct vpbe_display *disp_dev,
  314. struct vpbe_layer *layer)
  315. {
  316. enum vpbe_display_device_id thiswin, otherwin;
  317. thiswin = layer->device_id;
  318. otherwin = (thiswin == VPBE_DISPLAY_DEVICE_0) ?
  319. VPBE_DISPLAY_DEVICE_1 : VPBE_DISPLAY_DEVICE_0;
  320. return disp_dev->dev[otherwin];
  321. }
  322. static int vpbe_set_osd_display_params(struct vpbe_display *disp_dev,
  323. struct vpbe_layer *layer)
  324. {
  325. struct osd_layer_config *cfg = &layer->layer_info.config;
  326. struct osd_state *osd_device = disp_dev->osd_device;
  327. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  328. unsigned long addr;
  329. int ret;
  330. addr = vb2_dma_contig_plane_dma_addr(&layer->cur_frm->vb, 0);
  331. /* Set address in the display registers */
  332. osd_device->ops.start_layer(osd_device,
  333. layer->layer_info.id,
  334. addr,
  335. disp_dev->cbcr_ofst);
  336. ret = osd_device->ops.enable_layer(osd_device,
  337. layer->layer_info.id, 0);
  338. if (ret < 0) {
  339. v4l2_err(&vpbe_dev->v4l2_dev,
  340. "Error in enabling osd window layer 0\n");
  341. return -1;
  342. }
  343. /* Enable the window */
  344. layer->layer_info.enable = 1;
  345. if (cfg->pixfmt == PIXFMT_NV12) {
  346. struct vpbe_layer *otherlayer =
  347. _vpbe_display_get_other_win_layer(disp_dev, layer);
  348. ret = osd_device->ops.enable_layer(osd_device,
  349. otherlayer->layer_info.id, 1);
  350. if (ret < 0) {
  351. v4l2_err(&vpbe_dev->v4l2_dev,
  352. "Error in enabling osd window layer 1\n");
  353. return -1;
  354. }
  355. otherlayer->layer_info.enable = 1;
  356. }
  357. return 0;
  358. }
  359. static void
  360. vpbe_disp_calculate_scale_factor(struct vpbe_display *disp_dev,
  361. struct vpbe_layer *layer,
  362. int expected_xsize, int expected_ysize)
  363. {
  364. struct display_layer_info *layer_info = &layer->layer_info;
  365. struct v4l2_pix_format *pixfmt = &layer->pix_fmt;
  366. struct osd_layer_config *cfg = &layer->layer_info.config;
  367. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  368. int calculated_xsize;
  369. int h_exp = 0;
  370. int v_exp = 0;
  371. int h_scale;
  372. int v_scale;
  373. v4l2_std_id standard_id = vpbe_dev->current_timings.std_id;
  374. /*
  375. * Application initially set the image format. Current display
  376. * size is obtained from the vpbe display controller. expected_xsize
  377. * and expected_ysize are set through S_CROP ioctl. Based on this,
  378. * driver will calculate the scale factors for vertical and
  379. * horizontal direction so that the image is displayed scaled
  380. * and expanded. Application uses expansion to display the image
  381. * in a square pixel. Otherwise it is displayed using displays
  382. * pixel aspect ratio.It is expected that application chooses
  383. * the crop coordinates for cropped or scaled display. if crop
  384. * size is less than the image size, it is displayed cropped or
  385. * it is displayed scaled and/or expanded.
  386. *
  387. * to begin with, set the crop window same as expected. Later we
  388. * will override with scaled window size
  389. */
  390. cfg->xsize = pixfmt->width;
  391. cfg->ysize = pixfmt->height;
  392. layer_info->h_zoom = ZOOM_X1; /* no horizontal zoom */
  393. layer_info->v_zoom = ZOOM_X1; /* no horizontal zoom */
  394. layer_info->h_exp = H_EXP_OFF; /* no horizontal zoom */
  395. layer_info->v_exp = V_EXP_OFF; /* no horizontal zoom */
  396. if (pixfmt->width < expected_xsize) {
  397. h_scale = vpbe_dev->current_timings.xres / pixfmt->width;
  398. if (h_scale < 2)
  399. h_scale = 1;
  400. else if (h_scale >= 4)
  401. h_scale = 4;
  402. else
  403. h_scale = 2;
  404. cfg->xsize *= h_scale;
  405. if (cfg->xsize < expected_xsize) {
  406. if ((standard_id & V4L2_STD_525_60) ||
  407. (standard_id & V4L2_STD_625_50)) {
  408. calculated_xsize = (cfg->xsize *
  409. VPBE_DISPLAY_H_EXP_RATIO_N) /
  410. VPBE_DISPLAY_H_EXP_RATIO_D;
  411. if (calculated_xsize <= expected_xsize) {
  412. h_exp = 1;
  413. cfg->xsize = calculated_xsize;
  414. }
  415. }
  416. }
  417. if (h_scale == 2)
  418. layer_info->h_zoom = ZOOM_X2;
  419. else if (h_scale == 4)
  420. layer_info->h_zoom = ZOOM_X4;
  421. if (h_exp)
  422. layer_info->h_exp = H_EXP_9_OVER_8;
  423. } else {
  424. /* no scaling, only cropping. Set display area to crop area */
  425. cfg->xsize = expected_xsize;
  426. }
  427. if (pixfmt->height < expected_ysize) {
  428. v_scale = expected_ysize / pixfmt->height;
  429. if (v_scale < 2)
  430. v_scale = 1;
  431. else if (v_scale >= 4)
  432. v_scale = 4;
  433. else
  434. v_scale = 2;
  435. cfg->ysize *= v_scale;
  436. if (cfg->ysize < expected_ysize) {
  437. if ((standard_id & V4L2_STD_625_50)) {
  438. calculated_xsize = (cfg->ysize *
  439. VPBE_DISPLAY_V_EXP_RATIO_N) /
  440. VPBE_DISPLAY_V_EXP_RATIO_D;
  441. if (calculated_xsize <= expected_ysize) {
  442. v_exp = 1;
  443. cfg->ysize = calculated_xsize;
  444. }
  445. }
  446. }
  447. if (v_scale == 2)
  448. layer_info->v_zoom = ZOOM_X2;
  449. else if (v_scale == 4)
  450. layer_info->v_zoom = ZOOM_X4;
  451. if (v_exp)
  452. layer_info->h_exp = V_EXP_6_OVER_5;
  453. } else {
  454. /* no scaling, only cropping. Set display area to crop area */
  455. cfg->ysize = expected_ysize;
  456. }
  457. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  458. "crop display xsize = %d, ysize = %d\n",
  459. cfg->xsize, cfg->ysize);
  460. }
  461. static void vpbe_disp_adj_position(struct vpbe_display *disp_dev,
  462. struct vpbe_layer *layer,
  463. int top, int left)
  464. {
  465. struct osd_layer_config *cfg = &layer->layer_info.config;
  466. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  467. cfg->xpos = min((unsigned int)left,
  468. vpbe_dev->current_timings.xres - cfg->xsize);
  469. cfg->ypos = min((unsigned int)top,
  470. vpbe_dev->current_timings.yres - cfg->ysize);
  471. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  472. "new xpos = %d, ypos = %d\n",
  473. cfg->xpos, cfg->ypos);
  474. }
  475. static void vpbe_disp_check_window_params(struct vpbe_display *disp_dev,
  476. struct v4l2_rect *c)
  477. {
  478. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  479. if ((c->width == 0) ||
  480. ((c->width + c->left) > vpbe_dev->current_timings.xres))
  481. c->width = vpbe_dev->current_timings.xres - c->left;
  482. if ((c->height == 0) || ((c->height + c->top) >
  483. vpbe_dev->current_timings.yres))
  484. c->height = vpbe_dev->current_timings.yres - c->top;
  485. /* window height must be even for interlaced display */
  486. if (vpbe_dev->current_timings.interlaced)
  487. c->height &= (~0x01);
  488. }
  489. /**
  490. * vpbe_try_format()
  491. * If user application provides width and height, and have bytesperline set
  492. * to zero, driver calculates bytesperline and sizeimage based on hardware
  493. * limits.
  494. */
  495. static int vpbe_try_format(struct vpbe_display *disp_dev,
  496. struct v4l2_pix_format *pixfmt, int check)
  497. {
  498. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  499. int min_height = 1;
  500. int min_width = 32;
  501. int max_height;
  502. int max_width;
  503. int bpp;
  504. if ((pixfmt->pixelformat != V4L2_PIX_FMT_UYVY) &&
  505. (pixfmt->pixelformat != V4L2_PIX_FMT_NV12))
  506. /* choose default as V4L2_PIX_FMT_UYVY */
  507. pixfmt->pixelformat = V4L2_PIX_FMT_UYVY;
  508. /* Check the field format */
  509. if ((pixfmt->field != V4L2_FIELD_INTERLACED) &&
  510. (pixfmt->field != V4L2_FIELD_NONE)) {
  511. if (vpbe_dev->current_timings.interlaced)
  512. pixfmt->field = V4L2_FIELD_INTERLACED;
  513. else
  514. pixfmt->field = V4L2_FIELD_NONE;
  515. }
  516. if (pixfmt->field == V4L2_FIELD_INTERLACED)
  517. min_height = 2;
  518. if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12)
  519. bpp = 1;
  520. else
  521. bpp = 2;
  522. max_width = vpbe_dev->current_timings.xres;
  523. max_height = vpbe_dev->current_timings.yres;
  524. min_width /= bpp;
  525. if (!pixfmt->width || (pixfmt->width < min_width) ||
  526. (pixfmt->width > max_width)) {
  527. pixfmt->width = vpbe_dev->current_timings.xres;
  528. }
  529. if (!pixfmt->height || (pixfmt->height < min_height) ||
  530. (pixfmt->height > max_height)) {
  531. pixfmt->height = vpbe_dev->current_timings.yres;
  532. }
  533. if (pixfmt->bytesperline < (pixfmt->width * bpp))
  534. pixfmt->bytesperline = pixfmt->width * bpp;
  535. /* Make the bytesperline 32 byte aligned */
  536. pixfmt->bytesperline = ((pixfmt->width * bpp + 31) & ~31);
  537. if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12)
  538. pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height +
  539. (pixfmt->bytesperline * pixfmt->height >> 1);
  540. else
  541. pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height;
  542. return 0;
  543. }
  544. static int vpbe_display_querycap(struct file *file, void *priv,
  545. struct v4l2_capability *cap)
  546. {
  547. struct vpbe_layer *layer = video_drvdata(file);
  548. struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
  549. cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
  550. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  551. snprintf(cap->driver, sizeof(cap->driver), "%s",
  552. dev_name(vpbe_dev->pdev));
  553. snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
  554. dev_name(vpbe_dev->pdev));
  555. strlcpy(cap->card, vpbe_dev->cfg->module_name, sizeof(cap->card));
  556. return 0;
  557. }
  558. static int vpbe_display_s_crop(struct file *file, void *priv,
  559. const struct v4l2_crop *crop)
  560. {
  561. struct vpbe_layer *layer = video_drvdata(file);
  562. struct vpbe_display *disp_dev = layer->disp_dev;
  563. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  564. struct osd_layer_config *cfg = &layer->layer_info.config;
  565. struct osd_state *osd_device = disp_dev->osd_device;
  566. struct v4l2_rect rect = crop->c;
  567. int ret;
  568. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  569. "VIDIOC_S_CROP, layer id = %d\n", layer->device_id);
  570. if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  571. v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buf type\n");
  572. return -EINVAL;
  573. }
  574. if (rect.top < 0)
  575. rect.top = 0;
  576. if (rect.left < 0)
  577. rect.left = 0;
  578. vpbe_disp_check_window_params(disp_dev, &rect);
  579. osd_device->ops.get_layer_config(osd_device,
  580. layer->layer_info.id, cfg);
  581. vpbe_disp_calculate_scale_factor(disp_dev, layer,
  582. rect.width,
  583. rect.height);
  584. vpbe_disp_adj_position(disp_dev, layer, rect.top,
  585. rect.left);
  586. ret = osd_device->ops.set_layer_config(osd_device,
  587. layer->layer_info.id, cfg);
  588. if (ret < 0) {
  589. v4l2_err(&vpbe_dev->v4l2_dev,
  590. "Error in set layer config:\n");
  591. return -EINVAL;
  592. }
  593. /* apply zooming and h or v expansion */
  594. osd_device->ops.set_zoom(osd_device,
  595. layer->layer_info.id,
  596. layer->layer_info.h_zoom,
  597. layer->layer_info.v_zoom);
  598. ret = osd_device->ops.set_vid_expansion(osd_device,
  599. layer->layer_info.h_exp,
  600. layer->layer_info.v_exp);
  601. if (ret < 0) {
  602. v4l2_err(&vpbe_dev->v4l2_dev,
  603. "Error in set vid expansion:\n");
  604. return -EINVAL;
  605. }
  606. if ((layer->layer_info.h_zoom != ZOOM_X1) ||
  607. (layer->layer_info.v_zoom != ZOOM_X1) ||
  608. (layer->layer_info.h_exp != H_EXP_OFF) ||
  609. (layer->layer_info.v_exp != V_EXP_OFF))
  610. /* Enable expansion filter */
  611. osd_device->ops.set_interpolation_filter(osd_device, 1);
  612. else
  613. osd_device->ops.set_interpolation_filter(osd_device, 0);
  614. return 0;
  615. }
  616. static int vpbe_display_g_crop(struct file *file, void *priv,
  617. struct v4l2_crop *crop)
  618. {
  619. struct vpbe_layer *layer = video_drvdata(file);
  620. struct osd_layer_config *cfg = &layer->layer_info.config;
  621. struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
  622. struct osd_state *osd_device = layer->disp_dev->osd_device;
  623. struct v4l2_rect *rect = &crop->c;
  624. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  625. "VIDIOC_G_CROP, layer id = %d\n",
  626. 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. osd_device->ops.get_layer_config(osd_device,
  632. layer->layer_info.id, cfg);
  633. rect->top = cfg->ypos;
  634. rect->left = cfg->xpos;
  635. rect->width = cfg->xsize;
  636. rect->height = cfg->ysize;
  637. return 0;
  638. }
  639. static int vpbe_display_cropcap(struct file *file, void *priv,
  640. struct v4l2_cropcap *cropcap)
  641. {
  642. struct vpbe_layer *layer = video_drvdata(file);
  643. struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
  644. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_CROPCAP ioctl\n");
  645. cropcap->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  646. cropcap->bounds.left = 0;
  647. cropcap->bounds.top = 0;
  648. cropcap->bounds.width = vpbe_dev->current_timings.xres;
  649. cropcap->bounds.height = vpbe_dev->current_timings.yres;
  650. cropcap->pixelaspect = vpbe_dev->current_timings.aspect;
  651. cropcap->defrect = cropcap->bounds;
  652. return 0;
  653. }
  654. static int vpbe_display_g_fmt(struct file *file, void *priv,
  655. struct v4l2_format *fmt)
  656. {
  657. struct vpbe_layer *layer = video_drvdata(file);
  658. struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
  659. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  660. "VIDIOC_G_FMT, layer id = %d\n",
  661. layer->device_id);
  662. /* If buffer type is video output */
  663. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != fmt->type) {
  664. v4l2_err(&vpbe_dev->v4l2_dev, "invalid type\n");
  665. return -EINVAL;
  666. }
  667. /* Fill in the information about format */
  668. fmt->fmt.pix = layer->pix_fmt;
  669. return 0;
  670. }
  671. static int vpbe_display_enum_fmt(struct file *file, void *priv,
  672. struct v4l2_fmtdesc *fmt)
  673. {
  674. struct vpbe_layer *layer = video_drvdata(file);
  675. struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
  676. unsigned int index = 0;
  677. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  678. "VIDIOC_ENUM_FMT, layer id = %d\n",
  679. layer->device_id);
  680. if (fmt->index > 1) {
  681. v4l2_err(&vpbe_dev->v4l2_dev, "Invalid format index\n");
  682. return -EINVAL;
  683. }
  684. /* Fill in the information about format */
  685. index = fmt->index;
  686. memset(fmt, 0, sizeof(*fmt));
  687. fmt->index = index;
  688. fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  689. if (index == 0) {
  690. strcpy(fmt->description, "YUV 4:2:2 - UYVY");
  691. fmt->pixelformat = V4L2_PIX_FMT_UYVY;
  692. } else {
  693. strcpy(fmt->description, "Y/CbCr 4:2:0");
  694. fmt->pixelformat = V4L2_PIX_FMT_NV12;
  695. }
  696. return 0;
  697. }
  698. static int vpbe_display_s_fmt(struct file *file, void *priv,
  699. struct v4l2_format *fmt)
  700. {
  701. struct vpbe_layer *layer = video_drvdata(file);
  702. struct vpbe_display *disp_dev = layer->disp_dev;
  703. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  704. struct osd_layer_config *cfg = &layer->layer_info.config;
  705. struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
  706. struct osd_state *osd_device = disp_dev->osd_device;
  707. int ret;
  708. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  709. "VIDIOC_S_FMT, layer id = %d\n",
  710. layer->device_id);
  711. if (vb2_is_busy(&layer->buffer_queue))
  712. return -EBUSY;
  713. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != fmt->type) {
  714. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "invalid type\n");
  715. return -EINVAL;
  716. }
  717. /* Check for valid pixel format */
  718. ret = vpbe_try_format(disp_dev, pixfmt, 1);
  719. if (ret)
  720. return ret;
  721. /* YUV420 is requested, check availability of the
  722. other video window */
  723. layer->pix_fmt = *pixfmt;
  724. if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12) {
  725. struct vpbe_layer *otherlayer;
  726. otherlayer = _vpbe_display_get_other_win_layer(disp_dev, layer);
  727. /* if other layer is available, only
  728. * claim it, do not configure it
  729. */
  730. ret = osd_device->ops.request_layer(osd_device,
  731. otherlayer->layer_info.id);
  732. if (ret < 0) {
  733. v4l2_err(&vpbe_dev->v4l2_dev,
  734. "Display Manager failed to allocate layer\n");
  735. return -EBUSY;
  736. }
  737. }
  738. /* Get osd layer config */
  739. osd_device->ops.get_layer_config(osd_device,
  740. layer->layer_info.id, cfg);
  741. /* Store the pixel format in the layer object */
  742. cfg->xsize = pixfmt->width;
  743. cfg->ysize = pixfmt->height;
  744. cfg->line_length = pixfmt->bytesperline;
  745. cfg->ypos = 0;
  746. cfg->xpos = 0;
  747. cfg->interlaced = vpbe_dev->current_timings.interlaced;
  748. if (V4L2_PIX_FMT_UYVY == pixfmt->pixelformat)
  749. cfg->pixfmt = PIXFMT_YCBCRI;
  750. /* Change of the default pixel format for both video windows */
  751. if (V4L2_PIX_FMT_NV12 == pixfmt->pixelformat) {
  752. struct vpbe_layer *otherlayer;
  753. cfg->pixfmt = PIXFMT_NV12;
  754. otherlayer = _vpbe_display_get_other_win_layer(disp_dev,
  755. layer);
  756. otherlayer->layer_info.config.pixfmt = PIXFMT_NV12;
  757. }
  758. /* Set the layer config in the osd window */
  759. ret = osd_device->ops.set_layer_config(osd_device,
  760. layer->layer_info.id, cfg);
  761. if (ret < 0) {
  762. v4l2_err(&vpbe_dev->v4l2_dev,
  763. "Error in S_FMT params:\n");
  764. return -EINVAL;
  765. }
  766. /* Readback and fill the local copy of current pix format */
  767. osd_device->ops.get_layer_config(osd_device,
  768. layer->layer_info.id, cfg);
  769. return 0;
  770. }
  771. static int vpbe_display_try_fmt(struct file *file, void *priv,
  772. struct v4l2_format *fmt)
  773. {
  774. struct vpbe_layer *layer = video_drvdata(file);
  775. struct vpbe_display *disp_dev = layer->disp_dev;
  776. struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
  777. struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
  778. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_TRY_FMT\n");
  779. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != fmt->type) {
  780. v4l2_err(&vpbe_dev->v4l2_dev, "invalid type\n");
  781. return -EINVAL;
  782. }
  783. /* Check for valid field format */
  784. return vpbe_try_format(disp_dev, pixfmt, 0);
  785. }
  786. /**
  787. * vpbe_display_s_std - Set the given standard in the encoder
  788. *
  789. * Sets the standard if supported by the current encoder. Return the status.
  790. * 0 - success & -EINVAL on error
  791. */
  792. static int vpbe_display_s_std(struct file *file, void *priv,
  793. v4l2_std_id std_id)
  794. {
  795. struct vpbe_layer *layer = video_drvdata(file);
  796. struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
  797. int ret;
  798. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_S_STD\n");
  799. if (vb2_is_busy(&layer->buffer_queue))
  800. return -EBUSY;
  801. if (NULL != vpbe_dev->ops.s_std) {
  802. ret = vpbe_dev->ops.s_std(vpbe_dev, std_id);
  803. if (ret) {
  804. v4l2_err(&vpbe_dev->v4l2_dev,
  805. "Failed to set standard for sub devices\n");
  806. return -EINVAL;
  807. }
  808. } else {
  809. return -EINVAL;
  810. }
  811. return 0;
  812. }
  813. /**
  814. * vpbe_display_g_std - Get the standard in the current encoder
  815. *
  816. * Get the standard in the current encoder. Return the status. 0 - success
  817. * -EINVAL on error
  818. */
  819. static int vpbe_display_g_std(struct file *file, void *priv,
  820. v4l2_std_id *std_id)
  821. {
  822. struct vpbe_layer *layer = video_drvdata(file);
  823. struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
  824. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_G_STD\n");
  825. /* Get the standard from the current encoder */
  826. if (vpbe_dev->current_timings.timings_type & VPBE_ENC_STD) {
  827. *std_id = vpbe_dev->current_timings.std_id;
  828. return 0;
  829. }
  830. return -EINVAL;
  831. }
  832. /**
  833. * vpbe_display_enum_output - enumerate outputs
  834. *
  835. * Enumerates the outputs available at the vpbe display
  836. * returns the status, -EINVAL if end of output list
  837. */
  838. static int vpbe_display_enum_output(struct file *file, void *priv,
  839. struct v4l2_output *output)
  840. {
  841. struct vpbe_layer *layer = video_drvdata(file);
  842. struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
  843. int ret;
  844. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_ENUM_OUTPUT\n");
  845. /* Enumerate outputs */
  846. if (NULL == vpbe_dev->ops.enum_outputs)
  847. return -EINVAL;
  848. ret = vpbe_dev->ops.enum_outputs(vpbe_dev, output);
  849. if (ret) {
  850. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  851. "Failed to enumerate outputs\n");
  852. return -EINVAL;
  853. }
  854. return 0;
  855. }
  856. /**
  857. * vpbe_display_s_output - Set output to
  858. * the output specified by the index
  859. */
  860. static int vpbe_display_s_output(struct file *file, void *priv,
  861. unsigned int i)
  862. {
  863. struct vpbe_layer *layer = video_drvdata(file);
  864. struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
  865. int ret;
  866. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_S_OUTPUT\n");
  867. if (vb2_is_busy(&layer->buffer_queue))
  868. return -EBUSY;
  869. if (NULL == vpbe_dev->ops.set_output)
  870. return -EINVAL;
  871. ret = vpbe_dev->ops.set_output(vpbe_dev, i);
  872. if (ret) {
  873. v4l2_err(&vpbe_dev->v4l2_dev,
  874. "Failed to set output for sub devices\n");
  875. return -EINVAL;
  876. }
  877. return 0;
  878. }
  879. /**
  880. * vpbe_display_g_output - Get output from subdevice
  881. * for a given by the index
  882. */
  883. static int vpbe_display_g_output(struct file *file, void *priv,
  884. unsigned int *i)
  885. {
  886. struct vpbe_layer *layer = video_drvdata(file);
  887. struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
  888. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_G_OUTPUT\n");
  889. /* Get the standard from the current encoder */
  890. *i = vpbe_dev->current_out_index;
  891. return 0;
  892. }
  893. /**
  894. * vpbe_display_enum_dv_timings - Enumerate the dv timings
  895. *
  896. * enum the timings in the current encoder. Return the status. 0 - success
  897. * -EINVAL on error
  898. */
  899. static int
  900. vpbe_display_enum_dv_timings(struct file *file, void *priv,
  901. struct v4l2_enum_dv_timings *timings)
  902. {
  903. struct vpbe_layer *layer = video_drvdata(file);
  904. struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
  905. int ret;
  906. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_ENUM_DV_TIMINGS\n");
  907. /* Enumerate outputs */
  908. if (NULL == vpbe_dev->ops.enum_dv_timings)
  909. return -EINVAL;
  910. ret = vpbe_dev->ops.enum_dv_timings(vpbe_dev, timings);
  911. if (ret) {
  912. v4l2_err(&vpbe_dev->v4l2_dev,
  913. "Failed to enumerate dv timings info\n");
  914. return -EINVAL;
  915. }
  916. return 0;
  917. }
  918. /**
  919. * vpbe_display_s_dv_timings - Set the dv timings
  920. *
  921. * Set the timings in the current encoder. Return the status. 0 - success
  922. * -EINVAL on error
  923. */
  924. static int
  925. vpbe_display_s_dv_timings(struct file *file, void *priv,
  926. struct v4l2_dv_timings *timings)
  927. {
  928. struct vpbe_layer *layer = video_drvdata(file);
  929. struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
  930. int ret;
  931. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_S_DV_TIMINGS\n");
  932. if (vb2_is_busy(&layer->buffer_queue))
  933. return -EBUSY;
  934. /* Set the given standard in the encoder */
  935. if (!vpbe_dev->ops.s_dv_timings)
  936. return -EINVAL;
  937. ret = vpbe_dev->ops.s_dv_timings(vpbe_dev, timings);
  938. if (ret) {
  939. v4l2_err(&vpbe_dev->v4l2_dev,
  940. "Failed to set the dv timings info\n");
  941. return -EINVAL;
  942. }
  943. return 0;
  944. }
  945. /**
  946. * vpbe_display_g_dv_timings - Set the dv timings
  947. *
  948. * Get the timings in the current encoder. Return the status. 0 - success
  949. * -EINVAL on error
  950. */
  951. static int
  952. vpbe_display_g_dv_timings(struct file *file, void *priv,
  953. struct v4l2_dv_timings *dv_timings)
  954. {
  955. struct vpbe_layer *layer = video_drvdata(file);
  956. struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
  957. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_G_DV_TIMINGS\n");
  958. /* Get the given standard in the encoder */
  959. if (vpbe_dev->current_timings.timings_type &
  960. VPBE_ENC_DV_TIMINGS) {
  961. *dv_timings = vpbe_dev->current_timings.dv_timings;
  962. } else {
  963. return -EINVAL;
  964. }
  965. return 0;
  966. }
  967. /*
  968. * vpbe_display_open()
  969. * It creates object of file handle structure and stores it in private_data
  970. * member of filepointer
  971. */
  972. static int vpbe_display_open(struct file *file)
  973. {
  974. struct vpbe_layer *layer = video_drvdata(file);
  975. struct vpbe_display *disp_dev = layer->disp_dev;
  976. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  977. struct osd_state *osd_device = disp_dev->osd_device;
  978. int err;
  979. /* creating context for file descriptor */
  980. err = v4l2_fh_open(file);
  981. if (err) {
  982. v4l2_err(&vpbe_dev->v4l2_dev, "v4l2_fh_open failed\n");
  983. return err;
  984. }
  985. /* leaving if layer is already initialized */
  986. if (!v4l2_fh_is_singular_file(file))
  987. return err;
  988. if (!layer->usrs) {
  989. if (mutex_lock_interruptible(&layer->opslock))
  990. return -ERESTARTSYS;
  991. /* First claim the layer for this device */
  992. err = osd_device->ops.request_layer(osd_device,
  993. layer->layer_info.id);
  994. mutex_unlock(&layer->opslock);
  995. if (err < 0) {
  996. /* Couldn't get layer */
  997. v4l2_err(&vpbe_dev->v4l2_dev,
  998. "Display Manager failed to allocate layer\n");
  999. v4l2_fh_release(file);
  1000. return -EINVAL;
  1001. }
  1002. }
  1003. /* Increment layer usrs counter */
  1004. layer->usrs++;
  1005. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  1006. "vpbe display device opened successfully\n");
  1007. return 0;
  1008. }
  1009. /*
  1010. * vpbe_display_release()
  1011. * This function deletes buffer queue, frees the buffers and the davinci
  1012. * display file * handle
  1013. */
  1014. static int vpbe_display_release(struct file *file)
  1015. {
  1016. struct vpbe_layer *layer = video_drvdata(file);
  1017. struct osd_layer_config *cfg = &layer->layer_info.config;
  1018. struct vpbe_display *disp_dev = layer->disp_dev;
  1019. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  1020. struct osd_state *osd_device = disp_dev->osd_device;
  1021. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_release\n");
  1022. mutex_lock(&layer->opslock);
  1023. osd_device->ops.disable_layer(osd_device,
  1024. layer->layer_info.id);
  1025. /* Decrement layer usrs counter */
  1026. layer->usrs--;
  1027. /* If this file handle has initialize encoder device, reset it */
  1028. if (!layer->usrs) {
  1029. if (cfg->pixfmt == PIXFMT_NV12) {
  1030. struct vpbe_layer *otherlayer;
  1031. otherlayer =
  1032. _vpbe_display_get_other_win_layer(disp_dev, layer);
  1033. osd_device->ops.disable_layer(osd_device,
  1034. otherlayer->layer_info.id);
  1035. osd_device->ops.release_layer(osd_device,
  1036. otherlayer->layer_info.id);
  1037. }
  1038. osd_device->ops.disable_layer(osd_device,
  1039. layer->layer_info.id);
  1040. osd_device->ops.release_layer(osd_device,
  1041. layer->layer_info.id);
  1042. }
  1043. _vb2_fop_release(file, NULL);
  1044. mutex_unlock(&layer->opslock);
  1045. disp_dev->cbcr_ofst = 0;
  1046. return 0;
  1047. }
  1048. /* vpbe capture ioctl operations */
  1049. static const struct v4l2_ioctl_ops vpbe_ioctl_ops = {
  1050. .vidioc_querycap = vpbe_display_querycap,
  1051. .vidioc_g_fmt_vid_out = vpbe_display_g_fmt,
  1052. .vidioc_enum_fmt_vid_out = vpbe_display_enum_fmt,
  1053. .vidioc_s_fmt_vid_out = vpbe_display_s_fmt,
  1054. .vidioc_try_fmt_vid_out = vpbe_display_try_fmt,
  1055. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  1056. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  1057. .vidioc_querybuf = vb2_ioctl_querybuf,
  1058. .vidioc_qbuf = vb2_ioctl_qbuf,
  1059. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  1060. .vidioc_streamon = vb2_ioctl_streamon,
  1061. .vidioc_streamoff = vb2_ioctl_streamoff,
  1062. .vidioc_expbuf = vb2_ioctl_expbuf,
  1063. .vidioc_cropcap = vpbe_display_cropcap,
  1064. .vidioc_g_crop = vpbe_display_g_crop,
  1065. .vidioc_s_crop = vpbe_display_s_crop,
  1066. .vidioc_s_std = vpbe_display_s_std,
  1067. .vidioc_g_std = vpbe_display_g_std,
  1068. .vidioc_enum_output = vpbe_display_enum_output,
  1069. .vidioc_s_output = vpbe_display_s_output,
  1070. .vidioc_g_output = vpbe_display_g_output,
  1071. .vidioc_s_dv_timings = vpbe_display_s_dv_timings,
  1072. .vidioc_g_dv_timings = vpbe_display_g_dv_timings,
  1073. .vidioc_enum_dv_timings = vpbe_display_enum_dv_timings,
  1074. };
  1075. static struct v4l2_file_operations vpbe_fops = {
  1076. .owner = THIS_MODULE,
  1077. .open = vpbe_display_open,
  1078. .release = vpbe_display_release,
  1079. .unlocked_ioctl = video_ioctl2,
  1080. .mmap = vb2_fop_mmap,
  1081. .poll = vb2_fop_poll,
  1082. };
  1083. static int vpbe_device_get(struct device *dev, void *data)
  1084. {
  1085. struct platform_device *pdev = to_platform_device(dev);
  1086. struct vpbe_display *vpbe_disp = data;
  1087. if (strcmp("vpbe_controller", pdev->name) == 0)
  1088. vpbe_disp->vpbe_dev = platform_get_drvdata(pdev);
  1089. if (strstr(pdev->name, "vpbe-osd") != NULL)
  1090. vpbe_disp->osd_device = platform_get_drvdata(pdev);
  1091. return 0;
  1092. }
  1093. static int init_vpbe_layer(int i, struct vpbe_display *disp_dev,
  1094. struct platform_device *pdev)
  1095. {
  1096. struct vpbe_layer *vpbe_display_layer = NULL;
  1097. struct video_device *vbd = NULL;
  1098. /* Allocate memory for four plane display objects */
  1099. disp_dev->dev[i] =
  1100. kzalloc(sizeof(struct vpbe_layer), GFP_KERNEL);
  1101. /* If memory allocation fails, return error */
  1102. if (!disp_dev->dev[i]) {
  1103. printk(KERN_ERR "ran out of memory\n");
  1104. return -ENOMEM;
  1105. }
  1106. spin_lock_init(&disp_dev->dev[i]->irqlock);
  1107. mutex_init(&disp_dev->dev[i]->opslock);
  1108. /* Get the pointer to the layer object */
  1109. vpbe_display_layer = disp_dev->dev[i];
  1110. vbd = &vpbe_display_layer->video_dev;
  1111. /* Initialize field of video device */
  1112. vbd->release = video_device_release_empty;
  1113. vbd->fops = &vpbe_fops;
  1114. vbd->ioctl_ops = &vpbe_ioctl_ops;
  1115. vbd->minor = -1;
  1116. vbd->v4l2_dev = &disp_dev->vpbe_dev->v4l2_dev;
  1117. vbd->lock = &vpbe_display_layer->opslock;
  1118. vbd->vfl_dir = VFL_DIR_TX;
  1119. if (disp_dev->vpbe_dev->current_timings.timings_type &
  1120. VPBE_ENC_STD)
  1121. vbd->tvnorms = (V4L2_STD_525_60 | V4L2_STD_625_50);
  1122. snprintf(vbd->name, sizeof(vbd->name),
  1123. "DaVinci_VPBE Display_DRIVER_V%d.%d.%d",
  1124. (VPBE_DISPLAY_VERSION_CODE >> 16) & 0xff,
  1125. (VPBE_DISPLAY_VERSION_CODE >> 8) & 0xff,
  1126. (VPBE_DISPLAY_VERSION_CODE) & 0xff);
  1127. vpbe_display_layer->device_id = i;
  1128. vpbe_display_layer->layer_info.id =
  1129. ((i == VPBE_DISPLAY_DEVICE_0) ? WIN_VID0 : WIN_VID1);
  1130. return 0;
  1131. }
  1132. static int register_device(struct vpbe_layer *vpbe_display_layer,
  1133. struct vpbe_display *disp_dev,
  1134. struct platform_device *pdev)
  1135. {
  1136. int err;
  1137. v4l2_info(&disp_dev->vpbe_dev->v4l2_dev,
  1138. "Trying to register VPBE display device.\n");
  1139. v4l2_info(&disp_dev->vpbe_dev->v4l2_dev,
  1140. "layer=%x,layer->video_dev=%x\n",
  1141. (int)vpbe_display_layer,
  1142. (int)&vpbe_display_layer->video_dev);
  1143. vpbe_display_layer->video_dev.queue = &vpbe_display_layer->buffer_queue;
  1144. err = video_register_device(&vpbe_display_layer->video_dev,
  1145. VFL_TYPE_GRABBER,
  1146. -1);
  1147. if (err)
  1148. return -ENODEV;
  1149. vpbe_display_layer->disp_dev = disp_dev;
  1150. /* set the driver data in platform device */
  1151. platform_set_drvdata(pdev, disp_dev);
  1152. video_set_drvdata(&vpbe_display_layer->video_dev,
  1153. vpbe_display_layer);
  1154. return 0;
  1155. }
  1156. /*
  1157. * vpbe_display_probe()
  1158. * This function creates device entries by register itself to the V4L2 driver
  1159. * and initializes fields of each layer objects
  1160. */
  1161. static int vpbe_display_probe(struct platform_device *pdev)
  1162. {
  1163. struct vpbe_display *disp_dev;
  1164. struct v4l2_device *v4l2_dev;
  1165. struct resource *res = NULL;
  1166. struct vb2_queue *q;
  1167. int k;
  1168. int i;
  1169. int err;
  1170. int irq;
  1171. printk(KERN_DEBUG "vpbe_display_probe\n");
  1172. /* Allocate memory for vpbe_display */
  1173. disp_dev = devm_kzalloc(&pdev->dev, sizeof(struct vpbe_display),
  1174. GFP_KERNEL);
  1175. if (!disp_dev)
  1176. return -ENOMEM;
  1177. spin_lock_init(&disp_dev->dma_queue_lock);
  1178. /*
  1179. * Scan all the platform devices to find the vpbe
  1180. * controller device and get the vpbe_dev object
  1181. */
  1182. err = bus_for_each_dev(&platform_bus_type, NULL, disp_dev,
  1183. vpbe_device_get);
  1184. if (err < 0)
  1185. return err;
  1186. v4l2_dev = &disp_dev->vpbe_dev->v4l2_dev;
  1187. /* Initialize the vpbe display controller */
  1188. if (NULL != disp_dev->vpbe_dev->ops.initialize) {
  1189. err = disp_dev->vpbe_dev->ops.initialize(&pdev->dev,
  1190. disp_dev->vpbe_dev);
  1191. if (err) {
  1192. v4l2_err(v4l2_dev, "Error initing vpbe\n");
  1193. err = -ENOMEM;
  1194. goto probe_out;
  1195. }
  1196. }
  1197. for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
  1198. if (init_vpbe_layer(i, disp_dev, pdev)) {
  1199. err = -ENODEV;
  1200. goto probe_out;
  1201. }
  1202. }
  1203. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1204. if (!res) {
  1205. v4l2_err(v4l2_dev, "Unable to get VENC interrupt resource\n");
  1206. err = -ENODEV;
  1207. goto probe_out;
  1208. }
  1209. irq = res->start;
  1210. err = devm_request_irq(&pdev->dev, irq, venc_isr, 0,
  1211. VPBE_DISPLAY_DRIVER, disp_dev);
  1212. if (err) {
  1213. v4l2_err(v4l2_dev, "VPBE IRQ request failed\n");
  1214. goto probe_out;
  1215. }
  1216. for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
  1217. /* initialize vb2 queue */
  1218. q = &disp_dev->dev[i]->buffer_queue;
  1219. memset(q, 0, sizeof(*q));
  1220. q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  1221. q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
  1222. q->drv_priv = disp_dev->dev[i];
  1223. q->ops = &video_qops;
  1224. q->mem_ops = &vb2_dma_contig_memops;
  1225. q->buf_struct_size = sizeof(struct vpbe_disp_buffer);
  1226. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1227. q->min_buffers_needed = 1;
  1228. q->lock = &disp_dev->dev[i]->opslock;
  1229. err = vb2_queue_init(q);
  1230. if (err) {
  1231. v4l2_err(v4l2_dev, "vb2_queue_init() failed\n");
  1232. goto probe_out;
  1233. }
  1234. disp_dev->dev[i]->alloc_ctx =
  1235. vb2_dma_contig_init_ctx(disp_dev->vpbe_dev->pdev);
  1236. if (IS_ERR(disp_dev->dev[i]->alloc_ctx)) {
  1237. v4l2_err(v4l2_dev, "Failed to get the context\n");
  1238. err = PTR_ERR(disp_dev->dev[i]->alloc_ctx);
  1239. goto probe_out;
  1240. }
  1241. INIT_LIST_HEAD(&disp_dev->dev[i]->dma_queue);
  1242. if (register_device(disp_dev->dev[i], disp_dev, pdev)) {
  1243. err = -ENODEV;
  1244. goto probe_out;
  1245. }
  1246. }
  1247. v4l2_dbg(1, debug, v4l2_dev,
  1248. "Successfully completed the probing of vpbe v4l2 device\n");
  1249. return 0;
  1250. probe_out:
  1251. for (k = 0; k < VPBE_DISPLAY_MAX_DEVICES; k++) {
  1252. /* Unregister video device */
  1253. if (disp_dev->dev[k] != NULL) {
  1254. vb2_dma_contig_cleanup_ctx(disp_dev->dev[k]->alloc_ctx);
  1255. video_unregister_device(&disp_dev->dev[k]->video_dev);
  1256. kfree(disp_dev->dev[k]);
  1257. }
  1258. }
  1259. return err;
  1260. }
  1261. /*
  1262. * vpbe_display_remove()
  1263. * It un-register hardware layer from V4L2 driver
  1264. */
  1265. static int vpbe_display_remove(struct platform_device *pdev)
  1266. {
  1267. struct vpbe_layer *vpbe_display_layer;
  1268. struct vpbe_display *disp_dev = platform_get_drvdata(pdev);
  1269. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  1270. int i;
  1271. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_remove\n");
  1272. /* deinitialize the vpbe display controller */
  1273. if (NULL != vpbe_dev->ops.deinitialize)
  1274. vpbe_dev->ops.deinitialize(&pdev->dev, vpbe_dev);
  1275. /* un-register device */
  1276. for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
  1277. /* Get the pointer to the layer object */
  1278. vpbe_display_layer = disp_dev->dev[i];
  1279. vb2_dma_contig_cleanup_ctx(vpbe_display_layer->alloc_ctx);
  1280. /* Unregister video device */
  1281. video_unregister_device(&vpbe_display_layer->video_dev);
  1282. }
  1283. for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
  1284. kfree(disp_dev->dev[i]);
  1285. disp_dev->dev[i] = NULL;
  1286. }
  1287. return 0;
  1288. }
  1289. static struct platform_driver vpbe_display_driver = {
  1290. .driver = {
  1291. .name = VPBE_DISPLAY_DRIVER,
  1292. .bus = &platform_bus_type,
  1293. },
  1294. .probe = vpbe_display_probe,
  1295. .remove = vpbe_display_remove,
  1296. };
  1297. module_platform_driver(vpbe_display_driver);
  1298. MODULE_DESCRIPTION("TI DM644x/DM355/DM365 VPBE Display controller");
  1299. MODULE_LICENSE("GPL");
  1300. MODULE_AUTHOR("Texas Instruments");