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