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