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