1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * 4 * device driver for philips saa7134 based TV cards 5 * tv audio decoder (fm stereo, nicam, ...) 6 * 7 * (c) 2001-03 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs] 8 */ 9 10 #include "saa7134.h" 11 #include "saa7134-reg.h" 12 13 #include <linux/init.h> 14 #include <linux/list.h> 15 #include <linux/module.h> 16 #include <linux/kernel.h> 17 #include <linux/kthread.h> 18 #include <linux/delay.h> 19 #include <linux/freezer.h> 20 #include <asm/div64.h> 21 22 /* ------------------------------------------------------------------ */ 23 24 static unsigned int audio_debug; 25 module_param(audio_debug, int, 0644); 26 MODULE_PARM_DESC(audio_debug,"enable debug messages [tv audio]"); 27 28 static unsigned int audio_ddep; 29 module_param(audio_ddep, int, 0644); 30 MODULE_PARM_DESC(audio_ddep,"audio ddep overwrite"); 31 32 static int audio_clock_override = UNSET; 33 module_param(audio_clock_override, int, 0644); 34 35 static int audio_clock_tweak; 36 module_param(audio_clock_tweak, int, 0644); 37 MODULE_PARM_DESC(audio_clock_tweak, "Audio clock tick fine tuning for cards with audio crystal that's slightly off (range [-1024 .. 1024])"); 38 39 #define audio_dbg(level, fmt, arg...) do { \ 40 if (audio_debug >= level) \ 41 printk(KERN_DEBUG pr_fmt("audio: " fmt), ## arg); \ 42 } while (0) 43 44 /* msecs */ 45 #define SCAN_INITIAL_DELAY 1000 46 #define SCAN_SAMPLE_DELAY 200 47 #define SCAN_SUBCARRIER_DELAY 2000 48 49 /* ------------------------------------------------------------------ */ 50 /* saa7134 code */ 51 52 static struct mainscan { 53 char *name; 54 v4l2_std_id std; 55 int carr; 56 } mainscan[] = { 57 { 58 .name = "MN", 59 .std = V4L2_STD_MN, 60 .carr = 4500, 61 },{ 62 .name = "BGH", 63 .std = V4L2_STD_B | V4L2_STD_GH, 64 .carr = 5500, 65 },{ 66 .name = "I", 67 .std = V4L2_STD_PAL_I, 68 .carr = 6000, 69 },{ 70 .name = "DKL", 71 .std = V4L2_STD_DK | V4L2_STD_SECAM_L | V4L2_STD_SECAM_LC, 72 .carr = 6500, 73 } 74 }; 75 76 static struct saa7134_tvaudio tvaudio[] = { 77 { 78 .name = "PAL-B/G FM-stereo", 79 .std = V4L2_STD_PAL_BG, 80 .mode = TVAUDIO_FM_BG_STEREO, 81 .carr1 = 5500, 82 .carr2 = 5742, 83 },{ 84 .name = "PAL-D/K1 FM-stereo", 85 .std = V4L2_STD_PAL_DK, 86 .carr1 = 6500, 87 .carr2 = 6258, 88 .mode = TVAUDIO_FM_BG_STEREO, 89 },{ 90 .name = "PAL-D/K2 FM-stereo", 91 .std = V4L2_STD_PAL_DK, 92 .carr1 = 6500, 93 .carr2 = 6742, 94 .mode = TVAUDIO_FM_BG_STEREO, 95 },{ 96 .name = "PAL-D/K3 FM-stereo", 97 .std = V4L2_STD_PAL_DK, 98 .carr1 = 6500, 99 .carr2 = 5742, 100 .mode = TVAUDIO_FM_BG_STEREO, 101 },{ 102 .name = "PAL-B/G NICAM", 103 .std = V4L2_STD_PAL_BG, 104 .carr1 = 5500, 105 .carr2 = 5850, 106 .mode = TVAUDIO_NICAM_FM, 107 },{ 108 .name = "PAL-I NICAM", 109 .std = V4L2_STD_PAL_I, 110 .carr1 = 6000, 111 .carr2 = 6552, 112 .mode = TVAUDIO_NICAM_FM, 113 },{ 114 .name = "PAL-D/K NICAM", 115 .std = V4L2_STD_PAL_DK, 116 .carr1 = 6500, 117 .carr2 = 5850, 118 .mode = TVAUDIO_NICAM_FM, 119 },{ 120 .name = "SECAM-L NICAM", 121 .std = V4L2_STD_SECAM_L, 122 .carr1 = 6500, 123 .carr2 = 5850, 124 .mode = TVAUDIO_NICAM_AM, 125 },{ 126 .name = "SECAM-D/K NICAM", 127 .std = V4L2_STD_SECAM_DK, 128 .carr1 = 6500, 129 .carr2 = 5850, 130 .mode = TVAUDIO_NICAM_FM, 131 },{ 132 .name = "NTSC-A2 FM-stereo", 133 .std = V4L2_STD_NTSC, 134 .carr1 = 4500, 135 .carr2 = 4724, 136 .mode = TVAUDIO_FM_K_STEREO, 137 },{ 138 .name = "NTSC-M", 139 .std = V4L2_STD_NTSC, 140 .carr1 = 4500, 141 .carr2 = -1, 142 .mode = TVAUDIO_FM_MONO, 143 } 144 }; 145 #define TVAUDIO ARRAY_SIZE(tvaudio) 146 147 /* ------------------------------------------------------------------ */ 148 149 static u32 tvaudio_carr2reg(u32 carrier) 150 { 151 u64 a = carrier; 152 153 a <<= 24; 154 do_div(a,12288); 155 return a; 156 } 157 158 static void tvaudio_setcarrier(struct saa7134_dev *dev, 159 int primary, int secondary) 160 { 161 if (-1 == secondary) 162 secondary = primary; 163 saa_writel(SAA7134_CARRIER1_FREQ0 >> 2, tvaudio_carr2reg(primary)); 164 saa_writel(SAA7134_CARRIER2_FREQ0 >> 2, tvaudio_carr2reg(secondary)); 165 } 166 167 #define SAA7134_MUTE_MASK 0xbb 168 #define SAA7134_MUTE_ANALOG 0x04 169 #define SAA7134_MUTE_I2S 0x40 170 171 static void mute_input_7134(struct saa7134_dev *dev) 172 { 173 unsigned int mute; 174 struct saa7134_input *in; 175 int ausel=0, ics=0, ocs=0; 176 int mask; 177 178 /* look what is to do ... */ 179 in = dev->input; 180 mute = (dev->ctl_mute || 181 (dev->automute && (&card(dev).radio) != in)); 182 if (card(dev).mute.type) { 183 /* 184 * 7130 - we'll mute using some unconnected audio input 185 * 7134 - we'll probably should switch external mux with gpio 186 */ 187 if (mute) 188 in = &card(dev).mute; 189 } 190 191 if (dev->hw_mute == mute && 192 dev->hw_input == in && !dev->insuspend) { 193 audio_dbg(1, "mute/input: nothing to do [mute=%d,input=%s]\n", 194 mute, saa7134_input_name[in->type]); 195 return; 196 } 197 198 audio_dbg(1, "ctl_mute=%d automute=%d input=%s => mute=%d input=%s\n", 199 dev->ctl_mute, dev->automute, 200 saa7134_input_name[dev->input->type], mute, 201 saa7134_input_name[in->type]); 202 dev->hw_mute = mute; 203 dev->hw_input = in; 204 205 if (PCI_DEVICE_ID_PHILIPS_SAA7134 == dev->pci->device) 206 /* 7134 mute */ 207 saa_writeb(SAA7134_AUDIO_MUTE_CTRL, mute ? 208 SAA7134_MUTE_MASK | 209 SAA7134_MUTE_ANALOG | 210 SAA7134_MUTE_I2S : 211 SAA7134_MUTE_MASK); 212 213 /* switch internal audio mux */ 214 switch (in->amux) { 215 case TV: ausel=0xc0; ics=0x00; ocs=0x02; break; 216 case LINE1: ausel=0x80; ics=0x00; ocs=0x00; break; 217 case LINE2: ausel=0x80; ics=0x08; ocs=0x01; break; 218 case LINE2_LEFT: ausel=0x80; ics=0x08; ocs=0x05; break; 219 } 220 saa_andorb(SAA7134_AUDIO_FORMAT_CTRL, 0xc0, ausel); 221 saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x08, ics); 222 saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x07, ocs); 223 // for oss, we need to change the clock configuration 224 if (in->amux == TV) 225 saa_andorb(SAA7134_SIF_SAMPLE_FREQ, 0x03, 0x00); 226 else 227 saa_andorb(SAA7134_SIF_SAMPLE_FREQ, 0x03, 0x01); 228 229 /* switch gpio-connected external audio mux */ 230 if (0 == card(dev).gpiomask) 231 return; 232 233 mask = card(dev).gpiomask; 234 saa_andorl(SAA7134_GPIO_GPMODE0 >> 2, mask, mask); 235 saa_andorl(SAA7134_GPIO_GPSTATUS0 >> 2, mask, in->gpio); 236 saa7134_track_gpio(dev, saa7134_input_name[in->type]); 237 } 238 239 static void tvaudio_setmode(struct saa7134_dev *dev, 240 struct saa7134_tvaudio *audio, 241 char *note) 242 { 243 int acpf, tweak = 0; 244 245 if (dev->tvnorm->id == V4L2_STD_NTSC) { 246 acpf = 0x19066; 247 } else { 248 acpf = 0x1e000; 249 } 250 if (audio_clock_tweak > -1024 && audio_clock_tweak < 1024) 251 tweak = audio_clock_tweak; 252 253 if (note) 254 audio_dbg(1, "tvaudio_setmode: %s %s [%d.%03d/%d.%03d MHz] acpf=%d%+d\n", 255 note, audio->name, 256 audio->carr1 / 1000, audio->carr1 % 1000, 257 audio->carr2 / 1000, audio->carr2 % 1000, 258 acpf, tweak); 259 260 acpf += tweak; 261 saa_writeb(SAA7134_AUDIO_CLOCKS_PER_FIELD0, (acpf & 0x0000ff) >> 0); 262 saa_writeb(SAA7134_AUDIO_CLOCKS_PER_FIELD1, (acpf & 0x00ff00) >> 8); 263 saa_writeb(SAA7134_AUDIO_CLOCKS_PER_FIELD2, (acpf & 0x030000) >> 16); 264 tvaudio_setcarrier(dev,audio->carr1,audio->carr2); 265 266 switch (audio->mode) { 267 case TVAUDIO_FM_MONO: 268 case TVAUDIO_FM_BG_STEREO: 269 saa_writeb(SAA7134_DEMODULATOR, 0x00); 270 saa_writeb(SAA7134_DCXO_IDENT_CTRL, 0x00); 271 saa_writeb(SAA7134_FM_DEEMPHASIS, 0x22); 272 saa_writeb(SAA7134_FM_DEMATRIX, 0x80); 273 saa_writeb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0xa0); 274 break; 275 case TVAUDIO_FM_K_STEREO: 276 saa_writeb(SAA7134_DEMODULATOR, 0x00); 277 saa_writeb(SAA7134_DCXO_IDENT_CTRL, 0x01); 278 saa_writeb(SAA7134_FM_DEEMPHASIS, 0x22); 279 saa_writeb(SAA7134_FM_DEMATRIX, 0x80); 280 saa_writeb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0xa0); 281 break; 282 case TVAUDIO_NICAM_FM: 283 saa_writeb(SAA7134_DEMODULATOR, 0x10); 284 saa_writeb(SAA7134_DCXO_IDENT_CTRL, 0x00); 285 saa_writeb(SAA7134_FM_DEEMPHASIS, 0x44); 286 saa_writeb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0xa1); 287 saa_writeb(SAA7134_NICAM_CONFIG, 0x00); 288 break; 289 case TVAUDIO_NICAM_AM: 290 saa_writeb(SAA7134_DEMODULATOR, 0x12); 291 saa_writeb(SAA7134_DCXO_IDENT_CTRL, 0x00); 292 saa_writeb(SAA7134_FM_DEEMPHASIS, 0x44); 293 saa_writeb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0xa1); 294 saa_writeb(SAA7134_NICAM_CONFIG, 0x00); 295 break; 296 case TVAUDIO_FM_SAT_STEREO: 297 /* not implemented (yet) */ 298 break; 299 } 300 } 301 302 static int tvaudio_sleep(struct saa7134_dev *dev, int timeout) 303 { 304 if (dev->thread.scan1 == dev->thread.scan2 && 305 !kthread_should_stop()) { 306 if (timeout < 0) { 307 set_current_state(TASK_INTERRUPTIBLE); 308 schedule(); 309 } else { 310 schedule_timeout_interruptible 311 (msecs_to_jiffies(timeout)); 312 } 313 } 314 return dev->thread.scan1 != dev->thread.scan2; 315 } 316 317 static int tvaudio_checkcarrier(struct saa7134_dev *dev, struct mainscan *scan) 318 { 319 __s32 left,right,value; 320 321 if (!(dev->tvnorm->id & scan->std)) { 322 value = 0; 323 audio_dbg(1, "skipping %d.%03d MHz [%4s]\n", 324 scan->carr / 1000, scan->carr % 1000, scan->name); 325 return 0; 326 } 327 328 if (audio_debug > 1) { 329 int i; 330 audio_dbg(1, "debug %d:", scan->carr); 331 for (i = -150; i <= 150; i += 30) { 332 tvaudio_setcarrier(dev,scan->carr+i,scan->carr+i); 333 saa_readl(SAA7134_LEVEL_READOUT1 >> 2); 334 if (tvaudio_sleep(dev,SCAN_SAMPLE_DELAY)) 335 return -1; 336 value = saa_readl(SAA7134_LEVEL_READOUT1 >> 2); 337 if (0 == i) 338 pr_cont(" # %6d # ", value >> 16); 339 else 340 pr_cont(" %6d", value >> 16); 341 } 342 pr_cont("\n"); 343 } 344 345 tvaudio_setcarrier(dev,scan->carr-90,scan->carr-90); 346 saa_readl(SAA7134_LEVEL_READOUT1 >> 2); 347 if (tvaudio_sleep(dev,SCAN_SAMPLE_DELAY)) 348 return -1; 349 left = saa_readl(SAA7134_LEVEL_READOUT1 >> 2); 350 351 tvaudio_setcarrier(dev,scan->carr+90,scan->carr+90); 352 saa_readl(SAA7134_LEVEL_READOUT1 >> 2); 353 if (tvaudio_sleep(dev,SCAN_SAMPLE_DELAY)) 354 return -1; 355 right = saa_readl(SAA7134_LEVEL_READOUT1 >> 2); 356 357 left >>= 16; 358 right >>= 16; 359 value = left > right ? left - right : right - left; 360 audio_dbg(1, "scanning %d.%03d MHz [%4s] => dc is %5d [%d/%d]\n", 361 scan->carr / 1000, scan->carr % 1000, 362 scan->name, value, left, right); 363 return value; 364 } 365 366 367 static int tvaudio_getstereo(struct saa7134_dev *dev, struct saa7134_tvaudio *audio) 368 { 369 __u32 idp, nicam, nicam_status; 370 int retval = -1; 371 372 switch (audio->mode) { 373 case TVAUDIO_FM_MONO: 374 return V4L2_TUNER_SUB_MONO; 375 case TVAUDIO_FM_K_STEREO: 376 case TVAUDIO_FM_BG_STEREO: 377 idp = (saa_readb(SAA7134_IDENT_SIF) & 0xe0) >> 5; 378 audio_dbg(1, "getstereo: fm/stereo: idp=0x%x\n", idp); 379 if (0x03 == (idp & 0x03)) 380 retval = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2; 381 else if (0x05 == (idp & 0x05)) 382 retval = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO; 383 else if (0x01 == (idp & 0x01)) 384 retval = V4L2_TUNER_SUB_MONO; 385 break; 386 case TVAUDIO_FM_SAT_STEREO: 387 /* not implemented (yet) */ 388 break; 389 case TVAUDIO_NICAM_FM: 390 case TVAUDIO_NICAM_AM: 391 nicam = saa_readb(SAA7134_AUDIO_STATUS); 392 audio_dbg(1, "getstereo: nicam=0x%x\n", nicam); 393 if (nicam & 0x1) { 394 nicam_status = saa_readb(SAA7134_NICAM_STATUS); 395 audio_dbg(1, "getstereo: nicam_status=0x%x\n", 396 nicam_status); 397 398 switch (nicam_status & 0x03) { 399 case 0x01: 400 retval = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2; 401 break; 402 case 0x02: 403 retval = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO; 404 break; 405 default: 406 retval = V4L2_TUNER_SUB_MONO; 407 } 408 } else { 409 /* No nicam detected */ 410 } 411 break; 412 } 413 if (retval != -1) 414 audio_dbg(1, "found audio subchannels:%s%s%s%s\n", 415 (retval & V4L2_TUNER_SUB_MONO) ? " mono" : "", 416 (retval & V4L2_TUNER_SUB_STEREO) ? " stereo" : "", 417 (retval & V4L2_TUNER_SUB_LANG1) ? " lang1" : "", 418 (retval & V4L2_TUNER_SUB_LANG2) ? " lang2" : ""); 419 return retval; 420 } 421 422 static int tvaudio_setstereo(struct saa7134_dev *dev, struct saa7134_tvaudio *audio, 423 u32 mode) 424 { 425 static char *name[] = { 426 [ V4L2_TUNER_MODE_MONO ] = "mono", 427 [ V4L2_TUNER_MODE_STEREO ] = "stereo", 428 [ V4L2_TUNER_MODE_LANG1 ] = "lang1", 429 [ V4L2_TUNER_MODE_LANG2 ] = "lang2", 430 [ V4L2_TUNER_MODE_LANG1_LANG2 ] = "lang1+lang2", 431 }; 432 static u32 fm[] = { 433 [ V4L2_TUNER_MODE_MONO ] = 0x00, /* ch1 */ 434 [ V4L2_TUNER_MODE_STEREO ] = 0x80, /* auto */ 435 [ V4L2_TUNER_MODE_LANG1 ] = 0x00, /* ch1 */ 436 [ V4L2_TUNER_MODE_LANG2 ] = 0x01, /* ch2 */ 437 [ V4L2_TUNER_MODE_LANG1_LANG2 ] = 0x80, /* auto */ 438 }; 439 u32 reg; 440 441 switch (audio->mode) { 442 case TVAUDIO_FM_MONO: 443 /* nothing to do ... */ 444 break; 445 case TVAUDIO_FM_K_STEREO: 446 case TVAUDIO_FM_BG_STEREO: 447 case TVAUDIO_NICAM_AM: 448 case TVAUDIO_NICAM_FM: 449 audio_dbg(1, "setstereo [fm] => %s\n", 450 name[mode % ARRAY_SIZE(name)]); 451 reg = fm[ mode % ARRAY_SIZE(fm) ]; 452 saa_writeb(SAA7134_FM_DEMATRIX, reg); 453 break; 454 case TVAUDIO_FM_SAT_STEREO: 455 /* Not implemented */ 456 break; 457 } 458 return 0; 459 } 460 461 static int tvaudio_thread(void *data) 462 { 463 struct saa7134_dev *dev = data; 464 int carr_vals[ARRAY_SIZE(mainscan)]; 465 unsigned int i, audio, nscan; 466 int max1,max2,carrier,rx,mode,lastmode,default_carrier; 467 468 set_freezable(); 469 470 for (;;) { 471 tvaudio_sleep(dev,-1); 472 if (kthread_should_stop()) 473 goto done; 474 475 restart: 476 try_to_freeze(); 477 478 dev->thread.scan1 = dev->thread.scan2; 479 audio_dbg(1, "tvaudio thread scan start [%d]\n", 480 dev->thread.scan1); 481 dev->tvaudio = NULL; 482 483 saa_writeb(SAA7134_MONITOR_SELECT, 0xa0); 484 saa_writeb(SAA7134_FM_DEMATRIX, 0x80); 485 486 if (dev->ctl_automute) 487 dev->automute = 1; 488 489 mute_input_7134(dev); 490 491 /* give the tuner some time */ 492 if (tvaudio_sleep(dev,SCAN_INITIAL_DELAY)) 493 goto restart; 494 495 max1 = 0; 496 max2 = 0; 497 nscan = 0; 498 carrier = 0; 499 default_carrier = 0; 500 for (i = 0; i < ARRAY_SIZE(mainscan); i++) { 501 if (!(dev->tvnorm->id & mainscan[i].std)) 502 continue; 503 if (!default_carrier) 504 default_carrier = mainscan[i].carr; 505 nscan++; 506 } 507 508 if (1 == nscan) { 509 /* only one candidate -- skip scan ;) */ 510 audio_dbg(1, "only one main carrier candidate - skipping scan\n"); 511 max1 = 12345; 512 carrier = default_carrier; 513 } else { 514 /* scan for the main carrier */ 515 saa_writeb(SAA7134_MONITOR_SELECT,0x00); 516 tvaudio_setmode(dev,&tvaudio[0],NULL); 517 for (i = 0; i < ARRAY_SIZE(mainscan); i++) { 518 carr_vals[i] = tvaudio_checkcarrier(dev, mainscan+i); 519 if (dev->thread.scan1 != dev->thread.scan2) 520 goto restart; 521 } 522 for (max1 = 0, max2 = 0, i = 0; i < ARRAY_SIZE(mainscan); i++) { 523 if (max1 < carr_vals[i]) { 524 max2 = max1; 525 max1 = carr_vals[i]; 526 carrier = mainscan[i].carr; 527 } else if (max2 < carr_vals[i]) { 528 max2 = carr_vals[i]; 529 } 530 } 531 } 532 533 if (0 != carrier && max1 > 2000 && max1 > max2*3) { 534 /* found good carrier */ 535 audio_dbg(1, "found %s main sound carrier @ %d.%03d MHz [%d/%d]\n", 536 dev->tvnorm->name, carrier/1000, carrier%1000, 537 max1, max2); 538 dev->last_carrier = carrier; 539 dev->automute = 0; 540 541 } else if (0 != dev->last_carrier) { 542 /* no carrier -- try last detected one as fallback */ 543 carrier = dev->last_carrier; 544 audio_dbg(1, "audio carrier scan failed, using %d.%03d MHz [last detected]\n", 545 carrier/1000, carrier%1000); 546 dev->automute = 1; 547 548 } else { 549 /* no carrier + no fallback -- use default */ 550 carrier = default_carrier; 551 audio_dbg(1, "audio carrier scan failed, using %d.%03d MHz [default]\n", 552 carrier/1000, carrier%1000); 553 dev->automute = 1; 554 } 555 tvaudio_setcarrier(dev,carrier,carrier); 556 saa_andorb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0x30, 0x00); 557 saa7134_tvaudio_setmute(dev); 558 /* find the exact tv audio norm */ 559 for (audio = UNSET, i = 0; i < TVAUDIO; i++) { 560 if (dev->tvnorm->id != UNSET && 561 !(dev->tvnorm->id & tvaudio[i].std)) 562 continue; 563 if (tvaudio[i].carr1 != carrier) 564 continue; 565 /* Note: at least the primary carrier is right here */ 566 if (UNSET == audio) 567 audio = i; 568 tvaudio_setmode(dev,&tvaudio[i],"trying"); 569 if (tvaudio_sleep(dev,SCAN_SUBCARRIER_DELAY)) 570 goto restart; 571 if (-1 != tvaudio_getstereo(dev,&tvaudio[i])) { 572 audio = i; 573 break; 574 } 575 } 576 saa_andorb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0x30, 0x30); 577 if (UNSET == audio) 578 continue; 579 tvaudio_setmode(dev,&tvaudio[audio],"using"); 580 581 tvaudio_setstereo(dev,&tvaudio[audio],V4L2_TUNER_MODE_MONO); 582 dev->tvaudio = &tvaudio[audio]; 583 584 lastmode = 42; 585 for (;;) { 586 587 try_to_freeze(); 588 589 if (tvaudio_sleep(dev,5000)) 590 goto restart; 591 if (kthread_should_stop()) 592 break; 593 if (UNSET == dev->thread.mode) { 594 rx = tvaudio_getstereo(dev, &tvaudio[audio]); 595 mode = saa7134_tvaudio_rx2mode(rx); 596 } else { 597 mode = dev->thread.mode; 598 } 599 if (lastmode != mode) { 600 tvaudio_setstereo(dev,&tvaudio[audio],mode); 601 lastmode = mode; 602 } 603 } 604 } 605 606 done: 607 dev->thread.stopped = 1; 608 return 0; 609 } 610 611 /* ------------------------------------------------------------------ */ 612 /* saa7133 / saa7135 code */ 613 614 static char *stdres[0x20] = { 615 [0x00] = "no standard detected", 616 [0x01] = "B/G (in progress)", 617 [0x02] = "D/K (in progress)", 618 [0x03] = "M (in progress)", 619 620 [0x04] = "B/G A2", 621 [0x05] = "B/G NICAM", 622 [0x06] = "D/K A2 (1)", 623 [0x07] = "D/K A2 (2)", 624 [0x08] = "D/K A2 (3)", 625 [0x09] = "D/K NICAM", 626 [0x0a] = "L NICAM", 627 [0x0b] = "I NICAM", 628 629 [0x0c] = "M Korea", 630 [0x0d] = "M BTSC ", 631 [0x0e] = "M EIAJ", 632 633 [0x0f] = "FM radio / IF 10.7 / 50 deemp", 634 [0x10] = "FM radio / IF 10.7 / 75 deemp", 635 [0x11] = "FM radio / IF sel / 50 deemp", 636 [0x12] = "FM radio / IF sel / 75 deemp", 637 638 [0x13 ... 0x1e ] = "unknown", 639 [0x1f] = "??? [in progress]", 640 }; 641 642 #define DSP_RETRY 32 643 #define DSP_DELAY 16 644 #define SAA7135_DSP_RWCLEAR_RERR 1 645 646 static inline int saa_dsp_reset_error_bit(struct saa7134_dev *dev) 647 { 648 int state = saa_readb(SAA7135_DSP_RWSTATE); 649 if (unlikely(state & SAA7135_DSP_RWSTATE_ERR)) { 650 audio_dbg(2, "%s: resetting error bit\n", dev->name); 651 saa_writeb(SAA7135_DSP_RWCLEAR, SAA7135_DSP_RWCLEAR_RERR); 652 } 653 return 0; 654 } 655 656 static inline int saa_dsp_wait_bit(struct saa7134_dev *dev, int bit) 657 { 658 int state, count = DSP_RETRY; 659 660 state = saa_readb(SAA7135_DSP_RWSTATE); 661 if (unlikely(state & SAA7135_DSP_RWSTATE_ERR)) { 662 pr_warn("%s: dsp access error\n", dev->name); 663 saa_dsp_reset_error_bit(dev); 664 return -EIO; 665 } 666 while (0 == (state & bit)) { 667 if (unlikely(0 == count)) { 668 pr_err("dsp access wait timeout [bit=%s]\n", 669 (bit & SAA7135_DSP_RWSTATE_WRR) ? "WRR" : 670 (bit & SAA7135_DSP_RWSTATE_RDB) ? "RDB" : 671 (bit & SAA7135_DSP_RWSTATE_IDA) ? "IDA" : 672 "???"); 673 return -EIO; 674 } 675 saa_wait(DSP_DELAY); 676 state = saa_readb(SAA7135_DSP_RWSTATE); 677 count--; 678 } 679 return 0; 680 } 681 682 683 int saa_dsp_writel(struct saa7134_dev *dev, int reg, u32 value) 684 { 685 int err; 686 687 audio_dbg(2, "dsp write reg 0x%x = 0x%06x\n", reg << 2, value); 688 err = saa_dsp_wait_bit(dev,SAA7135_DSP_RWSTATE_WRR); 689 if (err < 0) 690 return err; 691 saa_writel(reg,value); 692 err = saa_dsp_wait_bit(dev,SAA7135_DSP_RWSTATE_WRR); 693 if (err < 0) 694 return err; 695 return 0; 696 } 697 698 static int getstereo_7133(struct saa7134_dev *dev) 699 { 700 int retval = V4L2_TUNER_SUB_MONO; 701 u32 value; 702 703 value = saa_readl(0x528 >> 2); 704 if (value & 0x20) 705 retval = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO; 706 if (value & 0x40) 707 retval = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2; 708 return retval; 709 } 710 711 static int mute_input_7133(struct saa7134_dev *dev) 712 { 713 u32 reg = 0; 714 u32 xbarin, xbarout; 715 int mask; 716 struct saa7134_input *in; 717 718 xbarin = 0x03; 719 switch (dev->input->amux) { 720 case TV: 721 reg = 0x02; 722 xbarin = 0; 723 break; 724 case LINE1: 725 reg = 0x00; 726 break; 727 case LINE2: 728 case LINE2_LEFT: 729 reg = 0x09; 730 break; 731 } 732 saa_dsp_writel(dev, 0x464 >> 2, xbarin); 733 if (dev->ctl_mute) { 734 reg = 0x07; 735 xbarout = 0xbbbbbb; 736 } else 737 xbarout = 0xbbbb10; 738 saa_dsp_writel(dev, 0x46c >> 2, xbarout); 739 740 saa_writel(0x594 >> 2, reg); 741 742 743 /* switch gpio-connected external audio mux */ 744 if (0 != card(dev).gpiomask) { 745 mask = card(dev).gpiomask; 746 747 if (card(dev).mute.type && dev->ctl_mute) 748 in = &card(dev).mute; 749 else 750 in = dev->input; 751 752 saa_andorl(SAA7134_GPIO_GPMODE0 >> 2, mask, mask); 753 saa_andorl(SAA7134_GPIO_GPSTATUS0 >> 2, mask, in->gpio); 754 saa7134_track_gpio(dev, saa7134_input_name[in->type]); 755 } 756 757 return 0; 758 } 759 760 static int tvaudio_thread_ddep(void *data) 761 { 762 struct saa7134_dev *dev = data; 763 u32 value, norms; 764 765 set_freezable(); 766 for (;;) { 767 tvaudio_sleep(dev,-1); 768 if (kthread_should_stop()) 769 goto done; 770 restart: 771 try_to_freeze(); 772 773 dev->thread.scan1 = dev->thread.scan2; 774 audio_dbg(1, "tvaudio thread scan start [%d]\n", 775 dev->thread.scan1); 776 777 if (audio_ddep >= 0x04 && audio_ddep <= 0x0e) { 778 /* insmod option override */ 779 norms = (audio_ddep << 2) | 0x01; 780 audio_dbg(1, "ddep override: %s\n", 781 stdres[audio_ddep]); 782 } else if (&card(dev).radio == dev->input) { 783 audio_dbg(1, "FM Radio\n"); 784 if (dev->tuner_type == TUNER_PHILIPS_TDA8290) { 785 norms = (0x11 << 2) | 0x01; 786 /* set IF frequency to 5.5 MHz */ 787 saa_dsp_writel(dev, 0x42c >> 2, 0x729555); 788 } else { 789 norms = (0x0f << 2) | 0x01; 790 } 791 } else { 792 /* (let chip) scan for sound carrier */ 793 norms = 0; 794 if (dev->tvnorm->id & (V4L2_STD_B | V4L2_STD_GH)) 795 norms |= 0x04; 796 if (dev->tvnorm->id & V4L2_STD_PAL_I) 797 norms |= 0x20; 798 if (dev->tvnorm->id & V4L2_STD_DK) 799 norms |= 0x08; 800 if (dev->tvnorm->id & V4L2_STD_MN) 801 norms |= 0x40; 802 if (dev->tvnorm->id & (V4L2_STD_SECAM_L | V4L2_STD_SECAM_LC)) 803 norms |= 0x10; 804 if (0 == norms) 805 norms = 0x7c; /* all */ 806 audio_dbg(1, "scanning:%s%s%s%s%s\n", 807 (norms & 0x04) ? " B/G" : "", 808 (norms & 0x08) ? " D/K" : "", 809 (norms & 0x10) ? " L/L'" : "", 810 (norms & 0x20) ? " I" : "", 811 (norms & 0x40) ? " M" : ""); 812 } 813 814 /* kick automatic standard detection */ 815 saa_dsp_writel(dev, 0x454 >> 2, 0); 816 saa_dsp_writel(dev, 0x454 >> 2, norms | 0x80); 817 818 /* setup crossbars */ 819 saa_dsp_writel(dev, 0x464 >> 2, 0x000000); 820 saa_dsp_writel(dev, 0x470 >> 2, 0x101010); 821 822 if (tvaudio_sleep(dev,3000)) 823 goto restart; 824 value = saa_readl(0x528 >> 2) & 0xffffff; 825 826 audio_dbg(1, "tvaudio thread status: 0x%x [%s%s%s]\n", 827 value, stdres[value & 0x1f], 828 (value & 0x000020) ? ",stereo" : "", 829 (value & 0x000040) ? ",dual" : ""); 830 audio_dbg(1, "detailed status: %s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s\n", 831 (value & 0x000080) ? " A2/EIAJ pilot tone " : "", 832 (value & 0x000100) ? " A2/EIAJ dual " : "", 833 (value & 0x000200) ? " A2/EIAJ stereo " : "", 834 (value & 0x000400) ? " A2/EIAJ noise mute " : "", 835 836 (value & 0x000800) ? " BTSC/FM radio pilot " : "", 837 (value & 0x001000) ? " SAP carrier " : "", 838 (value & 0x002000) ? " BTSC stereo noise mute " : "", 839 (value & 0x004000) ? " SAP noise mute " : "", 840 (value & 0x008000) ? " VDSP " : "", 841 842 (value & 0x010000) ? " NICST " : "", 843 (value & 0x020000) ? " NICDU " : "", 844 (value & 0x040000) ? " NICAM muted " : "", 845 (value & 0x080000) ? " NICAM reserve sound " : "", 846 847 (value & 0x100000) ? " init done " : ""); 848 } 849 850 done: 851 dev->thread.stopped = 1; 852 return 0; 853 } 854 855 /* ------------------------------------------------------------------ */ 856 /* common stuff + external entry points */ 857 858 void saa7134_enable_i2s(struct saa7134_dev *dev) 859 { 860 int i2s_format; 861 862 if (!card_is_empress(dev)) 863 return; 864 865 if (dev->pci->device == PCI_DEVICE_ID_PHILIPS_SAA7130) 866 return; 867 868 /* configure GPIO for out */ 869 saa_andorl(SAA7134_GPIO_GPMODE0 >> 2, 0x0E000000, 0x00000000); 870 871 switch (dev->pci->device) { 872 case PCI_DEVICE_ID_PHILIPS_SAA7133: 873 case PCI_DEVICE_ID_PHILIPS_SAA7135: 874 /* Set I2S format (SONY) */ 875 saa_writeb(SAA7133_I2S_AUDIO_CONTROL, 0x00); 876 /* Start I2S */ 877 saa_writeb(SAA7134_I2S_AUDIO_OUTPUT, 0x11); 878 break; 879 880 case PCI_DEVICE_ID_PHILIPS_SAA7134: 881 i2s_format = (dev->input->amux == TV) ? 0x00 : 0x01; 882 883 /* enable I2S audio output for the mpeg encoder */ 884 saa_writeb(SAA7134_I2S_OUTPUT_SELECT, 0x80); 885 saa_writeb(SAA7134_I2S_OUTPUT_FORMAT, i2s_format); 886 saa_writeb(SAA7134_I2S_OUTPUT_LEVEL, 0x0F); 887 saa_writeb(SAA7134_I2S_AUDIO_OUTPUT, 0x01); 888 889 default: 890 break; 891 } 892 } 893 894 int saa7134_tvaudio_rx2mode(u32 rx) 895 { 896 u32 mode; 897 898 mode = V4L2_TUNER_MODE_MONO; 899 if (rx & V4L2_TUNER_SUB_STEREO) 900 mode = V4L2_TUNER_MODE_STEREO; 901 else if (rx & V4L2_TUNER_SUB_LANG1) 902 mode = V4L2_TUNER_MODE_LANG1; 903 else if (rx & V4L2_TUNER_SUB_LANG2) 904 mode = V4L2_TUNER_MODE_LANG2; 905 return mode; 906 } 907 908 void saa7134_tvaudio_setmute(struct saa7134_dev *dev) 909 { 910 switch (dev->pci->device) { 911 case PCI_DEVICE_ID_PHILIPS_SAA7130: 912 case PCI_DEVICE_ID_PHILIPS_SAA7134: 913 mute_input_7134(dev); 914 break; 915 case PCI_DEVICE_ID_PHILIPS_SAA7133: 916 case PCI_DEVICE_ID_PHILIPS_SAA7135: 917 mute_input_7133(dev); 918 break; 919 } 920 } 921 922 void saa7134_tvaudio_setinput(struct saa7134_dev *dev, 923 struct saa7134_input *in) 924 { 925 dev->input = in; 926 switch (dev->pci->device) { 927 case PCI_DEVICE_ID_PHILIPS_SAA7130: 928 case PCI_DEVICE_ID_PHILIPS_SAA7134: 929 mute_input_7134(dev); 930 break; 931 case PCI_DEVICE_ID_PHILIPS_SAA7133: 932 case PCI_DEVICE_ID_PHILIPS_SAA7135: 933 mute_input_7133(dev); 934 break; 935 } 936 saa7134_enable_i2s(dev); 937 } 938 939 void saa7134_tvaudio_setvolume(struct saa7134_dev *dev, int level) 940 { 941 switch (dev->pci->device) { 942 case PCI_DEVICE_ID_PHILIPS_SAA7134: 943 saa_writeb(SAA7134_CHANNEL1_LEVEL, level & 0x1f); 944 saa_writeb(SAA7134_CHANNEL2_LEVEL, level & 0x1f); 945 saa_writeb(SAA7134_NICAM_LEVEL_ADJUST, level & 0x1f); 946 break; 947 } 948 } 949 950 int saa7134_tvaudio_getstereo(struct saa7134_dev *dev) 951 { 952 int retval = V4L2_TUNER_SUB_MONO; 953 954 switch (dev->pci->device) { 955 case PCI_DEVICE_ID_PHILIPS_SAA7134: 956 if (dev->tvaudio) 957 retval = tvaudio_getstereo(dev,dev->tvaudio); 958 break; 959 case PCI_DEVICE_ID_PHILIPS_SAA7133: 960 case PCI_DEVICE_ID_PHILIPS_SAA7135: 961 retval = getstereo_7133(dev); 962 break; 963 } 964 return retval; 965 } 966 967 void saa7134_tvaudio_init(struct saa7134_dev *dev) 968 { 969 int clock = saa7134_boards[dev->board].audio_clock; 970 971 if (UNSET != audio_clock_override) 972 clock = audio_clock_override; 973 974 switch (dev->pci->device) { 975 case PCI_DEVICE_ID_PHILIPS_SAA7134: 976 /* init all audio registers */ 977 saa_writeb(SAA7134_AUDIO_PLL_CTRL, 0x00); 978 if (need_resched()) 979 schedule(); 980 else 981 udelay(10); 982 983 saa_writeb(SAA7134_AUDIO_CLOCK0, clock & 0xff); 984 saa_writeb(SAA7134_AUDIO_CLOCK1, (clock >> 8) & 0xff); 985 saa_writeb(SAA7134_AUDIO_CLOCK2, (clock >> 16) & 0xff); 986 /* frame locked audio is mandatory for NICAM */ 987 saa_writeb(SAA7134_AUDIO_PLL_CTRL, 0x01); 988 saa_writeb(SAA7134_NICAM_ERROR_LOW, 0x14); 989 saa_writeb(SAA7134_NICAM_ERROR_HIGH, 0x50); 990 break; 991 case PCI_DEVICE_ID_PHILIPS_SAA7133: 992 case PCI_DEVICE_ID_PHILIPS_SAA7135: 993 saa_writel(0x598 >> 2, clock); 994 saa_dsp_writel(dev, 0x474 >> 2, 0x00); 995 saa_dsp_writel(dev, 0x450 >> 2, 0x00); 996 } 997 } 998 999 int saa7134_tvaudio_init2(struct saa7134_dev *dev) 1000 { 1001 int (*my_thread)(void *data) = NULL; 1002 1003 switch (dev->pci->device) { 1004 case PCI_DEVICE_ID_PHILIPS_SAA7134: 1005 my_thread = tvaudio_thread; 1006 break; 1007 case PCI_DEVICE_ID_PHILIPS_SAA7133: 1008 case PCI_DEVICE_ID_PHILIPS_SAA7135: 1009 my_thread = tvaudio_thread_ddep; 1010 break; 1011 } 1012 1013 dev->thread.thread = NULL; 1014 dev->thread.scan1 = dev->thread.scan2 = 0; 1015 if (my_thread) { 1016 saa7134_tvaudio_init(dev); 1017 /* start tvaudio thread */ 1018 dev->thread.thread = kthread_run(my_thread, dev, "%s", dev->name); 1019 if (IS_ERR(dev->thread.thread)) { 1020 pr_warn("%s: kernel_thread() failed\n", 1021 dev->name); 1022 /* XXX: missing error handling here */ 1023 } 1024 } 1025 1026 saa7134_enable_i2s(dev); 1027 return 0; 1028 } 1029 1030 int saa7134_tvaudio_close(struct saa7134_dev *dev) 1031 { 1032 dev->automute = 1; 1033 /* anything else to undo? */ 1034 return 0; 1035 } 1036 1037 int saa7134_tvaudio_fini(struct saa7134_dev *dev) 1038 { 1039 /* shutdown tvaudio thread */ 1040 if (dev->thread.thread && !dev->thread.stopped) 1041 kthread_stop(dev->thread.thread); 1042 1043 saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x07, 0x00); /* LINE1 */ 1044 return 0; 1045 } 1046 1047 int saa7134_tvaudio_do_scan(struct saa7134_dev *dev) 1048 { 1049 if (dev->input->amux != TV) { 1050 audio_dbg(1, "sound IF not in use, skipping scan\n"); 1051 dev->automute = 0; 1052 saa7134_tvaudio_setmute(dev); 1053 } else if (dev->thread.thread) { 1054 dev->thread.mode = UNSET; 1055 dev->thread.scan2++; 1056 1057 if (!dev->insuspend && !dev->thread.stopped) 1058 wake_up_process(dev->thread.thread); 1059 } else { 1060 dev->automute = 0; 1061 saa7134_tvaudio_setmute(dev); 1062 } 1063 return 0; 1064 } 1065 1066 EXPORT_SYMBOL(saa_dsp_writel); 1067 EXPORT_SYMBOL(saa7134_tvaudio_setmute); 1068