V4L/DVB (3317): msp3400: use v4l2_std_id and determine chip capabilities.
[safe/jmp/linux-2.6] / drivers / media / video / msp3400-kthreads.c
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 */
36 static struct {
37         int retval;
38         int main, second;
39         char *name;
40 } msp_modelist[] = {
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
63 static 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
116 struct msp3400c_carrier_detect {
117         int   cdo;
118         char *name;
119 };
120
121 static 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
129 static 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
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"   },
143 };
144
145 /* ------------------------------------------------------------------------ */
146
147 const char *msp_standard_mode_name(int mode)
148 {
149         int i;
150
151         for (i = 0; msp_modelist[i].name != NULL; i++)
152                 if (msp_modelist[i].retval == mode)
153                         return msp_modelist[i].name;
154         return "unknown";
155 }
156
157 void 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
166 void msp3400c_setmode(struct i2c_client *client, int type)
167 {
168         struct msp_state *state = i2c_get_clientdata(client);
169         int i;
170
171         v4l_dbg(1, client, "setmode: %d\n", type);
172         state->mode       = type;
173         state->audmode    = V4L2_TUNER_MODE_MONO;
174         state->rxsubchans = V4L2_TUNER_SUB_MONO;
175
176         msp_write_dem(client, 0x00bb, msp3400c_init_data[type].ad_cv);
177
178         for (i = 5; i >= 0; i--)               /* fir 1 */
179                 msp_write_dem(client, 0x0001, msp3400c_init_data[type].fir1[i]);
180
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]);
186
187         msp_write_dem(client, 0x0083, msp3400c_init_data[type].mode_reg);
188
189         msp3400c_setcarrier(client, msp3400c_init_data[type].cdo1,
190                             msp3400c_init_data[type].cdo2);
191
192         msp_write_dem(client, 0x0056, 0); /*LOAD_REG_1/2*/
193
194         if (dolby) {
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);
198         } else {
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);
202         }
203         msp_write_dsp(client, 0x000a, msp3400c_init_data[type].dsp_src);
204         msp_write_dsp(client, 0x000e, msp3400c_init_data[type].dsp_matrix);
205
206         if (state->has_nicam) {
207                 /* nicam prescale */
208                 msp_write_dsp(client, 0x0010, 0x5a00); /* was: 0x3000 */
209         }
210 }
211
212 /* given a bitmask of VIDEO_SOUND_XXX returns the "best" in the bitmask */
213 static int msp3400c_best_video_sound(int rxsubchans)
214 {
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;
222 }
223
224 /* turn on/off nicam + stereo */
225 void msp3400c_setstereo(struct i2c_client *client, int mode)
226 {
227         static char *strmode[] = { "0", "mono", "stereo", "3",
228                 "lang1", "5", "6", "7", "lang2"
229         };
230         struct msp_state *state = i2c_get_clientdata(client);
231         int nicam = 0;          /* channel source: FM/AM or nicam */
232         int src = 0;
233
234         if (state->opmode == OPMODE_AUTOSELECT) {
235                 /* this method would break everything, let's make sure
236                  * it's never called
237                  */
238                 v4l_dbg(1, client, "setstereo called with mode=%d instead of set_source (ignored)\n",
239                      mode);
240                 return;
241         }
242
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);
248                 switch (mode) {
249                 case V4L2_TUNER_MODE_STEREO:
250                         msp_write_dsp(client, 0x000e, 0x3001);
251                         break;
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);
256                         break;
257                 }
258                 break;
259         case MSP_MODE_FM_SAT:
260                 v4l_dbg(1, client, "SAT setstereo: %s\n", strmode[mode]);
261                 switch (mode) {
262                 case V4L2_TUNER_MODE_MONO:
263                         msp3400c_setcarrier(client, MSP_CARRIER(6.5), MSP_CARRIER(6.5));
264                         break;
265                 case V4L2_TUNER_MODE_STEREO:
266                         msp3400c_setcarrier(client, MSP_CARRIER(7.2), MSP_CARRIER(7.02));
267                         break;
268                 case V4L2_TUNER_MODE_LANG1:
269                         msp3400c_setcarrier(client, MSP_CARRIER(7.38), MSP_CARRIER(7.02));
270                         break;
271                 case V4L2_TUNER_MODE_LANG2:
272                         msp3400c_setcarrier(client, MSP_CARRIER(7.38), MSP_CARRIER(7.02));
273                         break;
274                 }
275                 break;
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);
281                 if (state->nicam_on)
282                         nicam=0x0100;
283                 break;
284         case MSP_MODE_BTSC:
285                 v4l_dbg(1, client, "BTSC setstereo: %s\n",strmode[mode]);
286                 nicam=0x0300;
287                 break;
288         case MSP_MODE_EXTERN:
289                 v4l_dbg(1, client, "extern setstereo: %s\n",strmode[mode]);
290                 nicam = 0x0200;
291                 break;
292         case MSP_MODE_FM_RADIO:
293                 v4l_dbg(1, client, "FM-Radio setstereo: %s\n",strmode[mode]);
294                 break;
295         default:
296                 v4l_dbg(1, client, "mono setstereo\n");
297                 return;
298         }
299
300         /* switch audio */
301         switch (msp3400c_best_video_sound(mode)) {
302         case V4L2_TUNER_MODE_STEREO:
303                 src = 0x0020 | nicam;
304                 break;
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);
311                         src = 0x0200;
312                         break;
313                 }
314         case V4L2_TUNER_MODE_LANG1:
315                 src = 0x0000 | nicam;
316                 break;
317         case V4L2_TUNER_MODE_LANG2:
318                 src = 0x0010 | nicam;
319                 break;
320         }
321         v4l_dbg(1, client, "setstereo final source/matrix = 0x%x\n", src);
322
323         if (dolby) {
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);
328         } else {
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);
333         }
334 }
335
336 static void msp3400c_print_mode(struct i2c_client *client)
337 {
338         struct msp_state *state = i2c_get_clientdata(client);
339
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);
343         } else {
344                 v4l_dbg(1, client, "main sound carrier: %d.%03d MHz\n",
345                        state->main / 910000, (state->main / 910) % 1000);
346         }
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);
356         }
357 }
358
359 /* ----------------------------------------------------------------------- */
360
361 int autodetect_stereo(struct i2c_client *client)
362 {
363         struct msp_state *state = i2c_get_clientdata(client);
364         int val;
365         int rxsubchans = state->rxsubchans;
366         int newnicam   = state->nicam_on;
367         int update = 0;
368
369         switch (state->mode) {
370         case MSP_MODE_FM_TERRA:
371                 val = msp_read_dsp(client, 0x18);
372                 if (val > 32767)
373                         val -= 65536;
374                 v4l_dbg(2, client, "stereo detect register: %d\n", val);
375                 if (val > 4096) {
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;
379                 } else {
380                         rxsubchans = V4L2_TUNER_SUB_MONO;
381                 }
382                 newnicam = 0;
383                 break;
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);
390
391                 if (val & 1) {
392                         /* nicam synced */
393                         switch ((val & 0x1e) >> 1)  {
394                         case 0:
395                         case 8:
396                                 rxsubchans = V4L2_TUNER_SUB_STEREO;
397                                 break;
398                         case 1:
399                         case 9:
400                                 rxsubchans = V4L2_TUNER_SUB_MONO
401                                         | V4L2_TUNER_SUB_LANG1;
402                                 break;
403                         case 2:
404                         case 10:
405                                 rxsubchans = V4L2_TUNER_SUB_MONO
406                                         | V4L2_TUNER_SUB_LANG1
407                                         | V4L2_TUNER_SUB_LANG2;
408                                 break;
409                         default:
410                                 rxsubchans = V4L2_TUNER_SUB_MONO;
411                                 break;
412                         }
413                         newnicam = 1;
414                 } else {
415                         newnicam = 0;
416                         rxsubchans = V4L2_TUNER_SUB_MONO;
417                 }
418                 break;
419         case MSP_MODE_BTSC:
420                 val = msp_read_dem(client, 0x200);
421                 v4l_dbg(2, client, "status=0x%x (pri=%s, sec=%s, %s%s%s)\n",
422                         val,
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;
431                 break;
432         }
433         if (rxsubchans != state->rxsubchans) {
434                 update = 1;
435                 v4l_dbg(1, client, "watch: rxsubchans %d => %d\n",
436                         state->rxsubchans,rxsubchans);
437                 state->rxsubchans = rxsubchans;
438         }
439         if (newnicam != state->nicam_on) {
440                 update = 1;
441                 v4l_dbg(1, client, "watch: nicam %d => %d\n",
442                         state->nicam_on,newnicam);
443                 state->nicam_on = newnicam;
444         }
445         return update;
446 }
447
448 /*
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
451  */
452 /* stereo/multilang monitoring */
453 static void watch_stereo(struct i2c_client *client)
454 {
455         struct msp_state *state = i2c_get_clientdata(client);
456
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);
462                 else
463                         msp3400c_setstereo(client, V4L2_TUNER_MODE_MONO);
464         }
465
466         if (once)
467                 state->watch_stereo = 0;
468 }
469
470 int msp3400c_thread(void *data)
471 {
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;
476
477
478         v4l_dbg(1, client, "msp3400 daemon started\n");
479         for (;;) {
480                 v4l_dbg(2, client, "msp3400 thread: sleep\n");
481                 msp_sleep(state, -1);
482                 v4l_dbg(2, client, "msp3400 thread: wakeup\n");
483
484         restart:
485                 v4l_dbg(1, client, "thread: restart scan\n");
486                 state->restart = 0;
487                 if (kthread_should_stop())
488                         break;
489
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);
494                         continue;
495                 }
496
497                 /* mute */
498                 msp_set_mute(client);
499                 msp3400c_setmode(client, MSP_MODE_AM_DETECT /* +1 */ );
500                 val1 = val2 = 0;
501                 max1 = max2 = -1;
502                 state->watch_stereo = 0;
503
504                 /* some time for the tuner to sync */
505                 if (msp_sleep(state,200))
506                         goto restart;
507
508                 /* carrier detect pass #1 -- main carrier */
509                 cd = msp3400c_carrier_detect_main;
510                 count = ARRAY_SIZE(msp3400c_carrier_detect_main);
511
512                 if (amsound && (state->std & V4L2_STD_SECAM)) {
513                         /* autodetect doesn't work well with AM ... */
514                         max1 = 3;
515                         count = 0;
516                         v4l_dbg(1, client, "AM sound override\n");
517                 }
518
519                 for (this = 0; this < count; this++) {
520                         msp3400c_setcarrier(client, cd[this].cdo,cd[this].cdo);
521                         if (msp_sleep(state,100))
522                                 goto restart;
523                         val = msp_read_dsp(client, 0x1b);
524                         if (val > 32767)
525                                 val -= 65536;
526                         if (val1 < val)
527                                 val1 = val, max1 = this;
528                         v4l_dbg(1, client, "carrier1 val: %5d / %s\n", val,cd[this].name);
529                 }
530
531                 /* carrier detect pass #2 -- second (stereo) carrier */
532                 switch (max1) {
533                 case 1: /* 5.5 */
534                         cd = msp3400c_carrier_detect_55;
535                         count = ARRAY_SIZE(msp3400c_carrier_detect_55);
536                         break;
537                 case 3: /* 6.5 */
538                         cd = msp3400c_carrier_detect_65;
539                         count = ARRAY_SIZE(msp3400c_carrier_detect_65);
540                         break;
541                 case 0: /* 4.5 */
542                 case 2: /* 6.0 */
543                 default:
544                         cd = NULL;
545                         count = 0;
546                         break;
547                 }
548
549                 if (amsound && (state->std & V4L2_STD_SECAM)) {
550                         /* autodetect doesn't work well with AM ... */
551                         cd = NULL;
552                         count = 0;
553                         max2 = 0;
554                 }
555                 for (this = 0; this < count; this++) {
556                         msp3400c_setcarrier(client, cd[this].cdo,cd[this].cdo);
557                         if (msp_sleep(state,100))
558                                 goto restart;
559                         val = msp_read_dsp(client, 0x1b);
560                         if (val > 32767)
561                                 val -= 65536;
562                         if (val2 < val)
563                                 val2 = val, max2 = this;
564                         v4l_dbg(1, client, "carrier2 val: %5d / %s\n", val,cd[this].name);
565                 }
566
567                 /* program the msp3400 according to the results */
568                 state->main   = msp3400c_carrier_detect_main[max1].cdo;
569                 switch (max1) {
570                 case 1: /* 5.5 */
571                         if (max2 == 0) {
572                                 /* B/G FM-stereo */
573                                 state->second = msp3400c_carrier_detect_55[max2].cdo;
574                                 msp3400c_setmode(client, MSP_MODE_FM_TERRA);
575                                 state->nicam_on = 0;
576                                 msp3400c_setstereo(client, V4L2_TUNER_MODE_MONO);
577                                 state->watch_stereo = 1;
578                         } else if (max2 == 1 && state->has_nicam) {
579                                 /* B/G NICAM */
580                                 state->second = msp3400c_carrier_detect_55[max2].cdo;
581                                 msp3400c_setmode(client, MSP_MODE_FM_NICAM1);
582                                 state->nicam_on = 1;
583                                 msp3400c_setcarrier(client, state->second, state->main);
584                                 state->watch_stereo = 1;
585                         } else {
586                                 goto no_second;
587                         }
588                         break;
589                 case 2: /* 6.0 */
590                         /* PAL I NICAM */
591                         state->second = MSP_CARRIER(6.552);
592                         msp3400c_setmode(client, MSP_MODE_FM_NICAM2);
593                         state->nicam_on = 1;
594                         msp3400c_setcarrier(client, state->second, state->main);
595                         state->watch_stereo = 1;
596                         break;
597                 case 3: /* 6.5 */
598                         if (max2 == 1 || max2 == 2) {
599                                 /* D/K FM-stereo */
600                                 state->second = msp3400c_carrier_detect_65[max2].cdo;
601                                 msp3400c_setmode(client, MSP_MODE_FM_TERRA);
602                                 state->nicam_on = 0;
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);
609                                 state->nicam_on = 0;
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) {
616                                 /* D/K NICAM */
617                                 state->second = msp3400c_carrier_detect_65[max2].cdo;
618                                 msp3400c_setmode(client, MSP_MODE_FM_NICAM1);
619                                 state->nicam_on = 1;
620                                 msp3400c_setcarrier(client, state->second, state->main);
621                                 state->watch_stereo = 1;
622                         } else {
623                                 goto no_second;
624                         }
625                         break;
626                 case 0: /* 4.5 */
627                 default:
628                 no_second:
629                         state->second = msp3400c_carrier_detect_main[max1].cdo;
630                         msp3400c_setmode(client, MSP_MODE_FM_TERRA);
631                         state->nicam_on = 0;
632                         msp3400c_setcarrier(client, state->second, state->main);
633                         state->rxsubchans = V4L2_TUNER_SUB_MONO;
634                         msp3400c_setstereo(client, V4L2_TUNER_MODE_MONO);
635                         break;
636                 }
637
638                 /* unmute */
639                 msp_set_audio(client);
640
641                 if (debug)
642                         msp3400c_print_mode(client);
643
644                 /* monitor tv audio mode */
645                 while (state->watch_stereo) {
646                         if (msp_sleep(state,5000))
647                                 goto restart;
648                         watch_stereo(client);
649                 }
650         }
651         v4l_dbg(1, client, "thread: exit\n");
652         return 0;
653 }
654
655
656 int msp3410d_thread(void *data)
657 {
658         struct i2c_client *client = data;
659         struct msp_state *state = i2c_get_clientdata(client);
660         int mode,val,i,std;
661
662         v4l_dbg(1, client, "msp3410 daemon started\n");
663
664         for (;;) {
665                 v4l_dbg(2, client, "msp3410 thread: sleep\n");
666                 msp_sleep(state,-1);
667                 v4l_dbg(2, client, "msp3410 thread: wakeup\n");
668
669         restart:
670                 v4l_dbg(1, client, "thread: restart scan\n");
671                 state->restart = 0;
672                 if (kthread_should_stop())
673                         break;
674
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);
679                         continue;
680                 }
681
682                 /* put into sane state (and mute) */
683                 msp_reset(client);
684
685                 /* some time for the tuner to sync */
686                 if (msp_sleep(state,200))
687                         goto restart;
688
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;
695
696                 if (debug)
697                         v4l_dbg(1, client, "setting mode: %s (0x%04x)\n",
698                                msp_standard_mode_name(std), std);
699
700                 if (std != 1) {
701                         /* programmed some specific mode */
702                         val = std;
703                 } else {
704                         /* triggered autodetect */
705                         for (;;) {
706                                 if (msp_sleep(state,100))
707                                         goto restart;
708
709                                 /* check results */
710                                 val = msp_read_dem(client, 0x7e);
711                                 if (val < 0x07ff)
712                                         break;
713                                 v4l_dbg(1, client, "detection still in progress\n");
714                         }
715                 }
716                 for (i = 0; msp_modelist[i].name != NULL; i++)
717                         if (msp_modelist[i].retval == val)
718                                 break;
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;
723
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);
729                         val = 0x0009;
730                         msp_write_dem(client, 0x20, val);
731                 }
732
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 */
737
738                 /* set stereo */
739                 switch (val) {
740                 case 0x0008: /* B/G NICAM */
741                 case 0x000a: /* I NICAM */
742                         if (val == 0x0008)
743                                 state->mode = MSP_MODE_FM_NICAM1;
744                         else
745                                 state->mode = MSP_MODE_FM_NICAM2;
746                         /* just turn on stereo */
747                         state->rxsubchans = V4L2_TUNER_SUB_STEREO;
748                         state->nicam_on = 1;
749                         state->watch_stereo = 1;
750                         msp3400c_setstereo(client,V4L2_TUNER_MODE_STEREO);
751                         break;
752                 case 0x0009:
753                         state->mode = MSP_MODE_AM_NICAM;
754                         state->rxsubchans = V4L2_TUNER_SUB_MONO;
755                         state->nicam_on = 1;
756                         msp3400c_setstereo(client,V4L2_TUNER_MODE_MONO);
757                         state->watch_stereo = 1;
758                         break;
759                 case 0x0020: /* BTSC */
760                         /* just turn on stereo */
761                         state->mode = MSP_MODE_BTSC;
762                         state->rxsubchans = V4L2_TUNER_SUB_STEREO;
763                         state->nicam_on = 0;
764                         state->watch_stereo = 1;
765                         msp3400c_setstereo(client,V4L2_TUNER_MODE_STEREO);
766                         break;
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;
771                         state->nicam_on = 0;
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),
777                                             MSP_CARRIER(10.7));
778                         /* scart routing */
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);
784                         break;
785                 case 0x0003:
786                 case 0x0004:
787                 case 0x0005:
788                         state->mode   = MSP_MODE_FM_TERRA;
789                         state->rxsubchans = V4L2_TUNER_SUB_MONO;
790                         state->audmode = V4L2_TUNER_MODE_MONO;
791                         state->nicam_on = 0;
792                         state->watch_stereo = 1;
793                         break;
794                 }
795
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);
800
801                 /* monitor tv audio mode */
802                 while (state->watch_stereo) {
803                         if (msp_sleep(state,5000))
804                                 goto restart;
805                         watch_stereo(client);
806                 }
807         }
808         v4l_dbg(1, client, "thread: exit\n");
809         return 0;
810 }
811
812 /* ----------------------------------------------------------------------- */
813
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      */
818
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
822  *
823  * this function replaces msp3400c_setstereo
824  */
825 static void msp34xxg_set_source(struct i2c_client *client, int source)
826 {
827         struct msp_state *state = i2c_get_clientdata(client);
828
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
832          */
833         int value = (source & 0x07) << 8 | (source == 0 ? 0x30 : 0x20);
834
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);
842         /*
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
848          */
849         /* a2 threshold for stereo/bilingual */
850         msp_write_dem(client, 0x22, stereo_threshold);
851         state->source = source;
852 }
853
854 /* (re-)initialize the msp34xxg, according to the current norm in state->norm
855  * return 0 if it worked, -1 if it failed
856  */
857 static int msp34xxg_reset(struct i2c_client *client)
858 {
859         struct msp_state *state = i2c_get_clientdata(client);
860         int modus, std;
861
862         if (msp_reset(client))
863                 return -1;
864
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))
868                 return -1;
869
870         msp_write_dem(client, 0x40, state->i2s_mode);
871
872         /* step-by-step initialisation, as described in the manual */
873         modus = msp_modus(client);
874         if (state->radio)
875                 std = 0x40;
876         else
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))
881                 return -1;
882         if (msp_write_dem(client, 0x20, std))
883                 return -1;
884
885         /* write the dsps that may have an influence on
886            standard/audio autodetection right now */
887         msp34xxg_set_source(client, state->source);
888
889         /* AM/FM Prescale [15:8] 75khz deviation */
890         if (msp_write_dsp(client, 0x0e, 0x3000))
891                 return -1;
892
893         /* NICAM Prescale 9db gain (as recommended) */
894         if (msp_write_dsp(client, 0x10, 0x5a00))
895                 return -1;
896
897         return 0;
898 }
899
900 int msp34xxg_thread(void *data)
901 {
902         struct i2c_client *client = data;
903         struct msp_state *state = i2c_get_clientdata(client);
904         int val, std, i;
905
906         v4l_dbg(1, client, "msp34xxg daemon started\n");
907
908         state->source = 1; /* default */
909         for (;;) {
910                 v4l_dbg(2, client, "msp34xxg thread: sleep\n");
911                 msp_sleep(state, -1);
912                 v4l_dbg(2, client, "msp34xxg thread: wakeup\n");
913
914         restart:
915                 v4l_dbg(1, client, "thread: restart scan\n");
916                 state->restart = 0;
917                 if (kthread_should_stop())
918                         break;
919
920                 /* setup the chip*/
921                 msp34xxg_reset(client);
922                 std = standard;
923                 if (std != 0x01)
924                         goto unmute;
925
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))
930                                 goto restart;
931
932                         /* check results */
933                         val = msp_read_dem(client, 0x7e);
934                         if (val < 0x07ff) {
935                                 std = val;
936                                 break;
937                         }
938                         v4l_dbg(2, client, "detection still in progress\n");
939                 }
940                 if (std == 1) {
941                         v4l_dbg(1, client, "detection still in progress after 10 tries. giving up.\n");
942                         continue;
943                 }
944
945         unmute:
946                 state->mode = std;
947                 v4l_dbg(1, client, "current mode: %s (0x%04x)\n",
948                         msp_standard_mode_name(std), std);
949
950                 /* unmute: dispatch sound to scart output, set scart volume */
951                 msp_set_audio(client);
952
953                 /* restore ACB */
954                 if (msp_write_dsp(client, 0x13, state->acb))
955                         return -1;
956
957                 msp_write_dem(client, 0x40, state->i2s_mode);
958         }
959         v4l_dbg(1, client, "thread: exit\n");
960         return 0;
961 }
962
963 void msp34xxg_detect_stereo(struct i2c_client *client)
964 {
965         struct msp_state *state = i2c_get_clientdata(client);
966
967         int status = msp_read_dem(client, 0x0200);
968         int is_bilingual = status & 0x100;
969         int is_stereo = status & 0x40;
970
971         state->rxsubchans = 0;
972         if (is_stereo)
973                 state->rxsubchans |= V4L2_TUNER_SUB_STEREO;
974         else
975                 state->rxsubchans |= V4L2_TUNER_SUB_MONO;
976         if (is_bilingual) {
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.
983                  */
984         }
985         v4l_dbg(1, client, "status=0x%x, stereo=%d, bilingual=%d -> rxsubchans=%d\n",
986                 status, is_stereo, is_bilingual, state->rxsubchans);
987 }
988
989 void msp34xxg_set_audmode(struct i2c_client *client, int audmode)
990 {
991         struct msp_state *state = i2c_get_clientdata(client);
992         int source;
993
994         switch (audmode) {
995         case V4L2_TUNER_MODE_MONO:
996                 source = 0; /* mono only */
997                 break;
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) */
1001                 break;
1002         case V4L2_TUNER_MODE_LANG1:
1003                 source = 3; /* stereo or A */
1004                 break;
1005         case V4L2_TUNER_MODE_LANG2:
1006                 source = 4; /* stereo or B */
1007                 break;
1008         default:
1009                 audmode = 0;
1010                 source  = 1;
1011                 break;
1012         }
1013         state->audmode = audmode;
1014         msp34xxg_set_source(client, source);
1015 }
1016