]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/media/platform/am437x/am437x-vpfe.c
Merge tag 'pstore-v4.8' of git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux
[linux.git] / drivers / media / platform / am437x / am437x-vpfe.c
1 /*
2  * TI VPFE capture Driver
3  *
4  * Copyright (C) 2013 - 2014 Texas Instruments, Inc.
5  *
6  * Benoit Parrot <bparrot@ti.com>
7  * Lad, Prabhakar <prabhakar.csengg@gmail.com>
8  *
9  * This program is free software; you may redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; version 2 of the License.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
14  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
15  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
17  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
18  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE.
21  */
22
23 #include <linux/delay.h>
24 #include <linux/err.h>
25 #include <linux/init.h>
26 #include <linux/interrupt.h>
27 #include <linux/io.h>
28 #include <linux/module.h>
29 #include <linux/pinctrl/consumer.h>
30 #include <linux/platform_device.h>
31 #include <linux/pm_runtime.h>
32 #include <linux/slab.h>
33 #include <linux/uaccess.h>
34 #include <linux/videodev2.h>
35
36 #include <media/v4l2-common.h>
37 #include <media/v4l2-ctrls.h>
38 #include <media/v4l2-event.h>
39 #include <media/v4l2-of.h>
40
41 #include "am437x-vpfe.h"
42
43 #define VPFE_MODULE_NAME        "vpfe"
44 #define VPFE_VERSION            "0.1.0"
45
46 static int debug;
47 module_param(debug, int, 0644);
48 MODULE_PARM_DESC(debug, "Debug level 0-8");
49
50 #define vpfe_dbg(level, dev, fmt, arg...)       \
51                 v4l2_dbg(level, debug, &dev->v4l2_dev, fmt, ##arg)
52 #define vpfe_info(dev, fmt, arg...)     \
53                 v4l2_info(&dev->v4l2_dev, fmt, ##arg)
54 #define vpfe_err(dev, fmt, arg...)      \
55                 v4l2_err(&dev->v4l2_dev, fmt, ##arg)
56
57 /* standard information */
58 struct vpfe_standard {
59         v4l2_std_id std_id;
60         unsigned int width;
61         unsigned int height;
62         struct v4l2_fract pixelaspect;
63         int frame_format;
64 };
65
66 static const struct vpfe_standard vpfe_standards[] = {
67         {V4L2_STD_525_60, 720, 480, {11, 10}, 1},
68         {V4L2_STD_625_50, 720, 576, {54, 59}, 1},
69 };
70
71 struct bus_format {
72         unsigned int width;
73         unsigned int bpp;
74 };
75
76 /*
77  * struct vpfe_fmt - VPFE media bus format information
78  * @name: V4L2 format description
79  * @code: V4L2 media bus format code
80  * @shifted: V4L2 media bus format code for the same pixel layout but
81  *      shifted to be 8 bits per pixel. =0 if format is not shiftable.
82  * @pixelformat: V4L2 pixel format FCC identifier
83  * @width: Bits per pixel (when transferred over a bus)
84  * @bpp: Bytes per pixel (when stored in memory)
85  * @supported: Indicates format supported by subdev
86  */
87 struct vpfe_fmt {
88         const char *name;
89         u32 fourcc;
90         u32 code;
91         struct bus_format l;
92         struct bus_format s;
93         bool supported;
94         u32 index;
95 };
96
97 static struct vpfe_fmt formats[] = {
98         {
99                 .name           = "YUV 4:2:2 packed, YCbYCr",
100                 .fourcc         = V4L2_PIX_FMT_YUYV,
101                 .code           = MEDIA_BUS_FMT_YUYV8_2X8,
102                 .l.width        = 10,
103                 .l.bpp          = 4,
104                 .s.width        = 8,
105                 .s.bpp          = 2,
106                 .supported      = false,
107         }, {
108                 .name           = "YUV 4:2:2 packed, CbYCrY",
109                 .fourcc         = V4L2_PIX_FMT_UYVY,
110                 .code           = MEDIA_BUS_FMT_UYVY8_2X8,
111                 .l.width        = 10,
112                 .l.bpp          = 4,
113                 .s.width        = 8,
114                 .s.bpp          = 2,
115                 .supported      = false,
116         }, {
117                 .name           = "YUV 4:2:2 packed, YCrYCb",
118                 .fourcc         = V4L2_PIX_FMT_YVYU,
119                 .code           = MEDIA_BUS_FMT_YVYU8_2X8,
120                 .l.width        = 10,
121                 .l.bpp          = 4,
122                 .s.width        = 8,
123                 .s.bpp          = 2,
124                 .supported      = false,
125         }, {
126                 .name           = "YUV 4:2:2 packed, CrYCbY",
127                 .fourcc         = V4L2_PIX_FMT_VYUY,
128                 .code           = MEDIA_BUS_FMT_VYUY8_2X8,
129                 .l.width        = 10,
130                 .l.bpp          = 4,
131                 .s.width        = 8,
132                 .s.bpp          = 2,
133                 .supported      = false,
134         }, {
135                 .name           = "RAW8 BGGR",
136                 .fourcc         = V4L2_PIX_FMT_SBGGR8,
137                 .code           = MEDIA_BUS_FMT_SBGGR8_1X8,
138                 .l.width        = 10,
139                 .l.bpp          = 2,
140                 .s.width        = 8,
141                 .s.bpp          = 1,
142                 .supported      = false,
143         }, {
144                 .name           = "RAW8 GBRG",
145                 .fourcc         = V4L2_PIX_FMT_SGBRG8,
146                 .code           = MEDIA_BUS_FMT_SGBRG8_1X8,
147                 .l.width        = 10,
148                 .l.bpp          = 2,
149                 .s.width        = 8,
150                 .s.bpp          = 1,
151                 .supported      = false,
152         }, {
153                 .name           = "RAW8 GRBG",
154                 .fourcc         = V4L2_PIX_FMT_SGRBG8,
155                 .code           = MEDIA_BUS_FMT_SGRBG8_1X8,
156                 .l.width        = 10,
157                 .l.bpp          = 2,
158                 .s.width        = 8,
159                 .s.bpp          = 1,
160                 .supported      = false,
161         }, {
162                 .name           = "RAW8 RGGB",
163                 .fourcc         = V4L2_PIX_FMT_SRGGB8,
164                 .code           = MEDIA_BUS_FMT_SRGGB8_1X8,
165                 .l.width        = 10,
166                 .l.bpp          = 2,
167                 .s.width        = 8,
168                 .s.bpp          = 1,
169                 .supported      = false,
170         }, {
171                 .name           = "RGB565 (LE)",
172                 .fourcc         = V4L2_PIX_FMT_RGB565,
173                 .code           = MEDIA_BUS_FMT_RGB565_2X8_LE,
174                 .l.width        = 10,
175                 .l.bpp          = 4,
176                 .s.width        = 8,
177                 .s.bpp          = 2,
178                 .supported      = false,
179         }, {
180                 .name           = "RGB565 (BE)",
181                 .fourcc         = V4L2_PIX_FMT_RGB565X,
182                 .code           = MEDIA_BUS_FMT_RGB565_2X8_BE,
183                 .l.width        = 10,
184                 .l.bpp          = 4,
185                 .s.width        = 8,
186                 .s.bpp          = 2,
187                 .supported      = false,
188         },
189 };
190
191 static int
192 __vpfe_get_format(struct vpfe_device *vpfe,
193                   struct v4l2_format *format, unsigned int *bpp);
194
195 static struct vpfe_fmt *find_format_by_code(unsigned int code)
196 {
197         struct vpfe_fmt *fmt;
198         unsigned int k;
199
200         for (k = 0; k < ARRAY_SIZE(formats); k++) {
201                 fmt = &formats[k];
202                 if (fmt->code == code)
203                         return fmt;
204         }
205
206         return NULL;
207 }
208
209 static struct vpfe_fmt *find_format_by_pix(unsigned int pixelformat)
210 {
211         struct vpfe_fmt *fmt;
212         unsigned int k;
213
214         for (k = 0; k < ARRAY_SIZE(formats); k++) {
215                 fmt = &formats[k];
216                 if (fmt->fourcc == pixelformat)
217                         return fmt;
218         }
219
220         return NULL;
221 }
222
223 static void
224 mbus_to_pix(struct vpfe_device *vpfe,
225             const struct v4l2_mbus_framefmt *mbus,
226             struct v4l2_pix_format *pix, unsigned int *bpp)
227 {
228         struct vpfe_subdev_info *sdinfo = vpfe->current_subdev;
229         unsigned int bus_width = sdinfo->vpfe_param.bus_width;
230         struct vpfe_fmt *fmt;
231
232         fmt = find_format_by_code(mbus->code);
233         if (WARN_ON(fmt == NULL)) {
234                 pr_err("Invalid mbus code set\n");
235                 *bpp = 1;
236                 return;
237         }
238
239         memset(pix, 0, sizeof(*pix));
240         v4l2_fill_pix_format(pix, mbus);
241         pix->pixelformat = fmt->fourcc;
242         *bpp = (bus_width == 10) ?  fmt->l.bpp : fmt->s.bpp;
243
244         /* pitch should be 32 bytes aligned */
245         pix->bytesperline = ALIGN(pix->width * *bpp, 32);
246         pix->sizeimage = pix->bytesperline * pix->height;
247 }
248
249 static void pix_to_mbus(struct vpfe_device *vpfe,
250                         struct v4l2_pix_format *pix_fmt,
251                         struct v4l2_mbus_framefmt *mbus_fmt)
252 {
253         struct vpfe_fmt *fmt;
254
255         fmt = find_format_by_pix(pix_fmt->pixelformat);
256         if (!fmt) {
257                 /* default to first entry */
258                 vpfe_dbg(3, vpfe, "Invalid pixel code: %x, default used instead\n",
259                         pix_fmt->pixelformat);
260                 fmt = &formats[0];
261         }
262
263         memset(mbus_fmt, 0, sizeof(*mbus_fmt));
264         v4l2_fill_mbus_format(mbus_fmt, pix_fmt, fmt->code);
265 }
266
267 /*  Print Four-character-code (FOURCC) */
268 static char *print_fourcc(u32 fmt)
269 {
270         static char code[5];
271
272         code[0] = (unsigned char)(fmt & 0xff);
273         code[1] = (unsigned char)((fmt >> 8) & 0xff);
274         code[2] = (unsigned char)((fmt >> 16) & 0xff);
275         code[3] = (unsigned char)((fmt >> 24) & 0xff);
276         code[4] = '\0';
277
278         return code;
279 }
280
281 static int
282 cmp_v4l2_format(const struct v4l2_format *lhs, const struct v4l2_format *rhs)
283 {
284         return lhs->type == rhs->type &&
285                 lhs->fmt.pix.width == rhs->fmt.pix.width &&
286                 lhs->fmt.pix.height == rhs->fmt.pix.height &&
287                 lhs->fmt.pix.pixelformat == rhs->fmt.pix.pixelformat &&
288                 lhs->fmt.pix.field == rhs->fmt.pix.field &&
289                 lhs->fmt.pix.colorspace == rhs->fmt.pix.colorspace &&
290                 lhs->fmt.pix.ycbcr_enc == rhs->fmt.pix.ycbcr_enc &&
291                 lhs->fmt.pix.quantization == rhs->fmt.pix.quantization &&
292                 lhs->fmt.pix.xfer_func == rhs->fmt.pix.xfer_func;
293 }
294
295 static inline u32 vpfe_reg_read(struct vpfe_ccdc *ccdc, u32 offset)
296 {
297         return ioread32(ccdc->ccdc_cfg.base_addr + offset);
298 }
299
300 static inline void vpfe_reg_write(struct vpfe_ccdc *ccdc, u32 val, u32 offset)
301 {
302         iowrite32(val, ccdc->ccdc_cfg.base_addr + offset);
303 }
304
305 static inline struct vpfe_device *to_vpfe(struct vpfe_ccdc *ccdc)
306 {
307         return container_of(ccdc, struct vpfe_device, ccdc);
308 }
309
310 static inline
311 struct vpfe_cap_buffer *to_vpfe_buffer(struct vb2_v4l2_buffer *vb)
312 {
313         return container_of(vb, struct vpfe_cap_buffer, vb);
314 }
315
316 static inline void vpfe_pcr_enable(struct vpfe_ccdc *ccdc, int flag)
317 {
318         vpfe_reg_write(ccdc, !!flag, VPFE_PCR);
319 }
320
321 static void vpfe_config_enable(struct vpfe_ccdc *ccdc, int flag)
322 {
323         unsigned int cfg;
324
325         if (!flag) {
326                 cfg = vpfe_reg_read(ccdc, VPFE_CONFIG);
327                 cfg &= ~(VPFE_CONFIG_EN_ENABLE << VPFE_CONFIG_EN_SHIFT);
328         } else {
329                 cfg = VPFE_CONFIG_EN_ENABLE << VPFE_CONFIG_EN_SHIFT;
330         }
331
332         vpfe_reg_write(ccdc, cfg, VPFE_CONFIG);
333 }
334
335 static void vpfe_ccdc_setwin(struct vpfe_ccdc *ccdc,
336                              struct v4l2_rect *image_win,
337                              enum ccdc_frmfmt frm_fmt,
338                              int bpp)
339 {
340         int horz_start, horz_nr_pixels;
341         int vert_start, vert_nr_lines;
342         int val, mid_img;
343
344         /*
345          * ppc - per pixel count. indicates how many pixels per cell
346          * output to SDRAM. example, for ycbcr, it is one y and one c, so 2.
347          * raw capture this is 1
348          */
349         horz_start = image_win->left * bpp;
350         horz_nr_pixels = (image_win->width * bpp) - 1;
351         vpfe_reg_write(ccdc, (horz_start << VPFE_HORZ_INFO_SPH_SHIFT) |
352                                 horz_nr_pixels, VPFE_HORZ_INFO);
353
354         vert_start = image_win->top;
355
356         if (frm_fmt == CCDC_FRMFMT_INTERLACED) {
357                 vert_nr_lines = (image_win->height >> 1) - 1;
358                 vert_start >>= 1;
359                 /* Since first line doesn't have any data */
360                 vert_start += 1;
361                 /* configure VDINT0 */
362                 val = (vert_start << VPFE_VDINT_VDINT0_SHIFT);
363         } else {
364                 /* Since first line doesn't have any data */
365                 vert_start += 1;
366                 vert_nr_lines = image_win->height - 1;
367                 /*
368                  * configure VDINT0 and VDINT1. VDINT1 will be at half
369                  * of image height
370                  */
371                 mid_img = vert_start + (image_win->height / 2);
372                 val = (vert_start << VPFE_VDINT_VDINT0_SHIFT) |
373                                 (mid_img & VPFE_VDINT_VDINT1_MASK);
374         }
375
376         vpfe_reg_write(ccdc, val, VPFE_VDINT);
377
378         vpfe_reg_write(ccdc, (vert_start << VPFE_VERT_START_SLV0_SHIFT) |
379                                 vert_start, VPFE_VERT_START);
380         vpfe_reg_write(ccdc, vert_nr_lines, VPFE_VERT_LINES);
381 }
382
383 static void vpfe_reg_dump(struct vpfe_ccdc *ccdc)
384 {
385         struct vpfe_device *vpfe = to_vpfe(ccdc);
386
387         vpfe_dbg(3, vpfe, "ALAW: 0x%x\n", vpfe_reg_read(ccdc, VPFE_ALAW));
388         vpfe_dbg(3, vpfe, "CLAMP: 0x%x\n", vpfe_reg_read(ccdc, VPFE_CLAMP));
389         vpfe_dbg(3, vpfe, "DCSUB: 0x%x\n", vpfe_reg_read(ccdc, VPFE_DCSUB));
390         vpfe_dbg(3, vpfe, "BLKCMP: 0x%x\n", vpfe_reg_read(ccdc, VPFE_BLKCMP));
391         vpfe_dbg(3, vpfe, "COLPTN: 0x%x\n", vpfe_reg_read(ccdc, VPFE_COLPTN));
392         vpfe_dbg(3, vpfe, "SDOFST: 0x%x\n", vpfe_reg_read(ccdc, VPFE_SDOFST));
393         vpfe_dbg(3, vpfe, "SYN_MODE: 0x%x\n",
394                  vpfe_reg_read(ccdc, VPFE_SYNMODE));
395         vpfe_dbg(3, vpfe, "HSIZE_OFF: 0x%x\n",
396                  vpfe_reg_read(ccdc, VPFE_HSIZE_OFF));
397         vpfe_dbg(3, vpfe, "HORZ_INFO: 0x%x\n",
398                  vpfe_reg_read(ccdc, VPFE_HORZ_INFO));
399         vpfe_dbg(3, vpfe, "VERT_START: 0x%x\n",
400                  vpfe_reg_read(ccdc, VPFE_VERT_START));
401         vpfe_dbg(3, vpfe, "VERT_LINES: 0x%x\n",
402                  vpfe_reg_read(ccdc, VPFE_VERT_LINES));
403 }
404
405 static int
406 vpfe_ccdc_validate_param(struct vpfe_ccdc *ccdc,
407                          struct vpfe_ccdc_config_params_raw *ccdcparam)
408 {
409         struct vpfe_device *vpfe = to_vpfe(ccdc);
410         u8 max_gamma, max_data;
411
412         if (!ccdcparam->alaw.enable)
413                 return 0;
414
415         max_gamma = ccdc_gamma_width_max_bit(ccdcparam->alaw.gamma_wd);
416         max_data = ccdc_data_size_max_bit(ccdcparam->data_sz);
417
418         if (ccdcparam->alaw.gamma_wd > VPFE_CCDC_GAMMA_BITS_09_0 ||
419             ccdcparam->alaw.gamma_wd < VPFE_CCDC_GAMMA_BITS_15_6 ||
420             max_gamma > max_data) {
421                 vpfe_dbg(1, vpfe, "Invalid data line select\n");
422                 return -EINVAL;
423         }
424
425         return 0;
426 }
427
428 static void
429 vpfe_ccdc_update_raw_params(struct vpfe_ccdc *ccdc,
430                             struct vpfe_ccdc_config_params_raw *raw_params)
431 {
432         struct vpfe_ccdc_config_params_raw *config_params =
433                                 &ccdc->ccdc_cfg.bayer.config_params;
434
435         *config_params = *raw_params;
436 }
437
438 /*
439  * vpfe_ccdc_restore_defaults()
440  * This function will write defaults to all CCDC registers
441  */
442 static void vpfe_ccdc_restore_defaults(struct vpfe_ccdc *ccdc)
443 {
444         int i;
445
446         /* Disable CCDC */
447         vpfe_pcr_enable(ccdc, 0);
448
449         /* set all registers to default value */
450         for (i = 4; i <= 0x94; i += 4)
451                 vpfe_reg_write(ccdc, 0,  i);
452
453         vpfe_reg_write(ccdc, VPFE_NO_CULLING, VPFE_CULLING);
454         vpfe_reg_write(ccdc, VPFE_CCDC_GAMMA_BITS_11_2, VPFE_ALAW);
455 }
456
457 static int vpfe_ccdc_close(struct vpfe_ccdc *ccdc, struct device *dev)
458 {
459         int dma_cntl, i, pcr;
460
461         /* If the CCDC module is still busy wait for it to be done */
462         for (i = 0; i < 10; i++) {
463                 usleep_range(5000, 6000);
464                 pcr = vpfe_reg_read(ccdc, VPFE_PCR);
465                 if (!pcr)
466                         break;
467
468                 /* make sure it it is disabled */
469                 vpfe_pcr_enable(ccdc, 0);
470         }
471
472         /* Disable CCDC by resetting all register to default POR values */
473         vpfe_ccdc_restore_defaults(ccdc);
474
475         /* if DMA_CNTL overflow bit is set. Clear it
476          *  It appears to take a while for this to become quiescent ~20ms
477          */
478         for (i = 0; i < 10; i++) {
479                 dma_cntl = vpfe_reg_read(ccdc, VPFE_DMA_CNTL);
480                 if (!(dma_cntl & VPFE_DMA_CNTL_OVERFLOW))
481                         break;
482
483                 /* Clear the overflow bit */
484                 vpfe_reg_write(ccdc, dma_cntl, VPFE_DMA_CNTL);
485                 usleep_range(5000, 6000);
486         }
487
488         /* Disabled the module at the CONFIG level */
489         vpfe_config_enable(ccdc, 0);
490
491         pm_runtime_put_sync(dev);
492
493         return 0;
494 }
495
496 static int vpfe_ccdc_set_params(struct vpfe_ccdc *ccdc, void __user *params)
497 {
498         struct vpfe_device *vpfe = container_of(ccdc, struct vpfe_device, ccdc);
499         struct vpfe_ccdc_config_params_raw raw_params;
500         int x;
501
502         if (ccdc->ccdc_cfg.if_type != VPFE_RAW_BAYER)
503                 return -EINVAL;
504
505         x = copy_from_user(&raw_params, params, sizeof(raw_params));
506         if (x) {
507                 vpfe_dbg(1, vpfe,
508                         "vpfe_ccdc_set_params: error in copying ccdc params, %d\n",
509                         x);
510                 return -EFAULT;
511         }
512
513         if (!vpfe_ccdc_validate_param(ccdc, &raw_params)) {
514                 vpfe_ccdc_update_raw_params(ccdc, &raw_params);
515                 return 0;
516         }
517
518         return -EINVAL;
519 }
520
521 /*
522  * vpfe_ccdc_config_ycbcr()
523  * This function will configure CCDC for YCbCr video capture
524  */
525 static void vpfe_ccdc_config_ycbcr(struct vpfe_ccdc *ccdc)
526 {
527         struct vpfe_device *vpfe = container_of(ccdc, struct vpfe_device, ccdc);
528         struct ccdc_params_ycbcr *params = &ccdc->ccdc_cfg.ycbcr;
529         u32 syn_mode;
530
531         vpfe_dbg(3, vpfe, "vpfe_ccdc_config_ycbcr:\n");
532         /*
533          * first restore the CCDC registers to default values
534          * This is important since we assume default values to be set in
535          * a lot of registers that we didn't touch
536          */
537         vpfe_ccdc_restore_defaults(ccdc);
538
539         /*
540          * configure pixel format, frame format, configure video frame
541          * format, enable output to SDRAM, enable internal timing generator
542          * and 8bit pack mode
543          */
544         syn_mode = (((params->pix_fmt & VPFE_SYN_MODE_INPMOD_MASK) <<
545                     VPFE_SYN_MODE_INPMOD_SHIFT) |
546                     ((params->frm_fmt & VPFE_SYN_FLDMODE_MASK) <<
547                     VPFE_SYN_FLDMODE_SHIFT) | VPFE_VDHDEN_ENABLE |
548                     VPFE_WEN_ENABLE | VPFE_DATA_PACK_ENABLE);
549
550         /* setup BT.656 sync mode */
551         if (params->bt656_enable) {
552                 vpfe_reg_write(ccdc, VPFE_REC656IF_BT656_EN, VPFE_REC656IF);
553
554                 /*
555                  * configure the FID, VD, HD pin polarity,
556                  * fld,hd pol positive, vd negative, 8-bit data
557                  */
558                 syn_mode |= VPFE_SYN_MODE_VD_POL_NEGATIVE;
559                 if (ccdc->ccdc_cfg.if_type == VPFE_BT656_10BIT)
560                         syn_mode |= VPFE_SYN_MODE_10BITS;
561                 else
562                         syn_mode |= VPFE_SYN_MODE_8BITS;
563         } else {
564                 /* y/c external sync mode */
565                 syn_mode |= (((params->fid_pol & VPFE_FID_POL_MASK) <<
566                              VPFE_FID_POL_SHIFT) |
567                              ((params->hd_pol & VPFE_HD_POL_MASK) <<
568                              VPFE_HD_POL_SHIFT) |
569                              ((params->vd_pol & VPFE_VD_POL_MASK) <<
570                              VPFE_VD_POL_SHIFT));
571         }
572         vpfe_reg_write(ccdc, syn_mode, VPFE_SYNMODE);
573
574         /* configure video window */
575         vpfe_ccdc_setwin(ccdc, &params->win,
576                          params->frm_fmt, params->bytesperpixel);
577
578         /*
579          * configure the order of y cb cr in SDRAM, and disable latch
580          * internal register on vsync
581          */
582         if (ccdc->ccdc_cfg.if_type == VPFE_BT656_10BIT)
583                 vpfe_reg_write(ccdc,
584                                (params->pix_order << VPFE_CCDCFG_Y8POS_SHIFT) |
585                                VPFE_LATCH_ON_VSYNC_DISABLE |
586                                VPFE_CCDCFG_BW656_10BIT, VPFE_CCDCFG);
587         else
588                 vpfe_reg_write(ccdc,
589                                (params->pix_order << VPFE_CCDCFG_Y8POS_SHIFT) |
590                                VPFE_LATCH_ON_VSYNC_DISABLE, VPFE_CCDCFG);
591
592         /*
593          * configure the horizontal line offset. This should be a
594          * on 32 byte boundary. So clear LSB 5 bits
595          */
596         vpfe_reg_write(ccdc, params->bytesperline, VPFE_HSIZE_OFF);
597
598         /* configure the memory line offset */
599         if (params->buf_type == CCDC_BUFTYPE_FLD_INTERLEAVED)
600                 /* two fields are interleaved in memory */
601                 vpfe_reg_write(ccdc, VPFE_SDOFST_FIELD_INTERLEAVED,
602                                VPFE_SDOFST);
603 }
604
605 static void
606 vpfe_ccdc_config_black_clamp(struct vpfe_ccdc *ccdc,
607                              struct vpfe_ccdc_black_clamp *bclamp)
608 {
609         u32 val;
610
611         if (!bclamp->enable) {
612                 /* configure DCSub */
613                 val = (bclamp->dc_sub) & VPFE_BLK_DC_SUB_MASK;
614                 vpfe_reg_write(ccdc, val, VPFE_DCSUB);
615                 vpfe_reg_write(ccdc, VPFE_CLAMP_DEFAULT_VAL, VPFE_CLAMP);
616                 return;
617         }
618         /*
619          * Configure gain,  Start pixel, No of line to be avg,
620          * No of pixel/line to be avg, & Enable the Black clamping
621          */
622         val = ((bclamp->sgain & VPFE_BLK_SGAIN_MASK) |
623                ((bclamp->start_pixel & VPFE_BLK_ST_PXL_MASK) <<
624                 VPFE_BLK_ST_PXL_SHIFT) |
625                ((bclamp->sample_ln & VPFE_BLK_SAMPLE_LINE_MASK) <<
626                 VPFE_BLK_SAMPLE_LINE_SHIFT) |
627                ((bclamp->sample_pixel & VPFE_BLK_SAMPLE_LN_MASK) <<
628                 VPFE_BLK_SAMPLE_LN_SHIFT) | VPFE_BLK_CLAMP_ENABLE);
629         vpfe_reg_write(ccdc, val, VPFE_CLAMP);
630         /* If Black clamping is enable then make dcsub 0 */
631         vpfe_reg_write(ccdc, VPFE_DCSUB_DEFAULT_VAL, VPFE_DCSUB);
632 }
633
634 static void
635 vpfe_ccdc_config_black_compense(struct vpfe_ccdc *ccdc,
636                                 struct vpfe_ccdc_black_compensation *bcomp)
637 {
638         u32 val;
639
640         val = ((bcomp->b & VPFE_BLK_COMP_MASK) |
641               ((bcomp->gb & VPFE_BLK_COMP_MASK) <<
642                VPFE_BLK_COMP_GB_COMP_SHIFT) |
643               ((bcomp->gr & VPFE_BLK_COMP_MASK) <<
644                VPFE_BLK_COMP_GR_COMP_SHIFT) |
645               ((bcomp->r & VPFE_BLK_COMP_MASK) <<
646                VPFE_BLK_COMP_R_COMP_SHIFT));
647         vpfe_reg_write(ccdc, val, VPFE_BLKCMP);
648 }
649
650 /*
651  * vpfe_ccdc_config_raw()
652  * This function will configure CCDC for Raw capture mode
653  */
654 static void vpfe_ccdc_config_raw(struct vpfe_ccdc *ccdc)
655 {
656         struct vpfe_device *vpfe = container_of(ccdc, struct vpfe_device, ccdc);
657         struct vpfe_ccdc_config_params_raw *config_params =
658                                 &ccdc->ccdc_cfg.bayer.config_params;
659         struct ccdc_params_raw *params = &ccdc->ccdc_cfg.bayer;
660         unsigned int syn_mode;
661         unsigned int val;
662
663         vpfe_dbg(3, vpfe, "vpfe_ccdc_config_raw:\n");
664
665         /* Reset CCDC */
666         vpfe_ccdc_restore_defaults(ccdc);
667
668         /* Disable latching function registers on VSYNC  */
669         vpfe_reg_write(ccdc, VPFE_LATCH_ON_VSYNC_DISABLE, VPFE_CCDCFG);
670
671         /*
672          * Configure the vertical sync polarity(SYN_MODE.VDPOL),
673          * horizontal sync polarity (SYN_MODE.HDPOL), frame id polarity
674          * (SYN_MODE.FLDPOL), frame format(progressive or interlace),
675          * data size(SYNMODE.DATSIZ), &pixel format (Input mode), output
676          * SDRAM, enable internal timing generator
677          */
678         syn_mode = (((params->vd_pol & VPFE_VD_POL_MASK) << VPFE_VD_POL_SHIFT) |
679                    ((params->hd_pol & VPFE_HD_POL_MASK) << VPFE_HD_POL_SHIFT) |
680                    ((params->fid_pol & VPFE_FID_POL_MASK) <<
681                    VPFE_FID_POL_SHIFT) | ((params->frm_fmt &
682                    VPFE_FRM_FMT_MASK) << VPFE_FRM_FMT_SHIFT) |
683                    ((config_params->data_sz & VPFE_DATA_SZ_MASK) <<
684                    VPFE_DATA_SZ_SHIFT) | ((params->pix_fmt &
685                    VPFE_PIX_FMT_MASK) << VPFE_PIX_FMT_SHIFT) |
686                    VPFE_WEN_ENABLE | VPFE_VDHDEN_ENABLE);
687
688         /* Enable and configure aLaw register if needed */
689         if (config_params->alaw.enable) {
690                 val = ((config_params->alaw.gamma_wd &
691                       VPFE_ALAW_GAMMA_WD_MASK) | VPFE_ALAW_ENABLE);
692                 vpfe_reg_write(ccdc, val, VPFE_ALAW);
693                 vpfe_dbg(3, vpfe, "\nWriting 0x%x to ALAW...\n", val);
694         }
695
696         /* Configure video window */
697         vpfe_ccdc_setwin(ccdc, &params->win, params->frm_fmt,
698                          params->bytesperpixel);
699
700         /* Configure Black Clamp */
701         vpfe_ccdc_config_black_clamp(ccdc, &config_params->blk_clamp);
702
703         /* Configure Black level compensation */
704         vpfe_ccdc_config_black_compense(ccdc, &config_params->blk_comp);
705
706         /* If data size is 8 bit then pack the data */
707         if ((config_params->data_sz == VPFE_CCDC_DATA_8BITS) ||
708             config_params->alaw.enable)
709                 syn_mode |= VPFE_DATA_PACK_ENABLE;
710
711         /*
712          * Configure Horizontal offset register. If pack 8 is enabled then
713          * 1 pixel will take 1 byte
714          */
715         vpfe_reg_write(ccdc, params->bytesperline, VPFE_HSIZE_OFF);
716
717         vpfe_dbg(3, vpfe, "Writing %d (%x) to HSIZE_OFF\n",
718                 params->bytesperline, params->bytesperline);
719
720         /* Set value for SDOFST */
721         if (params->frm_fmt == CCDC_FRMFMT_INTERLACED) {
722                 if (params->image_invert_enable) {
723                         /* For interlace inverse mode */
724                         vpfe_reg_write(ccdc, VPFE_INTERLACED_IMAGE_INVERT,
725                                    VPFE_SDOFST);
726                 } else {
727                         /* For interlace non inverse mode */
728                         vpfe_reg_write(ccdc, VPFE_INTERLACED_NO_IMAGE_INVERT,
729                                    VPFE_SDOFST);
730                 }
731         } else if (params->frm_fmt == CCDC_FRMFMT_PROGRESSIVE) {
732                 vpfe_reg_write(ccdc, VPFE_PROGRESSIVE_NO_IMAGE_INVERT,
733                            VPFE_SDOFST);
734         }
735
736         vpfe_reg_write(ccdc, syn_mode, VPFE_SYNMODE);
737
738         vpfe_reg_dump(ccdc);
739 }
740
741 static inline int
742 vpfe_ccdc_set_buftype(struct vpfe_ccdc *ccdc,
743                       enum ccdc_buftype buf_type)
744 {
745         if (ccdc->ccdc_cfg.if_type == VPFE_RAW_BAYER)
746                 ccdc->ccdc_cfg.bayer.buf_type = buf_type;
747         else
748                 ccdc->ccdc_cfg.ycbcr.buf_type = buf_type;
749
750         return 0;
751 }
752
753 static inline enum ccdc_buftype vpfe_ccdc_get_buftype(struct vpfe_ccdc *ccdc)
754 {
755         if (ccdc->ccdc_cfg.if_type == VPFE_RAW_BAYER)
756                 return ccdc->ccdc_cfg.bayer.buf_type;
757
758         return ccdc->ccdc_cfg.ycbcr.buf_type;
759 }
760
761 static int vpfe_ccdc_set_pixel_format(struct vpfe_ccdc *ccdc, u32 pixfmt)
762 {
763         struct vpfe_device *vpfe = container_of(ccdc, struct vpfe_device, ccdc);
764
765         vpfe_dbg(1, vpfe, "vpfe_ccdc_set_pixel_format: if_type: %d, pixfmt:%s\n",
766                  ccdc->ccdc_cfg.if_type, print_fourcc(pixfmt));
767
768         if (ccdc->ccdc_cfg.if_type == VPFE_RAW_BAYER) {
769                 ccdc->ccdc_cfg.bayer.pix_fmt = CCDC_PIXFMT_RAW;
770                 /*
771                  * Need to clear it in case it was left on
772                  * after the last capture.
773                  */
774                 ccdc->ccdc_cfg.bayer.config_params.alaw.enable = 0;
775
776                 switch (pixfmt) {
777                 case V4L2_PIX_FMT_SBGGR8:
778                         ccdc->ccdc_cfg.bayer.config_params.alaw.enable = 1;
779                         break;
780
781                 case V4L2_PIX_FMT_YUYV:
782                 case V4L2_PIX_FMT_UYVY:
783                 case V4L2_PIX_FMT_YUV420:
784                 case V4L2_PIX_FMT_NV12:
785                 case V4L2_PIX_FMT_RGB565X:
786                         break;
787
788                 case V4L2_PIX_FMT_SBGGR16:
789                 default:
790                         return -EINVAL;
791                 }
792         } else {
793                 switch (pixfmt) {
794                 case V4L2_PIX_FMT_YUYV:
795                         ccdc->ccdc_cfg.ycbcr.pix_order = CCDC_PIXORDER_YCBYCR;
796                         break;
797
798                 case V4L2_PIX_FMT_UYVY:
799                         ccdc->ccdc_cfg.ycbcr.pix_order = CCDC_PIXORDER_CBYCRY;
800                         break;
801
802                 default:
803                         return -EINVAL;
804                 }
805         }
806
807         return 0;
808 }
809
810 static u32 vpfe_ccdc_get_pixel_format(struct vpfe_ccdc *ccdc)
811 {
812         u32 pixfmt;
813
814         if (ccdc->ccdc_cfg.if_type == VPFE_RAW_BAYER) {
815                 pixfmt = V4L2_PIX_FMT_YUYV;
816         } else {
817                 if (ccdc->ccdc_cfg.ycbcr.pix_order == CCDC_PIXORDER_YCBYCR)
818                         pixfmt = V4L2_PIX_FMT_YUYV;
819                 else
820                         pixfmt = V4L2_PIX_FMT_UYVY;
821         }
822
823         return pixfmt;
824 }
825
826 static int
827 vpfe_ccdc_set_image_window(struct vpfe_ccdc *ccdc,
828                            struct v4l2_rect *win, unsigned int bpp)
829 {
830         if (ccdc->ccdc_cfg.if_type == VPFE_RAW_BAYER) {
831                 ccdc->ccdc_cfg.bayer.win = *win;
832                 ccdc->ccdc_cfg.bayer.bytesperpixel = bpp;
833                 ccdc->ccdc_cfg.bayer.bytesperline = ALIGN(win->width * bpp, 32);
834         } else {
835                 ccdc->ccdc_cfg.ycbcr.win = *win;
836                 ccdc->ccdc_cfg.ycbcr.bytesperpixel = bpp;
837                 ccdc->ccdc_cfg.ycbcr.bytesperline = ALIGN(win->width * bpp, 32);
838         }
839
840         return 0;
841 }
842
843 static inline void
844 vpfe_ccdc_get_image_window(struct vpfe_ccdc *ccdc,
845                            struct v4l2_rect *win)
846 {
847         if (ccdc->ccdc_cfg.if_type == VPFE_RAW_BAYER)
848                 *win = ccdc->ccdc_cfg.bayer.win;
849         else
850                 *win = ccdc->ccdc_cfg.ycbcr.win;
851 }
852
853 static inline unsigned int vpfe_ccdc_get_line_length(struct vpfe_ccdc *ccdc)
854 {
855         if (ccdc->ccdc_cfg.if_type == VPFE_RAW_BAYER)
856                 return ccdc->ccdc_cfg.bayer.bytesperline;
857
858         return ccdc->ccdc_cfg.ycbcr.bytesperline;
859 }
860
861 static inline int
862 vpfe_ccdc_set_frame_format(struct vpfe_ccdc *ccdc,
863                            enum ccdc_frmfmt frm_fmt)
864 {
865         if (ccdc->ccdc_cfg.if_type == VPFE_RAW_BAYER)
866                 ccdc->ccdc_cfg.bayer.frm_fmt = frm_fmt;
867         else
868                 ccdc->ccdc_cfg.ycbcr.frm_fmt = frm_fmt;
869
870         return 0;
871 }
872
873 static inline enum ccdc_frmfmt
874 vpfe_ccdc_get_frame_format(struct vpfe_ccdc *ccdc)
875 {
876         if (ccdc->ccdc_cfg.if_type == VPFE_RAW_BAYER)
877                 return ccdc->ccdc_cfg.bayer.frm_fmt;
878
879         return ccdc->ccdc_cfg.ycbcr.frm_fmt;
880 }
881
882 static inline int vpfe_ccdc_getfid(struct vpfe_ccdc *ccdc)
883 {
884         return (vpfe_reg_read(ccdc, VPFE_SYNMODE) >> 15) & 1;
885 }
886
887 static inline void vpfe_set_sdr_addr(struct vpfe_ccdc *ccdc, unsigned long addr)
888 {
889         vpfe_reg_write(ccdc, addr & 0xffffffe0, VPFE_SDR_ADDR);
890 }
891
892 static int vpfe_ccdc_set_hw_if_params(struct vpfe_ccdc *ccdc,
893                                       struct vpfe_hw_if_param *params)
894 {
895         struct vpfe_device *vpfe = container_of(ccdc, struct vpfe_device, ccdc);
896
897         ccdc->ccdc_cfg.if_type = params->if_type;
898
899         switch (params->if_type) {
900         case VPFE_BT656:
901         case VPFE_YCBCR_SYNC_16:
902         case VPFE_YCBCR_SYNC_8:
903         case VPFE_BT656_10BIT:
904                 ccdc->ccdc_cfg.ycbcr.vd_pol = params->vdpol;
905                 ccdc->ccdc_cfg.ycbcr.hd_pol = params->hdpol;
906                 break;
907
908         case VPFE_RAW_BAYER:
909                 ccdc->ccdc_cfg.bayer.vd_pol = params->vdpol;
910                 ccdc->ccdc_cfg.bayer.hd_pol = params->hdpol;
911                 if (params->bus_width == 10)
912                         ccdc->ccdc_cfg.bayer.config_params.data_sz =
913                                 VPFE_CCDC_DATA_10BITS;
914                 else
915                         ccdc->ccdc_cfg.bayer.config_params.data_sz =
916                                 VPFE_CCDC_DATA_8BITS;
917                 vpfe_dbg(1, vpfe, "params.bus_width: %d\n",
918                         params->bus_width);
919                 vpfe_dbg(1, vpfe, "config_params.data_sz: %d\n",
920                         ccdc->ccdc_cfg.bayer.config_params.data_sz);
921                 break;
922
923         default:
924                 return -EINVAL;
925         }
926
927         return 0;
928 }
929
930 static void vpfe_clear_intr(struct vpfe_ccdc *ccdc, int vdint)
931 {
932         unsigned int vpfe_int_status;
933
934         vpfe_int_status = vpfe_reg_read(ccdc, VPFE_IRQ_STS);
935
936         switch (vdint) {
937         /* VD0 interrupt */
938         case VPFE_VDINT0:
939                 vpfe_int_status &= ~VPFE_VDINT0;
940                 vpfe_int_status |= VPFE_VDINT0;
941                 break;
942
943         /* VD1 interrupt */
944         case VPFE_VDINT1:
945                 vpfe_int_status &= ~VPFE_VDINT1;
946                 vpfe_int_status |= VPFE_VDINT1;
947                 break;
948
949         /* VD2 interrupt */
950         case VPFE_VDINT2:
951                 vpfe_int_status &= ~VPFE_VDINT2;
952                 vpfe_int_status |= VPFE_VDINT2;
953                 break;
954
955         /* Clear all interrupts */
956         default:
957                 vpfe_int_status &= ~(VPFE_VDINT0 |
958                                 VPFE_VDINT1 |
959                                 VPFE_VDINT2);
960                 vpfe_int_status |= (VPFE_VDINT0 |
961                                 VPFE_VDINT1 |
962                                 VPFE_VDINT2);
963                 break;
964         }
965         /* Clear specific VDINT from the status register */
966         vpfe_reg_write(ccdc, vpfe_int_status, VPFE_IRQ_STS);
967
968         vpfe_int_status = vpfe_reg_read(ccdc, VPFE_IRQ_STS);
969
970         /* Acknowledge that we are done with all interrupts */
971         vpfe_reg_write(ccdc, 1, VPFE_IRQ_EOI);
972 }
973
974 static void vpfe_ccdc_config_defaults(struct vpfe_ccdc *ccdc)
975 {
976         ccdc->ccdc_cfg.if_type = VPFE_RAW_BAYER;
977
978         ccdc->ccdc_cfg.ycbcr.pix_fmt = CCDC_PIXFMT_YCBCR_8BIT;
979         ccdc->ccdc_cfg.ycbcr.frm_fmt = CCDC_FRMFMT_INTERLACED;
980         ccdc->ccdc_cfg.ycbcr.fid_pol = VPFE_PINPOL_POSITIVE;
981         ccdc->ccdc_cfg.ycbcr.vd_pol = VPFE_PINPOL_POSITIVE;
982         ccdc->ccdc_cfg.ycbcr.hd_pol = VPFE_PINPOL_POSITIVE;
983         ccdc->ccdc_cfg.ycbcr.pix_order = CCDC_PIXORDER_CBYCRY;
984         ccdc->ccdc_cfg.ycbcr.buf_type = CCDC_BUFTYPE_FLD_INTERLEAVED;
985
986         ccdc->ccdc_cfg.ycbcr.win.left = 0;
987         ccdc->ccdc_cfg.ycbcr.win.top = 0;
988         ccdc->ccdc_cfg.ycbcr.win.width = 720;
989         ccdc->ccdc_cfg.ycbcr.win.height = 576;
990         ccdc->ccdc_cfg.ycbcr.bt656_enable = 1;
991
992         ccdc->ccdc_cfg.bayer.pix_fmt = CCDC_PIXFMT_RAW;
993         ccdc->ccdc_cfg.bayer.frm_fmt = CCDC_FRMFMT_PROGRESSIVE;
994         ccdc->ccdc_cfg.bayer.fid_pol = VPFE_PINPOL_POSITIVE;
995         ccdc->ccdc_cfg.bayer.vd_pol = VPFE_PINPOL_POSITIVE;
996         ccdc->ccdc_cfg.bayer.hd_pol = VPFE_PINPOL_POSITIVE;
997
998         ccdc->ccdc_cfg.bayer.win.left = 0;
999         ccdc->ccdc_cfg.bayer.win.top = 0;
1000         ccdc->ccdc_cfg.bayer.win.width = 800;
1001         ccdc->ccdc_cfg.bayer.win.height = 600;
1002         ccdc->ccdc_cfg.bayer.config_params.data_sz = VPFE_CCDC_DATA_8BITS;
1003         ccdc->ccdc_cfg.bayer.config_params.alaw.gamma_wd =
1004                                                 VPFE_CCDC_GAMMA_BITS_09_0;
1005 }
1006
1007 /*
1008  * vpfe_get_ccdc_image_format - Get image parameters based on CCDC settings
1009  */
1010 static int vpfe_get_ccdc_image_format(struct vpfe_device *vpfe,
1011                                       struct v4l2_format *f)
1012 {
1013         struct v4l2_rect image_win;
1014         enum ccdc_buftype buf_type;
1015         enum ccdc_frmfmt frm_fmt;
1016
1017         memset(f, 0, sizeof(*f));
1018         f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1019         vpfe_ccdc_get_image_window(&vpfe->ccdc, &image_win);
1020         f->fmt.pix.width = image_win.width;
1021         f->fmt.pix.height = image_win.height;
1022         f->fmt.pix.bytesperline = vpfe_ccdc_get_line_length(&vpfe->ccdc);
1023         f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
1024                                 f->fmt.pix.height;
1025         buf_type = vpfe_ccdc_get_buftype(&vpfe->ccdc);
1026         f->fmt.pix.pixelformat = vpfe_ccdc_get_pixel_format(&vpfe->ccdc);
1027         frm_fmt = vpfe_ccdc_get_frame_format(&vpfe->ccdc);
1028
1029         if (frm_fmt == CCDC_FRMFMT_PROGRESSIVE) {
1030                 f->fmt.pix.field = V4L2_FIELD_NONE;
1031         } else if (frm_fmt == CCDC_FRMFMT_INTERLACED) {
1032                 if (buf_type == CCDC_BUFTYPE_FLD_INTERLEAVED) {
1033                         f->fmt.pix.field = V4L2_FIELD_INTERLACED;
1034                  } else if (buf_type == CCDC_BUFTYPE_FLD_SEPARATED) {
1035                         f->fmt.pix.field = V4L2_FIELD_SEQ_TB;
1036                 } else {
1037                         vpfe_err(vpfe, "Invalid buf_type\n");
1038                         return -EINVAL;
1039                 }
1040         } else {
1041                 vpfe_err(vpfe, "Invalid frm_fmt\n");
1042                 return -EINVAL;
1043         }
1044         return 0;
1045 }
1046
1047 static int vpfe_config_ccdc_image_format(struct vpfe_device *vpfe)
1048 {
1049         enum ccdc_frmfmt frm_fmt = CCDC_FRMFMT_INTERLACED;
1050         int ret = 0;
1051
1052         vpfe_dbg(2, vpfe, "vpfe_config_ccdc_image_format\n");
1053
1054         vpfe_dbg(1, vpfe, "pixelformat: %s\n",
1055                 print_fourcc(vpfe->fmt.fmt.pix.pixelformat));
1056
1057         if (vpfe_ccdc_set_pixel_format(&vpfe->ccdc,
1058                         vpfe->fmt.fmt.pix.pixelformat) < 0) {
1059                 vpfe_err(vpfe, "couldn't set pix format in ccdc\n");
1060                 return -EINVAL;
1061         }
1062
1063         /* configure the image window */
1064         vpfe_ccdc_set_image_window(&vpfe->ccdc, &vpfe->crop, vpfe->bpp);
1065
1066         switch (vpfe->fmt.fmt.pix.field) {
1067         case V4L2_FIELD_INTERLACED:
1068                 /* do nothing, since it is default */
1069                 ret = vpfe_ccdc_set_buftype(
1070                                 &vpfe->ccdc,
1071                                 CCDC_BUFTYPE_FLD_INTERLEAVED);
1072                 break;
1073
1074         case V4L2_FIELD_NONE:
1075                 frm_fmt = CCDC_FRMFMT_PROGRESSIVE;
1076                 /* buffer type only applicable for interlaced scan */
1077                 break;
1078
1079         case V4L2_FIELD_SEQ_TB:
1080                 ret = vpfe_ccdc_set_buftype(
1081                                 &vpfe->ccdc,
1082                                 CCDC_BUFTYPE_FLD_SEPARATED);
1083                 break;
1084
1085         default:
1086                 return -EINVAL;
1087         }
1088
1089         if (ret)
1090                 return ret;
1091
1092         return vpfe_ccdc_set_frame_format(&vpfe->ccdc, frm_fmt);
1093 }
1094
1095 /*
1096  * vpfe_config_image_format()
1097  * For a given standard, this functions sets up the default
1098  * pix format & crop values in the vpfe device and ccdc.  It first
1099  * starts with defaults based values from the standard table.
1100  * It then checks if sub device supports get_fmt and then override the
1101  * values based on that.Sets crop values to match with scan resolution
1102  * starting at 0,0. It calls vpfe_config_ccdc_image_format() set the
1103  * values in ccdc
1104  */
1105 static int vpfe_config_image_format(struct vpfe_device *vpfe,
1106                                     v4l2_std_id std_id)
1107 {
1108         struct v4l2_pix_format *pix = &vpfe->fmt.fmt.pix;
1109         int i, ret;
1110
1111         for (i = 0; i < ARRAY_SIZE(vpfe_standards); i++) {
1112                 if (vpfe_standards[i].std_id & std_id) {
1113                         vpfe->std_info.active_pixels =
1114                                         vpfe_standards[i].width;
1115                         vpfe->std_info.active_lines =
1116                                         vpfe_standards[i].height;
1117                         vpfe->std_info.frame_format =
1118                                         vpfe_standards[i].frame_format;
1119                         vpfe->std_index = i;
1120
1121                         break;
1122                 }
1123         }
1124
1125         if (i ==  ARRAY_SIZE(vpfe_standards)) {
1126                 vpfe_err(vpfe, "standard not supported\n");
1127                 return -EINVAL;
1128         }
1129
1130         vpfe->crop.top = vpfe->crop.left = 0;
1131         vpfe->crop.width = vpfe->std_info.active_pixels;
1132         vpfe->crop.height = vpfe->std_info.active_lines;
1133         pix->width = vpfe->crop.width;
1134         pix->height = vpfe->crop.height;
1135         pix->pixelformat = V4L2_PIX_FMT_YUYV;
1136
1137         /* first field and frame format based on standard frame format */
1138         if (vpfe->std_info.frame_format)
1139                 pix->field = V4L2_FIELD_INTERLACED;
1140         else
1141                 pix->field = V4L2_FIELD_NONE;
1142
1143         ret = __vpfe_get_format(vpfe, &vpfe->fmt, &vpfe->bpp);
1144         if (ret)
1145                 return ret;
1146
1147         /* Update the crop window based on found values */
1148         vpfe->crop.width = pix->width;
1149         vpfe->crop.height = pix->height;
1150
1151         return vpfe_config_ccdc_image_format(vpfe);
1152 }
1153
1154 static int vpfe_initialize_device(struct vpfe_device *vpfe)
1155 {
1156         struct vpfe_subdev_info *sdinfo;
1157         int ret;
1158
1159         sdinfo = &vpfe->cfg->sub_devs[0];
1160         sdinfo->sd = vpfe->sd[0];
1161         vpfe->current_input = 0;
1162         vpfe->std_index = 0;
1163         /* Configure the default format information */
1164         ret = vpfe_config_image_format(vpfe,
1165                                        vpfe_standards[vpfe->std_index].std_id);
1166         if (ret)
1167                 return ret;
1168
1169         pm_runtime_get_sync(vpfe->pdev);
1170
1171         vpfe_config_enable(&vpfe->ccdc, 1);
1172
1173         vpfe_ccdc_restore_defaults(&vpfe->ccdc);
1174
1175         /* Clear all VPFE interrupts */
1176         vpfe_clear_intr(&vpfe->ccdc, -1);
1177
1178         return ret;
1179 }
1180
1181 /*
1182  * vpfe_release : This function is based on the vb2_fop_release
1183  * helper function.
1184  * It has been augmented to handle module power management,
1185  * by disabling/enabling h/w module fcntl clock when necessary.
1186  */
1187 static int vpfe_release(struct file *file)
1188 {
1189         struct vpfe_device *vpfe = video_drvdata(file);
1190         bool fh_singular;
1191         int ret;
1192
1193         mutex_lock(&vpfe->lock);
1194
1195         /* Save the singular status before we call the clean-up helper */
1196         fh_singular = v4l2_fh_is_singular_file(file);
1197
1198         /* the release helper will cleanup any on-going streaming */
1199         ret = _vb2_fop_release(file, NULL);
1200
1201         /*
1202          * If this was the last open file.
1203          * Then de-initialize hw module.
1204          */
1205         if (fh_singular)
1206                 vpfe_ccdc_close(&vpfe->ccdc, vpfe->pdev);
1207
1208         mutex_unlock(&vpfe->lock);
1209
1210         return ret;
1211 }
1212
1213 /*
1214  * vpfe_open : This function is based on the v4l2_fh_open helper function.
1215  * It has been augmented to handle module power management,
1216  * by disabling/enabling h/w module fcntl clock when necessary.
1217  */
1218 static int vpfe_open(struct file *file)
1219 {
1220         struct vpfe_device *vpfe = video_drvdata(file);
1221         int ret;
1222
1223         mutex_lock(&vpfe->lock);
1224
1225         ret = v4l2_fh_open(file);
1226         if (ret) {
1227                 vpfe_err(vpfe, "v4l2_fh_open failed\n");
1228                 goto unlock;
1229         }
1230
1231         if (!v4l2_fh_is_singular_file(file))
1232                 goto unlock;
1233
1234         if (vpfe_initialize_device(vpfe)) {
1235                 v4l2_fh_release(file);
1236                 ret = -ENODEV;
1237         }
1238
1239 unlock:
1240         mutex_unlock(&vpfe->lock);
1241         return ret;
1242 }
1243
1244 /**
1245  * vpfe_schedule_next_buffer: set next buffer address for capture
1246  * @vpfe : ptr to vpfe device
1247  *
1248  * This function will get next buffer from the dma queue and
1249  * set the buffer address in the vpfe register for capture.
1250  * the buffer is marked active
1251  *
1252  * Assumes caller is holding vpfe->dma_queue_lock already
1253  */
1254 static inline void vpfe_schedule_next_buffer(struct vpfe_device *vpfe)
1255 {
1256         vpfe->next_frm = list_entry(vpfe->dma_queue.next,
1257                                     struct vpfe_cap_buffer, list);
1258         list_del(&vpfe->next_frm->list);
1259
1260         vpfe_set_sdr_addr(&vpfe->ccdc,
1261                vb2_dma_contig_plane_dma_addr(&vpfe->next_frm->vb.vb2_buf, 0));
1262 }
1263
1264 static inline void vpfe_schedule_bottom_field(struct vpfe_device *vpfe)
1265 {
1266         unsigned long addr;
1267
1268         addr = vb2_dma_contig_plane_dma_addr(&vpfe->next_frm->vb.vb2_buf, 0) +
1269                                         vpfe->field_off;
1270
1271         vpfe_set_sdr_addr(&vpfe->ccdc, addr);
1272 }
1273
1274 /*
1275  * vpfe_process_buffer_complete: process a completed buffer
1276  * @vpfe : ptr to vpfe device
1277  *
1278  * This function time stamp the buffer and mark it as DONE. It also
1279  * wake up any process waiting on the QUEUE and set the next buffer
1280  * as current
1281  */
1282 static inline void vpfe_process_buffer_complete(struct vpfe_device *vpfe)
1283 {
1284         vpfe->cur_frm->vb.vb2_buf.timestamp = ktime_get_ns();
1285         vpfe->cur_frm->vb.field = vpfe->fmt.fmt.pix.field;
1286         vpfe->cur_frm->vb.sequence = vpfe->sequence++;
1287         vb2_buffer_done(&vpfe->cur_frm->vb.vb2_buf, VB2_BUF_STATE_DONE);
1288         vpfe->cur_frm = vpfe->next_frm;
1289 }
1290
1291 /*
1292  * vpfe_isr : ISR handler for vpfe capture (VINT0)
1293  * @irq: irq number
1294  * @dev_id: dev_id ptr
1295  *
1296  * It changes status of the captured buffer, takes next buffer from the queue
1297  * and sets its address in VPFE registers
1298  */
1299 static irqreturn_t vpfe_isr(int irq, void *dev)
1300 {
1301         struct vpfe_device *vpfe = (struct vpfe_device *)dev;
1302         enum v4l2_field field;
1303         int intr_status;
1304         int fid;
1305
1306         intr_status = vpfe_reg_read(&vpfe->ccdc, VPFE_IRQ_STS);
1307
1308         if (intr_status & VPFE_VDINT0) {
1309                 field = vpfe->fmt.fmt.pix.field;
1310
1311                 if (field == V4L2_FIELD_NONE) {
1312                         /* handle progressive frame capture */
1313                         if (vpfe->cur_frm != vpfe->next_frm)
1314                                 vpfe_process_buffer_complete(vpfe);
1315                         goto next_intr;
1316                 }
1317
1318                 /* interlaced or TB capture check which field
1319                    we are in hardware */
1320                 fid = vpfe_ccdc_getfid(&vpfe->ccdc);
1321
1322                 /* switch the software maintained field id */
1323                 vpfe->field ^= 1;
1324                 if (fid == vpfe->field) {
1325                         /* we are in-sync here,continue */
1326                         if (fid == 0) {
1327                                 /*
1328                                  * One frame is just being captured. If the
1329                                  * next frame is available, release the
1330                                  * current frame and move on
1331                                  */
1332                                 if (vpfe->cur_frm != vpfe->next_frm)
1333                                         vpfe_process_buffer_complete(vpfe);
1334                                 /*
1335                                  * based on whether the two fields are stored
1336                                  * interleave or separately in memory,
1337                                  * reconfigure the CCDC memory address
1338                                  */
1339                                 if (field == V4L2_FIELD_SEQ_TB)
1340                                         vpfe_schedule_bottom_field(vpfe);
1341
1342                                 goto next_intr;
1343                         }
1344                         /*
1345                          * if one field is just being captured configure
1346                          * the next frame get the next frame from the empty
1347                          * queue if no frame is available hold on to the
1348                          * current buffer
1349                          */
1350                         spin_lock(&vpfe->dma_queue_lock);
1351                         if (!list_empty(&vpfe->dma_queue) &&
1352                             vpfe->cur_frm == vpfe->next_frm)
1353                                 vpfe_schedule_next_buffer(vpfe);
1354                         spin_unlock(&vpfe->dma_queue_lock);
1355                 } else if (fid == 0) {
1356                         /*
1357                          * out of sync. Recover from any hardware out-of-sync.
1358                          * May loose one frame
1359                          */
1360                         vpfe->field = fid;
1361                 }
1362         }
1363
1364 next_intr:
1365         if (intr_status & VPFE_VDINT1) {
1366                 spin_lock(&vpfe->dma_queue_lock);
1367                 if (vpfe->fmt.fmt.pix.field == V4L2_FIELD_NONE &&
1368                     !list_empty(&vpfe->dma_queue) &&
1369                     vpfe->cur_frm == vpfe->next_frm)
1370                         vpfe_schedule_next_buffer(vpfe);
1371                 spin_unlock(&vpfe->dma_queue_lock);
1372         }
1373
1374         vpfe_clear_intr(&vpfe->ccdc, intr_status);
1375
1376         return IRQ_HANDLED;
1377 }
1378
1379 static inline void vpfe_detach_irq(struct vpfe_device *vpfe)
1380 {
1381         unsigned int intr = VPFE_VDINT0;
1382         enum ccdc_frmfmt frame_format;
1383
1384         frame_format = vpfe_ccdc_get_frame_format(&vpfe->ccdc);
1385         if (frame_format == CCDC_FRMFMT_PROGRESSIVE)
1386                 intr |= VPFE_VDINT1;
1387
1388         vpfe_reg_write(&vpfe->ccdc, intr, VPFE_IRQ_EN_CLR);
1389 }
1390
1391 static inline void vpfe_attach_irq(struct vpfe_device *vpfe)
1392 {
1393         unsigned int intr = VPFE_VDINT0;
1394         enum ccdc_frmfmt frame_format;
1395
1396         frame_format = vpfe_ccdc_get_frame_format(&vpfe->ccdc);
1397         if (frame_format == CCDC_FRMFMT_PROGRESSIVE)
1398                 intr |= VPFE_VDINT1;
1399
1400         vpfe_reg_write(&vpfe->ccdc, intr, VPFE_IRQ_EN_SET);
1401 }
1402
1403 static int vpfe_querycap(struct file *file, void  *priv,
1404                          struct v4l2_capability *cap)
1405 {
1406         struct vpfe_device *vpfe = video_drvdata(file);
1407
1408         vpfe_dbg(2, vpfe, "vpfe_querycap\n");
1409
1410         strlcpy(cap->driver, VPFE_MODULE_NAME, sizeof(cap->driver));
1411         strlcpy(cap->card, "TI AM437x VPFE", sizeof(cap->card));
1412         snprintf(cap->bus_info, sizeof(cap->bus_info),
1413                         "platform:%s", vpfe->v4l2_dev.name);
1414         cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING |
1415                             V4L2_CAP_READWRITE;
1416         cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1417
1418         return 0;
1419 }
1420
1421 /* get the format set at output pad of the adjacent subdev */
1422 static int __vpfe_get_format(struct vpfe_device *vpfe,
1423                              struct v4l2_format *format, unsigned int *bpp)
1424 {
1425         struct v4l2_mbus_framefmt mbus_fmt;
1426         struct vpfe_subdev_info *sdinfo;
1427         struct v4l2_subdev_format fmt;
1428         int ret;
1429
1430         sdinfo = vpfe->current_subdev;
1431         if (!sdinfo->sd)
1432                 return -EINVAL;
1433
1434         fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1435         fmt.pad = 0;
1436
1437         ret = v4l2_subdev_call(sdinfo->sd, pad, get_fmt, NULL, &fmt);
1438         if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
1439                 return ret;
1440
1441         if (!ret) {
1442                 v4l2_fill_pix_format(&format->fmt.pix, &fmt.format);
1443                 mbus_to_pix(vpfe, &fmt.format, &format->fmt.pix, bpp);
1444         } else {
1445                 ret = v4l2_device_call_until_err(&vpfe->v4l2_dev,
1446                                                  sdinfo->grp_id,
1447                                                  pad, get_fmt,
1448                                                  NULL, &fmt);
1449                 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
1450                         return ret;
1451                 v4l2_fill_pix_format(&format->fmt.pix, &mbus_fmt);
1452                 mbus_to_pix(vpfe, &mbus_fmt, &format->fmt.pix, bpp);
1453         }
1454
1455         format->type = vpfe->fmt.type;
1456
1457         vpfe_dbg(1, vpfe,
1458                  "%s size %dx%d (%s) bytesperline = %d, size = %d, bpp = %d\n",
1459                  __func__, format->fmt.pix.width, format->fmt.pix.height,
1460                  print_fourcc(format->fmt.pix.pixelformat),
1461                  format->fmt.pix.bytesperline, format->fmt.pix.sizeimage, *bpp);
1462
1463         return 0;
1464 }
1465
1466 /* set the format at output pad of the adjacent subdev */
1467 static int __vpfe_set_format(struct vpfe_device *vpfe,
1468                              struct v4l2_format *format, unsigned int *bpp)
1469 {
1470         struct vpfe_subdev_info *sdinfo;
1471         struct v4l2_subdev_format fmt;
1472         int ret;
1473
1474         vpfe_dbg(2, vpfe, "__vpfe_set_format\n");
1475
1476         sdinfo = vpfe->current_subdev;
1477         if (!sdinfo->sd)
1478                 return -EINVAL;
1479
1480         fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1481         fmt.pad = 0;
1482
1483         pix_to_mbus(vpfe, &format->fmt.pix, &fmt.format);
1484
1485         ret = v4l2_subdev_call(sdinfo->sd, pad, set_fmt, NULL, &fmt);
1486         if (ret)
1487                 return ret;
1488
1489         v4l2_fill_pix_format(&format->fmt.pix, &fmt.format);
1490         mbus_to_pix(vpfe, &fmt.format, &format->fmt.pix, bpp);
1491
1492         format->type = vpfe->fmt.type;
1493
1494         vpfe_dbg(1, vpfe,
1495                  "%s size %dx%d (%s) bytesperline = %d, size = %d, bpp = %d\n",
1496                  __func__,  format->fmt.pix.width, format->fmt.pix.height,
1497                  print_fourcc(format->fmt.pix.pixelformat),
1498                  format->fmt.pix.bytesperline, format->fmt.pix.sizeimage, *bpp);
1499
1500         return 0;
1501 }
1502
1503 static int vpfe_g_fmt(struct file *file, void *priv,
1504                       struct v4l2_format *fmt)
1505 {
1506         struct vpfe_device *vpfe = video_drvdata(file);
1507
1508         vpfe_dbg(2, vpfe, "vpfe_g_fmt\n");
1509
1510         *fmt = vpfe->fmt;
1511
1512         return 0;
1513 }
1514
1515 static int vpfe_enum_fmt(struct file *file, void  *priv,
1516                          struct v4l2_fmtdesc *f)
1517 {
1518         struct vpfe_device *vpfe = video_drvdata(file);
1519         struct vpfe_subdev_info *sdinfo;
1520         struct vpfe_fmt *fmt = NULL;
1521         unsigned int k;
1522
1523         vpfe_dbg(2, vpfe, "vpfe_enum_format index:%d\n",
1524                 f->index);
1525
1526         sdinfo = vpfe->current_subdev;
1527         if (!sdinfo->sd)
1528                 return -EINVAL;
1529
1530         if (f->index > ARRAY_SIZE(formats))
1531                 return -EINVAL;
1532
1533         for (k = 0; k < ARRAY_SIZE(formats); k++) {
1534                 if (formats[k].index == f->index) {
1535                         fmt = &formats[k];
1536                         break;
1537                 }
1538         }
1539         if (!fmt)
1540                 return -EINVAL;
1541
1542         strncpy(f->description, fmt->name, sizeof(f->description) - 1);
1543         f->pixelformat = fmt->fourcc;
1544         f->type = vpfe->fmt.type;
1545
1546         vpfe_dbg(1, vpfe, "vpfe_enum_format: mbus index: %d code: %x pixelformat: %s [%s]\n",
1547                 f->index, fmt->code, print_fourcc(fmt->fourcc), fmt->name);
1548
1549         return 0;
1550 }
1551
1552 static int vpfe_try_fmt(struct file *file, void *priv,
1553                         struct v4l2_format *fmt)
1554 {
1555         struct vpfe_device *vpfe = video_drvdata(file);
1556         unsigned int bpp;
1557
1558         vpfe_dbg(2, vpfe, "vpfe_try_fmt\n");
1559
1560         return __vpfe_get_format(vpfe, fmt, &bpp);
1561 }
1562
1563 static int vpfe_s_fmt(struct file *file, void *priv,
1564                       struct v4l2_format *fmt)
1565 {
1566         struct vpfe_device *vpfe = video_drvdata(file);
1567         struct v4l2_format format;
1568         unsigned int bpp;
1569         int ret;
1570
1571         vpfe_dbg(2, vpfe, "vpfe_s_fmt\n");
1572
1573         /* If streaming is started, return error */
1574         if (vb2_is_busy(&vpfe->buffer_queue)) {
1575                 vpfe_err(vpfe, "%s device busy\n", __func__);
1576                 return -EBUSY;
1577         }
1578
1579         ret = vpfe_try_fmt(file, priv, &format);
1580         if (ret)
1581                 return ret;
1582
1583
1584         if (!cmp_v4l2_format(fmt, &format)) {
1585                 /* Sensor format is different from the requested format
1586                  * so we need to change it
1587                  */
1588                 ret = __vpfe_set_format(vpfe, fmt, &bpp);
1589                 if (ret)
1590                         return ret;
1591         } else /* Just make sure all of the fields are consistent */
1592                 *fmt = format;
1593
1594         /* First detach any IRQ if currently attached */
1595         vpfe_detach_irq(vpfe);
1596         vpfe->fmt = *fmt;
1597         vpfe->bpp = bpp;
1598
1599         /* Update the crop window based on found values */
1600         vpfe->crop.width = fmt->fmt.pix.width;
1601         vpfe->crop.height = fmt->fmt.pix.height;
1602
1603         /* set image capture parameters in the ccdc */
1604         return vpfe_config_ccdc_image_format(vpfe);
1605 }
1606
1607 static int vpfe_enum_size(struct file *file, void  *priv,
1608                           struct v4l2_frmsizeenum *fsize)
1609 {
1610         struct vpfe_device *vpfe = video_drvdata(file);
1611         struct v4l2_subdev_frame_size_enum fse;
1612         struct vpfe_subdev_info *sdinfo;
1613         struct v4l2_mbus_framefmt mbus;
1614         struct v4l2_pix_format pix;
1615         struct vpfe_fmt *fmt;
1616         int ret;
1617
1618         vpfe_dbg(2, vpfe, "vpfe_enum_size\n");
1619
1620         /* check for valid format */
1621         fmt = find_format_by_pix(fsize->pixel_format);
1622         if (!fmt) {
1623                 vpfe_dbg(3, vpfe, "Invalid pixel code: %x, default used instead\n",
1624                         fsize->pixel_format);
1625                 return -EINVAL;
1626         }
1627
1628         memset(fsize->reserved, 0x0, sizeof(fsize->reserved));
1629
1630         sdinfo = vpfe->current_subdev;
1631         if (!sdinfo->sd)
1632                 return -EINVAL;
1633
1634         memset(&pix, 0x0, sizeof(pix));
1635         /* Construct pix from parameter and use default for the rest */
1636         pix.pixelformat = fsize->pixel_format;
1637         pix.width = 640;
1638         pix.height = 480;
1639         pix.colorspace = V4L2_COLORSPACE_SRGB;
1640         pix.field = V4L2_FIELD_NONE;
1641         pix_to_mbus(vpfe, &pix, &mbus);
1642
1643         memset(&fse, 0x0, sizeof(fse));
1644         fse.index = fsize->index;
1645         fse.pad = 0;
1646         fse.code = mbus.code;
1647         fse.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1648         ret = v4l2_subdev_call(sdinfo->sd, pad, enum_frame_size, NULL, &fse);
1649         if (ret)
1650                 return -EINVAL;
1651
1652         vpfe_dbg(1, vpfe, "vpfe_enum_size: index: %d code: %x W:[%d,%d] H:[%d,%d]\n",
1653                 fse.index, fse.code, fse.min_width, fse.max_width,
1654                 fse.min_height, fse.max_height);
1655
1656         fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1657         fsize->discrete.width = fse.max_width;
1658         fsize->discrete.height = fse.max_height;
1659
1660         vpfe_dbg(1, vpfe, "vpfe_enum_size: index: %d pixformat: %s size: %dx%d\n",
1661                 fsize->index, print_fourcc(fsize->pixel_format),
1662                 fsize->discrete.width, fsize->discrete.height);
1663
1664         return 0;
1665 }
1666
1667 /*
1668  * vpfe_get_subdev_input_index - Get subdev index and subdev input index for a
1669  * given app input index
1670  */
1671 static int
1672 vpfe_get_subdev_input_index(struct vpfe_device *vpfe,
1673                             int *subdev_index,
1674                             int *subdev_input_index,
1675                             int app_input_index)
1676 {
1677         int i, j = 0;
1678
1679         for (i = 0; i < ARRAY_SIZE(vpfe->cfg->asd); i++) {
1680                 if (app_input_index < (j + 1)) {
1681                         *subdev_index = i;
1682                         *subdev_input_index = app_input_index - j;
1683                         return 0;
1684                 }
1685                 j++;
1686         }
1687         return -EINVAL;
1688 }
1689
1690 /*
1691  * vpfe_get_app_input - Get app input index for a given subdev input index
1692  * driver stores the input index of the current sub device and translate it
1693  * when application request the current input
1694  */
1695 static int vpfe_get_app_input_index(struct vpfe_device *vpfe,
1696                                     int *app_input_index)
1697 {
1698         struct vpfe_config *cfg = vpfe->cfg;
1699         struct vpfe_subdev_info *sdinfo;
1700         struct i2c_client *client;
1701         struct i2c_client *curr_client;
1702         int i, j = 0;
1703
1704         curr_client = v4l2_get_subdevdata(vpfe->current_subdev->sd);
1705         for (i = 0; i < ARRAY_SIZE(vpfe->cfg->asd); i++) {
1706                 sdinfo = &cfg->sub_devs[i];
1707                 client = v4l2_get_subdevdata(sdinfo->sd);
1708                 if (client->addr == curr_client->addr &&
1709                     client->adapter->nr == curr_client->adapter->nr) {
1710                         if (vpfe->current_input >= 1)
1711                                 return -1;
1712                         *app_input_index = j + vpfe->current_input;
1713                         return 0;
1714                 }
1715                 j++;
1716         }
1717         return -EINVAL;
1718 }
1719
1720 static int vpfe_enum_input(struct file *file, void *priv,
1721                            struct v4l2_input *inp)
1722 {
1723         struct vpfe_device *vpfe = video_drvdata(file);
1724         struct vpfe_subdev_info *sdinfo;
1725         int subdev, index;
1726
1727         vpfe_dbg(2, vpfe, "vpfe_enum_input\n");
1728
1729         if (vpfe_get_subdev_input_index(vpfe, &subdev, &index,
1730                                         inp->index) < 0) {
1731                 vpfe_dbg(1, vpfe,
1732                         "input information not found for the subdev\n");
1733                 return -EINVAL;
1734         }
1735         sdinfo = &vpfe->cfg->sub_devs[subdev];
1736         *inp = sdinfo->inputs[index];
1737
1738         return 0;
1739 }
1740
1741 static int vpfe_g_input(struct file *file, void *priv, unsigned int *index)
1742 {
1743         struct vpfe_device *vpfe = video_drvdata(file);
1744
1745         vpfe_dbg(2, vpfe, "vpfe_g_input\n");
1746
1747         return vpfe_get_app_input_index(vpfe, index);
1748 }
1749
1750 /* Assumes caller is holding vpfe_dev->lock */
1751 static int vpfe_set_input(struct vpfe_device *vpfe, unsigned int index)
1752 {
1753         int subdev_index = 0, inp_index = 0;
1754         struct vpfe_subdev_info *sdinfo;
1755         struct vpfe_route *route;
1756         u32 input, output;
1757         int ret;
1758
1759         vpfe_dbg(2, vpfe, "vpfe_set_input: index: %d\n", index);
1760
1761         /* If streaming is started, return error */
1762         if (vb2_is_busy(&vpfe->buffer_queue)) {
1763                 vpfe_err(vpfe, "%s device busy\n", __func__);
1764                 return -EBUSY;
1765         }
1766         ret = vpfe_get_subdev_input_index(vpfe,
1767                                           &subdev_index,
1768                                           &inp_index,
1769                                           index);
1770         if (ret < 0) {
1771                 vpfe_err(vpfe, "invalid input index: %d\n", index);
1772                 goto get_out;
1773         }
1774
1775         sdinfo = &vpfe->cfg->sub_devs[subdev_index];
1776         sdinfo->sd = vpfe->sd[subdev_index];
1777         route = &sdinfo->routes[inp_index];
1778         if (route && sdinfo->can_route) {
1779                 input = route->input;
1780                 output = route->output;
1781                 if (sdinfo->sd) {
1782                         ret = v4l2_subdev_call(sdinfo->sd, video,
1783                                         s_routing, input, output, 0);
1784                         if (ret) {
1785                                 vpfe_err(vpfe, "s_routing failed\n");
1786                                 ret = -EINVAL;
1787                                 goto get_out;
1788                         }
1789                 }
1790
1791         }
1792
1793         vpfe->current_subdev = sdinfo;
1794         if (sdinfo->sd)
1795                 vpfe->v4l2_dev.ctrl_handler = sdinfo->sd->ctrl_handler;
1796         vpfe->current_input = index;
1797         vpfe->std_index = 0;
1798
1799         /* set the bus/interface parameter for the sub device in ccdc */
1800         ret = vpfe_ccdc_set_hw_if_params(&vpfe->ccdc, &sdinfo->vpfe_param);
1801         if (ret)
1802                 return ret;
1803
1804         /* set the default image parameters in the device */
1805         return vpfe_config_image_format(vpfe,
1806                                         vpfe_standards[vpfe->std_index].std_id);
1807
1808 get_out:
1809         return ret;
1810 }
1811
1812 static int vpfe_s_input(struct file *file, void *priv, unsigned int index)
1813 {
1814         struct vpfe_device *vpfe = video_drvdata(file);
1815
1816         vpfe_dbg(2, vpfe,
1817                 "vpfe_s_input: index: %d\n", index);
1818
1819         return vpfe_set_input(vpfe, index);
1820 }
1821
1822 static int vpfe_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
1823 {
1824         struct vpfe_device *vpfe = video_drvdata(file);
1825         struct vpfe_subdev_info *sdinfo;
1826
1827         vpfe_dbg(2, vpfe, "vpfe_querystd\n");
1828
1829         sdinfo = vpfe->current_subdev;
1830         if (!(sdinfo->inputs[0].capabilities & V4L2_IN_CAP_STD))
1831                 return -ENODATA;
1832
1833         /* Call querystd function of decoder device */
1834         return v4l2_device_call_until_err(&vpfe->v4l2_dev, sdinfo->grp_id,
1835                                          video, querystd, std_id);
1836 }
1837
1838 static int vpfe_s_std(struct file *file, void *priv, v4l2_std_id std_id)
1839 {
1840         struct vpfe_device *vpfe = video_drvdata(file);
1841         struct vpfe_subdev_info *sdinfo;
1842         int ret;
1843
1844         vpfe_dbg(2, vpfe, "vpfe_s_std\n");
1845
1846         sdinfo = vpfe->current_subdev;
1847         if (!(sdinfo->inputs[0].capabilities & V4L2_IN_CAP_STD))
1848                 return -ENODATA;
1849
1850         /* If streaming is started, return error */
1851         if (vb2_is_busy(&vpfe->buffer_queue)) {
1852                 vpfe_err(vpfe, "%s device busy\n", __func__);
1853                 ret = -EBUSY;
1854                 return ret;
1855         }
1856
1857         ret = v4l2_device_call_until_err(&vpfe->v4l2_dev, sdinfo->grp_id,
1858                                          video, s_std, std_id);
1859         if (ret < 0) {
1860                 vpfe_err(vpfe, "Failed to set standard\n");
1861                 return ret;
1862         }
1863         ret = vpfe_config_image_format(vpfe, std_id);
1864
1865         return ret;
1866 }
1867
1868 static int vpfe_g_std(struct file *file, void *priv, v4l2_std_id *std_id)
1869 {
1870         struct vpfe_device *vpfe = video_drvdata(file);
1871         struct vpfe_subdev_info *sdinfo;
1872
1873         vpfe_dbg(2, vpfe, "vpfe_g_std\n");
1874
1875         sdinfo = vpfe->current_subdev;
1876         if (sdinfo->inputs[0].capabilities != V4L2_IN_CAP_STD)
1877                 return -ENODATA;
1878
1879         *std_id = vpfe_standards[vpfe->std_index].std_id;
1880
1881         return 0;
1882 }
1883
1884 /*
1885  * vpfe_calculate_offsets : This function calculates buffers offset
1886  * for top and bottom field
1887  */
1888 static void vpfe_calculate_offsets(struct vpfe_device *vpfe)
1889 {
1890         struct v4l2_rect image_win;
1891
1892         vpfe_dbg(2, vpfe, "vpfe_calculate_offsets\n");
1893
1894         vpfe_ccdc_get_image_window(&vpfe->ccdc, &image_win);
1895         vpfe->field_off = image_win.height * image_win.width;
1896 }
1897
1898 /*
1899  * vpfe_queue_setup - Callback function for buffer setup.
1900  * @vq: vb2_queue ptr
1901  * @nbuffers: ptr to number of buffers requested by application
1902  * @nplanes:: contains number of distinct video planes needed to hold a frame
1903  * @sizes[]: contains the size (in bytes) of each plane.
1904  * @alloc_ctxs: ptr to allocation context
1905  *
1906  * This callback function is called when reqbuf() is called to adjust
1907  * the buffer count and buffer size
1908  */
1909 static int vpfe_queue_setup(struct vb2_queue *vq,
1910                             unsigned int *nbuffers, unsigned int *nplanes,
1911                             unsigned int sizes[], void *alloc_ctxs[])
1912 {
1913         struct vpfe_device *vpfe = vb2_get_drv_priv(vq);
1914         unsigned size = vpfe->fmt.fmt.pix.sizeimage;
1915
1916         if (vq->num_buffers + *nbuffers < 3)
1917                 *nbuffers = 3 - vq->num_buffers;
1918         alloc_ctxs[0] = vpfe->alloc_ctx;
1919
1920         if (*nplanes) {
1921                 if (sizes[0] < size)
1922                         return -EINVAL;
1923                 size = sizes[0];
1924         }
1925
1926         *nplanes = 1;
1927         sizes[0] = size;
1928
1929         vpfe_dbg(1, vpfe,
1930                 "nbuffers=%d, size=%u\n", *nbuffers, sizes[0]);
1931
1932         /* Calculate field offset */
1933         vpfe_calculate_offsets(vpfe);
1934
1935         return 0;
1936 }
1937
1938 /*
1939  * vpfe_buffer_prepare :  callback function for buffer prepare
1940  * @vb: ptr to vb2_buffer
1941  *
1942  * This is the callback function for buffer prepare when vb2_qbuf()
1943  * function is called. The buffer is prepared and user space virtual address
1944  * or user address is converted into  physical address
1945  */
1946 static int vpfe_buffer_prepare(struct vb2_buffer *vb)
1947 {
1948         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1949         struct vpfe_device *vpfe = vb2_get_drv_priv(vb->vb2_queue);
1950
1951         vb2_set_plane_payload(vb, 0, vpfe->fmt.fmt.pix.sizeimage);
1952
1953         if (vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
1954                 return -EINVAL;
1955
1956         vbuf->field = vpfe->fmt.fmt.pix.field;
1957
1958         return 0;
1959 }
1960
1961 /*
1962  * vpfe_buffer_queue : Callback function to add buffer to DMA queue
1963  * @vb: ptr to vb2_buffer
1964  */
1965 static void vpfe_buffer_queue(struct vb2_buffer *vb)
1966 {
1967         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1968         struct vpfe_device *vpfe = vb2_get_drv_priv(vb->vb2_queue);
1969         struct vpfe_cap_buffer *buf = to_vpfe_buffer(vbuf);
1970         unsigned long flags = 0;
1971
1972         /* add the buffer to the DMA queue */
1973         spin_lock_irqsave(&vpfe->dma_queue_lock, flags);
1974         list_add_tail(&buf->list, &vpfe->dma_queue);
1975         spin_unlock_irqrestore(&vpfe->dma_queue_lock, flags);
1976 }
1977
1978 /*
1979  * vpfe_start_streaming : Starts the DMA engine for streaming
1980  * @vb: ptr to vb2_buffer
1981  * @count: number of buffers
1982  */
1983 static int vpfe_start_streaming(struct vb2_queue *vq, unsigned int count)
1984 {
1985         struct vpfe_device *vpfe = vb2_get_drv_priv(vq);
1986         struct vpfe_cap_buffer *buf, *tmp;
1987         struct vpfe_subdev_info *sdinfo;
1988         unsigned long flags;
1989         unsigned long addr;
1990         int ret;
1991
1992         spin_lock_irqsave(&vpfe->dma_queue_lock, flags);
1993
1994         vpfe->field = 0;
1995         vpfe->sequence = 0;
1996
1997         sdinfo = vpfe->current_subdev;
1998
1999         vpfe_attach_irq(vpfe);
2000
2001         if (vpfe->ccdc.ccdc_cfg.if_type == VPFE_RAW_BAYER)
2002                 vpfe_ccdc_config_raw(&vpfe->ccdc);
2003         else
2004                 vpfe_ccdc_config_ycbcr(&vpfe->ccdc);
2005
2006         /* Get the next frame from the buffer queue */
2007         vpfe->next_frm = list_entry(vpfe->dma_queue.next,
2008                                     struct vpfe_cap_buffer, list);
2009         vpfe->cur_frm = vpfe->next_frm;
2010         /* Remove buffer from the buffer queue */
2011         list_del(&vpfe->cur_frm->list);
2012         spin_unlock_irqrestore(&vpfe->dma_queue_lock, flags);
2013
2014         addr = vb2_dma_contig_plane_dma_addr(&vpfe->cur_frm->vb.vb2_buf, 0);
2015
2016         vpfe_set_sdr_addr(&vpfe->ccdc, (unsigned long)(addr));
2017
2018         vpfe_pcr_enable(&vpfe->ccdc, 1);
2019
2020         ret = v4l2_subdev_call(sdinfo->sd, video, s_stream, 1);
2021         if (ret < 0) {
2022                 vpfe_err(vpfe, "Error in attaching interrupt handle\n");
2023                 goto err;
2024         }
2025
2026         return 0;
2027
2028 err:
2029         list_for_each_entry_safe(buf, tmp, &vpfe->dma_queue, list) {
2030                 list_del(&buf->list);
2031                 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
2032         }
2033
2034         return ret;
2035 }
2036
2037 /*
2038  * vpfe_stop_streaming : Stop the DMA engine
2039  * @vq: ptr to vb2_queue
2040  *
2041  * This callback stops the DMA engine and any remaining buffers
2042  * in the DMA queue are released.
2043  */
2044 static void vpfe_stop_streaming(struct vb2_queue *vq)
2045 {
2046         struct vpfe_device *vpfe = vb2_get_drv_priv(vq);
2047         struct vpfe_subdev_info *sdinfo;
2048         unsigned long flags;
2049         int ret;
2050
2051         vpfe_pcr_enable(&vpfe->ccdc, 0);
2052
2053         vpfe_detach_irq(vpfe);
2054
2055         sdinfo = vpfe->current_subdev;
2056         ret = v4l2_subdev_call(sdinfo->sd, video, s_stream, 0);
2057         if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
2058                 vpfe_dbg(1, vpfe, "stream off failed in subdev\n");
2059
2060         /* release all active buffers */
2061         spin_lock_irqsave(&vpfe->dma_queue_lock, flags);
2062         if (vpfe->cur_frm == vpfe->next_frm) {
2063                 vb2_buffer_done(&vpfe->cur_frm->vb.vb2_buf,
2064                                 VB2_BUF_STATE_ERROR);
2065         } else {
2066                 if (vpfe->cur_frm != NULL)
2067                         vb2_buffer_done(&vpfe->cur_frm->vb.vb2_buf,
2068                                         VB2_BUF_STATE_ERROR);
2069                 if (vpfe->next_frm != NULL)
2070                         vb2_buffer_done(&vpfe->next_frm->vb.vb2_buf,
2071                                         VB2_BUF_STATE_ERROR);
2072         }
2073
2074         while (!list_empty(&vpfe->dma_queue)) {
2075                 vpfe->next_frm = list_entry(vpfe->dma_queue.next,
2076                                                 struct vpfe_cap_buffer, list);
2077                 list_del(&vpfe->next_frm->list);
2078                 vb2_buffer_done(&vpfe->next_frm->vb.vb2_buf,
2079                                 VB2_BUF_STATE_ERROR);
2080         }
2081         spin_unlock_irqrestore(&vpfe->dma_queue_lock, flags);
2082 }
2083
2084 static int vpfe_cropcap(struct file *file, void *priv,
2085                         struct v4l2_cropcap *crop)
2086 {
2087         struct vpfe_device *vpfe = video_drvdata(file);
2088
2089         vpfe_dbg(2, vpfe, "vpfe_cropcap\n");
2090
2091         if (vpfe->std_index >= ARRAY_SIZE(vpfe_standards))
2092                 return -EINVAL;
2093
2094         memset(crop, 0, sizeof(struct v4l2_cropcap));
2095
2096         crop->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2097         crop->defrect.width = vpfe_standards[vpfe->std_index].width;
2098         crop->bounds.width = crop->defrect.width;
2099         crop->defrect.height = vpfe_standards[vpfe->std_index].height;
2100         crop->bounds.height = crop->defrect.height;
2101         crop->pixelaspect = vpfe_standards[vpfe->std_index].pixelaspect;
2102
2103         return 0;
2104 }
2105
2106 static int
2107 vpfe_g_selection(struct file *file, void *fh, struct v4l2_selection *s)
2108 {
2109         struct vpfe_device *vpfe = video_drvdata(file);
2110
2111         switch (s->target) {
2112         case V4L2_SEL_TGT_CROP_BOUNDS:
2113         case V4L2_SEL_TGT_CROP_DEFAULT:
2114                 s->r.left = s->r.top = 0;
2115                 s->r.width = vpfe->crop.width;
2116                 s->r.height = vpfe->crop.height;
2117                 break;
2118
2119         case V4L2_SEL_TGT_CROP:
2120                 s->r = vpfe->crop;
2121                 break;
2122
2123         default:
2124                 return -EINVAL;
2125         }
2126
2127         return 0;
2128 }
2129
2130 static int enclosed_rectangle(struct v4l2_rect *a, struct v4l2_rect *b)
2131 {
2132         if (a->left < b->left || a->top < b->top)
2133                 return 0;
2134
2135         if (a->left + a->width > b->left + b->width)
2136                 return 0;
2137
2138         if (a->top + a->height > b->top + b->height)
2139                 return 0;
2140
2141         return 1;
2142 }
2143
2144 static int
2145 vpfe_s_selection(struct file *file, void *fh, struct v4l2_selection *s)
2146 {
2147         struct vpfe_device *vpfe = video_drvdata(file);
2148         struct v4l2_rect cr = vpfe->crop;
2149         struct v4l2_rect r = s->r;
2150
2151         /* If streaming is started, return error */
2152         if (vb2_is_busy(&vpfe->buffer_queue)) {
2153                 vpfe_err(vpfe, "%s device busy\n", __func__);
2154                 return -EBUSY;
2155         }
2156
2157         if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2158                         s->target != V4L2_SEL_TGT_CROP)
2159                 return -EINVAL;
2160
2161         v4l_bound_align_image(&r.width, 0, cr.width, 0,
2162                               &r.height, 0, cr.height, 0, 0);
2163
2164         r.left = clamp_t(unsigned int, r.left, 0, cr.width - r.width);
2165         r.top  = clamp_t(unsigned int, r.top, 0, cr.height - r.height);
2166
2167         if (s->flags & V4L2_SEL_FLAG_LE && !enclosed_rectangle(&r, &s->r))
2168                 return -ERANGE;
2169
2170         if (s->flags & V4L2_SEL_FLAG_GE && !enclosed_rectangle(&s->r, &r))
2171                 return -ERANGE;
2172
2173         s->r = vpfe->crop = r;
2174
2175         vpfe_ccdc_set_image_window(&vpfe->ccdc, &r, vpfe->bpp);
2176         vpfe->fmt.fmt.pix.width = r.width;
2177         vpfe->fmt.fmt.pix.height = r.height;
2178         vpfe->fmt.fmt.pix.bytesperline = vpfe_ccdc_get_line_length(&vpfe->ccdc);
2179         vpfe->fmt.fmt.pix.sizeimage = vpfe->fmt.fmt.pix.bytesperline *
2180                                                 vpfe->fmt.fmt.pix.height;
2181
2182         vpfe_dbg(1, vpfe, "cropped (%d,%d)/%dx%d of %dx%d\n",
2183                  r.left, r.top, r.width, r.height, cr.width, cr.height);
2184
2185         return 0;
2186 }
2187
2188 static long vpfe_ioctl_default(struct file *file, void *priv,
2189                                bool valid_prio, unsigned int cmd, void *param)
2190 {
2191         struct vpfe_device *vpfe = video_drvdata(file);
2192         int ret;
2193
2194         vpfe_dbg(2, vpfe, "vpfe_ioctl_default\n");
2195
2196         if (!valid_prio) {
2197                 vpfe_err(vpfe, "%s device busy\n", __func__);
2198                 return -EBUSY;
2199         }
2200
2201         /* If streaming is started, return error */
2202         if (vb2_is_busy(&vpfe->buffer_queue)) {
2203                 vpfe_err(vpfe, "%s device busy\n", __func__);
2204                 return -EBUSY;
2205         }
2206
2207         switch (cmd) {
2208         case VIDIOC_AM437X_CCDC_CFG:
2209                 ret = vpfe_ccdc_set_params(&vpfe->ccdc, (void __user *)param);
2210                 if (ret) {
2211                         vpfe_dbg(2, vpfe,
2212                                 "Error setting parameters in CCDC\n");
2213                         return ret;
2214                 }
2215                 ret = vpfe_get_ccdc_image_format(vpfe,
2216                                                  &vpfe->fmt);
2217                 if (ret < 0) {
2218                         vpfe_dbg(2, vpfe,
2219                                 "Invalid image format at CCDC\n");
2220                         return ret;
2221                 }
2222                 break;
2223
2224         default:
2225                 ret = -ENOTTY;
2226                 break;
2227         }
2228
2229         return ret;
2230 }
2231
2232 static const struct vb2_ops vpfe_video_qops = {
2233         .wait_prepare           = vb2_ops_wait_prepare,
2234         .wait_finish            = vb2_ops_wait_finish,
2235         .queue_setup            = vpfe_queue_setup,
2236         .buf_prepare            = vpfe_buffer_prepare,
2237         .buf_queue              = vpfe_buffer_queue,
2238         .start_streaming        = vpfe_start_streaming,
2239         .stop_streaming         = vpfe_stop_streaming,
2240 };
2241
2242 /* vpfe capture driver file operations */
2243 static const struct v4l2_file_operations vpfe_fops = {
2244         .owner          = THIS_MODULE,
2245         .open           = vpfe_open,
2246         .release        = vpfe_release,
2247         .read           = vb2_fop_read,
2248         .poll           = vb2_fop_poll,
2249         .unlocked_ioctl = video_ioctl2,
2250         .mmap           = vb2_fop_mmap,
2251 };
2252
2253 /* vpfe capture ioctl operations */
2254 static const struct v4l2_ioctl_ops vpfe_ioctl_ops = {
2255         .vidioc_querycap                = vpfe_querycap,
2256         .vidioc_enum_fmt_vid_cap        = vpfe_enum_fmt,
2257         .vidioc_g_fmt_vid_cap           = vpfe_g_fmt,
2258         .vidioc_s_fmt_vid_cap           = vpfe_s_fmt,
2259         .vidioc_try_fmt_vid_cap         = vpfe_try_fmt,
2260
2261         .vidioc_enum_framesizes         = vpfe_enum_size,
2262
2263         .vidioc_enum_input              = vpfe_enum_input,
2264         .vidioc_g_input                 = vpfe_g_input,
2265         .vidioc_s_input                 = vpfe_s_input,
2266
2267         .vidioc_querystd                = vpfe_querystd,
2268         .vidioc_s_std                   = vpfe_s_std,
2269         .vidioc_g_std                   = vpfe_g_std,
2270
2271         .vidioc_reqbufs                 = vb2_ioctl_reqbufs,
2272         .vidioc_create_bufs             = vb2_ioctl_create_bufs,
2273         .vidioc_prepare_buf             = vb2_ioctl_prepare_buf,
2274         .vidioc_querybuf                = vb2_ioctl_querybuf,
2275         .vidioc_qbuf                    = vb2_ioctl_qbuf,
2276         .vidioc_dqbuf                   = vb2_ioctl_dqbuf,
2277         .vidioc_expbuf                  = vb2_ioctl_expbuf,
2278         .vidioc_streamon                = vb2_ioctl_streamon,
2279         .vidioc_streamoff               = vb2_ioctl_streamoff,
2280
2281         .vidioc_log_status              = v4l2_ctrl_log_status,
2282         .vidioc_subscribe_event         = v4l2_ctrl_subscribe_event,
2283         .vidioc_unsubscribe_event       = v4l2_event_unsubscribe,
2284
2285         .vidioc_cropcap                 = vpfe_cropcap,
2286         .vidioc_g_selection             = vpfe_g_selection,
2287         .vidioc_s_selection             = vpfe_s_selection,
2288
2289         .vidioc_default                 = vpfe_ioctl_default,
2290 };
2291
2292 static int
2293 vpfe_async_bound(struct v4l2_async_notifier *notifier,
2294                  struct v4l2_subdev *subdev,
2295                  struct v4l2_async_subdev *asd)
2296 {
2297         struct vpfe_device *vpfe = container_of(notifier->v4l2_dev,
2298                                                struct vpfe_device, v4l2_dev);
2299         struct v4l2_subdev_mbus_code_enum mbus_code;
2300         struct vpfe_subdev_info *sdinfo;
2301         bool found = false;
2302         int i, j;
2303
2304         vpfe_dbg(1, vpfe, "vpfe_async_bound\n");
2305
2306         for (i = 0; i < ARRAY_SIZE(vpfe->cfg->asd); i++) {
2307                 if (vpfe->cfg->asd[i]->match.of.node == asd[i].match.of.node) {
2308                         sdinfo = &vpfe->cfg->sub_devs[i];
2309                         vpfe->sd[i] = subdev;
2310                         vpfe->sd[i]->grp_id = sdinfo->grp_id;
2311                         found = true;
2312                         break;
2313                 }
2314         }
2315
2316         if (!found) {
2317                 vpfe_info(vpfe, "sub device (%s) not matched\n", subdev->name);
2318                 return -EINVAL;
2319         }
2320
2321         vpfe->video_dev.tvnorms |= sdinfo->inputs[0].std;
2322
2323         /* setup the supported formats & indexes */
2324         for (j = 0, i = 0; ; ++j) {
2325                 struct vpfe_fmt *fmt;
2326                 int ret;
2327
2328                 memset(&mbus_code, 0, sizeof(mbus_code));
2329                 mbus_code.index = j;
2330                 mbus_code.which = V4L2_SUBDEV_FORMAT_ACTIVE;
2331                 ret = v4l2_subdev_call(subdev, pad, enum_mbus_code,
2332                                NULL, &mbus_code);
2333                 if (ret)
2334                         break;
2335
2336                 fmt = find_format_by_code(mbus_code.code);
2337                 if (!fmt)
2338                         continue;
2339
2340                 fmt->supported = true;
2341                 fmt->index = i++;
2342         }
2343
2344         return 0;
2345 }
2346
2347 static int vpfe_probe_complete(struct vpfe_device *vpfe)
2348 {
2349         struct video_device *vdev;
2350         struct vb2_queue *q;
2351         int err;
2352
2353         spin_lock_init(&vpfe->dma_queue_lock);
2354         mutex_init(&vpfe->lock);
2355
2356         vpfe->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2357
2358         /* set first sub device as current one */
2359         vpfe->current_subdev = &vpfe->cfg->sub_devs[0];
2360         vpfe->v4l2_dev.ctrl_handler = vpfe->sd[0]->ctrl_handler;
2361
2362         err = vpfe_set_input(vpfe, 0);
2363         if (err)
2364                 goto probe_out;
2365
2366         /* Initialize videobuf2 queue as per the buffer type */
2367         vpfe->alloc_ctx = vb2_dma_contig_init_ctx(vpfe->pdev);
2368         if (IS_ERR(vpfe->alloc_ctx)) {
2369                 vpfe_err(vpfe, "Failed to get the context\n");
2370                 err = PTR_ERR(vpfe->alloc_ctx);
2371                 goto probe_out;
2372         }
2373
2374         q = &vpfe->buffer_queue;
2375         q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2376         q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
2377         q->drv_priv = vpfe;
2378         q->ops = &vpfe_video_qops;
2379         q->mem_ops = &vb2_dma_contig_memops;
2380         q->buf_struct_size = sizeof(struct vpfe_cap_buffer);
2381         q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
2382         q->lock = &vpfe->lock;
2383         q->min_buffers_needed = 1;
2384
2385         err = vb2_queue_init(q);
2386         if (err) {
2387                 vpfe_err(vpfe, "vb2_queue_init() failed\n");
2388                 vb2_dma_contig_cleanup_ctx(vpfe->alloc_ctx);
2389                 goto probe_out;
2390         }
2391
2392         INIT_LIST_HEAD(&vpfe->dma_queue);
2393
2394         vdev = &vpfe->video_dev;
2395         strlcpy(vdev->name, VPFE_MODULE_NAME, sizeof(vdev->name));
2396         vdev->release = video_device_release_empty;
2397         vdev->fops = &vpfe_fops;
2398         vdev->ioctl_ops = &vpfe_ioctl_ops;
2399         vdev->v4l2_dev = &vpfe->v4l2_dev;
2400         vdev->vfl_dir = VFL_DIR_RX;
2401         vdev->queue = q;
2402         vdev->lock = &vpfe->lock;
2403         video_set_drvdata(vdev, vpfe);
2404         err = video_register_device(&vpfe->video_dev, VFL_TYPE_GRABBER, -1);
2405         if (err) {
2406                 vpfe_err(vpfe,
2407                         "Unable to register video device.\n");
2408                 goto probe_out;
2409         }
2410
2411         return 0;
2412
2413 probe_out:
2414         v4l2_device_unregister(&vpfe->v4l2_dev);
2415         return err;
2416 }
2417
2418 static int vpfe_async_complete(struct v4l2_async_notifier *notifier)
2419 {
2420         struct vpfe_device *vpfe = container_of(notifier->v4l2_dev,
2421                                         struct vpfe_device, v4l2_dev);
2422
2423         return vpfe_probe_complete(vpfe);
2424 }
2425
2426 static struct vpfe_config *
2427 vpfe_get_pdata(struct platform_device *pdev)
2428 {
2429         struct device_node *endpoint = NULL;
2430         struct v4l2_of_endpoint bus_cfg;
2431         struct vpfe_subdev_info *sdinfo;
2432         struct vpfe_config *pdata;
2433         unsigned int flags;
2434         unsigned int i;
2435         int err;
2436
2437         dev_dbg(&pdev->dev, "vpfe_get_pdata\n");
2438
2439         if (!IS_ENABLED(CONFIG_OF) || !pdev->dev.of_node)
2440                 return pdev->dev.platform_data;
2441
2442         pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
2443         if (!pdata)
2444                 return NULL;
2445
2446         for (i = 0; ; i++) {
2447                 struct device_node *rem;
2448
2449                 endpoint = of_graph_get_next_endpoint(pdev->dev.of_node,
2450                                                       endpoint);
2451                 if (!endpoint)
2452                         break;
2453
2454                 sdinfo = &pdata->sub_devs[i];
2455                 sdinfo->grp_id = 0;
2456
2457                 /* we only support camera */
2458                 sdinfo->inputs[0].index = i;
2459                 strcpy(sdinfo->inputs[0].name, "Camera");
2460                 sdinfo->inputs[0].type = V4L2_INPUT_TYPE_CAMERA;
2461                 sdinfo->inputs[0].std = V4L2_STD_ALL;
2462                 sdinfo->inputs[0].capabilities = V4L2_IN_CAP_STD;
2463
2464                 sdinfo->can_route = 0;
2465                 sdinfo->routes = NULL;
2466
2467                 of_property_read_u32(endpoint, "ti,am437x-vpfe-interface",
2468                                      &sdinfo->vpfe_param.if_type);
2469                 if (sdinfo->vpfe_param.if_type < 0 ||
2470                         sdinfo->vpfe_param.if_type > 4) {
2471                         sdinfo->vpfe_param.if_type = VPFE_RAW_BAYER;
2472                 }
2473
2474                 err = v4l2_of_parse_endpoint(endpoint, &bus_cfg);
2475                 if (err) {
2476                         dev_err(&pdev->dev, "Could not parse the endpoint\n");
2477                         goto done;
2478                 }
2479
2480                 sdinfo->vpfe_param.bus_width = bus_cfg.bus.parallel.bus_width;
2481
2482                 if (sdinfo->vpfe_param.bus_width < 8 ||
2483                         sdinfo->vpfe_param.bus_width > 16) {
2484                         dev_err(&pdev->dev, "Invalid bus width.\n");
2485                         goto done;
2486                 }
2487
2488                 flags = bus_cfg.bus.parallel.flags;
2489
2490                 if (flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
2491                         sdinfo->vpfe_param.hdpol = 1;
2492
2493                 if (flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH)
2494                         sdinfo->vpfe_param.vdpol = 1;
2495
2496                 rem = of_graph_get_remote_port_parent(endpoint);
2497                 if (!rem) {
2498                         dev_err(&pdev->dev, "Remote device at %s not found\n",
2499                                 endpoint->full_name);
2500                         goto done;
2501                 }
2502
2503                 pdata->asd[i] = devm_kzalloc(&pdev->dev,
2504                                              sizeof(struct v4l2_async_subdev),
2505                                              GFP_KERNEL);
2506                 if (!pdata->asd[i]) {
2507                         of_node_put(rem);
2508                         pdata = NULL;
2509                         goto done;
2510                 }
2511
2512                 pdata->asd[i]->match_type = V4L2_ASYNC_MATCH_OF;
2513                 pdata->asd[i]->match.of.node = rem;
2514                 of_node_put(rem);
2515         }
2516
2517         of_node_put(endpoint);
2518         return pdata;
2519
2520 done:
2521         of_node_put(endpoint);
2522         return NULL;
2523 }
2524
2525 /*
2526  * vpfe_probe : This function creates device entries by register
2527  * itself to the V4L2 driver and initializes fields of each
2528  * device objects
2529  */
2530 static int vpfe_probe(struct platform_device *pdev)
2531 {
2532         struct vpfe_config *vpfe_cfg = vpfe_get_pdata(pdev);
2533         struct vpfe_device *vpfe;
2534         struct vpfe_ccdc *ccdc;
2535         struct resource *res;
2536         int ret;
2537
2538         if (!vpfe_cfg) {
2539                 dev_err(&pdev->dev, "No platform data\n");
2540                 return -EINVAL;
2541         }
2542
2543         vpfe = devm_kzalloc(&pdev->dev, sizeof(*vpfe), GFP_KERNEL);
2544         if (!vpfe)
2545                 return -ENOMEM;
2546
2547         vpfe->pdev = &pdev->dev;
2548         vpfe->cfg = vpfe_cfg;
2549         ccdc = &vpfe->ccdc;
2550
2551         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2552         ccdc->ccdc_cfg.base_addr = devm_ioremap_resource(&pdev->dev, res);
2553         if (IS_ERR(ccdc->ccdc_cfg.base_addr))
2554                 return PTR_ERR(ccdc->ccdc_cfg.base_addr);
2555
2556         ret = platform_get_irq(pdev, 0);
2557         if (ret <= 0) {
2558                 dev_err(&pdev->dev, "No IRQ resource\n");
2559                 return -ENODEV;
2560         }
2561         vpfe->irq = ret;
2562
2563         ret = devm_request_irq(vpfe->pdev, vpfe->irq, vpfe_isr, 0,
2564                                "vpfe_capture0", vpfe);
2565         if (ret) {
2566                 dev_err(&pdev->dev, "Unable to request interrupt\n");
2567                 return -EINVAL;
2568         }
2569
2570         ret = v4l2_device_register(&pdev->dev, &vpfe->v4l2_dev);
2571         if (ret) {
2572                 vpfe_err(vpfe,
2573                         "Unable to register v4l2 device.\n");
2574                 return ret;
2575         }
2576
2577         /* set the driver data in platform device */
2578         platform_set_drvdata(pdev, vpfe);
2579         /* Enabling module functional clock */
2580         pm_runtime_enable(&pdev->dev);
2581
2582         /* for now just enable it here instead of waiting for the open */
2583         pm_runtime_get_sync(&pdev->dev);
2584
2585         vpfe_ccdc_config_defaults(ccdc);
2586
2587         pm_runtime_put_sync(&pdev->dev);
2588
2589         vpfe->sd = devm_kzalloc(&pdev->dev, sizeof(struct v4l2_subdev *) *
2590                                 ARRAY_SIZE(vpfe->cfg->asd), GFP_KERNEL);
2591         if (!vpfe->sd) {
2592                 ret = -ENOMEM;
2593                 goto probe_out_v4l2_unregister;
2594         }
2595
2596         vpfe->notifier.subdevs = vpfe->cfg->asd;
2597         vpfe->notifier.num_subdevs = ARRAY_SIZE(vpfe->cfg->asd);
2598         vpfe->notifier.bound = vpfe_async_bound;
2599         vpfe->notifier.complete = vpfe_async_complete;
2600         ret = v4l2_async_notifier_register(&vpfe->v4l2_dev,
2601                                                 &vpfe->notifier);
2602         if (ret) {
2603                 vpfe_err(vpfe, "Error registering async notifier\n");
2604                 ret = -EINVAL;
2605                 goto probe_out_v4l2_unregister;
2606         }
2607
2608         return 0;
2609
2610 probe_out_v4l2_unregister:
2611         v4l2_device_unregister(&vpfe->v4l2_dev);
2612         return ret;
2613 }
2614
2615 /*
2616  * vpfe_remove : It un-register device from V4L2 driver
2617  */
2618 static int vpfe_remove(struct platform_device *pdev)
2619 {
2620         struct vpfe_device *vpfe = platform_get_drvdata(pdev);
2621
2622         vpfe_dbg(2, vpfe, "vpfe_remove\n");
2623
2624         pm_runtime_disable(&pdev->dev);
2625
2626         v4l2_async_notifier_unregister(&vpfe->notifier);
2627         v4l2_device_unregister(&vpfe->v4l2_dev);
2628         video_unregister_device(&vpfe->video_dev);
2629
2630         return 0;
2631 }
2632
2633 #ifdef CONFIG_PM_SLEEP
2634
2635 static void vpfe_save_context(struct vpfe_ccdc *ccdc)
2636 {
2637         ccdc->ccdc_ctx[VPFE_PCR >> 2] = vpfe_reg_read(ccdc, VPFE_PCR);
2638         ccdc->ccdc_ctx[VPFE_SYNMODE >> 2] = vpfe_reg_read(ccdc, VPFE_SYNMODE);
2639         ccdc->ccdc_ctx[VPFE_SDOFST >> 2] = vpfe_reg_read(ccdc, VPFE_SDOFST);
2640         ccdc->ccdc_ctx[VPFE_SDR_ADDR >> 2] = vpfe_reg_read(ccdc, VPFE_SDR_ADDR);
2641         ccdc->ccdc_ctx[VPFE_CLAMP >> 2] = vpfe_reg_read(ccdc, VPFE_CLAMP);
2642         ccdc->ccdc_ctx[VPFE_DCSUB >> 2] = vpfe_reg_read(ccdc, VPFE_DCSUB);
2643         ccdc->ccdc_ctx[VPFE_COLPTN >> 2] = vpfe_reg_read(ccdc, VPFE_COLPTN);
2644         ccdc->ccdc_ctx[VPFE_BLKCMP >> 2] = vpfe_reg_read(ccdc, VPFE_BLKCMP);
2645         ccdc->ccdc_ctx[VPFE_VDINT >> 2] = vpfe_reg_read(ccdc, VPFE_VDINT);
2646         ccdc->ccdc_ctx[VPFE_ALAW >> 2] = vpfe_reg_read(ccdc, VPFE_ALAW);
2647         ccdc->ccdc_ctx[VPFE_REC656IF >> 2] = vpfe_reg_read(ccdc, VPFE_REC656IF);
2648         ccdc->ccdc_ctx[VPFE_CCDCFG >> 2] = vpfe_reg_read(ccdc, VPFE_CCDCFG);
2649         ccdc->ccdc_ctx[VPFE_CULLING >> 2] = vpfe_reg_read(ccdc, VPFE_CULLING);
2650         ccdc->ccdc_ctx[VPFE_HD_VD_WID >> 2] = vpfe_reg_read(ccdc,
2651                                                             VPFE_HD_VD_WID);
2652         ccdc->ccdc_ctx[VPFE_PIX_LINES >> 2] = vpfe_reg_read(ccdc,
2653                                                             VPFE_PIX_LINES);
2654         ccdc->ccdc_ctx[VPFE_HORZ_INFO >> 2] = vpfe_reg_read(ccdc,
2655                                                             VPFE_HORZ_INFO);
2656         ccdc->ccdc_ctx[VPFE_VERT_START >> 2] = vpfe_reg_read(ccdc,
2657                                                              VPFE_VERT_START);
2658         ccdc->ccdc_ctx[VPFE_VERT_LINES >> 2] = vpfe_reg_read(ccdc,
2659                                                              VPFE_VERT_LINES);
2660         ccdc->ccdc_ctx[VPFE_HSIZE_OFF >> 2] = vpfe_reg_read(ccdc,
2661                                                             VPFE_HSIZE_OFF);
2662 }
2663
2664 static int vpfe_suspend(struct device *dev)
2665 {
2666         struct platform_device *pdev = to_platform_device(dev);
2667         struct vpfe_device *vpfe = platform_get_drvdata(pdev);
2668         struct vpfe_ccdc *ccdc = &vpfe->ccdc;
2669
2670         /* if streaming has not started we don't care */
2671         if (!vb2_start_streaming_called(&vpfe->buffer_queue))
2672                 return 0;
2673
2674         pm_runtime_get_sync(dev);
2675         vpfe_config_enable(ccdc, 1);
2676
2677         /* Save VPFE context */
2678         vpfe_save_context(ccdc);
2679
2680         /* Disable CCDC */
2681         vpfe_pcr_enable(ccdc, 0);
2682         vpfe_config_enable(ccdc, 0);
2683
2684         /* Disable both master and slave clock */
2685         pm_runtime_put_sync(dev);
2686
2687         /* Select sleep pin state */
2688         pinctrl_pm_select_sleep_state(dev);
2689
2690         return 0;
2691 }
2692
2693 static void vpfe_restore_context(struct vpfe_ccdc *ccdc)
2694 {
2695         vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_SYNMODE >> 2], VPFE_SYNMODE);
2696         vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_CULLING >> 2], VPFE_CULLING);
2697         vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_SDOFST >> 2], VPFE_SDOFST);
2698         vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_SDR_ADDR >> 2], VPFE_SDR_ADDR);
2699         vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_CLAMP >> 2], VPFE_CLAMP);
2700         vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_DCSUB >> 2], VPFE_DCSUB);
2701         vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_COLPTN >> 2], VPFE_COLPTN);
2702         vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_BLKCMP >> 2], VPFE_BLKCMP);
2703         vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_VDINT >> 2], VPFE_VDINT);
2704         vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_ALAW >> 2], VPFE_ALAW);
2705         vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_REC656IF >> 2], VPFE_REC656IF);
2706         vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_CCDCFG >> 2], VPFE_CCDCFG);
2707         vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_PCR >> 2], VPFE_PCR);
2708         vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_HD_VD_WID >> 2],
2709                                                 VPFE_HD_VD_WID);
2710         vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_PIX_LINES >> 2],
2711                                                 VPFE_PIX_LINES);
2712         vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_HORZ_INFO >> 2],
2713                                                 VPFE_HORZ_INFO);
2714         vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_VERT_START >> 2],
2715                                                 VPFE_VERT_START);
2716         vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_VERT_LINES >> 2],
2717                                                 VPFE_VERT_LINES);
2718         vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_HSIZE_OFF >> 2],
2719                                                 VPFE_HSIZE_OFF);
2720 }
2721
2722 static int vpfe_resume(struct device *dev)
2723 {
2724         struct platform_device *pdev = to_platform_device(dev);
2725         struct vpfe_device *vpfe = platform_get_drvdata(pdev);
2726         struct vpfe_ccdc *ccdc = &vpfe->ccdc;
2727
2728         /* if streaming has not started we don't care */
2729         if (!vb2_start_streaming_called(&vpfe->buffer_queue))
2730                 return 0;
2731
2732         /* Enable both master and slave clock */
2733         pm_runtime_get_sync(dev);
2734         vpfe_config_enable(ccdc, 1);
2735
2736         /* Restore VPFE context */
2737         vpfe_restore_context(ccdc);
2738
2739         vpfe_config_enable(ccdc, 0);
2740         pm_runtime_put_sync(dev);
2741
2742         /* Select default pin state */
2743         pinctrl_pm_select_default_state(dev);
2744
2745         return 0;
2746 }
2747
2748 #endif
2749
2750 static SIMPLE_DEV_PM_OPS(vpfe_pm_ops, vpfe_suspend, vpfe_resume);
2751
2752 static const struct of_device_id vpfe_of_match[] = {
2753         { .compatible = "ti,am437x-vpfe", },
2754         { /* sentinel */ },
2755 };
2756 MODULE_DEVICE_TABLE(of, vpfe_of_match);
2757
2758 static struct platform_driver vpfe_driver = {
2759         .probe          = vpfe_probe,
2760         .remove         = vpfe_remove,
2761         .driver = {
2762                 .name   = VPFE_MODULE_NAME,
2763                 .pm     = &vpfe_pm_ops,
2764                 .of_match_table = of_match_ptr(vpfe_of_match),
2765         },
2766 };
2767
2768 module_platform_driver(vpfe_driver);
2769
2770 MODULE_AUTHOR("Texas Instruments");
2771 MODULE_DESCRIPTION("TI AM437x VPFE driver");
2772 MODULE_LICENSE("GPL");
2773 MODULE_VERSION(VPFE_VERSION);