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