cx25840-audio.c 14 KB

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