cx25840-audio.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. /* cx25840 audio functions
  2. *
  3. * This program is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU General Public License
  5. * as published by the Free Software Foundation; either version 2
  6. * of the License, or (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/videodev2.h>
  14. #include <linux/i2c.h>
  15. #include <media/v4l2-common.h>
  16. #include <media/drv-intf/cx25840.h>
  17. #include "cx25840-core.h"
  18. /*
  19. * Note: The PLL and SRC parameters are based on a reference frequency that
  20. * would ideally be:
  21. *
  22. * NTSC Color subcarrier freq * 8 = 4.5 MHz/286 * 455/2 * 8 = 28.63636363... MHz
  23. *
  24. * However, it's not the exact reference frequency that matters, only that the
  25. * firmware and modules that comprise the driver for a particular board all
  26. * use the same value (close to the ideal value).
  27. *
  28. * Comments below will note which reference frequency is assumed for various
  29. * parameters. They will usually be one of
  30. *
  31. * ref_freq = 28.636360 MHz
  32. * or
  33. * ref_freq = 28.636363 MHz
  34. */
  35. static int cx25840_set_audclk_freq(struct i2c_client *client, u32 freq)
  36. {
  37. struct cx25840_state *state = to_state(i2c_get_clientdata(client));
  38. if (state->aud_input != CX25840_AUDIO_SERIAL) {
  39. switch (freq) {
  40. case 32000:
  41. /*
  42. * VID_PLL Integer = 0x0f, VID_PLL Post Divider = 0x04
  43. * AUX_PLL Integer = 0x06, AUX PLL Post Divider = 0x10
  44. */
  45. cx25840_write4(client, 0x108, 0x1006040f);
  46. /*
  47. * VID_PLL Fraction (register 0x10c) = 0x2be2fe
  48. * 28636360 * 0xf.15f17f0/4 = 108 MHz
  49. * 432 MHz pre-postdivide
  50. */
  51. /*
  52. * AUX_PLL Fraction = 0x1bb39ee
  53. * 28636363 * 0x6.dd9cf70/0x10 = 32000 * 384
  54. * 196.6 MHz pre-postdivide
  55. * FIXME < 200 MHz is out of specified valid range
  56. * FIXME 28636363 ref_freq doesn't match VID PLL ref
  57. */
  58. cx25840_write4(client, 0x110, 0x01bb39ee);
  59. /*
  60. * SA_MCLK_SEL = 1
  61. * SA_MCLK_DIV = 0x10 = 384/384 * AUX_PLL post dvivider
  62. */
  63. cx25840_write(client, 0x127, 0x50);
  64. if (is_cx2583x(state))
  65. break;
  66. /* src3/4/6_ctl */
  67. /* 0x1.f77f = (4 * 28636360/8 * 2/455) / 32000 */
  68. cx25840_write4(client, 0x900, 0x0801f77f);
  69. cx25840_write4(client, 0x904, 0x0801f77f);
  70. cx25840_write4(client, 0x90c, 0x0801f77f);
  71. break;
  72. case 44100:
  73. /*
  74. * VID_PLL Integer = 0x0f, VID_PLL Post Divider = 0x04
  75. * AUX_PLL Integer = 0x09, AUX PLL Post Divider = 0x10
  76. */
  77. cx25840_write4(client, 0x108, 0x1009040f);
  78. /*
  79. * VID_PLL Fraction (register 0x10c) = 0x2be2fe
  80. * 28636360 * 0xf.15f17f0/4 = 108 MHz
  81. * 432 MHz pre-postdivide
  82. */
  83. /*
  84. * AUX_PLL Fraction = 0x0ec6bd6
  85. * 28636363 * 0x9.7635eb0/0x10 = 44100 * 384
  86. * 271 MHz pre-postdivide
  87. * FIXME 28636363 ref_freq doesn't match VID PLL ref
  88. */
  89. cx25840_write4(client, 0x110, 0x00ec6bd6);
  90. /*
  91. * SA_MCLK_SEL = 1
  92. * SA_MCLK_DIV = 0x10 = 384/384 * AUX_PLL post dvivider
  93. */
  94. cx25840_write(client, 0x127, 0x50);
  95. if (is_cx2583x(state))
  96. break;
  97. /* src3/4/6_ctl */
  98. /* 0x1.6d59 = (4 * 28636360/8 * 2/455) / 44100 */
  99. cx25840_write4(client, 0x900, 0x08016d59);
  100. cx25840_write4(client, 0x904, 0x08016d59);
  101. cx25840_write4(client, 0x90c, 0x08016d59);
  102. break;
  103. case 48000:
  104. /*
  105. * VID_PLL Integer = 0x0f, VID_PLL Post Divider = 0x04
  106. * AUX_PLL Integer = 0x0a, AUX PLL Post Divider = 0x10
  107. */
  108. cx25840_write4(client, 0x108, 0x100a040f);
  109. /*
  110. * VID_PLL Fraction (register 0x10c) = 0x2be2fe
  111. * 28636360 * 0xf.15f17f0/4 = 108 MHz
  112. * 432 MHz pre-postdivide
  113. */
  114. /*
  115. * AUX_PLL Fraction = 0x098d6e5
  116. * 28636363 * 0xa.4c6b728/0x10 = 48000 * 384
  117. * 295 MHz pre-postdivide
  118. * FIXME 28636363 ref_freq doesn't match VID PLL ref
  119. */
  120. cx25840_write4(client, 0x110, 0x0098d6e5);
  121. /*
  122. * SA_MCLK_SEL = 1
  123. * SA_MCLK_DIV = 0x10 = 384/384 * AUX_PLL post dvivider
  124. */
  125. cx25840_write(client, 0x127, 0x50);
  126. if (is_cx2583x(state))
  127. break;
  128. /* src3/4/6_ctl */
  129. /* 0x1.4faa = (4 * 28636360/8 * 2/455) / 48000 */
  130. cx25840_write4(client, 0x900, 0x08014faa);
  131. cx25840_write4(client, 0x904, 0x08014faa);
  132. cx25840_write4(client, 0x90c, 0x08014faa);
  133. break;
  134. }
  135. } else {
  136. switch (freq) {
  137. case 32000:
  138. /*
  139. * VID_PLL Integer = 0x0f, VID_PLL Post Divider = 0x04
  140. * AUX_PLL Integer = 0x08, AUX PLL Post Divider = 0x1e
  141. */
  142. cx25840_write4(client, 0x108, 0x1e08040f);
  143. /*
  144. * VID_PLL Fraction (register 0x10c) = 0x2be2fe
  145. * 28636360 * 0xf.15f17f0/4 = 108 MHz
  146. * 432 MHz pre-postdivide
  147. */
  148. /*
  149. * AUX_PLL Fraction = 0x12a0869
  150. * 28636363 * 0x8.9504348/0x1e = 32000 * 256
  151. * 246 MHz pre-postdivide
  152. * FIXME 28636363 ref_freq doesn't match VID PLL ref
  153. */
  154. cx25840_write4(client, 0x110, 0x012a0869);
  155. /*
  156. * SA_MCLK_SEL = 1
  157. * SA_MCLK_DIV = 0x14 = 256/384 * AUX_PLL post dvivider
  158. */
  159. cx25840_write(client, 0x127, 0x54);
  160. if (is_cx2583x(state))
  161. break;
  162. /* src1_ctl */
  163. /* 0x1.0000 = 32000/32000 */
  164. cx25840_write4(client, 0x8f8, 0x08010000);
  165. /* src3/4/6_ctl */
  166. /* 0x2.0000 = 2 * (32000/32000) */
  167. cx25840_write4(client, 0x900, 0x08020000);
  168. cx25840_write4(client, 0x904, 0x08020000);
  169. cx25840_write4(client, 0x90c, 0x08020000);
  170. break;
  171. case 44100:
  172. /*
  173. * VID_PLL Integer = 0x0f, VID_PLL Post Divider = 0x04
  174. * AUX_PLL Integer = 0x09, AUX PLL Post Divider = 0x18
  175. */
  176. cx25840_write4(client, 0x108, 0x1809040f);
  177. /*
  178. * VID_PLL Fraction (register 0x10c) = 0x2be2fe
  179. * 28636360 * 0xf.15f17f0/4 = 108 MHz
  180. * 432 MHz pre-postdivide
  181. */
  182. /*
  183. * AUX_PLL Fraction = 0x0ec6bd6
  184. * 28636363 * 0x9.7635eb0/0x18 = 44100 * 256
  185. * 271 MHz pre-postdivide
  186. * FIXME 28636363 ref_freq doesn't match VID PLL ref
  187. */
  188. cx25840_write4(client, 0x110, 0x00ec6bd6);
  189. /*
  190. * SA_MCLK_SEL = 1
  191. * SA_MCLK_DIV = 0x10 = 256/384 * AUX_PLL post dvivider
  192. */
  193. cx25840_write(client, 0x127, 0x50);
  194. if (is_cx2583x(state))
  195. break;
  196. /* src1_ctl */
  197. /* 0x1.60cd = 44100/32000 */
  198. cx25840_write4(client, 0x8f8, 0x080160cd);
  199. /* src3/4/6_ctl */
  200. /* 0x1.7385 = 2 * (32000/44100) */
  201. cx25840_write4(client, 0x900, 0x08017385);
  202. cx25840_write4(client, 0x904, 0x08017385);
  203. cx25840_write4(client, 0x90c, 0x08017385);
  204. break;
  205. case 48000:
  206. /*
  207. * VID_PLL Integer = 0x0f, VID_PLL Post Divider = 0x04
  208. * AUX_PLL Integer = 0x0a, AUX PLL Post Divider = 0x18
  209. */
  210. cx25840_write4(client, 0x108, 0x180a040f);
  211. /*
  212. * VID_PLL Fraction (register 0x10c) = 0x2be2fe
  213. * 28636360 * 0xf.15f17f0/4 = 108 MHz
  214. * 432 MHz pre-postdivide
  215. */
  216. /*
  217. * AUX_PLL Fraction = 0x098d6e5
  218. * 28636363 * 0xa.4c6b728/0x18 = 48000 * 256
  219. * 295 MHz pre-postdivide
  220. * FIXME 28636363 ref_freq doesn't match VID PLL ref
  221. */
  222. cx25840_write4(client, 0x110, 0x0098d6e5);
  223. /*
  224. * SA_MCLK_SEL = 1
  225. * SA_MCLK_DIV = 0x10 = 256/384 * AUX_PLL post dvivider
  226. */
  227. cx25840_write(client, 0x127, 0x50);
  228. if (is_cx2583x(state))
  229. break;
  230. /* src1_ctl */
  231. /* 0x1.8000 = 48000/32000 */
  232. cx25840_write4(client, 0x8f8, 0x08018000);
  233. /* src3/4/6_ctl */
  234. /* 0x1.5555 = 2 * (32000/48000) */
  235. cx25840_write4(client, 0x900, 0x08015555);
  236. cx25840_write4(client, 0x904, 0x08015555);
  237. cx25840_write4(client, 0x90c, 0x08015555);
  238. break;
  239. }
  240. }
  241. state->audclk_freq = freq;
  242. return 0;
  243. }
  244. static inline int cx25836_set_audclk_freq(struct i2c_client *client, u32 freq)
  245. {
  246. return cx25840_set_audclk_freq(client, freq);
  247. }
  248. static int cx23885_set_audclk_freq(struct i2c_client *client, u32 freq)
  249. {
  250. struct cx25840_state *state = to_state(i2c_get_clientdata(client));
  251. if (state->aud_input != CX25840_AUDIO_SERIAL) {
  252. switch (freq) {
  253. case 32000:
  254. case 44100:
  255. case 48000:
  256. /* We don't have register values
  257. * so avoid destroying registers. */
  258. /* FIXME return -EINVAL; */
  259. break;
  260. }
  261. } else {
  262. switch (freq) {
  263. case 32000:
  264. case 44100:
  265. /* We don't have register values
  266. * so avoid destroying registers. */
  267. /* FIXME return -EINVAL; */
  268. break;
  269. case 48000:
  270. /* src1_ctl */
  271. /* 0x1.867c = 48000 / (2 * 28636360/8 * 2/455) */
  272. cx25840_write4(client, 0x8f8, 0x0801867c);
  273. /* src3/4/6_ctl */
  274. /* 0x1.4faa = (4 * 28636360/8 * 2/455) / 48000 */
  275. cx25840_write4(client, 0x900, 0x08014faa);
  276. cx25840_write4(client, 0x904, 0x08014faa);
  277. cx25840_write4(client, 0x90c, 0x08014faa);
  278. break;
  279. }
  280. }
  281. state->audclk_freq = freq;
  282. return 0;
  283. }
  284. static int cx231xx_set_audclk_freq(struct i2c_client *client, u32 freq)
  285. {
  286. struct cx25840_state *state = to_state(i2c_get_clientdata(client));
  287. if (state->aud_input != CX25840_AUDIO_SERIAL) {
  288. switch (freq) {
  289. case 32000:
  290. /* src3/4/6_ctl */
  291. /* 0x1.f77f = (4 * 28636360/8 * 2/455) / 32000 */
  292. cx25840_write4(client, 0x900, 0x0801f77f);
  293. cx25840_write4(client, 0x904, 0x0801f77f);
  294. cx25840_write4(client, 0x90c, 0x0801f77f);
  295. break;
  296. case 44100:
  297. /* src3/4/6_ctl */
  298. /* 0x1.6d59 = (4 * 28636360/8 * 2/455) / 44100 */
  299. cx25840_write4(client, 0x900, 0x08016d59);
  300. cx25840_write4(client, 0x904, 0x08016d59);
  301. cx25840_write4(client, 0x90c, 0x08016d59);
  302. break;
  303. case 48000:
  304. /* src3/4/6_ctl */
  305. /* 0x1.4faa = (4 * 28636360/8 * 2/455) / 48000 */
  306. cx25840_write4(client, 0x900, 0x08014faa);
  307. cx25840_write4(client, 0x904, 0x08014faa);
  308. cx25840_write4(client, 0x90c, 0x08014faa);
  309. break;
  310. }
  311. } else {
  312. switch (freq) {
  313. /* FIXME These cases make different assumptions about audclk */
  314. case 32000:
  315. /* src1_ctl */
  316. /* 0x1.0000 = 32000/32000 */
  317. cx25840_write4(client, 0x8f8, 0x08010000);
  318. /* src3/4/6_ctl */
  319. /* 0x2.0000 = 2 * (32000/32000) */
  320. cx25840_write4(client, 0x900, 0x08020000);
  321. cx25840_write4(client, 0x904, 0x08020000);
  322. cx25840_write4(client, 0x90c, 0x08020000);
  323. break;
  324. case 44100:
  325. /* src1_ctl */
  326. /* 0x1.60cd = 44100/32000 */
  327. cx25840_write4(client, 0x8f8, 0x080160cd);
  328. /* src3/4/6_ctl */
  329. /* 0x1.7385 = 2 * (32000/44100) */
  330. cx25840_write4(client, 0x900, 0x08017385);
  331. cx25840_write4(client, 0x904, 0x08017385);
  332. cx25840_write4(client, 0x90c, 0x08017385);
  333. break;
  334. case 48000:
  335. /* src1_ctl */
  336. /* 0x1.867c = 48000 / (2 * 28636360/8 * 2/455) */
  337. cx25840_write4(client, 0x8f8, 0x0801867c);
  338. /* src3/4/6_ctl */
  339. /* 0x1.4faa = (4 * 28636360/8 * 2/455) / 48000 */
  340. cx25840_write4(client, 0x900, 0x08014faa);
  341. cx25840_write4(client, 0x904, 0x08014faa);
  342. cx25840_write4(client, 0x90c, 0x08014faa);
  343. break;
  344. }
  345. }
  346. state->audclk_freq = freq;
  347. return 0;
  348. }
  349. static int set_audclk_freq(struct i2c_client *client, u32 freq)
  350. {
  351. struct cx25840_state *state = to_state(i2c_get_clientdata(client));
  352. if (freq != 32000 && freq != 44100 && freq != 48000)
  353. return -EINVAL;
  354. if (is_cx231xx(state))
  355. return cx231xx_set_audclk_freq(client, freq);
  356. if (is_cx2388x(state))
  357. return cx23885_set_audclk_freq(client, freq);
  358. if (is_cx2583x(state))
  359. return cx25836_set_audclk_freq(client, freq);
  360. return cx25840_set_audclk_freq(client, freq);
  361. }
  362. void cx25840_audio_set_path(struct i2c_client *client)
  363. {
  364. struct cx25840_state *state = to_state(i2c_get_clientdata(client));
  365. if (!is_cx2583x(state)) {
  366. /* assert soft reset */
  367. cx25840_and_or(client, 0x810, ~0x1, 0x01);
  368. /* stop microcontroller */
  369. cx25840_and_or(client, 0x803, ~0x10, 0);
  370. /* Mute everything to prevent the PFFT! */
  371. cx25840_write(client, 0x8d3, 0x1f);
  372. if (state->aud_input == CX25840_AUDIO_SERIAL) {
  373. /* Set Path1 to Serial Audio Input */
  374. cx25840_write4(client, 0x8d0, 0x01011012);
  375. /* The microcontroller should not be started for the
  376. * non-tuner inputs: autodetection is specific for
  377. * TV audio. */
  378. } else {
  379. /* Set Path1 to Analog Demod Main Channel */
  380. cx25840_write4(client, 0x8d0, 0x1f063870);
  381. }
  382. }
  383. set_audclk_freq(client, state->audclk_freq);
  384. if (!is_cx2583x(state)) {
  385. if (state->aud_input != CX25840_AUDIO_SERIAL) {
  386. /* When the microcontroller detects the
  387. * audio format, it will unmute the lines */
  388. cx25840_and_or(client, 0x803, ~0x10, 0x10);
  389. }
  390. /* deassert soft reset */
  391. cx25840_and_or(client, 0x810, ~0x1, 0x00);
  392. /* Ensure the controller is running when we exit */
  393. if (is_cx2388x(state) || is_cx231xx(state))
  394. cx25840_and_or(client, 0x803, ~0x10, 0x10);
  395. }
  396. }
  397. static void set_volume(struct i2c_client *client, int volume)
  398. {
  399. int vol;
  400. /* Convert the volume to msp3400 values (0-127) */
  401. vol = volume >> 9;
  402. /* now scale it up to cx25840 values
  403. * -114dB to -96dB maps to 0
  404. * this should be 19, but in my testing that was 4dB too loud */
  405. if (vol <= 23) {
  406. vol = 0;
  407. } else {
  408. vol -= 23;
  409. }
  410. /* PATH1_VOLUME */
  411. cx25840_write(client, 0x8d4, 228 - (vol * 2));
  412. }
  413. static void set_balance(struct i2c_client *client, int balance)
  414. {
  415. int bal = balance >> 8;
  416. if (bal > 0x80) {
  417. /* PATH1_BAL_LEFT */
  418. cx25840_and_or(client, 0x8d5, 0x7f, 0x80);
  419. /* PATH1_BAL_LEVEL */
  420. cx25840_and_or(client, 0x8d5, ~0x7f, bal & 0x7f);
  421. } else {
  422. /* PATH1_BAL_LEFT */
  423. cx25840_and_or(client, 0x8d5, 0x7f, 0x00);
  424. /* PATH1_BAL_LEVEL */
  425. cx25840_and_or(client, 0x8d5, ~0x7f, 0x80 - bal);
  426. }
  427. }
  428. int cx25840_s_clock_freq(struct v4l2_subdev *sd, u32 freq)
  429. {
  430. struct i2c_client *client = v4l2_get_subdevdata(sd);
  431. struct cx25840_state *state = to_state(sd);
  432. int retval;
  433. if (!is_cx2583x(state))
  434. cx25840_and_or(client, 0x810, ~0x1, 1);
  435. if (state->aud_input != CX25840_AUDIO_SERIAL) {
  436. cx25840_and_or(client, 0x803, ~0x10, 0);
  437. cx25840_write(client, 0x8d3, 0x1f);
  438. }
  439. retval = set_audclk_freq(client, freq);
  440. if (state->aud_input != CX25840_AUDIO_SERIAL)
  441. cx25840_and_or(client, 0x803, ~0x10, 0x10);
  442. if (!is_cx2583x(state))
  443. cx25840_and_or(client, 0x810, ~0x1, 0);
  444. return retval;
  445. }
  446. static int cx25840_audio_s_ctrl(struct v4l2_ctrl *ctrl)
  447. {
  448. struct v4l2_subdev *sd = to_sd(ctrl);
  449. struct cx25840_state *state = to_state(sd);
  450. struct i2c_client *client = v4l2_get_subdevdata(sd);
  451. switch (ctrl->id) {
  452. case V4L2_CID_AUDIO_VOLUME:
  453. if (state->mute->val)
  454. set_volume(client, 0);
  455. else
  456. set_volume(client, state->volume->val);
  457. break;
  458. case V4L2_CID_AUDIO_BASS:
  459. /* PATH1_EQ_BASS_VOL */
  460. cx25840_and_or(client, 0x8d9, ~0x3f,
  461. 48 - (ctrl->val * 48 / 0xffff));
  462. break;
  463. case V4L2_CID_AUDIO_TREBLE:
  464. /* PATH1_EQ_TREBLE_VOL */
  465. cx25840_and_or(client, 0x8db, ~0x3f,
  466. 48 - (ctrl->val * 48 / 0xffff));
  467. break;
  468. case V4L2_CID_AUDIO_BALANCE:
  469. set_balance(client, ctrl->val);
  470. break;
  471. default:
  472. return -EINVAL;
  473. }
  474. return 0;
  475. }
  476. const struct v4l2_ctrl_ops cx25840_audio_ctrl_ops = {
  477. .s_ctrl = cx25840_audio_s_ctrl,
  478. };