2 * Copyright (C) 2006-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 DM6446
19 * ------------------------------
21 * This module is for configuring CCD controller of DM6446 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 Raw 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 includes dm644x_ccdc.h and vpfe_capture.h header
28 * files. The setparams() API is called by vpfe_capture driver
29 * to configure module parameters. This file is named DM644x so that other
30 * variants such DM6443 may be supported using the same module.
32 * TODO: Test Raw bayer parameter settings and bayer capture
33 * Split module parameter structure to module specific ioctl structs
34 * 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/gfp.h>
41 #include <linux/clk.h>
42 #include <linux/err.h>
44 #include <media/davinci/dm644x_ccdc.h>
45 #include <media/davinci/vpss.h>
47 #include "dm644x_ccdc_regs.h"
48 #include "ccdc_hw_device.h"
50 MODULE_LICENSE("GPL");
51 MODULE_DESCRIPTION("CCDC Driver for DM6446");
52 MODULE_AUTHOR("Texas Instruments");
54 static struct ccdc_oper_config
{
56 /* CCDC interface type */
57 enum vpfe_hw_if_type if_type
;
58 /* Raw Bayer configuration */
59 struct ccdc_params_raw bayer
;
60 /* YCbCr configuration */
61 struct ccdc_params_ycbcr ycbcr
;
66 /* ccdc base address */
67 void __iomem
*base_addr
;
69 /* Raw configurations */
71 .pix_fmt
= CCDC_PIXFMT_RAW
,
72 .frm_fmt
= CCDC_FRMFMT_PROGRESSIVE
,
74 .fid_pol
= VPFE_PINPOL_POSITIVE
,
75 .vd_pol
= VPFE_PINPOL_POSITIVE
,
76 .hd_pol
= VPFE_PINPOL_POSITIVE
,
78 .data_sz
= CCDC_DATA_10BITS
,
82 .pix_fmt
= CCDC_PIXFMT_YCBCR_8BIT
,
83 .frm_fmt
= CCDC_FRMFMT_INTERLACED
,
85 .fid_pol
= VPFE_PINPOL_POSITIVE
,
86 .vd_pol
= VPFE_PINPOL_POSITIVE
,
87 .hd_pol
= VPFE_PINPOL_POSITIVE
,
89 .pix_order
= CCDC_PIXORDER_CBYCRY
,
90 .buf_type
= CCDC_BUFTYPE_FLD_INTERLEAVED
94 #define CCDC_MAX_RAW_YUV_FORMATS 2
96 /* Raw Bayer formats */
97 static u32 ccdc_raw_bayer_pix_formats
[] =
98 {V4L2_PIX_FMT_SBGGR8
, V4L2_PIX_FMT_SBGGR16
};
100 /* Raw YUV formats */
101 static u32 ccdc_raw_yuv_pix_formats
[] =
102 {V4L2_PIX_FMT_UYVY
, V4L2_PIX_FMT_YUYV
};
104 /* CCDC Save/Restore context */
105 static u32 ccdc_ctx
[CCDC_REG_END
/ sizeof(u32
)];
107 /* register access routines */
108 static inline u32
regr(u32 offset
)
110 return __raw_readl(ccdc_cfg
.base_addr
+ offset
);
113 static inline void regw(u32 val
, u32 offset
)
115 __raw_writel(val
, ccdc_cfg
.base_addr
+ offset
);
118 static void ccdc_enable(int flag
)
120 regw(flag
, CCDC_PCR
);
123 static void ccdc_enable_vport(int flag
)
126 /* enable video port */
127 regw(CCDC_ENABLE_VIDEO_PORT
, CCDC_FMTCFG
);
129 regw(CCDC_DISABLE_VIDEO_PORT
, CCDC_FMTCFG
);
134 * This function will configure the window size
135 * to be capture in CCDC reg
137 void ccdc_setwin(struct v4l2_rect
*image_win
,
138 enum ccdc_frmfmt frm_fmt
,
141 int horz_start
, horz_nr_pixels
;
142 int vert_start
, vert_nr_lines
;
143 int val
= 0, mid_img
= 0;
145 dev_dbg(ccdc_cfg
.dev
, "\nStarting ccdc_setwin...");
147 * ppc - per pixel count. indicates how many pixels per cell
148 * output to SDRAM. example, for ycbcr, it is one y and one c, so 2.
149 * raw capture this is 1
151 horz_start
= image_win
->left
<< (ppc
- 1);
152 horz_nr_pixels
= (image_win
->width
<< (ppc
- 1)) - 1;
153 regw((horz_start
<< CCDC_HORZ_INFO_SPH_SHIFT
) | horz_nr_pixels
,
156 vert_start
= image_win
->top
;
158 if (frm_fmt
== CCDC_FRMFMT_INTERLACED
) {
159 vert_nr_lines
= (image_win
->height
>> 1) - 1;
161 /* Since first line doesn't have any data */
163 /* configure VDINT0 */
164 val
= (vert_start
<< CCDC_VDINT_VDINT0_SHIFT
);
165 regw(val
, CCDC_VDINT
);
168 /* Since first line doesn't have any data */
170 vert_nr_lines
= image_win
->height
- 1;
172 * configure VDINT0 and VDINT1. VDINT1 will be at half
175 mid_img
= vert_start
+ (image_win
->height
/ 2);
176 val
= (vert_start
<< CCDC_VDINT_VDINT0_SHIFT
) |
177 (mid_img
& CCDC_VDINT_VDINT1_MASK
);
178 regw(val
, CCDC_VDINT
);
181 regw((vert_start
<< CCDC_VERT_START_SLV0_SHIFT
) | vert_start
,
183 regw(vert_nr_lines
, CCDC_VERT_LINES
);
184 dev_dbg(ccdc_cfg
.dev
, "\nEnd of ccdc_setwin...");
187 static void ccdc_readregs(void)
189 unsigned int val
= 0;
191 val
= regr(CCDC_ALAW
);
192 dev_notice(ccdc_cfg
.dev
, "\nReading 0x%x to ALAW...\n", val
);
193 val
= regr(CCDC_CLAMP
);
194 dev_notice(ccdc_cfg
.dev
, "\nReading 0x%x to CLAMP...\n", val
);
195 val
= regr(CCDC_DCSUB
);
196 dev_notice(ccdc_cfg
.dev
, "\nReading 0x%x to DCSUB...\n", val
);
197 val
= regr(CCDC_BLKCMP
);
198 dev_notice(ccdc_cfg
.dev
, "\nReading 0x%x to BLKCMP...\n", val
);
199 val
= regr(CCDC_FPC_ADDR
);
200 dev_notice(ccdc_cfg
.dev
, "\nReading 0x%x to FPC_ADDR...\n", val
);
201 val
= regr(CCDC_FPC
);
202 dev_notice(ccdc_cfg
.dev
, "\nReading 0x%x to FPC...\n", val
);
203 val
= regr(CCDC_FMTCFG
);
204 dev_notice(ccdc_cfg
.dev
, "\nReading 0x%x to FMTCFG...\n", val
);
205 val
= regr(CCDC_COLPTN
);
206 dev_notice(ccdc_cfg
.dev
, "\nReading 0x%x to COLPTN...\n", val
);
207 val
= regr(CCDC_FMT_HORZ
);
208 dev_notice(ccdc_cfg
.dev
, "\nReading 0x%x to FMT_HORZ...\n", val
);
209 val
= regr(CCDC_FMT_VERT
);
210 dev_notice(ccdc_cfg
.dev
, "\nReading 0x%x to FMT_VERT...\n", val
);
211 val
= regr(CCDC_HSIZE_OFF
);
212 dev_notice(ccdc_cfg
.dev
, "\nReading 0x%x to HSIZE_OFF...\n", val
);
213 val
= regr(CCDC_SDOFST
);
214 dev_notice(ccdc_cfg
.dev
, "\nReading 0x%x to SDOFST...\n", val
);
215 val
= regr(CCDC_VP_OUT
);
216 dev_notice(ccdc_cfg
.dev
, "\nReading 0x%x to VP_OUT...\n", val
);
217 val
= regr(CCDC_SYN_MODE
);
218 dev_notice(ccdc_cfg
.dev
, "\nReading 0x%x to SYN_MODE...\n", val
);
219 val
= regr(CCDC_HORZ_INFO
);
220 dev_notice(ccdc_cfg
.dev
, "\nReading 0x%x to HORZ_INFO...\n", val
);
221 val
= regr(CCDC_VERT_START
);
222 dev_notice(ccdc_cfg
.dev
, "\nReading 0x%x to VERT_START...\n", val
);
223 val
= regr(CCDC_VERT_LINES
);
224 dev_notice(ccdc_cfg
.dev
, "\nReading 0x%x to VERT_LINES...\n", val
);
227 static int validate_ccdc_param(struct ccdc_config_params_raw
*ccdcparam
)
229 if (ccdcparam
->alaw
.enable
) {
230 if ((ccdcparam
->alaw
.gama_wd
> CCDC_GAMMA_BITS_09_0
) ||
231 (ccdcparam
->alaw
.gama_wd
< CCDC_GAMMA_BITS_15_6
) ||
232 (ccdcparam
->alaw
.gama_wd
< ccdcparam
->data_sz
)) {
233 dev_dbg(ccdc_cfg
.dev
, "\nInvalid data line select");
240 static int ccdc_update_raw_params(struct ccdc_config_params_raw
*raw_params
)
242 struct ccdc_config_params_raw
*config_params
=
243 &ccdc_cfg
.bayer
.config_params
;
244 unsigned int *fpc_virtaddr
= NULL
;
245 unsigned int *fpc_physaddr
= NULL
;
247 memcpy(config_params
, raw_params
, sizeof(*raw_params
));
249 * allocate memory for fault pixel table and copy the user
250 * values to the table
252 if (!config_params
->fault_pxl
.enable
)
255 fpc_physaddr
= (unsigned int *)config_params
->fault_pxl
.fpc_table_addr
;
256 fpc_virtaddr
= (unsigned int *)phys_to_virt(
257 (unsigned long)fpc_physaddr
);
259 * Allocate memory for FPC table if current
260 * FPC table buffer is not big enough to
261 * accommodate FPC Number requested
263 if (raw_params
->fault_pxl
.fp_num
!= config_params
->fault_pxl
.fp_num
) {
264 if (fpc_physaddr
!= NULL
) {
265 free_pages((unsigned long)fpc_physaddr
,
267 (config_params
->fault_pxl
.fp_num
*
271 /* Allocate memory for FPC table */
273 (unsigned int *)__get_free_pages(GFP_KERNEL
| GFP_DMA
,
274 get_order(raw_params
->
278 if (fpc_virtaddr
== NULL
) {
279 dev_dbg(ccdc_cfg
.dev
,
280 "\nUnable to allocate memory for FPC");
284 (unsigned int *)virt_to_phys((void *)fpc_virtaddr
);
287 /* Copy number of fault pixels and FPC table */
288 config_params
->fault_pxl
.fp_num
= raw_params
->fault_pxl
.fp_num
;
289 if (copy_from_user(fpc_virtaddr
,
290 (void __user
*)raw_params
->fault_pxl
.fpc_table_addr
,
291 config_params
->fault_pxl
.fp_num
* FP_NUM_BYTES
)) {
292 dev_dbg(ccdc_cfg
.dev
, "\n copy_from_user failed");
295 config_params
->fault_pxl
.fpc_table_addr
= (unsigned int)fpc_physaddr
;
299 static int ccdc_close(struct device
*dev
)
301 struct ccdc_config_params_raw
*config_params
=
302 &ccdc_cfg
.bayer
.config_params
;
303 unsigned int *fpc_physaddr
= NULL
, *fpc_virtaddr
= NULL
;
305 fpc_physaddr
= (unsigned int *)config_params
->fault_pxl
.fpc_table_addr
;
307 if (fpc_physaddr
!= NULL
) {
308 fpc_virtaddr
= (unsigned int *)
309 phys_to_virt((unsigned long)fpc_physaddr
);
310 free_pages((unsigned long)fpc_virtaddr
,
311 get_order(config_params
->fault_pxl
.fp_num
*
318 * ccdc_restore_defaults()
319 * This function will write defaults to all CCDC registers
321 static void ccdc_restore_defaults(void)
327 /* set all registers to default value */
328 for (i
= 4; i
<= 0x94; i
+= 4)
330 regw(CCDC_NO_CULLING
, CCDC_CULLING
);
331 regw(CCDC_GAMMA_BITS_11_2
, CCDC_ALAW
);
334 static int ccdc_open(struct device
*device
)
336 ccdc_restore_defaults();
337 if (ccdc_cfg
.if_type
== VPFE_RAW_BAYER
)
338 ccdc_enable_vport(1);
342 static void ccdc_sbl_reset(void)
344 vpss_clear_wbl_overflow(VPSS_PCR_CCDC_WBL_O
);
347 /* Parameter operations */
348 static int ccdc_set_params(void __user
*params
)
350 struct ccdc_config_params_raw ccdc_raw_params
;
353 if (ccdc_cfg
.if_type
!= VPFE_RAW_BAYER
)
356 x
= copy_from_user(&ccdc_raw_params
, params
, sizeof(ccdc_raw_params
));
358 dev_dbg(ccdc_cfg
.dev
, "ccdc_set_params: error in copying"
359 "ccdc params, %d\n", x
);
363 if (!validate_ccdc_param(&ccdc_raw_params
)) {
364 if (!ccdc_update_raw_params(&ccdc_raw_params
))
371 * ccdc_config_ycbcr()
372 * This function will configure CCDC for YCbCr video capture
374 void ccdc_config_ycbcr(void)
376 struct ccdc_params_ycbcr
*params
= &ccdc_cfg
.ycbcr
;
379 dev_dbg(ccdc_cfg
.dev
, "\nStarting ccdc_config_ycbcr...");
381 * first restore the CCDC registers to default values
382 * This is important since we assume default values to be set in
383 * a lot of registers that we didn't touch
385 ccdc_restore_defaults();
388 * configure pixel format, frame format, configure video frame
389 * format, enable output to SDRAM, enable internal timing generator
392 syn_mode
= (((params
->pix_fmt
& CCDC_SYN_MODE_INPMOD_MASK
) <<
393 CCDC_SYN_MODE_INPMOD_SHIFT
) |
394 ((params
->frm_fmt
& CCDC_SYN_FLDMODE_MASK
) <<
395 CCDC_SYN_FLDMODE_SHIFT
) | CCDC_VDHDEN_ENABLE
|
396 CCDC_WEN_ENABLE
| CCDC_DATA_PACK_ENABLE
);
398 /* setup BT.656 sync mode */
399 if (params
->bt656_enable
) {
400 regw(CCDC_REC656IF_BT656_EN
, CCDC_REC656IF
);
403 * configure the FID, VD, HD pin polarity,
404 * fld,hd pol positive, vd negative, 8-bit data
406 syn_mode
|= CCDC_SYN_MODE_VD_POL_NEGATIVE
;
407 if (ccdc_cfg
.if_type
== VPFE_BT656_10BIT
)
408 syn_mode
|= CCDC_SYN_MODE_10BITS
;
410 syn_mode
|= CCDC_SYN_MODE_8BITS
;
412 /* y/c external sync mode */
413 syn_mode
|= (((params
->fid_pol
& CCDC_FID_POL_MASK
) <<
414 CCDC_FID_POL_SHIFT
) |
415 ((params
->hd_pol
& CCDC_HD_POL_MASK
) <<
417 ((params
->vd_pol
& CCDC_VD_POL_MASK
) <<
420 regw(syn_mode
, CCDC_SYN_MODE
);
422 /* configure video window */
423 ccdc_setwin(¶ms
->win
, params
->frm_fmt
, 2);
426 * configure the order of y cb cr in SDRAM, and disable latch
427 * internal register on vsync
429 if (ccdc_cfg
.if_type
== VPFE_BT656_10BIT
)
430 regw((params
->pix_order
<< CCDC_CCDCFG_Y8POS_SHIFT
) |
431 CCDC_LATCH_ON_VSYNC_DISABLE
| CCDC_CCDCFG_BW656_10BIT
,
434 regw((params
->pix_order
<< CCDC_CCDCFG_Y8POS_SHIFT
) |
435 CCDC_LATCH_ON_VSYNC_DISABLE
, CCDC_CCDCFG
);
438 * configure the horizontal line offset. This should be a
439 * on 32 byte boundary. So clear LSB 5 bits
441 regw(((params
->win
.width
* 2 + 31) & ~0x1f), CCDC_HSIZE_OFF
);
443 /* configure the memory line offset */
444 if (params
->buf_type
== CCDC_BUFTYPE_FLD_INTERLEAVED
)
445 /* two fields are interleaved in memory */
446 regw(CCDC_SDOFST_FIELD_INTERLEAVED
, CCDC_SDOFST
);
449 dev_dbg(ccdc_cfg
.dev
, "\nEnd of ccdc_config_ycbcr...\n");
452 static void ccdc_config_black_clamp(struct ccdc_black_clamp
*bclamp
)
456 if (!bclamp
->enable
) {
457 /* configure DCSub */
458 val
= (bclamp
->dc_sub
) & CCDC_BLK_DC_SUB_MASK
;
459 regw(val
, CCDC_DCSUB
);
460 dev_dbg(ccdc_cfg
.dev
, "\nWriting 0x%x to DCSUB...\n", val
);
461 regw(CCDC_CLAMP_DEFAULT_VAL
, CCDC_CLAMP
);
462 dev_dbg(ccdc_cfg
.dev
, "\nWriting 0x0000 to CLAMP...\n");
466 * Configure gain, Start pixel, No of line to be avg,
467 * No of pixel/line to be avg, & Enable the Black clamping
469 val
= ((bclamp
->sgain
& CCDC_BLK_SGAIN_MASK
) |
470 ((bclamp
->start_pixel
& CCDC_BLK_ST_PXL_MASK
) <<
471 CCDC_BLK_ST_PXL_SHIFT
) |
472 ((bclamp
->sample_ln
& CCDC_BLK_SAMPLE_LINE_MASK
) <<
473 CCDC_BLK_SAMPLE_LINE_SHIFT
) |
474 ((bclamp
->sample_pixel
& CCDC_BLK_SAMPLE_LN_MASK
) <<
475 CCDC_BLK_SAMPLE_LN_SHIFT
) | CCDC_BLK_CLAMP_ENABLE
);
476 regw(val
, CCDC_CLAMP
);
477 dev_dbg(ccdc_cfg
.dev
, "\nWriting 0x%x to CLAMP...\n", val
);
478 /* If Black clamping is enable then make dcsub 0 */
479 regw(CCDC_DCSUB_DEFAULT_VAL
, CCDC_DCSUB
);
480 dev_dbg(ccdc_cfg
.dev
, "\nWriting 0x00000000 to DCSUB...\n");
483 static void ccdc_config_black_compense(struct ccdc_black_compensation
*bcomp
)
487 val
= ((bcomp
->b
& CCDC_BLK_COMP_MASK
) |
488 ((bcomp
->gb
& CCDC_BLK_COMP_MASK
) <<
489 CCDC_BLK_COMP_GB_COMP_SHIFT
) |
490 ((bcomp
->gr
& CCDC_BLK_COMP_MASK
) <<
491 CCDC_BLK_COMP_GR_COMP_SHIFT
) |
492 ((bcomp
->r
& CCDC_BLK_COMP_MASK
) <<
493 CCDC_BLK_COMP_R_COMP_SHIFT
));
494 regw(val
, CCDC_BLKCMP
);
497 static void ccdc_config_fpc(struct ccdc_fault_pixel
*fpc
)
501 /* Initially disable FPC */
502 val
= CCDC_FPC_DISABLE
;
508 /* Configure Fault pixel if needed */
509 regw(fpc
->fpc_table_addr
, CCDC_FPC_ADDR
);
510 dev_dbg(ccdc_cfg
.dev
, "\nWriting 0x%x to FPC_ADDR...\n",
511 (fpc
->fpc_table_addr
));
512 /* Write the FPC params with FPC disable */
513 val
= fpc
->fp_num
& CCDC_FPC_FPC_NUM_MASK
;
516 dev_dbg(ccdc_cfg
.dev
, "\nWriting 0x%x to FPC...\n", val
);
517 /* read the FPC register */
518 val
= regr(CCDC_FPC
) | CCDC_FPC_ENABLE
;
520 dev_dbg(ccdc_cfg
.dev
, "\nWriting 0x%x to FPC...\n", val
);
525 * This function will configure CCDC for Raw capture mode
527 void ccdc_config_raw(void)
529 struct ccdc_params_raw
*params
= &ccdc_cfg
.bayer
;
530 struct ccdc_config_params_raw
*config_params
=
531 &ccdc_cfg
.bayer
.config_params
;
532 unsigned int syn_mode
= 0;
535 dev_dbg(ccdc_cfg
.dev
, "\nStarting ccdc_config_raw...");
538 ccdc_restore_defaults();
540 /* Disable latching function registers on VSYNC */
541 regw(CCDC_LATCH_ON_VSYNC_DISABLE
, CCDC_CCDCFG
);
544 * Configure the vertical sync polarity(SYN_MODE.VDPOL),
545 * horizontal sync polarity (SYN_MODE.HDPOL), frame id polarity
546 * (SYN_MODE.FLDPOL), frame format(progressive or interlace),
547 * data size(SYNMODE.DATSIZ), &pixel format (Input mode), output
548 * SDRAM, enable internal timing generator
551 (((params
->vd_pol
& CCDC_VD_POL_MASK
) << CCDC_VD_POL_SHIFT
) |
552 ((params
->hd_pol
& CCDC_HD_POL_MASK
) << CCDC_HD_POL_SHIFT
) |
553 ((params
->fid_pol
& CCDC_FID_POL_MASK
) << CCDC_FID_POL_SHIFT
) |
554 ((params
->frm_fmt
& CCDC_FRM_FMT_MASK
) << CCDC_FRM_FMT_SHIFT
) |
555 ((config_params
->data_sz
& CCDC_DATA_SZ_MASK
) <<
556 CCDC_DATA_SZ_SHIFT
) |
557 ((params
->pix_fmt
& CCDC_PIX_FMT_MASK
) << CCDC_PIX_FMT_SHIFT
) |
558 CCDC_WEN_ENABLE
| CCDC_VDHDEN_ENABLE
);
560 /* Enable and configure aLaw register if needed */
561 if (config_params
->alaw
.enable
) {
562 val
= ((config_params
->alaw
.gama_wd
&
563 CCDC_ALAW_GAMA_WD_MASK
) | CCDC_ALAW_ENABLE
);
564 regw(val
, CCDC_ALAW
);
565 dev_dbg(ccdc_cfg
.dev
, "\nWriting 0x%x to ALAW...\n", val
);
568 /* Configure video window */
569 ccdc_setwin(¶ms
->win
, params
->frm_fmt
, CCDC_PPC_RAW
);
571 /* Configure Black Clamp */
572 ccdc_config_black_clamp(&config_params
->blk_clamp
);
574 /* Configure Black level compensation */
575 ccdc_config_black_compense(&config_params
->blk_comp
);
577 /* Configure Fault Pixel Correction */
578 ccdc_config_fpc(&config_params
->fault_pxl
);
580 /* If data size is 8 bit then pack the data */
581 if ((config_params
->data_sz
== CCDC_DATA_8BITS
) ||
582 config_params
->alaw
.enable
)
583 syn_mode
|= CCDC_DATA_PACK_ENABLE
;
585 #ifdef CONFIG_DM644X_VIDEO_PORT_ENABLE
586 /* enable video port */
587 val
= CCDC_ENABLE_VIDEO_PORT
;
589 /* disable video port */
590 val
= CCDC_DISABLE_VIDEO_PORT
;
593 if (config_params
->data_sz
== CCDC_DATA_8BITS
)
594 val
|= (CCDC_DATA_10BITS
& CCDC_FMTCFG_VPIN_MASK
)
595 << CCDC_FMTCFG_VPIN_SHIFT
;
597 val
|= (config_params
->data_sz
& CCDC_FMTCFG_VPIN_MASK
)
598 << CCDC_FMTCFG_VPIN_SHIFT
;
599 /* Write value in FMTCFG */
600 regw(val
, CCDC_FMTCFG
);
602 dev_dbg(ccdc_cfg
.dev
, "\nWriting 0x%x to FMTCFG...\n", val
);
603 /* Configure the color pattern according to mt9t001 sensor */
604 regw(CCDC_COLPTN_VAL
, CCDC_COLPTN
);
606 dev_dbg(ccdc_cfg
.dev
, "\nWriting 0xBB11BB11 to COLPTN...\n");
608 * Configure Data formatter(Video port) pixel selection
609 * (FMT_HORZ, FMT_VERT)
611 val
= ((params
->win
.left
& CCDC_FMT_HORZ_FMTSPH_MASK
) <<
612 CCDC_FMT_HORZ_FMTSPH_SHIFT
) |
613 (params
->win
.width
& CCDC_FMT_HORZ_FMTLNH_MASK
);
614 regw(val
, CCDC_FMT_HORZ
);
616 dev_dbg(ccdc_cfg
.dev
, "\nWriting 0x%x to FMT_HORZ...\n", val
);
617 val
= (params
->win
.top
& CCDC_FMT_VERT_FMTSLV_MASK
)
618 << CCDC_FMT_VERT_FMTSLV_SHIFT
;
619 if (params
->frm_fmt
== CCDC_FRMFMT_PROGRESSIVE
)
620 val
|= (params
->win
.height
) & CCDC_FMT_VERT_FMTLNV_MASK
;
622 val
|= (params
->win
.height
>> 1) & CCDC_FMT_VERT_FMTLNV_MASK
;
624 dev_dbg(ccdc_cfg
.dev
, "\nparams->win.height 0x%x ...\n",
626 regw(val
, CCDC_FMT_VERT
);
628 dev_dbg(ccdc_cfg
.dev
, "\nWriting 0x%x to FMT_VERT...\n", val
);
630 dev_dbg(ccdc_cfg
.dev
, "\nbelow regw(val, FMT_VERT)...");
633 * Configure Horizontal offset register. If pack 8 is enabled then
634 * 1 pixel will take 1 byte
636 if ((config_params
->data_sz
== CCDC_DATA_8BITS
) ||
637 config_params
->alaw
.enable
)
638 regw((params
->win
.width
+ CCDC_32BYTE_ALIGN_VAL
) &
639 CCDC_HSIZE_OFF_MASK
, CCDC_HSIZE_OFF
);
641 /* else one pixel will take 2 byte */
642 regw(((params
->win
.width
* CCDC_TWO_BYTES_PER_PIXEL
) +
643 CCDC_32BYTE_ALIGN_VAL
) & CCDC_HSIZE_OFF_MASK
,
646 /* Set value for SDOFST */
647 if (params
->frm_fmt
== CCDC_FRMFMT_INTERLACED
) {
648 if (params
->image_invert_enable
) {
649 /* For intelace inverse mode */
650 regw(CCDC_INTERLACED_IMAGE_INVERT
, CCDC_SDOFST
);
651 dev_dbg(ccdc_cfg
.dev
, "\nWriting 0x4B6D to SDOFST..\n");
655 /* For intelace non inverse mode */
656 regw(CCDC_INTERLACED_NO_IMAGE_INVERT
, CCDC_SDOFST
);
657 dev_dbg(ccdc_cfg
.dev
, "\nWriting 0x0249 to SDOFST..\n");
659 } else if (params
->frm_fmt
== CCDC_FRMFMT_PROGRESSIVE
) {
660 regw(CCDC_PROGRESSIVE_NO_IMAGE_INVERT
, CCDC_SDOFST
);
661 dev_dbg(ccdc_cfg
.dev
, "\nWriting 0x0000 to SDOFST...\n");
665 * Configure video port pixel selection (VPOUT)
666 * Here -1 is to make the height value less than FMT_VERT.FMTLNV
668 if (params
->frm_fmt
== CCDC_FRMFMT_PROGRESSIVE
)
669 val
= (((params
->win
.height
- 1) & CCDC_VP_OUT_VERT_NUM_MASK
))
670 << CCDC_VP_OUT_VERT_NUM_SHIFT
;
673 ((((params
->win
.height
>> CCDC_INTERLACED_HEIGHT_SHIFT
) -
674 1) & CCDC_VP_OUT_VERT_NUM_MASK
)) <<
675 CCDC_VP_OUT_VERT_NUM_SHIFT
;
677 val
|= ((((params
->win
.width
))) & CCDC_VP_OUT_HORZ_NUM_MASK
)
678 << CCDC_VP_OUT_HORZ_NUM_SHIFT
;
679 val
|= (params
->win
.left
) & CCDC_VP_OUT_HORZ_ST_MASK
;
680 regw(val
, CCDC_VP_OUT
);
682 dev_dbg(ccdc_cfg
.dev
, "\nWriting 0x%x to VP_OUT...\n", val
);
683 regw(syn_mode
, CCDC_SYN_MODE
);
684 dev_dbg(ccdc_cfg
.dev
, "\nWriting 0x%x to SYN_MODE...\n", syn_mode
);
687 dev_dbg(ccdc_cfg
.dev
, "\nend of ccdc_config_raw...");
691 static int ccdc_configure(void)
693 if (ccdc_cfg
.if_type
== VPFE_RAW_BAYER
)
700 static int ccdc_set_buftype(enum ccdc_buftype buf_type
)
702 if (ccdc_cfg
.if_type
== VPFE_RAW_BAYER
)
703 ccdc_cfg
.bayer
.buf_type
= buf_type
;
705 ccdc_cfg
.ycbcr
.buf_type
= buf_type
;
709 static enum ccdc_buftype
ccdc_get_buftype(void)
711 if (ccdc_cfg
.if_type
== VPFE_RAW_BAYER
)
712 return ccdc_cfg
.bayer
.buf_type
;
713 return ccdc_cfg
.ycbcr
.buf_type
;
716 static int ccdc_enum_pix(u32
*pix
, int i
)
719 if (ccdc_cfg
.if_type
== VPFE_RAW_BAYER
) {
720 if (i
< ARRAY_SIZE(ccdc_raw_bayer_pix_formats
)) {
721 *pix
= ccdc_raw_bayer_pix_formats
[i
];
725 if (i
< ARRAY_SIZE(ccdc_raw_yuv_pix_formats
)) {
726 *pix
= ccdc_raw_yuv_pix_formats
[i
];
733 static int ccdc_set_pixel_format(u32 pixfmt
)
735 if (ccdc_cfg
.if_type
== VPFE_RAW_BAYER
) {
736 ccdc_cfg
.bayer
.pix_fmt
= CCDC_PIXFMT_RAW
;
737 if (pixfmt
== V4L2_PIX_FMT_SBGGR8
)
738 ccdc_cfg
.bayer
.config_params
.alaw
.enable
= 1;
739 else if (pixfmt
!= V4L2_PIX_FMT_SBGGR16
)
742 if (pixfmt
== V4L2_PIX_FMT_YUYV
)
743 ccdc_cfg
.ycbcr
.pix_order
= CCDC_PIXORDER_YCBYCR
;
744 else if (pixfmt
== V4L2_PIX_FMT_UYVY
)
745 ccdc_cfg
.ycbcr
.pix_order
= CCDC_PIXORDER_CBYCRY
;
752 static u32
ccdc_get_pixel_format(void)
754 struct ccdc_a_law
*alaw
= &ccdc_cfg
.bayer
.config_params
.alaw
;
757 if (ccdc_cfg
.if_type
== VPFE_RAW_BAYER
)
759 pixfmt
= V4L2_PIX_FMT_SBGGR8
;
761 pixfmt
= V4L2_PIX_FMT_SBGGR16
;
763 if (ccdc_cfg
.ycbcr
.pix_order
== CCDC_PIXORDER_YCBYCR
)
764 pixfmt
= V4L2_PIX_FMT_YUYV
;
766 pixfmt
= V4L2_PIX_FMT_UYVY
;
771 static int ccdc_set_image_window(struct v4l2_rect
*win
)
773 if (ccdc_cfg
.if_type
== VPFE_RAW_BAYER
)
774 ccdc_cfg
.bayer
.win
= *win
;
776 ccdc_cfg
.ycbcr
.win
= *win
;
780 static void ccdc_get_image_window(struct v4l2_rect
*win
)
782 if (ccdc_cfg
.if_type
== VPFE_RAW_BAYER
)
783 *win
= ccdc_cfg
.bayer
.win
;
785 *win
= ccdc_cfg
.ycbcr
.win
;
788 static unsigned int ccdc_get_line_length(void)
790 struct ccdc_config_params_raw
*config_params
=
791 &ccdc_cfg
.bayer
.config_params
;
794 if (ccdc_cfg
.if_type
== VPFE_RAW_BAYER
) {
795 if ((config_params
->alaw
.enable
) ||
796 (config_params
->data_sz
== CCDC_DATA_8BITS
))
797 len
= ccdc_cfg
.bayer
.win
.width
;
799 len
= ccdc_cfg
.bayer
.win
.width
* 2;
801 len
= ccdc_cfg
.ycbcr
.win
.width
* 2;
802 return ALIGN(len
, 32);
805 static int ccdc_set_frame_format(enum ccdc_frmfmt frm_fmt
)
807 if (ccdc_cfg
.if_type
== VPFE_RAW_BAYER
)
808 ccdc_cfg
.bayer
.frm_fmt
= frm_fmt
;
810 ccdc_cfg
.ycbcr
.frm_fmt
= frm_fmt
;
814 static enum ccdc_frmfmt
ccdc_get_frame_format(void)
816 if (ccdc_cfg
.if_type
== VPFE_RAW_BAYER
)
817 return ccdc_cfg
.bayer
.frm_fmt
;
819 return ccdc_cfg
.ycbcr
.frm_fmt
;
822 static int ccdc_getfid(void)
824 return (regr(CCDC_SYN_MODE
) >> 15) & 1;
827 /* misc operations */
828 static inline void ccdc_setfbaddr(unsigned long addr
)
830 regw(addr
& 0xffffffe0, CCDC_SDR_ADDR
);
833 static int ccdc_set_hw_if_params(struct vpfe_hw_if_param
*params
)
835 ccdc_cfg
.if_type
= params
->if_type
;
837 switch (params
->if_type
) {
839 case VPFE_YCBCR_SYNC_16
:
840 case VPFE_YCBCR_SYNC_8
:
841 case VPFE_BT656_10BIT
:
842 ccdc_cfg
.ycbcr
.vd_pol
= params
->vdpol
;
843 ccdc_cfg
.ycbcr
.hd_pol
= params
->hdpol
;
846 /* TODO add support for raw bayer here */
852 static void ccdc_save_context(void)
854 ccdc_ctx
[CCDC_PCR
>> 2] = regr(CCDC_PCR
);
855 ccdc_ctx
[CCDC_SYN_MODE
>> 2] = regr(CCDC_SYN_MODE
);
856 ccdc_ctx
[CCDC_HD_VD_WID
>> 2] = regr(CCDC_HD_VD_WID
);
857 ccdc_ctx
[CCDC_PIX_LINES
>> 2] = regr(CCDC_PIX_LINES
);
858 ccdc_ctx
[CCDC_HORZ_INFO
>> 2] = regr(CCDC_HORZ_INFO
);
859 ccdc_ctx
[CCDC_VERT_START
>> 2] = regr(CCDC_VERT_START
);
860 ccdc_ctx
[CCDC_VERT_LINES
>> 2] = regr(CCDC_VERT_LINES
);
861 ccdc_ctx
[CCDC_CULLING
>> 2] = regr(CCDC_CULLING
);
862 ccdc_ctx
[CCDC_HSIZE_OFF
>> 2] = regr(CCDC_HSIZE_OFF
);
863 ccdc_ctx
[CCDC_SDOFST
>> 2] = regr(CCDC_SDOFST
);
864 ccdc_ctx
[CCDC_SDR_ADDR
>> 2] = regr(CCDC_SDR_ADDR
);
865 ccdc_ctx
[CCDC_CLAMP
>> 2] = regr(CCDC_CLAMP
);
866 ccdc_ctx
[CCDC_DCSUB
>> 2] = regr(CCDC_DCSUB
);
867 ccdc_ctx
[CCDC_COLPTN
>> 2] = regr(CCDC_COLPTN
);
868 ccdc_ctx
[CCDC_BLKCMP
>> 2] = regr(CCDC_BLKCMP
);
869 ccdc_ctx
[CCDC_FPC
>> 2] = regr(CCDC_FPC
);
870 ccdc_ctx
[CCDC_FPC_ADDR
>> 2] = regr(CCDC_FPC_ADDR
);
871 ccdc_ctx
[CCDC_VDINT
>> 2] = regr(CCDC_VDINT
);
872 ccdc_ctx
[CCDC_ALAW
>> 2] = regr(CCDC_ALAW
);
873 ccdc_ctx
[CCDC_REC656IF
>> 2] = regr(CCDC_REC656IF
);
874 ccdc_ctx
[CCDC_CCDCFG
>> 2] = regr(CCDC_CCDCFG
);
875 ccdc_ctx
[CCDC_FMTCFG
>> 2] = regr(CCDC_FMTCFG
);
876 ccdc_ctx
[CCDC_FMT_HORZ
>> 2] = regr(CCDC_FMT_HORZ
);
877 ccdc_ctx
[CCDC_FMT_VERT
>> 2] = regr(CCDC_FMT_VERT
);
878 ccdc_ctx
[CCDC_FMT_ADDR0
>> 2] = regr(CCDC_FMT_ADDR0
);
879 ccdc_ctx
[CCDC_FMT_ADDR1
>> 2] = regr(CCDC_FMT_ADDR1
);
880 ccdc_ctx
[CCDC_FMT_ADDR2
>> 2] = regr(CCDC_FMT_ADDR2
);
881 ccdc_ctx
[CCDC_FMT_ADDR3
>> 2] = regr(CCDC_FMT_ADDR3
);
882 ccdc_ctx
[CCDC_FMT_ADDR4
>> 2] = regr(CCDC_FMT_ADDR4
);
883 ccdc_ctx
[CCDC_FMT_ADDR5
>> 2] = regr(CCDC_FMT_ADDR5
);
884 ccdc_ctx
[CCDC_FMT_ADDR6
>> 2] = regr(CCDC_FMT_ADDR6
);
885 ccdc_ctx
[CCDC_FMT_ADDR7
>> 2] = regr(CCDC_FMT_ADDR7
);
886 ccdc_ctx
[CCDC_PRGEVEN_0
>> 2] = regr(CCDC_PRGEVEN_0
);
887 ccdc_ctx
[CCDC_PRGEVEN_1
>> 2] = regr(CCDC_PRGEVEN_1
);
888 ccdc_ctx
[CCDC_PRGODD_0
>> 2] = regr(CCDC_PRGODD_0
);
889 ccdc_ctx
[CCDC_PRGODD_1
>> 2] = regr(CCDC_PRGODD_1
);
890 ccdc_ctx
[CCDC_VP_OUT
>> 2] = regr(CCDC_VP_OUT
);
893 static void ccdc_restore_context(void)
895 regw(ccdc_ctx
[CCDC_SYN_MODE
>> 2], CCDC_SYN_MODE
);
896 regw(ccdc_ctx
[CCDC_HD_VD_WID
>> 2], CCDC_HD_VD_WID
);
897 regw(ccdc_ctx
[CCDC_PIX_LINES
>> 2], CCDC_PIX_LINES
);
898 regw(ccdc_ctx
[CCDC_HORZ_INFO
>> 2], CCDC_HORZ_INFO
);
899 regw(ccdc_ctx
[CCDC_VERT_START
>> 2], CCDC_VERT_START
);
900 regw(ccdc_ctx
[CCDC_VERT_LINES
>> 2], CCDC_VERT_LINES
);
901 regw(ccdc_ctx
[CCDC_CULLING
>> 2], CCDC_CULLING
);
902 regw(ccdc_ctx
[CCDC_HSIZE_OFF
>> 2], CCDC_HSIZE_OFF
);
903 regw(ccdc_ctx
[CCDC_SDOFST
>> 2], CCDC_SDOFST
);
904 regw(ccdc_ctx
[CCDC_SDR_ADDR
>> 2], CCDC_SDR_ADDR
);
905 regw(ccdc_ctx
[CCDC_CLAMP
>> 2], CCDC_CLAMP
);
906 regw(ccdc_ctx
[CCDC_DCSUB
>> 2], CCDC_DCSUB
);
907 regw(ccdc_ctx
[CCDC_COLPTN
>> 2], CCDC_COLPTN
);
908 regw(ccdc_ctx
[CCDC_BLKCMP
>> 2], CCDC_BLKCMP
);
909 regw(ccdc_ctx
[CCDC_FPC
>> 2], CCDC_FPC
);
910 regw(ccdc_ctx
[CCDC_FPC_ADDR
>> 2], CCDC_FPC_ADDR
);
911 regw(ccdc_ctx
[CCDC_VDINT
>> 2], CCDC_VDINT
);
912 regw(ccdc_ctx
[CCDC_ALAW
>> 2], CCDC_ALAW
);
913 regw(ccdc_ctx
[CCDC_REC656IF
>> 2], CCDC_REC656IF
);
914 regw(ccdc_ctx
[CCDC_CCDCFG
>> 2], CCDC_CCDCFG
);
915 regw(ccdc_ctx
[CCDC_FMTCFG
>> 2], CCDC_FMTCFG
);
916 regw(ccdc_ctx
[CCDC_FMT_HORZ
>> 2], CCDC_FMT_HORZ
);
917 regw(ccdc_ctx
[CCDC_FMT_VERT
>> 2], CCDC_FMT_VERT
);
918 regw(ccdc_ctx
[CCDC_FMT_ADDR0
>> 2], CCDC_FMT_ADDR0
);
919 regw(ccdc_ctx
[CCDC_FMT_ADDR1
>> 2], CCDC_FMT_ADDR1
);
920 regw(ccdc_ctx
[CCDC_FMT_ADDR2
>> 2], CCDC_FMT_ADDR2
);
921 regw(ccdc_ctx
[CCDC_FMT_ADDR3
>> 2], CCDC_FMT_ADDR3
);
922 regw(ccdc_ctx
[CCDC_FMT_ADDR4
>> 2], CCDC_FMT_ADDR4
);
923 regw(ccdc_ctx
[CCDC_FMT_ADDR5
>> 2], CCDC_FMT_ADDR5
);
924 regw(ccdc_ctx
[CCDC_FMT_ADDR6
>> 2], CCDC_FMT_ADDR6
);
925 regw(ccdc_ctx
[CCDC_FMT_ADDR7
>> 2], CCDC_FMT_ADDR7
);
926 regw(ccdc_ctx
[CCDC_PRGEVEN_0
>> 2], CCDC_PRGEVEN_0
);
927 regw(ccdc_ctx
[CCDC_PRGEVEN_1
>> 2], CCDC_PRGEVEN_1
);
928 regw(ccdc_ctx
[CCDC_PRGODD_0
>> 2], CCDC_PRGODD_0
);
929 regw(ccdc_ctx
[CCDC_PRGODD_1
>> 2], CCDC_PRGODD_1
);
930 regw(ccdc_ctx
[CCDC_VP_OUT
>> 2], CCDC_VP_OUT
);
931 regw(ccdc_ctx
[CCDC_PCR
>> 2], CCDC_PCR
);
933 static struct ccdc_hw_device ccdc_hw_dev
= {
934 .name
= "DM6446 CCDC",
935 .owner
= THIS_MODULE
,
939 .reset
= ccdc_sbl_reset
,
940 .enable
= ccdc_enable
,
941 .set_hw_if_params
= ccdc_set_hw_if_params
,
942 .set_params
= ccdc_set_params
,
943 .configure
= ccdc_configure
,
944 .set_buftype
= ccdc_set_buftype
,
945 .get_buftype
= ccdc_get_buftype
,
946 .enum_pix
= ccdc_enum_pix
,
947 .set_pixel_format
= ccdc_set_pixel_format
,
948 .get_pixel_format
= ccdc_get_pixel_format
,
949 .set_frame_format
= ccdc_set_frame_format
,
950 .get_frame_format
= ccdc_get_frame_format
,
951 .set_image_window
= ccdc_set_image_window
,
952 .get_image_window
= ccdc_get_image_window
,
953 .get_line_length
= ccdc_get_line_length
,
954 .setfbaddr
= ccdc_setfbaddr
,
955 .getfid
= ccdc_getfid
,
959 static int __init
dm644x_ccdc_probe(struct platform_device
*pdev
)
961 struct resource
*res
;
965 * first try to register with vpfe. If not correct platform, then we
966 * don't have to iomap
968 status
= vpfe_register_ccdc_device(&ccdc_hw_dev
);
972 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
978 res
= request_mem_region(res
->start
, resource_size(res
), res
->name
);
984 ccdc_cfg
.base_addr
= ioremap_nocache(res
->start
, resource_size(res
));
985 if (!ccdc_cfg
.base_addr
) {
990 /* Get and enable Master clock */
991 ccdc_cfg
.mclk
= clk_get(&pdev
->dev
, "master");
992 if (IS_ERR(ccdc_cfg
.mclk
)) {
993 status
= PTR_ERR(ccdc_cfg
.mclk
);
996 if (clk_enable(ccdc_cfg
.mclk
)) {
1001 /* Get and enable Slave clock */
1002 ccdc_cfg
.sclk
= clk_get(&pdev
->dev
, "slave");
1003 if (IS_ERR(ccdc_cfg
.sclk
)) {
1004 status
= PTR_ERR(ccdc_cfg
.sclk
);
1007 if (clk_enable(ccdc_cfg
.sclk
)) {
1011 ccdc_cfg
.dev
= &pdev
->dev
;
1012 printk(KERN_NOTICE
"%s is registered with vpfe.\n", ccdc_hw_dev
.name
);
1015 clk_put(ccdc_cfg
.sclk
);
1017 clk_put(ccdc_cfg
.mclk
);
1019 iounmap(ccdc_cfg
.base_addr
);
1021 release_mem_region(res
->start
, resource_size(res
));
1023 vpfe_unregister_ccdc_device(&ccdc_hw_dev
);
1027 static int dm644x_ccdc_remove(struct platform_device
*pdev
)
1029 struct resource
*res
;
1031 clk_put(ccdc_cfg
.mclk
);
1032 clk_put(ccdc_cfg
.sclk
);
1033 iounmap(ccdc_cfg
.base_addr
);
1034 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1036 release_mem_region(res
->start
, resource_size(res
));
1037 vpfe_unregister_ccdc_device(&ccdc_hw_dev
);
1041 static int dm644x_ccdc_suspend(struct device
*dev
)
1043 /* Save CCDC context */
1044 ccdc_save_context();
1047 /* Disable both master and slave clock */
1048 clk_disable(ccdc_cfg
.mclk
);
1049 clk_disable(ccdc_cfg
.sclk
);
1054 static int dm644x_ccdc_resume(struct device
*dev
)
1056 /* Enable both master and slave clock */
1057 clk_enable(ccdc_cfg
.mclk
);
1058 clk_enable(ccdc_cfg
.sclk
);
1059 /* Restore CCDC context */
1060 ccdc_restore_context();
1065 static const struct dev_pm_ops dm644x_ccdc_pm_ops
= {
1066 .suspend
= dm644x_ccdc_suspend
,
1067 .resume
= dm644x_ccdc_resume
,
1070 static struct platform_driver dm644x_ccdc_driver
= {
1072 .name
= "dm644x_ccdc",
1073 .owner
= THIS_MODULE
,
1074 .pm
= &dm644x_ccdc_pm_ops
,
1076 .remove
= __devexit_p(dm644x_ccdc_remove
),
1077 .probe
= dm644x_ccdc_probe
,
1080 static int __init
dm644x_ccdc_init(void)
1082 return platform_driver_register(&dm644x_ccdc_driver
);
1085 static void __exit
dm644x_ccdc_exit(void)
1087 platform_driver_unregister(&dm644x_ccdc_driver
);
1090 module_init(dm644x_ccdc_init
);
1091 module_exit(dm644x_ccdc_exit
);