blob: b9d927d1e5a8e80d7eacaed62fc942d134a4bc4e [file] [log] [blame]
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001/*
2 * Copyright (C) 2009 Texas Instruments Inc
Lad, Prabhakar96787eb2014-05-16 10:33:55 -03003 * Copyright (C) 2014 Lad, Prabhakar <prabhakar.csengg@gmail.com>
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030015 * TODO : add support for VBI & HBI data service
16 * add static buffer allocation
17 */
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030018
Lad, Prabhakar012eef72013-04-19 05:53:29 -030019#include <linux/module.h>
20#include <linux/interrupt.h>
21#include <linux/platform_device.h>
22#include <linux/slab.h>
23
Lad, Prabhakar012eef72013-04-19 05:53:29 -030024#include <media/v4l2-ioctl.h>
Kevin Hilman4a5f8ae2017-06-06 20:37:39 -030025#include <media/v4l2-of.h>
26#include <media/i2c/tvp514x.h>
Lad, Prabhakar012eef72013-04-19 05:53:29 -030027
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030028#include "vpif.h"
Lad, Prabhakar012eef72013-04-19 05:53:29 -030029#include "vpif_capture.h"
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030030
31MODULE_DESCRIPTION("TI DaVinci VPIF Capture driver");
32MODULE_LICENSE("GPL");
Mauro Carvalho Chehab64dc3c12011-06-25 11:28:37 -030033MODULE_VERSION(VPIF_CAPTURE_VERSION);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030034
35#define vpif_err(fmt, arg...) v4l2_err(&vpif_obj.v4l2_dev, fmt, ## arg)
36#define vpif_dbg(level, debug, fmt, arg...) \
37 v4l2_dbg(level, debug, &vpif_obj.v4l2_dev, fmt, ## arg)
38
39static int debug = 1;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030040
41module_param(debug, int, 0644);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030042
43MODULE_PARM_DESC(debug, "Debug level 0-1");
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030044
Lad, Prabhakare75ea0c2014-05-16 10:33:46 -030045#define VPIF_DRIVER_NAME "vpif_capture"
Kevin Hilmanbff782d2016-12-16 22:47:54 -020046MODULE_ALIAS("platform:" VPIF_DRIVER_NAME);
Lad, Prabhakare75ea0c2014-05-16 10:33:46 -030047
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030048/* global variables */
49static struct vpif_device vpif_obj = { {NULL} };
50static struct device *vpif_dev;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030051static void vpif_calculate_offsets(struct channel_obj *ch);
52static void vpif_config_addr(struct channel_obj *ch, int muxmode);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030053
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -030054static u8 channel_first_int[VPIF_NUMBER_OF_OBJECTS][2] = { {1, 1} };
55
Lad, Prabhakarc66238f2014-05-16 10:33:45 -030056/* Is set to 1 in case of SDTV formats, 2 in case of HDTV formats. */
57static int ycmux_mode;
58
Junghak Sung2d700712015-09-22 10:30:30 -030059static inline
60struct vpif_cap_buffer *to_vpif_buffer(struct vb2_v4l2_buffer *vb)
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -030061{
62 return container_of(vb, struct vpif_cap_buffer, vb);
63}
64
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030065/**
Lad, Prabhakar23e76a22014-05-16 10:33:37 -030066 * vpif_buffer_prepare : callback function for buffer prepare
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030067 * @vb: ptr to vb2_buffer
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030068 *
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030069 * This is the callback function for buffer prepare when vb2_qbuf()
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030070 * function is called. The buffer is prepared and user space virtual address
71 * or user address is converted into physical address
72 */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030073static int vpif_buffer_prepare(struct vb2_buffer *vb)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030074{
Junghak Sung2d700712015-09-22 10:30:30 -030075 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030076 struct vb2_queue *q = vb->vb2_queue;
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -030077 struct channel_obj *ch = vb2_get_drv_priv(q);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030078 struct common_obj *common;
79 unsigned long addr;
80
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030081 vpif_dbg(2, debug, "vpif_buffer_prepare\n");
82
83 common = &ch->common[VPIF_VIDEO_INDEX];
84
Lad, Prabhakar23e76a22014-05-16 10:33:37 -030085 vb2_set_plane_payload(vb, 0, common->fmt.fmt.pix.sizeimage);
86 if (vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
87 return -EINVAL;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030088
Junghak Sung2d700712015-09-22 10:30:30 -030089 vbuf->field = common->fmt.fmt.pix.field;
Lad, Prabhakar23e76a22014-05-16 10:33:37 -030090
91 addr = vb2_dma_contig_plane_dma_addr(vb, 0);
92 if (!IS_ALIGNED((addr + common->ytop_off), 8) ||
93 !IS_ALIGNED((addr + common->ybtm_off), 8) ||
94 !IS_ALIGNED((addr + common->ctop_off), 8) ||
95 !IS_ALIGNED((addr + common->cbtm_off), 8)) {
96 vpif_dbg(1, debug, "offset is not aligned\n");
97 return -EINVAL;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030098 }
Lad, Prabhakar23e76a22014-05-16 10:33:37 -030099
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300100 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300101}
102
103/**
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300104 * vpif_buffer_queue_setup : Callback function for buffer setup.
105 * @vq: vb2_queue ptr
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300106 * @nbuffers: ptr to number of buffers requested by application
107 * @nplanes:: contains number of distinct video planes needed to hold a frame
108 * @sizes[]: contains the size (in bytes) of each plane.
Hans Verkuil36c0f8b2016-04-15 09:15:05 -0300109 * @alloc_devs: ptr to allocation context
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300110 *
111 * This callback function is called when reqbuf() is called to adjust
112 * the buffer count and buffer size
113 */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300114static int vpif_buffer_queue_setup(struct vb2_queue *vq,
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300115 unsigned int *nbuffers, unsigned int *nplanes,
Hans Verkuil36c0f8b2016-04-15 09:15:05 -0300116 unsigned int sizes[], struct device *alloc_devs[])
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300117{
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -0300118 struct channel_obj *ch = vb2_get_drv_priv(vq);
Hans Verkuildf9ecb02015-10-28 00:50:37 -0200119 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
120 unsigned size = common->fmt.fmt.pix.sizeimage;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300121
122 vpif_dbg(2, debug, "vpif_buffer_setup\n");
123
Hans Verkuildf9ecb02015-10-28 00:50:37 -0200124 if (*nplanes) {
125 if (sizes[0] < size)
126 return -EINVAL;
127 size = sizes[0];
128 }
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300129
Lad, Prabhakar837939d2014-05-16 10:33:38 -0300130 if (vq->num_buffers + *nbuffers < 3)
131 *nbuffers = 3 - vq->num_buffers;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300132
133 *nplanes = 1;
Hans Verkuildf9ecb02015-10-28 00:50:37 -0200134 sizes[0] = size;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300135
Lad, Prabhakar837939d2014-05-16 10:33:38 -0300136 /* Calculate the offset for Y and C data in the buffer */
137 vpif_calculate_offsets(ch);
138
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300139 return 0;
140}
141
142/**
143 * vpif_buffer_queue : Callback function to add buffer to DMA queue
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300144 * @vb: ptr to vb2_buffer
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300145 */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300146static void vpif_buffer_queue(struct vb2_buffer *vb)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300147{
Junghak Sung2d700712015-09-22 10:30:30 -0300148 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -0300149 struct channel_obj *ch = vb2_get_drv_priv(vb->vb2_queue);
Junghak Sung2d700712015-09-22 10:30:30 -0300150 struct vpif_cap_buffer *buf = to_vpif_buffer(vbuf);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300151 struct common_obj *common;
Hans Verkuilaec96832012-11-16 12:03:06 -0300152 unsigned long flags;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300153
154 common = &ch->common[VPIF_VIDEO_INDEX];
155
156 vpif_dbg(2, debug, "vpif_buffer_queue\n");
157
Hans Verkuilaec96832012-11-16 12:03:06 -0300158 spin_lock_irqsave(&common->irqlock, flags);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300159 /* add the buffer to the DMA queue */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300160 list_add_tail(&buf->list, &common->dma_queue);
Hans Verkuilaec96832012-11-16 12:03:06 -0300161 spin_unlock_irqrestore(&common->irqlock, flags);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300162}
163
Lad, Prabhakar41b9f242014-05-16 10:33:39 -0300164/**
165 * vpif_start_streaming : Starts the DMA engine for streaming
166 * @vb: ptr to vb2_buffer
167 * @count: number of buffers
168 */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300169static int vpif_start_streaming(struct vb2_queue *vq, unsigned int count)
170{
171 struct vpif_capture_config *vpif_config_data =
172 vpif_dev->platform_data;
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -0300173 struct channel_obj *ch = vb2_get_drv_priv(vq);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300174 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
175 struct vpif_params *vpif = &ch->vpifparams;
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300176 struct vpif_cap_buffer *buf, *tmp;
177 unsigned long addr, flags;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300178 int ret;
179
Lad, Prabhakarc66238f2014-05-16 10:33:45 -0300180 /* Initialize field_id */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300181 ch->field_id = 0;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300182
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300183 /* configure 1 or 2 channel mode */
Lad, Prabhakarf4ad8d72012-09-27 02:33:12 -0300184 if (vpif_config_data->setup_input_channel_mode) {
185 ret = vpif_config_data->
186 setup_input_channel_mode(vpif->std_info.ycmux_mode);
187 if (ret < 0) {
188 vpif_dbg(1, debug, "can't set vpif channel mode\n");
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300189 goto err;
Lad, Prabhakarf4ad8d72012-09-27 02:33:12 -0300190 }
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300191 }
192
Lad, Prabhakard9a3b132014-05-16 10:33:42 -0300193 ret = v4l2_subdev_call(ch->sd, video, s_stream, 1);
194 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
195 vpif_dbg(1, debug, "stream on failed in subdev\n");
196 goto err;
197 }
198
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300199 /* Call vpif_set_params function to set the parameters and addresses */
200 ret = vpif_set_video_params(vpif, ch->channel_id);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300201 if (ret < 0) {
202 vpif_dbg(1, debug, "can't set video params\n");
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300203 goto err;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300204 }
205
Lad, Prabhakarc66238f2014-05-16 10:33:45 -0300206 ycmux_mode = ret;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300207 vpif_config_addr(ch, ret);
208
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300209 /* Get the next frame from the buffer queue */
Kevin Hilman62dd4ac2016-12-07 16:30:23 -0200210 spin_lock_irqsave(&common->irqlock, flags);
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300211 common->cur_frm = common->next_frm = list_entry(common->dma_queue.next,
212 struct vpif_cap_buffer, list);
213 /* Remove buffer from the buffer queue */
214 list_del(&common->cur_frm->list);
215 spin_unlock_irqrestore(&common->irqlock, flags);
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300216
Junghak Sung2d700712015-09-22 10:30:30 -0300217 addr = vb2_dma_contig_plane_dma_addr(&common->cur_frm->vb.vb2_buf, 0);
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300218
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300219 common->set_addr(addr + common->ytop_off,
220 addr + common->ybtm_off,
221 addr + common->ctop_off,
222 addr + common->cbtm_off);
223
224 /**
225 * Set interrupt for both the fields in VPIF Register enable channel in
226 * VPIF register
227 */
Lad, Prabhakar9e184042012-09-14 10:22:24 -0300228 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
Lad, Prabhakar41b9f242014-05-16 10:33:39 -0300229 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300230 channel0_intr_assert();
231 channel0_intr_enable(1);
232 enable_channel0(1);
233 }
Lad, Prabhakar41b9f242014-05-16 10:33:39 -0300234 if (VPIF_CHANNEL1_VIDEO == ch->channel_id ||
Lad, Prabhakarc66238f2014-05-16 10:33:45 -0300235 ycmux_mode == 2) {
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300236 channel1_intr_assert();
237 channel1_intr_enable(1);
238 enable_channel1(1);
239 }
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300240
241 return 0;
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300242
243err:
Kevin Hilman62dd4ac2016-12-07 16:30:23 -0200244 spin_lock_irqsave(&common->irqlock, flags);
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300245 list_for_each_entry_safe(buf, tmp, &common->dma_queue, list) {
246 list_del(&buf->list);
Junghak Sung2d700712015-09-22 10:30:30 -0300247 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300248 }
Dan Carpenter92491962014-06-12 04:01:45 -0300249 spin_unlock_irqrestore(&common->irqlock, flags);
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300250
251 return ret;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300252}
253
Lad, Prabhakar41b9f242014-05-16 10:33:39 -0300254/**
255 * vpif_stop_streaming : Stop the DMA engine
256 * @vq: ptr to vb2_queue
257 *
258 * This callback stops the DMA engine and any remaining buffers
259 * in the DMA queue are released.
260 */
Hans Verkuile37559b2014-04-17 02:47:21 -0300261static void vpif_stop_streaming(struct vb2_queue *vq)
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300262{
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -0300263 struct channel_obj *ch = vb2_get_drv_priv(vq);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300264 struct common_obj *common;
Hans Verkuilaec96832012-11-16 12:03:06 -0300265 unsigned long flags;
Lad, Prabhakard9a3b132014-05-16 10:33:42 -0300266 int ret;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300267
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300268 common = &ch->common[VPIF_VIDEO_INDEX];
269
Lad, Prabhakare6ba3db2014-03-22 08:03:07 -0300270 /* Disable channel as per its device type and channel id */
271 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
272 enable_channel0(0);
273 channel0_intr_enable(0);
274 }
Lad, Prabhakar41b9f242014-05-16 10:33:39 -0300275 if (VPIF_CHANNEL1_VIDEO == ch->channel_id ||
Lad, Prabhakarc66238f2014-05-16 10:33:45 -0300276 ycmux_mode == 2) {
Lad, Prabhakare6ba3db2014-03-22 08:03:07 -0300277 enable_channel1(0);
278 channel1_intr_enable(0);
279 }
Lad, Prabhakarc66238f2014-05-16 10:33:45 -0300280
281 ycmux_mode = 0;
Lad, Prabhakare6ba3db2014-03-22 08:03:07 -0300282
Lad, Prabhakard9a3b132014-05-16 10:33:42 -0300283 ret = v4l2_subdev_call(ch->sd, video, s_stream, 0);
284 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
285 vpif_dbg(1, debug, "stream off failed in subdev\n");
286
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300287 /* release all active buffers */
Lad, Prabhakare6ba3db2014-03-22 08:03:07 -0300288 if (common->cur_frm == common->next_frm) {
Junghak Sung2d700712015-09-22 10:30:30 -0300289 vb2_buffer_done(&common->cur_frm->vb.vb2_buf,
290 VB2_BUF_STATE_ERROR);
Lad, Prabhakare6ba3db2014-03-22 08:03:07 -0300291 } else {
Markus Elfring03161cd2016-10-12 10:20:34 -0300292 if (common->cur_frm)
Junghak Sung2d700712015-09-22 10:30:30 -0300293 vb2_buffer_done(&common->cur_frm->vb.vb2_buf,
Lad, Prabhakare6ba3db2014-03-22 08:03:07 -0300294 VB2_BUF_STATE_ERROR);
Markus Elfring03161cd2016-10-12 10:20:34 -0300295 if (common->next_frm)
Junghak Sung2d700712015-09-22 10:30:30 -0300296 vb2_buffer_done(&common->next_frm->vb.vb2_buf,
Lad, Prabhakare6ba3db2014-03-22 08:03:07 -0300297 VB2_BUF_STATE_ERROR);
298 }
299
Kevin Hilman62dd4ac2016-12-07 16:30:23 -0200300 spin_lock_irqsave(&common->irqlock, flags);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300301 while (!list_empty(&common->dma_queue)) {
302 common->next_frm = list_entry(common->dma_queue.next,
303 struct vpif_cap_buffer, list);
304 list_del(&common->next_frm->list);
Junghak Sung2d700712015-09-22 10:30:30 -0300305 vb2_buffer_done(&common->next_frm->vb.vb2_buf,
306 VB2_BUF_STATE_ERROR);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300307 }
Hans Verkuilaec96832012-11-16 12:03:06 -0300308 spin_unlock_irqrestore(&common->irqlock, flags);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300309}
310
311static struct vb2_ops video_qops = {
312 .queue_setup = vpif_buffer_queue_setup,
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300313 .buf_prepare = vpif_buffer_prepare,
314 .start_streaming = vpif_start_streaming,
315 .stop_streaming = vpif_stop_streaming,
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300316 .buf_queue = vpif_buffer_queue,
Prabhakar Ladb41a97a2014-11-26 19:42:33 -0300317 .wait_prepare = vb2_ops_wait_prepare,
318 .wait_finish = vb2_ops_wait_finish,
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300319};
320
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300321/**
322 * vpif_process_buffer_complete: process a completed buffer
323 * @common: ptr to common channel object
324 *
325 * This function time stamp the buffer and mark it as DONE. It also
326 * wake up any process waiting on the QUEUE and set the next buffer
327 * as current
328 */
329static void vpif_process_buffer_complete(struct common_obj *common)
330{
Junghak Sungd6dd6452015-11-03 08:16:37 -0200331 common->cur_frm->vb.vb2_buf.timestamp = ktime_get_ns();
Junghak Sung2d700712015-09-22 10:30:30 -0300332 vb2_buffer_done(&common->cur_frm->vb.vb2_buf, VB2_BUF_STATE_DONE);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300333 /* Make curFrm pointing to nextFrm */
334 common->cur_frm = common->next_frm;
335}
336
337/**
338 * vpif_schedule_next_buffer: set next buffer address for capture
339 * @common : ptr to common channel object
340 *
341 * This function will get next buffer from the dma queue and
342 * set the buffer address in the vpif register for capture.
343 * the buffer is marked active
344 */
345static void vpif_schedule_next_buffer(struct common_obj *common)
346{
347 unsigned long addr = 0;
348
Hans Verkuilaec96832012-11-16 12:03:06 -0300349 spin_lock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300350 common->next_frm = list_entry(common->dma_queue.next,
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300351 struct vpif_cap_buffer, list);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300352 /* Remove that buffer from the buffer queue */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300353 list_del(&common->next_frm->list);
Hans Verkuilaec96832012-11-16 12:03:06 -0300354 spin_unlock(&common->irqlock);
Junghak Sung2d700712015-09-22 10:30:30 -0300355 addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb.vb2_buf, 0);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300356
357 /* Set top and bottom field addresses in VPIF registers */
358 common->set_addr(addr + common->ytop_off,
359 addr + common->ybtm_off,
360 addr + common->ctop_off,
361 addr + common->cbtm_off);
362}
363
364/**
365 * vpif_channel_isr : ISR handler for vpif capture
366 * @irq: irq number
367 * @dev_id: dev_id ptr
368 *
369 * It changes status of the captured buffer, takes next buffer from the queue
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300370 * and sets its address in VPIF registers
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300371 */
372static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
373{
374 struct vpif_device *dev = &vpif_obj;
375 struct common_obj *common;
376 struct channel_obj *ch;
Markus Elfringc64d61d2016-10-12 10:25:08 -0300377 int channel_id;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300378 int fid = -1, i;
379
380 channel_id = *(int *)(dev_id);
Manjunath Hadlib1fc4232012-04-13 04:43:10 -0300381 if (!vpif_intr_status(channel_id))
382 return IRQ_NONE;
383
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300384 ch = dev->dev[channel_id];
385
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300386 for (i = 0; i < VPIF_NUMBER_OF_OBJECTS; i++) {
387 common = &ch->common[i];
388 /* skip If streaming is not started in this channel */
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300389 /* Check the field format */
390 if (1 == ch->vpifparams.std_info.frm_fmt) {
391 /* Progressive mode */
Hans Verkuilaec96832012-11-16 12:03:06 -0300392 spin_lock(&common->irqlock);
393 if (list_empty(&common->dma_queue)) {
394 spin_unlock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300395 continue;
Hans Verkuilaec96832012-11-16 12:03:06 -0300396 }
397 spin_unlock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300398
399 if (!channel_first_int[i][channel_id])
400 vpif_process_buffer_complete(common);
401
402 channel_first_int[i][channel_id] = 0;
403
404 vpif_schedule_next_buffer(common);
405
406
407 channel_first_int[i][channel_id] = 0;
408 } else {
409 /**
410 * Interlaced mode. If it is first interrupt, ignore
411 * it
412 */
413 if (channel_first_int[i][channel_id]) {
414 channel_first_int[i][channel_id] = 0;
415 continue;
416 }
417 if (0 == i) {
418 ch->field_id ^= 1;
419 /* Get field id from VPIF registers */
420 fid = vpif_channel_getfid(ch->channel_id);
421 if (fid != ch->field_id) {
422 /**
423 * If field id does not match stored
424 * field id, make them in sync
425 */
426 if (0 == fid)
427 ch->field_id = fid;
428 return IRQ_HANDLED;
429 }
430 }
431 /* device field id and local field id are in sync */
432 if (0 == fid) {
433 /* this is even field */
434 if (common->cur_frm == common->next_frm)
435 continue;
436
437 /* mark the current buffer as done */
438 vpif_process_buffer_complete(common);
439 } else if (1 == fid) {
440 /* odd field */
Hans Verkuilaec96832012-11-16 12:03:06 -0300441 spin_lock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300442 if (list_empty(&common->dma_queue) ||
Hans Verkuilaec96832012-11-16 12:03:06 -0300443 (common->cur_frm != common->next_frm)) {
444 spin_unlock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300445 continue;
Hans Verkuilaec96832012-11-16 12:03:06 -0300446 }
447 spin_unlock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300448
449 vpif_schedule_next_buffer(common);
450 }
451 }
452 }
453 return IRQ_HANDLED;
454}
455
456/**
457 * vpif_update_std_info() - update standard related info
458 * @ch: ptr to channel object
459 *
460 * For a given standard selected by application, update values
461 * in the device data structures
462 */
463static int vpif_update_std_info(struct channel_obj *ch)
464{
465 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
466 struct vpif_params *vpifparams = &ch->vpifparams;
467 const struct vpif_channel_config_params *config;
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300468 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300469 struct video_obj *vid_ch = &ch->video;
470 int index;
471
472 vpif_dbg(2, debug, "vpif_update_std_info\n");
473
Mats Randgaardaa444402010-12-16 12:17:42 -0300474 for (index = 0; index < vpif_ch_params_count; index++) {
Lad, Prabhakarced9b212013-03-20 01:28:27 -0300475 config = &vpif_ch_params[index];
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300476 if (config->hd_sd == 0) {
477 vpif_dbg(2, debug, "SD format\n");
478 if (config->stdid & vid_ch->stdid) {
479 memcpy(std_info, config, sizeof(*config));
480 break;
481 }
482 } else {
483 vpif_dbg(2, debug, "HD format\n");
Hans Verkuil0598c172012-09-18 07:18:47 -0300484 if (!memcmp(&config->dv_timings, &vid_ch->dv_timings,
485 sizeof(vid_ch->dv_timings))) {
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300486 memcpy(std_info, config, sizeof(*config));
487 break;
488 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300489 }
490 }
491
492 /* standard not found */
Mats Randgaardaa444402010-12-16 12:17:42 -0300493 if (index == vpif_ch_params_count)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300494 return -EINVAL;
495
496 common->fmt.fmt.pix.width = std_info->width;
497 common->width = std_info->width;
498 common->fmt.fmt.pix.height = std_info->height;
499 common->height = std_info->height;
Lad, Prabhakara4965c52014-05-16 10:33:54 -0300500 common->fmt.fmt.pix.sizeimage = common->height * common->width * 2;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300501 common->fmt.fmt.pix.bytesperline = std_info->width;
502 vpifparams->video_params.hpitch = std_info->width;
503 vpifparams->video_params.storage_mode = std_info->frm_fmt;
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300504
Lad, Prabhakara4965c52014-05-16 10:33:54 -0300505 if (vid_ch->stdid)
506 common->fmt.fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
507 else
508 common->fmt.fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
509
510 if (ch->vpifparams.std_info.frm_fmt)
511 common->fmt.fmt.pix.field = V4L2_FIELD_NONE;
512 else
513 common->fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
514
515 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER)
516 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;
517 else
Nori, Sekhar8946ab32017-05-26 07:55:27 -0300518 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_NV16;
Lad, Prabhakara4965c52014-05-16 10:33:54 -0300519
520 common->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
521
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300522 return 0;
523}
524
525/**
526 * vpif_calculate_offsets : This function calculates buffers offsets
527 * @ch : ptr to channel object
528 *
529 * This function calculates buffer offsets for Y and C in the top and
530 * bottom field
531 */
532static void vpif_calculate_offsets(struct channel_obj *ch)
533{
Mauro Carvalho Chehab24ab6332014-08-21 15:49:06 -0500534 unsigned int hpitch, sizeimage;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300535 struct video_obj *vid_ch = &(ch->video);
536 struct vpif_params *vpifparams = &ch->vpifparams;
537 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
538 enum v4l2_field field = common->fmt.fmt.pix.field;
539
540 vpif_dbg(2, debug, "vpif_calculate_offsets\n");
541
542 if (V4L2_FIELD_ANY == field) {
543 if (vpifparams->std_info.frm_fmt)
544 vid_ch->buf_field = V4L2_FIELD_NONE;
545 else
546 vid_ch->buf_field = V4L2_FIELD_INTERLACED;
547 } else
548 vid_ch->buf_field = common->fmt.fmt.pix.field;
549
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300550 sizeimage = common->fmt.fmt.pix.sizeimage;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300551
552 hpitch = common->fmt.fmt.pix.bytesperline;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300553
554 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
555 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
556 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
557 common->ytop_off = 0;
558 common->ybtm_off = hpitch;
559 common->ctop_off = sizeimage / 2;
560 common->cbtm_off = sizeimage / 2 + hpitch;
561 } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
562 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
563 common->ytop_off = 0;
564 common->ybtm_off = sizeimage / 4;
565 common->ctop_off = sizeimage / 2;
566 common->cbtm_off = common->ctop_off + sizeimage / 4;
567 } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
568 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
569 common->ybtm_off = 0;
570 common->ytop_off = sizeimage / 4;
571 common->cbtm_off = sizeimage / 2;
572 common->ctop_off = common->cbtm_off + sizeimage / 4;
573 }
574 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
575 (V4L2_FIELD_INTERLACED == vid_ch->buf_field))
576 vpifparams->video_params.storage_mode = 1;
577 else
578 vpifparams->video_params.storage_mode = 0;
579
580 if (1 == vpifparams->std_info.frm_fmt)
581 vpifparams->video_params.hpitch =
582 common->fmt.fmt.pix.bytesperline;
583 else {
584 if ((field == V4L2_FIELD_ANY)
585 || (field == V4L2_FIELD_INTERLACED))
586 vpifparams->video_params.hpitch =
587 common->fmt.fmt.pix.bytesperline * 2;
588 else
589 vpifparams->video_params.hpitch =
590 common->fmt.fmt.pix.bytesperline;
591 }
592
593 ch->vpifparams.video_params.stdid = vpifparams->std_info.stdid;
594}
595
596/**
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300597 * vpif_get_default_field() - Get default field type based on interface
598 * @vpif_params - ptr to vpif params
599 */
600static inline enum v4l2_field vpif_get_default_field(
601 struct vpif_interface *iface)
602{
603 return (iface->if_type == VPIF_IF_RAW_BAYER) ? V4L2_FIELD_NONE :
604 V4L2_FIELD_INTERLACED;
605}
606
607/**
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300608 * vpif_config_addr() - function to configure buffer address in vpif
609 * @ch - channel ptr
610 * @muxmode - channel mux mode
611 */
612static void vpif_config_addr(struct channel_obj *ch, int muxmode)
613{
614 struct common_obj *common;
615
616 vpif_dbg(2, debug, "vpif_config_addr\n");
617
618 common = &(ch->common[VPIF_VIDEO_INDEX]);
619
620 if (VPIF_CHANNEL1_VIDEO == ch->channel_id)
621 common->set_addr = ch1_set_videobuf_addr;
622 else if (2 == muxmode)
623 common->set_addr = ch0_set_videobuf_addr_yc_nmux;
624 else
625 common->set_addr = ch0_set_videobuf_addr;
626}
627
628/**
Hans Verkuil178cce12012-09-20 09:06:30 -0300629 * vpif_input_to_subdev() - Maps input to sub device
630 * @vpif_cfg - global config ptr
631 * @chan_cfg - channel config ptr
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300632 * @input_index - Given input index from application
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300633 *
634 * lookup the sub device information for a given input index.
635 * we report all the inputs to application. inputs table also
636 * has sub device name for the each input
637 */
Hans Verkuil178cce12012-09-20 09:06:30 -0300638static int vpif_input_to_subdev(
639 struct vpif_capture_config *vpif_cfg,
640 struct vpif_capture_chan_config *chan_cfg,
641 int input_index)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300642{
Hans Verkuil178cce12012-09-20 09:06:30 -0300643 struct vpif_subdev_info *subdev_info;
644 const char *subdev_name;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300645 int i;
646
Hans Verkuil178cce12012-09-20 09:06:30 -0300647 vpif_dbg(2, debug, "vpif_input_to_subdev\n");
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300648
Kevin Hilmanbff782d2016-12-16 22:47:54 -0200649 if (!chan_cfg)
650 return -1;
651 if (input_index >= chan_cfg->input_count)
652 return -1;
Hans Verkuil178cce12012-09-20 09:06:30 -0300653 subdev_name = chan_cfg->inputs[input_index].subdev_name;
Markus Elfring03161cd2016-10-12 10:20:34 -0300654 if (!subdev_name)
Hans Verkuil178cce12012-09-20 09:06:30 -0300655 return -1;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300656
657 /* loop through the sub device list to get the sub device info */
658 for (i = 0; i < vpif_cfg->subdev_count; i++) {
659 subdev_info = &vpif_cfg->subdev_info[i];
Kevin Hilman4a5f8ae2017-06-06 20:37:39 -0300660 if (subdev_info && !strcmp(subdev_info->name, subdev_name))
Hans Verkuil178cce12012-09-20 09:06:30 -0300661 return i;
662 }
663 return -1;
664}
665
666/**
667 * vpif_set_input() - Select an input
668 * @vpif_cfg - global config ptr
669 * @ch - channel
670 * @_index - Given input index from application
671 *
672 * Select the given input.
673 */
674static int vpif_set_input(
675 struct vpif_capture_config *vpif_cfg,
676 struct channel_obj *ch,
677 int index)
678{
679 struct vpif_capture_chan_config *chan_cfg =
680 &vpif_cfg->chan_config[ch->channel_id];
681 struct vpif_subdev_info *subdev_info = NULL;
682 struct v4l2_subdev *sd = NULL;
683 u32 input = 0, output = 0;
684 int sd_index;
685 int ret;
686
687 sd_index = vpif_input_to_subdev(vpif_cfg, chan_cfg, index);
688 if (sd_index >= 0) {
689 sd = vpif_obj.sd[sd_index];
690 subdev_info = &vpif_cfg->subdev_info[sd_index];
Kevin Hilmanbff782d2016-12-16 22:47:54 -0200691 } else {
692 /* no subdevice, no input to setup */
693 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300694 }
695
Hans Verkuil178cce12012-09-20 09:06:30 -0300696 /* first setup input path from sub device to vpif */
697 if (sd && vpif_cfg->setup_input_path) {
698 ret = vpif_cfg->setup_input_path(ch->channel_id,
699 subdev_info->name);
700 if (ret < 0) {
701 vpif_dbg(1, debug, "couldn't setup input path for the" \
702 " sub device %s, for input index %d\n",
703 subdev_info->name, index);
704 return ret;
705 }
706 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300707
Hans Verkuil178cce12012-09-20 09:06:30 -0300708 if (sd) {
709 input = chan_cfg->inputs[index].input_route;
710 output = chan_cfg->inputs[index].output_route;
711 ret = v4l2_subdev_call(sd, video, s_routing,
712 input, output, 0);
713 if (ret < 0 && ret != -ENOIOCTLCMD) {
714 vpif_dbg(1, debug, "Failed to set input\n");
715 return ret;
716 }
717 }
718 ch->input_idx = index;
719 ch->sd = sd;
720 /* copy interface parameters to vpif */
Hans Verkuil0d4f35f2012-09-20 09:06:32 -0300721 ch->vpifparams.iface = chan_cfg->vpif_if;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300722
Hans Verkuil178cce12012-09-20 09:06:30 -0300723 /* update tvnorms from the sub device input info */
Lad, Prabhakar8eef6662015-03-08 18:57:23 -0300724 ch->video_dev.tvnorms = chan_cfg->inputs[index].input.std;
Hans Verkuil178cce12012-09-20 09:06:30 -0300725 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300726}
727
728/**
729 * vpif_querystd() - querystd handler
730 * @file: file ptr
731 * @priv: file handle
732 * @std_id: ptr to std id
733 *
734 * This function is called to detect standard at the selected input
735 */
736static int vpif_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
737{
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300738 struct video_device *vdev = video_devdata(file);
739 struct channel_obj *ch = video_get_drvdata(vdev);
Markus Elfring6a842cc2016-10-12 10:22:45 -0300740 int ret;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300741
742 vpif_dbg(2, debug, "vpif_querystd\n");
743
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300744 /* Call querystd function of decoder device */
Hans Verkuil178cce12012-09-20 09:06:30 -0300745 ret = v4l2_subdev_call(ch->sd, video, querystd, std_id);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300746
Hans Verkuil178cce12012-09-20 09:06:30 -0300747 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
748 return -ENODATA;
749 if (ret) {
750 vpif_dbg(1, debug, "Failed to query standard for sub devices\n");
751 return ret;
752 }
753
754 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300755}
756
757/**
758 * vpif_g_std() - get STD handler
759 * @file: file ptr
760 * @priv: file handle
761 * @std_id: ptr to std id
762 */
763static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
764{
Lad, Prabhakard557b7d2014-05-16 10:33:52 -0300765 struct vpif_capture_config *config = vpif_dev->platform_data;
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300766 struct video_device *vdev = video_devdata(file);
767 struct channel_obj *ch = video_get_drvdata(vdev);
Lad, Prabhakard557b7d2014-05-16 10:33:52 -0300768 struct vpif_capture_chan_config *chan_cfg;
769 struct v4l2_input input;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300770
771 vpif_dbg(2, debug, "vpif_g_std\n");
772
Markus Elfring03161cd2016-10-12 10:20:34 -0300773 if (!config->chan_config[ch->channel_id].inputs)
Lad, Prabhakard557b7d2014-05-16 10:33:52 -0300774 return -ENODATA;
775
776 chan_cfg = &config->chan_config[ch->channel_id];
777 input = chan_cfg->inputs[ch->input_idx].input;
778 if (input.capabilities != V4L2_IN_CAP_STD)
779 return -ENODATA;
780
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300781 *std = ch->video.stdid;
782 return 0;
783}
784
785/**
786 * vpif_s_std() - set STD handler
787 * @file: file ptr
788 * @priv: file handle
789 * @std_id: ptr to std id
790 */
Hans Verkuil314527a2013-03-15 06:10:40 -0300791static int vpif_s_std(struct file *file, void *priv, v4l2_std_id std_id)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300792{
Lad, Prabhakard557b7d2014-05-16 10:33:52 -0300793 struct vpif_capture_config *config = vpif_dev->platform_data;
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300794 struct video_device *vdev = video_devdata(file);
795 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300796 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakard557b7d2014-05-16 10:33:52 -0300797 struct vpif_capture_chan_config *chan_cfg;
798 struct v4l2_input input;
799 int ret;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300800
801 vpif_dbg(2, debug, "vpif_s_std\n");
802
Markus Elfring03161cd2016-10-12 10:20:34 -0300803 if (!config->chan_config[ch->channel_id].inputs)
Lad, Prabhakard557b7d2014-05-16 10:33:52 -0300804 return -ENODATA;
805
806 chan_cfg = &config->chan_config[ch->channel_id];
807 input = chan_cfg->inputs[ch->input_idx].input;
808 if (input.capabilities != V4L2_IN_CAP_STD)
809 return -ENODATA;
810
Lad, Prabhakarc66238f2014-05-16 10:33:45 -0300811 if (vb2_is_busy(&common->buffer_queue))
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300812 return -EBUSY;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300813
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300814 /* Call encoder subdevice function to set the standard */
Hans Verkuil314527a2013-03-15 06:10:40 -0300815 ch->video.stdid = std_id;
Hans Verkuil0598c172012-09-18 07:18:47 -0300816 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300817
818 /* Get the information about the standard */
819 if (vpif_update_std_info(ch)) {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300820 vpif_err("Error getting the standard info\n");
Hans Verkuil46656af2011-01-04 06:51:35 -0300821 return -EINVAL;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300822 }
823
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300824 /* set standard in the sub device */
Laurent Pinchart8774bed2014-04-28 16:53:01 -0300825 ret = v4l2_subdev_call(ch->sd, video, s_std, std_id);
Hans Verkuil178cce12012-09-20 09:06:30 -0300826 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300827 vpif_dbg(1, debug, "Failed to set standard for sub devices\n");
Hans Verkuil178cce12012-09-20 09:06:30 -0300828 return ret;
829 }
830 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300831}
832
833/**
834 * vpif_enum_input() - ENUMINPUT handler
835 * @file: file ptr
836 * @priv: file handle
837 * @input: ptr to input structure
838 */
839static int vpif_enum_input(struct file *file, void *priv,
840 struct v4l2_input *input)
841{
842
843 struct vpif_capture_config *config = vpif_dev->platform_data;
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300844 struct video_device *vdev = video_devdata(file);
845 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300846 struct vpif_capture_chan_config *chan_cfg;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300847
848 chan_cfg = &config->chan_config[ch->channel_id];
849
Lad, Prabhakara4965c52014-05-16 10:33:54 -0300850 if (input->index >= chan_cfg->input_count)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300851 return -EINVAL;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300852
853 memcpy(input, &chan_cfg->inputs[input->index].input,
854 sizeof(*input));
855 return 0;
856}
857
858/**
859 * vpif_g_input() - Get INPUT handler
860 * @file: file ptr
861 * @priv: file handle
862 * @index: ptr to input index
863 */
864static int vpif_g_input(struct file *file, void *priv, unsigned int *index)
865{
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300866 struct video_device *vdev = video_devdata(file);
867 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300868
Hans Verkuil6f47c6c2012-09-20 09:06:22 -0300869 *index = ch->input_idx;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300870 return 0;
871}
872
873/**
874 * vpif_s_input() - Set INPUT handler
875 * @file: file ptr
876 * @priv: file handle
877 * @index: input index
878 */
879static int vpif_s_input(struct file *file, void *priv, unsigned int index)
880{
881 struct vpif_capture_config *config = vpif_dev->platform_data;
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300882 struct video_device *vdev = video_devdata(file);
883 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300884 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300885 struct vpif_capture_chan_config *chan_cfg;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300886
887 chan_cfg = &config->chan_config[ch->channel_id];
888
Hans Verkuil7aaad132012-09-20 09:06:25 -0300889 if (index >= chan_cfg->input_count)
890 return -EINVAL;
891
Lad, Prabhakarc66238f2014-05-16 10:33:45 -0300892 if (vb2_is_busy(&common->buffer_queue))
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300893 return -EBUSY;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300894
Hans Verkuil178cce12012-09-20 09:06:30 -0300895 return vpif_set_input(config, ch, index);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300896}
897
898/**
899 * vpif_enum_fmt_vid_cap() - ENUM_FMT handler
900 * @file: file ptr
901 * @priv: file handle
902 * @index: input index
903 */
904static int vpif_enum_fmt_vid_cap(struct file *file, void *priv,
905 struct v4l2_fmtdesc *fmt)
906{
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300907 struct video_device *vdev = video_devdata(file);
908 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300909
910 if (fmt->index != 0) {
911 vpif_dbg(1, debug, "Invalid format index\n");
912 return -EINVAL;
913 }
914
915 /* Fill in the information about format */
916 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER) {
917 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
918 strcpy(fmt->description, "Raw Mode -Bayer Pattern GrRBGb");
919 fmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
920 } else {
921 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Nori, Sekhar8946ab32017-05-26 07:55:27 -0300922 strcpy(fmt->description, "YCbCr4:2:2 Semi-Planar");
923 fmt->pixelformat = V4L2_PIX_FMT_NV16;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300924 }
925 return 0;
926}
927
928/**
929 * vpif_try_fmt_vid_cap() - TRY_FMT handler
930 * @file: file ptr
931 * @priv: file handle
932 * @fmt: ptr to v4l2 format structure
933 */
934static int vpif_try_fmt_vid_cap(struct file *file, void *priv,
935 struct v4l2_format *fmt)
936{
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300937 struct video_device *vdev = video_devdata(file);
938 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300939 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
Lad, Prabhakara4965c52014-05-16 10:33:54 -0300940 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
Lad, Prabhakara4965c52014-05-16 10:33:54 -0300941
942 vpif_update_std_info(ch);
943
944 pixfmt->field = common->fmt.fmt.pix.field;
945 pixfmt->colorspace = common->fmt.fmt.pix.colorspace;
946 pixfmt->bytesperline = common->fmt.fmt.pix.width;
947 pixfmt->width = common->fmt.fmt.pix.width;
948 pixfmt->height = common->fmt.fmt.pix.height;
949 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height * 2;
950 pixfmt->priv = 0;
951
952 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300953}
954
955
956/**
957 * vpif_g_fmt_vid_cap() - Set INPUT handler
958 * @file: file ptr
959 * @priv: file handle
960 * @fmt: ptr to v4l2 format structure
961 */
962static int vpif_g_fmt_vid_cap(struct file *file, void *priv,
963 struct v4l2_format *fmt)
964{
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300965 struct video_device *vdev = video_devdata(file);
966 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300967 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
968
969 /* Check the validity of the buffer type */
970 if (common->fmt.type != fmt->type)
971 return -EINVAL;
972
973 /* Fill in the information about format */
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300974 *fmt = common->fmt;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300975 return 0;
976}
977
978/**
979 * vpif_s_fmt_vid_cap() - Set FMT handler
980 * @file: file ptr
981 * @priv: file handle
982 * @fmt: ptr to v4l2 format structure
983 */
984static int vpif_s_fmt_vid_cap(struct file *file, void *priv,
985 struct v4l2_format *fmt)
986{
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300987 struct video_device *vdev = video_devdata(file);
988 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300989 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakara4965c52014-05-16 10:33:54 -0300990 int ret;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300991
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300992 vpif_dbg(2, debug, "%s\n", __func__);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300993
Lad, Prabhakarc66238f2014-05-16 10:33:45 -0300994 if (vb2_is_busy(&common->buffer_queue))
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300995 return -EBUSY;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300996
Lad, Prabhakara4965c52014-05-16 10:33:54 -0300997 ret = vpif_try_fmt_vid_cap(file, priv, fmt);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300998 if (ret)
999 return ret;
Lad, Prabhakara4965c52014-05-16 10:33:54 -03001000
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001001 /* store the format in the channel object */
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001002 common->fmt = *fmt;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001003 return 0;
1004}
1005
1006/**
1007 * vpif_querycap() - QUERYCAP handler
1008 * @file: file ptr
1009 * @priv: file handle
1010 * @cap: ptr to v4l2_capability structure
1011 */
1012static int vpif_querycap(struct file *file, void *priv,
1013 struct v4l2_capability *cap)
1014{
1015 struct vpif_capture_config *config = vpif_dev->platform_data;
1016
Lad, Prabhakar626d533f2012-09-25 11:21:55 -03001017 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1018 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
Lad, Prabhakare75ea0c2014-05-16 10:33:46 -03001019 strlcpy(cap->driver, VPIF_DRIVER_NAME, sizeof(cap->driver));
Lad, Prabhakar626d533f2012-09-25 11:21:55 -03001020 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
1021 dev_name(vpif_dev));
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001022 strlcpy(cap->card, config->card_name, sizeof(cap->card));
1023
1024 return 0;
1025}
1026
1027/**
Hans Verkuil0598c172012-09-18 07:18:47 -03001028 * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001029 * @file: file ptr
1030 * @priv: file handle
Hans Verkuil0598c172012-09-18 07:18:47 -03001031 * @timings: input timings
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001032 */
Hans Verkuil0598c172012-09-18 07:18:47 -03001033static int
1034vpif_enum_dv_timings(struct file *file, void *priv,
1035 struct v4l2_enum_dv_timings *timings)
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001036{
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001037 struct vpif_capture_config *config = vpif_dev->platform_data;
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001038 struct video_device *vdev = video_devdata(file);
1039 struct channel_obj *ch = video_get_drvdata(vdev);
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001040 struct vpif_capture_chan_config *chan_cfg;
1041 struct v4l2_input input;
Hans Verkuil178cce12012-09-20 09:06:30 -03001042 int ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001043
Markus Elfring03161cd2016-10-12 10:20:34 -03001044 if (!config->chan_config[ch->channel_id].inputs)
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001045 return -ENODATA;
1046
1047 chan_cfg = &config->chan_config[ch->channel_id];
1048 input = chan_cfg->inputs[ch->input_idx].input;
1049 if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
1050 return -ENODATA;
1051
Laurent Pinchart10d21462014-01-31 09:04:19 -03001052 timings->pad = 0;
1053
1054 ret = v4l2_subdev_call(ch->sd, pad, enum_dv_timings, timings);
Wei Yongjune070f1b2012-10-30 09:45:00 -03001055 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
Hans Verkuil178cce12012-09-20 09:06:30 -03001056 return -EINVAL;
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001057
Hans Verkuil178cce12012-09-20 09:06:30 -03001058 return ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001059}
1060
1061/**
Hans Verkuil0598c172012-09-18 07:18:47 -03001062 * vpif_query_dv_timings() - QUERY_DV_TIMINGS handler
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001063 * @file: file ptr
1064 * @priv: file handle
Hans Verkuil0598c172012-09-18 07:18:47 -03001065 * @timings: input timings
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001066 */
Hans Verkuil0598c172012-09-18 07:18:47 -03001067static int
1068vpif_query_dv_timings(struct file *file, void *priv,
1069 struct v4l2_dv_timings *timings)
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001070{
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001071 struct vpif_capture_config *config = vpif_dev->platform_data;
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001072 struct video_device *vdev = video_devdata(file);
1073 struct channel_obj *ch = video_get_drvdata(vdev);
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001074 struct vpif_capture_chan_config *chan_cfg;
1075 struct v4l2_input input;
Hans Verkuil178cce12012-09-20 09:06:30 -03001076 int ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001077
Markus Elfring03161cd2016-10-12 10:20:34 -03001078 if (!config->chan_config[ch->channel_id].inputs)
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001079 return -ENODATA;
1080
1081 chan_cfg = &config->chan_config[ch->channel_id];
1082 input = chan_cfg->inputs[ch->input_idx].input;
1083 if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
1084 return -ENODATA;
1085
Hans Verkuil178cce12012-09-20 09:06:30 -03001086 ret = v4l2_subdev_call(ch->sd, video, query_dv_timings, timings);
Wei Yongjune070f1b2012-10-30 09:45:00 -03001087 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
Hans Verkuil178cce12012-09-20 09:06:30 -03001088 return -ENODATA;
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001089
Hans Verkuil178cce12012-09-20 09:06:30 -03001090 return ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001091}
1092
Mats Randgaardc027e162010-12-16 12:17:44 -03001093/**
1094 * vpif_s_dv_timings() - S_DV_TIMINGS handler
1095 * @file: file ptr
1096 * @priv: file handle
1097 * @timings: digital video timings
1098 */
1099static int vpif_s_dv_timings(struct file *file, void *priv,
1100 struct v4l2_dv_timings *timings)
1101{
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001102 struct vpif_capture_config *config = vpif_dev->platform_data;
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001103 struct video_device *vdev = video_devdata(file);
1104 struct channel_obj *ch = video_get_drvdata(vdev);
Mats Randgaardc027e162010-12-16 12:17:44 -03001105 struct vpif_params *vpifparams = &ch->vpifparams;
1106 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001107 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
Mats Randgaardc027e162010-12-16 12:17:44 -03001108 struct video_obj *vid_ch = &ch->video;
Hans Verkuil0598c172012-09-18 07:18:47 -03001109 struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001110 struct vpif_capture_chan_config *chan_cfg;
1111 struct v4l2_input input;
Mats Randgaardc027e162010-12-16 12:17:44 -03001112 int ret;
1113
Markus Elfring03161cd2016-10-12 10:20:34 -03001114 if (!config->chan_config[ch->channel_id].inputs)
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001115 return -ENODATA;
1116
1117 chan_cfg = &config->chan_config[ch->channel_id];
1118 input = chan_cfg->inputs[ch->input_idx].input;
1119 if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
1120 return -ENODATA;
1121
Mats Randgaardc027e162010-12-16 12:17:44 -03001122 if (timings->type != V4L2_DV_BT_656_1120) {
1123 vpif_dbg(2, debug, "Timing type not defined\n");
1124 return -EINVAL;
1125 }
1126
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001127 if (vb2_is_busy(&common->buffer_queue))
1128 return -EBUSY;
1129
Mats Randgaardc027e162010-12-16 12:17:44 -03001130 /* Configure subdevice timings, if any */
Hans Verkuil178cce12012-09-20 09:06:30 -03001131 ret = v4l2_subdev_call(ch->sd, video, s_dv_timings, timings);
1132 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1133 ret = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001134 if (ret < 0) {
1135 vpif_dbg(2, debug, "Error setting custom DV timings\n");
1136 return ret;
1137 }
1138
1139 if (!(timings->bt.width && timings->bt.height &&
1140 (timings->bt.hbackporch ||
1141 timings->bt.hfrontporch ||
1142 timings->bt.hsync) &&
1143 timings->bt.vfrontporch &&
1144 (timings->bt.vbackporch ||
1145 timings->bt.vsync))) {
Mauro Carvalho Chehabded026e2016-10-18 17:44:08 -02001146 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");
Mats Randgaardc027e162010-12-16 12:17:44 -03001147 return -EINVAL;
1148 }
1149
Hans Verkuil0598c172012-09-18 07:18:47 -03001150 vid_ch->dv_timings = *timings;
Mats Randgaardc027e162010-12-16 12:17:44 -03001151
1152 /* Configure video port timings */
1153
Hans Verkuile3655262013-07-29 08:41:00 -03001154 std_info->eav2sav = V4L2_DV_BT_BLANKING_WIDTH(bt) - 8;
Mats Randgaardc027e162010-12-16 12:17:44 -03001155 std_info->sav2eav = bt->width;
1156
1157 std_info->l1 = 1;
1158 std_info->l3 = bt->vsync + bt->vbackporch + 1;
1159
Hans Verkuile3655262013-07-29 08:41:00 -03001160 std_info->vsize = V4L2_DV_BT_FRAME_HEIGHT(bt);
Mats Randgaardc027e162010-12-16 12:17:44 -03001161 if (bt->interlaced) {
1162 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
Mats Randgaardc027e162010-12-16 12:17:44 -03001163 std_info->l5 = std_info->vsize/2 -
1164 (bt->vfrontporch - 1);
1165 std_info->l7 = std_info->vsize/2 + 1;
1166 std_info->l9 = std_info->l7 + bt->il_vsync +
1167 bt->il_vbackporch + 1;
1168 std_info->l11 = std_info->vsize -
1169 (bt->il_vfrontporch - 1);
1170 } else {
Mauro Carvalho Chehabded026e2016-10-18 17:44:08 -02001171 vpif_dbg(2, debug, "Required timing values for interlaced BT format missing\n");
Mats Randgaardc027e162010-12-16 12:17:44 -03001172 return -EINVAL;
1173 }
1174 } else {
Mats Randgaardc027e162010-12-16 12:17:44 -03001175 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
1176 }
1177 strncpy(std_info->name, "Custom timings BT656/1120", VPIF_MAX_NAME);
1178 std_info->width = bt->width;
1179 std_info->height = bt->height;
1180 std_info->frm_fmt = bt->interlaced ? 0 : 1;
1181 std_info->ycmux_mode = 0;
1182 std_info->capture_format = 0;
1183 std_info->vbi_supported = 0;
1184 std_info->hd_sd = 1;
1185 std_info->stdid = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001186
1187 vid_ch->stdid = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001188 return 0;
1189}
1190
1191/**
1192 * vpif_g_dv_timings() - G_DV_TIMINGS handler
1193 * @file: file ptr
1194 * @priv: file handle
1195 * @timings: digital video timings
1196 */
1197static int vpif_g_dv_timings(struct file *file, void *priv,
1198 struct v4l2_dv_timings *timings)
1199{
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001200 struct vpif_capture_config *config = vpif_dev->platform_data;
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001201 struct video_device *vdev = video_devdata(file);
1202 struct channel_obj *ch = video_get_drvdata(vdev);
Mats Randgaardc027e162010-12-16 12:17:44 -03001203 struct video_obj *vid_ch = &ch->video;
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001204 struct vpif_capture_chan_config *chan_cfg;
1205 struct v4l2_input input;
1206
Markus Elfring03161cd2016-10-12 10:20:34 -03001207 if (!config->chan_config[ch->channel_id].inputs)
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001208 return -ENODATA;
1209
1210 chan_cfg = &config->chan_config[ch->channel_id];
1211 input = chan_cfg->inputs[ch->input_idx].input;
1212 if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
1213 return -ENODATA;
Mats Randgaardc027e162010-12-16 12:17:44 -03001214
Hans Verkuil0598c172012-09-18 07:18:47 -03001215 *timings = vid_ch->dv_timings;
Mats Randgaardc027e162010-12-16 12:17:44 -03001216
1217 return 0;
1218}
1219
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001220/*
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001221 * vpif_log_status() - Status information
1222 * @file: file ptr
1223 * @priv: file handle
1224 *
1225 * Returns zero.
1226 */
1227static int vpif_log_status(struct file *filep, void *priv)
1228{
1229 /* status for sub devices */
1230 v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1231
1232 return 0;
1233}
1234
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001235/* vpif capture ioctl operations */
1236static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
Lad, Prabhakar7b4657f2014-05-16 10:33:49 -03001237 .vidioc_querycap = vpif_querycap,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001238 .vidioc_enum_fmt_vid_cap = vpif_enum_fmt_vid_cap,
Lad, Prabhakar7b4657f2014-05-16 10:33:49 -03001239 .vidioc_g_fmt_vid_cap = vpif_g_fmt_vid_cap,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001240 .vidioc_s_fmt_vid_cap = vpif_s_fmt_vid_cap,
1241 .vidioc_try_fmt_vid_cap = vpif_try_fmt_vid_cap,
Lad, Prabhakar7b4657f2014-05-16 10:33:49 -03001242
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001243 .vidioc_enum_input = vpif_enum_input,
1244 .vidioc_s_input = vpif_s_input,
1245 .vidioc_g_input = vpif_g_input,
Lad, Prabhakard9a3b132014-05-16 10:33:42 -03001246
1247 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1248 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1249 .vidioc_querybuf = vb2_ioctl_querybuf,
1250 .vidioc_qbuf = vb2_ioctl_qbuf,
1251 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1252 .vidioc_expbuf = vb2_ioctl_expbuf,
1253 .vidioc_streamon = vb2_ioctl_streamon,
1254 .vidioc_streamoff = vb2_ioctl_streamoff,
1255
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001256 .vidioc_querystd = vpif_querystd,
Lad, Prabhakar7b4657f2014-05-16 10:33:49 -03001257 .vidioc_s_std = vpif_s_std,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001258 .vidioc_g_std = vpif_g_std,
Lad, Prabhakard9a3b132014-05-16 10:33:42 -03001259
Lad, Prabhakar7b4657f2014-05-16 10:33:49 -03001260 .vidioc_enum_dv_timings = vpif_enum_dv_timings,
1261 .vidioc_query_dv_timings = vpif_query_dv_timings,
1262 .vidioc_s_dv_timings = vpif_s_dv_timings,
1263 .vidioc_g_dv_timings = vpif_g_dv_timings,
1264
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001265 .vidioc_log_status = vpif_log_status,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001266};
1267
1268/* vpif file operations */
1269static struct v4l2_file_operations vpif_fops = {
1270 .owner = THIS_MODULE,
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001271 .open = v4l2_fh_open,
1272 .release = vb2_fop_release,
Hans Verkuil46656af2011-01-04 06:51:35 -03001273 .unlocked_ioctl = video_ioctl2,
Lad, Prabhakard26d26b2014-05-16 10:33:40 -03001274 .mmap = vb2_fop_mmap,
1275 .poll = vb2_fop_poll
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001276};
1277
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001278/**
1279 * initialize_vpif() - Initialize vpif data structures
1280 *
1281 * Allocate memory for data structures and initialize them
1282 */
1283static int initialize_vpif(void)
1284{
Lad, Prabhakar4015bef2014-05-16 10:33:47 -03001285 int err, i, j;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001286 int free_channel_objects_index;
1287
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001288 /* Allocate memory for six channel objects */
1289 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1290 vpif_obj.dev[i] =
1291 kzalloc(sizeof(*vpif_obj.dev[i]), GFP_KERNEL);
1292 /* If memory allocation fails, return error */
1293 if (!vpif_obj.dev[i]) {
1294 free_channel_objects_index = i;
1295 err = -ENOMEM;
1296 goto vpif_init_free_channel_objects;
1297 }
1298 }
1299 return 0;
1300
1301vpif_init_free_channel_objects:
1302 for (j = 0; j < free_channel_objects_index; j++)
1303 kfree(vpif_obj.dev[j]);
1304 return err;
1305}
1306
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001307static int vpif_async_bound(struct v4l2_async_notifier *notifier,
1308 struct v4l2_subdev *subdev,
1309 struct v4l2_async_subdev *asd)
1310{
1311 int i;
1312
Kevin Hilman4a5f8ae2017-06-06 20:37:39 -03001313 for (i = 0; i < vpif_obj.config->asd_sizes[0]; i++) {
1314 struct v4l2_async_subdev *_asd = vpif_obj.config->asd[i];
1315 const struct device_node *node = _asd->match.of.node;
1316
1317 if (node == subdev->of_node) {
1318 vpif_obj.sd[i] = subdev;
1319 vpif_obj.config->chan_config->inputs[i].subdev_name =
1320 (char *)subdev->of_node->full_name;
1321 vpif_dbg(2, debug,
1322 "%s: setting input %d subdev_name = %s\n",
1323 __func__, i, subdev->of_node->full_name);
1324 return 0;
1325 }
1326 }
1327
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001328 for (i = 0; i < vpif_obj.config->subdev_count; i++)
1329 if (!strcmp(vpif_obj.config->subdev_info[i].name,
1330 subdev->name)) {
1331 vpif_obj.sd[i] = subdev;
1332 return 0;
1333 }
1334
1335 return -EINVAL;
1336}
1337
1338static int vpif_probe_complete(void)
1339{
1340 struct common_obj *common;
Lad, Prabhakard26d26b2014-05-16 10:33:40 -03001341 struct video_device *vdev;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001342 struct channel_obj *ch;
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001343 struct vb2_queue *q;
Lad, Prabhakar8eef6662015-03-08 18:57:23 -03001344 int j, err, k;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001345
1346 for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
1347 ch = vpif_obj.dev[j];
1348 ch->channel_id = j;
1349 common = &(ch->common[VPIF_VIDEO_INDEX]);
1350 spin_lock_init(&common->irqlock);
1351 mutex_init(&common->lock);
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001352
1353 /* select input 0 */
1354 err = vpif_set_input(vpif_obj.config, ch, 0);
1355 if (err)
1356 goto probe_out;
1357
Lad, Prabhakara4965c52014-05-16 10:33:54 -03001358 /* set initial format */
1359 ch->video.stdid = V4L2_STD_525_60;
1360 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
1361 vpif_update_std_info(ch);
1362
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001363 /* Initialize vb2 queue */
1364 q = &common->buffer_queue;
1365 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1366 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1367 q->drv_priv = ch;
1368 q->ops = &video_qops;
1369 q->mem_ops = &vb2_dma_contig_memops;
1370 q->buf_struct_size = sizeof(struct vpif_cap_buffer);
1371 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1372 q->min_buffers_needed = 1;
Lad, Prabhakar999ba692014-05-16 10:33:33 -03001373 q->lock = &common->lock;
Hans Verkuil53ddcc62016-02-15 13:09:10 -02001374 q->dev = vpif_dev;
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001375
1376 err = vb2_queue_init(q);
1377 if (err) {
1378 vpif_err("vpif_capture: vb2_queue_init() failed\n");
1379 goto probe_out;
1380 }
1381
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001382 INIT_LIST_HEAD(&common->dma_queue);
1383
Lad, Prabhakare75ea0c2014-05-16 10:33:46 -03001384 /* Initialize the video_device structure */
Lad, Prabhakar8eef6662015-03-08 18:57:23 -03001385 vdev = &ch->video_dev;
Lad, Prabhakare75ea0c2014-05-16 10:33:46 -03001386 strlcpy(vdev->name, VPIF_DRIVER_NAME, sizeof(vdev->name));
Lad, Prabhakar8eef6662015-03-08 18:57:23 -03001387 vdev->release = video_device_release_empty;
Lad, Prabhakare75ea0c2014-05-16 10:33:46 -03001388 vdev->fops = &vpif_fops;
1389 vdev->ioctl_ops = &vpif_ioctl_ops;
1390 vdev->v4l2_dev = &vpif_obj.v4l2_dev;
1391 vdev->vfl_dir = VFL_DIR_RX;
Lad, Prabhakard26d26b2014-05-16 10:33:40 -03001392 vdev->queue = q;
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001393 vdev->lock = &common->lock;
Lad, Prabhakar8eef6662015-03-08 18:57:23 -03001394 video_set_drvdata(&ch->video_dev, ch);
Lad, Prabhakard26d26b2014-05-16 10:33:40 -03001395 err = video_register_device(vdev,
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001396 VFL_TYPE_GRABBER, (j ? 1 : 0));
1397 if (err)
1398 goto probe_out;
1399 }
1400
1401 v4l2_info(&vpif_obj.v4l2_dev, "VPIF capture driver initialized\n");
1402 return 0;
1403
1404probe_out:
1405 for (k = 0; k < j; k++) {
1406 /* Get the pointer to the channel object */
1407 ch = vpif_obj.dev[k];
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001408 common = &ch->common[k];
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001409 /* Unregister video device */
Lad, Prabhakar8eef6662015-03-08 18:57:23 -03001410 video_unregister_device(&ch->video_dev);
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001411 }
1412 kfree(vpif_obj.sd);
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001413 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1414
1415 return err;
1416}
1417
1418static int vpif_async_complete(struct v4l2_async_notifier *notifier)
1419{
1420 return vpif_probe_complete();
1421}
1422
Kevin Hilman4a5f8ae2017-06-06 20:37:39 -03001423static struct vpif_capture_config *
1424vpif_capture_get_pdata(struct platform_device *pdev)
1425{
1426 struct device_node *endpoint = NULL;
1427 struct v4l2_of_endpoint bus_cfg;
1428 struct vpif_capture_config *pdata;
1429 struct vpif_subdev_info *sdinfo;
1430 struct vpif_capture_chan_config *chan;
1431 unsigned int i;
1432
1433 /*
1434 * DT boot: OF node from parent device contains
1435 * video ports & endpoints data.
1436 */
1437 if (pdev->dev.parent && pdev->dev.parent->of_node)
1438 pdev->dev.of_node = pdev->dev.parent->of_node;
1439 if (!IS_ENABLED(CONFIG_OF) || !pdev->dev.of_node)
1440 return pdev->dev.platform_data;
1441
1442 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1443 if (!pdata)
1444 return NULL;
1445 pdata->subdev_info =
1446 devm_kzalloc(&pdev->dev, sizeof(*pdata->subdev_info) *
1447 VPIF_CAPTURE_NUM_CHANNELS, GFP_KERNEL);
1448
1449 if (!pdata->subdev_info)
1450 return NULL;
1451
1452 for (i = 0; i < VPIF_CAPTURE_NUM_CHANNELS; i++) {
1453 struct device_node *rem;
1454 unsigned int flags;
1455 int err;
1456
1457 endpoint = of_graph_get_next_endpoint(pdev->dev.of_node,
1458 endpoint);
1459 if (!endpoint)
1460 break;
1461
1462 sdinfo = &pdata->subdev_info[i];
1463 chan = &pdata->chan_config[i];
1464 chan->inputs = devm_kzalloc(&pdev->dev,
1465 sizeof(*chan->inputs) *
1466 VPIF_CAPTURE_NUM_CHANNELS,
1467 GFP_KERNEL);
1468
1469 chan->input_count++;
1470 chan->inputs[i].input.type = V4L2_INPUT_TYPE_CAMERA;
1471 chan->inputs[i].input.std = V4L2_STD_ALL;
1472 chan->inputs[i].input.capabilities = V4L2_IN_CAP_STD;
1473
1474 err = v4l2_of_parse_endpoint(endpoint, &bus_cfg);
1475 if (err) {
1476 dev_err(&pdev->dev, "Could not parse the endpoint\n");
1477 goto done;
1478 }
1479 dev_dbg(&pdev->dev, "Endpoint %s, bus_width = %d\n",
1480 endpoint->full_name, bus_cfg.bus.parallel.bus_width);
1481 flags = bus_cfg.bus.parallel.flags;
1482
1483 if (flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
1484 chan->vpif_if.hd_pol = 1;
1485
1486 if (flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH)
1487 chan->vpif_if.vd_pol = 1;
1488
1489 rem = of_graph_get_remote_port_parent(endpoint);
1490 if (!rem) {
1491 dev_dbg(&pdev->dev, "Remote device at %s not found\n",
1492 endpoint->full_name);
1493 goto done;
1494 }
1495
1496 dev_dbg(&pdev->dev, "Remote device %s, %s found\n",
1497 rem->name, rem->full_name);
1498 sdinfo->name = rem->full_name;
1499
1500 pdata->asd[i] = devm_kzalloc(&pdev->dev,
1501 sizeof(struct v4l2_async_subdev),
1502 GFP_KERNEL);
1503 if (!pdata->asd[i]) {
1504 of_node_put(rem);
1505 pdata = NULL;
1506 goto done;
1507 }
1508
1509 pdata->asd[i]->match_type = V4L2_ASYNC_MATCH_OF;
1510 pdata->asd[i]->match.of.node = rem;
1511 of_node_put(rem);
1512 }
1513
1514done:
1515 pdata->asd_sizes[0] = i;
1516 pdata->subdev_count = i;
1517 pdata->card_name = "DA850/OMAP-L138 Video Capture";
1518
1519 return pdata;
1520}
1521
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001522/**
1523 * vpif_probe : This function probes the vpif capture driver
1524 * @pdev: platform device pointer
1525 *
1526 * This creates device entries by register itself to the V4L2 driver and
1527 * initializes fields of each channel objects
1528 */
1529static __init int vpif_probe(struct platform_device *pdev)
1530{
1531 struct vpif_subdev_info *subdevdata;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001532 struct i2c_adapter *i2c_adap;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001533 struct resource *res;
1534 int subdev_count;
Lad, Prabhakar8eef6662015-03-08 18:57:23 -03001535 int res_idx = 0;
1536 int i, err;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001537
Kevin Hilman4a5f8ae2017-06-06 20:37:39 -03001538 pdev->dev.platform_data = vpif_capture_get_pdata(pdev);
1539 if (!pdev->dev.platform_data) {
1540 dev_warn(&pdev->dev, "Missing platform data. Giving up.\n");
1541 return -EINVAL;
1542 }
1543
Kevin Hilmanbff782d2016-12-16 22:47:54 -02001544 if (!pdev->dev.platform_data) {
1545 dev_warn(&pdev->dev, "Missing platform data. Giving up.\n");
1546 return -EINVAL;
1547 }
1548
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001549 vpif_dev = &pdev->dev;
1550
1551 err = initialize_vpif();
1552 if (err) {
1553 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
1554 return err;
1555 }
1556
Hans Verkuild2f7a1a2011-12-13 05:44:42 -03001557 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
1558 if (err) {
1559 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
1560 return err;
1561 }
1562
Hans Verkuil5be452c2012-09-20 09:06:29 -03001563 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
Lad, Prabhakara76a0b32013-06-17 11:20:47 -03001564 err = devm_request_irq(&pdev->dev, res->start, vpif_channel_isr,
Lad, Prabhakare75ea0c2014-05-16 10:33:46 -03001565 IRQF_SHARED, VPIF_DRIVER_NAME,
Lad, Prabhakara76a0b32013-06-17 11:20:47 -03001566 (void *)(&vpif_obj.dev[res_idx]->
1567 channel_id));
1568 if (err) {
1569 err = -EINVAL;
1570 goto vpif_unregister;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001571 }
Hans Verkuil5be452c2012-09-20 09:06:29 -03001572 res_idx++;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001573 }
1574
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001575 vpif_obj.config = pdev->dev.platform_data;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001576
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001577 subdev_count = vpif_obj.config->subdev_count;
Markus Elfringb442c462016-10-12 10:15:34 -03001578 vpif_obj.sd = kcalloc(subdev_count, sizeof(*vpif_obj.sd), GFP_KERNEL);
Markus Elfring03161cd2016-10-12 10:20:34 -03001579 if (!vpif_obj.sd) {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001580 err = -ENOMEM;
Lad, Prabhakar8eef6662015-03-08 18:57:23 -03001581 goto vpif_unregister;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001582 }
1583
Kevin Hilman4a5f8ae2017-06-06 20:37:39 -03001584 if (!vpif_obj.config->asd_sizes[0]) {
Kevin Hilman2d40cb32016-12-07 16:30:22 -02001585 int i2c_id = vpif_obj.config->i2c_adapter_id;
1586
1587 i2c_adap = i2c_get_adapter(i2c_id);
1588 WARN_ON(!i2c_adap);
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001589 for (i = 0; i < subdev_count; i++) {
1590 subdevdata = &vpif_obj.config->subdev_info[i];
1591 vpif_obj.sd[i] =
1592 v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
1593 i2c_adap,
1594 &subdevdata->
1595 board_info,
1596 NULL);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001597
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001598 if (!vpif_obj.sd[i]) {
1599 vpif_err("Error registering v4l2 subdevice\n");
Wei Yongjun2fcd9dc2013-09-02 05:06:10 -03001600 err = -ENODEV;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001601 goto probe_subdev_out;
1602 }
1603 v4l2_info(&vpif_obj.v4l2_dev,
1604 "registered sub device %s\n",
1605 subdevdata->name);
1606 }
1607 vpif_probe_complete();
1608 } else {
Sylwester Nawrockie8419d02013-07-19 12:31:10 -03001609 vpif_obj.notifier.subdevs = vpif_obj.config->asd;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001610 vpif_obj.notifier.num_subdevs = vpif_obj.config->asd_sizes[0];
1611 vpif_obj.notifier.bound = vpif_async_bound;
1612 vpif_obj.notifier.complete = vpif_async_complete;
1613 err = v4l2_async_notifier_register(&vpif_obj.v4l2_dev,
1614 &vpif_obj.notifier);
1615 if (err) {
1616 vpif_err("Error registering async notifier\n");
1617 err = -EINVAL;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001618 goto probe_subdev_out;
1619 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001620 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001621
1622 return 0;
1623
Hans Verkuilb65814e2012-09-20 09:06:26 -03001624probe_subdev_out:
1625 /* free sub devices memory */
1626 kfree(vpif_obj.sd);
Lad, Prabhakarb2de4f22013-06-17 11:20:46 -03001627vpif_unregister:
Hans Verkuild2f7a1a2011-12-13 05:44:42 -03001628 v4l2_device_unregister(&vpif_obj.v4l2_dev);
Lad, Prabhakarb2de4f22013-06-17 11:20:46 -03001629
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001630 return err;
1631}
1632
1633/**
1634 * vpif_remove() - driver remove handler
1635 * @device: ptr to platform device structure
1636 *
1637 * The vidoe device is unregistered
1638 */
1639static int vpif_remove(struct platform_device *device)
1640{
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001641 struct common_obj *common;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001642 struct channel_obj *ch;
Lad, Prabhakarb2de4f22013-06-17 11:20:46 -03001643 int i;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001644
1645 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1646
Lad, Prabhakar410ca602013-06-17 11:20:44 -03001647 kfree(vpif_obj.sd);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001648 /* un-register device */
1649 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1650 /* Get the pointer to the channel object */
1651 ch = vpif_obj.dev[i];
Prabhakar Lade9b58722014-07-18 13:31:51 -03001652 common = &ch->common[VPIF_VIDEO_INDEX];
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001653 /* Unregister video device */
Lad, Prabhakar8eef6662015-03-08 18:57:23 -03001654 video_unregister_device(&ch->video_dev);
Lad, Prabhakar410ca602013-06-17 11:20:44 -03001655 kfree(vpif_obj.dev[i]);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001656 }
1657 return 0;
1658}
1659
Lad, Prabhakarb7047712014-05-16 10:33:50 -03001660#ifdef CONFIG_PM_SLEEP
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001661/**
1662 * vpif_suspend: vpif device suspend
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001663 */
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001664static int vpif_suspend(struct device *dev)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001665{
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001666
1667 struct common_obj *common;
1668 struct channel_obj *ch;
1669 int i;
1670
1671 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1672 /* Get the pointer to the channel object */
1673 ch = vpif_obj.dev[i];
1674 common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakarb7047712014-05-16 10:33:50 -03001675
Prabhakar Ladc54d4a02014-09-06 12:26:51 -03001676 if (!vb2_start_streaming_called(&common->buffer_queue))
Lad, Prabhakarb7047712014-05-16 10:33:50 -03001677 continue;
1678
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001679 mutex_lock(&common->lock);
Lad, Prabhakarb7047712014-05-16 10:33:50 -03001680 /* Disable channel */
1681 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
1682 enable_channel0(0);
1683 channel0_intr_enable(0);
1684 }
1685 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
1686 ycmux_mode == 2) {
1687 enable_channel1(0);
1688 channel1_intr_enable(0);
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001689 }
1690 mutex_unlock(&common->lock);
1691 }
1692
1693 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001694}
1695
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001696/*
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001697 * vpif_resume: vpif device suspend
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001698 */
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001699static int vpif_resume(struct device *dev)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001700{
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001701 struct common_obj *common;
1702 struct channel_obj *ch;
1703 int i;
1704
1705 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1706 /* Get the pointer to the channel object */
1707 ch = vpif_obj.dev[i];
1708 common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakarb7047712014-05-16 10:33:50 -03001709
Prabhakar Ladc54d4a02014-09-06 12:26:51 -03001710 if (!vb2_start_streaming_called(&common->buffer_queue))
Lad, Prabhakarb7047712014-05-16 10:33:50 -03001711 continue;
1712
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001713 mutex_lock(&common->lock);
Lad, Prabhakarb7047712014-05-16 10:33:50 -03001714 /* Enable channel */
1715 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
1716 enable_channel0(1);
1717 channel0_intr_enable(1);
1718 }
1719 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
1720 ycmux_mode == 2) {
1721 enable_channel1(1);
1722 channel1_intr_enable(1);
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001723 }
1724 mutex_unlock(&common->lock);
1725 }
1726
1727 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001728}
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001729#endif
1730
Lad, Prabhakarb7047712014-05-16 10:33:50 -03001731static SIMPLE_DEV_PM_OPS(vpif_pm_ops, vpif_suspend, vpif_resume);
1732
Mats Randgaardffa1b392010-08-30 10:30:36 -03001733static __refdata struct platform_driver vpif_driver = {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001734 .driver = {
Lad, Prabhakare75ea0c2014-05-16 10:33:46 -03001735 .name = VPIF_DRIVER_NAME,
Lad, Prabhakarb7047712014-05-16 10:33:50 -03001736 .pm = &vpif_pm_ops,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001737 },
1738 .probe = vpif_probe,
1739 .remove = vpif_remove,
1740};
1741
Lad, Prabhakarfe424b22013-06-17 11:20:45 -03001742module_platform_driver(vpif_driver);