cx25840-audio.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /* cx25840 audio functions
  2. *
  3. * This program is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU General Public License
  5. * as published by the Free Software Foundation; either version 2
  6. * of the License, or (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. */
  17. #include <linux/videodev2.h>
  18. #include <linux/i2c.h>
  19. #include <media/v4l2-common.h>
  20. #include <media/cx25840.h>
  21. #include "cx25840-core.h"
  22. static int set_audclk_freq(struct i2c_client *client, u32 freq)
  23. {
  24. struct cx25840_state *state = i2c_get_clientdata(client);
  25. if (freq != 32000 && freq != 44100 && freq != 48000)
  26. return -EINVAL;
  27. /* common for all inputs and rates */
  28. /* SA_MCLK_SEL=1, SA_MCLK_DIV=0x10 */
  29. cx25840_write(client, 0x127, 0x50);
  30. if (state->aud_input != CX25840_AUDIO_SERIAL) {
  31. switch (freq) {
  32. case 32000:
  33. /* VID_PLL and AUX_PLL */
  34. cx25840_write4(client, 0x108, 0x1006040f);
  35. /* AUX_PLL_FRAC */
  36. cx25840_write4(client, 0x110, 0x01bb39ee);
  37. if (state->is_cx25836)
  38. break;
  39. /* src3/4/6_ctl = 0x0801f77f */
  40. cx25840_write4(client, 0x900, 0x0801f77f);
  41. cx25840_write4(client, 0x904, 0x0801f77f);
  42. cx25840_write4(client, 0x90c, 0x0801f77f);
  43. break;
  44. case 44100:
  45. /* VID_PLL and AUX_PLL */
  46. cx25840_write4(client, 0x108, 0x1009040f);
  47. /* AUX_PLL_FRAC */
  48. cx25840_write4(client, 0x110, 0x00ec6bd6);
  49. if (state->is_cx25836)
  50. break;
  51. /* src3/4/6_ctl = 0x08016d59 */
  52. cx25840_write4(client, 0x900, 0x08016d59);
  53. cx25840_write4(client, 0x904, 0x08016d59);
  54. cx25840_write4(client, 0x90c, 0x08016d59);
  55. break;
  56. case 48000:
  57. /* VID_PLL and AUX_PLL */
  58. cx25840_write4(client, 0x108, 0x100a040f);
  59. /* AUX_PLL_FRAC */
  60. cx25840_write4(client, 0x110, 0x0098d6e5);
  61. if (state->is_cx25836)
  62. break;
  63. /* src3/4/6_ctl = 0x08014faa */
  64. cx25840_write4(client, 0x900, 0x08014faa);
  65. cx25840_write4(client, 0x904, 0x08014faa);
  66. cx25840_write4(client, 0x90c, 0x08014faa);
  67. break;
  68. }
  69. } else {
  70. switch (freq) {
  71. case 32000:
  72. /* VID_PLL and AUX_PLL */
  73. cx25840_write4(client, 0x108, 0x1e08040f);
  74. /* AUX_PLL_FRAC */
  75. cx25840_write4(client, 0x110, 0x012a0869);
  76. if (state->is_cx25836)
  77. break;
  78. /* src1_ctl = 0x08010000 */
  79. cx25840_write4(client, 0x8f8, 0x08010000);
  80. /* src3/4/6_ctl = 0x08020000 */
  81. cx25840_write4(client, 0x900, 0x08020000);
  82. cx25840_write4(client, 0x904, 0x08020000);
  83. cx25840_write4(client, 0x90c, 0x08020000);
  84. /* SA_MCLK_SEL=1, SA_MCLK_DIV=0x14 */
  85. cx25840_write(client, 0x127, 0x54);
  86. break;
  87. case 44100:
  88. /* VID_PLL and AUX_PLL */
  89. cx25840_write4(client, 0x108, 0x1809040f);
  90. /* AUX_PLL_FRAC */
  91. cx25840_write4(client, 0x110, 0x00ec6bd6);
  92. if (state->is_cx25836)
  93. break;
  94. /* src1_ctl = 0x08010000 */
  95. cx25840_write4(client, 0x8f8, 0x080160cd);
  96. /* src3/4/6_ctl = 0x08020000 */
  97. cx25840_write4(client, 0x900, 0x08017385);
  98. cx25840_write4(client, 0x904, 0x08017385);
  99. cx25840_write4(client, 0x90c, 0x08017385);
  100. break;
  101. case 48000:
  102. /* VID_PLL and AUX_PLL */
  103. cx25840_write4(client, 0x108, 0x180a040f);
  104. /* AUX_PLL_FRAC */
  105. cx25840_write4(client, 0x110, 0x0098d6e5);
  106. if (state->is_cx25836)
  107. break;
  108. /* src1_ctl = 0x08010000 */
  109. cx25840_write4(client, 0x8f8, 0x08018000);
  110. /* src3/4/6_ctl = 0x08020000 */
  111. cx25840_write4(client, 0x900, 0x08015555);
  112. cx25840_write4(client, 0x904, 0x08015555);
  113. cx25840_write4(client, 0x90c, 0x08015555);
  114. break;
  115. }
  116. }
  117. state->audclk_freq = freq;
  118. return 0;
  119. }
  120. void cx25840_audio_set_path(struct i2c_client *client)
  121. {
  122. struct cx25840_state *state = i2c_get_clientdata(client);
  123. /* assert soft reset */
  124. cx25840_and_or(client, 0x810, ~0x1, 0x01);
  125. /* stop microcontroller */
  126. cx25840_and_or(client, 0x803, ~0x10, 0);
  127. /* Mute everything to prevent the PFFT! */
  128. cx25840_write(client, 0x8d3, 0x1f);
  129. if (state->aud_input == CX25840_AUDIO_SERIAL) {
  130. /* Set Path1 to Serial Audio Input */
  131. cx25840_write4(client, 0x8d0, 0x01011012);
  132. /* The microcontroller should not be started for the
  133. * non-tuner inputs: autodetection is specific for
  134. * TV audio. */
  135. } else {
  136. /* Set Path1 to Analog Demod Main Channel */
  137. cx25840_write4(client, 0x8d0, 0x1f063870);
  138. }
  139. set_audclk_freq(client, state->audclk_freq);
  140. if (state->aud_input != CX25840_AUDIO_SERIAL) {
  141. /* When the microcontroller detects the
  142. * audio format, it will unmute the lines */
  143. cx25840_and_or(client, 0x803, ~0x10, 0x10);
  144. }
  145. /* deassert soft reset */
  146. cx25840_and_or(client, 0x810, ~0x1, 0x00);
  147. }
  148. static int get_volume(struct i2c_client *client)
  149. {
  150. struct cx25840_state *state = i2c_get_clientdata(client);
  151. int vol;
  152. if (state->unmute_volume >= 0)
  153. return state->unmute_volume;
  154. /* Volume runs +18dB to -96dB in 1/2dB steps
  155. * change to fit the msp3400 -114dB to +12dB range */
  156. /* check PATH1_VOLUME */
  157. vol = 228 - cx25840_read(client, 0x8d4);
  158. vol = (vol / 2) + 23;
  159. return vol << 9;
  160. }
  161. static void set_volume(struct i2c_client *client, int volume)
  162. {
  163. struct cx25840_state *state = i2c_get_clientdata(client);
  164. int vol;
  165. if (state->unmute_volume >= 0) {
  166. state->unmute_volume = volume;
  167. return;
  168. }
  169. /* Convert the volume to msp3400 values (0-127) */
  170. vol = volume >> 9;
  171. /* now scale it up to cx25840 values
  172. * -114dB to -96dB maps to 0
  173. * this should be 19, but in my testing that was 4dB too loud */
  174. if (vol <= 23) {
  175. vol = 0;
  176. } else {
  177. vol -= 23;
  178. }
  179. /* PATH1_VOLUME */
  180. cx25840_write(client, 0x8d4, 228 - (vol * 2));
  181. }
  182. static int get_bass(struct i2c_client *client)
  183. {
  184. /* bass is 49 steps +12dB to -12dB */
  185. /* check PATH1_EQ_BASS_VOL */
  186. int bass = cx25840_read(client, 0x8d9) & 0x3f;
  187. bass = (((48 - bass) * 0xffff) + 47) / 48;
  188. return bass;
  189. }
  190. static void set_bass(struct i2c_client *client, int bass)
  191. {
  192. /* PATH1_EQ_BASS_VOL */
  193. cx25840_and_or(client, 0x8d9, ~0x3f, 48 - (bass * 48 / 0xffff));
  194. }
  195. static int get_treble(struct i2c_client *client)
  196. {
  197. /* treble is 49 steps +12dB to -12dB */
  198. /* check PATH1_EQ_TREBLE_VOL */
  199. int treble = cx25840_read(client, 0x8db) & 0x3f;
  200. treble = (((48 - treble) * 0xffff) + 47) / 48;
  201. return treble;
  202. }
  203. static void set_treble(struct i2c_client *client, int treble)
  204. {
  205. /* PATH1_EQ_TREBLE_VOL */
  206. cx25840_and_or(client, 0x8db, ~0x3f, 48 - (treble * 48 / 0xffff));
  207. }
  208. static int get_balance(struct i2c_client *client)
  209. {
  210. /* balance is 7 bit, 0 to -96dB */
  211. /* check PATH1_BAL_LEVEL */
  212. int balance = cx25840_read(client, 0x8d5) & 0x7f;
  213. /* check PATH1_BAL_LEFT */
  214. if ((cx25840_read(client, 0x8d5) & 0x80) == 0)
  215. balance = 0x80 - balance;
  216. else
  217. balance = 0x80 + balance;
  218. return balance << 8;
  219. }
  220. static void set_balance(struct i2c_client *client, int balance)
  221. {
  222. int bal = balance >> 8;
  223. if (bal > 0x80) {
  224. /* PATH1_BAL_LEFT */
  225. cx25840_and_or(client, 0x8d5, 0x7f, 0x80);
  226. /* PATH1_BAL_LEVEL */
  227. cx25840_and_or(client, 0x8d5, ~0x7f, bal & 0x7f);
  228. } else {
  229. /* PATH1_BAL_LEFT */
  230. cx25840_and_or(client, 0x8d5, 0x7f, 0x00);
  231. /* PATH1_BAL_LEVEL */
  232. cx25840_and_or(client, 0x8d5, ~0x7f, 0x80 - bal);
  233. }
  234. }
  235. static int get_mute(struct i2c_client *client)
  236. {
  237. struct cx25840_state *state = i2c_get_clientdata(client);
  238. return state->unmute_volume >= 0;
  239. }
  240. static void set_mute(struct i2c_client *client, int mute)
  241. {
  242. struct cx25840_state *state = i2c_get_clientdata(client);
  243. if (mute && state->unmute_volume == -1) {
  244. int vol = get_volume(client);
  245. set_volume(client, 0);
  246. state->unmute_volume = vol;
  247. }
  248. else if (!mute && state->unmute_volume != -1) {
  249. int vol = state->unmute_volume;
  250. state->unmute_volume = -1;
  251. set_volume(client, vol);
  252. }
  253. }
  254. int cx25840_audio(struct i2c_client *client, unsigned int cmd, void *arg)
  255. {
  256. struct cx25840_state *state = i2c_get_clientdata(client);
  257. struct v4l2_control *ctrl = arg;
  258. int retval;
  259. switch (cmd) {
  260. case VIDIOC_INT_AUDIO_CLOCK_FREQ:
  261. if (!state->is_cx25836)
  262. cx25840_and_or(client, 0x810, ~0x1, 1);
  263. if (state->aud_input != CX25840_AUDIO_SERIAL) {
  264. cx25840_and_or(client, 0x803, ~0x10, 0);
  265. cx25840_write(client, 0x8d3, 0x1f);
  266. }
  267. retval = set_audclk_freq(client, *(u32 *)arg);
  268. if (state->aud_input != CX25840_AUDIO_SERIAL) {
  269. cx25840_and_or(client, 0x803, ~0x10, 0x10);
  270. }
  271. if (!state->is_cx25836)
  272. cx25840_and_or(client, 0x810, ~0x1, 0);
  273. return retval;
  274. case VIDIOC_G_CTRL:
  275. switch (ctrl->id) {
  276. case V4L2_CID_AUDIO_VOLUME:
  277. ctrl->value = get_volume(client);
  278. break;
  279. case V4L2_CID_AUDIO_BASS:
  280. ctrl->value = get_bass(client);
  281. break;
  282. case V4L2_CID_AUDIO_TREBLE:
  283. ctrl->value = get_treble(client);
  284. break;
  285. case V4L2_CID_AUDIO_BALANCE:
  286. ctrl->value = get_balance(client);
  287. break;
  288. case V4L2_CID_AUDIO_MUTE:
  289. ctrl->value = get_mute(client);
  290. break;
  291. default:
  292. return -EINVAL;
  293. }
  294. break;
  295. case VIDIOC_S_CTRL:
  296. switch (ctrl->id) {
  297. case V4L2_CID_AUDIO_VOLUME:
  298. set_volume(client, ctrl->value);
  299. break;
  300. case V4L2_CID_AUDIO_BASS:
  301. set_bass(client, ctrl->value);
  302. break;
  303. case V4L2_CID_AUDIO_TREBLE:
  304. set_treble(client, ctrl->value);
  305. break;
  306. case V4L2_CID_AUDIO_BALANCE:
  307. set_balance(client, ctrl->value);
  308. break;
  309. case V4L2_CID_AUDIO_MUTE:
  310. set_mute(client, ctrl->value);
  311. break;
  312. default:
  313. return -EINVAL;
  314. }
  315. break;
  316. default:
  317. return -EINVAL;
  318. }
  319. return 0;
  320. }