blob: 2a4c2914d077ddbacad37dace8023742c5bbd377 [file] [log] [blame]
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001/*
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
44static int debug;
45
Manjunath Hadlia2c25b42011-06-17 04:01:31 -030046#define VPBE_DEFAULT_NUM_BUFS 3
47
48module_param(debug, int, 0644);
49
Lad, Prabhakar13fc23d32012-10-22 09:27:13 -030050static int vpbe_set_osd_display_params(struct vpbe_display *disp_dev,
51 struct vpbe_layer *layer);
52
Manjunath Hadlia2c25b42011-06-17 04:01:31 -030053static 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
71static void vpbe_isr_even_field(struct vpbe_display *disp_obj,
72 struct vpbe_layer *layer)
73{
Manjunath Hadlia2c25b42011-06-17 04:01:31 -030074 if (layer->cur_frm == layer->next_frm)
75 return;
Lad, Prabhakare9763992015-05-26 11:20:27 -030076
Junghak Sungd6dd6452015-11-03 08:16:37 -020077 layer->cur_frm->vb.vb2_buf.timestamp = ktime_get_ns();
Junghak Sung2d700712015-09-22 10:30:30 -030078 vb2_buffer_done(&layer->cur_frm->vb.vb2_buf, VB2_BUF_STATE_DONE);
Manjunath Hadlia2c25b42011-06-17 04:01:31 -030079 /* Make cur_frm pointing to next_frm */
80 layer->cur_frm = layer->next_frm;
81}
82
83static 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 */
Lad, Prabhakar13fc23d32012-10-22 09:27:13 -0300101 layer->next_frm = list_entry(layer->dma_queue.next,
102 struct vpbe_disp_buffer, list);
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300103 /* Remove that from the buffer queue */
Lad, Prabhakar13fc23d32012-10-22 09:27:13 -0300104 list_del(&layer->next_frm->list);
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300105 spin_unlock(&disp_obj->dma_queue_lock);
106 /* Mark state of the frame to active */
Junghak Sung2d700712015-09-22 10:30:30 -0300107 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);
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300109 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 */
116static 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];
Prabhakar Lad1b73f032014-10-12 17:40:42 -0300150
151 if (!vb2_start_streaming_called(&layer->buffer_queue))
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300152 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()
Lad, Prabhakar13fc23d32012-10-22 09:27:13 -0300199 * This is the callback function called from vb2_qbuf() function
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300200 * the buffer is prepared and user space virtual address is converted into
201 * physical address
202 */
Lad, Prabhakar13fc23d32012-10-22 09:27:13 -0300203static int vpbe_buffer_prepare(struct vb2_buffer *vb)
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300204{
Lad, Prabhakar13fc23d32012-10-22 09:27:13 -0300205 struct vb2_queue *q = vb->vb2_queue;
Prabhakar Lade71a1802014-10-12 17:40:31 -0300206 struct vpbe_layer *layer = vb2_get_drv_priv(q);
207 struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300208 unsigned long addr;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300209
210 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
211 "vpbe_buffer_prepare\n");
212
Prabhakar Lad50d94812014-10-12 17:40:35 -0300213 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;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300216
Prabhakar Lad50d94812014-10-12 17:40:35 -0300217 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;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300222 }
223 return 0;
224}
225
226/*
227 * vpbe_buffer_setup()
228 * This function allocates memory for the buffers
229 */
Lad, Prabhakar13fc23d32012-10-22 09:27:13 -0300230static int
Hans Verkuildf9ecb0c2015-10-28 00:50:37 -0200231vpbe_buffer_queue_setup(struct vb2_queue *vq,
Lad, Prabhakar13fc23d32012-10-22 09:27:13 -0300232 unsigned int *nbuffers, unsigned int *nplanes,
233 unsigned int sizes[], void *alloc_ctxs[])
234
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300235{
236 /* Get the file handle object and layer object */
Prabhakar Lade71a1802014-10-12 17:40:31 -0300237 struct vpbe_layer *layer = vb2_get_drv_priv(vq);
238 struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300239
240 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_buffer_setup\n");
241
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300242 /* Store number of buffers allocated in numbuffer member */
Prabhakar Lad7041bc92014-10-22 18:42:01 -0300243 if (vq->num_buffers + *nbuffers < VPBE_DEFAULT_NUM_BUFS)
244 *nbuffers = VPBE_DEFAULT_NUM_BUFS - vq->num_buffers;
Hans Verkuildf9ecb0c2015-10-28 00:50:37 -0200245
246 if (*nplanes)
247 return sizes[0] < layer->pix_fmt.sizeimage ? -EINVAL : 0;
Lad, Prabhakar13fc23d32012-10-22 09:27:13 -0300248
249 *nplanes = 1;
Hans Verkuildf9ecb0c2015-10-28 00:50:37 -0200250 sizes[0] = layer->pix_fmt.sizeimage;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300251
252 return 0;
253}
254
255/*
256 * vpbe_buffer_queue()
257 * This function adds the buffer to DMA queue
258 */
Lad, Prabhakar13fc23d32012-10-22 09:27:13 -0300259static void vpbe_buffer_queue(struct vb2_buffer *vb)
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300260{
Junghak Sung2d700712015-09-22 10:30:30 -0300261 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300262 /* Get the file handle object and layer object */
Junghak Sung2d700712015-09-22 10:30:30 -0300263 struct vpbe_disp_buffer *buf = container_of(vbuf,
Lad, Prabhakar13fc23d32012-10-22 09:27:13 -0300264 struct vpbe_disp_buffer, vb);
Prabhakar Lade71a1802014-10-12 17:40:31 -0300265 struct vpbe_layer *layer = vb2_get_drv_priv(vb->vb2_queue);
266 struct vpbe_display *disp = layer->disp_dev;
267 struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300268 unsigned long flags;
269
270 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
271 "vpbe_buffer_queue\n");
272
273 /* add the buffer to the DMA queue */
274 spin_lock_irqsave(&disp->dma_queue_lock, flags);
Lad, Prabhakar13fc23d32012-10-22 09:27:13 -0300275 list_add_tail(&buf->list, &layer->dma_queue);
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300276 spin_unlock_irqrestore(&disp->dma_queue_lock, flags);
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300277}
278
Lad, Prabhakar13fc23d32012-10-22 09:27:13 -0300279static int vpbe_start_streaming(struct vb2_queue *vq, unsigned int count)
280{
Prabhakar Lade71a1802014-10-12 17:40:31 -0300281 struct vpbe_layer *layer = vb2_get_drv_priv(vq);
Prabhakar Lad41cf47b2014-10-12 17:40:38 -0300282 struct osd_state *osd_device = layer->disp_dev->osd_device;
Lad, Prabhakar13fc23d32012-10-22 09:27:13 -0300283 int ret;
284
Prabhakar Lad41cf47b2014-10-12 17:40:38 -0300285 osd_device->ops.disable_layer(osd_device, layer->layer_info.id);
286
Lad, Prabhakar13fc23d32012-10-22 09:27:13 -0300287 /* Get the next frame from the buffer queue */
288 layer->next_frm = layer->cur_frm = list_entry(layer->dma_queue.next,
289 struct vpbe_disp_buffer, list);
290 /* Remove buffer from the buffer queue */
291 list_del(&layer->cur_frm->list);
292 /* Mark state of the current frame to active */
Junghak Sung2d700712015-09-22 10:30:30 -0300293 layer->cur_frm->vb.vb2_buf.state = VB2_BUF_STATE_ACTIVE;
Lad, Prabhakar13fc23d32012-10-22 09:27:13 -0300294 /* Initialize field_id and started member */
295 layer->field_id = 0;
296
297 /* Set parameters in OSD and VENC */
Prabhakar Lade71a1802014-10-12 17:40:31 -0300298 ret = vpbe_set_osd_display_params(layer->disp_dev, layer);
Lad, Prabhakarb1c090d2014-04-14 11:52:31 -0300299 if (ret < 0) {
300 struct vpbe_disp_buffer *buf, *tmp;
301
Junghak Sung2d700712015-09-22 10:30:30 -0300302 vb2_buffer_done(&layer->cur_frm->vb.vb2_buf,
303 VB2_BUF_STATE_QUEUED);
Lad, Prabhakarb1c090d2014-04-14 11:52:31 -0300304 list_for_each_entry_safe(buf, tmp, &layer->dma_queue, list) {
305 list_del(&buf->list);
Junghak Sung2d700712015-09-22 10:30:30 -0300306 vb2_buffer_done(&buf->vb.vb2_buf,
307 VB2_BUF_STATE_QUEUED);
Lad, Prabhakarb1c090d2014-04-14 11:52:31 -0300308 }
309
Lad, Prabhakar13fc23d32012-10-22 09:27:13 -0300310 return ret;
Lad, Prabhakarb1c090d2014-04-14 11:52:31 -0300311 }
Lad, Prabhakar13fc23d32012-10-22 09:27:13 -0300312
313 /*
314 * if request format is yuv420 semiplanar, need to
315 * enable both video windows
316 */
Lad, Prabhakar13fc23d32012-10-22 09:27:13 -0300317 layer->layer_first_int = 1;
318
319 return ret;
320}
321
Hans Verkuile37559b2014-04-17 02:47:21 -0300322static void vpbe_stop_streaming(struct vb2_queue *vq)
Lad, Prabhakar13fc23d32012-10-22 09:27:13 -0300323{
Prabhakar Lade71a1802014-10-12 17:40:31 -0300324 struct vpbe_layer *layer = vb2_get_drv_priv(vq);
Prabhakar Lad41cf47b2014-10-12 17:40:38 -0300325 struct osd_state *osd_device = layer->disp_dev->osd_device;
Prabhakar Lade71a1802014-10-12 17:40:31 -0300326 struct vpbe_display *disp = layer->disp_dev;
Lad, Prabhakarb699f092014-03-22 08:03:09 -0300327 unsigned long flags;
Lad, Prabhakar13fc23d32012-10-22 09:27:13 -0300328
329 if (!vb2_is_streaming(vq))
Hans Verkuile37559b2014-04-17 02:47:21 -0300330 return;
Lad, Prabhakar13fc23d32012-10-22 09:27:13 -0300331
Prabhakar Lad41cf47b2014-10-12 17:40:38 -0300332 osd_device->ops.disable_layer(osd_device, layer->layer_info.id);
333
Lad, Prabhakar13fc23d32012-10-22 09:27:13 -0300334 /* release all active buffers */
Lad, Prabhakarb699f092014-03-22 08:03:09 -0300335 spin_lock_irqsave(&disp->dma_queue_lock, flags);
336 if (layer->cur_frm == layer->next_frm) {
Junghak Sung2d700712015-09-22 10:30:30 -0300337 vb2_buffer_done(&layer->cur_frm->vb.vb2_buf,
338 VB2_BUF_STATE_ERROR);
Lad, Prabhakarb699f092014-03-22 08:03:09 -0300339 } else {
340 if (layer->cur_frm != NULL)
Junghak Sung2d700712015-09-22 10:30:30 -0300341 vb2_buffer_done(&layer->cur_frm->vb.vb2_buf,
Lad, Prabhakarb699f092014-03-22 08:03:09 -0300342 VB2_BUF_STATE_ERROR);
343 if (layer->next_frm != NULL)
Junghak Sung2d700712015-09-22 10:30:30 -0300344 vb2_buffer_done(&layer->next_frm->vb.vb2_buf,
Lad, Prabhakarb699f092014-03-22 08:03:09 -0300345 VB2_BUF_STATE_ERROR);
346 }
347
Lad, Prabhakar13fc23d32012-10-22 09:27:13 -0300348 while (!list_empty(&layer->dma_queue)) {
349 layer->next_frm = list_entry(layer->dma_queue.next,
350 struct vpbe_disp_buffer, list);
351 list_del(&layer->next_frm->list);
Junghak Sung2d700712015-09-22 10:30:30 -0300352 vb2_buffer_done(&layer->next_frm->vb.vb2_buf,
353 VB2_BUF_STATE_ERROR);
Lad, Prabhakar13fc23d32012-10-22 09:27:13 -0300354 }
Lad, Prabhakarb699f092014-03-22 08:03:09 -0300355 spin_unlock_irqrestore(&disp->dma_queue_lock, flags);
Lad, Prabhakar13fc23d32012-10-22 09:27:13 -0300356}
357
358static struct vb2_ops video_qops = {
359 .queue_setup = vpbe_buffer_queue_setup,
Prabhakar Lad488735b2014-10-12 17:40:33 -0300360 .wait_prepare = vb2_ops_wait_prepare,
361 .wait_finish = vb2_ops_wait_finish,
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300362 .buf_prepare = vpbe_buffer_prepare,
Lad, Prabhakar13fc23d32012-10-22 09:27:13 -0300363 .start_streaming = vpbe_start_streaming,
364 .stop_streaming = vpbe_stop_streaming,
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300365 .buf_queue = vpbe_buffer_queue,
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300366};
367
368static
369struct vpbe_layer*
370_vpbe_display_get_other_win_layer(struct vpbe_display *disp_dev,
371 struct vpbe_layer *layer)
372{
373 enum vpbe_display_device_id thiswin, otherwin;
374 thiswin = layer->device_id;
375
376 otherwin = (thiswin == VPBE_DISPLAY_DEVICE_0) ?
377 VPBE_DISPLAY_DEVICE_1 : VPBE_DISPLAY_DEVICE_0;
378 return disp_dev->dev[otherwin];
379}
380
381static int vpbe_set_osd_display_params(struct vpbe_display *disp_dev,
382 struct vpbe_layer *layer)
383{
384 struct osd_layer_config *cfg = &layer->layer_info.config;
385 struct osd_state *osd_device = disp_dev->osd_device;
386 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
387 unsigned long addr;
388 int ret;
389
Junghak Sung2d700712015-09-22 10:30:30 -0300390 addr = vb2_dma_contig_plane_dma_addr(&layer->cur_frm->vb.vb2_buf, 0);
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300391 /* Set address in the display registers */
392 osd_device->ops.start_layer(osd_device,
393 layer->layer_info.id,
394 addr,
395 disp_dev->cbcr_ofst);
396
397 ret = osd_device->ops.enable_layer(osd_device,
398 layer->layer_info.id, 0);
399 if (ret < 0) {
400 v4l2_err(&vpbe_dev->v4l2_dev,
401 "Error in enabling osd window layer 0\n");
402 return -1;
403 }
404
405 /* Enable the window */
406 layer->layer_info.enable = 1;
407 if (cfg->pixfmt == PIXFMT_NV12) {
408 struct vpbe_layer *otherlayer =
409 _vpbe_display_get_other_win_layer(disp_dev, layer);
410
411 ret = osd_device->ops.enable_layer(osd_device,
412 otherlayer->layer_info.id, 1);
413 if (ret < 0) {
414 v4l2_err(&vpbe_dev->v4l2_dev,
415 "Error in enabling osd window layer 1\n");
416 return -1;
417 }
418 otherlayer->layer_info.enable = 1;
419 }
420 return 0;
421}
422
423static void
424vpbe_disp_calculate_scale_factor(struct vpbe_display *disp_dev,
425 struct vpbe_layer *layer,
426 int expected_xsize, int expected_ysize)
427{
428 struct display_layer_info *layer_info = &layer->layer_info;
429 struct v4l2_pix_format *pixfmt = &layer->pix_fmt;
430 struct osd_layer_config *cfg = &layer->layer_info.config;
431 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
432 int calculated_xsize;
433 int h_exp = 0;
434 int v_exp = 0;
435 int h_scale;
436 int v_scale;
437
Hans Verkuil36864082012-10-01 11:39:46 -0300438 v4l2_std_id standard_id = vpbe_dev->current_timings.std_id;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300439
440 /*
441 * Application initially set the image format. Current display
442 * size is obtained from the vpbe display controller. expected_xsize
443 * and expected_ysize are set through S_CROP ioctl. Based on this,
444 * driver will calculate the scale factors for vertical and
445 * horizontal direction so that the image is displayed scaled
446 * and expanded. Application uses expansion to display the image
447 * in a square pixel. Otherwise it is displayed using displays
448 * pixel aspect ratio.It is expected that application chooses
449 * the crop coordinates for cropped or scaled display. if crop
450 * size is less than the image size, it is displayed cropped or
451 * it is displayed scaled and/or expanded.
452 *
453 * to begin with, set the crop window same as expected. Later we
454 * will override with scaled window size
455 */
456
457 cfg->xsize = pixfmt->width;
458 cfg->ysize = pixfmt->height;
459 layer_info->h_zoom = ZOOM_X1; /* no horizontal zoom */
460 layer_info->v_zoom = ZOOM_X1; /* no horizontal zoom */
461 layer_info->h_exp = H_EXP_OFF; /* no horizontal zoom */
462 layer_info->v_exp = V_EXP_OFF; /* no horizontal zoom */
463
464 if (pixfmt->width < expected_xsize) {
465 h_scale = vpbe_dev->current_timings.xres / pixfmt->width;
466 if (h_scale < 2)
467 h_scale = 1;
468 else if (h_scale >= 4)
469 h_scale = 4;
470 else
471 h_scale = 2;
472 cfg->xsize *= h_scale;
473 if (cfg->xsize < expected_xsize) {
474 if ((standard_id & V4L2_STD_525_60) ||
475 (standard_id & V4L2_STD_625_50)) {
476 calculated_xsize = (cfg->xsize *
477 VPBE_DISPLAY_H_EXP_RATIO_N) /
478 VPBE_DISPLAY_H_EXP_RATIO_D;
479 if (calculated_xsize <= expected_xsize) {
480 h_exp = 1;
481 cfg->xsize = calculated_xsize;
482 }
483 }
484 }
485 if (h_scale == 2)
486 layer_info->h_zoom = ZOOM_X2;
487 else if (h_scale == 4)
488 layer_info->h_zoom = ZOOM_X4;
489 if (h_exp)
490 layer_info->h_exp = H_EXP_9_OVER_8;
491 } else {
492 /* no scaling, only cropping. Set display area to crop area */
493 cfg->xsize = expected_xsize;
494 }
495
496 if (pixfmt->height < expected_ysize) {
497 v_scale = expected_ysize / pixfmt->height;
498 if (v_scale < 2)
499 v_scale = 1;
500 else if (v_scale >= 4)
501 v_scale = 4;
502 else
503 v_scale = 2;
504 cfg->ysize *= v_scale;
505 if (cfg->ysize < expected_ysize) {
506 if ((standard_id & V4L2_STD_625_50)) {
507 calculated_xsize = (cfg->ysize *
508 VPBE_DISPLAY_V_EXP_RATIO_N) /
509 VPBE_DISPLAY_V_EXP_RATIO_D;
510 if (calculated_xsize <= expected_ysize) {
511 v_exp = 1;
512 cfg->ysize = calculated_xsize;
513 }
514 }
515 }
516 if (v_scale == 2)
517 layer_info->v_zoom = ZOOM_X2;
518 else if (v_scale == 4)
519 layer_info->v_zoom = ZOOM_X4;
520 if (v_exp)
521 layer_info->h_exp = V_EXP_6_OVER_5;
522 } else {
523 /* no scaling, only cropping. Set display area to crop area */
524 cfg->ysize = expected_ysize;
525 }
526 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
527 "crop display xsize = %d, ysize = %d\n",
528 cfg->xsize, cfg->ysize);
529}
530
531static void vpbe_disp_adj_position(struct vpbe_display *disp_dev,
532 struct vpbe_layer *layer,
533 int top, int left)
534{
535 struct osd_layer_config *cfg = &layer->layer_info.config;
536 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
537
538 cfg->xpos = min((unsigned int)left,
539 vpbe_dev->current_timings.xres - cfg->xsize);
540 cfg->ypos = min((unsigned int)top,
541 vpbe_dev->current_timings.yres - cfg->ysize);
542
543 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
544 "new xpos = %d, ypos = %d\n",
545 cfg->xpos, cfg->ypos);
546}
547
548static void vpbe_disp_check_window_params(struct vpbe_display *disp_dev,
549 struct v4l2_rect *c)
550{
551 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
552
553 if ((c->width == 0) ||
554 ((c->width + c->left) > vpbe_dev->current_timings.xres))
555 c->width = vpbe_dev->current_timings.xres - c->left;
556
557 if ((c->height == 0) || ((c->height + c->top) >
558 vpbe_dev->current_timings.yres))
559 c->height = vpbe_dev->current_timings.yres - c->top;
560
561 /* window height must be even for interlaced display */
562 if (vpbe_dev->current_timings.interlaced)
563 c->height &= (~0x01);
564
565}
566
567/**
568 * vpbe_try_format()
569 * If user application provides width and height, and have bytesperline set
570 * to zero, driver calculates bytesperline and sizeimage based on hardware
571 * limits.
572 */
573static int vpbe_try_format(struct vpbe_display *disp_dev,
574 struct v4l2_pix_format *pixfmt, int check)
575{
576 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
577 int min_height = 1;
578 int min_width = 32;
579 int max_height;
580 int max_width;
581 int bpp;
582
583 if ((pixfmt->pixelformat != V4L2_PIX_FMT_UYVY) &&
584 (pixfmt->pixelformat != V4L2_PIX_FMT_NV12))
585 /* choose default as V4L2_PIX_FMT_UYVY */
586 pixfmt->pixelformat = V4L2_PIX_FMT_UYVY;
587
588 /* Check the field format */
589 if ((pixfmt->field != V4L2_FIELD_INTERLACED) &&
590 (pixfmt->field != V4L2_FIELD_NONE)) {
591 if (vpbe_dev->current_timings.interlaced)
592 pixfmt->field = V4L2_FIELD_INTERLACED;
593 else
594 pixfmt->field = V4L2_FIELD_NONE;
595 }
596
597 if (pixfmt->field == V4L2_FIELD_INTERLACED)
598 min_height = 2;
599
600 if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12)
601 bpp = 1;
602 else
603 bpp = 2;
604
605 max_width = vpbe_dev->current_timings.xres;
606 max_height = vpbe_dev->current_timings.yres;
607
608 min_width /= bpp;
609
610 if (!pixfmt->width || (pixfmt->width < min_width) ||
611 (pixfmt->width > max_width)) {
612 pixfmt->width = vpbe_dev->current_timings.xres;
613 }
614
615 if (!pixfmt->height || (pixfmt->height < min_height) ||
616 (pixfmt->height > max_height)) {
617 pixfmt->height = vpbe_dev->current_timings.yres;
618 }
619
620 if (pixfmt->bytesperline < (pixfmt->width * bpp))
621 pixfmt->bytesperline = pixfmt->width * bpp;
622
623 /* Make the bytesperline 32 byte aligned */
624 pixfmt->bytesperline = ((pixfmt->width * bpp + 31) & ~31);
625
626 if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12)
627 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height +
628 (pixfmt->bytesperline * pixfmt->height >> 1);
629 else
630 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height;
631
632 return 0;
633}
634
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300635static int vpbe_display_querycap(struct file *file, void *priv,
636 struct v4l2_capability *cap)
637{
Prabhakar Lad4bb12312014-10-12 17:40:37 -0300638 struct vpbe_layer *layer = video_drvdata(file);
639 struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300640
Lad, Prabhakard0466282012-10-22 09:27:14 -0300641 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
642 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
643 snprintf(cap->driver, sizeof(cap->driver), "%s",
644 dev_name(vpbe_dev->pdev));
645 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
646 dev_name(vpbe_dev->pdev));
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300647 strlcpy(cap->card, vpbe_dev->cfg->module_name, sizeof(cap->card));
648
649 return 0;
650}
651
652static int vpbe_display_s_crop(struct file *file, void *priv,
Hans Verkuil4f996592012-09-05 05:10:48 -0300653 const struct v4l2_crop *crop)
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300654{
Prabhakar Lad4bb12312014-10-12 17:40:37 -0300655 struct vpbe_layer *layer = video_drvdata(file);
656 struct vpbe_display *disp_dev = layer->disp_dev;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300657 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
658 struct osd_layer_config *cfg = &layer->layer_info.config;
659 struct osd_state *osd_device = disp_dev->osd_device;
Lad, Prabhakarfabc4e92012-10-03 02:13:28 -0300660 struct v4l2_rect rect = crop->c;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300661 int ret;
662
663 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
664 "VIDIOC_S_CROP, layer id = %d\n", layer->device_id);
665
666 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
667 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buf type\n");
668 return -EINVAL;
669 }
670
Lad, Prabhakarfabc4e92012-10-03 02:13:28 -0300671 if (rect.top < 0)
672 rect.top = 0;
673 if (rect.left < 0)
674 rect.left = 0;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300675
Lad, Prabhakarfabc4e92012-10-03 02:13:28 -0300676 vpbe_disp_check_window_params(disp_dev, &rect);
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300677
678 osd_device->ops.get_layer_config(osd_device,
679 layer->layer_info.id, cfg);
680
681 vpbe_disp_calculate_scale_factor(disp_dev, layer,
Lad, Prabhakarfabc4e92012-10-03 02:13:28 -0300682 rect.width,
683 rect.height);
684 vpbe_disp_adj_position(disp_dev, layer, rect.top,
685 rect.left);
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300686 ret = osd_device->ops.set_layer_config(osd_device,
687 layer->layer_info.id, cfg);
688 if (ret < 0) {
689 v4l2_err(&vpbe_dev->v4l2_dev,
690 "Error in set layer config:\n");
691 return -EINVAL;
692 }
693
694 /* apply zooming and h or v expansion */
695 osd_device->ops.set_zoom(osd_device,
696 layer->layer_info.id,
697 layer->layer_info.h_zoom,
698 layer->layer_info.v_zoom);
699 ret = osd_device->ops.set_vid_expansion(osd_device,
700 layer->layer_info.h_exp,
701 layer->layer_info.v_exp);
702 if (ret < 0) {
703 v4l2_err(&vpbe_dev->v4l2_dev,
704 "Error in set vid expansion:\n");
705 return -EINVAL;
706 }
707
708 if ((layer->layer_info.h_zoom != ZOOM_X1) ||
709 (layer->layer_info.v_zoom != ZOOM_X1) ||
710 (layer->layer_info.h_exp != H_EXP_OFF) ||
711 (layer->layer_info.v_exp != V_EXP_OFF))
712 /* Enable expansion filter */
713 osd_device->ops.set_interpolation_filter(osd_device, 1);
714 else
715 osd_device->ops.set_interpolation_filter(osd_device, 0);
716
717 return 0;
718}
719
720static int vpbe_display_g_crop(struct file *file, void *priv,
721 struct v4l2_crop *crop)
722{
Prabhakar Lad4bb12312014-10-12 17:40:37 -0300723 struct vpbe_layer *layer = video_drvdata(file);
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300724 struct osd_layer_config *cfg = &layer->layer_info.config;
Prabhakar Lad4bb12312014-10-12 17:40:37 -0300725 struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
726 struct osd_state *osd_device = layer->disp_dev->osd_device;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300727 struct v4l2_rect *rect = &crop->c;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300728
729 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
730 "VIDIOC_G_CROP, layer id = %d\n",
731 layer->device_id);
732
733 if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
734 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid buf type\n");
Wei Yongjune276f032012-12-02 22:50:47 -0300735 return -EINVAL;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300736 }
737 osd_device->ops.get_layer_config(osd_device,
738 layer->layer_info.id, cfg);
739 rect->top = cfg->ypos;
740 rect->left = cfg->xpos;
741 rect->width = cfg->xsize;
742 rect->height = cfg->ysize;
743
744 return 0;
745}
746
747static int vpbe_display_cropcap(struct file *file, void *priv,
748 struct v4l2_cropcap *cropcap)
749{
Prabhakar Lad4bb12312014-10-12 17:40:37 -0300750 struct vpbe_layer *layer = video_drvdata(file);
751 struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300752
753 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_CROPCAP ioctl\n");
754
755 cropcap->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
756 cropcap->bounds.left = 0;
757 cropcap->bounds.top = 0;
758 cropcap->bounds.width = vpbe_dev->current_timings.xres;
759 cropcap->bounds.height = vpbe_dev->current_timings.yres;
760 cropcap->pixelaspect = vpbe_dev->current_timings.aspect;
761 cropcap->defrect = cropcap->bounds;
762 return 0;
763}
764
765static int vpbe_display_g_fmt(struct file *file, void *priv,
766 struct v4l2_format *fmt)
767{
Prabhakar Lad4bb12312014-10-12 17:40:37 -0300768 struct vpbe_layer *layer = video_drvdata(file);
769 struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300770
771 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
772 "VIDIOC_G_FMT, layer id = %d\n",
773 layer->device_id);
774
775 /* If buffer type is video output */
776 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != fmt->type) {
777 v4l2_err(&vpbe_dev->v4l2_dev, "invalid type\n");
778 return -EINVAL;
779 }
780 /* Fill in the information about format */
781 fmt->fmt.pix = layer->pix_fmt;
782
783 return 0;
784}
785
786static int vpbe_display_enum_fmt(struct file *file, void *priv,
787 struct v4l2_fmtdesc *fmt)
788{
Prabhakar Lad4bb12312014-10-12 17:40:37 -0300789 struct vpbe_layer *layer = video_drvdata(file);
790 struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300791 unsigned int index = 0;
792
793 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
794 "VIDIOC_ENUM_FMT, layer id = %d\n",
795 layer->device_id);
796 if (fmt->index > 1) {
797 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid format index\n");
798 return -EINVAL;
799 }
800
801 /* Fill in the information about format */
802 index = fmt->index;
803 memset(fmt, 0, sizeof(*fmt));
804 fmt->index = index;
805 fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
806 if (index == 0) {
807 strcpy(fmt->description, "YUV 4:2:2 - UYVY");
808 fmt->pixelformat = V4L2_PIX_FMT_UYVY;
809 } else {
810 strcpy(fmt->description, "Y/CbCr 4:2:0");
811 fmt->pixelformat = V4L2_PIX_FMT_NV12;
812 }
813
814 return 0;
815}
816
817static int vpbe_display_s_fmt(struct file *file, void *priv,
818 struct v4l2_format *fmt)
819{
Prabhakar Lad4bb12312014-10-12 17:40:37 -0300820 struct vpbe_layer *layer = video_drvdata(file);
821 struct vpbe_display *disp_dev = layer->disp_dev;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300822 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
823 struct osd_layer_config *cfg = &layer->layer_info.config;
824 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
825 struct osd_state *osd_device = disp_dev->osd_device;
826 int ret;
827
828 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
829 "VIDIOC_S_FMT, layer id = %d\n",
830 layer->device_id);
831
Prabhakar Lad1b73f032014-10-12 17:40:42 -0300832 if (vb2_is_busy(&layer->buffer_queue))
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300833 return -EBUSY;
Prabhakar Lad1b73f032014-10-12 17:40:42 -0300834
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300835 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != fmt->type) {
836 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "invalid type\n");
837 return -EINVAL;
838 }
839 /* Check for valid pixel format */
840 ret = vpbe_try_format(disp_dev, pixfmt, 1);
841 if (ret)
842 return ret;
843
844 /* YUV420 is requested, check availability of the
845 other video window */
846
847 layer->pix_fmt = *pixfmt;
Lad, Prabhakarbdea0d22013-05-07 01:07:25 -0300848 if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12) {
849 struct vpbe_layer *otherlayer;
850
851 otherlayer = _vpbe_display_get_other_win_layer(disp_dev, layer);
852 /* if other layer is available, only
853 * claim it, do not configure it
854 */
855 ret = osd_device->ops.request_layer(osd_device,
856 otherlayer->layer_info.id);
857 if (ret < 0) {
858 v4l2_err(&vpbe_dev->v4l2_dev,
859 "Display Manager failed to allocate layer\n");
860 return -EBUSY;
861 }
862 }
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300863
864 /* Get osd layer config */
865 osd_device->ops.get_layer_config(osd_device,
866 layer->layer_info.id, cfg);
867 /* Store the pixel format in the layer object */
868 cfg->xsize = pixfmt->width;
869 cfg->ysize = pixfmt->height;
870 cfg->line_length = pixfmt->bytesperline;
871 cfg->ypos = 0;
872 cfg->xpos = 0;
873 cfg->interlaced = vpbe_dev->current_timings.interlaced;
874
875 if (V4L2_PIX_FMT_UYVY == pixfmt->pixelformat)
Lad, Prabhakar849325e2013-05-03 08:39:25 -0300876 cfg->pixfmt = PIXFMT_YCBCRI;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300877
878 /* Change of the default pixel format for both video windows */
879 if (V4L2_PIX_FMT_NV12 == pixfmt->pixelformat) {
880 struct vpbe_layer *otherlayer;
881 cfg->pixfmt = PIXFMT_NV12;
882 otherlayer = _vpbe_display_get_other_win_layer(disp_dev,
883 layer);
884 otherlayer->layer_info.config.pixfmt = PIXFMT_NV12;
885 }
886
887 /* Set the layer config in the osd window */
888 ret = osd_device->ops.set_layer_config(osd_device,
889 layer->layer_info.id, cfg);
890 if (ret < 0) {
891 v4l2_err(&vpbe_dev->v4l2_dev,
892 "Error in S_FMT params:\n");
893 return -EINVAL;
894 }
895
896 /* Readback and fill the local copy of current pix format */
897 osd_device->ops.get_layer_config(osd_device,
898 layer->layer_info.id, cfg);
899
900 return 0;
901}
902
903static int vpbe_display_try_fmt(struct file *file, void *priv,
904 struct v4l2_format *fmt)
905{
Prabhakar Lad4bb12312014-10-12 17:40:37 -0300906 struct vpbe_layer *layer = video_drvdata(file);
907 struct vpbe_display *disp_dev = layer->disp_dev;
908 struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300909 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
910
911 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_TRY_FMT\n");
912
913 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != fmt->type) {
914 v4l2_err(&vpbe_dev->v4l2_dev, "invalid type\n");
915 return -EINVAL;
916 }
917
918 /* Check for valid field format */
919 return vpbe_try_format(disp_dev, pixfmt, 0);
920
921}
922
923/**
924 * vpbe_display_s_std - Set the given standard in the encoder
925 *
926 * Sets the standard if supported by the current encoder. Return the status.
927 * 0 - success & -EINVAL on error
928 */
929static int vpbe_display_s_std(struct file *file, void *priv,
Hans Verkuil314527a2013-03-15 06:10:40 -0300930 v4l2_std_id std_id)
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300931{
Prabhakar Lad4bb12312014-10-12 17:40:37 -0300932 struct vpbe_layer *layer = video_drvdata(file);
933 struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300934 int ret;
935
936 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_S_STD\n");
937
Prabhakar Lad1b73f032014-10-12 17:40:42 -0300938 if (vb2_is_busy(&layer->buffer_queue))
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300939 return -EBUSY;
Prabhakar Lad1b73f032014-10-12 17:40:42 -0300940
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300941 if (NULL != vpbe_dev->ops.s_std) {
942 ret = vpbe_dev->ops.s_std(vpbe_dev, std_id);
943 if (ret) {
944 v4l2_err(&vpbe_dev->v4l2_dev,
945 "Failed to set standard for sub devices\n");
946 return -EINVAL;
947 }
948 } else {
949 return -EINVAL;
950 }
951
952 return 0;
953}
954
955/**
956 * vpbe_display_g_std - Get the standard in the current encoder
957 *
958 * Get the standard in the current encoder. Return the status. 0 - success
959 * -EINVAL on error
960 */
961static int vpbe_display_g_std(struct file *file, void *priv,
962 v4l2_std_id *std_id)
963{
Prabhakar Lad4bb12312014-10-12 17:40:37 -0300964 struct vpbe_layer *layer = video_drvdata(file);
965 struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300966
967 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_G_STD\n");
968
969 /* Get the standard from the current encoder */
970 if (vpbe_dev->current_timings.timings_type & VPBE_ENC_STD) {
Hans Verkuil36864082012-10-01 11:39:46 -0300971 *std_id = vpbe_dev->current_timings.std_id;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300972 return 0;
973 }
974
975 return -EINVAL;
976}
977
978/**
979 * vpbe_display_enum_output - enumerate outputs
980 *
981 * Enumerates the outputs available at the vpbe display
982 * returns the status, -EINVAL if end of output list
983 */
984static int vpbe_display_enum_output(struct file *file, void *priv,
985 struct v4l2_output *output)
986{
Prabhakar Lad4bb12312014-10-12 17:40:37 -0300987 struct vpbe_layer *layer = video_drvdata(file);
988 struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300989 int ret;
990
991 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_ENUM_OUTPUT\n");
992
993 /* Enumerate outputs */
994
995 if (NULL == vpbe_dev->ops.enum_outputs)
996 return -EINVAL;
997
998 ret = vpbe_dev->ops.enum_outputs(vpbe_dev, output);
999 if (ret) {
1000 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1001 "Failed to enumerate outputs\n");
1002 return -EINVAL;
1003 }
1004
1005 return 0;
1006}
1007
1008/**
1009 * vpbe_display_s_output - Set output to
1010 * the output specified by the index
1011 */
1012static int vpbe_display_s_output(struct file *file, void *priv,
1013 unsigned int i)
1014{
Prabhakar Lad4bb12312014-10-12 17:40:37 -03001015 struct vpbe_layer *layer = video_drvdata(file);
1016 struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001017 int ret;
1018
1019 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_S_OUTPUT\n");
Prabhakar Lad1b73f032014-10-12 17:40:42 -03001020
1021 if (vb2_is_busy(&layer->buffer_queue))
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001022 return -EBUSY;
Prabhakar Lad1b73f032014-10-12 17:40:42 -03001023
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001024 if (NULL == vpbe_dev->ops.set_output)
1025 return -EINVAL;
1026
1027 ret = vpbe_dev->ops.set_output(vpbe_dev, i);
1028 if (ret) {
1029 v4l2_err(&vpbe_dev->v4l2_dev,
1030 "Failed to set output for sub devices\n");
1031 return -EINVAL;
1032 }
1033
1034 return 0;
1035}
1036
1037/**
1038 * vpbe_display_g_output - Get output from subdevice
1039 * for a given by the index
1040 */
1041static int vpbe_display_g_output(struct file *file, void *priv,
1042 unsigned int *i)
1043{
Prabhakar Lad4bb12312014-10-12 17:40:37 -03001044 struct vpbe_layer *layer = video_drvdata(file);
1045 struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001046
1047 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_G_OUTPUT\n");
1048 /* Get the standard from the current encoder */
1049 *i = vpbe_dev->current_out_index;
1050
1051 return 0;
1052}
1053
1054/**
Hans Verkuil36864082012-10-01 11:39:46 -03001055 * vpbe_display_enum_dv_timings - Enumerate the dv timings
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001056 *
Hans Verkuil36864082012-10-01 11:39:46 -03001057 * enum the timings in the current encoder. Return the status. 0 - success
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001058 * -EINVAL on error
1059 */
1060static int
Hans Verkuil36864082012-10-01 11:39:46 -03001061vpbe_display_enum_dv_timings(struct file *file, void *priv,
1062 struct v4l2_enum_dv_timings *timings)
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001063{
Prabhakar Lad4bb12312014-10-12 17:40:37 -03001064 struct vpbe_layer *layer = video_drvdata(file);
1065 struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001066 int ret;
1067
Hans Verkuil36864082012-10-01 11:39:46 -03001068 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_ENUM_DV_TIMINGS\n");
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001069
1070 /* Enumerate outputs */
Hans Verkuil36864082012-10-01 11:39:46 -03001071 if (NULL == vpbe_dev->ops.enum_dv_timings)
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001072 return -EINVAL;
1073
Hans Verkuil36864082012-10-01 11:39:46 -03001074 ret = vpbe_dev->ops.enum_dv_timings(vpbe_dev, timings);
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001075 if (ret) {
1076 v4l2_err(&vpbe_dev->v4l2_dev,
Hans Verkuil36864082012-10-01 11:39:46 -03001077 "Failed to enumerate dv timings info\n");
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001078 return -EINVAL;
1079 }
1080
1081 return 0;
1082}
1083
1084/**
Hans Verkuil36864082012-10-01 11:39:46 -03001085 * vpbe_display_s_dv_timings - Set the dv timings
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001086 *
Hans Verkuil36864082012-10-01 11:39:46 -03001087 * Set the timings in the current encoder. Return the status. 0 - success
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001088 * -EINVAL on error
1089 */
1090static int
Hans Verkuil36864082012-10-01 11:39:46 -03001091vpbe_display_s_dv_timings(struct file *file, void *priv,
1092 struct v4l2_dv_timings *timings)
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001093{
Prabhakar Lad4bb12312014-10-12 17:40:37 -03001094 struct vpbe_layer *layer = video_drvdata(file);
1095 struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001096 int ret;
1097
Hans Verkuil36864082012-10-01 11:39:46 -03001098 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_S_DV_TIMINGS\n");
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001099
Prabhakar Lad1b73f032014-10-12 17:40:42 -03001100 if (vb2_is_busy(&layer->buffer_queue))
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001101 return -EBUSY;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001102
1103 /* Set the given standard in the encoder */
Hans Verkuil36864082012-10-01 11:39:46 -03001104 if (!vpbe_dev->ops.s_dv_timings)
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001105 return -EINVAL;
1106
Hans Verkuil36864082012-10-01 11:39:46 -03001107 ret = vpbe_dev->ops.s_dv_timings(vpbe_dev, timings);
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001108 if (ret) {
1109 v4l2_err(&vpbe_dev->v4l2_dev,
Hans Verkuil36864082012-10-01 11:39:46 -03001110 "Failed to set the dv timings info\n");
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001111 return -EINVAL;
1112 }
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001113
1114 return 0;
1115}
1116
1117/**
Hans Verkuil36864082012-10-01 11:39:46 -03001118 * vpbe_display_g_dv_timings - Set the dv timings
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001119 *
Hans Verkuil36864082012-10-01 11:39:46 -03001120 * Get the timings in the current encoder. Return the status. 0 - success
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001121 * -EINVAL on error
1122 */
1123static int
Hans Verkuil36864082012-10-01 11:39:46 -03001124vpbe_display_g_dv_timings(struct file *file, void *priv,
1125 struct v4l2_dv_timings *dv_timings)
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001126{
Prabhakar Lad4bb12312014-10-12 17:40:37 -03001127 struct vpbe_layer *layer = video_drvdata(file);
1128 struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001129
Hans Verkuil36864082012-10-01 11:39:46 -03001130 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_G_DV_TIMINGS\n");
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001131
1132 /* Get the given standard in the encoder */
1133
1134 if (vpbe_dev->current_timings.timings_type &
Hans Verkuilef2d41b2013-02-15 15:06:28 -03001135 VPBE_ENC_DV_TIMINGS) {
Hans Verkuil36864082012-10-01 11:39:46 -03001136 *dv_timings = vpbe_dev->current_timings.dv_timings;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001137 } else {
1138 return -EINVAL;
1139 }
1140
1141 return 0;
1142}
1143
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001144/*
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001145 * vpbe_display_open()
1146 * It creates object of file handle structure and stores it in private_data
1147 * member of filepointer
1148 */
1149static int vpbe_display_open(struct file *file)
1150{
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001151 struct vpbe_layer *layer = video_drvdata(file);
1152 struct vpbe_display *disp_dev = layer->disp_dev;
1153 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
1154 struct osd_state *osd_device = disp_dev->osd_device;
1155 int err;
1156
Prabhakar Lad4bb12312014-10-12 17:40:37 -03001157 /* creating context for file descriptor */
1158 err = v4l2_fh_open(file);
1159 if (err) {
1160 v4l2_err(&vpbe_dev->v4l2_dev, "v4l2_fh_open failed\n");
1161 return err;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001162 }
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001163
Prabhakar Lad4bb12312014-10-12 17:40:37 -03001164 /* leaving if layer is already initialized */
1165 if (!v4l2_fh_is_singular_file(file))
1166 return err;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001167
1168 if (!layer->usrs) {
Hans Verkuilcbc807d2012-06-24 06:16:44 -03001169 if (mutex_lock_interruptible(&layer->opslock))
1170 return -ERESTARTSYS;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001171 /* First claim the layer for this device */
1172 err = osd_device->ops.request_layer(osd_device,
1173 layer->layer_info.id);
Hans Verkuilcbc807d2012-06-24 06:16:44 -03001174 mutex_unlock(&layer->opslock);
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001175 if (err < 0) {
1176 /* Couldn't get layer */
1177 v4l2_err(&vpbe_dev->v4l2_dev,
1178 "Display Manager failed to allocate layer\n");
Prabhakar Lad4bb12312014-10-12 17:40:37 -03001179 v4l2_fh_release(file);
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001180 return -EINVAL;
1181 }
1182 }
1183 /* Increment layer usrs counter */
1184 layer->usrs++;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001185 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1186 "vpbe display device opened successfully\n");
1187 return 0;
1188}
1189
1190/*
1191 * vpbe_display_release()
1192 * This function deletes buffer queue, frees the buffers and the davinci
1193 * display file * handle
1194 */
1195static int vpbe_display_release(struct file *file)
1196{
Prabhakar Lad4bb12312014-10-12 17:40:37 -03001197 struct vpbe_layer *layer = video_drvdata(file);
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001198 struct osd_layer_config *cfg = &layer->layer_info.config;
Prabhakar Lad4bb12312014-10-12 17:40:37 -03001199 struct vpbe_display *disp_dev = layer->disp_dev;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001200 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
1201 struct osd_state *osd_device = disp_dev->osd_device;
1202
1203 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_release\n");
1204
Hans Verkuilcbc807d2012-06-24 06:16:44 -03001205 mutex_lock(&layer->opslock);
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001206
Prabhakar Lad4bb12312014-10-12 17:40:37 -03001207 osd_device->ops.disable_layer(osd_device,
1208 layer->layer_info.id);
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001209 /* Decrement layer usrs counter */
1210 layer->usrs--;
1211 /* If this file handle has initialize encoder device, reset it */
1212 if (!layer->usrs) {
1213 if (cfg->pixfmt == PIXFMT_NV12) {
1214 struct vpbe_layer *otherlayer;
1215 otherlayer =
1216 _vpbe_display_get_other_win_layer(disp_dev, layer);
1217 osd_device->ops.disable_layer(osd_device,
1218 otherlayer->layer_info.id);
1219 osd_device->ops.release_layer(osd_device,
1220 otherlayer->layer_info.id);
1221 }
1222 osd_device->ops.disable_layer(osd_device,
1223 layer->layer_info.id);
1224 osd_device->ops.release_layer(osd_device,
1225 layer->layer_info.id);
1226 }
Lad, Prabhakar3d7543b92014-03-22 07:57:59 -03001227
Prabhakar Lad4bb12312014-10-12 17:40:37 -03001228 _vb2_fop_release(file, NULL);
Hans Verkuilcbc807d2012-06-24 06:16:44 -03001229 mutex_unlock(&layer->opslock);
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001230
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001231 disp_dev->cbcr_ofst = 0;
1232
1233 return 0;
1234}
1235
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001236/* vpbe capture ioctl operations */
1237static const struct v4l2_ioctl_ops vpbe_ioctl_ops = {
1238 .vidioc_querycap = vpbe_display_querycap,
1239 .vidioc_g_fmt_vid_out = vpbe_display_g_fmt,
1240 .vidioc_enum_fmt_vid_out = vpbe_display_enum_fmt,
1241 .vidioc_s_fmt_vid_out = vpbe_display_s_fmt,
1242 .vidioc_try_fmt_vid_out = vpbe_display_try_fmt,
Prabhakar Lad41cf47b2014-10-12 17:40:38 -03001243
1244 .vidioc_reqbufs = vb2_ioctl_reqbufs,
Prabhakar Lad7041bc92014-10-22 18:42:01 -03001245 .vidioc_create_bufs = vb2_ioctl_create_bufs,
Prabhakar Lad41cf47b2014-10-12 17:40:38 -03001246 .vidioc_querybuf = vb2_ioctl_querybuf,
1247 .vidioc_qbuf = vb2_ioctl_qbuf,
1248 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1249 .vidioc_streamon = vb2_ioctl_streamon,
1250 .vidioc_streamoff = vb2_ioctl_streamoff,
Prabhakar Ladc24376f2014-10-12 17:40:41 -03001251 .vidioc_expbuf = vb2_ioctl_expbuf,
Prabhakar Lad41cf47b2014-10-12 17:40:38 -03001252
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001253 .vidioc_cropcap = vpbe_display_cropcap,
1254 .vidioc_g_crop = vpbe_display_g_crop,
1255 .vidioc_s_crop = vpbe_display_s_crop,
Prabhakar Lada8afe382014-10-12 17:40:44 -03001256
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001257 .vidioc_s_std = vpbe_display_s_std,
1258 .vidioc_g_std = vpbe_display_g_std,
Prabhakar Lada8afe382014-10-12 17:40:44 -03001259
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001260 .vidioc_enum_output = vpbe_display_enum_output,
1261 .vidioc_s_output = vpbe_display_s_output,
1262 .vidioc_g_output = vpbe_display_g_output,
Prabhakar Lada8afe382014-10-12 17:40:44 -03001263
Hans Verkuil36864082012-10-01 11:39:46 -03001264 .vidioc_s_dv_timings = vpbe_display_s_dv_timings,
1265 .vidioc_g_dv_timings = vpbe_display_g_dv_timings,
1266 .vidioc_enum_dv_timings = vpbe_display_enum_dv_timings,
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001267};
1268
1269static struct v4l2_file_operations vpbe_fops = {
1270 .owner = THIS_MODULE,
1271 .open = vpbe_display_open,
1272 .release = vpbe_display_release,
1273 .unlocked_ioctl = video_ioctl2,
Prabhakar Lad266c9c22014-10-12 17:40:36 -03001274 .mmap = vb2_fop_mmap,
1275 .poll = vb2_fop_poll,
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001276};
1277
1278static int vpbe_device_get(struct device *dev, void *data)
1279{
1280 struct platform_device *pdev = to_platform_device(dev);
1281 struct vpbe_display *vpbe_disp = data;
1282
1283 if (strcmp("vpbe_controller", pdev->name) == 0)
1284 vpbe_disp->vpbe_dev = platform_get_drvdata(pdev);
1285
Lad, Prabhakarcaff80c2012-11-20 07:30:36 -03001286 if (strstr(pdev->name, "vpbe-osd") != NULL)
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001287 vpbe_disp->osd_device = platform_get_drvdata(pdev);
1288
1289 return 0;
1290}
1291
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001292static int init_vpbe_layer(int i, struct vpbe_display *disp_dev,
1293 struct platform_device *pdev)
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001294{
1295 struct vpbe_layer *vpbe_display_layer = NULL;
1296 struct video_device *vbd = NULL;
1297
1298 /* Allocate memory for four plane display objects */
1299
1300 disp_dev->dev[i] =
1301 kzalloc(sizeof(struct vpbe_layer), GFP_KERNEL);
1302
1303 /* If memory allocation fails, return error */
1304 if (!disp_dev->dev[i]) {
1305 printk(KERN_ERR "ran out of memory\n");
1306 return -ENOMEM;
1307 }
1308 spin_lock_init(&disp_dev->dev[i]->irqlock);
1309 mutex_init(&disp_dev->dev[i]->opslock);
1310
1311 /* Get the pointer to the layer object */
1312 vpbe_display_layer = disp_dev->dev[i];
1313 vbd = &vpbe_display_layer->video_dev;
1314 /* Initialize field of video device */
1315 vbd->release = video_device_release_empty;
1316 vbd->fops = &vpbe_fops;
1317 vbd->ioctl_ops = &vpbe_ioctl_ops;
1318 vbd->minor = -1;
1319 vbd->v4l2_dev = &disp_dev->vpbe_dev->v4l2_dev;
1320 vbd->lock = &vpbe_display_layer->opslock;
Hans Verkuil954f3402012-09-05 06:05:50 -03001321 vbd->vfl_dir = VFL_DIR_TX;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001322
1323 if (disp_dev->vpbe_dev->current_timings.timings_type &
Hans Verkuil142b66e2013-02-19 13:33:34 -03001324 VPBE_ENC_STD)
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001325 vbd->tvnorms = (V4L2_STD_525_60 | V4L2_STD_625_50);
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001326
1327 snprintf(vbd->name, sizeof(vbd->name),
1328 "DaVinci_VPBE Display_DRIVER_V%d.%d.%d",
1329 (VPBE_DISPLAY_VERSION_CODE >> 16) & 0xff,
1330 (VPBE_DISPLAY_VERSION_CODE >> 8) & 0xff,
1331 (VPBE_DISPLAY_VERSION_CODE) & 0xff);
1332
1333 vpbe_display_layer->device_id = i;
1334
1335 vpbe_display_layer->layer_info.id =
1336 ((i == VPBE_DISPLAY_DEVICE_0) ? WIN_VID0 : WIN_VID1);
1337
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001338
1339 return 0;
1340}
1341
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001342static int register_device(struct vpbe_layer *vpbe_display_layer,
1343 struct vpbe_display *disp_dev,
1344 struct platform_device *pdev)
1345{
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001346 int err;
1347
1348 v4l2_info(&disp_dev->vpbe_dev->v4l2_dev,
1349 "Trying to register VPBE display device.\n");
1350 v4l2_info(&disp_dev->vpbe_dev->v4l2_dev,
1351 "layer=%x,layer->video_dev=%x\n",
1352 (int)vpbe_display_layer,
1353 (int)&vpbe_display_layer->video_dev);
1354
Prabhakar Lad266c9c22014-10-12 17:40:36 -03001355 vpbe_display_layer->video_dev.queue = &vpbe_display_layer->buffer_queue;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001356 err = video_register_device(&vpbe_display_layer->video_dev,
1357 VFL_TYPE_GRABBER,
1358 -1);
1359 if (err)
1360 return -ENODEV;
1361
1362 vpbe_display_layer->disp_dev = disp_dev;
1363 /* set the driver data in platform device */
1364 platform_set_drvdata(pdev, disp_dev);
1365 video_set_drvdata(&vpbe_display_layer->video_dev,
1366 vpbe_display_layer);
1367
1368 return 0;
1369}
1370
1371
1372
1373/*
1374 * vpbe_display_probe()
1375 * This function creates device entries by register itself to the V4L2 driver
1376 * and initializes fields of each layer objects
1377 */
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001378static int vpbe_display_probe(struct platform_device *pdev)
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001379{
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001380 struct vpbe_display *disp_dev;
Prabhakar Lade71a1802014-10-12 17:40:31 -03001381 struct v4l2_device *v4l2_dev;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001382 struct resource *res = NULL;
Prabhakar Lade71a1802014-10-12 17:40:31 -03001383 struct vb2_queue *q;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001384 int k;
1385 int i;
1386 int err;
1387 int irq;
1388
1389 printk(KERN_DEBUG "vpbe_display_probe\n");
1390 /* Allocate memory for vpbe_display */
Lad, Prabhakar6b55b452013-07-13 04:50:29 -03001391 disp_dev = devm_kzalloc(&pdev->dev, sizeof(struct vpbe_display),
1392 GFP_KERNEL);
1393 if (!disp_dev)
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001394 return -ENOMEM;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001395
1396 spin_lock_init(&disp_dev->dma_queue_lock);
1397 /*
1398 * Scan all the platform devices to find the vpbe
1399 * controller device and get the vpbe_dev object
1400 */
1401 err = bus_for_each_dev(&platform_bus_type, NULL, disp_dev,
1402 vpbe_device_get);
1403 if (err < 0)
1404 return err;
Prabhakar Lade71a1802014-10-12 17:40:31 -03001405
1406 v4l2_dev = &disp_dev->vpbe_dev->v4l2_dev;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001407 /* Initialize the vpbe display controller */
1408 if (NULL != disp_dev->vpbe_dev->ops.initialize) {
1409 err = disp_dev->vpbe_dev->ops.initialize(&pdev->dev,
1410 disp_dev->vpbe_dev);
1411 if (err) {
Prabhakar Lade71a1802014-10-12 17:40:31 -03001412 v4l2_err(v4l2_dev, "Error initing vpbe\n");
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001413 err = -ENOMEM;
1414 goto probe_out;
1415 }
1416 }
1417
1418 for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
1419 if (init_vpbe_layer(i, disp_dev, pdev)) {
1420 err = -ENODEV;
1421 goto probe_out;
1422 }
1423 }
1424
1425 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1426 if (!res) {
Prabhakar Lade71a1802014-10-12 17:40:31 -03001427 v4l2_err(v4l2_dev, "Unable to get VENC interrupt resource\n");
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001428 err = -ENODEV;
1429 goto probe_out;
1430 }
1431
1432 irq = res->start;
Michael Opdenackerd8c279a2013-09-08 23:30:11 -03001433 err = devm_request_irq(&pdev->dev, irq, venc_isr, 0,
Lad, Prabhakar6b55b452013-07-13 04:50:29 -03001434 VPBE_DISPLAY_DRIVER, disp_dev);
1435 if (err) {
Prabhakar Lade71a1802014-10-12 17:40:31 -03001436 v4l2_err(v4l2_dev, "VPBE IRQ request failed\n");
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001437 goto probe_out;
1438 }
1439
1440 for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
Prabhakar Lade71a1802014-10-12 17:40:31 -03001441 /* initialize vb2 queue */
1442 q = &disp_dev->dev[i]->buffer_queue;
1443 memset(q, 0, sizeof(*q));
1444 q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
Prabhakar Lad01118c92014-10-12 17:40:39 -03001445 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
Prabhakar Lade71a1802014-10-12 17:40:31 -03001446 q->drv_priv = disp_dev->dev[i];
1447 q->ops = &video_qops;
1448 q->mem_ops = &vb2_dma_contig_memops;
1449 q->buf_struct_size = sizeof(struct vpbe_disp_buffer);
1450 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1451 q->min_buffers_needed = 1;
Prabhakar Lad488735b2014-10-12 17:40:33 -03001452 q->lock = &disp_dev->dev[i]->opslock;
Hans Verkuil53ddcc62016-02-15 13:09:10 -02001453 q->dev = disp_dev->vpbe_dev->pdev;
Prabhakar Lade71a1802014-10-12 17:40:31 -03001454 err = vb2_queue_init(q);
1455 if (err) {
1456 v4l2_err(v4l2_dev, "vb2_queue_init() failed\n");
1457 goto probe_out;
1458 }
1459
Prabhakar Lade71a1802014-10-12 17:40:31 -03001460 INIT_LIST_HEAD(&disp_dev->dev[i]->dma_queue);
1461
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001462 if (register_device(disp_dev->dev[i], disp_dev, pdev)) {
1463 err = -ENODEV;
Lad, Prabhakar6b55b452013-07-13 04:50:29 -03001464 goto probe_out;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001465 }
1466 }
1467
Prabhakar Lade71a1802014-10-12 17:40:31 -03001468 v4l2_dbg(1, debug, v4l2_dev,
1469 "Successfully completed the probing of vpbe v4l2 device\n");
1470
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001471 return 0;
1472
Julia Lawall49a05132011-10-28 19:58:17 -03001473probe_out:
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001474 for (k = 0; k < VPBE_DISPLAY_MAX_DEVICES; k++) {
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001475 /* Unregister video device */
Prabhakar Lade71a1802014-10-12 17:40:31 -03001476 if (disp_dev->dev[k] != NULL) {
Prabhakar Lade71a1802014-10-12 17:40:31 -03001477 video_unregister_device(&disp_dev->dev[k]->video_dev);
1478 kfree(disp_dev->dev[k]);
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001479 }
1480 }
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001481 return err;
1482}
1483
1484/*
1485 * vpbe_display_remove()
1486 * It un-register hardware layer from V4L2 driver
1487 */
1488static int vpbe_display_remove(struct platform_device *pdev)
1489{
1490 struct vpbe_layer *vpbe_display_layer;
1491 struct vpbe_display *disp_dev = platform_get_drvdata(pdev);
1492 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001493 int i;
1494
1495 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_remove\n");
1496
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001497 /* deinitialize the vpbe display controller */
1498 if (NULL != vpbe_dev->ops.deinitialize)
1499 vpbe_dev->ops.deinitialize(&pdev->dev, vpbe_dev);
1500 /* un-register device */
1501 for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
1502 /* Get the pointer to the layer object */
1503 vpbe_display_layer = disp_dev->dev[i];
1504 /* Unregister video device */
1505 video_unregister_device(&vpbe_display_layer->video_dev);
1506
1507 }
1508 for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
1509 kfree(disp_dev->dev[i]);
1510 disp_dev->dev[i] = NULL;
1511 }
1512
1513 return 0;
1514}
1515
1516static struct platform_driver vpbe_display_driver = {
1517 .driver = {
1518 .name = VPBE_DISPLAY_DRIVER,
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001519 .bus = &platform_bus_type,
1520 },
1521 .probe = vpbe_display_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001522 .remove = vpbe_display_remove,
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001523};
1524
Axel Lin1d6629b2012-01-10 03:21:49 -03001525module_platform_driver(vpbe_display_driver);
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001526
1527MODULE_DESCRIPTION("TI DM644x/DM355/DM365 VPBE Display controller");
1528MODULE_LICENSE("GPL");
1529MODULE_AUTHOR("Texas Instruments");