1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Programming the mspx4xx sound processor family
5 * (c) 1997-2001 Gerd Knorr <kraxel@bytesex.org>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/i2c.h>
12 #include <linux/freezer.h>
13 #include <linux/videodev2.h>
14 #include <media/v4l2-common.h>
15 #include <media/drv-intf/msp3400.h>
16 #include <linux/kthread.h>
17 #include <linux/suspend.h>
18 #include "msp3400-driver.h"
20 /* this one uses the automatic sound standard detection of newer msp34xx
28 { 0x0000, 0, 0, "could not detect sound standard", V4L2_STD_ALL
},
29 { 0x0001, 0, 0, "autodetect start", V4L2_STD_ALL
},
30 { 0x0002, MSP_CARRIER(4.5), MSP_CARRIER(4.72),
31 "4.5/4.72 M Dual FM-Stereo", V4L2_STD_MN
},
32 { 0x0003, MSP_CARRIER(5.5), MSP_CARRIER(5.7421875),
33 "5.5/5.74 B/G Dual FM-Stereo", V4L2_STD_BG
},
34 { 0x0004, MSP_CARRIER(6.5), MSP_CARRIER(6.2578125),
35 "6.5/6.25 D/K1 Dual FM-Stereo", V4L2_STD_DK
},
36 { 0x0005, MSP_CARRIER(6.5), MSP_CARRIER(6.7421875),
37 "6.5/6.74 D/K2 Dual FM-Stereo", V4L2_STD_DK
},
38 { 0x0006, MSP_CARRIER(6.5), MSP_CARRIER(6.5),
39 "6.5 D/K FM-Mono (HDEV3)", V4L2_STD_DK
},
40 { 0x0007, MSP_CARRIER(6.5), MSP_CARRIER(5.7421875),
41 "6.5/5.74 D/K3 Dual FM-Stereo", V4L2_STD_DK
},
42 { 0x0008, MSP_CARRIER(5.5), MSP_CARRIER(5.85),
43 "5.5/5.85 B/G NICAM FM", V4L2_STD_BG
},
44 { 0x0009, MSP_CARRIER(6.5), MSP_CARRIER(5.85),
45 "6.5/5.85 L NICAM AM", V4L2_STD_L
},
46 { 0x000a, MSP_CARRIER(6.0), MSP_CARRIER(6.55),
47 "6.0/6.55 I NICAM FM", V4L2_STD_PAL_I
},
48 { 0x000b, MSP_CARRIER(6.5), MSP_CARRIER(5.85),
49 "6.5/5.85 D/K NICAM FM", V4L2_STD_DK
},
50 { 0x000c, MSP_CARRIER(6.5), MSP_CARRIER(5.85),
51 "6.5/5.85 D/K NICAM FM (HDEV2)", V4L2_STD_DK
},
52 { 0x000d, MSP_CARRIER(6.5), MSP_CARRIER(5.85),
53 "6.5/5.85 D/K NICAM FM (HDEV3)", V4L2_STD_DK
},
54 { 0x0020, MSP_CARRIER(4.5), MSP_CARRIER(4.5),
55 "4.5 M BTSC-Stereo", V4L2_STD_MTS
},
56 { 0x0021, MSP_CARRIER(4.5), MSP_CARRIER(4.5),
57 "4.5 M BTSC-Mono + SAP", V4L2_STD_MTS
},
58 { 0x0030, MSP_CARRIER(4.5), MSP_CARRIER(4.5),
59 "4.5 M EIA-J Japan Stereo", V4L2_STD_NTSC_M_JP
},
60 { 0x0040, MSP_CARRIER(10.7), MSP_CARRIER(10.7),
61 "10.7 FM-Stereo Radio", V4L2_STD_ALL
},
62 { 0x0050, MSP_CARRIER(6.5), MSP_CARRIER(6.5),
63 "6.5 SAT-Mono", V4L2_STD_ALL
},
64 { 0x0051, MSP_CARRIER(7.02), MSP_CARRIER(7.20),
65 "7.02/7.20 SAT-Stereo", V4L2_STD_ALL
},
66 { 0x0060, MSP_CARRIER(7.2), MSP_CARRIER(7.2),
67 "7.2 SAT ADR", V4L2_STD_ALL
},
68 { -1, 0, 0, NULL
, 0 }, /* EOF */
71 static struct msp3400c_init_data_dem
{
80 } msp3400c_init_data
[] = {
81 { /* AM (for carrier detect / msp3400) */
82 {75, 19, 36, 35, 39, 40},
83 {75, 19, 36, 35, 39, 40},
84 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
85 0x00d0, 0x0500, 0x0020, 0x3000
86 }, { /* AM (for carrier detect / msp3410) */
87 {-1, -1, -8, 2, 59, 126},
88 {-1, -1, -8, 2, 59, 126},
89 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
90 0x00d0, 0x0100, 0x0020, 0x3000
92 {-8, -8, 4, 6, 78, 107},
93 {-8, -8, 4, 6, 78, 107},
94 MSP_CARRIER(10.7), MSP_CARRIER(10.7),
95 0x00d0, 0x0480, 0x0020, 0x3000
96 }, { /* Terrestrial FM-mono + FM-stereo */
97 {3, 18, 27, 48, 66, 72},
98 {3, 18, 27, 48, 66, 72},
99 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
100 0x00d0, 0x0480, 0x0030, 0x3000
101 }, { /* Sat FM-mono */
102 { 1, 9, 14, 24, 33, 37},
103 { 3, 18, 27, 48, 66, 72},
104 MSP_CARRIER(6.5), MSP_CARRIER(6.5),
105 0x00c6, 0x0480, 0x0000, 0x3000
106 }, { /* NICAM/FM -- B/G (5.5/5.85), D/K (6.5/5.85) */
107 {-2, -8, -10, 10, 50, 86},
108 {3, 18, 27, 48, 66, 72},
109 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
110 0x00d0, 0x0040, 0x0120, 0x3000
111 }, { /* NICAM/FM -- I (6.0/6.552) */
112 {2, 4, -6, -4, 40, 94},
113 {3, 18, 27, 48, 66, 72},
114 MSP_CARRIER(6.0), MSP_CARRIER(6.0),
115 0x00d0, 0x0040, 0x0120, 0x3000
116 }, { /* NICAM/AM -- L (6.5/5.85) */
117 {-2, -8, -10, 10, 50, 86},
118 {-4, -12, -9, 23, 79, 126},
119 MSP_CARRIER(6.5), MSP_CARRIER(6.5),
120 0x00c6, 0x0140, 0x0120, 0x7c00
124 struct msp3400c_carrier_detect
{
129 static struct msp3400c_carrier_detect msp3400c_carrier_detect_main
[] = {
131 { MSP_CARRIER(4.5), "4.5 NTSC" },
132 { MSP_CARRIER(5.5), "5.5 PAL B/G" },
133 { MSP_CARRIER(6.0), "6.0 PAL I" },
134 { MSP_CARRIER(6.5), "6.5 PAL D/K + SAT + SECAM" }
137 static struct msp3400c_carrier_detect msp3400c_carrier_detect_55
[] = {
139 { MSP_CARRIER(5.7421875), "5.742 PAL B/G FM-stereo" },
140 { MSP_CARRIER(5.85), "5.85 PAL B/G NICAM" }
143 static struct msp3400c_carrier_detect msp3400c_carrier_detect_65
[] = {
144 /* PAL SAT / SECAM */
145 { MSP_CARRIER(5.85), "5.85 PAL D/K + SECAM NICAM" },
146 { MSP_CARRIER(6.2578125), "6.25 PAL D/K1 FM-stereo" },
147 { MSP_CARRIER(6.7421875), "6.74 PAL D/K2 FM-stereo" },
148 { MSP_CARRIER(7.02), "7.02 PAL SAT FM-stereo s/b" },
149 { MSP_CARRIER(7.20), "7.20 PAL SAT FM-stereo s" },
150 { MSP_CARRIER(7.38), "7.38 PAL SAT FM-stereo b" },
153 /* ------------------------------------------------------------------------ */
155 const char *msp_standard_std_name(int std
)
159 for (i
= 0; msp_stdlist
[i
].name
!= NULL
; i
++)
160 if (msp_stdlist
[i
].retval
== std
)
161 return msp_stdlist
[i
].name
;
165 static v4l2_std_id
msp_standard_std(int std
)
169 for (i
= 0; msp_stdlist
[i
].name
!= NULL
; i
++)
170 if (msp_stdlist
[i
].retval
== std
)
171 return msp_stdlist
[i
].std
;
175 static void msp_set_source(struct i2c_client
*client
, u16 src
)
177 struct msp_state
*state
= to_state(i2c_get_clientdata(client
));
180 msp_write_dsp(client
, 0x0008, 0x0520); /* I2S1 */
181 msp_write_dsp(client
, 0x0009, 0x0620); /* I2S2 */
183 msp_write_dsp(client
, 0x0008, src
);
184 msp_write_dsp(client
, 0x0009, src
);
186 msp_write_dsp(client
, 0x000a, src
);
187 msp_write_dsp(client
, 0x000b, src
);
188 msp_write_dsp(client
, 0x000c, src
);
189 if (state
->has_scart2_out
)
190 msp_write_dsp(client
, 0x0041, src
);
193 void msp3400c_set_carrier(struct i2c_client
*client
, int cdo1
, int cdo2
)
195 msp_write_dem(client
, 0x0093, cdo1
& 0xfff);
196 msp_write_dem(client
, 0x009b, cdo1
>> 12);
197 msp_write_dem(client
, 0x00a3, cdo2
& 0xfff);
198 msp_write_dem(client
, 0x00ab, cdo2
>> 12);
199 msp_write_dem(client
, 0x0056, 0); /* LOAD_REG_1/2 */
202 void msp3400c_set_mode(struct i2c_client
*client
, int mode
)
204 struct msp_state
*state
= to_state(i2c_get_clientdata(client
));
205 struct msp3400c_init_data_dem
*data
= &msp3400c_init_data
[mode
];
206 int tuner
= (state
->route_in
>> 3) & 1;
209 dev_dbg_lvl(&client
->dev
, 1, msp_debug
, "set_mode: %d\n", mode
);
211 state
->rxsubchans
= V4L2_TUNER_SUB_MONO
;
213 msp_write_dem(client
, 0x00bb, data
->ad_cv
| (tuner
? 0x100 : 0));
215 for (i
= 5; i
>= 0; i
--) /* fir 1 */
216 msp_write_dem(client
, 0x0001, data
->fir1
[i
]);
218 msp_write_dem(client
, 0x0005, 0x0004); /* fir 2 */
219 msp_write_dem(client
, 0x0005, 0x0040);
220 msp_write_dem(client
, 0x0005, 0x0000);
221 for (i
= 5; i
>= 0; i
--)
222 msp_write_dem(client
, 0x0005, data
->fir2
[i
]);
224 msp_write_dem(client
, 0x0083, data
->mode_reg
);
226 msp3400c_set_carrier(client
, data
->cdo1
, data
->cdo2
);
228 msp_set_source(client
, data
->dsp_src
);
231 /* volume prescale for SCART (AM mono input) */
232 msp_write_dsp(client
, 0x000d, 0x1900);
233 msp_write_dsp(client
, 0x000e, data
->dsp_matrix
);
234 if (state
->has_nicam
) /* nicam prescale */
235 msp_write_dsp(client
, 0x0010, 0x5a00);
238 /* Set audio mode. Note that the pre-'G' models do not support BTSC+SAP,
239 nor do they support stereo BTSC. */
240 static void msp3400c_set_audmode(struct i2c_client
*client
)
242 static char *strmode
[] = {
243 "mono", "stereo", "lang2", "lang1", "lang1+lang2"
245 struct msp_state
*state
= to_state(i2c_get_clientdata(client
));
246 char *modestr
= (state
->audmode
>= 0 && state
->audmode
< 5) ?
247 strmode
[state
->audmode
] : "unknown";
248 int src
= 0; /* channel source: FM/AM, nicam or SCART */
249 int audmode
= state
->audmode
;
251 if (state
->opmode
== OPMODE_AUTOSELECT
) {
252 /* this method would break everything, let's make sure
255 dev_dbg_lvl(&client
->dev
, 1, msp_debug
,
256 "set_audmode called with mode=%d instead of set_source (ignored)\n",
261 /* Note: for the C and D revs no NTSC stereo + SAP is possible as
262 the hardware does not support SAP. So the rxsubchans combination
263 of STEREO | LANG2 does not occur. */
265 if (state
->mode
!= MSP_MODE_EXTERN
) {
266 /* switch to mono if only mono is available */
267 if (state
->rxsubchans
== V4L2_TUNER_SUB_MONO
)
268 audmode
= V4L2_TUNER_MODE_MONO
;
270 else if (state
->rxsubchans
& V4L2_TUNER_SUB_LANG2
) {
271 /* and mono or stereo, then fallback to lang1 */
272 if (audmode
== V4L2_TUNER_MODE_MONO
||
273 audmode
== V4L2_TUNER_MODE_STEREO
)
274 audmode
= V4L2_TUNER_MODE_LANG1
;
276 /* if stereo, and audmode is not mono, then switch to stereo */
277 else if (audmode
!= V4L2_TUNER_MODE_MONO
)
278 audmode
= V4L2_TUNER_MODE_STEREO
;
281 /* switch demodulator */
282 switch (state
->mode
) {
283 case MSP_MODE_FM_TERRA
:
284 dev_dbg_lvl(&client
->dev
, 1, msp_debug
, "FM set_audmode: %s\n", modestr
);
286 case V4L2_TUNER_MODE_STEREO
:
287 msp_write_dsp(client
, 0x000e, 0x3001);
289 case V4L2_TUNER_MODE_MONO
:
290 case V4L2_TUNER_MODE_LANG1
:
291 case V4L2_TUNER_MODE_LANG2
:
292 case V4L2_TUNER_MODE_LANG1_LANG2
:
293 msp_write_dsp(client
, 0x000e, 0x3000);
297 case MSP_MODE_FM_SAT
:
298 dev_dbg_lvl(&client
->dev
, 1, msp_debug
, "SAT set_audmode: %s\n", modestr
);
300 case V4L2_TUNER_MODE_MONO
:
301 msp3400c_set_carrier(client
, MSP_CARRIER(6.5), MSP_CARRIER(6.5));
303 case V4L2_TUNER_MODE_STEREO
:
304 case V4L2_TUNER_MODE_LANG1_LANG2
:
305 msp3400c_set_carrier(client
, MSP_CARRIER(7.2), MSP_CARRIER(7.02));
307 case V4L2_TUNER_MODE_LANG1
:
308 msp3400c_set_carrier(client
, MSP_CARRIER(7.38), MSP_CARRIER(7.02));
310 case V4L2_TUNER_MODE_LANG2
:
311 msp3400c_set_carrier(client
, MSP_CARRIER(7.38), MSP_CARRIER(7.02));
315 case MSP_MODE_FM_NICAM1
:
316 case MSP_MODE_FM_NICAM2
:
317 case MSP_MODE_AM_NICAM
:
318 dev_dbg_lvl(&client
->dev
, 1, msp_debug
,
319 "NICAM set_audmode: %s\n", modestr
);
321 src
= 0x0100; /* NICAM */
324 dev_dbg_lvl(&client
->dev
, 1, msp_debug
,
325 "BTSC set_audmode: %s\n", modestr
);
327 case MSP_MODE_EXTERN
:
328 dev_dbg_lvl(&client
->dev
, 1, msp_debug
,
329 "extern set_audmode: %s\n", modestr
);
330 src
= 0x0200; /* SCART */
332 case MSP_MODE_FM_RADIO
:
333 dev_dbg_lvl(&client
->dev
, 1, msp_debug
,
334 "FM-Radio set_audmode: %s\n", modestr
);
337 dev_dbg_lvl(&client
->dev
, 1, msp_debug
, "mono set_audmode\n");
342 dev_dbg_lvl(&client
->dev
, 1, msp_debug
, "set audmode %d\n", audmode
);
344 case V4L2_TUNER_MODE_STEREO
:
345 case V4L2_TUNER_MODE_LANG1_LANG2
:
348 case V4L2_TUNER_MODE_MONO
:
349 if (state
->mode
== MSP_MODE_AM_NICAM
) {
350 dev_dbg_lvl(&client
->dev
, 1, msp_debug
, "switching to AM mono\n");
351 /* AM mono decoding is handled by tuner, not MSP chip */
352 /* SCART switching control register */
353 msp_set_scart(client
, SCART_MONO
, 0);
357 if (state
->rxsubchans
& V4L2_TUNER_SUB_STEREO
)
360 case V4L2_TUNER_MODE_LANG1
:
362 case V4L2_TUNER_MODE_LANG2
:
366 dev_dbg_lvl(&client
->dev
, 1, msp_debug
,
367 "set_audmode final source/matrix = 0x%x\n", src
);
369 msp_set_source(client
, src
);
372 static void msp3400c_print_mode(struct i2c_client
*client
)
374 struct msp_state
*state
= to_state(i2c_get_clientdata(client
));
376 if (state
->main
== state
->second
)
377 dev_dbg_lvl(&client
->dev
, 1, msp_debug
,
378 "mono sound carrier: %d.%03d MHz\n",
379 state
->main
/ 910000, (state
->main
/ 910) % 1000);
381 dev_dbg_lvl(&client
->dev
, 1, msp_debug
,
382 "main sound carrier: %d.%03d MHz\n",
383 state
->main
/ 910000, (state
->main
/ 910) % 1000);
384 if (state
->mode
== MSP_MODE_FM_NICAM1
|| state
->mode
== MSP_MODE_FM_NICAM2
)
385 dev_dbg_lvl(&client
->dev
, 1, msp_debug
,
386 "NICAM/FM carrier : %d.%03d MHz\n",
387 state
->second
/ 910000, (state
->second
/910) % 1000);
388 if (state
->mode
== MSP_MODE_AM_NICAM
)
389 dev_dbg_lvl(&client
->dev
, 1, msp_debug
,
390 "NICAM/AM carrier : %d.%03d MHz\n",
391 state
->second
/ 910000, (state
->second
/ 910) % 1000);
392 if (state
->mode
== MSP_MODE_FM_TERRA
&& state
->main
!= state
->second
) {
393 dev_dbg_lvl(&client
->dev
, 1, msp_debug
,
394 "FM-stereo carrier : %d.%03d MHz\n",
395 state
->second
/ 910000, (state
->second
/ 910) % 1000);
399 /* ----------------------------------------------------------------------- */
401 static int msp3400c_detect_stereo(struct i2c_client
*client
)
403 struct msp_state
*state
= to_state(i2c_get_clientdata(client
));
405 int rxsubchans
= state
->rxsubchans
;
406 int newnicam
= state
->nicam_on
;
409 switch (state
->mode
) {
410 case MSP_MODE_FM_TERRA
:
411 val
= msp_read_dsp(client
, 0x18);
414 dev_dbg_lvl(&client
->dev
, 2, msp_debug
,
415 "stereo detect register: %d\n", val
);
417 rxsubchans
= V4L2_TUNER_SUB_STEREO
;
418 } else if (val
< -4096) {
419 rxsubchans
= V4L2_TUNER_SUB_LANG1
| V4L2_TUNER_SUB_LANG2
;
421 rxsubchans
= V4L2_TUNER_SUB_MONO
;
425 case MSP_MODE_FM_NICAM1
:
426 case MSP_MODE_FM_NICAM2
:
427 case MSP_MODE_AM_NICAM
:
428 val
= msp_read_dem(client
, 0x23);
429 dev_dbg_lvl(&client
->dev
, 2, msp_debug
, "nicam sync=%d, mode=%d\n",
430 val
& 1, (val
& 0x1e) >> 1);
434 switch ((val
& 0x1e) >> 1) {
437 rxsubchans
= V4L2_TUNER_SUB_STEREO
;
441 rxsubchans
= V4L2_TUNER_SUB_MONO
;
445 rxsubchans
= V4L2_TUNER_SUB_LANG1
| V4L2_TUNER_SUB_LANG2
;
448 rxsubchans
= V4L2_TUNER_SUB_MONO
;
454 rxsubchans
= V4L2_TUNER_SUB_MONO
;
458 if (rxsubchans
!= state
->rxsubchans
) {
460 dev_dbg_lvl(&client
->dev
, 1, msp_debug
,
461 "watch: rxsubchans %02x => %02x\n",
462 state
->rxsubchans
, rxsubchans
);
463 state
->rxsubchans
= rxsubchans
;
465 if (newnicam
!= state
->nicam_on
) {
467 dev_dbg_lvl(&client
->dev
, 1, msp_debug
, "watch: nicam %d => %d\n",
468 state
->nicam_on
, newnicam
);
469 state
->nicam_on
= newnicam
;
475 * A kernel thread for msp3400 control -- we don't want to block the
476 * in the ioctl while doing the sound carrier & stereo detect
478 /* stereo/multilang monitoring */
479 static void watch_stereo(struct i2c_client
*client
)
481 struct msp_state
*state
= to_state(i2c_get_clientdata(client
));
483 if (msp_detect_stereo(client
))
484 msp_set_audmode(client
);
487 state
->watch_stereo
= 0;
490 int msp3400c_thread(void *data
)
492 struct i2c_client
*client
= data
;
493 struct msp_state
*state
= to_state(i2c_get_clientdata(client
));
494 struct msp3400c_carrier_detect
*cd
;
495 int count
, max1
, max2
, val1
, val2
, val
, i
;
497 dev_dbg_lvl(&client
->dev
, 1, msp_debug
, "msp3400 daemon started\n");
498 state
->detected_std
= V4L2_STD_ALL
;
501 dev_dbg_lvl(&client
->dev
, 2, msp_debug
, "msp3400 thread: sleep\n");
502 msp_sleep(state
, -1);
503 dev_dbg_lvl(&client
->dev
, 2, msp_debug
, "msp3400 thread: wakeup\n");
506 dev_dbg_lvl(&client
->dev
, 2, msp_debug
, "thread: restart scan\n");
508 if (kthread_should_stop())
511 if (state
->radio
|| MSP_MODE_EXTERN
== state
->mode
) {
512 /* no carrier scan, just unmute */
513 dev_dbg_lvl(&client
->dev
, 1, msp_debug
,
514 "thread: no carrier scan\n");
515 state
->scan_in_progress
= 0;
516 msp_update_volume(state
);
521 state
->scan_in_progress
= 1;
522 msp_update_volume(state
);
524 msp3400c_set_mode(client
, MSP_MODE_AM_DETECT
);
527 state
->watch_stereo
= 0;
530 /* wait for tuner to settle down after a channel change */
531 if (msp_sleep(state
, 200))
534 /* carrier detect pass #1 -- main carrier */
535 cd
= msp3400c_carrier_detect_main
;
536 count
= ARRAY_SIZE(msp3400c_carrier_detect_main
);
538 if (msp_amsound
&& (state
->v4l2_std
& V4L2_STD_SECAM
)) {
539 /* autodetect doesn't work well with AM ... */
542 dev_dbg_lvl(&client
->dev
, 1, msp_debug
, "AM sound override\n");
545 for (i
= 0; i
< count
; i
++) {
546 msp3400c_set_carrier(client
, cd
[i
].cdo
, cd
[i
].cdo
);
547 if (msp_sleep(state
, 100))
549 val
= msp_read_dsp(client
, 0x1b);
553 val1
= val
, max1
= i
;
554 dev_dbg_lvl(&client
->dev
, 1, msp_debug
,
555 "carrier1 val: %5d / %s\n", val
, cd
[i
].name
);
558 /* carrier detect pass #2 -- second (stereo) carrier */
561 cd
= msp3400c_carrier_detect_55
;
562 count
= ARRAY_SIZE(msp3400c_carrier_detect_55
);
565 cd
= msp3400c_carrier_detect_65
;
566 count
= ARRAY_SIZE(msp3400c_carrier_detect_65
);
576 if (msp_amsound
&& (state
->v4l2_std
& V4L2_STD_SECAM
)) {
577 /* autodetect doesn't work well with AM ... */
582 for (i
= 0; i
< count
; i
++) {
583 msp3400c_set_carrier(client
, cd
[i
].cdo
, cd
[i
].cdo
);
584 if (msp_sleep(state
, 100))
586 val
= msp_read_dsp(client
, 0x1b);
590 val2
= val
, max2
= i
;
591 dev_dbg_lvl(&client
->dev
, 1, msp_debug
,
592 "carrier2 val: %5d / %s\n", val
, cd
[i
].name
);
595 /* program the msp3400 according to the results */
596 state
->main
= msp3400c_carrier_detect_main
[max1
].cdo
;
599 state
->detected_std
= V4L2_STD_BG
| V4L2_STD_PAL_H
;
602 state
->second
= msp3400c_carrier_detect_55
[max2
].cdo
;
603 msp3400c_set_mode(client
, MSP_MODE_FM_TERRA
);
604 state
->watch_stereo
= 1;
605 } else if (max2
== 1 && state
->has_nicam
) {
607 state
->second
= msp3400c_carrier_detect_55
[max2
].cdo
;
608 msp3400c_set_mode(client
, MSP_MODE_FM_NICAM1
);
610 state
->watch_stereo
= 1;
617 state
->detected_std
= V4L2_STD_PAL_I
;
618 state
->second
= MSP_CARRIER(6.552);
619 msp3400c_set_mode(client
, MSP_MODE_FM_NICAM2
);
621 state
->watch_stereo
= 1;
624 if (max2
== 1 || max2
== 2) {
626 state
->second
= msp3400c_carrier_detect_65
[max2
].cdo
;
627 msp3400c_set_mode(client
, MSP_MODE_FM_TERRA
);
628 state
->watch_stereo
= 1;
629 state
->detected_std
= V4L2_STD_DK
;
630 } else if (max2
== 0 && (state
->v4l2_std
& V4L2_STD_SECAM
)) {
631 /* L NICAM or AM-mono */
632 state
->second
= msp3400c_carrier_detect_65
[max2
].cdo
;
633 msp3400c_set_mode(client
, MSP_MODE_AM_NICAM
);
634 state
->watch_stereo
= 1;
635 state
->detected_std
= V4L2_STD_L
;
636 } else if (max2
== 0 && state
->has_nicam
) {
638 state
->second
= msp3400c_carrier_detect_65
[max2
].cdo
;
639 msp3400c_set_mode(client
, MSP_MODE_FM_NICAM1
);
641 state
->watch_stereo
= 1;
642 state
->detected_std
= V4L2_STD_DK
;
648 state
->detected_std
= V4L2_STD_MN
;
652 state
->second
= msp3400c_carrier_detect_main
[max1
].cdo
;
653 msp3400c_set_mode(client
, MSP_MODE_FM_TERRA
);
656 msp3400c_set_carrier(client
, state
->second
, state
->main
);
659 state
->scan_in_progress
= 0;
660 msp3400c_set_audmode(client
);
661 msp_update_volume(state
);
664 msp3400c_print_mode(client
);
666 /* monitor tv audio mode, the first time don't wait
667 so long to get a quick stereo/bilingual result */
669 while (state
->watch_stereo
) {
670 if (msp_sleep(state
, count
? 1000 : 5000))
674 watch_stereo(client
);
677 dev_dbg_lvl(&client
->dev
, 1, msp_debug
, "thread: exit\n");
682 int msp3410d_thread(void *data
)
684 struct i2c_client
*client
= data
;
685 struct msp_state
*state
= to_state(i2c_get_clientdata(client
));
686 int val
, i
, std
, count
;
688 dev_dbg_lvl(&client
->dev
, 1, msp_debug
, "msp3410 daemon started\n");
689 state
->detected_std
= V4L2_STD_ALL
;
692 dev_dbg_lvl(&client
->dev
, 2, msp_debug
, "msp3410 thread: sleep\n");
693 msp_sleep(state
, -1);
694 dev_dbg_lvl(&client
->dev
, 2, msp_debug
, "msp3410 thread: wakeup\n");
697 dev_dbg_lvl(&client
->dev
, 2, msp_debug
, "thread: restart scan\n");
699 if (kthread_should_stop())
702 if (state
->mode
== MSP_MODE_EXTERN
) {
703 /* no carrier scan needed, just unmute */
704 dev_dbg_lvl(&client
->dev
, 1, msp_debug
,
705 "thread: no carrier scan\n");
706 state
->scan_in_progress
= 0;
707 msp_update_volume(state
);
712 state
->scan_in_progress
= 1;
713 msp_update_volume(state
);
715 /* start autodetect. Note: autodetect is not supported for
716 NTSC-M and radio, hence we force the standard in those
721 std
= (state
->v4l2_std
& V4L2_STD_NTSC
) ? 0x20 : 1;
722 state
->watch_stereo
= 0;
725 /* wait for tuner to settle down after a channel change */
726 if (msp_sleep(state
, 200))
730 dev_dbg_lvl(&client
->dev
, 2, msp_debug
,
731 "setting standard: %s (0x%04x)\n",
732 msp_standard_std_name(std
), std
);
735 /* programmed some specific mode */
738 /* triggered autodetect */
739 msp_write_dem(client
, 0x20, std
);
741 if (msp_sleep(state
, 100))
745 val
= msp_read_dem(client
, 0x7e);
748 dev_dbg_lvl(&client
->dev
, 2, msp_debug
,
749 "detection still in progress\n");
752 for (i
= 0; msp_stdlist
[i
].name
!= NULL
; i
++)
753 if (msp_stdlist
[i
].retval
== val
)
755 dev_dbg_lvl(&client
->dev
, 1, msp_debug
, "current standard: %s (0x%04x)\n",
756 msp_standard_std_name(val
), val
);
757 state
->main
= msp_stdlist
[i
].main
;
758 state
->second
= msp_stdlist
[i
].second
;
760 state
->rxsubchans
= V4L2_TUNER_SUB_MONO
;
762 if (msp_amsound
&& !state
->radio
&&
763 (state
->v4l2_std
& V4L2_STD_SECAM
) && (val
!= 0x0009)) {
764 /* autodetection has failed, let backup */
765 dev_dbg_lvl(&client
->dev
, 1, msp_debug
, "autodetection failed, switching to backup standard: %s (0x%04x)\n",
766 msp_stdlist
[8].name
?
767 msp_stdlist
[8].name
: "unknown", val
);
768 state
->std
= val
= 0x0009;
769 msp_write_dem(client
, 0x20, val
);
771 state
->detected_std
= msp_standard_std(state
->std
);
776 case 0x0008: /* B/G NICAM */
777 case 0x000a: /* I NICAM */
778 case 0x000b: /* D/K NICAM */
780 state
->mode
= MSP_MODE_FM_NICAM2
;
782 state
->mode
= MSP_MODE_FM_NICAM1
;
783 /* just turn on stereo */
785 state
->watch_stereo
= 1;
788 state
->mode
= MSP_MODE_AM_NICAM
;
790 state
->watch_stereo
= 1;
792 case 0x0020: /* BTSC */
793 /* The pre-'G' models only have BTSC-mono */
794 state
->mode
= MSP_MODE_BTSC
;
796 case 0x0040: /* FM radio */
797 state
->mode
= MSP_MODE_FM_RADIO
;
798 state
->rxsubchans
= V4L2_TUNER_SUB_STEREO
;
799 /* not needed in theory if we have radio, but
800 short programming enables carrier mute */
801 msp3400c_set_mode(client
, MSP_MODE_FM_RADIO
);
802 msp3400c_set_carrier(client
, MSP_CARRIER(10.7),
809 state
->mode
= MSP_MODE_FM_TERRA
;
810 state
->watch_stereo
= 1;
814 /* set various prescales */
815 msp_write_dsp(client
, 0x0d, 0x1900); /* scart */
816 msp_write_dsp(client
, 0x0e, 0x3000); /* FM */
817 if (state
->has_nicam
)
818 msp_write_dsp(client
, 0x10, 0x5a00); /* nicam */
820 if (state
->has_i2s_conf
)
821 msp_write_dem(client
, 0x40, state
->i2s_mode
);
824 msp3400c_set_audmode(client
);
825 state
->scan_in_progress
= 0;
826 msp_update_volume(state
);
828 /* monitor tv audio mode, the first time don't wait
829 so long to get a quick stereo/bilingual result */
831 while (state
->watch_stereo
) {
832 if (msp_sleep(state
, count
? 1000 : 5000))
836 watch_stereo(client
);
839 dev_dbg_lvl(&client
->dev
, 1, msp_debug
, "thread: exit\n");
843 /* ----------------------------------------------------------------------- */
845 /* msp34xxG + (autoselect no-thread)
846 * this one uses both automatic standard detection and automatic sound
847 * select which are available in the newer G versions
848 * struct msp: only norm, acb and source are really used in this mode
851 static int msp34xxg_modus(struct i2c_client
*client
)
853 struct msp_state
*state
= to_state(i2c_get_clientdata(client
));
856 dev_dbg_lvl(&client
->dev
, 1, msp_debug
, "selected radio modus\n");
859 if (state
->v4l2_std
== V4L2_STD_NTSC_M_JP
) {
860 dev_dbg_lvl(&client
->dev
, 1, msp_debug
, "selected M (EIA-J) modus\n");
863 if (state
->v4l2_std
== V4L2_STD_NTSC_M_KR
) {
864 dev_dbg_lvl(&client
->dev
, 1, msp_debug
, "selected M (A2) modus\n");
867 if (state
->v4l2_std
== V4L2_STD_SECAM_L
) {
868 dev_dbg_lvl(&client
->dev
, 1, msp_debug
, "selected SECAM-L modus\n");
871 if (state
->v4l2_std
& V4L2_STD_MN
) {
872 dev_dbg_lvl(&client
->dev
, 1, msp_debug
, "selected M (BTSC) modus\n");
878 static void msp34xxg_set_source(struct i2c_client
*client
, u16 reg
, int in
)
880 struct msp_state
*state
= to_state(i2c_get_clientdata(client
));
883 switch (state
->audmode
) {
884 case V4L2_TUNER_MODE_MONO
:
885 source
= 0; /* mono only */
888 case V4L2_TUNER_MODE_LANG2
:
889 source
= 4; /* stereo or B */
892 case V4L2_TUNER_MODE_LANG1_LANG2
:
893 source
= 1; /* stereo or A|B */
896 case V4L2_TUNER_MODE_LANG1
:
897 source
= 3; /* stereo or A */
900 case V4L2_TUNER_MODE_STEREO
:
902 source
= 3; /* stereo or A */
907 if (in
== MSP_DSP_IN_TUNER
)
908 source
= (source
<< 8) | 0x20;
909 /* the msp34x2g puts the MAIN_AVC, MAIN and AUX sources in 12, 13, 14
910 instead of 11, 12, 13. So we add one for that msp version. */
911 else if (in
>= MSP_DSP_IN_MAIN_AVC
&& state
->has_dolby_pro_logic
)
912 source
= ((in
+ 1) << 8) | matrix
;
914 source
= (in
<< 8) | matrix
;
916 dev_dbg_lvl(&client
->dev
, 1, msp_debug
,
917 "set source to %d (0x%x) for output %02x\n", in
, source
, reg
);
918 msp_write_dsp(client
, reg
, source
);
921 static void msp34xxg_set_sources(struct i2c_client
*client
)
923 struct msp_state
*state
= to_state(i2c_get_clientdata(client
));
924 u32 in
= state
->route_in
;
926 msp34xxg_set_source(client
, 0x0008, (in
>> 4) & 0xf);
927 /* quasi-peak detector is set to same input as the loudspeaker (MAIN) */
928 msp34xxg_set_source(client
, 0x000c, (in
>> 4) & 0xf);
929 msp34xxg_set_source(client
, 0x0009, (in
>> 8) & 0xf);
930 msp34xxg_set_source(client
, 0x000a, (in
>> 12) & 0xf);
931 if (state
->has_scart2_out
)
932 msp34xxg_set_source(client
, 0x0041, (in
>> 16) & 0xf);
933 msp34xxg_set_source(client
, 0x000b, (in
>> 20) & 0xf);
936 /* (re-)initialize the msp34xxg */
937 static void msp34xxg_reset(struct i2c_client
*client
)
939 struct msp_state
*state
= to_state(i2c_get_clientdata(client
));
940 int tuner
= (state
->route_in
>> 3) & 1;
943 /* initialize std to 1 (autodetect) to signal that no standard is
949 if (state
->has_i2s_conf
)
950 msp_write_dem(client
, 0x40, state
->i2s_mode
);
952 /* step-by-step initialisation, as described in the manual */
953 modus
= msp34xxg_modus(client
);
954 modus
|= tuner
? 0x100 : 0;
955 msp_write_dem(client
, 0x30, modus
);
957 /* write the dsps that may have an influence on
958 standard/audio autodetection right now */
959 msp34xxg_set_sources(client
);
961 msp_write_dsp(client
, 0x0d, 0x1900); /* scart */
962 msp_write_dsp(client
, 0x0e, 0x3000); /* FM */
963 if (state
->has_nicam
)
964 msp_write_dsp(client
, 0x10, 0x5a00); /* nicam */
966 /* set identification threshold. Personally, I
967 * I set it to a higher value than the default
968 * of 0x190 to ignore noisy stereo signals.
969 * this needs tuning. (recommended range 0x00a0-0x03c0)
970 * 0x7f0 = forced mono mode
972 * a2 threshold for stereo/bilingual.
973 * Note: this register is part of the Manual/Compatibility mode.
974 * It is supported by all 'G'-family chips.
976 msp_write_dem(client
, 0x22, msp_stereo_thresh
);
979 int msp34xxg_thread(void *data
)
981 struct i2c_client
*client
= data
;
982 struct msp_state
*state
= to_state(i2c_get_clientdata(client
));
985 dev_dbg_lvl(&client
->dev
, 1, msp_debug
, "msp34xxg daemon started\n");
986 state
->detected_std
= V4L2_STD_ALL
;
989 dev_dbg_lvl(&client
->dev
, 2, msp_debug
, "msp34xxg thread: sleep\n");
990 msp_sleep(state
, -1);
991 dev_dbg_lvl(&client
->dev
, 2, msp_debug
, "msp34xxg thread: wakeup\n");
994 dev_dbg_lvl(&client
->dev
, 1, msp_debug
, "thread: restart scan\n");
996 if (kthread_should_stop())
999 if (state
->mode
== MSP_MODE_EXTERN
) {
1000 /* no carrier scan needed, just unmute */
1001 dev_dbg_lvl(&client
->dev
, 1, msp_debug
,
1002 "thread: no carrier scan\n");
1003 state
->scan_in_progress
= 0;
1004 msp_update_volume(state
);
1009 msp34xxg_reset(client
);
1010 state
->std
= state
->radio
? 0x40 :
1011 (state
->force_btsc
&& msp_standard
== 1) ? 32 : msp_standard
;
1012 msp_write_dem(client
, 0x20, state
->std
);
1013 /* start autodetect */
1014 if (state
->std
!= 1)
1017 /* watch autodetect */
1018 dev_dbg_lvl(&client
->dev
, 1, msp_debug
,
1019 "started autodetect, waiting for result\n");
1020 for (i
= 0; i
< 10; i
++) {
1021 if (msp_sleep(state
, 100))
1025 val
= msp_read_dem(client
, 0x7e);
1030 dev_dbg_lvl(&client
->dev
, 2, msp_debug
,
1031 "detection still in progress\n");
1033 if (state
->std
== 1) {
1034 dev_dbg_lvl(&client
->dev
, 1, msp_debug
,
1035 "detection still in progress after 10 tries. giving up.\n");
1040 dev_dbg_lvl(&client
->dev
, 1, msp_debug
,
1041 "detected standard: %s (0x%04x)\n",
1042 msp_standard_std_name(state
->std
), state
->std
);
1043 state
->detected_std
= msp_standard_std(state
->std
);
1045 if (state
->std
== 9) {
1047 msp_write_dsp(client
, 0x0e, 0x7c00);
1050 /* unmute: dispatch sound to scart output, set scart volume */
1051 msp_update_volume(state
);
1054 if (msp_write_dsp(client
, 0x13, state
->acb
))
1057 /* the periodic stereo/SAP check is only relevant for
1058 the 0x20 standard (BTSC) */
1059 if (state
->std
!= 0x20)
1062 state
->watch_stereo
= 1;
1064 /* monitor tv audio mode, the first time don't wait
1065 in order to get a quick stereo/SAP update */
1066 watch_stereo(client
);
1067 while (state
->watch_stereo
) {
1068 watch_stereo(client
);
1069 if (msp_sleep(state
, 5000))
1073 dev_dbg_lvl(&client
->dev
, 1, msp_debug
, "thread: exit\n");
1077 static int msp34xxg_detect_stereo(struct i2c_client
*client
)
1079 struct msp_state
*state
= to_state(i2c_get_clientdata(client
));
1080 int status
= msp_read_dem(client
, 0x0200);
1081 int is_bilingual
= status
& 0x100;
1082 int is_stereo
= status
& 0x40;
1083 int oldrx
= state
->rxsubchans
;
1085 if (state
->mode
== MSP_MODE_EXTERN
)
1088 state
->rxsubchans
= 0;
1090 state
->rxsubchans
= V4L2_TUNER_SUB_STEREO
;
1092 state
->rxsubchans
= V4L2_TUNER_SUB_MONO
;
1094 if (state
->std
== 0x20)
1095 state
->rxsubchans
|= V4L2_TUNER_SUB_SAP
;
1098 V4L2_TUNER_SUB_LANG1
| V4L2_TUNER_SUB_LANG2
;
1100 dev_dbg_lvl(&client
->dev
, 1, msp_debug
,
1101 "status=0x%x, stereo=%d, bilingual=%d -> rxsubchans=%d\n",
1102 status
, is_stereo
, is_bilingual
, state
->rxsubchans
);
1103 return (oldrx
!= state
->rxsubchans
);
1106 static void msp34xxg_set_audmode(struct i2c_client
*client
)
1108 struct msp_state
*state
= to_state(i2c_get_clientdata(client
));
1110 if (state
->std
== 0x20) {
1111 if ((state
->rxsubchans
& V4L2_TUNER_SUB_SAP
) &&
1112 (state
->audmode
== V4L2_TUNER_MODE_LANG1_LANG2
||
1113 state
->audmode
== V4L2_TUNER_MODE_LANG2
)) {
1114 msp_write_dem(client
, 0x20, 0x21);
1116 msp_write_dem(client
, 0x20, 0x20);
1120 msp34xxg_set_sources(client
);
1123 void msp_set_audmode(struct i2c_client
*client
)
1125 struct msp_state
*state
= to_state(i2c_get_clientdata(client
));
1127 switch (state
->opmode
) {
1129 case OPMODE_AUTODETECT
:
1130 msp3400c_set_audmode(client
);
1132 case OPMODE_AUTOSELECT
:
1133 msp34xxg_set_audmode(client
);
1138 int msp_detect_stereo(struct i2c_client
*client
)
1140 struct msp_state
*state
= to_state(i2c_get_clientdata(client
));
1142 switch (state
->opmode
) {
1144 case OPMODE_AUTODETECT
:
1145 return msp3400c_detect_stereo(client
);
1146 case OPMODE_AUTOSELECT
:
1147 return msp34xxg_detect_stereo(client
);