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