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