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