Main Page | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

vwsnd.c

Go to the documentation of this file.
00001 /*
00002  * Sound driver for Silicon Graphics 320 and 540 Visual Workstations'
00003  * onboard audio.  See notes in ../../Documentation/sound/vwsnd .
00004  *
00005  * Copyright 1999 Silicon Graphics, Inc.  All rights reserved.
00006  *
00007  * This program is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 2 of the License, or
00010  * (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00020  */
00021 
00022 #undef VWSND_DEBUG                      /* define for debugging */
00023 
00024 /*
00025  * XXX to do -
00026  *
00027  *      External sync.
00028  *      Rename swbuf, hwbuf, u&i, hwptr&swptr to something rational.
00029  *      Bug - if select() called before read(), pcm_setup() not called.
00030  *      Bug - output doesn't stop soon enough if process killed.
00031  */
00032 
00033 /*
00034  * Things to test -
00035  *
00036  *      Will readv/writev work?  Write a test.
00037  *
00038  *      insmod/rmmod 100 million times.
00039  *
00040  *      Run I/O until int ptrs wrap around (roughly 6.2 hours @ DAT
00041  *      rate).
00042  *
00043  *      Concurrent threads banging on mixer simultaneously, both UP
00044  *      and SMP kernels.  Especially, watch for thread A changing
00045  *      OUTSRC while thread B changes gain -- both write to the same
00046  *      ad1843 register.
00047  *
00048  *      What happens if a client opens /dev/audio then forks?
00049  *      Do two procs have /dev/audio open?  Test.
00050  *
00051  *      Pump audio through the CD, MIC and line inputs and verify that
00052  *      they mix/mute into the output.
00053  *
00054  *      Apps:
00055  *              amp
00056  *              mpg123
00057  *              x11amp
00058  *              mxv
00059  *              kmedia
00060  *              esound
00061  *              need more input apps
00062  *
00063  *      Run tests while bombarding with signals.  setitimer(2) will do it...  */
00064 
00065 /*
00066  * This driver is organized in nine sections.
00067  * The nine sections are:
00068  *
00069  *      debug stuff
00070  *      low level lithium access
00071  *      high level lithium access
00072  *      AD1843 access
00073  *      PCM I/O
00074  *      audio driver
00075  *      mixer driver
00076  *      probe/attach/unload
00077  *      initialization and loadable kernel module interface
00078  *
00079  * That is roughly the order of increasing abstraction, so forward
00080  * dependencies are minimal.
00081  */
00082 
00083 /*
00084  * Locking Notes
00085  *
00086  *      INC_USE_COUNT and DEC_USE_COUNT keep track of the number of
00087  *      open descriptors to this driver.  When the driver is compiled
00088  *      as a module, they call MOD_{INC,DEC}_USE_COUNT; otherwise they
00089  *      bump vwsnd_use_count.  The global device list, vwsnd_dev_list,
00090  *      is immutable when the IN_USE is true.
00091  *
00092  *      devc->open_lock is a semaphore that is used to enforce the
00093  *      single reader/single writer rule for /dev/audio.  The rule is
00094  *      that each device may have at most one reader and one writer.
00095  *      Open will block until the previous client has closed the
00096  *      device, unless O_NONBLOCK is specified.
00097  *
00098  *      The semaphore devc->io_sema serializes PCM I/O syscalls.  This
00099  *      is unnecessary in Linux 2.2, because the kernel lock
00100  *      serializes read, write, and ioctl globally, but it's there,
00101  *      ready for the brave, new post-kernel-lock world.
00102  *
00103  *      Locking between interrupt and baselevel is handled by the
00104  *      "lock" spinlock in vwsnd_port (one lock each for read and
00105  *      write).  Each half holds the lock just long enough to see what
00106  *      area it owns and update its pointers.  See pcm_output() and
00107  *      pcm_input() for most of the gory stuff.
00108  *
00109  *      devc->mix_sema serializes all mixer ioctls.  This is also
00110  *      redundant because of the kernel lock.
00111  *
00112  *      The lowest level lock is lith->lithium_lock.  It is a
00113  *      spinlock which is held during the two-register tango of
00114  *      reading/writing an AD1843 register.  See
00115  *      li_{read,write}_ad1843_reg().
00116  */
00117 
00118 /*
00119  * Sample Format Notes
00120  *
00121  *      Lithium's DMA engine has two formats: 16-bit 2's complement
00122  *      and 8-bit unsigned .  16-bit transfers the data unmodified, 2
00123  *      bytes per sample.  8-bit unsigned transfers 1 byte per sample
00124  *      and XORs each byte with 0x80.  Lithium can input or output
00125  *      either mono or stereo in either format.
00126  *
00127  *      The AD1843 has four formats: 16-bit 2's complement, 8-bit
00128  *      unsigned, 8-bit mu-Law and 8-bit A-Law.
00129  *
00130  *      This driver supports five formats: AFMT_S8, AFMT_U8,
00131  *      AFMT_MU_LAW, AFMT_A_LAW, and AFMT_S16_LE.
00132  *
00133  *      For AFMT_U8 output, we keep the AD1843 in 16-bit mode, and
00134  *      rely on Lithium's XOR to translate between U8 and S8.
00135  *
00136  *      For AFMT_S8, AFMT_MU_LAW and AFMT_A_LAW output, we have to XOR
00137  *      the 0x80 bit in software to compensate for Lithium's XOR.
00138  *      This happens in pcm_copy_{in,out}().
00139  */
00140 
00141 #include <linux/config.h>
00142 #include <linux/module.h>
00143 #include <linux/stddef.h>
00144 #include <asm/fixmap.h>
00145 #include <asm/sgi-cobalt.h>
00146 #include <asm/spinlock.h>
00147 
00148 #include "sound_config.h"
00149 
00150 /*****************************************************************************/
00151 /* debug stuff */
00152 
00153 #ifdef VWSND_DEBUG
00154 
00155 #include <linux/interrupt.h>            /* for in_interrupt() */
00156 
00157 static int shut_up = 1;
00158 
00159 /*
00160  * dbgassert - called when an assertion fails.
00161  */
00162 
00163 static void dbgassert(const char *fcn, int line, const char *expr)
00164 {
00165         if (in_interrupt())
00166                 panic("ASSERTION FAILED IN INTERRUPT, %s:%s:%d %s\n",
00167                       __FILE__, fcn, line, expr);
00168         else {
00169                 int x;
00170                 printk(KERN_ERR "ASSERTION FAILED, %s:%s:%d %s\n",
00171                        __FILE__, fcn, line, expr);
00172                 x = * (volatile int *) 0; /* force proc to exit */
00173         }
00174 }
00175 
00176 /*
00177  * Bunch of useful debug macros:
00178  *
00179  *      ASSERT  - print unless e nonzero (panic if in interrupt)
00180  *      DBGDO   - include arbitrary code if debugging
00181  *      DBGX    - debug print raw (w/o function name)
00182  *      DBGP    - debug print w/ function name
00183  *      DBGE    - debug print function entry
00184  *      DBGC    - debug print function call
00185  *      DBGR    - debug print function return
00186  *      DBGXV   - debug print raw when verbose
00187  *      DBGPV   - debug print when verbose
00188  *      DBGEV   - debug print function entry when verbose
00189  *      DBGRV   - debug print function return when verbose
00190  */
00191 
00192 #define ASSERT(e)      ((e) ? (void) 0 : dbgassert(__FUNCTION__, __LINE__, #e))
00193 #define DBGDO(x)            x
00194 #define DBGX(fmt, args...)  (in_interrupt() ? 0 : printk(KERN_ERR fmt, ##args))
00195 #define DBGP(fmt, args...)  (DBGX(__FUNCTION__ ": " fmt, ##args))
00196 #define DBGE(fmt, args...)  (DBGX(__FUNCTION__ fmt, ##args))
00197 #define DBGC(rtn)           (DBGP("calling %s\n", rtn))
00198 #define DBGR()              (DBGP("returning\n"))
00199 #define DBGXV(fmt, args...) (shut_up ? 0 : DBGX(fmt, ##args))
00200 #define DBGPV(fmt, args...) (shut_up ? 0 : DBGP(fmt, ##args))
00201 #define DBGEV(fmt, args...) (shut_up ? 0 : DBGE(fmt, ##args))
00202 #define DBGCV(rtn)          (shut_up ? 0 : DBGC(rtn))
00203 #define DBGRV()             (shut_up ? 0 : DBGR())
00204 
00205 #else /* !VWSND_DEBUG */
00206 
00207 #define ASSERT(e)           ((void) 0)
00208 #define DBGDO(x)            /* don't */
00209 #define DBGX(fmt, args...)  ((void) 0)
00210 #define DBGP(fmt, args...)  ((void) 0)
00211 #define DBGE(fmt, args...)  ((void) 0)
00212 #define DBGC(rtn)           ((void) 0)
00213 #define DBGR()              ((void) 0)
00214 #define DBGPV(fmt, args...) ((void) 0)
00215 #define DBGXV(fmt, args...) ((void) 0)
00216 #define DBGEV(fmt, args...) ((void) 0)
00217 #define DBGCV(rtn)          ((void) 0)
00218 #define DBGRV()             ((void) 0)
00219 
00220 #endif /* !VWSND_DEBUG */
00221 
00222 /*****************************************************************************/
00223 /* low level lithium access */
00224 
00225 /*
00226  * We need to talk to Lithium registers on three pages.  Here are
00227  * the pages' offsets from the base address (0xFF001000).
00228  */
00229 
00230 enum {
00231         LI_PAGE0_OFFSET = 0x01000 - 0x1000, /* FF001000 */
00232         LI_PAGE1_OFFSET = 0x0F000 - 0x1000, /* FF00F000 */
00233         LI_PAGE2_OFFSET = 0x10000 - 0x1000, /* FF010000 */
00234 };
00235 
00236 /* low-level lithium data */
00237 
00238 typedef struct lithium {
00239         caddr_t         page0;          /* virtual addresses */
00240         caddr_t         page1;
00241         caddr_t         page2;
00242         spinlock_t      lock;           /* protects codec and UST/MSC access */
00243 } lithium_t;
00244 
00245 /*
00246  * li_create initializes the lithium_t structure and sets up vm mappings
00247  * to access the registers.
00248  * Returns 0 on success, -errno on failure.
00249  */
00250 
00251 static int li_create(lithium_t *lith, unsigned long baseaddr)
00252 {
00253         static void li_destroy(lithium_t *);
00254 
00255         lith->lock = SPIN_LOCK_UNLOCKED;
00256         lith->page0 = ioremap_nocache(baseaddr + LI_PAGE0_OFFSET, PAGE_SIZE);
00257         lith->page1 = ioremap_nocache(baseaddr + LI_PAGE1_OFFSET, PAGE_SIZE);
00258         lith->page2 = ioremap_nocache(baseaddr + LI_PAGE2_OFFSET, PAGE_SIZE);
00259         if (!lith->page0 || !lith->page1 || !lith->page2) {
00260                 li_destroy(lith);
00261                 return -ENOMEM;
00262         }
00263         return 0;
00264 }
00265 
00266 /*
00267  * li_destroy destroys the lithium_t structure and vm mappings.
00268  */
00269 
00270 static void li_destroy(lithium_t *lith)
00271 {
00272         if (lith->page0) {
00273                 iounmap(lith->page0);
00274                 lith->page0 = NULL;
00275         }
00276         if (lith->page1) {
00277                 iounmap(lith->page1);
00278                 lith->page1 = NULL;
00279         }
00280         if (lith->page2) {
00281                 iounmap(lith->page2);
00282                 lith->page2 = NULL;
00283         }
00284 }
00285 
00286 /*
00287  * basic register accessors - read/write long/byte
00288  */
00289 
00290 static __inline__ unsigned long li_readl(lithium_t *lith, int off)
00291 {
00292         return * (volatile unsigned long *) (lith->page0 + off);
00293 }
00294 
00295 static __inline__ unsigned char li_readb(lithium_t *lith, int off)
00296 {
00297         return * (volatile unsigned char *) (lith->page0 + off);
00298 }
00299 
00300 static __inline__ void li_writel(lithium_t *lith, int off, unsigned long val)
00301 {
00302         * (volatile unsigned long *) (lith->page0 + off) = val;
00303 }
00304 
00305 static __inline__ void li_writeb(lithium_t *lith, int off, unsigned char val)
00306 {
00307         * (volatile unsigned char *) (lith->page0 + off) = val;
00308 }
00309 
00310 /*****************************************************************************/
00311 /* High Level Lithium Access */
00312 
00313 /*
00314  * Lithium DMA Notes
00315  *
00316  * Lithium has two dedicated DMA channels for audio.  They are known
00317  * as comm1 and comm2 (communication areas 1 and 2).  Comm1 is for
00318  * input, and comm2 is for output.  Each is controlled by three
00319  * registers: BASE (base address), CFG (config) and CCTL
00320  * (config/control).
00321  *
00322  * Each DMA channel points to a physically contiguous ring buffer in
00323  * main memory of up to 8 Kbytes.  (This driver always uses 8 Kb.)
00324  * There are three pointers into the ring buffer: read, write, and
00325  * trigger.  The pointers are 8 bits each.  Each pointer points to
00326  * 32-byte "chunks" of data.  The DMA engine moves 32 bytes at a time,
00327  * so there is no finer-granularity control.
00328  *
00329  * In comm1, the hardware updates the write ptr, and software updates
00330  * the read ptr.  In comm2, it's the opposite: hardware updates the
00331  * read ptr, and software updates the write ptr.  I designate the
00332  * hardware-updated ptr as the hwptr, and the software-updated ptr as
00333  * the swptr.
00334  *
00335  * The trigger ptr and trigger mask are used to trigger interrupts.
00336  * From the Lithium spec, section 5.6.8, revision of 12/15/1998:
00337  *
00338  *      Trigger Mask Value
00339  *
00340  *      A three bit wide field that represents a power of two mask
00341  *      that is used whenever the trigger pointer is compared to its
00342  *      respective read or write pointer.  A value of zero here
00343  *      implies a mask of 0xFF and a value of seven implies a mask
00344  *      0x01.  This value can be used to sub-divide the ring buffer
00345  *      into pie sections so that interrupts monitor the progress of
00346  *      hardware from section to section.
00347  *
00348  * My interpretation of that is, whenever the hw ptr is updated, it is
00349  * compared with the trigger ptr, and the result is masked by the
00350  * trigger mask.  (Actually, by the complement of the trigger mask.)
00351  * If the result is zero, an interrupt is triggered.  I.e., interrupt
00352  * if ((hwptr & ~mask) == (trptr & ~mask)).  The mask is formed from
00353  * the trigger register value as mask = (1 << (8 - tmreg)) - 1.
00354  *
00355  * In yet different words, setting tmreg to 0 causes an interrupt after
00356  * every 256 DMA chunks (8192 bytes) or once per traversal of the
00357  * ring buffer.  Setting it to 7 caues an interrupt every 2 DMA chunks
00358  * (64 bytes) or 128 times per traversal of the ring buffer.
00359  */
00360 
00361 /* Lithium register offsets and bit definitions */
00362 
00363 #define LI_HOST_CONTROLLER      0x000
00364 # define LI_HC_RESET             0x00008000
00365 # define LI_HC_LINK_ENABLE       0x00004000
00366 # define LI_HC_LINK_FAILURE      0x00000004
00367 # define LI_HC_LINK_CODEC        0x00000002
00368 # define LI_HC_LINK_READY        0x00000001
00369 
00370 #define LI_INTR_STATUS          0x010
00371 #define LI_INTR_MASK            0x014
00372 # define LI_INTR_LINK_ERR        0x00008000
00373 # define LI_INTR_COMM2_TRIG      0x00000008
00374 # define LI_INTR_COMM2_UNDERFLOW 0x00000004
00375 # define LI_INTR_COMM1_TRIG      0x00000002
00376 # define LI_INTR_COMM1_OVERFLOW  0x00000001
00377 
00378 #define LI_CODEC_COMMAND        0x018
00379 # define LI_CC_BUSY              0x00008000
00380 # define LI_CC_DIR               0x00000080
00381 #  define LI_CC_DIR_RD            LI_CC_DIR
00382 #  define LI_CC_DIR_WR          (!LI_CC_DIR)
00383 # define LI_CC_ADDR_MASK         0x0000007F
00384 
00385 #define LI_CODEC_DATA           0x01C
00386 
00387 #define LI_COMM1_BASE           0x100
00388 #define LI_COMM1_CTL            0x104
00389 # define LI_CCTL_RESET           0x80000000
00390 # define LI_CCTL_SIZE            0x70000000
00391 # define LI_CCTL_DMA_ENABLE      0x08000000
00392 # define LI_CCTL_TMASK           0x07000000 /* trigger mask */
00393 # define LI_CCTL_TPTR            0x00FF0000 /* trigger pointer */
00394 # define LI_CCTL_RPTR            0x0000FF00
00395 # define LI_CCTL_WPTR            0x000000FF
00396 #define LI_COMM1_CFG            0x108
00397 # define LI_CCFG_LOCK            0x00008000
00398 # define LI_CCFG_SLOT            0x00000070
00399 # define LI_CCFG_DIRECTION       0x00000008
00400 #  define LI_CCFG_DIR_IN        (!LI_CCFG_DIRECTION)
00401 #  define LI_CCFG_DIR_OUT         LI_CCFG_DIRECTION
00402 # define LI_CCFG_MODE            0x00000004
00403 #  define LI_CCFG_MODE_MONO     (!LI_CCFG_MODE)
00404 #  define LI_CCFG_MODE_STEREO     LI_CCFG_MODE
00405 # define LI_CCFG_FORMAT          0x00000003
00406 #  define LI_CCFG_FMT_8BIT        0x00000000
00407 #  define LI_CCFG_FMT_16BIT       0x00000001
00408 #define LI_COMM2_BASE           0x10C
00409 #define LI_COMM2_CTL            0x110
00410  /* bit definitions are the same as LI_COMM1_CTL */
00411 #define LI_COMM2_CFG            0x114
00412  /* bit definitions are the same as LI_COMM1_CFG */
00413 
00414 #define LI_UST_LOW              0x200   /* 64-bit Unadjusted System Time is */
00415 #define LI_UST_HIGH             0x204   /* microseconds since boot */
00416 
00417 #define LI_AUDIO1_UST           0x300   /* UST-MSC pairs */
00418 #define LI_AUDIO1_MSC           0x304   /* MSC (Media Stream Counter) */
00419 #define LI_AUDIO2_UST           0x308   /* counts samples actually */
00420 #define LI_AUDIO2_MSC           0x30C   /* processed as of time UST */
00421 
00422 /* 
00423  * Lithium's DMA engine operates on chunks of 32 bytes.  We call that
00424  * a DMACHUNK.
00425  */
00426 
00427 #define DMACHUNK_SHIFT 5
00428 #define DMACHUNK_SIZE (1 << DMACHUNK_SHIFT)
00429 #define BYTES_TO_CHUNKS(bytes) ((bytes) >> DMACHUNK_SHIFT)
00430 #define CHUNKS_TO_BYTES(chunks) ((chunks) << DMACHUNK_SHIFT)
00431 
00432 /*
00433  * Two convenient macros to shift bitfields into/out of position.
00434  *
00435  * Observe that (mask & -mask) is (1 << low_set_bit_of(mask)).
00436  * As long as mask is constant, we trust the compiler will change the
00437  * multipy and divide into shifts.
00438  */
00439 
00440 #define SHIFT_FIELD(val, mask) (((val) * ((mask) & -(mask))) & (mask))
00441 #define UNSHIFT_FIELD(val, mask) (((val) & (mask)) / ((mask) & -(mask)))
00442 
00443 /*
00444  * dma_chan_desc is invariant information about a Lithium
00445  * DMA channel.  There are two instances, li_comm1 and li_comm2.
00446  *
00447  * Note that the CCTL register fields are write ptr and read ptr, but what
00448  * we care about are which pointer is updated by software and which by
00449  * hardware.
00450  */
00451 
00452 typedef struct dma_chan_desc {
00453         int basereg;
00454         int cfgreg;
00455         int ctlreg;
00456         int hwptrreg;
00457         int swptrreg;
00458         int ustreg;
00459         int mscreg;
00460         unsigned long swptrmask;
00461         int ad1843_slot;
00462         int direction;                  /* LI_CCTL_DIR_IN/OUT */
00463 } dma_chan_desc_t;
00464 
00465 static const dma_chan_desc_t li_comm1 = {
00466         LI_COMM1_BASE,                  /* base register offset */
00467         LI_COMM1_CFG,                   /* config register offset */
00468         LI_COMM1_CTL,                   /* control register offset */
00469         LI_COMM1_CTL + 0,               /* hw ptr reg offset (write ptr) */
00470         LI_COMM1_CTL + 1,               /* sw ptr reg offset (read ptr) */
00471         LI_AUDIO1_UST,                  /* ust reg offset */
00472         LI_AUDIO1_MSC,                  /* msc reg offset */
00473         LI_CCTL_RPTR,                   /* sw ptr bitmask in ctlval */
00474         2,                              /* ad1843 serial slot */
00475         LI_CCFG_DIR_IN                  /* direction */
00476 };
00477 
00478 static const dma_chan_desc_t li_comm2 = {
00479         LI_COMM2_BASE,                  /* base register offset */
00480         LI_COMM2_CFG,                   /* config register offset */
00481         LI_COMM2_CTL,                   /* control register offset */
00482         LI_COMM2_CTL + 1,               /* hw ptr reg offset (read ptr) */
00483         LI_COMM2_CTL + 0,               /* sw ptr reg offset (writr ptr) */
00484         LI_AUDIO2_UST,                  /* ust reg offset */
00485         LI_AUDIO2_MSC,                  /* msc reg offset */
00486         LI_CCTL_WPTR,                   /* sw ptr bitmask in ctlval */
00487         2,                              /* ad1843 serial slot */
00488         LI_CCFG_DIR_OUT                 /* direction */
00489 };
00490 
00491 /*
00492  * dma_chan is variable information about a Lithium DMA channel.
00493  *
00494  * The desc field points to invariant information.
00495  * The lith field points to a lithium_t which is passed
00496  * to li_read* and li_write* to access the registers.
00497  * The *val fields shadow the lithium registers' contents.
00498  */
00499 
00500 typedef struct dma_chan {
00501         const dma_chan_desc_t *desc;
00502         lithium_t      *lith;
00503         unsigned long   baseval;
00504         unsigned long   cfgval;
00505         unsigned long   ctlval;
00506 } dma_chan_t;
00507 
00508 /*
00509  * ustmsc is a UST/MSC pair (Unadjusted System Time/Media Stream Counter).
00510  * UST is time in microseconds since the system booted, and MSC is a
00511  * counter that increments with every audio sample.
00512  */
00513 
00514 typedef struct ustmsc {
00515         unsigned long long ust;
00516         unsigned long msc;
00517 } ustmsc_t;
00518 
00519 /*
00520  * li_ad1843_wait waits until lithium says the AD1843 register
00521  * exchange is not busy.  Returns 0 on success, -EBUSY on timeout.
00522  *
00523  * Locking: must be called with lithium_lock held.
00524  */
00525 
00526 static int li_ad1843_wait(lithium_t *lith)
00527 {
00528         unsigned long later = jiffies + 2;
00529         while (li_readl(lith, LI_CODEC_COMMAND) & LI_CC_BUSY)
00530                 if (jiffies >= later)
00531                         return -EBUSY;
00532         return 0;
00533 }
00534 
00535 /*
00536  * li_read_ad1843_reg returns the current contents of a 16 bit AD1843 register.
00537  *
00538  * Returns unsigned register value on success, -errno on failure.
00539  */
00540 
00541 static int li_read_ad1843_reg(lithium_t *lith, int reg)
00542 {
00543         int val;
00544 
00545         ASSERT(!in_interrupt());
00546         spin_lock(&lith->lock);
00547         {
00548                 val = li_ad1843_wait(lith);
00549                 if (val == 0) {
00550                         li_writel(lith, LI_CODEC_COMMAND, LI_CC_DIR_RD | reg);
00551                         val = li_ad1843_wait(lith);
00552                 }
00553                 if (val == 0)
00554                         val = li_readl(lith, LI_CODEC_DATA);
00555         }
00556         spin_unlock(&lith->lock);
00557 
00558         DBGXV("li_read_ad1843_reg(lith=0x%p, reg=%d) returns 0x%04x\n",
00559               lith, reg, val);
00560 
00561         return val;
00562 }
00563 
00564 /*
00565  * li_write_ad1843_reg writes the specified value to a 16 bit AD1843 register.
00566  */
00567 
00568 static void li_write_ad1843_reg(lithium_t *lith, int reg, int newval)
00569 {
00570         spin_lock(&lith->lock);
00571         {
00572                 if (li_ad1843_wait(lith) == 0) {
00573                         li_writel(lith, LI_CODEC_DATA, newval);
00574                         li_writel(lith, LI_CODEC_COMMAND, LI_CC_DIR_WR | reg);
00575                 }
00576         }
00577         spin_unlock(&lith->lock);
00578 }
00579 
00580 /*
00581  * li_setup_dma calculates all the register settings for DMA in a particular
00582  * mode.  It takes too many arguments.
00583  */
00584 
00585 static void li_setup_dma(dma_chan_t *chan,
00586                          const dma_chan_desc_t *desc,
00587                          lithium_t *lith,
00588                          unsigned long buffer_paddr,
00589                          int bufshift,
00590                          int fragshift,
00591                          int channels,
00592                          int sampsize)
00593 {
00594         unsigned long mode, format;
00595         unsigned long size, tmask;
00596 
00597         DBGEV("(chan=0x%p, desc=0x%p, lith=0x%p, buffer_paddr=0x%lx, "
00598              "bufshift=%d, fragshift=%d, channels=%d, sampsize=%d)\n",
00599              chan, desc, lith, buffer_paddr,
00600              bufshift, fragshift, channels, sampsize);
00601 
00602         /* Reset the channel first. */
00603 
00604         li_writel(lith, desc->ctlreg, LI_CCTL_RESET);
00605 
00606         ASSERT(channels == 1 || channels == 2);
00607         if (channels == 2)
00608                 mode = LI_CCFG_MODE_STEREO;
00609         else
00610                 mode = LI_CCFG_MODE_MONO;
00611         ASSERT(sampsize == 1 || sampsize == 2);
00612         if (sampsize == 2)
00613                 format = LI_CCFG_FMT_16BIT;
00614         else
00615                 format = LI_CCFG_FMT_8BIT;
00616         chan->desc = desc;
00617         chan->lith = lith;
00618 
00619         /*
00620          * Lithium DMA address register takes a 40-bit physical
00621          * address, right-shifted by 8 so it fits in 32 bits.  Bit 37
00622          * must be set -- it enables cache coherence.
00623          */
00624 
00625         ASSERT(!(buffer_paddr & 0xFF));
00626         chan->baseval = (buffer_paddr >> 8) | 1 << (37 - 8);
00627 
00628         chan->cfgval = (!LI_CCFG_LOCK |
00629                         SHIFT_FIELD(desc->ad1843_slot, LI_CCFG_SLOT) |
00630                         desc->direction |
00631                         mode |
00632                         format);
00633 
00634         size = bufshift - 6;
00635         tmask = 13 - fragshift;         /* See Lithium DMA Notes above. */
00636         ASSERT(size >= 2 && size <= 7);
00637         ASSERT(tmask >= 1 && tmask <= 7);
00638         chan->ctlval = (!LI_CCTL_RESET |
00639                         SHIFT_FIELD(size, LI_CCTL_SIZE) |
00640                         !LI_CCTL_DMA_ENABLE |
00641                         SHIFT_FIELD(tmask, LI_CCTL_TMASK) |
00642                         SHIFT_FIELD(0, LI_CCTL_TPTR));
00643 
00644         DBGPV("basereg 0x%x = 0x%lx\n", desc->basereg, chan->baseval);
00645         DBGPV("cfgreg 0x%x = 0x%lx\n", desc->cfgreg, chan->cfgval);
00646         DBGPV("ctlreg 0x%x = 0x%lx\n", desc->ctlreg, chan->ctlval);
00647 
00648         li_writel(lith, desc->basereg, chan->baseval);
00649         li_writel(lith, desc->cfgreg, chan->cfgval);
00650         li_writel(lith, desc->ctlreg, chan->ctlval);
00651 
00652         DBGRV();
00653 }
00654 
00655 static void li_shutdown_dma(dma_chan_t *chan)
00656 {
00657         lithium_t *lith = chan->lith;
00658         caddr_t lith1 = lith->page1;
00659 
00660         DBGEV("(chan=0x%p)\n", chan);
00661         
00662         chan->ctlval &= ~LI_CCTL_DMA_ENABLE;
00663         DBGPV("ctlreg 0x%x = 0x%lx\n", chan->desc->ctlreg, chan->ctlval);
00664         li_writel(lith, chan->desc->ctlreg, chan->ctlval);
00665 
00666         /*
00667          * Offset 0x500 on Lithium page 1 is an undocumented,
00668          * unsupported register that holds the zero sample value.
00669          * Lithium is supposed to output zero samples when DMA is
00670          * inactive, and repeat the last sample when DMA underflows.
00671          * But it has a bug, where, after underflow occurs, the zero
00672          * sample is not reset.
00673          *
00674          * I expect this to break in a future rev of Lithium.
00675          */
00676 
00677         if (lith1 && chan->desc->direction == LI_CCFG_DIR_OUT)
00678                 * (volatile unsigned long *) (lith1 + 0x500) = 0;
00679 }
00680 
00681 /*
00682  * li_activate_dma always starts dma at the beginning of the buffer.
00683  *
00684  * N.B., these may be called from interrupt.
00685  */
00686 
00687 static __inline__ void li_activate_dma(dma_chan_t *chan)
00688 {
00689         chan->ctlval |= LI_CCTL_DMA_ENABLE;
00690         DBGPV("ctlval = 0x%lx\n", chan->ctlval);
00691         li_writel(chan->lith, chan->desc->ctlreg, chan->ctlval);
00692 }
00693 
00694 static void li_deactivate_dma(dma_chan_t *chan)
00695 {
00696         lithium_t *lith = chan->lith;
00697         caddr_t lith2 = lith->page2;
00698 
00699         chan->ctlval &= ~(LI_CCTL_DMA_ENABLE | LI_CCTL_RPTR | LI_CCTL_WPTR);
00700         DBGPV("ctlval = 0x%lx\n", chan->ctlval);
00701         DBGPV("ctlreg 0x%x = 0x%lx\n", chan->desc->ctlreg, chan->ctlval);
00702         li_writel(lith, chan->desc->ctlreg, chan->ctlval);
00703 
00704         /*
00705          * Offsets 0x98 and 0x9C on Lithium page 2 are undocumented,
00706          * unsupported registers that are internal copies of the DMA
00707          * read and write pointers.  Because of a Lithium bug, these
00708          * registers aren't zeroed correctly when DMA is shut off.  So
00709          * we whack them directly.
00710          *
00711          * I expect this to break in a future rev of Lithium.
00712          */
00713 
00714         if (lith2 && chan->desc->direction == LI_CCFG_DIR_OUT) {
00715                 * (volatile unsigned long *) (lith2 + 0x98) = 0;
00716                 * (volatile unsigned long *) (lith2 + 0x9C) = 0;
00717         }
00718 }
00719 
00720 /*
00721  * read/write the ring buffer pointers.  These routines' arguments and results
00722  * are byte offsets from the beginning of the ring buffer.
00723  */
00724 
00725 static __inline__ int li_read_swptr(dma_chan_t *chan)
00726 {
00727         const unsigned long mask = chan->desc->swptrmask;
00728 
00729         return CHUNKS_TO_BYTES(UNSHIFT_FIELD(chan->ctlval, mask));
00730 }
00731 
00732 static __inline__ int li_read_hwptr(dma_chan_t *chan)
00733 {
00734         return CHUNKS_TO_BYTES(li_readb(chan->lith, chan->desc->hwptrreg));
00735 }
00736 
00737 static __inline__ void li_write_swptr(dma_chan_t *chan, int val)
00738 {
00739         const unsigned long mask = chan->desc->swptrmask;
00740 
00741         ASSERT(!(val & ~CHUNKS_TO_BYTES(0xFF)));
00742         val = BYTES_TO_CHUNKS(val);
00743         chan->ctlval = (chan->ctlval & ~mask) | SHIFT_FIELD(val, mask);
00744         li_writeb(chan->lith, chan->desc->swptrreg, val);
00745 }
00746 
00747 /* li_read_USTMSC() returns a UST/MSC pair for the given channel. */
00748 
00749 static void li_read_USTMSC(dma_chan_t *chan, ustmsc_t *ustmsc)
00750 {
00751         lithium_t *lith = chan->lith;
00752         const dma_chan_desc_t *desc = chan->desc;
00753         unsigned long now_low, now_high0, now_high1, chan_ust;
00754 
00755         spin_lock(&lith->lock);
00756         {
00757                 /*
00758                  * retry until we do all five reads without the
00759                  * high word changing.  (High word increments
00760                  * every 2^32 microseconds, i.e., not often)
00761                  */
00762                 do {
00763                         now_high0 = li_readl(lith, LI_UST_HIGH);
00764                         now_low = li_readl(lith, LI_UST_LOW);
00765 
00766                         /*
00767                          * Lithium guarantees these two reads will be
00768                          * atomic -- ust will not increment after msc
00769                          * is read.
00770                          */
00771 
00772                         ustmsc->msc = li_readl(lith, desc->mscreg);
00773                         chan_ust = li_readl(lith, desc->ustreg);
00774 
00775                         now_high1 = li_readl(lith, LI_UST_HIGH);
00776                 } while (now_high0 != now_high1);
00777         }       
00778         spin_unlock(&lith->lock);
00779         ustmsc->ust = ((unsigned long long) now_high0 << 32 | chan_ust);
00780 }
00781 
00782 static void li_enable_interrupts(lithium_t *lith, unsigned int mask)
00783 {
00784         DBGEV("(lith=0x%p, mask=0x%x)\n", lith, mask);
00785 
00786         /* clear any already-pending interrupts. */
00787 
00788         li_writel(lith, LI_INTR_STATUS, mask);
00789 
00790         /* enable the interrupts. */
00791 
00792         mask |= li_readl(lith, LI_INTR_MASK);
00793         li_writel(lith, LI_INTR_MASK, mask);
00794 }
00795 
00796 static void li_disable_interrupts(lithium_t *lith, unsigned int mask)
00797 {
00798         unsigned int keepmask;
00799 
00800         DBGEV("(lith=0x%p, mask=0x%x)\n", lith, mask);
00801 
00802         /* disable the interrupts */
00803 
00804         keepmask = li_readl(lith, LI_INTR_MASK) & ~mask;
00805         li_writel(lith, LI_INTR_MASK, keepmask);
00806 
00807         /* clear any pending interrupts. */
00808 
00809         li_writel(lith, LI_INTR_STATUS, mask);
00810 }
00811 
00812 /* Get the interrupt status and clear all pending interrupts. */
00813 
00814 static unsigned int li_get_clear_intr_status(lithium_t *lith)
00815 {
00816         unsigned int status;
00817 
00818         status = li_readl(lith, LI_INTR_STATUS);
00819         li_writel(lith, LI_INTR_STATUS, ~0);
00820         return status & li_readl(lith, LI_INTR_MASK);
00821 }
00822 
00823 static int li_init(lithium_t *lith)
00824 {
00825         /* 1. System power supplies stabilize. */
00826 
00827         /* 2. Assert the ~RESET signal. */
00828 
00829         li_writel(lith, LI_HOST_CONTROLLER, LI_HC_RESET);
00830         udelay(1);
00831 
00832         /* 3. Deassert the ~RESET signal and enter a wait period to allow
00833            the AD1843 internal clocks and the external crystal oscillator
00834            to stabilize. */
00835 
00836         li_writel(lith, LI_HOST_CONTROLLER, LI_HC_LINK_ENABLE);
00837         udelay(1);
00838 
00839         return 0;
00840 }
00841 
00842 /*****************************************************************************/
00843 /* AD1843 access */
00844 
00845 /*
00846  * AD1843 bitfield definitions.  All are named as in the AD1843 data
00847  * sheet, with ad1843_ prepended and individual bit numbers removed.
00848  *
00849  * E.g., bits LSS0 through LSS2 become ad1843_LSS.
00850  *
00851  * Only the bitfields we need are defined.
00852  */
00853 
00854 typedef struct ad1843_bitfield {
00855         char reg;
00856         char lo_bit;
00857         char nbits;
00858 } ad1843_bitfield_t;
00859 
00860 static const ad1843_bitfield_t
00861         ad1843_PDNO   = {  0, 14,  1 }, /* Converter Power-Down Flag */
00862         ad1843_INIT   = {  0, 15,  1 }, /* Clock Initialization Flag */
00863         ad1843_RIG    = {  2,  0,  4 }, /* Right ADC Input Gain */
00864         ad1843_RMGE   = {  2,  4,  1 }, /* Right ADC Mic Gain Enable */
00865         ad1843_RSS    = {  2,  5,  3 }, /* Right ADC Source Select */
00866         ad1843_LIG    = {  2,  8,  4 }, /* Left ADC Input Gain */
00867         ad1843_LMGE   = {  2, 12,  1 }, /* Left ADC Mic Gain Enable */
00868         ad1843_LSS    = {  2, 13,  3 }, /* Left ADC Source Select */
00869         ad1843_RX1M   = {  4,  0,  5 }, /* Right Aux 1 Mix Gain/Atten */
00870         ad1843_RX1MM  = {  4,  7,  1 }, /* Right Aux 1 Mix Mute */
00871         ad1843_LX1M   = {  4,  8,  5 }, /* Left Aux 1 Mix Gain/Atten */
00872         ad1843_LX1MM  = {  4, 15,  1 }, /* Left Aux 1 Mix Mute */
00873         ad1843_RX2M   = {  5,  0,  5 }, /* Right Aux 2 Mix Gain/Atten */
00874         ad1843_RX2MM  = {  5,  7,  1 }, /* Right Aux 2 Mix Mute */
00875         ad1843_LX2M   = {  5,  8,  5 }, /* Left Aux 2 Mix Gain/Atten */
00876         ad1843_LX2MM  = {  5, 15,  1 }, /* Left Aux 2 Mix Mute */
00877         ad1843_RMCM   = {  7,  0,  5 }, /* Right Mic Mix Gain/Atten */
00878         ad1843_RMCMM  = {  7,  7,  1 }, /* Right Mic Mix Mute */
00879         ad1843_LMCM   = {  7,  8,  5 }, /* Left Mic Mix Gain/Atten */
00880         ad1843_LMCMM  = {  7, 15,  1 }, /* Left Mic Mix Mute */
00881         ad1843_HPOS   = {  8,  4,  1 }, /* Headphone Output Voltage Swing */
00882         ad1843_HPOM   = {  8,  5,  1 }, /* Headphone Output Mute */
00883         ad1843_RDA1G  = {  9,  0,  6 }, /* Right DAC1 Analog/Digital Gain */
00884         ad1843_RDA1GM = {  9,  7,  1 }, /* Right DAC1 Analog Mute */
00885         ad1843_LDA1G  = {  9,  8,  6 }, /* Left DAC1 Analog/Digital Gain */
00886         ad1843_LDA1GM = {  9, 15,  1 }, /* Left DAC1 Analog Mute */
00887         ad1843_RDA1AM = { 11,  7,  1 }, /* Right DAC1 Digital Mute */
00888         ad1843_LDA1AM = { 11, 15,  1 }, /* Left DAC1 Digital Mute */
00889         ad1843_ADLC   = { 15,  0,  2 }, /* ADC Left Sample Rate Source */
00890         ad1843_ADRC   = { 15,  2,  2 }, /* ADC Right Sample Rate Source */
00891         ad1843_DA1C   = { 15,  8,  2 }, /* DAC1 Sample Rate Source */
00892         ad1843_C1C    = { 17,  0, 16 }, /* Clock 1 Sample Rate Select */
00893         ad1843_C2C    = { 20,  0, 16 }, /* Clock 1 Sample Rate Select */
00894         ad1843_DAADL  = { 25,  4,  2 }, /* Digital ADC Left Source Select */
00895         ad1843_DAADR  = { 25,  6,  2 }, /* Digital ADC Right Source Select */
00896         ad1843_DRSFLT = { 25, 15,  1 }, /* Digital Reampler Filter Mode */
00897         ad1843_ADLF   = { 26,  0,  2 }, /* ADC Left Channel Data Format */
00898         ad1843_ADRF   = { 26,  2,  2 }, /* ADC Right Channel Data Format */
00899         ad1843_ADTLK  = { 26,  4,  1 }, /* ADC Transmit Lock Mode Select */
00900         ad1843_SCF    = { 26,  7,  1 }, /* SCLK Frequency Select */
00901         ad1843_DA1F   = { 26,  8,  2 }, /* DAC1 Data Format Select */
00902         ad1843_DA1SM  = { 26, 14,  1 }, /* DAC1 Stereo/Mono Mode Select */
00903         ad1843_ADLEN  = { 27,  0,  1 }, /* ADC Left Channel Enable */
00904         ad1843_ADREN  = { 27,  1,  1 }, /* ADC Right Channel Enable */
00905         ad1843_AAMEN  = { 27,  4,  1 }, /* Analog to Analog Mix Enable */
00906         ad1843_ANAEN  = { 27,  7,  1 }, /* Analog Channel Enable */
00907         ad1843_DA1EN  = { 27,  8,  1 }, /* DAC1 Enable */
00908         ad1843_DA2EN  = { 27,  9,  1 }, /* DAC2 Enable */
00909         ad1843_C1EN   = { 28, 11,  1 }, /* Clock Generator 1 Enable */
00910         ad1843_C2EN   = { 28, 12,  1 }, /* Clock Generator 2 Enable */
00911         ad1843_PDNI   = { 28, 15,  1 }; /* Converter Power Down */
00912 
00913 /*
00914  * The various registers of the AD1843 use three different formats for
00915  * specifying gain.  The ad1843_gain structure parameterizes the
00916  * formats.
00917  */
00918 
00919 typedef struct ad1843_gain {
00920 
00921         int     negative;               /* nonzero if gain is negative. */
00922         const ad1843_bitfield_t *lfield;
00923         const ad1843_bitfield_t *rfield;
00924 
00925 } ad1843_gain_t;
00926 
00927 static const ad1843_gain_t ad1843_gain_RECLEV
00928                                 = { 0, &ad1843_LIG,   &ad1843_RIG };
00929 static const ad1843_gain_t ad1843_gain_LINE
00930                                 = { 1, &ad1843_LX1M,  &ad1843_RX1M };
00931 static const ad1843_gain_t ad1843_gain_CD
00932                                 = { 1, &ad1843_LX2M,  &ad1843_RX2M };
00933 static const ad1843_gain_t ad1843_gain_MIC
00934                                 = { 1, &ad1843_LMCM,  &ad1843_RMCM };
00935 static const ad1843_gain_t ad1843_gain_PCM
00936                                 = { 1, &ad1843_LDA1G, &ad1843_RDA1G };
00937 
00938 /* read the current value of an AD1843 bitfield. */
00939 
00940 static int ad1843_read_bits(lithium_t *lith, const ad1843_bitfield_t *field)
00941 {
00942         int w = li_read_ad1843_reg(lith, field->reg);
00943         int val = w >> field->lo_bit & ((1 << field->nbits) - 1);
00944 
00945         DBGXV("ad1843_read_bits(lith=0x%p, field->{%d %d %d}) returns 0x%x\n",
00946               lith, field->reg, field->lo_bit, field->nbits, val);
00947 
00948         return val;
00949 }
00950 
00951 /*
00952  * write a new value to an AD1843 bitfield and return the old value.
00953  */
00954 
00955 static int ad1843_write_bits(lithium_t *lith,
00956                              const ad1843_bitfield_t *field,
00957                              int newval)
00958 {
00959         int w = li_read_ad1843_reg(lith, field->reg);
00960         int mask = ((1 << field->nbits) - 1) << field->lo_bit;
00961         int oldval = (w & mask) >> field->lo_bit;
00962         int newbits = (newval << field->lo_bit) & mask;
00963         w = (w & ~mask) | newbits;
00964         (void) li_write_ad1843_reg(lith, field->reg, w);
00965 
00966         DBGXV("ad1843_write_bits(lith=0x%p, field->{%d %d %d}, val=0x%x) "
00967               "returns 0x%x\n",
00968               lith, field->reg, field->lo_bit, field->nbits, newval,
00969               oldval);
00970 
00971         return oldval;
00972 }
00973 
00974 /*
00975  * ad1843_read_multi reads multiple bitfields from the same AD1843
00976  * register.  It uses a single read cycle to do it.  (Reading the
00977  * ad1843 requires 256 bit times at 12.288 MHz, or nearly 20
00978  * microseconds.)
00979  *
00980  * Called ike this.
00981  *
00982  *  ad1843_read_multi(lith, nfields,
00983  *                    &ad1843_FIELD1, &val1,
00984  *                    &ad1843_FIELD2, &val2, ...);
00985  */
00986 
00987 static void ad1843_read_multi(lithium_t *lith, int argcount, ...)
00988 {
00989         va_list ap;
00990         const ad1843_bitfield_t *fp;
00991         int w = 0, mask, *value, reg = -1;
00992 
00993         va_start(ap, argcount);
00994         while (--argcount >= 0) {
00995                 fp = va_arg(ap, const ad1843_bitfield_t *);
00996                 value = va_arg(ap, int *);
00997                 if (reg == -1) {
00998                         reg = fp->reg;
00999                         w = li_read_ad1843_reg(lith, reg);
01000                 }
01001                 ASSERT(reg == fp->reg);
01002                 mask = (1 << fp->nbits) - 1;
01003                 *value = w >> fp->lo_bit & mask;
01004         }
01005         va_end(ap);
01006 }
01007 
01008 /*
01009  * ad1843_write_multi stores multiple bitfields into the same AD1843
01010  * register.  It uses one read and one write cycle to do it.
01011  *
01012  * Called like this.
01013  *
01014  *  ad1843_write_multi(lith, nfields,
01015  *                     &ad1843_FIELD1, val1,
01016  *                     &ad1843_FIELF2, val2, ...);
01017  */
01018 
01019 static void ad1843_write_multi(lithium_t *lith, int argcount, ...)
01020 {
01021         va_list ap;
01022         int reg;
01023         const ad1843_bitfield_t *fp;
01024         int value;
01025         int w, m, mask, bits;
01026 
01027         mask = 0;
01028         bits = 0;
01029         reg = -1;
01030 
01031         va_start(ap, argcount);
01032         while (--argcount >= 0) {
01033                 fp = va_arg(ap, const ad1843_bitfield_t *);
01034                 value = va_arg(ap, int);
01035                 if (reg == -1)
01036                         reg = fp->reg;
01037                 ASSERT(fp->reg == reg);
01038                 m = ((1 << fp->nbits) - 1) << fp->lo_bit;
01039                 mask |= m;
01040                 bits |= (value << fp->lo_bit) & m;
01041         }
01042         va_end(ap);
01043         ASSERT(!(bits & ~mask));
01044         if (~mask & 0xFFFF)
01045                 w = li_read_ad1843_reg(lith, reg);
01046         else
01047                 w = 0;
01048         w = (w & ~mask) | bits;
01049         (void) li_write_ad1843_reg(lith, reg, w);
01050 }
01051 
01052 /*
01053  * ad1843_get_gain reads the specified register and extracts the gain value
01054  * using the supplied gain type.  It returns the gain in OSS format.
01055  */
01056 
01057 static int ad1843_get_gain(lithium_t *lith, const ad1843_gain_t *gp)
01058 {
01059         int lg, rg;
01060         unsigned short mask = (1 << gp->lfield->nbits) - 1;
01061 
01062         ad1843_read_multi(lith, 2, gp->lfield, &lg, gp->rfield, &rg);
01063         if (gp->negative) {
01064                 lg = mask - lg;
01065                 rg = mask - rg;
01066         }
01067         lg = (lg * 100 + (mask >> 1)) / mask;
01068         rg = (rg * 100 + (mask >> 1)) / mask;
01069         return lg << 0 | rg << 8;
01070 }
01071 
01072 /*
01073  * Set an audio channel's gain. Converts from OSS format to AD1843's
01074  * format.
01075  *
01076  * Returns the new gain, which may be lower than the old gain.
01077  */
01078 
01079 static int ad1843_set_gain(lithium_t *lith,
01080                            const ad1843_gain_t *gp,
01081                            int newval)
01082 {
01083         unsigned short mask = (1 << gp->lfield->nbits) - 1;
01084 
01085         int lg = newval >> 0 & 0xFF;
01086         int rg = newval >> 8;
01087         if (lg < 0 || lg > 100 || rg < 0 || rg > 100)
01088                 return -EINVAL;
01089         lg = (lg * mask + (mask >> 1)) / 100;
01090         rg = (rg * mask + (mask >> 1)) / 100;
01091         if (gp->negative) {
01092                 lg = mask - lg;
01093                 rg = mask - rg;
01094         }
01095         ad1843_write_multi(lith, 2, gp->lfield, lg, gp->rfield, rg);
01096         return ad1843_get_gain(lith, gp);
01097 }
01098 
01099 /* Returns the current recording source, in OSS format. */
01100 
01101 static int ad1843_get_recsrc(lithium_t *lith)
01102 {
01103         int ls = ad1843_read_bits(lith, &ad1843_LSS);
01104 
01105         switch (ls) {
01106         case 1:
01107                 return SOUND_MASK_MIC;
01108         case 2:
01109                 return SOUND_MASK_LINE;
01110         case 3:
01111                 return SOUND_MASK_CD;
01112         case 6:
01113                 return SOUND_MASK_PCM;
01114         default:
01115                 ASSERT(0);
01116                 return -1;
01117         }
01118 }
01119 
01120 /*
01121  * Enable/disable digital resample mode in the AD1843.
01122  *
01123  * The AD1843 requires that ADL, ADR, DA1 and DA2 be powered down
01124  * while switching modes.  So we save DA1's state (DA2's state is not
01125  * interesting), power them down, switch into/out of resample mode,
01126  * power them up, and restore state.
01127  *
01128  * This will cause audible glitches if D/A or A/D is going on, so the
01129  * driver disallows that (in mixer_write_ioctl()).
01130  *
01131  * The open question is, is this worth doing?  I'm leaving it in,
01132  * because it's written, but...
01133  */
01134 
01135 static void ad1843_set_resample_mode(lithium_t *lith, int onoff)
01136 {
01137         /* Save DA1 mute and gain (addr 9 is DA1 analog gain/attenuation) */
01138         int save_da1 = li_read_ad1843_reg(lith, 9);
01139 
01140         /* Power down A/D and D/A. */
01141         ad1843_write_multi(lith, 4,
01142                            &ad1843_DA1EN, 0,
01143                            &ad1843_DA2EN, 0,
01144                            &ad1843_ADLEN, 0,
01145                            &ad1843_ADREN, 0);
01146 
01147         /* Switch mode */
01148         ASSERT(onoff == 0 || onoff == 1);
01149         ad1843_write_bits(lith, &ad1843_DRSFLT, onoff);
01150 
01151         /* Power up A/D and D/A. */
01152         ad1843_write_multi(lith, 3,
01153                            &ad1843_DA1EN, 1,
01154                            &ad1843_ADLEN, 1,
01155                            &ad1843_ADREN, 1);
01156 
01157         /* Restore DA1 mute and gain. */
01158         li_write_ad1843_reg(lith, 9, save_da1);
01159 }
01160 
01161 /*
01162  * Set recording source.  Arg newsrc specifies an OSS channel mask.
01163  *
01164  * The complication is that when we switch into/out of loopback mode
01165  * (i.e., src = SOUND_MASK_PCM), we change the AD1843 into/out of
01166  * digital resampling mode.
01167  *
01168  * Returns newsrc on success, -errno on failure.
01169  */
01170 
01171 static int ad1843_set_recsrc(lithium_t *lith, int newsrc)
01172 {
01173         int bits;
01174         int oldbits;
01175 
01176         switch (newsrc) {
01177         case SOUND_MASK_PCM:
01178                 bits = 6;
01179                 break;
01180 
01181         case SOUND_MASK_MIC:
01182                 bits = 1;
01183                 break;
01184 
01185         case SOUND_MASK_LINE:
01186                 bits = 2;
01187                 break;
01188 
01189         case SOUND_MASK_CD:
01190                 bits = 3;
01191                 break;
01192 
01193         default:
01194                 return -EINVAL;
01195         }
01196         oldbits = ad1843_read_bits(lith, &ad1843_LSS);
01197         if (newsrc == SOUND_MASK_PCM && oldbits != 6) {
01198                 DBGP("enabling digital resample mode\n");
01199                 ad1843_set_resample_mode(lith, 1);
01200                 ad1843_write_multi(lith, 2,
01201                                    &ad1843_DAADL, 2,
01202                                    &ad1843_DAADR, 2);
01203         } else if (newsrc != SOUND_MASK_PCM && oldbits == 6) {
01204                 DBGP("disabling digital resample mode\n");
01205                 ad1843_set_resample_mode(lith, 0);
01206                 ad1843_write_multi(lith, 2,
01207                                    &ad1843_DAADL, 0,
01208                                    &ad1843_DAADR, 0);
01209         }
01210         ad1843_write_multi(lith, 2, &ad1843_LSS, bits, &ad1843_RSS, bits);
01211         return newsrc;
01212 }
01213 
01214 /*
01215  * Return current output sources, in OSS format.
01216  */
01217 
01218 static int ad1843_get_outsrc(lithium_t *lith)
01219 {
01220         int pcm, line, mic, cd;
01221 
01222         pcm  = ad1843_read_bits(lith, &ad1843_LDA1GM) ? 0 : SOUND_MASK_PCM;
01223         line = ad1843_read_bits(lith, &ad1843_LX1MM)  ? 0 : SOUND_MASK_LINE;
01224         cd   = ad1843_read_bits(lith, &ad1843_LX2MM)  ? 0 : SOUND_MASK_CD;
01225         mic  = ad1843_read_bits(lith, &ad1843_LMCMM)  ? 0 : SOUND_MASK_MIC;
01226 
01227         return pcm | line | cd | mic;
01228 }
01229 
01230 /*
01231  * Set output sources.  Arg is a mask of active sources in OSS format.
01232  *
01233  * Returns source mask on success, -errno on failure.
01234  */
01235 
01236 static int ad1843_set_outsrc(lithium_t *lith, int mask)
01237 {
01238         int pcm, line, mic, cd;
01239 
01240         if (mask & ~(SOUND_MASK_PCM | SOUND_MASK_LINE |
01241                      SOUND_MASK_CD | SOUND_MASK_MIC))
01242                 return -EINVAL;
01243         pcm  = (mask & SOUND_MASK_PCM)  ? 0 : 1;
01244         line = (mask & SOUND_MASK_LINE) ? 0 : 1;
01245         mic  = (mask & SOUND_MASK_MIC)  ? 0 : 1;
01246         cd   = (mask & SOUND_MASK_CD)   ? 0 : 1;
01247 
01248         ad1843_write_multi(lith, 2, &ad1843_LDA1GM, pcm, &ad1843_RDA1GM, pcm);
01249         ad1843_write_multi(lith, 2, &ad1843_LX1MM, line, &ad1843_RX1MM, line);
01250         ad1843_write_multi(lith, 2, &