blob: d8e1b98b778409785111cd177ae9207d95f8235d [file] [log] [blame]
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001/*
2 * Copyright (C) 2009 Texas Instruments Inc
Lad, Prabhakar96787eb2014-05-16 10:33:55 -03003 * Copyright (C) 2014 Lad, Prabhakar <prabhakar.csengg@gmail.com>
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * TODO : add support for VBI & HBI data service
20 * add static buffer allocation
21 */
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030022
Lad, Prabhakar012eef72013-04-19 05:53:29 -030023#include <linux/module.h>
24#include <linux/interrupt.h>
25#include <linux/platform_device.h>
26#include <linux/slab.h>
27
Lad, Prabhakar012eef72013-04-19 05:53:29 -030028#include <media/v4l2-ioctl.h>
29
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030030#include "vpif.h"
Lad, Prabhakar012eef72013-04-19 05:53:29 -030031#include "vpif_capture.h"
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030032
33MODULE_DESCRIPTION("TI DaVinci VPIF Capture driver");
34MODULE_LICENSE("GPL");
Mauro Carvalho Chehab64dc3c12011-06-25 11:28:37 -030035MODULE_VERSION(VPIF_CAPTURE_VERSION);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030036
37#define vpif_err(fmt, arg...) v4l2_err(&vpif_obj.v4l2_dev, fmt, ## arg)
38#define vpif_dbg(level, debug, fmt, arg...) \
39 v4l2_dbg(level, debug, &vpif_obj.v4l2_dev, fmt, ## arg)
40
41static int debug = 1;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030042
43module_param(debug, int, 0644);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030044
45MODULE_PARM_DESC(debug, "Debug level 0-1");
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030046
Lad, Prabhakare75ea0c2014-05-16 10:33:46 -030047#define VPIF_DRIVER_NAME "vpif_capture"
48
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030049/* global variables */
50static struct vpif_device vpif_obj = { {NULL} };
51static struct device *vpif_dev;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030052static void vpif_calculate_offsets(struct channel_obj *ch);
53static void vpif_config_addr(struct channel_obj *ch, int muxmode);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030054
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -030055static u8 channel_first_int[VPIF_NUMBER_OF_OBJECTS][2] = { {1, 1} };
56
Lad, Prabhakarc66238f2014-05-16 10:33:45 -030057/* Is set to 1 in case of SDTV formats, 2 in case of HDTV formats. */
58static int ycmux_mode;
59
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -030060static inline struct vpif_cap_buffer *to_vpif_buffer(struct vb2_buffer *vb)
61{
62 return container_of(vb, struct vpif_cap_buffer, vb);
63}
64
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030065/**
Lad, Prabhakar23e76a22014-05-16 10:33:37 -030066 * vpif_buffer_prepare : callback function for buffer prepare
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030067 * @vb: ptr to vb2_buffer
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030068 *
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030069 * This is the callback function for buffer prepare when vb2_qbuf()
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030070 * function is called. The buffer is prepared and user space virtual address
71 * or user address is converted into physical address
72 */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030073static int vpif_buffer_prepare(struct vb2_buffer *vb)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030074{
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030075 struct vb2_queue *q = vb->vb2_queue;
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -030076 struct channel_obj *ch = vb2_get_drv_priv(q);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030077 struct common_obj *common;
78 unsigned long addr;
79
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030080 vpif_dbg(2, debug, "vpif_buffer_prepare\n");
81
82 common = &ch->common[VPIF_VIDEO_INDEX];
83
Lad, Prabhakar23e76a22014-05-16 10:33:37 -030084 vb2_set_plane_payload(vb, 0, common->fmt.fmt.pix.sizeimage);
85 if (vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
86 return -EINVAL;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030087
Lad, Prabhakar23e76a22014-05-16 10:33:37 -030088 vb->v4l2_buf.field = common->fmt.fmt.pix.field;
89
90 addr = vb2_dma_contig_plane_dma_addr(vb, 0);
91 if (!IS_ALIGNED((addr + common->ytop_off), 8) ||
92 !IS_ALIGNED((addr + common->ybtm_off), 8) ||
93 !IS_ALIGNED((addr + common->ctop_off), 8) ||
94 !IS_ALIGNED((addr + common->cbtm_off), 8)) {
95 vpif_dbg(1, debug, "offset is not aligned\n");
96 return -EINVAL;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030097 }
Lad, Prabhakar23e76a22014-05-16 10:33:37 -030098
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030099 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300100}
101
102/**
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300103 * vpif_buffer_queue_setup : Callback function for buffer setup.
104 * @vq: vb2_queue ptr
105 * @fmt: v4l2 format
106 * @nbuffers: ptr to number of buffers requested by application
107 * @nplanes:: contains number of distinct video planes needed to hold a frame
108 * @sizes[]: contains the size (in bytes) of each plane.
109 * @alloc_ctxs: ptr to allocation context
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300110 *
111 * This callback function is called when reqbuf() is called to adjust
112 * the buffer count and buffer size
113 */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300114static int vpif_buffer_queue_setup(struct vb2_queue *vq,
115 const struct v4l2_format *fmt,
116 unsigned int *nbuffers, unsigned int *nplanes,
117 unsigned int sizes[], void *alloc_ctxs[])
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300118{
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -0300119 struct channel_obj *ch = vb2_get_drv_priv(vq);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300120 struct common_obj *common;
121
122 common = &ch->common[VPIF_VIDEO_INDEX];
123
124 vpif_dbg(2, debug, "vpif_buffer_setup\n");
125
Lad, Prabhakar837939d2014-05-16 10:33:38 -0300126 if (fmt && fmt->fmt.pix.sizeimage < common->fmt.fmt.pix.sizeimage)
127 return -EINVAL;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300128
Lad, Prabhakar837939d2014-05-16 10:33:38 -0300129 if (vq->num_buffers + *nbuffers < 3)
130 *nbuffers = 3 - vq->num_buffers;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300131
132 *nplanes = 1;
Lad, Prabhakar837939d2014-05-16 10:33:38 -0300133 sizes[0] = fmt ? fmt->fmt.pix.sizeimage : common->fmt.fmt.pix.sizeimage;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300134 alloc_ctxs[0] = common->alloc_ctx;
135
Lad, Prabhakar837939d2014-05-16 10:33:38 -0300136 /* Calculate the offset for Y and C data in the buffer */
137 vpif_calculate_offsets(ch);
138
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300139 return 0;
140}
141
142/**
143 * vpif_buffer_queue : Callback function to add buffer to DMA queue
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300144 * @vb: ptr to vb2_buffer
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300145 */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300146static void vpif_buffer_queue(struct vb2_buffer *vb)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300147{
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -0300148 struct channel_obj *ch = vb2_get_drv_priv(vb->vb2_queue);
149 struct vpif_cap_buffer *buf = to_vpif_buffer(vb);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300150 struct common_obj *common;
Hans Verkuilaec96832012-11-16 12:03:06 -0300151 unsigned long flags;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300152
153 common = &ch->common[VPIF_VIDEO_INDEX];
154
155 vpif_dbg(2, debug, "vpif_buffer_queue\n");
156
Hans Verkuilaec96832012-11-16 12:03:06 -0300157 spin_lock_irqsave(&common->irqlock, flags);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300158 /* add the buffer to the DMA queue */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300159 list_add_tail(&buf->list, &common->dma_queue);
Hans Verkuilaec96832012-11-16 12:03:06 -0300160 spin_unlock_irqrestore(&common->irqlock, flags);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300161}
162
Lad, Prabhakar41b9f242014-05-16 10:33:39 -0300163/**
164 * vpif_start_streaming : Starts the DMA engine for streaming
165 * @vb: ptr to vb2_buffer
166 * @count: number of buffers
167 */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300168static int vpif_start_streaming(struct vb2_queue *vq, unsigned int count)
169{
170 struct vpif_capture_config *vpif_config_data =
171 vpif_dev->platform_data;
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -0300172 struct channel_obj *ch = vb2_get_drv_priv(vq);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300173 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
174 struct vpif_params *vpif = &ch->vpifparams;
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300175 struct vpif_cap_buffer *buf, *tmp;
176 unsigned long addr, flags;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300177 int ret;
178
Hans Verkuilaec96832012-11-16 12:03:06 -0300179 spin_lock_irqsave(&common->irqlock, flags);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300180
Lad, Prabhakarc66238f2014-05-16 10:33:45 -0300181 /* Initialize field_id */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300182 ch->field_id = 0;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300183
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300184 /* configure 1 or 2 channel mode */
Lad, Prabhakarf4ad8d72012-09-27 02:33:12 -0300185 if (vpif_config_data->setup_input_channel_mode) {
186 ret = vpif_config_data->
187 setup_input_channel_mode(vpif->std_info.ycmux_mode);
188 if (ret < 0) {
189 vpif_dbg(1, debug, "can't set vpif channel mode\n");
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300190 goto err;
Lad, Prabhakarf4ad8d72012-09-27 02:33:12 -0300191 }
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300192 }
193
Lad, Prabhakard9a3b132014-05-16 10:33:42 -0300194 ret = v4l2_subdev_call(ch->sd, video, s_stream, 1);
195 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
196 vpif_dbg(1, debug, "stream on failed in subdev\n");
197 goto err;
198 }
199
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300200 /* Call vpif_set_params function to set the parameters and addresses */
201 ret = vpif_set_video_params(vpif, ch->channel_id);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300202 if (ret < 0) {
203 vpif_dbg(1, debug, "can't set video params\n");
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300204 goto err;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300205 }
206
Lad, Prabhakarc66238f2014-05-16 10:33:45 -0300207 ycmux_mode = ret;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300208 vpif_config_addr(ch, ret);
209
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300210 /* Get the next frame from the buffer queue */
211 common->cur_frm = common->next_frm = list_entry(common->dma_queue.next,
212 struct vpif_cap_buffer, list);
213 /* Remove buffer from the buffer queue */
214 list_del(&common->cur_frm->list);
215 spin_unlock_irqrestore(&common->irqlock, flags);
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300216
217 addr = vb2_dma_contig_plane_dma_addr(&common->cur_frm->vb, 0);
218
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300219 common->set_addr(addr + common->ytop_off,
220 addr + common->ybtm_off,
221 addr + common->ctop_off,
222 addr + common->cbtm_off);
223
224 /**
225 * Set interrupt for both the fields in VPIF Register enable channel in
226 * VPIF register
227 */
Lad, Prabhakar9e184042012-09-14 10:22:24 -0300228 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
Lad, Prabhakar41b9f242014-05-16 10:33:39 -0300229 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300230 channel0_intr_assert();
231 channel0_intr_enable(1);
232 enable_channel0(1);
233 }
Lad, Prabhakar41b9f242014-05-16 10:33:39 -0300234 if (VPIF_CHANNEL1_VIDEO == ch->channel_id ||
Lad, Prabhakarc66238f2014-05-16 10:33:45 -0300235 ycmux_mode == 2) {
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300236 channel1_intr_assert();
237 channel1_intr_enable(1);
238 enable_channel1(1);
239 }
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300240
241 return 0;
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300242
243err:
244 list_for_each_entry_safe(buf, tmp, &common->dma_queue, list) {
245 list_del(&buf->list);
246 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_QUEUED);
247 }
Dan Carpenter92491962014-06-12 04:01:45 -0300248 spin_unlock_irqrestore(&common->irqlock, flags);
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300249
250 return ret;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300251}
252
Lad, Prabhakar41b9f242014-05-16 10:33:39 -0300253/**
254 * vpif_stop_streaming : Stop the DMA engine
255 * @vq: ptr to vb2_queue
256 *
257 * This callback stops the DMA engine and any remaining buffers
258 * in the DMA queue are released.
259 */
Hans Verkuile37559b2014-04-17 02:47:21 -0300260static void vpif_stop_streaming(struct vb2_queue *vq)
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300261{
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -0300262 struct channel_obj *ch = vb2_get_drv_priv(vq);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300263 struct common_obj *common;
Hans Verkuilaec96832012-11-16 12:03:06 -0300264 unsigned long flags;
Lad, Prabhakard9a3b132014-05-16 10:33:42 -0300265 int ret;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300266
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300267 common = &ch->common[VPIF_VIDEO_INDEX];
268
Lad, Prabhakare6ba3db2014-03-22 08:03:07 -0300269 /* Disable channel as per its device type and channel id */
270 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
271 enable_channel0(0);
272 channel0_intr_enable(0);
273 }
Lad, Prabhakar41b9f242014-05-16 10:33:39 -0300274 if (VPIF_CHANNEL1_VIDEO == ch->channel_id ||
Lad, Prabhakarc66238f2014-05-16 10:33:45 -0300275 ycmux_mode == 2) {
Lad, Prabhakare6ba3db2014-03-22 08:03:07 -0300276 enable_channel1(0);
277 channel1_intr_enable(0);
278 }
Lad, Prabhakarc66238f2014-05-16 10:33:45 -0300279
280 ycmux_mode = 0;
Lad, Prabhakare6ba3db2014-03-22 08:03:07 -0300281
Lad, Prabhakard9a3b132014-05-16 10:33:42 -0300282 ret = v4l2_subdev_call(ch->sd, video, s_stream, 0);
283 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
284 vpif_dbg(1, debug, "stream off failed in subdev\n");
285
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300286 /* release all active buffers */
Hans Verkuilaec96832012-11-16 12:03:06 -0300287 spin_lock_irqsave(&common->irqlock, flags);
Lad, Prabhakare6ba3db2014-03-22 08:03:07 -0300288 if (common->cur_frm == common->next_frm) {
289 vb2_buffer_done(&common->cur_frm->vb, VB2_BUF_STATE_ERROR);
290 } else {
291 if (common->cur_frm != NULL)
292 vb2_buffer_done(&common->cur_frm->vb,
293 VB2_BUF_STATE_ERROR);
294 if (common->next_frm != NULL)
295 vb2_buffer_done(&common->next_frm->vb,
296 VB2_BUF_STATE_ERROR);
297 }
298
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300299 while (!list_empty(&common->dma_queue)) {
300 common->next_frm = list_entry(common->dma_queue.next,
301 struct vpif_cap_buffer, list);
302 list_del(&common->next_frm->list);
303 vb2_buffer_done(&common->next_frm->vb, VB2_BUF_STATE_ERROR);
304 }
Hans Verkuilaec96832012-11-16 12:03:06 -0300305 spin_unlock_irqrestore(&common->irqlock, flags);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300306}
307
308static struct vb2_ops video_qops = {
309 .queue_setup = vpif_buffer_queue_setup,
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300310 .buf_prepare = vpif_buffer_prepare,
311 .start_streaming = vpif_start_streaming,
312 .stop_streaming = vpif_stop_streaming,
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300313 .buf_queue = vpif_buffer_queue,
Prabhakar Ladb41a97a2014-11-26 19:42:33 -0300314 .wait_prepare = vb2_ops_wait_prepare,
315 .wait_finish = vb2_ops_wait_finish,
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300316};
317
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300318/**
319 * vpif_process_buffer_complete: process a completed buffer
320 * @common: ptr to common channel object
321 *
322 * This function time stamp the buffer and mark it as DONE. It also
323 * wake up any process waiting on the QUEUE and set the next buffer
324 * as current
325 */
326static void vpif_process_buffer_complete(struct common_obj *common)
327{
Sakari Ailus8e6057b2012-09-15 15:14:42 -0300328 v4l2_get_timestamp(&common->cur_frm->vb.v4l2_buf.timestamp);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300329 vb2_buffer_done(&common->cur_frm->vb,
330 VB2_BUF_STATE_DONE);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300331 /* Make curFrm pointing to nextFrm */
332 common->cur_frm = common->next_frm;
333}
334
335/**
336 * vpif_schedule_next_buffer: set next buffer address for capture
337 * @common : ptr to common channel object
338 *
339 * This function will get next buffer from the dma queue and
340 * set the buffer address in the vpif register for capture.
341 * the buffer is marked active
342 */
343static void vpif_schedule_next_buffer(struct common_obj *common)
344{
345 unsigned long addr = 0;
346
Hans Verkuilaec96832012-11-16 12:03:06 -0300347 spin_lock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300348 common->next_frm = list_entry(common->dma_queue.next,
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300349 struct vpif_cap_buffer, list);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300350 /* Remove that buffer from the buffer queue */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300351 list_del(&common->next_frm->list);
Hans Verkuilaec96832012-11-16 12:03:06 -0300352 spin_unlock(&common->irqlock);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300353 addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb, 0);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300354
355 /* Set top and bottom field addresses in VPIF registers */
356 common->set_addr(addr + common->ytop_off,
357 addr + common->ybtm_off,
358 addr + common->ctop_off,
359 addr + common->cbtm_off);
360}
361
362/**
363 * vpif_channel_isr : ISR handler for vpif capture
364 * @irq: irq number
365 * @dev_id: dev_id ptr
366 *
367 * It changes status of the captured buffer, takes next buffer from the queue
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300368 * and sets its address in VPIF registers
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300369 */
370static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
371{
372 struct vpif_device *dev = &vpif_obj;
373 struct common_obj *common;
374 struct channel_obj *ch;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300375 int channel_id = 0;
376 int fid = -1, i;
377
378 channel_id = *(int *)(dev_id);
Manjunath Hadlib1fc4232012-04-13 04:43:10 -0300379 if (!vpif_intr_status(channel_id))
380 return IRQ_NONE;
381
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300382 ch = dev->dev[channel_id];
383
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300384 for (i = 0; i < VPIF_NUMBER_OF_OBJECTS; i++) {
385 common = &ch->common[i];
386 /* skip If streaming is not started in this channel */
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300387 /* Check the field format */
388 if (1 == ch->vpifparams.std_info.frm_fmt) {
389 /* Progressive mode */
Hans Verkuilaec96832012-11-16 12:03:06 -0300390 spin_lock(&common->irqlock);
391 if (list_empty(&common->dma_queue)) {
392 spin_unlock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300393 continue;
Hans Verkuilaec96832012-11-16 12:03:06 -0300394 }
395 spin_unlock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300396
397 if (!channel_first_int[i][channel_id])
398 vpif_process_buffer_complete(common);
399
400 channel_first_int[i][channel_id] = 0;
401
402 vpif_schedule_next_buffer(common);
403
404
405 channel_first_int[i][channel_id] = 0;
406 } else {
407 /**
408 * Interlaced mode. If it is first interrupt, ignore
409 * it
410 */
411 if (channel_first_int[i][channel_id]) {
412 channel_first_int[i][channel_id] = 0;
413 continue;
414 }
415 if (0 == i) {
416 ch->field_id ^= 1;
417 /* Get field id from VPIF registers */
418 fid = vpif_channel_getfid(ch->channel_id);
419 if (fid != ch->field_id) {
420 /**
421 * If field id does not match stored
422 * field id, make them in sync
423 */
424 if (0 == fid)
425 ch->field_id = fid;
426 return IRQ_HANDLED;
427 }
428 }
429 /* device field id and local field id are in sync */
430 if (0 == fid) {
431 /* this is even field */
432 if (common->cur_frm == common->next_frm)
433 continue;
434
435 /* mark the current buffer as done */
436 vpif_process_buffer_complete(common);
437 } else if (1 == fid) {
438 /* odd field */
Hans Verkuilaec96832012-11-16 12:03:06 -0300439 spin_lock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300440 if (list_empty(&common->dma_queue) ||
Hans Verkuilaec96832012-11-16 12:03:06 -0300441 (common->cur_frm != common->next_frm)) {
442 spin_unlock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300443 continue;
Hans Verkuilaec96832012-11-16 12:03:06 -0300444 }
445 spin_unlock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300446
447 vpif_schedule_next_buffer(common);
448 }
449 }
450 }
451 return IRQ_HANDLED;
452}
453
454/**
455 * vpif_update_std_info() - update standard related info
456 * @ch: ptr to channel object
457 *
458 * For a given standard selected by application, update values
459 * in the device data structures
460 */
461static int vpif_update_std_info(struct channel_obj *ch)
462{
463 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
464 struct vpif_params *vpifparams = &ch->vpifparams;
465 const struct vpif_channel_config_params *config;
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300466 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300467 struct video_obj *vid_ch = &ch->video;
468 int index;
469
470 vpif_dbg(2, debug, "vpif_update_std_info\n");
471
Mats Randgaardaa444402010-12-16 12:17:42 -0300472 for (index = 0; index < vpif_ch_params_count; index++) {
Lad, Prabhakarced9b212013-03-20 01:28:27 -0300473 config = &vpif_ch_params[index];
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300474 if (config->hd_sd == 0) {
475 vpif_dbg(2, debug, "SD format\n");
476 if (config->stdid & vid_ch->stdid) {
477 memcpy(std_info, config, sizeof(*config));
478 break;
479 }
480 } else {
481 vpif_dbg(2, debug, "HD format\n");
Hans Verkuil0598c172012-09-18 07:18:47 -0300482 if (!memcmp(&config->dv_timings, &vid_ch->dv_timings,
483 sizeof(vid_ch->dv_timings))) {
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300484 memcpy(std_info, config, sizeof(*config));
485 break;
486 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300487 }
488 }
489
490 /* standard not found */
Mats Randgaardaa444402010-12-16 12:17:42 -0300491 if (index == vpif_ch_params_count)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300492 return -EINVAL;
493
494 common->fmt.fmt.pix.width = std_info->width;
495 common->width = std_info->width;
496 common->fmt.fmt.pix.height = std_info->height;
497 common->height = std_info->height;
Lad, Prabhakara4965c52014-05-16 10:33:54 -0300498 common->fmt.fmt.pix.sizeimage = common->height * common->width * 2;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300499 common->fmt.fmt.pix.bytesperline = std_info->width;
500 vpifparams->video_params.hpitch = std_info->width;
501 vpifparams->video_params.storage_mode = std_info->frm_fmt;
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300502
Lad, Prabhakara4965c52014-05-16 10:33:54 -0300503 if (vid_ch->stdid)
504 common->fmt.fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
505 else
506 common->fmt.fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
507
508 if (ch->vpifparams.std_info.frm_fmt)
509 common->fmt.fmt.pix.field = V4L2_FIELD_NONE;
510 else
511 common->fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
512
513 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER)
514 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;
515 else
516 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
517
518 common->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
519
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300520 return 0;
521}
522
523/**
524 * vpif_calculate_offsets : This function calculates buffers offsets
525 * @ch : ptr to channel object
526 *
527 * This function calculates buffer offsets for Y and C in the top and
528 * bottom field
529 */
530static void vpif_calculate_offsets(struct channel_obj *ch)
531{
Mauro Carvalho Chehab24ab6332014-08-21 15:49:06 -0500532 unsigned int hpitch, sizeimage;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300533 struct video_obj *vid_ch = &(ch->video);
534 struct vpif_params *vpifparams = &ch->vpifparams;
535 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
536 enum v4l2_field field = common->fmt.fmt.pix.field;
537
538 vpif_dbg(2, debug, "vpif_calculate_offsets\n");
539
540 if (V4L2_FIELD_ANY == field) {
541 if (vpifparams->std_info.frm_fmt)
542 vid_ch->buf_field = V4L2_FIELD_NONE;
543 else
544 vid_ch->buf_field = V4L2_FIELD_INTERLACED;
545 } else
546 vid_ch->buf_field = common->fmt.fmt.pix.field;
547
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300548 sizeimage = common->fmt.fmt.pix.sizeimage;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300549
550 hpitch = common->fmt.fmt.pix.bytesperline;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300551
552 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
553 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
554 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
555 common->ytop_off = 0;
556 common->ybtm_off = hpitch;
557 common->ctop_off = sizeimage / 2;
558 common->cbtm_off = sizeimage / 2 + hpitch;
559 } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
560 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
561 common->ytop_off = 0;
562 common->ybtm_off = sizeimage / 4;
563 common->ctop_off = sizeimage / 2;
564 common->cbtm_off = common->ctop_off + sizeimage / 4;
565 } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
566 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
567 common->ybtm_off = 0;
568 common->ytop_off = sizeimage / 4;
569 common->cbtm_off = sizeimage / 2;
570 common->ctop_off = common->cbtm_off + sizeimage / 4;
571 }
572 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
573 (V4L2_FIELD_INTERLACED == vid_ch->buf_field))
574 vpifparams->video_params.storage_mode = 1;
575 else
576 vpifparams->video_params.storage_mode = 0;
577
578 if (1 == vpifparams->std_info.frm_fmt)
579 vpifparams->video_params.hpitch =
580 common->fmt.fmt.pix.bytesperline;
581 else {
582 if ((field == V4L2_FIELD_ANY)
583 || (field == V4L2_FIELD_INTERLACED))
584 vpifparams->video_params.hpitch =
585 common->fmt.fmt.pix.bytesperline * 2;
586 else
587 vpifparams->video_params.hpitch =
588 common->fmt.fmt.pix.bytesperline;
589 }
590
591 ch->vpifparams.video_params.stdid = vpifparams->std_info.stdid;
592}
593
594/**
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300595 * vpif_get_default_field() - Get default field type based on interface
596 * @vpif_params - ptr to vpif params
597 */
598static inline enum v4l2_field vpif_get_default_field(
599 struct vpif_interface *iface)
600{
601 return (iface->if_type == VPIF_IF_RAW_BAYER) ? V4L2_FIELD_NONE :
602 V4L2_FIELD_INTERLACED;
603}
604
605/**
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300606 * vpif_config_addr() - function to configure buffer address in vpif
607 * @ch - channel ptr
608 * @muxmode - channel mux mode
609 */
610static void vpif_config_addr(struct channel_obj *ch, int muxmode)
611{
612 struct common_obj *common;
613
614 vpif_dbg(2, debug, "vpif_config_addr\n");
615
616 common = &(ch->common[VPIF_VIDEO_INDEX]);
617
618 if (VPIF_CHANNEL1_VIDEO == ch->channel_id)
619 common->set_addr = ch1_set_videobuf_addr;
620 else if (2 == muxmode)
621 common->set_addr = ch0_set_videobuf_addr_yc_nmux;
622 else
623 common->set_addr = ch0_set_videobuf_addr;
624}
625
626/**
Hans Verkuil178cce12012-09-20 09:06:30 -0300627 * vpif_input_to_subdev() - Maps input to sub device
628 * @vpif_cfg - global config ptr
629 * @chan_cfg - channel config ptr
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300630 * @input_index - Given input index from application
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300631 *
632 * lookup the sub device information for a given input index.
633 * we report all the inputs to application. inputs table also
634 * has sub device name for the each input
635 */
Hans Verkuil178cce12012-09-20 09:06:30 -0300636static int vpif_input_to_subdev(
637 struct vpif_capture_config *vpif_cfg,
638 struct vpif_capture_chan_config *chan_cfg,
639 int input_index)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300640{
Hans Verkuil178cce12012-09-20 09:06:30 -0300641 struct vpif_subdev_info *subdev_info;
642 const char *subdev_name;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300643 int i;
644
Hans Verkuil178cce12012-09-20 09:06:30 -0300645 vpif_dbg(2, debug, "vpif_input_to_subdev\n");
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300646
Hans Verkuil178cce12012-09-20 09:06:30 -0300647 subdev_name = chan_cfg->inputs[input_index].subdev_name;
648 if (subdev_name == NULL)
649 return -1;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300650
651 /* loop through the sub device list to get the sub device info */
652 for (i = 0; i < vpif_cfg->subdev_count; i++) {
653 subdev_info = &vpif_cfg->subdev_info[i];
654 if (!strcmp(subdev_info->name, subdev_name))
Hans Verkuil178cce12012-09-20 09:06:30 -0300655 return i;
656 }
657 return -1;
658}
659
660/**
661 * vpif_set_input() - Select an input
662 * @vpif_cfg - global config ptr
663 * @ch - channel
664 * @_index - Given input index from application
665 *
666 * Select the given input.
667 */
668static int vpif_set_input(
669 struct vpif_capture_config *vpif_cfg,
670 struct channel_obj *ch,
671 int index)
672{
673 struct vpif_capture_chan_config *chan_cfg =
674 &vpif_cfg->chan_config[ch->channel_id];
675 struct vpif_subdev_info *subdev_info = NULL;
676 struct v4l2_subdev *sd = NULL;
677 u32 input = 0, output = 0;
678 int sd_index;
679 int ret;
680
681 sd_index = vpif_input_to_subdev(vpif_cfg, chan_cfg, index);
682 if (sd_index >= 0) {
683 sd = vpif_obj.sd[sd_index];
684 subdev_info = &vpif_cfg->subdev_info[sd_index];
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300685 }
686
Hans Verkuil178cce12012-09-20 09:06:30 -0300687 /* first setup input path from sub device to vpif */
688 if (sd && vpif_cfg->setup_input_path) {
689 ret = vpif_cfg->setup_input_path(ch->channel_id,
690 subdev_info->name);
691 if (ret < 0) {
692 vpif_dbg(1, debug, "couldn't setup input path for the" \
693 " sub device %s, for input index %d\n",
694 subdev_info->name, index);
695 return ret;
696 }
697 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300698
Hans Verkuil178cce12012-09-20 09:06:30 -0300699 if (sd) {
700 input = chan_cfg->inputs[index].input_route;
701 output = chan_cfg->inputs[index].output_route;
702 ret = v4l2_subdev_call(sd, video, s_routing,
703 input, output, 0);
704 if (ret < 0 && ret != -ENOIOCTLCMD) {
705 vpif_dbg(1, debug, "Failed to set input\n");
706 return ret;
707 }
708 }
709 ch->input_idx = index;
710 ch->sd = sd;
711 /* copy interface parameters to vpif */
Hans Verkuil0d4f35f2012-09-20 09:06:32 -0300712 ch->vpifparams.iface = chan_cfg->vpif_if;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300713
Hans Verkuil178cce12012-09-20 09:06:30 -0300714 /* update tvnorms from the sub device input info */
715 ch->video_dev->tvnorms = chan_cfg->inputs[index].input.std;
716 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300717}
718
719/**
720 * vpif_querystd() - querystd handler
721 * @file: file ptr
722 * @priv: file handle
723 * @std_id: ptr to std id
724 *
725 * This function is called to detect standard at the selected input
726 */
727static int vpif_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
728{
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300729 struct video_device *vdev = video_devdata(file);
730 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300731 int ret = 0;
732
733 vpif_dbg(2, debug, "vpif_querystd\n");
734
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300735 /* Call querystd function of decoder device */
Hans Verkuil178cce12012-09-20 09:06:30 -0300736 ret = v4l2_subdev_call(ch->sd, video, querystd, std_id);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300737
Hans Verkuil178cce12012-09-20 09:06:30 -0300738 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
739 return -ENODATA;
740 if (ret) {
741 vpif_dbg(1, debug, "Failed to query standard for sub devices\n");
742 return ret;
743 }
744
745 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300746}
747
748/**
749 * vpif_g_std() - get STD handler
750 * @file: file ptr
751 * @priv: file handle
752 * @std_id: ptr to std id
753 */
754static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
755{
Lad, Prabhakard557b7d2014-05-16 10:33:52 -0300756 struct vpif_capture_config *config = vpif_dev->platform_data;
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300757 struct video_device *vdev = video_devdata(file);
758 struct channel_obj *ch = video_get_drvdata(vdev);
Lad, Prabhakard557b7d2014-05-16 10:33:52 -0300759 struct vpif_capture_chan_config *chan_cfg;
760 struct v4l2_input input;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300761
762 vpif_dbg(2, debug, "vpif_g_std\n");
763
Lad, Prabhakard557b7d2014-05-16 10:33:52 -0300764 if (config->chan_config[ch->channel_id].inputs == NULL)
765 return -ENODATA;
766
767 chan_cfg = &config->chan_config[ch->channel_id];
768 input = chan_cfg->inputs[ch->input_idx].input;
769 if (input.capabilities != V4L2_IN_CAP_STD)
770 return -ENODATA;
771
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300772 *std = ch->video.stdid;
773 return 0;
774}
775
776/**
777 * vpif_s_std() - set STD handler
778 * @file: file ptr
779 * @priv: file handle
780 * @std_id: ptr to std id
781 */
Hans Verkuil314527a2013-03-15 06:10:40 -0300782static int vpif_s_std(struct file *file, void *priv, v4l2_std_id std_id)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300783{
Lad, Prabhakard557b7d2014-05-16 10:33:52 -0300784 struct vpif_capture_config *config = vpif_dev->platform_data;
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300785 struct video_device *vdev = video_devdata(file);
786 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300787 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakard557b7d2014-05-16 10:33:52 -0300788 struct vpif_capture_chan_config *chan_cfg;
789 struct v4l2_input input;
790 int ret;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300791
792 vpif_dbg(2, debug, "vpif_s_std\n");
793
Lad, Prabhakard557b7d2014-05-16 10:33:52 -0300794 if (config->chan_config[ch->channel_id].inputs == NULL)
795 return -ENODATA;
796
797 chan_cfg = &config->chan_config[ch->channel_id];
798 input = chan_cfg->inputs[ch->input_idx].input;
799 if (input.capabilities != V4L2_IN_CAP_STD)
800 return -ENODATA;
801
Lad, Prabhakarc66238f2014-05-16 10:33:45 -0300802 if (vb2_is_busy(&common->buffer_queue))
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300803 return -EBUSY;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300804
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300805 /* Call encoder subdevice function to set the standard */
Hans Verkuil314527a2013-03-15 06:10:40 -0300806 ch->video.stdid = std_id;
Hans Verkuil0598c172012-09-18 07:18:47 -0300807 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300808
809 /* Get the information about the standard */
810 if (vpif_update_std_info(ch)) {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300811 vpif_err("Error getting the standard info\n");
Hans Verkuil46656af2011-01-04 06:51:35 -0300812 return -EINVAL;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300813 }
814
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300815 /* set standard in the sub device */
Laurent Pinchart8774bed2014-04-28 16:53:01 -0300816 ret = v4l2_subdev_call(ch->sd, video, s_std, std_id);
Hans Verkuil178cce12012-09-20 09:06:30 -0300817 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300818 vpif_dbg(1, debug, "Failed to set standard for sub devices\n");
Hans Verkuil178cce12012-09-20 09:06:30 -0300819 return ret;
820 }
821 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300822}
823
824/**
825 * vpif_enum_input() - ENUMINPUT handler
826 * @file: file ptr
827 * @priv: file handle
828 * @input: ptr to input structure
829 */
830static int vpif_enum_input(struct file *file, void *priv,
831 struct v4l2_input *input)
832{
833
834 struct vpif_capture_config *config = vpif_dev->platform_data;
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300835 struct video_device *vdev = video_devdata(file);
836 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300837 struct vpif_capture_chan_config *chan_cfg;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300838
839 chan_cfg = &config->chan_config[ch->channel_id];
840
Lad, Prabhakara4965c52014-05-16 10:33:54 -0300841 if (input->index >= chan_cfg->input_count)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300842 return -EINVAL;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300843
844 memcpy(input, &chan_cfg->inputs[input->index].input,
845 sizeof(*input));
846 return 0;
847}
848
849/**
850 * vpif_g_input() - Get INPUT handler
851 * @file: file ptr
852 * @priv: file handle
853 * @index: ptr to input index
854 */
855static int vpif_g_input(struct file *file, void *priv, unsigned int *index)
856{
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300857 struct video_device *vdev = video_devdata(file);
858 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300859
Hans Verkuil6f47c6c2012-09-20 09:06:22 -0300860 *index = ch->input_idx;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300861 return 0;
862}
863
864/**
865 * vpif_s_input() - Set INPUT handler
866 * @file: file ptr
867 * @priv: file handle
868 * @index: input index
869 */
870static int vpif_s_input(struct file *file, void *priv, unsigned int index)
871{
872 struct vpif_capture_config *config = vpif_dev->platform_data;
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300873 struct video_device *vdev = video_devdata(file);
874 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300875 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300876 struct vpif_capture_chan_config *chan_cfg;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300877
878 chan_cfg = &config->chan_config[ch->channel_id];
879
Hans Verkuil7aaad132012-09-20 09:06:25 -0300880 if (index >= chan_cfg->input_count)
881 return -EINVAL;
882
Lad, Prabhakarc66238f2014-05-16 10:33:45 -0300883 if (vb2_is_busy(&common->buffer_queue))
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300884 return -EBUSY;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300885
Hans Verkuil178cce12012-09-20 09:06:30 -0300886 return vpif_set_input(config, ch, index);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300887}
888
889/**
890 * vpif_enum_fmt_vid_cap() - ENUM_FMT handler
891 * @file: file ptr
892 * @priv: file handle
893 * @index: input index
894 */
895static int vpif_enum_fmt_vid_cap(struct file *file, void *priv,
896 struct v4l2_fmtdesc *fmt)
897{
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300898 struct video_device *vdev = video_devdata(file);
899 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300900
901 if (fmt->index != 0) {
902 vpif_dbg(1, debug, "Invalid format index\n");
903 return -EINVAL;
904 }
905
906 /* Fill in the information about format */
907 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER) {
908 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
909 strcpy(fmt->description, "Raw Mode -Bayer Pattern GrRBGb");
910 fmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
911 } else {
912 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
913 strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
914 fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
915 }
916 return 0;
917}
918
919/**
920 * vpif_try_fmt_vid_cap() - TRY_FMT handler
921 * @file: file ptr
922 * @priv: file handle
923 * @fmt: ptr to v4l2 format structure
924 */
925static int vpif_try_fmt_vid_cap(struct file *file, void *priv,
926 struct v4l2_format *fmt)
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 v4l2_pix_format *pixfmt = &fmt->fmt.pix;
Lad, Prabhakara4965c52014-05-16 10:33:54 -0300931 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
932 struct vpif_params *vpif_params = &ch->vpifparams;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300933
Lad, Prabhakara4965c52014-05-16 10:33:54 -0300934 /*
935 * to supress v4l-compliance warnings silently correct
936 * the pixelformat
937 */
938 if (vpif_params->iface.if_type == VPIF_IF_RAW_BAYER) {
939 if (pixfmt->pixelformat != V4L2_PIX_FMT_SBGGR8)
940 pixfmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
941 } else {
942 if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P)
943 pixfmt->pixelformat = V4L2_PIX_FMT_YUV422P;
944 }
945
946 common->fmt.fmt.pix.pixelformat = pixfmt->pixelformat;
947
948 vpif_update_std_info(ch);
949
950 pixfmt->field = common->fmt.fmt.pix.field;
951 pixfmt->colorspace = common->fmt.fmt.pix.colorspace;
952 pixfmt->bytesperline = common->fmt.fmt.pix.width;
953 pixfmt->width = common->fmt.fmt.pix.width;
954 pixfmt->height = common->fmt.fmt.pix.height;
955 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height * 2;
956 pixfmt->priv = 0;
957
958 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300959}
960
961
962/**
963 * vpif_g_fmt_vid_cap() - Set INPUT handler
964 * @file: file ptr
965 * @priv: file handle
966 * @fmt: ptr to v4l2 format structure
967 */
968static int vpif_g_fmt_vid_cap(struct file *file, void *priv,
969 struct v4l2_format *fmt)
970{
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300971 struct video_device *vdev = video_devdata(file);
972 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300973 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
974
975 /* Check the validity of the buffer type */
976 if (common->fmt.type != fmt->type)
977 return -EINVAL;
978
979 /* Fill in the information about format */
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300980 *fmt = common->fmt;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300981 return 0;
982}
983
984/**
985 * vpif_s_fmt_vid_cap() - Set FMT handler
986 * @file: file ptr
987 * @priv: file handle
988 * @fmt: ptr to v4l2 format structure
989 */
990static int vpif_s_fmt_vid_cap(struct file *file, void *priv,
991 struct v4l2_format *fmt)
992{
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300993 struct video_device *vdev = video_devdata(file);
994 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300995 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakara4965c52014-05-16 10:33:54 -0300996 int ret;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300997
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300998 vpif_dbg(2, debug, "%s\n", __func__);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300999
Lad, Prabhakarc66238f2014-05-16 10:33:45 -03001000 if (vb2_is_busy(&common->buffer_queue))
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001001 return -EBUSY;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001002
Lad, Prabhakara4965c52014-05-16 10:33:54 -03001003 ret = vpif_try_fmt_vid_cap(file, priv, fmt);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001004 if (ret)
1005 return ret;
Lad, Prabhakara4965c52014-05-16 10:33:54 -03001006
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001007 /* store the format in the channel object */
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001008 common->fmt = *fmt;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001009 return 0;
1010}
1011
1012/**
1013 * vpif_querycap() - QUERYCAP handler
1014 * @file: file ptr
1015 * @priv: file handle
1016 * @cap: ptr to v4l2_capability structure
1017 */
1018static int vpif_querycap(struct file *file, void *priv,
1019 struct v4l2_capability *cap)
1020{
1021 struct vpif_capture_config *config = vpif_dev->platform_data;
1022
Lad, Prabhakar626d533f2012-09-25 11:21:55 -03001023 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1024 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
Lad, Prabhakare75ea0c2014-05-16 10:33:46 -03001025 strlcpy(cap->driver, VPIF_DRIVER_NAME, sizeof(cap->driver));
Lad, Prabhakar626d533f2012-09-25 11:21:55 -03001026 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
1027 dev_name(vpif_dev));
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001028 strlcpy(cap->card, config->card_name, sizeof(cap->card));
1029
1030 return 0;
1031}
1032
1033/**
Hans Verkuil0598c172012-09-18 07:18:47 -03001034 * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001035 * @file: file ptr
1036 * @priv: file handle
Hans Verkuil0598c172012-09-18 07:18:47 -03001037 * @timings: input timings
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001038 */
Hans Verkuil0598c172012-09-18 07:18:47 -03001039static int
1040vpif_enum_dv_timings(struct file *file, void *priv,
1041 struct v4l2_enum_dv_timings *timings)
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001042{
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001043 struct vpif_capture_config *config = vpif_dev->platform_data;
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001044 struct video_device *vdev = video_devdata(file);
1045 struct channel_obj *ch = video_get_drvdata(vdev);
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001046 struct vpif_capture_chan_config *chan_cfg;
1047 struct v4l2_input input;
Hans Verkuil178cce12012-09-20 09:06:30 -03001048 int ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001049
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001050 if (config->chan_config[ch->channel_id].inputs == NULL)
1051 return -ENODATA;
1052
1053 chan_cfg = &config->chan_config[ch->channel_id];
1054 input = chan_cfg->inputs[ch->input_idx].input;
1055 if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
1056 return -ENODATA;
1057
Laurent Pinchart10d21462014-01-31 09:04:19 -03001058 timings->pad = 0;
1059
1060 ret = v4l2_subdev_call(ch->sd, pad, enum_dv_timings, timings);
Wei Yongjune070f1b2012-10-30 09:45:00 -03001061 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
Hans Verkuil178cce12012-09-20 09:06:30 -03001062 return -EINVAL;
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001063
Hans Verkuil178cce12012-09-20 09:06:30 -03001064 return ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001065}
1066
1067/**
Hans Verkuil0598c172012-09-18 07:18:47 -03001068 * vpif_query_dv_timings() - QUERY_DV_TIMINGS handler
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001069 * @file: file ptr
1070 * @priv: file handle
Hans Verkuil0598c172012-09-18 07:18:47 -03001071 * @timings: input timings
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001072 */
Hans Verkuil0598c172012-09-18 07:18:47 -03001073static int
1074vpif_query_dv_timings(struct file *file, void *priv,
1075 struct v4l2_dv_timings *timings)
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001076{
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001077 struct vpif_capture_config *config = vpif_dev->platform_data;
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001078 struct video_device *vdev = video_devdata(file);
1079 struct channel_obj *ch = video_get_drvdata(vdev);
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001080 struct vpif_capture_chan_config *chan_cfg;
1081 struct v4l2_input input;
Hans Verkuil178cce12012-09-20 09:06:30 -03001082 int ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001083
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001084 if (config->chan_config[ch->channel_id].inputs == NULL)
1085 return -ENODATA;
1086
1087 chan_cfg = &config->chan_config[ch->channel_id];
1088 input = chan_cfg->inputs[ch->input_idx].input;
1089 if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
1090 return -ENODATA;
1091
Hans Verkuil178cce12012-09-20 09:06:30 -03001092 ret = v4l2_subdev_call(ch->sd, video, query_dv_timings, timings);
Wei Yongjune070f1b2012-10-30 09:45:00 -03001093 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
Hans Verkuil178cce12012-09-20 09:06:30 -03001094 return -ENODATA;
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001095
Hans Verkuil178cce12012-09-20 09:06:30 -03001096 return ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001097}
1098
Mats Randgaardc027e162010-12-16 12:17:44 -03001099/**
1100 * vpif_s_dv_timings() - S_DV_TIMINGS handler
1101 * @file: file ptr
1102 * @priv: file handle
1103 * @timings: digital video timings
1104 */
1105static int vpif_s_dv_timings(struct file *file, void *priv,
1106 struct v4l2_dv_timings *timings)
1107{
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001108 struct vpif_capture_config *config = vpif_dev->platform_data;
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001109 struct video_device *vdev = video_devdata(file);
1110 struct channel_obj *ch = video_get_drvdata(vdev);
Mats Randgaardc027e162010-12-16 12:17:44 -03001111 struct vpif_params *vpifparams = &ch->vpifparams;
1112 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001113 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
Mats Randgaardc027e162010-12-16 12:17:44 -03001114 struct video_obj *vid_ch = &ch->video;
Hans Verkuil0598c172012-09-18 07:18:47 -03001115 struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001116 struct vpif_capture_chan_config *chan_cfg;
1117 struct v4l2_input input;
Mats Randgaardc027e162010-12-16 12:17:44 -03001118 int ret;
1119
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001120 if (config->chan_config[ch->channel_id].inputs == NULL)
1121 return -ENODATA;
1122
1123 chan_cfg = &config->chan_config[ch->channel_id];
1124 input = chan_cfg->inputs[ch->input_idx].input;
1125 if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
1126 return -ENODATA;
1127
Mats Randgaardc027e162010-12-16 12:17:44 -03001128 if (timings->type != V4L2_DV_BT_656_1120) {
1129 vpif_dbg(2, debug, "Timing type not defined\n");
1130 return -EINVAL;
1131 }
1132
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001133 if (vb2_is_busy(&common->buffer_queue))
1134 return -EBUSY;
1135
Mats Randgaardc027e162010-12-16 12:17:44 -03001136 /* Configure subdevice timings, if any */
Hans Verkuil178cce12012-09-20 09:06:30 -03001137 ret = v4l2_subdev_call(ch->sd, video, s_dv_timings, timings);
1138 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1139 ret = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001140 if (ret < 0) {
1141 vpif_dbg(2, debug, "Error setting custom DV timings\n");
1142 return ret;
1143 }
1144
1145 if (!(timings->bt.width && timings->bt.height &&
1146 (timings->bt.hbackporch ||
1147 timings->bt.hfrontporch ||
1148 timings->bt.hsync) &&
1149 timings->bt.vfrontporch &&
1150 (timings->bt.vbackporch ||
1151 timings->bt.vsync))) {
1152 vpif_dbg(2, debug, "Timings for width, height, "
1153 "horizontal back porch, horizontal sync, "
1154 "horizontal front porch, vertical back porch, "
1155 "vertical sync and vertical back porch "
1156 "must be defined\n");
1157 return -EINVAL;
1158 }
1159
Hans Verkuil0598c172012-09-18 07:18:47 -03001160 vid_ch->dv_timings = *timings;
Mats Randgaardc027e162010-12-16 12:17:44 -03001161
1162 /* Configure video port timings */
1163
Hans Verkuile3655262013-07-29 08:41:00 -03001164 std_info->eav2sav = V4L2_DV_BT_BLANKING_WIDTH(bt) - 8;
Mats Randgaardc027e162010-12-16 12:17:44 -03001165 std_info->sav2eav = bt->width;
1166
1167 std_info->l1 = 1;
1168 std_info->l3 = bt->vsync + bt->vbackporch + 1;
1169
Hans Verkuile3655262013-07-29 08:41:00 -03001170 std_info->vsize = V4L2_DV_BT_FRAME_HEIGHT(bt);
Mats Randgaardc027e162010-12-16 12:17:44 -03001171 if (bt->interlaced) {
1172 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
Mats Randgaardc027e162010-12-16 12:17:44 -03001173 std_info->l5 = std_info->vsize/2 -
1174 (bt->vfrontporch - 1);
1175 std_info->l7 = std_info->vsize/2 + 1;
1176 std_info->l9 = std_info->l7 + bt->il_vsync +
1177 bt->il_vbackporch + 1;
1178 std_info->l11 = std_info->vsize -
1179 (bt->il_vfrontporch - 1);
1180 } else {
1181 vpif_dbg(2, debug, "Required timing values for "
1182 "interlaced BT format missing\n");
1183 return -EINVAL;
1184 }
1185 } else {
Mats Randgaardc027e162010-12-16 12:17:44 -03001186 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
1187 }
1188 strncpy(std_info->name, "Custom timings BT656/1120", VPIF_MAX_NAME);
1189 std_info->width = bt->width;
1190 std_info->height = bt->height;
1191 std_info->frm_fmt = bt->interlaced ? 0 : 1;
1192 std_info->ycmux_mode = 0;
1193 std_info->capture_format = 0;
1194 std_info->vbi_supported = 0;
1195 std_info->hd_sd = 1;
1196 std_info->stdid = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001197
1198 vid_ch->stdid = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001199 return 0;
1200}
1201
1202/**
1203 * vpif_g_dv_timings() - G_DV_TIMINGS handler
1204 * @file: file ptr
1205 * @priv: file handle
1206 * @timings: digital video timings
1207 */
1208static int vpif_g_dv_timings(struct file *file, void *priv,
1209 struct v4l2_dv_timings *timings)
1210{
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001211 struct vpif_capture_config *config = vpif_dev->platform_data;
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001212 struct video_device *vdev = video_devdata(file);
1213 struct channel_obj *ch = video_get_drvdata(vdev);
Mats Randgaardc027e162010-12-16 12:17:44 -03001214 struct video_obj *vid_ch = &ch->video;
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001215 struct vpif_capture_chan_config *chan_cfg;
1216 struct v4l2_input input;
1217
1218 if (config->chan_config[ch->channel_id].inputs == NULL)
1219 return -ENODATA;
1220
1221 chan_cfg = &config->chan_config[ch->channel_id];
1222 input = chan_cfg->inputs[ch->input_idx].input;
1223 if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
1224 return -ENODATA;
Mats Randgaardc027e162010-12-16 12:17:44 -03001225
Hans Verkuil0598c172012-09-18 07:18:47 -03001226 *timings = vid_ch->dv_timings;
Mats Randgaardc027e162010-12-16 12:17:44 -03001227
1228 return 0;
1229}
1230
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001231/*
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001232 * vpif_log_status() - Status information
1233 * @file: file ptr
1234 * @priv: file handle
1235 *
1236 * Returns zero.
1237 */
1238static int vpif_log_status(struct file *filep, void *priv)
1239{
1240 /* status for sub devices */
1241 v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1242
1243 return 0;
1244}
1245
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001246/* vpif capture ioctl operations */
1247static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
Lad, Prabhakar7b4657f2014-05-16 10:33:49 -03001248 .vidioc_querycap = vpif_querycap,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001249 .vidioc_enum_fmt_vid_cap = vpif_enum_fmt_vid_cap,
Lad, Prabhakar7b4657f2014-05-16 10:33:49 -03001250 .vidioc_g_fmt_vid_cap = vpif_g_fmt_vid_cap,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001251 .vidioc_s_fmt_vid_cap = vpif_s_fmt_vid_cap,
1252 .vidioc_try_fmt_vid_cap = vpif_try_fmt_vid_cap,
Lad, Prabhakar7b4657f2014-05-16 10:33:49 -03001253
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001254 .vidioc_enum_input = vpif_enum_input,
1255 .vidioc_s_input = vpif_s_input,
1256 .vidioc_g_input = vpif_g_input,
Lad, Prabhakard9a3b132014-05-16 10:33:42 -03001257
1258 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1259 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1260 .vidioc_querybuf = vb2_ioctl_querybuf,
1261 .vidioc_qbuf = vb2_ioctl_qbuf,
1262 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1263 .vidioc_expbuf = vb2_ioctl_expbuf,
1264 .vidioc_streamon = vb2_ioctl_streamon,
1265 .vidioc_streamoff = vb2_ioctl_streamoff,
1266
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001267 .vidioc_querystd = vpif_querystd,
Lad, Prabhakar7b4657f2014-05-16 10:33:49 -03001268 .vidioc_s_std = vpif_s_std,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001269 .vidioc_g_std = vpif_g_std,
Lad, Prabhakard9a3b132014-05-16 10:33:42 -03001270
Lad, Prabhakar7b4657f2014-05-16 10:33:49 -03001271 .vidioc_enum_dv_timings = vpif_enum_dv_timings,
1272 .vidioc_query_dv_timings = vpif_query_dv_timings,
1273 .vidioc_s_dv_timings = vpif_s_dv_timings,
1274 .vidioc_g_dv_timings = vpif_g_dv_timings,
1275
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001276 .vidioc_log_status = vpif_log_status,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001277};
1278
1279/* vpif file operations */
1280static struct v4l2_file_operations vpif_fops = {
1281 .owner = THIS_MODULE,
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001282 .open = v4l2_fh_open,
1283 .release = vb2_fop_release,
Hans Verkuil46656af2011-01-04 06:51:35 -03001284 .unlocked_ioctl = video_ioctl2,
Lad, Prabhakard26d26b2014-05-16 10:33:40 -03001285 .mmap = vb2_fop_mmap,
1286 .poll = vb2_fop_poll
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001287};
1288
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001289/**
1290 * initialize_vpif() - Initialize vpif data structures
1291 *
1292 * Allocate memory for data structures and initialize them
1293 */
1294static int initialize_vpif(void)
1295{
Lad, Prabhakar4015bef2014-05-16 10:33:47 -03001296 int err, i, j;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001297 int free_channel_objects_index;
1298
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001299 /* Allocate memory for six channel objects */
1300 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1301 vpif_obj.dev[i] =
1302 kzalloc(sizeof(*vpif_obj.dev[i]), GFP_KERNEL);
1303 /* If memory allocation fails, return error */
1304 if (!vpif_obj.dev[i]) {
1305 free_channel_objects_index = i;
1306 err = -ENOMEM;
1307 goto vpif_init_free_channel_objects;
1308 }
1309 }
1310 return 0;
1311
1312vpif_init_free_channel_objects:
1313 for (j = 0; j < free_channel_objects_index; j++)
1314 kfree(vpif_obj.dev[j]);
1315 return err;
1316}
1317
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001318static int vpif_async_bound(struct v4l2_async_notifier *notifier,
1319 struct v4l2_subdev *subdev,
1320 struct v4l2_async_subdev *asd)
1321{
1322 int i;
1323
1324 for (i = 0; i < vpif_obj.config->subdev_count; i++)
1325 if (!strcmp(vpif_obj.config->subdev_info[i].name,
1326 subdev->name)) {
1327 vpif_obj.sd[i] = subdev;
1328 return 0;
1329 }
1330
1331 return -EINVAL;
1332}
1333
1334static int vpif_probe_complete(void)
1335{
1336 struct common_obj *common;
Lad, Prabhakard26d26b2014-05-16 10:33:40 -03001337 struct video_device *vdev;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001338 struct channel_obj *ch;
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001339 struct vb2_queue *q;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001340 int i, j, err, k;
1341
1342 for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
1343 ch = vpif_obj.dev[j];
1344 ch->channel_id = j;
1345 common = &(ch->common[VPIF_VIDEO_INDEX]);
1346 spin_lock_init(&common->irqlock);
1347 mutex_init(&common->lock);
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001348
1349 /* select input 0 */
1350 err = vpif_set_input(vpif_obj.config, ch, 0);
1351 if (err)
1352 goto probe_out;
1353
Lad, Prabhakara4965c52014-05-16 10:33:54 -03001354 /* set initial format */
1355 ch->video.stdid = V4L2_STD_525_60;
1356 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
1357 vpif_update_std_info(ch);
1358
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001359 /* Initialize vb2 queue */
1360 q = &common->buffer_queue;
1361 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1362 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1363 q->drv_priv = ch;
1364 q->ops = &video_qops;
1365 q->mem_ops = &vb2_dma_contig_memops;
1366 q->buf_struct_size = sizeof(struct vpif_cap_buffer);
1367 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1368 q->min_buffers_needed = 1;
Lad, Prabhakar999ba692014-05-16 10:33:33 -03001369 q->lock = &common->lock;
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001370
1371 err = vb2_queue_init(q);
1372 if (err) {
1373 vpif_err("vpif_capture: vb2_queue_init() failed\n");
1374 goto probe_out;
1375 }
1376
1377 common->alloc_ctx = vb2_dma_contig_init_ctx(vpif_dev);
1378 if (IS_ERR(common->alloc_ctx)) {
1379 vpif_err("Failed to get the context\n");
1380 err = PTR_ERR(common->alloc_ctx);
1381 goto probe_out;
1382 }
1383
1384 INIT_LIST_HEAD(&common->dma_queue);
1385
Lad, Prabhakare75ea0c2014-05-16 10:33:46 -03001386 /* Initialize the video_device structure */
Lad, Prabhakard26d26b2014-05-16 10:33:40 -03001387 vdev = ch->video_dev;
Lad, Prabhakare75ea0c2014-05-16 10:33:46 -03001388 strlcpy(vdev->name, VPIF_DRIVER_NAME, sizeof(vdev->name));
1389 vdev->release = video_device_release;
1390 vdev->fops = &vpif_fops;
1391 vdev->ioctl_ops = &vpif_ioctl_ops;
1392 vdev->v4l2_dev = &vpif_obj.v4l2_dev;
1393 vdev->vfl_dir = VFL_DIR_RX;
Lad, Prabhakard26d26b2014-05-16 10:33:40 -03001394 vdev->queue = q;
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001395 vdev->lock = &common->lock;
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001396 video_set_drvdata(ch->video_dev, ch);
Lad, Prabhakard26d26b2014-05-16 10:33:40 -03001397 err = video_register_device(vdev,
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001398 VFL_TYPE_GRABBER, (j ? 1 : 0));
1399 if (err)
1400 goto probe_out;
1401 }
1402
1403 v4l2_info(&vpif_obj.v4l2_dev, "VPIF capture driver initialized\n");
1404 return 0;
1405
1406probe_out:
1407 for (k = 0; k < j; k++) {
1408 /* Get the pointer to the channel object */
1409 ch = vpif_obj.dev[k];
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001410 common = &ch->common[k];
1411 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001412 /* Unregister video device */
1413 video_unregister_device(ch->video_dev);
1414 }
1415 kfree(vpif_obj.sd);
1416 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1417 ch = vpif_obj.dev[i];
1418 /* Note: does nothing if ch->video_dev == NULL */
1419 video_device_release(ch->video_dev);
1420 }
1421 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1422
1423 return err;
1424}
1425
1426static int vpif_async_complete(struct v4l2_async_notifier *notifier)
1427{
1428 return vpif_probe_complete();
1429}
1430
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001431/**
1432 * vpif_probe : This function probes the vpif capture driver
1433 * @pdev: platform device pointer
1434 *
1435 * This creates device entries by register itself to the V4L2 driver and
1436 * initializes fields of each channel objects
1437 */
1438static __init int vpif_probe(struct platform_device *pdev)
1439{
1440 struct vpif_subdev_info *subdevdata;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001441 int i, j, err;
Hans Verkuil5be452c2012-09-20 09:06:29 -03001442 int res_idx = 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001443 struct i2c_adapter *i2c_adap;
1444 struct channel_obj *ch;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001445 struct video_device *vfd;
1446 struct resource *res;
1447 int subdev_count;
1448
1449 vpif_dev = &pdev->dev;
1450
1451 err = initialize_vpif();
1452 if (err) {
1453 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
1454 return err;
1455 }
1456
Hans Verkuild2f7a1a2011-12-13 05:44:42 -03001457 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
1458 if (err) {
1459 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
1460 return err;
1461 }
1462
Hans Verkuil5be452c2012-09-20 09:06:29 -03001463 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
Lad, Prabhakara76a0b32013-06-17 11:20:47 -03001464 err = devm_request_irq(&pdev->dev, res->start, vpif_channel_isr,
Lad, Prabhakare75ea0c2014-05-16 10:33:46 -03001465 IRQF_SHARED, VPIF_DRIVER_NAME,
Lad, Prabhakara76a0b32013-06-17 11:20:47 -03001466 (void *)(&vpif_obj.dev[res_idx]->
1467 channel_id));
1468 if (err) {
1469 err = -EINVAL;
1470 goto vpif_unregister;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001471 }
Hans Verkuil5be452c2012-09-20 09:06:29 -03001472 res_idx++;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001473 }
1474
1475 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1476 /* Get the pointer to the channel object */
1477 ch = vpif_obj.dev[i];
1478 /* Allocate memory for video device */
1479 vfd = video_device_alloc();
1480 if (NULL == vfd) {
1481 for (j = 0; j < i; j++) {
1482 ch = vpif_obj.dev[j];
1483 video_device_release(ch->video_dev);
1484 }
1485 err = -ENOMEM;
Lad, Prabhakarb2de4f22013-06-17 11:20:46 -03001486 goto vpif_unregister;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001487 }
1488
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001489 /* Set video_dev to the video device */
1490 ch->video_dev = vfd;
1491 }
1492
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001493 vpif_obj.config = pdev->dev.platform_data;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001494
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001495 subdev_count = vpif_obj.config->subdev_count;
Mats Randgaard1f8766b2010-08-30 10:30:37 -03001496 vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001497 GFP_KERNEL);
1498 if (vpif_obj.sd == NULL) {
1499 vpif_err("unable to allocate memory for subdevice pointers\n");
1500 err = -ENOMEM;
Hans Verkuil5be452c2012-09-20 09:06:29 -03001501 goto vpif_sd_error;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001502 }
1503
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001504 if (!vpif_obj.config->asd_sizes) {
1505 i2c_adap = i2c_get_adapter(1);
1506 for (i = 0; i < subdev_count; i++) {
1507 subdevdata = &vpif_obj.config->subdev_info[i];
1508 vpif_obj.sd[i] =
1509 v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
1510 i2c_adap,
1511 &subdevdata->
1512 board_info,
1513 NULL);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001514
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001515 if (!vpif_obj.sd[i]) {
1516 vpif_err("Error registering v4l2 subdevice\n");
Wei Yongjun2fcd9dc2013-09-02 05:06:10 -03001517 err = -ENODEV;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001518 goto probe_subdev_out;
1519 }
1520 v4l2_info(&vpif_obj.v4l2_dev,
1521 "registered sub device %s\n",
1522 subdevdata->name);
1523 }
1524 vpif_probe_complete();
1525 } else {
Sylwester Nawrockie8419d02013-07-19 12:31:10 -03001526 vpif_obj.notifier.subdevs = vpif_obj.config->asd;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001527 vpif_obj.notifier.num_subdevs = vpif_obj.config->asd_sizes[0];
1528 vpif_obj.notifier.bound = vpif_async_bound;
1529 vpif_obj.notifier.complete = vpif_async_complete;
1530 err = v4l2_async_notifier_register(&vpif_obj.v4l2_dev,
1531 &vpif_obj.notifier);
1532 if (err) {
1533 vpif_err("Error registering async notifier\n");
1534 err = -EINVAL;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001535 goto probe_subdev_out;
1536 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001537 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001538
1539 return 0;
1540
Hans Verkuilb65814e2012-09-20 09:06:26 -03001541probe_subdev_out:
1542 /* free sub devices memory */
1543 kfree(vpif_obj.sd);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001544
Hans Verkuil5be452c2012-09-20 09:06:29 -03001545vpif_sd_error:
1546 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1547 ch = vpif_obj.dev[i];
1548 /* Note: does nothing if ch->video_dev == NULL */
1549 video_device_release(ch->video_dev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001550 }
Lad, Prabhakarb2de4f22013-06-17 11:20:46 -03001551vpif_unregister:
Hans Verkuild2f7a1a2011-12-13 05:44:42 -03001552 v4l2_device_unregister(&vpif_obj.v4l2_dev);
Lad, Prabhakarb2de4f22013-06-17 11:20:46 -03001553
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001554 return err;
1555}
1556
1557/**
1558 * vpif_remove() - driver remove handler
1559 * @device: ptr to platform device structure
1560 *
1561 * The vidoe device is unregistered
1562 */
1563static int vpif_remove(struct platform_device *device)
1564{
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001565 struct common_obj *common;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001566 struct channel_obj *ch;
Lad, Prabhakarb2de4f22013-06-17 11:20:46 -03001567 int i;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001568
1569 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1570
Lad, Prabhakar410ca602013-06-17 11:20:44 -03001571 kfree(vpif_obj.sd);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001572 /* un-register device */
1573 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1574 /* Get the pointer to the channel object */
1575 ch = vpif_obj.dev[i];
Prabhakar Lade9b58722014-07-18 13:31:51 -03001576 common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001577 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001578 /* Unregister video device */
1579 video_unregister_device(ch->video_dev);
Lad, Prabhakar410ca602013-06-17 11:20:44 -03001580 kfree(vpif_obj.dev[i]);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001581 }
1582 return 0;
1583}
1584
Lad, Prabhakarb7047712014-05-16 10:33:50 -03001585#ifdef CONFIG_PM_SLEEP
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001586/**
1587 * vpif_suspend: vpif device suspend
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001588 */
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001589static int vpif_suspend(struct device *dev)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001590{
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001591
1592 struct common_obj *common;
1593 struct channel_obj *ch;
1594 int i;
1595
1596 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1597 /* Get the pointer to the channel object */
1598 ch = vpif_obj.dev[i];
1599 common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakarb7047712014-05-16 10:33:50 -03001600
Prabhakar Ladc54d4a02014-09-06 12:26:51 -03001601 if (!vb2_start_streaming_called(&common->buffer_queue))
Lad, Prabhakarb7047712014-05-16 10:33:50 -03001602 continue;
1603
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001604 mutex_lock(&common->lock);
Lad, Prabhakarb7047712014-05-16 10:33:50 -03001605 /* Disable channel */
1606 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
1607 enable_channel0(0);
1608 channel0_intr_enable(0);
1609 }
1610 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
1611 ycmux_mode == 2) {
1612 enable_channel1(0);
1613 channel1_intr_enable(0);
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001614 }
1615 mutex_unlock(&common->lock);
1616 }
1617
1618 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001619}
1620
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001621/*
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001622 * vpif_resume: vpif device suspend
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001623 */
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001624static int vpif_resume(struct device *dev)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001625{
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001626 struct common_obj *common;
1627 struct channel_obj *ch;
1628 int i;
1629
1630 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1631 /* Get the pointer to the channel object */
1632 ch = vpif_obj.dev[i];
1633 common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakarb7047712014-05-16 10:33:50 -03001634
Prabhakar Ladc54d4a02014-09-06 12:26:51 -03001635 if (!vb2_start_streaming_called(&common->buffer_queue))
Lad, Prabhakarb7047712014-05-16 10:33:50 -03001636 continue;
1637
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001638 mutex_lock(&common->lock);
Lad, Prabhakarb7047712014-05-16 10:33:50 -03001639 /* Enable channel */
1640 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
1641 enable_channel0(1);
1642 channel0_intr_enable(1);
1643 }
1644 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
1645 ycmux_mode == 2) {
1646 enable_channel1(1);
1647 channel1_intr_enable(1);
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001648 }
1649 mutex_unlock(&common->lock);
1650 }
1651
1652 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001653}
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001654#endif
1655
Lad, Prabhakarb7047712014-05-16 10:33:50 -03001656static SIMPLE_DEV_PM_OPS(vpif_pm_ops, vpif_suspend, vpif_resume);
1657
Mats Randgaardffa1b392010-08-30 10:30:36 -03001658static __refdata struct platform_driver vpif_driver = {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001659 .driver = {
Lad, Prabhakare75ea0c2014-05-16 10:33:46 -03001660 .name = VPIF_DRIVER_NAME,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001661 .owner = THIS_MODULE,
Lad, Prabhakarb7047712014-05-16 10:33:50 -03001662 .pm = &vpif_pm_ops,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001663 },
1664 .probe = vpif_probe,
1665 .remove = vpif_remove,
1666};
1667
Lad, Prabhakarfe424b22013-06-17 11:20:45 -03001668module_platform_driver(vpif_driver);