cx25840-audio.c 9.5 KB

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