blob: 3a85238afee372764ba960c5289d3c4b37ac269c [file] [log] [blame]
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001/*
2 * Copyright (C) 2009 Texas Instruments Inc
Lad, Prabhakar96787eb2014-05-16 10:33:55 -03003 * Copyright (C) 2014 Lad, Prabhakar <prabhakar.csengg@gmail.com>
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * TODO : add support for VBI & HBI data service
20 * add static buffer allocation
21 */
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030022
Lad, Prabhakar012eef72013-04-19 05:53:29 -030023#include <linux/module.h>
24#include <linux/interrupt.h>
25#include <linux/platform_device.h>
26#include <linux/slab.h>
27
Lad, Prabhakar012eef72013-04-19 05:53:29 -030028#include <media/v4l2-ioctl.h>
29
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030030#include "vpif.h"
Lad, Prabhakar012eef72013-04-19 05:53:29 -030031#include "vpif_capture.h"
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030032
33MODULE_DESCRIPTION("TI DaVinci VPIF Capture driver");
34MODULE_LICENSE("GPL");
Mauro Carvalho Chehab64dc3c12011-06-25 11:28:37 -030035MODULE_VERSION(VPIF_CAPTURE_VERSION);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030036
37#define vpif_err(fmt, arg...) v4l2_err(&vpif_obj.v4l2_dev, fmt, ## arg)
38#define vpif_dbg(level, debug, fmt, arg...) \
39 v4l2_dbg(level, debug, &vpif_obj.v4l2_dev, fmt, ## arg)
40
41static int debug = 1;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030042
43module_param(debug, int, 0644);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030044
45MODULE_PARM_DESC(debug, "Debug level 0-1");
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030046
Lad, Prabhakare75ea0c2014-05-16 10:33:46 -030047#define VPIF_DRIVER_NAME "vpif_capture"
48
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030049/* global variables */
50static struct vpif_device vpif_obj = { {NULL} };
51static struct device *vpif_dev;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030052static void vpif_calculate_offsets(struct channel_obj *ch);
53static void vpif_config_addr(struct channel_obj *ch, int muxmode);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030054
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -030055static u8 channel_first_int[VPIF_NUMBER_OF_OBJECTS][2] = { {1, 1} };
56
Lad, Prabhakarc66238f2014-05-16 10:33:45 -030057/* Is set to 1 in case of SDTV formats, 2 in case of HDTV formats. */
58static int ycmux_mode;
59
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -030060static inline struct vpif_cap_buffer *to_vpif_buffer(struct vb2_buffer *vb)
61{
62 return container_of(vb, struct vpif_cap_buffer, vb);
63}
64
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030065/**
Lad, Prabhakar23e76a22014-05-16 10:33:37 -030066 * vpif_buffer_prepare : callback function for buffer prepare
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030067 * @vb: ptr to vb2_buffer
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030068 *
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030069 * This is the callback function for buffer prepare when vb2_qbuf()
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030070 * function is called. The buffer is prepared and user space virtual address
71 * or user address is converted into physical address
72 */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030073static int vpif_buffer_prepare(struct vb2_buffer *vb)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030074{
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030075 struct vb2_queue *q = vb->vb2_queue;
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -030076 struct channel_obj *ch = vb2_get_drv_priv(q);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030077 struct common_obj *common;
78 unsigned long addr;
79
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030080 vpif_dbg(2, debug, "vpif_buffer_prepare\n");
81
82 common = &ch->common[VPIF_VIDEO_INDEX];
83
Lad, Prabhakar23e76a22014-05-16 10:33:37 -030084 vb2_set_plane_payload(vb, 0, common->fmt.fmt.pix.sizeimage);
85 if (vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
86 return -EINVAL;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030087
Lad, Prabhakar23e76a22014-05-16 10:33:37 -030088 vb->v4l2_buf.field = common->fmt.fmt.pix.field;
89
90 addr = vb2_dma_contig_plane_dma_addr(vb, 0);
91 if (!IS_ALIGNED((addr + common->ytop_off), 8) ||
92 !IS_ALIGNED((addr + common->ybtm_off), 8) ||
93 !IS_ALIGNED((addr + common->ctop_off), 8) ||
94 !IS_ALIGNED((addr + common->cbtm_off), 8)) {
95 vpif_dbg(1, debug, "offset is not aligned\n");
96 return -EINVAL;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030097 }
Lad, Prabhakar23e76a22014-05-16 10:33:37 -030098
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030099 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300100}
101
102/**
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300103 * vpif_buffer_queue_setup : Callback function for buffer setup.
104 * @vq: vb2_queue ptr
105 * @fmt: v4l2 format
106 * @nbuffers: ptr to number of buffers requested by application
107 * @nplanes:: contains number of distinct video planes needed to hold a frame
108 * @sizes[]: contains the size (in bytes) of each plane.
109 * @alloc_ctxs: ptr to allocation context
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300110 *
111 * This callback function is called when reqbuf() is called to adjust
112 * the buffer count and buffer size
113 */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300114static int vpif_buffer_queue_setup(struct vb2_queue *vq,
115 const struct v4l2_format *fmt,
116 unsigned int *nbuffers, unsigned int *nplanes,
117 unsigned int sizes[], void *alloc_ctxs[])
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300118{
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -0300119 struct channel_obj *ch = vb2_get_drv_priv(vq);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300120 struct common_obj *common;
121
122 common = &ch->common[VPIF_VIDEO_INDEX];
123
124 vpif_dbg(2, debug, "vpif_buffer_setup\n");
125
Lad, Prabhakar837939d2014-05-16 10:33:38 -0300126 if (fmt && fmt->fmt.pix.sizeimage < common->fmt.fmt.pix.sizeimage)
127 return -EINVAL;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300128
Lad, Prabhakar837939d2014-05-16 10:33:38 -0300129 if (vq->num_buffers + *nbuffers < 3)
130 *nbuffers = 3 - vq->num_buffers;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300131
132 *nplanes = 1;
Lad, Prabhakar837939d2014-05-16 10:33:38 -0300133 sizes[0] = fmt ? fmt->fmt.pix.sizeimage : common->fmt.fmt.pix.sizeimage;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300134 alloc_ctxs[0] = common->alloc_ctx;
135
Lad, Prabhakar837939d2014-05-16 10:33:38 -0300136 /* Calculate the offset for Y and C data in the buffer */
137 vpif_calculate_offsets(ch);
138
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300139 return 0;
140}
141
142/**
143 * vpif_buffer_queue : Callback function to add buffer to DMA queue
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300144 * @vb: ptr to vb2_buffer
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300145 */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300146static void vpif_buffer_queue(struct vb2_buffer *vb)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300147{
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -0300148 struct channel_obj *ch = vb2_get_drv_priv(vb->vb2_queue);
149 struct vpif_cap_buffer *buf = to_vpif_buffer(vb);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300150 struct common_obj *common;
Hans Verkuilaec96832012-11-16 12:03:06 -0300151 unsigned long flags;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300152
153 common = &ch->common[VPIF_VIDEO_INDEX];
154
155 vpif_dbg(2, debug, "vpif_buffer_queue\n");
156
Hans Verkuilaec96832012-11-16 12:03:06 -0300157 spin_lock_irqsave(&common->irqlock, flags);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300158 /* add the buffer to the DMA queue */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300159 list_add_tail(&buf->list, &common->dma_queue);
Hans Verkuilaec96832012-11-16 12:03:06 -0300160 spin_unlock_irqrestore(&common->irqlock, flags);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300161}
162
Lad, Prabhakar41b9f242014-05-16 10:33:39 -0300163/**
164 * vpif_start_streaming : Starts the DMA engine for streaming
165 * @vb: ptr to vb2_buffer
166 * @count: number of buffers
167 */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300168static int vpif_start_streaming(struct vb2_queue *vq, unsigned int count)
169{
170 struct vpif_capture_config *vpif_config_data =
171 vpif_dev->platform_data;
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -0300172 struct channel_obj *ch = vb2_get_drv_priv(vq);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300173 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
174 struct vpif_params *vpif = &ch->vpifparams;
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300175 struct vpif_cap_buffer *buf, *tmp;
176 unsigned long addr, flags;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300177 int ret;
178
Hans Verkuilaec96832012-11-16 12:03:06 -0300179 spin_lock_irqsave(&common->irqlock, flags);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300180
Lad, Prabhakarc66238f2014-05-16 10:33:45 -0300181 /* Initialize field_id */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300182 ch->field_id = 0;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300183
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300184 /* configure 1 or 2 channel mode */
Lad, Prabhakarf4ad8d72012-09-27 02:33:12 -0300185 if (vpif_config_data->setup_input_channel_mode) {
186 ret = vpif_config_data->
187 setup_input_channel_mode(vpif->std_info.ycmux_mode);
188 if (ret < 0) {
189 vpif_dbg(1, debug, "can't set vpif channel mode\n");
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300190 goto err;
Lad, Prabhakarf4ad8d72012-09-27 02:33:12 -0300191 }
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300192 }
193
Lad, Prabhakard9a3b132014-05-16 10:33:42 -0300194 ret = v4l2_subdev_call(ch->sd, video, s_stream, 1);
195 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
196 vpif_dbg(1, debug, "stream on failed in subdev\n");
197 goto err;
198 }
199
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300200 /* Call vpif_set_params function to set the parameters and addresses */
201 ret = vpif_set_video_params(vpif, ch->channel_id);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300202 if (ret < 0) {
203 vpif_dbg(1, debug, "can't set video params\n");
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300204 goto err;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300205 }
206
Lad, Prabhakarc66238f2014-05-16 10:33:45 -0300207 ycmux_mode = ret;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300208 vpif_config_addr(ch, ret);
209
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300210 /* Get the next frame from the buffer queue */
211 common->cur_frm = common->next_frm = list_entry(common->dma_queue.next,
212 struct vpif_cap_buffer, list);
213 /* Remove buffer from the buffer queue */
214 list_del(&common->cur_frm->list);
215 spin_unlock_irqrestore(&common->irqlock, flags);
216 /* Mark state of the current frame to active */
217 common->cur_frm->vb.state = VB2_BUF_STATE_ACTIVE;
218
219 addr = vb2_dma_contig_plane_dma_addr(&common->cur_frm->vb, 0);
220
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300221 common->set_addr(addr + common->ytop_off,
222 addr + common->ybtm_off,
223 addr + common->ctop_off,
224 addr + common->cbtm_off);
225
226 /**
227 * Set interrupt for both the fields in VPIF Register enable channel in
228 * VPIF register
229 */
Lad, Prabhakar9e184042012-09-14 10:22:24 -0300230 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
Lad, Prabhakar41b9f242014-05-16 10:33:39 -0300231 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300232 channel0_intr_assert();
233 channel0_intr_enable(1);
234 enable_channel0(1);
235 }
Lad, Prabhakar41b9f242014-05-16 10:33:39 -0300236 if (VPIF_CHANNEL1_VIDEO == ch->channel_id ||
Lad, Prabhakarc66238f2014-05-16 10:33:45 -0300237 ycmux_mode == 2) {
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300238 channel1_intr_assert();
239 channel1_intr_enable(1);
240 enable_channel1(1);
241 }
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300242
243 return 0;
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300244
245err:
246 list_for_each_entry_safe(buf, tmp, &common->dma_queue, list) {
247 list_del(&buf->list);
248 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_QUEUED);
249 }
250
251 return ret;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300252}
253
Lad, Prabhakar41b9f242014-05-16 10:33:39 -0300254/**
255 * vpif_stop_streaming : Stop the DMA engine
256 * @vq: ptr to vb2_queue
257 *
258 * This callback stops the DMA engine and any remaining buffers
259 * in the DMA queue are released.
260 */
Hans Verkuile37559b2014-04-17 02:47:21 -0300261static void vpif_stop_streaming(struct vb2_queue *vq)
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300262{
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -0300263 struct channel_obj *ch = vb2_get_drv_priv(vq);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300264 struct common_obj *common;
Hans Verkuilaec96832012-11-16 12:03:06 -0300265 unsigned long flags;
Lad, Prabhakard9a3b132014-05-16 10:33:42 -0300266 int ret;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300267
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300268 common = &ch->common[VPIF_VIDEO_INDEX];
269
Lad, Prabhakare6ba3db2014-03-22 08:03:07 -0300270 /* Disable channel as per its device type and channel id */
271 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
272 enable_channel0(0);
273 channel0_intr_enable(0);
274 }
Lad, Prabhakar41b9f242014-05-16 10:33:39 -0300275 if (VPIF_CHANNEL1_VIDEO == ch->channel_id ||
Lad, Prabhakarc66238f2014-05-16 10:33:45 -0300276 ycmux_mode == 2) {
Lad, Prabhakare6ba3db2014-03-22 08:03:07 -0300277 enable_channel1(0);
278 channel1_intr_enable(0);
279 }
Lad, Prabhakarc66238f2014-05-16 10:33:45 -0300280
281 ycmux_mode = 0;
Lad, Prabhakare6ba3db2014-03-22 08:03:07 -0300282
Lad, Prabhakard9a3b132014-05-16 10:33:42 -0300283 ret = v4l2_subdev_call(ch->sd, video, s_stream, 0);
284 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
285 vpif_dbg(1, debug, "stream off failed in subdev\n");
286
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300287 /* release all active buffers */
Hans Verkuilaec96832012-11-16 12:03:06 -0300288 spin_lock_irqsave(&common->irqlock, flags);
Lad, Prabhakare6ba3db2014-03-22 08:03:07 -0300289 if (common->cur_frm == common->next_frm) {
290 vb2_buffer_done(&common->cur_frm->vb, VB2_BUF_STATE_ERROR);
291 } else {
292 if (common->cur_frm != NULL)
293 vb2_buffer_done(&common->cur_frm->vb,
294 VB2_BUF_STATE_ERROR);
295 if (common->next_frm != NULL)
296 vb2_buffer_done(&common->next_frm->vb,
297 VB2_BUF_STATE_ERROR);
298 }
299
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300300 while (!list_empty(&common->dma_queue)) {
301 common->next_frm = list_entry(common->dma_queue.next,
302 struct vpif_cap_buffer, list);
303 list_del(&common->next_frm->list);
304 vb2_buffer_done(&common->next_frm->vb, VB2_BUF_STATE_ERROR);
305 }
Hans Verkuilaec96832012-11-16 12:03:06 -0300306 spin_unlock_irqrestore(&common->irqlock, flags);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300307}
308
309static struct vb2_ops video_qops = {
310 .queue_setup = vpif_buffer_queue_setup,
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300311 .buf_prepare = vpif_buffer_prepare,
312 .start_streaming = vpif_start_streaming,
313 .stop_streaming = vpif_stop_streaming,
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300314 .buf_queue = vpif_buffer_queue,
315};
316
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300317/**
318 * vpif_process_buffer_complete: process a completed buffer
319 * @common: ptr to common channel object
320 *
321 * This function time stamp the buffer and mark it as DONE. It also
322 * wake up any process waiting on the QUEUE and set the next buffer
323 * as current
324 */
325static void vpif_process_buffer_complete(struct common_obj *common)
326{
Sakari Ailus8e6057b2012-09-15 15:14:42 -0300327 v4l2_get_timestamp(&common->cur_frm->vb.v4l2_buf.timestamp);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300328 vb2_buffer_done(&common->cur_frm->vb,
329 VB2_BUF_STATE_DONE);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300330 /* Make curFrm pointing to nextFrm */
331 common->cur_frm = common->next_frm;
332}
333
334/**
335 * vpif_schedule_next_buffer: set next buffer address for capture
336 * @common : ptr to common channel object
337 *
338 * This function will get next buffer from the dma queue and
339 * set the buffer address in the vpif register for capture.
340 * the buffer is marked active
341 */
342static void vpif_schedule_next_buffer(struct common_obj *common)
343{
344 unsigned long addr = 0;
345
Hans Verkuilaec96832012-11-16 12:03:06 -0300346 spin_lock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300347 common->next_frm = list_entry(common->dma_queue.next,
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300348 struct vpif_cap_buffer, list);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300349 /* Remove that buffer from the buffer queue */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300350 list_del(&common->next_frm->list);
Hans Verkuilaec96832012-11-16 12:03:06 -0300351 spin_unlock(&common->irqlock);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300352 common->next_frm->vb.state = VB2_BUF_STATE_ACTIVE;
353 addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb, 0);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300354
355 /* Set top and bottom field addresses in VPIF registers */
356 common->set_addr(addr + common->ytop_off,
357 addr + common->ybtm_off,
358 addr + common->ctop_off,
359 addr + common->cbtm_off);
360}
361
362/**
363 * vpif_channel_isr : ISR handler for vpif capture
364 * @irq: irq number
365 * @dev_id: dev_id ptr
366 *
367 * It changes status of the captured buffer, takes next buffer from the queue
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300368 * and sets its address in VPIF registers
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300369 */
370static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
371{
372 struct vpif_device *dev = &vpif_obj;
373 struct common_obj *common;
374 struct channel_obj *ch;
375 enum v4l2_field field;
376 int channel_id = 0;
377 int fid = -1, i;
378
379 channel_id = *(int *)(dev_id);
Manjunath Hadlib1fc4232012-04-13 04:43:10 -0300380 if (!vpif_intr_status(channel_id))
381 return IRQ_NONE;
382
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300383 ch = dev->dev[channel_id];
384
385 field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
386
387 for (i = 0; i < VPIF_NUMBER_OF_OBJECTS; i++) {
388 common = &ch->common[i];
389 /* skip If streaming is not started in this channel */
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300390 /* Check the field format */
391 if (1 == ch->vpifparams.std_info.frm_fmt) {
392 /* Progressive mode */
Hans Verkuilaec96832012-11-16 12:03:06 -0300393 spin_lock(&common->irqlock);
394 if (list_empty(&common->dma_queue)) {
395 spin_unlock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300396 continue;
Hans Verkuilaec96832012-11-16 12:03:06 -0300397 }
398 spin_unlock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300399
400 if (!channel_first_int[i][channel_id])
401 vpif_process_buffer_complete(common);
402
403 channel_first_int[i][channel_id] = 0;
404
405 vpif_schedule_next_buffer(common);
406
407
408 channel_first_int[i][channel_id] = 0;
409 } else {
410 /**
411 * Interlaced mode. If it is first interrupt, ignore
412 * it
413 */
414 if (channel_first_int[i][channel_id]) {
415 channel_first_int[i][channel_id] = 0;
416 continue;
417 }
418 if (0 == i) {
419 ch->field_id ^= 1;
420 /* Get field id from VPIF registers */
421 fid = vpif_channel_getfid(ch->channel_id);
422 if (fid != ch->field_id) {
423 /**
424 * If field id does not match stored
425 * field id, make them in sync
426 */
427 if (0 == fid)
428 ch->field_id = fid;
429 return IRQ_HANDLED;
430 }
431 }
432 /* device field id and local field id are in sync */
433 if (0 == fid) {
434 /* this is even field */
435 if (common->cur_frm == common->next_frm)
436 continue;
437
438 /* mark the current buffer as done */
439 vpif_process_buffer_complete(common);
440 } else if (1 == fid) {
441 /* odd field */
Hans Verkuilaec96832012-11-16 12:03:06 -0300442 spin_lock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300443 if (list_empty(&common->dma_queue) ||
Hans Verkuilaec96832012-11-16 12:03:06 -0300444 (common->cur_frm != common->next_frm)) {
445 spin_unlock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300446 continue;
Hans Verkuilaec96832012-11-16 12:03:06 -0300447 }
448 spin_unlock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300449
450 vpif_schedule_next_buffer(common);
451 }
452 }
453 }
454 return IRQ_HANDLED;
455}
456
457/**
458 * vpif_update_std_info() - update standard related info
459 * @ch: ptr to channel object
460 *
461 * For a given standard selected by application, update values
462 * in the device data structures
463 */
464static int vpif_update_std_info(struct channel_obj *ch)
465{
466 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
467 struct vpif_params *vpifparams = &ch->vpifparams;
468 const struct vpif_channel_config_params *config;
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300469 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300470 struct video_obj *vid_ch = &ch->video;
471 int index;
472
473 vpif_dbg(2, debug, "vpif_update_std_info\n");
474
Mats Randgaardaa444402010-12-16 12:17:42 -0300475 for (index = 0; index < vpif_ch_params_count; index++) {
Lad, Prabhakarced9b212013-03-20 01:28:27 -0300476 config = &vpif_ch_params[index];
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300477 if (config->hd_sd == 0) {
478 vpif_dbg(2, debug, "SD format\n");
479 if (config->stdid & vid_ch->stdid) {
480 memcpy(std_info, config, sizeof(*config));
481 break;
482 }
483 } else {
484 vpif_dbg(2, debug, "HD format\n");
Hans Verkuil0598c172012-09-18 07:18:47 -0300485 if (!memcmp(&config->dv_timings, &vid_ch->dv_timings,
486 sizeof(vid_ch->dv_timings))) {
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300487 memcpy(std_info, config, sizeof(*config));
488 break;
489 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300490 }
491 }
492
493 /* standard not found */
Mats Randgaardaa444402010-12-16 12:17:42 -0300494 if (index == vpif_ch_params_count)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300495 return -EINVAL;
496
497 common->fmt.fmt.pix.width = std_info->width;
498 common->width = std_info->width;
499 common->fmt.fmt.pix.height = std_info->height;
500 common->height = std_info->height;
Lad, Prabhakara4965c52014-05-16 10:33:54 -0300501 common->fmt.fmt.pix.sizeimage = common->height * common->width * 2;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300502 common->fmt.fmt.pix.bytesperline = std_info->width;
503 vpifparams->video_params.hpitch = std_info->width;
504 vpifparams->video_params.storage_mode = std_info->frm_fmt;
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300505
Lad, Prabhakara4965c52014-05-16 10:33:54 -0300506 if (vid_ch->stdid)
507 common->fmt.fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
508 else
509 common->fmt.fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
510
511 if (ch->vpifparams.std_info.frm_fmt)
512 common->fmt.fmt.pix.field = V4L2_FIELD_NONE;
513 else
514 common->fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
515
516 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER)
517 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;
518 else
519 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
520
521 common->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
522
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300523 return 0;
524}
525
526/**
527 * vpif_calculate_offsets : This function calculates buffers offsets
528 * @ch : ptr to channel object
529 *
530 * This function calculates buffer offsets for Y and C in the top and
531 * bottom field
532 */
533static void vpif_calculate_offsets(struct channel_obj *ch)
534{
535 unsigned int hpitch, vpitch, sizeimage;
536 struct video_obj *vid_ch = &(ch->video);
537 struct vpif_params *vpifparams = &ch->vpifparams;
538 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
539 enum v4l2_field field = common->fmt.fmt.pix.field;
540
541 vpif_dbg(2, debug, "vpif_calculate_offsets\n");
542
543 if (V4L2_FIELD_ANY == field) {
544 if (vpifparams->std_info.frm_fmt)
545 vid_ch->buf_field = V4L2_FIELD_NONE;
546 else
547 vid_ch->buf_field = V4L2_FIELD_INTERLACED;
548 } else
549 vid_ch->buf_field = common->fmt.fmt.pix.field;
550
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300551 sizeimage = common->fmt.fmt.pix.sizeimage;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300552
553 hpitch = common->fmt.fmt.pix.bytesperline;
554 vpitch = sizeimage / (hpitch * 2);
555
556 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
557 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
558 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
559 common->ytop_off = 0;
560 common->ybtm_off = hpitch;
561 common->ctop_off = sizeimage / 2;
562 common->cbtm_off = sizeimage / 2 + hpitch;
563 } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
564 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
565 common->ytop_off = 0;
566 common->ybtm_off = sizeimage / 4;
567 common->ctop_off = sizeimage / 2;
568 common->cbtm_off = common->ctop_off + sizeimage / 4;
569 } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
570 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
571 common->ybtm_off = 0;
572 common->ytop_off = sizeimage / 4;
573 common->cbtm_off = sizeimage / 2;
574 common->ctop_off = common->cbtm_off + sizeimage / 4;
575 }
576 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
577 (V4L2_FIELD_INTERLACED == vid_ch->buf_field))
578 vpifparams->video_params.storage_mode = 1;
579 else
580 vpifparams->video_params.storage_mode = 0;
581
582 if (1 == vpifparams->std_info.frm_fmt)
583 vpifparams->video_params.hpitch =
584 common->fmt.fmt.pix.bytesperline;
585 else {
586 if ((field == V4L2_FIELD_ANY)
587 || (field == V4L2_FIELD_INTERLACED))
588 vpifparams->video_params.hpitch =
589 common->fmt.fmt.pix.bytesperline * 2;
590 else
591 vpifparams->video_params.hpitch =
592 common->fmt.fmt.pix.bytesperline;
593 }
594
595 ch->vpifparams.video_params.stdid = vpifparams->std_info.stdid;
596}
597
598/**
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300599 * vpif_get_default_field() - Get default field type based on interface
600 * @vpif_params - ptr to vpif params
601 */
602static inline enum v4l2_field vpif_get_default_field(
603 struct vpif_interface *iface)
604{
605 return (iface->if_type == VPIF_IF_RAW_BAYER) ? V4L2_FIELD_NONE :
606 V4L2_FIELD_INTERLACED;
607}
608
609/**
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300610 * vpif_config_addr() - function to configure buffer address in vpif
611 * @ch - channel ptr
612 * @muxmode - channel mux mode
613 */
614static void vpif_config_addr(struct channel_obj *ch, int muxmode)
615{
616 struct common_obj *common;
617
618 vpif_dbg(2, debug, "vpif_config_addr\n");
619
620 common = &(ch->common[VPIF_VIDEO_INDEX]);
621
622 if (VPIF_CHANNEL1_VIDEO == ch->channel_id)
623 common->set_addr = ch1_set_videobuf_addr;
624 else if (2 == muxmode)
625 common->set_addr = ch0_set_videobuf_addr_yc_nmux;
626 else
627 common->set_addr = ch0_set_videobuf_addr;
628}
629
630/**
Hans Verkuil178cce12012-09-20 09:06:30 -0300631 * vpif_input_to_subdev() - Maps input to sub device
632 * @vpif_cfg - global config ptr
633 * @chan_cfg - channel config ptr
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300634 * @input_index - Given input index from application
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300635 *
636 * lookup the sub device information for a given input index.
637 * we report all the inputs to application. inputs table also
638 * has sub device name for the each input
639 */
Hans Verkuil178cce12012-09-20 09:06:30 -0300640static int vpif_input_to_subdev(
641 struct vpif_capture_config *vpif_cfg,
642 struct vpif_capture_chan_config *chan_cfg,
643 int input_index)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300644{
Hans Verkuil178cce12012-09-20 09:06:30 -0300645 struct vpif_subdev_info *subdev_info;
646 const char *subdev_name;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300647 int i;
648
Hans Verkuil178cce12012-09-20 09:06:30 -0300649 vpif_dbg(2, debug, "vpif_input_to_subdev\n");
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300650
Hans Verkuil178cce12012-09-20 09:06:30 -0300651 subdev_name = chan_cfg->inputs[input_index].subdev_name;
652 if (subdev_name == NULL)
653 return -1;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300654
655 /* loop through the sub device list to get the sub device info */
656 for (i = 0; i < vpif_cfg->subdev_count; i++) {
657 subdev_info = &vpif_cfg->subdev_info[i];
658 if (!strcmp(subdev_info->name, subdev_name))
Hans Verkuil178cce12012-09-20 09:06:30 -0300659 return i;
660 }
661 return -1;
662}
663
664/**
665 * vpif_set_input() - Select an input
666 * @vpif_cfg - global config ptr
667 * @ch - channel
668 * @_index - Given input index from application
669 *
670 * Select the given input.
671 */
672static int vpif_set_input(
673 struct vpif_capture_config *vpif_cfg,
674 struct channel_obj *ch,
675 int index)
676{
677 struct vpif_capture_chan_config *chan_cfg =
678 &vpif_cfg->chan_config[ch->channel_id];
679 struct vpif_subdev_info *subdev_info = NULL;
680 struct v4l2_subdev *sd = NULL;
681 u32 input = 0, output = 0;
682 int sd_index;
683 int ret;
684
685 sd_index = vpif_input_to_subdev(vpif_cfg, chan_cfg, index);
686 if (sd_index >= 0) {
687 sd = vpif_obj.sd[sd_index];
688 subdev_info = &vpif_cfg->subdev_info[sd_index];
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300689 }
690
Hans Verkuil178cce12012-09-20 09:06:30 -0300691 /* first setup input path from sub device to vpif */
692 if (sd && vpif_cfg->setup_input_path) {
693 ret = vpif_cfg->setup_input_path(ch->channel_id,
694 subdev_info->name);
695 if (ret < 0) {
696 vpif_dbg(1, debug, "couldn't setup input path for the" \
697 " sub device %s, for input index %d\n",
698 subdev_info->name, index);
699 return ret;
700 }
701 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300702
Hans Verkuil178cce12012-09-20 09:06:30 -0300703 if (sd) {
704 input = chan_cfg->inputs[index].input_route;
705 output = chan_cfg->inputs[index].output_route;
706 ret = v4l2_subdev_call(sd, video, s_routing,
707 input, output, 0);
708 if (ret < 0 && ret != -ENOIOCTLCMD) {
709 vpif_dbg(1, debug, "Failed to set input\n");
710 return ret;
711 }
712 }
713 ch->input_idx = index;
714 ch->sd = sd;
715 /* copy interface parameters to vpif */
Hans Verkuil0d4f35f2012-09-20 09:06:32 -0300716 ch->vpifparams.iface = chan_cfg->vpif_if;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300717
Hans Verkuil178cce12012-09-20 09:06:30 -0300718 /* update tvnorms from the sub device input info */
719 ch->video_dev->tvnorms = chan_cfg->inputs[index].input.std;
720 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300721}
722
723/**
724 * vpif_querystd() - querystd handler
725 * @file: file ptr
726 * @priv: file handle
727 * @std_id: ptr to std id
728 *
729 * This function is called to detect standard at the selected input
730 */
731static int vpif_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
732{
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300733 struct video_device *vdev = video_devdata(file);
734 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300735 int ret = 0;
736
737 vpif_dbg(2, debug, "vpif_querystd\n");
738
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300739 /* Call querystd function of decoder device */
Hans Verkuil178cce12012-09-20 09:06:30 -0300740 ret = v4l2_subdev_call(ch->sd, video, querystd, std_id);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300741
Hans Verkuil178cce12012-09-20 09:06:30 -0300742 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
743 return -ENODATA;
744 if (ret) {
745 vpif_dbg(1, debug, "Failed to query standard for sub devices\n");
746 return ret;
747 }
748
749 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300750}
751
752/**
753 * vpif_g_std() - get STD handler
754 * @file: file ptr
755 * @priv: file handle
756 * @std_id: ptr to std id
757 */
758static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
759{
Lad, Prabhakard557b7d2014-05-16 10:33:52 -0300760 struct vpif_capture_config *config = vpif_dev->platform_data;
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300761 struct video_device *vdev = video_devdata(file);
762 struct channel_obj *ch = video_get_drvdata(vdev);
Lad, Prabhakard557b7d2014-05-16 10:33:52 -0300763 struct vpif_capture_chan_config *chan_cfg;
764 struct v4l2_input input;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300765
766 vpif_dbg(2, debug, "vpif_g_std\n");
767
Lad, Prabhakard557b7d2014-05-16 10:33:52 -0300768 if (config->chan_config[ch->channel_id].inputs == NULL)
769 return -ENODATA;
770
771 chan_cfg = &config->chan_config[ch->channel_id];
772 input = chan_cfg->inputs[ch->input_idx].input;
773 if (input.capabilities != V4L2_IN_CAP_STD)
774 return -ENODATA;
775
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300776 *std = ch->video.stdid;
777 return 0;
778}
779
780/**
781 * vpif_s_std() - set STD handler
782 * @file: file ptr
783 * @priv: file handle
784 * @std_id: ptr to std id
785 */
Hans Verkuil314527a2013-03-15 06:10:40 -0300786static int vpif_s_std(struct file *file, void *priv, v4l2_std_id std_id)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300787{
Lad, Prabhakard557b7d2014-05-16 10:33:52 -0300788 struct vpif_capture_config *config = vpif_dev->platform_data;
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300789 struct video_device *vdev = video_devdata(file);
790 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300791 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakard557b7d2014-05-16 10:33:52 -0300792 struct vpif_capture_chan_config *chan_cfg;
793 struct v4l2_input input;
794 int ret;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300795
796 vpif_dbg(2, debug, "vpif_s_std\n");
797
Lad, Prabhakard557b7d2014-05-16 10:33:52 -0300798 if (config->chan_config[ch->channel_id].inputs == NULL)
799 return -ENODATA;
800
801 chan_cfg = &config->chan_config[ch->channel_id];
802 input = chan_cfg->inputs[ch->input_idx].input;
803 if (input.capabilities != V4L2_IN_CAP_STD)
804 return -ENODATA;
805
Lad, Prabhakarc66238f2014-05-16 10:33:45 -0300806 if (vb2_is_busy(&common->buffer_queue))
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300807 return -EBUSY;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300808
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300809 /* Call encoder subdevice function to set the standard */
Hans Verkuil314527a2013-03-15 06:10:40 -0300810 ch->video.stdid = std_id;
Hans Verkuil0598c172012-09-18 07:18:47 -0300811 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300812
813 /* Get the information about the standard */
814 if (vpif_update_std_info(ch)) {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300815 vpif_err("Error getting the standard info\n");
Hans Verkuil46656af2011-01-04 06:51:35 -0300816 return -EINVAL;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300817 }
818
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300819 /* set standard in the sub device */
Laurent Pinchart8774bed2014-04-28 16:53:01 -0300820 ret = v4l2_subdev_call(ch->sd, video, s_std, std_id);
Hans Verkuil178cce12012-09-20 09:06:30 -0300821 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300822 vpif_dbg(1, debug, "Failed to set standard for sub devices\n");
Hans Verkuil178cce12012-09-20 09:06:30 -0300823 return ret;
824 }
825 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300826}
827
828/**
829 * vpif_enum_input() - ENUMINPUT handler
830 * @file: file ptr
831 * @priv: file handle
832 * @input: ptr to input structure
833 */
834static int vpif_enum_input(struct file *file, void *priv,
835 struct v4l2_input *input)
836{
837
838 struct vpif_capture_config *config = vpif_dev->platform_data;
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300839 struct video_device *vdev = video_devdata(file);
840 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300841 struct vpif_capture_chan_config *chan_cfg;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300842
843 chan_cfg = &config->chan_config[ch->channel_id];
844
Lad, Prabhakara4965c52014-05-16 10:33:54 -0300845 if (input->index >= chan_cfg->input_count)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300846 return -EINVAL;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300847
848 memcpy(input, &chan_cfg->inputs[input->index].input,
849 sizeof(*input));
850 return 0;
851}
852
853/**
854 * vpif_g_input() - Get INPUT handler
855 * @file: file ptr
856 * @priv: file handle
857 * @index: ptr to input index
858 */
859static int vpif_g_input(struct file *file, void *priv, unsigned int *index)
860{
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300861 struct video_device *vdev = video_devdata(file);
862 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300863
Hans Verkuil6f47c6c2012-09-20 09:06:22 -0300864 *index = ch->input_idx;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300865 return 0;
866}
867
868/**
869 * vpif_s_input() - Set INPUT handler
870 * @file: file ptr
871 * @priv: file handle
872 * @index: input index
873 */
874static int vpif_s_input(struct file *file, void *priv, unsigned int index)
875{
876 struct vpif_capture_config *config = vpif_dev->platform_data;
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300877 struct video_device *vdev = video_devdata(file);
878 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300879 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300880 struct vpif_capture_chan_config *chan_cfg;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300881
882 chan_cfg = &config->chan_config[ch->channel_id];
883
Hans Verkuil7aaad132012-09-20 09:06:25 -0300884 if (index >= chan_cfg->input_count)
885 return -EINVAL;
886
Lad, Prabhakarc66238f2014-05-16 10:33:45 -0300887 if (vb2_is_busy(&common->buffer_queue))
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300888 return -EBUSY;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300889
Hans Verkuil178cce12012-09-20 09:06:30 -0300890 return vpif_set_input(config, ch, index);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300891}
892
893/**
894 * vpif_enum_fmt_vid_cap() - ENUM_FMT handler
895 * @file: file ptr
896 * @priv: file handle
897 * @index: input index
898 */
899static int vpif_enum_fmt_vid_cap(struct file *file, void *priv,
900 struct v4l2_fmtdesc *fmt)
901{
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300902 struct video_device *vdev = video_devdata(file);
903 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300904
905 if (fmt->index != 0) {
906 vpif_dbg(1, debug, "Invalid format index\n");
907 return -EINVAL;
908 }
909
910 /* Fill in the information about format */
911 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER) {
912 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
913 strcpy(fmt->description, "Raw Mode -Bayer Pattern GrRBGb");
914 fmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
915 } else {
916 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
917 strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
918 fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
919 }
920 return 0;
921}
922
923/**
924 * vpif_try_fmt_vid_cap() - TRY_FMT handler
925 * @file: file ptr
926 * @priv: file handle
927 * @fmt: ptr to v4l2 format structure
928 */
929static int vpif_try_fmt_vid_cap(struct file *file, void *priv,
930 struct v4l2_format *fmt)
931{
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300932 struct video_device *vdev = video_devdata(file);
933 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300934 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
Lad, Prabhakara4965c52014-05-16 10:33:54 -0300935 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
936 struct vpif_params *vpif_params = &ch->vpifparams;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300937
Lad, Prabhakara4965c52014-05-16 10:33:54 -0300938 /*
939 * to supress v4l-compliance warnings silently correct
940 * the pixelformat
941 */
942 if (vpif_params->iface.if_type == VPIF_IF_RAW_BAYER) {
943 if (pixfmt->pixelformat != V4L2_PIX_FMT_SBGGR8)
944 pixfmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
945 } else {
946 if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P)
947 pixfmt->pixelformat = V4L2_PIX_FMT_YUV422P;
948 }
949
950 common->fmt.fmt.pix.pixelformat = pixfmt->pixelformat;
951
952 vpif_update_std_info(ch);
953
954 pixfmt->field = common->fmt.fmt.pix.field;
955 pixfmt->colorspace = common->fmt.fmt.pix.colorspace;
956 pixfmt->bytesperline = common->fmt.fmt.pix.width;
957 pixfmt->width = common->fmt.fmt.pix.width;
958 pixfmt->height = common->fmt.fmt.pix.height;
959 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height * 2;
960 pixfmt->priv = 0;
961
962 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300963}
964
965
966/**
967 * vpif_g_fmt_vid_cap() - Set INPUT handler
968 * @file: file ptr
969 * @priv: file handle
970 * @fmt: ptr to v4l2 format structure
971 */
972static int vpif_g_fmt_vid_cap(struct file *file, void *priv,
973 struct v4l2_format *fmt)
974{
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300975 struct video_device *vdev = video_devdata(file);
976 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300977 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
978
979 /* Check the validity of the buffer type */
980 if (common->fmt.type != fmt->type)
981 return -EINVAL;
982
983 /* Fill in the information about format */
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300984 *fmt = common->fmt;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300985 return 0;
986}
987
988/**
989 * vpif_s_fmt_vid_cap() - Set FMT handler
990 * @file: file ptr
991 * @priv: file handle
992 * @fmt: ptr to v4l2 format structure
993 */
994static int vpif_s_fmt_vid_cap(struct file *file, void *priv,
995 struct v4l2_format *fmt)
996{
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300997 struct video_device *vdev = video_devdata(file);
998 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300999 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakara4965c52014-05-16 10:33:54 -03001000 int ret;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001001
Mats Randgaard2c0ddd12010-12-16 12:17:45 -03001002 vpif_dbg(2, debug, "%s\n", __func__);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001003
Lad, Prabhakarc66238f2014-05-16 10:33:45 -03001004 if (vb2_is_busy(&common->buffer_queue))
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001005 return -EBUSY;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001006
Lad, Prabhakara4965c52014-05-16 10:33:54 -03001007 ret = vpif_try_fmt_vid_cap(file, priv, fmt);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001008 if (ret)
1009 return ret;
Lad, Prabhakara4965c52014-05-16 10:33:54 -03001010
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001011 /* store the format in the channel object */
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001012 common->fmt = *fmt;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001013 return 0;
1014}
1015
1016/**
1017 * vpif_querycap() - QUERYCAP handler
1018 * @file: file ptr
1019 * @priv: file handle
1020 * @cap: ptr to v4l2_capability structure
1021 */
1022static int vpif_querycap(struct file *file, void *priv,
1023 struct v4l2_capability *cap)
1024{
1025 struct vpif_capture_config *config = vpif_dev->platform_data;
1026
Lad, Prabhakar626d533f2012-09-25 11:21:55 -03001027 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1028 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
Lad, Prabhakare75ea0c2014-05-16 10:33:46 -03001029 strlcpy(cap->driver, VPIF_DRIVER_NAME, sizeof(cap->driver));
Lad, Prabhakar626d533f2012-09-25 11:21:55 -03001030 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
1031 dev_name(vpif_dev));
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001032 strlcpy(cap->card, config->card_name, sizeof(cap->card));
1033
1034 return 0;
1035}
1036
1037/**
Hans Verkuil0598c172012-09-18 07:18:47 -03001038 * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001039 * @file: file ptr
1040 * @priv: file handle
Hans Verkuil0598c172012-09-18 07:18:47 -03001041 * @timings: input timings
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001042 */
Hans Verkuil0598c172012-09-18 07:18:47 -03001043static int
1044vpif_enum_dv_timings(struct file *file, void *priv,
1045 struct v4l2_enum_dv_timings *timings)
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001046{
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001047 struct vpif_capture_config *config = vpif_dev->platform_data;
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001048 struct video_device *vdev = video_devdata(file);
1049 struct channel_obj *ch = video_get_drvdata(vdev);
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001050 struct vpif_capture_chan_config *chan_cfg;
1051 struct v4l2_input input;
Hans Verkuil178cce12012-09-20 09:06:30 -03001052 int ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001053
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001054 if (config->chan_config[ch->channel_id].inputs == NULL)
1055 return -ENODATA;
1056
1057 chan_cfg = &config->chan_config[ch->channel_id];
1058 input = chan_cfg->inputs[ch->input_idx].input;
1059 if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
1060 return -ENODATA;
1061
Laurent Pinchart10d21462014-01-31 09:04:19 -03001062 timings->pad = 0;
1063
1064 ret = v4l2_subdev_call(ch->sd, pad, enum_dv_timings, timings);
Wei Yongjune070f1b2012-10-30 09:45:00 -03001065 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
Hans Verkuil178cce12012-09-20 09:06:30 -03001066 return -EINVAL;
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001067
Hans Verkuil178cce12012-09-20 09:06:30 -03001068 return ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001069}
1070
1071/**
Hans Verkuil0598c172012-09-18 07:18:47 -03001072 * vpif_query_dv_timings() - QUERY_DV_TIMINGS handler
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001073 * @file: file ptr
1074 * @priv: file handle
Hans Verkuil0598c172012-09-18 07:18:47 -03001075 * @timings: input timings
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001076 */
Hans Verkuil0598c172012-09-18 07:18:47 -03001077static int
1078vpif_query_dv_timings(struct file *file, void *priv,
1079 struct v4l2_dv_timings *timings)
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001080{
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001081 struct vpif_capture_config *config = vpif_dev->platform_data;
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001082 struct video_device *vdev = video_devdata(file);
1083 struct channel_obj *ch = video_get_drvdata(vdev);
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001084 struct vpif_capture_chan_config *chan_cfg;
1085 struct v4l2_input input;
Hans Verkuil178cce12012-09-20 09:06:30 -03001086 int ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001087
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001088 if (config->chan_config[ch->channel_id].inputs == NULL)
1089 return -ENODATA;
1090
1091 chan_cfg = &config->chan_config[ch->channel_id];
1092 input = chan_cfg->inputs[ch->input_idx].input;
1093 if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
1094 return -ENODATA;
1095
Hans Verkuil178cce12012-09-20 09:06:30 -03001096 ret = v4l2_subdev_call(ch->sd, video, query_dv_timings, timings);
Wei Yongjune070f1b2012-10-30 09:45:00 -03001097 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
Hans Verkuil178cce12012-09-20 09:06:30 -03001098 return -ENODATA;
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001099
Hans Verkuil178cce12012-09-20 09:06:30 -03001100 return ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001101}
1102
Mats Randgaardc027e162010-12-16 12:17:44 -03001103/**
1104 * vpif_s_dv_timings() - S_DV_TIMINGS handler
1105 * @file: file ptr
1106 * @priv: file handle
1107 * @timings: digital video timings
1108 */
1109static int vpif_s_dv_timings(struct file *file, void *priv,
1110 struct v4l2_dv_timings *timings)
1111{
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001112 struct vpif_capture_config *config = vpif_dev->platform_data;
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001113 struct video_device *vdev = video_devdata(file);
1114 struct channel_obj *ch = video_get_drvdata(vdev);
Mats Randgaardc027e162010-12-16 12:17:44 -03001115 struct vpif_params *vpifparams = &ch->vpifparams;
1116 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001117 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
Mats Randgaardc027e162010-12-16 12:17:44 -03001118 struct video_obj *vid_ch = &ch->video;
Hans Verkuil0598c172012-09-18 07:18:47 -03001119 struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001120 struct vpif_capture_chan_config *chan_cfg;
1121 struct v4l2_input input;
Mats Randgaardc027e162010-12-16 12:17:44 -03001122 int ret;
1123
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001124 if (config->chan_config[ch->channel_id].inputs == NULL)
1125 return -ENODATA;
1126
1127 chan_cfg = &config->chan_config[ch->channel_id];
1128 input = chan_cfg->inputs[ch->input_idx].input;
1129 if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
1130 return -ENODATA;
1131
Mats Randgaardc027e162010-12-16 12:17:44 -03001132 if (timings->type != V4L2_DV_BT_656_1120) {
1133 vpif_dbg(2, debug, "Timing type not defined\n");
1134 return -EINVAL;
1135 }
1136
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001137 if (vb2_is_busy(&common->buffer_queue))
1138 return -EBUSY;
1139
Mats Randgaardc027e162010-12-16 12:17:44 -03001140 /* Configure subdevice timings, if any */
Hans Verkuil178cce12012-09-20 09:06:30 -03001141 ret = v4l2_subdev_call(ch->sd, video, s_dv_timings, timings);
1142 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1143 ret = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001144 if (ret < 0) {
1145 vpif_dbg(2, debug, "Error setting custom DV timings\n");
1146 return ret;
1147 }
1148
1149 if (!(timings->bt.width && timings->bt.height &&
1150 (timings->bt.hbackporch ||
1151 timings->bt.hfrontporch ||
1152 timings->bt.hsync) &&
1153 timings->bt.vfrontporch &&
1154 (timings->bt.vbackporch ||
1155 timings->bt.vsync))) {
1156 vpif_dbg(2, debug, "Timings for width, height, "
1157 "horizontal back porch, horizontal sync, "
1158 "horizontal front porch, vertical back porch, "
1159 "vertical sync and vertical back porch "
1160 "must be defined\n");
1161 return -EINVAL;
1162 }
1163
Hans Verkuil0598c172012-09-18 07:18:47 -03001164 vid_ch->dv_timings = *timings;
Mats Randgaardc027e162010-12-16 12:17:44 -03001165
1166 /* Configure video port timings */
1167
Hans Verkuile3655262013-07-29 08:41:00 -03001168 std_info->eav2sav = V4L2_DV_BT_BLANKING_WIDTH(bt) - 8;
Mats Randgaardc027e162010-12-16 12:17:44 -03001169 std_info->sav2eav = bt->width;
1170
1171 std_info->l1 = 1;
1172 std_info->l3 = bt->vsync + bt->vbackporch + 1;
1173
Hans Verkuile3655262013-07-29 08:41:00 -03001174 std_info->vsize = V4L2_DV_BT_FRAME_HEIGHT(bt);
Mats Randgaardc027e162010-12-16 12:17:44 -03001175 if (bt->interlaced) {
1176 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
Mats Randgaardc027e162010-12-16 12:17:44 -03001177 std_info->l5 = std_info->vsize/2 -
1178 (bt->vfrontporch - 1);
1179 std_info->l7 = std_info->vsize/2 + 1;
1180 std_info->l9 = std_info->l7 + bt->il_vsync +
1181 bt->il_vbackporch + 1;
1182 std_info->l11 = std_info->vsize -
1183 (bt->il_vfrontporch - 1);
1184 } else {
1185 vpif_dbg(2, debug, "Required timing values for "
1186 "interlaced BT format missing\n");
1187 return -EINVAL;
1188 }
1189 } else {
Mats Randgaardc027e162010-12-16 12:17:44 -03001190 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
1191 }
1192 strncpy(std_info->name, "Custom timings BT656/1120", VPIF_MAX_NAME);
1193 std_info->width = bt->width;
1194 std_info->height = bt->height;
1195 std_info->frm_fmt = bt->interlaced ? 0 : 1;
1196 std_info->ycmux_mode = 0;
1197 std_info->capture_format = 0;
1198 std_info->vbi_supported = 0;
1199 std_info->hd_sd = 1;
1200 std_info->stdid = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001201
1202 vid_ch->stdid = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001203 return 0;
1204}
1205
1206/**
1207 * vpif_g_dv_timings() - G_DV_TIMINGS handler
1208 * @file: file ptr
1209 * @priv: file handle
1210 * @timings: digital video timings
1211 */
1212static int vpif_g_dv_timings(struct file *file, void *priv,
1213 struct v4l2_dv_timings *timings)
1214{
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001215 struct vpif_capture_config *config = vpif_dev->platform_data;
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001216 struct video_device *vdev = video_devdata(file);
1217 struct channel_obj *ch = video_get_drvdata(vdev);
Mats Randgaardc027e162010-12-16 12:17:44 -03001218 struct video_obj *vid_ch = &ch->video;
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001219 struct vpif_capture_chan_config *chan_cfg;
1220 struct v4l2_input input;
1221
1222 if (config->chan_config[ch->channel_id].inputs == NULL)
1223 return -ENODATA;
1224
1225 chan_cfg = &config->chan_config[ch->channel_id];
1226 input = chan_cfg->inputs[ch->input_idx].input;
1227 if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
1228 return -ENODATA;
Mats Randgaardc027e162010-12-16 12:17:44 -03001229
Hans Verkuil0598c172012-09-18 07:18:47 -03001230 *timings = vid_ch->dv_timings;
Mats Randgaardc027e162010-12-16 12:17:44 -03001231
1232 return 0;
1233}
1234
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001235/*
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001236 * vpif_log_status() - Status information
1237 * @file: file ptr
1238 * @priv: file handle
1239 *
1240 * Returns zero.
1241 */
1242static int vpif_log_status(struct file *filep, void *priv)
1243{
1244 /* status for sub devices */
1245 v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1246
1247 return 0;
1248}
1249
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001250/* vpif capture ioctl operations */
1251static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
Lad, Prabhakar7b4657f2014-05-16 10:33:49 -03001252 .vidioc_querycap = vpif_querycap,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001253 .vidioc_enum_fmt_vid_cap = vpif_enum_fmt_vid_cap,
Lad, Prabhakar7b4657f2014-05-16 10:33:49 -03001254 .vidioc_g_fmt_vid_cap = vpif_g_fmt_vid_cap,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001255 .vidioc_s_fmt_vid_cap = vpif_s_fmt_vid_cap,
1256 .vidioc_try_fmt_vid_cap = vpif_try_fmt_vid_cap,
Lad, Prabhakar7b4657f2014-05-16 10:33:49 -03001257
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001258 .vidioc_enum_input = vpif_enum_input,
1259 .vidioc_s_input = vpif_s_input,
1260 .vidioc_g_input = vpif_g_input,
Lad, Prabhakard9a3b132014-05-16 10:33:42 -03001261
1262 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1263 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1264 .vidioc_querybuf = vb2_ioctl_querybuf,
1265 .vidioc_qbuf = vb2_ioctl_qbuf,
1266 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1267 .vidioc_expbuf = vb2_ioctl_expbuf,
1268 .vidioc_streamon = vb2_ioctl_streamon,
1269 .vidioc_streamoff = vb2_ioctl_streamoff,
1270
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001271 .vidioc_querystd = vpif_querystd,
Lad, Prabhakar7b4657f2014-05-16 10:33:49 -03001272 .vidioc_s_std = vpif_s_std,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001273 .vidioc_g_std = vpif_g_std,
Lad, Prabhakard9a3b132014-05-16 10:33:42 -03001274
Lad, Prabhakar7b4657f2014-05-16 10:33:49 -03001275 .vidioc_enum_dv_timings = vpif_enum_dv_timings,
1276 .vidioc_query_dv_timings = vpif_query_dv_timings,
1277 .vidioc_s_dv_timings = vpif_s_dv_timings,
1278 .vidioc_g_dv_timings = vpif_g_dv_timings,
1279
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001280 .vidioc_log_status = vpif_log_status,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001281};
1282
1283/* vpif file operations */
1284static struct v4l2_file_operations vpif_fops = {
1285 .owner = THIS_MODULE,
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001286 .open = v4l2_fh_open,
1287 .release = vb2_fop_release,
Hans Verkuil46656af2011-01-04 06:51:35 -03001288 .unlocked_ioctl = video_ioctl2,
Lad, Prabhakard26d26b2014-05-16 10:33:40 -03001289 .mmap = vb2_fop_mmap,
1290 .poll = vb2_fop_poll
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001291};
1292
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001293/**
1294 * initialize_vpif() - Initialize vpif data structures
1295 *
1296 * Allocate memory for data structures and initialize them
1297 */
1298static int initialize_vpif(void)
1299{
Lad, Prabhakar4015bef2014-05-16 10:33:47 -03001300 int err, i, j;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001301 int free_channel_objects_index;
1302
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001303 /* Allocate memory for six channel objects */
1304 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1305 vpif_obj.dev[i] =
1306 kzalloc(sizeof(*vpif_obj.dev[i]), GFP_KERNEL);
1307 /* If memory allocation fails, return error */
1308 if (!vpif_obj.dev[i]) {
1309 free_channel_objects_index = i;
1310 err = -ENOMEM;
1311 goto vpif_init_free_channel_objects;
1312 }
1313 }
1314 return 0;
1315
1316vpif_init_free_channel_objects:
1317 for (j = 0; j < free_channel_objects_index; j++)
1318 kfree(vpif_obj.dev[j]);
1319 return err;
1320}
1321
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001322static int vpif_async_bound(struct v4l2_async_notifier *notifier,
1323 struct v4l2_subdev *subdev,
1324 struct v4l2_async_subdev *asd)
1325{
1326 int i;
1327
1328 for (i = 0; i < vpif_obj.config->subdev_count; i++)
1329 if (!strcmp(vpif_obj.config->subdev_info[i].name,
1330 subdev->name)) {
1331 vpif_obj.sd[i] = subdev;
1332 return 0;
1333 }
1334
1335 return -EINVAL;
1336}
1337
1338static int vpif_probe_complete(void)
1339{
1340 struct common_obj *common;
Lad, Prabhakard26d26b2014-05-16 10:33:40 -03001341 struct video_device *vdev;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001342 struct channel_obj *ch;
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001343 struct vb2_queue *q;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001344 int i, j, err, k;
1345
1346 for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
1347 ch = vpif_obj.dev[j];
1348 ch->channel_id = j;
1349 common = &(ch->common[VPIF_VIDEO_INDEX]);
1350 spin_lock_init(&common->irqlock);
1351 mutex_init(&common->lock);
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001352
1353 /* select input 0 */
1354 err = vpif_set_input(vpif_obj.config, ch, 0);
1355 if (err)
1356 goto probe_out;
1357
Lad, Prabhakara4965c52014-05-16 10:33:54 -03001358 /* set initial format */
1359 ch->video.stdid = V4L2_STD_525_60;
1360 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
1361 vpif_update_std_info(ch);
1362
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001363 /* Initialize vb2 queue */
1364 q = &common->buffer_queue;
1365 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1366 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1367 q->drv_priv = ch;
1368 q->ops = &video_qops;
1369 q->mem_ops = &vb2_dma_contig_memops;
1370 q->buf_struct_size = sizeof(struct vpif_cap_buffer);
1371 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1372 q->min_buffers_needed = 1;
Lad, Prabhakar999ba692014-05-16 10:33:33 -03001373 q->lock = &common->lock;
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001374
1375 err = vb2_queue_init(q);
1376 if (err) {
1377 vpif_err("vpif_capture: vb2_queue_init() failed\n");
1378 goto probe_out;
1379 }
1380
1381 common->alloc_ctx = vb2_dma_contig_init_ctx(vpif_dev);
1382 if (IS_ERR(common->alloc_ctx)) {
1383 vpif_err("Failed to get the context\n");
1384 err = PTR_ERR(common->alloc_ctx);
1385 goto probe_out;
1386 }
1387
1388 INIT_LIST_HEAD(&common->dma_queue);
1389
Lad, Prabhakare75ea0c2014-05-16 10:33:46 -03001390 /* Initialize the video_device structure */
Lad, Prabhakard26d26b2014-05-16 10:33:40 -03001391 vdev = ch->video_dev;
Lad, Prabhakare75ea0c2014-05-16 10:33:46 -03001392 strlcpy(vdev->name, VPIF_DRIVER_NAME, sizeof(vdev->name));
1393 vdev->release = video_device_release;
1394 vdev->fops = &vpif_fops;
1395 vdev->ioctl_ops = &vpif_ioctl_ops;
1396 vdev->v4l2_dev = &vpif_obj.v4l2_dev;
1397 vdev->vfl_dir = VFL_DIR_RX;
Lad, Prabhakard26d26b2014-05-16 10:33:40 -03001398 vdev->queue = q;
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001399 vdev->lock = &common->lock;
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001400 video_set_drvdata(ch->video_dev, ch);
Lad, Prabhakard26d26b2014-05-16 10:33:40 -03001401 err = video_register_device(vdev,
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001402 VFL_TYPE_GRABBER, (j ? 1 : 0));
1403 if (err)
1404 goto probe_out;
1405 }
1406
1407 v4l2_info(&vpif_obj.v4l2_dev, "VPIF capture driver initialized\n");
1408 return 0;
1409
1410probe_out:
1411 for (k = 0; k < j; k++) {
1412 /* Get the pointer to the channel object */
1413 ch = vpif_obj.dev[k];
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001414 common = &ch->common[k];
1415 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001416 /* Unregister video device */
1417 video_unregister_device(ch->video_dev);
1418 }
1419 kfree(vpif_obj.sd);
1420 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1421 ch = vpif_obj.dev[i];
1422 /* Note: does nothing if ch->video_dev == NULL */
1423 video_device_release(ch->video_dev);
1424 }
1425 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1426
1427 return err;
1428}
1429
1430static int vpif_async_complete(struct v4l2_async_notifier *notifier)
1431{
1432 return vpif_probe_complete();
1433}
1434
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001435/**
1436 * vpif_probe : This function probes the vpif capture driver
1437 * @pdev: platform device pointer
1438 *
1439 * This creates device entries by register itself to the V4L2 driver and
1440 * initializes fields of each channel objects
1441 */
1442static __init int vpif_probe(struct platform_device *pdev)
1443{
1444 struct vpif_subdev_info *subdevdata;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001445 int i, j, err;
Hans Verkuil5be452c2012-09-20 09:06:29 -03001446 int res_idx = 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001447 struct i2c_adapter *i2c_adap;
1448 struct channel_obj *ch;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001449 struct video_device *vfd;
1450 struct resource *res;
1451 int subdev_count;
1452
1453 vpif_dev = &pdev->dev;
1454
1455 err = initialize_vpif();
1456 if (err) {
1457 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
1458 return err;
1459 }
1460
Hans Verkuild2f7a1a2011-12-13 05:44:42 -03001461 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
1462 if (err) {
1463 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
1464 return err;
1465 }
1466
Hans Verkuil5be452c2012-09-20 09:06:29 -03001467 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
Lad, Prabhakara76a0b32013-06-17 11:20:47 -03001468 err = devm_request_irq(&pdev->dev, res->start, vpif_channel_isr,
Lad, Prabhakare75ea0c2014-05-16 10:33:46 -03001469 IRQF_SHARED, VPIF_DRIVER_NAME,
Lad, Prabhakara76a0b32013-06-17 11:20:47 -03001470 (void *)(&vpif_obj.dev[res_idx]->
1471 channel_id));
1472 if (err) {
1473 err = -EINVAL;
1474 goto vpif_unregister;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001475 }
Hans Verkuil5be452c2012-09-20 09:06:29 -03001476 res_idx++;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001477 }
1478
1479 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1480 /* Get the pointer to the channel object */
1481 ch = vpif_obj.dev[i];
1482 /* Allocate memory for video device */
1483 vfd = video_device_alloc();
1484 if (NULL == vfd) {
1485 for (j = 0; j < i; j++) {
1486 ch = vpif_obj.dev[j];
1487 video_device_release(ch->video_dev);
1488 }
1489 err = -ENOMEM;
Lad, Prabhakarb2de4f22013-06-17 11:20:46 -03001490 goto vpif_unregister;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001491 }
1492
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001493 /* Set video_dev to the video device */
1494 ch->video_dev = vfd;
1495 }
1496
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001497 vpif_obj.config = pdev->dev.platform_data;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001498
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001499 subdev_count = vpif_obj.config->subdev_count;
Mats Randgaard1f8766b2010-08-30 10:30:37 -03001500 vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001501 GFP_KERNEL);
1502 if (vpif_obj.sd == NULL) {
1503 vpif_err("unable to allocate memory for subdevice pointers\n");
1504 err = -ENOMEM;
Hans Verkuil5be452c2012-09-20 09:06:29 -03001505 goto vpif_sd_error;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001506 }
1507
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001508 if (!vpif_obj.config->asd_sizes) {
1509 i2c_adap = i2c_get_adapter(1);
1510 for (i = 0; i < subdev_count; i++) {
1511 subdevdata = &vpif_obj.config->subdev_info[i];
1512 vpif_obj.sd[i] =
1513 v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
1514 i2c_adap,
1515 &subdevdata->
1516 board_info,
1517 NULL);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001518
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001519 if (!vpif_obj.sd[i]) {
1520 vpif_err("Error registering v4l2 subdevice\n");
Wei Yongjun2fcd9dc2013-09-02 05:06:10 -03001521 err = -ENODEV;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001522 goto probe_subdev_out;
1523 }
1524 v4l2_info(&vpif_obj.v4l2_dev,
1525 "registered sub device %s\n",
1526 subdevdata->name);
1527 }
1528 vpif_probe_complete();
1529 } else {
Sylwester Nawrockie8419d02013-07-19 12:31:10 -03001530 vpif_obj.notifier.subdevs = vpif_obj.config->asd;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001531 vpif_obj.notifier.num_subdevs = vpif_obj.config->asd_sizes[0];
1532 vpif_obj.notifier.bound = vpif_async_bound;
1533 vpif_obj.notifier.complete = vpif_async_complete;
1534 err = v4l2_async_notifier_register(&vpif_obj.v4l2_dev,
1535 &vpif_obj.notifier);
1536 if (err) {
1537 vpif_err("Error registering async notifier\n");
1538 err = -EINVAL;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001539 goto probe_subdev_out;
1540 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001541 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001542
1543 return 0;
1544
Hans Verkuilb65814e2012-09-20 09:06:26 -03001545probe_subdev_out:
1546 /* free sub devices memory */
1547 kfree(vpif_obj.sd);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001548
Hans Verkuil5be452c2012-09-20 09:06:29 -03001549vpif_sd_error:
1550 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1551 ch = vpif_obj.dev[i];
1552 /* Note: does nothing if ch->video_dev == NULL */
1553 video_device_release(ch->video_dev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001554 }
Lad, Prabhakarb2de4f22013-06-17 11:20:46 -03001555vpif_unregister:
Hans Verkuild2f7a1a2011-12-13 05:44:42 -03001556 v4l2_device_unregister(&vpif_obj.v4l2_dev);
Lad, Prabhakarb2de4f22013-06-17 11:20:46 -03001557
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001558 return err;
1559}
1560
1561/**
1562 * vpif_remove() - driver remove handler
1563 * @device: ptr to platform device structure
1564 *
1565 * The vidoe device is unregistered
1566 */
1567static int vpif_remove(struct platform_device *device)
1568{
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001569 struct common_obj *common;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001570 struct channel_obj *ch;
Lad, Prabhakarb2de4f22013-06-17 11:20:46 -03001571 int i;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001572
1573 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1574
Lad, Prabhakar410ca602013-06-17 11:20:44 -03001575 kfree(vpif_obj.sd);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001576 /* un-register device */
1577 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1578 /* Get the pointer to the channel object */
1579 ch = vpif_obj.dev[i];
Prabhakar Lade9b58722014-07-18 13:31:51 -03001580 common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001581 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001582 /* Unregister video device */
1583 video_unregister_device(ch->video_dev);
Lad, Prabhakar410ca602013-06-17 11:20:44 -03001584 kfree(vpif_obj.dev[i]);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001585 }
1586 return 0;
1587}
1588
Lad, Prabhakarb7047712014-05-16 10:33:50 -03001589#ifdef CONFIG_PM_SLEEP
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001590/**
1591 * vpif_suspend: vpif device suspend
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001592 */
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001593static int vpif_suspend(struct device *dev)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001594{
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001595
1596 struct common_obj *common;
1597 struct channel_obj *ch;
1598 int i;
1599
1600 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1601 /* Get the pointer to the channel object */
1602 ch = vpif_obj.dev[i];
1603 common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakarb7047712014-05-16 10:33:50 -03001604
1605 if (!vb2_is_streaming(&common->buffer_queue))
1606 continue;
1607
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001608 mutex_lock(&common->lock);
Lad, Prabhakarb7047712014-05-16 10:33:50 -03001609 /* Disable channel */
1610 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
1611 enable_channel0(0);
1612 channel0_intr_enable(0);
1613 }
1614 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
1615 ycmux_mode == 2) {
1616 enable_channel1(0);
1617 channel1_intr_enable(0);
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001618 }
1619 mutex_unlock(&common->lock);
1620 }
1621
1622 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001623}
1624
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001625/*
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001626 * vpif_resume: vpif device suspend
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001627 */
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001628static int vpif_resume(struct device *dev)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001629{
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001630 struct common_obj *common;
1631 struct channel_obj *ch;
1632 int i;
1633
1634 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1635 /* Get the pointer to the channel object */
1636 ch = vpif_obj.dev[i];
1637 common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakarb7047712014-05-16 10:33:50 -03001638
1639 if (!vb2_is_streaming(&common->buffer_queue))
1640 continue;
1641
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001642 mutex_lock(&common->lock);
Lad, Prabhakarb7047712014-05-16 10:33:50 -03001643 /* Enable channel */
1644 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
1645 enable_channel0(1);
1646 channel0_intr_enable(1);
1647 }
1648 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
1649 ycmux_mode == 2) {
1650 enable_channel1(1);
1651 channel1_intr_enable(1);
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001652 }
1653 mutex_unlock(&common->lock);
1654 }
1655
1656 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001657}
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001658#endif
1659
Lad, Prabhakarb7047712014-05-16 10:33:50 -03001660static SIMPLE_DEV_PM_OPS(vpif_pm_ops, vpif_suspend, vpif_resume);
1661
Mats Randgaardffa1b392010-08-30 10:30:36 -03001662static __refdata struct platform_driver vpif_driver = {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001663 .driver = {
Lad, Prabhakare75ea0c2014-05-16 10:33:46 -03001664 .name = VPIF_DRIVER_NAME,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001665 .owner = THIS_MODULE,
Lad, Prabhakarb7047712014-05-16 10:33:50 -03001666 .pm = &vpif_pm_ops,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001667 },
1668 .probe = vpif_probe,
1669 .remove = vpif_remove,
1670};
1671
Lad, Prabhakarfe424b22013-06-17 11:20:45 -03001672module_platform_driver(vpif_driver);