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