dm644x_ccdc.c 27 KB

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