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