]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/media/platform/davinci/vpif_capture.c
[media] videobuf2-core: remove duplicated code
[mirror_ubuntu-bionic-kernel.git] / drivers / media / platform / davinci / vpif_capture.c
1 /*
2 * Copyright (C) 2009 Texas Instruments Inc
3 * Copyright (C) 2014 Lad, Prabhakar <prabhakar.csengg@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * TODO : add support for VBI & HBI data service
20 * add static buffer allocation
21 */
22
23 #include <linux/module.h>
24 #include <linux/interrupt.h>
25 #include <linux/platform_device.h>
26 #include <linux/slab.h>
27
28 #include <media/v4l2-ioctl.h>
29
30 #include "vpif.h"
31 #include "vpif_capture.h"
32
33 MODULE_DESCRIPTION("TI DaVinci VPIF Capture driver");
34 MODULE_LICENSE("GPL");
35 MODULE_VERSION(VPIF_CAPTURE_VERSION);
36
37 #define vpif_err(fmt, arg...) v4l2_err(&vpif_obj.v4l2_dev, fmt, ## arg)
38 #define vpif_dbg(level, debug, fmt, arg...) \
39 v4l2_dbg(level, debug, &vpif_obj.v4l2_dev, fmt, ## arg)
40
41 static int debug = 1;
42 static u32 ch0_numbuffers = 3;
43 static u32 ch1_numbuffers = 3;
44 static u32 ch0_bufsize = 1920 * 1080 * 2;
45 static u32 ch1_bufsize = 720 * 576 * 2;
46
47 module_param(debug, int, 0644);
48 module_param(ch0_numbuffers, uint, S_IRUGO);
49 module_param(ch1_numbuffers, uint, S_IRUGO);
50 module_param(ch0_bufsize, uint, S_IRUGO);
51 module_param(ch1_bufsize, uint, S_IRUGO);
52
53 MODULE_PARM_DESC(debug, "Debug level 0-1");
54 MODULE_PARM_DESC(ch2_numbuffers, "Channel0 buffer count (default:3)");
55 MODULE_PARM_DESC(ch3_numbuffers, "Channel1 buffer count (default:3)");
56 MODULE_PARM_DESC(ch2_bufsize, "Channel0 buffer size (default:1920 x 1080 x 2)");
57 MODULE_PARM_DESC(ch3_bufsize, "Channel1 buffer size (default:720 x 576 x 2)");
58
59 static struct vpif_config_params config_params = {
60 .min_numbuffers = 3,
61 .numbuffers[0] = 3,
62 .numbuffers[1] = 3,
63 .min_bufsize[0] = 720 * 480 * 2,
64 .min_bufsize[1] = 720 * 480 * 2,
65 .channel_bufsize[0] = 1920 * 1080 * 2,
66 .channel_bufsize[1] = 720 * 576 * 2,
67 };
68
69 #define VPIF_DRIVER_NAME "vpif_capture"
70
71 /* global variables */
72 static struct vpif_device vpif_obj = { {NULL} };
73 static struct device *vpif_dev;
74 static void vpif_calculate_offsets(struct channel_obj *ch);
75 static void vpif_config_addr(struct channel_obj *ch, int muxmode);
76
77 static u8 channel_first_int[VPIF_NUMBER_OF_OBJECTS][2] = { {1, 1} };
78
79 /* Is set to 1 in case of SDTV formats, 2 in case of HDTV formats. */
80 static int ycmux_mode;
81
82 static inline struct vpif_cap_buffer *to_vpif_buffer(struct vb2_buffer *vb)
83 {
84 return container_of(vb, struct vpif_cap_buffer, vb);
85 }
86
87 /**
88 * vpif_buffer_prepare : callback function for buffer prepare
89 * @vb: ptr to vb2_buffer
90 *
91 * This is the callback function for buffer prepare when vb2_qbuf()
92 * function is called. The buffer is prepared and user space virtual address
93 * or user address is converted into physical address
94 */
95 static int vpif_buffer_prepare(struct vb2_buffer *vb)
96 {
97 struct vb2_queue *q = vb->vb2_queue;
98 struct channel_obj *ch = vb2_get_drv_priv(q);
99 struct common_obj *common;
100 unsigned long addr;
101
102 vpif_dbg(2, debug, "vpif_buffer_prepare\n");
103
104 common = &ch->common[VPIF_VIDEO_INDEX];
105
106 vb2_set_plane_payload(vb, 0, common->fmt.fmt.pix.sizeimage);
107 if (vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
108 return -EINVAL;
109
110 vb->v4l2_buf.field = common->fmt.fmt.pix.field;
111
112 addr = vb2_dma_contig_plane_dma_addr(vb, 0);
113 if (!IS_ALIGNED((addr + common->ytop_off), 8) ||
114 !IS_ALIGNED((addr + common->ybtm_off), 8) ||
115 !IS_ALIGNED((addr + common->ctop_off), 8) ||
116 !IS_ALIGNED((addr + common->cbtm_off), 8)) {
117 vpif_dbg(1, debug, "offset is not aligned\n");
118 return -EINVAL;
119 }
120
121 return 0;
122 }
123
124 /**
125 * vpif_buffer_queue_setup : Callback function for buffer setup.
126 * @vq: vb2_queue ptr
127 * @fmt: v4l2 format
128 * @nbuffers: ptr to number of buffers requested by application
129 * @nplanes:: contains number of distinct video planes needed to hold a frame
130 * @sizes[]: contains the size (in bytes) of each plane.
131 * @alloc_ctxs: ptr to allocation context
132 *
133 * This callback function is called when reqbuf() is called to adjust
134 * the buffer count and buffer size
135 */
136 static int vpif_buffer_queue_setup(struct vb2_queue *vq,
137 const struct v4l2_format *fmt,
138 unsigned int *nbuffers, unsigned int *nplanes,
139 unsigned int sizes[], void *alloc_ctxs[])
140 {
141 struct channel_obj *ch = vb2_get_drv_priv(vq);
142 struct common_obj *common;
143
144 common = &ch->common[VPIF_VIDEO_INDEX];
145
146 vpif_dbg(2, debug, "vpif_buffer_setup\n");
147
148 if (fmt && fmt->fmt.pix.sizeimage < common->fmt.fmt.pix.sizeimage)
149 return -EINVAL;
150
151 if (vq->num_buffers + *nbuffers < 3)
152 *nbuffers = 3 - vq->num_buffers;
153
154 *nplanes = 1;
155 sizes[0] = fmt ? fmt->fmt.pix.sizeimage : common->fmt.fmt.pix.sizeimage;
156 alloc_ctxs[0] = common->alloc_ctx;
157
158 /* Calculate the offset for Y and C data in the buffer */
159 vpif_calculate_offsets(ch);
160
161 return 0;
162 }
163
164 /**
165 * vpif_buffer_queue : Callback function to add buffer to DMA queue
166 * @vb: ptr to vb2_buffer
167 */
168 static void vpif_buffer_queue(struct vb2_buffer *vb)
169 {
170 struct channel_obj *ch = vb2_get_drv_priv(vb->vb2_queue);
171 struct vpif_cap_buffer *buf = to_vpif_buffer(vb);
172 struct common_obj *common;
173 unsigned long flags;
174
175 common = &ch->common[VPIF_VIDEO_INDEX];
176
177 vpif_dbg(2, debug, "vpif_buffer_queue\n");
178
179 spin_lock_irqsave(&common->irqlock, flags);
180 /* add the buffer to the DMA queue */
181 list_add_tail(&buf->list, &common->dma_queue);
182 spin_unlock_irqrestore(&common->irqlock, flags);
183 }
184
185 /**
186 * vpif_start_streaming : Starts the DMA engine for streaming
187 * @vb: ptr to vb2_buffer
188 * @count: number of buffers
189 */
190 static int vpif_start_streaming(struct vb2_queue *vq, unsigned int count)
191 {
192 struct vpif_capture_config *vpif_config_data =
193 vpif_dev->platform_data;
194 struct channel_obj *ch = vb2_get_drv_priv(vq);
195 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
196 struct vpif_params *vpif = &ch->vpifparams;
197 struct vpif_cap_buffer *buf, *tmp;
198 unsigned long addr, flags;
199 int ret;
200
201 spin_lock_irqsave(&common->irqlock, flags);
202
203 /* Initialize field_id */
204 ch->field_id = 0;
205
206 /* configure 1 or 2 channel mode */
207 if (vpif_config_data->setup_input_channel_mode) {
208 ret = vpif_config_data->
209 setup_input_channel_mode(vpif->std_info.ycmux_mode);
210 if (ret < 0) {
211 vpif_dbg(1, debug, "can't set vpif channel mode\n");
212 goto err;
213 }
214 }
215
216 ret = v4l2_subdev_call(ch->sd, video, s_stream, 1);
217 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
218 vpif_dbg(1, debug, "stream on failed in subdev\n");
219 goto err;
220 }
221
222 /* Call vpif_set_params function to set the parameters and addresses */
223 ret = vpif_set_video_params(vpif, ch->channel_id);
224 if (ret < 0) {
225 vpif_dbg(1, debug, "can't set video params\n");
226 goto err;
227 }
228
229 ycmux_mode = ret;
230 vpif_config_addr(ch, ret);
231
232 /* Get the next frame from the buffer queue */
233 common->cur_frm = common->next_frm = list_entry(common->dma_queue.next,
234 struct vpif_cap_buffer, list);
235 /* Remove buffer from the buffer queue */
236 list_del(&common->cur_frm->list);
237 spin_unlock_irqrestore(&common->irqlock, flags);
238 /* Mark state of the current frame to active */
239 common->cur_frm->vb.state = VB2_BUF_STATE_ACTIVE;
240
241 addr = vb2_dma_contig_plane_dma_addr(&common->cur_frm->vb, 0);
242
243 common->set_addr(addr + common->ytop_off,
244 addr + common->ybtm_off,
245 addr + common->ctop_off,
246 addr + common->cbtm_off);
247
248 /**
249 * Set interrupt for both the fields in VPIF Register enable channel in
250 * VPIF register
251 */
252 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
253 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
254 channel0_intr_assert();
255 channel0_intr_enable(1);
256 enable_channel0(1);
257 }
258 if (VPIF_CHANNEL1_VIDEO == ch->channel_id ||
259 ycmux_mode == 2) {
260 channel1_intr_assert();
261 channel1_intr_enable(1);
262 enable_channel1(1);
263 }
264
265 return 0;
266
267 err:
268 list_for_each_entry_safe(buf, tmp, &common->dma_queue, list) {
269 list_del(&buf->list);
270 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_QUEUED);
271 }
272
273 return ret;
274 }
275
276 /**
277 * vpif_stop_streaming : Stop the DMA engine
278 * @vq: ptr to vb2_queue
279 *
280 * This callback stops the DMA engine and any remaining buffers
281 * in the DMA queue are released.
282 */
283 static void vpif_stop_streaming(struct vb2_queue *vq)
284 {
285 struct channel_obj *ch = vb2_get_drv_priv(vq);
286 struct common_obj *common;
287 unsigned long flags;
288 int ret;
289
290 common = &ch->common[VPIF_VIDEO_INDEX];
291
292 /* Disable channel as per its device type and channel id */
293 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
294 enable_channel0(0);
295 channel0_intr_enable(0);
296 }
297 if (VPIF_CHANNEL1_VIDEO == ch->channel_id ||
298 ycmux_mode == 2) {
299 enable_channel1(0);
300 channel1_intr_enable(0);
301 }
302
303 ycmux_mode = 0;
304
305 ret = v4l2_subdev_call(ch->sd, video, s_stream, 0);
306 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
307 vpif_dbg(1, debug, "stream off failed in subdev\n");
308
309 /* release all active buffers */
310 spin_lock_irqsave(&common->irqlock, flags);
311 if (common->cur_frm == common->next_frm) {
312 vb2_buffer_done(&common->cur_frm->vb, VB2_BUF_STATE_ERROR);
313 } else {
314 if (common->cur_frm != NULL)
315 vb2_buffer_done(&common->cur_frm->vb,
316 VB2_BUF_STATE_ERROR);
317 if (common->next_frm != NULL)
318 vb2_buffer_done(&common->next_frm->vb,
319 VB2_BUF_STATE_ERROR);
320 }
321
322 while (!list_empty(&common->dma_queue)) {
323 common->next_frm = list_entry(common->dma_queue.next,
324 struct vpif_cap_buffer, list);
325 list_del(&common->next_frm->list);
326 vb2_buffer_done(&common->next_frm->vb, VB2_BUF_STATE_ERROR);
327 }
328 spin_unlock_irqrestore(&common->irqlock, flags);
329 }
330
331 static struct vb2_ops video_qops = {
332 .queue_setup = vpif_buffer_queue_setup,
333 .buf_prepare = vpif_buffer_prepare,
334 .start_streaming = vpif_start_streaming,
335 .stop_streaming = vpif_stop_streaming,
336 .buf_queue = vpif_buffer_queue,
337 };
338
339 /**
340 * vpif_process_buffer_complete: process a completed buffer
341 * @common: ptr to common channel object
342 *
343 * This function time stamp the buffer and mark it as DONE. It also
344 * wake up any process waiting on the QUEUE and set the next buffer
345 * as current
346 */
347 static void vpif_process_buffer_complete(struct common_obj *common)
348 {
349 v4l2_get_timestamp(&common->cur_frm->vb.v4l2_buf.timestamp);
350 vb2_buffer_done(&common->cur_frm->vb,
351 VB2_BUF_STATE_DONE);
352 /* Make curFrm pointing to nextFrm */
353 common->cur_frm = common->next_frm;
354 }
355
356 /**
357 * vpif_schedule_next_buffer: set next buffer address for capture
358 * @common : ptr to common channel object
359 *
360 * This function will get next buffer from the dma queue and
361 * set the buffer address in the vpif register for capture.
362 * the buffer is marked active
363 */
364 static void vpif_schedule_next_buffer(struct common_obj *common)
365 {
366 unsigned long addr = 0;
367
368 spin_lock(&common->irqlock);
369 common->next_frm = list_entry(common->dma_queue.next,
370 struct vpif_cap_buffer, list);
371 /* Remove that buffer from the buffer queue */
372 list_del(&common->next_frm->list);
373 spin_unlock(&common->irqlock);
374 common->next_frm->vb.state = VB2_BUF_STATE_ACTIVE;
375 addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb, 0);
376
377 /* Set top and bottom field addresses in VPIF registers */
378 common->set_addr(addr + common->ytop_off,
379 addr + common->ybtm_off,
380 addr + common->ctop_off,
381 addr + common->cbtm_off);
382 }
383
384 /**
385 * vpif_channel_isr : ISR handler for vpif capture
386 * @irq: irq number
387 * @dev_id: dev_id ptr
388 *
389 * It changes status of the captured buffer, takes next buffer from the queue
390 * and sets its address in VPIF registers
391 */
392 static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
393 {
394 struct vpif_device *dev = &vpif_obj;
395 struct common_obj *common;
396 struct channel_obj *ch;
397 enum v4l2_field field;
398 int channel_id = 0;
399 int fid = -1, i;
400
401 channel_id = *(int *)(dev_id);
402 if (!vpif_intr_status(channel_id))
403 return IRQ_NONE;
404
405 ch = dev->dev[channel_id];
406
407 field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
408
409 for (i = 0; i < VPIF_NUMBER_OF_OBJECTS; i++) {
410 common = &ch->common[i];
411 /* skip If streaming is not started in this channel */
412 /* Check the field format */
413 if (1 == ch->vpifparams.std_info.frm_fmt) {
414 /* Progressive mode */
415 spin_lock(&common->irqlock);
416 if (list_empty(&common->dma_queue)) {
417 spin_unlock(&common->irqlock);
418 continue;
419 }
420 spin_unlock(&common->irqlock);
421
422 if (!channel_first_int[i][channel_id])
423 vpif_process_buffer_complete(common);
424
425 channel_first_int[i][channel_id] = 0;
426
427 vpif_schedule_next_buffer(common);
428
429
430 channel_first_int[i][channel_id] = 0;
431 } else {
432 /**
433 * Interlaced mode. If it is first interrupt, ignore
434 * it
435 */
436 if (channel_first_int[i][channel_id]) {
437 channel_first_int[i][channel_id] = 0;
438 continue;
439 }
440 if (0 == i) {
441 ch->field_id ^= 1;
442 /* Get field id from VPIF registers */
443 fid = vpif_channel_getfid(ch->channel_id);
444 if (fid != ch->field_id) {
445 /**
446 * If field id does not match stored
447 * field id, make them in sync
448 */
449 if (0 == fid)
450 ch->field_id = fid;
451 return IRQ_HANDLED;
452 }
453 }
454 /* device field id and local field id are in sync */
455 if (0 == fid) {
456 /* this is even field */
457 if (common->cur_frm == common->next_frm)
458 continue;
459
460 /* mark the current buffer as done */
461 vpif_process_buffer_complete(common);
462 } else if (1 == fid) {
463 /* odd field */
464 spin_lock(&common->irqlock);
465 if (list_empty(&common->dma_queue) ||
466 (common->cur_frm != common->next_frm)) {
467 spin_unlock(&common->irqlock);
468 continue;
469 }
470 spin_unlock(&common->irqlock);
471
472 vpif_schedule_next_buffer(common);
473 }
474 }
475 }
476 return IRQ_HANDLED;
477 }
478
479 /**
480 * vpif_update_std_info() - update standard related info
481 * @ch: ptr to channel object
482 *
483 * For a given standard selected by application, update values
484 * in the device data structures
485 */
486 static int vpif_update_std_info(struct channel_obj *ch)
487 {
488 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
489 struct vpif_params *vpifparams = &ch->vpifparams;
490 const struct vpif_channel_config_params *config;
491 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
492 struct video_obj *vid_ch = &ch->video;
493 int index;
494
495 vpif_dbg(2, debug, "vpif_update_std_info\n");
496
497 for (index = 0; index < vpif_ch_params_count; index++) {
498 config = &vpif_ch_params[index];
499 if (config->hd_sd == 0) {
500 vpif_dbg(2, debug, "SD format\n");
501 if (config->stdid & vid_ch->stdid) {
502 memcpy(std_info, config, sizeof(*config));
503 break;
504 }
505 } else {
506 vpif_dbg(2, debug, "HD format\n");
507 if (!memcmp(&config->dv_timings, &vid_ch->dv_timings,
508 sizeof(vid_ch->dv_timings))) {
509 memcpy(std_info, config, sizeof(*config));
510 break;
511 }
512 }
513 }
514
515 /* standard not found */
516 if (index == vpif_ch_params_count)
517 return -EINVAL;
518
519 common->fmt.fmt.pix.width = std_info->width;
520 common->width = std_info->width;
521 common->fmt.fmt.pix.height = std_info->height;
522 common->height = std_info->height;
523 common->fmt.fmt.pix.bytesperline = std_info->width;
524 vpifparams->video_params.hpitch = std_info->width;
525 vpifparams->video_params.storage_mode = std_info->frm_fmt;
526
527 return 0;
528 }
529
530 /**
531 * vpif_calculate_offsets : This function calculates buffers offsets
532 * @ch : ptr to channel object
533 *
534 * This function calculates buffer offsets for Y and C in the top and
535 * bottom field
536 */
537 static void vpif_calculate_offsets(struct channel_obj *ch)
538 {
539 unsigned int hpitch, vpitch, sizeimage;
540 struct video_obj *vid_ch = &(ch->video);
541 struct vpif_params *vpifparams = &ch->vpifparams;
542 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
543 enum v4l2_field field = common->fmt.fmt.pix.field;
544
545 vpif_dbg(2, debug, "vpif_calculate_offsets\n");
546
547 if (V4L2_FIELD_ANY == field) {
548 if (vpifparams->std_info.frm_fmt)
549 vid_ch->buf_field = V4L2_FIELD_NONE;
550 else
551 vid_ch->buf_field = V4L2_FIELD_INTERLACED;
552 } else
553 vid_ch->buf_field = common->fmt.fmt.pix.field;
554
555 sizeimage = common->fmt.fmt.pix.sizeimage;
556
557 hpitch = common->fmt.fmt.pix.bytesperline;
558 vpitch = sizeimage / (hpitch * 2);
559
560 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
561 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
562 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
563 common->ytop_off = 0;
564 common->ybtm_off = hpitch;
565 common->ctop_off = sizeimage / 2;
566 common->cbtm_off = sizeimage / 2 + hpitch;
567 } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
568 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
569 common->ytop_off = 0;
570 common->ybtm_off = sizeimage / 4;
571 common->ctop_off = sizeimage / 2;
572 common->cbtm_off = common->ctop_off + sizeimage / 4;
573 } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
574 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
575 common->ybtm_off = 0;
576 common->ytop_off = sizeimage / 4;
577 common->cbtm_off = sizeimage / 2;
578 common->ctop_off = common->cbtm_off + sizeimage / 4;
579 }
580 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
581 (V4L2_FIELD_INTERLACED == vid_ch->buf_field))
582 vpifparams->video_params.storage_mode = 1;
583 else
584 vpifparams->video_params.storage_mode = 0;
585
586 if (1 == vpifparams->std_info.frm_fmt)
587 vpifparams->video_params.hpitch =
588 common->fmt.fmt.pix.bytesperline;
589 else {
590 if ((field == V4L2_FIELD_ANY)
591 || (field == V4L2_FIELD_INTERLACED))
592 vpifparams->video_params.hpitch =
593 common->fmt.fmt.pix.bytesperline * 2;
594 else
595 vpifparams->video_params.hpitch =
596 common->fmt.fmt.pix.bytesperline;
597 }
598
599 ch->vpifparams.video_params.stdid = vpifparams->std_info.stdid;
600 }
601
602 /**
603 * vpif_config_format: configure default frame format in the device
604 * ch : ptr to channel object
605 */
606 static void vpif_config_format(struct channel_obj *ch)
607 {
608 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
609
610 vpif_dbg(2, debug, "vpif_config_format\n");
611
612 common->fmt.fmt.pix.field = V4L2_FIELD_ANY;
613 common->fmt.fmt.pix.sizeimage
614 = config_params.channel_bufsize[ch->channel_id];
615
616 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER)
617 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;
618 else
619 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
620 common->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
621 }
622
623 /**
624 * vpif_get_default_field() - Get default field type based on interface
625 * @vpif_params - ptr to vpif params
626 */
627 static inline enum v4l2_field vpif_get_default_field(
628 struct vpif_interface *iface)
629 {
630 return (iface->if_type == VPIF_IF_RAW_BAYER) ? V4L2_FIELD_NONE :
631 V4L2_FIELD_INTERLACED;
632 }
633
634 /**
635 * vpif_check_format() - check given pixel format for compatibility
636 * @ch - channel ptr
637 * @pixfmt - Given pixel format
638 * @update - update the values as per hardware requirement
639 *
640 * Check the application pixel format for S_FMT and update the input
641 * values as per hardware limits for TRY_FMT. The default pixel and
642 * field format is selected based on interface type.
643 */
644 static int vpif_check_format(struct channel_obj *ch,
645 struct v4l2_pix_format *pixfmt,
646 int update)
647 {
648 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
649 struct vpif_params *vpif_params = &ch->vpifparams;
650 enum v4l2_field field = pixfmt->field;
651 u32 sizeimage, hpitch, vpitch;
652 int ret = -EINVAL;
653
654 vpif_dbg(2, debug, "vpif_check_format\n");
655 /**
656 * first check for the pixel format. If if_type is Raw bayer,
657 * only V4L2_PIX_FMT_SBGGR8 format is supported. Otherwise only
658 * V4L2_PIX_FMT_YUV422P is supported
659 */
660 if (vpif_params->iface.if_type == VPIF_IF_RAW_BAYER) {
661 if (pixfmt->pixelformat != V4L2_PIX_FMT_SBGGR8) {
662 if (!update) {
663 vpif_dbg(2, debug, "invalid pix format\n");
664 goto exit;
665 }
666 pixfmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
667 }
668 } else {
669 if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P) {
670 if (!update) {
671 vpif_dbg(2, debug, "invalid pixel format\n");
672 goto exit;
673 }
674 pixfmt->pixelformat = V4L2_PIX_FMT_YUV422P;
675 }
676 }
677
678 if (!(VPIF_VALID_FIELD(field))) {
679 if (!update) {
680 vpif_dbg(2, debug, "invalid field format\n");
681 goto exit;
682 }
683 /**
684 * By default use FIELD_NONE for RAW Bayer capture
685 * and FIELD_INTERLACED for other interfaces
686 */
687 field = vpif_get_default_field(&vpif_params->iface);
688 } else if (field == V4L2_FIELD_ANY)
689 /* unsupported field. Use default */
690 field = vpif_get_default_field(&vpif_params->iface);
691
692 /* validate the hpitch */
693 hpitch = pixfmt->bytesperline;
694 if (hpitch < vpif_params->std_info.width) {
695 if (!update) {
696 vpif_dbg(2, debug, "invalid hpitch\n");
697 goto exit;
698 }
699 hpitch = vpif_params->std_info.width;
700 }
701
702 sizeimage = pixfmt->sizeimage;
703
704 vpitch = sizeimage / (hpitch * 2);
705
706 /* validate the vpitch */
707 if (vpitch < vpif_params->std_info.height) {
708 if (!update) {
709 vpif_dbg(2, debug, "Invalid vpitch\n");
710 goto exit;
711 }
712 vpitch = vpif_params->std_info.height;
713 }
714
715 /* Check for 8 byte alignment */
716 if (!ALIGN(hpitch, 8)) {
717 if (!update) {
718 vpif_dbg(2, debug, "invalid pitch alignment\n");
719 goto exit;
720 }
721 /* adjust to next 8 byte boundary */
722 hpitch = (((hpitch + 7) / 8) * 8);
723 }
724 /* if update is set, modify the bytesperline and sizeimage */
725 if (update) {
726 pixfmt->bytesperline = hpitch;
727 pixfmt->sizeimage = hpitch * vpitch * 2;
728 }
729 /**
730 * Image width and height is always based on current standard width and
731 * height
732 */
733 pixfmt->width = common->fmt.fmt.pix.width;
734 pixfmt->height = common->fmt.fmt.pix.height;
735 return 0;
736 exit:
737 return ret;
738 }
739
740 /**
741 * vpif_config_addr() - function to configure buffer address in vpif
742 * @ch - channel ptr
743 * @muxmode - channel mux mode
744 */
745 static void vpif_config_addr(struct channel_obj *ch, int muxmode)
746 {
747 struct common_obj *common;
748
749 vpif_dbg(2, debug, "vpif_config_addr\n");
750
751 common = &(ch->common[VPIF_VIDEO_INDEX]);
752
753 if (VPIF_CHANNEL1_VIDEO == ch->channel_id)
754 common->set_addr = ch1_set_videobuf_addr;
755 else if (2 == muxmode)
756 common->set_addr = ch0_set_videobuf_addr_yc_nmux;
757 else
758 common->set_addr = ch0_set_videobuf_addr;
759 }
760
761 /**
762 * vpif_input_to_subdev() - Maps input to sub device
763 * @vpif_cfg - global config ptr
764 * @chan_cfg - channel config ptr
765 * @input_index - Given input index from application
766 *
767 * lookup the sub device information for a given input index.
768 * we report all the inputs to application. inputs table also
769 * has sub device name for the each input
770 */
771 static int vpif_input_to_subdev(
772 struct vpif_capture_config *vpif_cfg,
773 struct vpif_capture_chan_config *chan_cfg,
774 int input_index)
775 {
776 struct vpif_subdev_info *subdev_info;
777 const char *subdev_name;
778 int i;
779
780 vpif_dbg(2, debug, "vpif_input_to_subdev\n");
781
782 subdev_name = chan_cfg->inputs[input_index].subdev_name;
783 if (subdev_name == NULL)
784 return -1;
785
786 /* loop through the sub device list to get the sub device info */
787 for (i = 0; i < vpif_cfg->subdev_count; i++) {
788 subdev_info = &vpif_cfg->subdev_info[i];
789 if (!strcmp(subdev_info->name, subdev_name))
790 return i;
791 }
792 return -1;
793 }
794
795 /**
796 * vpif_set_input() - Select an input
797 * @vpif_cfg - global config ptr
798 * @ch - channel
799 * @_index - Given input index from application
800 *
801 * Select the given input.
802 */
803 static int vpif_set_input(
804 struct vpif_capture_config *vpif_cfg,
805 struct channel_obj *ch,
806 int index)
807 {
808 struct vpif_capture_chan_config *chan_cfg =
809 &vpif_cfg->chan_config[ch->channel_id];
810 struct vpif_subdev_info *subdev_info = NULL;
811 struct v4l2_subdev *sd = NULL;
812 u32 input = 0, output = 0;
813 int sd_index;
814 int ret;
815
816 sd_index = vpif_input_to_subdev(vpif_cfg, chan_cfg, index);
817 if (sd_index >= 0) {
818 sd = vpif_obj.sd[sd_index];
819 subdev_info = &vpif_cfg->subdev_info[sd_index];
820 }
821
822 /* first setup input path from sub device to vpif */
823 if (sd && vpif_cfg->setup_input_path) {
824 ret = vpif_cfg->setup_input_path(ch->channel_id,
825 subdev_info->name);
826 if (ret < 0) {
827 vpif_dbg(1, debug, "couldn't setup input path for the" \
828 " sub device %s, for input index %d\n",
829 subdev_info->name, index);
830 return ret;
831 }
832 }
833
834 if (sd) {
835 input = chan_cfg->inputs[index].input_route;
836 output = chan_cfg->inputs[index].output_route;
837 ret = v4l2_subdev_call(sd, video, s_routing,
838 input, output, 0);
839 if (ret < 0 && ret != -ENOIOCTLCMD) {
840 vpif_dbg(1, debug, "Failed to set input\n");
841 return ret;
842 }
843 }
844 ch->input_idx = index;
845 ch->sd = sd;
846 /* copy interface parameters to vpif */
847 ch->vpifparams.iface = chan_cfg->vpif_if;
848
849 /* update tvnorms from the sub device input info */
850 ch->video_dev->tvnorms = chan_cfg->inputs[index].input.std;
851 return 0;
852 }
853
854 /**
855 * vpif_querystd() - querystd handler
856 * @file: file ptr
857 * @priv: file handle
858 * @std_id: ptr to std id
859 *
860 * This function is called to detect standard at the selected input
861 */
862 static int vpif_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
863 {
864 struct video_device *vdev = video_devdata(file);
865 struct channel_obj *ch = video_get_drvdata(vdev);
866 int ret = 0;
867
868 vpif_dbg(2, debug, "vpif_querystd\n");
869
870 /* Call querystd function of decoder device */
871 ret = v4l2_subdev_call(ch->sd, video, querystd, std_id);
872
873 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
874 return -ENODATA;
875 if (ret) {
876 vpif_dbg(1, debug, "Failed to query standard for sub devices\n");
877 return ret;
878 }
879
880 return 0;
881 }
882
883 /**
884 * vpif_g_std() - get STD handler
885 * @file: file ptr
886 * @priv: file handle
887 * @std_id: ptr to std id
888 */
889 static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
890 {
891 struct vpif_capture_config *config = vpif_dev->platform_data;
892 struct video_device *vdev = video_devdata(file);
893 struct channel_obj *ch = video_get_drvdata(vdev);
894 struct vpif_capture_chan_config *chan_cfg;
895 struct v4l2_input input;
896
897 vpif_dbg(2, debug, "vpif_g_std\n");
898
899 if (config->chan_config[ch->channel_id].inputs == NULL)
900 return -ENODATA;
901
902 chan_cfg = &config->chan_config[ch->channel_id];
903 input = chan_cfg->inputs[ch->input_idx].input;
904 if (input.capabilities != V4L2_IN_CAP_STD)
905 return -ENODATA;
906
907 *std = ch->video.stdid;
908 return 0;
909 }
910
911 /**
912 * vpif_s_std() - set STD handler
913 * @file: file ptr
914 * @priv: file handle
915 * @std_id: ptr to std id
916 */
917 static int vpif_s_std(struct file *file, void *priv, v4l2_std_id std_id)
918 {
919 struct vpif_capture_config *config = vpif_dev->platform_data;
920 struct video_device *vdev = video_devdata(file);
921 struct channel_obj *ch = video_get_drvdata(vdev);
922 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
923 struct vpif_capture_chan_config *chan_cfg;
924 struct v4l2_input input;
925 int ret;
926
927 vpif_dbg(2, debug, "vpif_s_std\n");
928
929 if (config->chan_config[ch->channel_id].inputs == NULL)
930 return -ENODATA;
931
932 chan_cfg = &config->chan_config[ch->channel_id];
933 input = chan_cfg->inputs[ch->input_idx].input;
934 if (input.capabilities != V4L2_IN_CAP_STD)
935 return -ENODATA;
936
937 if (vb2_is_busy(&common->buffer_queue))
938 return -EBUSY;
939
940 /* Call encoder subdevice function to set the standard */
941 ch->video.stdid = std_id;
942 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
943
944 /* Get the information about the standard */
945 if (vpif_update_std_info(ch)) {
946 vpif_err("Error getting the standard info\n");
947 return -EINVAL;
948 }
949
950 /* Configure the default format information */
951 vpif_config_format(ch);
952
953 /* set standard in the sub device */
954 ret = v4l2_subdev_call(ch->sd, core, s_std, std_id);
955 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
956 vpif_dbg(1, debug, "Failed to set standard for sub devices\n");
957 return ret;
958 }
959 return 0;
960 }
961
962 /**
963 * vpif_enum_input() - ENUMINPUT handler
964 * @file: file ptr
965 * @priv: file handle
966 * @input: ptr to input structure
967 */
968 static int vpif_enum_input(struct file *file, void *priv,
969 struct v4l2_input *input)
970 {
971
972 struct vpif_capture_config *config = vpif_dev->platform_data;
973 struct video_device *vdev = video_devdata(file);
974 struct channel_obj *ch = video_get_drvdata(vdev);
975 struct vpif_capture_chan_config *chan_cfg;
976
977 chan_cfg = &config->chan_config[ch->channel_id];
978
979 if (input->index >= chan_cfg->input_count) {
980 vpif_dbg(1, debug, "Invalid input index\n");
981 return -EINVAL;
982 }
983
984 memcpy(input, &chan_cfg->inputs[input->index].input,
985 sizeof(*input));
986 return 0;
987 }
988
989 /**
990 * vpif_g_input() - Get INPUT handler
991 * @file: file ptr
992 * @priv: file handle
993 * @index: ptr to input index
994 */
995 static int vpif_g_input(struct file *file, void *priv, unsigned int *index)
996 {
997 struct video_device *vdev = video_devdata(file);
998 struct channel_obj *ch = video_get_drvdata(vdev);
999
1000 *index = ch->input_idx;
1001 return 0;
1002 }
1003
1004 /**
1005 * vpif_s_input() - Set INPUT handler
1006 * @file: file ptr
1007 * @priv: file handle
1008 * @index: input index
1009 */
1010 static int vpif_s_input(struct file *file, void *priv, unsigned int index)
1011 {
1012 struct vpif_capture_config *config = vpif_dev->platform_data;
1013 struct video_device *vdev = video_devdata(file);
1014 struct channel_obj *ch = video_get_drvdata(vdev);
1015 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1016 struct vpif_capture_chan_config *chan_cfg;
1017
1018 chan_cfg = &config->chan_config[ch->channel_id];
1019
1020 if (index >= chan_cfg->input_count)
1021 return -EINVAL;
1022
1023 if (vb2_is_busy(&common->buffer_queue))
1024 return -EBUSY;
1025
1026 return vpif_set_input(config, ch, index);
1027 }
1028
1029 /**
1030 * vpif_enum_fmt_vid_cap() - ENUM_FMT handler
1031 * @file: file ptr
1032 * @priv: file handle
1033 * @index: input index
1034 */
1035 static int vpif_enum_fmt_vid_cap(struct file *file, void *priv,
1036 struct v4l2_fmtdesc *fmt)
1037 {
1038 struct video_device *vdev = video_devdata(file);
1039 struct channel_obj *ch = video_get_drvdata(vdev);
1040
1041 if (fmt->index != 0) {
1042 vpif_dbg(1, debug, "Invalid format index\n");
1043 return -EINVAL;
1044 }
1045
1046 /* Fill in the information about format */
1047 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER) {
1048 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1049 strcpy(fmt->description, "Raw Mode -Bayer Pattern GrRBGb");
1050 fmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
1051 } else {
1052 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1053 strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
1054 fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
1055 }
1056 return 0;
1057 }
1058
1059 /**
1060 * vpif_try_fmt_vid_cap() - TRY_FMT handler
1061 * @file: file ptr
1062 * @priv: file handle
1063 * @fmt: ptr to v4l2 format structure
1064 */
1065 static int vpif_try_fmt_vid_cap(struct file *file, void *priv,
1066 struct v4l2_format *fmt)
1067 {
1068 struct video_device *vdev = video_devdata(file);
1069 struct channel_obj *ch = video_get_drvdata(vdev);
1070 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
1071
1072 return vpif_check_format(ch, pixfmt, 1);
1073 }
1074
1075
1076 /**
1077 * vpif_g_fmt_vid_cap() - Set INPUT handler
1078 * @file: file ptr
1079 * @priv: file handle
1080 * @fmt: ptr to v4l2 format structure
1081 */
1082 static int vpif_g_fmt_vid_cap(struct file *file, void *priv,
1083 struct v4l2_format *fmt)
1084 {
1085 struct video_device *vdev = video_devdata(file);
1086 struct channel_obj *ch = video_get_drvdata(vdev);
1087 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1088
1089 /* Check the validity of the buffer type */
1090 if (common->fmt.type != fmt->type)
1091 return -EINVAL;
1092
1093 /* Fill in the information about format */
1094 *fmt = common->fmt;
1095 return 0;
1096 }
1097
1098 /**
1099 * vpif_s_fmt_vid_cap() - Set FMT handler
1100 * @file: file ptr
1101 * @priv: file handle
1102 * @fmt: ptr to v4l2 format structure
1103 */
1104 static int vpif_s_fmt_vid_cap(struct file *file, void *priv,
1105 struct v4l2_format *fmt)
1106 {
1107 struct video_device *vdev = video_devdata(file);
1108 struct channel_obj *ch = video_get_drvdata(vdev);
1109 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1110 struct v4l2_pix_format *pixfmt;
1111 int ret = 0;
1112
1113 vpif_dbg(2, debug, "%s\n", __func__);
1114
1115 if (vb2_is_busy(&common->buffer_queue))
1116 return -EBUSY;
1117
1118 pixfmt = &fmt->fmt.pix;
1119 /* Check for valid field format */
1120 ret = vpif_check_format(ch, pixfmt, 0);
1121
1122 if (ret)
1123 return ret;
1124 /* store the format in the channel object */
1125 common->fmt = *fmt;
1126 return 0;
1127 }
1128
1129 /**
1130 * vpif_querycap() - QUERYCAP handler
1131 * @file: file ptr
1132 * @priv: file handle
1133 * @cap: ptr to v4l2_capability structure
1134 */
1135 static int vpif_querycap(struct file *file, void *priv,
1136 struct v4l2_capability *cap)
1137 {
1138 struct vpif_capture_config *config = vpif_dev->platform_data;
1139
1140 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1141 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1142 strlcpy(cap->driver, VPIF_DRIVER_NAME, sizeof(cap->driver));
1143 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
1144 dev_name(vpif_dev));
1145 strlcpy(cap->card, config->card_name, sizeof(cap->card));
1146
1147 return 0;
1148 }
1149
1150 /**
1151 * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler
1152 * @file: file ptr
1153 * @priv: file handle
1154 * @timings: input timings
1155 */
1156 static int
1157 vpif_enum_dv_timings(struct file *file, void *priv,
1158 struct v4l2_enum_dv_timings *timings)
1159 {
1160 struct vpif_capture_config *config = vpif_dev->platform_data;
1161 struct video_device *vdev = video_devdata(file);
1162 struct channel_obj *ch = video_get_drvdata(vdev);
1163 struct vpif_capture_chan_config *chan_cfg;
1164 struct v4l2_input input;
1165 int ret;
1166
1167 if (config->chan_config[ch->channel_id].inputs == NULL)
1168 return -ENODATA;
1169
1170 chan_cfg = &config->chan_config[ch->channel_id];
1171 input = chan_cfg->inputs[ch->input_idx].input;
1172 if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
1173 return -ENODATA;
1174
1175 ret = v4l2_subdev_call(ch->sd, video, enum_dv_timings, timings);
1176 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1177 return -EINVAL;
1178
1179 return ret;
1180 }
1181
1182 /**
1183 * vpif_query_dv_timings() - QUERY_DV_TIMINGS handler
1184 * @file: file ptr
1185 * @priv: file handle
1186 * @timings: input timings
1187 */
1188 static int
1189 vpif_query_dv_timings(struct file *file, void *priv,
1190 struct v4l2_dv_timings *timings)
1191 {
1192 struct vpif_capture_config *config = vpif_dev->platform_data;
1193 struct video_device *vdev = video_devdata(file);
1194 struct channel_obj *ch = video_get_drvdata(vdev);
1195 struct vpif_capture_chan_config *chan_cfg;
1196 struct v4l2_input input;
1197 int ret;
1198
1199 if (config->chan_config[ch->channel_id].inputs == NULL)
1200 return -ENODATA;
1201
1202 chan_cfg = &config->chan_config[ch->channel_id];
1203 input = chan_cfg->inputs[ch->input_idx].input;
1204 if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
1205 return -ENODATA;
1206
1207 ret = v4l2_subdev_call(ch->sd, video, query_dv_timings, timings);
1208 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1209 return -ENODATA;
1210
1211 return ret;
1212 }
1213
1214 /**
1215 * vpif_s_dv_timings() - S_DV_TIMINGS handler
1216 * @file: file ptr
1217 * @priv: file handle
1218 * @timings: digital video timings
1219 */
1220 static int vpif_s_dv_timings(struct file *file, void *priv,
1221 struct v4l2_dv_timings *timings)
1222 {
1223 struct vpif_capture_config *config = vpif_dev->platform_data;
1224 struct video_device *vdev = video_devdata(file);
1225 struct channel_obj *ch = video_get_drvdata(vdev);
1226 struct vpif_params *vpifparams = &ch->vpifparams;
1227 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
1228 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1229 struct video_obj *vid_ch = &ch->video;
1230 struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
1231 struct vpif_capture_chan_config *chan_cfg;
1232 struct v4l2_input input;
1233 int ret;
1234
1235 if (config->chan_config[ch->channel_id].inputs == NULL)
1236 return -ENODATA;
1237
1238 chan_cfg = &config->chan_config[ch->channel_id];
1239 input = chan_cfg->inputs[ch->input_idx].input;
1240 if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
1241 return -ENODATA;
1242
1243 if (timings->type != V4L2_DV_BT_656_1120) {
1244 vpif_dbg(2, debug, "Timing type not defined\n");
1245 return -EINVAL;
1246 }
1247
1248 if (vb2_is_busy(&common->buffer_queue))
1249 return -EBUSY;
1250
1251 /* Configure subdevice timings, if any */
1252 ret = v4l2_subdev_call(ch->sd, video, s_dv_timings, timings);
1253 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1254 ret = 0;
1255 if (ret < 0) {
1256 vpif_dbg(2, debug, "Error setting custom DV timings\n");
1257 return ret;
1258 }
1259
1260 if (!(timings->bt.width && timings->bt.height &&
1261 (timings->bt.hbackporch ||
1262 timings->bt.hfrontporch ||
1263 timings->bt.hsync) &&
1264 timings->bt.vfrontporch &&
1265 (timings->bt.vbackporch ||
1266 timings->bt.vsync))) {
1267 vpif_dbg(2, debug, "Timings for width, height, "
1268 "horizontal back porch, horizontal sync, "
1269 "horizontal front porch, vertical back porch, "
1270 "vertical sync and vertical back porch "
1271 "must be defined\n");
1272 return -EINVAL;
1273 }
1274
1275 vid_ch->dv_timings = *timings;
1276
1277 /* Configure video port timings */
1278
1279 std_info->eav2sav = V4L2_DV_BT_BLANKING_WIDTH(bt) - 8;
1280 std_info->sav2eav = bt->width;
1281
1282 std_info->l1 = 1;
1283 std_info->l3 = bt->vsync + bt->vbackporch + 1;
1284
1285 std_info->vsize = V4L2_DV_BT_FRAME_HEIGHT(bt);
1286 if (bt->interlaced) {
1287 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
1288 std_info->l5 = std_info->vsize/2 -
1289 (bt->vfrontporch - 1);
1290 std_info->l7 = std_info->vsize/2 + 1;
1291 std_info->l9 = std_info->l7 + bt->il_vsync +
1292 bt->il_vbackporch + 1;
1293 std_info->l11 = std_info->vsize -
1294 (bt->il_vfrontporch - 1);
1295 } else {
1296 vpif_dbg(2, debug, "Required timing values for "
1297 "interlaced BT format missing\n");
1298 return -EINVAL;
1299 }
1300 } else {
1301 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
1302 }
1303 strncpy(std_info->name, "Custom timings BT656/1120", VPIF_MAX_NAME);
1304 std_info->width = bt->width;
1305 std_info->height = bt->height;
1306 std_info->frm_fmt = bt->interlaced ? 0 : 1;
1307 std_info->ycmux_mode = 0;
1308 std_info->capture_format = 0;
1309 std_info->vbi_supported = 0;
1310 std_info->hd_sd = 1;
1311 std_info->stdid = 0;
1312
1313 vid_ch->stdid = 0;
1314 return 0;
1315 }
1316
1317 /**
1318 * vpif_g_dv_timings() - G_DV_TIMINGS handler
1319 * @file: file ptr
1320 * @priv: file handle
1321 * @timings: digital video timings
1322 */
1323 static int vpif_g_dv_timings(struct file *file, void *priv,
1324 struct v4l2_dv_timings *timings)
1325 {
1326 struct vpif_capture_config *config = vpif_dev->platform_data;
1327 struct video_device *vdev = video_devdata(file);
1328 struct channel_obj *ch = video_get_drvdata(vdev);
1329 struct video_obj *vid_ch = &ch->video;
1330 struct vpif_capture_chan_config *chan_cfg;
1331 struct v4l2_input input;
1332
1333 if (config->chan_config[ch->channel_id].inputs == NULL)
1334 return -ENODATA;
1335
1336 chan_cfg = &config->chan_config[ch->channel_id];
1337 input = chan_cfg->inputs[ch->input_idx].input;
1338 if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
1339 return -ENODATA;
1340
1341 *timings = vid_ch->dv_timings;
1342
1343 return 0;
1344 }
1345
1346 /*
1347 * vpif_log_status() - Status information
1348 * @file: file ptr
1349 * @priv: file handle
1350 *
1351 * Returns zero.
1352 */
1353 static int vpif_log_status(struct file *filep, void *priv)
1354 {
1355 /* status for sub devices */
1356 v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1357
1358 return 0;
1359 }
1360
1361 /* vpif capture ioctl operations */
1362 static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
1363 .vidioc_querycap = vpif_querycap,
1364 .vidioc_enum_fmt_vid_cap = vpif_enum_fmt_vid_cap,
1365 .vidioc_g_fmt_vid_cap = vpif_g_fmt_vid_cap,
1366 .vidioc_s_fmt_vid_cap = vpif_s_fmt_vid_cap,
1367 .vidioc_try_fmt_vid_cap = vpif_try_fmt_vid_cap,
1368
1369 .vidioc_enum_input = vpif_enum_input,
1370 .vidioc_s_input = vpif_s_input,
1371 .vidioc_g_input = vpif_g_input,
1372
1373 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1374 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1375 .vidioc_querybuf = vb2_ioctl_querybuf,
1376 .vidioc_qbuf = vb2_ioctl_qbuf,
1377 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1378 .vidioc_expbuf = vb2_ioctl_expbuf,
1379 .vidioc_streamon = vb2_ioctl_streamon,
1380 .vidioc_streamoff = vb2_ioctl_streamoff,
1381
1382 .vidioc_querystd = vpif_querystd,
1383 .vidioc_s_std = vpif_s_std,
1384 .vidioc_g_std = vpif_g_std,
1385
1386 .vidioc_enum_dv_timings = vpif_enum_dv_timings,
1387 .vidioc_query_dv_timings = vpif_query_dv_timings,
1388 .vidioc_s_dv_timings = vpif_s_dv_timings,
1389 .vidioc_g_dv_timings = vpif_g_dv_timings,
1390
1391 .vidioc_log_status = vpif_log_status,
1392 };
1393
1394 /* vpif file operations */
1395 static struct v4l2_file_operations vpif_fops = {
1396 .owner = THIS_MODULE,
1397 .open = v4l2_fh_open,
1398 .release = vb2_fop_release,
1399 .unlocked_ioctl = video_ioctl2,
1400 .mmap = vb2_fop_mmap,
1401 .poll = vb2_fop_poll
1402 };
1403
1404 /**
1405 * initialize_vpif() - Initialize vpif data structures
1406 *
1407 * Allocate memory for data structures and initialize them
1408 */
1409 static int initialize_vpif(void)
1410 {
1411 int err = 0, i, j;
1412 int free_channel_objects_index;
1413
1414 /* Default number of buffers should be 3 */
1415 if ((ch0_numbuffers > 0) &&
1416 (ch0_numbuffers < config_params.min_numbuffers))
1417 ch0_numbuffers = config_params.min_numbuffers;
1418 if ((ch1_numbuffers > 0) &&
1419 (ch1_numbuffers < config_params.min_numbuffers))
1420 ch1_numbuffers = config_params.min_numbuffers;
1421
1422 /* Set buffer size to min buffers size if it is invalid */
1423 if (ch0_bufsize < config_params.min_bufsize[VPIF_CHANNEL0_VIDEO])
1424 ch0_bufsize =
1425 config_params.min_bufsize[VPIF_CHANNEL0_VIDEO];
1426 if (ch1_bufsize < config_params.min_bufsize[VPIF_CHANNEL1_VIDEO])
1427 ch1_bufsize =
1428 config_params.min_bufsize[VPIF_CHANNEL1_VIDEO];
1429
1430 config_params.numbuffers[VPIF_CHANNEL0_VIDEO] = ch0_numbuffers;
1431 config_params.numbuffers[VPIF_CHANNEL1_VIDEO] = ch1_numbuffers;
1432 if (ch0_numbuffers) {
1433 config_params.channel_bufsize[VPIF_CHANNEL0_VIDEO]
1434 = ch0_bufsize;
1435 }
1436 if (ch1_numbuffers) {
1437 config_params.channel_bufsize[VPIF_CHANNEL1_VIDEO]
1438 = ch1_bufsize;
1439 }
1440
1441 /* Allocate memory for six channel objects */
1442 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1443 vpif_obj.dev[i] =
1444 kzalloc(sizeof(*vpif_obj.dev[i]), GFP_KERNEL);
1445 /* If memory allocation fails, return error */
1446 if (!vpif_obj.dev[i]) {
1447 free_channel_objects_index = i;
1448 err = -ENOMEM;
1449 goto vpif_init_free_channel_objects;
1450 }
1451 }
1452 return 0;
1453
1454 vpif_init_free_channel_objects:
1455 for (j = 0; j < free_channel_objects_index; j++)
1456 kfree(vpif_obj.dev[j]);
1457 return err;
1458 }
1459
1460 static int vpif_async_bound(struct v4l2_async_notifier *notifier,
1461 struct v4l2_subdev *subdev,
1462 struct v4l2_async_subdev *asd)
1463 {
1464 int i;
1465
1466 for (i = 0; i < vpif_obj.config->subdev_count; i++)
1467 if (!strcmp(vpif_obj.config->subdev_info[i].name,
1468 subdev->name)) {
1469 vpif_obj.sd[i] = subdev;
1470 return 0;
1471 }
1472
1473 return -EINVAL;
1474 }
1475
1476 static int vpif_probe_complete(void)
1477 {
1478 struct common_obj *common;
1479 struct video_device *vdev;
1480 struct channel_obj *ch;
1481 struct vb2_queue *q;
1482 int i, j, err, k;
1483
1484 for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
1485 ch = vpif_obj.dev[j];
1486 ch->channel_id = j;
1487 common = &(ch->common[VPIF_VIDEO_INDEX]);
1488 spin_lock_init(&common->irqlock);
1489 mutex_init(&common->lock);
1490
1491 /* select input 0 */
1492 err = vpif_set_input(vpif_obj.config, ch, 0);
1493 if (err)
1494 goto probe_out;
1495
1496 /* Initialize vb2 queue */
1497 q = &common->buffer_queue;
1498 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1499 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1500 q->drv_priv = ch;
1501 q->ops = &video_qops;
1502 q->mem_ops = &vb2_dma_contig_memops;
1503 q->buf_struct_size = sizeof(struct vpif_cap_buffer);
1504 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1505 q->min_buffers_needed = 1;
1506 q->lock = &common->lock;
1507
1508 err = vb2_queue_init(q);
1509 if (err) {
1510 vpif_err("vpif_capture: vb2_queue_init() failed\n");
1511 goto probe_out;
1512 }
1513
1514 common->alloc_ctx = vb2_dma_contig_init_ctx(vpif_dev);
1515 if (IS_ERR(common->alloc_ctx)) {
1516 vpif_err("Failed to get the context\n");
1517 err = PTR_ERR(common->alloc_ctx);
1518 goto probe_out;
1519 }
1520
1521 INIT_LIST_HEAD(&common->dma_queue);
1522
1523 /* Initialize the video_device structure */
1524 vdev = ch->video_dev;
1525 strlcpy(vdev->name, VPIF_DRIVER_NAME, sizeof(vdev->name));
1526 vdev->release = video_device_release;
1527 vdev->fops = &vpif_fops;
1528 vdev->ioctl_ops = &vpif_ioctl_ops;
1529 vdev->v4l2_dev = &vpif_obj.v4l2_dev;
1530 vdev->vfl_dir = VFL_DIR_RX;
1531 vdev->queue = q;
1532 vdev->lock = &common->lock;
1533 set_bit(V4L2_FL_USE_FH_PRIO, &vdev->flags);
1534 video_set_drvdata(ch->video_dev, ch);
1535 err = video_register_device(vdev,
1536 VFL_TYPE_GRABBER, (j ? 1 : 0));
1537 if (err)
1538 goto probe_out;
1539 }
1540
1541 v4l2_info(&vpif_obj.v4l2_dev, "VPIF capture driver initialized\n");
1542 return 0;
1543
1544 probe_out:
1545 for (k = 0; k < j; k++) {
1546 /* Get the pointer to the channel object */
1547 ch = vpif_obj.dev[k];
1548 common = &ch->common[k];
1549 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
1550 /* Unregister video device */
1551 video_unregister_device(ch->video_dev);
1552 }
1553 kfree(vpif_obj.sd);
1554 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1555 ch = vpif_obj.dev[i];
1556 /* Note: does nothing if ch->video_dev == NULL */
1557 video_device_release(ch->video_dev);
1558 }
1559 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1560
1561 return err;
1562 }
1563
1564 static int vpif_async_complete(struct v4l2_async_notifier *notifier)
1565 {
1566 return vpif_probe_complete();
1567 }
1568
1569 /**
1570 * vpif_probe : This function probes the vpif capture driver
1571 * @pdev: platform device pointer
1572 *
1573 * This creates device entries by register itself to the V4L2 driver and
1574 * initializes fields of each channel objects
1575 */
1576 static __init int vpif_probe(struct platform_device *pdev)
1577 {
1578 struct vpif_subdev_info *subdevdata;
1579 int i, j, err;
1580 int res_idx = 0;
1581 struct i2c_adapter *i2c_adap;
1582 struct channel_obj *ch;
1583 struct video_device *vfd;
1584 struct resource *res;
1585 int subdev_count;
1586
1587 vpif_dev = &pdev->dev;
1588
1589 err = initialize_vpif();
1590 if (err) {
1591 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
1592 return err;
1593 }
1594
1595 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
1596 if (err) {
1597 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
1598 return err;
1599 }
1600
1601 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
1602 err = devm_request_irq(&pdev->dev, res->start, vpif_channel_isr,
1603 IRQF_SHARED, VPIF_DRIVER_NAME,
1604 (void *)(&vpif_obj.dev[res_idx]->
1605 channel_id));
1606 if (err) {
1607 err = -EINVAL;
1608 goto vpif_unregister;
1609 }
1610 res_idx++;
1611 }
1612
1613 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1614 /* Get the pointer to the channel object */
1615 ch = vpif_obj.dev[i];
1616 /* Allocate memory for video device */
1617 vfd = video_device_alloc();
1618 if (NULL == vfd) {
1619 for (j = 0; j < i; j++) {
1620 ch = vpif_obj.dev[j];
1621 video_device_release(ch->video_dev);
1622 }
1623 err = -ENOMEM;
1624 goto vpif_unregister;
1625 }
1626
1627 /* Set video_dev to the video device */
1628 ch->video_dev = vfd;
1629 }
1630
1631 vpif_obj.config = pdev->dev.platform_data;
1632
1633 subdev_count = vpif_obj.config->subdev_count;
1634 vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
1635 GFP_KERNEL);
1636 if (vpif_obj.sd == NULL) {
1637 vpif_err("unable to allocate memory for subdevice pointers\n");
1638 err = -ENOMEM;
1639 goto vpif_sd_error;
1640 }
1641
1642 if (!vpif_obj.config->asd_sizes) {
1643 i2c_adap = i2c_get_adapter(1);
1644 for (i = 0; i < subdev_count; i++) {
1645 subdevdata = &vpif_obj.config->subdev_info[i];
1646 vpif_obj.sd[i] =
1647 v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
1648 i2c_adap,
1649 &subdevdata->
1650 board_info,
1651 NULL);
1652
1653 if (!vpif_obj.sd[i]) {
1654 vpif_err("Error registering v4l2 subdevice\n");
1655 err = -ENODEV;
1656 goto probe_subdev_out;
1657 }
1658 v4l2_info(&vpif_obj.v4l2_dev,
1659 "registered sub device %s\n",
1660 subdevdata->name);
1661 }
1662 vpif_probe_complete();
1663 } else {
1664 vpif_obj.notifier.subdevs = vpif_obj.config->asd;
1665 vpif_obj.notifier.num_subdevs = vpif_obj.config->asd_sizes[0];
1666 vpif_obj.notifier.bound = vpif_async_bound;
1667 vpif_obj.notifier.complete = vpif_async_complete;
1668 err = v4l2_async_notifier_register(&vpif_obj.v4l2_dev,
1669 &vpif_obj.notifier);
1670 if (err) {
1671 vpif_err("Error registering async notifier\n");
1672 err = -EINVAL;
1673 goto probe_subdev_out;
1674 }
1675 }
1676
1677 return 0;
1678
1679 probe_subdev_out:
1680 /* free sub devices memory */
1681 kfree(vpif_obj.sd);
1682
1683 vpif_sd_error:
1684 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1685 ch = vpif_obj.dev[i];
1686 /* Note: does nothing if ch->video_dev == NULL */
1687 video_device_release(ch->video_dev);
1688 }
1689 vpif_unregister:
1690 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1691
1692 return err;
1693 }
1694
1695 /**
1696 * vpif_remove() - driver remove handler
1697 * @device: ptr to platform device structure
1698 *
1699 * The vidoe device is unregistered
1700 */
1701 static int vpif_remove(struct platform_device *device)
1702 {
1703 struct common_obj *common;
1704 struct channel_obj *ch;
1705 int i;
1706
1707 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1708
1709 kfree(vpif_obj.sd);
1710 /* un-register device */
1711 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1712 /* Get the pointer to the channel object */
1713 ch = vpif_obj.dev[i];
1714 common = &ch->common[i];
1715 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
1716 /* Unregister video device */
1717 video_unregister_device(ch->video_dev);
1718 kfree(vpif_obj.dev[i]);
1719 }
1720 return 0;
1721 }
1722
1723 #ifdef CONFIG_PM_SLEEP
1724 /**
1725 * vpif_suspend: vpif device suspend
1726 */
1727 static int vpif_suspend(struct device *dev)
1728 {
1729
1730 struct common_obj *common;
1731 struct channel_obj *ch;
1732 int i;
1733
1734 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1735 /* Get the pointer to the channel object */
1736 ch = vpif_obj.dev[i];
1737 common = &ch->common[VPIF_VIDEO_INDEX];
1738
1739 if (!vb2_is_streaming(&common->buffer_queue))
1740 continue;
1741
1742 mutex_lock(&common->lock);
1743 /* Disable channel */
1744 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
1745 enable_channel0(0);
1746 channel0_intr_enable(0);
1747 }
1748 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
1749 ycmux_mode == 2) {
1750 enable_channel1(0);
1751 channel1_intr_enable(0);
1752 }
1753 mutex_unlock(&common->lock);
1754 }
1755
1756 return 0;
1757 }
1758
1759 /*
1760 * vpif_resume: vpif device suspend
1761 */
1762 static int vpif_resume(struct device *dev)
1763 {
1764 struct common_obj *common;
1765 struct channel_obj *ch;
1766 int i;
1767
1768 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1769 /* Get the pointer to the channel object */
1770 ch = vpif_obj.dev[i];
1771 common = &ch->common[VPIF_VIDEO_INDEX];
1772
1773 if (!vb2_is_streaming(&common->buffer_queue))
1774 continue;
1775
1776 mutex_lock(&common->lock);
1777 /* Enable channel */
1778 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
1779 enable_channel0(1);
1780 channel0_intr_enable(1);
1781 }
1782 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
1783 ycmux_mode == 2) {
1784 enable_channel1(1);
1785 channel1_intr_enable(1);
1786 }
1787 mutex_unlock(&common->lock);
1788 }
1789
1790 return 0;
1791 }
1792 #endif
1793
1794 static SIMPLE_DEV_PM_OPS(vpif_pm_ops, vpif_suspend, vpif_resume);
1795
1796 static __refdata struct platform_driver vpif_driver = {
1797 .driver = {
1798 .name = VPIF_DRIVER_NAME,
1799 .owner = THIS_MODULE,
1800 .pm = &vpif_pm_ops,
1801 },
1802 .probe = vpif_probe,
1803 .remove = vpif_remove,
1804 };
1805
1806 module_platform_driver(vpif_driver);