cx25821-medusa-video.c 20 KB

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