blob: 6ad9e09d2bb27f7adcc7fcc446ab40254b6aca58 [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_reqbufs() - request buffer handler
755 * @file: file ptr
756 * @priv: file handle
757 * @reqbuf: request buffer structure ptr
758 */
759static int vpif_reqbufs(struct file *file, void *priv,
760 struct v4l2_requestbuffers *reqbuf)
761{
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300762 struct video_device *vdev = video_devdata(file);
763 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300764 struct common_obj *common;
765 u8 index = 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300766
767 vpif_dbg(2, debug, "vpif_reqbufs\n");
768
Manjunath Hadli764af392012-04-13 04:49:34 -0300769 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != reqbuf->type || !vpif_dev)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300770 return -EINVAL;
771
772 index = VPIF_VIDEO_INDEX;
773
774 common = &ch->common[index];
775
Hans Verkuil46656af2011-01-04 06:51:35 -0300776 if (0 != common->io_usrs)
777 return -EBUSY;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300778
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300779 /* Increment io usrs member of channel object to 1 */
780 common->io_usrs = 1;
781 /* Store type of memory requested in channel object */
782 common->memory = reqbuf->memory;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300783
784 /* Allocate buffers */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300785 return vb2_reqbufs(&common->buffer_queue, reqbuf);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300786}
787
788/**
789 * vpif_querybuf() - query buffer handler
790 * @file: file ptr
791 * @priv: file handle
792 * @buf: v4l2 buffer structure ptr
793 */
794static int vpif_querybuf(struct file *file, void *priv,
795 struct v4l2_buffer *buf)
796{
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300797 struct video_device *vdev = video_devdata(file);
798 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300799 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
800
801 vpif_dbg(2, debug, "vpif_querybuf\n");
802
803 if (common->fmt.type != buf->type)
804 return -EINVAL;
805
806 if (common->memory != V4L2_MEMORY_MMAP) {
807 vpif_dbg(1, debug, "Invalid memory\n");
808 return -EINVAL;
809 }
810
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300811 return vb2_querybuf(&common->buffer_queue, buf);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300812}
813
814/**
815 * vpif_qbuf() - query buffer handler
816 * @file: file ptr
817 * @priv: file handle
818 * @buf: v4l2 buffer structure ptr
819 */
820static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
821{
822
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300823 struct video_device *vdev = video_devdata(file);
824 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300825 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
826 struct v4l2_buffer tbuf = *buf;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300827
828 vpif_dbg(2, debug, "vpif_qbuf\n");
829
830 if (common->fmt.type != tbuf.type) {
831 vpif_err("invalid buffer type\n");
832 return -EINVAL;
833 }
834
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300835 return vb2_qbuf(&common->buffer_queue, buf);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300836}
837
838/**
839 * vpif_dqbuf() - query buffer handler
840 * @file: file ptr
841 * @priv: file handle
842 * @buf: v4l2 buffer structure ptr
843 */
844static int vpif_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
845{
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300846 struct video_device *vdev = video_devdata(file);
847 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300848 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
849
850 vpif_dbg(2, debug, "vpif_dqbuf\n");
851
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300852 return vb2_dqbuf(&common->buffer_queue, buf,
853 (file->f_flags & O_NONBLOCK));
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300854}
855
856/**
857 * vpif_streamon() - streamon handler
858 * @file: file ptr
859 * @priv: file handle
860 * @buftype: v4l2 buffer type
861 */
862static int vpif_streamon(struct file *file, void *priv,
863 enum v4l2_buf_type buftype)
864{
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300865 struct video_device *vdev = video_devdata(file);
866 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300867 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
868 struct channel_obj *oth_ch = vpif_obj.dev[!ch->channel_id];
869 struct vpif_params *vpif;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300870 int ret = 0;
871
872 vpif_dbg(2, debug, "vpif_streamon\n");
873
874 vpif = &ch->vpifparams;
875
876 if (buftype != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
877 vpif_dbg(1, debug, "buffer type not supported\n");
878 return -EINVAL;
879 }
880
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300881 /* If Streaming is already started, return error */
882 if (common->started) {
883 vpif_dbg(1, debug, "channel->started\n");
884 return -EBUSY;
885 }
886
887 if ((ch->channel_id == VPIF_CHANNEL0_VIDEO &&
888 oth_ch->common[VPIF_VIDEO_INDEX].started &&
889 vpif->std_info.ycmux_mode == 0) ||
890 ((ch->channel_id == VPIF_CHANNEL1_VIDEO) &&
891 (2 == oth_ch->common[VPIF_VIDEO_INDEX].started))) {
892 vpif_dbg(1, debug, "other channel is being used\n");
893 return -EBUSY;
894 }
895
896 ret = vpif_check_format(ch, &common->fmt.fmt.pix, 0);
897 if (ret)
898 return ret;
899
900 /* Enable streamon on the sub device */
Hans Verkuil178cce12012-09-20 09:06:30 -0300901 ret = v4l2_subdev_call(ch->sd, video, s_stream, 1);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300902
Hans Verkuil178cce12012-09-20 09:06:30 -0300903 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300904 vpif_dbg(1, debug, "stream on failed in subdev\n");
905 return ret;
906 }
907
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300908 /* Call vb2_streamon to start streaming in videobuf2 */
909 ret = vb2_streamon(&common->buffer_queue, buftype);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300910 if (ret) {
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300911 vpif_dbg(1, debug, "vb2_streamon\n");
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300912 return ret;
913 }
914
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300915 return ret;
916}
917
918/**
919 * vpif_streamoff() - streamoff handler
920 * @file: file ptr
921 * @priv: file handle
922 * @buftype: v4l2 buffer type
923 */
924static int vpif_streamoff(struct file *file, void *priv,
925 enum v4l2_buf_type buftype)
926{
927
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300928 struct video_device *vdev = video_devdata(file);
929 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300930 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
931 int ret;
932
933 vpif_dbg(2, debug, "vpif_streamoff\n");
934
935 if (buftype != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
936 vpif_dbg(1, debug, "buffer type not supported\n");
937 return -EINVAL;
938 }
939
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300940 /* If streaming is not started, return error */
941 if (!common->started) {
942 vpif_dbg(1, debug, "channel->started\n");
943 return -EINVAL;
944 }
945
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300946 /* disable channel */
947 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
948 enable_channel0(0);
949 channel0_intr_enable(0);
950 } else {
951 enable_channel1(0);
952 channel1_intr_enable(0);
953 }
954
955 common->started = 0;
956
Hans Verkuil178cce12012-09-20 09:06:30 -0300957 ret = v4l2_subdev_call(ch->sd, video, s_stream, 0);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300958
Hans Verkuil178cce12012-09-20 09:06:30 -0300959 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300960 vpif_dbg(1, debug, "stream off failed in subdev\n");
961
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300962 return vb2_streamoff(&common->buffer_queue, buftype);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300963}
964
965/**
Hans Verkuil178cce12012-09-20 09:06:30 -0300966 * vpif_input_to_subdev() - Maps input to sub device
967 * @vpif_cfg - global config ptr
968 * @chan_cfg - channel config ptr
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300969 * @input_index - Given input index from application
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300970 *
971 * lookup the sub device information for a given input index.
972 * we report all the inputs to application. inputs table also
973 * has sub device name for the each input
974 */
Hans Verkuil178cce12012-09-20 09:06:30 -0300975static int vpif_input_to_subdev(
976 struct vpif_capture_config *vpif_cfg,
977 struct vpif_capture_chan_config *chan_cfg,
978 int input_index)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300979{
Hans Verkuil178cce12012-09-20 09:06:30 -0300980 struct vpif_subdev_info *subdev_info;
981 const char *subdev_name;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300982 int i;
983
Hans Verkuil178cce12012-09-20 09:06:30 -0300984 vpif_dbg(2, debug, "vpif_input_to_subdev\n");
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300985
Hans Verkuil178cce12012-09-20 09:06:30 -0300986 subdev_name = chan_cfg->inputs[input_index].subdev_name;
987 if (subdev_name == NULL)
988 return -1;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300989
990 /* loop through the sub device list to get the sub device info */
991 for (i = 0; i < vpif_cfg->subdev_count; i++) {
992 subdev_info = &vpif_cfg->subdev_info[i];
993 if (!strcmp(subdev_info->name, subdev_name))
Hans Verkuil178cce12012-09-20 09:06:30 -0300994 return i;
995 }
996 return -1;
997}
998
999/**
1000 * vpif_set_input() - Select an input
1001 * @vpif_cfg - global config ptr
1002 * @ch - channel
1003 * @_index - Given input index from application
1004 *
1005 * Select the given input.
1006 */
1007static int vpif_set_input(
1008 struct vpif_capture_config *vpif_cfg,
1009 struct channel_obj *ch,
1010 int index)
1011{
1012 struct vpif_capture_chan_config *chan_cfg =
1013 &vpif_cfg->chan_config[ch->channel_id];
1014 struct vpif_subdev_info *subdev_info = NULL;
1015 struct v4l2_subdev *sd = NULL;
1016 u32 input = 0, output = 0;
1017 int sd_index;
1018 int ret;
1019
1020 sd_index = vpif_input_to_subdev(vpif_cfg, chan_cfg, index);
1021 if (sd_index >= 0) {
1022 sd = vpif_obj.sd[sd_index];
1023 subdev_info = &vpif_cfg->subdev_info[sd_index];
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001024 }
1025
Hans Verkuil178cce12012-09-20 09:06:30 -03001026 /* first setup input path from sub device to vpif */
1027 if (sd && vpif_cfg->setup_input_path) {
1028 ret = vpif_cfg->setup_input_path(ch->channel_id,
1029 subdev_info->name);
1030 if (ret < 0) {
1031 vpif_dbg(1, debug, "couldn't setup input path for the" \
1032 " sub device %s, for input index %d\n",
1033 subdev_info->name, index);
1034 return ret;
1035 }
1036 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001037
Hans Verkuil178cce12012-09-20 09:06:30 -03001038 if (sd) {
1039 input = chan_cfg->inputs[index].input_route;
1040 output = chan_cfg->inputs[index].output_route;
1041 ret = v4l2_subdev_call(sd, video, s_routing,
1042 input, output, 0);
1043 if (ret < 0 && ret != -ENOIOCTLCMD) {
1044 vpif_dbg(1, debug, "Failed to set input\n");
1045 return ret;
1046 }
1047 }
1048 ch->input_idx = index;
1049 ch->sd = sd;
1050 /* copy interface parameters to vpif */
Hans Verkuil0d4f35f2012-09-20 09:06:32 -03001051 ch->vpifparams.iface = chan_cfg->vpif_if;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001052
Hans Verkuil178cce12012-09-20 09:06:30 -03001053 /* update tvnorms from the sub device input info */
1054 ch->video_dev->tvnorms = chan_cfg->inputs[index].input.std;
1055 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001056}
1057
1058/**
1059 * vpif_querystd() - querystd handler
1060 * @file: file ptr
1061 * @priv: file handle
1062 * @std_id: ptr to std id
1063 *
1064 * This function is called to detect standard at the selected input
1065 */
1066static int vpif_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
1067{
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001068 struct video_device *vdev = video_devdata(file);
1069 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001070 int ret = 0;
1071
1072 vpif_dbg(2, debug, "vpif_querystd\n");
1073
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001074 /* Call querystd function of decoder device */
Hans Verkuil178cce12012-09-20 09:06:30 -03001075 ret = v4l2_subdev_call(ch->sd, video, querystd, std_id);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001076
Hans Verkuil178cce12012-09-20 09:06:30 -03001077 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1078 return -ENODATA;
1079 if (ret) {
1080 vpif_dbg(1, debug, "Failed to query standard for sub devices\n");
1081 return ret;
1082 }
1083
1084 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001085}
1086
1087/**
1088 * vpif_g_std() - get STD handler
1089 * @file: file ptr
1090 * @priv: file handle
1091 * @std_id: ptr to std id
1092 */
1093static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
1094{
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001095 struct video_device *vdev = video_devdata(file);
1096 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001097
1098 vpif_dbg(2, debug, "vpif_g_std\n");
1099
1100 *std = ch->video.stdid;
1101 return 0;
1102}
1103
1104/**
1105 * vpif_s_std() - set STD handler
1106 * @file: file ptr
1107 * @priv: file handle
1108 * @std_id: ptr to std id
1109 */
Hans Verkuil314527a2013-03-15 06:10:40 -03001110static int vpif_s_std(struct file *file, void *priv, v4l2_std_id std_id)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001111{
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001112 struct video_device *vdev = video_devdata(file);
1113 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001114 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1115 int ret = 0;
1116
1117 vpif_dbg(2, debug, "vpif_s_std\n");
1118
1119 if (common->started) {
1120 vpif_err("streaming in progress\n");
1121 return -EBUSY;
1122 }
1123
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001124 /* Call encoder subdevice function to set the standard */
Hans Verkuil314527a2013-03-15 06:10:40 -03001125 ch->video.stdid = std_id;
Hans Verkuil0598c172012-09-18 07:18:47 -03001126 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001127
1128 /* Get the information about the standard */
1129 if (vpif_update_std_info(ch)) {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001130 vpif_err("Error getting the standard info\n");
Hans Verkuil46656af2011-01-04 06:51:35 -03001131 return -EINVAL;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001132 }
1133
1134 /* Configure the default format information */
1135 vpif_config_format(ch);
1136
1137 /* set standard in the sub device */
Hans Verkuil314527a2013-03-15 06:10:40 -03001138 ret = v4l2_subdev_call(ch->sd, core, s_std, std_id);
Hans Verkuil178cce12012-09-20 09:06:30 -03001139 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001140 vpif_dbg(1, debug, "Failed to set standard for sub devices\n");
Hans Verkuil178cce12012-09-20 09:06:30 -03001141 return ret;
1142 }
1143 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001144}
1145
1146/**
1147 * vpif_enum_input() - ENUMINPUT handler
1148 * @file: file ptr
1149 * @priv: file handle
1150 * @input: ptr to input structure
1151 */
1152static int vpif_enum_input(struct file *file, void *priv,
1153 struct v4l2_input *input)
1154{
1155
1156 struct vpif_capture_config *config = vpif_dev->platform_data;
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001157 struct video_device *vdev = video_devdata(file);
1158 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001159 struct vpif_capture_chan_config *chan_cfg;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001160
1161 chan_cfg = &config->chan_config[ch->channel_id];
1162
1163 if (input->index >= chan_cfg->input_count) {
1164 vpif_dbg(1, debug, "Invalid input index\n");
1165 return -EINVAL;
1166 }
1167
1168 memcpy(input, &chan_cfg->inputs[input->index].input,
1169 sizeof(*input));
1170 return 0;
1171}
1172
1173/**
1174 * vpif_g_input() - Get INPUT handler
1175 * @file: file ptr
1176 * @priv: file handle
1177 * @index: ptr to input index
1178 */
1179static int vpif_g_input(struct file *file, void *priv, unsigned int *index)
1180{
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001181 struct video_device *vdev = video_devdata(file);
1182 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001183
Hans Verkuil6f47c6c2012-09-20 09:06:22 -03001184 *index = ch->input_idx;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001185 return 0;
1186}
1187
1188/**
1189 * vpif_s_input() - Set INPUT handler
1190 * @file: file ptr
1191 * @priv: file handle
1192 * @index: input index
1193 */
1194static int vpif_s_input(struct file *file, void *priv, unsigned int index)
1195{
1196 struct vpif_capture_config *config = vpif_dev->platform_data;
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001197 struct video_device *vdev = video_devdata(file);
1198 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001199 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001200 struct vpif_capture_chan_config *chan_cfg;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001201
1202 chan_cfg = &config->chan_config[ch->channel_id];
1203
Hans Verkuil7aaad132012-09-20 09:06:25 -03001204 if (index >= chan_cfg->input_count)
1205 return -EINVAL;
1206
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001207 if (common->started) {
1208 vpif_err("Streaming in progress\n");
1209 return -EBUSY;
1210 }
1211
Hans Verkuil178cce12012-09-20 09:06:30 -03001212 return vpif_set_input(config, ch, index);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001213}
1214
1215/**
1216 * vpif_enum_fmt_vid_cap() - ENUM_FMT handler
1217 * @file: file ptr
1218 * @priv: file handle
1219 * @index: input index
1220 */
1221static int vpif_enum_fmt_vid_cap(struct file *file, void *priv,
1222 struct v4l2_fmtdesc *fmt)
1223{
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001224 struct video_device *vdev = video_devdata(file);
1225 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001226
1227 if (fmt->index != 0) {
1228 vpif_dbg(1, debug, "Invalid format index\n");
1229 return -EINVAL;
1230 }
1231
1232 /* Fill in the information about format */
1233 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER) {
1234 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1235 strcpy(fmt->description, "Raw Mode -Bayer Pattern GrRBGb");
1236 fmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
1237 } else {
1238 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1239 strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
1240 fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
1241 }
1242 return 0;
1243}
1244
1245/**
1246 * vpif_try_fmt_vid_cap() - TRY_FMT handler
1247 * @file: file ptr
1248 * @priv: file handle
1249 * @fmt: ptr to v4l2 format structure
1250 */
1251static int vpif_try_fmt_vid_cap(struct file *file, void *priv,
1252 struct v4l2_format *fmt)
1253{
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001254 struct video_device *vdev = video_devdata(file);
1255 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001256 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
1257
1258 return vpif_check_format(ch, pixfmt, 1);
1259}
1260
1261
1262/**
1263 * vpif_g_fmt_vid_cap() - Set INPUT handler
1264 * @file: file ptr
1265 * @priv: file handle
1266 * @fmt: ptr to v4l2 format structure
1267 */
1268static int vpif_g_fmt_vid_cap(struct file *file, void *priv,
1269 struct v4l2_format *fmt)
1270{
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001271 struct video_device *vdev = video_devdata(file);
1272 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001273 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1274
1275 /* Check the validity of the buffer type */
1276 if (common->fmt.type != fmt->type)
1277 return -EINVAL;
1278
1279 /* Fill in the information about format */
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001280 *fmt = common->fmt;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001281 return 0;
1282}
1283
1284/**
1285 * vpif_s_fmt_vid_cap() - Set FMT handler
1286 * @file: file ptr
1287 * @priv: file handle
1288 * @fmt: ptr to v4l2 format structure
1289 */
1290static int vpif_s_fmt_vid_cap(struct file *file, void *priv,
1291 struct v4l2_format *fmt)
1292{
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001293 struct video_device *vdev = video_devdata(file);
1294 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001295 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1296 struct v4l2_pix_format *pixfmt;
1297 int ret = 0;
1298
Mats Randgaard2c0ddd12010-12-16 12:17:45 -03001299 vpif_dbg(2, debug, "%s\n", __func__);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001300
1301 /* If streaming is started, return error */
1302 if (common->started) {
1303 vpif_dbg(1, debug, "Streaming is started\n");
1304 return -EBUSY;
1305 }
1306
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001307 pixfmt = &fmt->fmt.pix;
1308 /* Check for valid field format */
1309 ret = vpif_check_format(ch, pixfmt, 0);
1310
1311 if (ret)
1312 return ret;
1313 /* store the format in the channel object */
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001314 common->fmt = *fmt;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001315 return 0;
1316}
1317
1318/**
1319 * vpif_querycap() - QUERYCAP handler
1320 * @file: file ptr
1321 * @priv: file handle
1322 * @cap: ptr to v4l2_capability structure
1323 */
1324static int vpif_querycap(struct file *file, void *priv,
1325 struct v4l2_capability *cap)
1326{
1327 struct vpif_capture_config *config = vpif_dev->platform_data;
1328
Lad, Prabhakar626d533f2012-09-25 11:21:55 -03001329 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1330 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1331 snprintf(cap->driver, sizeof(cap->driver), "%s", dev_name(vpif_dev));
1332 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
1333 dev_name(vpif_dev));
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001334 strlcpy(cap->card, config->card_name, sizeof(cap->card));
1335
1336 return 0;
1337}
1338
1339/**
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001340 * vpif_cropcap() - cropcap handler
1341 * @file: file ptr
1342 * @priv: file handle
1343 * @crop: ptr to v4l2_cropcap structure
1344 */
1345static int vpif_cropcap(struct file *file, void *priv,
1346 struct v4l2_cropcap *crop)
1347{
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001348 struct video_device *vdev = video_devdata(file);
1349 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001350 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1351
1352 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != crop->type)
1353 return -EINVAL;
1354
1355 crop->bounds.left = 0;
1356 crop->bounds.top = 0;
1357 crop->bounds.height = common->height;
1358 crop->bounds.width = common->width;
1359 crop->defrect = crop->bounds;
1360 return 0;
1361}
1362
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001363/**
Hans Verkuil0598c172012-09-18 07:18:47 -03001364 * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001365 * @file: file ptr
1366 * @priv: file handle
Hans Verkuil0598c172012-09-18 07:18:47 -03001367 * @timings: input timings
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001368 */
Hans Verkuil0598c172012-09-18 07:18:47 -03001369static int
1370vpif_enum_dv_timings(struct file *file, void *priv,
1371 struct v4l2_enum_dv_timings *timings)
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001372{
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001373 struct video_device *vdev = video_devdata(file);
1374 struct channel_obj *ch = video_get_drvdata(vdev);
Hans Verkuil178cce12012-09-20 09:06:30 -03001375 int ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001376
Hans Verkuil178cce12012-09-20 09:06:30 -03001377 ret = v4l2_subdev_call(ch->sd, video, enum_dv_timings, timings);
Wei Yongjune070f1b2012-10-30 09:45:00 -03001378 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
Hans Verkuil178cce12012-09-20 09:06:30 -03001379 return -EINVAL;
1380 return ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001381}
1382
1383/**
Hans Verkuil0598c172012-09-18 07:18:47 -03001384 * vpif_query_dv_timings() - QUERY_DV_TIMINGS handler
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001385 * @file: file ptr
1386 * @priv: file handle
Hans Verkuil0598c172012-09-18 07:18:47 -03001387 * @timings: input timings
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001388 */
Hans Verkuil0598c172012-09-18 07:18:47 -03001389static int
1390vpif_query_dv_timings(struct file *file, void *priv,
1391 struct v4l2_dv_timings *timings)
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001392{
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001393 struct video_device *vdev = video_devdata(file);
1394 struct channel_obj *ch = video_get_drvdata(vdev);
Hans Verkuil178cce12012-09-20 09:06:30 -03001395 int ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001396
Hans Verkuil178cce12012-09-20 09:06:30 -03001397 ret = v4l2_subdev_call(ch->sd, video, query_dv_timings, timings);
Wei Yongjune070f1b2012-10-30 09:45:00 -03001398 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
Hans Verkuil178cce12012-09-20 09:06:30 -03001399 return -ENODATA;
1400 return ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001401}
1402
Mats Randgaardc027e162010-12-16 12:17:44 -03001403/**
1404 * vpif_s_dv_timings() - S_DV_TIMINGS handler
1405 * @file: file ptr
1406 * @priv: file handle
1407 * @timings: digital video timings
1408 */
1409static int vpif_s_dv_timings(struct file *file, void *priv,
1410 struct v4l2_dv_timings *timings)
1411{
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001412 struct video_device *vdev = video_devdata(file);
1413 struct channel_obj *ch = video_get_drvdata(vdev);
Mats Randgaardc027e162010-12-16 12:17:44 -03001414 struct vpif_params *vpifparams = &ch->vpifparams;
1415 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
1416 struct video_obj *vid_ch = &ch->video;
Hans Verkuil0598c172012-09-18 07:18:47 -03001417 struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
Mats Randgaardc027e162010-12-16 12:17:44 -03001418 int ret;
1419
1420 if (timings->type != V4L2_DV_BT_656_1120) {
1421 vpif_dbg(2, debug, "Timing type not defined\n");
1422 return -EINVAL;
1423 }
1424
1425 /* Configure subdevice timings, if any */
Hans Verkuil178cce12012-09-20 09:06:30 -03001426 ret = v4l2_subdev_call(ch->sd, video, s_dv_timings, timings);
1427 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1428 ret = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001429 if (ret < 0) {
1430 vpif_dbg(2, debug, "Error setting custom DV timings\n");
1431 return ret;
1432 }
1433
1434 if (!(timings->bt.width && timings->bt.height &&
1435 (timings->bt.hbackporch ||
1436 timings->bt.hfrontporch ||
1437 timings->bt.hsync) &&
1438 timings->bt.vfrontporch &&
1439 (timings->bt.vbackporch ||
1440 timings->bt.vsync))) {
1441 vpif_dbg(2, debug, "Timings for width, height, "
1442 "horizontal back porch, horizontal sync, "
1443 "horizontal front porch, vertical back porch, "
1444 "vertical sync and vertical back porch "
1445 "must be defined\n");
1446 return -EINVAL;
1447 }
1448
Hans Verkuil0598c172012-09-18 07:18:47 -03001449 vid_ch->dv_timings = *timings;
Mats Randgaardc027e162010-12-16 12:17:44 -03001450
1451 /* Configure video port timings */
1452
Hans Verkuile3655262013-07-29 08:41:00 -03001453 std_info->eav2sav = V4L2_DV_BT_BLANKING_WIDTH(bt) - 8;
Mats Randgaardc027e162010-12-16 12:17:44 -03001454 std_info->sav2eav = bt->width;
1455
1456 std_info->l1 = 1;
1457 std_info->l3 = bt->vsync + bt->vbackporch + 1;
1458
Hans Verkuile3655262013-07-29 08:41:00 -03001459 std_info->vsize = V4L2_DV_BT_FRAME_HEIGHT(bt);
Mats Randgaardc027e162010-12-16 12:17:44 -03001460 if (bt->interlaced) {
1461 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
Mats Randgaardc027e162010-12-16 12:17:44 -03001462 std_info->l5 = std_info->vsize/2 -
1463 (bt->vfrontporch - 1);
1464 std_info->l7 = std_info->vsize/2 + 1;
1465 std_info->l9 = std_info->l7 + bt->il_vsync +
1466 bt->il_vbackporch + 1;
1467 std_info->l11 = std_info->vsize -
1468 (bt->il_vfrontporch - 1);
1469 } else {
1470 vpif_dbg(2, debug, "Required timing values for "
1471 "interlaced BT format missing\n");
1472 return -EINVAL;
1473 }
1474 } else {
Mats Randgaardc027e162010-12-16 12:17:44 -03001475 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
1476 }
1477 strncpy(std_info->name, "Custom timings BT656/1120", VPIF_MAX_NAME);
1478 std_info->width = bt->width;
1479 std_info->height = bt->height;
1480 std_info->frm_fmt = bt->interlaced ? 0 : 1;
1481 std_info->ycmux_mode = 0;
1482 std_info->capture_format = 0;
1483 std_info->vbi_supported = 0;
1484 std_info->hd_sd = 1;
1485 std_info->stdid = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001486
1487 vid_ch->stdid = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001488 return 0;
1489}
1490
1491/**
1492 * vpif_g_dv_timings() - G_DV_TIMINGS handler
1493 * @file: file ptr
1494 * @priv: file handle
1495 * @timings: digital video timings
1496 */
1497static int vpif_g_dv_timings(struct file *file, void *priv,
1498 struct v4l2_dv_timings *timings)
1499{
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001500 struct video_device *vdev = video_devdata(file);
1501 struct channel_obj *ch = video_get_drvdata(vdev);
Mats Randgaardc027e162010-12-16 12:17:44 -03001502 struct video_obj *vid_ch = &ch->video;
Mats Randgaardc027e162010-12-16 12:17:44 -03001503
Hans Verkuil0598c172012-09-18 07:18:47 -03001504 *timings = vid_ch->dv_timings;
Mats Randgaardc027e162010-12-16 12:17:44 -03001505
1506 return 0;
1507}
1508
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001509/*
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001510 * vpif_log_status() - Status information
1511 * @file: file ptr
1512 * @priv: file handle
1513 *
1514 * Returns zero.
1515 */
1516static int vpif_log_status(struct file *filep, void *priv)
1517{
1518 /* status for sub devices */
1519 v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1520
1521 return 0;
1522}
1523
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001524/* vpif capture ioctl operations */
1525static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
1526 .vidioc_querycap = vpif_querycap,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001527 .vidioc_enum_fmt_vid_cap = vpif_enum_fmt_vid_cap,
1528 .vidioc_g_fmt_vid_cap = vpif_g_fmt_vid_cap,
1529 .vidioc_s_fmt_vid_cap = vpif_s_fmt_vid_cap,
1530 .vidioc_try_fmt_vid_cap = vpif_try_fmt_vid_cap,
1531 .vidioc_enum_input = vpif_enum_input,
1532 .vidioc_s_input = vpif_s_input,
1533 .vidioc_g_input = vpif_g_input,
1534 .vidioc_reqbufs = vpif_reqbufs,
1535 .vidioc_querybuf = vpif_querybuf,
1536 .vidioc_querystd = vpif_querystd,
1537 .vidioc_s_std = vpif_s_std,
1538 .vidioc_g_std = vpif_g_std,
1539 .vidioc_qbuf = vpif_qbuf,
1540 .vidioc_dqbuf = vpif_dqbuf,
1541 .vidioc_streamon = vpif_streamon,
1542 .vidioc_streamoff = vpif_streamoff,
1543 .vidioc_cropcap = vpif_cropcap,
Hans Verkuil0598c172012-09-18 07:18:47 -03001544 .vidioc_enum_dv_timings = vpif_enum_dv_timings,
1545 .vidioc_query_dv_timings = vpif_query_dv_timings,
Mats Randgaardc027e162010-12-16 12:17:44 -03001546 .vidioc_s_dv_timings = vpif_s_dv_timings,
1547 .vidioc_g_dv_timings = vpif_g_dv_timings,
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001548 .vidioc_log_status = vpif_log_status,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001549};
1550
1551/* vpif file operations */
1552static struct v4l2_file_operations vpif_fops = {
1553 .owner = THIS_MODULE,
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001554 .open = v4l2_fh_open,
1555 .release = vb2_fop_release,
Hans Verkuil46656af2011-01-04 06:51:35 -03001556 .unlocked_ioctl = video_ioctl2,
Lad, Prabhakard26d26b2014-05-16 10:33:40 -03001557 .mmap = vb2_fop_mmap,
1558 .poll = vb2_fop_poll
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001559};
1560
1561/* vpif video template */
1562static struct video_device vpif_video_template = {
1563 .name = "vpif",
1564 .fops = &vpif_fops,
1565 .minor = -1,
1566 .ioctl_ops = &vpif_ioctl_ops,
1567};
1568
1569/**
1570 * initialize_vpif() - Initialize vpif data structures
1571 *
1572 * Allocate memory for data structures and initialize them
1573 */
1574static int initialize_vpif(void)
1575{
1576 int err = 0, i, j;
1577 int free_channel_objects_index;
1578
1579 /* Default number of buffers should be 3 */
1580 if ((ch0_numbuffers > 0) &&
1581 (ch0_numbuffers < config_params.min_numbuffers))
1582 ch0_numbuffers = config_params.min_numbuffers;
1583 if ((ch1_numbuffers > 0) &&
1584 (ch1_numbuffers < config_params.min_numbuffers))
1585 ch1_numbuffers = config_params.min_numbuffers;
1586
1587 /* Set buffer size to min buffers size if it is invalid */
1588 if (ch0_bufsize < config_params.min_bufsize[VPIF_CHANNEL0_VIDEO])
1589 ch0_bufsize =
1590 config_params.min_bufsize[VPIF_CHANNEL0_VIDEO];
1591 if (ch1_bufsize < config_params.min_bufsize[VPIF_CHANNEL1_VIDEO])
1592 ch1_bufsize =
1593 config_params.min_bufsize[VPIF_CHANNEL1_VIDEO];
1594
1595 config_params.numbuffers[VPIF_CHANNEL0_VIDEO] = ch0_numbuffers;
1596 config_params.numbuffers[VPIF_CHANNEL1_VIDEO] = ch1_numbuffers;
1597 if (ch0_numbuffers) {
1598 config_params.channel_bufsize[VPIF_CHANNEL0_VIDEO]
1599 = ch0_bufsize;
1600 }
1601 if (ch1_numbuffers) {
1602 config_params.channel_bufsize[VPIF_CHANNEL1_VIDEO]
1603 = ch1_bufsize;
1604 }
1605
1606 /* Allocate memory for six channel objects */
1607 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1608 vpif_obj.dev[i] =
1609 kzalloc(sizeof(*vpif_obj.dev[i]), GFP_KERNEL);
1610 /* If memory allocation fails, return error */
1611 if (!vpif_obj.dev[i]) {
1612 free_channel_objects_index = i;
1613 err = -ENOMEM;
1614 goto vpif_init_free_channel_objects;
1615 }
1616 }
1617 return 0;
1618
1619vpif_init_free_channel_objects:
1620 for (j = 0; j < free_channel_objects_index; j++)
1621 kfree(vpif_obj.dev[j]);
1622 return err;
1623}
1624
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001625static int vpif_async_bound(struct v4l2_async_notifier *notifier,
1626 struct v4l2_subdev *subdev,
1627 struct v4l2_async_subdev *asd)
1628{
1629 int i;
1630
1631 for (i = 0; i < vpif_obj.config->subdev_count; i++)
1632 if (!strcmp(vpif_obj.config->subdev_info[i].name,
1633 subdev->name)) {
1634 vpif_obj.sd[i] = subdev;
1635 return 0;
1636 }
1637
1638 return -EINVAL;
1639}
1640
1641static int vpif_probe_complete(void)
1642{
1643 struct common_obj *common;
Lad, Prabhakard26d26b2014-05-16 10:33:40 -03001644 struct video_device *vdev;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001645 struct channel_obj *ch;
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001646 struct vb2_queue *q;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001647 int i, j, err, k;
1648
1649 for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
1650 ch = vpif_obj.dev[j];
1651 ch->channel_id = j;
1652 common = &(ch->common[VPIF_VIDEO_INDEX]);
1653 spin_lock_init(&common->irqlock);
1654 mutex_init(&common->lock);
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001655
1656 /* select input 0 */
1657 err = vpif_set_input(vpif_obj.config, ch, 0);
1658 if (err)
1659 goto probe_out;
1660
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001661 /* Initialize vb2 queue */
1662 q = &common->buffer_queue;
1663 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1664 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1665 q->drv_priv = ch;
1666 q->ops = &video_qops;
1667 q->mem_ops = &vb2_dma_contig_memops;
1668 q->buf_struct_size = sizeof(struct vpif_cap_buffer);
1669 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1670 q->min_buffers_needed = 1;
Lad, Prabhakar999ba692014-05-16 10:33:33 -03001671 q->lock = &common->lock;
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001672
1673 err = vb2_queue_init(q);
1674 if (err) {
1675 vpif_err("vpif_capture: vb2_queue_init() failed\n");
1676 goto probe_out;
1677 }
1678
1679 common->alloc_ctx = vb2_dma_contig_init_ctx(vpif_dev);
1680 if (IS_ERR(common->alloc_ctx)) {
1681 vpif_err("Failed to get the context\n");
1682 err = PTR_ERR(common->alloc_ctx);
1683 goto probe_out;
1684 }
1685
1686 INIT_LIST_HEAD(&common->dma_queue);
1687
Lad, Prabhakard26d26b2014-05-16 10:33:40 -03001688 vdev = ch->video_dev;
1689 vdev->queue = q;
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001690 vdev->lock = &common->lock;
1691 set_bit(V4L2_FL_USE_FH_PRIO, &vdev->flags);
1692 video_set_drvdata(ch->video_dev, ch);
Lad, Prabhakard26d26b2014-05-16 10:33:40 -03001693 err = video_register_device(vdev,
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001694 VFL_TYPE_GRABBER, (j ? 1 : 0));
1695 if (err)
1696 goto probe_out;
1697 }
1698
1699 v4l2_info(&vpif_obj.v4l2_dev, "VPIF capture driver initialized\n");
1700 return 0;
1701
1702probe_out:
1703 for (k = 0; k < j; k++) {
1704 /* Get the pointer to the channel object */
1705 ch = vpif_obj.dev[k];
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001706 common = &ch->common[k];
1707 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001708 /* Unregister video device */
1709 video_unregister_device(ch->video_dev);
1710 }
1711 kfree(vpif_obj.sd);
1712 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1713 ch = vpif_obj.dev[i];
1714 /* Note: does nothing if ch->video_dev == NULL */
1715 video_device_release(ch->video_dev);
1716 }
1717 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1718
1719 return err;
1720}
1721
1722static int vpif_async_complete(struct v4l2_async_notifier *notifier)
1723{
1724 return vpif_probe_complete();
1725}
1726
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001727/**
1728 * vpif_probe : This function probes the vpif capture driver
1729 * @pdev: platform device pointer
1730 *
1731 * This creates device entries by register itself to the V4L2 driver and
1732 * initializes fields of each channel objects
1733 */
1734static __init int vpif_probe(struct platform_device *pdev)
1735{
1736 struct vpif_subdev_info *subdevdata;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001737 int i, j, err;
Hans Verkuil5be452c2012-09-20 09:06:29 -03001738 int res_idx = 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001739 struct i2c_adapter *i2c_adap;
1740 struct channel_obj *ch;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001741 struct video_device *vfd;
1742 struct resource *res;
1743 int subdev_count;
Manjunath Hadli764af392012-04-13 04:49:34 -03001744 size_t size;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001745
1746 vpif_dev = &pdev->dev;
1747
1748 err = initialize_vpif();
1749 if (err) {
1750 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
1751 return err;
1752 }
1753
Hans Verkuild2f7a1a2011-12-13 05:44:42 -03001754 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
1755 if (err) {
1756 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
1757 return err;
1758 }
1759
Hans Verkuil5be452c2012-09-20 09:06:29 -03001760 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
Lad, Prabhakara76a0b32013-06-17 11:20:47 -03001761 err = devm_request_irq(&pdev->dev, res->start, vpif_channel_isr,
1762 IRQF_SHARED, "VPIF_Capture",
1763 (void *)(&vpif_obj.dev[res_idx]->
1764 channel_id));
1765 if (err) {
1766 err = -EINVAL;
1767 goto vpif_unregister;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001768 }
Hans Verkuil5be452c2012-09-20 09:06:29 -03001769 res_idx++;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001770 }
1771
1772 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1773 /* Get the pointer to the channel object */
1774 ch = vpif_obj.dev[i];
1775 /* Allocate memory for video device */
1776 vfd = video_device_alloc();
1777 if (NULL == vfd) {
1778 for (j = 0; j < i; j++) {
1779 ch = vpif_obj.dev[j];
1780 video_device_release(ch->video_dev);
1781 }
1782 err = -ENOMEM;
Lad, Prabhakarb2de4f22013-06-17 11:20:46 -03001783 goto vpif_unregister;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001784 }
1785
1786 /* Initialize field of video device */
1787 *vfd = vpif_video_template;
1788 vfd->v4l2_dev = &vpif_obj.v4l2_dev;
1789 vfd->release = video_device_release;
1790 snprintf(vfd->name, sizeof(vfd->name),
Manjunath Hadli0a631722012-04-13 04:44:00 -03001791 "VPIF_Capture_DRIVER_V%s",
Mauro Carvalho Chehab64dc3c12011-06-25 11:28:37 -03001792 VPIF_CAPTURE_VERSION);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001793 /* Set video_dev to the video device */
1794 ch->video_dev = vfd;
1795 }
1796
Manjunath Hadli764af392012-04-13 04:49:34 -03001797 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1798 if (res) {
1799 size = resource_size(res);
1800 /* The resources are divided into two equal memory and when we
1801 * have HD output we can add them together
1802 */
1803 for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
1804 ch = vpif_obj.dev[j];
1805 ch->channel_id = j;
1806 /* only enabled if second resource exists */
1807 config_params.video_limit[ch->channel_id] = 0;
1808 if (size)
1809 config_params.video_limit[ch->channel_id] =
1810 size/2;
1811 }
1812 }
1813
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001814 vpif_obj.config = pdev->dev.platform_data;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001815
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001816 subdev_count = vpif_obj.config->subdev_count;
Mats Randgaard1f8766b2010-08-30 10:30:37 -03001817 vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001818 GFP_KERNEL);
1819 if (vpif_obj.sd == NULL) {
1820 vpif_err("unable to allocate memory for subdevice pointers\n");
1821 err = -ENOMEM;
Hans Verkuil5be452c2012-09-20 09:06:29 -03001822 goto vpif_sd_error;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001823 }
1824
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001825 if (!vpif_obj.config->asd_sizes) {
1826 i2c_adap = i2c_get_adapter(1);
1827 for (i = 0; i < subdev_count; i++) {
1828 subdevdata = &vpif_obj.config->subdev_info[i];
1829 vpif_obj.sd[i] =
1830 v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
1831 i2c_adap,
1832 &subdevdata->
1833 board_info,
1834 NULL);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001835
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001836 if (!vpif_obj.sd[i]) {
1837 vpif_err("Error registering v4l2 subdevice\n");
Wei Yongjun2fcd9dc2013-09-02 05:06:10 -03001838 err = -ENODEV;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001839 goto probe_subdev_out;
1840 }
1841 v4l2_info(&vpif_obj.v4l2_dev,
1842 "registered sub device %s\n",
1843 subdevdata->name);
1844 }
1845 vpif_probe_complete();
1846 } else {
Sylwester Nawrockie8419d02013-07-19 12:31:10 -03001847 vpif_obj.notifier.subdevs = vpif_obj.config->asd;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001848 vpif_obj.notifier.num_subdevs = vpif_obj.config->asd_sizes[0];
1849 vpif_obj.notifier.bound = vpif_async_bound;
1850 vpif_obj.notifier.complete = vpif_async_complete;
1851 err = v4l2_async_notifier_register(&vpif_obj.v4l2_dev,
1852 &vpif_obj.notifier);
1853 if (err) {
1854 vpif_err("Error registering async notifier\n");
1855 err = -EINVAL;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001856 goto probe_subdev_out;
1857 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001858 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001859
1860 return 0;
1861
Hans Verkuilb65814e2012-09-20 09:06:26 -03001862probe_subdev_out:
1863 /* free sub devices memory */
1864 kfree(vpif_obj.sd);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001865
Hans Verkuil5be452c2012-09-20 09:06:29 -03001866vpif_sd_error:
1867 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1868 ch = vpif_obj.dev[i];
1869 /* Note: does nothing if ch->video_dev == NULL */
1870 video_device_release(ch->video_dev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001871 }
Lad, Prabhakarb2de4f22013-06-17 11:20:46 -03001872vpif_unregister:
Hans Verkuild2f7a1a2011-12-13 05:44:42 -03001873 v4l2_device_unregister(&vpif_obj.v4l2_dev);
Lad, Prabhakarb2de4f22013-06-17 11:20:46 -03001874
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001875 return err;
1876}
1877
1878/**
1879 * vpif_remove() - driver remove handler
1880 * @device: ptr to platform device structure
1881 *
1882 * The vidoe device is unregistered
1883 */
1884static int vpif_remove(struct platform_device *device)
1885{
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001886 struct common_obj *common;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001887 struct channel_obj *ch;
Lad, Prabhakarb2de4f22013-06-17 11:20:46 -03001888 int i;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001889
1890 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1891
Lad, Prabhakar410ca602013-06-17 11:20:44 -03001892 kfree(vpif_obj.sd);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001893 /* un-register device */
1894 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1895 /* Get the pointer to the channel object */
1896 ch = vpif_obj.dev[i];
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001897 common = &ch->common[i];
1898 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001899 /* Unregister video device */
1900 video_unregister_device(ch->video_dev);
Lad, Prabhakar410ca602013-06-17 11:20:44 -03001901 kfree(vpif_obj.dev[i]);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001902 }
1903 return 0;
1904}
1905
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001906#ifdef CONFIG_PM
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001907/**
1908 * vpif_suspend: vpif device suspend
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001909 */
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001910static int vpif_suspend(struct device *dev)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001911{
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001912
1913 struct common_obj *common;
1914 struct channel_obj *ch;
1915 int i;
1916
1917 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1918 /* Get the pointer to the channel object */
1919 ch = vpif_obj.dev[i];
1920 common = &ch->common[VPIF_VIDEO_INDEX];
1921 mutex_lock(&common->lock);
1922 if (ch->usrs && common->io_usrs) {
1923 /* Disable channel */
1924 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
1925 enable_channel0(0);
1926 channel0_intr_enable(0);
1927 }
1928 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
1929 common->started == 2) {
1930 enable_channel1(0);
1931 channel1_intr_enable(0);
1932 }
1933 }
1934 mutex_unlock(&common->lock);
1935 }
1936
1937 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001938}
1939
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001940/*
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001941 * vpif_resume: vpif device suspend
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001942 */
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001943static int vpif_resume(struct device *dev)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001944{
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001945 struct common_obj *common;
1946 struct channel_obj *ch;
1947 int i;
1948
1949 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1950 /* Get the pointer to the channel object */
1951 ch = vpif_obj.dev[i];
1952 common = &ch->common[VPIF_VIDEO_INDEX];
1953 mutex_lock(&common->lock);
1954 if (ch->usrs && common->io_usrs) {
1955 /* Disable channel */
1956 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
1957 enable_channel0(1);
1958 channel0_intr_enable(1);
1959 }
1960 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
1961 common->started == 2) {
1962 enable_channel1(1);
1963 channel1_intr_enable(1);
1964 }
1965 }
1966 mutex_unlock(&common->lock);
1967 }
1968
1969 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001970}
1971
Alexey Dobriyan47145212009-12-14 18:00:08 -08001972static const struct dev_pm_ops vpif_dev_pm_ops = {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001973 .suspend = vpif_suspend,
1974 .resume = vpif_resume,
1975};
1976
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001977#define vpif_pm_ops (&vpif_dev_pm_ops)
1978#else
1979#define vpif_pm_ops NULL
1980#endif
1981
Mats Randgaardffa1b392010-08-30 10:30:36 -03001982static __refdata struct platform_driver vpif_driver = {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001983 .driver = {
1984 .name = "vpif_capture",
1985 .owner = THIS_MODULE,
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001986 .pm = vpif_pm_ops,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001987 },
1988 .probe = vpif_probe,
1989 .remove = vpif_remove,
1990};
1991
Lad, Prabhakarfe424b22013-06-17 11:20:45 -03001992module_platform_driver(vpif_driver);