cx18-av-audio.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. /*
  2. * cx18 ADEC audio functions
  3. *
  4. * Derived from cx25840-audio.c
  5. *
  6. * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
  7. * Copyright (C) 2008 Andy Walls <awalls@md.metrocast.net>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. */
  19. #include "cx18-driver.h"
  20. static int set_audclk_freq(struct cx18 *cx, u32 freq)
  21. {
  22. struct cx18_av_state *state = &cx->av_state;
  23. if (freq != 32000 && freq != 44100 && freq != 48000)
  24. return -EINVAL;
  25. /*
  26. * The PLL parameters are based on the external crystal frequency that
  27. * would ideally be:
  28. *
  29. * NTSC Color subcarrier freq * 8 =
  30. * 4.5 MHz/286 * 455/2 * 8 = 28.63636363... MHz
  31. *
  32. * The accidents of history and rationale that explain from where this
  33. * combination of magic numbers originate can be found in:
  34. *
  35. * [1] Abrahams, I. C., "Choice of Chrominance Subcarrier Frequency in
  36. * the NTSC Standards", Proceedings of the I-R-E, January 1954, pp 79-80
  37. *
  38. * [2] Abrahams, I. C., "The 'Frequency Interleaving' Principle in the
  39. * NTSC Standards", Proceedings of the I-R-E, January 1954, pp 81-83
  40. *
  41. * As Mike Bradley has rightly pointed out, it's not the exact crystal
  42. * frequency that matters, only that all parts of the driver and
  43. * firmware are using the same value (close to the ideal value).
  44. *
  45. * Since I have a strong suspicion that, if the firmware ever assumes a
  46. * crystal value at all, it will assume 28.636360 MHz, the crystal
  47. * freq used in calculations in this driver will be:
  48. *
  49. * xtal_freq = 28.636360 MHz
  50. *
  51. * an error of less than 0.13 ppm which is way, way better than any off
  52. * the shelf crystal will have for accuracy anyway.
  53. *
  54. * Below I aim to run the PLLs' VCOs near 400 MHz to minimze error.
  55. *
  56. * Many thanks to Jeff Campbell and Mike Bradley for their extensive
  57. * investigation, experimentation, testing, and suggested solutions of
  58. * of audio/video sync problems with SVideo and CVBS captures.
  59. */
  60. if (state->aud_input > CX18_AV_AUDIO_SERIAL2) {
  61. switch (freq) {
  62. case 32000:
  63. /*
  64. * VID_PLL Integer = 0x0f, VID_PLL Post Divider = 0x04
  65. * AUX_PLL Integer = 0x0d, AUX PLL Post Divider = 0x20
  66. */
  67. cx18_av_write4(cx, 0x108, 0x200d040f);
  68. /* VID_PLL Fraction = 0x2be2fe */
  69. /* xtal * 0xf.15f17f0/4 = 108 MHz: 432 MHz pre-postdiv*/
  70. cx18_av_write4(cx, 0x10c, 0x002be2fe);
  71. /* AUX_PLL Fraction = 0x176740c */
  72. /* xtal * 0xd.bb3a060/0x20 = 32000 * 384: 393 MHz p-pd*/
  73. cx18_av_write4(cx, 0x110, 0x0176740c);
  74. /* src3/4/6_ctl */
  75. /* 0x1.f77f = (4 * xtal/8*2/455) / 32000 */
  76. cx18_av_write4(cx, 0x900, 0x0801f77f);
  77. cx18_av_write4(cx, 0x904, 0x0801f77f);
  78. cx18_av_write4(cx, 0x90c, 0x0801f77f);
  79. /* SA_MCLK_SEL=1, SA_MCLK_DIV=0x20 */
  80. cx18_av_write(cx, 0x127, 0x60);
  81. /* AUD_COUNT = 0x2fff = 8 samples * 4 * 384 - 1 */
  82. cx18_av_write4(cx, 0x12c, 0x11202fff);
  83. /*
  84. * EN_AV_LOCK = 0
  85. * VID_COUNT = 0x0d2ef8 = 107999.000 * 8 =
  86. * ((8 samples/32,000) * (13,500,000 * 8) * 4 - 1) * 8
  87. */
  88. cx18_av_write4(cx, 0x128, 0xa00d2ef8);
  89. break;
  90. case 44100:
  91. /*
  92. * VID_PLL Integer = 0x0f, VID_PLL Post Divider = 0x04
  93. * AUX_PLL Integer = 0x0e, AUX PLL Post Divider = 0x18
  94. */
  95. cx18_av_write4(cx, 0x108, 0x180e040f);
  96. /* VID_PLL Fraction = 0x2be2fe */
  97. /* xtal * 0xf.15f17f0/4 = 108 MHz: 432 MHz pre-postdiv*/
  98. cx18_av_write4(cx, 0x10c, 0x002be2fe);
  99. /* AUX_PLL Fraction = 0x062a1f2 */
  100. /* xtal * 0xe.3150f90/0x18 = 44100 * 384: 406 MHz p-pd*/
  101. cx18_av_write4(cx, 0x110, 0x0062a1f2);
  102. /* src3/4/6_ctl */
  103. /* 0x1.6d59 = (4 * xtal/8*2/455) / 44100 */
  104. cx18_av_write4(cx, 0x900, 0x08016d59);
  105. cx18_av_write4(cx, 0x904, 0x08016d59);
  106. cx18_av_write4(cx, 0x90c, 0x08016d59);
  107. /* SA_MCLK_SEL=1, SA_MCLK_DIV=0x18 */
  108. cx18_av_write(cx, 0x127, 0x58);
  109. /* AUD_COUNT = 0x92ff = 49 samples * 2 * 384 - 1 */
  110. cx18_av_write4(cx, 0x12c, 0x112092ff);
  111. /*
  112. * EN_AV_LOCK = 0
  113. * VID_COUNT = 0x1d4bf8 = 239999.000 * 8 =
  114. * ((49 samples/44,100) * (13,500,000 * 8) * 2 - 1) * 8
  115. */
  116. cx18_av_write4(cx, 0x128, 0xa01d4bf8);
  117. break;
  118. case 48000:
  119. /*
  120. * VID_PLL Integer = 0x0f, VID_PLL Post Divider = 0x04
  121. * AUX_PLL Integer = 0x0e, AUX PLL Post Divider = 0x16
  122. */
  123. cx18_av_write4(cx, 0x108, 0x160e040f);
  124. /* VID_PLL Fraction = 0x2be2fe */
  125. /* xtal * 0xf.15f17f0/4 = 108 MHz: 432 MHz pre-postdiv*/
  126. cx18_av_write4(cx, 0x10c, 0x002be2fe);
  127. /* AUX_PLL Fraction = 0x05227ad */
  128. /* xtal * 0xe.2913d68/0x16 = 48000 * 384: 406 MHz p-pd*/
  129. cx18_av_write4(cx, 0x110, 0x005227ad);
  130. /* src3/4/6_ctl */
  131. /* 0x1.4faa = (4 * xtal/8*2/455) / 48000 */
  132. cx18_av_write4(cx, 0x900, 0x08014faa);
  133. cx18_av_write4(cx, 0x904, 0x08014faa);
  134. cx18_av_write4(cx, 0x90c, 0x08014faa);
  135. /* SA_MCLK_SEL=1, SA_MCLK_DIV=0x16 */
  136. cx18_av_write(cx, 0x127, 0x56);
  137. /* AUD_COUNT = 0x5fff = 4 samples * 16 * 384 - 1 */
  138. cx18_av_write4(cx, 0x12c, 0x11205fff);
  139. /*
  140. * EN_AV_LOCK = 0
  141. * VID_COUNT = 0x1193f8 = 143999.000 * 8 =
  142. * ((4 samples/48,000) * (13,500,000 * 8) * 16 - 1) * 8
  143. */
  144. cx18_av_write4(cx, 0x128, 0xa01193f8);
  145. break;
  146. }
  147. } else {
  148. switch (freq) {
  149. case 32000:
  150. /*
  151. * VID_PLL Integer = 0x0f, VID_PLL Post Divider = 0x04
  152. * AUX_PLL Integer = 0x0d, AUX PLL Post Divider = 0x30
  153. */
  154. cx18_av_write4(cx, 0x108, 0x300d040f);
  155. /* VID_PLL Fraction = 0x2be2fe */
  156. /* xtal * 0xf.15f17f0/4 = 108 MHz: 432 MHz pre-postdiv*/
  157. cx18_av_write4(cx, 0x10c, 0x002be2fe);
  158. /* AUX_PLL Fraction = 0x176740c */
  159. /* xtal * 0xd.bb3a060/0x30 = 32000 * 256: 393 MHz p-pd*/
  160. cx18_av_write4(cx, 0x110, 0x0176740c);
  161. /* src1_ctl */
  162. /* 0x1.0000 = 32000/32000 */
  163. cx18_av_write4(cx, 0x8f8, 0x08010000);
  164. /* src3/4/6_ctl */
  165. /* 0x2.0000 = 2 * (32000/32000) */
  166. cx18_av_write4(cx, 0x900, 0x08020000);
  167. cx18_av_write4(cx, 0x904, 0x08020000);
  168. cx18_av_write4(cx, 0x90c, 0x08020000);
  169. /* SA_MCLK_SEL=1, SA_MCLK_DIV=0x30 */
  170. cx18_av_write(cx, 0x127, 0x70);
  171. /* AUD_COUNT = 0x1fff = 8 samples * 4 * 256 - 1 */
  172. cx18_av_write4(cx, 0x12c, 0x11201fff);
  173. /*
  174. * EN_AV_LOCK = 0
  175. * VID_COUNT = 0x0d2ef8 = 107999.000 * 8 =
  176. * ((8 samples/32,000) * (13,500,000 * 8) * 4 - 1) * 8
  177. */
  178. cx18_av_write4(cx, 0x128, 0xa00d2ef8);
  179. break;
  180. case 44100:
  181. /*
  182. * VID_PLL Integer = 0x0f, VID_PLL Post Divider = 0x04
  183. * AUX_PLL Integer = 0x0e, AUX PLL Post Divider = 0x24
  184. */
  185. cx18_av_write4(cx, 0x108, 0x240e040f);
  186. /* VID_PLL Fraction = 0x2be2fe */
  187. /* xtal * 0xf.15f17f0/4 = 108 MHz: 432 MHz pre-postdiv*/
  188. cx18_av_write4(cx, 0x10c, 0x002be2fe);
  189. /* AUX_PLL Fraction = 0x062a1f2 */
  190. /* xtal * 0xe.3150f90/0x24 = 44100 * 256: 406 MHz p-pd*/
  191. cx18_av_write4(cx, 0x110, 0x0062a1f2);
  192. /* src1_ctl */
  193. /* 0x1.60cd = 44100/32000 */
  194. cx18_av_write4(cx, 0x8f8, 0x080160cd);
  195. /* src3/4/6_ctl */
  196. /* 0x1.7385 = 2 * (32000/44100) */
  197. cx18_av_write4(cx, 0x900, 0x08017385);
  198. cx18_av_write4(cx, 0x904, 0x08017385);
  199. cx18_av_write4(cx, 0x90c, 0x08017385);
  200. /* SA_MCLK_SEL=1, SA_MCLK_DIV=0x24 */
  201. cx18_av_write(cx, 0x127, 0x64);
  202. /* AUD_COUNT = 0x61ff = 49 samples * 2 * 256 - 1 */
  203. cx18_av_write4(cx, 0x12c, 0x112061ff);
  204. /*
  205. * EN_AV_LOCK = 0
  206. * VID_COUNT = 0x1d4bf8 = 239999.000 * 8 =
  207. * ((49 samples/44,100) * (13,500,000 * 8) * 2 - 1) * 8
  208. */
  209. cx18_av_write4(cx, 0x128, 0xa01d4bf8);
  210. break;
  211. case 48000:
  212. /*
  213. * VID_PLL Integer = 0x0f, VID_PLL Post Divider = 0x04
  214. * AUX_PLL Integer = 0x0d, AUX PLL Post Divider = 0x20
  215. */
  216. cx18_av_write4(cx, 0x108, 0x200d040f);
  217. /* VID_PLL Fraction = 0x2be2fe */
  218. /* xtal * 0xf.15f17f0/4 = 108 MHz: 432 MHz pre-postdiv*/
  219. cx18_av_write4(cx, 0x10c, 0x002be2fe);
  220. /* AUX_PLL Fraction = 0x176740c */
  221. /* xtal * 0xd.bb3a060/0x20 = 48000 * 256: 393 MHz p-pd*/
  222. cx18_av_write4(cx, 0x110, 0x0176740c);
  223. /* src1_ctl */
  224. /* 0x1.8000 = 48000/32000 */
  225. cx18_av_write4(cx, 0x8f8, 0x08018000);
  226. /* src3/4/6_ctl */
  227. /* 0x1.5555 = 2 * (32000/48000) */
  228. cx18_av_write4(cx, 0x900, 0x08015555);
  229. cx18_av_write4(cx, 0x904, 0x08015555);
  230. cx18_av_write4(cx, 0x90c, 0x08015555);
  231. /* SA_MCLK_SEL=1, SA_MCLK_DIV=0x20 */
  232. cx18_av_write(cx, 0x127, 0x60);
  233. /* AUD_COUNT = 0x3fff = 4 samples * 16 * 256 - 1 */
  234. cx18_av_write4(cx, 0x12c, 0x11203fff);
  235. /*
  236. * EN_AV_LOCK = 0
  237. * VID_COUNT = 0x1193f8 = 143999.000 * 8 =
  238. * ((4 samples/48,000) * (13,500,000 * 8) * 16 - 1) * 8
  239. */
  240. cx18_av_write4(cx, 0x128, 0xa01193f8);
  241. break;
  242. }
  243. }
  244. state->audclk_freq = freq;
  245. return 0;
  246. }
  247. void cx18_av_audio_set_path(struct cx18 *cx)
  248. {
  249. struct cx18_av_state *state = &cx->av_state;
  250. u8 v;
  251. /* stop microcontroller */
  252. v = cx18_av_read(cx, 0x803) & ~0x10;
  253. cx18_av_write_expect(cx, 0x803, v, v, 0x1f);
  254. /* assert soft reset */
  255. v = cx18_av_read(cx, 0x810) | 0x01;
  256. cx18_av_write_expect(cx, 0x810, v, v, 0x0f);
  257. /* Mute everything to prevent the PFFT! */
  258. cx18_av_write(cx, 0x8d3, 0x1f);
  259. if (state->aud_input <= CX18_AV_AUDIO_SERIAL2) {
  260. /* Set Path1 to Serial Audio Input */
  261. cx18_av_write4(cx, 0x8d0, 0x01011012);
  262. /* The microcontroller should not be started for the
  263. * non-tuner inputs: autodetection is specific for
  264. * TV audio. */
  265. } else {
  266. /* Set Path1 to Analog Demod Main Channel */
  267. cx18_av_write4(cx, 0x8d0, 0x1f063870);
  268. }
  269. set_audclk_freq(cx, state->audclk_freq);
  270. /* deassert soft reset */
  271. v = cx18_av_read(cx, 0x810) & ~0x01;
  272. cx18_av_write_expect(cx, 0x810, v, v, 0x0f);
  273. if (state->aud_input > CX18_AV_AUDIO_SERIAL2) {
  274. /* When the microcontroller detects the
  275. * audio format, it will unmute the lines */
  276. v = cx18_av_read(cx, 0x803) | 0x10;
  277. cx18_av_write_expect(cx, 0x803, v, v, 0x1f);
  278. }
  279. }
  280. static void set_volume(struct cx18 *cx, int volume)
  281. {
  282. /* First convert the volume to msp3400 values (0-127) */
  283. int vol = volume >> 9;
  284. /* now scale it up to cx18_av values
  285. * -114dB to -96dB maps to 0
  286. * this should be 19, but in my testing that was 4dB too loud */
  287. if (vol <= 23)
  288. vol = 0;
  289. else
  290. vol -= 23;
  291. /* PATH1_VOLUME */
  292. cx18_av_write(cx, 0x8d4, 228 - (vol * 2));
  293. }
  294. static void set_bass(struct cx18 *cx, int bass)
  295. {
  296. /* PATH1_EQ_BASS_VOL */
  297. cx18_av_and_or(cx, 0x8d9, ~0x3f, 48 - (bass * 48 / 0xffff));
  298. }
  299. static void set_treble(struct cx18 *cx, int treble)
  300. {
  301. /* PATH1_EQ_TREBLE_VOL */
  302. cx18_av_and_or(cx, 0x8db, ~0x3f, 48 - (treble * 48 / 0xffff));
  303. }
  304. static void set_balance(struct cx18 *cx, int balance)
  305. {
  306. int bal = balance >> 8;
  307. if (bal > 0x80) {
  308. /* PATH1_BAL_LEFT */
  309. cx18_av_and_or(cx, 0x8d5, 0x7f, 0x80);
  310. /* PATH1_BAL_LEVEL */
  311. cx18_av_and_or(cx, 0x8d5, ~0x7f, bal & 0x7f);
  312. } else {
  313. /* PATH1_BAL_LEFT */
  314. cx18_av_and_or(cx, 0x8d5, 0x7f, 0x00);
  315. /* PATH1_BAL_LEVEL */
  316. cx18_av_and_or(cx, 0x8d5, ~0x7f, 0x80 - bal);
  317. }
  318. }
  319. static void set_mute(struct cx18 *cx, int mute)
  320. {
  321. struct cx18_av_state *state = &cx->av_state;
  322. u8 v;
  323. if (state->aud_input > CX18_AV_AUDIO_SERIAL2) {
  324. /* Must turn off microcontroller in order to mute sound.
  325. * Not sure if this is the best method, but it does work.
  326. * If the microcontroller is running, then it will undo any
  327. * changes to the mute register. */
  328. v = cx18_av_read(cx, 0x803);
  329. if (mute) {
  330. /* disable microcontroller */
  331. v &= ~0x10;
  332. cx18_av_write_expect(cx, 0x803, v, v, 0x1f);
  333. cx18_av_write(cx, 0x8d3, 0x1f);
  334. } else {
  335. /* enable microcontroller */
  336. v |= 0x10;
  337. cx18_av_write_expect(cx, 0x803, v, v, 0x1f);
  338. }
  339. } else {
  340. /* SRC1_MUTE_EN */
  341. cx18_av_and_or(cx, 0x8d3, ~0x2, mute ? 0x02 : 0x00);
  342. }
  343. }
  344. int cx18_av_s_clock_freq(struct v4l2_subdev *sd, u32 freq)
  345. {
  346. struct cx18 *cx = v4l2_get_subdevdata(sd);
  347. struct cx18_av_state *state = &cx->av_state;
  348. int retval;
  349. u8 v;
  350. if (state->aud_input > CX18_AV_AUDIO_SERIAL2) {
  351. v = cx18_av_read(cx, 0x803) & ~0x10;
  352. cx18_av_write_expect(cx, 0x803, v, v, 0x1f);
  353. cx18_av_write(cx, 0x8d3, 0x1f);
  354. }
  355. v = cx18_av_read(cx, 0x810) | 0x1;
  356. cx18_av_write_expect(cx, 0x810, v, v, 0x0f);
  357. retval = set_audclk_freq(cx, freq);
  358. v = cx18_av_read(cx, 0x810) & ~0x1;
  359. cx18_av_write_expect(cx, 0x810, v, v, 0x0f);
  360. if (state->aud_input > CX18_AV_AUDIO_SERIAL2) {
  361. v = cx18_av_read(cx, 0x803) | 0x10;
  362. cx18_av_write_expect(cx, 0x803, v, v, 0x1f);
  363. }
  364. return retval;
  365. }
  366. static int cx18_av_audio_s_ctrl(struct v4l2_ctrl *ctrl)
  367. {
  368. struct v4l2_subdev *sd = to_sd(ctrl);
  369. struct cx18 *cx = v4l2_get_subdevdata(sd);
  370. switch (ctrl->id) {
  371. case V4L2_CID_AUDIO_VOLUME:
  372. set_volume(cx, ctrl->val);
  373. break;
  374. case V4L2_CID_AUDIO_BASS:
  375. set_bass(cx, ctrl->val);
  376. break;
  377. case V4L2_CID_AUDIO_TREBLE:
  378. set_treble(cx, ctrl->val);
  379. break;
  380. case V4L2_CID_AUDIO_BALANCE:
  381. set_balance(cx, ctrl->val);
  382. break;
  383. case V4L2_CID_AUDIO_MUTE:
  384. set_mute(cx, ctrl->val);
  385. break;
  386. default:
  387. return -EINVAL;
  388. }
  389. return 0;
  390. }
  391. const struct v4l2_ctrl_ops cx18_av_audio_ctrl_ops = {
  392. .s_ctrl = cx18_av_audio_s_ctrl,
  393. };