[media] cx25821: Replace struct memcpy with struct assignment
[deliverable/linux.git] / drivers / media / video / davinci / vpif_capture.c
1 /*
2 * Copyright (C) 2009 Texas Instruments Inc
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * TODO : add support for VBI & HBI data service
19 * add static buffer allocation
20 */
21 #include <linux/kernel.h>
22 #include <linux/init.h>
23 #include <linux/module.h>
24 #include <linux/errno.h>
25 #include <linux/fs.h>
26 #include <linux/mm.h>
27 #include <linux/interrupt.h>
28 #include <linux/workqueue.h>
29 #include <linux/string.h>
30 #include <linux/videodev2.h>
31 #include <linux/wait.h>
32 #include <linux/time.h>
33 #include <linux/i2c.h>
34 #include <linux/platform_device.h>
35 #include <linux/io.h>
36 #include <linux/slab.h>
37 #include <media/v4l2-device.h>
38 #include <media/v4l2-ioctl.h>
39 #include <media/v4l2-chip-ident.h>
40
41 #include "vpif_capture.h"
42 #include "vpif.h"
43
44 MODULE_DESCRIPTION("TI DaVinci VPIF Capture driver");
45 MODULE_LICENSE("GPL");
46 MODULE_VERSION(VPIF_CAPTURE_VERSION);
47
48 #define vpif_err(fmt, arg...) v4l2_err(&vpif_obj.v4l2_dev, fmt, ## arg)
49 #define vpif_dbg(level, debug, fmt, arg...) \
50 v4l2_dbg(level, debug, &vpif_obj.v4l2_dev, fmt, ## arg)
51
52 static int debug = 1;
53 static u32 ch0_numbuffers = 3;
54 static u32 ch1_numbuffers = 3;
55 static u32 ch0_bufsize = 1920 * 1080 * 2;
56 static u32 ch1_bufsize = 720 * 576 * 2;
57
58 module_param(debug, int, 0644);
59 module_param(ch0_numbuffers, uint, S_IRUGO);
60 module_param(ch1_numbuffers, uint, S_IRUGO);
61 module_param(ch0_bufsize, uint, S_IRUGO);
62 module_param(ch1_bufsize, uint, S_IRUGO);
63
64 MODULE_PARM_DESC(debug, "Debug level 0-1");
65 MODULE_PARM_DESC(ch2_numbuffers, "Channel0 buffer count (default:3)");
66 MODULE_PARM_DESC(ch3_numbuffers, "Channel1 buffer count (default:3)");
67 MODULE_PARM_DESC(ch2_bufsize, "Channel0 buffer size (default:1920 x 1080 x 2)");
68 MODULE_PARM_DESC(ch3_bufsize, "Channel1 buffer size (default:720 x 576 x 2)");
69
70 static struct vpif_config_params config_params = {
71 .min_numbuffers = 3,
72 .numbuffers[0] = 3,
73 .numbuffers[1] = 3,
74 .min_bufsize[0] = 720 * 480 * 2,
75 .min_bufsize[1] = 720 * 480 * 2,
76 .channel_bufsize[0] = 1920 * 1080 * 2,
77 .channel_bufsize[1] = 720 * 576 * 2,
78 };
79
80 /* global variables */
81 static struct vpif_device vpif_obj = { {NULL} };
82 static struct device *vpif_dev;
83 static void vpif_calculate_offsets(struct channel_obj *ch);
84 static void vpif_config_addr(struct channel_obj *ch, int muxmode);
85
86 /**
87 * buffer_prepare : callback function for buffer prepare
88 * @vb: ptr to vb2_buffer
89 *
90 * This is the callback function for buffer prepare when vb2_qbuf()
91 * function is called. The buffer is prepared and user space virtual address
92 * or user address is converted into physical address
93 */
94 static int vpif_buffer_prepare(struct vb2_buffer *vb)
95 {
96 /* Get the file handle object and channel object */
97 struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
98 struct vb2_queue *q = vb->vb2_queue;
99 struct channel_obj *ch = fh->channel;
100 struct common_obj *common;
101 unsigned long addr;
102
103 vpif_dbg(2, debug, "vpif_buffer_prepare\n");
104
105 common = &ch->common[VPIF_VIDEO_INDEX];
106
107 if (vb->state != VB2_BUF_STATE_ACTIVE &&
108 vb->state != VB2_BUF_STATE_PREPARED) {
109 vb2_set_plane_payload(vb, 0, common->fmt.fmt.pix.sizeimage);
110 if (vb2_plane_vaddr(vb, 0) &&
111 vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
112 goto exit;
113 addr = vb2_dma_contig_plane_dma_addr(vb, 0);
114
115 if (q->streaming) {
116 if (!IS_ALIGNED((addr + common->ytop_off), 8) ||
117 !IS_ALIGNED((addr + common->ybtm_off), 8) ||
118 !IS_ALIGNED((addr + common->ctop_off), 8) ||
119 !IS_ALIGNED((addr + common->cbtm_off), 8))
120 goto exit;
121 }
122 }
123 return 0;
124 exit:
125 vpif_dbg(1, debug, "buffer_prepare:offset is not aligned to 8 bytes\n");
126 return -EINVAL;
127 }
128
129 /**
130 * vpif_buffer_queue_setup : Callback function for buffer setup.
131 * @vq: vb2_queue ptr
132 * @fmt: v4l2 format
133 * @nbuffers: ptr to number of buffers requested by application
134 * @nplanes:: contains number of distinct video planes needed to hold a frame
135 * @sizes[]: contains the size (in bytes) of each plane.
136 * @alloc_ctxs: ptr to allocation context
137 *
138 * This callback function is called when reqbuf() is called to adjust
139 * the buffer count and buffer size
140 */
141 static int vpif_buffer_queue_setup(struct vb2_queue *vq,
142 const struct v4l2_format *fmt,
143 unsigned int *nbuffers, unsigned int *nplanes,
144 unsigned int sizes[], void *alloc_ctxs[])
145 {
146 /* Get the file handle object and channel object */
147 struct vpif_fh *fh = vb2_get_drv_priv(vq);
148 struct channel_obj *ch = fh->channel;
149 struct common_obj *common;
150 unsigned long size;
151
152 common = &ch->common[VPIF_VIDEO_INDEX];
153
154 vpif_dbg(2, debug, "vpif_buffer_setup\n");
155
156 /* If memory type is not mmap, return */
157 if (V4L2_MEMORY_MMAP == common->memory) {
158 /* Calculate the size of the buffer */
159 size = config_params.channel_bufsize[ch->channel_id];
160 /*
161 * Checking if the buffer size exceeds the available buffer
162 * ycmux_mode = 0 means 1 channel mode HD and
163 * ycmux_mode = 1 means 2 channels mode SD
164 */
165 if (ch->vpifparams.std_info.ycmux_mode == 0) {
166 if (config_params.video_limit[ch->channel_id])
167 while (size * *nbuffers >
168 (config_params.video_limit[0]
169 + config_params.video_limit[1]))
170 (*nbuffers)--;
171 } else {
172 if (config_params.video_limit[ch->channel_id])
173 while (size * *nbuffers >
174 config_params.video_limit[ch->channel_id])
175 (*nbuffers)--;
176 }
177
178 } else {
179 size = common->fmt.fmt.pix.sizeimage;
180 }
181
182 if (*nbuffers < config_params.min_numbuffers)
183 *nbuffers = config_params.min_numbuffers;
184
185 *nplanes = 1;
186 sizes[0] = size;
187 alloc_ctxs[0] = common->alloc_ctx;
188
189 return 0;
190 }
191
192 /**
193 * vpif_buffer_queue : Callback function to add buffer to DMA queue
194 * @vb: ptr to vb2_buffer
195 */
196 static void vpif_buffer_queue(struct vb2_buffer *vb)
197 {
198 /* Get the file handle object and channel object */
199 struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
200 struct channel_obj *ch = fh->channel;
201 struct vpif_cap_buffer *buf = container_of(vb,
202 struct vpif_cap_buffer, vb);
203 struct common_obj *common;
204
205 common = &ch->common[VPIF_VIDEO_INDEX];
206
207 vpif_dbg(2, debug, "vpif_buffer_queue\n");
208
209 /* add the buffer to the DMA queue */
210 list_add_tail(&buf->list, &common->dma_queue);
211 }
212
213 /**
214 * vpif_buf_cleanup : Callback function to free buffer
215 * @vb: ptr to vb2_buffer
216 *
217 * This function is called from the videobuf2 layer to free memory
218 * allocated to the buffers
219 */
220 static void vpif_buf_cleanup(struct vb2_buffer *vb)
221 {
222 /* Get the file handle object and channel object */
223 struct vpif_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
224 struct vpif_cap_buffer *buf = container_of(vb,
225 struct vpif_cap_buffer, vb);
226 struct channel_obj *ch = fh->channel;
227 struct common_obj *common;
228 unsigned long flags;
229
230 common = &ch->common[VPIF_VIDEO_INDEX];
231
232 spin_lock_irqsave(&common->irqlock, flags);
233 if (vb->state == VB2_BUF_STATE_ACTIVE)
234 list_del_init(&buf->list);
235 spin_unlock_irqrestore(&common->irqlock, flags);
236
237 }
238
239 static void vpif_wait_prepare(struct vb2_queue *vq)
240 {
241 struct vpif_fh *fh = vb2_get_drv_priv(vq);
242 struct channel_obj *ch = fh->channel;
243 struct common_obj *common;
244
245 common = &ch->common[VPIF_VIDEO_INDEX];
246 mutex_unlock(&common->lock);
247 }
248
249 static void vpif_wait_finish(struct vb2_queue *vq)
250 {
251 struct vpif_fh *fh = vb2_get_drv_priv(vq);
252 struct channel_obj *ch = fh->channel;
253 struct common_obj *common;
254
255 common = &ch->common[VPIF_VIDEO_INDEX];
256 mutex_lock(&common->lock);
257 }
258
259 static int vpif_buffer_init(struct vb2_buffer *vb)
260 {
261 struct vpif_cap_buffer *buf = container_of(vb,
262 struct vpif_cap_buffer, vb);
263
264 INIT_LIST_HEAD(&buf->list);
265
266 return 0;
267 }
268
269 static u8 channel_first_int[VPIF_NUMBER_OF_OBJECTS][2] =
270 { {1, 1} };
271
272 static int vpif_start_streaming(struct vb2_queue *vq, unsigned int count)
273 {
274 struct vpif_capture_config *vpif_config_data =
275 vpif_dev->platform_data;
276 struct vpif_fh *fh = vb2_get_drv_priv(vq);
277 struct channel_obj *ch = fh->channel;
278 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
279 struct vpif_params *vpif = &ch->vpifparams;
280 unsigned long addr = 0;
281 int ret;
282
283 /* If buffer queue is empty, return error */
284 if (list_empty(&common->dma_queue)) {
285 vpif_dbg(1, debug, "buffer queue is empty\n");
286 return -EIO;
287 }
288
289 /* Get the next frame from the buffer queue */
290 common->cur_frm = common->next_frm = list_entry(common->dma_queue.next,
291 struct vpif_cap_buffer, list);
292 /* Remove buffer from the buffer queue */
293 list_del(&common->cur_frm->list);
294 /* Mark state of the current frame to active */
295 common->cur_frm->vb.state = VB2_BUF_STATE_ACTIVE;
296 /* Initialize field_id and started member */
297 ch->field_id = 0;
298 common->started = 1;
299 addr = vb2_dma_contig_plane_dma_addr(&common->cur_frm->vb, 0);
300
301 /* Calculate the offset for Y and C data in the buffer */
302 vpif_calculate_offsets(ch);
303
304 if ((vpif->std_info.frm_fmt &&
305 ((common->fmt.fmt.pix.field != V4L2_FIELD_NONE) &&
306 (common->fmt.fmt.pix.field != V4L2_FIELD_ANY))) ||
307 (!vpif->std_info.frm_fmt &&
308 (common->fmt.fmt.pix.field == V4L2_FIELD_NONE))) {
309 vpif_dbg(1, debug, "conflict in field format and std format\n");
310 return -EINVAL;
311 }
312
313 /* configure 1 or 2 channel mode */
314 ret = vpif_config_data->setup_input_channel_mode
315 (vpif->std_info.ycmux_mode);
316
317 if (ret < 0) {
318 vpif_dbg(1, debug, "can't set vpif channel mode\n");
319 return ret;
320 }
321
322 /* Call vpif_set_params function to set the parameters and addresses */
323 ret = vpif_set_video_params(vpif, ch->channel_id);
324
325 if (ret < 0) {
326 vpif_dbg(1, debug, "can't set video params\n");
327 return ret;
328 }
329
330 common->started = ret;
331 vpif_config_addr(ch, ret);
332
333 common->set_addr(addr + common->ytop_off,
334 addr + common->ybtm_off,
335 addr + common->ctop_off,
336 addr + common->cbtm_off);
337
338 /**
339 * Set interrupt for both the fields in VPIF Register enable channel in
340 * VPIF register
341 */
342 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id)) {
343 channel0_intr_assert();
344 channel0_intr_enable(1);
345 enable_channel0(1);
346 }
347 if ((VPIF_CHANNEL1_VIDEO == ch->channel_id) ||
348 (common->started == 2)) {
349 channel1_intr_assert();
350 channel1_intr_enable(1);
351 enable_channel1(1);
352 }
353 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
354
355 return 0;
356 }
357
358 /* abort streaming and wait for last buffer */
359 static int vpif_stop_streaming(struct vb2_queue *vq)
360 {
361 struct vpif_fh *fh = vb2_get_drv_priv(vq);
362 struct channel_obj *ch = fh->channel;
363 struct common_obj *common;
364
365 if (!vb2_is_streaming(vq))
366 return 0;
367
368 common = &ch->common[VPIF_VIDEO_INDEX];
369
370 /* release all active buffers */
371 while (!list_empty(&common->dma_queue)) {
372 common->next_frm = list_entry(common->dma_queue.next,
373 struct vpif_cap_buffer, list);
374 list_del(&common->next_frm->list);
375 vb2_buffer_done(&common->next_frm->vb, VB2_BUF_STATE_ERROR);
376 }
377
378 return 0;
379 }
380
381 static struct vb2_ops video_qops = {
382 .queue_setup = vpif_buffer_queue_setup,
383 .wait_prepare = vpif_wait_prepare,
384 .wait_finish = vpif_wait_finish,
385 .buf_init = vpif_buffer_init,
386 .buf_prepare = vpif_buffer_prepare,
387 .start_streaming = vpif_start_streaming,
388 .stop_streaming = vpif_stop_streaming,
389 .buf_cleanup = vpif_buf_cleanup,
390 .buf_queue = vpif_buffer_queue,
391 };
392
393 /**
394 * vpif_process_buffer_complete: process a completed buffer
395 * @common: ptr to common channel object
396 *
397 * This function time stamp the buffer and mark it as DONE. It also
398 * wake up any process waiting on the QUEUE and set the next buffer
399 * as current
400 */
401 static void vpif_process_buffer_complete(struct common_obj *common)
402 {
403 do_gettimeofday(&common->cur_frm->vb.v4l2_buf.timestamp);
404 vb2_buffer_done(&common->cur_frm->vb,
405 VB2_BUF_STATE_DONE);
406 /* Make curFrm pointing to nextFrm */
407 common->cur_frm = common->next_frm;
408 }
409
410 /**
411 * vpif_schedule_next_buffer: set next buffer address for capture
412 * @common : ptr to common channel object
413 *
414 * This function will get next buffer from the dma queue and
415 * set the buffer address in the vpif register for capture.
416 * the buffer is marked active
417 */
418 static void vpif_schedule_next_buffer(struct common_obj *common)
419 {
420 unsigned long addr = 0;
421
422 common->next_frm = list_entry(common->dma_queue.next,
423 struct vpif_cap_buffer, list);
424 /* Remove that buffer from the buffer queue */
425 list_del(&common->next_frm->list);
426 common->next_frm->vb.state = VB2_BUF_STATE_ACTIVE;
427 addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb, 0);
428
429 /* Set top and bottom field addresses in VPIF registers */
430 common->set_addr(addr + common->ytop_off,
431 addr + common->ybtm_off,
432 addr + common->ctop_off,
433 addr + common->cbtm_off);
434 }
435
436 /**
437 * vpif_channel_isr : ISR handler for vpif capture
438 * @irq: irq number
439 * @dev_id: dev_id ptr
440 *
441 * It changes status of the captured buffer, takes next buffer from the queue
442 * and sets its address in VPIF registers
443 */
444 static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
445 {
446 struct vpif_device *dev = &vpif_obj;
447 struct common_obj *common;
448 struct channel_obj *ch;
449 enum v4l2_field field;
450 int channel_id = 0;
451 int fid = -1, i;
452
453 channel_id = *(int *)(dev_id);
454 if (!vpif_intr_status(channel_id))
455 return IRQ_NONE;
456
457 ch = dev->dev[channel_id];
458
459 field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
460
461 for (i = 0; i < VPIF_NUMBER_OF_OBJECTS; i++) {
462 common = &ch->common[i];
463 /* skip If streaming is not started in this channel */
464 if (0 == common->started)
465 continue;
466
467 /* Check the field format */
468 if (1 == ch->vpifparams.std_info.frm_fmt) {
469 /* Progressive mode */
470 if (list_empty(&common->dma_queue))
471 continue;
472
473 if (!channel_first_int[i][channel_id])
474 vpif_process_buffer_complete(common);
475
476 channel_first_int[i][channel_id] = 0;
477
478 vpif_schedule_next_buffer(common);
479
480
481 channel_first_int[i][channel_id] = 0;
482 } else {
483 /**
484 * Interlaced mode. If it is first interrupt, ignore
485 * it
486 */
487 if (channel_first_int[i][channel_id]) {
488 channel_first_int[i][channel_id] = 0;
489 continue;
490 }
491 if (0 == i) {
492 ch->field_id ^= 1;
493 /* Get field id from VPIF registers */
494 fid = vpif_channel_getfid(ch->channel_id);
495 if (fid != ch->field_id) {
496 /**
497 * If field id does not match stored
498 * field id, make them in sync
499 */
500 if (0 == fid)
501 ch->field_id = fid;
502 return IRQ_HANDLED;
503 }
504 }
505 /* device field id and local field id are in sync */
506 if (0 == fid) {
507 /* this is even field */
508 if (common->cur_frm == common->next_frm)
509 continue;
510
511 /* mark the current buffer as done */
512 vpif_process_buffer_complete(common);
513 } else if (1 == fid) {
514 /* odd field */
515 if (list_empty(&common->dma_queue) ||
516 (common->cur_frm != common->next_frm))
517 continue;
518
519 vpif_schedule_next_buffer(common);
520 }
521 }
522 }
523 return IRQ_HANDLED;
524 }
525
526 /**
527 * vpif_update_std_info() - update standard related info
528 * @ch: ptr to channel object
529 *
530 * For a given standard selected by application, update values
531 * in the device data structures
532 */
533 static int vpif_update_std_info(struct channel_obj *ch)
534 {
535 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
536 struct vpif_params *vpifparams = &ch->vpifparams;
537 const struct vpif_channel_config_params *config;
538 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
539 struct video_obj *vid_ch = &ch->video;
540 int index;
541
542 vpif_dbg(2, debug, "vpif_update_std_info\n");
543
544 for (index = 0; index < vpif_ch_params_count; index++) {
545 config = &ch_params[index];
546 if (config->hd_sd == 0) {
547 vpif_dbg(2, debug, "SD format\n");
548 if (config->stdid & vid_ch->stdid) {
549 memcpy(std_info, config, sizeof(*config));
550 break;
551 }
552 } else {
553 vpif_dbg(2, debug, "HD format\n");
554 if (config->dv_preset == vid_ch->dv_preset) {
555 memcpy(std_info, config, sizeof(*config));
556 break;
557 }
558 }
559 }
560
561 /* standard not found */
562 if (index == vpif_ch_params_count)
563 return -EINVAL;
564
565 common->fmt.fmt.pix.width = std_info->width;
566 common->width = std_info->width;
567 common->fmt.fmt.pix.height = std_info->height;
568 common->height = std_info->height;
569 common->fmt.fmt.pix.bytesperline = std_info->width;
570 vpifparams->video_params.hpitch = std_info->width;
571 vpifparams->video_params.storage_mode = std_info->frm_fmt;
572
573 return 0;
574 }
575
576 /**
577 * vpif_calculate_offsets : This function calculates buffers offsets
578 * @ch : ptr to channel object
579 *
580 * This function calculates buffer offsets for Y and C in the top and
581 * bottom field
582 */
583 static void vpif_calculate_offsets(struct channel_obj *ch)
584 {
585 unsigned int hpitch, vpitch, sizeimage;
586 struct video_obj *vid_ch = &(ch->video);
587 struct vpif_params *vpifparams = &ch->vpifparams;
588 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
589 enum v4l2_field field = common->fmt.fmt.pix.field;
590
591 vpif_dbg(2, debug, "vpif_calculate_offsets\n");
592
593 if (V4L2_FIELD_ANY == field) {
594 if (vpifparams->std_info.frm_fmt)
595 vid_ch->buf_field = V4L2_FIELD_NONE;
596 else
597 vid_ch->buf_field = V4L2_FIELD_INTERLACED;
598 } else
599 vid_ch->buf_field = common->fmt.fmt.pix.field;
600
601 sizeimage = common->fmt.fmt.pix.sizeimage;
602
603 hpitch = common->fmt.fmt.pix.bytesperline;
604 vpitch = sizeimage / (hpitch * 2);
605
606 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
607 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
608 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
609 common->ytop_off = 0;
610 common->ybtm_off = hpitch;
611 common->ctop_off = sizeimage / 2;
612 common->cbtm_off = sizeimage / 2 + hpitch;
613 } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
614 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
615 common->ytop_off = 0;
616 common->ybtm_off = sizeimage / 4;
617 common->ctop_off = sizeimage / 2;
618 common->cbtm_off = common->ctop_off + sizeimage / 4;
619 } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
620 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
621 common->ybtm_off = 0;
622 common->ytop_off = sizeimage / 4;
623 common->cbtm_off = sizeimage / 2;
624 common->ctop_off = common->cbtm_off + sizeimage / 4;
625 }
626 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
627 (V4L2_FIELD_INTERLACED == vid_ch->buf_field))
628 vpifparams->video_params.storage_mode = 1;
629 else
630 vpifparams->video_params.storage_mode = 0;
631
632 if (1 == vpifparams->std_info.frm_fmt)
633 vpifparams->video_params.hpitch =
634 common->fmt.fmt.pix.bytesperline;
635 else {
636 if ((field == V4L2_FIELD_ANY)
637 || (field == V4L2_FIELD_INTERLACED))
638 vpifparams->video_params.hpitch =
639 common->fmt.fmt.pix.bytesperline * 2;
640 else
641 vpifparams->video_params.hpitch =
642 common->fmt.fmt.pix.bytesperline;
643 }
644
645 ch->vpifparams.video_params.stdid = vpifparams->std_info.stdid;
646 }
647
648 /**
649 * vpif_config_format: configure default frame format in the device
650 * ch : ptr to channel object
651 */
652 static void vpif_config_format(struct channel_obj *ch)
653 {
654 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
655
656 vpif_dbg(2, debug, "vpif_config_format\n");
657
658 common->fmt.fmt.pix.field = V4L2_FIELD_ANY;
659 if (config_params.numbuffers[ch->channel_id] == 0)
660 common->memory = V4L2_MEMORY_USERPTR;
661 else
662 common->memory = V4L2_MEMORY_MMAP;
663
664 common->fmt.fmt.pix.sizeimage
665 = config_params.channel_bufsize[ch->channel_id];
666
667 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER)
668 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;
669 else
670 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
671 common->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
672 }
673
674 /**
675 * vpif_get_default_field() - Get default field type based on interface
676 * @vpif_params - ptr to vpif params
677 */
678 static inline enum v4l2_field vpif_get_default_field(
679 struct vpif_interface *iface)
680 {
681 return (iface->if_type == VPIF_IF_RAW_BAYER) ? V4L2_FIELD_NONE :
682 V4L2_FIELD_INTERLACED;
683 }
684
685 /**
686 * vpif_check_format() - check given pixel format for compatibility
687 * @ch - channel ptr
688 * @pixfmt - Given pixel format
689 * @update - update the values as per hardware requirement
690 *
691 * Check the application pixel format for S_FMT and update the input
692 * values as per hardware limits for TRY_FMT. The default pixel and
693 * field format is selected based on interface type.
694 */
695 static int vpif_check_format(struct channel_obj *ch,
696 struct v4l2_pix_format *pixfmt,
697 int update)
698 {
699 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
700 struct vpif_params *vpif_params = &ch->vpifparams;
701 enum v4l2_field field = pixfmt->field;
702 u32 sizeimage, hpitch, vpitch;
703 int ret = -EINVAL;
704
705 vpif_dbg(2, debug, "vpif_check_format\n");
706 /**
707 * first check for the pixel format. If if_type is Raw bayer,
708 * only V4L2_PIX_FMT_SBGGR8 format is supported. Otherwise only
709 * V4L2_PIX_FMT_YUV422P is supported
710 */
711 if (vpif_params->iface.if_type == VPIF_IF_RAW_BAYER) {
712 if (pixfmt->pixelformat != V4L2_PIX_FMT_SBGGR8) {
713 if (!update) {
714 vpif_dbg(2, debug, "invalid pix format\n");
715 goto exit;
716 }
717 pixfmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
718 }
719 } else {
720 if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P) {
721 if (!update) {
722 vpif_dbg(2, debug, "invalid pixel format\n");
723 goto exit;
724 }
725 pixfmt->pixelformat = V4L2_PIX_FMT_YUV422P;
726 }
727 }
728
729 if (!(VPIF_VALID_FIELD(field))) {
730 if (!update) {
731 vpif_dbg(2, debug, "invalid field format\n");
732 goto exit;
733 }
734 /**
735 * By default use FIELD_NONE for RAW Bayer capture
736 * and FIELD_INTERLACED for other interfaces
737 */
738 field = vpif_get_default_field(&vpif_params->iface);
739 } else if (field == V4L2_FIELD_ANY)
740 /* unsupported field. Use default */
741 field = vpif_get_default_field(&vpif_params->iface);
742
743 /* validate the hpitch */
744 hpitch = pixfmt->bytesperline;
745 if (hpitch < vpif_params->std_info.width) {
746 if (!update) {
747 vpif_dbg(2, debug, "invalid hpitch\n");
748 goto exit;
749 }
750 hpitch = vpif_params->std_info.width;
751 }
752
753 sizeimage = pixfmt->sizeimage;
754
755 vpitch = sizeimage / (hpitch * 2);
756
757 /* validate the vpitch */
758 if (vpitch < vpif_params->std_info.height) {
759 if (!update) {
760 vpif_dbg(2, debug, "Invalid vpitch\n");
761 goto exit;
762 }
763 vpitch = vpif_params->std_info.height;
764 }
765
766 /* Check for 8 byte alignment */
767 if (!ALIGN(hpitch, 8)) {
768 if (!update) {
769 vpif_dbg(2, debug, "invalid pitch alignment\n");
770 goto exit;
771 }
772 /* adjust to next 8 byte boundary */
773 hpitch = (((hpitch + 7) / 8) * 8);
774 }
775 /* if update is set, modify the bytesperline and sizeimage */
776 if (update) {
777 pixfmt->bytesperline = hpitch;
778 pixfmt->sizeimage = hpitch * vpitch * 2;
779 }
780 /**
781 * Image width and height is always based on current standard width and
782 * height
783 */
784 pixfmt->width = common->fmt.fmt.pix.width;
785 pixfmt->height = common->fmt.fmt.pix.height;
786 return 0;
787 exit:
788 return ret;
789 }
790
791 /**
792 * vpif_config_addr() - function to configure buffer address in vpif
793 * @ch - channel ptr
794 * @muxmode - channel mux mode
795 */
796 static void vpif_config_addr(struct channel_obj *ch, int muxmode)
797 {
798 struct common_obj *common;
799
800 vpif_dbg(2, debug, "vpif_config_addr\n");
801
802 common = &(ch->common[VPIF_VIDEO_INDEX]);
803
804 if (VPIF_CHANNEL1_VIDEO == ch->channel_id)
805 common->set_addr = ch1_set_videobuf_addr;
806 else if (2 == muxmode)
807 common->set_addr = ch0_set_videobuf_addr_yc_nmux;
808 else
809 common->set_addr = ch0_set_videobuf_addr;
810 }
811
812 /**
813 * vpfe_mmap : It is used to map kernel space buffers into user spaces
814 * @filep: file pointer
815 * @vma: ptr to vm_area_struct
816 */
817 static int vpif_mmap(struct file *filep, struct vm_area_struct *vma)
818 {
819 /* Get the channel object and file handle object */
820 struct vpif_fh *fh = filep->private_data;
821 struct channel_obj *ch = fh->channel;
822 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
823
824 vpif_dbg(2, debug, "vpif_mmap\n");
825
826 return vb2_mmap(&common->buffer_queue, vma);
827 }
828
829 /**
830 * vpif_poll: It is used for select/poll system call
831 * @filep: file pointer
832 * @wait: poll table to wait
833 */
834 static unsigned int vpif_poll(struct file *filep, poll_table * wait)
835 {
836 struct vpif_fh *fh = filep->private_data;
837 struct channel_obj *channel = fh->channel;
838 struct common_obj *common = &(channel->common[VPIF_VIDEO_INDEX]);
839
840 vpif_dbg(2, debug, "vpif_poll\n");
841
842 if (common->started)
843 return vb2_poll(&common->buffer_queue, filep, wait);
844 return 0;
845 }
846
847 /**
848 * vpif_open : vpif open handler
849 * @filep: file ptr
850 *
851 * It creates object of file handle structure and stores it in private_data
852 * member of filepointer
853 */
854 static int vpif_open(struct file *filep)
855 {
856 struct vpif_capture_config *config = vpif_dev->platform_data;
857 struct video_device *vdev = video_devdata(filep);
858 struct common_obj *common;
859 struct video_obj *vid_ch;
860 struct channel_obj *ch;
861 struct vpif_fh *fh;
862 int i;
863
864 vpif_dbg(2, debug, "vpif_open\n");
865
866 ch = video_get_drvdata(vdev);
867
868 vid_ch = &ch->video;
869 common = &ch->common[VPIF_VIDEO_INDEX];
870
871 if (NULL == ch->curr_subdev_info) {
872 /**
873 * search through the sub device to see a registered
874 * sub device and make it as current sub device
875 */
876 for (i = 0; i < config->subdev_count; i++) {
877 if (vpif_obj.sd[i]) {
878 /* the sub device is registered */
879 ch->curr_subdev_info = &config->subdev_info[i];
880 /* make first input as the current input */
881 vid_ch->input_idx = 0;
882 break;
883 }
884 }
885 if (i == config->subdev_count) {
886 vpif_err("No sub device registered\n");
887 return -ENOENT;
888 }
889 }
890
891 /* Allocate memory for the file handle object */
892 fh = kzalloc(sizeof(struct vpif_fh), GFP_KERNEL);
893 if (NULL == fh) {
894 vpif_err("unable to allocate memory for file handle object\n");
895 return -ENOMEM;
896 }
897
898 /* store pointer to fh in private_data member of filep */
899 filep->private_data = fh;
900 fh->channel = ch;
901 fh->initialized = 0;
902 /* If decoder is not initialized. initialize it */
903 if (!ch->initialized) {
904 fh->initialized = 1;
905 ch->initialized = 1;
906 memset(&(ch->vpifparams), 0, sizeof(struct vpif_params));
907 }
908 /* Increment channel usrs counter */
909 ch->usrs++;
910 /* Set io_allowed member to false */
911 fh->io_allowed[VPIF_VIDEO_INDEX] = 0;
912 /* Initialize priority of this instance to default priority */
913 fh->prio = V4L2_PRIORITY_UNSET;
914 v4l2_prio_open(&ch->prio, &fh->prio);
915 return 0;
916 }
917
918 /**
919 * vpif_release : function to clean up file close
920 * @filep: file pointer
921 *
922 * This function deletes buffer queue, frees the buffers and the vpfe file
923 * handle
924 */
925 static int vpif_release(struct file *filep)
926 {
927 struct vpif_fh *fh = filep->private_data;
928 struct channel_obj *ch = fh->channel;
929 struct common_obj *common;
930
931 vpif_dbg(2, debug, "vpif_release\n");
932
933 common = &ch->common[VPIF_VIDEO_INDEX];
934
935 /* if this instance is doing IO */
936 if (fh->io_allowed[VPIF_VIDEO_INDEX]) {
937 /* Reset io_usrs member of channel object */
938 common->io_usrs = 0;
939 /* Disable channel as per its device type and channel id */
940 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
941 enable_channel0(0);
942 channel0_intr_enable(0);
943 }
944 if ((VPIF_CHANNEL1_VIDEO == ch->channel_id) ||
945 (2 == common->started)) {
946 enable_channel1(0);
947 channel1_intr_enable(0);
948 }
949 common->started = 0;
950 /* Free buffers allocated */
951 vb2_queue_release(&common->buffer_queue);
952 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
953 }
954
955 /* Decrement channel usrs counter */
956 ch->usrs--;
957
958 /* Close the priority */
959 v4l2_prio_close(&ch->prio, fh->prio);
960
961 if (fh->initialized)
962 ch->initialized = 0;
963
964 filep->private_data = NULL;
965 kfree(fh);
966 return 0;
967 }
968
969 /**
970 * vpif_reqbufs() - request buffer handler
971 * @file: file ptr
972 * @priv: file handle
973 * @reqbuf: request buffer structure ptr
974 */
975 static int vpif_reqbufs(struct file *file, void *priv,
976 struct v4l2_requestbuffers *reqbuf)
977 {
978 struct vpif_fh *fh = priv;
979 struct channel_obj *ch = fh->channel;
980 struct common_obj *common;
981 u8 index = 0;
982 struct vb2_queue *q;
983
984 vpif_dbg(2, debug, "vpif_reqbufs\n");
985
986 /**
987 * This file handle has not initialized the channel,
988 * It is not allowed to do settings
989 */
990 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id)
991 || (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
992 if (!fh->initialized) {
993 vpif_dbg(1, debug, "Channel Busy\n");
994 return -EBUSY;
995 }
996 }
997
998 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != reqbuf->type || !vpif_dev)
999 return -EINVAL;
1000
1001 index = VPIF_VIDEO_INDEX;
1002
1003 common = &ch->common[index];
1004
1005 if (0 != common->io_usrs)
1006 return -EBUSY;
1007
1008 /* Initialize videobuf2 queue as per the buffer type */
1009 common->alloc_ctx = vb2_dma_contig_init_ctx(vpif_dev);
1010 if (!common->alloc_ctx) {
1011 vpif_err("Failed to get the context\n");
1012 return -EINVAL;
1013 }
1014 q = &common->buffer_queue;
1015 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1016 q->io_modes = VB2_MMAP | VB2_USERPTR;
1017 q->drv_priv = fh;
1018 q->ops = &video_qops;
1019 q->mem_ops = &vb2_dma_contig_memops;
1020 q->buf_struct_size = sizeof(struct vpif_cap_buffer);
1021
1022 vb2_queue_init(q);
1023
1024 /* Set io allowed member of file handle to TRUE */
1025 fh->io_allowed[index] = 1;
1026 /* Increment io usrs member of channel object to 1 */
1027 common->io_usrs = 1;
1028 /* Store type of memory requested in channel object */
1029 common->memory = reqbuf->memory;
1030 INIT_LIST_HEAD(&common->dma_queue);
1031
1032 /* Allocate buffers */
1033 return vb2_reqbufs(&common->buffer_queue, reqbuf);
1034 }
1035
1036 /**
1037 * vpif_querybuf() - query buffer handler
1038 * @file: file ptr
1039 * @priv: file handle
1040 * @buf: v4l2 buffer structure ptr
1041 */
1042 static int vpif_querybuf(struct file *file, void *priv,
1043 struct v4l2_buffer *buf)
1044 {
1045 struct vpif_fh *fh = priv;
1046 struct channel_obj *ch = fh->channel;
1047 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1048
1049 vpif_dbg(2, debug, "vpif_querybuf\n");
1050
1051 if (common->fmt.type != buf->type)
1052 return -EINVAL;
1053
1054 if (common->memory != V4L2_MEMORY_MMAP) {
1055 vpif_dbg(1, debug, "Invalid memory\n");
1056 return -EINVAL;
1057 }
1058
1059 return vb2_querybuf(&common->buffer_queue, buf);
1060 }
1061
1062 /**
1063 * vpif_qbuf() - query buffer handler
1064 * @file: file ptr
1065 * @priv: file handle
1066 * @buf: v4l2 buffer structure ptr
1067 */
1068 static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1069 {
1070
1071 struct vpif_fh *fh = priv;
1072 struct channel_obj *ch = fh->channel;
1073 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1074 struct v4l2_buffer tbuf = *buf;
1075
1076 vpif_dbg(2, debug, "vpif_qbuf\n");
1077
1078 if (common->fmt.type != tbuf.type) {
1079 vpif_err("invalid buffer type\n");
1080 return -EINVAL;
1081 }
1082
1083 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1084 vpif_err("fh io not allowed\n");
1085 return -EACCES;
1086 }
1087
1088 return vb2_qbuf(&common->buffer_queue, buf);
1089 }
1090
1091 /**
1092 * vpif_dqbuf() - query buffer handler
1093 * @file: file ptr
1094 * @priv: file handle
1095 * @buf: v4l2 buffer structure ptr
1096 */
1097 static int vpif_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1098 {
1099 struct vpif_fh *fh = priv;
1100 struct channel_obj *ch = fh->channel;
1101 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1102
1103 vpif_dbg(2, debug, "vpif_dqbuf\n");
1104
1105 return vb2_dqbuf(&common->buffer_queue, buf,
1106 (file->f_flags & O_NONBLOCK));
1107 }
1108
1109 /**
1110 * vpif_streamon() - streamon handler
1111 * @file: file ptr
1112 * @priv: file handle
1113 * @buftype: v4l2 buffer type
1114 */
1115 static int vpif_streamon(struct file *file, void *priv,
1116 enum v4l2_buf_type buftype)
1117 {
1118
1119 struct vpif_fh *fh = priv;
1120 struct channel_obj *ch = fh->channel;
1121 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1122 struct channel_obj *oth_ch = vpif_obj.dev[!ch->channel_id];
1123 struct vpif_params *vpif;
1124 int ret = 0;
1125
1126 vpif_dbg(2, debug, "vpif_streamon\n");
1127
1128 vpif = &ch->vpifparams;
1129
1130 if (buftype != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1131 vpif_dbg(1, debug, "buffer type not supported\n");
1132 return -EINVAL;
1133 }
1134
1135 /* If file handle is not allowed IO, return error */
1136 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1137 vpif_dbg(1, debug, "io not allowed\n");
1138 return -EACCES;
1139 }
1140
1141 /* If Streaming is already started, return error */
1142 if (common->started) {
1143 vpif_dbg(1, debug, "channel->started\n");
1144 return -EBUSY;
1145 }
1146
1147 if ((ch->channel_id == VPIF_CHANNEL0_VIDEO &&
1148 oth_ch->common[VPIF_VIDEO_INDEX].started &&
1149 vpif->std_info.ycmux_mode == 0) ||
1150 ((ch->channel_id == VPIF_CHANNEL1_VIDEO) &&
1151 (2 == oth_ch->common[VPIF_VIDEO_INDEX].started))) {
1152 vpif_dbg(1, debug, "other channel is being used\n");
1153 return -EBUSY;
1154 }
1155
1156 ret = vpif_check_format(ch, &common->fmt.fmt.pix, 0);
1157 if (ret)
1158 return ret;
1159
1160 /* Enable streamon on the sub device */
1161 ret = v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], video,
1162 s_stream, 1);
1163
1164 if (ret && (ret != -ENOIOCTLCMD)) {
1165 vpif_dbg(1, debug, "stream on failed in subdev\n");
1166 return ret;
1167 }
1168
1169 /* Call vb2_streamon to start streaming in videobuf2 */
1170 ret = vb2_streamon(&common->buffer_queue, buftype);
1171 if (ret) {
1172 vpif_dbg(1, debug, "vb2_streamon\n");
1173 return ret;
1174 }
1175
1176 return ret;
1177 }
1178
1179 /**
1180 * vpif_streamoff() - streamoff handler
1181 * @file: file ptr
1182 * @priv: file handle
1183 * @buftype: v4l2 buffer type
1184 */
1185 static int vpif_streamoff(struct file *file, void *priv,
1186 enum v4l2_buf_type buftype)
1187 {
1188
1189 struct vpif_fh *fh = priv;
1190 struct channel_obj *ch = fh->channel;
1191 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1192 int ret;
1193
1194 vpif_dbg(2, debug, "vpif_streamoff\n");
1195
1196 if (buftype != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1197 vpif_dbg(1, debug, "buffer type not supported\n");
1198 return -EINVAL;
1199 }
1200
1201 /* If io is allowed for this file handle, return error */
1202 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1203 vpif_dbg(1, debug, "io not allowed\n");
1204 return -EACCES;
1205 }
1206
1207 /* If streaming is not started, return error */
1208 if (!common->started) {
1209 vpif_dbg(1, debug, "channel->started\n");
1210 return -EINVAL;
1211 }
1212
1213 /* disable channel */
1214 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
1215 enable_channel0(0);
1216 channel0_intr_enable(0);
1217 } else {
1218 enable_channel1(0);
1219 channel1_intr_enable(0);
1220 }
1221
1222 common->started = 0;
1223
1224 ret = v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], video,
1225 s_stream, 0);
1226
1227 if (ret && (ret != -ENOIOCTLCMD))
1228 vpif_dbg(1, debug, "stream off failed in subdev\n");
1229
1230 return vb2_streamoff(&common->buffer_queue, buftype);
1231 }
1232
1233 /**
1234 * vpif_map_sub_device_to_input() - Maps sub device to input
1235 * @ch - ptr to channel
1236 * @config - ptr to capture configuration
1237 * @input_index - Given input index from application
1238 * @sub_device_index - index into sd table
1239 *
1240 * lookup the sub device information for a given input index.
1241 * we report all the inputs to application. inputs table also
1242 * has sub device name for the each input
1243 */
1244 static struct vpif_subdev_info *vpif_map_sub_device_to_input(
1245 struct channel_obj *ch,
1246 struct vpif_capture_config *vpif_cfg,
1247 int input_index,
1248 int *sub_device_index)
1249 {
1250 struct vpif_capture_chan_config *chan_cfg;
1251 struct vpif_subdev_info *subdev_info = NULL;
1252 const char *subdev_name = NULL;
1253 int i;
1254
1255 vpif_dbg(2, debug, "vpif_map_sub_device_to_input\n");
1256
1257 chan_cfg = &vpif_cfg->chan_config[ch->channel_id];
1258
1259 /**
1260 * search through the inputs to find the sub device supporting
1261 * the input
1262 */
1263 for (i = 0; i < chan_cfg->input_count; i++) {
1264 /* For each sub device, loop through input */
1265 if (i == input_index) {
1266 subdev_name = chan_cfg->inputs[i].subdev_name;
1267 break;
1268 }
1269 }
1270
1271 /* if reached maximum. return null */
1272 if (i == chan_cfg->input_count || (NULL == subdev_name))
1273 return subdev_info;
1274
1275 /* loop through the sub device list to get the sub device info */
1276 for (i = 0; i < vpif_cfg->subdev_count; i++) {
1277 subdev_info = &vpif_cfg->subdev_info[i];
1278 if (!strcmp(subdev_info->name, subdev_name))
1279 break;
1280 }
1281
1282 if (i == vpif_cfg->subdev_count)
1283 return subdev_info;
1284
1285 /* check if the sub device is registered */
1286 if (NULL == vpif_obj.sd[i])
1287 return NULL;
1288
1289 *sub_device_index = i;
1290 return subdev_info;
1291 }
1292
1293 /**
1294 * vpif_querystd() - querystd handler
1295 * @file: file ptr
1296 * @priv: file handle
1297 * @std_id: ptr to std id
1298 *
1299 * This function is called to detect standard at the selected input
1300 */
1301 static int vpif_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
1302 {
1303 struct vpif_fh *fh = priv;
1304 struct channel_obj *ch = fh->channel;
1305 int ret = 0;
1306
1307 vpif_dbg(2, debug, "vpif_querystd\n");
1308
1309 /* Call querystd function of decoder device */
1310 ret = v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], video,
1311 querystd, std_id);
1312 if (ret < 0)
1313 vpif_dbg(1, debug, "Failed to set standard for sub devices\n");
1314
1315 return ret;
1316 }
1317
1318 /**
1319 * vpif_g_std() - get STD handler
1320 * @file: file ptr
1321 * @priv: file handle
1322 * @std_id: ptr to std id
1323 */
1324 static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
1325 {
1326 struct vpif_fh *fh = priv;
1327 struct channel_obj *ch = fh->channel;
1328
1329 vpif_dbg(2, debug, "vpif_g_std\n");
1330
1331 *std = ch->video.stdid;
1332 return 0;
1333 }
1334
1335 /**
1336 * vpif_s_std() - set STD handler
1337 * @file: file ptr
1338 * @priv: file handle
1339 * @std_id: ptr to std id
1340 */
1341 static int vpif_s_std(struct file *file, void *priv, v4l2_std_id *std_id)
1342 {
1343 struct vpif_fh *fh = priv;
1344 struct channel_obj *ch = fh->channel;
1345 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1346 int ret = 0;
1347
1348 vpif_dbg(2, debug, "vpif_s_std\n");
1349
1350 if (common->started) {
1351 vpif_err("streaming in progress\n");
1352 return -EBUSY;
1353 }
1354
1355 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1356 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1357 if (!fh->initialized) {
1358 vpif_dbg(1, debug, "Channel Busy\n");
1359 return -EBUSY;
1360 }
1361 }
1362
1363 ret = v4l2_prio_check(&ch->prio, fh->prio);
1364 if (0 != ret)
1365 return ret;
1366
1367 fh->initialized = 1;
1368
1369 /* Call encoder subdevice function to set the standard */
1370 ch->video.stdid = *std_id;
1371 ch->video.dv_preset = V4L2_DV_INVALID;
1372 memset(&ch->video.bt_timings, 0, sizeof(ch->video.bt_timings));
1373
1374 /* Get the information about the standard */
1375 if (vpif_update_std_info(ch)) {
1376 vpif_err("Error getting the standard info\n");
1377 return -EINVAL;
1378 }
1379
1380 /* Configure the default format information */
1381 vpif_config_format(ch);
1382
1383 /* set standard in the sub device */
1384 ret = v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], core,
1385 s_std, *std_id);
1386 if (ret < 0)
1387 vpif_dbg(1, debug, "Failed to set standard for sub devices\n");
1388 return ret;
1389 }
1390
1391 /**
1392 * vpif_enum_input() - ENUMINPUT handler
1393 * @file: file ptr
1394 * @priv: file handle
1395 * @input: ptr to input structure
1396 */
1397 static int vpif_enum_input(struct file *file, void *priv,
1398 struct v4l2_input *input)
1399 {
1400
1401 struct vpif_capture_config *config = vpif_dev->platform_data;
1402 struct vpif_capture_chan_config *chan_cfg;
1403 struct vpif_fh *fh = priv;
1404 struct channel_obj *ch = fh->channel;
1405
1406 chan_cfg = &config->chan_config[ch->channel_id];
1407
1408 if (input->index >= chan_cfg->input_count) {
1409 vpif_dbg(1, debug, "Invalid input index\n");
1410 return -EINVAL;
1411 }
1412
1413 memcpy(input, &chan_cfg->inputs[input->index].input,
1414 sizeof(*input));
1415 return 0;
1416 }
1417
1418 /**
1419 * vpif_g_input() - Get INPUT handler
1420 * @file: file ptr
1421 * @priv: file handle
1422 * @index: ptr to input index
1423 */
1424 static int vpif_g_input(struct file *file, void *priv, unsigned int *index)
1425 {
1426 struct vpif_fh *fh = priv;
1427 struct channel_obj *ch = fh->channel;
1428 struct video_obj *vid_ch = &ch->video;
1429
1430 *index = vid_ch->input_idx;
1431
1432 return 0;
1433 }
1434
1435 /**
1436 * vpif_s_input() - Set INPUT handler
1437 * @file: file ptr
1438 * @priv: file handle
1439 * @index: input index
1440 */
1441 static int vpif_s_input(struct file *file, void *priv, unsigned int index)
1442 {
1443 struct vpif_capture_config *config = vpif_dev->platform_data;
1444 struct vpif_capture_chan_config *chan_cfg;
1445 struct vpif_fh *fh = priv;
1446 struct channel_obj *ch = fh->channel;
1447 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1448 struct video_obj *vid_ch = &ch->video;
1449 struct vpif_subdev_info *subdev_info;
1450 int ret = 0, sd_index = 0;
1451 u32 input = 0, output = 0;
1452
1453 chan_cfg = &config->chan_config[ch->channel_id];
1454
1455 if (common->started) {
1456 vpif_err("Streaming in progress\n");
1457 return -EBUSY;
1458 }
1459
1460 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1461 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1462 if (!fh->initialized) {
1463 vpif_dbg(1, debug, "Channel Busy\n");
1464 return -EBUSY;
1465 }
1466 }
1467
1468 ret = v4l2_prio_check(&ch->prio, fh->prio);
1469 if (0 != ret)
1470 return ret;
1471
1472 fh->initialized = 1;
1473 subdev_info = vpif_map_sub_device_to_input(ch, config, index,
1474 &sd_index);
1475 if (NULL == subdev_info) {
1476 vpif_dbg(1, debug,
1477 "couldn't lookup sub device for the input index\n");
1478 return -EINVAL;
1479 }
1480
1481 /* first setup input path from sub device to vpif */
1482 if (config->setup_input_path) {
1483 ret = config->setup_input_path(ch->channel_id,
1484 subdev_info->name);
1485 if (ret < 0) {
1486 vpif_dbg(1, debug, "couldn't setup input path for the"
1487 " sub device %s, for input index %d\n",
1488 subdev_info->name, index);
1489 return ret;
1490 }
1491 }
1492
1493 if (subdev_info->can_route) {
1494 input = subdev_info->input;
1495 output = subdev_info->output;
1496 ret = v4l2_subdev_call(vpif_obj.sd[sd_index], video, s_routing,
1497 input, output, 0);
1498 if (ret < 0) {
1499 vpif_dbg(1, debug, "Failed to set input\n");
1500 return ret;
1501 }
1502 }
1503 vid_ch->input_idx = index;
1504 ch->curr_subdev_info = subdev_info;
1505 ch->curr_sd_index = sd_index;
1506 /* copy interface parameters to vpif */
1507 ch->vpifparams.iface = subdev_info->vpif_if;
1508
1509 /* update tvnorms from the sub device input info */
1510 ch->video_dev->tvnorms = chan_cfg->inputs[index].input.std;
1511 return ret;
1512 }
1513
1514 /**
1515 * vpif_enum_fmt_vid_cap() - ENUM_FMT handler
1516 * @file: file ptr
1517 * @priv: file handle
1518 * @index: input index
1519 */
1520 static int vpif_enum_fmt_vid_cap(struct file *file, void *priv,
1521 struct v4l2_fmtdesc *fmt)
1522 {
1523 struct vpif_fh *fh = priv;
1524 struct channel_obj *ch = fh->channel;
1525
1526 if (fmt->index != 0) {
1527 vpif_dbg(1, debug, "Invalid format index\n");
1528 return -EINVAL;
1529 }
1530
1531 /* Fill in the information about format */
1532 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER) {
1533 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1534 strcpy(fmt->description, "Raw Mode -Bayer Pattern GrRBGb");
1535 fmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
1536 } else {
1537 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1538 strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
1539 fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
1540 }
1541 return 0;
1542 }
1543
1544 /**
1545 * vpif_try_fmt_vid_cap() - TRY_FMT handler
1546 * @file: file ptr
1547 * @priv: file handle
1548 * @fmt: ptr to v4l2 format structure
1549 */
1550 static int vpif_try_fmt_vid_cap(struct file *file, void *priv,
1551 struct v4l2_format *fmt)
1552 {
1553 struct vpif_fh *fh = priv;
1554 struct channel_obj *ch = fh->channel;
1555 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
1556
1557 return vpif_check_format(ch, pixfmt, 1);
1558 }
1559
1560
1561 /**
1562 * vpif_g_fmt_vid_cap() - Set INPUT handler
1563 * @file: file ptr
1564 * @priv: file handle
1565 * @fmt: ptr to v4l2 format structure
1566 */
1567 static int vpif_g_fmt_vid_cap(struct file *file, void *priv,
1568 struct v4l2_format *fmt)
1569 {
1570 struct vpif_fh *fh = priv;
1571 struct channel_obj *ch = fh->channel;
1572 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1573
1574 /* Check the validity of the buffer type */
1575 if (common->fmt.type != fmt->type)
1576 return -EINVAL;
1577
1578 /* Fill in the information about format */
1579 *fmt = common->fmt;
1580 return 0;
1581 }
1582
1583 /**
1584 * vpif_s_fmt_vid_cap() - Set FMT handler
1585 * @file: file ptr
1586 * @priv: file handle
1587 * @fmt: ptr to v4l2 format structure
1588 */
1589 static int vpif_s_fmt_vid_cap(struct file *file, void *priv,
1590 struct v4l2_format *fmt)
1591 {
1592 struct vpif_fh *fh = priv;
1593 struct channel_obj *ch = fh->channel;
1594 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1595 struct v4l2_pix_format *pixfmt;
1596 int ret = 0;
1597
1598 vpif_dbg(2, debug, "%s\n", __func__);
1599
1600 /* If streaming is started, return error */
1601 if (common->started) {
1602 vpif_dbg(1, debug, "Streaming is started\n");
1603 return -EBUSY;
1604 }
1605
1606 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1607 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1608 if (!fh->initialized) {
1609 vpif_dbg(1, debug, "Channel Busy\n");
1610 return -EBUSY;
1611 }
1612 }
1613
1614 ret = v4l2_prio_check(&ch->prio, fh->prio);
1615 if (0 != ret)
1616 return ret;
1617
1618 fh->initialized = 1;
1619
1620 pixfmt = &fmt->fmt.pix;
1621 /* Check for valid field format */
1622 ret = vpif_check_format(ch, pixfmt, 0);
1623
1624 if (ret)
1625 return ret;
1626 /* store the format in the channel object */
1627 common->fmt = *fmt;
1628 return 0;
1629 }
1630
1631 /**
1632 * vpif_querycap() - QUERYCAP handler
1633 * @file: file ptr
1634 * @priv: file handle
1635 * @cap: ptr to v4l2_capability structure
1636 */
1637 static int vpif_querycap(struct file *file, void *priv,
1638 struct v4l2_capability *cap)
1639 {
1640 struct vpif_capture_config *config = vpif_dev->platform_data;
1641
1642 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1643 strlcpy(cap->driver, "vpif capture", sizeof(cap->driver));
1644 strlcpy(cap->bus_info, "VPIF Platform", sizeof(cap->bus_info));
1645 strlcpy(cap->card, config->card_name, sizeof(cap->card));
1646
1647 return 0;
1648 }
1649
1650 /**
1651 * vpif_g_priority() - get priority handler
1652 * @file: file ptr
1653 * @priv: file handle
1654 * @prio: ptr to v4l2_priority structure
1655 */
1656 static int vpif_g_priority(struct file *file, void *priv,
1657 enum v4l2_priority *prio)
1658 {
1659 struct vpif_fh *fh = priv;
1660 struct channel_obj *ch = fh->channel;
1661
1662 *prio = v4l2_prio_max(&ch->prio);
1663
1664 return 0;
1665 }
1666
1667 /**
1668 * vpif_s_priority() - set priority handler
1669 * @file: file ptr
1670 * @priv: file handle
1671 * @prio: ptr to v4l2_priority structure
1672 */
1673 static int vpif_s_priority(struct file *file, void *priv, enum v4l2_priority p)
1674 {
1675 struct vpif_fh *fh = priv;
1676 struct channel_obj *ch = fh->channel;
1677
1678 return v4l2_prio_change(&ch->prio, &fh->prio, p);
1679 }
1680
1681 /**
1682 * vpif_cropcap() - cropcap handler
1683 * @file: file ptr
1684 * @priv: file handle
1685 * @crop: ptr to v4l2_cropcap structure
1686 */
1687 static int vpif_cropcap(struct file *file, void *priv,
1688 struct v4l2_cropcap *crop)
1689 {
1690 struct vpif_fh *fh = priv;
1691 struct channel_obj *ch = fh->channel;
1692 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1693
1694 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != crop->type)
1695 return -EINVAL;
1696
1697 crop->bounds.left = 0;
1698 crop->bounds.top = 0;
1699 crop->bounds.height = common->height;
1700 crop->bounds.width = common->width;
1701 crop->defrect = crop->bounds;
1702 return 0;
1703 }
1704
1705 /**
1706 * vpif_enum_dv_presets() - ENUM_DV_PRESETS handler
1707 * @file: file ptr
1708 * @priv: file handle
1709 * @preset: input preset
1710 */
1711 static int vpif_enum_dv_presets(struct file *file, void *priv,
1712 struct v4l2_dv_enum_preset *preset)
1713 {
1714 struct vpif_fh *fh = priv;
1715 struct channel_obj *ch = fh->channel;
1716
1717 return v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index],
1718 video, enum_dv_presets, preset);
1719 }
1720
1721 /**
1722 * vpif_query_dv_presets() - QUERY_DV_PRESET handler
1723 * @file: file ptr
1724 * @priv: file handle
1725 * @preset: input preset
1726 */
1727 static int vpif_query_dv_preset(struct file *file, void *priv,
1728 struct v4l2_dv_preset *preset)
1729 {
1730 struct vpif_fh *fh = priv;
1731 struct channel_obj *ch = fh->channel;
1732
1733 return v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index],
1734 video, query_dv_preset, preset);
1735 }
1736 /**
1737 * vpif_s_dv_presets() - S_DV_PRESETS handler
1738 * @file: file ptr
1739 * @priv: file handle
1740 * @preset: input preset
1741 */
1742 static int vpif_s_dv_preset(struct file *file, void *priv,
1743 struct v4l2_dv_preset *preset)
1744 {
1745 struct vpif_fh *fh = priv;
1746 struct channel_obj *ch = fh->channel;
1747 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1748 int ret = 0;
1749
1750 if (common->started) {
1751 vpif_dbg(1, debug, "streaming in progress\n");
1752 return -EBUSY;
1753 }
1754
1755 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1756 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1757 if (!fh->initialized) {
1758 vpif_dbg(1, debug, "Channel Busy\n");
1759 return -EBUSY;
1760 }
1761 }
1762
1763 ret = v4l2_prio_check(&ch->prio, fh->prio);
1764 if (ret)
1765 return ret;
1766
1767 fh->initialized = 1;
1768
1769 /* Call encoder subdevice function to set the standard */
1770 if (mutex_lock_interruptible(&common->lock))
1771 return -ERESTARTSYS;
1772
1773 ch->video.dv_preset = preset->preset;
1774 ch->video.stdid = V4L2_STD_UNKNOWN;
1775 memset(&ch->video.bt_timings, 0, sizeof(ch->video.bt_timings));
1776
1777 /* Get the information about the standard */
1778 if (vpif_update_std_info(ch)) {
1779 vpif_dbg(1, debug, "Error getting the standard info\n");
1780 ret = -EINVAL;
1781 } else {
1782 /* Configure the default format information */
1783 vpif_config_format(ch);
1784
1785 ret = v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index],
1786 video, s_dv_preset, preset);
1787 }
1788
1789 mutex_unlock(&common->lock);
1790
1791 return ret;
1792 }
1793 /**
1794 * vpif_g_dv_presets() - G_DV_PRESETS handler
1795 * @file: file ptr
1796 * @priv: file handle
1797 * @preset: input preset
1798 */
1799 static int vpif_g_dv_preset(struct file *file, void *priv,
1800 struct v4l2_dv_preset *preset)
1801 {
1802 struct vpif_fh *fh = priv;
1803 struct channel_obj *ch = fh->channel;
1804
1805 preset->preset = ch->video.dv_preset;
1806
1807 return 0;
1808 }
1809
1810 /**
1811 * vpif_s_dv_timings() - S_DV_TIMINGS handler
1812 * @file: file ptr
1813 * @priv: file handle
1814 * @timings: digital video timings
1815 */
1816 static int vpif_s_dv_timings(struct file *file, void *priv,
1817 struct v4l2_dv_timings *timings)
1818 {
1819 struct vpif_fh *fh = priv;
1820 struct channel_obj *ch = fh->channel;
1821 struct vpif_params *vpifparams = &ch->vpifparams;
1822 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
1823 struct video_obj *vid_ch = &ch->video;
1824 struct v4l2_bt_timings *bt = &vid_ch->bt_timings;
1825 int ret;
1826
1827 if (timings->type != V4L2_DV_BT_656_1120) {
1828 vpif_dbg(2, debug, "Timing type not defined\n");
1829 return -EINVAL;
1830 }
1831
1832 /* Configure subdevice timings, if any */
1833 ret = v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index],
1834 video, s_dv_timings, timings);
1835 if (ret == -ENOIOCTLCMD) {
1836 vpif_dbg(2, debug, "Custom DV timings not supported by "
1837 "subdevice\n");
1838 return -EINVAL;
1839 }
1840 if (ret < 0) {
1841 vpif_dbg(2, debug, "Error setting custom DV timings\n");
1842 return ret;
1843 }
1844
1845 if (!(timings->bt.width && timings->bt.height &&
1846 (timings->bt.hbackporch ||
1847 timings->bt.hfrontporch ||
1848 timings->bt.hsync) &&
1849 timings->bt.vfrontporch &&
1850 (timings->bt.vbackporch ||
1851 timings->bt.vsync))) {
1852 vpif_dbg(2, debug, "Timings for width, height, "
1853 "horizontal back porch, horizontal sync, "
1854 "horizontal front porch, vertical back porch, "
1855 "vertical sync and vertical back porch "
1856 "must be defined\n");
1857 return -EINVAL;
1858 }
1859
1860 *bt = timings->bt;
1861
1862 /* Configure video port timings */
1863
1864 std_info->eav2sav = bt->hbackporch + bt->hfrontporch +
1865 bt->hsync - 8;
1866 std_info->sav2eav = bt->width;
1867
1868 std_info->l1 = 1;
1869 std_info->l3 = bt->vsync + bt->vbackporch + 1;
1870
1871 if (bt->interlaced) {
1872 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
1873 std_info->vsize = bt->height * 2 +
1874 bt->vfrontporch + bt->vsync + bt->vbackporch +
1875 bt->il_vfrontporch + bt->il_vsync +
1876 bt->il_vbackporch;
1877 std_info->l5 = std_info->vsize/2 -
1878 (bt->vfrontporch - 1);
1879 std_info->l7 = std_info->vsize/2 + 1;
1880 std_info->l9 = std_info->l7 + bt->il_vsync +
1881 bt->il_vbackporch + 1;
1882 std_info->l11 = std_info->vsize -
1883 (bt->il_vfrontporch - 1);
1884 } else {
1885 vpif_dbg(2, debug, "Required timing values for "
1886 "interlaced BT format missing\n");
1887 return -EINVAL;
1888 }
1889 } else {
1890 std_info->vsize = bt->height + bt->vfrontporch +
1891 bt->vsync + bt->vbackporch;
1892 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
1893 }
1894 strncpy(std_info->name, "Custom timings BT656/1120", VPIF_MAX_NAME);
1895 std_info->width = bt->width;
1896 std_info->height = bt->height;
1897 std_info->frm_fmt = bt->interlaced ? 0 : 1;
1898 std_info->ycmux_mode = 0;
1899 std_info->capture_format = 0;
1900 std_info->vbi_supported = 0;
1901 std_info->hd_sd = 1;
1902 std_info->stdid = 0;
1903 std_info->dv_preset = V4L2_DV_INVALID;
1904
1905 vid_ch->stdid = 0;
1906 vid_ch->dv_preset = V4L2_DV_INVALID;
1907 return 0;
1908 }
1909
1910 /**
1911 * vpif_g_dv_timings() - G_DV_TIMINGS handler
1912 * @file: file ptr
1913 * @priv: file handle
1914 * @timings: digital video timings
1915 */
1916 static int vpif_g_dv_timings(struct file *file, void *priv,
1917 struct v4l2_dv_timings *timings)
1918 {
1919 struct vpif_fh *fh = priv;
1920 struct channel_obj *ch = fh->channel;
1921 struct video_obj *vid_ch = &ch->video;
1922 struct v4l2_bt_timings *bt = &vid_ch->bt_timings;
1923
1924 timings->bt = *bt;
1925
1926 return 0;
1927 }
1928
1929 /*
1930 * vpif_g_chip_ident() - Identify the chip
1931 * @file: file ptr
1932 * @priv: file handle
1933 * @chip: chip identity
1934 *
1935 * Returns zero or -EINVAL if read operations fails.
1936 */
1937 static int vpif_g_chip_ident(struct file *file, void *priv,
1938 struct v4l2_dbg_chip_ident *chip)
1939 {
1940 chip->ident = V4L2_IDENT_NONE;
1941 chip->revision = 0;
1942 if (chip->match.type != V4L2_CHIP_MATCH_I2C_DRIVER &&
1943 chip->match.type != V4L2_CHIP_MATCH_I2C_ADDR) {
1944 vpif_dbg(2, debug, "match_type is invalid.\n");
1945 return -EINVAL;
1946 }
1947
1948 return v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 0, core,
1949 g_chip_ident, chip);
1950 }
1951
1952 #ifdef CONFIG_VIDEO_ADV_DEBUG
1953 /*
1954 * vpif_dbg_g_register() - Read register
1955 * @file: file ptr
1956 * @priv: file handle
1957 * @reg: register to be read
1958 *
1959 * Debugging only
1960 * Returns zero or -EINVAL if read operations fails.
1961 */
1962 static int vpif_dbg_g_register(struct file *file, void *priv,
1963 struct v4l2_dbg_register *reg){
1964 struct vpif_fh *fh = priv;
1965 struct channel_obj *ch = fh->channel;
1966
1967 return v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], core,
1968 g_register, reg);
1969 }
1970
1971 /*
1972 * vpif_dbg_s_register() - Write to register
1973 * @file: file ptr
1974 * @priv: file handle
1975 * @reg: register to be modified
1976 *
1977 * Debugging only
1978 * Returns zero or -EINVAL if write operations fails.
1979 */
1980 static int vpif_dbg_s_register(struct file *file, void *priv,
1981 struct v4l2_dbg_register *reg){
1982 struct vpif_fh *fh = priv;
1983 struct channel_obj *ch = fh->channel;
1984
1985 return v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], core,
1986 s_register, reg);
1987 }
1988 #endif
1989
1990 /*
1991 * vpif_log_status() - Status information
1992 * @file: file ptr
1993 * @priv: file handle
1994 *
1995 * Returns zero.
1996 */
1997 static int vpif_log_status(struct file *filep, void *priv)
1998 {
1999 /* status for sub devices */
2000 v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
2001
2002 return 0;
2003 }
2004
2005 /* vpif capture ioctl operations */
2006 static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
2007 .vidioc_querycap = vpif_querycap,
2008 .vidioc_g_priority = vpif_g_priority,
2009 .vidioc_s_priority = vpif_s_priority,
2010 .vidioc_enum_fmt_vid_cap = vpif_enum_fmt_vid_cap,
2011 .vidioc_g_fmt_vid_cap = vpif_g_fmt_vid_cap,
2012 .vidioc_s_fmt_vid_cap = vpif_s_fmt_vid_cap,
2013 .vidioc_try_fmt_vid_cap = vpif_try_fmt_vid_cap,
2014 .vidioc_enum_input = vpif_enum_input,
2015 .vidioc_s_input = vpif_s_input,
2016 .vidioc_g_input = vpif_g_input,
2017 .vidioc_reqbufs = vpif_reqbufs,
2018 .vidioc_querybuf = vpif_querybuf,
2019 .vidioc_querystd = vpif_querystd,
2020 .vidioc_s_std = vpif_s_std,
2021 .vidioc_g_std = vpif_g_std,
2022 .vidioc_qbuf = vpif_qbuf,
2023 .vidioc_dqbuf = vpif_dqbuf,
2024 .vidioc_streamon = vpif_streamon,
2025 .vidioc_streamoff = vpif_streamoff,
2026 .vidioc_cropcap = vpif_cropcap,
2027 .vidioc_enum_dv_presets = vpif_enum_dv_presets,
2028 .vidioc_s_dv_preset = vpif_s_dv_preset,
2029 .vidioc_g_dv_preset = vpif_g_dv_preset,
2030 .vidioc_query_dv_preset = vpif_query_dv_preset,
2031 .vidioc_s_dv_timings = vpif_s_dv_timings,
2032 .vidioc_g_dv_timings = vpif_g_dv_timings,
2033 .vidioc_g_chip_ident = vpif_g_chip_ident,
2034 #ifdef CONFIG_VIDEO_ADV_DEBUG
2035 .vidioc_g_register = vpif_dbg_g_register,
2036 .vidioc_s_register = vpif_dbg_s_register,
2037 #endif
2038 .vidioc_log_status = vpif_log_status,
2039 };
2040
2041 /* vpif file operations */
2042 static struct v4l2_file_operations vpif_fops = {
2043 .owner = THIS_MODULE,
2044 .open = vpif_open,
2045 .release = vpif_release,
2046 .unlocked_ioctl = video_ioctl2,
2047 .mmap = vpif_mmap,
2048 .poll = vpif_poll
2049 };
2050
2051 /* vpif video template */
2052 static struct video_device vpif_video_template = {
2053 .name = "vpif",
2054 .fops = &vpif_fops,
2055 .minor = -1,
2056 .ioctl_ops = &vpif_ioctl_ops,
2057 };
2058
2059 /**
2060 * initialize_vpif() - Initialize vpif data structures
2061 *
2062 * Allocate memory for data structures and initialize them
2063 */
2064 static int initialize_vpif(void)
2065 {
2066 int err = 0, i, j;
2067 int free_channel_objects_index;
2068
2069 /* Default number of buffers should be 3 */
2070 if ((ch0_numbuffers > 0) &&
2071 (ch0_numbuffers < config_params.min_numbuffers))
2072 ch0_numbuffers = config_params.min_numbuffers;
2073 if ((ch1_numbuffers > 0) &&
2074 (ch1_numbuffers < config_params.min_numbuffers))
2075 ch1_numbuffers = config_params.min_numbuffers;
2076
2077 /* Set buffer size to min buffers size if it is invalid */
2078 if (ch0_bufsize < config_params.min_bufsize[VPIF_CHANNEL0_VIDEO])
2079 ch0_bufsize =
2080 config_params.min_bufsize[VPIF_CHANNEL0_VIDEO];
2081 if (ch1_bufsize < config_params.min_bufsize[VPIF_CHANNEL1_VIDEO])
2082 ch1_bufsize =
2083 config_params.min_bufsize[VPIF_CHANNEL1_VIDEO];
2084
2085 config_params.numbuffers[VPIF_CHANNEL0_VIDEO] = ch0_numbuffers;
2086 config_params.numbuffers[VPIF_CHANNEL1_VIDEO] = ch1_numbuffers;
2087 if (ch0_numbuffers) {
2088 config_params.channel_bufsize[VPIF_CHANNEL0_VIDEO]
2089 = ch0_bufsize;
2090 }
2091 if (ch1_numbuffers) {
2092 config_params.channel_bufsize[VPIF_CHANNEL1_VIDEO]
2093 = ch1_bufsize;
2094 }
2095
2096 /* Allocate memory for six channel objects */
2097 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2098 vpif_obj.dev[i] =
2099 kzalloc(sizeof(*vpif_obj.dev[i]), GFP_KERNEL);
2100 /* If memory allocation fails, return error */
2101 if (!vpif_obj.dev[i]) {
2102 free_channel_objects_index = i;
2103 err = -ENOMEM;
2104 goto vpif_init_free_channel_objects;
2105 }
2106 }
2107 return 0;
2108
2109 vpif_init_free_channel_objects:
2110 for (j = 0; j < free_channel_objects_index; j++)
2111 kfree(vpif_obj.dev[j]);
2112 return err;
2113 }
2114
2115 /**
2116 * vpif_probe : This function probes the vpif capture driver
2117 * @pdev: platform device pointer
2118 *
2119 * This creates device entries by register itself to the V4L2 driver and
2120 * initializes fields of each channel objects
2121 */
2122 static __init int vpif_probe(struct platform_device *pdev)
2123 {
2124 struct vpif_subdev_info *subdevdata;
2125 struct vpif_capture_config *config;
2126 int i, j, k, m, q, err;
2127 struct i2c_adapter *i2c_adap;
2128 struct channel_obj *ch;
2129 struct common_obj *common;
2130 struct video_device *vfd;
2131 struct resource *res;
2132 int subdev_count;
2133 size_t size;
2134
2135 vpif_dev = &pdev->dev;
2136
2137 err = initialize_vpif();
2138 if (err) {
2139 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
2140 return err;
2141 }
2142
2143 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
2144 if (err) {
2145 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
2146 return err;
2147 }
2148
2149 k = 0;
2150 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, k))) {
2151 for (i = res->start; i <= res->end; i++) {
2152 if (request_irq(i, vpif_channel_isr, IRQF_SHARED,
2153 "VPIF_Capture",
2154 (void *)(&vpif_obj.dev[k]->channel_id))) {
2155 err = -EBUSY;
2156 i--;
2157 goto vpif_int_err;
2158 }
2159 }
2160 k++;
2161 }
2162
2163 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2164 /* Get the pointer to the channel object */
2165 ch = vpif_obj.dev[i];
2166 /* Allocate memory for video device */
2167 vfd = video_device_alloc();
2168 if (NULL == vfd) {
2169 for (j = 0; j < i; j++) {
2170 ch = vpif_obj.dev[j];
2171 video_device_release(ch->video_dev);
2172 }
2173 err = -ENOMEM;
2174 goto vpif_dev_alloc_err;
2175 }
2176
2177 /* Initialize field of video device */
2178 *vfd = vpif_video_template;
2179 vfd->v4l2_dev = &vpif_obj.v4l2_dev;
2180 vfd->release = video_device_release;
2181 snprintf(vfd->name, sizeof(vfd->name),
2182 "VPIF_Capture_DRIVER_V%s",
2183 VPIF_CAPTURE_VERSION);
2184 /* Set video_dev to the video device */
2185 ch->video_dev = vfd;
2186 }
2187
2188 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2189 if (res) {
2190 size = resource_size(res);
2191 /* The resources are divided into two equal memory and when we
2192 * have HD output we can add them together
2193 */
2194 for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
2195 ch = vpif_obj.dev[j];
2196 ch->channel_id = j;
2197 /* only enabled if second resource exists */
2198 config_params.video_limit[ch->channel_id] = 0;
2199 if (size)
2200 config_params.video_limit[ch->channel_id] =
2201 size/2;
2202 }
2203 }
2204
2205 for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
2206 ch = vpif_obj.dev[j];
2207 ch->channel_id = j;
2208 common = &(ch->common[VPIF_VIDEO_INDEX]);
2209 spin_lock_init(&common->irqlock);
2210 mutex_init(&common->lock);
2211 /* Locking in file operations other than ioctl should be done
2212 by the driver, not the V4L2 core.
2213 This driver needs auditing so that this flag can be removed. */
2214 set_bit(V4L2_FL_LOCK_ALL_FOPS, &ch->video_dev->flags);
2215 ch->video_dev->lock = &common->lock;
2216 /* Initialize prio member of channel object */
2217 v4l2_prio_init(&ch->prio);
2218 err = video_register_device(ch->video_dev,
2219 VFL_TYPE_GRABBER, (j ? 1 : 0));
2220 if (err)
2221 goto probe_out;
2222
2223 video_set_drvdata(ch->video_dev, ch);
2224
2225 }
2226
2227 i2c_adap = i2c_get_adapter(1);
2228 config = pdev->dev.platform_data;
2229
2230 subdev_count = config->subdev_count;
2231 vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
2232 GFP_KERNEL);
2233 if (vpif_obj.sd == NULL) {
2234 vpif_err("unable to allocate memory for subdevice pointers\n");
2235 err = -ENOMEM;
2236 goto probe_out;
2237 }
2238
2239 for (i = 0; i < subdev_count; i++) {
2240 subdevdata = &config->subdev_info[i];
2241 vpif_obj.sd[i] =
2242 v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
2243 i2c_adap,
2244 &subdevdata->board_info,
2245 NULL);
2246
2247 if (!vpif_obj.sd[i]) {
2248 vpif_err("Error registering v4l2 subdevice\n");
2249 goto probe_subdev_out;
2250 }
2251 v4l2_info(&vpif_obj.v4l2_dev, "registered sub device %s\n",
2252 subdevdata->name);
2253
2254 if (vpif_obj.sd[i])
2255 vpif_obj.sd[i]->grp_id = 1 << i;
2256 }
2257
2258 v4l2_info(&vpif_obj.v4l2_dev, "VPIF capture driver initialized\n");
2259 return 0;
2260
2261 probe_subdev_out:
2262 /* free sub devices memory */
2263 kfree(vpif_obj.sd);
2264
2265 j = VPIF_CAPTURE_MAX_DEVICES;
2266 probe_out:
2267 for (k = 0; k < j; k++) {
2268 /* Get the pointer to the channel object */
2269 ch = vpif_obj.dev[k];
2270 /* Unregister video device */
2271 video_unregister_device(ch->video_dev);
2272 }
2273
2274 vpif_dev_alloc_err:
2275 k = VPIF_CAPTURE_MAX_DEVICES-1;
2276 res = platform_get_resource(pdev, IORESOURCE_IRQ, k);
2277 i = res->end;
2278
2279 vpif_int_err:
2280 for (q = k; q >= 0; q--) {
2281 for (m = i; m >= (int)res->start; m--)
2282 free_irq(m, (void *)(&vpif_obj.dev[q]->channel_id));
2283
2284 res = platform_get_resource(pdev, IORESOURCE_IRQ, q-1);
2285 if (res)
2286 i = res->end;
2287 }
2288 v4l2_device_unregister(&vpif_obj.v4l2_dev);
2289 return err;
2290 }
2291
2292 /**
2293 * vpif_remove() - driver remove handler
2294 * @device: ptr to platform device structure
2295 *
2296 * The vidoe device is unregistered
2297 */
2298 static int vpif_remove(struct platform_device *device)
2299 {
2300 int i;
2301 struct channel_obj *ch;
2302
2303 v4l2_device_unregister(&vpif_obj.v4l2_dev);
2304
2305 /* un-register device */
2306 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2307 /* Get the pointer to the channel object */
2308 ch = vpif_obj.dev[i];
2309 /* Unregister video device */
2310 video_unregister_device(ch->video_dev);
2311 }
2312 return 0;
2313 }
2314
2315 #ifdef CONFIG_PM
2316 /**
2317 * vpif_suspend: vpif device suspend
2318 */
2319 static int vpif_suspend(struct device *dev)
2320 {
2321
2322 struct common_obj *common;
2323 struct channel_obj *ch;
2324 int i;
2325
2326 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2327 /* Get the pointer to the channel object */
2328 ch = vpif_obj.dev[i];
2329 common = &ch->common[VPIF_VIDEO_INDEX];
2330 mutex_lock(&common->lock);
2331 if (ch->usrs && common->io_usrs) {
2332 /* Disable channel */
2333 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
2334 enable_channel0(0);
2335 channel0_intr_enable(0);
2336 }
2337 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
2338 common->started == 2) {
2339 enable_channel1(0);
2340 channel1_intr_enable(0);
2341 }
2342 }
2343 mutex_unlock(&common->lock);
2344 }
2345
2346 return 0;
2347 }
2348
2349 /*
2350 * vpif_resume: vpif device suspend
2351 */
2352 static int vpif_resume(struct device *dev)
2353 {
2354 struct common_obj *common;
2355 struct channel_obj *ch;
2356 int i;
2357
2358 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2359 /* Get the pointer to the channel object */
2360 ch = vpif_obj.dev[i];
2361 common = &ch->common[VPIF_VIDEO_INDEX];
2362 mutex_lock(&common->lock);
2363 if (ch->usrs && common->io_usrs) {
2364 /* Disable channel */
2365 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
2366 enable_channel0(1);
2367 channel0_intr_enable(1);
2368 }
2369 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
2370 common->started == 2) {
2371 enable_channel1(1);
2372 channel1_intr_enable(1);
2373 }
2374 }
2375 mutex_unlock(&common->lock);
2376 }
2377
2378 return 0;
2379 }
2380
2381 static const struct dev_pm_ops vpif_dev_pm_ops = {
2382 .suspend = vpif_suspend,
2383 .resume = vpif_resume,
2384 };
2385
2386 #define vpif_pm_ops (&vpif_dev_pm_ops)
2387 #else
2388 #define vpif_pm_ops NULL
2389 #endif
2390
2391 static __refdata struct platform_driver vpif_driver = {
2392 .driver = {
2393 .name = "vpif_capture",
2394 .owner = THIS_MODULE,
2395 .pm = vpif_pm_ops,
2396 },
2397 .probe = vpif_probe,
2398 .remove = vpif_remove,
2399 };
2400
2401 /**
2402 * vpif_init: initialize the vpif driver
2403 *
2404 * This function registers device and driver to the kernel, requests irq
2405 * handler and allocates memory
2406 * for channel objects
2407 */
2408 static __init int vpif_init(void)
2409 {
2410 return platform_driver_register(&vpif_driver);
2411 }
2412
2413 /**
2414 * vpif_cleanup : This function clean up the vpif capture resources
2415 *
2416 * This will un-registers device and driver to the kernel, frees
2417 * requested irq handler and de-allocates memory allocated for channel
2418 * objects.
2419 */
2420 static void vpif_cleanup(void)
2421 {
2422 struct platform_device *pdev;
2423 struct resource *res;
2424 int irq_num;
2425 int i = 0;
2426
2427 pdev = container_of(vpif_dev, struct platform_device, dev);
2428 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, i))) {
2429 for (irq_num = res->start; irq_num <= res->end; irq_num++)
2430 free_irq(irq_num,
2431 (void *)(&vpif_obj.dev[i]->channel_id));
2432 i++;
2433 }
2434
2435 platform_driver_unregister(&vpif_driver);
2436
2437 kfree(vpif_obj.sd);
2438 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++)
2439 kfree(vpif_obj.dev[i]);
2440 }
2441
2442 /* Function for module initialization and cleanup */
2443 module_init(vpif_init);
2444 module_exit(vpif_cleanup);
This page took 0.08076 seconds and 5 git commands to generate.