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