a622dbb72ed89e166fec1566f40abcc825e27c23
[safe/jmp/linux-2.6] / drivers / media / video / msp3400-driver.c
1 /*
2  * Programming the mspx4xx 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  * TODO:
28  *   - better SAT support
29  *
30  * 980623  Thomas Sailer (sailer@ife.ee.ethz.ch)
31  *         using soundcore instead of OSS
32  *
33  * This program is free software; you can redistribute it and/or
34  * modify it under the terms of the GNU General Public License
35  * as published by the Free Software Foundation; either version 2
36  * of the License, or (at your option) any later version.
37  *
38  * This program is distributed in the hope that it will be useful,
39  * but WITHOUT ANY WARRANTY; without even the implied warranty of
40  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
41  * GNU General Public License for more details.
42  *
43  * You should have received a copy of the GNU General Public License
44  * along with this program; if not, write to the Free Software
45  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
46  * 02110-1301, USA.
47  */
48
49
50 #include <linux/kernel.h>
51 #include <linux/module.h>
52 #include <linux/slab.h>
53 #include <linux/i2c.h>
54 #include <linux/kthread.h>
55 #include <linux/freezer.h>
56 #include <linux/videodev2.h>
57 #include <media/v4l2-device.h>
58 #include <media/v4l2-ioctl.h>
59 #include <media/v4l2-i2c-drv-legacy.h>
60 #include <media/msp3400.h>
61 #include <media/tvaudio.h>
62 #include "msp3400-driver.h"
63
64 /* ---------------------------------------------------------------------- */
65
66 MODULE_DESCRIPTION("device driver for msp34xx TV sound processor");
67 MODULE_AUTHOR("Gerd Knorr");
68 MODULE_LICENSE("GPL");
69
70 /* module parameters */
71 static int opmode   = OPMODE_AUTO;
72 int msp_debug;           /* msp_debug output */
73 int msp_once;            /* no continous stereo monitoring */
74 int msp_amsound;         /* hard-wire AM sound at 6.5 Hz (france),
75                             the autoscan seems work well only with FM... */
76 int msp_standard = 1;    /* Override auto detect of audio msp_standard,
77                             if needed. */
78 int msp_dolby;
79
80 int msp_stereo_thresh = 0x190; /* a2 threshold for stereo/bilingual
81                                         (msp34xxg only) 0x00a0-0x03c0 */
82
83 /* read-only */
84 module_param(opmode,           int, 0444);
85
86 /* read-write */
87 module_param_named(once, msp_once,                      bool, 0644);
88 module_param_named(debug, msp_debug,                    int,  0644);
89 module_param_named(stereo_threshold, msp_stereo_thresh, int,  0644);
90 module_param_named(standard, msp_standard,              int,  0644);
91 module_param_named(amsound, msp_amsound,                bool, 0644);
92 module_param_named(dolby, msp_dolby,                    bool, 0644);
93
94 MODULE_PARM_DESC(opmode, "Forces a MSP3400 opmode. 0=Manual, 1=Autodetect, 2=Autodetect and autoselect");
95 MODULE_PARM_DESC(once, "No continuous stereo monitoring");
96 MODULE_PARM_DESC(debug, "Enable debug messages [0-3]");
97 MODULE_PARM_DESC(stereo_threshold, "Sets signal threshold to activate stereo");
98 MODULE_PARM_DESC(standard, "Specify audio standard: 32 = NTSC, 64 = radio, Default: Autodetect");
99 MODULE_PARM_DESC(amsound, "Hardwire AM sound at 6.5Hz (France), FM can autoscan");
100 MODULE_PARM_DESC(dolby, "Activates Dolby processsing");
101
102 /* ---------------------------------------------------------------------- */
103
104 /* control subaddress */
105 #define I2C_MSP_CONTROL 0x00
106 /* demodulator unit subaddress */
107 #define I2C_MSP_DEM     0x10
108 /* DSP unit subaddress */
109 #define I2C_MSP_DSP     0x12
110
111 /* Addresses to scan */
112 static unsigned short normal_i2c[] = { 0x80 >> 1, 0x88 >> 1, I2C_CLIENT_END };
113
114 I2C_CLIENT_INSMOD;
115
116 /* ----------------------------------------------------------------------- */
117 /* functions for talking to the MSP3400C Sound processor                   */
118
119 int msp_reset(struct i2c_client *client)
120 {
121         /* reset and read revision code */
122         static u8 reset_off[3] = { I2C_MSP_CONTROL, 0x80, 0x00 };
123         static u8 reset_on[3]  = { I2C_MSP_CONTROL, 0x00, 0x00 };
124         static u8 write[3]     = { I2C_MSP_DSP + 1, 0x00, 0x1e };
125         u8 read[2];
126         struct i2c_msg reset[2] = {
127                 { client->addr, I2C_M_IGNORE_NAK, 3, reset_off },
128                 { client->addr, I2C_M_IGNORE_NAK, 3, reset_on  },
129         };
130         struct i2c_msg test[2] = {
131                 { client->addr, 0,        3, write },
132                 { client->addr, I2C_M_RD, 2, read  },
133         };
134
135         v4l_dbg(3, msp_debug, client, "msp_reset\n");
136         if (i2c_transfer(client->adapter, &reset[0], 1) != 1 ||
137             i2c_transfer(client->adapter, &reset[1], 1) != 1 ||
138             i2c_transfer(client->adapter, test, 2) != 2) {
139                 v4l_err(client, "chip reset failed\n");
140                 return -1;
141         }
142         return 0;
143 }
144
145 static int msp_read(struct i2c_client *client, int dev, int addr)
146 {
147         int err, retval;
148         u8 write[3];
149         u8 read[2];
150         struct i2c_msg msgs[2] = {
151                 { client->addr, 0,        3, write },
152                 { client->addr, I2C_M_RD, 2, read  }
153         };
154
155         write[0] = dev + 1;
156         write[1] = addr >> 8;
157         write[2] = addr & 0xff;
158
159         for (err = 0; err < 3; err++) {
160                 if (i2c_transfer(client->adapter, msgs, 2) == 2)
161                         break;
162                 v4l_warn(client, "I/O error #%d (read 0x%02x/0x%02x)\n", err,
163                        dev, addr);
164                 schedule_timeout_interruptible(msecs_to_jiffies(10));
165         }
166         if (err == 3) {
167                 v4l_warn(client, "resetting chip, sound will go off.\n");
168                 msp_reset(client);
169                 return -1;
170         }
171         retval = read[0] << 8 | read[1];
172         v4l_dbg(3, msp_debug, client, "msp_read(0x%x, 0x%x): 0x%x\n",
173                         dev, addr, retval);
174         return retval;
175 }
176
177 int msp_read_dem(struct i2c_client *client, int addr)
178 {
179         return msp_read(client, I2C_MSP_DEM, addr);
180 }
181
182 int msp_read_dsp(struct i2c_client *client, int addr)
183 {
184         return msp_read(client, I2C_MSP_DSP, addr);
185 }
186
187 static int msp_write(struct i2c_client *client, int dev, int addr, int val)
188 {
189         int err;
190         u8 buffer[5];
191
192         buffer[0] = dev;
193         buffer[1] = addr >> 8;
194         buffer[2] = addr &  0xff;
195         buffer[3] = val  >> 8;
196         buffer[4] = val  &  0xff;
197
198         v4l_dbg(3, msp_debug, client, "msp_write(0x%x, 0x%x, 0x%x)\n",
199                         dev, addr, val);
200         for (err = 0; err < 3; err++) {
201                 if (i2c_master_send(client, buffer, 5) == 5)
202                         break;
203                 v4l_warn(client, "I/O error #%d (write 0x%02x/0x%02x)\n", err,
204                        dev, addr);
205                 schedule_timeout_interruptible(msecs_to_jiffies(10));
206         }
207         if (err == 3) {
208                 v4l_warn(client, "resetting chip, sound will go off.\n");
209                 msp_reset(client);
210                 return -1;
211         }
212         return 0;
213 }
214
215 int msp_write_dem(struct i2c_client *client, int addr, int val)
216 {
217         return msp_write(client, I2C_MSP_DEM, addr, val);
218 }
219
220 int msp_write_dsp(struct i2c_client *client, int addr, int val)
221 {
222         return msp_write(client, I2C_MSP_DSP, addr, val);
223 }
224
225 /* ----------------------------------------------------------------------- *
226  * bits  9  8  5 - SCART DSP input Select:
227  *       0  0  0 - SCART 1 to DSP input (reset position)
228  *       0  1  0 - MONO to DSP input
229  *       1  0  0 - SCART 2 to DSP input
230  *       1  1  1 - Mute DSP input
231  *
232  * bits 11 10  6 - SCART 1 Output Select:
233  *       0  0  0 - undefined (reset position)
234  *       0  1  0 - SCART 2 Input to SCART 1 Output (for devices with 2 SCARTS)
235  *       1  0  0 - MONO input to SCART 1 Output
236  *       1  1  0 - SCART 1 DA to SCART 1 Output
237  *       0  0  1 - SCART 2 DA to SCART 1 Output
238  *       0  1  1 - SCART 1 Input to SCART 1 Output
239  *       1  1  1 - Mute SCART 1 Output
240  *
241  * bits 13 12  7 - SCART 2 Output Select (for devices with 2 Output SCART):
242  *       0  0  0 - SCART 1 DA to SCART 2 Output (reset position)
243  *       0  1  0 - SCART 1 Input to SCART 2 Output
244  *       1  0  0 - MONO input to SCART 2 Output
245  *       0  0  1 - SCART 2 DA to SCART 2 Output
246  *       0  1  1 - SCART 2 Input to SCART 2 Output
247  *       1  1  0 - Mute SCART 2 Output
248  *
249  * Bits 4 to 0 should be zero.
250  * ----------------------------------------------------------------------- */
251
252 static int scarts[3][9] = {
253         /* MASK   IN1     IN2     IN3     IN4     IN1_DA  IN2_DA  MONO    MUTE   */
254         /* SCART DSP Input select */
255         { 0x0320, 0x0000, 0x0200, 0x0300, 0x0020, -1,     -1,     0x0100, 0x0320 },
256         /* SCART1 Output select */
257         { 0x0c40, 0x0440, 0x0400, 0x0000, 0x0840, 0x0c00, 0x0040, 0x0800, 0x0c40 },
258         /* SCART2 Output select */
259         { 0x3080, 0x1000, 0x1080, 0x2080, 0x3080, 0x0000, 0x0080, 0x2000, 0x3000 },
260 };
261
262 static char *scart_names[] = {
263         "in1", "in2", "in3", "in4", "in1 da", "in2 da", "mono", "mute"
264 };
265
266 void msp_set_scart(struct i2c_client *client, int in, int out)
267 {
268         struct msp_state *state = to_state(i2c_get_clientdata(client));
269
270         state->in_scart = in;
271
272         if (in >= 0 && in <= 7 && out >= 0 && out <= 2) {
273                 if (-1 == scarts[out][in + 1])
274                         return;
275
276                 state->acb &= ~scarts[out][0];
277                 state->acb |=  scarts[out][in + 1];
278         } else
279                 state->acb = 0xf60; /* Mute Input and SCART 1 Output */
280
281         v4l_dbg(1, msp_debug, client, "scart switch: %s => %d (ACB=0x%04x)\n",
282                                         scart_names[in], out, state->acb);
283         msp_write_dsp(client, 0x13, state->acb);
284
285         /* Sets I2S speed 0 = 1.024 Mbps, 1 = 2.048 Mbps */
286         if (state->has_i2s_conf)
287                 msp_write_dem(client, 0x40, state->i2s_mode);
288 }
289
290 void msp_set_audio(struct i2c_client *client)
291 {
292         struct msp_state *state = to_state(i2c_get_clientdata(client));
293         int bal = 0, bass, treble, loudness;
294         int val = 0;
295         int reallymuted = state->muted | state->scan_in_progress;
296
297         if (!reallymuted)
298                 val = (state->volume * 0x7f / 65535) << 8;
299
300         v4l_dbg(1, msp_debug, client, "mute=%s scanning=%s volume=%d\n",
301                 state->muted ? "on" : "off",
302                 state->scan_in_progress ? "yes" : "no",
303                 state->volume);
304
305         msp_write_dsp(client, 0x0000, val);
306         msp_write_dsp(client, 0x0007, reallymuted ? 0x1 : (val | 0x1));
307         if (state->has_scart2_out_volume)
308                 msp_write_dsp(client, 0x0040, reallymuted ? 0x1 : (val | 0x1));
309         if (state->has_headphones)
310                 msp_write_dsp(client, 0x0006, val);
311         if (!state->has_sound_processing)
312                 return;
313
314         if (val)
315                 bal = (u8)((state->balance / 256) - 128);
316         bass = ((state->bass - 32768) * 0x60 / 65535) << 8;
317         treble = ((state->treble - 32768) * 0x60 / 65535) << 8;
318         loudness = state->loudness ? ((5 * 4) << 8) : 0;
319
320         v4l_dbg(1, msp_debug, client, "balance=%d bass=%d treble=%d loudness=%d\n",
321                 state->balance, state->bass, state->treble, state->loudness);
322
323         msp_write_dsp(client, 0x0001, bal << 8);
324         msp_write_dsp(client, 0x0002, bass);
325         msp_write_dsp(client, 0x0003, treble);
326         msp_write_dsp(client, 0x0004, loudness);
327         if (!state->has_headphones)
328                 return;
329         msp_write_dsp(client, 0x0030, bal << 8);
330         msp_write_dsp(client, 0x0031, bass);
331         msp_write_dsp(client, 0x0032, treble);
332         msp_write_dsp(client, 0x0033, loudness);
333 }
334
335 /* ------------------------------------------------------------------------ */
336
337 static void msp_wake_thread(struct i2c_client *client)
338 {
339         struct msp_state *state = to_state(i2c_get_clientdata(client));
340
341         if (NULL == state->kthread)
342                 return;
343         state->watch_stereo = 0;
344         state->restart = 1;
345         wake_up_interruptible(&state->wq);
346 }
347
348 int msp_sleep(struct msp_state *state, int timeout)
349 {
350         DECLARE_WAITQUEUE(wait, current);
351
352         add_wait_queue(&state->wq, &wait);
353         if (!kthread_should_stop()) {
354                 if (timeout < 0) {
355                         set_current_state(TASK_INTERRUPTIBLE);
356                         schedule();
357                 } else {
358                         schedule_timeout_interruptible
359                                                 (msecs_to_jiffies(timeout));
360                 }
361         }
362
363         remove_wait_queue(&state->wq, &wait);
364         try_to_freeze();
365         return state->restart;
366 }
367
368 /* ------------------------------------------------------------------------ */
369 #ifdef CONFIG_VIDEO_ALLOW_V4L1
370 static int msp_mode_v4l2_to_v4l1(int rxsubchans, int audmode)
371 {
372         if (rxsubchans == V4L2_TUNER_SUB_MONO)
373                 return VIDEO_SOUND_MONO;
374         if (rxsubchans == V4L2_TUNER_SUB_STEREO)
375                 return VIDEO_SOUND_STEREO;
376         if (audmode == V4L2_TUNER_MODE_LANG2)
377                 return VIDEO_SOUND_LANG2;
378         return VIDEO_SOUND_LANG1;
379 }
380
381 static int msp_mode_v4l1_to_v4l2(int mode)
382 {
383         if (mode & VIDEO_SOUND_STEREO)
384                 return V4L2_TUNER_MODE_STEREO;
385         if (mode & VIDEO_SOUND_LANG2)
386                 return V4L2_TUNER_MODE_LANG2;
387         if (mode & VIDEO_SOUND_LANG1)
388                 return V4L2_TUNER_MODE_LANG1;
389         return V4L2_TUNER_MODE_MONO;
390 }
391 #endif
392
393 static int msp_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
394 {
395         struct msp_state *state = to_state(sd);
396
397         switch (ctrl->id) {
398         case V4L2_CID_AUDIO_VOLUME:
399                 ctrl->value = state->volume;
400                 break;
401
402         case V4L2_CID_AUDIO_MUTE:
403                 ctrl->value = state->muted;
404                 break;
405
406         case V4L2_CID_AUDIO_BALANCE:
407                 if (!state->has_sound_processing)
408                         return -EINVAL;
409                 ctrl->value = state->balance;
410                 break;
411
412         case V4L2_CID_AUDIO_BASS:
413                 if (!state->has_sound_processing)
414                         return -EINVAL;
415                 ctrl->value = state->bass;
416                 break;
417
418         case V4L2_CID_AUDIO_TREBLE:
419                 if (!state->has_sound_processing)
420                         return -EINVAL;
421                 ctrl->value = state->treble;
422                 break;
423
424         case V4L2_CID_AUDIO_LOUDNESS:
425                 if (!state->has_sound_processing)
426                         return -EINVAL;
427                 ctrl->value = state->loudness;
428                 break;
429
430         default:
431                 return -EINVAL;
432         }
433         return 0;
434 }
435
436 static int msp_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
437 {
438         struct msp_state *state = to_state(sd);
439         struct i2c_client *client = v4l2_get_subdevdata(sd);
440
441         switch (ctrl->id) {
442         case V4L2_CID_AUDIO_VOLUME:
443                 state->volume = ctrl->value;
444                 if (state->volume == 0)
445                         state->balance = 32768;
446                 break;
447
448         case V4L2_CID_AUDIO_MUTE:
449                 if (ctrl->value < 0 || ctrl->value >= 2)
450                         return -ERANGE;
451                 state->muted = ctrl->value;
452                 break;
453
454         case V4L2_CID_AUDIO_BASS:
455                 if (!state->has_sound_processing)
456                         return -EINVAL;
457                 state->bass = ctrl->value;
458                 break;
459
460         case V4L2_CID_AUDIO_TREBLE:
461                 if (!state->has_sound_processing)
462                         return -EINVAL;
463                 state->treble = ctrl->value;
464                 break;
465
466         case V4L2_CID_AUDIO_LOUDNESS:
467                 if (!state->has_sound_processing)
468                         return -EINVAL;
469                 state->loudness = ctrl->value;
470                 break;
471
472         case V4L2_CID_AUDIO_BALANCE:
473                 if (!state->has_sound_processing)
474                         return -EINVAL;
475                 state->balance = ctrl->value;
476                 break;
477
478         default:
479                 return -EINVAL;
480         }
481         msp_set_audio(client);
482         return 0;
483 }
484
485 #ifdef CONFIG_VIDEO_ALLOW_V4L1
486 static int msp_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
487 {
488         struct msp_state *state = to_state(sd);
489         struct i2c_client *client = v4l2_get_subdevdata(sd);
490
491         switch (cmd) {
492         /* --- v4l ioctls --- */
493         /* take care: bttv does userspace copying, we'll get a
494            kernel pointer here... */
495         case VIDIOCGAUDIO:
496         {
497                 struct video_audio *va = arg;
498
499                 va->flags |= VIDEO_AUDIO_VOLUME | VIDEO_AUDIO_MUTABLE;
500                 if (state->has_sound_processing)
501                         va->flags |= VIDEO_AUDIO_BALANCE |
502                                 VIDEO_AUDIO_BASS |
503                                 VIDEO_AUDIO_TREBLE;
504                 if (state->muted)
505                         va->flags |= VIDEO_AUDIO_MUTE;
506                 va->volume = state->volume;
507                 va->balance = state->volume ? state->balance : 32768;
508                 va->bass = state->bass;
509                 va->treble = state->treble;
510
511                 if (state->radio)
512                         break;
513                 if (state->opmode == OPMODE_AUTOSELECT)
514                         msp_detect_stereo(client);
515                 va->mode = msp_mode_v4l2_to_v4l1(state->rxsubchans, state->audmode);
516                 break;
517         }
518
519         case VIDIOCSAUDIO:
520         {
521                 struct video_audio *va = arg;
522
523                 state->muted = (va->flags & VIDEO_AUDIO_MUTE);
524                 state->volume = va->volume;
525                 state->balance = va->balance;
526                 state->bass = va->bass;
527                 state->treble = va->treble;
528                 msp_set_audio(client);
529
530                 if (va->mode != 0 && state->radio == 0 &&
531                     state->audmode != msp_mode_v4l1_to_v4l2(va->mode)) {
532                         state->audmode = msp_mode_v4l1_to_v4l2(va->mode);
533                         msp_set_audmode(client);
534                 }
535                 break;
536         }
537
538         case VIDIOCSCHAN:
539         {
540                 struct video_channel *vc = arg;
541                 int update = 0;
542                 v4l2_std_id std;
543
544                 if (state->radio)
545                         update = 1;
546                 state->radio = 0;
547                 if (vc->norm == VIDEO_MODE_PAL)
548                         std = V4L2_STD_PAL;
549                 else if (vc->norm == VIDEO_MODE_SECAM)
550                         std = V4L2_STD_SECAM;
551                 else
552                         std = V4L2_STD_NTSC;
553                 if (std != state->v4l2_std) {
554                         state->v4l2_std = std;
555                         update = 1;
556                 }
557                 if (update)
558                         msp_wake_thread(client);
559                 break;
560         }
561
562         case VIDIOCSFREQ:
563         {
564                 /* new channel -- kick audio carrier scan */
565                 msp_wake_thread(client);
566                 break;
567         }
568         default:
569                 return -ENOIOCTLCMD;
570         }
571         return 0;
572 }
573 #endif
574
575 /* --- v4l2 ioctls --- */
576 static int msp_s_radio(struct v4l2_subdev *sd)
577 {
578         struct msp_state *state = to_state(sd);
579         struct i2c_client *client = v4l2_get_subdevdata(sd);
580
581         if (state->radio)
582                 return 0;
583         state->radio = 1;
584         v4l_dbg(1, msp_debug, client, "switching to radio mode\n");
585         state->watch_stereo = 0;
586         switch (state->opmode) {
587         case OPMODE_MANUAL:
588                 /* set msp3400 to FM radio mode */
589                 msp3400c_set_mode(client, MSP_MODE_FM_RADIO);
590                 msp3400c_set_carrier(client, MSP_CARRIER(10.7),
591                                 MSP_CARRIER(10.7));
592                 msp_set_audio(client);
593                 break;
594         case OPMODE_AUTODETECT:
595         case OPMODE_AUTOSELECT:
596                 /* the thread will do for us */
597                 msp_wake_thread(client);
598                 break;
599         }
600         return 0;
601 }
602
603 static int msp_s_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *freq)
604 {
605         struct i2c_client *client = v4l2_get_subdevdata(sd);
606
607         /* new channel -- kick audio carrier scan */
608         msp_wake_thread(client);
609         return 0;
610 }
611
612 static int msp_s_std(struct v4l2_subdev *sd, v4l2_std_id id)
613 {
614         struct msp_state *state = to_state(sd);
615         struct i2c_client *client = v4l2_get_subdevdata(sd);
616         int update = state->radio || state->v4l2_std != id;
617
618         state->v4l2_std = id;
619         state->radio = 0;
620         if (update)
621                 msp_wake_thread(client);
622         return 0;
623 }
624
625 static int msp_s_routing(struct v4l2_subdev *sd, const struct v4l2_routing *rt)
626 {
627         struct msp_state *state = to_state(sd);
628         struct i2c_client *client = v4l2_get_subdevdata(sd);
629         int tuner = (rt->input >> 3) & 1;
630         int sc_in = rt->input & 0x7;
631         int sc1_out = rt->output & 0xf;
632         int sc2_out = (rt->output >> 4) & 0xf;
633         u16 val, reg;
634         int i;
635         int extern_input = 1;
636
637         if (state->routing.input == rt->input &&
638                         state->routing.output == rt->output)
639                 return 0;
640         state->routing = *rt;
641         /* check if the tuner input is used */
642         for (i = 0; i < 5; i++) {
643                 if (((rt->input >> (4 + i * 4)) & 0xf) == 0)
644                         extern_input = 0;
645         }
646         state->mode = extern_input ? MSP_MODE_EXTERN : MSP_MODE_AM_DETECT;
647         state->rxsubchans = V4L2_TUNER_SUB_STEREO;
648         msp_set_scart(client, sc_in, 0);
649         msp_set_scart(client, sc1_out, 1);
650         msp_set_scart(client, sc2_out, 2);
651         msp_set_audmode(client);
652         reg = (state->opmode == OPMODE_AUTOSELECT) ? 0x30 : 0xbb;
653         val = msp_read_dem(client, reg);
654         msp_write_dem(client, reg, (val & ~0x100) | (tuner << 8));
655         /* wake thread when a new input is chosen */
656         msp_wake_thread(client);
657         return 0;
658 }
659
660 static int msp_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
661 {
662         struct msp_state *state = to_state(sd);
663         struct i2c_client *client = v4l2_get_subdevdata(sd);
664
665         if (state->radio)
666                 return 0;
667         if (state->opmode == OPMODE_AUTOSELECT)
668                 msp_detect_stereo(client);
669         vt->audmode    = state->audmode;
670         vt->rxsubchans = state->rxsubchans;
671         vt->capability |= V4L2_TUNER_CAP_STEREO |
672                 V4L2_TUNER_CAP_LANG1 | V4L2_TUNER_CAP_LANG2;
673         return 0;
674 }
675
676 static int msp_s_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
677 {
678         struct msp_state *state = to_state(sd);
679         struct i2c_client *client = v4l2_get_subdevdata(sd);
680
681         if (state->radio)  /* TODO: add mono/stereo support for radio */
682                 return 0;
683         if (state->audmode == vt->audmode)
684                 return 0;
685         state->audmode = vt->audmode;
686         /* only set audmode */
687         msp_set_audmode(client);
688         return 0;
689 }
690
691 static int msp_s_i2s_clock_freq(struct v4l2_subdev *sd, u32 freq)
692 {
693         struct msp_state *state = to_state(sd);
694         struct i2c_client *client = v4l2_get_subdevdata(sd);
695
696         v4l_dbg(1, msp_debug, client, "Setting I2S speed to %d\n", freq);
697
698         switch (freq) {
699                 case 1024000:
700                         state->i2s_mode = 0;
701                         break;
702                 case 2048000:
703                         state->i2s_mode = 1;
704                         break;
705                 default:
706                         return -EINVAL;
707         }
708         return 0;
709 }
710
711 static int msp_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
712 {
713         struct msp_state *state = to_state(sd);
714
715         switch (qc->id) {
716                 case V4L2_CID_AUDIO_VOLUME:
717                 case V4L2_CID_AUDIO_MUTE:
718                         return v4l2_ctrl_query_fill_std(qc);
719                 default:
720                         break;
721         }
722         if (!state->has_sound_processing)
723                 return -EINVAL;
724         switch (qc->id) {
725                 case V4L2_CID_AUDIO_LOUDNESS:
726                 case V4L2_CID_AUDIO_BALANCE:
727                 case V4L2_CID_AUDIO_BASS:
728                 case V4L2_CID_AUDIO_TREBLE:
729                         return v4l2_ctrl_query_fill_std(qc);
730                 default:
731                         return -EINVAL;
732         }
733         return 0;
734 }
735
736 static int msp_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_chip_ident *chip)
737 {
738         struct msp_state *state = to_state(sd);
739         struct i2c_client *client = v4l2_get_subdevdata(sd);
740
741         return v4l2_chip_ident_i2c_client(client, chip, state->ident,
742                         (state->rev1 << 16) | state->rev2);
743 }
744
745 static int msp_log_status(struct v4l2_subdev *sd)
746 {
747         struct msp_state *state = to_state(sd);
748         struct i2c_client *client = v4l2_get_subdevdata(sd);
749         const char *p;
750
751         if (state->opmode == OPMODE_AUTOSELECT)
752                 msp_detect_stereo(client);
753         v4l_info(client, "%s rev1 = 0x%04x rev2 = 0x%04x\n",
754                         client->name, state->rev1, state->rev2);
755         v4l_info(client, "Audio:    volume %d%s\n",
756                         state->volume, state->muted ? " (muted)" : "");
757         if (state->has_sound_processing) {
758                 v4l_info(client, "Audio:    balance %d bass %d treble %d loudness %s\n",
759                                 state->balance, state->bass,
760                                 state->treble,
761                                 state->loudness ? "on" : "off");
762         }
763         switch (state->mode) {
764                 case MSP_MODE_AM_DETECT: p = "AM (for carrier detect)"; break;
765                 case MSP_MODE_FM_RADIO: p = "FM Radio"; break;
766                 case MSP_MODE_FM_TERRA: p = "Terrestial FM-mono/stereo"; break;
767                 case MSP_MODE_FM_SAT: p = "Satellite FM-mono"; break;
768                 case MSP_MODE_FM_NICAM1: p = "NICAM/FM (B/G, D/K)"; break;
769                 case MSP_MODE_FM_NICAM2: p = "NICAM/FM (I)"; break;
770                 case MSP_MODE_AM_NICAM: p = "NICAM/AM (L)"; break;
771                 case MSP_MODE_BTSC: p = "BTSC"; break;
772                 case MSP_MODE_EXTERN: p = "External input"; break;
773                 default: p = "unknown"; break;
774         }
775         if (state->mode == MSP_MODE_EXTERN) {
776                 v4l_info(client, "Mode:     %s\n", p);
777         } else if (state->opmode == OPMODE_MANUAL) {
778                 v4l_info(client, "Mode:     %s (%s%s)\n", p,
779                                 (state->rxsubchans & V4L2_TUNER_SUB_STEREO) ? "stereo" : "mono",
780                                 (state->rxsubchans & V4L2_TUNER_SUB_LANG2) ? ", dual" : "");
781         } else {
782                 if (state->opmode == OPMODE_AUTODETECT)
783                         v4l_info(client, "Mode:     %s\n", p);
784                 v4l_info(client, "Standard: %s (%s%s)\n",
785                                 msp_standard_std_name(state->std),
786                                 (state->rxsubchans & V4L2_TUNER_SUB_STEREO) ? "stereo" : "mono",
787                                 (state->rxsubchans & V4L2_TUNER_SUB_LANG2) ? ", dual" : "");
788         }
789         v4l_info(client, "Audmode:  0x%04x\n", state->audmode);
790         v4l_info(client, "Routing:  0x%08x (input) 0x%08x (output)\n",
791                         state->routing.input, state->routing.output);
792         v4l_info(client, "ACB:      0x%04x\n", state->acb);
793         return 0;
794 }
795
796 static int msp_suspend(struct i2c_client *client, pm_message_t state)
797 {
798         v4l_dbg(1, msp_debug, client, "suspend\n");
799         msp_reset(client);
800         return 0;
801 }
802
803 static int msp_resume(struct i2c_client *client)
804 {
805         v4l_dbg(1, msp_debug, client, "resume\n");
806         msp_wake_thread(client);
807         return 0;
808 }
809
810 static int msp_command(struct i2c_client *client, unsigned cmd, void *arg)
811 {
812         return v4l2_subdev_command(i2c_get_clientdata(client), cmd, arg);
813 }
814
815 /* ----------------------------------------------------------------------- */
816
817 static const struct v4l2_subdev_core_ops msp_core_ops = {
818         .log_status = msp_log_status,
819         .g_chip_ident = msp_g_chip_ident,
820         .g_ctrl = msp_g_ctrl,
821         .s_ctrl = msp_s_ctrl,
822         .queryctrl = msp_queryctrl,
823 #ifdef CONFIG_VIDEO_ALLOW_V4L1
824         .ioctl = msp_ioctl,
825 #endif
826 };
827
828 static const struct v4l2_subdev_tuner_ops msp_tuner_ops = {
829         .s_frequency = msp_s_frequency,
830         .g_tuner = msp_g_tuner,
831         .s_tuner = msp_s_tuner,
832         .s_radio = msp_s_radio,
833         .s_std = msp_s_std,
834 };
835
836 static const struct v4l2_subdev_audio_ops msp_audio_ops = {
837         .s_routing = msp_s_routing,
838         .s_i2s_clock_freq = msp_s_i2s_clock_freq,
839 };
840
841 static const struct v4l2_subdev_ops msp_ops = {
842         .core = &msp_core_ops,
843         .tuner = &msp_tuner_ops,
844         .audio = &msp_audio_ops,
845 };
846
847 /* ----------------------------------------------------------------------- */
848
849 static int msp_probe(struct i2c_client *client, const struct i2c_device_id *id)
850 {
851         struct msp_state *state;
852         struct v4l2_subdev *sd;
853         int (*thread_func)(void *data) = NULL;
854         int msp_hard;
855         int msp_family;
856         int msp_revision;
857         int msp_product, msp_prod_hi, msp_prod_lo;
858         int msp_rom;
859
860         if (!id)
861                 strlcpy(client->name, "msp3400", sizeof(client->name));
862
863         if (msp_reset(client) == -1) {
864                 v4l_dbg(1, msp_debug, client, "msp3400 not found\n");
865                 return -ENODEV;
866         }
867
868         state = kzalloc(sizeof(*state), GFP_KERNEL);
869         if (!state)
870                 return -ENOMEM;
871
872         sd = &state->sd;
873         v4l2_i2c_subdev_init(sd, client, &msp_ops);
874
875         state->v4l2_std = V4L2_STD_NTSC;
876         state->audmode = V4L2_TUNER_MODE_STEREO;
877         state->volume = 58880;  /* 0db gain */
878         state->balance = 32768; /* 0db gain */
879         state->bass = 32768;
880         state->treble = 32768;
881         state->loudness = 0;
882         state->input = -1;
883         state->muted = 0;
884         state->i2s_mode = 0;
885         init_waitqueue_head(&state->wq);
886         /* These are the reset input/output positions */
887         state->routing.input = MSP_INPUT_DEFAULT;
888         state->routing.output = MSP_OUTPUT_DEFAULT;
889
890         state->rev1 = msp_read_dsp(client, 0x1e);
891         if (state->rev1 != -1)
892                 state->rev2 = msp_read_dsp(client, 0x1f);
893         v4l_dbg(1, msp_debug, client, "rev1=0x%04x, rev2=0x%04x\n",
894                         state->rev1, state->rev2);
895         if (state->rev1 == -1 || (state->rev1 == 0 && state->rev2 == 0)) {
896                 v4l_dbg(1, msp_debug, client,
897                                 "not an msp3400 (cannot read chip version)\n");
898                 kfree(state);
899                 return -ENODEV;
900         }
901
902         msp_set_audio(client);
903
904         msp_family = ((state->rev1 >> 4) & 0x0f) + 3;
905         msp_product = (state->rev2 >> 8) & 0xff;
906         msp_prod_hi = msp_product / 10;
907         msp_prod_lo = msp_product % 10;
908         msp_revision = (state->rev1 & 0x0f) + '@';
909         msp_hard = ((state->rev1 >> 8) & 0xff) + '@';
910         msp_rom = state->rev2 & 0x1f;
911         /* Rev B=2, C=3, D=4, G=7 */
912         state->ident = msp_family * 10000 + 4000 + msp_product * 10 +
913                         msp_revision - '@';
914
915         /* Has NICAM support: all mspx41x and mspx45x products have NICAM */
916         state->has_nicam =
917                 msp_prod_hi == 1 || msp_prod_hi == 5;
918         /* Has radio support: was added with revision G */
919         state->has_radio =
920                 msp_revision >= 'G';
921         /* Has headphones output: not for stripped down products */
922         state->has_headphones =
923                 msp_prod_lo < 5;
924         /* Has scart2 input: not in stripped down products of the '3' family */
925         state->has_scart2 =
926                 msp_family >= 4 || msp_prod_lo < 7;
927         /* Has scart3 input: not in stripped down products of the '3' family */
928         state->has_scart3 =
929                 msp_family >= 4 || msp_prod_lo < 5;
930         /* Has scart4 input: not in pre D revisions, not in stripped D revs */
931         state->has_scart4 =
932                 msp_family >= 4 || (msp_revision >= 'D' && msp_prod_lo < 5);
933         /* Has scart2 output: not in stripped down products of
934          * the '3' family */
935         state->has_scart2_out =
936                 msp_family >= 4 || msp_prod_lo < 5;
937         /* Has scart2 a volume control? Not in pre-D revisions. */
938         state->has_scart2_out_volume =
939                 msp_revision > 'C' && state->has_scart2_out;
940         /* Has a configurable i2s out? */
941         state->has_i2s_conf =
942                 msp_revision >= 'G' && msp_prod_lo < 7;
943         /* Has subwoofer output: not in pre-D revs and not in stripped down
944          * products */
945         state->has_subwoofer =
946                 msp_revision >= 'D' && msp_prod_lo < 5;
947         /* Has soundprocessing (bass/treble/balance/loudness/equalizer):
948          *  not in stripped down products */
949         state->has_sound_processing =
950                 msp_prod_lo < 7;
951         /* Has Virtual Dolby Surround: only in msp34x1 */
952         state->has_virtual_dolby_surround =
953                 msp_revision == 'G' && msp_prod_lo == 1;
954         /* Has Virtual Dolby Surround & Dolby Pro Logic: only in msp34x2 */
955         state->has_dolby_pro_logic =
956                 msp_revision == 'G' && msp_prod_lo == 2;
957         /* The msp343xG supports BTSC only and cannot do Automatic Standard
958          * Detection. */
959         state->force_btsc =
960                 msp_family == 3 && msp_revision == 'G' && msp_prod_hi == 3;
961
962         state->opmode = opmode;
963         if (state->opmode == OPMODE_AUTO) {
964                 /* MSP revision G and up have both autodetect and autoselect */
965                 if (msp_revision >= 'G')
966                         state->opmode = OPMODE_AUTOSELECT;
967                 /* MSP revision D and up have autodetect */
968                 else if (msp_revision >= 'D')
969                         state->opmode = OPMODE_AUTODETECT;
970                 else
971                         state->opmode = OPMODE_MANUAL;
972         }
973
974         /* hello world :-) */
975         v4l_info(client, "MSP%d4%02d%c-%c%d found @ 0x%x (%s)\n",
976                         msp_family, msp_product,
977                         msp_revision, msp_hard, msp_rom,
978                         client->addr << 1, client->adapter->name);
979         v4l_info(client, "%s ", client->name);
980         if (state->has_nicam && state->has_radio)
981                 printk(KERN_CONT "supports nicam and radio, ");
982         else if (state->has_nicam)
983                 printk(KERN_CONT "supports nicam, ");
984         else if (state->has_radio)
985                 printk(KERN_CONT "supports radio, ");
986         printk(KERN_CONT "mode is ");
987
988         /* version-specific initialization */
989         switch (state->opmode) {
990         case OPMODE_MANUAL:
991                 printk(KERN_CONT "manual");
992                 thread_func = msp3400c_thread;
993                 break;
994         case OPMODE_AUTODETECT:
995                 printk(KERN_CONT "autodetect");
996                 thread_func = msp3410d_thread;
997                 break;
998         case OPMODE_AUTOSELECT:
999                 printk(KERN_CONT "autodetect and autoselect");
1000                 thread_func = msp34xxg_thread;
1001                 break;
1002         }
1003         printk(KERN_CONT "\n");
1004
1005         /* startup control thread if needed */
1006         if (thread_func) {
1007                 state->kthread = kthread_run(thread_func, client, "msp34xx");
1008
1009                 if (IS_ERR(state->kthread))
1010                         v4l_warn(client, "kernel_thread() failed\n");
1011                 msp_wake_thread(client);
1012         }
1013         return 0;
1014 }
1015
1016 static int msp_remove(struct i2c_client *client)
1017 {
1018         struct msp_state *state = to_state(i2c_get_clientdata(client));
1019
1020         v4l2_device_unregister_subdev(&state->sd);
1021         /* shutdown control thread */
1022         if (state->kthread) {
1023                 state->restart = 1;
1024                 kthread_stop(state->kthread);
1025         }
1026         msp_reset(client);
1027
1028         kfree(state);
1029         return 0;
1030 }
1031
1032 /* ----------------------------------------------------------------------- */
1033
1034 static const struct i2c_device_id msp_id[] = {
1035         { "msp3400", 0 },
1036         { }
1037 };
1038 MODULE_DEVICE_TABLE(i2c, msp_id);
1039
1040 static struct v4l2_i2c_driver_data v4l2_i2c_data = {
1041         .name = "msp3400",
1042         .driverid = I2C_DRIVERID_MSP3400,
1043         .command = msp_command,
1044         .probe = msp_probe,
1045         .remove = msp_remove,
1046         .suspend = msp_suspend,
1047         .resume = msp_resume,
1048         .id_table = msp_id,
1049 };
1050
1051 /*
1052  * Overrides for Emacs so that we follow Linus's tabbing style.
1053  * ---------------------------------------------------------------------------
1054  * Local variables:
1055  * c-basic-offset: 8
1056  * End:
1057  */