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