cx25821-medusa-video.c 19 KB

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