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