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