0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/kernel.h>
0010 #include <linux/module.h>
0011 #include <linux/i2c.h>
0012 #include <linux/freezer.h>
0013 #include <linux/videodev2.h>
0014 #include <media/v4l2-common.h>
0015 #include <media/drv-intf/msp3400.h>
0016 #include <linux/kthread.h>
0017 #include <linux/suspend.h>
0018 #include "msp3400-driver.h"
0019
0020
0021
0022 static struct {
0023 int retval;
0024 int main, second;
0025 char *name;
0026 v4l2_std_id std;
0027 } msp_stdlist[] = {
0028 { 0x0000, 0, 0, "could not detect sound standard", V4L2_STD_ALL },
0029 { 0x0001, 0, 0, "autodetect start", V4L2_STD_ALL },
0030 { 0x0002, MSP_CARRIER(4.5), MSP_CARRIER(4.72),
0031 "4.5/4.72 M Dual FM-Stereo", V4L2_STD_MN },
0032 { 0x0003, MSP_CARRIER(5.5), MSP_CARRIER(5.7421875),
0033 "5.5/5.74 B/G Dual FM-Stereo", V4L2_STD_BG },
0034 { 0x0004, MSP_CARRIER(6.5), MSP_CARRIER(6.2578125),
0035 "6.5/6.25 D/K1 Dual FM-Stereo", V4L2_STD_DK },
0036 { 0x0005, MSP_CARRIER(6.5), MSP_CARRIER(6.7421875),
0037 "6.5/6.74 D/K2 Dual FM-Stereo", V4L2_STD_DK },
0038 { 0x0006, MSP_CARRIER(6.5), MSP_CARRIER(6.5),
0039 "6.5 D/K FM-Mono (HDEV3)", V4L2_STD_DK },
0040 { 0x0007, MSP_CARRIER(6.5), MSP_CARRIER(5.7421875),
0041 "6.5/5.74 D/K3 Dual FM-Stereo", V4L2_STD_DK },
0042 { 0x0008, MSP_CARRIER(5.5), MSP_CARRIER(5.85),
0043 "5.5/5.85 B/G NICAM FM", V4L2_STD_BG },
0044 { 0x0009, MSP_CARRIER(6.5), MSP_CARRIER(5.85),
0045 "6.5/5.85 L NICAM AM", V4L2_STD_L },
0046 { 0x000a, MSP_CARRIER(6.0), MSP_CARRIER(6.55),
0047 "6.0/6.55 I NICAM FM", V4L2_STD_PAL_I },
0048 { 0x000b, MSP_CARRIER(6.5), MSP_CARRIER(5.85),
0049 "6.5/5.85 D/K NICAM FM", V4L2_STD_DK },
0050 { 0x000c, MSP_CARRIER(6.5), MSP_CARRIER(5.85),
0051 "6.5/5.85 D/K NICAM FM (HDEV2)", V4L2_STD_DK },
0052 { 0x000d, MSP_CARRIER(6.5), MSP_CARRIER(5.85),
0053 "6.5/5.85 D/K NICAM FM (HDEV3)", V4L2_STD_DK },
0054 { 0x0020, MSP_CARRIER(4.5), MSP_CARRIER(4.5),
0055 "4.5 M BTSC-Stereo", V4L2_STD_MTS },
0056 { 0x0021, MSP_CARRIER(4.5), MSP_CARRIER(4.5),
0057 "4.5 M BTSC-Mono + SAP", V4L2_STD_MTS },
0058 { 0x0030, MSP_CARRIER(4.5), MSP_CARRIER(4.5),
0059 "4.5 M EIA-J Japan Stereo", V4L2_STD_NTSC_M_JP },
0060 { 0x0040, MSP_CARRIER(10.7), MSP_CARRIER(10.7),
0061 "10.7 FM-Stereo Radio", V4L2_STD_ALL },
0062 { 0x0050, MSP_CARRIER(6.5), MSP_CARRIER(6.5),
0063 "6.5 SAT-Mono", V4L2_STD_ALL },
0064 { 0x0051, MSP_CARRIER(7.02), MSP_CARRIER(7.20),
0065 "7.02/7.20 SAT-Stereo", V4L2_STD_ALL },
0066 { 0x0060, MSP_CARRIER(7.2), MSP_CARRIER(7.2),
0067 "7.2 SAT ADR", V4L2_STD_ALL },
0068 { -1, 0, 0, NULL, 0 },
0069 };
0070
0071 static struct msp3400c_init_data_dem {
0072 int fir1[6];
0073 int fir2[6];
0074 int cdo1;
0075 int cdo2;
0076 int ad_cv;
0077 int mode_reg;
0078 int dsp_src;
0079 int dsp_matrix;
0080 } msp3400c_init_data[] = {
0081 {
0082 {75, 19, 36, 35, 39, 40},
0083 {75, 19, 36, 35, 39, 40},
0084 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
0085 0x00d0, 0x0500, 0x0020, 0x3000
0086 }, {
0087 {-1, -1, -8, 2, 59, 126},
0088 {-1, -1, -8, 2, 59, 126},
0089 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
0090 0x00d0, 0x0100, 0x0020, 0x3000
0091 }, {
0092 {-8, -8, 4, 6, 78, 107},
0093 {-8, -8, 4, 6, 78, 107},
0094 MSP_CARRIER(10.7), MSP_CARRIER(10.7),
0095 0x00d0, 0x0480, 0x0020, 0x3000
0096 }, {
0097 {3, 18, 27, 48, 66, 72},
0098 {3, 18, 27, 48, 66, 72},
0099 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
0100 0x00d0, 0x0480, 0x0030, 0x3000
0101 }, {
0102 { 1, 9, 14, 24, 33, 37},
0103 { 3, 18, 27, 48, 66, 72},
0104 MSP_CARRIER(6.5), MSP_CARRIER(6.5),
0105 0x00c6, 0x0480, 0x0000, 0x3000
0106 }, {
0107 {-2, -8, -10, 10, 50, 86},
0108 {3, 18, 27, 48, 66, 72},
0109 MSP_CARRIER(5.5), MSP_CARRIER(5.5),
0110 0x00d0, 0x0040, 0x0120, 0x3000
0111 }, {
0112 {2, 4, -6, -4, 40, 94},
0113 {3, 18, 27, 48, 66, 72},
0114 MSP_CARRIER(6.0), MSP_CARRIER(6.0),
0115 0x00d0, 0x0040, 0x0120, 0x3000
0116 }, {
0117 {-2, -8, -10, 10, 50, 86},
0118 {-4, -12, -9, 23, 79, 126},
0119 MSP_CARRIER(6.5), MSP_CARRIER(6.5),
0120 0x00c6, 0x0140, 0x0120, 0x7c00
0121 },
0122 };
0123
0124 struct msp3400c_carrier_detect {
0125 int cdo;
0126 char *name;
0127 };
0128
0129 static struct msp3400c_carrier_detect msp3400c_carrier_detect_main[] = {
0130
0131 { MSP_CARRIER(4.5), "4.5 NTSC" },
0132 { MSP_CARRIER(5.5), "5.5 PAL B/G" },
0133 { MSP_CARRIER(6.0), "6.0 PAL I" },
0134 { MSP_CARRIER(6.5), "6.5 PAL D/K + SAT + SECAM" }
0135 };
0136
0137 static struct msp3400c_carrier_detect msp3400c_carrier_detect_55[] = {
0138
0139 { MSP_CARRIER(5.7421875), "5.742 PAL B/G FM-stereo" },
0140 { MSP_CARRIER(5.85), "5.85 PAL B/G NICAM" }
0141 };
0142
0143 static struct msp3400c_carrier_detect msp3400c_carrier_detect_65[] = {
0144
0145 { MSP_CARRIER(5.85), "5.85 PAL D/K + SECAM NICAM" },
0146 { MSP_CARRIER(6.2578125), "6.25 PAL D/K1 FM-stereo" },
0147 { MSP_CARRIER(6.7421875), "6.74 PAL D/K2 FM-stereo" },
0148 { MSP_CARRIER(7.02), "7.02 PAL SAT FM-stereo s/b" },
0149 { MSP_CARRIER(7.20), "7.20 PAL SAT FM-stereo s" },
0150 { MSP_CARRIER(7.38), "7.38 PAL SAT FM-stereo b" },
0151 };
0152
0153
0154
0155 const char *msp_standard_std_name(int std)
0156 {
0157 int i;
0158
0159 for (i = 0; msp_stdlist[i].name != NULL; i++)
0160 if (msp_stdlist[i].retval == std)
0161 return msp_stdlist[i].name;
0162 return "unknown";
0163 }
0164
0165 static v4l2_std_id msp_standard_std(int std)
0166 {
0167 int i;
0168
0169 for (i = 0; msp_stdlist[i].name != NULL; i++)
0170 if (msp_stdlist[i].retval == std)
0171 return msp_stdlist[i].std;
0172 return V4L2_STD_ALL;
0173 }
0174
0175 static void msp_set_source(struct i2c_client *client, u16 src)
0176 {
0177 struct msp_state *state = to_state(i2c_get_clientdata(client));
0178
0179 if (msp_dolby) {
0180 msp_write_dsp(client, 0x0008, 0x0520);
0181 msp_write_dsp(client, 0x0009, 0x0620);
0182 } else {
0183 msp_write_dsp(client, 0x0008, src);
0184 msp_write_dsp(client, 0x0009, src);
0185 }
0186 msp_write_dsp(client, 0x000a, src);
0187 msp_write_dsp(client, 0x000b, src);
0188 msp_write_dsp(client, 0x000c, src);
0189 if (state->has_scart2_out)
0190 msp_write_dsp(client, 0x0041, src);
0191 }
0192
0193 void msp3400c_set_carrier(struct i2c_client *client, int cdo1, int cdo2)
0194 {
0195 msp_write_dem(client, 0x0093, cdo1 & 0xfff);
0196 msp_write_dem(client, 0x009b, cdo1 >> 12);
0197 msp_write_dem(client, 0x00a3, cdo2 & 0xfff);
0198 msp_write_dem(client, 0x00ab, cdo2 >> 12);
0199 msp_write_dem(client, 0x0056, 0);
0200 }
0201
0202 void msp3400c_set_mode(struct i2c_client *client, int mode)
0203 {
0204 struct msp_state *state = to_state(i2c_get_clientdata(client));
0205 struct msp3400c_init_data_dem *data = &msp3400c_init_data[mode];
0206 int tuner = (state->route_in >> 3) & 1;
0207 int i;
0208
0209 dev_dbg_lvl(&client->dev, 1, msp_debug, "set_mode: %d\n", mode);
0210 state->mode = mode;
0211 state->rxsubchans = V4L2_TUNER_SUB_MONO;
0212
0213 msp_write_dem(client, 0x00bb, data->ad_cv | (tuner ? 0x100 : 0));
0214
0215 for (i = 5; i >= 0; i--)
0216 msp_write_dem(client, 0x0001, data->fir1[i]);
0217
0218 msp_write_dem(client, 0x0005, 0x0004);
0219 msp_write_dem(client, 0x0005, 0x0040);
0220 msp_write_dem(client, 0x0005, 0x0000);
0221 for (i = 5; i >= 0; i--)
0222 msp_write_dem(client, 0x0005, data->fir2[i]);
0223
0224 msp_write_dem(client, 0x0083, data->mode_reg);
0225
0226 msp3400c_set_carrier(client, data->cdo1, data->cdo2);
0227
0228 msp_set_source(client, data->dsp_src);
0229
0230
0231
0232 msp_write_dsp(client, 0x000d, 0x1900);
0233 msp_write_dsp(client, 0x000e, data->dsp_matrix);
0234 if (state->has_nicam)
0235 msp_write_dsp(client, 0x0010, 0x5a00);
0236 }
0237
0238
0239
0240 static void msp3400c_set_audmode(struct i2c_client *client)
0241 {
0242 static char *strmode[] = {
0243 "mono", "stereo", "lang2", "lang1", "lang1+lang2"
0244 };
0245 struct msp_state *state = to_state(i2c_get_clientdata(client));
0246 char *modestr = (state->audmode >= 0 && state->audmode < 5) ?
0247 strmode[state->audmode] : "unknown";
0248 int src = 0;
0249 int audmode = state->audmode;
0250
0251 if (state->opmode == OPMODE_AUTOSELECT) {
0252
0253
0254
0255 dev_dbg_lvl(&client->dev, 1, msp_debug,
0256 "set_audmode called with mode=%d instead of set_source (ignored)\n",
0257 state->audmode);
0258 return;
0259 }
0260
0261
0262
0263
0264
0265 if (state->mode != MSP_MODE_EXTERN) {
0266
0267 if (state->rxsubchans == V4L2_TUNER_SUB_MONO)
0268 audmode = V4L2_TUNER_MODE_MONO;
0269
0270 else if (state->rxsubchans & V4L2_TUNER_SUB_LANG2) {
0271
0272 if (audmode == V4L2_TUNER_MODE_MONO ||
0273 audmode == V4L2_TUNER_MODE_STEREO)
0274 audmode = V4L2_TUNER_MODE_LANG1;
0275 }
0276
0277 else if (audmode != V4L2_TUNER_MODE_MONO)
0278 audmode = V4L2_TUNER_MODE_STEREO;
0279 }
0280
0281
0282 switch (state->mode) {
0283 case MSP_MODE_FM_TERRA:
0284 dev_dbg_lvl(&client->dev, 1, msp_debug, "FM set_audmode: %s\n", modestr);
0285 switch (audmode) {
0286 case V4L2_TUNER_MODE_STEREO:
0287 msp_write_dsp(client, 0x000e, 0x3001);
0288 break;
0289 case V4L2_TUNER_MODE_MONO:
0290 case V4L2_TUNER_MODE_LANG1:
0291 case V4L2_TUNER_MODE_LANG2:
0292 case V4L2_TUNER_MODE_LANG1_LANG2:
0293 msp_write_dsp(client, 0x000e, 0x3000);
0294 break;
0295 }
0296 break;
0297 case MSP_MODE_FM_SAT:
0298 dev_dbg_lvl(&client->dev, 1, msp_debug, "SAT set_audmode: %s\n", modestr);
0299 switch (audmode) {
0300 case V4L2_TUNER_MODE_MONO:
0301 msp3400c_set_carrier(client, MSP_CARRIER(6.5), MSP_CARRIER(6.5));
0302 break;
0303 case V4L2_TUNER_MODE_STEREO:
0304 case V4L2_TUNER_MODE_LANG1_LANG2:
0305 msp3400c_set_carrier(client, MSP_CARRIER(7.2), MSP_CARRIER(7.02));
0306 break;
0307 case V4L2_TUNER_MODE_LANG1:
0308 msp3400c_set_carrier(client, MSP_CARRIER(7.38), MSP_CARRIER(7.02));
0309 break;
0310 case V4L2_TUNER_MODE_LANG2:
0311 msp3400c_set_carrier(client, MSP_CARRIER(7.38), MSP_CARRIER(7.02));
0312 break;
0313 }
0314 break;
0315 case MSP_MODE_FM_NICAM1:
0316 case MSP_MODE_FM_NICAM2:
0317 case MSP_MODE_AM_NICAM:
0318 dev_dbg_lvl(&client->dev, 1, msp_debug,
0319 "NICAM set_audmode: %s\n", modestr);
0320 if (state->nicam_on)
0321 src = 0x0100;
0322 break;
0323 case MSP_MODE_BTSC:
0324 dev_dbg_lvl(&client->dev, 1, msp_debug,
0325 "BTSC set_audmode: %s\n", modestr);
0326 break;
0327 case MSP_MODE_EXTERN:
0328 dev_dbg_lvl(&client->dev, 1, msp_debug,
0329 "extern set_audmode: %s\n", modestr);
0330 src = 0x0200;
0331 break;
0332 case MSP_MODE_FM_RADIO:
0333 dev_dbg_lvl(&client->dev, 1, msp_debug,
0334 "FM-Radio set_audmode: %s\n", modestr);
0335 break;
0336 default:
0337 dev_dbg_lvl(&client->dev, 1, msp_debug, "mono set_audmode\n");
0338 return;
0339 }
0340
0341
0342 dev_dbg_lvl(&client->dev, 1, msp_debug, "set audmode %d\n", audmode);
0343 switch (audmode) {
0344 case V4L2_TUNER_MODE_STEREO:
0345 case V4L2_TUNER_MODE_LANG1_LANG2:
0346 src |= 0x0020;
0347 break;
0348 case V4L2_TUNER_MODE_MONO:
0349 if (state->mode == MSP_MODE_AM_NICAM) {
0350 dev_dbg_lvl(&client->dev, 1, msp_debug, "switching to AM mono\n");
0351
0352
0353 msp_set_scart(client, SCART_MONO, 0);
0354 src = 0x0200;
0355 break;
0356 }
0357 if (state->rxsubchans & V4L2_TUNER_SUB_STEREO)
0358 src = 0x0030;
0359 break;
0360 case V4L2_TUNER_MODE_LANG1:
0361 break;
0362 case V4L2_TUNER_MODE_LANG2:
0363 src |= 0x0010;
0364 break;
0365 }
0366 dev_dbg_lvl(&client->dev, 1, msp_debug,
0367 "set_audmode final source/matrix = 0x%x\n", src);
0368
0369 msp_set_source(client, src);
0370 }
0371
0372 static void msp3400c_print_mode(struct i2c_client *client)
0373 {
0374 struct msp_state *state = to_state(i2c_get_clientdata(client));
0375
0376 if (state->main == state->second)
0377 dev_dbg_lvl(&client->dev, 1, msp_debug,
0378 "mono sound carrier: %d.%03d MHz\n",
0379 state->main / 910000, (state->main / 910) % 1000);
0380 else
0381 dev_dbg_lvl(&client->dev, 1, msp_debug,
0382 "main sound carrier: %d.%03d MHz\n",
0383 state->main / 910000, (state->main / 910) % 1000);
0384 if (state->mode == MSP_MODE_FM_NICAM1 || state->mode == MSP_MODE_FM_NICAM2)
0385 dev_dbg_lvl(&client->dev, 1, msp_debug,
0386 "NICAM/FM carrier : %d.%03d MHz\n",
0387 state->second / 910000, (state->second/910) % 1000);
0388 if (state->mode == MSP_MODE_AM_NICAM)
0389 dev_dbg_lvl(&client->dev, 1, msp_debug,
0390 "NICAM/AM carrier : %d.%03d MHz\n",
0391 state->second / 910000, (state->second / 910) % 1000);
0392 if (state->mode == MSP_MODE_FM_TERRA && state->main != state->second) {
0393 dev_dbg_lvl(&client->dev, 1, msp_debug,
0394 "FM-stereo carrier : %d.%03d MHz\n",
0395 state->second / 910000, (state->second / 910) % 1000);
0396 }
0397 }
0398
0399
0400
0401 static int msp3400c_detect_stereo(struct i2c_client *client)
0402 {
0403 struct msp_state *state = to_state(i2c_get_clientdata(client));
0404 int val;
0405 int rxsubchans = state->rxsubchans;
0406 int newnicam = state->nicam_on;
0407 int update = 0;
0408
0409 switch (state->mode) {
0410 case MSP_MODE_FM_TERRA:
0411 val = msp_read_dsp(client, 0x18);
0412 if (val > 32767)
0413 val -= 65536;
0414 dev_dbg_lvl(&client->dev, 2, msp_debug,
0415 "stereo detect register: %d\n", val);
0416 if (val > 8192) {
0417 rxsubchans = V4L2_TUNER_SUB_STEREO;
0418 } else if (val < -4096) {
0419 rxsubchans = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
0420 } else {
0421 rxsubchans = V4L2_TUNER_SUB_MONO;
0422 }
0423 newnicam = 0;
0424 break;
0425 case MSP_MODE_FM_NICAM1:
0426 case MSP_MODE_FM_NICAM2:
0427 case MSP_MODE_AM_NICAM:
0428 val = msp_read_dem(client, 0x23);
0429 dev_dbg_lvl(&client->dev, 2, msp_debug, "nicam sync=%d, mode=%d\n",
0430 val & 1, (val & 0x1e) >> 1);
0431
0432 if (val & 1) {
0433
0434 switch ((val & 0x1e) >> 1) {
0435 case 0:
0436 case 8:
0437 rxsubchans = V4L2_TUNER_SUB_STEREO;
0438 break;
0439 case 1:
0440 case 9:
0441 rxsubchans = V4L2_TUNER_SUB_MONO;
0442 break;
0443 case 2:
0444 case 10:
0445 rxsubchans = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
0446 break;
0447 default:
0448 rxsubchans = V4L2_TUNER_SUB_MONO;
0449 break;
0450 }
0451 newnicam = 1;
0452 } else {
0453 newnicam = 0;
0454 rxsubchans = V4L2_TUNER_SUB_MONO;
0455 }
0456 break;
0457 }
0458 if (rxsubchans != state->rxsubchans) {
0459 update = 1;
0460 dev_dbg_lvl(&client->dev, 1, msp_debug,
0461 "watch: rxsubchans %02x => %02x\n",
0462 state->rxsubchans, rxsubchans);
0463 state->rxsubchans = rxsubchans;
0464 }
0465 if (newnicam != state->nicam_on) {
0466 update = 1;
0467 dev_dbg_lvl(&client->dev, 1, msp_debug, "watch: nicam %d => %d\n",
0468 state->nicam_on, newnicam);
0469 state->nicam_on = newnicam;
0470 }
0471 return update;
0472 }
0473
0474
0475
0476
0477
0478
0479 static void watch_stereo(struct i2c_client *client)
0480 {
0481 struct msp_state *state = to_state(i2c_get_clientdata(client));
0482
0483 if (msp_detect_stereo(client))
0484 msp_set_audmode(client);
0485
0486 if (msp_once)
0487 state->watch_stereo = 0;
0488 }
0489
0490 int msp3400c_thread(void *data)
0491 {
0492 struct i2c_client *client = data;
0493 struct msp_state *state = to_state(i2c_get_clientdata(client));
0494 struct msp3400c_carrier_detect *cd;
0495 int count, max1, max2, val1, val2, val, i;
0496
0497 dev_dbg_lvl(&client->dev, 1, msp_debug, "msp3400 daemon started\n");
0498 state->detected_std = V4L2_STD_ALL;
0499 set_freezable();
0500 for (;;) {
0501 dev_dbg_lvl(&client->dev, 2, msp_debug, "msp3400 thread: sleep\n");
0502 msp_sleep(state, -1);
0503 dev_dbg_lvl(&client->dev, 2, msp_debug, "msp3400 thread: wakeup\n");
0504
0505 restart:
0506 dev_dbg_lvl(&client->dev, 2, msp_debug, "thread: restart scan\n");
0507 state->restart = 0;
0508 if (kthread_should_stop())
0509 break;
0510
0511 if (state->radio || MSP_MODE_EXTERN == state->mode) {
0512
0513 dev_dbg_lvl(&client->dev, 1, msp_debug,
0514 "thread: no carrier scan\n");
0515 state->scan_in_progress = 0;
0516 msp_update_volume(state);
0517 continue;
0518 }
0519
0520
0521 state->scan_in_progress = 1;
0522 msp_update_volume(state);
0523
0524 msp3400c_set_mode(client, MSP_MODE_AM_DETECT);
0525 val1 = val2 = 0;
0526 max1 = max2 = -1;
0527 state->watch_stereo = 0;
0528 state->nicam_on = 0;
0529
0530
0531 if (msp_sleep(state, 200))
0532 goto restart;
0533
0534
0535 cd = msp3400c_carrier_detect_main;
0536 count = ARRAY_SIZE(msp3400c_carrier_detect_main);
0537
0538 if (msp_amsound && (state->v4l2_std & V4L2_STD_SECAM)) {
0539
0540 max1 = 3;
0541 count = 0;
0542 dev_dbg_lvl(&client->dev, 1, msp_debug, "AM sound override\n");
0543 }
0544
0545 for (i = 0; i < count; i++) {
0546 msp3400c_set_carrier(client, cd[i].cdo, cd[i].cdo);
0547 if (msp_sleep(state, 100))
0548 goto restart;
0549 val = msp_read_dsp(client, 0x1b);
0550 if (val > 32767)
0551 val -= 65536;
0552 if (val1 < val) {
0553 val1 = val;
0554 max1 = i;
0555 }
0556 dev_dbg_lvl(&client->dev, 1, msp_debug,
0557 "carrier1 val: %5d / %s\n", val, cd[i].name);
0558 }
0559
0560
0561 switch (max1) {
0562 case 1:
0563 cd = msp3400c_carrier_detect_55;
0564 count = ARRAY_SIZE(msp3400c_carrier_detect_55);
0565 break;
0566 case 3:
0567 cd = msp3400c_carrier_detect_65;
0568 count = ARRAY_SIZE(msp3400c_carrier_detect_65);
0569 break;
0570 case 0:
0571 case 2:
0572 default:
0573 cd = NULL;
0574 count = 0;
0575 break;
0576 }
0577
0578 if (msp_amsound && (state->v4l2_std & V4L2_STD_SECAM)) {
0579
0580 cd = NULL;
0581 count = 0;
0582 max2 = 0;
0583 }
0584 for (i = 0; i < count; i++) {
0585 msp3400c_set_carrier(client, cd[i].cdo, cd[i].cdo);
0586 if (msp_sleep(state, 100))
0587 goto restart;
0588 val = msp_read_dsp(client, 0x1b);
0589 if (val > 32767)
0590 val -= 65536;
0591 if (val2 < val) {
0592 val2 = val;
0593 max2 = i;
0594 }
0595 dev_dbg_lvl(&client->dev, 1, msp_debug,
0596 "carrier2 val: %5d / %s\n", val, cd[i].name);
0597 }
0598
0599
0600 state->main = msp3400c_carrier_detect_main[max1].cdo;
0601 switch (max1) {
0602 case 1:
0603 state->detected_std = V4L2_STD_BG | V4L2_STD_PAL_H;
0604 if (max2 == 0) {
0605
0606 state->second = msp3400c_carrier_detect_55[max2].cdo;
0607 msp3400c_set_mode(client, MSP_MODE_FM_TERRA);
0608 state->watch_stereo = 1;
0609 } else if (max2 == 1 && state->has_nicam) {
0610
0611 state->second = msp3400c_carrier_detect_55[max2].cdo;
0612 msp3400c_set_mode(client, MSP_MODE_FM_NICAM1);
0613 state->nicam_on = 1;
0614 state->watch_stereo = 1;
0615 } else {
0616 goto no_second;
0617 }
0618 break;
0619 case 2:
0620
0621 state->detected_std = V4L2_STD_PAL_I;
0622 state->second = MSP_CARRIER(6.552);
0623 msp3400c_set_mode(client, MSP_MODE_FM_NICAM2);
0624 state->nicam_on = 1;
0625 state->watch_stereo = 1;
0626 break;
0627 case 3:
0628 if (max2 == 1 || max2 == 2) {
0629
0630 state->second = msp3400c_carrier_detect_65[max2].cdo;
0631 msp3400c_set_mode(client, MSP_MODE_FM_TERRA);
0632 state->watch_stereo = 1;
0633 state->detected_std = V4L2_STD_DK;
0634 } else if (max2 == 0 && (state->v4l2_std & V4L2_STD_SECAM)) {
0635
0636 state->second = msp3400c_carrier_detect_65[max2].cdo;
0637 msp3400c_set_mode(client, MSP_MODE_AM_NICAM);
0638 state->watch_stereo = 1;
0639 state->detected_std = V4L2_STD_L;
0640 } else if (max2 == 0 && state->has_nicam) {
0641
0642 state->second = msp3400c_carrier_detect_65[max2].cdo;
0643 msp3400c_set_mode(client, MSP_MODE_FM_NICAM1);
0644 state->nicam_on = 1;
0645 state->watch_stereo = 1;
0646 state->detected_std = V4L2_STD_DK;
0647 } else {
0648 goto no_second;
0649 }
0650 break;
0651 case 0:
0652 state->detected_std = V4L2_STD_MN;
0653 fallthrough;
0654 default:
0655 no_second:
0656 state->second = msp3400c_carrier_detect_main[max1].cdo;
0657 msp3400c_set_mode(client, MSP_MODE_FM_TERRA);
0658 break;
0659 }
0660 msp3400c_set_carrier(client, state->second, state->main);
0661
0662
0663 state->scan_in_progress = 0;
0664 msp3400c_set_audmode(client);
0665 msp_update_volume(state);
0666
0667 if (msp_debug)
0668 msp3400c_print_mode(client);
0669
0670
0671
0672 count = 3;
0673 while (state->watch_stereo) {
0674 if (msp_sleep(state, count ? 1000 : 5000))
0675 goto restart;
0676 if (count)
0677 count--;
0678 watch_stereo(client);
0679 }
0680 }
0681 dev_dbg_lvl(&client->dev, 1, msp_debug, "thread: exit\n");
0682 return 0;
0683 }
0684
0685
0686 int msp3410d_thread(void *data)
0687 {
0688 struct i2c_client *client = data;
0689 struct msp_state *state = to_state(i2c_get_clientdata(client));
0690 int val, i, std, count;
0691
0692 dev_dbg_lvl(&client->dev, 1, msp_debug, "msp3410 daemon started\n");
0693 state->detected_std = V4L2_STD_ALL;
0694 set_freezable();
0695 for (;;) {
0696 dev_dbg_lvl(&client->dev, 2, msp_debug, "msp3410 thread: sleep\n");
0697 msp_sleep(state, -1);
0698 dev_dbg_lvl(&client->dev, 2, msp_debug, "msp3410 thread: wakeup\n");
0699
0700 restart:
0701 dev_dbg_lvl(&client->dev, 2, msp_debug, "thread: restart scan\n");
0702 state->restart = 0;
0703 if (kthread_should_stop())
0704 break;
0705
0706 if (state->mode == MSP_MODE_EXTERN) {
0707
0708 dev_dbg_lvl(&client->dev, 1, msp_debug,
0709 "thread: no carrier scan\n");
0710 state->scan_in_progress = 0;
0711 msp_update_volume(state);
0712 continue;
0713 }
0714
0715
0716 state->scan_in_progress = 1;
0717 msp_update_volume(state);
0718
0719
0720
0721
0722 if (state->radio)
0723 std = 0x40;
0724 else
0725 std = (state->v4l2_std & V4L2_STD_NTSC) ? 0x20 : 1;
0726 state->watch_stereo = 0;
0727 state->nicam_on = 0;
0728
0729
0730 if (msp_sleep(state, 200))
0731 goto restart;
0732
0733 if (msp_debug)
0734 dev_dbg_lvl(&client->dev, 2, msp_debug,
0735 "setting standard: %s (0x%04x)\n",
0736 msp_standard_std_name(std), std);
0737
0738 if (std != 1) {
0739
0740 val = std;
0741 } else {
0742
0743 msp_write_dem(client, 0x20, std);
0744 for (;;) {
0745 if (msp_sleep(state, 100))
0746 goto restart;
0747
0748
0749 val = msp_read_dem(client, 0x7e);
0750 if (val < 0x07ff)
0751 break;
0752 dev_dbg_lvl(&client->dev, 2, msp_debug,
0753 "detection still in progress\n");
0754 }
0755 }
0756 for (i = 0; msp_stdlist[i].name != NULL; i++)
0757 if (msp_stdlist[i].retval == val)
0758 break;
0759 dev_dbg_lvl(&client->dev, 1, msp_debug, "current standard: %s (0x%04x)\n",
0760 msp_standard_std_name(val), val);
0761 state->main = msp_stdlist[i].main;
0762 state->second = msp_stdlist[i].second;
0763 state->std = val;
0764 state->rxsubchans = V4L2_TUNER_SUB_MONO;
0765
0766 if (msp_amsound && !state->radio &&
0767 (state->v4l2_std & V4L2_STD_SECAM) && (val != 0x0009)) {
0768
0769 dev_dbg_lvl(&client->dev, 1, msp_debug, "autodetection failed, switching to backup standard: %s (0x%04x)\n",
0770 msp_stdlist[8].name ?
0771 msp_stdlist[8].name : "unknown", val);
0772 state->std = val = 0x0009;
0773 msp_write_dem(client, 0x20, val);
0774 } else {
0775 state->detected_std = msp_standard_std(state->std);
0776 }
0777
0778
0779 switch (val) {
0780 case 0x0008:
0781 case 0x000a:
0782 case 0x000b:
0783 if (val == 0x000a)
0784 state->mode = MSP_MODE_FM_NICAM2;
0785 else
0786 state->mode = MSP_MODE_FM_NICAM1;
0787
0788 state->nicam_on = 1;
0789 state->watch_stereo = 1;
0790 break;
0791 case 0x0009:
0792 state->mode = MSP_MODE_AM_NICAM;
0793 state->nicam_on = 1;
0794 state->watch_stereo = 1;
0795 break;
0796 case 0x0020:
0797
0798 state->mode = MSP_MODE_BTSC;
0799 break;
0800 case 0x0040:
0801 state->mode = MSP_MODE_FM_RADIO;
0802 state->rxsubchans = V4L2_TUNER_SUB_STEREO;
0803
0804
0805 msp3400c_set_mode(client, MSP_MODE_FM_RADIO);
0806 msp3400c_set_carrier(client, MSP_CARRIER(10.7),
0807 MSP_CARRIER(10.7));
0808 break;
0809 case 0x0002:
0810 case 0x0003:
0811 case 0x0004:
0812 case 0x0005:
0813 state->mode = MSP_MODE_FM_TERRA;
0814 state->watch_stereo = 1;
0815 break;
0816 }
0817
0818
0819 msp_write_dsp(client, 0x0d, 0x1900);
0820 msp_write_dsp(client, 0x0e, 0x3000);
0821 if (state->has_nicam)
0822 msp_write_dsp(client, 0x10, 0x5a00);
0823
0824 if (state->has_i2s_conf)
0825 msp_write_dem(client, 0x40, state->i2s_mode);
0826
0827
0828 msp3400c_set_audmode(client);
0829 state->scan_in_progress = 0;
0830 msp_update_volume(state);
0831
0832
0833
0834 count = 3;
0835 while (state->watch_stereo) {
0836 if (msp_sleep(state, count ? 1000 : 5000))
0837 goto restart;
0838 if (count)
0839 count--;
0840 watch_stereo(client);
0841 }
0842 }
0843 dev_dbg_lvl(&client->dev, 1, msp_debug, "thread: exit\n");
0844 return 0;
0845 }
0846
0847
0848
0849
0850
0851
0852
0853
0854
0855 static int msp34xxg_modus(struct i2c_client *client)
0856 {
0857 struct msp_state *state = to_state(i2c_get_clientdata(client));
0858
0859 if (state->radio) {
0860 dev_dbg_lvl(&client->dev, 1, msp_debug, "selected radio modus\n");
0861 return 0x0001;
0862 }
0863 if (state->v4l2_std == V4L2_STD_NTSC_M_JP) {
0864 dev_dbg_lvl(&client->dev, 1, msp_debug, "selected M (EIA-J) modus\n");
0865 return 0x4001;
0866 }
0867 if (state->v4l2_std == V4L2_STD_NTSC_M_KR) {
0868 dev_dbg_lvl(&client->dev, 1, msp_debug, "selected M (A2) modus\n");
0869 return 0x0001;
0870 }
0871 if (state->v4l2_std == V4L2_STD_SECAM_L) {
0872 dev_dbg_lvl(&client->dev, 1, msp_debug, "selected SECAM-L modus\n");
0873 return 0x6001;
0874 }
0875 if (state->v4l2_std & V4L2_STD_MN) {
0876 dev_dbg_lvl(&client->dev, 1, msp_debug, "selected M (BTSC) modus\n");
0877 return 0x2001;
0878 }
0879 return 0x7001;
0880 }
0881
0882 static void msp34xxg_set_source(struct i2c_client *client, u16 reg, int in)
0883 {
0884 struct msp_state *state = to_state(i2c_get_clientdata(client));
0885 int source, matrix;
0886
0887 switch (state->audmode) {
0888 case V4L2_TUNER_MODE_MONO:
0889 source = 0;
0890 matrix = 0x30;
0891 break;
0892 case V4L2_TUNER_MODE_LANG2:
0893 source = 4;
0894 matrix = 0x10;
0895 break;
0896 case V4L2_TUNER_MODE_LANG1_LANG2:
0897 source = 1;
0898 matrix = 0x20;
0899 break;
0900 case V4L2_TUNER_MODE_LANG1:
0901 source = 3;
0902 matrix = 0x00;
0903 break;
0904 case V4L2_TUNER_MODE_STEREO:
0905 default:
0906 source = 3;
0907 matrix = 0x20;
0908 break;
0909 }
0910
0911 if (in == MSP_DSP_IN_TUNER)
0912 source = (source << 8) | 0x20;
0913
0914
0915 else if (in >= MSP_DSP_IN_MAIN_AVC && state->has_dolby_pro_logic)
0916 source = ((in + 1) << 8) | matrix;
0917 else
0918 source = (in << 8) | matrix;
0919
0920 dev_dbg_lvl(&client->dev, 1, msp_debug,
0921 "set source to %d (0x%x) for output %02x\n", in, source, reg);
0922 msp_write_dsp(client, reg, source);
0923 }
0924
0925 static void msp34xxg_set_sources(struct i2c_client *client)
0926 {
0927 struct msp_state *state = to_state(i2c_get_clientdata(client));
0928 u32 in = state->route_in;
0929
0930 msp34xxg_set_source(client, 0x0008, (in >> 4) & 0xf);
0931
0932 msp34xxg_set_source(client, 0x000c, (in >> 4) & 0xf);
0933 msp34xxg_set_source(client, 0x0009, (in >> 8) & 0xf);
0934 msp34xxg_set_source(client, 0x000a, (in >> 12) & 0xf);
0935 if (state->has_scart2_out)
0936 msp34xxg_set_source(client, 0x0041, (in >> 16) & 0xf);
0937 msp34xxg_set_source(client, 0x000b, (in >> 20) & 0xf);
0938 }
0939
0940
0941 static void msp34xxg_reset(struct i2c_client *client)
0942 {
0943 struct msp_state *state = to_state(i2c_get_clientdata(client));
0944 int tuner = (state->route_in >> 3) & 1;
0945 int modus;
0946
0947
0948
0949 state->std = 1;
0950
0951 msp_reset(client);
0952
0953 if (state->has_i2s_conf)
0954 msp_write_dem(client, 0x40, state->i2s_mode);
0955
0956
0957 modus = msp34xxg_modus(client);
0958 modus |= tuner ? 0x100 : 0;
0959 msp_write_dem(client, 0x30, modus);
0960
0961
0962
0963 msp34xxg_set_sources(client);
0964
0965 msp_write_dsp(client, 0x0d, 0x1900);
0966 msp_write_dsp(client, 0x0e, 0x3000);
0967 if (state->has_nicam)
0968 msp_write_dsp(client, 0x10, 0x5a00);
0969
0970
0971
0972
0973
0974
0975
0976
0977
0978
0979
0980 msp_write_dem(client, 0x22, msp_stereo_thresh);
0981 }
0982
0983 int msp34xxg_thread(void *data)
0984 {
0985 struct i2c_client *client = data;
0986 struct msp_state *state = to_state(i2c_get_clientdata(client));
0987 int val, i;
0988
0989 dev_dbg_lvl(&client->dev, 1, msp_debug, "msp34xxg daemon started\n");
0990 state->detected_std = V4L2_STD_ALL;
0991 set_freezable();
0992 for (;;) {
0993 dev_dbg_lvl(&client->dev, 2, msp_debug, "msp34xxg thread: sleep\n");
0994 msp_sleep(state, -1);
0995 dev_dbg_lvl(&client->dev, 2, msp_debug, "msp34xxg thread: wakeup\n");
0996
0997 restart:
0998 dev_dbg_lvl(&client->dev, 1, msp_debug, "thread: restart scan\n");
0999 state->restart = 0;
1000 if (kthread_should_stop())
1001 break;
1002
1003 if (state->mode == MSP_MODE_EXTERN) {
1004
1005 dev_dbg_lvl(&client->dev, 1, msp_debug,
1006 "thread: no carrier scan\n");
1007 state->scan_in_progress = 0;
1008 msp_update_volume(state);
1009 continue;
1010 }
1011
1012
1013 msp34xxg_reset(client);
1014 state->std = state->radio ? 0x40 :
1015 (state->force_btsc && msp_standard == 1) ? 32 : msp_standard;
1016 msp_write_dem(client, 0x20, state->std);
1017
1018 if (state->std != 1)
1019 goto unmute;
1020
1021
1022 dev_dbg_lvl(&client->dev, 1, msp_debug,
1023 "started autodetect, waiting for result\n");
1024 for (i = 0; i < 10; i++) {
1025 if (msp_sleep(state, 100))
1026 goto restart;
1027
1028
1029 val = msp_read_dem(client, 0x7e);
1030 if (val < 0x07ff) {
1031 state->std = val;
1032 break;
1033 }
1034 dev_dbg_lvl(&client->dev, 2, msp_debug,
1035 "detection still in progress\n");
1036 }
1037 if (state->std == 1) {
1038 dev_dbg_lvl(&client->dev, 1, msp_debug,
1039 "detection still in progress after 10 tries. giving up.\n");
1040 continue;
1041 }
1042
1043 unmute:
1044 dev_dbg_lvl(&client->dev, 1, msp_debug,
1045 "detected standard: %s (0x%04x)\n",
1046 msp_standard_std_name(state->std), state->std);
1047 state->detected_std = msp_standard_std(state->std);
1048
1049 if (state->std == 9) {
1050
1051 msp_write_dsp(client, 0x0e, 0x7c00);
1052 }
1053
1054
1055 msp_update_volume(state);
1056
1057
1058 if (msp_write_dsp(client, 0x13, state->acb))
1059 return -1;
1060
1061
1062
1063 if (state->std != 0x20)
1064 continue;
1065
1066 state->watch_stereo = 1;
1067
1068
1069
1070 watch_stereo(client);
1071 while (state->watch_stereo) {
1072 watch_stereo(client);
1073 if (msp_sleep(state, 5000))
1074 goto restart;
1075 }
1076 }
1077 dev_dbg_lvl(&client->dev, 1, msp_debug, "thread: exit\n");
1078 return 0;
1079 }
1080
1081 static int msp34xxg_detect_stereo(struct i2c_client *client)
1082 {
1083 struct msp_state *state = to_state(i2c_get_clientdata(client));
1084 int status = msp_read_dem(client, 0x0200);
1085 int is_bilingual = status & 0x100;
1086 int is_stereo = status & 0x40;
1087 int oldrx = state->rxsubchans;
1088
1089 if (state->mode == MSP_MODE_EXTERN)
1090 return 0;
1091
1092 state->rxsubchans = 0;
1093 if (is_stereo)
1094 state->rxsubchans = V4L2_TUNER_SUB_STEREO;
1095 else
1096 state->rxsubchans = V4L2_TUNER_SUB_MONO;
1097 if (is_bilingual) {
1098 if (state->std == 0x20)
1099 state->rxsubchans |= V4L2_TUNER_SUB_SAP;
1100 else
1101 state->rxsubchans =
1102 V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
1103 }
1104 dev_dbg_lvl(&client->dev, 1, msp_debug,
1105 "status=0x%x, stereo=%d, bilingual=%d -> rxsubchans=%d\n",
1106 status, is_stereo, is_bilingual, state->rxsubchans);
1107 return (oldrx != state->rxsubchans);
1108 }
1109
1110 static void msp34xxg_set_audmode(struct i2c_client *client)
1111 {
1112 struct msp_state *state = to_state(i2c_get_clientdata(client));
1113
1114 if (state->std == 0x20) {
1115 if ((state->rxsubchans & V4L2_TUNER_SUB_SAP) &&
1116 (state->audmode == V4L2_TUNER_MODE_LANG1_LANG2 ||
1117 state->audmode == V4L2_TUNER_MODE_LANG2)) {
1118 msp_write_dem(client, 0x20, 0x21);
1119 } else {
1120 msp_write_dem(client, 0x20, 0x20);
1121 }
1122 }
1123
1124 msp34xxg_set_sources(client);
1125 }
1126
1127 void msp_set_audmode(struct i2c_client *client)
1128 {
1129 struct msp_state *state = to_state(i2c_get_clientdata(client));
1130
1131 switch (state->opmode) {
1132 case OPMODE_MANUAL:
1133 case OPMODE_AUTODETECT:
1134 msp3400c_set_audmode(client);
1135 break;
1136 case OPMODE_AUTOSELECT:
1137 msp34xxg_set_audmode(client);
1138 break;
1139 }
1140 }
1141
1142 int msp_detect_stereo(struct i2c_client *client)
1143 {
1144 struct msp_state *state = to_state(i2c_get_clientdata(client));
1145
1146 switch (state->opmode) {
1147 case OPMODE_MANUAL:
1148 case OPMODE_AUTODETECT:
1149 return msp3400c_detect_stereo(client);
1150 case OPMODE_AUTOSELECT:
1151 return msp34xxg_detect_stereo(client);
1152 }
1153 return 0;
1154 }
1155