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/of_graph.h>
30 #include <linux/pinctrl/consumer.h>
31 #include <linux/platform_device.h>
32 #include <linux/pm_runtime.h>
33 #include <linux/slab.h>
34 #include <linux/uaccess.h>
35 #include <linux/videodev2.h>
37 #include <media/v4l2-common.h>
38 #include <media/v4l2-ctrls.h>
39 #include <media/v4l2-event.h>
40 #include <media/v4l2-fwnode.h>
42 #include "am437x-vpfe.h"
44 #define VPFE_MODULE_NAME "vpfe"
45 #define VPFE_VERSION "0.1.0"
48 module_param(debug, int, 0644);
49 MODULE_PARM_DESC(debug, "Debug level 0-8");
51 #define vpfe_dbg(level, dev, fmt, arg...) \
52 v4l2_dbg(level, debug, &dev->v4l2_dev, fmt, ##arg)
53 #define vpfe_info(dev, fmt, arg...) \
54 v4l2_info(&dev->v4l2_dev, fmt, ##arg)
55 #define vpfe_err(dev, fmt, arg...) \
56 v4l2_err(&dev->v4l2_dev, fmt, ##arg)
58 /* standard information */
59 struct vpfe_standard {
63 struct v4l2_fract pixelaspect;
67 static const struct vpfe_standard vpfe_standards[] = {
68 {V4L2_STD_525_60, 720, 480, {11, 10}, 1},
69 {V4L2_STD_625_50, 720, 576, {54, 59}, 1},
78 * struct vpfe_fmt - VPFE media bus format information
79 * @name: V4L2 format description
80 * @code: V4L2 media bus format code
81 * @shifted: V4L2 media bus format code for the same pixel layout but
82 * shifted to be 8 bits per pixel. =0 if format is not shiftable.
83 * @pixelformat: V4L2 pixel format FCC identifier
84 * @width: Bits per pixel (when transferred over a bus)
85 * @bpp: Bytes per pixel (when stored in memory)
86 * @supported: Indicates format supported by subdev
98 static struct vpfe_fmt formats[] = {
100 .name = "YUV 4:2:2 packed, YCbYCr",
101 .fourcc = V4L2_PIX_FMT_YUYV,
102 .code = MEDIA_BUS_FMT_YUYV8_2X8,
109 .name = "YUV 4:2:2 packed, CbYCrY",
110 .fourcc = V4L2_PIX_FMT_UYVY,
111 .code = MEDIA_BUS_FMT_UYVY8_2X8,
118 .name = "YUV 4:2:2 packed, YCrYCb",
119 .fourcc = V4L2_PIX_FMT_YVYU,
120 .code = MEDIA_BUS_FMT_YVYU8_2X8,
127 .name = "YUV 4:2:2 packed, CrYCbY",
128 .fourcc = V4L2_PIX_FMT_VYUY,
129 .code = MEDIA_BUS_FMT_VYUY8_2X8,
137 .fourcc = V4L2_PIX_FMT_SBGGR8,
138 .code = MEDIA_BUS_FMT_SBGGR8_1X8,
146 .fourcc = V4L2_PIX_FMT_SGBRG8,
147 .code = MEDIA_BUS_FMT_SGBRG8_1X8,
155 .fourcc = V4L2_PIX_FMT_SGRBG8,
156 .code = MEDIA_BUS_FMT_SGRBG8_1X8,
164 .fourcc = V4L2_PIX_FMT_SRGGB8,
165 .code = MEDIA_BUS_FMT_SRGGB8_1X8,
172 .name = "RGB565 (LE)",
173 .fourcc = V4L2_PIX_FMT_RGB565,
174 .code = MEDIA_BUS_FMT_RGB565_2X8_LE,
181 .name = "RGB565 (BE)",
182 .fourcc = V4L2_PIX_FMT_RGB565X,
183 .code = MEDIA_BUS_FMT_RGB565_2X8_BE,
193 __vpfe_get_format(struct vpfe_device *vpfe,
194 struct v4l2_format *format, unsigned int *bpp);
196 static struct vpfe_fmt *find_format_by_code(unsigned int code)
198 struct vpfe_fmt *fmt;
201 for (k = 0; k < ARRAY_SIZE(formats); k++) {
203 if (fmt->code == code)
210 static struct vpfe_fmt *find_format_by_pix(unsigned int pixelformat)
212 struct vpfe_fmt *fmt;
215 for (k = 0; k < ARRAY_SIZE(formats); k++) {
217 if (fmt->fourcc == pixelformat)
225 mbus_to_pix(struct vpfe_device *vpfe,
226 const struct v4l2_mbus_framefmt *mbus,
227 struct v4l2_pix_format *pix, unsigned int *bpp)
229 struct vpfe_subdev_info *sdinfo = vpfe->current_subdev;
230 unsigned int bus_width = sdinfo->vpfe_param.bus_width;
231 struct vpfe_fmt *fmt;
233 fmt = find_format_by_code(mbus->code);
234 if (WARN_ON(fmt == NULL)) {
235 pr_err("Invalid mbus code set\n");
240 memset(pix, 0, sizeof(*pix));
241 v4l2_fill_pix_format(pix, mbus);
242 pix->pixelformat = fmt->fourcc;
243 *bpp = (bus_width == 10) ? fmt->l.bpp : fmt->s.bpp;
245 /* pitch should be 32 bytes aligned */
246 pix->bytesperline = ALIGN(pix->width * *bpp, 32);
247 pix->sizeimage = pix->bytesperline * pix->height;
250 static void pix_to_mbus(struct vpfe_device *vpfe,
251 struct v4l2_pix_format *pix_fmt,
252 struct v4l2_mbus_framefmt *mbus_fmt)
254 struct vpfe_fmt *fmt;
256 fmt = find_format_by_pix(pix_fmt->pixelformat);
258 /* default to first entry */
259 vpfe_dbg(3, vpfe, "Invalid pixel code: %x, default used instead\n",
260 pix_fmt->pixelformat);
264 memset(mbus_fmt, 0, sizeof(*mbus_fmt));
265 v4l2_fill_mbus_format(mbus_fmt, pix_fmt, fmt->code);
268 /* Print Four-character-code (FOURCC) */
269 static char *print_fourcc(u32 fmt)
273 code[0] = (unsigned char)(fmt & 0xff);
274 code[1] = (unsigned char)((fmt >> 8) & 0xff);
275 code[2] = (unsigned char)((fmt >> 16) & 0xff);
276 code[3] = (unsigned char)((fmt >> 24) & 0xff);
283 cmp_v4l2_format(const struct v4l2_format *lhs, const struct v4l2_format *rhs)
285 return lhs->type == rhs->type &&
286 lhs->fmt.pix.width == rhs->fmt.pix.width &&
287 lhs->fmt.pix.height == rhs->fmt.pix.height &&
288 lhs->fmt.pix.pixelformat == rhs->fmt.pix.pixelformat &&
289 lhs->fmt.pix.field == rhs->fmt.pix.field &&
290 lhs->fmt.pix.colorspace == rhs->fmt.pix.colorspace &&
291 lhs->fmt.pix.ycbcr_enc == rhs->fmt.pix.ycbcr_enc &&
292 lhs->fmt.pix.quantization == rhs->fmt.pix.quantization &&
293 lhs->fmt.pix.xfer_func == rhs->fmt.pix.xfer_func;
296 static inline u32 vpfe_reg_read(struct vpfe_ccdc *ccdc, u32 offset)
298 return ioread32(ccdc->ccdc_cfg.base_addr + offset);
301 static inline void vpfe_reg_write(struct vpfe_ccdc *ccdc, u32 val, u32 offset)
303 iowrite32(val, ccdc->ccdc_cfg.base_addr + offset);
306 static inline struct vpfe_device *to_vpfe(struct vpfe_ccdc *ccdc)
308 return container_of(ccdc, struct vpfe_device, ccdc);
312 struct vpfe_cap_buffer *to_vpfe_buffer(struct vb2_v4l2_buffer *vb)
314 return container_of(vb, struct vpfe_cap_buffer, vb);
317 static inline void vpfe_pcr_enable(struct vpfe_ccdc *ccdc, int flag)
319 vpfe_reg_write(ccdc, !!flag, VPFE_PCR);
322 static void vpfe_config_enable(struct vpfe_ccdc *ccdc, int flag)
327 cfg = vpfe_reg_read(ccdc, VPFE_CONFIG);
328 cfg &= ~(VPFE_CONFIG_EN_ENABLE << VPFE_CONFIG_EN_SHIFT);
330 cfg = VPFE_CONFIG_EN_ENABLE << VPFE_CONFIG_EN_SHIFT;
333 vpfe_reg_write(ccdc, cfg, VPFE_CONFIG);
336 static void vpfe_ccdc_setwin(struct vpfe_ccdc *ccdc,
337 struct v4l2_rect *image_win,
338 enum ccdc_frmfmt frm_fmt,
341 int horz_start, horz_nr_pixels;
342 int vert_start, vert_nr_lines;
346 * ppc - per pixel count. indicates how many pixels per cell
347 * output to SDRAM. example, for ycbcr, it is one y and one c, so 2.
348 * raw capture this is 1
350 horz_start = image_win->left * bpp;
351 horz_nr_pixels = (image_win->width * bpp) - 1;
352 vpfe_reg_write(ccdc, (horz_start << VPFE_HORZ_INFO_SPH_SHIFT) |
353 horz_nr_pixels, VPFE_HORZ_INFO);
355 vert_start = image_win->top;
357 if (frm_fmt == CCDC_FRMFMT_INTERLACED) {
358 vert_nr_lines = (image_win->height >> 1) - 1;
360 /* Since first line doesn't have any data */
362 /* configure VDINT0 */
363 val = (vert_start << VPFE_VDINT_VDINT0_SHIFT);
365 /* Since first line doesn't have any data */
367 vert_nr_lines = image_win->height - 1;
369 * configure VDINT0 and VDINT1. VDINT1 will be at half
372 mid_img = vert_start + (image_win->height / 2);
373 val = (vert_start << VPFE_VDINT_VDINT0_SHIFT) |
374 (mid_img & VPFE_VDINT_VDINT1_MASK);
377 vpfe_reg_write(ccdc, val, VPFE_VDINT);
379 vpfe_reg_write(ccdc, (vert_start << VPFE_VERT_START_SLV0_SHIFT) |
380 vert_start, VPFE_VERT_START);
381 vpfe_reg_write(ccdc, vert_nr_lines, VPFE_VERT_LINES);
384 static void vpfe_reg_dump(struct vpfe_ccdc *ccdc)
386 struct vpfe_device *vpfe = to_vpfe(ccdc);
388 vpfe_dbg(3, vpfe, "ALAW: 0x%x\n", vpfe_reg_read(ccdc, VPFE_ALAW));
389 vpfe_dbg(3, vpfe, "CLAMP: 0x%x\n", vpfe_reg_read(ccdc, VPFE_CLAMP));
390 vpfe_dbg(3, vpfe, "DCSUB: 0x%x\n", vpfe_reg_read(ccdc, VPFE_DCSUB));
391 vpfe_dbg(3, vpfe, "BLKCMP: 0x%x\n", vpfe_reg_read(ccdc, VPFE_BLKCMP));
392 vpfe_dbg(3, vpfe, "COLPTN: 0x%x\n", vpfe_reg_read(ccdc, VPFE_COLPTN));
393 vpfe_dbg(3, vpfe, "SDOFST: 0x%x\n", vpfe_reg_read(ccdc, VPFE_SDOFST));
394 vpfe_dbg(3, vpfe, "SYN_MODE: 0x%x\n",
395 vpfe_reg_read(ccdc, VPFE_SYNMODE));
396 vpfe_dbg(3, vpfe, "HSIZE_OFF: 0x%x\n",
397 vpfe_reg_read(ccdc, VPFE_HSIZE_OFF));
398 vpfe_dbg(3, vpfe, "HORZ_INFO: 0x%x\n",
399 vpfe_reg_read(ccdc, VPFE_HORZ_INFO));
400 vpfe_dbg(3, vpfe, "VERT_START: 0x%x\n",
401 vpfe_reg_read(ccdc, VPFE_VERT_START));
402 vpfe_dbg(3, vpfe, "VERT_LINES: 0x%x\n",
403 vpfe_reg_read(ccdc, VPFE_VERT_LINES));
407 vpfe_ccdc_validate_param(struct vpfe_ccdc *ccdc,
408 struct vpfe_ccdc_config_params_raw *ccdcparam)
410 struct vpfe_device *vpfe = to_vpfe(ccdc);
411 u8 max_gamma, max_data;
413 if (!ccdcparam->alaw.enable)
416 max_gamma = ccdc_gamma_width_max_bit(ccdcparam->alaw.gamma_wd);
417 max_data = ccdc_data_size_max_bit(ccdcparam->data_sz);
419 if (ccdcparam->alaw.gamma_wd > VPFE_CCDC_GAMMA_BITS_09_0 ||
420 ccdcparam->alaw.gamma_wd < VPFE_CCDC_GAMMA_BITS_15_6 ||
421 max_gamma > max_data) {
422 vpfe_dbg(1, vpfe, "Invalid data line select\n");
430 vpfe_ccdc_update_raw_params(struct vpfe_ccdc *ccdc,
431 struct vpfe_ccdc_config_params_raw *raw_params)
433 struct vpfe_ccdc_config_params_raw *config_params =
434 &ccdc->ccdc_cfg.bayer.config_params;
436 *config_params = *raw_params;
440 * vpfe_ccdc_restore_defaults()
441 * This function will write defaults to all CCDC registers
443 static void vpfe_ccdc_restore_defaults(struct vpfe_ccdc *ccdc)
448 vpfe_pcr_enable(ccdc, 0);
450 /* set all registers to default value */
451 for (i = 4; i <= 0x94; i += 4)
452 vpfe_reg_write(ccdc, 0, i);
454 vpfe_reg_write(ccdc, VPFE_NO_CULLING, VPFE_CULLING);
455 vpfe_reg_write(ccdc, VPFE_CCDC_GAMMA_BITS_11_2, VPFE_ALAW);
458 static int vpfe_ccdc_close(struct vpfe_ccdc *ccdc, struct device *dev)
460 int dma_cntl, i, pcr;
462 /* If the CCDC module is still busy wait for it to be done */
463 for (i = 0; i < 10; i++) {
464 usleep_range(5000, 6000);
465 pcr = vpfe_reg_read(ccdc, VPFE_PCR);
469 /* make sure it it is disabled */
470 vpfe_pcr_enable(ccdc, 0);
473 /* Disable CCDC by resetting all register to default POR values */
474 vpfe_ccdc_restore_defaults(ccdc);
476 /* if DMA_CNTL overflow bit is set. Clear it
477 * It appears to take a while for this to become quiescent ~20ms
479 for (i = 0; i < 10; i++) {
480 dma_cntl = vpfe_reg_read(ccdc, VPFE_DMA_CNTL);
481 if (!(dma_cntl & VPFE_DMA_CNTL_OVERFLOW))
484 /* Clear the overflow bit */
485 vpfe_reg_write(ccdc, dma_cntl, VPFE_DMA_CNTL);
486 usleep_range(5000, 6000);
489 /* Disabled the module at the CONFIG level */
490 vpfe_config_enable(ccdc, 0);
492 pm_runtime_put_sync(dev);
497 static int vpfe_ccdc_set_params(struct vpfe_ccdc *ccdc, void __user *params)
499 struct vpfe_device *vpfe = container_of(ccdc, struct vpfe_device, ccdc);
500 struct vpfe_ccdc_config_params_raw raw_params;
503 if (ccdc->ccdc_cfg.if_type != VPFE_RAW_BAYER)
506 x = copy_from_user(&raw_params, params, sizeof(raw_params));
509 "vpfe_ccdc_set_params: error in copying ccdc params, %d\n",
514 if (!vpfe_ccdc_validate_param(ccdc, &raw_params)) {
515 vpfe_ccdc_update_raw_params(ccdc, &raw_params);
523 * vpfe_ccdc_config_ycbcr()
524 * This function will configure CCDC for YCbCr video capture
526 static void vpfe_ccdc_config_ycbcr(struct vpfe_ccdc *ccdc)
528 struct vpfe_device *vpfe = container_of(ccdc, struct vpfe_device, ccdc);
529 struct ccdc_params_ycbcr *params = &ccdc->ccdc_cfg.ycbcr;
532 vpfe_dbg(3, vpfe, "vpfe_ccdc_config_ycbcr:\n");
534 * first restore the CCDC registers to default values
535 * This is important since we assume default values to be set in
536 * a lot of registers that we didn't touch
538 vpfe_ccdc_restore_defaults(ccdc);
541 * configure pixel format, frame format, configure video frame
542 * format, enable output to SDRAM, enable internal timing generator
545 syn_mode = (((params->pix_fmt & VPFE_SYN_MODE_INPMOD_MASK) <<
546 VPFE_SYN_MODE_INPMOD_SHIFT) |
547 ((params->frm_fmt & VPFE_SYN_FLDMODE_MASK) <<
548 VPFE_SYN_FLDMODE_SHIFT) | VPFE_VDHDEN_ENABLE |
549 VPFE_WEN_ENABLE | VPFE_DATA_PACK_ENABLE);
551 /* setup BT.656 sync mode */
552 if (params->bt656_enable) {
553 vpfe_reg_write(ccdc, VPFE_REC656IF_BT656_EN, VPFE_REC656IF);
556 * configure the FID, VD, HD pin polarity,
557 * fld,hd pol positive, vd negative, 8-bit data
559 syn_mode |= VPFE_SYN_MODE_VD_POL_NEGATIVE;
560 if (ccdc->ccdc_cfg.if_type == VPFE_BT656_10BIT)
561 syn_mode |= VPFE_SYN_MODE_10BITS;
563 syn_mode |= VPFE_SYN_MODE_8BITS;
565 /* y/c external sync mode */
566 syn_mode |= (((params->fid_pol & VPFE_FID_POL_MASK) <<
567 VPFE_FID_POL_SHIFT) |
568 ((params->hd_pol & VPFE_HD_POL_MASK) <<
570 ((params->vd_pol & VPFE_VD_POL_MASK) <<
573 vpfe_reg_write(ccdc, syn_mode, VPFE_SYNMODE);
575 /* configure video window */
576 vpfe_ccdc_setwin(ccdc, ¶ms->win,
577 params->frm_fmt, params->bytesperpixel);
580 * configure the order of y cb cr in SDRAM, and disable latch
581 * internal register on vsync
583 if (ccdc->ccdc_cfg.if_type == VPFE_BT656_10BIT)
585 (params->pix_order << VPFE_CCDCFG_Y8POS_SHIFT) |
586 VPFE_LATCH_ON_VSYNC_DISABLE |
587 VPFE_CCDCFG_BW656_10BIT, VPFE_CCDCFG);
590 (params->pix_order << VPFE_CCDCFG_Y8POS_SHIFT) |
591 VPFE_LATCH_ON_VSYNC_DISABLE, VPFE_CCDCFG);
594 * configure the horizontal line offset. This should be a
595 * on 32 byte boundary. So clear LSB 5 bits
597 vpfe_reg_write(ccdc, params->bytesperline, VPFE_HSIZE_OFF);
599 /* configure the memory line offset */
600 if (params->buf_type == CCDC_BUFTYPE_FLD_INTERLEAVED)
601 /* two fields are interleaved in memory */
602 vpfe_reg_write(ccdc, VPFE_SDOFST_FIELD_INTERLEAVED,
607 vpfe_ccdc_config_black_clamp(struct vpfe_ccdc *ccdc,
608 struct vpfe_ccdc_black_clamp *bclamp)
612 if (!bclamp->enable) {
613 /* configure DCSub */
614 val = (bclamp->dc_sub) & VPFE_BLK_DC_SUB_MASK;
615 vpfe_reg_write(ccdc, val, VPFE_DCSUB);
616 vpfe_reg_write(ccdc, VPFE_CLAMP_DEFAULT_VAL, VPFE_CLAMP);
620 * Configure gain, Start pixel, No of line to be avg,
621 * No of pixel/line to be avg, & Enable the Black clamping
623 val = ((bclamp->sgain & VPFE_BLK_SGAIN_MASK) |
624 ((bclamp->start_pixel & VPFE_BLK_ST_PXL_MASK) <<
625 VPFE_BLK_ST_PXL_SHIFT) |
626 ((bclamp->sample_ln & VPFE_BLK_SAMPLE_LINE_MASK) <<
627 VPFE_BLK_SAMPLE_LINE_SHIFT) |
628 ((bclamp->sample_pixel & VPFE_BLK_SAMPLE_LN_MASK) <<
629 VPFE_BLK_SAMPLE_LN_SHIFT) | VPFE_BLK_CLAMP_ENABLE);
630 vpfe_reg_write(ccdc, val, VPFE_CLAMP);
631 /* If Black clamping is enable then make dcsub 0 */
632 vpfe_reg_write(ccdc, VPFE_DCSUB_DEFAULT_VAL, VPFE_DCSUB);
636 vpfe_ccdc_config_black_compense(struct vpfe_ccdc *ccdc,
637 struct vpfe_ccdc_black_compensation *bcomp)
641 val = ((bcomp->b & VPFE_BLK_COMP_MASK) |
642 ((bcomp->gb & VPFE_BLK_COMP_MASK) <<
643 VPFE_BLK_COMP_GB_COMP_SHIFT) |
644 ((bcomp->gr & VPFE_BLK_COMP_MASK) <<
645 VPFE_BLK_COMP_GR_COMP_SHIFT) |
646 ((bcomp->r & VPFE_BLK_COMP_MASK) <<
647 VPFE_BLK_COMP_R_COMP_SHIFT));
648 vpfe_reg_write(ccdc, val, VPFE_BLKCMP);
652 * vpfe_ccdc_config_raw()
653 * This function will configure CCDC for Raw capture mode
655 static void vpfe_ccdc_config_raw(struct vpfe_ccdc *ccdc)
657 struct vpfe_device *vpfe = container_of(ccdc, struct vpfe_device, ccdc);
658 struct vpfe_ccdc_config_params_raw *config_params =
659 &ccdc->ccdc_cfg.bayer.config_params;
660 struct ccdc_params_raw *params = &ccdc->ccdc_cfg.bayer;
661 unsigned int syn_mode;
664 vpfe_dbg(3, vpfe, "vpfe_ccdc_config_raw:\n");
667 vpfe_ccdc_restore_defaults(ccdc);
669 /* Disable latching function registers on VSYNC */
670 vpfe_reg_write(ccdc, VPFE_LATCH_ON_VSYNC_DISABLE, VPFE_CCDCFG);
673 * Configure the vertical sync polarity(SYN_MODE.VDPOL),
674 * horizontal sync polarity (SYN_MODE.HDPOL), frame id polarity
675 * (SYN_MODE.FLDPOL), frame format(progressive or interlace),
676 * data size(SYNMODE.DATSIZ), &pixel format (Input mode), output
677 * SDRAM, enable internal timing generator
679 syn_mode = (((params->vd_pol & VPFE_VD_POL_MASK) << VPFE_VD_POL_SHIFT) |
680 ((params->hd_pol & VPFE_HD_POL_MASK) << VPFE_HD_POL_SHIFT) |
681 ((params->fid_pol & VPFE_FID_POL_MASK) <<
682 VPFE_FID_POL_SHIFT) | ((params->frm_fmt &
683 VPFE_FRM_FMT_MASK) << VPFE_FRM_FMT_SHIFT) |
684 ((config_params->data_sz & VPFE_DATA_SZ_MASK) <<
685 VPFE_DATA_SZ_SHIFT) | ((params->pix_fmt &
686 VPFE_PIX_FMT_MASK) << VPFE_PIX_FMT_SHIFT) |
687 VPFE_WEN_ENABLE | VPFE_VDHDEN_ENABLE);
689 /* Enable and configure aLaw register if needed */
690 if (config_params->alaw.enable) {
691 val = ((config_params->alaw.gamma_wd &
692 VPFE_ALAW_GAMMA_WD_MASK) | VPFE_ALAW_ENABLE);
693 vpfe_reg_write(ccdc, val, VPFE_ALAW);
694 vpfe_dbg(3, vpfe, "\nWriting 0x%x to ALAW...\n", val);
697 /* Configure video window */
698 vpfe_ccdc_setwin(ccdc, ¶ms->win, params->frm_fmt,
699 params->bytesperpixel);
701 /* Configure Black Clamp */
702 vpfe_ccdc_config_black_clamp(ccdc, &config_params->blk_clamp);
704 /* Configure Black level compensation */
705 vpfe_ccdc_config_black_compense(ccdc, &config_params->blk_comp);
707 /* If data size is 8 bit then pack the data */
708 if ((config_params->data_sz == VPFE_CCDC_DATA_8BITS) ||
709 config_params->alaw.enable)
710 syn_mode |= VPFE_DATA_PACK_ENABLE;
713 * Configure Horizontal offset register. If pack 8 is enabled then
714 * 1 pixel will take 1 byte
716 vpfe_reg_write(ccdc, params->bytesperline, VPFE_HSIZE_OFF);
718 vpfe_dbg(3, vpfe, "Writing %d (%x) to HSIZE_OFF\n",
719 params->bytesperline, params->bytesperline);
721 /* Set value for SDOFST */
722 if (params->frm_fmt == CCDC_FRMFMT_INTERLACED) {
723 if (params->image_invert_enable) {
724 /* For interlace inverse mode */
725 vpfe_reg_write(ccdc, VPFE_INTERLACED_IMAGE_INVERT,
728 /* For interlace non inverse mode */
729 vpfe_reg_write(ccdc, VPFE_INTERLACED_NO_IMAGE_INVERT,
732 } else if (params->frm_fmt == CCDC_FRMFMT_PROGRESSIVE) {
733 vpfe_reg_write(ccdc, VPFE_PROGRESSIVE_NO_IMAGE_INVERT,
737 vpfe_reg_write(ccdc, syn_mode, VPFE_SYNMODE);
743 vpfe_ccdc_set_buftype(struct vpfe_ccdc *ccdc,
744 enum ccdc_buftype buf_type)
746 if (ccdc->ccdc_cfg.if_type == VPFE_RAW_BAYER)
747 ccdc->ccdc_cfg.bayer.buf_type = buf_type;
749 ccdc->ccdc_cfg.ycbcr.buf_type = buf_type;
754 static inline enum ccdc_buftype vpfe_ccdc_get_buftype(struct vpfe_ccdc *ccdc)
756 if (ccdc->ccdc_cfg.if_type == VPFE_RAW_BAYER)
757 return ccdc->ccdc_cfg.bayer.buf_type;
759 return ccdc->ccdc_cfg.ycbcr.buf_type;
762 static int vpfe_ccdc_set_pixel_format(struct vpfe_ccdc *ccdc, u32 pixfmt)
764 struct vpfe_device *vpfe = container_of(ccdc, struct vpfe_device, ccdc);
766 vpfe_dbg(1, vpfe, "vpfe_ccdc_set_pixel_format: if_type: %d, pixfmt:%s\n",
767 ccdc->ccdc_cfg.if_type, print_fourcc(pixfmt));
769 if (ccdc->ccdc_cfg.if_type == VPFE_RAW_BAYER) {
770 ccdc->ccdc_cfg.bayer.pix_fmt = CCDC_PIXFMT_RAW;
772 * Need to clear it in case it was left on
773 * after the last capture.
775 ccdc->ccdc_cfg.bayer.config_params.alaw.enable = 0;
778 case V4L2_PIX_FMT_SBGGR8:
779 ccdc->ccdc_cfg.bayer.config_params.alaw.enable = 1;
782 case V4L2_PIX_FMT_YUYV:
783 case V4L2_PIX_FMT_UYVY:
784 case V4L2_PIX_FMT_YUV420:
785 case V4L2_PIX_FMT_NV12:
786 case V4L2_PIX_FMT_RGB565X:
789 case V4L2_PIX_FMT_SBGGR16:
795 case V4L2_PIX_FMT_YUYV:
796 ccdc->ccdc_cfg.ycbcr.pix_order = CCDC_PIXORDER_YCBYCR;
799 case V4L2_PIX_FMT_UYVY:
800 ccdc->ccdc_cfg.ycbcr.pix_order = CCDC_PIXORDER_CBYCRY;
811 static u32 vpfe_ccdc_get_pixel_format(struct vpfe_ccdc *ccdc)
815 if (ccdc->ccdc_cfg.if_type == VPFE_RAW_BAYER) {
816 pixfmt = V4L2_PIX_FMT_YUYV;
818 if (ccdc->ccdc_cfg.ycbcr.pix_order == CCDC_PIXORDER_YCBYCR)
819 pixfmt = V4L2_PIX_FMT_YUYV;
821 pixfmt = V4L2_PIX_FMT_UYVY;
828 vpfe_ccdc_set_image_window(struct vpfe_ccdc *ccdc,
829 struct v4l2_rect *win, unsigned int bpp)
831 if (ccdc->ccdc_cfg.if_type == VPFE_RAW_BAYER) {
832 ccdc->ccdc_cfg.bayer.win = *win;
833 ccdc->ccdc_cfg.bayer.bytesperpixel = bpp;
834 ccdc->ccdc_cfg.bayer.bytesperline = ALIGN(win->width * bpp, 32);
836 ccdc->ccdc_cfg.ycbcr.win = *win;
837 ccdc->ccdc_cfg.ycbcr.bytesperpixel = bpp;
838 ccdc->ccdc_cfg.ycbcr.bytesperline = ALIGN(win->width * bpp, 32);
845 vpfe_ccdc_get_image_window(struct vpfe_ccdc *ccdc,
846 struct v4l2_rect *win)
848 if (ccdc->ccdc_cfg.if_type == VPFE_RAW_BAYER)
849 *win = ccdc->ccdc_cfg.bayer.win;
851 *win = ccdc->ccdc_cfg.ycbcr.win;
854 static inline unsigned int vpfe_ccdc_get_line_length(struct vpfe_ccdc *ccdc)
856 if (ccdc->ccdc_cfg.if_type == VPFE_RAW_BAYER)
857 return ccdc->ccdc_cfg.bayer.bytesperline;
859 return ccdc->ccdc_cfg.ycbcr.bytesperline;
863 vpfe_ccdc_set_frame_format(struct vpfe_ccdc *ccdc,
864 enum ccdc_frmfmt frm_fmt)
866 if (ccdc->ccdc_cfg.if_type == VPFE_RAW_BAYER)
867 ccdc->ccdc_cfg.bayer.frm_fmt = frm_fmt;
869 ccdc->ccdc_cfg.ycbcr.frm_fmt = frm_fmt;
874 static inline enum ccdc_frmfmt
875 vpfe_ccdc_get_frame_format(struct vpfe_ccdc *ccdc)
877 if (ccdc->ccdc_cfg.if_type == VPFE_RAW_BAYER)
878 return ccdc->ccdc_cfg.bayer.frm_fmt;
880 return ccdc->ccdc_cfg.ycbcr.frm_fmt;
883 static inline int vpfe_ccdc_getfid(struct vpfe_ccdc *ccdc)
885 return (vpfe_reg_read(ccdc, VPFE_SYNMODE) >> 15) & 1;
888 static inline void vpfe_set_sdr_addr(struct vpfe_ccdc *ccdc, unsigned long addr)
890 vpfe_reg_write(ccdc, addr & 0xffffffe0, VPFE_SDR_ADDR);
893 static int vpfe_ccdc_set_hw_if_params(struct vpfe_ccdc *ccdc,
894 struct vpfe_hw_if_param *params)
896 struct vpfe_device *vpfe = container_of(ccdc, struct vpfe_device, ccdc);
898 ccdc->ccdc_cfg.if_type = params->if_type;
900 switch (params->if_type) {
902 case VPFE_YCBCR_SYNC_16:
903 case VPFE_YCBCR_SYNC_8:
904 case VPFE_BT656_10BIT:
905 ccdc->ccdc_cfg.ycbcr.vd_pol = params->vdpol;
906 ccdc->ccdc_cfg.ycbcr.hd_pol = params->hdpol;
910 ccdc->ccdc_cfg.bayer.vd_pol = params->vdpol;
911 ccdc->ccdc_cfg.bayer.hd_pol = params->hdpol;
912 if (params->bus_width == 10)
913 ccdc->ccdc_cfg.bayer.config_params.data_sz =
914 VPFE_CCDC_DATA_10BITS;
916 ccdc->ccdc_cfg.bayer.config_params.data_sz =
917 VPFE_CCDC_DATA_8BITS;
918 vpfe_dbg(1, vpfe, "params.bus_width: %d\n",
920 vpfe_dbg(1, vpfe, "config_params.data_sz: %d\n",
921 ccdc->ccdc_cfg.bayer.config_params.data_sz);
931 static void vpfe_clear_intr(struct vpfe_ccdc *ccdc, int vdint)
933 unsigned int vpfe_int_status;
935 vpfe_int_status = vpfe_reg_read(ccdc, VPFE_IRQ_STS);
940 vpfe_int_status &= ~VPFE_VDINT0;
941 vpfe_int_status |= VPFE_VDINT0;
946 vpfe_int_status &= ~VPFE_VDINT1;
947 vpfe_int_status |= VPFE_VDINT1;
952 vpfe_int_status &= ~VPFE_VDINT2;
953 vpfe_int_status |= VPFE_VDINT2;
956 /* Clear all interrupts */
958 vpfe_int_status &= ~(VPFE_VDINT0 |
961 vpfe_int_status |= (VPFE_VDINT0 |
966 /* Clear specific VDINT from the status register */
967 vpfe_reg_write(ccdc, vpfe_int_status, VPFE_IRQ_STS);
969 vpfe_int_status = vpfe_reg_read(ccdc, VPFE_IRQ_STS);
971 /* Acknowledge that we are done with all interrupts */
972 vpfe_reg_write(ccdc, 1, VPFE_IRQ_EOI);
975 static void vpfe_ccdc_config_defaults(struct vpfe_ccdc *ccdc)
977 ccdc->ccdc_cfg.if_type = VPFE_RAW_BAYER;
979 ccdc->ccdc_cfg.ycbcr.pix_fmt = CCDC_PIXFMT_YCBCR_8BIT;
980 ccdc->ccdc_cfg.ycbcr.frm_fmt = CCDC_FRMFMT_INTERLACED;
981 ccdc->ccdc_cfg.ycbcr.fid_pol = VPFE_PINPOL_POSITIVE;
982 ccdc->ccdc_cfg.ycbcr.vd_pol = VPFE_PINPOL_POSITIVE;
983 ccdc->ccdc_cfg.ycbcr.hd_pol = VPFE_PINPOL_POSITIVE;
984 ccdc->ccdc_cfg.ycbcr.pix_order = CCDC_PIXORDER_CBYCRY;
985 ccdc->ccdc_cfg.ycbcr.buf_type = CCDC_BUFTYPE_FLD_INTERLEAVED;
987 ccdc->ccdc_cfg.ycbcr.win.left = 0;
988 ccdc->ccdc_cfg.ycbcr.win.top = 0;
989 ccdc->ccdc_cfg.ycbcr.win.width = 720;
990 ccdc->ccdc_cfg.ycbcr.win.height = 576;
991 ccdc->ccdc_cfg.ycbcr.bt656_enable = 1;
993 ccdc->ccdc_cfg.bayer.pix_fmt = CCDC_PIXFMT_RAW;
994 ccdc->ccdc_cfg.bayer.frm_fmt = CCDC_FRMFMT_PROGRESSIVE;
995 ccdc->ccdc_cfg.bayer.fid_pol = VPFE_PINPOL_POSITIVE;
996 ccdc->ccdc_cfg.bayer.vd_pol = VPFE_PINPOL_POSITIVE;
997 ccdc->ccdc_cfg.bayer.hd_pol = VPFE_PINPOL_POSITIVE;
999 ccdc->ccdc_cfg.bayer.win.left = 0;
1000 ccdc->ccdc_cfg.bayer.win.top = 0;
1001 ccdc->ccdc_cfg.bayer.win.width = 800;
1002 ccdc->ccdc_cfg.bayer.win.height = 600;
1003 ccdc->ccdc_cfg.bayer.config_params.data_sz = VPFE_CCDC_DATA_8BITS;
1004 ccdc->ccdc_cfg.bayer.config_params.alaw.gamma_wd =
1005 VPFE_CCDC_GAMMA_BITS_09_0;
1009 * vpfe_get_ccdc_image_format - Get image parameters based on CCDC settings
1011 static int vpfe_get_ccdc_image_format(struct vpfe_device *vpfe,
1012 struct v4l2_format *f)
1014 struct v4l2_rect image_win;
1015 enum ccdc_buftype buf_type;
1016 enum ccdc_frmfmt frm_fmt;
1018 memset(f, 0, sizeof(*f));
1019 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1020 vpfe_ccdc_get_image_window(&vpfe->ccdc, &image_win);
1021 f->fmt.pix.width = image_win.width;
1022 f->fmt.pix.height = image_win.height;
1023 f->fmt.pix.bytesperline = vpfe_ccdc_get_line_length(&vpfe->ccdc);
1024 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
1026 buf_type = vpfe_ccdc_get_buftype(&vpfe->ccdc);
1027 f->fmt.pix.pixelformat = vpfe_ccdc_get_pixel_format(&vpfe->ccdc);
1028 frm_fmt = vpfe_ccdc_get_frame_format(&vpfe->ccdc);
1030 if (frm_fmt == CCDC_FRMFMT_PROGRESSIVE) {
1031 f->fmt.pix.field = V4L2_FIELD_NONE;
1032 } else if (frm_fmt == CCDC_FRMFMT_INTERLACED) {
1033 if (buf_type == CCDC_BUFTYPE_FLD_INTERLEAVED) {
1034 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
1035 } else if (buf_type == CCDC_BUFTYPE_FLD_SEPARATED) {
1036 f->fmt.pix.field = V4L2_FIELD_SEQ_TB;
1038 vpfe_err(vpfe, "Invalid buf_type\n");
1042 vpfe_err(vpfe, "Invalid frm_fmt\n");
1048 static int vpfe_config_ccdc_image_format(struct vpfe_device *vpfe)
1050 enum ccdc_frmfmt frm_fmt = CCDC_FRMFMT_INTERLACED;
1053 vpfe_dbg(2, vpfe, "vpfe_config_ccdc_image_format\n");
1055 vpfe_dbg(1, vpfe, "pixelformat: %s\n",
1056 print_fourcc(vpfe->fmt.fmt.pix.pixelformat));
1058 if (vpfe_ccdc_set_pixel_format(&vpfe->ccdc,
1059 vpfe->fmt.fmt.pix.pixelformat) < 0) {
1060 vpfe_err(vpfe, "couldn't set pix format in ccdc\n");
1064 /* configure the image window */
1065 vpfe_ccdc_set_image_window(&vpfe->ccdc, &vpfe->crop, vpfe->bpp);
1067 switch (vpfe->fmt.fmt.pix.field) {
1068 case V4L2_FIELD_INTERLACED:
1069 /* do nothing, since it is default */
1070 ret = vpfe_ccdc_set_buftype(
1072 CCDC_BUFTYPE_FLD_INTERLEAVED);
1075 case V4L2_FIELD_NONE:
1076 frm_fmt = CCDC_FRMFMT_PROGRESSIVE;
1077 /* buffer type only applicable for interlaced scan */
1080 case V4L2_FIELD_SEQ_TB:
1081 ret = vpfe_ccdc_set_buftype(
1083 CCDC_BUFTYPE_FLD_SEPARATED);
1093 return vpfe_ccdc_set_frame_format(&vpfe->ccdc, frm_fmt);
1097 * vpfe_config_image_format()
1098 * For a given standard, this functions sets up the default
1099 * pix format & crop values in the vpfe device and ccdc. It first
1100 * starts with defaults based values from the standard table.
1101 * It then checks if sub device supports get_fmt and then override the
1102 * values based on that.Sets crop values to match with scan resolution
1103 * starting at 0,0. It calls vpfe_config_ccdc_image_format() set the
1106 static int vpfe_config_image_format(struct vpfe_device *vpfe,
1109 struct v4l2_pix_format *pix = &vpfe->fmt.fmt.pix;
1112 for (i = 0; i < ARRAY_SIZE(vpfe_standards); i++) {
1113 if (vpfe_standards[i].std_id & std_id) {
1114 vpfe->std_info.active_pixels =
1115 vpfe_standards[i].width;
1116 vpfe->std_info.active_lines =
1117 vpfe_standards[i].height;
1118 vpfe->std_info.frame_format =
1119 vpfe_standards[i].frame_format;
1120 vpfe->std_index = i;
1126 if (i == ARRAY_SIZE(vpfe_standards)) {
1127 vpfe_err(vpfe, "standard not supported\n");
1131 vpfe->crop.top = vpfe->crop.left = 0;
1132 vpfe->crop.width = vpfe->std_info.active_pixels;
1133 vpfe->crop.height = vpfe->std_info.active_lines;
1134 pix->width = vpfe->crop.width;
1135 pix->height = vpfe->crop.height;
1136 pix->pixelformat = V4L2_PIX_FMT_YUYV;
1138 /* first field and frame format based on standard frame format */
1139 if (vpfe->std_info.frame_format)
1140 pix->field = V4L2_FIELD_INTERLACED;
1142 pix->field = V4L2_FIELD_NONE;
1144 ret = __vpfe_get_format(vpfe, &vpfe->fmt, &vpfe->bpp);
1148 /* Update the crop window based on found values */
1149 vpfe->crop.width = pix->width;
1150 vpfe->crop.height = pix->height;
1152 return vpfe_config_ccdc_image_format(vpfe);
1155 static int vpfe_initialize_device(struct vpfe_device *vpfe)
1157 struct vpfe_subdev_info *sdinfo;
1160 sdinfo = &vpfe->cfg->sub_devs[0];
1161 sdinfo->sd = vpfe->sd[0];
1162 vpfe->current_input = 0;
1163 vpfe->std_index = 0;
1164 /* Configure the default format information */
1165 ret = vpfe_config_image_format(vpfe,
1166 vpfe_standards[vpfe->std_index].std_id);
1170 pm_runtime_get_sync(vpfe->pdev);
1172 vpfe_config_enable(&vpfe->ccdc, 1);
1174 vpfe_ccdc_restore_defaults(&vpfe->ccdc);
1176 /* Clear all VPFE interrupts */
1177 vpfe_clear_intr(&vpfe->ccdc, -1);
1183 * vpfe_release : This function is based on the vb2_fop_release
1185 * It has been augmented to handle module power management,
1186 * by disabling/enabling h/w module fcntl clock when necessary.
1188 static int vpfe_release(struct file *file)
1190 struct vpfe_device *vpfe = video_drvdata(file);
1194 mutex_lock(&vpfe->lock);
1196 /* Save the singular status before we call the clean-up helper */
1197 fh_singular = v4l2_fh_is_singular_file(file);
1199 /* the release helper will cleanup any on-going streaming */
1200 ret = _vb2_fop_release(file, NULL);
1203 * If this was the last open file.
1204 * Then de-initialize hw module.
1207 vpfe_ccdc_close(&vpfe->ccdc, vpfe->pdev);
1209 mutex_unlock(&vpfe->lock);
1215 * vpfe_open : This function is based on the v4l2_fh_open helper function.
1216 * It has been augmented to handle module power management,
1217 * by disabling/enabling h/w module fcntl clock when necessary.
1219 static int vpfe_open(struct file *file)
1221 struct vpfe_device *vpfe = video_drvdata(file);
1224 mutex_lock(&vpfe->lock);
1226 ret = v4l2_fh_open(file);
1228 vpfe_err(vpfe, "v4l2_fh_open failed\n");
1232 if (!v4l2_fh_is_singular_file(file))
1235 if (vpfe_initialize_device(vpfe)) {
1236 v4l2_fh_release(file);
1241 mutex_unlock(&vpfe->lock);
1246 * vpfe_schedule_next_buffer: set next buffer address for capture
1247 * @vpfe : ptr to vpfe device
1249 * This function will get next buffer from the dma queue and
1250 * set the buffer address in the vpfe register for capture.
1251 * the buffer is marked active
1253 * Assumes caller is holding vpfe->dma_queue_lock already
1255 static inline void vpfe_schedule_next_buffer(struct vpfe_device *vpfe)
1257 vpfe->next_frm = list_entry(vpfe->dma_queue.next,
1258 struct vpfe_cap_buffer, list);
1259 list_del(&vpfe->next_frm->list);
1261 vpfe_set_sdr_addr(&vpfe->ccdc,
1262 vb2_dma_contig_plane_dma_addr(&vpfe->next_frm->vb.vb2_buf, 0));
1265 static inline void vpfe_schedule_bottom_field(struct vpfe_device *vpfe)
1269 addr = vb2_dma_contig_plane_dma_addr(&vpfe->next_frm->vb.vb2_buf, 0) +
1272 vpfe_set_sdr_addr(&vpfe->ccdc, addr);
1276 * vpfe_process_buffer_complete: process a completed buffer
1277 * @vpfe : ptr to vpfe device
1279 * This function time stamp the buffer and mark it as DONE. It also
1280 * wake up any process waiting on the QUEUE and set the next buffer
1283 static inline void vpfe_process_buffer_complete(struct vpfe_device *vpfe)
1285 vpfe->cur_frm->vb.vb2_buf.timestamp = ktime_get_ns();
1286 vpfe->cur_frm->vb.field = vpfe->fmt.fmt.pix.field;
1287 vpfe->cur_frm->vb.sequence = vpfe->sequence++;
1288 vb2_buffer_done(&vpfe->cur_frm->vb.vb2_buf, VB2_BUF_STATE_DONE);
1289 vpfe->cur_frm = vpfe->next_frm;
1293 * vpfe_isr : ISR handler for vpfe capture (VINT0)
1295 * @dev_id: dev_id ptr
1297 * It changes status of the captured buffer, takes next buffer from the queue
1298 * and sets its address in VPFE registers
1300 static irqreturn_t vpfe_isr(int irq, void *dev)
1302 struct vpfe_device *vpfe = (struct vpfe_device *)dev;
1303 enum v4l2_field field;
1307 intr_status = vpfe_reg_read(&vpfe->ccdc, VPFE_IRQ_STS);
1309 if (intr_status & VPFE_VDINT0) {
1310 field = vpfe->fmt.fmt.pix.field;
1312 if (field == V4L2_FIELD_NONE) {
1313 /* handle progressive frame capture */
1314 if (vpfe->cur_frm != vpfe->next_frm)
1315 vpfe_process_buffer_complete(vpfe);
1319 /* interlaced or TB capture check which field
1320 we are in hardware */
1321 fid = vpfe_ccdc_getfid(&vpfe->ccdc);
1323 /* switch the software maintained field id */
1325 if (fid == vpfe->field) {
1326 /* we are in-sync here,continue */
1329 * One frame is just being captured. If the
1330 * next frame is available, release the
1331 * current frame and move on
1333 if (vpfe->cur_frm != vpfe->next_frm)
1334 vpfe_process_buffer_complete(vpfe);
1336 * based on whether the two fields are stored
1337 * interleave or separately in memory,
1338 * reconfigure the CCDC memory address
1340 if (field == V4L2_FIELD_SEQ_TB)
1341 vpfe_schedule_bottom_field(vpfe);
1346 * if one field is just being captured configure
1347 * the next frame get the next frame from the empty
1348 * queue if no frame is available hold on to the
1351 spin_lock(&vpfe->dma_queue_lock);
1352 if (!list_empty(&vpfe->dma_queue) &&
1353 vpfe->cur_frm == vpfe->next_frm)
1354 vpfe_schedule_next_buffer(vpfe);
1355 spin_unlock(&vpfe->dma_queue_lock);
1356 } else if (fid == 0) {
1358 * out of sync. Recover from any hardware out-of-sync.
1359 * May loose one frame
1366 if (intr_status & VPFE_VDINT1) {
1367 spin_lock(&vpfe->dma_queue_lock);
1368 if (vpfe->fmt.fmt.pix.field == V4L2_FIELD_NONE &&
1369 !list_empty(&vpfe->dma_queue) &&
1370 vpfe->cur_frm == vpfe->next_frm)
1371 vpfe_schedule_next_buffer(vpfe);
1372 spin_unlock(&vpfe->dma_queue_lock);
1375 vpfe_clear_intr(&vpfe->ccdc, intr_status);
1380 static inline void vpfe_detach_irq(struct vpfe_device *vpfe)
1382 unsigned int intr = VPFE_VDINT0;
1383 enum ccdc_frmfmt frame_format;
1385 frame_format = vpfe_ccdc_get_frame_format(&vpfe->ccdc);
1386 if (frame_format == CCDC_FRMFMT_PROGRESSIVE)
1387 intr |= VPFE_VDINT1;
1389 vpfe_reg_write(&vpfe->ccdc, intr, VPFE_IRQ_EN_CLR);
1392 static inline void vpfe_attach_irq(struct vpfe_device *vpfe)
1394 unsigned int intr = VPFE_VDINT0;
1395 enum ccdc_frmfmt frame_format;
1397 frame_format = vpfe_ccdc_get_frame_format(&vpfe->ccdc);
1398 if (frame_format == CCDC_FRMFMT_PROGRESSIVE)
1399 intr |= VPFE_VDINT1;
1401 vpfe_reg_write(&vpfe->ccdc, intr, VPFE_IRQ_EN_SET);
1404 static int vpfe_querycap(struct file *file, void *priv,
1405 struct v4l2_capability *cap)
1407 struct vpfe_device *vpfe = video_drvdata(file);
1409 vpfe_dbg(2, vpfe, "vpfe_querycap\n");
1411 strlcpy(cap->driver, VPFE_MODULE_NAME, sizeof(cap->driver));
1412 strlcpy(cap->card, "TI AM437x VPFE", sizeof(cap->card));
1413 snprintf(cap->bus_info, sizeof(cap->bus_info),
1414 "platform:%s", vpfe->v4l2_dev.name);
1415 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING |
1417 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1422 /* get the format set at output pad of the adjacent subdev */
1423 static int __vpfe_get_format(struct vpfe_device *vpfe,
1424 struct v4l2_format *format, unsigned int *bpp)
1426 struct v4l2_mbus_framefmt mbus_fmt;
1427 struct vpfe_subdev_info *sdinfo;
1428 struct v4l2_subdev_format fmt;
1431 sdinfo = vpfe->current_subdev;
1435 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1438 ret = v4l2_subdev_call(sdinfo->sd, pad, get_fmt, NULL, &fmt);
1439 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
1443 v4l2_fill_pix_format(&format->fmt.pix, &fmt.format);
1444 mbus_to_pix(vpfe, &fmt.format, &format->fmt.pix, bpp);
1446 ret = v4l2_device_call_until_err(&vpfe->v4l2_dev,
1450 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
1452 v4l2_fill_pix_format(&format->fmt.pix, &mbus_fmt);
1453 mbus_to_pix(vpfe, &mbus_fmt, &format->fmt.pix, bpp);
1456 format->type = vpfe->fmt.type;
1459 "%s size %dx%d (%s) bytesperline = %d, size = %d, bpp = %d\n",
1460 __func__, format->fmt.pix.width, format->fmt.pix.height,
1461 print_fourcc(format->fmt.pix.pixelformat),
1462 format->fmt.pix.bytesperline, format->fmt.pix.sizeimage, *bpp);
1467 /* set the format at output pad of the adjacent subdev */
1468 static int __vpfe_set_format(struct vpfe_device *vpfe,
1469 struct v4l2_format *format, unsigned int *bpp)
1471 struct vpfe_subdev_info *sdinfo;
1472 struct v4l2_subdev_format fmt;
1475 vpfe_dbg(2, vpfe, "__vpfe_set_format\n");
1477 sdinfo = vpfe->current_subdev;
1481 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1484 pix_to_mbus(vpfe, &format->fmt.pix, &fmt.format);
1486 ret = v4l2_subdev_call(sdinfo->sd, pad, set_fmt, NULL, &fmt);
1490 v4l2_fill_pix_format(&format->fmt.pix, &fmt.format);
1491 mbus_to_pix(vpfe, &fmt.format, &format->fmt.pix, bpp);
1493 format->type = vpfe->fmt.type;
1496 "%s size %dx%d (%s) bytesperline = %d, size = %d, bpp = %d\n",
1497 __func__, format->fmt.pix.width, format->fmt.pix.height,
1498 print_fourcc(format->fmt.pix.pixelformat),
1499 format->fmt.pix.bytesperline, format->fmt.pix.sizeimage, *bpp);
1504 static int vpfe_g_fmt(struct file *file, void *priv,
1505 struct v4l2_format *fmt)
1507 struct vpfe_device *vpfe = video_drvdata(file);
1509 vpfe_dbg(2, vpfe, "vpfe_g_fmt\n");
1516 static int vpfe_enum_fmt(struct file *file, void *priv,
1517 struct v4l2_fmtdesc *f)
1519 struct vpfe_device *vpfe = video_drvdata(file);
1520 struct vpfe_subdev_info *sdinfo;
1521 struct vpfe_fmt *fmt = NULL;
1524 vpfe_dbg(2, vpfe, "vpfe_enum_format index:%d\n",
1527 sdinfo = vpfe->current_subdev;
1531 if (f->index > ARRAY_SIZE(formats))
1534 for (k = 0; k < ARRAY_SIZE(formats); k++) {
1535 if (formats[k].index == f->index) {
1543 strncpy(f->description, fmt->name, sizeof(f->description) - 1);
1544 f->pixelformat = fmt->fourcc;
1545 f->type = vpfe->fmt.type;
1547 vpfe_dbg(1, vpfe, "vpfe_enum_format: mbus index: %d code: %x pixelformat: %s [%s]\n",
1548 f->index, fmt->code, print_fourcc(fmt->fourcc), fmt->name);
1553 static int vpfe_try_fmt(struct file *file, void *priv,
1554 struct v4l2_format *fmt)
1556 struct vpfe_device *vpfe = video_drvdata(file);
1559 vpfe_dbg(2, vpfe, "vpfe_try_fmt\n");
1561 return __vpfe_get_format(vpfe, fmt, &bpp);
1564 static int vpfe_s_fmt(struct file *file, void *priv,
1565 struct v4l2_format *fmt)
1567 struct vpfe_device *vpfe = video_drvdata(file);
1568 struct v4l2_format format;
1572 vpfe_dbg(2, vpfe, "vpfe_s_fmt\n");
1574 /* If streaming is started, return error */
1575 if (vb2_is_busy(&vpfe->buffer_queue)) {
1576 vpfe_err(vpfe, "%s device busy\n", __func__);
1580 ret = __vpfe_get_format(vpfe, &format, &bpp);
1585 if (!cmp_v4l2_format(fmt, &format)) {
1586 /* Sensor format is different from the requested format
1587 * so we need to change it
1589 ret = __vpfe_set_format(vpfe, fmt, &bpp);
1592 } else /* Just make sure all of the fields are consistent */
1595 /* First detach any IRQ if currently attached */
1596 vpfe_detach_irq(vpfe);
1600 /* Update the crop window based on found values */
1601 vpfe->crop.width = fmt->fmt.pix.width;
1602 vpfe->crop.height = fmt->fmt.pix.height;
1604 /* set image capture parameters in the ccdc */
1605 return vpfe_config_ccdc_image_format(vpfe);
1608 static int vpfe_enum_size(struct file *file, void *priv,
1609 struct v4l2_frmsizeenum *fsize)
1611 struct vpfe_device *vpfe = video_drvdata(file);
1612 struct v4l2_subdev_frame_size_enum fse;
1613 struct vpfe_subdev_info *sdinfo;
1614 struct v4l2_mbus_framefmt mbus;
1615 struct v4l2_pix_format pix;
1616 struct vpfe_fmt *fmt;
1619 vpfe_dbg(2, vpfe, "vpfe_enum_size\n");
1621 /* check for valid format */
1622 fmt = find_format_by_pix(fsize->pixel_format);
1624 vpfe_dbg(3, vpfe, "Invalid pixel code: %x, default used instead\n",
1625 fsize->pixel_format);
1629 memset(fsize->reserved, 0x0, sizeof(fsize->reserved));
1631 sdinfo = vpfe->current_subdev;
1635 memset(&pix, 0x0, sizeof(pix));
1636 /* Construct pix from parameter and use default for the rest */
1637 pix.pixelformat = fsize->pixel_format;
1640 pix.colorspace = V4L2_COLORSPACE_SRGB;
1641 pix.field = V4L2_FIELD_NONE;
1642 pix_to_mbus(vpfe, &pix, &mbus);
1644 memset(&fse, 0x0, sizeof(fse));
1645 fse.index = fsize->index;
1647 fse.code = mbus.code;
1648 fse.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1649 ret = v4l2_subdev_call(sdinfo->sd, pad, enum_frame_size, NULL, &fse);
1653 vpfe_dbg(1, vpfe, "vpfe_enum_size: index: %d code: %x W:[%d,%d] H:[%d,%d]\n",
1654 fse.index, fse.code, fse.min_width, fse.max_width,
1655 fse.min_height, fse.max_height);
1657 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1658 fsize->discrete.width = fse.max_width;
1659 fsize->discrete.height = fse.max_height;
1661 vpfe_dbg(1, vpfe, "vpfe_enum_size: index: %d pixformat: %s size: %dx%d\n",
1662 fsize->index, print_fourcc(fsize->pixel_format),
1663 fsize->discrete.width, fsize->discrete.height);
1669 * vpfe_get_subdev_input_index - Get subdev index and subdev input index for a
1670 * given app input index
1673 vpfe_get_subdev_input_index(struct vpfe_device *vpfe,
1675 int *subdev_input_index,
1676 int app_input_index)
1680 for (i = 0; i < ARRAY_SIZE(vpfe->cfg->asd); i++) {
1681 if (app_input_index < (j + 1)) {
1683 *subdev_input_index = app_input_index - j;
1692 * vpfe_get_app_input - Get app input index for a given subdev input index
1693 * driver stores the input index of the current sub device and translate it
1694 * when application request the current input
1696 static int vpfe_get_app_input_index(struct vpfe_device *vpfe,
1697 int *app_input_index)
1699 struct vpfe_config *cfg = vpfe->cfg;
1700 struct vpfe_subdev_info *sdinfo;
1701 struct i2c_client *client;
1702 struct i2c_client *curr_client;
1705 curr_client = v4l2_get_subdevdata(vpfe->current_subdev->sd);
1706 for (i = 0; i < ARRAY_SIZE(vpfe->cfg->asd); i++) {
1707 sdinfo = &cfg->sub_devs[i];
1708 client = v4l2_get_subdevdata(sdinfo->sd);
1709 if (client->addr == curr_client->addr &&
1710 client->adapter->nr == curr_client->adapter->nr) {
1711 if (vpfe->current_input >= 1)
1713 *app_input_index = j + vpfe->current_input;
1721 static int vpfe_enum_input(struct file *file, void *priv,
1722 struct v4l2_input *inp)
1724 struct vpfe_device *vpfe = video_drvdata(file);
1725 struct vpfe_subdev_info *sdinfo;
1728 vpfe_dbg(2, vpfe, "vpfe_enum_input\n");
1730 if (vpfe_get_subdev_input_index(vpfe, &subdev, &index,
1733 "input information not found for the subdev\n");
1736 sdinfo = &vpfe->cfg->sub_devs[subdev];
1737 *inp = sdinfo->inputs[index];
1742 static int vpfe_g_input(struct file *file, void *priv, unsigned int *index)
1744 struct vpfe_device *vpfe = video_drvdata(file);
1746 vpfe_dbg(2, vpfe, "vpfe_g_input\n");
1748 return vpfe_get_app_input_index(vpfe, index);
1751 /* Assumes caller is holding vpfe_dev->lock */
1752 static int vpfe_set_input(struct vpfe_device *vpfe, unsigned int index)
1754 int subdev_index = 0, inp_index = 0;
1755 struct vpfe_subdev_info *sdinfo;
1756 struct vpfe_route *route;
1760 vpfe_dbg(2, vpfe, "vpfe_set_input: index: %d\n", index);
1762 /* If streaming is started, return error */
1763 if (vb2_is_busy(&vpfe->buffer_queue)) {
1764 vpfe_err(vpfe, "%s device busy\n", __func__);
1767 ret = vpfe_get_subdev_input_index(vpfe,
1772 vpfe_err(vpfe, "invalid input index: %d\n", index);
1776 sdinfo = &vpfe->cfg->sub_devs[subdev_index];
1777 sdinfo->sd = vpfe->sd[subdev_index];
1778 route = &sdinfo->routes[inp_index];
1779 if (route && sdinfo->can_route) {
1780 input = route->input;
1781 output = route->output;
1783 ret = v4l2_subdev_call(sdinfo->sd, video,
1784 s_routing, input, output, 0);
1786 vpfe_err(vpfe, "s_routing failed\n");
1794 vpfe->current_subdev = sdinfo;
1796 vpfe->v4l2_dev.ctrl_handler = sdinfo->sd->ctrl_handler;
1797 vpfe->current_input = index;
1798 vpfe->std_index = 0;
1800 /* set the bus/interface parameter for the sub device in ccdc */
1801 ret = vpfe_ccdc_set_hw_if_params(&vpfe->ccdc, &sdinfo->vpfe_param);
1805 /* set the default image parameters in the device */
1806 return vpfe_config_image_format(vpfe,
1807 vpfe_standards[vpfe->std_index].std_id);
1813 static int vpfe_s_input(struct file *file, void *priv, unsigned int index)
1815 struct vpfe_device *vpfe = video_drvdata(file);
1818 "vpfe_s_input: index: %d\n", index);
1820 return vpfe_set_input(vpfe, index);
1823 static int vpfe_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
1825 struct vpfe_device *vpfe = video_drvdata(file);
1826 struct vpfe_subdev_info *sdinfo;
1828 vpfe_dbg(2, vpfe, "vpfe_querystd\n");
1830 sdinfo = vpfe->current_subdev;
1831 if (!(sdinfo->inputs[0].capabilities & V4L2_IN_CAP_STD))
1834 /* Call querystd function of decoder device */
1835 return v4l2_device_call_until_err(&vpfe->v4l2_dev, sdinfo->grp_id,
1836 video, querystd, std_id);
1839 static int vpfe_s_std(struct file *file, void *priv, v4l2_std_id std_id)
1841 struct vpfe_device *vpfe = video_drvdata(file);
1842 struct vpfe_subdev_info *sdinfo;
1845 vpfe_dbg(2, vpfe, "vpfe_s_std\n");
1847 sdinfo = vpfe->current_subdev;
1848 if (!(sdinfo->inputs[0].capabilities & V4L2_IN_CAP_STD))
1851 /* If streaming is started, return error */
1852 if (vb2_is_busy(&vpfe->buffer_queue)) {
1853 vpfe_err(vpfe, "%s device busy\n", __func__);
1858 ret = v4l2_device_call_until_err(&vpfe->v4l2_dev, sdinfo->grp_id,
1859 video, s_std, std_id);
1861 vpfe_err(vpfe, "Failed to set standard\n");
1864 ret = vpfe_config_image_format(vpfe, std_id);
1869 static int vpfe_g_std(struct file *file, void *priv, v4l2_std_id *std_id)
1871 struct vpfe_device *vpfe = video_drvdata(file);
1872 struct vpfe_subdev_info *sdinfo;
1874 vpfe_dbg(2, vpfe, "vpfe_g_std\n");
1876 sdinfo = vpfe->current_subdev;
1877 if (sdinfo->inputs[0].capabilities != V4L2_IN_CAP_STD)
1880 *std_id = vpfe_standards[vpfe->std_index].std_id;
1886 * vpfe_calculate_offsets : This function calculates buffers offset
1887 * for top and bottom field
1889 static void vpfe_calculate_offsets(struct vpfe_device *vpfe)
1891 struct v4l2_rect image_win;
1893 vpfe_dbg(2, vpfe, "vpfe_calculate_offsets\n");
1895 vpfe_ccdc_get_image_window(&vpfe->ccdc, &image_win);
1896 vpfe->field_off = image_win.height * image_win.width;
1900 * vpfe_queue_setup - Callback function for buffer setup.
1901 * @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_devs: 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,
1911 unsigned int *nbuffers, unsigned int *nplanes,
1912 unsigned int sizes[], struct device *alloc_devs[])
1914 struct vpfe_device *vpfe = vb2_get_drv_priv(vq);
1915 unsigned size = vpfe->fmt.fmt.pix.sizeimage;
1917 if (vq->num_buffers + *nbuffers < 3)
1918 *nbuffers = 3 - vq->num_buffers;
1921 if (sizes[0] < size)
1930 "nbuffers=%d, size=%u\n", *nbuffers, sizes[0]);
1932 /* Calculate field offset */
1933 vpfe_calculate_offsets(vpfe);
1939 * vpfe_buffer_prepare : callback function for buffer prepare
1940 * @vb: ptr to vb2_buffer
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
1946 static int vpfe_buffer_prepare(struct vb2_buffer *vb)
1948 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1949 struct vpfe_device *vpfe = vb2_get_drv_priv(vb->vb2_queue);
1951 vb2_set_plane_payload(vb, 0, vpfe->fmt.fmt.pix.sizeimage);
1953 if (vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
1956 vbuf->field = vpfe->fmt.fmt.pix.field;
1962 * vpfe_buffer_queue : Callback function to add buffer to DMA queue
1963 * @vb: ptr to vb2_buffer
1965 static void vpfe_buffer_queue(struct vb2_buffer *vb)
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;
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);
1979 * vpfe_start_streaming : Starts the DMA engine for streaming
1980 * @vb: ptr to vb2_buffer
1981 * @count: number of buffers
1983 static int vpfe_start_streaming(struct vb2_queue *vq, unsigned int count)
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;
1992 spin_lock_irqsave(&vpfe->dma_queue_lock, flags);
1997 sdinfo = vpfe->current_subdev;
1999 vpfe_attach_irq(vpfe);
2001 if (vpfe->ccdc.ccdc_cfg.if_type == VPFE_RAW_BAYER)
2002 vpfe_ccdc_config_raw(&vpfe->ccdc);
2004 vpfe_ccdc_config_ycbcr(&vpfe->ccdc);
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);
2014 addr = vb2_dma_contig_plane_dma_addr(&vpfe->cur_frm->vb.vb2_buf, 0);
2016 vpfe_set_sdr_addr(&vpfe->ccdc, (unsigned long)(addr));
2018 vpfe_pcr_enable(&vpfe->ccdc, 1);
2020 ret = v4l2_subdev_call(sdinfo->sd, video, s_stream, 1);
2022 vpfe_err(vpfe, "Error in attaching interrupt handle\n");
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);
2038 * vpfe_stop_streaming : Stop the DMA engine
2039 * @vq: ptr to vb2_queue
2041 * This callback stops the DMA engine and any remaining buffers
2042 * in the DMA queue are released.
2044 static void vpfe_stop_streaming(struct vb2_queue *vq)
2046 struct vpfe_device *vpfe = vb2_get_drv_priv(vq);
2047 struct vpfe_subdev_info *sdinfo;
2048 unsigned long flags;
2051 vpfe_pcr_enable(&vpfe->ccdc, 0);
2053 vpfe_detach_irq(vpfe);
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");
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);
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);
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);
2081 spin_unlock_irqrestore(&vpfe->dma_queue_lock, flags);
2084 static int vpfe_cropcap(struct file *file, void *priv,
2085 struct v4l2_cropcap *crop)
2087 struct vpfe_device *vpfe = video_drvdata(file);
2089 vpfe_dbg(2, vpfe, "vpfe_cropcap\n");
2091 if (vpfe->std_index >= ARRAY_SIZE(vpfe_standards))
2094 memset(crop, 0, sizeof(struct v4l2_cropcap));
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;
2107 vpfe_g_selection(struct file *file, void *fh, struct v4l2_selection *s)
2109 struct vpfe_device *vpfe = video_drvdata(file);
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;
2119 case V4L2_SEL_TGT_CROP:
2130 static int enclosed_rectangle(struct v4l2_rect *a, struct v4l2_rect *b)
2132 if (a->left < b->left || a->top < b->top)
2135 if (a->left + a->width > b->left + b->width)
2138 if (a->top + a->height > b->top + b->height)
2145 vpfe_s_selection(struct file *file, void *fh, struct v4l2_selection *s)
2147 struct vpfe_device *vpfe = video_drvdata(file);
2148 struct v4l2_rect cr = vpfe->crop;
2149 struct v4l2_rect r = s->r;
2151 /* If streaming is started, return error */
2152 if (vb2_is_busy(&vpfe->buffer_queue)) {
2153 vpfe_err(vpfe, "%s device busy\n", __func__);
2157 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2158 s->target != V4L2_SEL_TGT_CROP)
2161 v4l_bound_align_image(&r.width, 0, cr.width, 0,
2162 &r.height, 0, cr.height, 0, 0);
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);
2167 if (s->flags & V4L2_SEL_FLAG_LE && !enclosed_rectangle(&r, &s->r))
2170 if (s->flags & V4L2_SEL_FLAG_GE && !enclosed_rectangle(&s->r, &r))
2173 s->r = vpfe->crop = r;
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;
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);
2188 static long vpfe_ioctl_default(struct file *file, void *priv,
2189 bool valid_prio, unsigned int cmd, void *param)
2191 struct vpfe_device *vpfe = video_drvdata(file);
2194 vpfe_dbg(2, vpfe, "vpfe_ioctl_default\n");
2197 vpfe_err(vpfe, "%s device busy\n", __func__);
2201 /* If streaming is started, return error */
2202 if (vb2_is_busy(&vpfe->buffer_queue)) {
2203 vpfe_err(vpfe, "%s device busy\n", __func__);
2208 case VIDIOC_AM437X_CCDC_CFG:
2209 ret = vpfe_ccdc_set_params(&vpfe->ccdc, (void __user *)param);
2212 "Error setting parameters in CCDC\n");
2215 ret = vpfe_get_ccdc_image_format(vpfe,
2219 "Invalid image format at CCDC\n");
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,
2242 /* vpfe capture driver file operations */
2243 static const struct v4l2_file_operations vpfe_fops = {
2244 .owner = THIS_MODULE,
2246 .release = vpfe_release,
2247 .read = vb2_fop_read,
2248 .poll = vb2_fop_poll,
2249 .unlocked_ioctl = video_ioctl2,
2250 .mmap = vb2_fop_mmap,
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,
2261 .vidioc_enum_framesizes = vpfe_enum_size,
2263 .vidioc_enum_input = vpfe_enum_input,
2264 .vidioc_g_input = vpfe_g_input,
2265 .vidioc_s_input = vpfe_s_input,
2267 .vidioc_querystd = vpfe_querystd,
2268 .vidioc_s_std = vpfe_s_std,
2269 .vidioc_g_std = vpfe_g_std,
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,
2281 .vidioc_log_status = v4l2_ctrl_log_status,
2282 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
2283 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
2285 .vidioc_cropcap = vpfe_cropcap,
2286 .vidioc_g_selection = vpfe_g_selection,
2287 .vidioc_s_selection = vpfe_s_selection,
2289 .vidioc_default = vpfe_ioctl_default,
2293 vpfe_async_bound(struct v4l2_async_notifier *notifier,
2294 struct v4l2_subdev *subdev,
2295 struct v4l2_async_subdev *asd)
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;
2304 vpfe_dbg(1, vpfe, "vpfe_async_bound\n");
2306 for (i = 0; i < ARRAY_SIZE(vpfe->cfg->asd); i++) {
2307 if (vpfe->cfg->asd[i]->match.fwnode ==
2308 asd[i].match.fwnode) {
2309 sdinfo = &vpfe->cfg->sub_devs[i];
2310 vpfe->sd[i] = subdev;
2311 vpfe->sd[i]->grp_id = sdinfo->grp_id;
2318 vpfe_info(vpfe, "sub device (%s) not matched\n", subdev->name);
2322 vpfe->video_dev.tvnorms |= sdinfo->inputs[0].std;
2324 /* setup the supported formats & indexes */
2325 for (j = 0, i = 0; ; ++j) {
2326 struct vpfe_fmt *fmt;
2329 memset(&mbus_code, 0, sizeof(mbus_code));
2330 mbus_code.index = j;
2331 mbus_code.which = V4L2_SUBDEV_FORMAT_ACTIVE;
2332 ret = v4l2_subdev_call(subdev, pad, enum_mbus_code,
2337 fmt = find_format_by_code(mbus_code.code);
2341 fmt->supported = true;
2348 static int vpfe_probe_complete(struct vpfe_device *vpfe)
2350 struct video_device *vdev;
2351 struct vb2_queue *q;
2354 spin_lock_init(&vpfe->dma_queue_lock);
2355 mutex_init(&vpfe->lock);
2357 vpfe->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2359 /* set first sub device as current one */
2360 vpfe->current_subdev = &vpfe->cfg->sub_devs[0];
2361 vpfe->v4l2_dev.ctrl_handler = vpfe->sd[0]->ctrl_handler;
2363 err = vpfe_set_input(vpfe, 0);
2367 /* Initialize videobuf2 queue as per the buffer type */
2368 q = &vpfe->buffer_queue;
2369 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2370 q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
2372 q->ops = &vpfe_video_qops;
2373 q->mem_ops = &vb2_dma_contig_memops;
2374 q->buf_struct_size = sizeof(struct vpfe_cap_buffer);
2375 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
2376 q->lock = &vpfe->lock;
2377 q->min_buffers_needed = 1;
2378 q->dev = vpfe->pdev;
2380 err = vb2_queue_init(q);
2382 vpfe_err(vpfe, "vb2_queue_init() failed\n");
2386 INIT_LIST_HEAD(&vpfe->dma_queue);
2388 vdev = &vpfe->video_dev;
2389 strlcpy(vdev->name, VPFE_MODULE_NAME, sizeof(vdev->name));
2390 vdev->release = video_device_release_empty;
2391 vdev->fops = &vpfe_fops;
2392 vdev->ioctl_ops = &vpfe_ioctl_ops;
2393 vdev->v4l2_dev = &vpfe->v4l2_dev;
2394 vdev->vfl_dir = VFL_DIR_RX;
2396 vdev->lock = &vpfe->lock;
2397 video_set_drvdata(vdev, vpfe);
2398 err = video_register_device(&vpfe->video_dev, VFL_TYPE_GRABBER, -1);
2401 "Unable to register video device.\n");
2408 v4l2_device_unregister(&vpfe->v4l2_dev);
2412 static int vpfe_async_complete(struct v4l2_async_notifier *notifier)
2414 struct vpfe_device *vpfe = container_of(notifier->v4l2_dev,
2415 struct vpfe_device, v4l2_dev);
2417 return vpfe_probe_complete(vpfe);
2420 static const struct v4l2_async_notifier_operations vpfe_async_ops = {
2421 .bound = vpfe_async_bound,
2422 .complete = vpfe_async_complete,
2425 static struct vpfe_config *
2426 vpfe_get_pdata(struct platform_device *pdev)
2428 struct device_node *endpoint = NULL;
2429 struct v4l2_fwnode_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_fwnode_endpoint_parse(of_fwnode_handle(endpoint),
2476 dev_err(&pdev->dev, "Could not parse the endpoint\n");
2480 sdinfo->vpfe_param.bus_width = bus_cfg.bus.parallel.bus_width;
2482 if (sdinfo->vpfe_param.bus_width < 8 ||
2483 sdinfo->vpfe_param.bus_width > 16) {
2484 dev_err(&pdev->dev, "Invalid bus width.\n");
2488 flags = bus_cfg.bus.parallel.flags;
2490 if (flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
2491 sdinfo->vpfe_param.hdpol = 1;
2493 if (flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH)
2494 sdinfo->vpfe_param.vdpol = 1;
2496 rem = of_graph_get_remote_port_parent(endpoint);
2498 dev_err(&pdev->dev, "Remote device at %pOF not found\n",
2503 pdata->asd[i] = devm_kzalloc(&pdev->dev,
2504 sizeof(struct v4l2_async_subdev),
2506 if (!pdata->asd[i]) {
2512 pdata->asd[i]->match_type = V4L2_ASYNC_MATCH_FWNODE;
2513 pdata->asd[i]->match.fwnode = of_fwnode_handle(rem);
2517 of_node_put(endpoint);
2521 of_node_put(endpoint);
2526 * vpfe_probe : This function creates device entries by register
2527 * itself to the V4L2 driver and initializes fields of each
2530 static int vpfe_probe(struct platform_device *pdev)
2532 struct vpfe_config *vpfe_cfg = vpfe_get_pdata(pdev);
2533 struct vpfe_device *vpfe;
2534 struct vpfe_ccdc *ccdc;
2535 struct resource *res;
2539 dev_err(&pdev->dev, "No platform data\n");
2543 vpfe = devm_kzalloc(&pdev->dev, sizeof(*vpfe), GFP_KERNEL);
2547 vpfe->pdev = &pdev->dev;
2548 vpfe->cfg = vpfe_cfg;
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);
2556 ret = platform_get_irq(pdev, 0);
2558 dev_err(&pdev->dev, "No IRQ resource\n");
2563 ret = devm_request_irq(vpfe->pdev, vpfe->irq, vpfe_isr, 0,
2564 "vpfe_capture0", vpfe);
2566 dev_err(&pdev->dev, "Unable to request interrupt\n");
2570 ret = v4l2_device_register(&pdev->dev, &vpfe->v4l2_dev);
2573 "Unable to register v4l2 device.\n");
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);
2582 /* for now just enable it here instead of waiting for the open */
2583 pm_runtime_get_sync(&pdev->dev);
2585 vpfe_ccdc_config_defaults(ccdc);
2587 pm_runtime_put_sync(&pdev->dev);
2589 vpfe->sd = devm_kcalloc(&pdev->dev,
2590 ARRAY_SIZE(vpfe->cfg->asd),
2591 sizeof(struct v4l2_subdev *),
2595 goto probe_out_v4l2_unregister;
2598 vpfe->notifier.subdevs = vpfe->cfg->asd;
2599 vpfe->notifier.num_subdevs = ARRAY_SIZE(vpfe->cfg->asd);
2600 vpfe->notifier.ops = &vpfe_async_ops;
2601 ret = v4l2_async_notifier_register(&vpfe->v4l2_dev,
2604 vpfe_err(vpfe, "Error registering async notifier\n");
2606 goto probe_out_v4l2_unregister;
2611 probe_out_v4l2_unregister:
2612 v4l2_device_unregister(&vpfe->v4l2_dev);
2617 * vpfe_remove : It un-register device from V4L2 driver
2619 static int vpfe_remove(struct platform_device *pdev)
2621 struct vpfe_device *vpfe = platform_get_drvdata(pdev);
2623 vpfe_dbg(2, vpfe, "vpfe_remove\n");
2625 pm_runtime_disable(&pdev->dev);
2627 v4l2_async_notifier_unregister(&vpfe->notifier);
2628 v4l2_device_unregister(&vpfe->v4l2_dev);
2629 video_unregister_device(&vpfe->video_dev);
2634 #ifdef CONFIG_PM_SLEEP
2636 static void vpfe_save_context(struct vpfe_ccdc *ccdc)
2638 ccdc->ccdc_ctx[VPFE_PCR >> 2] = vpfe_reg_read(ccdc, VPFE_PCR);
2639 ccdc->ccdc_ctx[VPFE_SYNMODE >> 2] = vpfe_reg_read(ccdc, VPFE_SYNMODE);
2640 ccdc->ccdc_ctx[VPFE_SDOFST >> 2] = vpfe_reg_read(ccdc, VPFE_SDOFST);
2641 ccdc->ccdc_ctx[VPFE_SDR_ADDR >> 2] = vpfe_reg_read(ccdc, VPFE_SDR_ADDR);
2642 ccdc->ccdc_ctx[VPFE_CLAMP >> 2] = vpfe_reg_read(ccdc, VPFE_CLAMP);
2643 ccdc->ccdc_ctx[VPFE_DCSUB >> 2] = vpfe_reg_read(ccdc, VPFE_DCSUB);
2644 ccdc->ccdc_ctx[VPFE_COLPTN >> 2] = vpfe_reg_read(ccdc, VPFE_COLPTN);
2645 ccdc->ccdc_ctx[VPFE_BLKCMP >> 2] = vpfe_reg_read(ccdc, VPFE_BLKCMP);
2646 ccdc->ccdc_ctx[VPFE_VDINT >> 2] = vpfe_reg_read(ccdc, VPFE_VDINT);
2647 ccdc->ccdc_ctx[VPFE_ALAW >> 2] = vpfe_reg_read(ccdc, VPFE_ALAW);
2648 ccdc->ccdc_ctx[VPFE_REC656IF >> 2] = vpfe_reg_read(ccdc, VPFE_REC656IF);
2649 ccdc->ccdc_ctx[VPFE_CCDCFG >> 2] = vpfe_reg_read(ccdc, VPFE_CCDCFG);
2650 ccdc->ccdc_ctx[VPFE_CULLING >> 2] = vpfe_reg_read(ccdc, VPFE_CULLING);
2651 ccdc->ccdc_ctx[VPFE_HD_VD_WID >> 2] = vpfe_reg_read(ccdc,
2653 ccdc->ccdc_ctx[VPFE_PIX_LINES >> 2] = vpfe_reg_read(ccdc,
2655 ccdc->ccdc_ctx[VPFE_HORZ_INFO >> 2] = vpfe_reg_read(ccdc,
2657 ccdc->ccdc_ctx[VPFE_VERT_START >> 2] = vpfe_reg_read(ccdc,
2659 ccdc->ccdc_ctx[VPFE_VERT_LINES >> 2] = vpfe_reg_read(ccdc,
2661 ccdc->ccdc_ctx[VPFE_HSIZE_OFF >> 2] = vpfe_reg_read(ccdc,
2665 static int vpfe_suspend(struct device *dev)
2667 struct vpfe_device *vpfe = dev_get_drvdata(dev);
2668 struct vpfe_ccdc *ccdc = &vpfe->ccdc;
2670 /* if streaming has not started we don't care */
2671 if (!vb2_start_streaming_called(&vpfe->buffer_queue))
2674 pm_runtime_get_sync(dev);
2675 vpfe_config_enable(ccdc, 1);
2677 /* Save VPFE context */
2678 vpfe_save_context(ccdc);
2681 vpfe_pcr_enable(ccdc, 0);
2682 vpfe_config_enable(ccdc, 0);
2684 /* Disable both master and slave clock */
2685 pm_runtime_put_sync(dev);
2687 /* Select sleep pin state */
2688 pinctrl_pm_select_sleep_state(dev);
2693 static void vpfe_restore_context(struct vpfe_ccdc *ccdc)
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],
2710 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_PIX_LINES >> 2],
2712 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_HORZ_INFO >> 2],
2714 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_VERT_START >> 2],
2716 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_VERT_LINES >> 2],
2718 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_HSIZE_OFF >> 2],
2722 static int vpfe_resume(struct device *dev)
2724 struct vpfe_device *vpfe = dev_get_drvdata(dev);
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);