cx18-av-audio.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. /* common for all inputs and rates */
  30. /* SA_MCLK_SEL=1, SA_MCLK_DIV=0x10 */
  31. cx18_av_write(cx, 0x127, 0x50);
  32. if (state->aud_input != CX18_AV_AUDIO_SERIAL) {
  33. switch (freq) {
  34. case 32000:
  35. /* VID_PLL and AUX_PLL */
  36. cx18_av_write4(cx, 0x108, 0x1006040f);
  37. /* AUX_PLL_FRAC */
  38. cx18_av_write4(cx, 0x110, 0x01bb39ee);
  39. /* src3/4/6_ctl = 0x0801f77f */
  40. cx18_av_write4(cx, 0x900, 0x0801f77f);
  41. cx18_av_write4(cx, 0x904, 0x0801f77f);
  42. cx18_av_write4(cx, 0x90c, 0x0801f77f);
  43. break;
  44. case 44100:
  45. /* VID_PLL and AUX_PLL */
  46. cx18_av_write4(cx, 0x108, 0x1009040f);
  47. /* AUX_PLL_FRAC */
  48. cx18_av_write4(cx, 0x110, 0x00ec6bd6);
  49. /* src3/4/6_ctl = 0x08016d59 */
  50. cx18_av_write4(cx, 0x900, 0x08016d59);
  51. cx18_av_write4(cx, 0x904, 0x08016d59);
  52. cx18_av_write4(cx, 0x90c, 0x08016d59);
  53. break;
  54. case 48000:
  55. /* VID_PLL and AUX_PLL */
  56. cx18_av_write4(cx, 0x108, 0x100a040f);
  57. /* AUX_PLL_FRAC */
  58. cx18_av_write4(cx, 0x110, 0x0098d6e5);
  59. /* src3/4/6_ctl = 0x08014faa */
  60. cx18_av_write4(cx, 0x900, 0x08014faa);
  61. cx18_av_write4(cx, 0x904, 0x08014faa);
  62. cx18_av_write4(cx, 0x90c, 0x08014faa);
  63. break;
  64. }
  65. } else {
  66. switch (freq) {
  67. case 32000:
  68. /* VID_PLL and AUX_PLL */
  69. cx18_av_write4(cx, 0x108, 0x1e08040f);
  70. /* AUX_PLL_FRAC */
  71. cx18_av_write4(cx, 0x110, 0x012a0869);
  72. /* src1_ctl = 0x08010000 */
  73. cx18_av_write4(cx, 0x8f8, 0x08010000);
  74. /* src3/4/6_ctl = 0x08020000 */
  75. cx18_av_write4(cx, 0x900, 0x08020000);
  76. cx18_av_write4(cx, 0x904, 0x08020000);
  77. cx18_av_write4(cx, 0x90c, 0x08020000);
  78. /* SA_MCLK_SEL=1, SA_MCLK_DIV=0x14 */
  79. cx18_av_write(cx, 0x127, 0x54);
  80. break;
  81. case 44100:
  82. /* VID_PLL and AUX_PLL */
  83. cx18_av_write4(cx, 0x108, 0x1809040f);
  84. /* AUX_PLL_FRAC */
  85. cx18_av_write4(cx, 0x110, 0x00ec6bd6);
  86. /* src1_ctl = 0x08010000 */
  87. cx18_av_write4(cx, 0x8f8, 0x080160cd);
  88. /* src3/4/6_ctl = 0x08020000 */
  89. cx18_av_write4(cx, 0x900, 0x08017385);
  90. cx18_av_write4(cx, 0x904, 0x08017385);
  91. cx18_av_write4(cx, 0x90c, 0x08017385);
  92. break;
  93. case 48000:
  94. /* VID_PLL and AUX_PLL */
  95. cx18_av_write4(cx, 0x108, 0x180a040f);
  96. /* AUX_PLL_FRAC */
  97. cx18_av_write4(cx, 0x110, 0x0098d6e5);
  98. /* src1_ctl = 0x08010000 */
  99. cx18_av_write4(cx, 0x8f8, 0x08018000);
  100. /* src3/4/6_ctl = 0x08020000 */
  101. cx18_av_write4(cx, 0x900, 0x08015555);
  102. cx18_av_write4(cx, 0x904, 0x08015555);
  103. cx18_av_write4(cx, 0x90c, 0x08015555);
  104. break;
  105. }
  106. }
  107. state->audclk_freq = freq;
  108. return 0;
  109. }
  110. void cx18_av_audio_set_path(struct cx18 *cx)
  111. {
  112. struct cx18_av_state *state = &cx->av_state;
  113. /* stop microcontroller */
  114. cx18_av_and_or(cx, 0x803, ~0x10, 0);
  115. /* assert soft reset */
  116. cx18_av_and_or(cx, 0x810, ~0x1, 0x01);
  117. /* Mute everything to prevent the PFFT! */
  118. cx18_av_write(cx, 0x8d3, 0x1f);
  119. if (state->aud_input == CX18_AV_AUDIO_SERIAL) {
  120. /* Set Path1 to Serial Audio Input */
  121. cx18_av_write4(cx, 0x8d0, 0x01011012);
  122. /* The microcontroller should not be started for the
  123. * non-tuner inputs: autodetection is specific for
  124. * TV audio. */
  125. } else {
  126. /* Set Path1 to Analog Demod Main Channel */
  127. cx18_av_write4(cx, 0x8d0, 0x1f063870);
  128. }
  129. set_audclk_freq(cx, state->audclk_freq);
  130. /* deassert soft reset */
  131. cx18_av_and_or(cx, 0x810, ~0x1, 0x00);
  132. if (state->aud_input != CX18_AV_AUDIO_SERIAL) {
  133. /* When the microcontroller detects the
  134. * audio format, it will unmute the lines */
  135. cx18_av_and_or(cx, 0x803, ~0x10, 0x10);
  136. }
  137. }
  138. static int get_volume(struct cx18 *cx)
  139. {
  140. /* Volume runs +18dB to -96dB in 1/2dB steps
  141. * change to fit the msp3400 -114dB to +12dB range */
  142. /* check PATH1_VOLUME */
  143. int vol = 228 - cx18_av_read(cx, 0x8d4);
  144. vol = (vol / 2) + 23;
  145. return vol << 9;
  146. }
  147. static void set_volume(struct cx18 *cx, int volume)
  148. {
  149. /* First convert the volume to msp3400 values (0-127) */
  150. int vol = volume >> 9;
  151. /* now scale it up to cx18_av values
  152. * -114dB to -96dB maps to 0
  153. * this should be 19, but in my testing that was 4dB too loud */
  154. if (vol <= 23)
  155. vol = 0;
  156. else
  157. vol -= 23;
  158. /* PATH1_VOLUME */
  159. cx18_av_write(cx, 0x8d4, 228 - (vol * 2));
  160. }
  161. static int get_bass(struct cx18 *cx)
  162. {
  163. /* bass is 49 steps +12dB to -12dB */
  164. /* check PATH1_EQ_BASS_VOL */
  165. int bass = cx18_av_read(cx, 0x8d9) & 0x3f;
  166. bass = (((48 - bass) * 0xffff) + 47) / 48;
  167. return bass;
  168. }
  169. static void set_bass(struct cx18 *cx, int bass)
  170. {
  171. /* PATH1_EQ_BASS_VOL */
  172. cx18_av_and_or(cx, 0x8d9, ~0x3f, 48 - (bass * 48 / 0xffff));
  173. }
  174. static int get_treble(struct cx18 *cx)
  175. {
  176. /* treble is 49 steps +12dB to -12dB */
  177. /* check PATH1_EQ_TREBLE_VOL */
  178. int treble = cx18_av_read(cx, 0x8db) & 0x3f;
  179. treble = (((48 - treble) * 0xffff) + 47) / 48;
  180. return treble;
  181. }
  182. static void set_treble(struct cx18 *cx, int treble)
  183. {
  184. /* PATH1_EQ_TREBLE_VOL */
  185. cx18_av_and_or(cx, 0x8db, ~0x3f, 48 - (treble * 48 / 0xffff));
  186. }
  187. static int get_balance(struct cx18 *cx)
  188. {
  189. /* balance is 7 bit, 0 to -96dB */
  190. /* check PATH1_BAL_LEVEL */
  191. int balance = cx18_av_read(cx, 0x8d5) & 0x7f;
  192. /* check PATH1_BAL_LEFT */
  193. if ((cx18_av_read(cx, 0x8d5) & 0x80) == 0)
  194. balance = 0x80 - balance;
  195. else
  196. balance = 0x80 + balance;
  197. return balance << 8;
  198. }
  199. static void set_balance(struct cx18 *cx, int balance)
  200. {
  201. int bal = balance >> 8;
  202. if (bal > 0x80) {
  203. /* PATH1_BAL_LEFT */
  204. cx18_av_and_or(cx, 0x8d5, 0x7f, 0x80);
  205. /* PATH1_BAL_LEVEL */
  206. cx18_av_and_or(cx, 0x8d5, ~0x7f, bal & 0x7f);
  207. } else {
  208. /* PATH1_BAL_LEFT */
  209. cx18_av_and_or(cx, 0x8d5, 0x7f, 0x00);
  210. /* PATH1_BAL_LEVEL */
  211. cx18_av_and_or(cx, 0x8d5, ~0x7f, 0x80 - bal);
  212. }
  213. }
  214. static int get_mute(struct cx18 *cx)
  215. {
  216. /* check SRC1_MUTE_EN */
  217. return cx18_av_read(cx, 0x8d3) & 0x2 ? 1 : 0;
  218. }
  219. static void set_mute(struct cx18 *cx, int mute)
  220. {
  221. struct cx18_av_state *state = &cx->av_state;
  222. if (state->aud_input != CX18_AV_AUDIO_SERIAL) {
  223. /* Must turn off microcontroller in order to mute sound.
  224. * Not sure if this is the best method, but it does work.
  225. * If the microcontroller is running, then it will undo any
  226. * changes to the mute register. */
  227. if (mute) {
  228. /* disable microcontroller */
  229. cx18_av_and_or(cx, 0x803, ~0x10, 0x00);
  230. cx18_av_write(cx, 0x8d3, 0x1f);
  231. } else {
  232. /* enable microcontroller */
  233. cx18_av_and_or(cx, 0x803, ~0x10, 0x10);
  234. }
  235. } else {
  236. /* SRC1_MUTE_EN */
  237. cx18_av_and_or(cx, 0x8d3, ~0x2, mute ? 0x02 : 0x00);
  238. }
  239. }
  240. int cx18_av_audio(struct cx18 *cx, unsigned int cmd, void *arg)
  241. {
  242. struct cx18_av_state *state = &cx->av_state;
  243. struct v4l2_control *ctrl = arg;
  244. int retval;
  245. switch (cmd) {
  246. case VIDIOC_INT_AUDIO_CLOCK_FREQ:
  247. if (state->aud_input != CX18_AV_AUDIO_SERIAL) {
  248. cx18_av_and_or(cx, 0x803, ~0x10, 0);
  249. cx18_av_write(cx, 0x8d3, 0x1f);
  250. }
  251. cx18_av_and_or(cx, 0x810, ~0x1, 1);
  252. retval = set_audclk_freq(cx, *(u32 *)arg);
  253. cx18_av_and_or(cx, 0x810, ~0x1, 0);
  254. if (state->aud_input != CX18_AV_AUDIO_SERIAL)
  255. cx18_av_and_or(cx, 0x803, ~0x10, 0x10);
  256. return retval;
  257. case VIDIOC_G_CTRL:
  258. switch (ctrl->id) {
  259. case V4L2_CID_AUDIO_VOLUME:
  260. ctrl->value = get_volume(cx);
  261. break;
  262. case V4L2_CID_AUDIO_BASS:
  263. ctrl->value = get_bass(cx);
  264. break;
  265. case V4L2_CID_AUDIO_TREBLE:
  266. ctrl->value = get_treble(cx);
  267. break;
  268. case V4L2_CID_AUDIO_BALANCE:
  269. ctrl->value = get_balance(cx);
  270. break;
  271. case V4L2_CID_AUDIO_MUTE:
  272. ctrl->value = get_mute(cx);
  273. break;
  274. default:
  275. return -EINVAL;
  276. }
  277. break;
  278. case VIDIOC_S_CTRL:
  279. switch (ctrl->id) {
  280. case V4L2_CID_AUDIO_VOLUME:
  281. set_volume(cx, ctrl->value);
  282. break;
  283. case V4L2_CID_AUDIO_BASS:
  284. set_bass(cx, ctrl->value);
  285. break;
  286. case V4L2_CID_AUDIO_TREBLE:
  287. set_treble(cx, ctrl->value);
  288. break;
  289. case V4L2_CID_AUDIO_BALANCE:
  290. set_balance(cx, ctrl->value);
  291. break;
  292. case V4L2_CID_AUDIO_MUTE:
  293. set_mute(cx, ctrl->value);
  294. break;
  295. default:
  296. return -EINVAL;
  297. }
  298. break;
  299. default:
  300. return -EINVAL;
  301. }
  302. return 0;
  303. }