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 <linux/err.h>
41 #include <linux/module.h>
43 #include <media/davinci/dm355_ccdc.h>
44 #include <media/davinci/vpss.h>
46 #include "dm355_ccdc_regs.h"
47 #include "ccdc_hw_device.h"
49 MODULE_LICENSE("GPL");
50 MODULE_DESCRIPTION("CCDC Driver for DM355");
51 MODULE_AUTHOR("Texas Instruments");
53 static struct ccdc_oper_config
{
55 /* CCDC interface type */
56 enum vpfe_hw_if_type if_type
;
57 /* Raw Bayer configuration */
58 struct ccdc_params_raw bayer
;
59 /* YCbCr configuration */
60 struct ccdc_params_ycbcr ycbcr
;
61 /* ccdc base address */
62 void __iomem
*base_addr
;
64 /* Raw configurations */
66 .pix_fmt
= CCDC_PIXFMT_RAW
,
67 .frm_fmt
= CCDC_FRMFMT_PROGRESSIVE
,
69 .fid_pol
= VPFE_PINPOL_POSITIVE
,
70 .vd_pol
= VPFE_PINPOL_POSITIVE
,
71 .hd_pol
= VPFE_PINPOL_POSITIVE
,
80 .mfilt1
= CCDC_NO_MEDIAN_FILTER1
,
81 .mfilt2
= CCDC_NO_MEDIAN_FILTER2
,
90 .olop
= CCDC_GREEN_BLUE
,
93 .elep
= CCDC_GREEN_RED
96 .olop
= CCDC_GREEN_BLUE
,
99 .elep
= CCDC_GREEN_RED
103 /* YCbCr configuration */
106 .pix_fmt
= CCDC_PIXFMT_YCBCR_8BIT
,
107 .frm_fmt
= CCDC_FRMFMT_INTERLACED
,
108 .fid_pol
= VPFE_PINPOL_POSITIVE
,
109 .vd_pol
= VPFE_PINPOL_POSITIVE
,
110 .hd_pol
= VPFE_PINPOL_POSITIVE
,
112 .pix_order
= CCDC_PIXORDER_CBYCRY
,
113 .buf_type
= CCDC_BUFTYPE_FLD_INTERLEAVED
118 /* Raw Bayer formats */
119 static u32 ccdc_raw_bayer_pix_formats
[] =
120 {V4L2_PIX_FMT_SBGGR8
, V4L2_PIX_FMT_SBGGR16
};
122 /* Raw YUV formats */
123 static u32 ccdc_raw_yuv_pix_formats
[] =
124 {V4L2_PIX_FMT_UYVY
, V4L2_PIX_FMT_YUYV
};
126 /* register access routines */
127 static inline u32
regr(u32 offset
)
129 return __raw_readl(ccdc_cfg
.base_addr
+ offset
);
132 static inline void regw(u32 val
, u32 offset
)
134 __raw_writel(val
, ccdc_cfg
.base_addr
+ offset
);
137 static void ccdc_enable(int en
)
141 temp
&= (~CCDC_SYNCEN_VDHDEN_MASK
);
142 temp
|= (en
& CCDC_SYNCEN_VDHDEN_MASK
);
146 static void ccdc_enable_output_to_sdram(int en
)
150 temp
&= (~(CCDC_SYNCEN_WEN_MASK
));
151 temp
|= ((en
<< CCDC_SYNCEN_WEN_SHIFT
) & CCDC_SYNCEN_WEN_MASK
);
155 static void ccdc_config_gain_offset(void)
158 regw(ccdc_cfg
.bayer
.gain
.r_ye
, RYEGAIN
);
159 regw(ccdc_cfg
.bayer
.gain
.gr_cy
, GRCYGAIN
);
160 regw(ccdc_cfg
.bayer
.gain
.gb_g
, GBGGAIN
);
161 regw(ccdc_cfg
.bayer
.gain
.b_mg
, BMGGAIN
);
162 /* configure offset */
163 regw(ccdc_cfg
.bayer
.ccdc_offset
, OFFSET
);
167 * ccdc_restore_defaults()
168 * This function restore power on defaults in the ccdc registers
170 static int ccdc_restore_defaults(void)
174 dev_dbg(ccdc_cfg
.dev
, "\nstarting ccdc_restore_defaults...");
175 /* set all registers to zero */
176 for (i
= 0; i
<= CCDC_REG_LAST
; i
+= 4)
179 /* now override the values with power on defaults in registers */
180 regw(MODESET_DEFAULT
, MODESET
);
181 /* no culling support */
182 regw(CULH_DEFAULT
, CULH
);
183 regw(CULV_DEFAULT
, CULV
);
184 /* Set default Gain and Offset */
185 ccdc_cfg
.bayer
.gain
.r_ye
= GAIN_DEFAULT
;
186 ccdc_cfg
.bayer
.gain
.gb_g
= GAIN_DEFAULT
;
187 ccdc_cfg
.bayer
.gain
.gr_cy
= GAIN_DEFAULT
;
188 ccdc_cfg
.bayer
.gain
.b_mg
= GAIN_DEFAULT
;
189 ccdc_config_gain_offset();
190 regw(OUTCLIP_DEFAULT
, OUTCLIP
);
191 regw(LSCCFG2_DEFAULT
, LSCCFG2
);
192 /* select ccdc input */
193 if (vpss_select_ccdc_source(VPSS_CCDCIN
)) {
194 dev_dbg(ccdc_cfg
.dev
, "\ncouldn't select ccdc input source");
197 /* select ccdc clock */
198 if (vpss_enable_clock(VPSS_CCDC_CLOCK
, 1) < 0) {
199 dev_dbg(ccdc_cfg
.dev
, "\ncouldn't enable ccdc clock");
202 dev_dbg(ccdc_cfg
.dev
, "\nEnd of ccdc_restore_defaults...");
206 static int ccdc_open(struct device
*device
)
208 return ccdc_restore_defaults();
211 static int ccdc_close(struct device
*device
)
214 vpss_enable_clock(VPSS_CCDC_CLOCK
, 0);
215 /* do nothing for now */
220 * This function will configure the window size to
221 * be capture in CCDC reg.
223 static void ccdc_setwin(struct v4l2_rect
*image_win
,
224 enum ccdc_frmfmt frm_fmt
, int ppc
)
226 int horz_start
, horz_nr_pixels
;
227 int vert_start
, vert_nr_lines
;
230 dev_dbg(ccdc_cfg
.dev
, "\nStarting ccdc_setwin...");
233 * ppc - per pixel count. indicates how many pixels per cell
234 * output to SDRAM. example, for ycbcr, it is one y and one c, so 2.
235 * raw capture this is 1
237 horz_start
= image_win
->left
<< (ppc
- 1);
238 horz_nr_pixels
= ((image_win
->width
) << (ppc
- 1)) - 1;
240 /* Writing the horizontal info into the registers */
241 regw(horz_start
, SPH
);
242 regw(horz_nr_pixels
, NPH
);
243 vert_start
= image_win
->top
;
245 if (frm_fmt
== CCDC_FRMFMT_INTERLACED
) {
246 vert_nr_lines
= (image_win
->height
>> 1) - 1;
248 /* Since first line doesn't have any data */
250 /* configure VDINT0 and VDINT1 */
251 regw(vert_start
, VDINT0
);
253 /* Since first line doesn't have any data */
255 vert_nr_lines
= image_win
->height
- 1;
256 /* configure VDINT0 and VDINT1 */
257 mid_img
= vert_start
+ (image_win
->height
/ 2);
258 regw(vert_start
, VDINT0
);
259 regw(mid_img
, VDINT1
);
261 regw(vert_start
& CCDC_START_VER_ONE_MASK
, SLV0
);
262 regw(vert_start
& CCDC_START_VER_TWO_MASK
, SLV1
);
263 regw(vert_nr_lines
& CCDC_NUM_LINES_VER
, NLV
);
264 dev_dbg(ccdc_cfg
.dev
, "\nEnd of ccdc_setwin...");
267 static int validate_ccdc_param(struct ccdc_config_params_raw
*ccdcparam
)
269 if (ccdcparam
->datasft
< CCDC_DATA_NO_SHIFT
||
270 ccdcparam
->datasft
> CCDC_DATA_SHIFT_6BIT
) {
271 dev_dbg(ccdc_cfg
.dev
, "Invalid value of data shift\n");
275 if (ccdcparam
->mfilt1
< CCDC_NO_MEDIAN_FILTER1
||
276 ccdcparam
->mfilt1
> CCDC_MEDIAN_FILTER1
) {
277 dev_dbg(ccdc_cfg
.dev
, "Invalid value of median filter1\n");
281 if (ccdcparam
->mfilt2
< CCDC_NO_MEDIAN_FILTER2
||
282 ccdcparam
->mfilt2
> CCDC_MEDIAN_FILTER2
) {
283 dev_dbg(ccdc_cfg
.dev
, "Invalid value of median filter2\n");
287 if ((ccdcparam
->med_filt_thres
< 0) ||
288 (ccdcparam
->med_filt_thres
> CCDC_MED_FILT_THRESH
)) {
289 dev_dbg(ccdc_cfg
.dev
,
290 "Invalid value of median filter threshold\n");
294 if (ccdcparam
->data_sz
< CCDC_DATA_16BITS
||
295 ccdcparam
->data_sz
> CCDC_DATA_8BITS
) {
296 dev_dbg(ccdc_cfg
.dev
, "Invalid value of data size\n");
300 if (ccdcparam
->alaw
.enable
) {
301 if (ccdcparam
->alaw
.gamma_wd
< CCDC_GAMMA_BITS_13_4
||
302 ccdcparam
->alaw
.gamma_wd
> CCDC_GAMMA_BITS_09_0
) {
303 dev_dbg(ccdc_cfg
.dev
, "Invalid value of ALAW\n");
308 if (ccdcparam
->blk_clamp
.b_clamp_enable
) {
309 if (ccdcparam
->blk_clamp
.sample_pixel
< CCDC_SAMPLE_1PIXELS
||
310 ccdcparam
->blk_clamp
.sample_pixel
> CCDC_SAMPLE_16PIXELS
) {
311 dev_dbg(ccdc_cfg
.dev
,
312 "Invalid value of sample pixel\n");
315 if (ccdcparam
->blk_clamp
.sample_ln
< CCDC_SAMPLE_1LINES
||
316 ccdcparam
->blk_clamp
.sample_ln
> CCDC_SAMPLE_16LINES
) {
317 dev_dbg(ccdc_cfg
.dev
,
318 "Invalid value of sample lines\n");
325 /* Parameter operations */
326 static int ccdc_set_params(void __user
*params
)
328 struct ccdc_config_params_raw ccdc_raw_params
;
331 /* only raw module parameters can be set through the IOCTL */
332 if (ccdc_cfg
.if_type
!= VPFE_RAW_BAYER
)
335 x
= copy_from_user(&ccdc_raw_params
, params
, sizeof(ccdc_raw_params
));
337 dev_dbg(ccdc_cfg
.dev
, "ccdc_set_params: error in copying ccdc"
342 if (!validate_ccdc_param(&ccdc_raw_params
)) {
343 memcpy(&ccdc_cfg
.bayer
.config_params
,
345 sizeof(ccdc_raw_params
));
351 /* This function will configure CCDC for YCbCr video capture */
352 static void ccdc_config_ycbcr(void)
354 struct ccdc_params_ycbcr
*params
= &ccdc_cfg
.ycbcr
;
357 /* first set the CCDC power on defaults values in all registers */
358 dev_dbg(ccdc_cfg
.dev
, "\nStarting ccdc_config_ycbcr...");
359 ccdc_restore_defaults();
361 /* configure pixel format & video frame format */
362 temp
= (((params
->pix_fmt
& CCDC_INPUT_MODE_MASK
) <<
363 CCDC_INPUT_MODE_SHIFT
) |
364 ((params
->frm_fmt
& CCDC_FRM_FMT_MASK
) <<
365 CCDC_FRM_FMT_SHIFT
));
367 /* setup BT.656 sync mode */
368 if (params
->bt656_enable
) {
369 regw(CCDC_REC656IF_BT656_EN
, REC656IF
);
371 * configure the FID, VD, HD pin polarity fld,hd pol positive,
372 * vd negative, 8-bit pack mode
374 temp
|= CCDC_VD_POL_NEGATIVE
;
375 } else { /* y/c external sync mode */
376 temp
|= (((params
->fid_pol
& CCDC_FID_POL_MASK
) <<
377 CCDC_FID_POL_SHIFT
) |
378 ((params
->hd_pol
& CCDC_HD_POL_MASK
) <<
380 ((params
->vd_pol
& CCDC_VD_POL_MASK
) <<
384 /* pack the data to 8-bit */
385 temp
|= CCDC_DATA_PACK_ENABLE
;
389 /* configure video window */
390 ccdc_setwin(¶ms
->win
, params
->frm_fmt
, 2);
392 /* configure the order of y cb cr in SD-RAM */
393 temp
= (params
->pix_order
<< CCDC_Y8POS_SHIFT
);
394 temp
|= CCDC_LATCH_ON_VSYNC_DISABLE
| CCDC_CCDCFG_FIDMD_NO_LATCH_VSYNC
;
398 * configure the horizontal line offset. This is done by rounding up
399 * width to a multiple of 16 pixels and multiply by two to account for
402 regw(((params
->win
.width
* 2 + 31) >> 5), HSIZE
);
404 /* configure the memory line offset */
405 if (params
->buf_type
== CCDC_BUFTYPE_FLD_INTERLEAVED
) {
406 /* two fields are interleaved in memory */
407 regw(CCDC_SDOFST_FIELD_INTERLEAVED
, SDOFST
);
410 dev_dbg(ccdc_cfg
.dev
, "\nEnd of ccdc_config_ycbcr...\n");
414 * ccdc_config_black_clamp()
415 * configure parameters for Optical Black Clamp
417 static void ccdc_config_black_clamp(struct ccdc_black_clamp
*bclamp
)
421 if (!bclamp
->b_clamp_enable
) {
422 /* configure DCSub */
423 regw(bclamp
->dc_sub
& CCDC_BLK_DC_SUB_MASK
, DCSUB
);
427 /* Enable the Black clamping, set sample lines and pixels */
428 val
= (bclamp
->start_pixel
& CCDC_BLK_ST_PXL_MASK
) |
429 ((bclamp
->sample_pixel
& CCDC_BLK_SAMPLE_LN_MASK
) <<
430 CCDC_BLK_SAMPLE_LN_SHIFT
) | CCDC_BLK_CLAMP_ENABLE
;
433 /* If Black clamping is enable then make dcsub 0 */
434 val
= (bclamp
->sample_ln
& CCDC_NUM_LINE_CALC_MASK
)
435 << CCDC_NUM_LINE_CALC_SHIFT
;
440 * ccdc_config_black_compense()
441 * configure parameters for Black Compensation
443 static void ccdc_config_black_compense(struct ccdc_black_compensation
*bcomp
)
447 val
= (bcomp
->b
& CCDC_BLK_COMP_MASK
) |
448 ((bcomp
->gb
& CCDC_BLK_COMP_MASK
) <<
449 CCDC_BLK_COMP_GB_COMP_SHIFT
);
452 val
= ((bcomp
->gr
& CCDC_BLK_COMP_MASK
) <<
453 CCDC_BLK_COMP_GR_COMP_SHIFT
) |
454 ((bcomp
->r
& CCDC_BLK_COMP_MASK
) <<
455 CCDC_BLK_COMP_R_COMP_SHIFT
);
460 * ccdc_write_dfc_entry()
461 * write an entry in the dfc table.
463 static int ccdc_write_dfc_entry(int index
, struct ccdc_vertical_dft
*dfc
)
465 /* TODO This is to be re-visited and adjusted */
466 #define DFC_WRITE_WAIT_COUNT 1000
467 u32 val
, count
= DFC_WRITE_WAIT_COUNT
;
469 regw(dfc
->dft_corr_vert
[index
], DFCMEM0
);
470 regw(dfc
->dft_corr_horz
[index
], DFCMEM1
);
471 regw(dfc
->dft_corr_sub1
[index
], DFCMEM2
);
472 regw(dfc
->dft_corr_sub2
[index
], DFCMEM3
);
473 regw(dfc
->dft_corr_sub3
[index
], DFCMEM4
);
474 /* set WR bit to write */
475 val
= regr(DFCMEMCTL
) | CCDC_DFCMEMCTL_DFCMWR_MASK
;
476 regw(val
, DFCMEMCTL
);
479 * Assume, it is very short. If we get an error, we need to
482 while (regr(DFCMEMCTL
) & CCDC_DFCMEMCTL_DFCMWR_MASK
)
485 * TODO We expect the count to be non-zero to be successful. Adjust
486 * the count if write requires more time
490 dev_err(ccdc_cfg
.dev
, "defect table write timeout !!!\n");
498 * configure parameters for Vertical Defect Correction
500 static int ccdc_config_vdfc(struct ccdc_vertical_dft
*dfc
)
505 /* Configure General Defect Correction. The table used is from IPIPE */
506 val
= dfc
->gen_dft_en
& CCDC_DFCCTL_GDFCEN_MASK
;
508 /* Configure Vertical Defect Correction if needed */
509 if (!dfc
->ver_dft_en
) {
510 /* Enable only General Defect Correction */
515 if (dfc
->table_size
> CCDC_DFT_TABLE_SIZE
)
518 val
|= CCDC_DFCCTL_VDFC_DISABLE
;
519 val
|= (dfc
->dft_corr_ctl
.vdfcsl
& CCDC_DFCCTL_VDFCSL_MASK
) <<
520 CCDC_DFCCTL_VDFCSL_SHIFT
;
521 val
|= (dfc
->dft_corr_ctl
.vdfcuda
& CCDC_DFCCTL_VDFCUDA_MASK
) <<
522 CCDC_DFCCTL_VDFCUDA_SHIFT
;
523 val
|= (dfc
->dft_corr_ctl
.vdflsft
& CCDC_DFCCTL_VDFLSFT_MASK
) <<
524 CCDC_DFCCTL_VDFLSFT_SHIFT
;
527 /* clear address ptr to offset 0 */
528 val
= CCDC_DFCMEMCTL_DFCMARST_MASK
<< CCDC_DFCMEMCTL_DFCMARST_SHIFT
;
530 /* write defect table entries */
531 for (i
= 0; i
< dfc
->table_size
; i
++) {
532 /* increment address for non zero index */
534 val
= CCDC_DFCMEMCTL_INC_ADDR
;
535 regw(val
, DFCMEMCTL
);
536 if (ccdc_write_dfc_entry(i
, dfc
) < 0)
540 /* update saturation level and enable dfc */
541 regw(dfc
->saturation_ctl
& CCDC_VDC_DFCVSAT_MASK
, DFCVSAT
);
542 val
= regr(DFCCTL
) | (CCDC_DFCCTL_VDFCEN_MASK
<<
543 CCDC_DFCCTL_VDFCEN_SHIFT
);
550 * configure parameters for color space conversion
551 * Each register CSCM0-7 has two values in S8Q5 format.
553 static void ccdc_config_csc(struct ccdc_csc
*csc
)
561 /* Enable the CSC sub-module */
562 regw(CCDC_CSC_ENABLE
, CSCCTL
);
564 /* Converting the co-eff as per the format of the register */
565 for (i
= 0; i
< CCDC_CSC_COEFF_TABLE_SIZE
; i
++) {
568 val1
= (csc
->coeff
[i
].integer
&
569 CCDC_CSC_COEF_INTEG_MASK
)
570 << CCDC_CSC_COEF_INTEG_SHIFT
;
572 * convert decimal part to binary. Use 2 decimal
573 * precision, user values range from .00 - 0.99
575 val1
|= (((csc
->coeff
[i
].decimal
&
576 CCDC_CSC_COEF_DECIMAL_MASK
) *
577 CCDC_CSC_DEC_MAX
) / 100);
581 val2
= (csc
->coeff
[i
].integer
&
582 CCDC_CSC_COEF_INTEG_MASK
)
583 << CCDC_CSC_COEF_INTEG_SHIFT
;
584 val2
|= (((csc
->coeff
[i
].decimal
&
585 CCDC_CSC_COEF_DECIMAL_MASK
) *
586 CCDC_CSC_DEC_MAX
) / 100);
587 val2
<<= CCDC_CSCM_MSB_SHIFT
;
589 regw(val2
, (CSCM0
+ ((i
- 1) << 1)));
595 * ccdc_config_color_patterns()
596 * configure parameters for color patterns
598 static void ccdc_config_color_patterns(struct ccdc_col_pat
*pat0
,
599 struct ccdc_col_pat
*pat1
)
603 val
= (pat0
->olop
| (pat0
->olep
<< 2) | (pat0
->elop
<< 4) |
604 (pat0
->elep
<< 6) | (pat1
->olop
<< 8) | (pat1
->olep
<< 10) |
605 (pat1
->elop
<< 12) | (pat1
->elep
<< 14));
609 /* This function will configure CCDC for Raw mode image capture */
610 static int ccdc_config_raw(void)
612 struct ccdc_params_raw
*params
= &ccdc_cfg
.bayer
;
613 struct ccdc_config_params_raw
*config_params
=
614 &ccdc_cfg
.bayer
.config_params
;
617 dev_dbg(ccdc_cfg
.dev
, "\nStarting ccdc_config_raw...");
619 /* restore power on defaults to register */
620 ccdc_restore_defaults();
623 * set CCD Not to swap input since input is RAW data
624 * set FID detection function to Latch at V-Sync
625 * set WENLOG - ccdc valid area to AND
626 * set TRGSEL to WENBIT
627 * set EXTRG to DISABLE
628 * disable latching function on VSYNC - shadowed registers
630 regw(CCDC_YCINSWP_RAW
| CCDC_CCDCFG_FIDMD_LATCH_VSYNC
|
631 CCDC_CCDCFG_WENLOG_AND
| CCDC_CCDCFG_TRGSEL_WEN
|
632 CCDC_CCDCFG_EXTRG_DISABLE
| CCDC_LATCH_ON_VSYNC_DISABLE
, CCDCFG
);
635 * Set VDHD direction to input, input type to raw input
636 * normal data polarity, do not use external WEN
638 val
= (CCDC_VDHDOUT_INPUT
| CCDC_RAW_IP_MODE
| CCDC_DATAPOL_NORMAL
|
642 * Configure the vertical sync polarity (MODESET.VDPOL), horizontal
643 * sync polarity (MODESET.HDPOL), field id polarity (MODESET.FLDPOL),
644 * frame format(progressive or interlace), & pixel format (Input mode)
646 val
|= (((params
->vd_pol
& CCDC_VD_POL_MASK
) << CCDC_VD_POL_SHIFT
) |
647 ((params
->hd_pol
& CCDC_HD_POL_MASK
) << CCDC_HD_POL_SHIFT
) |
648 ((params
->fid_pol
& CCDC_FID_POL_MASK
) << CCDC_FID_POL_SHIFT
) |
649 ((params
->frm_fmt
& CCDC_FRM_FMT_MASK
) << CCDC_FRM_FMT_SHIFT
) |
650 ((params
->pix_fmt
& CCDC_PIX_FMT_MASK
) << CCDC_PIX_FMT_SHIFT
));
652 /* set pack for alaw compression */
653 if ((config_params
->data_sz
== CCDC_DATA_8BITS
) ||
654 config_params
->alaw
.enable
)
655 val
|= CCDC_DATA_PACK_ENABLE
;
657 /* Configure for LPF */
658 if (config_params
->lpf_enable
)
659 val
|= (config_params
->lpf_enable
& CCDC_LPF_MASK
) <<
662 /* Configure the data shift */
663 val
|= (config_params
->datasft
& CCDC_DATASFT_MASK
) <<
666 dev_dbg(ccdc_cfg
.dev
, "\nWriting 0x%x to MODESET...\n", val
);
668 /* Configure the Median Filter threshold */
669 regw((config_params
->med_filt_thres
) & CCDC_MED_FILT_THRESH
, MEDFILT
);
671 /* Configure GAMMAWD register. defaur 11-2, and Mosaic cfa pattern */
672 val
= CCDC_GAMMA_BITS_11_2
<< CCDC_GAMMAWD_INPUT_SHIFT
|
675 /* Enable and configure aLaw register if needed */
676 if (config_params
->alaw
.enable
) {
677 val
|= (CCDC_ALAW_ENABLE
|
678 ((config_params
->alaw
.gamma_wd
&
679 CCDC_ALAW_GAMMA_WD_MASK
) <<
680 CCDC_GAMMAWD_INPUT_SHIFT
));
683 /* Configure Median filter1 & filter2 */
684 val
|= ((config_params
->mfilt1
<< CCDC_MFILT1_SHIFT
) |
685 (config_params
->mfilt2
<< CCDC_MFILT2_SHIFT
));
688 dev_dbg(ccdc_cfg
.dev
, "\nWriting 0x%x to GAMMAWD...\n", val
);
690 /* configure video window */
691 ccdc_setwin(¶ms
->win
, params
->frm_fmt
, 1);
693 /* Optical Clamp Averaging */
694 ccdc_config_black_clamp(&config_params
->blk_clamp
);
696 /* Black level compensation */
697 ccdc_config_black_compense(&config_params
->blk_comp
);
699 /* Vertical Defect Correction if needed */
700 if (ccdc_config_vdfc(&config_params
->vertical_dft
) < 0)
703 /* color space conversion */
704 ccdc_config_csc(&config_params
->csc
);
707 ccdc_config_color_patterns(&config_params
->col_pat_field0
,
708 &config_params
->col_pat_field1
);
710 /* Configure the Gain & offset control */
711 ccdc_config_gain_offset();
713 dev_dbg(ccdc_cfg
.dev
, "\nWriting %x to COLPTN...\n", val
);
715 /* Configure DATAOFST register */
716 val
= (config_params
->data_offset
.horz_offset
& CCDC_DATAOFST_MASK
) <<
717 CCDC_DATAOFST_H_SHIFT
;
718 val
|= (config_params
->data_offset
.vert_offset
& CCDC_DATAOFST_MASK
) <<
719 CCDC_DATAOFST_V_SHIFT
;
722 /* configuring HSIZE register */
723 val
= (params
->horz_flip_enable
& CCDC_HSIZE_FLIP_MASK
) <<
724 CCDC_HSIZE_FLIP_SHIFT
;
726 /* If pack 8 is enable then 1 pixel will take 1 byte */
727 if ((config_params
->data_sz
== CCDC_DATA_8BITS
) ||
728 config_params
->alaw
.enable
) {
729 val
|= (((params
->win
.width
) + 31) >> 5) &
732 /* adjust to multiple of 32 */
733 dev_dbg(ccdc_cfg
.dev
, "\nWriting 0x%x to HSIZE...\n",
734 (((params
->win
.width
) + 31) >> 5) &
735 CCDC_HSIZE_VAL_MASK
);
737 /* else one pixel will take 2 byte */
738 val
|= (((params
->win
.width
* 2) + 31) >> 5) &
741 dev_dbg(ccdc_cfg
.dev
, "\nWriting 0x%x to HSIZE...\n",
742 (((params
->win
.width
* 2) + 31) >> 5) &
743 CCDC_HSIZE_VAL_MASK
);
747 /* Configure SDOFST register */
748 if (params
->frm_fmt
== CCDC_FRMFMT_INTERLACED
) {
749 if (params
->image_invert_enable
) {
750 /* For interlace inverse mode */
751 regw(CCDC_SDOFST_INTERLACE_INVERSE
, SDOFST
);
752 dev_dbg(ccdc_cfg
.dev
, "\nWriting %x to SDOFST...\n",
753 CCDC_SDOFST_INTERLACE_INVERSE
);
755 /* For interlace non inverse mode */
756 regw(CCDC_SDOFST_INTERLACE_NORMAL
, SDOFST
);
757 dev_dbg(ccdc_cfg
.dev
, "\nWriting %x to SDOFST...\n",
758 CCDC_SDOFST_INTERLACE_NORMAL
);
760 } else if (params
->frm_fmt
== CCDC_FRMFMT_PROGRESSIVE
) {
761 if (params
->image_invert_enable
) {
762 /* For progessive inverse mode */
763 regw(CCDC_SDOFST_PROGRESSIVE_INVERSE
, SDOFST
);
764 dev_dbg(ccdc_cfg
.dev
, "\nWriting %x to SDOFST...\n",
765 CCDC_SDOFST_PROGRESSIVE_INVERSE
);
767 /* For progessive non inverse mode */
768 regw(CCDC_SDOFST_PROGRESSIVE_NORMAL
, SDOFST
);
769 dev_dbg(ccdc_cfg
.dev
, "\nWriting %x to SDOFST...\n",
770 CCDC_SDOFST_PROGRESSIVE_NORMAL
);
773 dev_dbg(ccdc_cfg
.dev
, "\nend of ccdc_config_raw...");
777 static int ccdc_configure(void)
779 if (ccdc_cfg
.if_type
== VPFE_RAW_BAYER
)
780 return ccdc_config_raw();
786 static int ccdc_set_buftype(enum ccdc_buftype buf_type
)
788 if (ccdc_cfg
.if_type
== VPFE_RAW_BAYER
)
789 ccdc_cfg
.bayer
.buf_type
= buf_type
;
791 ccdc_cfg
.ycbcr
.buf_type
= buf_type
;
794 static enum ccdc_buftype
ccdc_get_buftype(void)
796 if (ccdc_cfg
.if_type
== VPFE_RAW_BAYER
)
797 return ccdc_cfg
.bayer
.buf_type
;
798 return ccdc_cfg
.ycbcr
.buf_type
;
801 static int ccdc_enum_pix(u32
*pix
, int i
)
804 if (ccdc_cfg
.if_type
== VPFE_RAW_BAYER
) {
805 if (i
< ARRAY_SIZE(ccdc_raw_bayer_pix_formats
)) {
806 *pix
= ccdc_raw_bayer_pix_formats
[i
];
810 if (i
< ARRAY_SIZE(ccdc_raw_yuv_pix_formats
)) {
811 *pix
= ccdc_raw_yuv_pix_formats
[i
];
818 static int ccdc_set_pixel_format(u32 pixfmt
)
820 struct ccdc_a_law
*alaw
= &ccdc_cfg
.bayer
.config_params
.alaw
;
822 if (ccdc_cfg
.if_type
== VPFE_RAW_BAYER
) {
823 ccdc_cfg
.bayer
.pix_fmt
= CCDC_PIXFMT_RAW
;
824 if (pixfmt
== V4L2_PIX_FMT_SBGGR8
)
826 else if (pixfmt
!= V4L2_PIX_FMT_SBGGR16
)
829 if (pixfmt
== V4L2_PIX_FMT_YUYV
)
830 ccdc_cfg
.ycbcr
.pix_order
= CCDC_PIXORDER_YCBYCR
;
831 else if (pixfmt
== V4L2_PIX_FMT_UYVY
)
832 ccdc_cfg
.ycbcr
.pix_order
= CCDC_PIXORDER_CBYCRY
;
838 static u32
ccdc_get_pixel_format(void)
840 struct ccdc_a_law
*alaw
= &ccdc_cfg
.bayer
.config_params
.alaw
;
843 if (ccdc_cfg
.if_type
== VPFE_RAW_BAYER
)
845 pixfmt
= V4L2_PIX_FMT_SBGGR8
;
847 pixfmt
= V4L2_PIX_FMT_SBGGR16
;
849 if (ccdc_cfg
.ycbcr
.pix_order
== CCDC_PIXORDER_YCBYCR
)
850 pixfmt
= V4L2_PIX_FMT_YUYV
;
852 pixfmt
= V4L2_PIX_FMT_UYVY
;
856 static int ccdc_set_image_window(struct v4l2_rect
*win
)
858 if (ccdc_cfg
.if_type
== VPFE_RAW_BAYER
)
859 ccdc_cfg
.bayer
.win
= *win
;
861 ccdc_cfg
.ycbcr
.win
= *win
;
865 static void ccdc_get_image_window(struct v4l2_rect
*win
)
867 if (ccdc_cfg
.if_type
== VPFE_RAW_BAYER
)
868 *win
= ccdc_cfg
.bayer
.win
;
870 *win
= ccdc_cfg
.ycbcr
.win
;
873 static unsigned int ccdc_get_line_length(void)
875 struct ccdc_config_params_raw
*config_params
=
876 &ccdc_cfg
.bayer
.config_params
;
879 if (ccdc_cfg
.if_type
== VPFE_RAW_BAYER
) {
880 if ((config_params
->alaw
.enable
) ||
881 (config_params
->data_sz
== CCDC_DATA_8BITS
))
882 len
= ccdc_cfg
.bayer
.win
.width
;
884 len
= ccdc_cfg
.bayer
.win
.width
* 2;
886 len
= ccdc_cfg
.ycbcr
.win
.width
* 2;
887 return ALIGN(len
, 32);
890 static int ccdc_set_frame_format(enum ccdc_frmfmt frm_fmt
)
892 if (ccdc_cfg
.if_type
== VPFE_RAW_BAYER
)
893 ccdc_cfg
.bayer
.frm_fmt
= frm_fmt
;
895 ccdc_cfg
.ycbcr
.frm_fmt
= frm_fmt
;
899 static enum ccdc_frmfmt
ccdc_get_frame_format(void)
901 if (ccdc_cfg
.if_type
== VPFE_RAW_BAYER
)
902 return ccdc_cfg
.bayer
.frm_fmt
;
904 return ccdc_cfg
.ycbcr
.frm_fmt
;
907 static int ccdc_getfid(void)
909 return (regr(MODESET
) >> 15) & 1;
912 /* misc operations */
913 static inline void ccdc_setfbaddr(unsigned long addr
)
915 regw((addr
>> 21) & 0x007f, STADRH
);
916 regw((addr
>> 5) & 0x0ffff, STADRL
);
919 static int ccdc_set_hw_if_params(struct vpfe_hw_if_param
*params
)
921 ccdc_cfg
.if_type
= params
->if_type
;
923 switch (params
->if_type
) {
925 case VPFE_YCBCR_SYNC_16
:
926 case VPFE_YCBCR_SYNC_8
:
927 ccdc_cfg
.ycbcr
.vd_pol
= params
->vdpol
;
928 ccdc_cfg
.ycbcr
.hd_pol
= params
->hdpol
;
931 /* TODO add support for raw bayer here */
937 static struct ccdc_hw_device ccdc_hw_dev
= {
938 .name
= "DM355 CCDC",
939 .owner
= THIS_MODULE
,
943 .enable
= ccdc_enable
,
944 .enable_out_to_sdram
= ccdc_enable_output_to_sdram
,
945 .set_hw_if_params
= ccdc_set_hw_if_params
,
946 .set_params
= ccdc_set_params
,
947 .configure
= ccdc_configure
,
948 .set_buftype
= ccdc_set_buftype
,
949 .get_buftype
= ccdc_get_buftype
,
950 .enum_pix
= ccdc_enum_pix
,
951 .set_pixel_format
= ccdc_set_pixel_format
,
952 .get_pixel_format
= ccdc_get_pixel_format
,
953 .set_frame_format
= ccdc_set_frame_format
,
954 .get_frame_format
= ccdc_get_frame_format
,
955 .set_image_window
= ccdc_set_image_window
,
956 .get_image_window
= ccdc_get_image_window
,
957 .get_line_length
= ccdc_get_line_length
,
958 .setfbaddr
= ccdc_setfbaddr
,
959 .getfid
= ccdc_getfid
,
963 static int dm355_ccdc_probe(struct platform_device
*pdev
)
965 void (*setup_pinmux
)(void);
966 struct resource
*res
;
970 * first try to register with vpfe. If not correct platform, then we
971 * don't have to iomap
973 status
= vpfe_register_ccdc_device(&ccdc_hw_dev
);
977 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
983 res
= request_mem_region(res
->start
, resource_size(res
), res
->name
);
989 ccdc_cfg
.base_addr
= ioremap_nocache(res
->start
, resource_size(res
));
990 if (!ccdc_cfg
.base_addr
) {
995 /* Platform data holds setup_pinmux function ptr */
996 if (NULL
== pdev
->dev
.platform_data
) {
1000 setup_pinmux
= pdev
->dev
.platform_data
;
1002 * setup Mux configuration for ccdc which may be different for
1003 * different SoCs using this CCDC
1006 ccdc_cfg
.dev
= &pdev
->dev
;
1007 printk(KERN_NOTICE
"%s is registered with vpfe.\n", ccdc_hw_dev
.name
);
1010 iounmap(ccdc_cfg
.base_addr
);
1012 release_mem_region(res
->start
, resource_size(res
));
1014 vpfe_unregister_ccdc_device(&ccdc_hw_dev
);
1018 static int dm355_ccdc_remove(struct platform_device
*pdev
)
1020 struct resource
*res
;
1022 iounmap(ccdc_cfg
.base_addr
);
1023 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1025 release_mem_region(res
->start
, resource_size(res
));
1026 vpfe_unregister_ccdc_device(&ccdc_hw_dev
);
1030 static struct platform_driver dm355_ccdc_driver
= {
1032 .name
= "dm355_ccdc",
1034 .remove
= dm355_ccdc_remove
,
1035 .probe
= dm355_ccdc_probe
,
1038 module_platform_driver(dm355_ccdc_driver
);