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