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