Merge tag 'please-pull-fixia64' of git://git.kernel.org/pub/scm/linux/kernel/git...
[sfrench/cifs-2.6.git] / drivers / staging / media / davinci_vpfe / vpfe_mc_capture.c
1 /*
2  * Copyright (C) 2012 Texas Instruments Inc
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation version 2.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16  *
17  * Contributors:
18  *      Manjunath Hadli <manjunath.hadli@ti.com>
19  *      Prabhakar Lad <prabhakar.lad@ti.com>
20  *
21  *
22  * Driver name : VPFE Capture driver
23  *    VPFE Capture driver allows applications to capture and stream video
24  *    frames on DaVinci SoCs (DM6446, DM355 etc) from a YUV source such as
25  *    TVP5146 or  Raw Bayer RGB image data from an image sensor
26  *    such as Microns' MT9T001, MT9T031 etc.
27  *
28  *    These SoCs have, in common, a Video Processing Subsystem (VPSS) that
29  *    consists of a Video Processing Front End (VPFE) for capturing
30  *    video/raw image data and Video Processing Back End (VPBE) for displaying
31  *    YUV data through an in-built analog encoder or Digital LCD port. This
32  *    driver is for capture through VPFE. A typical EVM using these SoCs have
33  *    following high level configuration.
34  *
35  *    decoder(TVP5146/          YUV/
36  *      MT9T001)   -->  Raw Bayer RGB ---> MUX -> VPFE (CCDC/ISIF)
37  *                      data input              |      |
38  *                                                      V      |
39  *                                                    SDRAM    |
40  *                                                             V
41  *                                                         Image Processor
42  *                                                             |
43  *                                                             V
44  *                                                           SDRAM
45  *    The data flow happens from a decoder connected to the VPFE over a
46  *    YUV embedded (BT.656/BT.1120) or separate sync or raw bayer rgb interface
47  *    and to the input of VPFE through an optional MUX (if more inputs are
48  *    to be interfaced on the EVM). The input data is first passed through
49  *    CCDC (CCD Controller, a.k.a Image Sensor Interface, ISIF). The CCDC
50  *    does very little or no processing on YUV data and does pre-process Raw
51  *    Bayer RGB data through modules such as Defect Pixel Correction (DFC)
52  *    Color Space Conversion (CSC), data gain/offset etc. After this, data
53  *    can be written to SDRAM or can be connected to the image processing
54  *    block such as IPIPE (on DM355/DM365 only).
55  *
56  *    Features supported
57  *              - MMAP IO
58  *              - USERPTR IO
59  *              - Capture using TVP5146 over BT.656
60  *              - Support for interfacing decoders using sub device model
61  *              - Work with DM365 or DM355 or DM6446 CCDC to do Raw Bayer
62  *                RGB/YUV data capture to SDRAM.
63  *              - Chaining of Image Processor
64  *              - SINGLE-SHOT mode
65  */
66
67 #include <linux/interrupt.h>
68 #include <linux/module.h>
69 #include <linux/slab.h>
70
71 #include "vpfe.h"
72 #include "vpfe_mc_capture.h"
73
74 static bool debug;
75 static bool interface;
76
77 module_param(interface, bool, S_IRUGO);
78 module_param(debug, bool, 0644);
79
80 /**
81  * VPFE capture can be used for capturing video such as from TVP5146 or TVP7002
82  * and for capture raw bayer data from camera sensors such as mt9p031. At this
83  * point there is problem in co-existence of mt9p031 and tvp5146 due to i2c
84  * address collision. So set the variable below from bootargs to do either video
85  * capture or camera capture.
86  * interface = 0 - video capture (from TVP514x or such),
87  * interface = 1 - Camera capture (from mt9p031 or such)
88  * Re-visit this when we fix the co-existence issue
89  */
90 MODULE_PARM_DESC(interface, "interface 0-1 (default:0)");
91 MODULE_PARM_DESC(debug, "Debug level 0-1");
92
93 MODULE_DESCRIPTION("VPFE Video for Linux Capture Driver");
94 MODULE_LICENSE("GPL");
95 MODULE_AUTHOR("Texas Instruments");
96
97 /* map mbus_fmt to pixelformat */
98 void mbus_to_pix(const struct v4l2_mbus_framefmt *mbus,
99                            struct v4l2_pix_format *pix)
100 {
101         switch (mbus->code) {
102         case V4L2_MBUS_FMT_UYVY8_2X8:
103                 pix->pixelformat = V4L2_PIX_FMT_UYVY;
104                 pix->bytesperline = pix->width * 2;
105                 break;
106
107         case V4L2_MBUS_FMT_YUYV8_2X8:
108                 pix->pixelformat = V4L2_PIX_FMT_YUYV;
109                 pix->bytesperline = pix->width * 2;
110                 break;
111
112         case V4L2_MBUS_FMT_YUYV10_1X20:
113                 pix->pixelformat = V4L2_PIX_FMT_UYVY;
114                 pix->bytesperline = pix->width * 2;
115                 break;
116
117         case V4L2_MBUS_FMT_SGRBG12_1X12:
118                 pix->pixelformat = V4L2_PIX_FMT_SBGGR16;
119                 pix->bytesperline = pix->width * 2;
120                 break;
121
122         case V4L2_MBUS_FMT_SGRBG10_DPCM8_1X8:
123                 pix->pixelformat = V4L2_PIX_FMT_SGRBG10DPCM8;
124                 pix->bytesperline = pix->width;
125                 break;
126
127         case V4L2_MBUS_FMT_SGRBG10_ALAW8_1X8:
128                 pix->pixelformat = V4L2_PIX_FMT_SGRBG10ALAW8;
129                 pix->bytesperline = pix->width;
130                 break;
131
132         case V4L2_MBUS_FMT_YDYUYDYV8_1X16:
133                 pix->pixelformat = V4L2_PIX_FMT_NV12;
134                 pix->bytesperline = pix->width;
135                 break;
136
137         case V4L2_MBUS_FMT_Y8_1X8:
138                 pix->pixelformat = V4L2_PIX_FMT_GREY;
139                 pix->bytesperline = pix->width;
140                 break;
141
142         case V4L2_MBUS_FMT_UV8_1X8:
143                 pix->pixelformat = V4L2_PIX_FMT_UV8;
144                 pix->bytesperline = pix->width;
145                 break;
146
147         default:
148                 pr_err("Invalid mbus code set\n");
149         }
150         /* pitch should be 32 bytes aligned */
151         pix->bytesperline = ALIGN(pix->bytesperline, 32);
152         if (pix->pixelformat == V4L2_PIX_FMT_NV12)
153                 pix->sizeimage = pix->bytesperline * pix->height +
154                                 ((pix->bytesperline * pix->height) >> 1);
155         else
156                 pix->sizeimage = pix->bytesperline * pix->height;
157 }
158
159 /* ISR for VINT0*/
160 static irqreturn_t vpfe_isr(int irq, void *dev_id)
161 {
162         struct vpfe_device *vpfe_dev = dev_id;
163
164         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_isr\n");
165         vpfe_isif_buffer_isr(&vpfe_dev->vpfe_isif);
166         vpfe_resizer_buffer_isr(&vpfe_dev->vpfe_resizer);
167         return IRQ_HANDLED;
168 }
169
170 /* vpfe_vdint1_isr() - isr handler for VINT1 interrupt */
171 static irqreturn_t vpfe_vdint1_isr(int irq, void *dev_id)
172 {
173         struct vpfe_device *vpfe_dev = dev_id;
174
175         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_vdint1_isr\n");
176         vpfe_isif_vidint1_isr(&vpfe_dev->vpfe_isif);
177         return IRQ_HANDLED;
178 }
179
180 /* vpfe_imp_dma_isr() - ISR for ipipe dma completion */
181 static irqreturn_t vpfe_imp_dma_isr(int irq, void *dev_id)
182 {
183         struct vpfe_device *vpfe_dev = dev_id;
184
185         v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_imp_dma_isr\n");
186         vpfe_ipipeif_ss_buffer_isr(&vpfe_dev->vpfe_ipipeif);
187         vpfe_resizer_dma_isr(&vpfe_dev->vpfe_resizer);
188         return IRQ_HANDLED;
189 }
190
191 /*
192  * vpfe_disable_clock() - Disable clocks for vpfe capture driver
193  * @vpfe_dev - ptr to vpfe capture device
194  *
195  * Disables clocks defined in vpfe configuration. The function
196  * assumes that at least one clock is to be defined which is
197  * true as of now.
198  */
199 static void vpfe_disable_clock(struct vpfe_device *vpfe_dev)
200 {
201         struct vpfe_config *vpfe_cfg = vpfe_dev->cfg;
202         int i;
203
204         for (i = 0; i < vpfe_cfg->num_clocks; i++) {
205                 clk_disable_unprepare(vpfe_dev->clks[i]);
206                 clk_put(vpfe_dev->clks[i]);
207         }
208         kzfree(vpfe_dev->clks);
209         v4l2_info(vpfe_dev->pdev->driver, "vpfe capture clocks disabled\n");
210 }
211
212 /*
213  * vpfe_enable_clock() - Enable clocks for vpfe capture driver
214  * @vpfe_dev - ptr to vpfe capture device
215  *
216  * Enables clocks defined in vpfe configuration. The function
217  * assumes that at least one clock is to be defined which is
218  * true as of now.
219  */
220 static int vpfe_enable_clock(struct vpfe_device *vpfe_dev)
221 {
222         struct vpfe_config *vpfe_cfg = vpfe_dev->cfg;
223         int ret = -EFAULT;
224         int i;
225
226         if (!vpfe_cfg->num_clocks)
227                 return 0;
228
229         vpfe_dev->clks = kzalloc(vpfe_cfg->num_clocks *
230                                    sizeof(struct clock *), GFP_KERNEL);
231         if (vpfe_dev->clks == NULL) {
232                 v4l2_err(vpfe_dev->pdev->driver, "Memory allocation failed\n");
233                 return -ENOMEM;
234         }
235
236         for (i = 0; i < vpfe_cfg->num_clocks; i++) {
237                 if (vpfe_cfg->clocks[i] == NULL) {
238                         v4l2_err(vpfe_dev->pdev->driver,
239                                 "clock %s is not defined in vpfe config\n",
240                                 vpfe_cfg->clocks[i]);
241                         goto out;
242                 }
243
244                 vpfe_dev->clks[i] =
245                                 clk_get(vpfe_dev->pdev, vpfe_cfg->clocks[i]);
246                 if (IS_ERR(vpfe_dev->clks[i])) {
247                         v4l2_err(vpfe_dev->pdev->driver,
248                                 "Failed to get clock %s\n",
249                                 vpfe_cfg->clocks[i]);
250                         goto out;
251                 }
252
253                 if (clk_prepare_enable(vpfe_dev->clks[i])) {
254                         v4l2_err(vpfe_dev->pdev->driver,
255                                 "vpfe clock %s not enabled\n",
256                                 vpfe_cfg->clocks[i]);
257                         goto out;
258                 }
259
260                 v4l2_info(vpfe_dev->pdev->driver, "vpss clock %s enabled",
261                           vpfe_cfg->clocks[i]);
262         }
263
264         return 0;
265 out:
266         for (i = 0; i < vpfe_cfg->num_clocks; i++)
267                 if (!IS_ERR(vpfe_dev->clks[i])) {
268                         clk_disable_unprepare(vpfe_dev->clks[i]);
269                         clk_put(vpfe_dev->clks[i]);
270                 }
271
272         v4l2_err(vpfe_dev->pdev->driver, "Failed to enable clocks\n");
273         kzfree(vpfe_dev->clks);
274
275         return ret;
276 }
277
278 /*
279  * vpfe_detach_irq() - Detach IRQs for vpfe capture driver
280  * @vpfe_dev - ptr to vpfe capture device
281  *
282  * Detach all IRQs defined in vpfe configuration.
283  */
284 static void vpfe_detach_irq(struct vpfe_device *vpfe_dev)
285 {
286         free_irq(vpfe_dev->ccdc_irq0, vpfe_dev);
287         free_irq(vpfe_dev->ccdc_irq1, vpfe_dev);
288         free_irq(vpfe_dev->imp_dma_irq, vpfe_dev);
289 }
290
291 /*
292  * vpfe_attach_irq() - Attach IRQs for vpfe capture driver
293  * @vpfe_dev - ptr to vpfe capture device
294  *
295  * Attach all IRQs defined in vpfe configuration.
296  */
297 static int vpfe_attach_irq(struct vpfe_device *vpfe_dev)
298 {
299         int ret = 0;
300
301         ret = request_irq(vpfe_dev->ccdc_irq0, vpfe_isr, IRQF_DISABLED,
302                           "vpfe_capture0", vpfe_dev);
303         if (ret < 0) {
304                 v4l2_err(&vpfe_dev->v4l2_dev,
305                         "Error: requesting VINT0 interrupt\n");
306                 return ret;
307         }
308
309         ret = request_irq(vpfe_dev->ccdc_irq1, vpfe_vdint1_isr, IRQF_DISABLED,
310                           "vpfe_capture1", vpfe_dev);
311         if (ret < 0) {
312                 v4l2_err(&vpfe_dev->v4l2_dev,
313                         "Error: requesting VINT1 interrupt\n");
314                 free_irq(vpfe_dev->ccdc_irq0, vpfe_dev);
315                 return ret;
316         }
317
318         ret = request_irq(vpfe_dev->imp_dma_irq, vpfe_imp_dma_isr,
319                           IRQF_DISABLED, "Imp_Sdram_Irq", vpfe_dev);
320         if (ret < 0) {
321                 v4l2_err(&vpfe_dev->v4l2_dev,
322                          "Error: requesting IMP IRQ interrupt\n");
323                 free_irq(vpfe_dev->ccdc_irq1, vpfe_dev);
324                 free_irq(vpfe_dev->ccdc_irq0, vpfe_dev);
325                 return ret;
326         }
327
328         return 0;
329 }
330
331 /*
332  * register_i2c_devices() - register all i2c v4l2 subdevs
333  * @vpfe_dev - ptr to vpfe capture device
334  *
335  * register all i2c v4l2 subdevs
336  */
337 static int register_i2c_devices(struct vpfe_device *vpfe_dev)
338 {
339         struct vpfe_ext_subdev_info *sdinfo;
340         struct vpfe_config *vpfe_cfg;
341         struct i2c_adapter *i2c_adap;
342         unsigned int num_subdevs;
343         int ret;
344         int i;
345         int k;
346
347         vpfe_cfg = vpfe_dev->cfg;
348         i2c_adap = i2c_get_adapter(1);
349         num_subdevs = vpfe_cfg->num_subdevs;
350         vpfe_dev->sd =
351                   kzalloc(sizeof(struct v4l2_subdev *)*num_subdevs, GFP_KERNEL);
352         if (vpfe_dev->sd == NULL) {
353                 v4l2_err(&vpfe_dev->v4l2_dev,
354                         "unable to allocate memory for subdevice\n");
355                 return -ENOMEM;
356         }
357
358         for (i = 0, k = 0; i < num_subdevs; i++) {
359                 sdinfo = &vpfe_cfg->sub_devs[i];
360                 /*
361                  * register subdevices based on interface setting. Currently
362                  * tvp5146 and mt9p031 cannot co-exists due to i2c address
363                  * conflicts. So only one of them is registered. Re-visit this
364                  * once we have support for i2c switch handling in i2c driver
365                  * framework
366                  */
367                 if (interface == sdinfo->is_camera) {
368                         /* setup input path */
369                         if (vpfe_cfg->setup_input &&
370                                 vpfe_cfg->setup_input(sdinfo->grp_id) < 0) {
371                                 ret = -EFAULT;
372                                 v4l2_info(&vpfe_dev->v4l2_dev,
373                                           "could not setup input for %s\n",
374                                                 sdinfo->module_name);
375                                 goto probe_sd_out;
376                         }
377                         /* Load up the subdevice */
378                         vpfe_dev->sd[k] =
379                                 v4l2_i2c_new_subdev_board(&vpfe_dev->v4l2_dev,
380                                                   i2c_adap, &sdinfo->board_info,
381                                                   NULL);
382                         if (vpfe_dev->sd[k]) {
383                                 v4l2_info(&vpfe_dev->v4l2_dev,
384                                                 "v4l2 sub device %s registered\n",
385                                                 sdinfo->module_name);
386
387                                 vpfe_dev->sd[k]->grp_id = sdinfo->grp_id;
388                                 k++;
389
390                                 sdinfo->registered = 1;
391                         }
392                 } else {
393                         v4l2_info(&vpfe_dev->v4l2_dev,
394                                   "v4l2 sub device %s is not registered\n",
395                                   sdinfo->module_name);
396                 }
397         }
398         vpfe_dev->num_ext_subdevs = k;
399
400         return 0;
401
402 probe_sd_out:
403         kzfree(vpfe_dev->sd);
404
405         return ret;
406 }
407
408 /*
409  * vpfe_register_entities() - register all v4l2 subdevs and media entities
410  * @vpfe_dev - ptr to vpfe capture device
411  *
412  * register all v4l2 subdevs, media entities, and creates links
413  * between entities
414  */
415 static int vpfe_register_entities(struct vpfe_device *vpfe_dev)
416 {
417         unsigned int flags = 0;
418         int ret;
419         int i;
420
421         /* register i2c devices first */
422         ret = register_i2c_devices(vpfe_dev);
423         if (ret)
424                 return ret;
425
426         /* register rest of the sub-devs */
427         ret = vpfe_isif_register_entities(&vpfe_dev->vpfe_isif,
428                                           &vpfe_dev->v4l2_dev);
429         if (ret)
430                 return ret;
431
432         ret = vpfe_ipipeif_register_entities(&vpfe_dev->vpfe_ipipeif,
433                                              &vpfe_dev->v4l2_dev);
434         if (ret)
435                 goto out_isif_register;
436
437         ret = vpfe_ipipe_register_entities(&vpfe_dev->vpfe_ipipe,
438                                            &vpfe_dev->v4l2_dev);
439         if (ret)
440                 goto out_ipipeif_register;
441
442         ret = vpfe_resizer_register_entities(&vpfe_dev->vpfe_resizer,
443                                              &vpfe_dev->v4l2_dev);
444         if (ret)
445                 goto out_ipipe_register;
446
447         /* create links now, starting with external(i2c) entities */
448         for (i = 0; i < vpfe_dev->num_ext_subdevs; i++)
449                 /* if entity has no pads (ex: amplifier),
450                    cant establish link */
451                 if (vpfe_dev->sd[i]->entity.num_pads) {
452                         ret = media_entity_create_link(&vpfe_dev->sd[i]->entity,
453                                 0, &vpfe_dev->vpfe_isif.subdev.entity,
454                                 0, flags);
455                         if (ret < 0)
456                                 goto out_resizer_register;
457                 }
458
459         ret = media_entity_create_link(&vpfe_dev->vpfe_isif.subdev.entity, 1,
460                                        &vpfe_dev->vpfe_ipipeif.subdev.entity,
461                                        0, flags);
462         if (ret < 0)
463                 goto out_resizer_register;
464
465         ret = media_entity_create_link(&vpfe_dev->vpfe_ipipeif.subdev.entity, 1,
466                                        &vpfe_dev->vpfe_ipipe.subdev.entity,
467                                        0, flags);
468         if (ret < 0)
469                 goto out_resizer_register;
470
471         ret = media_entity_create_link(&vpfe_dev->vpfe_ipipe.subdev.entity,
472                         1, &vpfe_dev->vpfe_resizer.crop_resizer.subdev.entity,
473                         0, flags);
474         if (ret < 0)
475                 goto out_resizer_register;
476
477         ret = media_entity_create_link(&vpfe_dev->vpfe_ipipeif.subdev.entity, 1,
478                         &vpfe_dev->vpfe_resizer.crop_resizer.subdev.entity,
479                         0, flags);
480         if (ret < 0)
481                 goto out_resizer_register;
482
483         ret = v4l2_device_register_subdev_nodes(&vpfe_dev->v4l2_dev);
484         if (ret < 0)
485                 goto out_resizer_register;
486
487         return 0;
488
489 out_resizer_register:
490         vpfe_resizer_unregister_entities(&vpfe_dev->vpfe_resizer);
491 out_ipipe_register:
492         vpfe_ipipe_unregister_entities(&vpfe_dev->vpfe_ipipe);
493 out_ipipeif_register:
494         vpfe_ipipeif_unregister_entities(&vpfe_dev->vpfe_ipipeif);
495 out_isif_register:
496         vpfe_isif_unregister_entities(&vpfe_dev->vpfe_isif);
497
498         return ret;
499 }
500
501 /*
502  * vpfe_unregister_entities() - unregister all v4l2 subdevs and media entities
503  * @vpfe_dev - ptr to vpfe capture device
504  *
505  * unregister all v4l2 subdevs and media entities
506  */
507 static void vpfe_unregister_entities(struct vpfe_device *vpfe_dev)
508 {
509         vpfe_isif_unregister_entities(&vpfe_dev->vpfe_isif);
510         vpfe_ipipeif_unregister_entities(&vpfe_dev->vpfe_ipipeif);
511         vpfe_ipipe_unregister_entities(&vpfe_dev->vpfe_ipipe);
512         vpfe_resizer_unregister_entities(&vpfe_dev->vpfe_resizer);
513 }
514
515 /*
516  * vpfe_cleanup_modules() - cleanup all non-i2c v4l2 subdevs
517  * @vpfe_dev - ptr to vpfe capture device
518  * @pdev - pointer to platform device
519  *
520  * cleanup all v4l2 subdevs
521  */
522 static void vpfe_cleanup_modules(struct vpfe_device *vpfe_dev,
523                                  struct platform_device *pdev)
524 {
525         vpfe_isif_cleanup(&vpfe_dev->vpfe_isif, pdev);
526         vpfe_ipipeif_cleanup(&vpfe_dev->vpfe_ipipeif, pdev);
527         vpfe_ipipe_cleanup(&vpfe_dev->vpfe_ipipe, pdev);
528         vpfe_resizer_cleanup(&vpfe_dev->vpfe_resizer, pdev);
529 }
530
531 /*
532  * vpfe_initialize_modules() - initialize all non-i2c v4l2 subdevs
533  * @vpfe_dev - ptr to vpfe capture device
534  * @pdev - pointer to platform device
535  *
536  * intialize all v4l2 subdevs and media entities
537  */
538 static int vpfe_initialize_modules(struct vpfe_device *vpfe_dev,
539                                    struct platform_device *pdev)
540 {
541         int ret;
542
543         ret = vpfe_isif_init(&vpfe_dev->vpfe_isif, pdev);
544         if (ret)
545                 return ret;
546
547         ret = vpfe_ipipeif_init(&vpfe_dev->vpfe_ipipeif, pdev);
548         if (ret)
549                 goto out_isif_init;
550
551         ret = vpfe_ipipe_init(&vpfe_dev->vpfe_ipipe, pdev);
552         if (ret)
553                 goto out_ipipeif_init;
554
555         ret = vpfe_resizer_init(&vpfe_dev->vpfe_resizer, pdev);
556         if (ret)
557                 goto out_ipipe_init;
558
559         return 0;
560
561 out_ipipe_init:
562         vpfe_ipipe_cleanup(&vpfe_dev->vpfe_ipipe, pdev);
563 out_ipipeif_init:
564         vpfe_ipipeif_cleanup(&vpfe_dev->vpfe_ipipeif, pdev);
565 out_isif_init:
566         vpfe_isif_cleanup(&vpfe_dev->vpfe_isif, pdev);
567
568         return ret;
569 }
570
571 /*
572  * vpfe_probe() : vpfe probe function
573  * @pdev: platform device pointer
574  *
575  * This function creates device entries by register itself to the V4L2 driver
576  * and initializes fields of each device objects
577  */
578 static int vpfe_probe(struct platform_device *pdev)
579 {
580         struct vpfe_device *vpfe_dev;
581         struct resource *res1;
582         int ret = -ENOMEM;
583
584         vpfe_dev = kzalloc(sizeof(*vpfe_dev), GFP_KERNEL);
585         if (!vpfe_dev) {
586                 v4l2_err(pdev->dev.driver,
587                         "Failed to allocate memory for vpfe_dev\n");
588                 return ret;
589         }
590
591         if (pdev->dev.platform_data == NULL) {
592                 v4l2_err(pdev->dev.driver, "Unable to get vpfe config\n");
593                 ret = -ENOENT;
594                 goto probe_free_dev_mem;
595         }
596
597         vpfe_dev->cfg = pdev->dev.platform_data;
598         if (vpfe_dev->cfg->card_name == NULL ||
599                         vpfe_dev->cfg->sub_devs == NULL) {
600                 v4l2_err(pdev->dev.driver, "null ptr in vpfe_cfg\n");
601                 ret = -ENOENT;
602                 goto probe_free_dev_mem;
603         }
604
605         /* Get VINT0 irq resource */
606         res1 = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
607         if (!res1) {
608                 v4l2_err(pdev->dev.driver,
609                          "Unable to get interrupt for VINT0\n");
610                 ret = -ENOENT;
611                 goto probe_free_dev_mem;
612         }
613         vpfe_dev->ccdc_irq0 = res1->start;
614
615         /* Get VINT1 irq resource */
616         res1 = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
617         if (!res1) {
618                 v4l2_err(pdev->dev.driver,
619                          "Unable to get interrupt for VINT1\n");
620                 ret = -ENOENT;
621                 goto probe_free_dev_mem;
622         }
623         vpfe_dev->ccdc_irq1 = res1->start;
624
625         /* Get DMA irq resource */
626         res1 = platform_get_resource(pdev, IORESOURCE_IRQ, 2);
627         if (!res1) {
628                 v4l2_err(pdev->dev.driver,
629                          "Unable to get interrupt for DMA\n");
630                 ret = -ENOENT;
631                 goto probe_free_dev_mem;
632         }
633         vpfe_dev->imp_dma_irq = res1->start;
634
635         vpfe_dev->pdev = &pdev->dev;
636
637         /* enable vpss clocks */
638         ret = vpfe_enable_clock(vpfe_dev);
639         if (ret)
640                 goto probe_free_dev_mem;
641
642         ret = vpfe_initialize_modules(vpfe_dev, pdev);
643         if (ret)
644                 goto probe_disable_clock;
645
646         vpfe_dev->media_dev.dev = vpfe_dev->pdev;
647         strcpy((char *)&vpfe_dev->media_dev.model, "davinci-media");
648
649         ret = media_device_register(&vpfe_dev->media_dev);
650         if (ret) {
651                 v4l2_err(pdev->dev.driver,
652                         "Unable to register media device.\n");
653                 goto probe_out_entities_cleanup;
654         }
655
656         vpfe_dev->v4l2_dev.mdev = &vpfe_dev->media_dev;
657         ret = v4l2_device_register(&pdev->dev, &vpfe_dev->v4l2_dev);
658         if (ret) {
659                 v4l2_err(pdev->dev.driver, "Unable to register v4l2 device.\n");
660                 goto probe_out_media_unregister;
661         }
662
663         v4l2_info(&vpfe_dev->v4l2_dev, "v4l2 device registered\n");
664         /* set the driver data in platform device */
665         platform_set_drvdata(pdev, vpfe_dev);
666         /* register subdevs/entities */
667         ret = vpfe_register_entities(vpfe_dev);
668         if (ret)
669                 goto probe_out_v4l2_unregister;
670
671         ret = vpfe_attach_irq(vpfe_dev);
672         if (ret)
673                 goto probe_out_entities_unregister;
674
675         return 0;
676
677 probe_out_entities_unregister:
678         vpfe_unregister_entities(vpfe_dev);
679         kzfree(vpfe_dev->sd);
680 probe_out_v4l2_unregister:
681         v4l2_device_unregister(&vpfe_dev->v4l2_dev);
682 probe_out_media_unregister:
683         media_device_unregister(&vpfe_dev->media_dev);
684 probe_out_entities_cleanup:
685         vpfe_cleanup_modules(vpfe_dev, pdev);
686 probe_disable_clock:
687         vpfe_disable_clock(vpfe_dev);
688 probe_free_dev_mem:
689         kzfree(vpfe_dev);
690
691         return ret;
692 }
693
694 /*
695  * vpfe_remove : This function un-registers device from V4L2 driver
696  */
697 static int vpfe_remove(struct platform_device *pdev)
698 {
699         struct vpfe_device *vpfe_dev = platform_get_drvdata(pdev);
700
701         v4l2_info(pdev->dev.driver, "vpfe_remove\n");
702
703         kzfree(vpfe_dev->sd);
704         vpfe_detach_irq(vpfe_dev);
705         vpfe_unregister_entities(vpfe_dev);
706         vpfe_cleanup_modules(vpfe_dev, pdev);
707         v4l2_device_unregister(&vpfe_dev->v4l2_dev);
708         media_device_unregister(&vpfe_dev->media_dev);
709         vpfe_disable_clock(vpfe_dev);
710         kzfree(vpfe_dev);
711
712         return 0;
713 }
714
715 static struct platform_driver vpfe_driver = {
716         .driver = {
717                 .name = CAPTURE_DRV_NAME,
718                 .owner = THIS_MODULE,
719         },
720         .probe = vpfe_probe,
721         .remove = vpfe_remove,
722 };
723
724 module_platform_driver(vpfe_driver);