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