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. #ifdef CONFIG_ARCH_DAVINCI
  29. #include <mach/cputype.h>
  30. #endif
  31. #include <media/v4l2-dev.h>
  32. #include <media/v4l2-common.h>
  33. #include <media/v4l2-ioctl.h>
  34. #include <media/v4l2-device.h>
  35. #include <media/davinci/vpbe_display.h>
  36. #include <media/davinci/vpbe_types.h>
  37. #include <media/davinci/vpbe.h>
  38. #include <media/davinci/vpbe_venc.h>
  39. #include <media/davinci/vpbe_osd.h>
  40. #include "vpbe_venc_regs.h"
  41. #define VPBE_DISPLAY_DRIVER "vpbe-v4l2"
  42. static int debug;
  43. #define VPBE_DEFAULT_NUM_BUFS 3
  44. module_param(debug, int, 0644);
  45. static int vpbe_set_osd_display_params(struct vpbe_display *disp_dev,
  46. struct vpbe_layer *layer);
  47. static int venc_is_second_field(struct vpbe_display *disp_dev)
  48. {
  49. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  50. int ret, val;
  51. ret = v4l2_subdev_call(vpbe_dev->venc,
  52. core,
  53. ioctl,
  54. VENC_GET_FLD,
  55. &val);
  56. if (ret < 0) {
  57. v4l2_err(&vpbe_dev->v4l2_dev,
  58. "Error in getting Field ID 0\n");
  59. return 1;
  60. }
  61. return val;
  62. }
  63. static void vpbe_isr_even_field(struct vpbe_display *disp_obj,
  64. struct vpbe_layer *layer)
  65. {
  66. if (layer->cur_frm == layer->next_frm)
  67. return;
  68. layer->cur_frm->vb.vb2_buf.timestamp = ktime_get_ns();
  69. vb2_buffer_done(&layer->cur_frm->vb.vb2_buf, VB2_BUF_STATE_DONE);
  70. /* Make cur_frm pointing to next_frm */
  71. layer->cur_frm = layer->next_frm;
  72. }
  73. static void vpbe_isr_odd_field(struct vpbe_display *disp_obj,
  74. struct vpbe_layer *layer)
  75. {
  76. struct osd_state *osd_device = disp_obj->osd_device;
  77. unsigned long addr;
  78. spin_lock(&disp_obj->dma_queue_lock);
  79. if (list_empty(&layer->dma_queue) ||
  80. (layer->cur_frm != layer->next_frm)) {
  81. spin_unlock(&disp_obj->dma_queue_lock);
  82. return;
  83. }
  84. /*
  85. * one field is displayed configure
  86. * the next frame if it is available
  87. * otherwise hold on current frame
  88. * Get next from the buffer queue
  89. */
  90. layer->next_frm = list_entry(layer->dma_queue.next,
  91. struct vpbe_disp_buffer, list);
  92. /* Remove that from the buffer queue */
  93. list_del(&layer->next_frm->list);
  94. spin_unlock(&disp_obj->dma_queue_lock);
  95. /* Mark state of the frame to active */
  96. layer->next_frm->vb.vb2_buf.state = VB2_BUF_STATE_ACTIVE;
  97. addr = vb2_dma_contig_plane_dma_addr(&layer->next_frm->vb.vb2_buf, 0);
  98. osd_device->ops.start_layer(osd_device,
  99. layer->layer_info.id,
  100. addr,
  101. disp_obj->cbcr_ofst);
  102. }
  103. /* interrupt service routine */
  104. static irqreturn_t venc_isr(int irq, void *arg)
  105. {
  106. struct vpbe_display *disp_dev = (struct vpbe_display *)arg;
  107. struct vpbe_layer *layer;
  108. static unsigned last_event;
  109. unsigned event = 0;
  110. int fid;
  111. int i;
  112. if (!arg || !disp_dev->dev[0])
  113. return IRQ_HANDLED;
  114. if (venc_is_second_field(disp_dev))
  115. event |= VENC_SECOND_FIELD;
  116. else
  117. event |= VENC_FIRST_FIELD;
  118. if (event == (last_event & ~VENC_END_OF_FRAME)) {
  119. /*
  120. * If the display is non-interlaced, then we need to flag the
  121. * end-of-frame event at every interrupt regardless of the
  122. * value of the FIDST bit. We can conclude that the display is
  123. * non-interlaced if the value of the FIDST bit is unchanged
  124. * from the previous interrupt.
  125. */
  126. event |= VENC_END_OF_FRAME;
  127. } else if (event == VENC_SECOND_FIELD) {
  128. /* end-of-frame for interlaced display */
  129. event |= VENC_END_OF_FRAME;
  130. }
  131. last_event = event;
  132. for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
  133. layer = disp_dev->dev[i];
  134. if (!vb2_start_streaming_called(&layer->buffer_queue))
  135. continue;
  136. if (layer->layer_first_int) {
  137. layer->layer_first_int = 0;
  138. continue;
  139. }
  140. /* Check the field format */
  141. if ((V4L2_FIELD_NONE == layer->pix_fmt.field) &&
  142. (event & VENC_END_OF_FRAME)) {
  143. /* Progressive mode */
  144. vpbe_isr_even_field(disp_dev, layer);
  145. vpbe_isr_odd_field(disp_dev, layer);
  146. } else {
  147. /* Interlaced mode */
  148. layer->field_id ^= 1;
  149. if (event & VENC_FIRST_FIELD)
  150. fid = 0;
  151. else
  152. fid = 1;
  153. /*
  154. * If field id does not match with store
  155. * field id
  156. */
  157. if (fid != layer->field_id) {
  158. /* Make them in sync */
  159. layer->field_id = fid;
  160. continue;
  161. }
  162. /*
  163. * device field id and local field id are
  164. * in sync. If this is even field
  165. */
  166. if (0 == fid)
  167. vpbe_isr_even_field(disp_dev, layer);
  168. else /* odd field */
  169. vpbe_isr_odd_field(disp_dev, layer);
  170. }
  171. }
  172. return IRQ_HANDLED;
  173. }
  174. /*
  175. * vpbe_buffer_prepare()
  176. * This is the callback function called from vb2_qbuf() function
  177. * the buffer is prepared and user space virtual address is converted into
  178. * physical address
  179. */
  180. static int vpbe_buffer_prepare(struct vb2_buffer *vb)
  181. {
  182. struct vb2_queue *q = vb->vb2_queue;
  183. struct vpbe_layer *layer = vb2_get_drv_priv(q);
  184. struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
  185. unsigned long addr;
  186. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  187. "vpbe_buffer_prepare\n");
  188. vb2_set_plane_payload(vb, 0, layer->pix_fmt.sizeimage);
  189. if (vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
  190. return -EINVAL;
  191. addr = vb2_dma_contig_plane_dma_addr(vb, 0);
  192. if (!IS_ALIGNED(addr, 8)) {
  193. v4l2_err(&vpbe_dev->v4l2_dev,
  194. "buffer_prepare:offset is not aligned to 32 bytes\n");
  195. return -EINVAL;
  196. }
  197. return 0;
  198. }
  199. /*
  200. * vpbe_buffer_setup()
  201. * This function allocates memory for the buffers
  202. */
  203. static int
  204. vpbe_buffer_queue_setup(struct vb2_queue *vq,
  205. unsigned int *nbuffers, unsigned int *nplanes,
  206. unsigned int sizes[], struct device *alloc_devs[])
  207. {
  208. /* Get the file handle object and layer object */
  209. struct vpbe_layer *layer = vb2_get_drv_priv(vq);
  210. struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
  211. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_buffer_setup\n");
  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. if (*nplanes)
  216. return sizes[0] < layer->pix_fmt.sizeimage ? -EINVAL : 0;
  217. *nplanes = 1;
  218. sizes[0] = layer->pix_fmt.sizeimage;
  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)
  293. vb2_buffer_done(&layer->cur_frm->vb.vb2_buf,
  294. VB2_BUF_STATE_ERROR);
  295. if (layer->next_frm)
  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 const 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_SELECTION 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->v_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_selection(struct file *file, void *priv,
  565. struct v4l2_selection *sel)
  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 = sel->r;
  573. int ret;
  574. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  575. "VIDIOC_S_SELECTION, layer id = %d\n", layer->device_id);
  576. if (sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
  577. sel->target != V4L2_SEL_TGT_CROP)
  578. return -EINVAL;
  579. if (rect.top < 0)
  580. rect.top = 0;
  581. if (rect.left < 0)
  582. rect.left = 0;
  583. vpbe_disp_check_window_params(disp_dev, &rect);
  584. osd_device->ops.get_layer_config(osd_device,
  585. layer->layer_info.id, cfg);
  586. vpbe_disp_calculate_scale_factor(disp_dev, layer,
  587. rect.width,
  588. rect.height);
  589. vpbe_disp_adj_position(disp_dev, layer, rect.top,
  590. rect.left);
  591. ret = osd_device->ops.set_layer_config(osd_device,
  592. layer->layer_info.id, cfg);
  593. if (ret < 0) {
  594. v4l2_err(&vpbe_dev->v4l2_dev,
  595. "Error in set layer config:\n");
  596. return -EINVAL;
  597. }
  598. /* apply zooming and h or v expansion */
  599. osd_device->ops.set_zoom(osd_device,
  600. layer->layer_info.id,
  601. layer->layer_info.h_zoom,
  602. layer->layer_info.v_zoom);
  603. ret = osd_device->ops.set_vid_expansion(osd_device,
  604. layer->layer_info.h_exp,
  605. layer->layer_info.v_exp);
  606. if (ret < 0) {
  607. v4l2_err(&vpbe_dev->v4l2_dev,
  608. "Error in set vid expansion:\n");
  609. return -EINVAL;
  610. }
  611. if ((layer->layer_info.h_zoom != ZOOM_X1) ||
  612. (layer->layer_info.v_zoom != ZOOM_X1) ||
  613. (layer->layer_info.h_exp != H_EXP_OFF) ||
  614. (layer->layer_info.v_exp != V_EXP_OFF))
  615. /* Enable expansion filter */
  616. osd_device->ops.set_interpolation_filter(osd_device, 1);
  617. else
  618. osd_device->ops.set_interpolation_filter(osd_device, 0);
  619. sel->r = rect;
  620. return 0;
  621. }
  622. static int vpbe_display_g_selection(struct file *file, void *priv,
  623. struct v4l2_selection *sel)
  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 = &sel->r;
  630. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  631. "VIDIOC_G_SELECTION, layer id = %d\n",
  632. layer->device_id);
  633. if (sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  634. return -EINVAL;
  635. switch (sel->target) {
  636. case V4L2_SEL_TGT_CROP:
  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. break;
  644. case V4L2_SEL_TGT_CROP_DEFAULT:
  645. case V4L2_SEL_TGT_CROP_BOUNDS:
  646. rect->left = 0;
  647. rect->top = 0;
  648. rect->width = vpbe_dev->current_timings.xres;
  649. rect->height = vpbe_dev->current_timings.yres;
  650. break;
  651. default:
  652. return -EINVAL;
  653. }
  654. return 0;
  655. }
  656. static int vpbe_display_cropcap(struct file *file, void *priv,
  657. struct v4l2_cropcap *cropcap)
  658. {
  659. struct vpbe_layer *layer = video_drvdata(file);
  660. struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
  661. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_CROPCAP ioctl\n");
  662. if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  663. return -EINVAL;
  664. cropcap->pixelaspect = vpbe_dev->current_timings.aspect;
  665. return 0;
  666. }
  667. static int vpbe_display_g_fmt(struct file *file, void *priv,
  668. struct v4l2_format *fmt)
  669. {
  670. struct vpbe_layer *layer = video_drvdata(file);
  671. struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
  672. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  673. "VIDIOC_G_FMT, layer id = %d\n",
  674. layer->device_id);
  675. /* If buffer type is video output */
  676. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != fmt->type) {
  677. v4l2_err(&vpbe_dev->v4l2_dev, "invalid type\n");
  678. return -EINVAL;
  679. }
  680. /* Fill in the information about format */
  681. fmt->fmt.pix = layer->pix_fmt;
  682. return 0;
  683. }
  684. static int vpbe_display_enum_fmt(struct file *file, void *priv,
  685. struct v4l2_fmtdesc *fmt)
  686. {
  687. struct vpbe_layer *layer = video_drvdata(file);
  688. struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
  689. unsigned int index = 0;
  690. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  691. "VIDIOC_ENUM_FMT, layer id = %d\n",
  692. layer->device_id);
  693. if (fmt->index > 1) {
  694. v4l2_err(&vpbe_dev->v4l2_dev, "Invalid format index\n");
  695. return -EINVAL;
  696. }
  697. /* Fill in the information about format */
  698. index = fmt->index;
  699. memset(fmt, 0, sizeof(*fmt));
  700. fmt->index = index;
  701. fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  702. if (index == 0) {
  703. strcpy(fmt->description, "YUV 4:2:2 - UYVY");
  704. fmt->pixelformat = V4L2_PIX_FMT_UYVY;
  705. } else {
  706. strcpy(fmt->description, "Y/CbCr 4:2:0");
  707. fmt->pixelformat = V4L2_PIX_FMT_NV12;
  708. }
  709. return 0;
  710. }
  711. static int vpbe_display_s_fmt(struct file *file, void *priv,
  712. struct v4l2_format *fmt)
  713. {
  714. struct vpbe_layer *layer = video_drvdata(file);
  715. struct vpbe_display *disp_dev = layer->disp_dev;
  716. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  717. struct osd_layer_config *cfg = &layer->layer_info.config;
  718. struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
  719. struct osd_state *osd_device = disp_dev->osd_device;
  720. int ret;
  721. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  722. "VIDIOC_S_FMT, layer id = %d\n",
  723. layer->device_id);
  724. if (vb2_is_busy(&layer->buffer_queue))
  725. return -EBUSY;
  726. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != fmt->type) {
  727. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "invalid type\n");
  728. return -EINVAL;
  729. }
  730. /* Check for valid pixel format */
  731. ret = vpbe_try_format(disp_dev, pixfmt, 1);
  732. if (ret)
  733. return ret;
  734. /* YUV420 is requested, check availability of the
  735. other video window */
  736. layer->pix_fmt = *pixfmt;
  737. if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12) {
  738. struct vpbe_layer *otherlayer;
  739. otherlayer = _vpbe_display_get_other_win_layer(disp_dev, layer);
  740. /* if other layer is available, only
  741. * claim it, do not configure it
  742. */
  743. ret = osd_device->ops.request_layer(osd_device,
  744. otherlayer->layer_info.id);
  745. if (ret < 0) {
  746. v4l2_err(&vpbe_dev->v4l2_dev,
  747. "Display Manager failed to allocate layer\n");
  748. return -EBUSY;
  749. }
  750. }
  751. /* Get osd layer config */
  752. osd_device->ops.get_layer_config(osd_device,
  753. layer->layer_info.id, cfg);
  754. /* Store the pixel format in the layer object */
  755. cfg->xsize = pixfmt->width;
  756. cfg->ysize = pixfmt->height;
  757. cfg->line_length = pixfmt->bytesperline;
  758. cfg->ypos = 0;
  759. cfg->xpos = 0;
  760. cfg->interlaced = vpbe_dev->current_timings.interlaced;
  761. if (V4L2_PIX_FMT_UYVY == pixfmt->pixelformat)
  762. cfg->pixfmt = PIXFMT_YCBCRI;
  763. /* Change of the default pixel format for both video windows */
  764. if (V4L2_PIX_FMT_NV12 == pixfmt->pixelformat) {
  765. struct vpbe_layer *otherlayer;
  766. cfg->pixfmt = PIXFMT_NV12;
  767. otherlayer = _vpbe_display_get_other_win_layer(disp_dev,
  768. layer);
  769. otherlayer->layer_info.config.pixfmt = PIXFMT_NV12;
  770. }
  771. /* Set the layer config in the osd window */
  772. ret = osd_device->ops.set_layer_config(osd_device,
  773. layer->layer_info.id, cfg);
  774. if (ret < 0) {
  775. v4l2_err(&vpbe_dev->v4l2_dev,
  776. "Error in S_FMT params:\n");
  777. return -EINVAL;
  778. }
  779. /* Readback and fill the local copy of current pix format */
  780. osd_device->ops.get_layer_config(osd_device,
  781. layer->layer_info.id, cfg);
  782. return 0;
  783. }
  784. static int vpbe_display_try_fmt(struct file *file, void *priv,
  785. struct v4l2_format *fmt)
  786. {
  787. struct vpbe_layer *layer = video_drvdata(file);
  788. struct vpbe_display *disp_dev = layer->disp_dev;
  789. struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
  790. struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
  791. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_TRY_FMT\n");
  792. if (V4L2_BUF_TYPE_VIDEO_OUTPUT != fmt->type) {
  793. v4l2_err(&vpbe_dev->v4l2_dev, "invalid type\n");
  794. return -EINVAL;
  795. }
  796. /* Check for valid field format */
  797. return vpbe_try_format(disp_dev, pixfmt, 0);
  798. }
  799. /*
  800. * vpbe_display_s_std - Set the given standard in the encoder
  801. *
  802. * Sets the standard if supported by the current encoder. Return the status.
  803. * 0 - success & -EINVAL on error
  804. */
  805. static int vpbe_display_s_std(struct file *file, void *priv,
  806. v4l2_std_id std_id)
  807. {
  808. struct vpbe_layer *layer = video_drvdata(file);
  809. struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
  810. int ret;
  811. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_S_STD\n");
  812. if (vb2_is_busy(&layer->buffer_queue))
  813. return -EBUSY;
  814. if (vpbe_dev->ops.s_std) {
  815. ret = vpbe_dev->ops.s_std(vpbe_dev, std_id);
  816. if (ret) {
  817. v4l2_err(&vpbe_dev->v4l2_dev,
  818. "Failed to set standard for sub devices\n");
  819. return -EINVAL;
  820. }
  821. } else {
  822. return -EINVAL;
  823. }
  824. return 0;
  825. }
  826. /*
  827. * vpbe_display_g_std - Get the standard in the current encoder
  828. *
  829. * Get the standard in the current encoder. Return the status. 0 - success
  830. * -EINVAL on error
  831. */
  832. static int vpbe_display_g_std(struct file *file, void *priv,
  833. v4l2_std_id *std_id)
  834. {
  835. struct vpbe_layer *layer = video_drvdata(file);
  836. struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
  837. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_G_STD\n");
  838. /* Get the standard from the current encoder */
  839. if (vpbe_dev->current_timings.timings_type & VPBE_ENC_STD) {
  840. *std_id = vpbe_dev->current_timings.std_id;
  841. return 0;
  842. }
  843. return -EINVAL;
  844. }
  845. /*
  846. * vpbe_display_enum_output - enumerate outputs
  847. *
  848. * Enumerates the outputs available at the vpbe display
  849. * returns the status, -EINVAL if end of output list
  850. */
  851. static int vpbe_display_enum_output(struct file *file, void *priv,
  852. struct v4l2_output *output)
  853. {
  854. struct vpbe_layer *layer = video_drvdata(file);
  855. struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
  856. int ret;
  857. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_ENUM_OUTPUT\n");
  858. /* Enumerate outputs */
  859. if (!vpbe_dev->ops.enum_outputs)
  860. return -EINVAL;
  861. ret = vpbe_dev->ops.enum_outputs(vpbe_dev, output);
  862. if (ret) {
  863. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  864. "Failed to enumerate outputs\n");
  865. return -EINVAL;
  866. }
  867. return 0;
  868. }
  869. /*
  870. * vpbe_display_s_output - Set output to
  871. * the output specified by the index
  872. */
  873. static int vpbe_display_s_output(struct file *file, void *priv,
  874. unsigned int i)
  875. {
  876. struct vpbe_layer *layer = video_drvdata(file);
  877. struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
  878. int ret;
  879. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_S_OUTPUT\n");
  880. if (vb2_is_busy(&layer->buffer_queue))
  881. return -EBUSY;
  882. if (!vpbe_dev->ops.set_output)
  883. return -EINVAL;
  884. ret = vpbe_dev->ops.set_output(vpbe_dev, i);
  885. if (ret) {
  886. v4l2_err(&vpbe_dev->v4l2_dev,
  887. "Failed to set output for sub devices\n");
  888. return -EINVAL;
  889. }
  890. return 0;
  891. }
  892. /*
  893. * vpbe_display_g_output - Get output from subdevice
  894. * for a given by the index
  895. */
  896. static int vpbe_display_g_output(struct file *file, void *priv,
  897. unsigned int *i)
  898. {
  899. struct vpbe_layer *layer = video_drvdata(file);
  900. struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
  901. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_G_OUTPUT\n");
  902. /* Get the standard from the current encoder */
  903. *i = vpbe_dev->current_out_index;
  904. return 0;
  905. }
  906. /*
  907. * vpbe_display_enum_dv_timings - Enumerate the dv timings
  908. *
  909. * enum the timings in the current encoder. Return the status. 0 - success
  910. * -EINVAL on error
  911. */
  912. static int
  913. vpbe_display_enum_dv_timings(struct file *file, void *priv,
  914. struct v4l2_enum_dv_timings *timings)
  915. {
  916. struct vpbe_layer *layer = video_drvdata(file);
  917. struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
  918. int ret;
  919. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_ENUM_DV_TIMINGS\n");
  920. /* Enumerate outputs */
  921. if (!vpbe_dev->ops.enum_dv_timings)
  922. return -EINVAL;
  923. ret = vpbe_dev->ops.enum_dv_timings(vpbe_dev, timings);
  924. if (ret) {
  925. v4l2_err(&vpbe_dev->v4l2_dev,
  926. "Failed to enumerate dv timings info\n");
  927. return -EINVAL;
  928. }
  929. return 0;
  930. }
  931. /*
  932. * vpbe_display_s_dv_timings - Set the dv timings
  933. *
  934. * Set the timings in the current encoder. Return the status. 0 - success
  935. * -EINVAL on error
  936. */
  937. static int
  938. vpbe_display_s_dv_timings(struct file *file, void *priv,
  939. struct v4l2_dv_timings *timings)
  940. {
  941. struct vpbe_layer *layer = video_drvdata(file);
  942. struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
  943. int ret;
  944. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_S_DV_TIMINGS\n");
  945. if (vb2_is_busy(&layer->buffer_queue))
  946. return -EBUSY;
  947. /* Set the given standard in the encoder */
  948. if (!vpbe_dev->ops.s_dv_timings)
  949. return -EINVAL;
  950. ret = vpbe_dev->ops.s_dv_timings(vpbe_dev, timings);
  951. if (ret) {
  952. v4l2_err(&vpbe_dev->v4l2_dev,
  953. "Failed to set the dv timings info\n");
  954. return -EINVAL;
  955. }
  956. return 0;
  957. }
  958. /*
  959. * vpbe_display_g_dv_timings - Set the dv timings
  960. *
  961. * Get the timings in the current encoder. Return the status. 0 - success
  962. * -EINVAL on error
  963. */
  964. static int
  965. vpbe_display_g_dv_timings(struct file *file, void *priv,
  966. struct v4l2_dv_timings *dv_timings)
  967. {
  968. struct vpbe_layer *layer = video_drvdata(file);
  969. struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
  970. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_G_DV_TIMINGS\n");
  971. /* Get the given standard in the encoder */
  972. if (vpbe_dev->current_timings.timings_type &
  973. VPBE_ENC_DV_TIMINGS) {
  974. *dv_timings = vpbe_dev->current_timings.dv_timings;
  975. } else {
  976. return -EINVAL;
  977. }
  978. return 0;
  979. }
  980. /*
  981. * vpbe_display_open()
  982. * It creates object of file handle structure and stores it in private_data
  983. * member of filepointer
  984. */
  985. static int vpbe_display_open(struct file *file)
  986. {
  987. struct vpbe_layer *layer = video_drvdata(file);
  988. struct vpbe_display *disp_dev = layer->disp_dev;
  989. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  990. struct osd_state *osd_device = disp_dev->osd_device;
  991. int err;
  992. /* creating context for file descriptor */
  993. err = v4l2_fh_open(file);
  994. if (err) {
  995. v4l2_err(&vpbe_dev->v4l2_dev, "v4l2_fh_open failed\n");
  996. return err;
  997. }
  998. /* leaving if layer is already initialized */
  999. if (!v4l2_fh_is_singular_file(file))
  1000. return err;
  1001. if (!layer->usrs) {
  1002. if (mutex_lock_interruptible(&layer->opslock))
  1003. return -ERESTARTSYS;
  1004. /* First claim the layer for this device */
  1005. err = osd_device->ops.request_layer(osd_device,
  1006. layer->layer_info.id);
  1007. mutex_unlock(&layer->opslock);
  1008. if (err < 0) {
  1009. /* Couldn't get layer */
  1010. v4l2_err(&vpbe_dev->v4l2_dev,
  1011. "Display Manager failed to allocate layer\n");
  1012. v4l2_fh_release(file);
  1013. return -EINVAL;
  1014. }
  1015. }
  1016. /* Increment layer usrs counter */
  1017. layer->usrs++;
  1018. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
  1019. "vpbe display device opened successfully\n");
  1020. return 0;
  1021. }
  1022. /*
  1023. * vpbe_display_release()
  1024. * This function deletes buffer queue, frees the buffers and the davinci
  1025. * display file * handle
  1026. */
  1027. static int vpbe_display_release(struct file *file)
  1028. {
  1029. struct vpbe_layer *layer = video_drvdata(file);
  1030. struct osd_layer_config *cfg = &layer->layer_info.config;
  1031. struct vpbe_display *disp_dev = layer->disp_dev;
  1032. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  1033. struct osd_state *osd_device = disp_dev->osd_device;
  1034. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_release\n");
  1035. mutex_lock(&layer->opslock);
  1036. osd_device->ops.disable_layer(osd_device,
  1037. layer->layer_info.id);
  1038. /* Decrement layer usrs counter */
  1039. layer->usrs--;
  1040. /* If this file handle has initialize encoder device, reset it */
  1041. if (!layer->usrs) {
  1042. if (cfg->pixfmt == PIXFMT_NV12) {
  1043. struct vpbe_layer *otherlayer;
  1044. otherlayer =
  1045. _vpbe_display_get_other_win_layer(disp_dev, layer);
  1046. osd_device->ops.disable_layer(osd_device,
  1047. otherlayer->layer_info.id);
  1048. osd_device->ops.release_layer(osd_device,
  1049. otherlayer->layer_info.id);
  1050. }
  1051. osd_device->ops.disable_layer(osd_device,
  1052. layer->layer_info.id);
  1053. osd_device->ops.release_layer(osd_device,
  1054. layer->layer_info.id);
  1055. }
  1056. _vb2_fop_release(file, NULL);
  1057. mutex_unlock(&layer->opslock);
  1058. disp_dev->cbcr_ofst = 0;
  1059. return 0;
  1060. }
  1061. /* vpbe capture ioctl operations */
  1062. static const struct v4l2_ioctl_ops vpbe_ioctl_ops = {
  1063. .vidioc_querycap = vpbe_display_querycap,
  1064. .vidioc_g_fmt_vid_out = vpbe_display_g_fmt,
  1065. .vidioc_enum_fmt_vid_out = vpbe_display_enum_fmt,
  1066. .vidioc_s_fmt_vid_out = vpbe_display_s_fmt,
  1067. .vidioc_try_fmt_vid_out = vpbe_display_try_fmt,
  1068. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  1069. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  1070. .vidioc_querybuf = vb2_ioctl_querybuf,
  1071. .vidioc_qbuf = vb2_ioctl_qbuf,
  1072. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  1073. .vidioc_streamon = vb2_ioctl_streamon,
  1074. .vidioc_streamoff = vb2_ioctl_streamoff,
  1075. .vidioc_expbuf = vb2_ioctl_expbuf,
  1076. .vidioc_cropcap = vpbe_display_cropcap,
  1077. .vidioc_g_selection = vpbe_display_g_selection,
  1078. .vidioc_s_selection = vpbe_display_s_selection,
  1079. .vidioc_s_std = vpbe_display_s_std,
  1080. .vidioc_g_std = vpbe_display_g_std,
  1081. .vidioc_enum_output = vpbe_display_enum_output,
  1082. .vidioc_s_output = vpbe_display_s_output,
  1083. .vidioc_g_output = vpbe_display_g_output,
  1084. .vidioc_s_dv_timings = vpbe_display_s_dv_timings,
  1085. .vidioc_g_dv_timings = vpbe_display_g_dv_timings,
  1086. .vidioc_enum_dv_timings = vpbe_display_enum_dv_timings,
  1087. };
  1088. static const struct v4l2_file_operations vpbe_fops = {
  1089. .owner = THIS_MODULE,
  1090. .open = vpbe_display_open,
  1091. .release = vpbe_display_release,
  1092. .unlocked_ioctl = video_ioctl2,
  1093. .mmap = vb2_fop_mmap,
  1094. .poll = vb2_fop_poll,
  1095. };
  1096. static int vpbe_device_get(struct device *dev, void *data)
  1097. {
  1098. struct platform_device *pdev = to_platform_device(dev);
  1099. struct vpbe_display *vpbe_disp = data;
  1100. if (strcmp("vpbe_controller", pdev->name) == 0)
  1101. vpbe_disp->vpbe_dev = platform_get_drvdata(pdev);
  1102. if (strstr(pdev->name, "vpbe-osd"))
  1103. vpbe_disp->osd_device = platform_get_drvdata(pdev);
  1104. return 0;
  1105. }
  1106. static int init_vpbe_layer(int i, struct vpbe_display *disp_dev,
  1107. struct platform_device *pdev)
  1108. {
  1109. struct vpbe_layer *vpbe_display_layer = NULL;
  1110. struct video_device *vbd = NULL;
  1111. /* Allocate memory for four plane display objects */
  1112. disp_dev->dev[i] = kzalloc(sizeof(*disp_dev->dev[i]), GFP_KERNEL);
  1113. if (!disp_dev->dev[i])
  1114. return -ENOMEM;
  1115. spin_lock_init(&disp_dev->dev[i]->irqlock);
  1116. mutex_init(&disp_dev->dev[i]->opslock);
  1117. /* Get the pointer to the layer object */
  1118. vpbe_display_layer = disp_dev->dev[i];
  1119. vbd = &vpbe_display_layer->video_dev;
  1120. /* Initialize field of video device */
  1121. vbd->release = video_device_release_empty;
  1122. vbd->fops = &vpbe_fops;
  1123. vbd->ioctl_ops = &vpbe_ioctl_ops;
  1124. vbd->minor = -1;
  1125. vbd->v4l2_dev = &disp_dev->vpbe_dev->v4l2_dev;
  1126. vbd->lock = &vpbe_display_layer->opslock;
  1127. vbd->vfl_dir = VFL_DIR_TX;
  1128. if (disp_dev->vpbe_dev->current_timings.timings_type &
  1129. VPBE_ENC_STD)
  1130. vbd->tvnorms = (V4L2_STD_525_60 | V4L2_STD_625_50);
  1131. snprintf(vbd->name, sizeof(vbd->name),
  1132. "DaVinci_VPBE Display_DRIVER_V%d.%d.%d",
  1133. (VPBE_DISPLAY_VERSION_CODE >> 16) & 0xff,
  1134. (VPBE_DISPLAY_VERSION_CODE >> 8) & 0xff,
  1135. (VPBE_DISPLAY_VERSION_CODE) & 0xff);
  1136. vpbe_display_layer->device_id = i;
  1137. vpbe_display_layer->layer_info.id =
  1138. ((i == VPBE_DISPLAY_DEVICE_0) ? WIN_VID0 : WIN_VID1);
  1139. return 0;
  1140. }
  1141. static int register_device(struct vpbe_layer *vpbe_display_layer,
  1142. struct vpbe_display *disp_dev,
  1143. struct platform_device *pdev)
  1144. {
  1145. int err;
  1146. v4l2_info(&disp_dev->vpbe_dev->v4l2_dev,
  1147. "Trying to register VPBE display device.\n");
  1148. v4l2_info(&disp_dev->vpbe_dev->v4l2_dev,
  1149. "layer=%p,layer->video_dev=%p\n",
  1150. vpbe_display_layer,
  1151. &vpbe_display_layer->video_dev);
  1152. vpbe_display_layer->video_dev.queue = &vpbe_display_layer->buffer_queue;
  1153. err = video_register_device(&vpbe_display_layer->video_dev,
  1154. VFL_TYPE_GRABBER,
  1155. -1);
  1156. if (err)
  1157. return -ENODEV;
  1158. vpbe_display_layer->disp_dev = disp_dev;
  1159. /* set the driver data in platform device */
  1160. platform_set_drvdata(pdev, disp_dev);
  1161. video_set_drvdata(&vpbe_display_layer->video_dev,
  1162. vpbe_display_layer);
  1163. return 0;
  1164. }
  1165. /*
  1166. * vpbe_display_probe()
  1167. * This function creates device entries by register itself to the V4L2 driver
  1168. * and initializes fields of each layer objects
  1169. */
  1170. static int vpbe_display_probe(struct platform_device *pdev)
  1171. {
  1172. struct vpbe_display *disp_dev;
  1173. struct v4l2_device *v4l2_dev;
  1174. struct resource *res = NULL;
  1175. struct vb2_queue *q;
  1176. int k;
  1177. int i;
  1178. int err;
  1179. int irq;
  1180. printk(KERN_DEBUG "vpbe_display_probe\n");
  1181. /* Allocate memory for vpbe_display */
  1182. disp_dev = devm_kzalloc(&pdev->dev, sizeof(*disp_dev), GFP_KERNEL);
  1183. if (!disp_dev)
  1184. return -ENOMEM;
  1185. spin_lock_init(&disp_dev->dma_queue_lock);
  1186. /*
  1187. * Scan all the platform devices to find the vpbe
  1188. * controller device and get the vpbe_dev object
  1189. */
  1190. err = bus_for_each_dev(&platform_bus_type, NULL, disp_dev,
  1191. vpbe_device_get);
  1192. if (err < 0)
  1193. return err;
  1194. v4l2_dev = &disp_dev->vpbe_dev->v4l2_dev;
  1195. /* Initialize the vpbe display controller */
  1196. if (disp_dev->vpbe_dev->ops.initialize) {
  1197. err = disp_dev->vpbe_dev->ops.initialize(&pdev->dev,
  1198. disp_dev->vpbe_dev);
  1199. if (err) {
  1200. v4l2_err(v4l2_dev, "Error initing vpbe\n");
  1201. err = -ENOMEM;
  1202. goto probe_out;
  1203. }
  1204. }
  1205. for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
  1206. if (init_vpbe_layer(i, disp_dev, pdev)) {
  1207. err = -ENODEV;
  1208. goto probe_out;
  1209. }
  1210. }
  1211. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1212. if (!res) {
  1213. v4l2_err(v4l2_dev, "Unable to get VENC interrupt resource\n");
  1214. err = -ENODEV;
  1215. goto probe_out;
  1216. }
  1217. irq = res->start;
  1218. err = devm_request_irq(&pdev->dev, irq, venc_isr, 0,
  1219. VPBE_DISPLAY_DRIVER, disp_dev);
  1220. if (err) {
  1221. v4l2_err(v4l2_dev, "VPBE IRQ request failed\n");
  1222. goto probe_out;
  1223. }
  1224. for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
  1225. /* initialize vb2 queue */
  1226. q = &disp_dev->dev[i]->buffer_queue;
  1227. memset(q, 0, sizeof(*q));
  1228. q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  1229. q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
  1230. q->drv_priv = disp_dev->dev[i];
  1231. q->ops = &video_qops;
  1232. q->mem_ops = &vb2_dma_contig_memops;
  1233. q->buf_struct_size = sizeof(struct vpbe_disp_buffer);
  1234. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1235. q->min_buffers_needed = 1;
  1236. q->lock = &disp_dev->dev[i]->opslock;
  1237. q->dev = disp_dev->vpbe_dev->pdev;
  1238. err = vb2_queue_init(q);
  1239. if (err) {
  1240. v4l2_err(v4l2_dev, "vb2_queue_init() failed\n");
  1241. goto probe_out;
  1242. }
  1243. INIT_LIST_HEAD(&disp_dev->dev[i]->dma_queue);
  1244. if (register_device(disp_dev->dev[i], disp_dev, pdev)) {
  1245. err = -ENODEV;
  1246. goto probe_out;
  1247. }
  1248. }
  1249. v4l2_dbg(1, debug, v4l2_dev,
  1250. "Successfully completed the probing of vpbe v4l2 device\n");
  1251. return 0;
  1252. probe_out:
  1253. for (k = 0; k < VPBE_DISPLAY_MAX_DEVICES; k++) {
  1254. /* Unregister video device */
  1255. if (disp_dev->dev[k]) {
  1256. video_unregister_device(&disp_dev->dev[k]->video_dev);
  1257. kfree(disp_dev->dev[k]);
  1258. }
  1259. }
  1260. return err;
  1261. }
  1262. /*
  1263. * vpbe_display_remove()
  1264. * It un-register hardware layer from V4L2 driver
  1265. */
  1266. static int vpbe_display_remove(struct platform_device *pdev)
  1267. {
  1268. struct vpbe_layer *vpbe_display_layer;
  1269. struct vpbe_display *disp_dev = platform_get_drvdata(pdev);
  1270. struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
  1271. int i;
  1272. v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_remove\n");
  1273. /* deinitialize the vpbe display controller */
  1274. if (vpbe_dev->ops.deinitialize)
  1275. vpbe_dev->ops.deinitialize(&pdev->dev, vpbe_dev);
  1276. /* un-register device */
  1277. for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
  1278. /* Get the pointer to the layer object */
  1279. vpbe_display_layer = disp_dev->dev[i];
  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");