cx25840-audio.c 9.0 KB

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