cx25840-audio.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. if (!state->is_cx23885)
  30. cx25840_write(client, 0x127, 0x50);
  31. if (state->aud_input != CX25840_AUDIO_SERIAL) {
  32. switch (freq) {
  33. case 32000:
  34. if (state->is_cx23885) {
  35. /* We don't have register values
  36. * so avoid destroying registers. */
  37. break;
  38. }
  39. /* VID_PLL and AUX_PLL */
  40. cx25840_write4(client, 0x108, 0x1006040f);
  41. /* AUX_PLL_FRAC */
  42. cx25840_write4(client, 0x110, 0x01bb39ee);
  43. if (state->is_cx25836)
  44. break;
  45. /* src3/4/6_ctl = 0x0801f77f */
  46. cx25840_write4(client, 0x900, 0x0801f77f);
  47. cx25840_write4(client, 0x904, 0x0801f77f);
  48. cx25840_write4(client, 0x90c, 0x0801f77f);
  49. break;
  50. case 44100:
  51. if (state->is_cx23885) {
  52. /* We don't have register values
  53. * so avoid destroying registers. */
  54. break;
  55. }
  56. /* VID_PLL and AUX_PLL */
  57. cx25840_write4(client, 0x108, 0x1009040f);
  58. /* AUX_PLL_FRAC */
  59. cx25840_write4(client, 0x110, 0x00ec6bd6);
  60. if (state->is_cx25836)
  61. break;
  62. /* src3/4/6_ctl = 0x08016d59 */
  63. cx25840_write4(client, 0x900, 0x08016d59);
  64. cx25840_write4(client, 0x904, 0x08016d59);
  65. cx25840_write4(client, 0x90c, 0x08016d59);
  66. break;
  67. case 48000:
  68. if (state->is_cx23885) {
  69. /* We don't have register values
  70. * so avoid destroying registers. */
  71. break;
  72. }
  73. /* VID_PLL and AUX_PLL */
  74. cx25840_write4(client, 0x108, 0x100a040f);
  75. /* AUX_PLL_FRAC */
  76. cx25840_write4(client, 0x110, 0x0098d6e5);
  77. if (state->is_cx25836)
  78. break;
  79. /* src3/4/6_ctl = 0x08014faa */
  80. cx25840_write4(client, 0x900, 0x08014faa);
  81. cx25840_write4(client, 0x904, 0x08014faa);
  82. cx25840_write4(client, 0x90c, 0x08014faa);
  83. break;
  84. }
  85. } else {
  86. switch (freq) {
  87. case 32000:
  88. if (state->is_cx23885) {
  89. /* We don't have register values
  90. * so avoid destroying registers. */
  91. break;
  92. }
  93. /* VID_PLL and AUX_PLL */
  94. cx25840_write4(client, 0x108, 0x1e08040f);
  95. /* AUX_PLL_FRAC */
  96. cx25840_write4(client, 0x110, 0x012a0869);
  97. if (state->is_cx25836)
  98. break;
  99. /* src1_ctl = 0x08010000 */
  100. cx25840_write4(client, 0x8f8, 0x08010000);
  101. /* src3/4/6_ctl = 0x08020000 */
  102. cx25840_write4(client, 0x900, 0x08020000);
  103. cx25840_write4(client, 0x904, 0x08020000);
  104. cx25840_write4(client, 0x90c, 0x08020000);
  105. /* SA_MCLK_SEL=1, SA_MCLK_DIV=0x14 */
  106. cx25840_write(client, 0x127, 0x54);
  107. break;
  108. case 44100:
  109. if (state->is_cx23885) {
  110. /* We don't have register values
  111. * so avoid destroying registers. */
  112. break;
  113. }
  114. /* VID_PLL and AUX_PLL */
  115. cx25840_write4(client, 0x108, 0x1809040f);
  116. /* AUX_PLL_FRAC */
  117. cx25840_write4(client, 0x110, 0x00ec6bd6);
  118. if (state->is_cx25836)
  119. break;
  120. /* src1_ctl = 0x08010000 */
  121. cx25840_write4(client, 0x8f8, 0x080160cd);
  122. /* src3/4/6_ctl = 0x08020000 */
  123. cx25840_write4(client, 0x900, 0x08017385);
  124. cx25840_write4(client, 0x904, 0x08017385);
  125. cx25840_write4(client, 0x90c, 0x08017385);
  126. break;
  127. case 48000:
  128. if (!state->is_cx23885) {
  129. /* VID_PLL and AUX_PLL */
  130. cx25840_write4(client, 0x108, 0x180a040f);
  131. /* AUX_PLL_FRAC */
  132. cx25840_write4(client, 0x110, 0x0098d6e5);
  133. }
  134. if (state->is_cx25836)
  135. break;
  136. if (!state->is_cx23885) {
  137. /* src1_ctl */
  138. cx25840_write4(client, 0x8f8, 0x08018000);
  139. /* src3/4/6_ctl */
  140. cx25840_write4(client, 0x900, 0x08015555);
  141. cx25840_write4(client, 0x904, 0x08015555);
  142. cx25840_write4(client, 0x90c, 0x08015555);
  143. } else {
  144. cx25840_write4(client, 0x8f8, 0x0801867c);
  145. cx25840_write4(client, 0x900, 0x08014faa);
  146. cx25840_write4(client, 0x904, 0x08014faa);
  147. cx25840_write4(client, 0x90c, 0x08014faa);
  148. }
  149. break;
  150. }
  151. }
  152. state->audclk_freq = freq;
  153. return 0;
  154. }
  155. void cx25840_audio_set_path(struct i2c_client *client)
  156. {
  157. struct cx25840_state *state = i2c_get_clientdata(client);
  158. /* assert soft reset */
  159. cx25840_and_or(client, 0x810, ~0x1, 0x01);
  160. /* stop microcontroller */
  161. cx25840_and_or(client, 0x803, ~0x10, 0);
  162. /* Mute everything to prevent the PFFT! */
  163. cx25840_write(client, 0x8d3, 0x1f);
  164. if (state->aud_input == CX25840_AUDIO_SERIAL) {
  165. /* Set Path1 to Serial Audio Input */
  166. cx25840_write4(client, 0x8d0, 0x01011012);
  167. /* The microcontroller should not be started for the
  168. * non-tuner inputs: autodetection is specific for
  169. * TV audio. */
  170. } else {
  171. /* Set Path1 to Analog Demod Main Channel */
  172. cx25840_write4(client, 0x8d0, 0x1f063870);
  173. }
  174. set_audclk_freq(client, state->audclk_freq);
  175. if (state->aud_input != CX25840_AUDIO_SERIAL) {
  176. /* When the microcontroller detects the
  177. * audio format, it will unmute the lines */
  178. cx25840_and_or(client, 0x803, ~0x10, 0x10);
  179. }
  180. /* deassert soft reset */
  181. cx25840_and_or(client, 0x810, ~0x1, 0x00);
  182. if (state->is_cx23885) {
  183. /* Ensure the controller is running when we exit */
  184. cx25840_and_or(client, 0x803, ~0x10, 0x10);
  185. }
  186. }
  187. static int get_volume(struct i2c_client *client)
  188. {
  189. struct cx25840_state *state = i2c_get_clientdata(client);
  190. int vol;
  191. if (state->unmute_volume >= 0)
  192. return state->unmute_volume;
  193. /* Volume runs +18dB to -96dB in 1/2dB steps
  194. * change to fit the msp3400 -114dB to +12dB range */
  195. /* check PATH1_VOLUME */
  196. vol = 228 - cx25840_read(client, 0x8d4);
  197. vol = (vol / 2) + 23;
  198. return vol << 9;
  199. }
  200. static void set_volume(struct i2c_client *client, int volume)
  201. {
  202. struct cx25840_state *state = i2c_get_clientdata(client);
  203. int vol;
  204. if (state->unmute_volume >= 0) {
  205. state->unmute_volume = volume;
  206. return;
  207. }
  208. /* Convert the volume to msp3400 values (0-127) */
  209. vol = volume >> 9;
  210. /* now scale it up to cx25840 values
  211. * -114dB to -96dB maps to 0
  212. * this should be 19, but in my testing that was 4dB too loud */
  213. if (vol <= 23) {
  214. vol = 0;
  215. } else {
  216. vol -= 23;
  217. }
  218. /* PATH1_VOLUME */
  219. cx25840_write(client, 0x8d4, 228 - (vol * 2));
  220. }
  221. static int get_bass(struct i2c_client *client)
  222. {
  223. /* bass is 49 steps +12dB to -12dB */
  224. /* check PATH1_EQ_BASS_VOL */
  225. int bass = cx25840_read(client, 0x8d9) & 0x3f;
  226. bass = (((48 - bass) * 0xffff) + 47) / 48;
  227. return bass;
  228. }
  229. static void set_bass(struct i2c_client *client, int bass)
  230. {
  231. /* PATH1_EQ_BASS_VOL */
  232. cx25840_and_or(client, 0x8d9, ~0x3f, 48 - (bass * 48 / 0xffff));
  233. }
  234. static int get_treble(struct i2c_client *client)
  235. {
  236. /* treble is 49 steps +12dB to -12dB */
  237. /* check PATH1_EQ_TREBLE_VOL */
  238. int treble = cx25840_read(client, 0x8db) & 0x3f;
  239. treble = (((48 - treble) * 0xffff) + 47) / 48;
  240. return treble;
  241. }
  242. static void set_treble(struct i2c_client *client, int treble)
  243. {
  244. /* PATH1_EQ_TREBLE_VOL */
  245. cx25840_and_or(client, 0x8db, ~0x3f, 48 - (treble * 48 / 0xffff));
  246. }
  247. static int get_balance(struct i2c_client *client)
  248. {
  249. /* balance is 7 bit, 0 to -96dB */
  250. /* check PATH1_BAL_LEVEL */
  251. int balance = cx25840_read(client, 0x8d5) & 0x7f;
  252. /* check PATH1_BAL_LEFT */
  253. if ((cx25840_read(client, 0x8d5) & 0x80) == 0)
  254. balance = 0x80 - balance;
  255. else
  256. balance = 0x80 + balance;
  257. return balance << 8;
  258. }
  259. static void set_balance(struct i2c_client *client, int balance)
  260. {
  261. int bal = balance >> 8;
  262. if (bal > 0x80) {
  263. /* PATH1_BAL_LEFT */
  264. cx25840_and_or(client, 0x8d5, 0x7f, 0x80);
  265. /* PATH1_BAL_LEVEL */
  266. cx25840_and_or(client, 0x8d5, ~0x7f, bal & 0x7f);
  267. } else {
  268. /* PATH1_BAL_LEFT */
  269. cx25840_and_or(client, 0x8d5, 0x7f, 0x00);
  270. /* PATH1_BAL_LEVEL */
  271. cx25840_and_or(client, 0x8d5, ~0x7f, 0x80 - bal);
  272. }
  273. }
  274. static int get_mute(struct i2c_client *client)
  275. {
  276. struct cx25840_state *state = i2c_get_clientdata(client);
  277. return state->unmute_volume >= 0;
  278. }
  279. static void set_mute(struct i2c_client *client, int mute)
  280. {
  281. struct cx25840_state *state = i2c_get_clientdata(client);
  282. if (mute && state->unmute_volume == -1) {
  283. int vol = get_volume(client);
  284. set_volume(client, 0);
  285. state->unmute_volume = vol;
  286. }
  287. else if (!mute && state->unmute_volume != -1) {
  288. int vol = state->unmute_volume;
  289. state->unmute_volume = -1;
  290. set_volume(client, vol);
  291. }
  292. }
  293. int cx25840_audio(struct i2c_client *client, unsigned int cmd, void *arg)
  294. {
  295. struct cx25840_state *state = i2c_get_clientdata(client);
  296. struct v4l2_control *ctrl = arg;
  297. int retval;
  298. switch (cmd) {
  299. case VIDIOC_INT_AUDIO_CLOCK_FREQ:
  300. if (!state->is_cx25836)
  301. cx25840_and_or(client, 0x810, ~0x1, 1);
  302. if (state->aud_input != CX25840_AUDIO_SERIAL) {
  303. cx25840_and_or(client, 0x803, ~0x10, 0);
  304. cx25840_write(client, 0x8d3, 0x1f);
  305. }
  306. retval = set_audclk_freq(client, *(u32 *)arg);
  307. if (state->aud_input != CX25840_AUDIO_SERIAL) {
  308. cx25840_and_or(client, 0x803, ~0x10, 0x10);
  309. }
  310. if (!state->is_cx25836)
  311. cx25840_and_or(client, 0x810, ~0x1, 0);
  312. return retval;
  313. case VIDIOC_G_CTRL:
  314. switch (ctrl->id) {
  315. case V4L2_CID_AUDIO_VOLUME:
  316. ctrl->value = get_volume(client);
  317. break;
  318. case V4L2_CID_AUDIO_BASS:
  319. ctrl->value = get_bass(client);
  320. break;
  321. case V4L2_CID_AUDIO_TREBLE:
  322. ctrl->value = get_treble(client);
  323. break;
  324. case V4L2_CID_AUDIO_BALANCE:
  325. ctrl->value = get_balance(client);
  326. break;
  327. case V4L2_CID_AUDIO_MUTE:
  328. ctrl->value = get_mute(client);
  329. break;
  330. default:
  331. return -EINVAL;
  332. }
  333. break;
  334. case VIDIOC_S_CTRL:
  335. switch (ctrl->id) {
  336. case V4L2_CID_AUDIO_VOLUME:
  337. set_volume(client, ctrl->value);
  338. break;
  339. case V4L2_CID_AUDIO_BASS:
  340. set_bass(client, ctrl->value);
  341. break;
  342. case V4L2_CID_AUDIO_TREBLE:
  343. set_treble(client, ctrl->value);
  344. break;
  345. case V4L2_CID_AUDIO_BALANCE:
  346. set_balance(client, ctrl->value);
  347. break;
  348. case V4L2_CID_AUDIO_MUTE:
  349. set_mute(client, ctrl->value);
  350. break;
  351. default:
  352. return -EINVAL;
  353. }
  354. break;
  355. default:
  356. return -EINVAL;
  357. }
  358. return 0;
  359. }