cx25840-audio.c 15 KB

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