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 02110-1301, USA.
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/slab.h>
25 #include <linux/i2c.h>
26 #include <linux/videodev.h>
27 #include <linux/videodev2.h>
28 #include <media/v4l2-common.h>
29 #include <media/audiochip.h>
30 #include <linux/kthread.h>
31 #include <linux/suspend.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 { 0x0008, MSP_CARRIER(5.5), MSP_CARRIER(5.85), "5.5/5.85 B/G NICAM FM" },
49 { 0x0009, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85 L NICAM AM" },
50 { 0x000a, MSP_CARRIER(6.0), MSP_CARRIER(6.55), "6.0/6.55 I NICAM FM" },
51 { 0x000b, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85 D/K NICAM FM" },
52 { 0x000c, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85 D/K NICAM FM (HDEV2)" },
53 { 0x0020, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5 M BTSC-Stereo" },
54 { 0x0021, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5 M BTSC-Mono + SAP" },
55 { 0x0030, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5 M EIA-J Japan Stereo" },
56 { 0x0040, MSP_CARRIER(10.7), MSP_CARRIER(10.7), "10.7 FM-Stereo Radio" },
57 { 0x0050, MSP_CARRIER(6.5), MSP_CARRIER(6.5), "6.5 SAT-Mono" },
58 { 0x0051, MSP_CARRIER(7.02), MSP_CARRIER(7.20), "7.02/7.20 SAT-Stereo" },
59 { 0x0060, MSP_CARRIER(7.2), MSP_CARRIER(7.2), "7.2 SAT ADR" },
60 { -1, 0, 0, NULL }, /* EOF */
63 static struct msp3400c_init_data_dem {
72 } msp3400c_init_data[] = {
73 { /* AM (for carrier detect / msp3400) */
74 {75, 19, 36, 35, 39, 40},
75 {75, 19, 36, 35, 39, 40},
76 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
77 0x00d0, 0x0500, 0x0020, 0x3000
78 },{ /* AM (for carrier detect / msp3410) */
79 {-1, -1, -8, 2, 59, 126},
80 {-1, -1, -8, 2, 59, 126},
81 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
82 0x00d0, 0x0100, 0x0020, 0x3000
84 {-8, -8, 4, 6, 78, 107},
85 {-8, -8, 4, 6, 78, 107},
86 MSP_CARRIER(10.7), MSP_CARRIER(10.7),
87 0x00d0, 0x0480, 0x0020, 0x3000
88 },{ /* Terrestial FM-mono + FM-stereo */
89 {3, 18, 27, 48, 66, 72},
90 {3, 18, 27, 48, 66, 72},
91 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
92 0x00d0, 0x0480, 0x0030, 0x3000
94 { 1, 9, 14, 24, 33, 37},
95 { 3, 18, 27, 48, 66, 72},
96 MSP_CARRIER(6.5), MSP_CARRIER(6.5),
97 0x00c6, 0x0480, 0x0000, 0x3000
98 },{ /* NICAM/FM -- B/G (5.5/5.85), D/K (6.5/5.85) */
99 {-2, -8, -10, 10, 50, 86},
100 {3, 18, 27, 48, 66, 72},
101 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
102 0x00d0, 0x0040, 0x0120, 0x3000
103 },{ /* NICAM/FM -- I (6.0/6.552) */
104 {2, 4, -6, -4, 40, 94},
105 {3, 18, 27, 48, 66, 72},
106 MSP_CARRIER(6.0), MSP_CARRIER(6.0),
107 0x00d0, 0x0040, 0x0120, 0x3000
108 },{ /* NICAM/AM -- L (6.5/5.85) */
109 {-2, -8, -10, 10, 50, 86},
110 {-4, -12, -9, 23, 79, 126},
111 MSP_CARRIER(6.5), MSP_CARRIER(6.5),
112 0x00c6, 0x0140, 0x0120, 0x7c03
116 struct msp3400c_carrier_detect {
121 static struct msp3400c_carrier_detect msp3400c_carrier_detect_main[] = {
123 { MSP_CARRIER(4.5), "4.5 NTSC" },
124 { MSP_CARRIER(5.5), "5.5 PAL B/G" },
125 { MSP_CARRIER(6.0), "6.0 PAL I" },
126 { MSP_CARRIER(6.5), "6.5 PAL D/K + SAT + SECAM" }
129 static struct msp3400c_carrier_detect msp3400c_carrier_detect_55[] = {
131 { MSP_CARRIER(5.7421875), "5.742 PAL B/G FM-stereo" },
132 { MSP_CARRIER(5.85), "5.85 PAL B/G NICAM" }
135 static struct msp3400c_carrier_detect msp3400c_carrier_detect_65[] = {
136 /* PAL SAT / SECAM */
137 { MSP_CARRIER(5.85), "5.85 PAL D/K + SECAM NICAM" },
138 { MSP_CARRIER(6.2578125), "6.25 PAL D/K1 FM-stereo" },
139 { MSP_CARRIER(6.7421875), "6.74 PAL D/K2 FM-stereo" },
140 { MSP_CARRIER(7.02), "7.02 PAL SAT FM-stereo s/b" },
141 { MSP_CARRIER(7.20), "7.20 PAL SAT FM-stereo s" },
142 { MSP_CARRIER(7.38), "7.38 PAL SAT FM-stereo b" },
145 /* ------------------------------------------------------------------------ */
147 const char *msp_standard_mode_name(int mode)
151 for (i = 0; msp_modelist[i].name != NULL; i++)
152 if (msp_modelist[i].retval == mode)
153 return msp_modelist[i].name;
157 void msp3400c_setcarrier(struct i2c_client *client, int cdo1, int cdo2)
159 msp_write_dem(client, 0x0093, cdo1 & 0xfff);
160 msp_write_dem(client, 0x009b, cdo1 >> 12);
161 msp_write_dem(client, 0x00a3, cdo2 & 0xfff);
162 msp_write_dem(client, 0x00ab, cdo2 >> 12);
163 msp_write_dem(client, 0x0056, 0); /*LOAD_REG_1/2*/
166 void msp3400c_setmode(struct i2c_client *client, int type)
168 struct msp_state *state = i2c_get_clientdata(client);
171 v4l_dbg(1, client, "setmode: %d\n", type);
173 state->audmode = V4L2_TUNER_MODE_MONO;
174 state->rxsubchans = V4L2_TUNER_SUB_MONO;
176 msp_write_dem(client, 0x00bb, msp3400c_init_data[type].ad_cv);
178 for (i = 5; i >= 0; i--) /* fir 1 */
179 msp_write_dem(client, 0x0001, msp3400c_init_data[type].fir1[i]);
181 msp_write_dem(client, 0x0005, 0x0004); /* fir 2 */
182 msp_write_dem(client, 0x0005, 0x0040);
183 msp_write_dem(client, 0x0005, 0x0000);
184 for (i = 5; i >= 0; i--)
185 msp_write_dem(client, 0x0005, msp3400c_init_data[type].fir2[i]);
187 msp_write_dem(client, 0x0083, msp3400c_init_data[type].mode_reg);
189 msp3400c_setcarrier(client, msp3400c_init_data[type].cdo1,
190 msp3400c_init_data[type].cdo2);
192 msp_write_dem(client, 0x0056, 0); /*LOAD_REG_1/2*/
195 msp_write_dsp(client, 0x0008, 0x0520); /* I2S1 */
196 msp_write_dsp(client, 0x0009, 0x0620); /* I2S2 */
197 msp_write_dsp(client, 0x000b, msp3400c_init_data[type].dsp_src);
199 msp_write_dsp(client, 0x0008, msp3400c_init_data[type].dsp_src);
200 msp_write_dsp(client, 0x0009, msp3400c_init_data[type].dsp_src);
201 msp_write_dsp(client, 0x000b, msp3400c_init_data[type].dsp_src);
203 msp_write_dsp(client, 0x000a, msp3400c_init_data[type].dsp_src);
204 msp_write_dsp(client, 0x000e, msp3400c_init_data[type].dsp_matrix);
206 if (state->has_nicam) {
208 msp_write_dsp(client, 0x0010, 0x5a00); /* was: 0x3000 */
212 /* given a bitmask of VIDEO_SOUND_XXX returns the "best" in the bitmask */
213 static int msp3400c_best_video_sound(int rxsubchans)
215 if (rxsubchans & V4L2_TUNER_SUB_STEREO)
216 return V4L2_TUNER_MODE_STEREO;
217 if (rxsubchans & V4L2_TUNER_SUB_LANG1)
218 return V4L2_TUNER_MODE_LANG1;
219 if (rxsubchans & V4L2_TUNER_SUB_LANG2)
220 return V4L2_TUNER_MODE_LANG2;
221 return V4L2_TUNER_MODE_MONO;
224 /* turn on/off nicam + stereo */
225 void msp3400c_setstereo(struct i2c_client *client, int mode)
227 static char *strmode[] = { "0", "mono", "stereo", "3",
228 "lang1", "5", "6", "7", "lang2"
230 struct msp_state *state = i2c_get_clientdata(client);
231 int nicam = 0; /* channel source: FM/AM or nicam */
234 if (state->opmode == OPMODE_AUTOSELECT) {
235 /* this method would break everything, let's make sure
238 v4l_dbg(1, client, "setstereo called with mode=%d instead of set_source (ignored)\n",
243 /* switch demodulator */
244 switch (state->mode) {
245 case MSP_MODE_FM_TERRA:
246 v4l_dbg(1, client, "FM setstereo: %s\n", strmode[mode]);
247 msp3400c_setcarrier(client,state->second,state->main);
249 case V4L2_TUNER_MODE_STEREO:
250 msp_write_dsp(client, 0x000e, 0x3001);
252 case V4L2_TUNER_MODE_MONO:
253 case V4L2_TUNER_MODE_LANG1:
254 case V4L2_TUNER_MODE_LANG2:
255 msp_write_dsp(client, 0x000e, 0x3000);
259 case MSP_MODE_FM_SAT:
260 v4l_dbg(1, client, "SAT setstereo: %s\n", strmode[mode]);
262 case V4L2_TUNER_MODE_MONO:
263 msp3400c_setcarrier(client, MSP_CARRIER(6.5), MSP_CARRIER(6.5));
265 case V4L2_TUNER_MODE_STEREO:
266 msp3400c_setcarrier(client, MSP_CARRIER(7.2), MSP_CARRIER(7.02));
268 case V4L2_TUNER_MODE_LANG1:
269 msp3400c_setcarrier(client, MSP_CARRIER(7.38), MSP_CARRIER(7.02));
271 case V4L2_TUNER_MODE_LANG2:
272 msp3400c_setcarrier(client, MSP_CARRIER(7.38), MSP_CARRIER(7.02));
276 case MSP_MODE_FM_NICAM1:
277 case MSP_MODE_FM_NICAM2:
278 case MSP_MODE_AM_NICAM:
279 v4l_dbg(1, client, "NICAM setstereo: %s\n",strmode[mode]);
280 msp3400c_setcarrier(client,state->second,state->main);
285 v4l_dbg(1, client, "BTSC setstereo: %s\n",strmode[mode]);
288 case MSP_MODE_EXTERN:
289 v4l_dbg(1, client, "extern setstereo: %s\n",strmode[mode]);
292 case MSP_MODE_FM_RADIO:
293 v4l_dbg(1, client, "FM-Radio setstereo: %s\n",strmode[mode]);
296 v4l_dbg(1, client, "mono setstereo\n");
301 switch (msp3400c_best_video_sound(mode)) {
302 case V4L2_TUNER_MODE_STEREO:
303 src = 0x0020 | nicam;
305 case V4L2_TUNER_MODE_MONO:
306 if (state->mode == MSP_MODE_AM_NICAM) {
307 v4l_dbg(1, client, "switching to AM mono\n");
308 /* AM mono decoding is handled by tuner, not MSP chip */
309 /* SCART switching control register */
310 msp_set_scart(client, SCART_MONO, 0);
314 case V4L2_TUNER_MODE_LANG1:
315 src = 0x0000 | nicam;
317 case V4L2_TUNER_MODE_LANG2:
318 src = 0x0010 | nicam;
321 v4l_dbg(1, client, "setstereo final source/matrix = 0x%x\n", src);
324 msp_write_dsp(client, 0x0008, 0x0520);
325 msp_write_dsp(client, 0x0009, 0x0620);
326 msp_write_dsp(client, 0x000a, src);
327 msp_write_dsp(client, 0x000b, src);
329 msp_write_dsp(client, 0x0008, src);
330 msp_write_dsp(client, 0x0009, src);
331 msp_write_dsp(client, 0x000a, src);
332 msp_write_dsp(client, 0x000b, src);
336 static void msp3400c_print_mode(struct i2c_client *client)
338 struct msp_state *state = i2c_get_clientdata(client);
340 if (state->main == state->second) {
341 v4l_dbg(1, client, "mono sound carrier: %d.%03d MHz\n",
342 state->main / 910000, (state->main / 910) % 1000);
344 v4l_dbg(1, client, "main sound carrier: %d.%03d MHz\n",
345 state->main / 910000, (state->main / 910) % 1000);
347 if (state->mode == MSP_MODE_FM_NICAM1 || state->mode == MSP_MODE_FM_NICAM2)
348 v4l_dbg(1, client, "NICAM/FM carrier : %d.%03d MHz\n",
349 state->second / 910000, (state->second/910) % 1000);
350 if (state->mode == MSP_MODE_AM_NICAM)
351 v4l_dbg(1, client, "NICAM/AM carrier : %d.%03d MHz\n",
352 state->second / 910000, (state->second / 910) % 1000);
353 if (state->mode == MSP_MODE_FM_TERRA && state->main != state->second) {
354 v4l_dbg(1, client, "FM-stereo carrier : %d.%03d MHz\n",
355 state->second / 910000, (state->second / 910) % 1000);
359 /* ----------------------------------------------------------------------- */
361 int autodetect_stereo(struct i2c_client *client)
363 struct msp_state *state = i2c_get_clientdata(client);
365 int rxsubchans = state->rxsubchans;
366 int newnicam = state->nicam_on;
369 switch (state->mode) {
370 case MSP_MODE_FM_TERRA:
371 val = msp_read_dsp(client, 0x18);
374 v4l_dbg(2, client, "stereo detect register: %d\n", val);
376 rxsubchans = V4L2_TUNER_SUB_STEREO | V4L2_TUNER_SUB_MONO;
377 } else if (val < -4096) {
378 rxsubchans = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
380 rxsubchans = V4L2_TUNER_SUB_MONO;
384 case MSP_MODE_FM_NICAM1:
385 case MSP_MODE_FM_NICAM2:
386 case MSP_MODE_AM_NICAM:
387 val = msp_read_dem(client, 0x23);
388 v4l_dbg(2, client, "nicam sync=%d, mode=%d\n",
389 val & 1, (val & 0x1e) >> 1);
393 switch ((val & 0x1e) >> 1) {
396 rxsubchans = V4L2_TUNER_SUB_STEREO;
400 rxsubchans = V4L2_TUNER_SUB_MONO
401 | V4L2_TUNER_SUB_LANG1;
405 rxsubchans = V4L2_TUNER_SUB_MONO
406 | V4L2_TUNER_SUB_LANG1
407 | V4L2_TUNER_SUB_LANG2;
410 rxsubchans = V4L2_TUNER_SUB_MONO;
416 rxsubchans = V4L2_TUNER_SUB_MONO;
420 val = msp_read_dem(client, 0x200);
421 v4l_dbg(2, client, "status=0x%x (pri=%s, sec=%s, %s%s%s)\n",
423 (val & 0x0002) ? "no" : "yes",
424 (val & 0x0004) ? "no" : "yes",
425 (val & 0x0040) ? "stereo" : "mono",
426 (val & 0x0080) ? ", nicam 2nd mono" : "",
427 (val & 0x0100) ? ", bilingual/SAP" : "");
428 rxsubchans = V4L2_TUNER_SUB_MONO;
429 if (val & 0x0040) rxsubchans |= V4L2_TUNER_SUB_STEREO;
430 if (val & 0x0100) rxsubchans |= V4L2_TUNER_SUB_LANG1;
433 if (rxsubchans != state->rxsubchans) {
435 v4l_dbg(1, client, "watch: rxsubchans %d => %d\n",
436 state->rxsubchans,rxsubchans);
437 state->rxsubchans = rxsubchans;
439 if (newnicam != state->nicam_on) {
441 v4l_dbg(1, client, "watch: nicam %d => %d\n",
442 state->nicam_on,newnicam);
443 state->nicam_on = newnicam;
449 * A kernel thread for msp3400 control -- we don't want to block the
450 * in the ioctl while doing the sound carrier & stereo detect
452 /* stereo/multilang monitoring */
453 static void watch_stereo(struct i2c_client *client)
455 struct msp_state *state = i2c_get_clientdata(client);
457 if (autodetect_stereo(client)) {
458 if (state->stereo & V4L2_TUNER_MODE_STEREO)
459 msp3400c_setstereo(client, V4L2_TUNER_MODE_STEREO);
460 else if (state->stereo & VIDEO_SOUND_LANG1)
461 msp3400c_setstereo(client, V4L2_TUNER_MODE_LANG1);
463 msp3400c_setstereo(client, V4L2_TUNER_MODE_MONO);
467 state->watch_stereo = 0;
470 int msp3400c_thread(void *data)
472 struct i2c_client *client = data;
473 struct msp_state *state = i2c_get_clientdata(client);
474 struct msp3400c_carrier_detect *cd;
475 int count, max1,max2,val1,val2, val,this;
478 v4l_dbg(1, client, "msp3400 daemon started\n");
480 v4l_dbg(2, client, "msp3400 thread: sleep\n");
481 msp_sleep(state, -1);
482 v4l_dbg(2, client, "msp3400 thread: wakeup\n");
485 v4l_dbg(1, client, "thread: restart scan\n");
487 if (kthread_should_stop())
490 if (state->radio || MSP_MODE_EXTERN == state->mode) {
491 /* no carrier scan, just unmute */
492 v4l_dbg(1, client, "thread: no carrier scan\n");
493 msp_set_audio(client);
498 msp_set_mute(client);
499 msp3400c_setmode(client, MSP_MODE_AM_DETECT /* +1 */ );
502 state->watch_stereo = 0;
504 /* some time for the tuner to sync */
505 if (msp_sleep(state,200))
508 /* carrier detect pass #1 -- main carrier */
509 cd = msp3400c_carrier_detect_main;
510 count = ARRAY_SIZE(msp3400c_carrier_detect_main);
512 if (amsound && (state->std & V4L2_STD_SECAM)) {
513 /* autodetect doesn't work well with AM ... */
516 v4l_dbg(1, client, "AM sound override\n");
519 for (this = 0; this < count; this++) {
520 msp3400c_setcarrier(client, cd[this].cdo,cd[this].cdo);
521 if (msp_sleep(state,100))
523 val = msp_read_dsp(client, 0x1b);
527 val1 = val, max1 = this;
528 v4l_dbg(1, client, "carrier1 val: %5d / %s\n", val,cd[this].name);
531 /* carrier detect pass #2 -- second (stereo) carrier */
534 cd = msp3400c_carrier_detect_55;
535 count = ARRAY_SIZE(msp3400c_carrier_detect_55);
538 cd = msp3400c_carrier_detect_65;
539 count = ARRAY_SIZE(msp3400c_carrier_detect_65);
549 if (amsound && (state->std & V4L2_STD_SECAM)) {
550 /* autodetect doesn't work well with AM ... */
555 for (this = 0; this < count; this++) {
556 msp3400c_setcarrier(client, cd[this].cdo,cd[this].cdo);
557 if (msp_sleep(state,100))
559 val = msp_read_dsp(client, 0x1b);
563 val2 = val, max2 = this;
564 v4l_dbg(1, client, "carrier2 val: %5d / %s\n", val,cd[this].name);
567 /* program the msp3400 according to the results */
568 state->main = msp3400c_carrier_detect_main[max1].cdo;
573 state->second = msp3400c_carrier_detect_55[max2].cdo;
574 msp3400c_setmode(client, MSP_MODE_FM_TERRA);
576 msp3400c_setstereo(client, V4L2_TUNER_MODE_MONO);
577 state->watch_stereo = 1;
578 } else if (max2 == 1 && state->has_nicam) {
580 state->second = msp3400c_carrier_detect_55[max2].cdo;
581 msp3400c_setmode(client, MSP_MODE_FM_NICAM1);
583 msp3400c_setcarrier(client, state->second, state->main);
584 state->watch_stereo = 1;
591 state->second = MSP_CARRIER(6.552);
592 msp3400c_setmode(client, MSP_MODE_FM_NICAM2);
594 msp3400c_setcarrier(client, state->second, state->main);
595 state->watch_stereo = 1;
598 if (max2 == 1 || max2 == 2) {
600 state->second = msp3400c_carrier_detect_65[max2].cdo;
601 msp3400c_setmode(client, MSP_MODE_FM_TERRA);
603 msp3400c_setstereo(client, V4L2_TUNER_MODE_MONO);
604 state->watch_stereo = 1;
605 } else if (max2 == 0 && (state->std & V4L2_STD_SECAM)) {
606 /* L NICAM or AM-mono */
607 state->second = msp3400c_carrier_detect_65[max2].cdo;
608 msp3400c_setmode(client, MSP_MODE_AM_NICAM);
610 msp3400c_setstereo(client, V4L2_TUNER_MODE_MONO);
611 msp3400c_setcarrier(client, state->second, state->main);
612 /* volume prescale for SCART (AM mono input) */
613 msp_write_dsp(client, 0x000d, 0x1900);
614 state->watch_stereo = 1;
615 } else if (max2 == 0 && state->has_nicam) {
617 state->second = msp3400c_carrier_detect_65[max2].cdo;
618 msp3400c_setmode(client, MSP_MODE_FM_NICAM1);
620 msp3400c_setcarrier(client, state->second, state->main);
621 state->watch_stereo = 1;
629 state->second = msp3400c_carrier_detect_main[max1].cdo;
630 msp3400c_setmode(client, MSP_MODE_FM_TERRA);
632 msp3400c_setcarrier(client, state->second, state->main);
633 state->rxsubchans = V4L2_TUNER_SUB_MONO;
634 msp3400c_setstereo(client, V4L2_TUNER_MODE_MONO);
639 msp_set_audio(client);
642 msp3400c_print_mode(client);
644 /* monitor tv audio mode */
645 while (state->watch_stereo) {
646 if (msp_sleep(state,5000))
648 watch_stereo(client);
651 v4l_dbg(1, client, "thread: exit\n");
656 int msp3410d_thread(void *data)
658 struct i2c_client *client = data;
659 struct msp_state *state = i2c_get_clientdata(client);
662 v4l_dbg(1, client, "msp3410 daemon started\n");
665 v4l_dbg(2, client, "msp3410 thread: sleep\n");
667 v4l_dbg(2, client, "msp3410 thread: wakeup\n");
670 v4l_dbg(1, client, "thread: restart scan\n");
672 if (kthread_should_stop())
675 if (state->mode == MSP_MODE_EXTERN) {
676 /* no carrier scan needed, just unmute */
677 v4l_dbg(1, client, "thread: no carrier scan\n");
678 msp_set_audio(client);
682 /* put into sane state (and mute) */
685 /* some time for the tuner to sync */
686 if (msp_sleep(state,200))
689 /* start autodetect */
690 mode = msp_modus(client);
691 std = (state->std & V4L2_STD_NTSC) ? 0x20 : 1;
692 msp_write_dem(client, 0x30, mode);
693 msp_write_dem(client, 0x20, std);
694 state->watch_stereo = 0;
697 v4l_dbg(1, client, "setting mode: %s (0x%04x)\n",
698 msp_standard_mode_name(std), std);
701 /* programmed some specific mode */
704 /* triggered autodetect */
706 if (msp_sleep(state,100))
710 val = msp_read_dem(client, 0x7e);
713 v4l_dbg(1, client, "detection still in progress\n");
716 for (i = 0; msp_modelist[i].name != NULL; i++)
717 if (msp_modelist[i].retval == val)
719 v4l_dbg(1, client, "current mode: %s (0x%04x)\n",
720 msp_standard_mode_name(val), val);
721 state->main = msp_modelist[i].main;
722 state->second = msp_modelist[i].second;
724 if (amsound && (state->std & V4L2_STD_SECAM) && (val != 0x0009)) {
725 /* autodetection has failed, let backup */
726 v4l_dbg(1, client, "autodetection failed,"
727 " switching to backup mode: %s (0x%04x)\n",
728 msp_modelist[8].name ? msp_modelist[8].name : "unknown",val);
730 msp_write_dem(client, 0x20, val);
733 /* set various prescales */
734 msp_write_dsp(client, 0x0d, 0x1900); /* scart */
735 msp_write_dsp(client, 0x0e, 0x2403); /* FM */
736 msp_write_dsp(client, 0x10, 0x5a00); /* nicam */
740 case 0x0008: /* B/G NICAM */
741 case 0x000a: /* I NICAM */
743 state->mode = MSP_MODE_FM_NICAM1;
745 state->mode = MSP_MODE_FM_NICAM2;
746 /* just turn on stereo */
747 state->rxsubchans = V4L2_TUNER_SUB_STEREO;
749 state->watch_stereo = 1;
750 msp3400c_setstereo(client,V4L2_TUNER_MODE_STEREO);
753 state->mode = MSP_MODE_AM_NICAM;
754 state->rxsubchans = V4L2_TUNER_SUB_MONO;
756 msp3400c_setstereo(client,V4L2_TUNER_MODE_MONO);
757 state->watch_stereo = 1;
759 case 0x0020: /* BTSC */
760 /* just turn on stereo */
761 state->mode = MSP_MODE_BTSC;
762 state->rxsubchans = V4L2_TUNER_SUB_STEREO;
764 state->watch_stereo = 1;
765 msp3400c_setstereo(client,V4L2_TUNER_MODE_STEREO);
767 case 0x0040: /* FM radio */
768 state->mode = MSP_MODE_FM_RADIO;
769 state->rxsubchans = V4L2_TUNER_SUB_STEREO;
770 state->audmode = V4L2_TUNER_MODE_STEREO;
772 state->watch_stereo = 0;
773 /* not needed in theory if we have radio, but
774 short programming enables carrier mute */
775 msp3400c_setmode(client,MSP_MODE_FM_RADIO);
776 msp3400c_setcarrier(client, MSP_CARRIER(10.7),
779 msp_set_scart(client,SCART_IN2,0);
780 /* msp34xx does radio decoding */
781 msp_write_dsp(client, 0x08, 0x0020);
782 msp_write_dsp(client, 0x09, 0x0020);
783 msp_write_dsp(client, 0x0b, 0x0020);
788 state->mode = MSP_MODE_FM_TERRA;
789 state->rxsubchans = V4L2_TUNER_SUB_MONO;
790 state->audmode = V4L2_TUNER_MODE_MONO;
792 state->watch_stereo = 1;
796 /* unmute, restore misc registers */
797 msp_set_audio(client);
798 msp_write_dsp(client, 0x13, state->acb);
799 msp_write_dem(client, 0x40, state->i2s_mode);
801 /* monitor tv audio mode */
802 while (state->watch_stereo) {
803 if (msp_sleep(state,5000))
805 watch_stereo(client);
808 v4l_dbg(1, client, "thread: exit\n");
812 /* ----------------------------------------------------------------------- */
814 /* msp34xxG + (autoselect no-thread) */
815 /* this one uses both automatic standard detection and automatic sound */
816 /* select which are available in the newer G versions */
817 /* struct msp: only norm, acb and source are really used in this mode */
819 /* set the same 'source' for the loudspeaker, scart and quasi-peak detector
820 * the value for source is the same as bit 15:8 of DSP registers 0x08,
821 * 0x0a and 0x0c: 0=mono, 1=stereo or A|B, 2=SCART, 3=stereo or A, 4=stereo or B
823 * this function replaces msp3400c_setstereo
825 static void msp34xxg_set_source(struct i2c_client *client, int source)
827 struct msp_state *state = i2c_get_clientdata(client);
829 /* fix matrix mode to stereo and let the msp choose what
830 * to output according to 'source', as recommended
831 * for MONO (source==0) downmixing set bit[7:0] to 0x30
833 int value = (source & 0x07) << 8 | (source == 0 ? 0x30 : 0x20);
835 v4l_dbg(1, client, "set source to %d (0x%x)\n", source, value);
836 /* Loudspeaker Output */
837 msp_write_dsp(client, 0x08, value);
838 /* SCART1 DA Output */
839 msp_write_dsp(client, 0x0a, value);
840 /* Quasi-peak detector */
841 msp_write_dsp(client, 0x0c, value);
843 * set identification threshold. Personally, I
844 * I set it to a higher value that the default
845 * of 0x190 to ignore noisy stereo signals.
846 * this needs tuning. (recommended range 0x00a0-0x03c0)
847 * 0x7f0 = forced mono mode
849 /* a2 threshold for stereo/bilingual */
850 msp_write_dem(client, 0x22, stereo_threshold);
851 state->source = source;
854 /* (re-)initialize the msp34xxg, according to the current norm in state->norm
855 * return 0 if it worked, -1 if it failed
857 static int msp34xxg_reset(struct i2c_client *client)
859 struct msp_state *state = i2c_get_clientdata(client);
862 if (msp_reset(client))
865 /* make sure that input/output is muted (paranoid mode) */
866 /* ACB, mute DSP input, mute SCART 1 */
867 if (msp_write_dsp(client, 0x13, 0x0f20))
870 msp_write_dem(client, 0x40, state->i2s_mode);
872 /* step-by-step initialisation, as described in the manual */
873 modus = msp_modus(client);
877 std = (state->std & V4L2_STD_NTSC) ? 0x20 : 1;
878 modus &= ~0x03; /* STATUS_CHANGE = 0 */
879 modus |= 0x01; /* AUTOMATIC_SOUND_DETECTION = 1 */
880 if (msp_write_dem(client, 0x30, modus))
882 if (msp_write_dem(client, 0x20, std))
885 /* write the dsps that may have an influence on
886 standard/audio autodetection right now */
887 msp34xxg_set_source(client, state->source);
889 /* AM/FM Prescale [15:8] 75khz deviation */
890 if (msp_write_dsp(client, 0x0e, 0x3000))
893 /* NICAM Prescale 9db gain (as recommended) */
894 if (msp_write_dsp(client, 0x10, 0x5a00))
900 int msp34xxg_thread(void *data)
902 struct i2c_client *client = data;
903 struct msp_state *state = i2c_get_clientdata(client);
906 v4l_dbg(1, client, "msp34xxg daemon started\n");
908 state->source = 1; /* default */
910 v4l_dbg(2, client, "msp34xxg thread: sleep\n");
911 msp_sleep(state, -1);
912 v4l_dbg(2, client, "msp34xxg thread: wakeup\n");
915 v4l_dbg(1, client, "thread: restart scan\n");
917 if (kthread_should_stop())
921 msp34xxg_reset(client);
926 /* watch autodetect */
927 v4l_dbg(1, client, "triggered autodetect, waiting for result\n");
928 for (i = 0; i < 10; i++) {
929 if (msp_sleep(state, 100))
933 val = msp_read_dem(client, 0x7e);
938 v4l_dbg(2, client, "detection still in progress\n");
941 v4l_dbg(1, client, "detection still in progress after 10 tries. giving up.\n");
947 v4l_dbg(1, client, "current mode: %s (0x%04x)\n",
948 msp_standard_mode_name(std), std);
950 /* unmute: dispatch sound to scart output, set scart volume */
951 msp_set_audio(client);
954 if (msp_write_dsp(client, 0x13, state->acb))
957 msp_write_dem(client, 0x40, state->i2s_mode);
959 v4l_dbg(1, client, "thread: exit\n");
963 void msp34xxg_detect_stereo(struct i2c_client *client)
965 struct msp_state *state = i2c_get_clientdata(client);
967 int status = msp_read_dem(client, 0x0200);
968 int is_bilingual = status & 0x100;
969 int is_stereo = status & 0x40;
971 state->rxsubchans = 0;
973 state->rxsubchans |= V4L2_TUNER_SUB_STEREO;
975 state->rxsubchans |= V4L2_TUNER_SUB_MONO;
977 state->rxsubchans |= V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
978 /* I'm supposed to check whether it's SAP or not
979 * and set only LANG2/SAP in this case. Yet, the MSP
980 * does a lot of work to hide this and handle everything
981 * the same way. I don't want to work around it so unless
982 * this is a problem, I'll handle SAP just like lang1/lang2.
985 v4l_dbg(1, client, "status=0x%x, stereo=%d, bilingual=%d -> rxsubchans=%d\n",
986 status, is_stereo, is_bilingual, state->rxsubchans);
989 void msp34xxg_set_audmode(struct i2c_client *client, int audmode)
991 struct msp_state *state = i2c_get_clientdata(client);
995 case V4L2_TUNER_MODE_MONO:
996 source = 0; /* mono only */
998 case V4L2_TUNER_MODE_STEREO:
999 source = 1; /* stereo or A|B, see comment in msp34xxg_get_v4l2_stereo() */
1000 /* problem: that could also mean 2 (scart input) */
1002 case V4L2_TUNER_MODE_LANG1:
1003 source = 3; /* stereo or A */
1005 case V4L2_TUNER_MODE_LANG2:
1006 source = 4; /* stereo or B */
1013 state->audmode = audmode;
1014 msp34xxg_set_source(client, source);