2 * programming the msp34* sound processor family
4 * (c) 1997-2001 Gerd Knorr <kraxel@bytesex.org>
6 * what works and what doesn't:
9 * Support for Hauppauge cards added (decoding handled by tuner) added by
10 * Frederic Crozat <fcrozat@mail.dotcom.fr>
13 * should work. The stereo modes are backward compatible to FM-mono,
14 * therefore FM-Mono should be allways available.
16 * FM-Stereo (B/G, used in germany)
17 * should work, with autodetect
19 * FM-Stereo (satellite)
20 * should work, no autodetect (i.e. default is mono, but you can
21 * switch to stereo -- untested)
23 * NICAM (B/G, L , used in UK, Scandinavia, Spain and France)
24 * should work, with autodetect. Support for NICAM was added by
25 * Pekka Pietikainen <pp@netppl.fi>
29 * - better SAT support
32 * 980623 Thomas Sailer (sailer@ife.ee.ethz.ch)
33 * using soundcore instead of OSS
37 #include <linux/config.h>
38 #include <linux/module.h>
39 #include <linux/moduleparam.h>
40 #include <linux/kernel.h>
41 #include <linux/sched.h>
42 #include <linux/string.h>
43 #include <linux/timer.h>
44 #include <linux/delay.h>
45 #include <linux/errno.h>
46 #include <linux/slab.h>
47 #include <linux/i2c.h>
48 #include <linux/videodev.h>
49 #include <linux/init.h>
50 #include <linux/smp_lock.h>
51 #include <linux/kthread.h>
52 #include <linux/suspend.h>
53 #include <asm/semaphore.h>
54 #include <asm/pgtable.h>
56 #include <media/audiochip.h>
60 #define OPMODE_AUTO -1
61 #define OPMODE_MANUAL 0
62 #define OPMODE_SIMPLE 1 /* use short programming (>= msp3410 only) */
63 #define OPMODE_SIMPLER 2 /* use shorter programming (>= msp34xxG) */
65 /* insmod parameters */
66 static int opmode
= OPMODE_AUTO
;
67 static int debug
= 0; /* debug output */
68 static int once
= 0; /* no continous stereo monitoring */
69 static int amsound
= 0; /* hard-wire AM sound at 6.5 Hz (france),
70 the autoscan seems work well only with FM... */
71 static int standard
= 1; /* Override auto detect of audio standard, if needed. */
74 static int stereo_threshold
= 0x190; /* a2 threshold for stereo/bilingual
75 (msp34xxg only) 0x00a0-0x03c0 */
76 #define DFP_COUNT 0x41
77 static const int bl_dfp
[] = {
78 0x00, 0x01, 0x02, 0x03, 0x06, 0x08, 0x09, 0x0a,
79 0x0b, 0x0d, 0x0e, 0x10
82 #define IS_MSP34XX_G(msp) ((msp)->opmode==2)
94 int main
, second
; /* sound carrier */
96 int source
; /* see msp34xxg_set_source */
103 int left
, right
; /* volume */
106 /* shadow register set */
107 int dfp_regs
[DFP_COUNT
];
110 struct task_struct
*kthread
;
111 wait_queue_head_t wq
;
116 #define MIN(a,b) (((a)>(b))?(b):(a))
117 #define MAX(a,b) (((a)>(b))?(a):(b))
118 #define HAVE_NICAM(msp) (((msp->rev2>>8) & 0xff) != 00)
119 #define HAVE_SIMPLE(msp) ((msp->rev1 & 0xff) >= 'D'-'@')
120 #define HAVE_SIMPLER(msp) ((msp->rev1 & 0xff) >= 'G'-'@')
121 #define HAVE_RADIO(msp) ((msp->rev1 & 0xff) >= 'G'-'@')
123 #define VIDEO_MODE_RADIO 16 /* norm magic for radio mode */
125 /* ---------------------------------------------------------------------- */
127 #define dprintk if (debug >= 1) printk
128 #define d2printk if (debug >= 2) printk
129 #define dprintk_trace if (debug>=16) printk
132 module_param(opmode
, int, 0444);
135 module_param(once
, int, 0644);
136 module_param(debug
, int, 0644);
137 module_param(stereo_threshold
, int, 0644);
138 module_param(standard
, int, 0644);
139 module_param(amsound
, int, 0644);
140 module_param(dolby
, int, 0644);
142 MODULE_PARM_DESC(opmode
, "Forces a MSP3400 opmode. 0=Manual, 1=Simple, 2=Simpler");
143 MODULE_PARM_DESC(once
, "No continuous stereo monitoring");
144 MODULE_PARM_DESC(debug
, "Enable debug messages");
145 MODULE_PARM_DESC(stereo_threshold
, "Sets signal threshold to activate stereo");
146 MODULE_PARM_DESC(standard
, "Specify audio standard: 32 = NTSC, 64 = radio, Default: Autodetect");
147 MODULE_PARM_DESC(amsound
, "Hardwire AM sound at 6.5Hz (France), FM can autoscan");
148 MODULE_PARM_DESC(dolby
, "Activates Dolby processsing");
150 /* ---------------------------------------------------------------------- */
152 #define I2C_MSP3400C 0x80
153 #define I2C_MSP3400C_ALT 0x88
155 #define I2C_MSP3400C_DEM 0x10
156 #define I2C_MSP3400C_DFP 0x12
158 /* Addresses to scan */
159 static unsigned short normal_i2c
[] = {
161 I2C_MSP3400C_ALT
>> 1,
166 MODULE_DESCRIPTION("device driver for msp34xx TV sound processor");
167 MODULE_AUTHOR("Gerd Knorr");
168 MODULE_LICENSE("GPL");
170 /* ----------------------------------------------------------------------- */
171 /* functions for talking to the MSP3400C Sound processor */
173 static int msp3400c_reset(struct i2c_client
*client
)
175 /* reset and read revision code */
176 static char reset_off
[3] = { 0x00, 0x80, 0x00 };
177 static char reset_on
[3] = { 0x00, 0x00, 0x00 };
178 static char write
[3] = { I2C_MSP3400C_DFP
+ 1, 0x00, 0x1e };
180 struct i2c_msg reset
[2] = {
181 { client
->addr
, I2C_M_IGNORE_NAK
, 3, reset_off
},
182 { client
->addr
, I2C_M_IGNORE_NAK
, 3, reset_on
},
184 struct i2c_msg test
[2] = {
185 { client
->addr
, 0, 3, write
},
186 { client
->addr
, I2C_M_RD
, 2, read
},
189 dprintk_trace("trace: msp3400c_reset\n");
190 if ( (1 != i2c_transfer(client
->adapter
,&reset
[0],1)) ||
191 (1 != i2c_transfer(client
->adapter
,&reset
[1],1)) ||
192 (2 != i2c_transfer(client
->adapter
,test
,2)) ) {
193 printk(KERN_ERR
"msp3400: chip reset failed\n");
199 static int msp3400c_read(struct i2c_client
*client
, int dev
, int addr
)
203 unsigned char write
[3];
204 unsigned char read
[2];
205 struct i2c_msg msgs
[2] = {
206 { client
->addr
, 0, 3, write
},
207 { client
->addr
, I2C_M_RD
, 2, read
}
211 write
[1] = addr
>> 8;
212 write
[2] = addr
& 0xff;
214 for (err
= 0; err
< 3;) {
215 if (2 == i2c_transfer(client
->adapter
,msgs
,2))
219 "msp34xx: I/O error #%d (read 0x%02x/0x%02x)\n", err
,
221 current
->state
= TASK_INTERRUPTIBLE
;
222 schedule_timeout(msecs_to_jiffies(10));
226 "msp34xx: giving up, reseting chip. Sound will go off, sorry folks :-|\n");
227 msp3400c_reset(client
);
230 retval
= read
[0] << 8 | read
[1];
231 dprintk_trace("trace: msp3400c_read(0x%x, 0x%x): 0x%x\n", dev
, addr
,
236 static int msp3400c_write(struct i2c_client
*client
, int dev
, int addr
, int val
)
239 unsigned char buffer
[5];
242 buffer
[1] = addr
>> 8;
243 buffer
[2] = addr
& 0xff;
244 buffer
[3] = val
>> 8;
245 buffer
[4] = val
& 0xff;
247 dprintk_trace("trace: msp3400c_write(0x%x, 0x%x, 0x%x)\n", dev
, addr
,
249 for (err
= 0; err
< 3;) {
250 if (5 == i2c_master_send(client
, buffer
, 5))
254 "msp34xx: I/O error #%d (write 0x%02x/0x%02x)\n", err
,
256 current
->state
= TASK_INTERRUPTIBLE
;
257 schedule_timeout(msecs_to_jiffies(10));
261 "msp34xx: giving up, reseting chip. Sound will go off, sorry folks :-|\n");
262 msp3400c_reset(client
);
268 /* ------------------------------------------------------------------------ */
270 /* This macro is allowed for *constants* only, gcc must calculate it
271 at compile time. Remember -- no floats in kernel mode */
272 #define MSP_CARRIER(freq) ((int)((float)(freq/18.432)*(1<<24)))
274 #define MSP_MODE_AM_DETECT 0
275 #define MSP_MODE_FM_RADIO 2
276 #define MSP_MODE_FM_TERRA 3
277 #define MSP_MODE_FM_SAT 4
278 #define MSP_MODE_FM_NICAM1 5
279 #define MSP_MODE_FM_NICAM2 6
280 #define MSP_MODE_AM_NICAM 7
281 #define MSP_MODE_BTSC 8
282 #define MSP_MODE_EXTERN 9
284 static struct MSP_INIT_DATA_DEM
{
293 } msp_init_data
[] = {
294 { /* AM (for carrier detect / msp3400) */
295 {75, 19, 36, 35, 39, 40},
296 {75, 19, 36, 35, 39, 40},
297 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
298 0x00d0, 0x0500, 0x0020, 0x3000
299 },{ /* AM (for carrier detect / msp3410) */
300 {-1, -1, -8, 2, 59, 126},
301 {-1, -1, -8, 2, 59, 126},
302 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
303 0x00d0, 0x0100, 0x0020, 0x3000
305 {-8, -8, 4, 6, 78, 107},
306 {-8, -8, 4, 6, 78, 107},
307 MSP_CARRIER(10.7), MSP_CARRIER(10.7),
308 0x00d0, 0x0480, 0x0020, 0x3000
309 },{ /* Terrestial FM-mono + FM-stereo */
310 {3, 18, 27, 48, 66, 72},
311 {3, 18, 27, 48, 66, 72},
312 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
313 0x00d0, 0x0480, 0x0030, 0x3000
314 },{ /* Sat FM-mono */
315 { 1, 9, 14, 24, 33, 37},
316 { 3, 18, 27, 48, 66, 72},
317 MSP_CARRIER(6.5), MSP_CARRIER(6.5),
318 0x00c6, 0x0480, 0x0000, 0x3000
319 },{ /* NICAM/FM -- B/G (5.5/5.85), D/K (6.5/5.85) */
320 {-2, -8, -10, 10, 50, 86},
321 {3, 18, 27, 48, 66, 72},
322 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
323 0x00d0, 0x0040, 0x0120, 0x3000
324 },{ /* NICAM/FM -- I (6.0/6.552) */
325 {2, 4, -6, -4, 40, 94},
326 {3, 18, 27, 48, 66, 72},
327 MSP_CARRIER(6.0), MSP_CARRIER(6.0),
328 0x00d0, 0x0040, 0x0120, 0x3000
329 },{ /* NICAM/AM -- L (6.5/5.85) */
330 {-2, -8, -10, 10, 50, 86},
331 {-4, -12, -9, 23, 79, 126},
332 MSP_CARRIER(6.5), MSP_CARRIER(6.5),
333 0x00c6, 0x0140, 0x0120, 0x7c03
337 struct CARRIER_DETECT
{
342 static struct CARRIER_DETECT carrier_detect_main
[] = {
344 { MSP_CARRIER(4.5), "4.5 NTSC" },
345 { MSP_CARRIER(5.5), "5.5 PAL B/G" },
346 { MSP_CARRIER(6.0), "6.0 PAL I" },
347 { MSP_CARRIER(6.5), "6.5 PAL D/K + SAT + SECAM" }
350 static struct CARRIER_DETECT carrier_detect_55
[] = {
352 { MSP_CARRIER(5.7421875), "5.742 PAL B/G FM-stereo" },
353 { MSP_CARRIER(5.85), "5.85 PAL B/G NICAM" }
356 static struct CARRIER_DETECT carrier_detect_65
[] = {
357 /* PAL SAT / SECAM */
358 { MSP_CARRIER(5.85), "5.85 PAL D/K + SECAM NICAM" },
359 { MSP_CARRIER(6.2578125), "6.25 PAL D/K1 FM-stereo" },
360 { MSP_CARRIER(6.7421875), "6.74 PAL D/K2 FM-stereo" },
361 { MSP_CARRIER(7.02), "7.02 PAL SAT FM-stereo s/b" },
362 { MSP_CARRIER(7.20), "7.20 PAL SAT FM-stereo s" },
363 { MSP_CARRIER(7.38), "7.38 PAL SAT FM-stereo b" },
366 #define CARRIER_COUNT(x) (sizeof(x)/sizeof(struct CARRIER_DETECT))
368 /* ----------------------------------------------------------------------- */
370 static int scarts
[3][9] = {
371 /* MASK IN1 IN2 IN1_DA IN2_DA IN3 IN4 MONO MUTE */
372 { 0x0320, 0x0000, 0x0200, -1, -1, 0x0300, 0x0020, 0x0100, 0x0320 },
373 { 0x0c40, 0x0440, 0x0400, 0x0c00, 0x0040, 0x0000, 0x0840, 0x0800, 0x0c40 },
374 { 0x3080, 0x1000, 0x1080, 0x0000, 0x0080, 0x2080, 0x3080, 0x2000, 0x3000 },
377 static char *scart_names
[] = {
378 "mask", "in1", "in2", "in1 da", "in2 da", "in3", "in4", "mono", "mute"
381 static void msp3400c_set_scart(struct i2c_client
*client
, int in
, int out
)
383 struct msp3400c
*msp
= i2c_get_clientdata(client
);
385 if (-1 == scarts
[out
][in
])
388 dprintk("msp34xx: scart switch: %s => %d\n", scart_names
[in
], out
);
389 msp
->acb
&= ~scarts
[out
][SCART_MASK
];
390 msp
->acb
|= scarts
[out
][in
];
391 msp3400c_write(client
,I2C_MSP3400C_DFP
, 0x0013, msp
->acb
);
394 /* ------------------------------------------------------------------------ */
396 static void msp3400c_setcarrier(struct i2c_client
*client
, int cdo1
, int cdo2
)
398 msp3400c_write(client
,I2C_MSP3400C_DEM
, 0x0093, cdo1
& 0xfff);
399 msp3400c_write(client
,I2C_MSP3400C_DEM
, 0x009b, cdo1
>> 12);
400 msp3400c_write(client
,I2C_MSP3400C_DEM
, 0x00a3, cdo2
& 0xfff);
401 msp3400c_write(client
,I2C_MSP3400C_DEM
, 0x00ab, cdo2
>> 12);
402 msp3400c_write(client
,I2C_MSP3400C_DEM
, 0x0056, 0); /*LOAD_REG_1/2*/
405 static void msp3400c_setvolume(struct i2c_client
*client
,
406 int muted
, int left
, int right
)
408 int vol
= 0, val
= 0, balance
= 0;
411 /* 0x7f instead if 0x73 here has sound quality issues,
412 * probably due to overmodulation + clipping ... */
413 vol
= (left
> right
) ? left
: right
;
414 val
= (vol
* 0x73 / 65535) << 8;
417 balance
= ((right
- left
) * 127) / vol
;
420 dprintk("msp34xx: setvolume: mute=%s %d:%d v=0x%02x b=0x%02x\n",
421 muted
? "on" : "off", left
, right
, val
>> 8, balance
);
422 msp3400c_write(client
,I2C_MSP3400C_DFP
, 0x0000, val
); /* loudspeaker */
423 msp3400c_write(client
,I2C_MSP3400C_DFP
, 0x0006, val
); /* headphones */
424 msp3400c_write(client
,I2C_MSP3400C_DFP
, 0x0007,
425 muted
? 0x1 : (val
| 0x1));
426 msp3400c_write(client
, I2C_MSP3400C_DFP
, 0x0001, balance
<< 8);
429 static void msp3400c_setbass(struct i2c_client
*client
, int bass
)
431 int val
= ((bass
-32768) * 0x60 / 65535) << 8;
433 dprintk("msp34xx: setbass: %d 0x%02x\n", bass
, val
>> 8);
434 msp3400c_write(client
,I2C_MSP3400C_DFP
, 0x0002, val
); /* loudspeaker */
437 static void msp3400c_settreble(struct i2c_client
*client
, int treble
)
439 int val
= ((treble
-32768) * 0x60 / 65535) << 8;
441 dprintk("msp34xx: settreble: %d 0x%02x\n",treble
, val
>>8);
442 msp3400c_write(client
,I2C_MSP3400C_DFP
, 0x0003, val
); /* loudspeaker */
445 static void msp3400c_setmode(struct i2c_client
*client
, int type
)
447 struct msp3400c
*msp
= i2c_get_clientdata(client
);
450 dprintk("msp3400: setmode: %d\n",type
);
452 msp
->audmode
= V4L2_TUNER_MODE_MONO
;
453 msp
->rxsubchans
= V4L2_TUNER_SUB_MONO
;
455 msp3400c_write(client
,I2C_MSP3400C_DEM
, 0x00bb, /* ad_cv */
456 msp_init_data
[type
].ad_cv
);
458 for (i
= 5; i
>= 0; i
--) /* fir 1 */
459 msp3400c_write(client
,I2C_MSP3400C_DEM
, 0x0001,
460 msp_init_data
[type
].fir1
[i
]);
462 msp3400c_write(client
,I2C_MSP3400C_DEM
, 0x0005, 0x0004); /* fir 2 */
463 msp3400c_write(client
,I2C_MSP3400C_DEM
, 0x0005, 0x0040);
464 msp3400c_write(client
,I2C_MSP3400C_DEM
, 0x0005, 0x0000);
465 for (i
= 5; i
>= 0; i
--)
466 msp3400c_write(client
,I2C_MSP3400C_DEM
, 0x0005,
467 msp_init_data
[type
].fir2
[i
]);
469 msp3400c_write(client
,I2C_MSP3400C_DEM
, 0x0083, /* MODE_REG */
470 msp_init_data
[type
].mode_reg
);
472 msp3400c_setcarrier(client
, msp_init_data
[type
].cdo1
,
473 msp_init_data
[type
].cdo2
);
475 msp3400c_write(client
,I2C_MSP3400C_DEM
, 0x0056, 0); /*LOAD_REG_1/2*/
478 msp3400c_write(client
,I2C_MSP3400C_DFP
, 0x0008,
480 msp3400c_write(client
,I2C_MSP3400C_DFP
, 0x0009,
482 msp3400c_write(client
,I2C_MSP3400C_DFP
, 0x000b,
483 msp_init_data
[type
].dfp_src
);
485 msp3400c_write(client
,I2C_MSP3400C_DFP
, 0x0008,
486 msp_init_data
[type
].dfp_src
);
487 msp3400c_write(client
,I2C_MSP3400C_DFP
, 0x0009,
488 msp_init_data
[type
].dfp_src
);
489 msp3400c_write(client
,I2C_MSP3400C_DFP
, 0x000b,
490 msp_init_data
[type
].dfp_src
);
492 msp3400c_write(client
,I2C_MSP3400C_DFP
, 0x000a,
493 msp_init_data
[type
].dfp_src
);
494 msp3400c_write(client
,I2C_MSP3400C_DFP
, 0x000e,
495 msp_init_data
[type
].dfp_matrix
);
497 if (HAVE_NICAM(msp
)) {
499 msp3400c_write(client
,I2C_MSP3400C_DFP
, 0x0010, 0x5a00); /* was: 0x3000 */
503 /* given a bitmask of VIDEO_SOUND_XXX returns the "best" in the bitmask */
504 static int best_video_sound(int rxsubchans
)
506 if (rxsubchans
& V4L2_TUNER_SUB_STEREO
)
507 return V4L2_TUNER_MODE_STEREO
;
508 if (rxsubchans
& V4L2_TUNER_SUB_LANG1
)
509 return V4L2_TUNER_MODE_LANG1
;
510 if (rxsubchans
& V4L2_TUNER_SUB_LANG2
)
511 return V4L2_TUNER_MODE_LANG2
;
512 return V4L2_TUNER_MODE_MONO
;
515 /* turn on/off nicam + stereo */
516 static void msp3400c_setstereo(struct i2c_client
*client
, int mode
)
518 static char *strmode
[] = { "0", "mono", "stereo", "3",
519 "lang1", "5", "6", "7", "lang2"
521 struct msp3400c
*msp
= i2c_get_clientdata(client
);
522 int nicam
= 0; /* channel source: FM/AM or nicam */
525 if (IS_MSP34XX_G(msp
)) {
526 /* this method would break everything, let's make sure
530 ("msp34xxg: DEBUG WARNING setstereo called with mode=%d instead of set_source (ignored)\n",
535 /* switch demodulator */
537 case MSP_MODE_FM_TERRA
:
538 dprintk("msp3400: FM setstereo: %s\n",
540 msp3400c_setcarrier(client
,msp
->second
,msp
->main
);
542 case V4L2_TUNER_MODE_STEREO
:
543 msp3400c_write(client
,I2C_MSP3400C_DFP
, 0x000e, 0x3001);
545 case V4L2_TUNER_MODE_MONO
:
546 case V4L2_TUNER_MODE_LANG1
:
547 case V4L2_TUNER_MODE_LANG2
:
548 msp3400c_write(client
,I2C_MSP3400C_DFP
, 0x000e, 0x3000);
552 case MSP_MODE_FM_SAT
:
553 dprintk("msp3400: SAT setstereo: %s\n", strmode
[mode
]);
555 case V4L2_TUNER_MODE_MONO
:
556 msp3400c_setcarrier(client
, MSP_CARRIER(6.5), MSP_CARRIER(6.5));
558 case V4L2_TUNER_MODE_STEREO
:
559 msp3400c_setcarrier(client
, MSP_CARRIER(7.2), MSP_CARRIER(7.02));
561 case V4L2_TUNER_MODE_LANG1
:
562 msp3400c_setcarrier(client
, MSP_CARRIER(7.38), MSP_CARRIER(7.02));
564 case V4L2_TUNER_MODE_LANG2
:
565 msp3400c_setcarrier(client
, MSP_CARRIER(7.38), MSP_CARRIER(7.02));
569 case MSP_MODE_FM_NICAM1
:
570 case MSP_MODE_FM_NICAM2
:
571 case MSP_MODE_AM_NICAM
:
572 dprintk("msp3400: NICAM setstereo: %s\n",strmode
[mode
]);
573 msp3400c_setcarrier(client
,msp
->second
,msp
->main
);
578 dprintk("msp3400: BTSC setstereo: %s\n",strmode
[mode
]);
581 case MSP_MODE_EXTERN
:
582 dprintk("msp3400: extern setstereo: %s\n",strmode
[mode
]);
585 case MSP_MODE_FM_RADIO
:
586 dprintk("msp3400: FM-Radio setstereo: %s\n",strmode
[mode
]);
589 dprintk("msp3400: mono setstereo\n");
594 switch (best_video_sound(mode
)) {
595 case V4L2_TUNER_MODE_STEREO
:
596 src
= 0x0020 | nicam
;
598 case V4L2_TUNER_MODE_MONO
:
599 if (msp
->mode
== MSP_MODE_AM_NICAM
) {
600 dprintk("msp3400: switching to AM mono\n");
601 /* AM mono decoding is handled by tuner, not MSP chip */
602 /* SCART switching control register */
603 msp3400c_set_scart(client
,SCART_MONO
,0);
607 case V4L2_TUNER_MODE_LANG1
:
608 src
= 0x0000 | nicam
;
610 case V4L2_TUNER_MODE_LANG2
:
611 src
= 0x0010 | nicam
;
614 dprintk("msp3400: setstereo final source/matrix = 0x%x\n", src
);
617 msp3400c_write(client
,I2C_MSP3400C_DFP
, 0x0008,0x0520);
618 msp3400c_write(client
,I2C_MSP3400C_DFP
, 0x0009,0x0620);
619 msp3400c_write(client
,I2C_MSP3400C_DFP
, 0x000a,src
);
620 msp3400c_write(client
,I2C_MSP3400C_DFP
, 0x000b,src
);
622 msp3400c_write(client
,I2C_MSP3400C_DFP
, 0x0008,src
);
623 msp3400c_write(client
,I2C_MSP3400C_DFP
, 0x0009,src
);
624 msp3400c_write(client
,I2C_MSP3400C_DFP
, 0x000a,src
);
625 msp3400c_write(client
,I2C_MSP3400C_DFP
, 0x000b,src
);
630 msp3400c_print_mode(struct msp3400c
*msp
)
632 if (msp
->main
== msp
->second
) {
633 dprintk("msp3400: mono sound carrier: %d.%03d MHz\n",
634 msp
->main
/910000,(msp
->main
/910)%1000);
636 dprintk("msp3400: main sound carrier: %d.%03d MHz\n",
637 msp
->main
/910000,(msp
->main
/910)%1000);
639 if (msp
->mode
== MSP_MODE_FM_NICAM1
|| msp
->mode
== MSP_MODE_FM_NICAM2
)
640 dprintk("msp3400: NICAM/FM carrier : %d.%03d MHz\n",
641 msp
->second
/910000,(msp
->second
/910)%1000);
642 if (msp
->mode
== MSP_MODE_AM_NICAM
)
643 dprintk("msp3400: NICAM/AM carrier : %d.%03d MHz\n",
644 msp
->second
/910000,(msp
->second
/910)%1000);
645 if (msp
->mode
== MSP_MODE_FM_TERRA
&&
646 msp
->main
!= msp
->second
) {
647 dprintk("msp3400: FM-stereo carrier : %d.%03d MHz\n",
648 msp
->second
/910000,(msp
->second
/910)%1000);
652 #define MSP3400_MAX 4
653 static struct i2c_client
*msps
[MSP3400_MAX
];
654 static void msp3400c_restore_dfp(struct i2c_client
*client
)
656 struct msp3400c
*msp
= i2c_get_clientdata(client
);
659 for (i
= 0; i
< DFP_COUNT
; i
++) {
660 if (-1 == msp
->dfp_regs
[i
])
662 msp3400c_write(client
, I2C_MSP3400C_DFP
, i
, msp
->dfp_regs
[i
]);
666 /* if the dfp_regs is set, set what's in there. Otherwise, set the default value */
667 static int msp3400c_write_dfp_with_default(struct i2c_client
*client
,
668 int addr
, int default_value
)
670 struct msp3400c
*msp
= i2c_get_clientdata(client
);
671 int value
= default_value
;
672 if (addr
< DFP_COUNT
&& -1 != msp
->dfp_regs
[addr
])
673 value
= msp
->dfp_regs
[addr
];
674 return msp3400c_write(client
, I2C_MSP3400C_DFP
, addr
, value
);
677 /* ----------------------------------------------------------------------- */
679 struct REGISTER_DUMP
{
684 struct REGISTER_DUMP d1
[] = {
685 {0x007e, "autodetect"},
686 {0x0023, "C_AD_BITS "},
687 {0x0038, "ADD_BITS "},
688 {0x003e, "CIB_BITS "},
689 {0x0057, "ERROR_RATE"},
692 static int autodetect_stereo(struct i2c_client
*client
)
694 struct msp3400c
*msp
= i2c_get_clientdata(client
);
696 int rxsubchans
= msp
->rxsubchans
;
697 int newnicam
= msp
->nicam_on
;
701 case MSP_MODE_FM_TERRA
:
702 val
= msp3400c_read(client
, I2C_MSP3400C_DFP
, 0x18);
705 dprintk("msp34xx: stereo detect register: %d\n",val
);
707 rxsubchans
= V4L2_TUNER_SUB_STEREO
| V4L2_TUNER_SUB_MONO
;
708 } else if (val
< -4096) {
709 rxsubchans
= V4L2_TUNER_SUB_LANG1
| V4L2_TUNER_SUB_LANG2
;
711 rxsubchans
= V4L2_TUNER_SUB_MONO
;
715 case MSP_MODE_FM_NICAM1
:
716 case MSP_MODE_FM_NICAM2
:
717 case MSP_MODE_AM_NICAM
:
718 val
= msp3400c_read(client
, I2C_MSP3400C_DEM
, 0x23);
719 dprintk("msp34xx: nicam sync=%d, mode=%d\n",
720 val
& 1, (val
& 0x1e) >> 1);
724 switch ((val
& 0x1e) >> 1) {
727 rxsubchans
= V4L2_TUNER_SUB_STEREO
;
731 rxsubchans
= V4L2_TUNER_SUB_MONO
732 | V4L2_TUNER_SUB_LANG1
;
736 rxsubchans
= V4L2_TUNER_SUB_MONO
737 | V4L2_TUNER_SUB_LANG1
738 | V4L2_TUNER_SUB_LANG2
;
741 rxsubchans
= V4L2_TUNER_SUB_MONO
;
747 rxsubchans
= V4L2_TUNER_SUB_MONO
;
751 val
= msp3400c_read(client
, I2C_MSP3400C_DEM
, 0x200);
752 dprintk("msp3410: status=0x%x (pri=%s, sec=%s, %s%s%s)\n",
754 (val
& 0x0002) ? "no" : "yes",
755 (val
& 0x0004) ? "no" : "yes",
756 (val
& 0x0040) ? "stereo" : "mono",
757 (val
& 0x0080) ? ", nicam 2nd mono" : "",
758 (val
& 0x0100) ? ", bilingual/SAP" : "");
759 rxsubchans
= V4L2_TUNER_SUB_MONO
;
760 if (val
& 0x0040) rxsubchans
|= V4L2_TUNER_SUB_STEREO
;
761 if (val
& 0x0100) rxsubchans
|= V4L2_TUNER_SUB_LANG1
;
764 if (rxsubchans
!= msp
->rxsubchans
) {
766 dprintk("msp34xx: watch: rxsubchans %d => %d\n",
767 msp
->rxsubchans
,rxsubchans
);
768 msp
->rxsubchans
= rxsubchans
;
770 if (newnicam
!= msp
->nicam_on
) {
772 dprintk("msp34xx: watch: nicam %d => %d\n",
773 msp
->nicam_on
,newnicam
);
774 msp
->nicam_on
= newnicam
;
780 * A kernel thread for msp3400 control -- we don't want to block the
781 * in the ioctl while doing the sound carrier & stereo detect
784 static int msp34xx_sleep(struct msp3400c
*msp
, int timeout
)
786 DECLARE_WAITQUEUE(wait
, current
);
788 add_wait_queue(&msp
->wq
, &wait
);
789 if (!kthread_should_stop()) {
791 set_current_state(TASK_INTERRUPTIBLE
);
794 schedule_timeout_interruptible
795 (msecs_to_jiffies(timeout
));
799 remove_wait_queue(&msp
->wq
, &wait
);
804 /* stereo/multilang monitoring */
805 static void watch_stereo(struct i2c_client
*client
)
807 struct msp3400c
*msp
= i2c_get_clientdata(client
);
809 if (autodetect_stereo(client
)) {
810 if (msp
->stereo
& V4L2_TUNER_MODE_STEREO
)
811 msp3400c_setstereo(client
, V4L2_TUNER_MODE_STEREO
);
812 else if (msp
->stereo
& VIDEO_SOUND_LANG1
)
813 msp3400c_setstereo(client
, V4L2_TUNER_MODE_LANG1
);
815 msp3400c_setstereo(client
, V4L2_TUNER_MODE_MONO
);
819 msp
->watch_stereo
= 0;
822 static int msp3400c_thread(void *data
)
824 struct i2c_client
*client
= data
;
825 struct msp3400c
*msp
= i2c_get_clientdata(client
);
826 struct CARRIER_DETECT
*cd
;
827 int count
, max1
,max2
,val1
,val2
, val
,this;
829 printk("msp3400: kthread started\n");
831 d2printk("msp3400: thread: sleep\n");
832 msp34xx_sleep(msp
,-1);
833 d2printk("msp3400: thread: wakeup\n");
836 dprintk("msp3410: thread: restart scan\n");
838 if (kthread_should_stop())
841 if (VIDEO_MODE_RADIO
== msp
->norm
||
842 MSP_MODE_EXTERN
== msp
->mode
) {
843 /* no carrier scan, just unmute */
844 printk("msp3400: thread: no carrier scan\n");
845 msp3400c_setvolume(client
, msp
->muted
, msp
->left
, msp
->right
);
850 msp3400c_setvolume(client
, msp
->muted
, 0, 0);
851 msp3400c_setmode(client
, MSP_MODE_AM_DETECT
/* +1 */ );
854 msp
->watch_stereo
= 0;
856 /* some time for the tuner to sync */
857 if (msp34xx_sleep(msp
,200))
860 /* carrier detect pass #1 -- main carrier */
861 cd
= carrier_detect_main
;
862 count
= CARRIER_COUNT(carrier_detect_main
);
864 if (amsound
&& (msp
->norm
== VIDEO_MODE_SECAM
)) {
865 /* autodetect doesn't work well with AM ... */
868 dprintk("msp3400: AM sound override\n");
871 for (this = 0; this < count
; this++) {
872 msp3400c_setcarrier(client
, cd
[this].cdo
,cd
[this].cdo
);
873 if (msp34xx_sleep(msp
,100))
875 val
= msp3400c_read(client
, I2C_MSP3400C_DFP
, 0x1b);
879 val1
= val
, max1
= this;
880 dprintk("msp3400: carrier1 val: %5d / %s\n", val
,cd
[this].name
);
883 /* carrier detect pass #2 -- second (stereo) carrier */
886 cd
= carrier_detect_55
;
887 count
= CARRIER_COUNT(carrier_detect_55
);
890 cd
= carrier_detect_65
;
891 count
= CARRIER_COUNT(carrier_detect_65
);
901 if (amsound
&& (msp
->norm
== VIDEO_MODE_SECAM
)) {
902 /* autodetect doesn't work well with AM ... */
907 for (this = 0; this < count
; this++) {
908 msp3400c_setcarrier(client
, cd
[this].cdo
,cd
[this].cdo
);
909 if (msp34xx_sleep(msp
,100))
911 val
= msp3400c_read(client
, I2C_MSP3400C_DFP
, 0x1b);
915 val2
= val
, max2
= this;
916 dprintk("msp3400: carrier2 val: %5d / %s\n", val
,cd
[this].name
);
919 /* programm the msp3400 according to the results */
920 msp
->main
= carrier_detect_main
[max1
].cdo
;
925 msp
->second
= carrier_detect_55
[max2
].cdo
;
926 msp3400c_setmode(client
, MSP_MODE_FM_TERRA
);
928 msp3400c_setstereo(client
, V4L2_TUNER_MODE_MONO
);
929 msp
->watch_stereo
= 1;
930 } else if (max2
== 1 && HAVE_NICAM(msp
)) {
932 msp
->second
= carrier_detect_55
[max2
].cdo
;
933 msp3400c_setmode(client
, MSP_MODE_FM_NICAM1
);
935 msp3400c_setcarrier(client
, msp
->second
, msp
->main
);
936 msp
->watch_stereo
= 1;
943 msp
->second
= MSP_CARRIER(6.552);
944 msp3400c_setmode(client
, MSP_MODE_FM_NICAM2
);
946 msp3400c_setcarrier(client
, msp
->second
, msp
->main
);
947 msp
->watch_stereo
= 1;
950 if (max2
== 1 || max2
== 2) {
952 msp
->second
= carrier_detect_65
[max2
].cdo
;
953 msp3400c_setmode(client
, MSP_MODE_FM_TERRA
);
955 msp3400c_setstereo(client
, V4L2_TUNER_MODE_MONO
);
956 msp
->watch_stereo
= 1;
957 } else if (max2
== 0 &&
958 msp
->norm
== VIDEO_MODE_SECAM
) {
959 /* L NICAM or AM-mono */
960 msp
->second
= carrier_detect_65
[max2
].cdo
;
961 msp3400c_setmode(client
, MSP_MODE_AM_NICAM
);
963 msp3400c_setstereo(client
, V4L2_TUNER_MODE_MONO
);
964 msp3400c_setcarrier(client
, msp
->second
, msp
->main
);
965 /* volume prescale for SCART (AM mono input) */
966 msp3400c_write(client
,I2C_MSP3400C_DFP
, 0x000d, 0x1900);
967 msp
->watch_stereo
= 1;
968 } else if (max2
== 0 && HAVE_NICAM(msp
)) {
970 msp
->second
= carrier_detect_65
[max2
].cdo
;
971 msp3400c_setmode(client
, MSP_MODE_FM_NICAM1
);
973 msp3400c_setcarrier(client
, msp
->second
, msp
->main
);
974 msp
->watch_stereo
= 1;
982 msp
->second
= carrier_detect_main
[max1
].cdo
;
983 msp3400c_setmode(client
, MSP_MODE_FM_TERRA
);
985 msp3400c_setcarrier(client
, msp
->second
, msp
->main
);
986 msp
->rxsubchans
= V4L2_TUNER_SUB_MONO
;
987 msp3400c_setstereo(client
, V4L2_TUNER_MODE_MONO
);
992 msp3400c_setvolume(client
, msp
->muted
, msp
->left
, msp
->right
);
993 msp3400c_restore_dfp(client
);
996 msp3400c_print_mode(msp
);
998 /* monitor tv audio mode */
999 while (msp
->watch_stereo
) {
1000 if (msp34xx_sleep(msp
,5000))
1002 watch_stereo(client
);
1005 dprintk("msp3400: thread: exit\n");
1009 /* ----------------------------------------------------------------------- */
1010 /* this one uses the automatic sound standard detection of newer */
1011 /* msp34xx chip versions */
1013 static struct MODES
{
1018 { 0x0000, 0, 0, "ERROR" },
1019 { 0x0001, 0, 0, "autodetect start" },
1020 { 0x0002, MSP_CARRIER(4.5), MSP_CARRIER(4.72), "4.5/4.72 M Dual FM-Stereo" },
1021 { 0x0003, MSP_CARRIER(5.5), MSP_CARRIER(5.7421875), "5.5/5.74 B/G Dual FM-Stereo" },
1022 { 0x0004, MSP_CARRIER(6.5), MSP_CARRIER(6.2578125), "6.5/6.25 D/K1 Dual FM-Stereo" },
1023 { 0x0005, MSP_CARRIER(6.5), MSP_CARRIER(6.7421875), "6.5/6.74 D/K2 Dual FM-Stereo" },
1024 { 0x0006, MSP_CARRIER(6.5), MSP_CARRIER(6.5), "6.5 D/K FM-Mono (HDEV3)" },
1025 { 0x0008, MSP_CARRIER(5.5), MSP_CARRIER(5.85), "5.5/5.85 B/G NICAM FM" },
1026 { 0x0009, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85 L NICAM AM" },
1027 { 0x000a, MSP_CARRIER(6.0), MSP_CARRIER(6.55), "6.0/6.55 I NICAM FM" },
1028 { 0x000b, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85 D/K NICAM FM" },
1029 { 0x000c, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85 D/K NICAM FM (HDEV2)" },
1030 { 0x0020, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5 M BTSC-Stereo" },
1031 { 0x0021, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5 M BTSC-Mono + SAP" },
1032 { 0x0030, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5 M EIA-J Japan Stereo" },
1033 { 0x0040, MSP_CARRIER(10.7), MSP_CARRIER(10.7), "10.7 FM-Stereo Radio" },
1034 { 0x0050, MSP_CARRIER(6.5), MSP_CARRIER(6.5), "6.5 SAT-Mono" },
1035 { 0x0051, MSP_CARRIER(7.02), MSP_CARRIER(7.20), "7.02/7.20 SAT-Stereo" },
1036 { 0x0060, MSP_CARRIER(7.2), MSP_CARRIER(7.2), "7.2 SAT ADR" },
1037 { -1, 0, 0, NULL
}, /* EOF */
1040 static inline const char *msp34xx_standard_mode_name(int mode
)
1043 for (i
= 0; modelist
[i
].name
!= NULL
; i
++)
1044 if (modelist
[i
].retval
== mode
)
1045 return modelist
[i
].name
;
1049 static int msp34xx_modus(int norm
)
1052 case VIDEO_MODE_PAL
:
1053 dprintk("msp34xx: video mode selected to PAL\n");
1056 /* experimental: not sure this works with all chip versions */
1059 /* previous value, try this if it breaks ... */
1062 case VIDEO_MODE_NTSC
: /* BTSC */
1063 dprintk("msp34xx: video mode selected to NTSC\n");
1065 case VIDEO_MODE_SECAM
:
1066 dprintk("msp34xx: video mode selected to SECAM\n");
1068 case VIDEO_MODE_RADIO
:
1069 dprintk("msp34xx: video mode selected to Radio\n");
1071 case VIDEO_MODE_AUTO
:
1072 dprintk("msp34xx: video mode selected to Auto\n");
1079 static int msp34xx_standard(int norm
)
1082 case VIDEO_MODE_PAL
:
1084 case VIDEO_MODE_NTSC
: /* BTSC */
1086 case VIDEO_MODE_SECAM
:
1088 case VIDEO_MODE_RADIO
:
1095 static int msp3410d_thread(void *data
)
1097 struct i2c_client
*client
= data
;
1098 struct msp3400c
*msp
= i2c_get_clientdata(client
);
1101 printk("msp3410: daemon started\n");
1103 d2printk("msp3410: thread: sleep\n");
1104 msp34xx_sleep(msp
,-1);
1105 d2printk("msp3410: thread: wakeup\n");
1108 dprintk("msp3410: thread: restart scan\n");
1110 if (kthread_should_stop())
1113 if (msp
->mode
== MSP_MODE_EXTERN
) {
1114 /* no carrier scan needed, just unmute */
1115 dprintk("msp3410: thread: no carrier scan\n");
1116 msp3400c_setvolume(client
, msp
->muted
, msp
->left
, msp
->right
);
1120 /* put into sane state (and mute) */
1121 msp3400c_reset(client
);
1123 /* some time for the tuner to sync */
1124 if (msp34xx_sleep(msp
,200))
1127 /* start autodetect */
1128 mode
= msp34xx_modus(msp
->norm
);
1129 std
= msp34xx_standard(msp
->norm
);
1130 msp3400c_write(client
, I2C_MSP3400C_DEM
, 0x30, mode
);
1131 msp3400c_write(client
, I2C_MSP3400C_DEM
, 0x20, std
);
1132 msp
->watch_stereo
= 0;
1135 dprintk("msp3410: setting mode: %s (0x%04x)\n",
1136 msp34xx_standard_mode_name(std
) ,std
);
1139 /* programmed some specific mode */
1142 /* triggered autodetect */
1144 if (msp34xx_sleep(msp
,100))
1148 val
= msp3400c_read(client
, I2C_MSP3400C_DEM
, 0x7e);
1151 dprintk("msp3410: detection still in progress\n");
1154 for (i
= 0; modelist
[i
].name
!= NULL
; i
++)
1155 if (modelist
[i
].retval
== val
)
1157 dprintk("msp3410: current mode: %s (0x%04x)\n",
1158 modelist
[i
].name
? modelist
[i
].name
: "unknown",
1160 msp
->main
= modelist
[i
].main
;
1161 msp
->second
= modelist
[i
].second
;
1163 if (amsound
&& (msp
->norm
== VIDEO_MODE_SECAM
) && (val
!= 0x0009)) {
1164 /* autodetection has failed, let backup */
1165 dprintk("msp3410: autodetection failed,"
1166 " switching to backup mode: %s (0x%04x)\n",
1167 modelist
[8].name
? modelist
[8].name
: "unknown",val
);
1169 msp3400c_write(client
, I2C_MSP3400C_DEM
, 0x20, val
);
1172 /* set various prescales */
1173 msp3400c_write(client
, I2C_MSP3400C_DFP
, 0x0d, 0x1900); /* scart */
1174 msp3400c_write(client
, I2C_MSP3400C_DFP
, 0x0e, 0x2403); /* FM */
1175 msp3400c_write(client
, I2C_MSP3400C_DFP
, 0x10, 0x5a00); /* nicam */
1179 case 0x0008: /* B/G NICAM */
1180 case 0x000a: /* I NICAM */
1182 msp
->mode
= MSP_MODE_FM_NICAM1
;
1184 msp
->mode
= MSP_MODE_FM_NICAM2
;
1185 /* just turn on stereo */
1186 msp
->rxsubchans
= V4L2_TUNER_SUB_STEREO
;
1188 msp
->watch_stereo
= 1;
1189 msp3400c_setstereo(client
,V4L2_TUNER_MODE_STEREO
);
1192 msp
->mode
= MSP_MODE_AM_NICAM
;
1193 msp
->rxsubchans
= V4L2_TUNER_SUB_MONO
;
1195 msp3400c_setstereo(client
,V4L2_TUNER_MODE_MONO
);
1196 msp
->watch_stereo
= 1;
1198 case 0x0020: /* BTSC */
1199 /* just turn on stereo */
1200 msp
->mode
= MSP_MODE_BTSC
;
1201 msp
->rxsubchans
= V4L2_TUNER_SUB_STEREO
;
1203 msp
->watch_stereo
= 1;
1204 msp3400c_setstereo(client
,V4L2_TUNER_MODE_STEREO
);
1206 case 0x0040: /* FM radio */
1207 msp
->mode
= MSP_MODE_FM_RADIO
;
1208 msp
->rxsubchans
= V4L2_TUNER_SUB_STEREO
;
1209 msp
->audmode
= V4L2_TUNER_MODE_STEREO
;
1211 msp
->watch_stereo
= 0;
1212 /* not needed in theory if HAVE_RADIO(), but
1213 short programming enables carrier mute */
1214 msp3400c_setmode(client
,MSP_MODE_FM_RADIO
);
1215 msp3400c_setcarrier(client
, MSP_CARRIER(10.7),
1218 msp3400c_set_scart(client
,SCART_IN2
,0);
1219 /* msp34xx does radio decoding */
1220 msp3400c_write(client
,I2C_MSP3400C_DFP
, 0x08, 0x0020);
1221 msp3400c_write(client
,I2C_MSP3400C_DFP
, 0x09, 0x0020);
1222 msp3400c_write(client
,I2C_MSP3400C_DFP
, 0x0b, 0x0020);
1227 msp
->mode
= MSP_MODE_FM_TERRA
;
1228 msp
->rxsubchans
= V4L2_TUNER_SUB_MONO
;
1229 msp
->audmode
= V4L2_TUNER_MODE_MONO
;
1231 msp
->watch_stereo
= 1;
1235 /* unmute, restore misc registers */
1236 msp3400c_setbass(client
, msp
->bass
);
1237 msp3400c_settreble(client
, msp
->treble
);
1238 msp3400c_setvolume(client
, msp
->muted
, msp
->left
, msp
->right
);
1239 msp3400c_write(client
, I2C_MSP3400C_DFP
, 0x0013, msp
->acb
);
1240 msp3400c_restore_dfp(client
);
1242 /* monitor tv audio mode */
1243 while (msp
->watch_stereo
) {
1244 if (msp34xx_sleep(msp
,5000))
1246 watch_stereo(client
);
1249 dprintk("msp3410: thread: exit\n");
1253 /* ----------------------------------------------------------------------- */
1254 /* msp34xxG + (simpler no-thread) */
1255 /* this one uses both automatic standard detection and automatic sound */
1256 /* select which are available in the newer G versions */
1257 /* struct msp: only norm, acb and source are really used in this mode */
1259 static void msp34xxg_set_source(struct i2c_client
*client
, int source
);
1261 /* (re-)initialize the msp34xxg, according to the current norm in msp->norm
1262 * return 0 if it worked, -1 if it failed
1264 static int msp34xxg_reset(struct i2c_client
*client
)
1266 struct msp3400c
*msp
= i2c_get_clientdata(client
);
1269 if (msp3400c_reset(client
))
1272 /* make sure that input/output is muted (paranoid mode) */
1273 if (msp3400c_write(client
,
1276 0x0f20 /* mute DSP input, mute SCART 1 */))
1279 /* step-by-step initialisation, as described in the manual */
1280 modus
= msp34xx_modus(msp
->norm
);
1281 std
= msp34xx_standard(msp
->norm
);
1282 modus
&= ~0x03; /* STATUS_CHANGE=0 */
1283 modus
|= 0x01; /* AUTOMATIC_SOUND_DETECTION=1 */
1284 if (msp3400c_write(client
,
1289 if (msp3400c_write(client
,
1295 /* write the dfps that may have an influence on
1296 standard/audio autodetection right now */
1297 msp34xxg_set_source(client
, msp
->source
);
1299 if (msp3400c_write_dfp_with_default(client
, 0x0e, /* AM/FM Prescale */
1301 /* default: [15:8] 75khz deviation */
1305 if (msp3400c_write_dfp_with_default(client
, 0x10, /* NICAM Prescale */
1307 /* default: 9db gain (as recommended) */
1314 static int msp34xxg_thread(void *data
)
1316 struct i2c_client
*client
= data
;
1317 struct msp3400c
*msp
= i2c_get_clientdata(client
);
1320 printk("msp34xxg: daemon started\n");
1321 msp
->source
= 1; /* default */
1323 d2printk("msp34xxg: thread: sleep\n");
1324 msp34xx_sleep(msp
,-1);
1325 d2printk("msp34xxg: thread: wakeup\n");
1328 dprintk("msp34xxg: thread: restart scan\n");
1330 if (kthread_should_stop())
1334 msp34xxg_reset(client
);
1339 /* watch autodetect */
1340 dprintk("msp34xxg: triggered autodetect, waiting for result\n");
1341 for (i
= 0; i
< 10; i
++) {
1342 if (msp34xx_sleep(msp
,100))
1346 val
= msp3400c_read(client
, I2C_MSP3400C_DEM
, 0x7e);
1351 dprintk("msp34xxg: detection still in progress\n");
1354 dprintk("msp34xxg: detection still in progress after 10 tries. giving up.\n");
1359 dprintk("msp34xxg: current mode: %s (0x%04x)\n",
1360 msp34xx_standard_mode_name(std
), std
);
1362 /* unmute: dispatch sound to scart output, set scart volume */
1363 dprintk("msp34xxg: unmute\n");
1365 msp3400c_setbass(client
, msp
->bass
);
1366 msp3400c_settreble(client
, msp
->treble
);
1367 msp3400c_setvolume(client
, msp
->muted
, msp
->left
, msp
->right
);
1370 if (msp3400c_write(client
,
1376 dprintk("msp34xxg: thread: exit\n");
1380 /* set the same 'source' for the loudspeaker, scart and quasi-peak detector
1381 * the value for source is the same as bit 15:8 of DFP registers 0x08,
1382 * 0x0a and 0x0c: 0=mono, 1=stereo or A|B, 2=SCART, 3=stereo or A, 4=stereo or B
1384 * this function replaces msp3400c_setstereo
1386 static void msp34xxg_set_source(struct i2c_client
*client
, int source
)
1388 struct msp3400c
*msp
= i2c_get_clientdata(client
);
1390 /* fix matrix mode to stereo and let the msp choose what
1391 * to output according to 'source', as recommended
1392 * for MONO (source==0) downmixing set bit[7:0] to 0x30
1394 int value
= (source
&0x07)<<8|(source
==0 ? 0x30:0x20);
1395 dprintk("msp34xxg: set source to %d (0x%x)\n", source
, value
);
1396 msp3400c_write(client
,
1398 0x08, /* Loudspeaker Output */
1400 msp3400c_write(client
,
1402 0x0a, /* SCART1 DA Output */
1404 msp3400c_write(client
,
1406 0x0c, /* Quasi-peak detector */
1409 * set identification threshold. Personally, I
1410 * I set it to a higher value that the default
1411 * of 0x190 to ignore noisy stereo signals.
1412 * this needs tuning. (recommended range 0x00a0-0x03c0)
1413 * 0x7f0 = forced mono mode
1415 msp3400c_write(client
,
1417 0x22, /* a2 threshold for stereo/bilingual */
1422 static void msp34xxg_detect_stereo(struct i2c_client
*client
)
1424 struct msp3400c
*msp
= i2c_get_clientdata(client
);
1426 int status
= msp3400c_read(client
,
1428 0x0200 /* STATUS */);
1429 int is_bilingual
= status
&0x100;
1430 int is_stereo
= status
&0x40;
1432 msp
->rxsubchans
= 0;
1434 msp
->rxsubchans
|= V4L2_TUNER_SUB_STEREO
;
1436 msp
->rxsubchans
|= V4L2_TUNER_SUB_MONO
;
1438 msp
->rxsubchans
|= V4L2_TUNER_SUB_LANG1
|V4L2_TUNER_SUB_LANG2
;
1439 /* I'm supposed to check whether it's SAP or not
1440 * and set only LANG2/SAP in this case. Yet, the MSP
1441 * does a lot of work to hide this and handle everything
1442 * the same way. I don't want to work around it so unless
1443 * this is a problem, I'll handle SAP just like lang1/lang2.
1446 dprintk("msp34xxg: status=0x%x, stereo=%d, bilingual=%d -> rxsubchans=%d\n",
1447 status
, is_stereo
, is_bilingual
, msp
->rxsubchans
);
1450 static void msp34xxg_set_audmode(struct i2c_client
*client
, int audmode
)
1452 struct msp3400c
*msp
= i2c_get_clientdata(client
);
1456 case V4L2_TUNER_MODE_MONO
:
1457 source
=0; /* mono only */
1459 case V4L2_TUNER_MODE_STEREO
:
1460 source
=1; /* stereo or A|B, see comment in msp34xxg_get_v4l2_stereo() */
1461 /* problem: that could also mean 2 (scart input) */
1463 case V4L2_TUNER_MODE_LANG1
:
1464 source
=3; /* stereo or A */
1466 case V4L2_TUNER_MODE_LANG2
:
1467 source
=4; /* stereo or B */
1474 msp
->audmode
= audmode
;
1475 msp34xxg_set_source(client
, source
);
1479 /* ----------------------------------------------------------------------- */
1481 static int msp_attach(struct i2c_adapter
*adap
, int addr
, int kind
);
1482 static int msp_detach(struct i2c_client
*client
);
1483 static int msp_probe(struct i2c_adapter
*adap
);
1484 static int msp_command(struct i2c_client
*client
, unsigned int cmd
, void *arg
);
1486 static int msp_suspend(struct device
* dev
, pm_message_t state
);
1487 static int msp_resume(struct device
* dev
);
1489 static void msp_wake_thread(struct i2c_client
*client
);
1491 static struct i2c_driver driver
= {
1492 .owner
= THIS_MODULE
,
1493 .name
= "i2c msp3400 driver",
1494 .id
= I2C_DRIVERID_MSP3400
,
1495 .flags
= I2C_DF_NOTIFY
,
1496 .attach_adapter
= msp_probe
,
1497 .detach_client
= msp_detach
,
1498 .command
= msp_command
,
1500 .suspend
= msp_suspend
,
1501 .resume
= msp_resume
,
1505 static struct i2c_client client_template
=
1508 .flags
= I2C_CLIENT_ALLOW_USE
,
1512 static int msp_attach(struct i2c_adapter
*adap
, int addr
, int kind
)
1514 struct msp3400c
*msp
;
1515 struct i2c_client
*c
;
1516 int (*thread_func
)(void *data
) = NULL
;
1519 client_template
.adapter
= adap
;
1520 client_template
.addr
= addr
;
1522 if (-1 == msp3400c_reset(&client_template
)) {
1523 dprintk("msp34xx: no chip found\n");
1527 if (NULL
== (c
= kmalloc(sizeof(struct i2c_client
),GFP_KERNEL
)))
1529 memcpy(c
,&client_template
,sizeof(struct i2c_client
));
1530 if (NULL
== (msp
= kmalloc(sizeof(struct msp3400c
),GFP_KERNEL
))) {
1535 memset(msp
,0,sizeof(struct msp3400c
));
1536 msp
->norm
= VIDEO_MODE_NTSC
;
1537 msp
->left
= 58880; /* 0db gain */
1538 msp
->right
= 58880; /* 0db gain */
1540 msp
->treble
= 32768;
1543 for (i
= 0; i
< DFP_COUNT
; i
++)
1544 msp
->dfp_regs
[i
] = -1;
1546 i2c_set_clientdata(c
, msp
);
1547 init_waitqueue_head(&msp
->wq
);
1549 if (-1 == msp3400c_reset(c
)) {
1552 dprintk("msp34xx: no chip found\n");
1556 msp
->rev1
= msp3400c_read(c
, I2C_MSP3400C_DFP
, 0x1e);
1557 if (-1 != msp
->rev1
)
1558 msp
->rev2
= msp3400c_read(c
, I2C_MSP3400C_DFP
, 0x1f);
1559 if ((-1 == msp
->rev1
) || (0 == msp
->rev1
&& 0 == msp
->rev2
)) {
1562 dprintk("msp34xx: error while reading chip version\n");
1565 printk(KERN_INFO
"msp34xx: rev1=0x%04x, rev2=0x%04x\n", msp
->rev1
, msp
->rev2
);
1567 msp3400c_setvolume(c
, msp
->muted
, msp
->left
, msp
->right
);
1569 snprintf(c
->name
, sizeof(c
->name
), "MSP34%02d%c-%c%d",
1570 (msp
->rev2
>>8)&0xff, (msp
->rev1
&0xff)+'@',
1571 ((msp
->rev1
>>8)&0xff)+'@', msp
->rev2
&0x1f);
1573 msp
->opmode
= opmode
;
1574 if (OPMODE_AUTO
== msp
->opmode
) {
1575 if (HAVE_SIMPLER(msp
))
1576 msp
->opmode
= OPMODE_SIMPLER
;
1577 else if (HAVE_SIMPLE(msp
))
1578 msp
->opmode
= OPMODE_SIMPLE
;
1580 msp
->opmode
= OPMODE_MANUAL
;
1583 /* hello world :-) */
1584 printk(KERN_INFO
"msp34xx: init: chip=%s", c
->name
);
1585 if (HAVE_NICAM(msp
))
1587 if (HAVE_SIMPLE(msp
))
1589 if (HAVE_SIMPLER(msp
))
1590 printk(" +simpler");
1591 if (HAVE_RADIO(msp
))
1594 /* version-specific initialization */
1595 switch (msp
->opmode
) {
1597 printk(" mode=manual");
1598 thread_func
= msp3400c_thread
;
1601 printk(" mode=simple");
1602 thread_func
= msp3410d_thread
;
1604 case OPMODE_SIMPLER
:
1605 printk(" mode=simpler");
1606 thread_func
= msp34xxg_thread
;
1611 /* startup control thread if needed */
1613 msp
->kthread
= kthread_run(thread_func
, c
, "msp34xx");
1615 if (NULL
== msp
->kthread
)
1616 printk(KERN_WARNING
"msp34xx: kernel_thread() failed\n");
1621 i2c_attach_client(c
);
1623 /* update our own array */
1624 for (i
= 0; i
< MSP3400_MAX
; i
++) {
1625 if (NULL
== msps
[i
]) {
1634 static int msp_detach(struct i2c_client
*client
)
1636 struct msp3400c
*msp
= i2c_get_clientdata(client
);
1639 /* shutdown control thread */
1642 kthread_stop(msp
->kthread
);
1644 msp3400c_reset(client
);
1646 /* update our own array */
1647 for (i
= 0; i
< MSP3400_MAX
; i
++) {
1648 if (client
== msps
[i
]) {
1654 i2c_detach_client(client
);
1661 static int msp_probe(struct i2c_adapter
*adap
)
1663 if (adap
->class & I2C_CLASS_TV_ANALOG
)
1664 return i2c_probe(adap
, &addr_data
, msp_attach
);
1668 static void msp_wake_thread(struct i2c_client
*client
)
1670 struct msp3400c
*msp
= i2c_get_clientdata(client
);
1672 if (NULL
== msp
->kthread
)
1674 msp3400c_setvolume(client
,msp
->muted
,0,0);
1675 msp
->watch_stereo
= 0;
1677 wake_up_interruptible(&msp
->wq
);
1680 /* ----------------------------------------------------------------------- */
1682 static int mode_v4l2_to_v4l1(int rxsubchans
)
1686 if (rxsubchans
& V4L2_TUNER_SUB_STEREO
)
1687 mode
|= VIDEO_SOUND_STEREO
;
1688 if (rxsubchans
& V4L2_TUNER_SUB_LANG2
)
1689 mode
|= VIDEO_SOUND_LANG2
;
1690 if (rxsubchans
& V4L2_TUNER_SUB_LANG1
)
1691 mode
|= VIDEO_SOUND_LANG1
;
1693 mode
|= VIDEO_SOUND_MONO
;
1697 static int mode_v4l1_to_v4l2(int mode
)
1699 if (mode
& VIDEO_SOUND_STEREO
)
1700 return V4L2_TUNER_MODE_STEREO
;
1701 if (mode
& VIDEO_SOUND_LANG2
)
1702 return V4L2_TUNER_MODE_LANG2
;
1703 if (mode
& VIDEO_SOUND_LANG1
)
1704 return V4L2_TUNER_MODE_LANG1
;
1705 return V4L2_TUNER_MODE_MONO
;
1708 static void msp_any_detect_stereo(struct i2c_client
*client
)
1710 struct msp3400c
*msp
= i2c_get_clientdata(client
);
1712 switch (msp
->opmode
) {
1715 autodetect_stereo(client
);
1717 case OPMODE_SIMPLER
:
1718 msp34xxg_detect_stereo(client
);
1723 static void msp_any_set_audmode(struct i2c_client
*client
, int audmode
)
1725 struct msp3400c
*msp
= i2c_get_clientdata(client
);
1727 switch (msp
->opmode
) {
1730 msp
->watch_stereo
= 0;
1731 msp3400c_setstereo(client
, audmode
);
1733 case OPMODE_SIMPLER
:
1734 msp34xxg_set_audmode(client
, audmode
);
1739 static int msp_command(struct i2c_client
*client
, unsigned int cmd
, void *arg
)
1741 struct msp3400c
*msp
= i2c_get_clientdata(client
);
1747 case AUDC_SET_INPUT
:
1748 dprintk("msp34xx: AUDC_SET_INPUT(%d)\n",*sarg
);
1749 if (*sarg
== msp
->input
)
1754 /* Hauppauge uses IN2 for the radio */
1755 msp
->mode
= MSP_MODE_FM_RADIO
;
1758 case AUDIO_EXTERN_1
:
1759 /* IN1 is often used for external input ... */
1760 msp
->mode
= MSP_MODE_EXTERN
;
1763 case AUDIO_EXTERN_2
:
1764 /* ... sometimes it is IN2 through ;) */
1765 msp
->mode
= MSP_MODE_EXTERN
;
1772 if (*sarg
& AUDIO_MUTE
)
1773 msp3400c_set_scart(client
,SCART_MUTE
,0);
1777 msp
->rxsubchans
= V4L2_TUNER_SUB_STEREO
;
1778 msp
->audmode
= V4L2_TUNER_MODE_STEREO
;
1779 msp3400c_set_scart(client
,scart
,0);
1780 msp3400c_write(client
,I2C_MSP3400C_DFP
,0x000d,0x1900);
1781 if (msp
->opmode
!= OPMODE_SIMPLER
)
1782 msp3400c_setstereo(client
, msp
->audmode
);
1784 msp_wake_thread(client
);
1787 case AUDC_SET_RADIO
:
1788 dprintk("msp34xx: AUDC_SET_RADIO\n");
1789 msp
->norm
= VIDEO_MODE_RADIO
;
1790 dprintk("msp34xx: switching to radio mode\n");
1791 msp
->watch_stereo
= 0;
1792 switch (msp
->opmode
) {
1794 /* set msp3400 to FM radio mode */
1795 msp3400c_setmode(client
,MSP_MODE_FM_RADIO
);
1796 msp3400c_setcarrier(client
, MSP_CARRIER(10.7),
1798 msp3400c_setvolume(client
, msp
->muted
, msp
->left
, msp
->right
);
1801 case OPMODE_SIMPLER
:
1802 /* the thread will do for us */
1803 msp_wake_thread(client
);
1807 /* work-in-progress: hook to control the DFP registers */
1808 case MSP_SET_DFPREG
:
1810 struct msp_dfpreg
*r
= arg
;
1813 if (r
->reg
< 0 || r
->reg
>= DFP_COUNT
)
1815 for (i
= 0; i
< sizeof(bl_dfp
) / sizeof(int); i
++)
1816 if (r
->reg
== bl_dfp
[i
])
1818 msp
->dfp_regs
[r
->reg
] = r
->value
;
1819 msp3400c_write(client
, I2C_MSP3400C_DFP
, r
->reg
, r
->value
);
1822 case MSP_GET_DFPREG
:
1824 struct msp_dfpreg
*r
= arg
;
1826 if (r
->reg
< 0 || r
->reg
>= DFP_COUNT
)
1828 r
->value
= msp3400c_read(client
, I2C_MSP3400C_DFP
, r
->reg
);
1832 /* --- v4l ioctls --- */
1833 /* take care: bttv does userspace copying, we'll get a
1834 kernel pointer here... */
1837 struct video_audio
*va
= arg
;
1839 dprintk("msp34xx: VIDIOCGAUDIO\n");
1840 va
->flags
|= VIDEO_AUDIO_VOLUME
|
1842 VIDEO_AUDIO_TREBLE
|
1843 VIDEO_AUDIO_MUTABLE
;
1845 va
->flags
|= VIDEO_AUDIO_MUTE
;
1848 va
->flags
|= VIDEO_AUDIO_MUTE
;
1849 va
->volume
= MAX(msp
->left
, msp
->right
);
1850 va
->balance
= (32768 * MIN(msp
->left
, msp
->right
)) /
1851 (va
->volume
? va
->volume
: 1);
1852 va
->balance
= (msp
->left
< msp
->right
) ?
1853 (65535 - va
->balance
) : va
->balance
;
1854 if (0 == va
->volume
)
1855 va
->balance
= 32768;
1856 va
->bass
= msp
->bass
;
1857 va
->treble
= msp
->treble
;
1859 msp_any_detect_stereo(client
);
1860 va
->mode
= mode_v4l2_to_v4l1(msp
->rxsubchans
);
1865 struct video_audio
*va
= arg
;
1867 dprintk("msp34xx: VIDIOCSAUDIO\n");
1868 msp
->muted
= (va
->flags
& VIDEO_AUDIO_MUTE
);
1869 msp
->left
= (MIN(65536 - va
->balance
, 32768) *
1870 va
->volume
) / 32768;
1871 msp
->right
= (MIN(va
->balance
, 32768) * va
->volume
) / 32768;
1872 msp
->bass
= va
->bass
;
1873 msp
->treble
= va
->treble
;
1874 dprintk("msp34xx: VIDIOCSAUDIO setting va->volume to %d\n",
1876 dprintk("msp34xx: VIDIOCSAUDIO setting va->balance to %d\n",
1878 dprintk("msp34xx: VIDIOCSAUDIO setting va->flags to %d\n",
1880 dprintk("msp34xx: VIDIOCSAUDIO setting msp->left to %d\n",
1882 dprintk("msp34xx: VIDIOCSAUDIO setting msp->right to %d\n",
1884 dprintk("msp34xx: VIDIOCSAUDIO setting msp->bass to %d\n",
1886 dprintk("msp34xx: VIDIOCSAUDIO setting msp->treble to %d\n",
1888 dprintk("msp34xx: VIDIOCSAUDIO setting msp->mode to %d\n",
1890 msp3400c_setvolume(client
, msp
->muted
, msp
->left
, msp
->right
);
1891 msp3400c_setbass(client
, msp
->bass
);
1892 msp3400c_settreble(client
, msp
->treble
);
1894 if (va
->mode
!= 0 && msp
->norm
!= VIDEO_MODE_RADIO
)
1895 msp_any_set_audmode(client
,mode_v4l1_to_v4l2(va
->mode
));
1901 struct video_channel
*vc
= arg
;
1903 dprintk("msp34xx: VIDIOCSCHAN (norm=%d)\n",vc
->norm
);
1904 msp
->norm
= vc
->norm
;
1905 msp_wake_thread(client
);
1910 case VIDIOC_S_FREQUENCY
:
1912 /* new channel -- kick audio carrier scan */
1913 dprintk("msp34xx: VIDIOCSFREQ\n");
1914 msp_wake_thread(client
);
1918 /* --- v4l2 ioctls --- */
1921 v4l2_std_id
*id
= arg
;
1923 /*FIXME: use V4L2 mode flags on msp3400 instead of V4L1*/
1924 if (*id
& V4L2_STD_PAL
) {
1925 msp
->norm
=VIDEO_MODE_PAL
;
1926 } else if (*id
& V4L2_STD_SECAM
) {
1927 msp
->norm
=VIDEO_MODE_SECAM
;
1929 msp
->norm
=VIDEO_MODE_NTSC
;
1932 msp_wake_thread(client
);
1936 case VIDIOC_G_AUDIO
:
1938 struct v4l2_audio
*a
= arg
;
1940 memset(a
,0,sizeof(*a
));
1944 strcpy(a
->name
,"Radio");
1946 case AUDIO_EXTERN_1
:
1947 strcpy(a
->name
,"Extern 1");
1949 case AUDIO_EXTERN_2
:
1950 strcpy(a
->name
,"Extern 2");
1953 strcpy(a
->name
,"Television");
1959 msp_any_detect_stereo(client
);
1960 if (msp
->audmode
== V4L2_TUNER_MODE_STEREO
) {
1961 a
->capability
=V4L2_AUDCAP_STEREO
;
1966 case VIDIOC_S_AUDIO
:
1968 struct v4l2_audio
*sarg
= arg
;
1970 switch (sarg
->index
) {
1972 /* Hauppauge uses IN2 for the radio */
1973 msp
->mode
= MSP_MODE_FM_RADIO
;
1976 case AUDIO_EXTERN_1
:
1977 /* IN1 is often used for external input ... */
1978 msp
->mode
= MSP_MODE_EXTERN
;
1981 case AUDIO_EXTERN_2
:
1982 /* ... sometimes it is IN2 through ;) */
1983 msp
->mode
= MSP_MODE_EXTERN
;
1991 msp
->rxsubchans
= V4L2_TUNER_SUB_STEREO
;
1992 msp
->audmode
= V4L2_TUNER_MODE_STEREO
;
1993 msp3400c_set_scart(client
,scart
,0);
1994 msp3400c_write(client
,I2C_MSP3400C_DFP
,0x000d,0x1900);
1996 if (sarg
->capability
==V4L2_AUDCAP_STEREO
) {
1997 msp
->audmode
= V4L2_TUNER_MODE_STEREO
;
1999 msp
->audmode
&= ~V4L2_TUNER_MODE_STEREO
;
2001 msp_any_set_audmode(client
, msp
->audmode
);
2002 msp_wake_thread(client
);
2005 case VIDIOC_G_TUNER
:
2007 struct v4l2_tuner
*vt
= arg
;
2009 msp_any_detect_stereo(client
);
2010 vt
->audmode
= msp
->audmode
;
2011 vt
->rxsubchans
= msp
->rxsubchans
;
2012 vt
->capability
= V4L2_TUNER_CAP_STEREO
|
2013 V4L2_TUNER_CAP_LANG1
|
2014 V4L2_TUNER_CAP_LANG2
;
2017 case VIDIOC_S_TUNER
:
2019 struct v4l2_tuner
*vt
=(struct v4l2_tuner
*)arg
;
2021 /* only set audmode */
2022 if (vt
->audmode
!= -1 && vt
->audmode
!= 0)
2023 msp_any_set_audmode(client
, vt
->audmode
);
2027 /* msp34xx specific */
2028 case MSP_SET_MATRIX
:
2030 struct msp_matrix
*mspm
= arg
;
2032 dprintk("msp34xx: MSP_SET_MATRIX\n");
2033 msp3400c_set_scart(client
, mspm
->input
, mspm
->output
);
2044 static int msp_suspend(struct device
* dev
, pm_message_t state
)
2046 struct i2c_client
*c
= container_of(dev
, struct i2c_client
, dev
);
2048 dprintk("msp34xx: suspend\n");
2053 static int msp_resume(struct device
* dev
)
2055 struct i2c_client
*c
= container_of(dev
, struct i2c_client
, dev
);
2057 dprintk("msp34xx: resume\n");
2062 /* ----------------------------------------------------------------------- */
2064 static int __init
msp3400_init_module(void)
2066 return i2c_add_driver(&driver
);
2069 static void __exit
msp3400_cleanup_module(void)
2071 i2c_del_driver(&driver
);
2074 module_init(msp3400_init_module
);
2075 module_exit(msp3400_cleanup_module
);
2078 * Overrides for Emacs so that we follow Linus's tabbing style.
2079 * ---------------------------------------------------------------------------