blob: 9e86b0d36640cd7fbf60691bc173ee4995324615 [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;
Mauro Carvalho Chehab693035d2018-04-06 08:12:51 -040059 int ret, val;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -030060
61 ret = v4l2_subdev_call(vpbe_dev->venc,
62 core,
63 ioctl,
64 VENC_GET_FLD,
65 &val);
66 if (ret < 0) {
67 v4l2_err(&vpbe_dev->v4l2_dev,
68 "Error in getting Field ID 0\n");
Mauro Carvalho Chehab693035d2018-04-06 08:12:51 -040069 return 1;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -030070 }
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
Mauro Carvalho Chehab8c2bb522018-04-06 08:24:27 -0400288 osd_device->ops.disable_layer(osd_device, layer->layer_info.id);
Prabhakar Lad41cf47b2014-10-12 17:40:38 -0300289
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)
Nathan Chancellor41587572018-09-15 02:16:15 -0400524 layer_info->v_exp = V_EXP_6_OVER_5;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300525 } 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));
Mauro Carvalho Chehabc0decac2018-09-10 08:19:14 -0400650 strscpy(cap->card, vpbe_dev->cfg->module_name, sizeof(cap->card));
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300651
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
Hans Verkuil5200ab62018-10-04 17:38:15 -0400762static int vpbe_display_g_pixelaspect(struct file *file, void *priv,
763 int type, struct v4l2_fract *f)
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300764{
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 Verkuil5200ab62018-10-04 17:38:15 -0400770 if (type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
Hans Verkuil516aca32016-07-03 07:53:31 -0300771 return -EINVAL;
772
Hans Verkuil5200ab62018-10-04 17:38:15 -0400773 *f = 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) {
Mauro Carvalho Chehabcc1e6312018-09-10 16:20:42 -0400819 strscpy(fmt->description, "YUV 4:2:2 - UYVY",
820 sizeof(fmt->description));
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300821 fmt->pixelformat = V4L2_PIX_FMT_UYVY;
822 } else {
Mauro Carvalho Chehabcc1e6312018-09-10 16:20:42 -0400823 strscpy(fmt->description, "Y/CbCr 4:2:0",
824 sizeof(fmt->description));
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300825 fmt->pixelformat = V4L2_PIX_FMT_NV12;
826 }
827
828 return 0;
829}
830
831static int vpbe_display_s_fmt(struct file *file, void *priv,
832 struct v4l2_format *fmt)
833{
Prabhakar Lad4bb12312014-10-12 17:40:37 -0300834 struct vpbe_layer *layer = video_drvdata(file);
835 struct vpbe_display *disp_dev = layer->disp_dev;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300836 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
837 struct osd_layer_config *cfg = &layer->layer_info.config;
838 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
839 struct osd_state *osd_device = disp_dev->osd_device;
840 int ret;
841
842 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
843 "VIDIOC_S_FMT, layer id = %d\n",
844 layer->device_id);
845
Prabhakar Lad1b73f032014-10-12 17:40:42 -0300846 if (vb2_is_busy(&layer->buffer_queue))
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300847 return -EBUSY;
Prabhakar Lad1b73f032014-10-12 17:40:42 -0300848
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300849 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != fmt->type) {
850 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "invalid type\n");
851 return -EINVAL;
852 }
853 /* Check for valid pixel format */
854 ret = vpbe_try_format(disp_dev, pixfmt, 1);
855 if (ret)
856 return ret;
857
858 /* YUV420 is requested, check availability of the
859 other video window */
860
861 layer->pix_fmt = *pixfmt;
Lad, Prabhakarbdea0d22013-05-07 01:07:25 -0300862 if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12) {
863 struct vpbe_layer *otherlayer;
864
865 otherlayer = _vpbe_display_get_other_win_layer(disp_dev, layer);
866 /* if other layer is available, only
867 * claim it, do not configure it
868 */
869 ret = osd_device->ops.request_layer(osd_device,
870 otherlayer->layer_info.id);
871 if (ret < 0) {
872 v4l2_err(&vpbe_dev->v4l2_dev,
873 "Display Manager failed to allocate layer\n");
874 return -EBUSY;
875 }
876 }
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300877
878 /* Get osd layer config */
879 osd_device->ops.get_layer_config(osd_device,
880 layer->layer_info.id, cfg);
881 /* Store the pixel format in the layer object */
882 cfg->xsize = pixfmt->width;
883 cfg->ysize = pixfmt->height;
884 cfg->line_length = pixfmt->bytesperline;
885 cfg->ypos = 0;
886 cfg->xpos = 0;
887 cfg->interlaced = vpbe_dev->current_timings.interlaced;
888
889 if (V4L2_PIX_FMT_UYVY == pixfmt->pixelformat)
Lad, Prabhakar849325e2013-05-03 08:39:25 -0300890 cfg->pixfmt = PIXFMT_YCBCRI;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300891
892 /* Change of the default pixel format for both video windows */
893 if (V4L2_PIX_FMT_NV12 == pixfmt->pixelformat) {
894 struct vpbe_layer *otherlayer;
895 cfg->pixfmt = PIXFMT_NV12;
896 otherlayer = _vpbe_display_get_other_win_layer(disp_dev,
897 layer);
898 otherlayer->layer_info.config.pixfmt = PIXFMT_NV12;
899 }
900
901 /* Set the layer config in the osd window */
902 ret = osd_device->ops.set_layer_config(osd_device,
903 layer->layer_info.id, cfg);
904 if (ret < 0) {
905 v4l2_err(&vpbe_dev->v4l2_dev,
906 "Error in S_FMT params:\n");
907 return -EINVAL;
908 }
909
910 /* Readback and fill the local copy of current pix format */
911 osd_device->ops.get_layer_config(osd_device,
912 layer->layer_info.id, cfg);
913
914 return 0;
915}
916
917static int vpbe_display_try_fmt(struct file *file, void *priv,
918 struct v4l2_format *fmt)
919{
Prabhakar Lad4bb12312014-10-12 17:40:37 -0300920 struct vpbe_layer *layer = video_drvdata(file);
921 struct vpbe_display *disp_dev = layer->disp_dev;
922 struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300923 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
924
925 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_TRY_FMT\n");
926
927 if (V4L2_BUF_TYPE_VIDEO_OUTPUT != fmt->type) {
928 v4l2_err(&vpbe_dev->v4l2_dev, "invalid type\n");
929 return -EINVAL;
930 }
931
932 /* Check for valid field format */
933 return vpbe_try_format(disp_dev, pixfmt, 0);
934
935}
936
Mauro Carvalho Chehabe199fa72018-04-05 12:51:00 -0400937/*
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300938 * vpbe_display_s_std - Set the given standard in the encoder
939 *
940 * Sets the standard if supported by the current encoder. Return the status.
941 * 0 - success & -EINVAL on error
942 */
943static int vpbe_display_s_std(struct file *file, void *priv,
Hans Verkuil314527a2013-03-15 06:10:40 -0300944 v4l2_std_id std_id)
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300945{
Prabhakar Lad4bb12312014-10-12 17:40:37 -0300946 struct vpbe_layer *layer = video_drvdata(file);
947 struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300948 int ret;
949
950 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_S_STD\n");
951
Prabhakar Lad1b73f032014-10-12 17:40:42 -0300952 if (vb2_is_busy(&layer->buffer_queue))
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300953 return -EBUSY;
Prabhakar Lad1b73f032014-10-12 17:40:42 -0300954
Markus Elfring7a6e6c32017-09-07 16:37:16 -0400955 if (vpbe_dev->ops.s_std) {
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300956 ret = vpbe_dev->ops.s_std(vpbe_dev, std_id);
957 if (ret) {
958 v4l2_err(&vpbe_dev->v4l2_dev,
959 "Failed to set standard for sub devices\n");
960 return -EINVAL;
961 }
962 } else {
963 return -EINVAL;
964 }
965
966 return 0;
967}
968
Mauro Carvalho Chehabe199fa72018-04-05 12:51:00 -0400969/*
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300970 * vpbe_display_g_std - Get the standard in the current encoder
971 *
972 * Get the standard in the current encoder. Return the status. 0 - success
973 * -EINVAL on error
974 */
975static int vpbe_display_g_std(struct file *file, void *priv,
976 v4l2_std_id *std_id)
977{
Prabhakar Lad4bb12312014-10-12 17:40:37 -0300978 struct vpbe_layer *layer = video_drvdata(file);
979 struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300980
981 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_G_STD\n");
982
983 /* Get the standard from the current encoder */
984 if (vpbe_dev->current_timings.timings_type & VPBE_ENC_STD) {
Hans Verkuil36864082012-10-01 11:39:46 -0300985 *std_id = vpbe_dev->current_timings.std_id;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300986 return 0;
987 }
988
989 return -EINVAL;
990}
991
Mauro Carvalho Chehabe199fa72018-04-05 12:51:00 -0400992/*
Manjunath Hadlia2c25b42011-06-17 04:01:31 -0300993 * vpbe_display_enum_output - enumerate outputs
994 *
995 * Enumerates the outputs available at the vpbe display
996 * returns the status, -EINVAL if end of output list
997 */
998static int vpbe_display_enum_output(struct file *file, void *priv,
999 struct v4l2_output *output)
1000{
Prabhakar Lad4bb12312014-10-12 17:40:37 -03001001 struct vpbe_layer *layer = video_drvdata(file);
1002 struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001003 int ret;
1004
1005 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_ENUM_OUTPUT\n");
1006
1007 /* Enumerate outputs */
Markus Elfring7a6e6c32017-09-07 16:37:16 -04001008 if (!vpbe_dev->ops.enum_outputs)
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001009 return -EINVAL;
1010
1011 ret = vpbe_dev->ops.enum_outputs(vpbe_dev, output);
1012 if (ret) {
1013 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1014 "Failed to enumerate outputs\n");
1015 return -EINVAL;
1016 }
1017
1018 return 0;
1019}
1020
Mauro Carvalho Chehabe199fa72018-04-05 12:51:00 -04001021/*
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001022 * vpbe_display_s_output - Set output to
1023 * the output specified by the index
1024 */
1025static int vpbe_display_s_output(struct file *file, void *priv,
1026 unsigned int i)
1027{
Prabhakar Lad4bb12312014-10-12 17:40:37 -03001028 struct vpbe_layer *layer = video_drvdata(file);
1029 struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001030 int ret;
1031
1032 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_S_OUTPUT\n");
Prabhakar Lad1b73f032014-10-12 17:40:42 -03001033
1034 if (vb2_is_busy(&layer->buffer_queue))
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001035 return -EBUSY;
Prabhakar Lad1b73f032014-10-12 17:40:42 -03001036
Markus Elfring7a6e6c32017-09-07 16:37:16 -04001037 if (!vpbe_dev->ops.set_output)
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001038 return -EINVAL;
1039
1040 ret = vpbe_dev->ops.set_output(vpbe_dev, i);
1041 if (ret) {
1042 v4l2_err(&vpbe_dev->v4l2_dev,
1043 "Failed to set output for sub devices\n");
1044 return -EINVAL;
1045 }
1046
1047 return 0;
1048}
1049
Mauro Carvalho Chehabe199fa72018-04-05 12:51:00 -04001050/*
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001051 * vpbe_display_g_output - Get output from subdevice
1052 * for a given by the index
1053 */
1054static int vpbe_display_g_output(struct file *file, void *priv,
1055 unsigned int *i)
1056{
Prabhakar Lad4bb12312014-10-12 17:40:37 -03001057 struct vpbe_layer *layer = video_drvdata(file);
1058 struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001059
1060 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_G_OUTPUT\n");
1061 /* Get the standard from the current encoder */
1062 *i = vpbe_dev->current_out_index;
1063
1064 return 0;
1065}
1066
Mauro Carvalho Chehabe199fa72018-04-05 12:51:00 -04001067/*
Hans Verkuil36864082012-10-01 11:39:46 -03001068 * vpbe_display_enum_dv_timings - Enumerate the dv timings
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001069 *
Hans Verkuil36864082012-10-01 11:39:46 -03001070 * enum the timings in the current encoder. Return the status. 0 - success
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001071 * -EINVAL on error
1072 */
1073static int
Hans Verkuil36864082012-10-01 11:39:46 -03001074vpbe_display_enum_dv_timings(struct file *file, void *priv,
1075 struct v4l2_enum_dv_timings *timings)
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001076{
Prabhakar Lad4bb12312014-10-12 17:40:37 -03001077 struct vpbe_layer *layer = video_drvdata(file);
1078 struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001079 int ret;
1080
Hans Verkuil36864082012-10-01 11:39:46 -03001081 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_ENUM_DV_TIMINGS\n");
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001082
1083 /* Enumerate outputs */
Markus Elfring7a6e6c32017-09-07 16:37:16 -04001084 if (!vpbe_dev->ops.enum_dv_timings)
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001085 return -EINVAL;
1086
Hans Verkuil36864082012-10-01 11:39:46 -03001087 ret = vpbe_dev->ops.enum_dv_timings(vpbe_dev, timings);
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001088 if (ret) {
1089 v4l2_err(&vpbe_dev->v4l2_dev,
Hans Verkuil36864082012-10-01 11:39:46 -03001090 "Failed to enumerate dv timings info\n");
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001091 return -EINVAL;
1092 }
1093
1094 return 0;
1095}
1096
Mauro Carvalho Chehabe199fa72018-04-05 12:51:00 -04001097/*
Hans Verkuil36864082012-10-01 11:39:46 -03001098 * vpbe_display_s_dv_timings - Set the dv timings
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001099 *
Hans Verkuil36864082012-10-01 11:39:46 -03001100 * Set the timings in the current encoder. Return the status. 0 - success
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001101 * -EINVAL on error
1102 */
1103static int
Hans Verkuil36864082012-10-01 11:39:46 -03001104vpbe_display_s_dv_timings(struct file *file, void *priv,
1105 struct v4l2_dv_timings *timings)
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001106{
Prabhakar Lad4bb12312014-10-12 17:40:37 -03001107 struct vpbe_layer *layer = video_drvdata(file);
1108 struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001109 int ret;
1110
Hans Verkuil36864082012-10-01 11:39:46 -03001111 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_S_DV_TIMINGS\n");
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001112
Prabhakar Lad1b73f032014-10-12 17:40:42 -03001113 if (vb2_is_busy(&layer->buffer_queue))
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001114 return -EBUSY;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001115
1116 /* Set the given standard in the encoder */
Hans Verkuil36864082012-10-01 11:39:46 -03001117 if (!vpbe_dev->ops.s_dv_timings)
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001118 return -EINVAL;
1119
Hans Verkuil36864082012-10-01 11:39:46 -03001120 ret = vpbe_dev->ops.s_dv_timings(vpbe_dev, timings);
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001121 if (ret) {
1122 v4l2_err(&vpbe_dev->v4l2_dev,
Hans Verkuil36864082012-10-01 11:39:46 -03001123 "Failed to set the dv timings info\n");
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001124 return -EINVAL;
1125 }
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001126
1127 return 0;
1128}
1129
Mauro Carvalho Chehabe199fa72018-04-05 12:51:00 -04001130/*
Hans Verkuil36864082012-10-01 11:39:46 -03001131 * vpbe_display_g_dv_timings - Set the dv timings
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001132 *
Hans Verkuil36864082012-10-01 11:39:46 -03001133 * Get the timings in the current encoder. Return the status. 0 - success
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001134 * -EINVAL on error
1135 */
1136static int
Hans Verkuil36864082012-10-01 11:39:46 -03001137vpbe_display_g_dv_timings(struct file *file, void *priv,
1138 struct v4l2_dv_timings *dv_timings)
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001139{
Prabhakar Lad4bb12312014-10-12 17:40:37 -03001140 struct vpbe_layer *layer = video_drvdata(file);
1141 struct vpbe_device *vpbe_dev = layer->disp_dev->vpbe_dev;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001142
Hans Verkuil36864082012-10-01 11:39:46 -03001143 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "VIDIOC_G_DV_TIMINGS\n");
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001144
1145 /* Get the given standard in the encoder */
1146
1147 if (vpbe_dev->current_timings.timings_type &
Hans Verkuilef2d41b2013-02-15 15:06:28 -03001148 VPBE_ENC_DV_TIMINGS) {
Hans Verkuil36864082012-10-01 11:39:46 -03001149 *dv_timings = vpbe_dev->current_timings.dv_timings;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001150 } else {
1151 return -EINVAL;
1152 }
1153
1154 return 0;
1155}
1156
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001157/*
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001158 * vpbe_display_open()
1159 * It creates object of file handle structure and stores it in private_data
1160 * member of filepointer
1161 */
1162static int vpbe_display_open(struct file *file)
1163{
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001164 struct vpbe_layer *layer = video_drvdata(file);
1165 struct vpbe_display *disp_dev = layer->disp_dev;
1166 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
1167 struct osd_state *osd_device = disp_dev->osd_device;
1168 int err;
1169
Prabhakar Lad4bb12312014-10-12 17:40:37 -03001170 /* creating context for file descriptor */
1171 err = v4l2_fh_open(file);
1172 if (err) {
1173 v4l2_err(&vpbe_dev->v4l2_dev, "v4l2_fh_open failed\n");
1174 return err;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001175 }
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001176
Prabhakar Lad4bb12312014-10-12 17:40:37 -03001177 /* leaving if layer is already initialized */
1178 if (!v4l2_fh_is_singular_file(file))
1179 return err;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001180
1181 if (!layer->usrs) {
Hans Verkuilcbc807d2012-06-24 06:16:44 -03001182 if (mutex_lock_interruptible(&layer->opslock))
1183 return -ERESTARTSYS;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001184 /* First claim the layer for this device */
1185 err = osd_device->ops.request_layer(osd_device,
1186 layer->layer_info.id);
Hans Verkuilcbc807d2012-06-24 06:16:44 -03001187 mutex_unlock(&layer->opslock);
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001188 if (err < 0) {
1189 /* Couldn't get layer */
1190 v4l2_err(&vpbe_dev->v4l2_dev,
1191 "Display Manager failed to allocate layer\n");
Prabhakar Lad4bb12312014-10-12 17:40:37 -03001192 v4l2_fh_release(file);
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001193 return -EINVAL;
1194 }
1195 }
1196 /* Increment layer usrs counter */
1197 layer->usrs++;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001198 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev,
1199 "vpbe display device opened successfully\n");
1200 return 0;
1201}
1202
1203/*
1204 * vpbe_display_release()
1205 * This function deletes buffer queue, frees the buffers and the davinci
1206 * display file * handle
1207 */
1208static int vpbe_display_release(struct file *file)
1209{
Prabhakar Lad4bb12312014-10-12 17:40:37 -03001210 struct vpbe_layer *layer = video_drvdata(file);
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001211 struct osd_layer_config *cfg = &layer->layer_info.config;
Prabhakar Lad4bb12312014-10-12 17:40:37 -03001212 struct vpbe_display *disp_dev = layer->disp_dev;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001213 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
1214 struct osd_state *osd_device = disp_dev->osd_device;
1215
1216 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_release\n");
1217
Hans Verkuilcbc807d2012-06-24 06:16:44 -03001218 mutex_lock(&layer->opslock);
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001219
Prabhakar Lad4bb12312014-10-12 17:40:37 -03001220 osd_device->ops.disable_layer(osd_device,
1221 layer->layer_info.id);
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001222 /* Decrement layer usrs counter */
1223 layer->usrs--;
1224 /* If this file handle has initialize encoder device, reset it */
1225 if (!layer->usrs) {
1226 if (cfg->pixfmt == PIXFMT_NV12) {
1227 struct vpbe_layer *otherlayer;
1228 otherlayer =
1229 _vpbe_display_get_other_win_layer(disp_dev, layer);
1230 osd_device->ops.disable_layer(osd_device,
1231 otherlayer->layer_info.id);
1232 osd_device->ops.release_layer(osd_device,
1233 otherlayer->layer_info.id);
1234 }
1235 osd_device->ops.disable_layer(osd_device,
1236 layer->layer_info.id);
1237 osd_device->ops.release_layer(osd_device,
1238 layer->layer_info.id);
1239 }
Lad, Prabhakar3d7543b92014-03-22 07:57:59 -03001240
Prabhakar Lad4bb12312014-10-12 17:40:37 -03001241 _vb2_fop_release(file, NULL);
Hans Verkuilcbc807d2012-06-24 06:16:44 -03001242 mutex_unlock(&layer->opslock);
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001243
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001244 disp_dev->cbcr_ofst = 0;
1245
1246 return 0;
1247}
1248
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001249/* vpbe capture ioctl operations */
1250static const struct v4l2_ioctl_ops vpbe_ioctl_ops = {
1251 .vidioc_querycap = vpbe_display_querycap,
1252 .vidioc_g_fmt_vid_out = vpbe_display_g_fmt,
1253 .vidioc_enum_fmt_vid_out = vpbe_display_enum_fmt,
1254 .vidioc_s_fmt_vid_out = vpbe_display_s_fmt,
1255 .vidioc_try_fmt_vid_out = vpbe_display_try_fmt,
Prabhakar Lad41cf47b2014-10-12 17:40:38 -03001256
1257 .vidioc_reqbufs = vb2_ioctl_reqbufs,
Prabhakar Lad7041bc92014-10-22 18:42:01 -03001258 .vidioc_create_bufs = vb2_ioctl_create_bufs,
Prabhakar Lad41cf47b2014-10-12 17:40:38 -03001259 .vidioc_querybuf = vb2_ioctl_querybuf,
1260 .vidioc_qbuf = vb2_ioctl_qbuf,
1261 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1262 .vidioc_streamon = vb2_ioctl_streamon,
1263 .vidioc_streamoff = vb2_ioctl_streamoff,
Prabhakar Ladc24376f2014-10-12 17:40:41 -03001264 .vidioc_expbuf = vb2_ioctl_expbuf,
Prabhakar Lad41cf47b2014-10-12 17:40:38 -03001265
Hans Verkuil5200ab62018-10-04 17:38:15 -04001266 .vidioc_g_pixelaspect = vpbe_display_g_pixelaspect,
Hans Verkuil516aca32016-07-03 07:53:31 -03001267 .vidioc_g_selection = vpbe_display_g_selection,
1268 .vidioc_s_selection = vpbe_display_s_selection,
Prabhakar Lada8afe382014-10-12 17:40:44 -03001269
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001270 .vidioc_s_std = vpbe_display_s_std,
1271 .vidioc_g_std = vpbe_display_g_std,
Prabhakar Lada8afe382014-10-12 17:40:44 -03001272
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001273 .vidioc_enum_output = vpbe_display_enum_output,
1274 .vidioc_s_output = vpbe_display_s_output,
1275 .vidioc_g_output = vpbe_display_g_output,
Prabhakar Lada8afe382014-10-12 17:40:44 -03001276
Hans Verkuil36864082012-10-01 11:39:46 -03001277 .vidioc_s_dv_timings = vpbe_display_s_dv_timings,
1278 .vidioc_g_dv_timings = vpbe_display_g_dv_timings,
1279 .vidioc_enum_dv_timings = vpbe_display_enum_dv_timings,
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001280};
1281
Bhumika Goyal6bcc0512017-06-29 04:51:24 -04001282static const struct v4l2_file_operations vpbe_fops = {
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001283 .owner = THIS_MODULE,
1284 .open = vpbe_display_open,
1285 .release = vpbe_display_release,
1286 .unlocked_ioctl = video_ioctl2,
Prabhakar Lad266c9c22014-10-12 17:40:36 -03001287 .mmap = vb2_fop_mmap,
1288 .poll = vb2_fop_poll,
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001289};
1290
1291static int vpbe_device_get(struct device *dev, void *data)
1292{
1293 struct platform_device *pdev = to_platform_device(dev);
1294 struct vpbe_display *vpbe_disp = data;
1295
1296 if (strcmp("vpbe_controller", pdev->name) == 0)
1297 vpbe_disp->vpbe_dev = platform_get_drvdata(pdev);
1298
Markus Elfring7a6e6c32017-09-07 16:37:16 -04001299 if (strstr(pdev->name, "vpbe-osd"))
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001300 vpbe_disp->osd_device = platform_get_drvdata(pdev);
1301
1302 return 0;
1303}
1304
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001305static int init_vpbe_layer(int i, struct vpbe_display *disp_dev,
1306 struct platform_device *pdev)
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001307{
1308 struct vpbe_layer *vpbe_display_layer = NULL;
1309 struct video_device *vbd = NULL;
1310
1311 /* Allocate memory for four plane display objects */
Markus Elfring7a6e6c32017-09-07 16:37:16 -04001312 disp_dev->dev[i] = kzalloc(sizeof(*disp_dev->dev[i]), GFP_KERNEL);
1313 if (!disp_dev->dev[i])
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001314 return -ENOMEM;
Markus Elfring7a6e6c32017-09-07 16:37:16 -04001315
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001316 spin_lock_init(&disp_dev->dev[i]->irqlock);
1317 mutex_init(&disp_dev->dev[i]->opslock);
1318
1319 /* Get the pointer to the layer object */
1320 vpbe_display_layer = disp_dev->dev[i];
1321 vbd = &vpbe_display_layer->video_dev;
1322 /* Initialize field of video device */
1323 vbd->release = video_device_release_empty;
1324 vbd->fops = &vpbe_fops;
1325 vbd->ioctl_ops = &vpbe_ioctl_ops;
1326 vbd->minor = -1;
1327 vbd->v4l2_dev = &disp_dev->vpbe_dev->v4l2_dev;
1328 vbd->lock = &vpbe_display_layer->opslock;
Hans Verkuil954f3402012-09-05 06:05:50 -03001329 vbd->vfl_dir = VFL_DIR_TX;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001330
1331 if (disp_dev->vpbe_dev->current_timings.timings_type &
Hans Verkuil142b66e2013-02-19 13:33:34 -03001332 VPBE_ENC_STD)
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001333 vbd->tvnorms = (V4L2_STD_525_60 | V4L2_STD_625_50);
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001334
1335 snprintf(vbd->name, sizeof(vbd->name),
1336 "DaVinci_VPBE Display_DRIVER_V%d.%d.%d",
1337 (VPBE_DISPLAY_VERSION_CODE >> 16) & 0xff,
1338 (VPBE_DISPLAY_VERSION_CODE >> 8) & 0xff,
1339 (VPBE_DISPLAY_VERSION_CODE) & 0xff);
1340
1341 vpbe_display_layer->device_id = i;
1342
1343 vpbe_display_layer->layer_info.id =
1344 ((i == VPBE_DISPLAY_DEVICE_0) ? WIN_VID0 : WIN_VID1);
1345
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001346
1347 return 0;
1348}
1349
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001350static int register_device(struct vpbe_layer *vpbe_display_layer,
1351 struct vpbe_display *disp_dev,
1352 struct platform_device *pdev)
1353{
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001354 int err;
1355
1356 v4l2_info(&disp_dev->vpbe_dev->v4l2_dev,
1357 "Trying to register VPBE display device.\n");
1358 v4l2_info(&disp_dev->vpbe_dev->v4l2_dev,
Mauro Carvalho Chehab8c2bb522018-04-06 08:24:27 -04001359 "layer=%p,layer->video_dev=%p\n",
1360 vpbe_display_layer,
1361 &vpbe_display_layer->video_dev);
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001362
Prabhakar Lad266c9c22014-10-12 17:40:36 -03001363 vpbe_display_layer->video_dev.queue = &vpbe_display_layer->buffer_queue;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001364 err = video_register_device(&vpbe_display_layer->video_dev,
1365 VFL_TYPE_GRABBER,
1366 -1);
1367 if (err)
1368 return -ENODEV;
1369
1370 vpbe_display_layer->disp_dev = disp_dev;
1371 /* set the driver data in platform device */
1372 platform_set_drvdata(pdev, disp_dev);
1373 video_set_drvdata(&vpbe_display_layer->video_dev,
1374 vpbe_display_layer);
1375
1376 return 0;
1377}
1378
1379
1380
1381/*
1382 * vpbe_display_probe()
1383 * This function creates device entries by register itself to the V4L2 driver
1384 * and initializes fields of each layer objects
1385 */
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001386static int vpbe_display_probe(struct platform_device *pdev)
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001387{
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001388 struct vpbe_display *disp_dev;
Prabhakar Lade71a1802014-10-12 17:40:31 -03001389 struct v4l2_device *v4l2_dev;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001390 struct resource *res = NULL;
Prabhakar Lade71a1802014-10-12 17:40:31 -03001391 struct vb2_queue *q;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001392 int k;
1393 int i;
1394 int err;
1395 int irq;
1396
1397 printk(KERN_DEBUG "vpbe_display_probe\n");
1398 /* Allocate memory for vpbe_display */
Markus Elfring7a6e6c32017-09-07 16:37:16 -04001399 disp_dev = devm_kzalloc(&pdev->dev, sizeof(*disp_dev), GFP_KERNEL);
Lad, Prabhakar6b55b452013-07-13 04:50:29 -03001400 if (!disp_dev)
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001401 return -ENOMEM;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001402
1403 spin_lock_init(&disp_dev->dma_queue_lock);
1404 /*
1405 * Scan all the platform devices to find the vpbe
1406 * controller device and get the vpbe_dev object
1407 */
1408 err = bus_for_each_dev(&platform_bus_type, NULL, disp_dev,
1409 vpbe_device_get);
1410 if (err < 0)
1411 return err;
Prabhakar Lade71a1802014-10-12 17:40:31 -03001412
1413 v4l2_dev = &disp_dev->vpbe_dev->v4l2_dev;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001414 /* Initialize the vpbe display controller */
Markus Elfring7a6e6c32017-09-07 16:37:16 -04001415 if (disp_dev->vpbe_dev->ops.initialize) {
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001416 err = disp_dev->vpbe_dev->ops.initialize(&pdev->dev,
1417 disp_dev->vpbe_dev);
1418 if (err) {
Prabhakar Lade71a1802014-10-12 17:40:31 -03001419 v4l2_err(v4l2_dev, "Error initing vpbe\n");
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001420 err = -ENOMEM;
1421 goto probe_out;
1422 }
1423 }
1424
1425 for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
1426 if (init_vpbe_layer(i, disp_dev, pdev)) {
1427 err = -ENODEV;
1428 goto probe_out;
1429 }
1430 }
1431
1432 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1433 if (!res) {
Prabhakar Lade71a1802014-10-12 17:40:31 -03001434 v4l2_err(v4l2_dev, "Unable to get VENC interrupt resource\n");
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001435 err = -ENODEV;
1436 goto probe_out;
1437 }
1438
1439 irq = res->start;
Michael Opdenackerd8c279a2013-09-08 23:30:11 -03001440 err = devm_request_irq(&pdev->dev, irq, venc_isr, 0,
Lad, Prabhakar6b55b452013-07-13 04:50:29 -03001441 VPBE_DISPLAY_DRIVER, disp_dev);
1442 if (err) {
Prabhakar Lade71a1802014-10-12 17:40:31 -03001443 v4l2_err(v4l2_dev, "VPBE IRQ request failed\n");
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001444 goto probe_out;
1445 }
1446
1447 for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
Prabhakar Lade71a1802014-10-12 17:40:31 -03001448 /* initialize vb2 queue */
1449 q = &disp_dev->dev[i]->buffer_queue;
1450 memset(q, 0, sizeof(*q));
1451 q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
Prabhakar Lad01118c92014-10-12 17:40:39 -03001452 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
Prabhakar Lade71a1802014-10-12 17:40:31 -03001453 q->drv_priv = disp_dev->dev[i];
1454 q->ops = &video_qops;
1455 q->mem_ops = &vb2_dma_contig_memops;
1456 q->buf_struct_size = sizeof(struct vpbe_disp_buffer);
1457 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1458 q->min_buffers_needed = 1;
Prabhakar Lad488735b2014-10-12 17:40:33 -03001459 q->lock = &disp_dev->dev[i]->opslock;
Hans Verkuil53ddcc62016-02-15 13:09:10 -02001460 q->dev = disp_dev->vpbe_dev->pdev;
Prabhakar Lade71a1802014-10-12 17:40:31 -03001461 err = vb2_queue_init(q);
1462 if (err) {
1463 v4l2_err(v4l2_dev, "vb2_queue_init() failed\n");
1464 goto probe_out;
1465 }
1466
Prabhakar Lade71a1802014-10-12 17:40:31 -03001467 INIT_LIST_HEAD(&disp_dev->dev[i]->dma_queue);
1468
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001469 if (register_device(disp_dev->dev[i], disp_dev, pdev)) {
1470 err = -ENODEV;
Lad, Prabhakar6b55b452013-07-13 04:50:29 -03001471 goto probe_out;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001472 }
1473 }
1474
Prabhakar Lade71a1802014-10-12 17:40:31 -03001475 v4l2_dbg(1, debug, v4l2_dev,
1476 "Successfully completed the probing of vpbe v4l2 device\n");
1477
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001478 return 0;
1479
Julia Lawall49a05132011-10-28 19:58:17 -03001480probe_out:
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001481 for (k = 0; k < VPBE_DISPLAY_MAX_DEVICES; k++) {
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001482 /* Unregister video device */
Markus Elfring7a6e6c32017-09-07 16:37:16 -04001483 if (disp_dev->dev[k]) {
Prabhakar Lade71a1802014-10-12 17:40:31 -03001484 video_unregister_device(&disp_dev->dev[k]->video_dev);
1485 kfree(disp_dev->dev[k]);
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001486 }
1487 }
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001488 return err;
1489}
1490
1491/*
1492 * vpbe_display_remove()
1493 * It un-register hardware layer from V4L2 driver
1494 */
1495static int vpbe_display_remove(struct platform_device *pdev)
1496{
1497 struct vpbe_layer *vpbe_display_layer;
1498 struct vpbe_display *disp_dev = platform_get_drvdata(pdev);
1499 struct vpbe_device *vpbe_dev = disp_dev->vpbe_dev;
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001500 int i;
1501
1502 v4l2_dbg(1, debug, &vpbe_dev->v4l2_dev, "vpbe_display_remove\n");
1503
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001504 /* deinitialize the vpbe display controller */
Markus Elfring7a6e6c32017-09-07 16:37:16 -04001505 if (vpbe_dev->ops.deinitialize)
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001506 vpbe_dev->ops.deinitialize(&pdev->dev, vpbe_dev);
1507 /* un-register device */
1508 for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
1509 /* Get the pointer to the layer object */
1510 vpbe_display_layer = disp_dev->dev[i];
1511 /* Unregister video device */
1512 video_unregister_device(&vpbe_display_layer->video_dev);
1513
1514 }
1515 for (i = 0; i < VPBE_DISPLAY_MAX_DEVICES; i++) {
1516 kfree(disp_dev->dev[i]);
1517 disp_dev->dev[i] = NULL;
1518 }
1519
1520 return 0;
1521}
1522
1523static struct platform_driver vpbe_display_driver = {
1524 .driver = {
1525 .name = VPBE_DISPLAY_DRIVER,
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001526 .bus = &platform_bus_type,
1527 },
1528 .probe = vpbe_display_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001529 .remove = vpbe_display_remove,
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001530};
1531
Axel Lin1d6629b2012-01-10 03:21:49 -03001532module_platform_driver(vpbe_display_driver);
Manjunath Hadlia2c25b42011-06-17 04:01:31 -03001533
1534MODULE_DESCRIPTION("TI DM644x/DM355/DM365 VPBE Display controller");
1535MODULE_LICENSE("GPL");
1536MODULE_AUTHOR("Texas Instruments");