blob: 5104cc0ee40e35ca2b973efc9ebad4b2a71bdd6e [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
Junghak Sung2d700712015-09-22 10:30:30 -030060static inline
61struct vpif_cap_buffer *to_vpif_buffer(struct vb2_v4l2_buffer *vb)
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -030062{
63 return container_of(vb, struct vpif_cap_buffer, vb);
64}
65
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030066/**
Lad, Prabhakar23e76a22014-05-16 10:33:37 -030067 * vpif_buffer_prepare : callback function for buffer prepare
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030068 * @vb: ptr to vb2_buffer
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030069 *
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030070 * This is the callback function for buffer prepare when vb2_qbuf()
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030071 * function is called. The buffer is prepared and user space virtual address
72 * or user address is converted into physical address
73 */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030074static int vpif_buffer_prepare(struct vb2_buffer *vb)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030075{
Junghak Sung2d700712015-09-22 10:30:30 -030076 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -030077 struct vb2_queue *q = vb->vb2_queue;
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -030078 struct channel_obj *ch = vb2_get_drv_priv(q);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030079 struct common_obj *common;
80 unsigned long addr;
81
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030082 vpif_dbg(2, debug, "vpif_buffer_prepare\n");
83
84 common = &ch->common[VPIF_VIDEO_INDEX];
85
Lad, Prabhakar23e76a22014-05-16 10:33:37 -030086 vb2_set_plane_payload(vb, 0, common->fmt.fmt.pix.sizeimage);
87 if (vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
88 return -EINVAL;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030089
Junghak Sung2d700712015-09-22 10:30:30 -030090 vbuf->field = common->fmt.fmt.pix.field;
Lad, Prabhakar23e76a22014-05-16 10:33:37 -030091
92 addr = vb2_dma_contig_plane_dma_addr(vb, 0);
93 if (!IS_ALIGNED((addr + common->ytop_off), 8) ||
94 !IS_ALIGNED((addr + common->ybtm_off), 8) ||
95 !IS_ALIGNED((addr + common->ctop_off), 8) ||
96 !IS_ALIGNED((addr + common->cbtm_off), 8)) {
97 vpif_dbg(1, debug, "offset is not aligned\n");
98 return -EINVAL;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -030099 }
Lad, Prabhakar23e76a22014-05-16 10:33:37 -0300100
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300101 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300102}
103
104/**
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300105 * vpif_buffer_queue_setup : Callback function for buffer setup.
106 * @vq: vb2_queue ptr
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300107 * @nbuffers: ptr to number of buffers requested by application
108 * @nplanes:: contains number of distinct video planes needed to hold a frame
109 * @sizes[]: contains the size (in bytes) of each plane.
Hans Verkuil36c0f8b2016-04-15 09:15:05 -0300110 * @alloc_devs: ptr to allocation context
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300111 *
112 * This callback function is called when reqbuf() is called to adjust
113 * the buffer count and buffer size
114 */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300115static int vpif_buffer_queue_setup(struct vb2_queue *vq,
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300116 unsigned int *nbuffers, unsigned int *nplanes,
Hans Verkuil36c0f8b2016-04-15 09:15:05 -0300117 unsigned int sizes[], struct device *alloc_devs[])
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);
Hans Verkuildf9ecb02015-10-28 00:50:37 -0200120 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
121 unsigned size = common->fmt.fmt.pix.sizeimage;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300122
123 vpif_dbg(2, debug, "vpif_buffer_setup\n");
124
Hans Verkuildf9ecb02015-10-28 00:50:37 -0200125 if (*nplanes) {
126 if (sizes[0] < size)
127 return -EINVAL;
128 size = sizes[0];
129 }
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300130
Lad, Prabhakar837939d2014-05-16 10:33:38 -0300131 if (vq->num_buffers + *nbuffers < 3)
132 *nbuffers = 3 - vq->num_buffers;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300133
134 *nplanes = 1;
Hans Verkuildf9ecb02015-10-28 00:50:37 -0200135 sizes[0] = size;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300136
Lad, Prabhakar837939d2014-05-16 10:33:38 -0300137 /* Calculate the offset for Y and C data in the buffer */
138 vpif_calculate_offsets(ch);
139
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300140 return 0;
141}
142
143/**
144 * vpif_buffer_queue : Callback function to add buffer to DMA queue
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300145 * @vb: ptr to vb2_buffer
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300146 */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300147static void vpif_buffer_queue(struct vb2_buffer *vb)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300148{
Junghak Sung2d700712015-09-22 10:30:30 -0300149 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -0300150 struct channel_obj *ch = vb2_get_drv_priv(vb->vb2_queue);
Junghak Sung2d700712015-09-22 10:30:30 -0300151 struct vpif_cap_buffer *buf = to_vpif_buffer(vbuf);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300152 struct common_obj *common;
Hans Verkuilaec96832012-11-16 12:03:06 -0300153 unsigned long flags;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300154
155 common = &ch->common[VPIF_VIDEO_INDEX];
156
157 vpif_dbg(2, debug, "vpif_buffer_queue\n");
158
Hans Verkuilaec96832012-11-16 12:03:06 -0300159 spin_lock_irqsave(&common->irqlock, flags);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300160 /* add the buffer to the DMA queue */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300161 list_add_tail(&buf->list, &common->dma_queue);
Hans Verkuilaec96832012-11-16 12:03:06 -0300162 spin_unlock_irqrestore(&common->irqlock, flags);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300163}
164
Lad, Prabhakar41b9f242014-05-16 10:33:39 -0300165/**
166 * vpif_start_streaming : Starts the DMA engine for streaming
167 * @vb: ptr to vb2_buffer
168 * @count: number of buffers
169 */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300170static int vpif_start_streaming(struct vb2_queue *vq, unsigned int count)
171{
172 struct vpif_capture_config *vpif_config_data =
173 vpif_dev->platform_data;
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -0300174 struct channel_obj *ch = vb2_get_drv_priv(vq);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300175 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
176 struct vpif_params *vpif = &ch->vpifparams;
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300177 struct vpif_cap_buffer *buf, *tmp;
178 unsigned long addr, flags;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300179 int ret;
180
Hans Verkuilaec96832012-11-16 12:03:06 -0300181 spin_lock_irqsave(&common->irqlock, flags);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300182
Lad, Prabhakarc66238f2014-05-16 10:33:45 -0300183 /* Initialize field_id */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300184 ch->field_id = 0;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300185
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300186 /* configure 1 or 2 channel mode */
Lad, Prabhakarf4ad8d72012-09-27 02:33:12 -0300187 if (vpif_config_data->setup_input_channel_mode) {
188 ret = vpif_config_data->
189 setup_input_channel_mode(vpif->std_info.ycmux_mode);
190 if (ret < 0) {
191 vpif_dbg(1, debug, "can't set vpif channel mode\n");
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300192 goto err;
Lad, Prabhakarf4ad8d72012-09-27 02:33:12 -0300193 }
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300194 }
195
Lad, Prabhakard9a3b132014-05-16 10:33:42 -0300196 ret = v4l2_subdev_call(ch->sd, video, s_stream, 1);
197 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
198 vpif_dbg(1, debug, "stream on failed in subdev\n");
199 goto err;
200 }
201
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300202 /* Call vpif_set_params function to set the parameters and addresses */
203 ret = vpif_set_video_params(vpif, ch->channel_id);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300204 if (ret < 0) {
205 vpif_dbg(1, debug, "can't set video params\n");
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300206 goto err;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300207 }
208
Lad, Prabhakarc66238f2014-05-16 10:33:45 -0300209 ycmux_mode = ret;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300210 vpif_config_addr(ch, ret);
211
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300212 /* Get the next frame from the buffer queue */
213 common->cur_frm = common->next_frm = list_entry(common->dma_queue.next,
214 struct vpif_cap_buffer, list);
215 /* Remove buffer from the buffer queue */
216 list_del(&common->cur_frm->list);
217 spin_unlock_irqrestore(&common->irqlock, flags);
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300218
Junghak Sung2d700712015-09-22 10:30:30 -0300219 addr = vb2_dma_contig_plane_dma_addr(&common->cur_frm->vb.vb2_buf, 0);
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300220
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);
Junghak Sung2d700712015-09-22 10:30:30 -0300248 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300249 }
Dan Carpenter92491962014-06-12 04:01:45 -0300250 spin_unlock_irqrestore(&common->irqlock, flags);
Lad, Prabhakarbebd8d12014-05-16 10:33:35 -0300251
252 return ret;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300253}
254
Lad, Prabhakar41b9f242014-05-16 10:33:39 -0300255/**
256 * vpif_stop_streaming : Stop the DMA engine
257 * @vq: ptr to vb2_queue
258 *
259 * This callback stops the DMA engine and any remaining buffers
260 * in the DMA queue are released.
261 */
Hans Verkuile37559b2014-04-17 02:47:21 -0300262static void vpif_stop_streaming(struct vb2_queue *vq)
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300263{
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -0300264 struct channel_obj *ch = vb2_get_drv_priv(vq);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300265 struct common_obj *common;
Hans Verkuilaec96832012-11-16 12:03:06 -0300266 unsigned long flags;
Lad, Prabhakard9a3b132014-05-16 10:33:42 -0300267 int ret;
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300268
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300269 common = &ch->common[VPIF_VIDEO_INDEX];
270
Lad, Prabhakare6ba3db2014-03-22 08:03:07 -0300271 /* Disable channel as per its device type and channel id */
272 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
273 enable_channel0(0);
274 channel0_intr_enable(0);
275 }
Lad, Prabhakar41b9f242014-05-16 10:33:39 -0300276 if (VPIF_CHANNEL1_VIDEO == ch->channel_id ||
Lad, Prabhakarc66238f2014-05-16 10:33:45 -0300277 ycmux_mode == 2) {
Lad, Prabhakare6ba3db2014-03-22 08:03:07 -0300278 enable_channel1(0);
279 channel1_intr_enable(0);
280 }
Lad, Prabhakarc66238f2014-05-16 10:33:45 -0300281
282 ycmux_mode = 0;
Lad, Prabhakare6ba3db2014-03-22 08:03:07 -0300283
Lad, Prabhakard9a3b132014-05-16 10:33:42 -0300284 ret = v4l2_subdev_call(ch->sd, video, s_stream, 0);
285 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
286 vpif_dbg(1, debug, "stream off failed in subdev\n");
287
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300288 /* release all active buffers */
Hans Verkuilaec96832012-11-16 12:03:06 -0300289 spin_lock_irqsave(&common->irqlock, flags);
Lad, Prabhakare6ba3db2014-03-22 08:03:07 -0300290 if (common->cur_frm == common->next_frm) {
Junghak Sung2d700712015-09-22 10:30:30 -0300291 vb2_buffer_done(&common->cur_frm->vb.vb2_buf,
292 VB2_BUF_STATE_ERROR);
Lad, Prabhakare6ba3db2014-03-22 08:03:07 -0300293 } else {
294 if (common->cur_frm != NULL)
Junghak Sung2d700712015-09-22 10:30:30 -0300295 vb2_buffer_done(&common->cur_frm->vb.vb2_buf,
Lad, Prabhakare6ba3db2014-03-22 08:03:07 -0300296 VB2_BUF_STATE_ERROR);
297 if (common->next_frm != NULL)
Junghak Sung2d700712015-09-22 10:30:30 -0300298 vb2_buffer_done(&common->next_frm->vb.vb2_buf,
Lad, Prabhakare6ba3db2014-03-22 08:03:07 -0300299 VB2_BUF_STATE_ERROR);
300 }
301
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300302 while (!list_empty(&common->dma_queue)) {
303 common->next_frm = list_entry(common->dma_queue.next,
304 struct vpif_cap_buffer, list);
305 list_del(&common->next_frm->list);
Junghak Sung2d700712015-09-22 10:30:30 -0300306 vb2_buffer_done(&common->next_frm->vb.vb2_buf,
307 VB2_BUF_STATE_ERROR);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300308 }
Hans Verkuilaec96832012-11-16 12:03:06 -0300309 spin_unlock_irqrestore(&common->irqlock, flags);
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300310}
311
312static struct vb2_ops video_qops = {
313 .queue_setup = vpif_buffer_queue_setup,
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300314 .buf_prepare = vpif_buffer_prepare,
315 .start_streaming = vpif_start_streaming,
316 .stop_streaming = vpif_stop_streaming,
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300317 .buf_queue = vpif_buffer_queue,
Prabhakar Ladb41a97a2014-11-26 19:42:33 -0300318 .wait_prepare = vb2_ops_wait_prepare,
319 .wait_finish = vb2_ops_wait_finish,
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300320};
321
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300322/**
323 * vpif_process_buffer_complete: process a completed buffer
324 * @common: ptr to common channel object
325 *
326 * This function time stamp the buffer and mark it as DONE. It also
327 * wake up any process waiting on the QUEUE and set the next buffer
328 * as current
329 */
330static void vpif_process_buffer_complete(struct common_obj *common)
331{
Junghak Sungd6dd6452015-11-03 08:16:37 -0200332 common->cur_frm->vb.vb2_buf.timestamp = ktime_get_ns();
Junghak Sung2d700712015-09-22 10:30:30 -0300333 vb2_buffer_done(&common->cur_frm->vb.vb2_buf, VB2_BUF_STATE_DONE);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300334 /* Make curFrm pointing to nextFrm */
335 common->cur_frm = common->next_frm;
336}
337
338/**
339 * vpif_schedule_next_buffer: set next buffer address for capture
340 * @common : ptr to common channel object
341 *
342 * This function will get next buffer from the dma queue and
343 * set the buffer address in the vpif register for capture.
344 * the buffer is marked active
345 */
346static void vpif_schedule_next_buffer(struct common_obj *common)
347{
348 unsigned long addr = 0;
349
Hans Verkuilaec96832012-11-16 12:03:06 -0300350 spin_lock(&common->irqlock);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300351 common->next_frm = list_entry(common->dma_queue.next,
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300352 struct vpif_cap_buffer, list);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300353 /* Remove that buffer from the buffer queue */
Lad, Prabhakar60aa38d2012-06-28 09:28:05 -0300354 list_del(&common->next_frm->list);
Hans Verkuilaec96832012-11-16 12:03:06 -0300355 spin_unlock(&common->irqlock);
Junghak Sung2d700712015-09-22 10:30:30 -0300356 addr = vb2_dma_contig_plane_dma_addr(&common->next_frm->vb.vb2_buf, 0);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300357
358 /* Set top and bottom field addresses in VPIF registers */
359 common->set_addr(addr + common->ytop_off,
360 addr + common->ybtm_off,
361 addr + common->ctop_off,
362 addr + common->cbtm_off);
363}
364
365/**
366 * vpif_channel_isr : ISR handler for vpif capture
367 * @irq: irq number
368 * @dev_id: dev_id ptr
369 *
370 * It changes status of the captured buffer, takes next buffer from the queue
Mats Randgaard2c0ddd12010-12-16 12:17:45 -0300371 * and sets its address in VPIF registers
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300372 */
373static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
374{
375 struct vpif_device *dev = &vpif_obj;
376 struct common_obj *common;
377 struct channel_obj *ch;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300378 int channel_id = 0;
379 int fid = -1, i;
380
381 channel_id = *(int *)(dev_id);
Manjunath Hadlib1fc4232012-04-13 04:43:10 -0300382 if (!vpif_intr_status(channel_id))
383 return IRQ_NONE;
384
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300385 ch = dev->dev[channel_id];
386
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300387 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{
Mauro Carvalho Chehab24ab6332014-08-21 15:49:06 -0500535 unsigned int hpitch, sizeimage;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300536 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;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300554
555 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
556 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
557 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
558 common->ytop_off = 0;
559 common->ybtm_off = hpitch;
560 common->ctop_off = sizeimage / 2;
561 common->cbtm_off = sizeimage / 2 + hpitch;
562 } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
563 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
564 common->ytop_off = 0;
565 common->ybtm_off = sizeimage / 4;
566 common->ctop_off = sizeimage / 2;
567 common->cbtm_off = common->ctop_off + sizeimage / 4;
568 } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
569 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
570 common->ybtm_off = 0;
571 common->ytop_off = sizeimage / 4;
572 common->cbtm_off = sizeimage / 2;
573 common->ctop_off = common->cbtm_off + sizeimage / 4;
574 }
575 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
576 (V4L2_FIELD_INTERLACED == vid_ch->buf_field))
577 vpifparams->video_params.storage_mode = 1;
578 else
579 vpifparams->video_params.storage_mode = 0;
580
581 if (1 == vpifparams->std_info.frm_fmt)
582 vpifparams->video_params.hpitch =
583 common->fmt.fmt.pix.bytesperline;
584 else {
585 if ((field == V4L2_FIELD_ANY)
586 || (field == V4L2_FIELD_INTERLACED))
587 vpifparams->video_params.hpitch =
588 common->fmt.fmt.pix.bytesperline * 2;
589 else
590 vpifparams->video_params.hpitch =
591 common->fmt.fmt.pix.bytesperline;
592 }
593
594 ch->vpifparams.video_params.stdid = vpifparams->std_info.stdid;
595}
596
597/**
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300598 * vpif_get_default_field() - Get default field type based on interface
599 * @vpif_params - ptr to vpif params
600 */
601static inline enum v4l2_field vpif_get_default_field(
602 struct vpif_interface *iface)
603{
604 return (iface->if_type == VPIF_IF_RAW_BAYER) ? V4L2_FIELD_NONE :
605 V4L2_FIELD_INTERLACED;
606}
607
608/**
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300609 * vpif_config_addr() - function to configure buffer address in vpif
610 * @ch - channel ptr
611 * @muxmode - channel mux mode
612 */
613static void vpif_config_addr(struct channel_obj *ch, int muxmode)
614{
615 struct common_obj *common;
616
617 vpif_dbg(2, debug, "vpif_config_addr\n");
618
619 common = &(ch->common[VPIF_VIDEO_INDEX]);
620
621 if (VPIF_CHANNEL1_VIDEO == ch->channel_id)
622 common->set_addr = ch1_set_videobuf_addr;
623 else if (2 == muxmode)
624 common->set_addr = ch0_set_videobuf_addr_yc_nmux;
625 else
626 common->set_addr = ch0_set_videobuf_addr;
627}
628
629/**
Hans Verkuil178cce12012-09-20 09:06:30 -0300630 * vpif_input_to_subdev() - Maps input to sub device
631 * @vpif_cfg - global config ptr
632 * @chan_cfg - channel config ptr
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300633 * @input_index - Given input index from application
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300634 *
635 * lookup the sub device information for a given input index.
636 * we report all the inputs to application. inputs table also
637 * has sub device name for the each input
638 */
Hans Verkuil178cce12012-09-20 09:06:30 -0300639static int vpif_input_to_subdev(
640 struct vpif_capture_config *vpif_cfg,
641 struct vpif_capture_chan_config *chan_cfg,
642 int input_index)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300643{
Hans Verkuil178cce12012-09-20 09:06:30 -0300644 struct vpif_subdev_info *subdev_info;
645 const char *subdev_name;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300646 int i;
647
Hans Verkuil178cce12012-09-20 09:06:30 -0300648 vpif_dbg(2, debug, "vpif_input_to_subdev\n");
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300649
Hans Verkuil178cce12012-09-20 09:06:30 -0300650 subdev_name = chan_cfg->inputs[input_index].subdev_name;
651 if (subdev_name == NULL)
652 return -1;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300653
654 /* loop through the sub device list to get the sub device info */
655 for (i = 0; i < vpif_cfg->subdev_count; i++) {
656 subdev_info = &vpif_cfg->subdev_info[i];
657 if (!strcmp(subdev_info->name, subdev_name))
Hans Verkuil178cce12012-09-20 09:06:30 -0300658 return i;
659 }
660 return -1;
661}
662
663/**
664 * vpif_set_input() - Select an input
665 * @vpif_cfg - global config ptr
666 * @ch - channel
667 * @_index - Given input index from application
668 *
669 * Select the given input.
670 */
671static int vpif_set_input(
672 struct vpif_capture_config *vpif_cfg,
673 struct channel_obj *ch,
674 int index)
675{
676 struct vpif_capture_chan_config *chan_cfg =
677 &vpif_cfg->chan_config[ch->channel_id];
678 struct vpif_subdev_info *subdev_info = NULL;
679 struct v4l2_subdev *sd = NULL;
680 u32 input = 0, output = 0;
681 int sd_index;
682 int ret;
683
684 sd_index = vpif_input_to_subdev(vpif_cfg, chan_cfg, index);
685 if (sd_index >= 0) {
686 sd = vpif_obj.sd[sd_index];
687 subdev_info = &vpif_cfg->subdev_info[sd_index];
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300688 }
689
Hans Verkuil178cce12012-09-20 09:06:30 -0300690 /* first setup input path from sub device to vpif */
691 if (sd && vpif_cfg->setup_input_path) {
692 ret = vpif_cfg->setup_input_path(ch->channel_id,
693 subdev_info->name);
694 if (ret < 0) {
695 vpif_dbg(1, debug, "couldn't setup input path for the" \
696 " sub device %s, for input index %d\n",
697 subdev_info->name, index);
698 return ret;
699 }
700 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300701
Hans Verkuil178cce12012-09-20 09:06:30 -0300702 if (sd) {
703 input = chan_cfg->inputs[index].input_route;
704 output = chan_cfg->inputs[index].output_route;
705 ret = v4l2_subdev_call(sd, video, s_routing,
706 input, output, 0);
707 if (ret < 0 && ret != -ENOIOCTLCMD) {
708 vpif_dbg(1, debug, "Failed to set input\n");
709 return ret;
710 }
711 }
712 ch->input_idx = index;
713 ch->sd = sd;
714 /* copy interface parameters to vpif */
Hans Verkuil0d4f35f2012-09-20 09:06:32 -0300715 ch->vpifparams.iface = chan_cfg->vpif_if;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300716
Hans Verkuil178cce12012-09-20 09:06:30 -0300717 /* update tvnorms from the sub device input info */
Lad, Prabhakar8eef6662015-03-08 18:57:23 -0300718 ch->video_dev.tvnorms = chan_cfg->inputs[index].input.std;
Hans Verkuil178cce12012-09-20 09:06:30 -0300719 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300720}
721
722/**
723 * vpif_querystd() - querystd handler
724 * @file: file ptr
725 * @priv: file handle
726 * @std_id: ptr to std id
727 *
728 * This function is called to detect standard at the selected input
729 */
730static int vpif_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
731{
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300732 struct video_device *vdev = video_devdata(file);
733 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300734 int ret = 0;
735
736 vpif_dbg(2, debug, "vpif_querystd\n");
737
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300738 /* Call querystd function of decoder device */
Hans Verkuil178cce12012-09-20 09:06:30 -0300739 ret = v4l2_subdev_call(ch->sd, video, querystd, std_id);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300740
Hans Verkuil178cce12012-09-20 09:06:30 -0300741 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
742 return -ENODATA;
743 if (ret) {
744 vpif_dbg(1, debug, "Failed to query standard for sub devices\n");
745 return ret;
746 }
747
748 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300749}
750
751/**
752 * vpif_g_std() - get STD handler
753 * @file: file ptr
754 * @priv: file handle
755 * @std_id: ptr to std id
756 */
757static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
758{
Lad, Prabhakard557b7d2014-05-16 10:33:52 -0300759 struct vpif_capture_config *config = vpif_dev->platform_data;
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300760 struct video_device *vdev = video_devdata(file);
761 struct channel_obj *ch = video_get_drvdata(vdev);
Lad, Prabhakard557b7d2014-05-16 10:33:52 -0300762 struct vpif_capture_chan_config *chan_cfg;
763 struct v4l2_input input;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300764
765 vpif_dbg(2, debug, "vpif_g_std\n");
766
Lad, Prabhakard557b7d2014-05-16 10:33:52 -0300767 if (config->chan_config[ch->channel_id].inputs == NULL)
768 return -ENODATA;
769
770 chan_cfg = &config->chan_config[ch->channel_id];
771 input = chan_cfg->inputs[ch->input_idx].input;
772 if (input.capabilities != V4L2_IN_CAP_STD)
773 return -ENODATA;
774
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300775 *std = ch->video.stdid;
776 return 0;
777}
778
779/**
780 * vpif_s_std() - set STD handler
781 * @file: file ptr
782 * @priv: file handle
783 * @std_id: ptr to std id
784 */
Hans Verkuil314527a2013-03-15 06:10:40 -0300785static int vpif_s_std(struct file *file, void *priv, v4l2_std_id std_id)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300786{
Lad, Prabhakard557b7d2014-05-16 10:33:52 -0300787 struct vpif_capture_config *config = vpif_dev->platform_data;
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300788 struct video_device *vdev = video_devdata(file);
789 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300790 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakard557b7d2014-05-16 10:33:52 -0300791 struct vpif_capture_chan_config *chan_cfg;
792 struct v4l2_input input;
793 int ret;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300794
795 vpif_dbg(2, debug, "vpif_s_std\n");
796
Lad, Prabhakard557b7d2014-05-16 10:33:52 -0300797 if (config->chan_config[ch->channel_id].inputs == NULL)
798 return -ENODATA;
799
800 chan_cfg = &config->chan_config[ch->channel_id];
801 input = chan_cfg->inputs[ch->input_idx].input;
802 if (input.capabilities != V4L2_IN_CAP_STD)
803 return -ENODATA;
804
Lad, Prabhakarc66238f2014-05-16 10:33:45 -0300805 if (vb2_is_busy(&common->buffer_queue))
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300806 return -EBUSY;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300807
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300808 /* Call encoder subdevice function to set the standard */
Hans Verkuil314527a2013-03-15 06:10:40 -0300809 ch->video.stdid = std_id;
Hans Verkuil0598c172012-09-18 07:18:47 -0300810 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300811
812 /* Get the information about the standard */
813 if (vpif_update_std_info(ch)) {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300814 vpif_err("Error getting the standard info\n");
Hans Verkuil46656af2011-01-04 06:51:35 -0300815 return -EINVAL;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300816 }
817
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300818 /* set standard in the sub device */
Laurent Pinchart8774bed2014-04-28 16:53:01 -0300819 ret = v4l2_subdev_call(ch->sd, video, s_std, std_id);
Hans Verkuil178cce12012-09-20 09:06:30 -0300820 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV) {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300821 vpif_dbg(1, debug, "Failed to set standard for sub devices\n");
Hans Verkuil178cce12012-09-20 09:06:30 -0300822 return ret;
823 }
824 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300825}
826
827/**
828 * vpif_enum_input() - ENUMINPUT handler
829 * @file: file ptr
830 * @priv: file handle
831 * @input: ptr to input structure
832 */
833static int vpif_enum_input(struct file *file, void *priv,
834 struct v4l2_input *input)
835{
836
837 struct vpif_capture_config *config = vpif_dev->platform_data;
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300838 struct video_device *vdev = video_devdata(file);
839 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300840 struct vpif_capture_chan_config *chan_cfg;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300841
842 chan_cfg = &config->chan_config[ch->channel_id];
843
Lad, Prabhakara4965c52014-05-16 10:33:54 -0300844 if (input->index >= chan_cfg->input_count)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300845 return -EINVAL;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300846
847 memcpy(input, &chan_cfg->inputs[input->index].input,
848 sizeof(*input));
849 return 0;
850}
851
852/**
853 * vpif_g_input() - Get INPUT handler
854 * @file: file ptr
855 * @priv: file handle
856 * @index: ptr to input index
857 */
858static int vpif_g_input(struct file *file, void *priv, unsigned int *index)
859{
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300860 struct video_device *vdev = video_devdata(file);
861 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300862
Hans Verkuil6f47c6c2012-09-20 09:06:22 -0300863 *index = ch->input_idx;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300864 return 0;
865}
866
867/**
868 * vpif_s_input() - Set INPUT handler
869 * @file: file ptr
870 * @priv: file handle
871 * @index: input index
872 */
873static int vpif_s_input(struct file *file, void *priv, unsigned int index)
874{
875 struct vpif_capture_config *config = vpif_dev->platform_data;
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300876 struct video_device *vdev = video_devdata(file);
877 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300878 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300879 struct vpif_capture_chan_config *chan_cfg;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300880
881 chan_cfg = &config->chan_config[ch->channel_id];
882
Hans Verkuil7aaad132012-09-20 09:06:25 -0300883 if (index >= chan_cfg->input_count)
884 return -EINVAL;
885
Lad, Prabhakarc66238f2014-05-16 10:33:45 -0300886 if (vb2_is_busy(&common->buffer_queue))
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300887 return -EBUSY;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300888
Hans Verkuil178cce12012-09-20 09:06:30 -0300889 return vpif_set_input(config, ch, index);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300890}
891
892/**
893 * vpif_enum_fmt_vid_cap() - ENUM_FMT handler
894 * @file: file ptr
895 * @priv: file handle
896 * @index: input index
897 */
898static int vpif_enum_fmt_vid_cap(struct file *file, void *priv,
899 struct v4l2_fmtdesc *fmt)
900{
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300901 struct video_device *vdev = video_devdata(file);
902 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300903
904 if (fmt->index != 0) {
905 vpif_dbg(1, debug, "Invalid format index\n");
906 return -EINVAL;
907 }
908
909 /* Fill in the information about format */
910 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER) {
911 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
912 strcpy(fmt->description, "Raw Mode -Bayer Pattern GrRBGb");
913 fmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
914 } else {
915 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
916 strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
917 fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
918 }
919 return 0;
920}
921
922/**
923 * vpif_try_fmt_vid_cap() - TRY_FMT handler
924 * @file: file ptr
925 * @priv: file handle
926 * @fmt: ptr to v4l2 format structure
927 */
928static int vpif_try_fmt_vid_cap(struct file *file, void *priv,
929 struct v4l2_format *fmt)
930{
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300931 struct video_device *vdev = video_devdata(file);
932 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300933 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
Lad, Prabhakara4965c52014-05-16 10:33:54 -0300934 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
935 struct vpif_params *vpif_params = &ch->vpifparams;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300936
Lad, Prabhakara4965c52014-05-16 10:33:54 -0300937 /*
938 * to supress v4l-compliance warnings silently correct
939 * the pixelformat
940 */
941 if (vpif_params->iface.if_type == VPIF_IF_RAW_BAYER) {
942 if (pixfmt->pixelformat != V4L2_PIX_FMT_SBGGR8)
943 pixfmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
944 } else {
945 if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P)
946 pixfmt->pixelformat = V4L2_PIX_FMT_YUV422P;
947 }
948
949 common->fmt.fmt.pix.pixelformat = pixfmt->pixelformat;
950
951 vpif_update_std_info(ch);
952
953 pixfmt->field = common->fmt.fmt.pix.field;
954 pixfmt->colorspace = common->fmt.fmt.pix.colorspace;
955 pixfmt->bytesperline = common->fmt.fmt.pix.width;
956 pixfmt->width = common->fmt.fmt.pix.width;
957 pixfmt->height = common->fmt.fmt.pix.height;
958 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height * 2;
959 pixfmt->priv = 0;
960
961 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300962}
963
964
965/**
966 * vpif_g_fmt_vid_cap() - Set INPUT handler
967 * @file: file ptr
968 * @priv: file handle
969 * @fmt: ptr to v4l2 format structure
970 */
971static int vpif_g_fmt_vid_cap(struct file *file, void *priv,
972 struct v4l2_format *fmt)
973{
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300974 struct video_device *vdev = video_devdata(file);
975 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300976 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
977
978 /* Check the validity of the buffer type */
979 if (common->fmt.type != fmt->type)
980 return -EINVAL;
981
982 /* Fill in the information about format */
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300983 *fmt = common->fmt;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300984 return 0;
985}
986
987/**
988 * vpif_s_fmt_vid_cap() - Set FMT handler
989 * @file: file ptr
990 * @priv: file handle
991 * @fmt: ptr to v4l2 format structure
992 */
993static int vpif_s_fmt_vid_cap(struct file *file, void *priv,
994 struct v4l2_format *fmt)
995{
Lad, Prabhakar7ff51192014-05-16 10:33:41 -0300996 struct video_device *vdev = video_devdata(file);
997 struct channel_obj *ch = video_get_drvdata(vdev);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -0300998 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakara4965c52014-05-16 10:33:54 -0300999 int ret;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001000
Mats Randgaard2c0ddd12010-12-16 12:17:45 -03001001 vpif_dbg(2, debug, "%s\n", __func__);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001002
Lad, Prabhakarc66238f2014-05-16 10:33:45 -03001003 if (vb2_is_busy(&common->buffer_queue))
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001004 return -EBUSY;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001005
Lad, Prabhakara4965c52014-05-16 10:33:54 -03001006 ret = vpif_try_fmt_vid_cap(file, priv, fmt);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001007 if (ret)
1008 return ret;
Lad, Prabhakara4965c52014-05-16 10:33:54 -03001009
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001010 /* store the format in the channel object */
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001011 common->fmt = *fmt;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001012 return 0;
1013}
1014
1015/**
1016 * vpif_querycap() - QUERYCAP handler
1017 * @file: file ptr
1018 * @priv: file handle
1019 * @cap: ptr to v4l2_capability structure
1020 */
1021static int vpif_querycap(struct file *file, void *priv,
1022 struct v4l2_capability *cap)
1023{
1024 struct vpif_capture_config *config = vpif_dev->platform_data;
1025
Lad, Prabhakar626d533f2012-09-25 11:21:55 -03001026 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1027 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
Lad, Prabhakare75ea0c2014-05-16 10:33:46 -03001028 strlcpy(cap->driver, VPIF_DRIVER_NAME, sizeof(cap->driver));
Lad, Prabhakar626d533f2012-09-25 11:21:55 -03001029 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
1030 dev_name(vpif_dev));
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001031 strlcpy(cap->card, config->card_name, sizeof(cap->card));
1032
1033 return 0;
1034}
1035
1036/**
Hans Verkuil0598c172012-09-18 07:18:47 -03001037 * vpif_enum_dv_timings() - ENUM_DV_TIMINGS handler
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001038 * @file: file ptr
1039 * @priv: file handle
Hans Verkuil0598c172012-09-18 07:18:47 -03001040 * @timings: input timings
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001041 */
Hans Verkuil0598c172012-09-18 07:18:47 -03001042static int
1043vpif_enum_dv_timings(struct file *file, void *priv,
1044 struct v4l2_enum_dv_timings *timings)
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001045{
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001046 struct vpif_capture_config *config = vpif_dev->platform_data;
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001047 struct video_device *vdev = video_devdata(file);
1048 struct channel_obj *ch = video_get_drvdata(vdev);
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001049 struct vpif_capture_chan_config *chan_cfg;
1050 struct v4l2_input input;
Hans Verkuil178cce12012-09-20 09:06:30 -03001051 int ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001052
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001053 if (config->chan_config[ch->channel_id].inputs == NULL)
1054 return -ENODATA;
1055
1056 chan_cfg = &config->chan_config[ch->channel_id];
1057 input = chan_cfg->inputs[ch->input_idx].input;
1058 if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
1059 return -ENODATA;
1060
Laurent Pinchart10d21462014-01-31 09:04:19 -03001061 timings->pad = 0;
1062
1063 ret = v4l2_subdev_call(ch->sd, pad, enum_dv_timings, timings);
Wei Yongjune070f1b2012-10-30 09:45:00 -03001064 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
Hans Verkuil178cce12012-09-20 09:06:30 -03001065 return -EINVAL;
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001066
Hans Verkuil178cce12012-09-20 09:06:30 -03001067 return ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001068}
1069
1070/**
Hans Verkuil0598c172012-09-18 07:18:47 -03001071 * vpif_query_dv_timings() - QUERY_DV_TIMINGS handler
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001072 * @file: file ptr
1073 * @priv: file handle
Hans Verkuil0598c172012-09-18 07:18:47 -03001074 * @timings: input timings
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001075 */
Hans Verkuil0598c172012-09-18 07:18:47 -03001076static int
1077vpif_query_dv_timings(struct file *file, void *priv,
1078 struct v4l2_dv_timings *timings)
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001079{
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001080 struct vpif_capture_config *config = vpif_dev->platform_data;
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001081 struct video_device *vdev = video_devdata(file);
1082 struct channel_obj *ch = video_get_drvdata(vdev);
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001083 struct vpif_capture_chan_config *chan_cfg;
1084 struct v4l2_input input;
Hans Verkuil178cce12012-09-20 09:06:30 -03001085 int ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001086
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001087 if (config->chan_config[ch->channel_id].inputs == NULL)
1088 return -ENODATA;
1089
1090 chan_cfg = &config->chan_config[ch->channel_id];
1091 input = chan_cfg->inputs[ch->input_idx].input;
1092 if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
1093 return -ENODATA;
1094
Hans Verkuil178cce12012-09-20 09:06:30 -03001095 ret = v4l2_subdev_call(ch->sd, video, query_dv_timings, timings);
Wei Yongjune070f1b2012-10-30 09:45:00 -03001096 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
Hans Verkuil178cce12012-09-20 09:06:30 -03001097 return -ENODATA;
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001098
Hans Verkuil178cce12012-09-20 09:06:30 -03001099 return ret;
Mats Randgaard40c8bce2010-12-16 12:17:43 -03001100}
1101
Mats Randgaardc027e162010-12-16 12:17:44 -03001102/**
1103 * vpif_s_dv_timings() - S_DV_TIMINGS handler
1104 * @file: file ptr
1105 * @priv: file handle
1106 * @timings: digital video timings
1107 */
1108static int vpif_s_dv_timings(struct file *file, void *priv,
1109 struct v4l2_dv_timings *timings)
1110{
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001111 struct vpif_capture_config *config = vpif_dev->platform_data;
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001112 struct video_device *vdev = video_devdata(file);
1113 struct channel_obj *ch = video_get_drvdata(vdev);
Mats Randgaardc027e162010-12-16 12:17:44 -03001114 struct vpif_params *vpifparams = &ch->vpifparams;
1115 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001116 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
Mats Randgaardc027e162010-12-16 12:17:44 -03001117 struct video_obj *vid_ch = &ch->video;
Hans Verkuil0598c172012-09-18 07:18:47 -03001118 struct v4l2_bt_timings *bt = &vid_ch->dv_timings.bt;
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001119 struct vpif_capture_chan_config *chan_cfg;
1120 struct v4l2_input input;
Mats Randgaardc027e162010-12-16 12:17:44 -03001121 int ret;
1122
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001123 if (config->chan_config[ch->channel_id].inputs == NULL)
1124 return -ENODATA;
1125
1126 chan_cfg = &config->chan_config[ch->channel_id];
1127 input = chan_cfg->inputs[ch->input_idx].input;
1128 if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
1129 return -ENODATA;
1130
Mats Randgaardc027e162010-12-16 12:17:44 -03001131 if (timings->type != V4L2_DV_BT_656_1120) {
1132 vpif_dbg(2, debug, "Timing type not defined\n");
1133 return -EINVAL;
1134 }
1135
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001136 if (vb2_is_busy(&common->buffer_queue))
1137 return -EBUSY;
1138
Mats Randgaardc027e162010-12-16 12:17:44 -03001139 /* Configure subdevice timings, if any */
Hans Verkuil178cce12012-09-20 09:06:30 -03001140 ret = v4l2_subdev_call(ch->sd, video, s_dv_timings, timings);
1141 if (ret == -ENOIOCTLCMD || ret == -ENODEV)
1142 ret = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001143 if (ret < 0) {
1144 vpif_dbg(2, debug, "Error setting custom DV timings\n");
1145 return ret;
1146 }
1147
1148 if (!(timings->bt.width && timings->bt.height &&
1149 (timings->bt.hbackporch ||
1150 timings->bt.hfrontporch ||
1151 timings->bt.hsync) &&
1152 timings->bt.vfrontporch &&
1153 (timings->bt.vbackporch ||
1154 timings->bt.vsync))) {
1155 vpif_dbg(2, debug, "Timings for width, height, "
1156 "horizontal back porch, horizontal sync, "
1157 "horizontal front porch, vertical back porch, "
1158 "vertical sync and vertical back porch "
1159 "must be defined\n");
1160 return -EINVAL;
1161 }
1162
Hans Verkuil0598c172012-09-18 07:18:47 -03001163 vid_ch->dv_timings = *timings;
Mats Randgaardc027e162010-12-16 12:17:44 -03001164
1165 /* Configure video port timings */
1166
Hans Verkuile3655262013-07-29 08:41:00 -03001167 std_info->eav2sav = V4L2_DV_BT_BLANKING_WIDTH(bt) - 8;
Mats Randgaardc027e162010-12-16 12:17:44 -03001168 std_info->sav2eav = bt->width;
1169
1170 std_info->l1 = 1;
1171 std_info->l3 = bt->vsync + bt->vbackporch + 1;
1172
Hans Verkuile3655262013-07-29 08:41:00 -03001173 std_info->vsize = V4L2_DV_BT_FRAME_HEIGHT(bt);
Mats Randgaardc027e162010-12-16 12:17:44 -03001174 if (bt->interlaced) {
1175 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
Mats Randgaardc027e162010-12-16 12:17:44 -03001176 std_info->l5 = std_info->vsize/2 -
1177 (bt->vfrontporch - 1);
1178 std_info->l7 = std_info->vsize/2 + 1;
1179 std_info->l9 = std_info->l7 + bt->il_vsync +
1180 bt->il_vbackporch + 1;
1181 std_info->l11 = std_info->vsize -
1182 (bt->il_vfrontporch - 1);
1183 } else {
1184 vpif_dbg(2, debug, "Required timing values for "
1185 "interlaced BT format missing\n");
1186 return -EINVAL;
1187 }
1188 } else {
Mats Randgaardc027e162010-12-16 12:17:44 -03001189 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
1190 }
1191 strncpy(std_info->name, "Custom timings BT656/1120", VPIF_MAX_NAME);
1192 std_info->width = bt->width;
1193 std_info->height = bt->height;
1194 std_info->frm_fmt = bt->interlaced ? 0 : 1;
1195 std_info->ycmux_mode = 0;
1196 std_info->capture_format = 0;
1197 std_info->vbi_supported = 0;
1198 std_info->hd_sd = 1;
1199 std_info->stdid = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001200
1201 vid_ch->stdid = 0;
Mats Randgaardc027e162010-12-16 12:17:44 -03001202 return 0;
1203}
1204
1205/**
1206 * vpif_g_dv_timings() - G_DV_TIMINGS handler
1207 * @file: file ptr
1208 * @priv: file handle
1209 * @timings: digital video timings
1210 */
1211static int vpif_g_dv_timings(struct file *file, void *priv,
1212 struct v4l2_dv_timings *timings)
1213{
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001214 struct vpif_capture_config *config = vpif_dev->platform_data;
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001215 struct video_device *vdev = video_devdata(file);
1216 struct channel_obj *ch = video_get_drvdata(vdev);
Mats Randgaardc027e162010-12-16 12:17:44 -03001217 struct video_obj *vid_ch = &ch->video;
Lad, Prabhakar1e8852af2014-05-16 10:33:51 -03001218 struct vpif_capture_chan_config *chan_cfg;
1219 struct v4l2_input input;
1220
1221 if (config->chan_config[ch->channel_id].inputs == NULL)
1222 return -ENODATA;
1223
1224 chan_cfg = &config->chan_config[ch->channel_id];
1225 input = chan_cfg->inputs[ch->input_idx].input;
1226 if (input.capabilities != V4L2_IN_CAP_DV_TIMINGS)
1227 return -ENODATA;
Mats Randgaardc027e162010-12-16 12:17:44 -03001228
Hans Verkuil0598c172012-09-18 07:18:47 -03001229 *timings = vid_ch->dv_timings;
Mats Randgaardc027e162010-12-16 12:17:44 -03001230
1231 return 0;
1232}
1233
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001234/*
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001235 * vpif_log_status() - Status information
1236 * @file: file ptr
1237 * @priv: file handle
1238 *
1239 * Returns zero.
1240 */
1241static int vpif_log_status(struct file *filep, void *priv)
1242{
1243 /* status for sub devices */
1244 v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
1245
1246 return 0;
1247}
1248
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001249/* vpif capture ioctl operations */
1250static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
Lad, Prabhakar7b4657f2014-05-16 10:33:49 -03001251 .vidioc_querycap = vpif_querycap,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001252 .vidioc_enum_fmt_vid_cap = vpif_enum_fmt_vid_cap,
Lad, Prabhakar7b4657f2014-05-16 10:33:49 -03001253 .vidioc_g_fmt_vid_cap = vpif_g_fmt_vid_cap,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001254 .vidioc_s_fmt_vid_cap = vpif_s_fmt_vid_cap,
1255 .vidioc_try_fmt_vid_cap = vpif_try_fmt_vid_cap,
Lad, Prabhakar7b4657f2014-05-16 10:33:49 -03001256
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001257 .vidioc_enum_input = vpif_enum_input,
1258 .vidioc_s_input = vpif_s_input,
1259 .vidioc_g_input = vpif_g_input,
Lad, Prabhakard9a3b132014-05-16 10:33:42 -03001260
1261 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1262 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1263 .vidioc_querybuf = vb2_ioctl_querybuf,
1264 .vidioc_qbuf = vb2_ioctl_qbuf,
1265 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1266 .vidioc_expbuf = vb2_ioctl_expbuf,
1267 .vidioc_streamon = vb2_ioctl_streamon,
1268 .vidioc_streamoff = vb2_ioctl_streamoff,
1269
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001270 .vidioc_querystd = vpif_querystd,
Lad, Prabhakar7b4657f2014-05-16 10:33:49 -03001271 .vidioc_s_std = vpif_s_std,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001272 .vidioc_g_std = vpif_g_std,
Lad, Prabhakard9a3b132014-05-16 10:33:42 -03001273
Lad, Prabhakar7b4657f2014-05-16 10:33:49 -03001274 .vidioc_enum_dv_timings = vpif_enum_dv_timings,
1275 .vidioc_query_dv_timings = vpif_query_dv_timings,
1276 .vidioc_s_dv_timings = vpif_s_dv_timings,
1277 .vidioc_g_dv_timings = vpif_g_dv_timings,
1278
Mats Randgaard7036d6a2010-12-16 12:17:41 -03001279 .vidioc_log_status = vpif_log_status,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001280};
1281
1282/* vpif file operations */
1283static struct v4l2_file_operations vpif_fops = {
1284 .owner = THIS_MODULE,
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001285 .open = v4l2_fh_open,
1286 .release = vb2_fop_release,
Hans Verkuil46656af2011-01-04 06:51:35 -03001287 .unlocked_ioctl = video_ioctl2,
Lad, Prabhakard26d26b2014-05-16 10:33:40 -03001288 .mmap = vb2_fop_mmap,
1289 .poll = vb2_fop_poll
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001290};
1291
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001292/**
1293 * initialize_vpif() - Initialize vpif data structures
1294 *
1295 * Allocate memory for data structures and initialize them
1296 */
1297static int initialize_vpif(void)
1298{
Lad, Prabhakar4015bef2014-05-16 10:33:47 -03001299 int err, i, j;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001300 int free_channel_objects_index;
1301
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001302 /* Allocate memory for six channel objects */
1303 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1304 vpif_obj.dev[i] =
1305 kzalloc(sizeof(*vpif_obj.dev[i]), GFP_KERNEL);
1306 /* If memory allocation fails, return error */
1307 if (!vpif_obj.dev[i]) {
1308 free_channel_objects_index = i;
1309 err = -ENOMEM;
1310 goto vpif_init_free_channel_objects;
1311 }
1312 }
1313 return 0;
1314
1315vpif_init_free_channel_objects:
1316 for (j = 0; j < free_channel_objects_index; j++)
1317 kfree(vpif_obj.dev[j]);
1318 return err;
1319}
1320
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001321static int vpif_async_bound(struct v4l2_async_notifier *notifier,
1322 struct v4l2_subdev *subdev,
1323 struct v4l2_async_subdev *asd)
1324{
1325 int i;
1326
1327 for (i = 0; i < vpif_obj.config->subdev_count; i++)
1328 if (!strcmp(vpif_obj.config->subdev_info[i].name,
1329 subdev->name)) {
1330 vpif_obj.sd[i] = subdev;
1331 return 0;
1332 }
1333
1334 return -EINVAL;
1335}
1336
1337static int vpif_probe_complete(void)
1338{
1339 struct common_obj *common;
Lad, Prabhakard26d26b2014-05-16 10:33:40 -03001340 struct video_device *vdev;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001341 struct channel_obj *ch;
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001342 struct vb2_queue *q;
Lad, Prabhakar8eef6662015-03-08 18:57:23 -03001343 int j, err, k;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001344
1345 for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
1346 ch = vpif_obj.dev[j];
1347 ch->channel_id = j;
1348 common = &(ch->common[VPIF_VIDEO_INDEX]);
1349 spin_lock_init(&common->irqlock);
1350 mutex_init(&common->lock);
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001351
1352 /* select input 0 */
1353 err = vpif_set_input(vpif_obj.config, ch, 0);
1354 if (err)
1355 goto probe_out;
1356
Lad, Prabhakara4965c52014-05-16 10:33:54 -03001357 /* set initial format */
1358 ch->video.stdid = V4L2_STD_525_60;
1359 memset(&ch->video.dv_timings, 0, sizeof(ch->video.dv_timings));
1360 vpif_update_std_info(ch);
1361
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001362 /* Initialize vb2 queue */
1363 q = &common->buffer_queue;
1364 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1365 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1366 q->drv_priv = ch;
1367 q->ops = &video_qops;
1368 q->mem_ops = &vb2_dma_contig_memops;
1369 q->buf_struct_size = sizeof(struct vpif_cap_buffer);
1370 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1371 q->min_buffers_needed = 1;
Lad, Prabhakar999ba692014-05-16 10:33:33 -03001372 q->lock = &common->lock;
Hans Verkuil53ddcc62016-02-15 13:09:10 -02001373 q->dev = vpif_dev;
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
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001381 INIT_LIST_HEAD(&common->dma_queue);
1382
Lad, Prabhakare75ea0c2014-05-16 10:33:46 -03001383 /* Initialize the video_device structure */
Lad, Prabhakar8eef6662015-03-08 18:57:23 -03001384 vdev = &ch->video_dev;
Lad, Prabhakare75ea0c2014-05-16 10:33:46 -03001385 strlcpy(vdev->name, VPIF_DRIVER_NAME, sizeof(vdev->name));
Lad, Prabhakar8eef6662015-03-08 18:57:23 -03001386 vdev->release = video_device_release_empty;
Lad, Prabhakare75ea0c2014-05-16 10:33:46 -03001387 vdev->fops = &vpif_fops;
1388 vdev->ioctl_ops = &vpif_ioctl_ops;
1389 vdev->v4l2_dev = &vpif_obj.v4l2_dev;
1390 vdev->vfl_dir = VFL_DIR_RX;
Lad, Prabhakard26d26b2014-05-16 10:33:40 -03001391 vdev->queue = q;
Lad, Prabhakar7ff51192014-05-16 10:33:41 -03001392 vdev->lock = &common->lock;
Lad, Prabhakar8eef6662015-03-08 18:57:23 -03001393 video_set_drvdata(&ch->video_dev, ch);
Lad, Prabhakard26d26b2014-05-16 10:33:40 -03001394 err = video_register_device(vdev,
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001395 VFL_TYPE_GRABBER, (j ? 1 : 0));
1396 if (err)
1397 goto probe_out;
1398 }
1399
1400 v4l2_info(&vpif_obj.v4l2_dev, "VPIF capture driver initialized\n");
1401 return 0;
1402
1403probe_out:
1404 for (k = 0; k < j; k++) {
1405 /* Get the pointer to the channel object */
1406 ch = vpif_obj.dev[k];
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001407 common = &ch->common[k];
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001408 /* Unregister video device */
Lad, Prabhakar8eef6662015-03-08 18:57:23 -03001409 video_unregister_device(&ch->video_dev);
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001410 }
1411 kfree(vpif_obj.sd);
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001412 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1413
1414 return err;
1415}
1416
1417static int vpif_async_complete(struct v4l2_async_notifier *notifier)
1418{
1419 return vpif_probe_complete();
1420}
1421
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001422/**
1423 * vpif_probe : This function probes the vpif capture driver
1424 * @pdev: platform device pointer
1425 *
1426 * This creates device entries by register itself to the V4L2 driver and
1427 * initializes fields of each channel objects
1428 */
1429static __init int vpif_probe(struct platform_device *pdev)
1430{
1431 struct vpif_subdev_info *subdevdata;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001432 struct i2c_adapter *i2c_adap;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001433 struct resource *res;
1434 int subdev_count;
Lad, Prabhakar8eef6662015-03-08 18:57:23 -03001435 int res_idx = 0;
1436 int i, err;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001437
1438 vpif_dev = &pdev->dev;
1439
1440 err = initialize_vpif();
1441 if (err) {
1442 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
1443 return err;
1444 }
1445
Hans Verkuild2f7a1a2011-12-13 05:44:42 -03001446 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
1447 if (err) {
1448 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
1449 return err;
1450 }
1451
Hans Verkuil5be452c2012-09-20 09:06:29 -03001452 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, res_idx))) {
Lad, Prabhakara76a0b32013-06-17 11:20:47 -03001453 err = devm_request_irq(&pdev->dev, res->start, vpif_channel_isr,
Lad, Prabhakare75ea0c2014-05-16 10:33:46 -03001454 IRQF_SHARED, VPIF_DRIVER_NAME,
Lad, Prabhakara76a0b32013-06-17 11:20:47 -03001455 (void *)(&vpif_obj.dev[res_idx]->
1456 channel_id));
1457 if (err) {
1458 err = -EINVAL;
1459 goto vpif_unregister;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001460 }
Hans Verkuil5be452c2012-09-20 09:06:29 -03001461 res_idx++;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001462 }
1463
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001464 vpif_obj.config = pdev->dev.platform_data;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001465
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001466 subdev_count = vpif_obj.config->subdev_count;
Mats Randgaard1f8766b2010-08-30 10:30:37 -03001467 vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001468 GFP_KERNEL);
1469 if (vpif_obj.sd == NULL) {
1470 vpif_err("unable to allocate memory for subdevice pointers\n");
1471 err = -ENOMEM;
Lad, Prabhakar8eef6662015-03-08 18:57:23 -03001472 goto vpif_unregister;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001473 }
1474
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001475 if (!vpif_obj.config->asd_sizes) {
1476 i2c_adap = i2c_get_adapter(1);
1477 for (i = 0; i < subdev_count; i++) {
1478 subdevdata = &vpif_obj.config->subdev_info[i];
1479 vpif_obj.sd[i] =
1480 v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
1481 i2c_adap,
1482 &subdevdata->
1483 board_info,
1484 NULL);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001485
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001486 if (!vpif_obj.sd[i]) {
1487 vpif_err("Error registering v4l2 subdevice\n");
Wei Yongjun2fcd9dc2013-09-02 05:06:10 -03001488 err = -ENODEV;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001489 goto probe_subdev_out;
1490 }
1491 v4l2_info(&vpif_obj.v4l2_dev,
1492 "registered sub device %s\n",
1493 subdevdata->name);
1494 }
1495 vpif_probe_complete();
1496 } else {
Sylwester Nawrockie8419d02013-07-19 12:31:10 -03001497 vpif_obj.notifier.subdevs = vpif_obj.config->asd;
Lad, Prabhakar873229e2013-06-25 11:17:34 -03001498 vpif_obj.notifier.num_subdevs = vpif_obj.config->asd_sizes[0];
1499 vpif_obj.notifier.bound = vpif_async_bound;
1500 vpif_obj.notifier.complete = vpif_async_complete;
1501 err = v4l2_async_notifier_register(&vpif_obj.v4l2_dev,
1502 &vpif_obj.notifier);
1503 if (err) {
1504 vpif_err("Error registering async notifier\n");
1505 err = -EINVAL;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001506 goto probe_subdev_out;
1507 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001508 }
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001509
1510 return 0;
1511
Hans Verkuilb65814e2012-09-20 09:06:26 -03001512probe_subdev_out:
1513 /* free sub devices memory */
1514 kfree(vpif_obj.sd);
Lad, Prabhakarb2de4f22013-06-17 11:20:46 -03001515vpif_unregister:
Hans Verkuild2f7a1a2011-12-13 05:44:42 -03001516 v4l2_device_unregister(&vpif_obj.v4l2_dev);
Lad, Prabhakarb2de4f22013-06-17 11:20:46 -03001517
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001518 return err;
1519}
1520
1521/**
1522 * vpif_remove() - driver remove handler
1523 * @device: ptr to platform device structure
1524 *
1525 * The vidoe device is unregistered
1526 */
1527static int vpif_remove(struct platform_device *device)
1528{
Lad, Prabhakarfa09acc2014-05-16 10:33:31 -03001529 struct common_obj *common;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001530 struct channel_obj *ch;
Lad, Prabhakarb2de4f22013-06-17 11:20:46 -03001531 int i;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001532
1533 v4l2_device_unregister(&vpif_obj.v4l2_dev);
1534
Lad, Prabhakar410ca602013-06-17 11:20:44 -03001535 kfree(vpif_obj.sd);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001536 /* un-register device */
1537 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1538 /* Get the pointer to the channel object */
1539 ch = vpif_obj.dev[i];
Prabhakar Lade9b58722014-07-18 13:31:51 -03001540 common = &ch->common[VPIF_VIDEO_INDEX];
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001541 /* Unregister video device */
Lad, Prabhakar8eef6662015-03-08 18:57:23 -03001542 video_unregister_device(&ch->video_dev);
Lad, Prabhakar410ca602013-06-17 11:20:44 -03001543 kfree(vpif_obj.dev[i]);
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001544 }
1545 return 0;
1546}
1547
Lad, Prabhakarb7047712014-05-16 10:33:50 -03001548#ifdef CONFIG_PM_SLEEP
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001549/**
1550 * vpif_suspend: vpif device suspend
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001551 */
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001552static int vpif_suspend(struct device *dev)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001553{
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001554
1555 struct common_obj *common;
1556 struct channel_obj *ch;
1557 int i;
1558
1559 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1560 /* Get the pointer to the channel object */
1561 ch = vpif_obj.dev[i];
1562 common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakarb7047712014-05-16 10:33:50 -03001563
Prabhakar Ladc54d4a02014-09-06 12:26:51 -03001564 if (!vb2_start_streaming_called(&common->buffer_queue))
Lad, Prabhakarb7047712014-05-16 10:33:50 -03001565 continue;
1566
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001567 mutex_lock(&common->lock);
Lad, Prabhakarb7047712014-05-16 10:33:50 -03001568 /* Disable channel */
1569 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
1570 enable_channel0(0);
1571 channel0_intr_enable(0);
1572 }
1573 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
1574 ycmux_mode == 2) {
1575 enable_channel1(0);
1576 channel1_intr_enable(0);
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001577 }
1578 mutex_unlock(&common->lock);
1579 }
1580
1581 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001582}
1583
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001584/*
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001585 * vpif_resume: vpif device suspend
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001586 */
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001587static int vpif_resume(struct device *dev)
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001588{
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001589 struct common_obj *common;
1590 struct channel_obj *ch;
1591 int i;
1592
1593 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
1594 /* Get the pointer to the channel object */
1595 ch = vpif_obj.dev[i];
1596 common = &ch->common[VPIF_VIDEO_INDEX];
Lad, Prabhakarb7047712014-05-16 10:33:50 -03001597
Prabhakar Ladc54d4a02014-09-06 12:26:51 -03001598 if (!vb2_start_streaming_called(&common->buffer_queue))
Lad, Prabhakarb7047712014-05-16 10:33:50 -03001599 continue;
1600
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001601 mutex_lock(&common->lock);
Lad, Prabhakarb7047712014-05-16 10:33:50 -03001602 /* Enable channel */
1603 if (ch->channel_id == VPIF_CHANNEL0_VIDEO) {
1604 enable_channel0(1);
1605 channel0_intr_enable(1);
1606 }
1607 if (ch->channel_id == VPIF_CHANNEL1_VIDEO ||
1608 ycmux_mode == 2) {
1609 enable_channel1(1);
1610 channel1_intr_enable(1);
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001611 }
1612 mutex_unlock(&common->lock);
1613 }
1614
1615 return 0;
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001616}
Manjunath Hadli3d5946d2012-04-13 04:50:55 -03001617#endif
1618
Lad, Prabhakarb7047712014-05-16 10:33:50 -03001619static SIMPLE_DEV_PM_OPS(vpif_pm_ops, vpif_suspend, vpif_resume);
1620
Mats Randgaardffa1b392010-08-30 10:30:36 -03001621static __refdata struct platform_driver vpif_driver = {
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001622 .driver = {
Lad, Prabhakare75ea0c2014-05-16 10:33:46 -03001623 .name = VPIF_DRIVER_NAME,
Lad, Prabhakarb7047712014-05-16 10:33:50 -03001624 .pm = &vpif_pm_ops,
Muralidharan Karicheri6ffefff2009-09-16 14:31:10 -03001625 },
1626 .probe = vpif_probe,
1627 .remove = vpif_remove,
1628};
1629
Lad, Prabhakarfe424b22013-06-17 11:20:45 -03001630module_platform_driver(vpif_driver);