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