]> git.ipfire.org Git - thirdparty/kernel/stable.git/blame - drivers/media/platform/davinci/vpif_display.c
[media] media: vpif: use a configurable i2c_adapter_id for vpif display
[thirdparty/kernel/stable.git] / drivers / media / platform / davinci / vpif_display.c
CommitLineData
e7332e3a
C
1/*
2 * vpif-display - VPIF display driver
3 * Display driver for TI DaVinci VPIF
4 *
5 * Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/
96787eb4 6 * Copyright (C) 2014 Lad, Prabhakar <prabhakar.csengg@gmail.com>
e7332e3a
C
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation version 2.
11 *
12 * This program is distributed .as is. WITHOUT ANY WARRANTY of any
13 * kind, whether express or implied; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 */
17
e7332e3a 18#include <linux/interrupt.h>
012eef70 19#include <linux/module.h>
e7332e3a 20#include <linux/platform_device.h>
5a0e3ad6 21#include <linux/slab.h>
e7332e3a 22
012eef70 23#include <media/v4l2-ioctl.h>
e7332e3a 24
e7332e3a 25#include "vpif.h"
012eef70 26#include "vpif_display.h"
e7332e3a
C
27
28MODULE_DESCRIPTION("TI DaVinci VPIF Display driver");
29MODULE_LICENSE("GPL");
64dc3c1a 30MODULE_VERSION(VPIF_DISPLAY_VERSION);
e7332e3a 31
0a63172a 32#define VPIF_V4L2_STD (V4L2_STD_525_60 | V4L2_STD_625_50)
e7332e3a
C
33
34#define vpif_err(fmt, arg...) v4l2_err(&vpif_obj.v4l2_dev, fmt, ## arg)
35#define vpif_dbg(level, debug, fmt, arg...) \
36 v4l2_dbg(level, debug, &vpif_obj.v4l2_dev, fmt, ## arg)
37
38static int debug = 1;
e7332e3a
C
39
40module_param(debug, int, 0644);
e7332e3a
C
41
42MODULE_PARM_DESC(debug, "Debug level 0-1");
e7332e3a 43
76c4c2be 44#define VPIF_DRIVER_NAME "vpif_display"
bff782d7 45MODULE_ALIAS("platform:" VPIF_DRIVER_NAME);
2f5851b5
LP
46
47/* Is set to 1 in case of SDTV formats, 2 in case of HDTV formats. */
48static int ycmux_mode;
49
58334b00
LP
50static u8 channel_first_int[VPIF_NUMOBJECTS][2] = { {1, 1} };
51
e7332e3a
C
52static struct vpif_device vpif_obj = { {NULL} };
53static struct device *vpif_dev;
2401dd25
LP
54static void vpif_calculate_offsets(struct channel_obj *ch);
55static void vpif_config_addr(struct channel_obj *ch, int muxmode);
e7332e3a 56
2d700715
JS
57static inline
58struct vpif_disp_buffer *to_vpif_buffer(struct vb2_v4l2_buffer *vb)
58334b00
LP
59{
60 return container_of(vb, struct vpif_disp_buffer, vb);
61}
62
27cdf9ba
LP
63/**
64 * vpif_buffer_prepare : callback function for buffer prepare
65 * @vb: ptr to vb2_buffer
66 *
67 * This is the callback function for buffer prepare when vb2_qbuf()
68 * function is called. The buffer is prepared and user space virtual address
69 * or user address is converted into physical address
e7332e3a 70 */
2401dd25 71static int vpif_buffer_prepare(struct vb2_buffer *vb)
e7332e3a 72{
2d700715 73 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
27cdf9ba 74 struct channel_obj *ch = vb2_get_drv_priv(vb->vb2_queue);
e7332e3a 75 struct common_obj *common;
e7332e3a 76
a2b235cb 77 common = &ch->common[VPIF_VIDEO_INDEX];
27cdf9ba
LP
78
79 vb2_set_plane_payload(vb, 0, common->fmt.fmt.pix.sizeimage);
80 if (vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
81 return -EINVAL;
82
2d700715 83 vbuf->field = common->fmt.fmt.pix.field;
27cdf9ba
LP
84
85 if (vb->vb2_queue->type != V4L2_BUF_TYPE_SLICED_VBI_OUTPUT) {
86 unsigned long addr = vb2_dma_contig_plane_dma_addr(vb, 0);
87
88 if (!ISALIGNED(addr + common->ytop_off) ||
2401dd25
LP
89 !ISALIGNED(addr + common->ybtm_off) ||
90 !ISALIGNED(addr + common->ctop_off) ||
27cdf9ba
LP
91 !ISALIGNED(addr + common->cbtm_off)) {
92 vpif_err("buffer offset not aligned to 8 bytes\n");
93 return -EINVAL;
2401dd25 94 }
e7332e3a 95 }
e7332e3a 96
27cdf9ba 97 return 0;
e7332e3a
C
98}
99
172392a1
LP
100/**
101 * vpif_buffer_queue_setup : Callback function for buffer setup.
102 * @vq: vb2_queue ptr
172392a1
LP
103 * @nbuffers: ptr to number of buffers requested by application
104 * @nplanes:: contains number of distinct video planes needed to hold a frame
105 * @sizes[]: contains the size (in bytes) of each plane.
36c0f8b3 106 * @alloc_devs: ptr to allocation context
172392a1
LP
107 *
108 * This callback function is called when reqbuf() is called to adjust
109 * the buffer count and buffer size
e7332e3a 110 */
2401dd25 111static int vpif_buffer_queue_setup(struct vb2_queue *vq,
2401dd25 112 unsigned int *nbuffers, unsigned int *nplanes,
36c0f8b3 113 unsigned int sizes[], struct device *alloc_devs[])
e7332e3a 114{
a2b235cb 115 struct channel_obj *ch = vb2_get_drv_priv(vq);
e7332e3a 116 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
df9ecb0c 117 unsigned size = common->fmt.fmt.pix.sizeimage;
fc613d44 118
df9ecb0c
HV
119 if (*nplanes) {
120 if (sizes[0] < size)
121 return -EINVAL;
122 size = sizes[0];
123 }
172392a1
LP
124
125 if (vq->num_buffers + *nbuffers < 3)
126 *nbuffers = 3 - vq->num_buffers;
e7332e3a 127
2401dd25 128 *nplanes = 1;
df9ecb0c 129 sizes[0] = size;
172392a1
LP
130
131 /* Calculate the offset for Y and C data in the buffer */
132 vpif_calculate_offsets(ch);
133
e7332e3a
C
134 return 0;
135}
136
58334b00
LP
137/**
138 * vpif_buffer_queue : Callback function to add buffer to DMA queue
139 * @vb: ptr to vb2_buffer
140 *
141 * This callback fucntion queues the buffer to DMA engine
e7332e3a 142 */
2401dd25 143static void vpif_buffer_queue(struct vb2_buffer *vb)
e7332e3a 144{
2d700715
JS
145 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
146 struct vpif_disp_buffer *buf = to_vpif_buffer(vbuf);
a2b235cb 147 struct channel_obj *ch = vb2_get_drv_priv(vb->vb2_queue);
e7332e3a 148 struct common_obj *common;
c4697d7f 149 unsigned long flags;
e7332e3a 150
2401dd25 151 common = &ch->common[VPIF_VIDEO_INDEX];
e7332e3a
C
152
153 /* add the buffer to the DMA queue */
c4697d7f 154 spin_lock_irqsave(&common->irqlock, flags);
2401dd25 155 list_add_tail(&buf->list, &common->dma_queue);
c4697d7f 156 spin_unlock_irqrestore(&common->irqlock, flags);
e7332e3a
C
157}
158
58334b00
LP
159/**
160 * vpif_start_streaming : Starts the DMA engine for streaming
161 * @vb: ptr to vb2_buffer
162 * @count: number of buffers
163 */
2401dd25
LP
164static int vpif_start_streaming(struct vb2_queue *vq, unsigned int count)
165{
166 struct vpif_display_config *vpif_config_data =
167 vpif_dev->platform_data;
a2b235cb 168 struct channel_obj *ch = vb2_get_drv_priv(vq);
2401dd25
LP
169 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
170 struct vpif_params *vpif = &ch->vpifparams;
54a445a4
LP
171 struct vpif_disp_buffer *buf, *tmp;
172 unsigned long addr, flags;
2401dd25
LP
173 int ret;
174
c4697d7f 175 spin_lock_irqsave(&common->irqlock, flags);
2401dd25 176
2f5851b5 177 /* Initialize field_id */
2401dd25 178 ch->field_id = 0;
54a445a4 179
2401dd25 180 /* clock settings */
f4ad8d74
LP
181 if (vpif_config_data->set_clock) {
182 ret = vpif_config_data->set_clock(ch->vpifparams.std_info.
183 ycmux_mode, ch->vpifparams.std_info.hd_sd);
184 if (ret < 0) {
185 vpif_err("can't set clock\n");
54a445a4 186 goto err;
f4ad8d74 187 }
2401dd25
LP
188 }
189
190 /* set the parameters and addresses */
191 ret = vpif_set_video_params(vpif, ch->channel_id + 2);
192 if (ret < 0)
54a445a4 193 goto err;
2401dd25 194
2f5851b5 195 ycmux_mode = ret;
2401dd25 196 vpif_config_addr(ch, ret);
54a445a4
LP
197 /* Get the next frame from the buffer queue */
198 common->next_frm = common->cur_frm =
199 list_entry(common->dma_queue.next,
200 struct vpif_disp_buffer, list);
201
202 list_del(&common->cur_frm->list);
203 spin_unlock_irqrestore(&common->irqlock, flags);
54a445a4 204
2d700715 205 addr = vb2_dma_contig_plane_dma_addr(&common->cur_frm->vb.vb2_buf, 0);
2401dd25
LP
206 common->set_addr((addr + common->ytop_off),
207 (addr + common->ybtm_off),
208 (addr + common->ctop_off),
209 (addr + common->cbtm_off));
210
58334b00
LP
211 /*
212 * Set interrupt for both the fields in VPIF
213 * Register enable channel in VPIF register
214 */
9e18404a 215 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
2401dd25
LP
216 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
217 channel2_intr_assert();
218 channel2_intr_enable(1);
219 enable_channel2(1);
2bd4e58c 220 if (vpif_config_data->chan_config[VPIF_CHANNEL2_VIDEO].clip_en)
6964b103 221 channel2_clipping_enable(1);
2401dd25
LP
222 }
223
2f5851b5 224 if (VPIF_CHANNEL3_VIDEO == ch->channel_id || ycmux_mode == 2) {
2401dd25
LP
225 channel3_intr_assert();
226 channel3_intr_enable(1);
227 enable_channel3(1);
2bd4e58c 228 if (vpif_config_data->chan_config[VPIF_CHANNEL3_VIDEO].clip_en)
6964b103 229 channel3_clipping_enable(1);
2401dd25 230 }
2401dd25
LP
231
232 return 0;
54a445a4
LP
233
234err:
235 list_for_each_entry_safe(buf, tmp, &common->dma_queue, list) {
236 list_del(&buf->list);
2d700715 237 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
54a445a4 238 }
92491968 239 spin_unlock_irqrestore(&common->irqlock, flags);
54a445a4
LP
240
241 return ret;
2401dd25
LP
242}
243
58334b00
LP
244/**
245 * vpif_stop_streaming : Stop the DMA engine
246 * @vq: ptr to vb2_queue
247 *
248 * This callback stops the DMA engine and any remaining buffers
249 * in the DMA queue are released.
250 */
e37559b2 251static void vpif_stop_streaming(struct vb2_queue *vq)
2401dd25 252{
a2b235cb 253 struct channel_obj *ch = vb2_get_drv_priv(vq);
2401dd25 254 struct common_obj *common;
c4697d7f 255 unsigned long flags;
2401dd25 256
2401dd25
LP
257 common = &ch->common[VPIF_VIDEO_INDEX];
258
18c7adcf
LP
259 /* Disable channel */
260 if (VPIF_CHANNEL2_VIDEO == ch->channel_id) {
261 enable_channel2(0);
262 channel2_intr_enable(0);
263 }
2f5851b5 264 if (VPIF_CHANNEL3_VIDEO == ch->channel_id || ycmux_mode == 2) {
18c7adcf
LP
265 enable_channel3(0);
266 channel3_intr_enable(0);
267 }
18c7adcf 268
2401dd25 269 /* release all active buffers */
c4697d7f 270 spin_lock_irqsave(&common->irqlock, flags);
18c7adcf 271 if (common->cur_frm == common->next_frm) {
2d700715
JS
272 vb2_buffer_done(&common->cur_frm->vb.vb2_buf,
273 VB2_BUF_STATE_ERROR);
18c7adcf 274 } else {
396c88e6 275 if (common->cur_frm)
2d700715 276 vb2_buffer_done(&common->cur_frm->vb.vb2_buf,
18c7adcf 277 VB2_BUF_STATE_ERROR);
396c88e6 278 if (common->next_frm)
2d700715 279 vb2_buffer_done(&common->next_frm->vb.vb2_buf,
18c7adcf
LP
280 VB2_BUF_STATE_ERROR);
281 }
282
2401dd25
LP
283 while (!list_empty(&common->dma_queue)) {
284 common->next_frm = list_entry(common->dma_queue.next,
285 struct vpif_disp_buffer, list);
286 list_del(&common->next_frm->list);
2d700715
JS
287 vb2_buffer_done(&common->next_frm->vb.vb2_buf,
288 VB2_BUF_STATE_ERROR);
2401dd25 289 }
c4697d7f 290 spin_unlock_irqrestore(&common->irqlock, flags);
2401dd25
LP
291}
292
293static struct vb2_ops video_qops = {
294 .queue_setup = vpif_buffer_queue_setup,
d10ed5c1
LP
295 .wait_prepare = vb2_ops_wait_prepare,
296 .wait_finish = vb2_ops_wait_finish,
2401dd25
LP
297 .buf_prepare = vpif_buffer_prepare,
298 .start_streaming = vpif_start_streaming,
299 .stop_streaming = vpif_stop_streaming,
2401dd25
LP
300 .buf_queue = vpif_buffer_queue,
301};
302
e7332e3a
C
303static void process_progressive_mode(struct common_obj *common)
304{
ea0e437c 305 unsigned long addr;
e7332e3a 306
c4697d7f 307 spin_lock(&common->irqlock);
e7332e3a
C
308 /* Get the next buffer from buffer queue */
309 common->next_frm = list_entry(common->dma_queue.next,
2401dd25 310 struct vpif_disp_buffer, list);
e7332e3a 311 /* Remove that buffer from the buffer queue */
2401dd25 312 list_del(&common->next_frm->list);
c4697d7f 313 spin_unlock(&common->irqlock);
e7332e3a
C
314
315 /* Set top and bottom field addrs in VPIF registers */
2d700715 316 addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb.vb2_buf, 0);
e7332e3a
C
317 common->set_addr(addr + common->ytop_off,
318 addr + common->ybtm_off,
319 addr + common->ctop_off,
320 addr + common->cbtm_off);
321}
322
323static void process_interlaced_mode(int fid, struct common_obj *common)
324{
325 /* device field id and local field id are in sync */
326 /* If this is even field */
327 if (0 == fid) {
328 if (common->cur_frm == common->next_frm)
329 return;
330
331 /* one frame is displayed If next frame is
332 * available, release cur_frm and move on */
333 /* Copy frame display time */
d6dd645e 334 common->cur_frm->vb.vb2_buf.timestamp = ktime_get_ns();
e7332e3a 335 /* Change status of the cur_frm */
2d700715
JS
336 vb2_buffer_done(&common->cur_frm->vb.vb2_buf,
337 VB2_BUF_STATE_DONE);
e7332e3a
C
338 /* Make cur_frm pointing to next_frm */
339 common->cur_frm = common->next_frm;
340
341 } else if (1 == fid) { /* odd field */
c4697d7f 342 spin_lock(&common->irqlock);
e7332e3a
C
343 if (list_empty(&common->dma_queue)
344 || (common->cur_frm != common->next_frm)) {
c4697d7f 345 spin_unlock(&common->irqlock);
e7332e3a
C
346 return;
347 }
c4697d7f 348 spin_unlock(&common->irqlock);
e7332e3a
C
349 /* one field is displayed configure the next
350 * frame if it is available else hold on current
351 * frame */
352 /* Get next from the buffer queue */
353 process_progressive_mode(common);
e7332e3a
C
354 }
355}
356
357/*
358 * vpif_channel_isr: It changes status of the displayed buffer, takes next
359 * buffer from the queue and sets its address in VPIF registers
360 */
361static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
362{
363 struct vpif_device *dev = &vpif_obj;
364 struct channel_obj *ch;
365 struct common_obj *common;
e7332e3a 366 int fid = -1, i;
3ce5f660 367 int channel_id;
e7332e3a
C
368
369 channel_id = *(int *)(dev_id);
b1fc4230
MH
370 if (!vpif_intr_status(channel_id + 2))
371 return IRQ_NONE;
372
e7332e3a 373 ch = dev->dev[channel_id];
e7332e3a
C
374 for (i = 0; i < VPIF_NUMOBJECTS; i++) {
375 common = &ch->common[i];
376 /* If streaming is started in this channel */
e7332e3a
C
377
378 if (1 == ch->vpifparams.std_info.frm_fmt) {
c4697d7f
HV
379 spin_lock(&common->irqlock);
380 if (list_empty(&common->dma_queue)) {
381 spin_unlock(&common->irqlock);
e7332e3a 382 continue;
c4697d7f
HV
383 }
384 spin_unlock(&common->irqlock);
e7332e3a
C
385
386 /* Progressive mode */
387 if (!channel_first_int[i][channel_id]) {
388 /* Mark status of the cur_frm to
389 * done and unlock semaphore on it */
d6dd645e
JS
390 common->cur_frm->vb.vb2_buf.timestamp =
391 ktime_get_ns();
2d700715
JS
392 vb2_buffer_done(&common->cur_frm->vb.vb2_buf,
393 VB2_BUF_STATE_DONE);
e7332e3a
C
394 /* Make cur_frm pointing to next_frm */
395 common->cur_frm = common->next_frm;
396 }
397
398 channel_first_int[i][channel_id] = 0;
399 process_progressive_mode(common);
400 } else {
401 /* Interlaced mode */
402 /* If it is first interrupt, ignore it */
403
404 if (channel_first_int[i][channel_id]) {
405 channel_first_int[i][channel_id] = 0;
406 continue;
407 }
408
409 if (0 == i) {
410 ch->field_id ^= 1;
411 /* Get field id from VPIF registers */
412 fid = vpif_channel_getfid(ch->channel_id + 2);
413 /* If fid does not match with stored field id */
414 if (fid != ch->field_id) {
415 /* Make them in sync */
416 if (0 == fid)
417 ch->field_id = fid;
418
419 return IRQ_HANDLED;
420 }
421 }
422 process_interlaced_mode(fid, common);
423 }
424 }
425
426 return IRQ_HANDLED;
427}
428
c027e165 429static int vpif_update_std_info(struct channel_obj *ch)
e7332e3a 430{
e7332e3a
C
431 struct video_obj *vid_ch = &ch->video;
432 struct vpif_params *vpifparams = &ch->vpifparams;
433 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
434 const struct vpif_channel_config_params *config;
435
c027e165 436 int i;
e7332e3a 437
c027e165 438 for (i = 0; i < vpif_ch_params_count; i++) {
ced9b21f 439 config = &vpif_ch_params[i];
40c8bcea
MR
440 if (config->hd_sd == 0) {
441 vpif_dbg(2, debug, "SD format\n");
442 if (config->stdid & vid_ch->stdid) {
443 memcpy(std_info, config, sizeof(*config));
444 break;
445 }
e7332e3a
C
446 }
447 }
448
c027e165
MR
449 if (i == vpif_ch_params_count) {
450 vpif_dbg(1, debug, "Format not found\n");
aa444406 451 return -EINVAL;
c027e165
MR
452 }
453
454 return 0;
455}
456
457static int vpif_update_resolution(struct channel_obj *ch)
458{
459 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
460 struct video_obj *vid_ch = &ch->video;
461 struct vpif_params *vpifparams = &ch->vpifparams;
462 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
463
0598c17b 464 if (!vid_ch->stdid && !vid_ch->dv_timings.bt.height)
c027e165
MR
465 return -EINVAL;
466
0598c17b 467 if (vid_ch->stdid) {
c027e165
MR
468 if (vpif_update_std_info(ch))
469 return -EINVAL;
470 }
e7332e3a 471
9cba6534 472 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
e7332e3a
C
473 common->fmt.fmt.pix.width = std_info->width;
474 common->fmt.fmt.pix.height = std_info->height;
475 vpif_dbg(1, debug, "Pixel details: Width = %d,Height = %d\n",
476 common->fmt.fmt.pix.width, common->fmt.fmt.pix.height);
477
478 /* Set height and width paramateres */
c027e165
MR
479 common->height = std_info->height;
480 common->width = std_info->width;
9cba6534
LP
481 common->fmt.fmt.pix.sizeimage = common->height * common->width * 2;
482
483 if (vid_ch->stdid)
484 common->fmt.fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
485 else
486 common->fmt.fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
487
488 if (ch->vpifparams.std_info.frm_fmt)
489 common->fmt.fmt.pix.field = V4L2_FIELD_NONE;
490 else
491 common->fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
e7332e3a
C
492
493 return 0;
494}
495
496/*
497 * vpif_calculate_offsets: This function calculates buffers offset for Y and C
498 * in the top and bottom field
499 */
500static void vpif_calculate_offsets(struct channel_obj *ch)
501{
502 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
503 struct vpif_params *vpifparams = &ch->vpifparams;
504 enum v4l2_field field = common->fmt.fmt.pix.field;
505 struct video_obj *vid_ch = &ch->video;
a4f20e2f 506 unsigned int hpitch, sizeimage;
e7332e3a
C
507
508 if (V4L2_FIELD_ANY == common->fmt.fmt.pix.field) {
509 if (ch->vpifparams.std_info.frm_fmt)
510 vid_ch->buf_field = V4L2_FIELD_NONE;
511 else
512 vid_ch->buf_field = V4L2_FIELD_INTERLACED;
513 } else {
514 vid_ch->buf_field = common->fmt.fmt.pix.field;
515 }
516
2401dd25 517 sizeimage = common->fmt.fmt.pix.sizeimage;
e7332e3a
C
518
519 hpitch = common->fmt.fmt.pix.bytesperline;
e7332e3a
C
520 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
521 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
522 common->ytop_off = 0;
523 common->ybtm_off = hpitch;
524 common->ctop_off = sizeimage / 2;
525 common->cbtm_off = sizeimage / 2 + hpitch;
526 } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
527 common->ytop_off = 0;
528 common->ybtm_off = sizeimage / 4;
529 common->ctop_off = sizeimage / 2;
530 common->cbtm_off = common->ctop_off + sizeimage / 4;
531 } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
532 common->ybtm_off = 0;
533 common->ytop_off = sizeimage / 4;
534 common->cbtm_off = sizeimage / 2;
535 common->ctop_off = common->cbtm_off + sizeimage / 4;
536 }
537
538 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
539 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
540 vpifparams->video_params.storage_mode = 1;
541 } else {
542 vpifparams->video_params.storage_mode = 0;
543 }
544
545 if (ch->vpifparams.std_info.frm_fmt == 1) {
546 vpifparams->video_params.hpitch =
547 common->fmt.fmt.pix.bytesperline;
548 } else {
549 if ((field == V4L2_FIELD_ANY) ||
550 (field == V4L2_FIELD_INTERLACED))
551 vpifparams->video_params.hpitch =
552 common->fmt.fmt.pix.bytesperline * 2;
553 else
554 vpifparams->video_params.hpitch =
555 common->fmt.fmt.pix.bytesperline;
556 }
557
558 ch->vpifparams.video_params.stdid = ch->vpifparams.std_info.stdid;
559}
560
e7332e3a
C
561static void vpif_config_addr(struct channel_obj *ch, int muxmode)
562{
563 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
564
565 if (VPIF_CHANNEL3_VIDEO == ch->channel_id) {
566 common->set_addr = ch3_set_videobuf_addr;
567 } else {
568 if (2 == muxmode)
569 common->set_addr = ch2_set_videobuf_addr_yc_nmux;
570 else
571 common->set_addr = ch2_set_videobuf_addr;
572 }
573}
574
e7332e3a 575/* functions implementing ioctls */
2c0ddd17
MR
576/**
577 * vpif_querycap() - QUERYCAP handler
578 * @file: file ptr
579 * @priv: file handle
580 * @cap: ptr to v4l2_capability structure
581 */
e7332e3a
C
582static int vpif_querycap(struct file *file, void *priv,
583 struct v4l2_capability *cap)
584{
317b2e2f 585 struct vpif_display_config *config = vpif_dev->platform_data;
e7332e3a 586
626d533f
LP
587 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
588 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
76c4c2be 589 strlcpy(cap->driver, VPIF_DRIVER_NAME, sizeof(cap->driver));
626d533f
LP
590 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
591 dev_name(vpif_dev));
e7332e3a
C
592 strlcpy(cap->card, config->card_name, sizeof(cap->card));
593
594 return 0;
595}
596
597static int vpif_enum_fmt_vid_out(struct file *file, void *priv,
598 struct v4l2_fmtdesc *fmt)
599{
9cba6534 600 if (fmt->index != 0)
e7332e3a 601 return -EINVAL;
e7332e3a
C
602
603 /* Fill in the information about format */
604 fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
605 strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
606 fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
9cba6534 607 fmt->flags = 0;
e7332e3a
C
608 return 0;
609}
610
611static int vpif_g_fmt_vid_out(struct file *file, void *priv,
612 struct v4l2_format *fmt)
613{
fcc22af2
LP
614 struct video_device *vdev = video_devdata(file);
615 struct channel_obj *ch = video_get_drvdata(vdev);
e7332e3a
C
616 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
617
618 /* Check the validity of the buffer type */
619 if (common->fmt.type != fmt->type)
620 return -EINVAL;
621
c027e165 622 if (vpif_update_resolution(ch))
9bfaae24
HV
623 return -EINVAL;
624 *fmt = common->fmt;
625 return 0;
e7332e3a
C
626}
627
9cba6534 628static int vpif_try_fmt_vid_out(struct file *file, void *priv,
e7332e3a
C
629 struct v4l2_format *fmt)
630{
fcc22af2
LP
631 struct video_device *vdev = video_devdata(file);
632 struct channel_obj *ch = video_get_drvdata(vdev);
e7332e3a 633 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
9cba6534 634 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
e7332e3a 635
9cba6534
LP
636 /*
637 * to supress v4l-compliance warnings silently correct
638 * the pixelformat
639 */
640 if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P)
641 pixfmt->pixelformat = common->fmt.fmt.pix.pixelformat;
e7332e3a 642
9cba6534
LP
643 if (vpif_update_resolution(ch))
644 return -EINVAL;
645
646 pixfmt->colorspace = common->fmt.fmt.pix.colorspace;
647 pixfmt->field = common->fmt.fmt.pix.field;
648 pixfmt->bytesperline = common->fmt.fmt.pix.width;
649 pixfmt->width = common->fmt.fmt.pix.width;
650 pixfmt->height = common->fmt.fmt.pix.height;
651 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height * 2;
e7332e3a 652
e7332e3a
C
653 return 0;
654}
655
9cba6534 656static int vpif_s_fmt_vid_out(struct file *file, void *priv,
e7332e3a
C
657 struct v4l2_format *fmt)
658{
fcc22af2
LP
659 struct video_device *vdev = video_devdata(file);
660 struct channel_obj *ch = video_get_drvdata(vdev);
e7332e3a
C
661 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
662 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
9cba6534 663 int ret;
e7332e3a 664
9cba6534
LP
665 if (vb2_is_busy(&common->buffer_queue))
666 return -EBUSY;
e7332e3a 667
9cba6534
LP
668 ret = vpif_try_fmt_vid_out(file, priv, fmt);
669 if (ret)
670 return ret;
671
672 /* store the pix format in the channel object */
673 common->fmt.fmt.pix = *pixfmt;
674
675 /* store the format in the channel object */
676 common->fmt = *fmt;
677 return 0;
e7332e3a
C
678}
679
314527ac 680static int vpif_s_std(struct file *file, void *priv, v4l2_std_id std_id)
e7332e3a 681{
f27d0f45 682 struct vpif_display_config *config = vpif_dev->platform_data;
fcc22af2
LP
683 struct video_device *vdev = video_devdata(file);
684 struct channel_obj *ch = video_get_drvdata(vdev);
e7332e3a 685 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
f27d0f45
LP
686 struct vpif_display_chan_config *chan_cfg;
687 struct v4l2_output output;
688 int ret;
689
396c88e6 690 if (!config->chan_config[ch->channel_id].outputs)
f27d0f45
LP
691 return -ENODATA;
692
693 chan_cfg = &config->chan_config[ch->channel_id];
694 output = chan_cfg->outputs[ch->output_idx].output;
695 if (output.capabilities != V4L2_OUT_CAP_STD)
696 return -ENODATA;
e7332e3a 697
2f5851b5
LP
698 if (vb2_is_busy(&common->buffer_queue))
699 return -EBUSY;
700
f27d0f45 701
314527ac 702 if (!(std_id & VPIF_V4L2_STD))
e7332e3a
C
703 return -EINVAL;
704
e7332e3a 705 /* Call encoder subdevice function to set the standard */
314527ac 706 ch->video.stdid = std_id;
0598c17b 707 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
e7332e3a 708 /* Get the information about the standard */
9bfaae24
HV
709 if (vpif_update_resolution(ch))
710 return -EINVAL;
e7332e3a 711
e7332e3a 712 common->fmt.fmt.pix.bytesperline = common->fmt.fmt.pix.width;
e7332e3a
C
713
714 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
314527ac 715 s_std_output, std_id);
e7332e3a
C
716 if (ret < 0) {
717 vpif_err("Failed to set output standard\n");
9bfaae24 718 return ret;
e7332e3a
C
719 }
720
8774bed9 721 ret = v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 1, video,
314527ac 722 s_std, std_id);
e7332e3a
C
723 if (ret < 0)
724 vpif_err("Failed to set standard for sub devices\n");
e7332e3a
C
725 return ret;
726}
727
728static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
729{
f27d0f45 730 struct vpif_display_config *config = vpif_dev->platform_data;
fcc22af2
LP
731 struct video_device *vdev = video_devdata(file);
732 struct channel_obj *ch = video_get_drvdata(vdev);
f27d0f45
LP
733 struct vpif_display_chan_config *chan_cfg;
734 struct v4l2_output output;
735
396c88e6 736 if (!config->chan_config[ch->channel_id].outputs)
f27d0f45
LP
737 return -ENODATA;
738
739 chan_cfg = &config->chan_config[ch->channel_id];
740 output = chan_cfg->outputs[ch->output_idx].output;
741 if (output.capabilities != V4L2_OUT_CAP_STD)
742 return -ENODATA;
e7332e3a
C
743
744 *std = ch->video.stdid;
745 return 0;
746}
747
e7332e3a
C
748static int vpif_enum_output(struct file *file, void *fh,
749 struct v4l2_output *output)
750{
751
317b2e2f 752 struct vpif_display_config *config = vpif_dev->platform_data;
fcc22af2
LP
753 struct video_device *vdev = video_devdata(file);
754 struct channel_obj *ch = video_get_drvdata(vdev);
2bd4e58c 755 struct vpif_display_chan_config *chan_cfg;
e7332e3a 756
2bd4e58c
LP
757 chan_cfg = &config->chan_config[ch->channel_id];
758 if (output->index >= chan_cfg->output_count) {
e7332e3a
C
759 vpif_dbg(1, debug, "Invalid output index\n");
760 return -EINVAL;
761 }
762
2bd4e58c
LP
763 *output = chan_cfg->outputs[output->index].output;
764 return 0;
765}
766
767/**
768 * vpif_output_to_subdev() - Maps output to sub device
769 * @vpif_cfg - global config ptr
770 * @chan_cfg - channel config ptr
771 * @index - Given output index from application
772 *
773 * lookup the sub device information for a given output index.
774 * we report all the output to application. output table also
775 * has sub device name for the each output
776 */
777static int
778vpif_output_to_subdev(struct vpif_display_config *vpif_cfg,
779 struct vpif_display_chan_config *chan_cfg, int index)
780{
781 struct vpif_subdev_info *subdev_info;
782 const char *subdev_name;
783 int i;
784
785 vpif_dbg(2, debug, "vpif_output_to_subdev\n");
786
396c88e6 787 if (!chan_cfg->outputs)
2bd4e58c
LP
788 return -1;
789
790 subdev_name = chan_cfg->outputs[index].subdev_name;
396c88e6 791 if (!subdev_name)
2bd4e58c
LP
792 return -1;
793
794 /* loop through the sub device list to get the sub device info */
795 for (i = 0; i < vpif_cfg->subdev_count; i++) {
796 subdev_info = &vpif_cfg->subdevinfo[i];
797 if (!strcmp(subdev_info->name, subdev_name))
798 return i;
799 }
800 return -1;
801}
802
803/**
804 * vpif_set_output() - Select an output
805 * @vpif_cfg - global config ptr
806 * @ch - channel
807 * @index - Given output index from application
808 *
809 * Select the given output.
810 */
811static int vpif_set_output(struct vpif_display_config *vpif_cfg,
812 struct channel_obj *ch, int index)
813{
814 struct vpif_display_chan_config *chan_cfg =
815 &vpif_cfg->chan_config[ch->channel_id];
2bd4e58c
LP
816 struct v4l2_subdev *sd = NULL;
817 u32 input = 0, output = 0;
818 int sd_index;
819 int ret;
820
821 sd_index = vpif_output_to_subdev(vpif_cfg, chan_cfg, index);
a4f20e2f 822 if (sd_index >= 0)
2bd4e58c 823 sd = vpif_obj.sd[sd_index];
e7332e3a 824
2bd4e58c
LP
825 if (sd) {
826 input = chan_cfg->outputs[index].input_route;
827 output = chan_cfg->outputs[index].output_route;
828 ret = v4l2_subdev_call(sd, video, s_routing, input, output, 0);
829 if (ret < 0 && ret != -ENOIOCTLCMD) {
830 vpif_err("Failed to set output\n");
831 return ret;
832 }
833
834 }
835 ch->output_idx = index;
836 ch->sd = sd;
396c88e6 837 if (chan_cfg->outputs)
2bd4e58c 838 /* update tvnorms from the sub device output info */
e933c6bc 839 ch->video_dev.tvnorms = chan_cfg->outputs[index].output.std;
e7332e3a
C
840 return 0;
841}
842
843static int vpif_s_output(struct file *file, void *priv, unsigned int i)
844{
2bd4e58c 845 struct vpif_display_config *config = vpif_dev->platform_data;
fcc22af2
LP
846 struct video_device *vdev = video_devdata(file);
847 struct channel_obj *ch = video_get_drvdata(vdev);
2bd4e58c 848 struct vpif_display_chan_config *chan_cfg;
e7332e3a 849 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
2bd4e58c 850
2f5851b5
LP
851 if (vb2_is_busy(&common->buffer_queue))
852 return -EBUSY;
853
2bd4e58c
LP
854 chan_cfg = &config->chan_config[ch->channel_id];
855
856 if (i >= chan_cfg->output_count)
857 return -EINVAL;
e7332e3a 858
2bd4e58c 859 return vpif_set_output(config, ch, i);
e7332e3a
C
860}
861
862static int vpif_g_output(struct file *file, void *priv, unsigned int *i)
863{
fcc22af2
LP
864 struct video_device *vdev = video_devdata(file);
865 struct channel_obj *ch = video_get_drvdata(vdev);
e7332e3a 866
311673ee 867 *i = ch->output_idx;
e7332e3a
C
868
869 return 0;
870}
871
40c8bcea 872/**
0598c17b 873 * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler
40c8bcea
MR
874 * @file: file ptr
875 * @priv: file handle
0598c17b 876 * @timings: input timings
40c8bcea 877 */
0598c17b
HV
878static int
879vpif_enum_dv_timings(struct file *file, void *priv,
880 struct v4l2_enum_dv_timings *timings)
40c8bcea 881{
1093c590 882 struct vpif_display_config *config = vpif_dev->platform_data;
fcc22af2
LP
883 struct video_device *vdev = video_devdata(file);
884 struct channel_obj *ch = video_get_drvdata(vdev);
1093c590
LP
885 struct vpif_display_chan_config *chan_cfg;
886 struct v4l2_output output;
2bd4e58c 887 int ret;
40c8bcea 888
396c88e6 889 if (!config->chan_config[ch->channel_id].outputs)
1093c590
LP
890 return -ENODATA;
891
892 chan_cfg = &config->chan_config[ch->channel_id];
893 output = chan_cfg->outputs[ch->output_idx].output;
894 if (output.capabilities != V4L2_OUT_CAP_DV_TIMINGS)
895 return -ENODATA;
896
10d2146d
LP
897 timings->pad = 0;
898
899 ret = v4l2_subdev_call(ch->sd, pad, enum_dv_timings, timings);
63af4af5 900 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
2bd4e58c
LP
901 return -EINVAL;
902 return ret;
40c8bcea
MR
903}
904
c027e165
MR
905/**
906 * vpif_s_dv_timings() - S_DV_TIMINGS handler
907 * @file: file ptr
908 * @priv: file handle
909 * @timings: digital video timings
910 */
911static int vpif_s_dv_timings(struct file *file, void *priv,
912 struct v4l2_dv_timings *timings)
913{
1093c590 914 struct vpif_display_config *config = vpif_dev->platform_data;
fcc22af2
LP
915 struct video_device *vdev = video_devdata(file);
916 struct channel_obj *ch = video_get_drvdata(vdev);
c027e165 917 struct vpif_params *vpifparams = &ch->vpifparams;
1093c590 918 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
c027e165
MR
919 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
920 struct video_obj *vid_ch = &ch->video;
0598c17b 921 struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
1093c590
LP
922 struct vpif_display_chan_config *chan_cfg;
923 struct v4l2_output output;
c027e165
MR
924 int ret;
925
396c88e6 926 if (!config->chan_config[ch->channel_id].outputs)
1093c590
LP
927 return -ENODATA;
928
929 chan_cfg = &config->chan_config[ch->channel_id];
930 output = chan_cfg->outputs[ch->output_idx].output;
931 if (output.capabilities != V4L2_OUT_CAP_DV_TIMINGS)
932 return -ENODATA;
933
934 if (vb2_is_busy(&common->buffer_queue))
935 return -EBUSY;
936
c027e165
MR
937 if (timings->type != V4L2_DV_BT_656_1120) {
938 vpif_dbg(2, debug, "Timing type not defined\n");
939 return -EINVAL;
940 }
941
942 /* Configure subdevice timings, if any */
882084ad 943 ret = v4l2_subdev_call(ch->sd, video, s_dv_timings, timings);
2bd4e58c
LP
944 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
945 ret = 0;
946 if (ret < 0) {
c027e165
MR
947 vpif_dbg(2, debug, "Error setting custom DV timings\n");
948 return ret;
949 }
950
951 if (!(timings->bt.width && timings->bt.height &&
952 (timings->bt.hbackporch ||
953 timings->bt.hfrontporch ||
954 timings->bt.hsync) &&
955 timings->bt.vfrontporch &&
956 (timings->bt.vbackporch ||
957 timings->bt.vsync))) {
ded026e0 958 vpif_dbg(2, debug, "Timings for width, height, horizontal back porch, horizontal sync, horizontal front porch, vertical back porch, vertical sync and vertical back porch must be defined\n");
c027e165
MR
959 return -EINVAL;
960 }
961
0598c17b 962 vid_ch->dv_timings = *timings;
c027e165
MR
963
964 /* Configure video port timings */
965
e3655268 966 std_info->eav2sav = V4L2_DV_BT_BLANKING_WIDTH(bt) - 8;
c027e165
MR
967 std_info->sav2eav = bt->width;
968
969 std_info->l1 = 1;
970 std_info->l3 = bt->vsync + bt->vbackporch + 1;
971
e3655268 972 std_info->vsize = V4L2_DV_BT_FRAME_HEIGHT(bt);
c027e165
MR
973 if (bt->interlaced) {
974 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
c027e165
MR
975 std_info->l5 = std_info->vsize/2 -
976 (bt->vfrontporch - 1);
977 std_info->l7 = std_info->vsize/2 + 1;
978 std_info->l9 = std_info->l7 + bt->il_vsync +
979 bt->il_vbackporch + 1;
980 std_info->l11 = std_info->vsize -
981 (bt->il_vfrontporch - 1);
982 } else {
ded026e0 983 vpif_dbg(2, debug, "Required timing values for interlaced BT format missing\n");
c027e165
MR
984 return -EINVAL;
985 }
986 } else {
c027e165
MR
987 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
988 }
989 strncpy(std_info->name, "Custom timings BT656/1120",
990 VPIF_MAX_NAME);
991 std_info->width = bt->width;
992 std_info->height = bt->height;
993 std_info->frm_fmt = bt->interlaced ? 0 : 1;
994 std_info->ycmux_mode = 0;
995 std_info->capture_format = 0;
996 std_info->vbi_supported = 0;
997 std_info->hd_sd = 1;
998 std_info->stdid = 0;
c027e165 999 vid_ch->stdid = 0;
c027e165
MR
1000
1001 return 0;
1002}
1003
1004/**
1005 * vpif_g_dv_timings() - G_DV_TIMINGS handler
1006 * @file: file ptr
1007 * @priv: file handle
1008 * @timings: digital video timings
1009 */
1010static int vpif_g_dv_timings(struct file *file, void *priv,
1011 struct v4l2_dv_timings *timings)
1012{
1093c590 1013 struct vpif_display_config *config = vpif_dev->platform_data;
fcc22af2
LP
1014 struct video_device *vdev = video_devdata(file);
1015 struct channel_obj *ch = video_get_drvdata(vdev);
1093c590 1016 struct vpif_display_chan_config *chan_cfg;
c027e165 1017 struct video_obj *vid_ch = &ch->video;
1093c590
LP
1018 struct v4l2_output output;
1019
396c88e6 1020 if (!config->chan_config[ch->channel_id].outputs)
1093c590
LP
1021 goto error;
1022
1023 chan_cfg = &config->chan_config[ch->channel_id];
1024 output = chan_cfg->outputs[ch->output_idx].output;
1025
1026 if (output.capabilities != V4L2_OUT_CAP_DV_TIMINGS)
1027 goto error;
c027e165 1028
0598c17b 1029 *timings = vid_ch->dv_timings;
c027e165
MR
1030
1031 return 0;
1093c590
LP
1032error:
1033 return -ENODATA;
c027e165 1034}
7036d6a7 1035
7036d6a7
MR
1036/*
1037 * vpif_log_status() - Status information
1038 * @file: file ptr
1039 * @priv: file handle
1040 *
1041 * Returns zero.
1042 */
1043static int vpif_log_status(struct file *filep, void *priv)
1044{
1045 /* status for sub devices */
1046 v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1047
1048 return 0;
1049}
1050
e7332e3a
C
1051/* vpif display ioctl operations */
1052static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
dcce8669 1053 .vidioc_querycap = vpif_querycap,
e7332e3a 1054 .vidioc_enum_fmt_vid_out = vpif_enum_fmt_vid_out,
dcce8669
LP
1055 .vidioc_g_fmt_vid_out = vpif_g_fmt_vid_out,
1056 .vidioc_s_fmt_vid_out = vpif_s_fmt_vid_out,
1057 .vidioc_try_fmt_vid_out = vpif_try_fmt_vid_out,
b92cde17
LP
1058
1059 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1060 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1061 .vidioc_querybuf = vb2_ioctl_querybuf,
1062 .vidioc_qbuf = vb2_ioctl_qbuf,
1063 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1064 .vidioc_expbuf = vb2_ioctl_expbuf,
1065 .vidioc_streamon = vb2_ioctl_streamon,
1066 .vidioc_streamoff = vb2_ioctl_streamoff,
1067
dcce8669 1068 .vidioc_s_std = vpif_s_std,
e7332e3a 1069 .vidioc_g_std = vpif_g_std,
dcce8669 1070
e7332e3a
C
1071 .vidioc_enum_output = vpif_enum_output,
1072 .vidioc_s_output = vpif_s_output,
1073 .vidioc_g_output = vpif_g_output,
dcce8669
LP
1074
1075 .vidioc_enum_dv_timings = vpif_enum_dv_timings,
1076 .vidioc_s_dv_timings = vpif_s_dv_timings,
1077 .vidioc_g_dv_timings = vpif_g_dv_timings,
1078
7036d6a7 1079 .vidioc_log_status = vpif_log_status,
e7332e3a
C
1080};
1081
1082static const struct v4l2_file_operations vpif_fops = {
1083 .owner = THIS_MODULE,
fcc22af2
LP
1084 .open = v4l2_fh_open,
1085 .release = vb2_fop_release,
9bfaae24 1086 .unlocked_ioctl = video_ioctl2,
4be2153c
LP
1087 .mmap = vb2_fop_mmap,
1088 .poll = vb2_fop_poll
e7332e3a
C
1089};
1090
e7332e3a
C
1091/*Configure the channels, buffer sizei, request irq */
1092static int initialize_vpif(void)
1093{
1094 int free_channel_objects_index;
d5b94b99 1095 int err, i, j;
e7332e3a
C
1096
1097 /* Allocate memory for six channel objects */
1098 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1099 vpif_obj.dev[i] =
1f8766b4 1100 kzalloc(sizeof(struct channel_obj), GFP_KERNEL);
e7332e3a
C
1101 /* If memory allocation fails, return error */
1102 if (!vpif_obj.dev[i]) {
1103 free_channel_objects_index = i;
1104 err = -ENOMEM;
1105 goto vpif_init_free_channel_objects;
1106 }
1107 }
1108
e7332e3a
C
1109 return 0;
1110
1111vpif_init_free_channel_objects:
1112 for (j = 0; j < free_channel_objects_index; j++)
1113 kfree(vpif_obj.dev[j]);
1114 return err;
1115}
1116
4b8a531e
LP
1117static int vpif_async_bound(struct v4l2_async_notifier *notifier,
1118 struct v4l2_subdev *subdev,
1119 struct v4l2_async_subdev *asd)
1120{
1121 int i;
1122
1123 for (i = 0; i < vpif_obj.config->subdev_count; i++)
1124 if (!strcmp(vpif_obj.config->subdevinfo[i].name,
1125 subdev->name)) {
1126 vpif_obj.sd[i] = subdev;
1127 vpif_obj.sd[i]->grp_id = 1 << i;
1128 return 0;
1129 }
1130
1131 return -EINVAL;
1132}
1133
1134static int vpif_probe_complete(void)
1135{
1136 struct common_obj *common;
4be2153c 1137 struct video_device *vdev;
4b8a531e 1138 struct channel_obj *ch;
a2b235cb 1139 struct vb2_queue *q;
4b8a531e
LP
1140 int j, err, k;
1141
1142 for (j = 0; j < VPIF_DISPLAY_MAX_DEVICES; j++) {
1143 ch = vpif_obj.dev[j];
1144 /* Initialize field of the channel objects */
4b8a531e 1145 for (k = 0; k < VPIF_NUMOBJECTS; k++) {
4b8a531e 1146 common = &ch->common[k];
4b8a531e
LP
1147 spin_lock_init(&common->irqlock);
1148 mutex_init(&common->lock);
4b8a531e
LP
1149 common->set_addr = NULL;
1150 common->ytop_off = 0;
1151 common->ybtm_off = 0;
1152 common->ctop_off = 0;
1153 common->cbtm_off = 0;
1154 common->cur_frm = NULL;
1155 common->next_frm = NULL;
1156 memset(&common->fmt, 0, sizeof(common->fmt));
4b8a531e
LP
1157 }
1158 ch->initialized = 0;
1159 if (vpif_obj.config->subdev_count)
1160 ch->sd = vpif_obj.sd[0];
1161 ch->channel_id = j;
4b8a531e
LP
1162
1163 memset(&ch->vpifparams, 0, sizeof(ch->vpifparams));
1164
4b8a531e
LP
1165 ch->common[VPIF_VIDEO_INDEX].fmt.type =
1166 V4L2_BUF_TYPE_VIDEO_OUTPUT;
4b8a531e
LP
1167
1168 /* select output 0 */
1169 err = vpif_set_output(vpif_obj.config, ch, 0);
1170 if (err)
1171 goto probe_out;
1172
9cba6534
LP
1173 /* set initial format */
1174 ch->video.stdid = V4L2_STD_525_60;
1175 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
1176 vpif_update_resolution(ch);
1177
a2b235cb
LP
1178 /* Initialize vb2 queue */
1179 q = &common->buffer_queue;
1180 q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1181 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1182 q->drv_priv = ch;
1183 q->ops = &video_qops;
1184 q->mem_ops = &vb2_dma_contig_memops;
1185 q->buf_struct_size = sizeof(struct vpif_disp_buffer);
1186 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1187 q->min_buffers_needed = 1;
d10ed5c1 1188 q->lock = &common->lock;
53ddcc68 1189 q->dev = vpif_dev;
a2b235cb
LP
1190 err = vb2_queue_init(q);
1191 if (err) {
1192 vpif_err("vpif_display: vb2_queue_init() failed\n");
1193 goto probe_out;
1194 }
1195
a2b235cb
LP
1196 INIT_LIST_HEAD(&common->dma_queue);
1197
4b8a531e 1198 /* register video device */
212bdba3
MCC
1199 vpif_dbg(1, debug, "channel=%p,channel->video_dev=%p\n",
1200 ch, &ch->video_dev);
4b8a531e 1201
76c4c2be 1202 /* Initialize the video_device structure */
e933c6bc 1203 vdev = &ch->video_dev;
76c4c2be 1204 strlcpy(vdev->name, VPIF_DRIVER_NAME, sizeof(vdev->name));
e933c6bc 1205 vdev->release = video_device_release_empty;
76c4c2be
LP
1206 vdev->fops = &vpif_fops;
1207 vdev->ioctl_ops = &vpif_ioctl_ops;
1208 vdev->v4l2_dev = &vpif_obj.v4l2_dev;
1209 vdev->vfl_dir = VFL_DIR_TX;
4be2153c 1210 vdev->queue = q;
fcc22af2 1211 vdev->lock = &common->lock;
e933c6bc 1212 video_set_drvdata(&ch->video_dev, ch);
4be2153c
LP
1213 err = video_register_device(vdev, VFL_TYPE_GRABBER,
1214 (j ? 3 : 2));
4b8a531e
LP
1215 if (err < 0)
1216 goto probe_out;
1217 }
1218
1219 return 0;
1220
1221probe_out:
1222 for (k = 0; k < j; k++) {
1223 ch = vpif_obj.dev[k];
a2b235cb 1224 common = &ch->common[k];
e933c6bc 1225 video_unregister_device(&ch->video_dev);
4b8a531e
LP
1226 }
1227 return err;
1228}
1229
1230static int vpif_async_complete(struct v4l2_async_notifier *notifier)
1231{
1232 return vpif_probe_complete();
1233}
1234
e7332e3a
C
1235/*
1236 * vpif_probe: This function creates device entries by register itself to the
1237 * V4L2 driver and initializes fields of each channel objects
1238 */
1239static __init int vpif_probe(struct platform_device *pdev)
1240{
317b2e2f 1241 struct vpif_subdev_info *subdevdata;
e7332e3a 1242 struct i2c_adapter *i2c_adap;
e7332e3a
C
1243 struct resource *res;
1244 int subdev_count;
e933c6bc
LP
1245 int res_idx = 0;
1246 int i, err;
e7332e3a 1247
bff782d7
KH
1248 if (!pdev->dev.platform_data) {
1249 dev_warn(&pdev->dev, "Missing platform data. Giving up.\n");
1250 return -EINVAL;
1251 }
1252
e7332e3a 1253 vpif_dev = &pdev->dev;
317b2e2f 1254 err = initialize_vpif();
e7332e3a 1255
317b2e2f
MK
1256 if (err) {
1257 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
1258 return err;
e7332e3a
C
1259 }
1260
e7332e3a
C
1261 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
1262 if (err) {
1263 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
1264 return err;
1265 }
1266
01b1d975 1267 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
379d2cf4 1268 err = devm_request_irq(&pdev->dev, res->start, vpif_channel_isr,
76c4c2be 1269 IRQF_SHARED, VPIF_DRIVER_NAME,
379d2cf4
LP
1270 (void *)(&vpif_obj.dev[res_idx]->
1271 channel_id));
1272 if (err) {
1273 err = -EINVAL;
1274 vpif_err("VPIF IRQ request failed\n");
1275 goto vpif_unregister;
e7332e3a 1276 }
01b1d975 1277 res_idx++;
e7332e3a
C
1278 }
1279
4b8a531e
LP
1280 vpif_obj.config = pdev->dev.platform_data;
1281 subdev_count = vpif_obj.config->subdev_count;
1282 subdevdata = vpif_obj.config->subdevinfo;
d5cf467e 1283 vpif_obj.sd = kcalloc(subdev_count, sizeof(*vpif_obj.sd), GFP_KERNEL);
396c88e6 1284 if (!vpif_obj.sd) {
e6067f8b 1285 err = -ENOMEM;
e933c6bc 1286 goto vpif_unregister;
e6067f8b
HV
1287 }
1288
4b8a531e 1289 if (!vpif_obj.config->asd_sizes) {
a16cb91a 1290 i2c_adap = i2c_get_adapter(vpif_obj.config->i2c_adapter_id);
4b8a531e
LP
1291 for (i = 0; i < subdev_count; i++) {
1292 vpif_obj.sd[i] =
1293 v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
1294 i2c_adap,
1295 &subdevdata[i].
1296 board_info,
1297 NULL);
1298 if (!vpif_obj.sd[i]) {
1299 vpif_err("Error registering v4l2 subdevice\n");
9d3e976b 1300 err = -ENODEV;
4b8a531e
LP
1301 goto probe_subdev_out;
1302 }
e7332e3a 1303
4b8a531e
LP
1304 if (vpif_obj.sd[i])
1305 vpif_obj.sd[i]->grp_id = 1 << i;
1306 }
1307 vpif_probe_complete();
1308 } else {
e8419d08 1309 vpif_obj.notifier.subdevs = vpif_obj.config->asd;
4b8a531e
LP
1310 vpif_obj.notifier.num_subdevs = vpif_obj.config->asd_sizes[0];
1311 vpif_obj.notifier.bound = vpif_async_bound;
1312 vpif_obj.notifier.complete = vpif_async_complete;
1313 err = v4l2_async_notifier_register(&vpif_obj.v4l2_dev,
1314 &vpif_obj.notifier);
1315 if (err) {
1316 vpif_err("Error registering async notifier\n");
1317 err = -EINVAL;
1318 goto probe_subdev_out;
e7332e3a 1319 }
e7332e3a
C
1320 }
1321
1322 return 0;
1323
e6067f8b
HV
1324probe_subdev_out:
1325 kfree(vpif_obj.sd);
9c63e01e 1326vpif_unregister:
e7332e3a 1327 v4l2_device_unregister(&vpif_obj.v4l2_dev);
e7332e3a
C
1328
1329 return err;
1330}
1331
1332/*
1333 * vpif_remove: It un-register channels from V4L2 driver
1334 */
1335static int vpif_remove(struct platform_device *device)
1336{
a2b235cb 1337 struct common_obj *common;
e7332e3a 1338 struct channel_obj *ch;
9c63e01e 1339 int i;
e7332e3a
C
1340
1341 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1342
0b361583 1343 kfree(vpif_obj.sd);
e7332e3a
C
1344 /* un-register device */
1345 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1346 /* Get the pointer to the channel object */
1347 ch = vpif_obj.dev[i];
e9b5872c 1348 common = &ch->common[VPIF_VIDEO_INDEX];
e7332e3a 1349 /* Unregister video device */
e933c6bc 1350 video_unregister_device(&ch->video_dev);
0b361583 1351 kfree(vpif_obj.dev[i]);
e7332e3a
C
1352 }
1353
1354 return 0;
1355}
1356
ba1902e3 1357#ifdef CONFIG_PM_SLEEP
e9530dac
MH
1358static int vpif_suspend(struct device *dev)
1359{
1360 struct common_obj *common;
1361 struct channel_obj *ch;
1362 int i;
1363
1364 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1365 /* Get the pointer to the channel object */
1366 ch = vpif_obj.dev[i];
1367 common = &ch->common[VPIF_VIDEO_INDEX];
ba1902e3 1368
81578924 1369 if (!vb2_start_streaming_called(&common->buffer_queue))
ba1902e3
LP
1370 continue;
1371
e9530dac 1372 mutex_lock(&common->lock);
ba1902e3
LP
1373 /* Disable channel */
1374 if (ch->channel_id == VPIF_CHANNEL2_VIDEO) {
1375 enable_channel2(0);
1376 channel2_intr_enable(0);
1377 }
1378 if (ch->channel_id == VPIF_CHANNEL3_VIDEO ||
1379 ycmux_mode == 2) {
1380 enable_channel3(0);
1381 channel3_intr_enable(0);
e9530dac
MH
1382 }
1383 mutex_unlock(&common->lock);
1384 }
1385
1386 return 0;
1387}
1388
1389static int vpif_resume(struct device *dev)
1390{
1391
1392 struct common_obj *common;
1393 struct channel_obj *ch;
1394 int i;
1395
1396 for (i = 0; i < VPIF_DISPLAY_MAX_DEVICES; i++) {
1397 /* Get the pointer to the channel object */
1398 ch = vpif_obj.dev[i];
1399 common = &ch->common[VPIF_VIDEO_INDEX];
ba1902e3 1400
81578924 1401 if (!vb2_start_streaming_called(&common->buffer_queue))
ba1902e3
LP
1402 continue;
1403
e9530dac 1404 mutex_lock(&common->lock);
ba1902e3
LP
1405 /* Enable channel */
1406 if (ch->channel_id == VPIF_CHANNEL2_VIDEO) {
1407 enable_channel2(1);
1408 channel2_intr_enable(1);
1409 }
1410 if (ch->channel_id == VPIF_CHANNEL3_VIDEO ||
1411 ycmux_mode == 2) {
1412 enable_channel3(1);
1413 channel3_intr_enable(1);
e9530dac
MH
1414 }
1415 mutex_unlock(&common->lock);
1416 }
1417
1418 return 0;
1419}
1420
e9530dac
MH
1421#endif
1422
ba1902e3
LP
1423static SIMPLE_DEV_PM_OPS(vpif_pm_ops, vpif_suspend, vpif_resume);
1424
ffa1b391 1425static __refdata struct platform_driver vpif_driver = {
e7332e3a 1426 .driver = {
76c4c2be 1427 .name = VPIF_DRIVER_NAME,
ba1902e3 1428 .pm = &vpif_pm_ops,
e7332e3a
C
1429 },
1430 .probe = vpif_probe,
1431 .remove = vpif_remove,
1432};
1433
b8d067b6 1434module_platform_driver(vpif_driver);