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>
59 #define OPMODE_AUTO -1
60 #define OPMODE_MANUAL 0
61 #define OPMODE_SIMPLE 1 /* use short programming (>= msp3410 only) */
62 #define OPMODE_SIMPLER 2 /* use shorter programming (>= msp34xxG) */
64 /* insmod parameters */
65 static int opmode = OPMODE_AUTO;
66 static int debug = 0; /* debug output */
67 static int once = 0; /* no continous stereo monitoring */
68 static int amsound = 0; /* hard-wire AM sound at 6.5 Hz (france),
69 the autoscan seems work well only with FM... */
70 static int standard = 1; /* Override auto detect of audio standard, if needed. */
73 static int stereo_threshold = 0x190; /* a2 threshold for stereo/bilingual
74 (msp34xxg only) 0x00a0-0x03c0 */
75 #define DFP_COUNT 0x41
76 static const int bl_dfp[] = {
77 0x00, 0x01, 0x02, 0x03, 0x06, 0x08, 0x09, 0x0a,
78 0x0b, 0x0d, 0x0e, 0x10
81 #define IS_MSP34XX_G(msp) ((msp)->opmode==2)
93 int main, second; /* sound carrier */
95 int source; /* see msp34xxg_set_source */
102 int left, right; /* volume */
105 /* shadow register set */
106 int dfp_regs[DFP_COUNT];
109 struct task_struct *kthread;
110 wait_queue_head_t wq;
115 #define MIN(a,b) (((a)>(b))?(b):(a))
116 #define MAX(a,b) (((a)>(b))?(a):(b))
117 #define HAVE_NICAM(msp) (((msp->rev2>>8) & 0xff) != 00)
118 #define HAVE_SIMPLE(msp) ((msp->rev1 & 0xff) >= 'D'-'@')
119 #define HAVE_SIMPLER(msp) ((msp->rev1 & 0xff) >= 'G'-'@')
120 #define HAVE_RADIO(msp) ((msp->rev1 & 0xff) >= 'G'-'@')
122 #define VIDEO_MODE_RADIO 16 /* norm magic for radio mode */
124 /* ---------------------------------------------------------------------- */
126 #define dprintk if (debug >= 1) printk
127 #define d2printk if (debug >= 2) printk
128 #define dprintk_trace if (debug>=16) printk
131 module_param(opmode, int, 0444);
134 module_param(once, int, 0644);
135 module_param(debug, int, 0644);
136 module_param(stereo_threshold, int, 0644);
137 module_param(standard, int, 0644);
138 module_param(amsound, int, 0644);
139 module_param(dolby, int, 0644);
141 MODULE_PARM_DESC(opmode, "Forces a MSP3400 opmode. 0=Manual, 1=Simple, 2=Simpler");
142 MODULE_PARM_DESC(once, "No continuous stereo monitoring");
143 MODULE_PARM_DESC(debug, "Enable debug messages");
144 MODULE_PARM_DESC(stereo_threshold, "Sets signal threshold to activate stereo");
145 MODULE_PARM_DESC(standard, "Specify audio standard: 32 = NTSC, 64 = radio, Default: Autodetect");
146 MODULE_PARM_DESC(amsound, "Hardwire AM sound at 6.5Hz (France), FM can autoscan");
147 MODULE_PARM_DESC(dolby, "Activates Dolby processsing");
149 /* ---------------------------------------------------------------------- */
151 #define I2C_MSP3400C 0x80
152 #define I2C_MSP3400C_ALT 0x88
154 #define I2C_MSP3400C_DEM 0x10
155 #define I2C_MSP3400C_DFP 0x12
157 /* Addresses to scan */
158 static unsigned short normal_i2c[] = {
160 I2C_MSP3400C_ALT >> 1,
165 MODULE_DESCRIPTION("device driver for msp34xx TV sound processor");
166 MODULE_AUTHOR("Gerd Knorr");
167 MODULE_LICENSE("GPL");
169 /* ----------------------------------------------------------------------- */
170 /* functions for talking to the MSP3400C Sound processor */
172 static int msp3400c_reset(struct i2c_client *client)
174 /* reset and read revision code */
175 static char reset_off[3] = { 0x00, 0x80, 0x00 };
176 static char reset_on[3] = { 0x00, 0x00, 0x00 };
177 static char write[3] = { I2C_MSP3400C_DFP + 1, 0x00, 0x1e };
179 struct i2c_msg reset[2] = {
180 { client->addr, I2C_M_IGNORE_NAK, 3, reset_off },
181 { client->addr, I2C_M_IGNORE_NAK, 3, reset_on },
183 struct i2c_msg test[2] = {
184 { client->addr, 0, 3, write },
185 { client->addr, I2C_M_RD, 2, read },
188 dprintk_trace("trace: msp3400c_reset\n");
189 if ( (1 != i2c_transfer(client->adapter,&reset[0],1)) ||
190 (1 != i2c_transfer(client->adapter,&reset[1],1)) ||
191 (2 != i2c_transfer(client->adapter,test,2)) ) {
192 printk(KERN_ERR "msp3400: chip reset failed\n");
198 static int msp3400c_read(struct i2c_client *client, int dev, int addr)
202 unsigned char write[3];
203 unsigned char read[2];
204 struct i2c_msg msgs[2] = {
205 { client->addr, 0, 3, write },
206 { client->addr, I2C_M_RD, 2, read }
210 write[1] = addr >> 8;
211 write[2] = addr & 0xff;
213 for (err = 0; err < 3;) {
214 if (2 == i2c_transfer(client->adapter,msgs,2))
218 "msp34xx: I/O error #%d (read 0x%02x/0x%02x)\n", err,
220 current->state = TASK_INTERRUPTIBLE;
221 schedule_timeout(msecs_to_jiffies(10));
225 "msp34xx: giving up, reseting chip. Sound will go off, sorry folks :-|\n");
226 msp3400c_reset(client);
229 retval = read[0] << 8 | read[1];
230 dprintk_trace("trace: msp3400c_read(0x%x, 0x%x): 0x%x\n", dev, addr,
235 static int msp3400c_write(struct i2c_client *client, int dev, int addr, int val)
238 unsigned char buffer[5];
241 buffer[1] = addr >> 8;
242 buffer[2] = addr & 0xff;
243 buffer[3] = val >> 8;
244 buffer[4] = val & 0xff;
246 dprintk_trace("trace: msp3400c_write(0x%x, 0x%x, 0x%x)\n", dev, addr,
248 for (err = 0; err < 3;) {
249 if (5 == i2c_master_send(client, buffer, 5))
253 "msp34xx: I/O error #%d (write 0x%02x/0x%02x)\n", err,
255 current->state = TASK_INTERRUPTIBLE;
256 schedule_timeout(msecs_to_jiffies(10));
260 "msp34xx: giving up, reseting chip. Sound will go off, sorry folks :-|\n");
261 msp3400c_reset(client);
267 /* ------------------------------------------------------------------------ */
269 /* This macro is allowed for *constants* only, gcc must calculate it
270 at compile time. Remember -- no floats in kernel mode */
271 #define MSP_CARRIER(freq) ((int)((float)(freq/18.432)*(1<<24)))
273 #define MSP_MODE_AM_DETECT 0
274 #define MSP_MODE_FM_RADIO 2
275 #define MSP_MODE_FM_TERRA 3
276 #define MSP_MODE_FM_SAT 4
277 #define MSP_MODE_FM_NICAM1 5
278 #define MSP_MODE_FM_NICAM2 6
279 #define MSP_MODE_AM_NICAM 7
280 #define MSP_MODE_BTSC 8
281 #define MSP_MODE_EXTERN 9
283 static struct MSP_INIT_DATA_DEM {
292 } msp_init_data[] = {
293 { /* AM (for carrier detect / msp3400) */
294 {75, 19, 36, 35, 39, 40},
295 {75, 19, 36, 35, 39, 40},
296 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
297 0x00d0, 0x0500, 0x0020, 0x3000
298 },{ /* AM (for carrier detect / msp3410) */
299 {-1, -1, -8, 2, 59, 126},
300 {-1, -1, -8, 2, 59, 126},
301 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
302 0x00d0, 0x0100, 0x0020, 0x3000
304 {-8, -8, 4, 6, 78, 107},
305 {-8, -8, 4, 6, 78, 107},
306 MSP_CARRIER(10.7), MSP_CARRIER(10.7),
307 0x00d0, 0x0480, 0x0020, 0x3000
308 },{ /* Terrestial FM-mono + FM-stereo */
309 {3, 18, 27, 48, 66, 72},
310 {3, 18, 27, 48, 66, 72},
311 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
312 0x00d0, 0x0480, 0x0030, 0x3000
313 },{ /* Sat FM-mono */
314 { 1, 9, 14, 24, 33, 37},
315 { 3, 18, 27, 48, 66, 72},
316 MSP_CARRIER(6.5), MSP_CARRIER(6.5),
317 0x00c6, 0x0480, 0x0000, 0x3000
318 },{ /* NICAM/FM -- B/G (5.5/5.85), D/K (6.5/5.85) */
319 {-2, -8, -10, 10, 50, 86},
320 {3, 18, 27, 48, 66, 72},
321 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
322 0x00d0, 0x0040, 0x0120, 0x3000
323 },{ /* NICAM/FM -- I (6.0/6.552) */
324 {2, 4, -6, -4, 40, 94},
325 {3, 18, 27, 48, 66, 72},
326 MSP_CARRIER(6.0), MSP_CARRIER(6.0),
327 0x00d0, 0x0040, 0x0120, 0x3000
328 },{ /* NICAM/AM -- L (6.5/5.85) */
329 {-2, -8, -10, 10, 50, 86},
330 {-4, -12, -9, 23, 79, 126},
331 MSP_CARRIER(6.5), MSP_CARRIER(6.5),
332 0x00c6, 0x0140, 0x0120, 0x7c03
336 struct CARRIER_DETECT {
341 static struct CARRIER_DETECT carrier_detect_main[] = {
343 { MSP_CARRIER(4.5), "4.5 NTSC" },
344 { MSP_CARRIER(5.5), "5.5 PAL B/G" },
345 { MSP_CARRIER(6.0), "6.0 PAL I" },
346 { MSP_CARRIER(6.5), "6.5 PAL D/K + SAT + SECAM" }
349 static struct CARRIER_DETECT carrier_detect_55[] = {
351 { MSP_CARRIER(5.7421875), "5.742 PAL B/G FM-stereo" },
352 { MSP_CARRIER(5.85), "5.85 PAL B/G NICAM" }
355 static struct CARRIER_DETECT carrier_detect_65[] = {
356 /* PAL SAT / SECAM */
357 { MSP_CARRIER(5.85), "5.85 PAL D/K + SECAM NICAM" },
358 { MSP_CARRIER(6.2578125), "6.25 PAL D/K1 FM-stereo" },
359 { MSP_CARRIER(6.7421875), "6.74 PAL D/K2 FM-stereo" },
360 { MSP_CARRIER(7.02), "7.02 PAL SAT FM-stereo s/b" },
361 { MSP_CARRIER(7.20), "7.20 PAL SAT FM-stereo s" },
362 { MSP_CARRIER(7.38), "7.38 PAL SAT FM-stereo b" },
365 #define CARRIER_COUNT(x) (sizeof(x)/sizeof(struct CARRIER_DETECT))
367 /* ----------------------------------------------------------------------- */
369 static int scarts[3][9] = {
370 /* MASK IN1 IN2 IN1_DA IN2_DA IN3 IN4 MONO MUTE */
371 { 0x0320, 0x0000, 0x0200, -1, -1, 0x0300, 0x0020, 0x0100, 0x0320 },
372 { 0x0c40, 0x0440, 0x0400, 0x0c00, 0x0040, 0x0000, 0x0840, 0x0800, 0x0c40 },
373 { 0x3080, 0x1000, 0x1080, 0x0000, 0x0080, 0x2080, 0x3080, 0x2000, 0x3000 },
376 static char *scart_names[] = {
377 "mask", "in1", "in2", "in1 da", "in2 da", "in3", "in4", "mono", "mute"
380 static void msp3400c_set_scart(struct i2c_client *client, int in, int out)
382 struct msp3400c *msp = i2c_get_clientdata(client);
384 if (-1 == scarts[out][in])
387 dprintk("msp34xx: scart switch: %s => %d\n", scart_names[in], out);
388 msp->acb &= ~scarts[out][SCART_MASK];
389 msp->acb |= scarts[out][in];
390 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0013, msp->acb);
393 /* ------------------------------------------------------------------------ */
395 static void msp3400c_setcarrier(struct i2c_client *client, int cdo1, int cdo2)
397 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0093, cdo1 & 0xfff);
398 msp3400c_write(client,I2C_MSP3400C_DEM, 0x009b, cdo1 >> 12);
399 msp3400c_write(client,I2C_MSP3400C_DEM, 0x00a3, cdo2 & 0xfff);
400 msp3400c_write(client,I2C_MSP3400C_DEM, 0x00ab, cdo2 >> 12);
401 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0056, 0); /*LOAD_REG_1/2*/
404 static void msp3400c_setvolume(struct i2c_client *client,
405 int muted, int left, int right)
407 int vol = 0, val = 0, balance = 0;
410 /* 0x7f instead if 0x73 here has sound quality issues,
411 * probably due to overmodulation + clipping ... */
412 vol = (left > right) ? left : right;
413 val = (vol * 0x73 / 65535) << 8;
416 balance = ((right - left) * 127) / vol;
419 dprintk("msp34xx: setvolume: mute=%s %d:%d v=0x%02x b=0x%02x\n",
420 muted ? "on" : "off", left, right, val >> 8, balance);
421 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0000, val); /* loudspeaker */
422 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0006, val); /* headphones */
423 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0007,
424 muted ? 0x1 : (val | 0x1));
425 msp3400c_write(client, I2C_MSP3400C_DFP, 0x0001, balance << 8);
428 static void msp3400c_setbass(struct i2c_client *client, int bass)
430 int val = ((bass-32768) * 0x60 / 65535) << 8;
432 dprintk("msp34xx: setbass: %d 0x%02x\n", bass, val >> 8);
433 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0002, val); /* loudspeaker */
436 static void msp3400c_settreble(struct i2c_client *client, int treble)
438 int val = ((treble-32768) * 0x60 / 65535) << 8;
440 dprintk("msp34xx: settreble: %d 0x%02x\n",treble, val>>8);
441 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0003, val); /* loudspeaker */
444 static void msp3400c_setmode(struct i2c_client *client, int type)
446 struct msp3400c *msp = i2c_get_clientdata(client);
449 dprintk("msp3400: setmode: %d\n",type);
451 msp->audmode = V4L2_TUNER_MODE_MONO;
452 msp->rxsubchans = V4L2_TUNER_SUB_MONO;
454 msp3400c_write(client,I2C_MSP3400C_DEM, 0x00bb, /* ad_cv */
455 msp_init_data[type].ad_cv);
457 for (i = 5; i >= 0; i--) /* fir 1 */
458 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0001,
459 msp_init_data[type].fir1[i]);
461 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005, 0x0004); /* fir 2 */
462 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005, 0x0040);
463 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005, 0x0000);
464 for (i = 5; i >= 0; i--)
465 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005,
466 msp_init_data[type].fir2[i]);
468 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0083, /* MODE_REG */
469 msp_init_data[type].mode_reg);
471 msp3400c_setcarrier(client, msp_init_data[type].cdo1,
472 msp_init_data[type].cdo2);
474 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0056, 0); /*LOAD_REG_1/2*/
477 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008,
479 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009,
481 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b,
482 msp_init_data[type].dfp_src);
484 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008,
485 msp_init_data[type].dfp_src);
486 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009,
487 msp_init_data[type].dfp_src);
488 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b,
489 msp_init_data[type].dfp_src);
491 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000a,
492 msp_init_data[type].dfp_src);
493 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000e,
494 msp_init_data[type].dfp_matrix);
496 if (HAVE_NICAM(msp)) {
498 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0010, 0x5a00); /* was: 0x3000 */
502 /* given a bitmask of VIDEO_SOUND_XXX returns the "best" in the bitmask */
503 static int best_video_sound(int rxsubchans)
505 if (rxsubchans & V4L2_TUNER_SUB_STEREO)
506 return V4L2_TUNER_MODE_STEREO;
507 if (rxsubchans & V4L2_TUNER_SUB_LANG1)
508 return V4L2_TUNER_MODE_LANG1;
509 if (rxsubchans & V4L2_TUNER_SUB_LANG2)
510 return V4L2_TUNER_MODE_LANG2;
511 return V4L2_TUNER_MODE_MONO;
514 /* turn on/off nicam + stereo */
515 static void msp3400c_setstereo(struct i2c_client *client, int mode)
517 static char *strmode[] = { "0", "mono", "stereo", "3",
518 "lang1", "5", "6", "7", "lang2"
520 struct msp3400c *msp = i2c_get_clientdata(client);
521 int nicam = 0; /* channel source: FM/AM or nicam */
524 if (IS_MSP34XX_G(msp)) {
525 /* this method would break everything, let's make sure
529 ("msp34xxg: DEBUG WARNING setstereo called with mode=%d instead of set_source (ignored)\n",
534 /* switch demodulator */
536 case MSP_MODE_FM_TERRA:
537 dprintk("msp3400: FM setstereo: %s\n",
539 msp3400c_setcarrier(client,msp->second,msp->main);
541 case V4L2_TUNER_MODE_STEREO:
542 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000e, 0x3001);
544 case V4L2_TUNER_MODE_MONO:
545 case V4L2_TUNER_MODE_LANG1:
546 case V4L2_TUNER_MODE_LANG2:
547 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000e, 0x3000);
551 case MSP_MODE_FM_SAT:
552 dprintk("msp3400: SAT setstereo: %s\n", strmode[mode]);
554 case V4L2_TUNER_MODE_MONO:
555 msp3400c_setcarrier(client, MSP_CARRIER(6.5), MSP_CARRIER(6.5));
557 case V4L2_TUNER_MODE_STEREO:
558 msp3400c_setcarrier(client, MSP_CARRIER(7.2), MSP_CARRIER(7.02));
560 case V4L2_TUNER_MODE_LANG1:
561 msp3400c_setcarrier(client, MSP_CARRIER(7.38), MSP_CARRIER(7.02));
563 case V4L2_TUNER_MODE_LANG2:
564 msp3400c_setcarrier(client, MSP_CARRIER(7.38), MSP_CARRIER(7.02));
568 case MSP_MODE_FM_NICAM1:
569 case MSP_MODE_FM_NICAM2:
570 case MSP_MODE_AM_NICAM:
571 dprintk("msp3400: NICAM setstereo: %s\n",strmode[mode]);
572 msp3400c_setcarrier(client,msp->second,msp->main);
577 dprintk("msp3400: BTSC setstereo: %s\n",strmode[mode]);
580 case MSP_MODE_EXTERN:
581 dprintk("msp3400: extern setstereo: %s\n",strmode[mode]);
584 case MSP_MODE_FM_RADIO:
585 dprintk("msp3400: FM-Radio setstereo: %s\n",strmode[mode]);
588 dprintk("msp3400: mono setstereo\n");
593 switch (best_video_sound(mode)) {
594 case V4L2_TUNER_MODE_STEREO:
595 src = 0x0020 | nicam;
597 case V4L2_TUNER_MODE_MONO:
598 if (msp->mode == MSP_MODE_AM_NICAM) {
599 dprintk("msp3400: switching to AM mono\n");
600 /* AM mono decoding is handled by tuner, not MSP chip */
601 /* SCART switching control register */
602 msp3400c_set_scart(client,SCART_MONO,0);
606 case V4L2_TUNER_MODE_LANG1:
607 src = 0x0000 | nicam;
609 case V4L2_TUNER_MODE_LANG2:
610 src = 0x0010 | nicam;
613 dprintk("msp3400: setstereo final source/matrix = 0x%x\n", src);
616 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008,0x0520);
617 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009,0x0620);
618 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000a,src);
619 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b,src);
621 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008,src);
622 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009,src);
623 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000a,src);
624 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b,src);
629 msp3400c_print_mode(struct msp3400c *msp)
631 if (msp->main == msp->second) {
632 dprintk("msp3400: mono sound carrier: %d.%03d MHz\n",
633 msp->main/910000,(msp->main/910)%1000);
635 dprintk("msp3400: main sound carrier: %d.%03d MHz\n",
636 msp->main/910000,(msp->main/910)%1000);
638 if (msp->mode == MSP_MODE_FM_NICAM1 || msp->mode == MSP_MODE_FM_NICAM2)
639 dprintk("msp3400: NICAM/FM carrier : %d.%03d MHz\n",
640 msp->second/910000,(msp->second/910)%1000);
641 if (msp->mode == MSP_MODE_AM_NICAM)
642 dprintk("msp3400: NICAM/AM carrier : %d.%03d MHz\n",
643 msp->second/910000,(msp->second/910)%1000);
644 if (msp->mode == MSP_MODE_FM_TERRA &&
645 msp->main != msp->second) {
646 dprintk("msp3400: FM-stereo carrier : %d.%03d MHz\n",
647 msp->second/910000,(msp->second/910)%1000);
651 #define MSP3400_MAX 4
652 static struct i2c_client *msps[MSP3400_MAX];
653 static void msp3400c_restore_dfp(struct i2c_client *client)
655 struct msp3400c *msp = i2c_get_clientdata(client);
658 for (i = 0; i < DFP_COUNT; i++) {
659 if (-1 == msp->dfp_regs[i])
661 msp3400c_write(client, I2C_MSP3400C_DFP, i, msp->dfp_regs[i]);
665 /* if the dfp_regs is set, set what's in there. Otherwise, set the default value */
666 static int msp3400c_write_dfp_with_default(struct i2c_client *client,
667 int addr, int default_value)
669 struct msp3400c *msp = i2c_get_clientdata(client);
670 int value = default_value;
671 if (addr < DFP_COUNT && -1 != msp->dfp_regs[addr])
672 value = msp->dfp_regs[addr];
673 return msp3400c_write(client, I2C_MSP3400C_DFP, addr, value);
676 /* ----------------------------------------------------------------------- */
678 struct REGISTER_DUMP {
683 struct REGISTER_DUMP d1[] = {
684 {0x007e, "autodetect"},
685 {0x0023, "C_AD_BITS "},
686 {0x0038, "ADD_BITS "},
687 {0x003e, "CIB_BITS "},
688 {0x0057, "ERROR_RATE"},
691 static int autodetect_stereo(struct i2c_client *client)
693 struct msp3400c *msp = i2c_get_clientdata(client);
695 int rxsubchans = msp->rxsubchans;
696 int newnicam = msp->nicam_on;
700 case MSP_MODE_FM_TERRA:
701 val = msp3400c_read(client, I2C_MSP3400C_DFP, 0x18);
704 dprintk("msp34xx: stereo detect register: %d\n",val);
706 rxsubchans = V4L2_TUNER_SUB_STEREO | V4L2_TUNER_SUB_MONO;
707 } else if (val < -4096) {
708 rxsubchans = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
710 rxsubchans = V4L2_TUNER_SUB_MONO;
714 case MSP_MODE_FM_NICAM1:
715 case MSP_MODE_FM_NICAM2:
716 case MSP_MODE_AM_NICAM:
717 val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x23);
718 dprintk("msp34xx: nicam sync=%d, mode=%d\n",
719 val & 1, (val & 0x1e) >> 1);
723 switch ((val & 0x1e) >> 1) {
726 rxsubchans = V4L2_TUNER_SUB_STEREO;
730 rxsubchans = V4L2_TUNER_SUB_MONO
731 | V4L2_TUNER_SUB_LANG1;
735 rxsubchans = V4L2_TUNER_SUB_MONO
736 | V4L2_TUNER_SUB_LANG1
737 | V4L2_TUNER_SUB_LANG2;
740 rxsubchans = V4L2_TUNER_SUB_MONO;
746 rxsubchans = V4L2_TUNER_SUB_MONO;
750 val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x200);
751 dprintk("msp3410: status=0x%x (pri=%s, sec=%s, %s%s%s)\n",
753 (val & 0x0002) ? "no" : "yes",
754 (val & 0x0004) ? "no" : "yes",
755 (val & 0x0040) ? "stereo" : "mono",
756 (val & 0x0080) ? ", nicam 2nd mono" : "",
757 (val & 0x0100) ? ", bilingual/SAP" : "");
758 rxsubchans = V4L2_TUNER_SUB_MONO;
759 if (val & 0x0040) rxsubchans |= V4L2_TUNER_SUB_STEREO;
760 if (val & 0x0100) rxsubchans |= V4L2_TUNER_SUB_LANG1;
763 if (rxsubchans != msp->rxsubchans) {
765 dprintk("msp34xx: watch: rxsubchans %d => %d\n",
766 msp->rxsubchans,rxsubchans);
767 msp->rxsubchans = rxsubchans;
769 if (newnicam != msp->nicam_on) {
771 dprintk("msp34xx: watch: nicam %d => %d\n",
772 msp->nicam_on,newnicam);
773 msp->nicam_on = newnicam;
779 * A kernel thread for msp3400 control -- we don't want to block the
780 * in the ioctl while doing the sound carrier & stereo detect
783 static int msp34xx_sleep(struct msp3400c *msp, int timeout)
785 DECLARE_WAITQUEUE(wait, current);
787 add_wait_queue(&msp->wq, &wait);
788 if (!kthread_should_stop()) {
790 set_current_state(TASK_INTERRUPTIBLE);
793 schedule_timeout_interruptible
794 (msecs_to_jiffies(timeout));
798 remove_wait_queue(&msp->wq, &wait);
803 /* stereo/multilang monitoring */
804 static void watch_stereo(struct i2c_client *client)
806 struct msp3400c *msp = i2c_get_clientdata(client);
808 if (autodetect_stereo(client)) {
809 if (msp->stereo & V4L2_TUNER_MODE_STEREO)
810 msp3400c_setstereo(client, V4L2_TUNER_MODE_STEREO);
811 else if (msp->stereo & VIDEO_SOUND_LANG1)
812 msp3400c_setstereo(client, V4L2_TUNER_MODE_LANG1);
814 msp3400c_setstereo(client, V4L2_TUNER_MODE_MONO);
818 msp->watch_stereo = 0;
821 static int msp3400c_thread(void *data)
823 struct i2c_client *client = data;
824 struct msp3400c *msp = i2c_get_clientdata(client);
825 struct CARRIER_DETECT *cd;
826 int count, max1,max2,val1,val2, val,this;
828 printk("msp3400: kthread started\n");
830 d2printk("msp3400: thread: sleep\n");
831 msp34xx_sleep(msp,-1);
832 d2printk("msp3400: thread: wakeup\n");
835 dprintk("msp3410: thread: restart scan\n");
837 if (kthread_should_stop())
840 if (VIDEO_MODE_RADIO == msp->norm ||
841 MSP_MODE_EXTERN == msp->mode) {
842 /* no carrier scan, just unmute */
843 printk("msp3400: thread: no carrier scan\n");
844 msp3400c_setvolume(client, msp->muted, msp->left, msp->right);
849 msp3400c_setvolume(client, msp->muted, 0, 0);
850 msp3400c_setmode(client, MSP_MODE_AM_DETECT /* +1 */ );
853 msp->watch_stereo = 0;
855 /* some time for the tuner to sync */
856 if (msp34xx_sleep(msp,200))
859 /* carrier detect pass #1 -- main carrier */
860 cd = carrier_detect_main;
861 count = CARRIER_COUNT(carrier_detect_main);
863 if (amsound && (msp->norm == VIDEO_MODE_SECAM)) {
864 /* autodetect doesn't work well with AM ... */
867 dprintk("msp3400: AM sound override\n");
870 for (this = 0; this < count; this++) {
871 msp3400c_setcarrier(client, cd[this].cdo,cd[this].cdo);
872 if (msp34xx_sleep(msp,100))
874 val = msp3400c_read(client, I2C_MSP3400C_DFP, 0x1b);
878 val1 = val, max1 = this;
879 dprintk("msp3400: carrier1 val: %5d / %s\n", val,cd[this].name);
882 /* carrier detect pass #2 -- second (stereo) carrier */
885 cd = carrier_detect_55;
886 count = CARRIER_COUNT(carrier_detect_55);
889 cd = carrier_detect_65;
890 count = CARRIER_COUNT(carrier_detect_65);
900 if (amsound && (msp->norm == VIDEO_MODE_SECAM)) {
901 /* autodetect doesn't work well with AM ... */
906 for (this = 0; this < count; this++) {
907 msp3400c_setcarrier(client, cd[this].cdo,cd[this].cdo);
908 if (msp34xx_sleep(msp,100))
910 val = msp3400c_read(client, I2C_MSP3400C_DFP, 0x1b);
914 val2 = val, max2 = this;
915 dprintk("msp3400: carrier2 val: %5d / %s\n", val,cd[this].name);
918 /* programm the msp3400 according to the results */
919 msp->main = carrier_detect_main[max1].cdo;
924 msp->second = carrier_detect_55[max2].cdo;
925 msp3400c_setmode(client, MSP_MODE_FM_TERRA);
927 msp3400c_setstereo(client, V4L2_TUNER_MODE_MONO);
928 msp->watch_stereo = 1;
929 } else if (max2 == 1 && HAVE_NICAM(msp)) {
931 msp->second = carrier_detect_55[max2].cdo;
932 msp3400c_setmode(client, MSP_MODE_FM_NICAM1);
934 msp3400c_setcarrier(client, msp->second, msp->main);
935 msp->watch_stereo = 1;
942 msp->second = MSP_CARRIER(6.552);
943 msp3400c_setmode(client, MSP_MODE_FM_NICAM2);
945 msp3400c_setcarrier(client, msp->second, msp->main);
946 msp->watch_stereo = 1;
949 if (max2 == 1 || max2 == 2) {
951 msp->second = carrier_detect_65[max2].cdo;
952 msp3400c_setmode(client, MSP_MODE_FM_TERRA);
954 msp3400c_setstereo(client, V4L2_TUNER_MODE_MONO);
955 msp->watch_stereo = 1;
956 } else if (max2 == 0 &&
957 msp->norm == VIDEO_MODE_SECAM) {
958 /* L NICAM or AM-mono */
959 msp->second = carrier_detect_65[max2].cdo;
960 msp3400c_setmode(client, MSP_MODE_AM_NICAM);
962 msp3400c_setstereo(client, V4L2_TUNER_MODE_MONO);
963 msp3400c_setcarrier(client, msp->second, msp->main);
964 /* volume prescale for SCART (AM mono input) */
965 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000d, 0x1900);
966 msp->watch_stereo = 1;
967 } else if (max2 == 0 && HAVE_NICAM(msp)) {
969 msp->second = carrier_detect_65[max2].cdo;
970 msp3400c_setmode(client, MSP_MODE_FM_NICAM1);
972 msp3400c_setcarrier(client, msp->second, msp->main);
973 msp->watch_stereo = 1;
981 msp->second = carrier_detect_main[max1].cdo;
982 msp3400c_setmode(client, MSP_MODE_FM_TERRA);
984 msp3400c_setcarrier(client, msp->second, msp->main);
985 msp->rxsubchans = V4L2_TUNER_SUB_MONO;
986 msp3400c_setstereo(client, V4L2_TUNER_MODE_MONO);
991 msp3400c_setvolume(client, msp->muted, msp->left, msp->right);
992 msp3400c_restore_dfp(client);
995 msp3400c_print_mode(msp);
997 /* monitor tv audio mode */
998 while (msp->watch_stereo) {
999 if (msp34xx_sleep(msp,5000))
1001 watch_stereo(client);
1004 dprintk("msp3400: thread: exit\n");
1008 /* ----------------------------------------------------------------------- */
1009 /* this one uses the automatic sound standard detection of newer */
1010 /* msp34xx chip versions */
1012 static struct MODES {
1017 { 0x0000, 0, 0, "ERROR" },
1018 { 0x0001, 0, 0, "autodetect start" },
1019 { 0x0002, MSP_CARRIER(4.5), MSP_CARRIER(4.72), "4.5/4.72 M Dual FM-Stereo" },
1020 { 0x0003, MSP_CARRIER(5.5), MSP_CARRIER(5.7421875), "5.5/5.74 B/G Dual FM-Stereo" },
1021 { 0x0004, MSP_CARRIER(6.5), MSP_CARRIER(6.2578125), "6.5/6.25 D/K1 Dual FM-Stereo" },
1022 { 0x0005, MSP_CARRIER(6.5), MSP_CARRIER(6.7421875), "6.5/6.74 D/K2 Dual FM-Stereo" },
1023 { 0x0006, MSP_CARRIER(6.5), MSP_CARRIER(6.5), "6.5 D/K FM-Mono (HDEV3)" },
1024 { 0x0008, MSP_CARRIER(5.5), MSP_CARRIER(5.85), "5.5/5.85 B/G NICAM FM" },
1025 { 0x0009, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85 L NICAM AM" },
1026 { 0x000a, MSP_CARRIER(6.0), MSP_CARRIER(6.55), "6.0/6.55 I NICAM FM" },
1027 { 0x000b, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85 D/K NICAM FM" },
1028 { 0x000c, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85 D/K NICAM FM (HDEV2)" },
1029 { 0x0020, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5 M BTSC-Stereo" },
1030 { 0x0021, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5 M BTSC-Mono + SAP" },
1031 { 0x0030, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5 M EIA-J Japan Stereo" },
1032 { 0x0040, MSP_CARRIER(10.7), MSP_CARRIER(10.7), "10.7 FM-Stereo Radio" },
1033 { 0x0050, MSP_CARRIER(6.5), MSP_CARRIER(6.5), "6.5 SAT-Mono" },
1034 { 0x0051, MSP_CARRIER(7.02), MSP_CARRIER(7.20), "7.02/7.20 SAT-Stereo" },
1035 { 0x0060, MSP_CARRIER(7.2), MSP_CARRIER(7.2), "7.2 SAT ADR" },
1036 { -1, 0, 0, NULL }, /* EOF */
1039 static inline const char *msp34xx_standard_mode_name(int mode)
1042 for (i = 0; modelist[i].name != NULL; i++)
1043 if (modelist[i].retval == mode)
1044 return modelist[i].name;
1048 static int msp34xx_modus(int norm)
1051 case VIDEO_MODE_PAL:
1052 dprintk("msp34xx: video mode selected to PAL\n");
1055 /* experimental: not sure this works with all chip versions */
1058 /* previous value, try this if it breaks ... */
1061 case VIDEO_MODE_NTSC: /* BTSC */
1062 dprintk("msp34xx: video mode selected to NTSC\n");
1064 case VIDEO_MODE_SECAM:
1065 dprintk("msp34xx: video mode selected to SECAM\n");
1067 case VIDEO_MODE_RADIO:
1068 dprintk("msp34xx: video mode selected to Radio\n");
1070 case VIDEO_MODE_AUTO:
1071 dprintk("msp34xx: video mode selected to Auto\n");
1078 static int msp34xx_standard(int norm)
1081 case VIDEO_MODE_PAL:
1083 case VIDEO_MODE_NTSC: /* BTSC */
1085 case VIDEO_MODE_SECAM:
1087 case VIDEO_MODE_RADIO:
1094 static int msp3410d_thread(void *data)
1096 struct i2c_client *client = data;
1097 struct msp3400c *msp = i2c_get_clientdata(client);
1100 printk("msp3410: daemon started\n");
1102 d2printk("msp3410: thread: sleep\n");
1103 msp34xx_sleep(msp,-1);
1104 d2printk("msp3410: thread: wakeup\n");
1107 dprintk("msp3410: thread: restart scan\n");
1109 if (kthread_should_stop())
1112 if (msp->mode == MSP_MODE_EXTERN) {
1113 /* no carrier scan needed, just unmute */
1114 dprintk("msp3410: thread: no carrier scan\n");
1115 msp3400c_setvolume(client, msp->muted, msp->left, msp->right);
1119 /* put into sane state (and mute) */
1120 msp3400c_reset(client);
1122 /* some time for the tuner to sync */
1123 if (msp34xx_sleep(msp,200))
1126 /* start autodetect */
1127 mode = msp34xx_modus(msp->norm);
1128 std = msp34xx_standard(msp->norm);
1129 msp3400c_write(client, I2C_MSP3400C_DEM, 0x30, mode);
1130 msp3400c_write(client, I2C_MSP3400C_DEM, 0x20, std);
1131 msp->watch_stereo = 0;
1134 dprintk("msp3410: setting mode: %s (0x%04x)\n",
1135 msp34xx_standard_mode_name(std) ,std);
1138 /* programmed some specific mode */
1141 /* triggered autodetect */
1143 if (msp34xx_sleep(msp,100))
1147 val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x7e);
1150 dprintk("msp3410: detection still in progress\n");
1153 for (i = 0; modelist[i].name != NULL; i++)
1154 if (modelist[i].retval == val)
1156 dprintk("msp3410: current mode: %s (0x%04x)\n",
1157 modelist[i].name ? modelist[i].name : "unknown",
1159 msp->main = modelist[i].main;
1160 msp->second = modelist[i].second;
1162 if (amsound && (msp->norm == VIDEO_MODE_SECAM) && (val != 0x0009)) {
1163 /* autodetection has failed, let backup */
1164 dprintk("msp3410: autodetection failed,"
1165 " switching to backup mode: %s (0x%04x)\n",
1166 modelist[8].name ? modelist[8].name : "unknown",val);
1168 msp3400c_write(client, I2C_MSP3400C_DEM, 0x20, val);
1171 /* set various prescales */
1172 msp3400c_write(client, I2C_MSP3400C_DFP, 0x0d, 0x1900); /* scart */
1173 msp3400c_write(client, I2C_MSP3400C_DFP, 0x0e, 0x2403); /* FM */
1174 msp3400c_write(client, I2C_MSP3400C_DFP, 0x10, 0x5a00); /* nicam */
1178 case 0x0008: /* B/G NICAM */
1179 case 0x000a: /* I NICAM */
1181 msp->mode = MSP_MODE_FM_NICAM1;
1183 msp->mode = MSP_MODE_FM_NICAM2;
1184 /* just turn on stereo */
1185 msp->rxsubchans = V4L2_TUNER_SUB_STEREO;
1187 msp->watch_stereo = 1;
1188 msp3400c_setstereo(client,V4L2_TUNER_MODE_STEREO);
1191 msp->mode = MSP_MODE_AM_NICAM;
1192 msp->rxsubchans = V4L2_TUNER_SUB_MONO;
1194 msp3400c_setstereo(client,V4L2_TUNER_MODE_MONO);
1195 msp->watch_stereo = 1;
1197 case 0x0020: /* BTSC */
1198 /* just turn on stereo */
1199 msp->mode = MSP_MODE_BTSC;
1200 msp->rxsubchans = V4L2_TUNER_SUB_STEREO;
1202 msp->watch_stereo = 1;
1203 msp3400c_setstereo(client,V4L2_TUNER_MODE_STEREO);
1205 case 0x0040: /* FM radio */
1206 msp->mode = MSP_MODE_FM_RADIO;
1207 msp->rxsubchans = V4L2_TUNER_SUB_STEREO;
1208 msp->audmode = V4L2_TUNER_MODE_STEREO;
1210 msp->watch_stereo = 0;
1211 /* not needed in theory if HAVE_RADIO(), but
1212 short programming enables carrier mute */
1213 msp3400c_setmode(client,MSP_MODE_FM_RADIO);
1214 msp3400c_setcarrier(client, MSP_CARRIER(10.7),
1217 msp3400c_set_scart(client,SCART_IN2,0);
1218 /* msp34xx does radio decoding */
1219 msp3400c_write(client,I2C_MSP3400C_DFP, 0x08, 0x0020);
1220 msp3400c_write(client,I2C_MSP3400C_DFP, 0x09, 0x0020);
1221 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0b, 0x0020);
1226 msp->mode = MSP_MODE_FM_TERRA;
1227 msp->rxsubchans = V4L2_TUNER_SUB_MONO;
1228 msp->audmode = V4L2_TUNER_MODE_MONO;
1230 msp->watch_stereo = 1;
1234 /* unmute, restore misc registers */
1235 msp3400c_setbass(client, msp->bass);
1236 msp3400c_settreble(client, msp->treble);
1237 msp3400c_setvolume(client, msp->muted, msp->left, msp->right);
1238 msp3400c_write(client, I2C_MSP3400C_DFP, 0x0013, msp->acb);
1239 msp3400c_restore_dfp(client);
1241 /* monitor tv audio mode */
1242 while (msp->watch_stereo) {
1243 if (msp34xx_sleep(msp,5000))
1245 watch_stereo(client);
1248 dprintk("msp3410: thread: exit\n");
1252 /* ----------------------------------------------------------------------- */
1253 /* msp34xxG + (simpler no-thread) */
1254 /* this one uses both automatic standard detection and automatic sound */
1255 /* select which are available in the newer G versions */
1256 /* struct msp: only norm, acb and source are really used in this mode */
1258 static void msp34xxg_set_source(struct i2c_client *client, int source);
1260 /* (re-)initialize the msp34xxg, according to the current norm in msp->norm
1261 * return 0 if it worked, -1 if it failed
1263 static int msp34xxg_reset(struct i2c_client *client)
1265 struct msp3400c *msp = i2c_get_clientdata(client);
1268 if (msp3400c_reset(client))
1271 /* make sure that input/output is muted (paranoid mode) */
1272 if (msp3400c_write(client,
1275 0x0f20 /* mute DSP input, mute SCART 1 */))
1278 /* step-by-step initialisation, as described in the manual */
1279 modus = msp34xx_modus(msp->norm);
1280 std = msp34xx_standard(msp->norm);
1281 modus &= ~0x03; /* STATUS_CHANGE=0 */
1282 modus |= 0x01; /* AUTOMATIC_SOUND_DETECTION=1 */
1283 if (msp3400c_write(client,
1288 if (msp3400c_write(client,
1294 /* write the dfps that may have an influence on
1295 standard/audio autodetection right now */
1296 msp34xxg_set_source(client, msp->source);
1298 if (msp3400c_write_dfp_with_default(client, 0x0e, /* AM/FM Prescale */
1300 /* default: [15:8] 75khz deviation */
1304 if (msp3400c_write_dfp_with_default(client, 0x10, /* NICAM Prescale */
1306 /* default: 9db gain (as recommended) */
1313 static int msp34xxg_thread(void *data)
1315 struct i2c_client *client = data;
1316 struct msp3400c *msp = i2c_get_clientdata(client);
1319 printk("msp34xxg: daemon started\n");
1320 msp->source = 1; /* default */
1322 d2printk("msp34xxg: thread: sleep\n");
1323 msp34xx_sleep(msp,-1);
1324 d2printk("msp34xxg: thread: wakeup\n");
1327 dprintk("msp34xxg: thread: restart scan\n");
1329 if (kthread_should_stop())
1333 msp34xxg_reset(client);
1338 /* watch autodetect */
1339 dprintk("msp34xxg: triggered autodetect, waiting for result\n");
1340 for (i = 0; i < 10; i++) {
1341 if (msp34xx_sleep(msp,100))
1345 val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x7e);
1350 dprintk("msp34xxg: detection still in progress\n");
1353 dprintk("msp34xxg: detection still in progress after 10 tries. giving up.\n");
1358 dprintk("msp34xxg: current mode: %s (0x%04x)\n",
1359 msp34xx_standard_mode_name(std), std);
1361 /* unmute: dispatch sound to scart output, set scart volume */
1362 dprintk("msp34xxg: unmute\n");
1364 msp3400c_setbass(client, msp->bass);
1365 msp3400c_settreble(client, msp->treble);
1366 msp3400c_setvolume(client, msp->muted, msp->left, msp->right);
1369 if (msp3400c_write(client,
1375 dprintk("msp34xxg: thread: exit\n");
1379 /* set the same 'source' for the loudspeaker, scart and quasi-peak detector
1380 * the value for source is the same as bit 15:8 of DFP registers 0x08,
1381 * 0x0a and 0x0c: 0=mono, 1=stereo or A|B, 2=SCART, 3=stereo or A, 4=stereo or B
1383 * this function replaces msp3400c_setstereo
1385 static void msp34xxg_set_source(struct i2c_client *client, int source)
1387 struct msp3400c *msp = i2c_get_clientdata(client);
1389 /* fix matrix mode to stereo and let the msp choose what
1390 * to output according to 'source', as recommended
1391 * for MONO (source==0) downmixing set bit[7:0] to 0x30
1393 int value = (source&0x07)<<8|(source==0 ? 0x30:0x20);
1394 dprintk("msp34xxg: set source to %d (0x%x)\n", source, value);
1395 msp3400c_write(client,
1397 0x08, /* Loudspeaker Output */
1399 msp3400c_write(client,
1401 0x0a, /* SCART1 DA Output */
1403 msp3400c_write(client,
1405 0x0c, /* Quasi-peak detector */
1408 * set identification threshold. Personally, I
1409 * I set it to a higher value that the default
1410 * of 0x190 to ignore noisy stereo signals.
1411 * this needs tuning. (recommended range 0x00a0-0x03c0)
1412 * 0x7f0 = forced mono mode
1414 msp3400c_write(client,
1416 0x22, /* a2 threshold for stereo/bilingual */
1421 static void msp34xxg_detect_stereo(struct i2c_client *client)
1423 struct msp3400c *msp = i2c_get_clientdata(client);
1425 int status = msp3400c_read(client,
1427 0x0200 /* STATUS */);
1428 int is_bilingual = status&0x100;
1429 int is_stereo = status&0x40;
1431 msp->rxsubchans = 0;
1433 msp->rxsubchans |= V4L2_TUNER_SUB_STEREO;
1435 msp->rxsubchans |= V4L2_TUNER_SUB_MONO;
1437 msp->rxsubchans |= V4L2_TUNER_SUB_LANG1|V4L2_TUNER_SUB_LANG2;
1438 /* I'm supposed to check whether it's SAP or not
1439 * and set only LANG2/SAP in this case. Yet, the MSP
1440 * does a lot of work to hide this and handle everything
1441 * the same way. I don't want to work around it so unless
1442 * this is a problem, I'll handle SAP just like lang1/lang2.
1445 dprintk("msp34xxg: status=0x%x, stereo=%d, bilingual=%d -> rxsubchans=%d\n",
1446 status, is_stereo, is_bilingual, msp->rxsubchans);
1449 static void msp34xxg_set_audmode(struct i2c_client *client, int audmode)
1451 struct msp3400c *msp = i2c_get_clientdata(client);
1455 case V4L2_TUNER_MODE_MONO:
1456 source=0; /* mono only */
1458 case V4L2_TUNER_MODE_STEREO:
1459 source=1; /* stereo or A|B, see comment in msp34xxg_get_v4l2_stereo() */
1460 /* problem: that could also mean 2 (scart input) */
1462 case V4L2_TUNER_MODE_LANG1:
1463 source=3; /* stereo or A */
1465 case V4L2_TUNER_MODE_LANG2:
1466 source=4; /* stereo or B */
1473 msp->audmode = audmode;
1474 msp34xxg_set_source(client, source);
1478 /* ----------------------------------------------------------------------- */
1480 static int msp_attach(struct i2c_adapter *adap, int addr, int kind);
1481 static int msp_detach(struct i2c_client *client);
1482 static int msp_probe(struct i2c_adapter *adap);
1483 static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg);
1485 static int msp_suspend(struct device * dev, pm_message_t state);
1486 static int msp_resume(struct device * dev);
1488 static void msp_wake_thread(struct i2c_client *client);
1490 static struct i2c_driver driver = {
1491 .owner = THIS_MODULE,
1492 .name = "i2c msp3400 driver",
1493 .id = I2C_DRIVERID_MSP3400,
1494 .flags = I2C_DF_NOTIFY,
1495 .attach_adapter = msp_probe,
1496 .detach_client = msp_detach,
1497 .command = msp_command,
1499 .suspend = msp_suspend,
1500 .resume = msp_resume,
1504 static struct i2c_client client_template =
1507 .flags = I2C_CLIENT_ALLOW_USE,
1511 static int msp_attach(struct i2c_adapter *adap, int addr, int kind)
1513 struct msp3400c *msp;
1514 struct i2c_client *c;
1515 int (*thread_func)(void *data) = NULL;
1518 client_template.adapter = adap;
1519 client_template.addr = addr;
1521 if (-1 == msp3400c_reset(&client_template)) {
1522 dprintk("msp34xx: no chip found\n");
1526 if (NULL == (c = kmalloc(sizeof(struct i2c_client),GFP_KERNEL)))
1528 memcpy(c,&client_template,sizeof(struct i2c_client));
1529 if (NULL == (msp = kmalloc(sizeof(struct msp3400c),GFP_KERNEL))) {
1534 memset(msp,0,sizeof(struct msp3400c));
1535 msp->norm = VIDEO_MODE_NTSC;
1536 msp->left = 58880; /* 0db gain */
1537 msp->right = 58880; /* 0db gain */
1539 msp->treble = 32768;
1542 for (i = 0; i < DFP_COUNT; i++)
1543 msp->dfp_regs[i] = -1;
1545 i2c_set_clientdata(c, msp);
1546 init_waitqueue_head(&msp->wq);
1548 if (-1 == msp3400c_reset(c)) {
1551 dprintk("msp34xx: no chip found\n");
1555 msp->rev1 = msp3400c_read(c, I2C_MSP3400C_DFP, 0x1e);
1556 if (-1 != msp->rev1)
1557 msp->rev2 = msp3400c_read(c, I2C_MSP3400C_DFP, 0x1f);
1558 if ((-1 == msp->rev1) || (0 == msp->rev1 && 0 == msp->rev2)) {
1561 dprintk("msp34xx: error while reading chip version\n");
1564 printk(KERN_INFO "msp34xx: rev1=0x%04x, rev2=0x%04x\n", msp->rev1, msp->rev2);
1566 msp3400c_setvolume(c, msp->muted, msp->left, msp->right);
1568 snprintf(c->name, sizeof(c->name), "MSP34%02d%c-%c%d",
1569 (msp->rev2>>8)&0xff, (msp->rev1&0xff)+'@',
1570 ((msp->rev1>>8)&0xff)+'@', msp->rev2&0x1f);
1572 msp->opmode = opmode;
1573 if (OPMODE_AUTO == msp->opmode) {
1574 if (HAVE_SIMPLER(msp))
1575 msp->opmode = OPMODE_SIMPLER;
1576 else if (HAVE_SIMPLE(msp))
1577 msp->opmode = OPMODE_SIMPLE;
1579 msp->opmode = OPMODE_MANUAL;
1582 /* hello world :-) */
1583 printk(KERN_INFO "msp34xx: init: chip=%s", c->name);
1584 if (HAVE_NICAM(msp))
1586 if (HAVE_SIMPLE(msp))
1588 if (HAVE_SIMPLER(msp))
1589 printk(" +simpler");
1590 if (HAVE_RADIO(msp))
1593 /* version-specific initialization */
1594 switch (msp->opmode) {
1596 printk(" mode=manual");
1597 thread_func = msp3400c_thread;
1600 printk(" mode=simple");
1601 thread_func = msp3410d_thread;
1603 case OPMODE_SIMPLER:
1604 printk(" mode=simpler");
1605 thread_func = msp34xxg_thread;
1610 /* startup control thread if needed */
1612 msp->kthread = kthread_run(thread_func, c, "msp34xx");
1614 if (NULL == msp->kthread)
1615 printk(KERN_WARNING "msp34xx: kernel_thread() failed\n");
1620 i2c_attach_client(c);
1622 /* update our own array */
1623 for (i = 0; i < MSP3400_MAX; i++) {
1624 if (NULL == msps[i]) {
1633 static int msp_detach(struct i2c_client *client)
1635 struct msp3400c *msp = i2c_get_clientdata(client);
1638 /* shutdown control thread */
1641 kthread_stop(msp->kthread);
1643 msp3400c_reset(client);
1645 /* update our own array */
1646 for (i = 0; i < MSP3400_MAX; i++) {
1647 if (client == msps[i]) {
1653 i2c_detach_client(client);
1660 static int msp_probe(struct i2c_adapter *adap)
1662 if (adap->class & I2C_CLASS_TV_ANALOG)
1663 return i2c_probe(adap, &addr_data, msp_attach);
1667 static void msp_wake_thread(struct i2c_client *client)
1669 struct msp3400c *msp = i2c_get_clientdata(client);
1671 if (NULL == msp->kthread)
1673 msp3400c_setvolume(client,msp->muted,0,0);
1674 msp->watch_stereo = 0;
1676 wake_up_interruptible(&msp->wq);
1679 /* ----------------------------------------------------------------------- */
1681 static int mode_v4l2_to_v4l1(int rxsubchans)
1685 if (rxsubchans & V4L2_TUNER_SUB_STEREO)
1686 mode |= VIDEO_SOUND_STEREO;
1687 if (rxsubchans & V4L2_TUNER_SUB_LANG2)
1688 mode |= VIDEO_SOUND_LANG2;
1689 if (rxsubchans & V4L2_TUNER_SUB_LANG1)
1690 mode |= VIDEO_SOUND_LANG1;
1692 mode |= VIDEO_SOUND_MONO;
1696 static int mode_v4l1_to_v4l2(int mode)
1698 if (mode & VIDEO_SOUND_STEREO)
1699 return V4L2_TUNER_MODE_STEREO;
1700 if (mode & VIDEO_SOUND_LANG2)
1701 return V4L2_TUNER_MODE_LANG2;
1702 if (mode & VIDEO_SOUND_LANG1)
1703 return V4L2_TUNER_MODE_LANG1;
1704 return V4L2_TUNER_MODE_MONO;
1707 static void msp_any_detect_stereo(struct i2c_client *client)
1709 struct msp3400c *msp = i2c_get_clientdata(client);
1711 switch (msp->opmode) {
1714 autodetect_stereo(client);
1716 case OPMODE_SIMPLER:
1717 msp34xxg_detect_stereo(client);
1722 static void msp_any_set_audmode(struct i2c_client *client, int audmode)
1724 struct msp3400c *msp = i2c_get_clientdata(client);
1726 switch (msp->opmode) {
1729 msp->watch_stereo = 0;
1730 msp3400c_setstereo(client, audmode);
1732 case OPMODE_SIMPLER:
1733 msp34xxg_set_audmode(client, audmode);
1738 static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg)
1740 struct msp3400c *msp = i2c_get_clientdata(client);
1746 case AUDC_SET_INPUT:
1747 dprintk("msp34xx: AUDC_SET_INPUT(%d)\n",*sarg);
1748 if (*sarg == msp->input)
1753 /* Hauppauge uses IN2 for the radio */
1754 msp->mode = MSP_MODE_FM_RADIO;
1757 case AUDIO_EXTERN_1:
1758 /* IN1 is often used for external input ... */
1759 msp->mode = MSP_MODE_EXTERN;
1762 case AUDIO_EXTERN_2:
1763 /* ... sometimes it is IN2 through ;) */
1764 msp->mode = MSP_MODE_EXTERN;
1771 if (*sarg & AUDIO_MUTE)
1772 msp3400c_set_scart(client,SCART_MUTE,0);
1776 msp->rxsubchans = V4L2_TUNER_SUB_STEREO;
1777 msp->audmode = V4L2_TUNER_MODE_STEREO;
1778 msp3400c_set_scart(client,scart,0);
1779 msp3400c_write(client,I2C_MSP3400C_DFP,0x000d,0x1900);
1780 if (msp->opmode != OPMODE_SIMPLER)
1781 msp3400c_setstereo(client, msp->audmode);
1783 msp_wake_thread(client);
1786 case AUDC_SET_RADIO:
1787 dprintk("msp34xx: AUDC_SET_RADIO\n");
1788 msp->norm = VIDEO_MODE_RADIO;
1789 dprintk("msp34xx: switching to radio mode\n");
1790 if (IS_MSP34XX_G(msp))
1791 msp34xxg_reset(client);
1793 msp->watch_stereo = 0;
1794 switch (msp->opmode) {
1796 /* set msp3400 to FM radio mode */
1797 msp3400c_setmode(client,MSP_MODE_FM_RADIO);
1798 msp3400c_setcarrier(client, MSP_CARRIER(10.7),
1800 msp3400c_setvolume(client, msp->muted, msp->left, msp->right);
1803 case OPMODE_SIMPLER:
1804 /* the thread will do for us */
1805 msp_wake_thread(client);
1809 /* work-in-progress: hook to control the DFP registers */
1810 case MSP_SET_DFPREG:
1812 struct msp_dfpreg *r = arg;
1815 if (r->reg < 0 || r->reg >= DFP_COUNT)
1817 for (i = 0; i < sizeof(bl_dfp) / sizeof(int); i++)
1818 if (r->reg == bl_dfp[i])
1820 msp->dfp_regs[r->reg] = r->value;
1821 msp3400c_write(client, I2C_MSP3400C_DFP, r->reg, r->value);
1824 case MSP_GET_DFPREG:
1826 struct msp_dfpreg *r = arg;
1828 if (r->reg < 0 || r->reg >= DFP_COUNT)
1830 r->value = msp3400c_read(client, I2C_MSP3400C_DFP, r->reg);
1834 /* --- v4l ioctls --- */
1835 /* take care: bttv does userspace copying, we'll get a
1836 kernel pointer here... */
1839 struct video_audio *va = arg;
1841 dprintk("msp34xx: VIDIOCGAUDIO\n");
1842 va->flags |= VIDEO_AUDIO_VOLUME |
1844 VIDEO_AUDIO_TREBLE |
1845 VIDEO_AUDIO_MUTABLE;
1847 va->flags |= VIDEO_AUDIO_MUTE;
1850 va->flags |= VIDEO_AUDIO_MUTE;
1851 va->volume = MAX(msp->left, msp->right);
1852 va->balance = (32768 * MIN(msp->left, msp->right)) /
1853 (va->volume ? va->volume : 1);
1854 va->balance = (msp->left < msp->right) ?
1855 (65535 - va->balance) : va->balance;
1856 if (0 == va->volume)
1857 va->balance = 32768;
1858 va->bass = msp->bass;
1859 va->treble = msp->treble;
1861 msp_any_detect_stereo(client);
1862 va->mode = mode_v4l2_to_v4l1(msp->rxsubchans);
1867 struct video_audio *va = arg;
1869 dprintk("msp34xx: VIDIOCSAUDIO\n");
1870 msp->muted = (va->flags & VIDEO_AUDIO_MUTE);
1871 msp->left = (MIN(65536 - va->balance, 32768) *
1872 va->volume) / 32768;
1873 msp->right = (MIN(va->balance, 32768) * va->volume) / 32768;
1874 msp->bass = va->bass;
1875 msp->treble = va->treble;
1876 dprintk("msp34xx: VIDIOCSAUDIO setting va->volume to %d\n",
1878 dprintk("msp34xx: VIDIOCSAUDIO setting va->balance to %d\n",
1880 dprintk("msp34xx: VIDIOCSAUDIO setting va->flags to %d\n",
1882 dprintk("msp34xx: VIDIOCSAUDIO setting msp->left to %d\n",
1884 dprintk("msp34xx: VIDIOCSAUDIO setting msp->right to %d\n",
1886 dprintk("msp34xx: VIDIOCSAUDIO setting msp->bass to %d\n",
1888 dprintk("msp34xx: VIDIOCSAUDIO setting msp->treble to %d\n",
1890 dprintk("msp34xx: VIDIOCSAUDIO setting msp->mode to %d\n",
1892 msp3400c_setvolume(client, msp->muted, msp->left, msp->right);
1893 msp3400c_setbass(client, msp->bass);
1894 msp3400c_settreble(client, msp->treble);
1896 if (va->mode != 0 && msp->norm != VIDEO_MODE_RADIO)
1897 msp_any_set_audmode(client,mode_v4l1_to_v4l2(va->mode));
1903 struct video_channel *vc = arg;
1905 dprintk("msp34xx: VIDIOCSCHAN (norm=%d)\n",vc->norm);
1906 msp->norm = vc->norm;
1907 if (IS_MSP34XX_G(msp))
1908 msp34xxg_reset(client);
1910 msp_wake_thread(client);
1915 case VIDIOC_S_FREQUENCY:
1917 /* new channel -- kick audio carrier scan */
1918 dprintk("msp34xx: VIDIOCSFREQ\n");
1919 if (IS_MSP34XX_G(msp))
1920 msp34xxg_reset(client);
1922 msp_wake_thread(client);
1926 /* --- v4l2 ioctls --- */
1929 v4l2_std_id *id = arg;
1931 /*FIXME: use V4L2 mode flags on msp3400 instead of V4L1*/
1932 if (*id & V4L2_STD_PAL) {
1933 msp->norm=VIDEO_MODE_PAL;
1934 } else if (*id & V4L2_STD_SECAM) {
1935 msp->norm=VIDEO_MODE_SECAM;
1937 msp->norm=VIDEO_MODE_NTSC;
1940 msp_wake_thread(client);
1944 case VIDIOC_G_AUDIO:
1946 struct v4l2_audio *a = arg;
1948 memset(a,0,sizeof(*a));
1952 strcpy(a->name,"Radio");
1954 case AUDIO_EXTERN_1:
1955 strcpy(a->name,"Extern 1");
1957 case AUDIO_EXTERN_2:
1958 strcpy(a->name,"Extern 2");
1961 strcpy(a->name,"Television");
1967 msp_any_detect_stereo(client);
1968 if (msp->audmode == V4L2_TUNER_MODE_STEREO) {
1969 a->capability=V4L2_AUDCAP_STEREO;
1974 case VIDIOC_S_AUDIO:
1976 struct v4l2_audio *sarg = arg;
1978 switch (sarg->index) {
1980 /* Hauppauge uses IN2 for the radio */
1981 msp->mode = MSP_MODE_FM_RADIO;
1984 case AUDIO_EXTERN_1:
1985 /* IN1 is often used for external input ... */
1986 msp->mode = MSP_MODE_EXTERN;
1989 case AUDIO_EXTERN_2:
1990 /* ... sometimes it is IN2 through ;) */
1991 msp->mode = MSP_MODE_EXTERN;
1999 msp->rxsubchans = V4L2_TUNER_SUB_STEREO;
2000 msp->audmode = V4L2_TUNER_MODE_STEREO;
2001 msp3400c_set_scart(client,scart,0);
2002 msp3400c_write(client,I2C_MSP3400C_DFP,0x000d,0x1900);
2004 if (sarg->capability==V4L2_AUDCAP_STEREO) {
2005 msp->audmode = V4L2_TUNER_MODE_STEREO;
2007 msp->audmode &= ~V4L2_TUNER_MODE_STEREO;
2009 msp_any_set_audmode(client, msp->audmode);
2010 msp_wake_thread(client);
2013 case VIDIOC_G_TUNER:
2015 struct v4l2_tuner *vt = arg;
2017 msp_any_detect_stereo(client);
2018 vt->audmode = msp->audmode;
2019 vt->rxsubchans = msp->rxsubchans;
2020 vt->capability = V4L2_TUNER_CAP_STEREO |
2021 V4L2_TUNER_CAP_LANG1|
2022 V4L2_TUNER_CAP_LANG2;
2025 case VIDIOC_S_TUNER:
2027 struct v4l2_tuner *vt=(struct v4l2_tuner *)arg;
2029 /* only set audmode */
2030 if (vt->audmode != -1 && vt->audmode != 0)
2031 msp_any_set_audmode(client, vt->audmode);
2035 /* msp34xx specific */
2036 case MSP_SET_MATRIX:
2038 struct msp_matrix *mspm = arg;
2040 dprintk("msp34xx: MSP_SET_MATRIX\n");
2041 msp3400c_set_scart(client, mspm->input, mspm->output);
2052 static int msp_suspend(struct device * dev, pm_message_t state)
2054 struct i2c_client *c = container_of(dev, struct i2c_client, dev);
2056 dprintk("msp34xx: suspend\n");
2061 static int msp_resume(struct device * dev)
2063 struct i2c_client *c = container_of(dev, struct i2c_client, dev);
2065 dprintk("msp34xx: resume\n");
2070 /* ----------------------------------------------------------------------- */
2072 static int __init msp3400_init_module(void)
2074 return i2c_add_driver(&driver);
2077 static void __exit msp3400_cleanup_module(void)
2079 i2c_del_driver(&driver);
2082 module_init(msp3400_init_module);
2083 module_exit(msp3400_cleanup_module);
2086 * Overrides for Emacs so that we follow Linus's tabbing style.
2087 * ---------------------------------------------------------------------------