patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / media / video / msp3400.c
1 /*
2  * programming the msp34* sound processor family
3  *
4  * (c) 1997-2001 Gerd Knorr <kraxel@bytesex.org>
5  *
6  * what works and what doesn't:
7  *
8  *  AM-Mono
9  *      Support for Hauppauge cards added (decoding handled by tuner) added by
10  *      Frederic Crozat <fcrozat@mail.dotcom.fr>
11  *
12  *  FM-Mono
13  *      should work. The stereo modes are backward compatible to FM-mono,
14  *      therefore FM-Mono should be allways available.
15  *
16  *  FM-Stereo (B/G, used in germany)
17  *      should work, with autodetect
18  *
19  *  FM-Stereo (satellite)
20  *      should work, no autodetect (i.e. default is mono, but you can
21  *      switch to stereo -- untested)
22  *
23  *  NICAM (B/G, L , used in UK, Scandinavia, Spain and France)
24  *      should work, with autodetect. Support for NICAM was added by
25  *      Pekka Pietikainen <pp@netppl.fi>
26  *
27  *
28  * TODO:
29  *   - better SAT support
30  *
31  *
32  * 980623  Thomas Sailer (sailer@ife.ee.ethz.ch)
33  *         using soundcore instead of OSS
34  *
35  */
36
37 #include <linux/config.h>
38 #include <linux/module.h>
39 #include <linux/kernel.h>
40 #include <linux/sched.h>
41 #include <linux/string.h>
42 #include <linux/timer.h>
43 #include <linux/delay.h>
44 #include <linux/errno.h>
45 #include <linux/slab.h>
46 #include <linux/i2c.h>
47 #include <linux/videodev.h>
48 #include <linux/init.h>
49 #include <linux/smp_lock.h>
50 #include <asm/semaphore.h>
51 #include <asm/pgtable.h>
52
53 #include <media/audiochip.h>
54 #include "msp3400.h"
55
56 /* insmod parameters */
57 static int debug    = 0;    /* debug output */
58 static int once     = 0;    /* no continous stereo monitoring */
59 static int amsound  = 0;    /* hard-wire AM sound at 6.5 Hz (france),
60                               the autoscan seems work well only with FM... */
61 static int simple   = -1;   /* use short programming (>= msp3410 only) */
62 static int dolby    = 0;
63
64 #define DFP_COUNT 0x41
65 static const int bl_dfp[] = {
66         0x00, 0x01, 0x02, 0x03,  0x06, 0x08, 0x09, 0x0a,
67         0x0b, 0x0d, 0x0e, 0x10
68 };
69
70 struct msp3400c {
71         int rev1,rev2;
72         
73         int simple;
74         int mode;
75         int norm;
76         int stereo;
77         int nicam_on;
78         int acb;
79         int main, second;       /* sound carrier */
80         int input;
81
82         int muted;
83         int left, right;        /* volume */
84         int bass, treble;
85
86         /* shadow register set */
87         int dfp_regs[DFP_COUNT];
88
89         /* thread */
90         pid_t                tpid;
91         struct completion    texit;
92         wait_queue_head_t    wq;
93
94         int                  active:1;
95         int                  restart:1;
96         int                  rmmod:1;
97
98         int                  watch_stereo;
99         struct timer_list    wake_stereo;
100 };
101
102 #define HAVE_NICAM(msp)   (((msp->rev2>>8) & 0xff) != 00)
103 #define HAVE_SIMPLE(msp)  ((msp->rev1      & 0xff) >= 'D'-'@')
104 #define HAVE_RADIO(msp)   ((msp->rev1      & 0xff) >= 'G'-'@')
105
106 #define VIDEO_MODE_RADIO 16      /* norm magic for radio mode */
107
108 /* ---------------------------------------------------------------------- */
109
110 #define dprintk      if (debug >= 1) printk
111 #define d2printk     if (debug >= 2) printk
112
113 MODULE_PARM(once,"i");
114 MODULE_PARM(debug,"i");
115 MODULE_PARM(simple,"i");
116 MODULE_PARM(amsound,"i");
117 MODULE_PARM(dolby,"i");
118
119 MODULE_DESCRIPTION("device driver for msp34xx TV sound processor");
120 MODULE_AUTHOR("Gerd Knorr");
121 MODULE_LICENSE("Dual BSD/GPL"); /* FreeBSD uses this too */
122
123 /* ---------------------------------------------------------------------- */
124
125 #define I2C_MSP3400C       0x80
126 #define I2C_MSP3400C_ALT   0x88
127
128 #define I2C_MSP3400C_DEM   0x10
129 #define I2C_MSP3400C_DFP   0x12
130
131 /* Addresses to scan */
132 static unsigned short normal_i2c[] = {
133         I2C_MSP3400C      >> 1,
134         I2C_MSP3400C_ALT  >> 1,
135         I2C_CLIENT_END
136 };
137 static unsigned short normal_i2c_range[] = {I2C_CLIENT_END,I2C_CLIENT_END};
138 I2C_CLIENT_INSMOD;
139
140 /* ----------------------------------------------------------------------- */
141 /* functions for talking to the MSP3400C Sound processor                   */
142
143 #ifndef I2C_M_IGNORE_NAK
144 # define I2C_M_IGNORE_NAK 0x1000
145 #endif
146
147 static int msp3400c_reset(struct i2c_client *client)
148 {
149         /* reset and read revision code */
150         static char reset_off[3] = { 0x00, 0x80, 0x00 };
151         static char reset_on[3]  = { 0x00, 0x00, 0x00 };
152         static char write[3]     = { I2C_MSP3400C_DFP + 1, 0x00, 0x1e };
153         char read[2];
154         struct i2c_msg reset[2] = {
155                 { client->addr, I2C_M_IGNORE_NAK, 3, reset_off },
156                 { client->addr, I2C_M_IGNORE_NAK, 3, reset_on  },
157         };
158         struct i2c_msg test[2] = {
159                 { client->addr, 0,        3, write },
160                 { client->addr, I2C_M_RD, 2, read  },
161         };
162         
163         if ( (1 != i2c_transfer(client->adapter,&reset[0],1)) ||
164              (1 != i2c_transfer(client->adapter,&reset[1],1)) ||
165              (2 != i2c_transfer(client->adapter,test,2)) ) {
166                 printk(KERN_ERR "msp3400: chip reset failed\n");
167                 return -1;
168         }
169         return 0; 
170 }
171
172 static int
173 msp3400c_read(struct i2c_client *client, int dev, int addr)
174 {
175         int err;
176
177         unsigned char write[3];
178         unsigned char read[2];
179         struct i2c_msg msgs[2] = {
180                 { client->addr, 0,        3, write },
181                 { client->addr, I2C_M_RD, 2, read  }
182         };
183         write[0] = dev+1;
184         write[1] = addr >> 8;
185         write[2] = addr & 0xff;
186
187         for (err = 0; err < 3;) {
188                 if (2 == i2c_transfer(client->adapter,msgs,2))
189                         break;
190                 err++;
191                 printk(KERN_WARNING "msp34xx: I/O error #%d (read 0x%02x/0x%02x)\n",
192                        err, dev, addr);
193                 set_current_state(TASK_INTERRUPTIBLE);
194                 schedule_timeout(HZ/10);
195         }
196         if (3 == err) {
197                 printk(KERN_WARNING "msp34xx: giving up, reseting chip. Sound will go off, sorry folks :-|\n");
198                 msp3400c_reset(client);
199                 return -1;
200         }
201         return read[0] << 8 | read[1];
202 }
203
204 static int
205 msp3400c_write(struct i2c_client *client, int dev, int addr, int val)
206 {
207         int err;
208         unsigned char buffer[5];
209
210         buffer[0] = dev;
211         buffer[1] = addr >> 8;
212         buffer[2] = addr &  0xff;
213         buffer[3] = val  >> 8;
214         buffer[4] = val  &  0xff;
215
216         for (err = 0; err < 3;) {
217                 if (5 == i2c_master_send(client, buffer, 5))
218                         break;
219                 err++;
220                 printk(KERN_WARNING "msp34xx: I/O error #%d (write 0x%02x/0x%02x)\n",
221                        err, dev, addr);
222                 set_current_state(TASK_INTERRUPTIBLE);
223                 schedule_timeout(HZ/10);
224         }
225         if (3 == err) {
226                 printk(KERN_WARNING "msp34xx: giving up, reseting chip. Sound will go off, sorry folks :-|\n");
227                 msp3400c_reset(client);
228                 return -1;
229         }
230         return 0;
231 }
232
233 /* ------------------------------------------------------------------------ */
234
235 /* This macro is allowed for *constants* only, gcc must calculate it
236    at compile time.  Remember -- no floats in kernel mode */
237 #define MSP_CARRIER(freq) ((int)((float)(freq/18.432)*(1<<24)))
238
239 #define MSP_MODE_AM_DETECT   0
240 #define MSP_MODE_FM_RADIO    2
241 #define MSP_MODE_FM_TERRA    3
242 #define MSP_MODE_FM_SAT      4
243 #define MSP_MODE_FM_NICAM1   5
244 #define MSP_MODE_FM_NICAM2   6
245 #define MSP_MODE_AM_NICAM    7
246 #define MSP_MODE_BTSC        8
247 #define MSP_MODE_EXTERN      9
248
249 static struct MSP_INIT_DATA_DEM {
250         int fir1[6];
251         int fir2[6];
252         int cdo1;
253         int cdo2;
254         int ad_cv;
255         int mode_reg;
256         int dfp_src;
257         int dfp_matrix;
258 } msp_init_data[] = {
259         /* AM (for carrier detect / msp3400) */
260         { { 75, 19, 36, 35, 39, 40 }, { 75, 19, 36, 35, 39, 40 },
261           MSP_CARRIER(5.5), MSP_CARRIER(5.5),
262           0x00d0, 0x0500,   0x0020, 0x3000},
263
264         /* AM (for carrier detect / msp3410) */
265         { { -1, -1, -8, 2, 59, 126 }, { -1, -1, -8, 2, 59, 126 },
266           MSP_CARRIER(5.5), MSP_CARRIER(5.5),
267           0x00d0, 0x0100,   0x0020, 0x3000},
268
269         /* FM Radio */
270         { { -8, -8, 4, 6, 78, 107 }, { -8, -8, 4, 6, 78, 107 },
271           MSP_CARRIER(10.7), MSP_CARRIER(10.7),
272           0x00d0, 0x0480, 0x0020, 0x3000 },
273
274         /* Terrestial FM-mono + FM-stereo */
275         { {  3, 18, 27, 48, 66, 72 }, {  3, 18, 27, 48, 66, 72 },
276           MSP_CARRIER(5.5), MSP_CARRIER(5.5),
277           0x00d0, 0x0480,   0x0030, 0x3000},
278
279         /* Sat FM-mono */
280         { {  1,  9, 14, 24, 33, 37 }, {  3, 18, 27, 48, 66, 72 },
281           MSP_CARRIER(6.5), MSP_CARRIER(6.5),
282           0x00c6, 0x0480,   0x0000, 0x3000},
283
284         /* NICAM/FM --  B/G (5.5/5.85), D/K (6.5/5.85) */
285         { { -2, -8, -10, 10, 50, 86 }, {  3, 18, 27, 48, 66, 72 },
286           MSP_CARRIER(5.5), MSP_CARRIER(5.5),
287           0x00d0, 0x0040,   0x0120, 0x3000},
288
289         /* NICAM/FM -- I (6.0/6.552) */
290         { {  2, 4, -6, -4, 40, 94 }, {  3, 18, 27, 48, 66, 72 },
291           MSP_CARRIER(6.0), MSP_CARRIER(6.0),
292           0x00d0, 0x0040,   0x0120, 0x3000},
293
294         /* NICAM/AM -- L (6.5/5.85) */
295         { {  -2, -8, -10, 10, 50, 86 }, {  -4, -12, -9, 23, 79, 126 },
296           MSP_CARRIER(6.5), MSP_CARRIER(6.5),
297           0x00c6, 0x0140,   0x0120, 0x7c03},
298 };
299
300 struct CARRIER_DETECT {
301         int   cdo;
302         char *name;
303 };
304
305 static struct CARRIER_DETECT carrier_detect_main[] = {
306         /* main carrier */
307         { MSP_CARRIER(4.5),        "4.5   NTSC"                   }, 
308         { MSP_CARRIER(5.5),        "5.5   PAL B/G"                }, 
309         { MSP_CARRIER(6.0),        "6.0   PAL I"                  },
310         { MSP_CARRIER(6.5),        "6.5   PAL D/K + SAT + SECAM"  }
311 };
312
313 static struct CARRIER_DETECT carrier_detect_55[] = {
314         /* PAL B/G */
315         { MSP_CARRIER(5.7421875),  "5.742 PAL B/G FM-stereo"     }, 
316         { MSP_CARRIER(5.85),       "5.85  PAL B/G NICAM"         }
317 };
318
319 static struct CARRIER_DETECT carrier_detect_65[] = {
320         /* PAL SAT / SECAM */
321         { MSP_CARRIER(5.85),       "5.85  PAL D/K + SECAM NICAM" },
322         { MSP_CARRIER(6.2578125),  "6.25  PAL D/K1 FM-stereo" },
323         { MSP_CARRIER(6.7421875),  "6.74  PAL D/K2 FM-stereo" },
324         { MSP_CARRIER(7.02),       "7.02  PAL SAT FM-stereo s/b" },
325         { MSP_CARRIER(7.20),       "7.20  PAL SAT FM-stereo s"   },
326         { MSP_CARRIER(7.38),       "7.38  PAL SAT FM-stereo b"   },
327 };
328
329 #define CARRIER_COUNT(x) (sizeof(x)/sizeof(struct CARRIER_DETECT))
330
331 /* ----------------------------------------------------------------------- */
332
333 #define SCART_MASK    0
334 #define SCART_IN1     1
335 #define SCART_IN2     2
336 #define SCART_IN1_DA  3
337 #define SCART_IN2_DA  4
338 #define SCART_IN3     5
339 #define SCART_IN4     6
340 #define SCART_MONO    7
341 #define SCART_MUTE    8
342
343 static int scarts[3][9] = {
344   /* MASK    IN1     IN2     IN1_DA  IN2_DA  IN3     IN4     MONO    MUTE   */
345   {  0x0320, 0x0000, 0x0200, -1,     -1,     0x0300, 0x0020, 0x0100, 0x0320 },
346   {  0x0c40, 0x0440, 0x0400, 0x0c00, 0x0040, 0x0000, 0x0840, 0x0800, 0x0c40 },
347   {  0x3080, 0x1000, 0x1080, 0x0000, 0x0080, 0x2080, 0x3080, 0x2000, 0x3000 },
348 };
349
350 static char *scart_names[] = {
351   "mask", "in1", "in2", "in1 da", "in2 da", "in3", "in4", "mono", "mute"
352 };
353
354 static void
355 msp3400c_set_scart(struct i2c_client *client, int in, int out)
356 {
357         struct msp3400c *msp = i2c_get_clientdata(client);
358
359         if (-1 == scarts[out][in])
360                 return;
361
362         dprintk(KERN_DEBUG
363                 "msp34xx: scart switch: %s => %d\n",scart_names[in],out);
364         msp->acb &= ~scarts[out][SCART_MASK];
365         msp->acb |=  scarts[out][in];
366         msp3400c_write(client,I2C_MSP3400C_DFP, 0x0013, msp->acb);
367 }
368
369 /* ------------------------------------------------------------------------ */
370
371 static void msp3400c_setcarrier(struct i2c_client *client, int cdo1, int cdo2)
372 {
373         msp3400c_write(client,I2C_MSP3400C_DEM, 0x0093, cdo1 & 0xfff);
374         msp3400c_write(client,I2C_MSP3400C_DEM, 0x009b, cdo1 >> 12);
375         msp3400c_write(client,I2C_MSP3400C_DEM, 0x00a3, cdo2 & 0xfff);
376         msp3400c_write(client,I2C_MSP3400C_DEM, 0x00ab, cdo2 >> 12);
377         msp3400c_write(client,I2C_MSP3400C_DEM, 0x0056, 0); /*LOAD_REG_1/2*/
378 }
379
380 static void msp3400c_setvolume(struct i2c_client *client,
381                                int muted, int left, int right)
382 {
383         int vol = 0,val = 0,balance = 0;
384
385         if (!muted) {
386                 vol     = (left > right) ? left : right;
387                 val     = (vol * 0x73 / 65535) << 8;
388         }
389         if (vol > 0) {
390                 balance = ((right-left) * 127) / vol;
391         }
392
393         dprintk(KERN_DEBUG
394                 "msp34xx: setvolume: mute=%s %d:%d  v=0x%02x b=0x%02x\n",
395                 muted ? "on" : "off", left, right, val>>8, balance);
396         msp3400c_write(client,I2C_MSP3400C_DFP, 0x0000, val); /* loudspeaker */
397         msp3400c_write(client,I2C_MSP3400C_DFP, 0x0006, val); /* headphones  */
398         /* scart - on/off only */
399         msp3400c_write(client,I2C_MSP3400C_DFP, 0x0007, val ? 0x4000 : 0);
400         msp3400c_write(client,I2C_MSP3400C_DFP, 0x0001, balance << 8);
401 }
402
403 static void msp3400c_setbass(struct i2c_client *client, int bass)
404 {
405         int val = ((bass-32768) * 0x60 / 65535) << 8;
406
407         dprintk(KERN_DEBUG "msp34xx: setbass: %d 0x%02x\n",bass, val>>8);
408         msp3400c_write(client,I2C_MSP3400C_DFP, 0x0002, val); /* loudspeaker */
409 }
410
411 static void msp3400c_settreble(struct i2c_client *client, int treble)
412 {
413         int val = ((treble-32768) * 0x60 / 65535) << 8;
414
415         dprintk(KERN_DEBUG "msp34xx: settreble: %d 0x%02x\n",treble, val>>8);
416         msp3400c_write(client,I2C_MSP3400C_DFP, 0x0003, val); /* loudspeaker */
417 }
418
419 static void msp3400c_setmode(struct i2c_client *client, int type)
420 {
421         struct msp3400c *msp = i2c_get_clientdata(client);
422         int i;
423         
424         dprintk(KERN_DEBUG "msp3400: setmode: %d\n",type);
425         msp->mode   = type;
426         msp->stereo = VIDEO_SOUND_MONO;
427
428         msp3400c_write(client,I2C_MSP3400C_DEM, 0x00bb,          /* ad_cv */
429                        msp_init_data[type].ad_cv);
430     
431         for (i = 5; i >= 0; i--)                                   /* fir 1 */
432                 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0001,
433                                msp_init_data[type].fir1[i]);
434     
435         msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005, 0x0004); /* fir 2 */
436         msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005, 0x0040);
437         msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005, 0x0000);
438         for (i = 5; i >= 0; i--)
439                 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005,
440                                msp_init_data[type].fir2[i]);
441     
442         msp3400c_write(client,I2C_MSP3400C_DEM, 0x0083,     /* MODE_REG */
443                        msp_init_data[type].mode_reg);
444     
445         msp3400c_setcarrier(client, msp_init_data[type].cdo1,
446                             msp_init_data[type].cdo2);
447     
448         msp3400c_write(client,I2C_MSP3400C_DEM, 0x0056, 0); /*LOAD_REG_1/2*/
449
450         if (dolby) {
451                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008,
452                                0x0520); /* I2S1 */
453                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009,
454                                0x0620); /* I2S2 */
455                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b,
456                                msp_init_data[type].dfp_src);
457         } else {
458                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008,
459                                msp_init_data[type].dfp_src);
460                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009,
461                                msp_init_data[type].dfp_src);
462                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b,
463                                msp_init_data[type].dfp_src);
464         }
465         msp3400c_write(client,I2C_MSP3400C_DFP, 0x000a,
466                        msp_init_data[type].dfp_src);
467         msp3400c_write(client,I2C_MSP3400C_DFP, 0x000e,
468                        msp_init_data[type].dfp_matrix);
469
470         if (HAVE_NICAM(msp)) {
471                 /* nicam prescale */
472                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0010, 0x5a00); /* was: 0x3000 */
473         }
474 }
475
476 /* turn on/off nicam + stereo */
477 static void msp3400c_setstereo(struct i2c_client *client, int mode)
478 {
479         static char *strmode[16] = {
480 #if __GNUC__ >= 3
481                 [ 0 ... 15 ]           = "invalid",
482 #endif
483                 [ VIDEO_SOUND_MONO ]   = "mono",
484                 [ VIDEO_SOUND_STEREO ] = "stereo",
485                 [ VIDEO_SOUND_LANG1  ] = "lang1",
486                 [ VIDEO_SOUND_LANG2  ] = "lang2",
487         };
488         struct msp3400c *msp = i2c_get_clientdata(client);
489         int nicam=0; /* channel source: FM/AM or nicam */
490         int src=0;
491
492         /* switch demodulator */
493         switch (msp->mode) {
494         case MSP_MODE_FM_TERRA:
495                 dprintk(KERN_DEBUG "msp3400: FM setstereo: %s\n",strmode[mode]);
496                 msp3400c_setcarrier(client,msp->second,msp->main);
497                 switch (mode) {
498                 case VIDEO_SOUND_STEREO:
499                         msp3400c_write(client,I2C_MSP3400C_DFP, 0x000e, 0x3001);
500                         break;
501                 case VIDEO_SOUND_MONO:
502                 case VIDEO_SOUND_LANG1:
503                 case VIDEO_SOUND_LANG2:
504                         msp3400c_write(client,I2C_MSP3400C_DFP, 0x000e, 0x3000);
505                         break;
506                 }
507                 break;
508         case MSP_MODE_FM_SAT:
509                 dprintk(KERN_DEBUG "msp3400: SAT setstereo: %s\n",strmode[mode]);
510                 switch (mode) {
511                 case VIDEO_SOUND_MONO:
512                         msp3400c_setcarrier(client, MSP_CARRIER(6.5), MSP_CARRIER(6.5));
513                         break;
514                 case VIDEO_SOUND_STEREO:
515                         msp3400c_setcarrier(client, MSP_CARRIER(7.2), MSP_CARRIER(7.02));
516                         break;
517                 case VIDEO_SOUND_LANG1:
518                         msp3400c_setcarrier(client, MSP_CARRIER(7.38), MSP_CARRIER(7.02));
519                         break;
520                 case VIDEO_SOUND_LANG2:
521                         msp3400c_setcarrier(client, MSP_CARRIER(7.38), MSP_CARRIER(7.02));
522                         break;
523                 }
524                 break;
525         case MSP_MODE_FM_NICAM1:
526         case MSP_MODE_FM_NICAM2:
527         case MSP_MODE_AM_NICAM:
528                 dprintk(KERN_DEBUG "msp3400: NICAM setstereo: %s\n",strmode[mode]);
529                 msp3400c_setcarrier(client,msp->second,msp->main);
530                 if (msp->nicam_on)
531                         nicam=0x0100;
532                 break;
533         case MSP_MODE_BTSC:
534                 dprintk(KERN_DEBUG "msp3400: BTSC setstereo: %s\n",strmode[mode]);
535                 nicam=0x0300;
536                 break;
537         case MSP_MODE_EXTERN:
538                 dprintk(KERN_DEBUG "msp3400: extern setstereo: %s\n",strmode[mode]);
539                 nicam = 0x0200;
540                 break;
541         case MSP_MODE_FM_RADIO:
542                 dprintk(KERN_DEBUG "msp3400: FM-Radio setstereo: %s\n",strmode[mode]);
543                 break;
544         default:
545                 dprintk(KERN_DEBUG "msp3400: mono setstereo\n");
546                 return;
547         }
548
549         /* switch audio */
550         switch (mode) {
551         case VIDEO_SOUND_STEREO:
552                 src = 0x0020 | nicam;
553 #if 0 
554                 /* spatial effect */
555                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0005,0x4000);
556 #endif
557                 break;
558         case VIDEO_SOUND_MONO:
559                 if (msp->mode == MSP_MODE_AM_NICAM) {
560                         dprintk("msp3400: switching to AM mono\n");
561                         /* AM mono decoding is handled by tuner, not MSP chip */
562                         /* SCART switching control register */
563                         msp3400c_set_scart(client,SCART_MONO,0);
564                         src = 0x0200;
565                         break;
566                 }
567         case VIDEO_SOUND_LANG1:
568                 src = 0x0000 | nicam;
569                 break;
570         case VIDEO_SOUND_LANG2:
571                 src = 0x0010 | nicam;
572                 break;
573         }
574         dprintk(KERN_DEBUG
575                 "msp3400: setstereo final source/matrix = 0x%x\n", src);
576
577         if (dolby) {
578                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008,0x0520);
579                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009,0x0620);
580                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000a,src);
581                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b,src);
582         } else {
583                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008,src);
584                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009,src);
585                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000a,src);
586                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b,src);
587         }
588 }
589
590 static void
591 msp3400c_print_mode(struct msp3400c *msp)
592 {
593         if (msp->main == msp->second) {
594                 printk(KERN_DEBUG "msp3400: mono sound carrier: %d.%03d MHz\n",
595                        msp->main/910000,(msp->main/910)%1000);
596         } else {
597                 printk(KERN_DEBUG "msp3400: main sound carrier: %d.%03d MHz\n",
598                        msp->main/910000,(msp->main/910)%1000);
599         }
600         if (msp->mode == MSP_MODE_FM_NICAM1 ||
601             msp->mode == MSP_MODE_FM_NICAM2)
602                 printk(KERN_DEBUG "msp3400: NICAM/FM carrier   : %d.%03d MHz\n",
603                        msp->second/910000,(msp->second/910)%1000);
604         if (msp->mode == MSP_MODE_AM_NICAM)
605                 printk(KERN_DEBUG "msp3400: NICAM/AM carrier   : %d.%03d MHz\n",
606                        msp->second/910000,(msp->second/910)%1000);
607         if (msp->mode == MSP_MODE_FM_TERRA &&
608             msp->main != msp->second) {
609                 printk(KERN_DEBUG "msp3400: FM-stereo carrier : %d.%03d MHz\n",
610                        msp->second/910000,(msp->second/910)%1000);
611         }
612 }
613
614 static void
615 msp3400c_restore_dfp(struct i2c_client *client)
616 {
617         struct msp3400c *msp = i2c_get_clientdata(client);
618         int i;
619
620         for (i = 0; i < DFP_COUNT; i++) {
621                 if (-1 == msp->dfp_regs[i])
622                         continue;
623                 msp3400c_write(client,I2C_MSP3400C_DFP, i, msp->dfp_regs[i]);
624         }
625 }
626
627 /* ----------------------------------------------------------------------- */
628
629 struct REGISTER_DUMP {
630         int   addr;
631         char *name;
632 };
633
634 struct REGISTER_DUMP d1[] = {
635         { 0x007e, "autodetect" },
636         { 0x0023, "C_AD_BITS " },
637         { 0x0038, "ADD_BITS  " },
638         { 0x003e, "CIB_BITS  " },
639         { 0x0057, "ERROR_RATE" },
640 };
641
642 static int
643 autodetect_stereo(struct i2c_client *client)
644 {
645         struct msp3400c *msp = i2c_get_clientdata(client);
646         int val;
647         int newstereo = msp->stereo;
648         int newnicam  = msp->nicam_on;
649         int update = 0;
650
651         switch (msp->mode) {
652         case MSP_MODE_FM_TERRA:
653                 val = msp3400c_read(client, I2C_MSP3400C_DFP, 0x18);
654                 if (val > 32767)
655                         val -= 65536;
656                 dprintk(KERN_DEBUG
657                         "msp34xx: stereo detect register: %d\n",val);
658                 if (val > 4096) {
659                         newstereo = VIDEO_SOUND_STEREO | VIDEO_SOUND_MONO;
660                 } else if (val < -4096) {
661                         newstereo = VIDEO_SOUND_LANG1 | VIDEO_SOUND_LANG2;
662                 } else {
663                         newstereo = VIDEO_SOUND_MONO;
664                 }
665                 newnicam = 0;
666                 break;
667         case MSP_MODE_FM_NICAM1:
668         case MSP_MODE_FM_NICAM2:
669         case MSP_MODE_AM_NICAM:
670                 val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x23);
671                 dprintk(KERN_DEBUG
672                         "msp34xx: nicam sync=%d, mode=%d\n",
673                         val & 1, (val & 0x1e) >> 1);
674
675                 if (val & 1) {
676                         /* nicam synced */
677                         switch ((val & 0x1e) >> 1)  {
678                         case 0:
679                         case 8:
680                                 newstereo = VIDEO_SOUND_STEREO;
681                                 break;
682                         case 1:
683                         case 9:
684                                 newstereo = VIDEO_SOUND_MONO
685                                         | VIDEO_SOUND_LANG1;
686                                 break;
687                         case 2:
688                         case 10:
689                                 newstereo = VIDEO_SOUND_MONO
690                                         | VIDEO_SOUND_LANG1
691                                         | VIDEO_SOUND_LANG2;
692                                 break;
693                         default:
694                                 newstereo = VIDEO_SOUND_MONO;
695                                 break;
696                         }
697                         newnicam=1;
698                 } else {
699                         newnicam = 0;
700                         newstereo = VIDEO_SOUND_MONO;
701                 }
702                 break;
703         case MSP_MODE_BTSC:
704                 val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x200);
705                 dprintk(KERN_DEBUG
706                         "msp3410: status=0x%x (pri=%s, sec=%s, %s%s%s)\n",
707                         val,
708                         (val & 0x0002) ? "no"     : "yes",
709                         (val & 0x0004) ? "no"     : "yes",
710                         (val & 0x0040) ? "stereo" : "mono",
711                         (val & 0x0080) ? ", nicam 2nd mono" : "",
712                         (val & 0x0100) ? ", bilingual/SAP"  : "");
713                 newstereo = VIDEO_SOUND_MONO;
714                 if (val & 0x0040) newstereo |= VIDEO_SOUND_STEREO;
715                 if (val & 0x0100) newstereo |= VIDEO_SOUND_LANG1;
716                 break;
717         }
718         if (newstereo != msp->stereo) {
719                 update = 1;
720                 dprintk(KERN_DEBUG "msp34xx: watch: stereo %d => %d\n",
721                         msp->stereo,newstereo);
722                 msp->stereo   = newstereo;
723         }
724         if (newnicam != msp->nicam_on) {
725                 update = 1;
726                 dprintk(KERN_DEBUG "msp34xx: watch: nicam %d => %d\n",
727                         msp->nicam_on,newnicam);
728                 msp->nicam_on = newnicam;
729         }
730         return update;
731 }
732
733 /*
734  * A kernel thread for msp3400 control -- we don't want to block the
735  * in the ioctl while doing the sound carrier & stereo detect
736  */
737
738 static int msp34xx_sleep(struct msp3400c *msp, int timeout)
739 {
740         DECLARE_WAITQUEUE(wait, current);
741
742         add_wait_queue(&msp->wq, &wait);
743         if (!msp->rmmod) {
744                 set_current_state(TASK_INTERRUPTIBLE);
745                 if (timeout < 0)
746                         schedule();
747                 else
748                         schedule_timeout(timeout);
749         }
750         remove_wait_queue(&msp->wq, &wait);
751         return msp->rmmod || signal_pending(current);
752 }
753
754 static void msp3400c_stereo_wake(unsigned long data)
755 {
756         struct msp3400c *msp = (struct msp3400c*)data;   /* XXX alpha ??? */
757
758         wake_up_interruptible(&msp->wq);
759 }
760
761 /* stereo/multilang monitoring */
762 static void watch_stereo(struct i2c_client *client)
763 {
764         struct msp3400c *msp = i2c_get_clientdata(client);
765
766         if (autodetect_stereo(client)) {
767                 if (msp->stereo & VIDEO_SOUND_STEREO)
768                         msp3400c_setstereo(client,VIDEO_SOUND_STEREO);
769                 else if (msp->stereo & VIDEO_SOUND_LANG1)
770                         msp3400c_setstereo(client,VIDEO_SOUND_LANG1);
771                 else if (msp->stereo & VIDEO_SOUND_LANG2)
772                         msp3400c_setstereo(client,VIDEO_SOUND_LANG2);
773                 else
774                         msp3400c_setstereo(client,VIDEO_SOUND_MONO);
775         }
776         if (once)
777                 msp->watch_stereo = 0;
778         if (msp->watch_stereo)
779                 mod_timer(&msp->wake_stereo, jiffies+5*HZ);
780 }
781
782 static int msp3400c_thread(void *data)
783 {
784         struct i2c_client *client = data;
785         struct msp3400c *msp = i2c_get_clientdata(client);
786         
787         struct CARRIER_DETECT *cd;
788         int count, max1,max2,val1,val2, val,this;
789         
790         daemonize("msp3400");
791         allow_signal(SIGTERM);
792         printk("msp3400: daemon started\n");
793
794         for (;;) {
795                 d2printk("msp3400: thread: sleep\n");
796                 if (msp34xx_sleep(msp,-1))
797                         goto done;
798
799                 d2printk("msp3400: thread: wakeup\n");
800                 msp->active = 1;
801
802                 if (msp->watch_stereo) {
803                         watch_stereo(client);
804                         msp->active = 0;
805                         continue;
806                 }
807
808                 /* some time for the tuner to sync */
809                 if (msp34xx_sleep(msp,HZ/5))
810                         goto done;
811                 
812         restart:
813                 if (VIDEO_MODE_RADIO == msp->norm ||
814                     MSP_MODE_EXTERN  == msp->mode) {
815                         /* no carrier scan, just unmute */
816                         printk("msp3400: thread: no carrier scan\n");
817                         msp3400c_setvolume(client, msp->muted,
818                                            msp->left, msp->right);
819                         continue;
820                 }
821                 msp->restart = 0;
822                 msp3400c_setvolume(client, msp->muted, 0, 0);
823                 msp3400c_setmode(client, MSP_MODE_AM_DETECT /* +1 */ );
824                 val1 = val2 = 0;
825                 max1 = max2 = -1;
826                 del_timer(&msp->wake_stereo);
827                 msp->watch_stereo = 0;
828
829                 /* carrier detect pass #1 -- main carrier */
830                 cd = carrier_detect_main; count = CARRIER_COUNT(carrier_detect_main);
831
832                 if (amsound && (msp->norm == VIDEO_MODE_SECAM)) {
833                         /* autodetect doesn't work well with AM ... */
834                         max1 = 3;
835                         count = 0;
836                         dprintk("msp3400: AM sound override\n");
837                 }
838
839                 for (this = 0; this < count; this++) {
840                         msp3400c_setcarrier(client, cd[this].cdo,cd[this].cdo);
841
842                         if (msp34xx_sleep(msp,HZ/10))
843                                 goto done;
844                         if (msp->restart)
845                                 msp->restart = 0;
846
847                         val = msp3400c_read(client, I2C_MSP3400C_DFP, 0x1b);
848                         if (val > 32767)
849                                 val -= 65536;
850                         if (val1 < val)
851                                 val1 = val, max1 = this;
852                         dprintk("msp3400: carrier1 val: %5d / %s\n", val,cd[this].name);
853                 }
854         
855                 /* carrier detect pass #2 -- second (stereo) carrier */
856                 switch (max1) {
857                 case 1: /* 5.5 */
858                         cd = carrier_detect_55; count = CARRIER_COUNT(carrier_detect_55);
859                         break;
860                 case 3: /* 6.5 */
861                         cd = carrier_detect_65; count = CARRIER_COUNT(carrier_detect_65);
862                         break;
863                 case 0: /* 4.5 */
864                 case 2: /* 6.0 */
865                 default:
866                         cd = NULL; count = 0;
867                         break;
868                 }
869                 
870                 if (amsound && (msp->norm == VIDEO_MODE_SECAM)) {
871                         /* autodetect doesn't work well with AM ... */
872                         cd = NULL; count = 0; max2 = 0;
873                 }
874                 for (this = 0; this < count; this++) {
875                         msp3400c_setcarrier(client, cd[this].cdo,cd[this].cdo);
876
877                         if (msp34xx_sleep(msp,HZ/10))
878                                 goto done;
879                         if (msp->restart)
880                                 goto restart;
881
882                         val = msp3400c_read(client, I2C_MSP3400C_DFP, 0x1b);
883                         if (val > 32767)
884                                 val -= 65536;
885                         if (val2 < val)
886                                 val2 = val, max2 = this;
887                         dprintk("msp3400: carrier2 val: %5d / %s\n", val,cd[this].name);
888                 }
889
890                 /* programm the msp3400 according to the results */
891                 msp->main   = carrier_detect_main[max1].cdo;
892                 switch (max1) {
893                 case 1: /* 5.5 */
894                         if (max2 == 0) {
895                                 /* B/G FM-stereo */
896                                 msp->second = carrier_detect_55[max2].cdo;
897                                 msp3400c_setmode(client, MSP_MODE_FM_TERRA);
898                                 msp->nicam_on = 0;
899                                 msp3400c_setstereo(client, VIDEO_SOUND_MONO);
900                                 msp->watch_stereo = 1;
901                         } else if (max2 == 1 && HAVE_NICAM(msp)) {
902                                 /* B/G NICAM */
903                                 msp->second = carrier_detect_55[max2].cdo;
904                                 msp3400c_setmode(client, MSP_MODE_FM_NICAM1);
905                                 msp->nicam_on = 1;
906                                 msp3400c_setcarrier(client, msp->second, msp->main);
907                                 msp->watch_stereo = 1;
908                         } else {
909                                 goto no_second;
910                         }
911                         break;
912                 case 2: /* 6.0 */
913                         /* PAL I NICAM */
914                         msp->second = MSP_CARRIER(6.552);
915                         msp3400c_setmode(client, MSP_MODE_FM_NICAM2);
916                         msp->nicam_on = 1;
917                         msp3400c_setcarrier(client, msp->second, msp->main);
918                         msp->watch_stereo = 1;
919                         break;
920                 case 3: /* 6.5 */
921                         if (max2 == 1 || max2 == 2) {
922                                 /* D/K FM-stereo */
923                                 msp->second = carrier_detect_65[max2].cdo;
924                                 msp3400c_setmode(client, MSP_MODE_FM_TERRA);
925                                 msp->nicam_on = 0;
926                                 msp3400c_setstereo(client, VIDEO_SOUND_MONO);
927                                 msp->watch_stereo = 1;
928                         } else if (max2 == 0 &&
929                                    msp->norm == VIDEO_MODE_SECAM) {
930                                 /* L NICAM or AM-mono */
931                                 msp->second = carrier_detect_65[max2].cdo;
932                                 msp3400c_setmode(client, MSP_MODE_AM_NICAM);
933                                 msp->nicam_on = 0;
934                                 msp3400c_setstereo(client, VIDEO_SOUND_MONO);
935                                 msp3400c_setcarrier(client, msp->second, msp->main);
936                                 /* volume prescale for SCART (AM mono input) */
937                                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000d, 0x1900);
938                                 msp->watch_stereo = 1;
939                         } else if (max2 == 0 && HAVE_NICAM(msp)) {
940                                 /* D/K NICAM */
941                                 msp->second = carrier_detect_65[max2].cdo;
942                                 msp3400c_setmode(client, MSP_MODE_FM_NICAM1);
943                                 msp->nicam_on = 1;
944                                 msp3400c_setcarrier(client, msp->second, msp->main);
945                                 msp->watch_stereo = 1;
946                         } else {
947                                 goto no_second;
948                         }
949                         break;
950                 case 0: /* 4.5 */
951                 default:
952                 no_second:
953                         msp->second = carrier_detect_main[max1].cdo;
954                         msp3400c_setmode(client, MSP_MODE_FM_TERRA);
955                         msp->nicam_on = 0;
956                         msp3400c_setcarrier(client, msp->second, msp->main);
957                         msp->stereo = VIDEO_SOUND_MONO;
958                         msp3400c_setstereo(client, VIDEO_SOUND_MONO);
959                         break;
960                 }
961
962                 /* unmute + restore dfp registers */
963                 msp3400c_setvolume(client, msp->muted, msp->left, msp->right);
964                 msp3400c_restore_dfp(client);
965
966                 if (msp->watch_stereo)
967                         mod_timer(&msp->wake_stereo, jiffies+5*HZ);
968
969                 if (debug)
970                         msp3400c_print_mode(msp);
971                 
972                 msp->active = 0;
973         }
974
975 done:
976         msp->active = 0;
977         dprintk(KERN_DEBUG "msp3400: thread: exit\n");
978         complete_and_exit(&msp->texit, 0);
979 }
980
981 /* ----------------------------------------------------------------------- */
982 /* this one uses the automatic sound standard detection of newer           */
983 /* msp34xx chip versions                                                   */
984
985 static struct MODES {
986         int retval;
987         int main, second;
988         char *name;
989 } modelist[] = {
990         { 0x0000, 0, 0, "ERROR" },
991         { 0x0001, 0, 0, "autodetect start" },
992         { 0x0002, MSP_CARRIER(4.5), MSP_CARRIER(4.72), "4.5/4.72  M Dual FM-Stereo" },
993         { 0x0003, MSP_CARRIER(5.5), MSP_CARRIER(5.7421875), "5.5/5.74  B/G Dual FM-Stereo" },
994         { 0x0004, MSP_CARRIER(6.5), MSP_CARRIER(6.2578125), "6.5/6.25  D/K1 Dual FM-Stereo" },
995         { 0x0005, MSP_CARRIER(6.5), MSP_CARRIER(6.7421875), "6.5/6.74  D/K2 Dual FM-Stereo" },
996         { 0x0006, MSP_CARRIER(6.5), MSP_CARRIER(6.5), "6.5  D/K FM-Mono (HDEV3)" },
997         { 0x0008, MSP_CARRIER(5.5), MSP_CARRIER(5.85), "5.5/5.85  B/G NICAM FM" },
998         { 0x0009, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85  L NICAM AM" },
999         { 0x000a, MSP_CARRIER(6.0), MSP_CARRIER(6.55), "6.0/6.55  I NICAM FM" },
1000         { 0x000b, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85  D/K NICAM FM" },
1001         { 0x000c, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85  D/K NICAM FM (HDEV2)" },
1002         { 0x0020, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5  M BTSC-Stereo" },
1003         { 0x0021, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5  M BTSC-Mono + SAP" },
1004         { 0x0030, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5  M EIA-J Japan Stereo" },
1005         { 0x0040, MSP_CARRIER(10.7), MSP_CARRIER(10.7), "10.7  FM-Stereo Radio" },
1006         { 0x0050, MSP_CARRIER(6.5), MSP_CARRIER(6.5), "6.5  SAT-Mono" },
1007         { 0x0051, MSP_CARRIER(7.02), MSP_CARRIER(7.20), "7.02/7.20  SAT-Stereo" },
1008         { 0x0060, MSP_CARRIER(7.2), MSP_CARRIER(7.2), "7.2  SAT ADR" },
1009         {     -1, 0, 0, NULL }, /* EOF */
1010 };
1011  
1012 static int msp3410d_thread(void *data)
1013 {
1014         struct i2c_client *client = data;
1015         struct msp3400c *msp = i2c_get_clientdata(client);
1016         int mode,val,i,std;
1017     
1018         daemonize("msp3410 [auto]");
1019         allow_signal(SIGTERM);
1020         printk("msp3410: daemon started\n");
1021
1022         for (;;) {
1023                 d2printk(KERN_DEBUG "msp3410: thread: sleep\n");
1024                 if (msp34xx_sleep(msp,-1))
1025                         goto done;
1026
1027                 d2printk(KERN_DEBUG "msp3410: thread: wakeup\n");
1028                 msp->active = 1;
1029
1030                 if (msp->watch_stereo) {
1031                         watch_stereo(client);
1032                         msp->active = 0;
1033                         continue;
1034                 }
1035         
1036                 /* some time for the tuner to sync */
1037                 if (msp34xx_sleep(msp,HZ/5))
1038                         goto done;
1039
1040         restart:
1041                 if (msp->mode == MSP_MODE_EXTERN) {
1042                         /* no carrier scan needed, just unmute */
1043                         dprintk(KERN_DEBUG "msp3410: thread: no carrier scan\n");
1044                         msp3400c_setvolume(client, msp->muted,
1045                                            msp->left, msp->right);
1046                         continue;
1047                 }
1048                 msp->restart = 0;
1049                 del_timer(&msp->wake_stereo);
1050                 msp->watch_stereo = 0;
1051
1052                 /* put into sane state (and mute) */
1053                 msp3400c_reset(client);
1054
1055                 /* start autodetect */
1056                 switch (msp->norm) {
1057                 case VIDEO_MODE_PAL:
1058                         mode = 0x1003;
1059                         std  = 1;
1060                         break;
1061                 case VIDEO_MODE_NTSC:  /* BTSC */
1062                         mode = 0x2003;
1063                         std  = 0x0020;
1064                         break;
1065                 case VIDEO_MODE_SECAM: 
1066                         mode = 0x0003;
1067                         std  = 1;
1068                         break;
1069                 case VIDEO_MODE_RADIO:
1070                         mode = 0x0003;
1071                         std  = 0x0040;
1072                         break;
1073                 default:
1074                         mode = 0x0003;
1075                         std  = 1;
1076                         break;
1077                 }
1078                 msp3400c_write(client, I2C_MSP3400C_DEM, 0x30, mode);
1079                 msp3400c_write(client, I2C_MSP3400C_DEM, 0x20, std);
1080
1081                 if (debug) {
1082                         int i;
1083                         for (i = 0; modelist[i].name != NULL; i++)
1084                                 if (modelist[i].retval == std)
1085                                         break;
1086                         printk(KERN_DEBUG "msp3410: setting mode: %s (0x%04x)\n",
1087                                modelist[i].name ? modelist[i].name : "unknown",std);
1088                 }
1089
1090                 if (std != 1) {
1091                         /* programmed some specific mode */
1092                         val = std;
1093                 } else {
1094                         /* triggered autodetect */
1095                         for (;;) {
1096                                 if (msp34xx_sleep(msp,HZ/10))
1097                                         goto done;
1098                                 if (msp->restart)
1099                                         goto restart;
1100
1101                                 /* check results */
1102                                 val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x7e);
1103                                 if (val < 0x07ff)
1104                                         break;
1105                                 dprintk(KERN_DEBUG "msp3410: detection still in progress\n");
1106                         }
1107                 }
1108                 for (i = 0; modelist[i].name != NULL; i++)
1109                         if (modelist[i].retval == val)
1110                                 break;
1111                 dprintk(KERN_DEBUG "msp3410: current mode: %s (0x%04x)\n",
1112                         modelist[i].name ? modelist[i].name : "unknown",
1113                         val);
1114                 msp->main   = modelist[i].main;
1115                 msp->second = modelist[i].second;
1116
1117                 if (amsound && (msp->norm == VIDEO_MODE_SECAM) && (val != 0x0009)) {
1118                         /* autodetection has failed, let backup */
1119                         dprintk(KERN_DEBUG "msp3410: autodetection failed,"
1120                                 " switching to backup mode: %s (0x%04x)\n",
1121                                 modelist[8].name ? modelist[8].name : "unknown",val);
1122                         val = 0x0009;
1123                         msp3400c_write(client, I2C_MSP3400C_DEM, 0x20, val);
1124                 }
1125
1126                 /* set various prescales */
1127                 msp3400c_write(client, I2C_MSP3400C_DFP, 0x0d, 0x1900); /* scart */
1128                 msp3400c_write(client, I2C_MSP3400C_DFP, 0x0e, 0x2403); /* FM */
1129                 msp3400c_write(client, I2C_MSP3400C_DFP, 0x10, 0x5a00); /* nicam */
1130
1131                 /* set stereo */
1132                 switch (val) {
1133                 case 0x0008: /* B/G NICAM */
1134                 case 0x000a: /* I NICAM */
1135                         if (val == 0x0008)
1136                                 msp->mode = MSP_MODE_FM_NICAM1;
1137                         else
1138                                 msp->mode = MSP_MODE_FM_NICAM2;
1139                         /* just turn on stereo */
1140                         msp->stereo = VIDEO_SOUND_STEREO;
1141                         msp->nicam_on = 1;
1142                         msp->watch_stereo = 1;
1143                         msp3400c_setstereo(client,VIDEO_SOUND_STEREO);
1144                         break;
1145                 case 0x0009:                    
1146                         msp->mode = MSP_MODE_AM_NICAM;
1147                         msp->stereo = VIDEO_SOUND_MONO;
1148                         msp->nicam_on = 1;
1149                         msp3400c_setstereo(client,VIDEO_SOUND_MONO);
1150                         msp->watch_stereo = 1;
1151                         break;
1152                 case 0x0020: /* BTSC */
1153                         /* just turn on stereo */
1154                         msp->mode   = MSP_MODE_BTSC;
1155                         msp->stereo = VIDEO_SOUND_STEREO;
1156                         msp->nicam_on = 0;
1157                         msp->watch_stereo = 1;
1158                         msp3400c_setstereo(client,VIDEO_SOUND_STEREO);
1159                         break;
1160                 case 0x0040: /* FM radio */
1161                         msp->mode   = MSP_MODE_FM_RADIO;
1162                         msp->stereo = VIDEO_SOUND_STEREO;
1163                         msp->nicam_on = 0;
1164                         msp->watch_stereo = 0;
1165                         /* not needed in theory if HAVE_RADIO(), but
1166                            short programming enables carrier mute */
1167                         msp3400c_setmode(client,MSP_MODE_FM_RADIO);
1168                         msp3400c_setcarrier(client, MSP_CARRIER(10.7),
1169                                             MSP_CARRIER(10.7));
1170                         /* scart routing */
1171                         msp3400c_set_scart(client,SCART_IN2,0);
1172 #if 0
1173                         /* radio from SCART_IN2 */
1174                         msp3400c_write(client,I2C_MSP3400C_DFP, 0x08, 0x0220);
1175                         msp3400c_write(client,I2C_MSP3400C_DFP, 0x09, 0x0220);
1176                         msp3400c_write(client,I2C_MSP3400C_DFP, 0x0b, 0x0220);
1177 #else
1178                         /* msp34xx does radio decoding */
1179                         msp3400c_write(client,I2C_MSP3400C_DFP, 0x08, 0x0020);
1180                         msp3400c_write(client,I2C_MSP3400C_DFP, 0x09, 0x0020);
1181                         msp3400c_write(client,I2C_MSP3400C_DFP, 0x0b, 0x0020);
1182 #endif
1183                         break;
1184                 case 0x0003:
1185                 case 0x0004:
1186                 case 0x0005:
1187                         msp->mode   = MSP_MODE_FM_TERRA;
1188                         msp->stereo = VIDEO_SOUND_MONO;
1189                         msp->nicam_on = 0;
1190                         msp->watch_stereo = 1;
1191                         break;
1192                 }
1193                 
1194                 /* unmute + restore dfp registers */
1195                 msp3400c_setbass(client, msp->bass);
1196                 msp3400c_settreble(client, msp->treble);
1197                 msp3400c_setvolume(client, msp->muted, msp->left, msp->right);
1198                 msp3400c_restore_dfp(client);
1199
1200                 if (msp->watch_stereo)
1201                         mod_timer(&msp->wake_stereo, jiffies+HZ);
1202
1203                 msp->active = 0;
1204         }
1205
1206 done:
1207         msp->active = 0;
1208         dprintk(KERN_DEBUG "msp3410: thread: exit\n");
1209         complete_and_exit(&msp->texit, 0);
1210         return 0;
1211 }
1212
1213 /* ----------------------------------------------------------------------- */
1214
1215 static int msp_attach(struct i2c_adapter *adap, int addr, int kind);
1216 static int msp_detach(struct i2c_client *client);
1217 static int msp_probe(struct i2c_adapter *adap);
1218 static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg);
1219
1220 static struct i2c_driver driver = {
1221         .owner          = THIS_MODULE,
1222         .name           = "i2c msp3400 driver",
1223         .id             = I2C_DRIVERID_MSP3400,
1224         .flags          = I2C_DF_NOTIFY,
1225         .attach_adapter = msp_probe,
1226         .detach_client  = msp_detach,
1227         .command        = msp_command,
1228 };
1229
1230 static struct i2c_client client_template = 
1231 {
1232         I2C_DEVNAME("(unset)"),
1233         .flags     = I2C_CLIENT_ALLOW_USE,
1234         .driver    = &driver,
1235 };
1236
1237 static int msp_attach(struct i2c_adapter *adap, int addr, int kind)
1238 {
1239         struct msp3400c *msp;
1240         struct i2c_client *c;
1241         int i;
1242
1243         client_template.adapter = adap;
1244         client_template.addr = addr;
1245
1246         if (-1 == msp3400c_reset(&client_template)) {
1247                 dprintk("msp3400: no chip found\n");
1248                 return -1;
1249         }
1250         
1251         if (NULL == (c = kmalloc(sizeof(struct i2c_client),GFP_KERNEL)))
1252                 return -ENOMEM;
1253         memcpy(c,&client_template,sizeof(struct i2c_client));
1254         if (NULL == (msp = kmalloc(sizeof(struct msp3400c),GFP_KERNEL))) {
1255                 kfree(c);
1256                 return -ENOMEM;
1257         }
1258         
1259         memset(msp,0,sizeof(struct msp3400c));
1260         msp->left   = 65535;
1261         msp->right  = 65535;
1262         msp->bass   = 32768;
1263         msp->treble = 32768;
1264         msp->input  = -1;
1265         msp->muted  = 1;
1266         for (i = 0; i < DFP_COUNT; i++)
1267                 msp->dfp_regs[i] = -1;
1268
1269         i2c_set_clientdata(c, msp);
1270         init_waitqueue_head(&msp->wq);
1271
1272         if (-1 == msp3400c_reset(c)) {
1273                 kfree(msp);
1274                 kfree(c);
1275                 dprintk("msp3400: no chip found\n");
1276                 return -1;
1277         }
1278     
1279         msp->rev1 = msp3400c_read(c, I2C_MSP3400C_DFP, 0x1e);
1280         if (-1 != msp->rev1)
1281                 msp->rev2 = msp3400c_read(c, I2C_MSP3400C_DFP, 0x1f);
1282         if ((-1 == msp->rev1) || (0 == msp->rev1 && 0 == msp->rev2)) {
1283                 kfree(msp);
1284                 kfree(c);
1285                 printk("msp3400: error while reading chip version\n");
1286                 return -1;
1287         }
1288
1289 #if 0
1290         /* this will turn on a 1kHz beep - might be useful for debugging... */
1291         msp3400c_write(c,I2C_MSP3400C_DFP, 0x0014, 0x1040);
1292 #endif
1293         msp3400c_setvolume(c,msp->muted,msp->left,msp->right);
1294
1295         snprintf(c->name, sizeof(c->name), "MSP34%02d%c-%c%d",
1296                  (msp->rev2>>8)&0xff, (msp->rev1&0xff)+'@',
1297                  ((msp->rev1>>8)&0xff)+'@', msp->rev2&0x1f);
1298
1299         if (simple == -1) {
1300                 /* default mode */
1301                 msp->simple = HAVE_SIMPLE(msp);
1302         } else {
1303                 /* use insmod option */
1304                 msp->simple = simple;
1305         }
1306
1307         /* timer for stereo checking */
1308         init_timer(&msp->wake_stereo);
1309         msp->wake_stereo.function = msp3400c_stereo_wake;
1310         msp->wake_stereo.data     = (unsigned long)msp;
1311
1312         /* hello world :-) */
1313         printk(KERN_INFO "msp34xx: init: chip=%s",i2c_clientname(c));
1314         if (HAVE_NICAM(msp))
1315                 printk(" +nicam");
1316         if (HAVE_SIMPLE(msp))
1317                 printk(" +simple");
1318         if (HAVE_RADIO(msp))
1319                 printk(" +radio");
1320         printk("\n");
1321
1322         /* startup control thread */
1323         init_completion(&msp->texit);
1324         msp->tpid = kernel_thread(msp->simple ? msp3410d_thread : msp3400c_thread,
1325                                   (void *)c, 0);
1326         if (msp->tpid < 0)
1327                 printk(KERN_WARNING "msp34xx: kernel_thread() failed\n");
1328         wake_up_interruptible(&msp->wq);
1329
1330         /* done */
1331         i2c_attach_client(c);
1332         return 0;
1333 }
1334
1335 static int msp_detach(struct i2c_client *client)
1336 {
1337         struct msp3400c *msp  = i2c_get_clientdata(client);
1338         
1339         /* shutdown control thread */
1340         del_timer_sync(&msp->wake_stereo);
1341         if (msp->tpid >= 0) {
1342                 msp->rmmod = 1;
1343                 wake_up_interruptible(&msp->wq);
1344                 wait_for_completion(&msp->texit);
1345         }
1346         msp3400c_reset(client);
1347
1348         i2c_detach_client(client);
1349         kfree(msp);
1350         kfree(client);
1351         return 0;
1352 }
1353
1354 static int msp_probe(struct i2c_adapter *adap)
1355 {
1356 #ifdef I2C_CLASS_TV_ANALOG
1357         if (adap->class & I2C_CLASS_TV_ANALOG)
1358                 return i2c_probe(adap, &addr_data, msp_attach);
1359 #else
1360         switch (adap->id) {
1361         case I2C_ALGO_BIT | I2C_HW_SMBUS_VOODOO3:
1362         case I2C_ALGO_BIT | I2C_HW_B_BT848:
1363         //case I2C_ALGO_SAA7134:
1364                 return i2c_probe(adap, &addr_data, msp_attach);
1365                 break;
1366         }
1367 #endif
1368         return 0;
1369 }
1370
1371 static void msp_wake_thread(struct i2c_client *client)
1372 {
1373         struct msp3400c *msp  = i2c_get_clientdata(client);
1374
1375         msp3400c_setvolume(client,msp->muted,0,0);
1376         msp->watch_stereo=0;
1377         del_timer(&msp->wake_stereo);
1378         if (msp->active)
1379                 msp->restart = 1;
1380         wake_up_interruptible(&msp->wq);
1381 }
1382
1383 static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg)
1384 {
1385         struct msp3400c *msp  = i2c_get_clientdata(client);
1386         __u16           *sarg = arg;
1387         int scart = 0;
1388
1389         switch (cmd) {
1390
1391         case AUDC_SET_INPUT:
1392                 dprintk(KERN_DEBUG "msp34xx: AUDC_SET_INPUT(%d)\n",*sarg);
1393                 if (*sarg == msp->input)
1394                         break;
1395                 msp->input = *sarg;
1396                 switch (*sarg) {
1397                 case AUDIO_RADIO:
1398                         /* Hauppauge uses IN2 for the radio */
1399                         msp->mode   = MSP_MODE_FM_RADIO;
1400                         scart       = SCART_IN2;
1401                         break;
1402                 case AUDIO_EXTERN_1:
1403                         /* IN1 is often used for external input ... */
1404                         msp->mode   = MSP_MODE_EXTERN;
1405                         scart       = SCART_IN1;
1406                         break;
1407                 case AUDIO_EXTERN_2:
1408                         /* ... sometimes it is IN2 through ;) */
1409                         msp->mode   = MSP_MODE_EXTERN;
1410                         scart       = SCART_IN2;
1411                         break;
1412                 case AUDIO_TUNER:
1413                         msp->mode   = -1;
1414                         msp_wake_thread(client);
1415                         break;
1416                 default:
1417                         if (*sarg & AUDIO_MUTE)
1418                                 msp3400c_set_scart(client,SCART_MUTE,0);
1419                         break;
1420                 }
1421                 if (scart) {
1422                         msp->stereo = VIDEO_SOUND_STEREO;
1423                         msp3400c_set_scart(client,scart,0);
1424                         msp3400c_write(client,I2C_MSP3400C_DFP,0x000d,0x1900);
1425                         msp3400c_setstereo(client,msp->stereo);
1426                 }
1427                 if (msp->active)
1428                         msp->restart = 1;
1429                 break;
1430
1431         case AUDC_SET_RADIO:
1432                 dprintk(KERN_DEBUG "msp34xx: AUDC_SET_RADIO\n");
1433                 msp->norm = VIDEO_MODE_RADIO;
1434                 msp->watch_stereo=0;
1435                 del_timer(&msp->wake_stereo);
1436                 dprintk(KERN_DEBUG "msp34xx: switching to radio mode\n");
1437                 if (msp->simple) {
1438                         /* the thread will do for us */
1439                         msp_wake_thread(client);
1440                 } else {
1441                         /* set msp3400 to FM radio mode */
1442                         msp3400c_setmode(client,MSP_MODE_FM_RADIO);
1443                         msp3400c_setcarrier(client, MSP_CARRIER(10.7),MSP_CARRIER(10.7));
1444                         msp3400c_setvolume(client,msp->muted,msp->left,msp->right);                     
1445                 }
1446                 if (msp->active)
1447                         msp->restart = 1;
1448                 break;
1449
1450 #if 1
1451         /* work-in-progress:  hook to control the DFP registers */
1452         case MSP_SET_DFPREG:
1453         {
1454                 struct msp_dfpreg *r = arg;
1455                 unsigned int i;
1456
1457                 if (r->reg < 0 || r->reg >= DFP_COUNT)
1458                         return -EINVAL;
1459                 for (i = 0; i < ARRAY_SIZE(bl_dfp); i++)
1460                         if (r->reg == bl_dfp[i])
1461                                 return -EINVAL;
1462                 msp->dfp_regs[r->reg] = r->value;
1463                 msp3400c_write(client,I2C_MSP3400C_DFP,r->reg,r->value);
1464                 return 0;
1465         }
1466         case MSP_GET_DFPREG:
1467         {
1468                 struct msp_dfpreg *r = arg;
1469
1470                 if (r->reg < 0 || r->reg >= DFP_COUNT)
1471                         return -EINVAL;
1472                 r->value = msp3400c_read(client,I2C_MSP3400C_DFP,r->reg);
1473                 return 0;
1474         }
1475 #endif
1476
1477         /* --- v4l ioctls --- */
1478         /* take care: bttv does userspace copying, we'll get a
1479            kernel pointer here... */
1480         case VIDIOCGAUDIO:
1481         {
1482                 struct video_audio *va = arg;
1483
1484                 dprintk(KERN_DEBUG "msp34xx: VIDIOCGAUDIO\n");
1485                 va->flags |= VIDEO_AUDIO_VOLUME |
1486                         VIDEO_AUDIO_BASS |
1487                         VIDEO_AUDIO_TREBLE |
1488                         VIDEO_AUDIO_MUTABLE;
1489                 if (msp->muted)
1490                         va->flags |= VIDEO_AUDIO_MUTE;
1491                 va->volume=max(msp->left,msp->right);
1492
1493                 if (0 == va->volume) {
1494                         va->balance = 32768;
1495                 } else {
1496                         va->balance = (32768 * min(msp->left,msp->right))
1497                                 / va->volume;
1498                         va->balance = (msp->left<msp->right) ?
1499                                 (65535 - va->balance) : va->balance;
1500                 }
1501                 va->bass = msp->bass;
1502                 va->treble = msp->treble;
1503
1504                 if (msp->norm != VIDEO_MODE_RADIO) {
1505                         autodetect_stereo(client);
1506                         va->mode = msp->stereo;
1507                 }
1508                 break;
1509         }
1510         case VIDIOCSAUDIO:
1511         {
1512                 struct video_audio *va = arg;
1513
1514                 dprintk(KERN_DEBUG "msp34xx: VIDIOCSAUDIO\n");
1515                 msp->muted = (va->flags & VIDEO_AUDIO_MUTE);
1516                 msp->left = (min(65536 - va->balance,32768) *
1517                              va->volume) / 32768;
1518                 msp->right = (min(va->balance,(__u16)32768) *
1519                               va->volume) / 32768;
1520                 msp->bass = va->bass;
1521                 msp->treble = va->treble;
1522                 msp3400c_setvolume(client,msp->muted,msp->left,msp->right);
1523                 msp3400c_setbass(client,msp->bass);
1524                 msp3400c_settreble(client,msp->treble);
1525
1526                 if (va->mode != 0 && msp->norm != VIDEO_MODE_RADIO) {
1527                         msp->watch_stereo=0;
1528                         del_timer(&msp->wake_stereo);
1529                         msp->stereo = va->mode & 0x0f;
1530                         msp3400c_setstereo(client,va->mode & 0x0f);
1531                 }
1532                 break;
1533         }
1534         case VIDIOCSCHAN:
1535         {
1536                 struct video_channel *vc = arg;
1537                 
1538                 dprintk(KERN_DEBUG "msp34xx: VIDIOCSCHAN (norm=%d)\n",vc->norm);
1539                 msp->norm = vc->norm;
1540                 break;
1541         }
1542         case VIDIOCSFREQ:
1543         {
1544                 /* new channel -- kick audio carrier scan */
1545                 dprintk(KERN_DEBUG "msp34xx: VIDIOCSFREQ\n");
1546                 msp_wake_thread(client);
1547                 break;
1548         }
1549
1550         default:
1551                 /* nothing */
1552                 break;
1553         }
1554         return 0;
1555 }
1556
1557 /* ----------------------------------------------------------------------- */
1558
1559 static int msp3400_init_module(void)
1560 {
1561         i2c_add_driver(&driver);
1562         return 0;
1563 }
1564
1565 static void msp3400_cleanup_module(void)
1566 {
1567         i2c_del_driver(&driver);
1568 }
1569
1570 module_init(msp3400_init_module);
1571 module_exit(msp3400_cleanup_module);
1572
1573 /*
1574  * Overrides for Emacs so that we follow Linus's tabbing style.
1575  * ---------------------------------------------------------------------------
1576  * Local variables:
1577  * c-basic-offset: 8
1578  * End:
1579  */