cx18-av-audio.c 11 KB

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