blob: 71f1ac1adec02affd171f0289445f092b90a03a1 [file] [log] [blame]
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001/*
2 * Copyright (C) 2008-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 * Driver name : VPFE Capture driver
19 * VPFE Capture driver allows applications to capture and stream video
20 * frames on DaVinci SoCs (DM6446, DM355 etc) from a YUV source such as
21 * TVP5146 or Raw Bayer RGB image data from an image sensor
22 * such as Microns' MT9T001, MT9T031 etc.
23 *
24 * These SoCs have, in common, a Video Processing Subsystem (VPSS) that
25 * consists of a Video Processing Front End (VPFE) for capturing
26 * video/raw image data and Video Processing Back End (VPBE) for displaying
27 * YUV data through an in-built analog encoder or Digital LCD port. This
28 * driver is for capture through VPFE. A typical EVM using these SoCs have
29 * following high level configuration.
30 *
31 *
32 * decoder(TVP5146/ YUV/
33 * MT9T001) --> Raw Bayer RGB ---> MUX -> VPFE (CCDC/ISIF)
34 * data input | |
35 * V |
36 * SDRAM |
37 * V
38 * Image Processor
39 * |
40 * V
41 * SDRAM
42 * The data flow happens from a decoder connected to the VPFE over a
43 * YUV embedded (BT.656/BT.1120) or separate sync or raw bayer rgb interface
44 * and to the input of VPFE through an optional MUX (if more inputs are
45 * to be interfaced on the EVM). The input data is first passed through
46 * CCDC (CCD Controller, a.k.a Image Sensor Interface, ISIF). The CCDC
47 * does very little or no processing on YUV data and does pre-process Raw
48 * Bayer RGB data through modules such as Defect Pixel Correction (DFC)
49 * Color Space Conversion (CSC), data gain/offset etc. After this, data
50 * can be written to SDRAM or can be connected to the image processing
51 * block such as IPIPE (on DM355 only).
52 *
53 * Features supported
54 * - MMAP IO
55 * - Capture using TVP5146 over BT.656
56 * - support for interfacing decoders using sub device model
57 * - Work with DM355 or DM6446 CCDC to do Raw Bayer RGB/YUV
58 * data capture to SDRAM.
59 * TODO list
60 * - Support multiple REQBUF after open
61 * - Support for de-allocating buffers through REQBUF
62 * - Support for Raw Bayer RGB capture
63 * - Support for chaining Image Processor
64 * - Support for static allocation of buffers
65 * - Support for USERPTR IO
66 * - Support for STREAMON before QBUF
67 * - Support for control ioctls
68 */
69#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090070#include <linux/slab.h>
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -030071#include <linux/init.h>
72#include <linux/platform_device.h>
73#include <linux/interrupt.h>
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -030074#include <media/v4l2-common.h>
75#include <linux/io.h>
76#include <media/davinci/vpfe_capture.h>
77#include "ccdc_hw_device.h"
78
79static int debug;
80static u32 numbuffers = 3;
81static u32 bufsize = (720 * 576 * 2);
82
83module_param(numbuffers, uint, S_IRUGO);
84module_param(bufsize, uint, S_IRUGO);
85module_param(debug, int, 0644);
86
87MODULE_PARM_DESC(numbuffers, "buffer count (default:3)");
88MODULE_PARM_DESC(bufsize, "buffer size in bytes (default:720 x 576 x 2)");
89MODULE_PARM_DESC(debug, "Debug level 0-1");
90
91MODULE_DESCRIPTION("VPFE Video for Linux Capture Driver");
92MODULE_LICENSE("GPL");
93MODULE_AUTHOR("Texas Instruments");
94
95/* standard information */
96struct vpfe_standard {
97 v4l2_std_id std_id;
98 unsigned int width;
99 unsigned int height;
100 struct v4l2_fract pixelaspect;
101 /* 0 - progressive, 1 - interlaced */
102 int frame_format;
103};
104
105/* ccdc configuration */
106struct ccdc_config {
107 /* This make sure vpfe is probed and ready to go */
108 int vpfe_probed;
109 /* name of ccdc device */
110 char name[32];
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300111};
112
113/* data structures */
114static struct vpfe_config_params config_params = {
115 .min_numbuffers = 3,
116 .numbuffers = 3,
117 .min_bufsize = 720 * 480 * 2,
118 .device_bufsize = 720 * 576 * 2,
119};
120
121/* ccdc device registered */
122static struct ccdc_hw_device *ccdc_dev;
123/* lock for accessing ccdc information */
124static DEFINE_MUTEX(ccdc_lock);
125/* ccdc configuration */
126static struct ccdc_config *ccdc_cfg;
127
Fengguang Wu47c0b562014-08-27 22:12:43 -0300128static const struct vpfe_standard vpfe_standards[] = {
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300129 {V4L2_STD_525_60, 720, 480, {11, 10}, 1},
130 {V4L2_STD_625_50, 720, 576, {54, 59}, 1},
131};
132
133/* Used when raw Bayer image from ccdc is directly captured to SDRAM */
134static const struct vpfe_pixel_format vpfe_pix_fmts[] = {
135 {
136 .fmtdesc = {
137 .index = 0,
138 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
139 .description = "Bayer GrRBGb 8bit A-Law compr.",
140 .pixelformat = V4L2_PIX_FMT_SBGGR8,
141 },
142 .bpp = 1,
143 },
144 {
145 .fmtdesc = {
146 .index = 1,
147 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
148 .description = "Bayer GrRBGb - 16bit",
149 .pixelformat = V4L2_PIX_FMT_SBGGR16,
150 },
151 .bpp = 2,
152 },
153 {
154 .fmtdesc = {
155 .index = 2,
156 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
157 .description = "Bayer GrRBGb 8bit DPCM compr.",
158 .pixelformat = V4L2_PIX_FMT_SGRBG10DPCM8,
159 },
160 .bpp = 1,
161 },
162 {
163 .fmtdesc = {
164 .index = 3,
165 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
166 .description = "YCbCr 4:2:2 Interleaved UYVY",
167 .pixelformat = V4L2_PIX_FMT_UYVY,
168 },
169 .bpp = 2,
170 },
171 {
172 .fmtdesc = {
173 .index = 4,
174 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
175 .description = "YCbCr 4:2:2 Interleaved YUYV",
176 .pixelformat = V4L2_PIX_FMT_YUYV,
177 },
178 .bpp = 2,
179 },
180 {
181 .fmtdesc = {
182 .index = 5,
183 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
184 .description = "Y/CbCr 4:2:0 - Semi planar",
185 .pixelformat = V4L2_PIX_FMT_NV12,
186 },
187 .bpp = 1,
188 },
189};
190
191/*
192 * vpfe_lookup_pix_format()
193 * lookup an entry in the vpfe pix format table based on pix_format
194 */
195static const struct vpfe_pixel_format *vpfe_lookup_pix_format(u32 pix_format)
196{
197 int i;
198
199 for (i = 0; i < ARRAY_SIZE(vpfe_pix_fmts); i++) {
200 if (pix_format == vpfe_pix_fmts[i].fmtdesc.pixelformat)
201 return &vpfe_pix_fmts[i];
202 }
203 return NULL;
204}
205
206/*
207 * vpfe_register_ccdc_device. CCDC module calls this to
208 * register with vpfe capture
209 */
210int vpfe_register_ccdc_device(struct ccdc_hw_device *dev)
211{
212 int ret = 0;
213 printk(KERN_NOTICE "vpfe_register_ccdc_device: %s\n", dev->name);
214
215 BUG_ON(!dev->hw_ops.open);
216 BUG_ON(!dev->hw_ops.enable);
217 BUG_ON(!dev->hw_ops.set_hw_if_params);
218 BUG_ON(!dev->hw_ops.configure);
219 BUG_ON(!dev->hw_ops.set_buftype);
220 BUG_ON(!dev->hw_ops.get_buftype);
221 BUG_ON(!dev->hw_ops.enum_pix);
222 BUG_ON(!dev->hw_ops.set_frame_format);
223 BUG_ON(!dev->hw_ops.get_frame_format);
224 BUG_ON(!dev->hw_ops.get_pixel_format);
225 BUG_ON(!dev->hw_ops.set_pixel_format);
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300226 BUG_ON(!dev->hw_ops.set_image_window);
227 BUG_ON(!dev->hw_ops.get_image_window);
228 BUG_ON(!dev->hw_ops.get_line_length);
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300229 BUG_ON(!dev->hw_ops.getfid);
230
231 mutex_lock(&ccdc_lock);
Markus Elfringc580f292016-10-12 05:46:28 -0300232 if (!ccdc_cfg) {
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300233 /*
234 * TODO. Will this ever happen? if so, we need to fix it.
235 * Proabably we need to add the request to a linked list and
236 * walk through it during vpfe probe
237 */
238 printk(KERN_ERR "vpfe capture not initialized\n");
Muralidharan Karicheri51444ea2010-01-13 20:27:05 -0300239 ret = -EFAULT;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300240 goto unlock;
241 }
242
243 if (strcmp(dev->name, ccdc_cfg->name)) {
244 /* ignore this ccdc */
Muralidharan Karicheri51444ea2010-01-13 20:27:05 -0300245 ret = -EINVAL;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300246 goto unlock;
247 }
248
249 if (ccdc_dev) {
250 printk(KERN_ERR "ccdc already registered\n");
Muralidharan Karicheri51444ea2010-01-13 20:27:05 -0300251 ret = -EINVAL;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300252 goto unlock;
253 }
254
255 ccdc_dev = dev;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300256unlock:
257 mutex_unlock(&ccdc_lock);
258 return ret;
259}
260EXPORT_SYMBOL(vpfe_register_ccdc_device);
261
262/*
263 * vpfe_unregister_ccdc_device. CCDC module calls this to
264 * unregister with vpfe capture
265 */
266void vpfe_unregister_ccdc_device(struct ccdc_hw_device *dev)
267{
Markus Elfringc580f292016-10-12 05:46:28 -0300268 if (!dev) {
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300269 printk(KERN_ERR "invalid ccdc device ptr\n");
270 return;
271 }
272
273 printk(KERN_NOTICE "vpfe_unregister_ccdc_device, dev->name = %s\n",
274 dev->name);
275
276 if (strcmp(dev->name, ccdc_cfg->name)) {
277 /* ignore this ccdc */
278 return;
279 }
280
281 mutex_lock(&ccdc_lock);
282 ccdc_dev = NULL;
283 mutex_unlock(&ccdc_lock);
284 return;
285}
286EXPORT_SYMBOL(vpfe_unregister_ccdc_device);
287
288/*
289 * vpfe_get_ccdc_image_format - Get image parameters based on CCDC settings
290 */
291static int vpfe_get_ccdc_image_format(struct vpfe_device *vpfe_dev,
292 struct v4l2_format *f)
293{
294 struct v4l2_rect image_win;
295 enum ccdc_buftype buf_type;
296 enum ccdc_frmfmt frm_fmt;
297
298 memset(f, 0, sizeof(*f));
299 f->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
300 ccdc_dev->hw_ops.get_image_window(&image_win);
301 f->fmt.pix.width = image_win.width;
302 f->fmt.pix.height = image_win.height;
303 f->fmt.pix.bytesperline = ccdc_dev->hw_ops.get_line_length();
304 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
305 f->fmt.pix.height;
306 buf_type = ccdc_dev->hw_ops.get_buftype();
307 f->fmt.pix.pixelformat = ccdc_dev->hw_ops.get_pixel_format();
308 frm_fmt = ccdc_dev->hw_ops.get_frame_format();
309 if (frm_fmt == CCDC_FRMFMT_PROGRESSIVE)
310 f->fmt.pix.field = V4L2_FIELD_NONE;
311 else if (frm_fmt == CCDC_FRMFMT_INTERLACED) {
312 if (buf_type == CCDC_BUFTYPE_FLD_INTERLEAVED)
313 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
314 else if (buf_type == CCDC_BUFTYPE_FLD_SEPARATED)
315 f->fmt.pix.field = V4L2_FIELD_SEQ_TB;
316 else {
317 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf_type\n");
318 return -EINVAL;
319 }
320 } else {
321 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid frm_fmt\n");
322 return -EINVAL;
323 }
324 return 0;
325}
326
327/*
328 * vpfe_config_ccdc_image_format()
329 * For a pix format, configure ccdc to setup the capture
330 */
331static int vpfe_config_ccdc_image_format(struct vpfe_device *vpfe_dev)
332{
333 enum ccdc_frmfmt frm_fmt = CCDC_FRMFMT_INTERLACED;
334 int ret = 0;
335
336 if (ccdc_dev->hw_ops.set_pixel_format(
337 vpfe_dev->fmt.fmt.pix.pixelformat) < 0) {
338 v4l2_err(&vpfe_dev->v4l2_dev,
339 "couldn't set pix format in ccdc\n");
340 return -EINVAL;
341 }
342 /* configure the image window */
343 ccdc_dev->hw_ops.set_image_window(&vpfe_dev->crop);
344
345 switch (vpfe_dev->fmt.fmt.pix.field) {
346 case V4L2_FIELD_INTERLACED:
347 /* do nothing, since it is default */
348 ret = ccdc_dev->hw_ops.set_buftype(
349 CCDC_BUFTYPE_FLD_INTERLEAVED);
350 break;
351 case V4L2_FIELD_NONE:
352 frm_fmt = CCDC_FRMFMT_PROGRESSIVE;
353 /* buffer type only applicable for interlaced scan */
354 break;
355 case V4L2_FIELD_SEQ_TB:
356 ret = ccdc_dev->hw_ops.set_buftype(
357 CCDC_BUFTYPE_FLD_SEPARATED);
358 break;
359 default:
360 return -EINVAL;
361 }
362
363 /* set the frame format */
364 if (!ret)
365 ret = ccdc_dev->hw_ops.set_frame_format(frm_fmt);
366 return ret;
367}
368/*
369 * vpfe_config_image_format()
370 * For a given standard, this functions sets up the default
371 * pix format & crop values in the vpfe device and ccdc. It first
372 * starts with defaults based values from the standard table.
Hans Verkuilda298c62015-04-09 04:02:34 -0300373 * It then checks if sub device supports get_fmt and then override the
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300374 * values based on that.Sets crop values to match with scan resolution
375 * starting at 0,0. It calls vpfe_config_ccdc_image_format() set the
376 * values in ccdc
377 */
378static int vpfe_config_image_format(struct vpfe_device *vpfe_dev,
Hans Verkuil314527a2013-03-15 06:10:40 -0300379 v4l2_std_id std_id)
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300380{
381 struct vpfe_subdev_info *sdinfo = vpfe_dev->current_subdev;
Hans Verkuilda298c62015-04-09 04:02:34 -0300382 struct v4l2_subdev_format fmt = {
383 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
384 };
385 struct v4l2_mbus_framefmt *mbus_fmt = &fmt.format;
Hans Verkuild3ca7752010-08-07 06:49:33 -0300386 struct v4l2_pix_format *pix = &vpfe_dev->fmt.fmt.pix;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300387 int i, ret = 0;
388
389 for (i = 0; i < ARRAY_SIZE(vpfe_standards); i++) {
Hans Verkuil314527a2013-03-15 06:10:40 -0300390 if (vpfe_standards[i].std_id & std_id) {
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300391 vpfe_dev->std_info.active_pixels =
392 vpfe_standards[i].width;
393 vpfe_dev->std_info.active_lines =
394 vpfe_standards[i].height;
395 vpfe_dev->std_info.frame_format =
396 vpfe_standards[i].frame_format;
397 vpfe_dev->std_index = i;
398 break;
399 }
400 }
401
402 if (i == ARRAY_SIZE(vpfe_standards)) {
403 v4l2_err(&vpfe_dev->v4l2_dev, "standard not supported\n");
404 return -EINVAL;
405 }
406
407 vpfe_dev->crop.top = 0;
408 vpfe_dev->crop.left = 0;
409 vpfe_dev->crop.width = vpfe_dev->std_info.active_pixels;
410 vpfe_dev->crop.height = vpfe_dev->std_info.active_lines;
Hans Verkuild3ca7752010-08-07 06:49:33 -0300411 pix->width = vpfe_dev->crop.width;
412 pix->height = vpfe_dev->crop.height;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300413
414 /* first field and frame format based on standard frame format */
415 if (vpfe_dev->std_info.frame_format) {
Hans Verkuild3ca7752010-08-07 06:49:33 -0300416 pix->field = V4L2_FIELD_INTERLACED;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300417 /* assume V4L2_PIX_FMT_UYVY as default */
Hans Verkuild3ca7752010-08-07 06:49:33 -0300418 pix->pixelformat = V4L2_PIX_FMT_UYVY;
Hans Verkuilda298c62015-04-09 04:02:34 -0300419 v4l2_fill_mbus_format(mbus_fmt, pix,
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -0300420 MEDIA_BUS_FMT_YUYV10_2X10);
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300421 } else {
Hans Verkuild3ca7752010-08-07 06:49:33 -0300422 pix->field = V4L2_FIELD_NONE;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300423 /* assume V4L2_PIX_FMT_SBGGR8 */
Hans Verkuild3ca7752010-08-07 06:49:33 -0300424 pix->pixelformat = V4L2_PIX_FMT_SBGGR8;
Hans Verkuilda298c62015-04-09 04:02:34 -0300425 v4l2_fill_mbus_format(mbus_fmt, pix,
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -0300426 MEDIA_BUS_FMT_SBGGR8_1X8);
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300427 }
428
Hans Verkuilda298c62015-04-09 04:02:34 -0300429 /* if sub device supports get_fmt, override the defaults */
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300430 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev,
Hans Verkuilda298c62015-04-09 04:02:34 -0300431 sdinfo->grp_id, pad, get_fmt, NULL, &fmt);
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300432
433 if (ret && ret != -ENOIOCTLCMD) {
434 v4l2_err(&vpfe_dev->v4l2_dev,
Hans Verkuilda298c62015-04-09 04:02:34 -0300435 "error in getting get_fmt from sub device\n");
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300436 return ret;
437 }
Hans Verkuilda298c62015-04-09 04:02:34 -0300438 v4l2_fill_pix_format(pix, mbus_fmt);
Hans Verkuild3ca7752010-08-07 06:49:33 -0300439 pix->bytesperline = pix->width * 2;
440 pix->sizeimage = pix->bytesperline * pix->height;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300441
442 /* Sets the values in CCDC */
443 ret = vpfe_config_ccdc_image_format(vpfe_dev);
444 if (ret)
445 return ret;
446
447 /* Update the values of sizeimage and bytesperline */
Dan Carpenter692f637522014-09-22 05:00:08 -0300448 pix->bytesperline = ccdc_dev->hw_ops.get_line_length();
449 pix->sizeimage = pix->bytesperline * pix->height;
450
451 return 0;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300452}
453
454static int vpfe_initialize_device(struct vpfe_device *vpfe_dev)
455{
456 int ret = 0;
457
458 /* set first input of current subdevice as the current input */
459 vpfe_dev->current_input = 0;
460
461 /* set default standard */
462 vpfe_dev->std_index = 0;
463
464 /* Configure the default format information */
465 ret = vpfe_config_image_format(vpfe_dev,
Hans Verkuil314527a2013-03-15 06:10:40 -0300466 vpfe_standards[vpfe_dev->std_index].std_id);
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300467 if (ret)
468 return ret;
469
470 /* now open the ccdc device to initialize it */
471 mutex_lock(&ccdc_lock);
Markus Elfringc580f292016-10-12 05:46:28 -0300472 if (!ccdc_dev) {
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300473 v4l2_err(&vpfe_dev->v4l2_dev, "ccdc device not registered\n");
474 ret = -ENODEV;
475 goto unlock;
476 }
477
478 if (!try_module_get(ccdc_dev->owner)) {
479 v4l2_err(&vpfe_dev->v4l2_dev, "Couldn't lock ccdc module\n");
480 ret = -ENODEV;
481 goto unlock;
482 }
483 ret = ccdc_dev->hw_ops.open(vpfe_dev->pdev);
484 if (!ret)
485 vpfe_dev->initialized = 1;
Vaibhav Hiremath085b54a2010-03-27 09:37:07 -0300486
487 /* Clear all VPFE/CCDC interrupts */
488 if (vpfe_dev->cfg->clr_intr)
489 vpfe_dev->cfg->clr_intr(-1);
490
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300491unlock:
492 mutex_unlock(&ccdc_lock);
493 return ret;
494}
495
496/*
497 * vpfe_open : It creates object of file handle structure and
498 * stores it in private_data member of filepointer
499 */
500static int vpfe_open(struct file *file)
501{
502 struct vpfe_device *vpfe_dev = video_drvdata(file);
Lad, Prabhakar3bdaa382014-03-22 08:39:24 -0300503 struct video_device *vdev = video_devdata(file);
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300504 struct vpfe_fh *fh;
505
506 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_open\n");
507
508 if (!vpfe_dev->cfg->num_subdevs) {
509 v4l2_err(&vpfe_dev->v4l2_dev, "No decoder registered\n");
510 return -ENODEV;
511 }
512
513 /* Allocate memory for the file handle object */
Markus Elfring1d3811d2016-10-12 05:44:05 -0300514 fh = kmalloc(sizeof(*fh), GFP_KERNEL);
Markus Elfring11691f02016-10-12 05:22:47 -0300515 if (!fh)
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300516 return -ENOMEM;
Markus Elfring11691f02016-10-12 05:22:47 -0300517
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300518 /* store pointer to fh in private_data member of file */
519 file->private_data = fh;
520 fh->vpfe_dev = vpfe_dev;
Lad, Prabhakar3bdaa382014-03-22 08:39:24 -0300521 v4l2_fh_init(&fh->fh, vdev);
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300522 mutex_lock(&vpfe_dev->lock);
523 /* If decoder is not initialized. initialize it */
524 if (!vpfe_dev->initialized) {
525 if (vpfe_initialize_device(vpfe_dev)) {
526 mutex_unlock(&vpfe_dev->lock);
527 return -ENODEV;
528 }
529 }
530 /* Increment device usrs counter */
531 vpfe_dev->usrs++;
532 /* Set io_allowed member to false */
533 fh->io_allowed = 0;
Lad, Prabhakar3bdaa382014-03-22 08:39:24 -0300534 v4l2_fh_add(&fh->fh);
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300535 mutex_unlock(&vpfe_dev->lock);
536 return 0;
537}
538
539static void vpfe_schedule_next_buffer(struct vpfe_device *vpfe_dev)
540{
541 unsigned long addr;
542
543 vpfe_dev->next_frm = list_entry(vpfe_dev->dma_queue.next,
544 struct videobuf_buffer, queue);
545 list_del(&vpfe_dev->next_frm->queue);
546 vpfe_dev->next_frm->state = VIDEOBUF_ACTIVE;
547 addr = videobuf_to_dma_contig(vpfe_dev->next_frm);
Vaibhav Hiremath844cc0d2010-03-27 09:37:23 -0300548
549 ccdc_dev->hw_ops.setfbaddr(addr);
550}
551
552static void vpfe_schedule_bottom_field(struct vpfe_device *vpfe_dev)
553{
554 unsigned long addr;
555
556 addr = videobuf_to_dma_contig(vpfe_dev->cur_frm);
557 addr += vpfe_dev->field_off;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300558 ccdc_dev->hw_ops.setfbaddr(addr);
559}
560
561static void vpfe_process_buffer_complete(struct vpfe_device *vpfe_dev)
562{
Sakari Ailus8e6057b2012-09-15 15:14:42 -0300563 v4l2_get_timestamp(&vpfe_dev->cur_frm->ts);
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300564 vpfe_dev->cur_frm->state = VIDEOBUF_DONE;
565 vpfe_dev->cur_frm->size = vpfe_dev->fmt.fmt.pix.sizeimage;
566 wake_up_interruptible(&vpfe_dev->cur_frm->done);
567 vpfe_dev->cur_frm = vpfe_dev->next_frm;
568}
569
570/* ISR for VINT0*/
571static irqreturn_t vpfe_isr(int irq, void *dev_id)
572{
573 struct vpfe_device *vpfe_dev = dev_id;
574 enum v4l2_field field;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300575 int fid;
576
577 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "\nStarting vpfe_isr...\n");
578 field = vpfe_dev->fmt.fmt.pix.field;
579
580 /* if streaming not started, don't do anything */
581 if (!vpfe_dev->started)
Vaibhav Hiremath085b54a2010-03-27 09:37:07 -0300582 goto clear_intr;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300583
584 /* only for 6446 this will be applicable */
Markus Elfringc580f292016-10-12 05:46:28 -0300585 if (ccdc_dev->hw_ops.reset)
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300586 ccdc_dev->hw_ops.reset();
587
588 if (field == V4L2_FIELD_NONE) {
589 /* handle progressive frame capture */
590 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
591 "frame format is progressive...\n");
592 if (vpfe_dev->cur_frm != vpfe_dev->next_frm)
593 vpfe_process_buffer_complete(vpfe_dev);
Vaibhav Hiremath085b54a2010-03-27 09:37:07 -0300594 goto clear_intr;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300595 }
596
597 /* interlaced or TB capture check which field we are in hardware */
598 fid = ccdc_dev->hw_ops.getfid();
599
600 /* switch the software maintained field id */
601 vpfe_dev->field_id ^= 1;
602 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "field id = %x:%x.\n",
603 fid, vpfe_dev->field_id);
604 if (fid == vpfe_dev->field_id) {
605 /* we are in-sync here,continue */
606 if (fid == 0) {
607 /*
608 * One frame is just being captured. If the next frame
609 * is available, release the current frame and move on
610 */
611 if (vpfe_dev->cur_frm != vpfe_dev->next_frm)
612 vpfe_process_buffer_complete(vpfe_dev);
613 /*
614 * based on whether the two fields are stored
615 * interleavely or separately in memory, reconfigure
616 * the CCDC memory address
617 */
618 if (field == V4L2_FIELD_SEQ_TB) {
Vaibhav Hiremath844cc0d2010-03-27 09:37:23 -0300619 vpfe_schedule_bottom_field(vpfe_dev);
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300620 }
Vaibhav Hiremath085b54a2010-03-27 09:37:07 -0300621 goto clear_intr;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300622 }
623 /*
624 * if one field is just being captured configure
625 * the next frame get the next frame from the empty
626 * queue if no frame is available hold on to the
627 * current buffer
628 */
629 spin_lock(&vpfe_dev->dma_queue_lock);
630 if (!list_empty(&vpfe_dev->dma_queue) &&
631 vpfe_dev->cur_frm == vpfe_dev->next_frm)
632 vpfe_schedule_next_buffer(vpfe_dev);
633 spin_unlock(&vpfe_dev->dma_queue_lock);
634 } else if (fid == 0) {
635 /*
636 * out of sync. Recover from any hardware out-of-sync.
637 * May loose one frame
638 */
639 vpfe_dev->field_id = fid;
640 }
Vaibhav Hiremath085b54a2010-03-27 09:37:07 -0300641clear_intr:
642 if (vpfe_dev->cfg->clr_intr)
643 vpfe_dev->cfg->clr_intr(irq);
644
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300645 return IRQ_HANDLED;
646}
647
648/* vdint1_isr - isr handler for VINT1 interrupt */
649static irqreturn_t vdint1_isr(int irq, void *dev_id)
650{
651 struct vpfe_device *vpfe_dev = dev_id;
652
653 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "\nInside vdint1_isr...\n");
654
655 /* if streaming not started, don't do anything */
Vaibhav Hiremath085b54a2010-03-27 09:37:07 -0300656 if (!vpfe_dev->started) {
657 if (vpfe_dev->cfg->clr_intr)
658 vpfe_dev->cfg->clr_intr(irq);
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300659 return IRQ_HANDLED;
Vaibhav Hiremath085b54a2010-03-27 09:37:07 -0300660 }
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300661
662 spin_lock(&vpfe_dev->dma_queue_lock);
663 if ((vpfe_dev->fmt.fmt.pix.field == V4L2_FIELD_NONE) &&
664 !list_empty(&vpfe_dev->dma_queue) &&
665 vpfe_dev->cur_frm == vpfe_dev->next_frm)
666 vpfe_schedule_next_buffer(vpfe_dev);
667 spin_unlock(&vpfe_dev->dma_queue_lock);
Vaibhav Hiremath085b54a2010-03-27 09:37:07 -0300668
669 if (vpfe_dev->cfg->clr_intr)
670 vpfe_dev->cfg->clr_intr(irq);
671
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300672 return IRQ_HANDLED;
673}
674
675static void vpfe_detach_irq(struct vpfe_device *vpfe_dev)
676{
677 enum ccdc_frmfmt frame_format;
678
679 frame_format = ccdc_dev->hw_ops.get_frame_format();
680 if (frame_format == CCDC_FRMFMT_PROGRESSIVE)
Vaibhav Hiremath1ead6962009-11-09 09:15:07 -0300681 free_irq(vpfe_dev->ccdc_irq1, vpfe_dev);
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300682}
683
684static int vpfe_attach_irq(struct vpfe_device *vpfe_dev)
685{
686 enum ccdc_frmfmt frame_format;
687
688 frame_format = ccdc_dev->hw_ops.get_frame_format();
689 if (frame_format == CCDC_FRMFMT_PROGRESSIVE) {
690 return request_irq(vpfe_dev->ccdc_irq1, vdint1_isr,
Michael Opdenackerd8c279a2013-09-08 23:30:11 -0300691 0, "vpfe_capture1",
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300692 vpfe_dev);
693 }
694 return 0;
695}
696
697/* vpfe_stop_ccdc_capture: stop streaming in ccdc/isif */
698static void vpfe_stop_ccdc_capture(struct vpfe_device *vpfe_dev)
699{
700 vpfe_dev->started = 0;
701 ccdc_dev->hw_ops.enable(0);
702 if (ccdc_dev->hw_ops.enable_out_to_sdram)
703 ccdc_dev->hw_ops.enable_out_to_sdram(0);
704}
705
706/*
707 * vpfe_release : This function deletes buffer queue, frees the
708 * buffers and the vpfe file handle
709 */
710static int vpfe_release(struct file *file)
711{
712 struct vpfe_device *vpfe_dev = video_drvdata(file);
713 struct vpfe_fh *fh = file->private_data;
714 struct vpfe_subdev_info *sdinfo;
715 int ret;
716
717 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_release\n");
718
719 /* Get the device lock */
720 mutex_lock(&vpfe_dev->lock);
721 /* if this instance is doing IO */
722 if (fh->io_allowed) {
723 if (vpfe_dev->started) {
724 sdinfo = vpfe_dev->current_subdev;
725 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev,
726 sdinfo->grp_id,
727 video, s_stream, 0);
728 if (ret && (ret != -ENOIOCTLCMD))
729 v4l2_err(&vpfe_dev->v4l2_dev,
730 "stream off failed in subdev\n");
731 vpfe_stop_ccdc_capture(vpfe_dev);
732 vpfe_detach_irq(vpfe_dev);
733 videobuf_streamoff(&vpfe_dev->buffer_queue);
734 }
735 vpfe_dev->io_usrs = 0;
736 vpfe_dev->numbuffers = config_params.numbuffers;
Lad, Prabhakarc1d1e402014-03-23 02:44:11 -0300737 videobuf_stop(&vpfe_dev->buffer_queue);
738 videobuf_mmap_free(&vpfe_dev->buffer_queue);
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300739 }
740
741 /* Decrement device usrs counter */
742 vpfe_dev->usrs--;
Lad, Prabhakar3bdaa382014-03-22 08:39:24 -0300743 v4l2_fh_del(&fh->fh);
744 v4l2_fh_exit(&fh->fh);
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300745 /* If this is the last file handle */
746 if (!vpfe_dev->usrs) {
747 vpfe_dev->initialized = 0;
748 if (ccdc_dev->hw_ops.close)
749 ccdc_dev->hw_ops.close(vpfe_dev->pdev);
750 module_put(ccdc_dev->owner);
751 }
752 mutex_unlock(&vpfe_dev->lock);
753 file->private_data = NULL;
754 /* Free memory allocated to file handle object */
755 kfree(fh);
756 return 0;
757}
758
759/*
760 * vpfe_mmap : It is used to map kernel space buffers
761 * into user spaces
762 */
763static int vpfe_mmap(struct file *file, struct vm_area_struct *vma)
764{
765 /* Get the device object and file handle object */
766 struct vpfe_device *vpfe_dev = video_drvdata(file);
767
768 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_mmap\n");
769
770 return videobuf_mmap_mapper(&vpfe_dev->buffer_queue, vma);
771}
772
773/*
774 * vpfe_poll: It is used for select/poll system call
775 */
776static unsigned int vpfe_poll(struct file *file, poll_table *wait)
777{
778 struct vpfe_device *vpfe_dev = video_drvdata(file);
779
780 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_poll\n");
781
782 if (vpfe_dev->started)
783 return videobuf_poll_stream(file,
784 &vpfe_dev->buffer_queue, wait);
785 return 0;
786}
787
788/* vpfe capture driver file operations */
789static const struct v4l2_file_operations vpfe_fops = {
790 .owner = THIS_MODULE,
791 .open = vpfe_open,
792 .release = vpfe_release,
793 .unlocked_ioctl = video_ioctl2,
794 .mmap = vpfe_mmap,
795 .poll = vpfe_poll
796};
797
798/*
799 * vpfe_check_format()
800 * This function adjust the input pixel format as per hardware
801 * capabilities and update the same in pixfmt.
802 * Following algorithm used :-
803 *
804 * If given pixformat is not in the vpfe list of pix formats or not
805 * supported by the hardware, current value of pixformat in the device
806 * is used
807 * If given field is not supported, then current field is used. If field
808 * is different from current, then it is matched with that from sub device.
809 * Minimum height is 2 lines for interlaced or tb field and 1 line for
810 * progressive. Maximum height is clamped to active active lines of scan
811 * Minimum width is 32 bytes in memory and width is clamped to active
812 * pixels of scan.
813 * bytesperline is a multiple of 32.
814 */
815static const struct vpfe_pixel_format *
816 vpfe_check_format(struct vpfe_device *vpfe_dev,
817 struct v4l2_pix_format *pixfmt)
818{
819 u32 min_height = 1, min_width = 32, max_width, max_height;
820 const struct vpfe_pixel_format *vpfe_pix_fmt;
821 u32 pix;
822 int temp, found;
823
824 vpfe_pix_fmt = vpfe_lookup_pix_format(pixfmt->pixelformat);
Markus Elfringc580f292016-10-12 05:46:28 -0300825 if (!vpfe_pix_fmt) {
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300826 /*
827 * use current pixel format in the vpfe device. We
828 * will find this pix format in the table
829 */
830 pixfmt->pixelformat = vpfe_dev->fmt.fmt.pix.pixelformat;
831 vpfe_pix_fmt = vpfe_lookup_pix_format(pixfmt->pixelformat);
832 }
833
834 /* check if hw supports it */
835 temp = 0;
836 found = 0;
837 while (ccdc_dev->hw_ops.enum_pix(&pix, temp) >= 0) {
838 if (vpfe_pix_fmt->fmtdesc.pixelformat == pix) {
839 found = 1;
840 break;
841 }
842 temp++;
843 }
844
845 if (!found) {
846 /* use current pixel format */
847 pixfmt->pixelformat = vpfe_dev->fmt.fmt.pix.pixelformat;
848 /*
849 * Since this is currently used in the vpfe device, we
850 * will find this pix format in the table
851 */
852 vpfe_pix_fmt = vpfe_lookup_pix_format(pixfmt->pixelformat);
853 }
854
855 /* check what field format is supported */
856 if (pixfmt->field == V4L2_FIELD_ANY) {
857 /* if field is any, use current value as default */
858 pixfmt->field = vpfe_dev->fmt.fmt.pix.field;
859 }
860
861 /*
862 * if field is not same as current field in the vpfe device
863 * try matching the field with the sub device field
864 */
865 if (vpfe_dev->fmt.fmt.pix.field != pixfmt->field) {
866 /*
867 * If field value is not in the supported fields, use current
868 * field used in the device as default
869 */
870 switch (pixfmt->field) {
871 case V4L2_FIELD_INTERLACED:
872 case V4L2_FIELD_SEQ_TB:
873 /* if sub device is supporting progressive, use that */
874 if (!vpfe_dev->std_info.frame_format)
875 pixfmt->field = V4L2_FIELD_NONE;
876 break;
877 case V4L2_FIELD_NONE:
878 if (vpfe_dev->std_info.frame_format)
879 pixfmt->field = V4L2_FIELD_INTERLACED;
880 break;
881
882 default:
883 /* use current field as default */
884 pixfmt->field = vpfe_dev->fmt.fmt.pix.field;
885 break;
886 }
887 }
888
889 /* Now adjust image resolutions supported */
890 if (pixfmt->field == V4L2_FIELD_INTERLACED ||
891 pixfmt->field == V4L2_FIELD_SEQ_TB)
892 min_height = 2;
893
894 max_width = vpfe_dev->std_info.active_pixels;
895 max_height = vpfe_dev->std_info.active_lines;
896 min_width /= vpfe_pix_fmt->bpp;
897
898 v4l2_info(&vpfe_dev->v4l2_dev, "width = %d, height = %d, bpp = %d\n",
899 pixfmt->width, pixfmt->height, vpfe_pix_fmt->bpp);
900
901 pixfmt->width = clamp((pixfmt->width), min_width, max_width);
902 pixfmt->height = clamp((pixfmt->height), min_height, max_height);
903
904 /* If interlaced, adjust height to be a multiple of 2 */
905 if (pixfmt->field == V4L2_FIELD_INTERLACED)
906 pixfmt->height &= (~1);
907 /*
908 * recalculate bytesperline and sizeimage since width
909 * and height might have changed
910 */
911 pixfmt->bytesperline = (((pixfmt->width * vpfe_pix_fmt->bpp) + 31)
912 & ~31);
913 if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12)
914 pixfmt->sizeimage =
915 pixfmt->bytesperline * pixfmt->height +
916 ((pixfmt->bytesperline * pixfmt->height) >> 1);
917 else
918 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height;
919
Mauro Carvalho Chehabded026e2016-10-18 17:44:08 -0200920 v4l2_info(&vpfe_dev->v4l2_dev, "adjusted width = %d, height = %d, bpp = %d, bytesperline = %d, sizeimage = %d\n",
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300921 pixfmt->width, pixfmt->height, vpfe_pix_fmt->bpp,
922 pixfmt->bytesperline, pixfmt->sizeimage);
923 return vpfe_pix_fmt;
924}
925
926static int vpfe_querycap(struct file *file, void *priv,
927 struct v4l2_capability *cap)
928{
929 struct vpfe_device *vpfe_dev = video_drvdata(file);
930
931 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querycap\n");
932
Hans Verkuil8c17e5e2014-11-24 06:37:26 -0300933 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
934 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300935 strlcpy(cap->driver, CAPTURE_DRV_NAME, sizeof(cap->driver));
936 strlcpy(cap->bus_info, "VPFE", sizeof(cap->bus_info));
937 strlcpy(cap->card, vpfe_dev->cfg->card_name, sizeof(cap->card));
938 return 0;
939}
940
941static int vpfe_g_fmt_vid_cap(struct file *file, void *priv,
942 struct v4l2_format *fmt)
943{
944 struct vpfe_device *vpfe_dev = video_drvdata(file);
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300945
946 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_fmt_vid_cap\n");
947 /* Fill in the information about format */
948 *fmt = vpfe_dev->fmt;
Mauro Carvalho Chehabb80cefb2014-09-03 15:39:46 -0300949 return 0;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300950}
951
952static int vpfe_enum_fmt_vid_cap(struct file *file, void *priv,
953 struct v4l2_fmtdesc *fmt)
954{
955 struct vpfe_device *vpfe_dev = video_drvdata(file);
956 const struct vpfe_pixel_format *pix_fmt;
957 int temp_index;
958 u32 pix;
959
960 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_enum_fmt_vid_cap\n");
961
962 if (ccdc_dev->hw_ops.enum_pix(&pix, fmt->index) < 0)
963 return -EINVAL;
964
965 /* Fill in the information about format */
966 pix_fmt = vpfe_lookup_pix_format(pix);
Markus Elfringc580f292016-10-12 05:46:28 -0300967 if (pix_fmt) {
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300968 temp_index = fmt->index;
969 *fmt = pix_fmt->fmtdesc;
970 fmt->index = temp_index;
971 return 0;
972 }
973 return -EINVAL;
974}
975
976static int vpfe_s_fmt_vid_cap(struct file *file, void *priv,
977 struct v4l2_format *fmt)
978{
979 struct vpfe_device *vpfe_dev = video_drvdata(file);
980 const struct vpfe_pixel_format *pix_fmts;
981 int ret = 0;
982
983 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_fmt_vid_cap\n");
984
985 /* If streaming is started, return error */
986 if (vpfe_dev->started) {
987 v4l2_err(&vpfe_dev->v4l2_dev, "Streaming is started\n");
988 return -EBUSY;
989 }
990
991 /* Check for valid frame format */
992 pix_fmts = vpfe_check_format(vpfe_dev, &fmt->fmt.pix);
Markus Elfringc580f292016-10-12 05:46:28 -0300993 if (!pix_fmts)
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300994 return -EINVAL;
995
996 /* store the pixel format in the device object */
997 ret = mutex_lock_interruptible(&vpfe_dev->lock);
998 if (ret)
999 return ret;
1000
1001 /* First detach any IRQ if currently attached */
1002 vpfe_detach_irq(vpfe_dev);
1003 vpfe_dev->fmt = *fmt;
1004 /* set image capture parameters in the ccdc */
1005 ret = vpfe_config_ccdc_image_format(vpfe_dev);
1006 mutex_unlock(&vpfe_dev->lock);
1007 return ret;
1008}
1009
1010static int vpfe_try_fmt_vid_cap(struct file *file, void *priv,
1011 struct v4l2_format *f)
1012{
1013 struct vpfe_device *vpfe_dev = video_drvdata(file);
1014 const struct vpfe_pixel_format *pix_fmts;
1015
1016 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_try_fmt_vid_cap\n");
1017
1018 pix_fmts = vpfe_check_format(vpfe_dev, &f->fmt.pix);
Markus Elfringc580f292016-10-12 05:46:28 -03001019 if (!pix_fmts)
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001020 return -EINVAL;
1021 return 0;
1022}
1023
1024/*
1025 * vpfe_get_subdev_input_index - Get subdev index and subdev input index for a
1026 * given app input index
1027 */
1028static int vpfe_get_subdev_input_index(struct vpfe_device *vpfe_dev,
1029 int *subdev_index,
1030 int *subdev_input_index,
1031 int app_input_index)
1032{
1033 struct vpfe_config *cfg = vpfe_dev->cfg;
1034 struct vpfe_subdev_info *sdinfo;
1035 int i, j = 0;
1036
1037 for (i = 0; i < cfg->num_subdevs; i++) {
1038 sdinfo = &cfg->sub_devs[i];
1039 if (app_input_index < (j + sdinfo->num_inputs)) {
1040 *subdev_index = i;
1041 *subdev_input_index = app_input_index - j;
1042 return 0;
1043 }
1044 j += sdinfo->num_inputs;
1045 }
1046 return -EINVAL;
1047}
1048
1049/*
1050 * vpfe_get_app_input - Get app input index for a given subdev input index
1051 * driver stores the input index of the current sub device and translate it
1052 * when application request the current input
1053 */
1054static int vpfe_get_app_input_index(struct vpfe_device *vpfe_dev,
1055 int *app_input_index)
1056{
1057 struct vpfe_config *cfg = vpfe_dev->cfg;
1058 struct vpfe_subdev_info *sdinfo;
1059 int i, j = 0;
1060
1061 for (i = 0; i < cfg->num_subdevs; i++) {
1062 sdinfo = &cfg->sub_devs[i];
1063 if (!strcmp(sdinfo->name, vpfe_dev->current_subdev->name)) {
1064 if (vpfe_dev->current_input >= sdinfo->num_inputs)
1065 return -1;
1066 *app_input_index = j + vpfe_dev->current_input;
1067 return 0;
1068 }
1069 j += sdinfo->num_inputs;
1070 }
1071 return -EINVAL;
1072}
1073
1074static int vpfe_enum_input(struct file *file, void *priv,
1075 struct v4l2_input *inp)
1076{
1077 struct vpfe_device *vpfe_dev = video_drvdata(file);
1078 struct vpfe_subdev_info *sdinfo;
1079 int subdev, index ;
1080
1081 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_enum_input\n");
1082
1083 if (vpfe_get_subdev_input_index(vpfe_dev,
1084 &subdev,
1085 &index,
1086 inp->index) < 0) {
Mauro Carvalho Chehabded026e2016-10-18 17:44:08 -02001087 v4l2_err(&vpfe_dev->v4l2_dev, "input information not found for the subdev\n");
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001088 return -EINVAL;
1089 }
1090 sdinfo = &vpfe_dev->cfg->sub_devs[subdev];
1091 memcpy(inp, &sdinfo->inputs[index], sizeof(struct v4l2_input));
1092 return 0;
1093}
1094
1095static int vpfe_g_input(struct file *file, void *priv, unsigned int *index)
1096{
1097 struct vpfe_device *vpfe_dev = video_drvdata(file);
1098
1099 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_input\n");
1100
1101 return vpfe_get_app_input_index(vpfe_dev, index);
1102}
1103
1104
1105static int vpfe_s_input(struct file *file, void *priv, unsigned int index)
1106{
1107 struct vpfe_device *vpfe_dev = video_drvdata(file);
Hans Verkuil6f55dba2013-03-04 05:48:43 -03001108 struct v4l2_subdev *sd;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001109 struct vpfe_subdev_info *sdinfo;
1110 int subdev_index, inp_index;
1111 struct vpfe_route *route;
1112 u32 input = 0, output = 0;
1113 int ret = -EINVAL;
1114
1115 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_input\n");
1116
1117 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1118 if (ret)
1119 return ret;
1120
1121 /*
1122 * If streaming is started return device busy
1123 * error
1124 */
1125 if (vpfe_dev->started) {
1126 v4l2_err(&vpfe_dev->v4l2_dev, "Streaming is on\n");
1127 ret = -EBUSY;
1128 goto unlock_out;
1129 }
Peter Senna Tschudin9a888ba2012-09-04 08:05:03 -03001130 ret = vpfe_get_subdev_input_index(vpfe_dev,
1131 &subdev_index,
1132 &inp_index,
1133 index);
1134 if (ret < 0) {
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001135 v4l2_err(&vpfe_dev->v4l2_dev, "invalid input index\n");
1136 goto unlock_out;
1137 }
1138
1139 sdinfo = &vpfe_dev->cfg->sub_devs[subdev_index];
Hans Verkuil6f55dba2013-03-04 05:48:43 -03001140 sd = vpfe_dev->sd[subdev_index];
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001141 route = &sdinfo->routes[inp_index];
1142 if (route && sdinfo->can_route) {
1143 input = route->input;
1144 output = route->output;
1145 }
1146
Hans Verkuil6f55dba2013-03-04 05:48:43 -03001147 if (sd)
1148 ret = v4l2_subdev_call(sd, video, s_routing, input, output, 0);
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001149
1150 if (ret) {
1151 v4l2_err(&vpfe_dev->v4l2_dev,
1152 "vpfe_doioctl:error in setting input in decoder\n");
1153 ret = -EINVAL;
1154 goto unlock_out;
1155 }
1156 vpfe_dev->current_subdev = sdinfo;
Hans Verkuil6f55dba2013-03-04 05:48:43 -03001157 if (sd)
1158 vpfe_dev->v4l2_dev.ctrl_handler = sd->ctrl_handler;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001159 vpfe_dev->current_input = index;
1160 vpfe_dev->std_index = 0;
1161
1162 /* set the bus/interface parameter for the sub device in ccdc */
1163 ret = ccdc_dev->hw_ops.set_hw_if_params(&sdinfo->ccdc_if_params);
1164 if (ret)
1165 goto unlock_out;
1166
1167 /* set the default image parameters in the device */
1168 ret = vpfe_config_image_format(vpfe_dev,
Hans Verkuil314527a2013-03-15 06:10:40 -03001169 vpfe_standards[vpfe_dev->std_index].std_id);
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001170unlock_out:
1171 mutex_unlock(&vpfe_dev->lock);
1172 return ret;
1173}
1174
1175static int vpfe_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
1176{
1177 struct vpfe_device *vpfe_dev = video_drvdata(file);
1178 struct vpfe_subdev_info *sdinfo;
1179 int ret = 0;
1180
1181 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querystd\n");
1182
1183 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1184 sdinfo = vpfe_dev->current_subdev;
1185 if (ret)
1186 return ret;
1187 /* Call querystd function of decoder device */
1188 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1189 video, querystd, std_id);
1190 mutex_unlock(&vpfe_dev->lock);
1191 return ret;
1192}
1193
Hans Verkuil314527a2013-03-15 06:10:40 -03001194static int vpfe_s_std(struct file *file, void *priv, v4l2_std_id std_id)
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001195{
1196 struct vpfe_device *vpfe_dev = video_drvdata(file);
1197 struct vpfe_subdev_info *sdinfo;
1198 int ret = 0;
1199
1200 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_std\n");
1201
1202 /* Call decoder driver function to set the standard */
1203 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1204 if (ret)
1205 return ret;
1206
1207 sdinfo = vpfe_dev->current_subdev;
1208 /* If streaming is started, return device busy error */
1209 if (vpfe_dev->started) {
1210 v4l2_err(&vpfe_dev->v4l2_dev, "streaming is started\n");
1211 ret = -EBUSY;
1212 goto unlock_out;
1213 }
1214
1215 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
Laurent Pinchart8774bed2014-04-28 16:53:01 -03001216 video, s_std, std_id);
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001217 if (ret < 0) {
1218 v4l2_err(&vpfe_dev->v4l2_dev, "Failed to set standard\n");
1219 goto unlock_out;
1220 }
1221 ret = vpfe_config_image_format(vpfe_dev, std_id);
1222
1223unlock_out:
1224 mutex_unlock(&vpfe_dev->lock);
1225 return ret;
1226}
1227
1228static int vpfe_g_std(struct file *file, void *priv, v4l2_std_id *std_id)
1229{
1230 struct vpfe_device *vpfe_dev = video_drvdata(file);
1231
1232 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_std\n");
1233
1234 *std_id = vpfe_standards[vpfe_dev->std_index].std_id;
1235 return 0;
1236}
1237/*
1238 * Videobuf operations
1239 */
1240static int vpfe_videobuf_setup(struct videobuf_queue *vq,
1241 unsigned int *count,
1242 unsigned int *size)
1243{
1244 struct vpfe_fh *fh = vq->priv_data;
1245 struct vpfe_device *vpfe_dev = fh->vpfe_dev;
1246
1247 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buffer_setup\n");
Vaibhav Hiremath844cc0d2010-03-27 09:37:23 -03001248 *size = vpfe_dev->fmt.fmt.pix.sizeimage;
1249 if (vpfe_dev->memory == V4L2_MEMORY_MMAP &&
1250 vpfe_dev->fmt.fmt.pix.sizeimage > config_params.device_bufsize)
1251 *size = config_params.device_bufsize;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001252
1253 if (*count < config_params.min_numbuffers)
1254 *count = config_params.min_numbuffers;
1255 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1256 "count=%d, size=%d\n", *count, *size);
1257 return 0;
1258}
1259
1260static int vpfe_videobuf_prepare(struct videobuf_queue *vq,
1261 struct videobuf_buffer *vb,
1262 enum v4l2_field field)
1263{
1264 struct vpfe_fh *fh = vq->priv_data;
1265 struct vpfe_device *vpfe_dev = fh->vpfe_dev;
Vaibhav Hiremath844cc0d2010-03-27 09:37:23 -03001266 unsigned long addr;
1267 int ret;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001268
1269 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buffer_prepare\n");
1270
1271 /* If buffer is not initialized, initialize it */
1272 if (VIDEOBUF_NEEDS_INIT == vb->state) {
1273 vb->width = vpfe_dev->fmt.fmt.pix.width;
1274 vb->height = vpfe_dev->fmt.fmt.pix.height;
1275 vb->size = vpfe_dev->fmt.fmt.pix.sizeimage;
1276 vb->field = field;
Vaibhav Hiremath844cc0d2010-03-27 09:37:23 -03001277
Joe Perchesb1dc6142010-11-15 00:04:28 -03001278 ret = videobuf_iolock(vq, vb, NULL);
Vaibhav Hiremath844cc0d2010-03-27 09:37:23 -03001279 if (ret < 0)
1280 return ret;
1281
1282 addr = videobuf_to_dma_contig(vb);
1283 /* Make sure user addresses are aligned to 32 bytes */
1284 if (!ALIGN(addr, 32))
1285 return -EINVAL;
1286
1287 vb->state = VIDEOBUF_PREPARED;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001288 }
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001289 return 0;
1290}
1291
1292static void vpfe_videobuf_queue(struct videobuf_queue *vq,
1293 struct videobuf_buffer *vb)
1294{
1295 /* Get the file handle object and device object */
1296 struct vpfe_fh *fh = vq->priv_data;
1297 struct vpfe_device *vpfe_dev = fh->vpfe_dev;
1298 unsigned long flags;
1299
1300 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buffer_queue\n");
1301
1302 /* add the buffer to the DMA queue */
1303 spin_lock_irqsave(&vpfe_dev->dma_queue_lock, flags);
1304 list_add_tail(&vb->queue, &vpfe_dev->dma_queue);
1305 spin_unlock_irqrestore(&vpfe_dev->dma_queue_lock, flags);
1306
1307 /* Change state of the buffer */
1308 vb->state = VIDEOBUF_QUEUED;
1309}
1310
1311static void vpfe_videobuf_release(struct videobuf_queue *vq,
1312 struct videobuf_buffer *vb)
1313{
1314 struct vpfe_fh *fh = vq->priv_data;
1315 struct vpfe_device *vpfe_dev = fh->vpfe_dev;
1316 unsigned long flags;
1317
1318 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_videobuf_release\n");
1319
1320 /*
1321 * We need to flush the buffer from the dma queue since
1322 * they are de-allocated
1323 */
1324 spin_lock_irqsave(&vpfe_dev->dma_queue_lock, flags);
1325 INIT_LIST_HEAD(&vpfe_dev->dma_queue);
1326 spin_unlock_irqrestore(&vpfe_dev->dma_queue_lock, flags);
1327 videobuf_dma_contig_free(vq, vb);
1328 vb->state = VIDEOBUF_NEEDS_INIT;
1329}
1330
1331static struct videobuf_queue_ops vpfe_videobuf_qops = {
1332 .buf_setup = vpfe_videobuf_setup,
1333 .buf_prepare = vpfe_videobuf_prepare,
1334 .buf_queue = vpfe_videobuf_queue,
1335 .buf_release = vpfe_videobuf_release,
1336};
1337
1338/*
1339 * vpfe_reqbufs. currently support REQBUF only once opening
1340 * the device.
1341 */
1342static int vpfe_reqbufs(struct file *file, void *priv,
1343 struct v4l2_requestbuffers *req_buf)
1344{
1345 struct vpfe_device *vpfe_dev = video_drvdata(file);
1346 struct vpfe_fh *fh = file->private_data;
1347 int ret = 0;
1348
1349 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_reqbufs\n");
1350
1351 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != req_buf->type) {
1352 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buffer type\n");
1353 return -EINVAL;
1354 }
1355
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001356 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1357 if (ret)
1358 return ret;
1359
1360 if (vpfe_dev->io_usrs != 0) {
1361 v4l2_err(&vpfe_dev->v4l2_dev, "Only one IO user allowed\n");
1362 ret = -EBUSY;
1363 goto unlock_out;
1364 }
1365
1366 vpfe_dev->memory = req_buf->memory;
1367 videobuf_queue_dma_contig_init(&vpfe_dev->buffer_queue,
1368 &vpfe_videobuf_qops,
Vaibhav Hiremath204e6ea2009-11-09 09:07:36 -03001369 vpfe_dev->pdev,
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001370 &vpfe_dev->irqlock,
1371 req_buf->type,
1372 vpfe_dev->fmt.fmt.pix.field,
1373 sizeof(struct videobuf_buffer),
Hans Verkuile3cfd442010-09-30 09:18:52 -03001374 fh, NULL);
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001375
1376 fh->io_allowed = 1;
1377 vpfe_dev->io_usrs = 1;
1378 INIT_LIST_HEAD(&vpfe_dev->dma_queue);
1379 ret = videobuf_reqbufs(&vpfe_dev->buffer_queue, req_buf);
1380unlock_out:
1381 mutex_unlock(&vpfe_dev->lock);
1382 return ret;
1383}
1384
1385static int vpfe_querybuf(struct file *file, void *priv,
1386 struct v4l2_buffer *buf)
1387{
1388 struct vpfe_device *vpfe_dev = video_drvdata(file);
1389
1390 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querybuf\n");
1391
1392 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf->type) {
1393 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1394 return -EINVAL;
1395 }
1396
1397 if (vpfe_dev->memory != V4L2_MEMORY_MMAP) {
1398 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid memory\n");
1399 return -EINVAL;
1400 }
1401 /* Call videobuf_querybuf to get information */
1402 return videobuf_querybuf(&vpfe_dev->buffer_queue, buf);
1403}
1404
1405static int vpfe_qbuf(struct file *file, void *priv,
1406 struct v4l2_buffer *p)
1407{
1408 struct vpfe_device *vpfe_dev = video_drvdata(file);
1409 struct vpfe_fh *fh = file->private_data;
1410
1411 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_qbuf\n");
1412
1413 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != p->type) {
1414 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1415 return -EINVAL;
1416 }
1417
1418 /*
1419 * If this file handle is not allowed to do IO,
1420 * return error
1421 */
1422 if (!fh->io_allowed) {
1423 v4l2_err(&vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1424 return -EACCES;
1425 }
1426 return videobuf_qbuf(&vpfe_dev->buffer_queue, p);
1427}
1428
1429static int vpfe_dqbuf(struct file *file, void *priv,
1430 struct v4l2_buffer *buf)
1431{
1432 struct vpfe_device *vpfe_dev = video_drvdata(file);
1433
1434 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_dqbuf\n");
1435
1436 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf->type) {
1437 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1438 return -EINVAL;
1439 }
1440 return videobuf_dqbuf(&vpfe_dev->buffer_queue,
1441 buf, file->f_flags & O_NONBLOCK);
1442}
1443
1444/*
1445 * vpfe_calculate_offsets : This function calculates buffers offset
1446 * for top and bottom field
1447 */
1448static void vpfe_calculate_offsets(struct vpfe_device *vpfe_dev)
1449{
1450 struct v4l2_rect image_win;
1451
1452 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_calculate_offsets\n");
1453
1454 ccdc_dev->hw_ops.get_image_window(&image_win);
1455 vpfe_dev->field_off = image_win.height * image_win.width;
1456}
1457
1458/* vpfe_start_ccdc_capture: start streaming in ccdc/isif */
1459static void vpfe_start_ccdc_capture(struct vpfe_device *vpfe_dev)
1460{
1461 ccdc_dev->hw_ops.enable(1);
1462 if (ccdc_dev->hw_ops.enable_out_to_sdram)
1463 ccdc_dev->hw_ops.enable_out_to_sdram(1);
1464 vpfe_dev->started = 1;
1465}
1466
1467/*
1468 * vpfe_streamon. Assume the DMA queue is not empty.
1469 * application is expected to call QBUF before calling
1470 * this ioctl. If not, driver returns error
1471 */
1472static int vpfe_streamon(struct file *file, void *priv,
1473 enum v4l2_buf_type buf_type)
1474{
1475 struct vpfe_device *vpfe_dev = video_drvdata(file);
1476 struct vpfe_fh *fh = file->private_data;
1477 struct vpfe_subdev_info *sdinfo;
1478 unsigned long addr;
1479 int ret = 0;
1480
1481 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_streamon\n");
1482
1483 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf_type) {
1484 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1485 return -EINVAL;
1486 }
1487
1488 /* If file handle is not allowed IO, return error */
1489 if (!fh->io_allowed) {
1490 v4l2_err(&vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1491 return -EACCES;
1492 }
1493
1494 sdinfo = vpfe_dev->current_subdev;
1495 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1496 video, s_stream, 1);
1497
1498 if (ret && (ret != -ENOIOCTLCMD)) {
1499 v4l2_err(&vpfe_dev->v4l2_dev, "stream on failed in subdev\n");
1500 return -EINVAL;
1501 }
1502
1503 /* If buffer queue is empty, return error */
1504 if (list_empty(&vpfe_dev->buffer_queue.stream)) {
1505 v4l2_err(&vpfe_dev->v4l2_dev, "buffer queue is empty\n");
1506 return -EIO;
1507 }
1508
1509 /* Call videobuf_streamon to start streaming * in videobuf */
1510 ret = videobuf_streamon(&vpfe_dev->buffer_queue);
1511 if (ret)
1512 return ret;
1513
1514
1515 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1516 if (ret)
1517 goto streamoff;
1518 /* Get the next frame from the buffer queue */
1519 vpfe_dev->next_frm = list_entry(vpfe_dev->dma_queue.next,
1520 struct videobuf_buffer, queue);
1521 vpfe_dev->cur_frm = vpfe_dev->next_frm;
1522 /* Remove buffer from the buffer queue */
1523 list_del(&vpfe_dev->cur_frm->queue);
1524 /* Mark state of the current frame to active */
1525 vpfe_dev->cur_frm->state = VIDEOBUF_ACTIVE;
1526 /* Initialize field_id and started member */
1527 vpfe_dev->field_id = 0;
1528 addr = videobuf_to_dma_contig(vpfe_dev->cur_frm);
1529
1530 /* Calculate field offset */
1531 vpfe_calculate_offsets(vpfe_dev);
1532
1533 if (vpfe_attach_irq(vpfe_dev) < 0) {
1534 v4l2_err(&vpfe_dev->v4l2_dev,
1535 "Error in attaching interrupt handle\n");
1536 ret = -EFAULT;
1537 goto unlock_out;
1538 }
1539 if (ccdc_dev->hw_ops.configure() < 0) {
1540 v4l2_err(&vpfe_dev->v4l2_dev,
1541 "Error in configuring ccdc\n");
1542 ret = -EINVAL;
1543 goto unlock_out;
1544 }
1545 ccdc_dev->hw_ops.setfbaddr((unsigned long)(addr));
1546 vpfe_start_ccdc_capture(vpfe_dev);
1547 mutex_unlock(&vpfe_dev->lock);
1548 return ret;
1549unlock_out:
1550 mutex_unlock(&vpfe_dev->lock);
1551streamoff:
1552 ret = videobuf_streamoff(&vpfe_dev->buffer_queue);
1553 return ret;
1554}
1555
1556static int vpfe_streamoff(struct file *file, void *priv,
1557 enum v4l2_buf_type buf_type)
1558{
1559 struct vpfe_device *vpfe_dev = video_drvdata(file);
1560 struct vpfe_fh *fh = file->private_data;
1561 struct vpfe_subdev_info *sdinfo;
1562 int ret = 0;
1563
1564 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_streamoff\n");
1565
1566 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf_type) {
1567 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1568 return -EINVAL;
1569 }
1570
1571 /* If io is allowed for this file handle, return error */
1572 if (!fh->io_allowed) {
1573 v4l2_err(&vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1574 return -EACCES;
1575 }
1576
1577 /* If streaming is not started, return error */
1578 if (!vpfe_dev->started) {
1579 v4l2_err(&vpfe_dev->v4l2_dev, "device started\n");
1580 return -EINVAL;
1581 }
1582
1583 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1584 if (ret)
1585 return ret;
1586
1587 vpfe_stop_ccdc_capture(vpfe_dev);
1588 vpfe_detach_irq(vpfe_dev);
1589
1590 sdinfo = vpfe_dev->current_subdev;
1591 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1592 video, s_stream, 0);
1593
1594 if (ret && (ret != -ENOIOCTLCMD))
1595 v4l2_err(&vpfe_dev->v4l2_dev, "stream off failed in subdev\n");
1596 ret = videobuf_streamoff(&vpfe_dev->buffer_queue);
1597 mutex_unlock(&vpfe_dev->lock);
1598 return ret;
1599}
1600
1601static int vpfe_cropcap(struct file *file, void *priv,
1602 struct v4l2_cropcap *crop)
1603{
1604 struct vpfe_device *vpfe_dev = video_drvdata(file);
1605
1606 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_cropcap\n");
1607
Hans Verkuilffc2a6b2016-07-03 08:28:43 -03001608 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001609 return -EINVAL;
Hans Verkuilffc2a6b2016-07-03 08:28:43 -03001610 /* If std_index is invalid, then just return (== 1:1 aspect) */
1611 if (vpfe_dev->std_index >= ARRAY_SIZE(vpfe_standards))
1612 return 0;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001613
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001614 crop->pixelaspect = vpfe_standards[vpfe_dev->std_index].pixelaspect;
1615 return 0;
1616}
1617
Hans Verkuilffc2a6b2016-07-03 08:28:43 -03001618static int vpfe_g_selection(struct file *file, void *priv,
1619 struct v4l2_selection *sel)
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001620{
1621 struct vpfe_device *vpfe_dev = video_drvdata(file);
1622
Hans Verkuilffc2a6b2016-07-03 08:28:43 -03001623 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_selection\n");
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001624
Hans Verkuilffc2a6b2016-07-03 08:28:43 -03001625 if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1626 return -EINVAL;
1627
1628 switch (sel->target) {
1629 case V4L2_SEL_TGT_CROP:
1630 sel->r = vpfe_dev->crop;
1631 break;
1632 case V4L2_SEL_TGT_CROP_DEFAULT:
1633 case V4L2_SEL_TGT_CROP_BOUNDS:
1634 sel->r.width = vpfe_standards[vpfe_dev->std_index].width;
1635 sel->r.height = vpfe_standards[vpfe_dev->std_index].height;
1636 break;
1637 default:
1638 return -EINVAL;
1639 }
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001640 return 0;
1641}
1642
Hans Verkuilffc2a6b2016-07-03 08:28:43 -03001643static int vpfe_s_selection(struct file *file, void *priv,
1644 struct v4l2_selection *sel)
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001645{
1646 struct vpfe_device *vpfe_dev = video_drvdata(file);
Hans Verkuilffc2a6b2016-07-03 08:28:43 -03001647 struct v4l2_rect rect = sel->r;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001648 int ret = 0;
1649
Hans Verkuilffc2a6b2016-07-03 08:28:43 -03001650 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_selection\n");
1651
1652 if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1653 sel->target != V4L2_SEL_TGT_CROP)
1654 return -EINVAL;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001655
1656 if (vpfe_dev->started) {
1657 /* make sure streaming is not started */
1658 v4l2_err(&vpfe_dev->v4l2_dev,
1659 "Cannot change crop when streaming is ON\n");
1660 return -EBUSY;
1661 }
1662
1663 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1664 if (ret)
1665 return ret;
1666
Lad, Prabhakar8b6faac2012-10-01 08:52:48 -03001667 if (rect.top < 0 || rect.left < 0) {
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001668 v4l2_err(&vpfe_dev->v4l2_dev,
1669 "doesn't support negative values for top & left\n");
1670 ret = -EINVAL;
1671 goto unlock_out;
1672 }
1673
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001674 /* adjust the width to 16 pixel boundary */
Lad, Prabhakar8b6faac2012-10-01 08:52:48 -03001675 rect.width = ((rect.width + 15) & ~0xf);
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001676
1677 /* make sure parameters are valid */
Lad, Prabhakar8b6faac2012-10-01 08:52:48 -03001678 if ((rect.left + rect.width >
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001679 vpfe_dev->std_info.active_pixels) ||
Lad, Prabhakar8b6faac2012-10-01 08:52:48 -03001680 (rect.top + rect.height >
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001681 vpfe_dev->std_info.active_lines)) {
Hans Verkuilffc2a6b2016-07-03 08:28:43 -03001682 v4l2_err(&vpfe_dev->v4l2_dev, "Error in S_SELECTION params\n");
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001683 ret = -EINVAL;
1684 goto unlock_out;
1685 }
Lad, Prabhakar8b6faac2012-10-01 08:52:48 -03001686 ccdc_dev->hw_ops.set_image_window(&rect);
1687 vpfe_dev->fmt.fmt.pix.width = rect.width;
1688 vpfe_dev->fmt.fmt.pix.height = rect.height;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001689 vpfe_dev->fmt.fmt.pix.bytesperline =
1690 ccdc_dev->hw_ops.get_line_length();
1691 vpfe_dev->fmt.fmt.pix.sizeimage =
1692 vpfe_dev->fmt.fmt.pix.bytesperline *
1693 vpfe_dev->fmt.fmt.pix.height;
Lad, Prabhakar8b6faac2012-10-01 08:52:48 -03001694 vpfe_dev->crop = rect;
Hans Verkuilffc2a6b2016-07-03 08:28:43 -03001695 sel->r = rect;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001696unlock_out:
1697 mutex_unlock(&vpfe_dev->lock);
1698 return ret;
1699}
1700
1701
1702static long vpfe_param_handler(struct file *file, void *priv,
Mauro Carvalho Chehab6d43be72013-03-26 08:04:52 -03001703 bool valid_prio, unsigned int cmd, void *param)
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001704{
1705 struct vpfe_device *vpfe_dev = video_drvdata(file);
1706 int ret = 0;
1707
Muralidharan Karicheri6a4f0622010-03-18 11:44:12 -03001708 v4l2_dbg(2, debug, &vpfe_dev->v4l2_dev, "vpfe_param_handler\n");
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001709
1710 if (vpfe_dev->started) {
1711 /* only allowed if streaming is not started */
Muralidharan Karicheri6a4f0622010-03-18 11:44:12 -03001712 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1713 "device already started\n");
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001714 return -EBUSY;
1715 }
1716
1717 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1718 if (ret)
1719 return ret;
1720
1721 switch (cmd) {
1722 case VPFE_CMD_S_CCDC_RAW_PARAMS:
1723 v4l2_warn(&vpfe_dev->v4l2_dev,
1724 "VPFE_CMD_S_CCDC_RAW_PARAMS: experimental ioctl\n");
Muralidharan Karicheri6a4f0622010-03-18 11:44:12 -03001725 if (ccdc_dev->hw_ops.set_params) {
1726 ret = ccdc_dev->hw_ops.set_params(param);
1727 if (ret) {
1728 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1729 "Error setting parameters in CCDC\n");
1730 goto unlock_out;
1731 }
Peter Senna Tschudin9a888ba2012-09-04 08:05:03 -03001732 ret = vpfe_get_ccdc_image_format(vpfe_dev,
1733 &vpfe_dev->fmt);
1734 if (ret < 0) {
Muralidharan Karicheri6a4f0622010-03-18 11:44:12 -03001735 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1736 "Invalid image format at CCDC\n");
1737 goto unlock_out;
1738 }
1739 } else {
1740 ret = -EINVAL;
1741 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1742 "VPFE_CMD_S_CCDC_RAW_PARAMS not supported\n");
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001743 }
1744 break;
1745 default:
Hans Verkuild1c754a2012-04-19 12:36:03 -03001746 ret = -ENOTTY;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001747 }
1748unlock_out:
1749 mutex_unlock(&vpfe_dev->lock);
1750 return ret;
1751}
1752
1753
1754/* vpfe capture ioctl operations */
1755static const struct v4l2_ioctl_ops vpfe_ioctl_ops = {
1756 .vidioc_querycap = vpfe_querycap,
1757 .vidioc_g_fmt_vid_cap = vpfe_g_fmt_vid_cap,
1758 .vidioc_enum_fmt_vid_cap = vpfe_enum_fmt_vid_cap,
1759 .vidioc_s_fmt_vid_cap = vpfe_s_fmt_vid_cap,
1760 .vidioc_try_fmt_vid_cap = vpfe_try_fmt_vid_cap,
1761 .vidioc_enum_input = vpfe_enum_input,
1762 .vidioc_g_input = vpfe_g_input,
1763 .vidioc_s_input = vpfe_s_input,
1764 .vidioc_querystd = vpfe_querystd,
1765 .vidioc_s_std = vpfe_s_std,
1766 .vidioc_g_std = vpfe_g_std,
1767 .vidioc_reqbufs = vpfe_reqbufs,
1768 .vidioc_querybuf = vpfe_querybuf,
1769 .vidioc_qbuf = vpfe_qbuf,
1770 .vidioc_dqbuf = vpfe_dqbuf,
1771 .vidioc_streamon = vpfe_streamon,
1772 .vidioc_streamoff = vpfe_streamoff,
1773 .vidioc_cropcap = vpfe_cropcap,
Hans Verkuilffc2a6b2016-07-03 08:28:43 -03001774 .vidioc_g_selection = vpfe_g_selection,
1775 .vidioc_s_selection = vpfe_s_selection,
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001776 .vidioc_default = vpfe_param_handler,
1777};
1778
1779static struct vpfe_device *vpfe_initialize(void)
1780{
1781 struct vpfe_device *vpfe_dev;
1782
1783 /* Default number of buffers should be 3 */
1784 if ((numbuffers > 0) &&
1785 (numbuffers < config_params.min_numbuffers))
1786 numbuffers = config_params.min_numbuffers;
1787
1788 /*
1789 * Set buffer size to min buffers size if invalid buffer size is
1790 * given
1791 */
1792 if (bufsize < config_params.min_bufsize)
1793 bufsize = config_params.min_bufsize;
1794
1795 config_params.numbuffers = numbuffers;
1796
1797 if (numbuffers)
1798 config_params.device_bufsize = bufsize;
1799
1800 /* Allocate memory for device objects */
1801 vpfe_dev = kzalloc(sizeof(*vpfe_dev), GFP_KERNEL);
1802
1803 return vpfe_dev;
1804}
1805
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001806/*
1807 * vpfe_probe : This function creates device entries by register
1808 * itself to the V4L2 driver and initializes fields of each
1809 * device objects
1810 */
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001811static int vpfe_probe(struct platform_device *pdev)
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001812{
1813 struct vpfe_subdev_info *sdinfo;
1814 struct vpfe_config *vpfe_cfg;
1815 struct resource *res1;
1816 struct vpfe_device *vpfe_dev;
1817 struct i2c_adapter *i2c_adap;
1818 struct video_device *vfd;
Markus Elfringefb74462016-10-12 05:30:28 -03001819 int ret, i, j;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001820 int num_subdevs = 0;
1821
1822 /* Get the pointer to the device object */
1823 vpfe_dev = vpfe_initialize();
1824
1825 if (!vpfe_dev) {
1826 v4l2_err(pdev->dev.driver,
1827 "Failed to allocate memory for vpfe_dev\n");
Markus Elfringefb74462016-10-12 05:30:28 -03001828 return -ENOMEM;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001829 }
1830
1831 vpfe_dev->pdev = &pdev->dev;
1832
Markus Elfringc580f292016-10-12 05:46:28 -03001833 if (!pdev->dev.platform_data) {
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001834 v4l2_err(pdev->dev.driver, "Unable to get vpfe config\n");
Muralidharan Karicheri51444ea2010-01-13 20:27:05 -03001835 ret = -ENODEV;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001836 goto probe_free_dev_mem;
1837 }
1838
1839 vpfe_cfg = pdev->dev.platform_data;
1840 vpfe_dev->cfg = vpfe_cfg;
Markus Elfringc580f292016-10-12 05:46:28 -03001841 if (!vpfe_cfg->ccdc || !vpfe_cfg->card_name || !vpfe_cfg->sub_devs) {
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001842 v4l2_err(pdev->dev.driver, "null ptr in vpfe_cfg\n");
1843 ret = -ENOENT;
1844 goto probe_free_dev_mem;
1845 }
1846
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001847 /* Allocate memory for ccdc configuration */
Markus Elfringe4c0cd02016-10-12 05:24:57 -03001848 ccdc_cfg = kmalloc(sizeof(*ccdc_cfg), GFP_KERNEL);
Markus Elfring11691f02016-10-12 05:22:47 -03001849 if (!ccdc_cfg)
Lad, Prabhakar39e219d2013-05-10 00:48:38 -03001850 goto probe_free_dev_mem;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001851
Dan Carpenter2b080c52010-04-07 06:21:05 -03001852 mutex_lock(&ccdc_lock);
1853
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001854 strncpy(ccdc_cfg->name, vpfe_cfg->ccdc, 32);
1855 /* Get VINT0 irq resource */
1856 res1 = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1857 if (!res1) {
1858 v4l2_err(pdev->dev.driver,
1859 "Unable to get interrupt for VINT0\n");
Muralidharan Karicheri51444ea2010-01-13 20:27:05 -03001860 ret = -ENODEV;
1861 goto probe_free_ccdc_cfg_mem;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001862 }
1863 vpfe_dev->ccdc_irq0 = res1->start;
1864
1865 /* Get VINT1 irq resource */
Muralidharan Karicheri51444ea2010-01-13 20:27:05 -03001866 res1 = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001867 if (!res1) {
1868 v4l2_err(pdev->dev.driver,
1869 "Unable to get interrupt for VINT1\n");
Muralidharan Karicheri51444ea2010-01-13 20:27:05 -03001870 ret = -ENODEV;
1871 goto probe_free_ccdc_cfg_mem;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001872 }
1873 vpfe_dev->ccdc_irq1 = res1->start;
1874
Michael Opdenackerd8c279a2013-09-08 23:30:11 -03001875 ret = request_irq(vpfe_dev->ccdc_irq0, vpfe_isr, 0,
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001876 "vpfe_capture0", vpfe_dev);
1877
1878 if (0 != ret) {
1879 v4l2_err(pdev->dev.driver, "Unable to request interrupt\n");
Muralidharan Karicheri51444ea2010-01-13 20:27:05 -03001880 goto probe_free_ccdc_cfg_mem;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001881 }
1882
Lad, Prabhakar03c278f2015-03-10 14:53:05 -03001883 vfd = &vpfe_dev->video_dev;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001884 /* Initialize field of video device */
Lad, Prabhakar03c278f2015-03-10 14:53:05 -03001885 vfd->release = video_device_release_empty;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001886 vfd->fops = &vpfe_fops;
1887 vfd->ioctl_ops = &vpfe_ioctl_ops;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001888 vfd->tvnorms = 0;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001889 vfd->v4l2_dev = &vpfe_dev->v4l2_dev;
1890 snprintf(vfd->name, sizeof(vfd->name),
1891 "%s_V%d.%d.%d",
1892 CAPTURE_DRV_NAME,
1893 (VPFE_CAPTURE_VERSION_CODE >> 16) & 0xff,
1894 (VPFE_CAPTURE_VERSION_CODE >> 8) & 0xff,
1895 (VPFE_CAPTURE_VERSION_CODE) & 0xff);
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001896
1897 ret = v4l2_device_register(&pdev->dev, &vpfe_dev->v4l2_dev);
1898 if (ret) {
1899 v4l2_err(pdev->dev.driver,
1900 "Unable to register v4l2 device.\n");
Lad, Prabhakar03c278f2015-03-10 14:53:05 -03001901 goto probe_out_release_irq;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001902 }
1903 v4l2_info(&vpfe_dev->v4l2_dev, "v4l2 device registered\n");
1904 spin_lock_init(&vpfe_dev->irqlock);
1905 spin_lock_init(&vpfe_dev->dma_queue_lock);
1906 mutex_init(&vpfe_dev->lock);
1907
1908 /* Initialize field of the device objects */
1909 vpfe_dev->numbuffers = config_params.numbuffers;
1910
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001911 /* register video device */
1912 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1913 "trying to register vpfe device.\n");
1914 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
Mauro Carvalho Chehab212bdba2014-08-22 06:38:14 -05001915 "video_dev=%p\n", &vpfe_dev->video_dev);
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001916 vpfe_dev->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Lad, Prabhakar03c278f2015-03-10 14:53:05 -03001917 ret = video_register_device(&vpfe_dev->video_dev,
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001918 VFL_TYPE_GRABBER, -1);
1919
1920 if (ret) {
1921 v4l2_err(pdev->dev.driver,
1922 "Unable to register video device.\n");
1923 goto probe_out_v4l2_unregister;
1924 }
1925
1926 v4l2_info(&vpfe_dev->v4l2_dev, "video device registered\n");
1927 /* set the driver data in platform device */
1928 platform_set_drvdata(pdev, vpfe_dev);
1929 /* set driver private data */
Lad, Prabhakar03c278f2015-03-10 14:53:05 -03001930 video_set_drvdata(&vpfe_dev->video_dev, vpfe_dev);
Vaibhav Hiremath14cbaaf2009-11-09 09:14:16 -03001931 i2c_adap = i2c_get_adapter(vpfe_cfg->i2c_adapter_id);
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001932 num_subdevs = vpfe_cfg->num_subdevs;
Markus Elfringed011a22016-10-12 05:20:02 -03001933 vpfe_dev->sd = kmalloc_array(num_subdevs,
1934 sizeof(*vpfe_dev->sd),
1935 GFP_KERNEL);
Markus Elfringc580f292016-10-12 05:46:28 -03001936 if (!vpfe_dev->sd) {
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001937 ret = -ENOMEM;
1938 goto probe_out_video_unregister;
1939 }
1940
1941 for (i = 0; i < num_subdevs; i++) {
1942 struct v4l2_input *inps;
1943
1944 sdinfo = &vpfe_cfg->sub_devs[i];
1945
1946 /* Load up the subdevice */
1947 vpfe_dev->sd[i] =
1948 v4l2_i2c_new_subdev_board(&vpfe_dev->v4l2_dev,
1949 i2c_adap,
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001950 &sdinfo->board_info,
1951 NULL);
1952 if (vpfe_dev->sd[i]) {
1953 v4l2_info(&vpfe_dev->v4l2_dev,
1954 "v4l2 sub device %s registered\n",
1955 sdinfo->name);
1956 vpfe_dev->sd[i]->grp_id = sdinfo->grp_id;
1957 /* update tvnorms from the sub devices */
1958 for (j = 0; j < sdinfo->num_inputs; j++) {
1959 inps = &sdinfo->inputs[j];
1960 vfd->tvnorms |= inps->std;
1961 }
1962 } else {
1963 v4l2_info(&vpfe_dev->v4l2_dev,
1964 "v4l2 sub device %s register fails\n",
1965 sdinfo->name);
1966 goto probe_sd_out;
1967 }
1968 }
1969
1970 /* set first sub device as current one */
1971 vpfe_dev->current_subdev = &vpfe_cfg->sub_devs[0];
Hans Verkuil6f55dba2013-03-04 05:48:43 -03001972 vpfe_dev->v4l2_dev.ctrl_handler = vpfe_dev->sd[0]->ctrl_handler;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001973
1974 /* We have at least one sub device to work with */
1975 mutex_unlock(&ccdc_lock);
1976 return 0;
1977
1978probe_sd_out:
1979 kfree(vpfe_dev->sd);
1980probe_out_video_unregister:
Lad, Prabhakar03c278f2015-03-10 14:53:05 -03001981 video_unregister_device(&vpfe_dev->video_dev);
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001982probe_out_v4l2_unregister:
1983 v4l2_device_unregister(&vpfe_dev->v4l2_dev);
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001984probe_out_release_irq:
1985 free_irq(vpfe_dev->ccdc_irq0, vpfe_dev);
Muralidharan Karicheri51444ea2010-01-13 20:27:05 -03001986probe_free_ccdc_cfg_mem:
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001987 kfree(ccdc_cfg);
Murali Karicheriab51bec2010-03-01 19:54:02 -03001988 mutex_unlock(&ccdc_lock);
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001989probe_free_dev_mem:
1990 kfree(vpfe_dev);
1991 return ret;
1992}
1993
1994/*
1995 * vpfe_remove : It un-register device from V4L2 driver
1996 */
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001997static int vpfe_remove(struct platform_device *pdev)
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001998{
1999 struct vpfe_device *vpfe_dev = platform_get_drvdata(pdev);
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03002000
2001 v4l2_info(pdev->dev.driver, "vpfe_remove\n");
2002
2003 free_irq(vpfe_dev->ccdc_irq0, vpfe_dev);
2004 kfree(vpfe_dev->sd);
2005 v4l2_device_unregister(&vpfe_dev->v4l2_dev);
Lad, Prabhakar03c278f2015-03-10 14:53:05 -03002006 video_unregister_device(&vpfe_dev->video_dev);
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03002007 kfree(vpfe_dev);
2008 kfree(ccdc_cfg);
2009 return 0;
2010}
2011
Vaibhav Hiremathaa2dc902010-03-27 09:37:16 -03002012static int vpfe_suspend(struct device *dev)
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03002013{
Vaibhav Hiremathaa2dc902010-03-27 09:37:16 -03002014 return 0;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03002015}
2016
Vaibhav Hiremathaa2dc902010-03-27 09:37:16 -03002017static int vpfe_resume(struct device *dev)
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03002018{
Vaibhav Hiremathaa2dc902010-03-27 09:37:16 -03002019 return 0;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03002020}
2021
Alexey Dobriyan47145212009-12-14 18:00:08 -08002022static const struct dev_pm_ops vpfe_dev_pm_ops = {
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03002023 .suspend = vpfe_suspend,
2024 .resume = vpfe_resume,
2025};
2026
Lad, Prabhakara1b3a6c2012-08-14 01:23:09 -03002027static struct platform_driver vpfe_driver = {
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03002028 .driver = {
2029 .name = CAPTURE_DRV_NAME,
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03002030 .pm = &vpfe_dev_pm_ops,
2031 },
2032 .probe = vpfe_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08002033 .remove = vpfe_remove,
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03002034};
2035
Axel Lin1d6629b2012-01-10 03:21:49 -03002036module_platform_driver(vpfe_driver);