1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Programming the mspx4xx sound processor family 4 * 5 * (c) 1997-2001 Gerd Knorr <kraxel@bytesex.org> 6 */ 7 8 9 #include <linux/kernel.h> 10 #include <linux/module.h> 11 #include <linux/i2c.h> 12 #include <linux/freezer.h> 13 #include <linux/videodev2.h> 14 #include <media/v4l2-common.h> 15 #include <media/drv-intf/msp3400.h> 16 #include <linux/kthread.h> 17 #include <linux/suspend.h> 18 #include "msp3400-driver.h" 19 20 /* this one uses the automatic sound standard detection of newer msp34xx 21 chip versions */ 22 static struct { 23 int retval; 24 int main, second; 25 char *name; 26 v4l2_std_id std; 27 } msp_stdlist[] = { 28 { 0x0000, 0, 0, "could not detect sound standard", V4L2_STD_ALL }, 29 { 0x0001, 0, 0, "autodetect start", V4L2_STD_ALL }, 30 { 0x0002, MSP_CARRIER(4.5), MSP_CARRIER(4.72), 31 "4.5/4.72 M Dual FM-Stereo", V4L2_STD_MN }, 32 { 0x0003, MSP_CARRIER(5.5), MSP_CARRIER(5.7421875), 33 "5.5/5.74 B/G Dual FM-Stereo", V4L2_STD_BG }, 34 { 0x0004, MSP_CARRIER(6.5), MSP_CARRIER(6.2578125), 35 "6.5/6.25 D/K1 Dual FM-Stereo", V4L2_STD_DK }, 36 { 0x0005, MSP_CARRIER(6.5), MSP_CARRIER(6.7421875), 37 "6.5/6.74 D/K2 Dual FM-Stereo", V4L2_STD_DK }, 38 { 0x0006, MSP_CARRIER(6.5), MSP_CARRIER(6.5), 39 "6.5 D/K FM-Mono (HDEV3)", V4L2_STD_DK }, 40 { 0x0007, MSP_CARRIER(6.5), MSP_CARRIER(5.7421875), 41 "6.5/5.74 D/K3 Dual FM-Stereo", V4L2_STD_DK }, 42 { 0x0008, MSP_CARRIER(5.5), MSP_CARRIER(5.85), 43 "5.5/5.85 B/G NICAM FM", V4L2_STD_BG }, 44 { 0x0009, MSP_CARRIER(6.5), MSP_CARRIER(5.85), 45 "6.5/5.85 L NICAM AM", V4L2_STD_L }, 46 { 0x000a, MSP_CARRIER(6.0), MSP_CARRIER(6.55), 47 "6.0/6.55 I NICAM FM", V4L2_STD_PAL_I }, 48 { 0x000b, MSP_CARRIER(6.5), MSP_CARRIER(5.85), 49 "6.5/5.85 D/K NICAM FM", V4L2_STD_DK }, 50 { 0x000c, MSP_CARRIER(6.5), MSP_CARRIER(5.85), 51 "6.5/5.85 D/K NICAM FM (HDEV2)", V4L2_STD_DK }, 52 { 0x000d, MSP_CARRIER(6.5), MSP_CARRIER(5.85), 53 "6.5/5.85 D/K NICAM FM (HDEV3)", V4L2_STD_DK }, 54 { 0x0020, MSP_CARRIER(4.5), MSP_CARRIER(4.5), 55 "4.5 M BTSC-Stereo", V4L2_STD_MTS }, 56 { 0x0021, MSP_CARRIER(4.5), MSP_CARRIER(4.5), 57 "4.5 M BTSC-Mono + SAP", V4L2_STD_MTS }, 58 { 0x0030, MSP_CARRIER(4.5), MSP_CARRIER(4.5), 59 "4.5 M EIA-J Japan Stereo", V4L2_STD_NTSC_M_JP }, 60 { 0x0040, MSP_CARRIER(10.7), MSP_CARRIER(10.7), 61 "10.7 FM-Stereo Radio", V4L2_STD_ALL }, 62 { 0x0050, MSP_CARRIER(6.5), MSP_CARRIER(6.5), 63 "6.5 SAT-Mono", V4L2_STD_ALL }, 64 { 0x0051, MSP_CARRIER(7.02), MSP_CARRIER(7.20), 65 "7.02/7.20 SAT-Stereo", V4L2_STD_ALL }, 66 { 0x0060, MSP_CARRIER(7.2), MSP_CARRIER(7.2), 67 "7.2 SAT ADR", V4L2_STD_ALL }, 68 { -1, 0, 0, NULL, 0 }, /* EOF */ 69 }; 70 71 static struct msp3400c_init_data_dem { 72 int fir1[6]; 73 int fir2[6]; 74 int cdo1; 75 int cdo2; 76 int ad_cv; 77 int mode_reg; 78 int dsp_src; 79 int dsp_matrix; 80 } msp3400c_init_data[] = { 81 { /* AM (for carrier detect / msp3400) */ 82 {75, 19, 36, 35, 39, 40}, 83 {75, 19, 36, 35, 39, 40}, 84 MSP_CARRIER(5.5), MSP_CARRIER(5.5), 85 0x00d0, 0x0500, 0x0020, 0x3000 86 }, { /* AM (for carrier detect / msp3410) */ 87 {-1, -1, -8, 2, 59, 126}, 88 {-1, -1, -8, 2, 59, 126}, 89 MSP_CARRIER(5.5), MSP_CARRIER(5.5), 90 0x00d0, 0x0100, 0x0020, 0x3000 91 }, { /* FM Radio */ 92 {-8, -8, 4, 6, 78, 107}, 93 {-8, -8, 4, 6, 78, 107}, 94 MSP_CARRIER(10.7), MSP_CARRIER(10.7), 95 0x00d0, 0x0480, 0x0020, 0x3000 96 }, { /* Terrestrial FM-mono + FM-stereo */ 97 {3, 18, 27, 48, 66, 72}, 98 {3, 18, 27, 48, 66, 72}, 99 MSP_CARRIER(5.5), MSP_CARRIER(5.5), 100 0x00d0, 0x0480, 0x0030, 0x3000 101 }, { /* Sat FM-mono */ 102 { 1, 9, 14, 24, 33, 37}, 103 { 3, 18, 27, 48, 66, 72}, 104 MSP_CARRIER(6.5), MSP_CARRIER(6.5), 105 0x00c6, 0x0480, 0x0000, 0x3000 106 }, { /* NICAM/FM -- B/G (5.5/5.85), D/K (6.5/5.85) */ 107 {-2, -8, -10, 10, 50, 86}, 108 {3, 18, 27, 48, 66, 72}, 109 MSP_CARRIER(5.5), MSP_CARRIER(5.5), 110 0x00d0, 0x0040, 0x0120, 0x3000 111 }, { /* NICAM/FM -- I (6.0/6.552) */ 112 {2, 4, -6, -4, 40, 94}, 113 {3, 18, 27, 48, 66, 72}, 114 MSP_CARRIER(6.0), MSP_CARRIER(6.0), 115 0x00d0, 0x0040, 0x0120, 0x3000 116 }, { /* NICAM/AM -- L (6.5/5.85) */ 117 {-2, -8, -10, 10, 50, 86}, 118 {-4, -12, -9, 23, 79, 126}, 119 MSP_CARRIER(6.5), MSP_CARRIER(6.5), 120 0x00c6, 0x0140, 0x0120, 0x7c00 121 }, 122 }; 123 124 struct msp3400c_carrier_detect { 125 int cdo; 126 char *name; 127 }; 128 129 static struct msp3400c_carrier_detect msp3400c_carrier_detect_main[] = { 130 /* main carrier */ 131 { MSP_CARRIER(4.5), "4.5 NTSC" }, 132 { MSP_CARRIER(5.5), "5.5 PAL B/G" }, 133 { MSP_CARRIER(6.0), "6.0 PAL I" }, 134 { MSP_CARRIER(6.5), "6.5 PAL D/K + SAT + SECAM" } 135 }; 136 137 static struct msp3400c_carrier_detect msp3400c_carrier_detect_55[] = { 138 /* PAL B/G */ 139 { MSP_CARRIER(5.7421875), "5.742 PAL B/G FM-stereo" }, 140 { MSP_CARRIER(5.85), "5.85 PAL B/G NICAM" } 141 }; 142 143 static struct msp3400c_carrier_detect msp3400c_carrier_detect_65[] = { 144 /* PAL SAT / SECAM */ 145 { MSP_CARRIER(5.85), "5.85 PAL D/K + SECAM NICAM" }, 146 { MSP_CARRIER(6.2578125), "6.25 PAL D/K1 FM-stereo" }, 147 { MSP_CARRIER(6.7421875), "6.74 PAL D/K2 FM-stereo" }, 148 { MSP_CARRIER(7.02), "7.02 PAL SAT FM-stereo s/b" }, 149 { MSP_CARRIER(7.20), "7.20 PAL SAT FM-stereo s" }, 150 { MSP_CARRIER(7.38), "7.38 PAL SAT FM-stereo b" }, 151 }; 152 153 /* ------------------------------------------------------------------------ */ 154 155 const char *msp_standard_std_name(int std) 156 { 157 int i; 158 159 for (i = 0; msp_stdlist[i].name != NULL; i++) 160 if (msp_stdlist[i].retval == std) 161 return msp_stdlist[i].name; 162 return "unknown"; 163 } 164 165 static v4l2_std_id msp_standard_std(int std) 166 { 167 int i; 168 169 for (i = 0; msp_stdlist[i].name != NULL; i++) 170 if (msp_stdlist[i].retval == std) 171 return msp_stdlist[i].std; 172 return V4L2_STD_ALL; 173 } 174 175 static void msp_set_source(struct i2c_client *client, u16 src) 176 { 177 struct msp_state *state = to_state(i2c_get_clientdata(client)); 178 179 if (msp_dolby) { 180 msp_write_dsp(client, 0x0008, 0x0520); /* I2S1 */ 181 msp_write_dsp(client, 0x0009, 0x0620); /* I2S2 */ 182 } else { 183 msp_write_dsp(client, 0x0008, src); 184 msp_write_dsp(client, 0x0009, src); 185 } 186 msp_write_dsp(client, 0x000a, src); 187 msp_write_dsp(client, 0x000b, src); 188 msp_write_dsp(client, 0x000c, src); 189 if (state->has_scart2_out) 190 msp_write_dsp(client, 0x0041, src); 191 } 192 193 void msp3400c_set_carrier(struct i2c_client *client, int cdo1, int cdo2) 194 { 195 msp_write_dem(client, 0x0093, cdo1 & 0xfff); 196 msp_write_dem(client, 0x009b, cdo1 >> 12); 197 msp_write_dem(client, 0x00a3, cdo2 & 0xfff); 198 msp_write_dem(client, 0x00ab, cdo2 >> 12); 199 msp_write_dem(client, 0x0056, 0); /* LOAD_REG_1/2 */ 200 } 201 202 void msp3400c_set_mode(struct i2c_client *client, int mode) 203 { 204 struct msp_state *state = to_state(i2c_get_clientdata(client)); 205 struct msp3400c_init_data_dem *data = &msp3400c_init_data[mode]; 206 int tuner = (state->route_in >> 3) & 1; 207 int i; 208 209 dev_dbg_lvl(&client->dev, 1, msp_debug, "set_mode: %d\n", mode); 210 state->mode = mode; 211 state->rxsubchans = V4L2_TUNER_SUB_MONO; 212 213 msp_write_dem(client, 0x00bb, data->ad_cv | (tuner ? 0x100 : 0)); 214 215 for (i = 5; i >= 0; i--) /* fir 1 */ 216 msp_write_dem(client, 0x0001, data->fir1[i]); 217 218 msp_write_dem(client, 0x0005, 0x0004); /* fir 2 */ 219 msp_write_dem(client, 0x0005, 0x0040); 220 msp_write_dem(client, 0x0005, 0x0000); 221 for (i = 5; i >= 0; i--) 222 msp_write_dem(client, 0x0005, data->fir2[i]); 223 224 msp_write_dem(client, 0x0083, data->mode_reg); 225 226 msp3400c_set_carrier(client, data->cdo1, data->cdo2); 227 228 msp_set_source(client, data->dsp_src); 229 /* set prescales */ 230 231 /* volume prescale for SCART (AM mono input) */ 232 msp_write_dsp(client, 0x000d, 0x1900); 233 msp_write_dsp(client, 0x000e, data->dsp_matrix); 234 if (state->has_nicam) /* nicam prescale */ 235 msp_write_dsp(client, 0x0010, 0x5a00); 236 } 237 238 /* Set audio mode. Note that the pre-'G' models do not support BTSC+SAP, 239 nor do they support stereo BTSC. */ 240 static void msp3400c_set_audmode(struct i2c_client *client) 241 { 242 static char *strmode[] = { 243 "mono", "stereo", "lang2", "lang1", "lang1+lang2" 244 }; 245 struct msp_state *state = to_state(i2c_get_clientdata(client)); 246 char *modestr = (state->audmode >= 0 && state->audmode < 5) ? 247 strmode[state->audmode] : "unknown"; 248 int src = 0; /* channel source: FM/AM, nicam or SCART */ 249 int audmode = state->audmode; 250 251 if (state->opmode == OPMODE_AUTOSELECT) { 252 /* this method would break everything, let's make sure 253 * it's never called 254 */ 255 dev_dbg_lvl(&client->dev, 1, msp_debug, 256 "set_audmode called with mode=%d instead of set_source (ignored)\n", 257 state->audmode); 258 return; 259 } 260 261 /* Note: for the C and D revs no NTSC stereo + SAP is possible as 262 the hardware does not support SAP. So the rxsubchans combination 263 of STEREO | LANG2 does not occur. */ 264 265 if (state->mode != MSP_MODE_EXTERN) { 266 /* switch to mono if only mono is available */ 267 if (state->rxsubchans == V4L2_TUNER_SUB_MONO) 268 audmode = V4L2_TUNER_MODE_MONO; 269 /* if bilingual */ 270 else if (state->rxsubchans & V4L2_TUNER_SUB_LANG2) { 271 /* and mono or stereo, then fallback to lang1 */ 272 if (audmode == V4L2_TUNER_MODE_MONO || 273 audmode == V4L2_TUNER_MODE_STEREO) 274 audmode = V4L2_TUNER_MODE_LANG1; 275 } 276 /* if stereo, and audmode is not mono, then switch to stereo */ 277 else if (audmode != V4L2_TUNER_MODE_MONO) 278 audmode = V4L2_TUNER_MODE_STEREO; 279 } 280 281 /* switch demodulator */ 282 switch (state->mode) { 283 case MSP_MODE_FM_TERRA: 284 dev_dbg_lvl(&client->dev, 1, msp_debug, "FM set_audmode: %s\n", modestr); 285 switch (audmode) { 286 case V4L2_TUNER_MODE_STEREO: 287 msp_write_dsp(client, 0x000e, 0x3001); 288 break; 289 case V4L2_TUNER_MODE_MONO: 290 case V4L2_TUNER_MODE_LANG1: 291 case V4L2_TUNER_MODE_LANG2: 292 case V4L2_TUNER_MODE_LANG1_LANG2: 293 msp_write_dsp(client, 0x000e, 0x3000); 294 break; 295 } 296 break; 297 case MSP_MODE_FM_SAT: 298 dev_dbg_lvl(&client->dev, 1, msp_debug, "SAT set_audmode: %s\n", modestr); 299 switch (audmode) { 300 case V4L2_TUNER_MODE_MONO: 301 msp3400c_set_carrier(client, MSP_CARRIER(6.5), MSP_CARRIER(6.5)); 302 break; 303 case V4L2_TUNER_MODE_STEREO: 304 case V4L2_TUNER_MODE_LANG1_LANG2: 305 msp3400c_set_carrier(client, MSP_CARRIER(7.2), MSP_CARRIER(7.02)); 306 break; 307 case V4L2_TUNER_MODE_LANG1: 308 msp3400c_set_carrier(client, MSP_CARRIER(7.38), MSP_CARRIER(7.02)); 309 break; 310 case V4L2_TUNER_MODE_LANG2: 311 msp3400c_set_carrier(client, MSP_CARRIER(7.38), MSP_CARRIER(7.02)); 312 break; 313 } 314 break; 315 case MSP_MODE_FM_NICAM1: 316 case MSP_MODE_FM_NICAM2: 317 case MSP_MODE_AM_NICAM: 318 dev_dbg_lvl(&client->dev, 1, msp_debug, 319 "NICAM set_audmode: %s\n", modestr); 320 if (state->nicam_on) 321 src = 0x0100; /* NICAM */ 322 break; 323 case MSP_MODE_BTSC: 324 dev_dbg_lvl(&client->dev, 1, msp_debug, 325 "BTSC set_audmode: %s\n", modestr); 326 break; 327 case MSP_MODE_EXTERN: 328 dev_dbg_lvl(&client->dev, 1, msp_debug, 329 "extern set_audmode: %s\n", modestr); 330 src = 0x0200; /* SCART */ 331 break; 332 case MSP_MODE_FM_RADIO: 333 dev_dbg_lvl(&client->dev, 1, msp_debug, 334 "FM-Radio set_audmode: %s\n", modestr); 335 break; 336 default: 337 dev_dbg_lvl(&client->dev, 1, msp_debug, "mono set_audmode\n"); 338 return; 339 } 340 341 /* switch audio */ 342 dev_dbg_lvl(&client->dev, 1, msp_debug, "set audmode %d\n", audmode); 343 switch (audmode) { 344 case V4L2_TUNER_MODE_STEREO: 345 case V4L2_TUNER_MODE_LANG1_LANG2: 346 src |= 0x0020; 347 break; 348 case V4L2_TUNER_MODE_MONO: 349 if (state->mode == MSP_MODE_AM_NICAM) { 350 dev_dbg_lvl(&client->dev, 1, msp_debug, "switching to AM mono\n"); 351 /* AM mono decoding is handled by tuner, not MSP chip */ 352 /* SCART switching control register */ 353 msp_set_scart(client, SCART_MONO, 0); 354 src = 0x0200; 355 break; 356 } 357 if (state->rxsubchans & V4L2_TUNER_SUB_STEREO) 358 src = 0x0030; 359 break; 360 case V4L2_TUNER_MODE_LANG1: 361 break; 362 case V4L2_TUNER_MODE_LANG2: 363 src |= 0x0010; 364 break; 365 } 366 dev_dbg_lvl(&client->dev, 1, msp_debug, 367 "set_audmode final source/matrix = 0x%x\n", src); 368 369 msp_set_source(client, src); 370 } 371 372 static void msp3400c_print_mode(struct i2c_client *client) 373 { 374 struct msp_state *state = to_state(i2c_get_clientdata(client)); 375 376 if (state->main == state->second) 377 dev_dbg_lvl(&client->dev, 1, msp_debug, 378 "mono sound carrier: %d.%03d MHz\n", 379 state->main / 910000, (state->main / 910) % 1000); 380 else 381 dev_dbg_lvl(&client->dev, 1, msp_debug, 382 "main sound carrier: %d.%03d MHz\n", 383 state->main / 910000, (state->main / 910) % 1000); 384 if (state->mode == MSP_MODE_FM_NICAM1 || state->mode == MSP_MODE_FM_NICAM2) 385 dev_dbg_lvl(&client->dev, 1, msp_debug, 386 "NICAM/FM carrier : %d.%03d MHz\n", 387 state->second / 910000, (state->second/910) % 1000); 388 if (state->mode == MSP_MODE_AM_NICAM) 389 dev_dbg_lvl(&client->dev, 1, msp_debug, 390 "NICAM/AM carrier : %d.%03d MHz\n", 391 state->second / 910000, (state->second / 910) % 1000); 392 if (state->mode == MSP_MODE_FM_TERRA && state->main != state->second) { 393 dev_dbg_lvl(&client->dev, 1, msp_debug, 394 "FM-stereo carrier : %d.%03d MHz\n", 395 state->second / 910000, (state->second / 910) % 1000); 396 } 397 } 398 399 /* ----------------------------------------------------------------------- */ 400 401 static int msp3400c_detect_stereo(struct i2c_client *client) 402 { 403 struct msp_state *state = to_state(i2c_get_clientdata(client)); 404 int val; 405 int rxsubchans = state->rxsubchans; 406 int newnicam = state->nicam_on; 407 int update = 0; 408 409 switch (state->mode) { 410 case MSP_MODE_FM_TERRA: 411 val = msp_read_dsp(client, 0x18); 412 if (val > 32767) 413 val -= 65536; 414 dev_dbg_lvl(&client->dev, 2, msp_debug, 415 "stereo detect register: %d\n", val); 416 if (val > 8192) { 417 rxsubchans = V4L2_TUNER_SUB_STEREO; 418 } else if (val < -4096) { 419 rxsubchans = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2; 420 } else { 421 rxsubchans = V4L2_TUNER_SUB_MONO; 422 } 423 newnicam = 0; 424 break; 425 case MSP_MODE_FM_NICAM1: 426 case MSP_MODE_FM_NICAM2: 427 case MSP_MODE_AM_NICAM: 428 val = msp_read_dem(client, 0x23); 429 dev_dbg_lvl(&client->dev, 2, msp_debug, "nicam sync=%d, mode=%d\n", 430 val & 1, (val & 0x1e) >> 1); 431 432 if (val & 1) { 433 /* nicam synced */ 434 switch ((val & 0x1e) >> 1) { 435 case 0: 436 case 8: 437 rxsubchans = V4L2_TUNER_SUB_STEREO; 438 break; 439 case 1: 440 case 9: 441 rxsubchans = V4L2_TUNER_SUB_MONO; 442 break; 443 case 2: 444 case 10: 445 rxsubchans = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2; 446 break; 447 default: 448 rxsubchans = V4L2_TUNER_SUB_MONO; 449 break; 450 } 451 newnicam = 1; 452 } else { 453 newnicam = 0; 454 rxsubchans = V4L2_TUNER_SUB_MONO; 455 } 456 break; 457 } 458 if (rxsubchans != state->rxsubchans) { 459 update = 1; 460 dev_dbg_lvl(&client->dev, 1, msp_debug, 461 "watch: rxsubchans %02x => %02x\n", 462 state->rxsubchans, rxsubchans); 463 state->rxsubchans = rxsubchans; 464 } 465 if (newnicam != state->nicam_on) { 466 update = 1; 467 dev_dbg_lvl(&client->dev, 1, msp_debug, "watch: nicam %d => %d\n", 468 state->nicam_on, newnicam); 469 state->nicam_on = newnicam; 470 } 471 return update; 472 } 473 474 /* 475 * A kernel thread for msp3400 control -- we don't want to block the 476 * in the ioctl while doing the sound carrier & stereo detect 477 */ 478 /* stereo/multilang monitoring */ 479 static void watch_stereo(struct i2c_client *client) 480 { 481 struct msp_state *state = to_state(i2c_get_clientdata(client)); 482 483 if (msp_detect_stereo(client)) 484 msp_set_audmode(client); 485 486 if (msp_once) 487 state->watch_stereo = 0; 488 } 489 490 int msp3400c_thread(void *data) 491 { 492 struct i2c_client *client = data; 493 struct msp_state *state = to_state(i2c_get_clientdata(client)); 494 struct msp3400c_carrier_detect *cd; 495 int count, max1, max2, val1, val2, val, i; 496 497 dev_dbg_lvl(&client->dev, 1, msp_debug, "msp3400 daemon started\n"); 498 state->detected_std = V4L2_STD_ALL; 499 set_freezable(); 500 for (;;) { 501 dev_dbg_lvl(&client->dev, 2, msp_debug, "msp3400 thread: sleep\n"); 502 msp_sleep(state, -1); 503 dev_dbg_lvl(&client->dev, 2, msp_debug, "msp3400 thread: wakeup\n"); 504 505 restart: 506 dev_dbg_lvl(&client->dev, 2, msp_debug, "thread: restart scan\n"); 507 state->restart = 0; 508 if (kthread_should_stop()) 509 break; 510 511 if (state->radio || MSP_MODE_EXTERN == state->mode) { 512 /* no carrier scan, just unmute */ 513 dev_dbg_lvl(&client->dev, 1, msp_debug, 514 "thread: no carrier scan\n"); 515 state->scan_in_progress = 0; 516 msp_update_volume(state); 517 continue; 518 } 519 520 /* mute audio */ 521 state->scan_in_progress = 1; 522 msp_update_volume(state); 523 524 msp3400c_set_mode(client, MSP_MODE_AM_DETECT); 525 val1 = val2 = 0; 526 max1 = max2 = -1; 527 state->watch_stereo = 0; 528 state->nicam_on = 0; 529 530 /* wait for tuner to settle down after a channel change */ 531 if (msp_sleep(state, 200)) 532 goto restart; 533 534 /* carrier detect pass #1 -- main carrier */ 535 cd = msp3400c_carrier_detect_main; 536 count = ARRAY_SIZE(msp3400c_carrier_detect_main); 537 538 if (msp_amsound && (state->v4l2_std & V4L2_STD_SECAM)) { 539 /* autodetect doesn't work well with AM ... */ 540 max1 = 3; 541 count = 0; 542 dev_dbg_lvl(&client->dev, 1, msp_debug, "AM sound override\n"); 543 } 544 545 for (i = 0; i < count; i++) { 546 msp3400c_set_carrier(client, cd[i].cdo, cd[i].cdo); 547 if (msp_sleep(state, 100)) 548 goto restart; 549 val = msp_read_dsp(client, 0x1b); 550 if (val > 32767) 551 val -= 65536; 552 if (val1 < val) { 553 val1 = val; 554 max1 = i; 555 } 556 dev_dbg_lvl(&client->dev, 1, msp_debug, 557 "carrier1 val: %5d / %s\n", val, cd[i].name); 558 } 559 560 /* carrier detect pass #2 -- second (stereo) carrier */ 561 switch (max1) { 562 case 1: /* 5.5 */ 563 cd = msp3400c_carrier_detect_55; 564 count = ARRAY_SIZE(msp3400c_carrier_detect_55); 565 break; 566 case 3: /* 6.5 */ 567 cd = msp3400c_carrier_detect_65; 568 count = ARRAY_SIZE(msp3400c_carrier_detect_65); 569 break; 570 case 0: /* 4.5 */ 571 case 2: /* 6.0 */ 572 default: 573 cd = NULL; 574 count = 0; 575 break; 576 } 577 578 if (msp_amsound && (state->v4l2_std & V4L2_STD_SECAM)) { 579 /* autodetect doesn't work well with AM ... */ 580 cd = NULL; 581 count = 0; 582 max2 = 0; 583 } 584 for (i = 0; i < count; i++) { 585 msp3400c_set_carrier(client, cd[i].cdo, cd[i].cdo); 586 if (msp_sleep(state, 100)) 587 goto restart; 588 val = msp_read_dsp(client, 0x1b); 589 if (val > 32767) 590 val -= 65536; 591 if (val2 < val) { 592 val2 = val; 593 max2 = i; 594 } 595 dev_dbg_lvl(&client->dev, 1, msp_debug, 596 "carrier2 val: %5d / %s\n", val, cd[i].name); 597 } 598 599 if (max1 < 0 || max1 > 3) 600 goto restart; 601 /* program the msp3400 according to the results */ 602 state->main = msp3400c_carrier_detect_main[max1].cdo; 603 switch (max1) { 604 case 1: /* 5.5 */ 605 state->detected_std = V4L2_STD_BG | V4L2_STD_PAL_H; 606 if (max2 == 0) { 607 /* B/G FM-stereo */ 608 state->second = msp3400c_carrier_detect_55[max2].cdo; 609 msp3400c_set_mode(client, MSP_MODE_FM_TERRA); 610 state->watch_stereo = 1; 611 } else if (max2 == 1 && state->has_nicam) { 612 /* B/G NICAM */ 613 state->second = msp3400c_carrier_detect_55[max2].cdo; 614 msp3400c_set_mode(client, MSP_MODE_FM_NICAM1); 615 state->nicam_on = 1; 616 state->watch_stereo = 1; 617 } else { 618 goto no_second; 619 } 620 break; 621 case 2: /* 6.0 */ 622 /* PAL I NICAM */ 623 state->detected_std = V4L2_STD_PAL_I; 624 state->second = MSP_CARRIER(6.552); 625 msp3400c_set_mode(client, MSP_MODE_FM_NICAM2); 626 state->nicam_on = 1; 627 state->watch_stereo = 1; 628 break; 629 case 3: /* 6.5 */ 630 if (max2 == 1 || max2 == 2) { 631 /* D/K FM-stereo */ 632 state->second = msp3400c_carrier_detect_65[max2].cdo; 633 msp3400c_set_mode(client, MSP_MODE_FM_TERRA); 634 state->watch_stereo = 1; 635 state->detected_std = V4L2_STD_DK; 636 } else if (max2 == 0 && (state->v4l2_std & V4L2_STD_SECAM)) { 637 /* L NICAM or AM-mono */ 638 state->second = msp3400c_carrier_detect_65[max2].cdo; 639 msp3400c_set_mode(client, MSP_MODE_AM_NICAM); 640 state->watch_stereo = 1; 641 state->detected_std = V4L2_STD_L; 642 } else if (max2 == 0 && state->has_nicam) { 643 /* D/K NICAM */ 644 state->second = msp3400c_carrier_detect_65[max2].cdo; 645 msp3400c_set_mode(client, MSP_MODE_FM_NICAM1); 646 state->nicam_on = 1; 647 state->watch_stereo = 1; 648 state->detected_std = V4L2_STD_DK; 649 } else { 650 goto no_second; 651 } 652 break; 653 case 0: /* 4.5 */ 654 state->detected_std = V4L2_STD_MN; 655 fallthrough; 656 default: 657 no_second: 658 state->second = msp3400c_carrier_detect_main[max1].cdo; 659 msp3400c_set_mode(client, MSP_MODE_FM_TERRA); 660 break; 661 } 662 msp3400c_set_carrier(client, state->second, state->main); 663 664 /* unmute */ 665 state->scan_in_progress = 0; 666 msp3400c_set_audmode(client); 667 msp_update_volume(state); 668 669 if (msp_debug) 670 msp3400c_print_mode(client); 671 672 /* monitor tv audio mode, the first time don't wait 673 so long to get a quick stereo/bilingual result */ 674 count = 3; 675 while (state->watch_stereo) { 676 if (msp_sleep(state, count ? 1000 : 5000)) 677 goto restart; 678 if (count) 679 count--; 680 watch_stereo(client); 681 } 682 } 683 dev_dbg_lvl(&client->dev, 1, msp_debug, "thread: exit\n"); 684 return 0; 685 } 686 687 688 int msp3410d_thread(void *data) 689 { 690 struct i2c_client *client = data; 691 struct msp_state *state = to_state(i2c_get_clientdata(client)); 692 int val, i, std, count; 693 694 dev_dbg_lvl(&client->dev, 1, msp_debug, "msp3410 daemon started\n"); 695 state->detected_std = V4L2_STD_ALL; 696 set_freezable(); 697 for (;;) { 698 dev_dbg_lvl(&client->dev, 2, msp_debug, "msp3410 thread: sleep\n"); 699 msp_sleep(state, -1); 700 dev_dbg_lvl(&client->dev, 2, msp_debug, "msp3410 thread: wakeup\n"); 701 702 restart: 703 dev_dbg_lvl(&client->dev, 2, msp_debug, "thread: restart scan\n"); 704 state->restart = 0; 705 if (kthread_should_stop()) 706 break; 707 708 if (state->mode == MSP_MODE_EXTERN) { 709 /* no carrier scan needed, just unmute */ 710 dev_dbg_lvl(&client->dev, 1, msp_debug, 711 "thread: no carrier scan\n"); 712 state->scan_in_progress = 0; 713 msp_update_volume(state); 714 continue; 715 } 716 717 /* mute audio */ 718 state->scan_in_progress = 1; 719 msp_update_volume(state); 720 721 /* start autodetect. Note: autodetect is not supported for 722 NTSC-M and radio, hence we force the standard in those 723 cases. */ 724 if (state->radio) 725 std = 0x40; 726 else 727 std = (state->v4l2_std & V4L2_STD_NTSC) ? 0x20 : 1; 728 state->watch_stereo = 0; 729 state->nicam_on = 0; 730 731 /* wait for tuner to settle down after a channel change */ 732 if (msp_sleep(state, 200)) 733 goto restart; 734 735 if (msp_debug) 736 dev_dbg_lvl(&client->dev, 2, msp_debug, 737 "setting standard: %s (0x%04x)\n", 738 msp_standard_std_name(std), std); 739 740 if (std != 1) { 741 /* programmed some specific mode */ 742 val = std; 743 } else { 744 /* triggered autodetect */ 745 msp_write_dem(client, 0x20, std); 746 for (;;) { 747 if (msp_sleep(state, 100)) 748 goto restart; 749 750 /* check results */ 751 val = msp_read_dem(client, 0x7e); 752 if (val < 0x07ff) 753 break; 754 dev_dbg_lvl(&client->dev, 2, msp_debug, 755 "detection still in progress\n"); 756 } 757 } 758 for (i = 0; msp_stdlist[i].name != NULL; i++) 759 if (msp_stdlist[i].retval == val) 760 break; 761 dev_dbg_lvl(&client->dev, 1, msp_debug, "current standard: %s (0x%04x)\n", 762 msp_standard_std_name(val), val); 763 state->main = msp_stdlist[i].main; 764 state->second = msp_stdlist[i].second; 765 state->std = val; 766 state->rxsubchans = V4L2_TUNER_SUB_MONO; 767 768 if (msp_amsound && !state->radio && 769 (state->v4l2_std & V4L2_STD_SECAM) && (val != 0x0009)) { 770 /* autodetection has failed, let backup */ 771 dev_dbg_lvl(&client->dev, 1, msp_debug, "autodetection failed, switching to backup standard: %s (0x%04x)\n", 772 msp_stdlist[8].name ? 773 msp_stdlist[8].name : "unknown", val); 774 state->std = val = 0x0009; 775 msp_write_dem(client, 0x20, val); 776 } else { 777 state->detected_std = msp_standard_std(state->std); 778 } 779 780 /* set stereo */ 781 switch (val) { 782 case 0x0008: /* B/G NICAM */ 783 case 0x000a: /* I NICAM */ 784 case 0x000b: /* D/K NICAM */ 785 if (val == 0x000a) 786 state->mode = MSP_MODE_FM_NICAM2; 787 else 788 state->mode = MSP_MODE_FM_NICAM1; 789 /* just turn on stereo */ 790 state->nicam_on = 1; 791 state->watch_stereo = 1; 792 break; 793 case 0x0009: 794 state->mode = MSP_MODE_AM_NICAM; 795 state->nicam_on = 1; 796 state->watch_stereo = 1; 797 break; 798 case 0x0020: /* BTSC */ 799 /* The pre-'G' models only have BTSC-mono */ 800 state->mode = MSP_MODE_BTSC; 801 break; 802 case 0x0040: /* FM radio */ 803 state->mode = MSP_MODE_FM_RADIO; 804 state->rxsubchans = V4L2_TUNER_SUB_STEREO; 805 /* not needed in theory if we have radio, but 806 short programming enables carrier mute */ 807 msp3400c_set_mode(client, MSP_MODE_FM_RADIO); 808 msp3400c_set_carrier(client, MSP_CARRIER(10.7), 809 MSP_CARRIER(10.7)); 810 break; 811 case 0x0002: 812 case 0x0003: 813 case 0x0004: 814 case 0x0005: 815 state->mode = MSP_MODE_FM_TERRA; 816 state->watch_stereo = 1; 817 break; 818 } 819 820 /* set various prescales */ 821 msp_write_dsp(client, 0x0d, 0x1900); /* scart */ 822 msp_write_dsp(client, 0x0e, 0x3000); /* FM */ 823 if (state->has_nicam) 824 msp_write_dsp(client, 0x10, 0x5a00); /* nicam */ 825 826 if (state->has_i2s_conf) 827 msp_write_dem(client, 0x40, state->i2s_mode); 828 829 /* unmute */ 830 msp3400c_set_audmode(client); 831 state->scan_in_progress = 0; 832 msp_update_volume(state); 833 834 /* monitor tv audio mode, the first time don't wait 835 so long to get a quick stereo/bilingual result */ 836 count = 3; 837 while (state->watch_stereo) { 838 if (msp_sleep(state, count ? 1000 : 5000)) 839 goto restart; 840 if (count) 841 count--; 842 watch_stereo(client); 843 } 844 } 845 dev_dbg_lvl(&client->dev, 1, msp_debug, "thread: exit\n"); 846 return 0; 847 } 848 849 /* ----------------------------------------------------------------------- */ 850 851 /* msp34xxG + (autoselect no-thread) 852 * this one uses both automatic standard detection and automatic sound 853 * select which are available in the newer G versions 854 * struct msp: only norm, acb and source are really used in this mode 855 */ 856 857 static int msp34xxg_modus(struct i2c_client *client) 858 { 859 struct msp_state *state = to_state(i2c_get_clientdata(client)); 860 861 if (state->radio) { 862 dev_dbg_lvl(&client->dev, 1, msp_debug, "selected radio modus\n"); 863 return 0x0001; 864 } 865 if (state->v4l2_std == V4L2_STD_NTSC_M_JP) { 866 dev_dbg_lvl(&client->dev, 1, msp_debug, "selected M (EIA-J) modus\n"); 867 return 0x4001; 868 } 869 if (state->v4l2_std == V4L2_STD_NTSC_M_KR) { 870 dev_dbg_lvl(&client->dev, 1, msp_debug, "selected M (A2) modus\n"); 871 return 0x0001; 872 } 873 if (state->v4l2_std == V4L2_STD_SECAM_L) { 874 dev_dbg_lvl(&client->dev, 1, msp_debug, "selected SECAM-L modus\n"); 875 return 0x6001; 876 } 877 if (state->v4l2_std & V4L2_STD_MN) { 878 dev_dbg_lvl(&client->dev, 1, msp_debug, "selected M (BTSC) modus\n"); 879 return 0x2001; 880 } 881 return 0x7001; 882 } 883 884 static void msp34xxg_set_source(struct i2c_client *client, u16 reg, int in) 885 { 886 struct msp_state *state = to_state(i2c_get_clientdata(client)); 887 int source, matrix; 888 889 switch (state->audmode) { 890 case V4L2_TUNER_MODE_MONO: 891 source = 0; /* mono only */ 892 matrix = 0x30; 893 break; 894 case V4L2_TUNER_MODE_LANG2: 895 source = 4; /* stereo or B */ 896 matrix = 0x10; 897 break; 898 case V4L2_TUNER_MODE_LANG1_LANG2: 899 source = 1; /* stereo or A|B */ 900 matrix = 0x20; 901 break; 902 case V4L2_TUNER_MODE_LANG1: 903 source = 3; /* stereo or A */ 904 matrix = 0x00; 905 break; 906 case V4L2_TUNER_MODE_STEREO: 907 default: 908 source = 3; /* stereo or A */ 909 matrix = 0x20; 910 break; 911 } 912 913 if (in == MSP_DSP_IN_TUNER) 914 source = (source << 8) | 0x20; 915 /* the msp34x2g puts the MAIN_AVC, MAIN and AUX sources in 12, 13, 14 916 instead of 11, 12, 13. So we add one for that msp version. */ 917 else if (in >= MSP_DSP_IN_MAIN_AVC && state->has_dolby_pro_logic) 918 source = ((in + 1) << 8) | matrix; 919 else 920 source = (in << 8) | matrix; 921 922 dev_dbg_lvl(&client->dev, 1, msp_debug, 923 "set source to %d (0x%x) for output %02x\n", in, source, reg); 924 msp_write_dsp(client, reg, source); 925 } 926 927 static void msp34xxg_set_sources(struct i2c_client *client) 928 { 929 struct msp_state *state = to_state(i2c_get_clientdata(client)); 930 u32 in = state->route_in; 931 932 msp34xxg_set_source(client, 0x0008, (in >> 4) & 0xf); 933 /* quasi-peak detector is set to same input as the loudspeaker (MAIN) */ 934 msp34xxg_set_source(client, 0x000c, (in >> 4) & 0xf); 935 msp34xxg_set_source(client, 0x0009, (in >> 8) & 0xf); 936 msp34xxg_set_source(client, 0x000a, (in >> 12) & 0xf); 937 if (state->has_scart2_out) 938 msp34xxg_set_source(client, 0x0041, (in >> 16) & 0xf); 939 msp34xxg_set_source(client, 0x000b, (in >> 20) & 0xf); 940 } 941 942 /* (re-)initialize the msp34xxg */ 943 static void msp34xxg_reset(struct i2c_client *client) 944 { 945 struct msp_state *state = to_state(i2c_get_clientdata(client)); 946 int tuner = (state->route_in >> 3) & 1; 947 int modus; 948 949 /* initialize std to 1 (autodetect) to signal that no standard is 950 selected yet. */ 951 state->std = 1; 952 953 msp_reset(client); 954 955 if (state->has_i2s_conf) 956 msp_write_dem(client, 0x40, state->i2s_mode); 957 958 /* step-by-step initialisation, as described in the manual */ 959 modus = msp34xxg_modus(client); 960 modus |= tuner ? 0x100 : 0; 961 msp_write_dem(client, 0x30, modus); 962 963 /* write the dsps that may have an influence on 964 standard/audio autodetection right now */ 965 msp34xxg_set_sources(client); 966 967 msp_write_dsp(client, 0x0d, 0x1900); /* scart */ 968 msp_write_dsp(client, 0x0e, 0x3000); /* FM */ 969 if (state->has_nicam) 970 msp_write_dsp(client, 0x10, 0x5a00); /* nicam */ 971 972 /* set identification threshold. Personally, I 973 * I set it to a higher value than the default 974 * of 0x190 to ignore noisy stereo signals. 975 * this needs tuning. (recommended range 0x00a0-0x03c0) 976 * 0x7f0 = forced mono mode 977 * 978 * a2 threshold for stereo/bilingual. 979 * Note: this register is part of the Manual/Compatibility mode. 980 * It is supported by all 'G'-family chips. 981 */ 982 msp_write_dem(client, 0x22, msp_stereo_thresh); 983 } 984 985 int msp34xxg_thread(void *data) 986 { 987 struct i2c_client *client = data; 988 struct msp_state *state = to_state(i2c_get_clientdata(client)); 989 int val, i; 990 991 dev_dbg_lvl(&client->dev, 1, msp_debug, "msp34xxg daemon started\n"); 992 state->detected_std = V4L2_STD_ALL; 993 set_freezable(); 994 for (;;) { 995 dev_dbg_lvl(&client->dev, 2, msp_debug, "msp34xxg thread: sleep\n"); 996 msp_sleep(state, -1); 997 dev_dbg_lvl(&client->dev, 2, msp_debug, "msp34xxg thread: wakeup\n"); 998 999 restart: 1000 dev_dbg_lvl(&client->dev, 1, msp_debug, "thread: restart scan\n"); 1001 state->restart = 0; 1002 if (kthread_should_stop()) 1003 break; 1004 1005 if (state->mode == MSP_MODE_EXTERN) { 1006 /* no carrier scan needed, just unmute */ 1007 dev_dbg_lvl(&client->dev, 1, msp_debug, 1008 "thread: no carrier scan\n"); 1009 state->scan_in_progress = 0; 1010 msp_update_volume(state); 1011 continue; 1012 } 1013 1014 /* setup the chip*/ 1015 msp34xxg_reset(client); 1016 state->std = state->radio ? 0x40 : 1017 (state->force_btsc && msp_standard == 1) ? 32 : msp_standard; 1018 msp_write_dem(client, 0x20, state->std); 1019 /* start autodetect */ 1020 if (state->std != 1) 1021 goto unmute; 1022 1023 /* watch autodetect */ 1024 dev_dbg_lvl(&client->dev, 1, msp_debug, 1025 "started autodetect, waiting for result\n"); 1026 for (i = 0; i < 10; i++) { 1027 if (msp_sleep(state, 100)) 1028 goto restart; 1029 1030 /* check results */ 1031 val = msp_read_dem(client, 0x7e); 1032 if (val < 0x07ff) { 1033 state->std = val; 1034 break; 1035 } 1036 dev_dbg_lvl(&client->dev, 2, msp_debug, 1037 "detection still in progress\n"); 1038 } 1039 if (state->std == 1) { 1040 dev_dbg_lvl(&client->dev, 1, msp_debug, 1041 "detection still in progress after 10 tries. giving up.\n"); 1042 continue; 1043 } 1044 1045 unmute: 1046 dev_dbg_lvl(&client->dev, 1, msp_debug, 1047 "detected standard: %s (0x%04x)\n", 1048 msp_standard_std_name(state->std), state->std); 1049 state->detected_std = msp_standard_std(state->std); 1050 1051 if (state->std == 9) { 1052 /* AM NICAM mode */ 1053 msp_write_dsp(client, 0x0e, 0x7c00); 1054 } 1055 1056 /* unmute: dispatch sound to scart output, set scart volume */ 1057 msp_update_volume(state); 1058 1059 /* restore ACB */ 1060 if (msp_write_dsp(client, 0x13, state->acb)) 1061 return -1; 1062 1063 /* the periodic stereo/SAP check is only relevant for 1064 the 0x20 standard (BTSC) */ 1065 if (state->std != 0x20) 1066 continue; 1067 1068 state->watch_stereo = 1; 1069 1070 /* monitor tv audio mode, the first time don't wait 1071 in order to get a quick stereo/SAP update */ 1072 watch_stereo(client); 1073 while (state->watch_stereo) { 1074 watch_stereo(client); 1075 if (msp_sleep(state, 5000)) 1076 goto restart; 1077 } 1078 } 1079 dev_dbg_lvl(&client->dev, 1, msp_debug, "thread: exit\n"); 1080 return 0; 1081 } 1082 1083 static int msp34xxg_detect_stereo(struct i2c_client *client) 1084 { 1085 struct msp_state *state = to_state(i2c_get_clientdata(client)); 1086 int status = msp_read_dem(client, 0x0200); 1087 int is_bilingual = status & 0x100; 1088 int is_stereo = status & 0x40; 1089 int oldrx = state->rxsubchans; 1090 1091 if (state->mode == MSP_MODE_EXTERN) 1092 return 0; 1093 1094 state->rxsubchans = 0; 1095 if (is_stereo) 1096 state->rxsubchans = V4L2_TUNER_SUB_STEREO; 1097 else 1098 state->rxsubchans = V4L2_TUNER_SUB_MONO; 1099 if (is_bilingual) { 1100 if (state->std == 0x20) 1101 state->rxsubchans |= V4L2_TUNER_SUB_SAP; 1102 else 1103 state->rxsubchans = 1104 V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2; 1105 } 1106 dev_dbg_lvl(&client->dev, 1, msp_debug, 1107 "status=0x%x, stereo=%d, bilingual=%d -> rxsubchans=%d\n", 1108 status, is_stereo, is_bilingual, state->rxsubchans); 1109 return (oldrx != state->rxsubchans); 1110 } 1111 1112 static void msp34xxg_set_audmode(struct i2c_client *client) 1113 { 1114 struct msp_state *state = to_state(i2c_get_clientdata(client)); 1115 1116 if (state->std == 0x20) { 1117 if ((state->rxsubchans & V4L2_TUNER_SUB_SAP) && 1118 (state->audmode == V4L2_TUNER_MODE_LANG1_LANG2 || 1119 state->audmode == V4L2_TUNER_MODE_LANG2)) { 1120 msp_write_dem(client, 0x20, 0x21); 1121 } else { 1122 msp_write_dem(client, 0x20, 0x20); 1123 } 1124 } 1125 1126 msp34xxg_set_sources(client); 1127 } 1128 1129 void msp_set_audmode(struct i2c_client *client) 1130 { 1131 struct msp_state *state = to_state(i2c_get_clientdata(client)); 1132 1133 switch (state->opmode) { 1134 case OPMODE_MANUAL: 1135 case OPMODE_AUTODETECT: 1136 msp3400c_set_audmode(client); 1137 break; 1138 case OPMODE_AUTOSELECT: 1139 msp34xxg_set_audmode(client); 1140 break; 1141 } 1142 } 1143 1144 int msp_detect_stereo(struct i2c_client *client) 1145 { 1146 struct msp_state *state = to_state(i2c_get_clientdata(client)); 1147 1148 switch (state->opmode) { 1149 case OPMODE_MANUAL: 1150 case OPMODE_AUTODETECT: 1151 return msp3400c_detect_stereo(client); 1152 case OPMODE_AUTOSELECT: 1153 return msp34xxg_detect_stereo(client); 1154 } 1155 return 0; 1156 } 1157 1158