blob: a50e3927ab650b2f17fe727db0f1687478183a1c [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, Prabhakar41b9f242014-05-16 10:33:39 -0300179/**
180 * vpif_start_streaming : Starts the DMA engine for streaming
181 * @vb: ptr to vb2_buffer
182 * @count: number of buffers
183 */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300184static int vpif_start_streaming(struct vb2_queue *vq, unsigned int count)
185{
186 struct vpif_capture_config *vpif_config_data =
187 vpif_dev->platform_data;
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -0300188 struct channel_obj *ch = vb2_get_drv_priv(vq);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300189 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
190 struct vpif_params *vpif = &ch->vpifparams;
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300191 struct vpif_cap_buffer *buf, *tmp;
192 unsigned long addr, flags;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300193 int ret;
194
Hans Verkuilaec96832012-11-16 12:03:06 -0300195 spin_lock_irqsave(&common->irqlock, flags);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300196
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300197 /* Initialize field_id and started member */
198 ch->field_id = 0;
199 common->started = 1;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300200
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300201 /* configure 1 or 2 channel mode */
Lad, Prabhakarf4ad8d72012-09-27 02:33:12 -0300202 if (vpif_config_data->setup_input_channel_mode) {
203 ret = vpif_config_data->
204 setup_input_channel_mode(vpif->std_info.ycmux_mode);
205 if (ret < 0) {
206 vpif_dbg(1, debug, "can't set vpif channel mode\n");
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300207 goto err;
Lad, Prabhakarf4ad8d72012-09-27 02:33:12 -0300208 }
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300209 }
210
211 /* Call vpif_set_params function to set the parameters and addresses */
212 ret = vpif_set_video_params(vpif, ch->channel_id);
213
214 if (ret < 0) {
215 vpif_dbg(1, debug, "can't set video params\n");
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300216 goto err;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300217 }
218
219 common->started = ret;
220 vpif_config_addr(ch, ret);
221
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300222 /* Get the next frame from the buffer queue */
223 common->cur_frm = common->next_frm = list_entry(common->dma_queue.next,
224 struct vpif_cap_buffer, list);
225 /* Remove buffer from the buffer queue */
226 list_del(&common->cur_frm->list);
227 spin_unlock_irqrestore(&common->irqlock, flags);
228 /* Mark state of the current frame to active */
229 common->cur_frm->vb.state = VB2_BUF_STATE_ACTIVE;
230
231 addr = vb2_dma_contig_plane_dma_addr(&common->cur_frm->vb, 0);
232
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300233 common->set_addr(addr + common->ytop_off,
234 addr + common->ybtm_off,
235 addr + common->ctop_off,
236 addr + common->cbtm_off);
237
238 /**
239 * Set interrupt for both the fields in VPIF Register enable channel in
240 * VPIF register
241 */
Lad, Prabhakar9e184042012-09-14 10:22:24 -0300242 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
Lad, Prabhakar41b9f242014-05-16 10:33:39 -0300243 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300244 channel0_intr_assert();
245 channel0_intr_enable(1);
246 enable_channel0(1);
247 }
Lad, Prabhakar41b9f242014-05-16 10:33:39 -0300248 if (VPIF_CHANNEL1_VIDEO == ch->channel_id ||
249 common->started == 2) {
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300250 channel1_intr_assert();
251 channel1_intr_enable(1);
252 enable_channel1(1);
253 }
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300254
255 return 0;
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300256
257err:
258 list_for_each_entry_safe(buf, tmp, &common->dma_queue, list) {
259 list_del(&buf->list);
260 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_QUEUED);
261 }
262
263 return ret;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300264}
265
Lad, Prabhakar41b9f242014-05-16 10:33:39 -0300266/**
267 * vpif_stop_streaming : Stop the DMA engine
268 * @vq: ptr to vb2_queue
269 *
270 * This callback stops the DMA engine and any remaining buffers
271 * in the DMA queue are released.
272 */
Hans Verkuile37559b2014-04-17 02:47:21 -0300273static void vpif_stop_streaming(struct vb2_queue *vq)
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300274{
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -0300275 struct channel_obj *ch = vb2_get_drv_priv(vq);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300276 struct common_obj *common;
Hans Verkuilaec96832012-11-16 12:03:06 -0300277 unsigned long flags;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300278
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300279 common = &ch->common[VPIF_VIDEO_INDEX];
280
Lad, Prabhakare6ba3db2014-03-22 08:03:07 -0300281 /* Disable channel as per its device type and channel id */
282 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
283 enable_channel0(0);
284 channel0_intr_enable(0);
285 }
Lad, Prabhakar41b9f242014-05-16 10:33:39 -0300286 if (VPIF_CHANNEL1_VIDEO == ch->channel_id ||
287 2 == common->started) {
Lad, Prabhakare6ba3db2014-03-22 08:03:07 -0300288 enable_channel1(0);
289 channel1_intr_enable(0);
290 }
291 common->started = 0;
292
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300293 /* release all active buffers */
Hans Verkuilaec96832012-11-16 12:03:06 -0300294 spin_lock_irqsave(&common->irqlock, flags);
Lad, Prabhakare6ba3db2014-03-22 08:03:07 -0300295 if (common->cur_frm == common->next_frm) {
296 vb2_buffer_done(&common->cur_frm->vb, VB2_BUF_STATE_ERROR);
297 } else {
298 if (common->cur_frm != NULL)
299 vb2_buffer_done(&common->cur_frm->vb,
300 VB2_BUF_STATE_ERROR);
301 if (common->next_frm != NULL)
302 vb2_buffer_done(&common->next_frm->vb,
303 VB2_BUF_STATE_ERROR);
304 }
305
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300306 while (!list_empty(&common->dma_queue)) {
307 common->next_frm = list_entry(common->dma_queue.next,
308 struct vpif_cap_buffer, list);
309 list_del(&common->next_frm->list);
310 vb2_buffer_done(&common->next_frm->vb, VB2_BUF_STATE_ERROR);
311 }
Hans Verkuilaec96832012-11-16 12:03:06 -0300312 spin_unlock_irqrestore(&common->irqlock, flags);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300313}
314
315static struct vb2_ops video_qops = {
316 .queue_setup = vpif_buffer_queue_setup,
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300317 .buf_prepare = vpif_buffer_prepare,
318 .start_streaming = vpif_start_streaming,
319 .stop_streaming = vpif_stop_streaming,
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300320 .buf_queue = vpif_buffer_queue,
321};
322
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300323/**
324 * vpif_process_buffer_complete: process a completed buffer
325 * @common: ptr to common channel object
326 *
327 * This function time stamp the buffer and mark it as DONE. It also
328 * wake up any process waiting on the QUEUE and set the next buffer
329 * as current
330 */
331static void vpif_process_buffer_complete(struct common_obj *common)
332{
Sakari Ailus8e6057b2012-09-15 15:14:42 -0300333 v4l2_get_timestamp(&common->cur_frm->vb.v4l2_buf.timestamp);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300334 vb2_buffer_done(&common->cur_frm->vb,
335 VB2_BUF_STATE_DONE);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300336 /* Make curFrm pointing to nextFrm */
337 common->cur_frm = common->next_frm;
338}
339
340/**
341 * vpif_schedule_next_buffer: set next buffer address for capture
342 * @common : ptr to common channel object
343 *
344 * This function will get next buffer from the dma queue and
345 * set the buffer address in the vpif register for capture.
346 * the buffer is marked active
347 */
348static void vpif_schedule_next_buffer(struct common_obj *common)
349{
350 unsigned long addr = 0;
351
Hans Verkuilaec96832012-11-16 12:03:06 -0300352 spin_lock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300353 common->next_frm = list_entry(common->dma_queue.next,
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300354 struct vpif_cap_buffer, list);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300355 /* Remove that buffer from the buffer queue */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300356 list_del(&common->next_frm->list);
Hans Verkuilaec96832012-11-16 12:03:06 -0300357 spin_unlock(&common->irqlock);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300358 common->next_frm->vb.state = VB2_BUF_STATE_ACTIVE;
359 addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb, 0);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300360
361 /* Set top and bottom field addresses in VPIF registers */
362 common->set_addr(addr + common->ytop_off,
363 addr + common->ybtm_off,
364 addr + common->ctop_off,
365 addr + common->cbtm_off);
366}
367
368/**
369 * vpif_channel_isr : ISR handler for vpif capture
370 * @irq: irq number
371 * @dev_id: dev_id ptr
372 *
373 * It changes status of the captured buffer, takes next buffer from the queue
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300374 * and sets its address in VPIF registers
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300375 */
376static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
377{
378 struct vpif_device *dev = &vpif_obj;
379 struct common_obj *common;
380 struct channel_obj *ch;
381 enum v4l2_field field;
382 int channel_id = 0;
383 int fid = -1, i;
384
385 channel_id = *(int *)(dev_id);
Manjunath Hadlib1fc4232012-04-13 04:43:10 -0300386 if (!vpif_intr_status(channel_id))
387 return IRQ_NONE;
388
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300389 ch = dev->dev[channel_id];
390
391 field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
392
393 for (i = 0; i < VPIF_NUMBER_OF_OBJECTS; i++) {
394 common = &ch->common[i];
395 /* skip If streaming is not started in this channel */
396 if (0 == common->started)
397 continue;
398
399 /* Check the field format */
400 if (1 == ch->vpifparams.std_info.frm_fmt) {
401 /* Progressive mode */
Hans Verkuilaec96832012-11-16 12:03:06 -0300402 spin_lock(&common->irqlock);
403 if (list_empty(&common->dma_queue)) {
404 spin_unlock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300405 continue;
Hans Verkuilaec96832012-11-16 12:03:06 -0300406 }
407 spin_unlock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300408
409 if (!channel_first_int[i][channel_id])
410 vpif_process_buffer_complete(common);
411
412 channel_first_int[i][channel_id] = 0;
413
414 vpif_schedule_next_buffer(common);
415
416
417 channel_first_int[i][channel_id] = 0;
418 } else {
419 /**
420 * Interlaced mode. If it is first interrupt, ignore
421 * it
422 */
423 if (channel_first_int[i][channel_id]) {
424 channel_first_int[i][channel_id] = 0;
425 continue;
426 }
427 if (0 == i) {
428 ch->field_id ^= 1;
429 /* Get field id from VPIF registers */
430 fid = vpif_channel_getfid(ch->channel_id);
431 if (fid != ch->field_id) {
432 /**
433 * If field id does not match stored
434 * field id, make them in sync
435 */
436 if (0 == fid)
437 ch->field_id = fid;
438 return IRQ_HANDLED;
439 }
440 }
441 /* device field id and local field id are in sync */
442 if (0 == fid) {
443 /* this is even field */
444 if (common->cur_frm == common->next_frm)
445 continue;
446
447 /* mark the current buffer as done */
448 vpif_process_buffer_complete(common);
449 } else if (1 == fid) {
450 /* odd field */
Hans Verkuilaec96832012-11-16 12:03:06 -0300451 spin_lock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300452 if (list_empty(&common->dma_queue) ||
Hans Verkuilaec96832012-11-16 12:03:06 -0300453 (common->cur_frm != common->next_frm)) {
454 spin_unlock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300455 continue;
Hans Verkuilaec96832012-11-16 12:03:06 -0300456 }
457 spin_unlock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300458
459 vpif_schedule_next_buffer(common);
460 }
461 }
462 }
463 return IRQ_HANDLED;
464}
465
466/**
467 * vpif_update_std_info() - update standard related info
468 * @ch: ptr to channel object
469 *
470 * For a given standard selected by application, update values
471 * in the device data structures
472 */
473static int vpif_update_std_info(struct channel_obj *ch)
474{
475 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
476 struct vpif_params *vpifparams = &ch->vpifparams;
477 const struct vpif_channel_config_params *config;
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300478 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300479 struct video_obj *vid_ch = &ch->video;
480 int index;
481
482 vpif_dbg(2, debug, "vpif_update_std_info\n");
483
Mats Randgaardaa444402010-12-16 12:17:42 -0300484 for (index = 0; index < vpif_ch_params_count; index++) {
Lad, Prabhakarced9b212013-03-20 01:28:27 -0300485 config = &vpif_ch_params[index];
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300486 if (config->hd_sd == 0) {
487 vpif_dbg(2, debug, "SD format\n");
488 if (config->stdid & vid_ch->stdid) {
489 memcpy(std_info, config, sizeof(*config));
490 break;
491 }
492 } else {
493 vpif_dbg(2, debug, "HD format\n");
Hans Verkuil0598c172012-09-18 07:18:47 -0300494 if (!memcmp(&config->dv_timings, &vid_ch->dv_timings,
495 sizeof(vid_ch->dv_timings))) {
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300496 memcpy(std_info, config, sizeof(*config));
497 break;
498 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300499 }
500 }
501
502 /* standard not found */
Mats Randgaardaa444402010-12-16 12:17:42 -0300503 if (index == vpif_ch_params_count)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300504 return -EINVAL;
505
506 common->fmt.fmt.pix.width = std_info->width;
507 common->width = std_info->width;
508 common->fmt.fmt.pix.height = std_info->height;
509 common->height = std_info->height;
510 common->fmt.fmt.pix.bytesperline = std_info->width;
511 vpifparams->video_params.hpitch = std_info->width;
512 vpifparams->video_params.storage_mode = std_info->frm_fmt;
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300513
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300514 return 0;
515}
516
517/**
518 * vpif_calculate_offsets : This function calculates buffers offsets
519 * @ch : ptr to channel object
520 *
521 * This function calculates buffer offsets for Y and C in the top and
522 * bottom field
523 */
524static void vpif_calculate_offsets(struct channel_obj *ch)
525{
526 unsigned int hpitch, vpitch, sizeimage;
527 struct video_obj *vid_ch = &(ch->video);
528 struct vpif_params *vpifparams = &ch->vpifparams;
529 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
530 enum v4l2_field field = common->fmt.fmt.pix.field;
531
532 vpif_dbg(2, debug, "vpif_calculate_offsets\n");
533
534 if (V4L2_FIELD_ANY == field) {
535 if (vpifparams->std_info.frm_fmt)
536 vid_ch->buf_field = V4L2_FIELD_NONE;
537 else
538 vid_ch->buf_field = V4L2_FIELD_INTERLACED;
539 } else
540 vid_ch->buf_field = common->fmt.fmt.pix.field;
541
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300542 sizeimage = common->fmt.fmt.pix.sizeimage;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300543
544 hpitch = common->fmt.fmt.pix.bytesperline;
545 vpitch = sizeimage / (hpitch * 2);
546
547 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
548 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
549 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
550 common->ytop_off = 0;
551 common->ybtm_off = hpitch;
552 common->ctop_off = sizeimage / 2;
553 common->cbtm_off = sizeimage / 2 + hpitch;
554 } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
555 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
556 common->ytop_off = 0;
557 common->ybtm_off = sizeimage / 4;
558 common->ctop_off = sizeimage / 2;
559 common->cbtm_off = common->ctop_off + sizeimage / 4;
560 } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
561 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
562 common->ybtm_off = 0;
563 common->ytop_off = sizeimage / 4;
564 common->cbtm_off = sizeimage / 2;
565 common->ctop_off = common->cbtm_off + sizeimage / 4;
566 }
567 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
568 (V4L2_FIELD_INTERLACED == vid_ch->buf_field))
569 vpifparams->video_params.storage_mode = 1;
570 else
571 vpifparams->video_params.storage_mode = 0;
572
573 if (1 == vpifparams->std_info.frm_fmt)
574 vpifparams->video_params.hpitch =
575 common->fmt.fmt.pix.bytesperline;
576 else {
577 if ((field == V4L2_FIELD_ANY)
578 || (field == V4L2_FIELD_INTERLACED))
579 vpifparams->video_params.hpitch =
580 common->fmt.fmt.pix.bytesperline * 2;
581 else
582 vpifparams->video_params.hpitch =
583 common->fmt.fmt.pix.bytesperline;
584 }
585
586 ch->vpifparams.video_params.stdid = vpifparams->std_info.stdid;
587}
588
589/**
590 * vpif_config_format: configure default frame format in the device
591 * ch : ptr to channel object
592 */
593static void vpif_config_format(struct channel_obj *ch)
594{
595 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
596
597 vpif_dbg(2, debug, "vpif_config_format\n");
598
599 common->fmt.fmt.pix.field = V4L2_FIELD_ANY;
600 if (config_params.numbuffers[ch->channel_id] == 0)
601 common->memory = V4L2_MEMORY_USERPTR;
602 else
603 common->memory = V4L2_MEMORY_MMAP;
604
605 common->fmt.fmt.pix.sizeimage
606 = config_params.channel_bufsize[ch->channel_id];
607
608 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER)
609 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;
610 else
611 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
612 common->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
613}
614
615/**
616 * vpif_get_default_field() - Get default field type based on interface
617 * @vpif_params - ptr to vpif params
618 */
619static inline enum v4l2_field vpif_get_default_field(
620 struct vpif_interface *iface)
621{
622 return (iface->if_type == VPIF_IF_RAW_BAYER) ? V4L2_FIELD_NONE :
623 V4L2_FIELD_INTERLACED;
624}
625
626/**
627 * vpif_check_format() - check given pixel format for compatibility
628 * @ch - channel ptr
629 * @pixfmt - Given pixel format
630 * @update - update the values as per hardware requirement
631 *
632 * Check the application pixel format for S_FMT and update the input
633 * values as per hardware limits for TRY_FMT. The default pixel and
634 * field format is selected based on interface type.
635 */
636static int vpif_check_format(struct channel_obj *ch,
637 struct v4l2_pix_format *pixfmt,
638 int update)
639{
640 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
641 struct vpif_params *vpif_params = &ch->vpifparams;
642 enum v4l2_field field = pixfmt->field;
643 u32 sizeimage, hpitch, vpitch;
644 int ret = -EINVAL;
645
646 vpif_dbg(2, debug, "vpif_check_format\n");
647 /**
648 * first check for the pixel format. If if_type is Raw bayer,
649 * only V4L2_PIX_FMT_SBGGR8 format is supported. Otherwise only
650 * V4L2_PIX_FMT_YUV422P is supported
651 */
652 if (vpif_params->iface.if_type == VPIF_IF_RAW_BAYER) {
653 if (pixfmt->pixelformat != V4L2_PIX_FMT_SBGGR8) {
654 if (!update) {
655 vpif_dbg(2, debug, "invalid pix format\n");
656 goto exit;
657 }
658 pixfmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
659 }
660 } else {
661 if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P) {
662 if (!update) {
663 vpif_dbg(2, debug, "invalid pixel format\n");
664 goto exit;
665 }
666 pixfmt->pixelformat = V4L2_PIX_FMT_YUV422P;
667 }
668 }
669
670 if (!(VPIF_VALID_FIELD(field))) {
671 if (!update) {
672 vpif_dbg(2, debug, "invalid field format\n");
673 goto exit;
674 }
675 /**
676 * By default use FIELD_NONE for RAW Bayer capture
677 * and FIELD_INTERLACED for other interfaces
678 */
679 field = vpif_get_default_field(&vpif_params->iface);
680 } else if (field == V4L2_FIELD_ANY)
681 /* unsupported field. Use default */
682 field = vpif_get_default_field(&vpif_params->iface);
683
684 /* validate the hpitch */
685 hpitch = pixfmt->bytesperline;
686 if (hpitch < vpif_params->std_info.width) {
687 if (!update) {
688 vpif_dbg(2, debug, "invalid hpitch\n");
689 goto exit;
690 }
691 hpitch = vpif_params->std_info.width;
692 }
693
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300694 sizeimage = pixfmt->sizeimage;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300695
696 vpitch = sizeimage / (hpitch * 2);
697
698 /* validate the vpitch */
699 if (vpitch < vpif_params->std_info.height) {
700 if (!update) {
701 vpif_dbg(2, debug, "Invalid vpitch\n");
702 goto exit;
703 }
704 vpitch = vpif_params->std_info.height;
705 }
706
707 /* Check for 8 byte alignment */
708 if (!ALIGN(hpitch, 8)) {
709 if (!update) {
710 vpif_dbg(2, debug, "invalid pitch alignment\n");
711 goto exit;
712 }
713 /* adjust to next 8 byte boundary */
714 hpitch = (((hpitch + 7) / 8) * 8);
715 }
716 /* if update is set, modify the bytesperline and sizeimage */
717 if (update) {
718 pixfmt->bytesperline = hpitch;
719 pixfmt->sizeimage = hpitch * vpitch * 2;
720 }
721 /**
722 * Image width and height is always based on current standard width and
723 * height
724 */
725 pixfmt->width = common->fmt.fmt.pix.width;
726 pixfmt->height = common->fmt.fmt.pix.height;
727 return 0;
728exit:
729 return ret;
730}
731
732/**
733 * vpif_config_addr() - function to configure buffer address in vpif
734 * @ch - channel ptr
735 * @muxmode - channel mux mode
736 */
737static void vpif_config_addr(struct channel_obj *ch, int muxmode)
738{
739 struct common_obj *common;
740
741 vpif_dbg(2, debug, "vpif_config_addr\n");
742
743 common = &(ch->common[VPIF_VIDEO_INDEX]);
744
745 if (VPIF_CHANNEL1_VIDEO == ch->channel_id)
746 common->set_addr = ch1_set_videobuf_addr;
747 else if (2 == muxmode)
748 common->set_addr = ch0_set_videobuf_addr_yc_nmux;
749 else
750 common->set_addr = ch0_set_videobuf_addr;
751}
752
753/**
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300754 * vpif_open : vpif open handler
755 * @filep: file ptr
756 *
757 * It creates object of file handle structure and stores it in private_data
758 * member of filepointer
759 */
760static int vpif_open(struct file *filep)
761{
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300762 struct video_device *vdev = video_devdata(filep);
763 struct common_obj *common;
764 struct video_obj *vid_ch;
765 struct channel_obj *ch;
766 struct vpif_fh *fh;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300767
768 vpif_dbg(2, debug, "vpif_open\n");
769
770 ch = video_get_drvdata(vdev);
771
772 vid_ch = &ch->video;
773 common = &ch->common[VPIF_VIDEO_INDEX];
774
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300775 /* Allocate memory for the file handle object */
Mats Randgaard1f8766b2010-08-30 10:30:37 -0300776 fh = kzalloc(sizeof(struct vpif_fh), GFP_KERNEL);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300777 if (NULL == fh) {
778 vpif_err("unable to allocate memory for file handle object\n");
Hans Verkuil46656af2011-01-04 06:51:35 -0300779 return -ENOMEM;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300780 }
781
Hans Verkuil72246792012-07-31 03:48:31 -0300782 if (mutex_lock_interruptible(&common->lock)) {
783 kfree(fh);
784 return -ERESTARTSYS;
785 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300786 /* store pointer to fh in private_data member of filep */
787 filep->private_data = fh;
788 fh->channel = ch;
789 fh->initialized = 0;
790 /* If decoder is not initialized. initialize it */
791 if (!ch->initialized) {
792 fh->initialized = 1;
793 ch->initialized = 1;
794 memset(&(ch->vpifparams), 0, sizeof(struct vpif_params));
795 }
796 /* Increment channel usrs counter */
797 ch->usrs++;
798 /* Set io_allowed member to false */
799 fh->io_allowed[VPIF_VIDEO_INDEX] = 0;
800 /* Initialize priority of this instance to default priority */
801 fh->prio = V4L2_PRIORITY_UNSET;
802 v4l2_prio_open(&ch->prio, &fh->prio);
Hans Verkuil72246792012-07-31 03:48:31 -0300803 mutex_unlock(&common->lock);
Hans Verkuil46656af2011-01-04 06:51:35 -0300804 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300805}
806
807/**
808 * vpif_release : function to clean up file close
809 * @filep: file pointer
810 *
Dror Cohen6f45b1b2012-07-10 05:43:22 -0300811 * This function deletes buffer queue, frees the buffers and the vpif file
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300812 * handle
813 */
814static int vpif_release(struct file *filep)
815{
816 struct vpif_fh *fh = filep->private_data;
817 struct channel_obj *ch = fh->channel;
818 struct common_obj *common;
819
820 vpif_dbg(2, debug, "vpif_release\n");
821
822 common = &ch->common[VPIF_VIDEO_INDEX];
823
Hans Verkuil72246792012-07-31 03:48:31 -0300824 mutex_lock(&common->lock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300825 /* if this instance is doing IO */
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -0300826 if (fh->io_allowed[VPIF_VIDEO_INDEX])
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300827 /* Reset io_usrs member of channel object */
828 common->io_usrs = 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300829
830 /* Decrement channel usrs counter */
831 ch->usrs--;
832
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300833 /* Close the priority */
Hans Verkuilffb48772010-05-01 08:03:24 -0300834 v4l2_prio_close(&ch->prio, fh->prio);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300835
836 if (fh->initialized)
837 ch->initialized = 0;
838
Hans Verkuil72246792012-07-31 03:48:31 -0300839 mutex_unlock(&common->lock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300840 filep->private_data = NULL;
841 kfree(fh);
842 return 0;
843}
844
845/**
846 * vpif_reqbufs() - request buffer handler
847 * @file: file ptr
848 * @priv: file handle
849 * @reqbuf: request buffer structure ptr
850 */
851static int vpif_reqbufs(struct file *file, void *priv,
852 struct v4l2_requestbuffers *reqbuf)
853{
854 struct vpif_fh *fh = priv;
855 struct channel_obj *ch = fh->channel;
856 struct common_obj *common;
857 u8 index = 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300858
859 vpif_dbg(2, debug, "vpif_reqbufs\n");
860
861 /**
862 * This file handle has not initialized the channel,
863 * It is not allowed to do settings
864 */
865 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id)
866 || (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
867 if (!fh->initialized) {
868 vpif_dbg(1, debug, "Channel Busy\n");
869 return -EBUSY;
870 }
871 }
872
Manjunath Hadli764af392012-04-13 04:49:34 -0300873 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != reqbuf->type || !vpif_dev)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300874 return -EINVAL;
875
876 index = VPIF_VIDEO_INDEX;
877
878 common = &ch->common[index];
879
Hans Verkuil46656af2011-01-04 06:51:35 -0300880 if (0 != common->io_usrs)
881 return -EBUSY;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300882
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300883 /* Set io allowed member of file handle to TRUE */
884 fh->io_allowed[index] = 1;
885 /* Increment io usrs member of channel object to 1 */
886 common->io_usrs = 1;
887 /* Store type of memory requested in channel object */
888 common->memory = reqbuf->memory;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300889
890 /* Allocate buffers */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300891 return vb2_reqbufs(&common->buffer_queue, reqbuf);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300892}
893
894/**
895 * vpif_querybuf() - query buffer handler
896 * @file: file ptr
897 * @priv: file handle
898 * @buf: v4l2 buffer structure ptr
899 */
900static int vpif_querybuf(struct file *file, void *priv,
901 struct v4l2_buffer *buf)
902{
903 struct vpif_fh *fh = priv;
904 struct channel_obj *ch = fh->channel;
905 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
906
907 vpif_dbg(2, debug, "vpif_querybuf\n");
908
909 if (common->fmt.type != buf->type)
910 return -EINVAL;
911
912 if (common->memory != V4L2_MEMORY_MMAP) {
913 vpif_dbg(1, debug, "Invalid memory\n");
914 return -EINVAL;
915 }
916
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300917 return vb2_querybuf(&common->buffer_queue, buf);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300918}
919
920/**
921 * vpif_qbuf() - query buffer handler
922 * @file: file ptr
923 * @priv: file handle
924 * @buf: v4l2 buffer structure ptr
925 */
926static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
927{
928
929 struct vpif_fh *fh = priv;
930 struct channel_obj *ch = fh->channel;
931 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
932 struct v4l2_buffer tbuf = *buf;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300933
934 vpif_dbg(2, debug, "vpif_qbuf\n");
935
936 if (common->fmt.type != tbuf.type) {
937 vpif_err("invalid buffer type\n");
938 return -EINVAL;
939 }
940
941 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300942 vpif_err("fh io not allowed\n");
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300943 return -EACCES;
944 }
945
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300946 return vb2_qbuf(&common->buffer_queue, buf);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300947}
948
949/**
950 * vpif_dqbuf() - query buffer handler
951 * @file: file ptr
952 * @priv: file handle
953 * @buf: v4l2 buffer structure ptr
954 */
955static int vpif_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
956{
957 struct vpif_fh *fh = priv;
958 struct channel_obj *ch = fh->channel;
959 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
960
961 vpif_dbg(2, debug, "vpif_dqbuf\n");
962
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300963 return vb2_dqbuf(&common->buffer_queue, buf,
964 (file->f_flags & O_NONBLOCK));
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300965}
966
967/**
968 * vpif_streamon() - streamon handler
969 * @file: file ptr
970 * @priv: file handle
971 * @buftype: v4l2 buffer type
972 */
973static int vpif_streamon(struct file *file, void *priv,
974 enum v4l2_buf_type buftype)
975{
976
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300977 struct vpif_fh *fh = priv;
978 struct channel_obj *ch = fh->channel;
979 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
980 struct channel_obj *oth_ch = vpif_obj.dev[!ch->channel_id];
981 struct vpif_params *vpif;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300982 int ret = 0;
983
984 vpif_dbg(2, debug, "vpif_streamon\n");
985
986 vpif = &ch->vpifparams;
987
988 if (buftype != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
989 vpif_dbg(1, debug, "buffer type not supported\n");
990 return -EINVAL;
991 }
992
993 /* If file handle is not allowed IO, return error */
994 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
995 vpif_dbg(1, debug, "io not allowed\n");
996 return -EACCES;
997 }
998
999 /* If Streaming is already started, return error */
1000 if (common->started) {
1001 vpif_dbg(1, debug, "channel->started\n");
1002 return -EBUSY;
1003 }
1004
1005 if ((ch->channel_id == VPIF_CHANNEL0_VIDEO &&
1006 oth_ch->common[VPIF_VIDEO_INDEX].started &&
1007 vpif->std_info.ycmux_mode == 0) ||
1008 ((ch->channel_id == VPIF_CHANNEL1_VIDEO) &&
1009 (2 == oth_ch->common[VPIF_VIDEO_INDEX].started))) {
1010 vpif_dbg(1, debug, "other channel is being used\n");
1011 return -EBUSY;
1012 }
1013
1014 ret = vpif_check_format(ch, &common->fmt.fmt.pix, 0);
1015 if (ret)
1016 return ret;
1017
1018 /* Enable streamon on the sub device */
Hans Verkuil178cce12012-09-20 09:06:30 -03001019 ret = v4l2_subdev_call(ch->sd, video, s_stream, 1);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001020
Hans Verkuil178cce12012-09-20 09:06:30 -03001021 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001022 vpif_dbg(1, debug, "stream on failed in subdev\n");
1023 return ret;
1024 }
1025
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001026 /* Call vb2_streamon to start streaming in videobuf2 */
1027 ret = vb2_streamon(&common->buffer_queue, buftype);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001028 if (ret) {
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001029 vpif_dbg(1, debug, "vb2_streamon\n");
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001030 return ret;
1031 }
1032
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001033 return ret;
1034}
1035
1036/**
1037 * vpif_streamoff() - streamoff handler
1038 * @file: file ptr
1039 * @priv: file handle
1040 * @buftype: v4l2 buffer type
1041 */
1042static int vpif_streamoff(struct file *file, void *priv,
1043 enum v4l2_buf_type buftype)
1044{
1045
1046 struct vpif_fh *fh = priv;
1047 struct channel_obj *ch = fh->channel;
1048 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1049 int ret;
1050
1051 vpif_dbg(2, debug, "vpif_streamoff\n");
1052
1053 if (buftype != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1054 vpif_dbg(1, debug, "buffer type not supported\n");
1055 return -EINVAL;
1056 }
1057
1058 /* If io is allowed for this file handle, return error */
1059 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1060 vpif_dbg(1, debug, "io not allowed\n");
1061 return -EACCES;
1062 }
1063
1064 /* If streaming is not started, return error */
1065 if (!common->started) {
1066 vpif_dbg(1, debug, "channel->started\n");
1067 return -EINVAL;
1068 }
1069
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001070 /* disable channel */
1071 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
1072 enable_channel0(0);
1073 channel0_intr_enable(0);
1074 } else {
1075 enable_channel1(0);
1076 channel1_intr_enable(0);
1077 }
1078
1079 common->started = 0;
1080
Hans Verkuil178cce12012-09-20 09:06:30 -03001081 ret = v4l2_subdev_call(ch->sd, video, s_stream, 0);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001082
Hans Verkuil178cce12012-09-20 09:06:30 -03001083 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001084 vpif_dbg(1, debug, "stream off failed in subdev\n");
1085
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -03001086 return vb2_streamoff(&common->buffer_queue, buftype);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001087}
1088
1089/**
Hans Verkuil178cce12012-09-20 09:06:30 -03001090 * vpif_input_to_subdev() - Maps input to sub device
1091 * @vpif_cfg - global config ptr
1092 * @chan_cfg - channel config ptr
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001093 * @input_index - Given input index from application
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001094 *
1095 * lookup the sub device information for a given input index.
1096 * we report all the inputs to application. inputs table also
1097 * has sub device name for the each input
1098 */
Hans Verkuil178cce12012-09-20 09:06:30 -03001099static int vpif_input_to_subdev(
1100 struct vpif_capture_config *vpif_cfg,
1101 struct vpif_capture_chan_config *chan_cfg,
1102 int input_index)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001103{
Hans Verkuil178cce12012-09-20 09:06:30 -03001104 struct vpif_subdev_info *subdev_info;
1105 const char *subdev_name;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001106 int i;
1107
Hans Verkuil178cce12012-09-20 09:06:30 -03001108 vpif_dbg(2, debug, "vpif_input_to_subdev\n");
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001109
Hans Verkuil178cce12012-09-20 09:06:30 -03001110 subdev_name = chan_cfg->inputs[input_index].subdev_name;
1111 if (subdev_name == NULL)
1112 return -1;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001113
1114 /* loop through the sub device list to get the sub device info */
1115 for (i = 0; i < vpif_cfg->subdev_count; i++) {
1116 subdev_info = &vpif_cfg->subdev_info[i];
1117 if (!strcmp(subdev_info->name, subdev_name))
Hans Verkuil178cce12012-09-20 09:06:30 -03001118 return i;
1119 }
1120 return -1;
1121}
1122
1123/**
1124 * vpif_set_input() - Select an input
1125 * @vpif_cfg - global config ptr
1126 * @ch - channel
1127 * @_index - Given input index from application
1128 *
1129 * Select the given input.
1130 */
1131static int vpif_set_input(
1132 struct vpif_capture_config *vpif_cfg,
1133 struct channel_obj *ch,
1134 int index)
1135{
1136 struct vpif_capture_chan_config *chan_cfg =
1137 &vpif_cfg->chan_config[ch->channel_id];
1138 struct vpif_subdev_info *subdev_info = NULL;
1139 struct v4l2_subdev *sd = NULL;
1140 u32 input = 0, output = 0;
1141 int sd_index;
1142 int ret;
1143
1144 sd_index = vpif_input_to_subdev(vpif_cfg, chan_cfg, index);
1145 if (sd_index >= 0) {
1146 sd = vpif_obj.sd[sd_index];
1147 subdev_info = &vpif_cfg->subdev_info[sd_index];
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001148 }
1149
Hans Verkuil178cce12012-09-20 09:06:30 -03001150 /* first setup input path from sub device to vpif */
1151 if (sd && vpif_cfg->setup_input_path) {
1152 ret = vpif_cfg->setup_input_path(ch->channel_id,
1153 subdev_info->name);
1154 if (ret < 0) {
1155 vpif_dbg(1, debug, "couldn't setup input path for the" \
1156 " sub device %s, for input index %d\n",
1157 subdev_info->name, index);
1158 return ret;
1159 }
1160 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001161
Hans Verkuil178cce12012-09-20 09:06:30 -03001162 if (sd) {
1163 input = chan_cfg->inputs[index].input_route;
1164 output = chan_cfg->inputs[index].output_route;
1165 ret = v4l2_subdev_call(sd, video, s_routing,
1166 input, output, 0);
1167 if (ret < 0 && ret != -ENOIOCTLCMD) {
1168 vpif_dbg(1, debug, "Failed to set input\n");
1169 return ret;
1170 }
1171 }
1172 ch->input_idx = index;
1173 ch->sd = sd;
1174 /* copy interface parameters to vpif */
Hans Verkuil0d4f35f2012-09-20 09:06:32 -03001175 ch->vpifparams.iface = chan_cfg->vpif_if;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001176
Hans Verkuil178cce12012-09-20 09:06:30 -03001177 /* update tvnorms from the sub device input info */
1178 ch->video_dev->tvnorms = chan_cfg->inputs[index].input.std;
1179 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001180}
1181
1182/**
1183 * vpif_querystd() - querystd handler
1184 * @file: file ptr
1185 * @priv: file handle
1186 * @std_id: ptr to std id
1187 *
1188 * This function is called to detect standard at the selected input
1189 */
1190static int vpif_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
1191{
1192 struct vpif_fh *fh = priv;
1193 struct channel_obj *ch = fh->channel;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001194 int ret = 0;
1195
1196 vpif_dbg(2, debug, "vpif_querystd\n");
1197
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001198 /* Call querystd function of decoder device */
Hans Verkuil178cce12012-09-20 09:06:30 -03001199 ret = v4l2_subdev_call(ch->sd, video, querystd, std_id);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001200
Hans Verkuil178cce12012-09-20 09:06:30 -03001201 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1202 return -ENODATA;
1203 if (ret) {
1204 vpif_dbg(1, debug, "Failed to query standard for sub devices\n");
1205 return ret;
1206 }
1207
1208 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001209}
1210
1211/**
1212 * vpif_g_std() - get STD handler
1213 * @file: file ptr
1214 * @priv: file handle
1215 * @std_id: ptr to std id
1216 */
1217static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
1218{
1219 struct vpif_fh *fh = priv;
1220 struct channel_obj *ch = fh->channel;
1221
1222 vpif_dbg(2, debug, "vpif_g_std\n");
1223
1224 *std = ch->video.stdid;
1225 return 0;
1226}
1227
1228/**
1229 * vpif_s_std() - set STD handler
1230 * @file: file ptr
1231 * @priv: file handle
1232 * @std_id: ptr to std id
1233 */
Hans Verkuil314527a2013-03-15 06:10:40 -03001234static int vpif_s_std(struct file *file, void *priv, v4l2_std_id std_id)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001235{
1236 struct vpif_fh *fh = priv;
1237 struct channel_obj *ch = fh->channel;
1238 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1239 int ret = 0;
1240
1241 vpif_dbg(2, debug, "vpif_s_std\n");
1242
1243 if (common->started) {
1244 vpif_err("streaming in progress\n");
1245 return -EBUSY;
1246 }
1247
1248 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1249 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1250 if (!fh->initialized) {
1251 vpif_dbg(1, debug, "Channel Busy\n");
1252 return -EBUSY;
1253 }
1254 }
1255
Hans Verkuilffb48772010-05-01 08:03:24 -03001256 ret = v4l2_prio_check(&ch->prio, fh->prio);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001257 if (0 != ret)
1258 return ret;
1259
1260 fh->initialized = 1;
1261
1262 /* Call encoder subdevice function to set the standard */
Hans Verkuil314527a2013-03-15 06:10:40 -03001263 ch->video.stdid = std_id;
Hans Verkuil0598c172012-09-18 07:18:47 -03001264 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001265
1266 /* Get the information about the standard */
1267 if (vpif_update_std_info(ch)) {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001268 vpif_err("Error getting the standard info\n");
Hans Verkuil46656af2011-01-04 06:51:35 -03001269 return -EINVAL;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001270 }
1271
1272 /* Configure the default format information */
1273 vpif_config_format(ch);
1274
1275 /* set standard in the sub device */
Hans Verkuil314527a2013-03-15 06:10:40 -03001276 ret = v4l2_subdev_call(ch->sd, core, s_std, std_id);
Hans Verkuil178cce12012-09-20 09:06:30 -03001277 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001278 vpif_dbg(1, debug, "Failed to set standard for sub devices\n");
Hans Verkuil178cce12012-09-20 09:06:30 -03001279 return ret;
1280 }
1281 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001282}
1283
1284/**
1285 * vpif_enum_input() - ENUMINPUT handler
1286 * @file: file ptr
1287 * @priv: file handle
1288 * @input: ptr to input structure
1289 */
1290static int vpif_enum_input(struct file *file, void *priv,
1291 struct v4l2_input *input)
1292{
1293
1294 struct vpif_capture_config *config = vpif_dev->platform_data;
1295 struct vpif_capture_chan_config *chan_cfg;
1296 struct vpif_fh *fh = priv;
1297 struct channel_obj *ch = fh->channel;
1298
1299 chan_cfg = &config->chan_config[ch->channel_id];
1300
1301 if (input->index >= chan_cfg->input_count) {
1302 vpif_dbg(1, debug, "Invalid input index\n");
1303 return -EINVAL;
1304 }
1305
1306 memcpy(input, &chan_cfg->inputs[input->index].input,
1307 sizeof(*input));
1308 return 0;
1309}
1310
1311/**
1312 * vpif_g_input() - Get INPUT handler
1313 * @file: file ptr
1314 * @priv: file handle
1315 * @index: ptr to input index
1316 */
1317static int vpif_g_input(struct file *file, void *priv, unsigned int *index)
1318{
1319 struct vpif_fh *fh = priv;
1320 struct channel_obj *ch = fh->channel;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001321
Hans Verkuil6f47c6c2012-09-20 09:06:22 -03001322 *index = ch->input_idx;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001323 return 0;
1324}
1325
1326/**
1327 * vpif_s_input() - Set INPUT handler
1328 * @file: file ptr
1329 * @priv: file handle
1330 * @index: input index
1331 */
1332static int vpif_s_input(struct file *file, void *priv, unsigned int index)
1333{
1334 struct vpif_capture_config *config = vpif_dev->platform_data;
1335 struct vpif_capture_chan_config *chan_cfg;
1336 struct vpif_fh *fh = priv;
1337 struct channel_obj *ch = fh->channel;
1338 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
Hans Verkuil178cce12012-09-20 09:06:30 -03001339 int ret;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001340
1341 chan_cfg = &config->chan_config[ch->channel_id];
1342
Hans Verkuil7aaad132012-09-20 09:06:25 -03001343 if (index >= chan_cfg->input_count)
1344 return -EINVAL;
1345
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001346 if (common->started) {
1347 vpif_err("Streaming in progress\n");
1348 return -EBUSY;
1349 }
1350
1351 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1352 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1353 if (!fh->initialized) {
1354 vpif_dbg(1, debug, "Channel Busy\n");
1355 return -EBUSY;
1356 }
1357 }
1358
Hans Verkuilffb48772010-05-01 08:03:24 -03001359 ret = v4l2_prio_check(&ch->prio, fh->prio);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001360 if (0 != ret)
1361 return ret;
1362
1363 fh->initialized = 1;
Hans Verkuil178cce12012-09-20 09:06:30 -03001364 return vpif_set_input(config, ch, index);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001365}
1366
1367/**
1368 * vpif_enum_fmt_vid_cap() - ENUM_FMT handler
1369 * @file: file ptr
1370 * @priv: file handle
1371 * @index: input index
1372 */
1373static int vpif_enum_fmt_vid_cap(struct file *file, void *priv,
1374 struct v4l2_fmtdesc *fmt)
1375{
1376 struct vpif_fh *fh = priv;
1377 struct channel_obj *ch = fh->channel;
1378
1379 if (fmt->index != 0) {
1380 vpif_dbg(1, debug, "Invalid format index\n");
1381 return -EINVAL;
1382 }
1383
1384 /* Fill in the information about format */
1385 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER) {
1386 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1387 strcpy(fmt->description, "Raw Mode -Bayer Pattern GrRBGb");
1388 fmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
1389 } else {
1390 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1391 strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
1392 fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
1393 }
1394 return 0;
1395}
1396
1397/**
1398 * vpif_try_fmt_vid_cap() - TRY_FMT handler
1399 * @file: file ptr
1400 * @priv: file handle
1401 * @fmt: ptr to v4l2 format structure
1402 */
1403static int vpif_try_fmt_vid_cap(struct file *file, void *priv,
1404 struct v4l2_format *fmt)
1405{
1406 struct vpif_fh *fh = priv;
1407 struct channel_obj *ch = fh->channel;
1408 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
1409
1410 return vpif_check_format(ch, pixfmt, 1);
1411}
1412
1413
1414/**
1415 * vpif_g_fmt_vid_cap() - Set INPUT handler
1416 * @file: file ptr
1417 * @priv: file handle
1418 * @fmt: ptr to v4l2 format structure
1419 */
1420static int vpif_g_fmt_vid_cap(struct file *file, void *priv,
1421 struct v4l2_format *fmt)
1422{
1423 struct vpif_fh *fh = priv;
1424 struct channel_obj *ch = fh->channel;
1425 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1426
1427 /* Check the validity of the buffer type */
1428 if (common->fmt.type != fmt->type)
1429 return -EINVAL;
1430
1431 /* Fill in the information about format */
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001432 *fmt = common->fmt;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001433 return 0;
1434}
1435
1436/**
1437 * vpif_s_fmt_vid_cap() - Set FMT handler
1438 * @file: file ptr
1439 * @priv: file handle
1440 * @fmt: ptr to v4l2 format structure
1441 */
1442static int vpif_s_fmt_vid_cap(struct file *file, void *priv,
1443 struct v4l2_format *fmt)
1444{
1445 struct vpif_fh *fh = priv;
1446 struct channel_obj *ch = fh->channel;
1447 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1448 struct v4l2_pix_format *pixfmt;
1449 int ret = 0;
1450
Mats Randgaard2c0ddd12010-12-16 12:17:45 -03001451 vpif_dbg(2, debug, "%s\n", __func__);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001452
1453 /* If streaming is started, return error */
1454 if (common->started) {
1455 vpif_dbg(1, debug, "Streaming is started\n");
1456 return -EBUSY;
1457 }
1458
1459 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1460 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1461 if (!fh->initialized) {
1462 vpif_dbg(1, debug, "Channel Busy\n");
1463 return -EBUSY;
1464 }
1465 }
1466
Hans Verkuilffb48772010-05-01 08:03:24 -03001467 ret = v4l2_prio_check(&ch->prio, fh->prio);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001468 if (0 != ret)
1469 return ret;
1470
1471 fh->initialized = 1;
1472
1473 pixfmt = &fmt->fmt.pix;
1474 /* Check for valid field format */
1475 ret = vpif_check_format(ch, pixfmt, 0);
1476
1477 if (ret)
1478 return ret;
1479 /* store the format in the channel object */
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001480 common->fmt = *fmt;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001481 return 0;
1482}
1483
1484/**
1485 * vpif_querycap() - QUERYCAP handler
1486 * @file: file ptr
1487 * @priv: file handle
1488 * @cap: ptr to v4l2_capability structure
1489 */
1490static int vpif_querycap(struct file *file, void *priv,
1491 struct v4l2_capability *cap)
1492{
1493 struct vpif_capture_config *config = vpif_dev->platform_data;
1494
Lad, Prabhakar626d533f2012-09-25 11:21:55 -03001495 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1496 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1497 snprintf(cap->driver, sizeof(cap->driver), "%s", dev_name(vpif_dev));
1498 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
1499 dev_name(vpif_dev));
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001500 strlcpy(cap->card, config->card_name, sizeof(cap->card));
1501
1502 return 0;
1503}
1504
1505/**
1506 * vpif_g_priority() - get priority handler
1507 * @file: file ptr
1508 * @priv: file handle
1509 * @prio: ptr to v4l2_priority structure
1510 */
1511static int vpif_g_priority(struct file *file, void *priv,
1512 enum v4l2_priority *prio)
1513{
1514 struct vpif_fh *fh = priv;
1515 struct channel_obj *ch = fh->channel;
1516
1517 *prio = v4l2_prio_max(&ch->prio);
1518
1519 return 0;
1520}
1521
1522/**
1523 * vpif_s_priority() - set priority handler
1524 * @file: file ptr
1525 * @priv: file handle
1526 * @prio: ptr to v4l2_priority structure
1527 */
1528static int vpif_s_priority(struct file *file, void *priv, enum v4l2_priority p)
1529{
1530 struct vpif_fh *fh = priv;
1531 struct channel_obj *ch = fh->channel;
1532
1533 return v4l2_prio_change(&ch->prio, &fh->prio, p);
1534}
1535
1536/**
1537 * vpif_cropcap() - cropcap handler
1538 * @file: file ptr
1539 * @priv: file handle
1540 * @crop: ptr to v4l2_cropcap structure
1541 */
1542static int vpif_cropcap(struct file *file, void *priv,
1543 struct v4l2_cropcap *crop)
1544{
1545 struct vpif_fh *fh = priv;
1546 struct channel_obj *ch = fh->channel;
1547 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1548
1549 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != crop->type)
1550 return -EINVAL;
1551
1552 crop->bounds.left = 0;
1553 crop->bounds.top = 0;
1554 crop->bounds.height = common->height;
1555 crop->bounds.width = common->width;
1556 crop->defrect = crop->bounds;
1557 return 0;
1558}
1559
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001560/**
Hans Verkuil0598c172012-09-18 07:18:47 -03001561 * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001562 * @file: file ptr
1563 * @priv: file handle
Hans Verkuil0598c172012-09-18 07:18:47 -03001564 * @timings: input timings
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001565 */
Hans Verkuil0598c172012-09-18 07:18:47 -03001566static int
1567vpif_enum_dv_timings(struct file *file, void *priv,
1568 struct v4l2_enum_dv_timings *timings)
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001569{
1570 struct vpif_fh *fh = priv;
1571 struct channel_obj *ch = fh->channel;
Hans Verkuil178cce12012-09-20 09:06:30 -03001572 int ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001573
Hans Verkuil178cce12012-09-20 09:06:30 -03001574 ret = v4l2_subdev_call(ch->sd, video, enum_dv_timings, timings);
Wei Yongjune070f1b2012-10-30 09:45:00 -03001575 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
Hans Verkuil178cce12012-09-20 09:06:30 -03001576 return -EINVAL;
1577 return ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001578}
1579
1580/**
Hans Verkuil0598c172012-09-18 07:18:47 -03001581 * vpif_query_dv_timings() - QUERY_DV_TIMINGS handler
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001582 * @file: file ptr
1583 * @priv: file handle
Hans Verkuil0598c172012-09-18 07:18:47 -03001584 * @timings: input timings
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001585 */
Hans Verkuil0598c172012-09-18 07:18:47 -03001586static int
1587vpif_query_dv_timings(struct file *file, void *priv,
1588 struct v4l2_dv_timings *timings)
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001589{
1590 struct vpif_fh *fh = priv;
1591 struct channel_obj *ch = fh->channel;
Hans Verkuil178cce12012-09-20 09:06:30 -03001592 int ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001593
Hans Verkuil178cce12012-09-20 09:06:30 -03001594 ret = v4l2_subdev_call(ch->sd, video, query_dv_timings, timings);
Wei Yongjune070f1b2012-10-30 09:45:00 -03001595 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
Hans Verkuil178cce12012-09-20 09:06:30 -03001596 return -ENODATA;
1597 return ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001598}
1599
Mats Randgaardc027e162010-12-16 12:17:44 -03001600/**
1601 * vpif_s_dv_timings() - S_DV_TIMINGS handler
1602 * @file: file ptr
1603 * @priv: file handle
1604 * @timings: digital video timings
1605 */
1606static int vpif_s_dv_timings(struct file *file, void *priv,
1607 struct v4l2_dv_timings *timings)
1608{
1609 struct vpif_fh *fh = priv;
1610 struct channel_obj *ch = fh->channel;
1611 struct vpif_params *vpifparams = &ch->vpifparams;
1612 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
1613 struct video_obj *vid_ch = &ch->video;
Hans Verkuil0598c172012-09-18 07:18:47 -03001614 struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
Mats Randgaardc027e162010-12-16 12:17:44 -03001615 int ret;
1616
1617 if (timings->type != V4L2_DV_BT_656_1120) {
1618 vpif_dbg(2, debug, "Timing type not defined\n");
1619 return -EINVAL;
1620 }
1621
1622 /* Configure subdevice timings, if any */
Hans Verkuil178cce12012-09-20 09:06:30 -03001623 ret = v4l2_subdev_call(ch->sd, video, s_dv_timings, timings);
1624 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1625 ret = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001626 if (ret < 0) {
1627 vpif_dbg(2, debug, "Error setting custom DV timings\n");
1628 return ret;
1629 }
1630
1631 if (!(timings->bt.width && timings->bt.height &&
1632 (timings->bt.hbackporch ||
1633 timings->bt.hfrontporch ||
1634 timings->bt.hsync) &&
1635 timings->bt.vfrontporch &&
1636 (timings->bt.vbackporch ||
1637 timings->bt.vsync))) {
1638 vpif_dbg(2, debug, "Timings for width, height, "
1639 "horizontal back porch, horizontal sync, "
1640 "horizontal front porch, vertical back porch, "
1641 "vertical sync and vertical back porch "
1642 "must be defined\n");
1643 return -EINVAL;
1644 }
1645
Hans Verkuil0598c172012-09-18 07:18:47 -03001646 vid_ch->dv_timings = *timings;
Mats Randgaardc027e162010-12-16 12:17:44 -03001647
1648 /* Configure video port timings */
1649
Hans Verkuile3655262013-07-29 08:41:00 -03001650 std_info->eav2sav = V4L2_DV_BT_BLANKING_WIDTH(bt) - 8;
Mats Randgaardc027e162010-12-16 12:17:44 -03001651 std_info->sav2eav = bt->width;
1652
1653 std_info->l1 = 1;
1654 std_info->l3 = bt->vsync + bt->vbackporch + 1;
1655
Hans Verkuile3655262013-07-29 08:41:00 -03001656 std_info->vsize = V4L2_DV_BT_FRAME_HEIGHT(bt);
Mats Randgaardc027e162010-12-16 12:17:44 -03001657 if (bt->interlaced) {
1658 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
Mats Randgaardc027e162010-12-16 12:17:44 -03001659 std_info->l5 = std_info->vsize/2 -
1660 (bt->vfrontporch - 1);
1661 std_info->l7 = std_info->vsize/2 + 1;
1662 std_info->l9 = std_info->l7 + bt->il_vsync +
1663 bt->il_vbackporch + 1;
1664 std_info->l11 = std_info->vsize -
1665 (bt->il_vfrontporch - 1);
1666 } else {
1667 vpif_dbg(2, debug, "Required timing values for "
1668 "interlaced BT format missing\n");
1669 return -EINVAL;
1670 }
1671 } else {
Mats Randgaardc027e162010-12-16 12:17:44 -03001672 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
1673 }
1674 strncpy(std_info->name, "Custom timings BT656/1120", VPIF_MAX_NAME);
1675 std_info->width = bt->width;
1676 std_info->height = bt->height;
1677 std_info->frm_fmt = bt->interlaced ? 0 : 1;
1678 std_info->ycmux_mode = 0;
1679 std_info->capture_format = 0;
1680 std_info->vbi_supported = 0;
1681 std_info->hd_sd = 1;
1682 std_info->stdid = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001683
1684 vid_ch->stdid = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001685 return 0;
1686}
1687
1688/**
1689 * vpif_g_dv_timings() - G_DV_TIMINGS handler
1690 * @file: file ptr
1691 * @priv: file handle
1692 * @timings: digital video timings
1693 */
1694static int vpif_g_dv_timings(struct file *file, void *priv,
1695 struct v4l2_dv_timings *timings)
1696{
1697 struct vpif_fh *fh = priv;
1698 struct channel_obj *ch = fh->channel;
1699 struct video_obj *vid_ch = &ch->video;
Mats Randgaardc027e162010-12-16 12:17:44 -03001700
Hans Verkuil0598c172012-09-18 07:18:47 -03001701 *timings = vid_ch->dv_timings;
Mats Randgaardc027e162010-12-16 12:17:44 -03001702
1703 return 0;
1704}
1705
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001706/*
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001707 * vpif_log_status() - Status information
1708 * @file: file ptr
1709 * @priv: file handle
1710 *
1711 * Returns zero.
1712 */
1713static int vpif_log_status(struct file *filep, void *priv)
1714{
1715 /* status for sub devices */
1716 v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1717
1718 return 0;
1719}
1720
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001721/* vpif capture ioctl operations */
1722static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
1723 .vidioc_querycap = vpif_querycap,
1724 .vidioc_g_priority = vpif_g_priority,
1725 .vidioc_s_priority = vpif_s_priority,
1726 .vidioc_enum_fmt_vid_cap = vpif_enum_fmt_vid_cap,
1727 .vidioc_g_fmt_vid_cap = vpif_g_fmt_vid_cap,
1728 .vidioc_s_fmt_vid_cap = vpif_s_fmt_vid_cap,
1729 .vidioc_try_fmt_vid_cap = vpif_try_fmt_vid_cap,
1730 .vidioc_enum_input = vpif_enum_input,
1731 .vidioc_s_input = vpif_s_input,
1732 .vidioc_g_input = vpif_g_input,
1733 .vidioc_reqbufs = vpif_reqbufs,
1734 .vidioc_querybuf = vpif_querybuf,
1735 .vidioc_querystd = vpif_querystd,
1736 .vidioc_s_std = vpif_s_std,
1737 .vidioc_g_std = vpif_g_std,
1738 .vidioc_qbuf = vpif_qbuf,
1739 .vidioc_dqbuf = vpif_dqbuf,
1740 .vidioc_streamon = vpif_streamon,
1741 .vidioc_streamoff = vpif_streamoff,
1742 .vidioc_cropcap = vpif_cropcap,
Hans Verkuil0598c172012-09-18 07:18:47 -03001743 .vidioc_enum_dv_timings = vpif_enum_dv_timings,
1744 .vidioc_query_dv_timings = vpif_query_dv_timings,
Mats Randgaardc027e162010-12-16 12:17:44 -03001745 .vidioc_s_dv_timings = vpif_s_dv_timings,
1746 .vidioc_g_dv_timings = vpif_g_dv_timings,
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001747 .vidioc_log_status = vpif_log_status,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001748};
1749
1750/* vpif file operations */
1751static struct v4l2_file_operations vpif_fops = {
1752 .owner = THIS_MODULE,
1753 .open = vpif_open,
1754 .release = vpif_release,
Hans Verkuil46656af2011-01-04 06:51:35 -03001755 .unlocked_ioctl = video_ioctl2,
Lad, Prabhakard26d26b2014-05-16 10:33:40 -03001756 .mmap = vb2_fop_mmap,
1757 .poll = vb2_fop_poll
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001758};
1759
1760/* vpif video template */
1761static struct video_device vpif_video_template = {
1762 .name = "vpif",
1763 .fops = &vpif_fops,
1764 .minor = -1,
1765 .ioctl_ops = &vpif_ioctl_ops,
1766};
1767
1768/**
1769 * initialize_vpif() - Initialize vpif data structures
1770 *
1771 * Allocate memory for data structures and initialize them
1772 */
1773static int initialize_vpif(void)
1774{
1775 int err = 0, i, j;
1776 int free_channel_objects_index;
1777
1778 /* Default number of buffers should be 3 */
1779 if ((ch0_numbuffers > 0) &&
1780 (ch0_numbuffers < config_params.min_numbuffers))
1781 ch0_numbuffers = config_params.min_numbuffers;
1782 if ((ch1_numbuffers > 0) &&
1783 (ch1_numbuffers < config_params.min_numbuffers))
1784 ch1_numbuffers = config_params.min_numbuffers;
1785
1786 /* Set buffer size to min buffers size if it is invalid */
1787 if (ch0_bufsize < config_params.min_bufsize[VPIF_CHANNEL0_VIDEO])
1788 ch0_bufsize =
1789 config_params.min_bufsize[VPIF_CHANNEL0_VIDEO];
1790 if (ch1_bufsize < config_params.min_bufsize[VPIF_CHANNEL1_VIDEO])
1791 ch1_bufsize =
1792 config_params.min_bufsize[VPIF_CHANNEL1_VIDEO];
1793
1794 config_params.numbuffers[VPIF_CHANNEL0_VIDEO] = ch0_numbuffers;
1795 config_params.numbuffers[VPIF_CHANNEL1_VIDEO] = ch1_numbuffers;
1796 if (ch0_numbuffers) {
1797 config_params.channel_bufsize[VPIF_CHANNEL0_VIDEO]
1798 = ch0_bufsize;
1799 }
1800 if (ch1_numbuffers) {
1801 config_params.channel_bufsize[VPIF_CHANNEL1_VIDEO]
1802 = ch1_bufsize;
1803 }
1804
1805 /* Allocate memory for six channel objects */
1806 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1807 vpif_obj.dev[i] =
1808 kzalloc(sizeof(*vpif_obj.dev[i]), GFP_KERNEL);
1809 /* If memory allocation fails, return error */
1810 if (!vpif_obj.dev[i]) {
1811 free_channel_objects_index = i;
1812 err = -ENOMEM;
1813 goto vpif_init_free_channel_objects;
1814 }
1815 }
1816 return 0;
1817
1818vpif_init_free_channel_objects:
1819 for (j = 0; j < free_channel_objects_index; j++)
1820 kfree(vpif_obj.dev[j]);
1821 return err;
1822}
1823
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001824static int vpif_async_bound(struct v4l2_async_notifier *notifier,
1825 struct v4l2_subdev *subdev,
1826 struct v4l2_async_subdev *asd)
1827{
1828 int i;
1829
1830 for (i = 0; i < vpif_obj.config->subdev_count; i++)
1831 if (!strcmp(vpif_obj.config->subdev_info[i].name,
1832 subdev->name)) {
1833 vpif_obj.sd[i] = subdev;
1834 return 0;
1835 }
1836
1837 return -EINVAL;
1838}
1839
1840static int vpif_probe_complete(void)
1841{
1842 struct common_obj *common;
Lad, Prabhakard26d26b2014-05-16 10:33:40 -03001843 struct video_device *vdev;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001844 struct channel_obj *ch;
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001845 struct vb2_queue *q;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001846 int i, j, err, k;
1847
1848 for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
1849 ch = vpif_obj.dev[j];
1850 ch->channel_id = j;
1851 common = &(ch->common[VPIF_VIDEO_INDEX]);
1852 spin_lock_init(&common->irqlock);
1853 mutex_init(&common->lock);
1854 ch->video_dev->lock = &common->lock;
1855 /* Initialize prio member of channel object */
1856 v4l2_prio_init(&ch->prio);
1857 video_set_drvdata(ch->video_dev, ch);
1858
1859 /* select input 0 */
1860 err = vpif_set_input(vpif_obj.config, ch, 0);
1861 if (err)
1862 goto probe_out;
1863
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001864 /* Initialize vb2 queue */
1865 q = &common->buffer_queue;
1866 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1867 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1868 q->drv_priv = ch;
1869 q->ops = &video_qops;
1870 q->mem_ops = &vb2_dma_contig_memops;
1871 q->buf_struct_size = sizeof(struct vpif_cap_buffer);
1872 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1873 q->min_buffers_needed = 1;
Lad, Prabhakar999ba692014-05-16 10:33:33 -03001874 q->lock = &common->lock;
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001875
1876 err = vb2_queue_init(q);
1877 if (err) {
1878 vpif_err("vpif_capture: vb2_queue_init() failed\n");
1879 goto probe_out;
1880 }
1881
1882 common->alloc_ctx = vb2_dma_contig_init_ctx(vpif_dev);
1883 if (IS_ERR(common->alloc_ctx)) {
1884 vpif_err("Failed to get the context\n");
1885 err = PTR_ERR(common->alloc_ctx);
1886 goto probe_out;
1887 }
1888
1889 INIT_LIST_HEAD(&common->dma_queue);
1890
Lad, Prabhakard26d26b2014-05-16 10:33:40 -03001891 vdev = ch->video_dev;
1892 vdev->queue = q;
1893 err = video_register_device(vdev,
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001894 VFL_TYPE_GRABBER, (j ? 1 : 0));
1895 if (err)
1896 goto probe_out;
1897 }
1898
1899 v4l2_info(&vpif_obj.v4l2_dev, "VPIF capture driver initialized\n");
1900 return 0;
1901
1902probe_out:
1903 for (k = 0; k < j; k++) {
1904 /* Get the pointer to the channel object */
1905 ch = vpif_obj.dev[k];
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001906 common = &ch->common[k];
1907 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001908 /* Unregister video device */
1909 video_unregister_device(ch->video_dev);
1910 }
1911 kfree(vpif_obj.sd);
1912 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1913 ch = vpif_obj.dev[i];
1914 /* Note: does nothing if ch->video_dev == NULL */
1915 video_device_release(ch->video_dev);
1916 }
1917 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1918
1919 return err;
1920}
1921
1922static int vpif_async_complete(struct v4l2_async_notifier *notifier)
1923{
1924 return vpif_probe_complete();
1925}
1926
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001927/**
1928 * vpif_probe : This function probes the vpif capture driver
1929 * @pdev: platform device pointer
1930 *
1931 * This creates device entries by register itself to the V4L2 driver and
1932 * initializes fields of each channel objects
1933 */
1934static __init int vpif_probe(struct platform_device *pdev)
1935{
1936 struct vpif_subdev_info *subdevdata;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001937 int i, j, err;
Hans Verkuil5be452c2012-09-20 09:06:29 -03001938 int res_idx = 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001939 struct i2c_adapter *i2c_adap;
1940 struct channel_obj *ch;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001941 struct video_device *vfd;
1942 struct resource *res;
1943 int subdev_count;
Manjunath Hadli764af392012-04-13 04:49:34 -03001944 size_t size;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001945
1946 vpif_dev = &pdev->dev;
1947
1948 err = initialize_vpif();
1949 if (err) {
1950 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
1951 return err;
1952 }
1953
Hans Verkuild2f7a1a2011-12-13 05:44:42 -03001954 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
1955 if (err) {
1956 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
1957 return err;
1958 }
1959
Hans Verkuil5be452c2012-09-20 09:06:29 -03001960 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
Lad, Prabhakara76a0b32013-06-17 11:20:47 -03001961 err = devm_request_irq(&pdev->dev, res->start, vpif_channel_isr,
1962 IRQF_SHARED, "VPIF_Capture",
1963 (void *)(&vpif_obj.dev[res_idx]->
1964 channel_id));
1965 if (err) {
1966 err = -EINVAL;
1967 goto vpif_unregister;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001968 }
Hans Verkuil5be452c2012-09-20 09:06:29 -03001969 res_idx++;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001970 }
1971
1972 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1973 /* Get the pointer to the channel object */
1974 ch = vpif_obj.dev[i];
1975 /* Allocate memory for video device */
1976 vfd = video_device_alloc();
1977 if (NULL == vfd) {
1978 for (j = 0; j < i; j++) {
1979 ch = vpif_obj.dev[j];
1980 video_device_release(ch->video_dev);
1981 }
1982 err = -ENOMEM;
Lad, Prabhakarb2de4f22013-06-17 11:20:46 -03001983 goto vpif_unregister;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001984 }
1985
1986 /* Initialize field of video device */
1987 *vfd = vpif_video_template;
1988 vfd->v4l2_dev = &vpif_obj.v4l2_dev;
1989 vfd->release = video_device_release;
1990 snprintf(vfd->name, sizeof(vfd->name),
Manjunath Hadli0a631722012-04-13 04:44:00 -03001991 "VPIF_Capture_DRIVER_V%s",
Mauro Carvalho Chehab64dc3c12011-06-25 11:28:37 -03001992 VPIF_CAPTURE_VERSION);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001993 /* Set video_dev to the video device */
1994 ch->video_dev = vfd;
1995 }
1996
Manjunath Hadli764af392012-04-13 04:49:34 -03001997 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1998 if (res) {
1999 size = resource_size(res);
2000 /* The resources are divided into two equal memory and when we
2001 * have HD output we can add them together
2002 */
2003 for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
2004 ch = vpif_obj.dev[j];
2005 ch->channel_id = j;
2006 /* only enabled if second resource exists */
2007 config_params.video_limit[ch->channel_id] = 0;
2008 if (size)
2009 config_params.video_limit[ch->channel_id] =
2010 size/2;
2011 }
2012 }
2013
Lad, Prabhakar873229e2013-06-25 11:17:34 -03002014 vpif_obj.config = pdev->dev.platform_data;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002015
Lad, Prabhakar873229e2013-06-25 11:17:34 -03002016 subdev_count = vpif_obj.config->subdev_count;
Mats Randgaard1f8766b2010-08-30 10:30:37 -03002017 vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002018 GFP_KERNEL);
2019 if (vpif_obj.sd == NULL) {
2020 vpif_err("unable to allocate memory for subdevice pointers\n");
2021 err = -ENOMEM;
Hans Verkuil5be452c2012-09-20 09:06:29 -03002022 goto vpif_sd_error;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002023 }
2024
Lad, Prabhakar873229e2013-06-25 11:17:34 -03002025 if (!vpif_obj.config->asd_sizes) {
2026 i2c_adap = i2c_get_adapter(1);
2027 for (i = 0; i < subdev_count; i++) {
2028 subdevdata = &vpif_obj.config->subdev_info[i];
2029 vpif_obj.sd[i] =
2030 v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
2031 i2c_adap,
2032 &subdevdata->
2033 board_info,
2034 NULL);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002035
Lad, Prabhakar873229e2013-06-25 11:17:34 -03002036 if (!vpif_obj.sd[i]) {
2037 vpif_err("Error registering v4l2 subdevice\n");
Wei Yongjun2fcd9dc2013-09-02 05:06:10 -03002038 err = -ENODEV;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03002039 goto probe_subdev_out;
2040 }
2041 v4l2_info(&vpif_obj.v4l2_dev,
2042 "registered sub device %s\n",
2043 subdevdata->name);
2044 }
2045 vpif_probe_complete();
2046 } else {
Sylwester Nawrockie8419d02013-07-19 12:31:10 -03002047 vpif_obj.notifier.subdevs = vpif_obj.config->asd;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03002048 vpif_obj.notifier.num_subdevs = vpif_obj.config->asd_sizes[0];
2049 vpif_obj.notifier.bound = vpif_async_bound;
2050 vpif_obj.notifier.complete = vpif_async_complete;
2051 err = v4l2_async_notifier_register(&vpif_obj.v4l2_dev,
2052 &vpif_obj.notifier);
2053 if (err) {
2054 vpif_err("Error registering async notifier\n");
2055 err = -EINVAL;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002056 goto probe_subdev_out;
2057 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002058 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002059
2060 return 0;
2061
Hans Verkuilb65814e2012-09-20 09:06:26 -03002062probe_subdev_out:
2063 /* free sub devices memory */
2064 kfree(vpif_obj.sd);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002065
Hans Verkuil5be452c2012-09-20 09:06:29 -03002066vpif_sd_error:
2067 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2068 ch = vpif_obj.dev[i];
2069 /* Note: does nothing if ch->video_dev == NULL */
2070 video_device_release(ch->video_dev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002071 }
Lad, Prabhakarb2de4f22013-06-17 11:20:46 -03002072vpif_unregister:
Hans Verkuild2f7a1a2011-12-13 05:44:42 -03002073 v4l2_device_unregister(&vpif_obj.v4l2_dev);
Lad, Prabhakarb2de4f22013-06-17 11:20:46 -03002074
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002075 return err;
2076}
2077
2078/**
2079 * vpif_remove() - driver remove handler
2080 * @device: ptr to platform device structure
2081 *
2082 * The vidoe device is unregistered
2083 */
2084static int vpif_remove(struct platform_device *device)
2085{
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03002086 struct common_obj *common;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002087 struct channel_obj *ch;
Lad, Prabhakarb2de4f22013-06-17 11:20:46 -03002088 int i;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002089
2090 v4l2_device_unregister(&vpif_obj.v4l2_dev);
2091
Lad, Prabhakar410ca602013-06-17 11:20:44 -03002092 kfree(vpif_obj.sd);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002093 /* un-register device */
2094 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2095 /* Get the pointer to the channel object */
2096 ch = vpif_obj.dev[i];
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03002097 common = &ch->common[i];
2098 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002099 /* Unregister video device */
2100 video_unregister_device(ch->video_dev);
Lad, Prabhakar410ca602013-06-17 11:20:44 -03002101 kfree(vpif_obj.dev[i]);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002102 }
2103 return 0;
2104}
2105
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002106#ifdef CONFIG_PM
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002107/**
2108 * vpif_suspend: vpif device suspend
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002109 */
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002110static int vpif_suspend(struct device *dev)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002111{
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002112
2113 struct common_obj *common;
2114 struct channel_obj *ch;
2115 int i;
2116
2117 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2118 /* Get the pointer to the channel object */
2119 ch = vpif_obj.dev[i];
2120 common = &ch->common[VPIF_VIDEO_INDEX];
2121 mutex_lock(&common->lock);
2122 if (ch->usrs && common->io_usrs) {
2123 /* Disable channel */
2124 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
2125 enable_channel0(0);
2126 channel0_intr_enable(0);
2127 }
2128 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
2129 common->started == 2) {
2130 enable_channel1(0);
2131 channel1_intr_enable(0);
2132 }
2133 }
2134 mutex_unlock(&common->lock);
2135 }
2136
2137 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002138}
2139
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002140/*
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002141 * vpif_resume: vpif device suspend
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002142 */
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002143static int vpif_resume(struct device *dev)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002144{
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002145 struct common_obj *common;
2146 struct channel_obj *ch;
2147 int i;
2148
2149 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2150 /* Get the pointer to the channel object */
2151 ch = vpif_obj.dev[i];
2152 common = &ch->common[VPIF_VIDEO_INDEX];
2153 mutex_lock(&common->lock);
2154 if (ch->usrs && common->io_usrs) {
2155 /* Disable channel */
2156 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
2157 enable_channel0(1);
2158 channel0_intr_enable(1);
2159 }
2160 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
2161 common->started == 2) {
2162 enable_channel1(1);
2163 channel1_intr_enable(1);
2164 }
2165 }
2166 mutex_unlock(&common->lock);
2167 }
2168
2169 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002170}
2171
Alexey Dobriyan47145212009-12-14 18:00:08 -08002172static const struct dev_pm_ops vpif_dev_pm_ops = {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002173 .suspend = vpif_suspend,
2174 .resume = vpif_resume,
2175};
2176
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002177#define vpif_pm_ops (&vpif_dev_pm_ops)
2178#else
2179#define vpif_pm_ops NULL
2180#endif
2181
Mats Randgaardffa1b392010-08-30 10:30:36 -03002182static __refdata struct platform_driver vpif_driver = {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002183 .driver = {
2184 .name = "vpif_capture",
2185 .owner = THIS_MODULE,
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03002186 .pm = vpif_pm_ops,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03002187 },
2188 .probe = vpif_probe,
2189 .remove = vpif_remove,
2190};
2191
Lad, Prabhakarfe424b22013-06-17 11:20:45 -03002192module_platform_driver(vpif_driver);