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.
15 #include <linux/videodev2.h>
16 #include <linux/i2c.h>
17 #include <media/v4l2-common.h>
18 #include <media/drv-intf/cx25840.h>
20 #include "cx25840-core.h"
23 * Note: The PLL and SRC parameters are based on a reference frequency that
26 * NTSC Color subcarrier freq * 8 = 4.5 MHz/286 * 455/2 * 8 = 28.63636363... MHz
28 * However, it's not the exact reference frequency that matters, only that the
29 * firmware and modules that comprise the driver for a particular board all
30 * use the same value (close to the ideal value).
32 * Comments below will note which reference frequency is assumed for various
33 * parameters. They will usually be one of
35 * ref_freq = 28.636360 MHz
37 * ref_freq = 28.636363 MHz
40 static int cx25840_set_audclk_freq(struct i2c_client
*client
, u32 freq
)
42 struct cx25840_state
*state
= to_state(i2c_get_clientdata(client
));
44 if (state
->aud_input
!= CX25840_AUDIO_SERIAL
) {
48 * VID_PLL Integer = 0x0f, VID_PLL Post Divider = 0x04
49 * AUX_PLL Integer = 0x06, AUX PLL Post Divider = 0x10
51 cx25840_write4(client
, 0x108, 0x1006040f);
54 * VID_PLL Fraction (register 0x10c) = 0x2be2fe
55 * 28636360 * 0xf.15f17f0/4 = 108 MHz
56 * 432 MHz pre-postdivide
60 * AUX_PLL Fraction = 0x1bb39ee
61 * 28636363 * 0x6.dd9cf70/0x10 = 32000 * 384
62 * 196.6 MHz pre-postdivide
63 * FIXME < 200 MHz is out of specified valid range
64 * FIXME 28636363 ref_freq doesn't match VID PLL ref
66 cx25840_write4(client
, 0x110, 0x01bb39ee);
70 * SA_MCLK_DIV = 0x10 = 384/384 * AUX_PLL post dvivider
72 cx25840_write(client
, 0x127, 0x50);
74 if (is_cx2583x(state
))
78 /* 0x1.f77f = (4 * 28636360/8 * 2/455) / 32000 */
79 cx25840_write4(client
, 0x900, 0x0801f77f);
80 cx25840_write4(client
, 0x904, 0x0801f77f);
81 cx25840_write4(client
, 0x90c, 0x0801f77f);
86 * VID_PLL Integer = 0x0f, VID_PLL Post Divider = 0x04
87 * AUX_PLL Integer = 0x09, AUX PLL Post Divider = 0x10
89 cx25840_write4(client
, 0x108, 0x1009040f);
92 * VID_PLL Fraction (register 0x10c) = 0x2be2fe
93 * 28636360 * 0xf.15f17f0/4 = 108 MHz
94 * 432 MHz pre-postdivide
98 * AUX_PLL Fraction = 0x0ec6bd6
99 * 28636363 * 0x9.7635eb0/0x10 = 44100 * 384
100 * 271 MHz pre-postdivide
101 * FIXME 28636363 ref_freq doesn't match VID PLL ref
103 cx25840_write4(client
, 0x110, 0x00ec6bd6);
107 * SA_MCLK_DIV = 0x10 = 384/384 * AUX_PLL post dvivider
109 cx25840_write(client
, 0x127, 0x50);
111 if (is_cx2583x(state
))
115 /* 0x1.6d59 = (4 * 28636360/8 * 2/455) / 44100 */
116 cx25840_write4(client
, 0x900, 0x08016d59);
117 cx25840_write4(client
, 0x904, 0x08016d59);
118 cx25840_write4(client
, 0x90c, 0x08016d59);
123 * VID_PLL Integer = 0x0f, VID_PLL Post Divider = 0x04
124 * AUX_PLL Integer = 0x0a, AUX PLL Post Divider = 0x10
126 cx25840_write4(client
, 0x108, 0x100a040f);
129 * VID_PLL Fraction (register 0x10c) = 0x2be2fe
130 * 28636360 * 0xf.15f17f0/4 = 108 MHz
131 * 432 MHz pre-postdivide
135 * AUX_PLL Fraction = 0x098d6e5
136 * 28636363 * 0xa.4c6b728/0x10 = 48000 * 384
137 * 295 MHz pre-postdivide
138 * FIXME 28636363 ref_freq doesn't match VID PLL ref
140 cx25840_write4(client
, 0x110, 0x0098d6e5);
144 * SA_MCLK_DIV = 0x10 = 384/384 * AUX_PLL post dvivider
146 cx25840_write(client
, 0x127, 0x50);
148 if (is_cx2583x(state
))
152 /* 0x1.4faa = (4 * 28636360/8 * 2/455) / 48000 */
153 cx25840_write4(client
, 0x900, 0x08014faa);
154 cx25840_write4(client
, 0x904, 0x08014faa);
155 cx25840_write4(client
, 0x90c, 0x08014faa);
162 * VID_PLL Integer = 0x0f, VID_PLL Post Divider = 0x04
163 * AUX_PLL Integer = 0x08, AUX PLL Post Divider = 0x1e
165 cx25840_write4(client
, 0x108, 0x1e08040f);
168 * VID_PLL Fraction (register 0x10c) = 0x2be2fe
169 * 28636360 * 0xf.15f17f0/4 = 108 MHz
170 * 432 MHz pre-postdivide
174 * AUX_PLL Fraction = 0x12a0869
175 * 28636363 * 0x8.9504348/0x1e = 32000 * 256
176 * 246 MHz pre-postdivide
177 * FIXME 28636363 ref_freq doesn't match VID PLL ref
179 cx25840_write4(client
, 0x110, 0x012a0869);
183 * SA_MCLK_DIV = 0x14 = 256/384 * AUX_PLL post dvivider
185 cx25840_write(client
, 0x127, 0x54);
187 if (is_cx2583x(state
))
191 /* 0x1.0000 = 32000/32000 */
192 cx25840_write4(client
, 0x8f8, 0x08010000);
195 /* 0x2.0000 = 2 * (32000/32000) */
196 cx25840_write4(client
, 0x900, 0x08020000);
197 cx25840_write4(client
, 0x904, 0x08020000);
198 cx25840_write4(client
, 0x90c, 0x08020000);
203 * VID_PLL Integer = 0x0f, VID_PLL Post Divider = 0x04
204 * AUX_PLL Integer = 0x09, AUX PLL Post Divider = 0x18
206 cx25840_write4(client
, 0x108, 0x1809040f);
209 * VID_PLL Fraction (register 0x10c) = 0x2be2fe
210 * 28636360 * 0xf.15f17f0/4 = 108 MHz
211 * 432 MHz pre-postdivide
215 * AUX_PLL Fraction = 0x0ec6bd6
216 * 28636363 * 0x9.7635eb0/0x18 = 44100 * 256
217 * 271 MHz pre-postdivide
218 * FIXME 28636363 ref_freq doesn't match VID PLL ref
220 cx25840_write4(client
, 0x110, 0x00ec6bd6);
224 * SA_MCLK_DIV = 0x10 = 256/384 * AUX_PLL post dvivider
226 cx25840_write(client
, 0x127, 0x50);
228 if (is_cx2583x(state
))
232 /* 0x1.60cd = 44100/32000 */
233 cx25840_write4(client
, 0x8f8, 0x080160cd);
236 /* 0x1.7385 = 2 * (32000/44100) */
237 cx25840_write4(client
, 0x900, 0x08017385);
238 cx25840_write4(client
, 0x904, 0x08017385);
239 cx25840_write4(client
, 0x90c, 0x08017385);
244 * VID_PLL Integer = 0x0f, VID_PLL Post Divider = 0x04
245 * AUX_PLL Integer = 0x0a, AUX PLL Post Divider = 0x18
247 cx25840_write4(client
, 0x108, 0x180a040f);
250 * VID_PLL Fraction (register 0x10c) = 0x2be2fe
251 * 28636360 * 0xf.15f17f0/4 = 108 MHz
252 * 432 MHz pre-postdivide
256 * AUX_PLL Fraction = 0x098d6e5
257 * 28636363 * 0xa.4c6b728/0x18 = 48000 * 256
258 * 295 MHz pre-postdivide
259 * FIXME 28636363 ref_freq doesn't match VID PLL ref
261 cx25840_write4(client
, 0x110, 0x0098d6e5);
265 * SA_MCLK_DIV = 0x10 = 256/384 * AUX_PLL post dvivider
267 cx25840_write(client
, 0x127, 0x50);
269 if (is_cx2583x(state
))
273 /* 0x1.8000 = 48000/32000 */
274 cx25840_write4(client
, 0x8f8, 0x08018000);
277 /* 0x1.5555 = 2 * (32000/48000) */
278 cx25840_write4(client
, 0x900, 0x08015555);
279 cx25840_write4(client
, 0x904, 0x08015555);
280 cx25840_write4(client
, 0x90c, 0x08015555);
285 state
->audclk_freq
= freq
;
290 static inline int cx25836_set_audclk_freq(struct i2c_client
*client
, u32 freq
)
292 return cx25840_set_audclk_freq(client
, freq
);
295 static int cx23885_set_audclk_freq(struct i2c_client
*client
, u32 freq
)
297 struct cx25840_state
*state
= to_state(i2c_get_clientdata(client
));
299 if (state
->aud_input
!= CX25840_AUDIO_SERIAL
) {
304 /* We don't have register values
305 * so avoid destroying registers. */
306 /* FIXME return -EINVAL; */
313 /* We don't have register values
314 * so avoid destroying registers. */
315 /* FIXME return -EINVAL; */
320 /* 0x1.867c = 48000 / (2 * 28636360/8 * 2/455) */
321 cx25840_write4(client
, 0x8f8, 0x0801867c);
324 /* 0x1.4faa = (4 * 28636360/8 * 2/455) / 48000 */
325 cx25840_write4(client
, 0x900, 0x08014faa);
326 cx25840_write4(client
, 0x904, 0x08014faa);
327 cx25840_write4(client
, 0x90c, 0x08014faa);
332 state
->audclk_freq
= freq
;
337 static int cx231xx_set_audclk_freq(struct i2c_client
*client
, u32 freq
)
339 struct cx25840_state
*state
= to_state(i2c_get_clientdata(client
));
341 if (state
->aud_input
!= CX25840_AUDIO_SERIAL
) {
345 /* 0x1.f77f = (4 * 28636360/8 * 2/455) / 32000 */
346 cx25840_write4(client
, 0x900, 0x0801f77f);
347 cx25840_write4(client
, 0x904, 0x0801f77f);
348 cx25840_write4(client
, 0x90c, 0x0801f77f);
353 /* 0x1.6d59 = (4 * 28636360/8 * 2/455) / 44100 */
354 cx25840_write4(client
, 0x900, 0x08016d59);
355 cx25840_write4(client
, 0x904, 0x08016d59);
356 cx25840_write4(client
, 0x90c, 0x08016d59);
361 /* 0x1.4faa = (4 * 28636360/8 * 2/455) / 48000 */
362 cx25840_write4(client
, 0x900, 0x08014faa);
363 cx25840_write4(client
, 0x904, 0x08014faa);
364 cx25840_write4(client
, 0x90c, 0x08014faa);
369 /* FIXME These cases make different assumptions about audclk */
372 /* 0x1.0000 = 32000/32000 */
373 cx25840_write4(client
, 0x8f8, 0x08010000);
376 /* 0x2.0000 = 2 * (32000/32000) */
377 cx25840_write4(client
, 0x900, 0x08020000);
378 cx25840_write4(client
, 0x904, 0x08020000);
379 cx25840_write4(client
, 0x90c, 0x08020000);
384 /* 0x1.60cd = 44100/32000 */
385 cx25840_write4(client
, 0x8f8, 0x080160cd);
388 /* 0x1.7385 = 2 * (32000/44100) */
389 cx25840_write4(client
, 0x900, 0x08017385);
390 cx25840_write4(client
, 0x904, 0x08017385);
391 cx25840_write4(client
, 0x90c, 0x08017385);
396 /* 0x1.867c = 48000 / (2 * 28636360/8 * 2/455) */
397 cx25840_write4(client
, 0x8f8, 0x0801867c);
400 /* 0x1.4faa = (4 * 28636360/8 * 2/455) / 48000 */
401 cx25840_write4(client
, 0x900, 0x08014faa);
402 cx25840_write4(client
, 0x904, 0x08014faa);
403 cx25840_write4(client
, 0x90c, 0x08014faa);
408 state
->audclk_freq
= freq
;
413 static int set_audclk_freq(struct i2c_client
*client
, u32 freq
)
415 struct cx25840_state
*state
= to_state(i2c_get_clientdata(client
));
417 if (freq
!= 32000 && freq
!= 44100 && freq
!= 48000)
420 if (is_cx231xx(state
))
421 return cx231xx_set_audclk_freq(client
, freq
);
423 if (is_cx2388x(state
))
424 return cx23885_set_audclk_freq(client
, freq
);
426 if (is_cx2583x(state
))
427 return cx25836_set_audclk_freq(client
, freq
);
429 return cx25840_set_audclk_freq(client
, freq
);
432 void cx25840_audio_set_path(struct i2c_client
*client
)
434 struct cx25840_state
*state
= to_state(i2c_get_clientdata(client
));
436 if (!is_cx2583x(state
)) {
437 /* assert soft reset */
438 cx25840_and_or(client
, 0x810, ~0x1, 0x01);
440 /* stop microcontroller */
441 cx25840_and_or(client
, 0x803, ~0x10, 0);
443 /* Mute everything to prevent the PFFT! */
444 cx25840_write(client
, 0x8d3, 0x1f);
446 if (state
->aud_input
== CX25840_AUDIO_SERIAL
) {
447 /* Set Path1 to Serial Audio Input */
448 cx25840_write4(client
, 0x8d0, 0x01011012);
450 /* The microcontroller should not be started for the
451 * non-tuner inputs: autodetection is specific for
454 /* Set Path1 to Analog Demod Main Channel */
455 cx25840_write4(client
, 0x8d0, 0x1f063870);
459 set_audclk_freq(client
, state
->audclk_freq
);
461 if (!is_cx2583x(state
)) {
462 if (state
->aud_input
!= CX25840_AUDIO_SERIAL
) {
463 /* When the microcontroller detects the
464 * audio format, it will unmute the lines */
465 cx25840_and_or(client
, 0x803, ~0x10, 0x10);
468 /* deassert soft reset */
469 cx25840_and_or(client
, 0x810, ~0x1, 0x00);
471 /* Ensure the controller is running when we exit */
472 if (is_cx2388x(state
) || is_cx231xx(state
))
473 cx25840_and_or(client
, 0x803, ~0x10, 0x10);
477 static void set_volume(struct i2c_client
*client
, int volume
)
481 /* Convert the volume to msp3400 values (0-127) */
484 /* now scale it up to cx25840 values
485 * -114dB to -96dB maps to 0
486 * this should be 19, but in my testing that was 4dB too loud */
494 cx25840_write(client
, 0x8d4, 228 - (vol
* 2));
497 static void set_balance(struct i2c_client
*client
, int balance
)
499 int bal
= balance
>> 8;
502 cx25840_and_or(client
, 0x8d5, 0x7f, 0x80);
503 /* PATH1_BAL_LEVEL */
504 cx25840_and_or(client
, 0x8d5, ~0x7f, bal
& 0x7f);
507 cx25840_and_or(client
, 0x8d5, 0x7f, 0x00);
508 /* PATH1_BAL_LEVEL */
509 cx25840_and_or(client
, 0x8d5, ~0x7f, 0x80 - bal
);
513 int cx25840_s_clock_freq(struct v4l2_subdev
*sd
, u32 freq
)
515 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
516 struct cx25840_state
*state
= to_state(sd
);
519 if (!is_cx2583x(state
))
520 cx25840_and_or(client
, 0x810, ~0x1, 1);
521 if (state
->aud_input
!= CX25840_AUDIO_SERIAL
) {
522 cx25840_and_or(client
, 0x803, ~0x10, 0);
523 cx25840_write(client
, 0x8d3, 0x1f);
525 retval
= set_audclk_freq(client
, freq
);
526 if (state
->aud_input
!= CX25840_AUDIO_SERIAL
)
527 cx25840_and_or(client
, 0x803, ~0x10, 0x10);
528 if (!is_cx2583x(state
))
529 cx25840_and_or(client
, 0x810, ~0x1, 0);
533 static int cx25840_audio_s_ctrl(struct v4l2_ctrl
*ctrl
)
535 struct v4l2_subdev
*sd
= to_sd(ctrl
);
536 struct cx25840_state
*state
= to_state(sd
);
537 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
540 case V4L2_CID_AUDIO_VOLUME
:
541 if (state
->mute
->val
)
542 set_volume(client
, 0);
544 set_volume(client
, state
->volume
->val
);
546 case V4L2_CID_AUDIO_BASS
:
547 /* PATH1_EQ_BASS_VOL */
548 cx25840_and_or(client
, 0x8d9, ~0x3f,
549 48 - (ctrl
->val
* 48 / 0xffff));
551 case V4L2_CID_AUDIO_TREBLE
:
552 /* PATH1_EQ_TREBLE_VOL */
553 cx25840_and_or(client
, 0x8db, ~0x3f,
554 48 - (ctrl
->val
* 48 / 0xffff));
556 case V4L2_CID_AUDIO_BALANCE
:
557 set_balance(client
, ctrl
->val
);
565 const struct v4l2_ctrl_ops cx25840_audio_ctrl_ops
= {
566 .s_ctrl
= cx25840_audio_s_ctrl
,