Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Programming the mspx4xx sound processor family
0004  *
0005  * (c) 1997-2001 Gerd Knorr <kraxel@bytesex.org>
0006  *
0007  * what works and what doesn't:
0008  *
0009  *  AM-Mono
0010  *      Support for Hauppauge cards added (decoding handled by tuner) added by
0011  *      Frederic Crozat <fcrozat@mail.dotcom.fr>
0012  *
0013  *  FM-Mono
0014  *      should work. The stereo modes are backward compatible to FM-mono,
0015  *      therefore FM-Mono should be always available.
0016  *
0017  *  FM-Stereo (B/G, used in germany)
0018  *      should work, with autodetect
0019  *
0020  *  FM-Stereo (satellite)
0021  *      should work, no autodetect (i.e. default is mono, but you can
0022  *      switch to stereo -- untested)
0023  *
0024  *  NICAM (B/G, L , used in UK, Scandinavia, Spain and France)
0025  *      should work, with autodetect. Support for NICAM was added by
0026  *      Pekka Pietikainen <pp@netppl.fi>
0027  *
0028  * TODO:
0029  *   - better SAT support
0030  *
0031  * 980623  Thomas Sailer (sailer@ife.ee.ethz.ch)
0032  *         using soundcore instead of OSS
0033  */
0034 
0035 
0036 #include <linux/kernel.h>
0037 #include <linux/module.h>
0038 #include <linux/slab.h>
0039 #include <linux/i2c.h>
0040 #include <linux/kthread.h>
0041 #include <linux/freezer.h>
0042 #include <linux/videodev2.h>
0043 #include <media/v4l2-device.h>
0044 #include <media/v4l2-ioctl.h>
0045 #include <media/drv-intf/msp3400.h>
0046 #include <media/i2c/tvaudio.h>
0047 #include "msp3400-driver.h"
0048 
0049 /* ---------------------------------------------------------------------- */
0050 
0051 MODULE_DESCRIPTION("device driver for msp34xx TV sound processor");
0052 MODULE_AUTHOR("Gerd Knorr");
0053 MODULE_LICENSE("GPL");
0054 
0055 /* module parameters */
0056 static int opmode   = OPMODE_AUTO;
0057 int msp_debug;       /* msp_debug output */
0058 bool msp_once;       /* no continuous stereo monitoring */
0059 bool msp_amsound;    /* hard-wire AM sound at 6.5 Hz (france),
0060                 the autoscan seems work well only with FM... */
0061 int msp_standard = 1;    /* Override auto detect of audio msp_standard,
0062                 if needed. */
0063 bool msp_dolby;
0064 
0065 int msp_stereo_thresh = 0x190; /* a2 threshold for stereo/bilingual
0066                     (msp34xxg only) 0x00a0-0x03c0 */
0067 
0068 /* read-only */
0069 module_param(opmode,           int, 0444);
0070 
0071 /* read-write */
0072 module_param_named(once, msp_once,                      bool, 0644);
0073 module_param_named(debug, msp_debug,                    int,  0644);
0074 module_param_named(stereo_threshold, msp_stereo_thresh, int,  0644);
0075 module_param_named(standard, msp_standard,              int,  0644);
0076 module_param_named(amsound, msp_amsound,                bool, 0644);
0077 module_param_named(dolby, msp_dolby,                    bool, 0644);
0078 
0079 MODULE_PARM_DESC(opmode, "Forces a MSP3400 opmode. 0=Manual, 1=Autodetect, 2=Autodetect and autoselect");
0080 MODULE_PARM_DESC(once, "No continuous stereo monitoring");
0081 MODULE_PARM_DESC(debug, "Enable debug messages [0-3]");
0082 MODULE_PARM_DESC(stereo_threshold, "Sets signal threshold to activate stereo");
0083 MODULE_PARM_DESC(standard, "Specify audio standard: 32 = NTSC, 64 = radio, Default: Autodetect");
0084 MODULE_PARM_DESC(amsound, "Hardwire AM sound at 6.5Hz (France), FM can autoscan");
0085 MODULE_PARM_DESC(dolby, "Activates Dolby processing");
0086 
0087 /* ---------------------------------------------------------------------- */
0088 
0089 /* control subaddress */
0090 #define I2C_MSP_CONTROL 0x00
0091 /* demodulator unit subaddress */
0092 #define I2C_MSP_DEM     0x10
0093 /* DSP unit subaddress */
0094 #define I2C_MSP_DSP     0x12
0095 
0096 
0097 /* ----------------------------------------------------------------------- */
0098 /* functions for talking to the MSP3400C Sound processor                   */
0099 
0100 int msp_reset(struct i2c_client *client)
0101 {
0102     /* reset and read revision code */
0103     static u8 reset_off[3] = { I2C_MSP_CONTROL, 0x80, 0x00 };
0104     static u8 reset_on[3]  = { I2C_MSP_CONTROL, 0x00, 0x00 };
0105     static u8 write[3]     = { I2C_MSP_DSP + 1, 0x00, 0x1e };
0106     u8 read[2];
0107     struct i2c_msg reset[2] = {
0108         {
0109             .addr = client->addr,
0110             .flags = I2C_M_IGNORE_NAK,
0111             .len = 3,
0112             .buf = reset_off
0113         },
0114         {
0115             .addr = client->addr,
0116             .flags = I2C_M_IGNORE_NAK,
0117             .len = 3,
0118             .buf = reset_on
0119         },
0120     };
0121     struct i2c_msg test[2] = {
0122         {
0123             .addr = client->addr,
0124             .len = 3,
0125             .buf = write
0126         },
0127         {
0128             .addr = client->addr,
0129             .flags = I2C_M_RD,
0130             .len = 2,
0131             .buf = read
0132         },
0133     };
0134 
0135     dev_dbg_lvl(&client->dev, 3, msp_debug, "msp_reset\n");
0136     if (i2c_transfer(client->adapter, &reset[0], 1) != 1 ||
0137         i2c_transfer(client->adapter, &reset[1], 1) != 1 ||
0138         i2c_transfer(client->adapter, test, 2) != 2) {
0139         dev_err(&client->dev, "chip reset failed\n");
0140         return -1;
0141     }
0142     return 0;
0143 }
0144 
0145 static int msp_read(struct i2c_client *client, int dev, int addr)
0146 {
0147     int err, retval;
0148     u8 write[3];
0149     u8 read[2];
0150     struct i2c_msg msgs[2] = {
0151         {
0152             .addr = client->addr,
0153             .len = 3,
0154             .buf = write
0155         },
0156         {
0157             .addr = client->addr,
0158             .flags = I2C_M_RD,
0159             .len = 2,
0160             .buf = read
0161         }
0162     };
0163 
0164     write[0] = dev + 1;
0165     write[1] = addr >> 8;
0166     write[2] = addr & 0xff;
0167 
0168     for (err = 0; err < 3; err++) {
0169         if (i2c_transfer(client->adapter, msgs, 2) == 2)
0170             break;
0171         dev_warn(&client->dev, "I/O error #%d (read 0x%02x/0x%02x)\n", err,
0172                dev, addr);
0173         schedule_timeout_interruptible(msecs_to_jiffies(10));
0174     }
0175     if (err == 3) {
0176         dev_warn(&client->dev, "resetting chip, sound will go off.\n");
0177         msp_reset(client);
0178         return -1;
0179     }
0180     retval = read[0] << 8 | read[1];
0181     dev_dbg_lvl(&client->dev, 3, msp_debug, "msp_read(0x%x, 0x%x): 0x%x\n",
0182             dev, addr, retval);
0183     return retval;
0184 }
0185 
0186 int msp_read_dem(struct i2c_client *client, int addr)
0187 {
0188     return msp_read(client, I2C_MSP_DEM, addr);
0189 }
0190 
0191 int msp_read_dsp(struct i2c_client *client, int addr)
0192 {
0193     return msp_read(client, I2C_MSP_DSP, addr);
0194 }
0195 
0196 static int msp_write(struct i2c_client *client, int dev, int addr, int val)
0197 {
0198     int err;
0199     u8 buffer[5];
0200 
0201     buffer[0] = dev;
0202     buffer[1] = addr >> 8;
0203     buffer[2] = addr &  0xff;
0204     buffer[3] = val  >> 8;
0205     buffer[4] = val  &  0xff;
0206 
0207     dev_dbg_lvl(&client->dev, 3, msp_debug, "msp_write(0x%x, 0x%x, 0x%x)\n",
0208             dev, addr, val);
0209     for (err = 0; err < 3; err++) {
0210         if (i2c_master_send(client, buffer, 5) == 5)
0211             break;
0212         dev_warn(&client->dev, "I/O error #%d (write 0x%02x/0x%02x)\n", err,
0213                dev, addr);
0214         schedule_timeout_interruptible(msecs_to_jiffies(10));
0215     }
0216     if (err == 3) {
0217         dev_warn(&client->dev, "resetting chip, sound will go off.\n");
0218         msp_reset(client);
0219         return -1;
0220     }
0221     return 0;
0222 }
0223 
0224 int msp_write_dem(struct i2c_client *client, int addr, int val)
0225 {
0226     return msp_write(client, I2C_MSP_DEM, addr, val);
0227 }
0228 
0229 int msp_write_dsp(struct i2c_client *client, int addr, int val)
0230 {
0231     return msp_write(client, I2C_MSP_DSP, addr, val);
0232 }
0233 
0234 /* ----------------------------------------------------------------------- *
0235  * bits  9  8  5 - SCART DSP input Select:
0236  *       0  0  0 - SCART 1 to DSP input (reset position)
0237  *       0  1  0 - MONO to DSP input
0238  *       1  0  0 - SCART 2 to DSP input
0239  *       1  1  1 - Mute DSP input
0240  *
0241  * bits 11 10  6 - SCART 1 Output Select:
0242  *       0  0  0 - undefined (reset position)
0243  *       0  1  0 - SCART 2 Input to SCART 1 Output (for devices with 2 SCARTS)
0244  *       1  0  0 - MONO input to SCART 1 Output
0245  *       1  1  0 - SCART 1 DA to SCART 1 Output
0246  *       0  0  1 - SCART 2 DA to SCART 1 Output
0247  *       0  1  1 - SCART 1 Input to SCART 1 Output
0248  *       1  1  1 - Mute SCART 1 Output
0249  *
0250  * bits 13 12  7 - SCART 2 Output Select (for devices with 2 Output SCART):
0251  *       0  0  0 - SCART 1 DA to SCART 2 Output (reset position)
0252  *       0  1  0 - SCART 1 Input to SCART 2 Output
0253  *       1  0  0 - MONO input to SCART 2 Output
0254  *       0  0  1 - SCART 2 DA to SCART 2 Output
0255  *       0  1  1 - SCART 2 Input to SCART 2 Output
0256  *       1  1  0 - Mute SCART 2 Output
0257  *
0258  * Bits 4 to 0 should be zero.
0259  * ----------------------------------------------------------------------- */
0260 
0261 static int scarts[3][9] = {
0262     /* MASK   IN1     IN2     IN3     IN4     IN1_DA  IN2_DA  MONO    MUTE   */
0263     /* SCART DSP Input select */
0264     { 0x0320, 0x0000, 0x0200, 0x0300, 0x0020, -1,     -1,     0x0100, 0x0320 },
0265     /* SCART1 Output select */
0266     { 0x0c40, 0x0440, 0x0400, 0x0000, 0x0840, 0x0c00, 0x0040, 0x0800, 0x0c40 },
0267     /* SCART2 Output select */
0268     { 0x3080, 0x1000, 0x1080, 0x2080, 0x3080, 0x0000, 0x0080, 0x2000, 0x3000 },
0269 };
0270 
0271 static char *scart_names[] = {
0272     "in1", "in2", "in3", "in4", "in1 da", "in2 da", "mono", "mute"
0273 };
0274 
0275 void msp_set_scart(struct i2c_client *client, int in, int out)
0276 {
0277     struct msp_state *state = to_state(i2c_get_clientdata(client));
0278 
0279     state->in_scart = in;
0280 
0281     if (in >= 0 && in <= 7 && out >= 0 && out <= 2) {
0282         if (-1 == scarts[out][in + 1])
0283             return;
0284 
0285         state->acb &= ~scarts[out][0];
0286         state->acb |=  scarts[out][in + 1];
0287     } else
0288         state->acb = 0xf60; /* Mute Input and SCART 1 Output */
0289 
0290     dev_dbg_lvl(&client->dev, 1, msp_debug, "scart switch: %s => %d (ACB=0x%04x)\n",
0291                     scart_names[in], out, state->acb);
0292     msp_write_dsp(client, 0x13, state->acb);
0293 
0294     /* Sets I2S speed 0 = 1.024 Mbps, 1 = 2.048 Mbps */
0295     if (state->has_i2s_conf)
0296         msp_write_dem(client, 0x40, state->i2s_mode);
0297 }
0298 
0299 /* ------------------------------------------------------------------------ */
0300 
0301 static void msp_wake_thread(struct i2c_client *client)
0302 {
0303     struct msp_state *state = to_state(i2c_get_clientdata(client));
0304 
0305     if (NULL == state->kthread)
0306         return;
0307     state->watch_stereo = 0;
0308     state->restart = 1;
0309     wake_up_interruptible(&state->wq);
0310 }
0311 
0312 int msp_sleep(struct msp_state *state, int timeout)
0313 {
0314     DECLARE_WAITQUEUE(wait, current);
0315 
0316     add_wait_queue(&state->wq, &wait);
0317     if (!kthread_should_stop()) {
0318         if (timeout < 0) {
0319             set_current_state(TASK_INTERRUPTIBLE);
0320             schedule();
0321         } else {
0322             schedule_timeout_interruptible
0323                         (msecs_to_jiffies(timeout));
0324         }
0325     }
0326 
0327     remove_wait_queue(&state->wq, &wait);
0328     try_to_freeze();
0329     return state->restart;
0330 }
0331 
0332 /* ------------------------------------------------------------------------ */
0333 
0334 static int msp_s_ctrl(struct v4l2_ctrl *ctrl)
0335 {
0336     struct msp_state *state = ctrl_to_state(ctrl);
0337     struct i2c_client *client = v4l2_get_subdevdata(&state->sd);
0338     int val = ctrl->val;
0339 
0340     switch (ctrl->id) {
0341     case V4L2_CID_AUDIO_VOLUME: {
0342         /* audio volume cluster */
0343         int reallymuted = state->muted->val | state->scan_in_progress;
0344 
0345         if (!reallymuted)
0346             val = (val * 0x7f / 65535) << 8;
0347 
0348         dev_dbg_lvl(&client->dev, 1, msp_debug, "mute=%s scanning=%s volume=%d\n",
0349                 state->muted->val ? "on" : "off",
0350                 state->scan_in_progress ? "yes" : "no",
0351                 state->volume->val);
0352 
0353         msp_write_dsp(client, 0x0000, val);
0354         msp_write_dsp(client, 0x0007, reallymuted ? 0x1 : (val | 0x1));
0355         if (state->has_scart2_out_volume)
0356             msp_write_dsp(client, 0x0040, reallymuted ? 0x1 : (val | 0x1));
0357         if (state->has_headphones)
0358             msp_write_dsp(client, 0x0006, val);
0359         break;
0360     }
0361 
0362     case V4L2_CID_AUDIO_BASS:
0363         val = ((val - 32768) * 0x60 / 65535) << 8;
0364         msp_write_dsp(client, 0x0002, val);
0365         if (state->has_headphones)
0366             msp_write_dsp(client, 0x0031, val);
0367         break;
0368 
0369     case V4L2_CID_AUDIO_TREBLE:
0370         val = ((val - 32768) * 0x60 / 65535) << 8;
0371         msp_write_dsp(client, 0x0003, val);
0372         if (state->has_headphones)
0373             msp_write_dsp(client, 0x0032, val);
0374         break;
0375 
0376     case V4L2_CID_AUDIO_LOUDNESS:
0377         val = val ? ((5 * 4) << 8) : 0;
0378         msp_write_dsp(client, 0x0004, val);
0379         if (state->has_headphones)
0380             msp_write_dsp(client, 0x0033, val);
0381         break;
0382 
0383     case V4L2_CID_AUDIO_BALANCE:
0384         val = (u8)((val / 256) - 128);
0385         msp_write_dsp(client, 0x0001, val << 8);
0386         if (state->has_headphones)
0387             msp_write_dsp(client, 0x0030, val << 8);
0388         break;
0389 
0390     default:
0391         return -EINVAL;
0392     }
0393     return 0;
0394 }
0395 
0396 void msp_update_volume(struct msp_state *state)
0397 {
0398     /* Force an update of the volume/mute cluster */
0399     v4l2_ctrl_lock(state->volume);
0400     state->volume->val = state->volume->cur.val;
0401     state->muted->val = state->muted->cur.val;
0402     msp_s_ctrl(state->volume);
0403     v4l2_ctrl_unlock(state->volume);
0404 }
0405 
0406 /* --- v4l2 ioctls --- */
0407 static int msp_s_radio(struct v4l2_subdev *sd)
0408 {
0409     struct msp_state *state = to_state(sd);
0410     struct i2c_client *client = v4l2_get_subdevdata(sd);
0411 
0412     if (state->radio)
0413         return 0;
0414     state->radio = 1;
0415     dev_dbg_lvl(&client->dev, 1, msp_debug, "switching to radio mode\n");
0416     state->watch_stereo = 0;
0417     switch (state->opmode) {
0418     case OPMODE_MANUAL:
0419         /* set msp3400 to FM radio mode */
0420         msp3400c_set_mode(client, MSP_MODE_FM_RADIO);
0421         msp3400c_set_carrier(client, MSP_CARRIER(10.7),
0422                 MSP_CARRIER(10.7));
0423         msp_update_volume(state);
0424         break;
0425     case OPMODE_AUTODETECT:
0426     case OPMODE_AUTOSELECT:
0427         /* the thread will do for us */
0428         msp_wake_thread(client);
0429         break;
0430     }
0431     return 0;
0432 }
0433 
0434 static int msp_s_frequency(struct v4l2_subdev *sd, const struct v4l2_frequency *freq)
0435 {
0436     struct i2c_client *client = v4l2_get_subdevdata(sd);
0437 
0438     /* new channel -- kick audio carrier scan */
0439     msp_wake_thread(client);
0440     return 0;
0441 }
0442 
0443 static int msp_querystd(struct v4l2_subdev *sd, v4l2_std_id *id)
0444 {
0445     struct msp_state *state = to_state(sd);
0446     struct i2c_client *client = v4l2_get_subdevdata(sd);
0447 
0448     *id &= state->detected_std;
0449 
0450     dev_dbg_lvl(&client->dev, 2, msp_debug,
0451         "detected standard: %s(0x%08Lx)\n",
0452         msp_standard_std_name(state->std), state->detected_std);
0453 
0454     return 0;
0455 }
0456 
0457 static int msp_s_std(struct v4l2_subdev *sd, v4l2_std_id id)
0458 {
0459     struct msp_state *state = to_state(sd);
0460     struct i2c_client *client = v4l2_get_subdevdata(sd);
0461     int update = state->radio || state->v4l2_std != id;
0462 
0463     state->v4l2_std = id;
0464     state->radio = 0;
0465     if (update)
0466         msp_wake_thread(client);
0467     return 0;
0468 }
0469 
0470 static int msp_s_routing(struct v4l2_subdev *sd,
0471              u32 input, u32 output, u32 config)
0472 {
0473     struct msp_state *state = to_state(sd);
0474     struct i2c_client *client = v4l2_get_subdevdata(sd);
0475     int tuner = (input >> 3) & 1;
0476     int sc_in = input & 0x7;
0477     int sc1_out = output & 0xf;
0478     int sc2_out = (output >> 4) & 0xf;
0479     u16 val, reg;
0480     int i;
0481     int extern_input = 1;
0482 
0483     if (state->route_in == input && state->route_out == output)
0484         return 0;
0485     state->route_in = input;
0486     state->route_out = output;
0487     /* check if the tuner input is used */
0488     for (i = 0; i < 5; i++) {
0489         if (((input >> (4 + i * 4)) & 0xf) == 0)
0490             extern_input = 0;
0491     }
0492     state->mode = extern_input ? MSP_MODE_EXTERN : MSP_MODE_AM_DETECT;
0493     state->rxsubchans = V4L2_TUNER_SUB_STEREO;
0494     msp_set_scart(client, sc_in, 0);
0495     msp_set_scart(client, sc1_out, 1);
0496     msp_set_scart(client, sc2_out, 2);
0497     msp_set_audmode(client);
0498     reg = (state->opmode == OPMODE_AUTOSELECT) ? 0x30 : 0xbb;
0499     val = msp_read_dem(client, reg);
0500     msp_write_dem(client, reg, (val & ~0x100) | (tuner << 8));
0501     /* wake thread when a new input is chosen */
0502     msp_wake_thread(client);
0503     return 0;
0504 }
0505 
0506 static int msp_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
0507 {
0508     struct msp_state *state = to_state(sd);
0509     struct i2c_client *client = v4l2_get_subdevdata(sd);
0510 
0511     if (vt->type != V4L2_TUNER_ANALOG_TV)
0512         return 0;
0513     if (!state->radio) {
0514         if (state->opmode == OPMODE_AUTOSELECT)
0515             msp_detect_stereo(client);
0516         vt->rxsubchans = state->rxsubchans;
0517     }
0518     vt->audmode = state->audmode;
0519     vt->capability |= V4L2_TUNER_CAP_STEREO |
0520         V4L2_TUNER_CAP_LANG1 | V4L2_TUNER_CAP_LANG2;
0521     return 0;
0522 }
0523 
0524 static int msp_s_tuner(struct v4l2_subdev *sd, const struct v4l2_tuner *vt)
0525 {
0526     struct msp_state *state = to_state(sd);
0527     struct i2c_client *client = v4l2_get_subdevdata(sd);
0528 
0529     if (state->radio)  /* TODO: add mono/stereo support for radio */
0530         return 0;
0531     if (state->audmode == vt->audmode)
0532         return 0;
0533     state->audmode = vt->audmode;
0534     /* only set audmode */
0535     msp_set_audmode(client);
0536     return 0;
0537 }
0538 
0539 static int msp_s_i2s_clock_freq(struct v4l2_subdev *sd, u32 freq)
0540 {
0541     struct msp_state *state = to_state(sd);
0542     struct i2c_client *client = v4l2_get_subdevdata(sd);
0543 
0544     dev_dbg_lvl(&client->dev, 1, msp_debug, "Setting I2S speed to %d\n", freq);
0545 
0546     switch (freq) {
0547         case 1024000:
0548             state->i2s_mode = 0;
0549             break;
0550         case 2048000:
0551             state->i2s_mode = 1;
0552             break;
0553         default:
0554             return -EINVAL;
0555     }
0556     return 0;
0557 }
0558 
0559 static int msp_log_status(struct v4l2_subdev *sd)
0560 {
0561     struct msp_state *state = to_state(sd);
0562     struct i2c_client *client = v4l2_get_subdevdata(sd);
0563     const char *p;
0564     char prefix[V4L2_SUBDEV_NAME_SIZE + 20];
0565 
0566     if (state->opmode == OPMODE_AUTOSELECT)
0567         msp_detect_stereo(client);
0568     dev_info(&client->dev, "%s rev1 = 0x%04x rev2 = 0x%04x\n",
0569             client->name, state->rev1, state->rev2);
0570     snprintf(prefix, sizeof(prefix), "%s: Audio:    ", sd->name);
0571     v4l2_ctrl_handler_log_status(&state->hdl, prefix);
0572     switch (state->mode) {
0573         case MSP_MODE_AM_DETECT: p = "AM (for carrier detect)"; break;
0574         case MSP_MODE_FM_RADIO: p = "FM Radio"; break;
0575         case MSP_MODE_FM_TERRA: p = "Terrestrial FM-mono/stereo"; break;
0576         case MSP_MODE_FM_SAT: p = "Satellite FM-mono"; break;
0577         case MSP_MODE_FM_NICAM1: p = "NICAM/FM (B/G, D/K)"; break;
0578         case MSP_MODE_FM_NICAM2: p = "NICAM/FM (I)"; break;
0579         case MSP_MODE_AM_NICAM: p = "NICAM/AM (L)"; break;
0580         case MSP_MODE_BTSC: p = "BTSC"; break;
0581         case MSP_MODE_EXTERN: p = "External input"; break;
0582         default: p = "unknown"; break;
0583     }
0584     if (state->mode == MSP_MODE_EXTERN) {
0585         dev_info(&client->dev, "Mode:     %s\n", p);
0586     } else if (state->opmode == OPMODE_MANUAL) {
0587         dev_info(&client->dev, "Mode:     %s (%s%s)\n", p,
0588                 (state->rxsubchans & V4L2_TUNER_SUB_STEREO) ? "stereo" : "mono",
0589                 (state->rxsubchans & V4L2_TUNER_SUB_LANG2) ? ", dual" : "");
0590     } else {
0591         if (state->opmode == OPMODE_AUTODETECT)
0592             dev_info(&client->dev, "Mode:     %s\n", p);
0593         dev_info(&client->dev, "Standard: %s (%s%s)\n",
0594                 msp_standard_std_name(state->std),
0595                 (state->rxsubchans & V4L2_TUNER_SUB_STEREO) ? "stereo" : "mono",
0596                 (state->rxsubchans & V4L2_TUNER_SUB_LANG2) ? ", dual" : "");
0597     }
0598     dev_info(&client->dev, "Audmode:  0x%04x\n", state->audmode);
0599     dev_info(&client->dev, "Routing:  0x%08x (input) 0x%08x (output)\n",
0600             state->route_in, state->route_out);
0601     dev_info(&client->dev, "ACB:      0x%04x\n", state->acb);
0602     return 0;
0603 }
0604 
0605 #ifdef CONFIG_PM_SLEEP
0606 static int msp_suspend(struct device *dev)
0607 {
0608     struct i2c_client *client = to_i2c_client(dev);
0609     dev_dbg_lvl(&client->dev, 1, msp_debug, "suspend\n");
0610     msp_reset(client);
0611     return 0;
0612 }
0613 
0614 static int msp_resume(struct device *dev)
0615 {
0616     struct i2c_client *client = to_i2c_client(dev);
0617     dev_dbg_lvl(&client->dev, 1, msp_debug, "resume\n");
0618     msp_wake_thread(client);
0619     return 0;
0620 }
0621 #endif
0622 
0623 /* ----------------------------------------------------------------------- */
0624 
0625 static const struct v4l2_ctrl_ops msp_ctrl_ops = {
0626     .s_ctrl = msp_s_ctrl,
0627 };
0628 
0629 static const struct v4l2_subdev_core_ops msp_core_ops = {
0630     .log_status = msp_log_status,
0631 };
0632 
0633 static const struct v4l2_subdev_video_ops msp_video_ops = {
0634     .s_std = msp_s_std,
0635     .querystd = msp_querystd,
0636 };
0637 
0638 static const struct v4l2_subdev_tuner_ops msp_tuner_ops = {
0639     .s_frequency = msp_s_frequency,
0640     .g_tuner = msp_g_tuner,
0641     .s_tuner = msp_s_tuner,
0642     .s_radio = msp_s_radio,
0643 };
0644 
0645 static const struct v4l2_subdev_audio_ops msp_audio_ops = {
0646     .s_routing = msp_s_routing,
0647     .s_i2s_clock_freq = msp_s_i2s_clock_freq,
0648 };
0649 
0650 static const struct v4l2_subdev_ops msp_ops = {
0651     .core = &msp_core_ops,
0652     .video = &msp_video_ops,
0653     .tuner = &msp_tuner_ops,
0654     .audio = &msp_audio_ops,
0655 };
0656 
0657 /* ----------------------------------------------------------------------- */
0658 
0659 
0660 static const char * const opmode_str[] = {
0661     [OPMODE_MANUAL] = "manual",
0662     [OPMODE_AUTODETECT] = "autodetect",
0663     [OPMODE_AUTOSELECT] = "autodetect and autoselect",
0664 };
0665 
0666 static int msp_probe(struct i2c_client *client, const struct i2c_device_id *id)
0667 {
0668     struct msp_state *state;
0669     struct v4l2_subdev *sd;
0670     struct v4l2_ctrl_handler *hdl;
0671     int (*thread_func)(void *data) = NULL;
0672     int msp_hard;
0673     int msp_family;
0674     int msp_revision;
0675     int msp_product, msp_prod_hi, msp_prod_lo;
0676     int msp_rom;
0677 #if defined(CONFIG_MEDIA_CONTROLLER)
0678     int ret;
0679 #endif
0680 
0681     if (!id)
0682         strscpy(client->name, "msp3400", sizeof(client->name));
0683 
0684     if (msp_reset(client) == -1) {
0685         dev_dbg_lvl(&client->dev, 1, msp_debug, "msp3400 not found\n");
0686         return -ENODEV;
0687     }
0688 
0689     state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL);
0690     if (!state)
0691         return -ENOMEM;
0692 
0693     sd = &state->sd;
0694     v4l2_i2c_subdev_init(sd, client, &msp_ops);
0695 
0696 #if defined(CONFIG_MEDIA_CONTROLLER)
0697     state->pads[MSP3400_PAD_IF_INPUT].flags = MEDIA_PAD_FL_SINK;
0698     state->pads[MSP3400_PAD_IF_INPUT].sig_type = PAD_SIGNAL_AUDIO;
0699     state->pads[MSP3400_PAD_OUT].flags = MEDIA_PAD_FL_SOURCE;
0700     state->pads[MSP3400_PAD_OUT].sig_type = PAD_SIGNAL_AUDIO;
0701 
0702     sd->entity.function = MEDIA_ENT_F_IF_AUD_DECODER;
0703 
0704     ret = media_entity_pads_init(&sd->entity, 2, state->pads);
0705     if (ret < 0)
0706         return ret;
0707 #endif
0708 
0709     state->v4l2_std = V4L2_STD_NTSC;
0710     state->detected_std = V4L2_STD_ALL;
0711     state->audmode = V4L2_TUNER_MODE_STEREO;
0712     state->input = -1;
0713     state->i2s_mode = 0;
0714     init_waitqueue_head(&state->wq);
0715     /* These are the reset input/output positions */
0716     state->route_in = MSP_INPUT_DEFAULT;
0717     state->route_out = MSP_OUTPUT_DEFAULT;
0718 
0719     state->rev1 = msp_read_dsp(client, 0x1e);
0720     if (state->rev1 != -1)
0721         state->rev2 = msp_read_dsp(client, 0x1f);
0722     dev_dbg_lvl(&client->dev, 1, msp_debug, "rev1=0x%04x, rev2=0x%04x\n",
0723             state->rev1, state->rev2);
0724     if (state->rev1 == -1 || (state->rev1 == 0 && state->rev2 == 0)) {
0725         dev_dbg_lvl(&client->dev, 1, msp_debug,
0726                 "not an msp3400 (cannot read chip version)\n");
0727         return -ENODEV;
0728     }
0729 
0730     msp_family = ((state->rev1 >> 4) & 0x0f) + 3;
0731     msp_product = (state->rev2 >> 8) & 0xff;
0732     msp_prod_hi = msp_product / 10;
0733     msp_prod_lo = msp_product % 10;
0734     msp_revision = (state->rev1 & 0x0f) + '@';
0735     msp_hard = ((state->rev1 >> 8) & 0xff) + '@';
0736     msp_rom = state->rev2 & 0x1f;
0737     /* Rev B=2, C=3, D=4, G=7 */
0738     state->ident = msp_family * 10000 + 4000 + msp_product * 10 +
0739             msp_revision - '@';
0740 
0741     /* Has NICAM support: all mspx41x and mspx45x products have NICAM */
0742     state->has_nicam =
0743         msp_prod_hi == 1 || msp_prod_hi == 5;
0744     /* Has radio support: was added with revision G */
0745     state->has_radio =
0746         msp_revision >= 'G';
0747     /* Has headphones output: not for stripped down products */
0748     state->has_headphones =
0749         msp_prod_lo < 5;
0750     /* Has scart2 input: not in stripped down products of the '3' family */
0751     state->has_scart2 =
0752         msp_family >= 4 || msp_prod_lo < 7;
0753     /* Has scart3 input: not in stripped down products of the '3' family */
0754     state->has_scart3 =
0755         msp_family >= 4 || msp_prod_lo < 5;
0756     /* Has scart4 input: not in pre D revisions, not in stripped D revs */
0757     state->has_scart4 =
0758         msp_family >= 4 || (msp_revision >= 'D' && msp_prod_lo < 5);
0759     /* Has scart2 output: not in stripped down products of
0760      * the '3' family */
0761     state->has_scart2_out =
0762         msp_family >= 4 || msp_prod_lo < 5;
0763     /* Has scart2 a volume control? Not in pre-D revisions. */
0764     state->has_scart2_out_volume =
0765         msp_revision > 'C' && state->has_scart2_out;
0766     /* Has a configurable i2s out? */
0767     state->has_i2s_conf =
0768         msp_revision >= 'G' && msp_prod_lo < 7;
0769     /* Has subwoofer output: not in pre-D revs and not in stripped down
0770      * products */
0771     state->has_subwoofer =
0772         msp_revision >= 'D' && msp_prod_lo < 5;
0773     /* Has soundprocessing (bass/treble/balance/loudness/equalizer):
0774      *  not in stripped down products */
0775     state->has_sound_processing =
0776         msp_prod_lo < 7;
0777     /* Has Virtual Dolby Surround: only in msp34x1 */
0778     state->has_virtual_dolby_surround =
0779         msp_revision == 'G' && msp_prod_lo == 1;
0780     /* Has Virtual Dolby Surround & Dolby Pro Logic: only in msp34x2 */
0781     state->has_dolby_pro_logic =
0782         msp_revision == 'G' && msp_prod_lo == 2;
0783     /* The msp343xG supports BTSC only and cannot do Automatic Standard
0784      * Detection. */
0785     state->force_btsc =
0786         msp_family == 3 && msp_revision == 'G' && msp_prod_hi == 3;
0787 
0788     state->opmode = opmode;
0789     if (state->opmode < OPMODE_MANUAL
0790         || state->opmode > OPMODE_AUTOSELECT) {
0791         /* MSP revision G and up have both autodetect and autoselect */
0792         if (msp_revision >= 'G')
0793             state->opmode = OPMODE_AUTOSELECT;
0794         /* MSP revision D and up have autodetect */
0795         else if (msp_revision >= 'D')
0796             state->opmode = OPMODE_AUTODETECT;
0797         else
0798             state->opmode = OPMODE_MANUAL;
0799     }
0800 
0801     hdl = &state->hdl;
0802     v4l2_ctrl_handler_init(hdl, 6);
0803     if (state->has_sound_processing) {
0804         v4l2_ctrl_new_std(hdl, &msp_ctrl_ops,
0805             V4L2_CID_AUDIO_BASS, 0, 65535, 65535 / 100, 32768);
0806         v4l2_ctrl_new_std(hdl, &msp_ctrl_ops,
0807             V4L2_CID_AUDIO_TREBLE, 0, 65535, 65535 / 100, 32768);
0808         v4l2_ctrl_new_std(hdl, &msp_ctrl_ops,
0809             V4L2_CID_AUDIO_LOUDNESS, 0, 1, 1, 0);
0810     }
0811     state->volume = v4l2_ctrl_new_std(hdl, &msp_ctrl_ops,
0812             V4L2_CID_AUDIO_VOLUME, 0, 65535, 65535 / 100, 58880);
0813     v4l2_ctrl_new_std(hdl, &msp_ctrl_ops,
0814             V4L2_CID_AUDIO_BALANCE, 0, 65535, 65535 / 100, 32768);
0815     state->muted = v4l2_ctrl_new_std(hdl, &msp_ctrl_ops,
0816             V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
0817     sd->ctrl_handler = hdl;
0818     if (hdl->error) {
0819         int err = hdl->error;
0820 
0821         v4l2_ctrl_handler_free(hdl);
0822         return err;
0823     }
0824 
0825     v4l2_ctrl_cluster(2, &state->volume);
0826     v4l2_ctrl_handler_setup(hdl);
0827 
0828     dev_info(&client->dev,
0829          "MSP%d4%02d%c-%c%d found on %s: supports %s%s%s, mode is %s\n",
0830          msp_family, msp_product,
0831          msp_revision, msp_hard, msp_rom,
0832          client->adapter->name,
0833          (state->has_nicam) ? "nicam" : "",
0834          (state->has_nicam && state->has_radio) ? " and " : "",
0835          (state->has_radio) ? "radio" : "",
0836          opmode_str[state->opmode]);
0837 
0838     /* version-specific initialization */
0839     switch (state->opmode) {
0840     case OPMODE_MANUAL:
0841         thread_func = msp3400c_thread;
0842         break;
0843     case OPMODE_AUTODETECT:
0844         thread_func = msp3410d_thread;
0845         break;
0846     case OPMODE_AUTOSELECT:
0847         thread_func = msp34xxg_thread;
0848         break;
0849     }
0850 
0851     /* startup control thread if needed */
0852     if (thread_func) {
0853         state->kthread = kthread_run(thread_func, client, "msp34xx");
0854 
0855         if (IS_ERR(state->kthread))
0856             dev_warn(&client->dev, "kernel_thread() failed\n");
0857         msp_wake_thread(client);
0858     }
0859     return 0;
0860 }
0861 
0862 static int msp_remove(struct i2c_client *client)
0863 {
0864     struct msp_state *state = to_state(i2c_get_clientdata(client));
0865 
0866     v4l2_device_unregister_subdev(&state->sd);
0867     /* shutdown control thread */
0868     if (state->kthread) {
0869         state->restart = 1;
0870         kthread_stop(state->kthread);
0871     }
0872     msp_reset(client);
0873 
0874     v4l2_ctrl_handler_free(&state->hdl);
0875     return 0;
0876 }
0877 
0878 /* ----------------------------------------------------------------------- */
0879 
0880 static const struct dev_pm_ops msp3400_pm_ops = {
0881     SET_SYSTEM_SLEEP_PM_OPS(msp_suspend, msp_resume)
0882 };
0883 
0884 static const struct i2c_device_id msp_id[] = {
0885     { "msp3400", 0 },
0886     { }
0887 };
0888 MODULE_DEVICE_TABLE(i2c, msp_id);
0889 
0890 static struct i2c_driver msp_driver = {
0891     .driver = {
0892         .name   = "msp3400",
0893         .pm = &msp3400_pm_ops,
0894     },
0895     .probe      = msp_probe,
0896     .remove     = msp_remove,
0897     .id_table   = msp_id,
0898 };
0899 
0900 module_i2c_driver(msp_driver);