vpbe_display.c 42 KB

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