cx18-av-audio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /*
  2. * cx18 ADEC audio functions
  3. *
  4. * Derived from cx25840-audio.c
  5. *
  6. * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  21. * 02110-1301, USA.
  22. */
  23. #include "cx18-driver.h"
  24. static int set_audclk_freq(struct cx18 *cx, u32 freq)
  25. {
  26. struct cx18_av_state *state = &cx->av_state;
  27. if (freq != 32000 && freq != 44100 && freq != 48000)
  28. return -EINVAL;
  29. /* SA_MCLK_SEL=1, SA_MCLK_DIV=0x10 */
  30. cx18_av_write(cx, 0x127, 0x50);
  31. if (state->aud_input > CX18_AV_AUDIO_SERIAL2) {
  32. switch (freq) {
  33. case 32000:
  34. /* VID_PLL and AUX_PLL */
  35. cx18_av_write4(cx, 0x108, 0x1408040f);
  36. /* AUX_PLL_FRAC */
  37. /* 0x8.9504318a * 28,636,363.636 / 0x14 = 32000 * 384 */
  38. cx18_av_write4(cx, 0x110, 0x012a0863);
  39. /* src3/4/6_ctl */
  40. /* 0x1.f77f = (4 * 15734.26) / 32000 */
  41. cx18_av_write4(cx, 0x900, 0x0801f77f);
  42. cx18_av_write4(cx, 0x904, 0x0801f77f);
  43. cx18_av_write4(cx, 0x90c, 0x0801f77f);
  44. /* SA_MCLK_SEL=1, SA_MCLK_DIV=0x14 */
  45. cx18_av_write(cx, 0x127, 0x54);
  46. /* AUD_COUNT = 0x2fff = 8 samples * 4 * 384 - 1 */
  47. cx18_av_write4(cx, 0x12c, 0x11202fff);
  48. /*
  49. * EN_AV_LOCK = 1
  50. * VID_COUNT = 0x0d2ef8 = 107999.000 * 8 =
  51. * ((8 samples/32,000) * (13,500,000 * 8) * 4 - 1) * 8
  52. */
  53. cx18_av_write4(cx, 0x128, 0xa10d2ef8);
  54. break;
  55. case 44100:
  56. /* VID_PLL and AUX_PLL */
  57. cx18_av_write4(cx, 0x108, 0x1009040f);
  58. /* AUX_PLL_FRAC */
  59. /* 0x9.7635e7 * 28,636,363.63 / 0x10 = 44100 * 384 */
  60. cx18_av_write4(cx, 0x110, 0x00ec6bce);
  61. /* src3/4/6_ctl */
  62. /* 0x1.6d59 = (4 * 15734.26) / 44100 */
  63. cx18_av_write4(cx, 0x900, 0x08016d59);
  64. cx18_av_write4(cx, 0x904, 0x08016d59);
  65. cx18_av_write4(cx, 0x90c, 0x08016d59);
  66. /* AUD_COUNT = 0x92ff = 49 samples * 2 * 384 - 1 */
  67. cx18_av_write4(cx, 0x12c, 0x112092ff);
  68. /*
  69. * EN_AV_LOCK = 1
  70. * VID_COUNT = 0x1d4bf8 = 239999.000 * 8 =
  71. * ((49 samples/44,100) * (13,500,000 * 8) * 2 - 1) * 8
  72. */
  73. cx18_av_write4(cx, 0x128, 0xa11d4bf8);
  74. break;
  75. case 48000:
  76. /* VID_PLL and AUX_PLL */
  77. cx18_av_write4(cx, 0x108, 0x100a040f);
  78. /* AUX_PLL_FRAC */
  79. /* 0xa.4c6b6ea * 28,636,363.63 / 0x10 = 48000 * 384 */
  80. cx18_av_write4(cx, 0x110, 0x0098d6dd);
  81. /* src3/4/6_ctl */
  82. /* 0x1.4faa = (4 * 15734.26) / 48000 */
  83. cx18_av_write4(cx, 0x900, 0x08014faa);
  84. cx18_av_write4(cx, 0x904, 0x08014faa);
  85. cx18_av_write4(cx, 0x90c, 0x08014faa);
  86. /* AUD_COUNT = 0x5fff = 4 samples * 16 * 384 - 1 */
  87. cx18_av_write4(cx, 0x12c, 0x11205fff);
  88. /*
  89. * EN_AV_LOCK = 1
  90. * VID_COUNT = 0x1193f8 = 143999.000 * 8 =
  91. * ((4 samples/48,000) * (13,500,000 * 8) * 16 - 1) * 8
  92. */
  93. cx18_av_write4(cx, 0x128, 0xa11193f8);
  94. break;
  95. }
  96. } else {
  97. switch (freq) {
  98. case 32000:
  99. /* VID_PLL and AUX_PLL */
  100. cx18_av_write4(cx, 0x108, 0x1e08040f);
  101. /* AUX_PLL_FRAC */
  102. /* 0x8.9504318 * 28,636,363.63 / 0x1e = 32000 * 256 */
  103. cx18_av_write4(cx, 0x110, 0x012a0863);
  104. /* src1_ctl */
  105. /* 0x1.0000 = 32000/32000 */
  106. cx18_av_write4(cx, 0x8f8, 0x08010000);
  107. /* src3/4/6_ctl */
  108. /* 0x2.0000 = 2 * (32000/32000) */
  109. cx18_av_write4(cx, 0x900, 0x08020000);
  110. cx18_av_write4(cx, 0x904, 0x08020000);
  111. cx18_av_write4(cx, 0x90c, 0x08020000);
  112. /* SA_MCLK_SEL=1, SA_MCLK_DIV=0x14 */
  113. cx18_av_write(cx, 0x127, 0x54);
  114. /* AUD_COUNT = 0x1fff = 8 samples * 4 * 256 - 1 */
  115. cx18_av_write4(cx, 0x12c, 0x11201fff);
  116. /*
  117. * EN_AV_LOCK = 1
  118. * VID_COUNT = 0x0d2ef8 = 107999.000 * 8 =
  119. * ((8 samples/32,000) * (13,500,000 * 8) * 4 - 1) * 8
  120. */
  121. cx18_av_write4(cx, 0x128, 0xa10d2ef8);
  122. break;
  123. case 44100:
  124. /* VID_PLL and AUX_PLL */
  125. cx18_av_write4(cx, 0x108, 0x1809040f);
  126. /* AUX_PLL_FRAC */
  127. /* 0x9.7635e74 * 28,636,363.63 / 0x18 = 44100 * 256 */
  128. cx18_av_write4(cx, 0x110, 0x00ec6bce);
  129. /* src1_ctl */
  130. /* 0x1.60cd = 44100/32000 */
  131. cx18_av_write4(cx, 0x8f8, 0x080160cd);
  132. /* src3/4/6_ctl */
  133. /* 0x1.7385 = 2 * (32000/44100) */
  134. cx18_av_write4(cx, 0x900, 0x08017385);
  135. cx18_av_write4(cx, 0x904, 0x08017385);
  136. cx18_av_write4(cx, 0x90c, 0x08017385);
  137. /* AUD_COUNT = 0x61ff = 49 samples * 2 * 256 - 1 */
  138. cx18_av_write4(cx, 0x12c, 0x112061ff);
  139. /*
  140. * EN_AV_LOCK = 1
  141. * VID_COUNT = 0x1d4bf8 = 239999.000 * 8 =
  142. * ((49 samples/44,100) * (13,500,000 * 8) * 2 - 1) * 8
  143. */
  144. cx18_av_write4(cx, 0x128, 0xa11d4bf8);
  145. break;
  146. case 48000:
  147. /* VID_PLL and AUX_PLL */
  148. cx18_av_write4(cx, 0x108, 0x180a040f);
  149. /* AUX_PLL_FRAC */
  150. /* 0xa.4c6b6ea * 28,636,363.63 / 0x18 = 48000 * 256 */
  151. cx18_av_write4(cx, 0x110, 0x0098d6dd);
  152. /* src1_ctl */
  153. /* 0x1.8000 = 48000/32000 */
  154. cx18_av_write4(cx, 0x8f8, 0x08018000);
  155. /* src3/4/6_ctl */
  156. /* 0x1.5555 = 2 * (32000/48000) */
  157. cx18_av_write4(cx, 0x900, 0x08015555);
  158. cx18_av_write4(cx, 0x904, 0x08015555);
  159. cx18_av_write4(cx, 0x90c, 0x08015555);
  160. /* AUD_COUNT = 0x3fff = 4 samples * 16 * 256 - 1 */
  161. cx18_av_write4(cx, 0x12c, 0x11203fff);
  162. /*
  163. * EN_AV_LOCK = 1
  164. * VID_COUNT = 0x1193f8 = 143999.000 * 8 =
  165. * ((4 samples/48,000) * (13,500,000 * 8) * 16 - 1) * 8
  166. */
  167. cx18_av_write4(cx, 0x128, 0xa11193f8);
  168. break;
  169. }
  170. }
  171. state->audclk_freq = freq;
  172. return 0;
  173. }
  174. void cx18_av_audio_set_path(struct cx18 *cx)
  175. {
  176. struct cx18_av_state *state = &cx->av_state;
  177. u8 v;
  178. /* stop microcontroller */
  179. v = cx18_av_read(cx, 0x803) & ~0x10;
  180. cx18_av_write_expect(cx, 0x803, v, v, 0x1f);
  181. /* assert soft reset */
  182. v = cx18_av_read(cx, 0x810) | 0x01;
  183. cx18_av_write_expect(cx, 0x810, v, v, 0x0f);
  184. /* Mute everything to prevent the PFFT! */
  185. cx18_av_write(cx, 0x8d3, 0x1f);
  186. if (state->aud_input <= CX18_AV_AUDIO_SERIAL2) {
  187. /* Set Path1 to Serial Audio Input */
  188. cx18_av_write4(cx, 0x8d0, 0x01011012);
  189. /* The microcontroller should not be started for the
  190. * non-tuner inputs: autodetection is specific for
  191. * TV audio. */
  192. } else {
  193. /* Set Path1 to Analog Demod Main Channel */
  194. cx18_av_write4(cx, 0x8d0, 0x1f063870);
  195. }
  196. set_audclk_freq(cx, state->audclk_freq);
  197. /* deassert soft reset */
  198. v = cx18_av_read(cx, 0x810) & ~0x01;
  199. cx18_av_write_expect(cx, 0x810, v, v, 0x0f);
  200. if (state->aud_input > CX18_AV_AUDIO_SERIAL2) {
  201. /* When the microcontroller detects the
  202. * audio format, it will unmute the lines */
  203. v = cx18_av_read(cx, 0x803) | 0x10;
  204. cx18_av_write_expect(cx, 0x803, v, v, 0x1f);
  205. }
  206. }
  207. static int get_volume(struct cx18 *cx)
  208. {
  209. /* Volume runs +18dB to -96dB in 1/2dB steps
  210. * change to fit the msp3400 -114dB to +12dB range */
  211. /* check PATH1_VOLUME */
  212. int vol = 228 - cx18_av_read(cx, 0x8d4);
  213. vol = (vol / 2) + 23;
  214. return vol << 9;
  215. }
  216. static void set_volume(struct cx18 *cx, int volume)
  217. {
  218. /* First convert the volume to msp3400 values (0-127) */
  219. int vol = volume >> 9;
  220. /* now scale it up to cx18_av values
  221. * -114dB to -96dB maps to 0
  222. * this should be 19, but in my testing that was 4dB too loud */
  223. if (vol <= 23)
  224. vol = 0;
  225. else
  226. vol -= 23;
  227. /* PATH1_VOLUME */
  228. cx18_av_write(cx, 0x8d4, 228 - (vol * 2));
  229. }
  230. static int get_bass(struct cx18 *cx)
  231. {
  232. /* bass is 49 steps +12dB to -12dB */
  233. /* check PATH1_EQ_BASS_VOL */
  234. int bass = cx18_av_read(cx, 0x8d9) & 0x3f;
  235. bass = (((48 - bass) * 0xffff) + 47) / 48;
  236. return bass;
  237. }
  238. static void set_bass(struct cx18 *cx, int bass)
  239. {
  240. /* PATH1_EQ_BASS_VOL */
  241. cx18_av_and_or(cx, 0x8d9, ~0x3f, 48 - (bass * 48 / 0xffff));
  242. }
  243. static int get_treble(struct cx18 *cx)
  244. {
  245. /* treble is 49 steps +12dB to -12dB */
  246. /* check PATH1_EQ_TREBLE_VOL */
  247. int treble = cx18_av_read(cx, 0x8db) & 0x3f;
  248. treble = (((48 - treble) * 0xffff) + 47) / 48;
  249. return treble;
  250. }
  251. static void set_treble(struct cx18 *cx, int treble)
  252. {
  253. /* PATH1_EQ_TREBLE_VOL */
  254. cx18_av_and_or(cx, 0x8db, ~0x3f, 48 - (treble * 48 / 0xffff));
  255. }
  256. static int get_balance(struct cx18 *cx)
  257. {
  258. /* balance is 7 bit, 0 to -96dB */
  259. /* check PATH1_BAL_LEVEL */
  260. int balance = cx18_av_read(cx, 0x8d5) & 0x7f;
  261. /* check PATH1_BAL_LEFT */
  262. if ((cx18_av_read(cx, 0x8d5) & 0x80) == 0)
  263. balance = 0x80 - balance;
  264. else
  265. balance = 0x80 + balance;
  266. return balance << 8;
  267. }
  268. static void set_balance(struct cx18 *cx, int balance)
  269. {
  270. int bal = balance >> 8;
  271. if (bal > 0x80) {
  272. /* PATH1_BAL_LEFT */
  273. cx18_av_and_or(cx, 0x8d5, 0x7f, 0x80);
  274. /* PATH1_BAL_LEVEL */
  275. cx18_av_and_or(cx, 0x8d5, ~0x7f, bal & 0x7f);
  276. } else {
  277. /* PATH1_BAL_LEFT */
  278. cx18_av_and_or(cx, 0x8d5, 0x7f, 0x00);
  279. /* PATH1_BAL_LEVEL */
  280. cx18_av_and_or(cx, 0x8d5, ~0x7f, 0x80 - bal);
  281. }
  282. }
  283. static int get_mute(struct cx18 *cx)
  284. {
  285. /* check SRC1_MUTE_EN */
  286. return cx18_av_read(cx, 0x8d3) & 0x2 ? 1 : 0;
  287. }
  288. static void set_mute(struct cx18 *cx, int mute)
  289. {
  290. struct cx18_av_state *state = &cx->av_state;
  291. u8 v;
  292. if (state->aud_input > CX18_AV_AUDIO_SERIAL2) {
  293. /* Must turn off microcontroller in order to mute sound.
  294. * Not sure if this is the best method, but it does work.
  295. * If the microcontroller is running, then it will undo any
  296. * changes to the mute register. */
  297. v = cx18_av_read(cx, 0x803);
  298. if (mute) {
  299. /* disable microcontroller */
  300. v &= ~0x10;
  301. cx18_av_write_expect(cx, 0x803, v, v, 0x1f);
  302. cx18_av_write(cx, 0x8d3, 0x1f);
  303. } else {
  304. /* enable microcontroller */
  305. v |= 0x10;
  306. cx18_av_write_expect(cx, 0x803, v, v, 0x1f);
  307. }
  308. } else {
  309. /* SRC1_MUTE_EN */
  310. cx18_av_and_or(cx, 0x8d3, ~0x2, mute ? 0x02 : 0x00);
  311. }
  312. }
  313. int cx18_av_audio(struct cx18 *cx, unsigned int cmd, void *arg)
  314. {
  315. struct cx18_av_state *state = &cx->av_state;
  316. struct v4l2_control *ctrl = arg;
  317. int retval;
  318. switch (cmd) {
  319. case VIDIOC_INT_AUDIO_CLOCK_FREQ:
  320. {
  321. u8 v;
  322. if (state->aud_input > CX18_AV_AUDIO_SERIAL2) {
  323. v = cx18_av_read(cx, 0x803) & ~0x10;
  324. cx18_av_write_expect(cx, 0x803, v, v, 0x1f);
  325. cx18_av_write(cx, 0x8d3, 0x1f);
  326. }
  327. v = cx18_av_read(cx, 0x810) | 0x1;
  328. cx18_av_write_expect(cx, 0x810, v, v, 0x0f);
  329. retval = set_audclk_freq(cx, *(u32 *)arg);
  330. v = cx18_av_read(cx, 0x810) & ~0x1;
  331. cx18_av_write_expect(cx, 0x810, v, v, 0x0f);
  332. if (state->aud_input > CX18_AV_AUDIO_SERIAL2) {
  333. v = cx18_av_read(cx, 0x803) | 0x10;
  334. cx18_av_write_expect(cx, 0x803, v, v, 0x1f);
  335. }
  336. return retval;
  337. }
  338. case VIDIOC_G_CTRL:
  339. switch (ctrl->id) {
  340. case V4L2_CID_AUDIO_VOLUME:
  341. ctrl->value = get_volume(cx);
  342. break;
  343. case V4L2_CID_AUDIO_BASS:
  344. ctrl->value = get_bass(cx);
  345. break;
  346. case V4L2_CID_AUDIO_TREBLE:
  347. ctrl->value = get_treble(cx);
  348. break;
  349. case V4L2_CID_AUDIO_BALANCE:
  350. ctrl->value = get_balance(cx);
  351. break;
  352. case V4L2_CID_AUDIO_MUTE:
  353. ctrl->value = get_mute(cx);
  354. break;
  355. default:
  356. return -EINVAL;
  357. }
  358. break;
  359. case VIDIOC_S_CTRL:
  360. switch (ctrl->id) {
  361. case V4L2_CID_AUDIO_VOLUME:
  362. set_volume(cx, ctrl->value);
  363. break;
  364. case V4L2_CID_AUDIO_BASS:
  365. set_bass(cx, ctrl->value);
  366. break;
  367. case V4L2_CID_AUDIO_TREBLE:
  368. set_treble(cx, ctrl->value);
  369. break;
  370. case V4L2_CID_AUDIO_BALANCE:
  371. set_balance(cx, ctrl->value);
  372. break;
  373. case V4L2_CID_AUDIO_MUTE:
  374. set_mute(cx, ctrl->value);
  375. break;
  376. default:
  377. return -EINVAL;
  378. }
  379. break;
  380. default:
  381. return -EINVAL;
  382. }
  383. return 0;
  384. }