GNU Linux-libre 4.19.245-gnu1
[releases.git] / drivers / media / i2c / 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
44
45 #include <linux/kernel.h>
46 #include <linux/module.h>
47 #include <linux/slab.h>
48 #include <linux/i2c.h>
49 #include <linux/kthread.h>
50 #include <linux/freezer.h>
51 #include <linux/videodev2.h>
52 #include <media/v4l2-device.h>
53 #include <media/v4l2-ioctl.h>
54 #include <media/drv-intf/msp3400.h>
55 #include <media/i2c/tvaudio.h>
56 #include "msp3400-driver.h"
57
58 /* ---------------------------------------------------------------------- */
59
60 MODULE_DESCRIPTION("device driver for msp34xx TV sound processor");
61 MODULE_AUTHOR("Gerd Knorr");
62 MODULE_LICENSE("GPL");
63
64 /* module parameters */
65 static int opmode   = OPMODE_AUTO;
66 int msp_debug;           /* msp_debug output */
67 bool msp_once;           /* no continuous stereo monitoring */
68 bool msp_amsound;        /* hard-wire AM sound at 6.5 Hz (france),
69                             the autoscan seems work well only with FM... */
70 int msp_standard = 1;    /* Override auto detect of audio msp_standard,
71                             if needed. */
72 bool msp_dolby;
73
74 int msp_stereo_thresh = 0x190; /* a2 threshold for stereo/bilingual
75                                         (msp34xxg only) 0x00a0-0x03c0 */
76
77 /* read-only */
78 module_param(opmode,           int, 0444);
79
80 /* read-write */
81 module_param_named(once, msp_once,                      bool, 0644);
82 module_param_named(debug, msp_debug,                    int,  0644);
83 module_param_named(stereo_threshold, msp_stereo_thresh, int,  0644);
84 module_param_named(standard, msp_standard,              int,  0644);
85 module_param_named(amsound, msp_amsound,                bool, 0644);
86 module_param_named(dolby, msp_dolby,                    bool, 0644);
87
88 MODULE_PARM_DESC(opmode, "Forces a MSP3400 opmode. 0=Manual, 1=Autodetect, 2=Autodetect and autoselect");
89 MODULE_PARM_DESC(once, "No continuous stereo monitoring");
90 MODULE_PARM_DESC(debug, "Enable debug messages [0-3]");
91 MODULE_PARM_DESC(stereo_threshold, "Sets signal threshold to activate stereo");
92 MODULE_PARM_DESC(standard, "Specify audio standard: 32 = NTSC, 64 = radio, Default: Autodetect");
93 MODULE_PARM_DESC(amsound, "Hardwire AM sound at 6.5Hz (France), FM can autoscan");
94 MODULE_PARM_DESC(dolby, "Activates Dolby processing");
95
96 /* ---------------------------------------------------------------------- */
97
98 /* control subaddress */
99 #define I2C_MSP_CONTROL 0x00
100 /* demodulator unit subaddress */
101 #define I2C_MSP_DEM     0x10
102 /* DSP unit subaddress */
103 #define I2C_MSP_DSP     0x12
104
105
106 /* ----------------------------------------------------------------------- */
107 /* functions for talking to the MSP3400C Sound processor                   */
108
109 int msp_reset(struct i2c_client *client)
110 {
111         /* reset and read revision code */
112         static u8 reset_off[3] = { I2C_MSP_CONTROL, 0x80, 0x00 };
113         static u8 reset_on[3]  = { I2C_MSP_CONTROL, 0x00, 0x00 };
114         static u8 write[3]     = { I2C_MSP_DSP + 1, 0x00, 0x1e };
115         u8 read[2];
116         struct i2c_msg reset[2] = {
117                 {
118                         .addr = client->addr,
119                         .flags = I2C_M_IGNORE_NAK,
120                         .len = 3,
121                         .buf = reset_off
122                 },
123                 {
124                         .addr = client->addr,
125                         .flags = I2C_M_IGNORE_NAK,
126                         .len = 3,
127                         .buf = reset_on
128                 },
129         };
130         struct i2c_msg test[2] = {
131                 {
132                         .addr = client->addr,
133                         .len = 3,
134                         .buf = write
135                 },
136                 {
137                         .addr = client->addr,
138                         .flags = I2C_M_RD,
139                         .len = 2,
140                         .buf = read
141                 },
142         };
143
144         dev_dbg_lvl(&client->dev, 3, msp_debug, "msp_reset\n");
145         if (i2c_transfer(client->adapter, &reset[0], 1) != 1 ||
146             i2c_transfer(client->adapter, &reset[1], 1) != 1 ||
147             i2c_transfer(client->adapter, test, 2) != 2) {
148                 dev_err(&client->dev, "chip reset failed\n");
149                 return -1;
150         }
151         return 0;
152 }
153
154 static int msp_read(struct i2c_client *client, int dev, int addr)
155 {
156         int err, retval;
157         u8 write[3];
158         u8 read[2];
159         struct i2c_msg msgs[2] = {
160                 {
161                         .addr = client->addr,
162                         .len = 3,
163                         .buf = write
164                 },
165                 {
166                         .addr = client->addr,
167                         .flags = I2C_M_RD,
168                         .len = 2,
169                         .buf = read
170                 }
171         };
172
173         write[0] = dev + 1;
174         write[1] = addr >> 8;
175         write[2] = addr & 0xff;
176
177         for (err = 0; err < 3; err++) {
178                 if (i2c_transfer(client->adapter, msgs, 2) == 2)
179                         break;
180                 dev_warn(&client->dev, "I/O error #%d (read 0x%02x/0x%02x)\n", err,
181                        dev, addr);
182                 schedule_timeout_interruptible(msecs_to_jiffies(10));
183         }
184         if (err == 3) {
185                 dev_warn(&client->dev, "resetting chip, sound will go off.\n");
186                 msp_reset(client);
187                 return -1;
188         }
189         retval = read[0] << 8 | read[1];
190         dev_dbg_lvl(&client->dev, 3, msp_debug, "msp_read(0x%x, 0x%x): 0x%x\n",
191                         dev, addr, retval);
192         return retval;
193 }
194
195 int msp_read_dem(struct i2c_client *client, int addr)
196 {
197         return msp_read(client, I2C_MSP_DEM, addr);
198 }
199
200 int msp_read_dsp(struct i2c_client *client, int addr)
201 {
202         return msp_read(client, I2C_MSP_DSP, addr);
203 }
204
205 static int msp_write(struct i2c_client *client, int dev, int addr, int val)
206 {
207         int err;
208         u8 buffer[5];
209
210         buffer[0] = dev;
211         buffer[1] = addr >> 8;
212         buffer[2] = addr &  0xff;
213         buffer[3] = val  >> 8;
214         buffer[4] = val  &  0xff;
215
216         dev_dbg_lvl(&client->dev, 3, msp_debug, "msp_write(0x%x, 0x%x, 0x%x)\n",
217                         dev, addr, val);
218         for (err = 0; err < 3; err++) {
219                 if (i2c_master_send(client, buffer, 5) == 5)
220                         break;
221                 dev_warn(&client->dev, "I/O error #%d (write 0x%02x/0x%02x)\n", err,
222                        dev, addr);
223                 schedule_timeout_interruptible(msecs_to_jiffies(10));
224         }
225         if (err == 3) {
226                 dev_warn(&client->dev, "resetting chip, sound will go off.\n");
227                 msp_reset(client);
228                 return -1;
229         }
230         return 0;
231 }
232
233 int msp_write_dem(struct i2c_client *client, int addr, int val)
234 {
235         return msp_write(client, I2C_MSP_DEM, addr, val);
236 }
237
238 int msp_write_dsp(struct i2c_client *client, int addr, int val)
239 {
240         return msp_write(client, I2C_MSP_DSP, addr, val);
241 }
242
243 /* ----------------------------------------------------------------------- *
244  * bits  9  8  5 - SCART DSP input Select:
245  *       0  0  0 - SCART 1 to DSP input (reset position)
246  *       0  1  0 - MONO to DSP input
247  *       1  0  0 - SCART 2 to DSP input
248  *       1  1  1 - Mute DSP input
249  *
250  * bits 11 10  6 - SCART 1 Output Select:
251  *       0  0  0 - undefined (reset position)
252  *       0  1  0 - SCART 2 Input to SCART 1 Output (for devices with 2 SCARTS)
253  *       1  0  0 - MONO input to SCART 1 Output
254  *       1  1  0 - SCART 1 DA to SCART 1 Output
255  *       0  0  1 - SCART 2 DA to SCART 1 Output
256  *       0  1  1 - SCART 1 Input to SCART 1 Output
257  *       1  1  1 - Mute SCART 1 Output
258  *
259  * bits 13 12  7 - SCART 2 Output Select (for devices with 2 Output SCART):
260  *       0  0  0 - SCART 1 DA to SCART 2 Output (reset position)
261  *       0  1  0 - SCART 1 Input to SCART 2 Output
262  *       1  0  0 - MONO input to SCART 2 Output
263  *       0  0  1 - SCART 2 DA to SCART 2 Output
264  *       0  1  1 - SCART 2 Input to SCART 2 Output
265  *       1  1  0 - Mute SCART 2 Output
266  *
267  * Bits 4 to 0 should be zero.
268  * ----------------------------------------------------------------------- */
269
270 static int scarts[3][9] = {
271         /* MASK   IN1     IN2     IN3     IN4     IN1_DA  IN2_DA  MONO    MUTE   */
272         /* SCART DSP Input select */
273         { 0x0320, 0x0000, 0x0200, 0x0300, 0x0020, -1,     -1,     0x0100, 0x0320 },
274         /* SCART1 Output select */
275         { 0x0c40, 0x0440, 0x0400, 0x0000, 0x0840, 0x0c00, 0x0040, 0x0800, 0x0c40 },
276         /* SCART2 Output select */
277         { 0x3080, 0x1000, 0x1080, 0x2080, 0x3080, 0x0000, 0x0080, 0x2000, 0x3000 },
278 };
279
280 static char *scart_names[] = {
281         "in1", "in2", "in3", "in4", "in1 da", "in2 da", "mono", "mute"
282 };
283
284 void msp_set_scart(struct i2c_client *client, int in, int out)
285 {
286         struct msp_state *state = to_state(i2c_get_clientdata(client));
287
288         state->in_scart = in;
289
290         if (in >= 0 && in <= 7 && out >= 0 && out <= 2) {
291                 if (-1 == scarts[out][in + 1])
292                         return;
293
294                 state->acb &= ~scarts[out][0];
295                 state->acb |=  scarts[out][in + 1];
296         } else
297                 state->acb = 0xf60; /* Mute Input and SCART 1 Output */
298
299         dev_dbg_lvl(&client->dev, 1, msp_debug, "scart switch: %s => %d (ACB=0x%04x)\n",
300                                         scart_names[in], out, state->acb);
301         msp_write_dsp(client, 0x13, state->acb);
302
303         /* Sets I2S speed 0 = 1.024 Mbps, 1 = 2.048 Mbps */
304         if (state->has_i2s_conf)
305                 msp_write_dem(client, 0x40, state->i2s_mode);
306 }
307
308 /* ------------------------------------------------------------------------ */
309
310 static void msp_wake_thread(struct i2c_client *client)
311 {
312         struct msp_state *state = to_state(i2c_get_clientdata(client));
313
314         if (NULL == state->kthread)
315                 return;
316         state->watch_stereo = 0;
317         state->restart = 1;
318         wake_up_interruptible(&state->wq);
319 }
320
321 int msp_sleep(struct msp_state *state, int timeout)
322 {
323         DECLARE_WAITQUEUE(wait, current);
324
325         add_wait_queue(&state->wq, &wait);
326         if (!kthread_should_stop()) {
327                 if (timeout < 0) {
328                         set_current_state(TASK_INTERRUPTIBLE);
329                         schedule();
330                 } else {
331                         schedule_timeout_interruptible
332                                                 (msecs_to_jiffies(timeout));
333                 }
334         }
335
336         remove_wait_queue(&state->wq, &wait);
337         try_to_freeze();
338         return state->restart;
339 }
340
341 /* ------------------------------------------------------------------------ */
342
343 static int msp_s_ctrl(struct v4l2_ctrl *ctrl)
344 {
345         struct msp_state *state = ctrl_to_state(ctrl);
346         struct i2c_client *client = v4l2_get_subdevdata(&state->sd);
347         int val = ctrl->val;
348
349         switch (ctrl->id) {
350         case V4L2_CID_AUDIO_VOLUME: {
351                 /* audio volume cluster */
352                 int reallymuted = state->muted->val | state->scan_in_progress;
353
354                 if (!reallymuted)
355                         val = (val * 0x7f / 65535) << 8;
356
357                 dev_dbg_lvl(&client->dev, 1, msp_debug, "mute=%s scanning=%s volume=%d\n",
358                                 state->muted->val ? "on" : "off",
359                                 state->scan_in_progress ? "yes" : "no",
360                                 state->volume->val);
361
362                 msp_write_dsp(client, 0x0000, val);
363                 msp_write_dsp(client, 0x0007, reallymuted ? 0x1 : (val | 0x1));
364                 if (state->has_scart2_out_volume)
365                         msp_write_dsp(client, 0x0040, reallymuted ? 0x1 : (val | 0x1));
366                 if (state->has_headphones)
367                         msp_write_dsp(client, 0x0006, val);
368                 break;
369         }
370
371         case V4L2_CID_AUDIO_BASS:
372                 val = ((val - 32768) * 0x60 / 65535) << 8;
373                 msp_write_dsp(client, 0x0002, val);
374                 if (state->has_headphones)
375                         msp_write_dsp(client, 0x0031, val);
376                 break;
377
378         case V4L2_CID_AUDIO_TREBLE:
379                 val = ((val - 32768) * 0x60 / 65535) << 8;
380                 msp_write_dsp(client, 0x0003, val);
381                 if (state->has_headphones)
382                         msp_write_dsp(client, 0x0032, val);
383                 break;
384
385         case V4L2_CID_AUDIO_LOUDNESS:
386                 val = val ? ((5 * 4) << 8) : 0;
387                 msp_write_dsp(client, 0x0004, val);
388                 if (state->has_headphones)
389                         msp_write_dsp(client, 0x0033, val);
390                 break;
391
392         case V4L2_CID_AUDIO_BALANCE:
393                 val = (u8)((val / 256) - 128);
394                 msp_write_dsp(client, 0x0001, val << 8);
395                 if (state->has_headphones)
396                         msp_write_dsp(client, 0x0030, val << 8);
397                 break;
398
399         default:
400                 return -EINVAL;
401         }
402         return 0;
403 }
404
405 void msp_update_volume(struct msp_state *state)
406 {
407         /* Force an update of the volume/mute cluster */
408         v4l2_ctrl_lock(state->volume);
409         state->volume->val = state->volume->cur.val;
410         state->muted->val = state->muted->cur.val;
411         msp_s_ctrl(state->volume);
412         v4l2_ctrl_unlock(state->volume);
413 }
414
415 /* --- v4l2 ioctls --- */
416 static int msp_s_radio(struct v4l2_subdev *sd)
417 {
418         struct msp_state *state = to_state(sd);
419         struct i2c_client *client = v4l2_get_subdevdata(sd);
420
421         if (state->radio)
422                 return 0;
423         state->radio = 1;
424         dev_dbg_lvl(&client->dev, 1, msp_debug, "switching to radio mode\n");
425         state->watch_stereo = 0;
426         switch (state->opmode) {
427         case OPMODE_MANUAL:
428                 /* set msp3400 to FM radio mode */
429                 msp3400c_set_mode(client, MSP_MODE_FM_RADIO);
430                 msp3400c_set_carrier(client, MSP_CARRIER(10.7),
431                                 MSP_CARRIER(10.7));
432                 msp_update_volume(state);
433                 break;
434         case OPMODE_AUTODETECT:
435         case OPMODE_AUTOSELECT:
436                 /* the thread will do for us */
437                 msp_wake_thread(client);
438                 break;
439         }
440         return 0;
441 }
442
443 static int msp_s_frequency(struct v4l2_subdev *sd, const struct v4l2_frequency *freq)
444 {
445         struct i2c_client *client = v4l2_get_subdevdata(sd);
446
447         /* new channel -- kick audio carrier scan */
448         msp_wake_thread(client);
449         return 0;
450 }
451
452 static int msp_querystd(struct v4l2_subdev *sd, v4l2_std_id *id)
453 {
454         struct msp_state *state = to_state(sd);
455         struct i2c_client *client = v4l2_get_subdevdata(sd);
456
457         *id &= state->detected_std;
458
459         dev_dbg_lvl(&client->dev, 2, msp_debug,
460                 "detected standard: %s(0x%08Lx)\n",
461                 msp_standard_std_name(state->std), state->detected_std);
462
463         return 0;
464 }
465
466 static int msp_s_std(struct v4l2_subdev *sd, v4l2_std_id id)
467 {
468         struct msp_state *state = to_state(sd);
469         struct i2c_client *client = v4l2_get_subdevdata(sd);
470         int update = state->radio || state->v4l2_std != id;
471
472         state->v4l2_std = id;
473         state->radio = 0;
474         if (update)
475                 msp_wake_thread(client);
476         return 0;
477 }
478
479 static int msp_s_routing(struct v4l2_subdev *sd,
480                          u32 input, u32 output, u32 config)
481 {
482         struct msp_state *state = to_state(sd);
483         struct i2c_client *client = v4l2_get_subdevdata(sd);
484         int tuner = (input >> 3) & 1;
485         int sc_in = input & 0x7;
486         int sc1_out = output & 0xf;
487         int sc2_out = (output >> 4) & 0xf;
488         u16 val, reg;
489         int i;
490         int extern_input = 1;
491
492         if (state->route_in == input && state->route_out == output)
493                 return 0;
494         state->route_in = input;
495         state->route_out = output;
496         /* check if the tuner input is used */
497         for (i = 0; i < 5; i++) {
498                 if (((input >> (4 + i * 4)) & 0xf) == 0)
499                         extern_input = 0;
500         }
501         state->mode = extern_input ? MSP_MODE_EXTERN : MSP_MODE_AM_DETECT;
502         state->rxsubchans = V4L2_TUNER_SUB_STEREO;
503         msp_set_scart(client, sc_in, 0);
504         msp_set_scart(client, sc1_out, 1);
505         msp_set_scart(client, sc2_out, 2);
506         msp_set_audmode(client);
507         reg = (state->opmode == OPMODE_AUTOSELECT) ? 0x30 : 0xbb;
508         val = msp_read_dem(client, reg);
509         msp_write_dem(client, reg, (val & ~0x100) | (tuner << 8));
510         /* wake thread when a new input is chosen */
511         msp_wake_thread(client);
512         return 0;
513 }
514
515 static int msp_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
516 {
517         struct msp_state *state = to_state(sd);
518         struct i2c_client *client = v4l2_get_subdevdata(sd);
519
520         if (vt->type != V4L2_TUNER_ANALOG_TV)
521                 return 0;
522         if (!state->radio) {
523                 if (state->opmode == OPMODE_AUTOSELECT)
524                         msp_detect_stereo(client);
525                 vt->rxsubchans = state->rxsubchans;
526         }
527         vt->audmode = state->audmode;
528         vt->capability |= V4L2_TUNER_CAP_STEREO |
529                 V4L2_TUNER_CAP_LANG1 | V4L2_TUNER_CAP_LANG2;
530         return 0;
531 }
532
533 static int msp_s_tuner(struct v4l2_subdev *sd, const struct v4l2_tuner *vt)
534 {
535         struct msp_state *state = to_state(sd);
536         struct i2c_client *client = v4l2_get_subdevdata(sd);
537
538         if (state->radio)  /* TODO: add mono/stereo support for radio */
539                 return 0;
540         if (state->audmode == vt->audmode)
541                 return 0;
542         state->audmode = vt->audmode;
543         /* only set audmode */
544         msp_set_audmode(client);
545         return 0;
546 }
547
548 static int msp_s_i2s_clock_freq(struct v4l2_subdev *sd, u32 freq)
549 {
550         struct msp_state *state = to_state(sd);
551         struct i2c_client *client = v4l2_get_subdevdata(sd);
552
553         dev_dbg_lvl(&client->dev, 1, msp_debug, "Setting I2S speed to %d\n", freq);
554
555         switch (freq) {
556                 case 1024000:
557                         state->i2s_mode = 0;
558                         break;
559                 case 2048000:
560                         state->i2s_mode = 1;
561                         break;
562                 default:
563                         return -EINVAL;
564         }
565         return 0;
566 }
567
568 static int msp_log_status(struct v4l2_subdev *sd)
569 {
570         struct msp_state *state = to_state(sd);
571         struct i2c_client *client = v4l2_get_subdevdata(sd);
572         const char *p;
573         char prefix[V4L2_SUBDEV_NAME_SIZE + 20];
574
575         if (state->opmode == OPMODE_AUTOSELECT)
576                 msp_detect_stereo(client);
577         dev_info(&client->dev, "%s rev1 = 0x%04x rev2 = 0x%04x\n",
578                         client->name, state->rev1, state->rev2);
579         snprintf(prefix, sizeof(prefix), "%s: Audio:    ", sd->name);
580         v4l2_ctrl_handler_log_status(&state->hdl, prefix);
581         switch (state->mode) {
582                 case MSP_MODE_AM_DETECT: p = "AM (for carrier detect)"; break;
583                 case MSP_MODE_FM_RADIO: p = "FM Radio"; break;
584                 case MSP_MODE_FM_TERRA: p = "Terrestrial FM-mono/stereo"; break;
585                 case MSP_MODE_FM_SAT: p = "Satellite FM-mono"; break;
586                 case MSP_MODE_FM_NICAM1: p = "NICAM/FM (B/G, D/K)"; break;
587                 case MSP_MODE_FM_NICAM2: p = "NICAM/FM (I)"; break;
588                 case MSP_MODE_AM_NICAM: p = "NICAM/AM (L)"; break;
589                 case MSP_MODE_BTSC: p = "BTSC"; break;
590                 case MSP_MODE_EXTERN: p = "External input"; break;
591                 default: p = "unknown"; break;
592         }
593         if (state->mode == MSP_MODE_EXTERN) {
594                 dev_info(&client->dev, "Mode:     %s\n", p);
595         } else if (state->opmode == OPMODE_MANUAL) {
596                 dev_info(&client->dev, "Mode:     %s (%s%s)\n", p,
597                                 (state->rxsubchans & V4L2_TUNER_SUB_STEREO) ? "stereo" : "mono",
598                                 (state->rxsubchans & V4L2_TUNER_SUB_LANG2) ? ", dual" : "");
599         } else {
600                 if (state->opmode == OPMODE_AUTODETECT)
601                         dev_info(&client->dev, "Mode:     %s\n", p);
602                 dev_info(&client->dev, "Standard: %s (%s%s)\n",
603                                 msp_standard_std_name(state->std),
604                                 (state->rxsubchans & V4L2_TUNER_SUB_STEREO) ? "stereo" : "mono",
605                                 (state->rxsubchans & V4L2_TUNER_SUB_LANG2) ? ", dual" : "");
606         }
607         dev_info(&client->dev, "Audmode:  0x%04x\n", state->audmode);
608         dev_info(&client->dev, "Routing:  0x%08x (input) 0x%08x (output)\n",
609                         state->route_in, state->route_out);
610         dev_info(&client->dev, "ACB:      0x%04x\n", state->acb);
611         return 0;
612 }
613
614 #ifdef CONFIG_PM_SLEEP
615 static int msp_suspend(struct device *dev)
616 {
617         struct i2c_client *client = to_i2c_client(dev);
618         dev_dbg_lvl(&client->dev, 1, msp_debug, "suspend\n");
619         msp_reset(client);
620         return 0;
621 }
622
623 static int msp_resume(struct device *dev)
624 {
625         struct i2c_client *client = to_i2c_client(dev);
626         dev_dbg_lvl(&client->dev, 1, msp_debug, "resume\n");
627         msp_wake_thread(client);
628         return 0;
629 }
630 #endif
631
632 /* ----------------------------------------------------------------------- */
633
634 static const struct v4l2_ctrl_ops msp_ctrl_ops = {
635         .s_ctrl = msp_s_ctrl,
636 };
637
638 static const struct v4l2_subdev_core_ops msp_core_ops = {
639         .log_status = msp_log_status,
640 };
641
642 static const struct v4l2_subdev_video_ops msp_video_ops = {
643         .s_std = msp_s_std,
644         .querystd = msp_querystd,
645 };
646
647 static const struct v4l2_subdev_tuner_ops msp_tuner_ops = {
648         .s_frequency = msp_s_frequency,
649         .g_tuner = msp_g_tuner,
650         .s_tuner = msp_s_tuner,
651         .s_radio = msp_s_radio,
652 };
653
654 static const struct v4l2_subdev_audio_ops msp_audio_ops = {
655         .s_routing = msp_s_routing,
656         .s_i2s_clock_freq = msp_s_i2s_clock_freq,
657 };
658
659 static const struct v4l2_subdev_ops msp_ops = {
660         .core = &msp_core_ops,
661         .video = &msp_video_ops,
662         .tuner = &msp_tuner_ops,
663         .audio = &msp_audio_ops,
664 };
665
666 /* ----------------------------------------------------------------------- */
667
668
669 static const char * const opmode_str[] = {
670         [OPMODE_MANUAL] = "manual",
671         [OPMODE_AUTODETECT] = "autodetect",
672         [OPMODE_AUTOSELECT] = "autodetect and autoselect",
673 };
674
675 static int msp_probe(struct i2c_client *client, const struct i2c_device_id *id)
676 {
677         struct msp_state *state;
678         struct v4l2_subdev *sd;
679         struct v4l2_ctrl_handler *hdl;
680         int (*thread_func)(void *data) = NULL;
681         int msp_hard;
682         int msp_family;
683         int msp_revision;
684         int msp_product, msp_prod_hi, msp_prod_lo;
685         int msp_rom;
686 #if defined(CONFIG_MEDIA_CONTROLLER)
687         int ret;
688 #endif
689
690         if (!id)
691                 strlcpy(client->name, "msp3400", sizeof(client->name));
692
693         if (msp_reset(client) == -1) {
694                 dev_dbg_lvl(&client->dev, 1, msp_debug, "msp3400 not found\n");
695                 return -ENODEV;
696         }
697
698         state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL);
699         if (!state)
700                 return -ENOMEM;
701
702         sd = &state->sd;
703         v4l2_i2c_subdev_init(sd, client, &msp_ops);
704
705 #if defined(CONFIG_MEDIA_CONTROLLER)
706         state->pads[IF_AUD_DEC_PAD_IF_INPUT].flags = MEDIA_PAD_FL_SINK;
707         state->pads[IF_AUD_DEC_PAD_OUT].flags = MEDIA_PAD_FL_SOURCE;
708
709         sd->entity.function = MEDIA_ENT_F_IF_AUD_DECODER;
710
711         ret = media_entity_pads_init(&sd->entity, 2, state->pads);
712         if (ret < 0)
713                 return ret;
714 #endif
715
716         state->v4l2_std = V4L2_STD_NTSC;
717         state->detected_std = V4L2_STD_ALL;
718         state->audmode = V4L2_TUNER_MODE_STEREO;
719         state->input = -1;
720         state->i2s_mode = 0;
721         init_waitqueue_head(&state->wq);
722         /* These are the reset input/output positions */
723         state->route_in = MSP_INPUT_DEFAULT;
724         state->route_out = MSP_OUTPUT_DEFAULT;
725
726         state->rev1 = msp_read_dsp(client, 0x1e);
727         if (state->rev1 != -1)
728                 state->rev2 = msp_read_dsp(client, 0x1f);
729         dev_dbg_lvl(&client->dev, 1, msp_debug, "rev1=0x%04x, rev2=0x%04x\n",
730                         state->rev1, state->rev2);
731         if (state->rev1 == -1 || (state->rev1 == 0 && state->rev2 == 0)) {
732                 dev_dbg_lvl(&client->dev, 1, msp_debug,
733                                 "not an msp3400 (cannot read chip version)\n");
734                 return -ENODEV;
735         }
736
737         msp_family = ((state->rev1 >> 4) & 0x0f) + 3;
738         msp_product = (state->rev2 >> 8) & 0xff;
739         msp_prod_hi = msp_product / 10;
740         msp_prod_lo = msp_product % 10;
741         msp_revision = (state->rev1 & 0x0f) + '@';
742         msp_hard = ((state->rev1 >> 8) & 0xff) + '@';
743         msp_rom = state->rev2 & 0x1f;
744         /* Rev B=2, C=3, D=4, G=7 */
745         state->ident = msp_family * 10000 + 4000 + msp_product * 10 +
746                         msp_revision - '@';
747
748         /* Has NICAM support: all mspx41x and mspx45x products have NICAM */
749         state->has_nicam =
750                 msp_prod_hi == 1 || msp_prod_hi == 5;
751         /* Has radio support: was added with revision G */
752         state->has_radio =
753                 msp_revision >= 'G';
754         /* Has headphones output: not for stripped down products */
755         state->has_headphones =
756                 msp_prod_lo < 5;
757         /* Has scart2 input: not in stripped down products of the '3' family */
758         state->has_scart2 =
759                 msp_family >= 4 || msp_prod_lo < 7;
760         /* Has scart3 input: not in stripped down products of the '3' family */
761         state->has_scart3 =
762                 msp_family >= 4 || msp_prod_lo < 5;
763         /* Has scart4 input: not in pre D revisions, not in stripped D revs */
764         state->has_scart4 =
765                 msp_family >= 4 || (msp_revision >= 'D' && msp_prod_lo < 5);
766         /* Has scart2 output: not in stripped down products of
767          * the '3' family */
768         state->has_scart2_out =
769                 msp_family >= 4 || msp_prod_lo < 5;
770         /* Has scart2 a volume control? Not in pre-D revisions. */
771         state->has_scart2_out_volume =
772                 msp_revision > 'C' && state->has_scart2_out;
773         /* Has a configurable i2s out? */
774         state->has_i2s_conf =
775                 msp_revision >= 'G' && msp_prod_lo < 7;
776         /* Has subwoofer output: not in pre-D revs and not in stripped down
777          * products */
778         state->has_subwoofer =
779                 msp_revision >= 'D' && msp_prod_lo < 5;
780         /* Has soundprocessing (bass/treble/balance/loudness/equalizer):
781          *  not in stripped down products */
782         state->has_sound_processing =
783                 msp_prod_lo < 7;
784         /* Has Virtual Dolby Surround: only in msp34x1 */
785         state->has_virtual_dolby_surround =
786                 msp_revision == 'G' && msp_prod_lo == 1;
787         /* Has Virtual Dolby Surround & Dolby Pro Logic: only in msp34x2 */
788         state->has_dolby_pro_logic =
789                 msp_revision == 'G' && msp_prod_lo == 2;
790         /* The msp343xG supports BTSC only and cannot do Automatic Standard
791          * Detection. */
792         state->force_btsc =
793                 msp_family == 3 && msp_revision == 'G' && msp_prod_hi == 3;
794
795         state->opmode = opmode;
796         if (state->opmode < OPMODE_MANUAL
797             || state->opmode > OPMODE_AUTOSELECT) {
798                 /* MSP revision G and up have both autodetect and autoselect */
799                 if (msp_revision >= 'G')
800                         state->opmode = OPMODE_AUTOSELECT;
801                 /* MSP revision D and up have autodetect */
802                 else if (msp_revision >= 'D')
803                         state->opmode = OPMODE_AUTODETECT;
804                 else
805                         state->opmode = OPMODE_MANUAL;
806         }
807
808         hdl = &state->hdl;
809         v4l2_ctrl_handler_init(hdl, 6);
810         if (state->has_sound_processing) {
811                 v4l2_ctrl_new_std(hdl, &msp_ctrl_ops,
812                         V4L2_CID_AUDIO_BASS, 0, 65535, 65535 / 100, 32768);
813                 v4l2_ctrl_new_std(hdl, &msp_ctrl_ops,
814                         V4L2_CID_AUDIO_TREBLE, 0, 65535, 65535 / 100, 32768);
815                 v4l2_ctrl_new_std(hdl, &msp_ctrl_ops,
816                         V4L2_CID_AUDIO_LOUDNESS, 0, 1, 1, 0);
817         }
818         state->volume = v4l2_ctrl_new_std(hdl, &msp_ctrl_ops,
819                         V4L2_CID_AUDIO_VOLUME, 0, 65535, 65535 / 100, 58880);
820         v4l2_ctrl_new_std(hdl, &msp_ctrl_ops,
821                         V4L2_CID_AUDIO_BALANCE, 0, 65535, 65535 / 100, 32768);
822         state->muted = v4l2_ctrl_new_std(hdl, &msp_ctrl_ops,
823                         V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
824         sd->ctrl_handler = hdl;
825         if (hdl->error) {
826                 int err = hdl->error;
827
828                 v4l2_ctrl_handler_free(hdl);
829                 return err;
830         }
831
832         v4l2_ctrl_cluster(2, &state->volume);
833         v4l2_ctrl_handler_setup(hdl);
834
835         dev_info(&client->dev,
836                  "MSP%d4%02d%c-%c%d found on %s: supports %s%s%s, mode is %s\n",
837                  msp_family, msp_product,
838                  msp_revision, msp_hard, msp_rom,
839                  client->adapter->name,
840                  (state->has_nicam) ? "nicam" : "",
841                  (state->has_nicam && state->has_radio) ? " and " : "",
842                  (state->has_radio) ? "radio" : "",
843                  opmode_str[state->opmode]);
844
845         /* version-specific initialization */
846         switch (state->opmode) {
847         case OPMODE_MANUAL:
848                 thread_func = msp3400c_thread;
849                 break;
850         case OPMODE_AUTODETECT:
851                 thread_func = msp3410d_thread;
852                 break;
853         case OPMODE_AUTOSELECT:
854                 thread_func = msp34xxg_thread;
855                 break;
856         }
857
858         /* startup control thread if needed */
859         if (thread_func) {
860                 state->kthread = kthread_run(thread_func, client, "msp34xx");
861
862                 if (IS_ERR(state->kthread))
863                         dev_warn(&client->dev, "kernel_thread() failed\n");
864                 msp_wake_thread(client);
865         }
866         return 0;
867 }
868
869 static int msp_remove(struct i2c_client *client)
870 {
871         struct msp_state *state = to_state(i2c_get_clientdata(client));
872
873         v4l2_device_unregister_subdev(&state->sd);
874         /* shutdown control thread */
875         if (state->kthread) {
876                 state->restart = 1;
877                 kthread_stop(state->kthread);
878         }
879         msp_reset(client);
880
881         v4l2_ctrl_handler_free(&state->hdl);
882         return 0;
883 }
884
885 /* ----------------------------------------------------------------------- */
886
887 static const struct dev_pm_ops msp3400_pm_ops = {
888         SET_SYSTEM_SLEEP_PM_OPS(msp_suspend, msp_resume)
889 };
890
891 static const struct i2c_device_id msp_id[] = {
892         { "msp3400", 0 },
893         { }
894 };
895 MODULE_DEVICE_TABLE(i2c, msp_id);
896
897 static struct i2c_driver msp_driver = {
898         .driver = {
899                 .name   = "msp3400",
900                 .pm     = &msp3400_pm_ops,
901         },
902         .probe          = msp_probe,
903         .remove         = msp_remove,
904         .id_table       = msp_id,
905 };
906
907 module_i2c_driver(msp_driver);