cx18-av-audio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. /* stop microcontroller */
  178. cx18_av_and_or(cx, 0x803, ~0x10, 0);
  179. /* assert soft reset */
  180. cx18_av_and_or(cx, 0x810, ~0x1, 0x01);
  181. /* Mute everything to prevent the PFFT! */
  182. cx18_av_write(cx, 0x8d3, 0x1f);
  183. if (state->aud_input <= CX18_AV_AUDIO_SERIAL2) {
  184. /* Set Path1 to Serial Audio Input */
  185. cx18_av_write4(cx, 0x8d0, 0x01011012);
  186. /* The microcontroller should not be started for the
  187. * non-tuner inputs: autodetection is specific for
  188. * TV audio. */
  189. } else {
  190. /* Set Path1 to Analog Demod Main Channel */
  191. cx18_av_write4(cx, 0x8d0, 0x1f063870);
  192. }
  193. set_audclk_freq(cx, state->audclk_freq);
  194. /* deassert soft reset */
  195. cx18_av_and_or(cx, 0x810, ~0x1, 0x00);
  196. if (state->aud_input > CX18_AV_AUDIO_SERIAL2) {
  197. /* When the microcontroller detects the
  198. * audio format, it will unmute the lines */
  199. cx18_av_and_or(cx, 0x803, ~0x10, 0x10);
  200. }
  201. }
  202. static int get_volume(struct cx18 *cx)
  203. {
  204. /* Volume runs +18dB to -96dB in 1/2dB steps
  205. * change to fit the msp3400 -114dB to +12dB range */
  206. /* check PATH1_VOLUME */
  207. int vol = 228 - cx18_av_read(cx, 0x8d4);
  208. vol = (vol / 2) + 23;
  209. return vol << 9;
  210. }
  211. static void set_volume(struct cx18 *cx, int volume)
  212. {
  213. /* First convert the volume to msp3400 values (0-127) */
  214. int vol = volume >> 9;
  215. /* now scale it up to cx18_av values
  216. * -114dB to -96dB maps to 0
  217. * this should be 19, but in my testing that was 4dB too loud */
  218. if (vol <= 23)
  219. vol = 0;
  220. else
  221. vol -= 23;
  222. /* PATH1_VOLUME */
  223. cx18_av_write(cx, 0x8d4, 228 - (vol * 2));
  224. }
  225. static int get_bass(struct cx18 *cx)
  226. {
  227. /* bass is 49 steps +12dB to -12dB */
  228. /* check PATH1_EQ_BASS_VOL */
  229. int bass = cx18_av_read(cx, 0x8d9) & 0x3f;
  230. bass = (((48 - bass) * 0xffff) + 47) / 48;
  231. return bass;
  232. }
  233. static void set_bass(struct cx18 *cx, int bass)
  234. {
  235. /* PATH1_EQ_BASS_VOL */
  236. cx18_av_and_or(cx, 0x8d9, ~0x3f, 48 - (bass * 48 / 0xffff));
  237. }
  238. static int get_treble(struct cx18 *cx)
  239. {
  240. /* treble is 49 steps +12dB to -12dB */
  241. /* check PATH1_EQ_TREBLE_VOL */
  242. int treble = cx18_av_read(cx, 0x8db) & 0x3f;
  243. treble = (((48 - treble) * 0xffff) + 47) / 48;
  244. return treble;
  245. }
  246. static void set_treble(struct cx18 *cx, int treble)
  247. {
  248. /* PATH1_EQ_TREBLE_VOL */
  249. cx18_av_and_or(cx, 0x8db, ~0x3f, 48 - (treble * 48 / 0xffff));
  250. }
  251. static int get_balance(struct cx18 *cx)
  252. {
  253. /* balance is 7 bit, 0 to -96dB */
  254. /* check PATH1_BAL_LEVEL */
  255. int balance = cx18_av_read(cx, 0x8d5) & 0x7f;
  256. /* check PATH1_BAL_LEFT */
  257. if ((cx18_av_read(cx, 0x8d5) & 0x80) == 0)
  258. balance = 0x80 - balance;
  259. else
  260. balance = 0x80 + balance;
  261. return balance << 8;
  262. }
  263. static void set_balance(struct cx18 *cx, int balance)
  264. {
  265. int bal = balance >> 8;
  266. if (bal > 0x80) {
  267. /* PATH1_BAL_LEFT */
  268. cx18_av_and_or(cx, 0x8d5, 0x7f, 0x80);
  269. /* PATH1_BAL_LEVEL */
  270. cx18_av_and_or(cx, 0x8d5, ~0x7f, bal & 0x7f);
  271. } else {
  272. /* PATH1_BAL_LEFT */
  273. cx18_av_and_or(cx, 0x8d5, 0x7f, 0x00);
  274. /* PATH1_BAL_LEVEL */
  275. cx18_av_and_or(cx, 0x8d5, ~0x7f, 0x80 - bal);
  276. }
  277. }
  278. static int get_mute(struct cx18 *cx)
  279. {
  280. /* check SRC1_MUTE_EN */
  281. return cx18_av_read(cx, 0x8d3) & 0x2 ? 1 : 0;
  282. }
  283. static void set_mute(struct cx18 *cx, int mute)
  284. {
  285. struct cx18_av_state *state = &cx->av_state;
  286. if (state->aud_input > CX18_AV_AUDIO_SERIAL2) {
  287. /* Must turn off microcontroller in order to mute sound.
  288. * Not sure if this is the best method, but it does work.
  289. * If the microcontroller is running, then it will undo any
  290. * changes to the mute register. */
  291. if (mute) {
  292. /* disable microcontroller */
  293. cx18_av_and_or(cx, 0x803, ~0x10, 0x00);
  294. cx18_av_write(cx, 0x8d3, 0x1f);
  295. } else {
  296. /* enable microcontroller */
  297. cx18_av_and_or(cx, 0x803, ~0x10, 0x10);
  298. }
  299. } else {
  300. /* SRC1_MUTE_EN */
  301. cx18_av_and_or(cx, 0x8d3, ~0x2, mute ? 0x02 : 0x00);
  302. }
  303. }
  304. int cx18_av_audio(struct cx18 *cx, unsigned int cmd, void *arg)
  305. {
  306. struct cx18_av_state *state = &cx->av_state;
  307. struct v4l2_control *ctrl = arg;
  308. int retval;
  309. switch (cmd) {
  310. case VIDIOC_INT_AUDIO_CLOCK_FREQ:
  311. if (state->aud_input > CX18_AV_AUDIO_SERIAL2) {
  312. cx18_av_and_or(cx, 0x803, ~0x10, 0);
  313. cx18_av_write(cx, 0x8d3, 0x1f);
  314. }
  315. cx18_av_and_or(cx, 0x810, ~0x1, 1);
  316. retval = set_audclk_freq(cx, *(u32 *)arg);
  317. cx18_av_and_or(cx, 0x810, ~0x1, 0);
  318. if (state->aud_input > CX18_AV_AUDIO_SERIAL2)
  319. cx18_av_and_or(cx, 0x803, ~0x10, 0x10);
  320. return retval;
  321. case VIDIOC_G_CTRL:
  322. switch (ctrl->id) {
  323. case V4L2_CID_AUDIO_VOLUME:
  324. ctrl->value = get_volume(cx);
  325. break;
  326. case V4L2_CID_AUDIO_BASS:
  327. ctrl->value = get_bass(cx);
  328. break;
  329. case V4L2_CID_AUDIO_TREBLE:
  330. ctrl->value = get_treble(cx);
  331. break;
  332. case V4L2_CID_AUDIO_BALANCE:
  333. ctrl->value = get_balance(cx);
  334. break;
  335. case V4L2_CID_AUDIO_MUTE:
  336. ctrl->value = get_mute(cx);
  337. break;
  338. default:
  339. return -EINVAL;
  340. }
  341. break;
  342. case VIDIOC_S_CTRL:
  343. switch (ctrl->id) {
  344. case V4L2_CID_AUDIO_VOLUME:
  345. set_volume(cx, ctrl->value);
  346. break;
  347. case V4L2_CID_AUDIO_BASS:
  348. set_bass(cx, ctrl->value);
  349. break;
  350. case V4L2_CID_AUDIO_TREBLE:
  351. set_treble(cx, ctrl->value);
  352. break;
  353. case V4L2_CID_AUDIO_BALANCE:
  354. set_balance(cx, ctrl->value);
  355. break;
  356. case V4L2_CID_AUDIO_MUTE:
  357. set_mute(cx, ctrl->value);
  358. break;
  359. default:
  360. return -EINVAL;
  361. }
  362. break;
  363. default:
  364. return -EINVAL;
  365. }
  366. return 0;
  367. }