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