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