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 msp3400_dbg(fmt, arg...) \
62 printk(KERN_INFO "%s debug %d-%04x: " fmt, client->driver->name, \
63 i2c_adapter_id(client->adapter), client->addr , ## arg); \
66 /* Medium volume debug. */
67 #define msp3400_dbg_mediumvol(fmt, arg...) \
70 printk(KERN_INFO "%s debug %d-%04x: " fmt, client->driver->name, \
71 i2c_adapter_id(client->adapter), client->addr , ## arg); \
74 /* High volume debug. Use with care. */
75 #define msp3400_dbg_highvol(fmt, arg...) \
78 printk(KERN_INFO "%s debug %d-%04x: " fmt, client->driver->name, \
79 i2c_adapter_id(client->adapter), client->addr , ## arg); \
82 #define msp3400_err(fmt, arg...) do { \
83 printk(KERN_ERR "%s %d-%04x: " fmt, client->driver->name, \
84 i2c_adapter_id(client->adapter), client->addr , ## arg); } while (0)
85 #define msp3400_warn(fmt, arg...) do { \
86 printk(KERN_WARNING "%s %d-%04x: " fmt, client->driver->name, \
87 i2c_adapter_id(client->adapter), client->addr , ## arg); } while (0)
88 #define msp3400_info(fmt, arg...) do { \
89 printk(KERN_INFO "%s %d-%04x: " fmt, client->driver->name, \
90 i2c_adapter_id(client->adapter), client->addr , ## arg); } while (0)
92 #define OPMODE_AUTO -1
93 #define OPMODE_MANUAL 0
94 #define OPMODE_SIMPLE 1 /* use short programming (>= msp3410 only) */
95 #define OPMODE_SIMPLER 2 /* use shorter programming (>= msp34xxG) */
97 /* insmod parameters */
98 static int opmode = OPMODE_AUTO;
99 static int debug = 0; /* debug output */
100 static int once = 0; /* no continous stereo monitoring */
101 static int amsound = 0; /* hard-wire AM sound at 6.5 Hz (france),
102 the autoscan seems work well only with FM... */
103 static int standard = 1; /* Override auto detect of audio standard, if needed. */
104 static int dolby = 0;
106 static int stereo_threshold = 0x190; /* a2 threshold for stereo/bilingual
107 (msp34xxg only) 0x00a0-0x03c0 */
108 #define DFP_COUNT 0x41
109 static const int bl_dfp[] = {
110 0x00, 0x01, 0x02, 0x03, 0x06, 0x08, 0x09, 0x0a,
111 0x0b, 0x0d, 0x0e, 0x10
114 #define IS_MSP34XX_G(msp) ((msp)->opmode==2)
128 int main, second; /* sound carrier */
130 int source; /* see msp34xxg_set_source */
137 int left, right; /* volume */
140 /* shadow register set */
141 int dfp_regs[DFP_COUNT];
144 struct task_struct *kthread;
145 wait_queue_head_t wq;
150 #define MIN(a,b) (((a)>(b))?(b):(a))
151 #define MAX(a,b) (((a)>(b))?(a):(b))
152 #define HAVE_NICAM(msp) (((msp->rev2>>8) & 0xff) != 00)
153 #define HAVE_SIMPLE(msp) ((msp->rev1 & 0xff) >= 'D'-'@')
154 #define HAVE_SIMPLER(msp) ((msp->rev1 & 0xff) >= 'G'-'@')
155 #define HAVE_RADIO(msp) ((msp->rev1 & 0xff) >= 'G'-'@')
157 #define VIDEO_MODE_RADIO 16 /* norm magic for radio mode */
159 /* ---------------------------------------------------------------------- */
162 module_param(opmode, int, 0444);
165 module_param(once, int, 0644);
166 module_param(debug, int, 0644);
167 module_param(stereo_threshold, int, 0644);
168 module_param(standard, int, 0644);
169 module_param(amsound, int, 0644);
170 module_param(dolby, int, 0644);
172 MODULE_PARM_DESC(opmode, "Forces a MSP3400 opmode. 0=Manual, 1=Simple, 2=Simpler");
173 MODULE_PARM_DESC(once, "No continuous stereo monitoring");
174 MODULE_PARM_DESC(debug, "Enable debug messages");
175 MODULE_PARM_DESC(stereo_threshold, "Sets signal threshold to activate stereo");
176 MODULE_PARM_DESC(standard, "Specify audio standard: 32 = NTSC, 64 = radio, Default: Autodetect");
177 MODULE_PARM_DESC(amsound, "Hardwire AM sound at 6.5Hz (France), FM can autoscan");
178 MODULE_PARM_DESC(dolby, "Activates Dolby processsing");
180 /* ---------------------------------------------------------------------- */
182 #define I2C_MSP3400C 0x80
183 #define I2C_MSP3400C_ALT 0x88
185 #define I2C_MSP3400C_DEM 0x10
186 #define I2C_MSP3400C_DFP 0x12
188 /* Addresses to scan */
189 static unsigned short normal_i2c[] = {
191 I2C_MSP3400C_ALT >> 1,
196 MODULE_DESCRIPTION("device driver for msp34xx TV sound processor");
197 MODULE_AUTHOR("Gerd Knorr");
198 MODULE_LICENSE("GPL");
200 /* ----------------------------------------------------------------------- */
201 /* functions for talking to the MSP3400C Sound processor */
203 static int msp3400c_reset(struct i2c_client *client)
205 /* reset and read revision code */
206 static char reset_off[3] = { 0x00, 0x80, 0x00 };
207 static char reset_on[3] = { 0x00, 0x00, 0x00 };
208 static char write[3] = { I2C_MSP3400C_DFP + 1, 0x00, 0x1e };
210 struct i2c_msg reset[2] = {
211 { client->addr, I2C_M_IGNORE_NAK, 3, reset_off },
212 { client->addr, I2C_M_IGNORE_NAK, 3, reset_on },
214 struct i2c_msg test[2] = {
215 { client->addr, 0, 3, write },
216 { client->addr, I2C_M_RD, 2, read },
219 msp3400_dbg_highvol("msp3400c_reset\n");
220 if ( (1 != i2c_transfer(client->adapter,&reset[0],1)) ||
221 (1 != i2c_transfer(client->adapter,&reset[1],1)) ||
222 (2 != i2c_transfer(client->adapter,test,2)) ) {
223 msp3400_err("chip reset failed\n");
229 static int msp3400c_read(struct i2c_client *client, int dev, int addr)
233 unsigned char write[3];
234 unsigned char read[2];
235 struct i2c_msg msgs[2] = {
236 { client->addr, 0, 3, write },
237 { client->addr, I2C_M_RD, 2, read }
241 write[1] = addr >> 8;
242 write[2] = addr & 0xff;
244 for (err = 0; err < 3;) {
245 if (2 == i2c_transfer(client->adapter,msgs,2))
248 msp3400_warn("I/O error #%d (read 0x%02x/0x%02x)\n", err,
250 current->state = TASK_INTERRUPTIBLE;
251 schedule_timeout(msecs_to_jiffies(10));
254 msp3400_warn("giving up, resetting chip. Sound will go off, sorry folks :-|\n");
255 msp3400c_reset(client);
258 retval = read[0] << 8 | read[1];
259 msp3400_dbg_highvol("msp3400c_read(0x%x, 0x%x): 0x%x\n", dev, addr, retval);
263 static int msp3400c_write(struct i2c_client *client, int dev, int addr, int val)
266 unsigned char buffer[5];
269 buffer[1] = addr >> 8;
270 buffer[2] = addr & 0xff;
271 buffer[3] = val >> 8;
272 buffer[4] = val & 0xff;
274 msp3400_dbg_highvol("msp3400c_write(0x%x, 0x%x, 0x%x)\n", dev, addr, val);
275 for (err = 0; err < 3;) {
276 if (5 == i2c_master_send(client, buffer, 5))
279 msp3400_warn("I/O error #%d (write 0x%02x/0x%02x)\n", err,
281 current->state = TASK_INTERRUPTIBLE;
282 schedule_timeout(msecs_to_jiffies(10));
285 msp3400_warn("giving up, reseting chip. Sound will go off, sorry folks :-|\n");
286 msp3400c_reset(client);
292 /* ------------------------------------------------------------------------ */
294 /* This macro is allowed for *constants* only, gcc must calculate it
295 at compile time. Remember -- no floats in kernel mode */
296 #define MSP_CARRIER(freq) ((int)((float)(freq/18.432)*(1<<24)))
298 #define MSP_MODE_AM_DETECT 0
299 #define MSP_MODE_FM_RADIO 2
300 #define MSP_MODE_FM_TERRA 3
301 #define MSP_MODE_FM_SAT 4
302 #define MSP_MODE_FM_NICAM1 5
303 #define MSP_MODE_FM_NICAM2 6
304 #define MSP_MODE_AM_NICAM 7
305 #define MSP_MODE_BTSC 8
306 #define MSP_MODE_EXTERN 9
308 static struct MSP_INIT_DATA_DEM {
317 } msp_init_data[] = {
318 { /* AM (for carrier detect / msp3400) */
319 {75, 19, 36, 35, 39, 40},
320 {75, 19, 36, 35, 39, 40},
321 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
322 0x00d0, 0x0500, 0x0020, 0x3000
323 },{ /* AM (for carrier detect / msp3410) */
324 {-1, -1, -8, 2, 59, 126},
325 {-1, -1, -8, 2, 59, 126},
326 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
327 0x00d0, 0x0100, 0x0020, 0x3000
329 {-8, -8, 4, 6, 78, 107},
330 {-8, -8, 4, 6, 78, 107},
331 MSP_CARRIER(10.7), MSP_CARRIER(10.7),
332 0x00d0, 0x0480, 0x0020, 0x3000
333 },{ /* Terrestial FM-mono + FM-stereo */
334 {3, 18, 27, 48, 66, 72},
335 {3, 18, 27, 48, 66, 72},
336 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
337 0x00d0, 0x0480, 0x0030, 0x3000
338 },{ /* Sat FM-mono */
339 { 1, 9, 14, 24, 33, 37},
340 { 3, 18, 27, 48, 66, 72},
341 MSP_CARRIER(6.5), MSP_CARRIER(6.5),
342 0x00c6, 0x0480, 0x0000, 0x3000
343 },{ /* NICAM/FM -- B/G (5.5/5.85), D/K (6.5/5.85) */
344 {-2, -8, -10, 10, 50, 86},
345 {3, 18, 27, 48, 66, 72},
346 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
347 0x00d0, 0x0040, 0x0120, 0x3000
348 },{ /* NICAM/FM -- I (6.0/6.552) */
349 {2, 4, -6, -4, 40, 94},
350 {3, 18, 27, 48, 66, 72},
351 MSP_CARRIER(6.0), MSP_CARRIER(6.0),
352 0x00d0, 0x0040, 0x0120, 0x3000
353 },{ /* NICAM/AM -- L (6.5/5.85) */
354 {-2, -8, -10, 10, 50, 86},
355 {-4, -12, -9, 23, 79, 126},
356 MSP_CARRIER(6.5), MSP_CARRIER(6.5),
357 0x00c6, 0x0140, 0x0120, 0x7c03
361 struct CARRIER_DETECT {
366 static struct CARRIER_DETECT carrier_detect_main[] = {
368 { MSP_CARRIER(4.5), "4.5 NTSC" },
369 { MSP_CARRIER(5.5), "5.5 PAL B/G" },
370 { MSP_CARRIER(6.0), "6.0 PAL I" },
371 { MSP_CARRIER(6.5), "6.5 PAL D/K + SAT + SECAM" }
374 static struct CARRIER_DETECT carrier_detect_55[] = {
376 { MSP_CARRIER(5.7421875), "5.742 PAL B/G FM-stereo" },
377 { MSP_CARRIER(5.85), "5.85 PAL B/G NICAM" }
380 static struct CARRIER_DETECT carrier_detect_65[] = {
381 /* PAL SAT / SECAM */
382 { MSP_CARRIER(5.85), "5.85 PAL D/K + SECAM NICAM" },
383 { MSP_CARRIER(6.2578125), "6.25 PAL D/K1 FM-stereo" },
384 { MSP_CARRIER(6.7421875), "6.74 PAL D/K2 FM-stereo" },
385 { MSP_CARRIER(7.02), "7.02 PAL SAT FM-stereo s/b" },
386 { MSP_CARRIER(7.20), "7.20 PAL SAT FM-stereo s" },
387 { MSP_CARRIER(7.38), "7.38 PAL SAT FM-stereo b" },
390 #define CARRIER_COUNT(x) (sizeof(x)/sizeof(struct CARRIER_DETECT))
392 /* ----------------------------------------------------------------------- *
393 * bits 9 8 5 - SCART DSP input Select:
394 * 0 0 0 - SCART 1 to DSP input (reset position)
395 * 0 1 0 - MONO to DSP input
396 * 1 0 0 - SCART 2 to DSP input
397 * 1 1 1 - Mute DSP input
399 * bits 11 10 6 - SCART 1 Output Select:
400 * 0 0 0 - undefined (reset position)
401 * 0 1 0 - SCART 2 Input to SCART 1 Output (for devices with 2 SCARTS)
402 * 1 0 0 - MONO input to SCART 1 Output
403 * 1 1 0 - SCART 1 DA to SCART 1 Output
404 * 0 0 1 - SCART 2 DA to SCART 1 Output
405 * 0 1 1 - SCART 1 Input to SCART 1 Output
406 * 1 1 1 - Mute SCART 1 Output
408 * bits 13 12 7 - SCART 2 Output Select (for devices with 2 Output SCART):
409 * 0 0 0 - SCART 1 DA to SCART 2 Output (reset position)
410 * 0 1 0 - SCART 1 Input to SCART 2 Output
411 * 1 0 0 - MONO input to SCART 2 Output
412 * 0 0 1 - SCART 2 DA to SCART 2 Output
413 * 0 1 1 - SCART 2 Input to SCART 2 Output
414 * 1 1 0 - Mute SCART 2 Output
416 * Bits 4 to 0 should be zero.
417 * ----------------------------------------------------------------------- */
419 static int scarts[3][9] = {
420 /* MASK IN1 IN2 IN1_DA IN2_DA IN3 IN4 MONO MUTE */
421 /* SCART DSP Input select */
422 { 0x0320, 0x0000, 0x0200, -1, -1, 0x0300, 0x0020, 0x0100, 0x0320 },
423 /* SCART1 Output select */
424 { 0x0c40, 0x0440, 0x0400, 0x0c00, 0x0040, 0x0000, 0x0840, 0x0800, 0x0c40 },
425 /* SCART2 Output select */
426 { 0x3080, 0x1000, 0x1080, 0x0000, 0x0080, 0x2080, 0x3080, 0x2000, 0x3000 },
429 static char *scart_names[] = {
430 "mask", "in1", "in2", "in1 da", "in2 da", "in3", "in4", "mono", "mute"
433 static void msp3400c_set_scart(struct i2c_client *client, int in, int out)
435 struct msp3400c *msp = i2c_get_clientdata(client);
439 if (in >= 1 && in <= 8 && out >= 0 && out <= 2) {
440 if (-1 == scarts[out][in])
443 msp->acb &= ~scarts[out][SCART_MASK];
444 msp->acb |= scarts[out][in];
446 msp->acb = 0xf60; /* Mute Input and SCART 1 Output */
448 msp3400_dbg("scart switch: %s => %d (ACB=0x%04x)\n",
449 scart_names[in], out, msp->acb);
450 msp3400c_write(client,I2C_MSP3400C_DFP, 0x13, msp->acb);
452 /* Sets I2S speed 0 = 1.024 Mbps, 1 = 2.048 Mbps */
453 msp3400c_write(client,I2C_MSP3400C_DEM, 0x40, msp->i2s_mode);
456 /* ------------------------------------------------------------------------ */
458 static void msp3400c_setcarrier(struct i2c_client *client, int cdo1, int cdo2)
460 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0093, cdo1 & 0xfff);
461 msp3400c_write(client,I2C_MSP3400C_DEM, 0x009b, cdo1 >> 12);
462 msp3400c_write(client,I2C_MSP3400C_DEM, 0x00a3, cdo2 & 0xfff);
463 msp3400c_write(client,I2C_MSP3400C_DEM, 0x00ab, cdo2 >> 12);
464 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0056, 0); /*LOAD_REG_1/2*/
467 static void msp3400c_setvolume(struct i2c_client *client,
468 int muted, int left, int right)
470 int vol = 0, val = 0, balance = 0;
473 /* 0x7f instead if 0x73 here has sound quality issues,
474 * probably due to overmodulation + clipping ... */
475 vol = (left > right) ? left : right;
476 val = (vol * 0x73 / 65535) << 8;
479 balance = ((right - left) * 127) / vol;
482 msp3400_dbg("setvolume: mute=%s %d:%d v=0x%02x b=0x%02x\n",
483 muted ? "on" : "off", left, right, val >> 8, balance);
484 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0000, val); /* loudspeaker */
485 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0006, val); /* headphones */
486 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0007,
487 muted ? 0x1 : (val | 0x1));
488 msp3400c_write(client, I2C_MSP3400C_DFP, 0x0001, balance << 8);
491 static void msp3400c_setbass(struct i2c_client *client, int bass)
493 int val = ((bass-32768) * 0x60 / 65535) << 8;
495 msp3400_dbg("setbass: %d 0x%02x\n", bass, val >> 8);
496 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0002, val); /* loudspeaker */
499 static void msp3400c_settreble(struct i2c_client *client, int treble)
501 int val = ((treble-32768) * 0x60 / 65535) << 8;
503 msp3400_dbg("settreble: %d 0x%02x\n",treble, val>>8);
504 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0003, val); /* loudspeaker */
507 static void msp3400c_setmode(struct i2c_client *client, int type)
509 struct msp3400c *msp = i2c_get_clientdata(client);
512 msp3400_dbg("setmode: %d\n",type);
514 msp->audmode = V4L2_TUNER_MODE_MONO;
515 msp->rxsubchans = V4L2_TUNER_SUB_MONO;
517 msp3400c_write(client,I2C_MSP3400C_DEM, 0x00bb, /* ad_cv */
518 msp_init_data[type].ad_cv);
520 for (i = 5; i >= 0; i--) /* fir 1 */
521 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0001,
522 msp_init_data[type].fir1[i]);
524 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005, 0x0004); /* fir 2 */
525 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005, 0x0040);
526 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005, 0x0000);
527 for (i = 5; i >= 0; i--)
528 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005,
529 msp_init_data[type].fir2[i]);
531 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0083, /* MODE_REG */
532 msp_init_data[type].mode_reg);
534 msp3400c_setcarrier(client, msp_init_data[type].cdo1,
535 msp_init_data[type].cdo2);
537 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0056, 0); /*LOAD_REG_1/2*/
540 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008,
542 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009,
544 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b,
545 msp_init_data[type].dfp_src);
547 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008,
548 msp_init_data[type].dfp_src);
549 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009,
550 msp_init_data[type].dfp_src);
551 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b,
552 msp_init_data[type].dfp_src);
554 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000a,
555 msp_init_data[type].dfp_src);
556 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000e,
557 msp_init_data[type].dfp_matrix);
559 if (HAVE_NICAM(msp)) {
561 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0010, 0x5a00); /* was: 0x3000 */
565 /* given a bitmask of VIDEO_SOUND_XXX returns the "best" in the bitmask */
566 static int best_video_sound(int rxsubchans)
568 if (rxsubchans & V4L2_TUNER_SUB_STEREO)
569 return V4L2_TUNER_MODE_STEREO;
570 if (rxsubchans & V4L2_TUNER_SUB_LANG1)
571 return V4L2_TUNER_MODE_LANG1;
572 if (rxsubchans & V4L2_TUNER_SUB_LANG2)
573 return V4L2_TUNER_MODE_LANG2;
574 return V4L2_TUNER_MODE_MONO;
577 /* turn on/off nicam + stereo */
578 static void msp3400c_setstereo(struct i2c_client *client, int mode)
580 static char *strmode[] = { "0", "mono", "stereo", "3",
581 "lang1", "5", "6", "7", "lang2"
583 struct msp3400c *msp = i2c_get_clientdata(client);
584 int nicam = 0; /* channel source: FM/AM or nicam */
587 if (IS_MSP34XX_G(msp)) {
588 /* this method would break everything, let's make sure
592 ("DEBUG WARNING setstereo called with mode=%d instead of set_source (ignored)\n",
597 /* switch demodulator */
599 case MSP_MODE_FM_TERRA:
600 msp3400_dbg("FM setstereo: %s\n", strmode[mode]);
601 msp3400c_setcarrier(client,msp->second,msp->main);
603 case V4L2_TUNER_MODE_STEREO:
604 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000e, 0x3001);
606 case V4L2_TUNER_MODE_MONO:
607 case V4L2_TUNER_MODE_LANG1:
608 case V4L2_TUNER_MODE_LANG2:
609 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000e, 0x3000);
613 case MSP_MODE_FM_SAT:
614 msp3400_dbg("SAT setstereo: %s\n", strmode[mode]);
616 case V4L2_TUNER_MODE_MONO:
617 msp3400c_setcarrier(client, MSP_CARRIER(6.5), MSP_CARRIER(6.5));
619 case V4L2_TUNER_MODE_STEREO:
620 msp3400c_setcarrier(client, MSP_CARRIER(7.2), MSP_CARRIER(7.02));
622 case V4L2_TUNER_MODE_LANG1:
623 msp3400c_setcarrier(client, MSP_CARRIER(7.38), MSP_CARRIER(7.02));
625 case V4L2_TUNER_MODE_LANG2:
626 msp3400c_setcarrier(client, MSP_CARRIER(7.38), MSP_CARRIER(7.02));
630 case MSP_MODE_FM_NICAM1:
631 case MSP_MODE_FM_NICAM2:
632 case MSP_MODE_AM_NICAM:
633 msp3400_dbg("NICAM setstereo: %s\n",strmode[mode]);
634 msp3400c_setcarrier(client,msp->second,msp->main);
639 msp3400_dbg("BTSC setstereo: %s\n",strmode[mode]);
642 case MSP_MODE_EXTERN:
643 msp3400_dbg("extern setstereo: %s\n",strmode[mode]);
646 case MSP_MODE_FM_RADIO:
647 msp3400_dbg("FM-Radio setstereo: %s\n",strmode[mode]);
650 msp3400_dbg("mono setstereo\n");
655 switch (best_video_sound(mode)) {
656 case V4L2_TUNER_MODE_STEREO:
657 src = 0x0020 | nicam;
659 case V4L2_TUNER_MODE_MONO:
660 if (msp->mode == MSP_MODE_AM_NICAM) {
661 msp3400_dbg("switching to AM mono\n");
662 /* AM mono decoding is handled by tuner, not MSP chip */
663 /* SCART switching control register */
664 msp3400c_set_scart(client,SCART_MONO,0);
668 case V4L2_TUNER_MODE_LANG1:
669 src = 0x0000 | nicam;
671 case V4L2_TUNER_MODE_LANG2:
672 src = 0x0010 | nicam;
675 msp3400_dbg("setstereo final source/matrix = 0x%x\n", src);
678 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008,0x0520);
679 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009,0x0620);
680 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000a,src);
681 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b,src);
683 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008,src);
684 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009,src);
685 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000a,src);
686 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b,src);
691 msp3400c_print_mode(struct i2c_client *client)
693 struct msp3400c *msp = i2c_get_clientdata(client);
695 if (msp->main == msp->second) {
696 msp3400_dbg("mono sound carrier: %d.%03d MHz\n",
697 msp->main/910000,(msp->main/910)%1000);
699 msp3400_dbg("main sound carrier: %d.%03d MHz\n",
700 msp->main/910000,(msp->main/910)%1000);
702 if (msp->mode == MSP_MODE_FM_NICAM1 || msp->mode == MSP_MODE_FM_NICAM2)
703 msp3400_dbg("NICAM/FM carrier : %d.%03d MHz\n",
704 msp->second/910000,(msp->second/910)%1000);
705 if (msp->mode == MSP_MODE_AM_NICAM)
706 msp3400_dbg("NICAM/AM carrier : %d.%03d MHz\n",
707 msp->second/910000,(msp->second/910)%1000);
708 if (msp->mode == MSP_MODE_FM_TERRA &&
709 msp->main != msp->second) {
710 msp3400_dbg("FM-stereo carrier : %d.%03d MHz\n",
711 msp->second/910000,(msp->second/910)%1000);
715 #define MSP3400_MAX 4
716 static struct i2c_client *msps[MSP3400_MAX];
717 static void msp3400c_restore_dfp(struct i2c_client *client)
719 struct msp3400c *msp = i2c_get_clientdata(client);
722 for (i = 0; i < DFP_COUNT; i++) {
723 if (-1 == msp->dfp_regs[i])
725 msp3400c_write(client, I2C_MSP3400C_DFP, i, msp->dfp_regs[i]);
729 /* if the dfp_regs is set, set what's in there. Otherwise, set the default value */
730 static int msp3400c_write_dfp_with_default(struct i2c_client *client,
731 int addr, int default_value)
733 struct msp3400c *msp = i2c_get_clientdata(client);
734 int value = default_value;
735 if (addr < DFP_COUNT && -1 != msp->dfp_regs[addr])
736 value = msp->dfp_regs[addr];
737 return msp3400c_write(client, I2C_MSP3400C_DFP, addr, value);
740 /* ----------------------------------------------------------------------- */
742 struct REGISTER_DUMP {
747 struct REGISTER_DUMP d1[] = {
748 {0x007e, "autodetect"},
749 {0x0023, "C_AD_BITS "},
750 {0x0038, "ADD_BITS "},
751 {0x003e, "CIB_BITS "},
752 {0x0057, "ERROR_RATE"},
755 static int autodetect_stereo(struct i2c_client *client)
757 struct msp3400c *msp = i2c_get_clientdata(client);
759 int rxsubchans = msp->rxsubchans;
760 int newnicam = msp->nicam_on;
764 case MSP_MODE_FM_TERRA:
765 val = msp3400c_read(client, I2C_MSP3400C_DFP, 0x18);
768 msp3400_dbg("stereo detect register: %d\n",val);
770 rxsubchans = V4L2_TUNER_SUB_STEREO | V4L2_TUNER_SUB_MONO;
771 } else if (val < -4096) {
772 rxsubchans = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
774 rxsubchans = V4L2_TUNER_SUB_MONO;
778 case MSP_MODE_FM_NICAM1:
779 case MSP_MODE_FM_NICAM2:
780 case MSP_MODE_AM_NICAM:
781 val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x23);
782 msp3400_dbg("nicam sync=%d, mode=%d\n",
783 val & 1, (val & 0x1e) >> 1);
787 switch ((val & 0x1e) >> 1) {
790 rxsubchans = V4L2_TUNER_SUB_STEREO;
794 rxsubchans = V4L2_TUNER_SUB_MONO
795 | V4L2_TUNER_SUB_LANG1;
799 rxsubchans = V4L2_TUNER_SUB_MONO
800 | V4L2_TUNER_SUB_LANG1
801 | V4L2_TUNER_SUB_LANG2;
804 rxsubchans = V4L2_TUNER_SUB_MONO;
810 rxsubchans = V4L2_TUNER_SUB_MONO;
814 val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x200);
815 msp3400_dbg("status=0x%x (pri=%s, sec=%s, %s%s%s)\n",
817 (val & 0x0002) ? "no" : "yes",
818 (val & 0x0004) ? "no" : "yes",
819 (val & 0x0040) ? "stereo" : "mono",
820 (val & 0x0080) ? ", nicam 2nd mono" : "",
821 (val & 0x0100) ? ", bilingual/SAP" : "");
822 rxsubchans = V4L2_TUNER_SUB_MONO;
823 if (val & 0x0040) rxsubchans |= V4L2_TUNER_SUB_STEREO;
824 if (val & 0x0100) rxsubchans |= V4L2_TUNER_SUB_LANG1;
827 if (rxsubchans != msp->rxsubchans) {
829 msp3400_dbg("watch: rxsubchans %d => %d\n",
830 msp->rxsubchans,rxsubchans);
831 msp->rxsubchans = rxsubchans;
833 if (newnicam != msp->nicam_on) {
835 msp3400_dbg("watch: nicam %d => %d\n",
836 msp->nicam_on,newnicam);
837 msp->nicam_on = newnicam;
843 * A kernel thread for msp3400 control -- we don't want to block the
844 * in the ioctl while doing the sound carrier & stereo detect
847 static int msp34xx_sleep(struct msp3400c *msp, int timeout)
849 DECLARE_WAITQUEUE(wait, current);
851 add_wait_queue(&msp->wq, &wait);
852 if (!kthread_should_stop()) {
854 set_current_state(TASK_INTERRUPTIBLE);
857 schedule_timeout_interruptible
858 (msecs_to_jiffies(timeout));
862 remove_wait_queue(&msp->wq, &wait);
867 /* stereo/multilang monitoring */
868 static void watch_stereo(struct i2c_client *client)
870 struct msp3400c *msp = i2c_get_clientdata(client);
872 if (autodetect_stereo(client)) {
873 if (msp->stereo & V4L2_TUNER_MODE_STEREO)
874 msp3400c_setstereo(client, V4L2_TUNER_MODE_STEREO);
875 else if (msp->stereo & VIDEO_SOUND_LANG1)
876 msp3400c_setstereo(client, V4L2_TUNER_MODE_LANG1);
878 msp3400c_setstereo(client, V4L2_TUNER_MODE_MONO);
882 msp->watch_stereo = 0;
885 static int msp3400c_thread(void *data)
887 struct i2c_client *client = data;
888 struct msp3400c *msp = i2c_get_clientdata(client);
889 struct CARRIER_DETECT *cd;
890 int count, max1,max2,val1,val2, val,this;
892 msp3400_info("msp3400 daemon started\n");
894 msp3400_dbg_mediumvol("msp3400 thread: sleep\n");
895 msp34xx_sleep(msp,-1);
896 msp3400_dbg_mediumvol("msp3400 thread: wakeup\n");
899 msp3400_dbg("thread: restart scan\n");
901 if (kthread_should_stop())
904 if (VIDEO_MODE_RADIO == msp->norm ||
905 MSP_MODE_EXTERN == msp->mode) {
906 /* no carrier scan, just unmute */
907 msp3400_info("thread: no carrier scan\n");
908 msp3400c_setvolume(client, msp->muted, msp->left, msp->right);
913 msp3400c_setvolume(client, msp->muted, 0, 0);
914 msp3400c_setmode(client, MSP_MODE_AM_DETECT /* +1 */ );
917 msp->watch_stereo = 0;
919 /* some time for the tuner to sync */
920 if (msp34xx_sleep(msp,200))
923 /* carrier detect pass #1 -- main carrier */
924 cd = carrier_detect_main;
925 count = CARRIER_COUNT(carrier_detect_main);
927 if (amsound && (msp->norm == VIDEO_MODE_SECAM)) {
928 /* autodetect doesn't work well with AM ... */
931 msp3400_dbg("AM sound override\n");
934 for (this = 0; this < count; this++) {
935 msp3400c_setcarrier(client, cd[this].cdo,cd[this].cdo);
936 if (msp34xx_sleep(msp,100))
938 val = msp3400c_read(client, I2C_MSP3400C_DFP, 0x1b);
942 val1 = val, max1 = this;
943 msp3400_dbg("carrier1 val: %5d / %s\n", val,cd[this].name);
946 /* carrier detect pass #2 -- second (stereo) carrier */
949 cd = carrier_detect_55;
950 count = CARRIER_COUNT(carrier_detect_55);
953 cd = carrier_detect_65;
954 count = CARRIER_COUNT(carrier_detect_65);
964 if (amsound && (msp->norm == VIDEO_MODE_SECAM)) {
965 /* autodetect doesn't work well with AM ... */
970 for (this = 0; this < count; this++) {
971 msp3400c_setcarrier(client, cd[this].cdo,cd[this].cdo);
972 if (msp34xx_sleep(msp,100))
974 val = msp3400c_read(client, I2C_MSP3400C_DFP, 0x1b);
978 val2 = val, max2 = this;
979 msp3400_dbg("carrier2 val: %5d / %s\n", val,cd[this].name);
982 /* programm the msp3400 according to the results */
983 msp->main = carrier_detect_main[max1].cdo;
988 msp->second = carrier_detect_55[max2].cdo;
989 msp3400c_setmode(client, MSP_MODE_FM_TERRA);
991 msp3400c_setstereo(client, V4L2_TUNER_MODE_MONO);
992 msp->watch_stereo = 1;
993 } else if (max2 == 1 && HAVE_NICAM(msp)) {
995 msp->second = carrier_detect_55[max2].cdo;
996 msp3400c_setmode(client, MSP_MODE_FM_NICAM1);
998 msp3400c_setcarrier(client, msp->second, msp->main);
999 msp->watch_stereo = 1;
1006 msp->second = MSP_CARRIER(6.552);
1007 msp3400c_setmode(client, MSP_MODE_FM_NICAM2);
1009 msp3400c_setcarrier(client, msp->second, msp->main);
1010 msp->watch_stereo = 1;
1013 if (max2 == 1 || max2 == 2) {
1015 msp->second = carrier_detect_65[max2].cdo;
1016 msp3400c_setmode(client, MSP_MODE_FM_TERRA);
1018 msp3400c_setstereo(client, V4L2_TUNER_MODE_MONO);
1019 msp->watch_stereo = 1;
1020 } else if (max2 == 0 &&
1021 msp->norm == VIDEO_MODE_SECAM) {
1022 /* L NICAM or AM-mono */
1023 msp->second = carrier_detect_65[max2].cdo;
1024 msp3400c_setmode(client, MSP_MODE_AM_NICAM);
1026 msp3400c_setstereo(client, V4L2_TUNER_MODE_MONO);
1027 msp3400c_setcarrier(client, msp->second, msp->main);
1028 /* volume prescale for SCART (AM mono input) */
1029 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000d, 0x1900);
1030 msp->watch_stereo = 1;
1031 } else if (max2 == 0 && HAVE_NICAM(msp)) {
1033 msp->second = carrier_detect_65[max2].cdo;
1034 msp3400c_setmode(client, MSP_MODE_FM_NICAM1);
1036 msp3400c_setcarrier(client, msp->second, msp->main);
1037 msp->watch_stereo = 1;
1045 msp->second = carrier_detect_main[max1].cdo;
1046 msp3400c_setmode(client, MSP_MODE_FM_TERRA);
1048 msp3400c_setcarrier(client, msp->second, msp->main);
1049 msp->rxsubchans = V4L2_TUNER_SUB_MONO;
1050 msp3400c_setstereo(client, V4L2_TUNER_MODE_MONO);
1055 msp3400c_setvolume(client, msp->muted, msp->left, msp->right);
1056 msp3400c_restore_dfp(client);
1059 msp3400c_print_mode(client);
1061 /* monitor tv audio mode */
1062 while (msp->watch_stereo) {
1063 if (msp34xx_sleep(msp,5000))
1065 watch_stereo(client);
1068 msp3400_dbg("thread: exit\n");
1072 /* ----------------------------------------------------------------------- */
1073 /* this one uses the automatic sound standard detection of newer */
1074 /* msp34xx chip versions */
1076 static struct MODES {
1081 { 0x0000, 0, 0, "ERROR" },
1082 { 0x0001, 0, 0, "autodetect start" },
1083 { 0x0002, MSP_CARRIER(4.5), MSP_CARRIER(4.72), "4.5/4.72 M Dual FM-Stereo" },
1084 { 0x0003, MSP_CARRIER(5.5), MSP_CARRIER(5.7421875), "5.5/5.74 B/G Dual FM-Stereo" },
1085 { 0x0004, MSP_CARRIER(6.5), MSP_CARRIER(6.2578125), "6.5/6.25 D/K1 Dual FM-Stereo" },
1086 { 0x0005, MSP_CARRIER(6.5), MSP_CARRIER(6.7421875), "6.5/6.74 D/K2 Dual FM-Stereo" },
1087 { 0x0006, MSP_CARRIER(6.5), MSP_CARRIER(6.5), "6.5 D/K FM-Mono (HDEV3)" },
1088 { 0x0008, MSP_CARRIER(5.5), MSP_CARRIER(5.85), "5.5/5.85 B/G NICAM FM" },
1089 { 0x0009, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85 L NICAM AM" },
1090 { 0x000a, MSP_CARRIER(6.0), MSP_CARRIER(6.55), "6.0/6.55 I NICAM FM" },
1091 { 0x000b, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85 D/K NICAM FM" },
1092 { 0x000c, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85 D/K NICAM FM (HDEV2)" },
1093 { 0x0020, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5 M BTSC-Stereo" },
1094 { 0x0021, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5 M BTSC-Mono + SAP" },
1095 { 0x0030, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5 M EIA-J Japan Stereo" },
1096 { 0x0040, MSP_CARRIER(10.7), MSP_CARRIER(10.7), "10.7 FM-Stereo Radio" },
1097 { 0x0050, MSP_CARRIER(6.5), MSP_CARRIER(6.5), "6.5 SAT-Mono" },
1098 { 0x0051, MSP_CARRIER(7.02), MSP_CARRIER(7.20), "7.02/7.20 SAT-Stereo" },
1099 { 0x0060, MSP_CARRIER(7.2), MSP_CARRIER(7.2), "7.2 SAT ADR" },
1100 { -1, 0, 0, NULL }, /* EOF */
1103 static inline const char *msp34xx_standard_mode_name(int mode)
1106 for (i = 0; modelist[i].name != NULL; i++)
1107 if (modelist[i].retval == mode)
1108 return modelist[i].name;
1112 static int msp34xx_modus(struct i2c_client *client, int norm)
1115 case VIDEO_MODE_PAL:
1116 msp3400_dbg("video mode selected to PAL\n");
1119 /* experimental: not sure this works with all chip versions */
1122 /* previous value, try this if it breaks ... */
1125 case VIDEO_MODE_NTSC: /* BTSC */
1126 msp3400_dbg("video mode selected to NTSC\n");
1128 case VIDEO_MODE_SECAM:
1129 msp3400_dbg("video mode selected to SECAM\n");
1131 case VIDEO_MODE_RADIO:
1132 msp3400_dbg("video mode selected to Radio\n");
1134 case VIDEO_MODE_AUTO:
1135 msp3400_dbg("video mode selected to Auto\n");
1142 static int msp34xx_standard(int norm)
1145 case VIDEO_MODE_PAL:
1147 case VIDEO_MODE_NTSC: /* BTSC */
1149 case VIDEO_MODE_SECAM:
1151 case VIDEO_MODE_RADIO:
1158 static int msp3410d_thread(void *data)
1160 struct i2c_client *client = data;
1161 struct msp3400c *msp = i2c_get_clientdata(client);
1164 msp3400_info("msp3410 daemon started\n");
1166 msp3400_dbg_mediumvol("msp3410 thread: sleep\n");
1167 msp34xx_sleep(msp,-1);
1168 msp3400_dbg_mediumvol("msp3410 thread: wakeup\n");
1171 msp3400_dbg("thread: restart scan\n");
1173 if (kthread_should_stop())
1176 if (msp->mode == MSP_MODE_EXTERN) {
1177 /* no carrier scan needed, just unmute */
1178 msp3400_dbg("thread: no carrier scan\n");
1179 msp3400c_setvolume(client, msp->muted, msp->left, msp->right);
1183 /* put into sane state (and mute) */
1184 msp3400c_reset(client);
1186 /* some time for the tuner to sync */
1187 if (msp34xx_sleep(msp,200))
1190 /* start autodetect */
1191 mode = msp34xx_modus(client, msp->norm);
1192 std = msp34xx_standard(msp->norm);
1193 msp3400c_write(client, I2C_MSP3400C_DEM, 0x30, mode);
1194 msp3400c_write(client, I2C_MSP3400C_DEM, 0x20, std);
1195 msp->watch_stereo = 0;
1198 msp3400_dbg("setting mode: %s (0x%04x)\n",
1199 msp34xx_standard_mode_name(std) ,std);
1202 /* programmed some specific mode */
1205 /* triggered autodetect */
1207 if (msp34xx_sleep(msp,100))
1211 val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x7e);
1214 msp3400_dbg("detection still in progress\n");
1217 for (i = 0; modelist[i].name != NULL; i++)
1218 if (modelist[i].retval == val)
1220 msp3400_dbg("current mode: %s (0x%04x)\n",
1221 modelist[i].name ? modelist[i].name : "unknown",
1223 msp->main = modelist[i].main;
1224 msp->second = modelist[i].second;
1226 if (amsound && (msp->norm == VIDEO_MODE_SECAM) && (val != 0x0009)) {
1227 /* autodetection has failed, let backup */
1228 msp3400_dbg("autodetection failed,"
1229 " switching to backup mode: %s (0x%04x)\n",
1230 modelist[8].name ? modelist[8].name : "unknown",val);
1232 msp3400c_write(client, I2C_MSP3400C_DEM, 0x20, val);
1235 /* set various prescales */
1236 msp3400c_write(client, I2C_MSP3400C_DFP, 0x0d, 0x1900); /* scart */
1237 msp3400c_write(client, I2C_MSP3400C_DFP, 0x0e, 0x2403); /* FM */
1238 msp3400c_write(client, I2C_MSP3400C_DFP, 0x10, 0x5a00); /* nicam */
1242 case 0x0008: /* B/G NICAM */
1243 case 0x000a: /* I NICAM */
1245 msp->mode = MSP_MODE_FM_NICAM1;
1247 msp->mode = MSP_MODE_FM_NICAM2;
1248 /* just turn on stereo */
1249 msp->rxsubchans = V4L2_TUNER_SUB_STEREO;
1251 msp->watch_stereo = 1;
1252 msp3400c_setstereo(client,V4L2_TUNER_MODE_STEREO);
1255 msp->mode = MSP_MODE_AM_NICAM;
1256 msp->rxsubchans = V4L2_TUNER_SUB_MONO;
1258 msp3400c_setstereo(client,V4L2_TUNER_MODE_MONO);
1259 msp->watch_stereo = 1;
1261 case 0x0020: /* BTSC */
1262 /* just turn on stereo */
1263 msp->mode = MSP_MODE_BTSC;
1264 msp->rxsubchans = V4L2_TUNER_SUB_STEREO;
1266 msp->watch_stereo = 1;
1267 msp3400c_setstereo(client,V4L2_TUNER_MODE_STEREO);
1269 case 0x0040: /* FM radio */
1270 msp->mode = MSP_MODE_FM_RADIO;
1271 msp->rxsubchans = V4L2_TUNER_SUB_STEREO;
1272 msp->audmode = V4L2_TUNER_MODE_STEREO;
1274 msp->watch_stereo = 0;
1275 /* not needed in theory if HAVE_RADIO(), but
1276 short programming enables carrier mute */
1277 msp3400c_setmode(client,MSP_MODE_FM_RADIO);
1278 msp3400c_setcarrier(client, MSP_CARRIER(10.7),
1281 msp3400c_set_scart(client,SCART_IN2,0);
1282 /* msp34xx does radio decoding */
1283 msp3400c_write(client,I2C_MSP3400C_DFP, 0x08, 0x0020);
1284 msp3400c_write(client,I2C_MSP3400C_DFP, 0x09, 0x0020);
1285 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0b, 0x0020);
1290 msp->mode = MSP_MODE_FM_TERRA;
1291 msp->rxsubchans = V4L2_TUNER_SUB_MONO;
1292 msp->audmode = V4L2_TUNER_MODE_MONO;
1294 msp->watch_stereo = 1;
1298 /* unmute, restore misc registers */
1299 msp3400c_setbass(client, msp->bass);
1300 msp3400c_settreble(client, msp->treble);
1301 msp3400c_setvolume(client, msp->muted, msp->left, msp->right);
1302 msp3400c_write(client, I2C_MSP3400C_DFP, 0x13, msp->acb);
1303 msp3400c_write(client,I2C_MSP3400C_DEM, 0x40, msp->i2s_mode);
1304 msp3400c_restore_dfp(client);
1306 /* monitor tv audio mode */
1307 while (msp->watch_stereo) {
1308 if (msp34xx_sleep(msp,5000))
1310 watch_stereo(client);
1313 msp3400_dbg("thread: exit\n");
1317 /* ----------------------------------------------------------------------- */
1318 /* msp34xxG + (simpler no-thread) */
1319 /* this one uses both automatic standard detection and automatic sound */
1320 /* select which are available in the newer G versions */
1321 /* struct msp: only norm, acb and source are really used in this mode */
1323 static void msp34xxg_set_source(struct i2c_client *client, int source);
1325 /* (re-)initialize the msp34xxg, according to the current norm in msp->norm
1326 * return 0 if it worked, -1 if it failed
1328 static int msp34xxg_reset(struct i2c_client *client)
1330 struct msp3400c *msp = i2c_get_clientdata(client);
1333 if (msp3400c_reset(client))
1336 /* make sure that input/output is muted (paranoid mode) */
1337 if (msp3400c_write(client,
1340 0x0f20 /* mute DSP input, mute SCART 1 */))
1343 msp3400c_write(client,I2C_MSP3400C_DEM, 0x40, msp->i2s_mode);
1345 /* step-by-step initialisation, as described in the manual */
1346 modus = msp34xx_modus(client, msp->norm);
1347 std = msp34xx_standard(msp->norm);
1348 modus &= ~0x03; /* STATUS_CHANGE=0 */
1349 modus |= 0x01; /* AUTOMATIC_SOUND_DETECTION=1 */
1350 if (msp3400c_write(client,
1355 if (msp3400c_write(client,
1361 /* write the dfps that may have an influence on
1362 standard/audio autodetection right now */
1363 msp34xxg_set_source(client, msp->source);
1365 if (msp3400c_write_dfp_with_default(client, 0x0e, /* AM/FM Prescale */
1367 /* default: [15:8] 75khz deviation */
1371 if (msp3400c_write_dfp_with_default(client, 0x10, /* NICAM Prescale */
1373 /* default: 9db gain (as recommended) */
1380 static int msp34xxg_thread(void *data)
1382 struct i2c_client *client = data;
1383 struct msp3400c *msp = i2c_get_clientdata(client);
1386 msp3400_info("msp34xxg daemon started\n");
1387 msp->source = 1; /* default */
1389 msp3400_dbg_mediumvol("msp34xxg thread: sleep\n");
1390 msp34xx_sleep(msp,-1);
1391 msp3400_dbg_mediumvol("msp34xxg thread: wakeup\n");
1394 msp3400_dbg("thread: restart scan\n");
1396 if (kthread_should_stop())
1400 msp34xxg_reset(client);
1405 /* watch autodetect */
1406 msp3400_dbg("triggered autodetect, waiting for result\n");
1407 for (i = 0; i < 10; i++) {
1408 if (msp34xx_sleep(msp,100))
1412 val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x7e);
1417 msp3400_dbg("detection still in progress\n");
1420 msp3400_dbg("detection still in progress after 10 tries. giving up.\n");
1425 msp3400_dbg("current mode: %s (0x%04x)\n",
1426 msp34xx_standard_mode_name(std), std);
1428 /* unmute: dispatch sound to scart output, set scart volume */
1429 msp3400_dbg("unmute\n");
1431 msp3400c_setbass(client, msp->bass);
1432 msp3400c_settreble(client, msp->treble);
1433 msp3400c_setvolume(client, msp->muted, msp->left, msp->right);
1436 if (msp3400c_write(client,
1442 msp3400c_write(client,I2C_MSP3400C_DEM, 0x40, msp->i2s_mode);
1444 msp3400_dbg("thread: exit\n");
1448 /* set the same 'source' for the loudspeaker, scart and quasi-peak detector
1449 * the value for source is the same as bit 15:8 of DFP registers 0x08,
1450 * 0x0a and 0x0c: 0=mono, 1=stereo or A|B, 2=SCART, 3=stereo or A, 4=stereo or B
1452 * this function replaces msp3400c_setstereo
1454 static void msp34xxg_set_source(struct i2c_client *client, int source)
1456 struct msp3400c *msp = i2c_get_clientdata(client);
1458 /* fix matrix mode to stereo and let the msp choose what
1459 * to output according to 'source', as recommended
1460 * for MONO (source==0) downmixing set bit[7:0] to 0x30
1462 int value = (source&0x07)<<8|(source==0 ? 0x30:0x20);
1463 msp3400_dbg("set source to %d (0x%x)\n", source, value);
1464 msp3400c_write(client,
1466 0x08, /* Loudspeaker Output */
1468 msp3400c_write(client,
1470 0x0a, /* SCART1 DA Output */
1472 msp3400c_write(client,
1474 0x0c, /* Quasi-peak detector */
1477 * set identification threshold. Personally, I
1478 * I set it to a higher value that the default
1479 * of 0x190 to ignore noisy stereo signals.
1480 * this needs tuning. (recommended range 0x00a0-0x03c0)
1481 * 0x7f0 = forced mono mode
1483 msp3400c_write(client,
1485 0x22, /* a2 threshold for stereo/bilingual */
1490 static void msp34xxg_detect_stereo(struct i2c_client *client)
1492 struct msp3400c *msp = i2c_get_clientdata(client);
1494 int status = msp3400c_read(client,
1496 0x0200 /* STATUS */);
1497 int is_bilingual = status&0x100;
1498 int is_stereo = status&0x40;
1500 msp->rxsubchans = 0;
1502 msp->rxsubchans |= V4L2_TUNER_SUB_STEREO;
1504 msp->rxsubchans |= V4L2_TUNER_SUB_MONO;
1506 msp->rxsubchans |= V4L2_TUNER_SUB_LANG1|V4L2_TUNER_SUB_LANG2;
1507 /* I'm supposed to check whether it's SAP or not
1508 * and set only LANG2/SAP in this case. Yet, the MSP
1509 * does a lot of work to hide this and handle everything
1510 * the same way. I don't want to work around it so unless
1511 * this is a problem, I'll handle SAP just like lang1/lang2.
1514 msp3400_dbg("status=0x%x, stereo=%d, bilingual=%d -> rxsubchans=%d\n",
1515 status, is_stereo, is_bilingual, msp->rxsubchans);
1518 static void msp34xxg_set_audmode(struct i2c_client *client, int audmode)
1520 struct msp3400c *msp = i2c_get_clientdata(client);
1524 case V4L2_TUNER_MODE_MONO:
1525 source=0; /* mono only */
1527 case V4L2_TUNER_MODE_STEREO:
1528 source=1; /* stereo or A|B, see comment in msp34xxg_get_v4l2_stereo() */
1529 /* problem: that could also mean 2 (scart input) */
1531 case V4L2_TUNER_MODE_LANG1:
1532 source=3; /* stereo or A */
1534 case V4L2_TUNER_MODE_LANG2:
1535 source=4; /* stereo or B */
1542 msp->audmode = audmode;
1543 msp34xxg_set_source(client, source);
1547 /* ----------------------------------------------------------------------- */
1549 static int msp_attach(struct i2c_adapter *adap, int addr, int kind);
1550 static int msp_detach(struct i2c_client *client);
1551 static int msp_probe(struct i2c_adapter *adap);
1552 static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg);
1554 static int msp_suspend(struct device * dev, pm_message_t state);
1555 static int msp_resume(struct device * dev);
1557 static void msp_wake_thread(struct i2c_client *client);
1559 static struct i2c_driver driver = {
1560 .owner = THIS_MODULE,
1562 .id = I2C_DRIVERID_MSP3400,
1563 .flags = I2C_DF_NOTIFY,
1564 .attach_adapter = msp_probe,
1565 .detach_client = msp_detach,
1566 .command = msp_command,
1568 .suspend = msp_suspend,
1569 .resume = msp_resume,
1573 static struct i2c_client client_template =
1576 .flags = I2C_CLIENT_ALLOW_USE,
1580 static int msp_attach(struct i2c_adapter *adap, int addr, int kind)
1582 struct msp3400c *msp;
1583 struct i2c_client *client = &client_template;
1584 int (*thread_func)(void *data) = NULL;
1587 client_template.adapter = adap;
1588 client_template.addr = addr;
1590 if (-1 == msp3400c_reset(&client_template)) {
1591 msp3400_dbg("no chip found\n");
1595 if (NULL == (client = kmalloc(sizeof(struct i2c_client),GFP_KERNEL)))
1597 memcpy(client,&client_template,sizeof(struct i2c_client));
1598 if (NULL == (msp = kmalloc(sizeof(struct msp3400c),GFP_KERNEL))) {
1603 memset(msp,0,sizeof(struct msp3400c));
1604 msp->norm = VIDEO_MODE_NTSC;
1605 msp->left = 58880; /* 0db gain */
1606 msp->right = 58880; /* 0db gain */
1608 msp->treble = 32768;
1612 for (i = 0; i < DFP_COUNT; i++)
1613 msp->dfp_regs[i] = -1;
1615 i2c_set_clientdata(client, msp);
1616 init_waitqueue_head(&msp->wq);
1618 if (-1 == msp3400c_reset(client)) {
1621 msp3400_dbg("no chip found\n");
1625 msp->rev1 = msp3400c_read(client, I2C_MSP3400C_DFP, 0x1e);
1626 if (-1 != msp->rev1)
1627 msp->rev2 = msp3400c_read(client, I2C_MSP3400C_DFP, 0x1f);
1628 if ((-1 == msp->rev1) || (0 == msp->rev1 && 0 == msp->rev2)) {
1631 msp3400_dbg("error while reading chip version\n");
1634 msp3400_dbg("rev1=0x%04x, rev2=0x%04x\n", msp->rev1, msp->rev2);
1636 msp3400c_setvolume(client, msp->muted, msp->left, msp->right);
1638 snprintf(client->name, sizeof(client->name), "MSP%c4%02d%c-%c%d",
1639 ((msp->rev1>>4)&0x0f) + '3',
1640 (msp->rev2>>8)&0xff, (msp->rev1&0x0f)+'@',
1641 ((msp->rev1>>8)&0xff)+'@', msp->rev2&0x1f);
1643 msp->opmode = opmode;
1644 if (OPMODE_AUTO == msp->opmode) {
1645 if (HAVE_SIMPLER(msp))
1646 msp->opmode = OPMODE_SIMPLER;
1647 else if (HAVE_SIMPLE(msp))
1648 msp->opmode = OPMODE_SIMPLE;
1650 msp->opmode = OPMODE_MANUAL;
1653 /* hello world :-) */
1654 msp3400_info("chip=%s", client->name);
1655 if (HAVE_NICAM(msp))
1657 if (HAVE_SIMPLE(msp))
1659 if (HAVE_SIMPLER(msp))
1660 printk(" +simpler");
1661 if (HAVE_RADIO(msp))
1664 /* version-specific initialization */
1665 switch (msp->opmode) {
1667 printk(" mode=manual");
1668 thread_func = msp3400c_thread;
1671 printk(" mode=simple");
1672 thread_func = msp3410d_thread;
1674 case OPMODE_SIMPLER:
1675 printk(" mode=simpler");
1676 thread_func = msp34xxg_thread;
1681 /* startup control thread if needed */
1683 msp->kthread = kthread_run(thread_func, client, "msp34xx");
1685 if (NULL == msp->kthread)
1686 msp3400_warn("kernel_thread() failed\n");
1687 msp_wake_thread(client);
1691 i2c_attach_client(client);
1693 /* update our own array */
1694 for (i = 0; i < MSP3400_MAX; i++) {
1695 if (NULL == msps[i]) {
1704 static int msp_detach(struct i2c_client *client)
1706 struct msp3400c *msp = i2c_get_clientdata(client);
1709 /* shutdown control thread */
1712 kthread_stop(msp->kthread);
1714 msp3400c_reset(client);
1716 /* update our own array */
1717 for (i = 0; i < MSP3400_MAX; i++) {
1718 if (client == msps[i]) {
1724 i2c_detach_client(client);
1731 static int msp_probe(struct i2c_adapter *adap)
1733 if (adap->class & I2C_CLASS_TV_ANALOG)
1734 return i2c_probe(adap, &addr_data, msp_attach);
1738 static void msp_wake_thread(struct i2c_client *client)
1740 struct msp3400c *msp = i2c_get_clientdata(client);
1742 if (NULL == msp->kthread)
1744 msp3400c_setvolume(client,msp->muted,0,0);
1745 msp->watch_stereo = 0;
1747 wake_up_interruptible(&msp->wq);
1750 /* ----------------------------------------------------------------------- */
1752 static int mode_v4l2_to_v4l1(int rxsubchans)
1756 if (rxsubchans & V4L2_TUNER_SUB_STEREO)
1757 mode |= VIDEO_SOUND_STEREO;
1758 if (rxsubchans & V4L2_TUNER_SUB_LANG2)
1759 mode |= VIDEO_SOUND_LANG2;
1760 if (rxsubchans & V4L2_TUNER_SUB_LANG1)
1761 mode |= VIDEO_SOUND_LANG1;
1763 mode |= VIDEO_SOUND_MONO;
1767 static int mode_v4l1_to_v4l2(int mode)
1769 if (mode & VIDEO_SOUND_STEREO)
1770 return V4L2_TUNER_MODE_STEREO;
1771 if (mode & VIDEO_SOUND_LANG2)
1772 return V4L2_TUNER_MODE_LANG2;
1773 if (mode & VIDEO_SOUND_LANG1)
1774 return V4L2_TUNER_MODE_LANG1;
1775 return V4L2_TUNER_MODE_MONO;
1778 static void msp_any_detect_stereo(struct i2c_client *client)
1780 struct msp3400c *msp = i2c_get_clientdata(client);
1782 switch (msp->opmode) {
1785 autodetect_stereo(client);
1787 case OPMODE_SIMPLER:
1788 msp34xxg_detect_stereo(client);
1793 static void msp_any_set_audmode(struct i2c_client *client, int audmode)
1795 struct msp3400c *msp = i2c_get_clientdata(client);
1797 switch (msp->opmode) {
1800 msp->watch_stereo = 0;
1801 msp3400c_setstereo(client, audmode);
1803 case OPMODE_SIMPLER:
1804 msp34xxg_set_audmode(client, audmode);
1810 static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg)
1812 struct msp3400c *msp = i2c_get_clientdata(client);
1818 case AUDC_SET_INPUT:
1819 msp3400_dbg("AUDC_SET_INPUT(%d)\n",*sarg);
1821 if (*sarg == msp->input)
1826 /* Hauppauge uses IN2 for the radio */
1827 msp->mode = MSP_MODE_FM_RADIO;
1830 case AUDIO_EXTERN_1:
1831 /* IN1 is often used for external input ... */
1832 msp->mode = MSP_MODE_EXTERN;
1835 case AUDIO_EXTERN_2:
1836 /* ... sometimes it is IN2 through ;) */
1837 msp->mode = MSP_MODE_EXTERN;
1844 if (*sarg & AUDIO_MUTE)
1845 msp3400c_set_scart(client,SCART_MUTE,0);
1849 msp->rxsubchans = V4L2_TUNER_SUB_STEREO;
1850 msp->audmode = V4L2_TUNER_MODE_STEREO;
1851 msp3400c_set_scart(client,scart,0);
1852 msp3400c_write(client,I2C_MSP3400C_DFP,0x000d,0x1900);
1853 if (msp->opmode != OPMODE_SIMPLER)
1854 msp3400c_setstereo(client, msp->audmode);
1856 msp_wake_thread(client);
1859 case AUDC_SET_RADIO:
1860 msp3400_dbg("AUDC_SET_RADIO\n");
1861 msp->norm = VIDEO_MODE_RADIO;
1862 msp3400_dbg("switching to radio mode\n");
1863 msp->watch_stereo = 0;
1864 switch (msp->opmode) {
1866 /* set msp3400 to FM radio mode */
1867 msp3400c_setmode(client,MSP_MODE_FM_RADIO);
1868 msp3400c_setcarrier(client, MSP_CARRIER(10.7),
1870 msp3400c_setvolume(client, msp->muted, msp->left, msp->right);
1873 case OPMODE_SIMPLER:
1874 /* the thread will do for us */
1875 msp_wake_thread(client);
1879 /* work-in-progress: hook to control the DFP registers */
1880 case MSP_SET_DFPREG:
1882 struct msp_dfpreg *r = arg;
1885 if (r->reg < 0 || r->reg >= DFP_COUNT)
1887 for (i = 0; i < sizeof(bl_dfp) / sizeof(int); i++)
1888 if (r->reg == bl_dfp[i])
1890 msp->dfp_regs[r->reg] = r->value;
1891 msp3400c_write(client, I2C_MSP3400C_DFP, r->reg, r->value);
1894 case MSP_GET_DFPREG:
1896 struct msp_dfpreg *r = arg;
1898 if (r->reg < 0 || r->reg >= DFP_COUNT)
1900 r->value = msp3400c_read(client, I2C_MSP3400C_DFP, r->reg);
1904 /* --- v4l ioctls --- */
1905 /* take care: bttv does userspace copying, we'll get a
1906 kernel pointer here... */
1909 struct video_audio *va = arg;
1911 msp3400_dbg("VIDIOCGAUDIO\n");
1912 va->flags |= VIDEO_AUDIO_VOLUME |
1914 VIDEO_AUDIO_TREBLE |
1915 VIDEO_AUDIO_MUTABLE;
1917 va->flags |= VIDEO_AUDIO_MUTE;
1920 va->flags |= VIDEO_AUDIO_MUTE;
1921 va->volume = MAX(msp->left, msp->right);
1922 va->balance = (32768 * MIN(msp->left, msp->right)) /
1923 (va->volume ? va->volume : 1);
1924 va->balance = (msp->left < msp->right) ?
1925 (65535 - va->balance) : va->balance;
1926 if (0 == va->volume)
1927 va->balance = 32768;
1928 va->bass = msp->bass;
1929 va->treble = msp->treble;
1931 msp_any_detect_stereo(client);
1932 va->mode = mode_v4l2_to_v4l1(msp->rxsubchans);
1937 struct video_audio *va = arg;
1939 msp3400_dbg("VIDIOCSAUDIO\n");
1940 msp->muted = (va->flags & VIDEO_AUDIO_MUTE);
1941 msp->left = (MIN(65536 - va->balance, 32768) *
1942 va->volume) / 32768;
1943 msp->right = (MIN(va->balance, 32768) * va->volume) / 32768;
1944 msp->bass = va->bass;
1945 msp->treble = va->treble;
1946 msp3400_dbg("VIDIOCSAUDIO setting va->volume to %d\n",
1948 msp3400_dbg("VIDIOCSAUDIO setting va->balance to %d\n",
1950 msp3400_dbg("VIDIOCSAUDIO setting va->flags to %d\n",
1952 msp3400_dbg("VIDIOCSAUDIO setting msp->left to %d\n",
1954 msp3400_dbg("VIDIOCSAUDIO setting msp->right to %d\n",
1956 msp3400_dbg("VIDIOCSAUDIO setting msp->bass to %d\n",
1958 msp3400_dbg("VIDIOCSAUDIO setting msp->treble to %d\n",
1960 msp3400_dbg("VIDIOCSAUDIO setting msp->mode to %d\n",
1962 msp3400c_setvolume(client, msp->muted, msp->left, msp->right);
1963 msp3400c_setbass(client, msp->bass);
1964 msp3400c_settreble(client, msp->treble);
1966 if (va->mode != 0 && msp->norm != VIDEO_MODE_RADIO)
1967 msp_any_set_audmode(client,mode_v4l1_to_v4l2(va->mode));
1973 struct video_channel *vc = arg;
1975 msp3400_dbg("VIDIOCSCHAN (norm=%d)\n",vc->norm);
1976 msp->norm = vc->norm;
1977 msp_wake_thread(client);
1982 case VIDIOC_S_FREQUENCY:
1984 /* new channel -- kick audio carrier scan */
1985 msp3400_dbg("VIDIOCSFREQ\n");
1986 msp_wake_thread(client);
1990 /* msp34xx specific */
1991 case MSP_SET_MATRIX:
1993 struct msp_matrix *mspm = arg;
1995 msp3400_dbg("MSP_SET_MATRIX\n");
1996 msp3400c_set_scart(client, mspm->input, mspm->output);
2000 /* --- v4l2 ioctls --- */
2003 v4l2_std_id *id = arg;
2005 /*FIXME: use V4L2 mode flags on msp3400 instead of V4L1*/
2006 if (*id & V4L2_STD_PAL) {
2007 msp->norm=VIDEO_MODE_PAL;
2008 } else if (*id & V4L2_STD_SECAM) {
2009 msp->norm=VIDEO_MODE_SECAM;
2011 msp->norm=VIDEO_MODE_NTSC;
2014 msp_wake_thread(client);
2018 case VIDIOC_ENUMINPUT:
2020 struct v4l2_input *i = arg;
2025 i->type = V4L2_INPUT_TYPE_TUNER;
2028 strcpy(i->name,"Radio");
2030 case AUDIO_EXTERN_1:
2031 strcpy(i->name,"Extern 1");
2033 case AUDIO_EXTERN_2:
2034 strcpy(i->name,"Extern 2");
2037 strcpy(i->name,"Television");
2045 case VIDIOC_G_AUDIO:
2047 struct v4l2_audio *a = arg;
2049 memset(a,0,sizeof(*a));
2053 strcpy(a->name,"Radio");
2055 case AUDIO_EXTERN_1:
2056 strcpy(a->name,"Extern 1");
2058 case AUDIO_EXTERN_2:
2059 strcpy(a->name,"Extern 2");
2062 strcpy(a->name,"Television");
2068 msp_any_detect_stereo(client);
2069 if (msp->audmode == V4L2_TUNER_MODE_STEREO) {
2070 a->capability=V4L2_AUDCAP_STEREO;
2075 case VIDIOC_S_AUDIO:
2077 struct v4l2_audio *sarg = arg;
2079 switch (sarg->index) {
2081 /* Hauppauge uses IN2 for the radio */
2082 msp->mode = MSP_MODE_FM_RADIO;
2085 case AUDIO_EXTERN_1:
2086 /* IN1 is often used for external input ... */
2087 msp->mode = MSP_MODE_EXTERN;
2090 case AUDIO_EXTERN_2:
2091 /* ... sometimes it is IN2 through ;) */
2092 msp->mode = MSP_MODE_EXTERN;
2100 msp->rxsubchans = V4L2_TUNER_SUB_STEREO;
2101 msp->audmode = V4L2_TUNER_MODE_STEREO;
2102 msp3400c_set_scart(client,scart,0);
2103 msp3400c_write(client,I2C_MSP3400C_DFP,0x000d,0x1900);
2105 if (sarg->capability==V4L2_AUDCAP_STEREO) {
2106 msp->audmode = V4L2_TUNER_MODE_STEREO;
2108 msp->audmode &= ~V4L2_TUNER_MODE_STEREO;
2110 msp_any_set_audmode(client, msp->audmode);
2111 msp_wake_thread(client);
2114 case VIDIOC_G_TUNER:
2116 struct v4l2_tuner *vt = arg;
2118 msp_any_detect_stereo(client);
2119 vt->audmode = msp->audmode;
2120 vt->rxsubchans = msp->rxsubchans;
2121 vt->capability = V4L2_TUNER_CAP_STEREO |
2122 V4L2_TUNER_CAP_LANG1|
2123 V4L2_TUNER_CAP_LANG2;
2126 case VIDIOC_S_TUNER:
2128 struct v4l2_tuner *vt=(struct v4l2_tuner *)arg;
2130 /* only set audmode */
2131 if (vt->audmode != -1 && vt->audmode != 0)
2132 msp_any_set_audmode(client, vt->audmode);
2136 case VIDIOC_G_AUDOUT:
2138 struct v4l2_audioout *a=(struct v4l2_audioout *)arg;
2141 memset(a,0,sizeof(*a));
2145 strcpy(a->name,"Scart1 Out");
2148 strcpy(a->name,"Scart2 Out");
2151 strcpy(a->name,"I2S Out");
2159 case VIDIOC_S_AUDOUT:
2161 struct v4l2_audioout *a=(struct v4l2_audioout *)arg;
2163 if (a->index<0||a->index>2)
2167 if (a->mode == V4L2_AUDMODE_32BITS)
2172 msp3400_dbg("Setting audio out on msp34xx to input %i, mode %i\n",
2173 a->index,msp->i2s_mode);
2174 msp3400c_set_scart(client,msp->in_scart,a->index+1);
2186 static int msp_suspend(struct device * dev, pm_message_t state)
2188 struct i2c_client *client = container_of(dev, struct i2c_client, dev);
2190 msp3400_dbg("msp34xx: suspend\n");
2191 msp3400c_reset(client);
2195 static int msp_resume(struct device * dev)
2197 struct i2c_client *client = container_of(dev, struct i2c_client, dev);
2199 msp3400_dbg("msp34xx: resume\n");
2200 msp_wake_thread(client);
2204 /* ----------------------------------------------------------------------- */
2206 static int __init msp3400_init_module(void)
2208 return i2c_add_driver(&driver);
2211 static void __exit msp3400_cleanup_module(void)
2213 i2c_del_driver(&driver);
2216 module_init(msp3400_init_module);
2217 module_exit(msp3400_cleanup_module);
2220 * Overrides for Emacs so that we follow Linus's tabbing style.
2221 * ---------------------------------------------------------------------------