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