blob: 1acca83426ea002442fcce943fb9bc9652fe4db9 [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;
Markus Elfring2a0de2c2016-10-12 05:50:54 -0300387 int i, ret;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300388
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{
Markus Elfring2a0de2c2016-10-12 05:50:54 -0300456 int ret;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300457
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 */
Markus Elfringb8990652016-10-12 09:54:21 -0300618 if (field == V4L2_FIELD_SEQ_TB)
Vaibhav Hiremath844cc0d2010-03-27 09:37:23 -0300619 vpfe_schedule_bottom_field(vpfe_dev);
Vaibhav Hiremath085b54a2010-03-27 09:37:07 -0300620 goto clear_intr;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300621 }
622 /*
623 * if one field is just being captured configure
624 * the next frame get the next frame from the empty
625 * queue if no frame is available hold on to the
626 * current buffer
627 */
628 spin_lock(&vpfe_dev->dma_queue_lock);
629 if (!list_empty(&vpfe_dev->dma_queue) &&
630 vpfe_dev->cur_frm == vpfe_dev->next_frm)
631 vpfe_schedule_next_buffer(vpfe_dev);
632 spin_unlock(&vpfe_dev->dma_queue_lock);
633 } else if (fid == 0) {
634 /*
635 * out of sync. Recover from any hardware out-of-sync.
636 * May loose one frame
637 */
638 vpfe_dev->field_id = fid;
639 }
Vaibhav Hiremath085b54a2010-03-27 09:37:07 -0300640clear_intr:
641 if (vpfe_dev->cfg->clr_intr)
642 vpfe_dev->cfg->clr_intr(irq);
643
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300644 return IRQ_HANDLED;
645}
646
647/* vdint1_isr - isr handler for VINT1 interrupt */
648static irqreturn_t vdint1_isr(int irq, void *dev_id)
649{
650 struct vpfe_device *vpfe_dev = dev_id;
651
652 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "\nInside vdint1_isr...\n");
653
654 /* if streaming not started, don't do anything */
Vaibhav Hiremath085b54a2010-03-27 09:37:07 -0300655 if (!vpfe_dev->started) {
656 if (vpfe_dev->cfg->clr_intr)
657 vpfe_dev->cfg->clr_intr(irq);
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300658 return IRQ_HANDLED;
Vaibhav Hiremath085b54a2010-03-27 09:37:07 -0300659 }
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300660
661 spin_lock(&vpfe_dev->dma_queue_lock);
662 if ((vpfe_dev->fmt.fmt.pix.field == V4L2_FIELD_NONE) &&
663 !list_empty(&vpfe_dev->dma_queue) &&
664 vpfe_dev->cur_frm == vpfe_dev->next_frm)
665 vpfe_schedule_next_buffer(vpfe_dev);
666 spin_unlock(&vpfe_dev->dma_queue_lock);
Vaibhav Hiremath085b54a2010-03-27 09:37:07 -0300667
668 if (vpfe_dev->cfg->clr_intr)
669 vpfe_dev->cfg->clr_intr(irq);
670
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300671 return IRQ_HANDLED;
672}
673
674static void vpfe_detach_irq(struct vpfe_device *vpfe_dev)
675{
676 enum ccdc_frmfmt frame_format;
677
678 frame_format = ccdc_dev->hw_ops.get_frame_format();
679 if (frame_format == CCDC_FRMFMT_PROGRESSIVE)
Vaibhav Hiremath1ead6962009-11-09 09:15:07 -0300680 free_irq(vpfe_dev->ccdc_irq1, vpfe_dev);
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300681}
682
683static int vpfe_attach_irq(struct vpfe_device *vpfe_dev)
684{
685 enum ccdc_frmfmt frame_format;
686
687 frame_format = ccdc_dev->hw_ops.get_frame_format();
688 if (frame_format == CCDC_FRMFMT_PROGRESSIVE) {
689 return request_irq(vpfe_dev->ccdc_irq1, vdint1_isr,
Michael Opdenackerd8c279a2013-09-08 23:30:11 -0300690 0, "vpfe_capture1",
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300691 vpfe_dev);
692 }
693 return 0;
694}
695
696/* vpfe_stop_ccdc_capture: stop streaming in ccdc/isif */
697static void vpfe_stop_ccdc_capture(struct vpfe_device *vpfe_dev)
698{
699 vpfe_dev->started = 0;
700 ccdc_dev->hw_ops.enable(0);
701 if (ccdc_dev->hw_ops.enable_out_to_sdram)
702 ccdc_dev->hw_ops.enable_out_to_sdram(0);
703}
704
705/*
706 * vpfe_release : This function deletes buffer queue, frees the
707 * buffers and the vpfe file handle
708 */
709static int vpfe_release(struct file *file)
710{
711 struct vpfe_device *vpfe_dev = video_drvdata(file);
712 struct vpfe_fh *fh = file->private_data;
713 struct vpfe_subdev_info *sdinfo;
714 int ret;
715
716 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_release\n");
717
718 /* Get the device lock */
719 mutex_lock(&vpfe_dev->lock);
720 /* if this instance is doing IO */
721 if (fh->io_allowed) {
722 if (vpfe_dev->started) {
723 sdinfo = vpfe_dev->current_subdev;
724 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev,
725 sdinfo->grp_id,
726 video, s_stream, 0);
727 if (ret && (ret != -ENOIOCTLCMD))
728 v4l2_err(&vpfe_dev->v4l2_dev,
729 "stream off failed in subdev\n");
730 vpfe_stop_ccdc_capture(vpfe_dev);
731 vpfe_detach_irq(vpfe_dev);
732 videobuf_streamoff(&vpfe_dev->buffer_queue);
733 }
734 vpfe_dev->io_usrs = 0;
735 vpfe_dev->numbuffers = config_params.numbuffers;
Lad, Prabhakarc1d1e402014-03-23 02:44:11 -0300736 videobuf_stop(&vpfe_dev->buffer_queue);
737 videobuf_mmap_free(&vpfe_dev->buffer_queue);
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300738 }
739
740 /* Decrement device usrs counter */
741 vpfe_dev->usrs--;
Lad, Prabhakar3bdaa382014-03-22 08:39:24 -0300742 v4l2_fh_del(&fh->fh);
743 v4l2_fh_exit(&fh->fh);
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300744 /* If this is the last file handle */
745 if (!vpfe_dev->usrs) {
746 vpfe_dev->initialized = 0;
747 if (ccdc_dev->hw_ops.close)
748 ccdc_dev->hw_ops.close(vpfe_dev->pdev);
749 module_put(ccdc_dev->owner);
750 }
751 mutex_unlock(&vpfe_dev->lock);
752 file->private_data = NULL;
753 /* Free memory allocated to file handle object */
754 kfree(fh);
755 return 0;
756}
757
758/*
759 * vpfe_mmap : It is used to map kernel space buffers
760 * into user spaces
761 */
762static int vpfe_mmap(struct file *file, struct vm_area_struct *vma)
763{
764 /* Get the device object and file handle object */
765 struct vpfe_device *vpfe_dev = video_drvdata(file);
766
767 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_mmap\n");
768
769 return videobuf_mmap_mapper(&vpfe_dev->buffer_queue, vma);
770}
771
772/*
773 * vpfe_poll: It is used for select/poll system call
774 */
775static unsigned int vpfe_poll(struct file *file, poll_table *wait)
776{
777 struct vpfe_device *vpfe_dev = video_drvdata(file);
778
779 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_poll\n");
780
781 if (vpfe_dev->started)
782 return videobuf_poll_stream(file,
783 &vpfe_dev->buffer_queue, wait);
784 return 0;
785}
786
787/* vpfe capture driver file operations */
788static const struct v4l2_file_operations vpfe_fops = {
789 .owner = THIS_MODULE,
790 .open = vpfe_open,
791 .release = vpfe_release,
792 .unlocked_ioctl = video_ioctl2,
793 .mmap = vpfe_mmap,
794 .poll = vpfe_poll
795};
796
797/*
798 * vpfe_check_format()
799 * This function adjust the input pixel format as per hardware
800 * capabilities and update the same in pixfmt.
801 * Following algorithm used :-
802 *
803 * If given pixformat is not in the vpfe list of pix formats or not
804 * supported by the hardware, current value of pixformat in the device
805 * is used
806 * If given field is not supported, then current field is used. If field
807 * is different from current, then it is matched with that from sub device.
808 * Minimum height is 2 lines for interlaced or tb field and 1 line for
809 * progressive. Maximum height is clamped to active active lines of scan
810 * Minimum width is 32 bytes in memory and width is clamped to active
811 * pixels of scan.
812 * bytesperline is a multiple of 32.
813 */
814static const struct vpfe_pixel_format *
815 vpfe_check_format(struct vpfe_device *vpfe_dev,
816 struct v4l2_pix_format *pixfmt)
817{
818 u32 min_height = 1, min_width = 32, max_width, max_height;
819 const struct vpfe_pixel_format *vpfe_pix_fmt;
820 u32 pix;
821 int temp, found;
822
823 vpfe_pix_fmt = vpfe_lookup_pix_format(pixfmt->pixelformat);
Markus Elfringc580f292016-10-12 05:46:28 -0300824 if (!vpfe_pix_fmt) {
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300825 /*
826 * use current pixel format in the vpfe device. We
827 * will find this pix format in the table
828 */
829 pixfmt->pixelformat = vpfe_dev->fmt.fmt.pix.pixelformat;
830 vpfe_pix_fmt = vpfe_lookup_pix_format(pixfmt->pixelformat);
831 }
832
833 /* check if hw supports it */
834 temp = 0;
835 found = 0;
836 while (ccdc_dev->hw_ops.enum_pix(&pix, temp) >= 0) {
837 if (vpfe_pix_fmt->fmtdesc.pixelformat == pix) {
838 found = 1;
839 break;
840 }
841 temp++;
842 }
843
844 if (!found) {
845 /* use current pixel format */
846 pixfmt->pixelformat = vpfe_dev->fmt.fmt.pix.pixelformat;
847 /*
848 * Since this is currently used in the vpfe device, we
849 * will find this pix format in the table
850 */
851 vpfe_pix_fmt = vpfe_lookup_pix_format(pixfmt->pixelformat);
852 }
853
854 /* check what field format is supported */
855 if (pixfmt->field == V4L2_FIELD_ANY) {
856 /* if field is any, use current value as default */
857 pixfmt->field = vpfe_dev->fmt.fmt.pix.field;
858 }
859
860 /*
861 * if field is not same as current field in the vpfe device
862 * try matching the field with the sub device field
863 */
864 if (vpfe_dev->fmt.fmt.pix.field != pixfmt->field) {
865 /*
866 * If field value is not in the supported fields, use current
867 * field used in the device as default
868 */
869 switch (pixfmt->field) {
870 case V4L2_FIELD_INTERLACED:
871 case V4L2_FIELD_SEQ_TB:
872 /* if sub device is supporting progressive, use that */
873 if (!vpfe_dev->std_info.frame_format)
874 pixfmt->field = V4L2_FIELD_NONE;
875 break;
876 case V4L2_FIELD_NONE:
877 if (vpfe_dev->std_info.frame_format)
878 pixfmt->field = V4L2_FIELD_INTERLACED;
879 break;
880
881 default:
882 /* use current field as default */
883 pixfmt->field = vpfe_dev->fmt.fmt.pix.field;
884 break;
885 }
886 }
887
888 /* Now adjust image resolutions supported */
889 if (pixfmt->field == V4L2_FIELD_INTERLACED ||
890 pixfmt->field == V4L2_FIELD_SEQ_TB)
891 min_height = 2;
892
893 max_width = vpfe_dev->std_info.active_pixels;
894 max_height = vpfe_dev->std_info.active_lines;
895 min_width /= vpfe_pix_fmt->bpp;
896
897 v4l2_info(&vpfe_dev->v4l2_dev, "width = %d, height = %d, bpp = %d\n",
898 pixfmt->width, pixfmt->height, vpfe_pix_fmt->bpp);
899
900 pixfmt->width = clamp((pixfmt->width), min_width, max_width);
901 pixfmt->height = clamp((pixfmt->height), min_height, max_height);
902
903 /* If interlaced, adjust height to be a multiple of 2 */
904 if (pixfmt->field == V4L2_FIELD_INTERLACED)
905 pixfmt->height &= (~1);
906 /*
907 * recalculate bytesperline and sizeimage since width
908 * and height might have changed
909 */
910 pixfmt->bytesperline = (((pixfmt->width * vpfe_pix_fmt->bpp) + 31)
911 & ~31);
912 if (pixfmt->pixelformat == V4L2_PIX_FMT_NV12)
913 pixfmt->sizeimage =
914 pixfmt->bytesperline * pixfmt->height +
915 ((pixfmt->bytesperline * pixfmt->height) >> 1);
916 else
917 pixfmt->sizeimage = pixfmt->bytesperline * pixfmt->height;
918
Mauro Carvalho Chehabded026e2016-10-18 17:44:08 -0200919 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 -0300920 pixfmt->width, pixfmt->height, vpfe_pix_fmt->bpp,
921 pixfmt->bytesperline, pixfmt->sizeimage);
922 return vpfe_pix_fmt;
923}
924
925static int vpfe_querycap(struct file *file, void *priv,
926 struct v4l2_capability *cap)
927{
928 struct vpfe_device *vpfe_dev = video_drvdata(file);
929
930 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querycap\n");
931
Hans Verkuil8c17e5e2014-11-24 06:37:26 -0300932 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
933 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300934 strlcpy(cap->driver, CAPTURE_DRV_NAME, sizeof(cap->driver));
935 strlcpy(cap->bus_info, "VPFE", sizeof(cap->bus_info));
936 strlcpy(cap->card, vpfe_dev->cfg->card_name, sizeof(cap->card));
937 return 0;
938}
939
940static int vpfe_g_fmt_vid_cap(struct file *file, void *priv,
941 struct v4l2_format *fmt)
942{
943 struct vpfe_device *vpfe_dev = video_drvdata(file);
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300944
945 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_fmt_vid_cap\n");
946 /* Fill in the information about format */
947 *fmt = vpfe_dev->fmt;
Mauro Carvalho Chehabb80cefb2014-09-03 15:39:46 -0300948 return 0;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300949}
950
951static int vpfe_enum_fmt_vid_cap(struct file *file, void *priv,
952 struct v4l2_fmtdesc *fmt)
953{
954 struct vpfe_device *vpfe_dev = video_drvdata(file);
955 const struct vpfe_pixel_format *pix_fmt;
956 int temp_index;
957 u32 pix;
958
959 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_enum_fmt_vid_cap\n");
960
961 if (ccdc_dev->hw_ops.enum_pix(&pix, fmt->index) < 0)
962 return -EINVAL;
963
964 /* Fill in the information about format */
965 pix_fmt = vpfe_lookup_pix_format(pix);
Markus Elfringc580f292016-10-12 05:46:28 -0300966 if (pix_fmt) {
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300967 temp_index = fmt->index;
968 *fmt = pix_fmt->fmtdesc;
969 fmt->index = temp_index;
970 return 0;
971 }
972 return -EINVAL;
973}
974
975static int vpfe_s_fmt_vid_cap(struct file *file, void *priv,
976 struct v4l2_format *fmt)
977{
978 struct vpfe_device *vpfe_dev = video_drvdata(file);
979 const struct vpfe_pixel_format *pix_fmts;
Markus Elfring2a0de2c2016-10-12 05:50:54 -0300980 int ret;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300981
982 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_fmt_vid_cap\n");
983
984 /* If streaming is started, return error */
985 if (vpfe_dev->started) {
986 v4l2_err(&vpfe_dev->v4l2_dev, "Streaming is started\n");
987 return -EBUSY;
988 }
989
990 /* Check for valid frame format */
991 pix_fmts = vpfe_check_format(vpfe_dev, &fmt->fmt.pix);
Markus Elfringc580f292016-10-12 05:46:28 -0300992 if (!pix_fmts)
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -0300993 return -EINVAL;
994
995 /* store the pixel format in the device object */
996 ret = mutex_lock_interruptible(&vpfe_dev->lock);
997 if (ret)
998 return ret;
999
1000 /* First detach any IRQ if currently attached */
1001 vpfe_detach_irq(vpfe_dev);
1002 vpfe_dev->fmt = *fmt;
1003 /* set image capture parameters in the ccdc */
1004 ret = vpfe_config_ccdc_image_format(vpfe_dev);
1005 mutex_unlock(&vpfe_dev->lock);
1006 return ret;
1007}
1008
1009static int vpfe_try_fmt_vid_cap(struct file *file, void *priv,
1010 struct v4l2_format *f)
1011{
1012 struct vpfe_device *vpfe_dev = video_drvdata(file);
1013 const struct vpfe_pixel_format *pix_fmts;
1014
1015 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_try_fmt_vid_cap\n");
1016
1017 pix_fmts = vpfe_check_format(vpfe_dev, &f->fmt.pix);
Markus Elfringc580f292016-10-12 05:46:28 -03001018 if (!pix_fmts)
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001019 return -EINVAL;
1020 return 0;
1021}
1022
1023/*
1024 * vpfe_get_subdev_input_index - Get subdev index and subdev input index for a
1025 * given app input index
1026 */
1027static int vpfe_get_subdev_input_index(struct vpfe_device *vpfe_dev,
1028 int *subdev_index,
1029 int *subdev_input_index,
1030 int app_input_index)
1031{
1032 struct vpfe_config *cfg = vpfe_dev->cfg;
1033 struct vpfe_subdev_info *sdinfo;
1034 int i, j = 0;
1035
1036 for (i = 0; i < cfg->num_subdevs; i++) {
1037 sdinfo = &cfg->sub_devs[i];
1038 if (app_input_index < (j + sdinfo->num_inputs)) {
1039 *subdev_index = i;
1040 *subdev_input_index = app_input_index - j;
1041 return 0;
1042 }
1043 j += sdinfo->num_inputs;
1044 }
1045 return -EINVAL;
1046}
1047
1048/*
1049 * vpfe_get_app_input - Get app input index for a given subdev input index
1050 * driver stores the input index of the current sub device and translate it
1051 * when application request the current input
1052 */
1053static int vpfe_get_app_input_index(struct vpfe_device *vpfe_dev,
1054 int *app_input_index)
1055{
1056 struct vpfe_config *cfg = vpfe_dev->cfg;
1057 struct vpfe_subdev_info *sdinfo;
1058 int i, j = 0;
1059
1060 for (i = 0; i < cfg->num_subdevs; i++) {
1061 sdinfo = &cfg->sub_devs[i];
1062 if (!strcmp(sdinfo->name, vpfe_dev->current_subdev->name)) {
1063 if (vpfe_dev->current_input >= sdinfo->num_inputs)
1064 return -1;
1065 *app_input_index = j + vpfe_dev->current_input;
1066 return 0;
1067 }
1068 j += sdinfo->num_inputs;
1069 }
1070 return -EINVAL;
1071}
1072
1073static int vpfe_enum_input(struct file *file, void *priv,
1074 struct v4l2_input *inp)
1075{
1076 struct vpfe_device *vpfe_dev = video_drvdata(file);
1077 struct vpfe_subdev_info *sdinfo;
1078 int subdev, index ;
1079
1080 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_enum_input\n");
1081
1082 if (vpfe_get_subdev_input_index(vpfe_dev,
1083 &subdev,
1084 &index,
1085 inp->index) < 0) {
Mauro Carvalho Chehabded026e2016-10-18 17:44:08 -02001086 v4l2_err(&vpfe_dev->v4l2_dev, "input information not found for the subdev\n");
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001087 return -EINVAL;
1088 }
1089 sdinfo = &vpfe_dev->cfg->sub_devs[subdev];
1090 memcpy(inp, &sdinfo->inputs[index], sizeof(struct v4l2_input));
1091 return 0;
1092}
1093
1094static int vpfe_g_input(struct file *file, void *priv, unsigned int *index)
1095{
1096 struct vpfe_device *vpfe_dev = video_drvdata(file);
1097
1098 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_input\n");
1099
1100 return vpfe_get_app_input_index(vpfe_dev, index);
1101}
1102
1103
1104static int vpfe_s_input(struct file *file, void *priv, unsigned int index)
1105{
1106 struct vpfe_device *vpfe_dev = video_drvdata(file);
Hans Verkuil6f55dba2013-03-04 05:48:43 -03001107 struct v4l2_subdev *sd;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001108 struct vpfe_subdev_info *sdinfo;
1109 int subdev_index, inp_index;
1110 struct vpfe_route *route;
Markus Elfring19d46952016-10-12 06:22:23 -03001111 u32 input, output;
Markus Elfring2a0de2c2016-10-12 05:50:54 -03001112 int ret;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001113
1114 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_input\n");
1115
1116 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1117 if (ret)
1118 return ret;
1119
1120 /*
1121 * If streaming is started return device busy
1122 * error
1123 */
1124 if (vpfe_dev->started) {
1125 v4l2_err(&vpfe_dev->v4l2_dev, "Streaming is on\n");
1126 ret = -EBUSY;
1127 goto unlock_out;
1128 }
Peter Senna Tschudin9a888ba2012-09-04 08:05:03 -03001129 ret = vpfe_get_subdev_input_index(vpfe_dev,
1130 &subdev_index,
1131 &inp_index,
1132 index);
1133 if (ret < 0) {
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001134 v4l2_err(&vpfe_dev->v4l2_dev, "invalid input index\n");
1135 goto unlock_out;
1136 }
1137
1138 sdinfo = &vpfe_dev->cfg->sub_devs[subdev_index];
Hans Verkuil6f55dba2013-03-04 05:48:43 -03001139 sd = vpfe_dev->sd[subdev_index];
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001140 route = &sdinfo->routes[inp_index];
1141 if (route && sdinfo->can_route) {
1142 input = route->input;
1143 output = route->output;
Markus Elfring19d46952016-10-12 06:22:23 -03001144 } else {
1145 input = 0;
1146 output = 0;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001147 }
1148
Hans Verkuil6f55dba2013-03-04 05:48:43 -03001149 if (sd)
1150 ret = v4l2_subdev_call(sd, video, s_routing, input, output, 0);
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001151
1152 if (ret) {
1153 v4l2_err(&vpfe_dev->v4l2_dev,
1154 "vpfe_doioctl:error in setting input in decoder\n");
1155 ret = -EINVAL;
1156 goto unlock_out;
1157 }
1158 vpfe_dev->current_subdev = sdinfo;
Hans Verkuil6f55dba2013-03-04 05:48:43 -03001159 if (sd)
1160 vpfe_dev->v4l2_dev.ctrl_handler = sd->ctrl_handler;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001161 vpfe_dev->current_input = index;
1162 vpfe_dev->std_index = 0;
1163
1164 /* set the bus/interface parameter for the sub device in ccdc */
1165 ret = ccdc_dev->hw_ops.set_hw_if_params(&sdinfo->ccdc_if_params);
1166 if (ret)
1167 goto unlock_out;
1168
1169 /* set the default image parameters in the device */
1170 ret = vpfe_config_image_format(vpfe_dev,
Hans Verkuil314527a2013-03-15 06:10:40 -03001171 vpfe_standards[vpfe_dev->std_index].std_id);
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001172unlock_out:
1173 mutex_unlock(&vpfe_dev->lock);
1174 return ret;
1175}
1176
1177static int vpfe_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
1178{
1179 struct vpfe_device *vpfe_dev = video_drvdata(file);
1180 struct vpfe_subdev_info *sdinfo;
Markus Elfring2a0de2c2016-10-12 05:50:54 -03001181 int ret;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001182
1183 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querystd\n");
1184
1185 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1186 sdinfo = vpfe_dev->current_subdev;
1187 if (ret)
1188 return ret;
1189 /* Call querystd function of decoder device */
1190 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1191 video, querystd, std_id);
1192 mutex_unlock(&vpfe_dev->lock);
1193 return ret;
1194}
1195
Hans Verkuil314527a2013-03-15 06:10:40 -03001196static int vpfe_s_std(struct file *file, void *priv, v4l2_std_id std_id)
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001197{
1198 struct vpfe_device *vpfe_dev = video_drvdata(file);
1199 struct vpfe_subdev_info *sdinfo;
Markus Elfring2a0de2c2016-10-12 05:50:54 -03001200 int ret;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001201
1202 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_std\n");
1203
1204 /* Call decoder driver function to set the standard */
1205 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1206 if (ret)
1207 return ret;
1208
1209 sdinfo = vpfe_dev->current_subdev;
1210 /* If streaming is started, return device busy error */
1211 if (vpfe_dev->started) {
1212 v4l2_err(&vpfe_dev->v4l2_dev, "streaming is started\n");
1213 ret = -EBUSY;
1214 goto unlock_out;
1215 }
1216
1217 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
Laurent Pinchart8774bed2014-04-28 16:53:01 -03001218 video, s_std, std_id);
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001219 if (ret < 0) {
1220 v4l2_err(&vpfe_dev->v4l2_dev, "Failed to set standard\n");
1221 goto unlock_out;
1222 }
1223 ret = vpfe_config_image_format(vpfe_dev, std_id);
1224
1225unlock_out:
1226 mutex_unlock(&vpfe_dev->lock);
1227 return ret;
1228}
1229
1230static int vpfe_g_std(struct file *file, void *priv, v4l2_std_id *std_id)
1231{
1232 struct vpfe_device *vpfe_dev = video_drvdata(file);
1233
1234 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_std\n");
1235
1236 *std_id = vpfe_standards[vpfe_dev->std_index].std_id;
1237 return 0;
1238}
1239/*
1240 * Videobuf operations
1241 */
1242static int vpfe_videobuf_setup(struct videobuf_queue *vq,
1243 unsigned int *count,
1244 unsigned int *size)
1245{
1246 struct vpfe_fh *fh = vq->priv_data;
1247 struct vpfe_device *vpfe_dev = fh->vpfe_dev;
1248
1249 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buffer_setup\n");
Vaibhav Hiremath844cc0d2010-03-27 09:37:23 -03001250 *size = vpfe_dev->fmt.fmt.pix.sizeimage;
1251 if (vpfe_dev->memory == V4L2_MEMORY_MMAP &&
1252 vpfe_dev->fmt.fmt.pix.sizeimage > config_params.device_bufsize)
1253 *size = config_params.device_bufsize;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001254
1255 if (*count < config_params.min_numbuffers)
1256 *count = config_params.min_numbuffers;
1257 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1258 "count=%d, size=%d\n", *count, *size);
1259 return 0;
1260}
1261
1262static int vpfe_videobuf_prepare(struct videobuf_queue *vq,
1263 struct videobuf_buffer *vb,
1264 enum v4l2_field field)
1265{
1266 struct vpfe_fh *fh = vq->priv_data;
1267 struct vpfe_device *vpfe_dev = fh->vpfe_dev;
Vaibhav Hiremath844cc0d2010-03-27 09:37:23 -03001268 unsigned long addr;
1269 int ret;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001270
1271 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buffer_prepare\n");
1272
1273 /* If buffer is not initialized, initialize it */
1274 if (VIDEOBUF_NEEDS_INIT == vb->state) {
1275 vb->width = vpfe_dev->fmt.fmt.pix.width;
1276 vb->height = vpfe_dev->fmt.fmt.pix.height;
1277 vb->size = vpfe_dev->fmt.fmt.pix.sizeimage;
1278 vb->field = field;
Vaibhav Hiremath844cc0d2010-03-27 09:37:23 -03001279
Joe Perchesb1dc6142010-11-15 00:04:28 -03001280 ret = videobuf_iolock(vq, vb, NULL);
Vaibhav Hiremath844cc0d2010-03-27 09:37:23 -03001281 if (ret < 0)
1282 return ret;
1283
1284 addr = videobuf_to_dma_contig(vb);
1285 /* Make sure user addresses are aligned to 32 bytes */
1286 if (!ALIGN(addr, 32))
1287 return -EINVAL;
1288
1289 vb->state = VIDEOBUF_PREPARED;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001290 }
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001291 return 0;
1292}
1293
1294static void vpfe_videobuf_queue(struct videobuf_queue *vq,
1295 struct videobuf_buffer *vb)
1296{
1297 /* Get the file handle object and device object */
1298 struct vpfe_fh *fh = vq->priv_data;
1299 struct vpfe_device *vpfe_dev = fh->vpfe_dev;
1300 unsigned long flags;
1301
1302 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buffer_queue\n");
1303
1304 /* add the buffer to the DMA queue */
1305 spin_lock_irqsave(&vpfe_dev->dma_queue_lock, flags);
1306 list_add_tail(&vb->queue, &vpfe_dev->dma_queue);
1307 spin_unlock_irqrestore(&vpfe_dev->dma_queue_lock, flags);
1308
1309 /* Change state of the buffer */
1310 vb->state = VIDEOBUF_QUEUED;
1311}
1312
1313static void vpfe_videobuf_release(struct videobuf_queue *vq,
1314 struct videobuf_buffer *vb)
1315{
1316 struct vpfe_fh *fh = vq->priv_data;
1317 struct vpfe_device *vpfe_dev = fh->vpfe_dev;
1318 unsigned long flags;
1319
1320 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_videobuf_release\n");
1321
1322 /*
1323 * We need to flush the buffer from the dma queue since
1324 * they are de-allocated
1325 */
1326 spin_lock_irqsave(&vpfe_dev->dma_queue_lock, flags);
1327 INIT_LIST_HEAD(&vpfe_dev->dma_queue);
1328 spin_unlock_irqrestore(&vpfe_dev->dma_queue_lock, flags);
1329 videobuf_dma_contig_free(vq, vb);
1330 vb->state = VIDEOBUF_NEEDS_INIT;
1331}
1332
1333static struct videobuf_queue_ops vpfe_videobuf_qops = {
1334 .buf_setup = vpfe_videobuf_setup,
1335 .buf_prepare = vpfe_videobuf_prepare,
1336 .buf_queue = vpfe_videobuf_queue,
1337 .buf_release = vpfe_videobuf_release,
1338};
1339
1340/*
1341 * vpfe_reqbufs. currently support REQBUF only once opening
1342 * the device.
1343 */
1344static int vpfe_reqbufs(struct file *file, void *priv,
1345 struct v4l2_requestbuffers *req_buf)
1346{
1347 struct vpfe_device *vpfe_dev = video_drvdata(file);
1348 struct vpfe_fh *fh = file->private_data;
Markus Elfring2a0de2c2016-10-12 05:50:54 -03001349 int ret;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001350
1351 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_reqbufs\n");
1352
1353 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != req_buf->type) {
1354 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buffer type\n");
1355 return -EINVAL;
1356 }
1357
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001358 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1359 if (ret)
1360 return ret;
1361
1362 if (vpfe_dev->io_usrs != 0) {
1363 v4l2_err(&vpfe_dev->v4l2_dev, "Only one IO user allowed\n");
1364 ret = -EBUSY;
1365 goto unlock_out;
1366 }
1367
1368 vpfe_dev->memory = req_buf->memory;
1369 videobuf_queue_dma_contig_init(&vpfe_dev->buffer_queue,
1370 &vpfe_videobuf_qops,
Vaibhav Hiremath204e6ea2009-11-09 09:07:36 -03001371 vpfe_dev->pdev,
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001372 &vpfe_dev->irqlock,
1373 req_buf->type,
1374 vpfe_dev->fmt.fmt.pix.field,
1375 sizeof(struct videobuf_buffer),
Hans Verkuile3cfd442010-09-30 09:18:52 -03001376 fh, NULL);
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001377
1378 fh->io_allowed = 1;
1379 vpfe_dev->io_usrs = 1;
1380 INIT_LIST_HEAD(&vpfe_dev->dma_queue);
1381 ret = videobuf_reqbufs(&vpfe_dev->buffer_queue, req_buf);
1382unlock_out:
1383 mutex_unlock(&vpfe_dev->lock);
1384 return ret;
1385}
1386
1387static int vpfe_querybuf(struct file *file, void *priv,
1388 struct v4l2_buffer *buf)
1389{
1390 struct vpfe_device *vpfe_dev = video_drvdata(file);
1391
1392 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querybuf\n");
1393
1394 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf->type) {
1395 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1396 return -EINVAL;
1397 }
1398
1399 if (vpfe_dev->memory != V4L2_MEMORY_MMAP) {
1400 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid memory\n");
1401 return -EINVAL;
1402 }
1403 /* Call videobuf_querybuf to get information */
1404 return videobuf_querybuf(&vpfe_dev->buffer_queue, buf);
1405}
1406
1407static int vpfe_qbuf(struct file *file, void *priv,
1408 struct v4l2_buffer *p)
1409{
1410 struct vpfe_device *vpfe_dev = video_drvdata(file);
1411 struct vpfe_fh *fh = file->private_data;
1412
1413 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_qbuf\n");
1414
1415 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != p->type) {
1416 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1417 return -EINVAL;
1418 }
1419
1420 /*
1421 * If this file handle is not allowed to do IO,
1422 * return error
1423 */
1424 if (!fh->io_allowed) {
1425 v4l2_err(&vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1426 return -EACCES;
1427 }
1428 return videobuf_qbuf(&vpfe_dev->buffer_queue, p);
1429}
1430
1431static int vpfe_dqbuf(struct file *file, void *priv,
1432 struct v4l2_buffer *buf)
1433{
1434 struct vpfe_device *vpfe_dev = video_drvdata(file);
1435
1436 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_dqbuf\n");
1437
1438 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf->type) {
1439 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1440 return -EINVAL;
1441 }
1442 return videobuf_dqbuf(&vpfe_dev->buffer_queue,
1443 buf, file->f_flags & O_NONBLOCK);
1444}
1445
1446/*
1447 * vpfe_calculate_offsets : This function calculates buffers offset
1448 * for top and bottom field
1449 */
1450static void vpfe_calculate_offsets(struct vpfe_device *vpfe_dev)
1451{
1452 struct v4l2_rect image_win;
1453
1454 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_calculate_offsets\n");
1455
1456 ccdc_dev->hw_ops.get_image_window(&image_win);
1457 vpfe_dev->field_off = image_win.height * image_win.width;
1458}
1459
1460/* vpfe_start_ccdc_capture: start streaming in ccdc/isif */
1461static void vpfe_start_ccdc_capture(struct vpfe_device *vpfe_dev)
1462{
1463 ccdc_dev->hw_ops.enable(1);
1464 if (ccdc_dev->hw_ops.enable_out_to_sdram)
1465 ccdc_dev->hw_ops.enable_out_to_sdram(1);
1466 vpfe_dev->started = 1;
1467}
1468
1469/*
1470 * vpfe_streamon. Assume the DMA queue is not empty.
1471 * application is expected to call QBUF before calling
1472 * this ioctl. If not, driver returns error
1473 */
1474static int vpfe_streamon(struct file *file, void *priv,
1475 enum v4l2_buf_type buf_type)
1476{
1477 struct vpfe_device *vpfe_dev = video_drvdata(file);
1478 struct vpfe_fh *fh = file->private_data;
1479 struct vpfe_subdev_info *sdinfo;
1480 unsigned long addr;
Markus Elfring2a0de2c2016-10-12 05:50:54 -03001481 int ret;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001482
1483 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_streamon\n");
1484
1485 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf_type) {
1486 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1487 return -EINVAL;
1488 }
1489
1490 /* If file handle is not allowed IO, return error */
1491 if (!fh->io_allowed) {
1492 v4l2_err(&vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1493 return -EACCES;
1494 }
1495
1496 sdinfo = vpfe_dev->current_subdev;
1497 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1498 video, s_stream, 1);
1499
1500 if (ret && (ret != -ENOIOCTLCMD)) {
1501 v4l2_err(&vpfe_dev->v4l2_dev, "stream on failed in subdev\n");
1502 return -EINVAL;
1503 }
1504
1505 /* If buffer queue is empty, return error */
1506 if (list_empty(&vpfe_dev->buffer_queue.stream)) {
1507 v4l2_err(&vpfe_dev->v4l2_dev, "buffer queue is empty\n");
1508 return -EIO;
1509 }
1510
1511 /* Call videobuf_streamon to start streaming * in videobuf */
1512 ret = videobuf_streamon(&vpfe_dev->buffer_queue);
1513 if (ret)
1514 return ret;
1515
1516
1517 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1518 if (ret)
1519 goto streamoff;
1520 /* Get the next frame from the buffer queue */
1521 vpfe_dev->next_frm = list_entry(vpfe_dev->dma_queue.next,
1522 struct videobuf_buffer, queue);
1523 vpfe_dev->cur_frm = vpfe_dev->next_frm;
1524 /* Remove buffer from the buffer queue */
1525 list_del(&vpfe_dev->cur_frm->queue);
1526 /* Mark state of the current frame to active */
1527 vpfe_dev->cur_frm->state = VIDEOBUF_ACTIVE;
1528 /* Initialize field_id and started member */
1529 vpfe_dev->field_id = 0;
1530 addr = videobuf_to_dma_contig(vpfe_dev->cur_frm);
1531
1532 /* Calculate field offset */
1533 vpfe_calculate_offsets(vpfe_dev);
1534
1535 if (vpfe_attach_irq(vpfe_dev) < 0) {
1536 v4l2_err(&vpfe_dev->v4l2_dev,
1537 "Error in attaching interrupt handle\n");
1538 ret = -EFAULT;
1539 goto unlock_out;
1540 }
1541 if (ccdc_dev->hw_ops.configure() < 0) {
1542 v4l2_err(&vpfe_dev->v4l2_dev,
1543 "Error in configuring ccdc\n");
1544 ret = -EINVAL;
1545 goto unlock_out;
1546 }
1547 ccdc_dev->hw_ops.setfbaddr((unsigned long)(addr));
1548 vpfe_start_ccdc_capture(vpfe_dev);
1549 mutex_unlock(&vpfe_dev->lock);
1550 return ret;
1551unlock_out:
1552 mutex_unlock(&vpfe_dev->lock);
1553streamoff:
1554 ret = videobuf_streamoff(&vpfe_dev->buffer_queue);
1555 return ret;
1556}
1557
1558static int vpfe_streamoff(struct file *file, void *priv,
1559 enum v4l2_buf_type buf_type)
1560{
1561 struct vpfe_device *vpfe_dev = video_drvdata(file);
1562 struct vpfe_fh *fh = file->private_data;
1563 struct vpfe_subdev_info *sdinfo;
Markus Elfring2a0de2c2016-10-12 05:50:54 -03001564 int ret;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001565
1566 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_streamoff\n");
1567
1568 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf_type) {
1569 v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1570 return -EINVAL;
1571 }
1572
1573 /* If io is allowed for this file handle, return error */
1574 if (!fh->io_allowed) {
1575 v4l2_err(&vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1576 return -EACCES;
1577 }
1578
1579 /* If streaming is not started, return error */
1580 if (!vpfe_dev->started) {
1581 v4l2_err(&vpfe_dev->v4l2_dev, "device started\n");
1582 return -EINVAL;
1583 }
1584
1585 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1586 if (ret)
1587 return ret;
1588
1589 vpfe_stop_ccdc_capture(vpfe_dev);
1590 vpfe_detach_irq(vpfe_dev);
1591
1592 sdinfo = vpfe_dev->current_subdev;
1593 ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
1594 video, s_stream, 0);
1595
1596 if (ret && (ret != -ENOIOCTLCMD))
1597 v4l2_err(&vpfe_dev->v4l2_dev, "stream off failed in subdev\n");
1598 ret = videobuf_streamoff(&vpfe_dev->buffer_queue);
1599 mutex_unlock(&vpfe_dev->lock);
1600 return ret;
1601}
1602
1603static int vpfe_cropcap(struct file *file, void *priv,
1604 struct v4l2_cropcap *crop)
1605{
1606 struct vpfe_device *vpfe_dev = video_drvdata(file);
1607
1608 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_cropcap\n");
1609
Hans Verkuilffc2a6b2016-07-03 08:28:43 -03001610 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001611 return -EINVAL;
Hans Verkuilffc2a6b2016-07-03 08:28:43 -03001612 /* If std_index is invalid, then just return (== 1:1 aspect) */
1613 if (vpfe_dev->std_index >= ARRAY_SIZE(vpfe_standards))
1614 return 0;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001615
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001616 crop->pixelaspect = vpfe_standards[vpfe_dev->std_index].pixelaspect;
1617 return 0;
1618}
1619
Hans Verkuilffc2a6b2016-07-03 08:28:43 -03001620static int vpfe_g_selection(struct file *file, void *priv,
1621 struct v4l2_selection *sel)
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001622{
1623 struct vpfe_device *vpfe_dev = video_drvdata(file);
1624
Hans Verkuilffc2a6b2016-07-03 08:28:43 -03001625 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_selection\n");
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001626
Hans Verkuilffc2a6b2016-07-03 08:28:43 -03001627 if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1628 return -EINVAL;
1629
1630 switch (sel->target) {
1631 case V4L2_SEL_TGT_CROP:
1632 sel->r = vpfe_dev->crop;
1633 break;
1634 case V4L2_SEL_TGT_CROP_DEFAULT:
1635 case V4L2_SEL_TGT_CROP_BOUNDS:
1636 sel->r.width = vpfe_standards[vpfe_dev->std_index].width;
1637 sel->r.height = vpfe_standards[vpfe_dev->std_index].height;
1638 break;
1639 default:
1640 return -EINVAL;
1641 }
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001642 return 0;
1643}
1644
Hans Verkuilffc2a6b2016-07-03 08:28:43 -03001645static int vpfe_s_selection(struct file *file, void *priv,
1646 struct v4l2_selection *sel)
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001647{
1648 struct vpfe_device *vpfe_dev = video_drvdata(file);
Hans Verkuilffc2a6b2016-07-03 08:28:43 -03001649 struct v4l2_rect rect = sel->r;
Markus Elfring2a0de2c2016-10-12 05:50:54 -03001650 int ret;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001651
Hans Verkuilffc2a6b2016-07-03 08:28:43 -03001652 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_selection\n");
1653
1654 if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1655 sel->target != V4L2_SEL_TGT_CROP)
1656 return -EINVAL;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001657
1658 if (vpfe_dev->started) {
1659 /* make sure streaming is not started */
1660 v4l2_err(&vpfe_dev->v4l2_dev,
1661 "Cannot change crop when streaming is ON\n");
1662 return -EBUSY;
1663 }
1664
1665 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1666 if (ret)
1667 return ret;
1668
Lad, Prabhakar8b6faac2012-10-01 08:52:48 -03001669 if (rect.top < 0 || rect.left < 0) {
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001670 v4l2_err(&vpfe_dev->v4l2_dev,
1671 "doesn't support negative values for top & left\n");
1672 ret = -EINVAL;
1673 goto unlock_out;
1674 }
1675
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001676 /* adjust the width to 16 pixel boundary */
Lad, Prabhakar8b6faac2012-10-01 08:52:48 -03001677 rect.width = ((rect.width + 15) & ~0xf);
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001678
1679 /* make sure parameters are valid */
Lad, Prabhakar8b6faac2012-10-01 08:52:48 -03001680 if ((rect.left + rect.width >
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001681 vpfe_dev->std_info.active_pixels) ||
Lad, Prabhakar8b6faac2012-10-01 08:52:48 -03001682 (rect.top + rect.height >
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001683 vpfe_dev->std_info.active_lines)) {
Hans Verkuilffc2a6b2016-07-03 08:28:43 -03001684 v4l2_err(&vpfe_dev->v4l2_dev, "Error in S_SELECTION params\n");
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001685 ret = -EINVAL;
1686 goto unlock_out;
1687 }
Lad, Prabhakar8b6faac2012-10-01 08:52:48 -03001688 ccdc_dev->hw_ops.set_image_window(&rect);
1689 vpfe_dev->fmt.fmt.pix.width = rect.width;
1690 vpfe_dev->fmt.fmt.pix.height = rect.height;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001691 vpfe_dev->fmt.fmt.pix.bytesperline =
1692 ccdc_dev->hw_ops.get_line_length();
1693 vpfe_dev->fmt.fmt.pix.sizeimage =
1694 vpfe_dev->fmt.fmt.pix.bytesperline *
1695 vpfe_dev->fmt.fmt.pix.height;
Lad, Prabhakar8b6faac2012-10-01 08:52:48 -03001696 vpfe_dev->crop = rect;
Hans Verkuilffc2a6b2016-07-03 08:28:43 -03001697 sel->r = rect;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001698unlock_out:
1699 mutex_unlock(&vpfe_dev->lock);
1700 return ret;
1701}
1702
1703
1704static long vpfe_param_handler(struct file *file, void *priv,
Mauro Carvalho Chehab6d43be72013-03-26 08:04:52 -03001705 bool valid_prio, unsigned int cmd, void *param)
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001706{
1707 struct vpfe_device *vpfe_dev = video_drvdata(file);
Markus Elfring2a0de2c2016-10-12 05:50:54 -03001708 int ret;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001709
Muralidharan Karicheri6a4f0622010-03-18 11:44:12 -03001710 v4l2_dbg(2, debug, &vpfe_dev->v4l2_dev, "vpfe_param_handler\n");
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001711
1712 if (vpfe_dev->started) {
1713 /* only allowed if streaming is not started */
Muralidharan Karicheri6a4f0622010-03-18 11:44:12 -03001714 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1715 "device already started\n");
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001716 return -EBUSY;
1717 }
1718
1719 ret = mutex_lock_interruptible(&vpfe_dev->lock);
1720 if (ret)
1721 return ret;
1722
1723 switch (cmd) {
1724 case VPFE_CMD_S_CCDC_RAW_PARAMS:
1725 v4l2_warn(&vpfe_dev->v4l2_dev,
1726 "VPFE_CMD_S_CCDC_RAW_PARAMS: experimental ioctl\n");
Muralidharan Karicheri6a4f0622010-03-18 11:44:12 -03001727 if (ccdc_dev->hw_ops.set_params) {
1728 ret = ccdc_dev->hw_ops.set_params(param);
1729 if (ret) {
1730 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1731 "Error setting parameters in CCDC\n");
1732 goto unlock_out;
1733 }
Peter Senna Tschudin9a888ba2012-09-04 08:05:03 -03001734 ret = vpfe_get_ccdc_image_format(vpfe_dev,
1735 &vpfe_dev->fmt);
1736 if (ret < 0) {
Muralidharan Karicheri6a4f0622010-03-18 11:44:12 -03001737 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1738 "Invalid image format at CCDC\n");
1739 goto unlock_out;
1740 }
1741 } else {
1742 ret = -EINVAL;
1743 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1744 "VPFE_CMD_S_CCDC_RAW_PARAMS not supported\n");
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001745 }
1746 break;
1747 default:
Hans Verkuild1c754a2012-04-19 12:36:03 -03001748 ret = -ENOTTY;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001749 }
1750unlock_out:
1751 mutex_unlock(&vpfe_dev->lock);
1752 return ret;
1753}
1754
1755
1756/* vpfe capture ioctl operations */
1757static const struct v4l2_ioctl_ops vpfe_ioctl_ops = {
1758 .vidioc_querycap = vpfe_querycap,
1759 .vidioc_g_fmt_vid_cap = vpfe_g_fmt_vid_cap,
1760 .vidioc_enum_fmt_vid_cap = vpfe_enum_fmt_vid_cap,
1761 .vidioc_s_fmt_vid_cap = vpfe_s_fmt_vid_cap,
1762 .vidioc_try_fmt_vid_cap = vpfe_try_fmt_vid_cap,
1763 .vidioc_enum_input = vpfe_enum_input,
1764 .vidioc_g_input = vpfe_g_input,
1765 .vidioc_s_input = vpfe_s_input,
1766 .vidioc_querystd = vpfe_querystd,
1767 .vidioc_s_std = vpfe_s_std,
1768 .vidioc_g_std = vpfe_g_std,
1769 .vidioc_reqbufs = vpfe_reqbufs,
1770 .vidioc_querybuf = vpfe_querybuf,
1771 .vidioc_qbuf = vpfe_qbuf,
1772 .vidioc_dqbuf = vpfe_dqbuf,
1773 .vidioc_streamon = vpfe_streamon,
1774 .vidioc_streamoff = vpfe_streamoff,
1775 .vidioc_cropcap = vpfe_cropcap,
Hans Verkuilffc2a6b2016-07-03 08:28:43 -03001776 .vidioc_g_selection = vpfe_g_selection,
1777 .vidioc_s_selection = vpfe_s_selection,
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001778 .vidioc_default = vpfe_param_handler,
1779};
1780
1781static struct vpfe_device *vpfe_initialize(void)
1782{
1783 struct vpfe_device *vpfe_dev;
1784
1785 /* Default number of buffers should be 3 */
1786 if ((numbuffers > 0) &&
1787 (numbuffers < config_params.min_numbuffers))
1788 numbuffers = config_params.min_numbuffers;
1789
1790 /*
1791 * Set buffer size to min buffers size if invalid buffer size is
1792 * given
1793 */
1794 if (bufsize < config_params.min_bufsize)
1795 bufsize = config_params.min_bufsize;
1796
1797 config_params.numbuffers = numbuffers;
1798
1799 if (numbuffers)
1800 config_params.device_bufsize = bufsize;
1801
1802 /* Allocate memory for device objects */
1803 vpfe_dev = kzalloc(sizeof(*vpfe_dev), GFP_KERNEL);
1804
1805 return vpfe_dev;
1806}
1807
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001808/*
1809 * vpfe_probe : This function creates device entries by register
1810 * itself to the V4L2 driver and initializes fields of each
1811 * device objects
1812 */
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001813static int vpfe_probe(struct platform_device *pdev)
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001814{
1815 struct vpfe_subdev_info *sdinfo;
1816 struct vpfe_config *vpfe_cfg;
1817 struct resource *res1;
1818 struct vpfe_device *vpfe_dev;
1819 struct i2c_adapter *i2c_adap;
1820 struct video_device *vfd;
Markus Elfringefb74462016-10-12 05:30:28 -03001821 int ret, i, j;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001822 int num_subdevs = 0;
1823
1824 /* Get the pointer to the device object */
1825 vpfe_dev = vpfe_initialize();
1826
1827 if (!vpfe_dev) {
1828 v4l2_err(pdev->dev.driver,
1829 "Failed to allocate memory for vpfe_dev\n");
Markus Elfringefb74462016-10-12 05:30:28 -03001830 return -ENOMEM;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001831 }
1832
1833 vpfe_dev->pdev = &pdev->dev;
1834
Markus Elfringc580f292016-10-12 05:46:28 -03001835 if (!pdev->dev.platform_data) {
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001836 v4l2_err(pdev->dev.driver, "Unable to get vpfe config\n");
Muralidharan Karicheri51444ea2010-01-13 20:27:05 -03001837 ret = -ENODEV;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001838 goto probe_free_dev_mem;
1839 }
1840
1841 vpfe_cfg = pdev->dev.platform_data;
1842 vpfe_dev->cfg = vpfe_cfg;
Markus Elfringc580f292016-10-12 05:46:28 -03001843 if (!vpfe_cfg->ccdc || !vpfe_cfg->card_name || !vpfe_cfg->sub_devs) {
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001844 v4l2_err(pdev->dev.driver, "null ptr in vpfe_cfg\n");
1845 ret = -ENOENT;
1846 goto probe_free_dev_mem;
1847 }
1848
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001849 /* Allocate memory for ccdc configuration */
Markus Elfringe4c0cd02016-10-12 05:24:57 -03001850 ccdc_cfg = kmalloc(sizeof(*ccdc_cfg), GFP_KERNEL);
Markus Elfring11691f02016-10-12 05:22:47 -03001851 if (!ccdc_cfg)
Lad, Prabhakar39e219d2013-05-10 00:48:38 -03001852 goto probe_free_dev_mem;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001853
Dan Carpenter2b080c52010-04-07 06:21:05 -03001854 mutex_lock(&ccdc_lock);
1855
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001856 strncpy(ccdc_cfg->name, vpfe_cfg->ccdc, 32);
1857 /* Get VINT0 irq resource */
1858 res1 = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1859 if (!res1) {
1860 v4l2_err(pdev->dev.driver,
1861 "Unable to get interrupt for VINT0\n");
Muralidharan Karicheri51444ea2010-01-13 20:27:05 -03001862 ret = -ENODEV;
1863 goto probe_free_ccdc_cfg_mem;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001864 }
1865 vpfe_dev->ccdc_irq0 = res1->start;
1866
1867 /* Get VINT1 irq resource */
Muralidharan Karicheri51444ea2010-01-13 20:27:05 -03001868 res1 = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001869 if (!res1) {
1870 v4l2_err(pdev->dev.driver,
1871 "Unable to get interrupt for VINT1\n");
Muralidharan Karicheri51444ea2010-01-13 20:27:05 -03001872 ret = -ENODEV;
1873 goto probe_free_ccdc_cfg_mem;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001874 }
1875 vpfe_dev->ccdc_irq1 = res1->start;
1876
Michael Opdenackerd8c279a2013-09-08 23:30:11 -03001877 ret = request_irq(vpfe_dev->ccdc_irq0, vpfe_isr, 0,
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001878 "vpfe_capture0", vpfe_dev);
1879
1880 if (0 != ret) {
1881 v4l2_err(pdev->dev.driver, "Unable to request interrupt\n");
Muralidharan Karicheri51444ea2010-01-13 20:27:05 -03001882 goto probe_free_ccdc_cfg_mem;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001883 }
1884
Lad, Prabhakar03c278f2015-03-10 14:53:05 -03001885 vfd = &vpfe_dev->video_dev;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001886 /* Initialize field of video device */
Lad, Prabhakar03c278f2015-03-10 14:53:05 -03001887 vfd->release = video_device_release_empty;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001888 vfd->fops = &vpfe_fops;
1889 vfd->ioctl_ops = &vpfe_ioctl_ops;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001890 vfd->tvnorms = 0;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001891 vfd->v4l2_dev = &vpfe_dev->v4l2_dev;
1892 snprintf(vfd->name, sizeof(vfd->name),
1893 "%s_V%d.%d.%d",
1894 CAPTURE_DRV_NAME,
1895 (VPFE_CAPTURE_VERSION_CODE >> 16) & 0xff,
1896 (VPFE_CAPTURE_VERSION_CODE >> 8) & 0xff,
1897 (VPFE_CAPTURE_VERSION_CODE) & 0xff);
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001898
1899 ret = v4l2_device_register(&pdev->dev, &vpfe_dev->v4l2_dev);
1900 if (ret) {
1901 v4l2_err(pdev->dev.driver,
1902 "Unable to register v4l2 device.\n");
Lad, Prabhakar03c278f2015-03-10 14:53:05 -03001903 goto probe_out_release_irq;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001904 }
1905 v4l2_info(&vpfe_dev->v4l2_dev, "v4l2 device registered\n");
1906 spin_lock_init(&vpfe_dev->irqlock);
1907 spin_lock_init(&vpfe_dev->dma_queue_lock);
1908 mutex_init(&vpfe_dev->lock);
1909
1910 /* Initialize field of the device objects */
1911 vpfe_dev->numbuffers = config_params.numbuffers;
1912
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001913 /* register video device */
1914 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1915 "trying to register vpfe device.\n");
1916 v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
Mauro Carvalho Chehab212bdba2014-08-22 06:38:14 -05001917 "video_dev=%p\n", &vpfe_dev->video_dev);
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001918 vpfe_dev->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Lad, Prabhakar03c278f2015-03-10 14:53:05 -03001919 ret = video_register_device(&vpfe_dev->video_dev,
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001920 VFL_TYPE_GRABBER, -1);
1921
1922 if (ret) {
1923 v4l2_err(pdev->dev.driver,
1924 "Unable to register video device.\n");
1925 goto probe_out_v4l2_unregister;
1926 }
1927
1928 v4l2_info(&vpfe_dev->v4l2_dev, "video device registered\n");
1929 /* set the driver data in platform device */
1930 platform_set_drvdata(pdev, vpfe_dev);
1931 /* set driver private data */
Lad, Prabhakar03c278f2015-03-10 14:53:05 -03001932 video_set_drvdata(&vpfe_dev->video_dev, vpfe_dev);
Vaibhav Hiremath14cbaaf2009-11-09 09:14:16 -03001933 i2c_adap = i2c_get_adapter(vpfe_cfg->i2c_adapter_id);
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001934 num_subdevs = vpfe_cfg->num_subdevs;
Markus Elfringed011a22016-10-12 05:20:02 -03001935 vpfe_dev->sd = kmalloc_array(num_subdevs,
1936 sizeof(*vpfe_dev->sd),
1937 GFP_KERNEL);
Markus Elfringc580f292016-10-12 05:46:28 -03001938 if (!vpfe_dev->sd) {
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001939 ret = -ENOMEM;
1940 goto probe_out_video_unregister;
1941 }
1942
1943 for (i = 0; i < num_subdevs; i++) {
1944 struct v4l2_input *inps;
1945
1946 sdinfo = &vpfe_cfg->sub_devs[i];
1947
1948 /* Load up the subdevice */
1949 vpfe_dev->sd[i] =
1950 v4l2_i2c_new_subdev_board(&vpfe_dev->v4l2_dev,
1951 i2c_adap,
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001952 &sdinfo->board_info,
1953 NULL);
1954 if (vpfe_dev->sd[i]) {
1955 v4l2_info(&vpfe_dev->v4l2_dev,
1956 "v4l2 sub device %s registered\n",
1957 sdinfo->name);
1958 vpfe_dev->sd[i]->grp_id = sdinfo->grp_id;
1959 /* update tvnorms from the sub devices */
1960 for (j = 0; j < sdinfo->num_inputs; j++) {
1961 inps = &sdinfo->inputs[j];
1962 vfd->tvnorms |= inps->std;
1963 }
1964 } else {
1965 v4l2_info(&vpfe_dev->v4l2_dev,
1966 "v4l2 sub device %s register fails\n",
1967 sdinfo->name);
1968 goto probe_sd_out;
1969 }
1970 }
1971
1972 /* set first sub device as current one */
1973 vpfe_dev->current_subdev = &vpfe_cfg->sub_devs[0];
Hans Verkuil6f55dba2013-03-04 05:48:43 -03001974 vpfe_dev->v4l2_dev.ctrl_handler = vpfe_dev->sd[0]->ctrl_handler;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001975
1976 /* We have at least one sub device to work with */
1977 mutex_unlock(&ccdc_lock);
1978 return 0;
1979
1980probe_sd_out:
1981 kfree(vpfe_dev->sd);
1982probe_out_video_unregister:
Lad, Prabhakar03c278f2015-03-10 14:53:05 -03001983 video_unregister_device(&vpfe_dev->video_dev);
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001984probe_out_v4l2_unregister:
1985 v4l2_device_unregister(&vpfe_dev->v4l2_dev);
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001986probe_out_release_irq:
1987 free_irq(vpfe_dev->ccdc_irq0, vpfe_dev);
Muralidharan Karicheri51444ea2010-01-13 20:27:05 -03001988probe_free_ccdc_cfg_mem:
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001989 kfree(ccdc_cfg);
Murali Karicheriab51bec2010-03-01 19:54:02 -03001990 mutex_unlock(&ccdc_lock);
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03001991probe_free_dev_mem:
1992 kfree(vpfe_dev);
1993 return ret;
1994}
1995
1996/*
1997 * vpfe_remove : It un-register device from V4L2 driver
1998 */
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001999static int vpfe_remove(struct platform_device *pdev)
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03002000{
2001 struct vpfe_device *vpfe_dev = platform_get_drvdata(pdev);
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03002002
2003 v4l2_info(pdev->dev.driver, "vpfe_remove\n");
2004
2005 free_irq(vpfe_dev->ccdc_irq0, vpfe_dev);
2006 kfree(vpfe_dev->sd);
2007 v4l2_device_unregister(&vpfe_dev->v4l2_dev);
Lad, Prabhakar03c278f2015-03-10 14:53:05 -03002008 video_unregister_device(&vpfe_dev->video_dev);
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03002009 kfree(vpfe_dev);
2010 kfree(ccdc_cfg);
2011 return 0;
2012}
2013
Vaibhav Hiremathaa2dc902010-03-27 09:37:16 -03002014static int vpfe_suspend(struct device *dev)
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03002015{
Vaibhav Hiremathaa2dc902010-03-27 09:37:16 -03002016 return 0;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03002017}
2018
Vaibhav Hiremathaa2dc902010-03-27 09:37:16 -03002019static int vpfe_resume(struct device *dev)
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03002020{
Vaibhav Hiremathaa2dc902010-03-27 09:37:16 -03002021 return 0;
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03002022}
2023
Alexey Dobriyan47145212009-12-14 18:00:08 -08002024static const struct dev_pm_ops vpfe_dev_pm_ops = {
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03002025 .suspend = vpfe_suspend,
2026 .resume = vpfe_resume,
2027};
2028
Lad, Prabhakara1b3a6c2012-08-14 01:23:09 -03002029static struct platform_driver vpfe_driver = {
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03002030 .driver = {
2031 .name = CAPTURE_DRV_NAME,
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03002032 .pm = &vpfe_dev_pm_ops,
2033 },
2034 .probe = vpfe_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08002035 .remove = vpfe_remove,
Muralidharan Karicheri7da8a6c2009-07-06 15:04:12 -03002036};
2037
Axel Lin1d6629b2012-01-10 03:21:49 -03002038module_platform_driver(vpfe_driver);