V4L/DVB (3423): CodingStyle fixes.
[deliverable/linux.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
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20
21
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>
32#include "msp3400.h"
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)" },
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 */
61};
62
63static struct msp3400c_init_data_dem {
64 int fir1[6];
65 int fir2[6];
66 int cdo1;
67 int cdo2;
68 int ad_cv;
69 int mode_reg;
70 int dsp_src;
71 int dsp_matrix;
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
83 },{ /* FM Radio */
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
93 },{ /* Sat FM-mono */
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
113 },
114};
115
116struct msp3400c_carrier_detect {
117 int cdo;
118 char *name;
119};
120
121static struct msp3400c_carrier_detect msp3400c_carrier_detect_main[] = {
122 /* main carrier */
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" }
127};
128
129static struct msp3400c_carrier_detect msp3400c_carrier_detect_55[] = {
130 /* PAL B/G */
131 { MSP_CARRIER(5.7421875), "5.742 PAL B/G FM-stereo" },
132 { MSP_CARRIER(5.85), "5.85 PAL B/G NICAM" }
133};
134
135static 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" },
143};
144
145/* ------------------------------------------------------------------------ */
146
5af0c8f6 147const char *msp_standard_std_name(int std)
7e8b09ea
HV
148{
149 int i;
150
5af0c8f6
HV
151 for (i = 0; msp_stdlist[i].name != NULL; i++)
152 if (msp_stdlist[i].retval == std)
153 return msp_stdlist[i].name;
7e8b09ea
HV
154 return "unknown";
155}
156
157void msp3400c_setcarrier(struct i2c_client *client, int cdo1, int cdo2)
158{
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*/
164}
165
166void msp3400c_setmode(struct i2c_client *client, int type)
167{
168 struct msp_state *state = i2c_get_clientdata(client);
169 int i;
170
f167cb4e 171 v4l_dbg(1, msp_debug, client, "setmode: %d\n", type);
7e8b09ea 172 state->mode = type;
7e8b09ea
HV
173 state->rxsubchans = V4L2_TUNER_SUB_MONO;
174
175 msp_write_dem(client, 0x00bb, msp3400c_init_data[type].ad_cv);
176
177 for (i = 5; i >= 0; i--) /* fir 1 */
178 msp_write_dem(client, 0x0001, msp3400c_init_data[type].fir1[i]);
179
180 msp_write_dem(client, 0x0005, 0x0004); /* fir 2 */
181 msp_write_dem(client, 0x0005, 0x0040);
182 msp_write_dem(client, 0x0005, 0x0000);
183 for (i = 5; i >= 0; i--)
184 msp_write_dem(client, 0x0005, msp3400c_init_data[type].fir2[i]);
185
186 msp_write_dem(client, 0x0083, msp3400c_init_data[type].mode_reg);
187
188 msp3400c_setcarrier(client, msp3400c_init_data[type].cdo1,
189 msp3400c_init_data[type].cdo2);
190
191 msp_write_dem(client, 0x0056, 0); /*LOAD_REG_1/2*/
192
f167cb4e 193 if (msp_dolby) {
7e8b09ea
HV
194 msp_write_dsp(client, 0x0008, 0x0520); /* I2S1 */
195 msp_write_dsp(client, 0x0009, 0x0620); /* I2S2 */
196 msp_write_dsp(client, 0x000b, msp3400c_init_data[type].dsp_src);
197 } else {
198 msp_write_dsp(client, 0x0008, msp3400c_init_data[type].dsp_src);
199 msp_write_dsp(client, 0x0009, msp3400c_init_data[type].dsp_src);
200 msp_write_dsp(client, 0x000b, msp3400c_init_data[type].dsp_src);
201 }
202 msp_write_dsp(client, 0x000a, msp3400c_init_data[type].dsp_src);
203 msp_write_dsp(client, 0x000e, msp3400c_init_data[type].dsp_matrix);
204
7560d7a4 205 if (state->has_nicam) {
7e8b09ea
HV
206 /* nicam prescale */
207 msp_write_dsp(client, 0x0010, 0x5a00); /* was: 0x3000 */
208 }
209}
210
7e8b09ea 211/* turn on/off nicam + stereo */
53a72404 212void msp3400c_setstereo(struct i2c_client *client, int audmode)
7e8b09ea 213{
d312a46e 214 static char *strmode[] = { "mono", "stereo", "lang2", "lang1" };
7e8b09ea
HV
215 struct msp_state *state = i2c_get_clientdata(client);
216 int nicam = 0; /* channel source: FM/AM or nicam */
217 int src = 0;
218
219 if (state->opmode == OPMODE_AUTOSELECT) {
220 /* this method would break everything, let's make sure
221 * it's never called
222 */
f167cb4e 223 v4l_dbg(1, msp_debug, client, "setstereo called with mode=%d instead of set_source (ignored)\n",
53a72404 224 audmode);
7e8b09ea
HV
225 return;
226 }
227
228 /* switch demodulator */
229 switch (state->mode) {
230 case MSP_MODE_FM_TERRA:
53a72404 231 v4l_dbg(1, msp_debug, client, "FM setstereo: %s\n", strmode[audmode]);
d312a46e 232 msp3400c_setcarrier(client, state->second, state->main);
53a72404 233 switch (audmode) {
7e8b09ea
HV
234 case V4L2_TUNER_MODE_STEREO:
235 msp_write_dsp(client, 0x000e, 0x3001);
236 break;
237 case V4L2_TUNER_MODE_MONO:
238 case V4L2_TUNER_MODE_LANG1:
239 case V4L2_TUNER_MODE_LANG2:
240 msp_write_dsp(client, 0x000e, 0x3000);
241 break;
242 }
243 break;
244 case MSP_MODE_FM_SAT:
53a72404
HV
245 v4l_dbg(1, msp_debug, client, "SAT setstereo: %s\n", strmode[audmode]);
246 switch (audmode) {
7e8b09ea
HV
247 case V4L2_TUNER_MODE_MONO:
248 msp3400c_setcarrier(client, MSP_CARRIER(6.5), MSP_CARRIER(6.5));
249 break;
250 case V4L2_TUNER_MODE_STEREO:
251 msp3400c_setcarrier(client, MSP_CARRIER(7.2), MSP_CARRIER(7.02));
252 break;
253 case V4L2_TUNER_MODE_LANG1:
254 msp3400c_setcarrier(client, MSP_CARRIER(7.38), MSP_CARRIER(7.02));
255 break;
256 case V4L2_TUNER_MODE_LANG2:
257 msp3400c_setcarrier(client, MSP_CARRIER(7.38), MSP_CARRIER(7.02));
258 break;
259 }
260 break;
261 case MSP_MODE_FM_NICAM1:
262 case MSP_MODE_FM_NICAM2:
263 case MSP_MODE_AM_NICAM:
53a72404 264 v4l_dbg(1, msp_debug, client, "NICAM setstereo: %s\n",strmode[audmode]);
7e8b09ea
HV
265 msp3400c_setcarrier(client,state->second,state->main);
266 if (state->nicam_on)
267 nicam=0x0100;
268 break;
269 case MSP_MODE_BTSC:
53a72404 270 v4l_dbg(1, msp_debug, client, "BTSC setstereo: %s\n",strmode[audmode]);
7e8b09ea
HV
271 nicam=0x0300;
272 break;
273 case MSP_MODE_EXTERN:
53a72404 274 v4l_dbg(1, msp_debug, client, "extern setstereo: %s\n",strmode[audmode]);
7e8b09ea
HV
275 nicam = 0x0200;
276 break;
277 case MSP_MODE_FM_RADIO:
53a72404 278 v4l_dbg(1, msp_debug, client, "FM-Radio setstereo: %s\n",strmode[audmode]);
7e8b09ea
HV
279 break;
280 default:
f167cb4e 281 v4l_dbg(1, msp_debug, client, "mono setstereo\n");
7e8b09ea
HV
282 return;
283 }
284
285 /* switch audio */
53a72404 286 switch (audmode) {
7e8b09ea
HV
287 case V4L2_TUNER_MODE_STEREO:
288 src = 0x0020 | nicam;
289 break;
290 case V4L2_TUNER_MODE_MONO:
291 if (state->mode == MSP_MODE_AM_NICAM) {
f167cb4e 292 v4l_dbg(1, msp_debug, client, "switching to AM mono\n");
7e8b09ea
HV
293 /* AM mono decoding is handled by tuner, not MSP chip */
294 /* SCART switching control register */
295 msp_set_scart(client, SCART_MONO, 0);
296 src = 0x0200;
297 break;
298 }
299 case V4L2_TUNER_MODE_LANG1:
300 src = 0x0000 | nicam;
301 break;
302 case V4L2_TUNER_MODE_LANG2:
303 src = 0x0010 | nicam;
304 break;
305 }
f167cb4e 306 v4l_dbg(1, msp_debug, client, "setstereo final source/matrix = 0x%x\n", src);
7e8b09ea 307
f167cb4e 308 if (msp_dolby) {
7e8b09ea
HV
309 msp_write_dsp(client, 0x0008, 0x0520);
310 msp_write_dsp(client, 0x0009, 0x0620);
311 msp_write_dsp(client, 0x000a, src);
312 msp_write_dsp(client, 0x000b, src);
313 } else {
314 msp_write_dsp(client, 0x0008, src);
315 msp_write_dsp(client, 0x0009, src);
316 msp_write_dsp(client, 0x000a, src);
317 msp_write_dsp(client, 0x000b, src);
d312a46e
HV
318 msp_write_dsp(client, 0x000c, src);
319 if (state->has_scart23_in_scart2_out)
320 msp_write_dsp(client, 0x0041, src);
7e8b09ea
HV
321 }
322}
323
324static void msp3400c_print_mode(struct i2c_client *client)
325{
326 struct msp_state *state = i2c_get_clientdata(client);
327
328 if (state->main == state->second) {
f167cb4e 329 v4l_dbg(1, msp_debug, client, "mono sound carrier: %d.%03d MHz\n",
7e8b09ea
HV
330 state->main / 910000, (state->main / 910) % 1000);
331 } else {
f167cb4e 332 v4l_dbg(1, msp_debug, client, "main sound carrier: %d.%03d MHz\n",
7e8b09ea
HV
333 state->main / 910000, (state->main / 910) % 1000);
334 }
335 if (state->mode == MSP_MODE_FM_NICAM1 || state->mode == MSP_MODE_FM_NICAM2)
f167cb4e 336 v4l_dbg(1, msp_debug, client, "NICAM/FM carrier : %d.%03d MHz\n",
7e8b09ea
HV
337 state->second / 910000, (state->second/910) % 1000);
338 if (state->mode == MSP_MODE_AM_NICAM)
f167cb4e 339 v4l_dbg(1, msp_debug, client, "NICAM/AM carrier : %d.%03d MHz\n",
7e8b09ea
HV
340 state->second / 910000, (state->second / 910) % 1000);
341 if (state->mode == MSP_MODE_FM_TERRA && state->main != state->second) {
f167cb4e 342 v4l_dbg(1, msp_debug, client, "FM-stereo carrier : %d.%03d MHz\n",
7e8b09ea
HV
343 state->second / 910000, (state->second / 910) % 1000);
344 }
345}
346
347/* ----------------------------------------------------------------------- */
348
349int autodetect_stereo(struct i2c_client *client)
350{
351 struct msp_state *state = i2c_get_clientdata(client);
352 int val;
353 int rxsubchans = state->rxsubchans;
354 int newnicam = state->nicam_on;
355 int update = 0;
356
357 switch (state->mode) {
358 case MSP_MODE_FM_TERRA:
359 val = msp_read_dsp(client, 0x18);
360 if (val > 32767)
361 val -= 65536;
f167cb4e 362 v4l_dbg(2, msp_debug, client, "stereo detect register: %d\n", val);
7e8b09ea
HV
363 if (val > 4096) {
364 rxsubchans = V4L2_TUNER_SUB_STEREO | V4L2_TUNER_SUB_MONO;
365 } else if (val < -4096) {
366 rxsubchans = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
367 } else {
368 rxsubchans = V4L2_TUNER_SUB_MONO;
369 }
370 newnicam = 0;
371 break;
372 case MSP_MODE_FM_NICAM1:
373 case MSP_MODE_FM_NICAM2:
374 case MSP_MODE_AM_NICAM:
375 val = msp_read_dem(client, 0x23);
f167cb4e 376 v4l_dbg(2, msp_debug, client, "nicam sync=%d, mode=%d\n",
7e8b09ea
HV
377 val & 1, (val & 0x1e) >> 1);
378
379 if (val & 1) {
380 /* nicam synced */
381 switch ((val & 0x1e) >> 1) {
382 case 0:
383 case 8:
384 rxsubchans = V4L2_TUNER_SUB_STEREO;
385 break;
386 case 1:
387 case 9:
388 rxsubchans = V4L2_TUNER_SUB_MONO
389 | V4L2_TUNER_SUB_LANG1;
390 break;
391 case 2:
392 case 10:
393 rxsubchans = V4L2_TUNER_SUB_MONO
394 | V4L2_TUNER_SUB_LANG1
395 | V4L2_TUNER_SUB_LANG2;
396 break;
397 default:
398 rxsubchans = V4L2_TUNER_SUB_MONO;
399 break;
400 }
401 newnicam = 1;
402 } else {
403 newnicam = 0;
404 rxsubchans = V4L2_TUNER_SUB_MONO;
405 }
406 break;
407 case MSP_MODE_BTSC:
408 val = msp_read_dem(client, 0x200);
f167cb4e 409 v4l_dbg(2, msp_debug, client, "status=0x%x (pri=%s, sec=%s, %s%s%s)\n",
7e8b09ea
HV
410 val,
411 (val & 0x0002) ? "no" : "yes",
412 (val & 0x0004) ? "no" : "yes",
413 (val & 0x0040) ? "stereo" : "mono",
414 (val & 0x0080) ? ", nicam 2nd mono" : "",
415 (val & 0x0100) ? ", bilingual/SAP" : "");
416 rxsubchans = V4L2_TUNER_SUB_MONO;
417 if (val & 0x0040) rxsubchans |= V4L2_TUNER_SUB_STEREO;
418 if (val & 0x0100) rxsubchans |= V4L2_TUNER_SUB_LANG1;
419 break;
420 }
421 if (rxsubchans != state->rxsubchans) {
422 update = 1;
f167cb4e 423 v4l_dbg(1, msp_debug, client, "watch: rxsubchans %d => %d\n",
7e8b09ea
HV
424 state->rxsubchans,rxsubchans);
425 state->rxsubchans = rxsubchans;
426 }
427 if (newnicam != state->nicam_on) {
428 update = 1;
f167cb4e 429 v4l_dbg(1, msp_debug, client, "watch: nicam %d => %d\n",
7e8b09ea
HV
430 state->nicam_on,newnicam);
431 state->nicam_on = newnicam;
432 }
433 return update;
434}
435
436/*
437 * A kernel thread for msp3400 control -- we don't want to block the
438 * in the ioctl while doing the sound carrier & stereo detect
439 */
440/* stereo/multilang monitoring */
441static void watch_stereo(struct i2c_client *client)
442{
443 struct msp_state *state = i2c_get_clientdata(client);
444
445 if (autodetect_stereo(client)) {
d312a46e 446 if (state->rxsubchans & V4L2_TUNER_SUB_STEREO)
7e8b09ea 447 msp3400c_setstereo(client, V4L2_TUNER_MODE_STEREO);
d312a46e 448 else if (state->rxsubchans & V4L2_TUNER_SUB_LANG1)
7e8b09ea
HV
449 msp3400c_setstereo(client, V4L2_TUNER_MODE_LANG1);
450 else
451 msp3400c_setstereo(client, V4L2_TUNER_MODE_MONO);
452 }
453
f167cb4e 454 if (msp_once)
7e8b09ea
HV
455 state->watch_stereo = 0;
456}
457
458int msp3400c_thread(void *data)
459{
460 struct i2c_client *client = data;
461 struct msp_state *state = i2c_get_clientdata(client);
462 struct msp3400c_carrier_detect *cd;
463 int count, max1,max2,val1,val2, val,this;
464
465
f167cb4e 466 v4l_dbg(1, msp_debug, client, "msp3400 daemon started\n");
7e8b09ea 467 for (;;) {
f167cb4e 468 v4l_dbg(2, msp_debug, client, "msp3400 thread: sleep\n");
7e8b09ea 469 msp_sleep(state, -1);
f167cb4e 470 v4l_dbg(2, msp_debug, client, "msp3400 thread: wakeup\n");
7e8b09ea
HV
471
472 restart:
f167cb4e 473 v4l_dbg(1, msp_debug, client, "thread: restart scan\n");
7e8b09ea
HV
474 state->restart = 0;
475 if (kthread_should_stop())
476 break;
477
7560d7a4 478 if (state->radio || MSP_MODE_EXTERN == state->mode) {
7e8b09ea 479 /* no carrier scan, just unmute */
f167cb4e 480 v4l_dbg(1, msp_debug, client, "thread: no carrier scan\n");
7e8b09ea
HV
481 msp_set_audio(client);
482 continue;
483 }
484
485 /* mute */
486 msp_set_mute(client);
487 msp3400c_setmode(client, MSP_MODE_AM_DETECT /* +1 */ );
488 val1 = val2 = 0;
489 max1 = max2 = -1;
490 state->watch_stereo = 0;
491
492 /* some time for the tuner to sync */
493 if (msp_sleep(state,200))
494 goto restart;
495
496 /* carrier detect pass #1 -- main carrier */
497 cd = msp3400c_carrier_detect_main;
498 count = ARRAY_SIZE(msp3400c_carrier_detect_main);
499
f167cb4e 500 if (msp_amsound && (state->v4l2_std & V4L2_STD_SECAM)) {
7e8b09ea
HV
501 /* autodetect doesn't work well with AM ... */
502 max1 = 3;
503 count = 0;
f167cb4e 504 v4l_dbg(1, msp_debug, client, "AM sound override\n");
7e8b09ea
HV
505 }
506
507 for (this = 0; this < count; this++) {
508 msp3400c_setcarrier(client, cd[this].cdo,cd[this].cdo);
509 if (msp_sleep(state,100))
510 goto restart;
511 val = msp_read_dsp(client, 0x1b);
512 if (val > 32767)
513 val -= 65536;
514 if (val1 < val)
515 val1 = val, max1 = this;
f167cb4e 516 v4l_dbg(1, msp_debug, client, "carrier1 val: %5d / %s\n", val,cd[this].name);
7e8b09ea
HV
517 }
518
519 /* carrier detect pass #2 -- second (stereo) carrier */
520 switch (max1) {
521 case 1: /* 5.5 */
522 cd = msp3400c_carrier_detect_55;
523 count = ARRAY_SIZE(msp3400c_carrier_detect_55);
524 break;
525 case 3: /* 6.5 */
526 cd = msp3400c_carrier_detect_65;
527 count = ARRAY_SIZE(msp3400c_carrier_detect_65);
528 break;
529 case 0: /* 4.5 */
530 case 2: /* 6.0 */
531 default:
532 cd = NULL;
533 count = 0;
534 break;
535 }
536
f167cb4e 537 if (msp_amsound && (state->v4l2_std & V4L2_STD_SECAM)) {
7e8b09ea
HV
538 /* autodetect doesn't work well with AM ... */
539 cd = NULL;
540 count = 0;
541 max2 = 0;
542 }
543 for (this = 0; this < count; this++) {
544 msp3400c_setcarrier(client, cd[this].cdo,cd[this].cdo);
545 if (msp_sleep(state,100))
546 goto restart;
547 val = msp_read_dsp(client, 0x1b);
548 if (val > 32767)
549 val -= 65536;
550 if (val2 < val)
551 val2 = val, max2 = this;
f167cb4e 552 v4l_dbg(1, msp_debug, client, "carrier2 val: %5d / %s\n", val,cd[this].name);
7e8b09ea
HV
553 }
554
555 /* program the msp3400 according to the results */
556 state->main = msp3400c_carrier_detect_main[max1].cdo;
557 switch (max1) {
558 case 1: /* 5.5 */
559 if (max2 == 0) {
560 /* B/G FM-stereo */
561 state->second = msp3400c_carrier_detect_55[max2].cdo;
562 msp3400c_setmode(client, MSP_MODE_FM_TERRA);
563 state->nicam_on = 0;
564 msp3400c_setstereo(client, V4L2_TUNER_MODE_MONO);
565 state->watch_stereo = 1;
7560d7a4 566 } else if (max2 == 1 && state->has_nicam) {
7e8b09ea
HV
567 /* B/G NICAM */
568 state->second = msp3400c_carrier_detect_55[max2].cdo;
569 msp3400c_setmode(client, MSP_MODE_FM_NICAM1);
570 state->nicam_on = 1;
571 msp3400c_setcarrier(client, state->second, state->main);
572 state->watch_stereo = 1;
573 } else {
574 goto no_second;
575 }
576 break;
577 case 2: /* 6.0 */
578 /* PAL I NICAM */
579 state->second = MSP_CARRIER(6.552);
580 msp3400c_setmode(client, MSP_MODE_FM_NICAM2);
581 state->nicam_on = 1;
582 msp3400c_setcarrier(client, state->second, state->main);
583 state->watch_stereo = 1;
584 break;
585 case 3: /* 6.5 */
586 if (max2 == 1 || max2 == 2) {
587 /* D/K FM-stereo */
588 state->second = msp3400c_carrier_detect_65[max2].cdo;
589 msp3400c_setmode(client, MSP_MODE_FM_TERRA);
590 state->nicam_on = 0;
591 msp3400c_setstereo(client, V4L2_TUNER_MODE_MONO);
592 state->watch_stereo = 1;
5af0c8f6 593 } else if (max2 == 0 && (state->v4l2_std & V4L2_STD_SECAM)) {
7e8b09ea
HV
594 /* L NICAM or AM-mono */
595 state->second = msp3400c_carrier_detect_65[max2].cdo;
596 msp3400c_setmode(client, MSP_MODE_AM_NICAM);
597 state->nicam_on = 0;
598 msp3400c_setstereo(client, V4L2_TUNER_MODE_MONO);
599 msp3400c_setcarrier(client, state->second, state->main);
600 /* volume prescale for SCART (AM mono input) */
601 msp_write_dsp(client, 0x000d, 0x1900);
602 state->watch_stereo = 1;
7560d7a4 603 } else if (max2 == 0 && state->has_nicam) {
7e8b09ea
HV
604 /* D/K NICAM */
605 state->second = msp3400c_carrier_detect_65[max2].cdo;
606 msp3400c_setmode(client, MSP_MODE_FM_NICAM1);
607 state->nicam_on = 1;
608 msp3400c_setcarrier(client, state->second, state->main);
609 state->watch_stereo = 1;
610 } else {
611 goto no_second;
612 }
613 break;
614 case 0: /* 4.5 */
615 default:
616 no_second:
617 state->second = msp3400c_carrier_detect_main[max1].cdo;
618 msp3400c_setmode(client, MSP_MODE_FM_TERRA);
619 state->nicam_on = 0;
620 msp3400c_setcarrier(client, state->second, state->main);
621 state->rxsubchans = V4L2_TUNER_SUB_MONO;
622 msp3400c_setstereo(client, V4L2_TUNER_MODE_MONO);
623 break;
624 }
625
626 /* unmute */
627 msp_set_audio(client);
628
f167cb4e 629 if (msp_debug)
7e8b09ea
HV
630 msp3400c_print_mode(client);
631
632 /* monitor tv audio mode */
633 while (state->watch_stereo) {
634 if (msp_sleep(state,5000))
635 goto restart;
636 watch_stereo(client);
637 }
638 }
f167cb4e 639 v4l_dbg(1, msp_debug, client, "thread: exit\n");
7e8b09ea
HV
640 return 0;
641}
642
643
644int msp3410d_thread(void *data)
645{
646 struct i2c_client *client = data;
647 struct msp_state *state = i2c_get_clientdata(client);
d312a46e 648 int val, i, std;
7e8b09ea 649
f167cb4e 650 v4l_dbg(1, msp_debug, client, "msp3410 daemon started\n");
7e8b09ea
HV
651
652 for (;;) {
f167cb4e 653 v4l_dbg(2, msp_debug, client, "msp3410 thread: sleep\n");
7e8b09ea 654 msp_sleep(state,-1);
f167cb4e 655 v4l_dbg(2, msp_debug, client, "msp3410 thread: wakeup\n");
7e8b09ea
HV
656
657 restart:
f167cb4e 658 v4l_dbg(1, msp_debug, client, "thread: restart scan\n");
7e8b09ea
HV
659 state->restart = 0;
660 if (kthread_should_stop())
661 break;
662
663 if (state->mode == MSP_MODE_EXTERN) {
664 /* no carrier scan needed, just unmute */
f167cb4e 665 v4l_dbg(1, msp_debug, client, "thread: no carrier scan\n");
7e8b09ea
HV
666 msp_set_audio(client);
667 continue;
668 }
669
670 /* put into sane state (and mute) */
671 msp_reset(client);
672
673 /* some time for the tuner to sync */
674 if (msp_sleep(state,200))
675 goto restart;
676
677 /* start autodetect */
5af0c8f6
HV
678 if (state->radio)
679 std = 0x40;
d312a46e 680 else
5af0c8f6 681 std = (state->v4l2_std & V4L2_STD_NTSC) ? 0x20 : 1;
7e8b09ea
HV
682 state->watch_stereo = 0;
683
f167cb4e
MCC
684 if (msp_debug)
685 v4l_dbg(1, msp_debug, client, "setting standard: %s (0x%04x)\n",
5af0c8f6 686 msp_standard_std_name(std), std);
7e8b09ea
HV
687
688 if (std != 1) {
689 /* programmed some specific mode */
690 val = std;
691 } else {
692 /* triggered autodetect */
5af0c8f6 693 msp_write_dem(client, 0x20, std);
7e8b09ea 694 for (;;) {
d312a46e 695 if (msp_sleep(state, 100))
7e8b09ea
HV
696 goto restart;
697
698 /* check results */
699 val = msp_read_dem(client, 0x7e);
700 if (val < 0x07ff)
701 break;
f167cb4e 702 v4l_dbg(1, msp_debug, client, "detection still in progress\n");
7e8b09ea
HV
703 }
704 }
5af0c8f6
HV
705 for (i = 0; msp_stdlist[i].name != NULL; i++)
706 if (msp_stdlist[i].retval == val)
7e8b09ea 707 break;
f167cb4e 708 v4l_dbg(1, msp_debug, client, "current standard: %s (0x%04x)\n",
5af0c8f6
HV
709 msp_standard_std_name(val), val);
710 state->main = msp_stdlist[i].main;
711 state->second = msp_stdlist[i].second;
712 state->std = val;
713
f167cb4e 714 if (msp_amsound && !state->radio && (state->v4l2_std & V4L2_STD_SECAM) &&
5af0c8f6 715 (val != 0x0009)) {
7e8b09ea 716 /* autodetection has failed, let backup */
f167cb4e 717 v4l_dbg(1, msp_debug, client, "autodetection failed,"
5af0c8f6
HV
718 " switching to backup standard: %s (0x%04x)\n",
719 msp_stdlist[8].name ? msp_stdlist[8].name : "unknown",val);
7e8b09ea
HV
720 val = 0x0009;
721 msp_write_dem(client, 0x20, val);
722 }
723
724 /* set various prescales */
725 msp_write_dsp(client, 0x0d, 0x1900); /* scart */
726 msp_write_dsp(client, 0x0e, 0x2403); /* FM */
727 msp_write_dsp(client, 0x10, 0x5a00); /* nicam */
728
729 /* set stereo */
730 switch (val) {
731 case 0x0008: /* B/G NICAM */
732 case 0x000a: /* I NICAM */
733 if (val == 0x0008)
734 state->mode = MSP_MODE_FM_NICAM1;
735 else
736 state->mode = MSP_MODE_FM_NICAM2;
737 /* just turn on stereo */
738 state->rxsubchans = V4L2_TUNER_SUB_STEREO;
739 state->nicam_on = 1;
740 state->watch_stereo = 1;
741 msp3400c_setstereo(client,V4L2_TUNER_MODE_STEREO);
742 break;
743 case 0x0009:
744 state->mode = MSP_MODE_AM_NICAM;
745 state->rxsubchans = V4L2_TUNER_SUB_MONO;
746 state->nicam_on = 1;
747 msp3400c_setstereo(client,V4L2_TUNER_MODE_MONO);
748 state->watch_stereo = 1;
749 break;
750 case 0x0020: /* BTSC */
751 /* just turn on stereo */
752 state->mode = MSP_MODE_BTSC;
753 state->rxsubchans = V4L2_TUNER_SUB_STEREO;
754 state->nicam_on = 0;
755 state->watch_stereo = 1;
756 msp3400c_setstereo(client,V4L2_TUNER_MODE_STEREO);
757 break;
758 case 0x0040: /* FM radio */
7560d7a4 759 state->mode = MSP_MODE_FM_RADIO;
7e8b09ea 760 state->rxsubchans = V4L2_TUNER_SUB_STEREO;
7e8b09ea
HV
761 state->nicam_on = 0;
762 state->watch_stereo = 0;
7560d7a4 763 /* not needed in theory if we have radio, but
7e8b09ea 764 short programming enables carrier mute */
5af0c8f6 765 msp3400c_setmode(client, MSP_MODE_FM_RADIO);
7e8b09ea
HV
766 msp3400c_setcarrier(client, MSP_CARRIER(10.7),
767 MSP_CARRIER(10.7));
768 /* scart routing */
769 msp_set_scart(client,SCART_IN2,0);
770 /* msp34xx does radio decoding */
771 msp_write_dsp(client, 0x08, 0x0020);
772 msp_write_dsp(client, 0x09, 0x0020);
773 msp_write_dsp(client, 0x0b, 0x0020);
774 break;
775 case 0x0003:
776 case 0x0004:
777 case 0x0005:
5af0c8f6 778 state->mode = MSP_MODE_FM_TERRA;
7e8b09ea 779 state->rxsubchans = V4L2_TUNER_SUB_MONO;
7e8b09ea
HV
780 state->nicam_on = 0;
781 state->watch_stereo = 1;
782 break;
783 }
784
785 /* unmute, restore misc registers */
786 msp_set_audio(client);
787 msp_write_dsp(client, 0x13, state->acb);
5af0c8f6
HV
788 if (state->has_i2s_conf)
789 msp_write_dem(client, 0x40, state->i2s_mode);
7e8b09ea
HV
790
791 /* monitor tv audio mode */
792 while (state->watch_stereo) {
793 if (msp_sleep(state,5000))
794 goto restart;
795 watch_stereo(client);
796 }
797 }
f167cb4e 798 v4l_dbg(1, msp_debug, client, "thread: exit\n");
7e8b09ea
HV
799 return 0;
800}
801
802/* ----------------------------------------------------------------------- */
803
f167cb4e 804/* msp34xxG + (autoselect no-thread) */
7e8b09ea
HV
805/* this one uses both automatic standard detection and automatic sound */
806/* select which are available in the newer G versions */
807/* struct msp: only norm, acb and source are really used in this mode */
808
809/* set the same 'source' for the loudspeaker, scart and quasi-peak detector
810 * the value for source is the same as bit 15:8 of DSP registers 0x08,
811 * 0x0a and 0x0c: 0=mono, 1=stereo or A|B, 2=SCART, 3=stereo or A, 4=stereo or B
812 *
813 * this function replaces msp3400c_setstereo
814 */
815static void msp34xxg_set_source(struct i2c_client *client, int source)
816{
817 struct msp_state *state = i2c_get_clientdata(client);
818
819 /* fix matrix mode to stereo and let the msp choose what
820 * to output according to 'source', as recommended
821 * for MONO (source==0) downmixing set bit[7:0] to 0x30
822 */
823 int value = (source & 0x07) << 8 | (source == 0 ? 0x30 : 0x20);
824
f167cb4e 825 v4l_dbg(1, msp_debug, client, "set source to %d (0x%x)\n", source, value);
7e8b09ea
HV
826 /* Loudspeaker Output */
827 msp_write_dsp(client, 0x08, value);
828 /* SCART1 DA Output */
829 msp_write_dsp(client, 0x0a, value);
830 /* Quasi-peak detector */
831 msp_write_dsp(client, 0x0c, value);
832 /*
833 * set identification threshold. Personally, I
834 * I set it to a higher value that the default
835 * of 0x190 to ignore noisy stereo signals.
836 * this needs tuning. (recommended range 0x00a0-0x03c0)
837 * 0x7f0 = forced mono mode
838 */
839 /* a2 threshold for stereo/bilingual */
f167cb4e 840 msp_write_dem(client, 0x22, msp_stereo_thresh);
7e8b09ea
HV
841 state->source = source;
842}
843
844/* (re-)initialize the msp34xxg, according to the current norm in state->norm
845 * return 0 if it worked, -1 if it failed
846 */
847static int msp34xxg_reset(struct i2c_client *client)
848{
849 struct msp_state *state = i2c_get_clientdata(client);
850 int modus, std;
851
852 if (msp_reset(client))
853 return -1;
854
855 /* make sure that input/output is muted (paranoid mode) */
856 /* ACB, mute DSP input, mute SCART 1 */
857 if (msp_write_dsp(client, 0x13, 0x0f20))
858 return -1;
859
5af0c8f6
HV
860 if (state->has_i2s_conf)
861 msp_write_dem(client, 0x40, state->i2s_mode);
7e8b09ea
HV
862
863 /* step-by-step initialisation, as described in the manual */
7560d7a4
HV
864 modus = msp_modus(client);
865 if (state->radio)
866 std = 0x40;
867 else
5af0c8f6 868 std = (state->v4l2_std & V4L2_STD_NTSC) ? 0x20 : 1;
7e8b09ea
HV
869 modus &= ~0x03; /* STATUS_CHANGE = 0 */
870 modus |= 0x01; /* AUTOMATIC_SOUND_DETECTION = 1 */
871 if (msp_write_dem(client, 0x30, modus))
872 return -1;
873 if (msp_write_dem(client, 0x20, std))
874 return -1;
875
876 /* write the dsps that may have an influence on
877 standard/audio autodetection right now */
878 msp34xxg_set_source(client, state->source);
879
880 /* AM/FM Prescale [15:8] 75khz deviation */
881 if (msp_write_dsp(client, 0x0e, 0x3000))
882 return -1;
883
884 /* NICAM Prescale 9db gain (as recommended) */
885 if (msp_write_dsp(client, 0x10, 0x5a00))
886 return -1;
887
888 return 0;
889}
890
891int msp34xxg_thread(void *data)
892{
893 struct i2c_client *client = data;
894 struct msp_state *state = i2c_get_clientdata(client);
895 int val, std, i;
896
f167cb4e 897 v4l_dbg(1, msp_debug, client, "msp34xxg daemon started\n");
7e8b09ea
HV
898
899 state->source = 1; /* default */
900 for (;;) {
f167cb4e 901 v4l_dbg(2, msp_debug, client, "msp34xxg thread: sleep\n");
7e8b09ea 902 msp_sleep(state, -1);
f167cb4e 903 v4l_dbg(2, msp_debug, client, "msp34xxg thread: wakeup\n");
7e8b09ea
HV
904
905 restart:
f167cb4e 906 v4l_dbg(1, msp_debug, client, "thread: restart scan\n");
7e8b09ea
HV
907 state->restart = 0;
908 if (kthread_should_stop())
909 break;
910
911 /* setup the chip*/
912 msp34xxg_reset(client);
f167cb4e 913 std = msp_standard;
7e8b09ea
HV
914 if (std != 0x01)
915 goto unmute;
916
917 /* watch autodetect */
f167cb4e 918 v4l_dbg(1, msp_debug, client, "triggered autodetect, waiting for result\n");
7e8b09ea
HV
919 for (i = 0; i < 10; i++) {
920 if (msp_sleep(state, 100))
921 goto restart;
922
923 /* check results */
924 val = msp_read_dem(client, 0x7e);
925 if (val < 0x07ff) {
926 std = val;
927 break;
928 }
f167cb4e 929 v4l_dbg(2, msp_debug, client, "detection still in progress\n");
7e8b09ea
HV
930 }
931 if (std == 1) {
f167cb4e 932 v4l_dbg(1, msp_debug, client, "detection still in progress after 10 tries. giving up.\n");
7e8b09ea
HV
933 continue;
934 }
935
936 unmute:
5af0c8f6 937 state->std = std;
f167cb4e 938 v4l_dbg(1, msp_debug, client, "current standard: %s (0x%04x)\n",
5af0c8f6 939 msp_standard_std_name(std), std);
7e8b09ea
HV
940
941 /* unmute: dispatch sound to scart output, set scart volume */
942 msp_set_audio(client);
943
944 /* restore ACB */
945 if (msp_write_dsp(client, 0x13, state->acb))
946 return -1;
947
948 msp_write_dem(client, 0x40, state->i2s_mode);
949 }
f167cb4e 950 v4l_dbg(1, msp_debug, client, "thread: exit\n");
7e8b09ea
HV
951 return 0;
952}
953
954void msp34xxg_detect_stereo(struct i2c_client *client)
955{
956 struct msp_state *state = i2c_get_clientdata(client);
957
958 int status = msp_read_dem(client, 0x0200);
959 int is_bilingual = status & 0x100;
960 int is_stereo = status & 0x40;
961
962 state->rxsubchans = 0;
963 if (is_stereo)
964 state->rxsubchans |= V4L2_TUNER_SUB_STEREO;
965 else
966 state->rxsubchans |= V4L2_TUNER_SUB_MONO;
967 if (is_bilingual) {
968 state->rxsubchans |= V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
969 /* I'm supposed to check whether it's SAP or not
970 * and set only LANG2/SAP in this case. Yet, the MSP
971 * does a lot of work to hide this and handle everything
972 * the same way. I don't want to work around it so unless
973 * this is a problem, I'll handle SAP just like lang1/lang2.
974 */
975 }
f167cb4e 976 v4l_dbg(1, msp_debug, client, "status=0x%x, stereo=%d, bilingual=%d -> rxsubchans=%d\n",
7e8b09ea
HV
977 status, is_stereo, is_bilingual, state->rxsubchans);
978}
979
980void msp34xxg_set_audmode(struct i2c_client *client, int audmode)
981{
982 struct msp_state *state = i2c_get_clientdata(client);
983 int source;
984
985 switch (audmode) {
986 case V4L2_TUNER_MODE_MONO:
987 source = 0; /* mono only */
988 break;
989 case V4L2_TUNER_MODE_STEREO:
990 source = 1; /* stereo or A|B, see comment in msp34xxg_get_v4l2_stereo() */
991 /* problem: that could also mean 2 (scart input) */
992 break;
993 case V4L2_TUNER_MODE_LANG1:
994 source = 3; /* stereo or A */
995 break;
996 case V4L2_TUNER_MODE_LANG2:
997 source = 4; /* stereo or B */
998 break;
999 default:
1000 audmode = 0;
1001 source = 1;
1002 break;
1003 }
1004 state->audmode = audmode;
1005 msp34xxg_set_source(client, source);
1006}
1007
This page took 0.079164 seconds and 5 git commands to generate.