blob: b898779416613cac53127a46c1b6e383a0a5ae43 [file] [log] [blame]
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001/*
2 * Copyright (C) 2009 Texas Instruments Inc
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * TODO : add support for VBI & HBI data service
19 * add static buffer allocation
20 */
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030021
Lad, Prabhakar012eef72013-04-19 05:53:29 -030022#include <linux/module.h>
23#include <linux/interrupt.h>
24#include <linux/platform_device.h>
25#include <linux/slab.h>
26
Lad, Prabhakar012eef72013-04-19 05:53:29 -030027#include <media/v4l2-ioctl.h>
28
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030029#include "vpif.h"
Lad, Prabhakar012eef72013-04-19 05:53:29 -030030#include "vpif_capture.h"
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030031
32MODULE_DESCRIPTION("TI DaVinci VPIF Capture driver");
33MODULE_LICENSE("GPL");
Mauro Carvalho Chehab64dc3c12011-06-25 11:28:37 -030034MODULE_VERSION(VPIF_CAPTURE_VERSION);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030035
36#define vpif_err(fmt, arg...) v4l2_err(&vpif_obj.v4l2_dev, fmt, ## arg)
37#define vpif_dbg(level, debug, fmt, arg...) \
38 v4l2_dbg(level, debug, &vpif_obj.v4l2_dev, fmt, ## arg)
39
40static int debug = 1;
41static u32 ch0_numbuffers = 3;
42static u32 ch1_numbuffers = 3;
43static u32 ch0_bufsize = 1920 * 1080 * 2;
44static u32 ch1_bufsize = 720 * 576 * 2;
45
46module_param(debug, int, 0644);
47module_param(ch0_numbuffers, uint, S_IRUGO);
48module_param(ch1_numbuffers, uint, S_IRUGO);
49module_param(ch0_bufsize, uint, S_IRUGO);
50module_param(ch1_bufsize, uint, S_IRUGO);
51
52MODULE_PARM_DESC(debug, "Debug level 0-1");
53MODULE_PARM_DESC(ch2_numbuffers, "Channel0 buffer count (default:3)");
54MODULE_PARM_DESC(ch3_numbuffers, "Channel1 buffer count (default:3)");
55MODULE_PARM_DESC(ch2_bufsize, "Channel0 buffer size (default:1920 x 1080 x 2)");
56MODULE_PARM_DESC(ch3_bufsize, "Channel1 buffer size (default:720 x 576 x 2)");
57
58static struct vpif_config_params config_params = {
59 .min_numbuffers = 3,
60 .numbuffers[0] = 3,
61 .numbuffers[1] = 3,
62 .min_bufsize[0] = 720 * 480 * 2,
63 .min_bufsize[1] = 720 * 480 * 2,
64 .channel_bufsize[0] = 1920 * 1080 * 2,
65 .channel_bufsize[1] = 720 * 576 * 2,
66};
67
68/* global variables */
69static struct vpif_device vpif_obj = { {NULL} };
70static struct device *vpif_dev;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030071static void vpif_calculate_offsets(struct channel_obj *ch);
72static void vpif_config_addr(struct channel_obj *ch, int muxmode);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030073
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -030074static u8 channel_first_int[VPIF_NUMBER_OF_OBJECTS][2] = { {1, 1} };
75
Lad, Prabhakarc66238f2014-05-16 10:33:45 -030076/* Is set to 1 in case of SDTV formats, 2 in case of HDTV formats. */
77static int ycmux_mode;
78
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -030079static inline struct vpif_cap_buffer *to_vpif_buffer(struct vb2_buffer *vb)
80{
81 return container_of(vb, struct vpif_cap_buffer, vb);
82}
83
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030084/**
Lad, Prabhakar23e76a22014-05-16 10:33:37 -030085 * vpif_buffer_prepare : callback function for buffer prepare
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030086 * @vb: ptr to vb2_buffer
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030087 *
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030088 * This is the callback function for buffer prepare when vb2_qbuf()
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030089 * function is called. The buffer is prepared and user space virtual address
90 * or user address is converted into physical address
91 */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030092static int vpif_buffer_prepare(struct vb2_buffer *vb)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030093{
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030094 struct vb2_queue *q = vb->vb2_queue;
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -030095 struct channel_obj *ch = vb2_get_drv_priv(q);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030096 struct common_obj *common;
97 unsigned long addr;
98
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030099 vpif_dbg(2, debug, "vpif_buffer_prepare\n");
100
101 common = &ch->common[VPIF_VIDEO_INDEX];
102
Lad, Prabhakar23e76a22014-05-16 10:33:37 -0300103 vb2_set_plane_payload(vb, 0, common->fmt.fmt.pix.sizeimage);
104 if (vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
105 return -EINVAL;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300106
Lad, Prabhakar23e76a22014-05-16 10:33:37 -0300107 vb->v4l2_buf.field = common->fmt.fmt.pix.field;
108
109 addr = vb2_dma_contig_plane_dma_addr(vb, 0);
110 if (!IS_ALIGNED((addr + common->ytop_off), 8) ||
111 !IS_ALIGNED((addr + common->ybtm_off), 8) ||
112 !IS_ALIGNED((addr + common->ctop_off), 8) ||
113 !IS_ALIGNED((addr + common->cbtm_off), 8)) {
114 vpif_dbg(1, debug, "offset is not aligned\n");
115 return -EINVAL;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300116 }
Lad, Prabhakar23e76a22014-05-16 10:33:37 -0300117
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300118 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300119}
120
121/**
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300122 * vpif_buffer_queue_setup : Callback function for buffer setup.
123 * @vq: vb2_queue ptr
124 * @fmt: v4l2 format
125 * @nbuffers: ptr to number of buffers requested by application
126 * @nplanes:: contains number of distinct video planes needed to hold a frame
127 * @sizes[]: contains the size (in bytes) of each plane.
128 * @alloc_ctxs: ptr to allocation context
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300129 *
130 * This callback function is called when reqbuf() is called to adjust
131 * the buffer count and buffer size
132 */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300133static int vpif_buffer_queue_setup(struct vb2_queue *vq,
134 const struct v4l2_format *fmt,
135 unsigned int *nbuffers, unsigned int *nplanes,
136 unsigned int sizes[], void *alloc_ctxs[])
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300137{
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -0300138 struct channel_obj *ch = vb2_get_drv_priv(vq);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300139 struct common_obj *common;
140
141 common = &ch->common[VPIF_VIDEO_INDEX];
142
143 vpif_dbg(2, debug, "vpif_buffer_setup\n");
144
Lad, Prabhakar837939d2014-05-16 10:33:38 -0300145 if (fmt && fmt->fmt.pix.sizeimage < common->fmt.fmt.pix.sizeimage)
146 return -EINVAL;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300147
Lad, Prabhakar837939d2014-05-16 10:33:38 -0300148 if (vq->num_buffers + *nbuffers < 3)
149 *nbuffers = 3 - vq->num_buffers;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300150
151 *nplanes = 1;
Lad, Prabhakar837939d2014-05-16 10:33:38 -0300152 sizes[0] = fmt ? fmt->fmt.pix.sizeimage : common->fmt.fmt.pix.sizeimage;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300153 alloc_ctxs[0] = common->alloc_ctx;
154
Lad, Prabhakar837939d2014-05-16 10:33:38 -0300155 /* Calculate the offset for Y and C data in the buffer */
156 vpif_calculate_offsets(ch);
157
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300158 return 0;
159}
160
161/**
162 * vpif_buffer_queue : Callback function to add buffer to DMA queue
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300163 * @vb: ptr to vb2_buffer
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300164 */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300165static void vpif_buffer_queue(struct vb2_buffer *vb)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300166{
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -0300167 struct channel_obj *ch = vb2_get_drv_priv(vb->vb2_queue);
168 struct vpif_cap_buffer *buf = to_vpif_buffer(vb);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300169 struct common_obj *common;
Hans Verkuilaec96832012-11-16 12:03:06 -0300170 unsigned long flags;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300171
172 common = &ch->common[VPIF_VIDEO_INDEX];
173
174 vpif_dbg(2, debug, "vpif_buffer_queue\n");
175
Hans Verkuilaec96832012-11-16 12:03:06 -0300176 spin_lock_irqsave(&common->irqlock, flags);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300177 /* add the buffer to the DMA queue */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300178 list_add_tail(&buf->list, &common->dma_queue);
Hans Verkuilaec96832012-11-16 12:03:06 -0300179 spin_unlock_irqrestore(&common->irqlock, flags);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300180}
181
Lad, Prabhakar41b9f242014-05-16 10:33:39 -0300182/**
183 * vpif_start_streaming : Starts the DMA engine for streaming
184 * @vb: ptr to vb2_buffer
185 * @count: number of buffers
186 */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300187static int vpif_start_streaming(struct vb2_queue *vq, unsigned int count)
188{
189 struct vpif_capture_config *vpif_config_data =
190 vpif_dev->platform_data;
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -0300191 struct channel_obj *ch = vb2_get_drv_priv(vq);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300192 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
193 struct vpif_params *vpif = &ch->vpifparams;
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300194 struct vpif_cap_buffer *buf, *tmp;
195 unsigned long addr, flags;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300196 int ret;
197
Hans Verkuilaec96832012-11-16 12:03:06 -0300198 spin_lock_irqsave(&common->irqlock, flags);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300199
Lad, Prabhakarc66238f2014-05-16 10:33:45 -0300200 /* Initialize field_id */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300201 ch->field_id = 0;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300202
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300203 /* configure 1 or 2 channel mode */
Lad, Prabhakarf4ad8d72012-09-27 02:33:12 -0300204 if (vpif_config_data->setup_input_channel_mode) {
205 ret = vpif_config_data->
206 setup_input_channel_mode(vpif->std_info.ycmux_mode);
207 if (ret < 0) {
208 vpif_dbg(1, debug, "can't set vpif channel mode\n");
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300209 goto err;
Lad, Prabhakarf4ad8d72012-09-27 02:33:12 -0300210 }
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300211 }
212
Lad, Prabhakard9a3b132014-05-16 10:33:42 -0300213 ret = v4l2_subdev_call(ch->sd, video, s_stream, 1);
214 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
215 vpif_dbg(1, debug, "stream on failed in subdev\n");
216 goto err;
217 }
218
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300219 /* Call vpif_set_params function to set the parameters and addresses */
220 ret = vpif_set_video_params(vpif, ch->channel_id);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300221 if (ret < 0) {
222 vpif_dbg(1, debug, "can't set video params\n");
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300223 goto err;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300224 }
225
Lad, Prabhakarc66238f2014-05-16 10:33:45 -0300226 ycmux_mode = ret;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300227 vpif_config_addr(ch, ret);
228
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300229 /* Get the next frame from the buffer queue */
230 common->cur_frm = common->next_frm = list_entry(common->dma_queue.next,
231 struct vpif_cap_buffer, list);
232 /* Remove buffer from the buffer queue */
233 list_del(&common->cur_frm->list);
234 spin_unlock_irqrestore(&common->irqlock, flags);
235 /* Mark state of the current frame to active */
236 common->cur_frm->vb.state = VB2_BUF_STATE_ACTIVE;
237
238 addr = vb2_dma_contig_plane_dma_addr(&common->cur_frm->vb, 0);
239
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300240 common->set_addr(addr + common->ytop_off,
241 addr + common->ybtm_off,
242 addr + common->ctop_off,
243 addr + common->cbtm_off);
244
245 /**
246 * Set interrupt for both the fields in VPIF Register enable channel in
247 * VPIF register
248 */
Lad, Prabhakar9e184042012-09-14 10:22:24 -0300249 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
Lad, Prabhakar41b9f242014-05-16 10:33:39 -0300250 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300251 channel0_intr_assert();
252 channel0_intr_enable(1);
253 enable_channel0(1);
254 }
Lad, Prabhakar41b9f242014-05-16 10:33:39 -0300255 if (VPIF_CHANNEL1_VIDEO == ch->channel_id ||
Lad, Prabhakarc66238f2014-05-16 10:33:45 -0300256 ycmux_mode == 2) {
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300257 channel1_intr_assert();
258 channel1_intr_enable(1);
259 enable_channel1(1);
260 }
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300261
262 return 0;
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300263
264err:
265 list_for_each_entry_safe(buf, tmp, &common->dma_queue, list) {
266 list_del(&buf->list);
267 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_QUEUED);
268 }
269
270 return ret;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300271}
272
Lad, Prabhakar41b9f242014-05-16 10:33:39 -0300273/**
274 * vpif_stop_streaming : Stop the DMA engine
275 * @vq: ptr to vb2_queue
276 *
277 * This callback stops the DMA engine and any remaining buffers
278 * in the DMA queue are released.
279 */
Hans Verkuile37559b2014-04-17 02:47:21 -0300280static void vpif_stop_streaming(struct vb2_queue *vq)
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300281{
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -0300282 struct channel_obj *ch = vb2_get_drv_priv(vq);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300283 struct common_obj *common;
Hans Verkuilaec96832012-11-16 12:03:06 -0300284 unsigned long flags;
Lad, Prabhakard9a3b132014-05-16 10:33:42 -0300285 int ret;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300286
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300287 common = &ch->common[VPIF_VIDEO_INDEX];
288
Lad, Prabhakare6ba3db2014-03-22 08:03:07 -0300289 /* Disable channel as per its device type and channel id */
290 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
291 enable_channel0(0);
292 channel0_intr_enable(0);
293 }
Lad, Prabhakar41b9f242014-05-16 10:33:39 -0300294 if (VPIF_CHANNEL1_VIDEO == ch->channel_id ||
Lad, Prabhakarc66238f2014-05-16 10:33:45 -0300295 ycmux_mode == 2) {
Lad, Prabhakare6ba3db2014-03-22 08:03:07 -0300296 enable_channel1(0);
297 channel1_intr_enable(0);
298 }
Lad, Prabhakarc66238f2014-05-16 10:33:45 -0300299
300 ycmux_mode = 0;
Lad, Prabhakare6ba3db2014-03-22 08:03:07 -0300301
Lad, Prabhakard9a3b132014-05-16 10:33:42 -0300302 ret = v4l2_subdev_call(ch->sd, video, s_stream, 0);
303 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
304 vpif_dbg(1, debug, "stream off failed in subdev\n");
305
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300306 /* release all active buffers */
Hans Verkuilaec96832012-11-16 12:03:06 -0300307 spin_lock_irqsave(&common->irqlock, flags);
Lad, Prabhakare6ba3db2014-03-22 08:03:07 -0300308 if (common->cur_frm == common->next_frm) {
309 vb2_buffer_done(&common->cur_frm->vb, VB2_BUF_STATE_ERROR);
310 } else {
311 if (common->cur_frm != NULL)
312 vb2_buffer_done(&common->cur_frm->vb,
313 VB2_BUF_STATE_ERROR);
314 if (common->next_frm != NULL)
315 vb2_buffer_done(&common->next_frm->vb,
316 VB2_BUF_STATE_ERROR);
317 }
318
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300319 while (!list_empty(&common->dma_queue)) {
320 common->next_frm = list_entry(common->dma_queue.next,
321 struct vpif_cap_buffer, list);
322 list_del(&common->next_frm->list);
323 vb2_buffer_done(&common->next_frm->vb, VB2_BUF_STATE_ERROR);
324 }
Hans Verkuilaec96832012-11-16 12:03:06 -0300325 spin_unlock_irqrestore(&common->irqlock, flags);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300326}
327
328static struct vb2_ops video_qops = {
329 .queue_setup = vpif_buffer_queue_setup,
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300330 .buf_prepare = vpif_buffer_prepare,
331 .start_streaming = vpif_start_streaming,
332 .stop_streaming = vpif_stop_streaming,
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300333 .buf_queue = vpif_buffer_queue,
334};
335
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300336/**
337 * vpif_process_buffer_complete: process a completed buffer
338 * @common: ptr to common channel object
339 *
340 * This function time stamp the buffer and mark it as DONE. It also
341 * wake up any process waiting on the QUEUE and set the next buffer
342 * as current
343 */
344static void vpif_process_buffer_complete(struct common_obj *common)
345{
Sakari Ailus8e6057b2012-09-15 15:14:42 -0300346 v4l2_get_timestamp(&common->cur_frm->vb.v4l2_buf.timestamp);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300347 vb2_buffer_done(&common->cur_frm->vb,
348 VB2_BUF_STATE_DONE);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300349 /* Make curFrm pointing to nextFrm */
350 common->cur_frm = common->next_frm;
351}
352
353/**
354 * vpif_schedule_next_buffer: set next buffer address for capture
355 * @common : ptr to common channel object
356 *
357 * This function will get next buffer from the dma queue and
358 * set the buffer address in the vpif register for capture.
359 * the buffer is marked active
360 */
361static void vpif_schedule_next_buffer(struct common_obj *common)
362{
363 unsigned long addr = 0;
364
Hans Verkuilaec96832012-11-16 12:03:06 -0300365 spin_lock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300366 common->next_frm = list_entry(common->dma_queue.next,
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300367 struct vpif_cap_buffer, list);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300368 /* Remove that buffer from the buffer queue */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300369 list_del(&common->next_frm->list);
Hans Verkuilaec96832012-11-16 12:03:06 -0300370 spin_unlock(&common->irqlock);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300371 common->next_frm->vb.state = VB2_BUF_STATE_ACTIVE;
372 addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb, 0);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300373
374 /* Set top and bottom field addresses in VPIF registers */
375 common->set_addr(addr + common->ytop_off,
376 addr + common->ybtm_off,
377 addr + common->ctop_off,
378 addr + common->cbtm_off);
379}
380
381/**
382 * vpif_channel_isr : ISR handler for vpif capture
383 * @irq: irq number
384 * @dev_id: dev_id ptr
385 *
386 * It changes status of the captured buffer, takes next buffer from the queue
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300387 * and sets its address in VPIF registers
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300388 */
389static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
390{
391 struct vpif_device *dev = &vpif_obj;
392 struct common_obj *common;
393 struct channel_obj *ch;
394 enum v4l2_field field;
395 int channel_id = 0;
396 int fid = -1, i;
397
398 channel_id = *(int *)(dev_id);
Manjunath Hadlib1fc4232012-04-13 04:43:10 -0300399 if (!vpif_intr_status(channel_id))
400 return IRQ_NONE;
401
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300402 ch = dev->dev[channel_id];
403
404 field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
405
406 for (i = 0; i < VPIF_NUMBER_OF_OBJECTS; i++) {
407 common = &ch->common[i];
408 /* skip If streaming is not started in this channel */
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300409 /* Check the field format */
410 if (1 == ch->vpifparams.std_info.frm_fmt) {
411 /* Progressive mode */
Hans Verkuilaec96832012-11-16 12:03:06 -0300412 spin_lock(&common->irqlock);
413 if (list_empty(&common->dma_queue)) {
414 spin_unlock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300415 continue;
Hans Verkuilaec96832012-11-16 12:03:06 -0300416 }
417 spin_unlock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300418
419 if (!channel_first_int[i][channel_id])
420 vpif_process_buffer_complete(common);
421
422 channel_first_int[i][channel_id] = 0;
423
424 vpif_schedule_next_buffer(common);
425
426
427 channel_first_int[i][channel_id] = 0;
428 } else {
429 /**
430 * Interlaced mode. If it is first interrupt, ignore
431 * it
432 */
433 if (channel_first_int[i][channel_id]) {
434 channel_first_int[i][channel_id] = 0;
435 continue;
436 }
437 if (0 == i) {
438 ch->field_id ^= 1;
439 /* Get field id from VPIF registers */
440 fid = vpif_channel_getfid(ch->channel_id);
441 if (fid != ch->field_id) {
442 /**
443 * If field id does not match stored
444 * field id, make them in sync
445 */
446 if (0 == fid)
447 ch->field_id = fid;
448 return IRQ_HANDLED;
449 }
450 }
451 /* device field id and local field id are in sync */
452 if (0 == fid) {
453 /* this is even field */
454 if (common->cur_frm == common->next_frm)
455 continue;
456
457 /* mark the current buffer as done */
458 vpif_process_buffer_complete(common);
459 } else if (1 == fid) {
460 /* odd field */
Hans Verkuilaec96832012-11-16 12:03:06 -0300461 spin_lock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300462 if (list_empty(&common->dma_queue) ||
Hans Verkuilaec96832012-11-16 12:03:06 -0300463 (common->cur_frm != common->next_frm)) {
464 spin_unlock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300465 continue;
Hans Verkuilaec96832012-11-16 12:03:06 -0300466 }
467 spin_unlock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300468
469 vpif_schedule_next_buffer(common);
470 }
471 }
472 }
473 return IRQ_HANDLED;
474}
475
476/**
477 * vpif_update_std_info() - update standard related info
478 * @ch: ptr to channel object
479 *
480 * For a given standard selected by application, update values
481 * in the device data structures
482 */
483static int vpif_update_std_info(struct channel_obj *ch)
484{
485 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
486 struct vpif_params *vpifparams = &ch->vpifparams;
487 const struct vpif_channel_config_params *config;
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300488 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300489 struct video_obj *vid_ch = &ch->video;
490 int index;
491
492 vpif_dbg(2, debug, "vpif_update_std_info\n");
493
Mats Randgaardaa444402010-12-16 12:17:42 -0300494 for (index = 0; index < vpif_ch_params_count; index++) {
Lad, Prabhakarced9b212013-03-20 01:28:27 -0300495 config = &vpif_ch_params[index];
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300496 if (config->hd_sd == 0) {
497 vpif_dbg(2, debug, "SD format\n");
498 if (config->stdid & vid_ch->stdid) {
499 memcpy(std_info, config, sizeof(*config));
500 break;
501 }
502 } else {
503 vpif_dbg(2, debug, "HD format\n");
Hans Verkuil0598c172012-09-18 07:18:47 -0300504 if (!memcmp(&config->dv_timings, &vid_ch->dv_timings,
505 sizeof(vid_ch->dv_timings))) {
Mats Randgaard40c8bce2010-12-16 12:17:43 -0300506 memcpy(std_info, config, sizeof(*config));
507 break;
508 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300509 }
510 }
511
512 /* standard not found */
Mats Randgaardaa444402010-12-16 12:17:42 -0300513 if (index == vpif_ch_params_count)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300514 return -EINVAL;
515
516 common->fmt.fmt.pix.width = std_info->width;
517 common->width = std_info->width;
518 common->fmt.fmt.pix.height = std_info->height;
519 common->height = std_info->height;
520 common->fmt.fmt.pix.bytesperline = std_info->width;
521 vpifparams->video_params.hpitch = std_info->width;
522 vpifparams->video_params.storage_mode = std_info->frm_fmt;
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300523
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300524 return 0;
525}
526
527/**
528 * vpif_calculate_offsets : This function calculates buffers offsets
529 * @ch : ptr to channel object
530 *
531 * This function calculates buffer offsets for Y and C in the top and
532 * bottom field
533 */
534static void vpif_calculate_offsets(struct channel_obj *ch)
535{
536 unsigned int hpitch, vpitch, sizeimage;
537 struct video_obj *vid_ch = &(ch->video);
538 struct vpif_params *vpifparams = &ch->vpifparams;
539 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
540 enum v4l2_field field = common->fmt.fmt.pix.field;
541
542 vpif_dbg(2, debug, "vpif_calculate_offsets\n");
543
544 if (V4L2_FIELD_ANY == field) {
545 if (vpifparams->std_info.frm_fmt)
546 vid_ch->buf_field = V4L2_FIELD_NONE;
547 else
548 vid_ch->buf_field = V4L2_FIELD_INTERLACED;
549 } else
550 vid_ch->buf_field = common->fmt.fmt.pix.field;
551
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300552 sizeimage = common->fmt.fmt.pix.sizeimage;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300553
554 hpitch = common->fmt.fmt.pix.bytesperline;
555 vpitch = sizeimage / (hpitch * 2);
556
557 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
558 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
559 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
560 common->ytop_off = 0;
561 common->ybtm_off = hpitch;
562 common->ctop_off = sizeimage / 2;
563 common->cbtm_off = sizeimage / 2 + hpitch;
564 } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
565 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
566 common->ytop_off = 0;
567 common->ybtm_off = sizeimage / 4;
568 common->ctop_off = sizeimage / 2;
569 common->cbtm_off = common->ctop_off + sizeimage / 4;
570 } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
571 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
572 common->ybtm_off = 0;
573 common->ytop_off = sizeimage / 4;
574 common->cbtm_off = sizeimage / 2;
575 common->ctop_off = common->cbtm_off + sizeimage / 4;
576 }
577 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
578 (V4L2_FIELD_INTERLACED == vid_ch->buf_field))
579 vpifparams->video_params.storage_mode = 1;
580 else
581 vpifparams->video_params.storage_mode = 0;
582
583 if (1 == vpifparams->std_info.frm_fmt)
584 vpifparams->video_params.hpitch =
585 common->fmt.fmt.pix.bytesperline;
586 else {
587 if ((field == V4L2_FIELD_ANY)
588 || (field == V4L2_FIELD_INTERLACED))
589 vpifparams->video_params.hpitch =
590 common->fmt.fmt.pix.bytesperline * 2;
591 else
592 vpifparams->video_params.hpitch =
593 common->fmt.fmt.pix.bytesperline;
594 }
595
596 ch->vpifparams.video_params.stdid = vpifparams->std_info.stdid;
597}
598
599/**
600 * vpif_config_format: configure default frame format in the device
601 * ch : ptr to channel object
602 */
603static void vpif_config_format(struct channel_obj *ch)
604{
605 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
606
607 vpif_dbg(2, debug, "vpif_config_format\n");
608
609 common->fmt.fmt.pix.field = V4L2_FIELD_ANY;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300610 common->fmt.fmt.pix.sizeimage
611 = config_params.channel_bufsize[ch->channel_id];
612
613 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER)
614 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;
615 else
616 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
617 common->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
618}
619
620/**
621 * vpif_get_default_field() - Get default field type based on interface
622 * @vpif_params - ptr to vpif params
623 */
624static inline enum v4l2_field vpif_get_default_field(
625 struct vpif_interface *iface)
626{
627 return (iface->if_type == VPIF_IF_RAW_BAYER) ? V4L2_FIELD_NONE :
628 V4L2_FIELD_INTERLACED;
629}
630
631/**
632 * vpif_check_format() - check given pixel format for compatibility
633 * @ch - channel ptr
634 * @pixfmt - Given pixel format
635 * @update - update the values as per hardware requirement
636 *
637 * Check the application pixel format for S_FMT and update the input
638 * values as per hardware limits for TRY_FMT. The default pixel and
639 * field format is selected based on interface type.
640 */
641static int vpif_check_format(struct channel_obj *ch,
642 struct v4l2_pix_format *pixfmt,
643 int update)
644{
645 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
646 struct vpif_params *vpif_params = &ch->vpifparams;
647 enum v4l2_field field = pixfmt->field;
648 u32 sizeimage, hpitch, vpitch;
649 int ret = -EINVAL;
650
651 vpif_dbg(2, debug, "vpif_check_format\n");
652 /**
653 * first check for the pixel format. If if_type is Raw bayer,
654 * only V4L2_PIX_FMT_SBGGR8 format is supported. Otherwise only
655 * V4L2_PIX_FMT_YUV422P is supported
656 */
657 if (vpif_params->iface.if_type == VPIF_IF_RAW_BAYER) {
658 if (pixfmt->pixelformat != V4L2_PIX_FMT_SBGGR8) {
659 if (!update) {
660 vpif_dbg(2, debug, "invalid pix format\n");
661 goto exit;
662 }
663 pixfmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
664 }
665 } else {
666 if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P) {
667 if (!update) {
668 vpif_dbg(2, debug, "invalid pixel format\n");
669 goto exit;
670 }
671 pixfmt->pixelformat = V4L2_PIX_FMT_YUV422P;
672 }
673 }
674
675 if (!(VPIF_VALID_FIELD(field))) {
676 if (!update) {
677 vpif_dbg(2, debug, "invalid field format\n");
678 goto exit;
679 }
680 /**
681 * By default use FIELD_NONE for RAW Bayer capture
682 * and FIELD_INTERLACED for other interfaces
683 */
684 field = vpif_get_default_field(&vpif_params->iface);
685 } else if (field == V4L2_FIELD_ANY)
686 /* unsupported field. Use default */
687 field = vpif_get_default_field(&vpif_params->iface);
688
689 /* validate the hpitch */
690 hpitch = pixfmt->bytesperline;
691 if (hpitch < vpif_params->std_info.width) {
692 if (!update) {
693 vpif_dbg(2, debug, "invalid hpitch\n");
694 goto exit;
695 }
696 hpitch = vpif_params->std_info.width;
697 }
698
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300699 sizeimage = pixfmt->sizeimage;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300700
701 vpitch = sizeimage / (hpitch * 2);
702
703 /* validate the vpitch */
704 if (vpitch < vpif_params->std_info.height) {
705 if (!update) {
706 vpif_dbg(2, debug, "Invalid vpitch\n");
707 goto exit;
708 }
709 vpitch = vpif_params->std_info.height;
710 }
711
712 /* Check for 8 byte alignment */
713 if (!ALIGN(hpitch, 8)) {
714 if (!update) {
715 vpif_dbg(2, debug, "invalid pitch alignment\n");
716 goto exit;
717 }
718 /* adjust to next 8 byte boundary */
719 hpitch = (((hpitch + 7) / 8) * 8);
720 }
721 /* if update is set, modify the bytesperline and sizeimage */
722 if (update) {
723 pixfmt->bytesperline = hpitch;
724 pixfmt->sizeimage = hpitch * vpitch * 2;
725 }
726 /**
727 * Image width and height is always based on current standard width and
728 * height
729 */
730 pixfmt->width = common->fmt.fmt.pix.width;
731 pixfmt->height = common->fmt.fmt.pix.height;
732 return 0;
733exit:
734 return ret;
735}
736
737/**
738 * vpif_config_addr() - function to configure buffer address in vpif
739 * @ch - channel ptr
740 * @muxmode - channel mux mode
741 */
742static void vpif_config_addr(struct channel_obj *ch, int muxmode)
743{
744 struct common_obj *common;
745
746 vpif_dbg(2, debug, "vpif_config_addr\n");
747
748 common = &(ch->common[VPIF_VIDEO_INDEX]);
749
750 if (VPIF_CHANNEL1_VIDEO == ch->channel_id)
751 common->set_addr = ch1_set_videobuf_addr;
752 else if (2 == muxmode)
753 common->set_addr = ch0_set_videobuf_addr_yc_nmux;
754 else
755 common->set_addr = ch0_set_videobuf_addr;
756}
757
758/**
Hans Verkuil178cce12012-09-20 09:06:30 -0300759 * vpif_input_to_subdev() - Maps input to sub device
760 * @vpif_cfg - global config ptr
761 * @chan_cfg - channel config ptr
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300762 * @input_index - Given input index from application
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300763 *
764 * lookup the sub device information for a given input index.
765 * we report all the inputs to application. inputs table also
766 * has sub device name for the each input
767 */
Hans Verkuil178cce12012-09-20 09:06:30 -0300768static int vpif_input_to_subdev(
769 struct vpif_capture_config *vpif_cfg,
770 struct vpif_capture_chan_config *chan_cfg,
771 int input_index)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300772{
Hans Verkuil178cce12012-09-20 09:06:30 -0300773 struct vpif_subdev_info *subdev_info;
774 const char *subdev_name;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300775 int i;
776
Hans Verkuil178cce12012-09-20 09:06:30 -0300777 vpif_dbg(2, debug, "vpif_input_to_subdev\n");
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300778
Hans Verkuil178cce12012-09-20 09:06:30 -0300779 subdev_name = chan_cfg->inputs[input_index].subdev_name;
780 if (subdev_name == NULL)
781 return -1;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300782
783 /* loop through the sub device list to get the sub device info */
784 for (i = 0; i < vpif_cfg->subdev_count; i++) {
785 subdev_info = &vpif_cfg->subdev_info[i];
786 if (!strcmp(subdev_info->name, subdev_name))
Hans Verkuil178cce12012-09-20 09:06:30 -0300787 return i;
788 }
789 return -1;
790}
791
792/**
793 * vpif_set_input() - Select an input
794 * @vpif_cfg - global config ptr
795 * @ch - channel
796 * @_index - Given input index from application
797 *
798 * Select the given input.
799 */
800static int vpif_set_input(
801 struct vpif_capture_config *vpif_cfg,
802 struct channel_obj *ch,
803 int index)
804{
805 struct vpif_capture_chan_config *chan_cfg =
806 &vpif_cfg->chan_config[ch->channel_id];
807 struct vpif_subdev_info *subdev_info = NULL;
808 struct v4l2_subdev *sd = NULL;
809 u32 input = 0, output = 0;
810 int sd_index;
811 int ret;
812
813 sd_index = vpif_input_to_subdev(vpif_cfg, chan_cfg, index);
814 if (sd_index >= 0) {
815 sd = vpif_obj.sd[sd_index];
816 subdev_info = &vpif_cfg->subdev_info[sd_index];
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300817 }
818
Hans Verkuil178cce12012-09-20 09:06:30 -0300819 /* first setup input path from sub device to vpif */
820 if (sd && vpif_cfg->setup_input_path) {
821 ret = vpif_cfg->setup_input_path(ch->channel_id,
822 subdev_info->name);
823 if (ret < 0) {
824 vpif_dbg(1, debug, "couldn't setup input path for the" \
825 " sub device %s, for input index %d\n",
826 subdev_info->name, index);
827 return ret;
828 }
829 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300830
Hans Verkuil178cce12012-09-20 09:06:30 -0300831 if (sd) {
832 input = chan_cfg->inputs[index].input_route;
833 output = chan_cfg->inputs[index].output_route;
834 ret = v4l2_subdev_call(sd, video, s_routing,
835 input, output, 0);
836 if (ret < 0 && ret != -ENOIOCTLCMD) {
837 vpif_dbg(1, debug, "Failed to set input\n");
838 return ret;
839 }
840 }
841 ch->input_idx = index;
842 ch->sd = sd;
843 /* copy interface parameters to vpif */
Hans Verkuil0d4f35f2012-09-20 09:06:32 -0300844 ch->vpifparams.iface = chan_cfg->vpif_if;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300845
Hans Verkuil178cce12012-09-20 09:06:30 -0300846 /* update tvnorms from the sub device input info */
847 ch->video_dev->tvnorms = chan_cfg->inputs[index].input.std;
848 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300849}
850
851/**
852 * vpif_querystd() - querystd handler
853 * @file: file ptr
854 * @priv: file handle
855 * @std_id: ptr to std id
856 *
857 * This function is called to detect standard at the selected input
858 */
859static int vpif_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
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 int ret = 0;
864
865 vpif_dbg(2, debug, "vpif_querystd\n");
866
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300867 /* Call querystd function of decoder device */
Hans Verkuil178cce12012-09-20 09:06:30 -0300868 ret = v4l2_subdev_call(ch->sd, video, querystd, std_id);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300869
Hans Verkuil178cce12012-09-20 09:06:30 -0300870 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
871 return -ENODATA;
872 if (ret) {
873 vpif_dbg(1, debug, "Failed to query standard for sub devices\n");
874 return ret;
875 }
876
877 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300878}
879
880/**
881 * vpif_g_std() - get STD handler
882 * @file: file ptr
883 * @priv: file handle
884 * @std_id: ptr to std id
885 */
886static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
887{
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300888 struct video_device *vdev = video_devdata(file);
889 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300890
891 vpif_dbg(2, debug, "vpif_g_std\n");
892
893 *std = ch->video.stdid;
894 return 0;
895}
896
897/**
898 * vpif_s_std() - set STD handler
899 * @file: file ptr
900 * @priv: file handle
901 * @std_id: ptr to std id
902 */
Hans Verkuil314527a2013-03-15 06:10:40 -0300903static int vpif_s_std(struct file *file, void *priv, v4l2_std_id std_id)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300904{
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300905 struct video_device *vdev = video_devdata(file);
906 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300907 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
908 int ret = 0;
909
910 vpif_dbg(2, debug, "vpif_s_std\n");
911
Lad, Prabhakarc66238f2014-05-16 10:33:45 -0300912 if (vb2_is_busy(&common->buffer_queue))
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300913 return -EBUSY;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300914
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300915 /* Call encoder subdevice function to set the standard */
Hans Verkuil314527a2013-03-15 06:10:40 -0300916 ch->video.stdid = std_id;
Hans Verkuil0598c172012-09-18 07:18:47 -0300917 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300918
919 /* Get the information about the standard */
920 if (vpif_update_std_info(ch)) {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300921 vpif_err("Error getting the standard info\n");
Hans Verkuil46656af2011-01-04 06:51:35 -0300922 return -EINVAL;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300923 }
924
925 /* Configure the default format information */
926 vpif_config_format(ch);
927
928 /* set standard in the sub device */
Hans Verkuil314527a2013-03-15 06:10:40 -0300929 ret = v4l2_subdev_call(ch->sd, core, s_std, std_id);
Hans Verkuil178cce12012-09-20 09:06:30 -0300930 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300931 vpif_dbg(1, debug, "Failed to set standard for sub devices\n");
Hans Verkuil178cce12012-09-20 09:06:30 -0300932 return ret;
933 }
934 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300935}
936
937/**
938 * vpif_enum_input() - ENUMINPUT handler
939 * @file: file ptr
940 * @priv: file handle
941 * @input: ptr to input structure
942 */
943static int vpif_enum_input(struct file *file, void *priv,
944 struct v4l2_input *input)
945{
946
947 struct vpif_capture_config *config = vpif_dev->platform_data;
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300948 struct video_device *vdev = video_devdata(file);
949 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300950 struct vpif_capture_chan_config *chan_cfg;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300951
952 chan_cfg = &config->chan_config[ch->channel_id];
953
954 if (input->index >= chan_cfg->input_count) {
955 vpif_dbg(1, debug, "Invalid input index\n");
956 return -EINVAL;
957 }
958
959 memcpy(input, &chan_cfg->inputs[input->index].input,
960 sizeof(*input));
961 return 0;
962}
963
964/**
965 * vpif_g_input() - Get INPUT handler
966 * @file: file ptr
967 * @priv: file handle
968 * @index: ptr to input index
969 */
970static int vpif_g_input(struct file *file, void *priv, unsigned int *index)
971{
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300972 struct video_device *vdev = video_devdata(file);
973 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300974
Hans Verkuil6f47c6c2012-09-20 09:06:22 -0300975 *index = ch->input_idx;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300976 return 0;
977}
978
979/**
980 * vpif_s_input() - Set INPUT handler
981 * @file: file ptr
982 * @priv: file handle
983 * @index: input index
984 */
985static int vpif_s_input(struct file *file, void *priv, unsigned int index)
986{
987 struct vpif_capture_config *config = vpif_dev->platform_data;
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300988 struct video_device *vdev = video_devdata(file);
989 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300990 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300991 struct vpif_capture_chan_config *chan_cfg;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300992
993 chan_cfg = &config->chan_config[ch->channel_id];
994
Hans Verkuil7aaad132012-09-20 09:06:25 -0300995 if (index >= chan_cfg->input_count)
996 return -EINVAL;
997
Lad, Prabhakarc66238f2014-05-16 10:33:45 -0300998 if (vb2_is_busy(&common->buffer_queue))
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300999 return -EBUSY;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001000
Hans Verkuil178cce12012-09-20 09:06:30 -03001001 return vpif_set_input(config, ch, index);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001002}
1003
1004/**
1005 * vpif_enum_fmt_vid_cap() - ENUM_FMT handler
1006 * @file: file ptr
1007 * @priv: file handle
1008 * @index: input index
1009 */
1010static int vpif_enum_fmt_vid_cap(struct file *file, void *priv,
1011 struct v4l2_fmtdesc *fmt)
1012{
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001013 struct video_device *vdev = video_devdata(file);
1014 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001015
1016 if (fmt->index != 0) {
1017 vpif_dbg(1, debug, "Invalid format index\n");
1018 return -EINVAL;
1019 }
1020
1021 /* Fill in the information about format */
1022 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER) {
1023 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1024 strcpy(fmt->description, "Raw Mode -Bayer Pattern GrRBGb");
1025 fmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
1026 } else {
1027 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1028 strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
1029 fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
1030 }
1031 return 0;
1032}
1033
1034/**
1035 * vpif_try_fmt_vid_cap() - TRY_FMT handler
1036 * @file: file ptr
1037 * @priv: file handle
1038 * @fmt: ptr to v4l2 format structure
1039 */
1040static int vpif_try_fmt_vid_cap(struct file *file, void *priv,
1041 struct v4l2_format *fmt)
1042{
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001043 struct video_device *vdev = video_devdata(file);
1044 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001045 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
1046
1047 return vpif_check_format(ch, pixfmt, 1);
1048}
1049
1050
1051/**
1052 * vpif_g_fmt_vid_cap() - Set INPUT handler
1053 * @file: file ptr
1054 * @priv: file handle
1055 * @fmt: ptr to v4l2 format structure
1056 */
1057static int vpif_g_fmt_vid_cap(struct file *file, void *priv,
1058 struct v4l2_format *fmt)
1059{
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001060 struct video_device *vdev = video_devdata(file);
1061 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001062 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1063
1064 /* Check the validity of the buffer type */
1065 if (common->fmt.type != fmt->type)
1066 return -EINVAL;
1067
1068 /* Fill in the information about format */
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001069 *fmt = common->fmt;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001070 return 0;
1071}
1072
1073/**
1074 * vpif_s_fmt_vid_cap() - Set FMT handler
1075 * @file: file ptr
1076 * @priv: file handle
1077 * @fmt: ptr to v4l2 format structure
1078 */
1079static int vpif_s_fmt_vid_cap(struct file *file, void *priv,
1080 struct v4l2_format *fmt)
1081{
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001082 struct video_device *vdev = video_devdata(file);
1083 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001084 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1085 struct v4l2_pix_format *pixfmt;
1086 int ret = 0;
1087
Mats Randgaard2c0ddd12010-12-16 12:17:45 -03001088 vpif_dbg(2, debug, "%s\n", __func__);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001089
Lad, Prabhakarc66238f2014-05-16 10:33:45 -03001090 if (vb2_is_busy(&common->buffer_queue))
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001091 return -EBUSY;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001092
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001093 pixfmt = &fmt->fmt.pix;
1094 /* Check for valid field format */
1095 ret = vpif_check_format(ch, pixfmt, 0);
1096
1097 if (ret)
1098 return ret;
1099 /* store the format in the channel object */
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001100 common->fmt = *fmt;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001101 return 0;
1102}
1103
1104/**
1105 * vpif_querycap() - QUERYCAP handler
1106 * @file: file ptr
1107 * @priv: file handle
1108 * @cap: ptr to v4l2_capability structure
1109 */
1110static int vpif_querycap(struct file *file, void *priv,
1111 struct v4l2_capability *cap)
1112{
1113 struct vpif_capture_config *config = vpif_dev->platform_data;
1114
Lad, Prabhakar626d533f2012-09-25 11:21:55 -03001115 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1116 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1117 snprintf(cap->driver, sizeof(cap->driver), "%s", dev_name(vpif_dev));
1118 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
1119 dev_name(vpif_dev));
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001120 strlcpy(cap->card, config->card_name, sizeof(cap->card));
1121
1122 return 0;
1123}
1124
1125/**
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001126 * vpif_cropcap() - cropcap handler
1127 * @file: file ptr
1128 * @priv: file handle
1129 * @crop: ptr to v4l2_cropcap structure
1130 */
1131static int vpif_cropcap(struct file *file, void *priv,
1132 struct v4l2_cropcap *crop)
1133{
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001134 struct video_device *vdev = video_devdata(file);
1135 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001136 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1137
1138 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != crop->type)
1139 return -EINVAL;
1140
1141 crop->bounds.left = 0;
1142 crop->bounds.top = 0;
1143 crop->bounds.height = common->height;
1144 crop->bounds.width = common->width;
1145 crop->defrect = crop->bounds;
1146 return 0;
1147}
1148
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001149/**
Hans Verkuil0598c172012-09-18 07:18:47 -03001150 * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001151 * @file: file ptr
1152 * @priv: file handle
Hans Verkuil0598c172012-09-18 07:18:47 -03001153 * @timings: input timings
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001154 */
Hans Verkuil0598c172012-09-18 07:18:47 -03001155static int
1156vpif_enum_dv_timings(struct file *file, void *priv,
1157 struct v4l2_enum_dv_timings *timings)
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001158{
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001159 struct video_device *vdev = video_devdata(file);
1160 struct channel_obj *ch = video_get_drvdata(vdev);
Hans Verkuil178cce12012-09-20 09:06:30 -03001161 int ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001162
Hans Verkuil178cce12012-09-20 09:06:30 -03001163 ret = v4l2_subdev_call(ch->sd, video, enum_dv_timings, timings);
Wei Yongjune070f1b2012-10-30 09:45:00 -03001164 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
Hans Verkuil178cce12012-09-20 09:06:30 -03001165 return -EINVAL;
1166 return ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001167}
1168
1169/**
Hans Verkuil0598c172012-09-18 07:18:47 -03001170 * vpif_query_dv_timings() - QUERY_DV_TIMINGS handler
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001171 * @file: file ptr
1172 * @priv: file handle
Hans Verkuil0598c172012-09-18 07:18:47 -03001173 * @timings: input timings
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001174 */
Hans Verkuil0598c172012-09-18 07:18:47 -03001175static int
1176vpif_query_dv_timings(struct file *file, void *priv,
1177 struct v4l2_dv_timings *timings)
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001178{
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001179 struct video_device *vdev = video_devdata(file);
1180 struct channel_obj *ch = video_get_drvdata(vdev);
Hans Verkuil178cce12012-09-20 09:06:30 -03001181 int ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001182
Hans Verkuil178cce12012-09-20 09:06:30 -03001183 ret = v4l2_subdev_call(ch->sd, video, query_dv_timings, timings);
Wei Yongjune070f1b2012-10-30 09:45:00 -03001184 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
Hans Verkuil178cce12012-09-20 09:06:30 -03001185 return -ENODATA;
1186 return ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001187}
1188
Mats Randgaardc027e162010-12-16 12:17:44 -03001189/**
1190 * vpif_s_dv_timings() - S_DV_TIMINGS handler
1191 * @file: file ptr
1192 * @priv: file handle
1193 * @timings: digital video timings
1194 */
1195static int vpif_s_dv_timings(struct file *file, void *priv,
1196 struct v4l2_dv_timings *timings)
1197{
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001198 struct video_device *vdev = video_devdata(file);
1199 struct channel_obj *ch = video_get_drvdata(vdev);
Mats Randgaardc027e162010-12-16 12:17:44 -03001200 struct vpif_params *vpifparams = &ch->vpifparams;
1201 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
1202 struct video_obj *vid_ch = &ch->video;
Hans Verkuil0598c172012-09-18 07:18:47 -03001203 struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
Mats Randgaardc027e162010-12-16 12:17:44 -03001204 int ret;
1205
1206 if (timings->type != V4L2_DV_BT_656_1120) {
1207 vpif_dbg(2, debug, "Timing type not defined\n");
1208 return -EINVAL;
1209 }
1210
1211 /* Configure subdevice timings, if any */
Hans Verkuil178cce12012-09-20 09:06:30 -03001212 ret = v4l2_subdev_call(ch->sd, video, s_dv_timings, timings);
1213 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1214 ret = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001215 if (ret < 0) {
1216 vpif_dbg(2, debug, "Error setting custom DV timings\n");
1217 return ret;
1218 }
1219
1220 if (!(timings->bt.width && timings->bt.height &&
1221 (timings->bt.hbackporch ||
1222 timings->bt.hfrontporch ||
1223 timings->bt.hsync) &&
1224 timings->bt.vfrontporch &&
1225 (timings->bt.vbackporch ||
1226 timings->bt.vsync))) {
1227 vpif_dbg(2, debug, "Timings for width, height, "
1228 "horizontal back porch, horizontal sync, "
1229 "horizontal front porch, vertical back porch, "
1230 "vertical sync and vertical back porch "
1231 "must be defined\n");
1232 return -EINVAL;
1233 }
1234
Hans Verkuil0598c172012-09-18 07:18:47 -03001235 vid_ch->dv_timings = *timings;
Mats Randgaardc027e162010-12-16 12:17:44 -03001236
1237 /* Configure video port timings */
1238
Hans Verkuile3655262013-07-29 08:41:00 -03001239 std_info->eav2sav = V4L2_DV_BT_BLANKING_WIDTH(bt) - 8;
Mats Randgaardc027e162010-12-16 12:17:44 -03001240 std_info->sav2eav = bt->width;
1241
1242 std_info->l1 = 1;
1243 std_info->l3 = bt->vsync + bt->vbackporch + 1;
1244
Hans Verkuile3655262013-07-29 08:41:00 -03001245 std_info->vsize = V4L2_DV_BT_FRAME_HEIGHT(bt);
Mats Randgaardc027e162010-12-16 12:17:44 -03001246 if (bt->interlaced) {
1247 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
Mats Randgaardc027e162010-12-16 12:17:44 -03001248 std_info->l5 = std_info->vsize/2 -
1249 (bt->vfrontporch - 1);
1250 std_info->l7 = std_info->vsize/2 + 1;
1251 std_info->l9 = std_info->l7 + bt->il_vsync +
1252 bt->il_vbackporch + 1;
1253 std_info->l11 = std_info->vsize -
1254 (bt->il_vfrontporch - 1);
1255 } else {
1256 vpif_dbg(2, debug, "Required timing values for "
1257 "interlaced BT format missing\n");
1258 return -EINVAL;
1259 }
1260 } else {
Mats Randgaardc027e162010-12-16 12:17:44 -03001261 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
1262 }
1263 strncpy(std_info->name, "Custom timings BT656/1120", VPIF_MAX_NAME);
1264 std_info->width = bt->width;
1265 std_info->height = bt->height;
1266 std_info->frm_fmt = bt->interlaced ? 0 : 1;
1267 std_info->ycmux_mode = 0;
1268 std_info->capture_format = 0;
1269 std_info->vbi_supported = 0;
1270 std_info->hd_sd = 1;
1271 std_info->stdid = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001272
1273 vid_ch->stdid = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001274 return 0;
1275}
1276
1277/**
1278 * vpif_g_dv_timings() - G_DV_TIMINGS handler
1279 * @file: file ptr
1280 * @priv: file handle
1281 * @timings: digital video timings
1282 */
1283static int vpif_g_dv_timings(struct file *file, void *priv,
1284 struct v4l2_dv_timings *timings)
1285{
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001286 struct video_device *vdev = video_devdata(file);
1287 struct channel_obj *ch = video_get_drvdata(vdev);
Mats Randgaardc027e162010-12-16 12:17:44 -03001288 struct video_obj *vid_ch = &ch->video;
Mats Randgaardc027e162010-12-16 12:17:44 -03001289
Hans Verkuil0598c172012-09-18 07:18:47 -03001290 *timings = vid_ch->dv_timings;
Mats Randgaardc027e162010-12-16 12:17:44 -03001291
1292 return 0;
1293}
1294
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001295/*
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001296 * vpif_log_status() - Status information
1297 * @file: file ptr
1298 * @priv: file handle
1299 *
1300 * Returns zero.
1301 */
1302static int vpif_log_status(struct file *filep, void *priv)
1303{
1304 /* status for sub devices */
1305 v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1306
1307 return 0;
1308}
1309
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001310/* vpif capture ioctl operations */
1311static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
1312 .vidioc_querycap = vpif_querycap,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001313 .vidioc_enum_fmt_vid_cap = vpif_enum_fmt_vid_cap,
1314 .vidioc_g_fmt_vid_cap = vpif_g_fmt_vid_cap,
1315 .vidioc_s_fmt_vid_cap = vpif_s_fmt_vid_cap,
1316 .vidioc_try_fmt_vid_cap = vpif_try_fmt_vid_cap,
1317 .vidioc_enum_input = vpif_enum_input,
1318 .vidioc_s_input = vpif_s_input,
1319 .vidioc_g_input = vpif_g_input,
Lad, Prabhakard9a3b132014-05-16 10:33:42 -03001320
1321 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1322 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1323 .vidioc_querybuf = vb2_ioctl_querybuf,
1324 .vidioc_qbuf = vb2_ioctl_qbuf,
1325 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1326 .vidioc_expbuf = vb2_ioctl_expbuf,
1327 .vidioc_streamon = vb2_ioctl_streamon,
1328 .vidioc_streamoff = vb2_ioctl_streamoff,
1329
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001330 .vidioc_querystd = vpif_querystd,
1331 .vidioc_s_std = vpif_s_std,
1332 .vidioc_g_std = vpif_g_std,
Lad, Prabhakard9a3b132014-05-16 10:33:42 -03001333
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001334 .vidioc_cropcap = vpif_cropcap,
Hans Verkuil0598c172012-09-18 07:18:47 -03001335 .vidioc_enum_dv_timings = vpif_enum_dv_timings,
1336 .vidioc_query_dv_timings = vpif_query_dv_timings,
Mats Randgaardc027e162010-12-16 12:17:44 -03001337 .vidioc_s_dv_timings = vpif_s_dv_timings,
1338 .vidioc_g_dv_timings = vpif_g_dv_timings,
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001339 .vidioc_log_status = vpif_log_status,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001340};
1341
1342/* vpif file operations */
1343static struct v4l2_file_operations vpif_fops = {
1344 .owner = THIS_MODULE,
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001345 .open = v4l2_fh_open,
1346 .release = vb2_fop_release,
Hans Verkuil46656af2011-01-04 06:51:35 -03001347 .unlocked_ioctl = video_ioctl2,
Lad, Prabhakard26d26b2014-05-16 10:33:40 -03001348 .mmap = vb2_fop_mmap,
1349 .poll = vb2_fop_poll
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001350};
1351
1352/* vpif video template */
1353static struct video_device vpif_video_template = {
1354 .name = "vpif",
1355 .fops = &vpif_fops,
1356 .minor = -1,
1357 .ioctl_ops = &vpif_ioctl_ops,
1358};
1359
1360/**
1361 * initialize_vpif() - Initialize vpif data structures
1362 *
1363 * Allocate memory for data structures and initialize them
1364 */
1365static int initialize_vpif(void)
1366{
1367 int err = 0, i, j;
1368 int free_channel_objects_index;
1369
1370 /* Default number of buffers should be 3 */
1371 if ((ch0_numbuffers > 0) &&
1372 (ch0_numbuffers < config_params.min_numbuffers))
1373 ch0_numbuffers = config_params.min_numbuffers;
1374 if ((ch1_numbuffers > 0) &&
1375 (ch1_numbuffers < config_params.min_numbuffers))
1376 ch1_numbuffers = config_params.min_numbuffers;
1377
1378 /* Set buffer size to min buffers size if it is invalid */
1379 if (ch0_bufsize < config_params.min_bufsize[VPIF_CHANNEL0_VIDEO])
1380 ch0_bufsize =
1381 config_params.min_bufsize[VPIF_CHANNEL0_VIDEO];
1382 if (ch1_bufsize < config_params.min_bufsize[VPIF_CHANNEL1_VIDEO])
1383 ch1_bufsize =
1384 config_params.min_bufsize[VPIF_CHANNEL1_VIDEO];
1385
1386 config_params.numbuffers[VPIF_CHANNEL0_VIDEO] = ch0_numbuffers;
1387 config_params.numbuffers[VPIF_CHANNEL1_VIDEO] = ch1_numbuffers;
1388 if (ch0_numbuffers) {
1389 config_params.channel_bufsize[VPIF_CHANNEL0_VIDEO]
1390 = ch0_bufsize;
1391 }
1392 if (ch1_numbuffers) {
1393 config_params.channel_bufsize[VPIF_CHANNEL1_VIDEO]
1394 = ch1_bufsize;
1395 }
1396
1397 /* Allocate memory for six channel objects */
1398 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1399 vpif_obj.dev[i] =
1400 kzalloc(sizeof(*vpif_obj.dev[i]), GFP_KERNEL);
1401 /* If memory allocation fails, return error */
1402 if (!vpif_obj.dev[i]) {
1403 free_channel_objects_index = i;
1404 err = -ENOMEM;
1405 goto vpif_init_free_channel_objects;
1406 }
1407 }
1408 return 0;
1409
1410vpif_init_free_channel_objects:
1411 for (j = 0; j < free_channel_objects_index; j++)
1412 kfree(vpif_obj.dev[j]);
1413 return err;
1414}
1415
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001416static int vpif_async_bound(struct v4l2_async_notifier *notifier,
1417 struct v4l2_subdev *subdev,
1418 struct v4l2_async_subdev *asd)
1419{
1420 int i;
1421
1422 for (i = 0; i < vpif_obj.config->subdev_count; i++)
1423 if (!strcmp(vpif_obj.config->subdev_info[i].name,
1424 subdev->name)) {
1425 vpif_obj.sd[i] = subdev;
1426 return 0;
1427 }
1428
1429 return -EINVAL;
1430}
1431
1432static int vpif_probe_complete(void)
1433{
1434 struct common_obj *common;
Lad, Prabhakard26d26b2014-05-16 10:33:40 -03001435 struct video_device *vdev;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001436 struct channel_obj *ch;
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001437 struct vb2_queue *q;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001438 int i, j, err, k;
1439
1440 for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
1441 ch = vpif_obj.dev[j];
1442 ch->channel_id = j;
1443 common = &(ch->common[VPIF_VIDEO_INDEX]);
1444 spin_lock_init(&common->irqlock);
1445 mutex_init(&common->lock);
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001446
1447 /* select input 0 */
1448 err = vpif_set_input(vpif_obj.config, ch, 0);
1449 if (err)
1450 goto probe_out;
1451
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001452 /* Initialize vb2 queue */
1453 q = &common->buffer_queue;
1454 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1455 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1456 q->drv_priv = ch;
1457 q->ops = &video_qops;
1458 q->mem_ops = &vb2_dma_contig_memops;
1459 q->buf_struct_size = sizeof(struct vpif_cap_buffer);
1460 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1461 q->min_buffers_needed = 1;
Lad, Prabhakar999ba692014-05-16 10:33:33 -03001462 q->lock = &common->lock;
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001463
1464 err = vb2_queue_init(q);
1465 if (err) {
1466 vpif_err("vpif_capture: vb2_queue_init() failed\n");
1467 goto probe_out;
1468 }
1469
1470 common->alloc_ctx = vb2_dma_contig_init_ctx(vpif_dev);
1471 if (IS_ERR(common->alloc_ctx)) {
1472 vpif_err("Failed to get the context\n");
1473 err = PTR_ERR(common->alloc_ctx);
1474 goto probe_out;
1475 }
1476
1477 INIT_LIST_HEAD(&common->dma_queue);
1478
Lad, Prabhakard26d26b2014-05-16 10:33:40 -03001479 vdev = ch->video_dev;
1480 vdev->queue = q;
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001481 vdev->lock = &common->lock;
1482 set_bit(V4L2_FL_USE_FH_PRIO, &vdev->flags);
1483 video_set_drvdata(ch->video_dev, ch);
Lad, Prabhakard26d26b2014-05-16 10:33:40 -03001484 err = video_register_device(vdev,
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001485 VFL_TYPE_GRABBER, (j ? 1 : 0));
1486 if (err)
1487 goto probe_out;
1488 }
1489
1490 v4l2_info(&vpif_obj.v4l2_dev, "VPIF capture driver initialized\n");
1491 return 0;
1492
1493probe_out:
1494 for (k = 0; k < j; k++) {
1495 /* Get the pointer to the channel object */
1496 ch = vpif_obj.dev[k];
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001497 common = &ch->common[k];
1498 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001499 /* Unregister video device */
1500 video_unregister_device(ch->video_dev);
1501 }
1502 kfree(vpif_obj.sd);
1503 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1504 ch = vpif_obj.dev[i];
1505 /* Note: does nothing if ch->video_dev == NULL */
1506 video_device_release(ch->video_dev);
1507 }
1508 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1509
1510 return err;
1511}
1512
1513static int vpif_async_complete(struct v4l2_async_notifier *notifier)
1514{
1515 return vpif_probe_complete();
1516}
1517
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001518/**
1519 * vpif_probe : This function probes the vpif capture driver
1520 * @pdev: platform device pointer
1521 *
1522 * This creates device entries by register itself to the V4L2 driver and
1523 * initializes fields of each channel objects
1524 */
1525static __init int vpif_probe(struct platform_device *pdev)
1526{
1527 struct vpif_subdev_info *subdevdata;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001528 int i, j, err;
Hans Verkuil5be452c2012-09-20 09:06:29 -03001529 int res_idx = 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001530 struct i2c_adapter *i2c_adap;
1531 struct channel_obj *ch;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001532 struct video_device *vfd;
1533 struct resource *res;
1534 int subdev_count;
1535
1536 vpif_dev = &pdev->dev;
1537
1538 err = initialize_vpif();
1539 if (err) {
1540 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
1541 return err;
1542 }
1543
Hans Verkuild2f7a1a2011-12-13 05:44:42 -03001544 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
1545 if (err) {
1546 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
1547 return err;
1548 }
1549
Hans Verkuil5be452c2012-09-20 09:06:29 -03001550 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
Lad, Prabhakara76a0b32013-06-17 11:20:47 -03001551 err = devm_request_irq(&pdev->dev, res->start, vpif_channel_isr,
1552 IRQF_SHARED, "VPIF_Capture",
1553 (void *)(&vpif_obj.dev[res_idx]->
1554 channel_id));
1555 if (err) {
1556 err = -EINVAL;
1557 goto vpif_unregister;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001558 }
Hans Verkuil5be452c2012-09-20 09:06:29 -03001559 res_idx++;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001560 }
1561
1562 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1563 /* Get the pointer to the channel object */
1564 ch = vpif_obj.dev[i];
1565 /* Allocate memory for video device */
1566 vfd = video_device_alloc();
1567 if (NULL == vfd) {
1568 for (j = 0; j < i; j++) {
1569 ch = vpif_obj.dev[j];
1570 video_device_release(ch->video_dev);
1571 }
1572 err = -ENOMEM;
Lad, Prabhakarb2de4f22013-06-17 11:20:46 -03001573 goto vpif_unregister;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001574 }
1575
1576 /* Initialize field of video device */
1577 *vfd = vpif_video_template;
1578 vfd->v4l2_dev = &vpif_obj.v4l2_dev;
1579 vfd->release = video_device_release;
1580 snprintf(vfd->name, sizeof(vfd->name),
Manjunath Hadli0a631722012-04-13 04:44:00 -03001581 "VPIF_Capture_DRIVER_V%s",
Mauro Carvalho Chehab64dc3c12011-06-25 11:28:37 -03001582 VPIF_CAPTURE_VERSION);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001583 /* Set video_dev to the video device */
1584 ch->video_dev = vfd;
1585 }
1586
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001587 vpif_obj.config = pdev->dev.platform_data;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001588
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001589 subdev_count = vpif_obj.config->subdev_count;
Mats Randgaard1f8766b2010-08-30 10:30:37 -03001590 vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001591 GFP_KERNEL);
1592 if (vpif_obj.sd == NULL) {
1593 vpif_err("unable to allocate memory for subdevice pointers\n");
1594 err = -ENOMEM;
Hans Verkuil5be452c2012-09-20 09:06:29 -03001595 goto vpif_sd_error;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001596 }
1597
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001598 if (!vpif_obj.config->asd_sizes) {
1599 i2c_adap = i2c_get_adapter(1);
1600 for (i = 0; i < subdev_count; i++) {
1601 subdevdata = &vpif_obj.config->subdev_info[i];
1602 vpif_obj.sd[i] =
1603 v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
1604 i2c_adap,
1605 &subdevdata->
1606 board_info,
1607 NULL);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001608
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001609 if (!vpif_obj.sd[i]) {
1610 vpif_err("Error registering v4l2 subdevice\n");
Wei Yongjun2fcd9dc2013-09-02 05:06:10 -03001611 err = -ENODEV;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001612 goto probe_subdev_out;
1613 }
1614 v4l2_info(&vpif_obj.v4l2_dev,
1615 "registered sub device %s\n",
1616 subdevdata->name);
1617 }
1618 vpif_probe_complete();
1619 } else {
Sylwester Nawrockie8419d02013-07-19 12:31:10 -03001620 vpif_obj.notifier.subdevs = vpif_obj.config->asd;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001621 vpif_obj.notifier.num_subdevs = vpif_obj.config->asd_sizes[0];
1622 vpif_obj.notifier.bound = vpif_async_bound;
1623 vpif_obj.notifier.complete = vpif_async_complete;
1624 err = v4l2_async_notifier_register(&vpif_obj.v4l2_dev,
1625 &vpif_obj.notifier);
1626 if (err) {
1627 vpif_err("Error registering async notifier\n");
1628 err = -EINVAL;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001629 goto probe_subdev_out;
1630 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001631 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001632
1633 return 0;
1634
Hans Verkuilb65814e2012-09-20 09:06:26 -03001635probe_subdev_out:
1636 /* free sub devices memory */
1637 kfree(vpif_obj.sd);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001638
Hans Verkuil5be452c2012-09-20 09:06:29 -03001639vpif_sd_error:
1640 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1641 ch = vpif_obj.dev[i];
1642 /* Note: does nothing if ch->video_dev == NULL */
1643 video_device_release(ch->video_dev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001644 }
Lad, Prabhakarb2de4f22013-06-17 11:20:46 -03001645vpif_unregister:
Hans Verkuild2f7a1a2011-12-13 05:44:42 -03001646 v4l2_device_unregister(&vpif_obj.v4l2_dev);
Lad, Prabhakarb2de4f22013-06-17 11:20:46 -03001647
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001648 return err;
1649}
1650
1651/**
1652 * vpif_remove() - driver remove handler
1653 * @device: ptr to platform device structure
1654 *
1655 * The vidoe device is unregistered
1656 */
1657static int vpif_remove(struct platform_device *device)
1658{
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001659 struct common_obj *common;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001660 struct channel_obj *ch;
Lad, Prabhakarb2de4f22013-06-17 11:20:46 -03001661 int i;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001662
1663 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1664
Lad, Prabhakar410ca602013-06-17 11:20:44 -03001665 kfree(vpif_obj.sd);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001666 /* un-register device */
1667 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1668 /* Get the pointer to the channel object */
1669 ch = vpif_obj.dev[i];
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001670 common = &ch->common[i];
1671 vb2_dma_contig_cleanup_ctx(common->alloc_ctx);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001672 /* Unregister video device */
1673 video_unregister_device(ch->video_dev);
Lad, Prabhakar410ca602013-06-17 11:20:44 -03001674 kfree(vpif_obj.dev[i]);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001675 }
1676 return 0;
1677}
1678
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001679#ifdef CONFIG_PM
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001680/**
1681 * vpif_suspend: vpif device suspend
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001682 */
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001683static int vpif_suspend(struct device *dev)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001684{
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001685
1686 struct common_obj *common;
1687 struct channel_obj *ch;
1688 int i;
1689
1690 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1691 /* Get the pointer to the channel object */
1692 ch = vpif_obj.dev[i];
1693 common = &ch->common[VPIF_VIDEO_INDEX];
1694 mutex_lock(&common->lock);
1695 if (ch->usrs && common->io_usrs) {
1696 /* Disable channel */
1697 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
1698 enable_channel0(0);
1699 channel0_intr_enable(0);
1700 }
1701 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
Lad, Prabhakarc66238f2014-05-16 10:33:45 -03001702 ycmux_mode == 2) {
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001703 enable_channel1(0);
1704 channel1_intr_enable(0);
1705 }
1706 }
1707 mutex_unlock(&common->lock);
1708 }
1709
1710 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001711}
1712
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001713/*
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001714 * vpif_resume: vpif device suspend
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001715 */
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001716static int vpif_resume(struct device *dev)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001717{
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001718 struct common_obj *common;
1719 struct channel_obj *ch;
1720 int i;
1721
1722 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1723 /* Get the pointer to the channel object */
1724 ch = vpif_obj.dev[i];
1725 common = &ch->common[VPIF_VIDEO_INDEX];
1726 mutex_lock(&common->lock);
1727 if (ch->usrs && common->io_usrs) {
1728 /* Disable channel */
1729 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
1730 enable_channel0(1);
1731 channel0_intr_enable(1);
1732 }
1733 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
Lad, Prabhakarc66238f2014-05-16 10:33:45 -03001734 ycmux_mode == 2) {
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001735 enable_channel1(1);
1736 channel1_intr_enable(1);
1737 }
1738 }
1739 mutex_unlock(&common->lock);
1740 }
1741
1742 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001743}
1744
Alexey Dobriyan47145212009-12-14 18:00:08 -08001745static const struct dev_pm_ops vpif_dev_pm_ops = {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001746 .suspend = vpif_suspend,
1747 .resume = vpif_resume,
1748};
1749
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001750#define vpif_pm_ops (&vpif_dev_pm_ops)
1751#else
1752#define vpif_pm_ops NULL
1753#endif
1754
Mats Randgaardffa1b392010-08-30 10:30:36 -03001755static __refdata struct platform_driver vpif_driver = {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001756 .driver = {
1757 .name = "vpif_capture",
1758 .owner = THIS_MODULE,
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001759 .pm = vpif_pm_ops,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001760 },
1761 .probe = vpif_probe,
1762 .remove = vpif_remove,
1763};
1764
Lad, Prabhakarfe424b22013-06-17 11:20:45 -03001765module_platform_driver(vpif_driver);