cx25840-audio.c 8.7 KB

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