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