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