blob: c77c17695607e27154c623c47b8d367f390e49e0 [file] [log] [blame]
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001/*
2 * Copyright (C) 2009 Texas Instruments Inc
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * TODO : add support for VBI & HBI data service
19 * add static buffer allocation
20 */
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030021
Lad, Prabhakar012eef72013-04-19 05:53:29 -030022#include <linux/module.h>
23#include <linux/interrupt.h>
24#include <linux/platform_device.h>
25#include <linux/slab.h>
26
Lad, Prabhakar012eef72013-04-19 05:53:29 -030027#include <media/v4l2-ioctl.h>
28
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030029#include "vpif.h"
Lad, Prabhakar012eef72013-04-19 05:53:29 -030030#include "vpif_capture.h"
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030031
32MODULE_DESCRIPTION("TI DaVinci VPIF Capture driver");
33MODULE_LICENSE("GPL");
Mauro Carvalho Chehab64dc3c12011-06-25 11:28:37 -030034MODULE_VERSION(VPIF_CAPTURE_VERSION);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030035
36#define vpif_err(fmt, arg...) v4l2_err(&vpif_obj.v4l2_dev, fmt, ## arg)
37#define vpif_dbg(level, debug, fmt, arg...) \
38 v4l2_dbg(level, debug, &vpif_obj.v4l2_dev, fmt, ## arg)
39
40static int debug = 1;
41static u32 ch0_numbuffers = 3;
42static u32 ch1_numbuffers = 3;
43static u32 ch0_bufsize = 1920 * 1080 * 2;
44static u32 ch1_bufsize = 720 * 576 * 2;
45
46module_param(debug, int, 0644);
47module_param(ch0_numbuffers, uint, S_IRUGO);
48module_param(ch1_numbuffers, uint, S_IRUGO);
49module_param(ch0_bufsize, uint, S_IRUGO);
50module_param(ch1_bufsize, uint, S_IRUGO);
51
52MODULE_PARM_DESC(debug, "Debug level 0-1");
53MODULE_PARM_DESC(ch2_numbuffers, "Channel0 buffer count (default:3)");
54MODULE_PARM_DESC(ch3_numbuffers, "Channel1 buffer count (default:3)");
55MODULE_PARM_DESC(ch2_bufsize, "Channel0 buffer size (default:1920 x 1080 x 2)");
56MODULE_PARM_DESC(ch3_bufsize, "Channel1 buffer size (default:720 x 576 x 2)");
57
58static struct vpif_config_params config_params = {
59 .min_numbuffers = 3,
60 .numbuffers[0] = 3,
61 .numbuffers[1] = 3,
62 .min_bufsize[0] = 720 * 480 * 2,
63 .min_bufsize[1] = 720 * 480 * 2,
64 .channel_bufsize[0] = 1920 * 1080 * 2,
65 .channel_bufsize[1] = 720 * 576 * 2,
66};
67
68/* global variables */
69static struct vpif_device vpif_obj = { {NULL} };
70static struct device *vpif_dev;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030071static void vpif_calculate_offsets(struct channel_obj *ch);
72static void vpif_config_addr(struct channel_obj *ch, int muxmode);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030073
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -030074static u8 channel_first_int[VPIF_NUMBER_OF_OBJECTS][2] = { {1, 1} };
75
76static inline struct vpif_cap_buffer *to_vpif_buffer(struct vb2_buffer *vb)
77{
78 return container_of(vb, struct vpif_cap_buffer, vb);
79}
80
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030081/**
Lad, Prabhakar23e76a22014-05-16 10:33:37 -030082 * vpif_buffer_prepare : callback function for buffer prepare
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030083 * @vb: ptr to vb2_buffer
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030084 *
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030085 * This is the callback function for buffer prepare when vb2_qbuf()
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030086 * function is called. The buffer is prepared and user space virtual address
87 * or user address is converted into physical address
88 */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030089static int vpif_buffer_prepare(struct vb2_buffer *vb)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030090{
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030091 struct vb2_queue *q = vb->vb2_queue;
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -030092 struct channel_obj *ch = vb2_get_drv_priv(q);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030093 struct common_obj *common;
94 unsigned long addr;
95
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030096 vpif_dbg(2, debug, "vpif_buffer_prepare\n");
97
98 common = &ch->common[VPIF_VIDEO_INDEX];
99
Lad, Prabhakar23e76a22014-05-16 10:33:37 -0300100 vb2_set_plane_payload(vb, 0, common->fmt.fmt.pix.sizeimage);
101 if (vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
102 return -EINVAL;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300103
Lad, Prabhakar23e76a22014-05-16 10:33:37 -0300104 vb->v4l2_buf.field = common->fmt.fmt.pix.field;
105
106 addr = vb2_dma_contig_plane_dma_addr(vb, 0);
107 if (!IS_ALIGNED((addr + common->ytop_off), 8) ||
108 !IS_ALIGNED((addr + common->ybtm_off), 8) ||
109 !IS_ALIGNED((addr + common->ctop_off), 8) ||
110 !IS_ALIGNED((addr + common->cbtm_off), 8)) {
111 vpif_dbg(1, debug, "offset is not aligned\n");
112 return -EINVAL;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300113 }
Lad, Prabhakar23e76a22014-05-16 10:33:37 -0300114
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300115 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300116}
117
118/**
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300119 * vpif_buffer_queue_setup : Callback function for buffer setup.
120 * @vq: vb2_queue ptr
121 * @fmt: v4l2 format
122 * @nbuffers: ptr to number of buffers requested by application
123 * @nplanes:: contains number of distinct video planes needed to hold a frame
124 * @sizes[]: contains the size (in bytes) of each plane.
125 * @alloc_ctxs: ptr to allocation context
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300126 *
127 * This callback function is called when reqbuf() is called to adjust
128 * the buffer count and buffer size
129 */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300130static int vpif_buffer_queue_setup(struct vb2_queue *vq,
131 const struct v4l2_format *fmt,
132 unsigned int *nbuffers, unsigned int *nplanes,
133 unsigned int sizes[], void *alloc_ctxs[])
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300134{
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -0300135 struct channel_obj *ch = vb2_get_drv_priv(vq);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300136 struct common_obj *common;
137
138 common = &ch->common[VPIF_VIDEO_INDEX];
139
140 vpif_dbg(2, debug, "vpif_buffer_setup\n");
141
Lad, Prabhakar837939d2014-05-16 10:33:38 -0300142 if (fmt && fmt->fmt.pix.sizeimage < common->fmt.fmt.pix.sizeimage)
143 return -EINVAL;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300144
Lad, Prabhakar837939d2014-05-16 10:33:38 -0300145 if (vq->num_buffers + *nbuffers < 3)
146 *nbuffers = 3 - vq->num_buffers;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300147
148 *nplanes = 1;
Lad, Prabhakar837939d2014-05-16 10:33:38 -0300149 sizes[0] = fmt ? fmt->fmt.pix.sizeimage : common->fmt.fmt.pix.sizeimage;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300150 alloc_ctxs[0] = common->alloc_ctx;
151
Lad, Prabhakar837939d2014-05-16 10:33:38 -0300152 /* Calculate the offset for Y and C data in the buffer */
153 vpif_calculate_offsets(ch);
154
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300155 return 0;
156}
157
158/**
159 * vpif_buffer_queue : Callback function to add buffer to DMA queue
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300160 * @vb: ptr to vb2_buffer
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300161 */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300162static void vpif_buffer_queue(struct vb2_buffer *vb)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300163{
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -0300164 struct channel_obj *ch = vb2_get_drv_priv(vb->vb2_queue);
165 struct vpif_cap_buffer *buf = to_vpif_buffer(vb);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300166 struct common_obj *common;
Hans Verkuilaec96832012-11-16 12:03:06 -0300167 unsigned long flags;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300168
169 common = &ch->common[VPIF_VIDEO_INDEX];
170
171 vpif_dbg(2, debug, "vpif_buffer_queue\n");
172
Hans Verkuilaec96832012-11-16 12:03:06 -0300173 spin_lock_irqsave(&common->irqlock, flags);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300174 /* add the buffer to the DMA queue */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300175 list_add_tail(&buf->list, &common->dma_queue);
Hans Verkuilaec96832012-11-16 12:03:06 -0300176 spin_unlock_irqrestore(&common->irqlock, flags);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300177}
178
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300179static int vpif_start_streaming(struct vb2_queue *vq, unsigned int count)
180{
181 struct vpif_capture_config *vpif_config_data =
182 vpif_dev->platform_data;
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -0300183 struct channel_obj *ch = vb2_get_drv_priv(vq);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300184 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
185 struct vpif_params *vpif = &ch->vpifparams;
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300186 struct vpif_cap_buffer *buf, *tmp;
187 unsigned long addr, flags;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300188 int ret;
189
Hans Verkuilaec96832012-11-16 12:03:06 -0300190 spin_lock_irqsave(&common->irqlock, flags);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300191
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300192 /* Initialize field_id and started member */
193 ch->field_id = 0;
194 common->started = 1;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300195
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300196 if ((vpif->std_info.frm_fmt &&
197 ((common->fmt.fmt.pix.field != V4L2_FIELD_NONE) &&
198 (common->fmt.fmt.pix.field != V4L2_FIELD_ANY))) ||
199 (!vpif->std_info.frm_fmt &&
200 (common->fmt.fmt.pix.field == V4L2_FIELD_NONE))) {
201 vpif_dbg(1, debug, "conflict in field format and std format\n");
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300202 ret = -EINVAL;
203 goto err;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300204 }
205
206 /* configure 1 or 2 channel mode */
Lad, Prabhakarf4ad8d72012-09-27 02:33:12 -0300207 if (vpif_config_data->setup_input_channel_mode) {
208 ret = vpif_config_data->
209 setup_input_channel_mode(vpif->std_info.ycmux_mode);
210 if (ret < 0) {
211 vpif_dbg(1, debug, "can't set vpif channel mode\n");
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300212 goto err;
Lad, Prabhakarf4ad8d72012-09-27 02:33:12 -0300213 }
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300214 }
215
216 /* Call vpif_set_params function to set the parameters and addresses */
217 ret = vpif_set_video_params(vpif, ch->channel_id);
218
219 if (ret < 0) {
220 vpif_dbg(1, debug, "can't set video params\n");
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300221 goto err;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300222 }
223
224 common->started = ret;
225 vpif_config_addr(ch, ret);
226
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300227 /* Get the next frame from the buffer queue */
228 common->cur_frm = common->next_frm = list_entry(common->dma_queue.next,
229 struct vpif_cap_buffer, list);
230 /* Remove buffer from the buffer queue */
231 list_del(&common->cur_frm->list);
232 spin_unlock_irqrestore(&common->irqlock, flags);
233 /* Mark state of the current frame to active */
234 common->cur_frm->vb.state = VB2_BUF_STATE_ACTIVE;
235
236 addr = vb2_dma_contig_plane_dma_addr(&common->cur_frm->vb, 0);
237
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300238 common->set_addr(addr + common->ytop_off,
239 addr + common->ybtm_off,
240 addr + common->ctop_off,
241 addr + common->cbtm_off);
242
243 /**
244 * Set interrupt for both the fields in VPIF Register enable channel in
245 * VPIF register
246 */
Lad, Prabhakar9e184042012-09-14 10:22:24 -0300247 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300248 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id)) {
249 channel0_intr_assert();
250 channel0_intr_enable(1);
251 enable_channel0(1);
252 }
253 if ((VPIF_CHANNEL1_VIDEO == ch->channel_id) ||
254 (common->started == 2)) {
255 channel1_intr_assert();
256 channel1_intr_enable(1);
257 enable_channel1(1);
258 }
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300259
260 return 0;
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300261
262err:
263 list_for_each_entry_safe(buf, tmp, &common->dma_queue, list) {
264 list_del(&buf->list);
265 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_QUEUED);
266 }
267
268 return ret;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300269}
270
271/* abort streaming and wait for last buffer */
Hans Verkuile37559b2014-04-17 02:47:21 -0300272static void vpif_stop_streaming(struct vb2_queue *vq)
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300273{
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -0300274 struct channel_obj *ch = vb2_get_drv_priv(vq);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300275 struct common_obj *common;
Hans Verkuilaec96832012-11-16 12:03:06 -0300276 unsigned long flags;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300277
278 if (!vb2_is_streaming(vq))
Hans Verkuile37559b2014-04-17 02:47:21 -0300279 return;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300280
281 common = &ch->common[VPIF_VIDEO_INDEX];
282
Lad, Prabhakare6ba3db2014-03-22 08:03:07 -0300283 /* Disable channel as per its device type and channel id */
284 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
285 enable_channel0(0);
286 channel0_intr_enable(0);
287 }
288 if ((VPIF_CHANNEL1_VIDEO == ch->channel_id) ||
289 (2 == common->started)) {
290 enable_channel1(0);
291 channel1_intr_enable(0);
292 }
293 common->started = 0;
294
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300295 /* release all active buffers */
Hans Verkuilaec96832012-11-16 12:03:06 -0300296 spin_lock_irqsave(&common->irqlock, flags);
Lad, Prabhakare6ba3db2014-03-22 08:03:07 -0300297 if (common->cur_frm == common->next_frm) {
298 vb2_buffer_done(&common->cur_frm->vb, VB2_BUF_STATE_ERROR);
299 } else {
300 if (common->cur_frm != NULL)
301 vb2_buffer_done(&common->cur_frm->vb,
302 VB2_BUF_STATE_ERROR);
303 if (common->next_frm != NULL)
304 vb2_buffer_done(&common->next_frm->vb,
305 VB2_BUF_STATE_ERROR);
306 }
307
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300308 while (!list_empty(&common->dma_queue)) {
309 common->next_frm = list_entry(common->dma_queue.next,
310 struct vpif_cap_buffer, list);
311 list_del(&common->next_frm->list);
312 vb2_buffer_done(&common->next_frm->vb, VB2_BUF_STATE_ERROR);
313 }
Hans Verkuilaec96832012-11-16 12:03:06 -0300314 spin_unlock_irqrestore(&common->irqlock, flags);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300315}
316
317static struct vb2_ops video_qops = {
318 .queue_setup = vpif_buffer_queue_setup,
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300319 .buf_prepare = vpif_buffer_prepare,
320 .start_streaming = vpif_start_streaming,
321 .stop_streaming = vpif_stop_streaming,
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300322 .buf_queue = vpif_buffer_queue,
323};
324
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300325/**
326 * vpif_process_buffer_complete: process a completed buffer
327 * @common: ptr to common channel object
328 *
329 * This function time stamp the buffer and mark it as DONE. It also
330 * wake up any process waiting on the QUEUE and set the next buffer
331 * as current
332 */
333static void vpif_process_buffer_complete(struct common_obj *common)
334{
Sakari Ailus8e6057b2012-09-15 15:14:42 -0300335 v4l2_get_timestamp(&common->cur_frm->vb.v4l2_buf.timestamp);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300336 vb2_buffer_done(&common->cur_frm->vb,
337 VB2_BUF_STATE_DONE);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300338 /* Make curFrm pointing to nextFrm */
339 common->cur_frm = common->next_frm;
340}
341
342/**
343 * vpif_schedule_next_buffer: set next buffer address for capture
344 * @common : ptr to common channel object
345 *
346 * This function will get next buffer from the dma queue and
347 * set the buffer address in the vpif register for capture.
348 * the buffer is marked active
349 */
350static void vpif_schedule_next_buffer(struct common_obj *common)
351{
352 unsigned long addr = 0;
353
Hans Verkuilaec96832012-11-16 12:03:06 -0300354 spin_lock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300355 common->next_frm = list_entry(common->dma_queue.next,
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300356 struct vpif_cap_buffer, list);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300357 /* Remove that buffer from the buffer queue */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300358 list_del(&common->next_frm->list);
Hans Verkuilaec96832012-11-16 12:03:06 -0300359 spin_unlock(&common->irqlock);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300360 common->next_frm->vb.state = VB2_BUF_STATE_ACTIVE;
361 addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb, 0);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300362
363 /* Set top and bottom field addresses in VPIF registers */
364 common->set_addr(addr + common->ytop_off,
365 addr + common->ybtm_off,
366 addr + common->ctop_off,
367 addr + common->cbtm_off);
368}
369
370/**
371 * vpif_channel_isr : ISR handler for vpif capture
372 * @irq: irq number
373 * @dev_id: dev_id ptr
374 *
375 * It changes status of the captured buffer, takes next buffer from the queue
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300376 * and sets its address in VPIF registers
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300377 */
378static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
379{
380 struct vpif_device *dev = &vpif_obj;
381 struct common_obj *common;
382 struct channel_obj *ch;
383 enum v4l2_field field;
384 int channel_id = 0;
385 int fid = -1, i;
386
387 channel_id = *(int *)(dev_id);
Manjunath Hadlib1fc4232012-04-13 04:43:10 -0300388 if (!vpif_intr_status(channel_id))
389 return IRQ_NONE;
390
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300391 ch = dev->dev[channel_id];
392
393 field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
394
395 for (i = 0; i < VPIF_NUMBER_OF_OBJECTS; i++) {
396 common = &ch->common[i];
397 /* skip If streaming is not started in this channel */
398 if (0 == common->started)
399 continue;
400
401 /* Check the field format */
402 if (1 == ch->vpifparams.std_info.frm_fmt) {
403 /* Progressive mode */
Hans Verkuilaec96832012-11-16 12:03:06 -0300404 spin_lock(&common->irqlock);
405 if (list_empty(&common->dma_queue)) {
406 spin_unlock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300407 continue;
Hans Verkuilaec96832012-11-16 12:03:06 -0300408 }
409 spin_unlock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300410
411 if (!channel_first_int[i][channel_id])
412 vpif_process_buffer_complete(common);
413
414 channel_first_int[i][channel_id] = 0;
415
416 vpif_schedule_next_buffer(common);
417
418
419 channel_first_int[i][channel_id] = 0;
420 } else {
421 /**
422 * Interlaced mode. If it is first interrupt, ignore
423 * it
424 */
425 if (channel_first_int[i][channel_id]) {
426 channel_first_int[i][channel_id] = 0;
427 continue;
428 }
429 if (0 == i) {
430 ch->field_id ^= 1;
431 /* Get field id from VPIF registers */
432 fid = vpif_channel_getfid(ch->channel_id);
433 if (fid != ch->field_id) {
434 /**
435 * If field id does not match stored
436 * field id, make them in sync
437 */
438 if (0 == fid)
439 ch->field_id = fid;
440 return IRQ_HANDLED;
441 }
442 }
443 /* device field id and local field id are in sync */
444 if (0 == fid) {
445 /* this is even field */
446 if (common->cur_frm == common->next_frm)
447 continue;
448
449 /* mark the current buffer as done */
450 vpif_process_buffer_complete(common);
451 } else if (1 == fid) {
452 /* odd field */
Hans Verkuilaec96832012-11-16 12:03:06 -0300453 spin_lock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300454 if (list_empty(&common->dma_queue) ||
Hans Verkuilaec96832012-11-16 12:03:06 -0300455 (common->cur_frm != common->next_frm)) {
456 spin_unlock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300457 continue;
Hans Verkuilaec96832012-11-16 12:03:06 -0300458 }
459 spin_unlock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300460
461 vpif_schedule_next_buffer(common);
462 }
463 }
464 }
465 return IRQ_HANDLED;
466}
467
468/**
469 * vpif_update_std_info() - update standard related info
470 * @ch: ptr to channel object
471 *
472 * For a given standard selected by application, update values
473 * in the device data structures
474 */
475static int vpif_update_std_info(struct channel_obj *ch)
476{
477 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
478 struct vpif_params *vpifparams = &ch->vpifparams;
479 const struct vpif_channel_config_params *config;
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300480 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300481 struct video_obj *vid_ch = &ch->video;
482 int index;
483
484 vpif_dbg(2, debug, "vpif_update_std_info\n");
485
Mats Randgaardaa444402010-12-16 12:17:42 -0300486 for (index = 0; index < vpif_ch_params_count; index++) {
Lad, Prabhakarced9b212013-03-20 01:28:27 -0300487 config = &vpif_ch_params[index];
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300488 if (config->hd_sd == 0) {
489 vpif_dbg(2, debug, "SD format\n");
490 if (config->stdid & vid_ch->stdid) {
491 memcpy(std_info, config, sizeof(*config));
492 break;
493 }
494 } else {
495 vpif_dbg(2, debug, "HD format\n");
Hans Verkuil0598c172012-09-18 07:18:47 -0300496 if (!memcmp(&config->dv_timings, &vid_ch->dv_timings,
497 sizeof(vid_ch->dv_timings))) {
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300498 memcpy(std_info, config, sizeof(*config));
499 break;
500 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300501 }
502 }
503
504 /* standard not found */
Mats Randgaardaa444402010-12-16 12:17:42 -0300505 if (index == vpif_ch_params_count)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300506 return -EINVAL;
507
508 common->fmt.fmt.pix.width = std_info->width;
509 common->width = std_info->width;
510 common->fmt.fmt.pix.height = std_info->height;
511 common->height = std_info->height;
512 common->fmt.fmt.pix.bytesperline = std_info->width;
513 vpifparams->video_params.hpitch = std_info->width;
514 vpifparams->video_params.storage_mode = std_info->frm_fmt;
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300515
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300516 return 0;
517}
518
519/**
520 * vpif_calculate_offsets : This function calculates buffers offsets
521 * @ch : ptr to channel object
522 *
523 * This function calculates buffer offsets for Y and C in the top and
524 * bottom field
525 */
526static void vpif_calculate_offsets(struct channel_obj *ch)
527{
528 unsigned int hpitch, vpitch, sizeimage;
529 struct video_obj *vid_ch = &(ch->video);
530 struct vpif_params *vpifparams = &ch->vpifparams;
531 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
532 enum v4l2_field field = common->fmt.fmt.pix.field;
533
534 vpif_dbg(2, debug, "vpif_calculate_offsets\n");
535
536 if (V4L2_FIELD_ANY == field) {
537 if (vpifparams->std_info.frm_fmt)
538 vid_ch->buf_field = V4L2_FIELD_NONE;
539 else
540 vid_ch->buf_field = V4L2_FIELD_INTERLACED;
541 } else
542 vid_ch->buf_field = common->fmt.fmt.pix.field;
543
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300544 sizeimage = common->fmt.fmt.pix.sizeimage;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300545
546 hpitch = common->fmt.fmt.pix.bytesperline;
547 vpitch = sizeimage / (hpitch * 2);
548
549 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
550 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
551 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
552 common->ytop_off = 0;
553 common->ybtm_off = hpitch;
554 common->ctop_off = sizeimage / 2;
555 common->cbtm_off = sizeimage / 2 + hpitch;
556 } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
557 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
558 common->ytop_off = 0;
559 common->ybtm_off = sizeimage / 4;
560 common->ctop_off = sizeimage / 2;
561 common->cbtm_off = common->ctop_off + sizeimage / 4;
562 } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
563 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
564 common->ybtm_off = 0;
565 common->ytop_off = sizeimage / 4;
566 common->cbtm_off = sizeimage / 2;
567 common->ctop_off = common->cbtm_off + sizeimage / 4;
568 }
569 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
570 (V4L2_FIELD_INTERLACED == vid_ch->buf_field))
571 vpifparams->video_params.storage_mode = 1;
572 else
573 vpifparams->video_params.storage_mode = 0;
574
575 if (1 == vpifparams->std_info.frm_fmt)
576 vpifparams->video_params.hpitch =
577 common->fmt.fmt.pix.bytesperline;
578 else {
579 if ((field == V4L2_FIELD_ANY)
580 || (field == V4L2_FIELD_INTERLACED))
581 vpifparams->video_params.hpitch =
582 common->fmt.fmt.pix.bytesperline * 2;
583 else
584 vpifparams->video_params.hpitch =
585 common->fmt.fmt.pix.bytesperline;
586 }
587
588 ch->vpifparams.video_params.stdid = vpifparams->std_info.stdid;
589}
590
591/**
592 * vpif_config_format: configure default frame format in the device
593 * ch : ptr to channel object
594 */
595static void vpif_config_format(struct channel_obj *ch)
596{
597 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
598
599 vpif_dbg(2, debug, "vpif_config_format\n");
600
601 common->fmt.fmt.pix.field = V4L2_FIELD_ANY;
602 if (config_params.numbuffers[ch->channel_id] == 0)
603 common->memory = V4L2_MEMORY_USERPTR;
604 else
605 common->memory = V4L2_MEMORY_MMAP;
606
607 common->fmt.fmt.pix.sizeimage
608 = config_params.channel_bufsize[ch->channel_id];
609
610 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER)
611 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;
612 else
613 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
614 common->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
615}
616
617/**
618 * vpif_get_default_field() - Get default field type based on interface
619 * @vpif_params - ptr to vpif params
620 */
621static inline enum v4l2_field vpif_get_default_field(
622 struct vpif_interface *iface)
623{
624 return (iface->if_type == VPIF_IF_RAW_BAYER) ? V4L2_FIELD_NONE :
625 V4L2_FIELD_INTERLACED;
626}
627
628/**
629 * vpif_check_format() - check given pixel format for compatibility
630 * @ch - channel ptr
631 * @pixfmt - Given pixel format
632 * @update - update the values as per hardware requirement
633 *
634 * Check the application pixel format for S_FMT and update the input
635 * values as per hardware limits for TRY_FMT. The default pixel and
636 * field format is selected based on interface type.
637 */
638static int vpif_check_format(struct channel_obj *ch,
639 struct v4l2_pix_format *pixfmt,
640 int update)
641{
642 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
643 struct vpif_params *vpif_params = &ch->vpifparams;
644 enum v4l2_field field = pixfmt->field;
645 u32 sizeimage, hpitch, vpitch;
646 int ret = -EINVAL;
647
648 vpif_dbg(2, debug, "vpif_check_format\n");
649 /**
650 * first check for the pixel format. If if_type is Raw bayer,
651 * only V4L2_PIX_FMT_SBGGR8 format is supported. Otherwise only
652 * V4L2_PIX_FMT_YUV422P is supported
653 */
654 if (vpif_params->iface.if_type == VPIF_IF_RAW_BAYER) {
655 if (pixfmt->pixelformat != V4L2_PIX_FMT_SBGGR8) {
656 if (!update) {
657 vpif_dbg(2, debug, "invalid pix format\n");
658 goto exit;
659 }
660 pixfmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
661 }
662 } else {
663 if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P) {
664 if (!update) {
665 vpif_dbg(2, debug, "invalid pixel format\n");
666 goto exit;
667 }
668 pixfmt->pixelformat = V4L2_PIX_FMT_YUV422P;
669 }
670 }
671
672 if (!(VPIF_VALID_FIELD(field))) {
673 if (!update) {
674 vpif_dbg(2, debug, "invalid field format\n");
675 goto exit;
676 }
677 /**
678 * By default use FIELD_NONE for RAW Bayer capture
679 * and FIELD_INTERLACED for other interfaces
680 */
681 field = vpif_get_default_field(&vpif_params->iface);
682 } else if (field == V4L2_FIELD_ANY)
683 /* unsupported field. Use default */
684 field = vpif_get_default_field(&vpif_params->iface);
685
686 /* validate the hpitch */
687 hpitch = pixfmt->bytesperline;
688 if (hpitch < vpif_params->std_info.width) {
689 if (!update) {
690 vpif_dbg(2, debug, "invalid hpitch\n");
691 goto exit;
692 }
693 hpitch = vpif_params->std_info.width;
694 }
695
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300696 sizeimage = pixfmt->sizeimage;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300697
698 vpitch = sizeimage / (hpitch * 2);
699
700 /* validate the vpitch */
701 if (vpitch < vpif_params->std_info.height) {
702 if (!update) {
703 vpif_dbg(2, debug, "Invalid vpitch\n");
704 goto exit;
705 }
706 vpitch = vpif_params->std_info.height;
707 }
708
709 /* Check for 8 byte alignment */
710 if (!ALIGN(hpitch, 8)) {
711 if (!update) {
712 vpif_dbg(2, debug, "invalid pitch alignment\n");
713 goto exit;
714 }
715 /* adjust to next 8 byte boundary */
716 hpitch = (((hpitch + 7) / 8) * 8);
717 }
718 /* if update is set, modify the bytesperline and sizeimage */
719 if (update) {
720 pixfmt->bytesperline = hpitch;
721 pixfmt->sizeimage = hpitch * vpitch * 2;
722 }
723 /**
724 * Image width and height is always based on current standard width and
725 * height
726 */
727 pixfmt->width = common->fmt.fmt.pix.width;
728 pixfmt->height = common->fmt.fmt.pix.height;
729 return 0;
730exit:
731 return ret;
732}
733
734/**
735 * vpif_config_addr() - function to configure buffer address in vpif
736 * @ch - channel ptr
737 * @muxmode - channel mux mode
738 */
739static void vpif_config_addr(struct channel_obj *ch, int muxmode)
740{
741 struct common_obj *common;
742
743 vpif_dbg(2, debug, "vpif_config_addr\n");
744
745 common = &(ch->common[VPIF_VIDEO_INDEX]);
746
747 if (VPIF_CHANNEL1_VIDEO == ch->channel_id)
748 common->set_addr = ch1_set_videobuf_addr;
749 else if (2 == muxmode)
750 common->set_addr = ch0_set_videobuf_addr_yc_nmux;
751 else
752 common->set_addr = ch0_set_videobuf_addr;
753}
754
755/**
Dror Cohen6f45b1b2012-07-10 05:43:22 -0300756 * vpif_mmap : It is used to map kernel space buffers into user spaces
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300757 * @filep: file pointer
758 * @vma: ptr to vm_area_struct
759 */
760static int vpif_mmap(struct file *filep, struct vm_area_struct *vma)
761{
762 /* Get the channel object and file handle object */
763 struct vpif_fh *fh = filep->private_data;
764 struct channel_obj *ch = fh->channel;
765 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
Hans Verkuil72246792012-07-31 03:48:31 -0300766 int ret;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300767
768 vpif_dbg(2, debug, "vpif_mmap\n");
769
Hans Verkuil72246792012-07-31 03:48:31 -0300770 if (mutex_lock_interruptible(&common->lock))
771 return -ERESTARTSYS;
772 ret = vb2_mmap(&common->buffer_queue, vma);
773 mutex_unlock(&common->lock);
774 return ret;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300775}
776
777/**
778 * vpif_poll: It is used for select/poll system call
779 * @filep: file pointer
780 * @wait: poll table to wait
781 */
782static unsigned int vpif_poll(struct file *filep, poll_table * wait)
783{
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300784 struct vpif_fh *fh = filep->private_data;
785 struct channel_obj *channel = fh->channel;
786 struct common_obj *common = &(channel->common[VPIF_VIDEO_INDEX]);
Hans Verkuil72246792012-07-31 03:48:31 -0300787 unsigned int res = 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300788
789 vpif_dbg(2, debug, "vpif_poll\n");
790
Hans Verkuil72246792012-07-31 03:48:31 -0300791 if (common->started) {
792 mutex_lock(&common->lock);
793 res = vb2_poll(&common->buffer_queue, filep, wait);
794 mutex_unlock(&common->lock);
795 }
796 return res;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300797}
798
799/**
800 * vpif_open : vpif open handler
801 * @filep: file ptr
802 *
803 * It creates object of file handle structure and stores it in private_data
804 * member of filepointer
805 */
806static int vpif_open(struct file *filep)
807{
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300808 struct video_device *vdev = video_devdata(filep);
809 struct common_obj *common;
810 struct video_obj *vid_ch;
811 struct channel_obj *ch;
812 struct vpif_fh *fh;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300813
814 vpif_dbg(2, debug, "vpif_open\n");
815
816 ch = video_get_drvdata(vdev);
817
818 vid_ch = &ch->video;
819 common = &ch->common[VPIF_VIDEO_INDEX];
820
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300821 /* Allocate memory for the file handle object */
Mats Randgaard1f8766b2010-08-30 10:30:37 -0300822 fh = kzalloc(sizeof(struct vpif_fh), GFP_KERNEL);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300823 if (NULL == fh) {
824 vpif_err("unable to allocate memory for file handle object\n");
Hans Verkuil46656af2011-01-04 06:51:35 -0300825 return -ENOMEM;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300826 }
827
Hans Verkuil72246792012-07-31 03:48:31 -0300828 if (mutex_lock_interruptible(&common->lock)) {
829 kfree(fh);
830 return -ERESTARTSYS;
831 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300832 /* store pointer to fh in private_data member of filep */
833 filep->private_data = fh;
834 fh->channel = ch;
835 fh->initialized = 0;
836 /* If decoder is not initialized. initialize it */
837 if (!ch->initialized) {
838 fh->initialized = 1;
839 ch->initialized = 1;
840 memset(&(ch->vpifparams), 0, sizeof(struct vpif_params));
841 }
842 /* Increment channel usrs counter */
843 ch->usrs++;
844 /* Set io_allowed member to false */
845 fh->io_allowed[VPIF_VIDEO_INDEX] = 0;
846 /* Initialize priority of this instance to default priority */
847 fh->prio = V4L2_PRIORITY_UNSET;
848 v4l2_prio_open(&ch->prio, &fh->prio);
Hans Verkuil72246792012-07-31 03:48:31 -0300849 mutex_unlock(&common->lock);
Hans Verkuil46656af2011-01-04 06:51:35 -0300850 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300851}
852
853/**
854 * vpif_release : function to clean up file close
855 * @filep: file pointer
856 *
Dror Cohen6f45b1b2012-07-10 05:43:22 -0300857 * This function deletes buffer queue, frees the buffers and the vpif file
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300858 * handle
859 */
860static int vpif_release(struct file *filep)
861{
862 struct vpif_fh *fh = filep->private_data;
863 struct channel_obj *ch = fh->channel;
864 struct common_obj *common;
865
866 vpif_dbg(2, debug, "vpif_release\n");
867
868 common = &ch->common[VPIF_VIDEO_INDEX];
869
Hans Verkuil72246792012-07-31 03:48:31 -0300870 mutex_lock(&common->lock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300871 /* if this instance is doing IO */
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -0300872 if (fh->io_allowed[VPIF_VIDEO_INDEX])
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300873 /* Reset io_usrs member of channel object */
874 common->io_usrs = 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300875
876 /* Decrement channel usrs counter */
877 ch->usrs--;
878
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300879 /* Close the priority */
Hans Verkuilffb48772010-05-01 08:03:24 -0300880 v4l2_prio_close(&ch->prio, fh->prio);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300881
882 if (fh->initialized)
883 ch->initialized = 0;
884
Hans Verkuil72246792012-07-31 03:48:31 -0300885 mutex_unlock(&common->lock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300886 filep->private_data = NULL;
887 kfree(fh);
888 return 0;
889}
890
891/**
892 * vpif_reqbufs() - request buffer handler
893 * @file: file ptr
894 * @priv: file handle
895 * @reqbuf: request buffer structure ptr
896 */
897static int vpif_reqbufs(struct file *file, void *priv,
898 struct v4l2_requestbuffers *reqbuf)
899{
900 struct vpif_fh *fh = priv;
901 struct channel_obj *ch = fh->channel;
902 struct common_obj *common;
903 u8 index = 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300904
905 vpif_dbg(2, debug, "vpif_reqbufs\n");
906
907 /**
908 * This file handle has not initialized the channel,
909 * It is not allowed to do settings
910 */
911 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id)
912 || (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
913 if (!fh->initialized) {
914 vpif_dbg(1, debug, "Channel Busy\n");
915 return -EBUSY;
916 }
917 }
918
Manjunath Hadli764af392012-04-13 04:49:34 -0300919 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != reqbuf->type || !vpif_dev)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300920 return -EINVAL;
921
922 index = VPIF_VIDEO_INDEX;
923
924 common = &ch->common[index];
925
Hans Verkuil46656af2011-01-04 06:51:35 -0300926 if (0 != common->io_usrs)
927 return -EBUSY;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300928
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300929 /* Set io allowed member of file handle to TRUE */
930 fh->io_allowed[index] = 1;
931 /* Increment io usrs member of channel object to 1 */
932 common->io_usrs = 1;
933 /* Store type of memory requested in channel object */
934 common->memory = reqbuf->memory;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300935
936 /* Allocate buffers */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300937 return vb2_reqbufs(&common->buffer_queue, reqbuf);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300938}
939
940/**
941 * vpif_querybuf() - query buffer handler
942 * @file: file ptr
943 * @priv: file handle
944 * @buf: v4l2 buffer structure ptr
945 */
946static int vpif_querybuf(struct file *file, void *priv,
947 struct v4l2_buffer *buf)
948{
949 struct vpif_fh *fh = priv;
950 struct channel_obj *ch = fh->channel;
951 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
952
953 vpif_dbg(2, debug, "vpif_querybuf\n");
954
955 if (common->fmt.type != buf->type)
956 return -EINVAL;
957
958 if (common->memory != V4L2_MEMORY_MMAP) {
959 vpif_dbg(1, debug, "Invalid memory\n");
960 return -EINVAL;
961 }
962
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300963 return vb2_querybuf(&common->buffer_queue, buf);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300964}
965
966/**
967 * vpif_qbuf() - query buffer handler
968 * @file: file ptr
969 * @priv: file handle
970 * @buf: v4l2 buffer structure ptr
971 */
972static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
973{
974
975 struct vpif_fh *fh = priv;
976 struct channel_obj *ch = fh->channel;
977 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
978 struct v4l2_buffer tbuf = *buf;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300979
980 vpif_dbg(2, debug, "vpif_qbuf\n");
981
982 if (common->fmt.type != tbuf.type) {
983 vpif_err("invalid buffer type\n");
984 return -EINVAL;
985 }
986
987 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300988 vpif_err("fh io not allowed\n");
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300989 return -EACCES;
990 }
991
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300992 return vb2_qbuf(&common->buffer_queue, buf);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300993}
994
995/**
996 * vpif_dqbuf() - query buffer handler
997 * @file: file ptr
998 * @priv: file handle
999 * @buf: v4l2 buffer structure ptr
1000 */
1001static int vpif_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1002{
1003 struct vpif_fh *fh = priv;
1004 struct channel_obj *ch = fh->channel;
1005 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1006
1007 vpif_dbg(2, debug, "vpif_dqbuf\n");
1008
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001009 return vb2_dqbuf(&common->buffer_queue, buf,
1010 (file->f_flags & O_NONBLOCK));
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001011}
1012
1013/**
1014 * vpif_streamon() - streamon handler
1015 * @file: file ptr
1016 * @priv: file handle
1017 * @buftype: v4l2 buffer type
1018 */
1019static int vpif_streamon(struct file *file, void *priv,
1020 enum v4l2_buf_type buftype)
1021{
1022
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001023 struct vpif_fh *fh = priv;
1024 struct channel_obj *ch = fh->channel;
1025 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1026 struct channel_obj *oth_ch = vpif_obj.dev[!ch->channel_id];
1027 struct vpif_params *vpif;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001028 int ret = 0;
1029
1030 vpif_dbg(2, debug, "vpif_streamon\n");
1031
1032 vpif = &ch->vpifparams;
1033
1034 if (buftype != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1035 vpif_dbg(1, debug, "buffer type not supported\n");
1036 return -EINVAL;
1037 }
1038
1039 /* If file handle is not allowed IO, return error */
1040 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1041 vpif_dbg(1, debug, "io not allowed\n");
1042 return -EACCES;
1043 }
1044
1045 /* If Streaming is already started, return error */
1046 if (common->started) {
1047 vpif_dbg(1, debug, "channel->started\n");
1048 return -EBUSY;
1049 }
1050
1051 if ((ch->channel_id == VPIF_CHANNEL0_VIDEO &&
1052 oth_ch->common[VPIF_VIDEO_INDEX].started &&
1053 vpif->std_info.ycmux_mode == 0) ||
1054 ((ch->channel_id == VPIF_CHANNEL1_VIDEO) &&
1055 (2 == oth_ch->common[VPIF_VIDEO_INDEX].started))) {
1056 vpif_dbg(1, debug, "other channel is being used\n");
1057 return -EBUSY;
1058 }
1059
1060 ret = vpif_check_format(ch, &common->fmt.fmt.pix, 0);
1061 if (ret)
1062 return ret;
1063
1064 /* Enable streamon on the sub device */
Hans Verkuil178cce12012-09-20 09:06:30 -03001065 ret = v4l2_subdev_call(ch->sd, video, s_stream, 1);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001066
Hans Verkuil178cce12012-09-20 09:06:30 -03001067 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001068 vpif_dbg(1, debug, "stream on failed in subdev\n");
1069 return ret;
1070 }
1071
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001072 /* Call vb2_streamon to start streaming in videobuf2 */
1073 ret = vb2_streamon(&common->buffer_queue, buftype);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001074 if (ret) {
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001075 vpif_dbg(1, debug, "vb2_streamon\n");
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001076 return ret;
1077 }
1078
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001079 return ret;
1080}
1081
1082/**
1083 * vpif_streamoff() - streamoff handler
1084 * @file: file ptr
1085 * @priv: file handle
1086 * @buftype: v4l2 buffer type
1087 */
1088static int vpif_streamoff(struct file *file, void *priv,
1089 enum v4l2_buf_type buftype)
1090{
1091
1092 struct vpif_fh *fh = priv;
1093 struct channel_obj *ch = fh->channel;
1094 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1095 int ret;
1096
1097 vpif_dbg(2, debug, "vpif_streamoff\n");
1098
1099 if (buftype != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1100 vpif_dbg(1, debug, "buffer type not supported\n");
1101 return -EINVAL;
1102 }
1103
1104 /* If io is allowed for this file handle, return error */
1105 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1106 vpif_dbg(1, debug, "io not allowed\n");
1107 return -EACCES;
1108 }
1109
1110 /* If streaming is not started, return error */
1111 if (!common->started) {
1112 vpif_dbg(1, debug, "channel->started\n");
1113 return -EINVAL;
1114 }
1115
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001116 /* disable channel */
1117 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
1118 enable_channel0(0);
1119 channel0_intr_enable(0);
1120 } else {
1121 enable_channel1(0);
1122 channel1_intr_enable(0);
1123 }
1124
1125 common->started = 0;
1126
Hans Verkuil178cce12012-09-20 09:06:30 -03001127 ret = v4l2_subdev_call(ch->sd, video, s_stream, 0);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001128
Hans Verkuil178cce12012-09-20 09:06:30 -03001129 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001130 vpif_dbg(1, debug, "stream off failed in subdev\n");
1131
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001132 return vb2_streamoff(&common->buffer_queue, buftype);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001133}
1134
1135/**
Hans Verkuil178cce12012-09-20 09:06:30 -03001136 * vpif_input_to_subdev() - Maps input to sub device
1137 * @vpif_cfg - global config ptr
1138 * @chan_cfg - channel config ptr
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001139 * @input_index - Given input index from application
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001140 *
1141 * lookup the sub device information for a given input index.
1142 * we report all the inputs to application. inputs table also
1143 * has sub device name for the each input
1144 */
Hans Verkuil178cce12012-09-20 09:06:30 -03001145static int vpif_input_to_subdev(
1146 struct vpif_capture_config *vpif_cfg,
1147 struct vpif_capture_chan_config *chan_cfg,
1148 int input_index)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001149{
Hans Verkuil178cce12012-09-20 09:06:30 -03001150 struct vpif_subdev_info *subdev_info;
1151 const char *subdev_name;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001152 int i;
1153
Hans Verkuil178cce12012-09-20 09:06:30 -03001154 vpif_dbg(2, debug, "vpif_input_to_subdev\n");
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001155
Hans Verkuil178cce12012-09-20 09:06:30 -03001156 subdev_name = chan_cfg->inputs[input_index].subdev_name;
1157 if (subdev_name == NULL)
1158 return -1;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001159
1160 /* loop through the sub device list to get the sub device info */
1161 for (i = 0; i < vpif_cfg->subdev_count; i++) {
1162 subdev_info = &vpif_cfg->subdev_info[i];
1163 if (!strcmp(subdev_info->name, subdev_name))
Hans Verkuil178cce12012-09-20 09:06:30 -03001164 return i;
1165 }
1166 return -1;
1167}
1168
1169/**
1170 * vpif_set_input() - Select an input
1171 * @vpif_cfg - global config ptr
1172 * @ch - channel
1173 * @_index - Given input index from application
1174 *
1175 * Select the given input.
1176 */
1177static int vpif_set_input(
1178 struct vpif_capture_config *vpif_cfg,
1179 struct channel_obj *ch,
1180 int index)
1181{
1182 struct vpif_capture_chan_config *chan_cfg =
1183 &vpif_cfg->chan_config[ch->channel_id];
1184 struct vpif_subdev_info *subdev_info = NULL;
1185 struct v4l2_subdev *sd = NULL;
1186 u32 input = 0, output = 0;
1187 int sd_index;
1188 int ret;
1189
1190 sd_index = vpif_input_to_subdev(vpif_cfg, chan_cfg, index);
1191 if (sd_index >= 0) {
1192 sd = vpif_obj.sd[sd_index];
1193 subdev_info = &vpif_cfg->subdev_info[sd_index];
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001194 }
1195
Hans Verkuil178cce12012-09-20 09:06:30 -03001196 /* first setup input path from sub device to vpif */
1197 if (sd && vpif_cfg->setup_input_path) {
1198 ret = vpif_cfg->setup_input_path(ch->channel_id,
1199 subdev_info->name);
1200 if (ret < 0) {
1201 vpif_dbg(1, debug, "couldn't setup input path for the" \
1202 " sub device %s, for input index %d\n",
1203 subdev_info->name, index);
1204 return ret;
1205 }
1206 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001207
Hans Verkuil178cce12012-09-20 09:06:30 -03001208 if (sd) {
1209 input = chan_cfg->inputs[index].input_route;
1210 output = chan_cfg->inputs[index].output_route;
1211 ret = v4l2_subdev_call(sd, video, s_routing,
1212 input, output, 0);
1213 if (ret < 0 && ret != -ENOIOCTLCMD) {
1214 vpif_dbg(1, debug, "Failed to set input\n");
1215 return ret;
1216 }
1217 }
1218 ch->input_idx = index;
1219 ch->sd = sd;
1220 /* copy interface parameters to vpif */
Hans Verkuil0d4f35f2012-09-20 09:06:32 -03001221 ch->vpifparams.iface = chan_cfg->vpif_if;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001222
Hans Verkuil178cce12012-09-20 09:06:30 -03001223 /* update tvnorms from the sub device input info */
1224 ch->video_dev->tvnorms = chan_cfg->inputs[index].input.std;
1225 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001226}
1227
1228/**
1229 * vpif_querystd() - querystd handler
1230 * @file: file ptr
1231 * @priv: file handle
1232 * @std_id: ptr to std id
1233 *
1234 * This function is called to detect standard at the selected input
1235 */
1236static int vpif_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
1237{
1238 struct vpif_fh *fh = priv;
1239 struct channel_obj *ch = fh->channel;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001240 int ret = 0;
1241
1242 vpif_dbg(2, debug, "vpif_querystd\n");
1243
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001244 /* Call querystd function of decoder device */
Hans Verkuil178cce12012-09-20 09:06:30 -03001245 ret = v4l2_subdev_call(ch->sd, video, querystd, std_id);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001246
Hans Verkuil178cce12012-09-20 09:06:30 -03001247 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1248 return -ENODATA;
1249 if (ret) {
1250 vpif_dbg(1, debug, "Failed to query standard for sub devices\n");
1251 return ret;
1252 }
1253
1254 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001255}
1256
1257/**
1258 * vpif_g_std() - get STD handler
1259 * @file: file ptr
1260 * @priv: file handle
1261 * @std_id: ptr to std id
1262 */
1263static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
1264{
1265 struct vpif_fh *fh = priv;
1266 struct channel_obj *ch = fh->channel;
1267
1268 vpif_dbg(2, debug, "vpif_g_std\n");
1269
1270 *std = ch->video.stdid;
1271 return 0;
1272}
1273
1274/**
1275 * vpif_s_std() - set STD handler
1276 * @file: file ptr
1277 * @priv: file handle
1278 * @std_id: ptr to std id
1279 */
Hans Verkuil314527a2013-03-15 06:10:40 -03001280static int vpif_s_std(struct file *file, void *priv, v4l2_std_id std_id)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001281{
1282 struct vpif_fh *fh = priv;
1283 struct channel_obj *ch = fh->channel;
1284 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1285 int ret = 0;
1286
1287 vpif_dbg(2, debug, "vpif_s_std\n");
1288
1289 if (common->started) {
1290 vpif_err("streaming in progress\n");
1291 return -EBUSY;
1292 }
1293
1294 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1295 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1296 if (!fh->initialized) {
1297 vpif_dbg(1, debug, "Channel Busy\n");
1298 return -EBUSY;
1299 }
1300 }
1301
Hans Verkuilffb48772010-05-01 08:03:24 -03001302 ret = v4l2_prio_check(&ch->prio, fh->prio);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001303 if (0 != ret)
1304 return ret;
1305
1306 fh->initialized = 1;
1307
1308 /* Call encoder subdevice function to set the standard */
Hans Verkuil314527a2013-03-15 06:10:40 -03001309 ch->video.stdid = std_id;
Hans Verkuil0598c172012-09-18 07:18:47 -03001310 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001311
1312 /* Get the information about the standard */
1313 if (vpif_update_std_info(ch)) {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001314 vpif_err("Error getting the standard info\n");
Hans Verkuil46656af2011-01-04 06:51:35 -03001315 return -EINVAL;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001316 }
1317
1318 /* Configure the default format information */
1319 vpif_config_format(ch);
1320
1321 /* set standard in the sub device */
Hans Verkuil314527a2013-03-15 06:10:40 -03001322 ret = v4l2_subdev_call(ch->sd, core, s_std, std_id);
Hans Verkuil178cce12012-09-20 09:06:30 -03001323 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001324 vpif_dbg(1, debug, "Failed to set standard for sub devices\n");
Hans Verkuil178cce12012-09-20 09:06:30 -03001325 return ret;
1326 }
1327 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001328}
1329
1330/**
1331 * vpif_enum_input() - ENUMINPUT handler
1332 * @file: file ptr
1333 * @priv: file handle
1334 * @input: ptr to input structure
1335 */
1336static int vpif_enum_input(struct file *file, void *priv,
1337 struct v4l2_input *input)
1338{
1339
1340 struct vpif_capture_config *config = vpif_dev->platform_data;
1341 struct vpif_capture_chan_config *chan_cfg;
1342 struct vpif_fh *fh = priv;
1343 struct channel_obj *ch = fh->channel;
1344
1345 chan_cfg = &config->chan_config[ch->channel_id];
1346
1347 if (input->index >= chan_cfg->input_count) {
1348 vpif_dbg(1, debug, "Invalid input index\n");
1349 return -EINVAL;
1350 }
1351
1352 memcpy(input, &chan_cfg->inputs[input->index].input,
1353 sizeof(*input));
1354 return 0;
1355}
1356
1357/**
1358 * vpif_g_input() - Get INPUT handler
1359 * @file: file ptr
1360 * @priv: file handle
1361 * @index: ptr to input index
1362 */
1363static int vpif_g_input(struct file *file, void *priv, unsigned int *index)
1364{
1365 struct vpif_fh *fh = priv;
1366 struct channel_obj *ch = fh->channel;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001367
Hans Verkuil6f47c6c2012-09-20 09:06:22 -03001368 *index = ch->input_idx;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001369 return 0;
1370}
1371
1372/**
1373 * vpif_s_input() - Set INPUT handler
1374 * @file: file ptr
1375 * @priv: file handle
1376 * @index: input index
1377 */
1378static int vpif_s_input(struct file *file, void *priv, unsigned int index)
1379{
1380 struct vpif_capture_config *config = vpif_dev->platform_data;
1381 struct vpif_capture_chan_config *chan_cfg;
1382 struct vpif_fh *fh = priv;
1383 struct channel_obj *ch = fh->channel;
1384 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
Hans Verkuil178cce12012-09-20 09:06:30 -03001385 int ret;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001386
1387 chan_cfg = &config->chan_config[ch->channel_id];
1388
Hans Verkuil7aaad132012-09-20 09:06:25 -03001389 if (index >= chan_cfg->input_count)
1390 return -EINVAL;
1391
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001392 if (common->started) {
1393 vpif_err("Streaming in progress\n");
1394 return -EBUSY;
1395 }
1396
1397 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1398 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1399 if (!fh->initialized) {
1400 vpif_dbg(1, debug, "Channel Busy\n");
1401 return -EBUSY;
1402 }
1403 }
1404
Hans Verkuilffb48772010-05-01 08:03:24 -03001405 ret = v4l2_prio_check(&ch->prio, fh->prio);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001406 if (0 != ret)
1407 return ret;
1408
1409 fh->initialized = 1;
Hans Verkuil178cce12012-09-20 09:06:30 -03001410 return vpif_set_input(config, ch, index);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001411}
1412
1413/**
1414 * vpif_enum_fmt_vid_cap() - ENUM_FMT handler
1415 * @file: file ptr
1416 * @priv: file handle
1417 * @index: input index
1418 */
1419static int vpif_enum_fmt_vid_cap(struct file *file, void *priv,
1420 struct v4l2_fmtdesc *fmt)
1421{
1422 struct vpif_fh *fh = priv;
1423 struct channel_obj *ch = fh->channel;
1424
1425 if (fmt->index != 0) {
1426 vpif_dbg(1, debug, "Invalid format index\n");
1427 return -EINVAL;
1428 }
1429
1430 /* Fill in the information about format */
1431 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER) {
1432 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1433 strcpy(fmt->description, "Raw Mode -Bayer Pattern GrRBGb");
1434 fmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
1435 } else {
1436 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1437 strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
1438 fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
1439 }
1440 return 0;
1441}
1442
1443/**
1444 * vpif_try_fmt_vid_cap() - TRY_FMT handler
1445 * @file: file ptr
1446 * @priv: file handle
1447 * @fmt: ptr to v4l2 format structure
1448 */
1449static int vpif_try_fmt_vid_cap(struct file *file, void *priv,
1450 struct v4l2_format *fmt)
1451{
1452 struct vpif_fh *fh = priv;
1453 struct channel_obj *ch = fh->channel;
1454 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
1455
1456 return vpif_check_format(ch, pixfmt, 1);
1457}
1458
1459
1460/**
1461 * vpif_g_fmt_vid_cap() - Set INPUT handler
1462 * @file: file ptr
1463 * @priv: file handle
1464 * @fmt: ptr to v4l2 format structure
1465 */
1466static int vpif_g_fmt_vid_cap(struct file *file, void *priv,
1467 struct v4l2_format *fmt)
1468{
1469 struct vpif_fh *fh = priv;
1470 struct channel_obj *ch = fh->channel;
1471 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1472
1473 /* Check the validity of the buffer type */
1474 if (common->fmt.type != fmt->type)
1475 return -EINVAL;
1476
1477 /* Fill in the information about format */
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001478 *fmt = common->fmt;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001479 return 0;
1480}
1481
1482/**
1483 * vpif_s_fmt_vid_cap() - Set FMT handler
1484 * @file: file ptr
1485 * @priv: file handle
1486 * @fmt: ptr to v4l2 format structure
1487 */
1488static int vpif_s_fmt_vid_cap(struct file *file, void *priv,
1489 struct v4l2_format *fmt)
1490{
1491 struct vpif_fh *fh = priv;
1492 struct channel_obj *ch = fh->channel;
1493 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1494 struct v4l2_pix_format *pixfmt;
1495 int ret = 0;
1496
Mats Randgaard2c0ddd12010-12-16 12:17:45 -03001497 vpif_dbg(2, debug, "%s\n", __func__);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001498
1499 /* If streaming is started, return error */
1500 if (common->started) {
1501 vpif_dbg(1, debug, "Streaming is started\n");
1502 return -EBUSY;
1503 }
1504
1505 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1506 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1507 if (!fh->initialized) {
1508 vpif_dbg(1, debug, "Channel Busy\n");
1509 return -EBUSY;
1510 }
1511 }
1512
Hans Verkuilffb48772010-05-01 08:03:24 -03001513 ret = v4l2_prio_check(&ch->prio, fh->prio);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001514 if (0 != ret)
1515 return ret;
1516
1517 fh->initialized = 1;
1518
1519 pixfmt = &fmt->fmt.pix;
1520 /* Check for valid field format */
1521 ret = vpif_check_format(ch, pixfmt, 0);
1522
1523 if (ret)
1524 return ret;
1525 /* store the format in the channel object */
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001526 common->fmt = *fmt;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001527 return 0;
1528}
1529
1530/**
1531 * vpif_querycap() - QUERYCAP handler
1532 * @file: file ptr
1533 * @priv: file handle
1534 * @cap: ptr to v4l2_capability structure
1535 */
1536static int vpif_querycap(struct file *file, void *priv,
1537 struct v4l2_capability *cap)
1538{
1539 struct vpif_capture_config *config = vpif_dev->platform_data;
1540
Lad, Prabhakar626d533f2012-09-25 11:21:55 -03001541 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1542 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1543 snprintf(cap->driver, sizeof(cap->driver), "%s", dev_name(vpif_dev));
1544 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
1545 dev_name(vpif_dev));
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001546 strlcpy(cap->card, config->card_name, sizeof(cap->card));
1547
1548 return 0;
1549}
1550
1551/**
1552 * vpif_g_priority() - get priority handler
1553 * @file: file ptr
1554 * @priv: file handle
1555 * @prio: ptr to v4l2_priority structure
1556 */
1557static int vpif_g_priority(struct file *file, void *priv,
1558 enum v4l2_priority *prio)
1559{
1560 struct vpif_fh *fh = priv;
1561 struct channel_obj *ch = fh->channel;
1562
1563 *prio = v4l2_prio_max(&ch->prio);
1564
1565 return 0;
1566}
1567
1568/**
1569 * vpif_s_priority() - set priority handler
1570 * @file: file ptr
1571 * @priv: file handle
1572 * @prio: ptr to v4l2_priority structure
1573 */
1574static int vpif_s_priority(struct file *file, void *priv, enum v4l2_priority p)
1575{
1576 struct vpif_fh *fh = priv;
1577 struct channel_obj *ch = fh->channel;
1578
1579 return v4l2_prio_change(&ch->prio, &fh->prio, p);
1580}
1581
1582/**
1583 * vpif_cropcap() - cropcap handler
1584 * @file: file ptr
1585 * @priv: file handle
1586 * @crop: ptr to v4l2_cropcap structure
1587 */
1588static int vpif_cropcap(struct file *file, void *priv,
1589 struct v4l2_cropcap *crop)
1590{
1591 struct vpif_fh *fh = priv;
1592 struct channel_obj *ch = fh->channel;
1593 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1594
1595 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != crop->type)
1596 return -EINVAL;
1597
1598 crop->bounds.left = 0;
1599 crop->bounds.top = 0;
1600 crop->bounds.height = common->height;
1601 crop->bounds.width = common->width;
1602 crop->defrect = crop->bounds;
1603 return 0;
1604}
1605
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001606/**
Hans Verkuil0598c172012-09-18 07:18:47 -03001607 * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001608 * @file: file ptr
1609 * @priv: file handle
Hans Verkuil0598c172012-09-18 07:18:47 -03001610 * @timings: input timings
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001611 */
Hans Verkuil0598c172012-09-18 07:18:47 -03001612static int
1613vpif_enum_dv_timings(struct file *file, void *priv,
1614 struct v4l2_enum_dv_timings *timings)
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001615{
1616 struct vpif_fh *fh = priv;
1617 struct channel_obj *ch = fh->channel;
Hans Verkuil178cce12012-09-20 09:06:30 -03001618 int ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001619
Hans Verkuil178cce12012-09-20 09:06:30 -03001620 ret = v4l2_subdev_call(ch->sd, video, enum_dv_timings, timings);
Wei Yongjune070f1b2012-10-30 09:45:00 -03001621 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
Hans Verkuil178cce12012-09-20 09:06:30 -03001622 return -EINVAL;
1623 return ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001624}
1625
1626/**
Hans Verkuil0598c172012-09-18 07:18:47 -03001627 * vpif_query_dv_timings() - QUERY_DV_TIMINGS handler
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001628 * @file: file ptr
1629 * @priv: file handle
Hans Verkuil0598c172012-09-18 07:18:47 -03001630 * @timings: input timings
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001631 */
Hans Verkuil0598c172012-09-18 07:18:47 -03001632static int
1633vpif_query_dv_timings(struct file *file, void *priv,
1634 struct v4l2_dv_timings *timings)
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001635{
1636 struct vpif_fh *fh = priv;
1637 struct channel_obj *ch = fh->channel;
Hans Verkuil178cce12012-09-20 09:06:30 -03001638 int ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001639
Hans Verkuil178cce12012-09-20 09:06:30 -03001640 ret = v4l2_subdev_call(ch->sd, video, query_dv_timings, timings);
Wei Yongjune070f1b2012-10-30 09:45:00 -03001641 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
Hans Verkuil178cce12012-09-20 09:06:30 -03001642 return -ENODATA;
1643 return ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001644}
1645
Mats Randgaardc027e162010-12-16 12:17:44 -03001646/**
1647 * vpif_s_dv_timings() - S_DV_TIMINGS handler
1648 * @file: file ptr
1649 * @priv: file handle
1650 * @timings: digital video timings
1651 */
1652static int vpif_s_dv_timings(struct file *file, void *priv,
1653 struct v4l2_dv_timings *timings)
1654{
1655 struct vpif_fh *fh = priv;
1656 struct channel_obj *ch = fh->channel;
1657 struct vpif_params *vpifparams = &ch->vpifparams;
1658 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
1659 struct video_obj *vid_ch = &ch->video;
Hans Verkuil0598c172012-09-18 07:18:47 -03001660 struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
Mats Randgaardc027e162010-12-16 12:17:44 -03001661 int ret;
1662
1663 if (timings->type != V4L2_DV_BT_656_1120) {
1664 vpif_dbg(2, debug, "Timing type not defined\n");
1665 return -EINVAL;
1666 }
1667
1668 /* Configure subdevice timings, if any */
Hans Verkuil178cce12012-09-20 09:06:30 -03001669 ret = v4l2_subdev_call(ch->sd, video, s_dv_timings, timings);
1670 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1671 ret = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001672 if (ret < 0) {
1673 vpif_dbg(2, debug, "Error setting custom DV timings\n");
1674 return ret;
1675 }
1676
1677 if (!(timings->bt.width && timings->bt.height &&
1678 (timings->bt.hbackporch ||
1679 timings->bt.hfrontporch ||
1680 timings->bt.hsync) &&
1681 timings->bt.vfrontporch &&
1682 (timings->bt.vbackporch ||
1683 timings->bt.vsync))) {
1684 vpif_dbg(2, debug, "Timings for width, height, "
1685 "horizontal back porch, horizontal sync, "
1686 "horizontal front porch, vertical back porch, "
1687 "vertical sync and vertical back porch "
1688 "must be defined\n");
1689 return -EINVAL;
1690 }
1691
Hans Verkuil0598c172012-09-18 07:18:47 -03001692 vid_ch->dv_timings = *timings;
Mats Randgaardc027e162010-12-16 12:17:44 -03001693
1694 /* Configure video port timings */
1695
Hans Verkuile3655262013-07-29 08:41:00 -03001696 std_info->eav2sav = V4L2_DV_BT_BLANKING_WIDTH(bt) - 8;
Mats Randgaardc027e162010-12-16 12:17:44 -03001697 std_info->sav2eav = bt->width;
1698
1699 std_info->l1 = 1;
1700 std_info->l3 = bt->vsync + bt->vbackporch + 1;
1701
Hans Verkuile3655262013-07-29 08:41:00 -03001702 std_info->vsize = V4L2_DV_BT_FRAME_HEIGHT(bt);
Mats Randgaardc027e162010-12-16 12:17:44 -03001703 if (bt->interlaced) {
1704 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
Mats Randgaardc027e162010-12-16 12:17:44 -03001705 std_info->l5 = std_info->vsize/2 -
1706 (bt->vfrontporch - 1);
1707 std_info->l7 = std_info->vsize/2 + 1;
1708 std_info->l9 = std_info->l7 + bt->il_vsync +
1709 bt->il_vbackporch + 1;
1710 std_info->l11 = std_info->vsize -
1711 (bt->il_vfrontporch - 1);
1712 } else {
1713 vpif_dbg(2, debug, "Required timing values for "
1714 "interlaced BT format missing\n");
1715 return -EINVAL;
1716 }
1717 } else {
Mats Randgaardc027e162010-12-16 12:17:44 -03001718 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
1719 }
1720 strncpy(std_info->name, "Custom timings BT656/1120", VPIF_MAX_NAME);
1721 std_info->width = bt->width;
1722 std_info->height = bt->height;
1723 std_info->frm_fmt = bt->interlaced ? 0 : 1;
1724 std_info->ycmux_mode = 0;
1725 std_info->capture_format = 0;
1726 std_info->vbi_supported = 0;
1727 std_info->hd_sd = 1;
1728 std_info->stdid = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001729
1730 vid_ch->stdid = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001731 return 0;
1732}
1733
1734/**
1735 * vpif_g_dv_timings() - G_DV_TIMINGS handler
1736 * @file: file ptr
1737 * @priv: file handle
1738 * @timings: digital video timings
1739 */
1740static int vpif_g_dv_timings(struct file *file, void *priv,
1741 struct v4l2_dv_timings *timings)
1742{
1743 struct vpif_fh *fh = priv;
1744 struct channel_obj *ch = fh->channel;
1745 struct video_obj *vid_ch = &ch->video;
Mats Randgaardc027e162010-12-16 12:17:44 -03001746
Hans Verkuil0598c172012-09-18 07:18:47 -03001747 *timings = vid_ch->dv_timings;
Mats Randgaardc027e162010-12-16 12:17:44 -03001748
1749 return 0;
1750}
1751
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001752/*
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001753 * vpif_log_status() - Status information
1754 * @file: file ptr
1755 * @priv: file handle
1756 *
1757 * Returns zero.
1758 */
1759static int vpif_log_status(struct file *filep, void *priv)
1760{
1761 /* status for sub devices */
1762 v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1763
1764 return 0;
1765}
1766
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001767/* vpif capture ioctl operations */
1768static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
1769 .vidioc_querycap = vpif_querycap,
1770 .vidioc_g_priority = vpif_g_priority,
1771 .vidioc_s_priority = vpif_s_priority,
1772 .vidioc_enum_fmt_vid_cap = vpif_enum_fmt_vid_cap,
1773 .vidioc_g_fmt_vid_cap = vpif_g_fmt_vid_cap,
1774 .vidioc_s_fmt_vid_cap = vpif_s_fmt_vid_cap,
1775 .vidioc_try_fmt_vid_cap = vpif_try_fmt_vid_cap,
1776 .vidioc_enum_input = vpif_enum_input,
1777 .vidioc_s_input = vpif_s_input,
1778 .vidioc_g_input = vpif_g_input,
1779 .vidioc_reqbufs = vpif_reqbufs,
1780 .vidioc_querybuf = vpif_querybuf,
1781 .vidioc_querystd = vpif_querystd,
1782 .vidioc_s_std = vpif_s_std,
1783 .vidioc_g_std = vpif_g_std,
1784 .vidioc_qbuf = vpif_qbuf,
1785 .vidioc_dqbuf = vpif_dqbuf,
1786 .vidioc_streamon = vpif_streamon,
1787 .vidioc_streamoff = vpif_streamoff,
1788 .vidioc_cropcap = vpif_cropcap,
Hans Verkuil0598c172012-09-18 07:18:47 -03001789 .vidioc_enum_dv_timings = vpif_enum_dv_timings,
1790 .vidioc_query_dv_timings = vpif_query_dv_timings,
Mats Randgaardc027e162010-12-16 12:17:44 -03001791 .vidioc_s_dv_timings = vpif_s_dv_timings,
1792 .vidioc_g_dv_timings = vpif_g_dv_timings,
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001793 .vidioc_log_status = vpif_log_status,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001794};
1795
1796/* vpif file operations */
1797static struct v4l2_file_operations vpif_fops = {
1798 .owner = THIS_MODULE,
1799 .open = vpif_open,
1800 .release = vpif_release,
Hans Verkuil46656af2011-01-04 06:51:35 -03001801 .unlocked_ioctl = video_ioctl2,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001802 .mmap = vpif_mmap,
1803 .poll = vpif_poll
1804};
1805
1806/* vpif video template */
1807static struct video_device vpif_video_template = {
1808 .name = "vpif",
1809 .fops = &vpif_fops,
1810 .minor = -1,
1811 .ioctl_ops = &vpif_ioctl_ops,
1812};
1813
1814/**
1815 * initialize_vpif() - Initialize vpif data structures
1816 *
1817 * Allocate memory for data structures and initialize them
1818 */
1819static int initialize_vpif(void)
1820{
1821 int err = 0, i, j;
1822 int free_channel_objects_index;
1823
1824 /* Default number of buffers should be 3 */
1825 if ((ch0_numbuffers > 0) &&
1826 (ch0_numbuffers < config_params.min_numbuffers))
1827 ch0_numbuffers = config_params.min_numbuffers;
1828 if ((ch1_numbuffers > 0) &&
1829 (ch1_numbuffers < config_params.min_numbuffers))
1830 ch1_numbuffers = config_params.min_numbuffers;
1831
1832 /* Set buffer size to min buffers size if it is invalid */
1833 if (ch0_bufsize < config_params.min_bufsize[VPIF_CHANNEL0_VIDEO])
1834 ch0_bufsize =
1835 config_params.min_bufsize[VPIF_CHANNEL0_VIDEO];
1836 if (ch1_bufsize < config_params.min_bufsize[VPIF_CHANNEL1_VIDEO])
1837 ch1_bufsize =
1838 config_params.min_bufsize[VPIF_CHANNEL1_VIDEO];
1839
1840 config_params.numbuffers[VPIF_CHANNEL0_VIDEO] = ch0_numbuffers;
1841 config_params.numbuffers[VPIF_CHANNEL1_VIDEO] = ch1_numbuffers;
1842 if (ch0_numbuffers) {
1843 config_params.channel_bufsize[VPIF_CHANNEL0_VIDEO]
1844 = ch0_bufsize;
1845 }
1846 if (ch1_numbuffers) {
1847 config_params.channel_bufsize[VPIF_CHANNEL1_VIDEO]
1848 = ch1_bufsize;
1849 }
1850
1851 /* Allocate memory for six channel objects */
1852 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1853 vpif_obj.dev[i] =
1854 kzalloc(sizeof(*vpif_obj.dev[i]), GFP_KERNEL);
1855 /* If memory allocation fails, return error */
1856 if (!vpif_obj.dev[i]) {
1857 free_channel_objects_index = i;
1858 err = -ENOMEM;
1859 goto vpif_init_free_channel_objects;
1860 }
1861 }
1862 return 0;
1863
1864vpif_init_free_channel_objects:
1865 for (j = 0; j < free_channel_objects_index; j++)
1866 kfree(vpif_obj.dev[j]);
1867 return err;
1868}
1869
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001870static int vpif_async_bound(struct v4l2_async_notifier *notifier,
1871 struct v4l2_subdev *subdev,
1872 struct v4l2_async_subdev *asd)
1873{
1874 int i;
1875
1876 for (i = 0; i < vpif_obj.config->subdev_count; i++)
1877 if (!strcmp(vpif_obj.config->subdev_info[i].name,
1878 subdev->name)) {
1879 vpif_obj.sd[i] = subdev;
1880 return 0;
1881 }
1882
1883 return -EINVAL;
1884}
1885
1886static int vpif_probe_complete(void)
1887{
1888 struct common_obj *common;
1889 struct channel_obj *ch;
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001890 struct vb2_queue *q;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001891 int i, j, err, k;
1892
1893 for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
1894 ch = vpif_obj.dev[j];
1895 ch->channel_id = j;
1896 common = &(ch->common[VPIF_VIDEO_INDEX]);
1897 spin_lock_init(&common->irqlock);
1898 mutex_init(&common->lock);
1899 ch->video_dev->lock = &common->lock;
1900 /* Initialize prio member of channel object */
1901 v4l2_prio_init(&ch->prio);
1902 video_set_drvdata(ch->video_dev, ch);
1903
1904 /* select input 0 */
1905 err = vpif_set_input(vpif_obj.config, ch, 0);
1906 if (err)
1907 goto probe_out;
1908
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001909 /* Initialize vb2 queue */
1910 q = &common->buffer_queue;
1911 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1912 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1913 q->drv_priv = ch;
1914 q->ops = &video_qops;
1915 q->mem_ops = &vb2_dma_contig_memops;
1916 q->buf_struct_size = sizeof(struct vpif_cap_buffer);
1917 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1918 q->min_buffers_needed = 1;
Lad, Prabhakar999ba692014-05-16 10:33:33 -03001919 q->lock = &common->lock;
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001920
1921 err = vb2_queue_init(q);
1922 if (err) {
1923 vpif_err("vpif_capture: vb2_queue_init() failed\n");
1924 goto probe_out;
1925 }
1926
1927 common->alloc_ctx = vb2_dma_contig_init_ctx(vpif_dev);
1928 if (IS_ERR(common->alloc_ctx)) {
1929 vpif_err("Failed to get the context\n");
1930 err = PTR_ERR(common->alloc_ctx);
1931 goto probe_out;
1932 }
1933
1934 INIT_LIST_HEAD(&common->dma_queue);
1935
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001936 err = video_register_device(ch->video_dev,
1937 VFL_TYPE_GRABBER, (j ? 1 : 0));
1938 if (err)
1939 goto probe_out;
1940 }
1941
1942 v4l2_info(&vpif_obj.v4l2_dev, "VPIF capture driver initialized\n");
1943 return 0;
1944
1945probe_out:
1946 for (k = 0; k < j; k++) {
1947 /* Get the pointer to the channel object */
1948 ch = vpif_obj.dev[k];
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001949 common = &ch->common[k];
1950 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001951 /* Unregister video device */
1952 video_unregister_device(ch->video_dev);
1953 }
1954 kfree(vpif_obj.sd);
1955 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1956 ch = vpif_obj.dev[i];
1957 /* Note: does nothing if ch->video_dev == NULL */
1958 video_device_release(ch->video_dev);
1959 }
1960 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1961
1962 return err;
1963}
1964
1965static int vpif_async_complete(struct v4l2_async_notifier *notifier)
1966{
1967 return vpif_probe_complete();
1968}
1969
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001970/**
1971 * vpif_probe : This function probes the vpif capture driver
1972 * @pdev: platform device pointer
1973 *
1974 * This creates device entries by register itself to the V4L2 driver and
1975 * initializes fields of each channel objects
1976 */
1977static __init int vpif_probe(struct platform_device *pdev)
1978{
1979 struct vpif_subdev_info *subdevdata;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001980 int i, j, err;
Hans Verkuil5be452c2012-09-20 09:06:29 -03001981 int res_idx = 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001982 struct i2c_adapter *i2c_adap;
1983 struct channel_obj *ch;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001984 struct video_device *vfd;
1985 struct resource *res;
1986 int subdev_count;
Manjunath Hadli764af392012-04-13 04:49:34 -03001987 size_t size;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001988
1989 vpif_dev = &pdev->dev;
1990
1991 err = initialize_vpif();
1992 if (err) {
1993 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
1994 return err;
1995 }
1996
Hans Verkuild2f7a1a2011-12-13 05:44:42 -03001997 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
1998 if (err) {
1999 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
2000 return err;
2001 }
2002
Hans Verkuil5be452c2012-09-20 09:06:29 -03002003 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
Lad, Prabhakara76a0b32013-06-17 11:20:47 -03002004 err = devm_request_irq(&pdev->dev, res->start, vpif_channel_isr,
2005 IRQF_SHARED, "VPIF_Capture",
2006 (void *)(&vpif_obj.dev[res_idx]->
2007 channel_id));
2008 if (err) {
2009 err = -EINVAL;
2010 goto vpif_unregister;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002011 }
Hans Verkuil5be452c2012-09-20 09:06:29 -03002012 res_idx++;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002013 }
2014
2015 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2016 /* Get the pointer to the channel object */
2017 ch = vpif_obj.dev[i];
2018 /* Allocate memory for video device */
2019 vfd = video_device_alloc();
2020 if (NULL == vfd) {
2021 for (j = 0; j < i; j++) {
2022 ch = vpif_obj.dev[j];
2023 video_device_release(ch->video_dev);
2024 }
2025 err = -ENOMEM;
Lad, Prabhakarb2de4f22013-06-17 11:20:46 -03002026 goto vpif_unregister;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002027 }
2028
2029 /* Initialize field of video device */
2030 *vfd = vpif_video_template;
2031 vfd->v4l2_dev = &vpif_obj.v4l2_dev;
2032 vfd->release = video_device_release;
2033 snprintf(vfd->name, sizeof(vfd->name),
Manjunath Hadli0a631722012-04-13 04:44:00 -03002034 "VPIF_Capture_DRIVER_V%s",
Mauro Carvalho Chehab64dc3c12011-06-25 11:28:37 -03002035 VPIF_CAPTURE_VERSION);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002036 /* Set video_dev to the video device */
2037 ch->video_dev = vfd;
2038 }
2039
Manjunath Hadli764af392012-04-13 04:49:34 -03002040 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2041 if (res) {
2042 size = resource_size(res);
2043 /* The resources are divided into two equal memory and when we
2044 * have HD output we can add them together
2045 */
2046 for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
2047 ch = vpif_obj.dev[j];
2048 ch->channel_id = j;
2049 /* only enabled if second resource exists */
2050 config_params.video_limit[ch->channel_id] = 0;
2051 if (size)
2052 config_params.video_limit[ch->channel_id] =
2053 size/2;
2054 }
2055 }
2056
Lad, Prabhakar873229e2013-06-25 11:17:34 -03002057 vpif_obj.config = pdev->dev.platform_data;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002058
Lad, Prabhakar873229e2013-06-25 11:17:34 -03002059 subdev_count = vpif_obj.config->subdev_count;
Mats Randgaard1f8766b2010-08-30 10:30:37 -03002060 vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002061 GFP_KERNEL);
2062 if (vpif_obj.sd == NULL) {
2063 vpif_err("unable to allocate memory for subdevice pointers\n");
2064 err = -ENOMEM;
Hans Verkuil5be452c2012-09-20 09:06:29 -03002065 goto vpif_sd_error;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002066 }
2067
Lad, Prabhakar873229e2013-06-25 11:17:34 -03002068 if (!vpif_obj.config->asd_sizes) {
2069 i2c_adap = i2c_get_adapter(1);
2070 for (i = 0; i < subdev_count; i++) {
2071 subdevdata = &vpif_obj.config->subdev_info[i];
2072 vpif_obj.sd[i] =
2073 v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
2074 i2c_adap,
2075 &subdevdata->
2076 board_info,
2077 NULL);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002078
Lad, Prabhakar873229e2013-06-25 11:17:34 -03002079 if (!vpif_obj.sd[i]) {
2080 vpif_err("Error registering v4l2 subdevice\n");
Wei Yongjun2fcd9dc2013-09-02 05:06:10 -03002081 err = -ENODEV;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03002082 goto probe_subdev_out;
2083 }
2084 v4l2_info(&vpif_obj.v4l2_dev,
2085 "registered sub device %s\n",
2086 subdevdata->name);
2087 }
2088 vpif_probe_complete();
2089 } else {
Sylwester Nawrockie8419d02013-07-19 12:31:10 -03002090 vpif_obj.notifier.subdevs = vpif_obj.config->asd;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03002091 vpif_obj.notifier.num_subdevs = vpif_obj.config->asd_sizes[0];
2092 vpif_obj.notifier.bound = vpif_async_bound;
2093 vpif_obj.notifier.complete = vpif_async_complete;
2094 err = v4l2_async_notifier_register(&vpif_obj.v4l2_dev,
2095 &vpif_obj.notifier);
2096 if (err) {
2097 vpif_err("Error registering async notifier\n");
2098 err = -EINVAL;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002099 goto probe_subdev_out;
2100 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002101 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002102
2103 return 0;
2104
Hans Verkuilb65814e2012-09-20 09:06:26 -03002105probe_subdev_out:
2106 /* free sub devices memory */
2107 kfree(vpif_obj.sd);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002108
Hans Verkuil5be452c2012-09-20 09:06:29 -03002109vpif_sd_error:
2110 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2111 ch = vpif_obj.dev[i];
2112 /* Note: does nothing if ch->video_dev == NULL */
2113 video_device_release(ch->video_dev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002114 }
Lad, Prabhakarb2de4f22013-06-17 11:20:46 -03002115vpif_unregister:
Hans Verkuild2f7a1a2011-12-13 05:44:42 -03002116 v4l2_device_unregister(&vpif_obj.v4l2_dev);
Lad, Prabhakarb2de4f22013-06-17 11:20:46 -03002117
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002118 return err;
2119}
2120
2121/**
2122 * vpif_remove() - driver remove handler
2123 * @device: ptr to platform device structure
2124 *
2125 * The vidoe device is unregistered
2126 */
2127static int vpif_remove(struct platform_device *device)
2128{
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03002129 struct common_obj *common;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002130 struct channel_obj *ch;
Lad, Prabhakarb2de4f22013-06-17 11:20:46 -03002131 int i;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002132
2133 v4l2_device_unregister(&vpif_obj.v4l2_dev);
2134
Lad, Prabhakar410ca602013-06-17 11:20:44 -03002135 kfree(vpif_obj.sd);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002136 /* un-register device */
2137 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2138 /* Get the pointer to the channel object */
2139 ch = vpif_obj.dev[i];
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03002140 common = &ch->common[i];
2141 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002142 /* Unregister video device */
2143 video_unregister_device(ch->video_dev);
Lad, Prabhakar410ca602013-06-17 11:20:44 -03002144 kfree(vpif_obj.dev[i]);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002145 }
2146 return 0;
2147}
2148
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002149#ifdef CONFIG_PM
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002150/**
2151 * vpif_suspend: vpif device suspend
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002152 */
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002153static int vpif_suspend(struct device *dev)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002154{
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002155
2156 struct common_obj *common;
2157 struct channel_obj *ch;
2158 int i;
2159
2160 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2161 /* Get the pointer to the channel object */
2162 ch = vpif_obj.dev[i];
2163 common = &ch->common[VPIF_VIDEO_INDEX];
2164 mutex_lock(&common->lock);
2165 if (ch->usrs && common->io_usrs) {
2166 /* Disable channel */
2167 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
2168 enable_channel0(0);
2169 channel0_intr_enable(0);
2170 }
2171 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
2172 common->started == 2) {
2173 enable_channel1(0);
2174 channel1_intr_enable(0);
2175 }
2176 }
2177 mutex_unlock(&common->lock);
2178 }
2179
2180 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002181}
2182
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002183/*
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002184 * vpif_resume: vpif device suspend
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002185 */
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002186static int vpif_resume(struct device *dev)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002187{
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002188 struct common_obj *common;
2189 struct channel_obj *ch;
2190 int i;
2191
2192 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2193 /* Get the pointer to the channel object */
2194 ch = vpif_obj.dev[i];
2195 common = &ch->common[VPIF_VIDEO_INDEX];
2196 mutex_lock(&common->lock);
2197 if (ch->usrs && common->io_usrs) {
2198 /* Disable channel */
2199 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
2200 enable_channel0(1);
2201 channel0_intr_enable(1);
2202 }
2203 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
2204 common->started == 2) {
2205 enable_channel1(1);
2206 channel1_intr_enable(1);
2207 }
2208 }
2209 mutex_unlock(&common->lock);
2210 }
2211
2212 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002213}
2214
Alexey Dobriyan47145212009-12-14 18:00:08 -08002215static const struct dev_pm_ops vpif_dev_pm_ops = {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002216 .suspend = vpif_suspend,
2217 .resume = vpif_resume,
2218};
2219
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002220#define vpif_pm_ops (&vpif_dev_pm_ops)
2221#else
2222#define vpif_pm_ops NULL
2223#endif
2224
Mats Randgaardffa1b392010-08-30 10:30:36 -03002225static __refdata struct platform_driver vpif_driver = {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002226 .driver = {
2227 .name = "vpif_capture",
2228 .owner = THIS_MODULE,
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002229 .pm = vpif_pm_ops,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002230 },
2231 .probe = vpif_probe,
2232 .remove = vpif_remove,
2233};
2234
Lad, Prabhakarfe424b22013-06-17 11:20:45 -03002235module_platform_driver(vpif_driver);