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