The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/bktr/msp34xx.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 1997-2001 Gerd Knorr <kraxel@bytesex.org>
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  *
   14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   24  * SUCH DAMAGE.
   25  *
   26  * $FreeBSD$
   27  */
   28 
   29 /*
   30  * programming the msp34* sound processor family
   31  *
   32  * (c) 1997-2001 Gerd Knorr <kraxel@bytesex.org>
   33  *
   34  * what works and what doesn't:
   35  *
   36  *  AM-Mono
   37  *      Support for Hauppauge cards added (decoding handled by tuner) added by
   38  *      Frederic Crozat <fcrozat@mail.dotcom.fr>
   39  *
   40  *  FM-Mono
   41  *      should work. The stereo modes are backward compatible to FM-mono,
   42  *      therefore FM-Mono should be allways available.
   43  *
   44  *  FM-Stereo (B/G, used in germany)
   45  *      should work, with autodetect
   46  *
   47  *  FM-Stereo (satellite)
   48  *      should work, no autodetect (i.e. default is mono, but you can
   49  *      switch to stereo -- untested)
   50  *
   51  *  NICAM (B/G, L , used in UK, Scandinavia, Spain and France)
   52  *      should work, with autodetect. Support for NICAM was added by
   53  *      Pekka Pietikainen <pp@netppl.fi>
   54  *
   55  *
   56  * TODO:
   57  *   - better SAT support
   58  *
   59  *
   60  * 980623  Thomas Sailer (sailer@ife.ee.ethz.ch)
   61  *         using soundcore instead of OSS
   62  *
   63  *
   64  * The FreeBSD modifications by Alexander Langer <alex@FreeBSD.org>
   65  * are in the public domain.  Please contact me (Alex) and not Gerd for
   66  * any problems you encounter under FreeBSD.
   67  * 
   68  * FreeBSD TODO: 
   69  * - mutex handling (currently not mp-safe)
   70  * - the various options here as loader tunables or compile time or whatever
   71  * - how does the new dolby flag work with the current dpl_* stuff?
   72  *   Maybe it's just enough to set the dolby flag to 1 and it works.
   73  *   As I don't have a dolby card myself, I can't test it, though.
   74  */
   75 
   76 #include "opt_bktr.h"                   /* Include any kernel config options */
   77 #ifdef BKTR_NEW_MSP34XX_DRIVER          /* file only needed for new driver */
   78 
   79 #include <sys/param.h>
   80 #include <sys/systm.h>
   81 #include <sys/kernel.h>
   82 #include <sys/vnode.h>
   83 
   84 #include <sys/unistd.h>
   85 #include <sys/kthread.h>
   86 #include <sys/malloc.h>
   87 
   88 #ifdef BKTR_USE_FREEBSD_SMBUS
   89 #include <sys/bus.h>                    /* required by bktr_reg.h */
   90 #endif
   91 
   92 #include <machine/bus.h>                /* required by bktr_reg.h */
   93 
   94 #include <dev/bktr/ioctl_meteor.h>
   95 #include <dev/bktr/ioctl_bt848.h>       /* extensions to ioctl_meteor.h */
   96 #include <dev/bktr/bktr_reg.h>
   97 #include <dev/bktr/bktr_tuner.h>
   98 #include <dev/bktr/bktr_audio.h>
   99 #include <dev/bktr/bktr_core.h>
  100 
  101 #define VIDEO_MODE_PAL          0
  102 #define VIDEO_MODE_NTSC         1
  103 #define VIDEO_MODE_SECAM        2
  104 #define VIDEO_MODE_AUTO         3
  105 
  106 #define VIDEO_SOUND_MONO        1
  107 #define VIDEO_SOUND_STEREO      2
  108 #define VIDEO_SOUND_LANG1       4
  109 #define VIDEO_SOUND_LANG2       8
  110 
  111 #define DFP_COUNT 0x41
  112 static const int bl_dfp[] = {
  113         0x00, 0x01, 0x02, 0x03,  0x06, 0x08, 0x09, 0x0a,
  114         0x0b, 0x0d, 0x0e, 0x10
  115 };
  116 
  117 struct msp3400c {
  118         int simple;
  119         int nicam;
  120         int mode;
  121         int norm;
  122         int stereo;
  123         int nicam_on;
  124         int acb;
  125         int main, second;       /* sound carrier */
  126         int input;
  127 
  128         int muted;
  129         int left, right;        /* volume */
  130         int bass, treble;
  131 
  132         /* shadow register set */
  133         int dfp_regs[DFP_COUNT];
  134 
  135         /* thread */
  136         struct proc         *kthread;
  137         char                *threaddesc;
  138 
  139         int                  active,restart,rmmod;
  140 
  141         int                  watch_stereo;
  142         int                  halt_thread;
  143 };
  144 
  145 #define VIDEO_MODE_RADIO 16      /* norm magic for radio mode */
  146 
  147 /* ---------------------------------------------------------------------- */
  148 
  149 #define dprintk(...) do {                                               \
  150         if (bootverbose) {                                              \
  151                 printf("%s: ", bktr_name(client));                      \
  152                 printf(__VA_ARGS__);                                    \
  153         }                                                               \
  154 } while (0)
  155 
  156 /* ---------------------------------------------------------------------- */
  157 
  158 #define I2C_MSP3400C       0x80
  159 #define I2C_MSP3400C_DEM   0x10
  160 #define I2C_MSP3400C_DFP   0x12
  161 
  162 /* ----------------------------------------------------------------------- */
  163 /* functions for talking to the MSP3400C Sound processor                   */
  164 
  165 static int msp3400c_reset(bktr_ptr_t client)
  166 {
  167         /* use our own which handles config(8) options */
  168         msp_dpl_reset(client, client->msp_addr);
  169 
  170         return 0;
  171 }
  172 
  173 static int
  174 msp3400c_read(bktr_ptr_t client, int dev, int addr)
  175 {
  176         /* use our own */
  177         return(msp_dpl_read(client, client->msp_addr, dev, addr));
  178 }
  179 
  180 static int
  181 msp3400c_write(bktr_ptr_t client, int dev, int addr, int val)
  182 {
  183         /* use our own */
  184         msp_dpl_write(client, client->msp_addr, dev, addr, val);
  185 
  186         return(0);
  187 }
  188 
  189 /* ------------------------------------------------------------------------ */
  190 
  191 /* This macro is allowed for *constants* only, gcc must calculate it
  192    at compile time.  Remember -- no floats in kernel mode */
  193 #define MSP_CARRIER(freq) ((int)((float)(freq/18.432)*(1<<24)))
  194 
  195 #define MSP_MODE_AM_DETECT   0
  196 #define MSP_MODE_FM_RADIO    2
  197 #define MSP_MODE_FM_TERRA    3
  198 #define MSP_MODE_FM_SAT      4
  199 #define MSP_MODE_FM_NICAM1   5
  200 #define MSP_MODE_FM_NICAM2   6
  201 #define MSP_MODE_AM_NICAM    7
  202 #define MSP_MODE_BTSC        8
  203 #define MSP_MODE_EXTERN      9
  204 
  205 static struct MSP_INIT_DATA_DEM {
  206         int fir1[6];
  207         int fir2[6];
  208         int cdo1;
  209         int cdo2;
  210         int ad_cv;
  211         int mode_reg;
  212         int dfp_src;
  213         int dfp_matrix;
  214 } msp_init_data[] = {
  215         /* AM (for carrier detect / msp3400) */
  216         { { 75, 19, 36, 35, 39, 40 }, { 75, 19, 36, 35, 39, 40 },
  217           MSP_CARRIER(5.5), MSP_CARRIER(5.5),
  218           0x00d0, 0x0500,   0x0020, 0x3000},
  219 
  220         /* AM (for carrier detect / msp3410) */
  221         { { -1, -1, -8, 2, 59, 126 }, { -1, -1, -8, 2, 59, 126 },
  222           MSP_CARRIER(5.5), MSP_CARRIER(5.5),
  223           0x00d0, 0x0100,   0x0020, 0x3000},
  224 
  225         /* FM Radio */
  226         { { -8, -8, 4, 6, 78, 107 }, { -8, -8, 4, 6, 78, 107 },
  227           MSP_CARRIER(10.7), MSP_CARRIER(10.7),
  228           0x00d0, 0x0480, 0x0020, 0x3000 },
  229 
  230         /* Terrestial FM-mono + FM-stereo */
  231         { {  3, 18, 27, 48, 66, 72 }, {  3, 18, 27, 48, 66, 72 },
  232           MSP_CARRIER(5.5), MSP_CARRIER(5.5),
  233           0x00d0, 0x0480,   0x0030, 0x3000},
  234 
  235         /* Sat FM-mono */
  236         { {  1,  9, 14, 24, 33, 37 }, {  3, 18, 27, 48, 66, 72 },
  237           MSP_CARRIER(6.5), MSP_CARRIER(6.5),
  238           0x00c6, 0x0480,   0x0000, 0x3000},
  239 
  240         /* NICAM/FM --  B/G (5.5/5.85), D/K (6.5/5.85) */
  241         { { -2, -8, -10, 10, 50, 86 }, {  3, 18, 27, 48, 66, 72 },
  242           MSP_CARRIER(5.5), MSP_CARRIER(5.5),
  243           0x00d0, 0x0040,   0x0120, 0x3000},
  244 
  245         /* NICAM/FM -- I (6.0/6.552) */
  246         { {  2, 4, -6, -4, 40, 94 }, {  3, 18, 27, 48, 66, 72 },
  247           MSP_CARRIER(6.0), MSP_CARRIER(6.0),
  248           0x00d0, 0x0040,   0x0120, 0x3000},
  249 
  250         /* NICAM/AM -- L (6.5/5.85) */
  251         { {  -2, -8, -10, 10, 50, 86 }, {  -4, -12, -9, 23, 79, 126 },
  252           MSP_CARRIER(6.5), MSP_CARRIER(6.5),
  253           0x00c6, 0x0140,   0x0120, 0x7c03},
  254 };
  255 
  256 struct CARRIER_DETECT {
  257         int   cdo;
  258         char *name;
  259 };
  260 
  261 static struct CARRIER_DETECT carrier_detect_main[] = {
  262         /* main carrier */
  263         { MSP_CARRIER(4.5),        "4.5   NTSC"                   }, 
  264         { MSP_CARRIER(5.5),        "5.5   PAL B/G"                }, 
  265         { MSP_CARRIER(6.0),        "6.0   PAL I"                  },
  266         { MSP_CARRIER(6.5),        "6.5   PAL D/K + SAT + SECAM"  }
  267 };
  268 
  269 static struct CARRIER_DETECT carrier_detect_55[] = {
  270         /* PAL B/G */
  271         { MSP_CARRIER(5.7421875),  "5.742 PAL B/G FM-stereo"     }, 
  272         { MSP_CARRIER(5.85),       "5.85  PAL B/G NICAM"         }
  273 };
  274 
  275 static struct CARRIER_DETECT carrier_detect_65[] = {
  276         /* PAL SAT / SECAM */
  277         { MSP_CARRIER(5.85),       "5.85  PAL D/K + SECAM NICAM" },
  278         { MSP_CARRIER(6.2578125),  "6.25  PAL D/K1 FM-stereo" },
  279         { MSP_CARRIER(6.7421875),  "6.74  PAL D/K2 FM-stereo" },
  280         { MSP_CARRIER(7.02),       "7.02  PAL SAT FM-stereo s/b" },
  281         { MSP_CARRIER(7.20),       "7.20  PAL SAT FM-stereo s"   },
  282         { MSP_CARRIER(7.38),       "7.38  PAL SAT FM-stereo b"   },
  283 };
  284 
  285 #define CARRIER_COUNT(x) (sizeof(x)/sizeof(struct CARRIER_DETECT))
  286 
  287 /* ----------------------------------------------------------------------- */
  288 
  289 #define SCART_MASK    0
  290 #define SCART_IN1     1
  291 #define SCART_IN2     2
  292 #define SCART_IN1_DA  3
  293 #define SCART_IN2_DA  4
  294 #define SCART_IN3     5
  295 #define SCART_IN4     6
  296 #define SCART_MONO    7
  297 #define SCART_MUTE    8
  298 
  299 static int scarts[3][9] = {
  300   /* MASK    IN1     IN2     IN1_DA  IN2_DA  IN3     IN4     MONO    MUTE   */
  301   {  0x0320, 0x0000, 0x0200, -1,     -1,     0x0300, 0x0020, 0x0100, 0x0320 },
  302   {  0x0c40, 0x0440, 0x0400, 0x0c00, 0x0040, 0x0000, 0x0840, 0x0800, 0x0c40 },
  303   {  0x3080, 0x1000, 0x1080, 0x0000, 0x0080, 0x2080, 0x3080, 0x2000, 0x3000 },
  304 };
  305 
  306 static char *scart_names[] = {
  307   "mask", "in1", "in2", "in1 da", "in2 da", "in3", "in4", "mono", "mute"
  308 };
  309 
  310 static void
  311 msp3400c_set_scart(bktr_ptr_t client, int in, int out)
  312 {
  313         struct msp3400c *msp = client->msp3400c_info;
  314 
  315         if (-1 == scarts[out][in])
  316                 return;
  317 
  318         dprintk("msp34xx: scart switch: %s => %d\n",scart_names[in],out);
  319         msp->acb &= ~scarts[out][SCART_MASK];
  320         msp->acb |=  scarts[out][in];
  321         msp3400c_write(client,I2C_MSP3400C_DFP, 0x0013, msp->acb);
  322 }
  323 
  324 /* ------------------------------------------------------------------------ */
  325 
  326 static void msp3400c_setcarrier(bktr_ptr_t client, int cdo1, int cdo2)
  327 {
  328         msp3400c_write(client,I2C_MSP3400C_DEM, 0x0093, cdo1 & 0xfff);
  329         msp3400c_write(client,I2C_MSP3400C_DEM, 0x009b, cdo1 >> 12);
  330         msp3400c_write(client,I2C_MSP3400C_DEM, 0x00a3, cdo2 & 0xfff);
  331         msp3400c_write(client,I2C_MSP3400C_DEM, 0x00ab, cdo2 >> 12);
  332         msp3400c_write(client,I2C_MSP3400C_DEM, 0x0056, 0); /*LOAD_REG_1/2*/
  333 }
  334 
  335 static void msp3400c_setvolume(bktr_ptr_t client,
  336                                int muted, int left, int right)
  337 {
  338         int vol = 0,val = 0,balance = 0;
  339 
  340         if (!muted) {
  341                 vol     = (left > right) ? left : right;
  342                 val     = (vol * 0x73 / 65535) << 8;
  343         }
  344         if (vol > 0) {
  345                 balance = ((right-left) * 127) / vol;
  346         }
  347 
  348         dprintk("msp34xx: setvolume: mute=%s %d:%d  v=0x%02x b=0x%02x\n",
  349                 muted ? "on" : "off", left, right, val>>8, balance);
  350         msp3400c_write(client,I2C_MSP3400C_DFP, 0x0000, val); /* loudspeaker */
  351         msp3400c_write(client,I2C_MSP3400C_DFP, 0x0006, val); /* headphones  */
  352         /* scart - on/off only */
  353         msp3400c_write(client,I2C_MSP3400C_DFP, 0x0007, val ? 0x4000 : 0);
  354         msp3400c_write(client,I2C_MSP3400C_DFP, 0x0001, balance << 8);
  355 }
  356 
  357 static void msp3400c_setbass(bktr_ptr_t client, int bass)
  358 {
  359         int val = ((bass-32768) * 0x60 / 65535) << 8;
  360 
  361         dprintk("msp34xx: setbass: %d 0x%02x\n",bass, val>>8);
  362         msp3400c_write(client,I2C_MSP3400C_DFP, 0x0002, val); /* loudspeaker */
  363 }
  364 
  365 static void msp3400c_settreble(bktr_ptr_t client, int treble)
  366 {
  367         int val = ((treble-32768) * 0x60 / 65535) << 8;
  368 
  369         dprintk("msp34xx: settreble: %d 0x%02x\n",treble, val>>8);
  370         msp3400c_write(client,I2C_MSP3400C_DFP, 0x0003, val); /* loudspeaker */
  371 }
  372 
  373 static void msp3400c_setmode(bktr_ptr_t client, int type)
  374 {
  375         struct msp3400c *msp = client->msp3400c_info;
  376         int i;
  377         
  378         dprintk("msp3400: setmode: %d\n",type);
  379         msp->mode   = type;
  380         msp->stereo = VIDEO_SOUND_MONO;
  381 
  382         msp3400c_write(client,I2C_MSP3400C_DEM, 0x00bb,          /* ad_cv */
  383                        msp_init_data[type].ad_cv);
  384     
  385         for (i = 5; i >= 0; i--)                                   /* fir 1 */
  386                 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0001,
  387                                msp_init_data[type].fir1[i]);
  388     
  389         msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005, 0x0004); /* fir 2 */
  390         msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005, 0x0040);
  391         msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005, 0x0000);
  392         for (i = 5; i >= 0; i--)
  393                 msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005,
  394                                msp_init_data[type].fir2[i]);
  395     
  396         msp3400c_write(client,I2C_MSP3400C_DEM, 0x0083,     /* MODE_REG */
  397                        msp_init_data[type].mode_reg);
  398     
  399         msp3400c_setcarrier(client, msp_init_data[type].cdo1,
  400                             msp_init_data[type].cdo2);
  401     
  402         msp3400c_write(client,I2C_MSP3400C_DEM, 0x0056, 0); /*LOAD_REG_1/2*/
  403 
  404         if (client->dolby) {
  405                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008,
  406                                0x0520); /* I2S1 */
  407                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009,
  408                                0x0620); /* I2S2 */
  409                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b,
  410                                msp_init_data[type].dfp_src);
  411         } else {
  412                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008,
  413                                msp_init_data[type].dfp_src);
  414                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009,
  415                                msp_init_data[type].dfp_src);
  416                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b,
  417                                msp_init_data[type].dfp_src);
  418         }
  419         msp3400c_write(client,I2C_MSP3400C_DFP, 0x000a,
  420                        msp_init_data[type].dfp_src);
  421         msp3400c_write(client,I2C_MSP3400C_DFP, 0x000e,
  422                        msp_init_data[type].dfp_matrix);
  423 
  424         if (msp->nicam) {
  425                 /* nicam prescale */
  426                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0010, 0x5a00); /* was: 0x3000 */
  427         }
  428 }
  429 
  430 /* turn on/off nicam + stereo */
  431 static void msp3400c_setstereo(bktr_ptr_t client, int mode)
  432 {
  433         static char *strmode[] = { "", "mono", "stereo", "3",
  434                                    "lang1", "5", "6", "7", "lang2" };
  435         struct msp3400c *msp = client->msp3400c_info;
  436         int nicam=0; /* channel source: FM/AM or nicam */
  437         int src=0;
  438 
  439         /* switch demodulator */
  440         switch (msp->mode) {
  441         case MSP_MODE_FM_TERRA:
  442                 dprintk("msp3400: FM setstereo: %s\n",strmode[mode]);
  443                 msp3400c_setcarrier(client,msp->second,msp->main);
  444                 switch (mode) {
  445                 case VIDEO_SOUND_STEREO:
  446                         msp3400c_write(client,I2C_MSP3400C_DFP, 0x000e, 0x3001);
  447                         break;
  448                 case VIDEO_SOUND_MONO:
  449                 case VIDEO_SOUND_LANG1:
  450                 case VIDEO_SOUND_LANG2:
  451                         msp3400c_write(client,I2C_MSP3400C_DFP, 0x000e, 0x3000);
  452                         break;
  453                 }
  454                 break;
  455         case MSP_MODE_FM_SAT:
  456                 dprintk("msp3400: SAT setstereo: %s\n",strmode[mode]);
  457                 switch (mode) {
  458                 case VIDEO_SOUND_MONO:
  459                         msp3400c_setcarrier(client, MSP_CARRIER(6.5), MSP_CARRIER(6.5));
  460                         break;
  461                 case VIDEO_SOUND_STEREO:
  462                         msp3400c_setcarrier(client, MSP_CARRIER(7.2), MSP_CARRIER(7.02));
  463                         break;
  464                 case VIDEO_SOUND_LANG1:
  465                         msp3400c_setcarrier(client, MSP_CARRIER(7.38), MSP_CARRIER(7.02));
  466                         break;
  467                 case VIDEO_SOUND_LANG2:
  468                         msp3400c_setcarrier(client, MSP_CARRIER(7.38), MSP_CARRIER(7.02));
  469                         break;
  470                 }
  471                 break;
  472         case MSP_MODE_FM_NICAM1:
  473         case MSP_MODE_FM_NICAM2:
  474         case MSP_MODE_AM_NICAM:
  475                 dprintk("msp3400: NICAM setstereo: %s\n",strmode[mode]);
  476                 msp3400c_setcarrier(client,msp->second,msp->main);
  477                 if (msp->nicam_on)
  478                         nicam=0x0100;
  479                 break;
  480         case MSP_MODE_BTSC:
  481                 dprintk("msp3400: BTSC setstereo: %s\n",strmode[mode]);
  482                 nicam=0x0300;
  483                 break;
  484         case MSP_MODE_EXTERN:
  485                 dprintk("msp3400: extern setstereo: %s\n",strmode[mode]);
  486                 nicam = 0x0200;
  487                 break;
  488         case MSP_MODE_FM_RADIO:
  489                 dprintk("msp3400: FM-Radio setstereo: %s\n",strmode[mode]);
  490                 break;
  491         default:
  492                 dprintk("msp3400: mono setstereo\n");
  493                 return;
  494         }
  495 
  496         /* switch audio */
  497         switch (mode) {
  498         case VIDEO_SOUND_STEREO:
  499                 src = 0x0020 | nicam;
  500 #if 0 
  501                 /* spatial effect */
  502                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0005,0x4000);
  503 #endif
  504                 break;
  505         case VIDEO_SOUND_MONO:
  506                 if (msp->mode == MSP_MODE_AM_NICAM) {
  507                         dprintk("msp3400: switching to AM mono\n");
  508                         /* AM mono decoding is handled by tuner, not MSP chip */
  509                         /* SCART switching control register */
  510                         msp3400c_set_scart(client,SCART_MONO,0);
  511                         src = 0x0200;
  512                         break;
  513                 }
  514         case VIDEO_SOUND_LANG1:
  515                 src = 0x0000 | nicam;
  516                 break;
  517         case VIDEO_SOUND_LANG2:
  518                 src = 0x0010 | nicam;
  519                 break;
  520         }
  521         dprintk("msp3400: setstereo final source/matrix = 0x%x\n", src);
  522 
  523         if (client->dolby) {
  524                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008,0x0520);
  525                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009,0x0620);
  526                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000a,src);
  527                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b,src);
  528         } else {
  529                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008,src);
  530                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009,src);
  531                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000a,src);
  532                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b,src);
  533         }
  534 }
  535 
  536 static void
  537 msp3400c_print_mode(struct msp3400c *msp)
  538 {
  539         if (msp->main == msp->second) {
  540                 printf("bktr: msp3400: mono sound carrier: %d.%03d MHz\n",
  541                        msp->main/910000,(msp->main/910)%1000);
  542         } else {
  543                 printf("bktr: msp3400: main sound carrier: %d.%03d MHz\n",
  544                        msp->main/910000,(msp->main/910)%1000);
  545         }
  546         if (msp->mode == MSP_MODE_FM_NICAM1 ||
  547             msp->mode == MSP_MODE_FM_NICAM2)
  548                 printf("bktr: msp3400: NICAM/FM carrier   : %d.%03d MHz\n",
  549                        msp->second/910000,(msp->second/910)%1000);
  550         if (msp->mode == MSP_MODE_AM_NICAM)
  551                 printf("bktr: msp3400: NICAM/AM carrier   : %d.%03d MHz\n",
  552                        msp->second/910000,(msp->second/910)%1000);
  553         if (msp->mode == MSP_MODE_FM_TERRA &&
  554             msp->main != msp->second) {
  555                 printf("bktr: msp3400: FM-stereo carrier : %d.%03d MHz\n",
  556                        msp->second/910000,(msp->second/910)%1000);
  557         }
  558 }
  559 
  560 static void
  561 msp3400c_restore_dfp(bktr_ptr_t client)
  562 {
  563         struct msp3400c *msp = (struct msp3400c*)client->msp3400c_info;
  564         int i;
  565 
  566         for (i = 0; i < DFP_COUNT; i++) {
  567                 if (-1 == msp->dfp_regs[i])
  568                         continue;
  569                 msp3400c_write(client,I2C_MSP3400C_DFP, i, msp->dfp_regs[i]);
  570         }
  571 }
  572 
  573 /* ----------------------------------------------------------------------- */
  574 
  575 struct REGISTER_DUMP {
  576         int   addr;
  577         char *name;
  578 };
  579 
  580 struct REGISTER_DUMP d1[] = {
  581         { 0x007e, "autodetect" },
  582         { 0x0023, "C_AD_BITS " },
  583         { 0x0038, "ADD_BITS  " },
  584         { 0x003e, "CIB_BITS  " },
  585         { 0x0057, "ERROR_RATE" },
  586 };
  587 
  588 static int
  589 autodetect_stereo(bktr_ptr_t client)
  590 {
  591         struct msp3400c *msp = (struct msp3400c*)client->msp3400c_info;
  592         int val;
  593         int newstereo = msp->stereo;
  594         int newnicam  = msp->nicam_on;
  595         int update = 0;
  596 
  597         switch (msp->mode) {
  598         case MSP_MODE_FM_TERRA:
  599                 val = msp3400c_read(client, I2C_MSP3400C_DFP, 0x18);
  600                 if (val > 32767)
  601                         val -= 65536;
  602                 dprintk("msp34xx: stereo detect register: %d\n",val);
  603                 
  604                 if (val > 4096) {
  605                         newstereo = VIDEO_SOUND_STEREO | VIDEO_SOUND_MONO;
  606                 } else if (val < -4096) {
  607                         newstereo = VIDEO_SOUND_LANG1 | VIDEO_SOUND_LANG2;
  608                 } else {
  609                         newstereo = VIDEO_SOUND_MONO;
  610                 }
  611                 newnicam = 0;
  612                 break;
  613         case MSP_MODE_FM_NICAM1:
  614         case MSP_MODE_FM_NICAM2:
  615         case MSP_MODE_AM_NICAM:
  616                 val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x23);
  617                 dprintk("msp34xx: nicam sync=%d, mode=%d\n",val & 1, (val & 0x1e) >> 1);
  618 
  619                 if (val & 1) {
  620                         /* nicam synced */
  621                         switch ((val & 0x1e) >> 1)  {
  622                         case 0:
  623                         case 8:
  624                                 newstereo = VIDEO_SOUND_STEREO;
  625                                 break;
  626                         case 1:
  627                         case 9:
  628                                 newstereo = VIDEO_SOUND_MONO
  629                                         | VIDEO_SOUND_LANG1;
  630                                 break;
  631                         case 2:
  632                         case 10:
  633                                 newstereo = VIDEO_SOUND_MONO
  634                                         | VIDEO_SOUND_LANG1
  635                                         | VIDEO_SOUND_LANG2;
  636                                 break;
  637                         default:
  638                                 newstereo = VIDEO_SOUND_MONO;
  639                                 break;
  640                         }
  641                         newnicam=1;
  642                 } else {
  643                         newnicam = 0;
  644                         newstereo = VIDEO_SOUND_MONO;
  645                 }
  646                 break;
  647         case MSP_MODE_BTSC:
  648                 val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x200);
  649                 dprintk("msp3410: status=0x%x (pri=%s, sec=%s, %s%s%s)\n",
  650                         val,
  651                         (val & 0x0002) ? "no"     : "yes",
  652                         (val & 0x0004) ? "no"     : "yes",
  653                         (val & 0x0040) ? "stereo" : "mono",
  654                         (val & 0x0080) ? ", nicam 2nd mono" : "",
  655                         (val & 0x0100) ? ", bilingual/SAP"  : "");
  656                 newstereo = VIDEO_SOUND_MONO;
  657                 if (val & 0x0040) newstereo |= VIDEO_SOUND_STEREO;
  658                 if (val & 0x0100) newstereo |= VIDEO_SOUND_LANG1;
  659                 break;
  660         }
  661         if (newstereo != msp->stereo) {
  662                 update = 1;
  663                 dprintk("msp34xx: watch: stereo %d => %d\n",
  664                         msp->stereo,newstereo);
  665                 msp->stereo   = newstereo;
  666         }
  667         if (newnicam != msp->nicam_on) {
  668                 update = 1;
  669                 dprintk("msp34xx: watch: nicam %d => %d\n",
  670                         msp->nicam_on,newnicam);
  671                 msp->nicam_on = newnicam;
  672         }
  673         return update;
  674 }
  675 
  676 /*
  677  * A kernel thread for msp3400 control -- we don't want to block the
  678  * in the ioctl while doing the sound carrier & stereo detect
  679  */
  680 
  681 /* stereo/multilang monitoring */
  682 static void watch_stereo(bktr_ptr_t client)
  683 {
  684         struct msp3400c *msp = (struct msp3400c*)client->msp3400c_info;
  685 
  686         if (autodetect_stereo(client)) {
  687                 if (msp->stereo & VIDEO_SOUND_STEREO)
  688                         msp3400c_setstereo(client,VIDEO_SOUND_STEREO);
  689                 else if (msp->stereo & VIDEO_SOUND_LANG1)
  690                         msp3400c_setstereo(client,VIDEO_SOUND_LANG1);
  691                 else
  692                         msp3400c_setstereo(client,VIDEO_SOUND_MONO);
  693         }
  694         if (client->stereo_once)
  695                 msp->watch_stereo = 0;
  696 
  697 }
  698 
  699 static void msp3400c_thread(void *data)
  700 {
  701         bktr_ptr_t client = data;
  702         struct msp3400c *msp = (struct msp3400c*)client->msp3400c_info;
  703         
  704         struct CARRIER_DETECT *cd;
  705         int count, max1,max2,val1,val2, val,this;
  706         
  707         dprintk("msp3400: thread started\n");
  708         
  709         mtx_lock(&Giant);
  710         for (;;) {
  711                 if (msp->rmmod)
  712                         goto done;
  713                 tsleep(msp->kthread, PRIBIO, "idle", 0);
  714                 if (msp->rmmod)
  715                         goto done;
  716                 if (msp->halt_thread) {
  717                         msp->watch_stereo = 0;
  718                         msp->halt_thread = 0;
  719                         continue;
  720                 }
  721                 
  722                 if (VIDEO_MODE_RADIO == msp->norm ||
  723                     MSP_MODE_EXTERN  == msp->mode)
  724                         continue;  /* nothing to do */
  725                 
  726                 msp->active = 1;
  727                 
  728                 if (msp->watch_stereo) {
  729                         watch_stereo(client);
  730                         msp->active = 0;
  731                         continue;
  732                 }
  733 
  734                 /* some time for the tuner to sync */
  735                 tsleep(msp->kthread, PRIBIO, "tuner sync", hz/2);
  736                 
  737         restart:
  738                 if (VIDEO_MODE_RADIO == msp->norm ||
  739                     MSP_MODE_EXTERN  == msp->mode)
  740                         continue;  /* nothing to do */
  741                 msp->restart = 0;
  742                 msp3400c_setvolume(client, msp->muted, 0, 0);
  743                 msp3400c_setmode(client, MSP_MODE_AM_DETECT /* +1 */ );
  744                 val1 = val2 = 0;
  745                 max1 = max2 = -1;
  746                 msp->watch_stereo = 0;
  747 
  748                 /* carrier detect pass #1 -- main carrier */
  749                 cd = carrier_detect_main; count = CARRIER_COUNT(carrier_detect_main);
  750 
  751                 if (client->amsound && (msp->norm == VIDEO_MODE_SECAM)) {
  752                         /* autodetect doesn't work well with AM ... */
  753                         max1 = 3;
  754                         count = 0;
  755                         dprintk("msp3400: AM sound override\n");
  756                 }
  757                 
  758                 for (this = 0; this < count; this++) {
  759                         msp3400c_setcarrier(client, cd[this].cdo,cd[this].cdo);
  760 
  761                         tsleep(msp->kthread, PRIBIO, "carrier detect", hz/100);
  762                         
  763                         if (msp->restart)
  764                                 msp->restart = 0;
  765 
  766                         val = msp3400c_read(client, I2C_MSP3400C_DFP, 0x1b);
  767                         if (val > 32767)
  768                                 val -= 65536;
  769                         if (val1 < val)
  770                                 val1 = val, max1 = this;
  771                         dprintk("msp3400: carrier1 val: %5d / %s\n", val,cd[this].name);
  772                 }
  773         
  774                 /* carrier detect pass #2 -- second (stereo) carrier */
  775                 switch (max1) {
  776                 case 1: /* 5.5 */
  777                         cd = carrier_detect_55; count = CARRIER_COUNT(carrier_detect_55);
  778                         break;
  779                 case 3: /* 6.5 */
  780                         cd = carrier_detect_65; count = CARRIER_COUNT(carrier_detect_65);
  781                         break;
  782                 case 0: /* 4.5 */
  783                 case 2: /* 6.0 */
  784                 default:
  785                         cd = NULL; count = 0;
  786                         break;
  787                 }
  788                 
  789                 if (client->amsound && (msp->norm == VIDEO_MODE_SECAM)) {
  790                         /* autodetect doesn't work well with AM ... */
  791                         cd = NULL; count = 0; max2 = 0;
  792                 }
  793                 for (this = 0; this < count; this++) {
  794                         msp3400c_setcarrier(client, cd[this].cdo,cd[this].cdo);
  795 
  796                         tsleep(msp->kthread, PRIBIO, "carrier detection", hz/100);
  797                         if (msp->restart)
  798                                 goto restart;
  799 
  800                         val = msp3400c_read(client, I2C_MSP3400C_DFP, 0x1b);
  801                         if (val > 32767)
  802                                 val -= 65536;
  803                         if (val2 < val)
  804                                 val2 = val, max2 = this;
  805                         dprintk("msp3400: carrier2 val: %5d / %s\n", val,cd[this].name);
  806                 }
  807 
  808                 /* programm the msp3400 according to the results */
  809                 msp->main   = carrier_detect_main[max1].cdo;
  810                 switch (max1) {
  811                 case 1: /* 5.5 */
  812                         if (max2 == 0) {
  813                                 /* B/G FM-stereo */
  814                                 msp->second = carrier_detect_55[max2].cdo;
  815                                 msp3400c_setmode(client, MSP_MODE_FM_TERRA);
  816                                 msp->nicam_on = 0;
  817                                 /* XXX why mono? this probably can do stereo... - Alex*/
  818                                 msp3400c_setstereo(client, VIDEO_SOUND_MONO);
  819                                 msp->watch_stereo = 1;
  820                         } else if (max2 == 1 && msp->nicam) {
  821                                 /* B/G NICAM */
  822                                 msp->second = carrier_detect_55[max2].cdo;
  823                                 msp3400c_setmode(client, MSP_MODE_FM_NICAM1);
  824                                 msp->nicam_on = 1;
  825                                 msp3400c_setcarrier(client, msp->second, msp->main);
  826                                 msp->watch_stereo = 1;
  827                         } else {
  828                                 goto no_second;
  829                         }
  830                         break;
  831                 case 2: /* 6.0 */
  832                         /* PAL I NICAM */
  833                         msp->second = MSP_CARRIER(6.552);
  834                         msp3400c_setmode(client, MSP_MODE_FM_NICAM2);
  835                         msp->nicam_on = 1;
  836                         msp3400c_setcarrier(client, msp->second, msp->main);
  837                         msp->watch_stereo = 1;
  838                         break;
  839                 case 3: /* 6.5 */
  840                         if (max2 == 1 || max2 == 2) {
  841                                 /* D/K FM-stereo */
  842                                 msp->second = carrier_detect_65[max2].cdo;
  843                                 msp3400c_setmode(client, MSP_MODE_FM_TERRA);
  844                                 msp->nicam_on = 0;
  845                                 msp3400c_setstereo(client, VIDEO_SOUND_MONO);
  846                                 msp->watch_stereo = 1;
  847                         } else if (max2 == 0 &&
  848                                    msp->norm == VIDEO_MODE_SECAM) {
  849                                 /* L NICAM or AM-mono */
  850                                 msp->second = carrier_detect_65[max2].cdo;
  851                                 msp3400c_setmode(client, MSP_MODE_AM_NICAM);
  852                                 msp->nicam_on = 0;
  853                                 msp3400c_setstereo(client, VIDEO_SOUND_MONO);
  854                                 msp3400c_setcarrier(client, msp->second, msp->main);
  855                                 /* volume prescale for SCART (AM mono input) */
  856                                 msp3400c_write(client,I2C_MSP3400C_DFP, 0x000d, 0x1900);
  857                                 msp->watch_stereo = 1;
  858                         } else if (max2 == 0 && msp->nicam) {
  859                                 /* D/K NICAM */
  860                                 msp->second = carrier_detect_65[max2].cdo;
  861                                 msp3400c_setmode(client, MSP_MODE_FM_NICAM1);
  862                                 msp->nicam_on = 1;
  863                                 msp3400c_setcarrier(client, msp->second, msp->main);
  864                                 msp->watch_stereo = 1;
  865                         } else {
  866                                 goto no_second;
  867                         }
  868                         break;
  869                 case 0: /* 4.5 */
  870                 default:
  871                 no_second:
  872                         msp->second = carrier_detect_main[max1].cdo;
  873                         msp3400c_setmode(client, MSP_MODE_FM_TERRA);
  874                         msp->nicam_on = 0;
  875                         msp3400c_setcarrier(client, msp->second, msp->main);
  876                         msp->stereo = VIDEO_SOUND_MONO;
  877                         msp3400c_setstereo(client, VIDEO_SOUND_MONO);
  878                         break;
  879                 }
  880 
  881                 if (msp->watch_stereo)
  882                         watch_stereo(client);
  883 
  884                 /* unmute + restore dfp registers */
  885                 msp3400c_setvolume(client, msp->muted, msp->left, msp->right);
  886                 msp3400c_restore_dfp(client);
  887 
  888                 if (bootverbose)
  889                         msp3400c_print_mode(msp);
  890                 
  891                 msp->active = 0;
  892         }
  893 
  894 done:
  895         dprintk("msp3400: thread: exit\n");
  896         msp->active = 0;
  897 
  898         msp->kthread = NULL;
  899         wakeup(&msp->kthread);
  900         mtx_unlock(&Giant);
  901 
  902         kthread_exit(0);
  903 }
  904 
  905 /* ----------------------------------------------------------------------- */
  906 /* this one uses the automatic sound standard detection of newer           */
  907 /* msp34xx chip versions                                                   */
  908 
  909 static struct MODES {
  910         int retval;
  911         int main, second;
  912         char *name;
  913 } modelist[] = {
  914         { 0x0000, 0, 0, "ERROR" },
  915         { 0x0001, 0, 0, "autodetect start" },
  916         { 0x0002, MSP_CARRIER(4.5), MSP_CARRIER(4.72), "4.5/4.72  M Dual FM-Stereo" },
  917         { 0x0003, MSP_CARRIER(5.5), MSP_CARRIER(5.7421875), "5.5/5.74  B/G Dual FM-Stereo" },
  918         { 0x0004, MSP_CARRIER(6.5), MSP_CARRIER(6.2578125), "6.5/6.25  D/K1 Dual FM-Stereo" },
  919         { 0x0005, MSP_CARRIER(6.5), MSP_CARRIER(6.7421875), "6.5/6.74  D/K2 Dual FM-Stereo" },
  920         { 0x0006, MSP_CARRIER(6.5), MSP_CARRIER(6.5), "6.5  D/K FM-Mono (HDEV3)" },
  921         { 0x0008, MSP_CARRIER(5.5), MSP_CARRIER(5.85), "5.5/5.85  B/G NICAM FM" },
  922         { 0x0009, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85  L NICAM AM" },
  923         { 0x000a, MSP_CARRIER(6.0), MSP_CARRIER(6.55), "6.0/6.55  I NICAM FM" },
  924         { 0x000b, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85  D/K NICAM FM" },
  925         { 0x000c, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85  D/K NICAM FM (HDEV2)" },
  926         { 0x0020, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5  M BTSC-Stereo" },
  927         { 0x0021, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5  M BTSC-Mono + SAP" },
  928         { 0x0030, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5  M EIA-J Japan Stereo" },
  929         { 0x0040, MSP_CARRIER(10.7), MSP_CARRIER(10.7), "10.7  FM-Stereo Radio" },
  930         { 0x0050, MSP_CARRIER(6.5), MSP_CARRIER(6.5), "6.5  SAT-Mono" },
  931         { 0x0051, MSP_CARRIER(7.02), MSP_CARRIER(7.20), "7.02/7.20  SAT-Stereo" },
  932         { 0x0060, MSP_CARRIER(7.2), MSP_CARRIER(7.2), "7.2  SAT ADR" },
  933         {     -1, 0, 0, NULL }, /* EOF */
  934 };
  935  
  936 static void msp3410d_thread(void *data)
  937 {
  938         bktr_ptr_t client = data;
  939         struct msp3400c *msp = (struct msp3400c*)client->msp3400c_info;
  940         int mode,val,i,std;
  941         int timo = 0;
  942     
  943         dprintk("msp3410: thread started\n");
  944                 
  945         mtx_lock(&Giant);
  946         for (;;) {
  947                 if (msp->rmmod)
  948                         goto done;
  949                 if (!msp->watch_stereo)
  950                         timo = 0;
  951                 else
  952                         timo = 10*hz;
  953                 tsleep(msp->kthread, PRIBIO, "idle", timo);
  954                 if (msp->rmmod)
  955                         goto done;
  956                 if (msp->halt_thread) {
  957                         msp->watch_stereo = 0;
  958                         msp->halt_thread = 0;
  959                         dprintk("msp3410: thread halted\n");
  960                         continue;
  961                 }
  962                 
  963                 if (msp->mode == MSP_MODE_EXTERN)
  964                         continue;
  965                 
  966                 msp->active = 1;
  967                 
  968                 if (msp->watch_stereo) {
  969                         watch_stereo(client);
  970                         msp->active = 0;
  971                         continue;
  972                 }
  973         
  974                 /* some time for the tuner to sync */
  975                 tsleep(msp->kthread, PRIBIO, "tuner sync", hz/2);
  976 
  977         restart:
  978                 if (msp->mode == MSP_MODE_EXTERN)
  979                         continue;
  980                 msp->restart = 0;
  981                 msp->watch_stereo = 0;
  982 
  983                 /* put into sane state (and mute) */
  984                 msp3400c_reset(client);
  985 
  986                 /* start autodetect */
  987                 switch (msp->norm) {
  988                 case VIDEO_MODE_PAL:
  989                         mode = 0x1003;
  990                         std  = 1;
  991                         break;
  992                 case VIDEO_MODE_NTSC:  /* BTSC */
  993                         mode = 0x2003;
  994                         std  = 0x0020;
  995                         break;
  996                 case VIDEO_MODE_SECAM: 
  997                         mode = 0x0003;
  998                         std  = 1;
  999                         break;
 1000                 case VIDEO_MODE_RADIO: 
 1001                         mode = 0x0003;
 1002                         std  = 0x0040;
 1003                         break;
 1004                 default:
 1005                         mode = 0x0003;
 1006                         std  = 1;
 1007                         break;
 1008                 }
 1009                 msp3400c_write(client, I2C_MSP3400C_DEM, 0x30, mode);
 1010                 msp3400c_write(client, I2C_MSP3400C_DEM, 0x20, std);
 1011 
 1012                 if (bootverbose) {
 1013                         int i;
 1014                         for (i = 0; modelist[i].name != NULL; i++)
 1015                                 if (modelist[i].retval == std)
 1016                                         break;
 1017                         dprintk("msp3410: setting mode: %s (0x%04x)\n",
 1018                                modelist[i].name ? modelist[i].name : "unknown",std);
 1019                 }
 1020 
 1021                 if (std != 1) {
 1022                         /* programmed some specific mode */
 1023                         val = std;
 1024                 } else {
 1025                         /* triggered autodetect */
 1026                         for (;;) {
 1027                                 tsleep(msp->kthread, PRIBIO, "autodetection", hz/10);
 1028                                 if (msp->restart)
 1029                                         goto restart;
 1030 
 1031                                 /* check results */
 1032                                 val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x7e);
 1033                                 if (val < 0x07ff)
 1034                                         break;
 1035                                 dprintk("msp3410: detection still in progress\n");
 1036                         }
 1037                 }
 1038                 for (i = 0; modelist[i].name != NULL; i++)
 1039                         if (modelist[i].retval == val)
 1040                                 break;
 1041                 dprintk("msp3410: current mode: %s (0x%04x)\n",
 1042                         modelist[i].name ? modelist[i].name : "unknown",
 1043                         val);
 1044                 msp->main   = modelist[i].main;
 1045                 msp->second = modelist[i].second;
 1046 
 1047                 if (client->amsound && (msp->norm == VIDEO_MODE_SECAM) && (val != 0x0009)) {
 1048                         /* autodetection has failed, let backup */
 1049                         dprintk("msp3410: autodetection failed, switching to backup mode: %s (0x%04x)\n",
 1050                                 modelist[8].name ? modelist[8].name : "unknown",val);
 1051                         val = 0x0009;
 1052                         msp3400c_write(client, I2C_MSP3400C_DEM, 0x20, val);
 1053                 }
 1054 
 1055                 /* set various prescales */
 1056                 msp3400c_write(client, I2C_MSP3400C_DFP, 0x0d, 0x1900); /* scart */
 1057                 msp3400c_write(client, I2C_MSP3400C_DFP, 0x0e, 0x2403); /* FM */
 1058                 msp3400c_write(client, I2C_MSP3400C_DFP, 0x10, 0x5a00); /* nicam */
 1059 
 1060                 /* set stereo */
 1061                 switch (val) {
 1062                 case 0x0008: /* B/G NICAM */
 1063                 case 0x000a: /* I NICAM */
 1064                         if (val == 0x0008)
 1065                                 msp->mode = MSP_MODE_FM_NICAM1;
 1066                         else
 1067                                 msp->mode = MSP_MODE_FM_NICAM2;
 1068                         /* just turn on stereo */
 1069                         msp->stereo = VIDEO_SOUND_STEREO;
 1070                         msp->nicam_on = 1;
 1071                         msp->watch_stereo = 1;
 1072                         msp3400c_setstereo(client,VIDEO_SOUND_STEREO);
 1073                         break;
 1074                 case 0x0009:                    
 1075                         msp->mode = MSP_MODE_AM_NICAM;
 1076                         msp->stereo = VIDEO_SOUND_MONO;
 1077                         msp->nicam_on = 1;
 1078                         msp3400c_setstereo(client,VIDEO_SOUND_MONO);
 1079                         msp->watch_stereo = 1;
 1080                         break;
 1081                 case 0x0020: /* BTSC */
 1082                         /* just turn on stereo */
 1083                         msp->mode   = MSP_MODE_BTSC;
 1084                         msp->stereo = VIDEO_SOUND_STEREO;
 1085                         msp->nicam_on = 0;
 1086                         msp->watch_stereo = 1;
 1087                         msp3400c_setstereo(client,VIDEO_SOUND_STEREO);
 1088                         break;
 1089                 case 0x0040: /* FM radio */
 1090                         msp->mode   = MSP_MODE_FM_RADIO;
 1091                         msp->stereo = VIDEO_SOUND_STEREO;
 1092                         msp->nicam_on = 0;
 1093                         msp->watch_stereo = 0;
 1094                         /* scart routing */
 1095                         msp3400c_set_scart(client,SCART_IN2,0);
 1096                         msp3400c_write(client,I2C_MSP3400C_DFP, 0x08, 0x0220);
 1097                         msp3400c_write(client,I2C_MSP3400C_DFP, 0x09, 0x0220);
 1098                         msp3400c_write(client,I2C_MSP3400C_DFP, 0x0b, 0x0220);
 1099                         break;
 1100                 case 0x0003:
 1101                         msp->mode   = MSP_MODE_FM_TERRA;
 1102                         msp->stereo = VIDEO_SOUND_STEREO;
 1103                         msp->nicam_on = 0;
 1104                         msp->watch_stereo = 1;
 1105                         msp3400c_setstereo(client,VIDEO_SOUND_STEREO);
 1106                         break;
 1107                 }
 1108 
 1109                 if (msp->watch_stereo)
 1110                         watch_stereo(client);
 1111                 
 1112                 /* unmute + restore dfp registers */
 1113                 msp3400c_setbass(client, msp->bass);
 1114                 msp3400c_settreble(client, msp->treble);
 1115                 msp3400c_setvolume(client, msp->muted, msp->left, msp->right);
 1116                 msp3400c_restore_dfp(client);
 1117 
 1118                 msp->active = 0;
 1119         }
 1120 
 1121 done:
 1122         dprintk("msp3410: thread: exit\n");
 1123         msp->active = 0;
 1124 
 1125         msp->kthread = NULL;
 1126         wakeup(&msp->kthread);
 1127         mtx_unlock(&Giant);
 1128 
 1129         kthread_exit(0);
 1130 }
 1131 
 1132 int msp_attach(bktr_ptr_t bktr)
 1133 {
 1134         struct msp3400c *msp;
 1135         int              rev1,rev2,i;
 1136         int              err;
 1137         char             buf[20];
 1138 
 1139         msp = (struct msp3400c *) malloc(sizeof(struct msp3400c), M_DEVBUF, M_NOWAIT);
 1140         if (msp == NULL)
 1141                 return ENOMEM;
 1142         bktr->msp3400c_info = msp;
 1143         
 1144         memset(msp,0,sizeof(struct msp3400c));
 1145         msp->left   = 65535;
 1146         msp->right  = 65535;
 1147         msp->bass   = 32768;
 1148         msp->treble = 32768;
 1149         msp->input  = -1;
 1150         msp->threaddesc = malloc(15 * sizeof(char), M_DEVBUF, M_NOWAIT);
 1151         if (msp->threaddesc == NULL) {
 1152                 free(msp, M_DEVBUF);
 1153                 return ENOMEM;
 1154         }
 1155         snprintf(msp->threaddesc, 14, "%s_msp34xx_thread", bktr->bktr_xname);
 1156 
 1157         for (i = 0; i < DFP_COUNT; i++)
 1158                 msp->dfp_regs[i] = -1;
 1159         
 1160         msp3400c_reset(bktr);
 1161         
 1162         rev1 = msp3400c_read(bktr, I2C_MSP3400C_DFP, 0x1e);
 1163         if (-1 != rev1)
 1164                 rev2 = msp3400c_read(bktr, I2C_MSP3400C_DFP, 0x1f);
 1165         if ((-1 == rev1) || (0 == rev1 && 0 == rev2)) {
 1166                 free(msp->threaddesc, M_DEVBUF);
 1167                 free(msp, M_DEVBUF);
 1168                 bktr->msp3400c_info = NULL;
 1169                 printf("%s: msp3400: error while reading chip version\n", bktr_name(bktr));
 1170                 return ENXIO;
 1171         }
 1172 
 1173 #if 0
 1174         /* this will turn on a 1kHz beep - might be useful for debugging... */
 1175         msp3400c_write(bktr,I2C_MSP3400C_DFP, 0x0014, 0x1040);
 1176 #endif
 1177 
 1178         sprintf(buf,"MSP34%02d%c-%c%d",
 1179                 (rev2>>8)&0xff, (rev1&0xff)+'@', ((rev1>>8)&0xff)+'@', rev2&0x1f);
 1180         msp->nicam = (((rev2>>8)&0xff) != 00) ? 1 : 0;
 1181         
 1182         if (bktr->mspsimple == -1) {
 1183                 /* default mode */
 1184                 /* msp->simple = (((rev2>>8)&0xff) == 0) ? 0 : 1; */
 1185                 msp->simple = ((rev1&0xff)+'@' > 'C');
 1186         } else {
 1187                 /* use kenv value */
 1188                 msp->simple = bktr->mspsimple;
 1189         }
 1190 
 1191         /* hello world :-) */
 1192         if (bootverbose) {
 1193                 printf("%s: msp34xx: init: chip=%s", bktr_name(bktr), buf);
 1194                 if (msp->nicam)
 1195                         printf(", has NICAM support");
 1196                 printf("\n");
 1197         }
 1198 
 1199         /* startup control thread */
 1200         err = kthread_create(msp->simple ? msp3410d_thread : msp3400c_thread,
 1201                              bktr, &msp->kthread, (RFFDG | RFPROC), 0,
 1202                              msp->threaddesc);
 1203         if (err) {
 1204                 printf("%s: Error returned by kthread_create: %d", bktr_name(bktr), err);
 1205                 free(msp->threaddesc, M_DEVBUF);
 1206                 free(msp, M_DEVBUF);
 1207                 bktr->msp3400c_info = NULL;
 1208                 return ENXIO;
 1209         }
 1210         wakeup(msp->kthread);
 1211 
 1212         /* done */
 1213         return 0;
 1214 }
 1215 
 1216 int msp_detach(bktr_ptr_t client)
 1217 {
 1218         struct msp3400c *msp  = (struct msp3400c*)client->msp3400c_info;
 1219         
 1220         /* shutdown control thread */
 1221         if (msp->kthread) 
 1222         {
 1223                 /* XXX mutex lock required */
 1224                 mtx_lock(&Giant);
 1225                 msp->rmmod = 1;
 1226                 msp->watch_stereo = 0;
 1227                 wakeup(msp->kthread);
 1228 
 1229                 while (msp->kthread)
 1230                         tsleep(&msp->kthread, PRIBIO, "wait for kthread", hz/10);
 1231                 mtx_unlock(&Giant);
 1232         }
 1233 
 1234         if (client->msp3400c_info != NULL) {
 1235                 free(client->msp3400c_info, M_DEVBUF);
 1236                 client->msp3400c_info = NULL;
 1237         }
 1238 
 1239         msp3400c_reset(client);
 1240         
 1241         return 0;
 1242 }
 1243 
 1244 void msp_wake_thread(bktr_ptr_t client)
 1245 {
 1246         struct msp3400c *msp  = (struct msp3400c*)client->msp3400c_info;
 1247 
 1248         msp3400c_setvolume(client,msp->muted,0,0);
 1249         msp->watch_stereo=0;
 1250         if (msp->active)
 1251                 msp->restart = 1;
 1252         wakeup(msp->kthread);
 1253 }
 1254 
 1255 void msp_halt_thread(bktr_ptr_t client)
 1256 {
 1257         struct msp3400c *msp  = (struct msp3400c*)client->msp3400c_info;
 1258 
 1259         msp3400c_setvolume(client,msp->muted,0,0);
 1260         if (msp->active)
 1261                 msp->restart = 1;
 1262         msp->halt_thread = 1;
 1263         wakeup(msp->kthread);
 1264 }
 1265 #endif /* BKTR_NEW_MSP34XX_DRIVER */

Cache object: 89fc854d18545eb01e337e8a4050f26c


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.