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