dm644x_ccdc.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  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 | CCDC_SYN_MODE_8BITS;
  362. } else {
  363. /* y/c external sync mode */
  364. syn_mode |= (((params->fid_pol & CCDC_FID_POL_MASK) <<
  365. CCDC_FID_POL_SHIFT) |
  366. ((params->hd_pol & CCDC_HD_POL_MASK) <<
  367. CCDC_HD_POL_SHIFT) |
  368. ((params->vd_pol & CCDC_VD_POL_MASK) <<
  369. CCDC_VD_POL_SHIFT));
  370. }
  371. regw(syn_mode, CCDC_SYN_MODE);
  372. /* configure video window */
  373. ccdc_setwin(&params->win, params->frm_fmt, 2);
  374. /*
  375. * configure the order of y cb cr in SDRAM, and disable latch
  376. * internal register on vsync
  377. */
  378. regw((params->pix_order << CCDC_CCDCFG_Y8POS_SHIFT) |
  379. CCDC_LATCH_ON_VSYNC_DISABLE, CCDC_CCDCFG);
  380. /*
  381. * configure the horizontal line offset. This should be a
  382. * on 32 byte bondary. So clear LSB 5 bits
  383. */
  384. regw(((params->win.width * 2 + 31) & ~0x1f), CCDC_HSIZE_OFF);
  385. /* configure the memory line offset */
  386. if (params->buf_type == CCDC_BUFTYPE_FLD_INTERLEAVED)
  387. /* two fields are interleaved in memory */
  388. regw(CCDC_SDOFST_FIELD_INTERLEAVED, CCDC_SDOFST);
  389. ccdc_sbl_reset();
  390. dev_dbg(ccdc_cfg.dev, "\nEnd of ccdc_config_ycbcr...\n");
  391. ccdc_readregs();
  392. }
  393. static void ccdc_config_black_clamp(struct ccdc_black_clamp *bclamp)
  394. {
  395. u32 val;
  396. if (!bclamp->enable) {
  397. /* configure DCSub */
  398. val = (bclamp->dc_sub) & CCDC_BLK_DC_SUB_MASK;
  399. regw(val, CCDC_DCSUB);
  400. dev_dbg(ccdc_cfg.dev, "\nWriting 0x%x to DCSUB...\n", val);
  401. regw(CCDC_CLAMP_DEFAULT_VAL, CCDC_CLAMP);
  402. dev_dbg(ccdc_cfg.dev, "\nWriting 0x0000 to CLAMP...\n");
  403. return;
  404. }
  405. /*
  406. * Configure gain, Start pixel, No of line to be avg,
  407. * No of pixel/line to be avg, & Enable the Black clamping
  408. */
  409. val = ((bclamp->sgain & CCDC_BLK_SGAIN_MASK) |
  410. ((bclamp->start_pixel & CCDC_BLK_ST_PXL_MASK) <<
  411. CCDC_BLK_ST_PXL_SHIFT) |
  412. ((bclamp->sample_ln & CCDC_BLK_SAMPLE_LINE_MASK) <<
  413. CCDC_BLK_SAMPLE_LINE_SHIFT) |
  414. ((bclamp->sample_pixel & CCDC_BLK_SAMPLE_LN_MASK) <<
  415. CCDC_BLK_SAMPLE_LN_SHIFT) | CCDC_BLK_CLAMP_ENABLE);
  416. regw(val, CCDC_CLAMP);
  417. dev_dbg(ccdc_cfg.dev, "\nWriting 0x%x to CLAMP...\n", val);
  418. /* If Black clamping is enable then make dcsub 0 */
  419. regw(CCDC_DCSUB_DEFAULT_VAL, CCDC_DCSUB);
  420. dev_dbg(ccdc_cfg.dev, "\nWriting 0x00000000 to DCSUB...\n");
  421. }
  422. static void ccdc_config_black_compense(struct ccdc_black_compensation *bcomp)
  423. {
  424. u32 val;
  425. val = ((bcomp->b & CCDC_BLK_COMP_MASK) |
  426. ((bcomp->gb & CCDC_BLK_COMP_MASK) <<
  427. CCDC_BLK_COMP_GB_COMP_SHIFT) |
  428. ((bcomp->gr & CCDC_BLK_COMP_MASK) <<
  429. CCDC_BLK_COMP_GR_COMP_SHIFT) |
  430. ((bcomp->r & CCDC_BLK_COMP_MASK) <<
  431. CCDC_BLK_COMP_R_COMP_SHIFT));
  432. regw(val, CCDC_BLKCMP);
  433. }
  434. static void ccdc_config_fpc(struct ccdc_fault_pixel *fpc)
  435. {
  436. u32 val;
  437. /* Initially disable FPC */
  438. val = CCDC_FPC_DISABLE;
  439. regw(val, CCDC_FPC);
  440. if (!fpc->enable)
  441. return;
  442. /* Configure Fault pixel if needed */
  443. regw(fpc->fpc_table_addr, CCDC_FPC_ADDR);
  444. dev_dbg(ccdc_cfg.dev, "\nWriting 0x%x to FPC_ADDR...\n",
  445. (fpc->fpc_table_addr));
  446. /* Write the FPC params with FPC disable */
  447. val = fpc->fp_num & CCDC_FPC_FPC_NUM_MASK;
  448. regw(val, CCDC_FPC);
  449. dev_dbg(ccdc_cfg.dev, "\nWriting 0x%x to FPC...\n", val);
  450. /* read the FPC register */
  451. val = regr(CCDC_FPC) | CCDC_FPC_ENABLE;
  452. regw(val, CCDC_FPC);
  453. dev_dbg(ccdc_cfg.dev, "\nWriting 0x%x to FPC...\n", val);
  454. }
  455. /*
  456. * ccdc_config_raw()
  457. * This function will configure CCDC for Raw capture mode
  458. */
  459. void ccdc_config_raw(void)
  460. {
  461. struct ccdc_params_raw *params = &ccdc_cfg.bayer;
  462. struct ccdc_config_params_raw *config_params =
  463. &ccdc_cfg.bayer.config_params;
  464. unsigned int syn_mode = 0;
  465. unsigned int val;
  466. dev_dbg(ccdc_cfg.dev, "\nStarting ccdc_config_raw...");
  467. /* Reset CCDC */
  468. ccdc_restore_defaults();
  469. /* Disable latching function registers on VSYNC */
  470. regw(CCDC_LATCH_ON_VSYNC_DISABLE, CCDC_CCDCFG);
  471. /*
  472. * Configure the vertical sync polarity(SYN_MODE.VDPOL),
  473. * horizontal sync polarity (SYN_MODE.HDPOL), frame id polarity
  474. * (SYN_MODE.FLDPOL), frame format(progressive or interlace),
  475. * data size(SYNMODE.DATSIZ), &pixel format (Input mode), output
  476. * SDRAM, enable internal timing generator
  477. */
  478. syn_mode =
  479. (((params->vd_pol & CCDC_VD_POL_MASK) << CCDC_VD_POL_SHIFT) |
  480. ((params->hd_pol & CCDC_HD_POL_MASK) << CCDC_HD_POL_SHIFT) |
  481. ((params->fid_pol & CCDC_FID_POL_MASK) << CCDC_FID_POL_SHIFT) |
  482. ((params->frm_fmt & CCDC_FRM_FMT_MASK) << CCDC_FRM_FMT_SHIFT) |
  483. ((config_params->data_sz & CCDC_DATA_SZ_MASK) <<
  484. CCDC_DATA_SZ_SHIFT) |
  485. ((params->pix_fmt & CCDC_PIX_FMT_MASK) << CCDC_PIX_FMT_SHIFT) |
  486. CCDC_WEN_ENABLE | CCDC_VDHDEN_ENABLE);
  487. /* Enable and configure aLaw register if needed */
  488. if (config_params->alaw.enable) {
  489. val = ((config_params->alaw.gama_wd &
  490. CCDC_ALAW_GAMA_WD_MASK) | CCDC_ALAW_ENABLE);
  491. regw(val, CCDC_ALAW);
  492. dev_dbg(ccdc_cfg.dev, "\nWriting 0x%x to ALAW...\n", val);
  493. }
  494. /* Configure video window */
  495. ccdc_setwin(&params->win, params->frm_fmt, CCDC_PPC_RAW);
  496. /* Configure Black Clamp */
  497. ccdc_config_black_clamp(&config_params->blk_clamp);
  498. /* Configure Black level compensation */
  499. ccdc_config_black_compense(&config_params->blk_comp);
  500. /* Configure Fault Pixel Correction */
  501. ccdc_config_fpc(&config_params->fault_pxl);
  502. /* If data size is 8 bit then pack the data */
  503. if ((config_params->data_sz == CCDC_DATA_8BITS) ||
  504. config_params->alaw.enable)
  505. syn_mode |= CCDC_DATA_PACK_ENABLE;
  506. #ifdef CONFIG_DM644X_VIDEO_PORT_ENABLE
  507. /* enable video port */
  508. val = CCDC_ENABLE_VIDEO_PORT;
  509. #else
  510. /* disable video port */
  511. val = CCDC_DISABLE_VIDEO_PORT;
  512. #endif
  513. if (config_params->data_sz == CCDC_DATA_8BITS)
  514. val |= (CCDC_DATA_10BITS & CCDC_FMTCFG_VPIN_MASK)
  515. << CCDC_FMTCFG_VPIN_SHIFT;
  516. else
  517. val |= (config_params->data_sz & CCDC_FMTCFG_VPIN_MASK)
  518. << CCDC_FMTCFG_VPIN_SHIFT;
  519. /* Write value in FMTCFG */
  520. regw(val, CCDC_FMTCFG);
  521. dev_dbg(ccdc_cfg.dev, "\nWriting 0x%x to FMTCFG...\n", val);
  522. /* Configure the color pattern according to mt9t001 sensor */
  523. regw(CCDC_COLPTN_VAL, CCDC_COLPTN);
  524. dev_dbg(ccdc_cfg.dev, "\nWriting 0xBB11BB11 to COLPTN...\n");
  525. /*
  526. * Configure Data formatter(Video port) pixel selection
  527. * (FMT_HORZ, FMT_VERT)
  528. */
  529. val = ((params->win.left & CCDC_FMT_HORZ_FMTSPH_MASK) <<
  530. CCDC_FMT_HORZ_FMTSPH_SHIFT) |
  531. (params->win.width & CCDC_FMT_HORZ_FMTLNH_MASK);
  532. regw(val, CCDC_FMT_HORZ);
  533. dev_dbg(ccdc_cfg.dev, "\nWriting 0x%x to FMT_HORZ...\n", val);
  534. val = (params->win.top & CCDC_FMT_VERT_FMTSLV_MASK)
  535. << CCDC_FMT_VERT_FMTSLV_SHIFT;
  536. if (params->frm_fmt == CCDC_FRMFMT_PROGRESSIVE)
  537. val |= (params->win.height) & CCDC_FMT_VERT_FMTLNV_MASK;
  538. else
  539. val |= (params->win.height >> 1) & CCDC_FMT_VERT_FMTLNV_MASK;
  540. dev_dbg(ccdc_cfg.dev, "\nparams->win.height 0x%x ...\n",
  541. params->win.height);
  542. regw(val, CCDC_FMT_VERT);
  543. dev_dbg(ccdc_cfg.dev, "\nWriting 0x%x to FMT_VERT...\n", val);
  544. dev_dbg(ccdc_cfg.dev, "\nbelow regw(val, FMT_VERT)...");
  545. /*
  546. * Configure Horizontal offset register. If pack 8 is enabled then
  547. * 1 pixel will take 1 byte
  548. */
  549. if ((config_params->data_sz == CCDC_DATA_8BITS) ||
  550. config_params->alaw.enable)
  551. regw((params->win.width + CCDC_32BYTE_ALIGN_VAL) &
  552. CCDC_HSIZE_OFF_MASK, CCDC_HSIZE_OFF);
  553. else
  554. /* else one pixel will take 2 byte */
  555. regw(((params->win.width * CCDC_TWO_BYTES_PER_PIXEL) +
  556. CCDC_32BYTE_ALIGN_VAL) & CCDC_HSIZE_OFF_MASK,
  557. CCDC_HSIZE_OFF);
  558. /* Set value for SDOFST */
  559. if (params->frm_fmt == CCDC_FRMFMT_INTERLACED) {
  560. if (params->image_invert_enable) {
  561. /* For intelace inverse mode */
  562. regw(CCDC_INTERLACED_IMAGE_INVERT, CCDC_SDOFST);
  563. dev_dbg(ccdc_cfg.dev, "\nWriting 0x4B6D to SDOFST..\n");
  564. }
  565. else {
  566. /* For intelace non inverse mode */
  567. regw(CCDC_INTERLACED_NO_IMAGE_INVERT, CCDC_SDOFST);
  568. dev_dbg(ccdc_cfg.dev, "\nWriting 0x0249 to SDOFST..\n");
  569. }
  570. } else if (params->frm_fmt == CCDC_FRMFMT_PROGRESSIVE) {
  571. regw(CCDC_PROGRESSIVE_NO_IMAGE_INVERT, CCDC_SDOFST);
  572. dev_dbg(ccdc_cfg.dev, "\nWriting 0x0000 to SDOFST...\n");
  573. }
  574. /*
  575. * Configure video port pixel selection (VPOUT)
  576. * Here -1 is to make the height value less than FMT_VERT.FMTLNV
  577. */
  578. if (params->frm_fmt == CCDC_FRMFMT_PROGRESSIVE)
  579. val = (((params->win.height - 1) & CCDC_VP_OUT_VERT_NUM_MASK))
  580. << CCDC_VP_OUT_VERT_NUM_SHIFT;
  581. else
  582. val =
  583. ((((params->win.height >> CCDC_INTERLACED_HEIGHT_SHIFT) -
  584. 1) & CCDC_VP_OUT_VERT_NUM_MASK)) <<
  585. CCDC_VP_OUT_VERT_NUM_SHIFT;
  586. val |= ((((params->win.width))) & CCDC_VP_OUT_HORZ_NUM_MASK)
  587. << CCDC_VP_OUT_HORZ_NUM_SHIFT;
  588. val |= (params->win.left) & CCDC_VP_OUT_HORZ_ST_MASK;
  589. regw(val, CCDC_VP_OUT);
  590. dev_dbg(ccdc_cfg.dev, "\nWriting 0x%x to VP_OUT...\n", val);
  591. regw(syn_mode, CCDC_SYN_MODE);
  592. dev_dbg(ccdc_cfg.dev, "\nWriting 0x%x to SYN_MODE...\n", syn_mode);
  593. ccdc_sbl_reset();
  594. dev_dbg(ccdc_cfg.dev, "\nend of ccdc_config_raw...");
  595. ccdc_readregs();
  596. }
  597. static int ccdc_configure(void)
  598. {
  599. if (ccdc_cfg.if_type == VPFE_RAW_BAYER)
  600. ccdc_config_raw();
  601. else
  602. ccdc_config_ycbcr();
  603. return 0;
  604. }
  605. static int ccdc_set_buftype(enum ccdc_buftype buf_type)
  606. {
  607. if (ccdc_cfg.if_type == VPFE_RAW_BAYER)
  608. ccdc_cfg.bayer.buf_type = buf_type;
  609. else
  610. ccdc_cfg.ycbcr.buf_type = buf_type;
  611. return 0;
  612. }
  613. static enum ccdc_buftype ccdc_get_buftype(void)
  614. {
  615. if (ccdc_cfg.if_type == VPFE_RAW_BAYER)
  616. return ccdc_cfg.bayer.buf_type;
  617. return ccdc_cfg.ycbcr.buf_type;
  618. }
  619. static int ccdc_enum_pix(u32 *pix, int i)
  620. {
  621. int ret = -EINVAL;
  622. if (ccdc_cfg.if_type == VPFE_RAW_BAYER) {
  623. if (i < ARRAY_SIZE(ccdc_raw_bayer_pix_formats)) {
  624. *pix = ccdc_raw_bayer_pix_formats[i];
  625. ret = 0;
  626. }
  627. } else {
  628. if (i < ARRAY_SIZE(ccdc_raw_yuv_pix_formats)) {
  629. *pix = ccdc_raw_yuv_pix_formats[i];
  630. ret = 0;
  631. }
  632. }
  633. return ret;
  634. }
  635. static int ccdc_set_pixel_format(u32 pixfmt)
  636. {
  637. if (ccdc_cfg.if_type == VPFE_RAW_BAYER) {
  638. ccdc_cfg.bayer.pix_fmt = CCDC_PIXFMT_RAW;
  639. if (pixfmt == V4L2_PIX_FMT_SBGGR8)
  640. ccdc_cfg.bayer.config_params.alaw.enable = 1;
  641. else if (pixfmt != V4L2_PIX_FMT_SBGGR16)
  642. return -EINVAL;
  643. } else {
  644. if (pixfmt == V4L2_PIX_FMT_YUYV)
  645. ccdc_cfg.ycbcr.pix_order = CCDC_PIXORDER_YCBYCR;
  646. else if (pixfmt == V4L2_PIX_FMT_UYVY)
  647. ccdc_cfg.ycbcr.pix_order = CCDC_PIXORDER_CBYCRY;
  648. else
  649. return -EINVAL;
  650. }
  651. return 0;
  652. }
  653. static u32 ccdc_get_pixel_format(void)
  654. {
  655. struct ccdc_a_law *alaw = &ccdc_cfg.bayer.config_params.alaw;
  656. u32 pixfmt;
  657. if (ccdc_cfg.if_type == VPFE_RAW_BAYER)
  658. if (alaw->enable)
  659. pixfmt = V4L2_PIX_FMT_SBGGR8;
  660. else
  661. pixfmt = V4L2_PIX_FMT_SBGGR16;
  662. else {
  663. if (ccdc_cfg.ycbcr.pix_order == CCDC_PIXORDER_YCBYCR)
  664. pixfmt = V4L2_PIX_FMT_YUYV;
  665. else
  666. pixfmt = V4L2_PIX_FMT_UYVY;
  667. }
  668. return pixfmt;
  669. }
  670. static int ccdc_set_image_window(struct v4l2_rect *win)
  671. {
  672. if (ccdc_cfg.if_type == VPFE_RAW_BAYER)
  673. ccdc_cfg.bayer.win = *win;
  674. else
  675. ccdc_cfg.ycbcr.win = *win;
  676. return 0;
  677. }
  678. static void ccdc_get_image_window(struct v4l2_rect *win)
  679. {
  680. if (ccdc_cfg.if_type == VPFE_RAW_BAYER)
  681. *win = ccdc_cfg.bayer.win;
  682. else
  683. *win = ccdc_cfg.ycbcr.win;
  684. }
  685. static unsigned int ccdc_get_line_length(void)
  686. {
  687. struct ccdc_config_params_raw *config_params =
  688. &ccdc_cfg.bayer.config_params;
  689. unsigned int len;
  690. if (ccdc_cfg.if_type == VPFE_RAW_BAYER) {
  691. if ((config_params->alaw.enable) ||
  692. (config_params->data_sz == CCDC_DATA_8BITS))
  693. len = ccdc_cfg.bayer.win.width;
  694. else
  695. len = ccdc_cfg.bayer.win.width * 2;
  696. } else
  697. len = ccdc_cfg.ycbcr.win.width * 2;
  698. return ALIGN(len, 32);
  699. }
  700. static int ccdc_set_frame_format(enum ccdc_frmfmt frm_fmt)
  701. {
  702. if (ccdc_cfg.if_type == VPFE_RAW_BAYER)
  703. ccdc_cfg.bayer.frm_fmt = frm_fmt;
  704. else
  705. ccdc_cfg.ycbcr.frm_fmt = frm_fmt;
  706. return 0;
  707. }
  708. static enum ccdc_frmfmt ccdc_get_frame_format(void)
  709. {
  710. if (ccdc_cfg.if_type == VPFE_RAW_BAYER)
  711. return ccdc_cfg.bayer.frm_fmt;
  712. else
  713. return ccdc_cfg.ycbcr.frm_fmt;
  714. }
  715. static int ccdc_getfid(void)
  716. {
  717. return (regr(CCDC_SYN_MODE) >> 15) & 1;
  718. }
  719. /* misc operations */
  720. static inline void ccdc_setfbaddr(unsigned long addr)
  721. {
  722. regw(addr & 0xffffffe0, CCDC_SDR_ADDR);
  723. }
  724. static int ccdc_set_hw_if_params(struct vpfe_hw_if_param *params)
  725. {
  726. ccdc_cfg.if_type = params->if_type;
  727. switch (params->if_type) {
  728. case VPFE_BT656:
  729. case VPFE_YCBCR_SYNC_16:
  730. case VPFE_YCBCR_SYNC_8:
  731. ccdc_cfg.ycbcr.vd_pol = params->vdpol;
  732. ccdc_cfg.ycbcr.hd_pol = params->hdpol;
  733. break;
  734. default:
  735. /* TODO add support for raw bayer here */
  736. return -EINVAL;
  737. }
  738. return 0;
  739. }
  740. static struct ccdc_hw_device ccdc_hw_dev = {
  741. .name = "DM6446 CCDC",
  742. .owner = THIS_MODULE,
  743. .hw_ops = {
  744. .open = ccdc_open,
  745. .close = ccdc_close,
  746. .reset = ccdc_sbl_reset,
  747. .enable = ccdc_enable,
  748. .set_hw_if_params = ccdc_set_hw_if_params,
  749. .set_params = ccdc_set_params,
  750. .configure = ccdc_configure,
  751. .set_buftype = ccdc_set_buftype,
  752. .get_buftype = ccdc_get_buftype,
  753. .enum_pix = ccdc_enum_pix,
  754. .set_pixel_format = ccdc_set_pixel_format,
  755. .get_pixel_format = ccdc_get_pixel_format,
  756. .set_frame_format = ccdc_set_frame_format,
  757. .get_frame_format = ccdc_get_frame_format,
  758. .set_image_window = ccdc_set_image_window,
  759. .get_image_window = ccdc_get_image_window,
  760. .get_line_length = ccdc_get_line_length,
  761. .setfbaddr = ccdc_setfbaddr,
  762. .getfid = ccdc_getfid,
  763. },
  764. };
  765. static int __init dm644x_ccdc_probe(struct platform_device *pdev)
  766. {
  767. struct resource *res;
  768. int status = 0;
  769. /*
  770. * first try to register with vpfe. If not correct platform, then we
  771. * don't have to iomap
  772. */
  773. status = vpfe_register_ccdc_device(&ccdc_hw_dev);
  774. if (status < 0)
  775. return status;
  776. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  777. if (!res) {
  778. status = -ENODEV;
  779. goto fail_nores;
  780. }
  781. res = request_mem_region(res->start, resource_size(res), res->name);
  782. if (!res) {
  783. status = -EBUSY;
  784. goto fail_nores;
  785. }
  786. ccdc_cfg.base_addr = ioremap_nocache(res->start, resource_size(res));
  787. if (!ccdc_cfg.base_addr) {
  788. status = -ENOMEM;
  789. goto fail_nomem;
  790. }
  791. /* Get and enable Master clock */
  792. ccdc_cfg.mclk = clk_get(&pdev->dev, "master");
  793. if (IS_ERR(ccdc_cfg.mclk)) {
  794. status = PTR_ERR(ccdc_cfg.mclk);
  795. goto fail_nomap;
  796. }
  797. if (clk_enable(ccdc_cfg.mclk)) {
  798. status = -ENODEV;
  799. goto fail_mclk;
  800. }
  801. /* Get and enable Slave clock */
  802. ccdc_cfg.sclk = clk_get(&pdev->dev, "slave");
  803. if (IS_ERR(ccdc_cfg.sclk)) {
  804. status = PTR_ERR(ccdc_cfg.sclk);
  805. goto fail_mclk;
  806. }
  807. if (clk_enable(ccdc_cfg.sclk)) {
  808. status = -ENODEV;
  809. goto fail_sclk;
  810. }
  811. ccdc_cfg.dev = &pdev->dev;
  812. printk(KERN_NOTICE "%s is registered with vpfe.\n", ccdc_hw_dev.name);
  813. return 0;
  814. fail_sclk:
  815. clk_put(ccdc_cfg.sclk);
  816. fail_mclk:
  817. clk_put(ccdc_cfg.mclk);
  818. fail_nomap:
  819. iounmap(ccdc_cfg.base_addr);
  820. fail_nomem:
  821. release_mem_region(res->start, resource_size(res));
  822. fail_nores:
  823. vpfe_unregister_ccdc_device(&ccdc_hw_dev);
  824. return status;
  825. }
  826. static int dm644x_ccdc_remove(struct platform_device *pdev)
  827. {
  828. struct resource *res;
  829. clk_put(ccdc_cfg.mclk);
  830. clk_put(ccdc_cfg.sclk);
  831. iounmap(ccdc_cfg.base_addr);
  832. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  833. if (res)
  834. release_mem_region(res->start, resource_size(res));
  835. vpfe_unregister_ccdc_device(&ccdc_hw_dev);
  836. return 0;
  837. }
  838. static struct platform_driver dm644x_ccdc_driver = {
  839. .driver = {
  840. .name = "dm644x_ccdc",
  841. .owner = THIS_MODULE,
  842. },
  843. .remove = __devexit_p(dm644x_ccdc_remove),
  844. .probe = dm644x_ccdc_probe,
  845. };
  846. static int __init dm644x_ccdc_init(void)
  847. {
  848. return platform_driver_register(&dm644x_ccdc_driver);
  849. }
  850. static void __exit dm644x_ccdc_exit(void)
  851. {
  852. platform_driver_unregister(&dm644x_ccdc_driver);
  853. }
  854. module_init(dm644x_ccdc_init);
  855. module_exit(dm644x_ccdc_exit);