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.
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/i2c.h>
21 #include <linux/freezer.h>
22 #include <linux/videodev2.h>
23 #include <media/v4l2-common.h>
24 #include <media/drv-intf/msp3400.h>
25 #include <linux/kthread.h>
26 #include <linux/suspend.h>
27 #include "msp3400-driver.h"
29 /* this one uses the automatic sound standard detection of newer msp34xx
37 { 0x0000, 0, 0, "could not detect sound standard", V4L2_STD_ALL
},
38 { 0x0001, 0, 0, "autodetect start", V4L2_STD_ALL
},
39 { 0x0002, MSP_CARRIER(4.5), MSP_CARRIER(4.72),
40 "4.5/4.72 M Dual FM-Stereo", V4L2_STD_MN
},
41 { 0x0003, MSP_CARRIER(5.5), MSP_CARRIER(5.7421875),
42 "5.5/5.74 B/G Dual FM-Stereo", V4L2_STD_BG
},
43 { 0x0004, MSP_CARRIER(6.5), MSP_CARRIER(6.2578125),
44 "6.5/6.25 D/K1 Dual FM-Stereo", V4L2_STD_DK
},
45 { 0x0005, MSP_CARRIER(6.5), MSP_CARRIER(6.7421875),
46 "6.5/6.74 D/K2 Dual FM-Stereo", V4L2_STD_DK
},
47 { 0x0006, MSP_CARRIER(6.5), MSP_CARRIER(6.5),
48 "6.5 D/K FM-Mono (HDEV3)", V4L2_STD_DK
},
49 { 0x0007, MSP_CARRIER(6.5), MSP_CARRIER(5.7421875),
50 "6.5/5.74 D/K3 Dual FM-Stereo", V4L2_STD_DK
},
51 { 0x0008, MSP_CARRIER(5.5), MSP_CARRIER(5.85),
52 "5.5/5.85 B/G NICAM FM", V4L2_STD_BG
},
53 { 0x0009, MSP_CARRIER(6.5), MSP_CARRIER(5.85),
54 "6.5/5.85 L NICAM AM", V4L2_STD_L
},
55 { 0x000a, MSP_CARRIER(6.0), MSP_CARRIER(6.55),
56 "6.0/6.55 I NICAM FM", V4L2_STD_PAL_I
},
57 { 0x000b, MSP_CARRIER(6.5), MSP_CARRIER(5.85),
58 "6.5/5.85 D/K NICAM FM", V4L2_STD_DK
},
59 { 0x000c, MSP_CARRIER(6.5), MSP_CARRIER(5.85),
60 "6.5/5.85 D/K NICAM FM (HDEV2)", V4L2_STD_DK
},
61 { 0x000d, MSP_CARRIER(6.5), MSP_CARRIER(5.85),
62 "6.5/5.85 D/K NICAM FM (HDEV3)", V4L2_STD_DK
},
63 { 0x0020, MSP_CARRIER(4.5), MSP_CARRIER(4.5),
64 "4.5 M BTSC-Stereo", V4L2_STD_MTS
},
65 { 0x0021, MSP_CARRIER(4.5), MSP_CARRIER(4.5),
66 "4.5 M BTSC-Mono + SAP", V4L2_STD_MTS
},
67 { 0x0030, MSP_CARRIER(4.5), MSP_CARRIER(4.5),
68 "4.5 M EIA-J Japan Stereo", V4L2_STD_NTSC_M_JP
},
69 { 0x0040, MSP_CARRIER(10.7), MSP_CARRIER(10.7),
70 "10.7 FM-Stereo Radio", V4L2_STD_ALL
},
71 { 0x0050, MSP_CARRIER(6.5), MSP_CARRIER(6.5),
72 "6.5 SAT-Mono", V4L2_STD_ALL
},
73 { 0x0051, MSP_CARRIER(7.02), MSP_CARRIER(7.20),
74 "7.02/7.20 SAT-Stereo", V4L2_STD_ALL
},
75 { 0x0060, MSP_CARRIER(7.2), MSP_CARRIER(7.2),
76 "7.2 SAT ADR", V4L2_STD_ALL
},
77 { -1, 0, 0, NULL
, 0 }, /* EOF */
80 static struct msp3400c_init_data_dem
{
89 } msp3400c_init_data
[] = {
90 { /* AM (for carrier detect / msp3400) */
91 {75, 19, 36, 35, 39, 40},
92 {75, 19, 36, 35, 39, 40},
93 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
94 0x00d0, 0x0500, 0x0020, 0x3000
95 }, { /* AM (for carrier detect / msp3410) */
96 {-1, -1, -8, 2, 59, 126},
97 {-1, -1, -8, 2, 59, 126},
98 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
99 0x00d0, 0x0100, 0x0020, 0x3000
101 {-8, -8, 4, 6, 78, 107},
102 {-8, -8, 4, 6, 78, 107},
103 MSP_CARRIER(10.7), MSP_CARRIER(10.7),
104 0x00d0, 0x0480, 0x0020, 0x3000
105 }, { /* Terrestrial FM-mono + FM-stereo */
106 {3, 18, 27, 48, 66, 72},
107 {3, 18, 27, 48, 66, 72},
108 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
109 0x00d0, 0x0480, 0x0030, 0x3000
110 }, { /* Sat FM-mono */
111 { 1, 9, 14, 24, 33, 37},
112 { 3, 18, 27, 48, 66, 72},
113 MSP_CARRIER(6.5), MSP_CARRIER(6.5),
114 0x00c6, 0x0480, 0x0000, 0x3000
115 }, { /* NICAM/FM -- B/G (5.5/5.85), D/K (6.5/5.85) */
116 {-2, -8, -10, 10, 50, 86},
117 {3, 18, 27, 48, 66, 72},
118 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
119 0x00d0, 0x0040, 0x0120, 0x3000
120 }, { /* NICAM/FM -- I (6.0/6.552) */
121 {2, 4, -6, -4, 40, 94},
122 {3, 18, 27, 48, 66, 72},
123 MSP_CARRIER(6.0), MSP_CARRIER(6.0),
124 0x00d0, 0x0040, 0x0120, 0x3000
125 }, { /* NICAM/AM -- L (6.5/5.85) */
126 {-2, -8, -10, 10, 50, 86},
127 {-4, -12, -9, 23, 79, 126},
128 MSP_CARRIER(6.5), MSP_CARRIER(6.5),
129 0x00c6, 0x0140, 0x0120, 0x7c00
133 struct msp3400c_carrier_detect
{
138 static struct msp3400c_carrier_detect msp3400c_carrier_detect_main
[] = {
140 { MSP_CARRIER(4.5), "4.5 NTSC" },
141 { MSP_CARRIER(5.5), "5.5 PAL B/G" },
142 { MSP_CARRIER(6.0), "6.0 PAL I" },
143 { MSP_CARRIER(6.5), "6.5 PAL D/K + SAT + SECAM" }
146 static struct msp3400c_carrier_detect msp3400c_carrier_detect_55
[] = {
148 { MSP_CARRIER(5.7421875), "5.742 PAL B/G FM-stereo" },
149 { MSP_CARRIER(5.85), "5.85 PAL B/G NICAM" }
152 static struct msp3400c_carrier_detect msp3400c_carrier_detect_65
[] = {
153 /* PAL SAT / SECAM */
154 { MSP_CARRIER(5.85), "5.85 PAL D/K + SECAM NICAM" },
155 { MSP_CARRIER(6.2578125), "6.25 PAL D/K1 FM-stereo" },
156 { MSP_CARRIER(6.7421875), "6.74 PAL D/K2 FM-stereo" },
157 { MSP_CARRIER(7.02), "7.02 PAL SAT FM-stereo s/b" },
158 { MSP_CARRIER(7.20), "7.20 PAL SAT FM-stereo s" },
159 { MSP_CARRIER(7.38), "7.38 PAL SAT FM-stereo b" },
162 /* ------------------------------------------------------------------------ */
164 const char *msp_standard_std_name(int std
)
168 for (i
= 0; msp_stdlist
[i
].name
!= NULL
; i
++)
169 if (msp_stdlist
[i
].retval
== std
)
170 return msp_stdlist
[i
].name
;
174 static v4l2_std_id
msp_standard_std(int std
)
178 for (i
= 0; msp_stdlist
[i
].name
!= NULL
; i
++)
179 if (msp_stdlist
[i
].retval
== std
)
180 return msp_stdlist
[i
].std
;
184 static void msp_set_source(struct i2c_client
*client
, u16 src
)
186 struct msp_state
*state
= to_state(i2c_get_clientdata(client
));
189 msp_write_dsp(client
, 0x0008, 0x0520); /* I2S1 */
190 msp_write_dsp(client
, 0x0009, 0x0620); /* I2S2 */
192 msp_write_dsp(client
, 0x0008, src
);
193 msp_write_dsp(client
, 0x0009, src
);
195 msp_write_dsp(client
, 0x000a, src
);
196 msp_write_dsp(client
, 0x000b, src
);
197 msp_write_dsp(client
, 0x000c, src
);
198 if (state
->has_scart2_out
)
199 msp_write_dsp(client
, 0x0041, src
);
202 void msp3400c_set_carrier(struct i2c_client
*client
, int cdo1
, int cdo2
)
204 msp_write_dem(client
, 0x0093, cdo1
& 0xfff);
205 msp_write_dem(client
, 0x009b, cdo1
>> 12);
206 msp_write_dem(client
, 0x00a3, cdo2
& 0xfff);
207 msp_write_dem(client
, 0x00ab, cdo2
>> 12);
208 msp_write_dem(client
, 0x0056, 0); /* LOAD_REG_1/2 */
211 void msp3400c_set_mode(struct i2c_client
*client
, int mode
)
213 struct msp_state
*state
= to_state(i2c_get_clientdata(client
));
214 struct msp3400c_init_data_dem
*data
= &msp3400c_init_data
[mode
];
215 int tuner
= (state
->route_in
>> 3) & 1;
218 dev_dbg_lvl(&client
->dev
, 1, msp_debug
, "set_mode: %d\n", mode
);
220 state
->rxsubchans
= V4L2_TUNER_SUB_MONO
;
222 msp_write_dem(client
, 0x00bb, data
->ad_cv
| (tuner
? 0x100 : 0));
224 for (i
= 5; i
>= 0; i
--) /* fir 1 */
225 msp_write_dem(client
, 0x0001, data
->fir1
[i
]);
227 msp_write_dem(client
, 0x0005, 0x0004); /* fir 2 */
228 msp_write_dem(client
, 0x0005, 0x0040);
229 msp_write_dem(client
, 0x0005, 0x0000);
230 for (i
= 5; i
>= 0; i
--)
231 msp_write_dem(client
, 0x0005, data
->fir2
[i
]);
233 msp_write_dem(client
, 0x0083, data
->mode_reg
);
235 msp3400c_set_carrier(client
, data
->cdo1
, data
->cdo2
);
237 msp_set_source(client
, data
->dsp_src
);
240 /* volume prescale for SCART (AM mono input) */
241 msp_write_dsp(client
, 0x000d, 0x1900);
242 msp_write_dsp(client
, 0x000e, data
->dsp_matrix
);
243 if (state
->has_nicam
) /* nicam prescale */
244 msp_write_dsp(client
, 0x0010, 0x5a00);
247 /* Set audio mode. Note that the pre-'G' models do not support BTSC+SAP,
248 nor do they support stereo BTSC. */
249 static void msp3400c_set_audmode(struct i2c_client
*client
)
251 static char *strmode
[] = {
252 "mono", "stereo", "lang2", "lang1", "lang1+lang2"
254 struct msp_state
*state
= to_state(i2c_get_clientdata(client
));
255 char *modestr
= (state
->audmode
>= 0 && state
->audmode
< 5) ?
256 strmode
[state
->audmode
] : "unknown";
257 int src
= 0; /* channel source: FM/AM, nicam or SCART */
258 int audmode
= state
->audmode
;
260 if (state
->opmode
== OPMODE_AUTOSELECT
) {
261 /* this method would break everything, let's make sure
264 dev_dbg_lvl(&client
->dev
, 1, msp_debug
,
265 "set_audmode called with mode=%d instead of set_source (ignored)\n",
270 /* Note: for the C and D revs no NTSC stereo + SAP is possible as
271 the hardware does not support SAP. So the rxsubchans combination
272 of STEREO | LANG2 does not occur. */
274 if (state
->mode
!= MSP_MODE_EXTERN
) {
275 /* switch to mono if only mono is available */
276 if (state
->rxsubchans
== V4L2_TUNER_SUB_MONO
)
277 audmode
= V4L2_TUNER_MODE_MONO
;
279 else if (state
->rxsubchans
& V4L2_TUNER_SUB_LANG2
) {
280 /* and mono or stereo, then fallback to lang1 */
281 if (audmode
== V4L2_TUNER_MODE_MONO
||
282 audmode
== V4L2_TUNER_MODE_STEREO
)
283 audmode
= V4L2_TUNER_MODE_LANG1
;
285 /* if stereo, and audmode is not mono, then switch to stereo */
286 else if (audmode
!= V4L2_TUNER_MODE_MONO
)
287 audmode
= V4L2_TUNER_MODE_STEREO
;
290 /* switch demodulator */
291 switch (state
->mode
) {
292 case MSP_MODE_FM_TERRA
:
293 dev_dbg_lvl(&client
->dev
, 1, msp_debug
, "FM set_audmode: %s\n", modestr
);
295 case V4L2_TUNER_MODE_STEREO
:
296 msp_write_dsp(client
, 0x000e, 0x3001);
298 case V4L2_TUNER_MODE_MONO
:
299 case V4L2_TUNER_MODE_LANG1
:
300 case V4L2_TUNER_MODE_LANG2
:
301 case V4L2_TUNER_MODE_LANG1_LANG2
:
302 msp_write_dsp(client
, 0x000e, 0x3000);
306 case MSP_MODE_FM_SAT
:
307 dev_dbg_lvl(&client
->dev
, 1, msp_debug
, "SAT set_audmode: %s\n", modestr
);
309 case V4L2_TUNER_MODE_MONO
:
310 msp3400c_set_carrier(client
, MSP_CARRIER(6.5), MSP_CARRIER(6.5));
312 case V4L2_TUNER_MODE_STEREO
:
313 case V4L2_TUNER_MODE_LANG1_LANG2
:
314 msp3400c_set_carrier(client
, MSP_CARRIER(7.2), MSP_CARRIER(7.02));
316 case V4L2_TUNER_MODE_LANG1
:
317 msp3400c_set_carrier(client
, MSP_CARRIER(7.38), MSP_CARRIER(7.02));
319 case V4L2_TUNER_MODE_LANG2
:
320 msp3400c_set_carrier(client
, MSP_CARRIER(7.38), MSP_CARRIER(7.02));
324 case MSP_MODE_FM_NICAM1
:
325 case MSP_MODE_FM_NICAM2
:
326 case MSP_MODE_AM_NICAM
:
327 dev_dbg_lvl(&client
->dev
, 1, msp_debug
,
328 "NICAM set_audmode: %s\n", modestr
);
330 src
= 0x0100; /* NICAM */
333 dev_dbg_lvl(&client
->dev
, 1, msp_debug
,
334 "BTSC set_audmode: %s\n", modestr
);
336 case MSP_MODE_EXTERN
:
337 dev_dbg_lvl(&client
->dev
, 1, msp_debug
,
338 "extern set_audmode: %s\n", modestr
);
339 src
= 0x0200; /* SCART */
341 case MSP_MODE_FM_RADIO
:
342 dev_dbg_lvl(&client
->dev
, 1, msp_debug
,
343 "FM-Radio set_audmode: %s\n", modestr
);
346 dev_dbg_lvl(&client
->dev
, 1, msp_debug
, "mono set_audmode\n");
351 dev_dbg_lvl(&client
->dev
, 1, msp_debug
, "set audmode %d\n", audmode
);
353 case V4L2_TUNER_MODE_STEREO
:
354 case V4L2_TUNER_MODE_LANG1_LANG2
:
357 case V4L2_TUNER_MODE_MONO
:
358 if (state
->mode
== MSP_MODE_AM_NICAM
) {
359 dev_dbg_lvl(&client
->dev
, 1, msp_debug
, "switching to AM mono\n");
360 /* AM mono decoding is handled by tuner, not MSP chip */
361 /* SCART switching control register */
362 msp_set_scart(client
, SCART_MONO
, 0);
366 if (state
->rxsubchans
& V4L2_TUNER_SUB_STEREO
)
369 case V4L2_TUNER_MODE_LANG1
:
371 case V4L2_TUNER_MODE_LANG2
:
375 dev_dbg_lvl(&client
->dev
, 1, msp_debug
,
376 "set_audmode final source/matrix = 0x%x\n", src
);
378 msp_set_source(client
, src
);
381 static void msp3400c_print_mode(struct i2c_client
*client
)
383 struct msp_state
*state
= to_state(i2c_get_clientdata(client
));
385 if (state
->main
== state
->second
)
386 dev_dbg_lvl(&client
->dev
, 1, msp_debug
,
387 "mono sound carrier: %d.%03d MHz\n",
388 state
->main
/ 910000, (state
->main
/ 910) % 1000);
390 dev_dbg_lvl(&client
->dev
, 1, msp_debug
,
391 "main sound carrier: %d.%03d MHz\n",
392 state
->main
/ 910000, (state
->main
/ 910) % 1000);
393 if (state
->mode
== MSP_MODE_FM_NICAM1
|| state
->mode
== MSP_MODE_FM_NICAM2
)
394 dev_dbg_lvl(&client
->dev
, 1, msp_debug
,
395 "NICAM/FM carrier : %d.%03d MHz\n",
396 state
->second
/ 910000, (state
->second
/910) % 1000);
397 if (state
->mode
== MSP_MODE_AM_NICAM
)
398 dev_dbg_lvl(&client
->dev
, 1, msp_debug
,
399 "NICAM/AM carrier : %d.%03d MHz\n",
400 state
->second
/ 910000, (state
->second
/ 910) % 1000);
401 if (state
->mode
== MSP_MODE_FM_TERRA
&& state
->main
!= state
->second
) {
402 dev_dbg_lvl(&client
->dev
, 1, msp_debug
,
403 "FM-stereo carrier : %d.%03d MHz\n",
404 state
->second
/ 910000, (state
->second
/ 910) % 1000);
408 /* ----------------------------------------------------------------------- */
410 static int msp3400c_detect_stereo(struct i2c_client
*client
)
412 struct msp_state
*state
= to_state(i2c_get_clientdata(client
));
414 int rxsubchans
= state
->rxsubchans
;
415 int newnicam
= state
->nicam_on
;
418 switch (state
->mode
) {
419 case MSP_MODE_FM_TERRA
:
420 val
= msp_read_dsp(client
, 0x18);
423 dev_dbg_lvl(&client
->dev
, 2, msp_debug
,
424 "stereo detect register: %d\n", val
);
426 rxsubchans
= V4L2_TUNER_SUB_STEREO
;
427 } else if (val
< -4096) {
428 rxsubchans
= V4L2_TUNER_SUB_LANG1
| V4L2_TUNER_SUB_LANG2
;
430 rxsubchans
= V4L2_TUNER_SUB_MONO
;
434 case MSP_MODE_FM_NICAM1
:
435 case MSP_MODE_FM_NICAM2
:
436 case MSP_MODE_AM_NICAM
:
437 val
= msp_read_dem(client
, 0x23);
438 dev_dbg_lvl(&client
->dev
, 2, msp_debug
, "nicam sync=%d, mode=%d\n",
439 val
& 1, (val
& 0x1e) >> 1);
443 switch ((val
& 0x1e) >> 1) {
446 rxsubchans
= V4L2_TUNER_SUB_STEREO
;
450 rxsubchans
= V4L2_TUNER_SUB_MONO
;
454 rxsubchans
= V4L2_TUNER_SUB_LANG1
| V4L2_TUNER_SUB_LANG2
;
457 rxsubchans
= V4L2_TUNER_SUB_MONO
;
463 rxsubchans
= V4L2_TUNER_SUB_MONO
;
467 if (rxsubchans
!= state
->rxsubchans
) {
469 dev_dbg_lvl(&client
->dev
, 1, msp_debug
,
470 "watch: rxsubchans %02x => %02x\n",
471 state
->rxsubchans
, rxsubchans
);
472 state
->rxsubchans
= rxsubchans
;
474 if (newnicam
!= state
->nicam_on
) {
476 dev_dbg_lvl(&client
->dev
, 1, msp_debug
, "watch: nicam %d => %d\n",
477 state
->nicam_on
, newnicam
);
478 state
->nicam_on
= newnicam
;
484 * A kernel thread for msp3400 control -- we don't want to block the
485 * in the ioctl while doing the sound carrier & stereo detect
487 /* stereo/multilang monitoring */
488 static void watch_stereo(struct i2c_client
*client
)
490 struct msp_state
*state
= to_state(i2c_get_clientdata(client
));
492 if (msp_detect_stereo(client
))
493 msp_set_audmode(client
);
496 state
->watch_stereo
= 0;
499 int msp3400c_thread(void *data
)
501 struct i2c_client
*client
= data
;
502 struct msp_state
*state
= to_state(i2c_get_clientdata(client
));
503 struct msp3400c_carrier_detect
*cd
;
504 int count
, max1
, max2
, val1
, val2
, val
, i
;
506 dev_dbg_lvl(&client
->dev
, 1, msp_debug
, "msp3400 daemon started\n");
507 state
->detected_std
= V4L2_STD_ALL
;
510 dev_dbg_lvl(&client
->dev
, 2, msp_debug
, "msp3400 thread: sleep\n");
511 msp_sleep(state
, -1);
512 dev_dbg_lvl(&client
->dev
, 2, msp_debug
, "msp3400 thread: wakeup\n");
515 dev_dbg_lvl(&client
->dev
, 2, msp_debug
, "thread: restart scan\n");
517 if (kthread_should_stop())
520 if (state
->radio
|| MSP_MODE_EXTERN
== state
->mode
) {
521 /* no carrier scan, just unmute */
522 dev_dbg_lvl(&client
->dev
, 1, msp_debug
,
523 "thread: no carrier scan\n");
524 state
->scan_in_progress
= 0;
525 msp_update_volume(state
);
530 state
->scan_in_progress
= 1;
531 msp_update_volume(state
);
533 msp3400c_set_mode(client
, MSP_MODE_AM_DETECT
);
536 state
->watch_stereo
= 0;
539 /* wait for tuner to settle down after a channel change */
540 if (msp_sleep(state
, 200))
543 /* carrier detect pass #1 -- main carrier */
544 cd
= msp3400c_carrier_detect_main
;
545 count
= ARRAY_SIZE(msp3400c_carrier_detect_main
);
547 if (msp_amsound
&& (state
->v4l2_std
& V4L2_STD_SECAM
)) {
548 /* autodetect doesn't work well with AM ... */
551 dev_dbg_lvl(&client
->dev
, 1, msp_debug
, "AM sound override\n");
554 for (i
= 0; i
< count
; i
++) {
555 msp3400c_set_carrier(client
, cd
[i
].cdo
, cd
[i
].cdo
);
556 if (msp_sleep(state
, 100))
558 val
= msp_read_dsp(client
, 0x1b);
562 val1
= val
, max1
= i
;
563 dev_dbg_lvl(&client
->dev
, 1, msp_debug
,
564 "carrier1 val: %5d / %s\n", val
, cd
[i
].name
);
567 /* carrier detect pass #2 -- second (stereo) carrier */
570 cd
= msp3400c_carrier_detect_55
;
571 count
= ARRAY_SIZE(msp3400c_carrier_detect_55
);
574 cd
= msp3400c_carrier_detect_65
;
575 count
= ARRAY_SIZE(msp3400c_carrier_detect_65
);
585 if (msp_amsound
&& (state
->v4l2_std
& V4L2_STD_SECAM
)) {
586 /* autodetect doesn't work well with AM ... */
591 for (i
= 0; i
< count
; i
++) {
592 msp3400c_set_carrier(client
, cd
[i
].cdo
, cd
[i
].cdo
);
593 if (msp_sleep(state
, 100))
595 val
= msp_read_dsp(client
, 0x1b);
599 val2
= val
, max2
= i
;
600 dev_dbg_lvl(&client
->dev
, 1, msp_debug
,
601 "carrier2 val: %5d / %s\n", val
, cd
[i
].name
);
604 /* program the msp3400 according to the results */
605 state
->main
= msp3400c_carrier_detect_main
[max1
].cdo
;
608 state
->detected_std
= V4L2_STD_BG
| V4L2_STD_PAL_H
;
611 state
->second
= msp3400c_carrier_detect_55
[max2
].cdo
;
612 msp3400c_set_mode(client
, MSP_MODE_FM_TERRA
);
613 state
->watch_stereo
= 1;
614 } else if (max2
== 1 && state
->has_nicam
) {
616 state
->second
= msp3400c_carrier_detect_55
[max2
].cdo
;
617 msp3400c_set_mode(client
, MSP_MODE_FM_NICAM1
);
619 state
->watch_stereo
= 1;
626 state
->detected_std
= V4L2_STD_PAL_I
;
627 state
->second
= MSP_CARRIER(6.552);
628 msp3400c_set_mode(client
, MSP_MODE_FM_NICAM2
);
630 state
->watch_stereo
= 1;
633 if (max2
== 1 || max2
== 2) {
635 state
->second
= msp3400c_carrier_detect_65
[max2
].cdo
;
636 msp3400c_set_mode(client
, MSP_MODE_FM_TERRA
);
637 state
->watch_stereo
= 1;
638 state
->detected_std
= V4L2_STD_DK
;
639 } else if (max2
== 0 && (state
->v4l2_std
& V4L2_STD_SECAM
)) {
640 /* L NICAM or AM-mono */
641 state
->second
= msp3400c_carrier_detect_65
[max2
].cdo
;
642 msp3400c_set_mode(client
, MSP_MODE_AM_NICAM
);
643 state
->watch_stereo
= 1;
644 state
->detected_std
= V4L2_STD_L
;
645 } else if (max2
== 0 && state
->has_nicam
) {
647 state
->second
= msp3400c_carrier_detect_65
[max2
].cdo
;
648 msp3400c_set_mode(client
, MSP_MODE_FM_NICAM1
);
650 state
->watch_stereo
= 1;
651 state
->detected_std
= V4L2_STD_DK
;
657 state
->detected_std
= V4L2_STD_MN
;
661 state
->second
= msp3400c_carrier_detect_main
[max1
].cdo
;
662 msp3400c_set_mode(client
, MSP_MODE_FM_TERRA
);
665 msp3400c_set_carrier(client
, state
->second
, state
->main
);
668 state
->scan_in_progress
= 0;
669 msp3400c_set_audmode(client
);
670 msp_update_volume(state
);
673 msp3400c_print_mode(client
);
675 /* monitor tv audio mode, the first time don't wait
676 so long to get a quick stereo/bilingual result */
678 while (state
->watch_stereo
) {
679 if (msp_sleep(state
, count
? 1000 : 5000))
683 watch_stereo(client
);
686 dev_dbg_lvl(&client
->dev
, 1, msp_debug
, "thread: exit\n");
691 int msp3410d_thread(void *data
)
693 struct i2c_client
*client
= data
;
694 struct msp_state
*state
= to_state(i2c_get_clientdata(client
));
695 int val
, i
, std
, count
;
697 dev_dbg_lvl(&client
->dev
, 1, msp_debug
, "msp3410 daemon started\n");
698 state
->detected_std
= V4L2_STD_ALL
;
701 dev_dbg_lvl(&client
->dev
, 2, msp_debug
, "msp3410 thread: sleep\n");
702 msp_sleep(state
, -1);
703 dev_dbg_lvl(&client
->dev
, 2, msp_debug
, "msp3410 thread: wakeup\n");
706 dev_dbg_lvl(&client
->dev
, 2, msp_debug
, "thread: restart scan\n");
708 if (kthread_should_stop())
711 if (state
->mode
== MSP_MODE_EXTERN
) {
712 /* no carrier scan needed, just unmute */
713 dev_dbg_lvl(&client
->dev
, 1, msp_debug
,
714 "thread: no carrier scan\n");
715 state
->scan_in_progress
= 0;
716 msp_update_volume(state
);
721 state
->scan_in_progress
= 1;
722 msp_update_volume(state
);
724 /* start autodetect. Note: autodetect is not supported for
725 NTSC-M and radio, hence we force the standard in those
730 std
= (state
->v4l2_std
& V4L2_STD_NTSC
) ? 0x20 : 1;
731 state
->watch_stereo
= 0;
734 /* wait for tuner to settle down after a channel change */
735 if (msp_sleep(state
, 200))
739 dev_dbg_lvl(&client
->dev
, 2, msp_debug
,
740 "setting standard: %s (0x%04x)\n",
741 msp_standard_std_name(std
), std
);
744 /* programmed some specific mode */
747 /* triggered autodetect */
748 msp_write_dem(client
, 0x20, std
);
750 if (msp_sleep(state
, 100))
754 val
= msp_read_dem(client
, 0x7e);
757 dev_dbg_lvl(&client
->dev
, 2, msp_debug
,
758 "detection still in progress\n");
761 for (i
= 0; msp_stdlist
[i
].name
!= NULL
; i
++)
762 if (msp_stdlist
[i
].retval
== val
)
764 dev_dbg_lvl(&client
->dev
, 1, msp_debug
, "current standard: %s (0x%04x)\n",
765 msp_standard_std_name(val
), val
);
766 state
->main
= msp_stdlist
[i
].main
;
767 state
->second
= msp_stdlist
[i
].second
;
769 state
->rxsubchans
= V4L2_TUNER_SUB_MONO
;
771 if (msp_amsound
&& !state
->radio
&&
772 (state
->v4l2_std
& V4L2_STD_SECAM
) && (val
!= 0x0009)) {
773 /* autodetection has failed, let backup */
774 dev_dbg_lvl(&client
->dev
, 1, msp_debug
, "autodetection failed, switching to backup standard: %s (0x%04x)\n",
775 msp_stdlist
[8].name
?
776 msp_stdlist
[8].name
: "unknown", val
);
777 state
->std
= val
= 0x0009;
778 msp_write_dem(client
, 0x20, val
);
780 state
->detected_std
= msp_standard_std(state
->std
);
785 case 0x0008: /* B/G NICAM */
786 case 0x000a: /* I NICAM */
787 case 0x000b: /* D/K NICAM */
789 state
->mode
= MSP_MODE_FM_NICAM2
;
791 state
->mode
= MSP_MODE_FM_NICAM1
;
792 /* just turn on stereo */
794 state
->watch_stereo
= 1;
797 state
->mode
= MSP_MODE_AM_NICAM
;
799 state
->watch_stereo
= 1;
801 case 0x0020: /* BTSC */
802 /* The pre-'G' models only have BTSC-mono */
803 state
->mode
= MSP_MODE_BTSC
;
805 case 0x0040: /* FM radio */
806 state
->mode
= MSP_MODE_FM_RADIO
;
807 state
->rxsubchans
= V4L2_TUNER_SUB_STEREO
;
808 /* not needed in theory if we have radio, but
809 short programming enables carrier mute */
810 msp3400c_set_mode(client
, MSP_MODE_FM_RADIO
);
811 msp3400c_set_carrier(client
, MSP_CARRIER(10.7),
818 state
->mode
= MSP_MODE_FM_TERRA
;
819 state
->watch_stereo
= 1;
823 /* set various prescales */
824 msp_write_dsp(client
, 0x0d, 0x1900); /* scart */
825 msp_write_dsp(client
, 0x0e, 0x3000); /* FM */
826 if (state
->has_nicam
)
827 msp_write_dsp(client
, 0x10, 0x5a00); /* nicam */
829 if (state
->has_i2s_conf
)
830 msp_write_dem(client
, 0x40, state
->i2s_mode
);
833 msp3400c_set_audmode(client
);
834 state
->scan_in_progress
= 0;
835 msp_update_volume(state
);
837 /* monitor tv audio mode, the first time don't wait
838 so long to get a quick stereo/bilingual result */
840 while (state
->watch_stereo
) {
841 if (msp_sleep(state
, count
? 1000 : 5000))
845 watch_stereo(client
);
848 dev_dbg_lvl(&client
->dev
, 1, msp_debug
, "thread: exit\n");
852 /* ----------------------------------------------------------------------- */
854 /* msp34xxG + (autoselect no-thread)
855 * this one uses both automatic standard detection and automatic sound
856 * select which are available in the newer G versions
857 * struct msp: only norm, acb and source are really used in this mode
860 static int msp34xxg_modus(struct i2c_client
*client
)
862 struct msp_state
*state
= to_state(i2c_get_clientdata(client
));
865 dev_dbg_lvl(&client
->dev
, 1, msp_debug
, "selected radio modus\n");
868 if (state
->v4l2_std
== V4L2_STD_NTSC_M_JP
) {
869 dev_dbg_lvl(&client
->dev
, 1, msp_debug
, "selected M (EIA-J) modus\n");
872 if (state
->v4l2_std
== V4L2_STD_NTSC_M_KR
) {
873 dev_dbg_lvl(&client
->dev
, 1, msp_debug
, "selected M (A2) modus\n");
876 if (state
->v4l2_std
== V4L2_STD_SECAM_L
) {
877 dev_dbg_lvl(&client
->dev
, 1, msp_debug
, "selected SECAM-L modus\n");
880 if (state
->v4l2_std
& V4L2_STD_MN
) {
881 dev_dbg_lvl(&client
->dev
, 1, msp_debug
, "selected M (BTSC) modus\n");
887 static void msp34xxg_set_source(struct i2c_client
*client
, u16 reg
, int in
)
889 struct msp_state
*state
= to_state(i2c_get_clientdata(client
));
892 switch (state
->audmode
) {
893 case V4L2_TUNER_MODE_MONO
:
894 source
= 0; /* mono only */
897 case V4L2_TUNER_MODE_LANG2
:
898 source
= 4; /* stereo or B */
901 case V4L2_TUNER_MODE_LANG1_LANG2
:
902 source
= 1; /* stereo or A|B */
905 case V4L2_TUNER_MODE_LANG1
:
906 source
= 3; /* stereo or A */
909 case V4L2_TUNER_MODE_STEREO
:
911 source
= 3; /* stereo or A */
916 if (in
== MSP_DSP_IN_TUNER
)
917 source
= (source
<< 8) | 0x20;
918 /* the msp34x2g puts the MAIN_AVC, MAIN and AUX sources in 12, 13, 14
919 instead of 11, 12, 13. So we add one for that msp version. */
920 else if (in
>= MSP_DSP_IN_MAIN_AVC
&& state
->has_dolby_pro_logic
)
921 source
= ((in
+ 1) << 8) | matrix
;
923 source
= (in
<< 8) | matrix
;
925 dev_dbg_lvl(&client
->dev
, 1, msp_debug
,
926 "set source to %d (0x%x) for output %02x\n", in
, source
, reg
);
927 msp_write_dsp(client
, reg
, source
);
930 static void msp34xxg_set_sources(struct i2c_client
*client
)
932 struct msp_state
*state
= to_state(i2c_get_clientdata(client
));
933 u32 in
= state
->route_in
;
935 msp34xxg_set_source(client
, 0x0008, (in
>> 4) & 0xf);
936 /* quasi-peak detector is set to same input as the loudspeaker (MAIN) */
937 msp34xxg_set_source(client
, 0x000c, (in
>> 4) & 0xf);
938 msp34xxg_set_source(client
, 0x0009, (in
>> 8) & 0xf);
939 msp34xxg_set_source(client
, 0x000a, (in
>> 12) & 0xf);
940 if (state
->has_scart2_out
)
941 msp34xxg_set_source(client
, 0x0041, (in
>> 16) & 0xf);
942 msp34xxg_set_source(client
, 0x000b, (in
>> 20) & 0xf);
945 /* (re-)initialize the msp34xxg */
946 static void msp34xxg_reset(struct i2c_client
*client
)
948 struct msp_state
*state
= to_state(i2c_get_clientdata(client
));
949 int tuner
= (state
->route_in
>> 3) & 1;
952 /* initialize std to 1 (autodetect) to signal that no standard is
958 if (state
->has_i2s_conf
)
959 msp_write_dem(client
, 0x40, state
->i2s_mode
);
961 /* step-by-step initialisation, as described in the manual */
962 modus
= msp34xxg_modus(client
);
963 modus
|= tuner
? 0x100 : 0;
964 msp_write_dem(client
, 0x30, modus
);
966 /* write the dsps that may have an influence on
967 standard/audio autodetection right now */
968 msp34xxg_set_sources(client
);
970 msp_write_dsp(client
, 0x0d, 0x1900); /* scart */
971 msp_write_dsp(client
, 0x0e, 0x3000); /* FM */
972 if (state
->has_nicam
)
973 msp_write_dsp(client
, 0x10, 0x5a00); /* nicam */
975 /* set identification threshold. Personally, I
976 * I set it to a higher value than the default
977 * of 0x190 to ignore noisy stereo signals.
978 * this needs tuning. (recommended range 0x00a0-0x03c0)
979 * 0x7f0 = forced mono mode
981 * a2 threshold for stereo/bilingual.
982 * Note: this register is part of the Manual/Compatibility mode.
983 * It is supported by all 'G'-family chips.
985 msp_write_dem(client
, 0x22, msp_stereo_thresh
);
988 int msp34xxg_thread(void *data
)
990 struct i2c_client
*client
= data
;
991 struct msp_state
*state
= to_state(i2c_get_clientdata(client
));
994 dev_dbg_lvl(&client
->dev
, 1, msp_debug
, "msp34xxg daemon started\n");
995 state
->detected_std
= V4L2_STD_ALL
;
998 dev_dbg_lvl(&client
->dev
, 2, msp_debug
, "msp34xxg thread: sleep\n");
999 msp_sleep(state
, -1);
1000 dev_dbg_lvl(&client
->dev
, 2, msp_debug
, "msp34xxg thread: wakeup\n");
1003 dev_dbg_lvl(&client
->dev
, 1, msp_debug
, "thread: restart scan\n");
1005 if (kthread_should_stop())
1008 if (state
->mode
== MSP_MODE_EXTERN
) {
1009 /* no carrier scan needed, just unmute */
1010 dev_dbg_lvl(&client
->dev
, 1, msp_debug
,
1011 "thread: no carrier scan\n");
1012 state
->scan_in_progress
= 0;
1013 msp_update_volume(state
);
1018 msp34xxg_reset(client
);
1019 state
->std
= state
->radio
? 0x40 :
1020 (state
->force_btsc
&& msp_standard
== 1) ? 32 : msp_standard
;
1021 msp_write_dem(client
, 0x20, state
->std
);
1022 /* start autodetect */
1023 if (state
->std
!= 1)
1026 /* watch autodetect */
1027 dev_dbg_lvl(&client
->dev
, 1, msp_debug
,
1028 "started autodetect, waiting for result\n");
1029 for (i
= 0; i
< 10; i
++) {
1030 if (msp_sleep(state
, 100))
1034 val
= msp_read_dem(client
, 0x7e);
1039 dev_dbg_lvl(&client
->dev
, 2, msp_debug
,
1040 "detection still in progress\n");
1042 if (state
->std
== 1) {
1043 dev_dbg_lvl(&client
->dev
, 1, msp_debug
,
1044 "detection still in progress after 10 tries. giving up.\n");
1049 dev_dbg_lvl(&client
->dev
, 1, msp_debug
,
1050 "detected standard: %s (0x%04x)\n",
1051 msp_standard_std_name(state
->std
), state
->std
);
1052 state
->detected_std
= msp_standard_std(state
->std
);
1054 if (state
->std
== 9) {
1056 msp_write_dsp(client
, 0x0e, 0x7c00);
1059 /* unmute: dispatch sound to scart output, set scart volume */
1060 msp_update_volume(state
);
1063 if (msp_write_dsp(client
, 0x13, state
->acb
))
1066 /* the periodic stereo/SAP check is only relevant for
1067 the 0x20 standard (BTSC) */
1068 if (state
->std
!= 0x20)
1071 state
->watch_stereo
= 1;
1073 /* monitor tv audio mode, the first time don't wait
1074 in order to get a quick stereo/SAP update */
1075 watch_stereo(client
);
1076 while (state
->watch_stereo
) {
1077 watch_stereo(client
);
1078 if (msp_sleep(state
, 5000))
1082 dev_dbg_lvl(&client
->dev
, 1, msp_debug
, "thread: exit\n");
1086 static int msp34xxg_detect_stereo(struct i2c_client
*client
)
1088 struct msp_state
*state
= to_state(i2c_get_clientdata(client
));
1089 int status
= msp_read_dem(client
, 0x0200);
1090 int is_bilingual
= status
& 0x100;
1091 int is_stereo
= status
& 0x40;
1092 int oldrx
= state
->rxsubchans
;
1094 if (state
->mode
== MSP_MODE_EXTERN
)
1097 state
->rxsubchans
= 0;
1099 state
->rxsubchans
= V4L2_TUNER_SUB_STEREO
;
1101 state
->rxsubchans
= V4L2_TUNER_SUB_MONO
;
1103 if (state
->std
== 0x20)
1104 state
->rxsubchans
|= V4L2_TUNER_SUB_SAP
;
1107 V4L2_TUNER_SUB_LANG1
| V4L2_TUNER_SUB_LANG2
;
1109 dev_dbg_lvl(&client
->dev
, 1, msp_debug
,
1110 "status=0x%x, stereo=%d, bilingual=%d -> rxsubchans=%d\n",
1111 status
, is_stereo
, is_bilingual
, state
->rxsubchans
);
1112 return (oldrx
!= state
->rxsubchans
);
1115 static void msp34xxg_set_audmode(struct i2c_client
*client
)
1117 struct msp_state
*state
= to_state(i2c_get_clientdata(client
));
1119 if (state
->std
== 0x20) {
1120 if ((state
->rxsubchans
& V4L2_TUNER_SUB_SAP
) &&
1121 (state
->audmode
== V4L2_TUNER_MODE_LANG1_LANG2
||
1122 state
->audmode
== V4L2_TUNER_MODE_LANG2
)) {
1123 msp_write_dem(client
, 0x20, 0x21);
1125 msp_write_dem(client
, 0x20, 0x20);
1129 msp34xxg_set_sources(client
);
1132 void msp_set_audmode(struct i2c_client
*client
)
1134 struct msp_state
*state
= to_state(i2c_get_clientdata(client
));
1136 switch (state
->opmode
) {
1138 case OPMODE_AUTODETECT
:
1139 msp3400c_set_audmode(client
);
1141 case OPMODE_AUTOSELECT
:
1142 msp34xxg_set_audmode(client
);
1147 int msp_detect_stereo(struct i2c_client
*client
)
1149 struct msp_state
*state
= to_state(i2c_get_clientdata(client
));
1151 switch (state
->opmode
) {
1153 case OPMODE_AUTODETECT
:
1154 return msp3400c_detect_stereo(client
);
1155 case OPMODE_AUTOSELECT
:
1156 return msp34xxg_detect_stereo(client
);