Linux 4.16.11
[linux/fpc-iii.git] / drivers / media / platform / am437x / am437x-vpfe.c
blob601ae6487617f7a5890b39f11d80c1dbc00480a1
1 /*
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
20 * SOFTWARE.
23 #include <linux/delay.h>
24 #include <linux/err.h>
25 #include <linux/init.h>
26 #include <linux/interrupt.h>
27 #include <linux/io.h>
28 #include <linux/module.h>
29 #include <linux/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"
47 static int debug;
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 {
60 v4l2_std_id std_id;
61 unsigned int width;
62 unsigned int height;
63 struct v4l2_fract pixelaspect;
64 int frame_format;
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},
72 struct bus_format {
73 unsigned int width;
74 unsigned int bpp;
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
88 struct vpfe_fmt {
89 const char *name;
90 u32 fourcc;
91 u32 code;
92 struct bus_format l;
93 struct bus_format s;
94 bool supported;
95 u32 index;
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,
103 .l.width = 10,
104 .l.bpp = 4,
105 .s.width = 8,
106 .s.bpp = 2,
107 .supported = false,
108 }, {
109 .name = "YUV 4:2:2 packed, CbYCrY",
110 .fourcc = V4L2_PIX_FMT_UYVY,
111 .code = MEDIA_BUS_FMT_UYVY8_2X8,
112 .l.width = 10,
113 .l.bpp = 4,
114 .s.width = 8,
115 .s.bpp = 2,
116 .supported = false,
117 }, {
118 .name = "YUV 4:2:2 packed, YCrYCb",
119 .fourcc = V4L2_PIX_FMT_YVYU,
120 .code = MEDIA_BUS_FMT_YVYU8_2X8,
121 .l.width = 10,
122 .l.bpp = 4,
123 .s.width = 8,
124 .s.bpp = 2,
125 .supported = false,
126 }, {
127 .name = "YUV 4:2:2 packed, CrYCbY",
128 .fourcc = V4L2_PIX_FMT_VYUY,
129 .code = MEDIA_BUS_FMT_VYUY8_2X8,
130 .l.width = 10,
131 .l.bpp = 4,
132 .s.width = 8,
133 .s.bpp = 2,
134 .supported = false,
135 }, {
136 .name = "RAW8 BGGR",
137 .fourcc = V4L2_PIX_FMT_SBGGR8,
138 .code = MEDIA_BUS_FMT_SBGGR8_1X8,
139 .l.width = 10,
140 .l.bpp = 2,
141 .s.width = 8,
142 .s.bpp = 1,
143 .supported = false,
144 }, {
145 .name = "RAW8 GBRG",
146 .fourcc = V4L2_PIX_FMT_SGBRG8,
147 .code = MEDIA_BUS_FMT_SGBRG8_1X8,
148 .l.width = 10,
149 .l.bpp = 2,
150 .s.width = 8,
151 .s.bpp = 1,
152 .supported = false,
153 }, {
154 .name = "RAW8 GRBG",
155 .fourcc = V4L2_PIX_FMT_SGRBG8,
156 .code = MEDIA_BUS_FMT_SGRBG8_1X8,
157 .l.width = 10,
158 .l.bpp = 2,
159 .s.width = 8,
160 .s.bpp = 1,
161 .supported = false,
162 }, {
163 .name = "RAW8 RGGB",
164 .fourcc = V4L2_PIX_FMT_SRGGB8,
165 .code = MEDIA_BUS_FMT_SRGGB8_1X8,
166 .l.width = 10,
167 .l.bpp = 2,
168 .s.width = 8,
169 .s.bpp = 1,
170 .supported = false,
171 }, {
172 .name = "RGB565 (LE)",
173 .fourcc = V4L2_PIX_FMT_RGB565,
174 .code = MEDIA_BUS_FMT_RGB565_2X8_LE,
175 .l.width = 10,
176 .l.bpp = 4,
177 .s.width = 8,
178 .s.bpp = 2,
179 .supported = false,
180 }, {
181 .name = "RGB565 (BE)",
182 .fourcc = V4L2_PIX_FMT_RGB565X,
183 .code = MEDIA_BUS_FMT_RGB565_2X8_BE,
184 .l.width = 10,
185 .l.bpp = 4,
186 .s.width = 8,
187 .s.bpp = 2,
188 .supported = false,
192 static int
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;
199 unsigned int k;
201 for (k = 0; k < ARRAY_SIZE(formats); k++) {
202 fmt = &formats[k];
203 if (fmt->code == code)
204 return fmt;
207 return NULL;
210 static struct vpfe_fmt *find_format_by_pix(unsigned int pixelformat)
212 struct vpfe_fmt *fmt;
213 unsigned int k;
215 for (k = 0; k < ARRAY_SIZE(formats); k++) {
216 fmt = &formats[k];
217 if (fmt->fourcc == pixelformat)
218 return fmt;
221 return NULL;
224 static void
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");
236 *bpp = 1;
237 return;
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);
257 if (!fmt) {
258 /* default to first entry */
259 vpfe_dbg(3, vpfe, "Invalid pixel code: %x, default used instead\n",
260 pix_fmt->pixelformat);
261 fmt = &formats[0];
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)
271 static char code[5];
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);
277 code[4] = '\0';
279 return code;
282 static int
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);
311 static inline
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)
324 unsigned int cfg;
326 if (!flag) {
327 cfg = vpfe_reg_read(ccdc, VPFE_CONFIG);
328 cfg &= ~(VPFE_CONFIG_EN_ENABLE << VPFE_CONFIG_EN_SHIFT);
329 } else {
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,
339 int bpp)
341 int horz_start, horz_nr_pixels;
342 int vert_start, vert_nr_lines;
343 int val, mid_img;
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;
359 vert_start >>= 1;
360 /* Since first line doesn't have any data */
361 vert_start += 1;
362 /* configure VDINT0 */
363 val = (vert_start << VPFE_VDINT_VDINT0_SHIFT);
364 } else {
365 /* Since first line doesn't have any data */
366 vert_start += 1;
367 vert_nr_lines = image_win->height - 1;
369 * configure VDINT0 and VDINT1. VDINT1 will be at half
370 * of image height
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));
406 static int
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)
414 return 0;
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");
423 return -EINVAL;
426 return 0;
429 static void
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)
445 int i;
447 /* Disable 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);
466 if (!pcr)
467 break;
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))
482 break;
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);
494 return 0;
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;
501 int x;
503 if (ccdc->ccdc_cfg.if_type != VPFE_RAW_BAYER)
504 return -EINVAL;
506 x = copy_from_user(&raw_params, params, sizeof(raw_params));
507 if (x) {
508 vpfe_dbg(1, vpfe,
509 "vpfe_ccdc_set_params: error in copying ccdc params, %d\n",
511 return -EFAULT;
514 if (!vpfe_ccdc_validate_param(ccdc, &raw_params)) {
515 vpfe_ccdc_update_raw_params(ccdc, &raw_params);
516 return 0;
519 return -EINVAL;
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;
530 u32 syn_mode;
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
543 * and 8bit pack mode
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;
562 else
563 syn_mode |= VPFE_SYN_MODE_8BITS;
564 } else {
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) <<
569 VPFE_HD_POL_SHIFT) |
570 ((params->vd_pol & VPFE_VD_POL_MASK) <<
571 VPFE_VD_POL_SHIFT));
573 vpfe_reg_write(ccdc, syn_mode, VPFE_SYNMODE);
575 /* configure video window */
576 vpfe_ccdc_setwin(ccdc, &params->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)
584 vpfe_reg_write(ccdc,
585 (params->pix_order << VPFE_CCDCFG_Y8POS_SHIFT) |
586 VPFE_LATCH_ON_VSYNC_DISABLE |
587 VPFE_CCDCFG_BW656_10BIT, VPFE_CCDCFG);
588 else
589 vpfe_reg_write(ccdc,
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,
603 VPFE_SDOFST);
606 static void
607 vpfe_ccdc_config_black_clamp(struct vpfe_ccdc *ccdc,
608 struct vpfe_ccdc_black_clamp *bclamp)
610 u32 val;
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);
617 return;
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);
635 static void
636 vpfe_ccdc_config_black_compense(struct vpfe_ccdc *ccdc,
637 struct vpfe_ccdc_black_compensation *bcomp)
639 u32 val;
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;
662 unsigned int val;
664 vpfe_dbg(3, vpfe, "vpfe_ccdc_config_raw:\n");
666 /* Reset CCDC */
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, &params->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,
726 VPFE_SDOFST);
727 } else {
728 /* For interlace non inverse mode */
729 vpfe_reg_write(ccdc, VPFE_INTERLACED_NO_IMAGE_INVERT,
730 VPFE_SDOFST);
732 } else if (params->frm_fmt == CCDC_FRMFMT_PROGRESSIVE) {
733 vpfe_reg_write(ccdc, VPFE_PROGRESSIVE_NO_IMAGE_INVERT,
734 VPFE_SDOFST);
737 vpfe_reg_write(ccdc, syn_mode, VPFE_SYNMODE);
739 vpfe_reg_dump(ccdc);
742 static inline int
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;
748 else
749 ccdc->ccdc_cfg.ycbcr.buf_type = buf_type;
751 return 0;
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;
777 switch (pixfmt) {
778 case V4L2_PIX_FMT_SBGGR8:
779 ccdc->ccdc_cfg.bayer.config_params.alaw.enable = 1;
780 break;
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:
787 break;
789 case V4L2_PIX_FMT_SBGGR16:
790 default:
791 return -EINVAL;
793 } else {
794 switch (pixfmt) {
795 case V4L2_PIX_FMT_YUYV:
796 ccdc->ccdc_cfg.ycbcr.pix_order = CCDC_PIXORDER_YCBYCR;
797 break;
799 case V4L2_PIX_FMT_UYVY:
800 ccdc->ccdc_cfg.ycbcr.pix_order = CCDC_PIXORDER_CBYCRY;
801 break;
803 default:
804 return -EINVAL;
808 return 0;
811 static u32 vpfe_ccdc_get_pixel_format(struct vpfe_ccdc *ccdc)
813 u32 pixfmt;
815 if (ccdc->ccdc_cfg.if_type == VPFE_RAW_BAYER) {
816 pixfmt = V4L2_PIX_FMT_YUYV;
817 } else {
818 if (ccdc->ccdc_cfg.ycbcr.pix_order == CCDC_PIXORDER_YCBYCR)
819 pixfmt = V4L2_PIX_FMT_YUYV;
820 else
821 pixfmt = V4L2_PIX_FMT_UYVY;
824 return pixfmt;
827 static int
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);
835 } else {
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);
841 return 0;
844 static inline void
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;
850 else
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;
862 static inline int
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;
868 else
869 ccdc->ccdc_cfg.ycbcr.frm_fmt = frm_fmt;
871 return 0;
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) {
901 case VPFE_BT656:
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;
907 break;
909 case VPFE_RAW_BAYER:
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;
915 else
916 ccdc->ccdc_cfg.bayer.config_params.data_sz =
917 VPFE_CCDC_DATA_8BITS;
918 vpfe_dbg(1, vpfe, "params.bus_width: %d\n",
919 params->bus_width);
920 vpfe_dbg(1, vpfe, "config_params.data_sz: %d\n",
921 ccdc->ccdc_cfg.bayer.config_params.data_sz);
922 break;
924 default:
925 return -EINVAL;
928 return 0;
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);
937 switch (vdint) {
938 /* VD0 interrupt */
939 case VPFE_VDINT0:
940 vpfe_int_status &= ~VPFE_VDINT0;
941 vpfe_int_status |= VPFE_VDINT0;
942 break;
944 /* VD1 interrupt */
945 case VPFE_VDINT1:
946 vpfe_int_status &= ~VPFE_VDINT1;
947 vpfe_int_status |= VPFE_VDINT1;
948 break;
950 /* VD2 interrupt */
951 case VPFE_VDINT2:
952 vpfe_int_status &= ~VPFE_VDINT2;
953 vpfe_int_status |= VPFE_VDINT2;
954 break;
956 /* Clear all interrupts */
957 default:
958 vpfe_int_status &= ~(VPFE_VDINT0 |
959 VPFE_VDINT1 |
960 VPFE_VDINT2);
961 vpfe_int_status |= (VPFE_VDINT0 |
962 VPFE_VDINT1 |
963 VPFE_VDINT2);
964 break;
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 *
1025 f->fmt.pix.height;
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;
1037 } else {
1038 vpfe_err(vpfe, "Invalid buf_type\n");
1039 return -EINVAL;
1041 } else {
1042 vpfe_err(vpfe, "Invalid frm_fmt\n");
1043 return -EINVAL;
1045 return 0;
1048 static int vpfe_config_ccdc_image_format(struct vpfe_device *vpfe)
1050 enum ccdc_frmfmt frm_fmt = CCDC_FRMFMT_INTERLACED;
1051 int ret = 0;
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");
1061 return -EINVAL;
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(
1071 &vpfe->ccdc,
1072 CCDC_BUFTYPE_FLD_INTERLEAVED);
1073 break;
1075 case V4L2_FIELD_NONE:
1076 frm_fmt = CCDC_FRMFMT_PROGRESSIVE;
1077 /* buffer type only applicable for interlaced scan */
1078 break;
1080 case V4L2_FIELD_SEQ_TB:
1081 ret = vpfe_ccdc_set_buftype(
1082 &vpfe->ccdc,
1083 CCDC_BUFTYPE_FLD_SEPARATED);
1084 break;
1086 default:
1087 return -EINVAL;
1090 if (ret)
1091 return ret;
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
1104 * values in ccdc
1106 static int vpfe_config_image_format(struct vpfe_device *vpfe,
1107 v4l2_std_id std_id)
1109 struct v4l2_pix_format *pix = &vpfe->fmt.fmt.pix;
1110 int i, ret;
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;
1122 break;
1126 if (i == ARRAY_SIZE(vpfe_standards)) {
1127 vpfe_err(vpfe, "standard not supported\n");
1128 return -EINVAL;
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;
1141 else
1142 pix->field = V4L2_FIELD_NONE;
1144 ret = __vpfe_get_format(vpfe, &vpfe->fmt, &vpfe->bpp);
1145 if (ret)
1146 return ret;
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;
1158 int ret;
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);
1167 if (ret)
1168 return ret;
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);
1179 return ret;
1183 * vpfe_release : This function is based on the vb2_fop_release
1184 * helper function.
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);
1191 bool fh_singular;
1192 int ret;
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.
1206 if (fh_singular)
1207 vpfe_ccdc_close(&vpfe->ccdc, vpfe->pdev);
1209 mutex_unlock(&vpfe->lock);
1211 return ret;
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);
1222 int ret;
1224 mutex_lock(&vpfe->lock);
1226 ret = v4l2_fh_open(file);
1227 if (ret) {
1228 vpfe_err(vpfe, "v4l2_fh_open failed\n");
1229 goto unlock;
1232 if (!v4l2_fh_is_singular_file(file))
1233 goto unlock;
1235 if (vpfe_initialize_device(vpfe)) {
1236 v4l2_fh_release(file);
1237 ret = -ENODEV;
1240 unlock:
1241 mutex_unlock(&vpfe->lock);
1242 return ret;
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)
1267 unsigned long addr;
1269 addr = vb2_dma_contig_plane_dma_addr(&vpfe->next_frm->vb.vb2_buf, 0) +
1270 vpfe->field_off;
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
1281 * as current
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)
1294 * @irq: irq number
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;
1304 int intr_status;
1305 int fid;
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);
1316 goto next_intr;
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 */
1324 vpfe->field ^= 1;
1325 if (fid == vpfe->field) {
1326 /* we are in-sync here,continue */
1327 if (fid == 0) {
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);
1343 goto next_intr;
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
1349 * current buffer
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
1361 vpfe->field = fid;
1365 next_intr:
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);
1377 return IRQ_HANDLED;
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 |
1416 V4L2_CAP_READWRITE;
1417 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1419 return 0;
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;
1429 int ret;
1431 sdinfo = vpfe->current_subdev;
1432 if (!sdinfo->sd)
1433 return -EINVAL;
1435 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1436 fmt.pad = 0;
1438 ret = v4l2_subdev_call(sdinfo->sd, pad, get_fmt, NULL, &fmt);
1439 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
1440 return ret;
1442 if (!ret) {
1443 v4l2_fill_pix_format(&format->fmt.pix, &fmt.format);
1444 mbus_to_pix(vpfe, &fmt.format, &format->fmt.pix, bpp);
1445 } else {
1446 ret = v4l2_device_call_until_err(&vpfe->v4l2_dev,
1447 sdinfo->grp_id,
1448 pad, get_fmt,
1449 NULL, &fmt);
1450 if (ret && ret != -ENOIOCTLCMD && ret != -ENODEV)
1451 return ret;
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;
1458 vpfe_dbg(1, vpfe,
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);
1464 return 0;
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;
1473 int ret;
1475 vpfe_dbg(2, vpfe, "__vpfe_set_format\n");
1477 sdinfo = vpfe->current_subdev;
1478 if (!sdinfo->sd)
1479 return -EINVAL;
1481 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1482 fmt.pad = 0;
1484 pix_to_mbus(vpfe, &format->fmt.pix, &fmt.format);
1486 ret = v4l2_subdev_call(sdinfo->sd, pad, set_fmt, NULL, &fmt);
1487 if (ret)
1488 return ret;
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;
1495 vpfe_dbg(1, vpfe,
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);
1501 return 0;
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");
1511 *fmt = vpfe->fmt;
1513 return 0;
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;
1522 unsigned int k;
1524 vpfe_dbg(2, vpfe, "vpfe_enum_format index:%d\n",
1525 f->index);
1527 sdinfo = vpfe->current_subdev;
1528 if (!sdinfo->sd)
1529 return -EINVAL;
1531 if (f->index > ARRAY_SIZE(formats))
1532 return -EINVAL;
1534 for (k = 0; k < ARRAY_SIZE(formats); k++) {
1535 if (formats[k].index == f->index) {
1536 fmt = &formats[k];
1537 break;
1540 if (!fmt)
1541 return -EINVAL;
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);
1550 return 0;
1553 static int vpfe_try_fmt(struct file *file, void *priv,
1554 struct v4l2_format *fmt)
1556 struct vpfe_device *vpfe = video_drvdata(file);
1557 unsigned int bpp;
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;
1569 unsigned int bpp;
1570 int ret;
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__);
1577 return -EBUSY;
1580 ret = __vpfe_get_format(vpfe, &format, &bpp);
1581 if (ret)
1582 return ret;
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);
1590 if (ret)
1591 return ret;
1592 } else /* Just make sure all of the fields are consistent */
1593 *fmt = format;
1595 /* First detach any IRQ if currently attached */
1596 vpfe_detach_irq(vpfe);
1597 vpfe->fmt = *fmt;
1598 vpfe->bpp = bpp;
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;
1617 int ret;
1619 vpfe_dbg(2, vpfe, "vpfe_enum_size\n");
1621 /* check for valid format */
1622 fmt = find_format_by_pix(fsize->pixel_format);
1623 if (!fmt) {
1624 vpfe_dbg(3, vpfe, "Invalid pixel code: %x, default used instead\n",
1625 fsize->pixel_format);
1626 return -EINVAL;
1629 memset(fsize->reserved, 0x0, sizeof(fsize->reserved));
1631 sdinfo = vpfe->current_subdev;
1632 if (!sdinfo->sd)
1633 return -EINVAL;
1635 memset(&pix, 0x0, sizeof(pix));
1636 /* Construct pix from parameter and use default for the rest */
1637 pix.pixelformat = fsize->pixel_format;
1638 pix.width = 640;
1639 pix.height = 480;
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;
1646 fse.pad = 0;
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);
1650 if (ret)
1651 return -EINVAL;
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);
1665 return 0;
1669 * vpfe_get_subdev_input_index - Get subdev index and subdev input index for a
1670 * given app input index
1672 static int
1673 vpfe_get_subdev_input_index(struct vpfe_device *vpfe,
1674 int *subdev_index,
1675 int *subdev_input_index,
1676 int app_input_index)
1678 int i, j = 0;
1680 for (i = 0; i < ARRAY_SIZE(vpfe->cfg->asd); i++) {
1681 if (app_input_index < (j + 1)) {
1682 *subdev_index = i;
1683 *subdev_input_index = app_input_index - j;
1684 return 0;
1686 j++;
1688 return -EINVAL;
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;
1703 int i, j = 0;
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)
1712 return -1;
1713 *app_input_index = j + vpfe->current_input;
1714 return 0;
1716 j++;
1718 return -EINVAL;
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;
1726 int subdev, index;
1728 vpfe_dbg(2, vpfe, "vpfe_enum_input\n");
1730 if (vpfe_get_subdev_input_index(vpfe, &subdev, &index,
1731 inp->index) < 0) {
1732 vpfe_dbg(1, vpfe,
1733 "input information not found for the subdev\n");
1734 return -EINVAL;
1736 sdinfo = &vpfe->cfg->sub_devs[subdev];
1737 *inp = sdinfo->inputs[index];
1739 return 0;
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;
1757 u32 input, output;
1758 int ret;
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__);
1765 return -EBUSY;
1767 ret = vpfe_get_subdev_input_index(vpfe,
1768 &subdev_index,
1769 &inp_index,
1770 index);
1771 if (ret < 0) {
1772 vpfe_err(vpfe, "invalid input index: %d\n", index);
1773 goto get_out;
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;
1782 if (sdinfo->sd) {
1783 ret = v4l2_subdev_call(sdinfo->sd, video,
1784 s_routing, input, output, 0);
1785 if (ret) {
1786 vpfe_err(vpfe, "s_routing failed\n");
1787 ret = -EINVAL;
1788 goto get_out;
1794 vpfe->current_subdev = sdinfo;
1795 if (sdinfo->sd)
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);
1802 if (ret)
1803 return ret;
1805 /* set the default image parameters in the device */
1806 return vpfe_config_image_format(vpfe,
1807 vpfe_standards[vpfe->std_index].std_id);
1809 get_out:
1810 return ret;
1813 static int vpfe_s_input(struct file *file, void *priv, unsigned int index)
1815 struct vpfe_device *vpfe = video_drvdata(file);
1817 vpfe_dbg(2, vpfe,
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))
1832 return -ENODATA;
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;
1843 int ret;
1845 vpfe_dbg(2, vpfe, "vpfe_s_std\n");
1847 sdinfo = vpfe->current_subdev;
1848 if (!(sdinfo->inputs[0].capabilities & V4L2_IN_CAP_STD))
1849 return -ENODATA;
1851 /* If streaming is started, return error */
1852 if (vb2_is_busy(&vpfe->buffer_queue)) {
1853 vpfe_err(vpfe, "%s device busy\n", __func__);
1854 ret = -EBUSY;
1855 return ret;
1858 ret = v4l2_device_call_until_err(&vpfe->v4l2_dev, sdinfo->grp_id,
1859 video, s_std, std_id);
1860 if (ret < 0) {
1861 vpfe_err(vpfe, "Failed to set standard\n");
1862 return ret;
1864 ret = vpfe_config_image_format(vpfe, std_id);
1866 return ret;
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)
1878 return -ENODATA;
1880 *std_id = vpfe_standards[vpfe->std_index].std_id;
1882 return 0;
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;
1920 if (*nplanes) {
1921 if (sizes[0] < size)
1922 return -EINVAL;
1923 size = sizes[0];
1926 *nplanes = 1;
1927 sizes[0] = size;
1929 vpfe_dbg(1, vpfe,
1930 "nbuffers=%d, size=%u\n", *nbuffers, sizes[0]);
1932 /* Calculate field offset */
1933 vpfe_calculate_offsets(vpfe);
1935 return 0;
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))
1954 return -EINVAL;
1956 vbuf->field = vpfe->fmt.fmt.pix.field;
1958 return 0;
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;
1989 unsigned long addr;
1990 int ret;
1992 spin_lock_irqsave(&vpfe->dma_queue_lock, flags);
1994 vpfe->field = 0;
1995 vpfe->sequence = 0;
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);
2003 else
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);
2021 if (ret < 0) {
2022 vpfe_err(vpfe, "Error in attaching interrupt handle\n");
2023 goto err;
2026 return 0;
2028 err:
2029 list_for_each_entry_safe(buf, tmp, &vpfe->dma_queue, list) {
2030 list_del(&buf->list);
2031 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
2034 return ret;
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;
2049 int ret;
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);
2065 } else {
2066 if (vpfe->cur_frm != NULL)
2067 vb2_buffer_done(&vpfe->cur_frm->vb.vb2_buf,
2068 VB2_BUF_STATE_ERROR);
2069 if (vpfe->next_frm != NULL)
2070 vb2_buffer_done(&vpfe->next_frm->vb.vb2_buf,
2071 VB2_BUF_STATE_ERROR);
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))
2092 return -EINVAL;
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;
2103 return 0;
2106 static int
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;
2117 break;
2119 case V4L2_SEL_TGT_CROP:
2120 s->r = vpfe->crop;
2121 break;
2123 default:
2124 return -EINVAL;
2127 return 0;
2130 static int enclosed_rectangle(struct v4l2_rect *a, struct v4l2_rect *b)
2132 if (a->left < b->left || a->top < b->top)
2133 return 0;
2135 if (a->left + a->width > b->left + b->width)
2136 return 0;
2138 if (a->top + a->height > b->top + b->height)
2139 return 0;
2141 return 1;
2144 static int
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__);
2154 return -EBUSY;
2157 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2158 s->target != V4L2_SEL_TGT_CROP)
2159 return -EINVAL;
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))
2168 return -ERANGE;
2170 if (s->flags & V4L2_SEL_FLAG_GE && !enclosed_rectangle(&s->r, &r))
2171 return -ERANGE;
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);
2185 return 0;
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);
2192 int ret;
2194 vpfe_dbg(2, vpfe, "vpfe_ioctl_default\n");
2196 if (!valid_prio) {
2197 vpfe_err(vpfe, "%s device busy\n", __func__);
2198 return -EBUSY;
2201 /* If streaming is started, return error */
2202 if (vb2_is_busy(&vpfe->buffer_queue)) {
2203 vpfe_err(vpfe, "%s device busy\n", __func__);
2204 return -EBUSY;
2207 switch (cmd) {
2208 case VIDIOC_AM437X_CCDC_CFG:
2209 ret = vpfe_ccdc_set_params(&vpfe->ccdc, (void __user *)param);
2210 if (ret) {
2211 vpfe_dbg(2, vpfe,
2212 "Error setting parameters in CCDC\n");
2213 return ret;
2215 ret = vpfe_get_ccdc_image_format(vpfe,
2216 &vpfe->fmt);
2217 if (ret < 0) {
2218 vpfe_dbg(2, vpfe,
2219 "Invalid image format at CCDC\n");
2220 return ret;
2222 break;
2224 default:
2225 ret = -ENOTTY;
2226 break;
2229 return ret;
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,
2245 .open = vpfe_open,
2246 .release = vpfe_release,
2247 .read = vb2_fop_read,
2248 .poll = vb2_fop_poll,
2249 .unlocked_ioctl = video_ioctl2,
2250 .mmap = vb2_fop_mmap,
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,
2292 static int
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;
2301 bool found = false;
2302 int i, j;
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;
2312 found = true;
2313 break;
2317 if (!found) {
2318 vpfe_info(vpfe, "sub device (%s) not matched\n", subdev->name);
2319 return -EINVAL;
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;
2327 int ret;
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,
2333 NULL, &mbus_code);
2334 if (ret)
2335 break;
2337 fmt = find_format_by_code(mbus_code.code);
2338 if (!fmt)
2339 continue;
2341 fmt->supported = true;
2342 fmt->index = i++;
2345 return 0;
2348 static int vpfe_probe_complete(struct vpfe_device *vpfe)
2350 struct video_device *vdev;
2351 struct vb2_queue *q;
2352 int err;
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);
2364 if (err)
2365 goto probe_out;
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;
2371 q->drv_priv = vpfe;
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);
2381 if (err) {
2382 vpfe_err(vpfe, "vb2_queue_init() failed\n");
2383 goto probe_out;
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;
2395 vdev->queue = q;
2396 vdev->lock = &vpfe->lock;
2397 video_set_drvdata(vdev, vpfe);
2398 err = video_register_device(&vpfe->video_dev, VFL_TYPE_GRABBER, -1);
2399 if (err) {
2400 vpfe_err(vpfe,
2401 "Unable to register video device.\n");
2402 goto probe_out;
2405 return 0;
2407 probe_out:
2408 v4l2_device_unregister(&vpfe->v4l2_dev);
2409 return err;
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;
2432 unsigned int flags;
2433 unsigned int i;
2434 int err;
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);
2442 if (!pdata)
2443 return NULL;
2445 for (i = 0; ; i++) {
2446 struct device_node *rem;
2448 endpoint = of_graph_get_next_endpoint(pdev->dev.of_node,
2449 endpoint);
2450 if (!endpoint)
2451 break;
2453 sdinfo = &pdata->sub_devs[i];
2454 sdinfo->grp_id = 0;
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),
2474 &bus_cfg);
2475 if (err) {
2476 dev_err(&pdev->dev, "Could not parse the endpoint\n");
2477 goto done;
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");
2485 goto done;
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);
2497 if (!rem) {
2498 dev_err(&pdev->dev, "Remote device at %pOF not found\n",
2499 endpoint);
2500 goto done;
2503 pdata->asd[i] = devm_kzalloc(&pdev->dev,
2504 sizeof(struct v4l2_async_subdev),
2505 GFP_KERNEL);
2506 if (!pdata->asd[i]) {
2507 of_node_put(rem);
2508 pdata = NULL;
2509 goto done;
2512 pdata->asd[i]->match_type = V4L2_ASYNC_MATCH_FWNODE;
2513 pdata->asd[i]->match.fwnode = of_fwnode_handle(rem);
2514 of_node_put(rem);
2517 of_node_put(endpoint);
2518 return pdata;
2520 done:
2521 of_node_put(endpoint);
2522 return NULL;
2526 * vpfe_probe : This function creates device entries by register
2527 * itself to the V4L2 driver and initializes fields of each
2528 * device objects
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;
2536 int ret;
2538 if (!vpfe_cfg) {
2539 dev_err(&pdev->dev, "No platform data\n");
2540 return -EINVAL;
2543 vpfe = devm_kzalloc(&pdev->dev, sizeof(*vpfe), GFP_KERNEL);
2544 if (!vpfe)
2545 return -ENOMEM;
2547 vpfe->pdev = &pdev->dev;
2548 vpfe->cfg = vpfe_cfg;
2549 ccdc = &vpfe->ccdc;
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);
2557 if (ret <= 0) {
2558 dev_err(&pdev->dev, "No IRQ resource\n");
2559 return -ENODEV;
2561 vpfe->irq = ret;
2563 ret = devm_request_irq(vpfe->pdev, vpfe->irq, vpfe_isr, 0,
2564 "vpfe_capture0", vpfe);
2565 if (ret) {
2566 dev_err(&pdev->dev, "Unable to request interrupt\n");
2567 return -EINVAL;
2570 ret = v4l2_device_register(&pdev->dev, &vpfe->v4l2_dev);
2571 if (ret) {
2572 vpfe_err(vpfe,
2573 "Unable to register v4l2 device.\n");
2574 return ret;
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_kzalloc(&pdev->dev, sizeof(struct v4l2_subdev *) *
2590 ARRAY_SIZE(vpfe->cfg->asd), GFP_KERNEL);
2591 if (!vpfe->sd) {
2592 ret = -ENOMEM;
2593 goto probe_out_v4l2_unregister;
2596 vpfe->notifier.subdevs = vpfe->cfg->asd;
2597 vpfe->notifier.num_subdevs = ARRAY_SIZE(vpfe->cfg->asd);
2598 vpfe->notifier.ops = &vpfe_async_ops;
2599 ret = v4l2_async_notifier_register(&vpfe->v4l2_dev,
2600 &vpfe->notifier);
2601 if (ret) {
2602 vpfe_err(vpfe, "Error registering async notifier\n");
2603 ret = -EINVAL;
2604 goto probe_out_v4l2_unregister;
2607 return 0;
2609 probe_out_v4l2_unregister:
2610 v4l2_device_unregister(&vpfe->v4l2_dev);
2611 return ret;
2615 * vpfe_remove : It un-register device from V4L2 driver
2617 static int vpfe_remove(struct platform_device *pdev)
2619 struct vpfe_device *vpfe = platform_get_drvdata(pdev);
2621 vpfe_dbg(2, vpfe, "vpfe_remove\n");
2623 pm_runtime_disable(&pdev->dev);
2625 v4l2_async_notifier_unregister(&vpfe->notifier);
2626 v4l2_device_unregister(&vpfe->v4l2_dev);
2627 video_unregister_device(&vpfe->video_dev);
2629 return 0;
2632 #ifdef CONFIG_PM_SLEEP
2634 static void vpfe_save_context(struct vpfe_ccdc *ccdc)
2636 ccdc->ccdc_ctx[VPFE_PCR >> 2] = vpfe_reg_read(ccdc, VPFE_PCR);
2637 ccdc->ccdc_ctx[VPFE_SYNMODE >> 2] = vpfe_reg_read(ccdc, VPFE_SYNMODE);
2638 ccdc->ccdc_ctx[VPFE_SDOFST >> 2] = vpfe_reg_read(ccdc, VPFE_SDOFST);
2639 ccdc->ccdc_ctx[VPFE_SDR_ADDR >> 2] = vpfe_reg_read(ccdc, VPFE_SDR_ADDR);
2640 ccdc->ccdc_ctx[VPFE_CLAMP >> 2] = vpfe_reg_read(ccdc, VPFE_CLAMP);
2641 ccdc->ccdc_ctx[VPFE_DCSUB >> 2] = vpfe_reg_read(ccdc, VPFE_DCSUB);
2642 ccdc->ccdc_ctx[VPFE_COLPTN >> 2] = vpfe_reg_read(ccdc, VPFE_COLPTN);
2643 ccdc->ccdc_ctx[VPFE_BLKCMP >> 2] = vpfe_reg_read(ccdc, VPFE_BLKCMP);
2644 ccdc->ccdc_ctx[VPFE_VDINT >> 2] = vpfe_reg_read(ccdc, VPFE_VDINT);
2645 ccdc->ccdc_ctx[VPFE_ALAW >> 2] = vpfe_reg_read(ccdc, VPFE_ALAW);
2646 ccdc->ccdc_ctx[VPFE_REC656IF >> 2] = vpfe_reg_read(ccdc, VPFE_REC656IF);
2647 ccdc->ccdc_ctx[VPFE_CCDCFG >> 2] = vpfe_reg_read(ccdc, VPFE_CCDCFG);
2648 ccdc->ccdc_ctx[VPFE_CULLING >> 2] = vpfe_reg_read(ccdc, VPFE_CULLING);
2649 ccdc->ccdc_ctx[VPFE_HD_VD_WID >> 2] = vpfe_reg_read(ccdc,
2650 VPFE_HD_VD_WID);
2651 ccdc->ccdc_ctx[VPFE_PIX_LINES >> 2] = vpfe_reg_read(ccdc,
2652 VPFE_PIX_LINES);
2653 ccdc->ccdc_ctx[VPFE_HORZ_INFO >> 2] = vpfe_reg_read(ccdc,
2654 VPFE_HORZ_INFO);
2655 ccdc->ccdc_ctx[VPFE_VERT_START >> 2] = vpfe_reg_read(ccdc,
2656 VPFE_VERT_START);
2657 ccdc->ccdc_ctx[VPFE_VERT_LINES >> 2] = vpfe_reg_read(ccdc,
2658 VPFE_VERT_LINES);
2659 ccdc->ccdc_ctx[VPFE_HSIZE_OFF >> 2] = vpfe_reg_read(ccdc,
2660 VPFE_HSIZE_OFF);
2663 static int vpfe_suspend(struct device *dev)
2665 struct platform_device *pdev = to_platform_device(dev);
2666 struct vpfe_device *vpfe = platform_get_drvdata(pdev);
2667 struct vpfe_ccdc *ccdc = &vpfe->ccdc;
2669 /* if streaming has not started we don't care */
2670 if (!vb2_start_streaming_called(&vpfe->buffer_queue))
2671 return 0;
2673 pm_runtime_get_sync(dev);
2674 vpfe_config_enable(ccdc, 1);
2676 /* Save VPFE context */
2677 vpfe_save_context(ccdc);
2679 /* Disable CCDC */
2680 vpfe_pcr_enable(ccdc, 0);
2681 vpfe_config_enable(ccdc, 0);
2683 /* Disable both master and slave clock */
2684 pm_runtime_put_sync(dev);
2686 /* Select sleep pin state */
2687 pinctrl_pm_select_sleep_state(dev);
2689 return 0;
2692 static void vpfe_restore_context(struct vpfe_ccdc *ccdc)
2694 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_SYNMODE >> 2], VPFE_SYNMODE);
2695 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_CULLING >> 2], VPFE_CULLING);
2696 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_SDOFST >> 2], VPFE_SDOFST);
2697 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_SDR_ADDR >> 2], VPFE_SDR_ADDR);
2698 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_CLAMP >> 2], VPFE_CLAMP);
2699 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_DCSUB >> 2], VPFE_DCSUB);
2700 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_COLPTN >> 2], VPFE_COLPTN);
2701 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_BLKCMP >> 2], VPFE_BLKCMP);
2702 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_VDINT >> 2], VPFE_VDINT);
2703 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_ALAW >> 2], VPFE_ALAW);
2704 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_REC656IF >> 2], VPFE_REC656IF);
2705 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_CCDCFG >> 2], VPFE_CCDCFG);
2706 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_PCR >> 2], VPFE_PCR);
2707 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_HD_VD_WID >> 2],
2708 VPFE_HD_VD_WID);
2709 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_PIX_LINES >> 2],
2710 VPFE_PIX_LINES);
2711 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_HORZ_INFO >> 2],
2712 VPFE_HORZ_INFO);
2713 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_VERT_START >> 2],
2714 VPFE_VERT_START);
2715 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_VERT_LINES >> 2],
2716 VPFE_VERT_LINES);
2717 vpfe_reg_write(ccdc, ccdc->ccdc_ctx[VPFE_HSIZE_OFF >> 2],
2718 VPFE_HSIZE_OFF);
2721 static int vpfe_resume(struct device *dev)
2723 struct platform_device *pdev = to_platform_device(dev);
2724 struct vpfe_device *vpfe = platform_get_drvdata(pdev);
2725 struct vpfe_ccdc *ccdc = &vpfe->ccdc;
2727 /* if streaming has not started we don't care */
2728 if (!vb2_start_streaming_called(&vpfe->buffer_queue))
2729 return 0;
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);
2744 return 0;
2747 #endif
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", },
2753 { /* sentinel */ },
2755 MODULE_DEVICE_TABLE(of, vpfe_of_match);
2757 static struct platform_driver vpfe_driver = {
2758 .probe = vpfe_probe,
2759 .remove = vpfe_remove,
2760 .driver = {
2761 .name = VPFE_MODULE_NAME,
2762 .pm = &vpfe_pm_ops,
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);