dm355_ccdc.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  1. /*
  2. * Copyright (C) 2005-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 DM355
  15. * ------------------------------
  16. *
  17. * This module is for configuring DM355 CCD controller of 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 Bayer RGB data, before writing it to SDRAM. This
  21. * module also allows application to configure individual
  22. * module parameters through VPFE_CMD_S_CCDC_RAW_PARAMS IOCTL.
  23. * To do so, application include dm355_ccdc.h and vpfe_capture.h header
  24. * files. The setparams() API is called by vpfe_capture driver
  25. * to configure module parameters
  26. *
  27. * TODO: 1) Raw bayer parameter settings and bayer capture
  28. * 2) Split module parameter structure to module specific ioctl structs
  29. * 3) add support for lense shading correction
  30. * 4) investigate if enum used for user space type definition
  31. * to be replaced by #defines or integer
  32. */
  33. #include <linux/platform_device.h>
  34. #include <linux/uaccess.h>
  35. #include <linux/videodev2.h>
  36. #include <linux/err.h>
  37. #include <linux/module.h>
  38. #include <media/davinci/dm355_ccdc.h>
  39. #include <media/davinci/vpss.h>
  40. #include "dm355_ccdc_regs.h"
  41. #include "ccdc_hw_device.h"
  42. MODULE_LICENSE("GPL");
  43. MODULE_DESCRIPTION("CCDC Driver for DM355");
  44. MODULE_AUTHOR("Texas Instruments");
  45. static struct ccdc_oper_config {
  46. struct device *dev;
  47. /* CCDC interface type */
  48. enum vpfe_hw_if_type if_type;
  49. /* Raw Bayer configuration */
  50. struct ccdc_params_raw bayer;
  51. /* YCbCr configuration */
  52. struct ccdc_params_ycbcr ycbcr;
  53. /* ccdc base address */
  54. void __iomem *base_addr;
  55. } ccdc_cfg = {
  56. /* Raw configurations */
  57. .bayer = {
  58. .pix_fmt = CCDC_PIXFMT_RAW,
  59. .frm_fmt = CCDC_FRMFMT_PROGRESSIVE,
  60. .win = CCDC_WIN_VGA,
  61. .fid_pol = VPFE_PINPOL_POSITIVE,
  62. .vd_pol = VPFE_PINPOL_POSITIVE,
  63. .hd_pol = VPFE_PINPOL_POSITIVE,
  64. .gain = {
  65. .r_ye = 256,
  66. .gb_g = 256,
  67. .gr_cy = 256,
  68. .b_mg = 256
  69. },
  70. .config_params = {
  71. .datasft = 2,
  72. .mfilt1 = CCDC_NO_MEDIAN_FILTER1,
  73. .mfilt2 = CCDC_NO_MEDIAN_FILTER2,
  74. .alaw = {
  75. .gamma_wd = 2,
  76. },
  77. .blk_clamp = {
  78. .sample_pixel = 1,
  79. .dc_sub = 25
  80. },
  81. .col_pat_field0 = {
  82. .olop = CCDC_GREEN_BLUE,
  83. .olep = CCDC_BLUE,
  84. .elop = CCDC_RED,
  85. .elep = CCDC_GREEN_RED
  86. },
  87. .col_pat_field1 = {
  88. .olop = CCDC_GREEN_BLUE,
  89. .olep = CCDC_BLUE,
  90. .elop = CCDC_RED,
  91. .elep = CCDC_GREEN_RED
  92. },
  93. },
  94. },
  95. /* YCbCr configuration */
  96. .ycbcr = {
  97. .win = CCDC_WIN_PAL,
  98. .pix_fmt = CCDC_PIXFMT_YCBCR_8BIT,
  99. .frm_fmt = CCDC_FRMFMT_INTERLACED,
  100. .fid_pol = VPFE_PINPOL_POSITIVE,
  101. .vd_pol = VPFE_PINPOL_POSITIVE,
  102. .hd_pol = VPFE_PINPOL_POSITIVE,
  103. .bt656_enable = 1,
  104. .pix_order = CCDC_PIXORDER_CBYCRY,
  105. .buf_type = CCDC_BUFTYPE_FLD_INTERLEAVED
  106. },
  107. };
  108. /* Raw Bayer formats */
  109. static u32 ccdc_raw_bayer_pix_formats[] =
  110. {V4L2_PIX_FMT_SBGGR8, V4L2_PIX_FMT_SBGGR16};
  111. /* Raw YUV formats */
  112. static u32 ccdc_raw_yuv_pix_formats[] =
  113. {V4L2_PIX_FMT_UYVY, V4L2_PIX_FMT_YUYV};
  114. /* register access routines */
  115. static inline u32 regr(u32 offset)
  116. {
  117. return __raw_readl(ccdc_cfg.base_addr + offset);
  118. }
  119. static inline void regw(u32 val, u32 offset)
  120. {
  121. __raw_writel(val, ccdc_cfg.base_addr + offset);
  122. }
  123. static void ccdc_enable(int en)
  124. {
  125. unsigned int temp;
  126. temp = regr(SYNCEN);
  127. temp &= (~CCDC_SYNCEN_VDHDEN_MASK);
  128. temp |= (en & CCDC_SYNCEN_VDHDEN_MASK);
  129. regw(temp, SYNCEN);
  130. }
  131. static void ccdc_enable_output_to_sdram(int en)
  132. {
  133. unsigned int temp;
  134. temp = regr(SYNCEN);
  135. temp &= (~(CCDC_SYNCEN_WEN_MASK));
  136. temp |= ((en << CCDC_SYNCEN_WEN_SHIFT) & CCDC_SYNCEN_WEN_MASK);
  137. regw(temp, SYNCEN);
  138. }
  139. static void ccdc_config_gain_offset(void)
  140. {
  141. /* configure gain */
  142. regw(ccdc_cfg.bayer.gain.r_ye, RYEGAIN);
  143. regw(ccdc_cfg.bayer.gain.gr_cy, GRCYGAIN);
  144. regw(ccdc_cfg.bayer.gain.gb_g, GBGGAIN);
  145. regw(ccdc_cfg.bayer.gain.b_mg, BMGGAIN);
  146. /* configure offset */
  147. regw(ccdc_cfg.bayer.ccdc_offset, OFFSET);
  148. }
  149. /*
  150. * ccdc_restore_defaults()
  151. * This function restore power on defaults in the ccdc registers
  152. */
  153. static int ccdc_restore_defaults(void)
  154. {
  155. int i;
  156. dev_dbg(ccdc_cfg.dev, "\nstarting ccdc_restore_defaults...");
  157. /* set all registers to zero */
  158. for (i = 0; i <= CCDC_REG_LAST; i += 4)
  159. regw(0, i);
  160. /* now override the values with power on defaults in registers */
  161. regw(MODESET_DEFAULT, MODESET);
  162. /* no culling support */
  163. regw(CULH_DEFAULT, CULH);
  164. regw(CULV_DEFAULT, CULV);
  165. /* Set default Gain and Offset */
  166. ccdc_cfg.bayer.gain.r_ye = GAIN_DEFAULT;
  167. ccdc_cfg.bayer.gain.gb_g = GAIN_DEFAULT;
  168. ccdc_cfg.bayer.gain.gr_cy = GAIN_DEFAULT;
  169. ccdc_cfg.bayer.gain.b_mg = GAIN_DEFAULT;
  170. ccdc_config_gain_offset();
  171. regw(OUTCLIP_DEFAULT, OUTCLIP);
  172. regw(LSCCFG2_DEFAULT, LSCCFG2);
  173. /* select ccdc input */
  174. if (vpss_select_ccdc_source(VPSS_CCDCIN)) {
  175. dev_dbg(ccdc_cfg.dev, "\ncouldn't select ccdc input source");
  176. return -EFAULT;
  177. }
  178. /* select ccdc clock */
  179. if (vpss_enable_clock(VPSS_CCDC_CLOCK, 1) < 0) {
  180. dev_dbg(ccdc_cfg.dev, "\ncouldn't enable ccdc clock");
  181. return -EFAULT;
  182. }
  183. dev_dbg(ccdc_cfg.dev, "\nEnd of ccdc_restore_defaults...");
  184. return 0;
  185. }
  186. static int ccdc_open(struct device *device)
  187. {
  188. return ccdc_restore_defaults();
  189. }
  190. static int ccdc_close(struct device *device)
  191. {
  192. /* disable clock */
  193. vpss_enable_clock(VPSS_CCDC_CLOCK, 0);
  194. /* do nothing for now */
  195. return 0;
  196. }
  197. /*
  198. * ccdc_setwin()
  199. * This function will configure the window size to
  200. * be capture in CCDC reg.
  201. */
  202. static void ccdc_setwin(struct v4l2_rect *image_win,
  203. enum ccdc_frmfmt frm_fmt, int ppc)
  204. {
  205. int horz_start, horz_nr_pixels;
  206. int vert_start, vert_nr_lines;
  207. int mid_img = 0;
  208. dev_dbg(ccdc_cfg.dev, "\nStarting ccdc_setwin...");
  209. /*
  210. * ppc - per pixel count. indicates how many pixels per cell
  211. * output to SDRAM. example, for ycbcr, it is one y and one c, so 2.
  212. * raw capture this is 1
  213. */
  214. horz_start = image_win->left << (ppc - 1);
  215. horz_nr_pixels = ((image_win->width) << (ppc - 1)) - 1;
  216. /* Writing the horizontal info into the registers */
  217. regw(horz_start, SPH);
  218. regw(horz_nr_pixels, NPH);
  219. vert_start = image_win->top;
  220. if (frm_fmt == CCDC_FRMFMT_INTERLACED) {
  221. vert_nr_lines = (image_win->height >> 1) - 1;
  222. vert_start >>= 1;
  223. /* Since first line doesn't have any data */
  224. vert_start += 1;
  225. /* configure VDINT0 and VDINT1 */
  226. regw(vert_start, VDINT0);
  227. } else {
  228. /* Since first line doesn't have any data */
  229. vert_start += 1;
  230. vert_nr_lines = image_win->height - 1;
  231. /* configure VDINT0 and VDINT1 */
  232. mid_img = vert_start + (image_win->height / 2);
  233. regw(vert_start, VDINT0);
  234. regw(mid_img, VDINT1);
  235. }
  236. regw(vert_start & CCDC_START_VER_ONE_MASK, SLV0);
  237. regw(vert_start & CCDC_START_VER_TWO_MASK, SLV1);
  238. regw(vert_nr_lines & CCDC_NUM_LINES_VER, NLV);
  239. dev_dbg(ccdc_cfg.dev, "\nEnd of ccdc_setwin...");
  240. }
  241. static int validate_ccdc_param(struct ccdc_config_params_raw *ccdcparam)
  242. {
  243. if (ccdcparam->datasft < CCDC_DATA_NO_SHIFT ||
  244. ccdcparam->datasft > CCDC_DATA_SHIFT_6BIT) {
  245. dev_dbg(ccdc_cfg.dev, "Invalid value of data shift\n");
  246. return -EINVAL;
  247. }
  248. if (ccdcparam->mfilt1 < CCDC_NO_MEDIAN_FILTER1 ||
  249. ccdcparam->mfilt1 > CCDC_MEDIAN_FILTER1) {
  250. dev_dbg(ccdc_cfg.dev, "Invalid value of median filter1\n");
  251. return -EINVAL;
  252. }
  253. if (ccdcparam->mfilt2 < CCDC_NO_MEDIAN_FILTER2 ||
  254. ccdcparam->mfilt2 > CCDC_MEDIAN_FILTER2) {
  255. dev_dbg(ccdc_cfg.dev, "Invalid value of median filter2\n");
  256. return -EINVAL;
  257. }
  258. if ((ccdcparam->med_filt_thres < 0) ||
  259. (ccdcparam->med_filt_thres > CCDC_MED_FILT_THRESH)) {
  260. dev_dbg(ccdc_cfg.dev,
  261. "Invalid value of median filter threshold\n");
  262. return -EINVAL;
  263. }
  264. if (ccdcparam->data_sz < CCDC_DATA_16BITS ||
  265. ccdcparam->data_sz > CCDC_DATA_8BITS) {
  266. dev_dbg(ccdc_cfg.dev, "Invalid value of data size\n");
  267. return -EINVAL;
  268. }
  269. if (ccdcparam->alaw.enable) {
  270. if (ccdcparam->alaw.gamma_wd < CCDC_GAMMA_BITS_13_4 ||
  271. ccdcparam->alaw.gamma_wd > CCDC_GAMMA_BITS_09_0) {
  272. dev_dbg(ccdc_cfg.dev, "Invalid value of ALAW\n");
  273. return -EINVAL;
  274. }
  275. }
  276. if (ccdcparam->blk_clamp.b_clamp_enable) {
  277. if (ccdcparam->blk_clamp.sample_pixel < CCDC_SAMPLE_1PIXELS ||
  278. ccdcparam->blk_clamp.sample_pixel > CCDC_SAMPLE_16PIXELS) {
  279. dev_dbg(ccdc_cfg.dev,
  280. "Invalid value of sample pixel\n");
  281. return -EINVAL;
  282. }
  283. if (ccdcparam->blk_clamp.sample_ln < CCDC_SAMPLE_1LINES ||
  284. ccdcparam->blk_clamp.sample_ln > CCDC_SAMPLE_16LINES) {
  285. dev_dbg(ccdc_cfg.dev,
  286. "Invalid value of sample lines\n");
  287. return -EINVAL;
  288. }
  289. }
  290. return 0;
  291. }
  292. /* Parameter operations */
  293. static int ccdc_set_params(void __user *params)
  294. {
  295. struct ccdc_config_params_raw ccdc_raw_params;
  296. int x;
  297. /* only raw module parameters can be set through the IOCTL */
  298. if (ccdc_cfg.if_type != VPFE_RAW_BAYER)
  299. return -EINVAL;
  300. x = copy_from_user(&ccdc_raw_params, params, sizeof(ccdc_raw_params));
  301. if (x) {
  302. dev_dbg(ccdc_cfg.dev, "ccdc_set_params: error in copying ccdcparams, %d\n",
  303. x);
  304. return -EFAULT;
  305. }
  306. if (!validate_ccdc_param(&ccdc_raw_params)) {
  307. memcpy(&ccdc_cfg.bayer.config_params,
  308. &ccdc_raw_params,
  309. sizeof(ccdc_raw_params));
  310. return 0;
  311. }
  312. return -EINVAL;
  313. }
  314. /* This function will configure CCDC for YCbCr video capture */
  315. static void ccdc_config_ycbcr(void)
  316. {
  317. struct ccdc_params_ycbcr *params = &ccdc_cfg.ycbcr;
  318. u32 temp;
  319. /* first set the CCDC power on defaults values in all registers */
  320. dev_dbg(ccdc_cfg.dev, "\nStarting ccdc_config_ycbcr...");
  321. ccdc_restore_defaults();
  322. /* configure pixel format & video frame format */
  323. temp = (((params->pix_fmt & CCDC_INPUT_MODE_MASK) <<
  324. CCDC_INPUT_MODE_SHIFT) |
  325. ((params->frm_fmt & CCDC_FRM_FMT_MASK) <<
  326. CCDC_FRM_FMT_SHIFT));
  327. /* setup BT.656 sync mode */
  328. if (params->bt656_enable) {
  329. regw(CCDC_REC656IF_BT656_EN, REC656IF);
  330. /*
  331. * configure the FID, VD, HD pin polarity fld,hd pol positive,
  332. * vd negative, 8-bit pack mode
  333. */
  334. temp |= CCDC_VD_POL_NEGATIVE;
  335. } else { /* y/c external sync mode */
  336. temp |= (((params->fid_pol & CCDC_FID_POL_MASK) <<
  337. CCDC_FID_POL_SHIFT) |
  338. ((params->hd_pol & CCDC_HD_POL_MASK) <<
  339. CCDC_HD_POL_SHIFT) |
  340. ((params->vd_pol & CCDC_VD_POL_MASK) <<
  341. CCDC_VD_POL_SHIFT));
  342. }
  343. /* pack the data to 8-bit */
  344. temp |= CCDC_DATA_PACK_ENABLE;
  345. regw(temp, MODESET);
  346. /* configure video window */
  347. ccdc_setwin(&params->win, params->frm_fmt, 2);
  348. /* configure the order of y cb cr in SD-RAM */
  349. temp = (params->pix_order << CCDC_Y8POS_SHIFT);
  350. temp |= CCDC_LATCH_ON_VSYNC_DISABLE | CCDC_CCDCFG_FIDMD_NO_LATCH_VSYNC;
  351. regw(temp, CCDCFG);
  352. /*
  353. * configure the horizontal line offset. This is done by rounding up
  354. * width to a multiple of 16 pixels and multiply by two to account for
  355. * y:cb:cr 4:2:2 data
  356. */
  357. regw(((params->win.width * 2 + 31) >> 5), HSIZE);
  358. /* configure the memory line offset */
  359. if (params->buf_type == CCDC_BUFTYPE_FLD_INTERLEAVED) {
  360. /* two fields are interleaved in memory */
  361. regw(CCDC_SDOFST_FIELD_INTERLEAVED, SDOFST);
  362. }
  363. dev_dbg(ccdc_cfg.dev, "\nEnd of ccdc_config_ycbcr...\n");
  364. }
  365. /*
  366. * ccdc_config_black_clamp()
  367. * configure parameters for Optical Black Clamp
  368. */
  369. static void ccdc_config_black_clamp(struct ccdc_black_clamp *bclamp)
  370. {
  371. u32 val;
  372. if (!bclamp->b_clamp_enable) {
  373. /* configure DCSub */
  374. regw(bclamp->dc_sub & CCDC_BLK_DC_SUB_MASK, DCSUB);
  375. regw(0x0000, CLAMP);
  376. return;
  377. }
  378. /* Enable the Black clamping, set sample lines and pixels */
  379. val = (bclamp->start_pixel & CCDC_BLK_ST_PXL_MASK) |
  380. ((bclamp->sample_pixel & CCDC_BLK_SAMPLE_LN_MASK) <<
  381. CCDC_BLK_SAMPLE_LN_SHIFT) | CCDC_BLK_CLAMP_ENABLE;
  382. regw(val, CLAMP);
  383. /* If Black clamping is enable then make dcsub 0 */
  384. val = (bclamp->sample_ln & CCDC_NUM_LINE_CALC_MASK)
  385. << CCDC_NUM_LINE_CALC_SHIFT;
  386. regw(val, DCSUB);
  387. }
  388. /*
  389. * ccdc_config_black_compense()
  390. * configure parameters for Black Compensation
  391. */
  392. static void ccdc_config_black_compense(struct ccdc_black_compensation *bcomp)
  393. {
  394. u32 val;
  395. val = (bcomp->b & CCDC_BLK_COMP_MASK) |
  396. ((bcomp->gb & CCDC_BLK_COMP_MASK) <<
  397. CCDC_BLK_COMP_GB_COMP_SHIFT);
  398. regw(val, BLKCMP1);
  399. val = ((bcomp->gr & CCDC_BLK_COMP_MASK) <<
  400. CCDC_BLK_COMP_GR_COMP_SHIFT) |
  401. ((bcomp->r & CCDC_BLK_COMP_MASK) <<
  402. CCDC_BLK_COMP_R_COMP_SHIFT);
  403. regw(val, BLKCMP0);
  404. }
  405. /*
  406. * ccdc_write_dfc_entry()
  407. * write an entry in the dfc table.
  408. */
  409. static int ccdc_write_dfc_entry(int index, struct ccdc_vertical_dft *dfc)
  410. {
  411. /* TODO This is to be re-visited and adjusted */
  412. #define DFC_WRITE_WAIT_COUNT 1000
  413. u32 val, count = DFC_WRITE_WAIT_COUNT;
  414. regw(dfc->dft_corr_vert[index], DFCMEM0);
  415. regw(dfc->dft_corr_horz[index], DFCMEM1);
  416. regw(dfc->dft_corr_sub1[index], DFCMEM2);
  417. regw(dfc->dft_corr_sub2[index], DFCMEM3);
  418. regw(dfc->dft_corr_sub3[index], DFCMEM4);
  419. /* set WR bit to write */
  420. val = regr(DFCMEMCTL) | CCDC_DFCMEMCTL_DFCMWR_MASK;
  421. regw(val, DFCMEMCTL);
  422. /*
  423. * Assume, it is very short. If we get an error, we need to
  424. * adjust this value
  425. */
  426. while (regr(DFCMEMCTL) & CCDC_DFCMEMCTL_DFCMWR_MASK)
  427. count--;
  428. /*
  429. * TODO We expect the count to be non-zero to be successful. Adjust
  430. * the count if write requires more time
  431. */
  432. if (count) {
  433. dev_err(ccdc_cfg.dev, "defect table write timeout !!!\n");
  434. return -1;
  435. }
  436. return 0;
  437. }
  438. /*
  439. * ccdc_config_vdfc()
  440. * configure parameters for Vertical Defect Correction
  441. */
  442. static int ccdc_config_vdfc(struct ccdc_vertical_dft *dfc)
  443. {
  444. u32 val;
  445. int i;
  446. /* Configure General Defect Correction. The table used is from IPIPE */
  447. val = dfc->gen_dft_en & CCDC_DFCCTL_GDFCEN_MASK;
  448. /* Configure Vertical Defect Correction if needed */
  449. if (!dfc->ver_dft_en) {
  450. /* Enable only General Defect Correction */
  451. regw(val, DFCCTL);
  452. return 0;
  453. }
  454. if (dfc->table_size > CCDC_DFT_TABLE_SIZE)
  455. return -EINVAL;
  456. val |= CCDC_DFCCTL_VDFC_DISABLE;
  457. val |= (dfc->dft_corr_ctl.vdfcsl & CCDC_DFCCTL_VDFCSL_MASK) <<
  458. CCDC_DFCCTL_VDFCSL_SHIFT;
  459. val |= (dfc->dft_corr_ctl.vdfcuda & CCDC_DFCCTL_VDFCUDA_MASK) <<
  460. CCDC_DFCCTL_VDFCUDA_SHIFT;
  461. val |= (dfc->dft_corr_ctl.vdflsft & CCDC_DFCCTL_VDFLSFT_MASK) <<
  462. CCDC_DFCCTL_VDFLSFT_SHIFT;
  463. regw(val , DFCCTL);
  464. /* clear address ptr to offset 0 */
  465. val = CCDC_DFCMEMCTL_DFCMARST_MASK << CCDC_DFCMEMCTL_DFCMARST_SHIFT;
  466. /* write defect table entries */
  467. for (i = 0; i < dfc->table_size; i++) {
  468. /* increment address for non zero index */
  469. if (i != 0)
  470. val = CCDC_DFCMEMCTL_INC_ADDR;
  471. regw(val, DFCMEMCTL);
  472. if (ccdc_write_dfc_entry(i, dfc) < 0)
  473. return -EFAULT;
  474. }
  475. /* update saturation level and enable dfc */
  476. regw(dfc->saturation_ctl & CCDC_VDC_DFCVSAT_MASK, DFCVSAT);
  477. val = regr(DFCCTL) | (CCDC_DFCCTL_VDFCEN_MASK <<
  478. CCDC_DFCCTL_VDFCEN_SHIFT);
  479. regw(val, DFCCTL);
  480. return 0;
  481. }
  482. /*
  483. * ccdc_config_csc()
  484. * configure parameters for color space conversion
  485. * Each register CSCM0-7 has two values in S8Q5 format.
  486. */
  487. static void ccdc_config_csc(struct ccdc_csc *csc)
  488. {
  489. u32 val1 = 0, val2;
  490. int i;
  491. if (!csc->enable)
  492. return;
  493. /* Enable the CSC sub-module */
  494. regw(CCDC_CSC_ENABLE, CSCCTL);
  495. /* Converting the co-eff as per the format of the register */
  496. for (i = 0; i < CCDC_CSC_COEFF_TABLE_SIZE; i++) {
  497. if ((i % 2) == 0) {
  498. /* CSCM - LSB */
  499. val1 = (csc->coeff[i].integer &
  500. CCDC_CSC_COEF_INTEG_MASK)
  501. << CCDC_CSC_COEF_INTEG_SHIFT;
  502. /*
  503. * convert decimal part to binary. Use 2 decimal
  504. * precision, user values range from .00 - 0.99
  505. */
  506. val1 |= (((csc->coeff[i].decimal &
  507. CCDC_CSC_COEF_DECIMAL_MASK) *
  508. CCDC_CSC_DEC_MAX) / 100);
  509. } else {
  510. /* CSCM - MSB */
  511. val2 = (csc->coeff[i].integer &
  512. CCDC_CSC_COEF_INTEG_MASK)
  513. << CCDC_CSC_COEF_INTEG_SHIFT;
  514. val2 |= (((csc->coeff[i].decimal &
  515. CCDC_CSC_COEF_DECIMAL_MASK) *
  516. CCDC_CSC_DEC_MAX) / 100);
  517. val2 <<= CCDC_CSCM_MSB_SHIFT;
  518. val2 |= val1;
  519. regw(val2, (CSCM0 + ((i - 1) << 1)));
  520. }
  521. }
  522. }
  523. /*
  524. * ccdc_config_color_patterns()
  525. * configure parameters for color patterns
  526. */
  527. static void ccdc_config_color_patterns(struct ccdc_col_pat *pat0,
  528. struct ccdc_col_pat *pat1)
  529. {
  530. u32 val;
  531. val = (pat0->olop | (pat0->olep << 2) | (pat0->elop << 4) |
  532. (pat0->elep << 6) | (pat1->olop << 8) | (pat1->olep << 10) |
  533. (pat1->elop << 12) | (pat1->elep << 14));
  534. regw(val, COLPTN);
  535. }
  536. /* This function will configure CCDC for Raw mode image capture */
  537. static int ccdc_config_raw(void)
  538. {
  539. struct ccdc_params_raw *params = &ccdc_cfg.bayer;
  540. struct ccdc_config_params_raw *config_params =
  541. &ccdc_cfg.bayer.config_params;
  542. unsigned int val;
  543. dev_dbg(ccdc_cfg.dev, "\nStarting ccdc_config_raw...");
  544. /* restore power on defaults to register */
  545. ccdc_restore_defaults();
  546. /* CCDCFG register:
  547. * set CCD Not to swap input since input is RAW data
  548. * set FID detection function to Latch at V-Sync
  549. * set WENLOG - ccdc valid area to AND
  550. * set TRGSEL to WENBIT
  551. * set EXTRG to DISABLE
  552. * disable latching function on VSYNC - shadowed registers
  553. */
  554. regw(CCDC_YCINSWP_RAW | CCDC_CCDCFG_FIDMD_LATCH_VSYNC |
  555. CCDC_CCDCFG_WENLOG_AND | CCDC_CCDCFG_TRGSEL_WEN |
  556. CCDC_CCDCFG_EXTRG_DISABLE | CCDC_LATCH_ON_VSYNC_DISABLE, CCDCFG);
  557. /*
  558. * Set VDHD direction to input, input type to raw input
  559. * normal data polarity, do not use external WEN
  560. */
  561. val = (CCDC_VDHDOUT_INPUT | CCDC_RAW_IP_MODE | CCDC_DATAPOL_NORMAL |
  562. CCDC_EXWEN_DISABLE);
  563. /*
  564. * Configure the vertical sync polarity (MODESET.VDPOL), horizontal
  565. * sync polarity (MODESET.HDPOL), field id polarity (MODESET.FLDPOL),
  566. * frame format(progressive or interlace), & pixel format (Input mode)
  567. */
  568. val |= (((params->vd_pol & CCDC_VD_POL_MASK) << CCDC_VD_POL_SHIFT) |
  569. ((params->hd_pol & CCDC_HD_POL_MASK) << CCDC_HD_POL_SHIFT) |
  570. ((params->fid_pol & CCDC_FID_POL_MASK) << CCDC_FID_POL_SHIFT) |
  571. ((params->frm_fmt & CCDC_FRM_FMT_MASK) << CCDC_FRM_FMT_SHIFT) |
  572. ((params->pix_fmt & CCDC_PIX_FMT_MASK) << CCDC_PIX_FMT_SHIFT));
  573. /* set pack for alaw compression */
  574. if ((config_params->data_sz == CCDC_DATA_8BITS) ||
  575. config_params->alaw.enable)
  576. val |= CCDC_DATA_PACK_ENABLE;
  577. /* Configure for LPF */
  578. if (config_params->lpf_enable)
  579. val |= (config_params->lpf_enable & CCDC_LPF_MASK) <<
  580. CCDC_LPF_SHIFT;
  581. /* Configure the data shift */
  582. val |= (config_params->datasft & CCDC_DATASFT_MASK) <<
  583. CCDC_DATASFT_SHIFT;
  584. regw(val , MODESET);
  585. dev_dbg(ccdc_cfg.dev, "\nWriting 0x%x to MODESET...\n", val);
  586. /* Configure the Median Filter threshold */
  587. regw((config_params->med_filt_thres) & CCDC_MED_FILT_THRESH, MEDFILT);
  588. /* Configure GAMMAWD register. defaur 11-2, and Mosaic cfa pattern */
  589. val = CCDC_GAMMA_BITS_11_2 << CCDC_GAMMAWD_INPUT_SHIFT |
  590. CCDC_CFA_MOSAIC;
  591. /* Enable and configure aLaw register if needed */
  592. if (config_params->alaw.enable) {
  593. val |= (CCDC_ALAW_ENABLE |
  594. ((config_params->alaw.gamma_wd &
  595. CCDC_ALAW_GAMMA_WD_MASK) <<
  596. CCDC_GAMMAWD_INPUT_SHIFT));
  597. }
  598. /* Configure Median filter1 & filter2 */
  599. val |= ((config_params->mfilt1 << CCDC_MFILT1_SHIFT) |
  600. (config_params->mfilt2 << CCDC_MFILT2_SHIFT));
  601. regw(val, GAMMAWD);
  602. dev_dbg(ccdc_cfg.dev, "\nWriting 0x%x to GAMMAWD...\n", val);
  603. /* configure video window */
  604. ccdc_setwin(&params->win, params->frm_fmt, 1);
  605. /* Optical Clamp Averaging */
  606. ccdc_config_black_clamp(&config_params->blk_clamp);
  607. /* Black level compensation */
  608. ccdc_config_black_compense(&config_params->blk_comp);
  609. /* Vertical Defect Correction if needed */
  610. if (ccdc_config_vdfc(&config_params->vertical_dft) < 0)
  611. return -EFAULT;
  612. /* color space conversion */
  613. ccdc_config_csc(&config_params->csc);
  614. /* color pattern */
  615. ccdc_config_color_patterns(&config_params->col_pat_field0,
  616. &config_params->col_pat_field1);
  617. /* Configure the Gain & offset control */
  618. ccdc_config_gain_offset();
  619. dev_dbg(ccdc_cfg.dev, "\nWriting %x to COLPTN...\n", val);
  620. /* Configure DATAOFST register */
  621. val = (config_params->data_offset.horz_offset & CCDC_DATAOFST_MASK) <<
  622. CCDC_DATAOFST_H_SHIFT;
  623. val |= (config_params->data_offset.vert_offset & CCDC_DATAOFST_MASK) <<
  624. CCDC_DATAOFST_V_SHIFT;
  625. regw(val, DATAOFST);
  626. /* configuring HSIZE register */
  627. val = (params->horz_flip_enable & CCDC_HSIZE_FLIP_MASK) <<
  628. CCDC_HSIZE_FLIP_SHIFT;
  629. /* If pack 8 is enable then 1 pixel will take 1 byte */
  630. if ((config_params->data_sz == CCDC_DATA_8BITS) ||
  631. config_params->alaw.enable) {
  632. val |= (((params->win.width) + 31) >> 5) &
  633. CCDC_HSIZE_VAL_MASK;
  634. /* adjust to multiple of 32 */
  635. dev_dbg(ccdc_cfg.dev, "\nWriting 0x%x to HSIZE...\n",
  636. (((params->win.width) + 31) >> 5) &
  637. CCDC_HSIZE_VAL_MASK);
  638. } else {
  639. /* else one pixel will take 2 byte */
  640. val |= (((params->win.width * 2) + 31) >> 5) &
  641. CCDC_HSIZE_VAL_MASK;
  642. dev_dbg(ccdc_cfg.dev, "\nWriting 0x%x to HSIZE...\n",
  643. (((params->win.width * 2) + 31) >> 5) &
  644. CCDC_HSIZE_VAL_MASK);
  645. }
  646. regw(val, HSIZE);
  647. /* Configure SDOFST register */
  648. if (params->frm_fmt == CCDC_FRMFMT_INTERLACED) {
  649. if (params->image_invert_enable) {
  650. /* For interlace inverse mode */
  651. regw(CCDC_SDOFST_INTERLACE_INVERSE, SDOFST);
  652. dev_dbg(ccdc_cfg.dev, "\nWriting %x to SDOFST...\n",
  653. CCDC_SDOFST_INTERLACE_INVERSE);
  654. } else {
  655. /* For interlace non inverse mode */
  656. regw(CCDC_SDOFST_INTERLACE_NORMAL, SDOFST);
  657. dev_dbg(ccdc_cfg.dev, "\nWriting %x to SDOFST...\n",
  658. CCDC_SDOFST_INTERLACE_NORMAL);
  659. }
  660. } else if (params->frm_fmt == CCDC_FRMFMT_PROGRESSIVE) {
  661. if (params->image_invert_enable) {
  662. /* For progessive inverse mode */
  663. regw(CCDC_SDOFST_PROGRESSIVE_INVERSE, SDOFST);
  664. dev_dbg(ccdc_cfg.dev, "\nWriting %x to SDOFST...\n",
  665. CCDC_SDOFST_PROGRESSIVE_INVERSE);
  666. } else {
  667. /* For progessive non inverse mode */
  668. regw(CCDC_SDOFST_PROGRESSIVE_NORMAL, SDOFST);
  669. dev_dbg(ccdc_cfg.dev, "\nWriting %x to SDOFST...\n",
  670. CCDC_SDOFST_PROGRESSIVE_NORMAL);
  671. }
  672. }
  673. dev_dbg(ccdc_cfg.dev, "\nend of ccdc_config_raw...");
  674. return 0;
  675. }
  676. static int ccdc_configure(void)
  677. {
  678. if (ccdc_cfg.if_type == VPFE_RAW_BAYER)
  679. return ccdc_config_raw();
  680. else
  681. ccdc_config_ycbcr();
  682. return 0;
  683. }
  684. static int ccdc_set_buftype(enum ccdc_buftype buf_type)
  685. {
  686. if (ccdc_cfg.if_type == VPFE_RAW_BAYER)
  687. ccdc_cfg.bayer.buf_type = buf_type;
  688. else
  689. ccdc_cfg.ycbcr.buf_type = buf_type;
  690. return 0;
  691. }
  692. static enum ccdc_buftype ccdc_get_buftype(void)
  693. {
  694. if (ccdc_cfg.if_type == VPFE_RAW_BAYER)
  695. return ccdc_cfg.bayer.buf_type;
  696. return ccdc_cfg.ycbcr.buf_type;
  697. }
  698. static int ccdc_enum_pix(u32 *pix, int i)
  699. {
  700. int ret = -EINVAL;
  701. if (ccdc_cfg.if_type == VPFE_RAW_BAYER) {
  702. if (i < ARRAY_SIZE(ccdc_raw_bayer_pix_formats)) {
  703. *pix = ccdc_raw_bayer_pix_formats[i];
  704. ret = 0;
  705. }
  706. } else {
  707. if (i < ARRAY_SIZE(ccdc_raw_yuv_pix_formats)) {
  708. *pix = ccdc_raw_yuv_pix_formats[i];
  709. ret = 0;
  710. }
  711. }
  712. return ret;
  713. }
  714. static int ccdc_set_pixel_format(u32 pixfmt)
  715. {
  716. struct ccdc_a_law *alaw = &ccdc_cfg.bayer.config_params.alaw;
  717. if (ccdc_cfg.if_type == VPFE_RAW_BAYER) {
  718. ccdc_cfg.bayer.pix_fmt = CCDC_PIXFMT_RAW;
  719. if (pixfmt == V4L2_PIX_FMT_SBGGR8)
  720. alaw->enable = 1;
  721. else if (pixfmt != V4L2_PIX_FMT_SBGGR16)
  722. return -EINVAL;
  723. } else {
  724. if (pixfmt == V4L2_PIX_FMT_YUYV)
  725. ccdc_cfg.ycbcr.pix_order = CCDC_PIXORDER_YCBYCR;
  726. else if (pixfmt == V4L2_PIX_FMT_UYVY)
  727. ccdc_cfg.ycbcr.pix_order = CCDC_PIXORDER_CBYCRY;
  728. else
  729. return -EINVAL;
  730. }
  731. return 0;
  732. }
  733. static u32 ccdc_get_pixel_format(void)
  734. {
  735. struct ccdc_a_law *alaw = &ccdc_cfg.bayer.config_params.alaw;
  736. u32 pixfmt;
  737. if (ccdc_cfg.if_type == VPFE_RAW_BAYER)
  738. if (alaw->enable)
  739. pixfmt = V4L2_PIX_FMT_SBGGR8;
  740. else
  741. pixfmt = V4L2_PIX_FMT_SBGGR16;
  742. else {
  743. if (ccdc_cfg.ycbcr.pix_order == CCDC_PIXORDER_YCBYCR)
  744. pixfmt = V4L2_PIX_FMT_YUYV;
  745. else
  746. pixfmt = V4L2_PIX_FMT_UYVY;
  747. }
  748. return pixfmt;
  749. }
  750. static int ccdc_set_image_window(struct v4l2_rect *win)
  751. {
  752. if (ccdc_cfg.if_type == VPFE_RAW_BAYER)
  753. ccdc_cfg.bayer.win = *win;
  754. else
  755. ccdc_cfg.ycbcr.win = *win;
  756. return 0;
  757. }
  758. static void ccdc_get_image_window(struct v4l2_rect *win)
  759. {
  760. if (ccdc_cfg.if_type == VPFE_RAW_BAYER)
  761. *win = ccdc_cfg.bayer.win;
  762. else
  763. *win = ccdc_cfg.ycbcr.win;
  764. }
  765. static unsigned int ccdc_get_line_length(void)
  766. {
  767. struct ccdc_config_params_raw *config_params =
  768. &ccdc_cfg.bayer.config_params;
  769. unsigned int len;
  770. if (ccdc_cfg.if_type == VPFE_RAW_BAYER) {
  771. if ((config_params->alaw.enable) ||
  772. (config_params->data_sz == CCDC_DATA_8BITS))
  773. len = ccdc_cfg.bayer.win.width;
  774. else
  775. len = ccdc_cfg.bayer.win.width * 2;
  776. } else
  777. len = ccdc_cfg.ycbcr.win.width * 2;
  778. return ALIGN(len, 32);
  779. }
  780. static int ccdc_set_frame_format(enum ccdc_frmfmt frm_fmt)
  781. {
  782. if (ccdc_cfg.if_type == VPFE_RAW_BAYER)
  783. ccdc_cfg.bayer.frm_fmt = frm_fmt;
  784. else
  785. ccdc_cfg.ycbcr.frm_fmt = frm_fmt;
  786. return 0;
  787. }
  788. static enum ccdc_frmfmt ccdc_get_frame_format(void)
  789. {
  790. if (ccdc_cfg.if_type == VPFE_RAW_BAYER)
  791. return ccdc_cfg.bayer.frm_fmt;
  792. else
  793. return ccdc_cfg.ycbcr.frm_fmt;
  794. }
  795. static int ccdc_getfid(void)
  796. {
  797. return (regr(MODESET) >> 15) & 1;
  798. }
  799. /* misc operations */
  800. static inline void ccdc_setfbaddr(unsigned long addr)
  801. {
  802. regw((addr >> 21) & 0x007f, STADRH);
  803. regw((addr >> 5) & 0x0ffff, STADRL);
  804. }
  805. static int ccdc_set_hw_if_params(struct vpfe_hw_if_param *params)
  806. {
  807. ccdc_cfg.if_type = params->if_type;
  808. switch (params->if_type) {
  809. case VPFE_BT656:
  810. case VPFE_YCBCR_SYNC_16:
  811. case VPFE_YCBCR_SYNC_8:
  812. ccdc_cfg.ycbcr.vd_pol = params->vdpol;
  813. ccdc_cfg.ycbcr.hd_pol = params->hdpol;
  814. break;
  815. default:
  816. /* TODO add support for raw bayer here */
  817. return -EINVAL;
  818. }
  819. return 0;
  820. }
  821. static struct ccdc_hw_device ccdc_hw_dev = {
  822. .name = "DM355 CCDC",
  823. .owner = THIS_MODULE,
  824. .hw_ops = {
  825. .open = ccdc_open,
  826. .close = ccdc_close,
  827. .enable = ccdc_enable,
  828. .enable_out_to_sdram = ccdc_enable_output_to_sdram,
  829. .set_hw_if_params = ccdc_set_hw_if_params,
  830. .set_params = ccdc_set_params,
  831. .configure = ccdc_configure,
  832. .set_buftype = ccdc_set_buftype,
  833. .get_buftype = ccdc_get_buftype,
  834. .enum_pix = ccdc_enum_pix,
  835. .set_pixel_format = ccdc_set_pixel_format,
  836. .get_pixel_format = ccdc_get_pixel_format,
  837. .set_frame_format = ccdc_set_frame_format,
  838. .get_frame_format = ccdc_get_frame_format,
  839. .set_image_window = ccdc_set_image_window,
  840. .get_image_window = ccdc_get_image_window,
  841. .get_line_length = ccdc_get_line_length,
  842. .setfbaddr = ccdc_setfbaddr,
  843. .getfid = ccdc_getfid,
  844. },
  845. };
  846. static int dm355_ccdc_probe(struct platform_device *pdev)
  847. {
  848. void (*setup_pinmux)(void);
  849. struct resource *res;
  850. int status = 0;
  851. /*
  852. * first try to register with vpfe. If not correct platform, then we
  853. * don't have to iomap
  854. */
  855. status = vpfe_register_ccdc_device(&ccdc_hw_dev);
  856. if (status < 0)
  857. return status;
  858. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  859. if (!res) {
  860. status = -ENODEV;
  861. goto fail_nores;
  862. }
  863. res = request_mem_region(res->start, resource_size(res), res->name);
  864. if (!res) {
  865. status = -EBUSY;
  866. goto fail_nores;
  867. }
  868. ccdc_cfg.base_addr = ioremap_nocache(res->start, resource_size(res));
  869. if (!ccdc_cfg.base_addr) {
  870. status = -ENOMEM;
  871. goto fail_nomem;
  872. }
  873. /* Platform data holds setup_pinmux function ptr */
  874. if (NULL == pdev->dev.platform_data) {
  875. status = -ENODEV;
  876. goto fail_nomap;
  877. }
  878. setup_pinmux = pdev->dev.platform_data;
  879. /*
  880. * setup Mux configuration for ccdc which may be different for
  881. * different SoCs using this CCDC
  882. */
  883. setup_pinmux();
  884. ccdc_cfg.dev = &pdev->dev;
  885. printk(KERN_NOTICE "%s is registered with vpfe.\n", ccdc_hw_dev.name);
  886. return 0;
  887. fail_nomap:
  888. iounmap(ccdc_cfg.base_addr);
  889. fail_nomem:
  890. release_mem_region(res->start, resource_size(res));
  891. fail_nores:
  892. vpfe_unregister_ccdc_device(&ccdc_hw_dev);
  893. return status;
  894. }
  895. static int dm355_ccdc_remove(struct platform_device *pdev)
  896. {
  897. struct resource *res;
  898. iounmap(ccdc_cfg.base_addr);
  899. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  900. if (res)
  901. release_mem_region(res->start, resource_size(res));
  902. vpfe_unregister_ccdc_device(&ccdc_hw_dev);
  903. return 0;
  904. }
  905. static struct platform_driver dm355_ccdc_driver = {
  906. .driver = {
  907. .name = "dm355_ccdc",
  908. },
  909. .remove = dm355_ccdc_remove,
  910. .probe = dm355_ccdc_probe,
  911. };
  912. module_platform_driver(dm355_ccdc_driver);