cx18-av-audio.c 13 KB

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