]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/media/video/msp3400-kthreads.c
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[net-next-2.6.git] / drivers / media / video / msp3400-kthreads.c
CommitLineData
7e8b09ea
HV
1/*
2 * Programming the mspx4xx sound processor family
3 *
4 * (c) 1997-2001 Gerd Knorr <kraxel@bytesex.org>
5 *
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.
10 *
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.
15 *
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
d52c7385
HV
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 * 02110-1301, USA.
7e8b09ea
HV
20 */
21
22
23#include <linux/kernel.h>
24#include <linux/module.h>
7e8b09ea 25#include <linux/i2c.h>
83144186 26#include <linux/freezer.h>
7e8b09ea
HV
27#include <linux/videodev2.h>
28#include <media/v4l2-common.h>
49965a80 29#include <media/msp3400.h>
7e8b09ea
HV
30#include <linux/kthread.h>
31#include <linux/suspend.h>
49965a80 32#include "msp3400-driver.h"
7e8b09ea
HV
33
34/* this one uses the automatic sound standard detection of newer msp34xx
35 chip versions */
36static struct {
37 int retval;
38 int main, second;
39 char *name;
5af0c8f6 40} msp_stdlist[] = {
7e8b09ea
HV
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)" },
de533ccf 48 { 0x0007, MSP_CARRIER(6.5), MSP_CARRIER(5.7421875), "6.5/5.74 D/K3 Dual FM-Stereo" },
7e8b09ea
HV
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)" },
de533ccf 54 { 0x000d, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85 D/K NICAM FM (HDEV3)" },
7e8b09ea
HV
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 */
63};
64
65static struct msp3400c_init_data_dem {
66 int fir1[6];
67 int fir2[6];
68 int cdo1;
69 int cdo2;
70 int ad_cv;
71 int mode_reg;
72 int dsp_src;
73 int dsp_matrix;
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
d52c7385 80 }, { /* AM (for carrier detect / msp3410) */
7e8b09ea
HV
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
d52c7385 85 }, { /* FM Radio */
7e8b09ea
HV
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
d52c7385 90 }, { /* Terrestial FM-mono + FM-stereo */
7e8b09ea
HV
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
d52c7385 95 }, { /* Sat FM-mono */
7e8b09ea
HV
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
d52c7385 100 }, { /* NICAM/FM -- B/G (5.5/5.85), D/K (6.5/5.85) */
7e8b09ea
HV
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
d52c7385 105 }, { /* NICAM/FM -- I (6.0/6.552) */
7e8b09ea
HV
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
d52c7385 110 }, { /* NICAM/AM -- L (6.5/5.85) */
7e8b09ea
HV
111 {-2, -8, -10, 10, 50, 86},
112 {-4, -12, -9, 23, 79, 126},
113 MSP_CARRIER(6.5), MSP_CARRIER(6.5),
8a4b275f 114 0x00c6, 0x0140, 0x0120, 0x7c00
7e8b09ea
HV
115 },
116};
117
118struct msp3400c_carrier_detect {
119 int cdo;
120 char *name;
121};
122
123static struct msp3400c_carrier_detect msp3400c_carrier_detect_main[] = {
124 /* main carrier */
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" }
129};
130
131static struct msp3400c_carrier_detect msp3400c_carrier_detect_55[] = {
132 /* PAL B/G */
133 { MSP_CARRIER(5.7421875), "5.742 PAL B/G FM-stereo" },
134 { MSP_CARRIER(5.85), "5.85 PAL B/G NICAM" }
135};
136
137static 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" },
145};
146
147/* ------------------------------------------------------------------------ */
148
5af0c8f6 149const char *msp_standard_std_name(int std)
7e8b09ea
HV
150{
151 int i;
152
5af0c8f6
HV
153 for (i = 0; msp_stdlist[i].name != NULL; i++)
154 if (msp_stdlist[i].retval == std)
155 return msp_stdlist[i].name;
7e8b09ea
HV
156 return "unknown";
157}
158
044f324f 159static void msp_set_source(struct i2c_client *client, u16 src)
8a4b275f 160{
76efd62f 161 struct msp_state *state = to_state(i2c_get_clientdata(client));
8a4b275f
HV
162
163 if (msp_dolby) {
164 msp_write_dsp(client, 0x0008, 0x0520); /* I2S1 */
165 msp_write_dsp(client, 0x0009, 0x0620); /* I2S2 */
166 } else {
167 msp_write_dsp(client, 0x0008, src);
168 msp_write_dsp(client, 0x0009, src);
169 }
170 msp_write_dsp(client, 0x000a, src);
171 msp_write_dsp(client, 0x000b, src);
172 msp_write_dsp(client, 0x000c, src);
0020d3ef 173 if (state->has_scart2_out)
8a4b275f
HV
174 msp_write_dsp(client, 0x0041, src);
175}
176
177void msp3400c_set_carrier(struct i2c_client *client, int cdo1, int cdo2)
7e8b09ea
HV
178{
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);
8a4b275f 183 msp_write_dem(client, 0x0056, 0); /* LOAD_REG_1/2 */
7e8b09ea
HV
184}
185
8a4b275f 186void msp3400c_set_mode(struct i2c_client *client, int mode)
7e8b09ea 187{
76efd62f 188 struct msp_state *state = to_state(i2c_get_clientdata(client));
8a4b275f 189 struct msp3400c_init_data_dem *data = &msp3400c_init_data[mode];
5325b427 190 int tuner = (state->route_in >> 3) & 1;
7e8b09ea
HV
191 int i;
192
8a4b275f
HV
193 v4l_dbg(1, msp_debug, client, "set_mode: %d\n", mode);
194 state->mode = mode;
7e8b09ea
HV
195 state->rxsubchans = V4L2_TUNER_SUB_MONO;
196
2474ed44 197 msp_write_dem(client, 0x00bb, data->ad_cv | (tuner ? 0x100 : 0));
7e8b09ea
HV
198
199 for (i = 5; i >= 0; i--) /* fir 1 */
8a4b275f 200 msp_write_dem(client, 0x0001, data->fir1[i]);
7e8b09ea
HV
201
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--)
8a4b275f 206 msp_write_dem(client, 0x0005, data->fir2[i]);
7e8b09ea 207
8a4b275f 208 msp_write_dem(client, 0x0083, data->mode_reg);
7e8b09ea 209
8a4b275f 210 msp3400c_set_carrier(client, data->cdo1, data->cdo2);
7e8b09ea 211
8a4b275f 212 msp_set_source(client, data->dsp_src);
de533ccf 213 /* set prescales */
7e8b09ea 214
de533ccf
HV
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);
7e8b09ea
HV
220}
221
8a4b275f
HV
222/* Set audio mode. Note that the pre-'G' models do not support BTSC+SAP,
223 nor do they support stereo BTSC. */
044f324f 224static void msp3400c_set_audmode(struct i2c_client *client)
7e8b09ea 225{
d52c7385
HV
226 static char *strmode[] = {
227 "mono", "stereo", "lang2", "lang1", "lang1+lang2"
228 };
76efd62f 229 struct msp_state *state = to_state(i2c_get_clientdata(client));
301e22d6 230 char *modestr = (state->audmode >= 0 && state->audmode < 5) ?
8a4b275f
HV
231 strmode[state->audmode] : "unknown";
232 int src = 0; /* channel source: FM/AM, nicam or SCART */
7061561e 233 int audmode = state->audmode;
7e8b09ea
HV
234
235 if (state->opmode == OPMODE_AUTOSELECT) {
236 /* this method would break everything, let's make sure
237 * it's never called
238 */
8a4b275f
HV
239 v4l_dbg(1, msp_debug, client,
240 "set_audmode called with mode=%d instead of set_source (ignored)\n",
241 state->audmode);
7e8b09ea
HV
242 return;
243 }
244
0020d3ef
HV
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. */
248
2eb606db
HV
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;
253 /* if bilingual */
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;
259 }
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;
0020d3ef 263 }
7061561e 264
7e8b09ea
HV
265 /* switch demodulator */
266 switch (state->mode) {
267 case MSP_MODE_FM_TERRA:
8a4b275f 268 v4l_dbg(1, msp_debug, client, "FM set_audmode: %s\n", modestr);
7061561e 269 switch (audmode) {
7e8b09ea
HV
270 case V4L2_TUNER_MODE_STEREO:
271 msp_write_dsp(client, 0x000e, 0x3001);
272 break;
273 case V4L2_TUNER_MODE_MONO:
274 case V4L2_TUNER_MODE_LANG1:
275 case V4L2_TUNER_MODE_LANG2:
301e22d6 276 case V4L2_TUNER_MODE_LANG1_LANG2:
7e8b09ea
HV
277 msp_write_dsp(client, 0x000e, 0x3000);
278 break;
279 }
280 break;
281 case MSP_MODE_FM_SAT:
8a4b275f 282 v4l_dbg(1, msp_debug, client, "SAT set_audmode: %s\n", modestr);
7061561e 283 switch (audmode) {
7e8b09ea 284 case V4L2_TUNER_MODE_MONO:
8a4b275f 285 msp3400c_set_carrier(client, MSP_CARRIER(6.5), MSP_CARRIER(6.5));
7e8b09ea
HV
286 break;
287 case V4L2_TUNER_MODE_STEREO:
301e22d6 288 case V4L2_TUNER_MODE_LANG1_LANG2:
8a4b275f 289 msp3400c_set_carrier(client, MSP_CARRIER(7.2), MSP_CARRIER(7.02));
7e8b09ea
HV
290 break;
291 case V4L2_TUNER_MODE_LANG1:
8a4b275f 292 msp3400c_set_carrier(client, MSP_CARRIER(7.38), MSP_CARRIER(7.02));
7e8b09ea
HV
293 break;
294 case V4L2_TUNER_MODE_LANG2:
8a4b275f 295 msp3400c_set_carrier(client, MSP_CARRIER(7.38), MSP_CARRIER(7.02));
7e8b09ea
HV
296 break;
297 }
298 break;
299 case MSP_MODE_FM_NICAM1:
300 case MSP_MODE_FM_NICAM2:
301 case MSP_MODE_AM_NICAM:
d52c7385
HV
302 v4l_dbg(1, msp_debug, client,
303 "NICAM set_audmode: %s\n", modestr);
7e8b09ea 304 if (state->nicam_on)
8a4b275f 305 src = 0x0100; /* NICAM */
7e8b09ea
HV
306 break;
307 case MSP_MODE_BTSC:
d52c7385
HV
308 v4l_dbg(1, msp_debug, client,
309 "BTSC set_audmode: %s\n", modestr);
7e8b09ea
HV
310 break;
311 case MSP_MODE_EXTERN:
d52c7385
HV
312 v4l_dbg(1, msp_debug, client,
313 "extern set_audmode: %s\n", modestr);
8a4b275f 314 src = 0x0200; /* SCART */
7e8b09ea
HV
315 break;
316 case MSP_MODE_FM_RADIO:
d52c7385
HV
317 v4l_dbg(1, msp_debug, client,
318 "FM-Radio set_audmode: %s\n", modestr);
7e8b09ea
HV
319 break;
320 default:
8a4b275f 321 v4l_dbg(1, msp_debug, client, "mono set_audmode\n");
7e8b09ea
HV
322 return;
323 }
324
325 /* switch audio */
0020d3ef 326 v4l_dbg(1, msp_debug, client, "set audmode %d\n", audmode);
7061561e 327 switch (audmode) {
7e8b09ea 328 case V4L2_TUNER_MODE_STEREO:
301e22d6 329 case V4L2_TUNER_MODE_LANG1_LANG2:
8a4b275f 330 src |= 0x0020;
7e8b09ea
HV
331 break;
332 case V4L2_TUNER_MODE_MONO:
333 if (state->mode == MSP_MODE_AM_NICAM) {
f167cb4e 334 v4l_dbg(1, msp_debug, client, "switching to AM mono\n");
7e8b09ea
HV
335 /* AM mono decoding is handled by tuner, not MSP chip */
336 /* SCART switching control register */
337 msp_set_scart(client, SCART_MONO, 0);
338 src = 0x0200;
339 break;
340 }
8a4b275f
HV
341 if (state->rxsubchans & V4L2_TUNER_SUB_STEREO)
342 src = 0x0030;
343 break;
7e8b09ea 344 case V4L2_TUNER_MODE_LANG1:
7e8b09ea
HV
345 break;
346 case V4L2_TUNER_MODE_LANG2:
8a4b275f 347 src |= 0x0010;
7e8b09ea
HV
348 break;
349 }
d52c7385
HV
350 v4l_dbg(1, msp_debug, client,
351 "set_audmode final source/matrix = 0x%x\n", src);
7e8b09ea 352
8a4b275f 353 msp_set_source(client, src);
7e8b09ea
HV
354}
355
356static void msp3400c_print_mode(struct i2c_client *client)
357{
76efd62f 358 struct msp_state *state = to_state(i2c_get_clientdata(client));
7e8b09ea 359
d52c7385
HV
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);
364 else
365 v4l_dbg(1, msp_debug, client,
366 "main sound carrier: %d.%03d MHz\n",
367 state->main / 910000, (state->main / 910) % 1000);
7e8b09ea 368 if (state->mode == MSP_MODE_FM_NICAM1 || state->mode == MSP_MODE_FM_NICAM2)
d52c7385
HV
369 v4l_dbg(1, msp_debug, client,
370 "NICAM/FM carrier : %d.%03d MHz\n",
371 state->second / 910000, (state->second/910) % 1000);
7e8b09ea 372 if (state->mode == MSP_MODE_AM_NICAM)
d52c7385
HV
373 v4l_dbg(1, msp_debug, client,
374 "NICAM/AM carrier : %d.%03d MHz\n",
375 state->second / 910000, (state->second / 910) % 1000);
7e8b09ea 376 if (state->mode == MSP_MODE_FM_TERRA && state->main != state->second) {
d52c7385
HV
377 v4l_dbg(1, msp_debug, client,
378 "FM-stereo carrier : %d.%03d MHz\n",
379 state->second / 910000, (state->second / 910) % 1000);
7e8b09ea
HV
380 }
381}
382
383/* ----------------------------------------------------------------------- */
384
8a4b275f 385static int msp3400c_detect_stereo(struct i2c_client *client)
7e8b09ea 386{
76efd62f 387 struct msp_state *state = to_state(i2c_get_clientdata(client));
7e8b09ea
HV
388 int val;
389 int rxsubchans = state->rxsubchans;
8a4b275f 390 int newnicam = state->nicam_on;
7e8b09ea
HV
391 int update = 0;
392
393 switch (state->mode) {
394 case MSP_MODE_FM_TERRA:
395 val = msp_read_dsp(client, 0x18);
396 if (val > 32767)
397 val -= 65536;
d52c7385
HV
398 v4l_dbg(2, msp_debug, client,
399 "stereo detect register: %d\n", val);
3bbe5a83 400 if (val > 8192) {
8a4b275f 401 rxsubchans = V4L2_TUNER_SUB_STEREO;
7e8b09ea
HV
402 } else if (val < -4096) {
403 rxsubchans = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
404 } else {
405 rxsubchans = V4L2_TUNER_SUB_MONO;
406 }
407 newnicam = 0;
408 break;
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);
f167cb4e 413 v4l_dbg(2, msp_debug, client, "nicam sync=%d, mode=%d\n",
7e8b09ea
HV
414 val & 1, (val & 0x1e) >> 1);
415
416 if (val & 1) {
417 /* nicam synced */
418 switch ((val & 0x1e) >> 1) {
419 case 0:
420 case 8:
421 rxsubchans = V4L2_TUNER_SUB_STEREO;
422 break;
423 case 1:
424 case 9:
8a4b275f 425 rxsubchans = V4L2_TUNER_SUB_MONO;
7e8b09ea
HV
426 break;
427 case 2:
428 case 10:
8a4b275f 429 rxsubchans = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
7e8b09ea
HV
430 break;
431 default:
432 rxsubchans = V4L2_TUNER_SUB_MONO;
433 break;
434 }
435 newnicam = 1;
436 } else {
437 newnicam = 0;
438 rxsubchans = V4L2_TUNER_SUB_MONO;
439 }
440 break;
7e8b09ea
HV
441 }
442 if (rxsubchans != state->rxsubchans) {
443 update = 1;
d52c7385
HV
444 v4l_dbg(1, msp_debug, client,
445 "watch: rxsubchans %02x => %02x\n",
8a4b275f 446 state->rxsubchans, rxsubchans);
7e8b09ea
HV
447 state->rxsubchans = rxsubchans;
448 }
449 if (newnicam != state->nicam_on) {
450 update = 1;
f167cb4e 451 v4l_dbg(1, msp_debug, client, "watch: nicam %d => %d\n",
8a4b275f 452 state->nicam_on, newnicam);
7e8b09ea
HV
453 state->nicam_on = newnicam;
454 }
455 return update;
456}
457
458/*
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
461 */
462/* stereo/multilang monitoring */
463static void watch_stereo(struct i2c_client *client)
464{
76efd62f 465 struct msp_state *state = to_state(i2c_get_clientdata(client));
7e8b09ea 466
d52c7385 467 if (msp_detect_stereo(client))
de533ccf 468 msp_set_audmode(client);
7e8b09ea 469
f167cb4e 470 if (msp_once)
7e8b09ea
HV
471 state->watch_stereo = 0;
472}
473
474int msp3400c_thread(void *data)
475{
476 struct i2c_client *client = data;
76efd62f 477 struct msp_state *state = to_state(i2c_get_clientdata(client));
7e8b09ea 478 struct msp3400c_carrier_detect *cd;
d52c7385 479 int count, max1, max2, val1, val2, val, i;
7e8b09ea 480
f167cb4e 481 v4l_dbg(1, msp_debug, client, "msp3400 daemon started\n");
83144186 482 set_freezable();
7e8b09ea 483 for (;;) {
f167cb4e 484 v4l_dbg(2, msp_debug, client, "msp3400 thread: sleep\n");
7e8b09ea 485 msp_sleep(state, -1);
f167cb4e 486 v4l_dbg(2, msp_debug, client, "msp3400 thread: wakeup\n");
7e8b09ea 487
d52c7385 488restart:
8a4b275f 489 v4l_dbg(2, msp_debug, client, "thread: restart scan\n");
7e8b09ea
HV
490 state->restart = 0;
491 if (kthread_should_stop())
492 break;
493
7560d7a4 494 if (state->radio || MSP_MODE_EXTERN == state->mode) {
7e8b09ea 495 /* no carrier scan, just unmute */
d52c7385
HV
496 v4l_dbg(1, msp_debug, client,
497 "thread: no carrier scan\n");
3bbe5a83 498 state->scan_in_progress = 0;
7e8b09ea
HV
499 msp_set_audio(client);
500 continue;
501 }
502
3bbe5a83
HV
503 /* mute audio */
504 state->scan_in_progress = 1;
505 msp_set_audio(client);
0020d3ef 506
de533ccf 507 msp3400c_set_mode(client, MSP_MODE_AM_DETECT);
7e8b09ea
HV
508 val1 = val2 = 0;
509 max1 = max2 = -1;
510 state->watch_stereo = 0;
8a4b275f 511 state->nicam_on = 0;
7e8b09ea 512
3bbe5a83 513 /* wait for tuner to settle down after a channel change */
8a4b275f 514 if (msp_sleep(state, 200))
7e8b09ea
HV
515 goto restart;
516
517 /* carrier detect pass #1 -- main carrier */
518 cd = msp3400c_carrier_detect_main;
519 count = ARRAY_SIZE(msp3400c_carrier_detect_main);
520
f167cb4e 521 if (msp_amsound && (state->v4l2_std & V4L2_STD_SECAM)) {
7e8b09ea
HV
522 /* autodetect doesn't work well with AM ... */
523 max1 = 3;
524 count = 0;
f167cb4e 525 v4l_dbg(1, msp_debug, client, "AM sound override\n");
7e8b09ea
HV
526 }
527
d52c7385
HV
528 for (i = 0; i < count; i++) {
529 msp3400c_set_carrier(client, cd[i].cdo, cd[i].cdo);
530 if (msp_sleep(state, 100))
7e8b09ea
HV
531 goto restart;
532 val = msp_read_dsp(client, 0x1b);
533 if (val > 32767)
534 val -= 65536;
535 if (val1 < val)
d52c7385
HV
536 val1 = val, max1 = i;
537 v4l_dbg(1, msp_debug, client,
538 "carrier1 val: %5d / %s\n", val, cd[i].name);
7e8b09ea
HV
539 }
540
541 /* carrier detect pass #2 -- second (stereo) carrier */
542 switch (max1) {
543 case 1: /* 5.5 */
544 cd = msp3400c_carrier_detect_55;
545 count = ARRAY_SIZE(msp3400c_carrier_detect_55);
546 break;
547 case 3: /* 6.5 */
548 cd = msp3400c_carrier_detect_65;
549 count = ARRAY_SIZE(msp3400c_carrier_detect_65);
550 break;
551 case 0: /* 4.5 */
552 case 2: /* 6.0 */
553 default:
554 cd = NULL;
555 count = 0;
556 break;
557 }
558
f167cb4e 559 if (msp_amsound && (state->v4l2_std & V4L2_STD_SECAM)) {
7e8b09ea
HV
560 /* autodetect doesn't work well with AM ... */
561 cd = NULL;
562 count = 0;
563 max2 = 0;
564 }
d52c7385
HV
565 for (i = 0; i < count; i++) {
566 msp3400c_set_carrier(client, cd[i].cdo, cd[i].cdo);
567 if (msp_sleep(state, 100))
7e8b09ea
HV
568 goto restart;
569 val = msp_read_dsp(client, 0x1b);
570 if (val > 32767)
571 val -= 65536;
572 if (val2 < val)
d52c7385
HV
573 val2 = val, max2 = i;
574 v4l_dbg(1, msp_debug, client,
575 "carrier2 val: %5d / %s\n", val, cd[i].name);
7e8b09ea
HV
576 }
577
578 /* program the msp3400 according to the results */
8a4b275f 579 state->main = msp3400c_carrier_detect_main[max1].cdo;
7e8b09ea
HV
580 switch (max1) {
581 case 1: /* 5.5 */
582 if (max2 == 0) {
583 /* B/G FM-stereo */
584 state->second = msp3400c_carrier_detect_55[max2].cdo;
8a4b275f 585 msp3400c_set_mode(client, MSP_MODE_FM_TERRA);
7e8b09ea 586 state->watch_stereo = 1;
7560d7a4 587 } else if (max2 == 1 && state->has_nicam) {
7e8b09ea
HV
588 /* B/G NICAM */
589 state->second = msp3400c_carrier_detect_55[max2].cdo;
8a4b275f 590 msp3400c_set_mode(client, MSP_MODE_FM_NICAM1);
7e8b09ea 591 state->nicam_on = 1;
7e8b09ea
HV
592 state->watch_stereo = 1;
593 } else {
594 goto no_second;
595 }
596 break;
597 case 2: /* 6.0 */
598 /* PAL I NICAM */
599 state->second = MSP_CARRIER(6.552);
8a4b275f 600 msp3400c_set_mode(client, MSP_MODE_FM_NICAM2);
7e8b09ea 601 state->nicam_on = 1;
7e8b09ea
HV
602 state->watch_stereo = 1;
603 break;
604 case 3: /* 6.5 */
605 if (max2 == 1 || max2 == 2) {
606 /* D/K FM-stereo */
607 state->second = msp3400c_carrier_detect_65[max2].cdo;
8a4b275f 608 msp3400c_set_mode(client, MSP_MODE_FM_TERRA);
7e8b09ea 609 state->watch_stereo = 1;
5af0c8f6 610 } else if (max2 == 0 && (state->v4l2_std & V4L2_STD_SECAM)) {
7e8b09ea
HV
611 /* L NICAM or AM-mono */
612 state->second = msp3400c_carrier_detect_65[max2].cdo;
8a4b275f 613 msp3400c_set_mode(client, MSP_MODE_AM_NICAM);
7e8b09ea 614 state->watch_stereo = 1;
7560d7a4 615 } else if (max2 == 0 && state->has_nicam) {
7e8b09ea
HV
616 /* D/K NICAM */
617 state->second = msp3400c_carrier_detect_65[max2].cdo;
8a4b275f 618 msp3400c_set_mode(client, MSP_MODE_FM_NICAM1);
7e8b09ea 619 state->nicam_on = 1;
7e8b09ea
HV
620 state->watch_stereo = 1;
621 } else {
622 goto no_second;
623 }
624 break;
625 case 0: /* 4.5 */
626 default:
d52c7385 627no_second:
7e8b09ea 628 state->second = msp3400c_carrier_detect_main[max1].cdo;
8a4b275f 629 msp3400c_set_mode(client, MSP_MODE_FM_TERRA);
7e8b09ea
HV
630 break;
631 }
0020d3ef 632 msp3400c_set_carrier(client, state->second, state->main);
7e8b09ea 633
3bbe5a83
HV
634 /* unmute */
635 state->scan_in_progress = 0;
8a4b275f 636 msp3400c_set_audmode(client);
3bbe5a83 637 msp_set_audio(client);
7e8b09ea 638
f167cb4e 639 if (msp_debug)
7e8b09ea
HV
640 msp3400c_print_mode(client);
641
8a4b275f
HV
642 /* monitor tv audio mode, the first time don't wait
643 so long to get a quick stereo/bilingual result */
3bbe5a83 644 count = 3;
7e8b09ea 645 while (state->watch_stereo) {
3bbe5a83 646 if (msp_sleep(state, count ? 1000 : 5000))
7e8b09ea 647 goto restart;
d52c7385
HV
648 if (count)
649 count--;
3bbe5a83 650 watch_stereo(client);
7e8b09ea
HV
651 }
652 }
f167cb4e 653 v4l_dbg(1, msp_debug, client, "thread: exit\n");
7e8b09ea
HV
654 return 0;
655}
656
657
658int msp3410d_thread(void *data)
659{
660 struct i2c_client *client = data;
76efd62f 661 struct msp_state *state = to_state(i2c_get_clientdata(client));
0020d3ef 662 int val, i, std, count;
7e8b09ea 663
f167cb4e 664 v4l_dbg(1, msp_debug, client, "msp3410 daemon started\n");
83144186 665 set_freezable();
7e8b09ea 666 for (;;) {
f167cb4e 667 v4l_dbg(2, msp_debug, client, "msp3410 thread: sleep\n");
d52c7385 668 msp_sleep(state, -1);
f167cb4e 669 v4l_dbg(2, msp_debug, client, "msp3410 thread: wakeup\n");
7e8b09ea 670
d52c7385 671restart:
8a4b275f 672 v4l_dbg(2, msp_debug, client, "thread: restart scan\n");
7e8b09ea
HV
673 state->restart = 0;
674 if (kthread_should_stop())
675 break;
676
677 if (state->mode == MSP_MODE_EXTERN) {
678 /* no carrier scan needed, just unmute */
d52c7385
HV
679 v4l_dbg(1, msp_debug, client,
680 "thread: no carrier scan\n");
3bbe5a83 681 state->scan_in_progress = 0;
7e8b09ea
HV
682 msp_set_audio(client);
683 continue;
684 }
685
3bbe5a83
HV
686 /* mute audio */
687 state->scan_in_progress = 1;
688 msp_set_audio(client);
7e8b09ea 689
de533ccf 690 /* start autodetect. Note: autodetect is not supported for
d52c7385
HV
691 NTSC-M and radio, hence we force the standard in those
692 cases. */
5af0c8f6
HV
693 if (state->radio)
694 std = 0x40;
d312a46e 695 else
5af0c8f6 696 std = (state->v4l2_std & V4L2_STD_NTSC) ? 0x20 : 1;
7e8b09ea 697 state->watch_stereo = 0;
8a4b275f 698 state->nicam_on = 0;
7e8b09ea 699
3bbe5a83
HV
700 /* wait for tuner to settle down after a channel change */
701 if (msp_sleep(state, 200))
702 goto restart;
703
f167cb4e 704 if (msp_debug)
d52c7385
HV
705 v4l_dbg(2, msp_debug, client,
706 "setting standard: %s (0x%04x)\n",
707 msp_standard_std_name(std), std);
7e8b09ea
HV
708
709 if (std != 1) {
710 /* programmed some specific mode */
711 val = std;
712 } else {
713 /* triggered autodetect */
5af0c8f6 714 msp_write_dem(client, 0x20, std);
7e8b09ea 715 for (;;) {
d312a46e 716 if (msp_sleep(state, 100))
7e8b09ea
HV
717 goto restart;
718
719 /* check results */
720 val = msp_read_dem(client, 0x7e);
721 if (val < 0x07ff)
722 break;
d52c7385
HV
723 v4l_dbg(2, msp_debug, client,
724 "detection still in progress\n");
7e8b09ea
HV
725 }
726 }
5af0c8f6
HV
727 for (i = 0; msp_stdlist[i].name != NULL; i++)
728 if (msp_stdlist[i].retval == val)
7e8b09ea 729 break;
f167cb4e 730 v4l_dbg(1, msp_debug, client, "current standard: %s (0x%04x)\n",
5af0c8f6
HV
731 msp_standard_std_name(val), val);
732 state->main = msp_stdlist[i].main;
733 state->second = msp_stdlist[i].second;
734 state->std = val;
3bbe5a83 735 state->rxsubchans = V4L2_TUNER_SUB_MONO;
5af0c8f6 736
d52c7385
HV
737 if (msp_amsound && !state->radio &&
738 (state->v4l2_std & V4L2_STD_SECAM) && (val != 0x0009)) {
7e8b09ea 739 /* autodetection has failed, let backup */
f167cb4e 740 v4l_dbg(1, msp_debug, client, "autodetection failed,"
5af0c8f6 741 " switching to backup standard: %s (0x%04x)\n",
d52c7385
HV
742 msp_stdlist[8].name ?
743 msp_stdlist[8].name : "unknown", val);
de533ccf 744 state->std = val = 0x0009;
7e8b09ea
HV
745 msp_write_dem(client, 0x20, val);
746 }
747
7e8b09ea
HV
748 /* set stereo */
749 switch (val) {
750 case 0x0008: /* B/G NICAM */
751 case 0x000a: /* I NICAM */
de533ccf
HV
752 case 0x000b: /* D/K NICAM */
753 if (val == 0x000a)
7e8b09ea 754 state->mode = MSP_MODE_FM_NICAM2;
de533ccf
HV
755 else
756 state->mode = MSP_MODE_FM_NICAM1;
7e8b09ea 757 /* just turn on stereo */
7e8b09ea
HV
758 state->nicam_on = 1;
759 state->watch_stereo = 1;
7e8b09ea
HV
760 break;
761 case 0x0009:
762 state->mode = MSP_MODE_AM_NICAM;
7e8b09ea 763 state->nicam_on = 1;
7e8b09ea
HV
764 state->watch_stereo = 1;
765 break;
766 case 0x0020: /* BTSC */
8a4b275f 767 /* The pre-'G' models only have BTSC-mono */
7e8b09ea 768 state->mode = MSP_MODE_BTSC;
7e8b09ea
HV
769 break;
770 case 0x0040: /* FM radio */
7560d7a4 771 state->mode = MSP_MODE_FM_RADIO;
7e8b09ea 772 state->rxsubchans = V4L2_TUNER_SUB_STEREO;
7560d7a4 773 /* not needed in theory if we have radio, but
7e8b09ea 774 short programming enables carrier mute */
8a4b275f
HV
775 msp3400c_set_mode(client, MSP_MODE_FM_RADIO);
776 msp3400c_set_carrier(client, MSP_CARRIER(10.7),
7e8b09ea 777 MSP_CARRIER(10.7));
7e8b09ea 778 break;
de533ccf 779 case 0x0002:
7e8b09ea
HV
780 case 0x0003:
781 case 0x0004:
782 case 0x0005:
5af0c8f6 783 state->mode = MSP_MODE_FM_TERRA;
7e8b09ea
HV
784 state->watch_stereo = 1;
785 break;
786 }
787
de533ccf
HV
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 */
793
5af0c8f6
HV
794 if (state->has_i2s_conf)
795 msp_write_dem(client, 0x40, state->i2s_mode);
7e8b09ea 796
3bbe5a83 797 /* unmute */
8a4b275f 798 msp3400c_set_audmode(client);
3bbe5a83
HV
799 state->scan_in_progress = 0;
800 msp_set_audio(client);
8a4b275f
HV
801
802 /* monitor tv audio mode, the first time don't wait
803 so long to get a quick stereo/bilingual result */
3bbe5a83 804 count = 3;
7e8b09ea 805 while (state->watch_stereo) {
3bbe5a83 806 if (msp_sleep(state, count ? 1000 : 5000))
8a4b275f 807 goto restart;
d52c7385
HV
808 if (count)
809 count--;
3bbe5a83 810 watch_stereo(client);
7e8b09ea
HV
811 }
812 }
f167cb4e 813 v4l_dbg(1, msp_debug, client, "thread: exit\n");
7e8b09ea
HV
814 return 0;
815}
816
817/* ----------------------------------------------------------------------- */
818
de533ccf
HV
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
823 */
7e8b09ea 824
9a80a93d
HV
825static int msp34xxg_modus(struct i2c_client *client)
826{
76efd62f 827 struct msp_state *state = to_state(i2c_get_clientdata(client));
9a80a93d
HV
828
829 if (state->radio) {
830 v4l_dbg(1, msp_debug, client, "selected radio modus\n");
831 return 0x0001;
832 }
9a80a93d
HV
833 if (state->v4l2_std == V4L2_STD_NTSC_M_JP) {
834 v4l_dbg(1, msp_debug, client, "selected M (EIA-J) modus\n");
835 return 0x4001;
836 }
837 if (state->v4l2_std == V4L2_STD_NTSC_M_KR) {
838 v4l_dbg(1, msp_debug, client, "selected M (A2) modus\n");
839 return 0x0001;
840 }
2fd3c14c
HV
841 if (state->v4l2_std == V4L2_STD_SECAM_L) {
842 v4l_dbg(1, msp_debug, client, "selected SECAM-L modus\n");
843 return 0x6001;
844 }
9a80a93d
HV
845 if (state->v4l2_std & V4L2_STD_MN) {
846 v4l_dbg(1, msp_debug, client, "selected M (BTSC) modus\n");
847 return 0x2001;
848 }
2fd3c14c 849 return 0x7001;
9a80a93d
HV
850}
851
2474ed44
HV
852static void msp34xxg_set_source(struct i2c_client *client, u16 reg, int in)
853 {
76efd62f 854 struct msp_state *state = to_state(i2c_get_clientdata(client));
2474ed44
HV
855 int source, matrix;
856
857 switch (state->audmode) {
858 case V4L2_TUNER_MODE_MONO:
859 source = 0; /* mono only */
860 matrix = 0x30;
861 break;
2474ed44
HV
862 case V4L2_TUNER_MODE_LANG2:
863 source = 4; /* stereo or B */
864 matrix = 0x10;
865 break;
301e22d6 866 case V4L2_TUNER_MODE_LANG1_LANG2:
2474ed44
HV
867 source = 1; /* stereo or A|B */
868 matrix = 0x20;
869 break;
0020d3ef 870 case V4L2_TUNER_MODE_LANG1:
0020d3ef
HV
871 source = 3; /* stereo or A */
872 matrix = 0x00;
873 break;
b331def2
HV
874 case V4L2_TUNER_MODE_STEREO:
875 default:
876 source = 3; /* stereo or A */
877 matrix = 0x20;
878 break;
2474ed44
HV
879 }
880
07151724 881 if (in == MSP_DSP_IN_TUNER)
2474ed44
HV
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. */
07151724 885 else if (in >= MSP_DSP_IN_MAIN_AVC && state->has_dolby_pro_logic)
2474ed44
HV
886 source = ((in + 1) << 8) | matrix;
887 else
888 source = (in << 8) | matrix;
889
d52c7385
HV
890 v4l_dbg(1, msp_debug, client,
891 "set source to %d (0x%x) for output %02x\n", in, source, reg);
2474ed44
HV
892 msp_write_dsp(client, reg, source);
893}
894
895static void msp34xxg_set_sources(struct i2c_client *client)
896{
76efd62f 897 struct msp_state *state = to_state(i2c_get_clientdata(client));
5325b427 898 u32 in = state->route_in;
2474ed44
HV
899
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);
0020d3ef 905 if (state->has_scart2_out)
2474ed44
HV
906 msp34xxg_set_source(client, 0x0041, (in >> 16) & 0xf);
907 msp34xxg_set_source(client, 0x000b, (in >> 20) & 0xf);
908}
909
de533ccf
HV
910/* (re-)initialize the msp34xxg */
911static void msp34xxg_reset(struct i2c_client *client)
7e8b09ea 912{
76efd62f 913 struct msp_state *state = to_state(i2c_get_clientdata(client));
5325b427 914 int tuner = (state->route_in >> 3) & 1;
de533ccf 915 int modus;
7e8b09ea 916
de533ccf
HV
917 /* initialize std to 1 (autodetect) to signal that no standard is
918 selected yet. */
919 state->std = 1;
920
921 msp_reset(client);
7e8b09ea 922
5af0c8f6
HV
923 if (state->has_i2s_conf)
924 msp_write_dem(client, 0x40, state->i2s_mode);
7e8b09ea
HV
925
926 /* step-by-step initialisation, as described in the manual */
9a80a93d 927 modus = msp34xxg_modus(client);
2474ed44 928 modus |= tuner ? 0x100 : 0;
de533ccf 929 msp_write_dem(client, 0x30, modus);
7e8b09ea
HV
930
931 /* write the dsps that may have an influence on
932 standard/audio autodetection right now */
2474ed44 933 msp34xxg_set_sources(client);
7e8b09ea 934
de533ccf
HV
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 */
7e8b09ea 939
de533ccf
HV
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
945 *
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.
949 */
950 msp_write_dem(client, 0x22, msp_stereo_thresh);
7e8b09ea
HV
951}
952
953int msp34xxg_thread(void *data)
954{
955 struct i2c_client *client = data;
76efd62f 956 struct msp_state *state = to_state(i2c_get_clientdata(client));
de533ccf 957 int val, i;
7e8b09ea 958
f167cb4e 959 v4l_dbg(1, msp_debug, client, "msp34xxg daemon started\n");
83144186 960 set_freezable();
7e8b09ea 961 for (;;) {
f167cb4e 962 v4l_dbg(2, msp_debug, client, "msp34xxg thread: sleep\n");
7e8b09ea 963 msp_sleep(state, -1);
f167cb4e 964 v4l_dbg(2, msp_debug, client, "msp34xxg thread: wakeup\n");
7e8b09ea 965
d52c7385 966restart:
f167cb4e 967 v4l_dbg(1, msp_debug, client, "thread: restart scan\n");
7e8b09ea
HV
968 state->restart = 0;
969 if (kthread_should_stop())
970 break;
971
2eb606db
HV
972 if (state->mode == MSP_MODE_EXTERN) {
973 /* no carrier scan needed, just unmute */
d52c7385
HV
974 v4l_dbg(1, msp_debug, client,
975 "thread: no carrier scan\n");
2eb606db
HV
976 state->scan_in_progress = 0;
977 msp_set_audio(client);
978 continue;
979 }
980
7e8b09ea
HV
981 /* setup the chip*/
982 msp34xxg_reset(client);
de98cdaf
HV
983 state->std = state->radio ? 0x40 :
984 (state->force_btsc && msp_standard == 1) ? 32 : msp_standard;
de533ccf 985 msp_write_dem(client, 0x20, state->std);
de98cdaf 986 /* start autodetect */
e335fada
HV
987 if (state->std != 1)
988 goto unmute;
7e8b09ea
HV
989
990 /* watch autodetect */
d52c7385
HV
991 v4l_dbg(1, msp_debug, client,
992 "started autodetect, waiting for result\n");
7e8b09ea
HV
993 for (i = 0; i < 10; i++) {
994 if (msp_sleep(state, 100))
995 goto restart;
996
997 /* check results */
998 val = msp_read_dem(client, 0x7e);
999 if (val < 0x07ff) {
de533ccf 1000 state->std = val;
7e8b09ea
HV
1001 break;
1002 }
d52c7385
HV
1003 v4l_dbg(2, msp_debug, client,
1004 "detection still in progress\n");
7e8b09ea 1005 }
de533ccf 1006 if (state->std == 1) {
d52c7385
HV
1007 v4l_dbg(1, msp_debug, client,
1008 "detection still in progress after 10 tries. giving up.\n");
7e8b09ea
HV
1009 continue;
1010 }
1011
d52c7385
HV
1012unmute:
1013 v4l_dbg(1, msp_debug, client,
1014 "detected standard: %s (0x%04x)\n",
de533ccf 1015 msp_standard_std_name(state->std), state->std);
7e8b09ea 1016
2eb606db
HV
1017 if (state->std == 9) {
1018 /* AM NICAM mode */
1019 msp_write_dsp(client, 0x0e, 0x7c00);
1020 }
1021
7e8b09ea
HV
1022 /* unmute: dispatch sound to scart output, set scart volume */
1023 msp_set_audio(client);
1024
1025 /* restore ACB */
1026 if (msp_write_dsp(client, 0x13, state->acb))
1027 return -1;
1028
de533ccf
HV
1029 /* the periodic stereo/SAP check is only relevant for
1030 the 0x20 standard (BTSC) */
1031 if (state->std != 0x20)
1032 continue;
1033
1034 state->watch_stereo = 1;
1035
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))
1042 goto restart;
1043 }
7e8b09ea 1044 }
f167cb4e 1045 v4l_dbg(1, msp_debug, client, "thread: exit\n");
7e8b09ea
HV
1046 return 0;
1047}
1048
de533ccf 1049static int msp34xxg_detect_stereo(struct i2c_client *client)
7e8b09ea 1050{
76efd62f 1051 struct msp_state *state = to_state(i2c_get_clientdata(client));
7e8b09ea
HV
1052 int status = msp_read_dem(client, 0x0200);
1053 int is_bilingual = status & 0x100;
1054 int is_stereo = status & 0x40;
de533ccf 1055 int oldrx = state->rxsubchans;
7e8b09ea 1056
b331def2
HV
1057 if (state->mode == MSP_MODE_EXTERN)
1058 return 0;
1059
7e8b09ea
HV
1060 state->rxsubchans = 0;
1061 if (is_stereo)
8a4b275f 1062 state->rxsubchans = V4L2_TUNER_SUB_STEREO;
7e8b09ea 1063 else
8a4b275f 1064 state->rxsubchans = V4L2_TUNER_SUB_MONO;
7e8b09ea 1065 if (is_bilingual) {
de533ccf
HV
1066 if (state->std == 0x20)
1067 state->rxsubchans |= V4L2_TUNER_SUB_SAP;
1068 else
d52c7385
HV
1069 state->rxsubchans =
1070 V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
7e8b09ea 1071 }
d52c7385
HV
1072 v4l_dbg(1, msp_debug, client,
1073 "status=0x%x, stereo=%d, bilingual=%d -> rxsubchans=%d\n",
7e8b09ea 1074 status, is_stereo, is_bilingual, state->rxsubchans);
de533ccf 1075 return (oldrx != state->rxsubchans);
7e8b09ea
HV
1076}
1077
044f324f 1078static void msp34xxg_set_audmode(struct i2c_client *client)
7e8b09ea 1079{
76efd62f 1080 struct msp_state *state = to_state(i2c_get_clientdata(client));
7e8b09ea 1081
de533ccf
HV
1082 if (state->std == 0x20) {
1083 if ((state->rxsubchans & V4L2_TUNER_SUB_SAP) &&
cc33668b 1084 (state->audmode == V4L2_TUNER_MODE_LANG1_LANG2 ||
de533ccf
HV
1085 state->audmode == V4L2_TUNER_MODE_LANG2)) {
1086 msp_write_dem(client, 0x20, 0x21);
1087 } else {
1088 msp_write_dem(client, 0x20, 0x20);
1089 }
1090 }
1091
2474ed44 1092 msp34xxg_set_sources(client);
7e8b09ea
HV
1093}
1094
8a4b275f
HV
1095void msp_set_audmode(struct i2c_client *client)
1096{
76efd62f 1097 struct msp_state *state = to_state(i2c_get_clientdata(client));
8a4b275f
HV
1098
1099 switch (state->opmode) {
1100 case OPMODE_MANUAL:
1101 case OPMODE_AUTODETECT:
8a4b275f
HV
1102 msp3400c_set_audmode(client);
1103 break;
1104 case OPMODE_AUTOSELECT:
1105 msp34xxg_set_audmode(client);
1106 break;
1107 }
1108}
1109
de533ccf 1110int msp_detect_stereo(struct i2c_client *client)
8a4b275f 1111{
76efd62f 1112 struct msp_state *state = to_state(i2c_get_clientdata(client));
8a4b275f
HV
1113
1114 switch (state->opmode) {
1115 case OPMODE_MANUAL:
1116 case OPMODE_AUTODETECT:
de533ccf 1117 return msp3400c_detect_stereo(client);
8a4b275f 1118 case OPMODE_AUTOSELECT:
de533ccf 1119 return msp34xxg_detect_stereo(client);
8a4b275f 1120 }
de533ccf 1121 return 0;
8a4b275f
HV
1122}
1123