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)
95 int main, second; /* sound carrier */
97 int source; /* see msp34xxg_set_source */
104 int left, right; /* volume */
107 /* shadow register set */
108 int dfp_regs[DFP_COUNT];
111 struct task_struct *kthread;
112 wait_queue_head_t wq;
117 #define MIN(a,b) (((a)>(b))?(b):(a))
118 #define MAX(a,b) (((a)>(b))?(a):(b))
119 #define HAVE_NICAM(msp) (((msp->rev2>>8) & 0xff) != 00)
120 #define HAVE_SIMPLE(msp) ((msp->rev1 & 0xff) >= 'D'-'@')
121 #define HAVE_SIMPLER(msp) ((msp->rev1 & 0xff) >= 'G'-'@')
122 #define HAVE_RADIO(msp) ((msp->rev1 & 0xff) >= 'G'-'@')
124 #define VIDEO_MODE_RADIO 16 /* norm magic for radio mode */
126 /* ---------------------------------------------------------------------- */
128 #define dprintk if (debug >= 1) printk
129 #define d2printk if (debug >= 2) printk
130 #define dprintk_trace if (debug>=16) printk
133 module_param(opmode, int, 0444);
136 module_param(once, int, 0644);
137 module_param(debug, int, 0644);
138 module_param(stereo_threshold, int, 0644);
139 module_param(standard, int, 0644);
140 module_param(amsound, int, 0644);
141 module_param(dolby, int, 0644);
143 MODULE_PARM_DESC(opmode, "Forces a MSP3400 opmode. 0=Manual, 1=Simple, 2=Simpler");
144 MODULE_PARM_DESC(once, "No continuous stereo monitoring");
145 MODULE_PARM_DESC(debug, "Enable debug messages");
146 MODULE_PARM_DESC(stereo_threshold, "Sets signal threshold to activate stereo");
147 MODULE_PARM_DESC(standard, "Specify audio standard: 32 = NTSC, 64 = radio, Default: Autodetect");
148 MODULE_PARM_DESC(amsound, "Hardwire AM sound at 6.5Hz (France), FM can autoscan");
149 MODULE_PARM_DESC(dolby, "Activates Dolby processsing");
151 /* ---------------------------------------------------------------------- */
153 #define I2C_MSP3400C 0x80
154 #define I2C_MSP3400C_ALT 0x88
156 #define I2C_MSP3400C_DEM 0x10
157 #define I2C_MSP3400C_DFP 0x12
159 /* Addresses to scan */
160 static unsigned short normal_i2c[] = {
162 I2C_MSP3400C_ALT >> 1,
167 MODULE_DESCRIPTION("device driver for msp34xx TV sound processor");
168 MODULE_AUTHOR("Gerd Knorr");
169 MODULE_LICENSE("GPL");
171 /* ----------------------------------------------------------------------- */
172 /* functions for talking to the MSP3400C Sound processor */
174 static int msp3400c_reset(struct i2c_client *client)
176 /* reset and read revision code */
177 static char reset_off[3] = { 0x00, 0x80, 0x00 };
178 static char reset_on[3] = { 0x00, 0x00, 0x00 };
179 static char write[3] = { I2C_MSP3400C_DFP + 1, 0x00, 0x1e };
181 struct i2c_msg reset[2] = {
182 { client->addr, I2C_M_IGNORE_NAK, 3, reset_off },
183 { client->addr, I2C_M_IGNORE_NAK, 3, reset_on },
185 struct i2c_msg test[2] = {
186 { client->addr, 0, 3, write },
187 { client->addr, I2C_M_RD, 2, read },
190 dprintk_trace("trace: msp3400c_reset\n");
191 if ( (1 != i2c_transfer(client->adapter,&reset[0],1)) ||
192 (1 != i2c_transfer(client->adapter,&reset[1],1)) ||
193 (2 != i2c_transfer(client->adapter,test,2)) ) {
194 printk(KERN_ERR "msp3400: chip reset failed\n");
200 static int msp3400c_read(struct i2c_client *client, int dev, int addr)
204 unsigned char write[3];
205 unsigned char read[2];
206 struct i2c_msg msgs[2] = {
207 { client->addr, 0, 3, write },
208 { client->addr, I2C_M_RD, 2, read }
212 write[1] = addr >> 8;
213 write[2] = addr & 0xff;
215 for (err = 0; err < 3;) {
216 if (2 == i2c_transfer(client->adapter,msgs,2))
220 "msp34xx: I/O error #%d (read 0x%02x/0x%02x)\n", err,
222 current->state = TASK_INTERRUPTIBLE;
223 schedule_timeout(msecs_to_jiffies(10));
227 "msp34xx: giving up, reseting chip. Sound will go off, sorry folks :-|\n");
228 msp3400c_reset(client);
231 retval = read[0] << 8 | read[1];
232 dprintk_trace("trace: msp3400c_read(0x%x, 0x%x): 0x%x\n", dev, addr,
237 static int msp3400c_write(struct i2c_client *client, int dev, int addr, int val)
240 unsigned char buffer[5];
243 buffer[1] = addr >> 8;
244 buffer[2] = addr & 0xff;
245 buffer[3] = val >> 8;
246 buffer[4] = val & 0xff;
248 dprintk_trace("trace: msp3400c_write(0x%x, 0x%x, 0x%x)\n", dev, addr,
250 for (err = 0; err < 3;) {
251 if (5 == i2c_master_send(client, buffer, 5))
255 "msp34xx: I/O error #%d (write 0x%02x/0x%02x)\n", err,
257 current->state = TASK_INTERRUPTIBLE;
258 schedule_timeout(msecs_to_jiffies(10));
262 "msp34xx: giving up, reseting chip. Sound will go off, sorry folks :-|\n");
263 msp3400c_reset(client);
269 /* ------------------------------------------------------------------------ */
271 /* This macro is allowed for *constants* only, gcc must calculate it
272 at compile time. Remember -- no floats in kernel mode */
273 #define MSP_CARRIER(freq) ((int)((float)(freq/18.432)*(1<<24)))
275 #define MSP_MODE_AM_DETECT 0
276 #define MSP_MODE_FM_RADIO 2
277 #define MSP_MODE_FM_TERRA 3
278 #define MSP_MODE_FM_SAT 4
279 #define MSP_MODE_FM_NICAM1 5
280 #define MSP_MODE_FM_NICAM2 6
281 #define MSP_MODE_AM_NICAM 7
282 #define MSP_MODE_BTSC 8
283 #define MSP_MODE_EXTERN 9
285 static struct MSP_INIT_DATA_DEM {
294 } msp_init_data[] = {
295 { /* AM (for carrier detect / msp3400) */
296 {75, 19, 36, 35, 39, 40},
297 {75, 19, 36, 35, 39, 40},
298 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
299 0x00d0, 0x0500, 0x0020, 0x3000
300 },{ /* AM (for carrier detect / msp3410) */
301 {-1, -1, -8, 2, 59, 126},
302 {-1, -1, -8, 2, 59, 126},
303 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
304 0x00d0, 0x0100, 0x0020, 0x3000
306 {-8, -8, 4, 6, 78, 107},
307 {-8, -8, 4, 6, 78, 107},
308 MSP_CARRIER(10.7), MSP_CARRIER(10.7),
309 0x00d0, 0x0480, 0x0020, 0x3000
310 },{ /* Terrestial FM-mono + FM-stereo */
311 {3, 18, 27, 48, 66, 72},
312 {3, 18, 27, 48, 66, 72},
313 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
314 0x00d0, 0x0480, 0x0030, 0x3000
315 },{ /* Sat FM-mono */
316 { 1, 9, 14, 24, 33, 37},
317 { 3, 18, 27, 48, 66, 72},
318 MSP_CARRIER(6.5), MSP_CARRIER(6.5),
319 0x00c6, 0x0480, 0x0000, 0x3000
320 },{ /* NICAM/FM -- B/G (5.5/5.85), D/K (6.5/5.85) */
321 {-2, -8, -10, 10, 50, 86},
322 {3, 18, 27, 48, 66, 72},
323 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
324 0x00d0, 0x0040, 0x0120, 0x3000
325 },{ /* NICAM/FM -- I (6.0/6.552) */
326 {2, 4, -6, -4, 40, 94},
327 {3, 18, 27, 48, 66, 72},
328 MSP_CARRIER(6.0), MSP_CARRIER(6.0),
329 0x00d0, 0x0040, 0x0120, 0x3000
330 },{ /* NICAM/AM -- L (6.5/5.85) */
331 {-2, -8, -10, 10, 50, 86},
332 {-4, -12, -9, 23, 79, 126},
333 MSP_CARRIER(6.5), MSP_CARRIER(6.5),
334 0x00c6, 0x0140, 0x0120, 0x7c03
338 struct CARRIER_DETECT {
343 static struct CARRIER_DETECT carrier_detect_main[] = {
345 { MSP_CARRIER(4.5), "4.5 NTSC" },
346 { MSP_CARRIER(5.5), "5.5 PAL B/G" },
347 { MSP_CARRIER(6.0), "6.0 PAL I" },
348 { MSP_CARRIER(6.5), "6.5 PAL D/K + SAT + SECAM" }
351 static struct CARRIER_DETECT carrier_detect_55[] = {
353 { MSP_CARRIER(5.7421875), "5.742 PAL B/G FM-stereo" },
354 { MSP_CARRIER(5.85), "5.85 PAL B/G NICAM" }
357 static struct CARRIER_DETECT carrier_detect_65[] = {
358 /* PAL SAT / SECAM */
359 { MSP_CARRIER(5.85), "5.85 PAL D/K + SECAM NICAM" },
360 { MSP_CARRIER(6.2578125), "6.25 PAL D/K1 FM-stereo" },
361 { MSP_CARRIER(6.7421875), "6.74 PAL D/K2 FM-stereo" },
362 { MSP_CARRIER(7.02), "7.02 PAL SAT FM-stereo s/b" },
363 { MSP_CARRIER(7.20), "7.20 PAL SAT FM-stereo s" },
364 { MSP_CARRIER(7.38), "7.38 PAL SAT FM-stereo b" },
367 #define CARRIER_COUNT(x) (sizeof(x)/sizeof(struct CARRIER_DETECT))
369 /* ----------------------------------------------------------------------- *
370 * bits 9 8 5 - SCART DSP input Select:
371 * 0 0 0 - SCART 1 to DSP input (reset position)
372 * 0 1 0 - MONO to DSP input
373 * 1 0 0 - SCART 2 to DSP input
374 * 1 1 1 - Mute DSP input
376 * bits 11 10 6 - SCART 1 Output Select:
377 * 0 0 0 - undefined (reset position)
378 * 0 1 0 - SCART 2 Input to SCART 1 Output (for devices with 2 SCARTS)
379 * 1 0 0 - MONO input to SCART 1 Output
380 * 1 1 0 - SCART 1 DA to SCART 1 Output
381 * 0 0 1 - SCART 2 DA to SCART 1 Output
382 * 0 1 1 - SCART 1 Input to SCART 1 Output
383 * 1 1 1 - Mute SCART 1 Output
385 * bits 13 12 7 - SCART 2 Output Select (for devices with 2 Output SCART):
386 * 0 0 0 - SCART 1 DA to SCART 2 Output (reset position)
387 * 0 1 0 - SCART 1 Input to SCART 2 Output
388 * 1 0 0 - MONO input to SCART 2 Output
389 * 0 0 1 - SCART 2 DA to SCART 2 Output
390 * 0 1 1 - SCART 2 Input to SCART 2 Output
391 * 1 1 0 - Mute SCART 2 Output
393 * Bits 4 to 0 should be zero.
394 * ----------------------------------------------------------------------- */
396 static int scarts[3][9] = {
397 /* MASK IN1 IN2 IN1_DA IN2_DA IN3 IN4 MONO MUTE */
398 /* SCART DSP Input select */
399 { 0x0320, 0x0000, 0x0200, -1, -1, 0x0300, 0x0020, 0x0100, 0x0320 },
400 /* SCART1 Output select */
401 { 0x0c40, 0x0440, 0x0400, 0x0c00, 0x0040, 0x0000, 0x0840, 0x0800, 0x0c40 },
402 /* SCART2 Output select */
403 { 0x3080, 0x1000, 0x1080, 0x0000, 0x0080, 0x2080, 0x3080, 0x2000, 0x3000 },
406 static char *scart_names[] = {
407 "mask", "in1", "in2", "in1 da", "in2 da", "in3", "in4", "mono", "mute"
410 static void msp3400c_set_scart(struct i2c_client *client, int in, int out)
412 struct msp3400c *msp = i2c_get_clientdata(client);
417 if (-1 == scarts[out][in])
420 msp->acb &= ~scarts[out][SCART_MASK];
421 msp->acb |= scarts[out][in];
423 msp->acb = 0xf60; /* Mute Input and SCART 1 Output */
425 dprintk("msp34xx: scart switch: %s => %d (ACB=0x%04x)\n",
426 scart_names[in], out, msp->acb);
427 msp3400c_write(client,I2C_MSP3400C_DFP, 0x13, msp->acb);
429 /* Sets I2S speed 0 = 1.024 Mbps, 1 = 2.048 Mbps */
430 msp3400c_write(client,I2C_MSP3400C_DEM, 0x40, msp->i2s_mode);
433 /* ------------------------------------------------------------------------ */
435 static void msp3400c_setcarrier(struct i2c_client *client, int cdo1, int cdo2)
437 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0093, cdo1 & 0xfff);
438 msp3400c_write(client,I2C_MSP3400C_DEM, 0x009b, cdo1 >> 12);
439 msp3400c_write(client,I2C_MSP3400C_DEM, 0x00a3, cdo2 & 0xfff);
440 msp3400c_write(client,I2C_MSP3400C_DEM, 0x00ab, cdo2 >> 12);
441 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0056, 0); /*LOAD_REG_1/2*/
444 static void msp3400c_setvolume(struct i2c_client *client,
445 int muted, int left, int right)
447 int vol = 0, val = 0, balance = 0;
450 /* 0x7f instead if 0x73 here has sound quality issues,
451 * probably due to overmodulation + clipping ... */
452 vol = (left > right) ? left : right;
453 val = (vol * 0x73 / 65535) << 8;
456 balance = ((right - left) * 127) / vol;
459 dprintk("msp34xx: setvolume: mute=%s %d:%d v=0x%02x b=0x%02x\n",
460 muted ? "on" : "off", left, right, val >> 8, balance);
461 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0000, val); /* loudspeaker */
462 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0006, val); /* headphones */
463 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0007,
464 muted ? 0x1 : (val | 0x1));
465 msp3400c_write(client, I2C_MSP3400C_DFP, 0x0001, balance << 8);
468 static void msp3400c_setbass(struct i2c_client *client, int bass)
470 int val = ((bass-32768) * 0x60 / 65535) << 8;
472 dprintk("msp34xx: setbass: %d 0x%02x\n", bass, val >> 8);
473 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0002, val); /* loudspeaker */
476 static void msp3400c_settreble(struct i2c_client *client, int treble)
478 int val = ((treble-32768) * 0x60 / 65535) << 8;
480 dprintk("msp34xx: settreble: %d 0x%02x\n",treble, val>>8);
481 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0003, val); /* loudspeaker */
484 static void msp3400c_setmode(struct i2c_client *client, int type)
486 struct msp3400c *msp = i2c_get_clientdata(client);
489 dprintk("msp3400: setmode: %d\n",type);
491 msp->audmode = V4L2_TUNER_MODE_MONO;
492 msp->rxsubchans = V4L2_TUNER_SUB_MONO;
494 msp3400c_write(client,I2C_MSP3400C_DEM, 0x00bb, /* ad_cv */
495 msp_init_data[type].ad_cv);
497 for (i = 5; i >= 0; i--) /* fir 1 */
498 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0001,
499 msp_init_data[type].fir1[i]);
501 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005, 0x0004); /* fir 2 */
502 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005, 0x0040);
503 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005, 0x0000);
504 for (i = 5; i >= 0; i--)
505 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005,
506 msp_init_data[type].fir2[i]);
508 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0083, /* MODE_REG */
509 msp_init_data[type].mode_reg);
511 msp3400c_setcarrier(client, msp_init_data[type].cdo1,
512 msp_init_data[type].cdo2);
514 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0056, 0); /*LOAD_REG_1/2*/
517 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008,
519 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009,
521 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b,
522 msp_init_data[type].dfp_src);
524 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008,
525 msp_init_data[type].dfp_src);
526 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009,
527 msp_init_data[type].dfp_src);
528 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b,
529 msp_init_data[type].dfp_src);
531 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000a,
532 msp_init_data[type].dfp_src);
533 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000e,
534 msp_init_data[type].dfp_matrix);
536 if (HAVE_NICAM(msp)) {
538 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0010, 0x5a00); /* was: 0x3000 */
542 /* given a bitmask of VIDEO_SOUND_XXX returns the "best" in the bitmask */
543 static int best_video_sound(int rxsubchans)
545 if (rxsubchans & V4L2_TUNER_SUB_STEREO)
546 return V4L2_TUNER_MODE_STEREO;
547 if (rxsubchans & V4L2_TUNER_SUB_LANG1)
548 return V4L2_TUNER_MODE_LANG1;
549 if (rxsubchans & V4L2_TUNER_SUB_LANG2)
550 return V4L2_TUNER_MODE_LANG2;
551 return V4L2_TUNER_MODE_MONO;
554 /* turn on/off nicam + stereo */
555 static void msp3400c_setstereo(struct i2c_client *client, int mode)
557 static char *strmode[] = { "0", "mono", "stereo", "3",
558 "lang1", "5", "6", "7", "lang2"
560 struct msp3400c *msp = i2c_get_clientdata(client);
561 int nicam = 0; /* channel source: FM/AM or nicam */
564 if (IS_MSP34XX_G(msp)) {
565 /* this method would break everything, let's make sure
569 ("msp34xxg: DEBUG WARNING setstereo called with mode=%d instead of set_source (ignored)\n",
574 /* switch demodulator */
576 case MSP_MODE_FM_TERRA:
577 dprintk("msp3400: FM setstereo: %s\n",
579 msp3400c_setcarrier(client,msp->second,msp->main);
581 case V4L2_TUNER_MODE_STEREO:
582 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000e, 0x3001);
584 case V4L2_TUNER_MODE_MONO:
585 case V4L2_TUNER_MODE_LANG1:
586 case V4L2_TUNER_MODE_LANG2:
587 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000e, 0x3000);
591 case MSP_MODE_FM_SAT:
592 dprintk("msp3400: SAT setstereo: %s\n", strmode[mode]);
594 case V4L2_TUNER_MODE_MONO:
595 msp3400c_setcarrier(client, MSP_CARRIER(6.5), MSP_CARRIER(6.5));
597 case V4L2_TUNER_MODE_STEREO:
598 msp3400c_setcarrier(client, MSP_CARRIER(7.2), MSP_CARRIER(7.02));
600 case V4L2_TUNER_MODE_LANG1:
601 msp3400c_setcarrier(client, MSP_CARRIER(7.38), MSP_CARRIER(7.02));
603 case V4L2_TUNER_MODE_LANG2:
604 msp3400c_setcarrier(client, MSP_CARRIER(7.38), MSP_CARRIER(7.02));
608 case MSP_MODE_FM_NICAM1:
609 case MSP_MODE_FM_NICAM2:
610 case MSP_MODE_AM_NICAM:
611 dprintk("msp3400: NICAM setstereo: %s\n",strmode[mode]);
612 msp3400c_setcarrier(client,msp->second,msp->main);
617 dprintk("msp3400: BTSC setstereo: %s\n",strmode[mode]);
620 case MSP_MODE_EXTERN:
621 dprintk("msp3400: extern setstereo: %s\n",strmode[mode]);
624 case MSP_MODE_FM_RADIO:
625 dprintk("msp3400: FM-Radio setstereo: %s\n",strmode[mode]);
628 dprintk("msp3400: mono setstereo\n");
633 switch (best_video_sound(mode)) {
634 case V4L2_TUNER_MODE_STEREO:
635 src = 0x0020 | nicam;
637 case V4L2_TUNER_MODE_MONO:
638 if (msp->mode == MSP_MODE_AM_NICAM) {
639 dprintk("msp3400: switching to AM mono\n");
640 /* AM mono decoding is handled by tuner, not MSP chip */
641 /* SCART switching control register */
642 msp3400c_set_scart(client,SCART_MONO,0);
646 case V4L2_TUNER_MODE_LANG1:
647 src = 0x0000 | nicam;
649 case V4L2_TUNER_MODE_LANG2:
650 src = 0x0010 | nicam;
653 dprintk("msp3400: setstereo final source/matrix = 0x%x\n", src);
656 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008,0x0520);
657 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009,0x0620);
658 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000a,src);
659 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b,src);
661 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008,src);
662 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009,src);
663 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000a,src);
664 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b,src);
669 msp3400c_print_mode(struct msp3400c *msp)
671 if (msp->main == msp->second) {
672 dprintk("msp3400: mono sound carrier: %d.%03d MHz\n",
673 msp->main/910000,(msp->main/910)%1000);
675 dprintk("msp3400: main sound carrier: %d.%03d MHz\n",
676 msp->main/910000,(msp->main/910)%1000);
678 if (msp->mode == MSP_MODE_FM_NICAM1 || msp->mode == MSP_MODE_FM_NICAM2)
679 dprintk("msp3400: NICAM/FM carrier : %d.%03d MHz\n",
680 msp->second/910000,(msp->second/910)%1000);
681 if (msp->mode == MSP_MODE_AM_NICAM)
682 dprintk("msp3400: NICAM/AM carrier : %d.%03d MHz\n",
683 msp->second/910000,(msp->second/910)%1000);
684 if (msp->mode == MSP_MODE_FM_TERRA &&
685 msp->main != msp->second) {
686 dprintk("msp3400: FM-stereo carrier : %d.%03d MHz\n",
687 msp->second/910000,(msp->second/910)%1000);
691 #define MSP3400_MAX 4
692 static struct i2c_client *msps[MSP3400_MAX];
693 static void msp3400c_restore_dfp(struct i2c_client *client)
695 struct msp3400c *msp = i2c_get_clientdata(client);
698 for (i = 0; i < DFP_COUNT; i++) {
699 if (-1 == msp->dfp_regs[i])
701 msp3400c_write(client, I2C_MSP3400C_DFP, i, msp->dfp_regs[i]);
705 /* if the dfp_regs is set, set what's in there. Otherwise, set the default value */
706 static int msp3400c_write_dfp_with_default(struct i2c_client *client,
707 int addr, int default_value)
709 struct msp3400c *msp = i2c_get_clientdata(client);
710 int value = default_value;
711 if (addr < DFP_COUNT && -1 != msp->dfp_regs[addr])
712 value = msp->dfp_regs[addr];
713 return msp3400c_write(client, I2C_MSP3400C_DFP, addr, value);
716 /* ----------------------------------------------------------------------- */
718 struct REGISTER_DUMP {
723 struct REGISTER_DUMP d1[] = {
724 {0x007e, "autodetect"},
725 {0x0023, "C_AD_BITS "},
726 {0x0038, "ADD_BITS "},
727 {0x003e, "CIB_BITS "},
728 {0x0057, "ERROR_RATE"},
731 static int autodetect_stereo(struct i2c_client *client)
733 struct msp3400c *msp = i2c_get_clientdata(client);
735 int rxsubchans = msp->rxsubchans;
736 int newnicam = msp->nicam_on;
740 case MSP_MODE_FM_TERRA:
741 val = msp3400c_read(client, I2C_MSP3400C_DFP, 0x18);
744 dprintk("msp34xx: stereo detect register: %d\n",val);
746 rxsubchans = V4L2_TUNER_SUB_STEREO | V4L2_TUNER_SUB_MONO;
747 } else if (val < -4096) {
748 rxsubchans = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
750 rxsubchans = V4L2_TUNER_SUB_MONO;
754 case MSP_MODE_FM_NICAM1:
755 case MSP_MODE_FM_NICAM2:
756 case MSP_MODE_AM_NICAM:
757 val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x23);
758 dprintk("msp34xx: nicam sync=%d, mode=%d\n",
759 val & 1, (val & 0x1e) >> 1);
763 switch ((val & 0x1e) >> 1) {
766 rxsubchans = V4L2_TUNER_SUB_STEREO;
770 rxsubchans = V4L2_TUNER_SUB_MONO
771 | V4L2_TUNER_SUB_LANG1;
775 rxsubchans = V4L2_TUNER_SUB_MONO
776 | V4L2_TUNER_SUB_LANG1
777 | V4L2_TUNER_SUB_LANG2;
780 rxsubchans = V4L2_TUNER_SUB_MONO;
786 rxsubchans = V4L2_TUNER_SUB_MONO;
790 val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x200);
791 dprintk("msp3410: status=0x%x (pri=%s, sec=%s, %s%s%s)\n",
793 (val & 0x0002) ? "no" : "yes",
794 (val & 0x0004) ? "no" : "yes",
795 (val & 0x0040) ? "stereo" : "mono",
796 (val & 0x0080) ? ", nicam 2nd mono" : "",
797 (val & 0x0100) ? ", bilingual/SAP" : "");
798 rxsubchans = V4L2_TUNER_SUB_MONO;
799 if (val & 0x0040) rxsubchans |= V4L2_TUNER_SUB_STEREO;
800 if (val & 0x0100) rxsubchans |= V4L2_TUNER_SUB_LANG1;
803 if (rxsubchans != msp->rxsubchans) {
805 dprintk("msp34xx: watch: rxsubchans %d => %d\n",
806 msp->rxsubchans,rxsubchans);
807 msp->rxsubchans = rxsubchans;
809 if (newnicam != msp->nicam_on) {
811 dprintk("msp34xx: watch: nicam %d => %d\n",
812 msp->nicam_on,newnicam);
813 msp->nicam_on = newnicam;
819 * A kernel thread for msp3400 control -- we don't want to block the
820 * in the ioctl while doing the sound carrier & stereo detect
823 static int msp34xx_sleep(struct msp3400c *msp, int timeout)
825 DECLARE_WAITQUEUE(wait, current);
827 add_wait_queue(&msp->wq, &wait);
828 if (!kthread_should_stop()) {
830 set_current_state(TASK_INTERRUPTIBLE);
833 schedule_timeout_interruptible
834 (msecs_to_jiffies(timeout));
838 remove_wait_queue(&msp->wq, &wait);
843 /* stereo/multilang monitoring */
844 static void watch_stereo(struct i2c_client *client)
846 struct msp3400c *msp = i2c_get_clientdata(client);
848 if (autodetect_stereo(client)) {
849 if (msp->stereo & V4L2_TUNER_MODE_STEREO)
850 msp3400c_setstereo(client, V4L2_TUNER_MODE_STEREO);
851 else if (msp->stereo & VIDEO_SOUND_LANG1)
852 msp3400c_setstereo(client, V4L2_TUNER_MODE_LANG1);
854 msp3400c_setstereo(client, V4L2_TUNER_MODE_MONO);
858 msp->watch_stereo = 0;
861 static int msp3400c_thread(void *data)
863 struct i2c_client *client = data;
864 struct msp3400c *msp = i2c_get_clientdata(client);
865 struct CARRIER_DETECT *cd;
866 int count, max1,max2,val1,val2, val,this;
868 printk("msp3400: kthread started\n");
870 d2printk("msp3400: thread: sleep\n");
871 msp34xx_sleep(msp,-1);
872 d2printk("msp3400: thread: wakeup\n");
875 dprintk("msp3410: thread: restart scan\n");
877 if (kthread_should_stop())
880 if (VIDEO_MODE_RADIO == msp->norm ||
881 MSP_MODE_EXTERN == msp->mode) {
882 /* no carrier scan, just unmute */
883 printk("msp3400: thread: no carrier scan\n");
884 msp3400c_setvolume(client, msp->muted, msp->left, msp->right);
889 msp3400c_setvolume(client, msp->muted, 0, 0);
890 msp3400c_setmode(client, MSP_MODE_AM_DETECT /* +1 */ );
893 msp->watch_stereo = 0;
895 /* some time for the tuner to sync */
896 if (msp34xx_sleep(msp,200))
899 /* carrier detect pass #1 -- main carrier */
900 cd = carrier_detect_main;
901 count = CARRIER_COUNT(carrier_detect_main);
903 if (amsound && (msp->norm == VIDEO_MODE_SECAM)) {
904 /* autodetect doesn't work well with AM ... */
907 dprintk("msp3400: AM sound override\n");
910 for (this = 0; this < count; this++) {
911 msp3400c_setcarrier(client, cd[this].cdo,cd[this].cdo);
912 if (msp34xx_sleep(msp,100))
914 val = msp3400c_read(client, I2C_MSP3400C_DFP, 0x1b);
918 val1 = val, max1 = this;
919 dprintk("msp3400: carrier1 val: %5d / %s\n", val,cd[this].name);
922 /* carrier detect pass #2 -- second (stereo) carrier */
925 cd = carrier_detect_55;
926 count = CARRIER_COUNT(carrier_detect_55);
929 cd = carrier_detect_65;
930 count = CARRIER_COUNT(carrier_detect_65);
940 if (amsound && (msp->norm == VIDEO_MODE_SECAM)) {
941 /* autodetect doesn't work well with AM ... */
946 for (this = 0; this < count; this++) {
947 msp3400c_setcarrier(client, cd[this].cdo,cd[this].cdo);
948 if (msp34xx_sleep(msp,100))
950 val = msp3400c_read(client, I2C_MSP3400C_DFP, 0x1b);
954 val2 = val, max2 = this;
955 dprintk("msp3400: carrier2 val: %5d / %s\n", val,cd[this].name);
958 /* programm the msp3400 according to the results */
959 msp->main = carrier_detect_main[max1].cdo;
964 msp->second = carrier_detect_55[max2].cdo;
965 msp3400c_setmode(client, MSP_MODE_FM_TERRA);
967 msp3400c_setstereo(client, V4L2_TUNER_MODE_MONO);
968 msp->watch_stereo = 1;
969 } else if (max2 == 1 && HAVE_NICAM(msp)) {
971 msp->second = carrier_detect_55[max2].cdo;
972 msp3400c_setmode(client, MSP_MODE_FM_NICAM1);
974 msp3400c_setcarrier(client, msp->second, msp->main);
975 msp->watch_stereo = 1;
982 msp->second = MSP_CARRIER(6.552);
983 msp3400c_setmode(client, MSP_MODE_FM_NICAM2);
985 msp3400c_setcarrier(client, msp->second, msp->main);
986 msp->watch_stereo = 1;
989 if (max2 == 1 || max2 == 2) {
991 msp->second = carrier_detect_65[max2].cdo;
992 msp3400c_setmode(client, MSP_MODE_FM_TERRA);
994 msp3400c_setstereo(client, V4L2_TUNER_MODE_MONO);
995 msp->watch_stereo = 1;
996 } else if (max2 == 0 &&
997 msp->norm == VIDEO_MODE_SECAM) {
998 /* L NICAM or AM-mono */
999 msp->second = carrier_detect_65[max2].cdo;
1000 msp3400c_setmode(client, MSP_MODE_AM_NICAM);
1002 msp3400c_setstereo(client, V4L2_TUNER_MODE_MONO);
1003 msp3400c_setcarrier(client, msp->second, msp->main);
1004 /* volume prescale for SCART (AM mono input) */
1005 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000d, 0x1900);
1006 msp->watch_stereo = 1;
1007 } else if (max2 == 0 && HAVE_NICAM(msp)) {
1009 msp->second = carrier_detect_65[max2].cdo;
1010 msp3400c_setmode(client, MSP_MODE_FM_NICAM1);
1012 msp3400c_setcarrier(client, msp->second, msp->main);
1013 msp->watch_stereo = 1;
1021 msp->second = carrier_detect_main[max1].cdo;
1022 msp3400c_setmode(client, MSP_MODE_FM_TERRA);
1024 msp3400c_setcarrier(client, msp->second, msp->main);
1025 msp->rxsubchans = V4L2_TUNER_SUB_MONO;
1026 msp3400c_setstereo(client, V4L2_TUNER_MODE_MONO);
1031 msp3400c_setvolume(client, msp->muted, msp->left, msp->right);
1032 msp3400c_restore_dfp(client);
1035 msp3400c_print_mode(msp);
1037 /* monitor tv audio mode */
1038 while (msp->watch_stereo) {
1039 if (msp34xx_sleep(msp,5000))
1041 watch_stereo(client);
1044 dprintk("msp3400: thread: exit\n");
1048 /* ----------------------------------------------------------------------- */
1049 /* this one uses the automatic sound standard detection of newer */
1050 /* msp34xx chip versions */
1052 static struct MODES {
1057 { 0x0000, 0, 0, "ERROR" },
1058 { 0x0001, 0, 0, "autodetect start" },
1059 { 0x0002, MSP_CARRIER(4.5), MSP_CARRIER(4.72), "4.5/4.72 M Dual FM-Stereo" },
1060 { 0x0003, MSP_CARRIER(5.5), MSP_CARRIER(5.7421875), "5.5/5.74 B/G Dual FM-Stereo" },
1061 { 0x0004, MSP_CARRIER(6.5), MSP_CARRIER(6.2578125), "6.5/6.25 D/K1 Dual FM-Stereo" },
1062 { 0x0005, MSP_CARRIER(6.5), MSP_CARRIER(6.7421875), "6.5/6.74 D/K2 Dual FM-Stereo" },
1063 { 0x0006, MSP_CARRIER(6.5), MSP_CARRIER(6.5), "6.5 D/K FM-Mono (HDEV3)" },
1064 { 0x0008, MSP_CARRIER(5.5), MSP_CARRIER(5.85), "5.5/5.85 B/G NICAM FM" },
1065 { 0x0009, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85 L NICAM AM" },
1066 { 0x000a, MSP_CARRIER(6.0), MSP_CARRIER(6.55), "6.0/6.55 I NICAM FM" },
1067 { 0x000b, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85 D/K NICAM FM" },
1068 { 0x000c, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85 D/K NICAM FM (HDEV2)" },
1069 { 0x0020, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5 M BTSC-Stereo" },
1070 { 0x0021, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5 M BTSC-Mono + SAP" },
1071 { 0x0030, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5 M EIA-J Japan Stereo" },
1072 { 0x0040, MSP_CARRIER(10.7), MSP_CARRIER(10.7), "10.7 FM-Stereo Radio" },
1073 { 0x0050, MSP_CARRIER(6.5), MSP_CARRIER(6.5), "6.5 SAT-Mono" },
1074 { 0x0051, MSP_CARRIER(7.02), MSP_CARRIER(7.20), "7.02/7.20 SAT-Stereo" },
1075 { 0x0060, MSP_CARRIER(7.2), MSP_CARRIER(7.2), "7.2 SAT ADR" },
1076 { -1, 0, 0, NULL }, /* EOF */
1079 static inline const char *msp34xx_standard_mode_name(int mode)
1082 for (i = 0; modelist[i].name != NULL; i++)
1083 if (modelist[i].retval == mode)
1084 return modelist[i].name;
1088 static int msp34xx_modus(int norm)
1091 case VIDEO_MODE_PAL:
1092 dprintk("msp34xx: video mode selected to PAL\n");
1095 /* experimental: not sure this works with all chip versions */
1098 /* previous value, try this if it breaks ... */
1101 case VIDEO_MODE_NTSC: /* BTSC */
1102 dprintk("msp34xx: video mode selected to NTSC\n");
1104 case VIDEO_MODE_SECAM:
1105 dprintk("msp34xx: video mode selected to SECAM\n");
1107 case VIDEO_MODE_RADIO:
1108 dprintk("msp34xx: video mode selected to Radio\n");
1110 case VIDEO_MODE_AUTO:
1111 dprintk("msp34xx: video mode selected to Auto\n");
1118 static int msp34xx_standard(int norm)
1121 case VIDEO_MODE_PAL:
1123 case VIDEO_MODE_NTSC: /* BTSC */
1125 case VIDEO_MODE_SECAM:
1127 case VIDEO_MODE_RADIO:
1134 static int msp3410d_thread(void *data)
1136 struct i2c_client *client = data;
1137 struct msp3400c *msp = i2c_get_clientdata(client);
1140 printk("msp3410: daemon started\n");
1142 d2printk("msp3410: thread: sleep\n");
1143 msp34xx_sleep(msp,-1);
1144 d2printk("msp3410: thread: wakeup\n");
1147 dprintk("msp3410: thread: restart scan\n");
1149 if (kthread_should_stop())
1152 if (msp->mode == MSP_MODE_EXTERN) {
1153 /* no carrier scan needed, just unmute */
1154 dprintk("msp3410: thread: no carrier scan\n");
1155 msp3400c_setvolume(client, msp->muted, msp->left, msp->right);
1159 /* put into sane state (and mute) */
1160 msp3400c_reset(client);
1162 /* some time for the tuner to sync */
1163 if (msp34xx_sleep(msp,200))
1166 /* start autodetect */
1167 mode = msp34xx_modus(msp->norm);
1168 std = msp34xx_standard(msp->norm);
1169 msp3400c_write(client, I2C_MSP3400C_DEM, 0x30, mode);
1170 msp3400c_write(client, I2C_MSP3400C_DEM, 0x20, std);
1171 msp->watch_stereo = 0;
1174 dprintk("msp3410: setting mode: %s (0x%04x)\n",
1175 msp34xx_standard_mode_name(std) ,std);
1178 /* programmed some specific mode */
1181 /* triggered autodetect */
1183 if (msp34xx_sleep(msp,100))
1187 val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x7e);
1190 dprintk("msp3410: detection still in progress\n");
1193 for (i = 0; modelist[i].name != NULL; i++)
1194 if (modelist[i].retval == val)
1196 dprintk("msp3410: current mode: %s (0x%04x)\n",
1197 modelist[i].name ? modelist[i].name : "unknown",
1199 msp->main = modelist[i].main;
1200 msp->second = modelist[i].second;
1202 if (amsound && (msp->norm == VIDEO_MODE_SECAM) && (val != 0x0009)) {
1203 /* autodetection has failed, let backup */
1204 dprintk("msp3410: autodetection failed,"
1205 " switching to backup mode: %s (0x%04x)\n",
1206 modelist[8].name ? modelist[8].name : "unknown",val);
1208 msp3400c_write(client, I2C_MSP3400C_DEM, 0x20, val);
1211 /* set various prescales */
1212 msp3400c_write(client, I2C_MSP3400C_DFP, 0x0d, 0x1900); /* scart */
1213 msp3400c_write(client, I2C_MSP3400C_DFP, 0x0e, 0x2403); /* FM */
1214 msp3400c_write(client, I2C_MSP3400C_DFP, 0x10, 0x5a00); /* nicam */
1218 case 0x0008: /* B/G NICAM */
1219 case 0x000a: /* I NICAM */
1221 msp->mode = MSP_MODE_FM_NICAM1;
1223 msp->mode = MSP_MODE_FM_NICAM2;
1224 /* just turn on stereo */
1225 msp->rxsubchans = V4L2_TUNER_SUB_STEREO;
1227 msp->watch_stereo = 1;
1228 msp3400c_setstereo(client,V4L2_TUNER_MODE_STEREO);
1231 msp->mode = MSP_MODE_AM_NICAM;
1232 msp->rxsubchans = V4L2_TUNER_SUB_MONO;
1234 msp3400c_setstereo(client,V4L2_TUNER_MODE_MONO);
1235 msp->watch_stereo = 1;
1237 case 0x0020: /* BTSC */
1238 /* just turn on stereo */
1239 msp->mode = MSP_MODE_BTSC;
1240 msp->rxsubchans = V4L2_TUNER_SUB_STEREO;
1242 msp->watch_stereo = 1;
1243 msp3400c_setstereo(client,V4L2_TUNER_MODE_STEREO);
1245 case 0x0040: /* FM radio */
1246 msp->mode = MSP_MODE_FM_RADIO;
1247 msp->rxsubchans = V4L2_TUNER_SUB_STEREO;
1248 msp->audmode = V4L2_TUNER_MODE_STEREO;
1250 msp->watch_stereo = 0;
1251 /* not needed in theory if HAVE_RADIO(), but
1252 short programming enables carrier mute */
1253 msp3400c_setmode(client,MSP_MODE_FM_RADIO);
1254 msp3400c_setcarrier(client, MSP_CARRIER(10.7),
1257 msp3400c_set_scart(client,SCART_IN2,0);
1258 /* msp34xx does radio decoding */
1259 msp3400c_write(client,I2C_MSP3400C_DFP, 0x08, 0x0020);
1260 msp3400c_write(client,I2C_MSP3400C_DFP, 0x09, 0x0020);
1261 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0b, 0x0020);
1266 msp->mode = MSP_MODE_FM_TERRA;
1267 msp->rxsubchans = V4L2_TUNER_SUB_MONO;
1268 msp->audmode = V4L2_TUNER_MODE_MONO;
1270 msp->watch_stereo = 1;
1274 /* unmute, restore misc registers */
1275 msp3400c_setbass(client, msp->bass);
1276 msp3400c_settreble(client, msp->treble);
1277 msp3400c_setvolume(client, msp->muted, msp->left, msp->right);
1278 msp3400c_write(client, I2C_MSP3400C_DFP, 0x13, msp->acb);
1279 msp3400c_write(client,I2C_MSP3400C_DEM, 0x40, msp->i2s_mode);
1280 msp3400c_restore_dfp(client);
1282 /* monitor tv audio mode */
1283 while (msp->watch_stereo) {
1284 if (msp34xx_sleep(msp,5000))
1286 watch_stereo(client);
1289 dprintk("msp3410: thread: exit\n");
1293 /* ----------------------------------------------------------------------- */
1294 /* msp34xxG + (simpler no-thread) */
1295 /* this one uses both automatic standard detection and automatic sound */
1296 /* select which are available in the newer G versions */
1297 /* struct msp: only norm, acb and source are really used in this mode */
1299 static void msp34xxg_set_source(struct i2c_client *client, int source);
1301 /* (re-)initialize the msp34xxg, according to the current norm in msp->norm
1302 * return 0 if it worked, -1 if it failed
1304 static int msp34xxg_reset(struct i2c_client *client)
1306 struct msp3400c *msp = i2c_get_clientdata(client);
1309 if (msp3400c_reset(client))
1312 /* make sure that input/output is muted (paranoid mode) */
1313 if (msp3400c_write(client,
1316 0x0f20 /* mute DSP input, mute SCART 1 */))
1319 msp3400c_write(client,I2C_MSP3400C_DEM, 0x40, msp->i2s_mode);
1321 /* step-by-step initialisation, as described in the manual */
1322 modus = msp34xx_modus(msp->norm);
1323 std = msp34xx_standard(msp->norm);
1324 modus &= ~0x03; /* STATUS_CHANGE=0 */
1325 modus |= 0x01; /* AUTOMATIC_SOUND_DETECTION=1 */
1326 if (msp3400c_write(client,
1331 if (msp3400c_write(client,
1337 /* write the dfps that may have an influence on
1338 standard/audio autodetection right now */
1339 msp34xxg_set_source(client, msp->source);
1341 if (msp3400c_write_dfp_with_default(client, 0x0e, /* AM/FM Prescale */
1343 /* default: [15:8] 75khz deviation */
1347 if (msp3400c_write_dfp_with_default(client, 0x10, /* NICAM Prescale */
1349 /* default: 9db gain (as recommended) */
1356 static int msp34xxg_thread(void *data)
1358 struct i2c_client *client = data;
1359 struct msp3400c *msp = i2c_get_clientdata(client);
1362 printk("msp34xxg: daemon started\n");
1363 msp->source = 1; /* default */
1365 d2printk("msp34xxg: thread: sleep\n");
1366 msp34xx_sleep(msp,-1);
1367 d2printk("msp34xxg: thread: wakeup\n");
1370 dprintk("msp34xxg: thread: restart scan\n");
1372 if (kthread_should_stop())
1376 msp34xxg_reset(client);
1381 /* watch autodetect */
1382 dprintk("msp34xxg: triggered autodetect, waiting for result\n");
1383 for (i = 0; i < 10; i++) {
1384 if (msp34xx_sleep(msp,100))
1388 val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x7e);
1393 dprintk("msp34xxg: detection still in progress\n");
1396 dprintk("msp34xxg: detection still in progress after 10 tries. giving up.\n");
1401 dprintk("msp34xxg: current mode: %s (0x%04x)\n",
1402 msp34xx_standard_mode_name(std), std);
1404 /* unmute: dispatch sound to scart output, set scart volume */
1405 dprintk("msp34xxg: unmute\n");
1407 msp3400c_setbass(client, msp->bass);
1408 msp3400c_settreble(client, msp->treble);
1409 msp3400c_setvolume(client, msp->muted, msp->left, msp->right);
1412 if (msp3400c_write(client,
1418 msp3400c_write(client,I2C_MSP3400C_DEM, 0x40, msp->i2s_mode);
1420 dprintk("msp34xxg: thread: exit\n");
1424 /* set the same 'source' for the loudspeaker, scart and quasi-peak detector
1425 * the value for source is the same as bit 15:8 of DFP registers 0x08,
1426 * 0x0a and 0x0c: 0=mono, 1=stereo or A|B, 2=SCART, 3=stereo or A, 4=stereo or B
1428 * this function replaces msp3400c_setstereo
1430 static void msp34xxg_set_source(struct i2c_client *client, int source)
1432 struct msp3400c *msp = i2c_get_clientdata(client);
1434 /* fix matrix mode to stereo and let the msp choose what
1435 * to output according to 'source', as recommended
1436 * for MONO (source==0) downmixing set bit[7:0] to 0x30
1438 int value = (source&0x07)<<8|(source==0 ? 0x30:0x20);
1439 dprintk("msp34xxg: set source to %d (0x%x)\n", source, value);
1440 msp3400c_write(client,
1442 0x08, /* Loudspeaker Output */
1444 msp3400c_write(client,
1446 0x0a, /* SCART1 DA Output */
1448 msp3400c_write(client,
1450 0x0c, /* Quasi-peak detector */
1453 * set identification threshold. Personally, I
1454 * I set it to a higher value that the default
1455 * of 0x190 to ignore noisy stereo signals.
1456 * this needs tuning. (recommended range 0x00a0-0x03c0)
1457 * 0x7f0 = forced mono mode
1459 msp3400c_write(client,
1461 0x22, /* a2 threshold for stereo/bilingual */
1466 static void msp34xxg_detect_stereo(struct i2c_client *client)
1468 struct msp3400c *msp = i2c_get_clientdata(client);
1470 int status = msp3400c_read(client,
1472 0x0200 /* STATUS */);
1473 int is_bilingual = status&0x100;
1474 int is_stereo = status&0x40;
1476 msp->rxsubchans = 0;
1478 msp->rxsubchans |= V4L2_TUNER_SUB_STEREO;
1480 msp->rxsubchans |= V4L2_TUNER_SUB_MONO;
1482 msp->rxsubchans |= V4L2_TUNER_SUB_LANG1|V4L2_TUNER_SUB_LANG2;
1483 /* I'm supposed to check whether it's SAP or not
1484 * and set only LANG2/SAP in this case. Yet, the MSP
1485 * does a lot of work to hide this and handle everything
1486 * the same way. I don't want to work around it so unless
1487 * this is a problem, I'll handle SAP just like lang1/lang2.
1490 dprintk("msp34xxg: status=0x%x, stereo=%d, bilingual=%d -> rxsubchans=%d\n",
1491 status, is_stereo, is_bilingual, msp->rxsubchans);
1494 static void msp34xxg_set_audmode(struct i2c_client *client, int audmode)
1496 struct msp3400c *msp = i2c_get_clientdata(client);
1500 case V4L2_TUNER_MODE_MONO:
1501 source=0; /* mono only */
1503 case V4L2_TUNER_MODE_STEREO:
1504 source=1; /* stereo or A|B, see comment in msp34xxg_get_v4l2_stereo() */
1505 /* problem: that could also mean 2 (scart input) */
1507 case V4L2_TUNER_MODE_LANG1:
1508 source=3; /* stereo or A */
1510 case V4L2_TUNER_MODE_LANG2:
1511 source=4; /* stereo or B */
1518 msp->audmode = audmode;
1519 msp34xxg_set_source(client, source);
1523 /* ----------------------------------------------------------------------- */
1525 static int msp_attach(struct i2c_adapter *adap, int addr, int kind);
1526 static int msp_detach(struct i2c_client *client);
1527 static int msp_probe(struct i2c_adapter *adap);
1528 static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg);
1530 static int msp_suspend(struct device * dev, pm_message_t state);
1531 static int msp_resume(struct device * dev);
1533 static void msp_wake_thread(struct i2c_client *client);
1535 static struct i2c_driver driver = {
1536 .owner = THIS_MODULE,
1537 .name = "i2c msp3400 driver",
1538 .id = I2C_DRIVERID_MSP3400,
1539 .flags = I2C_DF_NOTIFY,
1540 .attach_adapter = msp_probe,
1541 .detach_client = msp_detach,
1542 .command = msp_command,
1544 .suspend = msp_suspend,
1545 .resume = msp_resume,
1549 static struct i2c_client client_template =
1552 .flags = I2C_CLIENT_ALLOW_USE,
1556 static int msp_attach(struct i2c_adapter *adap, int addr, int kind)
1558 struct msp3400c *msp;
1559 struct i2c_client *c;
1560 int (*thread_func)(void *data) = NULL;
1563 client_template.adapter = adap;
1564 client_template.addr = addr;
1566 if (-1 == msp3400c_reset(&client_template)) {
1567 dprintk("msp34xx: no chip found\n");
1571 if (NULL == (c = kmalloc(sizeof(struct i2c_client),GFP_KERNEL)))
1573 memcpy(c,&client_template,sizeof(struct i2c_client));
1574 if (NULL == (msp = kmalloc(sizeof(struct msp3400c),GFP_KERNEL))) {
1579 memset(msp,0,sizeof(struct msp3400c));
1580 msp->norm = VIDEO_MODE_NTSC;
1581 msp->left = 58880; /* 0db gain */
1582 msp->right = 58880; /* 0db gain */
1584 msp->treble = 32768;
1588 for (i = 0; i < DFP_COUNT; i++)
1589 msp->dfp_regs[i] = -1;
1591 i2c_set_clientdata(c, msp);
1592 init_waitqueue_head(&msp->wq);
1594 if (-1 == msp3400c_reset(c)) {
1597 dprintk("msp34xx: no chip found\n");
1601 msp->rev1 = msp3400c_read(c, I2C_MSP3400C_DFP, 0x1e);
1602 if (-1 != msp->rev1)
1603 msp->rev2 = msp3400c_read(c, I2C_MSP3400C_DFP, 0x1f);
1604 if ((-1 == msp->rev1) || (0 == msp->rev1 && 0 == msp->rev2)) {
1607 dprintk("msp34xx: error while reading chip version\n");
1610 printk(KERN_INFO "msp34xx: rev1=0x%04x, rev2=0x%04x\n", msp->rev1, msp->rev2);
1612 msp3400c_setvolume(c, msp->muted, msp->left, msp->right);
1614 snprintf(c->name, sizeof(c->name), "MSP34%02d%c-%c%d",
1615 (msp->rev2>>8)&0xff, (msp->rev1&0xff)+'@',
1616 ((msp->rev1>>8)&0xff)+'@', msp->rev2&0x1f);
1618 msp->opmode = opmode;
1619 if (OPMODE_AUTO == msp->opmode) {
1620 if (HAVE_SIMPLER(msp))
1621 msp->opmode = OPMODE_SIMPLER;
1622 else if (HAVE_SIMPLE(msp))
1623 msp->opmode = OPMODE_SIMPLE;
1625 msp->opmode = OPMODE_MANUAL;
1628 /* hello world :-) */
1629 printk(KERN_INFO "msp34xx: init: chip=%s", c->name);
1630 if (HAVE_NICAM(msp))
1632 if (HAVE_SIMPLE(msp))
1634 if (HAVE_SIMPLER(msp))
1635 printk(" +simpler");
1636 if (HAVE_RADIO(msp))
1639 /* version-specific initialization */
1640 switch (msp->opmode) {
1642 printk(" mode=manual");
1643 thread_func = msp3400c_thread;
1646 printk(" mode=simple");
1647 thread_func = msp3410d_thread;
1649 case OPMODE_SIMPLER:
1650 printk(" mode=simpler");
1651 thread_func = msp34xxg_thread;
1656 /* startup control thread if needed */
1658 msp->kthread = kthread_run(thread_func, c, "msp34xx");
1660 if (NULL == msp->kthread)
1661 printk(KERN_WARNING "msp34xx: kernel_thread() failed\n");
1666 i2c_attach_client(c);
1668 /* update our own array */
1669 for (i = 0; i < MSP3400_MAX; i++) {
1670 if (NULL == msps[i]) {
1679 static int msp_detach(struct i2c_client *client)
1681 struct msp3400c *msp = i2c_get_clientdata(client);
1684 /* shutdown control thread */
1687 kthread_stop(msp->kthread);
1689 msp3400c_reset(client);
1691 /* update our own array */
1692 for (i = 0; i < MSP3400_MAX; i++) {
1693 if (client == msps[i]) {
1699 i2c_detach_client(client);
1706 static int msp_probe(struct i2c_adapter *adap)
1708 if (adap->class & I2C_CLASS_TV_ANALOG)
1709 return i2c_probe(adap, &addr_data, msp_attach);
1713 static void msp_wake_thread(struct i2c_client *client)
1715 struct msp3400c *msp = i2c_get_clientdata(client);
1717 if (NULL == msp->kthread)
1719 msp3400c_setvolume(client,msp->muted,0,0);
1720 msp->watch_stereo = 0;
1722 wake_up_interruptible(&msp->wq);
1725 /* ----------------------------------------------------------------------- */
1727 static int mode_v4l2_to_v4l1(int rxsubchans)
1731 if (rxsubchans & V4L2_TUNER_SUB_STEREO)
1732 mode |= VIDEO_SOUND_STEREO;
1733 if (rxsubchans & V4L2_TUNER_SUB_LANG2)
1734 mode |= VIDEO_SOUND_LANG2;
1735 if (rxsubchans & V4L2_TUNER_SUB_LANG1)
1736 mode |= VIDEO_SOUND_LANG1;
1738 mode |= VIDEO_SOUND_MONO;
1742 static int mode_v4l1_to_v4l2(int mode)
1744 if (mode & VIDEO_SOUND_STEREO)
1745 return V4L2_TUNER_MODE_STEREO;
1746 if (mode & VIDEO_SOUND_LANG2)
1747 return V4L2_TUNER_MODE_LANG2;
1748 if (mode & VIDEO_SOUND_LANG1)
1749 return V4L2_TUNER_MODE_LANG1;
1750 return V4L2_TUNER_MODE_MONO;
1753 static void msp_any_detect_stereo(struct i2c_client *client)
1755 struct msp3400c *msp = i2c_get_clientdata(client);
1757 switch (msp->opmode) {
1760 autodetect_stereo(client);
1762 case OPMODE_SIMPLER:
1763 msp34xxg_detect_stereo(client);
1768 static void msp_any_set_audmode(struct i2c_client *client, int audmode)
1770 struct msp3400c *msp = i2c_get_clientdata(client);
1772 switch (msp->opmode) {
1775 msp->watch_stereo = 0;
1776 msp3400c_setstereo(client, audmode);
1778 case OPMODE_SIMPLER:
1779 msp34xxg_set_audmode(client, audmode);
1785 static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg)
1787 struct msp3400c *msp = i2c_get_clientdata(client);
1793 case AUDC_SET_INPUT:
1794 dprintk("msp34xx: AUDC_SET_INPUT(%d)\n",*sarg);
1796 if (*sarg == msp->input)
1801 /* Hauppauge uses IN2 for the radio */
1802 msp->mode = MSP_MODE_FM_RADIO;
1805 case AUDIO_EXTERN_1:
1806 /* IN1 is often used for external input ... */
1807 msp->mode = MSP_MODE_EXTERN;
1810 case AUDIO_EXTERN_2:
1811 /* ... sometimes it is IN2 through ;) */
1812 msp->mode = MSP_MODE_EXTERN;
1819 if (*sarg & AUDIO_MUTE)
1820 msp3400c_set_scart(client,SCART_MUTE,0);
1824 msp->rxsubchans = V4L2_TUNER_SUB_STEREO;
1825 msp->audmode = V4L2_TUNER_MODE_STEREO;
1826 msp3400c_set_scart(client,scart,0);
1827 msp3400c_write(client,I2C_MSP3400C_DFP,0x000d,0x1900);
1828 if (msp->opmode != OPMODE_SIMPLER)
1829 msp3400c_setstereo(client, msp->audmode);
1831 msp_wake_thread(client);
1834 case AUDC_SET_RADIO:
1835 dprintk("msp34xx: AUDC_SET_RADIO\n");
1836 msp->norm = VIDEO_MODE_RADIO;
1837 dprintk("msp34xx: switching to radio mode\n");
1838 if (IS_MSP34XX_G(msp))
1839 msp34xxg_reset(client);
1841 msp->watch_stereo = 0;
1842 switch (msp->opmode) {
1844 /* set msp3400 to FM radio mode */
1845 msp3400c_setmode(client,MSP_MODE_FM_RADIO);
1846 msp3400c_setcarrier(client, MSP_CARRIER(10.7),
1848 msp3400c_setvolume(client, msp->muted, msp->left, msp->right);
1851 case OPMODE_SIMPLER:
1852 /* the thread will do for us */
1853 msp_wake_thread(client);
1857 /* work-in-progress: hook to control the DFP registers */
1858 case MSP_SET_DFPREG:
1860 struct msp_dfpreg *r = arg;
1863 if (r->reg < 0 || r->reg >= DFP_COUNT)
1865 for (i = 0; i < sizeof(bl_dfp) / sizeof(int); i++)
1866 if (r->reg == bl_dfp[i])
1868 msp->dfp_regs[r->reg] = r->value;
1869 msp3400c_write(client, I2C_MSP3400C_DFP, r->reg, r->value);
1872 case MSP_GET_DFPREG:
1874 struct msp_dfpreg *r = arg;
1876 if (r->reg < 0 || r->reg >= DFP_COUNT)
1878 r->value = msp3400c_read(client, I2C_MSP3400C_DFP, r->reg);
1882 /* --- v4l ioctls --- */
1883 /* take care: bttv does userspace copying, we'll get a
1884 kernel pointer here... */
1887 struct video_audio *va = arg;
1889 dprintk("msp34xx: VIDIOCGAUDIO\n");
1890 va->flags |= VIDEO_AUDIO_VOLUME |
1892 VIDEO_AUDIO_TREBLE |
1893 VIDEO_AUDIO_MUTABLE;
1895 va->flags |= VIDEO_AUDIO_MUTE;
1898 va->flags |= VIDEO_AUDIO_MUTE;
1899 va->volume = MAX(msp->left, msp->right);
1900 va->balance = (32768 * MIN(msp->left, msp->right)) /
1901 (va->volume ? va->volume : 1);
1902 va->balance = (msp->left < msp->right) ?
1903 (65535 - va->balance) : va->balance;
1904 if (0 == va->volume)
1905 va->balance = 32768;
1906 va->bass = msp->bass;
1907 va->treble = msp->treble;
1909 msp_any_detect_stereo(client);
1910 va->mode = mode_v4l2_to_v4l1(msp->rxsubchans);
1915 struct video_audio *va = arg;
1917 dprintk("msp34xx: VIDIOCSAUDIO\n");
1918 msp->muted = (va->flags & VIDEO_AUDIO_MUTE);
1919 msp->left = (MIN(65536 - va->balance, 32768) *
1920 va->volume) / 32768;
1921 msp->right = (MIN(va->balance, 32768) * va->volume) / 32768;
1922 msp->bass = va->bass;
1923 msp->treble = va->treble;
1924 dprintk("msp34xx: VIDIOCSAUDIO setting va->volume to %d\n",
1926 dprintk("msp34xx: VIDIOCSAUDIO setting va->balance to %d\n",
1928 dprintk("msp34xx: VIDIOCSAUDIO setting va->flags to %d\n",
1930 dprintk("msp34xx: VIDIOCSAUDIO setting msp->left to %d\n",
1932 dprintk("msp34xx: VIDIOCSAUDIO setting msp->right to %d\n",
1934 dprintk("msp34xx: VIDIOCSAUDIO setting msp->bass to %d\n",
1936 dprintk("msp34xx: VIDIOCSAUDIO setting msp->treble to %d\n",
1938 dprintk("msp34xx: VIDIOCSAUDIO setting msp->mode to %d\n",
1940 msp3400c_setvolume(client, msp->muted, msp->left, msp->right);
1941 msp3400c_setbass(client, msp->bass);
1942 msp3400c_settreble(client, msp->treble);
1944 if (va->mode != 0 && msp->norm != VIDEO_MODE_RADIO)
1945 msp_any_set_audmode(client,mode_v4l1_to_v4l2(va->mode));
1951 struct video_channel *vc = arg;
1953 dprintk("msp34xx: VIDIOCSCHAN (norm=%d)\n",vc->norm);
1954 msp->norm = vc->norm;
1955 if (IS_MSP34XX_G(msp))
1956 msp34xxg_reset(client);
1958 msp_wake_thread(client);
1963 case VIDIOC_S_FREQUENCY:
1965 /* new channel -- kick audio carrier scan */
1966 dprintk("msp34xx: VIDIOCSFREQ\n");
1967 if (IS_MSP34XX_G(msp))
1968 msp34xxg_reset(client);
1970 msp_wake_thread(client);
1974 /* msp34xx specific */
1975 case MSP_SET_MATRIX:
1977 struct msp_matrix *mspm = arg;
1979 dprintk("msp34xx: MSP_SET_MATRIX\n");
1980 msp3400c_set_scart(client, mspm->input, mspm->output);
1984 /* --- v4l2 ioctls --- */
1987 v4l2_std_id *id = arg;
1989 /*FIXME: use V4L2 mode flags on msp3400 instead of V4L1*/
1990 if (*id & V4L2_STD_PAL) {
1991 msp->norm=VIDEO_MODE_PAL;
1992 } else if (*id & V4L2_STD_SECAM) {
1993 msp->norm=VIDEO_MODE_SECAM;
1995 msp->norm=VIDEO_MODE_NTSC;
1998 msp_wake_thread(client);
2002 case VIDIOC_ENUMINPUT:
2004 struct v4l2_input *i = arg;
2009 i->type = V4L2_INPUT_TYPE_TUNER;
2012 strcpy(i->name,"Radio");
2014 case AUDIO_EXTERN_1:
2015 strcpy(i->name,"Extern 1");
2017 case AUDIO_EXTERN_2:
2018 strcpy(i->name,"Extern 2");
2021 strcpy(i->name,"Television");
2029 case VIDIOC_G_AUDIO:
2031 struct v4l2_audio *a = arg;
2033 memset(a,0,sizeof(*a));
2037 strcpy(a->name,"Radio");
2039 case AUDIO_EXTERN_1:
2040 strcpy(a->name,"Extern 1");
2042 case AUDIO_EXTERN_2:
2043 strcpy(a->name,"Extern 2");
2046 strcpy(a->name,"Television");
2052 msp_any_detect_stereo(client);
2053 if (msp->audmode == V4L2_TUNER_MODE_STEREO) {
2054 a->capability=V4L2_AUDCAP_STEREO;
2059 case VIDIOC_S_AUDIO:
2061 struct v4l2_audio *sarg = arg;
2063 switch (sarg->index) {
2065 /* Hauppauge uses IN2 for the radio */
2066 msp->mode = MSP_MODE_FM_RADIO;
2069 case AUDIO_EXTERN_1:
2070 /* IN1 is often used for external input ... */
2071 msp->mode = MSP_MODE_EXTERN;
2074 case AUDIO_EXTERN_2:
2075 /* ... sometimes it is IN2 through ;) */
2076 msp->mode = MSP_MODE_EXTERN;
2084 msp->rxsubchans = V4L2_TUNER_SUB_STEREO;
2085 msp->audmode = V4L2_TUNER_MODE_STEREO;
2086 msp3400c_set_scart(client,scart,0);
2087 msp3400c_write(client,I2C_MSP3400C_DFP,0x000d,0x1900);
2089 if (sarg->capability==V4L2_AUDCAP_STEREO) {
2090 msp->audmode = V4L2_TUNER_MODE_STEREO;
2092 msp->audmode &= ~V4L2_TUNER_MODE_STEREO;
2094 msp_any_set_audmode(client, msp->audmode);
2095 msp_wake_thread(client);
2098 case VIDIOC_G_TUNER:
2100 struct v4l2_tuner *vt = arg;
2102 msp_any_detect_stereo(client);
2103 vt->audmode = msp->audmode;
2104 vt->rxsubchans = msp->rxsubchans;
2105 vt->capability = V4L2_TUNER_CAP_STEREO |
2106 V4L2_TUNER_CAP_LANG1|
2107 V4L2_TUNER_CAP_LANG2;
2110 case VIDIOC_S_TUNER:
2112 struct v4l2_tuner *vt=(struct v4l2_tuner *)arg;
2114 /* only set audmode */
2115 if (vt->audmode != -1 && vt->audmode != 0)
2116 msp_any_set_audmode(client, vt->audmode);
2120 case VIDIOC_G_AUDOUT:
2122 struct v4l2_audioout *a=(struct v4l2_audioout *)arg;
2124 memset(a,0,sizeof(*a));
2128 strcpy(a->name,"Scart1 Out");
2131 strcpy(a->name,"Scart2 Out");
2134 strcpy(a->name,"I2S Out");
2142 case VIDIOC_S_AUDOUT:
2144 struct v4l2_audioout *a=(struct v4l2_audioout *)arg;
2146 if (a->index<0||a->index>2)
2150 if (a->mode == V4L2_AUDMODE_32BITS)
2155 printk("Setting audio out on msp34xx to input %i, mode %i\n",a->index,msp->i2s_mode);
2156 msp3400c_set_scart(client,msp->in_scart,a->index);
2168 static int msp_suspend(struct device * dev, pm_message_t state)
2170 struct i2c_client *c = container_of(dev, struct i2c_client, dev);
2172 dprintk("msp34xx: suspend\n");
2177 static int msp_resume(struct device * dev)
2179 struct i2c_client *c = container_of(dev, struct i2c_client, dev);
2181 dprintk("msp34xx: resume\n");
2186 /* ----------------------------------------------------------------------- */
2188 static int __init msp3400_init_module(void)
2190 return i2c_add_driver(&driver);
2193 static void __exit msp3400_cleanup_module(void)
2195 i2c_del_driver(&driver);
2198 module_init(msp3400_init_module);
2199 module_exit(msp3400_cleanup_module);
2202 * Overrides for Emacs so that we follow Linus's tabbing style.
2203 * ---------------------------------------------------------------------------