dm644x_ccdc.c 26 KB

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