cx25840-audio.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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/audiochip.h>
  20. #include <media/v4l2-common.h>
  21. #include "cx25840.h"
  22. inline static int set_audclk_freq(struct i2c_client *client,
  23. enum v4l2_audio_clock_freq freq)
  24. {
  25. struct cx25840_state *state = i2c_get_clientdata(client);
  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. switch (state->audio_input) {
  32. case AUDIO_TUNER:
  33. switch (freq) {
  34. case V4L2_AUDCLK_32_KHZ:
  35. /* VID_PLL and AUX_PLL */
  36. cx25840_write4(client, 0x108, 0x0f040610);
  37. /* AUX_PLL_FRAC */
  38. cx25840_write4(client, 0x110, 0xee39bb01);
  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 V4L2_AUDCLK_441_KHZ:
  45. /* VID_PLL and AUX_PLL */
  46. cx25840_write4(client, 0x108, 0x0f040910);
  47. /* AUX_PLL_FRAC */
  48. cx25840_write4(client, 0x110, 0xd66bec00);
  49. /* src3/4/6_ctl = 0x08016d59 */
  50. cx25840_write4(client, 0x900, 0x596d0108);
  51. cx25840_write4(client, 0x904, 0x596d0108);
  52. cx25840_write4(client, 0x90c, 0x596d0108);
  53. break;
  54. case V4L2_AUDCLK_48_KHZ:
  55. /* VID_PLL and AUX_PLL */
  56. cx25840_write4(client, 0x108, 0x0f040a10);
  57. /* AUX_PLL_FRAC */
  58. cx25840_write4(client, 0x110, 0xe5d69800);
  59. /* src3/4/6_ctl = 0x08014faa */
  60. cx25840_write4(client, 0x900, 0xaa4f0108);
  61. cx25840_write4(client, 0x904, 0xaa4f0108);
  62. cx25840_write4(client, 0x90c, 0xaa4f0108);
  63. break;
  64. }
  65. break;
  66. case AUDIO_EXTERN_1:
  67. case AUDIO_EXTERN_2:
  68. case AUDIO_INTERN:
  69. case AUDIO_RADIO:
  70. switch (freq) {
  71. case V4L2_AUDCLK_32_KHZ:
  72. /* VID_PLL and AUX_PLL */
  73. cx25840_write4(client, 0x108, 0x0f04081e);
  74. /* AUX_PLL_FRAC */
  75. cx25840_write4(client, 0x110, 0x69082a01);
  76. /* src1_ctl = 0x08010000 */
  77. cx25840_write4(client, 0x8f8, 0x00000108);
  78. /* src3/4/6_ctl = 0x08020000 */
  79. cx25840_write4(client, 0x900, 0x00000208);
  80. cx25840_write4(client, 0x904, 0x00000208);
  81. cx25840_write4(client, 0x90c, 0x00000208);
  82. /* SA_MCLK_SEL=1, SA_MCLK_DIV=0x14 */
  83. cx25840_write(client, 0x127, 0x54);
  84. break;
  85. case V4L2_AUDCLK_441_KHZ:
  86. /* VID_PLL and AUX_PLL */
  87. cx25840_write4(client, 0x108, 0x0f040918);
  88. /* AUX_PLL_FRAC */
  89. cx25840_write4(client, 0x110, 0xd66bec00);
  90. /* src1_ctl = 0x08010000 */
  91. cx25840_write4(client, 0x8f8, 0xcd600108);
  92. /* src3/4/6_ctl = 0x08020000 */
  93. cx25840_write4(client, 0x900, 0x85730108);
  94. cx25840_write4(client, 0x904, 0x85730108);
  95. cx25840_write4(client, 0x90c, 0x85730108);
  96. break;
  97. case V4L2_AUDCLK_48_KHZ:
  98. /* VID_PLL and AUX_PLL */
  99. cx25840_write4(client, 0x108, 0x0f040a18);
  100. /* AUX_PLL_FRAC */
  101. cx25840_write4(client, 0x110, 0xe5d69800);
  102. /* src1_ctl = 0x08010000 */
  103. cx25840_write4(client, 0x8f8, 0x00800108);
  104. /* src3/4/6_ctl = 0x08020000 */
  105. cx25840_write4(client, 0x900, 0x55550108);
  106. cx25840_write4(client, 0x904, 0x55550108);
  107. cx25840_write4(client, 0x90c, 0x55550108);
  108. break;
  109. }
  110. break;
  111. }
  112. /* deassert soft reset */
  113. cx25840_and_or(client, 0x810, ~0x1, 0x00);
  114. state->audclk_freq = freq;
  115. return 0;
  116. }
  117. static int set_input(struct i2c_client *client, int audio_input)
  118. {
  119. struct cx25840_state *state = i2c_get_clientdata(client);
  120. cx25840_dbg("set audio input (%d)\n", audio_input);
  121. /* stop microcontroller */
  122. cx25840_and_or(client, 0x803, ~0x10, 0);
  123. /* Mute everything to prevent the PFFT! */
  124. cx25840_write(client, 0x8d3, 0x1f);
  125. switch (audio_input) {
  126. case AUDIO_TUNER:
  127. /* Set Path1 to Analog Demod Main Channel */
  128. cx25840_write4(client, 0x8d0, 0x7038061f);
  129. /* When the microcontroller detects the
  130. * audio format, it will unmute the lines */
  131. cx25840_and_or(client, 0x803, ~0x10, 0x10);
  132. break;
  133. case AUDIO_EXTERN_1:
  134. case AUDIO_EXTERN_2:
  135. case AUDIO_INTERN:
  136. case AUDIO_RADIO:
  137. /* Set Path1 to Serial Audio Input */
  138. cx25840_write4(client, 0x8d0, 0x12100101);
  139. /* The microcontroller should not be started for the
  140. * non-tuner inputs: autodetection is specific for
  141. * TV audio. */
  142. break;
  143. default:
  144. cx25840_dbg("Invalid audio input selection %d\n", audio_input);
  145. return -EINVAL;
  146. }
  147. state->audio_input = audio_input;
  148. return set_audclk_freq(client, state->audclk_freq);
  149. }
  150. inline 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. inline 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. inline 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. inline 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. inline 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. inline 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. inline 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. inline 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. inline 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. inline static void set_mute(struct i2c_client *client, int mute)
  233. {
  234. struct cx25840_state *state = i2c_get_clientdata(client);
  235. if (state->audio_input == AUDIO_TUNER) {
  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 v4l2_control *ctrl = arg;
  256. switch (cmd) {
  257. case AUDC_SET_INPUT:
  258. return set_input(client, *(int *)arg);
  259. case VIDIOC_INT_AUDIO_CLOCK_FREQ:
  260. return set_audclk_freq(client, *(enum v4l2_audio_clock_freq *)arg);
  261. case VIDIOC_G_CTRL:
  262. switch (ctrl->id) {
  263. case V4L2_CID_AUDIO_VOLUME:
  264. ctrl->value = get_volume(client);
  265. break;
  266. case V4L2_CID_AUDIO_BASS:
  267. ctrl->value = get_bass(client);
  268. break;
  269. case V4L2_CID_AUDIO_TREBLE:
  270. ctrl->value = get_treble(client);
  271. break;
  272. case V4L2_CID_AUDIO_BALANCE:
  273. ctrl->value = get_balance(client);
  274. break;
  275. case V4L2_CID_AUDIO_MUTE:
  276. ctrl->value = get_mute(client);
  277. break;
  278. default:
  279. return -EINVAL;
  280. }
  281. break;
  282. case VIDIOC_S_CTRL:
  283. switch (ctrl->id) {
  284. case V4L2_CID_AUDIO_VOLUME:
  285. set_volume(client, ctrl->value);
  286. break;
  287. case V4L2_CID_AUDIO_BASS:
  288. set_bass(client, ctrl->value);
  289. break;
  290. case V4L2_CID_AUDIO_TREBLE:
  291. set_treble(client, ctrl->value);
  292. break;
  293. case V4L2_CID_AUDIO_BALANCE:
  294. set_balance(client, ctrl->value);
  295. break;
  296. case V4L2_CID_AUDIO_MUTE:
  297. set_mute(client, ctrl->value);
  298. break;
  299. default:
  300. return -EINVAL;
  301. }
  302. break;
  303. default:
  304. return -EINVAL;
  305. }
  306. return 0;
  307. }