Linux 2.6.34-rc3
[pohmelfs.git] / drivers / media / video / cx25840 / cx25840-audio.c
blob45608d50529c97233a8e0947976053b3dda3a2df
1 /* cx25840 audio functions
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.
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.
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.
19 #include <linux/videodev2.h>
20 #include <linux/i2c.h>
21 #include <media/v4l2-common.h>
22 #include <media/cx25840.h>
24 #include "cx25840-core.h"
27 * Note: The PLL and SRC parameters are based on a reference frequency that
28 * would ideally be:
30 * NTSC Color subcarrier freq * 8 = 4.5 MHz/286 * 455/2 * 8 = 28.63636363... MHz
32 * However, it's not the exact reference frequency that matters, only that the
33 * firmware and modules that comprise the driver for a particular board all
34 * use the same value (close to the ideal value).
36 * Comments below will note which reference frequency is assumed for various
37 * parameters. They will usually be one of
39 * ref_freq = 28.636360 MHz
40 * or
41 * ref_freq = 28.636363 MHz
44 static int cx25840_set_audclk_freq(struct i2c_client *client, u32 freq)
46 struct cx25840_state *state = to_state(i2c_get_clientdata(client));
48 if (state->aud_input != CX25840_AUDIO_SERIAL) {
49 switch (freq) {
50 case 32000:
52 * VID_PLL Integer = 0x0f, VID_PLL Post Divider = 0x04
53 * AUX_PLL Integer = 0x06, AUX PLL Post Divider = 0x10
55 cx25840_write4(client, 0x108, 0x1006040f);
58 * VID_PLL Fraction (register 0x10c) = 0x2be2fe
59 * 28636360 * 0xf.15f17f0/4 = 108 MHz
60 * 432 MHz pre-postdivide
64 * AUX_PLL Fraction = 0x1bb39ee
65 * 28636363 * 0x6.dd9cf70/0x10 = 32000 * 384
66 * 196.6 MHz pre-postdivide
67 * FIXME < 200 MHz is out of specified valid range
68 * FIXME 28636363 ref_freq doesn't match VID PLL ref
70 cx25840_write4(client, 0x110, 0x01bb39ee);
73 * SA_MCLK_SEL = 1
74 * SA_MCLK_DIV = 0x10 = 384/384 * AUX_PLL post dvivider
76 cx25840_write(client, 0x127, 0x50);
78 if (is_cx2583x(state))
79 break;
81 /* src3/4/6_ctl */
82 /* 0x1.f77f = (4 * 28636360/8 * 2/455) / 32000 */
83 cx25840_write4(client, 0x900, 0x0801f77f);
84 cx25840_write4(client, 0x904, 0x0801f77f);
85 cx25840_write4(client, 0x90c, 0x0801f77f);
86 break;
88 case 44100:
90 * VID_PLL Integer = 0x0f, VID_PLL Post Divider = 0x04
91 * AUX_PLL Integer = 0x09, AUX PLL Post Divider = 0x10
93 cx25840_write4(client, 0x108, 0x1009040f);
96 * VID_PLL Fraction (register 0x10c) = 0x2be2fe
97 * 28636360 * 0xf.15f17f0/4 = 108 MHz
98 * 432 MHz pre-postdivide
102 * AUX_PLL Fraction = 0x0ec6bd6
103 * 28636363 * 0x9.7635eb0/0x10 = 44100 * 384
104 * 271 MHz pre-postdivide
105 * FIXME 28636363 ref_freq doesn't match VID PLL ref
107 cx25840_write4(client, 0x110, 0x00ec6bd6);
110 * SA_MCLK_SEL = 1
111 * SA_MCLK_DIV = 0x10 = 384/384 * AUX_PLL post dvivider
113 cx25840_write(client, 0x127, 0x50);
115 if (is_cx2583x(state))
116 break;
118 /* src3/4/6_ctl */
119 /* 0x1.6d59 = (4 * 28636360/8 * 2/455) / 44100 */
120 cx25840_write4(client, 0x900, 0x08016d59);
121 cx25840_write4(client, 0x904, 0x08016d59);
122 cx25840_write4(client, 0x90c, 0x08016d59);
123 break;
125 case 48000:
127 * VID_PLL Integer = 0x0f, VID_PLL Post Divider = 0x04
128 * AUX_PLL Integer = 0x0a, AUX PLL Post Divider = 0x10
130 cx25840_write4(client, 0x108, 0x100a040f);
133 * VID_PLL Fraction (register 0x10c) = 0x2be2fe
134 * 28636360 * 0xf.15f17f0/4 = 108 MHz
135 * 432 MHz pre-postdivide
139 * AUX_PLL Fraction = 0x098d6e5
140 * 28636363 * 0xa.4c6b728/0x10 = 48000 * 384
141 * 295 MHz pre-postdivide
142 * FIXME 28636363 ref_freq doesn't match VID PLL ref
144 cx25840_write4(client, 0x110, 0x0098d6e5);
147 * SA_MCLK_SEL = 1
148 * SA_MCLK_DIV = 0x10 = 384/384 * AUX_PLL post dvivider
150 cx25840_write(client, 0x127, 0x50);
152 if (is_cx2583x(state))
153 break;
155 /* src3/4/6_ctl */
156 /* 0x1.4faa = (4 * 28636360/8 * 2/455) / 48000 */
157 cx25840_write4(client, 0x900, 0x08014faa);
158 cx25840_write4(client, 0x904, 0x08014faa);
159 cx25840_write4(client, 0x90c, 0x08014faa);
160 break;
162 } else {
163 switch (freq) {
164 case 32000:
166 * VID_PLL Integer = 0x0f, VID_PLL Post Divider = 0x04
167 * AUX_PLL Integer = 0x08, AUX PLL Post Divider = 0x1e
169 cx25840_write4(client, 0x108, 0x1e08040f);
172 * VID_PLL Fraction (register 0x10c) = 0x2be2fe
173 * 28636360 * 0xf.15f17f0/4 = 108 MHz
174 * 432 MHz pre-postdivide
178 * AUX_PLL Fraction = 0x12a0869
179 * 28636363 * 0x8.9504348/0x1e = 32000 * 256
180 * 246 MHz pre-postdivide
181 * FIXME 28636363 ref_freq doesn't match VID PLL ref
183 cx25840_write4(client, 0x110, 0x012a0869);
186 * SA_MCLK_SEL = 1
187 * SA_MCLK_DIV = 0x14 = 256/384 * AUX_PLL post dvivider
189 cx25840_write(client, 0x127, 0x54);
191 if (is_cx2583x(state))
192 break;
194 /* src1_ctl */
195 /* 0x1.0000 = 32000/32000 */
196 cx25840_write4(client, 0x8f8, 0x08010000);
198 /* src3/4/6_ctl */
199 /* 0x2.0000 = 2 * (32000/32000) */
200 cx25840_write4(client, 0x900, 0x08020000);
201 cx25840_write4(client, 0x904, 0x08020000);
202 cx25840_write4(client, 0x90c, 0x08020000);
203 break;
205 case 44100:
207 * VID_PLL Integer = 0x0f, VID_PLL Post Divider = 0x04
208 * AUX_PLL Integer = 0x09, AUX PLL Post Divider = 0x18
210 cx25840_write4(client, 0x108, 0x1809040f);
213 * VID_PLL Fraction (register 0x10c) = 0x2be2fe
214 * 28636360 * 0xf.15f17f0/4 = 108 MHz
215 * 432 MHz pre-postdivide
219 * AUX_PLL Fraction = 0x0ec6bd6
220 * 28636363 * 0x9.7635eb0/0x18 = 44100 * 256
221 * 271 MHz pre-postdivide
222 * FIXME 28636363 ref_freq doesn't match VID PLL ref
224 cx25840_write4(client, 0x110, 0x00ec6bd6);
227 * SA_MCLK_SEL = 1
228 * SA_MCLK_DIV = 0x10 = 256/384 * AUX_PLL post dvivider
230 cx25840_write(client, 0x127, 0x50);
232 if (is_cx2583x(state))
233 break;
235 /* src1_ctl */
236 /* 0x1.60cd = 44100/32000 */
237 cx25840_write4(client, 0x8f8, 0x080160cd);
239 /* src3/4/6_ctl */
240 /* 0x1.7385 = 2 * (32000/44100) */
241 cx25840_write4(client, 0x900, 0x08017385);
242 cx25840_write4(client, 0x904, 0x08017385);
243 cx25840_write4(client, 0x90c, 0x08017385);
244 break;
246 case 48000:
248 * VID_PLL Integer = 0x0f, VID_PLL Post Divider = 0x04
249 * AUX_PLL Integer = 0x0a, AUX PLL Post Divider = 0x18
251 cx25840_write4(client, 0x108, 0x180a040f);
254 * VID_PLL Fraction (register 0x10c) = 0x2be2fe
255 * 28636360 * 0xf.15f17f0/4 = 108 MHz
256 * 432 MHz pre-postdivide
260 * AUX_PLL Fraction = 0x098d6e5
261 * 28636363 * 0xa.4c6b728/0x18 = 48000 * 256
262 * 295 MHz pre-postdivide
263 * FIXME 28636363 ref_freq doesn't match VID PLL ref
265 cx25840_write4(client, 0x110, 0x0098d6e5);
268 * SA_MCLK_SEL = 1
269 * SA_MCLK_DIV = 0x10 = 256/384 * AUX_PLL post dvivider
271 cx25840_write(client, 0x127, 0x50);
273 if (is_cx2583x(state))
274 break;
276 /* src1_ctl */
277 /* 0x1.8000 = 48000/32000 */
278 cx25840_write4(client, 0x8f8, 0x08018000);
280 /* src3/4/6_ctl */
281 /* 0x1.5555 = 2 * (32000/48000) */
282 cx25840_write4(client, 0x900, 0x08015555);
283 cx25840_write4(client, 0x904, 0x08015555);
284 cx25840_write4(client, 0x90c, 0x08015555);
285 break;
289 state->audclk_freq = freq;
291 return 0;
294 static inline int cx25836_set_audclk_freq(struct i2c_client *client, u32 freq)
296 return cx25840_set_audclk_freq(client, freq);
299 static int cx23885_set_audclk_freq(struct i2c_client *client, u32 freq)
301 struct cx25840_state *state = to_state(i2c_get_clientdata(client));
303 if (state->aud_input != CX25840_AUDIO_SERIAL) {
304 switch (freq) {
305 case 32000:
306 case 44100:
307 case 48000:
308 /* We don't have register values
309 * so avoid destroying registers. */
310 /* FIXME return -EINVAL; */
311 break;
313 } else {
314 switch (freq) {
315 case 32000:
316 case 44100:
317 /* We don't have register values
318 * so avoid destroying registers. */
319 /* FIXME return -EINVAL; */
320 break;
322 case 48000:
323 /* src1_ctl */
324 /* 0x1.867c = 48000 / (2 * 28636360/8 * 2/455) */
325 cx25840_write4(client, 0x8f8, 0x0801867c);
327 /* src3/4/6_ctl */
328 /* 0x1.4faa = (4 * 28636360/8 * 2/455) / 48000 */
329 cx25840_write4(client, 0x900, 0x08014faa);
330 cx25840_write4(client, 0x904, 0x08014faa);
331 cx25840_write4(client, 0x90c, 0x08014faa);
332 break;
336 state->audclk_freq = freq;
338 return 0;
341 static int cx231xx_set_audclk_freq(struct i2c_client *client, u32 freq)
343 struct cx25840_state *state = to_state(i2c_get_clientdata(client));
345 if (state->aud_input != CX25840_AUDIO_SERIAL) {
346 switch (freq) {
347 case 32000:
348 /* src3/4/6_ctl */
349 /* 0x1.f77f = (4 * 28636360/8 * 2/455) / 32000 */
350 cx25840_write4(client, 0x900, 0x0801f77f);
351 cx25840_write4(client, 0x904, 0x0801f77f);
352 cx25840_write4(client, 0x90c, 0x0801f77f);
353 break;
355 case 44100:
356 /* src3/4/6_ctl */
357 /* 0x1.6d59 = (4 * 28636360/8 * 2/455) / 44100 */
358 cx25840_write4(client, 0x900, 0x08016d59);
359 cx25840_write4(client, 0x904, 0x08016d59);
360 cx25840_write4(client, 0x90c, 0x08016d59);
361 break;
363 case 48000:
364 /* src3/4/6_ctl */
365 /* 0x1.4faa = (4 * 28636360/8 * 2/455) / 48000 */
366 cx25840_write4(client, 0x900, 0x08014faa);
367 cx25840_write4(client, 0x904, 0x08014faa);
368 cx25840_write4(client, 0x90c, 0x08014faa);
369 break;
371 } else {
372 switch (freq) {
373 /* FIXME These cases make different assumptions about audclk */
374 case 32000:
375 /* src1_ctl */
376 /* 0x1.0000 = 32000/32000 */
377 cx25840_write4(client, 0x8f8, 0x08010000);
379 /* src3/4/6_ctl */
380 /* 0x2.0000 = 2 * (32000/32000) */
381 cx25840_write4(client, 0x900, 0x08020000);
382 cx25840_write4(client, 0x904, 0x08020000);
383 cx25840_write4(client, 0x90c, 0x08020000);
384 break;
386 case 44100:
387 /* src1_ctl */
388 /* 0x1.60cd = 44100/32000 */
389 cx25840_write4(client, 0x8f8, 0x080160cd);
391 /* src3/4/6_ctl */
392 /* 0x1.7385 = 2 * (32000/44100) */
393 cx25840_write4(client, 0x900, 0x08017385);
394 cx25840_write4(client, 0x904, 0x08017385);
395 cx25840_write4(client, 0x90c, 0x08017385);
396 break;
398 case 48000:
399 /* src1_ctl */
400 /* 0x1.867c = 48000 / (2 * 28636360/8 * 2/455) */
401 cx25840_write4(client, 0x8f8, 0x0801867c);
403 /* src3/4/6_ctl */
404 /* 0x1.4faa = (4 * 28636360/8 * 2/455) / 48000 */
405 cx25840_write4(client, 0x900, 0x08014faa);
406 cx25840_write4(client, 0x904, 0x08014faa);
407 cx25840_write4(client, 0x90c, 0x08014faa);
408 break;
412 state->audclk_freq = freq;
414 return 0;
417 static int set_audclk_freq(struct i2c_client *client, u32 freq)
419 struct cx25840_state *state = to_state(i2c_get_clientdata(client));
421 if (freq != 32000 && freq != 44100 && freq != 48000)
422 return -EINVAL;
424 if (is_cx231xx(state))
425 return cx231xx_set_audclk_freq(client, freq);
427 if (is_cx2388x(state))
428 return cx23885_set_audclk_freq(client, freq);
430 if (is_cx2583x(state))
431 return cx25836_set_audclk_freq(client, freq);
433 return cx25840_set_audclk_freq(client, freq);
436 void cx25840_audio_set_path(struct i2c_client *client)
438 struct cx25840_state *state = to_state(i2c_get_clientdata(client));
440 /* assert soft reset */
441 cx25840_and_or(client, 0x810, ~0x1, 0x01);
443 /* stop microcontroller */
444 cx25840_and_or(client, 0x803, ~0x10, 0);
446 /* Mute everything to prevent the PFFT! */
447 cx25840_write(client, 0x8d3, 0x1f);
449 if (state->aud_input == CX25840_AUDIO_SERIAL) {
450 /* Set Path1 to Serial Audio Input */
451 cx25840_write4(client, 0x8d0, 0x01011012);
453 /* The microcontroller should not be started for the
454 * non-tuner inputs: autodetection is specific for
455 * TV audio. */
456 } else {
457 /* Set Path1 to Analog Demod Main Channel */
458 cx25840_write4(client, 0x8d0, 0x1f063870);
461 set_audclk_freq(client, state->audclk_freq);
463 if (state->aud_input != CX25840_AUDIO_SERIAL) {
464 /* When the microcontroller detects the
465 * audio format, it will unmute the lines */
466 cx25840_and_or(client, 0x803, ~0x10, 0x10);
469 /* deassert soft reset */
470 cx25840_and_or(client, 0x810, ~0x1, 0x00);
472 /* Ensure the controller is running when we exit */
473 if (is_cx2388x(state) || is_cx231xx(state))
474 cx25840_and_or(client, 0x803, ~0x10, 0x10);
477 static int get_volume(struct i2c_client *client)
479 struct cx25840_state *state = to_state(i2c_get_clientdata(client));
480 int vol;
482 if (state->unmute_volume >= 0)
483 return state->unmute_volume;
485 /* Volume runs +18dB to -96dB in 1/2dB steps
486 * change to fit the msp3400 -114dB to +12dB range */
488 /* check PATH1_VOLUME */
489 vol = 228 - cx25840_read(client, 0x8d4);
490 vol = (vol / 2) + 23;
491 return vol << 9;
494 static void set_volume(struct i2c_client *client, int volume)
496 struct cx25840_state *state = to_state(i2c_get_clientdata(client));
497 int vol;
499 if (state->unmute_volume >= 0) {
500 state->unmute_volume = volume;
501 return;
504 /* Convert the volume to msp3400 values (0-127) */
505 vol = volume >> 9;
507 /* now scale it up to cx25840 values
508 * -114dB to -96dB maps to 0
509 * this should be 19, but in my testing that was 4dB too loud */
510 if (vol <= 23) {
511 vol = 0;
512 } else {
513 vol -= 23;
516 /* PATH1_VOLUME */
517 cx25840_write(client, 0x8d4, 228 - (vol * 2));
520 static int get_bass(struct i2c_client *client)
522 /* bass is 49 steps +12dB to -12dB */
524 /* check PATH1_EQ_BASS_VOL */
525 int bass = cx25840_read(client, 0x8d9) & 0x3f;
526 bass = (((48 - bass) * 0xffff) + 47) / 48;
527 return bass;
530 static void set_bass(struct i2c_client *client, int bass)
532 /* PATH1_EQ_BASS_VOL */
533 cx25840_and_or(client, 0x8d9, ~0x3f, 48 - (bass * 48 / 0xffff));
536 static int get_treble(struct i2c_client *client)
538 /* treble is 49 steps +12dB to -12dB */
540 /* check PATH1_EQ_TREBLE_VOL */
541 int treble = cx25840_read(client, 0x8db) & 0x3f;
542 treble = (((48 - treble) * 0xffff) + 47) / 48;
543 return treble;
546 static void set_treble(struct i2c_client *client, int treble)
548 /* PATH1_EQ_TREBLE_VOL */
549 cx25840_and_or(client, 0x8db, ~0x3f, 48 - (treble * 48 / 0xffff));
552 static int get_balance(struct i2c_client *client)
554 /* balance is 7 bit, 0 to -96dB */
556 /* check PATH1_BAL_LEVEL */
557 int balance = cx25840_read(client, 0x8d5) & 0x7f;
558 /* check PATH1_BAL_LEFT */
559 if ((cx25840_read(client, 0x8d5) & 0x80) == 0)
560 balance = 0x80 - balance;
561 else
562 balance = 0x80 + balance;
563 return balance << 8;
566 static void set_balance(struct i2c_client *client, int balance)
568 int bal = balance >> 8;
569 if (bal > 0x80) {
570 /* PATH1_BAL_LEFT */
571 cx25840_and_or(client, 0x8d5, 0x7f, 0x80);
572 /* PATH1_BAL_LEVEL */
573 cx25840_and_or(client, 0x8d5, ~0x7f, bal & 0x7f);
574 } else {
575 /* PATH1_BAL_LEFT */
576 cx25840_and_or(client, 0x8d5, 0x7f, 0x00);
577 /* PATH1_BAL_LEVEL */
578 cx25840_and_or(client, 0x8d5, ~0x7f, 0x80 - bal);
582 static int get_mute(struct i2c_client *client)
584 struct cx25840_state *state = to_state(i2c_get_clientdata(client));
586 return state->unmute_volume >= 0;
589 static void set_mute(struct i2c_client *client, int mute)
591 struct cx25840_state *state = to_state(i2c_get_clientdata(client));
593 if (mute && state->unmute_volume == -1) {
594 int vol = get_volume(client);
596 set_volume(client, 0);
597 state->unmute_volume = vol;
599 else if (!mute && state->unmute_volume != -1) {
600 int vol = state->unmute_volume;
602 state->unmute_volume = -1;
603 set_volume(client, vol);
607 int cx25840_s_clock_freq(struct v4l2_subdev *sd, u32 freq)
609 struct i2c_client *client = v4l2_get_subdevdata(sd);
610 struct cx25840_state *state = to_state(sd);
611 int retval;
613 if (!is_cx2583x(state))
614 cx25840_and_or(client, 0x810, ~0x1, 1);
615 if (state->aud_input != CX25840_AUDIO_SERIAL) {
616 cx25840_and_or(client, 0x803, ~0x10, 0);
617 cx25840_write(client, 0x8d3, 0x1f);
619 retval = set_audclk_freq(client, freq);
620 if (state->aud_input != CX25840_AUDIO_SERIAL)
621 cx25840_and_or(client, 0x803, ~0x10, 0x10);
622 if (!is_cx2583x(state))
623 cx25840_and_or(client, 0x810, ~0x1, 0);
624 return retval;
627 int cx25840_audio_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
629 struct i2c_client *client = v4l2_get_subdevdata(sd);
631 switch (ctrl->id) {
632 case V4L2_CID_AUDIO_VOLUME:
633 ctrl->value = get_volume(client);
634 break;
635 case V4L2_CID_AUDIO_BASS:
636 ctrl->value = get_bass(client);
637 break;
638 case V4L2_CID_AUDIO_TREBLE:
639 ctrl->value = get_treble(client);
640 break;
641 case V4L2_CID_AUDIO_BALANCE:
642 ctrl->value = get_balance(client);
643 break;
644 case V4L2_CID_AUDIO_MUTE:
645 ctrl->value = get_mute(client);
646 break;
647 default:
648 return -EINVAL;
650 return 0;
653 int cx25840_audio_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
655 struct i2c_client *client = v4l2_get_subdevdata(sd);
657 switch (ctrl->id) {
658 case V4L2_CID_AUDIO_VOLUME:
659 set_volume(client, ctrl->value);
660 break;
661 case V4L2_CID_AUDIO_BASS:
662 set_bass(client, ctrl->value);
663 break;
664 case V4L2_CID_AUDIO_TREBLE:
665 set_treble(client, ctrl->value);
666 break;
667 case V4L2_CID_AUDIO_BALANCE:
668 set_balance(client, ctrl->value);
669 break;
670 case V4L2_CID_AUDIO_MUTE:
671 set_mute(client, ctrl->value);
672 break;
673 default:
674 return -EINVAL;
676 return 0;