2 * Copyright (C) 2005-2009 Texas Instruments Inc
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * CCDC hardware module for DM355
19 * ------------------------------
21 * This module is for configuring DM355 CCD controller of VPFE to capture
22 * Raw yuv or Bayer RGB data from a decoder. CCDC has several modules
23 * such as Defect Pixel Correction, Color Space Conversion etc to
24 * pre-process the Bayer RGB data, before writing it to SDRAM. This
25 * module also allows application to configure individual
26 * module parameters through VPFE_CMD_S_CCDC_RAW_PARAMS IOCTL.
27 * To do so, application include dm355_ccdc.h and vpfe_capture.h header
28 * files. The setparams() API is called by vpfe_capture driver
29 * to configure module parameters
31 * TODO: 1) Raw bayer parameter settings and bayer capture
32 * 2) Split module parameter structure to module specific ioctl structs
33 * 3) add support for lense shading correction
34 * 4) investigate if enum used for user space type definition
35 * to be replaced by #defines or integer
37 #include <linux/platform_device.h>
38 #include <linux/uaccess.h>
39 #include <linux/videodev2.h>
40 #include <media/davinci/dm355_ccdc.h>
41 #include <media/davinci/vpss.h>
42 #include "dm355_ccdc_regs.h"
43 #include "ccdc_hw_device.h"
45 MODULE_LICENSE("GPL");
46 MODULE_DESCRIPTION("CCDC Driver for DM355");
47 MODULE_AUTHOR("Texas Instruments");
49 static struct device
*dev
;
51 /* Object for CCDC raw mode */
52 static struct ccdc_params_raw ccdc_hw_params_raw
= {
53 .pix_fmt
= CCDC_PIXFMT_RAW
,
54 .frm_fmt
= CCDC_FRMFMT_PROGRESSIVE
,
56 .fid_pol
= VPFE_PINPOL_POSITIVE
,
57 .vd_pol
= VPFE_PINPOL_POSITIVE
,
58 .hd_pol
= VPFE_PINPOL_POSITIVE
,
67 .data_sz
= CCDC_DATA_10BITS
,
68 .mfilt1
= CCDC_NO_MEDIAN_FILTER1
,
69 .mfilt2
= CCDC_NO_MEDIAN_FILTER2
,
78 .olop
= CCDC_GREEN_BLUE
,
81 .elep
= CCDC_GREEN_RED
84 .olop
= CCDC_GREEN_BLUE
,
87 .elep
= CCDC_GREEN_RED
93 /* Object for CCDC ycbcr mode */
94 static struct ccdc_params_ycbcr ccdc_hw_params_ycbcr
= {
96 .pix_fmt
= CCDC_PIXFMT_YCBCR_8BIT
,
97 .frm_fmt
= CCDC_FRMFMT_INTERLACED
,
98 .fid_pol
= VPFE_PINPOL_POSITIVE
,
99 .vd_pol
= VPFE_PINPOL_POSITIVE
,
100 .hd_pol
= VPFE_PINPOL_POSITIVE
,
102 .pix_order
= CCDC_PIXORDER_CBYCRY
,
103 .buf_type
= CCDC_BUFTYPE_FLD_INTERLEAVED
106 static enum vpfe_hw_if_type ccdc_if_type
;
107 static void *__iomem ccdc_base_addr
;
108 static int ccdc_addr_size
;
110 /* Raw Bayer formats */
111 static u32 ccdc_raw_bayer_pix_formats
[] =
112 {V4L2_PIX_FMT_SBGGR8
, V4L2_PIX_FMT_SBGGR16
};
114 /* Raw YUV formats */
115 static u32 ccdc_raw_yuv_pix_formats
[] =
116 {V4L2_PIX_FMT_UYVY
, V4L2_PIX_FMT_YUYV
};
118 /* register access routines */
119 static inline u32
regr(u32 offset
)
121 return __raw_readl(ccdc_base_addr
+ offset
);
124 static inline void regw(u32 val
, u32 offset
)
126 __raw_writel(val
, ccdc_base_addr
+ offset
);
129 static void ccdc_set_ccdc_base(void *addr
, int size
)
131 ccdc_base_addr
= addr
;
132 ccdc_addr_size
= size
;
135 static void ccdc_enable(int en
)
139 temp
&= (~CCDC_SYNCEN_VDHDEN_MASK
);
140 temp
|= (en
& CCDC_SYNCEN_VDHDEN_MASK
);
144 static void ccdc_enable_output_to_sdram(int en
)
148 temp
&= (~(CCDC_SYNCEN_WEN_MASK
));
149 temp
|= ((en
<< CCDC_SYNCEN_WEN_SHIFT
) & CCDC_SYNCEN_WEN_MASK
);
153 static void ccdc_config_gain_offset(void)
156 regw(ccdc_hw_params_raw
.gain
.r_ye
, RYEGAIN
);
157 regw(ccdc_hw_params_raw
.gain
.gr_cy
, GRCYGAIN
);
158 regw(ccdc_hw_params_raw
.gain
.gb_g
, GBGGAIN
);
159 regw(ccdc_hw_params_raw
.gain
.b_mg
, BMGGAIN
);
160 /* configure offset */
161 regw(ccdc_hw_params_raw
.ccdc_offset
, OFFSET
);
165 * ccdc_restore_defaults()
166 * This function restore power on defaults in the ccdc registers
168 static int ccdc_restore_defaults(void)
172 dev_dbg(dev
, "\nstarting ccdc_restore_defaults...");
173 /* set all registers to zero */
174 for (i
= 0; i
<= CCDC_REG_LAST
; i
+= 4)
177 /* now override the values with power on defaults in registers */
178 regw(MODESET_DEFAULT
, MODESET
);
179 /* no culling support */
180 regw(CULH_DEFAULT
, CULH
);
181 regw(CULV_DEFAULT
, CULV
);
182 /* Set default Gain and Offset */
183 ccdc_hw_params_raw
.gain
.r_ye
= GAIN_DEFAULT
;
184 ccdc_hw_params_raw
.gain
.gb_g
= GAIN_DEFAULT
;
185 ccdc_hw_params_raw
.gain
.gr_cy
= GAIN_DEFAULT
;
186 ccdc_hw_params_raw
.gain
.b_mg
= GAIN_DEFAULT
;
187 ccdc_config_gain_offset();
188 regw(OUTCLIP_DEFAULT
, OUTCLIP
);
189 regw(LSCCFG2_DEFAULT
, LSCCFG2
);
190 /* select ccdc input */
191 if (vpss_select_ccdc_source(VPSS_CCDCIN
)) {
192 dev_dbg(dev
, "\ncouldn't select ccdc input source");
195 /* select ccdc clock */
196 if (vpss_enable_clock(VPSS_CCDC_CLOCK
, 1) < 0) {
197 dev_dbg(dev
, "\ncouldn't enable ccdc clock");
200 dev_dbg(dev
, "\nEnd of ccdc_restore_defaults...");
204 static int ccdc_open(struct device
*device
)
207 return ccdc_restore_defaults();
210 static int ccdc_close(struct device
*device
)
213 vpss_enable_clock(VPSS_CCDC_CLOCK
, 0);
214 /* do nothing for now */
219 * This function will configure the window size to
220 * be capture in CCDC reg.
222 static void ccdc_setwin(struct v4l2_rect
*image_win
,
223 enum ccdc_frmfmt frm_fmt
, int ppc
)
225 int horz_start
, horz_nr_pixels
;
226 int vert_start
, vert_nr_lines
;
229 dev_dbg(dev
, "\nStarting ccdc_setwin...");
232 * ppc - per pixel count. indicates how many pixels per cell
233 * output to SDRAM. example, for ycbcr, it is one y and one c, so 2.
234 * raw capture this is 1
236 horz_start
= image_win
->left
<< (ppc
- 1);
237 horz_nr_pixels
= ((image_win
->width
) << (ppc
- 1)) - 1;
239 /* Writing the horizontal info into the registers */
240 regw(horz_start
, SPH
);
241 regw(horz_nr_pixels
, NPH
);
242 vert_start
= image_win
->top
;
244 if (frm_fmt
== CCDC_FRMFMT_INTERLACED
) {
245 vert_nr_lines
= (image_win
->height
>> 1) - 1;
247 /* Since first line doesn't have any data */
249 /* configure VDINT0 and VDINT1 */
250 regw(vert_start
, VDINT0
);
252 /* Since first line doesn't have any data */
254 vert_nr_lines
= image_win
->height
- 1;
255 /* configure VDINT0 and VDINT1 */
256 mid_img
= vert_start
+ (image_win
->height
/ 2);
257 regw(vert_start
, VDINT0
);
258 regw(mid_img
, VDINT1
);
260 regw(vert_start
& CCDC_START_VER_ONE_MASK
, SLV0
);
261 regw(vert_start
& CCDC_START_VER_TWO_MASK
, SLV1
);
262 regw(vert_nr_lines
& CCDC_NUM_LINES_VER
, NLV
);
263 dev_dbg(dev
, "\nEnd of ccdc_setwin...");
266 static int validate_ccdc_param(struct ccdc_config_params_raw
*ccdcparam
)
268 if (ccdcparam
->datasft
< CCDC_DATA_NO_SHIFT
||
269 ccdcparam
->datasft
> CCDC_DATA_SHIFT_6BIT
) {
270 dev_dbg(dev
, "Invalid value of data shift\n");
274 if (ccdcparam
->mfilt1
< CCDC_NO_MEDIAN_FILTER1
||
275 ccdcparam
->mfilt1
> CCDC_MEDIAN_FILTER1
) {
276 dev_dbg(dev
, "Invalid value of median filter1\n");
280 if (ccdcparam
->mfilt2
< CCDC_NO_MEDIAN_FILTER2
||
281 ccdcparam
->mfilt2
> CCDC_MEDIAN_FILTER2
) {
282 dev_dbg(dev
, "Invalid value of median filter2\n");
286 if ((ccdcparam
->med_filt_thres
< 0) ||
287 (ccdcparam
->med_filt_thres
> CCDC_MED_FILT_THRESH
)) {
288 dev_dbg(dev
, "Invalid value of median filter thresold\n");
292 if (ccdcparam
->data_sz
< CCDC_DATA_16BITS
||
293 ccdcparam
->data_sz
> CCDC_DATA_8BITS
) {
294 dev_dbg(dev
, "Invalid value of data size\n");
298 if (ccdcparam
->alaw
.enable
) {
299 if (ccdcparam
->alaw
.gama_wd
< CCDC_GAMMA_BITS_13_4
||
300 ccdcparam
->alaw
.gama_wd
> CCDC_GAMMA_BITS_09_0
) {
301 dev_dbg(dev
, "Invalid value of ALAW\n");
306 if (ccdcparam
->blk_clamp
.b_clamp_enable
) {
307 if (ccdcparam
->blk_clamp
.sample_pixel
< CCDC_SAMPLE_1PIXELS
||
308 ccdcparam
->blk_clamp
.sample_pixel
> CCDC_SAMPLE_16PIXELS
) {
309 dev_dbg(dev
, "Invalid value of sample pixel\n");
312 if (ccdcparam
->blk_clamp
.sample_ln
< CCDC_SAMPLE_1LINES
||
313 ccdcparam
->blk_clamp
.sample_ln
> CCDC_SAMPLE_16LINES
) {
314 dev_dbg(dev
, "Invalid value of sample lines\n");
321 /* Parameter operations */
322 static int ccdc_set_params(void __user
*params
)
324 struct ccdc_config_params_raw ccdc_raw_params
;
327 /* only raw module parameters can be set through the IOCTL */
328 if (ccdc_if_type
!= VPFE_RAW_BAYER
)
331 x
= copy_from_user(&ccdc_raw_params
, params
, sizeof(ccdc_raw_params
));
333 dev_dbg(dev
, "ccdc_set_params: error in copying ccdc"
338 if (!validate_ccdc_param(&ccdc_raw_params
)) {
339 memcpy(&ccdc_hw_params_raw
.config_params
,
341 sizeof(ccdc_raw_params
));
347 /* This function will configure CCDC for YCbCr video capture */
348 static void ccdc_config_ycbcr(void)
350 struct ccdc_params_ycbcr
*params
= &ccdc_hw_params_ycbcr
;
353 /* first set the CCDC power on defaults values in all registers */
354 dev_dbg(dev
, "\nStarting ccdc_config_ycbcr...");
355 ccdc_restore_defaults();
357 /* configure pixel format & video frame format */
358 temp
= (((params
->pix_fmt
& CCDC_INPUT_MODE_MASK
) <<
359 CCDC_INPUT_MODE_SHIFT
) |
360 ((params
->frm_fmt
& CCDC_FRM_FMT_MASK
) <<
361 CCDC_FRM_FMT_SHIFT
));
363 /* setup BT.656 sync mode */
364 if (params
->bt656_enable
) {
365 regw(CCDC_REC656IF_BT656_EN
, REC656IF
);
367 * configure the FID, VD, HD pin polarity fld,hd pol positive,
368 * vd negative, 8-bit pack mode
370 temp
|= CCDC_VD_POL_NEGATIVE
;
371 } else { /* y/c external sync mode */
372 temp
|= (((params
->fid_pol
& CCDC_FID_POL_MASK
) <<
373 CCDC_FID_POL_SHIFT
) |
374 ((params
->hd_pol
& CCDC_HD_POL_MASK
) <<
376 ((params
->vd_pol
& CCDC_VD_POL_MASK
) <<
380 /* pack the data to 8-bit */
381 temp
|= CCDC_DATA_PACK_ENABLE
;
385 /* configure video window */
386 ccdc_setwin(¶ms
->win
, params
->frm_fmt
, 2);
388 /* configure the order of y cb cr in SD-RAM */
389 temp
= (params
->pix_order
<< CCDC_Y8POS_SHIFT
);
390 temp
|= CCDC_LATCH_ON_VSYNC_DISABLE
| CCDC_CCDCFG_FIDMD_NO_LATCH_VSYNC
;
394 * configure the horizontal line offset. This is done by rounding up
395 * width to a multiple of 16 pixels and multiply by two to account for
398 regw(((params
->win
.width
* 2 + 31) >> 5), HSIZE
);
400 /* configure the memory line offset */
401 if (params
->buf_type
== CCDC_BUFTYPE_FLD_INTERLEAVED
) {
402 /* two fields are interleaved in memory */
403 regw(CCDC_SDOFST_FIELD_INTERLEAVED
, SDOFST
);
406 dev_dbg(dev
, "\nEnd of ccdc_config_ycbcr...\n");
410 * ccdc_config_black_clamp()
411 * configure parameters for Optical Black Clamp
413 static void ccdc_config_black_clamp(struct ccdc_black_clamp
*bclamp
)
417 if (!bclamp
->b_clamp_enable
) {
418 /* configure DCSub */
419 regw(bclamp
->dc_sub
& CCDC_BLK_DC_SUB_MASK
, DCSUB
);
423 /* Enable the Black clamping, set sample lines and pixels */
424 val
= (bclamp
->start_pixel
& CCDC_BLK_ST_PXL_MASK
) |
425 ((bclamp
->sample_pixel
& CCDC_BLK_SAMPLE_LN_MASK
) <<
426 CCDC_BLK_SAMPLE_LN_SHIFT
) | CCDC_BLK_CLAMP_ENABLE
;
429 /* If Black clamping is enable then make dcsub 0 */
430 val
= (bclamp
->sample_ln
& CCDC_NUM_LINE_CALC_MASK
)
431 << CCDC_NUM_LINE_CALC_SHIFT
;
436 * ccdc_config_black_compense()
437 * configure parameters for Black Compensation
439 static void ccdc_config_black_compense(struct ccdc_black_compensation
*bcomp
)
443 val
= (bcomp
->b
& CCDC_BLK_COMP_MASK
) |
444 ((bcomp
->gb
& CCDC_BLK_COMP_MASK
) <<
445 CCDC_BLK_COMP_GB_COMP_SHIFT
);
448 val
= ((bcomp
->gr
& CCDC_BLK_COMP_MASK
) <<
449 CCDC_BLK_COMP_GR_COMP_SHIFT
) |
450 ((bcomp
->r
& CCDC_BLK_COMP_MASK
) <<
451 CCDC_BLK_COMP_R_COMP_SHIFT
);
456 * ccdc_write_dfc_entry()
457 * write an entry in the dfc table.
459 int ccdc_write_dfc_entry(int index
, struct ccdc_vertical_dft
*dfc
)
461 /* TODO This is to be re-visited and adjusted */
462 #define DFC_WRITE_WAIT_COUNT 1000
463 u32 val
, count
= DFC_WRITE_WAIT_COUNT
;
465 regw(dfc
->dft_corr_vert
[index
], DFCMEM0
);
466 regw(dfc
->dft_corr_horz
[index
], DFCMEM1
);
467 regw(dfc
->dft_corr_sub1
[index
], DFCMEM2
);
468 regw(dfc
->dft_corr_sub2
[index
], DFCMEM3
);
469 regw(dfc
->dft_corr_sub3
[index
], DFCMEM4
);
470 /* set WR bit to write */
471 val
= regr(DFCMEMCTL
) | CCDC_DFCMEMCTL_DFCMWR_MASK
;
472 regw(val
, DFCMEMCTL
);
475 * Assume, it is very short. If we get an error, we need to
478 while (regr(DFCMEMCTL
) & CCDC_DFCMEMCTL_DFCMWR_MASK
)
481 * TODO We expect the count to be non-zero to be successful. Adjust
482 * the count if write requires more time
486 dev_err(dev
, "defect table write timeout !!!\n");
494 * configure parameters for Vertical Defect Correction
496 static int ccdc_config_vdfc(struct ccdc_vertical_dft
*dfc
)
501 /* Configure General Defect Correction. The table used is from IPIPE */
502 val
= dfc
->gen_dft_en
& CCDC_DFCCTL_GDFCEN_MASK
;
504 /* Configure Vertical Defect Correction if needed */
505 if (!dfc
->ver_dft_en
) {
506 /* Enable only General Defect Correction */
511 if (dfc
->table_size
> CCDC_DFT_TABLE_SIZE
)
514 val
|= CCDC_DFCCTL_VDFC_DISABLE
;
515 val
|= (dfc
->dft_corr_ctl
.vdfcsl
& CCDC_DFCCTL_VDFCSL_MASK
) <<
516 CCDC_DFCCTL_VDFCSL_SHIFT
;
517 val
|= (dfc
->dft_corr_ctl
.vdfcuda
& CCDC_DFCCTL_VDFCUDA_MASK
) <<
518 CCDC_DFCCTL_VDFCUDA_SHIFT
;
519 val
|= (dfc
->dft_corr_ctl
.vdflsft
& CCDC_DFCCTL_VDFLSFT_MASK
) <<
520 CCDC_DFCCTL_VDFLSFT_SHIFT
;
523 /* clear address ptr to offset 0 */
524 val
= CCDC_DFCMEMCTL_DFCMARST_MASK
<< CCDC_DFCMEMCTL_DFCMARST_SHIFT
;
526 /* write defect table entries */
527 for (i
= 0; i
< dfc
->table_size
; i
++) {
528 /* increment address for non zero index */
530 val
= CCDC_DFCMEMCTL_INC_ADDR
;
531 regw(val
, DFCMEMCTL
);
532 if (ccdc_write_dfc_entry(i
, dfc
) < 0)
536 /* update saturation level and enable dfc */
537 regw(dfc
->saturation_ctl
& CCDC_VDC_DFCVSAT_MASK
, DFCVSAT
);
538 val
= regr(DFCCTL
) | (CCDC_DFCCTL_VDFCEN_MASK
<<
539 CCDC_DFCCTL_VDFCEN_SHIFT
);
546 * configure parameters for color space conversion
547 * Each register CSCM0-7 has two values in S8Q5 format.
549 static void ccdc_config_csc(struct ccdc_csc
*csc
)
557 /* Enable the CSC sub-module */
558 regw(CCDC_CSC_ENABLE
, CSCCTL
);
560 /* Converting the co-eff as per the format of the register */
561 for (i
= 0; i
< CCDC_CSC_COEFF_TABLE_SIZE
; i
++) {
564 val1
= (csc
->coeff
[i
].integer
&
565 CCDC_CSC_COEF_INTEG_MASK
)
566 << CCDC_CSC_COEF_INTEG_SHIFT
;
568 * convert decimal part to binary. Use 2 decimal
569 * precision, user values range from .00 - 0.99
571 val1
|= (((csc
->coeff
[i
].decimal
&
572 CCDC_CSC_COEF_DECIMAL_MASK
) *
573 CCDC_CSC_DEC_MAX
) / 100);
577 val2
= (csc
->coeff
[i
].integer
&
578 CCDC_CSC_COEF_INTEG_MASK
)
579 << CCDC_CSC_COEF_INTEG_SHIFT
;
580 val2
|= (((csc
->coeff
[i
].decimal
&
581 CCDC_CSC_COEF_DECIMAL_MASK
) *
582 CCDC_CSC_DEC_MAX
) / 100);
583 val2
<<= CCDC_CSCM_MSB_SHIFT
;
585 regw(val2
, (CSCM0
+ ((i
- 1) << 1)));
591 * ccdc_config_color_patterns()
592 * configure parameters for color patterns
594 static void ccdc_config_color_patterns(struct ccdc_col_pat
*pat0
,
595 struct ccdc_col_pat
*pat1
)
599 val
= (pat0
->olop
| (pat0
->olep
<< 2) | (pat0
->elop
<< 4) |
600 (pat0
->elep
<< 6) | (pat1
->olop
<< 8) | (pat1
->olep
<< 10) |
601 (pat1
->elop
<< 12) | (pat1
->elep
<< 14));
605 /* This function will configure CCDC for Raw mode image capture */
606 static int ccdc_config_raw(void)
608 struct ccdc_params_raw
*params
= &ccdc_hw_params_raw
;
609 struct ccdc_config_params_raw
*config_params
=
610 &ccdc_hw_params_raw
.config_params
;
613 dev_dbg(dev
, "\nStarting ccdc_config_raw...");
615 /* restore power on defaults to register */
616 ccdc_restore_defaults();
619 * set CCD Not to swap input since input is RAW data
620 * set FID detection function to Latch at V-Sync
621 * set WENLOG - ccdc valid area to AND
622 * set TRGSEL to WENBIT
623 * set EXTRG to DISABLE
624 * disable latching function on VSYNC - shadowed registers
626 regw(CCDC_YCINSWP_RAW
| CCDC_CCDCFG_FIDMD_LATCH_VSYNC
|
627 CCDC_CCDCFG_WENLOG_AND
| CCDC_CCDCFG_TRGSEL_WEN
|
628 CCDC_CCDCFG_EXTRG_DISABLE
| CCDC_LATCH_ON_VSYNC_DISABLE
, CCDCFG
);
631 * Set VDHD direction to input, input type to raw input
632 * normal data polarity, do not use external WEN
634 val
= (CCDC_VDHDOUT_INPUT
| CCDC_RAW_IP_MODE
| CCDC_DATAPOL_NORMAL
|
638 * Configure the vertical sync polarity (MODESET.VDPOL), horizontal
639 * sync polarity (MODESET.HDPOL), field id polarity (MODESET.FLDPOL),
640 * frame format(progressive or interlace), & pixel format (Input mode)
642 val
|= (((params
->vd_pol
& CCDC_VD_POL_MASK
) << CCDC_VD_POL_SHIFT
) |
643 ((params
->hd_pol
& CCDC_HD_POL_MASK
) << CCDC_HD_POL_SHIFT
) |
644 ((params
->fid_pol
& CCDC_FID_POL_MASK
) << CCDC_FID_POL_SHIFT
) |
645 ((params
->frm_fmt
& CCDC_FRM_FMT_MASK
) << CCDC_FRM_FMT_SHIFT
) |
646 ((params
->pix_fmt
& CCDC_PIX_FMT_MASK
) << CCDC_PIX_FMT_SHIFT
));
648 /* set pack for alaw compression */
649 if ((config_params
->data_sz
== CCDC_DATA_8BITS
) ||
650 config_params
->alaw
.enable
)
651 val
|= CCDC_DATA_PACK_ENABLE
;
653 /* Configure for LPF */
654 if (config_params
->lpf_enable
)
655 val
|= (config_params
->lpf_enable
& CCDC_LPF_MASK
) <<
658 /* Configure the data shift */
659 val
|= (config_params
->datasft
& CCDC_DATASFT_MASK
) <<
662 dev_dbg(dev
, "\nWriting 0x%x to MODESET...\n", val
);
664 /* Configure the Median Filter threshold */
665 regw((config_params
->med_filt_thres
) & CCDC_MED_FILT_THRESH
, MEDFILT
);
667 /* Configure GAMMAWD register. defaur 11-2, and Mosaic cfa pattern */
668 val
= CCDC_GAMMA_BITS_11_2
<< CCDC_GAMMAWD_INPUT_SHIFT
|
671 /* Enable and configure aLaw register if needed */
672 if (config_params
->alaw
.enable
) {
673 val
|= (CCDC_ALAW_ENABLE
|
674 ((config_params
->alaw
.gama_wd
&
675 CCDC_ALAW_GAMA_WD_MASK
) <<
676 CCDC_GAMMAWD_INPUT_SHIFT
));
679 /* Configure Median filter1 & filter2 */
680 val
|= ((config_params
->mfilt1
<< CCDC_MFILT1_SHIFT
) |
681 (config_params
->mfilt2
<< CCDC_MFILT2_SHIFT
));
684 dev_dbg(dev
, "\nWriting 0x%x to GAMMAWD...\n", val
);
686 /* configure video window */
687 ccdc_setwin(¶ms
->win
, params
->frm_fmt
, 1);
689 /* Optical Clamp Averaging */
690 ccdc_config_black_clamp(&config_params
->blk_clamp
);
692 /* Black level compensation */
693 ccdc_config_black_compense(&config_params
->blk_comp
);
695 /* Vertical Defect Correction if needed */
696 if (ccdc_config_vdfc(&config_params
->vertical_dft
) < 0)
699 /* color space conversion */
700 ccdc_config_csc(&config_params
->csc
);
703 ccdc_config_color_patterns(&config_params
->col_pat_field0
,
704 &config_params
->col_pat_field1
);
706 /* Configure the Gain & offset control */
707 ccdc_config_gain_offset();
709 dev_dbg(dev
, "\nWriting %x to COLPTN...\n", val
);
711 /* Configure DATAOFST register */
712 val
= (config_params
->data_offset
.horz_offset
& CCDC_DATAOFST_MASK
) <<
713 CCDC_DATAOFST_H_SHIFT
;
714 val
|= (config_params
->data_offset
.vert_offset
& CCDC_DATAOFST_MASK
) <<
715 CCDC_DATAOFST_V_SHIFT
;
718 /* configuring HSIZE register */
719 val
= (params
->horz_flip_enable
& CCDC_HSIZE_FLIP_MASK
) <<
720 CCDC_HSIZE_FLIP_SHIFT
;
722 /* If pack 8 is enable then 1 pixel will take 1 byte */
723 if ((config_params
->data_sz
== CCDC_DATA_8BITS
) ||
724 config_params
->alaw
.enable
) {
725 val
|= (((params
->win
.width
) + 31) >> 5) &
728 /* adjust to multiple of 32 */
729 dev_dbg(dev
, "\nWriting 0x%x to HSIZE...\n",
730 (((params
->win
.width
) + 31) >> 5) &
731 CCDC_HSIZE_VAL_MASK
);
733 /* else one pixel will take 2 byte */
734 val
|= (((params
->win
.width
* 2) + 31) >> 5) &
737 dev_dbg(dev
, "\nWriting 0x%x to HSIZE...\n",
738 (((params
->win
.width
* 2) + 31) >> 5) &
739 CCDC_HSIZE_VAL_MASK
);
743 /* Configure SDOFST register */
744 if (params
->frm_fmt
== CCDC_FRMFMT_INTERLACED
) {
745 if (params
->image_invert_enable
) {
746 /* For interlace inverse mode */
747 regw(CCDC_SDOFST_INTERLACE_INVERSE
, SDOFST
);
748 dev_dbg(dev
, "\nWriting %x to SDOFST...\n",
749 CCDC_SDOFST_INTERLACE_INVERSE
);
751 /* For interlace non inverse mode */
752 regw(CCDC_SDOFST_INTERLACE_NORMAL
, SDOFST
);
753 dev_dbg(dev
, "\nWriting %x to SDOFST...\n",
754 CCDC_SDOFST_INTERLACE_NORMAL
);
756 } else if (params
->frm_fmt
== CCDC_FRMFMT_PROGRESSIVE
) {
757 if (params
->image_invert_enable
) {
758 /* For progessive inverse mode */
759 regw(CCDC_SDOFST_PROGRESSIVE_INVERSE
, SDOFST
);
760 dev_dbg(dev
, "\nWriting %x to SDOFST...\n",
761 CCDC_SDOFST_PROGRESSIVE_INVERSE
);
763 /* For progessive non inverse mode */
764 regw(CCDC_SDOFST_PROGRESSIVE_NORMAL
, SDOFST
);
765 dev_dbg(dev
, "\nWriting %x to SDOFST...\n",
766 CCDC_SDOFST_PROGRESSIVE_NORMAL
);
769 dev_dbg(dev
, "\nend of ccdc_config_raw...");
773 static int ccdc_configure(void)
775 if (ccdc_if_type
== VPFE_RAW_BAYER
)
776 return ccdc_config_raw();
782 static int ccdc_set_buftype(enum ccdc_buftype buf_type
)
784 if (ccdc_if_type
== VPFE_RAW_BAYER
)
785 ccdc_hw_params_raw
.buf_type
= buf_type
;
787 ccdc_hw_params_ycbcr
.buf_type
= buf_type
;
790 static enum ccdc_buftype
ccdc_get_buftype(void)
792 if (ccdc_if_type
== VPFE_RAW_BAYER
)
793 return ccdc_hw_params_raw
.buf_type
;
794 return ccdc_hw_params_ycbcr
.buf_type
;
797 static int ccdc_enum_pix(u32
*pix
, int i
)
800 if (ccdc_if_type
== VPFE_RAW_BAYER
) {
801 if (i
< ARRAY_SIZE(ccdc_raw_bayer_pix_formats
)) {
802 *pix
= ccdc_raw_bayer_pix_formats
[i
];
806 if (i
< ARRAY_SIZE(ccdc_raw_yuv_pix_formats
)) {
807 *pix
= ccdc_raw_yuv_pix_formats
[i
];
814 static int ccdc_set_pixel_format(u32 pixfmt
)
816 struct ccdc_a_law
*alaw
=
817 &ccdc_hw_params_raw
.config_params
.alaw
;
819 if (ccdc_if_type
== VPFE_RAW_BAYER
) {
820 ccdc_hw_params_raw
.pix_fmt
= CCDC_PIXFMT_RAW
;
821 if (pixfmt
== V4L2_PIX_FMT_SBGGR8
)
823 else if (pixfmt
!= V4L2_PIX_FMT_SBGGR16
)
826 if (pixfmt
== V4L2_PIX_FMT_YUYV
)
827 ccdc_hw_params_ycbcr
.pix_order
= CCDC_PIXORDER_YCBYCR
;
828 else if (pixfmt
== V4L2_PIX_FMT_UYVY
)
829 ccdc_hw_params_ycbcr
.pix_order
= CCDC_PIXORDER_CBYCRY
;
835 static u32
ccdc_get_pixel_format(void)
837 struct ccdc_a_law
*alaw
=
838 &ccdc_hw_params_raw
.config_params
.alaw
;
841 if (ccdc_if_type
== VPFE_RAW_BAYER
)
843 pixfmt
= V4L2_PIX_FMT_SBGGR8
;
845 pixfmt
= V4L2_PIX_FMT_SBGGR16
;
847 if (ccdc_hw_params_ycbcr
.pix_order
== CCDC_PIXORDER_YCBYCR
)
848 pixfmt
= V4L2_PIX_FMT_YUYV
;
850 pixfmt
= V4L2_PIX_FMT_UYVY
;
854 static int ccdc_set_image_window(struct v4l2_rect
*win
)
856 if (ccdc_if_type
== VPFE_RAW_BAYER
)
857 ccdc_hw_params_raw
.win
= *win
;
859 ccdc_hw_params_ycbcr
.win
= *win
;
863 static void ccdc_get_image_window(struct v4l2_rect
*win
)
865 if (ccdc_if_type
== VPFE_RAW_BAYER
)
866 *win
= ccdc_hw_params_raw
.win
;
868 *win
= ccdc_hw_params_ycbcr
.win
;
871 static unsigned int ccdc_get_line_length(void)
873 struct ccdc_config_params_raw
*config_params
=
874 &ccdc_hw_params_raw
.config_params
;
877 if (ccdc_if_type
== VPFE_RAW_BAYER
) {
878 if ((config_params
->alaw
.enable
) ||
879 (config_params
->data_sz
== CCDC_DATA_8BITS
))
880 len
= ccdc_hw_params_raw
.win
.width
;
882 len
= ccdc_hw_params_raw
.win
.width
* 2;
884 len
= ccdc_hw_params_ycbcr
.win
.width
* 2;
885 return ALIGN(len
, 32);
888 static int ccdc_set_frame_format(enum ccdc_frmfmt frm_fmt
)
890 if (ccdc_if_type
== VPFE_RAW_BAYER
)
891 ccdc_hw_params_raw
.frm_fmt
= frm_fmt
;
893 ccdc_hw_params_ycbcr
.frm_fmt
= frm_fmt
;
897 static enum ccdc_frmfmt
ccdc_get_frame_format(void)
899 if (ccdc_if_type
== VPFE_RAW_BAYER
)
900 return ccdc_hw_params_raw
.frm_fmt
;
902 return ccdc_hw_params_ycbcr
.frm_fmt
;
905 static int ccdc_getfid(void)
907 return (regr(MODESET
) >> 15) & 1;
910 /* misc operations */
911 static inline void ccdc_setfbaddr(unsigned long addr
)
913 regw((addr
>> 21) & 0x007f, STADRH
);
914 regw((addr
>> 5) & 0x0ffff, STADRL
);
917 static int ccdc_set_hw_if_params(struct vpfe_hw_if_param
*params
)
919 ccdc_if_type
= params
->if_type
;
921 switch (params
->if_type
) {
923 case VPFE_YCBCR_SYNC_16
:
924 case VPFE_YCBCR_SYNC_8
:
925 ccdc_hw_params_ycbcr
.vd_pol
= params
->vdpol
;
926 ccdc_hw_params_ycbcr
.hd_pol
= params
->hdpol
;
929 /* TODO add support for raw bayer here */
935 static struct ccdc_hw_device ccdc_hw_dev
= {
936 .name
= "DM355 CCDC",
937 .owner
= THIS_MODULE
,
941 .set_ccdc_base
= ccdc_set_ccdc_base
,
942 .enable
= ccdc_enable
,
943 .enable_out_to_sdram
= ccdc_enable_output_to_sdram
,
944 .set_hw_if_params
= ccdc_set_hw_if_params
,
945 .set_params
= ccdc_set_params
,
946 .configure
= ccdc_configure
,
947 .set_buftype
= ccdc_set_buftype
,
948 .get_buftype
= ccdc_get_buftype
,
949 .enum_pix
= ccdc_enum_pix
,
950 .set_pixel_format
= ccdc_set_pixel_format
,
951 .get_pixel_format
= ccdc_get_pixel_format
,
952 .set_frame_format
= ccdc_set_frame_format
,
953 .get_frame_format
= ccdc_get_frame_format
,
954 .set_image_window
= ccdc_set_image_window
,
955 .get_image_window
= ccdc_get_image_window
,
956 .get_line_length
= ccdc_get_line_length
,
957 .setfbaddr
= ccdc_setfbaddr
,
958 .getfid
= ccdc_getfid
,
962 static int dm355_ccdc_init(void)
964 printk(KERN_NOTICE
"dm355_ccdc_init\n");
965 if (vpfe_register_ccdc_device(&ccdc_hw_dev
) < 0)
967 printk(KERN_NOTICE
"%s is registered with vpfe.\n",
972 static void dm355_ccdc_exit(void)
974 vpfe_unregister_ccdc_device(&ccdc_hw_dev
);
977 module_init(dm355_ccdc_init
);
978 module_exit(dm355_ccdc_exit
);