cx25821-medusa-video.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  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 = dev->_max_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. /* update hardware */
  439. fld_cnt = cx25821_i2c_read(&dev->i2c_bus[0], disp_cnt_reg, &tmp);
  440. if (!(decoder % 2)) { /* EVEN decoder */
  441. fld_cnt &= 0xFFFF0000;
  442. fld_cnt |= duration;
  443. } else {
  444. fld_cnt &= 0x0000FFFF;
  445. fld_cnt |= ((u32) duration) << 16;
  446. }
  447. cx25821_i2c_write(&dev->i2c_bus[0], disp_cnt_reg, fld_cnt);
  448. }
  449. /* Map to Medusa register setting */
  450. static int mapM(int srcMin, int srcMax, int srcVal, int dstMin, int dstMax,
  451. int *dstVal)
  452. {
  453. int numerator;
  454. int denominator;
  455. int quotient;
  456. if ((srcMin == srcMax) || (srcVal < srcMin) || (srcVal > srcMax))
  457. return -1;
  458. /*
  459. * This is the overall expression used:
  460. * *dstVal =
  461. * (srcVal - srcMin)*(dstMax - dstMin) / (srcMax - srcMin) + dstMin;
  462. * but we need to account for rounding so below we use the modulus
  463. * operator to find the remainder and increment if necessary.
  464. */
  465. numerator = (srcVal - srcMin) * (dstMax - dstMin);
  466. denominator = srcMax - srcMin;
  467. quotient = numerator / denominator;
  468. if (2 * (numerator % denominator) >= denominator)
  469. quotient++;
  470. *dstVal = quotient + dstMin;
  471. return 0;
  472. }
  473. static unsigned long convert_to_twos(long numeric, unsigned long bits_len)
  474. {
  475. unsigned char temp;
  476. if (numeric >= 0)
  477. return numeric;
  478. else {
  479. temp = ~(abs(numeric) & 0xFF);
  480. temp += 1;
  481. return temp;
  482. }
  483. }
  484. int medusa_set_brightness(struct cx25821_dev *dev, int brightness, int decoder)
  485. {
  486. int ret_val = 0;
  487. int value = 0;
  488. u32 val = 0, tmp = 0;
  489. if ((brightness > VIDEO_PROCAMP_MAX) ||
  490. (brightness < VIDEO_PROCAMP_MIN)) {
  491. return -1;
  492. }
  493. ret_val = mapM(VIDEO_PROCAMP_MIN, VIDEO_PROCAMP_MAX, brightness,
  494. SIGNED_BYTE_MIN, SIGNED_BYTE_MAX, &value);
  495. value = convert_to_twos(value, 8);
  496. val = cx25821_i2c_read(&dev->i2c_bus[0],
  497. VDEC_A_BRITE_CTRL + (0x200 * decoder), &tmp);
  498. val &= 0xFFFFFF00;
  499. ret_val |= cx25821_i2c_write(&dev->i2c_bus[0],
  500. VDEC_A_BRITE_CTRL + (0x200 * decoder), val | value);
  501. return ret_val;
  502. }
  503. int medusa_set_contrast(struct cx25821_dev *dev, int contrast, int decoder)
  504. {
  505. int ret_val = 0;
  506. int value = 0;
  507. u32 val = 0, tmp = 0;
  508. if ((contrast > VIDEO_PROCAMP_MAX) || (contrast < VIDEO_PROCAMP_MIN)) {
  509. return -1;
  510. }
  511. ret_val = mapM(VIDEO_PROCAMP_MIN, VIDEO_PROCAMP_MAX, contrast,
  512. UNSIGNED_BYTE_MIN, UNSIGNED_BYTE_MAX, &value);
  513. val = cx25821_i2c_read(&dev->i2c_bus[0],
  514. VDEC_A_CNTRST_CTRL + (0x200 * decoder), &tmp);
  515. val &= 0xFFFFFF00;
  516. ret_val |= cx25821_i2c_write(&dev->i2c_bus[0],
  517. VDEC_A_CNTRST_CTRL + (0x200 * decoder), val | value);
  518. return ret_val;
  519. }
  520. int medusa_set_hue(struct cx25821_dev *dev, int hue, int decoder)
  521. {
  522. int ret_val = 0;
  523. int value = 0;
  524. u32 val = 0, tmp = 0;
  525. if ((hue > VIDEO_PROCAMP_MAX) || (hue < VIDEO_PROCAMP_MIN)) {
  526. return -1;
  527. }
  528. ret_val = mapM(VIDEO_PROCAMP_MIN, VIDEO_PROCAMP_MAX, hue,
  529. SIGNED_BYTE_MIN, SIGNED_BYTE_MAX, &value);
  530. value = convert_to_twos(value, 8);
  531. val = cx25821_i2c_read(&dev->i2c_bus[0],
  532. VDEC_A_HUE_CTRL + (0x200 * decoder), &tmp);
  533. val &= 0xFFFFFF00;
  534. ret_val |= cx25821_i2c_write(&dev->i2c_bus[0],
  535. VDEC_A_HUE_CTRL + (0x200 * decoder), val | value);
  536. return ret_val;
  537. }
  538. int medusa_set_saturation(struct cx25821_dev *dev, int saturation, int decoder)
  539. {
  540. int ret_val = 0;
  541. int value = 0;
  542. u32 val = 0, tmp = 0;
  543. if ((saturation > VIDEO_PROCAMP_MAX) ||
  544. (saturation < VIDEO_PROCAMP_MIN)) {
  545. return -1;
  546. }
  547. ret_val = mapM(VIDEO_PROCAMP_MIN, VIDEO_PROCAMP_MAX, saturation,
  548. UNSIGNED_BYTE_MIN, UNSIGNED_BYTE_MAX, &value);
  549. val = cx25821_i2c_read(&dev->i2c_bus[0],
  550. VDEC_A_USAT_CTRL + (0x200 * decoder), &tmp);
  551. val &= 0xFFFFFF00;
  552. ret_val |= cx25821_i2c_write(&dev->i2c_bus[0],
  553. VDEC_A_USAT_CTRL + (0x200 * decoder), val | value);
  554. val = cx25821_i2c_read(&dev->i2c_bus[0],
  555. VDEC_A_VSAT_CTRL + (0x200 * decoder), &tmp);
  556. val &= 0xFFFFFF00;
  557. ret_val |= cx25821_i2c_write(&dev->i2c_bus[0],
  558. VDEC_A_VSAT_CTRL + (0x200 * decoder), val | value);
  559. return ret_val;
  560. }
  561. /* Program the display sequence and monitor output. */
  562. int medusa_video_init(struct cx25821_dev *dev)
  563. {
  564. u32 value = 0, tmp = 0;
  565. int ret_val = 0;
  566. int i = 0;
  567. /* disable Auto source selection on all video decoders */
  568. value = cx25821_i2c_read(&dev->i2c_bus[0], MON_A_CTRL, &tmp);
  569. value &= 0xFFFFF0FF;
  570. ret_val = cx25821_i2c_write(&dev->i2c_bus[0], MON_A_CTRL, value);
  571. if (ret_val < 0)
  572. goto error;
  573. /* Turn off Master source switch enable */
  574. value = cx25821_i2c_read(&dev->i2c_bus[0], MON_A_CTRL, &tmp);
  575. value &= 0xFFFFFFDF;
  576. ret_val = cx25821_i2c_write(&dev->i2c_bus[0], MON_A_CTRL, value);
  577. if (ret_val < 0)
  578. goto error;
  579. /*
  580. * FIXME: due to a coding bug the duration was always 0. It's
  581. * likely that it really should be something else, but due to the
  582. * lack of documentation I have no idea what it should be. For
  583. * now just fill in 0 as the duration.
  584. */
  585. for (i = 0; i < dev->_max_num_decoders; i++)
  586. medusa_set_decoderduration(dev, i, 0);
  587. /* Select monitor as DENC A input, power up the DAC */
  588. value = cx25821_i2c_read(&dev->i2c_bus[0], DENC_AB_CTRL, &tmp);
  589. value &= 0xFF70FF70;
  590. value |= 0x00090008; /* set en_active */
  591. ret_val = cx25821_i2c_write(&dev->i2c_bus[0], DENC_AB_CTRL, value);
  592. if (ret_val < 0)
  593. goto error;
  594. /* enable input is VIP/656 */
  595. value = cx25821_i2c_read(&dev->i2c_bus[0], BYP_AB_CTRL, &tmp);
  596. value |= 0x00040100; /* enable VIP */
  597. ret_val = cx25821_i2c_write(&dev->i2c_bus[0], BYP_AB_CTRL, value);
  598. if (ret_val < 0)
  599. goto error;
  600. /* select AFE clock to output mode */
  601. value = cx25821_i2c_read(&dev->i2c_bus[0], AFE_AB_DIAG_CTRL, &tmp);
  602. value &= 0x83FFFFFF;
  603. ret_val = cx25821_i2c_write(&dev->i2c_bus[0], AFE_AB_DIAG_CTRL,
  604. value | 0x10000000);
  605. if (ret_val < 0)
  606. goto error;
  607. /* Turn on all of the data out and control output pins. */
  608. value = cx25821_i2c_read(&dev->i2c_bus[0], PIN_OE_CTRL, &tmp);
  609. value &= 0xFEF0FE00;
  610. if (dev->_max_num_decoders == MAX_DECODERS) {
  611. /*
  612. * Note: The octal board does not support control pins(bit16-19)
  613. * These bits are ignored in the octal board.
  614. *
  615. * disable VDEC A-C port, default to Mobilygen Interface
  616. */
  617. value |= 0x010001F8;
  618. } else {
  619. /* disable VDEC A-C port, default to Mobilygen Interface */
  620. value |= 0x010F0108;
  621. }
  622. value |= 7;
  623. ret_val = cx25821_i2c_write(&dev->i2c_bus[0], PIN_OE_CTRL, value);
  624. if (ret_val < 0)
  625. goto error;
  626. ret_val = medusa_set_videostandard(dev);
  627. error:
  628. return ret_val;
  629. }