2 * programming the msp34* sound processor family
4 * (c) 1997-2001 Gerd Knorr <kraxel@bytesex.org>
6 * what works and what doesn't:
9 * Support for Hauppauge cards added (decoding handled by tuner) added by
10 * Frederic Crozat <fcrozat@mail.dotcom.fr>
13 * should work. The stereo modes are backward compatible to FM-mono,
14 * therefore FM-Mono should be allways available.
16 * FM-Stereo (B/G, used in germany)
17 * should work, with autodetect
19 * FM-Stereo (satellite)
20 * should work, no autodetect (i.e. default is mono, but you can
21 * switch to stereo -- untested)
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>
29 * - better SAT support
32 * 980623 Thomas Sailer (sailer@ife.ee.ethz.ch)
33 * using soundcore instead of OSS
37 #include <linux/config.h>
38 #include <linux/module.h>
39 #include <linux/moduleparam.h>
40 #include <linux/kernel.h>
41 #include <linux/sched.h>
42 #include <linux/string.h>
43 #include <linux/timer.h>
44 #include <linux/delay.h>
45 #include <linux/errno.h>
46 #include <linux/slab.h>
47 #include <linux/i2c.h>
48 #include <linux/videodev.h>
49 #include <linux/init.h>
50 #include <linux/smp_lock.h>
51 #include <linux/kthread.h>
52 #include <linux/suspend.h>
53 #include <asm/semaphore.h>
54 #include <asm/pgtable.h>
56 #include <media/audiochip.h>
60 #define OPMODE_AUTO -1
61 #define OPMODE_MANUAL 0
62 #define OPMODE_SIMPLE 1 /* use short programming (>= msp3410 only) */
63 #define OPMODE_SIMPLER 2 /* use shorter programming (>= msp34xxG) */
65 /* insmod parameters */
66 static int opmode = OPMODE_AUTO;
67 static int debug = 0; /* debug output */
68 static int once = 0; /* no continous stereo monitoring */
69 static int amsound = 0; /* hard-wire AM sound at 6.5 Hz (france),
70 the autoscan seems work well only with FM... */
71 static int standard = 1; /* Override auto detect of audio standard, if needed. */
74 static int stereo_threshold = 0x190; /* a2 threshold for stereo/bilingual
75 (msp34xxg only) 0x00a0-0x03c0 */
85 int main, second; /* sound carrier */
87 int source; /* see msp34xxg_set_source */
98 struct task_struct *kthread;
104 #define HAVE_NICAM(msp) (((msp->rev2>>8) & 0xff) != 00)
105 #define HAVE_SIMPLE(msp) ((msp->rev1 & 0xff) >= 'D'-'@')
106 #define HAVE_SIMPLER(msp) ((msp->rev1 & 0xff) >= 'G'-'@')
107 #define HAVE_RADIO(msp) ((msp->rev1 & 0xff) >= 'G'-'@')
109 #define VIDEO_MODE_RADIO 16 /* norm magic for radio mode */
111 /* ---------------------------------------------------------------------- */
113 #define dprintk if (debug >= 1) printk
114 #define d2printk if (debug >= 2) printk
117 module_param(opmode, int, 0444);
120 module_param(once, int, 0644);
121 module_param(debug, int, 0644);
122 module_param(stereo_threshold, int, 0644);
123 module_param(standard, int, 0644);
124 module_param(amsound, int, 0644);
125 module_param(dolby, int, 0644);
127 MODULE_PARM_DESC(opmode, "Forces a MSP3400 opmode. 0=Manual, 1=Simple, 2=Simpler");
128 MODULE_PARM_DESC(once, "No continuous stereo monitoring");
129 MODULE_PARM_DESC(debug, "Enable debug messages");
130 MODULE_PARM_DESC(stereo_threshold, "Sets signal threshold to activate stereo");
131 MODULE_PARM_DESC(standard, "Specify audio standard: 32 = NTSC, 64 = radio, Default: Autodetect");
132 MODULE_PARM_DESC(amsound, "Hardwire AM sound at 6.5Hz (France), FM can autoscan");
133 MODULE_PARM_DESC(dolby, "Activates Dolby processsing");
136 MODULE_DESCRIPTION("device driver for msp34xx TV sound processor");
137 MODULE_AUTHOR("Gerd Knorr");
138 MODULE_LICENSE("Dual BSD/GPL"); /* FreeBSD uses this too */
140 /* ---------------------------------------------------------------------- */
142 #define I2C_MSP3400C 0x80
143 #define I2C_MSP3400C_ALT 0x88
145 #define I2C_MSP3400C_DEM 0x10
146 #define I2C_MSP3400C_DFP 0x12
148 /* Addresses to scan */
149 static unsigned short normal_i2c[] = {
151 I2C_MSP3400C_ALT >> 1,
156 /* ----------------------------------------------------------------------- */
157 /* functions for talking to the MSP3400C Sound processor */
159 static int msp3400c_reset(struct i2c_client *client)
161 /* reset and read revision code */
162 static char reset_off[3] = { 0x00, 0x80, 0x00 };
163 static char reset_on[3] = { 0x00, 0x00, 0x00 };
164 static char write[3] = { I2C_MSP3400C_DFP + 1, 0x00, 0x1e };
166 struct i2c_msg reset[2] = {
167 { client->addr, I2C_M_IGNORE_NAK, 3, reset_off },
168 { client->addr, I2C_M_IGNORE_NAK, 3, reset_on },
170 struct i2c_msg test[2] = {
171 { client->addr, 0, 3, write },
172 { client->addr, I2C_M_RD, 2, read },
175 if ( (1 != i2c_transfer(client->adapter,&reset[0],1)) ||
176 (1 != i2c_transfer(client->adapter,&reset[1],1)) ||
177 (2 != i2c_transfer(client->adapter,test,2)) ) {
178 printk(KERN_ERR "msp3400: chip reset failed\n");
185 msp3400c_read(struct i2c_client *client, int dev, int addr)
189 unsigned char write[3];
190 unsigned char read[2];
191 struct i2c_msg msgs[2] = {
192 { client->addr, 0, 3, write },
193 { client->addr, I2C_M_RD, 2, read }
196 write[1] = addr >> 8;
197 write[2] = addr & 0xff;
199 for (err = 0; err < 3;) {
200 if (2 == i2c_transfer(client->adapter,msgs,2))
203 printk(KERN_WARNING "msp34xx: I/O error #%d (read 0x%02x/0x%02x)\n",
208 printk(KERN_WARNING "msp34xx: giving up, reseting chip. Sound will go off, sorry folks :-|\n");
209 msp3400c_reset(client);
212 return read[0] << 8 | read[1];
216 msp3400c_write(struct i2c_client *client, int dev, int addr, int val)
219 unsigned char buffer[5];
222 buffer[1] = addr >> 8;
223 buffer[2] = addr & 0xff;
224 buffer[3] = val >> 8;
225 buffer[4] = val & 0xff;
227 for (err = 0; err < 3;) {
228 if (5 == i2c_master_send(client, buffer, 5))
231 printk(KERN_WARNING "msp34xx: I/O error #%d (write 0x%02x/0x%02x)\n",
236 printk(KERN_WARNING "msp34xx: giving up, reseting chip. Sound will go off, sorry folks :-|\n");
237 msp3400c_reset(client);
243 /* ------------------------------------------------------------------------ */
245 /* This macro is allowed for *constants* only, gcc must calculate it
246 at compile time. Remember -- no floats in kernel mode */
247 #define MSP_CARRIER(freq) ((int)((float)(freq/18.432)*(1<<24)))
249 #define MSP_MODE_AM_DETECT 0
250 #define MSP_MODE_FM_RADIO 2
251 #define MSP_MODE_FM_TERRA 3
252 #define MSP_MODE_FM_SAT 4
253 #define MSP_MODE_FM_NICAM1 5
254 #define MSP_MODE_FM_NICAM2 6
255 #define MSP_MODE_AM_NICAM 7
256 #define MSP_MODE_BTSC 8
257 #define MSP_MODE_EXTERN 9
259 static struct MSP_INIT_DATA_DEM {
268 } msp_init_data[] = {
269 /* AM (for carrier detect / msp3400) */
270 { { 75, 19, 36, 35, 39, 40 }, { 75, 19, 36, 35, 39, 40 },
271 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
272 0x00d0, 0x0500, 0x0020, 0x3000},
274 /* AM (for carrier detect / msp3410) */
275 { { -1, -1, -8, 2, 59, 126 }, { -1, -1, -8, 2, 59, 126 },
276 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
277 0x00d0, 0x0100, 0x0020, 0x3000},
280 { { -8, -8, 4, 6, 78, 107 }, { -8, -8, 4, 6, 78, 107 },
281 MSP_CARRIER(10.7), MSP_CARRIER(10.7),
282 0x00d0, 0x0480, 0x0020, 0x3000 },
284 /* Terrestial FM-mono + FM-stereo */
285 { { 3, 18, 27, 48, 66, 72 }, { 3, 18, 27, 48, 66, 72 },
286 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
287 0x00d0, 0x0480, 0x0030, 0x3000},
290 { { 1, 9, 14, 24, 33, 37 }, { 3, 18, 27, 48, 66, 72 },
291 MSP_CARRIER(6.5), MSP_CARRIER(6.5),
292 0x00c6, 0x0480, 0x0000, 0x3000},
294 /* NICAM/FM -- B/G (5.5/5.85), D/K (6.5/5.85) */
295 { { -2, -8, -10, 10, 50, 86 }, { 3, 18, 27, 48, 66, 72 },
296 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
297 0x00d0, 0x0040, 0x0120, 0x3000},
299 /* NICAM/FM -- I (6.0/6.552) */
300 { { 2, 4, -6, -4, 40, 94 }, { 3, 18, 27, 48, 66, 72 },
301 MSP_CARRIER(6.0), MSP_CARRIER(6.0),
302 0x00d0, 0x0040, 0x0120, 0x3000},
304 /* NICAM/AM -- L (6.5/5.85) */
305 { { -2, -8, -10, 10, 50, 86 }, { -4, -12, -9, 23, 79, 126 },
306 MSP_CARRIER(6.5), MSP_CARRIER(6.5),
307 0x00c6, 0x0140, 0x0120, 0x7c03},
310 struct CARRIER_DETECT {
315 static struct CARRIER_DETECT carrier_detect_main[] = {
317 { MSP_CARRIER(4.5), "4.5 NTSC" },
318 { MSP_CARRIER(5.5), "5.5 PAL B/G" },
319 { MSP_CARRIER(6.0), "6.0 PAL I" },
320 { MSP_CARRIER(6.5), "6.5 PAL D/K + SAT + SECAM" }
323 static struct CARRIER_DETECT carrier_detect_55[] = {
325 { MSP_CARRIER(5.7421875), "5.742 PAL B/G FM-stereo" },
326 { MSP_CARRIER(5.85), "5.85 PAL B/G NICAM" }
329 static struct CARRIER_DETECT carrier_detect_65[] = {
330 /* PAL SAT / SECAM */
331 { MSP_CARRIER(5.85), "5.85 PAL D/K + SECAM NICAM" },
332 { MSP_CARRIER(6.2578125), "6.25 PAL D/K1 FM-stereo" },
333 { MSP_CARRIER(6.7421875), "6.74 PAL D/K2 FM-stereo" },
334 { MSP_CARRIER(7.02), "7.02 PAL SAT FM-stereo s/b" },
335 { MSP_CARRIER(7.20), "7.20 PAL SAT FM-stereo s" },
336 { MSP_CARRIER(7.38), "7.38 PAL SAT FM-stereo b" },
339 #define CARRIER_COUNT(x) (sizeof(x)/sizeof(struct CARRIER_DETECT))
341 /* ----------------------------------------------------------------------- */
343 static int scarts[3][9] = {
344 /* MASK IN1 IN2 IN1_DA IN2_DA IN3 IN4 MONO MUTE */
345 { 0x0320, 0x0000, 0x0200, -1, -1, 0x0300, 0x0020, 0x0100, 0x0320 },
346 { 0x0c40, 0x0440, 0x0400, 0x0c00, 0x0040, 0x0000, 0x0840, 0x0800, 0x0c40 },
347 { 0x3080, 0x1000, 0x1080, 0x0000, 0x0080, 0x2080, 0x3080, 0x2000, 0x3000 },
350 static char *scart_names[] = {
351 "mask", "in1", "in2", "in1 da", "in2 da", "in3", "in4", "mono", "mute"
355 msp3400c_set_scart(struct i2c_client *client, int in, int out)
357 struct msp3400c *msp = i2c_get_clientdata(client);
359 if (-1 == scarts[out][in])
363 "msp34xx: scart switch: %s => %d\n",scart_names[in],out);
364 msp->acb &= ~scarts[out][SCART_MASK];
365 msp->acb |= scarts[out][in];
366 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0013, msp->acb);
369 /* ------------------------------------------------------------------------ */
371 static void msp3400c_setcarrier(struct i2c_client *client, int cdo1, int cdo2)
373 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0093, cdo1 & 0xfff);
374 msp3400c_write(client,I2C_MSP3400C_DEM, 0x009b, cdo1 >> 12);
375 msp3400c_write(client,I2C_MSP3400C_DEM, 0x00a3, cdo2 & 0xfff);
376 msp3400c_write(client,I2C_MSP3400C_DEM, 0x00ab, cdo2 >> 12);
377 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0056, 0); /*LOAD_REG_1/2*/
380 static void msp3400c_setvolume(struct i2c_client *client,
381 int muted, int volume, int balance)
383 int val = 0, bal = 0;
386 /* 0x7f instead if 0x73 here has sound quality issues,
387 * probably due to overmodulation + clipping ... */
388 val = (volume * 0x73 / 65535) << 8;
391 bal = (balance / 256) - 128;
394 "msp34xx: setvolume: mute=%s %d:%d v=0x%02x b=0x%02x\n",
395 muted ? "on" : "off", volume, balance, val>>8, bal);
396 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0000, val); /* loudspeaker */
397 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0006, val); /* headphones */
398 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0007,
399 muted ? 0x01 : (val | 0x01));
400 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0001, bal << 8);
403 static void msp3400c_setbass(struct i2c_client *client, int bass)
405 int val = ((bass-32768) * 0x60 / 65535) << 8;
407 dprintk(KERN_DEBUG "msp34xx: setbass: %d 0x%02x\n",bass, val>>8);
408 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0002, val); /* loudspeaker */
411 static void msp3400c_settreble(struct i2c_client *client, int treble)
413 int val = ((treble-32768) * 0x60 / 65535) << 8;
415 dprintk(KERN_DEBUG "msp34xx: settreble: %d 0x%02x\n",treble, val>>8);
416 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0003, val); /* loudspeaker */
419 static void msp3400c_setmode(struct i2c_client *client, int type)
421 struct msp3400c *msp = i2c_get_clientdata(client);
424 dprintk(KERN_DEBUG "msp3400: setmode: %d\n",type);
426 msp->audmode = V4L2_TUNER_MODE_MONO;
427 msp->rxsubchans = V4L2_TUNER_SUB_MONO;
429 msp3400c_write(client,I2C_MSP3400C_DEM, 0x00bb, /* ad_cv */
430 msp_init_data[type].ad_cv);
432 for (i = 5; i >= 0; i--) /* fir 1 */
433 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0001,
434 msp_init_data[type].fir1[i]);
436 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005, 0x0004); /* fir 2 */
437 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005, 0x0040);
438 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005, 0x0000);
439 for (i = 5; i >= 0; i--)
440 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005,
441 msp_init_data[type].fir2[i]);
443 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0083, /* MODE_REG */
444 msp_init_data[type].mode_reg);
446 msp3400c_setcarrier(client, msp_init_data[type].cdo1,
447 msp_init_data[type].cdo2);
449 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0056, 0); /*LOAD_REG_1/2*/
452 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008,
454 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009,
456 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b,
457 msp_init_data[type].dfp_src);
459 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008,
460 msp_init_data[type].dfp_src);
461 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009,
462 msp_init_data[type].dfp_src);
463 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b,
464 msp_init_data[type].dfp_src);
466 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000a,
467 msp_init_data[type].dfp_src);
468 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000e,
469 msp_init_data[type].dfp_matrix);
471 if (HAVE_NICAM(msp)) {
473 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0010, 0x5a00); /* was: 0x3000 */
477 static int best_audio_mode(int rxsubchans)
479 if (rxsubchans & V4L2_TUNER_SUB_STEREO)
480 return V4L2_TUNER_MODE_STEREO;
481 if (rxsubchans & V4L2_TUNER_SUB_LANG1)
482 return V4L2_TUNER_MODE_LANG1;
483 if (rxsubchans & V4L2_TUNER_SUB_LANG2)
484 return V4L2_TUNER_MODE_LANG2;
485 return V4L2_TUNER_MODE_MONO;
488 /* turn on/off nicam + stereo */
489 static void msp3400c_set_audmode(struct i2c_client *client, int audmode)
491 static char *strmode[16] = {
493 [ 0 ... 15 ] = "invalid",
495 [ V4L2_TUNER_MODE_MONO ] = "mono",
496 [ V4L2_TUNER_MODE_STEREO ] = "stereo",
497 [ V4L2_TUNER_MODE_LANG1 ] = "lang1",
498 [ V4L2_TUNER_MODE_LANG2 ] = "lang2",
500 struct msp3400c *msp = i2c_get_clientdata(client);
501 int nicam=0; /* channel source: FM/AM or nicam */
504 BUG_ON(msp->opmode == OPMODE_SIMPLER);
505 msp->audmode = audmode;
507 /* switch demodulator */
509 case MSP_MODE_FM_TERRA:
510 dprintk(KERN_DEBUG "msp3400: FM setstereo: %s\n",
512 msp3400c_setcarrier(client,msp->second,msp->main);
514 case V4L2_TUNER_MODE_STEREO:
515 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000e, 0x3001);
517 case V4L2_TUNER_MODE_MONO:
518 case V4L2_TUNER_MODE_LANG1:
519 case V4L2_TUNER_MODE_LANG2:
520 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000e, 0x3000);
524 case MSP_MODE_FM_SAT:
525 dprintk(KERN_DEBUG "msp3400: SAT setstereo: %s\n",
528 case V4L2_TUNER_MODE_MONO:
529 msp3400c_setcarrier(client, MSP_CARRIER(6.5), MSP_CARRIER(6.5));
531 case V4L2_TUNER_MODE_STEREO:
532 msp3400c_setcarrier(client, MSP_CARRIER(7.2), MSP_CARRIER(7.02));
534 case V4L2_TUNER_MODE_LANG1:
535 msp3400c_setcarrier(client, MSP_CARRIER(7.38), MSP_CARRIER(7.02));
537 case V4L2_TUNER_MODE_LANG2:
538 msp3400c_setcarrier(client, MSP_CARRIER(7.38), MSP_CARRIER(7.02));
542 case MSP_MODE_FM_NICAM1:
543 case MSP_MODE_FM_NICAM2:
544 case MSP_MODE_AM_NICAM:
545 dprintk(KERN_DEBUG "msp3400: NICAM setstereo: %s\n",
547 msp3400c_setcarrier(client,msp->second,msp->main);
552 dprintk(KERN_DEBUG "msp3400: BTSC setstereo: %s\n",
556 case MSP_MODE_EXTERN:
557 dprintk(KERN_DEBUG "msp3400: extern setstereo: %s\n",
561 case MSP_MODE_FM_RADIO:
562 dprintk(KERN_DEBUG "msp3400: FM-Radio setstereo: %s\n",
566 dprintk(KERN_DEBUG "msp3400: mono setstereo\n");
572 case V4L2_TUNER_MODE_STEREO:
573 src = 0x0020 | nicam;
575 case V4L2_TUNER_MODE_MONO:
576 if (msp->mode == MSP_MODE_AM_NICAM) {
577 dprintk("msp3400: switching to AM mono\n");
578 /* AM mono decoding is handled by tuner, not MSP chip */
579 /* SCART switching control register */
580 msp3400c_set_scart(client,SCART_MONO,0);
584 case V4L2_TUNER_MODE_LANG1:
585 src = 0x0000 | nicam;
587 case V4L2_TUNER_MODE_LANG2:
588 src = 0x0010 | nicam;
592 "msp3400: setstereo final source/matrix = 0x%x\n", src);
595 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008,0x0520);
596 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009,0x0620);
597 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000a,src);
598 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b,src);
600 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008,src);
601 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009,src);
602 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000a,src);
603 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b,src);
608 msp3400c_print_mode(struct msp3400c *msp)
610 if (msp->main == msp->second) {
611 printk(KERN_DEBUG "msp3400: mono sound carrier: %d.%03d MHz\n",
612 msp->main/910000,(msp->main/910)%1000);
614 printk(KERN_DEBUG "msp3400: main sound carrier: %d.%03d MHz\n",
615 msp->main/910000,(msp->main/910)%1000);
617 if (msp->mode == MSP_MODE_FM_NICAM1 ||
618 msp->mode == MSP_MODE_FM_NICAM2)
619 printk(KERN_DEBUG "msp3400: NICAM/FM carrier : %d.%03d MHz\n",
620 msp->second/910000,(msp->second/910)%1000);
621 if (msp->mode == MSP_MODE_AM_NICAM)
622 printk(KERN_DEBUG "msp3400: NICAM/AM carrier : %d.%03d MHz\n",
623 msp->second/910000,(msp->second/910)%1000);
624 if (msp->mode == MSP_MODE_FM_TERRA &&
625 msp->main != msp->second) {
626 printk(KERN_DEBUG "msp3400: FM-stereo carrier : %d.%03d MHz\n",
627 msp->second/910000,(msp->second/910)%1000);
631 /* ----------------------------------------------------------------------- */
633 struct REGISTER_DUMP {
639 autodetect_stereo(struct i2c_client *client)
641 struct msp3400c *msp = i2c_get_clientdata(client);
643 int rxsubchans = msp->rxsubchans;
644 int newnicam = msp->nicam_on;
648 case MSP_MODE_FM_TERRA:
649 val = msp3400c_read(client, I2C_MSP3400C_DFP, 0x18);
653 "msp34xx: stereo detect register: %d\n",val);
655 rxsubchans = V4L2_TUNER_SUB_STEREO | V4L2_TUNER_SUB_MONO;
656 } else if (val < -4096) {
657 rxsubchans = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
659 rxsubchans = V4L2_TUNER_SUB_MONO;
663 case MSP_MODE_FM_NICAM1:
664 case MSP_MODE_FM_NICAM2:
665 case MSP_MODE_AM_NICAM:
666 val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x23);
668 "msp34xx: nicam sync=%d, mode=%d\n",
669 val & 1, (val & 0x1e) >> 1);
673 switch ((val & 0x1e) >> 1) {
676 rxsubchans = V4L2_TUNER_SUB_STEREO;
680 rxsubchans = V4L2_TUNER_SUB_MONO
681 | V4L2_TUNER_SUB_LANG1;
685 rxsubchans = V4L2_TUNER_SUB_MONO
686 | V4L2_TUNER_SUB_LANG1
687 | V4L2_TUNER_SUB_LANG2;
690 rxsubchans = V4L2_TUNER_SUB_MONO;
696 rxsubchans = V4L2_TUNER_SUB_MONO;
700 val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x200);
702 "msp3410: status=0x%x (pri=%s, sec=%s, %s%s%s)\n",
704 (val & 0x0002) ? "no" : "yes",
705 (val & 0x0004) ? "no" : "yes",
706 (val & 0x0040) ? "stereo" : "mono",
707 (val & 0x0080) ? ", nicam 2nd mono" : "",
708 (val & 0x0100) ? ", bilingual/SAP" : "");
709 rxsubchans = V4L2_TUNER_SUB_MONO;
710 if (val & 0x0040) rxsubchans |= V4L2_TUNER_SUB_STEREO;
711 if (val & 0x0100) rxsubchans |= V4L2_TUNER_SUB_LANG1;
714 if (rxsubchans != msp->rxsubchans) {
716 dprintk(KERN_DEBUG "msp34xx: watch: rxsubchans %d => %d\n",
717 msp->rxsubchans,rxsubchans);
718 msp->rxsubchans = rxsubchans;
720 if (newnicam != msp->nicam_on) {
722 dprintk(KERN_DEBUG "msp34xx: watch: nicam %d => %d\n",
723 msp->nicam_on,newnicam);
724 msp->nicam_on = newnicam;
730 * A kernel thread for msp3400 control -- we don't want to block the
731 * in the ioctl while doing the sound carrier & stereo detect
734 static int msp34xx_sleep(struct msp3400c *msp, int timeout)
736 DECLARE_WAITQUEUE(wait, current);
738 add_wait_queue(&msp->wq, &wait);
739 if (!kthread_should_stop()) {
741 set_current_state(TASK_INTERRUPTIBLE);
744 schedule_timeout_interruptible
745 (msecs_to_jiffies(timeout));
749 remove_wait_queue(&msp->wq, &wait);
754 /* stereo/multilang monitoring */
755 static void watch_stereo(struct i2c_client *client)
757 struct msp3400c *msp = i2c_get_clientdata(client);
759 if (autodetect_stereo(client))
760 msp3400c_set_audmode(client,best_audio_mode(msp->rxsubchans));
762 msp->watch_stereo = 0;
765 static int msp3400c_thread(void *data)
767 struct i2c_client *client = data;
768 struct msp3400c *msp = i2c_get_clientdata(client);
769 struct CARRIER_DETECT *cd;
770 int count, max1,max2,val1,val2, val,this;
772 printk("msp3400: kthread started\n");
774 d2printk("msp3400: thread: sleep\n");
775 msp34xx_sleep(msp,-1);
776 d2printk("msp3400: thread: wakeup\n");
779 dprintk("msp3410: thread: restart scan\n");
781 if (kthread_should_stop())
784 if (VIDEO_MODE_RADIO == msp->norm ||
785 MSP_MODE_EXTERN == msp->mode) {
786 /* no carrier scan, just unmute */
787 printk("msp3400: thread: no carrier scan\n");
788 msp3400c_setvolume(client, msp->muted,
789 msp->volume, msp->balance);
794 msp3400c_setvolume(client, msp->muted, 0, 0);
795 msp3400c_setmode(client, MSP_MODE_AM_DETECT /* +1 */ );
798 msp->watch_stereo = 0;
800 /* some time for the tuner to sync */
801 if (msp34xx_sleep(msp,200))
804 /* carrier detect pass #1 -- main carrier */
805 cd = carrier_detect_main; count = CARRIER_COUNT(carrier_detect_main);
807 if (amsound && (msp->norm == VIDEO_MODE_SECAM)) {
808 /* autodetect doesn't work well with AM ... */
811 dprintk("msp3400: AM sound override\n");
814 for (this = 0; this < count; this++) {
815 msp3400c_setcarrier(client, cd[this].cdo,cd[this].cdo);
816 if (msp34xx_sleep(msp,100))
818 val = msp3400c_read(client, I2C_MSP3400C_DFP, 0x1b);
822 val1 = val, max1 = this;
823 dprintk("msp3400: carrier1 val: %5d / %s\n", val,cd[this].name);
826 /* carrier detect pass #2 -- second (stereo) carrier */
829 cd = carrier_detect_55;
830 count = CARRIER_COUNT(carrier_detect_55);
833 cd = carrier_detect_65;
834 count = CARRIER_COUNT(carrier_detect_65);
839 cd = NULL; count = 0;
843 if (amsound && (msp->norm == VIDEO_MODE_SECAM)) {
844 /* autodetect doesn't work well with AM ... */
845 cd = NULL; count = 0; max2 = 0;
847 for (this = 0; this < count; this++) {
848 msp3400c_setcarrier(client, cd[this].cdo,cd[this].cdo);
849 if (msp34xx_sleep(msp,100))
851 val = msp3400c_read(client, I2C_MSP3400C_DFP, 0x1b);
855 val2 = val, max2 = this;
856 dprintk("msp3400: carrier2 val: %5d / %s\n", val,cd[this].name);
859 /* programm the msp3400 according to the results */
860 msp->main = carrier_detect_main[max1].cdo;
865 msp->second = carrier_detect_55[max2].cdo;
866 msp3400c_setmode(client, MSP_MODE_FM_TERRA);
868 msp3400c_set_audmode(client, V4L2_TUNER_MODE_MONO);
869 msp->watch_stereo = 1;
870 } else if (max2 == 1 && HAVE_NICAM(msp)) {
872 msp->second = carrier_detect_55[max2].cdo;
873 msp3400c_setmode(client, MSP_MODE_FM_NICAM1);
875 msp3400c_setcarrier(client, msp->second, msp->main);
876 msp->watch_stereo = 1;
883 msp->second = MSP_CARRIER(6.552);
884 msp3400c_setmode(client, MSP_MODE_FM_NICAM2);
886 msp3400c_setcarrier(client, msp->second, msp->main);
887 msp->watch_stereo = 1;
890 if (max2 == 1 || max2 == 2) {
892 msp->second = carrier_detect_65[max2].cdo;
893 msp3400c_setmode(client, MSP_MODE_FM_TERRA);
895 msp3400c_set_audmode(client, V4L2_TUNER_MODE_MONO);
896 msp->watch_stereo = 1;
897 } else if (max2 == 0 &&
898 msp->norm == VIDEO_MODE_SECAM) {
899 /* L NICAM or AM-mono */
900 msp->second = carrier_detect_65[max2].cdo;
901 msp3400c_setmode(client, MSP_MODE_AM_NICAM);
903 msp3400c_set_audmode(client, V4L2_TUNER_MODE_MONO);
904 msp3400c_setcarrier(client, msp->second, msp->main);
905 /* volume prescale for SCART (AM mono input) */
906 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000d, 0x1900);
907 msp->watch_stereo = 1;
908 } else if (max2 == 0 && HAVE_NICAM(msp)) {
910 msp->second = carrier_detect_65[max2].cdo;
911 msp3400c_setmode(client, MSP_MODE_FM_NICAM1);
913 msp3400c_setcarrier(client, msp->second, msp->main);
914 msp->watch_stereo = 1;
922 msp->second = carrier_detect_main[max1].cdo;
923 msp3400c_setmode(client, MSP_MODE_FM_TERRA);
925 msp3400c_setcarrier(client, msp->second, msp->main);
926 msp->rxsubchans = V4L2_TUNER_SUB_MONO;
927 msp3400c_set_audmode(client, V4L2_TUNER_MODE_MONO);
932 msp3400c_setvolume(client, msp->muted,
933 msp->volume, msp->balance);
935 msp3400c_print_mode(msp);
937 /* monitor tv audio mode */
938 while (msp->watch_stereo) {
939 if (msp34xx_sleep(msp,5000))
941 watch_stereo(client);
944 dprintk(KERN_DEBUG "msp3400: thread: exit\n");
948 /* ----------------------------------------------------------------------- */
949 /* this one uses the automatic sound standard detection of newer */
950 /* msp34xx chip versions */
952 static struct MODES {
957 { 0x0000, 0, 0, "ERROR" },
958 { 0x0001, 0, 0, "autodetect start" },
959 { 0x0002, MSP_CARRIER(4.5), MSP_CARRIER(4.72), "4.5/4.72 M Dual FM-Stereo" },
960 { 0x0003, MSP_CARRIER(5.5), MSP_CARRIER(5.7421875), "5.5/5.74 B/G Dual FM-Stereo" },
961 { 0x0004, MSP_CARRIER(6.5), MSP_CARRIER(6.2578125), "6.5/6.25 D/K1 Dual FM-Stereo" },
962 { 0x0005, MSP_CARRIER(6.5), MSP_CARRIER(6.7421875), "6.5/6.74 D/K2 Dual FM-Stereo" },
963 { 0x0006, MSP_CARRIER(6.5), MSP_CARRIER(6.5), "6.5 D/K FM-Mono (HDEV3)" },
964 { 0x0008, MSP_CARRIER(5.5), MSP_CARRIER(5.85), "5.5/5.85 B/G NICAM FM" },
965 { 0x0009, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85 L NICAM AM" },
966 { 0x000a, MSP_CARRIER(6.0), MSP_CARRIER(6.55), "6.0/6.55 I NICAM FM" },
967 { 0x000b, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85 D/K NICAM FM" },
968 { 0x000c, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85 D/K NICAM FM (HDEV2)" },
969 { 0x0020, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5 M BTSC-Stereo" },
970 { 0x0021, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5 M BTSC-Mono + SAP" },
971 { 0x0030, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5 M EIA-J Japan Stereo" },
972 { 0x0040, MSP_CARRIER(10.7), MSP_CARRIER(10.7), "10.7 FM-Stereo Radio" },
973 { 0x0050, MSP_CARRIER(6.5), MSP_CARRIER(6.5), "6.5 SAT-Mono" },
974 { 0x0051, MSP_CARRIER(7.02), MSP_CARRIER(7.20), "7.02/7.20 SAT-Stereo" },
975 { 0x0060, MSP_CARRIER(7.2), MSP_CARRIER(7.2), "7.2 SAT ADR" },
976 { -1, 0, 0, NULL }, /* EOF */
979 static inline const char *msp34xx_standard_mode_name(int mode)
982 for (i = 0; modelist[i].name != NULL; i++)
983 if (modelist[i].retval == mode)
984 return modelist[i].name;
988 static int msp34xx_modus(int norm)
993 /* experimental: not sure this works with all chip versions */
996 /* previous value, try this if it breaks ... */
999 case VIDEO_MODE_NTSC: /* BTSC */
1001 case VIDEO_MODE_SECAM:
1003 case VIDEO_MODE_RADIO:
1005 case VIDEO_MODE_AUTO:
1012 static int msp34xx_standard(int norm)
1015 case VIDEO_MODE_PAL:
1017 case VIDEO_MODE_NTSC: /* BTSC */
1019 case VIDEO_MODE_SECAM:
1021 case VIDEO_MODE_RADIO:
1028 static int msp3410d_thread(void *data)
1030 struct i2c_client *client = data;
1031 struct msp3400c *msp = i2c_get_clientdata(client);
1034 printk("msp3410: daemon started\n");
1036 d2printk(KERN_DEBUG "msp3410: thread: sleep\n");
1037 msp34xx_sleep(msp,-1);
1038 d2printk(KERN_DEBUG "msp3410: thread: wakeup\n");
1041 dprintk("msp3410: thread: restart scan\n");
1043 if (kthread_should_stop())
1046 if (msp->mode == MSP_MODE_EXTERN) {
1047 /* no carrier scan needed, just unmute */
1048 dprintk(KERN_DEBUG "msp3410: thread: no carrier scan\n");
1049 msp3400c_setvolume(client, msp->muted,
1050 msp->volume, msp->balance);
1054 /* put into sane state (and mute) */
1055 msp3400c_reset(client);
1057 /* some time for the tuner to sync */
1058 if (msp34xx_sleep(msp,200))
1061 /* start autodetect */
1062 mode = msp34xx_modus(msp->norm);
1063 std = msp34xx_standard(msp->norm);
1064 msp3400c_write(client, I2C_MSP3400C_DEM, 0x30, mode);
1065 msp3400c_write(client, I2C_MSP3400C_DEM, 0x20, std);
1066 msp->watch_stereo = 0;
1069 printk(KERN_DEBUG "msp3410: setting mode: %s (0x%04x)\n",
1070 msp34xx_standard_mode_name(std) ,std);
1073 /* programmed some specific mode */
1076 /* triggered autodetect */
1078 if (msp34xx_sleep(msp,100))
1082 val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x7e);
1085 dprintk(KERN_DEBUG "msp3410: detection still in progress\n");
1088 for (i = 0; modelist[i].name != NULL; i++)
1089 if (modelist[i].retval == val)
1091 dprintk(KERN_DEBUG "msp3410: current mode: %s (0x%04x)\n",
1092 modelist[i].name ? modelist[i].name : "unknown",
1094 msp->main = modelist[i].main;
1095 msp->second = modelist[i].second;
1097 if (amsound && (msp->norm == VIDEO_MODE_SECAM) && (val != 0x0009)) {
1098 /* autodetection has failed, let backup */
1099 dprintk(KERN_DEBUG "msp3410: autodetection failed,"
1100 " switching to backup mode: %s (0x%04x)\n",
1101 modelist[8].name ? modelist[8].name : "unknown",val);
1103 msp3400c_write(client, I2C_MSP3400C_DEM, 0x20, val);
1106 /* set various prescales */
1107 msp3400c_write(client, I2C_MSP3400C_DFP, 0x0d, 0x1900); /* scart */
1108 msp3400c_write(client, I2C_MSP3400C_DFP, 0x0e, 0x2403); /* FM */
1109 msp3400c_write(client, I2C_MSP3400C_DFP, 0x10, 0x5a00); /* nicam */
1113 case 0x0008: /* B/G NICAM */
1114 case 0x000a: /* I NICAM */
1116 msp->mode = MSP_MODE_FM_NICAM1;
1118 msp->mode = MSP_MODE_FM_NICAM2;
1119 /* just turn on stereo */
1120 msp->rxsubchans = V4L2_TUNER_SUB_STEREO;
1122 msp->watch_stereo = 1;
1123 msp3400c_set_audmode(client,V4L2_TUNER_MODE_STEREO);
1126 msp->mode = MSP_MODE_AM_NICAM;
1127 msp->rxsubchans = V4L2_TUNER_SUB_MONO;
1129 msp3400c_set_audmode(client,V4L2_TUNER_MODE_MONO);
1130 msp->watch_stereo = 1;
1132 case 0x0020: /* BTSC */
1133 /* just turn on stereo */
1134 msp->mode = MSP_MODE_BTSC;
1135 msp->rxsubchans = V4L2_TUNER_SUB_STEREO;
1137 msp->watch_stereo = 1;
1138 msp3400c_set_audmode(client,V4L2_TUNER_MODE_STEREO);
1140 case 0x0040: /* FM radio */
1141 msp->mode = MSP_MODE_FM_RADIO;
1142 msp->rxsubchans = V4L2_TUNER_SUB_STEREO;
1143 msp->audmode = V4L2_TUNER_MODE_STEREO;
1145 msp->watch_stereo = 0;
1146 /* not needed in theory if HAVE_RADIO(), but
1147 short programming enables carrier mute */
1148 msp3400c_setmode(client,MSP_MODE_FM_RADIO);
1149 msp3400c_setcarrier(client, MSP_CARRIER(10.7),
1152 msp3400c_set_scart(client,SCART_IN2,0);
1153 /* msp34xx does radio decoding */
1154 msp3400c_write(client,I2C_MSP3400C_DFP, 0x08, 0x0020);
1155 msp3400c_write(client,I2C_MSP3400C_DFP, 0x09, 0x0020);
1156 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0b, 0x0020);
1161 msp->mode = MSP_MODE_FM_TERRA;
1162 msp->rxsubchans = V4L2_TUNER_SUB_MONO;
1163 msp->audmode = V4L2_TUNER_MODE_MONO;
1165 msp->watch_stereo = 1;
1169 /* unmute, restore misc registers */
1170 msp3400c_setbass(client, msp->bass);
1171 msp3400c_settreble(client, msp->treble);
1172 msp3400c_setvolume(client, msp->muted,
1173 msp->volume, msp->balance);
1174 msp3400c_write(client, I2C_MSP3400C_DFP, 0x0013, msp->acb);
1176 /* monitor tv audio mode */
1177 while (msp->watch_stereo) {
1178 if (msp34xx_sleep(msp,5000))
1180 watch_stereo(client);
1183 dprintk(KERN_DEBUG "msp3410: thread: exit\n");
1187 /* ----------------------------------------------------------------------- */
1188 /* msp34xxG + (simpler no-thread) */
1189 /* this one uses both automatic standard detection and automatic sound */
1190 /* select which are available in the newer G versions */
1191 /* struct msp: only norm, acb and source are really used in this mode */
1193 static void msp34xxg_set_source(struct i2c_client *client, int source);
1195 /* (re-)initialize the msp34xxg, according to the current norm in msp->norm
1196 * return 0 if it worked, -1 if it failed
1198 static int msp34xxg_init(struct i2c_client *client)
1200 struct msp3400c *msp = i2c_get_clientdata(client);
1203 if (msp3400c_reset(client))
1206 /* make sure that input/output is muted (paranoid mode) */
1207 if (msp3400c_write(client,
1210 0x0f20 /* mute DSP input, mute SCART 1 */))
1213 /* step-by-step initialisation, as described in the manual */
1214 modus = msp34xx_modus(msp->norm);
1215 std = msp34xx_standard(msp->norm);
1216 modus &= ~0x03; /* STATUS_CHANGE=0 */
1217 modus |= 0x01; /* AUTOMATIC_SOUND_DETECTION=1 */
1218 if (msp3400c_write(client,
1223 if (msp3400c_write(client,
1229 /* write the dfps that may have an influence on
1230 standard/audio autodetection right now */
1231 msp34xxg_set_source(client, msp->source);
1233 if (msp3400c_write(client, I2C_MSP3400C_DFP,
1234 0x0e, /* AM/FM Prescale */
1235 0x3000 /* default: [15:8] 75khz deviation */))
1238 if (msp3400c_write(client, I2C_MSP3400C_DFP,
1239 0x10, /* NICAM Prescale */
1240 0x5a00 /* default: 9db gain (as recommended) */))
1243 if (msp3400c_write(client,
1245 0x20, /* STANDARD SELECT */
1246 standard /* default: 0x01 for automatic standard select*/))
1251 static int msp34xxg_thread(void *data)
1253 struct i2c_client *client = data;
1254 struct msp3400c *msp = i2c_get_clientdata(client);
1257 printk("msp34xxg: daemon started\n");
1258 msp->source = 1; /* default */
1260 d2printk(KERN_DEBUG "msp34xxg: thread: sleep\n");
1261 msp34xx_sleep(msp,-1);
1262 d2printk(KERN_DEBUG "msp34xxg: thread: wakeup\n");
1265 dprintk("msp34xxg: thread: restart scan\n");
1267 if (kthread_should_stop())
1271 msp34xxg_init(client);
1276 /* watch autodetect */
1277 dprintk("msp34xxg: triggered autodetect, waiting for result\n");
1278 for (i = 0; i < 10; i++) {
1279 if (msp34xx_sleep(msp,100))
1283 val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x7e);
1288 dprintk("msp34xxg: detection still in progress\n");
1291 dprintk("msp34xxg: detection still in progress after 10 tries. giving up.\n");
1296 dprintk("msp34xxg: current mode: %s (0x%04x)\n",
1297 msp34xx_standard_mode_name(std), std);
1299 /* unmute: dispatch sound to scart output, set scart volume */
1300 dprintk("msp34xxg: unmute\n");
1302 msp3400c_setbass(client, msp->bass);
1303 msp3400c_settreble(client, msp->treble);
1304 msp3400c_setvolume(client, msp->muted, msp->volume, msp->balance);
1307 if (msp3400c_write(client,
1313 dprintk(KERN_DEBUG "msp34xxg: thread: exit\n");
1317 /* set the same 'source' for the loudspeaker, scart and quasi-peak detector
1318 * the value for source is the same as bit 15:8 of DFP registers 0x08,
1319 * 0x0a and 0x0c: 0=mono, 1=stereo or A|B, 2=SCART, 3=stereo or A, 4=stereo or B
1321 * this function replaces msp3400c_setstereo
1323 static void msp34xxg_set_source(struct i2c_client *client, int source)
1325 struct msp3400c *msp = i2c_get_clientdata(client);
1327 /* fix matrix mode to stereo and let the msp choose what
1328 * to output according to 'source', as recommended
1329 * for MONO (source==0) downmixing set bit[7:0] to 0x30
1331 int value = (source&0x07)<<8|(source==0 ? 0x30:0x20);
1332 dprintk("msp34xxg: set source to %d (0x%x)\n", source, value);
1333 msp3400c_write(client,
1335 0x08, /* Loudspeaker Output */
1337 msp3400c_write(client,
1339 0x0a, /* SCART1 DA Output */
1341 msp3400c_write(client,
1343 0x0c, /* Quasi-peak detector */
1346 * set identification threshold. Personally, I
1347 * I set it to a higher value that the default
1348 * of 0x190 to ignore noisy stereo signals.
1349 * this needs tuning. (recommended range 0x00a0-0x03c0)
1350 * 0x7f0 = forced mono mode
1352 msp3400c_write(client,
1354 0x22, /* a2 threshold for stereo/bilingual */
1359 static void msp34xxg_detect_stereo(struct i2c_client *client)
1361 struct msp3400c *msp = i2c_get_clientdata(client);
1363 int status = msp3400c_read(client,
1365 0x0200 /* STATUS */);
1366 int is_bilingual = status&0x100;
1367 int is_stereo = status&0x40;
1369 msp->rxsubchans = 0;
1371 msp->rxsubchans |= V4L2_TUNER_SUB_STEREO;
1373 msp->rxsubchans |= V4L2_TUNER_SUB_MONO;
1375 msp->rxsubchans |= V4L2_TUNER_SUB_LANG1|V4L2_TUNER_SUB_LANG2;
1376 /* I'm supposed to check whether it's SAP or not
1377 * and set only LANG2/SAP in this case. Yet, the MSP
1378 * does a lot of work to hide this and handle everything
1379 * the same way. I don't want to work around it so unless
1380 * this is a problem, I'll handle SAP just like lang1/lang2.
1383 dprintk("msp34xxg: status=0x%x, stereo=%d, bilingual=%d -> rxsubchans=%d\n",
1384 status, is_stereo, is_bilingual, msp->rxsubchans);
1387 static void msp34xxg_set_audmode(struct i2c_client *client, int audmode)
1389 struct msp3400c *msp = i2c_get_clientdata(client);
1393 case V4L2_TUNER_MODE_MONO:
1394 source=0; /* mono only */
1396 case V4L2_TUNER_MODE_STEREO:
1397 source=1; /* stereo or A|B, see comment in msp34xxg_get_v4l2_stereo() */
1398 /* problem: that could also mean 2 (scart input) */
1400 case V4L2_TUNER_MODE_LANG1:
1401 source=3; /* stereo or A */
1403 case V4L2_TUNER_MODE_LANG2:
1404 source=4; /* stereo or B */
1411 msp->audmode = audmode;
1412 msp34xxg_set_source(client, source);
1416 /* ----------------------------------------------------------------------- */
1418 static int msp_attach(struct i2c_adapter *adap, int addr, int kind);
1419 static int msp_detach(struct i2c_client *client);
1420 static int msp_probe(struct i2c_adapter *adap);
1421 static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg);
1423 static int msp_suspend(struct device * dev, pm_message_t state);
1424 static int msp_resume(struct device * dev);
1426 static void msp_wake_thread(struct i2c_client *client);
1428 static struct i2c_driver driver = {
1429 .owner = THIS_MODULE,
1430 .name = "i2c msp3400 driver",
1431 .id = I2C_DRIVERID_MSP3400,
1432 .flags = I2C_DF_NOTIFY,
1433 .attach_adapter = msp_probe,
1434 .detach_client = msp_detach,
1435 .command = msp_command,
1437 .suspend = msp_suspend,
1438 .resume = msp_resume,
1442 static struct i2c_client client_template =
1445 .flags = I2C_CLIENT_ALLOW_USE,
1449 static int msp_attach(struct i2c_adapter *adap, int addr, int kind)
1451 struct msp3400c *msp;
1452 struct i2c_client *c;
1453 int (*thread_func)(void *data) = NULL;
1455 client_template.adapter = adap;
1456 client_template.addr = addr;
1458 if (-1 == msp3400c_reset(&client_template)) {
1459 dprintk("msp34xx: no chip found\n");
1463 if (NULL == (c = kmalloc(sizeof(struct i2c_client),GFP_KERNEL)))
1465 memcpy(c,&client_template,sizeof(struct i2c_client));
1466 if (NULL == (msp = kmalloc(sizeof(struct msp3400c),GFP_KERNEL))) {
1471 memset(msp,0,sizeof(struct msp3400c));
1472 msp->volume = 58880; /* 0db gain */
1473 msp->balance = 32768;
1475 msp->treble = 32768;
1479 i2c_set_clientdata(c, msp);
1480 init_waitqueue_head(&msp->wq);
1482 if (-1 == msp3400c_reset(c)) {
1485 dprintk("msp34xx: no chip found\n");
1489 msp->rev1 = msp3400c_read(c, I2C_MSP3400C_DFP, 0x1e);
1490 if (-1 != msp->rev1)
1491 msp->rev2 = msp3400c_read(c, I2C_MSP3400C_DFP, 0x1f);
1492 if ((-1 == msp->rev1) || (0 == msp->rev1 && 0 == msp->rev2)) {
1495 dprintk("msp34xx: error while reading chip version\n");
1499 msp3400c_setvolume(c, msp->muted, msp->volume, msp->balance);
1501 snprintf(c->name, sizeof(c->name), "MSP34%02d%c-%c%d",
1502 (msp->rev2>>8)&0xff, (msp->rev1&0xff)+'@',
1503 ((msp->rev1>>8)&0xff)+'@', msp->rev2&0x1f);
1505 msp->opmode = opmode;
1506 if (OPMODE_AUTO == msp->opmode) {
1507 if (HAVE_SIMPLER(msp))
1508 msp->opmode = OPMODE_SIMPLER;
1509 else if (HAVE_SIMPLE(msp))
1510 msp->opmode = OPMODE_SIMPLE;
1512 msp->opmode = OPMODE_MANUAL;
1515 /* hello world :-) */
1516 printk(KERN_INFO "msp34xx: init: chip=%s", c->name);
1517 if (HAVE_NICAM(msp))
1519 if (HAVE_SIMPLE(msp))
1521 if (HAVE_SIMPLER(msp))
1522 printk(" +simpler");
1523 if (HAVE_RADIO(msp))
1526 /* version-specific initialization */
1527 switch (msp->opmode) {
1529 printk(" mode=manual");
1530 thread_func = msp3400c_thread;
1533 printk(" mode=simple");
1534 thread_func = msp3410d_thread;
1536 case OPMODE_SIMPLER:
1537 printk(" mode=simpler");
1538 thread_func = msp34xxg_thread;
1543 /* startup control thread if needed */
1545 msp->kthread = kthread_run(thread_func, c, "msp34xx");
1546 if (NULL == msp->kthread)
1547 printk(KERN_WARNING "msp34xx: kernel_thread() failed\n");
1552 i2c_attach_client(c);
1556 static int msp_detach(struct i2c_client *client)
1558 struct msp3400c *msp = i2c_get_clientdata(client);
1560 /* shutdown control thread */
1561 if (msp->kthread >= 0) {
1563 kthread_stop(msp->kthread);
1565 msp3400c_reset(client);
1567 i2c_detach_client(client);
1573 static int msp_probe(struct i2c_adapter *adap)
1575 if (adap->class & I2C_CLASS_TV_ANALOG)
1576 return i2c_probe(adap, &addr_data, msp_attach);
1580 static void msp_wake_thread(struct i2c_client *client)
1582 struct msp3400c *msp = i2c_get_clientdata(client);
1584 if (NULL == msp->kthread)
1586 msp3400c_setvolume(client,msp->muted,0,0);
1587 msp->watch_stereo = 0;
1589 wake_up_interruptible(&msp->wq);
1592 /* ----------------------------------------------------------------------- */
1594 static int mode_v4l2_to_v4l1(int rxsubchans)
1598 if (rxsubchans & V4L2_TUNER_SUB_STEREO)
1599 mode |= VIDEO_SOUND_STEREO;
1600 if (rxsubchans & V4L2_TUNER_SUB_LANG2)
1601 mode |= VIDEO_SOUND_LANG2;
1602 if (rxsubchans & V4L2_TUNER_SUB_LANG1)
1603 mode |= VIDEO_SOUND_LANG1;
1605 mode |= VIDEO_SOUND_MONO;
1609 static int mode_v4l1_to_v4l2(int mode)
1611 if (mode & VIDEO_SOUND_STEREO)
1612 return V4L2_TUNER_MODE_STEREO;
1613 if (mode & VIDEO_SOUND_LANG2)
1614 return V4L2_TUNER_MODE_LANG2;
1615 if (mode & VIDEO_SOUND_LANG1)
1616 return V4L2_TUNER_MODE_LANG1;
1617 return V4L2_TUNER_MODE_MONO;
1620 static void msp_any_detect_stereo(struct i2c_client *client)
1622 struct msp3400c *msp = i2c_get_clientdata(client);
1624 switch (msp->opmode) {
1627 autodetect_stereo(client);
1629 case OPMODE_SIMPLER:
1630 msp34xxg_detect_stereo(client);
1635 static void msp_any_set_audmode(struct i2c_client *client, int audmode)
1637 struct msp3400c *msp = i2c_get_clientdata(client);
1639 switch (msp->opmode) {
1642 msp->watch_stereo = 0;
1643 msp3400c_set_audmode(client, audmode);
1645 case OPMODE_SIMPLER:
1646 msp34xxg_set_audmode(client, audmode);
1651 static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg)
1653 struct msp3400c *msp = i2c_get_clientdata(client);
1659 case AUDC_SET_INPUT:
1660 dprintk(KERN_DEBUG "msp34xx: AUDC_SET_INPUT(%d)\n",*sarg);
1661 if (*sarg == msp->input)
1666 /* Hauppauge uses IN2 for the radio */
1667 msp->mode = MSP_MODE_FM_RADIO;
1670 case AUDIO_EXTERN_1:
1671 /* IN1 is often used for external input ... */
1672 msp->mode = MSP_MODE_EXTERN;
1675 case AUDIO_EXTERN_2:
1676 /* ... sometimes it is IN2 through ;) */
1677 msp->mode = MSP_MODE_EXTERN;
1684 if (*sarg & AUDIO_MUTE)
1685 msp3400c_set_scart(client,SCART_MUTE,0);
1689 msp->rxsubchans = V4L2_TUNER_SUB_STEREO;
1690 msp->audmode = V4L2_TUNER_MODE_STEREO;
1691 msp3400c_set_scart(client,scart,0);
1692 msp3400c_write(client,I2C_MSP3400C_DFP,0x000d,0x1900);
1693 if (msp->opmode != OPMODE_SIMPLER)
1694 msp3400c_set_audmode(client, msp->audmode);
1696 msp_wake_thread(client);
1699 case AUDC_SET_RADIO:
1700 dprintk(KERN_DEBUG "msp34xx: AUDC_SET_RADIO\n");
1701 msp->norm = VIDEO_MODE_RADIO;
1702 dprintk(KERN_DEBUG "msp34xx: switching to radio mode\n");
1703 msp->watch_stereo = 0;
1704 switch (msp->opmode) {
1706 /* set msp3400 to FM radio mode */
1707 msp3400c_setmode(client,MSP_MODE_FM_RADIO);
1708 msp3400c_setcarrier(client, MSP_CARRIER(10.7),
1710 msp3400c_setvolume(client, msp->muted,
1711 msp->volume, msp->balance);
1714 case OPMODE_SIMPLER:
1715 /* the thread will do for us */
1716 msp_wake_thread(client);
1721 /* --- v4l ioctls --- */
1722 /* take care: bttv does userspace copying, we'll get a
1723 kernel pointer here... */
1726 struct video_audio *va = arg;
1728 dprintk(KERN_DEBUG "msp34xx: VIDIOCGAUDIO\n");
1729 va->flags |= VIDEO_AUDIO_VOLUME |
1731 VIDEO_AUDIO_TREBLE |
1732 VIDEO_AUDIO_MUTABLE;
1734 va->flags |= VIDEO_AUDIO_MUTE;
1736 va->volume = msp->volume;
1737 va->balance = (va->volume) ? msp->balance : 32768;
1738 va->bass = msp->bass;
1739 va->treble = msp->treble;
1741 msp_any_detect_stereo(client);
1742 va->mode = mode_v4l2_to_v4l1(msp->rxsubchans);
1747 struct video_audio *va = arg;
1749 dprintk(KERN_DEBUG "msp34xx: VIDIOCSAUDIO\n");
1750 msp->muted = (va->flags & VIDEO_AUDIO_MUTE);
1751 msp->volume = va->volume;
1752 msp->balance = va->balance;
1753 msp->bass = va->bass;
1754 msp->treble = va->treble;
1756 msp3400c_setvolume(client, msp->muted,
1757 msp->volume, msp->balance);
1758 msp3400c_setbass(client,msp->bass);
1759 msp3400c_settreble(client,msp->treble);
1761 if (va->mode != 0 && msp->norm != VIDEO_MODE_RADIO)
1762 msp_any_set_audmode(client,mode_v4l1_to_v4l2(va->mode));
1767 struct video_channel *vc = arg;
1769 dprintk(KERN_DEBUG "msp34xx: VIDIOCSCHAN (norm=%d)\n",vc->norm);
1770 msp->norm = vc->norm;
1771 msp_wake_thread(client);
1776 case VIDIOC_S_FREQUENCY:
1778 /* new channel -- kick audio carrier scan */
1779 dprintk(KERN_DEBUG "msp34xx: VIDIOCSFREQ\n");
1780 msp_wake_thread(client);
1784 /* --- v4l2 ioctls --- */
1785 case VIDIOC_G_TUNER:
1787 struct v4l2_tuner *vt = arg;
1789 msp_any_detect_stereo(client);
1790 vt->audmode = msp->audmode;
1791 vt->rxsubchans = msp->rxsubchans;
1792 vt->capability = V4L2_TUNER_CAP_STEREO |
1793 V4L2_TUNER_CAP_LANG1|
1794 V4L2_TUNER_CAP_LANG2;
1797 case VIDIOC_S_TUNER:
1799 struct v4l2_tuner *vt=(struct v4l2_tuner *)arg;
1801 /* only set audmode */
1802 if (vt->audmode != -1 && vt->audmode != 0)
1803 msp_any_set_audmode(client, vt->audmode);
1807 /* msp34xx specific */
1808 case MSP_SET_MATRIX:
1810 struct msp_matrix *mspm = arg;
1812 dprintk(KERN_DEBUG "msp34xx: MSP_SET_MATRIX\n");
1813 msp3400c_set_scart(client, mspm->input, mspm->output);
1824 static int msp_suspend(struct device * dev, pm_message_t state)
1826 struct i2c_client *c = container_of(dev, struct i2c_client, dev);
1828 dprintk("msp34xx: suspend\n");
1833 static int msp_resume(struct device * dev)
1835 struct i2c_client *c = container_of(dev, struct i2c_client, dev);
1837 dprintk("msp34xx: resume\n");
1842 /* ----------------------------------------------------------------------- */
1844 static int __init msp3400_init_module(void)
1846 return i2c_add_driver(&driver);
1849 static void __exit msp3400_cleanup_module(void)
1851 i2c_del_driver(&driver);
1854 module_init(msp3400_init_module);
1855 module_exit(msp3400_cleanup_module);
1858 * Overrides for Emacs so that we follow Linus's tabbing style.
1859 * ---------------------------------------------------------------------------