2 * TI VPFE capture Driver
4 * Copyright (C) 2013 - 2014 Texas Instruments, Inc.
6 * Benoit Parrot <bparrot@ti.com>
7 * Lad, Prabhakar <prabhakar.csengg@gmail.com>
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.
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
23 #include <linux/delay.h>
24 #include <linux/err.h>
25 #include <linux/init.h>
26 #include <linux/interrupt.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>
36 #include <media/v4l2-common.h>
37 #include <media/v4l2-ctrls.h>
38 #include <media/v4l2-event.h>
39 #include <media/v4l2-of.h>
41 #include "am437x-vpfe.h"
43 #define VPFE_MODULE_NAME "vpfe"
44 #define VPFE_VERSION "0.1.0"
47 module_param(debug, int, 0644);
48 MODULE_PARM_DESC(debug, "Debug level 0-8");
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)
57 /* standard information */
58 struct vpfe_standard {
62 struct v4l2_fract pixelaspect;
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},
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
97 static struct vpfe_fmt formats[] = {
99 .name = "YUV 4:2:2 packed, YCbYCr",
100 .fourcc = V4L2_PIX_FMT_YUYV,
101 .code = MEDIA_BUS_FMT_YUYV8_2X8,
108 .name = "YUV 4:2:2 packed, CbYCrY",
109 .fourcc = V4L2_PIX_FMT_UYVY,
110 .code = MEDIA_BUS_FMT_UYVY8_2X8,
117 .name = "YUV 4:2:2 packed, YCrYCb",
118 .fourcc = V4L2_PIX_FMT_YVYU,
119 .code = MEDIA_BUS_FMT_YVYU8_2X8,
126 .name = "YUV 4:2:2 packed, CrYCbY",
127 .fourcc = V4L2_PIX_FMT_VYUY,
128 .code = MEDIA_BUS_FMT_VYUY8_2X8,
136 .fourcc = V4L2_PIX_FMT_SBGGR8,
137 .code = MEDIA_BUS_FMT_SBGGR8_1X8,
145 .fourcc = V4L2_PIX_FMT_SGBRG8,
146 .code = MEDIA_BUS_FMT_SGBRG8_1X8,
154 .fourcc = V4L2_PIX_FMT_SGRBG8,
155 .code = MEDIA_BUS_FMT_SGRBG8_1X8,
163 .fourcc = V4L2_PIX_FMT_SRGGB8,
164 .code = MEDIA_BUS_FMT_SRGGB8_1X8,
171 .name = "RGB565 (LE)",
172 .fourcc = V4L2_PIX_FMT_RGB565,
173 .code = MEDIA_BUS_FMT_RGB565_2X8_LE,
180 .name = "RGB565 (BE)",
181 .fourcc = V4L2_PIX_FMT_RGB565X,
182 .code = MEDIA_BUS_FMT_RGB565_2X8_BE,
192 __vpfe_get_format(struct vpfe_device *vpfe,
193 struct v4l2_format *format, unsigned int *bpp);
195 static struct vpfe_fmt *find_format_by_code(unsigned int code)
197 struct vpfe_fmt *fmt;
200 for (k = 0; k < ARRAY_SIZE(formats); k++) {
202 if (fmt->code == code)
209 static struct vpfe_fmt *find_format_by_pix(unsigned int pixelformat)
211 struct vpfe_fmt *fmt;
214 for (k = 0; k < ARRAY_SIZE(formats); k++) {
216 if (fmt->fourcc == pixelformat)
224 mbus_to_pix(struct vpfe_device *vpfe,
225 const struct v4l2_mbus_framefmt *mbus,
226 struct v4l2_pix_format *pix, unsigned int *bpp)
228 struct vpfe_subdev_info *sdinfo = vpfe->current_subdev;
229 unsigned int bus_width = sdinfo->vpfe_param.bus_width;
230 struct vpfe_fmt *fmt;
232 fmt = find_format_by_code(mbus->code);
233 if (WARN_ON(fmt == NULL)) {
234 pr_err("Invalid mbus code set\n");
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;
244 /* pitch should be 32 bytes aligned */
245 pix->bytesperline = ALIGN(pix->width * *bpp, 32);
246 pix->sizeimage = pix->bytesperline * pix->height;
249 static void pix_to_mbus(struct vpfe_device *vpfe,
250 struct v4l2_pix_format *pix_fmt,
251 struct v4l2_mbus_framefmt *mbus_fmt)
253 struct vpfe_fmt *fmt;
255 fmt = find_format_by_pix(pix_fmt->pixelformat);
257 /* default to first entry */
258 vpfe_dbg(3, vpfe, "Invalid pixel code: %x, default used instead\n",
259 pix_fmt->pixelformat);
263 memset(mbus_fmt, 0, sizeof(*mbus_fmt));
264 v4l2_fill_mbus_format(mbus_fmt, pix_fmt, fmt->code);
267 /* Print Four-character-code (FOURCC) */
268 static char *print_fourcc(u32 fmt)
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);
282 cmp_v4l2_format(const struct v4l2_format *lhs, const struct v4l2_format *rhs)
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;
295 static inline u32 vpfe_reg_read(struct vpfe_ccdc *ccdc, u32 offset)
297 return ioread32(ccdc->ccdc_cfg.base_addr + offset);
300 static inline void vpfe_reg_write(struct vpfe_ccdc *ccdc, u32 val, u32 offset)
302 iowrite32(val, ccdc->ccdc_cfg.base_addr + offset);
305 static inline struct vpfe_device *to_vpfe(struct vpfe_ccdc *ccdc)
307 return container_of(ccdc, struct vpfe_device, ccdc);
311 struct vpfe_cap_buffer *to_vpfe_buffer(struct vb2_v4l2_buffer *vb)
313 return container_of(vb, struct vpfe_cap_buffer, vb);
316 static inline void vpfe_pcr_enable(struct vpfe_ccdc *ccdc, int flag)
318 vpfe_reg_write(ccdc, !!flag, VPFE_PCR);
321 static void vpfe_config_enable(struct vpfe_ccdc *ccdc, int flag)
326 cfg = vpfe_reg_read(ccdc, VPFE_CONFIG);
327 cfg &= ~(VPFE_CONFIG_EN_ENABLE << VPFE_CONFIG_EN_SHIFT);
329 cfg = VPFE_CONFIG_EN_ENABLE << VPFE_CONFIG_EN_SHIFT;
332 vpfe_reg_write(ccdc, cfg, VPFE_CONFIG);
335 static void vpfe_ccdc_setwin(struct vpfe_ccdc *ccdc,
336 struct v4l2_rect *image_win,
337 enum ccdc_frmfmt frm_fmt,
340 int horz_start, horz_nr_pixels;
341 int vert_start, vert_nr_lines;
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
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);
354 vert_start = image_win->top;
356 if (frm_fmt == CCDC_FRMFMT_INTERLACED) {
357 vert_nr_lines = (image_win->height >> 1) - 1;
359 /* Since first line doesn't have any data */
361 /* configure VDINT0 */
362 val = (vert_start << VPFE_VDINT_VDINT0_SHIFT);
364 /* Since first line doesn't have any data */
366 vert_nr_lines = image_win->height - 1;
368 * configure VDINT0 and VDINT1. VDINT1 will be at half
371 mid_img = vert_start + (image_win->height / 2);
372 val = (vert_start << VPFE_VDINT_VDINT0_SHIFT) |
373 (mid_img & VPFE_VDINT_VDINT1_MASK);
376 vpfe_reg_write(ccdc, val, VPFE_VDINT);
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);
383 static void vpfe_reg_dump(struct vpfe_ccdc *ccdc)
385 struct vpfe_device *vpfe = to_vpfe(ccdc);
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));
406 vpfe_ccdc_validate_param(struct vpfe_ccdc *ccdc,
407 struct vpfe_ccdc_config_params_raw *ccdcparam)
409 struct vpfe_device *vpfe = to_vpfe(ccdc);
410 u8 max_gamma, max_data;
412 if (!ccdcparam->alaw.enable)
415 max_gamma = ccdc_gamma_width_max_bit(ccdcparam->alaw.gamma_wd);
416 max_data = ccdc_data_size_max_bit(ccdcparam->data_sz);
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");
429 vpfe_ccdc_update_raw_params(struct vpfe_ccdc *ccdc,
430 struct vpfe_ccdc_config_params_raw *raw_params)
432 struct vpfe_ccdc_config_params_raw *config_params =
433 &ccdc->ccdc_cfg.bayer.config_params;
435 *config_params = *raw_params;
439 * vpfe_ccdc_restore_defaults()
440 * This function will write defaults to all CCDC registers
442 static void vpfe_ccdc_restore_defaults(struct vpfe_ccdc *ccdc)
447 vpfe_pcr_enable(ccdc, 0);
449 /* set all registers to default value */
450 for (i = 4; i <= 0x94; i += 4)
451 vpfe_reg_write(ccdc, 0, i);
453 vpfe_reg_write(ccdc, VPFE_NO_CULLING, VPFE_CULLING);
454 vpfe_reg_write(ccdc, VPFE_CCDC_GAMMA_BITS_11_2, VPFE_ALAW);
457 static int vpfe_ccdc_close(struct vpfe_ccdc *ccdc, struct device *dev)
459 int dma_cntl, i, pcr;
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);
468 /* make sure it it is disabled */
469 vpfe_pcr_enable(ccdc, 0);
472 /* Disable CCDC by resetting all register to default POR values */
473 vpfe_ccdc_restore_defaults(ccdc);
475 /* if DMA_CNTL overflow bit is set. Clear it
476 * It appears to take a while for this to become quiescent ~20ms
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))
483 /* Clear the overflow bit */
484 vpfe_reg_write(ccdc, dma_cntl, VPFE_DMA_CNTL);
485 usleep_range(5000, 6000);
488 /* Disabled the module at the CONFIG level */
489 vpfe_config_enable(ccdc, 0);
491 pm_runtime_put_sync(dev);
496 static int vpfe_ccdc_set_params(struct vpfe_ccdc *ccdc, void __user *params)
498 struct vpfe_device *vpfe = container_of(ccdc, struct vpfe_device, ccdc);
499 struct vpfe_ccdc_config_params_raw raw_params;
502 if (ccdc->ccdc_cfg.if_type != VPFE_RAW_BAYER)
505 x = copy_from_user(&raw_params, params, sizeof(raw_params));
508 "vpfe_ccdc_set_params: error in copying ccdc params, %d\n",
513 if (!vpfe_ccdc_validate_param(ccdc, &raw_params)) {
514 vpfe_ccdc_update_raw_params(ccdc, &raw_params);
522 * vpfe_ccdc_config_ycbcr()
523 * This function will configure CCDC for YCbCr video capture
525 static void vpfe_ccdc_config_ycbcr(struct vpfe_ccdc *ccdc)
527 struct vpfe_device *vpfe = container_of(ccdc, struct vpfe_device, ccdc);
528 struct ccdc_params_ycbcr *params = &ccdc->ccdc_cfg.ycbcr;
531 vpfe_dbg(3, vpfe, "vpfe_ccdc_config_ycbcr:\n");
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
537 vpfe_ccdc_restore_defaults(ccdc);
540 * configure pixel format, frame format, configure video frame
541 * format, enable output to SDRAM, enable internal timing generator
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);
550 /* setup BT.656 sync mode */
551 if (params->bt656_enable) {
552 vpfe_reg_write(ccdc, VPFE_REC656IF_BT656_EN, VPFE_REC656IF);
555 * configure the FID, VD, HD pin polarity,
556 * fld,hd pol positive, vd negative, 8-bit data
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;
562 syn_mode |= VPFE_SYN_MODE_8BITS;
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) <<
569 ((params->vd_pol & VPFE_VD_POL_MASK) <<
572 vpfe_reg_write(ccdc, syn_mode, VPFE_SYNMODE);
574 /* configure video window */
575 vpfe_ccdc_setwin(ccdc, ¶ms->win,
576 params->frm_fmt, params->bytesperpixel);
579 * configure the order of y cb cr in SDRAM, and disable latch
580 * internal register on vsync
582 if (ccdc->ccdc_cfg.if_type == VPFE_BT656_10BIT)
584 (params->pix_order << VPFE_CCDCFG_Y8POS_SHIFT) |
585 VPFE_LATCH_ON_VSYNC_DISABLE |
586 VPFE_CCDCFG_BW656_10BIT, VPFE_CCDCFG);
589 (params->pix_order << VPFE_CCDCFG_Y8POS_SHIFT) |
590 VPFE_LATCH_ON_VSYNC_DISABLE, VPFE_CCDCFG);
593 * configure the horizontal line offset. This should be a
594 * on 32 byte boundary. So clear LSB 5 bits
596 vpfe_reg_write(ccdc, params->bytesperline, VPFE_HSIZE_OFF);
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,
606 vpfe_ccdc_config_black_clamp(struct vpfe_ccdc *ccdc,
607 struct vpfe_ccdc_black_clamp *bclamp)
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);
619 * Configure gain, Start pixel, No of line to be avg,
620 * No of pixel/line to be avg, & Enable the Black clamping
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);
635 vpfe_ccdc_config_black_compense(struct vpfe_ccdc *ccdc,
636 struct vpfe_ccdc_black_compensation *bcomp)
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);
651 * vpfe_ccdc_config_raw()
652 * This function will configure CCDC for Raw capture mode
654 static void vpfe_ccdc_config_raw(struct vpfe_ccdc *ccdc)
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;
663 vpfe_dbg(3, vpfe, "vpfe_ccdc_config_raw:\n");
666 vpfe_ccdc_restore_defaults(ccdc);
668 /* Disable latching function registers on VSYNC */
669 vpfe_reg_write(ccdc, VPFE_LATCH_ON_VSYNC_DISABLE, VPFE_CCDCFG);
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
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);
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);
696 /* Configure video window */
697 vpfe_ccdc_setwin(ccdc, ¶ms->win, params->frm_fmt,
698 params->bytesperpixel);
700 /* Configure Black Clamp */
701 vpfe_ccdc_config_black_clamp(ccdc, &config_params->blk_clamp);
703 /* Configure Black level compensation */
704 vpfe_ccdc_config_black_compense(ccdc, &config_params->blk_comp);
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;
712 * Configure Horizontal offset register. If pack 8 is enabled then
713 * 1 pixel will take 1 byte
715 vpfe_reg_write(ccdc, params->bytesperline, VPFE_HSIZE_OFF);
717 vpfe_dbg(3, vpfe, "Writing %d (%x) to HSIZE_OFF\n",
718 params->bytesperline, params->bytesperline);
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,
727 /* For interlace non inverse mode */
728 vpfe_reg_write(ccdc, VPFE_INTERLACED_NO_IMAGE_INVERT,
731 } else if (params->frm_fmt == CCDC_FRMFMT_PROGRESSIVE) {
732 vpfe_reg_write(ccdc, VPFE_PROGRESSIVE_NO_IMAGE_INVERT,
736 vpfe_reg_write(ccdc, syn_mode, VPFE_SYNMODE);
742 vpfe_ccdc_set_buftype(struct vpfe_ccdc *ccdc,
743 enum ccdc_buftype buf_type)
745 if (ccdc->ccdc_cfg.if_type == VPFE_RAW_BAYER)
746 ccdc->ccdc_cfg.bayer.buf_type = buf_type;
748 ccdc->ccdc_cfg.ycbcr.buf_type = buf_type;
753 static inline enum ccdc_buftype vpfe_ccdc_get_buftype(struct vpfe_ccdc *ccdc)
755 if (ccdc->ccdc_cfg.if_type == VPFE_RAW_BAYER)
756 return ccdc->ccdc_cfg.bayer.buf_type;
758 return ccdc->ccdc_cfg.ycbcr.buf_type;
761 static int vpfe_ccdc_set_pixel_format(struct vpfe_ccdc *ccdc, u32 pixfmt)
763 struct vpfe_device *vpfe = container_of(ccdc, struct vpfe_device, ccdc);
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));
768 if (ccdc->ccdc_cfg.if_type == VPFE_RAW_BAYER) {
769 ccdc->ccdc_cfg.bayer.pix_fmt = CCDC_PIXFMT_RAW;
771 * Need to clear it in case it was left on
772 * after the last capture.
774 ccdc->ccdc_cfg.bayer.config_params.alaw.enable = 0;
777 case V4L2_PIX_FMT_SBGGR8:
778 ccdc->ccdc_cfg.bayer.config_params.alaw.enable = 1;
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:
788 case V4L2_PIX_FMT_SBGGR16:
794 case V4L2_PIX_FMT_YUYV:
795 ccdc->ccdc_cfg.ycbcr.pix_order = CCDC_PIXORDER_YCBYCR;
798 case V4L2_PIX_FMT_UYVY:
799 ccdc->ccdc_cfg.ycbcr.pix_order = CCDC_PIXORDER_CBYCRY;
810 static u32 vpfe_ccdc_get_pixel_format(struct vpfe_ccdc *ccdc)
814 if (ccdc->ccdc_cfg.if_type == VPFE_RAW_BAYER) {
815 pixfmt = V4L2_PIX_FMT_YUYV;
817 if (ccdc->ccdc_cfg.ycbcr.pix_order == CCDC_PIXORDER_YCBYCR)
818 pixfmt = V4L2_PIX_FMT_YUYV;
820 pixfmt = V4L2_PIX_FMT_UYVY;
827 vpfe_ccdc_set_image_window(struct vpfe_ccdc *ccdc,
828 struct v4l2_rect *win, unsigned int bpp)
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);
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);
844 vpfe_ccdc_get_image_window(struct vpfe_ccdc *ccdc,
845 struct v4l2_rect *win)
847 if (ccdc->ccdc_cfg.if_type == VPFE_RAW_BAYER)
848 *win = ccdc->ccdc_cfg.bayer.win;
850 *win = ccdc->ccdc_cfg.ycbcr.win;
853 static inline unsigned int vpfe_ccdc_get_line_length(struct vpfe_ccdc *ccdc)
855 if (ccdc->ccdc_cfg.if_type == VPFE_RAW_BAYER)
856 return ccdc->ccdc_cfg.bayer.bytesperline;
858 return ccdc->ccdc_cfg.ycbcr.bytesperline;
862 vpfe_ccdc_set_frame_format(struct vpfe_ccdc *ccdc,
863 enum ccdc_frmfmt frm_fmt)
865 if (ccdc->ccdc_cfg.if_type == VPFE_RAW_BAYER)
866 ccdc->ccdc_cfg.bayer.frm_fmt = frm_fmt;
868 ccdc->ccdc_cfg.ycbcr.frm_fmt = frm_fmt;
873 static inline enum ccdc_frmfmt
874 vpfe_ccdc_get_frame_format(struct vpfe_ccdc *ccdc)
876 if (ccdc->ccdc_cfg.if_type == VPFE_RAW_BAYER)
877 return ccdc->ccdc_cfg.bayer.frm_fmt;
879 return ccdc->ccdc_cfg.ycbcr.frm_fmt;
882 static inline int vpfe_ccdc_getfid(struct vpfe_ccdc *ccdc)
884 return (vpfe_reg_read(ccdc, VPFE_SYNMODE) >> 15) & 1;
887 static inline void vpfe_set_sdr_addr(struct vpfe_ccdc *ccdc, unsigned long addr)
889 vpfe_reg_write(ccdc, addr & 0xffffffe0, VPFE_SDR_ADDR);
892 static int vpfe_ccdc_set_hw_if_params(struct vpfe_ccdc *ccdc,
893 struct vpfe_hw_if_param *params)
895 struct vpfe_device *vpfe = container_of(ccdc, struct vpfe_device, ccdc);
897 ccdc->ccdc_cfg.if_type = params->if_type;
899 switch (params->if_type) {
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;
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;
915 ccdc->ccdc_cfg.bayer.config_params.data_sz =
916 VPFE_CCDC_DATA_8BITS;
917 vpfe_dbg(1, vpfe, "params.bus_width: %d\n",
919 vpfe_dbg(1, vpfe, "config_params.data_sz: %d\n",
920 ccdc->ccdc_cfg.bayer.config_params.data_sz);
930 static void vpfe_clear_intr(struct vpfe_ccdc *ccdc, int vdint)
932 unsigned int vpfe_int_status;
934 vpfe_int_status = vpfe_reg_read(ccdc, VPFE_IRQ_STS);
939 vpfe_int_status &= ~VPFE_VDINT0;
940 vpfe_int_status |= VPFE_VDINT0;
945 vpfe_int_status &= ~VPFE_VDINT1;
946 vpfe_int_status |= VPFE_VDINT1;
951 vpfe_int_status &= ~VPFE_VDINT2;
952 vpfe_int_status |= VPFE_VDINT2;
955 /* Clear all interrupts */
957 vpfe_int_status &= ~(VPFE_VDINT0 |
960 vpfe_int_status |= (VPFE_VDINT0 |
965 /* Clear specific VDINT from the status register */
966 vpfe_reg_write(ccdc, vpfe_int_status, VPFE_IRQ_STS);
968 vpfe_int_status = vpfe_reg_read(ccdc, VPFE_IRQ_STS);
970 /* Acknowledge that we are done with all interrupts */
971 vpfe_reg_write(ccdc, 1, VPFE_IRQ_EOI);
974 static void vpfe_ccdc_config_defaults(struct vpfe_ccdc *ccdc)
976 ccdc->ccdc_cfg.if_type = VPFE_RAW_BAYER;
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;
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;
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;
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;
1008 * vpfe_get_ccdc_image_format - Get image parameters based on CCDC settings
1010 static int vpfe_get_ccdc_image_format(struct vpfe_device *vpfe,
1011 struct v4l2_format *f)
1013 struct v4l2_rect image_win;
1014 enum ccdc_buftype buf_type;
1015 enum ccdc_frmfmt frm_fmt;
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 *
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);
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;
1037 vpfe_err(vpfe, "Invalid buf_type\n");
1041 vpfe_err(vpfe, "Invalid frm_fmt\n");
1047 static int vpfe_config_ccdc_image_format(struct vpfe_device *vpfe)
1049 enum ccdc_frmfmt frm_fmt = CCDC_FRMFMT_INTERLACED;
1052 vpfe_dbg(2, vpfe, "vpfe_config_ccdc_image_format\n");
1054 vpfe_dbg(1, vpfe, "pixelformat: %s\n",
1055 print_fourcc(vpfe->fmt.fmt.pix.pixelformat));
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");
1063 /* configure the image window */
1064 vpfe_ccdc_set_image_window(&vpfe->ccdc, &vpfe->crop, vpfe->bpp);
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(
1071 CCDC_BUFTYPE_FLD_INTERLEAVED);
1074 case V4L2_FIELD_NONE:
1075 frm_fmt = CCDC_FRMFMT_PROGRESSIVE;
1076 /* buffer type only applicable for interlaced scan */
1079 case V4L2_FIELD_SEQ_TB:
1080 ret = vpfe_ccdc_set_buftype(
1082 CCDC_BUFTYPE_FLD_SEPARATED);
1092 return vpfe_ccdc_set_frame_format(&vpfe->ccdc, frm_fmt);
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
1105 static int vpfe_config_image_format(struct vpfe_device *vpfe,
1108 struct v4l2_pix_format *pix = &vpfe->fmt.fmt.pix;
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;
1125 if (i == ARRAY_SIZE(vpfe_standards)) {
1126 vpfe_err(vpfe, "standard not supported\n");
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;
1137 /* first field and frame format based on standard frame format */
1138 if (vpfe->std_info.frame_format)
1139 pix->field = V4L2_FIELD_INTERLACED;
1141 pix->field = V4L2_FIELD_NONE;
1143 ret = __vpfe_get_format(vpfe, &vpfe->fmt, &vpfe->bpp);
1147 /* Update the crop window based on found values */
1148 vpfe->crop.width = pix->width;
1149 vpfe->crop.height = pix->height;
1151 return vpfe_config_ccdc_image_format(vpfe);
1154 static int vpfe_initialize_device(struct vpfe_device *vpfe)
1156 struct vpfe_subdev_info *sdinfo;
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);
1169 pm_runtime_get_sync(vpfe->pdev);
1171 vpfe_config_enable(&vpfe->ccdc, 1);
1173 vpfe_ccdc_restore_defaults(&vpfe->ccdc);
1175 /* Clear all VPFE interrupts */
1176 vpfe_clear_intr(&vpfe->ccdc, -1);
1182 * vpfe_release : This function is based on the vb2_fop_release
1184 * It has been augmented to handle module power management,
1185 * by disabling/enabling h/w module fcntl clock when necessary.
1187 static int vpfe_release(struct file *file)
1189 struct vpfe_device *vpfe = video_drvdata(file);
1193 mutex_lock(&vpfe->lock);
1195 /* Save the singular status before we call the clean-up helper */
1196 fh_singular = v4l2_fh_is_singular_file(file);
1198 /* the release helper will cleanup any on-going streaming */
1199 ret = _vb2_fop_release(file, NULL);
1202 * If this was the last open file.
1203 * Then de-initialize hw module.
1206 vpfe_ccdc_close(&vpfe->ccdc, vpfe->pdev);
1208 mutex_unlock(&vpfe->lock);
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.
1218 static int vpfe_open(struct file *file)
1220 struct vpfe_device *vpfe = video_drvdata(file);
1223 mutex_lock(&vpfe->lock);
1225 ret = v4l2_fh_open(file);
1227 vpfe_err(vpfe, "v4l2_fh_open failed\n");
1231 if (!v4l2_fh_is_singular_file(file))
1234 if (vpfe_initialize_device(vpfe)) {
1235 v4l2_fh_release(file);
1240 mutex_unlock(&vpfe->lock);
1245 * vpfe_schedule_next_buffer: set next buffer address for capture
1246 * @vpfe : ptr to vpfe device
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
1252 * Assumes caller is holding vpfe->dma_queue_lock already
1254 static inline void vpfe_schedule_next_buffer(struct vpfe_device *vpfe)
1256 vpfe->next_frm = list_entry(vpfe->dma_queue.next,
1257 struct vpfe_cap_buffer, list);
1258 list_del(&vpfe->next_frm->list);
1260 vpfe_set_sdr_addr(&vpfe->ccdc,
1261 vb2_dma_contig_plane_dma_addr(&vpfe->next_frm->vb.vb2_buf, 0));
1264 static inline void vpfe_schedule_bottom_field(struct vpfe_device *vpfe)
1268 addr = vb2_dma_contig_plane_dma_addr(&vpfe->next_frm->vb.vb2_buf, 0) +
1271 vpfe_set_sdr_addr(&vpfe->ccdc, addr);
1275 * vpfe_process_buffer_complete: process a completed buffer
1276 * @vpfe : ptr to vpfe device
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
1282 static inline void vpfe_process_buffer_complete(struct vpfe_device *vpfe)
1284 v4l2_get_timestamp(&vpfe->cur_frm->vb.timestamp);
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;
1292 * vpfe_isr : ISR handler for vpfe capture (VINT0)
1294 * @dev_id: dev_id ptr
1296 * It changes status of the captured buffer, takes next buffer from the queue
1297 * and sets its address in VPFE registers
1299 static irqreturn_t vpfe_isr(int irq, void *dev)
1301 struct vpfe_device *vpfe = (struct vpfe_device *)dev;
1302 enum v4l2_field field;
1306 intr_status = vpfe_reg_read(&vpfe->ccdc, VPFE_IRQ_STS);
1308 if (intr_status & VPFE_VDINT0) {
1309 field = vpfe->fmt.fmt.pix.field;
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);
1318 /* interlaced or TB capture check which field
1319 we are in hardware */
1320 fid = vpfe_ccdc_getfid(&vpfe->ccdc);
1322 /* switch the software maintained field id */
1324 if (fid == vpfe->field) {
1325 /* we are in-sync here,continue */
1328 * One frame is just being captured. If the
1329 * next frame is available, release the
1330 * current frame and move on
1332 if (vpfe->cur_frm != vpfe->next_frm)
1333 vpfe_process_buffer_complete(vpfe);
1335 * based on whether the two fields are stored
1336 * interleave or separately in memory,
1337 * reconfigure the CCDC memory address
1339 if (field == V4L2_FIELD_SEQ_TB)
1340 vpfe_schedule_bottom_field(vpfe);
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
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) {
1357 * out of sync. Recover from any hardware out-of-sync.
1358 * May loose one frame
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);
1374 vpfe_clear_intr(&vpfe->ccdc, intr_status);
1379 static inline void vpfe_detach_irq(struct vpfe_device *vpfe)
1381 unsigned int intr = VPFE_VDINT0;
1382 enum ccdc_frmfmt frame_format;
1384 frame_format = vpfe_ccdc_get_frame_format(&vpfe->ccdc);
1385 if (frame_format == CCDC_FRMFMT_PROGRESSIVE)
1386 intr |= VPFE_VDINT1;
1388 vpfe_reg_write(&vpfe->ccdc, intr, VPFE_IRQ_EN_CLR);
1391 static inline void vpfe_attach_irq(struct vpfe_device *vpfe)
1393 unsigned int intr = VPFE_VDINT0;
1394 enum ccdc_frmfmt frame_format;
1396 frame_format = vpfe_ccdc_get_frame_format(&vpfe->ccdc);
1397 if (frame_format == CCDC_FRMFMT_PROGRESSIVE)
1398 intr |= VPFE_VDINT1;
1400 vpfe_reg_write(&vpfe->ccdc, intr, VPFE_IRQ_EN_SET);
1403 static int vpfe_querycap(struct file *file, void *priv,
1404 struct v4l2_capability *cap)
1406 struct vpfe_device *vpfe = video_drvdata(file);
1408 vpfe_dbg(2, vpfe, "vpfe_querycap\n");
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 |
1416 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
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)
1425 struct v4l2_mbus_framefmt mbus_fmt;
1426 struct vpfe_subdev_info *sdinfo;
1427 struct v4l2_subdev_format fmt;
1430 sdinfo = vpfe->current_subdev;
1434 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1437 ret = v4l2_subdev_call(sdinfo->sd, pad, get_fmt, NULL, &fmt);
1438 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
1442 v4l2_fill_pix_format(&format->fmt.pix, &fmt.format);
1443 mbus_to_pix(vpfe, &fmt.format, &format->fmt.pix, bpp);
1445 ret = v4l2_device_call_until_err(&vpfe->v4l2_dev,
1449 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
1451 v4l2_fill_pix_format(&format->fmt.pix, &mbus_fmt);
1452 mbus_to_pix(vpfe, &mbus_fmt, &format->fmt.pix, bpp);
1455 format->type = vpfe->fmt.type;
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);
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)
1470 struct vpfe_subdev_info *sdinfo;
1471 struct v4l2_subdev_format fmt;
1474 vpfe_dbg(2, vpfe, "__vpfe_set_format\n");
1476 sdinfo = vpfe->current_subdev;
1480 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1483 pix_to_mbus(vpfe, &format->fmt.pix, &fmt.format);
1485 ret = v4l2_subdev_call(sdinfo->sd, pad, set_fmt, NULL, &fmt);
1489 v4l2_fill_pix_format(&format->fmt.pix, &fmt.format);
1490 mbus_to_pix(vpfe, &fmt.format, &format->fmt.pix, bpp);
1492 format->type = vpfe->fmt.type;
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);
1503 static int vpfe_g_fmt(struct file *file, void *priv,
1504 struct v4l2_format *fmt)
1506 struct vpfe_device *vpfe = video_drvdata(file);
1508 vpfe_dbg(2, vpfe, "vpfe_g_fmt\n");
1515 static int vpfe_enum_fmt(struct file *file, void *priv,
1516 struct v4l2_fmtdesc *f)
1518 struct vpfe_device *vpfe = video_drvdata(file);
1519 struct vpfe_subdev_info *sdinfo;
1520 struct vpfe_fmt *fmt = NULL;
1523 vpfe_dbg(2, vpfe, "vpfe_enum_format index:%d\n",
1526 sdinfo = vpfe->current_subdev;
1530 if (f->index > ARRAY_SIZE(formats))
1533 for (k = 0; k < ARRAY_SIZE(formats); k++) {
1534 if (formats[k].index == f->index) {
1542 strncpy(f->description, fmt->name, sizeof(f->description) - 1);
1543 f->pixelformat = fmt->fourcc;
1544 f->type = vpfe->fmt.type;
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);
1552 static int vpfe_try_fmt(struct file *file, void *priv,
1553 struct v4l2_format *fmt)
1555 struct vpfe_device *vpfe = video_drvdata(file);
1558 vpfe_dbg(2, vpfe, "vpfe_try_fmt\n");
1560 return __vpfe_get_format(vpfe, fmt, &bpp);
1563 static int vpfe_s_fmt(struct file *file, void *priv,
1564 struct v4l2_format *fmt)
1566 struct vpfe_device *vpfe = video_drvdata(file);
1567 struct v4l2_format format;
1571 vpfe_dbg(2, vpfe, "vpfe_s_fmt\n");
1573 /* If streaming is started, return error */
1574 if (vb2_is_busy(&vpfe->buffer_queue)) {
1575 vpfe_err(vpfe, "%s device busy\n", __func__);
1579 ret = vpfe_try_fmt(file, priv, &format);
1584 if (!cmp_v4l2_format(fmt, &format)) {
1585 /* Sensor format is different from the requested format
1586 * so we need to change it
1588 ret = __vpfe_set_format(vpfe, fmt, &bpp);
1591 } else /* Just make sure all of the fields are consistent */
1594 /* First detach any IRQ if currently attached */
1595 vpfe_detach_irq(vpfe);
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;
1603 /* set image capture parameters in the ccdc */
1604 return vpfe_config_ccdc_image_format(vpfe);
1607 static int vpfe_enum_size(struct file *file, void *priv,
1608 struct v4l2_frmsizeenum *fsize)
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;
1618 vpfe_dbg(2, vpfe, "vpfe_enum_size\n");
1620 /* check for valid format */
1621 fmt = find_format_by_pix(fsize->pixel_format);
1623 vpfe_dbg(3, vpfe, "Invalid pixel code: %x, default used instead\n",
1624 fsize->pixel_format);
1628 memset(fsize->reserved, 0x0, sizeof(fsize->reserved));
1630 sdinfo = vpfe->current_subdev;
1634 memset(&pix, 0x0, sizeof(pix));
1635 /* Construct pix from parameter and use default for the rest */
1636 pix.pixelformat = fsize->pixel_format;
1639 pix.colorspace = V4L2_COLORSPACE_SRGB;
1640 pix.field = V4L2_FIELD_NONE;
1641 pix_to_mbus(vpfe, &pix, &mbus);
1643 memset(&fse, 0x0, sizeof(fse));
1644 fse.index = fsize->index;
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);
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);
1656 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1657 fsize->discrete.width = fse.max_width;
1658 fsize->discrete.height = fse.max_height;
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);
1668 * vpfe_get_subdev_input_index - Get subdev index and subdev input index for a
1669 * given app input index
1672 vpfe_get_subdev_input_index(struct vpfe_device *vpfe,
1674 int *subdev_input_index,
1675 int app_input_index)
1679 for (i = 0; i < ARRAY_SIZE(vpfe->cfg->asd); i++) {
1680 if (app_input_index < (j + 1)) {
1682 *subdev_input_index = app_input_index - j;
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
1695 static int vpfe_get_app_input_index(struct vpfe_device *vpfe,
1696 int *app_input_index)
1698 struct vpfe_config *cfg = vpfe->cfg;
1699 struct vpfe_subdev_info *sdinfo;
1700 struct i2c_client *client;
1701 struct i2c_client *curr_client;
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 == client->adapter->nr) {
1710 if (vpfe->current_input >= 1)
1712 *app_input_index = j + vpfe->current_input;
1720 static int vpfe_enum_input(struct file *file, void *priv,
1721 struct v4l2_input *inp)
1723 struct vpfe_device *vpfe = video_drvdata(file);
1724 struct vpfe_subdev_info *sdinfo;
1727 vpfe_dbg(2, vpfe, "vpfe_enum_input\n");
1729 if (vpfe_get_subdev_input_index(vpfe, &subdev, &index,
1732 "input information not found for the subdev\n");
1735 sdinfo = &vpfe->cfg->sub_devs[subdev];
1736 *inp = sdinfo->inputs[index];
1741 static int vpfe_g_input(struct file *file, void *priv, unsigned int *index)
1743 struct vpfe_device *vpfe = video_drvdata(file);
1745 vpfe_dbg(2, vpfe, "vpfe_g_input\n");
1747 return vpfe_get_app_input_index(vpfe, index);
1750 /* Assumes caller is holding vpfe_dev->lock */
1751 static int vpfe_set_input(struct vpfe_device *vpfe, unsigned int index)
1753 int subdev_index = 0, inp_index = 0;
1754 struct vpfe_subdev_info *sdinfo;
1755 struct vpfe_route *route;
1759 vpfe_dbg(2, vpfe, "vpfe_set_input: index: %d\n", index);
1761 /* If streaming is started, return error */
1762 if (vb2_is_busy(&vpfe->buffer_queue)) {
1763 vpfe_err(vpfe, "%s device busy\n", __func__);
1766 ret = vpfe_get_subdev_input_index(vpfe,
1771 vpfe_err(vpfe, "invalid input index: %d\n", index);
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;
1782 ret = v4l2_subdev_call(sdinfo->sd, video,
1783 s_routing, input, output, 0);
1785 vpfe_err(vpfe, "s_routing failed\n");
1793 vpfe->current_subdev = sdinfo;
1795 vpfe->v4l2_dev.ctrl_handler = sdinfo->sd->ctrl_handler;
1796 vpfe->current_input = index;
1797 vpfe->std_index = 0;
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);
1804 /* set the default image parameters in the device */
1805 return vpfe_config_image_format(vpfe,
1806 vpfe_standards[vpfe->std_index].std_id);
1812 static int vpfe_s_input(struct file *file, void *priv, unsigned int index)
1814 struct vpfe_device *vpfe = video_drvdata(file);
1817 "vpfe_s_input: index: %d\n", index);
1819 return vpfe_set_input(vpfe, index);
1822 static int vpfe_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
1824 struct vpfe_device *vpfe = video_drvdata(file);
1825 struct vpfe_subdev_info *sdinfo;
1827 vpfe_dbg(2, vpfe, "vpfe_querystd\n");
1829 sdinfo = vpfe->current_subdev;
1830 if (!(sdinfo->inputs[0].capabilities & V4L2_IN_CAP_STD))
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);
1838 static int vpfe_s_std(struct file *file, void *priv, v4l2_std_id std_id)
1840 struct vpfe_device *vpfe = video_drvdata(file);
1841 struct vpfe_subdev_info *sdinfo;
1844 vpfe_dbg(2, vpfe, "vpfe_s_std\n");
1846 sdinfo = vpfe->current_subdev;
1847 if (!(sdinfo->inputs[0].capabilities & V4L2_IN_CAP_STD))
1850 /* If streaming is started, return error */
1851 if (vb2_is_busy(&vpfe->buffer_queue)) {
1852 vpfe_err(vpfe, "%s device busy\n", __func__);
1857 ret = v4l2_device_call_until_err(&vpfe->v4l2_dev, sdinfo->grp_id,
1858 video, s_std, std_id);
1860 vpfe_err(vpfe, "Failed to set standard\n");
1863 ret = vpfe_config_image_format(vpfe, std_id);
1868 static int vpfe_g_std(struct file *file, void *priv, v4l2_std_id *std_id)
1870 struct vpfe_device *vpfe = video_drvdata(file);
1871 struct vpfe_subdev_info *sdinfo;
1873 vpfe_dbg(2, vpfe, "vpfe_g_std\n");
1875 sdinfo = vpfe->current_subdev;
1876 if (sdinfo->inputs[0].capabilities != V4L2_IN_CAP_STD)
1879 *std_id = vpfe_standards[vpfe->std_index].std_id;
1885 * vpfe_calculate_offsets : This function calculates buffers offset
1886 * for top and bottom field
1888 static void vpfe_calculate_offsets(struct vpfe_device *vpfe)
1890 struct v4l2_rect image_win;
1892 vpfe_dbg(2, vpfe, "vpfe_calculate_offsets\n");
1894 vpfe_ccdc_get_image_window(&vpfe->ccdc, &image_win);
1895 vpfe->field_off = image_win.height * image_win.width;
1899 * vpfe_queue_setup - Callback function for buffer setup.
1900 * @vq: vb2_queue ptr
1902 * @nbuffers: ptr to number of buffers requested by application
1903 * @nplanes:: contains number of distinct video planes needed to hold a frame
1904 * @sizes[]: contains the size (in bytes) of each plane.
1905 * @alloc_ctxs: ptr to allocation context
1907 * This callback function is called when reqbuf() is called to adjust
1908 * the buffer count and buffer size
1910 static int vpfe_queue_setup(struct vb2_queue *vq,
1912 unsigned int *nbuffers, unsigned int *nplanes,
1913 unsigned int sizes[], void *alloc_ctxs[])
1915 const struct v4l2_format *fmt = parg;
1916 struct vpfe_device *vpfe = vb2_get_drv_priv(vq);
1918 if (fmt && fmt->fmt.pix.sizeimage < vpfe->fmt.fmt.pix.sizeimage)
1921 if (vq->num_buffers + *nbuffers < 3)
1922 *nbuffers = 3 - vq->num_buffers;
1925 sizes[0] = fmt ? fmt->fmt.pix.sizeimage : vpfe->fmt.fmt.pix.sizeimage;
1926 alloc_ctxs[0] = vpfe->alloc_ctx;
1929 "nbuffers=%d, size=%u\n", *nbuffers, sizes[0]);
1931 /* Calculate field offset */
1932 vpfe_calculate_offsets(vpfe);
1938 * vpfe_buffer_prepare : callback function for buffer prepare
1939 * @vb: ptr to vb2_buffer
1941 * This is the callback function for buffer prepare when vb2_qbuf()
1942 * function is called. The buffer is prepared and user space virtual address
1943 * or user address is converted into physical address
1945 static int vpfe_buffer_prepare(struct vb2_buffer *vb)
1947 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1948 struct vpfe_device *vpfe = vb2_get_drv_priv(vb->vb2_queue);
1950 vb2_set_plane_payload(vb, 0, vpfe->fmt.fmt.pix.sizeimage);
1952 if (vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
1955 vbuf->field = vpfe->fmt.fmt.pix.field;
1961 * vpfe_buffer_queue : Callback function to add buffer to DMA queue
1962 * @vb: ptr to vb2_buffer
1964 static void vpfe_buffer_queue(struct vb2_buffer *vb)
1966 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1967 struct vpfe_device *vpfe = vb2_get_drv_priv(vb->vb2_queue);
1968 struct vpfe_cap_buffer *buf = to_vpfe_buffer(vbuf);
1969 unsigned long flags = 0;
1971 /* add the buffer to the DMA queue */
1972 spin_lock_irqsave(&vpfe->dma_queue_lock, flags);
1973 list_add_tail(&buf->list, &vpfe->dma_queue);
1974 spin_unlock_irqrestore(&vpfe->dma_queue_lock, flags);
1978 * vpfe_start_streaming : Starts the DMA engine for streaming
1979 * @vb: ptr to vb2_buffer
1980 * @count: number of buffers
1982 static int vpfe_start_streaming(struct vb2_queue *vq, unsigned int count)
1984 struct vpfe_device *vpfe = vb2_get_drv_priv(vq);
1985 struct vpfe_cap_buffer *buf, *tmp;
1986 struct vpfe_subdev_info *sdinfo;
1987 unsigned long flags;
1991 spin_lock_irqsave(&vpfe->dma_queue_lock, flags);
1996 sdinfo = vpfe->current_subdev;
1998 vpfe_attach_irq(vpfe);
2000 if (vpfe->ccdc.ccdc_cfg.if_type == VPFE_RAW_BAYER)
2001 vpfe_ccdc_config_raw(&vpfe->ccdc);
2003 vpfe_ccdc_config_ycbcr(&vpfe->ccdc);
2005 /* Get the next frame from the buffer queue */
2006 vpfe->next_frm = list_entry(vpfe->dma_queue.next,
2007 struct vpfe_cap_buffer, list);
2008 vpfe->cur_frm = vpfe->next_frm;
2009 /* Remove buffer from the buffer queue */
2010 list_del(&vpfe->cur_frm->list);
2011 spin_unlock_irqrestore(&vpfe->dma_queue_lock, flags);
2013 addr = vb2_dma_contig_plane_dma_addr(&vpfe->cur_frm->vb.vb2_buf, 0);
2015 vpfe_set_sdr_addr(&vpfe->ccdc, (unsigned long)(addr));
2017 vpfe_pcr_enable(&vpfe->ccdc, 1);
2019 ret = v4l2_subdev_call(sdinfo->sd, video, s_stream, 1);
2021 vpfe_err(vpfe, "Error in attaching interrupt handle\n");
2028 list_for_each_entry_safe(buf, tmp, &vpfe->dma_queue, list) {
2029 list_del(&buf->list);
2030 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
2037 * vpfe_stop_streaming : Stop the DMA engine
2038 * @vq: ptr to vb2_queue
2040 * This callback stops the DMA engine and any remaining buffers
2041 * in the DMA queue are released.
2043 static void vpfe_stop_streaming(struct vb2_queue *vq)
2045 struct vpfe_device *vpfe = vb2_get_drv_priv(vq);
2046 struct vpfe_subdev_info *sdinfo;
2047 unsigned long flags;
2050 vpfe_pcr_enable(&vpfe->ccdc, 0);
2052 vpfe_detach_irq(vpfe);
2054 sdinfo = vpfe->current_subdev;
2055 ret = v4l2_subdev_call(sdinfo->sd, video, s_stream, 0);
2056 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
2057 vpfe_dbg(1, vpfe, "stream off failed in subdev\n");
2059 /* release all active buffers */
2060 spin_lock_irqsave(&vpfe->dma_queue_lock, flags);
2061 if (vpfe->cur_frm == vpfe->next_frm) {
2062 vb2_buffer_done(&vpfe->cur_frm->vb.vb2_buf,
2063 VB2_BUF_STATE_ERROR);
2065 if (vpfe->cur_frm != NULL)
2066 vb2_buffer_done(&vpfe->cur_frm->vb.vb2_buf,
2067 VB2_BUF_STATE_ERROR);
2068 if (vpfe->next_frm != NULL)
2069 vb2_buffer_done(&vpfe->next_frm->vb.vb2_buf,
2070 VB2_BUF_STATE_ERROR);
2073 while (!list_empty(&vpfe->dma_queue)) {
2074 vpfe->next_frm = list_entry(vpfe->dma_queue.next,
2075 struct vpfe_cap_buffer, list);
2076 list_del(&vpfe->next_frm->list);
2077 vb2_buffer_done(&vpfe->next_frm->vb.vb2_buf,
2078 VB2_BUF_STATE_ERROR);
2080 spin_unlock_irqrestore(&vpfe->dma_queue_lock, flags);
2083 static int vpfe_cropcap(struct file *file, void *priv,
2084 struct v4l2_cropcap *crop)
2086 struct vpfe_device *vpfe = video_drvdata(file);
2088 vpfe_dbg(2, vpfe, "vpfe_cropcap\n");
2090 if (vpfe->std_index >= ARRAY_SIZE(vpfe_standards))
2093 memset(crop, 0, sizeof(struct v4l2_cropcap));
2095 crop->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2096 crop->defrect.width = vpfe_standards[vpfe->std_index].width;
2097 crop->bounds.width = crop->defrect.width;
2098 crop->defrect.height = vpfe_standards[vpfe->std_index].height;
2099 crop->bounds.height = crop->defrect.height;
2100 crop->pixelaspect = vpfe_standards[vpfe->std_index].pixelaspect;
2106 vpfe_g_selection(struct file *file, void *fh, struct v4l2_selection *s)
2108 struct vpfe_device *vpfe = video_drvdata(file);
2110 switch (s->target) {
2111 case V4L2_SEL_TGT_CROP_BOUNDS:
2112 case V4L2_SEL_TGT_CROP_DEFAULT:
2113 s->r.left = s->r.top = 0;
2114 s->r.width = vpfe->crop.width;
2115 s->r.height = vpfe->crop.height;
2118 case V4L2_SEL_TGT_CROP:
2129 static int enclosed_rectangle(struct v4l2_rect *a, struct v4l2_rect *b)
2131 if (a->left < b->left || a->top < b->top)
2134 if (a->left + a->width > b->left + b->width)
2137 if (a->top + a->height > b->top + b->height)
2144 vpfe_s_selection(struct file *file, void *fh, struct v4l2_selection *s)
2146 struct vpfe_device *vpfe = video_drvdata(file);
2147 struct v4l2_rect cr = vpfe->crop;
2148 struct v4l2_rect r = s->r;
2150 /* If streaming is started, return error */
2151 if (vb2_is_busy(&vpfe->buffer_queue)) {
2152 vpfe_err(vpfe, "%s device busy\n", __func__);
2156 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2157 s->target != V4L2_SEL_TGT_CROP)
2160 v4l_bound_align_image(&r.width, 0, cr.width, 0,
2161 &r.height, 0, cr.height, 0, 0);
2163 r.left = clamp_t(unsigned int, r.left, 0, cr.width - r.width);
2164 r.top = clamp_t(unsigned int, r.top, 0, cr.height - r.height);
2166 if (s->flags & V4L2_SEL_FLAG_LE && !enclosed_rectangle(&r, &s->r))
2169 if (s->flags & V4L2_SEL_FLAG_GE && !enclosed_rectangle(&s->r, &r))
2172 s->r = vpfe->crop = r;
2174 vpfe_ccdc_set_image_window(&vpfe->ccdc, &r, vpfe->bpp);
2175 vpfe->fmt.fmt.pix.width = r.width;
2176 vpfe->fmt.fmt.pix.height = r.height;
2177 vpfe->fmt.fmt.pix.bytesperline = vpfe_ccdc_get_line_length(&vpfe->ccdc);
2178 vpfe->fmt.fmt.pix.sizeimage = vpfe->fmt.fmt.pix.bytesperline *
2179 vpfe->fmt.fmt.pix.height;
2181 vpfe_dbg(1, vpfe, "cropped (%d,%d)/%dx%d of %dx%d\n",
2182 r.left, r.top, r.width, r.height, cr.width, cr.height);
2187 static long vpfe_ioctl_default(struct file *file, void *priv,
2188 bool valid_prio, unsigned int cmd, void *param)
2190 struct vpfe_device *vpfe = video_drvdata(file);
2193 vpfe_dbg(2, vpfe, "vpfe_ioctl_default\n");
2196 vpfe_err(vpfe, "%s device busy\n", __func__);
2200 /* If streaming is started, return error */
2201 if (vb2_is_busy(&vpfe->buffer_queue)) {
2202 vpfe_err(vpfe, "%s device busy\n", __func__);
2207 case VIDIOC_AM437X_CCDC_CFG:
2208 ret = vpfe_ccdc_set_params(&vpfe->ccdc, (void __user *)param);
2211 "Error setting parameters in CCDC\n");
2214 ret = vpfe_get_ccdc_image_format(vpfe,
2218 "Invalid image format at CCDC\n");
2231 static const struct vb2_ops vpfe_video_qops = {
2232 .wait_prepare = vb2_ops_wait_prepare,
2233 .wait_finish = vb2_ops_wait_finish,
2234 .queue_setup = vpfe_queue_setup,
2235 .buf_prepare = vpfe_buffer_prepare,
2236 .buf_queue = vpfe_buffer_queue,
2237 .start_streaming = vpfe_start_streaming,
2238 .stop_streaming = vpfe_stop_streaming,
2241 /* vpfe capture driver file operations */
2242 static const struct v4l2_file_operations vpfe_fops = {
2243 .owner = THIS_MODULE,
2245 .release = vpfe_release,
2246 .read = vb2_fop_read,
2247 .poll = vb2_fop_poll,
2248 .unlocked_ioctl = video_ioctl2,
2249 .mmap = vb2_fop_mmap,
2252 /* vpfe capture ioctl operations */
2253 static const struct v4l2_ioctl_ops vpfe_ioctl_ops = {
2254 .vidioc_querycap = vpfe_querycap,
2255 .vidioc_enum_fmt_vid_cap = vpfe_enum_fmt,
2256 .vidioc_g_fmt_vid_cap = vpfe_g_fmt,
2257 .vidioc_s_fmt_vid_cap = vpfe_s_fmt,
2258 .vidioc_try_fmt_vid_cap = vpfe_try_fmt,
2260 .vidioc_enum_framesizes = vpfe_enum_size,
2262 .vidioc_enum_input = vpfe_enum_input,
2263 .vidioc_g_input = vpfe_g_input,
2264 .vidioc_s_input = vpfe_s_input,
2266 .vidioc_querystd = vpfe_querystd,
2267 .vidioc_s_std = vpfe_s_std,
2268 .vidioc_g_std = vpfe_g_std,
2270 .vidioc_reqbufs = vb2_ioctl_reqbufs,
2271 .vidioc_create_bufs = vb2_ioctl_create_bufs,
2272 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
2273 .vidioc_querybuf = vb2_ioctl_querybuf,
2274 .vidioc_qbuf = vb2_ioctl_qbuf,
2275 .vidioc_dqbuf = vb2_ioctl_dqbuf,
2276 .vidioc_expbuf = vb2_ioctl_expbuf,
2277 .vidioc_streamon = vb2_ioctl_streamon,
2278 .vidioc_streamoff = vb2_ioctl_streamoff,
2280 .vidioc_log_status = v4l2_ctrl_log_status,
2281 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
2282 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
2284 .vidioc_cropcap = vpfe_cropcap,
2285 .vidioc_g_selection = vpfe_g_selection,
2286 .vidioc_s_selection = vpfe_s_selection,
2288 .vidioc_default = vpfe_ioctl_default,
2292 vpfe_async_bound(struct v4l2_async_notifier *notifier,
2293 struct v4l2_subdev *subdev,
2294 struct v4l2_async_subdev *asd)
2296 struct vpfe_device *vpfe = container_of(notifier->v4l2_dev,
2297 struct vpfe_device, v4l2_dev);
2298 struct v4l2_subdev_mbus_code_enum mbus_code;
2299 struct vpfe_subdev_info *sdinfo;
2303 vpfe_dbg(1, vpfe, "vpfe_async_bound\n");
2305 for (i = 0; i < ARRAY_SIZE(vpfe->cfg->asd); i++) {
2306 if (vpfe->cfg->asd[i]->match.of.node == asd[i].match.of.node) {
2307 sdinfo = &vpfe->cfg->sub_devs[i];
2308 vpfe->sd[i] = subdev;
2309 vpfe->sd[i]->grp_id = sdinfo->grp_id;
2316 vpfe_info(vpfe, "sub device (%s) not matched\n", subdev->name);
2320 vpfe->video_dev.tvnorms |= sdinfo->inputs[0].std;
2322 /* setup the supported formats & indexes */
2323 for (j = 0, i = 0; ; ++j) {
2324 struct vpfe_fmt *fmt;
2327 memset(&mbus_code, 0, sizeof(mbus_code));
2328 mbus_code.index = j;
2329 mbus_code.which = V4L2_SUBDEV_FORMAT_ACTIVE;
2330 ret = v4l2_subdev_call(subdev, pad, enum_mbus_code,
2335 fmt = find_format_by_code(mbus_code.code);
2339 fmt->supported = true;
2346 static int vpfe_probe_complete(struct vpfe_device *vpfe)
2348 struct video_device *vdev;
2349 struct vb2_queue *q;
2352 spin_lock_init(&vpfe->dma_queue_lock);
2353 mutex_init(&vpfe->lock);
2355 vpfe->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2357 /* set first sub device as current one */
2358 vpfe->current_subdev = &vpfe->cfg->sub_devs[0];
2359 vpfe->v4l2_dev.ctrl_handler = vpfe->sd[0]->ctrl_handler;
2361 err = vpfe_set_input(vpfe, 0);
2365 /* Initialize videobuf2 queue as per the buffer type */
2366 vpfe->alloc_ctx = vb2_dma_contig_init_ctx(vpfe->pdev);
2367 if (IS_ERR(vpfe->alloc_ctx)) {
2368 vpfe_err(vpfe, "Failed to get the context\n");
2369 err = PTR_ERR(vpfe->alloc_ctx);
2373 q = &vpfe->buffer_queue;
2374 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2375 q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
2377 q->ops = &vpfe_video_qops;
2378 q->mem_ops = &vb2_dma_contig_memops;
2379 q->buf_struct_size = sizeof(struct vpfe_cap_buffer);
2380 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
2381 q->lock = &vpfe->lock;
2382 q->min_buffers_needed = 1;
2384 err = vb2_queue_init(q);
2386 vpfe_err(vpfe, "vb2_queue_init() failed\n");
2387 vb2_dma_contig_cleanup_ctx(vpfe->alloc_ctx);
2391 INIT_LIST_HEAD(&vpfe->dma_queue);
2393 vdev = &vpfe->video_dev;
2394 strlcpy(vdev->name, VPFE_MODULE_NAME, sizeof(vdev->name));
2395 vdev->release = video_device_release_empty;
2396 vdev->fops = &vpfe_fops;
2397 vdev->ioctl_ops = &vpfe_ioctl_ops;
2398 vdev->v4l2_dev = &vpfe->v4l2_dev;
2399 vdev->vfl_dir = VFL_DIR_RX;
2401 vdev->lock = &vpfe->lock;
2402 video_set_drvdata(vdev, vpfe);
2403 err = video_register_device(&vpfe->video_dev, VFL_TYPE_GRABBER, -1);
2406 "Unable to register video device.\n");
2413 v4l2_device_unregister(&vpfe->v4l2_dev);
2417 static int vpfe_async_complete(struct v4l2_async_notifier *notifier)
2419 struct vpfe_device *vpfe = container_of(notifier->v4l2_dev,
2420 struct vpfe_device, v4l2_dev);
2422 return vpfe_probe_complete(vpfe);
2425 static struct vpfe_config *
2426 vpfe_get_pdata(struct platform_device *pdev)
2428 struct device_node *endpoint = NULL;
2429 struct v4l2_of_endpoint bus_cfg;
2430 struct vpfe_subdev_info *sdinfo;
2431 struct vpfe_config *pdata;
2436 dev_dbg(&pdev->dev, "vpfe_get_pdata\n");
2438 if (!IS_ENABLED(CONFIG_OF) || !pdev->dev.of_node)
2439 return pdev->dev.platform_data;
2441 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
2445 for (i = 0; ; i++) {
2446 struct device_node *rem;
2448 endpoint = of_graph_get_next_endpoint(pdev->dev.of_node,
2453 sdinfo = &pdata->sub_devs[i];
2456 /* we only support camera */
2457 sdinfo->inputs[0].index = i;
2458 strcpy(sdinfo->inputs[0].name, "Camera");
2459 sdinfo->inputs[0].type = V4L2_INPUT_TYPE_CAMERA;
2460 sdinfo->inputs[0].std = V4L2_STD_ALL;
2461 sdinfo->inputs[0].capabilities = V4L2_IN_CAP_STD;
2463 sdinfo->can_route = 0;
2464 sdinfo->routes = NULL;
2466 of_property_read_u32(endpoint, "ti,am437x-vpfe-interface",
2467 &sdinfo->vpfe_param.if_type);
2468 if (sdinfo->vpfe_param.if_type < 0 ||
2469 sdinfo->vpfe_param.if_type > 4) {
2470 sdinfo->vpfe_param.if_type = VPFE_RAW_BAYER;
2473 err = v4l2_of_parse_endpoint(endpoint, &bus_cfg);
2475 dev_err(&pdev->dev, "Could not parse the endpoint\n");
2479 sdinfo->vpfe_param.bus_width = bus_cfg.bus.parallel.bus_width;
2481 if (sdinfo->vpfe_param.bus_width < 8 ||
2482 sdinfo->vpfe_param.bus_width > 16) {
2483 dev_err(&pdev->dev, "Invalid bus width.\n");
2487 flags = bus_cfg.bus.parallel.flags;
2489 if (flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
2490 sdinfo->vpfe_param.hdpol = 1;
2492 if (flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH)
2493 sdinfo->vpfe_param.vdpol = 1;
2495 rem = of_graph_get_remote_port_parent(endpoint);
2497 dev_err(&pdev->dev, "Remote device at %s not found\n",
2498 endpoint->full_name);
2502 pdata->asd[i] = devm_kzalloc(&pdev->dev,
2503 sizeof(struct v4l2_async_subdev),
2505 if (!pdata->asd[i]) {
2511 pdata->asd[i]->match_type = V4L2_ASYNC_MATCH_OF;
2512 pdata->asd[i]->match.of.node = rem;
2516 of_node_put(endpoint);
2520 of_node_put(endpoint);
2525 * vpfe_probe : This function creates device entries by register
2526 * itself to the V4L2 driver and initializes fields of each
2529 static int vpfe_probe(struct platform_device *pdev)
2531 struct vpfe_config *vpfe_cfg = vpfe_get_pdata(pdev);
2532 struct vpfe_device *vpfe;
2533 struct vpfe_ccdc *ccdc;
2534 struct resource *res;
2538 dev_err(&pdev->dev, "No platform data\n");
2542 vpfe = devm_kzalloc(&pdev->dev, sizeof(*vpfe), GFP_KERNEL);
2546 vpfe->pdev = &pdev->dev;
2547 vpfe->cfg = vpfe_cfg;
2550 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2551 ccdc->ccdc_cfg.base_addr = devm_ioremap_resource(&pdev->dev, res);
2552 if (IS_ERR(ccdc->ccdc_cfg.base_addr))
2553 return PTR_ERR(ccdc->ccdc_cfg.base_addr);
2555 ret = platform_get_irq(pdev, 0);
2557 dev_err(&pdev->dev, "No IRQ resource\n");
2562 ret = devm_request_irq(vpfe->pdev, vpfe->irq, vpfe_isr, 0,
2563 "vpfe_capture0", vpfe);
2565 dev_err(&pdev->dev, "Unable to request interrupt\n");
2569 ret = v4l2_device_register(&pdev->dev, &vpfe->v4l2_dev);
2572 "Unable to register v4l2 device.\n");
2576 /* set the driver data in platform device */
2577 platform_set_drvdata(pdev, vpfe);
2578 /* Enabling module functional clock */
2579 pm_runtime_enable(&pdev->dev);
2581 /* for now just enable it here instead of waiting for the open */
2582 pm_runtime_get_sync(&pdev->dev);
2584 vpfe_ccdc_config_defaults(ccdc);
2586 pm_runtime_put_sync(&pdev->dev);
2588 vpfe->sd = devm_kzalloc(&pdev->dev, sizeof(struct v4l2_subdev *) *
2589 ARRAY_SIZE(vpfe->cfg->asd), GFP_KERNEL);
2592 goto probe_out_v4l2_unregister;
2595 vpfe->notifier.subdevs = vpfe->cfg->asd;
2596 vpfe->notifier.num_subdevs = ARRAY_SIZE(vpfe->cfg->asd);
2597 vpfe->notifier.bound = vpfe_async_bound;
2598 vpfe->notifier.complete = vpfe_async_complete;
2599 ret = v4l2_async_notifier_register(&vpfe->v4l2_dev,
2602 vpfe_err(vpfe, "Error registering async notifier\n");
2604 goto probe_out_v4l2_unregister;
2609 probe_out_v4l2_unregister:
2610 v4l2_device_unregister(&vpfe->v4l2_dev);
2615 * vpfe_remove : It un-register device from V4L2 driver
2617 static int vpfe_remove(struct platform_device *pdev)
2619 struct vpfe_device *vpfe = platform_get_drvdata(pdev);
2621 vpfe_dbg(2, vpfe, "vpfe_remove\n");
2623 pm_runtime_disable(&pdev->dev);
2625 v4l2_async_notifier_unregister(&vpfe->notifier);
2626 v4l2_device_unregister(&vpfe->v4l2_dev);
2627 video_unregister_device(&vpfe->video_dev);
2632 #ifdef CONFIG_PM_SLEEP
2634 static void vpfe_save_context(struct vpfe_ccdc *ccdc)
2636 ccdc->ccdc_ctx[VPFE_PCR >> 2] = vpfe_reg_read(ccdc, VPFE_PCR);
2637 ccdc->ccdc_ctx[VPFE_SYNMODE >> 2] = vpfe_reg_read(ccdc, VPFE_SYNMODE);
2638 ccdc->ccdc_ctx[VPFE_SDOFST >> 2] = vpfe_reg_read(ccdc, VPFE_SDOFST);
2639 ccdc->ccdc_ctx[VPFE_SDR_ADDR >> 2] = vpfe_reg_read(ccdc, VPFE_SDR_ADDR);
2640 ccdc->ccdc_ctx[VPFE_CLAMP >> 2] = vpfe_reg_read(ccdc, VPFE_CLAMP);
2641 ccdc->ccdc_ctx[VPFE_DCSUB >> 2] = vpfe_reg_read(ccdc, VPFE_DCSUB);
2642 ccdc->ccdc_ctx[VPFE_COLPTN >> 2] = vpfe_reg_read(ccdc, VPFE_COLPTN);
2643 ccdc->ccdc_ctx[VPFE_BLKCMP >> 2] = vpfe_reg_read(ccdc, VPFE_BLKCMP);
2644 ccdc->ccdc_ctx[VPFE_VDINT >> 2] = vpfe_reg_read(ccdc, VPFE_VDINT);
2645 ccdc->ccdc_ctx[VPFE_ALAW >> 2] = vpfe_reg_read(ccdc, VPFE_ALAW);
2646 ccdc->ccdc_ctx[VPFE_REC656IF >> 2] = vpfe_reg_read(ccdc, VPFE_REC656IF);
2647 ccdc->ccdc_ctx[VPFE_CCDCFG >> 2] = vpfe_reg_read(ccdc, VPFE_CCDCFG);
2648 ccdc->ccdc_ctx[VPFE_CULLING >> 2] = vpfe_reg_read(ccdc, VPFE_CULLING);
2649 ccdc->ccdc_ctx[VPFE_HD_VD_WID >> 2] = vpfe_reg_read(ccdc,
2651 ccdc->ccdc_ctx[VPFE_PIX_LINES >> 2] = vpfe_reg_read(ccdc,
2653 ccdc->ccdc_ctx[VPFE_HORZ_INFO >> 2] = vpfe_reg_read(ccdc,
2655 ccdc->ccdc_ctx[VPFE_VERT_START >> 2] = vpfe_reg_read(ccdc,
2657 ccdc->ccdc_ctx[VPFE_VERT_LINES >> 2] = vpfe_reg_read(ccdc,
2659 ccdc->ccdc_ctx[VPFE_HSIZE_OFF >> 2] = vpfe_reg_read(ccdc,
2663 static int vpfe_suspend(struct device *dev)
2665 struct platform_device *pdev = to_platform_device(dev);
2666 struct vpfe_device *vpfe = platform_get_drvdata(pdev);
2667 struct vpfe_ccdc *ccdc = &vpfe->ccdc;
2669 /* if streaming has not started we don't care */
2670 if (!vb2_start_streaming_called(&vpfe->buffer_queue))
2673 pm_runtime_get_sync(dev);
2674 vpfe_config_enable(ccdc, 1);
2676 /* Save VPFE context */
2677 vpfe_save_context(ccdc);
2680 vpfe_pcr_enable(ccdc, 0);
2681 vpfe_config_enable(ccdc, 0);
2683 /* Disable both master and slave clock */
2684 pm_runtime_put_sync(dev);
2686 /* Select sleep pin state */
2687 pinctrl_pm_select_sleep_state(dev);
2692 static void vpfe_restore_context(struct vpfe_ccdc *ccdc)
2694 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_SYNMODE >> 2], VPFE_SYNMODE);
2695 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_CULLING >> 2], VPFE_CULLING);
2696 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_SDOFST >> 2], VPFE_SDOFST);
2697 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_SDR_ADDR >> 2], VPFE_SDR_ADDR);
2698 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_CLAMP >> 2], VPFE_CLAMP);
2699 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_DCSUB >> 2], VPFE_DCSUB);
2700 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_COLPTN >> 2], VPFE_COLPTN);
2701 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_BLKCMP >> 2], VPFE_BLKCMP);
2702 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_VDINT >> 2], VPFE_VDINT);
2703 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_ALAW >> 2], VPFE_ALAW);
2704 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_REC656IF >> 2], VPFE_REC656IF);
2705 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_CCDCFG >> 2], VPFE_CCDCFG);
2706 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_PCR >> 2], VPFE_PCR);
2707 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_HD_VD_WID >> 2],
2709 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_PIX_LINES >> 2],
2711 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_HORZ_INFO >> 2],
2713 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_VERT_START >> 2],
2715 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_VERT_LINES >> 2],
2717 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_HSIZE_OFF >> 2],
2721 static int vpfe_resume(struct device *dev)
2723 struct platform_device *pdev = to_platform_device(dev);
2724 struct vpfe_device *vpfe = platform_get_drvdata(pdev);
2725 struct vpfe_ccdc *ccdc = &vpfe->ccdc;
2727 /* if streaming has not started we don't care */
2728 if (!vb2_start_streaming_called(&vpfe->buffer_queue))
2731 /* Enable both master and slave clock */
2732 pm_runtime_get_sync(dev);
2733 vpfe_config_enable(ccdc, 1);
2735 /* Restore VPFE context */
2736 vpfe_restore_context(ccdc);
2738 vpfe_config_enable(ccdc, 0);
2739 pm_runtime_put_sync(dev);
2741 /* Select default pin state */
2742 pinctrl_pm_select_default_state(dev);
2749 static SIMPLE_DEV_PM_OPS(vpfe_pm_ops, vpfe_suspend, vpfe_resume);
2751 static const struct of_device_id vpfe_of_match[] = {
2752 { .compatible = "ti,am437x-vpfe", },
2755 MODULE_DEVICE_TABLE(of, vpfe_of_match);
2757 static struct platform_driver vpfe_driver = {
2758 .probe = vpfe_probe,
2759 .remove = vpfe_remove,
2761 .name = VPFE_MODULE_NAME,
2763 .of_match_table = of_match_ptr(vpfe_of_match),
2767 module_platform_driver(vpfe_driver);
2769 MODULE_AUTHOR("Texas Instruments");
2770 MODULE_DESCRIPTION("TI AM437x VPFE driver");
2771 MODULE_LICENSE("GPL");
2772 MODULE_VERSION(VPFE_VERSION);