1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2009 Texas Instruments Inc
4 * Copyright (C) 2014 Lad, Prabhakar <prabhakar.csengg@gmail.com>
6 * TODO : add support for VBI & HBI data service
7 * add static buffer allocation
10 #include <linux/module.h>
11 #include <linux/interrupt.h>
12 #include <linux/of_graph.h>
13 #include <linux/platform_device.h>
14 #include <linux/slab.h>
16 #include <media/v4l2-fwnode.h>
17 #include <media/v4l2-ioctl.h>
18 #include <media/i2c/tvp514x.h>
19 #include <media/v4l2-mediabus.h>
21 #include <linux/videodev2.h>
24 #include "vpif_capture.h"
26 MODULE_DESCRIPTION("TI DaVinci VPIF Capture driver");
27 MODULE_LICENSE("GPL");
28 MODULE_VERSION(VPIF_CAPTURE_VERSION);
30 #define vpif_err(fmt, arg...) v4l2_err(&vpif_obj.v4l2_dev, fmt, ## arg)
31 #define vpif_dbg(level, debug, fmt, arg...) \
32 v4l2_dbg(level, debug, &vpif_obj.v4l2_dev, fmt, ## arg)
36 module_param(debug, int, 0644);
38 MODULE_PARM_DESC(debug, "Debug level 0-1");
40 #define VPIF_DRIVER_NAME "vpif_capture"
41 MODULE_ALIAS("platform:" VPIF_DRIVER_NAME);
43 /* global variables */
44 static struct vpif_device vpif_obj = { {NULL} };
45 static struct device *vpif_dev;
46 static void vpif_calculate_offsets(struct channel_obj *ch);
47 static void vpif_config_addr(struct channel_obj *ch, int muxmode);
49 static u8 channel_first_int[VPIF_NUMBER_OF_OBJECTS][2] = { {1, 1} };
51 /* Is set to 1 in case of SDTV formats, 2 in case of HDTV formats. */
52 static int ycmux_mode;
55 struct vpif_cap_buffer *to_vpif_buffer(struct vb2_v4l2_buffer *vb)
57 return container_of(vb, struct vpif_cap_buffer, vb);
61 * vpif_buffer_prepare : callback function for buffer prepare
62 * @vb: ptr to vb2_buffer
64 * This is the callback function for buffer prepare when vb2_qbuf()
65 * function is called. The buffer is prepared and user space virtual address
66 * or user address is converted into physical address
68 static int vpif_buffer_prepare(struct vb2_buffer *vb)
70 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
71 struct vb2_queue *q = vb->vb2_queue;
72 struct channel_obj *ch = vb2_get_drv_priv(q);
73 struct common_obj *common;
76 vpif_dbg(2, debug, "vpif_buffer_prepare\n");
78 common = &ch->common[VPIF_VIDEO_INDEX];
80 vb2_set_plane_payload(vb, 0, common->fmt.fmt.pix.sizeimage);
81 if (vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
84 vbuf->field = common->fmt.fmt.pix.field;
86 addr = vb2_dma_contig_plane_dma_addr(vb, 0);
87 if (!IS_ALIGNED((addr + common->ytop_off), 8) ||
88 !IS_ALIGNED((addr + common->ybtm_off), 8) ||
89 !IS_ALIGNED((addr + common->ctop_off), 8) ||
90 !IS_ALIGNED((addr + common->cbtm_off), 8)) {
91 vpif_dbg(1, debug, "offset is not aligned\n");
99 * vpif_buffer_queue_setup : Callback function for buffer setup.
101 * @nbuffers: ptr to number of buffers requested by application
102 * @nplanes:: contains number of distinct video planes needed to hold a frame
103 * @sizes: contains the size (in bytes) of each plane.
104 * @alloc_devs: ptr to allocation context
106 * This callback function is called when reqbuf() is called to adjust
107 * the buffer count and buffer size
109 static int vpif_buffer_queue_setup(struct vb2_queue *vq,
110 unsigned int *nbuffers, unsigned int *nplanes,
111 unsigned int sizes[], struct device *alloc_devs[])
113 struct channel_obj *ch = vb2_get_drv_priv(vq);
114 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
115 unsigned size = common->fmt.fmt.pix.sizeimage;
117 vpif_dbg(2, debug, "vpif_buffer_setup\n");
125 if (vq->num_buffers + *nbuffers < 3)
126 *nbuffers = 3 - vq->num_buffers;
131 /* Calculate the offset for Y and C data in the buffer */
132 vpif_calculate_offsets(ch);
138 * vpif_buffer_queue : Callback function to add buffer to DMA queue
139 * @vb: ptr to vb2_buffer
141 static void vpif_buffer_queue(struct vb2_buffer *vb)
143 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
144 struct channel_obj *ch = vb2_get_drv_priv(vb->vb2_queue);
145 struct vpif_cap_buffer *buf = to_vpif_buffer(vbuf);
146 struct common_obj *common;
149 common = &ch->common[VPIF_VIDEO_INDEX];
151 vpif_dbg(2, debug, "vpif_buffer_queue\n");
153 spin_lock_irqsave(&common->irqlock, flags);
154 /* add the buffer to the DMA queue */
155 list_add_tail(&buf->list, &common->dma_queue);
156 spin_unlock_irqrestore(&common->irqlock, flags);
160 * vpif_start_streaming : Starts the DMA engine for streaming
161 * @vq: ptr to vb2_buffer
162 * @count: number of buffers
164 static int vpif_start_streaming(struct vb2_queue *vq, unsigned int count)
166 struct vpif_capture_config *vpif_config_data =
167 vpif_dev->platform_data;
168 struct channel_obj *ch = vb2_get_drv_priv(vq);
169 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
170 struct vpif_params *vpif = &ch->vpifparams;
171 struct vpif_cap_buffer *buf, *tmp;
172 unsigned long addr, flags;
175 /* Initialize field_id */
178 /* configure 1 or 2 channel mode */
179 if (vpif_config_data->setup_input_channel_mode) {
180 ret = vpif_config_data->
181 setup_input_channel_mode(vpif->std_info.ycmux_mode);
183 vpif_dbg(1, debug, "can't set vpif channel mode\n");
188 ret = v4l2_subdev_call(ch->sd, video, s_stream, 1);
189 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
190 vpif_dbg(1, debug, "stream on failed in subdev\n");
194 /* Call vpif_set_params function to set the parameters and addresses */
195 ret = vpif_set_video_params(vpif, ch->channel_id);
197 vpif_dbg(1, debug, "can't set video params\n");
202 vpif_config_addr(ch, ret);
204 /* Get the next frame from the buffer queue */
205 spin_lock_irqsave(&common->irqlock, flags);
206 common->cur_frm = common->next_frm = list_entry(common->dma_queue.next,
207 struct vpif_cap_buffer, list);
208 /* Remove buffer from the buffer queue */
209 list_del(&common->cur_frm->list);
210 spin_unlock_irqrestore(&common->irqlock, flags);
212 addr = vb2_dma_contig_plane_dma_addr(&common->cur_frm->vb.vb2_buf, 0);
214 common->set_addr(addr + common->ytop_off,
215 addr + common->ybtm_off,
216 addr + common->ctop_off,
217 addr + common->cbtm_off);
220 * Set interrupt for both the fields in VPIF Register enable channel in
223 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
224 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
225 channel0_intr_assert();
226 channel0_intr_enable(1);
229 if (VPIF_CHANNEL1_VIDEO == ch->channel_id ||
231 channel1_intr_assert();
232 channel1_intr_enable(1);
239 spin_lock_irqsave(&common->irqlock, flags);
240 list_for_each_entry_safe(buf, tmp, &common->dma_queue, list) {
241 list_del(&buf->list);
242 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
244 spin_unlock_irqrestore(&common->irqlock, flags);
250 * vpif_stop_streaming : Stop the DMA engine
251 * @vq: ptr to vb2_queue
253 * This callback stops the DMA engine and any remaining buffers
254 * in the DMA queue are released.
256 static void vpif_stop_streaming(struct vb2_queue *vq)
258 struct channel_obj *ch = vb2_get_drv_priv(vq);
259 struct common_obj *common;
263 common = &ch->common[VPIF_VIDEO_INDEX];
265 /* Disable channel as per its device type and channel id */
266 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
268 channel0_intr_enable(0);
270 if (VPIF_CHANNEL1_VIDEO == ch->channel_id ||
273 channel1_intr_enable(0);
278 ret = v4l2_subdev_call(ch->sd, video, s_stream, 0);
279 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
280 vpif_dbg(1, debug, "stream off failed in subdev\n");
282 /* release all active buffers */
283 if (common->cur_frm == common->next_frm) {
284 vb2_buffer_done(&common->cur_frm->vb.vb2_buf,
285 VB2_BUF_STATE_ERROR);
288 vb2_buffer_done(&common->cur_frm->vb.vb2_buf,
289 VB2_BUF_STATE_ERROR);
290 if (common->next_frm)
291 vb2_buffer_done(&common->next_frm->vb.vb2_buf,
292 VB2_BUF_STATE_ERROR);
295 spin_lock_irqsave(&common->irqlock, flags);
296 while (!list_empty(&common->dma_queue)) {
297 common->next_frm = list_entry(common->dma_queue.next,
298 struct vpif_cap_buffer, list);
299 list_del(&common->next_frm->list);
300 vb2_buffer_done(&common->next_frm->vb.vb2_buf,
301 VB2_BUF_STATE_ERROR);
303 spin_unlock_irqrestore(&common->irqlock, flags);
306 static const struct vb2_ops video_qops = {
307 .queue_setup = vpif_buffer_queue_setup,
308 .buf_prepare = vpif_buffer_prepare,
309 .start_streaming = vpif_start_streaming,
310 .stop_streaming = vpif_stop_streaming,
311 .buf_queue = vpif_buffer_queue,
312 .wait_prepare = vb2_ops_wait_prepare,
313 .wait_finish = vb2_ops_wait_finish,
317 * vpif_process_buffer_complete: process a completed buffer
318 * @common: ptr to common channel object
320 * This function time stamp the buffer and mark it as DONE. It also
321 * wake up any process waiting on the QUEUE and set the next buffer
324 static void vpif_process_buffer_complete(struct common_obj *common)
326 common->cur_frm->vb.vb2_buf.timestamp = ktime_get_ns();
327 vb2_buffer_done(&common->cur_frm->vb.vb2_buf, VB2_BUF_STATE_DONE);
328 /* Make curFrm pointing to nextFrm */
329 common->cur_frm = common->next_frm;
333 * vpif_schedule_next_buffer: set next buffer address for capture
334 * @common : ptr to common channel object
336 * This function will get next buffer from the dma queue and
337 * set the buffer address in the vpif register for capture.
338 * the buffer is marked active
340 static void vpif_schedule_next_buffer(struct common_obj *common)
342 unsigned long addr = 0;
344 spin_lock(&common->irqlock);
345 common->next_frm = list_entry(common->dma_queue.next,
346 struct vpif_cap_buffer, list);
347 /* Remove that buffer from the buffer queue */
348 list_del(&common->next_frm->list);
349 spin_unlock(&common->irqlock);
350 addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb.vb2_buf, 0);
352 /* Set top and bottom field addresses in VPIF registers */
353 common->set_addr(addr + common->ytop_off,
354 addr + common->ybtm_off,
355 addr + common->ctop_off,
356 addr + common->cbtm_off);
360 * vpif_channel_isr : ISR handler for vpif capture
362 * @dev_id: dev_id ptr
364 * It changes status of the captured buffer, takes next buffer from the queue
365 * and sets its address in VPIF registers
367 static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
369 struct vpif_device *dev = &vpif_obj;
370 struct common_obj *common;
371 struct channel_obj *ch;
375 channel_id = *(int *)(dev_id);
376 if (!vpif_intr_status(channel_id))
379 ch = dev->dev[channel_id];
381 for (i = 0; i < VPIF_NUMBER_OF_OBJECTS; i++) {
382 common = &ch->common[i];
383 /* skip If streaming is not started in this channel */
384 /* Check the field format */
385 if (1 == ch->vpifparams.std_info.frm_fmt ||
386 common->fmt.fmt.pix.field == V4L2_FIELD_NONE) {
387 /* Progressive mode */
388 spin_lock(&common->irqlock);
389 if (list_empty(&common->dma_queue)) {
390 spin_unlock(&common->irqlock);
393 spin_unlock(&common->irqlock);
395 if (!channel_first_int[i][channel_id])
396 vpif_process_buffer_complete(common);
398 channel_first_int[i][channel_id] = 0;
400 vpif_schedule_next_buffer(common);
403 channel_first_int[i][channel_id] = 0;
406 * Interlaced mode. If it is first interrupt, ignore
409 if (channel_first_int[i][channel_id]) {
410 channel_first_int[i][channel_id] = 0;
415 /* Get field id from VPIF registers */
416 fid = vpif_channel_getfid(ch->channel_id);
417 if (fid != ch->field_id) {
419 * If field id does not match stored
420 * field id, make them in sync
427 /* device field id and local field id are in sync */
429 /* this is even field */
430 if (common->cur_frm == common->next_frm)
433 /* mark the current buffer as done */
434 vpif_process_buffer_complete(common);
435 } else if (1 == fid) {
437 spin_lock(&common->irqlock);
438 if (list_empty(&common->dma_queue) ||
439 (common->cur_frm != common->next_frm)) {
440 spin_unlock(&common->irqlock);
443 spin_unlock(&common->irqlock);
445 vpif_schedule_next_buffer(common);
453 * vpif_update_std_info() - update standard related info
454 * @ch: ptr to channel object
456 * For a given standard selected by application, update values
457 * in the device data structures
459 static int vpif_update_std_info(struct channel_obj *ch)
461 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
462 struct vpif_params *vpifparams = &ch->vpifparams;
463 const struct vpif_channel_config_params *config;
464 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
465 struct video_obj *vid_ch = &ch->video;
467 struct v4l2_pix_format *pixfmt = &common->fmt.fmt.pix;
469 vpif_dbg(2, debug, "vpif_update_std_info\n");
472 * if called after try_fmt or g_fmt, there will already be a size
473 * so use that by default.
475 if (pixfmt->width && pixfmt->height) {
476 if (pixfmt->field == V4L2_FIELD_ANY ||
477 pixfmt->field == V4L2_FIELD_NONE)
478 pixfmt->field = V4L2_FIELD_NONE;
480 vpifparams->iface.if_type = VPIF_IF_BT656;
481 if (pixfmt->pixelformat == V4L2_PIX_FMT_SGRBG10 ||
482 pixfmt->pixelformat == V4L2_PIX_FMT_SBGGR8)
483 vpifparams->iface.if_type = VPIF_IF_RAW_BAYER;
485 if (pixfmt->pixelformat == V4L2_PIX_FMT_SGRBG10)
486 vpifparams->params.data_sz = 1; /* 10 bits/pixel. */
489 * For raw formats from camera sensors, we don't need
490 * the std_info from table lookup, so nothing else to do here.
492 if (vpifparams->iface.if_type == VPIF_IF_RAW_BAYER) {
493 memset(std_info, 0, sizeof(struct vpif_channel_config_params));
494 vpifparams->std_info.capture_format = 1; /* CCD/raw mode */
499 for (index = 0; index < vpif_ch_params_count; index++) {
500 config = &vpif_ch_params[index];
501 if (config->hd_sd == 0) {
502 vpif_dbg(2, debug, "SD format\n");
503 if (config->stdid & vid_ch->stdid) {
504 memcpy(std_info, config, sizeof(*config));
508 vpif_dbg(2, debug, "HD format\n");
509 if (!memcmp(&config->dv_timings, &vid_ch->dv_timings,
510 sizeof(vid_ch->dv_timings))) {
511 memcpy(std_info, config, sizeof(*config));
517 /* standard not found */
518 if (index == vpif_ch_params_count)
521 common->fmt.fmt.pix.width = std_info->width;
522 common->width = std_info->width;
523 common->fmt.fmt.pix.height = std_info->height;
524 common->height = std_info->height;
525 common->fmt.fmt.pix.sizeimage = common->height * common->width * 2;
526 common->fmt.fmt.pix.bytesperline = std_info->width;
527 vpifparams->video_params.hpitch = std_info->width;
528 vpifparams->video_params.storage_mode = std_info->frm_fmt;
531 common->fmt.fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
533 common->fmt.fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
535 if (ch->vpifparams.std_info.frm_fmt)
536 common->fmt.fmt.pix.field = V4L2_FIELD_NONE;
538 common->fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
540 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER)
541 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;
543 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_NV16;
545 common->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
551 * vpif_calculate_offsets : This function calculates buffers offsets
552 * @ch : ptr to channel object
554 * This function calculates buffer offsets for Y and C in the top and
557 static void vpif_calculate_offsets(struct channel_obj *ch)
559 unsigned int hpitch, sizeimage;
560 struct video_obj *vid_ch = &(ch->video);
561 struct vpif_params *vpifparams = &ch->vpifparams;
562 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
563 enum v4l2_field field = common->fmt.fmt.pix.field;
565 vpif_dbg(2, debug, "vpif_calculate_offsets\n");
567 if (V4L2_FIELD_ANY == field) {
568 if (vpifparams->std_info.frm_fmt)
569 vid_ch->buf_field = V4L2_FIELD_NONE;
571 vid_ch->buf_field = V4L2_FIELD_INTERLACED;
573 vid_ch->buf_field = common->fmt.fmt.pix.field;
575 sizeimage = common->fmt.fmt.pix.sizeimage;
577 hpitch = common->fmt.fmt.pix.bytesperline;
579 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
580 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
581 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
582 common->ytop_off = 0;
583 common->ybtm_off = hpitch;
584 common->ctop_off = sizeimage / 2;
585 common->cbtm_off = sizeimage / 2 + hpitch;
586 } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
587 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
588 common->ytop_off = 0;
589 common->ybtm_off = sizeimage / 4;
590 common->ctop_off = sizeimage / 2;
591 common->cbtm_off = common->ctop_off + sizeimage / 4;
592 } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
593 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
594 common->ybtm_off = 0;
595 common->ytop_off = sizeimage / 4;
596 common->cbtm_off = sizeimage / 2;
597 common->ctop_off = common->cbtm_off + sizeimage / 4;
599 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
600 (V4L2_FIELD_INTERLACED == vid_ch->buf_field))
601 vpifparams->video_params.storage_mode = 1;
603 vpifparams->video_params.storage_mode = 0;
605 if (1 == vpifparams->std_info.frm_fmt)
606 vpifparams->video_params.hpitch =
607 common->fmt.fmt.pix.bytesperline;
609 if ((field == V4L2_FIELD_ANY)
610 || (field == V4L2_FIELD_INTERLACED))
611 vpifparams->video_params.hpitch =
612 common->fmt.fmt.pix.bytesperline * 2;
614 vpifparams->video_params.hpitch =
615 common->fmt.fmt.pix.bytesperline;
618 ch->vpifparams.video_params.stdid = vpifparams->std_info.stdid;
622 * vpif_get_default_field() - Get default field type based on interface
623 * @iface: ptr to vpif interface
625 static inline enum v4l2_field vpif_get_default_field(
626 struct vpif_interface *iface)
628 return (iface->if_type == VPIF_IF_RAW_BAYER) ? V4L2_FIELD_NONE :
629 V4L2_FIELD_INTERLACED;
633 * vpif_config_addr() - function to configure buffer address in vpif
635 * @muxmode: channel mux mode
637 static void vpif_config_addr(struct channel_obj *ch, int muxmode)
639 struct common_obj *common;
641 vpif_dbg(2, debug, "vpif_config_addr\n");
643 common = &(ch->common[VPIF_VIDEO_INDEX]);
645 if (VPIF_CHANNEL1_VIDEO == ch->channel_id)
646 common->set_addr = ch1_set_videobuf_addr;
647 else if (2 == muxmode)
648 common->set_addr = ch0_set_videobuf_addr_yc_nmux;
650 common->set_addr = ch0_set_videobuf_addr;
654 * vpif_input_to_subdev() - Maps input to sub device
655 * @vpif_cfg: global config ptr
656 * @chan_cfg: channel config ptr
657 * @input_index: Given input index from application
659 * lookup the sub device information for a given input index.
660 * we report all the inputs to application. inputs table also
661 * has sub device name for the each input
663 static int vpif_input_to_subdev(
664 struct vpif_capture_config *vpif_cfg,
665 struct vpif_capture_chan_config *chan_cfg,
668 struct vpif_subdev_info *subdev_info;
669 const char *subdev_name;
672 vpif_dbg(2, debug, "vpif_input_to_subdev\n");
676 if (input_index >= chan_cfg->input_count)
678 subdev_name = chan_cfg->inputs[input_index].subdev_name;
682 /* loop through the sub device list to get the sub device info */
683 for (i = 0; i < vpif_cfg->subdev_count; i++) {
684 subdev_info = &vpif_cfg->subdev_info[i];
685 if (subdev_info && !strcmp(subdev_info->name, subdev_name))
692 * vpif_set_input() - Select an input
693 * @vpif_cfg: global config ptr
695 * @index: Given input index from application
697 * Select the given input.
699 static int vpif_set_input(
700 struct vpif_capture_config *vpif_cfg,
701 struct channel_obj *ch,
704 struct vpif_capture_chan_config *chan_cfg =
705 &vpif_cfg->chan_config[ch->channel_id];
706 struct vpif_subdev_info *subdev_info = NULL;
707 struct v4l2_subdev *sd = NULL;
708 u32 input = 0, output = 0;
712 sd_index = vpif_input_to_subdev(vpif_cfg, chan_cfg, index);
714 sd = vpif_obj.sd[sd_index];
715 subdev_info = &vpif_cfg->subdev_info[sd_index];
717 /* no subdevice, no input to setup */
721 /* first setup input path from sub device to vpif */
722 if (sd && vpif_cfg->setup_input_path) {
723 ret = vpif_cfg->setup_input_path(ch->channel_id,
726 vpif_dbg(1, debug, "couldn't setup input path for the" \
727 " sub device %s, for input index %d\n",
728 subdev_info->name, index);
734 input = chan_cfg->inputs[index].input_route;
735 output = chan_cfg->inputs[index].output_route;
736 ret = v4l2_subdev_call(sd, video, s_routing,
738 if (ret < 0 && ret != -ENOIOCTLCMD) {
739 vpif_dbg(1, debug, "Failed to set input\n");
743 ch->input_idx = index;
745 /* copy interface parameters to vpif */
746 ch->vpifparams.iface = chan_cfg->vpif_if;
748 /* update tvnorms from the sub device input info */
749 ch->video_dev.tvnorms = chan_cfg->inputs[index].input.std;
754 * vpif_querystd() - querystd handler
757 * @std_id: ptr to std id
759 * This function is called to detect standard at the selected input
761 static int vpif_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
763 struct video_device *vdev = video_devdata(file);
764 struct channel_obj *ch = video_get_drvdata(vdev);
767 vpif_dbg(2, debug, "vpif_querystd\n");
769 /* Call querystd function of decoder device */
770 ret = v4l2_subdev_call(ch->sd, video, querystd, std_id);
772 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
775 vpif_dbg(1, debug, "Failed to query standard for sub devices\n");
783 * vpif_g_std() - get STD handler
786 * @std: ptr to std id
788 static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
790 struct vpif_capture_config *config = vpif_dev->platform_data;
791 struct video_device *vdev = video_devdata(file);
792 struct channel_obj *ch = video_get_drvdata(vdev);
793 struct vpif_capture_chan_config *chan_cfg;
794 struct v4l2_input input;
796 vpif_dbg(2, debug, "vpif_g_std\n");
798 if (!config->chan_config[ch->channel_id].inputs)
801 chan_cfg = &config->chan_config[ch->channel_id];
802 input = chan_cfg->inputs[ch->input_idx].input;
803 if (input.capabilities != V4L2_IN_CAP_STD)
806 *std = ch->video.stdid;
811 * vpif_s_std() - set STD handler
814 * @std_id: ptr to std id
816 static int vpif_s_std(struct file *file, void *priv, v4l2_std_id std_id)
818 struct vpif_capture_config *config = vpif_dev->platform_data;
819 struct video_device *vdev = video_devdata(file);
820 struct channel_obj *ch = video_get_drvdata(vdev);
821 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
822 struct vpif_capture_chan_config *chan_cfg;
823 struct v4l2_input input;
826 vpif_dbg(2, debug, "vpif_s_std\n");
828 if (!config->chan_config[ch->channel_id].inputs)
831 chan_cfg = &config->chan_config[ch->channel_id];
832 input = chan_cfg->inputs[ch->input_idx].input;
833 if (input.capabilities != V4L2_IN_CAP_STD)
836 if (vb2_is_busy(&common->buffer_queue))
839 /* Call encoder subdevice function to set the standard */
840 ch->video.stdid = std_id;
841 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
843 /* Get the information about the standard */
844 if (vpif_update_std_info(ch)) {
845 vpif_err("Error getting the standard info\n");
849 /* set standard in the sub device */
850 ret = v4l2_subdev_call(ch->sd, video, s_std, std_id);
851 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
852 vpif_dbg(1, debug, "Failed to set standard for sub devices\n");
859 * vpif_enum_input() - ENUMINPUT handler
862 * @input: ptr to input structure
864 static int vpif_enum_input(struct file *file, void *priv,
865 struct v4l2_input *input)
868 struct vpif_capture_config *config = vpif_dev->platform_data;
869 struct video_device *vdev = video_devdata(file);
870 struct channel_obj *ch = video_get_drvdata(vdev);
871 struct vpif_capture_chan_config *chan_cfg;
873 chan_cfg = &config->chan_config[ch->channel_id];
875 if (input->index >= chan_cfg->input_count)
878 memcpy(input, &chan_cfg->inputs[input->index].input,
884 * vpif_g_input() - Get INPUT handler
887 * @index: ptr to input index
889 static int vpif_g_input(struct file *file, void *priv, unsigned int *index)
891 struct video_device *vdev = video_devdata(file);
892 struct channel_obj *ch = video_get_drvdata(vdev);
894 *index = ch->input_idx;
899 * vpif_s_input() - Set INPUT handler
902 * @index: input index
904 static int vpif_s_input(struct file *file, void *priv, unsigned int index)
906 struct vpif_capture_config *config = vpif_dev->platform_data;
907 struct video_device *vdev = video_devdata(file);
908 struct channel_obj *ch = video_get_drvdata(vdev);
909 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
910 struct vpif_capture_chan_config *chan_cfg;
912 chan_cfg = &config->chan_config[ch->channel_id];
914 if (index >= chan_cfg->input_count)
917 if (vb2_is_busy(&common->buffer_queue))
920 return vpif_set_input(config, ch, index);
924 * vpif_enum_fmt_vid_cap() - ENUM_FMT handler
927 * @fmt: ptr to V4L2 format descriptor
929 static int vpif_enum_fmt_vid_cap(struct file *file, void *priv,
930 struct v4l2_fmtdesc *fmt)
932 struct video_device *vdev = video_devdata(file);
933 struct channel_obj *ch = video_get_drvdata(vdev);
935 if (fmt->index != 0) {
936 vpif_dbg(1, debug, "Invalid format index\n");
940 /* Fill in the information about format */
941 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER)
942 fmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
944 fmt->pixelformat = V4L2_PIX_FMT_NV16;
949 * vpif_try_fmt_vid_cap() - TRY_FMT handler
952 * @fmt: ptr to v4l2 format structure
954 static int vpif_try_fmt_vid_cap(struct file *file, void *priv,
955 struct v4l2_format *fmt)
957 struct video_device *vdev = video_devdata(file);
958 struct channel_obj *ch = video_get_drvdata(vdev);
959 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
960 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
963 vpif_update_std_info(ch);
965 pixfmt->field = common->fmt.fmt.pix.field;
966 pixfmt->colorspace = common->fmt.fmt.pix.colorspace;
967 pixfmt->bytesperline = common->fmt.fmt.pix.width;
968 pixfmt->width = common->fmt.fmt.pix.width;
969 pixfmt->height = common->fmt.fmt.pix.height;
970 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height * 2;
971 if (pixfmt->pixelformat == V4L2_PIX_FMT_SGRBG10) {
972 pixfmt->bytesperline = common->fmt.fmt.pix.width * 2;
973 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height;
976 dev_dbg(vpif_dev, "%s: %d x %d; pitch=%d pixelformat=0x%08x, field=%d, size=%d\n", __func__,
977 pixfmt->width, pixfmt->height,
978 pixfmt->bytesperline, pixfmt->pixelformat,
979 pixfmt->field, pixfmt->sizeimage);
986 * vpif_g_fmt_vid_cap() - Set INPUT handler
989 * @fmt: ptr to v4l2 format structure
991 static int vpif_g_fmt_vid_cap(struct file *file, void *priv,
992 struct v4l2_format *fmt)
994 struct video_device *vdev = video_devdata(file);
995 struct channel_obj *ch = video_get_drvdata(vdev);
996 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
997 struct v4l2_pix_format *pix_fmt = &fmt->fmt.pix;
998 struct v4l2_subdev_format format = {
999 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1001 struct v4l2_mbus_framefmt *mbus_fmt = &format.format;
1004 /* Check the validity of the buffer type */
1005 if (common->fmt.type != fmt->type)
1008 /* By default, use currently set fmt */
1011 /* If subdev has get_fmt, use that to override */
1012 ret = v4l2_subdev_call(ch->sd, pad, get_fmt, NULL, &format);
1013 if (!ret && mbus_fmt->code) {
1014 v4l2_fill_pix_format(pix_fmt, mbus_fmt);
1015 pix_fmt->bytesperline = pix_fmt->width;
1016 if (mbus_fmt->code == MEDIA_BUS_FMT_SGRBG10_1X10) {
1018 pix_fmt->pixelformat = V4L2_PIX_FMT_SGRBG10;
1019 pix_fmt->bytesperline = pix_fmt->width * 2;
1020 } else if (mbus_fmt->code == MEDIA_BUS_FMT_UYVY8_2X8) {
1022 pix_fmt->pixelformat = V4L2_PIX_FMT_NV16;
1023 pix_fmt->bytesperline = pix_fmt->width * 2;
1025 dev_warn(vpif_dev, "%s: Unhandled media-bus format 0x%x\n",
1026 __func__, mbus_fmt->code);
1028 pix_fmt->sizeimage = pix_fmt->bytesperline * pix_fmt->height;
1029 dev_dbg(vpif_dev, "%s: %d x %d; pitch=%d, pixelformat=0x%08x, code=0x%x, field=%d, size=%d\n", __func__,
1030 pix_fmt->width, pix_fmt->height,
1031 pix_fmt->bytesperline, pix_fmt->pixelformat,
1032 mbus_fmt->code, pix_fmt->field, pix_fmt->sizeimage);
1035 vpif_update_std_info(ch);
1042 * vpif_s_fmt_vid_cap() - Set FMT handler
1044 * @priv: file handle
1045 * @fmt: ptr to v4l2 format structure
1047 static int vpif_s_fmt_vid_cap(struct file *file, void *priv,
1048 struct v4l2_format *fmt)
1050 struct video_device *vdev = video_devdata(file);
1051 struct channel_obj *ch = video_get_drvdata(vdev);
1052 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1055 vpif_dbg(2, debug, "%s\n", __func__);
1057 if (vb2_is_busy(&common->buffer_queue))
1060 ret = vpif_try_fmt_vid_cap(file, priv, fmt);
1064 /* store the format in the channel object */
1070 * vpif_querycap() - QUERYCAP handler
1072 * @priv: file handle
1073 * @cap: ptr to v4l2_capability structure
1075 static int vpif_querycap(struct file *file, void *priv,
1076 struct v4l2_capability *cap)
1078 struct vpif_capture_config *config = vpif_dev->platform_data;
1080 strscpy(cap->driver, VPIF_DRIVER_NAME, sizeof(cap->driver));
1081 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
1082 dev_name(vpif_dev));
1083 strscpy(cap->card, config->card_name, sizeof(cap->card));
1089 * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler
1091 * @priv: file handle
1092 * @timings: input timings
1095 vpif_enum_dv_timings(struct file *file, void *priv,
1096 struct v4l2_enum_dv_timings *timings)
1098 struct vpif_capture_config *config = vpif_dev->platform_data;
1099 struct video_device *vdev = video_devdata(file);
1100 struct channel_obj *ch = video_get_drvdata(vdev);
1101 struct vpif_capture_chan_config *chan_cfg;
1102 struct v4l2_input input;
1105 if (!config->chan_config[ch->channel_id].inputs)
1108 chan_cfg = &config->chan_config[ch->channel_id];
1109 input = chan_cfg->inputs[ch->input_idx].input;
1110 if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
1115 ret = v4l2_subdev_call(ch->sd, pad, enum_dv_timings, timings);
1116 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1123 * vpif_query_dv_timings() - QUERY_DV_TIMINGS handler
1125 * @priv: file handle
1126 * @timings: input timings
1129 vpif_query_dv_timings(struct file *file, void *priv,
1130 struct v4l2_dv_timings *timings)
1132 struct vpif_capture_config *config = vpif_dev->platform_data;
1133 struct video_device *vdev = video_devdata(file);
1134 struct channel_obj *ch = video_get_drvdata(vdev);
1135 struct vpif_capture_chan_config *chan_cfg;
1136 struct v4l2_input input;
1139 if (!config->chan_config[ch->channel_id].inputs)
1142 chan_cfg = &config->chan_config[ch->channel_id];
1143 input = chan_cfg->inputs[ch->input_idx].input;
1144 if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
1147 ret = v4l2_subdev_call(ch->sd, video, query_dv_timings, timings);
1148 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1155 * vpif_s_dv_timings() - S_DV_TIMINGS handler
1157 * @priv: file handle
1158 * @timings: digital video timings
1160 static int vpif_s_dv_timings(struct file *file, void *priv,
1161 struct v4l2_dv_timings *timings)
1163 struct vpif_capture_config *config = vpif_dev->platform_data;
1164 struct video_device *vdev = video_devdata(file);
1165 struct channel_obj *ch = video_get_drvdata(vdev);
1166 struct vpif_params *vpifparams = &ch->vpifparams;
1167 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
1168 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1169 struct video_obj *vid_ch = &ch->video;
1170 struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
1171 struct vpif_capture_chan_config *chan_cfg;
1172 struct v4l2_input input;
1175 if (!config->chan_config[ch->channel_id].inputs)
1178 chan_cfg = &config->chan_config[ch->channel_id];
1179 input = chan_cfg->inputs[ch->input_idx].input;
1180 if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
1183 if (timings->type != V4L2_DV_BT_656_1120) {
1184 vpif_dbg(2, debug, "Timing type not defined\n");
1188 if (vb2_is_busy(&common->buffer_queue))
1191 /* Configure subdevice timings, if any */
1192 ret = v4l2_subdev_call(ch->sd, video, s_dv_timings, timings);
1193 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1196 vpif_dbg(2, debug, "Error setting custom DV timings\n");
1200 if (!(timings->bt.width && timings->bt.height &&
1201 (timings->bt.hbackporch ||
1202 timings->bt.hfrontporch ||
1203 timings->bt.hsync) &&
1204 timings->bt.vfrontporch &&
1205 (timings->bt.vbackporch ||
1206 timings->bt.vsync))) {
1207 vpif_dbg(2, debug, "Timings for width, height, horizontal back porch, horizontal sync, horizontal front porch, vertical back porch, vertical sync and vertical back porch must be defined\n");
1211 vid_ch->dv_timings = *timings;
1213 /* Configure video port timings */
1215 std_info->eav2sav = V4L2_DV_BT_BLANKING_WIDTH(bt) - 8;
1216 std_info->sav2eav = bt->width;
1219 std_info->l3 = bt->vsync + bt->vbackporch + 1;
1221 std_info->vsize = V4L2_DV_BT_FRAME_HEIGHT(bt);
1222 if (bt->interlaced) {
1223 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
1224 std_info->l5 = std_info->vsize/2 -
1225 (bt->vfrontporch - 1);
1226 std_info->l7 = std_info->vsize/2 + 1;
1227 std_info->l9 = std_info->l7 + bt->il_vsync +
1228 bt->il_vbackporch + 1;
1229 std_info->l11 = std_info->vsize -
1230 (bt->il_vfrontporch - 1);
1232 vpif_dbg(2, debug, "Required timing values for interlaced BT format missing\n");
1236 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
1238 strscpy(std_info->name, "Custom timings BT656/1120",
1239 sizeof(std_info->name));
1240 std_info->width = bt->width;
1241 std_info->height = bt->height;
1242 std_info->frm_fmt = bt->interlaced ? 0 : 1;
1243 std_info->ycmux_mode = 0;
1244 std_info->capture_format = 0;
1245 std_info->vbi_supported = 0;
1246 std_info->hd_sd = 1;
1247 std_info->stdid = 0;
1254 * vpif_g_dv_timings() - G_DV_TIMINGS handler
1256 * @priv: file handle
1257 * @timings: digital video timings
1259 static int vpif_g_dv_timings(struct file *file, void *priv,
1260 struct v4l2_dv_timings *timings)
1262 struct vpif_capture_config *config = vpif_dev->platform_data;
1263 struct video_device *vdev = video_devdata(file);
1264 struct channel_obj *ch = video_get_drvdata(vdev);
1265 struct video_obj *vid_ch = &ch->video;
1266 struct vpif_capture_chan_config *chan_cfg;
1267 struct v4l2_input input;
1269 if (!config->chan_config[ch->channel_id].inputs)
1272 chan_cfg = &config->chan_config[ch->channel_id];
1273 input = chan_cfg->inputs[ch->input_idx].input;
1274 if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
1277 *timings = vid_ch->dv_timings;
1283 * vpif_log_status() - Status information
1285 * @priv: file handle
1289 static int vpif_log_status(struct file *filep, void *priv)
1291 /* status for sub devices */
1292 v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1297 /* vpif capture ioctl operations */
1298 static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
1299 .vidioc_querycap = vpif_querycap,
1300 .vidioc_enum_fmt_vid_cap = vpif_enum_fmt_vid_cap,
1301 .vidioc_g_fmt_vid_cap = vpif_g_fmt_vid_cap,
1302 .vidioc_s_fmt_vid_cap = vpif_s_fmt_vid_cap,
1303 .vidioc_try_fmt_vid_cap = vpif_try_fmt_vid_cap,
1305 .vidioc_enum_input = vpif_enum_input,
1306 .vidioc_s_input = vpif_s_input,
1307 .vidioc_g_input = vpif_g_input,
1309 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1310 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1311 .vidioc_querybuf = vb2_ioctl_querybuf,
1312 .vidioc_qbuf = vb2_ioctl_qbuf,
1313 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1314 .vidioc_expbuf = vb2_ioctl_expbuf,
1315 .vidioc_streamon = vb2_ioctl_streamon,
1316 .vidioc_streamoff = vb2_ioctl_streamoff,
1318 .vidioc_querystd = vpif_querystd,
1319 .vidioc_s_std = vpif_s_std,
1320 .vidioc_g_std = vpif_g_std,
1322 .vidioc_enum_dv_timings = vpif_enum_dv_timings,
1323 .vidioc_query_dv_timings = vpif_query_dv_timings,
1324 .vidioc_s_dv_timings = vpif_s_dv_timings,
1325 .vidioc_g_dv_timings = vpif_g_dv_timings,
1327 .vidioc_log_status = vpif_log_status,
1330 /* vpif file operations */
1331 static const struct v4l2_file_operations vpif_fops = {
1332 .owner = THIS_MODULE,
1333 .open = v4l2_fh_open,
1334 .release = vb2_fop_release,
1335 .unlocked_ioctl = video_ioctl2,
1336 .mmap = vb2_fop_mmap,
1337 .poll = vb2_fop_poll
1341 * initialize_vpif() - Initialize vpif data structures
1343 * Allocate memory for data structures and initialize them
1345 static int initialize_vpif(void)
1348 int free_channel_objects_index;
1350 /* Allocate memory for six channel objects */
1351 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1353 kzalloc(sizeof(*vpif_obj.dev[i]), GFP_KERNEL);
1354 /* If memory allocation fails, return error */
1355 if (!vpif_obj.dev[i]) {
1356 free_channel_objects_index = i;
1358 goto vpif_init_free_channel_objects;
1363 vpif_init_free_channel_objects:
1364 for (j = 0; j < free_channel_objects_index; j++)
1365 kfree(vpif_obj.dev[j]);
1369 static inline void free_vpif_objs(void)
1373 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++)
1374 kfree(vpif_obj.dev[i]);
1377 static int vpif_async_bound(struct v4l2_async_notifier *notifier,
1378 struct v4l2_subdev *subdev,
1379 struct v4l2_async_subdev *asd)
1383 for (i = 0; i < vpif_obj.config->asd_sizes[0]; i++) {
1384 struct v4l2_async_subdev *_asd = vpif_obj.config->asd[i];
1385 const struct fwnode_handle *fwnode = _asd->match.fwnode;
1387 if (fwnode == subdev->fwnode) {
1388 vpif_obj.sd[i] = subdev;
1389 vpif_obj.config->chan_config->inputs[i].subdev_name =
1390 (char *)to_of_node(subdev->fwnode)->full_name;
1392 "%s: setting input %d subdev_name = %s\n",
1394 vpif_obj.config->chan_config->inputs[i].subdev_name);
1399 for (i = 0; i < vpif_obj.config->subdev_count; i++)
1400 if (!strcmp(vpif_obj.config->subdev_info[i].name,
1402 vpif_obj.sd[i] = subdev;
1409 static int vpif_probe_complete(void)
1411 struct common_obj *common;
1412 struct video_device *vdev;
1413 struct channel_obj *ch;
1414 struct vb2_queue *q;
1417 for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
1418 ch = vpif_obj.dev[j];
1420 common = &(ch->common[VPIF_VIDEO_INDEX]);
1421 spin_lock_init(&common->irqlock);
1422 mutex_init(&common->lock);
1424 /* select input 0 */
1425 err = vpif_set_input(vpif_obj.config, ch, 0);
1429 /* set initial format */
1430 ch->video.stdid = V4L2_STD_525_60;
1431 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
1432 common->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1433 vpif_update_std_info(ch);
1435 /* Initialize vb2 queue */
1436 q = &common->buffer_queue;
1437 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1438 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1440 q->ops = &video_qops;
1441 q->mem_ops = &vb2_dma_contig_memops;
1442 q->buf_struct_size = sizeof(struct vpif_cap_buffer);
1443 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1444 q->min_buffers_needed = 1;
1445 q->lock = &common->lock;
1448 err = vb2_queue_init(q);
1450 vpif_err("vpif_capture: vb2_queue_init() failed\n");
1454 INIT_LIST_HEAD(&common->dma_queue);
1456 /* Initialize the video_device structure */
1457 vdev = &ch->video_dev;
1458 strscpy(vdev->name, VPIF_DRIVER_NAME, sizeof(vdev->name));
1459 vdev->release = video_device_release_empty;
1460 vdev->fops = &vpif_fops;
1461 vdev->ioctl_ops = &vpif_ioctl_ops;
1462 vdev->v4l2_dev = &vpif_obj.v4l2_dev;
1463 vdev->vfl_dir = VFL_DIR_RX;
1465 vdev->lock = &common->lock;
1466 vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1467 video_set_drvdata(&ch->video_dev, ch);
1468 err = video_register_device(vdev,
1469 VFL_TYPE_VIDEO, (j ? 1 : 0));
1474 v4l2_info(&vpif_obj.v4l2_dev, "VPIF capture driver initialized\n");
1478 for (k = 0; k < j; k++) {
1479 /* Get the pointer to the channel object */
1480 ch = vpif_obj.dev[k];
1481 common = &ch->common[k];
1482 /* Unregister video device */
1483 video_unregister_device(&ch->video_dev);
1489 static int vpif_async_complete(struct v4l2_async_notifier *notifier)
1491 return vpif_probe_complete();
1494 static const struct v4l2_async_notifier_operations vpif_async_ops = {
1495 .bound = vpif_async_bound,
1496 .complete = vpif_async_complete,
1499 static struct vpif_capture_config *
1500 vpif_capture_get_pdata(struct platform_device *pdev)
1502 struct device_node *endpoint = NULL;
1503 struct device_node *rem = NULL;
1504 struct vpif_capture_config *pdata;
1505 struct vpif_subdev_info *sdinfo;
1506 struct vpif_capture_chan_config *chan;
1509 v4l2_async_notifier_init(&vpif_obj.notifier);
1512 * DT boot: OF node from parent device contains
1513 * video ports & endpoints data.
1515 if (pdev->dev.parent && pdev->dev.parent->of_node)
1516 pdev->dev.of_node = pdev->dev.parent->of_node;
1517 if (!IS_ENABLED(CONFIG_OF) || !pdev->dev.of_node)
1518 return pdev->dev.platform_data;
1520 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1523 pdata->subdev_info =
1524 devm_kcalloc(&pdev->dev,
1525 VPIF_CAPTURE_NUM_CHANNELS,
1526 sizeof(*pdata->subdev_info),
1529 if (!pdata->subdev_info)
1532 for (i = 0; i < VPIF_CAPTURE_NUM_CHANNELS; i++) {
1533 struct v4l2_fwnode_endpoint bus_cfg = { .bus_type = 0 };
1537 endpoint = of_graph_get_next_endpoint(pdev->dev.of_node,
1542 rem = of_graph_get_remote_port_parent(endpoint);
1544 dev_dbg(&pdev->dev, "Remote device at %pOF not found\n",
1549 sdinfo = &pdata->subdev_info[i];
1550 chan = &pdata->chan_config[i];
1551 chan->inputs = devm_kcalloc(&pdev->dev,
1552 VPIF_CAPTURE_NUM_CHANNELS,
1553 sizeof(*chan->inputs),
1558 chan->input_count++;
1559 chan->inputs[i].input.type = V4L2_INPUT_TYPE_CAMERA;
1560 chan->inputs[i].input.std = V4L2_STD_ALL;
1561 chan->inputs[i].input.capabilities = V4L2_IN_CAP_STD;
1563 err = v4l2_fwnode_endpoint_parse(of_fwnode_handle(endpoint),
1566 dev_err(&pdev->dev, "Could not parse the endpoint\n");
1571 dev_dbg(&pdev->dev, "Endpoint %pOF, bus_width = %d\n",
1572 endpoint, bus_cfg.bus.parallel.bus_width);
1574 flags = bus_cfg.bus.parallel.flags;
1576 if (flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
1577 chan->vpif_if.hd_pol = 1;
1579 if (flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH)
1580 chan->vpif_if.vd_pol = 1;
1582 dev_dbg(&pdev->dev, "Remote device %pOF found\n", rem);
1583 sdinfo->name = rem->full_name;
1585 pdata->asd[i] = v4l2_async_notifier_add_fwnode_subdev(
1586 &vpif_obj.notifier, of_fwnode_handle(rem),
1587 sizeof(struct v4l2_async_subdev));
1588 if (IS_ERR(pdata->asd[i]))
1595 of_node_put(endpoint);
1596 pdata->asd_sizes[0] = i;
1597 pdata->subdev_count = i;
1598 pdata->card_name = "DA850/OMAP-L138 Video Capture";
1604 of_node_put(endpoint);
1605 v4l2_async_notifier_cleanup(&vpif_obj.notifier);
1611 * vpif_probe : This function probes the vpif capture driver
1612 * @pdev: platform device pointer
1614 * This creates device entries by register itself to the V4L2 driver and
1615 * initializes fields of each channel objects
1617 static __init int vpif_probe(struct platform_device *pdev)
1619 struct vpif_subdev_info *subdevdata;
1620 struct i2c_adapter *i2c_adap;
1621 struct resource *res;
1626 pdev->dev.platform_data = vpif_capture_get_pdata(pdev);
1627 if (!pdev->dev.platform_data) {
1628 dev_warn(&pdev->dev, "Missing platform data. Giving up.\n");
1632 vpif_dev = &pdev->dev;
1634 err = initialize_vpif();
1636 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
1640 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
1642 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
1646 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
1647 err = devm_request_irq(&pdev->dev, res->start, vpif_channel_isr,
1648 IRQF_SHARED, VPIF_DRIVER_NAME,
1649 (void *)(&vpif_obj.dev[res_idx]->
1653 goto vpif_unregister;
1658 vpif_obj.config = pdev->dev.platform_data;
1660 subdev_count = vpif_obj.config->subdev_count;
1661 vpif_obj.sd = kcalloc(subdev_count, sizeof(*vpif_obj.sd), GFP_KERNEL);
1664 goto vpif_unregister;
1667 if (!vpif_obj.config->asd_sizes[0]) {
1668 int i2c_id = vpif_obj.config->i2c_adapter_id;
1670 i2c_adap = i2c_get_adapter(i2c_id);
1672 for (i = 0; i < subdev_count; i++) {
1673 subdevdata = &vpif_obj.config->subdev_info[i];
1675 v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
1681 if (!vpif_obj.sd[i]) {
1682 vpif_err("Error registering v4l2 subdevice\n");
1684 goto probe_subdev_out;
1686 v4l2_info(&vpif_obj.v4l2_dev,
1687 "registered sub device %s\n",
1690 err = vpif_probe_complete();
1692 goto probe_subdev_out;
1694 vpif_obj.notifier.ops = &vpif_async_ops;
1695 err = v4l2_async_notifier_register(&vpif_obj.v4l2_dev,
1696 &vpif_obj.notifier);
1698 vpif_err("Error registering async notifier\n");
1700 goto probe_subdev_out;
1707 /* free sub devices memory */
1710 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1714 v4l2_async_notifier_cleanup(&vpif_obj.notifier);
1720 * vpif_remove() - driver remove handler
1721 * @device: ptr to platform device structure
1723 * The vidoe device is unregistered
1725 static int vpif_remove(struct platform_device *device)
1727 struct channel_obj *ch;
1730 v4l2_async_notifier_unregister(&vpif_obj.notifier);
1731 v4l2_async_notifier_cleanup(&vpif_obj.notifier);
1732 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1735 /* un-register device */
1736 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1737 /* Get the pointer to the channel object */
1738 ch = vpif_obj.dev[i];
1739 /* Unregister video device */
1740 video_unregister_device(&ch->video_dev);
1741 kfree(vpif_obj.dev[i]);
1746 #ifdef CONFIG_PM_SLEEP
1748 * vpif_suspend: vpif device suspend
1749 * @dev: pointer to &struct device
1751 static int vpif_suspend(struct device *dev)
1754 struct common_obj *common;
1755 struct channel_obj *ch;
1758 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1759 /* Get the pointer to the channel object */
1760 ch = vpif_obj.dev[i];
1761 common = &ch->common[VPIF_VIDEO_INDEX];
1763 if (!vb2_start_streaming_called(&common->buffer_queue))
1766 mutex_lock(&common->lock);
1767 /* Disable channel */
1768 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
1770 channel0_intr_enable(0);
1772 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
1775 channel1_intr_enable(0);
1777 mutex_unlock(&common->lock);
1784 * vpif_resume: vpif device suspend
1786 static int vpif_resume(struct device *dev)
1788 struct common_obj *common;
1789 struct channel_obj *ch;
1792 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1793 /* Get the pointer to the channel object */
1794 ch = vpif_obj.dev[i];
1795 common = &ch->common[VPIF_VIDEO_INDEX];
1797 if (!vb2_start_streaming_called(&common->buffer_queue))
1800 mutex_lock(&common->lock);
1801 /* Enable channel */
1802 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
1804 channel0_intr_enable(1);
1806 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
1809 channel1_intr_enable(1);
1811 mutex_unlock(&common->lock);
1818 static SIMPLE_DEV_PM_OPS(vpif_pm_ops, vpif_suspend, vpif_resume);
1820 static __refdata struct platform_driver vpif_driver = {
1822 .name = VPIF_DRIVER_NAME,
1825 .probe = vpif_probe,
1826 .remove = vpif_remove,
1829 module_platform_driver(vpif_driver);