dm355_ccdc.c 29 KB

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