]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/media/video/msp3400.c
[PATCH] v4l: 721: check kthread correctly
[mirror_ubuntu-artful-kernel.git] / drivers / media / video / msp3400.c
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/moduleparam.h>
40 #include <linux/kernel.h>
41 #include <linux/sched.h>
42 #include <linux/string.h>
43 #include <linux/timer.h>
44 #include <linux/delay.h>
45 #include <linux/errno.h>
46 #include <linux/slab.h>
47 #include <linux/i2c.h>
48 #include <linux/videodev.h>
49 #include <linux/init.h>
50 #include <linux/smp_lock.h>
51 #include <linux/kthread.h>
52 #include <linux/suspend.h>
53 #include <asm/semaphore.h>
54 #include <asm/pgtable.h>
55
56 #include <media/audiochip.h>
57 #include <media/id.h>
58 #include "msp3400.h"
59
60 #define OPMODE_AUTO -1
61 #define OPMODE_MANUAL 0
62 #define OPMODE_SIMPLE 1 /* use short programming (>= msp3410 only) */
63 #define OPMODE_SIMPLER 2 /* use shorter programming (>= msp34xxG) */
64
65 /* insmod parameters */
66 static int opmode = OPMODE_AUTO;
67 static int debug = 0; /* debug output */
68 static int once = 0; /* no continous stereo monitoring */
69 static int amsound = 0; /* hard-wire AM sound at 6.5 Hz (france),
70 the autoscan seems work well only with FM... */
71 static int standard = 1; /* Override auto detect of audio standard, if needed. */
72 static int dolby = 0;
73
74 static int stereo_threshold = 0x190; /* a2 threshold for stereo/bilingual
75 (msp34xxg only) 0x00a0-0x03c0 */
76
77 struct msp3400c {
78 int rev1,rev2;
79
80 int opmode;
81 int mode;
82 int norm;
83 int nicam_on;
84 int acb;
85 int main, second; /* sound carrier */
86 int input;
87 int source; /* see msp34xxg_set_source */
88
89 /* v4l2 */
90 int audmode;
91 int rxsubchans;
92
93 int muted;
94 int volume, balance;
95 int bass, treble;
96
97 /* thread */
98 struct task_struct *kthread;
99 wait_queue_head_t wq;
100 int restart:1;
101 int watch_stereo:1;
102 };
103
104 #define HAVE_NICAM(msp) (((msp->rev2>>8) & 0xff) != 00)
105 #define HAVE_SIMPLE(msp) ((msp->rev1 & 0xff) >= 'D'-'@')
106 #define HAVE_SIMPLER(msp) ((msp->rev1 & 0xff) >= 'G'-'@')
107 #define HAVE_RADIO(msp) ((msp->rev1 & 0xff) >= 'G'-'@')
108
109 #define VIDEO_MODE_RADIO 16 /* norm magic for radio mode */
110
111 /* ---------------------------------------------------------------------- */
112
113 #define dprintk if (debug >= 1) printk
114 #define d2printk if (debug >= 2) printk
115
116 /* read-only */
117 module_param(opmode, int, 0444);
118
119 /* read-write */
120 module_param(once, int, 0644);
121 module_param(debug, int, 0644);
122 module_param(stereo_threshold, int, 0644);
123 module_param(standard, int, 0644);
124 module_param(amsound, int, 0644);
125 module_param(dolby, int, 0644);
126
127 MODULE_PARM_DESC(opmode, "Forces a MSP3400 opmode. 0=Manual, 1=Simple, 2=Simpler");
128 MODULE_PARM_DESC(once, "No continuous stereo monitoring");
129 MODULE_PARM_DESC(debug, "Enable debug messages");
130 MODULE_PARM_DESC(stereo_threshold, "Sets signal threshold to activate stereo");
131 MODULE_PARM_DESC(standard, "Specify audio standard: 32 = NTSC, 64 = radio, Default: Autodetect");
132 MODULE_PARM_DESC(amsound, "Hardwire AM sound at 6.5Hz (France), FM can autoscan");
133 MODULE_PARM_DESC(dolby, "Activates Dolby processsing");
134
135
136 MODULE_DESCRIPTION("device driver for msp34xx TV sound processor");
137 MODULE_AUTHOR("Gerd Knorr");
138 MODULE_LICENSE("Dual BSD/GPL"); /* FreeBSD uses this too */
139
140 /* ---------------------------------------------------------------------- */
141
142 #define I2C_MSP3400C 0x80
143 #define I2C_MSP3400C_ALT 0x88
144
145 #define I2C_MSP3400C_DEM 0x10
146 #define I2C_MSP3400C_DFP 0x12
147
148 /* Addresses to scan */
149 static unsigned short normal_i2c[] = {
150 I2C_MSP3400C >> 1,
151 I2C_MSP3400C_ALT >> 1,
152 I2C_CLIENT_END
153 };
154 I2C_CLIENT_INSMOD;
155
156 /* ----------------------------------------------------------------------- */
157 /* functions for talking to the MSP3400C Sound processor */
158
159 static int msp3400c_reset(struct i2c_client *client)
160 {
161 /* reset and read revision code */
162 static char reset_off[3] = { 0x00, 0x80, 0x00 };
163 static char reset_on[3] = { 0x00, 0x00, 0x00 };
164 static char write[3] = { I2C_MSP3400C_DFP + 1, 0x00, 0x1e };
165 char read[2];
166 struct i2c_msg reset[2] = {
167 { client->addr, I2C_M_IGNORE_NAK, 3, reset_off },
168 { client->addr, I2C_M_IGNORE_NAK, 3, reset_on },
169 };
170 struct i2c_msg test[2] = {
171 { client->addr, 0, 3, write },
172 { client->addr, I2C_M_RD, 2, read },
173 };
174
175 if ( (1 != i2c_transfer(client->adapter,&reset[0],1)) ||
176 (1 != i2c_transfer(client->adapter,&reset[1],1)) ||
177 (2 != i2c_transfer(client->adapter,test,2)) ) {
178 printk(KERN_ERR "msp3400: chip reset failed\n");
179 return -1;
180 }
181 return 0;
182 }
183
184 static int
185 msp3400c_read(struct i2c_client *client, int dev, int addr)
186 {
187 int err;
188
189 unsigned char write[3];
190 unsigned char read[2];
191 struct i2c_msg msgs[2] = {
192 { client->addr, 0, 3, write },
193 { client->addr, I2C_M_RD, 2, read }
194 };
195 write[0] = dev+1;
196 write[1] = addr >> 8;
197 write[2] = addr & 0xff;
198
199 for (err = 0; err < 3;) {
200 if (2 == i2c_transfer(client->adapter,msgs,2))
201 break;
202 err++;
203 printk(KERN_WARNING "msp34xx: I/O error #%d (read 0x%02x/0x%02x)\n",
204 err, dev, addr);
205 msleep(10);
206 }
207 if (3 == err) {
208 printk(KERN_WARNING "msp34xx: giving up, reseting chip. Sound will go off, sorry folks :-|\n");
209 msp3400c_reset(client);
210 return -1;
211 }
212 return read[0] << 8 | read[1];
213 }
214
215 static int
216 msp3400c_write(struct i2c_client *client, int dev, int addr, int val)
217 {
218 int err;
219 unsigned char buffer[5];
220
221 buffer[0] = dev;
222 buffer[1] = addr >> 8;
223 buffer[2] = addr & 0xff;
224 buffer[3] = val >> 8;
225 buffer[4] = val & 0xff;
226
227 for (err = 0; err < 3;) {
228 if (5 == i2c_master_send(client, buffer, 5))
229 break;
230 err++;
231 printk(KERN_WARNING "msp34xx: I/O error #%d (write 0x%02x/0x%02x)\n",
232 err, dev, addr);
233 msleep(10);
234 }
235 if (3 == err) {
236 printk(KERN_WARNING "msp34xx: giving up, reseting chip. Sound will go off, sorry folks :-|\n");
237 msp3400c_reset(client);
238 return -1;
239 }
240 return 0;
241 }
242
243 /* ------------------------------------------------------------------------ */
244
245 /* This macro is allowed for *constants* only, gcc must calculate it
246 at compile time. Remember -- no floats in kernel mode */
247 #define MSP_CARRIER(freq) ((int)((float)(freq/18.432)*(1<<24)))
248
249 #define MSP_MODE_AM_DETECT 0
250 #define MSP_MODE_FM_RADIO 2
251 #define MSP_MODE_FM_TERRA 3
252 #define MSP_MODE_FM_SAT 4
253 #define MSP_MODE_FM_NICAM1 5
254 #define MSP_MODE_FM_NICAM2 6
255 #define MSP_MODE_AM_NICAM 7
256 #define MSP_MODE_BTSC 8
257 #define MSP_MODE_EXTERN 9
258
259 static struct MSP_INIT_DATA_DEM {
260 int fir1[6];
261 int fir2[6];
262 int cdo1;
263 int cdo2;
264 int ad_cv;
265 int mode_reg;
266 int dfp_src;
267 int dfp_matrix;
268 } msp_init_data[] = {
269 /* AM (for carrier detect / msp3400) */
270 { { 75, 19, 36, 35, 39, 40 }, { 75, 19, 36, 35, 39, 40 },
271 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
272 0x00d0, 0x0500, 0x0020, 0x3000},
273
274 /* AM (for carrier detect / msp3410) */
275 { { -1, -1, -8, 2, 59, 126 }, { -1, -1, -8, 2, 59, 126 },
276 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
277 0x00d0, 0x0100, 0x0020, 0x3000},
278
279 /* FM Radio */
280 { { -8, -8, 4, 6, 78, 107 }, { -8, -8, 4, 6, 78, 107 },
281 MSP_CARRIER(10.7), MSP_CARRIER(10.7),
282 0x00d0, 0x0480, 0x0020, 0x3000 },
283
284 /* Terrestial FM-mono + FM-stereo */
285 { { 3, 18, 27, 48, 66, 72 }, { 3, 18, 27, 48, 66, 72 },
286 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
287 0x00d0, 0x0480, 0x0030, 0x3000},
288
289 /* Sat FM-mono */
290 { { 1, 9, 14, 24, 33, 37 }, { 3, 18, 27, 48, 66, 72 },
291 MSP_CARRIER(6.5), MSP_CARRIER(6.5),
292 0x00c6, 0x0480, 0x0000, 0x3000},
293
294 /* NICAM/FM -- B/G (5.5/5.85), D/K (6.5/5.85) */
295 { { -2, -8, -10, 10, 50, 86 }, { 3, 18, 27, 48, 66, 72 },
296 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
297 0x00d0, 0x0040, 0x0120, 0x3000},
298
299 /* NICAM/FM -- I (6.0/6.552) */
300 { { 2, 4, -6, -4, 40, 94 }, { 3, 18, 27, 48, 66, 72 },
301 MSP_CARRIER(6.0), MSP_CARRIER(6.0),
302 0x00d0, 0x0040, 0x0120, 0x3000},
303
304 /* NICAM/AM -- L (6.5/5.85) */
305 { { -2, -8, -10, 10, 50, 86 }, { -4, -12, -9, 23, 79, 126 },
306 MSP_CARRIER(6.5), MSP_CARRIER(6.5),
307 0x00c6, 0x0140, 0x0120, 0x7c03},
308 };
309
310 struct CARRIER_DETECT {
311 int cdo;
312 char *name;
313 };
314
315 static struct CARRIER_DETECT carrier_detect_main[] = {
316 /* main carrier */
317 { MSP_CARRIER(4.5), "4.5 NTSC" },
318 { MSP_CARRIER(5.5), "5.5 PAL B/G" },
319 { MSP_CARRIER(6.0), "6.0 PAL I" },
320 { MSP_CARRIER(6.5), "6.5 PAL D/K + SAT + SECAM" }
321 };
322
323 static struct CARRIER_DETECT carrier_detect_55[] = {
324 /* PAL B/G */
325 { MSP_CARRIER(5.7421875), "5.742 PAL B/G FM-stereo" },
326 { MSP_CARRIER(5.85), "5.85 PAL B/G NICAM" }
327 };
328
329 static struct CARRIER_DETECT carrier_detect_65[] = {
330 /* PAL SAT / SECAM */
331 { MSP_CARRIER(5.85), "5.85 PAL D/K + SECAM NICAM" },
332 { MSP_CARRIER(6.2578125), "6.25 PAL D/K1 FM-stereo" },
333 { MSP_CARRIER(6.7421875), "6.74 PAL D/K2 FM-stereo" },
334 { MSP_CARRIER(7.02), "7.02 PAL SAT FM-stereo s/b" },
335 { MSP_CARRIER(7.20), "7.20 PAL SAT FM-stereo s" },
336 { MSP_CARRIER(7.38), "7.38 PAL SAT FM-stereo b" },
337 };
338
339 #define CARRIER_COUNT(x) (sizeof(x)/sizeof(struct CARRIER_DETECT))
340
341 /* ----------------------------------------------------------------------- */
342
343 static int scarts[3][9] = {
344 /* MASK IN1 IN2 IN1_DA IN2_DA IN3 IN4 MONO MUTE */
345 { 0x0320, 0x0000, 0x0200, -1, -1, 0x0300, 0x0020, 0x0100, 0x0320 },
346 { 0x0c40, 0x0440, 0x0400, 0x0c00, 0x0040, 0x0000, 0x0840, 0x0800, 0x0c40 },
347 { 0x3080, 0x1000, 0x1080, 0x0000, 0x0080, 0x2080, 0x3080, 0x2000, 0x3000 },
348 };
349
350 static char *scart_names[] = {
351 "mask", "in1", "in2", "in1 da", "in2 da", "in3", "in4", "mono", "mute"
352 };
353
354 static void
355 msp3400c_set_scart(struct i2c_client *client, int in, int out)
356 {
357 struct msp3400c *msp = i2c_get_clientdata(client);
358
359 if (-1 == scarts[out][in])
360 return;
361
362 dprintk(KERN_DEBUG
363 "msp34xx: scart switch: %s => %d\n",scart_names[in],out);
364 msp->acb &= ~scarts[out][SCART_MASK];
365 msp->acb |= scarts[out][in];
366 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0013, msp->acb);
367 }
368
369 /* ------------------------------------------------------------------------ */
370
371 static void msp3400c_setcarrier(struct i2c_client *client, int cdo1, int cdo2)
372 {
373 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0093, cdo1 & 0xfff);
374 msp3400c_write(client,I2C_MSP3400C_DEM, 0x009b, cdo1 >> 12);
375 msp3400c_write(client,I2C_MSP3400C_DEM, 0x00a3, cdo2 & 0xfff);
376 msp3400c_write(client,I2C_MSP3400C_DEM, 0x00ab, cdo2 >> 12);
377 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0056, 0); /*LOAD_REG_1/2*/
378 }
379
380 static void msp3400c_setvolume(struct i2c_client *client,
381 int muted, int volume, int balance)
382 {
383 int val = 0, bal = 0;
384
385 muted=0;
386 if (!muted) {
387 /* 0x7f instead if 0x73 here has sound quality issues,
388 * probably due to overmodulation + clipping ... */
389 val = (volume * 0x73 / 65535) << 8;
390 }
391 if (val) {
392 bal = (balance / 256) - 128;
393 }
394 dprintk(KERN_DEBUG
395 "msp34xx: setvolume: mute=%s %d:%d v=0x%02x b=0x%02x\n",
396 muted ? "on" : "off", volume, balance, val>>8, bal);
397 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0000, val); /* loudspeaker */
398 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0006, val); /* headphones */
399 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0007,
400 muted ? 0x01 : (val | 0x01));
401 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0001, bal << 8);
402 }
403
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
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
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->audmode = V4L2_TUNER_MODE_MONO;
428 msp->rxsubchans = V4L2_TUNER_SUB_MONO;
429
430 msp3400c_write(client,I2C_MSP3400C_DEM, 0x00bb, /* ad_cv */
431 msp_init_data[type].ad_cv);
432
433 for (i = 5; i >= 0; i--) /* fir 1 */
434 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0001,
435 msp_init_data[type].fir1[i]);
436
437 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005, 0x0004); /* fir 2 */
438 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005, 0x0040);
439 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005, 0x0000);
440 for (i = 5; i >= 0; i--)
441 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005,
442 msp_init_data[type].fir2[i]);
443
444 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0083, /* MODE_REG */
445 msp_init_data[type].mode_reg);
446
447 msp3400c_setcarrier(client, msp_init_data[type].cdo1,
448 msp_init_data[type].cdo2);
449
450 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0056, 0); /*LOAD_REG_1/2*/
451
452 if (dolby) {
453 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008,
454 0x0520); /* I2S1 */
455 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009,
456 0x0620); /* I2S2 */
457 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b,
458 msp_init_data[type].dfp_src);
459 } else {
460 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008,
461 msp_init_data[type].dfp_src);
462 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009,
463 msp_init_data[type].dfp_src);
464 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b,
465 msp_init_data[type].dfp_src);
466 }
467 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000a,
468 msp_init_data[type].dfp_src);
469 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000e,
470 msp_init_data[type].dfp_matrix);
471
472 if (HAVE_NICAM(msp)) {
473 /* nicam prescale */
474 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0010, 0x5a00); /* was: 0x3000 */
475 }
476 }
477
478 static int best_audio_mode(int rxsubchans)
479 {
480 if (rxsubchans & V4L2_TUNER_SUB_STEREO)
481 return V4L2_TUNER_MODE_STEREO;
482 if (rxsubchans & V4L2_TUNER_SUB_LANG1)
483 return V4L2_TUNER_MODE_LANG1;
484 if (rxsubchans & V4L2_TUNER_SUB_LANG2)
485 return V4L2_TUNER_MODE_LANG2;
486 return V4L2_TUNER_MODE_MONO;
487 }
488
489 /* turn on/off nicam + stereo */
490 static void msp3400c_set_audmode(struct i2c_client *client, int audmode)
491 {
492 static char *strmode[16] = {
493 #if __GNUC__ >= 3
494 [ 0 ... 15 ] = "invalid",
495 #endif
496 [ V4L2_TUNER_MODE_MONO ] = "mono",
497 [ V4L2_TUNER_MODE_STEREO ] = "stereo",
498 [ V4L2_TUNER_MODE_LANG1 ] = "lang1",
499 [ V4L2_TUNER_MODE_LANG2 ] = "lang2",
500 };
501 struct msp3400c *msp = i2c_get_clientdata(client);
502 int nicam=0; /* channel source: FM/AM or nicam */
503 int src=0;
504
505 BUG_ON(msp->opmode == OPMODE_SIMPLER);
506 msp->audmode = audmode;
507
508 /* switch demodulator */
509 switch (msp->mode) {
510 case MSP_MODE_FM_TERRA:
511 dprintk(KERN_DEBUG "msp3400: FM setstereo: %s\n",
512 strmode[audmode]);
513 msp3400c_setcarrier(client,msp->second,msp->main);
514 switch (audmode) {
515 case V4L2_TUNER_MODE_STEREO:
516 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000e, 0x3001);
517 break;
518 case V4L2_TUNER_MODE_MONO:
519 case V4L2_TUNER_MODE_LANG1:
520 case V4L2_TUNER_MODE_LANG2:
521 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000e, 0x3000);
522 break;
523 }
524 break;
525 case MSP_MODE_FM_SAT:
526 dprintk(KERN_DEBUG "msp3400: SAT setstereo: %s\n",
527 strmode[audmode]);
528 switch (audmode) {
529 case V4L2_TUNER_MODE_MONO:
530 msp3400c_setcarrier(client, MSP_CARRIER(6.5), MSP_CARRIER(6.5));
531 break;
532 case V4L2_TUNER_MODE_STEREO:
533 msp3400c_setcarrier(client, MSP_CARRIER(7.2), MSP_CARRIER(7.02));
534 break;
535 case V4L2_TUNER_MODE_LANG1:
536 msp3400c_setcarrier(client, MSP_CARRIER(7.38), MSP_CARRIER(7.02));
537 break;
538 case V4L2_TUNER_MODE_LANG2:
539 msp3400c_setcarrier(client, MSP_CARRIER(7.38), MSP_CARRIER(7.02));
540 break;
541 }
542 break;
543 case MSP_MODE_FM_NICAM1:
544 case MSP_MODE_FM_NICAM2:
545 case MSP_MODE_AM_NICAM:
546 dprintk(KERN_DEBUG "msp3400: NICAM setstereo: %s\n",
547 strmode[audmode]);
548 msp3400c_setcarrier(client,msp->second,msp->main);
549 if (msp->nicam_on)
550 nicam=0x0100;
551 break;
552 case MSP_MODE_BTSC:
553 dprintk(KERN_DEBUG "msp3400: BTSC setstereo: %s\n",
554 strmode[audmode]);
555 nicam=0x0300;
556 break;
557 case MSP_MODE_EXTERN:
558 dprintk(KERN_DEBUG "msp3400: extern setstereo: %s\n",
559 strmode[audmode]);
560 nicam = 0x0200;
561 break;
562 case MSP_MODE_FM_RADIO:
563 dprintk(KERN_DEBUG "msp3400: FM-Radio setstereo: %s\n",
564 strmode[audmode]);
565 break;
566 default:
567 dprintk(KERN_DEBUG "msp3400: mono setstereo\n");
568 return;
569 }
570
571 /* switch audio */
572 switch (audmode) {
573 case V4L2_TUNER_MODE_STEREO:
574 src = 0x0020 | nicam;
575 break;
576 case V4L2_TUNER_MODE_MONO:
577 if (msp->mode == MSP_MODE_AM_NICAM) {
578 dprintk("msp3400: switching to AM mono\n");
579 /* AM mono decoding is handled by tuner, not MSP chip */
580 /* SCART switching control register */
581 msp3400c_set_scart(client,SCART_MONO,0);
582 src = 0x0200;
583 break;
584 }
585 case V4L2_TUNER_MODE_LANG1:
586 src = 0x0000 | nicam;
587 break;
588 case V4L2_TUNER_MODE_LANG2:
589 src = 0x0010 | nicam;
590 break;
591 }
592 dprintk(KERN_DEBUG
593 "msp3400: setstereo final source/matrix = 0x%x\n", src);
594
595 if (dolby) {
596 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008,0x0520);
597 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009,0x0620);
598 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000a,src);
599 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b,src);
600 } else {
601 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008,src);
602 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009,src);
603 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000a,src);
604 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b,src);
605 }
606 }
607
608 static void
609 msp3400c_print_mode(struct msp3400c *msp)
610 {
611 if (msp->main == msp->second) {
612 printk(KERN_DEBUG "msp3400: mono sound carrier: %d.%03d MHz\n",
613 msp->main/910000,(msp->main/910)%1000);
614 } else {
615 printk(KERN_DEBUG "msp3400: main sound carrier: %d.%03d MHz\n",
616 msp->main/910000,(msp->main/910)%1000);
617 }
618 if (msp->mode == MSP_MODE_FM_NICAM1 ||
619 msp->mode == MSP_MODE_FM_NICAM2)
620 printk(KERN_DEBUG "msp3400: NICAM/FM carrier : %d.%03d MHz\n",
621 msp->second/910000,(msp->second/910)%1000);
622 if (msp->mode == MSP_MODE_AM_NICAM)
623 printk(KERN_DEBUG "msp3400: NICAM/AM carrier : %d.%03d MHz\n",
624 msp->second/910000,(msp->second/910)%1000);
625 if (msp->mode == MSP_MODE_FM_TERRA &&
626 msp->main != msp->second) {
627 printk(KERN_DEBUG "msp3400: FM-stereo carrier : %d.%03d MHz\n",
628 msp->second/910000,(msp->second/910)%1000);
629 }
630 }
631
632 /* ----------------------------------------------------------------------- */
633
634 struct REGISTER_DUMP {
635 int addr;
636 char *name;
637 };
638
639 static int
640 autodetect_stereo(struct i2c_client *client)
641 {
642 struct msp3400c *msp = i2c_get_clientdata(client);
643 int val;
644 int rxsubchans = msp->rxsubchans;
645 int newnicam = msp->nicam_on;
646 int update = 0;
647
648 switch (msp->mode) {
649 case MSP_MODE_FM_TERRA:
650 val = msp3400c_read(client, I2C_MSP3400C_DFP, 0x18);
651 if (val > 32767)
652 val -= 65536;
653 dprintk(KERN_DEBUG
654 "msp34xx: stereo detect register: %d\n",val);
655 if (val > 4096) {
656 rxsubchans = V4L2_TUNER_SUB_STEREO | V4L2_TUNER_SUB_MONO;
657 } else if (val < -4096) {
658 rxsubchans = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
659 } else {
660 rxsubchans = V4L2_TUNER_SUB_MONO;
661 }
662 newnicam = 0;
663 break;
664 case MSP_MODE_FM_NICAM1:
665 case MSP_MODE_FM_NICAM2:
666 case MSP_MODE_AM_NICAM:
667 val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x23);
668 dprintk(KERN_DEBUG
669 "msp34xx: nicam sync=%d, mode=%d\n",
670 val & 1, (val & 0x1e) >> 1);
671
672 if (val & 1) {
673 /* nicam synced */
674 switch ((val & 0x1e) >> 1) {
675 case 0:
676 case 8:
677 rxsubchans = V4L2_TUNER_SUB_STEREO;
678 break;
679 case 1:
680 case 9:
681 rxsubchans = V4L2_TUNER_SUB_MONO
682 | V4L2_TUNER_SUB_LANG1;
683 break;
684 case 2:
685 case 10:
686 rxsubchans = V4L2_TUNER_SUB_MONO
687 | V4L2_TUNER_SUB_LANG1
688 | V4L2_TUNER_SUB_LANG2;
689 break;
690 default:
691 rxsubchans = V4L2_TUNER_SUB_MONO;
692 break;
693 }
694 newnicam=1;
695 } else {
696 newnicam = 0;
697 rxsubchans = V4L2_TUNER_SUB_MONO;
698 }
699 break;
700 case MSP_MODE_BTSC:
701 val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x200);
702 dprintk(KERN_DEBUG
703 "msp3410: status=0x%x (pri=%s, sec=%s, %s%s%s)\n",
704 val,
705 (val & 0x0002) ? "no" : "yes",
706 (val & 0x0004) ? "no" : "yes",
707 (val & 0x0040) ? "stereo" : "mono",
708 (val & 0x0080) ? ", nicam 2nd mono" : "",
709 (val & 0x0100) ? ", bilingual/SAP" : "");
710 rxsubchans = V4L2_TUNER_SUB_MONO;
711 if (val & 0x0040) rxsubchans |= V4L2_TUNER_SUB_STEREO;
712 if (val & 0x0100) rxsubchans |= V4L2_TUNER_SUB_LANG1;
713 break;
714 }
715 if (rxsubchans != msp->rxsubchans) {
716 update = 1;
717 dprintk(KERN_DEBUG "msp34xx: watch: rxsubchans %d => %d\n",
718 msp->rxsubchans,rxsubchans);
719 msp->rxsubchans = rxsubchans;
720 }
721 if (newnicam != msp->nicam_on) {
722 update = 1;
723 dprintk(KERN_DEBUG "msp34xx: watch: nicam %d => %d\n",
724 msp->nicam_on,newnicam);
725 msp->nicam_on = newnicam;
726 }
727 return update;
728 }
729
730 /*
731 * A kernel thread for msp3400 control -- we don't want to block the
732 * in the ioctl while doing the sound carrier & stereo detect
733 */
734
735 static int msp34xx_sleep(struct msp3400c *msp, int timeout)
736 {
737 DECLARE_WAITQUEUE(wait, current);
738
739 add_wait_queue(&msp->wq, &wait);
740 if (!kthread_should_stop()) {
741 if (timeout < 0) {
742 set_current_state(TASK_INTERRUPTIBLE);
743 schedule();
744 } else {
745 schedule_timeout_interruptible
746 (msecs_to_jiffies(timeout));
747 }
748 }
749
750 remove_wait_queue(&msp->wq, &wait);
751 try_to_freeze();
752 return msp->restart;
753 }
754
755 /* stereo/multilang monitoring */
756 static void watch_stereo(struct i2c_client *client)
757 {
758 struct msp3400c *msp = i2c_get_clientdata(client);
759
760 if (autodetect_stereo(client))
761 msp3400c_set_audmode(client,best_audio_mode(msp->rxsubchans));
762 if (once)
763 msp->watch_stereo = 0;
764 }
765
766 static int msp3400c_thread(void *data)
767 {
768 struct i2c_client *client = data;
769 struct msp3400c *msp = i2c_get_clientdata(client);
770 struct CARRIER_DETECT *cd;
771 int count, max1,max2,val1,val2, val,this;
772
773 printk("msp3400: kthread started\n");
774 for (;;) {
775 d2printk("msp3400: thread: sleep\n");
776 msp34xx_sleep(msp,-1);
777 d2printk("msp3400: thread: wakeup\n");
778
779 restart:
780 dprintk("msp3410: thread: restart scan\n");
781 msp->restart = 0;
782 if (kthread_should_stop())
783 break;
784
785 if (VIDEO_MODE_RADIO == msp->norm ||
786 MSP_MODE_EXTERN == msp->mode) {
787 /* no carrier scan, just unmute */
788 printk("msp3400: thread: no carrier scan\n");
789 msp3400c_setvolume(client, msp->muted,
790 msp->volume, msp->balance);
791 continue;
792 }
793
794 /* mute */
795 msp3400c_setvolume(client, msp->muted, 0, 0);
796 msp3400c_setmode(client, MSP_MODE_AM_DETECT /* +1 */ );
797 val1 = val2 = 0;
798 max1 = max2 = -1;
799 msp->watch_stereo = 0;
800
801 /* some time for the tuner to sync */
802 if (msp34xx_sleep(msp,200))
803 goto restart;
804
805 /* carrier detect pass #1 -- main carrier */
806 cd = carrier_detect_main; count = CARRIER_COUNT(carrier_detect_main);
807
808 if (amsound && (msp->norm == VIDEO_MODE_SECAM)) {
809 /* autodetect doesn't work well with AM ... */
810 max1 = 3;
811 count = 0;
812 dprintk("msp3400: AM sound override\n");
813 }
814
815 for (this = 0; this < count; this++) {
816 msp3400c_setcarrier(client, cd[this].cdo,cd[this].cdo);
817 if (msp34xx_sleep(msp,100))
818 goto restart;
819 val = msp3400c_read(client, I2C_MSP3400C_DFP, 0x1b);
820 if (val > 32767)
821 val -= 65536;
822 if (val1 < val)
823 val1 = val, max1 = this;
824 dprintk("msp3400: carrier1 val: %5d / %s\n", val,cd[this].name);
825 }
826
827 /* carrier detect pass #2 -- second (stereo) carrier */
828 switch (max1) {
829 case 1: /* 5.5 */
830 cd = carrier_detect_55;
831 count = CARRIER_COUNT(carrier_detect_55);
832 break;
833 case 3: /* 6.5 */
834 cd = carrier_detect_65;
835 count = CARRIER_COUNT(carrier_detect_65);
836 break;
837 case 0: /* 4.5 */
838 case 2: /* 6.0 */
839 default:
840 cd = NULL; count = 0;
841 break;
842 }
843
844 if (amsound && (msp->norm == VIDEO_MODE_SECAM)) {
845 /* autodetect doesn't work well with AM ... */
846 cd = NULL; count = 0; max2 = 0;
847 }
848 for (this = 0; this < count; this++) {
849 msp3400c_setcarrier(client, cd[this].cdo,cd[this].cdo);
850 if (msp34xx_sleep(msp,100))
851 goto restart;
852 val = msp3400c_read(client, I2C_MSP3400C_DFP, 0x1b);
853 if (val > 32767)
854 val -= 65536;
855 if (val2 < val)
856 val2 = val, max2 = this;
857 dprintk("msp3400: carrier2 val: %5d / %s\n", val,cd[this].name);
858 }
859
860 /* programm the msp3400 according to the results */
861 msp->main = carrier_detect_main[max1].cdo;
862 switch (max1) {
863 case 1: /* 5.5 */
864 if (max2 == 0) {
865 /* B/G FM-stereo */
866 msp->second = carrier_detect_55[max2].cdo;
867 msp3400c_setmode(client, MSP_MODE_FM_TERRA);
868 msp->nicam_on = 0;
869 msp3400c_set_audmode(client, V4L2_TUNER_MODE_MONO);
870 msp->watch_stereo = 1;
871 } else if (max2 == 1 && HAVE_NICAM(msp)) {
872 /* B/G NICAM */
873 msp->second = carrier_detect_55[max2].cdo;
874 msp3400c_setmode(client, MSP_MODE_FM_NICAM1);
875 msp->nicam_on = 1;
876 msp3400c_setcarrier(client, msp->second, msp->main);
877 msp->watch_stereo = 1;
878 } else {
879 goto no_second;
880 }
881 break;
882 case 2: /* 6.0 */
883 /* PAL I NICAM */
884 msp->second = MSP_CARRIER(6.552);
885 msp3400c_setmode(client, MSP_MODE_FM_NICAM2);
886 msp->nicam_on = 1;
887 msp3400c_setcarrier(client, msp->second, msp->main);
888 msp->watch_stereo = 1;
889 break;
890 case 3: /* 6.5 */
891 if (max2 == 1 || max2 == 2) {
892 /* D/K FM-stereo */
893 msp->second = carrier_detect_65[max2].cdo;
894 msp3400c_setmode(client, MSP_MODE_FM_TERRA);
895 msp->nicam_on = 0;
896 msp3400c_set_audmode(client, V4L2_TUNER_MODE_MONO);
897 msp->watch_stereo = 1;
898 } else if (max2 == 0 &&
899 msp->norm == VIDEO_MODE_SECAM) {
900 /* L NICAM or AM-mono */
901 msp->second = carrier_detect_65[max2].cdo;
902 msp3400c_setmode(client, MSP_MODE_AM_NICAM);
903 msp->nicam_on = 0;
904 msp3400c_set_audmode(client, V4L2_TUNER_MODE_MONO);
905 msp3400c_setcarrier(client, msp->second, msp->main);
906 /* volume prescale for SCART (AM mono input) */
907 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000d, 0x1900);
908 msp->watch_stereo = 1;
909 } else if (max2 == 0 && HAVE_NICAM(msp)) {
910 /* D/K NICAM */
911 msp->second = carrier_detect_65[max2].cdo;
912 msp3400c_setmode(client, MSP_MODE_FM_NICAM1);
913 msp->nicam_on = 1;
914 msp3400c_setcarrier(client, msp->second, msp->main);
915 msp->watch_stereo = 1;
916 } else {
917 goto no_second;
918 }
919 break;
920 case 0: /* 4.5 */
921 default:
922 no_second:
923 msp->second = carrier_detect_main[max1].cdo;
924 msp3400c_setmode(client, MSP_MODE_FM_TERRA);
925 msp->nicam_on = 0;
926 msp3400c_setcarrier(client, msp->second, msp->main);
927 msp->rxsubchans = V4L2_TUNER_SUB_MONO;
928 msp3400c_set_audmode(client, V4L2_TUNER_MODE_MONO);
929 break;
930 }
931
932 /* unmute */
933 msp3400c_setvolume(client, msp->muted,
934 msp->volume, msp->balance);
935 if (debug)
936 msp3400c_print_mode(msp);
937
938 /* monitor tv audio mode */
939 while (msp->watch_stereo) {
940 if (msp34xx_sleep(msp,5000))
941 goto restart;
942 watch_stereo(client);
943 }
944 }
945 dprintk(KERN_DEBUG "msp3400: thread: exit\n");
946 return 0;
947 }
948
949 /* ----------------------------------------------------------------------- */
950 /* this one uses the automatic sound standard detection of newer */
951 /* msp34xx chip versions */
952
953 static struct MODES {
954 int retval;
955 int main, second;
956 char *name;
957 } modelist[] = {
958 { 0x0000, 0, 0, "ERROR" },
959 { 0x0001, 0, 0, "autodetect start" },
960 { 0x0002, MSP_CARRIER(4.5), MSP_CARRIER(4.72), "4.5/4.72 M Dual FM-Stereo" },
961 { 0x0003, MSP_CARRIER(5.5), MSP_CARRIER(5.7421875), "5.5/5.74 B/G Dual FM-Stereo" },
962 { 0x0004, MSP_CARRIER(6.5), MSP_CARRIER(6.2578125), "6.5/6.25 D/K1 Dual FM-Stereo" },
963 { 0x0005, MSP_CARRIER(6.5), MSP_CARRIER(6.7421875), "6.5/6.74 D/K2 Dual FM-Stereo" },
964 { 0x0006, MSP_CARRIER(6.5), MSP_CARRIER(6.5), "6.5 D/K FM-Mono (HDEV3)" },
965 { 0x0008, MSP_CARRIER(5.5), MSP_CARRIER(5.85), "5.5/5.85 B/G NICAM FM" },
966 { 0x0009, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85 L NICAM AM" },
967 { 0x000a, MSP_CARRIER(6.0), MSP_CARRIER(6.55), "6.0/6.55 I NICAM FM" },
968 { 0x000b, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85 D/K NICAM FM" },
969 { 0x000c, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85 D/K NICAM FM (HDEV2)" },
970 { 0x0020, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5 M BTSC-Stereo" },
971 { 0x0021, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5 M BTSC-Mono + SAP" },
972 { 0x0030, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5 M EIA-J Japan Stereo" },
973 { 0x0040, MSP_CARRIER(10.7), MSP_CARRIER(10.7), "10.7 FM-Stereo Radio" },
974 { 0x0050, MSP_CARRIER(6.5), MSP_CARRIER(6.5), "6.5 SAT-Mono" },
975 { 0x0051, MSP_CARRIER(7.02), MSP_CARRIER(7.20), "7.02/7.20 SAT-Stereo" },
976 { 0x0060, MSP_CARRIER(7.2), MSP_CARRIER(7.2), "7.2 SAT ADR" },
977 { -1, 0, 0, NULL }, /* EOF */
978 };
979
980 static inline const char *msp34xx_standard_mode_name(int mode)
981 {
982 int i;
983 for (i = 0; modelist[i].name != NULL; i++)
984 if (modelist[i].retval == mode)
985 return modelist[i].name;
986 return "unknown";
987 }
988
989 static int msp34xx_modus(int norm)
990 {
991 switch (norm) {
992 case VIDEO_MODE_PAL:
993 dprintk(KERN_DEBUG "msp34xx: video mode selected to PAL\n");
994
995 #if 1
996 /* experimental: not sure this works with all chip versions */
997 return 0x7003;
998 #else
999 /* previous value, try this if it breaks ... */
1000 return 0x1003;
1001 #endif
1002 case VIDEO_MODE_NTSC: /* BTSC */
1003 dprintk(KERN_DEBUG "msp34xx: video mode selected to NTSC\n");
1004 return 0x2003;
1005 case VIDEO_MODE_SECAM:
1006 dprintk(KERN_DEBUG "msp34xx: video mode selected to SECAM\n");
1007 return 0x0003;
1008 case VIDEO_MODE_RADIO:
1009 dprintk(KERN_DEBUG "msp34xx: video mode selected to Radio\n");
1010 return 0x0003;
1011 case VIDEO_MODE_AUTO:
1012 dprintk(KERN_DEBUG "msp34xx: video mode selected to Auto\n");
1013 return 0x2003;
1014 default:
1015 return 0x0003;
1016 }
1017 }
1018
1019 static int msp34xx_standard(int norm)
1020 {
1021 switch (norm) {
1022 case VIDEO_MODE_PAL:
1023 return 1;
1024 case VIDEO_MODE_NTSC: /* BTSC */
1025 return 0x0020;
1026 case VIDEO_MODE_SECAM:
1027 return 1;
1028 case VIDEO_MODE_RADIO:
1029 return 0x0040;
1030 default:
1031 return 1;
1032 }
1033 }
1034
1035 static int msp3410d_thread(void *data)
1036 {
1037 struct i2c_client *client = data;
1038 struct msp3400c *msp = i2c_get_clientdata(client);
1039 int mode,val,i,std;
1040
1041 printk("msp3410: daemon started\n");
1042 for (;;) {
1043 d2printk(KERN_DEBUG "msp3410: thread: sleep\n");
1044 msp34xx_sleep(msp,-1);
1045 d2printk(KERN_DEBUG "msp3410: thread: wakeup\n");
1046
1047 restart:
1048 dprintk("msp3410: thread: restart scan\n");
1049 msp->restart = 0;
1050 if (kthread_should_stop())
1051 break;
1052
1053 if (msp->mode == MSP_MODE_EXTERN) {
1054 /* no carrier scan needed, just unmute */
1055 dprintk(KERN_DEBUG "msp3410: thread: no carrier scan\n");
1056 msp3400c_setvolume(client, msp->muted,
1057 msp->volume, msp->balance);
1058 continue;
1059 }
1060
1061 /* put into sane state (and mute) */
1062 msp3400c_reset(client);
1063
1064 /* some time for the tuner to sync */
1065 if (msp34xx_sleep(msp,200))
1066 goto restart;
1067
1068 /* start autodetect */
1069 mode = msp34xx_modus(msp->norm);
1070 std = msp34xx_standard(msp->norm);
1071 msp3400c_write(client, I2C_MSP3400C_DEM, 0x30, mode);
1072 msp3400c_write(client, I2C_MSP3400C_DEM, 0x20, std);
1073 msp->watch_stereo = 0;
1074
1075 if (debug)
1076 printk(KERN_DEBUG "msp3410: setting mode: %s (0x%04x)\n",
1077 msp34xx_standard_mode_name(std) ,std);
1078
1079 if (std != 1) {
1080 /* programmed some specific mode */
1081 val = std;
1082 } else {
1083 /* triggered autodetect */
1084 for (;;) {
1085 if (msp34xx_sleep(msp,100))
1086 goto restart;
1087
1088 /* check results */
1089 val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x7e);
1090 if (val < 0x07ff)
1091 break;
1092 dprintk(KERN_DEBUG "msp3410: detection still in progress\n");
1093 }
1094 }
1095 for (i = 0; modelist[i].name != NULL; i++)
1096 if (modelist[i].retval == val)
1097 break;
1098 dprintk(KERN_DEBUG "msp3410: current mode: %s (0x%04x)\n",
1099 modelist[i].name ? modelist[i].name : "unknown",
1100 val);
1101 msp->main = modelist[i].main;
1102 msp->second = modelist[i].second;
1103
1104 if (amsound && (msp->norm == VIDEO_MODE_SECAM) && (val != 0x0009)) {
1105 /* autodetection has failed, let backup */
1106 dprintk(KERN_DEBUG "msp3410: autodetection failed,"
1107 " switching to backup mode: %s (0x%04x)\n",
1108 modelist[8].name ? modelist[8].name : "unknown",val);
1109 val = 0x0009;
1110 msp3400c_write(client, I2C_MSP3400C_DEM, 0x20, val);
1111 }
1112
1113 /* set various prescales */
1114 msp3400c_write(client, I2C_MSP3400C_DFP, 0x0d, 0x1900); /* scart */
1115 msp3400c_write(client, I2C_MSP3400C_DFP, 0x0e, 0x2403); /* FM */
1116 msp3400c_write(client, I2C_MSP3400C_DFP, 0x10, 0x5a00); /* nicam */
1117
1118 /* set stereo */
1119 switch (val) {
1120 case 0x0008: /* B/G NICAM */
1121 case 0x000a: /* I NICAM */
1122 if (val == 0x0008)
1123 msp->mode = MSP_MODE_FM_NICAM1;
1124 else
1125 msp->mode = MSP_MODE_FM_NICAM2;
1126 /* just turn on stereo */
1127 msp->rxsubchans = V4L2_TUNER_SUB_STEREO;
1128 msp->nicam_on = 1;
1129 msp->watch_stereo = 1;
1130 msp3400c_set_audmode(client,V4L2_TUNER_MODE_STEREO);
1131 break;
1132 case 0x0009:
1133 msp->mode = MSP_MODE_AM_NICAM;
1134 msp->rxsubchans = V4L2_TUNER_SUB_MONO;
1135 msp->nicam_on = 1;
1136 msp3400c_set_audmode(client,V4L2_TUNER_MODE_MONO);
1137 msp->watch_stereo = 1;
1138 break;
1139 case 0x0020: /* BTSC */
1140 /* just turn on stereo */
1141 msp->mode = MSP_MODE_BTSC;
1142 msp->rxsubchans = V4L2_TUNER_SUB_STEREO;
1143 msp->nicam_on = 0;
1144 msp->watch_stereo = 1;
1145 msp3400c_set_audmode(client,V4L2_TUNER_MODE_STEREO);
1146 break;
1147 case 0x0040: /* FM radio */
1148 msp->mode = MSP_MODE_FM_RADIO;
1149 msp->rxsubchans = V4L2_TUNER_SUB_STEREO;
1150 msp->audmode = V4L2_TUNER_MODE_STEREO;
1151 msp->nicam_on = 0;
1152 msp->watch_stereo = 0;
1153 /* not needed in theory if HAVE_RADIO(), but
1154 short programming enables carrier mute */
1155 msp3400c_setmode(client,MSP_MODE_FM_RADIO);
1156 msp3400c_setcarrier(client, MSP_CARRIER(10.7),
1157 MSP_CARRIER(10.7));
1158 /* scart routing */
1159 msp3400c_set_scart(client,SCART_IN2,0);
1160 /* msp34xx does radio decoding */
1161 msp3400c_write(client,I2C_MSP3400C_DFP, 0x08, 0x0020);
1162 msp3400c_write(client,I2C_MSP3400C_DFP, 0x09, 0x0020);
1163 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0b, 0x0020);
1164 break;
1165 case 0x0003:
1166 case 0x0004:
1167 case 0x0005:
1168 msp->mode = MSP_MODE_FM_TERRA;
1169 msp->rxsubchans = V4L2_TUNER_SUB_MONO;
1170 msp->audmode = V4L2_TUNER_MODE_MONO;
1171 msp->nicam_on = 0;
1172 msp->watch_stereo = 1;
1173 break;
1174 }
1175
1176 /* unmute, restore misc registers */
1177 msp3400c_setbass(client, msp->bass);
1178 msp3400c_settreble(client, msp->treble);
1179 msp3400c_setvolume(client, msp->muted,
1180 msp->volume, msp->balance);
1181 msp3400c_write(client, I2C_MSP3400C_DFP, 0x0013, msp->acb);
1182
1183 /* monitor tv audio mode */
1184 while (msp->watch_stereo) {
1185 if (msp34xx_sleep(msp,5000))
1186 goto restart;
1187 watch_stereo(client);
1188 }
1189 }
1190 dprintk(KERN_DEBUG "msp3410: thread: exit\n");
1191 return 0;
1192 }
1193
1194 /* ----------------------------------------------------------------------- */
1195 /* msp34xxG + (simpler no-thread) */
1196 /* this one uses both automatic standard detection and automatic sound */
1197 /* select which are available in the newer G versions */
1198 /* struct msp: only norm, acb and source are really used in this mode */
1199
1200 static void msp34xxg_set_source(struct i2c_client *client, int source);
1201
1202 /* (re-)initialize the msp34xxg, according to the current norm in msp->norm
1203 * return 0 if it worked, -1 if it failed
1204 */
1205 static int msp34xxg_init(struct i2c_client *client)
1206 {
1207 struct msp3400c *msp = i2c_get_clientdata(client);
1208 int modus,std;
1209
1210 if (msp3400c_reset(client))
1211 return -1;
1212
1213 /* make sure that input/output is muted (paranoid mode) */
1214 if (msp3400c_write(client,
1215 I2C_MSP3400C_DFP,
1216 0x13, /* ACB */
1217 0x0f20 /* mute DSP input, mute SCART 1 */))
1218 return -1;
1219
1220 /* step-by-step initialisation, as described in the manual */
1221 modus = msp34xx_modus(msp->norm);
1222 std = msp34xx_standard(msp->norm);
1223 modus &= ~0x03; /* STATUS_CHANGE=0 */
1224 modus |= 0x01; /* AUTOMATIC_SOUND_DETECTION=1 */
1225 if (msp3400c_write(client,
1226 I2C_MSP3400C_DEM,
1227 0x30/*MODUS*/,
1228 modus))
1229 return -1;
1230 if (msp3400c_write(client,
1231 I2C_MSP3400C_DEM,
1232 0x20/*stanard*/,
1233 std))
1234 return -1;
1235
1236 /* write the dfps that may have an influence on
1237 standard/audio autodetection right now */
1238 msp34xxg_set_source(client, msp->source);
1239
1240 if (msp3400c_write(client, I2C_MSP3400C_DFP,
1241 0x0e, /* AM/FM Prescale */
1242 0x3000 /* default: [15:8] 75khz deviation */))
1243 return -1;
1244
1245 if (msp3400c_write(client, I2C_MSP3400C_DFP,
1246 0x10, /* NICAM Prescale */
1247 0x5a00 /* default: 9db gain (as recommended) */))
1248 return -1;
1249
1250 if (msp3400c_write(client,
1251 I2C_MSP3400C_DEM,
1252 0x20, /* STANDARD SELECT */
1253 standard /* default: 0x01 for automatic standard select*/))
1254 return -1;
1255 return 0;
1256 }
1257
1258 static int msp34xxg_thread(void *data)
1259 {
1260 struct i2c_client *client = data;
1261 struct msp3400c *msp = i2c_get_clientdata(client);
1262 int val, std, i;
1263
1264 printk("msp34xxg: daemon started\n");
1265 msp->source = 1; /* default */
1266 for (;;) {
1267 d2printk(KERN_DEBUG "msp34xxg: thread: sleep\n");
1268 msp34xx_sleep(msp,-1);
1269 d2printk(KERN_DEBUG "msp34xxg: thread: wakeup\n");
1270
1271 restart:
1272 dprintk("msp34xxg: thread: restart scan\n");
1273 msp->restart = 0;
1274 if (kthread_should_stop())
1275 break;
1276
1277 /* setup the chip*/
1278 msp34xxg_init(client);
1279 std = standard;
1280 if (std != 0x01)
1281 goto unmute;
1282
1283 /* watch autodetect */
1284 dprintk("msp34xxg: triggered autodetect, waiting for result\n");
1285 for (i = 0; i < 10; i++) {
1286 if (msp34xx_sleep(msp,100))
1287 goto restart;
1288
1289 /* check results */
1290 val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x7e);
1291 if (val < 0x07ff) {
1292 std = val;
1293 break;
1294 }
1295 dprintk("msp34xxg: detection still in progress\n");
1296 }
1297 if (0x01 == std) {
1298 dprintk("msp34xxg: detection still in progress after 10 tries. giving up.\n");
1299 continue;
1300 }
1301
1302 unmute:
1303 dprintk("msp34xxg: current mode: %s (0x%04x)\n",
1304 msp34xx_standard_mode_name(std), std);
1305
1306 /* unmute: dispatch sound to scart output, set scart volume */
1307 dprintk("msp34xxg: unmute\n");
1308
1309 msp3400c_setbass(client, msp->bass);
1310 msp3400c_settreble(client, msp->treble);
1311 msp3400c_setvolume(client, msp->muted, msp->volume, msp->balance);
1312
1313 /* restore ACB */
1314 if (msp3400c_write(client,
1315 I2C_MSP3400C_DFP,
1316 0x13, /* ACB */
1317 msp->acb))
1318 return -1;
1319 }
1320 dprintk(KERN_DEBUG "msp34xxg: thread: exit\n");
1321 return 0;
1322 }
1323
1324 /* set the same 'source' for the loudspeaker, scart and quasi-peak detector
1325 * the value for source is the same as bit 15:8 of DFP registers 0x08,
1326 * 0x0a and 0x0c: 0=mono, 1=stereo or A|B, 2=SCART, 3=stereo or A, 4=stereo or B
1327 *
1328 * this function replaces msp3400c_setstereo
1329 */
1330 static void msp34xxg_set_source(struct i2c_client *client, int source)
1331 {
1332 struct msp3400c *msp = i2c_get_clientdata(client);
1333
1334 /* fix matrix mode to stereo and let the msp choose what
1335 * to output according to 'source', as recommended
1336 * for MONO (source==0) downmixing set bit[7:0] to 0x30
1337 */
1338 int value = (source&0x07)<<8|(source==0 ? 0x30:0x20);
1339 dprintk("msp34xxg: set source to %d (0x%x)\n", source, value);
1340 msp3400c_write(client,
1341 I2C_MSP3400C_DFP,
1342 0x08, /* Loudspeaker Output */
1343 value);
1344 msp3400c_write(client,
1345 I2C_MSP3400C_DFP,
1346 0x0a, /* SCART1 DA Output */
1347 value);
1348 msp3400c_write(client,
1349 I2C_MSP3400C_DFP,
1350 0x0c, /* Quasi-peak detector */
1351 value);
1352 /*
1353 * set identification threshold. Personally, I
1354 * I set it to a higher value that the default
1355 * of 0x190 to ignore noisy stereo signals.
1356 * this needs tuning. (recommended range 0x00a0-0x03c0)
1357 * 0x7f0 = forced mono mode
1358 */
1359 msp3400c_write(client,
1360 I2C_MSP3400C_DEM,
1361 0x22, /* a2 threshold for stereo/bilingual */
1362 stereo_threshold);
1363 msp->source=source;
1364 }
1365
1366 static void msp34xxg_detect_stereo(struct i2c_client *client)
1367 {
1368 struct msp3400c *msp = i2c_get_clientdata(client);
1369
1370 int status = msp3400c_read(client,
1371 I2C_MSP3400C_DEM,
1372 0x0200 /* STATUS */);
1373 int is_bilingual = status&0x100;
1374 int is_stereo = status&0x40;
1375
1376 msp->rxsubchans = 0;
1377 if (is_stereo)
1378 msp->rxsubchans |= V4L2_TUNER_SUB_STEREO;
1379 else
1380 msp->rxsubchans |= V4L2_TUNER_SUB_MONO;
1381 if (is_bilingual) {
1382 msp->rxsubchans |= V4L2_TUNER_SUB_LANG1|V4L2_TUNER_SUB_LANG2;
1383 /* I'm supposed to check whether it's SAP or not
1384 * and set only LANG2/SAP in this case. Yet, the MSP
1385 * does a lot of work to hide this and handle everything
1386 * the same way. I don't want to work around it so unless
1387 * this is a problem, I'll handle SAP just like lang1/lang2.
1388 */
1389 }
1390 dprintk("msp34xxg: status=0x%x, stereo=%d, bilingual=%d -> rxsubchans=%d\n",
1391 status, is_stereo, is_bilingual, msp->rxsubchans);
1392 }
1393
1394 static void msp34xxg_set_audmode(struct i2c_client *client, int audmode)
1395 {
1396 struct msp3400c *msp = i2c_get_clientdata(client);
1397 int source;
1398
1399 switch (audmode) {
1400 case V4L2_TUNER_MODE_MONO:
1401 source=0; /* mono only */
1402 break;
1403 case V4L2_TUNER_MODE_STEREO:
1404 source=1; /* stereo or A|B, see comment in msp34xxg_get_v4l2_stereo() */
1405 /* problem: that could also mean 2 (scart input) */
1406 break;
1407 case V4L2_TUNER_MODE_LANG1:
1408 source=3; /* stereo or A */
1409 break;
1410 case V4L2_TUNER_MODE_LANG2:
1411 source=4; /* stereo or B */
1412 break;
1413 default:
1414 audmode = 0;
1415 source = 1;
1416 break;
1417 }
1418 msp->audmode = audmode;
1419 msp34xxg_set_source(client, source);
1420 }
1421
1422
1423 /* ----------------------------------------------------------------------- */
1424
1425 static int msp_attach(struct i2c_adapter *adap, int addr, int kind);
1426 static int msp_detach(struct i2c_client *client);
1427 static int msp_probe(struct i2c_adapter *adap);
1428 static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg);
1429
1430 static int msp_suspend(struct device * dev, pm_message_t state);
1431 static int msp_resume(struct device * dev);
1432
1433 static void msp_wake_thread(struct i2c_client *client);
1434
1435 static struct i2c_driver driver = {
1436 .owner = THIS_MODULE,
1437 .name = "i2c msp3400 driver",
1438 .id = I2C_DRIVERID_MSP3400,
1439 .flags = I2C_DF_NOTIFY,
1440 .attach_adapter = msp_probe,
1441 .detach_client = msp_detach,
1442 .command = msp_command,
1443 .driver = {
1444 .suspend = msp_suspend,
1445 .resume = msp_resume,
1446 },
1447 };
1448
1449 static struct i2c_client client_template =
1450 {
1451 .name = "(unset)",
1452 .flags = I2C_CLIENT_ALLOW_USE,
1453 .driver = &driver,
1454 };
1455
1456 static int msp_attach(struct i2c_adapter *adap, int addr, int kind)
1457 {
1458 struct msp3400c *msp;
1459 struct i2c_client *c;
1460 int (*thread_func)(void *data) = NULL;
1461
1462 client_template.adapter = adap;
1463 client_template.addr = addr;
1464
1465 if (-1 == msp3400c_reset(&client_template)) {
1466 dprintk("msp34xx: no chip found\n");
1467 return -1;
1468 }
1469
1470 if (NULL == (c = kmalloc(sizeof(struct i2c_client),GFP_KERNEL)))
1471 return -ENOMEM;
1472 memcpy(c,&client_template,sizeof(struct i2c_client));
1473 if (NULL == (msp = kmalloc(sizeof(struct msp3400c),GFP_KERNEL))) {
1474 kfree(c);
1475 return -ENOMEM;
1476 }
1477
1478 memset(msp,0,sizeof(struct msp3400c));
1479 msp->volume = 58880; /* 0db gain */
1480 msp->balance = 32768;
1481 msp->bass = 32768;
1482 msp->treble = 32768;
1483 msp->input = -1;
1484 msp->muted = 1;
1485
1486 i2c_set_clientdata(c, msp);
1487 init_waitqueue_head(&msp->wq);
1488
1489 if (-1 == msp3400c_reset(c)) {
1490 kfree(msp);
1491 kfree(c);
1492 dprintk("msp34xx: no chip found\n");
1493 return -1;
1494 }
1495
1496 msp->rev1 = msp3400c_read(c, I2C_MSP3400C_DFP, 0x1e);
1497 if (-1 != msp->rev1)
1498 msp->rev2 = msp3400c_read(c, I2C_MSP3400C_DFP, 0x1f);
1499 if ((-1 == msp->rev1) || (0 == msp->rev1 && 0 == msp->rev2)) {
1500 kfree(msp);
1501 kfree(c);
1502 dprintk("msp34xx: error while reading chip version\n");
1503 return -1;
1504 }
1505 printk(KERN_INFO "msp34xx: rev1=0x%04x, rev2=0x%04x\n", msp->rev1, msp->rev2);
1506
1507 msp3400c_setvolume(c, msp->muted, msp->volume, msp->balance);
1508
1509 snprintf(c->name, sizeof(c->name), "MSP34%02d%c-%c%d",
1510 (msp->rev2>>8)&0xff, (msp->rev1&0xff)+'@',
1511 ((msp->rev1>>8)&0xff)+'@', msp->rev2&0x1f);
1512
1513 msp->opmode = opmode;
1514 if (OPMODE_AUTO == msp->opmode) {
1515 if (HAVE_SIMPLER(msp))
1516 msp->opmode = OPMODE_SIMPLER;
1517 else if (HAVE_SIMPLE(msp))
1518 msp->opmode = OPMODE_SIMPLE;
1519 else
1520 msp->opmode = OPMODE_MANUAL;
1521 }
1522
1523 /* hello world :-) */
1524 printk(KERN_INFO "msp34xx: init: chip=%s", c->name);
1525 if (HAVE_NICAM(msp))
1526 printk(" +nicam");
1527 if (HAVE_SIMPLE(msp))
1528 printk(" +simple");
1529 if (HAVE_SIMPLER(msp))
1530 printk(" +simpler");
1531 if (HAVE_RADIO(msp))
1532 printk(" +radio");
1533
1534 /* version-specific initialization */
1535 switch (msp->opmode) {
1536 case OPMODE_MANUAL:
1537 printk(" mode=manual");
1538 thread_func = msp3400c_thread;
1539 break;
1540 case OPMODE_SIMPLE:
1541 printk(" mode=simple");
1542 thread_func = msp3410d_thread;
1543 break;
1544 case OPMODE_SIMPLER:
1545 printk(" mode=simpler");
1546 thread_func = msp34xxg_thread;
1547 break;
1548 }
1549 printk("\n");
1550
1551 /* startup control thread if needed */
1552 if (thread_func) {
1553 msp->kthread = kthread_run(thread_func, c, "msp34xx");
1554 if (NULL == msp->kthread)
1555 printk(KERN_WARNING "msp34xx: kernel_thread() failed\n");
1556 msp_wake_thread(c);
1557 }
1558
1559 /* done */
1560 i2c_attach_client(c);
1561 return 0;
1562 }
1563
1564 static int msp_detach(struct i2c_client *client)
1565 {
1566 struct msp3400c *msp = i2c_get_clientdata(client);
1567
1568 /* shutdown control thread */
1569 if (msp->kthread) {
1570 msp->restart = 1;
1571 kthread_stop(msp->kthread);
1572 }
1573 msp3400c_reset(client);
1574
1575 i2c_detach_client(client);
1576 kfree(msp);
1577 kfree(client);
1578 return 0;
1579 }
1580
1581 static int msp_probe(struct i2c_adapter *adap)
1582 {
1583 if (adap->class & I2C_CLASS_TV_ANALOG)
1584 return i2c_probe(adap, &addr_data, msp_attach);
1585 return 0;
1586 }
1587
1588 static void msp_wake_thread(struct i2c_client *client)
1589 {
1590 struct msp3400c *msp = i2c_get_clientdata(client);
1591
1592 if (NULL == msp->kthread)
1593 return;
1594 msp3400c_setvolume(client,msp->muted,0,0);
1595 msp->watch_stereo = 0;
1596 msp->restart = 1;
1597 wake_up_interruptible(&msp->wq);
1598 }
1599
1600 /* ----------------------------------------------------------------------- */
1601
1602 static int mode_v4l2_to_v4l1(int rxsubchans)
1603 {
1604 int mode = 0;
1605
1606 if (rxsubchans & V4L2_TUNER_SUB_STEREO)
1607 mode |= VIDEO_SOUND_STEREO;
1608 if (rxsubchans & V4L2_TUNER_SUB_LANG2)
1609 mode |= VIDEO_SOUND_LANG2;
1610 if (rxsubchans & V4L2_TUNER_SUB_LANG1)
1611 mode |= VIDEO_SOUND_LANG1;
1612 if (0 == mode)
1613 mode |= VIDEO_SOUND_MONO;
1614 return mode;
1615 }
1616
1617 static int mode_v4l1_to_v4l2(int mode)
1618 {
1619 if (mode & VIDEO_SOUND_STEREO)
1620 return V4L2_TUNER_MODE_STEREO;
1621 if (mode & VIDEO_SOUND_LANG2)
1622 return V4L2_TUNER_MODE_LANG2;
1623 if (mode & VIDEO_SOUND_LANG1)
1624 return V4L2_TUNER_MODE_LANG1;
1625 return V4L2_TUNER_MODE_MONO;
1626 }
1627
1628 static void msp_any_detect_stereo(struct i2c_client *client)
1629 {
1630 struct msp3400c *msp = i2c_get_clientdata(client);
1631
1632 switch (msp->opmode) {
1633 case OPMODE_MANUAL:
1634 case OPMODE_SIMPLE:
1635 autodetect_stereo(client);
1636 break;
1637 case OPMODE_SIMPLER:
1638 msp34xxg_detect_stereo(client);
1639 break;
1640 }
1641 }
1642
1643 static void msp_any_set_audmode(struct i2c_client *client, int audmode)
1644 {
1645 struct msp3400c *msp = i2c_get_clientdata(client);
1646
1647 switch (msp->opmode) {
1648 case OPMODE_MANUAL:
1649 case OPMODE_SIMPLE:
1650 msp->watch_stereo = 0;
1651 msp3400c_set_audmode(client, audmode);
1652 break;
1653 case OPMODE_SIMPLER:
1654 msp34xxg_set_audmode(client, audmode);
1655 break;
1656 }
1657 }
1658
1659 static int msp_command(struct i2c_client *client, unsigned int cmd, void *arg)
1660 {
1661 struct msp3400c *msp = i2c_get_clientdata(client);
1662 __u16 *sarg = arg;
1663 int scart = 0;
1664
1665 switch (cmd) {
1666
1667 case AUDC_SET_INPUT:
1668 dprintk(KERN_DEBUG "msp34xx: AUDC_SET_INPUT(%d)\n",*sarg);
1669 if (*sarg == msp->input)
1670 break;
1671 msp->input = *sarg;
1672 switch (*sarg) {
1673 case AUDIO_RADIO:
1674 /* Hauppauge uses IN2 for the radio */
1675 msp->mode = MSP_MODE_FM_RADIO;
1676 scart = SCART_IN2;
1677 break;
1678 case AUDIO_EXTERN_1:
1679 /* IN1 is often used for external input ... */
1680 msp->mode = MSP_MODE_EXTERN;
1681 scart = SCART_IN1;
1682 break;
1683 case AUDIO_EXTERN_2:
1684 /* ... sometimes it is IN2 through ;) */
1685 msp->mode = MSP_MODE_EXTERN;
1686 scart = SCART_IN2;
1687 break;
1688 case AUDIO_TUNER:
1689 msp->mode = -1;
1690 break;
1691 default:
1692 if (*sarg & AUDIO_MUTE)
1693 msp3400c_set_scart(client,SCART_MUTE,0);
1694 break;
1695 }
1696 if (scart) {
1697 msp->rxsubchans = V4L2_TUNER_SUB_STEREO;
1698 msp->audmode = V4L2_TUNER_MODE_STEREO;
1699 msp3400c_set_scart(client,scart,0);
1700 msp3400c_write(client,I2C_MSP3400C_DFP,0x000d,0x1900);
1701 if (msp->opmode != OPMODE_SIMPLER)
1702 msp3400c_set_audmode(client, msp->audmode);
1703 }
1704 msp_wake_thread(client);
1705 break;
1706
1707 case AUDC_SET_RADIO:
1708 dprintk(KERN_DEBUG "msp34xx: AUDC_SET_RADIO\n");
1709 msp->norm = VIDEO_MODE_RADIO;
1710 dprintk(KERN_DEBUG "msp34xx: switching to radio mode\n");
1711 msp->watch_stereo = 0;
1712 switch (msp->opmode) {
1713 case OPMODE_MANUAL:
1714 /* set msp3400 to FM radio mode */
1715 msp3400c_setmode(client,MSP_MODE_FM_RADIO);
1716 msp3400c_setcarrier(client, MSP_CARRIER(10.7),
1717 MSP_CARRIER(10.7));
1718 msp3400c_setvolume(client, msp->muted,
1719 msp->volume, msp->balance);
1720 break;
1721 case OPMODE_SIMPLE:
1722 case OPMODE_SIMPLER:
1723 /* the thread will do for us */
1724 msp_wake_thread(client);
1725 break;
1726 }
1727 break;
1728
1729 /* --- v4l ioctls --- */
1730 /* take care: bttv does userspace copying, we'll get a
1731 kernel pointer here... */
1732 case VIDIOCGAUDIO:
1733 {
1734 struct video_audio *va = arg;
1735
1736 dprintk(KERN_DEBUG "msp34xx: VIDIOCGAUDIO\n");
1737 va->flags |= VIDEO_AUDIO_VOLUME |
1738 VIDEO_AUDIO_BASS |
1739 VIDEO_AUDIO_TREBLE |
1740 VIDEO_AUDIO_MUTABLE;
1741 if (msp->muted)
1742 va->flags |= VIDEO_AUDIO_MUTE;
1743
1744 va->volume = msp->volume;
1745 va->balance = (va->volume) ? msp->balance : 32768;
1746 va->bass = msp->bass;
1747 va->treble = msp->treble;
1748
1749 msp_any_detect_stereo(client);
1750 va->mode = mode_v4l2_to_v4l1(msp->rxsubchans);
1751 break;
1752 }
1753 case VIDIOCSAUDIO:
1754 {
1755 struct video_audio *va = arg;
1756
1757 dprintk(KERN_DEBUG "msp34xx: VIDIOCSAUDIO\n");
1758 msp->muted = (va->flags & VIDEO_AUDIO_MUTE);
1759 msp->volume = va->volume;
1760 msp->balance = va->balance;
1761 msp->bass = va->bass;
1762 msp->treble = va->treble;
1763
1764 msp3400c_setvolume(client, msp->muted,
1765 msp->volume, msp->balance);
1766 msp3400c_setbass(client,msp->bass);
1767 msp3400c_settreble(client,msp->treble);
1768
1769 if (va->mode != 0 && msp->norm != VIDEO_MODE_RADIO)
1770 msp_any_set_audmode(client,mode_v4l1_to_v4l2(va->mode));
1771 break;
1772 }
1773
1774 case VIDIOCSCHAN:
1775 {
1776 struct video_channel *vc = arg;
1777
1778 dprintk(KERN_DEBUG "msp34xx: VIDIOCSCHAN (norm=%d)\n",vc->norm);
1779 msp->norm = vc->norm;
1780 msp_wake_thread(client);
1781 break;
1782 }
1783
1784 case VIDIOCSFREQ:
1785 case VIDIOC_S_FREQUENCY:
1786 {
1787 /* new channel -- kick audio carrier scan */
1788 dprintk(KERN_DEBUG "msp34xx: VIDIOCSFREQ\n");
1789 msp_wake_thread(client);
1790 break;
1791 }
1792
1793 /* --- v4l2 ioctls --- */
1794 case VIDIOC_S_STD:
1795 {
1796 v4l2_std_id *id = arg;
1797
1798 /*FIXME: use V4L2 mode flags on msp3400 instead of V4L1*/
1799 if (*id & V4L2_STD_PAL) {
1800 msp->norm=VIDEO_MODE_PAL;
1801 } else if (*id & V4L2_STD_SECAM) {
1802 msp->norm=VIDEO_MODE_SECAM;
1803 } else {
1804 msp->norm=VIDEO_MODE_NTSC;
1805 }
1806
1807 msp_wake_thread(client);
1808 return 0;
1809 }
1810
1811 case VIDIOC_G_AUDIO:
1812 {
1813 struct v4l2_audio *a = arg;
1814
1815 memset(a,0,sizeof(*a));
1816
1817 switch (a->index) {
1818 case AUDIO_RADIO:
1819 strcpy(a->name,"Radio");
1820 break;
1821 case AUDIO_EXTERN_1:
1822 strcpy(a->name,"Extern 1");
1823 break;
1824 case AUDIO_EXTERN_2:
1825 strcpy(a->name,"Extern 2");
1826 break;
1827 case AUDIO_TUNER:
1828 strcpy(a->name,"Television");
1829 break;
1830 default:
1831 return -EINVAL;
1832 }
1833
1834 msp_any_detect_stereo(client);
1835 if (msp->audmode == V4L2_TUNER_MODE_STEREO) {
1836 a->capability=V4L2_AUDCAP_STEREO;
1837 }
1838
1839 break;
1840 }
1841 case VIDIOC_S_AUDIO:
1842 {
1843 struct v4l2_audio *sarg = arg;
1844
1845 switch (sarg->index) {
1846 case AUDIO_RADIO:
1847 /* Hauppauge uses IN2 for the radio */
1848 msp->mode = MSP_MODE_FM_RADIO;
1849 scart = SCART_IN2;
1850 break;
1851 case AUDIO_EXTERN_1:
1852 /* IN1 is often used for external input ... */
1853 msp->mode = MSP_MODE_EXTERN;
1854 scart = SCART_IN1;
1855 break;
1856 case AUDIO_EXTERN_2:
1857 /* ... sometimes it is IN2 through ;) */
1858 msp->mode = MSP_MODE_EXTERN;
1859 scart = SCART_IN2;
1860 break;
1861 case AUDIO_TUNER:
1862 msp->mode = -1;
1863 break;
1864 }
1865 if (scart) {
1866 msp->rxsubchans = V4L2_TUNER_SUB_STEREO;
1867 msp->audmode = V4L2_TUNER_MODE_STEREO;
1868 msp3400c_set_scart(client,scart,0);
1869 msp3400c_write(client,I2C_MSP3400C_DFP,0x000d,0x1900);
1870 }
1871 if (sarg->capability==V4L2_AUDCAP_STEREO) {
1872 msp->audmode = V4L2_TUNER_MODE_STEREO;
1873 } else {
1874 msp->audmode &= ~V4L2_TUNER_MODE_STEREO;
1875 }
1876 msp_any_set_audmode(client, msp->audmode);
1877 msp_wake_thread(client);
1878 break;
1879 }
1880 case VIDIOC_G_TUNER:
1881 {
1882 struct v4l2_tuner *vt = arg;
1883
1884 msp_any_detect_stereo(client);
1885 vt->audmode = msp->audmode;
1886 vt->rxsubchans = msp->rxsubchans;
1887 vt->capability = V4L2_TUNER_CAP_STEREO |
1888 V4L2_TUNER_CAP_LANG1|
1889 V4L2_TUNER_CAP_LANG2;
1890 break;
1891 }
1892 case VIDIOC_S_TUNER:
1893 {
1894 struct v4l2_tuner *vt=(struct v4l2_tuner *)arg;
1895
1896 /* only set audmode */
1897 if (vt->audmode != -1 && vt->audmode != 0)
1898 msp_any_set_audmode(client, vt->audmode);
1899 break;
1900 }
1901
1902 /* msp34xx specific */
1903 case MSP_SET_MATRIX:
1904 {
1905 struct msp_matrix *mspm = arg;
1906
1907 dprintk(KERN_DEBUG "msp34xx: MSP_SET_MATRIX\n");
1908 msp3400c_set_scart(client, mspm->input, mspm->output);
1909 break;
1910 }
1911
1912 default:
1913 /* nothing */
1914 break;
1915 }
1916 return 0;
1917 }
1918
1919 static int msp_suspend(struct device * dev, pm_message_t state)
1920 {
1921 struct i2c_client *c = container_of(dev, struct i2c_client, dev);
1922
1923 dprintk("msp34xx: suspend\n");
1924 msp3400c_reset(c);
1925 return 0;
1926 }
1927
1928 static int msp_resume(struct device * dev)
1929 {
1930 struct i2c_client *c = container_of(dev, struct i2c_client, dev);
1931
1932 dprintk("msp34xx: resume\n");
1933 msp_wake_thread(c);
1934 return 0;
1935 }
1936
1937 /* ----------------------------------------------------------------------- */
1938
1939 static int __init msp3400_init_module(void)
1940 {
1941 return i2c_add_driver(&driver);
1942 }
1943
1944 static void __exit msp3400_cleanup_module(void)
1945 {
1946 i2c_del_driver(&driver);
1947 }
1948
1949 module_init(msp3400_init_module);
1950 module_exit(msp3400_cleanup_module);
1951
1952 /*
1953 * Overrides for Emacs so that we follow Linus's tabbing style.
1954 * ---------------------------------------------------------------------------
1955 * Local variables:
1956 * c-basic-offset: 8
1957 * End:
1958 */