treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / media / platform / am437x / am437x-vpfe.c
blob09104304bd06be9cbb92ec8ab008bcbcf6a58e5f
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * TI VPFE capture Driver
5 * Copyright (C) 2013 - 2014 Texas Instruments, Inc.
7 * Benoit Parrot <bparrot@ti.com>
8 * Lad, Prabhakar <prabhakar.csengg@gmail.com>
9 */
11 #include <linux/delay.h>
12 #include <linux/err.h>
13 #include <linux/init.h>
14 #include <linux/interrupt.h>
15 #include <linux/io.h>
16 #include <linux/module.h>
17 #include <linux/of_graph.h>
18 #include <linux/pinctrl/consumer.h>
19 #include <linux/platform_device.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/slab.h>
22 #include <linux/uaccess.h>
23 #include <linux/videodev2.h>
25 #include <media/v4l2-common.h>
26 #include <media/v4l2-ctrls.h>
27 #include <media/v4l2-event.h>
28 #include <media/v4l2-fwnode.h>
30 #include "am437x-vpfe.h"
32 #define VPFE_MODULE_NAME "vpfe"
33 #define VPFE_VERSION "0.1.0"
35 static int debug;
36 module_param(debug, int, 0644);
37 MODULE_PARM_DESC(debug, "Debug level 0-8");
39 #define vpfe_dbg(level, dev, fmt, arg...) \
40 v4l2_dbg(level, debug, &dev->v4l2_dev, fmt, ##arg)
41 #define vpfe_info(dev, fmt, arg...) \
42 v4l2_info(&dev->v4l2_dev, fmt, ##arg)
43 #define vpfe_err(dev, fmt, arg...) \
44 v4l2_err(&dev->v4l2_dev, fmt, ##arg)
46 /* standard information */
47 struct vpfe_standard {
48 v4l2_std_id std_id;
49 unsigned int width;
50 unsigned int height;
51 struct v4l2_fract pixelaspect;
52 int frame_format;
55 static const struct vpfe_standard vpfe_standards[] = {
56 {V4L2_STD_525_60, 720, 480, {11, 10}, 1},
57 {V4L2_STD_625_50, 720, 576, {54, 59}, 1},
60 static struct vpfe_fmt formats[VPFE_NUM_FORMATS] = {
62 .fourcc = V4L2_PIX_FMT_YUYV,
63 .code = MEDIA_BUS_FMT_YUYV8_2X8,
64 .bitsperpixel = 16,
65 }, {
66 .fourcc = V4L2_PIX_FMT_UYVY,
67 .code = MEDIA_BUS_FMT_UYVY8_2X8,
68 .bitsperpixel = 16,
69 }, {
70 .fourcc = V4L2_PIX_FMT_YVYU,
71 .code = MEDIA_BUS_FMT_YVYU8_2X8,
72 .bitsperpixel = 16,
73 }, {
74 .fourcc = V4L2_PIX_FMT_VYUY,
75 .code = MEDIA_BUS_FMT_VYUY8_2X8,
76 .bitsperpixel = 16,
77 }, {
78 .fourcc = V4L2_PIX_FMT_SBGGR8,
79 .code = MEDIA_BUS_FMT_SBGGR8_1X8,
80 .bitsperpixel = 8,
81 }, {
82 .fourcc = V4L2_PIX_FMT_SGBRG8,
83 .code = MEDIA_BUS_FMT_SGBRG8_1X8,
84 .bitsperpixel = 8,
85 }, {
86 .fourcc = V4L2_PIX_FMT_SGRBG8,
87 .code = MEDIA_BUS_FMT_SGRBG8_1X8,
88 .bitsperpixel = 8,
89 }, {
90 .fourcc = V4L2_PIX_FMT_SRGGB8,
91 .code = MEDIA_BUS_FMT_SRGGB8_1X8,
92 .bitsperpixel = 8,
93 }, {
94 .fourcc = V4L2_PIX_FMT_RGB565,
95 .code = MEDIA_BUS_FMT_RGB565_2X8_LE,
96 .bitsperpixel = 16,
97 }, {
98 .fourcc = V4L2_PIX_FMT_RGB565X,
99 .code = MEDIA_BUS_FMT_RGB565_2X8_BE,
100 .bitsperpixel = 16,
104 static int __subdev_get_format(struct vpfe_device *vpfe,
105 struct v4l2_mbus_framefmt *fmt);
106 static int vpfe_calc_format_size(struct vpfe_device *vpfe,
107 const struct vpfe_fmt *fmt,
108 struct v4l2_format *f);
110 static struct vpfe_fmt *find_format_by_code(struct vpfe_device *vpfe,
111 unsigned int code)
113 struct vpfe_fmt *fmt;
114 unsigned int k;
116 for (k = 0; k < vpfe->num_active_fmt; k++) {
117 fmt = vpfe->active_fmt[k];
118 if (fmt->code == code)
119 return fmt;
122 return NULL;
125 static struct vpfe_fmt *find_format_by_pix(struct vpfe_device *vpfe,
126 unsigned int pixelformat)
128 struct vpfe_fmt *fmt;
129 unsigned int k;
131 for (k = 0; k < vpfe->num_active_fmt; k++) {
132 fmt = vpfe->active_fmt[k];
133 if (fmt->fourcc == pixelformat)
134 return fmt;
137 return NULL;
140 static unsigned int __get_bytesperpixel(struct vpfe_device *vpfe,
141 const struct vpfe_fmt *fmt)
143 struct vpfe_subdev_info *sdinfo = vpfe->current_subdev;
144 unsigned int bus_width = sdinfo->vpfe_param.bus_width;
145 u32 bpp, bus_width_bytes, clocksperpixel;
147 bus_width_bytes = ALIGN(bus_width, 8) >> 3;
148 clocksperpixel = DIV_ROUND_UP(fmt->bitsperpixel, bus_width);
149 bpp = clocksperpixel * bus_width_bytes;
151 return bpp;
154 /* Print Four-character-code (FOURCC) */
155 static char *print_fourcc(u32 fmt)
157 static char code[5];
159 code[0] = (unsigned char)(fmt & 0xff);
160 code[1] = (unsigned char)((fmt >> 8) & 0xff);
161 code[2] = (unsigned char)((fmt >> 16) & 0xff);
162 code[3] = (unsigned char)((fmt >> 24) & 0xff);
163 code[4] = '\0';
165 return code;
168 static inline u32 vpfe_reg_read(struct vpfe_ccdc *ccdc, u32 offset)
170 return ioread32(ccdc->ccdc_cfg.base_addr + offset);
173 static inline void vpfe_reg_write(struct vpfe_ccdc *ccdc, u32 val, u32 offset)
175 iowrite32(val, ccdc->ccdc_cfg.base_addr + offset);
178 static inline struct vpfe_device *to_vpfe(struct vpfe_ccdc *ccdc)
180 return container_of(ccdc, struct vpfe_device, ccdc);
183 static inline
184 struct vpfe_cap_buffer *to_vpfe_buffer(struct vb2_v4l2_buffer *vb)
186 return container_of(vb, struct vpfe_cap_buffer, vb);
189 static inline void vpfe_pcr_enable(struct vpfe_ccdc *ccdc, int flag)
191 vpfe_reg_write(ccdc, !!flag, VPFE_PCR);
194 static void vpfe_config_enable(struct vpfe_ccdc *ccdc, int flag)
196 unsigned int cfg;
198 if (!flag) {
199 cfg = vpfe_reg_read(ccdc, VPFE_CONFIG);
200 cfg &= ~(VPFE_CONFIG_EN_ENABLE << VPFE_CONFIG_EN_SHIFT);
201 } else {
202 cfg = VPFE_CONFIG_EN_ENABLE << VPFE_CONFIG_EN_SHIFT;
205 vpfe_reg_write(ccdc, cfg, VPFE_CONFIG);
208 static void vpfe_ccdc_setwin(struct vpfe_ccdc *ccdc,
209 struct v4l2_rect *image_win,
210 enum ccdc_frmfmt frm_fmt,
211 int bpp)
213 int horz_start, horz_nr_pixels;
214 int vert_start, vert_nr_lines;
215 int val, mid_img;
218 * ppc - per pixel count. indicates how many pixels per cell
219 * output to SDRAM. example, for ycbcr, it is one y and one c, so 2.
220 * raw capture this is 1
222 horz_start = image_win->left * bpp;
223 horz_nr_pixels = (image_win->width * bpp) - 1;
224 vpfe_reg_write(ccdc, (horz_start << VPFE_HORZ_INFO_SPH_SHIFT) |
225 horz_nr_pixels, VPFE_HORZ_INFO);
227 vert_start = image_win->top;
229 if (frm_fmt == CCDC_FRMFMT_INTERLACED) {
230 vert_nr_lines = (image_win->height >> 1) - 1;
231 vert_start >>= 1;
232 /* configure VDINT0 */
233 val = (vert_start << VPFE_VDINT_VDINT0_SHIFT);
234 } else {
235 vert_nr_lines = image_win->height - 1;
237 * configure VDINT0 and VDINT1. VDINT1 will be at half
238 * of image height
240 mid_img = vert_start + (image_win->height / 2);
241 val = (vert_start << VPFE_VDINT_VDINT0_SHIFT) |
242 (mid_img & VPFE_VDINT_VDINT1_MASK);
245 vpfe_reg_write(ccdc, val, VPFE_VDINT);
247 vpfe_reg_write(ccdc, (vert_start << VPFE_VERT_START_SLV0_SHIFT) |
248 vert_start, VPFE_VERT_START);
249 vpfe_reg_write(ccdc, vert_nr_lines, VPFE_VERT_LINES);
252 static void vpfe_reg_dump(struct vpfe_ccdc *ccdc)
254 struct vpfe_device *vpfe = to_vpfe(ccdc);
256 vpfe_dbg(3, vpfe, "ALAW: 0x%x\n", vpfe_reg_read(ccdc, VPFE_ALAW));
257 vpfe_dbg(3, vpfe, "CLAMP: 0x%x\n", vpfe_reg_read(ccdc, VPFE_CLAMP));
258 vpfe_dbg(3, vpfe, "DCSUB: 0x%x\n", vpfe_reg_read(ccdc, VPFE_DCSUB));
259 vpfe_dbg(3, vpfe, "BLKCMP: 0x%x\n", vpfe_reg_read(ccdc, VPFE_BLKCMP));
260 vpfe_dbg(3, vpfe, "COLPTN: 0x%x\n", vpfe_reg_read(ccdc, VPFE_COLPTN));
261 vpfe_dbg(3, vpfe, "SDOFST: 0x%x\n", vpfe_reg_read(ccdc, VPFE_SDOFST));
262 vpfe_dbg(3, vpfe, "SYN_MODE: 0x%x\n",
263 vpfe_reg_read(ccdc, VPFE_SYNMODE));
264 vpfe_dbg(3, vpfe, "HSIZE_OFF: 0x%x\n",
265 vpfe_reg_read(ccdc, VPFE_HSIZE_OFF));
266 vpfe_dbg(3, vpfe, "HORZ_INFO: 0x%x\n",
267 vpfe_reg_read(ccdc, VPFE_HORZ_INFO));
268 vpfe_dbg(3, vpfe, "VERT_START: 0x%x\n",
269 vpfe_reg_read(ccdc, VPFE_VERT_START));
270 vpfe_dbg(3, vpfe, "VERT_LINES: 0x%x\n",
271 vpfe_reg_read(ccdc, VPFE_VERT_LINES));
274 static int
275 vpfe_ccdc_validate_param(struct vpfe_ccdc *ccdc,
276 struct vpfe_ccdc_config_params_raw *ccdcparam)
278 struct vpfe_device *vpfe = to_vpfe(ccdc);
279 u8 max_gamma, max_data;
281 if (!ccdcparam->alaw.enable)
282 return 0;
284 max_gamma = ccdc_gamma_width_max_bit(ccdcparam->alaw.gamma_wd);
285 max_data = ccdc_data_size_max_bit(ccdcparam->data_sz);
287 if (ccdcparam->alaw.gamma_wd > VPFE_CCDC_GAMMA_BITS_09_0 ||
288 max_gamma > max_data) {
289 vpfe_dbg(1, vpfe, "Invalid data line select\n");
290 return -EINVAL;
293 return 0;
296 static void
297 vpfe_ccdc_update_raw_params(struct vpfe_ccdc *ccdc,
298 struct vpfe_ccdc_config_params_raw *raw_params)
300 struct vpfe_ccdc_config_params_raw *config_params =
301 &ccdc->ccdc_cfg.bayer.config_params;
303 *config_params = *raw_params;
307 * vpfe_ccdc_restore_defaults()
308 * This function will write defaults to all CCDC registers
310 static void vpfe_ccdc_restore_defaults(struct vpfe_ccdc *ccdc)
312 int i;
314 /* Disable CCDC */
315 vpfe_pcr_enable(ccdc, 0);
317 /* set all registers to default value */
318 for (i = 4; i <= 0x94; i += 4)
319 vpfe_reg_write(ccdc, 0, i);
321 vpfe_reg_write(ccdc, VPFE_NO_CULLING, VPFE_CULLING);
322 vpfe_reg_write(ccdc, VPFE_CCDC_GAMMA_BITS_11_2, VPFE_ALAW);
325 static int vpfe_ccdc_close(struct vpfe_ccdc *ccdc, struct device *dev)
327 struct vpfe_device *vpfe = container_of(ccdc, struct vpfe_device, ccdc);
328 u32 dma_cntl, pcr;
330 pcr = vpfe_reg_read(ccdc, VPFE_PCR);
331 if (pcr)
332 vpfe_dbg(1, vpfe, "VPFE_PCR is still set (%x)", pcr);
334 dma_cntl = vpfe_reg_read(ccdc, VPFE_DMA_CNTL);
335 if ((dma_cntl & VPFE_DMA_CNTL_OVERFLOW))
336 vpfe_dbg(1, vpfe, "VPFE_DMA_CNTL_OVERFLOW is still set (%x)",
337 dma_cntl);
339 /* Disable CCDC by resetting all register to default POR values */
340 vpfe_ccdc_restore_defaults(ccdc);
342 /* Disabled the module at the CONFIG level */
343 vpfe_config_enable(ccdc, 0);
345 pm_runtime_put_sync(dev);
346 return 0;
349 static int vpfe_ccdc_set_params(struct vpfe_ccdc *ccdc, void __user *params)
351 struct vpfe_device *vpfe = container_of(ccdc, struct vpfe_device, ccdc);
352 struct vpfe_ccdc_config_params_raw raw_params;
353 int x;
355 if (ccdc->ccdc_cfg.if_type != VPFE_RAW_BAYER)
356 return -EINVAL;
358 x = copy_from_user(&raw_params, params, sizeof(raw_params));
359 if (x) {
360 vpfe_dbg(1, vpfe,
361 "%s: error in copying ccdc params, %d\n",
362 __func__, x);
363 return -EFAULT;
366 if (!vpfe_ccdc_validate_param(ccdc, &raw_params)) {
367 vpfe_ccdc_update_raw_params(ccdc, &raw_params);
368 return 0;
371 return -EINVAL;
375 * vpfe_ccdc_config_ycbcr()
376 * This function will configure CCDC for YCbCr video capture
378 static void vpfe_ccdc_config_ycbcr(struct vpfe_ccdc *ccdc)
380 struct ccdc_params_ycbcr *params = &ccdc->ccdc_cfg.ycbcr;
381 u32 syn_mode;
384 * first restore the CCDC registers to default values
385 * This is important since we assume default values to be set in
386 * a lot of registers that we didn't touch
388 vpfe_ccdc_restore_defaults(ccdc);
391 * configure pixel format, frame format, configure video frame
392 * format, enable output to SDRAM, enable internal timing generator
393 * and 8bit pack mode
395 syn_mode = (((params->pix_fmt & VPFE_SYN_MODE_INPMOD_MASK) <<
396 VPFE_SYN_MODE_INPMOD_SHIFT) |
397 ((params->frm_fmt & VPFE_SYN_FLDMODE_MASK) <<
398 VPFE_SYN_FLDMODE_SHIFT) | VPFE_VDHDEN_ENABLE |
399 VPFE_WEN_ENABLE | VPFE_DATA_PACK_ENABLE);
401 /* setup BT.656 sync mode */
402 if (params->bt656_enable) {
403 vpfe_reg_write(ccdc, VPFE_REC656IF_BT656_EN, VPFE_REC656IF);
406 * configure the FID, VD, HD pin polarity,
407 * fld,hd pol positive, vd negative, 8-bit data
409 syn_mode |= VPFE_SYN_MODE_VD_POL_NEGATIVE;
410 if (ccdc->ccdc_cfg.if_type == VPFE_BT656_10BIT)
411 syn_mode |= VPFE_SYN_MODE_10BITS;
412 else
413 syn_mode |= VPFE_SYN_MODE_8BITS;
414 } else {
415 /* y/c external sync mode */
416 syn_mode |= (((params->fid_pol & VPFE_FID_POL_MASK) <<
417 VPFE_FID_POL_SHIFT) |
418 ((params->hd_pol & VPFE_HD_POL_MASK) <<
419 VPFE_HD_POL_SHIFT) |
420 ((params->vd_pol & VPFE_VD_POL_MASK) <<
421 VPFE_VD_POL_SHIFT));
423 vpfe_reg_write(ccdc, syn_mode, VPFE_SYNMODE);
425 /* configure video window */
426 vpfe_ccdc_setwin(ccdc, &params->win,
427 params->frm_fmt, params->bytesperpixel);
430 * configure the order of y cb cr in SDRAM, and disable latch
431 * internal register on vsync
433 if (ccdc->ccdc_cfg.if_type == VPFE_BT656_10BIT)
434 vpfe_reg_write(ccdc,
435 (params->pix_order << VPFE_CCDCFG_Y8POS_SHIFT) |
436 VPFE_LATCH_ON_VSYNC_DISABLE |
437 VPFE_CCDCFG_BW656_10BIT, VPFE_CCDCFG);
438 else
439 vpfe_reg_write(ccdc,
440 (params->pix_order << VPFE_CCDCFG_Y8POS_SHIFT) |
441 VPFE_LATCH_ON_VSYNC_DISABLE, VPFE_CCDCFG);
444 * configure the horizontal line offset. This should be a
445 * on 32 byte boundary. So clear LSB 5 bits
447 vpfe_reg_write(ccdc, params->bytesperline, VPFE_HSIZE_OFF);
449 /* configure the memory line offset */
450 if (params->buf_type == CCDC_BUFTYPE_FLD_INTERLEAVED)
451 /* two fields are interleaved in memory */
452 vpfe_reg_write(ccdc, VPFE_SDOFST_FIELD_INTERLEAVED,
453 VPFE_SDOFST);
456 static void
457 vpfe_ccdc_config_black_clamp(struct vpfe_ccdc *ccdc,
458 struct vpfe_ccdc_black_clamp *bclamp)
460 u32 val;
462 if (!bclamp->enable) {
463 /* configure DCSub */
464 val = (bclamp->dc_sub) & VPFE_BLK_DC_SUB_MASK;
465 vpfe_reg_write(ccdc, val, VPFE_DCSUB);
466 vpfe_reg_write(ccdc, VPFE_CLAMP_DEFAULT_VAL, VPFE_CLAMP);
467 return;
470 * Configure gain, Start pixel, No of line to be avg,
471 * No of pixel/line to be avg, & Enable the Black clamping
473 val = ((bclamp->sgain & VPFE_BLK_SGAIN_MASK) |
474 ((bclamp->start_pixel & VPFE_BLK_ST_PXL_MASK) <<
475 VPFE_BLK_ST_PXL_SHIFT) |
476 ((bclamp->sample_ln & VPFE_BLK_SAMPLE_LINE_MASK) <<
477 VPFE_BLK_SAMPLE_LINE_SHIFT) |
478 ((bclamp->sample_pixel & VPFE_BLK_SAMPLE_LN_MASK) <<
479 VPFE_BLK_SAMPLE_LN_SHIFT) | VPFE_BLK_CLAMP_ENABLE);
480 vpfe_reg_write(ccdc, val, VPFE_CLAMP);
481 /* If Black clamping is enable then make dcsub 0 */
482 vpfe_reg_write(ccdc, VPFE_DCSUB_DEFAULT_VAL, VPFE_DCSUB);
485 static void
486 vpfe_ccdc_config_black_compense(struct vpfe_ccdc *ccdc,
487 struct vpfe_ccdc_black_compensation *bcomp)
489 u32 val;
491 val = ((bcomp->b & VPFE_BLK_COMP_MASK) |
492 ((bcomp->gb & VPFE_BLK_COMP_MASK) <<
493 VPFE_BLK_COMP_GB_COMP_SHIFT) |
494 ((bcomp->gr & VPFE_BLK_COMP_MASK) <<
495 VPFE_BLK_COMP_GR_COMP_SHIFT) |
496 ((bcomp->r & VPFE_BLK_COMP_MASK) <<
497 VPFE_BLK_COMP_R_COMP_SHIFT));
498 vpfe_reg_write(ccdc, val, VPFE_BLKCMP);
502 * vpfe_ccdc_config_raw()
503 * This function will configure CCDC for Raw capture mode
505 static void vpfe_ccdc_config_raw(struct vpfe_ccdc *ccdc)
507 struct vpfe_device *vpfe = container_of(ccdc, struct vpfe_device, ccdc);
508 struct vpfe_ccdc_config_params_raw *config_params =
509 &ccdc->ccdc_cfg.bayer.config_params;
510 struct ccdc_params_raw *params = &ccdc->ccdc_cfg.bayer;
511 unsigned int syn_mode;
512 unsigned int val;
514 /* Reset CCDC */
515 vpfe_ccdc_restore_defaults(ccdc);
517 /* Disable latching function registers on VSYNC */
518 vpfe_reg_write(ccdc, VPFE_LATCH_ON_VSYNC_DISABLE, VPFE_CCDCFG);
521 * Configure the vertical sync polarity(SYN_MODE.VDPOL),
522 * horizontal sync polarity (SYN_MODE.HDPOL), frame id polarity
523 * (SYN_MODE.FLDPOL), frame format(progressive or interlace),
524 * data size(SYNMODE.DATSIZ), &pixel format (Input mode), output
525 * SDRAM, enable internal timing generator
527 syn_mode = (((params->vd_pol & VPFE_VD_POL_MASK) << VPFE_VD_POL_SHIFT) |
528 ((params->hd_pol & VPFE_HD_POL_MASK) << VPFE_HD_POL_SHIFT) |
529 ((params->fid_pol & VPFE_FID_POL_MASK) <<
530 VPFE_FID_POL_SHIFT) | ((params->frm_fmt &
531 VPFE_FRM_FMT_MASK) << VPFE_FRM_FMT_SHIFT) |
532 ((config_params->data_sz & VPFE_DATA_SZ_MASK) <<
533 VPFE_DATA_SZ_SHIFT) | ((params->pix_fmt &
534 VPFE_PIX_FMT_MASK) << VPFE_PIX_FMT_SHIFT) |
535 VPFE_WEN_ENABLE | VPFE_VDHDEN_ENABLE);
537 /* Enable and configure aLaw register if needed */
538 if (config_params->alaw.enable) {
539 val = ((config_params->alaw.gamma_wd &
540 VPFE_ALAW_GAMMA_WD_MASK) | VPFE_ALAW_ENABLE);
541 vpfe_reg_write(ccdc, val, VPFE_ALAW);
542 vpfe_dbg(3, vpfe, "\nWriting 0x%x to ALAW...\n", val);
545 /* Configure video window */
546 vpfe_ccdc_setwin(ccdc, &params->win, params->frm_fmt,
547 params->bytesperpixel);
549 /* Configure Black Clamp */
550 vpfe_ccdc_config_black_clamp(ccdc, &config_params->blk_clamp);
552 /* Configure Black level compensation */
553 vpfe_ccdc_config_black_compense(ccdc, &config_params->blk_comp);
555 /* If data size is 8 bit then pack the data */
556 if ((config_params->data_sz == VPFE_CCDC_DATA_8BITS) ||
557 config_params->alaw.enable)
558 syn_mode |= VPFE_DATA_PACK_ENABLE;
561 * Configure Horizontal offset register. If pack 8 is enabled then
562 * 1 pixel will take 1 byte
564 vpfe_reg_write(ccdc, params->bytesperline, VPFE_HSIZE_OFF);
566 vpfe_dbg(3, vpfe, "Writing %d (%x) to HSIZE_OFF\n",
567 params->bytesperline, params->bytesperline);
569 /* Set value for SDOFST */
570 if (params->frm_fmt == CCDC_FRMFMT_INTERLACED) {
571 if (params->image_invert_enable) {
572 /* For interlace inverse mode */
573 vpfe_reg_write(ccdc, VPFE_INTERLACED_IMAGE_INVERT,
574 VPFE_SDOFST);
575 } else {
576 /* For interlace non inverse mode */
577 vpfe_reg_write(ccdc, VPFE_INTERLACED_NO_IMAGE_INVERT,
578 VPFE_SDOFST);
580 } else if (params->frm_fmt == CCDC_FRMFMT_PROGRESSIVE) {
581 vpfe_reg_write(ccdc, VPFE_PROGRESSIVE_NO_IMAGE_INVERT,
582 VPFE_SDOFST);
585 vpfe_reg_write(ccdc, syn_mode, VPFE_SYNMODE);
587 vpfe_reg_dump(ccdc);
590 static inline int
591 vpfe_ccdc_set_buftype(struct vpfe_ccdc *ccdc,
592 enum ccdc_buftype buf_type)
594 if (ccdc->ccdc_cfg.if_type == VPFE_RAW_BAYER)
595 ccdc->ccdc_cfg.bayer.buf_type = buf_type;
596 else
597 ccdc->ccdc_cfg.ycbcr.buf_type = buf_type;
599 return 0;
602 static inline enum ccdc_buftype vpfe_ccdc_get_buftype(struct vpfe_ccdc *ccdc)
604 if (ccdc->ccdc_cfg.if_type == VPFE_RAW_BAYER)
605 return ccdc->ccdc_cfg.bayer.buf_type;
607 return ccdc->ccdc_cfg.ycbcr.buf_type;
610 static int vpfe_ccdc_set_pixel_format(struct vpfe_ccdc *ccdc, u32 pixfmt)
612 struct vpfe_device *vpfe = container_of(ccdc, struct vpfe_device, ccdc);
614 vpfe_dbg(1, vpfe, "%s: if_type: %d, pixfmt:%s\n",
615 __func__, ccdc->ccdc_cfg.if_type, print_fourcc(pixfmt));
617 if (ccdc->ccdc_cfg.if_type == VPFE_RAW_BAYER) {
618 ccdc->ccdc_cfg.bayer.pix_fmt = CCDC_PIXFMT_RAW;
620 * Need to clear it in case it was left on
621 * after the last capture.
623 ccdc->ccdc_cfg.bayer.config_params.alaw.enable = 0;
625 switch (pixfmt) {
626 case V4L2_PIX_FMT_SBGGR8:
627 ccdc->ccdc_cfg.bayer.config_params.alaw.enable = 1;
628 break;
630 case V4L2_PIX_FMT_YUYV:
631 case V4L2_PIX_FMT_UYVY:
632 case V4L2_PIX_FMT_YUV420:
633 case V4L2_PIX_FMT_NV12:
634 case V4L2_PIX_FMT_RGB565X:
635 break;
637 case V4L2_PIX_FMT_SBGGR16:
638 default:
639 return -EINVAL;
641 } else {
642 switch (pixfmt) {
643 case V4L2_PIX_FMT_YUYV:
644 ccdc->ccdc_cfg.ycbcr.pix_order = CCDC_PIXORDER_YCBYCR;
645 break;
647 case V4L2_PIX_FMT_UYVY:
648 ccdc->ccdc_cfg.ycbcr.pix_order = CCDC_PIXORDER_CBYCRY;
649 break;
651 default:
652 return -EINVAL;
656 return 0;
659 static u32 vpfe_ccdc_get_pixel_format(struct vpfe_ccdc *ccdc)
661 u32 pixfmt;
663 if (ccdc->ccdc_cfg.if_type == VPFE_RAW_BAYER) {
664 pixfmt = V4L2_PIX_FMT_YUYV;
665 } else {
666 if (ccdc->ccdc_cfg.ycbcr.pix_order == CCDC_PIXORDER_YCBYCR)
667 pixfmt = V4L2_PIX_FMT_YUYV;
668 else
669 pixfmt = V4L2_PIX_FMT_UYVY;
672 return pixfmt;
675 static int
676 vpfe_ccdc_set_image_window(struct vpfe_ccdc *ccdc,
677 struct v4l2_rect *win, unsigned int bpp)
679 if (ccdc->ccdc_cfg.if_type == VPFE_RAW_BAYER) {
680 ccdc->ccdc_cfg.bayer.win = *win;
681 ccdc->ccdc_cfg.bayer.bytesperpixel = bpp;
682 ccdc->ccdc_cfg.bayer.bytesperline = ALIGN(win->width * bpp, 32);
683 } else {
684 ccdc->ccdc_cfg.ycbcr.win = *win;
685 ccdc->ccdc_cfg.ycbcr.bytesperpixel = bpp;
686 ccdc->ccdc_cfg.ycbcr.bytesperline = ALIGN(win->width * bpp, 32);
689 return 0;
692 static inline void
693 vpfe_ccdc_get_image_window(struct vpfe_ccdc *ccdc,
694 struct v4l2_rect *win)
696 if (ccdc->ccdc_cfg.if_type == VPFE_RAW_BAYER)
697 *win = ccdc->ccdc_cfg.bayer.win;
698 else
699 *win = ccdc->ccdc_cfg.ycbcr.win;
702 static inline unsigned int vpfe_ccdc_get_line_length(struct vpfe_ccdc *ccdc)
704 if (ccdc->ccdc_cfg.if_type == VPFE_RAW_BAYER)
705 return ccdc->ccdc_cfg.bayer.bytesperline;
707 return ccdc->ccdc_cfg.ycbcr.bytesperline;
710 static inline int
711 vpfe_ccdc_set_frame_format(struct vpfe_ccdc *ccdc,
712 enum ccdc_frmfmt frm_fmt)
714 if (ccdc->ccdc_cfg.if_type == VPFE_RAW_BAYER)
715 ccdc->ccdc_cfg.bayer.frm_fmt = frm_fmt;
716 else
717 ccdc->ccdc_cfg.ycbcr.frm_fmt = frm_fmt;
719 return 0;
722 static inline enum ccdc_frmfmt
723 vpfe_ccdc_get_frame_format(struct vpfe_ccdc *ccdc)
725 if (ccdc->ccdc_cfg.if_type == VPFE_RAW_BAYER)
726 return ccdc->ccdc_cfg.bayer.frm_fmt;
728 return ccdc->ccdc_cfg.ycbcr.frm_fmt;
731 static inline int vpfe_ccdc_getfid(struct vpfe_ccdc *ccdc)
733 return (vpfe_reg_read(ccdc, VPFE_SYNMODE) >> 15) & 1;
736 static inline void vpfe_set_sdr_addr(struct vpfe_ccdc *ccdc, unsigned long addr)
738 vpfe_reg_write(ccdc, addr & 0xffffffe0, VPFE_SDR_ADDR);
741 static int vpfe_ccdc_set_hw_if_params(struct vpfe_ccdc *ccdc,
742 struct vpfe_hw_if_param *params)
744 struct vpfe_device *vpfe = container_of(ccdc, struct vpfe_device, ccdc);
746 ccdc->ccdc_cfg.if_type = params->if_type;
748 switch (params->if_type) {
749 case VPFE_BT656:
750 case VPFE_YCBCR_SYNC_16:
751 case VPFE_YCBCR_SYNC_8:
752 case VPFE_BT656_10BIT:
753 ccdc->ccdc_cfg.ycbcr.vd_pol = params->vdpol;
754 ccdc->ccdc_cfg.ycbcr.hd_pol = params->hdpol;
755 break;
757 case VPFE_RAW_BAYER:
758 ccdc->ccdc_cfg.bayer.vd_pol = params->vdpol;
759 ccdc->ccdc_cfg.bayer.hd_pol = params->hdpol;
760 if (params->bus_width == 10)
761 ccdc->ccdc_cfg.bayer.config_params.data_sz =
762 VPFE_CCDC_DATA_10BITS;
763 else
764 ccdc->ccdc_cfg.bayer.config_params.data_sz =
765 VPFE_CCDC_DATA_8BITS;
766 vpfe_dbg(1, vpfe, "params.bus_width: %d\n",
767 params->bus_width);
768 vpfe_dbg(1, vpfe, "config_params.data_sz: %d\n",
769 ccdc->ccdc_cfg.bayer.config_params.data_sz);
770 break;
772 default:
773 return -EINVAL;
776 return 0;
779 static void vpfe_clear_intr(struct vpfe_ccdc *ccdc, int vdint)
781 unsigned int vpfe_int_status;
783 vpfe_int_status = vpfe_reg_read(ccdc, VPFE_IRQ_STS);
785 switch (vdint) {
786 /* VD0 interrupt */
787 case VPFE_VDINT0:
788 vpfe_int_status &= ~VPFE_VDINT0;
789 vpfe_int_status |= VPFE_VDINT0;
790 break;
792 /* VD1 interrupt */
793 case VPFE_VDINT1:
794 vpfe_int_status &= ~VPFE_VDINT1;
795 vpfe_int_status |= VPFE_VDINT1;
796 break;
798 /* VD2 interrupt */
799 case VPFE_VDINT2:
800 vpfe_int_status &= ~VPFE_VDINT2;
801 vpfe_int_status |= VPFE_VDINT2;
802 break;
804 /* Clear all interrupts */
805 default:
806 vpfe_int_status &= ~(VPFE_VDINT0 |
807 VPFE_VDINT1 |
808 VPFE_VDINT2);
809 vpfe_int_status |= (VPFE_VDINT0 |
810 VPFE_VDINT1 |
811 VPFE_VDINT2);
812 break;
814 /* Clear specific VDINT from the status register */
815 vpfe_reg_write(ccdc, vpfe_int_status, VPFE_IRQ_STS);
817 vpfe_int_status = vpfe_reg_read(ccdc, VPFE_IRQ_STS);
819 /* Acknowledge that we are done with all interrupts */
820 vpfe_reg_write(ccdc, 1, VPFE_IRQ_EOI);
823 static void vpfe_ccdc_config_defaults(struct vpfe_ccdc *ccdc)
825 ccdc->ccdc_cfg.if_type = VPFE_RAW_BAYER;
827 ccdc->ccdc_cfg.ycbcr.pix_fmt = CCDC_PIXFMT_YCBCR_8BIT;
828 ccdc->ccdc_cfg.ycbcr.frm_fmt = CCDC_FRMFMT_INTERLACED;
829 ccdc->ccdc_cfg.ycbcr.fid_pol = VPFE_PINPOL_POSITIVE;
830 ccdc->ccdc_cfg.ycbcr.vd_pol = VPFE_PINPOL_POSITIVE;
831 ccdc->ccdc_cfg.ycbcr.hd_pol = VPFE_PINPOL_POSITIVE;
832 ccdc->ccdc_cfg.ycbcr.pix_order = CCDC_PIXORDER_CBYCRY;
833 ccdc->ccdc_cfg.ycbcr.buf_type = CCDC_BUFTYPE_FLD_INTERLEAVED;
835 ccdc->ccdc_cfg.ycbcr.win.left = 0;
836 ccdc->ccdc_cfg.ycbcr.win.top = 0;
837 ccdc->ccdc_cfg.ycbcr.win.width = 720;
838 ccdc->ccdc_cfg.ycbcr.win.height = 576;
839 ccdc->ccdc_cfg.ycbcr.bt656_enable = 1;
841 ccdc->ccdc_cfg.bayer.pix_fmt = CCDC_PIXFMT_RAW;
842 ccdc->ccdc_cfg.bayer.frm_fmt = CCDC_FRMFMT_PROGRESSIVE;
843 ccdc->ccdc_cfg.bayer.fid_pol = VPFE_PINPOL_POSITIVE;
844 ccdc->ccdc_cfg.bayer.vd_pol = VPFE_PINPOL_POSITIVE;
845 ccdc->ccdc_cfg.bayer.hd_pol = VPFE_PINPOL_POSITIVE;
847 ccdc->ccdc_cfg.bayer.win.left = 0;
848 ccdc->ccdc_cfg.bayer.win.top = 0;
849 ccdc->ccdc_cfg.bayer.win.width = 800;
850 ccdc->ccdc_cfg.bayer.win.height = 600;
851 ccdc->ccdc_cfg.bayer.config_params.data_sz = VPFE_CCDC_DATA_8BITS;
852 ccdc->ccdc_cfg.bayer.config_params.alaw.gamma_wd =
853 VPFE_CCDC_GAMMA_BITS_09_0;
857 * vpfe_get_ccdc_image_format - Get image parameters based on CCDC settings
859 static int vpfe_get_ccdc_image_format(struct vpfe_device *vpfe,
860 struct v4l2_format *f)
862 struct v4l2_rect image_win;
863 enum ccdc_buftype buf_type;
864 enum ccdc_frmfmt frm_fmt;
866 memset(f, 0, sizeof(*f));
867 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
868 vpfe_ccdc_get_image_window(&vpfe->ccdc, &image_win);
869 f->fmt.pix.width = image_win.width;
870 f->fmt.pix.height = image_win.height;
871 f->fmt.pix.bytesperline = vpfe_ccdc_get_line_length(&vpfe->ccdc);
872 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
873 f->fmt.pix.height;
874 buf_type = vpfe_ccdc_get_buftype(&vpfe->ccdc);
875 f->fmt.pix.pixelformat = vpfe_ccdc_get_pixel_format(&vpfe->ccdc);
876 frm_fmt = vpfe_ccdc_get_frame_format(&vpfe->ccdc);
878 if (frm_fmt == CCDC_FRMFMT_PROGRESSIVE) {
879 f->fmt.pix.field = V4L2_FIELD_NONE;
880 } else if (frm_fmt == CCDC_FRMFMT_INTERLACED) {
881 if (buf_type == CCDC_BUFTYPE_FLD_INTERLEAVED) {
882 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
883 } else if (buf_type == CCDC_BUFTYPE_FLD_SEPARATED) {
884 f->fmt.pix.field = V4L2_FIELD_SEQ_TB;
885 } else {
886 vpfe_err(vpfe, "Invalid buf_type\n");
887 return -EINVAL;
889 } else {
890 vpfe_err(vpfe, "Invalid frm_fmt\n");
891 return -EINVAL;
893 return 0;
896 static int vpfe_config_ccdc_image_format(struct vpfe_device *vpfe)
898 enum ccdc_frmfmt frm_fmt = CCDC_FRMFMT_INTERLACED;
899 u32 bpp;
900 int ret = 0;
902 vpfe_dbg(1, vpfe, "pixelformat: %s\n",
903 print_fourcc(vpfe->fmt.fmt.pix.pixelformat));
905 if (vpfe_ccdc_set_pixel_format(&vpfe->ccdc,
906 vpfe->fmt.fmt.pix.pixelformat) < 0) {
907 vpfe_err(vpfe, "couldn't set pix format in ccdc\n");
908 return -EINVAL;
911 /* configure the image window */
912 bpp = __get_bytesperpixel(vpfe, vpfe->current_vpfe_fmt);
913 vpfe_ccdc_set_image_window(&vpfe->ccdc, &vpfe->crop, bpp);
915 switch (vpfe->fmt.fmt.pix.field) {
916 case V4L2_FIELD_INTERLACED:
917 /* do nothing, since it is default */
918 ret = vpfe_ccdc_set_buftype(
919 &vpfe->ccdc,
920 CCDC_BUFTYPE_FLD_INTERLEAVED);
921 break;
923 case V4L2_FIELD_NONE:
924 frm_fmt = CCDC_FRMFMT_PROGRESSIVE;
925 /* buffer type only applicable for interlaced scan */
926 break;
928 case V4L2_FIELD_SEQ_TB:
929 ret = vpfe_ccdc_set_buftype(
930 &vpfe->ccdc,
931 CCDC_BUFTYPE_FLD_SEPARATED);
932 break;
934 default:
935 return -EINVAL;
938 if (ret)
939 return ret;
941 return vpfe_ccdc_set_frame_format(&vpfe->ccdc, frm_fmt);
945 * vpfe_config_image_format()
946 * For a given standard, this functions sets up the default
947 * pix format & crop values in the vpfe device and ccdc. It first
948 * starts with defaults based values from the standard table.
949 * It then checks if sub device supports get_fmt and then override the
950 * values based on that.Sets crop values to match with scan resolution
951 * starting at 0,0. It calls vpfe_config_ccdc_image_format() set the
952 * values in ccdc
954 static int vpfe_config_image_format(struct vpfe_device *vpfe,
955 v4l2_std_id std_id)
957 struct vpfe_fmt *fmt;
958 struct v4l2_mbus_framefmt mbus_fmt;
959 int i, ret;
961 for (i = 0; i < ARRAY_SIZE(vpfe_standards); i++) {
962 if (vpfe_standards[i].std_id & std_id) {
963 vpfe->std_info.active_pixels =
964 vpfe_standards[i].width;
965 vpfe->std_info.active_lines =
966 vpfe_standards[i].height;
967 vpfe->std_info.frame_format =
968 vpfe_standards[i].frame_format;
969 vpfe->std_index = i;
971 break;
975 if (i == ARRAY_SIZE(vpfe_standards)) {
976 vpfe_err(vpfe, "standard not supported\n");
977 return -EINVAL;
980 ret = __subdev_get_format(vpfe, &mbus_fmt);
981 if (ret)
982 return ret;
984 fmt = find_format_by_code(vpfe, mbus_fmt.code);
985 if (!fmt) {
986 vpfe_dbg(3, vpfe, "mbus code format (0x%08x) not found.\n",
987 mbus_fmt.code);
988 return -EINVAL;
991 /* Save current subdev format */
992 v4l2_fill_pix_format(&vpfe->fmt.fmt.pix, &mbus_fmt);
993 vpfe->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
994 vpfe->fmt.fmt.pix.pixelformat = fmt->fourcc;
995 vpfe_calc_format_size(vpfe, fmt, &vpfe->fmt);
996 vpfe->current_vpfe_fmt = fmt;
998 /* Update the crop window based on found values */
999 vpfe->crop.top = 0;
1000 vpfe->crop.left = 0;
1001 vpfe->crop.width = mbus_fmt.width;
1002 vpfe->crop.height = mbus_fmt.height;
1004 return vpfe_config_ccdc_image_format(vpfe);
1007 static int vpfe_initialize_device(struct vpfe_device *vpfe)
1009 struct vpfe_subdev_info *sdinfo;
1010 int ret;
1012 sdinfo = &vpfe->cfg->sub_devs[0];
1013 sdinfo->sd = vpfe->sd[0];
1014 vpfe->current_input = 0;
1015 vpfe->std_index = 0;
1016 /* Configure the default format information */
1017 ret = vpfe_config_image_format(vpfe,
1018 vpfe_standards[vpfe->std_index].std_id);
1019 if (ret)
1020 return ret;
1022 pm_runtime_get_sync(vpfe->pdev);
1024 vpfe_config_enable(&vpfe->ccdc, 1);
1026 vpfe_ccdc_restore_defaults(&vpfe->ccdc);
1028 /* Clear all VPFE interrupts */
1029 vpfe_clear_intr(&vpfe->ccdc, -1);
1031 return ret;
1035 * vpfe_release : This function is based on the vb2_fop_release
1036 * helper function.
1037 * It has been augmented to handle module power management,
1038 * by disabling/enabling h/w module fcntl clock when necessary.
1040 static int vpfe_release(struct file *file)
1042 struct vpfe_device *vpfe = video_drvdata(file);
1043 bool fh_singular;
1044 int ret;
1046 mutex_lock(&vpfe->lock);
1048 /* Save the singular status before we call the clean-up helper */
1049 fh_singular = v4l2_fh_is_singular_file(file);
1051 /* the release helper will cleanup any on-going streaming */
1052 ret = _vb2_fop_release(file, NULL);
1055 * If this was the last open file.
1056 * Then de-initialize hw module.
1058 if (fh_singular)
1059 vpfe_ccdc_close(&vpfe->ccdc, vpfe->pdev);
1061 mutex_unlock(&vpfe->lock);
1063 return ret;
1067 * vpfe_open : This function is based on the v4l2_fh_open helper function.
1068 * It has been augmented to handle module power management,
1069 * by disabling/enabling h/w module fcntl clock when necessary.
1071 static int vpfe_open(struct file *file)
1073 struct vpfe_device *vpfe = video_drvdata(file);
1074 int ret;
1076 mutex_lock(&vpfe->lock);
1078 ret = v4l2_fh_open(file);
1079 if (ret) {
1080 vpfe_err(vpfe, "v4l2_fh_open failed\n");
1081 goto unlock;
1084 if (!v4l2_fh_is_singular_file(file))
1085 goto unlock;
1087 if (vpfe_initialize_device(vpfe)) {
1088 v4l2_fh_release(file);
1089 ret = -ENODEV;
1092 unlock:
1093 mutex_unlock(&vpfe->lock);
1094 return ret;
1098 * vpfe_schedule_next_buffer: set next buffer address for capture
1099 * @vpfe : ptr to vpfe device
1101 * This function will get next buffer from the dma queue and
1102 * set the buffer address in the vpfe register for capture.
1103 * the buffer is marked active
1105 static void vpfe_schedule_next_buffer(struct vpfe_device *vpfe)
1107 dma_addr_t addr;
1109 spin_lock(&vpfe->dma_queue_lock);
1110 if (list_empty(&vpfe->dma_queue)) {
1111 spin_unlock(&vpfe->dma_queue_lock);
1112 return;
1115 vpfe->next_frm = list_entry(vpfe->dma_queue.next,
1116 struct vpfe_cap_buffer, list);
1117 list_del(&vpfe->next_frm->list);
1118 spin_unlock(&vpfe->dma_queue_lock);
1120 addr = vb2_dma_contig_plane_dma_addr(&vpfe->next_frm->vb.vb2_buf, 0);
1121 vpfe_set_sdr_addr(&vpfe->ccdc, addr);
1124 static inline void vpfe_schedule_bottom_field(struct vpfe_device *vpfe)
1126 dma_addr_t addr;
1128 addr = vb2_dma_contig_plane_dma_addr(&vpfe->next_frm->vb.vb2_buf, 0) +
1129 vpfe->field_off;
1131 vpfe_set_sdr_addr(&vpfe->ccdc, addr);
1135 * vpfe_process_buffer_complete: process a completed buffer
1136 * @vpfe : ptr to vpfe device
1138 * This function time stamp the buffer and mark it as DONE. It also
1139 * wake up any process waiting on the QUEUE and set the next buffer
1140 * as current
1142 static inline void vpfe_process_buffer_complete(struct vpfe_device *vpfe)
1144 vpfe->cur_frm->vb.vb2_buf.timestamp = ktime_get_ns();
1145 vpfe->cur_frm->vb.field = vpfe->fmt.fmt.pix.field;
1146 vpfe->cur_frm->vb.sequence = vpfe->sequence++;
1147 vb2_buffer_done(&vpfe->cur_frm->vb.vb2_buf, VB2_BUF_STATE_DONE);
1148 vpfe->cur_frm = vpfe->next_frm;
1151 static void vpfe_handle_interlaced_irq(struct vpfe_device *vpfe,
1152 enum v4l2_field field)
1154 int fid;
1156 /* interlaced or TB capture check which field
1157 * we are in hardware
1159 fid = vpfe_ccdc_getfid(&vpfe->ccdc);
1161 /* switch the software maintained field id */
1162 vpfe->field ^= 1;
1163 if (fid == vpfe->field) {
1164 /* we are in-sync here,continue */
1165 if (fid == 0) {
1167 * One frame is just being captured. If the
1168 * next frame is available, release the
1169 * current frame and move on
1171 if (vpfe->cur_frm != vpfe->next_frm)
1172 vpfe_process_buffer_complete(vpfe);
1174 if (vpfe->stopping)
1175 return;
1178 * based on whether the two fields are stored
1179 * interleave or separately in memory,
1180 * reconfigure the CCDC memory address
1182 if (field == V4L2_FIELD_SEQ_TB)
1183 vpfe_schedule_bottom_field(vpfe);
1184 } else {
1186 * if one field is just being captured configure
1187 * the next frame get the next frame from the empty
1188 * queue if no frame is available hold on to the
1189 * current buffer
1191 if (vpfe->cur_frm == vpfe->next_frm)
1192 vpfe_schedule_next_buffer(vpfe);
1194 } else if (fid == 0) {
1196 * out of sync. Recover from any hardware out-of-sync.
1197 * May loose one frame
1199 vpfe->field = fid;
1204 * vpfe_isr : ISR handler for vpfe capture (VINT0)
1205 * @irq: irq number
1206 * @dev_id: dev_id ptr
1208 * It changes status of the captured buffer, takes next buffer from the queue
1209 * and sets its address in VPFE registers
1211 static irqreturn_t vpfe_isr(int irq, void *dev)
1213 struct vpfe_device *vpfe = (struct vpfe_device *)dev;
1214 enum v4l2_field field = vpfe->fmt.fmt.pix.field;
1215 int intr_status, stopping = vpfe->stopping;
1217 intr_status = vpfe_reg_read(&vpfe->ccdc, VPFE_IRQ_STS);
1219 if (intr_status & VPFE_VDINT0) {
1220 if (field == V4L2_FIELD_NONE) {
1221 if (vpfe->cur_frm != vpfe->next_frm)
1222 vpfe_process_buffer_complete(vpfe);
1223 } else {
1224 vpfe_handle_interlaced_irq(vpfe, field);
1226 if (stopping) {
1227 vpfe->stopping = false;
1228 complete(&vpfe->capture_stop);
1232 if (intr_status & VPFE_VDINT1 && !stopping) {
1233 if (field == V4L2_FIELD_NONE &&
1234 vpfe->cur_frm == vpfe->next_frm)
1235 vpfe_schedule_next_buffer(vpfe);
1238 vpfe_clear_intr(&vpfe->ccdc, intr_status);
1240 return IRQ_HANDLED;
1243 static inline void vpfe_detach_irq(struct vpfe_device *vpfe)
1245 unsigned int intr = VPFE_VDINT0;
1246 enum ccdc_frmfmt frame_format;
1248 frame_format = vpfe_ccdc_get_frame_format(&vpfe->ccdc);
1249 if (frame_format == CCDC_FRMFMT_PROGRESSIVE)
1250 intr |= VPFE_VDINT1;
1252 vpfe_reg_write(&vpfe->ccdc, intr, VPFE_IRQ_EN_CLR);
1255 static inline void vpfe_attach_irq(struct vpfe_device *vpfe)
1257 unsigned int intr = VPFE_VDINT0;
1258 enum ccdc_frmfmt frame_format;
1260 frame_format = vpfe_ccdc_get_frame_format(&vpfe->ccdc);
1261 if (frame_format == CCDC_FRMFMT_PROGRESSIVE)
1262 intr |= VPFE_VDINT1;
1264 vpfe_reg_write(&vpfe->ccdc, intr, VPFE_IRQ_EN_SET);
1267 static int vpfe_querycap(struct file *file, void *priv,
1268 struct v4l2_capability *cap)
1270 struct vpfe_device *vpfe = video_drvdata(file);
1272 strscpy(cap->driver, VPFE_MODULE_NAME, sizeof(cap->driver));
1273 strscpy(cap->card, "TI AM437x VPFE", sizeof(cap->card));
1274 snprintf(cap->bus_info, sizeof(cap->bus_info),
1275 "platform:%s", vpfe->v4l2_dev.name);
1276 return 0;
1279 /* get the format set at output pad of the adjacent subdev */
1280 static int __subdev_get_format(struct vpfe_device *vpfe,
1281 struct v4l2_mbus_framefmt *fmt)
1283 struct v4l2_subdev *sd = vpfe->current_subdev->sd;
1284 struct v4l2_subdev_format sd_fmt;
1285 struct v4l2_mbus_framefmt *mbus_fmt = &sd_fmt.format;
1286 int ret;
1288 sd_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1289 sd_fmt.pad = 0;
1291 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &sd_fmt);
1292 if (ret)
1293 return ret;
1295 *fmt = *mbus_fmt;
1297 vpfe_dbg(1, vpfe, "%s: %dx%d code:%04X\n", __func__,
1298 fmt->width, fmt->height, fmt->code);
1300 return 0;
1303 /* set the format at output pad of the adjacent subdev */
1304 static int __subdev_set_format(struct vpfe_device *vpfe,
1305 struct v4l2_mbus_framefmt *fmt)
1307 struct v4l2_subdev *sd = vpfe->current_subdev->sd;
1308 struct v4l2_subdev_format sd_fmt;
1309 struct v4l2_mbus_framefmt *mbus_fmt = &sd_fmt.format;
1310 int ret;
1312 sd_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1313 sd_fmt.pad = 0;
1314 *mbus_fmt = *fmt;
1316 ret = v4l2_subdev_call(sd, pad, set_fmt, NULL, &sd_fmt);
1317 if (ret)
1318 return ret;
1320 vpfe_dbg(1, vpfe, "%s %dx%d code:%04X\n", __func__,
1321 fmt->width, fmt->height, fmt->code);
1323 return 0;
1326 static int vpfe_calc_format_size(struct vpfe_device *vpfe,
1327 const struct vpfe_fmt *fmt,
1328 struct v4l2_format *f)
1330 u32 bpp;
1332 if (!fmt) {
1333 vpfe_dbg(3, vpfe, "No vpfe_fmt provided!\n");
1334 return -EINVAL;
1337 bpp = __get_bytesperpixel(vpfe, fmt);
1339 /* pitch should be 32 bytes aligned */
1340 f->fmt.pix.bytesperline = ALIGN(f->fmt.pix.width * bpp, 32);
1341 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline *
1342 f->fmt.pix.height;
1344 vpfe_dbg(3, vpfe, "%s: fourcc: %s size: %dx%d bpl:%d img_size:%d\n",
1345 __func__, print_fourcc(f->fmt.pix.pixelformat),
1346 f->fmt.pix.width, f->fmt.pix.height,
1347 f->fmt.pix.bytesperline, f->fmt.pix.sizeimage);
1349 return 0;
1352 static int vpfe_g_fmt(struct file *file, void *priv,
1353 struct v4l2_format *fmt)
1355 struct vpfe_device *vpfe = video_drvdata(file);
1357 *fmt = vpfe->fmt;
1359 return 0;
1362 static int vpfe_enum_fmt(struct file *file, void *priv,
1363 struct v4l2_fmtdesc *f)
1365 struct vpfe_device *vpfe = video_drvdata(file);
1366 struct vpfe_subdev_info *sdinfo;
1367 struct vpfe_fmt *fmt;
1369 sdinfo = vpfe->current_subdev;
1370 if (!sdinfo->sd)
1371 return -EINVAL;
1373 if (f->index >= vpfe->num_active_fmt)
1374 return -EINVAL;
1376 fmt = vpfe->active_fmt[f->index];
1378 f->pixelformat = fmt->fourcc;
1380 vpfe_dbg(1, vpfe, "%s: mbus index: %d code: %x pixelformat: %s\n",
1381 __func__, f->index, fmt->code, print_fourcc(fmt->fourcc));
1383 return 0;
1386 static int vpfe_try_fmt(struct file *file, void *priv,
1387 struct v4l2_format *f)
1389 struct vpfe_device *vpfe = video_drvdata(file);
1390 struct v4l2_subdev *sd = vpfe->current_subdev->sd;
1391 const struct vpfe_fmt *fmt;
1392 struct v4l2_subdev_frame_size_enum fse;
1393 int ret, found;
1395 fmt = find_format_by_pix(vpfe, f->fmt.pix.pixelformat);
1396 if (!fmt) {
1397 /* default to first entry */
1398 vpfe_dbg(3, vpfe, "Invalid pixel code: %x, default used instead\n",
1399 f->fmt.pix.pixelformat);
1400 fmt = vpfe->active_fmt[0];
1401 f->fmt.pix.pixelformat = fmt->fourcc;
1404 f->fmt.pix.field = vpfe->fmt.fmt.pix.field;
1406 /* check for/find a valid width/height */
1407 ret = 0;
1408 found = false;
1409 fse.pad = 0;
1410 fse.code = fmt->code;
1411 fse.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1412 for (fse.index = 0; ; fse.index++) {
1413 ret = v4l2_subdev_call(sd, pad, enum_frame_size,
1414 NULL, &fse);
1415 if (ret)
1416 break;
1418 if (f->fmt.pix.width == fse.max_width &&
1419 f->fmt.pix.height == fse.max_height) {
1420 found = true;
1421 break;
1422 } else if (f->fmt.pix.width >= fse.min_width &&
1423 f->fmt.pix.width <= fse.max_width &&
1424 f->fmt.pix.height >= fse.min_height &&
1425 f->fmt.pix.height <= fse.max_height) {
1426 found = true;
1427 break;
1431 if (!found) {
1432 /* use existing values as default */
1433 f->fmt.pix.width = vpfe->fmt.fmt.pix.width;
1434 f->fmt.pix.height = vpfe->fmt.fmt.pix.height;
1438 * Use current colorspace for now, it will get
1439 * updated properly during s_fmt
1441 f->fmt.pix.colorspace = vpfe->fmt.fmt.pix.colorspace;
1442 return vpfe_calc_format_size(vpfe, fmt, f);
1445 static int vpfe_s_fmt(struct file *file, void *priv,
1446 struct v4l2_format *fmt)
1448 struct vpfe_device *vpfe = video_drvdata(file);
1449 struct vpfe_fmt *f;
1450 struct v4l2_mbus_framefmt mbus_fmt;
1451 int ret;
1453 /* If streaming is started, return error */
1454 if (vb2_is_busy(&vpfe->buffer_queue)) {
1455 vpfe_err(vpfe, "%s device busy\n", __func__);
1456 return -EBUSY;
1459 ret = vpfe_try_fmt(file, priv, fmt);
1460 if (ret < 0)
1461 return ret;
1463 f = find_format_by_pix(vpfe, fmt->fmt.pix.pixelformat);
1465 v4l2_fill_mbus_format(&mbus_fmt, &fmt->fmt.pix, f->code);
1467 ret = __subdev_set_format(vpfe, &mbus_fmt);
1468 if (ret)
1469 return ret;
1471 /* Just double check nothing has gone wrong */
1472 if (mbus_fmt.code != f->code) {
1473 vpfe_dbg(3, vpfe,
1474 "%s subdev changed format on us, this should not happen\n",
1475 __func__);
1476 return -EINVAL;
1479 v4l2_fill_pix_format(&vpfe->fmt.fmt.pix, &mbus_fmt);
1480 vpfe->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1481 vpfe->fmt.fmt.pix.pixelformat = f->fourcc;
1482 vpfe_calc_format_size(vpfe, f, &vpfe->fmt);
1483 *fmt = vpfe->fmt;
1484 vpfe->current_vpfe_fmt = f;
1486 /* Update the crop window based on found values */
1487 vpfe->crop.width = fmt->fmt.pix.width;
1488 vpfe->crop.height = fmt->fmt.pix.height;
1490 /* set image capture parameters in the ccdc */
1491 return vpfe_config_ccdc_image_format(vpfe);
1494 static int vpfe_enum_size(struct file *file, void *priv,
1495 struct v4l2_frmsizeenum *fsize)
1497 struct vpfe_device *vpfe = video_drvdata(file);
1498 struct v4l2_subdev_frame_size_enum fse;
1499 struct v4l2_subdev *sd = vpfe->current_subdev->sd;
1500 struct vpfe_fmt *fmt;
1501 int ret;
1503 /* check for valid format */
1504 fmt = find_format_by_pix(vpfe, fsize->pixel_format);
1505 if (!fmt) {
1506 vpfe_dbg(3, vpfe, "Invalid pixel code: %x\n",
1507 fsize->pixel_format);
1508 return -EINVAL;
1511 memset(fsize->reserved, 0x0, sizeof(fsize->reserved));
1513 memset(&fse, 0x0, sizeof(fse));
1514 fse.index = fsize->index;
1515 fse.pad = 0;
1516 fse.code = fmt->code;
1517 fse.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1518 ret = v4l2_subdev_call(sd, pad, enum_frame_size, NULL, &fse);
1519 if (ret)
1520 return ret;
1522 vpfe_dbg(1, vpfe, "%s: index: %d code: %x W:[%d,%d] H:[%d,%d]\n",
1523 __func__, fse.index, fse.code, fse.min_width, fse.max_width,
1524 fse.min_height, fse.max_height);
1526 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1527 fsize->discrete.width = fse.max_width;
1528 fsize->discrete.height = fse.max_height;
1530 vpfe_dbg(1, vpfe, "%s: index: %d pixformat: %s size: %dx%d\n",
1531 __func__, fsize->index, print_fourcc(fsize->pixel_format),
1532 fsize->discrete.width, fsize->discrete.height);
1534 return 0;
1538 * vpfe_get_subdev_input_index - Get subdev index and subdev input index for a
1539 * given app input index
1541 static int
1542 vpfe_get_subdev_input_index(struct vpfe_device *vpfe,
1543 int *subdev_index,
1544 int *subdev_input_index,
1545 int app_input_index)
1547 int i, j = 0;
1549 for (i = 0; i < ARRAY_SIZE(vpfe->cfg->asd); i++) {
1550 if (app_input_index < (j + 1)) {
1551 *subdev_index = i;
1552 *subdev_input_index = app_input_index - j;
1553 return 0;
1555 j++;
1557 return -EINVAL;
1561 * vpfe_get_app_input - Get app input index for a given subdev input index
1562 * driver stores the input index of the current sub device and translate it
1563 * when application request the current input
1565 static int vpfe_get_app_input_index(struct vpfe_device *vpfe,
1566 int *app_input_index)
1568 struct vpfe_config *cfg = vpfe->cfg;
1569 struct vpfe_subdev_info *sdinfo;
1570 struct i2c_client *client;
1571 struct i2c_client *curr_client;
1572 int i, j = 0;
1574 curr_client = v4l2_get_subdevdata(vpfe->current_subdev->sd);
1575 for (i = 0; i < ARRAY_SIZE(vpfe->cfg->asd); i++) {
1576 sdinfo = &cfg->sub_devs[i];
1577 client = v4l2_get_subdevdata(sdinfo->sd);
1578 if (client->addr == curr_client->addr &&
1579 client->adapter->nr == curr_client->adapter->nr) {
1580 if (vpfe->current_input >= 1)
1581 return -1;
1582 *app_input_index = j + vpfe->current_input;
1583 return 0;
1585 j++;
1587 return -EINVAL;
1590 static int vpfe_enum_input(struct file *file, void *priv,
1591 struct v4l2_input *inp)
1593 struct vpfe_device *vpfe = video_drvdata(file);
1594 struct vpfe_subdev_info *sdinfo;
1595 int subdev, index;
1597 if (vpfe_get_subdev_input_index(vpfe, &subdev, &index,
1598 inp->index) < 0) {
1599 vpfe_dbg(1, vpfe,
1600 "input information not found for the subdev\n");
1601 return -EINVAL;
1603 sdinfo = &vpfe->cfg->sub_devs[subdev];
1604 *inp = sdinfo->inputs[index];
1606 return 0;
1609 static int vpfe_g_input(struct file *file, void *priv, unsigned int *index)
1611 struct vpfe_device *vpfe = video_drvdata(file);
1613 return vpfe_get_app_input_index(vpfe, index);
1616 /* Assumes caller is holding vpfe_dev->lock */
1617 static int vpfe_set_input(struct vpfe_device *vpfe, unsigned int index)
1619 int subdev_index = 0, inp_index = 0;
1620 struct vpfe_subdev_info *sdinfo;
1621 struct vpfe_route *route;
1622 u32 input, output;
1623 int ret;
1625 /* If streaming is started, return error */
1626 if (vb2_is_busy(&vpfe->buffer_queue)) {
1627 vpfe_err(vpfe, "%s device busy\n", __func__);
1628 return -EBUSY;
1630 ret = vpfe_get_subdev_input_index(vpfe,
1631 &subdev_index,
1632 &inp_index,
1633 index);
1634 if (ret < 0) {
1635 vpfe_err(vpfe, "invalid input index: %d\n", index);
1636 goto get_out;
1639 sdinfo = &vpfe->cfg->sub_devs[subdev_index];
1640 sdinfo->sd = vpfe->sd[subdev_index];
1641 route = &sdinfo->routes[inp_index];
1642 if (route && sdinfo->can_route) {
1643 input = route->input;
1644 output = route->output;
1645 if (sdinfo->sd) {
1646 ret = v4l2_subdev_call(sdinfo->sd, video,
1647 s_routing, input, output, 0);
1648 if (ret) {
1649 vpfe_err(vpfe, "s_routing failed\n");
1650 ret = -EINVAL;
1651 goto get_out;
1657 vpfe->current_subdev = sdinfo;
1658 if (sdinfo->sd)
1659 vpfe->v4l2_dev.ctrl_handler = sdinfo->sd->ctrl_handler;
1660 vpfe->current_input = index;
1661 vpfe->std_index = 0;
1663 /* set the bus/interface parameter for the sub device in ccdc */
1664 ret = vpfe_ccdc_set_hw_if_params(&vpfe->ccdc, &sdinfo->vpfe_param);
1665 if (ret)
1666 return ret;
1668 /* set the default image parameters in the device */
1669 return vpfe_config_image_format(vpfe,
1670 vpfe_standards[vpfe->std_index].std_id);
1672 get_out:
1673 return ret;
1676 static int vpfe_s_input(struct file *file, void *priv, unsigned int index)
1678 struct vpfe_device *vpfe = video_drvdata(file);
1680 return vpfe_set_input(vpfe, index);
1683 static int vpfe_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
1685 struct vpfe_device *vpfe = video_drvdata(file);
1686 struct vpfe_subdev_info *sdinfo;
1688 sdinfo = vpfe->current_subdev;
1689 if (!(sdinfo->inputs[0].capabilities & V4L2_IN_CAP_STD))
1690 return -ENODATA;
1692 /* Call querystd function of decoder device */
1693 return v4l2_device_call_until_err(&vpfe->v4l2_dev, sdinfo->grp_id,
1694 video, querystd, std_id);
1697 static int vpfe_s_std(struct file *file, void *priv, v4l2_std_id std_id)
1699 struct vpfe_device *vpfe = video_drvdata(file);
1700 struct vpfe_subdev_info *sdinfo;
1701 int ret;
1703 sdinfo = vpfe->current_subdev;
1704 if (!(sdinfo->inputs[0].capabilities & V4L2_IN_CAP_STD))
1705 return -ENODATA;
1707 /* if trying to set the same std then nothing to do */
1708 if (vpfe_standards[vpfe->std_index].std_id == std_id)
1709 return 0;
1711 /* If streaming is started, return error */
1712 if (vb2_is_busy(&vpfe->buffer_queue)) {
1713 vpfe_err(vpfe, "%s device busy\n", __func__);
1714 ret = -EBUSY;
1715 return ret;
1718 ret = v4l2_device_call_until_err(&vpfe->v4l2_dev, sdinfo->grp_id,
1719 video, s_std, std_id);
1720 if (ret < 0) {
1721 vpfe_err(vpfe, "Failed to set standard\n");
1722 return ret;
1724 ret = vpfe_config_image_format(vpfe, std_id);
1726 return ret;
1729 static int vpfe_g_std(struct file *file, void *priv, v4l2_std_id *std_id)
1731 struct vpfe_device *vpfe = video_drvdata(file);
1732 struct vpfe_subdev_info *sdinfo;
1734 sdinfo = vpfe->current_subdev;
1735 if (sdinfo->inputs[0].capabilities != V4L2_IN_CAP_STD)
1736 return -ENODATA;
1738 *std_id = vpfe_standards[vpfe->std_index].std_id;
1740 return 0;
1744 * vpfe_calculate_offsets : This function calculates buffers offset
1745 * for top and bottom field
1747 static void vpfe_calculate_offsets(struct vpfe_device *vpfe)
1749 struct v4l2_rect image_win;
1751 vpfe_ccdc_get_image_window(&vpfe->ccdc, &image_win);
1752 vpfe->field_off = image_win.height * image_win.width;
1756 * vpfe_queue_setup - Callback function for buffer setup.
1757 * @vq: vb2_queue ptr
1758 * @nbuffers: ptr to number of buffers requested by application
1759 * @nplanes:: contains number of distinct video planes needed to hold a frame
1760 * @sizes[]: contains the size (in bytes) of each plane.
1761 * @alloc_devs: ptr to allocation context
1763 * This callback function is called when reqbuf() is called to adjust
1764 * the buffer count and buffer size
1766 static int vpfe_queue_setup(struct vb2_queue *vq,
1767 unsigned int *nbuffers, unsigned int *nplanes,
1768 unsigned int sizes[], struct device *alloc_devs[])
1770 struct vpfe_device *vpfe = vb2_get_drv_priv(vq);
1771 unsigned size = vpfe->fmt.fmt.pix.sizeimage;
1773 if (vq->num_buffers + *nbuffers < 3)
1774 *nbuffers = 3 - vq->num_buffers;
1776 if (*nplanes) {
1777 if (sizes[0] < size)
1778 return -EINVAL;
1779 size = sizes[0];
1782 *nplanes = 1;
1783 sizes[0] = size;
1785 vpfe_dbg(1, vpfe,
1786 "nbuffers=%d, size=%u\n", *nbuffers, sizes[0]);
1788 /* Calculate field offset */
1789 vpfe_calculate_offsets(vpfe);
1791 return 0;
1795 * vpfe_buffer_prepare : callback function for buffer prepare
1796 * @vb: ptr to vb2_buffer
1798 * This is the callback function for buffer prepare when vb2_qbuf()
1799 * function is called. The buffer is prepared and user space virtual address
1800 * or user address is converted into physical address
1802 static int vpfe_buffer_prepare(struct vb2_buffer *vb)
1804 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1805 struct vpfe_device *vpfe = vb2_get_drv_priv(vb->vb2_queue);
1807 vb2_set_plane_payload(vb, 0, vpfe->fmt.fmt.pix.sizeimage);
1809 if (vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
1810 return -EINVAL;
1812 vbuf->field = vpfe->fmt.fmt.pix.field;
1814 return 0;
1818 * vpfe_buffer_queue : Callback function to add buffer to DMA queue
1819 * @vb: ptr to vb2_buffer
1821 static void vpfe_buffer_queue(struct vb2_buffer *vb)
1823 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1824 struct vpfe_device *vpfe = vb2_get_drv_priv(vb->vb2_queue);
1825 struct vpfe_cap_buffer *buf = to_vpfe_buffer(vbuf);
1826 unsigned long flags = 0;
1828 /* add the buffer to the DMA queue */
1829 spin_lock_irqsave(&vpfe->dma_queue_lock, flags);
1830 list_add_tail(&buf->list, &vpfe->dma_queue);
1831 spin_unlock_irqrestore(&vpfe->dma_queue_lock, flags);
1834 static void vpfe_return_all_buffers(struct vpfe_device *vpfe,
1835 enum vb2_buffer_state state)
1837 struct vpfe_cap_buffer *buf, *node;
1838 unsigned long flags;
1840 spin_lock_irqsave(&vpfe->dma_queue_lock, flags);
1841 list_for_each_entry_safe(buf, node, &vpfe->dma_queue, list) {
1842 vb2_buffer_done(&buf->vb.vb2_buf, state);
1843 list_del(&buf->list);
1846 if (vpfe->cur_frm)
1847 vb2_buffer_done(&vpfe->cur_frm->vb.vb2_buf, state);
1849 if (vpfe->next_frm && vpfe->next_frm != vpfe->cur_frm)
1850 vb2_buffer_done(&vpfe->next_frm->vb.vb2_buf, state);
1852 vpfe->cur_frm = NULL;
1853 vpfe->next_frm = NULL;
1854 spin_unlock_irqrestore(&vpfe->dma_queue_lock, flags);
1858 * vpfe_start_streaming : Starts the DMA engine for streaming
1859 * @vb: ptr to vb2_buffer
1860 * @count: number of buffers
1862 static int vpfe_start_streaming(struct vb2_queue *vq, unsigned int count)
1864 struct vpfe_device *vpfe = vb2_get_drv_priv(vq);
1865 struct vpfe_subdev_info *sdinfo;
1866 unsigned long flags;
1867 unsigned long addr;
1868 int ret;
1870 spin_lock_irqsave(&vpfe->dma_queue_lock, flags);
1872 vpfe->field = 0;
1873 vpfe->sequence = 0;
1875 sdinfo = vpfe->current_subdev;
1877 vpfe_attach_irq(vpfe);
1879 vpfe->stopping = false;
1880 init_completion(&vpfe->capture_stop);
1882 if (vpfe->ccdc.ccdc_cfg.if_type == VPFE_RAW_BAYER)
1883 vpfe_ccdc_config_raw(&vpfe->ccdc);
1884 else
1885 vpfe_ccdc_config_ycbcr(&vpfe->ccdc);
1887 /* Get the next frame from the buffer queue */
1888 vpfe->next_frm = list_entry(vpfe->dma_queue.next,
1889 struct vpfe_cap_buffer, list);
1890 vpfe->cur_frm = vpfe->next_frm;
1891 /* Remove buffer from the buffer queue */
1892 list_del(&vpfe->cur_frm->list);
1893 spin_unlock_irqrestore(&vpfe->dma_queue_lock, flags);
1895 addr = vb2_dma_contig_plane_dma_addr(&vpfe->cur_frm->vb.vb2_buf, 0);
1897 vpfe_set_sdr_addr(&vpfe->ccdc, (unsigned long)(addr));
1899 vpfe_pcr_enable(&vpfe->ccdc, 1);
1901 ret = v4l2_subdev_call(sdinfo->sd, video, s_stream, 1);
1902 if (ret < 0) {
1903 vpfe_err(vpfe, "Error in attaching interrupt handle\n");
1904 goto err;
1907 return 0;
1909 err:
1910 vpfe_return_all_buffers(vpfe, VB2_BUF_STATE_QUEUED);
1911 vpfe_pcr_enable(&vpfe->ccdc, 0);
1912 return ret;
1916 * vpfe_stop_streaming : Stop the DMA engine
1917 * @vq: ptr to vb2_queue
1919 * This callback stops the DMA engine and any remaining buffers
1920 * in the DMA queue are released.
1922 static void vpfe_stop_streaming(struct vb2_queue *vq)
1924 struct vpfe_device *vpfe = vb2_get_drv_priv(vq);
1925 struct vpfe_subdev_info *sdinfo;
1926 int ret;
1928 vpfe_pcr_enable(&vpfe->ccdc, 0);
1930 /* Wait for the last frame to be captured */
1931 vpfe->stopping = true;
1932 wait_for_completion_timeout(&vpfe->capture_stop,
1933 msecs_to_jiffies(250));
1935 vpfe_detach_irq(vpfe);
1937 sdinfo = vpfe->current_subdev;
1938 ret = v4l2_subdev_call(sdinfo->sd, video, s_stream, 0);
1939 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
1940 vpfe_dbg(1, vpfe, "stream off failed in subdev\n");
1942 /* release all active buffers */
1943 vpfe_return_all_buffers(vpfe, VB2_BUF_STATE_ERROR);
1946 static int vpfe_g_pixelaspect(struct file *file, void *priv,
1947 int type, struct v4l2_fract *f)
1949 struct vpfe_device *vpfe = video_drvdata(file);
1951 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1952 vpfe->std_index >= ARRAY_SIZE(vpfe_standards))
1953 return -EINVAL;
1955 *f = vpfe_standards[vpfe->std_index].pixelaspect;
1957 return 0;
1960 static int
1961 vpfe_g_selection(struct file *file, void *fh, struct v4l2_selection *s)
1963 struct vpfe_device *vpfe = video_drvdata(file);
1965 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1966 vpfe->std_index >= ARRAY_SIZE(vpfe_standards))
1967 return -EINVAL;
1969 switch (s->target) {
1970 case V4L2_SEL_TGT_CROP_BOUNDS:
1971 case V4L2_SEL_TGT_CROP_DEFAULT:
1972 s->r.left = 0;
1973 s->r.top = 0;
1974 s->r.width = vpfe_standards[vpfe->std_index].width;
1975 s->r.height = vpfe_standards[vpfe->std_index].height;
1976 break;
1978 case V4L2_SEL_TGT_CROP:
1979 s->r = vpfe->crop;
1980 break;
1982 default:
1983 return -EINVAL;
1986 return 0;
1989 static int enclosed_rectangle(struct v4l2_rect *a, struct v4l2_rect *b)
1991 if (a->left < b->left || a->top < b->top)
1992 return 0;
1994 if (a->left + a->width > b->left + b->width)
1995 return 0;
1997 if (a->top + a->height > b->top + b->height)
1998 return 0;
2000 return 1;
2003 static int
2004 vpfe_s_selection(struct file *file, void *fh, struct v4l2_selection *s)
2006 struct vpfe_device *vpfe = video_drvdata(file);
2007 struct v4l2_rect cr = vpfe->crop;
2008 struct v4l2_rect r = s->r;
2009 u32 bpp;
2011 /* If streaming is started, return error */
2012 if (vb2_is_busy(&vpfe->buffer_queue)) {
2013 vpfe_err(vpfe, "%s device busy\n", __func__);
2014 return -EBUSY;
2017 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2018 s->target != V4L2_SEL_TGT_CROP)
2019 return -EINVAL;
2021 v4l_bound_align_image(&r.width, 0, cr.width, 0,
2022 &r.height, 0, cr.height, 0, 0);
2024 r.left = clamp_t(unsigned int, r.left, 0, cr.width - r.width);
2025 r.top = clamp_t(unsigned int, r.top, 0, cr.height - r.height);
2027 if (s->flags & V4L2_SEL_FLAG_LE && !enclosed_rectangle(&r, &s->r))
2028 return -ERANGE;
2030 if (s->flags & V4L2_SEL_FLAG_GE && !enclosed_rectangle(&s->r, &r))
2031 return -ERANGE;
2033 s->r = vpfe->crop = r;
2035 bpp = __get_bytesperpixel(vpfe, vpfe->current_vpfe_fmt);
2036 vpfe_ccdc_set_image_window(&vpfe->ccdc, &r, bpp);
2037 vpfe->fmt.fmt.pix.width = r.width;
2038 vpfe->fmt.fmt.pix.height = r.height;
2039 vpfe->fmt.fmt.pix.bytesperline =
2040 vpfe_ccdc_get_line_length(&vpfe->ccdc);
2041 vpfe->fmt.fmt.pix.sizeimage = vpfe->fmt.fmt.pix.bytesperline *
2042 vpfe->fmt.fmt.pix.height;
2044 vpfe_dbg(1, vpfe, "cropped (%d,%d)/%dx%d of %dx%d\n",
2045 r.left, r.top, r.width, r.height, cr.width, cr.height);
2047 return 0;
2050 static long vpfe_ioctl_default(struct file *file, void *priv,
2051 bool valid_prio, unsigned int cmd, void *param)
2053 struct vpfe_device *vpfe = video_drvdata(file);
2054 int ret;
2056 if (!valid_prio) {
2057 vpfe_err(vpfe, "%s device busy\n", __func__);
2058 return -EBUSY;
2061 /* If streaming is started, return error */
2062 if (vb2_is_busy(&vpfe->buffer_queue)) {
2063 vpfe_err(vpfe, "%s device busy\n", __func__);
2064 return -EBUSY;
2067 switch (cmd) {
2068 case VIDIOC_AM437X_CCDC_CFG:
2069 ret = vpfe_ccdc_set_params(&vpfe->ccdc, (void __user *)param);
2070 if (ret) {
2071 vpfe_dbg(2, vpfe,
2072 "Error setting parameters in CCDC\n");
2073 return ret;
2075 ret = vpfe_get_ccdc_image_format(vpfe,
2076 &vpfe->fmt);
2077 if (ret < 0) {
2078 vpfe_dbg(2, vpfe,
2079 "Invalid image format at CCDC\n");
2080 return ret;
2082 break;
2084 default:
2085 ret = -ENOTTY;
2086 break;
2089 return ret;
2092 static const struct vb2_ops vpfe_video_qops = {
2093 .wait_prepare = vb2_ops_wait_prepare,
2094 .wait_finish = vb2_ops_wait_finish,
2095 .queue_setup = vpfe_queue_setup,
2096 .buf_prepare = vpfe_buffer_prepare,
2097 .buf_queue = vpfe_buffer_queue,
2098 .start_streaming = vpfe_start_streaming,
2099 .stop_streaming = vpfe_stop_streaming,
2102 /* vpfe capture driver file operations */
2103 static const struct v4l2_file_operations vpfe_fops = {
2104 .owner = THIS_MODULE,
2105 .open = vpfe_open,
2106 .release = vpfe_release,
2107 .read = vb2_fop_read,
2108 .poll = vb2_fop_poll,
2109 .unlocked_ioctl = video_ioctl2,
2110 .mmap = vb2_fop_mmap,
2113 /* vpfe capture ioctl operations */
2114 static const struct v4l2_ioctl_ops vpfe_ioctl_ops = {
2115 .vidioc_querycap = vpfe_querycap,
2116 .vidioc_enum_fmt_vid_cap = vpfe_enum_fmt,
2117 .vidioc_g_fmt_vid_cap = vpfe_g_fmt,
2118 .vidioc_s_fmt_vid_cap = vpfe_s_fmt,
2119 .vidioc_try_fmt_vid_cap = vpfe_try_fmt,
2121 .vidioc_enum_framesizes = vpfe_enum_size,
2123 .vidioc_enum_input = vpfe_enum_input,
2124 .vidioc_g_input = vpfe_g_input,
2125 .vidioc_s_input = vpfe_s_input,
2127 .vidioc_querystd = vpfe_querystd,
2128 .vidioc_s_std = vpfe_s_std,
2129 .vidioc_g_std = vpfe_g_std,
2131 .vidioc_reqbufs = vb2_ioctl_reqbufs,
2132 .vidioc_create_bufs = vb2_ioctl_create_bufs,
2133 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
2134 .vidioc_querybuf = vb2_ioctl_querybuf,
2135 .vidioc_qbuf = vb2_ioctl_qbuf,
2136 .vidioc_dqbuf = vb2_ioctl_dqbuf,
2137 .vidioc_expbuf = vb2_ioctl_expbuf,
2138 .vidioc_streamon = vb2_ioctl_streamon,
2139 .vidioc_streamoff = vb2_ioctl_streamoff,
2141 .vidioc_log_status = v4l2_ctrl_log_status,
2142 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
2143 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
2145 .vidioc_g_pixelaspect = vpfe_g_pixelaspect,
2146 .vidioc_g_selection = vpfe_g_selection,
2147 .vidioc_s_selection = vpfe_s_selection,
2149 .vidioc_default = vpfe_ioctl_default,
2152 static int
2153 vpfe_async_bound(struct v4l2_async_notifier *notifier,
2154 struct v4l2_subdev *subdev,
2155 struct v4l2_async_subdev *asd)
2157 struct vpfe_device *vpfe = container_of(notifier->v4l2_dev,
2158 struct vpfe_device, v4l2_dev);
2159 struct v4l2_subdev_mbus_code_enum mbus_code;
2160 struct vpfe_subdev_info *sdinfo;
2161 struct vpfe_fmt *fmt;
2162 int ret = 0;
2163 bool found = false;
2164 int i, j, k;
2166 for (i = 0; i < ARRAY_SIZE(vpfe->cfg->asd); i++) {
2167 if (vpfe->cfg->asd[i]->match.fwnode ==
2168 asd[i].match.fwnode) {
2169 sdinfo = &vpfe->cfg->sub_devs[i];
2170 vpfe->sd[i] = subdev;
2171 vpfe->sd[i]->grp_id = sdinfo->grp_id;
2172 found = true;
2173 break;
2177 if (!found) {
2178 vpfe_info(vpfe, "sub device (%s) not matched\n", subdev->name);
2179 return -EINVAL;
2182 vpfe->video_dev.tvnorms |= sdinfo->inputs[0].std;
2184 vpfe->num_active_fmt = 0;
2185 for (j = 0, i = 0; (ret != -EINVAL); ++j) {
2186 memset(&mbus_code, 0, sizeof(mbus_code));
2187 mbus_code.index = j;
2188 mbus_code.which = V4L2_SUBDEV_FORMAT_ACTIVE;
2189 ret = v4l2_subdev_call(subdev, pad, enum_mbus_code,
2190 NULL, &mbus_code);
2191 if (ret)
2192 continue;
2194 vpfe_dbg(3, vpfe,
2195 "subdev %s: code: %04x idx: %d\n",
2196 subdev->name, mbus_code.code, j);
2198 for (k = 0; k < ARRAY_SIZE(formats); k++) {
2199 fmt = &formats[k];
2200 if (mbus_code.code != fmt->code)
2201 continue;
2202 vpfe->active_fmt[i] = fmt;
2203 vpfe_dbg(3, vpfe,
2204 "matched fourcc: %s code: %04x idx: %d\n",
2205 print_fourcc(fmt->fourcc), mbus_code.code, i);
2206 vpfe->num_active_fmt = ++i;
2210 if (!i) {
2211 vpfe_err(vpfe, "No suitable format reported by subdev %s\n",
2212 subdev->name);
2213 return -EINVAL;
2215 return 0;
2218 static int vpfe_probe_complete(struct vpfe_device *vpfe)
2220 struct video_device *vdev;
2221 struct vb2_queue *q;
2222 int err;
2224 spin_lock_init(&vpfe->dma_queue_lock);
2225 mutex_init(&vpfe->lock);
2227 vpfe->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2229 /* set first sub device as current one */
2230 vpfe->current_subdev = &vpfe->cfg->sub_devs[0];
2231 vpfe->v4l2_dev.ctrl_handler = vpfe->sd[0]->ctrl_handler;
2233 err = vpfe_set_input(vpfe, 0);
2234 if (err)
2235 goto probe_out;
2237 /* Initialize videobuf2 queue as per the buffer type */
2238 q = &vpfe->buffer_queue;
2239 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2240 q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
2241 q->drv_priv = vpfe;
2242 q->ops = &vpfe_video_qops;
2243 q->mem_ops = &vb2_dma_contig_memops;
2244 q->buf_struct_size = sizeof(struct vpfe_cap_buffer);
2245 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
2246 q->lock = &vpfe->lock;
2247 q->min_buffers_needed = 1;
2248 q->dev = vpfe->pdev;
2250 err = vb2_queue_init(q);
2251 if (err) {
2252 vpfe_err(vpfe, "vb2_queue_init() failed\n");
2253 goto probe_out;
2256 INIT_LIST_HEAD(&vpfe->dma_queue);
2258 vdev = &vpfe->video_dev;
2259 strscpy(vdev->name, VPFE_MODULE_NAME, sizeof(vdev->name));
2260 vdev->release = video_device_release_empty;
2261 vdev->fops = &vpfe_fops;
2262 vdev->ioctl_ops = &vpfe_ioctl_ops;
2263 vdev->v4l2_dev = &vpfe->v4l2_dev;
2264 vdev->vfl_dir = VFL_DIR_RX;
2265 vdev->queue = q;
2266 vdev->lock = &vpfe->lock;
2267 vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING |
2268 V4L2_CAP_READWRITE;
2269 video_set_drvdata(vdev, vpfe);
2270 err = video_register_device(&vpfe->video_dev, VFL_TYPE_GRABBER, -1);
2271 if (err) {
2272 vpfe_err(vpfe,
2273 "Unable to register video device.\n");
2274 goto probe_out;
2277 return 0;
2279 probe_out:
2280 v4l2_device_unregister(&vpfe->v4l2_dev);
2281 return err;
2284 static int vpfe_async_complete(struct v4l2_async_notifier *notifier)
2286 struct vpfe_device *vpfe = container_of(notifier->v4l2_dev,
2287 struct vpfe_device, v4l2_dev);
2289 return vpfe_probe_complete(vpfe);
2292 static const struct v4l2_async_notifier_operations vpfe_async_ops = {
2293 .bound = vpfe_async_bound,
2294 .complete = vpfe_async_complete,
2297 static struct vpfe_config *
2298 vpfe_get_pdata(struct vpfe_device *vpfe)
2300 struct device_node *endpoint = NULL;
2301 struct device *dev = vpfe->pdev;
2302 struct vpfe_subdev_info *sdinfo;
2303 struct vpfe_config *pdata;
2304 unsigned int flags;
2305 unsigned int i;
2306 int err;
2308 dev_dbg(dev, "vpfe_get_pdata\n");
2310 v4l2_async_notifier_init(&vpfe->notifier);
2312 if (!IS_ENABLED(CONFIG_OF) || !dev->of_node)
2313 return dev->platform_data;
2315 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
2316 if (!pdata)
2317 return NULL;
2319 for (i = 0; ; i++) {
2320 struct v4l2_fwnode_endpoint bus_cfg = { .bus_type = 0 };
2321 struct device_node *rem;
2323 endpoint = of_graph_get_next_endpoint(dev->of_node, endpoint);
2324 if (!endpoint)
2325 break;
2327 sdinfo = &pdata->sub_devs[i];
2328 sdinfo->grp_id = 0;
2330 /* we only support camera */
2331 sdinfo->inputs[0].index = i;
2332 strscpy(sdinfo->inputs[0].name, "Camera",
2333 sizeof(sdinfo->inputs[0].name));
2334 sdinfo->inputs[0].type = V4L2_INPUT_TYPE_CAMERA;
2335 sdinfo->inputs[0].std = V4L2_STD_ALL;
2336 sdinfo->inputs[0].capabilities = V4L2_IN_CAP_STD;
2338 sdinfo->can_route = 0;
2339 sdinfo->routes = NULL;
2341 of_property_read_u32(endpoint, "ti,am437x-vpfe-interface",
2342 &sdinfo->vpfe_param.if_type);
2343 if (sdinfo->vpfe_param.if_type < 0 ||
2344 sdinfo->vpfe_param.if_type > 4) {
2345 sdinfo->vpfe_param.if_type = VPFE_RAW_BAYER;
2348 err = v4l2_fwnode_endpoint_parse(of_fwnode_handle(endpoint),
2349 &bus_cfg);
2350 if (err) {
2351 dev_err(dev, "Could not parse the endpoint\n");
2352 goto cleanup;
2355 sdinfo->vpfe_param.bus_width = bus_cfg.bus.parallel.bus_width;
2357 if (sdinfo->vpfe_param.bus_width < 8 ||
2358 sdinfo->vpfe_param.bus_width > 16) {
2359 dev_err(dev, "Invalid bus width.\n");
2360 goto cleanup;
2363 flags = bus_cfg.bus.parallel.flags;
2365 if (flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
2366 sdinfo->vpfe_param.hdpol = 1;
2368 if (flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH)
2369 sdinfo->vpfe_param.vdpol = 1;
2371 rem = of_graph_get_remote_port_parent(endpoint);
2372 if (!rem) {
2373 dev_err(dev, "Remote device at %pOF not found\n",
2374 endpoint);
2375 goto cleanup;
2378 pdata->asd[i] = v4l2_async_notifier_add_fwnode_subdev(
2379 &vpfe->notifier, of_fwnode_handle(rem),
2380 sizeof(struct v4l2_async_subdev));
2381 of_node_put(rem);
2382 if (IS_ERR(pdata->asd[i]))
2383 goto cleanup;
2386 of_node_put(endpoint);
2387 return pdata;
2389 cleanup:
2390 v4l2_async_notifier_cleanup(&vpfe->notifier);
2391 of_node_put(endpoint);
2392 return NULL;
2396 * vpfe_probe : This function creates device entries by register
2397 * itself to the V4L2 driver and initializes fields of each
2398 * device objects
2400 static int vpfe_probe(struct platform_device *pdev)
2402 struct vpfe_config *vpfe_cfg;
2403 struct vpfe_device *vpfe;
2404 struct vpfe_ccdc *ccdc;
2405 struct resource *res;
2406 int ret;
2408 vpfe = devm_kzalloc(&pdev->dev, sizeof(*vpfe), GFP_KERNEL);
2409 if (!vpfe)
2410 return -ENOMEM;
2412 vpfe->pdev = &pdev->dev;
2414 vpfe_cfg = vpfe_get_pdata(vpfe);
2415 if (!vpfe_cfg) {
2416 dev_err(&pdev->dev, "No platform data\n");
2417 return -EINVAL;
2420 vpfe->cfg = vpfe_cfg;
2421 ccdc = &vpfe->ccdc;
2423 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2424 ccdc->ccdc_cfg.base_addr = devm_ioremap_resource(&pdev->dev, res);
2425 if (IS_ERR(ccdc->ccdc_cfg.base_addr)) {
2426 ret = PTR_ERR(ccdc->ccdc_cfg.base_addr);
2427 goto probe_out_cleanup;
2430 ret = platform_get_irq(pdev, 0);
2431 if (ret <= 0) {
2432 ret = -ENODEV;
2433 goto probe_out_cleanup;
2435 vpfe->irq = ret;
2437 ret = devm_request_irq(vpfe->pdev, vpfe->irq, vpfe_isr, 0,
2438 "vpfe_capture0", vpfe);
2439 if (ret) {
2440 dev_err(&pdev->dev, "Unable to request interrupt\n");
2441 ret = -EINVAL;
2442 goto probe_out_cleanup;
2445 ret = v4l2_device_register(&pdev->dev, &vpfe->v4l2_dev);
2446 if (ret) {
2447 vpfe_err(vpfe,
2448 "Unable to register v4l2 device.\n");
2449 goto probe_out_cleanup;
2452 /* set the driver data in platform device */
2453 platform_set_drvdata(pdev, vpfe);
2454 /* Enabling module functional clock */
2455 pm_runtime_enable(&pdev->dev);
2457 /* for now just enable it here instead of waiting for the open */
2458 pm_runtime_get_sync(&pdev->dev);
2460 vpfe_ccdc_config_defaults(ccdc);
2462 pm_runtime_put_sync(&pdev->dev);
2464 vpfe->sd = devm_kcalloc(&pdev->dev,
2465 ARRAY_SIZE(vpfe->cfg->asd),
2466 sizeof(struct v4l2_subdev *),
2467 GFP_KERNEL);
2468 if (!vpfe->sd) {
2469 ret = -ENOMEM;
2470 goto probe_out_v4l2_unregister;
2473 vpfe->notifier.ops = &vpfe_async_ops;
2474 ret = v4l2_async_notifier_register(&vpfe->v4l2_dev, &vpfe->notifier);
2475 if (ret) {
2476 vpfe_err(vpfe, "Error registering async notifier\n");
2477 ret = -EINVAL;
2478 goto probe_out_v4l2_unregister;
2481 return 0;
2483 probe_out_v4l2_unregister:
2484 v4l2_device_unregister(&vpfe->v4l2_dev);
2485 probe_out_cleanup:
2486 v4l2_async_notifier_cleanup(&vpfe->notifier);
2487 return ret;
2491 * vpfe_remove : It un-register device from V4L2 driver
2493 static int vpfe_remove(struct platform_device *pdev)
2495 struct vpfe_device *vpfe = platform_get_drvdata(pdev);
2497 pm_runtime_disable(&pdev->dev);
2499 v4l2_async_notifier_unregister(&vpfe->notifier);
2500 v4l2_async_notifier_cleanup(&vpfe->notifier);
2501 v4l2_device_unregister(&vpfe->v4l2_dev);
2502 video_unregister_device(&vpfe->video_dev);
2504 return 0;
2507 #ifdef CONFIG_PM_SLEEP
2509 static void vpfe_save_context(struct vpfe_ccdc *ccdc)
2511 ccdc->ccdc_ctx[VPFE_PCR >> 2] = vpfe_reg_read(ccdc, VPFE_PCR);
2512 ccdc->ccdc_ctx[VPFE_SYNMODE >> 2] = vpfe_reg_read(ccdc, VPFE_SYNMODE);
2513 ccdc->ccdc_ctx[VPFE_SDOFST >> 2] = vpfe_reg_read(ccdc, VPFE_SDOFST);
2514 ccdc->ccdc_ctx[VPFE_SDR_ADDR >> 2] = vpfe_reg_read(ccdc, VPFE_SDR_ADDR);
2515 ccdc->ccdc_ctx[VPFE_CLAMP >> 2] = vpfe_reg_read(ccdc, VPFE_CLAMP);
2516 ccdc->ccdc_ctx[VPFE_DCSUB >> 2] = vpfe_reg_read(ccdc, VPFE_DCSUB);
2517 ccdc->ccdc_ctx[VPFE_COLPTN >> 2] = vpfe_reg_read(ccdc, VPFE_COLPTN);
2518 ccdc->ccdc_ctx[VPFE_BLKCMP >> 2] = vpfe_reg_read(ccdc, VPFE_BLKCMP);
2519 ccdc->ccdc_ctx[VPFE_VDINT >> 2] = vpfe_reg_read(ccdc, VPFE_VDINT);
2520 ccdc->ccdc_ctx[VPFE_ALAW >> 2] = vpfe_reg_read(ccdc, VPFE_ALAW);
2521 ccdc->ccdc_ctx[VPFE_REC656IF >> 2] = vpfe_reg_read(ccdc, VPFE_REC656IF);
2522 ccdc->ccdc_ctx[VPFE_CCDCFG >> 2] = vpfe_reg_read(ccdc, VPFE_CCDCFG);
2523 ccdc->ccdc_ctx[VPFE_CULLING >> 2] = vpfe_reg_read(ccdc, VPFE_CULLING);
2524 ccdc->ccdc_ctx[VPFE_HD_VD_WID >> 2] = vpfe_reg_read(ccdc,
2525 VPFE_HD_VD_WID);
2526 ccdc->ccdc_ctx[VPFE_PIX_LINES >> 2] = vpfe_reg_read(ccdc,
2527 VPFE_PIX_LINES);
2528 ccdc->ccdc_ctx[VPFE_HORZ_INFO >> 2] = vpfe_reg_read(ccdc,
2529 VPFE_HORZ_INFO);
2530 ccdc->ccdc_ctx[VPFE_VERT_START >> 2] = vpfe_reg_read(ccdc,
2531 VPFE_VERT_START);
2532 ccdc->ccdc_ctx[VPFE_VERT_LINES >> 2] = vpfe_reg_read(ccdc,
2533 VPFE_VERT_LINES);
2534 ccdc->ccdc_ctx[VPFE_HSIZE_OFF >> 2] = vpfe_reg_read(ccdc,
2535 VPFE_HSIZE_OFF);
2538 static int vpfe_suspend(struct device *dev)
2540 struct vpfe_device *vpfe = dev_get_drvdata(dev);
2541 struct vpfe_ccdc *ccdc = &vpfe->ccdc;
2543 /* only do full suspend if streaming has started */
2544 if (vb2_start_streaming_called(&vpfe->buffer_queue)) {
2545 pm_runtime_get_sync(dev);
2546 vpfe_config_enable(ccdc, 1);
2548 /* Save VPFE context */
2549 vpfe_save_context(ccdc);
2551 /* Disable CCDC */
2552 vpfe_pcr_enable(ccdc, 0);
2553 vpfe_config_enable(ccdc, 0);
2555 /* Disable both master and slave clock */
2556 pm_runtime_put_sync(dev);
2559 /* Select sleep pin state */
2560 pinctrl_pm_select_sleep_state(dev);
2562 return 0;
2565 static void vpfe_restore_context(struct vpfe_ccdc *ccdc)
2567 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_SYNMODE >> 2], VPFE_SYNMODE);
2568 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_CULLING >> 2], VPFE_CULLING);
2569 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_SDOFST >> 2], VPFE_SDOFST);
2570 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_SDR_ADDR >> 2], VPFE_SDR_ADDR);
2571 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_CLAMP >> 2], VPFE_CLAMP);
2572 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_DCSUB >> 2], VPFE_DCSUB);
2573 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_COLPTN >> 2], VPFE_COLPTN);
2574 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_BLKCMP >> 2], VPFE_BLKCMP);
2575 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_VDINT >> 2], VPFE_VDINT);
2576 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_ALAW >> 2], VPFE_ALAW);
2577 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_REC656IF >> 2], VPFE_REC656IF);
2578 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_CCDCFG >> 2], VPFE_CCDCFG);
2579 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_PCR >> 2], VPFE_PCR);
2580 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_HD_VD_WID >> 2],
2581 VPFE_HD_VD_WID);
2582 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_PIX_LINES >> 2],
2583 VPFE_PIX_LINES);
2584 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_HORZ_INFO >> 2],
2585 VPFE_HORZ_INFO);
2586 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_VERT_START >> 2],
2587 VPFE_VERT_START);
2588 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_VERT_LINES >> 2],
2589 VPFE_VERT_LINES);
2590 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_HSIZE_OFF >> 2],
2591 VPFE_HSIZE_OFF);
2594 static int vpfe_resume(struct device *dev)
2596 struct vpfe_device *vpfe = dev_get_drvdata(dev);
2597 struct vpfe_ccdc *ccdc = &vpfe->ccdc;
2599 /* only do full resume if streaming has started */
2600 if (vb2_start_streaming_called(&vpfe->buffer_queue)) {
2601 /* Enable both master and slave clock */
2602 pm_runtime_get_sync(dev);
2603 vpfe_config_enable(ccdc, 1);
2605 /* Restore VPFE context */
2606 vpfe_restore_context(ccdc);
2608 vpfe_config_enable(ccdc, 0);
2609 pm_runtime_put_sync(dev);
2612 /* Select default pin state */
2613 pinctrl_pm_select_default_state(dev);
2615 return 0;
2618 #endif
2620 static SIMPLE_DEV_PM_OPS(vpfe_pm_ops, vpfe_suspend, vpfe_resume);
2622 static const struct of_device_id vpfe_of_match[] = {
2623 { .compatible = "ti,am437x-vpfe", },
2624 { /* sentinel */ },
2626 MODULE_DEVICE_TABLE(of, vpfe_of_match);
2628 static struct platform_driver vpfe_driver = {
2629 .probe = vpfe_probe,
2630 .remove = vpfe_remove,
2631 .driver = {
2632 .name = VPFE_MODULE_NAME,
2633 .pm = &vpfe_pm_ops,
2634 .of_match_table = of_match_ptr(vpfe_of_match),
2638 module_platform_driver(vpfe_driver);
2640 MODULE_AUTHOR("Texas Instruments");
2641 MODULE_DESCRIPTION("TI AM437x VPFE driver");
2642 MODULE_LICENSE("GPL");
2643 MODULE_VERSION(VPFE_VERSION);