cx25840-audio.c 11 KB

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