cx25821-medusa-video.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. /*
  2. * Driver for the Conexant CX25821 PCIe bridge
  3. *
  4. * Copyright (C) 2009 Conexant Systems Inc.
  5. * Authors <shu.lin@conexant.com>, <hiep.huynh@conexant.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. *
  16. * GNU General Public License for more details.
  17. */
  18. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include "cx25821.h"
  20. #include "cx25821-medusa-video.h"
  21. #include "cx25821-biffuncs.h"
  22. /*
  23. * medusa_enable_bluefield_output()
  24. *
  25. * Enable the generation of blue filed output if no video
  26. *
  27. */
  28. static void medusa_enable_bluefield_output(struct cx25821_dev *dev, int channel,
  29. int enable)
  30. {
  31. u32 value = 0;
  32. u32 tmp = 0;
  33. int out_ctrl = OUT_CTRL1;
  34. int out_ctrl_ns = OUT_CTRL_NS;
  35. switch (channel) {
  36. default:
  37. case VDEC_A:
  38. break;
  39. case VDEC_B:
  40. out_ctrl = VDEC_B_OUT_CTRL1;
  41. out_ctrl_ns = VDEC_B_OUT_CTRL_NS;
  42. break;
  43. case VDEC_C:
  44. out_ctrl = VDEC_C_OUT_CTRL1;
  45. out_ctrl_ns = VDEC_C_OUT_CTRL_NS;
  46. break;
  47. case VDEC_D:
  48. out_ctrl = VDEC_D_OUT_CTRL1;
  49. out_ctrl_ns = VDEC_D_OUT_CTRL_NS;
  50. break;
  51. case VDEC_E:
  52. out_ctrl = VDEC_E_OUT_CTRL1;
  53. out_ctrl_ns = VDEC_E_OUT_CTRL_NS;
  54. return;
  55. case VDEC_F:
  56. out_ctrl = VDEC_F_OUT_CTRL1;
  57. out_ctrl_ns = VDEC_F_OUT_CTRL_NS;
  58. return;
  59. case VDEC_G:
  60. out_ctrl = VDEC_G_OUT_CTRL1;
  61. out_ctrl_ns = VDEC_G_OUT_CTRL_NS;
  62. return;
  63. case VDEC_H:
  64. out_ctrl = VDEC_H_OUT_CTRL1;
  65. out_ctrl_ns = VDEC_H_OUT_CTRL_NS;
  66. return;
  67. }
  68. value = cx25821_i2c_read(&dev->i2c_bus[0], out_ctrl, &tmp);
  69. value &= 0xFFFFFF7F; /* clear BLUE_FIELD_EN */
  70. if (enable)
  71. value |= 0x00000080; /* set BLUE_FIELD_EN */
  72. cx25821_i2c_write(&dev->i2c_bus[0], out_ctrl, value);
  73. value = cx25821_i2c_read(&dev->i2c_bus[0], out_ctrl_ns, &tmp);
  74. value &= 0xFFFFFF7F;
  75. if (enable)
  76. value |= 0x00000080; /* set BLUE_FIELD_EN */
  77. cx25821_i2c_write(&dev->i2c_bus[0], out_ctrl_ns, value);
  78. }
  79. static int medusa_initialize_ntsc(struct cx25821_dev *dev)
  80. {
  81. int ret_val = 0;
  82. int i = 0;
  83. u32 value = 0;
  84. u32 tmp = 0;
  85. for (i = 0; i < MAX_DECODERS; i++) {
  86. /* set video format NTSC-M */
  87. value = cx25821_i2c_read(&dev->i2c_bus[0],
  88. MODE_CTRL + (0x200 * i), &tmp);
  89. value &= 0xFFFFFFF0;
  90. /* enable the fast locking mode bit[16] */
  91. value |= 0x10001;
  92. ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
  93. MODE_CTRL + (0x200 * i), value);
  94. /* resolution NTSC 720x480 */
  95. value = cx25821_i2c_read(&dev->i2c_bus[0],
  96. HORIZ_TIM_CTRL + (0x200 * i), &tmp);
  97. value &= 0x00C00C00;
  98. value |= 0x612D0074;
  99. ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
  100. HORIZ_TIM_CTRL + (0x200 * i), value);
  101. value = cx25821_i2c_read(&dev->i2c_bus[0],
  102. VERT_TIM_CTRL + (0x200 * i), &tmp);
  103. value &= 0x00C00C00;
  104. value |= 0x1C1E001A; /* vblank_cnt + 2 to get camera ID */
  105. ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
  106. VERT_TIM_CTRL + (0x200 * i), value);
  107. /* chroma subcarrier step size */
  108. ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
  109. SC_STEP_SIZE + (0x200 * i), 0x43E00000);
  110. /* enable VIP optional active */
  111. value = cx25821_i2c_read(&dev->i2c_bus[0],
  112. OUT_CTRL_NS + (0x200 * i), &tmp);
  113. value &= 0xFFFBFFFF;
  114. value |= 0x00040000;
  115. ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
  116. OUT_CTRL_NS + (0x200 * i), value);
  117. /* enable VIP optional active (VIP_OPT_AL) for direct output. */
  118. value = cx25821_i2c_read(&dev->i2c_bus[0],
  119. OUT_CTRL1 + (0x200 * i), &tmp);
  120. value &= 0xFFFBFFFF;
  121. value |= 0x00040000;
  122. ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
  123. OUT_CTRL1 + (0x200 * i), value);
  124. /*
  125. * clear VPRES_VERT_EN bit, fixes the chroma run away problem
  126. * when the input switching rate < 16 fields
  127. */
  128. value = cx25821_i2c_read(&dev->i2c_bus[0],
  129. MISC_TIM_CTRL + (0x200 * i), &tmp);
  130. /* disable special play detection */
  131. value = setBitAtPos(value, 14);
  132. value = clearBitAtPos(value, 15);
  133. ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
  134. MISC_TIM_CTRL + (0x200 * i), value);
  135. /* set vbi_gate_en to 0 */
  136. value = cx25821_i2c_read(&dev->i2c_bus[0],
  137. DFE_CTRL1 + (0x200 * i), &tmp);
  138. value = clearBitAtPos(value, 29);
  139. ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
  140. DFE_CTRL1 + (0x200 * i), value);
  141. /* Enable the generation of blue field output if no video */
  142. medusa_enable_bluefield_output(dev, i, 1);
  143. }
  144. for (i = 0; i < MAX_ENCODERS; i++) {
  145. /* NTSC hclock */
  146. value = cx25821_i2c_read(&dev->i2c_bus[0],
  147. DENC_A_REG_1 + (0x100 * i), &tmp);
  148. value &= 0xF000FC00;
  149. value |= 0x06B402D0;
  150. ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
  151. DENC_A_REG_1 + (0x100 * i), value);
  152. /* burst begin and burst end */
  153. value = cx25821_i2c_read(&dev->i2c_bus[0],
  154. DENC_A_REG_2 + (0x100 * i), &tmp);
  155. value &= 0xFF000000;
  156. value |= 0x007E9054;
  157. ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
  158. DENC_A_REG_2 + (0x100 * i), value);
  159. value = cx25821_i2c_read(&dev->i2c_bus[0],
  160. DENC_A_REG_3 + (0x100 * i), &tmp);
  161. value &= 0xFC00FE00;
  162. value |= 0x00EC00F0;
  163. ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
  164. DENC_A_REG_3 + (0x100 * i), value);
  165. /* set NTSC vblank, no phase alternation, 7.5 IRE pedestal */
  166. value = cx25821_i2c_read(&dev->i2c_bus[0],
  167. DENC_A_REG_4 + (0x100 * i), &tmp);
  168. value &= 0x00FCFFFF;
  169. value |= 0x13020000;
  170. ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
  171. DENC_A_REG_4 + (0x100 * i), value);
  172. value = cx25821_i2c_read(&dev->i2c_bus[0],
  173. DENC_A_REG_5 + (0x100 * i), &tmp);
  174. value &= 0xFFFF0000;
  175. value |= 0x0000E575;
  176. ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
  177. DENC_A_REG_5 + (0x100 * i), value);
  178. ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
  179. DENC_A_REG_6 + (0x100 * i), 0x009A89C1);
  180. /* Subcarrier Increment */
  181. ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
  182. DENC_A_REG_7 + (0x100 * i), 0x21F07C1F);
  183. }
  184. /* set picture resolutions */
  185. /* 0 - 720 */
  186. ret_val = cx25821_i2c_write(&dev->i2c_bus[0], HSCALE_CTRL, 0x0);
  187. /* 0 - 480 */
  188. ret_val = cx25821_i2c_write(&dev->i2c_bus[0], VSCALE_CTRL, 0x0);
  189. /* set Bypass input format to NTSC 525 lines */
  190. value = cx25821_i2c_read(&dev->i2c_bus[0], BYP_AB_CTRL, &tmp);
  191. value |= 0x00080200;
  192. ret_val = cx25821_i2c_write(&dev->i2c_bus[0], BYP_AB_CTRL, value);
  193. return ret_val;
  194. }
  195. static int medusa_PALCombInit(struct cx25821_dev *dev, int dec)
  196. {
  197. int ret_val = -1;
  198. u32 value = 0, tmp = 0;
  199. /* Setup for 2D threshold */
  200. ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
  201. COMB_2D_HFS_CFG + (0x200 * dec), 0x20002861);
  202. ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
  203. COMB_2D_HFD_CFG + (0x200 * dec), 0x20002861);
  204. ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
  205. COMB_2D_LF_CFG + (0x200 * dec), 0x200A1023);
  206. /* Setup flat chroma and luma thresholds */
  207. value = cx25821_i2c_read(&dev->i2c_bus[0],
  208. COMB_FLAT_THRESH_CTRL + (0x200 * dec), &tmp);
  209. value &= 0x06230000;
  210. ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
  211. COMB_FLAT_THRESH_CTRL + (0x200 * dec), value);
  212. /* set comb 2D blend */
  213. ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
  214. COMB_2D_BLEND + (0x200 * dec), 0x210F0F0F);
  215. /* COMB MISC CONTROL */
  216. ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
  217. COMB_MISC_CTRL + (0x200 * dec), 0x41120A7F);
  218. return ret_val;
  219. }
  220. static int medusa_initialize_pal(struct cx25821_dev *dev)
  221. {
  222. int ret_val = 0;
  223. int i = 0;
  224. u32 value = 0;
  225. u32 tmp = 0;
  226. for (i = 0; i < MAX_DECODERS; i++) {
  227. /* set video format PAL-BDGHI */
  228. value = cx25821_i2c_read(&dev->i2c_bus[0],
  229. MODE_CTRL + (0x200 * i), &tmp);
  230. value &= 0xFFFFFFF0;
  231. /* enable the fast locking mode bit[16] */
  232. value |= 0x10004;
  233. ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
  234. MODE_CTRL + (0x200 * i), value);
  235. /* resolution PAL 720x576 */
  236. value = cx25821_i2c_read(&dev->i2c_bus[0],
  237. HORIZ_TIM_CTRL + (0x200 * i), &tmp);
  238. value &= 0x00C00C00;
  239. value |= 0x632D007D;
  240. ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
  241. HORIZ_TIM_CTRL + (0x200 * i), value);
  242. /* vblank656_cnt=x26, vactive_cnt=240h, vblank_cnt=x24 */
  243. value = cx25821_i2c_read(&dev->i2c_bus[0],
  244. VERT_TIM_CTRL + (0x200 * i), &tmp);
  245. value &= 0x00C00C00;
  246. value |= 0x28240026; /* vblank_cnt + 2 to get camera ID */
  247. ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
  248. VERT_TIM_CTRL + (0x200 * i), value);
  249. /* chroma subcarrier step size */
  250. ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
  251. SC_STEP_SIZE + (0x200 * i), 0x5411E2D0);
  252. /* enable VIP optional active */
  253. value = cx25821_i2c_read(&dev->i2c_bus[0],
  254. OUT_CTRL_NS + (0x200 * i), &tmp);
  255. value &= 0xFFFBFFFF;
  256. value |= 0x00040000;
  257. ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
  258. OUT_CTRL_NS + (0x200 * i), value);
  259. /* enable VIP optional active (VIP_OPT_AL) for direct output. */
  260. value = cx25821_i2c_read(&dev->i2c_bus[0],
  261. OUT_CTRL1 + (0x200 * i), &tmp);
  262. value &= 0xFFFBFFFF;
  263. value |= 0x00040000;
  264. ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
  265. OUT_CTRL1 + (0x200 * i), value);
  266. /*
  267. * clear VPRES_VERT_EN bit, fixes the chroma run away problem
  268. * when the input switching rate < 16 fields
  269. */
  270. value = cx25821_i2c_read(&dev->i2c_bus[0],
  271. MISC_TIM_CTRL + (0x200 * i), &tmp);
  272. /* disable special play detection */
  273. value = setBitAtPos(value, 14);
  274. value = clearBitAtPos(value, 15);
  275. ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
  276. MISC_TIM_CTRL + (0x200 * i), value);
  277. /* set vbi_gate_en to 0 */
  278. value = cx25821_i2c_read(&dev->i2c_bus[0],
  279. DFE_CTRL1 + (0x200 * i), &tmp);
  280. value = clearBitAtPos(value, 29);
  281. ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
  282. DFE_CTRL1 + (0x200 * i), value);
  283. medusa_PALCombInit(dev, i);
  284. /* Enable the generation of blue field output if no video */
  285. medusa_enable_bluefield_output(dev, i, 1);
  286. }
  287. for (i = 0; i < MAX_ENCODERS; i++) {
  288. /* PAL hclock */
  289. value = cx25821_i2c_read(&dev->i2c_bus[0],
  290. DENC_A_REG_1 + (0x100 * i), &tmp);
  291. value &= 0xF000FC00;
  292. value |= 0x06C002D0;
  293. ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
  294. DENC_A_REG_1 + (0x100 * i), value);
  295. /* burst begin and burst end */
  296. value = cx25821_i2c_read(&dev->i2c_bus[0],
  297. DENC_A_REG_2 + (0x100 * i), &tmp);
  298. value &= 0xFF000000;
  299. value |= 0x007E9754;
  300. ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
  301. DENC_A_REG_2 + (0x100 * i), value);
  302. /* hblank and vactive */
  303. value = cx25821_i2c_read(&dev->i2c_bus[0],
  304. DENC_A_REG_3 + (0x100 * i), &tmp);
  305. value &= 0xFC00FE00;
  306. value |= 0x00FC0120;
  307. ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
  308. DENC_A_REG_3 + (0x100 * i), value);
  309. /* set PAL vblank, phase alternation, 0 IRE pedestal */
  310. value = cx25821_i2c_read(&dev->i2c_bus[0],
  311. DENC_A_REG_4 + (0x100 * i), &tmp);
  312. value &= 0x00FCFFFF;
  313. value |= 0x14010000;
  314. ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
  315. DENC_A_REG_4 + (0x100 * i), value);
  316. value = cx25821_i2c_read(&dev->i2c_bus[0],
  317. DENC_A_REG_5 + (0x100 * i), &tmp);
  318. value &= 0xFFFF0000;
  319. value |= 0x0000F078;
  320. ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
  321. DENC_A_REG_5 + (0x100 * i), value);
  322. ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
  323. DENC_A_REG_6 + (0x100 * i), 0x00A493CF);
  324. /* Subcarrier Increment */
  325. ret_val = cx25821_i2c_write(&dev->i2c_bus[0],
  326. DENC_A_REG_7 + (0x100 * i), 0x2A098ACB);
  327. }
  328. /* set picture resolutions */
  329. /* 0 - 720 */
  330. ret_val = cx25821_i2c_write(&dev->i2c_bus[0], HSCALE_CTRL, 0x0);
  331. /* 0 - 576 */
  332. ret_val = cx25821_i2c_write(&dev->i2c_bus[0], VSCALE_CTRL, 0x0);
  333. /* set Bypass input format to PAL 625 lines */
  334. value = cx25821_i2c_read(&dev->i2c_bus[0], BYP_AB_CTRL, &tmp);
  335. value &= 0xFFF7FDFF;
  336. ret_val = cx25821_i2c_write(&dev->i2c_bus[0], BYP_AB_CTRL, value);
  337. return ret_val;
  338. }
  339. int medusa_set_videostandard(struct cx25821_dev *dev)
  340. {
  341. int status = 0;
  342. u32 value = 0, tmp = 0;
  343. if (dev->tvnorm & V4L2_STD_PAL_BG || dev->tvnorm & V4L2_STD_PAL_DK)
  344. status = medusa_initialize_pal(dev);
  345. else
  346. status = medusa_initialize_ntsc(dev);
  347. /* Enable DENC_A output */
  348. value = cx25821_i2c_read(&dev->i2c_bus[0], DENC_A_REG_4, &tmp);
  349. value = setBitAtPos(value, 4);
  350. status = cx25821_i2c_write(&dev->i2c_bus[0], DENC_A_REG_4, value);
  351. /* Enable DENC_B output */
  352. value = cx25821_i2c_read(&dev->i2c_bus[0], DENC_B_REG_4, &tmp);
  353. value = setBitAtPos(value, 4);
  354. status = cx25821_i2c_write(&dev->i2c_bus[0], DENC_B_REG_4, value);
  355. return status;
  356. }
  357. void medusa_set_resolution(struct cx25821_dev *dev, int width,
  358. int decoder_select)
  359. {
  360. int decoder = 0;
  361. int decoder_count = 0;
  362. u32 hscale = 0x0;
  363. u32 vscale = 0x0;
  364. const int MAX_WIDTH = 720;
  365. /* validate the width */
  366. if (width > MAX_WIDTH) {
  367. pr_info("%s(): width %d > MAX_WIDTH %d ! resetting to MAX_WIDTH\n",
  368. __func__, width, MAX_WIDTH);
  369. width = MAX_WIDTH;
  370. }
  371. if (decoder_select <= 7 && decoder_select >= 0) {
  372. decoder = decoder_select;
  373. decoder_count = decoder_select + 1;
  374. } else {
  375. decoder = 0;
  376. decoder_count = dev->_max_num_decoders;
  377. }
  378. switch (width) {
  379. case 320:
  380. hscale = 0x13E34B;
  381. vscale = 0x0;
  382. break;
  383. case 352:
  384. hscale = 0x10A273;
  385. vscale = 0x0;
  386. break;
  387. case 176:
  388. hscale = 0x3115B2;
  389. vscale = 0x1E00;
  390. break;
  391. case 160:
  392. hscale = 0x378D84;
  393. vscale = 0x1E00;
  394. break;
  395. default: /* 720 */
  396. hscale = 0x0;
  397. vscale = 0x0;
  398. break;
  399. }
  400. for (; decoder < decoder_count; decoder++) {
  401. /* write scaling values for each decoder */
  402. cx25821_i2c_write(&dev->i2c_bus[0],
  403. HSCALE_CTRL + (0x200 * decoder), hscale);
  404. cx25821_i2c_write(&dev->i2c_bus[0],
  405. VSCALE_CTRL + (0x200 * decoder), vscale);
  406. }
  407. }
  408. static void medusa_set_decoderduration(struct cx25821_dev *dev, int decoder,
  409. int duration)
  410. {
  411. u32 fld_cnt = 0;
  412. u32 tmp = 0;
  413. u32 disp_cnt_reg = DISP_AB_CNT;
  414. /* no support */
  415. if (decoder < VDEC_A || decoder > VDEC_H) {
  416. return;
  417. }
  418. switch (decoder) {
  419. default:
  420. break;
  421. case VDEC_C:
  422. case VDEC_D:
  423. disp_cnt_reg = DISP_CD_CNT;
  424. break;
  425. case VDEC_E:
  426. case VDEC_F:
  427. disp_cnt_reg = DISP_EF_CNT;
  428. break;
  429. case VDEC_G:
  430. case VDEC_H:
  431. disp_cnt_reg = DISP_GH_CNT;
  432. break;
  433. }
  434. /* update hardware */
  435. fld_cnt = cx25821_i2c_read(&dev->i2c_bus[0], disp_cnt_reg, &tmp);
  436. if (!(decoder % 2)) { /* EVEN decoder */
  437. fld_cnt &= 0xFFFF0000;
  438. fld_cnt |= duration;
  439. } else {
  440. fld_cnt &= 0x0000FFFF;
  441. fld_cnt |= ((u32) duration) << 16;
  442. }
  443. cx25821_i2c_write(&dev->i2c_bus[0], disp_cnt_reg, fld_cnt);
  444. }
  445. /* Map to Medusa register setting */
  446. static int mapM(int srcMin, int srcMax, int srcVal, int dstMin, int dstMax,
  447. int *dstVal)
  448. {
  449. int numerator;
  450. int denominator;
  451. int quotient;
  452. if ((srcMin == srcMax) || (srcVal < srcMin) || (srcVal > srcMax))
  453. return -1;
  454. /*
  455. * This is the overall expression used:
  456. * *dstVal =
  457. * (srcVal - srcMin)*(dstMax - dstMin) / (srcMax - srcMin) + dstMin;
  458. * but we need to account for rounding so below we use the modulus
  459. * operator to find the remainder and increment if necessary.
  460. */
  461. numerator = (srcVal - srcMin) * (dstMax - dstMin);
  462. denominator = srcMax - srcMin;
  463. quotient = numerator / denominator;
  464. if (2 * (numerator % denominator) >= denominator)
  465. quotient++;
  466. *dstVal = quotient + dstMin;
  467. return 0;
  468. }
  469. static unsigned long convert_to_twos(long numeric, unsigned long bits_len)
  470. {
  471. unsigned char temp;
  472. if (numeric >= 0)
  473. return numeric;
  474. else {
  475. temp = ~(abs(numeric) & 0xFF);
  476. temp += 1;
  477. return temp;
  478. }
  479. }
  480. int medusa_set_brightness(struct cx25821_dev *dev, int brightness, int decoder)
  481. {
  482. int ret_val = 0;
  483. int value = 0;
  484. u32 val = 0, tmp = 0;
  485. if ((brightness > VIDEO_PROCAMP_MAX) ||
  486. (brightness < VIDEO_PROCAMP_MIN)) {
  487. return -1;
  488. }
  489. ret_val = mapM(VIDEO_PROCAMP_MIN, VIDEO_PROCAMP_MAX, brightness,
  490. SIGNED_BYTE_MIN, SIGNED_BYTE_MAX, &value);
  491. value = convert_to_twos(value, 8);
  492. val = cx25821_i2c_read(&dev->i2c_bus[0],
  493. VDEC_A_BRITE_CTRL + (0x200 * decoder), &tmp);
  494. val &= 0xFFFFFF00;
  495. ret_val |= cx25821_i2c_write(&dev->i2c_bus[0],
  496. VDEC_A_BRITE_CTRL + (0x200 * decoder), val | value);
  497. return ret_val;
  498. }
  499. int medusa_set_contrast(struct cx25821_dev *dev, int contrast, int decoder)
  500. {
  501. int ret_val = 0;
  502. int value = 0;
  503. u32 val = 0, tmp = 0;
  504. if ((contrast > VIDEO_PROCAMP_MAX) || (contrast < VIDEO_PROCAMP_MIN)) {
  505. return -1;
  506. }
  507. ret_val = mapM(VIDEO_PROCAMP_MIN, VIDEO_PROCAMP_MAX, contrast,
  508. UNSIGNED_BYTE_MIN, UNSIGNED_BYTE_MAX, &value);
  509. val = cx25821_i2c_read(&dev->i2c_bus[0],
  510. VDEC_A_CNTRST_CTRL + (0x200 * decoder), &tmp);
  511. val &= 0xFFFFFF00;
  512. ret_val |= cx25821_i2c_write(&dev->i2c_bus[0],
  513. VDEC_A_CNTRST_CTRL + (0x200 * decoder), val | value);
  514. return ret_val;
  515. }
  516. int medusa_set_hue(struct cx25821_dev *dev, int hue, int decoder)
  517. {
  518. int ret_val = 0;
  519. int value = 0;
  520. u32 val = 0, tmp = 0;
  521. if ((hue > VIDEO_PROCAMP_MAX) || (hue < VIDEO_PROCAMP_MIN)) {
  522. return -1;
  523. }
  524. ret_val = mapM(VIDEO_PROCAMP_MIN, VIDEO_PROCAMP_MAX, hue,
  525. SIGNED_BYTE_MIN, SIGNED_BYTE_MAX, &value);
  526. value = convert_to_twos(value, 8);
  527. val = cx25821_i2c_read(&dev->i2c_bus[0],
  528. VDEC_A_HUE_CTRL + (0x200 * decoder), &tmp);
  529. val &= 0xFFFFFF00;
  530. ret_val |= cx25821_i2c_write(&dev->i2c_bus[0],
  531. VDEC_A_HUE_CTRL + (0x200 * decoder), val | value);
  532. return ret_val;
  533. }
  534. int medusa_set_saturation(struct cx25821_dev *dev, int saturation, int decoder)
  535. {
  536. int ret_val = 0;
  537. int value = 0;
  538. u32 val = 0, tmp = 0;
  539. if ((saturation > VIDEO_PROCAMP_MAX) ||
  540. (saturation < VIDEO_PROCAMP_MIN)) {
  541. return -1;
  542. }
  543. ret_val = mapM(VIDEO_PROCAMP_MIN, VIDEO_PROCAMP_MAX, saturation,
  544. UNSIGNED_BYTE_MIN, UNSIGNED_BYTE_MAX, &value);
  545. val = cx25821_i2c_read(&dev->i2c_bus[0],
  546. VDEC_A_USAT_CTRL + (0x200 * decoder), &tmp);
  547. val &= 0xFFFFFF00;
  548. ret_val |= cx25821_i2c_write(&dev->i2c_bus[0],
  549. VDEC_A_USAT_CTRL + (0x200 * decoder), val | value);
  550. val = cx25821_i2c_read(&dev->i2c_bus[0],
  551. VDEC_A_VSAT_CTRL + (0x200 * decoder), &tmp);
  552. val &= 0xFFFFFF00;
  553. ret_val |= cx25821_i2c_write(&dev->i2c_bus[0],
  554. VDEC_A_VSAT_CTRL + (0x200 * decoder), val | value);
  555. return ret_val;
  556. }
  557. /* Program the display sequence and monitor output. */
  558. int medusa_video_init(struct cx25821_dev *dev)
  559. {
  560. u32 value = 0, tmp = 0;
  561. int ret_val = 0;
  562. int i = 0;
  563. /* disable Auto source selection on all video decoders */
  564. value = cx25821_i2c_read(&dev->i2c_bus[0], MON_A_CTRL, &tmp);
  565. value &= 0xFFFFF0FF;
  566. ret_val = cx25821_i2c_write(&dev->i2c_bus[0], MON_A_CTRL, value);
  567. if (ret_val < 0)
  568. goto error;
  569. /* Turn off Master source switch enable */
  570. value = cx25821_i2c_read(&dev->i2c_bus[0], MON_A_CTRL, &tmp);
  571. value &= 0xFFFFFFDF;
  572. ret_val = cx25821_i2c_write(&dev->i2c_bus[0], MON_A_CTRL, value);
  573. if (ret_val < 0)
  574. goto error;
  575. /*
  576. * FIXME: due to a coding bug the duration was always 0. It's
  577. * likely that it really should be something else, but due to the
  578. * lack of documentation I have no idea what it should be. For
  579. * now just fill in 0 as the duration.
  580. */
  581. for (i = 0; i < dev->_max_num_decoders; i++)
  582. medusa_set_decoderduration(dev, i, 0);
  583. /* Select monitor as DENC A input, power up the DAC */
  584. value = cx25821_i2c_read(&dev->i2c_bus[0], DENC_AB_CTRL, &tmp);
  585. value &= 0xFF70FF70;
  586. value |= 0x00090008; /* set en_active */
  587. ret_val = cx25821_i2c_write(&dev->i2c_bus[0], DENC_AB_CTRL, value);
  588. if (ret_val < 0)
  589. goto error;
  590. /* enable input is VIP/656 */
  591. value = cx25821_i2c_read(&dev->i2c_bus[0], BYP_AB_CTRL, &tmp);
  592. value |= 0x00040100; /* enable VIP */
  593. ret_val = cx25821_i2c_write(&dev->i2c_bus[0], BYP_AB_CTRL, value);
  594. if (ret_val < 0)
  595. goto error;
  596. /* select AFE clock to output mode */
  597. value = cx25821_i2c_read(&dev->i2c_bus[0], AFE_AB_DIAG_CTRL, &tmp);
  598. value &= 0x83FFFFFF;
  599. ret_val = cx25821_i2c_write(&dev->i2c_bus[0], AFE_AB_DIAG_CTRL,
  600. value | 0x10000000);
  601. if (ret_val < 0)
  602. goto error;
  603. /* Turn on all of the data out and control output pins. */
  604. value = cx25821_i2c_read(&dev->i2c_bus[0], PIN_OE_CTRL, &tmp);
  605. value &= 0xFEF0FE00;
  606. if (dev->_max_num_decoders == MAX_DECODERS) {
  607. /*
  608. * Note: The octal board does not support control pins(bit16-19)
  609. * These bits are ignored in the octal board.
  610. *
  611. * disable VDEC A-C port, default to Mobilygen Interface
  612. */
  613. value |= 0x010001F8;
  614. } else {
  615. /* disable VDEC A-C port, default to Mobilygen Interface */
  616. value |= 0x010F0108;
  617. }
  618. value |= 7;
  619. ret_val = cx25821_i2c_write(&dev->i2c_bus[0], PIN_OE_CTRL, value);
  620. if (ret_val < 0)
  621. goto error;
  622. ret_val = medusa_set_videostandard(dev);
  623. error:
  624. return ret_val;
  625. }