2 * Programming the mspx4xx sound processor family
4 * (c) 1997-2001 Gerd Knorr <kraxel@bytesex.org>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/i2c.h>
26 #include <linux/freezer.h>
27 #include <linux/videodev2.h>
28 #include <media/v4l2-common.h>
29 #include <media/msp3400.h>
30 #include <linux/kthread.h>
31 #include <linux/suspend.h>
32 #include "msp3400-driver.h"
34 /* this one uses the automatic sound standard detection of newer msp34xx
41 { 0x0000, 0, 0, "could not detect sound standard" },
42 { 0x0001, 0, 0, "autodetect start" },
43 { 0x0002, MSP_CARRIER(4.5), MSP_CARRIER(4.72), "4.5/4.72 M Dual FM-Stereo" },
44 { 0x0003, MSP_CARRIER(5.5), MSP_CARRIER(5.7421875), "5.5/5.74 B/G Dual FM-Stereo" },
45 { 0x0004, MSP_CARRIER(6.5), MSP_CARRIER(6.2578125), "6.5/6.25 D/K1 Dual FM-Stereo" },
46 { 0x0005, MSP_CARRIER(6.5), MSP_CARRIER(6.7421875), "6.5/6.74 D/K2 Dual FM-Stereo" },
47 { 0x0006, MSP_CARRIER(6.5), MSP_CARRIER(6.5), "6.5 D/K FM-Mono (HDEV3)" },
48 { 0x0007, MSP_CARRIER(6.5), MSP_CARRIER(5.7421875), "6.5/5.74 D/K3 Dual FM-Stereo" },
49 { 0x0008, MSP_CARRIER(5.5), MSP_CARRIER(5.85), "5.5/5.85 B/G NICAM FM" },
50 { 0x0009, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85 L NICAM AM" },
51 { 0x000a, MSP_CARRIER(6.0), MSP_CARRIER(6.55), "6.0/6.55 I NICAM FM" },
52 { 0x000b, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85 D/K NICAM FM" },
53 { 0x000c, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85 D/K NICAM FM (HDEV2)" },
54 { 0x000d, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85 D/K NICAM FM (HDEV3)" },
55 { 0x0020, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5 M BTSC-Stereo" },
56 { 0x0021, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5 M BTSC-Mono + SAP" },
57 { 0x0030, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5 M EIA-J Japan Stereo" },
58 { 0x0040, MSP_CARRIER(10.7), MSP_CARRIER(10.7), "10.7 FM-Stereo Radio" },
59 { 0x0050, MSP_CARRIER(6.5), MSP_CARRIER(6.5), "6.5 SAT-Mono" },
60 { 0x0051, MSP_CARRIER(7.02), MSP_CARRIER(7.20), "7.02/7.20 SAT-Stereo" },
61 { 0x0060, MSP_CARRIER(7.2), MSP_CARRIER(7.2), "7.2 SAT ADR" },
62 { -1, 0, 0, NULL
}, /* EOF */
65 static struct msp3400c_init_data_dem
{
74 } msp3400c_init_data
[] = {
75 { /* AM (for carrier detect / msp3400) */
76 {75, 19, 36, 35, 39, 40},
77 {75, 19, 36, 35, 39, 40},
78 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
79 0x00d0, 0x0500, 0x0020, 0x3000
80 }, { /* AM (for carrier detect / msp3410) */
81 {-1, -1, -8, 2, 59, 126},
82 {-1, -1, -8, 2, 59, 126},
83 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
84 0x00d0, 0x0100, 0x0020, 0x3000
86 {-8, -8, 4, 6, 78, 107},
87 {-8, -8, 4, 6, 78, 107},
88 MSP_CARRIER(10.7), MSP_CARRIER(10.7),
89 0x00d0, 0x0480, 0x0020, 0x3000
90 }, { /* Terrestial FM-mono + FM-stereo */
91 {3, 18, 27, 48, 66, 72},
92 {3, 18, 27, 48, 66, 72},
93 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
94 0x00d0, 0x0480, 0x0030, 0x3000
95 }, { /* Sat FM-mono */
96 { 1, 9, 14, 24, 33, 37},
97 { 3, 18, 27, 48, 66, 72},
98 MSP_CARRIER(6.5), MSP_CARRIER(6.5),
99 0x00c6, 0x0480, 0x0000, 0x3000
100 }, { /* NICAM/FM -- B/G (5.5/5.85), D/K (6.5/5.85) */
101 {-2, -8, -10, 10, 50, 86},
102 {3, 18, 27, 48, 66, 72},
103 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
104 0x00d0, 0x0040, 0x0120, 0x3000
105 }, { /* NICAM/FM -- I (6.0/6.552) */
106 {2, 4, -6, -4, 40, 94},
107 {3, 18, 27, 48, 66, 72},
108 MSP_CARRIER(6.0), MSP_CARRIER(6.0),
109 0x00d0, 0x0040, 0x0120, 0x3000
110 }, { /* NICAM/AM -- L (6.5/5.85) */
111 {-2, -8, -10, 10, 50, 86},
112 {-4, -12, -9, 23, 79, 126},
113 MSP_CARRIER(6.5), MSP_CARRIER(6.5),
114 0x00c6, 0x0140, 0x0120, 0x7c00
118 struct msp3400c_carrier_detect
{
123 static struct msp3400c_carrier_detect msp3400c_carrier_detect_main
[] = {
125 { MSP_CARRIER(4.5), "4.5 NTSC" },
126 { MSP_CARRIER(5.5), "5.5 PAL B/G" },
127 { MSP_CARRIER(6.0), "6.0 PAL I" },
128 { MSP_CARRIER(6.5), "6.5 PAL D/K + SAT + SECAM" }
131 static struct msp3400c_carrier_detect msp3400c_carrier_detect_55
[] = {
133 { MSP_CARRIER(5.7421875), "5.742 PAL B/G FM-stereo" },
134 { MSP_CARRIER(5.85), "5.85 PAL B/G NICAM" }
137 static struct msp3400c_carrier_detect msp3400c_carrier_detect_65
[] = {
138 /* PAL SAT / SECAM */
139 { MSP_CARRIER(5.85), "5.85 PAL D/K + SECAM NICAM" },
140 { MSP_CARRIER(6.2578125), "6.25 PAL D/K1 FM-stereo" },
141 { MSP_CARRIER(6.7421875), "6.74 PAL D/K2 FM-stereo" },
142 { MSP_CARRIER(7.02), "7.02 PAL SAT FM-stereo s/b" },
143 { MSP_CARRIER(7.20), "7.20 PAL SAT FM-stereo s" },
144 { MSP_CARRIER(7.38), "7.38 PAL SAT FM-stereo b" },
147 /* ------------------------------------------------------------------------ */
149 const char *msp_standard_std_name(int std
)
153 for (i
= 0; msp_stdlist
[i
].name
!= NULL
; i
++)
154 if (msp_stdlist
[i
].retval
== std
)
155 return msp_stdlist
[i
].name
;
159 static void msp_set_source(struct i2c_client
*client
, u16 src
)
161 struct msp_state
*state
= to_state(i2c_get_clientdata(client
));
164 msp_write_dsp(client
, 0x0008, 0x0520); /* I2S1 */
165 msp_write_dsp(client
, 0x0009, 0x0620); /* I2S2 */
167 msp_write_dsp(client
, 0x0008, src
);
168 msp_write_dsp(client
, 0x0009, src
);
170 msp_write_dsp(client
, 0x000a, src
);
171 msp_write_dsp(client
, 0x000b, src
);
172 msp_write_dsp(client
, 0x000c, src
);
173 if (state
->has_scart2_out
)
174 msp_write_dsp(client
, 0x0041, src
);
177 void msp3400c_set_carrier(struct i2c_client
*client
, int cdo1
, int cdo2
)
179 msp_write_dem(client
, 0x0093, cdo1
& 0xfff);
180 msp_write_dem(client
, 0x009b, cdo1
>> 12);
181 msp_write_dem(client
, 0x00a3, cdo2
& 0xfff);
182 msp_write_dem(client
, 0x00ab, cdo2
>> 12);
183 msp_write_dem(client
, 0x0056, 0); /* LOAD_REG_1/2 */
186 void msp3400c_set_mode(struct i2c_client
*client
, int mode
)
188 struct msp_state
*state
= to_state(i2c_get_clientdata(client
));
189 struct msp3400c_init_data_dem
*data
= &msp3400c_init_data
[mode
];
190 int tuner
= (state
->route_in
>> 3) & 1;
193 v4l_dbg(1, msp_debug
, client
, "set_mode: %d\n", mode
);
195 state
->rxsubchans
= V4L2_TUNER_SUB_MONO
;
197 msp_write_dem(client
, 0x00bb, data
->ad_cv
| (tuner
? 0x100 : 0));
199 for (i
= 5; i
>= 0; i
--) /* fir 1 */
200 msp_write_dem(client
, 0x0001, data
->fir1
[i
]);
202 msp_write_dem(client
, 0x0005, 0x0004); /* fir 2 */
203 msp_write_dem(client
, 0x0005, 0x0040);
204 msp_write_dem(client
, 0x0005, 0x0000);
205 for (i
= 5; i
>= 0; i
--)
206 msp_write_dem(client
, 0x0005, data
->fir2
[i
]);
208 msp_write_dem(client
, 0x0083, data
->mode_reg
);
210 msp3400c_set_carrier(client
, data
->cdo1
, data
->cdo2
);
212 msp_set_source(client
, data
->dsp_src
);
215 /* volume prescale for SCART (AM mono input) */
216 msp_write_dsp(client
, 0x000d, 0x1900);
217 msp_write_dsp(client
, 0x000e, data
->dsp_matrix
);
218 if (state
->has_nicam
) /* nicam prescale */
219 msp_write_dsp(client
, 0x0010, 0x5a00);
222 /* Set audio mode. Note that the pre-'G' models do not support BTSC+SAP,
223 nor do they support stereo BTSC. */
224 static void msp3400c_set_audmode(struct i2c_client
*client
)
226 static char *strmode
[] = {
227 "mono", "stereo", "lang2", "lang1", "lang1+lang2"
229 struct msp_state
*state
= to_state(i2c_get_clientdata(client
));
230 char *modestr
= (state
->audmode
>= 0 && state
->audmode
< 5) ?
231 strmode
[state
->audmode
] : "unknown";
232 int src
= 0; /* channel source: FM/AM, nicam or SCART */
233 int audmode
= state
->audmode
;
235 if (state
->opmode
== OPMODE_AUTOSELECT
) {
236 /* this method would break everything, let's make sure
239 v4l_dbg(1, msp_debug
, client
,
240 "set_audmode called with mode=%d instead of set_source (ignored)\n",
245 /* Note: for the C and D revs no NTSC stereo + SAP is possible as
246 the hardware does not support SAP. So the rxsubchans combination
247 of STEREO | LANG2 does not occur. */
249 if (state
->mode
!= MSP_MODE_EXTERN
) {
250 /* switch to mono if only mono is available */
251 if (state
->rxsubchans
== V4L2_TUNER_SUB_MONO
)
252 audmode
= V4L2_TUNER_MODE_MONO
;
254 else if (state
->rxsubchans
& V4L2_TUNER_SUB_LANG2
) {
255 /* and mono or stereo, then fallback to lang1 */
256 if (audmode
== V4L2_TUNER_MODE_MONO
||
257 audmode
== V4L2_TUNER_MODE_STEREO
)
258 audmode
= V4L2_TUNER_MODE_LANG1
;
260 /* if stereo, and audmode is not mono, then switch to stereo */
261 else if (audmode
!= V4L2_TUNER_MODE_MONO
)
262 audmode
= V4L2_TUNER_MODE_STEREO
;
265 /* switch demodulator */
266 switch (state
->mode
) {
267 case MSP_MODE_FM_TERRA
:
268 v4l_dbg(1, msp_debug
, client
, "FM set_audmode: %s\n", modestr
);
270 case V4L2_TUNER_MODE_STEREO
:
271 msp_write_dsp(client
, 0x000e, 0x3001);
273 case V4L2_TUNER_MODE_MONO
:
274 case V4L2_TUNER_MODE_LANG1
:
275 case V4L2_TUNER_MODE_LANG2
:
276 case V4L2_TUNER_MODE_LANG1_LANG2
:
277 msp_write_dsp(client
, 0x000e, 0x3000);
281 case MSP_MODE_FM_SAT
:
282 v4l_dbg(1, msp_debug
, client
, "SAT set_audmode: %s\n", modestr
);
284 case V4L2_TUNER_MODE_MONO
:
285 msp3400c_set_carrier(client
, MSP_CARRIER(6.5), MSP_CARRIER(6.5));
287 case V4L2_TUNER_MODE_STEREO
:
288 case V4L2_TUNER_MODE_LANG1_LANG2
:
289 msp3400c_set_carrier(client
, MSP_CARRIER(7.2), MSP_CARRIER(7.02));
291 case V4L2_TUNER_MODE_LANG1
:
292 msp3400c_set_carrier(client
, MSP_CARRIER(7.38), MSP_CARRIER(7.02));
294 case V4L2_TUNER_MODE_LANG2
:
295 msp3400c_set_carrier(client
, MSP_CARRIER(7.38), MSP_CARRIER(7.02));
299 case MSP_MODE_FM_NICAM1
:
300 case MSP_MODE_FM_NICAM2
:
301 case MSP_MODE_AM_NICAM
:
302 v4l_dbg(1, msp_debug
, client
,
303 "NICAM set_audmode: %s\n", modestr
);
305 src
= 0x0100; /* NICAM */
308 v4l_dbg(1, msp_debug
, client
,
309 "BTSC set_audmode: %s\n", modestr
);
311 case MSP_MODE_EXTERN
:
312 v4l_dbg(1, msp_debug
, client
,
313 "extern set_audmode: %s\n", modestr
);
314 src
= 0x0200; /* SCART */
316 case MSP_MODE_FM_RADIO
:
317 v4l_dbg(1, msp_debug
, client
,
318 "FM-Radio set_audmode: %s\n", modestr
);
321 v4l_dbg(1, msp_debug
, client
, "mono set_audmode\n");
326 v4l_dbg(1, msp_debug
, client
, "set audmode %d\n", audmode
);
328 case V4L2_TUNER_MODE_STEREO
:
329 case V4L2_TUNER_MODE_LANG1_LANG2
:
332 case V4L2_TUNER_MODE_MONO
:
333 if (state
->mode
== MSP_MODE_AM_NICAM
) {
334 v4l_dbg(1, msp_debug
, client
, "switching to AM mono\n");
335 /* AM mono decoding is handled by tuner, not MSP chip */
336 /* SCART switching control register */
337 msp_set_scart(client
, SCART_MONO
, 0);
341 if (state
->rxsubchans
& V4L2_TUNER_SUB_STEREO
)
344 case V4L2_TUNER_MODE_LANG1
:
346 case V4L2_TUNER_MODE_LANG2
:
350 v4l_dbg(1, msp_debug
, client
,
351 "set_audmode final source/matrix = 0x%x\n", src
);
353 msp_set_source(client
, src
);
356 static void msp3400c_print_mode(struct i2c_client
*client
)
358 struct msp_state
*state
= to_state(i2c_get_clientdata(client
));
360 if (state
->main
== state
->second
)
361 v4l_dbg(1, msp_debug
, client
,
362 "mono sound carrier: %d.%03d MHz\n",
363 state
->main
/ 910000, (state
->main
/ 910) % 1000);
365 v4l_dbg(1, msp_debug
, client
,
366 "main sound carrier: %d.%03d MHz\n",
367 state
->main
/ 910000, (state
->main
/ 910) % 1000);
368 if (state
->mode
== MSP_MODE_FM_NICAM1
|| state
->mode
== MSP_MODE_FM_NICAM2
)
369 v4l_dbg(1, msp_debug
, client
,
370 "NICAM/FM carrier : %d.%03d MHz\n",
371 state
->second
/ 910000, (state
->second
/910) % 1000);
372 if (state
->mode
== MSP_MODE_AM_NICAM
)
373 v4l_dbg(1, msp_debug
, client
,
374 "NICAM/AM carrier : %d.%03d MHz\n",
375 state
->second
/ 910000, (state
->second
/ 910) % 1000);
376 if (state
->mode
== MSP_MODE_FM_TERRA
&& state
->main
!= state
->second
) {
377 v4l_dbg(1, msp_debug
, client
,
378 "FM-stereo carrier : %d.%03d MHz\n",
379 state
->second
/ 910000, (state
->second
/ 910) % 1000);
383 /* ----------------------------------------------------------------------- */
385 static int msp3400c_detect_stereo(struct i2c_client
*client
)
387 struct msp_state
*state
= to_state(i2c_get_clientdata(client
));
389 int rxsubchans
= state
->rxsubchans
;
390 int newnicam
= state
->nicam_on
;
393 switch (state
->mode
) {
394 case MSP_MODE_FM_TERRA
:
395 val
= msp_read_dsp(client
, 0x18);
398 v4l_dbg(2, msp_debug
, client
,
399 "stereo detect register: %d\n", val
);
401 rxsubchans
= V4L2_TUNER_SUB_STEREO
;
402 } else if (val
< -4096) {
403 rxsubchans
= V4L2_TUNER_SUB_LANG1
| V4L2_TUNER_SUB_LANG2
;
405 rxsubchans
= V4L2_TUNER_SUB_MONO
;
409 case MSP_MODE_FM_NICAM1
:
410 case MSP_MODE_FM_NICAM2
:
411 case MSP_MODE_AM_NICAM
:
412 val
= msp_read_dem(client
, 0x23);
413 v4l_dbg(2, msp_debug
, client
, "nicam sync=%d, mode=%d\n",
414 val
& 1, (val
& 0x1e) >> 1);
418 switch ((val
& 0x1e) >> 1) {
421 rxsubchans
= V4L2_TUNER_SUB_STEREO
;
425 rxsubchans
= V4L2_TUNER_SUB_MONO
;
429 rxsubchans
= V4L2_TUNER_SUB_LANG1
| V4L2_TUNER_SUB_LANG2
;
432 rxsubchans
= V4L2_TUNER_SUB_MONO
;
438 rxsubchans
= V4L2_TUNER_SUB_MONO
;
442 if (rxsubchans
!= state
->rxsubchans
) {
444 v4l_dbg(1, msp_debug
, client
,
445 "watch: rxsubchans %02x => %02x\n",
446 state
->rxsubchans
, rxsubchans
);
447 state
->rxsubchans
= rxsubchans
;
449 if (newnicam
!= state
->nicam_on
) {
451 v4l_dbg(1, msp_debug
, client
, "watch: nicam %d => %d\n",
452 state
->nicam_on
, newnicam
);
453 state
->nicam_on
= newnicam
;
459 * A kernel thread for msp3400 control -- we don't want to block the
460 * in the ioctl while doing the sound carrier & stereo detect
462 /* stereo/multilang monitoring */
463 static void watch_stereo(struct i2c_client
*client
)
465 struct msp_state
*state
= to_state(i2c_get_clientdata(client
));
467 if (msp_detect_stereo(client
))
468 msp_set_audmode(client
);
471 state
->watch_stereo
= 0;
474 int msp3400c_thread(void *data
)
476 struct i2c_client
*client
= data
;
477 struct msp_state
*state
= to_state(i2c_get_clientdata(client
));
478 struct msp3400c_carrier_detect
*cd
;
479 int count
, max1
, max2
, val1
, val2
, val
, i
;
481 v4l_dbg(1, msp_debug
, client
, "msp3400 daemon started\n");
484 v4l_dbg(2, msp_debug
, client
, "msp3400 thread: sleep\n");
485 msp_sleep(state
, -1);
486 v4l_dbg(2, msp_debug
, client
, "msp3400 thread: wakeup\n");
489 v4l_dbg(2, msp_debug
, client
, "thread: restart scan\n");
491 if (kthread_should_stop())
494 if (state
->radio
|| MSP_MODE_EXTERN
== state
->mode
) {
495 /* no carrier scan, just unmute */
496 v4l_dbg(1, msp_debug
, client
,
497 "thread: no carrier scan\n");
498 state
->scan_in_progress
= 0;
499 msp_update_volume(state
);
504 state
->scan_in_progress
= 1;
505 msp_update_volume(state
);
507 msp3400c_set_mode(client
, MSP_MODE_AM_DETECT
);
510 state
->watch_stereo
= 0;
513 /* wait for tuner to settle down after a channel change */
514 if (msp_sleep(state
, 200))
517 /* carrier detect pass #1 -- main carrier */
518 cd
= msp3400c_carrier_detect_main
;
519 count
= ARRAY_SIZE(msp3400c_carrier_detect_main
);
521 if (msp_amsound
&& (state
->v4l2_std
& V4L2_STD_SECAM
)) {
522 /* autodetect doesn't work well with AM ... */
525 v4l_dbg(1, msp_debug
, client
, "AM sound override\n");
528 for (i
= 0; i
< count
; i
++) {
529 msp3400c_set_carrier(client
, cd
[i
].cdo
, cd
[i
].cdo
);
530 if (msp_sleep(state
, 100))
532 val
= msp_read_dsp(client
, 0x1b);
536 val1
= val
, max1
= i
;
537 v4l_dbg(1, msp_debug
, client
,
538 "carrier1 val: %5d / %s\n", val
, cd
[i
].name
);
541 /* carrier detect pass #2 -- second (stereo) carrier */
544 cd
= msp3400c_carrier_detect_55
;
545 count
= ARRAY_SIZE(msp3400c_carrier_detect_55
);
548 cd
= msp3400c_carrier_detect_65
;
549 count
= ARRAY_SIZE(msp3400c_carrier_detect_65
);
559 if (msp_amsound
&& (state
->v4l2_std
& V4L2_STD_SECAM
)) {
560 /* autodetect doesn't work well with AM ... */
565 for (i
= 0; i
< count
; i
++) {
566 msp3400c_set_carrier(client
, cd
[i
].cdo
, cd
[i
].cdo
);
567 if (msp_sleep(state
, 100))
569 val
= msp_read_dsp(client
, 0x1b);
573 val2
= val
, max2
= i
;
574 v4l_dbg(1, msp_debug
, client
,
575 "carrier2 val: %5d / %s\n", val
, cd
[i
].name
);
578 /* program the msp3400 according to the results */
579 state
->main
= msp3400c_carrier_detect_main
[max1
].cdo
;
584 state
->second
= msp3400c_carrier_detect_55
[max2
].cdo
;
585 msp3400c_set_mode(client
, MSP_MODE_FM_TERRA
);
586 state
->watch_stereo
= 1;
587 } else if (max2
== 1 && state
->has_nicam
) {
589 state
->second
= msp3400c_carrier_detect_55
[max2
].cdo
;
590 msp3400c_set_mode(client
, MSP_MODE_FM_NICAM1
);
592 state
->watch_stereo
= 1;
599 state
->second
= MSP_CARRIER(6.552);
600 msp3400c_set_mode(client
, MSP_MODE_FM_NICAM2
);
602 state
->watch_stereo
= 1;
605 if (max2
== 1 || max2
== 2) {
607 state
->second
= msp3400c_carrier_detect_65
[max2
].cdo
;
608 msp3400c_set_mode(client
, MSP_MODE_FM_TERRA
);
609 state
->watch_stereo
= 1;
610 } else if (max2
== 0 && (state
->v4l2_std
& V4L2_STD_SECAM
)) {
611 /* L NICAM or AM-mono */
612 state
->second
= msp3400c_carrier_detect_65
[max2
].cdo
;
613 msp3400c_set_mode(client
, MSP_MODE_AM_NICAM
);
614 state
->watch_stereo
= 1;
615 } else if (max2
== 0 && state
->has_nicam
) {
617 state
->second
= msp3400c_carrier_detect_65
[max2
].cdo
;
618 msp3400c_set_mode(client
, MSP_MODE_FM_NICAM1
);
620 state
->watch_stereo
= 1;
628 state
->second
= msp3400c_carrier_detect_main
[max1
].cdo
;
629 msp3400c_set_mode(client
, MSP_MODE_FM_TERRA
);
632 msp3400c_set_carrier(client
, state
->second
, state
->main
);
635 state
->scan_in_progress
= 0;
636 msp3400c_set_audmode(client
);
637 msp_update_volume(state
);
640 msp3400c_print_mode(client
);
642 /* monitor tv audio mode, the first time don't wait
643 so long to get a quick stereo/bilingual result */
645 while (state
->watch_stereo
) {
646 if (msp_sleep(state
, count
? 1000 : 5000))
650 watch_stereo(client
);
653 v4l_dbg(1, msp_debug
, client
, "thread: exit\n");
658 int msp3410d_thread(void *data
)
660 struct i2c_client
*client
= data
;
661 struct msp_state
*state
= to_state(i2c_get_clientdata(client
));
662 int val
, i
, std
, count
;
664 v4l_dbg(1, msp_debug
, client
, "msp3410 daemon started\n");
667 v4l_dbg(2, msp_debug
, client
, "msp3410 thread: sleep\n");
668 msp_sleep(state
, -1);
669 v4l_dbg(2, msp_debug
, client
, "msp3410 thread: wakeup\n");
672 v4l_dbg(2, msp_debug
, client
, "thread: restart scan\n");
674 if (kthread_should_stop())
677 if (state
->mode
== MSP_MODE_EXTERN
) {
678 /* no carrier scan needed, just unmute */
679 v4l_dbg(1, msp_debug
, client
,
680 "thread: no carrier scan\n");
681 state
->scan_in_progress
= 0;
682 msp_update_volume(state
);
687 state
->scan_in_progress
= 1;
688 msp_update_volume(state
);
690 /* start autodetect. Note: autodetect is not supported for
691 NTSC-M and radio, hence we force the standard in those
696 std
= (state
->v4l2_std
& V4L2_STD_NTSC
) ? 0x20 : 1;
697 state
->watch_stereo
= 0;
700 /* wait for tuner to settle down after a channel change */
701 if (msp_sleep(state
, 200))
705 v4l_dbg(2, msp_debug
, client
,
706 "setting standard: %s (0x%04x)\n",
707 msp_standard_std_name(std
), std
);
710 /* programmed some specific mode */
713 /* triggered autodetect */
714 msp_write_dem(client
, 0x20, std
);
716 if (msp_sleep(state
, 100))
720 val
= msp_read_dem(client
, 0x7e);
723 v4l_dbg(2, msp_debug
, client
,
724 "detection still in progress\n");
727 for (i
= 0; msp_stdlist
[i
].name
!= NULL
; i
++)
728 if (msp_stdlist
[i
].retval
== val
)
730 v4l_dbg(1, msp_debug
, client
, "current standard: %s (0x%04x)\n",
731 msp_standard_std_name(val
), val
);
732 state
->main
= msp_stdlist
[i
].main
;
733 state
->second
= msp_stdlist
[i
].second
;
735 state
->rxsubchans
= V4L2_TUNER_SUB_MONO
;
737 if (msp_amsound
&& !state
->radio
&&
738 (state
->v4l2_std
& V4L2_STD_SECAM
) && (val
!= 0x0009)) {
739 /* autodetection has failed, let backup */
740 v4l_dbg(1, msp_debug
, client
, "autodetection failed,"
741 " switching to backup standard: %s (0x%04x)\n",
742 msp_stdlist
[8].name
?
743 msp_stdlist
[8].name
: "unknown", val
);
744 state
->std
= val
= 0x0009;
745 msp_write_dem(client
, 0x20, val
);
750 case 0x0008: /* B/G NICAM */
751 case 0x000a: /* I NICAM */
752 case 0x000b: /* D/K NICAM */
754 state
->mode
= MSP_MODE_FM_NICAM2
;
756 state
->mode
= MSP_MODE_FM_NICAM1
;
757 /* just turn on stereo */
759 state
->watch_stereo
= 1;
762 state
->mode
= MSP_MODE_AM_NICAM
;
764 state
->watch_stereo
= 1;
766 case 0x0020: /* BTSC */
767 /* The pre-'G' models only have BTSC-mono */
768 state
->mode
= MSP_MODE_BTSC
;
770 case 0x0040: /* FM radio */
771 state
->mode
= MSP_MODE_FM_RADIO
;
772 state
->rxsubchans
= V4L2_TUNER_SUB_STEREO
;
773 /* not needed in theory if we have radio, but
774 short programming enables carrier mute */
775 msp3400c_set_mode(client
, MSP_MODE_FM_RADIO
);
776 msp3400c_set_carrier(client
, MSP_CARRIER(10.7),
783 state
->mode
= MSP_MODE_FM_TERRA
;
784 state
->watch_stereo
= 1;
788 /* set various prescales */
789 msp_write_dsp(client
, 0x0d, 0x1900); /* scart */
790 msp_write_dsp(client
, 0x0e, 0x3000); /* FM */
791 if (state
->has_nicam
)
792 msp_write_dsp(client
, 0x10, 0x5a00); /* nicam */
794 if (state
->has_i2s_conf
)
795 msp_write_dem(client
, 0x40, state
->i2s_mode
);
798 msp3400c_set_audmode(client
);
799 state
->scan_in_progress
= 0;
800 msp_update_volume(state
);
802 /* monitor tv audio mode, the first time don't wait
803 so long to get a quick stereo/bilingual result */
805 while (state
->watch_stereo
) {
806 if (msp_sleep(state
, count
? 1000 : 5000))
810 watch_stereo(client
);
813 v4l_dbg(1, msp_debug
, client
, "thread: exit\n");
817 /* ----------------------------------------------------------------------- */
819 /* msp34xxG + (autoselect no-thread)
820 * this one uses both automatic standard detection and automatic sound
821 * select which are available in the newer G versions
822 * struct msp: only norm, acb and source are really used in this mode
825 static int msp34xxg_modus(struct i2c_client
*client
)
827 struct msp_state
*state
= to_state(i2c_get_clientdata(client
));
830 v4l_dbg(1, msp_debug
, client
, "selected radio modus\n");
833 if (state
->v4l2_std
== V4L2_STD_NTSC_M_JP
) {
834 v4l_dbg(1, msp_debug
, client
, "selected M (EIA-J) modus\n");
837 if (state
->v4l2_std
== V4L2_STD_NTSC_M_KR
) {
838 v4l_dbg(1, msp_debug
, client
, "selected M (A2) modus\n");
841 if (state
->v4l2_std
== V4L2_STD_SECAM_L
) {
842 v4l_dbg(1, msp_debug
, client
, "selected SECAM-L modus\n");
845 if (state
->v4l2_std
& V4L2_STD_MN
) {
846 v4l_dbg(1, msp_debug
, client
, "selected M (BTSC) modus\n");
852 static void msp34xxg_set_source(struct i2c_client
*client
, u16 reg
, int in
)
854 struct msp_state
*state
= to_state(i2c_get_clientdata(client
));
857 switch (state
->audmode
) {
858 case V4L2_TUNER_MODE_MONO
:
859 source
= 0; /* mono only */
862 case V4L2_TUNER_MODE_LANG2
:
863 source
= 4; /* stereo or B */
866 case V4L2_TUNER_MODE_LANG1_LANG2
:
867 source
= 1; /* stereo or A|B */
870 case V4L2_TUNER_MODE_LANG1
:
871 source
= 3; /* stereo or A */
874 case V4L2_TUNER_MODE_STEREO
:
876 source
= 3; /* stereo or A */
881 if (in
== MSP_DSP_IN_TUNER
)
882 source
= (source
<< 8) | 0x20;
883 /* the msp34x2g puts the MAIN_AVC, MAIN and AUX sources in 12, 13, 14
884 instead of 11, 12, 13. So we add one for that msp version. */
885 else if (in
>= MSP_DSP_IN_MAIN_AVC
&& state
->has_dolby_pro_logic
)
886 source
= ((in
+ 1) << 8) | matrix
;
888 source
= (in
<< 8) | matrix
;
890 v4l_dbg(1, msp_debug
, client
,
891 "set source to %d (0x%x) for output %02x\n", in
, source
, reg
);
892 msp_write_dsp(client
, reg
, source
);
895 static void msp34xxg_set_sources(struct i2c_client
*client
)
897 struct msp_state
*state
= to_state(i2c_get_clientdata(client
));
898 u32 in
= state
->route_in
;
900 msp34xxg_set_source(client
, 0x0008, (in
>> 4) & 0xf);
901 /* quasi-peak detector is set to same input as the loudspeaker (MAIN) */
902 msp34xxg_set_source(client
, 0x000c, (in
>> 4) & 0xf);
903 msp34xxg_set_source(client
, 0x0009, (in
>> 8) & 0xf);
904 msp34xxg_set_source(client
, 0x000a, (in
>> 12) & 0xf);
905 if (state
->has_scart2_out
)
906 msp34xxg_set_source(client
, 0x0041, (in
>> 16) & 0xf);
907 msp34xxg_set_source(client
, 0x000b, (in
>> 20) & 0xf);
910 /* (re-)initialize the msp34xxg */
911 static void msp34xxg_reset(struct i2c_client
*client
)
913 struct msp_state
*state
= to_state(i2c_get_clientdata(client
));
914 int tuner
= (state
->route_in
>> 3) & 1;
917 /* initialize std to 1 (autodetect) to signal that no standard is
923 if (state
->has_i2s_conf
)
924 msp_write_dem(client
, 0x40, state
->i2s_mode
);
926 /* step-by-step initialisation, as described in the manual */
927 modus
= msp34xxg_modus(client
);
928 modus
|= tuner
? 0x100 : 0;
929 msp_write_dem(client
, 0x30, modus
);
931 /* write the dsps that may have an influence on
932 standard/audio autodetection right now */
933 msp34xxg_set_sources(client
);
935 msp_write_dsp(client
, 0x0d, 0x1900); /* scart */
936 msp_write_dsp(client
, 0x0e, 0x3000); /* FM */
937 if (state
->has_nicam
)
938 msp_write_dsp(client
, 0x10, 0x5a00); /* nicam */
940 /* set identification threshold. Personally, I
941 * I set it to a higher value than the default
942 * of 0x190 to ignore noisy stereo signals.
943 * this needs tuning. (recommended range 0x00a0-0x03c0)
944 * 0x7f0 = forced mono mode
946 * a2 threshold for stereo/bilingual.
947 * Note: this register is part of the Manual/Compatibility mode.
948 * It is supported by all 'G'-family chips.
950 msp_write_dem(client
, 0x22, msp_stereo_thresh
);
953 int msp34xxg_thread(void *data
)
955 struct i2c_client
*client
= data
;
956 struct msp_state
*state
= to_state(i2c_get_clientdata(client
));
959 v4l_dbg(1, msp_debug
, client
, "msp34xxg daemon started\n");
962 v4l_dbg(2, msp_debug
, client
, "msp34xxg thread: sleep\n");
963 msp_sleep(state
, -1);
964 v4l_dbg(2, msp_debug
, client
, "msp34xxg thread: wakeup\n");
967 v4l_dbg(1, msp_debug
, client
, "thread: restart scan\n");
969 if (kthread_should_stop())
972 if (state
->mode
== MSP_MODE_EXTERN
) {
973 /* no carrier scan needed, just unmute */
974 v4l_dbg(1, msp_debug
, client
,
975 "thread: no carrier scan\n");
976 state
->scan_in_progress
= 0;
977 msp_update_volume(state
);
982 msp34xxg_reset(client
);
983 state
->std
= state
->radio
? 0x40 :
984 (state
->force_btsc
&& msp_standard
== 1) ? 32 : msp_standard
;
985 msp_write_dem(client
, 0x20, state
->std
);
986 /* start autodetect */
990 /* watch autodetect */
991 v4l_dbg(1, msp_debug
, client
,
992 "started autodetect, waiting for result\n");
993 for (i
= 0; i
< 10; i
++) {
994 if (msp_sleep(state
, 100))
998 val
= msp_read_dem(client
, 0x7e);
1003 v4l_dbg(2, msp_debug
, client
,
1004 "detection still in progress\n");
1006 if (state
->std
== 1) {
1007 v4l_dbg(1, msp_debug
, client
,
1008 "detection still in progress after 10 tries. giving up.\n");
1013 v4l_dbg(1, msp_debug
, client
,
1014 "detected standard: %s (0x%04x)\n",
1015 msp_standard_std_name(state
->std
), state
->std
);
1017 if (state
->std
== 9) {
1019 msp_write_dsp(client
, 0x0e, 0x7c00);
1022 /* unmute: dispatch sound to scart output, set scart volume */
1023 msp_update_volume(state
);
1026 if (msp_write_dsp(client
, 0x13, state
->acb
))
1029 /* the periodic stereo/SAP check is only relevant for
1030 the 0x20 standard (BTSC) */
1031 if (state
->std
!= 0x20)
1034 state
->watch_stereo
= 1;
1036 /* monitor tv audio mode, the first time don't wait
1037 in order to get a quick stereo/SAP update */
1038 watch_stereo(client
);
1039 while (state
->watch_stereo
) {
1040 watch_stereo(client
);
1041 if (msp_sleep(state
, 5000))
1045 v4l_dbg(1, msp_debug
, client
, "thread: exit\n");
1049 static int msp34xxg_detect_stereo(struct i2c_client
*client
)
1051 struct msp_state
*state
= to_state(i2c_get_clientdata(client
));
1052 int status
= msp_read_dem(client
, 0x0200);
1053 int is_bilingual
= status
& 0x100;
1054 int is_stereo
= status
& 0x40;
1055 int oldrx
= state
->rxsubchans
;
1057 if (state
->mode
== MSP_MODE_EXTERN
)
1060 state
->rxsubchans
= 0;
1062 state
->rxsubchans
= V4L2_TUNER_SUB_STEREO
;
1064 state
->rxsubchans
= V4L2_TUNER_SUB_MONO
;
1066 if (state
->std
== 0x20)
1067 state
->rxsubchans
|= V4L2_TUNER_SUB_SAP
;
1070 V4L2_TUNER_SUB_LANG1
| V4L2_TUNER_SUB_LANG2
;
1072 v4l_dbg(1, msp_debug
, client
,
1073 "status=0x%x, stereo=%d, bilingual=%d -> rxsubchans=%d\n",
1074 status
, is_stereo
, is_bilingual
, state
->rxsubchans
);
1075 return (oldrx
!= state
->rxsubchans
);
1078 static void msp34xxg_set_audmode(struct i2c_client
*client
)
1080 struct msp_state
*state
= to_state(i2c_get_clientdata(client
));
1082 if (state
->std
== 0x20) {
1083 if ((state
->rxsubchans
& V4L2_TUNER_SUB_SAP
) &&
1084 (state
->audmode
== V4L2_TUNER_MODE_LANG1_LANG2
||
1085 state
->audmode
== V4L2_TUNER_MODE_LANG2
)) {
1086 msp_write_dem(client
, 0x20, 0x21);
1088 msp_write_dem(client
, 0x20, 0x20);
1092 msp34xxg_set_sources(client
);
1095 void msp_set_audmode(struct i2c_client
*client
)
1097 struct msp_state
*state
= to_state(i2c_get_clientdata(client
));
1099 switch (state
->opmode
) {
1101 case OPMODE_AUTODETECT
:
1102 msp3400c_set_audmode(client
);
1104 case OPMODE_AUTOSELECT
:
1105 msp34xxg_set_audmode(client
);
1110 int msp_detect_stereo(struct i2c_client
*client
)
1112 struct msp_state
*state
= to_state(i2c_get_clientdata(client
));
1114 switch (state
->opmode
) {
1116 case OPMODE_AUTODETECT
:
1117 return msp3400c_detect_stereo(client
);
1118 case OPMODE_AUTOSELECT
:
1119 return msp34xxg_detect_stereo(client
);