blob: cb746e9a73f605d0005819886ecb2f7dd6ca5837 [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 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * TODO : add support for VBI & HBI data service
20 * add static buffer allocation
21 */
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030022
Lad, Prabhakar012eef72013-04-19 05:53:29 -030023#include <linux/module.h>
24#include <linux/interrupt.h>
25#include <linux/platform_device.h>
26#include <linux/slab.h>
27
Lad, Prabhakar012eef72013-04-19 05:53:29 -030028#include <media/v4l2-ioctl.h>
29
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030030#include "vpif.h"
Lad, Prabhakar012eef72013-04-19 05:53:29 -030031#include "vpif_capture.h"
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030032
33MODULE_DESCRIPTION("TI DaVinci VPIF Capture driver");
34MODULE_LICENSE("GPL");
Mauro Carvalho Chehab64dc3c12011-06-25 11:28:37 -030035MODULE_VERSION(VPIF_CAPTURE_VERSION);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030036
37#define vpif_err(fmt, arg...) v4l2_err(&vpif_obj.v4l2_dev, fmt, ## arg)
38#define vpif_dbg(level, debug, fmt, arg...) \
39 v4l2_dbg(level, debug, &vpif_obj.v4l2_dev, fmt, ## arg)
40
41static int debug = 1;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030042
43module_param(debug, int, 0644);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030044
45MODULE_PARM_DESC(debug, "Debug level 0-1");
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030046
Lad, Prabhakare75ea0c2014-05-16 10:33:46 -030047#define VPIF_DRIVER_NAME "vpif_capture"
48
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030049/* global variables */
50static struct vpif_device vpif_obj = { {NULL} };
51static struct device *vpif_dev;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030052static void vpif_calculate_offsets(struct channel_obj *ch);
53static void vpif_config_addr(struct channel_obj *ch, int muxmode);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030054
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -030055static u8 channel_first_int[VPIF_NUMBER_OF_OBJECTS][2] = { {1, 1} };
56
Lad, Prabhakarc66238f2014-05-16 10:33:45 -030057/* Is set to 1 in case of SDTV formats, 2 in case of HDTV formats. */
58static int ycmux_mode;
59
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -030060static inline struct vpif_cap_buffer *to_vpif_buffer(struct vb2_buffer *vb)
61{
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{
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030075 struct vb2_queue *q = vb->vb2_queue;
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -030076 struct channel_obj *ch = vb2_get_drv_priv(q);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030077 struct common_obj *common;
78 unsigned long addr;
79
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030080 vpif_dbg(2, debug, "vpif_buffer_prepare\n");
81
82 common = &ch->common[VPIF_VIDEO_INDEX];
83
Lad, Prabhakar23e76a22014-05-16 10:33:37 -030084 vb2_set_plane_payload(vb, 0, common->fmt.fmt.pix.sizeimage);
85 if (vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
86 return -EINVAL;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030087
Lad, Prabhakar23e76a22014-05-16 10:33:37 -030088 vb->v4l2_buf.field = common->fmt.fmt.pix.field;
89
90 addr = vb2_dma_contig_plane_dma_addr(vb, 0);
91 if (!IS_ALIGNED((addr + common->ytop_off), 8) ||
92 !IS_ALIGNED((addr + common->ybtm_off), 8) ||
93 !IS_ALIGNED((addr + common->ctop_off), 8) ||
94 !IS_ALIGNED((addr + common->cbtm_off), 8)) {
95 vpif_dbg(1, debug, "offset is not aligned\n");
96 return -EINVAL;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030097 }
Lad, Prabhakar23e76a22014-05-16 10:33:37 -030098
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030099 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300100}
101
102/**
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300103 * vpif_buffer_queue_setup : Callback function for buffer setup.
104 * @vq: vb2_queue ptr
105 * @fmt: v4l2 format
106 * @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.
109 * @alloc_ctxs: 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,
115 const struct v4l2_format *fmt,
116 unsigned int *nbuffers, unsigned int *nplanes,
117 unsigned int sizes[], void *alloc_ctxs[])
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300118{
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -0300119 struct channel_obj *ch = vb2_get_drv_priv(vq);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300120 struct common_obj *common;
121
122 common = &ch->common[VPIF_VIDEO_INDEX];
123
124 vpif_dbg(2, debug, "vpif_buffer_setup\n");
125
Lad, Prabhakar837939d2014-05-16 10:33:38 -0300126 if (fmt && fmt->fmt.pix.sizeimage < common->fmt.fmt.pix.sizeimage)
127 return -EINVAL;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300128
Lad, Prabhakar837939d2014-05-16 10:33:38 -0300129 if (vq->num_buffers + *nbuffers < 3)
130 *nbuffers = 3 - vq->num_buffers;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300131
132 *nplanes = 1;
Lad, Prabhakar837939d2014-05-16 10:33:38 -0300133 sizes[0] = fmt ? fmt->fmt.pix.sizeimage : common->fmt.fmt.pix.sizeimage;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300134 alloc_ctxs[0] = common->alloc_ctx;
135
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{
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -0300148 struct channel_obj *ch = vb2_get_drv_priv(vb->vb2_queue);
149 struct vpif_cap_buffer *buf = to_vpif_buffer(vb);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300150 struct common_obj *common;
Hans Verkuilaec96832012-11-16 12:03:06 -0300151 unsigned long flags;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300152
153 common = &ch->common[VPIF_VIDEO_INDEX];
154
155 vpif_dbg(2, debug, "vpif_buffer_queue\n");
156
Hans Verkuilaec96832012-11-16 12:03:06 -0300157 spin_lock_irqsave(&common->irqlock, flags);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300158 /* add the buffer to the DMA queue */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300159 list_add_tail(&buf->list, &common->dma_queue);
Hans Verkuilaec96832012-11-16 12:03:06 -0300160 spin_unlock_irqrestore(&common->irqlock, flags);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300161}
162
Lad, Prabhakar41b9f242014-05-16 10:33:39 -0300163/**
164 * vpif_start_streaming : Starts the DMA engine for streaming
165 * @vb: ptr to vb2_buffer
166 * @count: number of buffers
167 */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300168static int vpif_start_streaming(struct vb2_queue *vq, unsigned int count)
169{
170 struct vpif_capture_config *vpif_config_data =
171 vpif_dev->platform_data;
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -0300172 struct channel_obj *ch = vb2_get_drv_priv(vq);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300173 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
174 struct vpif_params *vpif = &ch->vpifparams;
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300175 struct vpif_cap_buffer *buf, *tmp;
176 unsigned long addr, flags;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300177 int ret;
178
Hans Verkuilaec96832012-11-16 12:03:06 -0300179 spin_lock_irqsave(&common->irqlock, flags);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300180
Lad, Prabhakarc66238f2014-05-16 10:33:45 -0300181 /* Initialize field_id */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300182 ch->field_id = 0;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300183
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300184 /* configure 1 or 2 channel mode */
Lad, Prabhakarf4ad8d72012-09-27 02:33:12 -0300185 if (vpif_config_data->setup_input_channel_mode) {
186 ret = vpif_config_data->
187 setup_input_channel_mode(vpif->std_info.ycmux_mode);
188 if (ret < 0) {
189 vpif_dbg(1, debug, "can't set vpif channel mode\n");
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300190 goto err;
Lad, Prabhakarf4ad8d72012-09-27 02:33:12 -0300191 }
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300192 }
193
Lad, Prabhakard9a3b132014-05-16 10:33:42 -0300194 ret = v4l2_subdev_call(ch->sd, video, s_stream, 1);
195 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
196 vpif_dbg(1, debug, "stream on failed in subdev\n");
197 goto err;
198 }
199
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300200 /* Call vpif_set_params function to set the parameters and addresses */
201 ret = vpif_set_video_params(vpif, ch->channel_id);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300202 if (ret < 0) {
203 vpif_dbg(1, debug, "can't set video params\n");
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300204 goto err;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300205 }
206
Lad, Prabhakarc66238f2014-05-16 10:33:45 -0300207 ycmux_mode = ret;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300208 vpif_config_addr(ch, ret);
209
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300210 /* Get the next frame from the buffer queue */
211 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);
216 /* Mark state of the current frame to active */
217 common->cur_frm->vb.state = VB2_BUF_STATE_ACTIVE;
218
219 addr = vb2_dma_contig_plane_dma_addr(&common->cur_frm->vb, 0);
220
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300221 common->set_addr(addr + common->ytop_off,
222 addr + common->ybtm_off,
223 addr + common->ctop_off,
224 addr + common->cbtm_off);
225
226 /**
227 * Set interrupt for both the fields in VPIF Register enable channel in
228 * VPIF register
229 */
Lad, Prabhakar9e184042012-09-14 10:22:24 -0300230 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
Lad, Prabhakar41b9f242014-05-16 10:33:39 -0300231 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300232 channel0_intr_assert();
233 channel0_intr_enable(1);
234 enable_channel0(1);
235 }
Lad, Prabhakar41b9f242014-05-16 10:33:39 -0300236 if (VPIF_CHANNEL1_VIDEO == ch->channel_id ||
Lad, Prabhakarc66238f2014-05-16 10:33:45 -0300237 ycmux_mode == 2) {
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300238 channel1_intr_assert();
239 channel1_intr_enable(1);
240 enable_channel1(1);
241 }
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300242
243 return 0;
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300244
245err:
246 list_for_each_entry_safe(buf, tmp, &common->dma_queue, list) {
247 list_del(&buf->list);
248 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_QUEUED);
249 }
250
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 */
Hans Verkuilaec96832012-11-16 12:03:06 -0300288 spin_lock_irqsave(&common->irqlock, flags);
Lad, Prabhakare6ba3db2014-03-22 08:03:07 -0300289 if (common->cur_frm == common->next_frm) {
290 vb2_buffer_done(&common->cur_frm->vb, VB2_BUF_STATE_ERROR);
291 } else {
292 if (common->cur_frm != NULL)
293 vb2_buffer_done(&common->cur_frm->vb,
294 VB2_BUF_STATE_ERROR);
295 if (common->next_frm != NULL)
296 vb2_buffer_done(&common->next_frm->vb,
297 VB2_BUF_STATE_ERROR);
298 }
299
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300300 while (!list_empty(&common->dma_queue)) {
301 common->next_frm = list_entry(common->dma_queue.next,
302 struct vpif_cap_buffer, list);
303 list_del(&common->next_frm->list);
304 vb2_buffer_done(&common->next_frm->vb, VB2_BUF_STATE_ERROR);
305 }
Hans Verkuilaec96832012-11-16 12:03:06 -0300306 spin_unlock_irqrestore(&common->irqlock, flags);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300307}
308
309static struct vb2_ops video_qops = {
310 .queue_setup = vpif_buffer_queue_setup,
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300311 .buf_prepare = vpif_buffer_prepare,
312 .start_streaming = vpif_start_streaming,
313 .stop_streaming = vpif_stop_streaming,
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300314 .buf_queue = vpif_buffer_queue,
315};
316
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300317/**
318 * vpif_process_buffer_complete: process a completed buffer
319 * @common: ptr to common channel object
320 *
321 * This function time stamp the buffer and mark it as DONE. It also
322 * wake up any process waiting on the QUEUE and set the next buffer
323 * as current
324 */
325static void vpif_process_buffer_complete(struct common_obj *common)
326{
Sakari Ailus8e6057b2012-09-15 15:14:42 -0300327 v4l2_get_timestamp(&common->cur_frm->vb.v4l2_buf.timestamp);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300328 vb2_buffer_done(&common->cur_frm->vb,
329 VB2_BUF_STATE_DONE);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300330 /* Make curFrm pointing to nextFrm */
331 common->cur_frm = common->next_frm;
332}
333
334/**
335 * vpif_schedule_next_buffer: set next buffer address for capture
336 * @common : ptr to common channel object
337 *
338 * This function will get next buffer from the dma queue and
339 * set the buffer address in the vpif register for capture.
340 * the buffer is marked active
341 */
342static void vpif_schedule_next_buffer(struct common_obj *common)
343{
344 unsigned long addr = 0;
345
Hans Verkuilaec96832012-11-16 12:03:06 -0300346 spin_lock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300347 common->next_frm = list_entry(common->dma_queue.next,
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300348 struct vpif_cap_buffer, list);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300349 /* Remove that buffer from the buffer queue */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300350 list_del(&common->next_frm->list);
Hans Verkuilaec96832012-11-16 12:03:06 -0300351 spin_unlock(&common->irqlock);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300352 common->next_frm->vb.state = VB2_BUF_STATE_ACTIVE;
353 addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb, 0);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300354
355 /* Set top and bottom field addresses in VPIF registers */
356 common->set_addr(addr + common->ytop_off,
357 addr + common->ybtm_off,
358 addr + common->ctop_off,
359 addr + common->cbtm_off);
360}
361
362/**
363 * vpif_channel_isr : ISR handler for vpif capture
364 * @irq: irq number
365 * @dev_id: dev_id ptr
366 *
367 * It changes status of the captured buffer, takes next buffer from the queue
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300368 * and sets its address in VPIF registers
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300369 */
370static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
371{
372 struct vpif_device *dev = &vpif_obj;
373 struct common_obj *common;
374 struct channel_obj *ch;
375 enum v4l2_field field;
376 int channel_id = 0;
377 int fid = -1, i;
378
379 channel_id = *(int *)(dev_id);
Manjunath Hadlib1fc4232012-04-13 04:43:10 -0300380 if (!vpif_intr_status(channel_id))
381 return IRQ_NONE;
382
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300383 ch = dev->dev[channel_id];
384
385 field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
386
387 for (i = 0; i < VPIF_NUMBER_OF_OBJECTS; i++) {
388 common = &ch->common[i];
389 /* skip If streaming is not started in this channel */
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300390 /* Check the field format */
391 if (1 == ch->vpifparams.std_info.frm_fmt) {
392 /* Progressive mode */
Hans Verkuilaec96832012-11-16 12:03:06 -0300393 spin_lock(&common->irqlock);
394 if (list_empty(&common->dma_queue)) {
395 spin_unlock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300396 continue;
Hans Verkuilaec96832012-11-16 12:03:06 -0300397 }
398 spin_unlock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300399
400 if (!channel_first_int[i][channel_id])
401 vpif_process_buffer_complete(common);
402
403 channel_first_int[i][channel_id] = 0;
404
405 vpif_schedule_next_buffer(common);
406
407
408 channel_first_int[i][channel_id] = 0;
409 } else {
410 /**
411 * Interlaced mode. If it is first interrupt, ignore
412 * it
413 */
414 if (channel_first_int[i][channel_id]) {
415 channel_first_int[i][channel_id] = 0;
416 continue;
417 }
418 if (0 == i) {
419 ch->field_id ^= 1;
420 /* Get field id from VPIF registers */
421 fid = vpif_channel_getfid(ch->channel_id);
422 if (fid != ch->field_id) {
423 /**
424 * If field id does not match stored
425 * field id, make them in sync
426 */
427 if (0 == fid)
428 ch->field_id = fid;
429 return IRQ_HANDLED;
430 }
431 }
432 /* device field id and local field id are in sync */
433 if (0 == fid) {
434 /* this is even field */
435 if (common->cur_frm == common->next_frm)
436 continue;
437
438 /* mark the current buffer as done */
439 vpif_process_buffer_complete(common);
440 } else if (1 == fid) {
441 /* odd field */
Hans Verkuilaec96832012-11-16 12:03:06 -0300442 spin_lock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300443 if (list_empty(&common->dma_queue) ||
Hans Verkuilaec96832012-11-16 12:03:06 -0300444 (common->cur_frm != common->next_frm)) {
445 spin_unlock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300446 continue;
Hans Verkuilaec96832012-11-16 12:03:06 -0300447 }
448 spin_unlock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300449
450 vpif_schedule_next_buffer(common);
451 }
452 }
453 }
454 return IRQ_HANDLED;
455}
456
457/**
458 * vpif_update_std_info() - update standard related info
459 * @ch: ptr to channel object
460 *
461 * For a given standard selected by application, update values
462 * in the device data structures
463 */
464static int vpif_update_std_info(struct channel_obj *ch)
465{
466 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
467 struct vpif_params *vpifparams = &ch->vpifparams;
468 const struct vpif_channel_config_params *config;
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300469 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300470 struct video_obj *vid_ch = &ch->video;
471 int index;
472
473 vpif_dbg(2, debug, "vpif_update_std_info\n");
474
Mats Randgaardaa444402010-12-16 12:17:42 -0300475 for (index = 0; index < vpif_ch_params_count; index++) {
Lad, Prabhakarced9b212013-03-20 01:28:27 -0300476 config = &vpif_ch_params[index];
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300477 if (config->hd_sd == 0) {
478 vpif_dbg(2, debug, "SD format\n");
479 if (config->stdid & vid_ch->stdid) {
480 memcpy(std_info, config, sizeof(*config));
481 break;
482 }
483 } else {
484 vpif_dbg(2, debug, "HD format\n");
Hans Verkuil0598c172012-09-18 07:18:47 -0300485 if (!memcmp(&config->dv_timings, &vid_ch->dv_timings,
486 sizeof(vid_ch->dv_timings))) {
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300487 memcpy(std_info, config, sizeof(*config));
488 break;
489 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300490 }
491 }
492
493 /* standard not found */
Mats Randgaardaa444402010-12-16 12:17:42 -0300494 if (index == vpif_ch_params_count)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300495 return -EINVAL;
496
497 common->fmt.fmt.pix.width = std_info->width;
498 common->width = std_info->width;
499 common->fmt.fmt.pix.height = std_info->height;
500 common->height = std_info->height;
501 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
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300505 return 0;
506}
507
508/**
509 * vpif_calculate_offsets : This function calculates buffers offsets
510 * @ch : ptr to channel object
511 *
512 * This function calculates buffer offsets for Y and C in the top and
513 * bottom field
514 */
515static void vpif_calculate_offsets(struct channel_obj *ch)
516{
517 unsigned int hpitch, vpitch, sizeimage;
518 struct video_obj *vid_ch = &(ch->video);
519 struct vpif_params *vpifparams = &ch->vpifparams;
520 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
521 enum v4l2_field field = common->fmt.fmt.pix.field;
522
523 vpif_dbg(2, debug, "vpif_calculate_offsets\n");
524
525 if (V4L2_FIELD_ANY == field) {
526 if (vpifparams->std_info.frm_fmt)
527 vid_ch->buf_field = V4L2_FIELD_NONE;
528 else
529 vid_ch->buf_field = V4L2_FIELD_INTERLACED;
530 } else
531 vid_ch->buf_field = common->fmt.fmt.pix.field;
532
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300533 sizeimage = common->fmt.fmt.pix.sizeimage;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300534
535 hpitch = common->fmt.fmt.pix.bytesperline;
536 vpitch = sizeimage / (hpitch * 2);
537
538 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
539 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
540 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
541 common->ytop_off = 0;
542 common->ybtm_off = hpitch;
543 common->ctop_off = sizeimage / 2;
544 common->cbtm_off = sizeimage / 2 + hpitch;
545 } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
546 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
547 common->ytop_off = 0;
548 common->ybtm_off = sizeimage / 4;
549 common->ctop_off = sizeimage / 2;
550 common->cbtm_off = common->ctop_off + sizeimage / 4;
551 } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
552 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
553 common->ybtm_off = 0;
554 common->ytop_off = sizeimage / 4;
555 common->cbtm_off = sizeimage / 2;
556 common->ctop_off = common->cbtm_off + sizeimage / 4;
557 }
558 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
559 (V4L2_FIELD_INTERLACED == vid_ch->buf_field))
560 vpifparams->video_params.storage_mode = 1;
561 else
562 vpifparams->video_params.storage_mode = 0;
563
564 if (1 == vpifparams->std_info.frm_fmt)
565 vpifparams->video_params.hpitch =
566 common->fmt.fmt.pix.bytesperline;
567 else {
568 if ((field == V4L2_FIELD_ANY)
569 || (field == V4L2_FIELD_INTERLACED))
570 vpifparams->video_params.hpitch =
571 common->fmt.fmt.pix.bytesperline * 2;
572 else
573 vpifparams->video_params.hpitch =
574 common->fmt.fmt.pix.bytesperline;
575 }
576
577 ch->vpifparams.video_params.stdid = vpifparams->std_info.stdid;
578}
579
580/**
581 * vpif_config_format: configure default frame format in the device
582 * ch : ptr to channel object
583 */
584static void vpif_config_format(struct channel_obj *ch)
585{
586 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
587
588 vpif_dbg(2, debug, "vpif_config_format\n");
589
590 common->fmt.fmt.pix.field = V4L2_FIELD_ANY;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300591 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER)
592 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;
593 else
594 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
595 common->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
596}
597
598/**
599 * vpif_get_default_field() - Get default field type based on interface
600 * @vpif_params - ptr to vpif params
601 */
602static inline enum v4l2_field vpif_get_default_field(
603 struct vpif_interface *iface)
604{
605 return (iface->if_type == VPIF_IF_RAW_BAYER) ? V4L2_FIELD_NONE :
606 V4L2_FIELD_INTERLACED;
607}
608
609/**
610 * vpif_check_format() - check given pixel format for compatibility
611 * @ch - channel ptr
612 * @pixfmt - Given pixel format
613 * @update - update the values as per hardware requirement
614 *
615 * Check the application pixel format for S_FMT and update the input
616 * values as per hardware limits for TRY_FMT. The default pixel and
617 * field format is selected based on interface type.
618 */
619static int vpif_check_format(struct channel_obj *ch,
620 struct v4l2_pix_format *pixfmt,
621 int update)
622{
623 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
624 struct vpif_params *vpif_params = &ch->vpifparams;
625 enum v4l2_field field = pixfmt->field;
626 u32 sizeimage, hpitch, vpitch;
627 int ret = -EINVAL;
628
629 vpif_dbg(2, debug, "vpif_check_format\n");
630 /**
631 * first check for the pixel format. If if_type is Raw bayer,
632 * only V4L2_PIX_FMT_SBGGR8 format is supported. Otherwise only
633 * V4L2_PIX_FMT_YUV422P is supported
634 */
635 if (vpif_params->iface.if_type == VPIF_IF_RAW_BAYER) {
636 if (pixfmt->pixelformat != V4L2_PIX_FMT_SBGGR8) {
637 if (!update) {
638 vpif_dbg(2, debug, "invalid pix format\n");
639 goto exit;
640 }
641 pixfmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
642 }
643 } else {
644 if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P) {
645 if (!update) {
646 vpif_dbg(2, debug, "invalid pixel format\n");
647 goto exit;
648 }
649 pixfmt->pixelformat = V4L2_PIX_FMT_YUV422P;
650 }
651 }
652
653 if (!(VPIF_VALID_FIELD(field))) {
654 if (!update) {
655 vpif_dbg(2, debug, "invalid field format\n");
656 goto exit;
657 }
658 /**
659 * By default use FIELD_NONE for RAW Bayer capture
660 * and FIELD_INTERLACED for other interfaces
661 */
662 field = vpif_get_default_field(&vpif_params->iface);
663 } else if (field == V4L2_FIELD_ANY)
664 /* unsupported field. Use default */
665 field = vpif_get_default_field(&vpif_params->iface);
666
667 /* validate the hpitch */
668 hpitch = pixfmt->bytesperline;
669 if (hpitch < vpif_params->std_info.width) {
670 if (!update) {
671 vpif_dbg(2, debug, "invalid hpitch\n");
672 goto exit;
673 }
674 hpitch = vpif_params->std_info.width;
675 }
676
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300677 sizeimage = pixfmt->sizeimage;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300678
679 vpitch = sizeimage / (hpitch * 2);
680
681 /* validate the vpitch */
682 if (vpitch < vpif_params->std_info.height) {
683 if (!update) {
684 vpif_dbg(2, debug, "Invalid vpitch\n");
685 goto exit;
686 }
687 vpitch = vpif_params->std_info.height;
688 }
689
690 /* Check for 8 byte alignment */
691 if (!ALIGN(hpitch, 8)) {
692 if (!update) {
693 vpif_dbg(2, debug, "invalid pitch alignment\n");
694 goto exit;
695 }
696 /* adjust to next 8 byte boundary */
697 hpitch = (((hpitch + 7) / 8) * 8);
698 }
699 /* if update is set, modify the bytesperline and sizeimage */
700 if (update) {
701 pixfmt->bytesperline = hpitch;
702 pixfmt->sizeimage = hpitch * vpitch * 2;
703 }
704 /**
705 * Image width and height is always based on current standard width and
706 * height
707 */
708 pixfmt->width = common->fmt.fmt.pix.width;
709 pixfmt->height = common->fmt.fmt.pix.height;
710 return 0;
711exit:
712 return ret;
713}
714
715/**
716 * vpif_config_addr() - function to configure buffer address in vpif
717 * @ch - channel ptr
718 * @muxmode - channel mux mode
719 */
720static void vpif_config_addr(struct channel_obj *ch, int muxmode)
721{
722 struct common_obj *common;
723
724 vpif_dbg(2, debug, "vpif_config_addr\n");
725
726 common = &(ch->common[VPIF_VIDEO_INDEX]);
727
728 if (VPIF_CHANNEL1_VIDEO == ch->channel_id)
729 common->set_addr = ch1_set_videobuf_addr;
730 else if (2 == muxmode)
731 common->set_addr = ch0_set_videobuf_addr_yc_nmux;
732 else
733 common->set_addr = ch0_set_videobuf_addr;
734}
735
736/**
Hans Verkuil178cce12012-09-20 09:06:30 -0300737 * vpif_input_to_subdev() - Maps input to sub device
738 * @vpif_cfg - global config ptr
739 * @chan_cfg - channel config ptr
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300740 * @input_index - Given input index from application
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300741 *
742 * lookup the sub device information for a given input index.
743 * we report all the inputs to application. inputs table also
744 * has sub device name for the each input
745 */
Hans Verkuil178cce12012-09-20 09:06:30 -0300746static int vpif_input_to_subdev(
747 struct vpif_capture_config *vpif_cfg,
748 struct vpif_capture_chan_config *chan_cfg,
749 int input_index)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300750{
Hans Verkuil178cce12012-09-20 09:06:30 -0300751 struct vpif_subdev_info *subdev_info;
752 const char *subdev_name;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300753 int i;
754
Hans Verkuil178cce12012-09-20 09:06:30 -0300755 vpif_dbg(2, debug, "vpif_input_to_subdev\n");
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300756
Hans Verkuil178cce12012-09-20 09:06:30 -0300757 subdev_name = chan_cfg->inputs[input_index].subdev_name;
758 if (subdev_name == NULL)
759 return -1;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300760
761 /* loop through the sub device list to get the sub device info */
762 for (i = 0; i < vpif_cfg->subdev_count; i++) {
763 subdev_info = &vpif_cfg->subdev_info[i];
764 if (!strcmp(subdev_info->name, subdev_name))
Hans Verkuil178cce12012-09-20 09:06:30 -0300765 return i;
766 }
767 return -1;
768}
769
770/**
771 * vpif_set_input() - Select an input
772 * @vpif_cfg - global config ptr
773 * @ch - channel
774 * @_index - Given input index from application
775 *
776 * Select the given input.
777 */
778static int vpif_set_input(
779 struct vpif_capture_config *vpif_cfg,
780 struct channel_obj *ch,
781 int index)
782{
783 struct vpif_capture_chan_config *chan_cfg =
784 &vpif_cfg->chan_config[ch->channel_id];
785 struct vpif_subdev_info *subdev_info = NULL;
786 struct v4l2_subdev *sd = NULL;
787 u32 input = 0, output = 0;
788 int sd_index;
789 int ret;
790
791 sd_index = vpif_input_to_subdev(vpif_cfg, chan_cfg, index);
792 if (sd_index >= 0) {
793 sd = vpif_obj.sd[sd_index];
794 subdev_info = &vpif_cfg->subdev_info[sd_index];
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300795 }
796
Hans Verkuil178cce12012-09-20 09:06:30 -0300797 /* first setup input path from sub device to vpif */
798 if (sd && vpif_cfg->setup_input_path) {
799 ret = vpif_cfg->setup_input_path(ch->channel_id,
800 subdev_info->name);
801 if (ret < 0) {
802 vpif_dbg(1, debug, "couldn't setup input path for the" \
803 " sub device %s, for input index %d\n",
804 subdev_info->name, index);
805 return ret;
806 }
807 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300808
Hans Verkuil178cce12012-09-20 09:06:30 -0300809 if (sd) {
810 input = chan_cfg->inputs[index].input_route;
811 output = chan_cfg->inputs[index].output_route;
812 ret = v4l2_subdev_call(sd, video, s_routing,
813 input, output, 0);
814 if (ret < 0 && ret != -ENOIOCTLCMD) {
815 vpif_dbg(1, debug, "Failed to set input\n");
816 return ret;
817 }
818 }
819 ch->input_idx = index;
820 ch->sd = sd;
821 /* copy interface parameters to vpif */
Hans Verkuil0d4f35f2012-09-20 09:06:32 -0300822 ch->vpifparams.iface = chan_cfg->vpif_if;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300823
Hans Verkuil178cce12012-09-20 09:06:30 -0300824 /* update tvnorms from the sub device input info */
825 ch->video_dev->tvnorms = chan_cfg->inputs[index].input.std;
826 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300827}
828
829/**
830 * vpif_querystd() - querystd handler
831 * @file: file ptr
832 * @priv: file handle
833 * @std_id: ptr to std id
834 *
835 * This function is called to detect standard at the selected input
836 */
837static int vpif_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
838{
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300839 struct video_device *vdev = video_devdata(file);
840 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300841 int ret = 0;
842
843 vpif_dbg(2, debug, "vpif_querystd\n");
844
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300845 /* Call querystd function of decoder device */
Hans Verkuil178cce12012-09-20 09:06:30 -0300846 ret = v4l2_subdev_call(ch->sd, video, querystd, std_id);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300847
Hans Verkuil178cce12012-09-20 09:06:30 -0300848 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
849 return -ENODATA;
850 if (ret) {
851 vpif_dbg(1, debug, "Failed to query standard for sub devices\n");
852 return ret;
853 }
854
855 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300856}
857
858/**
859 * vpif_g_std() - get STD handler
860 * @file: file ptr
861 * @priv: file handle
862 * @std_id: ptr to std id
863 */
864static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
865{
Lad, Prabhakard557b7d2014-05-16 10:33:52 -0300866 struct vpif_capture_config *config = vpif_dev->platform_data;
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300867 struct video_device *vdev = video_devdata(file);
868 struct channel_obj *ch = video_get_drvdata(vdev);
Lad, Prabhakard557b7d2014-05-16 10:33:52 -0300869 struct vpif_capture_chan_config *chan_cfg;
870 struct v4l2_input input;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300871
872 vpif_dbg(2, debug, "vpif_g_std\n");
873
Lad, Prabhakard557b7d2014-05-16 10:33:52 -0300874 if (config->chan_config[ch->channel_id].inputs == NULL)
875 return -ENODATA;
876
877 chan_cfg = &config->chan_config[ch->channel_id];
878 input = chan_cfg->inputs[ch->input_idx].input;
879 if (input.capabilities != V4L2_IN_CAP_STD)
880 return -ENODATA;
881
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300882 *std = ch->video.stdid;
883 return 0;
884}
885
886/**
887 * vpif_s_std() - set STD handler
888 * @file: file ptr
889 * @priv: file handle
890 * @std_id: ptr to std id
891 */
Hans Verkuil314527a2013-03-15 06:10:40 -0300892static int vpif_s_std(struct file *file, void *priv, v4l2_std_id std_id)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300893{
Lad, Prabhakard557b7d2014-05-16 10:33:52 -0300894 struct vpif_capture_config *config = vpif_dev->platform_data;
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300895 struct video_device *vdev = video_devdata(file);
896 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300897 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakard557b7d2014-05-16 10:33:52 -0300898 struct vpif_capture_chan_config *chan_cfg;
899 struct v4l2_input input;
900 int ret;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300901
902 vpif_dbg(2, debug, "vpif_s_std\n");
903
Lad, Prabhakard557b7d2014-05-16 10:33:52 -0300904 if (config->chan_config[ch->channel_id].inputs == NULL)
905 return -ENODATA;
906
907 chan_cfg = &config->chan_config[ch->channel_id];
908 input = chan_cfg->inputs[ch->input_idx].input;
909 if (input.capabilities != V4L2_IN_CAP_STD)
910 return -ENODATA;
911
Lad, Prabhakarc66238f2014-05-16 10:33:45 -0300912 if (vb2_is_busy(&common->buffer_queue))
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300913 return -EBUSY;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300914
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300915 /* Call encoder subdevice function to set the standard */
Hans Verkuil314527a2013-03-15 06:10:40 -0300916 ch->video.stdid = std_id;
Hans Verkuil0598c172012-09-18 07:18:47 -0300917 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300918
919 /* Get the information about the standard */
920 if (vpif_update_std_info(ch)) {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300921 vpif_err("Error getting the standard info\n");
Hans Verkuil46656af2011-01-04 06:51:35 -0300922 return -EINVAL;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300923 }
924
925 /* Configure the default format information */
926 vpif_config_format(ch);
927
928 /* set standard in the sub device */
Laurent Pinchart8774bed2014-04-28 16:53:01 -0300929 ret = v4l2_subdev_call(ch->sd, video, s_std, std_id);
Hans Verkuil178cce12012-09-20 09:06:30 -0300930 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300931 vpif_dbg(1, debug, "Failed to set standard for sub devices\n");
Hans Verkuil178cce12012-09-20 09:06:30 -0300932 return ret;
933 }
934 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300935}
936
937/**
938 * vpif_enum_input() - ENUMINPUT handler
939 * @file: file ptr
940 * @priv: file handle
941 * @input: ptr to input structure
942 */
943static int vpif_enum_input(struct file *file, void *priv,
944 struct v4l2_input *input)
945{
946
947 struct vpif_capture_config *config = vpif_dev->platform_data;
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300948 struct video_device *vdev = video_devdata(file);
949 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300950 struct vpif_capture_chan_config *chan_cfg;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300951
952 chan_cfg = &config->chan_config[ch->channel_id];
953
954 if (input->index >= chan_cfg->input_count) {
955 vpif_dbg(1, debug, "Invalid input index\n");
956 return -EINVAL;
957 }
958
959 memcpy(input, &chan_cfg->inputs[input->index].input,
960 sizeof(*input));
961 return 0;
962}
963
964/**
965 * vpif_g_input() - Get INPUT handler
966 * @file: file ptr
967 * @priv: file handle
968 * @index: ptr to input index
969 */
970static int vpif_g_input(struct file *file, void *priv, unsigned int *index)
971{
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300972 struct video_device *vdev = video_devdata(file);
973 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300974
Hans Verkuil6f47c6c2012-09-20 09:06:22 -0300975 *index = ch->input_idx;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300976 return 0;
977}
978
979/**
980 * vpif_s_input() - Set INPUT handler
981 * @file: file ptr
982 * @priv: file handle
983 * @index: input index
984 */
985static int vpif_s_input(struct file *file, void *priv, unsigned int index)
986{
987 struct vpif_capture_config *config = vpif_dev->platform_data;
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300988 struct video_device *vdev = video_devdata(file);
989 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300990 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300991 struct vpif_capture_chan_config *chan_cfg;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300992
993 chan_cfg = &config->chan_config[ch->channel_id];
994
Hans Verkuil7aaad132012-09-20 09:06:25 -0300995 if (index >= chan_cfg->input_count)
996 return -EINVAL;
997
Lad, Prabhakarc66238f2014-05-16 10:33:45 -0300998 if (vb2_is_busy(&common->buffer_queue))
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300999 return -EBUSY;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001000
Hans Verkuil178cce12012-09-20 09:06:30 -03001001 return vpif_set_input(config, ch, index);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001002}
1003
1004/**
1005 * vpif_enum_fmt_vid_cap() - ENUM_FMT handler
1006 * @file: file ptr
1007 * @priv: file handle
1008 * @index: input index
1009 */
1010static int vpif_enum_fmt_vid_cap(struct file *file, void *priv,
1011 struct v4l2_fmtdesc *fmt)
1012{
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001013 struct video_device *vdev = video_devdata(file);
1014 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001015
1016 if (fmt->index != 0) {
1017 vpif_dbg(1, debug, "Invalid format index\n");
1018 return -EINVAL;
1019 }
1020
1021 /* Fill in the information about format */
1022 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER) {
1023 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1024 strcpy(fmt->description, "Raw Mode -Bayer Pattern GrRBGb");
1025 fmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
1026 } else {
1027 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1028 strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
1029 fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
1030 }
1031 return 0;
1032}
1033
1034/**
1035 * vpif_try_fmt_vid_cap() - TRY_FMT handler
1036 * @file: file ptr
1037 * @priv: file handle
1038 * @fmt: ptr to v4l2 format structure
1039 */
1040static int vpif_try_fmt_vid_cap(struct file *file, void *priv,
1041 struct v4l2_format *fmt)
1042{
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001043 struct video_device *vdev = video_devdata(file);
1044 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001045 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
1046
1047 return vpif_check_format(ch, pixfmt, 1);
1048}
1049
1050
1051/**
1052 * vpif_g_fmt_vid_cap() - Set INPUT handler
1053 * @file: file ptr
1054 * @priv: file handle
1055 * @fmt: ptr to v4l2 format structure
1056 */
1057static int vpif_g_fmt_vid_cap(struct file *file, void *priv,
1058 struct v4l2_format *fmt)
1059{
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001060 struct video_device *vdev = video_devdata(file);
1061 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001062 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1063
1064 /* Check the validity of the buffer type */
1065 if (common->fmt.type != fmt->type)
1066 return -EINVAL;
1067
1068 /* Fill in the information about format */
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001069 *fmt = common->fmt;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001070 return 0;
1071}
1072
1073/**
1074 * vpif_s_fmt_vid_cap() - Set FMT handler
1075 * @file: file ptr
1076 * @priv: file handle
1077 * @fmt: ptr to v4l2 format structure
1078 */
1079static int vpif_s_fmt_vid_cap(struct file *file, void *priv,
1080 struct v4l2_format *fmt)
1081{
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001082 struct video_device *vdev = video_devdata(file);
1083 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001084 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1085 struct v4l2_pix_format *pixfmt;
1086 int ret = 0;
1087
Mats Randgaard2c0ddd12010-12-16 12:17:45 -03001088 vpif_dbg(2, debug, "%s\n", __func__);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001089
Lad, Prabhakarc66238f2014-05-16 10:33:45 -03001090 if (vb2_is_busy(&common->buffer_queue))
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001091 return -EBUSY;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001092
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001093 pixfmt = &fmt->fmt.pix;
1094 /* Check for valid field format */
1095 ret = vpif_check_format(ch, pixfmt, 0);
1096
1097 if (ret)
1098 return ret;
1099 /* store the format in the channel object */
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001100 common->fmt = *fmt;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001101 return 0;
1102}
1103
1104/**
1105 * vpif_querycap() - QUERYCAP handler
1106 * @file: file ptr
1107 * @priv: file handle
1108 * @cap: ptr to v4l2_capability structure
1109 */
1110static int vpif_querycap(struct file *file, void *priv,
1111 struct v4l2_capability *cap)
1112{
1113 struct vpif_capture_config *config = vpif_dev->platform_data;
1114
Lad, Prabhakar626d533f2012-09-25 11:21:55 -03001115 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1116 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
Lad, Prabhakare75ea0c2014-05-16 10:33:46 -03001117 strlcpy(cap->driver, VPIF_DRIVER_NAME, sizeof(cap->driver));
Lad, Prabhakar626d533f2012-09-25 11:21:55 -03001118 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
1119 dev_name(vpif_dev));
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001120 strlcpy(cap->card, config->card_name, sizeof(cap->card));
1121
1122 return 0;
1123}
1124
1125/**
Hans Verkuil0598c172012-09-18 07:18:47 -03001126 * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001127 * @file: file ptr
1128 * @priv: file handle
Hans Verkuil0598c172012-09-18 07:18:47 -03001129 * @timings: input timings
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001130 */
Hans Verkuil0598c172012-09-18 07:18:47 -03001131static int
1132vpif_enum_dv_timings(struct file *file, void *priv,
1133 struct v4l2_enum_dv_timings *timings)
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001134{
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001135 struct vpif_capture_config *config = vpif_dev->platform_data;
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001136 struct video_device *vdev = video_devdata(file);
1137 struct channel_obj *ch = video_get_drvdata(vdev);
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001138 struct vpif_capture_chan_config *chan_cfg;
1139 struct v4l2_input input;
Hans Verkuil178cce12012-09-20 09:06:30 -03001140 int ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001141
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001142 if (config->chan_config[ch->channel_id].inputs == NULL)
1143 return -ENODATA;
1144
1145 chan_cfg = &config->chan_config[ch->channel_id];
1146 input = chan_cfg->inputs[ch->input_idx].input;
1147 if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
1148 return -ENODATA;
1149
Laurent Pinchart10d21462014-01-31 09:04:19 -03001150 timings->pad = 0;
1151
1152 ret = v4l2_subdev_call(ch->sd, pad, enum_dv_timings, timings);
Wei Yongjune070f1b2012-10-30 09:45:00 -03001153 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
Hans Verkuil178cce12012-09-20 09:06:30 -03001154 return -EINVAL;
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001155
Hans Verkuil178cce12012-09-20 09:06:30 -03001156 return ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001157}
1158
1159/**
Hans Verkuil0598c172012-09-18 07:18:47 -03001160 * vpif_query_dv_timings() - QUERY_DV_TIMINGS handler
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001161 * @file: file ptr
1162 * @priv: file handle
Hans Verkuil0598c172012-09-18 07:18:47 -03001163 * @timings: input timings
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001164 */
Hans Verkuil0598c172012-09-18 07:18:47 -03001165static int
1166vpif_query_dv_timings(struct file *file, void *priv,
1167 struct v4l2_dv_timings *timings)
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001168{
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001169 struct vpif_capture_config *config = vpif_dev->platform_data;
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001170 struct video_device *vdev = video_devdata(file);
1171 struct channel_obj *ch = video_get_drvdata(vdev);
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001172 struct vpif_capture_chan_config *chan_cfg;
1173 struct v4l2_input input;
Hans Verkuil178cce12012-09-20 09:06:30 -03001174 int ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001175
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001176 if (config->chan_config[ch->channel_id].inputs == NULL)
1177 return -ENODATA;
1178
1179 chan_cfg = &config->chan_config[ch->channel_id];
1180 input = chan_cfg->inputs[ch->input_idx].input;
1181 if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
1182 return -ENODATA;
1183
Hans Verkuil178cce12012-09-20 09:06:30 -03001184 ret = v4l2_subdev_call(ch->sd, video, query_dv_timings, timings);
Wei Yongjune070f1b2012-10-30 09:45:00 -03001185 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
Hans Verkuil178cce12012-09-20 09:06:30 -03001186 return -ENODATA;
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001187
Hans Verkuil178cce12012-09-20 09:06:30 -03001188 return ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001189}
1190
Mats Randgaardc027e162010-12-16 12:17:44 -03001191/**
1192 * vpif_s_dv_timings() - S_DV_TIMINGS handler
1193 * @file: file ptr
1194 * @priv: file handle
1195 * @timings: digital video timings
1196 */
1197static int vpif_s_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 vpif_params *vpifparams = &ch->vpifparams;
1204 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001205 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
Mats Randgaardc027e162010-12-16 12:17:44 -03001206 struct video_obj *vid_ch = &ch->video;
Hans Verkuil0598c172012-09-18 07:18:47 -03001207 struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001208 struct vpif_capture_chan_config *chan_cfg;
1209 struct v4l2_input input;
Mats Randgaardc027e162010-12-16 12:17:44 -03001210 int ret;
1211
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001212 if (config->chan_config[ch->channel_id].inputs == NULL)
1213 return -ENODATA;
1214
1215 chan_cfg = &config->chan_config[ch->channel_id];
1216 input = chan_cfg->inputs[ch->input_idx].input;
1217 if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
1218 return -ENODATA;
1219
Mats Randgaardc027e162010-12-16 12:17:44 -03001220 if (timings->type != V4L2_DV_BT_656_1120) {
1221 vpif_dbg(2, debug, "Timing type not defined\n");
1222 return -EINVAL;
1223 }
1224
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001225 if (vb2_is_busy(&common->buffer_queue))
1226 return -EBUSY;
1227
Mats Randgaardc027e162010-12-16 12:17:44 -03001228 /* Configure subdevice timings, if any */
Hans Verkuil178cce12012-09-20 09:06:30 -03001229 ret = v4l2_subdev_call(ch->sd, video, s_dv_timings, timings);
1230 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1231 ret = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001232 if (ret < 0) {
1233 vpif_dbg(2, debug, "Error setting custom DV timings\n");
1234 return ret;
1235 }
1236
1237 if (!(timings->bt.width && timings->bt.height &&
1238 (timings->bt.hbackporch ||
1239 timings->bt.hfrontporch ||
1240 timings->bt.hsync) &&
1241 timings->bt.vfrontporch &&
1242 (timings->bt.vbackporch ||
1243 timings->bt.vsync))) {
1244 vpif_dbg(2, debug, "Timings for width, height, "
1245 "horizontal back porch, horizontal sync, "
1246 "horizontal front porch, vertical back porch, "
1247 "vertical sync and vertical back porch "
1248 "must be defined\n");
1249 return -EINVAL;
1250 }
1251
Hans Verkuil0598c172012-09-18 07:18:47 -03001252 vid_ch->dv_timings = *timings;
Mats Randgaardc027e162010-12-16 12:17:44 -03001253
1254 /* Configure video port timings */
1255
Hans Verkuile3655262013-07-29 08:41:00 -03001256 std_info->eav2sav = V4L2_DV_BT_BLANKING_WIDTH(bt) - 8;
Mats Randgaardc027e162010-12-16 12:17:44 -03001257 std_info->sav2eav = bt->width;
1258
1259 std_info->l1 = 1;
1260 std_info->l3 = bt->vsync + bt->vbackporch + 1;
1261
Hans Verkuile3655262013-07-29 08:41:00 -03001262 std_info->vsize = V4L2_DV_BT_FRAME_HEIGHT(bt);
Mats Randgaardc027e162010-12-16 12:17:44 -03001263 if (bt->interlaced) {
1264 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
Mats Randgaardc027e162010-12-16 12:17:44 -03001265 std_info->l5 = std_info->vsize/2 -
1266 (bt->vfrontporch - 1);
1267 std_info->l7 = std_info->vsize/2 + 1;
1268 std_info->l9 = std_info->l7 + bt->il_vsync +
1269 bt->il_vbackporch + 1;
1270 std_info->l11 = std_info->vsize -
1271 (bt->il_vfrontporch - 1);
1272 } else {
1273 vpif_dbg(2, debug, "Required timing values for "
1274 "interlaced BT format missing\n");
1275 return -EINVAL;
1276 }
1277 } else {
Mats Randgaardc027e162010-12-16 12:17:44 -03001278 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
1279 }
1280 strncpy(std_info->name, "Custom timings BT656/1120", VPIF_MAX_NAME);
1281 std_info->width = bt->width;
1282 std_info->height = bt->height;
1283 std_info->frm_fmt = bt->interlaced ? 0 : 1;
1284 std_info->ycmux_mode = 0;
1285 std_info->capture_format = 0;
1286 std_info->vbi_supported = 0;
1287 std_info->hd_sd = 1;
1288 std_info->stdid = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001289
1290 vid_ch->stdid = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001291 return 0;
1292}
1293
1294/**
1295 * vpif_g_dv_timings() - G_DV_TIMINGS handler
1296 * @file: file ptr
1297 * @priv: file handle
1298 * @timings: digital video timings
1299 */
1300static int vpif_g_dv_timings(struct file *file, void *priv,
1301 struct v4l2_dv_timings *timings)
1302{
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001303 struct vpif_capture_config *config = vpif_dev->platform_data;
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001304 struct video_device *vdev = video_devdata(file);
1305 struct channel_obj *ch = video_get_drvdata(vdev);
Mats Randgaardc027e162010-12-16 12:17:44 -03001306 struct video_obj *vid_ch = &ch->video;
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001307 struct vpif_capture_chan_config *chan_cfg;
1308 struct v4l2_input input;
1309
1310 if (config->chan_config[ch->channel_id].inputs == NULL)
1311 return -ENODATA;
1312
1313 chan_cfg = &config->chan_config[ch->channel_id];
1314 input = chan_cfg->inputs[ch->input_idx].input;
1315 if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
1316 return -ENODATA;
Mats Randgaardc027e162010-12-16 12:17:44 -03001317
Hans Verkuil0598c172012-09-18 07:18:47 -03001318 *timings = vid_ch->dv_timings;
Mats Randgaardc027e162010-12-16 12:17:44 -03001319
1320 return 0;
1321}
1322
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001323/*
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001324 * vpif_log_status() - Status information
1325 * @file: file ptr
1326 * @priv: file handle
1327 *
1328 * Returns zero.
1329 */
1330static int vpif_log_status(struct file *filep, void *priv)
1331{
1332 /* status for sub devices */
1333 v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1334
1335 return 0;
1336}
1337
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001338/* vpif capture ioctl operations */
1339static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
Lad, Prabhakar7b4657f2014-05-16 10:33:49 -03001340 .vidioc_querycap = vpif_querycap,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001341 .vidioc_enum_fmt_vid_cap = vpif_enum_fmt_vid_cap,
Lad, Prabhakar7b4657f2014-05-16 10:33:49 -03001342 .vidioc_g_fmt_vid_cap = vpif_g_fmt_vid_cap,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001343 .vidioc_s_fmt_vid_cap = vpif_s_fmt_vid_cap,
1344 .vidioc_try_fmt_vid_cap = vpif_try_fmt_vid_cap,
Lad, Prabhakar7b4657f2014-05-16 10:33:49 -03001345
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001346 .vidioc_enum_input = vpif_enum_input,
1347 .vidioc_s_input = vpif_s_input,
1348 .vidioc_g_input = vpif_g_input,
Lad, Prabhakard9a3b132014-05-16 10:33:42 -03001349
1350 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1351 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1352 .vidioc_querybuf = vb2_ioctl_querybuf,
1353 .vidioc_qbuf = vb2_ioctl_qbuf,
1354 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1355 .vidioc_expbuf = vb2_ioctl_expbuf,
1356 .vidioc_streamon = vb2_ioctl_streamon,
1357 .vidioc_streamoff = vb2_ioctl_streamoff,
1358
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001359 .vidioc_querystd = vpif_querystd,
Lad, Prabhakar7b4657f2014-05-16 10:33:49 -03001360 .vidioc_s_std = vpif_s_std,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001361 .vidioc_g_std = vpif_g_std,
Lad, Prabhakard9a3b132014-05-16 10:33:42 -03001362
Lad, Prabhakar7b4657f2014-05-16 10:33:49 -03001363 .vidioc_enum_dv_timings = vpif_enum_dv_timings,
1364 .vidioc_query_dv_timings = vpif_query_dv_timings,
1365 .vidioc_s_dv_timings = vpif_s_dv_timings,
1366 .vidioc_g_dv_timings = vpif_g_dv_timings,
1367
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001368 .vidioc_log_status = vpif_log_status,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001369};
1370
1371/* vpif file operations */
1372static struct v4l2_file_operations vpif_fops = {
1373 .owner = THIS_MODULE,
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001374 .open = v4l2_fh_open,
1375 .release = vb2_fop_release,
Hans Verkuil46656af2011-01-04 06:51:35 -03001376 .unlocked_ioctl = video_ioctl2,
Lad, Prabhakard26d26b2014-05-16 10:33:40 -03001377 .mmap = vb2_fop_mmap,
1378 .poll = vb2_fop_poll
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001379};
1380
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001381/**
1382 * initialize_vpif() - Initialize vpif data structures
1383 *
1384 * Allocate memory for data structures and initialize them
1385 */
1386static int initialize_vpif(void)
1387{
Lad, Prabhakar4015bef2014-05-16 10:33:47 -03001388 int err, i, j;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001389 int free_channel_objects_index;
1390
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001391 /* Allocate memory for six channel objects */
1392 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1393 vpif_obj.dev[i] =
1394 kzalloc(sizeof(*vpif_obj.dev[i]), GFP_KERNEL);
1395 /* If memory allocation fails, return error */
1396 if (!vpif_obj.dev[i]) {
1397 free_channel_objects_index = i;
1398 err = -ENOMEM;
1399 goto vpif_init_free_channel_objects;
1400 }
1401 }
1402 return 0;
1403
1404vpif_init_free_channel_objects:
1405 for (j = 0; j < free_channel_objects_index; j++)
1406 kfree(vpif_obj.dev[j]);
1407 return err;
1408}
1409
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001410static int vpif_async_bound(struct v4l2_async_notifier *notifier,
1411 struct v4l2_subdev *subdev,
1412 struct v4l2_async_subdev *asd)
1413{
1414 int i;
1415
1416 for (i = 0; i < vpif_obj.config->subdev_count; i++)
1417 if (!strcmp(vpif_obj.config->subdev_info[i].name,
1418 subdev->name)) {
1419 vpif_obj.sd[i] = subdev;
1420 return 0;
1421 }
1422
1423 return -EINVAL;
1424}
1425
1426static int vpif_probe_complete(void)
1427{
1428 struct common_obj *common;
Lad, Prabhakard26d26b2014-05-16 10:33:40 -03001429 struct video_device *vdev;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001430 struct channel_obj *ch;
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001431 struct vb2_queue *q;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001432 int i, j, err, k;
1433
1434 for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
1435 ch = vpif_obj.dev[j];
1436 ch->channel_id = j;
1437 common = &(ch->common[VPIF_VIDEO_INDEX]);
1438 spin_lock_init(&common->irqlock);
1439 mutex_init(&common->lock);
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001440
1441 /* select input 0 */
1442 err = vpif_set_input(vpif_obj.config, ch, 0);
1443 if (err)
1444 goto probe_out;
1445
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001446 /* Initialize vb2 queue */
1447 q = &common->buffer_queue;
1448 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1449 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1450 q->drv_priv = ch;
1451 q->ops = &video_qops;
1452 q->mem_ops = &vb2_dma_contig_memops;
1453 q->buf_struct_size = sizeof(struct vpif_cap_buffer);
1454 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1455 q->min_buffers_needed = 1;
Lad, Prabhakar999ba692014-05-16 10:33:33 -03001456 q->lock = &common->lock;
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001457
1458 err = vb2_queue_init(q);
1459 if (err) {
1460 vpif_err("vpif_capture: vb2_queue_init() failed\n");
1461 goto probe_out;
1462 }
1463
1464 common->alloc_ctx = vb2_dma_contig_init_ctx(vpif_dev);
1465 if (IS_ERR(common->alloc_ctx)) {
1466 vpif_err("Failed to get the context\n");
1467 err = PTR_ERR(common->alloc_ctx);
1468 goto probe_out;
1469 }
1470
1471 INIT_LIST_HEAD(&common->dma_queue);
1472
Lad, Prabhakare75ea0c2014-05-16 10:33:46 -03001473 /* Initialize the video_device structure */
Lad, Prabhakard26d26b2014-05-16 10:33:40 -03001474 vdev = ch->video_dev;
Lad, Prabhakare75ea0c2014-05-16 10:33:46 -03001475 strlcpy(vdev->name, VPIF_DRIVER_NAME, sizeof(vdev->name));
1476 vdev->release = video_device_release;
1477 vdev->fops = &vpif_fops;
1478 vdev->ioctl_ops = &vpif_ioctl_ops;
1479 vdev->v4l2_dev = &vpif_obj.v4l2_dev;
1480 vdev->vfl_dir = VFL_DIR_RX;
Lad, Prabhakard26d26b2014-05-16 10:33:40 -03001481 vdev->queue = q;
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001482 vdev->lock = &common->lock;
1483 set_bit(V4L2_FL_USE_FH_PRIO, &vdev->flags);
1484 video_set_drvdata(ch->video_dev, ch);
Lad, Prabhakard26d26b2014-05-16 10:33:40 -03001485 err = video_register_device(vdev,
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001486 VFL_TYPE_GRABBER, (j ? 1 : 0));
1487 if (err)
1488 goto probe_out;
1489 }
1490
1491 v4l2_info(&vpif_obj.v4l2_dev, "VPIF capture driver initialized\n");
1492 return 0;
1493
1494probe_out:
1495 for (k = 0; k < j; k++) {
1496 /* Get the pointer to the channel object */
1497 ch = vpif_obj.dev[k];
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001498 common = &ch->common[k];
1499 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001500 /* Unregister video device */
1501 video_unregister_device(ch->video_dev);
1502 }
1503 kfree(vpif_obj.sd);
1504 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1505 ch = vpif_obj.dev[i];
1506 /* Note: does nothing if ch->video_dev == NULL */
1507 video_device_release(ch->video_dev);
1508 }
1509 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1510
1511 return err;
1512}
1513
1514static int vpif_async_complete(struct v4l2_async_notifier *notifier)
1515{
1516 return vpif_probe_complete();
1517}
1518
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001519/**
1520 * vpif_probe : This function probes the vpif capture driver
1521 * @pdev: platform device pointer
1522 *
1523 * This creates device entries by register itself to the V4L2 driver and
1524 * initializes fields of each channel objects
1525 */
1526static __init int vpif_probe(struct platform_device *pdev)
1527{
1528 struct vpif_subdev_info *subdevdata;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001529 int i, j, err;
Hans Verkuil5be452c2012-09-20 09:06:29 -03001530 int res_idx = 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001531 struct i2c_adapter *i2c_adap;
1532 struct channel_obj *ch;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001533 struct video_device *vfd;
1534 struct resource *res;
1535 int subdev_count;
1536
1537 vpif_dev = &pdev->dev;
1538
1539 err = initialize_vpif();
1540 if (err) {
1541 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
1542 return err;
1543 }
1544
Hans Verkuild2f7a1a2011-12-13 05:44:42 -03001545 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
1546 if (err) {
1547 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
1548 return err;
1549 }
1550
Hans Verkuil5be452c2012-09-20 09:06:29 -03001551 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
Lad, Prabhakara76a0b32013-06-17 11:20:47 -03001552 err = devm_request_irq(&pdev->dev, res->start, vpif_channel_isr,
Lad, Prabhakare75ea0c2014-05-16 10:33:46 -03001553 IRQF_SHARED, VPIF_DRIVER_NAME,
Lad, Prabhakara76a0b32013-06-17 11:20:47 -03001554 (void *)(&vpif_obj.dev[res_idx]->
1555 channel_id));
1556 if (err) {
1557 err = -EINVAL;
1558 goto vpif_unregister;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001559 }
Hans Verkuil5be452c2012-09-20 09:06:29 -03001560 res_idx++;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001561 }
1562
1563 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1564 /* Get the pointer to the channel object */
1565 ch = vpif_obj.dev[i];
1566 /* Allocate memory for video device */
1567 vfd = video_device_alloc();
1568 if (NULL == vfd) {
1569 for (j = 0; j < i; j++) {
1570 ch = vpif_obj.dev[j];
1571 video_device_release(ch->video_dev);
1572 }
1573 err = -ENOMEM;
Lad, Prabhakarb2de4f22013-06-17 11:20:46 -03001574 goto vpif_unregister;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001575 }
1576
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001577 /* Set video_dev to the video device */
1578 ch->video_dev = vfd;
1579 }
1580
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001581 vpif_obj.config = pdev->dev.platform_data;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001582
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001583 subdev_count = vpif_obj.config->subdev_count;
Mats Randgaard1f8766b2010-08-30 10:30:37 -03001584 vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001585 GFP_KERNEL);
1586 if (vpif_obj.sd == NULL) {
1587 vpif_err("unable to allocate memory for subdevice pointers\n");
1588 err = -ENOMEM;
Hans Verkuil5be452c2012-09-20 09:06:29 -03001589 goto vpif_sd_error;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001590 }
1591
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001592 if (!vpif_obj.config->asd_sizes) {
1593 i2c_adap = i2c_get_adapter(1);
1594 for (i = 0; i < subdev_count; i++) {
1595 subdevdata = &vpif_obj.config->subdev_info[i];
1596 vpif_obj.sd[i] =
1597 v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
1598 i2c_adap,
1599 &subdevdata->
1600 board_info,
1601 NULL);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001602
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001603 if (!vpif_obj.sd[i]) {
1604 vpif_err("Error registering v4l2 subdevice\n");
Wei Yongjun2fcd9dc2013-09-02 05:06:10 -03001605 err = -ENODEV;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001606 goto probe_subdev_out;
1607 }
1608 v4l2_info(&vpif_obj.v4l2_dev,
1609 "registered sub device %s\n",
1610 subdevdata->name);
1611 }
1612 vpif_probe_complete();
1613 } else {
Sylwester Nawrockie8419d02013-07-19 12:31:10 -03001614 vpif_obj.notifier.subdevs = vpif_obj.config->asd;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001615 vpif_obj.notifier.num_subdevs = vpif_obj.config->asd_sizes[0];
1616 vpif_obj.notifier.bound = vpif_async_bound;
1617 vpif_obj.notifier.complete = vpif_async_complete;
1618 err = v4l2_async_notifier_register(&vpif_obj.v4l2_dev,
1619 &vpif_obj.notifier);
1620 if (err) {
1621 vpif_err("Error registering async notifier\n");
1622 err = -EINVAL;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001623 goto probe_subdev_out;
1624 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001625 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001626
1627 return 0;
1628
Hans Verkuilb65814e2012-09-20 09:06:26 -03001629probe_subdev_out:
1630 /* free sub devices memory */
1631 kfree(vpif_obj.sd);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001632
Hans Verkuil5be452c2012-09-20 09:06:29 -03001633vpif_sd_error:
1634 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1635 ch = vpif_obj.dev[i];
1636 /* Note: does nothing if ch->video_dev == NULL */
1637 video_device_release(ch->video_dev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001638 }
Lad, Prabhakarb2de4f22013-06-17 11:20:46 -03001639vpif_unregister:
Hans Verkuild2f7a1a2011-12-13 05:44:42 -03001640 v4l2_device_unregister(&vpif_obj.v4l2_dev);
Lad, Prabhakarb2de4f22013-06-17 11:20:46 -03001641
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001642 return err;
1643}
1644
1645/**
1646 * vpif_remove() - driver remove handler
1647 * @device: ptr to platform device structure
1648 *
1649 * The vidoe device is unregistered
1650 */
1651static int vpif_remove(struct platform_device *device)
1652{
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001653 struct common_obj *common;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001654 struct channel_obj *ch;
Lad, Prabhakarb2de4f22013-06-17 11:20:46 -03001655 int i;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001656
1657 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1658
Lad, Prabhakar410ca602013-06-17 11:20:44 -03001659 kfree(vpif_obj.sd);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001660 /* un-register device */
1661 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1662 /* Get the pointer to the channel object */
1663 ch = vpif_obj.dev[i];
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001664 common = &ch->common[i];
1665 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001666 /* Unregister video device */
1667 video_unregister_device(ch->video_dev);
Lad, Prabhakar410ca602013-06-17 11:20:44 -03001668 kfree(vpif_obj.dev[i]);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001669 }
1670 return 0;
1671}
1672
Lad, Prabhakarb7047712014-05-16 10:33:50 -03001673#ifdef CONFIG_PM_SLEEP
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001674/**
1675 * vpif_suspend: vpif device suspend
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001676 */
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001677static int vpif_suspend(struct device *dev)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001678{
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001679
1680 struct common_obj *common;
1681 struct channel_obj *ch;
1682 int i;
1683
1684 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1685 /* Get the pointer to the channel object */
1686 ch = vpif_obj.dev[i];
1687 common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakarb7047712014-05-16 10:33:50 -03001688
1689 if (!vb2_is_streaming(&common->buffer_queue))
1690 continue;
1691
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001692 mutex_lock(&common->lock);
Lad, Prabhakarb7047712014-05-16 10:33:50 -03001693 /* Disable channel */
1694 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
1695 enable_channel0(0);
1696 channel0_intr_enable(0);
1697 }
1698 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
1699 ycmux_mode == 2) {
1700 enable_channel1(0);
1701 channel1_intr_enable(0);
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001702 }
1703 mutex_unlock(&common->lock);
1704 }
1705
1706 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001707}
1708
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001709/*
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001710 * vpif_resume: vpif device suspend
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001711 */
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001712static int vpif_resume(struct device *dev)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001713{
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001714 struct common_obj *common;
1715 struct channel_obj *ch;
1716 int i;
1717
1718 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1719 /* Get the pointer to the channel object */
1720 ch = vpif_obj.dev[i];
1721 common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakarb7047712014-05-16 10:33:50 -03001722
1723 if (!vb2_is_streaming(&common->buffer_queue))
1724 continue;
1725
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001726 mutex_lock(&common->lock);
Lad, Prabhakarb7047712014-05-16 10:33:50 -03001727 /* Enable channel */
1728 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
1729 enable_channel0(1);
1730 channel0_intr_enable(1);
1731 }
1732 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
1733 ycmux_mode == 2) {
1734 enable_channel1(1);
1735 channel1_intr_enable(1);
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001736 }
1737 mutex_unlock(&common->lock);
1738 }
1739
1740 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001741}
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001742#endif
1743
Lad, Prabhakarb7047712014-05-16 10:33:50 -03001744static SIMPLE_DEV_PM_OPS(vpif_pm_ops, vpif_suspend, vpif_resume);
1745
Mats Randgaardffa1b392010-08-30 10:30:36 -03001746static __refdata struct platform_driver vpif_driver = {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001747 .driver = {
Lad, Prabhakare75ea0c2014-05-16 10:33:46 -03001748 .name = VPIF_DRIVER_NAME,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001749 .owner = THIS_MODULE,
Lad, Prabhakarb7047712014-05-16 10:33:50 -03001750 .pm = &vpif_pm_ops,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001751 },
1752 .probe = vpif_probe,
1753 .remove = vpif_remove,
1754};
1755
Lad, Prabhakarfe424b22013-06-17 11:20:45 -03001756module_platform_driver(vpif_driver);