1 /*
2 * programming the msp34* sound processor family
3 *
4 * (c) 1997-2001 Gerd Knorr <kraxel@bytesex.org>
5 *
6 * what works and what doesn't:
7 *
8 * AM-Mono
9 * Support for Hauppauge cards added (decoding handled by tuner) added by
10 * Frederic Crozat <fcrozat@mail.dotcom.fr>
11 *
12 * FM-Mono
13 * should work. The stereo modes are backward compatible to FM-mono,
14 * therefore FM-Mono should be allways available.
15 *
16 * FM-Stereo (B/G, used in germany)
17 * should work, with autodetect
18 *
19 * FM-Stereo (satellite)
20 * should work, no autodetect (i.e. default is mono, but you can
21 * switch to stereo -- untested)
22 *
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>
26 *
27 *
28 * TODO:
29 * - better SAT support
30 *
31 *
32 * 980623 Thomas Sailer (sailer@ife.ee.ethz.ch)
33 * using soundcore instead of OSS
34 *
35 */
36
37 #include <linux/config.h>
38 #include <linux/module.h>
39 #include <linux/kernel.h>
40 #include <linux/sched.h>
41 #include <linux/string.h>
42 #include <linux/timer.h>
43 #include <linux/delay.h>
44 #include <linux/errno.h>
45 #include <linux/slab.h>
46 #include <linux/i2c.h>
47 #include <linux/videodev.h>
48 #include <linux/init.h>
49 #include <linux/smp_lock.h>
50 #include <asm/semaphore.h>
51 #include <asm/pgtable.h>
52
53 #include "audiochip.h"
54 #include "i2c-compat.h"
55 #include "msp3400.h"
56
57 /* insmod parameters */
58 static int debug = 0; /* debug output */
59 static int once = 0; /* no continous stereo monitoring */
60 static int amsound = 0; /* hard-wire AM sound at 6.5 Hz (france),
61 the autoscan seems work well only with FM... */
62 static int simple = -1; /* use short programming (>= msp3410 only) */
63 static int dolby = 0;
64
65 #define DFP_COUNT 0x41
66 static const int bl_dfp[] = {
67 0x00, 0x01, 0x02, 0x03, 0x06, 0x08, 0x09, 0x0a,
68 0x0b, 0x0d, 0x0e, 0x10
69 };
70
71 struct msp3400c {
72 int rev1,rev2;
73
74 int simple;
75 int mode;
76 int norm;
77 int stereo;
78 int nicam_on;
79 int acb;
80 int main, second; /* sound carrier */
81 int input;
82
83 int muted;
84 int left, right; /* volume */
85 int bass, treble;
86
87 /* shadow register set */
88 int dfp_regs[DFP_COUNT];
89
90 /* thread */
91 struct task_struct *thread;
92 wait_queue_head_t wq;
93
94 struct semaphore *notify;
95 int active,restart,rmmod;
96
97 int watch_stereo;
98 struct timer_list wake_stereo;
99 };
100
101 #define HAVE_NICAM(msp) (((msp->rev2>>8) & 0xff) != 00)
102 #define HAVE_SIMPLE(msp) ((msp->rev1 & 0xff) >= 'D'-'@')
103 #define HAVE_RADIO(msp) ((msp->rev1 & 0xff) >= 'G'-'@')
104
105 #define MSP3400_MAX 4
106 static struct i2c_client *msps[MSP3400_MAX];
107
108 #define VIDEO_MODE_RADIO 16 /* norm magic for radio mode */
109
110 /* ---------------------------------------------------------------------- */
111
112 #define dprintk if (debug) printk
113
114 MODULE_PARM(once,"i");
115 MODULE_PARM(debug,"i");
116 MODULE_PARM(simple,"i");
117 MODULE_PARM(amsound,"i");
118 MODULE_PARM(dolby,"i");
119
120 MODULE_DESCRIPTION("device driver for msp34xx TV sound processor");
121 MODULE_AUTHOR("Gerd Knorr");
122 MODULE_LICENSE("Dual BSD/GPL"); /* FreeBSD uses this too */
123
124 /* ---------------------------------------------------------------------- */
125
126 #define I2C_MSP3400C 0x80
127 #define I2C_MSP3400C_ALT 0x88
128
129 #define I2C_MSP3400C_DEM 0x10
130 #define I2C_MSP3400C_DFP 0x12
131
132 /* Addresses to scan */
133 static unsigned short normal_i2c[] = {
134 I2C_MSP3400C >> 1,
135 I2C_MSP3400C_ALT >> 1,
136 I2C_CLIENT_END
137 };
138 static unsigned short normal_i2c_range[] = {I2C_CLIENT_END,I2C_CLIENT_END};
139 I2C_CLIENT_INSMOD;
140
141 /* ----------------------------------------------------------------------- */
142 /* functions for talking to the MSP3400C Sound processor */
143
144 #ifndef I2C_M_IGNORE_NAK
145 # define I2C_M_IGNORE_NAK 0x1000
146 #endif
147
msp3400c_reset(struct i2c_client * client)148 static int msp3400c_reset(struct i2c_client *client)
149 {
150 /* reset and read revision code */
151 static char reset_off[3] = { 0x00, 0x80, 0x00 };
152 static char reset_on[3] = { 0x00, 0x00, 0x00 };
153 static char write[3] = { I2C_MSP3400C_DFP + 1, 0x00, 0x1e };
154 char read[2];
155 struct i2c_msg reset[2] = {
156 { client->addr, I2C_M_IGNORE_NAK, 3, reset_off },
157 { client->addr, I2C_M_IGNORE_NAK, 3, reset_on },
158 };
159 struct i2c_msg test[2] = {
160 { client->addr, 0, 3, write },
161 { client->addr, I2C_M_RD, 2, read },
162 };
163
164 if ( (1 != i2c_transfer(client->adapter,&reset[0],1)) ||
165 (1 != i2c_transfer(client->adapter,&reset[1],1)) ||
166 (2 != i2c_transfer(client->adapter,test,2)) ) {
167 printk(KERN_ERR "msp3400: chip reset failed\n");
168 return -1;
169 }
170 return 0;
171 }
172
173 static int
msp3400c_read(struct i2c_client * client,int dev,int addr)174 msp3400c_read(struct i2c_client *client, int dev, int addr)
175 {
176 int err;
177
178 unsigned char write[3];
179 unsigned char read[2];
180 struct i2c_msg msgs[2] = {
181 { client->addr, 0, 3, write },
182 { client->addr, I2C_M_RD, 2, read }
183 };
184 write[0] = dev+1;
185 write[1] = addr >> 8;
186 write[2] = addr & 0xff;
187
188 for (err = 0; err < 3;) {
189 if (2 == i2c_transfer(client->adapter,msgs,2))
190 break;
191 err++;
192 printk(KERN_WARNING "msp34xx: I/O error #%d (read 0x%02x/0x%02x)\n",
193 err, dev, addr);
194 current->state = TASK_INTERRUPTIBLE;
195 schedule_timeout(HZ/10);
196 }
197 if (3 == err) {
198 printk(KERN_WARNING "msp34xx: giving up, reseting chip. Sound will go off, sorry folks :-|\n");
199 msp3400c_reset(client);
200 return -1;
201 }
202 return read[0] << 8 | read[1];
203 }
204
205 static int
msp3400c_write(struct i2c_client * client,int dev,int addr,int val)206 msp3400c_write(struct i2c_client *client, int dev, int addr, int val)
207 {
208 int err;
209 unsigned char buffer[5];
210
211 buffer[0] = dev;
212 buffer[1] = addr >> 8;
213 buffer[2] = addr & 0xff;
214 buffer[3] = val >> 8;
215 buffer[4] = val & 0xff;
216
217 for (err = 0; err < 3;) {
218 if (5 == i2c_master_send(client, buffer, 5))
219 break;
220 err++;
221 printk(KERN_WARNING "msp34xx: I/O error #%d (write 0x%02x/0x%02x)\n",
222 err, dev, addr);
223 current->state = TASK_INTERRUPTIBLE;
224 schedule_timeout(HZ/10);
225 }
226 if (3 == err) {
227 printk(KERN_WARNING "msp34xx: giving up, reseting chip. Sound will go off, sorry folks :-|\n");
228 msp3400c_reset(client);
229 return -1;
230 }
231 return 0;
232 }
233
234 /* ------------------------------------------------------------------------ */
235
236 /* This macro is allowed for *constants* only, gcc must calculate it
237 at compile time. Remember -- no floats in kernel mode */
238 #define MSP_CARRIER(freq) ((int)((float)(freq/18.432)*(1<<24)))
239
240 #define MSP_MODE_AM_DETECT 0
241 #define MSP_MODE_FM_RADIO 2
242 #define MSP_MODE_FM_TERRA 3
243 #define MSP_MODE_FM_SAT 4
244 #define MSP_MODE_FM_NICAM1 5
245 #define MSP_MODE_FM_NICAM2 6
246 #define MSP_MODE_AM_NICAM 7
247 #define MSP_MODE_BTSC 8
248 #define MSP_MODE_EXTERN 9
249
250 static struct MSP_INIT_DATA_DEM {
251 int fir1[6];
252 int fir2[6];
253 int cdo1;
254 int cdo2;
255 int ad_cv;
256 int mode_reg;
257 int dfp_src;
258 int dfp_matrix;
259 } msp_init_data[] = {
260 /* AM (for carrier detect / msp3400) */
261 { { 75, 19, 36, 35, 39, 40 }, { 75, 19, 36, 35, 39, 40 },
262 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
263 0x00d0, 0x0500, 0x0020, 0x3000},
264
265 /* AM (for carrier detect / msp3410) */
266 { { -1, -1, -8, 2, 59, 126 }, { -1, -1, -8, 2, 59, 126 },
267 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
268 0x00d0, 0x0100, 0x0020, 0x3000},
269
270 /* FM Radio */
271 { { -8, -8, 4, 6, 78, 107 }, { -8, -8, 4, 6, 78, 107 },
272 MSP_CARRIER(10.7), MSP_CARRIER(10.7),
273 0x00d0, 0x0480, 0x0020, 0x3000 },
274
275 /* Terrestial FM-mono + FM-stereo */
276 { { 3, 18, 27, 48, 66, 72 }, { 3, 18, 27, 48, 66, 72 },
277 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
278 0x00d0, 0x0480, 0x0030, 0x3000},
279
280 /* Sat FM-mono */
281 { { 1, 9, 14, 24, 33, 37 }, { 3, 18, 27, 48, 66, 72 },
282 MSP_CARRIER(6.5), MSP_CARRIER(6.5),
283 0x00c6, 0x0480, 0x0000, 0x3000},
284
285 /* NICAM/FM -- B/G (5.5/5.85), D/K (6.5/5.85) */
286 { { -2, -8, -10, 10, 50, 86 }, { 3, 18, 27, 48, 66, 72 },
287 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
288 0x00d0, 0x0040, 0x0120, 0x3000},
289
290 /* NICAM/FM -- I (6.0/6.552) */
291 { { 2, 4, -6, -4, 40, 94 }, { 3, 18, 27, 48, 66, 72 },
292 MSP_CARRIER(6.0), MSP_CARRIER(6.0),
293 0x00d0, 0x0040, 0x0120, 0x3000},
294
295 /* NICAM/AM -- L (6.5/5.85) */
296 { { -2, -8, -10, 10, 50, 86 }, { -4, -12, -9, 23, 79, 126 },
297 MSP_CARRIER(6.5), MSP_CARRIER(6.5),
298 0x00c6, 0x0140, 0x0120, 0x7c03},
299 };
300
301 struct CARRIER_DETECT {
302 int cdo;
303 char *name;
304 };
305
306 static struct CARRIER_DETECT carrier_detect_main[] = {
307 /* main carrier */
308 { MSP_CARRIER(4.5), "4.5 NTSC" },
309 { MSP_CARRIER(5.5), "5.5 PAL B/G" },
310 { MSP_CARRIER(6.0), "6.0 PAL I" },
311 { MSP_CARRIER(6.5), "6.5 PAL D/K + SAT + SECAM" }
312 };
313
314 static struct CARRIER_DETECT carrier_detect_55[] = {
315 /* PAL B/G */
316 { MSP_CARRIER(5.7421875), "5.742 PAL B/G FM-stereo" },
317 { MSP_CARRIER(5.85), "5.85 PAL B/G NICAM" }
318 };
319
320 static struct CARRIER_DETECT carrier_detect_65[] = {
321 /* PAL SAT / SECAM */
322 { MSP_CARRIER(5.85), "5.85 PAL D/K + SECAM NICAM" },
323 { MSP_CARRIER(6.2578125), "6.25 PAL D/K1 FM-stereo" },
324 { MSP_CARRIER(6.7421875), "6.74 PAL D/K2 FM-stereo" },
325 { MSP_CARRIER(7.02), "7.02 PAL SAT FM-stereo s/b" },
326 { MSP_CARRIER(7.20), "7.20 PAL SAT FM-stereo s" },
327 { MSP_CARRIER(7.38), "7.38 PAL SAT FM-stereo b" },
328 };
329
330 #define CARRIER_COUNT(x) (sizeof(x)/sizeof(struct CARRIER_DETECT))
331
332 /* ----------------------------------------------------------------------- */
333
334 #define SCART_MASK 0
335 #define SCART_IN1 1
336 #define SCART_IN2 2
337 #define SCART_IN1_DA 3
338 #define SCART_IN2_DA 4
339 #define SCART_IN3 5
340 #define SCART_IN4 6
341 #define SCART_MONO 7
342 #define SCART_MUTE 8
343
344 static int scarts[3][9] = {
345 /* MASK IN1 IN2 IN1_DA IN2_DA IN3 IN4 MONO MUTE */
346 { 0x0320, 0x0000, 0x0200, -1, -1, 0x0300, 0x0020, 0x0100, 0x0320 },
347 { 0x0c40, 0x0440, 0x0400, 0x0c00, 0x0040, 0x0000, 0x0840, 0x0800, 0x0c40 },
348 { 0x3080, 0x1000, 0x1080, 0x0000, 0x0080, 0x2080, 0x3080, 0x2000, 0x3000 },
349 };
350
351 static char *scart_names[] = {
352 "mask", "in1", "in2", "in1 da", "in2 da", "in3", "in4", "mono", "mute"
353 };
354
355 static void
msp3400c_set_scart(struct i2c_client * client,int in,int out)356 msp3400c_set_scart(struct i2c_client *client, int in, int out)
357 {
358 struct msp3400c *msp = i2c_get_clientdata(client);
359
360 if (-1 == scarts[out][in])
361 return;
362
363 dprintk(KERN_DEBUG
364 "msp34xx: scart switch: %s => %d\n",scart_names[in],out);
365 msp->acb &= ~scarts[out][SCART_MASK];
366 msp->acb |= scarts[out][in];
367 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0013, msp->acb);
368 }
369
370 /* ------------------------------------------------------------------------ */
371
msp3400c_setcarrier(struct i2c_client * client,int cdo1,int cdo2)372 static void msp3400c_setcarrier(struct i2c_client *client, int cdo1, int cdo2)
373 {
374 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0093, cdo1 & 0xfff);
375 msp3400c_write(client,I2C_MSP3400C_DEM, 0x009b, cdo1 >> 12);
376 msp3400c_write(client,I2C_MSP3400C_DEM, 0x00a3, cdo2 & 0xfff);
377 msp3400c_write(client,I2C_MSP3400C_DEM, 0x00ab, cdo2 >> 12);
378 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0056, 0); /*LOAD_REG_1/2*/
379 }
380
msp3400c_setvolume(struct i2c_client * client,int muted,int left,int right)381 static void msp3400c_setvolume(struct i2c_client *client,
382 int muted, int left, int right)
383 {
384 int vol = 0,val = 0,balance = 0;
385
386 if (!muted) {
387 vol = (left > right) ? left : right;
388 val = (vol * 0x73 / 65535) << 8;
389 }
390 if (vol > 0) {
391 balance = ((right-left) * 127) / vol;
392 }
393
394 dprintk(KERN_DEBUG
395 "msp34xx: setvolume: mute=%s %d:%d v=0x%02x b=0x%02x\n",
396 muted ? "on" : "off", left, right, val>>8, balance);
397 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0000, val); /* loudspeaker */
398 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0006, val); /* headphones */
399 /* scart - on/off only */
400 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0007, val ? 0x4000 : 0);
401 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0001, balance << 8);
402 }
403
msp3400c_setbass(struct i2c_client * client,int bass)404 static void msp3400c_setbass(struct i2c_client *client, int bass)
405 {
406 int val = ((bass-32768) * 0x60 / 65535) << 8;
407
408 dprintk(KERN_DEBUG "msp34xx: setbass: %d 0x%02x\n",bass, val>>8);
409 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0002, val); /* loudspeaker */
410 }
411
msp3400c_settreble(struct i2c_client * client,int treble)412 static void msp3400c_settreble(struct i2c_client *client, int treble)
413 {
414 int val = ((treble-32768) * 0x60 / 65535) << 8;
415
416 dprintk(KERN_DEBUG "msp34xx: settreble: %d 0x%02x\n",treble, val>>8);
417 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0003, val); /* loudspeaker */
418 }
419
msp3400c_setmode(struct i2c_client * client,int type)420 static void msp3400c_setmode(struct i2c_client *client, int type)
421 {
422 struct msp3400c *msp = i2c_get_clientdata(client);
423 int i;
424
425 dprintk(KERN_DEBUG "msp3400: setmode: %d\n",type);
426 msp->mode = type;
427 msp->stereo = VIDEO_SOUND_MONO;
428
429 msp3400c_write(client,I2C_MSP3400C_DEM, 0x00bb, /* ad_cv */
430 msp_init_data[type].ad_cv);
431
432 for (i = 5; i >= 0; i--) /* fir 1 */
433 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0001,
434 msp_init_data[type].fir1[i]);
435
436 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005, 0x0004); /* fir 2 */
437 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005, 0x0040);
438 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005, 0x0000);
439 for (i = 5; i >= 0; i--)
440 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005,
441 msp_init_data[type].fir2[i]);
442
443 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0083, /* MODE_REG */
444 msp_init_data[type].mode_reg);
445
446 msp3400c_setcarrier(client, msp_init_data[type].cdo1,
447 msp_init_data[type].cdo2);
448
449 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0056, 0); /*LOAD_REG_1/2*/
450
451 if (dolby) {
452 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008,
453 0x0520); /* I2S1 */
454 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009,
455 0x0620); /* I2S2 */
456 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b,
457 msp_init_data[type].dfp_src);
458 } else {
459 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008,
460 msp_init_data[type].dfp_src);
461 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009,
462 msp_init_data[type].dfp_src);
463 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b,
464 msp_init_data[type].dfp_src);
465 }
466 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000a,
467 msp_init_data[type].dfp_src);
468 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000e,
469 msp_init_data[type].dfp_matrix);
470
471 if (HAVE_NICAM(msp)) {
472 /* nicam prescale */
473 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0010, 0x5a00); /* was: 0x3000 */
474 }
475 }
476
477 /* turn on/off nicam + stereo */
msp3400c_setstereo(struct i2c_client * client,int mode)478 static void msp3400c_setstereo(struct i2c_client *client, int mode)
479 {
480 static char *strmode[16] = {
481 #if __GNUC__ >= 3
482 [ 0 ... 15 ] = "invalid",
483 #endif
484 [ VIDEO_SOUND_MONO ] = "mono",
485 [ VIDEO_SOUND_STEREO ] = "stereo",
486 [ VIDEO_SOUND_LANG1 ] = "lang1",
487 [ VIDEO_SOUND_LANG2 ] = "lang2",
488 };
489 struct msp3400c *msp = i2c_get_clientdata(client);
490 int nicam=0; /* channel source: FM/AM or nicam */
491 int src=0;
492
493 /* switch demodulator */
494 switch (msp->mode) {
495 case MSP_MODE_FM_TERRA:
496 dprintk(KERN_DEBUG "msp3400: FM setstereo: %s\n",strmode[mode]);
497 msp3400c_setcarrier(client,msp->second,msp->main);
498 switch (mode) {
499 case VIDEO_SOUND_STEREO:
500 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000e, 0x3001);
501 break;
502 case VIDEO_SOUND_MONO:
503 case VIDEO_SOUND_LANG1:
504 case VIDEO_SOUND_LANG2:
505 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000e, 0x3000);
506 break;
507 }
508 break;
509 case MSP_MODE_FM_SAT:
510 dprintk(KERN_DEBUG "msp3400: SAT setstereo: %s\n",strmode[mode]);
511 switch (mode) {
512 case VIDEO_SOUND_MONO:
513 msp3400c_setcarrier(client, MSP_CARRIER(6.5), MSP_CARRIER(6.5));
514 break;
515 case VIDEO_SOUND_STEREO:
516 msp3400c_setcarrier(client, MSP_CARRIER(7.2), MSP_CARRIER(7.02));
517 break;
518 case VIDEO_SOUND_LANG1:
519 msp3400c_setcarrier(client, MSP_CARRIER(7.38), MSP_CARRIER(7.02));
520 break;
521 case VIDEO_SOUND_LANG2:
522 msp3400c_setcarrier(client, MSP_CARRIER(7.38), MSP_CARRIER(7.02));
523 break;
524 }
525 break;
526 case MSP_MODE_FM_NICAM1:
527 case MSP_MODE_FM_NICAM2:
528 case MSP_MODE_AM_NICAM:
529 dprintk(KERN_DEBUG "msp3400: NICAM setstereo: %s\n",strmode[mode]);
530 msp3400c_setcarrier(client,msp->second,msp->main);
531 if (msp->nicam_on)
532 nicam=0x0100;
533 break;
534 case MSP_MODE_BTSC:
535 dprintk(KERN_DEBUG "msp3400: BTSC setstereo: %s\n",strmode[mode]);
536 nicam=0x0300;
537 break;
538 case MSP_MODE_EXTERN:
539 dprintk(KERN_DEBUG "msp3400: extern setstereo: %s\n",strmode[mode]);
540 nicam = 0x0200;
541 break;
542 case MSP_MODE_FM_RADIO:
543 dprintk(KERN_DEBUG "msp3400: FM-Radio setstereo: %s\n",strmode[mode]);
544 break;
545 default:
546 dprintk(KERN_DEBUG "msp3400: mono setstereo\n");
547 return;
548 }
549
550 /* switch audio */
551 switch (mode) {
552 case VIDEO_SOUND_STEREO:
553 src = 0x0020 | nicam;
554 #if 0
555 /* spatial effect */
556 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0005,0x4000);
557 #endif
558 break;
559 case VIDEO_SOUND_MONO:
560 if (msp->mode == MSP_MODE_AM_NICAM) {
561 dprintk("msp3400: switching to AM mono\n");
562 /* AM mono decoding is handled by tuner, not MSP chip */
563 /* SCART switching control register */
564 msp3400c_set_scart(client,SCART_MONO,0);
565 src = 0x0200;
566 break;
567 }
568 case VIDEO_SOUND_LANG1:
569 src = 0x0000 | nicam;
570 break;
571 case VIDEO_SOUND_LANG2:
572 src = 0x0010 | nicam;
573 break;
574 }
575 dprintk(KERN_DEBUG
576 "msp3400: setstereo final source/matrix = 0x%x\n", src);
577
578 if (dolby) {
579 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008,0x0520);
580 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009,0x0620);
581 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000a,src);
582 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b,src);
583 } else {
584 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008,src);
585 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009,src);
586 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000a,src);
587 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b,src);
588 }
589 }
590
591 static void
msp3400c_print_mode(struct msp3400c * msp)592 msp3400c_print_mode(struct msp3400c *msp)
593 {
594 if (msp->main == msp->second) {
595 printk(KERN_DEBUG "msp3400: mono sound carrier: %d.%03d MHz\n",
596 msp->main/910000,(msp->main/910)%1000);
597 } else {
598 printk(KERN_DEBUG "msp3400: main sound carrier: %d.%03d MHz\n",
599 msp->main/910000,(msp->main/910)%1000);
600 }
601 if (msp->mode == MSP_MODE_FM_NICAM1 ||
602 msp->mode == MSP_MODE_FM_NICAM2)
603 printk(KERN_DEBUG "msp3400: NICAM/FM carrier : %d.%03d MHz\n",
604 msp->second/910000,(msp->second/910)%1000);
605 if (msp->mode == MSP_MODE_AM_NICAM)
606 printk(KERN_DEBUG "msp3400: NICAM/AM carrier : %d.%03d MHz\n",
607 msp->second/910000,(msp->second/910)%1000);
608 if (msp->mode == MSP_MODE_FM_TERRA &&
609 msp->main != msp->second) {
610 printk(KERN_DEBUG "msp3400: FM-stereo carrier : %d.%03d MHz\n",
611 msp->second/910000,(msp->second/910)%1000);
612 }
613 }
614
615 static void
msp3400c_restore_dfp(struct i2c_client * client)616 msp3400c_restore_dfp(struct i2c_client *client)
617 {
618 struct msp3400c *msp = i2c_get_clientdata(client);
619 int i;
620
621 for (i = 0; i < DFP_COUNT; i++) {
622 if (-1 == msp->dfp_regs[i])
623 continue;
624 msp3400c_write(client,I2C_MSP3400C_DFP, i, msp->dfp_regs[i]);
625 }
626 }
627
628 /* ----------------------------------------------------------------------- */
629
630 struct REGISTER_DUMP {
631 int addr;
632 char *name;
633 };
634
635 struct REGISTER_DUMP d1[] = {
636 { 0x007e, "autodetect" },
637 { 0x0023, "C_AD_BITS " },
638 { 0x0038, "ADD_BITS " },
639 { 0x003e, "CIB_BITS " },
640 { 0x0057, "ERROR_RATE" },
641 };
642
643 static int
autodetect_stereo(struct i2c_client * client)644 autodetect_stereo(struct i2c_client *client)
645 {
646 struct msp3400c *msp = i2c_get_clientdata(client);
647 int val;
648 int newstereo = msp->stereo;
649 int newnicam = msp->nicam_on;
650 int update = 0;
651
652 switch (msp->mode) {
653 case MSP_MODE_FM_TERRA:
654 val = msp3400c_read(client, I2C_MSP3400C_DFP, 0x18);
655 if (val > 32767)
656 val -= 65536;
657 dprintk(KERN_DEBUG
658 "msp34xx: stereo detect register: %d\n",val);
659 if (val > 4096) {
660 newstereo = VIDEO_SOUND_STEREO | VIDEO_SOUND_MONO;
661 } else if (val < -4096) {
662 newstereo = VIDEO_SOUND_LANG1 | VIDEO_SOUND_LANG2;
663 } else {
664 newstereo = VIDEO_SOUND_MONO;
665 }
666 newnicam = 0;
667 break;
668 case MSP_MODE_FM_NICAM1:
669 case MSP_MODE_FM_NICAM2:
670 case MSP_MODE_AM_NICAM:
671 val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x23);
672 dprintk(KERN_DEBUG
673 "msp34xx: nicam sync=%d, mode=%d\n",
674 val & 1, (val & 0x1e) >> 1);
675
676 if (val & 1) {
677 /* nicam synced */
678 switch ((val & 0x1e) >> 1) {
679 case 0:
680 case 8:
681 newstereo = VIDEO_SOUND_STEREO;
682 break;
683 case 1:
684 case 9:
685 newstereo = VIDEO_SOUND_MONO
686 | VIDEO_SOUND_LANG1;
687 break;
688 case 2:
689 case 10:
690 newstereo = VIDEO_SOUND_MONO
691 | VIDEO_SOUND_LANG1
692 | VIDEO_SOUND_LANG2;
693 break;
694 default:
695 newstereo = VIDEO_SOUND_MONO;
696 break;
697 }
698 newnicam=1;
699 } else {
700 newnicam = 0;
701 newstereo = VIDEO_SOUND_MONO;
702 }
703 break;
704 case MSP_MODE_BTSC:
705 val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x200);
706 dprintk(KERN_DEBUG
707 "msp3410: status=0x%x (pri=%s, sec=%s, %s%s%s)\n",
708 val,
709 (val & 0x0002) ? "no" : "yes",
710 (val & 0x0004) ? "no" : "yes",
711 (val & 0x0040) ? "stereo" : "mono",
712 (val & 0x0080) ? ", nicam 2nd mono" : "",
713 (val & 0x0100) ? ", bilingual/SAP" : "");
714 newstereo = VIDEO_SOUND_MONO;
715 if (val & 0x0040) newstereo |= VIDEO_SOUND_STEREO;
716 if (val & 0x0100) newstereo |= VIDEO_SOUND_LANG1;
717 break;
718 }
719 if (newstereo != msp->stereo) {
720 update = 1;
721 dprintk(KERN_DEBUG "msp34xx: watch: stereo %d => %d\n",
722 msp->stereo,newstereo);
723 msp->stereo = newstereo;
724 }
725 if (newnicam != msp->nicam_on) {
726 update = 1;
727 dprintk(KERN_DEBUG "msp34xx: watch: nicam %d => %d\n",
728 msp->nicam_on,newnicam);
729 msp->nicam_on = newnicam;
730 }
731 return update;
732 }
733
734 /*
735 * A kernel thread for msp3400 control -- we don't want to block the
736 * in the ioctl while doing the sound carrier & stereo detect
737 */
738
msp3400c_stereo_wake(unsigned long data)739 static void msp3400c_stereo_wake(unsigned long data)
740 {
741 struct msp3400c *msp = (struct msp3400c*)data; /* XXX alpha ??? */
742
743 wake_up_interruptible(&msp->wq);
744 }
745
746 /* stereo/multilang monitoring */
watch_stereo(struct i2c_client * client)747 static void watch_stereo(struct i2c_client *client)
748 {
749 struct msp3400c *msp = i2c_get_clientdata(client);
750
751 if (autodetect_stereo(client)) {
752 if (msp->stereo & VIDEO_SOUND_STEREO)
753 msp3400c_setstereo(client,VIDEO_SOUND_STEREO);
754 else if (msp->stereo & VIDEO_SOUND_LANG1)
755 msp3400c_setstereo(client,VIDEO_SOUND_LANG1);
756 else if (msp->stereo & VIDEO_SOUND_LANG2)
757 msp3400c_setstereo(client,VIDEO_SOUND_LANG2);
758 else
759 msp3400c_setstereo(client,VIDEO_SOUND_MONO);
760 }
761 if (once)
762 msp->watch_stereo = 0;
763 if (msp->watch_stereo)
764 mod_timer(&msp->wake_stereo, jiffies+5*HZ);
765 }
766
msp3400c_thread(void * data)767 static int msp3400c_thread(void *data)
768 {
769 struct i2c_client *client = data;
770 struct msp3400c *msp = i2c_get_clientdata(client);
771
772 struct CARRIER_DETECT *cd;
773 int count, max1,max2,val1,val2, val,this;
774
775 lock_kernel();
776 daemonize();
777 sigfillset(¤t->blocked);
778 strcpy(current->comm,"msp3400");
779 msp->thread = current;
780 unlock_kernel();
781
782 printk("msp3400: daemon started\n");
783 if(msp->notify != NULL)
784 up(msp->notify);
785
786 for (;;) {
787 if (msp->rmmod)
788 goto done;
789 if (debug > 1)
790 printk("msp3400: thread: sleep\n");
791 interruptible_sleep_on(&msp->wq);
792 if (debug > 1)
793 printk("msp3400: thread: wakeup\n");
794 if (msp->rmmod || signal_pending(current))
795 goto done;
796
797 msp->active = 1;
798
799 if (msp->watch_stereo) {
800 watch_stereo(client);
801 msp->active = 0;
802 continue;
803 }
804
805 /* some time for the tuner to sync */
806 current->state = TASK_INTERRUPTIBLE;
807 schedule_timeout(HZ/5);
808 if (signal_pending(current))
809 goto done;
810
811 restart:
812 if (VIDEO_MODE_RADIO == msp->norm ||
813 MSP_MODE_EXTERN == msp->mode) {
814 /* no carrier scan, just unmute */
815 printk("msp3400: thread: no carrier scan\n");
816 msp3400c_setvolume(client, msp->muted,
817 msp->left, msp->right);
818 continue;
819 }
820 msp->restart = 0;
821 msp3400c_setvolume(client, msp->muted, 0, 0);
822 msp3400c_setmode(client, MSP_MODE_AM_DETECT /* +1 */ );
823 val1 = val2 = 0;
824 max1 = max2 = -1;
825 del_timer(&msp->wake_stereo);
826 msp->watch_stereo = 0;
827
828 /* carrier detect pass #1 -- main carrier */
829 cd = carrier_detect_main; count = CARRIER_COUNT(carrier_detect_main);
830
831 if (amsound && (msp->norm == VIDEO_MODE_SECAM)) {
832 /* autodetect doesn't work well with AM ... */
833 max1 = 3;
834 count = 0;
835 dprintk("msp3400: AM sound override\n");
836 }
837
838 for (this = 0; this < count; this++) {
839 msp3400c_setcarrier(client, cd[this].cdo,cd[this].cdo);
840
841 current->state = TASK_INTERRUPTIBLE;
842 schedule_timeout(HZ/10);
843 if (signal_pending(current))
844 goto done;
845 if (msp->restart)
846 msp->restart = 0;
847
848 val = msp3400c_read(client, I2C_MSP3400C_DFP, 0x1b);
849 if (val > 32767)
850 val -= 65536;
851 if (val1 < val)
852 val1 = val, max1 = this;
853 dprintk("msp3400: carrier1 val: %5d / %s\n", val,cd[this].name);
854 }
855
856 /* carrier detect pass #2 -- second (stereo) carrier */
857 switch (max1) {
858 case 1: /* 5.5 */
859 cd = carrier_detect_55; count = CARRIER_COUNT(carrier_detect_55);
860 break;
861 case 3: /* 6.5 */
862 cd = carrier_detect_65; count = CARRIER_COUNT(carrier_detect_65);
863 break;
864 case 0: /* 4.5 */
865 case 2: /* 6.0 */
866 default:
867 cd = NULL; count = 0;
868 break;
869 }
870
871 if (amsound && (msp->norm == VIDEO_MODE_SECAM)) {
872 /* autodetect doesn't work well with AM ... */
873 cd = NULL; count = 0; max2 = 0;
874 }
875 for (this = 0; this < count; this++) {
876 msp3400c_setcarrier(client, cd[this].cdo,cd[this].cdo);
877
878 current->state = TASK_INTERRUPTIBLE;
879 schedule_timeout(HZ/10);
880 if (signal_pending(current))
881 goto done;
882 if (msp->restart)
883 goto restart;
884
885 val = msp3400c_read(client, I2C_MSP3400C_DFP, 0x1b);
886 if (val > 32767)
887 val -= 65536;
888 if (val2 < val)
889 val2 = val, max2 = this;
890 dprintk("msp3400: carrier2 val: %5d / %s\n", val,cd[this].name);
891 }
892
893 /* programm the msp3400 according to the results */
894 msp->main = carrier_detect_main[max1].cdo;
895 switch (max1) {
896 case 1: /* 5.5 */
897 if (max2 == 0) {
898 /* B/G FM-stereo */
899 msp->second = carrier_detect_55[max2].cdo;
900 msp3400c_setmode(client, MSP_MODE_FM_TERRA);
901 msp->nicam_on = 0;
902 msp3400c_setstereo(client, VIDEO_SOUND_MONO);
903 msp->watch_stereo = 1;
904 } else if (max2 == 1 && HAVE_NICAM(msp)) {
905 /* B/G NICAM */
906 msp->second = carrier_detect_55[max2].cdo;
907 msp3400c_setmode(client, MSP_MODE_FM_NICAM1);
908 msp->nicam_on = 1;
909 msp3400c_setcarrier(client, msp->second, msp->main);
910 msp->watch_stereo = 1;
911 } else {
912 goto no_second;
913 }
914 break;
915 case 2: /* 6.0 */
916 /* PAL I NICAM */
917 msp->second = MSP_CARRIER(6.552);
918 msp3400c_setmode(client, MSP_MODE_FM_NICAM2);
919 msp->nicam_on = 1;
920 msp3400c_setcarrier(client, msp->second, msp->main);
921 msp->watch_stereo = 1;
922 break;
923 case 3: /* 6.5 */
924 if (max2 == 1 || max2 == 2) {
925 /* D/K FM-stereo */
926 msp->second = carrier_detect_65[max2].cdo;
927 msp3400c_setmode(client, MSP_MODE_FM_TERRA);
928 msp->nicam_on = 0;
929 msp3400c_setstereo(client, VIDEO_SOUND_MONO);
930 msp->watch_stereo = 1;
931 } else if (max2 == 0 &&
932 msp->norm == VIDEO_MODE_SECAM) {
933 /* L NICAM or AM-mono */
934 msp->second = carrier_detect_65[max2].cdo;
935 msp3400c_setmode(client, MSP_MODE_AM_NICAM);
936 msp->nicam_on = 0;
937 msp3400c_setstereo(client, VIDEO_SOUND_MONO);
938 msp3400c_setcarrier(client, msp->second, msp->main);
939 /* volume prescale for SCART (AM mono input) */
940 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000d, 0x1900);
941 msp->watch_stereo = 1;
942 } else if (max2 == 0 && HAVE_NICAM(msp)) {
943 /* D/K NICAM */
944 msp->second = carrier_detect_65[max2].cdo;
945 msp3400c_setmode(client, MSP_MODE_FM_NICAM1);
946 msp->nicam_on = 1;
947 msp3400c_setcarrier(client, msp->second, msp->main);
948 msp->watch_stereo = 1;
949 } else {
950 goto no_second;
951 }
952 break;
953 case 0: /* 4.5 */
954 default:
955 no_second:
956 msp->second = carrier_detect_main[max1].cdo;
957 msp3400c_setmode(client, MSP_MODE_FM_TERRA);
958 msp->nicam_on = 0;
959 msp3400c_setcarrier(client, msp->second, msp->main);
960 msp->stereo = VIDEO_SOUND_MONO;
961 msp3400c_setstereo(client, VIDEO_SOUND_MONO);
962 break;
963 }
964
965 /* unmute + restore dfp registers */
966 msp3400c_setvolume(client, msp->muted, msp->left, msp->right);
967 msp3400c_restore_dfp(client);
968
969 if (msp->watch_stereo)
970 mod_timer(&msp->wake_stereo, jiffies+5*HZ);
971
972 if (debug)
973 msp3400c_print_mode(msp);
974
975 msp->active = 0;
976 }
977
978 done:
979 dprintk(KERN_DEBUG "msp3400: thread: exit\n");
980 msp->active = 0;
981 msp->thread = NULL;
982
983 if(msp->notify != NULL)
984 up(msp->notify);
985 return 0;
986 }
987
988 /* ----------------------------------------------------------------------- */
989 /* this one uses the automatic sound standard detection of newer */
990 /* msp34xx chip versions */
991
992 static struct MODES {
993 int retval;
994 int main, second;
995 char *name;
996 } modelist[] = {
997 { 0x0000, 0, 0, "ERROR" },
998 { 0x0001, 0, 0, "autodetect start" },
999 { 0x0002, MSP_CARRIER(4.5), MSP_CARRIER(4.72), "4.5/4.72 M Dual FM-Stereo" },
1000 { 0x0003, MSP_CARRIER(5.5), MSP_CARRIER(5.7421875), "5.5/5.74 B/G Dual FM-Stereo" },
1001 { 0x0004, MSP_CARRIER(6.5), MSP_CARRIER(6.2578125), "6.5/6.25 D/K1 Dual FM-Stereo" },
1002 { 0x0005, MSP_CARRIER(6.5), MSP_CARRIER(6.7421875), "6.5/6.74 D/K2 Dual FM-Stereo" },
1003 { 0x0006, MSP_CARRIER(6.5), MSP_CARRIER(6.5), "6.5 D/K FM-Mono (HDEV3)" },
1004 { 0x0008, MSP_CARRIER(5.5), MSP_CARRIER(5.85), "5.5/5.85 B/G NICAM FM" },
1005 { 0x0009, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85 L NICAM AM" },
1006 { 0x000a, MSP_CARRIER(6.0), MSP_CARRIER(6.55), "6.0/6.55 I NICAM FM" },
1007 { 0x000b, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85 D/K NICAM FM" },
1008 { 0x000c, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85 D/K NICAM FM (HDEV2)" },
1009 { 0x0020, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5 M BTSC-Stereo" },
1010 { 0x0021, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5 M BTSC-Mono + SAP" },
1011 { 0x0030, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5 M EIA-J Japan Stereo" },
1012 { 0x0040, MSP_CARRIER(10.7), MSP_CARRIER(10.7), "10.7 FM-Stereo Radio" },
1013 { 0x0050, MSP_CARRIER(6.5), MSP_CARRIER(6.5), "6.5 SAT-Mono" },
1014 { 0x0051, MSP_CARRIER(7.02), MSP_CARRIER(7.20), "7.02/7.20 SAT-Stereo" },
1015 { 0x0060, MSP_CARRIER(7.2), MSP_CARRIER(7.2), "7.2 SAT ADR" },
1016 { -1, 0, 0, NULL }, /* EOF */
1017 };
1018
msp3410d_thread(void * data)1019 static int msp3410d_thread(void *data)
1020 {
1021 struct i2c_client *client = data;
1022 struct msp3400c *msp = i2c_get_clientdata(client);
1023 int mode,val,i,std;
1024
1025 lock_kernel();
1026 daemonize();
1027 sigfillset(¤t->blocked);
1028 strcpy(current->comm,"msp3410 [auto]");
1029 msp->thread = current;
1030 unlock_kernel();
1031
1032 printk("msp3410: daemon started\n");
1033 if(msp->notify != NULL)
1034 up(msp->notify);
1035
1036 for (;;) {
1037 if (msp->rmmod)
1038 goto done;
1039 if (debug > 1)
1040 printk(KERN_DEBUG "msp3410: thread: sleep\n");
1041 interruptible_sleep_on(&msp->wq);
1042 if (debug > 1)
1043 printk(KERN_DEBUG "msp3410: thread: wakeup\n");
1044 if (msp->rmmod || signal_pending(current))
1045 goto done;
1046
1047 msp->active = 1;
1048
1049 if (msp->watch_stereo) {
1050 watch_stereo(client);
1051 msp->active = 0;
1052 continue;
1053 }
1054
1055 /* some time for the tuner to sync */
1056 current->state = TASK_INTERRUPTIBLE;
1057 schedule_timeout(HZ/5);
1058 if (signal_pending(current))
1059 goto done;
1060
1061 restart:
1062 if (msp->mode == MSP_MODE_EXTERN) {
1063 /* no carrier scan needed, just unmute */
1064 dprintk(KERN_DEBUG "msp3410: thread: no carrier scan\n");
1065 msp3400c_setvolume(client, msp->muted,
1066 msp->left, msp->right);
1067 continue;
1068 }
1069 msp->restart = 0;
1070 del_timer(&msp->wake_stereo);
1071 msp->watch_stereo = 0;
1072
1073 /* put into sane state (and mute) */
1074 msp3400c_reset(client);
1075
1076 /* start autodetect */
1077 switch (msp->norm) {
1078 case VIDEO_MODE_PAL:
1079 mode = 0x1003;
1080 std = 1;
1081 break;
1082 case VIDEO_MODE_NTSC: /* BTSC */
1083 mode = 0x2003;
1084 std = 0x0020;
1085 break;
1086 case VIDEO_MODE_SECAM:
1087 mode = 0x0003;
1088 std = 1;
1089 break;
1090 case VIDEO_MODE_RADIO:
1091 mode = 0x0003;
1092 std = 0x0040;
1093 break;
1094 default:
1095 mode = 0x0003;
1096 std = 1;
1097 break;
1098 }
1099 msp3400c_write(client, I2C_MSP3400C_DEM, 0x30, mode);
1100 msp3400c_write(client, I2C_MSP3400C_DEM, 0x20, std);
1101
1102 if (debug) {
1103 int i;
1104 for (i = 0; modelist[i].name != NULL; i++)
1105 if (modelist[i].retval == std)
1106 break;
1107 printk(KERN_DEBUG "msp3410: setting mode: %s (0x%04x)\n",
1108 modelist[i].name ? modelist[i].name : "unknown",std);
1109 }
1110
1111 if (std != 1) {
1112 /* programmed some specific mode */
1113 val = std;
1114 } else {
1115 /* triggered autodetect */
1116 for (;;) {
1117 current->state = TASK_INTERRUPTIBLE;
1118 schedule_timeout(HZ/10);
1119 if (signal_pending(current))
1120 goto done;
1121 if (msp->restart)
1122 goto restart;
1123
1124 /* check results */
1125 val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x7e);
1126 if (val < 0x07ff)
1127 break;
1128 dprintk(KERN_DEBUG "msp3410: detection still in progress\n");
1129 }
1130 }
1131 for (i = 0; modelist[i].name != NULL; i++)
1132 if (modelist[i].retval == val)
1133 break;
1134 dprintk(KERN_DEBUG "msp3410: current mode: %s (0x%04x)\n",
1135 modelist[i].name ? modelist[i].name : "unknown",
1136 val);
1137 msp->main = modelist[i].main;
1138 msp->second = modelist[i].second;
1139
1140 if (amsound && (msp->norm == VIDEO_MODE_SECAM) && (val != 0x0009)) {
1141 /* autodetection has failed, let backup */
1142 dprintk(KERN_DEBUG "msp3410: autodetection failed,"
1143 " switching to backup mode: %s (0x%04x)\n",
1144 modelist[8].name ? modelist[8].name : "unknown",val);
1145 val = 0x0009;
1146 msp3400c_write(client, I2C_MSP3400C_DEM, 0x20, val);
1147 }
1148
1149 /* set various prescales */
1150 msp3400c_write(client, I2C_MSP3400C_DFP, 0x0d, 0x1900); /* scart */
1151 msp3400c_write(client, I2C_MSP3400C_DFP, 0x0e, 0x2403); /* FM */
1152 msp3400c_write(client, I2C_MSP3400C_DFP, 0x10, 0x5a00); /* nicam */
1153
1154 /* set stereo */
1155 switch (val) {
1156 case 0x0008: /* B/G NICAM */
1157 case 0x000a: /* I NICAM */
1158 if (val == 0x0008)
1159 msp->mode = MSP_MODE_FM_NICAM1;
1160 else
1161 msp->mode = MSP_MODE_FM_NICAM2;
1162 /* just turn on stereo */
1163 msp->stereo = VIDEO_SOUND_STEREO;
1164 msp->nicam_on = 1;
1165 msp->watch_stereo = 1;
1166 msp3400c_setstereo(client,VIDEO_SOUND_STEREO);
1167 break;
1168 case 0x0009:
1169 msp->mode = MSP_MODE_AM_NICAM;
1170 msp->stereo = VIDEO_SOUND_MONO;
1171 msp->nicam_on = 1;
1172 msp3400c_setstereo(client,VIDEO_SOUND_MONO);
1173 msp->watch_stereo = 1;
1174 break;
1175 case 0x0020: /* BTSC */
1176 /* just turn on stereo */
1177 msp->mode = MSP_MODE_BTSC;
1178 msp->stereo = VIDEO_SOUND_STEREO;
1179 msp->nicam_on = 0;
1180 msp->watch_stereo = 1;
1181 msp3400c_setstereo(client,VIDEO_SOUND_STEREO);
1182 break;
1183 case 0x0040: /* FM radio */
1184 msp->mode = MSP_MODE_FM_RADIO;
1185 msp->stereo = VIDEO_SOUND_STEREO;
1186 msp->nicam_on = 0;
1187 msp->watch_stereo = 0;
1188 /* not needed in theory if HAVE_RADIO(), but
1189 short programming enables carrier mute */
1190 msp3400c_setmode(client,MSP_MODE_FM_RADIO);
1191 msp3400c_setcarrier(client, MSP_CARRIER(10.7),
1192 MSP_CARRIER(10.7));
1193 /* scart routing */
1194 msp3400c_set_scart(client,SCART_IN2,0);
1195 #if 0
1196 /* radio from SCART_IN2 */
1197 msp3400c_write(client,I2C_MSP3400C_DFP, 0x08, 0x0220);
1198 msp3400c_write(client,I2C_MSP3400C_DFP, 0x09, 0x0220);
1199 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0b, 0x0220);
1200 #else
1201 /* msp34xx does radio decoding */
1202 msp3400c_write(client,I2C_MSP3400C_DFP, 0x08, 0x0020);
1203 msp3400c_write(client,I2C_MSP3400C_DFP, 0x09, 0x0020);
1204 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0b, 0x0020);
1205 #endif
1206 break;
1207 case 0x0003:
1208 msp->mode = MSP_MODE_FM_TERRA;
1209 msp->stereo = VIDEO_SOUND_MONO;
1210 msp->nicam_on = 0;
1211 msp->watch_stereo = 1;
1212 break;
1213 }
1214
1215 /* unmute + restore dfp registers */
1216 msp3400c_setbass(client, msp->bass);
1217 msp3400c_settreble(client, msp->treble);
1218 msp3400c_setvolume(client, msp->muted, msp->left, msp->right);
1219 msp3400c_restore_dfp(client);
1220
1221 if (msp->watch_stereo)
1222 mod_timer(&msp->wake_stereo, jiffies+HZ);
1223
1224 msp->active = 0;
1225 }
1226
1227 done:
1228 dprintk(KERN_DEBUG "msp3410: thread: exit\n");
1229 msp->active = 0;
1230 msp->thread = NULL;
1231
1232 if(msp->notify != NULL)
1233 up(msp->notify);
1234 return 0;
1235 }
1236
1237 /* ----------------------------------------------------------------------- */
1238
1239 static int msp_attach(struct i2c_adapter *adap, int addr,
1240 unsigned short flags, int kind);
1241 static int msp_detach(struct i2c_client *client);
1242 static int msp_probe(struct i2c_adapter *adap);
1243 static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg);
1244
1245 static struct i2c_driver driver = {
1246 .name = "i2c msp3400 driver",
1247 .id = I2C_DRIVERID_MSP3400,
1248 .flags = I2C_DF_NOTIFY,
1249 .attach_adapter = msp_probe,
1250 .detach_client = msp_detach,
1251 .command = msp_command,
1252 };
1253
1254 static struct i2c_client client_template =
1255 {
1256 I2C_DEVNAME("(unset)"),
1257 .flags = I2C_CLIENT_ALLOW_USE,
1258 .driver = &driver,
1259 };
1260
msp_attach(struct i2c_adapter * adap,int addr,unsigned short flags,int kind)1261 static int msp_attach(struct i2c_adapter *adap, int addr,
1262 unsigned short flags, int kind)
1263 {
1264 DECLARE_MUTEX_LOCKED(sem);
1265 struct msp3400c *msp;
1266 struct i2c_client *c;
1267 int i, rc;
1268
1269 client_template.adapter = adap;
1270 client_template.addr = addr;
1271
1272 if (-1 == msp3400c_reset(&client_template)) {
1273 dprintk("msp3400: no chip found\n");
1274 return -1;
1275 }
1276
1277 if (NULL == (c = kmalloc(sizeof(struct i2c_client),GFP_KERNEL)))
1278 return -ENOMEM;
1279 memcpy(c,&client_template,sizeof(struct i2c_client));
1280 if (NULL == (msp = kmalloc(sizeof(struct msp3400c),GFP_KERNEL))) {
1281 kfree(c);
1282 return -ENOMEM;
1283 }
1284
1285 memset(msp,0,sizeof(struct msp3400c));
1286 msp->left = 65535;
1287 msp->right = 65535;
1288 msp->bass = 32768;
1289 msp->treble = 32768;
1290 msp->input = -1;
1291 msp->muted = 1;
1292 for (i = 0; i < DFP_COUNT; i++)
1293 msp->dfp_regs[i] = -1;
1294
1295 i2c_set_clientdata(c, msp);
1296 init_waitqueue_head(&msp->wq);
1297
1298 if (-1 == msp3400c_reset(c)) {
1299 kfree(msp);
1300 kfree(c);
1301 dprintk("msp3400: no chip found\n");
1302 return -1;
1303 }
1304
1305 msp->rev1 = msp3400c_read(c, I2C_MSP3400C_DFP, 0x1e);
1306 if (-1 != msp->rev1)
1307 msp->rev2 = msp3400c_read(c, I2C_MSP3400C_DFP, 0x1f);
1308 if ((-1 == msp->rev1) || (0 == msp->rev1 && 0 == msp->rev2)) {
1309 kfree(msp);
1310 kfree(c);
1311 printk("msp3400: error while reading chip version\n");
1312 return -1;
1313 }
1314
1315 #if 0
1316 /* this will turn on a 1kHz beep - might be useful for debugging... */
1317 msp3400c_write(c,I2C_MSP3400C_DFP, 0x0014, 0x1040);
1318 #endif
1319 msp3400c_setvolume(c,msp->muted,msp->left,msp->right);
1320
1321 sprintf(c->name,"MSP34%02d%c-%c%d",
1322 (msp->rev2>>8)&0xff, (msp->rev1&0xff)+'@',
1323 ((msp->rev1>>8)&0xff)+'@', msp->rev2&0x1f);
1324
1325 if (simple == -1) {
1326 /* default mode */
1327 msp->simple = HAVE_SIMPLE(msp);
1328 } else {
1329 /* use insmod option */
1330 msp->simple = simple;
1331 }
1332
1333 /* timer for stereo checking */
1334 init_timer(&msp->wake_stereo);
1335 msp->wake_stereo.function = msp3400c_stereo_wake;
1336 msp->wake_stereo.data = (unsigned long)msp;
1337
1338 /* hello world :-) */
1339 printk(KERN_INFO "msp34xx: init: chip=%s",i2c_clientname(c));
1340 if (HAVE_NICAM(msp))
1341 printk(" +nicam");
1342 if (HAVE_SIMPLE(msp))
1343 printk(" +simple");
1344 if (HAVE_RADIO(msp))
1345 printk(" +radio");
1346 printk("\n");
1347
1348 /* startup control thread */
1349 MOD_INC_USE_COUNT;
1350 msp->notify = &sem;
1351 rc = kernel_thread(msp->simple ? msp3410d_thread : msp3400c_thread,
1352 (void *)c, 0);
1353 if (rc < 0)
1354 printk(KERN_WARNING "msp34xx: kernel_thread() failed\n");
1355 else
1356 down(&sem);
1357 msp->notify = NULL;
1358 wake_up_interruptible(&msp->wq);
1359
1360 /* update our own array */
1361 for (i = 0; i < MSP3400_MAX; i++) {
1362 if (NULL == msps[i]) {
1363 msps[i] = c;
1364 break;
1365 }
1366 }
1367
1368 /* done */
1369 i2c_attach_client(c);
1370 return 0;
1371 }
1372
msp_detach(struct i2c_client * client)1373 static int msp_detach(struct i2c_client *client)
1374 {
1375 DECLARE_MUTEX_LOCKED(sem);
1376 struct msp3400c *msp = i2c_get_clientdata(client);
1377 int i;
1378
1379 /* shutdown control thread */
1380 del_timer(&msp->wake_stereo);
1381 if (msp->thread)
1382 {
1383 msp->notify = &sem;
1384 msp->rmmod = 1;
1385 wake_up_interruptible(&msp->wq);
1386 down(&sem);
1387 msp->notify = NULL;
1388 }
1389 msp3400c_reset(client);
1390
1391 /* update our own array */
1392 for (i = 0; i < MSP3400_MAX; i++) {
1393 if (client == msps[i]) {
1394 msps[i] = NULL;
1395 break;
1396 }
1397 }
1398
1399 i2c_detach_client(client);
1400 kfree(msp);
1401 kfree(client);
1402 MOD_DEC_USE_COUNT;
1403 return 0;
1404 }
1405
msp_probe(struct i2c_adapter * adap)1406 static int msp_probe(struct i2c_adapter *adap)
1407 {
1408 #ifdef I2C_ADAP_CLASS_TV_ANALOG
1409 if (adap->class & I2C_ADAP_CLASS_TV_ANALOG)
1410 return i2c_probe(adap, &addr_data, msp_attach);
1411 #else
1412 if (adap->id == (I2C_ALGO_BIT | I2C_HW_B_BT848))
1413 return i2c_probe(adap, &addr_data, msp_attach);
1414 #endif
1415 return 0;
1416 }
1417
msp_wake_thread(struct i2c_client * client)1418 static void msp_wake_thread(struct i2c_client *client)
1419 {
1420 struct msp3400c *msp = i2c_get_clientdata(client);
1421
1422 msp3400c_setvolume(client,msp->muted,0,0);
1423 msp->watch_stereo=0;
1424 del_timer(&msp->wake_stereo);
1425 if (msp->active)
1426 msp->restart = 1;
1427 wake_up_interruptible(&msp->wq);
1428 }
1429
msp_command(struct i2c_client * client,unsigned int cmd,void * arg)1430 static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg)
1431 {
1432 struct msp3400c *msp = i2c_get_clientdata(client);
1433 __u16 *sarg = arg;
1434 #if 0
1435 int *iarg = (int*)arg;
1436 #endif
1437
1438 switch (cmd) {
1439
1440 case AUDC_SET_INPUT:
1441 /* scart switching
1442 - IN1 is often used for external input
1443 - Hauppauge uses IN2 for the radio */
1444 dprintk(KERN_DEBUG "msp34xx: AUDC_SET_INPUT(%d)\n",*sarg);
1445 if (*sarg == msp->input)
1446 break;
1447 msp->input = *sarg;
1448 switch (*sarg) {
1449 case AUDIO_RADIO:
1450 msp->mode = MSP_MODE_FM_RADIO;
1451 msp->stereo = VIDEO_SOUND_STEREO;
1452 msp3400c_set_scart(client,SCART_IN2,0);
1453 msp3400c_write(client,I2C_MSP3400C_DFP,0x000d,0x1900);
1454 msp3400c_setstereo(client,msp->stereo);
1455 break;
1456 case AUDIO_EXTERN:
1457 msp->mode = MSP_MODE_EXTERN;
1458 msp->stereo = VIDEO_SOUND_STEREO;
1459 msp3400c_set_scart(client,SCART_IN1,0);
1460 msp3400c_write(client,I2C_MSP3400C_DFP,0x000d,0x1900);
1461 msp3400c_setstereo(client,msp->stereo);
1462 break;
1463 case AUDIO_TUNER:
1464 msp->mode = -1;
1465 msp_wake_thread(client);
1466 break;
1467 default:
1468 if (*sarg & AUDIO_MUTE)
1469 msp3400c_set_scart(client,SCART_MUTE,0);
1470 break;
1471 }
1472 if (msp->active)
1473 msp->restart = 1;
1474 break;
1475
1476 case AUDC_SET_RADIO:
1477 dprintk(KERN_DEBUG "msp34xx: AUDC_SET_RADIO\n");
1478 msp->norm = VIDEO_MODE_RADIO;
1479 msp->watch_stereo=0;
1480 del_timer(&msp->wake_stereo);
1481 dprintk(KERN_DEBUG "msp34xx: switching to radio mode\n");
1482 if (msp->simple) {
1483 /* the thread will do for us */
1484 msp_wake_thread(client);
1485 } else {
1486 /* set msp3400 to FM radio mode */
1487 msp3400c_setmode(client,MSP_MODE_FM_RADIO);
1488 msp3400c_setcarrier(client, MSP_CARRIER(10.7),MSP_CARRIER(10.7));
1489 msp3400c_setvolume(client,msp->muted,msp->left,msp->right);
1490 }
1491 if (msp->active)
1492 msp->restart = 1;
1493 break;
1494
1495 #if 1
1496 /* work-in-progress: hook to control the DFP registers */
1497 case MSP_SET_DFPREG:
1498 {
1499 struct msp_dfpreg *r = arg;
1500 unsigned int i;
1501
1502 if (r->reg < 0 || r->reg >= DFP_COUNT)
1503 return -EINVAL;
1504 for (i = 0; i < ARRAY_SIZE(bl_dfp); i++)
1505 if (r->reg == bl_dfp[i])
1506 return -EINVAL;
1507 msp->dfp_regs[r->reg] = r->value;
1508 msp3400c_write(client,I2C_MSP3400C_DFP,r->reg,r->value);
1509 return 0;
1510 }
1511 case MSP_GET_DFPREG:
1512 {
1513 struct msp_dfpreg *r = arg;
1514
1515 if (r->reg < 0 || r->reg >= DFP_COUNT)
1516 return -EINVAL;
1517 r->value = msp3400c_read(client,I2C_MSP3400C_DFP,r->reg);
1518 return 0;
1519 }
1520 #endif
1521
1522 /* --- v4l ioctls --- */
1523 /* take care: bttv does userspace copying, we'll get a
1524 kernel pointer here... */
1525 case VIDIOCGAUDIO:
1526 {
1527 struct video_audio *va = arg;
1528
1529 dprintk(KERN_DEBUG "msp34xx: VIDIOCGAUDIO\n");
1530 va->flags |= VIDEO_AUDIO_VOLUME |
1531 VIDEO_AUDIO_BASS |
1532 VIDEO_AUDIO_TREBLE |
1533 VIDEO_AUDIO_MUTABLE;
1534 if (msp->muted)
1535 va->flags |= VIDEO_AUDIO_MUTE;
1536 va->volume=max(msp->left,msp->right);
1537 va->balance=(32768*min(msp->left,msp->right))/
1538 (va->volume ? va->volume : 1);
1539 va->balance=(msp->left<msp->right)?
1540 (65535-va->balance) : va->balance;
1541 if (0 == va->volume)
1542 va->balance = 32768;
1543 va->bass = msp->bass;
1544 va->treble = msp->treble;
1545
1546 if (msp->norm != VIDEO_MODE_RADIO) {
1547 autodetect_stereo(client);
1548 va->mode = msp->stereo;
1549 }
1550 break;
1551 }
1552 case VIDIOCSAUDIO:
1553 {
1554 struct video_audio *va = arg;
1555
1556 dprintk(KERN_DEBUG "msp34xx: VIDIOCSAUDIO\n");
1557 msp->muted = (va->flags & VIDEO_AUDIO_MUTE);
1558 msp->left = (min(65536 - va->balance,32768) *
1559 va->volume) / 32768;
1560 msp->right = (min(va->balance,(__u16)32768) *
1561 va->volume) / 32768;
1562 msp->bass = va->bass;
1563 msp->treble = va->treble;
1564 msp3400c_setvolume(client,msp->muted,msp->left,msp->right);
1565 msp3400c_setbass(client,msp->bass);
1566 msp3400c_settreble(client,msp->treble);
1567
1568 if (va->mode != 0 && msp->norm != VIDEO_MODE_RADIO) {
1569 msp->watch_stereo=0;
1570 del_timer(&msp->wake_stereo);
1571 msp->stereo = va->mode & 0x0f;
1572 msp3400c_setstereo(client,va->mode & 0x0f);
1573 }
1574 break;
1575 }
1576 case VIDIOCSCHAN:
1577 {
1578 struct video_channel *vc = arg;
1579
1580 dprintk(KERN_DEBUG "msp34xx: VIDIOCSCHAN\n");
1581 msp->norm = vc->norm;
1582 break;
1583 }
1584 case VIDIOCSFREQ:
1585 {
1586 /* new channel -- kick audio carrier scan */
1587 dprintk(KERN_DEBUG "msp34xx: VIDIOCSFREQ\n");
1588 msp_wake_thread(client);
1589 break;
1590 }
1591
1592 default:
1593 /* nothing */
1594 break;
1595 }
1596 return 0;
1597 }
1598
1599 /* ----------------------------------------------------------------------- */
1600
msp3400_init_module(void)1601 static int msp3400_init_module(void)
1602 {
1603 i2c_add_driver(&driver);
1604 return 0;
1605 }
1606
msp3400_cleanup_module(void)1607 static void msp3400_cleanup_module(void)
1608 {
1609 i2c_del_driver(&driver);
1610 }
1611
1612 module_init(msp3400_init_module);
1613 module_exit(msp3400_cleanup_module);
1614
1615 /*
1616 * Overrides for Emacs so that we follow Linus's tabbing style.
1617 * ---------------------------------------------------------------------------
1618 * Local variables:
1619 * c-basic-offset: 8
1620 * End:
1621 */
1622