upgrade to linux 2.6.10-1.12_FC2
[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 <linux/kthread.h>
51 #include <linux/suspend.h>
52 #include <asm/semaphore.h>
53 #include <asm/pgtable.h>
54
55 #include <media/audiochip.h>
56 #include <media/id.h>
57 #include "msp3400.h"
58
59 #define OPMODE_AUTO    -1
60 #define OPMODE_MANUAL   0
61 #define OPMODE_SIMPLE   1   /* use short programming (>= msp3410 only) */
62 #define OPMODE_SIMPLER  2   /* use shorter programming (>= msp34xxG)   */
63
64 /* insmod parameters */
65 static int opmode   = OPMODE_AUTO;
66 static int debug    = 0;    /* debug output */
67 static int once     = 0;    /* no continous stereo monitoring */
68 static int amsound  = 0;    /* hard-wire AM sound at 6.5 Hz (france),
69                                the autoscan seems work well only with FM... */
70 static int standard = 1;    /* Override auto detect of audio standard, if needed. */
71 static int dolby    = 0;
72
73 static int stereo_threshold = 0x190; /* a2 threshold for stereo/bilingual
74                                         (msp34xxg only) 0x00a0-0x03c0 */
75
76 struct msp3400c {
77         int rev1,rev2;
78
79         int opmode;
80         int mode;
81         int norm;
82         int nicam_on;
83         int acb;
84         int main, second;       /* sound carrier */
85         int input;
86         int source;             /* see msp34xxg_set_source */
87
88         /* v4l2 */
89         int audmode;
90         int rxsubchans;
91
92         int muted;
93         int volume, balance;
94         int bass, treble;
95
96         /* thread */
97         struct task_struct   *kthread;
98         wait_queue_head_t    wq;
99         int                  restart:1;
100         int                  watch_stereo:1;
101 };
102
103 #define HAVE_NICAM(msp)   (((msp->rev2>>8) & 0xff) != 00)
104 #define HAVE_SIMPLE(msp)  ((msp->rev1      & 0xff) >= 'D'-'@')
105 #define HAVE_SIMPLER(msp) ((msp->rev1      & 0xff) >= 'G'-'@')
106 #define HAVE_RADIO(msp)   ((msp->rev1      & 0xff) >= 'G'-'@')
107
108 #define VIDEO_MODE_RADIO 16      /* norm magic for radio mode */
109
110 /* ---------------------------------------------------------------------- */
111
112 #define dprintk      if (debug >= 1) printk
113 #define d2printk     if (debug >= 2) printk
114
115 /* read-only */
116 module_param(opmode,           int, 0444);
117
118 /* read-write */
119 module_param(once,             int, 0644);
120 module_param(debug,            int, 0644);
121 module_param(stereo_threshold, int, 0644);
122 module_param(standard,         int, 0644);
123 module_param(amsound,          int, 0644);
124 module_param(dolby,            int, 0644);
125
126 MODULE_PARM_DESC(once, "No continuous stereo monitoring");
127 MODULE_PARM_DESC(debug, "Enable debug messages");
128 MODULE_PARM_DESC(standard, "Specify audio standard: 32 = NTSC, 64 = radio, Default: Autodetect");
129 MODULE_PARM_DESC(amsound, "Hardwire AM sound at 6.5Hz (France), FM can autoscan");
130
131 MODULE_DESCRIPTION("device driver for msp34xx TV sound processor");
132 MODULE_AUTHOR("Gerd Knorr");
133 MODULE_LICENSE("Dual BSD/GPL"); /* FreeBSD uses this too */
134
135 /* ---------------------------------------------------------------------- */
136
137 #define I2C_MSP3400C       0x80
138 #define I2C_MSP3400C_ALT   0x88
139
140 #define I2C_MSP3400C_DEM   0x10
141 #define I2C_MSP3400C_DFP   0x12
142
143 /* Addresses to scan */
144 static unsigned short normal_i2c[] = {
145         I2C_MSP3400C      >> 1,
146         I2C_MSP3400C_ALT  >> 1,
147         I2C_CLIENT_END
148 };
149 static unsigned short normal_i2c_range[] = {I2C_CLIENT_END,I2C_CLIENT_END};
150 I2C_CLIENT_INSMOD;
151
152 /* ----------------------------------------------------------------------- */
153 /* functions for talking to the MSP3400C Sound processor                   */
154
155 static int msp3400c_reset(struct i2c_client *client)
156 {
157         /* reset and read revision code */
158         static char reset_off[3] = { 0x00, 0x80, 0x00 };
159         static char reset_on[3]  = { 0x00, 0x00, 0x00 };
160         static char write[3]     = { I2C_MSP3400C_DFP + 1, 0x00, 0x1e };
161         char read[2];
162         struct i2c_msg reset[2] = {
163                 { client->addr, I2C_M_IGNORE_NAK, 3, reset_off },
164                 { client->addr, I2C_M_IGNORE_NAK, 3, reset_on  },
165         };
166         struct i2c_msg test[2] = {
167                 { client->addr, 0,        3, write },
168                 { client->addr, I2C_M_RD, 2, read  },
169         };
170
171         if ( (1 != i2c_transfer(client->adapter,&reset[0],1)) ||
172              (1 != i2c_transfer(client->adapter,&reset[1],1)) ||
173              (2 != i2c_transfer(client->adapter,test,2)) ) {
174                 printk(KERN_ERR "msp3400: chip reset failed\n");
175                 return -1;
176         }
177         return 0;
178 }
179
180 static int
181 msp3400c_read(struct i2c_client *client, int dev, int addr)
182 {
183         int err;
184
185         unsigned char write[3];
186         unsigned char read[2];
187         struct i2c_msg msgs[2] = {
188                 { client->addr, 0,        3, write },
189                 { client->addr, I2C_M_RD, 2, read  }
190         };
191         write[0] = dev+1;
192         write[1] = addr >> 8;
193         write[2] = addr & 0xff;
194
195         for (err = 0; err < 3;) {
196                 if (2 == i2c_transfer(client->adapter,msgs,2))
197                         break;
198                 err++;
199                 printk(KERN_WARNING "msp34xx: I/O error #%d (read 0x%02x/0x%02x)\n",
200                        err, dev, addr);
201                 msleep(10);
202         }
203         if (3 == err) {
204                 printk(KERN_WARNING "msp34xx: giving up, reseting chip. Sound will go off, sorry folks :-|\n");
205                 msp3400c_reset(client);
206                 return -1;
207         }
208         return read[0] << 8 | read[1];
209 }
210
211 static int
212 msp3400c_write(struct i2c_client *client, int dev, int addr, int val)
213 {
214         int err;
215         unsigned char buffer[5];
216
217         buffer[0] = dev;
218         buffer[1] = addr >> 8;
219         buffer[2] = addr &  0xff;
220         buffer[3] = val  >> 8;
221         buffer[4] = val  &  0xff;
222
223         for (err = 0; err < 3;) {
224                 if (5 == i2c_master_send(client, buffer, 5))
225                         break;
226                 err++;
227                 printk(KERN_WARNING "msp34xx: I/O error #%d (write 0x%02x/0x%02x)\n",
228                        err, dev, addr);
229                 msleep(10);
230         }
231         if (3 == err) {
232                 printk(KERN_WARNING "msp34xx: giving up, reseting chip. Sound will go off, sorry folks :-|\n");
233                 msp3400c_reset(client);
234                 return -1;
235         }
236         return 0;
237 }
238
239 /* ------------------------------------------------------------------------ */
240
241 /* This macro is allowed for *constants* only, gcc must calculate it
242    at compile time.  Remember -- no floats in kernel mode */
243 #define MSP_CARRIER(freq) ((int)((float)(freq/18.432)*(1<<24)))
244
245 #define MSP_MODE_AM_DETECT   0
246 #define MSP_MODE_FM_RADIO    2
247 #define MSP_MODE_FM_TERRA    3
248 #define MSP_MODE_FM_SAT      4
249 #define MSP_MODE_FM_NICAM1   5
250 #define MSP_MODE_FM_NICAM2   6
251 #define MSP_MODE_AM_NICAM    7
252 #define MSP_MODE_BTSC        8
253 #define MSP_MODE_EXTERN      9
254
255 static struct MSP_INIT_DATA_DEM {
256         int fir1[6];
257         int fir2[6];
258         int cdo1;
259         int cdo2;
260         int ad_cv;
261         int mode_reg;
262         int dfp_src;
263         int dfp_matrix;
264 } msp_init_data[] = {
265         /* AM (for carrier detect / msp3400) */
266         { { 75, 19, 36, 35, 39, 40 }, { 75, 19, 36, 35, 39, 40 },
267           MSP_CARRIER(5.5), MSP_CARRIER(5.5),
268           0x00d0, 0x0500,   0x0020, 0x3000},
269
270         /* AM (for carrier detect / msp3410) */
271         { { -1, -1, -8, 2, 59, 126 }, { -1, -1, -8, 2, 59, 126 },
272           MSP_CARRIER(5.5), MSP_CARRIER(5.5),
273           0x00d0, 0x0100,   0x0020, 0x3000},
274
275         /* FM Radio */
276         { { -8, -8, 4, 6, 78, 107 }, { -8, -8, 4, 6, 78, 107 },
277           MSP_CARRIER(10.7), MSP_CARRIER(10.7),
278           0x00d0, 0x0480, 0x0020, 0x3000 },
279
280         /* Terrestial FM-mono + FM-stereo */
281         { {  3, 18, 27, 48, 66, 72 }, {  3, 18, 27, 48, 66, 72 },
282           MSP_CARRIER(5.5), MSP_CARRIER(5.5),
283           0x00d0, 0x0480,   0x0030, 0x3000},
284
285         /* Sat FM-mono */
286         { {  1,  9, 14, 24, 33, 37 }, {  3, 18, 27, 48, 66, 72 },
287           MSP_CARRIER(6.5), MSP_CARRIER(6.5),
288           0x00c6, 0x0480,   0x0000, 0x3000},
289
290         /* NICAM/FM --  B/G (5.5/5.85), D/K (6.5/5.85) */
291         { { -2, -8, -10, 10, 50, 86 }, {  3, 18, 27, 48, 66, 72 },
292           MSP_CARRIER(5.5), MSP_CARRIER(5.5),
293           0x00d0, 0x0040,   0x0120, 0x3000},
294
295         /* NICAM/FM -- I (6.0/6.552) */
296         { {  2, 4, -6, -4, 40, 94 }, {  3, 18, 27, 48, 66, 72 },
297           MSP_CARRIER(6.0), MSP_CARRIER(6.0),
298           0x00d0, 0x0040,   0x0120, 0x3000},
299
300         /* NICAM/AM -- L (6.5/5.85) */
301         { {  -2, -8, -10, 10, 50, 86 }, {  -4, -12, -9, 23, 79, 126 },
302           MSP_CARRIER(6.5), MSP_CARRIER(6.5),
303           0x00c6, 0x0140,   0x0120, 0x7c03},
304 };
305
306 struct CARRIER_DETECT {
307         int   cdo;
308         char *name;
309 };
310
311 static struct CARRIER_DETECT carrier_detect_main[] = {
312         /* main carrier */
313         { MSP_CARRIER(4.5),        "4.5   NTSC"                   },
314         { MSP_CARRIER(5.5),        "5.5   PAL B/G"                },
315         { MSP_CARRIER(6.0),        "6.0   PAL I"                  },
316         { MSP_CARRIER(6.5),        "6.5   PAL D/K + SAT + SECAM"  }
317 };
318
319 static struct CARRIER_DETECT carrier_detect_55[] = {
320         /* PAL B/G */
321         { MSP_CARRIER(5.7421875),  "5.742 PAL B/G FM-stereo"     },
322         { MSP_CARRIER(5.85),       "5.85  PAL B/G NICAM"         }
323 };
324
325 static struct CARRIER_DETECT carrier_detect_65[] = {
326         /* PAL SAT / SECAM */
327         { MSP_CARRIER(5.85),       "5.85  PAL D/K + SECAM NICAM" },
328         { MSP_CARRIER(6.2578125),  "6.25  PAL D/K1 FM-stereo" },
329         { MSP_CARRIER(6.7421875),  "6.74  PAL D/K2 FM-stereo" },
330         { MSP_CARRIER(7.02),       "7.02  PAL SAT FM-stereo s/b" },
331         { MSP_CARRIER(7.20),       "7.20  PAL SAT FM-stereo s"   },
332         { MSP_CARRIER(7.38),       "7.38  PAL SAT FM-stereo b"   },
333 };
334
335 #define CARRIER_COUNT(x) (sizeof(x)/sizeof(struct CARRIER_DETECT))
336
337 /* ----------------------------------------------------------------------- */
338
339 static int scarts[3][9] = {
340   /* MASK    IN1     IN2     IN1_DA  IN2_DA  IN3     IN4     MONO    MUTE   */
341   {  0x0320, 0x0000, 0x0200, -1,     -1,     0x0300, 0x0020, 0x0100, 0x0320 },
342   {  0x0c40, 0x0440, 0x0400, 0x0c00, 0x0040, 0x0000, 0x0840, 0x0800, 0x0c40 },
343   {  0x3080, 0x1000, 0x1080, 0x0000, 0x0080, 0x2080, 0x3080, 0x2000, 0x3000 },
344 };
345
346 static char *scart_names[] = {
347   "mask", "in1", "in2", "in1 da", "in2 da", "in3", "in4", "mono", "mute"
348 };
349
350 static void
351 msp3400c_set_scart(struct i2c_client *client, int in, int out)
352 {
353         struct msp3400c *msp = i2c_get_clientdata(client);
354
355         if (-1 == scarts[out][in])
356                 return;
357
358         dprintk(KERN_DEBUG
359                 "msp34xx: scart switch: %s => %d\n",scart_names[in],out);
360         msp->acb &= ~scarts[out][SCART_MASK];
361         msp->acb |=  scarts[out][in];
362         msp3400c_write(client,I2C_MSP3400C_DFP, 0x0013, msp->acb);
363 }
364
365 /* ------------------------------------------------------------------------ */
366
367 static void msp3400c_setcarrier(struct i2c_client *client, int cdo1, int cdo2)
368 {
369         msp3400c_write(client,I2C_MSP3400C_DEM, 0x0093, cdo1 & 0xfff);
370         msp3400c_write(client,I2C_MSP3400C_DEM, 0x009b, cdo1 >> 12);
371         msp3400c_write(client,I2C_MSP3400C_DEM, 0x00a3, cdo2 & 0xfff);
372         msp3400c_write(client,I2C_MSP3400C_DEM, 0x00ab, cdo2 >> 12);
373         msp3400c_write(client,I2C_MSP3400C_DEM, 0x0056, 0); /*LOAD_REG_1/2*/
374 }
375
376 static void msp3400c_setvolume(struct i2c_client *client,
377                                int muted, int volume, int balance)
378 {
379         int val = 0, bal = 0;
380
381         if (!muted) {
382                 val = (volume * 0x73 / 65535) << 8;
383         }
384         if (val) {
385                 bal = (balance / 256) - 128;
386         }
387         dprintk(KERN_DEBUG
388                 "msp34xx: setvolume: mute=%s %d:%d  v=0x%02x b=0x%02x\n",
389                 muted ? "on" : "off", volume, balance, val>>8, bal);
390         msp3400c_write(client,I2C_MSP3400C_DFP, 0x0000, val); /* loudspeaker */
391         msp3400c_write(client,I2C_MSP3400C_DFP, 0x0006, val); /* headphones  */
392         msp3400c_write(client,I2C_MSP3400C_DFP, 0x0007,
393                        muted ? 0x01 : (val | 0x01));
394         msp3400c_write(client,I2C_MSP3400C_DFP, 0x0001, bal << 8);
395 }
396
397 static void msp3400c_setbass(struct i2c_client *client, int bass)
398 {
399         int val = ((bass-32768) * 0x60 / 65535) << 8;
400
401         dprintk(KERN_DEBUG "msp34xx: setbass: %d 0x%02x\n",bass, val>>8);
402         msp3400c_write(client,I2C_MSP3400C_DFP, 0x0002, val); /* loudspeaker */
403 }
404
405 static void msp3400c_settreble(struct i2c_client *client, int treble)
406 {
407         int val = ((treble-32768) * 0x60 / 65535) << 8;
408
409         dprintk(KERN_DEBUG "msp34xx: settreble: %d 0x%02x\n",treble, val>>8);
410         msp3400c_write(client,I2C_MSP3400C_DFP, 0x0003, val); /* loudspeaker */
411 }
412
413 static void msp3400c_setmode(struct i2c_client *client, int type)
414 {
415         struct msp3400c *msp = i2c_get_clientdata(client);
416         int i;
417
418         dprintk(KERN_DEBUG "msp3400: setmode: %d\n",type);
419         msp->mode       = type;
420         msp->audmode    = V4L2_TUNER_MODE_MONO;
421         msp->rxsubchans = V4L2_TUNER_SUB_MONO;
422
423         msp3400c_write(client,I2C_MSP3400C_DEM, 0x00bb,          /* ad_cv */
424                        msp_init_data[type].ad_cv);
425
426         for (i = 5; i >= 0; i--)                                   /* fir 1 */
427                 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0001,
428                                msp_init_data[type].fir1[i]);
429
430         msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005, 0x0004); /* fir 2 */
431         msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005, 0x0040);
432         msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005, 0x0000);
433         for (i = 5; i >= 0; i--)
434                 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005,
435                                msp_init_data[type].fir2[i]);
436
437         msp3400c_write(client,I2C_MSP3400C_DEM, 0x0083,     /* MODE_REG */
438                        msp_init_data[type].mode_reg);
439
440         msp3400c_setcarrier(client, msp_init_data[type].cdo1,
441                             msp_init_data[type].cdo2);
442
443         msp3400c_write(client,I2C_MSP3400C_DEM, 0x0056, 0); /*LOAD_REG_1/2*/
444
445         if (dolby) {
446                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008,
447                                0x0520); /* I2S1 */
448                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009,
449                                0x0620); /* I2S2 */
450                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b,
451                                msp_init_data[type].dfp_src);
452         } else {
453                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008,
454                                msp_init_data[type].dfp_src);
455                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009,
456                                msp_init_data[type].dfp_src);
457                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b,
458                                msp_init_data[type].dfp_src);
459         }
460         msp3400c_write(client,I2C_MSP3400C_DFP, 0x000a,
461                        msp_init_data[type].dfp_src);
462         msp3400c_write(client,I2C_MSP3400C_DFP, 0x000e,
463                        msp_init_data[type].dfp_matrix);
464
465         if (HAVE_NICAM(msp)) {
466                 /* nicam prescale */
467                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0010, 0x5a00); /* was: 0x3000 */
468         }
469 }
470
471 static int best_audio_mode(int rxsubchans)
472 {
473         if (rxsubchans & V4L2_TUNER_SUB_STEREO)
474                 return V4L2_TUNER_MODE_STEREO;
475         if (rxsubchans & V4L2_TUNER_SUB_LANG1)
476                 return V4L2_TUNER_MODE_LANG1;
477         if (rxsubchans & V4L2_TUNER_SUB_LANG2)
478                 return V4L2_TUNER_MODE_LANG2;
479         return V4L2_TUNER_MODE_MONO;
480 }
481
482 /* turn on/off nicam + stereo */
483 static void msp3400c_set_audmode(struct i2c_client *client, int audmode)
484 {
485         static char *strmode[16] = {
486 #if __GNUC__ >= 3
487                 [ 0 ... 15 ]               = "invalid",
488 #endif
489                 [ V4L2_TUNER_MODE_MONO   ] = "mono",
490                 [ V4L2_TUNER_MODE_STEREO ] = "stereo",
491                 [ V4L2_TUNER_MODE_LANG1  ] = "lang1",
492                 [ V4L2_TUNER_MODE_LANG2  ] = "lang2",
493         };
494         struct msp3400c *msp = i2c_get_clientdata(client);
495         int nicam=0; /* channel source: FM/AM or nicam */
496         int src=0;
497
498         BUG_ON(msp->opmode == OPMODE_SIMPLER);
499         msp->audmode = audmode;
500
501         /* switch demodulator */
502         switch (msp->mode) {
503         case MSP_MODE_FM_TERRA:
504                 dprintk(KERN_DEBUG "msp3400: FM setstereo: %s\n",
505                         strmode[audmode]);
506                 msp3400c_setcarrier(client,msp->second,msp->main);
507                 switch (audmode) {
508                 case V4L2_TUNER_MODE_STEREO:
509                         msp3400c_write(client,I2C_MSP3400C_DFP, 0x000e, 0x3001);
510                         break;
511                 case V4L2_TUNER_MODE_MONO:
512                 case V4L2_TUNER_MODE_LANG1:
513                 case V4L2_TUNER_MODE_LANG2:
514                         msp3400c_write(client,I2C_MSP3400C_DFP, 0x000e, 0x3000);
515                         break;
516                 }
517                 break;
518         case MSP_MODE_FM_SAT:
519                 dprintk(KERN_DEBUG "msp3400: SAT setstereo: %s\n",
520                         strmode[audmode]);
521                 switch (audmode) {
522                 case V4L2_TUNER_MODE_MONO:
523                         msp3400c_setcarrier(client, MSP_CARRIER(6.5), MSP_CARRIER(6.5));
524                         break;
525                 case V4L2_TUNER_MODE_STEREO:
526                         msp3400c_setcarrier(client, MSP_CARRIER(7.2), MSP_CARRIER(7.02));
527                         break;
528                 case V4L2_TUNER_MODE_LANG1:
529                         msp3400c_setcarrier(client, MSP_CARRIER(7.38), MSP_CARRIER(7.02));
530                         break;
531                 case V4L2_TUNER_MODE_LANG2:
532                         msp3400c_setcarrier(client, MSP_CARRIER(7.38), MSP_CARRIER(7.02));
533                         break;
534                 }
535                 break;
536         case MSP_MODE_FM_NICAM1:
537         case MSP_MODE_FM_NICAM2:
538         case MSP_MODE_AM_NICAM:
539                 dprintk(KERN_DEBUG "msp3400: NICAM setstereo: %s\n",
540                         strmode[audmode]);
541                 msp3400c_setcarrier(client,msp->second,msp->main);
542                 if (msp->nicam_on)
543                         nicam=0x0100;
544                 break;
545         case MSP_MODE_BTSC:
546                 dprintk(KERN_DEBUG "msp3400: BTSC setstereo: %s\n",
547                         strmode[audmode]);
548                 nicam=0x0300;
549                 break;
550         case MSP_MODE_EXTERN:
551                 dprintk(KERN_DEBUG "msp3400: extern setstereo: %s\n",
552                         strmode[audmode]);
553                 nicam = 0x0200;
554                 break;
555         case MSP_MODE_FM_RADIO:
556                 dprintk(KERN_DEBUG "msp3400: FM-Radio setstereo: %s\n",
557                         strmode[audmode]);
558                 break;
559         default:
560                 dprintk(KERN_DEBUG "msp3400: mono setstereo\n");
561                 return;
562         }
563
564         /* switch audio */
565         switch (audmode) {
566         case V4L2_TUNER_MODE_STEREO:
567                 src = 0x0020 | nicam;
568 #if 0
569                 /* spatial effect */
570                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0005,0x4000);
571 #endif
572                 break;
573         case V4L2_TUNER_MODE_MONO:
574                 if (msp->mode == MSP_MODE_AM_NICAM) {
575                         dprintk("msp3400: switching to AM mono\n");
576                         /* AM mono decoding is handled by tuner, not MSP chip */
577                         /* SCART switching control register */
578                         msp3400c_set_scart(client,SCART_MONO,0);
579                         src = 0x0200;
580                         break;
581                 }
582         case V4L2_TUNER_MODE_LANG1:
583                 src = 0x0000 | nicam;
584                 break;
585         case V4L2_TUNER_MODE_LANG2:
586                 src = 0x0010 | nicam;
587                 break;
588         }
589         dprintk(KERN_DEBUG
590                 "msp3400: setstereo final source/matrix = 0x%x\n", src);
591
592         if (dolby) {
593                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008,0x0520);
594                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009,0x0620);
595                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000a,src);
596                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b,src);
597         } else {
598                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008,src);
599                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009,src);
600                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000a,src);
601                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b,src);
602         }
603 }
604
605 static void
606 msp3400c_print_mode(struct msp3400c *msp)
607 {
608         if (msp->main == msp->second) {
609                 printk(KERN_DEBUG "msp3400: mono sound carrier: %d.%03d MHz\n",
610                        msp->main/910000,(msp->main/910)%1000);
611         } else {
612                 printk(KERN_DEBUG "msp3400: main sound carrier: %d.%03d MHz\n",
613                        msp->main/910000,(msp->main/910)%1000);
614         }
615         if (msp->mode == MSP_MODE_FM_NICAM1 ||
616             msp->mode == MSP_MODE_FM_NICAM2)
617                 printk(KERN_DEBUG "msp3400: NICAM/FM carrier   : %d.%03d MHz\n",
618                        msp->second/910000,(msp->second/910)%1000);
619         if (msp->mode == MSP_MODE_AM_NICAM)
620                 printk(KERN_DEBUG "msp3400: NICAM/AM carrier   : %d.%03d MHz\n",
621                        msp->second/910000,(msp->second/910)%1000);
622         if (msp->mode == MSP_MODE_FM_TERRA &&
623             msp->main != msp->second) {
624                 printk(KERN_DEBUG "msp3400: FM-stereo carrier : %d.%03d MHz\n",
625                        msp->second/910000,(msp->second/910)%1000);
626         }
627 }
628
629 /* ----------------------------------------------------------------------- */
630
631 struct REGISTER_DUMP {
632         int   addr;
633         char *name;
634 };
635
636 struct REGISTER_DUMP d1[] = {
637         { 0x007e, "autodetect" },
638         { 0x0023, "C_AD_BITS " },
639         { 0x0038, "ADD_BITS  " },
640         { 0x003e, "CIB_BITS  " },
641         { 0x0057, "ERROR_RATE" },
642 };
643
644 static int
645 autodetect_stereo(struct i2c_client *client)
646 {
647         struct msp3400c *msp = i2c_get_clientdata(client);
648         int val;
649         int rxsubchans = msp->rxsubchans;
650         int newnicam   = msp->nicam_on;
651         int update = 0;
652
653         switch (msp->mode) {
654         case MSP_MODE_FM_TERRA:
655                 val = msp3400c_read(client, I2C_MSP3400C_DFP, 0x18);
656                 if (val > 32767)
657                         val -= 65536;
658                 dprintk(KERN_DEBUG
659                         "msp34xx: stereo detect register: %d\n",val);
660                 if (val > 4096) {
661                         rxsubchans = V4L2_TUNER_SUB_STEREO | V4L2_TUNER_SUB_MONO;
662                 } else if (val < -4096) {
663                         rxsubchans = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
664                 } else {
665                         rxsubchans = V4L2_TUNER_SUB_MONO;
666                 }
667                 newnicam = 0;
668                 break;
669         case MSP_MODE_FM_NICAM1:
670         case MSP_MODE_FM_NICAM2:
671         case MSP_MODE_AM_NICAM:
672                 val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x23);
673                 dprintk(KERN_DEBUG
674                         "msp34xx: nicam sync=%d, mode=%d\n",
675                         val & 1, (val & 0x1e) >> 1);
676
677                 if (val & 1) {
678                         /* nicam synced */
679                         switch ((val & 0x1e) >> 1)  {
680                         case 0:
681                         case 8:
682                                 rxsubchans = V4L2_TUNER_SUB_STEREO;
683                                 break;
684                         case 1:
685                         case 9:
686                                 rxsubchans = V4L2_TUNER_SUB_MONO
687                                         | V4L2_TUNER_SUB_LANG1;
688                                 break;
689                         case 2:
690                         case 10:
691                                 rxsubchans = V4L2_TUNER_SUB_MONO
692                                         | V4L2_TUNER_SUB_LANG1
693                                         | V4L2_TUNER_SUB_LANG2;
694                                 break;
695                         default:
696                                 rxsubchans = V4L2_TUNER_SUB_MONO;
697                                 break;
698                         }
699                         newnicam=1;
700                 } else {
701                         newnicam = 0;
702                         rxsubchans = V4L2_TUNER_SUB_MONO;
703                 }
704                 break;
705         case MSP_MODE_BTSC:
706                 val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x200);
707                 dprintk(KERN_DEBUG
708                         "msp3410: status=0x%x (pri=%s, sec=%s, %s%s%s)\n",
709                         val,
710                         (val & 0x0002) ? "no"     : "yes",
711                         (val & 0x0004) ? "no"     : "yes",
712                         (val & 0x0040) ? "stereo" : "mono",
713                         (val & 0x0080) ? ", nicam 2nd mono" : "",
714                         (val & 0x0100) ? ", bilingual/SAP"  : "");
715                 rxsubchans = V4L2_TUNER_SUB_MONO;
716                 if (val & 0x0040) rxsubchans |= V4L2_TUNER_SUB_STEREO;
717                 if (val & 0x0100) rxsubchans |= V4L2_TUNER_SUB_LANG1;
718                 break;
719         }
720         if (rxsubchans != msp->rxsubchans) {
721                 update = 1;
722                 dprintk(KERN_DEBUG "msp34xx: watch: rxsubchans %d => %d\n",
723                         msp->rxsubchans,rxsubchans);
724                 msp->rxsubchans = rxsubchans;
725         }
726         if (newnicam != msp->nicam_on) {
727                 update = 1;
728                 dprintk(KERN_DEBUG "msp34xx: watch: nicam %d => %d\n",
729                         msp->nicam_on,newnicam);
730                 msp->nicam_on = newnicam;
731         }
732         return update;
733 }
734
735 /*
736  * A kernel thread for msp3400 control -- we don't want to block the
737  * in the ioctl while doing the sound carrier & stereo detect
738  */
739
740 static int msp34xx_sleep(struct msp3400c *msp, int timeout)
741 {
742         DECLARE_WAITQUEUE(wait, current);
743
744         add_wait_queue(&msp->wq, &wait);
745         if (!kthread_should_stop()) {
746                 if (timeout < 0) {
747                         set_current_state(TASK_INTERRUPTIBLE);
748                         schedule();
749                 } else {
750 #if 0
751                         /* hmm, that one doesn't return on wakeup ... */
752                         msleep_interruptible(timeout);
753 #else
754                         set_current_state(TASK_INTERRUPTIBLE);
755                         schedule_timeout(msecs_to_jiffies(timeout));
756 #endif
757                 }
758         }
759         if (current->flags & PF_FREEZE)
760                 refrigerator(PF_FREEZE);
761         remove_wait_queue(&msp->wq, &wait);
762         return msp->restart;
763 }
764
765 /* stereo/multilang monitoring */
766 static void watch_stereo(struct i2c_client *client)
767 {
768         struct msp3400c *msp = i2c_get_clientdata(client);
769
770         if (autodetect_stereo(client))
771                 msp3400c_set_audmode(client,best_audio_mode(msp->rxsubchans));
772         if (once)
773                 msp->watch_stereo = 0;
774 }
775
776 static int msp3400c_thread(void *data)
777 {
778         struct i2c_client *client = data;
779         struct msp3400c *msp = i2c_get_clientdata(client);
780         struct CARRIER_DETECT *cd;
781         int count, max1,max2,val1,val2, val,this;
782
783         printk("msp3400: kthread started\n");
784         for (;;) {
785                 d2printk("msp3400: thread: sleep\n");
786                 msp34xx_sleep(msp,-1);
787                 d2printk("msp3400: thread: wakeup\n");
788
789         restart:
790                 dprintk("msp3410: thread: restart scan\n");
791                 msp->restart = 0;
792                 if (kthread_should_stop())
793                         break;
794
795                 if (VIDEO_MODE_RADIO == msp->norm ||
796                     MSP_MODE_EXTERN  == msp->mode) {
797                         /* no carrier scan, just unmute */
798                         printk("msp3400: thread: no carrier scan\n");
799                         msp3400c_setvolume(client, msp->muted,
800                                            msp->volume, msp->balance);
801                         continue;
802                 }
803
804                 /* mute */
805                 msp3400c_setvolume(client, msp->muted, 0, 0);
806                 msp3400c_setmode(client, MSP_MODE_AM_DETECT /* +1 */ );
807                 val1 = val2 = 0;
808                 max1 = max2 = -1;
809                 msp->watch_stereo = 0;
810
811                 /* some time for the tuner to sync */
812                 if (msp34xx_sleep(msp,200))
813                         goto restart;
814
815                 /* carrier detect pass #1 -- main carrier */
816                 cd = carrier_detect_main; count = CARRIER_COUNT(carrier_detect_main);
817
818                 if (amsound && (msp->norm == VIDEO_MODE_SECAM)) {
819                         /* autodetect doesn't work well with AM ... */
820                         max1 = 3;
821                         count = 0;
822                         dprintk("msp3400: AM sound override\n");
823                 }
824
825                 for (this = 0; this < count; this++) {
826                         msp3400c_setcarrier(client, cd[this].cdo,cd[this].cdo);
827                         if (msp34xx_sleep(msp,100))
828                                 goto restart;
829                         val = msp3400c_read(client, I2C_MSP3400C_DFP, 0x1b);
830                         if (val > 32767)
831                                 val -= 65536;
832                         if (val1 < val)
833                                 val1 = val, max1 = this;
834                         dprintk("msp3400: carrier1 val: %5d / %s\n", val,cd[this].name);
835                 }
836
837                 /* carrier detect pass #2 -- second (stereo) carrier */
838                 switch (max1) {
839                 case 1: /* 5.5 */
840                         cd = carrier_detect_55;
841                         count = CARRIER_COUNT(carrier_detect_55);
842                         break;
843                 case 3: /* 6.5 */
844                         cd = carrier_detect_65;
845                         count = CARRIER_COUNT(carrier_detect_65);
846                         break;
847                 case 0: /* 4.5 */
848                 case 2: /* 6.0 */
849                 default:
850                         cd = NULL; count = 0;
851                         break;
852                 }
853
854                 if (amsound && (msp->norm == VIDEO_MODE_SECAM)) {
855                         /* autodetect doesn't work well with AM ... */
856                         cd = NULL; count = 0; max2 = 0;
857                 }
858                 for (this = 0; this < count; this++) {
859                         msp3400c_setcarrier(client, cd[this].cdo,cd[this].cdo);
860                         if (msp34xx_sleep(msp,100))
861                                 goto restart;
862                         val = msp3400c_read(client, I2C_MSP3400C_DFP, 0x1b);
863                         if (val > 32767)
864                                 val -= 65536;
865                         if (val2 < val)
866                                 val2 = val, max2 = this;
867                         dprintk("msp3400: carrier2 val: %5d / %s\n", val,cd[this].name);
868                 }
869
870                 /* programm the msp3400 according to the results */
871                 msp->main   = carrier_detect_main[max1].cdo;
872                 switch (max1) {
873                 case 1: /* 5.5 */
874                         if (max2 == 0) {
875                                 /* B/G FM-stereo */
876                                 msp->second = carrier_detect_55[max2].cdo;
877                                 msp3400c_setmode(client, MSP_MODE_FM_TERRA);
878                                 msp->nicam_on = 0;
879                                 msp3400c_set_audmode(client, V4L2_TUNER_MODE_MONO);
880                                 msp->watch_stereo = 1;
881                         } else if (max2 == 1 && HAVE_NICAM(msp)) {
882                                 /* B/G NICAM */
883                                 msp->second = carrier_detect_55[max2].cdo;
884                                 msp3400c_setmode(client, MSP_MODE_FM_NICAM1);
885                                 msp->nicam_on = 1;
886                                 msp3400c_setcarrier(client, msp->second, msp->main);
887                                 msp->watch_stereo = 1;
888                         } else {
889                                 goto no_second;
890                         }
891                         break;
892                 case 2: /* 6.0 */
893                         /* PAL I NICAM */
894                         msp->second = MSP_CARRIER(6.552);
895                         msp3400c_setmode(client, MSP_MODE_FM_NICAM2);
896                         msp->nicam_on = 1;
897                         msp3400c_setcarrier(client, msp->second, msp->main);
898                         msp->watch_stereo = 1;
899                         break;
900                 case 3: /* 6.5 */
901                         if (max2 == 1 || max2 == 2) {
902                                 /* D/K FM-stereo */
903                                 msp->second = carrier_detect_65[max2].cdo;
904                                 msp3400c_setmode(client, MSP_MODE_FM_TERRA);
905                                 msp->nicam_on = 0;
906                                 msp3400c_set_audmode(client, V4L2_TUNER_MODE_MONO);
907                                 msp->watch_stereo = 1;
908                         } else if (max2 == 0 &&
909                                    msp->norm == VIDEO_MODE_SECAM) {
910                                 /* L NICAM or AM-mono */
911                                 msp->second = carrier_detect_65[max2].cdo;
912                                 msp3400c_setmode(client, MSP_MODE_AM_NICAM);
913                                 msp->nicam_on = 0;
914                                 msp3400c_set_audmode(client, V4L2_TUNER_MODE_MONO);
915                                 msp3400c_setcarrier(client, msp->second, msp->main);
916                                 /* volume prescale for SCART (AM mono input) */
917                                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000d, 0x1900);
918                                 msp->watch_stereo = 1;
919                         } else if (max2 == 0 && HAVE_NICAM(msp)) {
920                                 /* D/K NICAM */
921                                 msp->second = carrier_detect_65[max2].cdo;
922                                 msp3400c_setmode(client, MSP_MODE_FM_NICAM1);
923                                 msp->nicam_on = 1;
924                                 msp3400c_setcarrier(client, msp->second, msp->main);
925                                 msp->watch_stereo = 1;
926                         } else {
927                                 goto no_second;
928                         }
929                         break;
930                 case 0: /* 4.5 */
931                 default:
932                 no_second:
933                         msp->second = carrier_detect_main[max1].cdo;
934                         msp3400c_setmode(client, MSP_MODE_FM_TERRA);
935                         msp->nicam_on = 0;
936                         msp3400c_setcarrier(client, msp->second, msp->main);
937                         msp->rxsubchans = V4L2_TUNER_SUB_MONO;
938                         msp3400c_set_audmode(client, V4L2_TUNER_MODE_MONO);
939                         break;
940                 }
941
942                 /* unmute */
943                 msp3400c_setvolume(client, msp->muted,
944                                    msp->volume, msp->balance);
945                 if (debug)
946                         msp3400c_print_mode(msp);
947
948                 /* monitor tv audio mode */
949                 while (msp->watch_stereo) {
950                         if (msp34xx_sleep(msp,5000))
951                                 goto restart;
952                         watch_stereo(client);
953                 }
954         }
955         dprintk(KERN_DEBUG "msp3400: thread: exit\n");
956         return 0;
957 }
958
959 /* ----------------------------------------------------------------------- */
960 /* this one uses the automatic sound standard detection of newer           */
961 /* msp34xx chip versions                                                   */
962
963 static struct MODES {
964         int retval;
965         int main, second;
966         char *name;
967 } modelist[] = {
968         { 0x0000, 0, 0, "ERROR" },
969         { 0x0001, 0, 0, "autodetect start" },
970         { 0x0002, MSP_CARRIER(4.5), MSP_CARRIER(4.72), "4.5/4.72  M Dual FM-Stereo" },
971         { 0x0003, MSP_CARRIER(5.5), MSP_CARRIER(5.7421875), "5.5/5.74  B/G Dual FM-Stereo" },
972         { 0x0004, MSP_CARRIER(6.5), MSP_CARRIER(6.2578125), "6.5/6.25  D/K1 Dual FM-Stereo" },
973         { 0x0005, MSP_CARRIER(6.5), MSP_CARRIER(6.7421875), "6.5/6.74  D/K2 Dual FM-Stereo" },
974         { 0x0006, MSP_CARRIER(6.5), MSP_CARRIER(6.5), "6.5  D/K FM-Mono (HDEV3)" },
975         { 0x0008, MSP_CARRIER(5.5), MSP_CARRIER(5.85), "5.5/5.85  B/G NICAM FM" },
976         { 0x0009, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85  L NICAM AM" },
977         { 0x000a, MSP_CARRIER(6.0), MSP_CARRIER(6.55), "6.0/6.55  I NICAM FM" },
978         { 0x000b, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85  D/K NICAM FM" },
979         { 0x000c, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85  D/K NICAM FM (HDEV2)" },
980         { 0x0020, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5  M BTSC-Stereo" },
981         { 0x0021, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5  M BTSC-Mono + SAP" },
982         { 0x0030, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5  M EIA-J Japan Stereo" },
983         { 0x0040, MSP_CARRIER(10.7), MSP_CARRIER(10.7), "10.7  FM-Stereo Radio" },
984         { 0x0050, MSP_CARRIER(6.5), MSP_CARRIER(6.5), "6.5  SAT-Mono" },
985         { 0x0051, MSP_CARRIER(7.02), MSP_CARRIER(7.20), "7.02/7.20  SAT-Stereo" },
986         { 0x0060, MSP_CARRIER(7.2), MSP_CARRIER(7.2), "7.2  SAT ADR" },
987         {     -1, 0, 0, NULL }, /* EOF */
988 };
989
990 static inline const char *msp34xx_standard_mode_name(int mode)
991 {
992         int i;
993         for (i = 0; modelist[i].name != NULL; i++)
994                 if (modelist[i].retval == mode)
995                         return modelist[i].name;
996         return "unknown";
997 }
998
999 static int msp34xx_modus(int norm)
1000 {
1001         switch (norm) {
1002         case VIDEO_MODE_PAL:
1003                 return 0x1003;
1004         case VIDEO_MODE_NTSC:  /* BTSC */
1005                 return 0x2003;
1006         case VIDEO_MODE_SECAM:
1007                 return 0x0003;
1008         case VIDEO_MODE_RADIO:
1009                 return 0x0003;
1010         case VIDEO_MODE_AUTO:
1011                 return 0x2003;
1012         default:
1013                 return 0x0003;
1014         }
1015 }
1016
1017 static int msp34xx_standard(int norm)
1018 {
1019         switch (norm) {
1020         case VIDEO_MODE_PAL:
1021                 return 1;
1022         case VIDEO_MODE_NTSC:  /* BTSC */
1023                 return 0x0020;
1024         case VIDEO_MODE_SECAM:
1025                 return 1;
1026         case VIDEO_MODE_RADIO:
1027                 return 0x0040;
1028         default:
1029                 return 1;
1030         }
1031 }
1032
1033 static int msp3410d_thread(void *data)
1034 {
1035         struct i2c_client *client = data;
1036         struct msp3400c *msp = i2c_get_clientdata(client);
1037         int mode,val,i,std;
1038
1039         printk("msp3410: daemon started\n");
1040         for (;;) {
1041                 d2printk(KERN_DEBUG "msp3410: thread: sleep\n");
1042                 msp34xx_sleep(msp,-1);
1043                 d2printk(KERN_DEBUG "msp3410: thread: wakeup\n");
1044
1045         restart:
1046                 dprintk("msp3410: thread: restart scan\n");
1047                 msp->restart = 0;
1048                 if (kthread_should_stop())
1049                         break;
1050
1051                 if (msp->mode == MSP_MODE_EXTERN) {
1052                         /* no carrier scan needed, just unmute */
1053                         dprintk(KERN_DEBUG "msp3410: thread: no carrier scan\n");
1054                         msp3400c_setvolume(client, msp->muted,
1055                                            msp->volume, msp->balance);
1056                         continue;
1057                 }
1058
1059                 /* put into sane state (and mute) */
1060                 msp3400c_reset(client);
1061
1062                 /* some time for the tuner to sync */
1063                 if (msp34xx_sleep(msp,200))
1064                         goto restart;
1065
1066                 /* start autodetect */
1067                 mode = msp34xx_modus(msp->norm);
1068                 std  = msp34xx_standard(msp->norm);
1069                 msp3400c_write(client, I2C_MSP3400C_DEM, 0x30, mode);
1070                 msp3400c_write(client, I2C_MSP3400C_DEM, 0x20, std);
1071                 msp->watch_stereo = 0;
1072
1073                 if (debug)
1074                         printk(KERN_DEBUG "msp3410: setting mode: %s (0x%04x)\n",
1075                                msp34xx_standard_mode_name(std) ,std);
1076
1077                 if (std != 1) {
1078                         /* programmed some specific mode */
1079                         val = std;
1080                 } else {
1081                         /* triggered autodetect */
1082                         for (;;) {
1083                                 if (msp34xx_sleep(msp,100))
1084                                         goto restart;
1085
1086                                 /* check results */
1087                                 val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x7e);
1088                                 if (val < 0x07ff)
1089                                         break;
1090                                 dprintk(KERN_DEBUG "msp3410: detection still in progress\n");
1091                         }
1092                 }
1093                 for (i = 0; modelist[i].name != NULL; i++)
1094                         if (modelist[i].retval == val)
1095                                 break;
1096                 dprintk(KERN_DEBUG "msp3410: current mode: %s (0x%04x)\n",
1097                         modelist[i].name ? modelist[i].name : "unknown",
1098                         val);
1099                 msp->main   = modelist[i].main;
1100                 msp->second = modelist[i].second;
1101
1102                 if (amsound && (msp->norm == VIDEO_MODE_SECAM) && (val != 0x0009)) {
1103                         /* autodetection has failed, let backup */
1104                         dprintk(KERN_DEBUG "msp3410: autodetection failed,"
1105                                 " switching to backup mode: %s (0x%04x)\n",
1106                                 modelist[8].name ? modelist[8].name : "unknown",val);
1107                         val = 0x0009;
1108                         msp3400c_write(client, I2C_MSP3400C_DEM, 0x20, val);
1109                 }
1110
1111                 /* set various prescales */
1112                 msp3400c_write(client, I2C_MSP3400C_DFP, 0x0d, 0x1900); /* scart */
1113                 msp3400c_write(client, I2C_MSP3400C_DFP, 0x0e, 0x2403); /* FM */
1114                 msp3400c_write(client, I2C_MSP3400C_DFP, 0x10, 0x5a00); /* nicam */
1115
1116                 /* set stereo */
1117                 switch (val) {
1118                 case 0x0008: /* B/G NICAM */
1119                 case 0x000a: /* I NICAM */
1120                         if (val == 0x0008)
1121                                 msp->mode = MSP_MODE_FM_NICAM1;
1122                         else
1123                                 msp->mode = MSP_MODE_FM_NICAM2;
1124                         /* just turn on stereo */
1125                         msp->rxsubchans = V4L2_TUNER_SUB_STEREO;
1126                         msp->nicam_on = 1;
1127                         msp->watch_stereo = 1;
1128                         msp3400c_set_audmode(client,V4L2_TUNER_MODE_STEREO);
1129                         break;
1130                 case 0x0009:
1131                         msp->mode = MSP_MODE_AM_NICAM;
1132                         msp->rxsubchans = V4L2_TUNER_SUB_MONO;
1133                         msp->nicam_on = 1;
1134                         msp3400c_set_audmode(client,V4L2_TUNER_MODE_MONO);
1135                         msp->watch_stereo = 1;
1136                         break;
1137                 case 0x0020: /* BTSC */
1138                         /* just turn on stereo */
1139                         msp->mode = MSP_MODE_BTSC;
1140                         msp->rxsubchans = V4L2_TUNER_SUB_STEREO;
1141                         msp->nicam_on = 0;
1142                         msp->watch_stereo = 1;
1143                         msp3400c_set_audmode(client,V4L2_TUNER_MODE_STEREO);
1144                         break;
1145                 case 0x0040: /* FM radio */
1146                         msp->mode   = MSP_MODE_FM_RADIO;
1147                         msp->rxsubchans = V4L2_TUNER_SUB_STEREO;
1148                         msp->audmode = V4L2_TUNER_MODE_STEREO;
1149                         msp->nicam_on = 0;
1150                         msp->watch_stereo = 0;
1151                         /* not needed in theory if HAVE_RADIO(), but
1152                            short programming enables carrier mute */
1153                         msp3400c_setmode(client,MSP_MODE_FM_RADIO);
1154                         msp3400c_setcarrier(client, MSP_CARRIER(10.7),
1155                                             MSP_CARRIER(10.7));
1156                         /* scart routing */
1157                         msp3400c_set_scart(client,SCART_IN2,0);
1158 #if 0
1159                         /* radio from SCART_IN2 */
1160                         msp3400c_write(client,I2C_MSP3400C_DFP, 0x08, 0x0220);
1161                         msp3400c_write(client,I2C_MSP3400C_DFP, 0x09, 0x0220);
1162                         msp3400c_write(client,I2C_MSP3400C_DFP, 0x0b, 0x0220);
1163 #else
1164                         /* msp34xx does radio decoding */
1165                         msp3400c_write(client,I2C_MSP3400C_DFP, 0x08, 0x0020);
1166                         msp3400c_write(client,I2C_MSP3400C_DFP, 0x09, 0x0020);
1167                         msp3400c_write(client,I2C_MSP3400C_DFP, 0x0b, 0x0020);
1168 #endif
1169                         break;
1170                 case 0x0003:
1171                 case 0x0004:
1172                 case 0x0005:
1173                         msp->mode   = MSP_MODE_FM_TERRA;
1174                         msp->rxsubchans = V4L2_TUNER_SUB_MONO;
1175                         msp->audmode = V4L2_TUNER_MODE_MONO;
1176                         msp->nicam_on = 0;
1177                         msp->watch_stereo = 1;
1178                         break;
1179                 }
1180
1181                 /* unmute, restore misc registers */
1182                 msp3400c_setbass(client, msp->bass);
1183                 msp3400c_settreble(client, msp->treble);
1184                 msp3400c_setvolume(client, msp->muted,
1185                                     msp->volume, msp->balance);
1186                 msp3400c_write(client, I2C_MSP3400C_DFP, 0x0013, msp->acb);
1187
1188                 /* monitor tv audio mode */
1189                 while (msp->watch_stereo) {
1190                         if (msp34xx_sleep(msp,5000))
1191                                 goto restart;
1192                         watch_stereo(client);
1193                 }
1194         }
1195         dprintk(KERN_DEBUG "msp3410: thread: exit\n");
1196         return 0;
1197 }
1198
1199 /* ----------------------------------------------------------------------- */
1200 /* msp34xxG + (simpler no-thread)                                          */
1201 /* this one uses both automatic standard detection and automatic sound     */
1202 /* select which are available in the newer G versions                      */
1203 /* struct msp: only norm, acb and source are really used in this mode      */
1204
1205 static void msp34xxg_set_source(struct i2c_client *client, int source);
1206
1207 /* (re-)initialize the msp34xxg, according to the current norm in msp->norm
1208  * return 0 if it worked, -1 if it failed
1209  */
1210 static int msp34xxg_init(struct i2c_client *client)
1211 {
1212         struct msp3400c *msp = i2c_get_clientdata(client);
1213         int modus;
1214
1215         if (msp3400c_reset(client))
1216                 return -1;
1217
1218         /* make sure that input/output is muted (paranoid mode) */
1219         if (msp3400c_write(client,
1220                            I2C_MSP3400C_DFP,
1221                            0x13, /* ACB */
1222                            0x0f20 /* mute DSP input, mute SCART 1 */))
1223                 return -1;
1224
1225         /* step-by-step initialisation, as described in the manual */
1226         modus = msp34xx_modus(msp->norm);
1227         modus &= ~0x03; /* STATUS_CHANGE=0 */
1228         modus |= 0x01;  /* AUTOMATIC_SOUND_DETECTION=1 */
1229         if (msp3400c_write(client,
1230                            I2C_MSP3400C_DEM,
1231                            0x30/*MODUS*/,
1232                            modus))
1233                 return -1;
1234
1235         /* write the dfps that may have an influence on
1236            standard/audio autodetection right now */
1237         msp34xxg_set_source(client, msp->source);
1238
1239         if (msp3400c_write(client, I2C_MSP3400C_DFP,
1240                            0x0e, /* AM/FM Prescale */
1241                            0x3000 /* default: [15:8] 75khz deviation */))
1242                 return -1;
1243
1244         if (msp3400c_write(client, I2C_MSP3400C_DFP,
1245                            0x10, /* NICAM Prescale */
1246                            0x5a00 /* default: 9db gain (as recommended) */))
1247                 return -1;
1248
1249         if (msp3400c_write(client,
1250                            I2C_MSP3400C_DEM,
1251                            0x20, /* STANDARD SELECT  */
1252                            standard /* default: 0x01 for automatic standard select*/))
1253                 return -1;
1254         return 0;
1255 }
1256
1257 static int msp34xxg_thread(void *data)
1258 {
1259         struct i2c_client *client = data;
1260         struct msp3400c *msp = i2c_get_clientdata(client);
1261         int val, std, i;
1262
1263         printk("msp34xxg: daemon started\n");
1264         for (;;) {
1265                 d2printk(KERN_DEBUG "msp34xxg: thread: sleep\n");
1266                 msp34xx_sleep(msp,-1);
1267                 d2printk(KERN_DEBUG "msp34xxg: thread: wakeup\n");
1268
1269         restart:
1270                 dprintk("msp34xxg: thread: restart scan\n");
1271                 msp->restart = 0;
1272                 if (kthread_should_stop())
1273                         break;
1274
1275                 /* setup the chip*/
1276                 msp34xxg_init(client);
1277                 std = standard;
1278                 if (std != 0x01)
1279                         goto unmute;
1280
1281                 /* watch autodetect */
1282                 dprintk("msp34xxg: triggered autodetect, waiting for result\n");
1283                 for (i = 0; i < 10; i++) {
1284                         if (msp34xx_sleep(msp,100))
1285                                 goto restart;
1286
1287                         /* check results */
1288                         val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x7e);
1289                         if (val < 0x07ff) {
1290                                 std = val;
1291                                 break;
1292                         }
1293                         dprintk("msp34xxg: detection still in progress\n");
1294                 }
1295                 if (0x01 == std) {
1296                         dprintk("msp34xxg: detection still in progress after 10 tries. giving up.\n");
1297                         continue;
1298                 }
1299
1300         unmute:
1301                 dprintk("msp34xxg: current mode: %s (0x%04x)\n",
1302                         msp34xx_standard_mode_name(std), std);
1303
1304                 /* unmute: dispatch sound to scart output, set scart volume */
1305                 dprintk("msp34xxg: unmute\n");
1306
1307                 msp3400c_setbass(client, msp->bass);
1308                 msp3400c_settreble(client, msp->treble);
1309                 msp3400c_setvolume(client, msp->muted, msp->volume, msp->balance);
1310
1311                 /* restore ACB */
1312                 if (msp3400c_write(client,
1313                                    I2C_MSP3400C_DFP,
1314                                    0x13, /* ACB */
1315                                    msp->acb))
1316                         return -1;
1317         }
1318         dprintk(KERN_DEBUG "msp34xxg: thread: exit\n");
1319         return 0;
1320 }
1321
1322 /* set the same 'source' for the loudspeaker, scart and quasi-peak detector
1323  * the value for source is the same as bit 15:8 of DFP registers 0x08,
1324  * 0x0a and 0x0c: 0=mono, 1=stereo or A|B, 2=SCART, 3=stereo or A, 4=stereo or B
1325  *
1326  * this function replaces msp3400c_setstereo
1327  */
1328 static void msp34xxg_set_source(struct i2c_client *client, int source)
1329 {
1330         struct msp3400c *msp = i2c_get_clientdata(client);
1331
1332         /* fix matrix mode to stereo and let the msp choose what
1333          * to output according to 'source', as recommended
1334          */
1335         int value = (source&0x07)<<8|(source==0 ? 0x00:0x20);
1336         dprintk("msp34xxg: set source to %d (0x%x)\n", source, value);
1337         msp3400c_write(client,
1338                        I2C_MSP3400C_DFP,
1339                        0x08, /* Loudspeaker Output */
1340                        value);
1341         msp3400c_write(client,
1342                        I2C_MSP3400C_DFP,
1343                        0x0a, /* SCART1 DA Output */
1344                        value);
1345         msp3400c_write(client,
1346                        I2C_MSP3400C_DFP,
1347                        0x0c, /* Quasi-peak detector */
1348                        value);
1349         /*
1350          * set identification threshold. Personally, I
1351          * I set it to a higher value that the default
1352          * of 0x190 to ignore noisy stereo signals.
1353          * this needs tuning. (recommended range 0x00a0-0x03c0)
1354          * 0x7f0 = forced mono mode
1355          */
1356         msp3400c_write(client,
1357                        I2C_MSP3400C_DEM,
1358                        0x22, /* a2 threshold for stereo/bilingual */
1359                        source==0 ? 0x7f0:stereo_threshold);
1360         msp->source=source;
1361 }
1362
1363 static void msp34xxg_detect_stereo(struct i2c_client *client)
1364 {
1365         struct msp3400c *msp = i2c_get_clientdata(client);
1366
1367         int status = msp3400c_read(client,
1368                                    I2C_MSP3400C_DEM,
1369                                    0x0200 /* STATUS */);
1370         int is_bilingual = status&0x100;
1371         int is_stereo = status&0x40;
1372
1373         msp->rxsubchans = 0;
1374         if (is_stereo)
1375                 msp->rxsubchans |= V4L2_TUNER_SUB_STEREO;
1376         else
1377                 msp->rxsubchans |= V4L2_TUNER_SUB_MONO;
1378         if (is_bilingual) {
1379                 msp->rxsubchans |= V4L2_TUNER_SUB_LANG1|V4L2_TUNER_SUB_LANG2;
1380                 /* I'm supposed to check whether it's SAP or not
1381                  * and set only LANG2/SAP in this case. Yet, the MSP
1382                  * does a lot of work to hide this and handle everything
1383                  * the same way. I don't want to work around it so unless
1384                  * this is a problem, I'll handle SAP just like lang1/lang2.
1385                  */
1386         }
1387         dprintk("msp34xxg: status=0x%x, stereo=%d, bilingual=%d -> rxsubchans=%d\n",
1388                 status, is_stereo, is_bilingual, msp->rxsubchans);
1389 }
1390
1391 static void msp34xxg_set_audmode(struct i2c_client *client, int audmode)
1392 {
1393         struct msp3400c *msp = i2c_get_clientdata(client);
1394         int source = 0;
1395
1396         switch (audmode) {
1397         case V4L2_TUNER_MODE_MONO:
1398                 source=0; /* mono only */
1399                 break;
1400         case V4L2_TUNER_MODE_STEREO:
1401                 source=1; /* stereo or A|B, see comment in msp34xxg_get_v4l2_stereo() */
1402                 /* problem: that could also mean 2 (scart input) */
1403                 break;
1404         case V4L2_TUNER_MODE_LANG1:
1405                 source=3; /* stereo or A */
1406                 break;
1407         case V4L2_TUNER_MODE_LANG2:
1408                 source=4; /* stereo or B */
1409                 break;
1410         default: /* doing nothing: a safe, sane default */
1411                 audmode = 0;
1412                 return;
1413         }
1414         msp->audmode = audmode;
1415         msp34xxg_set_source(client, source);
1416 }
1417
1418
1419 /* ----------------------------------------------------------------------- */
1420
1421 static int msp_attach(struct i2c_adapter *adap, int addr, int kind);
1422 static int msp_detach(struct i2c_client *client);
1423 static int msp_probe(struct i2c_adapter *adap);
1424 static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg);
1425
1426 static int msp_suspend(struct device * dev, u32 state, u32 level);
1427 static int msp_resume(struct device * dev, u32 level);
1428
1429 static void msp_wake_thread(struct i2c_client *client);
1430
1431 static struct i2c_driver driver = {
1432         .owner          = THIS_MODULE,
1433         .name           = "i2c msp3400 driver",
1434         .id             = I2C_DRIVERID_MSP3400,
1435         .flags          = I2C_DF_NOTIFY,
1436         .attach_adapter = msp_probe,
1437         .detach_client  = msp_detach,
1438         .command        = msp_command,
1439         .driver = {
1440                 .suspend = msp_suspend,
1441                 .resume  = msp_resume,
1442         },
1443 };
1444
1445 static struct i2c_client client_template =
1446 {
1447         I2C_DEVNAME("(unset)"),
1448         .flags     = I2C_CLIENT_ALLOW_USE,
1449         .driver    = &driver,
1450 };
1451
1452 static int msp_attach(struct i2c_adapter *adap, int addr, int kind)
1453 {
1454         struct msp3400c *msp;
1455         struct i2c_client *c;
1456         int (*thread_func)(void *data) = NULL;
1457
1458         client_template.adapter = adap;
1459         client_template.addr = addr;
1460
1461         if (-1 == msp3400c_reset(&client_template)) {
1462                 dprintk("msp3400: no chip found\n");
1463                 return -1;
1464         }
1465
1466         if (NULL == (c = kmalloc(sizeof(struct i2c_client),GFP_KERNEL)))
1467                 return -ENOMEM;
1468         memcpy(c,&client_template,sizeof(struct i2c_client));
1469         if (NULL == (msp = kmalloc(sizeof(struct msp3400c),GFP_KERNEL))) {
1470                 kfree(c);
1471                 return -ENOMEM;
1472         }
1473
1474         memset(msp,0,sizeof(struct msp3400c));
1475         msp->volume  = 58880;  /* 0db gain */
1476         msp->balance = 32768;
1477         msp->bass    = 32768;
1478         msp->treble  = 32768;
1479         msp->input   = -1;
1480         msp->muted   = 1;
1481
1482         i2c_set_clientdata(c, msp);
1483         init_waitqueue_head(&msp->wq);
1484
1485         if (-1 == msp3400c_reset(c)) {
1486                 kfree(msp);
1487                 kfree(c);
1488                 dprintk("msp3400: no chip found\n");
1489                 return -1;
1490         }
1491
1492         msp->rev1 = msp3400c_read(c, I2C_MSP3400C_DFP, 0x1e);
1493         if (-1 != msp->rev1)
1494                 msp->rev2 = msp3400c_read(c, I2C_MSP3400C_DFP, 0x1f);
1495         if ((-1 == msp->rev1) || (0 == msp->rev1 && 0 == msp->rev2)) {
1496                 kfree(msp);
1497                 kfree(c);
1498                 printk("msp3400: error while reading chip version\n");
1499                 return -1;
1500         }
1501
1502 #if 0
1503         /* this will turn on a 1kHz beep - might be useful for debugging... */
1504         msp3400c_write(c,I2C_MSP3400C_DFP, 0x0014, 0x1040);
1505 #endif
1506         msp3400c_setvolume(c, msp->muted, msp->volume, msp->balance);
1507
1508         snprintf(c->name, sizeof(c->name), "MSP34%02d%c-%c%d",
1509                  (msp->rev2>>8)&0xff, (msp->rev1&0xff)+'@',
1510                  ((msp->rev1>>8)&0xff)+'@', msp->rev2&0x1f);
1511
1512         msp->opmode = opmode;
1513         if (OPMODE_AUTO == msp->opmode) {
1514 #if 0 /* seems to work for ivtv only, disable by default for now ... */
1515                 if (HAVE_SIMPLER(msp))
1516                         msp->opmode = OPMODE_SIMPLER;
1517                 else
1518 #endif
1519                 if (HAVE_SIMPLE(msp))
1520                         msp->opmode = OPMODE_SIMPLE;
1521                 else
1522                         msp->opmode = OPMODE_MANUAL;
1523         }
1524
1525         /* hello world :-) */
1526         printk(KERN_INFO "msp34xx: init: chip=%s",i2c_clientname(c));
1527         if (HAVE_NICAM(msp))
1528                 printk(" +nicam");
1529         if (HAVE_SIMPLE(msp))
1530                 printk(" +simple");
1531         if (HAVE_SIMPLER(msp))
1532                 printk(" +simpler");
1533         if (HAVE_RADIO(msp))
1534                 printk(" +radio");
1535
1536         /* version-specific initialization */
1537         switch (msp->opmode) {
1538         case OPMODE_MANUAL:
1539                 printk(" mode=manual");
1540                 thread_func = msp3400c_thread;
1541                 break;
1542         case OPMODE_SIMPLE:
1543                 printk(" mode=simple");
1544                 thread_func = msp3410d_thread;
1545                 break;
1546         case OPMODE_SIMPLER:
1547                 printk(" mode=simpler");
1548                 thread_func = msp34xxg_thread;
1549                 break;
1550         }
1551         printk("\n");
1552
1553         /* startup control thread if needed */
1554         if (thread_func) {
1555                 msp->kthread = kthread_run(thread_func, c, "msp34xx");
1556                 if (NULL == msp->kthread)
1557                         printk(KERN_WARNING "msp34xx: kernel_thread() failed\n");
1558                 msp_wake_thread(c);
1559         }
1560
1561         /* done */
1562         i2c_attach_client(c);
1563         return 0;
1564 }
1565
1566 static int msp_detach(struct i2c_client *client)
1567 {
1568         struct msp3400c *msp  = i2c_get_clientdata(client);
1569
1570         /* shutdown control thread */
1571         if (msp->kthread >= 0) {
1572                 msp->restart = 1;
1573                 kthread_stop(msp->kthread);
1574         }
1575         msp3400c_reset(client);
1576
1577         i2c_detach_client(client);
1578         kfree(msp);
1579         kfree(client);
1580         return 0;
1581 }
1582
1583 static int msp_probe(struct i2c_adapter *adap)
1584 {
1585         if (adap->class & I2C_CLASS_TV_ANALOG)
1586                 return i2c_probe(adap, &addr_data, msp_attach);
1587         return 0;
1588 }
1589
1590 static void msp_wake_thread(struct i2c_client *client)
1591 {
1592         struct msp3400c *msp  = i2c_get_clientdata(client);
1593
1594         if (NULL == msp->kthread)
1595                 return;
1596         msp3400c_setvolume(client,msp->muted,0,0);
1597         msp->watch_stereo = 0;
1598         msp->restart = 1;
1599         wake_up_interruptible(&msp->wq);
1600 }
1601
1602 /* ----------------------------------------------------------------------- */
1603
1604 static int mode_v4l2_to_v4l1(int rxsubchans)
1605 {
1606         int mode = 0;
1607
1608         if (rxsubchans & V4L2_TUNER_SUB_STEREO)
1609                 mode |= VIDEO_SOUND_STEREO;
1610         if (rxsubchans & V4L2_TUNER_SUB_LANG2)
1611                 mode |= VIDEO_SOUND_LANG2;
1612         if (rxsubchans & V4L2_TUNER_SUB_LANG1)
1613                 mode |= VIDEO_SOUND_LANG1;
1614         if (0 == mode)
1615                 mode |= VIDEO_SOUND_MONO;
1616         return mode;
1617 }
1618
1619 static int mode_v4l1_to_v4l2(int mode)
1620 {
1621         if (mode & VIDEO_SOUND_STEREO)
1622                 return V4L2_TUNER_MODE_STEREO;
1623         if (mode & VIDEO_SOUND_LANG2)
1624                 return V4L2_TUNER_MODE_LANG2;
1625         if (mode & VIDEO_SOUND_LANG1)
1626                 return V4L2_TUNER_MODE_LANG1;
1627         return V4L2_TUNER_MODE_MONO;
1628 }
1629
1630 static void msp_any_detect_stereo(struct i2c_client *client)
1631 {
1632         struct msp3400c *msp  = i2c_get_clientdata(client);
1633
1634         switch (msp->opmode) {
1635         case OPMODE_MANUAL:
1636         case OPMODE_SIMPLE:
1637                 autodetect_stereo(client);
1638                 break;
1639         case OPMODE_SIMPLER:
1640                 msp34xxg_detect_stereo(client);
1641                 break;
1642         }
1643 }
1644
1645 static void msp_any_set_audmode(struct i2c_client *client, int audmode)
1646 {
1647         struct msp3400c *msp  = i2c_get_clientdata(client);
1648
1649         switch (msp->opmode) {
1650         case OPMODE_MANUAL:
1651         case OPMODE_SIMPLE:
1652                 msp->watch_stereo = 0;
1653                 msp3400c_set_audmode(client, audmode);
1654                 break;
1655         case OPMODE_SIMPLER:
1656                 msp34xxg_set_audmode(client, audmode);
1657                 break;
1658         }
1659 }
1660
1661 static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg)
1662 {
1663         struct msp3400c *msp  = i2c_get_clientdata(client);
1664         __u16           *sarg = arg;
1665         int scart = 0;
1666
1667         switch (cmd) {
1668
1669         case AUDC_SET_INPUT:
1670                 dprintk(KERN_DEBUG "msp34xx: AUDC_SET_INPUT(%d)\n",*sarg);
1671                 if (*sarg == msp->input)
1672                         break;
1673                 msp->input = *sarg;
1674                 switch (*sarg) {
1675                 case AUDIO_RADIO:
1676                         /* Hauppauge uses IN2 for the radio */
1677                         msp->mode   = MSP_MODE_FM_RADIO;
1678                         scart       = SCART_IN2;
1679                         break;
1680                 case AUDIO_EXTERN_1:
1681                         /* IN1 is often used for external input ... */
1682                         msp->mode   = MSP_MODE_EXTERN;
1683                         scart       = SCART_IN1;
1684                         break;
1685                 case AUDIO_EXTERN_2:
1686                         /* ... sometimes it is IN2 through ;) */
1687                         msp->mode   = MSP_MODE_EXTERN;
1688                         scart       = SCART_IN2;
1689                         break;
1690                 case AUDIO_TUNER:
1691                         msp->mode   = -1;
1692                         break;
1693                 default:
1694                         if (*sarg & AUDIO_MUTE)
1695                                 msp3400c_set_scart(client,SCART_MUTE,0);
1696                         break;
1697                 }
1698                 if (scart) {
1699                         msp->rxsubchans = V4L2_TUNER_SUB_STEREO;
1700                         msp->audmode = V4L2_TUNER_MODE_STEREO;
1701                         msp3400c_set_scart(client,scart,0);
1702                         msp3400c_write(client,I2C_MSP3400C_DFP,0x000d,0x1900);
1703                         if (msp->opmode != OPMODE_SIMPLER)
1704                                 msp3400c_set_audmode(client, msp->audmode);
1705                 }
1706                 msp_wake_thread(client);
1707                 break;
1708
1709         case AUDC_SET_RADIO:
1710                 dprintk(KERN_DEBUG "msp34xx: AUDC_SET_RADIO\n");
1711                 msp->norm = VIDEO_MODE_RADIO;
1712                 dprintk(KERN_DEBUG "msp34xx: switching to radio mode\n");
1713                 msp->watch_stereo = 0;
1714                 switch (msp->opmode) {
1715                 case OPMODE_MANUAL:
1716                         /* set msp3400 to FM radio mode */
1717                         msp3400c_setmode(client,MSP_MODE_FM_RADIO);
1718                         msp3400c_setcarrier(client, MSP_CARRIER(10.7),
1719                                             MSP_CARRIER(10.7));
1720                         msp3400c_setvolume(client, msp->muted,
1721                                            msp->volume, msp->balance);
1722                         break;
1723                 case OPMODE_SIMPLE:
1724                 case OPMODE_SIMPLER:
1725                         /* the thread will do for us */
1726                         msp_wake_thread(client);
1727                         break;
1728                 }
1729                 break;
1730
1731         /* --- v4l ioctls --- */
1732         /* take care: bttv does userspace copying, we'll get a
1733            kernel pointer here... */
1734         case VIDIOCGAUDIO:
1735         {
1736                 struct video_audio *va = arg;
1737
1738                 dprintk(KERN_DEBUG "msp34xx: VIDIOCGAUDIO\n");
1739                 va->flags |= VIDEO_AUDIO_VOLUME |
1740                         VIDEO_AUDIO_BASS |
1741                         VIDEO_AUDIO_TREBLE |
1742                         VIDEO_AUDIO_MUTABLE;
1743                 if (msp->muted)
1744                         va->flags |= VIDEO_AUDIO_MUTE;
1745
1746                 va->volume = msp->volume;
1747                 va->balance = (va->volume) ? msp->balance : 32768;
1748                 va->bass = msp->bass;
1749                 va->treble = msp->treble;
1750
1751                 msp_any_detect_stereo(client);
1752                 va->mode = mode_v4l2_to_v4l1(msp->rxsubchans);
1753                 break;
1754         }
1755         case VIDIOCSAUDIO:
1756         {
1757                 struct video_audio *va = arg;
1758
1759                 dprintk(KERN_DEBUG "msp34xx: VIDIOCSAUDIO\n");
1760                 msp->muted = (va->flags & VIDEO_AUDIO_MUTE);
1761                 msp->volume = va->volume;
1762                 msp->balance = va->balance;
1763                 msp->bass = va->bass;
1764                 msp->treble = va->treble;
1765
1766                 msp3400c_setvolume(client, msp->muted,
1767                                    msp->volume, msp->balance);
1768                 msp3400c_setbass(client,msp->bass);
1769                 msp3400c_settreble(client,msp->treble);
1770
1771                 if (va->mode != 0 && msp->norm != VIDEO_MODE_RADIO)
1772                         msp_any_set_audmode(client,mode_v4l1_to_v4l2(va->mode));
1773                 break;
1774         }
1775         case VIDIOCSCHAN:
1776         {
1777                 struct video_channel *vc = arg;
1778
1779                 dprintk(KERN_DEBUG "msp34xx: VIDIOCSCHAN (norm=%d)\n",vc->norm);
1780                 msp->norm = vc->norm;
1781                 msp_wake_thread(client);
1782                 break;
1783         }
1784
1785         case VIDIOCSFREQ:
1786         case VIDIOC_S_FREQUENCY:
1787         {
1788                 /* new channel -- kick audio carrier scan */
1789                 dprintk(KERN_DEBUG "msp34xx: VIDIOCSFREQ\n");
1790                 msp_wake_thread(client);
1791                 break;
1792         }
1793
1794         /* --- v4l2 ioctls --- */
1795         case VIDIOC_G_TUNER:
1796         {
1797                 struct v4l2_tuner *vt = arg;
1798
1799                 msp_any_detect_stereo(client);
1800                 vt->audmode    = msp->audmode;
1801                 vt->rxsubchans = msp->rxsubchans;
1802                 vt->capability = V4L2_TUNER_CAP_STEREO |
1803                         V4L2_TUNER_CAP_LANG1|
1804                         V4L2_TUNER_CAP_LANG2;
1805                 break;
1806         }
1807         case VIDIOC_S_TUNER:
1808         {
1809                 struct v4l2_tuner *vt=(struct v4l2_tuner *)arg;
1810
1811                 /* only set audmode */
1812                 if (vt->audmode != -1 && vt->audmode != 0)
1813                         msp_any_set_audmode(client, vt->audmode);
1814                 break;
1815         }
1816
1817         /* msp34xx specific */
1818         case MSP_SET_MATRIX:
1819         {
1820                 struct msp_matrix *mspm = arg;
1821
1822                 dprintk(KERN_DEBUG "msp34xx: MSP_SET_MATRIX\n");
1823                 msp3400c_set_scart(client, mspm->input, mspm->output);
1824                 break;
1825         }
1826
1827         default:
1828                 /* nothing */
1829                 break;
1830         }
1831         return 0;
1832 }
1833
1834 static int msp_suspend(struct device * dev, u32 state, u32 level)
1835 {
1836         struct i2c_client *c = container_of(dev, struct i2c_client, dev);
1837
1838         dprintk("msp34xx: suspend\n");
1839         msp3400c_reset(c);
1840         return 0;
1841 }
1842
1843 static int msp_resume(struct device * dev, u32 level)
1844 {
1845         struct i2c_client *c = container_of(dev, struct i2c_client, dev);
1846
1847         dprintk("msp34xx: resume\n");
1848         msp_wake_thread(c);
1849         return 0;
1850 }
1851
1852 /* ----------------------------------------------------------------------- */
1853
1854 static int __init msp3400_init_module(void)
1855 {
1856         return i2c_add_driver(&driver);
1857 }
1858
1859 static void __exit msp3400_cleanup_module(void)
1860 {
1861         i2c_del_driver(&driver);
1862 }
1863
1864 module_init(msp3400_init_module);
1865 module_exit(msp3400_cleanup_module);
1866
1867 /*
1868  * Overrides for Emacs so that we follow Linus's tabbing style.
1869  * ---------------------------------------------------------------------------
1870  * Local variables:
1871  * c-basic-offset: 8
1872  * End:
1873  */