blob: 9849e4405a6abe5eafffb48119f3049d879052b7 [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>
Mauro Carvalho Chehab05c90902018-04-05 12:31:39 -040029
30#ifdef CONFIG_ARCH_DAVINCI
Manjunath Hadlia2c25b42011-06-17 04:01:31 -030031#include <mach/cputype.h>
Mauro Carvalho Chehab05c90902018-04-05 12:31:39 -040032#endif
Manjunath Hadlia2c25b42011-06-17 04:01:31 -030033
34#include <media/v4l2-dev.h>
35#include <media/v4l2-common.h>
36#include <media/v4l2-ioctl.h>
37#include <media/v4l2-device.h>
38#include <media/davinci/vpbe_display.h>
39#include <media/davinci/vpbe_types.h>
40#include <media/davinci/vpbe.h>
41#include <media/davinci/vpbe_venc.h>
42#include <media/davinci/vpbe_osd.h>
43#include "vpbe_venc_regs.h"
44
45#define VPBE_DISPLAY_DRIVER "vpbe-v4l2"
46
47static int debug;
48
Manjunath Hadlia2c25b42011-06-17 04:01:31 -030049#define VPBE_DEFAULT_NUM_BUFS 3
50
51module_param(debug, int, 0644);
52
Lad, Prabhakar13fc23d32012-10-22 09:27:13 -030053static int vpbe_set_osd_display_params(struct vpbe_display *disp_dev,
54 struct vpbe_layer *layer);
55
Manjunath Hadlia2c25b42011-06-17 04:01:31 -030056static int venc_is_second_field(struct vpbe_display *disp_dev)
57{
58 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
59 int ret;
60 int val;
61
62 ret = v4l2_subdev_call(vpbe_dev->venc,
63 core,
64 ioctl,
65 VENC_GET_FLD,
66 &val);
67 if (ret < 0) {
68 v4l2_err(&vpbe_dev->v4l2_dev,
69 "Error in getting Field ID 0\n");
70 }
71 return val;
72}
73
74static void vpbe_isr_even_field(struct vpbe_display *disp_obj,
75 struct vpbe_layer *layer)
76{
Manjunath Hadlia2c25b42011-06-17 04:01:31 -030077 if (layer->cur_frm == layer->next_frm)
78 return;
Lad, Prabhakare9763992015-05-26 11:20:27 -030079
Junghak Sungd6dd6452015-11-03 08:16:37 -020080 layer->cur_frm->vb.vb2_buf.timestamp = ktime_get_ns();
Junghak Sung2d700712015-09-22 10:30:30 -030081 vb2_buffer_done(&layer->cur_frm->vb.vb2_buf, VB2_BUF_STATE_DONE);
Manjunath Hadlia2c25b42011-06-17 04:01:31 -030082 /* Make cur_frm pointing to next_frm */
83 layer->cur_frm = layer->next_frm;
84}
85
86static void vpbe_isr_odd_field(struct vpbe_display *disp_obj,
87 struct vpbe_layer *layer)
88{
89 struct osd_state *osd_device = disp_obj->osd_device;
90 unsigned long addr;
91
92 spin_lock(&disp_obj->dma_queue_lock);
93 if (list_empty(&layer->dma_queue) ||
94 (layer->cur_frm != layer->next_frm)) {
95 spin_unlock(&disp_obj->dma_queue_lock);
96 return;
97 }
98 /*
99 * one field is displayed configure
100 * the next frame if it is available
101 * otherwise hold on current frame
102 * Get next from the buffer queue
103 */
Lad, Prabhakar13fc23d32012-10-22 09:27:13 -0300104 layer->next_frm = list_entry(layer->dma_queue.next,
105 struct vpbe_disp_buffer, list);
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300106 /* Remove that from the buffer queue */
Lad, Prabhakar13fc23d32012-10-22 09:27:13 -0300107 list_del(&layer->next_frm->list);
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300108 spin_unlock(&disp_obj->dma_queue_lock);
109 /* Mark state of the frame to active */
Junghak Sung2d700712015-09-22 10:30:30 -0300110 layer->next_frm->vb.vb2_buf.state = VB2_BUF_STATE_ACTIVE;
111 addr = vb2_dma_contig_plane_dma_addr(&layer->next_frm->vb.vb2_buf, 0);
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300112 osd_device->ops.start_layer(osd_device,
113 layer->layer_info.id,
114 addr,
115 disp_obj->cbcr_ofst);
116}
117
118/* interrupt service routine */
119static irqreturn_t venc_isr(int irq, void *arg)
120{
121 struct vpbe_display *disp_dev = (struct vpbe_display *)arg;
122 struct vpbe_layer *layer;
123 static unsigned last_event;
124 unsigned event = 0;
125 int fid;
126 int i;
127
Markus Elfring7a6e6c32017-09-07 16:37:16 -0400128 if (!arg || !disp_dev->dev[0])
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300129 return IRQ_HANDLED;
130
131 if (venc_is_second_field(disp_dev))
132 event |= VENC_SECOND_FIELD;
133 else
134 event |= VENC_FIRST_FIELD;
135
136 if (event == (last_event & ~VENC_END_OF_FRAME)) {
137 /*
138 * If the display is non-interlaced, then we need to flag the
139 * end-of-frame event at every interrupt regardless of the
140 * value of the FIDST bit. We can conclude that the display is
141 * non-interlaced if the value of the FIDST bit is unchanged
142 * from the previous interrupt.
143 */
144 event |= VENC_END_OF_FRAME;
145 } else if (event == VENC_SECOND_FIELD) {
146 /* end-of-frame for interlaced display */
147 event |= VENC_END_OF_FRAME;
148 }
149 last_event = event;
150
151 for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
152 layer = disp_dev->dev[i];
Prabhakar Lad1b73f032014-10-12 17:40:42 -0300153
154 if (!vb2_start_streaming_called(&layer->buffer_queue))
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300155 continue;
156
157 if (layer->layer_first_int) {
158 layer->layer_first_int = 0;
159 continue;
160 }
161 /* Check the field format */
162 if ((V4L2_FIELD_NONE == layer->pix_fmt.field) &&
163 (event & VENC_END_OF_FRAME)) {
164 /* Progressive mode */
165
166 vpbe_isr_even_field(disp_dev, layer);
167 vpbe_isr_odd_field(disp_dev, layer);
168 } else {
169 /* Interlaced mode */
170
171 layer->field_id ^= 1;
172 if (event & VENC_FIRST_FIELD)
173 fid = 0;
174 else
175 fid = 1;
176
177 /*
178 * If field id does not match with store
179 * field id
180 */
181 if (fid != layer->field_id) {
182 /* Make them in sync */
183 layer->field_id = fid;
184 continue;
185 }
186 /*
187 * device field id and local field id are
188 * in sync. If this is even field
189 */
190 if (0 == fid)
191 vpbe_isr_even_field(disp_dev, layer);
192 else /* odd field */
193 vpbe_isr_odd_field(disp_dev, layer);
194 }
195 }
196
197 return IRQ_HANDLED;
198}
199
200/*
201 * vpbe_buffer_prepare()
Lad, Prabhakar13fc23d32012-10-22 09:27:13 -0300202 * This is the callback function called from vb2_qbuf() function
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300203 * the buffer is prepared and user space virtual address is converted into
204 * physical address
205 */
Lad, Prabhakar13fc23d32012-10-22 09:27:13 -0300206static int vpbe_buffer_prepare(struct vb2_buffer *vb)
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300207{
Lad, Prabhakar13fc23d32012-10-22 09:27:13 -0300208 struct vb2_queue *q = vb->vb2_queue;
Prabhakar Lade71a1802014-10-12 17:40:31 -0300209 struct vpbe_layer *layer = vb2_get_drv_priv(q);
210 struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300211 unsigned long addr;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300212
213 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
214 "vpbe_buffer_prepare\n");
215
Prabhakar Lad50d94812014-10-12 17:40:35 -0300216 vb2_set_plane_payload(vb, 0, layer->pix_fmt.sizeimage);
217 if (vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
218 return -EINVAL;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300219
Prabhakar Lad50d94812014-10-12 17:40:35 -0300220 addr = vb2_dma_contig_plane_dma_addr(vb, 0);
221 if (!IS_ALIGNED(addr, 8)) {
222 v4l2_err(&vpbe_dev->v4l2_dev,
223 "buffer_prepare:offset is not aligned to 32 bytes\n");
224 return -EINVAL;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300225 }
226 return 0;
227}
228
229/*
230 * vpbe_buffer_setup()
231 * This function allocates memory for the buffers
232 */
Lad, Prabhakar13fc23d32012-10-22 09:27:13 -0300233static int
Hans Verkuildf9ecb0c2015-10-28 00:50:37 -0200234vpbe_buffer_queue_setup(struct vb2_queue *vq,
Lad, Prabhakar13fc23d32012-10-22 09:27:13 -0300235 unsigned int *nbuffers, unsigned int *nplanes,
Hans Verkuil36c0f8b2016-04-15 09:15:05 -0300236 unsigned int sizes[], struct device *alloc_devs[])
Lad, Prabhakar13fc23d32012-10-22 09:27:13 -0300237
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300238{
239 /* Get the file handle object and layer object */
Prabhakar Lade71a1802014-10-12 17:40:31 -0300240 struct vpbe_layer *layer = vb2_get_drv_priv(vq);
241 struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300242
243 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_buffer_setup\n");
244
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300245 /* Store number of buffers allocated in numbuffer member */
Prabhakar Lad7041bc92014-10-22 18:42:01 -0300246 if (vq->num_buffers + *nbuffers < VPBE_DEFAULT_NUM_BUFS)
247 *nbuffers = VPBE_DEFAULT_NUM_BUFS - vq->num_buffers;
Hans Verkuildf9ecb0c2015-10-28 00:50:37 -0200248
249 if (*nplanes)
250 return sizes[0] < layer->pix_fmt.sizeimage ? -EINVAL : 0;
Lad, Prabhakar13fc23d32012-10-22 09:27:13 -0300251
252 *nplanes = 1;
Hans Verkuildf9ecb0c2015-10-28 00:50:37 -0200253 sizes[0] = layer->pix_fmt.sizeimage;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300254
255 return 0;
256}
257
258/*
259 * vpbe_buffer_queue()
260 * This function adds the buffer to DMA queue
261 */
Lad, Prabhakar13fc23d32012-10-22 09:27:13 -0300262static void vpbe_buffer_queue(struct vb2_buffer *vb)
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300263{
Junghak Sung2d700712015-09-22 10:30:30 -0300264 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300265 /* Get the file handle object and layer object */
Junghak Sung2d700712015-09-22 10:30:30 -0300266 struct vpbe_disp_buffer *buf = container_of(vbuf,
Lad, Prabhakar13fc23d32012-10-22 09:27:13 -0300267 struct vpbe_disp_buffer, vb);
Prabhakar Lade71a1802014-10-12 17:40:31 -0300268 struct vpbe_layer *layer = vb2_get_drv_priv(vb->vb2_queue);
269 struct vpbe_display *disp = layer->disp_dev;
270 struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300271 unsigned long flags;
272
273 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
274 "vpbe_buffer_queue\n");
275
276 /* add the buffer to the DMA queue */
277 spin_lock_irqsave(&disp->dma_queue_lock, flags);
Lad, Prabhakar13fc23d32012-10-22 09:27:13 -0300278 list_add_tail(&buf->list, &layer->dma_queue);
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300279 spin_unlock_irqrestore(&disp->dma_queue_lock, flags);
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300280}
281
Lad, Prabhakar13fc23d32012-10-22 09:27:13 -0300282static int vpbe_start_streaming(struct vb2_queue *vq, unsigned int count)
283{
Prabhakar Lade71a1802014-10-12 17:40:31 -0300284 struct vpbe_layer *layer = vb2_get_drv_priv(vq);
Prabhakar Lad41cf47b2014-10-12 17:40:38 -0300285 struct osd_state *osd_device = layer->disp_dev->osd_device;
Lad, Prabhakar13fc23d32012-10-22 09:27:13 -0300286 int ret;
287
Prabhakar Lad41cf47b2014-10-12 17:40:38 -0300288 osd_device->ops.disable_layer(osd_device, layer->layer_info.id);
289
Lad, Prabhakar13fc23d32012-10-22 09:27:13 -0300290 /* Get the next frame from the buffer queue */
291 layer->next_frm = layer->cur_frm = list_entry(layer->dma_queue.next,
292 struct vpbe_disp_buffer, list);
293 /* Remove buffer from the buffer queue */
294 list_del(&layer->cur_frm->list);
295 /* Mark state of the current frame to active */
Junghak Sung2d700712015-09-22 10:30:30 -0300296 layer->cur_frm->vb.vb2_buf.state = VB2_BUF_STATE_ACTIVE;
Lad, Prabhakar13fc23d32012-10-22 09:27:13 -0300297 /* Initialize field_id and started member */
298 layer->field_id = 0;
299
300 /* Set parameters in OSD and VENC */
Prabhakar Lade71a1802014-10-12 17:40:31 -0300301 ret = vpbe_set_osd_display_params(layer->disp_dev, layer);
Lad, Prabhakarb1c090d2014-04-14 11:52:31 -0300302 if (ret < 0) {
303 struct vpbe_disp_buffer *buf, *tmp;
304
Junghak Sung2d700712015-09-22 10:30:30 -0300305 vb2_buffer_done(&layer->cur_frm->vb.vb2_buf,
306 VB2_BUF_STATE_QUEUED);
Lad, Prabhakarb1c090d2014-04-14 11:52:31 -0300307 list_for_each_entry_safe(buf, tmp, &layer->dma_queue, list) {
308 list_del(&buf->list);
Junghak Sung2d700712015-09-22 10:30:30 -0300309 vb2_buffer_done(&buf->vb.vb2_buf,
310 VB2_BUF_STATE_QUEUED);
Lad, Prabhakarb1c090d2014-04-14 11:52:31 -0300311 }
312
Lad, Prabhakar13fc23d32012-10-22 09:27:13 -0300313 return ret;
Lad, Prabhakarb1c090d2014-04-14 11:52:31 -0300314 }
Lad, Prabhakar13fc23d32012-10-22 09:27:13 -0300315
316 /*
317 * if request format is yuv420 semiplanar, need to
318 * enable both video windows
319 */
Lad, Prabhakar13fc23d32012-10-22 09:27:13 -0300320 layer->layer_first_int = 1;
321
322 return ret;
323}
324
Hans Verkuile37559b2014-04-17 02:47:21 -0300325static void vpbe_stop_streaming(struct vb2_queue *vq)
Lad, Prabhakar13fc23d32012-10-22 09:27:13 -0300326{
Prabhakar Lade71a1802014-10-12 17:40:31 -0300327 struct vpbe_layer *layer = vb2_get_drv_priv(vq);
Prabhakar Lad41cf47b2014-10-12 17:40:38 -0300328 struct osd_state *osd_device = layer->disp_dev->osd_device;
Prabhakar Lade71a1802014-10-12 17:40:31 -0300329 struct vpbe_display *disp = layer->disp_dev;
Lad, Prabhakarb699f092014-03-22 08:03:09 -0300330 unsigned long flags;
Lad, Prabhakar13fc23d32012-10-22 09:27:13 -0300331
332 if (!vb2_is_streaming(vq))
Hans Verkuile37559b2014-04-17 02:47:21 -0300333 return;
Lad, Prabhakar13fc23d32012-10-22 09:27:13 -0300334
Prabhakar Lad41cf47b2014-10-12 17:40:38 -0300335 osd_device->ops.disable_layer(osd_device, layer->layer_info.id);
336
Lad, Prabhakar13fc23d32012-10-22 09:27:13 -0300337 /* release all active buffers */
Lad, Prabhakarb699f092014-03-22 08:03:09 -0300338 spin_lock_irqsave(&disp->dma_queue_lock, flags);
339 if (layer->cur_frm == layer->next_frm) {
Junghak Sung2d700712015-09-22 10:30:30 -0300340 vb2_buffer_done(&layer->cur_frm->vb.vb2_buf,
341 VB2_BUF_STATE_ERROR);
Lad, Prabhakarb699f092014-03-22 08:03:09 -0300342 } else {
Markus Elfring7a6e6c32017-09-07 16:37:16 -0400343 if (layer->cur_frm)
Junghak Sung2d700712015-09-22 10:30:30 -0300344 vb2_buffer_done(&layer->cur_frm->vb.vb2_buf,
Lad, Prabhakarb699f092014-03-22 08:03:09 -0300345 VB2_BUF_STATE_ERROR);
Markus Elfring7a6e6c32017-09-07 16:37:16 -0400346 if (layer->next_frm)
Junghak Sung2d700712015-09-22 10:30:30 -0300347 vb2_buffer_done(&layer->next_frm->vb.vb2_buf,
Lad, Prabhakarb699f092014-03-22 08:03:09 -0300348 VB2_BUF_STATE_ERROR);
349 }
350
Lad, Prabhakar13fc23d32012-10-22 09:27:13 -0300351 while (!list_empty(&layer->dma_queue)) {
352 layer->next_frm = list_entry(layer->dma_queue.next,
353 struct vpbe_disp_buffer, list);
354 list_del(&layer->next_frm->list);
Junghak Sung2d700712015-09-22 10:30:30 -0300355 vb2_buffer_done(&layer->next_frm->vb.vb2_buf,
356 VB2_BUF_STATE_ERROR);
Lad, Prabhakar13fc23d32012-10-22 09:27:13 -0300357 }
Lad, Prabhakarb699f092014-03-22 08:03:09 -0300358 spin_unlock_irqrestore(&disp->dma_queue_lock, flags);
Lad, Prabhakar13fc23d32012-10-22 09:27:13 -0300359}
360
Julia Lawallc2363232017-08-05 06:47:09 -0400361static const struct vb2_ops video_qops = {
Lad, Prabhakar13fc23d32012-10-22 09:27:13 -0300362 .queue_setup = vpbe_buffer_queue_setup,
Prabhakar Lad488735b2014-10-12 17:40:33 -0300363 .wait_prepare = vb2_ops_wait_prepare,
364 .wait_finish = vb2_ops_wait_finish,
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300365 .buf_prepare = vpbe_buffer_prepare,
Lad, Prabhakar13fc23d32012-10-22 09:27:13 -0300366 .start_streaming = vpbe_start_streaming,
367 .stop_streaming = vpbe_stop_streaming,
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300368 .buf_queue = vpbe_buffer_queue,
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300369};
370
371static
372struct vpbe_layer*
373_vpbe_display_get_other_win_layer(struct vpbe_display *disp_dev,
374 struct vpbe_layer *layer)
375{
376 enum vpbe_display_device_id thiswin, otherwin;
377 thiswin = layer->device_id;
378
379 otherwin = (thiswin == VPBE_DISPLAY_DEVICE_0) ?
380 VPBE_DISPLAY_DEVICE_1 : VPBE_DISPLAY_DEVICE_0;
381 return disp_dev->dev[otherwin];
382}
383
384static int vpbe_set_osd_display_params(struct vpbe_display *disp_dev,
385 struct vpbe_layer *layer)
386{
387 struct osd_layer_config *cfg = &layer->layer_info.config;
388 struct osd_state *osd_device = disp_dev->osd_device;
389 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
390 unsigned long addr;
391 int ret;
392
Junghak Sung2d700712015-09-22 10:30:30 -0300393 addr = vb2_dma_contig_plane_dma_addr(&layer->cur_frm->vb.vb2_buf, 0);
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300394 /* Set address in the display registers */
395 osd_device->ops.start_layer(osd_device,
396 layer->layer_info.id,
397 addr,
398 disp_dev->cbcr_ofst);
399
400 ret = osd_device->ops.enable_layer(osd_device,
401 layer->layer_info.id, 0);
402 if (ret < 0) {
403 v4l2_err(&vpbe_dev->v4l2_dev,
404 "Error in enabling osd window layer 0\n");
405 return -1;
406 }
407
408 /* Enable the window */
409 layer->layer_info.enable = 1;
410 if (cfg->pixfmt == PIXFMT_NV12) {
411 struct vpbe_layer *otherlayer =
412 _vpbe_display_get_other_win_layer(disp_dev, layer);
413
414 ret = osd_device->ops.enable_layer(osd_device,
415 otherlayer->layer_info.id, 1);
416 if (ret < 0) {
417 v4l2_err(&vpbe_dev->v4l2_dev,
418 "Error in enabling osd window layer 1\n");
419 return -1;
420 }
421 otherlayer->layer_info.enable = 1;
422 }
423 return 0;
424}
425
426static void
427vpbe_disp_calculate_scale_factor(struct vpbe_display *disp_dev,
428 struct vpbe_layer *layer,
429 int expected_xsize, int expected_ysize)
430{
431 struct display_layer_info *layer_info = &layer->layer_info;
432 struct v4l2_pix_format *pixfmt = &layer->pix_fmt;
433 struct osd_layer_config *cfg = &layer->layer_info.config;
434 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
435 int calculated_xsize;
436 int h_exp = 0;
437 int v_exp = 0;
438 int h_scale;
439 int v_scale;
440
Hans Verkuil36864082012-10-01 11:39:46 -0300441 v4l2_std_id standard_id = vpbe_dev->current_timings.std_id;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300442
443 /*
444 * Application initially set the image format. Current display
445 * size is obtained from the vpbe display controller. expected_xsize
Hans Verkuil516aca32016-07-03 07:53:31 -0300446 * and expected_ysize are set through S_SELECTION ioctl. Based on this,
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300447 * driver will calculate the scale factors for vertical and
448 * horizontal direction so that the image is displayed scaled
449 * and expanded. Application uses expansion to display the image
450 * in a square pixel. Otherwise it is displayed using displays
451 * pixel aspect ratio.It is expected that application chooses
452 * the crop coordinates for cropped or scaled display. if crop
453 * size is less than the image size, it is displayed cropped or
454 * it is displayed scaled and/or expanded.
455 *
456 * to begin with, set the crop window same as expected. Later we
457 * will override with scaled window size
458 */
459
460 cfg->xsize = pixfmt->width;
461 cfg->ysize = pixfmt->height;
462 layer_info->h_zoom = ZOOM_X1; /* no horizontal zoom */
463 layer_info->v_zoom = ZOOM_X1; /* no horizontal zoom */
464 layer_info->h_exp = H_EXP_OFF; /* no horizontal zoom */
465 layer_info->v_exp = V_EXP_OFF; /* no horizontal zoom */
466
467 if (pixfmt->width < expected_xsize) {
468 h_scale = vpbe_dev->current_timings.xres / pixfmt->width;
469 if (h_scale < 2)
470 h_scale = 1;
471 else if (h_scale >= 4)
472 h_scale = 4;
473 else
474 h_scale = 2;
475 cfg->xsize *= h_scale;
476 if (cfg->xsize < expected_xsize) {
477 if ((standard_id & V4L2_STD_525_60) ||
478 (standard_id & V4L2_STD_625_50)) {
479 calculated_xsize = (cfg->xsize *
480 VPBE_DISPLAY_H_EXP_RATIO_N) /
481 VPBE_DISPLAY_H_EXP_RATIO_D;
482 if (calculated_xsize <= expected_xsize) {
483 h_exp = 1;
484 cfg->xsize = calculated_xsize;
485 }
486 }
487 }
488 if (h_scale == 2)
489 layer_info->h_zoom = ZOOM_X2;
490 else if (h_scale == 4)
491 layer_info->h_zoom = ZOOM_X4;
492 if (h_exp)
493 layer_info->h_exp = H_EXP_9_OVER_8;
494 } else {
495 /* no scaling, only cropping. Set display area to crop area */
496 cfg->xsize = expected_xsize;
497 }
498
499 if (pixfmt->height < expected_ysize) {
500 v_scale = expected_ysize / pixfmt->height;
501 if (v_scale < 2)
502 v_scale = 1;
503 else if (v_scale >= 4)
504 v_scale = 4;
505 else
506 v_scale = 2;
507 cfg->ysize *= v_scale;
508 if (cfg->ysize < expected_ysize) {
509 if ((standard_id & V4L2_STD_625_50)) {
510 calculated_xsize = (cfg->ysize *
511 VPBE_DISPLAY_V_EXP_RATIO_N) /
512 VPBE_DISPLAY_V_EXP_RATIO_D;
513 if (calculated_xsize <= expected_ysize) {
514 v_exp = 1;
515 cfg->ysize = calculated_xsize;
516 }
517 }
518 }
519 if (v_scale == 2)
520 layer_info->v_zoom = ZOOM_X2;
521 else if (v_scale == 4)
522 layer_info->v_zoom = ZOOM_X4;
523 if (v_exp)
524 layer_info->h_exp = V_EXP_6_OVER_5;
525 } else {
526 /* no scaling, only cropping. Set display area to crop area */
527 cfg->ysize = expected_ysize;
528 }
529 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
530 "crop display xsize = %d, ysize = %d\n",
531 cfg->xsize, cfg->ysize);
532}
533
534static void vpbe_disp_adj_position(struct vpbe_display *disp_dev,
535 struct vpbe_layer *layer,
536 int top, int left)
537{
538 struct osd_layer_config *cfg = &layer->layer_info.config;
539 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
540
541 cfg->xpos = min((unsigned int)left,
542 vpbe_dev->current_timings.xres - cfg->xsize);
543 cfg->ypos = min((unsigned int)top,
544 vpbe_dev->current_timings.yres - cfg->ysize);
545
546 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
547 "new xpos = %d, ypos = %d\n",
548 cfg->xpos, cfg->ypos);
549}
550
551static void vpbe_disp_check_window_params(struct vpbe_display *disp_dev,
552 struct v4l2_rect *c)
553{
554 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
555
556 if ((c->width == 0) ||
557 ((c->width + c->left) > vpbe_dev->current_timings.xres))
558 c->width = vpbe_dev->current_timings.xres - c->left;
559
560 if ((c->height == 0) || ((c->height + c->top) >
561 vpbe_dev->current_timings.yres))
562 c->height = vpbe_dev->current_timings.yres - c->top;
563
564 /* window height must be even for interlaced display */
565 if (vpbe_dev->current_timings.interlaced)
566 c->height &= (~0x01);
567
568}
569
Mauro Carvalho Chehabe199fa72018-04-05 12:51:00 -0400570/*
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300571 * vpbe_try_format()
572 * If user application provides width and height, and have bytesperline set
573 * to zero, driver calculates bytesperline and sizeimage based on hardware
574 * limits.
575 */
576static int vpbe_try_format(struct vpbe_display *disp_dev,
577 struct v4l2_pix_format *pixfmt, int check)
578{
579 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
580 int min_height = 1;
581 int min_width = 32;
582 int max_height;
583 int max_width;
584 int bpp;
585
586 if ((pixfmt->pixelformat != V4L2_PIX_FMT_UYVY) &&
587 (pixfmt->pixelformat != V4L2_PIX_FMT_NV12))
588 /* choose default as V4L2_PIX_FMT_UYVY */
589 pixfmt->pixelformat = V4L2_PIX_FMT_UYVY;
590
591 /* Check the field format */
592 if ((pixfmt->field != V4L2_FIELD_INTERLACED) &&
593 (pixfmt->field != V4L2_FIELD_NONE)) {
594 if (vpbe_dev->current_timings.interlaced)
595 pixfmt->field = V4L2_FIELD_INTERLACED;
596 else
597 pixfmt->field = V4L2_FIELD_NONE;
598 }
599
600 if (pixfmt->field == V4L2_FIELD_INTERLACED)
601 min_height = 2;
602
603 if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12)
604 bpp = 1;
605 else
606 bpp = 2;
607
608 max_width = vpbe_dev->current_timings.xres;
609 max_height = vpbe_dev->current_timings.yres;
610
611 min_width /= bpp;
612
613 if (!pixfmt->width || (pixfmt->width < min_width) ||
614 (pixfmt->width > max_width)) {
615 pixfmt->width = vpbe_dev->current_timings.xres;
616 }
617
618 if (!pixfmt->height || (pixfmt->height < min_height) ||
619 (pixfmt->height > max_height)) {
620 pixfmt->height = vpbe_dev->current_timings.yres;
621 }
622
623 if (pixfmt->bytesperline < (pixfmt->width * bpp))
624 pixfmt->bytesperline = pixfmt->width * bpp;
625
626 /* Make the bytesperline 32 byte aligned */
627 pixfmt->bytesperline = ((pixfmt->width * bpp + 31) & ~31);
628
629 if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12)
630 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height +
631 (pixfmt->bytesperline * pixfmt->height >> 1);
632 else
633 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height;
634
635 return 0;
636}
637
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300638static int vpbe_display_querycap(struct file *file, void *priv,
639 struct v4l2_capability *cap)
640{
Prabhakar Lad4bb12312014-10-12 17:40:37 -0300641 struct vpbe_layer *layer = video_drvdata(file);
642 struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300643
Lad, Prabhakard0466282012-10-22 09:27:14 -0300644 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
645 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
646 snprintf(cap->driver, sizeof(cap->driver), "%s",
647 dev_name(vpbe_dev->pdev));
648 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
649 dev_name(vpbe_dev->pdev));
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300650 strlcpy(cap->card, vpbe_dev->cfg->module_name, sizeof(cap->card));
651
652 return 0;
653}
654
Hans Verkuil516aca32016-07-03 07:53:31 -0300655static int vpbe_display_s_selection(struct file *file, void *priv,
656 struct v4l2_selection *sel)
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300657{
Prabhakar Lad4bb12312014-10-12 17:40:37 -0300658 struct vpbe_layer *layer = video_drvdata(file);
659 struct vpbe_display *disp_dev = layer->disp_dev;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300660 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
661 struct osd_layer_config *cfg = &layer->layer_info.config;
662 struct osd_state *osd_device = disp_dev->osd_device;
Hans Verkuil516aca32016-07-03 07:53:31 -0300663 struct v4l2_rect rect = sel->r;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300664 int ret;
665
666 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
Hans Verkuil516aca32016-07-03 07:53:31 -0300667 "VIDIOC_S_SELECTION, layer id = %d\n", layer->device_id);
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300668
Hans Verkuil516aca32016-07-03 07:53:31 -0300669 if (sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
670 sel->target != V4L2_SEL_TGT_CROP)
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300671 return -EINVAL;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300672
Lad, Prabhakarfabc4e92012-10-03 02:13:28 -0300673 if (rect.top < 0)
674 rect.top = 0;
675 if (rect.left < 0)
676 rect.left = 0;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300677
Lad, Prabhakarfabc4e92012-10-03 02:13:28 -0300678 vpbe_disp_check_window_params(disp_dev, &rect);
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300679
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,
Lad, Prabhakarfabc4e92012-10-03 02:13:28 -0300684 rect.width,
685 rect.height);
686 vpbe_disp_adj_position(disp_dev, layer, rect.top,
687 rect.left);
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300688 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
Hans Verkuil516aca32016-07-03 07:53:31 -0300719 sel->r = rect;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300720 return 0;
721}
722
Hans Verkuil516aca32016-07-03 07:53:31 -0300723static int vpbe_display_g_selection(struct file *file, void *priv,
724 struct v4l2_selection *sel)
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300725{
Prabhakar Lad4bb12312014-10-12 17:40:37 -0300726 struct vpbe_layer *layer = video_drvdata(file);
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300727 struct osd_layer_config *cfg = &layer->layer_info.config;
Prabhakar Lad4bb12312014-10-12 17:40:37 -0300728 struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
729 struct osd_state *osd_device = layer->disp_dev->osd_device;
Hans Verkuil516aca32016-07-03 07:53:31 -0300730 struct v4l2_rect *rect = &sel->r;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300731
732 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
Hans Verkuil516aca32016-07-03 07:53:31 -0300733 "VIDIOC_G_SELECTION, layer id = %d\n",
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300734 layer->device_id);
735
Hans Verkuil516aca32016-07-03 07:53:31 -0300736 if (sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
737 return -EINVAL;
738
739 switch (sel->target) {
740 case V4L2_SEL_TGT_CROP:
741 osd_device->ops.get_layer_config(osd_device,
742 layer->layer_info.id, cfg);
743 rect->top = cfg->ypos;
744 rect->left = cfg->xpos;
745 rect->width = cfg->xsize;
746 rect->height = cfg->ysize;
747 break;
748 case V4L2_SEL_TGT_CROP_DEFAULT:
749 case V4L2_SEL_TGT_CROP_BOUNDS:
750 rect->left = 0;
751 rect->top = 0;
752 rect->width = vpbe_dev->current_timings.xres;
753 rect->height = vpbe_dev->current_timings.yres;
754 break;
755 default:
Wei Yongjune276f032012-12-02 22:50:47 -0300756 return -EINVAL;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300757 }
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300758
759 return 0;
760}
761
762static int vpbe_display_cropcap(struct file *file, void *priv,
763 struct v4l2_cropcap *cropcap)
764{
Prabhakar Lad4bb12312014-10-12 17:40:37 -0300765 struct vpbe_layer *layer = video_drvdata(file);
766 struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300767
768 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_CROPCAP ioctl\n");
769
Hans Verkuil516aca32016-07-03 07:53:31 -0300770 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
771 return -EINVAL;
772
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300773 cropcap->pixelaspect = vpbe_dev->current_timings.aspect;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300774 return 0;
775}
776
777static int vpbe_display_g_fmt(struct file *file, void *priv,
778 struct v4l2_format *fmt)
779{
Prabhakar Lad4bb12312014-10-12 17:40:37 -0300780 struct vpbe_layer *layer = video_drvdata(file);
781 struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300782
783 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
784 "VIDIOC_G_FMT, layer id = %d\n",
785 layer->device_id);
786
787 /* If buffer type is video output */
788 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != fmt->type) {
789 v4l2_err(&vpbe_dev->v4l2_dev, "invalid type\n");
790 return -EINVAL;
791 }
792 /* Fill in the information about format */
793 fmt->fmt.pix = layer->pix_fmt;
794
795 return 0;
796}
797
798static int vpbe_display_enum_fmt(struct file *file, void *priv,
799 struct v4l2_fmtdesc *fmt)
800{
Prabhakar Lad4bb12312014-10-12 17:40:37 -0300801 struct vpbe_layer *layer = video_drvdata(file);
802 struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300803 unsigned int index = 0;
804
805 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
806 "VIDIOC_ENUM_FMT, layer id = %d\n",
807 layer->device_id);
808 if (fmt->index > 1) {
809 v4l2_err(&vpbe_dev->v4l2_dev, "Invalid format index\n");
810 return -EINVAL;
811 }
812
813 /* Fill in the information about format */
814 index = fmt->index;
815 memset(fmt, 0, sizeof(*fmt));
816 fmt->index = index;
817 fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
818 if (index == 0) {
819 strcpy(fmt->description, "YUV 4:2:2 - UYVY");
820 fmt->pixelformat = V4L2_PIX_FMT_UYVY;
821 } else {
822 strcpy(fmt->description, "Y/CbCr 4:2:0");
823 fmt->pixelformat = V4L2_PIX_FMT_NV12;
824 }
825
826 return 0;
827}
828
829static int vpbe_display_s_fmt(struct file *file, void *priv,
830 struct v4l2_format *fmt)
831{
Prabhakar Lad4bb12312014-10-12 17:40:37 -0300832 struct vpbe_layer *layer = video_drvdata(file);
833 struct vpbe_display *disp_dev = layer->disp_dev;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300834 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
835 struct osd_layer_config *cfg = &layer->layer_info.config;
836 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
837 struct osd_state *osd_device = disp_dev->osd_device;
838 int ret;
839
840 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
841 "VIDIOC_S_FMT, layer id = %d\n",
842 layer->device_id);
843
Prabhakar Lad1b73f032014-10-12 17:40:42 -0300844 if (vb2_is_busy(&layer->buffer_queue))
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300845 return -EBUSY;
Prabhakar Lad1b73f032014-10-12 17:40:42 -0300846
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300847 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != fmt->type) {
848 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "invalid type\n");
849 return -EINVAL;
850 }
851 /* Check for valid pixel format */
852 ret = vpbe_try_format(disp_dev, pixfmt, 1);
853 if (ret)
854 return ret;
855
856 /* YUV420 is requested, check availability of the
857 other video window */
858
859 layer->pix_fmt = *pixfmt;
Lad, Prabhakarbdea0d22013-05-07 01:07:25 -0300860 if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12) {
861 struct vpbe_layer *otherlayer;
862
863 otherlayer = _vpbe_display_get_other_win_layer(disp_dev, layer);
864 /* if other layer is available, only
865 * claim it, do not configure it
866 */
867 ret = osd_device->ops.request_layer(osd_device,
868 otherlayer->layer_info.id);
869 if (ret < 0) {
870 v4l2_err(&vpbe_dev->v4l2_dev,
871 "Display Manager failed to allocate layer\n");
872 return -EBUSY;
873 }
874 }
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300875
876 /* Get osd layer config */
877 osd_device->ops.get_layer_config(osd_device,
878 layer->layer_info.id, cfg);
879 /* Store the pixel format in the layer object */
880 cfg->xsize = pixfmt->width;
881 cfg->ysize = pixfmt->height;
882 cfg->line_length = pixfmt->bytesperline;
883 cfg->ypos = 0;
884 cfg->xpos = 0;
885 cfg->interlaced = vpbe_dev->current_timings.interlaced;
886
887 if (V4L2_PIX_FMT_UYVY == pixfmt->pixelformat)
Lad, Prabhakar849325e2013-05-03 08:39:25 -0300888 cfg->pixfmt = PIXFMT_YCBCRI;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300889
890 /* Change of the default pixel format for both video windows */
891 if (V4L2_PIX_FMT_NV12 == pixfmt->pixelformat) {
892 struct vpbe_layer *otherlayer;
893 cfg->pixfmt = PIXFMT_NV12;
894 otherlayer = _vpbe_display_get_other_win_layer(disp_dev,
895 layer);
896 otherlayer->layer_info.config.pixfmt = PIXFMT_NV12;
897 }
898
899 /* Set the layer config in the osd window */
900 ret = osd_device->ops.set_layer_config(osd_device,
901 layer->layer_info.id, cfg);
902 if (ret < 0) {
903 v4l2_err(&vpbe_dev->v4l2_dev,
904 "Error in S_FMT params:\n");
905 return -EINVAL;
906 }
907
908 /* Readback and fill the local copy of current pix format */
909 osd_device->ops.get_layer_config(osd_device,
910 layer->layer_info.id, cfg);
911
912 return 0;
913}
914
915static int vpbe_display_try_fmt(struct file *file, void *priv,
916 struct v4l2_format *fmt)
917{
Prabhakar Lad4bb12312014-10-12 17:40:37 -0300918 struct vpbe_layer *layer = video_drvdata(file);
919 struct vpbe_display *disp_dev = layer->disp_dev;
920 struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300921 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
922
923 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_TRY_FMT\n");
924
925 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != fmt->type) {
926 v4l2_err(&vpbe_dev->v4l2_dev, "invalid type\n");
927 return -EINVAL;
928 }
929
930 /* Check for valid field format */
931 return vpbe_try_format(disp_dev, pixfmt, 0);
932
933}
934
Mauro Carvalho Chehabe199fa72018-04-05 12:51:00 -0400935/*
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300936 * vpbe_display_s_std - Set the given standard in the encoder
937 *
938 * Sets the standard if supported by the current encoder. Return the status.
939 * 0 - success & -EINVAL on error
940 */
941static int vpbe_display_s_std(struct file *file, void *priv,
Hans Verkuil314527a2013-03-15 06:10:40 -0300942 v4l2_std_id std_id)
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300943{
Prabhakar Lad4bb12312014-10-12 17:40:37 -0300944 struct vpbe_layer *layer = video_drvdata(file);
945 struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300946 int ret;
947
948 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_S_STD\n");
949
Prabhakar Lad1b73f032014-10-12 17:40:42 -0300950 if (vb2_is_busy(&layer->buffer_queue))
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300951 return -EBUSY;
Prabhakar Lad1b73f032014-10-12 17:40:42 -0300952
Markus Elfring7a6e6c32017-09-07 16:37:16 -0400953 if (vpbe_dev->ops.s_std) {
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300954 ret = vpbe_dev->ops.s_std(vpbe_dev, std_id);
955 if (ret) {
956 v4l2_err(&vpbe_dev->v4l2_dev,
957 "Failed to set standard for sub devices\n");
958 return -EINVAL;
959 }
960 } else {
961 return -EINVAL;
962 }
963
964 return 0;
965}
966
Mauro Carvalho Chehabe199fa72018-04-05 12:51:00 -0400967/*
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300968 * vpbe_display_g_std - Get the standard in the current encoder
969 *
970 * Get the standard in the current encoder. Return the status. 0 - success
971 * -EINVAL on error
972 */
973static int vpbe_display_g_std(struct file *file, void *priv,
974 v4l2_std_id *std_id)
975{
Prabhakar Lad4bb12312014-10-12 17:40:37 -0300976 struct vpbe_layer *layer = video_drvdata(file);
977 struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300978
979 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_G_STD\n");
980
981 /* Get the standard from the current encoder */
982 if (vpbe_dev->current_timings.timings_type & VPBE_ENC_STD) {
Hans Verkuil36864082012-10-01 11:39:46 -0300983 *std_id = vpbe_dev->current_timings.std_id;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300984 return 0;
985 }
986
987 return -EINVAL;
988}
989
Mauro Carvalho Chehabe199fa72018-04-05 12:51:00 -0400990/*
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300991 * vpbe_display_enum_output - enumerate outputs
992 *
993 * Enumerates the outputs available at the vpbe display
994 * returns the status, -EINVAL if end of output list
995 */
996static int vpbe_display_enum_output(struct file *file, void *priv,
997 struct v4l2_output *output)
998{
Prabhakar Lad4bb12312014-10-12 17:40:37 -0300999 struct vpbe_layer *layer = video_drvdata(file);
1000 struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001001 int ret;
1002
1003 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_ENUM_OUTPUT\n");
1004
1005 /* Enumerate outputs */
Markus Elfring7a6e6c32017-09-07 16:37:16 -04001006 if (!vpbe_dev->ops.enum_outputs)
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001007 return -EINVAL;
1008
1009 ret = vpbe_dev->ops.enum_outputs(vpbe_dev, output);
1010 if (ret) {
1011 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1012 "Failed to enumerate outputs\n");
1013 return -EINVAL;
1014 }
1015
1016 return 0;
1017}
1018
Mauro Carvalho Chehabe199fa72018-04-05 12:51:00 -04001019/*
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001020 * vpbe_display_s_output - Set output to
1021 * the output specified by the index
1022 */
1023static int vpbe_display_s_output(struct file *file, void *priv,
1024 unsigned int i)
1025{
Prabhakar Lad4bb12312014-10-12 17:40:37 -03001026 struct vpbe_layer *layer = video_drvdata(file);
1027 struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001028 int ret;
1029
1030 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_S_OUTPUT\n");
Prabhakar Lad1b73f032014-10-12 17:40:42 -03001031
1032 if (vb2_is_busy(&layer->buffer_queue))
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001033 return -EBUSY;
Prabhakar Lad1b73f032014-10-12 17:40:42 -03001034
Markus Elfring7a6e6c32017-09-07 16:37:16 -04001035 if (!vpbe_dev->ops.set_output)
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001036 return -EINVAL;
1037
1038 ret = vpbe_dev->ops.set_output(vpbe_dev, i);
1039 if (ret) {
1040 v4l2_err(&vpbe_dev->v4l2_dev,
1041 "Failed to set output for sub devices\n");
1042 return -EINVAL;
1043 }
1044
1045 return 0;
1046}
1047
Mauro Carvalho Chehabe199fa72018-04-05 12:51:00 -04001048/*
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001049 * vpbe_display_g_output - Get output from subdevice
1050 * for a given by the index
1051 */
1052static int vpbe_display_g_output(struct file *file, void *priv,
1053 unsigned int *i)
1054{
Prabhakar Lad4bb12312014-10-12 17:40:37 -03001055 struct vpbe_layer *layer = video_drvdata(file);
1056 struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001057
1058 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_G_OUTPUT\n");
1059 /* Get the standard from the current encoder */
1060 *i = vpbe_dev->current_out_index;
1061
1062 return 0;
1063}
1064
Mauro Carvalho Chehabe199fa72018-04-05 12:51:00 -04001065/*
Hans Verkuil36864082012-10-01 11:39:46 -03001066 * vpbe_display_enum_dv_timings - Enumerate the dv timings
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001067 *
Hans Verkuil36864082012-10-01 11:39:46 -03001068 * enum the timings in the current encoder. Return the status. 0 - success
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001069 * -EINVAL on error
1070 */
1071static int
Hans Verkuil36864082012-10-01 11:39:46 -03001072vpbe_display_enum_dv_timings(struct file *file, void *priv,
1073 struct v4l2_enum_dv_timings *timings)
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001074{
Prabhakar Lad4bb12312014-10-12 17:40:37 -03001075 struct vpbe_layer *layer = video_drvdata(file);
1076 struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001077 int ret;
1078
Hans Verkuil36864082012-10-01 11:39:46 -03001079 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_ENUM_DV_TIMINGS\n");
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001080
1081 /* Enumerate outputs */
Markus Elfring7a6e6c32017-09-07 16:37:16 -04001082 if (!vpbe_dev->ops.enum_dv_timings)
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001083 return -EINVAL;
1084
Hans Verkuil36864082012-10-01 11:39:46 -03001085 ret = vpbe_dev->ops.enum_dv_timings(vpbe_dev, timings);
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001086 if (ret) {
1087 v4l2_err(&vpbe_dev->v4l2_dev,
Hans Verkuil36864082012-10-01 11:39:46 -03001088 "Failed to enumerate dv timings info\n");
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001089 return -EINVAL;
1090 }
1091
1092 return 0;
1093}
1094
Mauro Carvalho Chehabe199fa72018-04-05 12:51:00 -04001095/*
Hans Verkuil36864082012-10-01 11:39:46 -03001096 * vpbe_display_s_dv_timings - Set the dv timings
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001097 *
Hans Verkuil36864082012-10-01 11:39:46 -03001098 * Set the timings in the current encoder. Return the status. 0 - success
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001099 * -EINVAL on error
1100 */
1101static int
Hans Verkuil36864082012-10-01 11:39:46 -03001102vpbe_display_s_dv_timings(struct file *file, void *priv,
1103 struct v4l2_dv_timings *timings)
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001104{
Prabhakar Lad4bb12312014-10-12 17:40:37 -03001105 struct vpbe_layer *layer = video_drvdata(file);
1106 struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001107 int ret;
1108
Hans Verkuil36864082012-10-01 11:39:46 -03001109 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_S_DV_TIMINGS\n");
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001110
Prabhakar Lad1b73f032014-10-12 17:40:42 -03001111 if (vb2_is_busy(&layer->buffer_queue))
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001112 return -EBUSY;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001113
1114 /* Set the given standard in the encoder */
Hans Verkuil36864082012-10-01 11:39:46 -03001115 if (!vpbe_dev->ops.s_dv_timings)
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001116 return -EINVAL;
1117
Hans Verkuil36864082012-10-01 11:39:46 -03001118 ret = vpbe_dev->ops.s_dv_timings(vpbe_dev, timings);
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001119 if (ret) {
1120 v4l2_err(&vpbe_dev->v4l2_dev,
Hans Verkuil36864082012-10-01 11:39:46 -03001121 "Failed to set the dv timings info\n");
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001122 return -EINVAL;
1123 }
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001124
1125 return 0;
1126}
1127
Mauro Carvalho Chehabe199fa72018-04-05 12:51:00 -04001128/*
Hans Verkuil36864082012-10-01 11:39:46 -03001129 * vpbe_display_g_dv_timings - Set the dv timings
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001130 *
Hans Verkuil36864082012-10-01 11:39:46 -03001131 * Get the timings in the current encoder. Return the status. 0 - success
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001132 * -EINVAL on error
1133 */
1134static int
Hans Verkuil36864082012-10-01 11:39:46 -03001135vpbe_display_g_dv_timings(struct file *file, void *priv,
1136 struct v4l2_dv_timings *dv_timings)
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001137{
Prabhakar Lad4bb12312014-10-12 17:40:37 -03001138 struct vpbe_layer *layer = video_drvdata(file);
1139 struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001140
Hans Verkuil36864082012-10-01 11:39:46 -03001141 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_G_DV_TIMINGS\n");
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001142
1143 /* Get the given standard in the encoder */
1144
1145 if (vpbe_dev->current_timings.timings_type &
Hans Verkuilef2d41b2013-02-15 15:06:28 -03001146 VPBE_ENC_DV_TIMINGS) {
Hans Verkuil36864082012-10-01 11:39:46 -03001147 *dv_timings = vpbe_dev->current_timings.dv_timings;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001148 } else {
1149 return -EINVAL;
1150 }
1151
1152 return 0;
1153}
1154
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001155/*
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001156 * vpbe_display_open()
1157 * It creates object of file handle structure and stores it in private_data
1158 * member of filepointer
1159 */
1160static int vpbe_display_open(struct file *file)
1161{
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001162 struct vpbe_layer *layer = video_drvdata(file);
1163 struct vpbe_display *disp_dev = layer->disp_dev;
1164 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
1165 struct osd_state *osd_device = disp_dev->osd_device;
1166 int err;
1167
Prabhakar Lad4bb12312014-10-12 17:40:37 -03001168 /* creating context for file descriptor */
1169 err = v4l2_fh_open(file);
1170 if (err) {
1171 v4l2_err(&vpbe_dev->v4l2_dev, "v4l2_fh_open failed\n");
1172 return err;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001173 }
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001174
Prabhakar Lad4bb12312014-10-12 17:40:37 -03001175 /* leaving if layer is already initialized */
1176 if (!v4l2_fh_is_singular_file(file))
1177 return err;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001178
1179 if (!layer->usrs) {
Hans Verkuilcbc807d2012-06-24 06:16:44 -03001180 if (mutex_lock_interruptible(&layer->opslock))
1181 return -ERESTARTSYS;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001182 /* First claim the layer for this device */
1183 err = osd_device->ops.request_layer(osd_device,
1184 layer->layer_info.id);
Hans Verkuilcbc807d2012-06-24 06:16:44 -03001185 mutex_unlock(&layer->opslock);
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001186 if (err < 0) {
1187 /* Couldn't get layer */
1188 v4l2_err(&vpbe_dev->v4l2_dev,
1189 "Display Manager failed to allocate layer\n");
Prabhakar Lad4bb12312014-10-12 17:40:37 -03001190 v4l2_fh_release(file);
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001191 return -EINVAL;
1192 }
1193 }
1194 /* Increment layer usrs counter */
1195 layer->usrs++;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001196 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1197 "vpbe display device opened successfully\n");
1198 return 0;
1199}
1200
1201/*
1202 * vpbe_display_release()
1203 * This function deletes buffer queue, frees the buffers and the davinci
1204 * display file * handle
1205 */
1206static int vpbe_display_release(struct file *file)
1207{
Prabhakar Lad4bb12312014-10-12 17:40:37 -03001208 struct vpbe_layer *layer = video_drvdata(file);
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001209 struct osd_layer_config *cfg = &layer->layer_info.config;
Prabhakar Lad4bb12312014-10-12 17:40:37 -03001210 struct vpbe_display *disp_dev = layer->disp_dev;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001211 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
1212 struct osd_state *osd_device = disp_dev->osd_device;
1213
1214 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_release\n");
1215
Hans Verkuilcbc807d2012-06-24 06:16:44 -03001216 mutex_lock(&layer->opslock);
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001217
Prabhakar Lad4bb12312014-10-12 17:40:37 -03001218 osd_device->ops.disable_layer(osd_device,
1219 layer->layer_info.id);
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001220 /* Decrement layer usrs counter */
1221 layer->usrs--;
1222 /* If this file handle has initialize encoder device, reset it */
1223 if (!layer->usrs) {
1224 if (cfg->pixfmt == PIXFMT_NV12) {
1225 struct vpbe_layer *otherlayer;
1226 otherlayer =
1227 _vpbe_display_get_other_win_layer(disp_dev, layer);
1228 osd_device->ops.disable_layer(osd_device,
1229 otherlayer->layer_info.id);
1230 osd_device->ops.release_layer(osd_device,
1231 otherlayer->layer_info.id);
1232 }
1233 osd_device->ops.disable_layer(osd_device,
1234 layer->layer_info.id);
1235 osd_device->ops.release_layer(osd_device,
1236 layer->layer_info.id);
1237 }
Lad, Prabhakar3d7543b92014-03-22 07:57:59 -03001238
Prabhakar Lad4bb12312014-10-12 17:40:37 -03001239 _vb2_fop_release(file, NULL);
Hans Verkuilcbc807d2012-06-24 06:16:44 -03001240 mutex_unlock(&layer->opslock);
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001241
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001242 disp_dev->cbcr_ofst = 0;
1243
1244 return 0;
1245}
1246
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001247/* vpbe capture ioctl operations */
1248static const struct v4l2_ioctl_ops vpbe_ioctl_ops = {
1249 .vidioc_querycap = vpbe_display_querycap,
1250 .vidioc_g_fmt_vid_out = vpbe_display_g_fmt,
1251 .vidioc_enum_fmt_vid_out = vpbe_display_enum_fmt,
1252 .vidioc_s_fmt_vid_out = vpbe_display_s_fmt,
1253 .vidioc_try_fmt_vid_out = vpbe_display_try_fmt,
Prabhakar Lad41cf47b2014-10-12 17:40:38 -03001254
1255 .vidioc_reqbufs = vb2_ioctl_reqbufs,
Prabhakar Lad7041bc92014-10-22 18:42:01 -03001256 .vidioc_create_bufs = vb2_ioctl_create_bufs,
Prabhakar Lad41cf47b2014-10-12 17:40:38 -03001257 .vidioc_querybuf = vb2_ioctl_querybuf,
1258 .vidioc_qbuf = vb2_ioctl_qbuf,
1259 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1260 .vidioc_streamon = vb2_ioctl_streamon,
1261 .vidioc_streamoff = vb2_ioctl_streamoff,
Prabhakar Ladc24376f2014-10-12 17:40:41 -03001262 .vidioc_expbuf = vb2_ioctl_expbuf,
Prabhakar Lad41cf47b2014-10-12 17:40:38 -03001263
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001264 .vidioc_cropcap = vpbe_display_cropcap,
Hans Verkuil516aca32016-07-03 07:53:31 -03001265 .vidioc_g_selection = vpbe_display_g_selection,
1266 .vidioc_s_selection = vpbe_display_s_selection,
Prabhakar Lada8afe382014-10-12 17:40:44 -03001267
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001268 .vidioc_s_std = vpbe_display_s_std,
1269 .vidioc_g_std = vpbe_display_g_std,
Prabhakar Lada8afe382014-10-12 17:40:44 -03001270
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001271 .vidioc_enum_output = vpbe_display_enum_output,
1272 .vidioc_s_output = vpbe_display_s_output,
1273 .vidioc_g_output = vpbe_display_g_output,
Prabhakar Lada8afe382014-10-12 17:40:44 -03001274
Hans Verkuil36864082012-10-01 11:39:46 -03001275 .vidioc_s_dv_timings = vpbe_display_s_dv_timings,
1276 .vidioc_g_dv_timings = vpbe_display_g_dv_timings,
1277 .vidioc_enum_dv_timings = vpbe_display_enum_dv_timings,
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001278};
1279
Bhumika Goyal6bcc0512017-06-29 04:51:24 -04001280static const struct v4l2_file_operations vpbe_fops = {
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001281 .owner = THIS_MODULE,
1282 .open = vpbe_display_open,
1283 .release = vpbe_display_release,
1284 .unlocked_ioctl = video_ioctl2,
Prabhakar Lad266c9c22014-10-12 17:40:36 -03001285 .mmap = vb2_fop_mmap,
1286 .poll = vb2_fop_poll,
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001287};
1288
1289static int vpbe_device_get(struct device *dev, void *data)
1290{
1291 struct platform_device *pdev = to_platform_device(dev);
1292 struct vpbe_display *vpbe_disp = data;
1293
1294 if (strcmp("vpbe_controller", pdev->name) == 0)
1295 vpbe_disp->vpbe_dev = platform_get_drvdata(pdev);
1296
Markus Elfring7a6e6c32017-09-07 16:37:16 -04001297 if (strstr(pdev->name, "vpbe-osd"))
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001298 vpbe_disp->osd_device = platform_get_drvdata(pdev);
1299
1300 return 0;
1301}
1302
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001303static int init_vpbe_layer(int i, struct vpbe_display *disp_dev,
1304 struct platform_device *pdev)
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001305{
1306 struct vpbe_layer *vpbe_display_layer = NULL;
1307 struct video_device *vbd = NULL;
1308
1309 /* Allocate memory for four plane display objects */
Markus Elfring7a6e6c32017-09-07 16:37:16 -04001310 disp_dev->dev[i] = kzalloc(sizeof(*disp_dev->dev[i]), GFP_KERNEL);
1311 if (!disp_dev->dev[i])
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001312 return -ENOMEM;
Markus Elfring7a6e6c32017-09-07 16:37:16 -04001313
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001314 spin_lock_init(&disp_dev->dev[i]->irqlock);
1315 mutex_init(&disp_dev->dev[i]->opslock);
1316
1317 /* Get the pointer to the layer object */
1318 vpbe_display_layer = disp_dev->dev[i];
1319 vbd = &vpbe_display_layer->video_dev;
1320 /* Initialize field of video device */
1321 vbd->release = video_device_release_empty;
1322 vbd->fops = &vpbe_fops;
1323 vbd->ioctl_ops = &vpbe_ioctl_ops;
1324 vbd->minor = -1;
1325 vbd->v4l2_dev = &disp_dev->vpbe_dev->v4l2_dev;
1326 vbd->lock = &vpbe_display_layer->opslock;
Hans Verkuil954f3402012-09-05 06:05:50 -03001327 vbd->vfl_dir = VFL_DIR_TX;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001328
1329 if (disp_dev->vpbe_dev->current_timings.timings_type &
Hans Verkuil142b66e2013-02-19 13:33:34 -03001330 VPBE_ENC_STD)
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001331 vbd->tvnorms = (V4L2_STD_525_60 | V4L2_STD_625_50);
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001332
1333 snprintf(vbd->name, sizeof(vbd->name),
1334 "DaVinci_VPBE Display_DRIVER_V%d.%d.%d",
1335 (VPBE_DISPLAY_VERSION_CODE >> 16) & 0xff,
1336 (VPBE_DISPLAY_VERSION_CODE >> 8) & 0xff,
1337 (VPBE_DISPLAY_VERSION_CODE) & 0xff);
1338
1339 vpbe_display_layer->device_id = i;
1340
1341 vpbe_display_layer->layer_info.id =
1342 ((i == VPBE_DISPLAY_DEVICE_0) ? WIN_VID0 : WIN_VID1);
1343
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001344
1345 return 0;
1346}
1347
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001348static int register_device(struct vpbe_layer *vpbe_display_layer,
1349 struct vpbe_display *disp_dev,
1350 struct platform_device *pdev)
1351{
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001352 int err;
1353
1354 v4l2_info(&disp_dev->vpbe_dev->v4l2_dev,
1355 "Trying to register VPBE display device.\n");
1356 v4l2_info(&disp_dev->vpbe_dev->v4l2_dev,
1357 "layer=%x,layer->video_dev=%x\n",
1358 (int)vpbe_display_layer,
1359 (int)&vpbe_display_layer->video_dev);
1360
Prabhakar Lad266c9c22014-10-12 17:40:36 -03001361 vpbe_display_layer->video_dev.queue = &vpbe_display_layer->buffer_queue;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001362 err = video_register_device(&vpbe_display_layer->video_dev,
1363 VFL_TYPE_GRABBER,
1364 -1);
1365 if (err)
1366 return -ENODEV;
1367
1368 vpbe_display_layer->disp_dev = disp_dev;
1369 /* set the driver data in platform device */
1370 platform_set_drvdata(pdev, disp_dev);
1371 video_set_drvdata(&vpbe_display_layer->video_dev,
1372 vpbe_display_layer);
1373
1374 return 0;
1375}
1376
1377
1378
1379/*
1380 * vpbe_display_probe()
1381 * This function creates device entries by register itself to the V4L2 driver
1382 * and initializes fields of each layer objects
1383 */
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001384static int vpbe_display_probe(struct platform_device *pdev)
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001385{
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001386 struct vpbe_display *disp_dev;
Prabhakar Lade71a1802014-10-12 17:40:31 -03001387 struct v4l2_device *v4l2_dev;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001388 struct resource *res = NULL;
Prabhakar Lade71a1802014-10-12 17:40:31 -03001389 struct vb2_queue *q;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001390 int k;
1391 int i;
1392 int err;
1393 int irq;
1394
1395 printk(KERN_DEBUG "vpbe_display_probe\n");
1396 /* Allocate memory for vpbe_display */
Markus Elfring7a6e6c32017-09-07 16:37:16 -04001397 disp_dev = devm_kzalloc(&pdev->dev, sizeof(*disp_dev), GFP_KERNEL);
Lad, Prabhakar6b55b452013-07-13 04:50:29 -03001398 if (!disp_dev)
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001399 return -ENOMEM;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001400
1401 spin_lock_init(&disp_dev->dma_queue_lock);
1402 /*
1403 * Scan all the platform devices to find the vpbe
1404 * controller device and get the vpbe_dev object
1405 */
1406 err = bus_for_each_dev(&platform_bus_type, NULL, disp_dev,
1407 vpbe_device_get);
1408 if (err < 0)
1409 return err;
Prabhakar Lade71a1802014-10-12 17:40:31 -03001410
1411 v4l2_dev = &disp_dev->vpbe_dev->v4l2_dev;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001412 /* Initialize the vpbe display controller */
Markus Elfring7a6e6c32017-09-07 16:37:16 -04001413 if (disp_dev->vpbe_dev->ops.initialize) {
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001414 err = disp_dev->vpbe_dev->ops.initialize(&pdev->dev,
1415 disp_dev->vpbe_dev);
1416 if (err) {
Prabhakar Lade71a1802014-10-12 17:40:31 -03001417 v4l2_err(v4l2_dev, "Error initing vpbe\n");
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001418 err = -ENOMEM;
1419 goto probe_out;
1420 }
1421 }
1422
1423 for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
1424 if (init_vpbe_layer(i, disp_dev, pdev)) {
1425 err = -ENODEV;
1426 goto probe_out;
1427 }
1428 }
1429
1430 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1431 if (!res) {
Prabhakar Lade71a1802014-10-12 17:40:31 -03001432 v4l2_err(v4l2_dev, "Unable to get VENC interrupt resource\n");
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001433 err = -ENODEV;
1434 goto probe_out;
1435 }
1436
1437 irq = res->start;
Michael Opdenackerd8c279a2013-09-08 23:30:11 -03001438 err = devm_request_irq(&pdev->dev, irq, venc_isr, 0,
Lad, Prabhakar6b55b452013-07-13 04:50:29 -03001439 VPBE_DISPLAY_DRIVER, disp_dev);
1440 if (err) {
Prabhakar Lade71a1802014-10-12 17:40:31 -03001441 v4l2_err(v4l2_dev, "VPBE IRQ request failed\n");
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001442 goto probe_out;
1443 }
1444
1445 for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
Prabhakar Lade71a1802014-10-12 17:40:31 -03001446 /* initialize vb2 queue */
1447 q = &disp_dev->dev[i]->buffer_queue;
1448 memset(q, 0, sizeof(*q));
1449 q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
Prabhakar Lad01118c92014-10-12 17:40:39 -03001450 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
Prabhakar Lade71a1802014-10-12 17:40:31 -03001451 q->drv_priv = disp_dev->dev[i];
1452 q->ops = &video_qops;
1453 q->mem_ops = &vb2_dma_contig_memops;
1454 q->buf_struct_size = sizeof(struct vpbe_disp_buffer);
1455 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1456 q->min_buffers_needed = 1;
Prabhakar Lad488735b2014-10-12 17:40:33 -03001457 q->lock = &disp_dev->dev[i]->opslock;
Hans Verkuil53ddcc62016-02-15 13:09:10 -02001458 q->dev = disp_dev->vpbe_dev->pdev;
Prabhakar Lade71a1802014-10-12 17:40:31 -03001459 err = vb2_queue_init(q);
1460 if (err) {
1461 v4l2_err(v4l2_dev, "vb2_queue_init() failed\n");
1462 goto probe_out;
1463 }
1464
Prabhakar Lade71a1802014-10-12 17:40:31 -03001465 INIT_LIST_HEAD(&disp_dev->dev[i]->dma_queue);
1466
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001467 if (register_device(disp_dev->dev[i], disp_dev, pdev)) {
1468 err = -ENODEV;
Lad, Prabhakar6b55b452013-07-13 04:50:29 -03001469 goto probe_out;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001470 }
1471 }
1472
Prabhakar Lade71a1802014-10-12 17:40:31 -03001473 v4l2_dbg(1, debug, v4l2_dev,
1474 "Successfully completed the probing of vpbe v4l2 device\n");
1475
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001476 return 0;
1477
Julia Lawall49a05132011-10-28 19:58:17 -03001478probe_out:
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001479 for (k = 0; k < VPBE_DISPLAY_MAX_DEVICES; k++) {
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001480 /* Unregister video device */
Markus Elfring7a6e6c32017-09-07 16:37:16 -04001481 if (disp_dev->dev[k]) {
Prabhakar Lade71a1802014-10-12 17:40:31 -03001482 video_unregister_device(&disp_dev->dev[k]->video_dev);
1483 kfree(disp_dev->dev[k]);
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001484 }
1485 }
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001486 return err;
1487}
1488
1489/*
1490 * vpbe_display_remove()
1491 * It un-register hardware layer from V4L2 driver
1492 */
1493static int vpbe_display_remove(struct platform_device *pdev)
1494{
1495 struct vpbe_layer *vpbe_display_layer;
1496 struct vpbe_display *disp_dev = platform_get_drvdata(pdev);
1497 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001498 int i;
1499
1500 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_remove\n");
1501
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001502 /* deinitialize the vpbe display controller */
Markus Elfring7a6e6c32017-09-07 16:37:16 -04001503 if (vpbe_dev->ops.deinitialize)
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001504 vpbe_dev->ops.deinitialize(&pdev->dev, vpbe_dev);
1505 /* un-register device */
1506 for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
1507 /* Get the pointer to the layer object */
1508 vpbe_display_layer = disp_dev->dev[i];
1509 /* Unregister video device */
1510 video_unregister_device(&vpbe_display_layer->video_dev);
1511
1512 }
1513 for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
1514 kfree(disp_dev->dev[i]);
1515 disp_dev->dev[i] = NULL;
1516 }
1517
1518 return 0;
1519}
1520
1521static struct platform_driver vpbe_display_driver = {
1522 .driver = {
1523 .name = VPBE_DISPLAY_DRIVER,
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001524 .bus = &platform_bus_type,
1525 },
1526 .probe = vpbe_display_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001527 .remove = vpbe_display_remove,
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001528};
1529
Axel Lin1d6629b2012-01-10 03:21:49 -03001530module_platform_driver(vpbe_display_driver);
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001531
1532MODULE_DESCRIPTION("TI DM644x/DM355/DM365 VPBE Display controller");
1533MODULE_LICENSE("GPL");
1534MODULE_AUTHOR("Texas Instruments");