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