dm644x_ccdc.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  1. /*
  2. * Copyright (C) 2006-2009 Texas Instruments Inc
  3. *
  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.
  8. *
  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.
  13. *
  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
  17. *
  18. * CCDC hardware module for DM6446
  19. * ------------------------------
  20. *
  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.
  31. *
  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
  36. */
  37. #include <linux/platform_device.h>
  38. #include <linux/uaccess.h>
  39. #include <linux/videodev2.h>
  40. #include <linux/clk.h>
  41. #include <linux/err.h>
  42. #include <media/davinci/dm644x_ccdc.h>
  43. #include <media/davinci/vpss.h>
  44. #include "dm644x_ccdc_regs.h"
  45. #include "ccdc_hw_device.h"
  46. MODULE_LICENSE("GPL");
  47. MODULE_DESCRIPTION("CCDC Driver for DM6446");
  48. MODULE_AUTHOR("Texas Instruments");
  49. static struct ccdc_oper_config {
  50. struct device *dev;
  51. /* CCDC interface type */
  52. enum vpfe_hw_if_type if_type;
  53. /* Raw Bayer configuration */
  54. struct ccdc_params_raw bayer;
  55. /* YCbCr configuration */
  56. struct ccdc_params_ycbcr ycbcr;
  57. /* Master clock */
  58. struct clk *mclk;
  59. /* slave clock */
  60. struct clk *sclk;
  61. /* ccdc base address */
  62. void __iomem *base_addr;
  63. } ccdc_cfg = {
  64. /* Raw configurations */
  65. .bayer = {
  66. .pix_fmt = CCDC_PIXFMT_RAW,
  67. .frm_fmt = CCDC_FRMFMT_PROGRESSIVE,
  68. .win = CCDC_WIN_VGA,
  69. .fid_pol = VPFE_PINPOL_POSITIVE,
  70. .vd_pol = VPFE_PINPOL_POSITIVE,
  71. .hd_pol = VPFE_PINPOL_POSITIVE,
  72. .config_params = {
  73. .data_sz = CCDC_DATA_10BITS,
  74. },
  75. },
  76. .ycbcr = {
  77. .pix_fmt = CCDC_PIXFMT_YCBCR_8BIT,
  78. .frm_fmt = CCDC_FRMFMT_INTERLACED,
  79. .win = CCDC_WIN_PAL,
  80. .fid_pol = VPFE_PINPOL_POSITIVE,
  81. .vd_pol = VPFE_PINPOL_POSITIVE,
  82. .hd_pol = VPFE_PINPOL_POSITIVE,
  83. .bt656_enable = 1,
  84. .pix_order = CCDC_PIXORDER_CBYCRY,
  85. .buf_type = CCDC_BUFTYPE_FLD_INTERLEAVED
  86. },
  87. };
  88. #define CCDC_MAX_RAW_YUV_FORMATS 2
  89. /* Raw Bayer formats */
  90. static u32 ccdc_raw_bayer_pix_formats[] =
  91. {V4L2_PIX_FMT_SBGGR8, V4L2_PIX_FMT_SBGGR16};
  92. /* Raw YUV formats */
  93. static u32 ccdc_raw_yuv_pix_formats[] =
  94. {V4L2_PIX_FMT_UYVY, V4L2_PIX_FMT_YUYV};
  95. /* register access routines */
  96. static inline u32 regr(u32 offset)
  97. {
  98. return __raw_readl(ccdc_cfg.base_addr + offset);
  99. }
  100. static inline void regw(u32 val, u32 offset)
  101. {
  102. __raw_writel(val, ccdc_cfg.base_addr + offset);
  103. }
  104. static void ccdc_enable(int flag)
  105. {
  106. regw(flag, CCDC_PCR);
  107. }
  108. static void ccdc_enable_vport(int flag)
  109. {
  110. if (flag)
  111. /* enable video port */
  112. regw(CCDC_ENABLE_VIDEO_PORT, CCDC_FMTCFG);
  113. else
  114. regw(CCDC_DISABLE_VIDEO_PORT, CCDC_FMTCFG);
  115. }
  116. /*
  117. * ccdc_setwin()
  118. * This function will configure the window size
  119. * to be capture in CCDC reg
  120. */
  121. void ccdc_setwin(struct v4l2_rect *image_win,
  122. enum ccdc_frmfmt frm_fmt,
  123. int ppc)
  124. {
  125. int horz_start, horz_nr_pixels;
  126. int vert_start, vert_nr_lines;
  127. int val = 0, mid_img = 0;
  128. dev_dbg(ccdc_cfg.dev, "\nStarting ccdc_setwin...");
  129. /*
  130. * ppc - per pixel count. indicates how many pixels per cell
  131. * output to SDRAM. example, for ycbcr, it is one y and one c, so 2.
  132. * raw capture this is 1
  133. */
  134. horz_start = image_win->left << (ppc - 1);
  135. horz_nr_pixels = (image_win->width << (ppc - 1)) - 1;
  136. regw((horz_start << CCDC_HORZ_INFO_SPH_SHIFT) | horz_nr_pixels,
  137. CCDC_HORZ_INFO);
  138. vert_start = image_win->top;
  139. if (frm_fmt == CCDC_FRMFMT_INTERLACED) {
  140. vert_nr_lines = (image_win->height >> 1) - 1;
  141. vert_start >>= 1;
  142. /* Since first line doesn't have any data */
  143. vert_start += 1;
  144. /* configure VDINT0 */
  145. val = (vert_start << CCDC_VDINT_VDINT0_SHIFT);
  146. regw(val, CCDC_VDINT);
  147. } else {
  148. /* Since first line doesn't have any data */
  149. vert_start += 1;
  150. vert_nr_lines = image_win->height - 1;
  151. /*
  152. * configure VDINT0 and VDINT1. VDINT1 will be at half
  153. * of image height
  154. */
  155. mid_img = vert_start + (image_win->height / 2);
  156. val = (vert_start << CCDC_VDINT_VDINT0_SHIFT) |
  157. (mid_img & CCDC_VDINT_VDINT1_MASK);
  158. regw(val, CCDC_VDINT);
  159. }
  160. regw((vert_start << CCDC_VERT_START_SLV0_SHIFT) | vert_start,
  161. CCDC_VERT_START);
  162. regw(vert_nr_lines, CCDC_VERT_LINES);
  163. dev_dbg(ccdc_cfg.dev, "\nEnd of ccdc_setwin...");
  164. }
  165. static void ccdc_readregs(void)
  166. {
  167. unsigned int val = 0;
  168. val = regr(CCDC_ALAW);
  169. dev_notice(ccdc_cfg.dev, "\nReading 0x%x to ALAW...\n", val);
  170. val = regr(CCDC_CLAMP);
  171. dev_notice(ccdc_cfg.dev, "\nReading 0x%x to CLAMP...\n", val);
  172. val = regr(CCDC_DCSUB);
  173. dev_notice(ccdc_cfg.dev, "\nReading 0x%x to DCSUB...\n", val);
  174. val = regr(CCDC_BLKCMP);
  175. dev_notice(ccdc_cfg.dev, "\nReading 0x%x to BLKCMP...\n", val);
  176. val = regr(CCDC_FPC_ADDR);
  177. dev_notice(ccdc_cfg.dev, "\nReading 0x%x to FPC_ADDR...\n", val);
  178. val = regr(CCDC_FPC);
  179. dev_notice(ccdc_cfg.dev, "\nReading 0x%x to FPC...\n", val);
  180. val = regr(CCDC_FMTCFG);
  181. dev_notice(ccdc_cfg.dev, "\nReading 0x%x to FMTCFG...\n", val);
  182. val = regr(CCDC_COLPTN);
  183. dev_notice(ccdc_cfg.dev, "\nReading 0x%x to COLPTN...\n", val);
  184. val = regr(CCDC_FMT_HORZ);
  185. dev_notice(ccdc_cfg.dev, "\nReading 0x%x to FMT_HORZ...\n", val);
  186. val = regr(CCDC_FMT_VERT);
  187. dev_notice(ccdc_cfg.dev, "\nReading 0x%x to FMT_VERT...\n", val);
  188. val = regr(CCDC_HSIZE_OFF);
  189. dev_notice(ccdc_cfg.dev, "\nReading 0x%x to HSIZE_OFF...\n", val);
  190. val = regr(CCDC_SDOFST);
  191. dev_notice(ccdc_cfg.dev, "\nReading 0x%x to SDOFST...\n", val);
  192. val = regr(CCDC_VP_OUT);
  193. dev_notice(ccdc_cfg.dev, "\nReading 0x%x to VP_OUT...\n", val);
  194. val = regr(CCDC_SYN_MODE);
  195. dev_notice(ccdc_cfg.dev, "\nReading 0x%x to SYN_MODE...\n", val);
  196. val = regr(CCDC_HORZ_INFO);
  197. dev_notice(ccdc_cfg.dev, "\nReading 0x%x to HORZ_INFO...\n", val);
  198. val = regr(CCDC_VERT_START);
  199. dev_notice(ccdc_cfg.dev, "\nReading 0x%x to VERT_START...\n", val);
  200. val = regr(CCDC_VERT_LINES);
  201. dev_notice(ccdc_cfg.dev, "\nReading 0x%x to VERT_LINES...\n", val);
  202. }
  203. static int validate_ccdc_param(struct ccdc_config_params_raw *ccdcparam)
  204. {
  205. if (ccdcparam->alaw.enable) {
  206. if ((ccdcparam->alaw.gama_wd > CCDC_GAMMA_BITS_09_0) ||
  207. (ccdcparam->alaw.gama_wd < CCDC_GAMMA_BITS_15_6) ||
  208. (ccdcparam->alaw.gama_wd < ccdcparam->data_sz)) {
  209. dev_dbg(ccdc_cfg.dev, "\nInvalid data line select");
  210. return -1;
  211. }
  212. }
  213. return 0;
  214. }
  215. static int ccdc_update_raw_params(struct ccdc_config_params_raw *raw_params)
  216. {
  217. struct ccdc_config_params_raw *config_params =
  218. &ccdc_cfg.bayer.config_params;
  219. unsigned int *fpc_virtaddr = NULL;
  220. unsigned int *fpc_physaddr = NULL;
  221. memcpy(config_params, raw_params, sizeof(*raw_params));
  222. /*
  223. * allocate memory for fault pixel table and copy the user
  224. * values to the table
  225. */
  226. if (!config_params->fault_pxl.enable)
  227. return 0;
  228. fpc_physaddr = (unsigned int *)config_params->fault_pxl.fpc_table_addr;
  229. fpc_virtaddr = (unsigned int *)phys_to_virt(
  230. (unsigned long)fpc_physaddr);
  231. /*
  232. * Allocate memory for FPC table if current
  233. * FPC table buffer is not big enough to
  234. * accomodate FPC Number requested
  235. */
  236. if (raw_params->fault_pxl.fp_num != config_params->fault_pxl.fp_num) {
  237. if (fpc_physaddr != NULL) {
  238. free_pages((unsigned long)fpc_physaddr,
  239. get_order
  240. (config_params->fault_pxl.fp_num *
  241. FP_NUM_BYTES));
  242. }
  243. /* Allocate memory for FPC table */
  244. fpc_virtaddr =
  245. (unsigned int *)__get_free_pages(GFP_KERNEL | GFP_DMA,
  246. get_order(raw_params->
  247. fault_pxl.fp_num *
  248. FP_NUM_BYTES));
  249. if (fpc_virtaddr == NULL) {
  250. dev_dbg(ccdc_cfg.dev,
  251. "\nUnable to allocate memory for FPC");
  252. return -EFAULT;
  253. }
  254. fpc_physaddr =
  255. (unsigned int *)virt_to_phys((void *)fpc_virtaddr);
  256. }
  257. /* Copy number of fault pixels and FPC table */
  258. config_params->fault_pxl.fp_num = raw_params->fault_pxl.fp_num;
  259. if (copy_from_user(fpc_virtaddr,
  260. (void __user *)raw_params->fault_pxl.fpc_table_addr,
  261. config_params->fault_pxl.fp_num * FP_NUM_BYTES)) {
  262. dev_dbg(ccdc_cfg.dev, "\n copy_from_user failed");
  263. return -EFAULT;
  264. }
  265. config_params->fault_pxl.fpc_table_addr = (unsigned int)fpc_physaddr;
  266. return 0;
  267. }
  268. static int ccdc_close(struct device *dev)
  269. {
  270. struct ccdc_config_params_raw *config_params =
  271. &ccdc_cfg.bayer.config_params;
  272. unsigned int *fpc_physaddr = NULL, *fpc_virtaddr = NULL;
  273. fpc_physaddr = (unsigned int *)config_params->fault_pxl.fpc_table_addr;
  274. if (fpc_physaddr != NULL) {
  275. fpc_virtaddr = (unsigned int *)
  276. phys_to_virt((unsigned long)fpc_physaddr);
  277. free_pages((unsigned long)fpc_virtaddr,
  278. get_order(config_params->fault_pxl.fp_num *
  279. FP_NUM_BYTES));
  280. }
  281. return 0;
  282. }
  283. /*
  284. * ccdc_restore_defaults()
  285. * This function will write defaults to all CCDC registers
  286. */
  287. static void ccdc_restore_defaults(void)
  288. {
  289. int i;
  290. /* disable CCDC */
  291. ccdc_enable(0);
  292. /* set all registers to default value */
  293. for (i = 4; i <= 0x94; i += 4)
  294. regw(0, i);
  295. regw(CCDC_NO_CULLING, CCDC_CULLING);
  296. regw(CCDC_GAMMA_BITS_11_2, CCDC_ALAW);
  297. }
  298. static int ccdc_open(struct device *device)
  299. {
  300. ccdc_restore_defaults();
  301. if (ccdc_cfg.if_type == VPFE_RAW_BAYER)
  302. ccdc_enable_vport(1);
  303. return 0;
  304. }
  305. static void ccdc_sbl_reset(void)
  306. {
  307. vpss_clear_wbl_overflow(VPSS_PCR_CCDC_WBL_O);
  308. }
  309. /* Parameter operations */
  310. static int ccdc_set_params(void __user *params)
  311. {
  312. struct ccdc_config_params_raw ccdc_raw_params;
  313. int x;
  314. if (ccdc_cfg.if_type != VPFE_RAW_BAYER)
  315. return -EINVAL;
  316. x = copy_from_user(&ccdc_raw_params, params, sizeof(ccdc_raw_params));
  317. if (x) {
  318. dev_dbg(ccdc_cfg.dev, "ccdc_set_params: error in copying"
  319. "ccdc params, %d\n", x);
  320. return -EFAULT;
  321. }
  322. if (!validate_ccdc_param(&ccdc_raw_params)) {
  323. if (!ccdc_update_raw_params(&ccdc_raw_params))
  324. return 0;
  325. }
  326. return -EINVAL;
  327. }
  328. /*
  329. * ccdc_config_ycbcr()
  330. * This function will configure CCDC for YCbCr video capture
  331. */
  332. void ccdc_config_ycbcr(void)
  333. {
  334. struct ccdc_params_ycbcr *params = &ccdc_cfg.ycbcr;
  335. u32 syn_mode;
  336. dev_dbg(ccdc_cfg.dev, "\nStarting ccdc_config_ycbcr...");
  337. /*
  338. * first restore the CCDC registers to default values
  339. * This is important since we assume default values to be set in
  340. * a lot of registers that we didn't touch
  341. */
  342. ccdc_restore_defaults();
  343. /*
  344. * configure pixel format, frame format, configure video frame
  345. * format, enable output to SDRAM, enable internal timing generator
  346. * and 8bit pack mode
  347. */
  348. syn_mode = (((params->pix_fmt & CCDC_SYN_MODE_INPMOD_MASK) <<
  349. CCDC_SYN_MODE_INPMOD_SHIFT) |
  350. ((params->frm_fmt & CCDC_SYN_FLDMODE_MASK) <<
  351. CCDC_SYN_FLDMODE_SHIFT) | CCDC_VDHDEN_ENABLE |
  352. CCDC_WEN_ENABLE | CCDC_DATA_PACK_ENABLE);
  353. /* setup BT.656 sync mode */
  354. if (params->bt656_enable) {
  355. regw(CCDC_REC656IF_BT656_EN, CCDC_REC656IF);
  356. /*
  357. * configure the FID, VD, HD pin polarity,
  358. * fld,hd pol positive, vd negative, 8-bit data
  359. */
  360. syn_mode |= CCDC_SYN_MODE_VD_POL_NEGATIVE | CCDC_SYN_MODE_8BITS;
  361. } else {
  362. /* y/c external sync mode */
  363. syn_mode |= (((params->fid_pol & CCDC_FID_POL_MASK) <<
  364. CCDC_FID_POL_SHIFT) |
  365. ((params->hd_pol & CCDC_HD_POL_MASK) <<
  366. CCDC_HD_POL_SHIFT) |
  367. ((params->vd_pol & CCDC_VD_POL_MASK) <<
  368. CCDC_VD_POL_SHIFT));
  369. }
  370. regw(syn_mode, CCDC_SYN_MODE);
  371. /* configure video window */
  372. ccdc_setwin(&params->win, params->frm_fmt, 2);
  373. /*
  374. * configure the order of y cb cr in SDRAM, and disable latch
  375. * internal register on vsync
  376. */
  377. regw((params->pix_order << CCDC_CCDCFG_Y8POS_SHIFT) |
  378. CCDC_LATCH_ON_VSYNC_DISABLE, CCDC_CCDCFG);
  379. /*
  380. * configure the horizontal line offset. This should be a
  381. * on 32 byte bondary. So clear LSB 5 bits
  382. */
  383. regw(((params->win.width * 2 + 31) & ~0x1f), CCDC_HSIZE_OFF);
  384. /* configure the memory line offset */
  385. if (params->buf_type == CCDC_BUFTYPE_FLD_INTERLEAVED)
  386. /* two fields are interleaved in memory */
  387. regw(CCDC_SDOFST_FIELD_INTERLEAVED, CCDC_SDOFST);
  388. ccdc_sbl_reset();
  389. dev_dbg(ccdc_cfg.dev, "\nEnd of ccdc_config_ycbcr...\n");
  390. ccdc_readregs();
  391. }
  392. static void ccdc_config_black_clamp(struct ccdc_black_clamp *bclamp)
  393. {
  394. u32 val;
  395. if (!bclamp->enable) {
  396. /* configure DCSub */
  397. val = (bclamp->dc_sub) & CCDC_BLK_DC_SUB_MASK;
  398. regw(val, CCDC_DCSUB);
  399. dev_dbg(ccdc_cfg.dev, "\nWriting 0x%x to DCSUB...\n", val);
  400. regw(CCDC_CLAMP_DEFAULT_VAL, CCDC_CLAMP);
  401. dev_dbg(ccdc_cfg.dev, "\nWriting 0x0000 to CLAMP...\n");
  402. return;
  403. }
  404. /*
  405. * Configure gain, Start pixel, No of line to be avg,
  406. * No of pixel/line to be avg, & Enable the Black clamping
  407. */
  408. val = ((bclamp->sgain & CCDC_BLK_SGAIN_MASK) |
  409. ((bclamp->start_pixel & CCDC_BLK_ST_PXL_MASK) <<
  410. CCDC_BLK_ST_PXL_SHIFT) |
  411. ((bclamp->sample_ln & CCDC_BLK_SAMPLE_LINE_MASK) <<
  412. CCDC_BLK_SAMPLE_LINE_SHIFT) |
  413. ((bclamp->sample_pixel & CCDC_BLK_SAMPLE_LN_MASK) <<
  414. CCDC_BLK_SAMPLE_LN_SHIFT) | CCDC_BLK_CLAMP_ENABLE);
  415. regw(val, CCDC_CLAMP);
  416. dev_dbg(ccdc_cfg.dev, "\nWriting 0x%x to CLAMP...\n", val);
  417. /* If Black clamping is enable then make dcsub 0 */
  418. regw(CCDC_DCSUB_DEFAULT_VAL, CCDC_DCSUB);
  419. dev_dbg(ccdc_cfg.dev, "\nWriting 0x00000000 to DCSUB...\n");
  420. }
  421. static void ccdc_config_black_compense(struct ccdc_black_compensation *bcomp)
  422. {
  423. u32 val;
  424. val = ((bcomp->b & CCDC_BLK_COMP_MASK) |
  425. ((bcomp->gb & CCDC_BLK_COMP_MASK) <<
  426. CCDC_BLK_COMP_GB_COMP_SHIFT) |
  427. ((bcomp->gr & CCDC_BLK_COMP_MASK) <<
  428. CCDC_BLK_COMP_GR_COMP_SHIFT) |
  429. ((bcomp->r & CCDC_BLK_COMP_MASK) <<
  430. CCDC_BLK_COMP_R_COMP_SHIFT));
  431. regw(val, CCDC_BLKCMP);
  432. }
  433. static void ccdc_config_fpc(struct ccdc_fault_pixel *fpc)
  434. {
  435. u32 val;
  436. /* Initially disable FPC */
  437. val = CCDC_FPC_DISABLE;
  438. regw(val, CCDC_FPC);
  439. if (!fpc->enable)
  440. return;
  441. /* Configure Fault pixel if needed */
  442. regw(fpc->fpc_table_addr, CCDC_FPC_ADDR);
  443. dev_dbg(ccdc_cfg.dev, "\nWriting 0x%x to FPC_ADDR...\n",
  444. (fpc->fpc_table_addr));
  445. /* Write the FPC params with FPC disable */
  446. val = fpc->fp_num & CCDC_FPC_FPC_NUM_MASK;
  447. regw(val, CCDC_FPC);
  448. dev_dbg(ccdc_cfg.dev, "\nWriting 0x%x to FPC...\n", val);
  449. /* read the FPC register */
  450. val = regr(CCDC_FPC) | CCDC_FPC_ENABLE;
  451. regw(val, CCDC_FPC);
  452. dev_dbg(ccdc_cfg.dev, "\nWriting 0x%x to FPC...\n", val);
  453. }
  454. /*
  455. * ccdc_config_raw()
  456. * This function will configure CCDC for Raw capture mode
  457. */
  458. void ccdc_config_raw(void)
  459. {
  460. struct ccdc_params_raw *params = &ccdc_cfg.bayer;
  461. struct ccdc_config_params_raw *config_params =
  462. &ccdc_cfg.bayer.config_params;
  463. unsigned int syn_mode = 0;
  464. unsigned int val;
  465. dev_dbg(ccdc_cfg.dev, "\nStarting ccdc_config_raw...");
  466. /* Reset CCDC */
  467. ccdc_restore_defaults();
  468. /* Disable latching function registers on VSYNC */
  469. regw(CCDC_LATCH_ON_VSYNC_DISABLE, CCDC_CCDCFG);
  470. /*
  471. * Configure the vertical sync polarity(SYN_MODE.VDPOL),
  472. * horizontal sync polarity (SYN_MODE.HDPOL), frame id polarity
  473. * (SYN_MODE.FLDPOL), frame format(progressive or interlace),
  474. * data size(SYNMODE.DATSIZ), &pixel format (Input mode), output
  475. * SDRAM, enable internal timing generator
  476. */
  477. syn_mode =
  478. (((params->vd_pol & CCDC_VD_POL_MASK) << CCDC_VD_POL_SHIFT) |
  479. ((params->hd_pol & CCDC_HD_POL_MASK) << CCDC_HD_POL_SHIFT) |
  480. ((params->fid_pol & CCDC_FID_POL_MASK) << CCDC_FID_POL_SHIFT) |
  481. ((params->frm_fmt & CCDC_FRM_FMT_MASK) << CCDC_FRM_FMT_SHIFT) |
  482. ((config_params->data_sz & CCDC_DATA_SZ_MASK) <<
  483. CCDC_DATA_SZ_SHIFT) |
  484. ((params->pix_fmt & CCDC_PIX_FMT_MASK) << CCDC_PIX_FMT_SHIFT) |
  485. CCDC_WEN_ENABLE | CCDC_VDHDEN_ENABLE);
  486. /* Enable and configure aLaw register if needed */
  487. if (config_params->alaw.enable) {
  488. val = ((config_params->alaw.gama_wd &
  489. CCDC_ALAW_GAMA_WD_MASK) | CCDC_ALAW_ENABLE);
  490. regw(val, CCDC_ALAW);
  491. dev_dbg(ccdc_cfg.dev, "\nWriting 0x%x to ALAW...\n", val);
  492. }
  493. /* Configure video window */
  494. ccdc_setwin(&params->win, params->frm_fmt, CCDC_PPC_RAW);
  495. /* Configure Black Clamp */
  496. ccdc_config_black_clamp(&config_params->blk_clamp);
  497. /* Configure Black level compensation */
  498. ccdc_config_black_compense(&config_params->blk_comp);
  499. /* Configure Fault Pixel Correction */
  500. ccdc_config_fpc(&config_params->fault_pxl);
  501. /* If data size is 8 bit then pack the data */
  502. if ((config_params->data_sz == CCDC_DATA_8BITS) ||
  503. config_params->alaw.enable)
  504. syn_mode |= CCDC_DATA_PACK_ENABLE;
  505. #ifdef CONFIG_DM644X_VIDEO_PORT_ENABLE
  506. /* enable video port */
  507. val = CCDC_ENABLE_VIDEO_PORT;
  508. #else
  509. /* disable video port */
  510. val = CCDC_DISABLE_VIDEO_PORT;
  511. #endif
  512. if (config_params->data_sz == CCDC_DATA_8BITS)
  513. val |= (CCDC_DATA_10BITS & CCDC_FMTCFG_VPIN_MASK)
  514. << CCDC_FMTCFG_VPIN_SHIFT;
  515. else
  516. val |= (config_params->data_sz & CCDC_FMTCFG_VPIN_MASK)
  517. << CCDC_FMTCFG_VPIN_SHIFT;
  518. /* Write value in FMTCFG */
  519. regw(val, CCDC_FMTCFG);
  520. dev_dbg(ccdc_cfg.dev, "\nWriting 0x%x to FMTCFG...\n", val);
  521. /* Configure the color pattern according to mt9t001 sensor */
  522. regw(CCDC_COLPTN_VAL, CCDC_COLPTN);
  523. dev_dbg(ccdc_cfg.dev, "\nWriting 0xBB11BB11 to COLPTN...\n");
  524. /*
  525. * Configure Data formatter(Video port) pixel selection
  526. * (FMT_HORZ, FMT_VERT)
  527. */
  528. val = ((params->win.left & CCDC_FMT_HORZ_FMTSPH_MASK) <<
  529. CCDC_FMT_HORZ_FMTSPH_SHIFT) |
  530. (params->win.width & CCDC_FMT_HORZ_FMTLNH_MASK);
  531. regw(val, CCDC_FMT_HORZ);
  532. dev_dbg(ccdc_cfg.dev, "\nWriting 0x%x to FMT_HORZ...\n", val);
  533. val = (params->win.top & CCDC_FMT_VERT_FMTSLV_MASK)
  534. << CCDC_FMT_VERT_FMTSLV_SHIFT;
  535. if (params->frm_fmt == CCDC_FRMFMT_PROGRESSIVE)
  536. val |= (params->win.height) & CCDC_FMT_VERT_FMTLNV_MASK;
  537. else
  538. val |= (params->win.height >> 1) & CCDC_FMT_VERT_FMTLNV_MASK;
  539. dev_dbg(ccdc_cfg.dev, "\nparams->win.height 0x%x ...\n",
  540. params->win.height);
  541. regw(val, CCDC_FMT_VERT);
  542. dev_dbg(ccdc_cfg.dev, "\nWriting 0x%x to FMT_VERT...\n", val);
  543. dev_dbg(ccdc_cfg.dev, "\nbelow regw(val, FMT_VERT)...");
  544. /*
  545. * Configure Horizontal offset register. If pack 8 is enabled then
  546. * 1 pixel will take 1 byte
  547. */
  548. if ((config_params->data_sz == CCDC_DATA_8BITS) ||
  549. config_params->alaw.enable)
  550. regw((params->win.width + CCDC_32BYTE_ALIGN_VAL) &
  551. CCDC_HSIZE_OFF_MASK, CCDC_HSIZE_OFF);
  552. else
  553. /* else one pixel will take 2 byte */
  554. regw(((params->win.width * CCDC_TWO_BYTES_PER_PIXEL) +
  555. CCDC_32BYTE_ALIGN_VAL) & CCDC_HSIZE_OFF_MASK,
  556. CCDC_HSIZE_OFF);
  557. /* Set value for SDOFST */
  558. if (params->frm_fmt == CCDC_FRMFMT_INTERLACED) {
  559. if (params->image_invert_enable) {
  560. /* For intelace inverse mode */
  561. regw(CCDC_INTERLACED_IMAGE_INVERT, CCDC_SDOFST);
  562. dev_dbg(ccdc_cfg.dev, "\nWriting 0x4B6D to SDOFST..\n");
  563. }
  564. else {
  565. /* For intelace non inverse mode */
  566. regw(CCDC_INTERLACED_NO_IMAGE_INVERT, CCDC_SDOFST);
  567. dev_dbg(ccdc_cfg.dev, "\nWriting 0x0249 to SDOFST..\n");
  568. }
  569. } else if (params->frm_fmt == CCDC_FRMFMT_PROGRESSIVE) {
  570. regw(CCDC_PROGRESSIVE_NO_IMAGE_INVERT, CCDC_SDOFST);
  571. dev_dbg(ccdc_cfg.dev, "\nWriting 0x0000 to SDOFST...\n");
  572. }
  573. /*
  574. * Configure video port pixel selection (VPOUT)
  575. * Here -1 is to make the height value less than FMT_VERT.FMTLNV
  576. */
  577. if (params->frm_fmt == CCDC_FRMFMT_PROGRESSIVE)
  578. val = (((params->win.height - 1) & CCDC_VP_OUT_VERT_NUM_MASK))
  579. << CCDC_VP_OUT_VERT_NUM_SHIFT;
  580. else
  581. val =
  582. ((((params->win.height >> CCDC_INTERLACED_HEIGHT_SHIFT) -
  583. 1) & CCDC_VP_OUT_VERT_NUM_MASK)) <<
  584. CCDC_VP_OUT_VERT_NUM_SHIFT;
  585. val |= ((((params->win.width))) & CCDC_VP_OUT_HORZ_NUM_MASK)
  586. << CCDC_VP_OUT_HORZ_NUM_SHIFT;
  587. val |= (params->win.left) & CCDC_VP_OUT_HORZ_ST_MASK;
  588. regw(val, CCDC_VP_OUT);
  589. dev_dbg(ccdc_cfg.dev, "\nWriting 0x%x to VP_OUT...\n", val);
  590. regw(syn_mode, CCDC_SYN_MODE);
  591. dev_dbg(ccdc_cfg.dev, "\nWriting 0x%x to SYN_MODE...\n", syn_mode);
  592. ccdc_sbl_reset();
  593. dev_dbg(ccdc_cfg.dev, "\nend of ccdc_config_raw...");
  594. ccdc_readregs();
  595. }
  596. static int ccdc_configure(void)
  597. {
  598. if (ccdc_cfg.if_type == VPFE_RAW_BAYER)
  599. ccdc_config_raw();
  600. else
  601. ccdc_config_ycbcr();
  602. return 0;
  603. }
  604. static int ccdc_set_buftype(enum ccdc_buftype buf_type)
  605. {
  606. if (ccdc_cfg.if_type == VPFE_RAW_BAYER)
  607. ccdc_cfg.bayer.buf_type = buf_type;
  608. else
  609. ccdc_cfg.ycbcr.buf_type = buf_type;
  610. return 0;
  611. }
  612. static enum ccdc_buftype ccdc_get_buftype(void)
  613. {
  614. if (ccdc_cfg.if_type == VPFE_RAW_BAYER)
  615. return ccdc_cfg.bayer.buf_type;
  616. return ccdc_cfg.ycbcr.buf_type;
  617. }
  618. static int ccdc_enum_pix(u32 *pix, int i)
  619. {
  620. int ret = -EINVAL;
  621. if (ccdc_cfg.if_type == VPFE_RAW_BAYER) {
  622. if (i < ARRAY_SIZE(ccdc_raw_bayer_pix_formats)) {
  623. *pix = ccdc_raw_bayer_pix_formats[i];
  624. ret = 0;
  625. }
  626. } else {
  627. if (i < ARRAY_SIZE(ccdc_raw_yuv_pix_formats)) {
  628. *pix = ccdc_raw_yuv_pix_formats[i];
  629. ret = 0;
  630. }
  631. }
  632. return ret;
  633. }
  634. static int ccdc_set_pixel_format(u32 pixfmt)
  635. {
  636. if (ccdc_cfg.if_type == VPFE_RAW_BAYER) {
  637. ccdc_cfg.bayer.pix_fmt = CCDC_PIXFMT_RAW;
  638. if (pixfmt == V4L2_PIX_FMT_SBGGR8)
  639. ccdc_cfg.bayer.config_params.alaw.enable = 1;
  640. else if (pixfmt != V4L2_PIX_FMT_SBGGR16)
  641. return -EINVAL;
  642. } else {
  643. if (pixfmt == V4L2_PIX_FMT_YUYV)
  644. ccdc_cfg.ycbcr.pix_order = CCDC_PIXORDER_YCBYCR;
  645. else if (pixfmt == V4L2_PIX_FMT_UYVY)
  646. ccdc_cfg.ycbcr.pix_order = CCDC_PIXORDER_CBYCRY;
  647. else
  648. return -EINVAL;
  649. }
  650. return 0;
  651. }
  652. static u32 ccdc_get_pixel_format(void)
  653. {
  654. struct ccdc_a_law *alaw = &ccdc_cfg.bayer.config_params.alaw;
  655. u32 pixfmt;
  656. if (ccdc_cfg.if_type == VPFE_RAW_BAYER)
  657. if (alaw->enable)
  658. pixfmt = V4L2_PIX_FMT_SBGGR8;
  659. else
  660. pixfmt = V4L2_PIX_FMT_SBGGR16;
  661. else {
  662. if (ccdc_cfg.ycbcr.pix_order == CCDC_PIXORDER_YCBYCR)
  663. pixfmt = V4L2_PIX_FMT_YUYV;
  664. else
  665. pixfmt = V4L2_PIX_FMT_UYVY;
  666. }
  667. return pixfmt;
  668. }
  669. static int ccdc_set_image_window(struct v4l2_rect *win)
  670. {
  671. if (ccdc_cfg.if_type == VPFE_RAW_BAYER)
  672. ccdc_cfg.bayer.win = *win;
  673. else
  674. ccdc_cfg.ycbcr.win = *win;
  675. return 0;
  676. }
  677. static void ccdc_get_image_window(struct v4l2_rect *win)
  678. {
  679. if (ccdc_cfg.if_type == VPFE_RAW_BAYER)
  680. *win = ccdc_cfg.bayer.win;
  681. else
  682. *win = ccdc_cfg.ycbcr.win;
  683. }
  684. static unsigned int ccdc_get_line_length(void)
  685. {
  686. struct ccdc_config_params_raw *config_params =
  687. &ccdc_cfg.bayer.config_params;
  688. unsigned int len;
  689. if (ccdc_cfg.if_type == VPFE_RAW_BAYER) {
  690. if ((config_params->alaw.enable) ||
  691. (config_params->data_sz == CCDC_DATA_8BITS))
  692. len = ccdc_cfg.bayer.win.width;
  693. else
  694. len = ccdc_cfg.bayer.win.width * 2;
  695. } else
  696. len = ccdc_cfg.ycbcr.win.width * 2;
  697. return ALIGN(len, 32);
  698. }
  699. static int ccdc_set_frame_format(enum ccdc_frmfmt frm_fmt)
  700. {
  701. if (ccdc_cfg.if_type == VPFE_RAW_BAYER)
  702. ccdc_cfg.bayer.frm_fmt = frm_fmt;
  703. else
  704. ccdc_cfg.ycbcr.frm_fmt = frm_fmt;
  705. return 0;
  706. }
  707. static enum ccdc_frmfmt ccdc_get_frame_format(void)
  708. {
  709. if (ccdc_cfg.if_type == VPFE_RAW_BAYER)
  710. return ccdc_cfg.bayer.frm_fmt;
  711. else
  712. return ccdc_cfg.ycbcr.frm_fmt;
  713. }
  714. static int ccdc_getfid(void)
  715. {
  716. return (regr(CCDC_SYN_MODE) >> 15) & 1;
  717. }
  718. /* misc operations */
  719. static inline void ccdc_setfbaddr(unsigned long addr)
  720. {
  721. regw(addr & 0xffffffe0, CCDC_SDR_ADDR);
  722. }
  723. static int ccdc_set_hw_if_params(struct vpfe_hw_if_param *params)
  724. {
  725. ccdc_cfg.if_type = params->if_type;
  726. switch (params->if_type) {
  727. case VPFE_BT656:
  728. case VPFE_YCBCR_SYNC_16:
  729. case VPFE_YCBCR_SYNC_8:
  730. ccdc_cfg.ycbcr.vd_pol = params->vdpol;
  731. ccdc_cfg.ycbcr.hd_pol = params->hdpol;
  732. break;
  733. default:
  734. /* TODO add support for raw bayer here */
  735. return -EINVAL;
  736. }
  737. return 0;
  738. }
  739. static struct ccdc_hw_device ccdc_hw_dev = {
  740. .name = "DM6446 CCDC",
  741. .owner = THIS_MODULE,
  742. .hw_ops = {
  743. .open = ccdc_open,
  744. .close = ccdc_close,
  745. .reset = ccdc_sbl_reset,
  746. .enable = ccdc_enable,
  747. .set_hw_if_params = ccdc_set_hw_if_params,
  748. .set_params = ccdc_set_params,
  749. .configure = ccdc_configure,
  750. .set_buftype = ccdc_set_buftype,
  751. .get_buftype = ccdc_get_buftype,
  752. .enum_pix = ccdc_enum_pix,
  753. .set_pixel_format = ccdc_set_pixel_format,
  754. .get_pixel_format = ccdc_get_pixel_format,
  755. .set_frame_format = ccdc_set_frame_format,
  756. .get_frame_format = ccdc_get_frame_format,
  757. .set_image_window = ccdc_set_image_window,
  758. .get_image_window = ccdc_get_image_window,
  759. .get_line_length = ccdc_get_line_length,
  760. .setfbaddr = ccdc_setfbaddr,
  761. .getfid = ccdc_getfid,
  762. },
  763. };
  764. static int __init dm644x_ccdc_probe(struct platform_device *pdev)
  765. {
  766. struct resource *res;
  767. int status = 0;
  768. /*
  769. * first try to register with vpfe. If not correct platform, then we
  770. * don't have to iomap
  771. */
  772. status = vpfe_register_ccdc_device(&ccdc_hw_dev);
  773. if (status < 0)
  774. return status;
  775. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  776. if (!res) {
  777. status = -ENODEV;
  778. goto fail_nores;
  779. }
  780. res = request_mem_region(res->start, resource_size(res), res->name);
  781. if (!res) {
  782. status = -EBUSY;
  783. goto fail_nores;
  784. }
  785. ccdc_cfg.base_addr = ioremap_nocache(res->start, resource_size(res));
  786. if (!ccdc_cfg.base_addr) {
  787. status = -ENOMEM;
  788. goto fail_nomem;
  789. }
  790. /* Get and enable Master clock */
  791. ccdc_cfg.mclk = clk_get(&pdev->dev, "master");
  792. if (IS_ERR(ccdc_cfg.mclk)) {
  793. status = PTR_ERR(ccdc_cfg.mclk);
  794. goto fail_nomap;
  795. }
  796. if (clk_enable(ccdc_cfg.mclk)) {
  797. status = -ENODEV;
  798. goto fail_mclk;
  799. }
  800. /* Get and enable Slave clock */
  801. ccdc_cfg.sclk = clk_get(&pdev->dev, "slave");
  802. if (IS_ERR(ccdc_cfg.sclk)) {
  803. status = PTR_ERR(ccdc_cfg.sclk);
  804. goto fail_mclk;
  805. }
  806. if (clk_enable(ccdc_cfg.sclk)) {
  807. status = -ENODEV;
  808. goto fail_sclk;
  809. }
  810. ccdc_cfg.dev = &pdev->dev;
  811. printk(KERN_NOTICE "%s is registered with vpfe.\n", ccdc_hw_dev.name);
  812. return 0;
  813. fail_sclk:
  814. clk_put(ccdc_cfg.sclk);
  815. fail_mclk:
  816. clk_put(ccdc_cfg.mclk);
  817. fail_nomap:
  818. iounmap(ccdc_cfg.base_addr);
  819. fail_nomem:
  820. release_mem_region(res->start, resource_size(res));
  821. fail_nores:
  822. vpfe_unregister_ccdc_device(&ccdc_hw_dev);
  823. return status;
  824. }
  825. static int dm644x_ccdc_remove(struct platform_device *pdev)
  826. {
  827. struct resource *res;
  828. clk_put(ccdc_cfg.mclk);
  829. clk_put(ccdc_cfg.sclk);
  830. iounmap(ccdc_cfg.base_addr);
  831. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  832. if (res)
  833. release_mem_region(res->start, resource_size(res));
  834. vpfe_unregister_ccdc_device(&ccdc_hw_dev);
  835. return 0;
  836. }
  837. static struct platform_driver dm644x_ccdc_driver = {
  838. .driver = {
  839. .name = "dm644x_ccdc",
  840. .owner = THIS_MODULE,
  841. },
  842. .remove = __devexit_p(dm644x_ccdc_remove),
  843. .probe = dm644x_ccdc_probe,
  844. };
  845. static int __init dm644x_ccdc_init(void)
  846. {
  847. return platform_driver_register(&dm644x_ccdc_driver);
  848. }
  849. static void __exit dm644x_ccdc_exit(void)
  850. {
  851. platform_driver_unregister(&dm644x_ccdc_driver);
  852. }
  853. module_init(dm644x_ccdc_init);
  854. module_exit(dm644x_ccdc_exit);