]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/media/video/msp3400.c
Merge /spare/repo/linux-2.6/
[karo-tx-linux.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 #if 0
571                 /* spatial effect */
572                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0005,0x4000);
573 #endif
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 #if 0
745                         /* hmm, that one doesn't return on wakeup ... */
746                         msleep_interruptible(timeout);
747 #else
748                         set_current_state(TASK_INTERRUPTIBLE);
749                         schedule_timeout(msecs_to_jiffies(timeout));
750 #endif
751                 }
752         }
753         try_to_freeze();
754         remove_wait_queue(&msp->wq, &wait);
755         return msp->restart;
756 }
757
758 /* stereo/multilang monitoring */
759 static void watch_stereo(struct i2c_client *client)
760 {
761         struct msp3400c *msp = i2c_get_clientdata(client);
762
763         if (autodetect_stereo(client))
764                 msp3400c_set_audmode(client,best_audio_mode(msp->rxsubchans));
765         if (once)
766                 msp->watch_stereo = 0;
767 }
768
769 static int msp3400c_thread(void *data)
770 {
771         struct i2c_client *client = data;
772         struct msp3400c *msp = i2c_get_clientdata(client);
773         struct CARRIER_DETECT *cd;
774         int count, max1,max2,val1,val2, val,this;
775
776         printk("msp3400: kthread started\n");
777         for (;;) {
778                 d2printk("msp3400: thread: sleep\n");
779                 msp34xx_sleep(msp,-1);
780                 d2printk("msp3400: thread: wakeup\n");
781
782         restart:
783                 dprintk("msp3410: thread: restart scan\n");
784                 msp->restart = 0;
785                 if (kthread_should_stop())
786                         break;
787
788                 if (VIDEO_MODE_RADIO == msp->norm ||
789                     MSP_MODE_EXTERN  == msp->mode) {
790                         /* no carrier scan, just unmute */
791                         printk("msp3400: thread: no carrier scan\n");
792                         msp3400c_setvolume(client, msp->muted,
793                                            msp->volume, msp->balance);
794                         continue;
795                 }
796
797                 /* mute */
798                 msp3400c_setvolume(client, msp->muted, 0, 0);
799                 msp3400c_setmode(client, MSP_MODE_AM_DETECT /* +1 */ );
800                 val1 = val2 = 0;
801                 max1 = max2 = -1;
802                 msp->watch_stereo = 0;
803
804                 /* some time for the tuner to sync */
805                 if (msp34xx_sleep(msp,200))
806                         goto restart;
807
808                 /* carrier detect pass #1 -- main carrier */
809                 cd = carrier_detect_main; count = CARRIER_COUNT(carrier_detect_main);
810
811                 if (amsound && (msp->norm == VIDEO_MODE_SECAM)) {
812                         /* autodetect doesn't work well with AM ... */
813                         max1 = 3;
814                         count = 0;
815                         dprintk("msp3400: AM sound override\n");
816                 }
817
818                 for (this = 0; this < count; this++) {
819                         msp3400c_setcarrier(client, cd[this].cdo,cd[this].cdo);
820                         if (msp34xx_sleep(msp,100))
821                                 goto restart;
822                         val = msp3400c_read(client, I2C_MSP3400C_DFP, 0x1b);
823                         if (val > 32767)
824                                 val -= 65536;
825                         if (val1 < val)
826                                 val1 = val, max1 = this;
827                         dprintk("msp3400: carrier1 val: %5d / %s\n", val,cd[this].name);
828                 }
829
830                 /* carrier detect pass #2 -- second (stereo) carrier */
831                 switch (max1) {
832                 case 1: /* 5.5 */
833                         cd = carrier_detect_55;
834                         count = CARRIER_COUNT(carrier_detect_55);
835                         break;
836                 case 3: /* 6.5 */
837                         cd = carrier_detect_65;
838                         count = CARRIER_COUNT(carrier_detect_65);
839                         break;
840                 case 0: /* 4.5 */
841                 case 2: /* 6.0 */
842                 default:
843                         cd = NULL; count = 0;
844                         break;
845                 }
846
847                 if (amsound && (msp->norm == VIDEO_MODE_SECAM)) {
848                         /* autodetect doesn't work well with AM ... */
849                         cd = NULL; count = 0; max2 = 0;
850                 }
851                 for (this = 0; this < count; this++) {
852                         msp3400c_setcarrier(client, cd[this].cdo,cd[this].cdo);
853                         if (msp34xx_sleep(msp,100))
854                                 goto restart;
855                         val = msp3400c_read(client, I2C_MSP3400C_DFP, 0x1b);
856                         if (val > 32767)
857                                 val -= 65536;
858                         if (val2 < val)
859                                 val2 = val, max2 = this;
860                         dprintk("msp3400: carrier2 val: %5d / %s\n", val,cd[this].name);
861                 }
862
863                 /* programm the msp3400 according to the results */
864                 msp->main   = carrier_detect_main[max1].cdo;
865                 switch (max1) {
866                 case 1: /* 5.5 */
867                         if (max2 == 0) {
868                                 /* B/G FM-stereo */
869                                 msp->second = carrier_detect_55[max2].cdo;
870                                 msp3400c_setmode(client, MSP_MODE_FM_TERRA);
871                                 msp->nicam_on = 0;
872                                 msp3400c_set_audmode(client, V4L2_TUNER_MODE_MONO);
873                                 msp->watch_stereo = 1;
874                         } else if (max2 == 1 && HAVE_NICAM(msp)) {
875                                 /* B/G NICAM */
876                                 msp->second = carrier_detect_55[max2].cdo;
877                                 msp3400c_setmode(client, MSP_MODE_FM_NICAM1);
878                                 msp->nicam_on = 1;
879                                 msp3400c_setcarrier(client, msp->second, msp->main);
880                                 msp->watch_stereo = 1;
881                         } else {
882                                 goto no_second;
883                         }
884                         break;
885                 case 2: /* 6.0 */
886                         /* PAL I NICAM */
887                         msp->second = MSP_CARRIER(6.552);
888                         msp3400c_setmode(client, MSP_MODE_FM_NICAM2);
889                         msp->nicam_on = 1;
890                         msp3400c_setcarrier(client, msp->second, msp->main);
891                         msp->watch_stereo = 1;
892                         break;
893                 case 3: /* 6.5 */
894                         if (max2 == 1 || max2 == 2) {
895                                 /* D/K FM-stereo */
896                                 msp->second = carrier_detect_65[max2].cdo;
897                                 msp3400c_setmode(client, MSP_MODE_FM_TERRA);
898                                 msp->nicam_on = 0;
899                                 msp3400c_set_audmode(client, V4L2_TUNER_MODE_MONO);
900                                 msp->watch_stereo = 1;
901                         } else if (max2 == 0 &&
902                                    msp->norm == VIDEO_MODE_SECAM) {
903                                 /* L NICAM or AM-mono */
904                                 msp->second = carrier_detect_65[max2].cdo;
905                                 msp3400c_setmode(client, MSP_MODE_AM_NICAM);
906                                 msp->nicam_on = 0;
907                                 msp3400c_set_audmode(client, V4L2_TUNER_MODE_MONO);
908                                 msp3400c_setcarrier(client, msp->second, msp->main);
909                                 /* volume prescale for SCART (AM mono input) */
910                                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000d, 0x1900);
911                                 msp->watch_stereo = 1;
912                         } else if (max2 == 0 && HAVE_NICAM(msp)) {
913                                 /* D/K NICAM */
914                                 msp->second = carrier_detect_65[max2].cdo;
915                                 msp3400c_setmode(client, MSP_MODE_FM_NICAM1);
916                                 msp->nicam_on = 1;
917                                 msp3400c_setcarrier(client, msp->second, msp->main);
918                                 msp->watch_stereo = 1;
919                         } else {
920                                 goto no_second;
921                         }
922                         break;
923                 case 0: /* 4.5 */
924                 default:
925                 no_second:
926                         msp->second = carrier_detect_main[max1].cdo;
927                         msp3400c_setmode(client, MSP_MODE_FM_TERRA);
928                         msp->nicam_on = 0;
929                         msp3400c_setcarrier(client, msp->second, msp->main);
930                         msp->rxsubchans = V4L2_TUNER_SUB_MONO;
931                         msp3400c_set_audmode(client, V4L2_TUNER_MODE_MONO);
932                         break;
933                 }
934
935                 /* unmute */
936                 msp3400c_setvolume(client, msp->muted,
937                                    msp->volume, msp->balance);
938                 if (debug)
939                         msp3400c_print_mode(msp);
940
941                 /* monitor tv audio mode */
942                 while (msp->watch_stereo) {
943                         if (msp34xx_sleep(msp,5000))
944                                 goto restart;
945                         watch_stereo(client);
946                 }
947         }
948         dprintk(KERN_DEBUG "msp3400: thread: exit\n");
949         return 0;
950 }
951
952 /* ----------------------------------------------------------------------- */
953 /* this one uses the automatic sound standard detection of newer           */
954 /* msp34xx chip versions                                                   */
955
956 static struct MODES {
957         int retval;
958         int main, second;
959         char *name;
960 } modelist[] = {
961         { 0x0000, 0, 0, "ERROR" },
962         { 0x0001, 0, 0, "autodetect start" },
963         { 0x0002, MSP_CARRIER(4.5), MSP_CARRIER(4.72), "4.5/4.72  M Dual FM-Stereo" },
964         { 0x0003, MSP_CARRIER(5.5), MSP_CARRIER(5.7421875), "5.5/5.74  B/G Dual FM-Stereo" },
965         { 0x0004, MSP_CARRIER(6.5), MSP_CARRIER(6.2578125), "6.5/6.25  D/K1 Dual FM-Stereo" },
966         { 0x0005, MSP_CARRIER(6.5), MSP_CARRIER(6.7421875), "6.5/6.74  D/K2 Dual FM-Stereo" },
967         { 0x0006, MSP_CARRIER(6.5), MSP_CARRIER(6.5), "6.5  D/K FM-Mono (HDEV3)" },
968         { 0x0008, MSP_CARRIER(5.5), MSP_CARRIER(5.85), "5.5/5.85  B/G NICAM FM" },
969         { 0x0009, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85  L NICAM AM" },
970         { 0x000a, MSP_CARRIER(6.0), MSP_CARRIER(6.55), "6.0/6.55  I NICAM FM" },
971         { 0x000b, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85  D/K NICAM FM" },
972         { 0x000c, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85  D/K NICAM FM (HDEV2)" },
973         { 0x0020, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5  M BTSC-Stereo" },
974         { 0x0021, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5  M BTSC-Mono + SAP" },
975         { 0x0030, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5  M EIA-J Japan Stereo" },
976         { 0x0040, MSP_CARRIER(10.7), MSP_CARRIER(10.7), "10.7  FM-Stereo Radio" },
977         { 0x0050, MSP_CARRIER(6.5), MSP_CARRIER(6.5), "6.5  SAT-Mono" },
978         { 0x0051, MSP_CARRIER(7.02), MSP_CARRIER(7.20), "7.02/7.20  SAT-Stereo" },
979         { 0x0060, MSP_CARRIER(7.2), MSP_CARRIER(7.2), "7.2  SAT ADR" },
980         {     -1, 0, 0, NULL }, /* EOF */
981 };
982
983 static inline const char *msp34xx_standard_mode_name(int mode)
984 {
985         int i;
986         for (i = 0; modelist[i].name != NULL; i++)
987                 if (modelist[i].retval == mode)
988                         return modelist[i].name;
989         return "unknown";
990 }
991
992 static int msp34xx_modus(int norm)
993 {
994         switch (norm) {
995         case VIDEO_MODE_PAL:
996 #if 1
997                 /* experimental: not sure this works with all chip versions */
998                 return 0x7003;
999 #else
1000                 /* previous value, try this if it breaks ... */
1001                 return 0x1003;
1002 #endif
1003         case VIDEO_MODE_NTSC:  /* BTSC */
1004                 return 0x2003;
1005         case VIDEO_MODE_SECAM:
1006                 return 0x0003;
1007         case VIDEO_MODE_RADIO:
1008                 return 0x0003;
1009         case VIDEO_MODE_AUTO:
1010                 return 0x2003;
1011         default:
1012                 return 0x0003;
1013         }
1014 }
1015
1016 static int msp34xx_standard(int norm)
1017 {
1018         switch (norm) {
1019         case VIDEO_MODE_PAL:
1020                 return 1;
1021         case VIDEO_MODE_NTSC:  /* BTSC */
1022                 return 0x0020;
1023         case VIDEO_MODE_SECAM:
1024                 return 1;
1025         case VIDEO_MODE_RADIO:
1026                 return 0x0040;
1027         default:
1028                 return 1;
1029         }
1030 }
1031
1032 static int msp3410d_thread(void *data)
1033 {
1034         struct i2c_client *client = data;
1035         struct msp3400c *msp = i2c_get_clientdata(client);
1036         int mode,val,i,std;
1037
1038         printk("msp3410: daemon started\n");
1039         for (;;) {
1040                 d2printk(KERN_DEBUG "msp3410: thread: sleep\n");
1041                 msp34xx_sleep(msp,-1);
1042                 d2printk(KERN_DEBUG "msp3410: thread: wakeup\n");
1043
1044         restart:
1045                 dprintk("msp3410: thread: restart scan\n");
1046                 msp->restart = 0;
1047                 if (kthread_should_stop())
1048                         break;
1049
1050                 if (msp->mode == MSP_MODE_EXTERN) {
1051                         /* no carrier scan needed, just unmute */
1052                         dprintk(KERN_DEBUG "msp3410: thread: no carrier scan\n");
1053                         msp3400c_setvolume(client, msp->muted,
1054                                            msp->volume, msp->balance);
1055                         continue;
1056                 }
1057
1058                 /* put into sane state (and mute) */
1059                 msp3400c_reset(client);
1060
1061                 /* some time for the tuner to sync */
1062                 if (msp34xx_sleep(msp,200))
1063                         goto restart;
1064
1065                 /* start autodetect */
1066                 mode = msp34xx_modus(msp->norm);
1067                 std  = msp34xx_standard(msp->norm);
1068                 msp3400c_write(client, I2C_MSP3400C_DEM, 0x30, mode);
1069                 msp3400c_write(client, I2C_MSP3400C_DEM, 0x20, std);
1070                 msp->watch_stereo = 0;
1071
1072                 if (debug)
1073                         printk(KERN_DEBUG "msp3410: setting mode: %s (0x%04x)\n",
1074                                msp34xx_standard_mode_name(std) ,std);
1075
1076                 if (std != 1) {
1077                         /* programmed some specific mode */
1078                         val = std;
1079                 } else {
1080                         /* triggered autodetect */
1081                         for (;;) {
1082                                 if (msp34xx_sleep(msp,100))
1083                                         goto restart;
1084
1085                                 /* check results */
1086                                 val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x7e);
1087                                 if (val < 0x07ff)
1088                                         break;
1089                                 dprintk(KERN_DEBUG "msp3410: detection still in progress\n");
1090                         }
1091                 }
1092                 for (i = 0; modelist[i].name != NULL; i++)
1093                         if (modelist[i].retval == val)
1094                                 break;
1095                 dprintk(KERN_DEBUG "msp3410: current mode: %s (0x%04x)\n",
1096                         modelist[i].name ? modelist[i].name : "unknown",
1097                         val);
1098                 msp->main   = modelist[i].main;
1099                 msp->second = modelist[i].second;
1100
1101                 if (amsound && (msp->norm == VIDEO_MODE_SECAM) && (val != 0x0009)) {
1102                         /* autodetection has failed, let backup */
1103                         dprintk(KERN_DEBUG "msp3410: autodetection failed,"
1104                                 " switching to backup mode: %s (0x%04x)\n",
1105                                 modelist[8].name ? modelist[8].name : "unknown",val);
1106                         val = 0x0009;
1107                         msp3400c_write(client, I2C_MSP3400C_DEM, 0x20, val);
1108                 }
1109
1110                 /* set various prescales */
1111                 msp3400c_write(client, I2C_MSP3400C_DFP, 0x0d, 0x1900); /* scart */
1112                 msp3400c_write(client, I2C_MSP3400C_DFP, 0x0e, 0x2403); /* FM */
1113                 msp3400c_write(client, I2C_MSP3400C_DFP, 0x10, 0x5a00); /* nicam */
1114
1115                 /* set stereo */
1116                 switch (val) {
1117                 case 0x0008: /* B/G NICAM */
1118                 case 0x000a: /* I NICAM */
1119                         if (val == 0x0008)
1120                                 msp->mode = MSP_MODE_FM_NICAM1;
1121                         else
1122                                 msp->mode = MSP_MODE_FM_NICAM2;
1123                         /* just turn on stereo */
1124                         msp->rxsubchans = V4L2_TUNER_SUB_STEREO;
1125                         msp->nicam_on = 1;
1126                         msp->watch_stereo = 1;
1127                         msp3400c_set_audmode(client,V4L2_TUNER_MODE_STEREO);
1128                         break;
1129                 case 0x0009:
1130                         msp->mode = MSP_MODE_AM_NICAM;
1131                         msp->rxsubchans = V4L2_TUNER_SUB_MONO;
1132                         msp->nicam_on = 1;
1133                         msp3400c_set_audmode(client,V4L2_TUNER_MODE_MONO);
1134                         msp->watch_stereo = 1;
1135                         break;
1136                 case 0x0020: /* BTSC */
1137                         /* just turn on stereo */
1138                         msp->mode = MSP_MODE_BTSC;
1139                         msp->rxsubchans = V4L2_TUNER_SUB_STEREO;
1140                         msp->nicam_on = 0;
1141                         msp->watch_stereo = 1;
1142                         msp3400c_set_audmode(client,V4L2_TUNER_MODE_STEREO);
1143                         break;
1144                 case 0x0040: /* FM radio */
1145                         msp->mode   = MSP_MODE_FM_RADIO;
1146                         msp->rxsubchans = V4L2_TUNER_SUB_STEREO;
1147                         msp->audmode = V4L2_TUNER_MODE_STEREO;
1148                         msp->nicam_on = 0;
1149                         msp->watch_stereo = 0;
1150                         /* not needed in theory if HAVE_RADIO(), but
1151                            short programming enables carrier mute */
1152                         msp3400c_setmode(client,MSP_MODE_FM_RADIO);
1153                         msp3400c_setcarrier(client, MSP_CARRIER(10.7),
1154                                             MSP_CARRIER(10.7));
1155                         /* scart routing */
1156                         msp3400c_set_scart(client,SCART_IN2,0);
1157 #if 0
1158                         /* radio from SCART_IN2 */
1159                         msp3400c_write(client,I2C_MSP3400C_DFP, 0x08, 0x0220);
1160                         msp3400c_write(client,I2C_MSP3400C_DFP, 0x09, 0x0220);
1161                         msp3400c_write(client,I2C_MSP3400C_DFP, 0x0b, 0x0220);
1162 #else
1163                         /* msp34xx does radio decoding */
1164                         msp3400c_write(client,I2C_MSP3400C_DFP, 0x08, 0x0020);
1165                         msp3400c_write(client,I2C_MSP3400C_DFP, 0x09, 0x0020);
1166                         msp3400c_write(client,I2C_MSP3400C_DFP, 0x0b, 0x0020);
1167 #endif
1168                         break;
1169                 case 0x0003:
1170                 case 0x0004:
1171                 case 0x0005:
1172                         msp->mode   = MSP_MODE_FM_TERRA;
1173                         msp->rxsubchans = V4L2_TUNER_SUB_MONO;
1174                         msp->audmode = V4L2_TUNER_MODE_MONO;
1175                         msp->nicam_on = 0;
1176                         msp->watch_stereo = 1;
1177                         break;
1178                 }
1179
1180                 /* unmute, restore misc registers */
1181                 msp3400c_setbass(client, msp->bass);
1182                 msp3400c_settreble(client, msp->treble);
1183                 msp3400c_setvolume(client, msp->muted,
1184                                     msp->volume, msp->balance);
1185                 msp3400c_write(client, I2C_MSP3400C_DFP, 0x0013, msp->acb);
1186
1187                 /* monitor tv audio mode */
1188                 while (msp->watch_stereo) {
1189                         if (msp34xx_sleep(msp,5000))
1190                                 goto restart;
1191                         watch_stereo(client);
1192                 }
1193         }
1194         dprintk(KERN_DEBUG "msp3410: thread: exit\n");
1195         return 0;
1196 }
1197
1198 /* ----------------------------------------------------------------------- */
1199 /* msp34xxG + (simpler no-thread)                                          */
1200 /* this one uses both automatic standard detection and automatic sound     */
1201 /* select which are available in the newer G versions                      */
1202 /* struct msp: only norm, acb and source are really used in this mode      */
1203
1204 static void msp34xxg_set_source(struct i2c_client *client, int source);
1205
1206 /* (re-)initialize the msp34xxg, according to the current norm in msp->norm
1207  * return 0 if it worked, -1 if it failed
1208  */
1209 static int msp34xxg_init(struct i2c_client *client)
1210 {
1211         struct msp3400c *msp = i2c_get_clientdata(client);
1212         int modus,std;
1213
1214         if (msp3400c_reset(client))
1215                 return -1;
1216
1217         /* make sure that input/output is muted (paranoid mode) */
1218         if (msp3400c_write(client,
1219                            I2C_MSP3400C_DFP,
1220                            0x13, /* ACB */
1221                            0x0f20 /* mute DSP input, mute SCART 1 */))
1222                 return -1;
1223
1224         /* step-by-step initialisation, as described in the manual */
1225         modus = msp34xx_modus(msp->norm);
1226         std   = msp34xx_standard(msp->norm);
1227         modus &= ~0x03; /* STATUS_CHANGE=0 */
1228         modus |= 0x01;  /* AUTOMATIC_SOUND_DETECTION=1 */
1229         if (msp3400c_write(client,
1230                            I2C_MSP3400C_DEM,
1231                            0x30/*MODUS*/,
1232                            modus))
1233                 return -1;
1234         if (msp3400c_write(client,
1235                            I2C_MSP3400C_DEM,
1236                            0x20/*stanard*/,
1237                            std))
1238                 return -1;
1239
1240         /* write the dfps that may have an influence on
1241            standard/audio autodetection right now */
1242         msp34xxg_set_source(client, msp->source);
1243
1244         if (msp3400c_write(client, I2C_MSP3400C_DFP,
1245                            0x0e, /* AM/FM Prescale */
1246                            0x3000 /* default: [15:8] 75khz deviation */))
1247                 return -1;
1248
1249         if (msp3400c_write(client, I2C_MSP3400C_DFP,
1250                            0x10, /* NICAM Prescale */
1251                            0x5a00 /* default: 9db gain (as recommended) */))
1252                 return -1;
1253
1254         if (msp3400c_write(client,
1255                            I2C_MSP3400C_DEM,
1256                            0x20, /* STANDARD SELECT  */
1257                            standard /* default: 0x01 for automatic standard select*/))
1258                 return -1;
1259         return 0;
1260 }
1261
1262 static int msp34xxg_thread(void *data)
1263 {
1264         struct i2c_client *client = data;
1265         struct msp3400c *msp = i2c_get_clientdata(client);
1266         int val, std, i;
1267
1268         printk("msp34xxg: daemon started\n");
1269         msp->source = 1; /* default */
1270         for (;;) {
1271                 d2printk(KERN_DEBUG "msp34xxg: thread: sleep\n");
1272                 msp34xx_sleep(msp,-1);
1273                 d2printk(KERN_DEBUG "msp34xxg: thread: wakeup\n");
1274
1275         restart:
1276                 dprintk("msp34xxg: thread: restart scan\n");
1277                 msp->restart = 0;
1278                 if (kthread_should_stop())
1279                         break;
1280
1281                 /* setup the chip*/
1282                 msp34xxg_init(client);
1283                 std = standard;
1284                 if (std != 0x01)
1285                         goto unmute;
1286
1287                 /* watch autodetect */
1288                 dprintk("msp34xxg: triggered autodetect, waiting for result\n");
1289                 for (i = 0; i < 10; i++) {
1290                         if (msp34xx_sleep(msp,100))
1291                                 goto restart;
1292
1293                         /* check results */
1294                         val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x7e);
1295                         if (val < 0x07ff) {
1296                                 std = val;
1297                                 break;
1298                         }
1299                         dprintk("msp34xxg: detection still in progress\n");
1300                 }
1301                 if (0x01 == std) {
1302                         dprintk("msp34xxg: detection still in progress after 10 tries. giving up.\n");
1303                         continue;
1304                 }
1305
1306         unmute:
1307                 dprintk("msp34xxg: current mode: %s (0x%04x)\n",
1308                         msp34xx_standard_mode_name(std), std);
1309
1310                 /* unmute: dispatch sound to scart output, set scart volume */
1311                 dprintk("msp34xxg: unmute\n");
1312
1313                 msp3400c_setbass(client, msp->bass);
1314                 msp3400c_settreble(client, msp->treble);
1315                 msp3400c_setvolume(client, msp->muted, msp->volume, msp->balance);
1316
1317                 /* restore ACB */
1318                 if (msp3400c_write(client,
1319                                    I2C_MSP3400C_DFP,
1320                                    0x13, /* ACB */
1321                                    msp->acb))
1322                         return -1;
1323         }
1324         dprintk(KERN_DEBUG "msp34xxg: thread: exit\n");
1325         return 0;
1326 }
1327
1328 /* set the same 'source' for the loudspeaker, scart and quasi-peak detector
1329  * the value for source is the same as bit 15:8 of DFP registers 0x08,
1330  * 0x0a and 0x0c: 0=mono, 1=stereo or A|B, 2=SCART, 3=stereo or A, 4=stereo or B
1331  *
1332  * this function replaces msp3400c_setstereo
1333  */
1334 static void msp34xxg_set_source(struct i2c_client *client, int source)
1335 {
1336         struct msp3400c *msp = i2c_get_clientdata(client);
1337
1338         /* fix matrix mode to stereo and let the msp choose what
1339          * to output according to 'source', as recommended
1340          * for MONO (source==0) downmixing set bit[7:0] to 0x30
1341          */
1342         int value = (source&0x07)<<8|(source==0 ? 0x30:0x20);
1343         dprintk("msp34xxg: set source to %d (0x%x)\n", source, value);
1344         msp3400c_write(client,
1345                        I2C_MSP3400C_DFP,
1346                        0x08, /* Loudspeaker Output */
1347                        value);
1348         msp3400c_write(client,
1349                        I2C_MSP3400C_DFP,
1350                        0x0a, /* SCART1 DA Output */
1351                        value);
1352         msp3400c_write(client,
1353                        I2C_MSP3400C_DFP,
1354                        0x0c, /* Quasi-peak detector */
1355                        value);
1356         /*
1357          * set identification threshold. Personally, I
1358          * I set it to a higher value that the default
1359          * of 0x190 to ignore noisy stereo signals.
1360          * this needs tuning. (recommended range 0x00a0-0x03c0)
1361          * 0x7f0 = forced mono mode
1362          */
1363         msp3400c_write(client,
1364                        I2C_MSP3400C_DEM,
1365                        0x22, /* a2 threshold for stereo/bilingual */
1366                        stereo_threshold);
1367         msp->source=source;
1368 }
1369
1370 static void msp34xxg_detect_stereo(struct i2c_client *client)
1371 {
1372         struct msp3400c *msp = i2c_get_clientdata(client);
1373
1374         int status = msp3400c_read(client,
1375                                    I2C_MSP3400C_DEM,
1376                                    0x0200 /* STATUS */);
1377         int is_bilingual = status&0x100;
1378         int is_stereo = status&0x40;
1379
1380         msp->rxsubchans = 0;
1381         if (is_stereo)
1382                 msp->rxsubchans |= V4L2_TUNER_SUB_STEREO;
1383         else
1384                 msp->rxsubchans |= V4L2_TUNER_SUB_MONO;
1385         if (is_bilingual) {
1386                 msp->rxsubchans |= V4L2_TUNER_SUB_LANG1|V4L2_TUNER_SUB_LANG2;
1387                 /* I'm supposed to check whether it's SAP or not
1388                  * and set only LANG2/SAP in this case. Yet, the MSP
1389                  * does a lot of work to hide this and handle everything
1390                  * the same way. I don't want to work around it so unless
1391                  * this is a problem, I'll handle SAP just like lang1/lang2.
1392                  */
1393         }
1394         dprintk("msp34xxg: status=0x%x, stereo=%d, bilingual=%d -> rxsubchans=%d\n",
1395                 status, is_stereo, is_bilingual, msp->rxsubchans);
1396 }
1397
1398 static void msp34xxg_set_audmode(struct i2c_client *client, int audmode)
1399 {
1400         struct msp3400c *msp = i2c_get_clientdata(client);
1401         int source;
1402
1403         switch (audmode) {
1404         case V4L2_TUNER_MODE_MONO:
1405                 source=0; /* mono only */
1406                 break;
1407         case V4L2_TUNER_MODE_STEREO:
1408                 source=1; /* stereo or A|B, see comment in msp34xxg_get_v4l2_stereo() */
1409                 /* problem: that could also mean 2 (scart input) */
1410                 break;
1411         case V4L2_TUNER_MODE_LANG1:
1412                 source=3; /* stereo or A */
1413                 break;
1414         case V4L2_TUNER_MODE_LANG2:
1415                 source=4; /* stereo or B */
1416                 break;
1417         default:
1418                 audmode = 0;
1419                 source  = 1;
1420                 break;
1421         }
1422         msp->audmode = audmode;
1423         msp34xxg_set_source(client, source);
1424 }
1425
1426
1427 /* ----------------------------------------------------------------------- */
1428
1429 static int msp_attach(struct i2c_adapter *adap, int addr, int kind);
1430 static int msp_detach(struct i2c_client *client);
1431 static int msp_probe(struct i2c_adapter *adap);
1432 static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg);
1433
1434 static int msp_suspend(struct device * dev, u32 state, u32 level);
1435 static int msp_resume(struct device * dev, u32 level);
1436
1437 static void msp_wake_thread(struct i2c_client *client);
1438
1439 static struct i2c_driver driver = {
1440         .owner          = THIS_MODULE,
1441         .name           = "i2c msp3400 driver",
1442         .id             = I2C_DRIVERID_MSP3400,
1443         .flags          = I2C_DF_NOTIFY,
1444         .attach_adapter = msp_probe,
1445         .detach_client  = msp_detach,
1446         .command        = msp_command,
1447         .driver = {
1448                 .suspend = msp_suspend,
1449                 .resume  = msp_resume,
1450         },
1451 };
1452
1453 static struct i2c_client client_template =
1454 {
1455         I2C_DEVNAME("(unset)"),
1456         .flags     = I2C_CLIENT_ALLOW_USE,
1457         .driver    = &driver,
1458 };
1459
1460 static int msp_attach(struct i2c_adapter *adap, int addr, int kind)
1461 {
1462         struct msp3400c *msp;
1463         struct i2c_client *c;
1464         int (*thread_func)(void *data) = NULL;
1465
1466         client_template.adapter = adap;
1467         client_template.addr = addr;
1468
1469         if (-1 == msp3400c_reset(&client_template)) {
1470                 dprintk("msp3400: no chip found\n");
1471                 return -1;
1472         }
1473
1474         if (NULL == (c = kmalloc(sizeof(struct i2c_client),GFP_KERNEL)))
1475                 return -ENOMEM;
1476         memcpy(c,&client_template,sizeof(struct i2c_client));
1477         if (NULL == (msp = kmalloc(sizeof(struct msp3400c),GFP_KERNEL))) {
1478                 kfree(c);
1479                 return -ENOMEM;
1480         }
1481
1482         memset(msp,0,sizeof(struct msp3400c));
1483         msp->volume  = 58880;  /* 0db gain */
1484         msp->balance = 32768;
1485         msp->bass    = 32768;
1486         msp->treble  = 32768;
1487         msp->input   = -1;
1488         msp->muted   = 1;
1489
1490         i2c_set_clientdata(c, msp);
1491         init_waitqueue_head(&msp->wq);
1492
1493         if (-1 == msp3400c_reset(c)) {
1494                 kfree(msp);
1495                 kfree(c);
1496                 dprintk("msp3400: no chip found\n");
1497                 return -1;
1498         }
1499
1500         msp->rev1 = msp3400c_read(c, I2C_MSP3400C_DFP, 0x1e);
1501         if (-1 != msp->rev1)
1502                 msp->rev2 = msp3400c_read(c, I2C_MSP3400C_DFP, 0x1f);
1503         if ((-1 == msp->rev1) || (0 == msp->rev1 && 0 == msp->rev2)) {
1504                 kfree(msp);
1505                 kfree(c);
1506                 printk("msp3400: error while reading chip version\n");
1507                 return -1;
1508         }
1509
1510 #if 0
1511         /* this will turn on a 1kHz beep - might be useful for debugging... */
1512         msp3400c_write(c,I2C_MSP3400C_DFP, 0x0014, 0x1040);
1513 #endif
1514         msp3400c_setvolume(c, msp->muted, msp->volume, msp->balance);
1515
1516         snprintf(c->name, sizeof(c->name), "MSP34%02d%c-%c%d",
1517                  (msp->rev2>>8)&0xff, (msp->rev1&0xff)+'@',
1518                  ((msp->rev1>>8)&0xff)+'@', msp->rev2&0x1f);
1519
1520         msp->opmode = opmode;
1521         if (OPMODE_AUTO == msp->opmode) {
1522                 if (HAVE_SIMPLER(msp))
1523                         msp->opmode = OPMODE_SIMPLER;
1524                 else if (HAVE_SIMPLE(msp))
1525                         msp->opmode = OPMODE_SIMPLE;
1526                 else
1527                         msp->opmode = OPMODE_MANUAL;
1528         }
1529
1530         /* hello world :-) */
1531         printk(KERN_INFO "msp34xx: init: chip=%s",i2c_clientname(c));
1532         if (HAVE_NICAM(msp))
1533                 printk(" +nicam");
1534         if (HAVE_SIMPLE(msp))
1535                 printk(" +simple");
1536         if (HAVE_SIMPLER(msp))
1537                 printk(" +simpler");
1538         if (HAVE_RADIO(msp))
1539                 printk(" +radio");
1540
1541         /* version-specific initialization */
1542         switch (msp->opmode) {
1543         case OPMODE_MANUAL:
1544                 printk(" mode=manual");
1545                 thread_func = msp3400c_thread;
1546                 break;
1547         case OPMODE_SIMPLE:
1548                 printk(" mode=simple");
1549                 thread_func = msp3410d_thread;
1550                 break;
1551         case OPMODE_SIMPLER:
1552                 printk(" mode=simpler");
1553                 thread_func = msp34xxg_thread;
1554                 break;
1555         }
1556         printk("\n");
1557
1558         /* startup control thread if needed */
1559         if (thread_func) {
1560                 msp->kthread = kthread_run(thread_func, c, "msp34xx");
1561                 if (NULL == msp->kthread)
1562                         printk(KERN_WARNING "msp34xx: kernel_thread() failed\n");
1563                 msp_wake_thread(c);
1564         }
1565
1566         /* done */
1567         i2c_attach_client(c);
1568         return 0;
1569 }
1570
1571 static int msp_detach(struct i2c_client *client)
1572 {
1573         struct msp3400c *msp  = i2c_get_clientdata(client);
1574
1575         /* shutdown control thread */
1576         if (msp->kthread >= 0) {
1577                 msp->restart = 1;
1578                 kthread_stop(msp->kthread);
1579         }
1580         msp3400c_reset(client);
1581
1582         i2c_detach_client(client);
1583         kfree(msp);
1584         kfree(client);
1585         return 0;
1586 }
1587
1588 static int msp_probe(struct i2c_adapter *adap)
1589 {
1590         if (adap->class & I2C_CLASS_TV_ANALOG)
1591                 return i2c_probe(adap, &addr_data, msp_attach);
1592         return 0;
1593 }
1594
1595 static void msp_wake_thread(struct i2c_client *client)
1596 {
1597         struct msp3400c *msp  = i2c_get_clientdata(client);
1598
1599         if (NULL == msp->kthread)
1600                 return;
1601         msp3400c_setvolume(client,msp->muted,0,0);
1602         msp->watch_stereo = 0;
1603         msp->restart = 1;
1604         wake_up_interruptible(&msp->wq);
1605 }
1606
1607 /* ----------------------------------------------------------------------- */
1608
1609 static int mode_v4l2_to_v4l1(int rxsubchans)
1610 {
1611         int mode = 0;
1612
1613         if (rxsubchans & V4L2_TUNER_SUB_STEREO)
1614                 mode |= VIDEO_SOUND_STEREO;
1615         if (rxsubchans & V4L2_TUNER_SUB_LANG2)
1616                 mode |= VIDEO_SOUND_LANG2;
1617         if (rxsubchans & V4L2_TUNER_SUB_LANG1)
1618                 mode |= VIDEO_SOUND_LANG1;
1619         if (0 == mode)
1620                 mode |= VIDEO_SOUND_MONO;
1621         return mode;
1622 }
1623
1624 static int mode_v4l1_to_v4l2(int mode)
1625 {
1626         if (mode & VIDEO_SOUND_STEREO)
1627                 return V4L2_TUNER_MODE_STEREO;
1628         if (mode & VIDEO_SOUND_LANG2)
1629                 return V4L2_TUNER_MODE_LANG2;
1630         if (mode & VIDEO_SOUND_LANG1)
1631                 return V4L2_TUNER_MODE_LANG1;
1632         return V4L2_TUNER_MODE_MONO;
1633 }
1634
1635 static void msp_any_detect_stereo(struct i2c_client *client)
1636 {
1637         struct msp3400c *msp  = i2c_get_clientdata(client);
1638
1639         switch (msp->opmode) {
1640         case OPMODE_MANUAL:
1641         case OPMODE_SIMPLE:
1642                 autodetect_stereo(client);
1643                 break;
1644         case OPMODE_SIMPLER:
1645                 msp34xxg_detect_stereo(client);
1646                 break;
1647         }
1648 }
1649
1650 static void msp_any_set_audmode(struct i2c_client *client, int audmode)
1651 {
1652         struct msp3400c *msp  = i2c_get_clientdata(client);
1653
1654         switch (msp->opmode) {
1655         case OPMODE_MANUAL:
1656         case OPMODE_SIMPLE:
1657                 msp->watch_stereo = 0;
1658                 msp3400c_set_audmode(client, audmode);
1659                 break;
1660         case OPMODE_SIMPLER:
1661                 msp34xxg_set_audmode(client, audmode);
1662                 break;
1663         }
1664 }
1665
1666 static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg)
1667 {
1668         struct msp3400c *msp  = i2c_get_clientdata(client);
1669         __u16           *sarg = arg;
1670         int scart = 0;
1671
1672         switch (cmd) {
1673
1674         case AUDC_SET_INPUT:
1675                 dprintk(KERN_DEBUG "msp34xx: AUDC_SET_INPUT(%d)\n",*sarg);
1676                 if (*sarg == msp->input)
1677                         break;
1678                 msp->input = *sarg;
1679                 switch (*sarg) {
1680                 case AUDIO_RADIO:
1681                         /* Hauppauge uses IN2 for the radio */
1682                         msp->mode   = MSP_MODE_FM_RADIO;
1683                         scart       = SCART_IN2;
1684                         break;
1685                 case AUDIO_EXTERN_1:
1686                         /* IN1 is often used for external input ... */
1687                         msp->mode   = MSP_MODE_EXTERN;
1688                         scart       = SCART_IN1;
1689                         break;
1690                 case AUDIO_EXTERN_2:
1691                         /* ... sometimes it is IN2 through ;) */
1692                         msp->mode   = MSP_MODE_EXTERN;
1693                         scart       = SCART_IN2;
1694                         break;
1695                 case AUDIO_TUNER:
1696                         msp->mode   = -1;
1697                         break;
1698                 default:
1699                         if (*sarg & AUDIO_MUTE)
1700                                 msp3400c_set_scart(client,SCART_MUTE,0);
1701                         break;
1702                 }
1703                 if (scart) {
1704                         msp->rxsubchans = V4L2_TUNER_SUB_STEREO;
1705                         msp->audmode = V4L2_TUNER_MODE_STEREO;
1706                         msp3400c_set_scart(client,scart,0);
1707                         msp3400c_write(client,I2C_MSP3400C_DFP,0x000d,0x1900);
1708                         if (msp->opmode != OPMODE_SIMPLER)
1709                                 msp3400c_set_audmode(client, msp->audmode);
1710                 }
1711                 msp_wake_thread(client);
1712                 break;
1713
1714         case AUDC_SET_RADIO:
1715                 dprintk(KERN_DEBUG "msp34xx: AUDC_SET_RADIO\n");
1716                 msp->norm = VIDEO_MODE_RADIO;
1717                 dprintk(KERN_DEBUG "msp34xx: switching to radio mode\n");
1718                 msp->watch_stereo = 0;
1719                 switch (msp->opmode) {
1720                 case OPMODE_MANUAL:
1721                         /* set msp3400 to FM radio mode */
1722                         msp3400c_setmode(client,MSP_MODE_FM_RADIO);
1723                         msp3400c_setcarrier(client, MSP_CARRIER(10.7),
1724                                             MSP_CARRIER(10.7));
1725                         msp3400c_setvolume(client, msp->muted,
1726                                            msp->volume, msp->balance);
1727                         break;
1728                 case OPMODE_SIMPLE:
1729                 case OPMODE_SIMPLER:
1730                         /* the thread will do for us */
1731                         msp_wake_thread(client);
1732                         break;
1733                 }
1734                 break;
1735
1736         /* --- v4l ioctls --- */
1737         /* take care: bttv does userspace copying, we'll get a
1738            kernel pointer here... */
1739         case VIDIOCGAUDIO:
1740         {
1741                 struct video_audio *va = arg;
1742
1743                 dprintk(KERN_DEBUG "msp34xx: VIDIOCGAUDIO\n");
1744                 va->flags |= VIDEO_AUDIO_VOLUME |
1745                         VIDEO_AUDIO_BASS |
1746                         VIDEO_AUDIO_TREBLE |
1747                         VIDEO_AUDIO_MUTABLE;
1748                 if (msp->muted)
1749                         va->flags |= VIDEO_AUDIO_MUTE;
1750
1751                 va->volume = msp->volume;
1752                 va->balance = (va->volume) ? msp->balance : 32768;
1753                 va->bass = msp->bass;
1754                 va->treble = msp->treble;
1755
1756                 msp_any_detect_stereo(client);
1757                 va->mode = mode_v4l2_to_v4l1(msp->rxsubchans);
1758                 break;
1759         }
1760         case VIDIOCSAUDIO:
1761         {
1762                 struct video_audio *va = arg;
1763
1764                 dprintk(KERN_DEBUG "msp34xx: VIDIOCSAUDIO\n");
1765                 msp->muted = (va->flags & VIDEO_AUDIO_MUTE);
1766                 msp->volume = va->volume;
1767                 msp->balance = va->balance;
1768                 msp->bass = va->bass;
1769                 msp->treble = va->treble;
1770
1771                 msp3400c_setvolume(client, msp->muted,
1772                                    msp->volume, msp->balance);
1773                 msp3400c_setbass(client,msp->bass);
1774                 msp3400c_settreble(client,msp->treble);
1775
1776                 if (va->mode != 0 && msp->norm != VIDEO_MODE_RADIO)
1777                         msp_any_set_audmode(client,mode_v4l1_to_v4l2(va->mode));
1778                 break;
1779         }
1780         case VIDIOCSCHAN:
1781         {
1782                 struct video_channel *vc = arg;
1783
1784                 dprintk(KERN_DEBUG "msp34xx: VIDIOCSCHAN (norm=%d)\n",vc->norm);
1785                 msp->norm = vc->norm;
1786                 msp_wake_thread(client);
1787                 break;
1788         }
1789
1790         case VIDIOCSFREQ:
1791         case VIDIOC_S_FREQUENCY:
1792         {
1793                 /* new channel -- kick audio carrier scan */
1794                 dprintk(KERN_DEBUG "msp34xx: VIDIOCSFREQ\n");
1795                 msp_wake_thread(client);
1796                 break;
1797         }
1798
1799         /* --- v4l2 ioctls --- */
1800         case VIDIOC_G_TUNER:
1801         {
1802                 struct v4l2_tuner *vt = arg;
1803
1804                 msp_any_detect_stereo(client);
1805                 vt->audmode    = msp->audmode;
1806                 vt->rxsubchans = msp->rxsubchans;
1807                 vt->capability = V4L2_TUNER_CAP_STEREO |
1808                         V4L2_TUNER_CAP_LANG1|
1809                         V4L2_TUNER_CAP_LANG2;
1810                 break;
1811         }
1812         case VIDIOC_S_TUNER:
1813         {
1814                 struct v4l2_tuner *vt=(struct v4l2_tuner *)arg;
1815
1816                 /* only set audmode */
1817                 if (vt->audmode != -1 && vt->audmode != 0)
1818                         msp_any_set_audmode(client, vt->audmode);
1819                 break;
1820         }
1821
1822         /* msp34xx specific */
1823         case MSP_SET_MATRIX:
1824         {
1825                 struct msp_matrix *mspm = arg;
1826
1827                 dprintk(KERN_DEBUG "msp34xx: MSP_SET_MATRIX\n");
1828                 msp3400c_set_scart(client, mspm->input, mspm->output);
1829                 break;
1830         }
1831
1832         default:
1833                 /* nothing */
1834                 break;
1835         }
1836         return 0;
1837 }
1838
1839 static int msp_suspend(struct device * dev, u32 state, u32 level)
1840 {
1841         struct i2c_client *c = container_of(dev, struct i2c_client, dev);
1842
1843         dprintk("msp34xx: suspend\n");
1844         msp3400c_reset(c);
1845         return 0;
1846 }
1847
1848 static int msp_resume(struct device * dev, u32 level)
1849 {
1850         struct i2c_client *c = container_of(dev, struct i2c_client, dev);
1851
1852         dprintk("msp34xx: resume\n");
1853         msp_wake_thread(c);
1854         return 0;
1855 }
1856
1857 /* ----------------------------------------------------------------------- */
1858
1859 static int __init msp3400_init_module(void)
1860 {
1861         return i2c_add_driver(&driver);
1862 }
1863
1864 static void __exit msp3400_cleanup_module(void)
1865 {
1866         i2c_del_driver(&driver);
1867 }
1868
1869 module_init(msp3400_init_module);
1870 module_exit(msp3400_cleanup_module);
1871
1872 /*
1873  * Overrides for Emacs so that we follow Linus's tabbing style.
1874  * ---------------------------------------------------------------------------
1875  * Local variables:
1876  * c-basic-offset: 8
1877  * End:
1878  */